Skip to content

Commit cd808de

Browse files
author
Miklos Szeredi
committed
ext4: support RENAME_WHITEOUT
Add whiteout support to ext4_rename(). A whiteout inode (chrdev/0,0) is created before the rename takes place. The whiteout inode is added to the old entry instead of deleting it. Signed-off-by: Miklos Szeredi <mszeredi@suse.cz>
1 parent 0d7a855 commit cd808de

1 file changed

Lines changed: 78 additions & 17 deletions

File tree

‎fs/ext4/namei.c‎

Lines changed: 78 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3190,6 +3190,39 @@ static void ext4_update_dir_count(handle_t *handle, struct ext4_renament *ent)
31903190
}
31913191
}
31923192

3193+
static struct inode *ext4_whiteout_for_rename(struct ext4_renament *ent,
3194+
int credits, handle_t **h)
3195+
{
3196+
struct inode *wh;
3197+
handle_t *handle;
3198+
int retries = 0;
3199+
3200+
/*
3201+
* for inode block, sb block, group summaries,
3202+
* and inode bitmap
3203+
*/
3204+
credits += (EXT4_MAXQUOTAS_TRANS_BLOCKS(ent->dir->i_sb) +
3205+
EXT4_XATTR_TRANS_BLOCKS + 4);
3206+
retry:
3207+
wh = ext4_new_inode_start_handle(ent->dir, S_IFCHR | WHITEOUT_MODE,
3208+
&ent->dentry->d_name, 0, NULL,
3209+
EXT4_HT_DIR, credits);
3210+
3211+
handle = ext4_journal_current_handle();
3212+
if (IS_ERR(wh)) {
3213+
if (handle)
3214+
ext4_journal_stop(handle);
3215+
if (PTR_ERR(wh) == -ENOSPC &&
3216+
ext4_should_retry_alloc(ent->dir->i_sb, &retries))
3217+
goto retry;
3218+
} else {
3219+
*h = handle;
3220+
init_special_inode(wh, wh->i_mode, WHITEOUT_DEV);
3221+
wh->i_op = &ext4_special_inode_operations;
3222+
}
3223+
return wh;
3224+
}
3225+
31933226
/*
31943227
* Anybody can rename anything with this: the permission checks are left to the
31953228
* higher-level routines.
@@ -3199,7 +3232,8 @@ static void ext4_update_dir_count(handle_t *handle, struct ext4_renament *ent)
31993232
* This comes from rename(const char *oldpath, const char *newpath)
32003233
*/
32013234
static int ext4_rename(struct inode *old_dir, struct dentry *old_dentry,
3202-
struct inode *new_dir, struct dentry *new_dentry)
3235+
struct inode *new_dir, struct dentry *new_dentry,
3236+
unsigned int flags)
32033237
{
32043238
handle_t *handle = NULL;
32053239
struct ext4_renament old = {
@@ -3214,6 +3248,9 @@ static int ext4_rename(struct inode *old_dir, struct dentry *old_dentry,
32143248
};
32153249
int force_reread;
32163250
int retval;
3251+
struct inode *whiteout = NULL;
3252+
int credits;
3253+
u8 old_file_type;
32173254

32183255
dquot_initialize(old.dir);
32193256
dquot_initialize(new.dir);
@@ -3252,11 +3289,17 @@ static int ext4_rename(struct inode *old_dir, struct dentry *old_dentry,
32523289
if (new.inode && !test_opt(new.dir->i_sb, NO_AUTO_DA_ALLOC))
32533290
ext4_alloc_da_blocks(old.inode);
32543291

3255-
handle = ext4_journal_start(old.dir, EXT4_HT_DIR,
3256-
(2 * EXT4_DATA_TRANS_BLOCKS(old.dir->i_sb) +
3257-
EXT4_INDEX_EXTRA_TRANS_BLOCKS + 2));
3258-
if (IS_ERR(handle))
3259-
return PTR_ERR(handle);
3292+
credits = (2 * EXT4_DATA_TRANS_BLOCKS(old.dir->i_sb) +
3293+
EXT4_INDEX_EXTRA_TRANS_BLOCKS + 2);
3294+
if (!(flags & RENAME_WHITEOUT)) {
3295+
handle = ext4_journal_start(old.dir, EXT4_HT_DIR, credits);
3296+
if (IS_ERR(handle))
3297+
return PTR_ERR(handle);
3298+
} else {
3299+
whiteout = ext4_whiteout_for_rename(&old, credits, &handle);
3300+
if (IS_ERR(whiteout))
3301+
return PTR_ERR(whiteout);
3302+
}
32603303

32613304
if (IS_DIRSYNC(old.dir) || IS_DIRSYNC(new.dir))
32623305
ext4_handle_sync(handle);
@@ -3284,13 +3327,26 @@ static int ext4_rename(struct inode *old_dir, struct dentry *old_dentry,
32843327
*/
32853328
force_reread = (new.dir->i_ino == old.dir->i_ino &&
32863329
ext4_test_inode_flag(new.dir, EXT4_INODE_INLINE_DATA));
3330+
3331+
old_file_type = old.de->file_type;
3332+
if (whiteout) {
3333+
/*
3334+
* Do this before adding a new entry, so the old entry is sure
3335+
* to be still pointing to the valid old entry.
3336+
*/
3337+
retval = ext4_setent(handle, &old, whiteout->i_ino,
3338+
EXT4_FT_CHRDEV);
3339+
if (retval)
3340+
goto end_rename;
3341+
ext4_mark_inode_dirty(handle, whiteout);
3342+
}
32873343
if (!new.bh) {
32883344
retval = ext4_add_entry(handle, new.dentry, old.inode);
32893345
if (retval)
32903346
goto end_rename;
32913347
} else {
32923348
retval = ext4_setent(handle, &new,
3293-
old.inode->i_ino, old.de->file_type);
3349+
old.inode->i_ino, old_file_type);
32943350
if (retval)
32953351
goto end_rename;
32963352
}
@@ -3305,10 +3361,12 @@ static int ext4_rename(struct inode *old_dir, struct dentry *old_dentry,
33053361
old.inode->i_ctime = ext4_current_time(old.inode);
33063362
ext4_mark_inode_dirty(handle, old.inode);
33073363

3308-
/*
3309-
* ok, that's it
3310-
*/
3311-
ext4_rename_delete(handle, &old, force_reread);
3364+
if (!whiteout) {
3365+
/*
3366+
* ok, that's it
3367+
*/
3368+
ext4_rename_delete(handle, &old, force_reread);
3369+
}
33123370

33133371
if (new.inode) {
33143372
ext4_dec_count(handle, new.inode);
@@ -3344,6 +3402,12 @@ static int ext4_rename(struct inode *old_dir, struct dentry *old_dentry,
33443402
brelse(old.dir_bh);
33453403
brelse(old.bh);
33463404
brelse(new.bh);
3405+
if (whiteout) {
3406+
if (retval)
3407+
drop_nlink(whiteout);
3408+
unlock_new_inode(whiteout);
3409+
iput(whiteout);
3410+
}
33473411
if (handle)
33483412
ext4_journal_stop(handle);
33493413
return retval;
@@ -3476,18 +3540,15 @@ static int ext4_rename2(struct inode *old_dir, struct dentry *old_dentry,
34763540
struct inode *new_dir, struct dentry *new_dentry,
34773541
unsigned int flags)
34783542
{
3479-
if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE))
3543+
if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
34803544
return -EINVAL;
34813545

34823546
if (flags & RENAME_EXCHANGE) {
34833547
return ext4_cross_rename(old_dir, old_dentry,
34843548
new_dir, new_dentry);
34853549
}
3486-
/*
3487-
* Existence checking was done by the VFS, otherwise "RENAME_NOREPLACE"
3488-
* is equivalent to regular rename.
3489-
*/
3490-
return ext4_rename(old_dir, old_dentry, new_dir, new_dentry);
3550+
3551+
return ext4_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
34913552
}
34923553

34933554
/*

0 commit comments

Comments
 (0)