Skip to content

Commit 2207c3c

Browse files
yangerkungregkh
authored andcommitted
ext4: fix bug for rename with RENAME_WHITEOUT
commit 6b4b8e6 upstream. We got a "deleted inode referenced" warning cross our fsstress test. The bug can be reproduced easily with following steps: cd /dev/shm mkdir test/ fallocate -l 128M img mkfs.ext4 -b 1024 img mount img test/ dd if=/dev/zero of=test/foo bs=1M count=128 mkdir test/dir/ && cd test/dir/ for ((i=0;i<1000;i++)); do touch file$i; done # consume all block cd ~ && renameat2(AT_FDCWD, /dev/shm/test/dir/file1, AT_FDCWD, /dev/shm/test/dir/dst_file, RENAME_WHITEOUT) # ext4_add_entry in ext4_rename will return ENOSPC!! cd /dev/shm/ && umount test/ && mount img test/ && ls -li test/dir/file1 We will get the output: "ls: cannot access 'test/dir/file1': Structure needs cleaning" and the dmesg show: "EXT4-fs error (device loop0): ext4_lookup:1626: inode raspberrypi#2049: comm ls: deleted inode referenced: 139" ext4_rename will create a special inode for whiteout and use this 'ino' to replace the source file's dir entry 'ino'. Once error happens latter(the error above was the ENOSPC return from ext4_add_entry in ext4_rename since all space has been consumed), the cleanup do drop the nlink for whiteout, but forget to restore 'ino' with source file. This will trigger the bug describle as above. Signed-off-by: yangerkun <yangerkun@huawei.com> Reviewed-by: Jan Kara <jack@suse.cz> Cc: stable@vger.kernel.org Fixes: cd808de ("ext4: support RENAME_WHITEOUT") Link: https://lore.kernel.org/r/20210105062857.3566-1-yangerkun@huawei.com Signed-off-by: Theodore Ts'o <tytso@mit.edu> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 15a062c commit 2207c3c

1 file changed

Lines changed: 9 additions & 8 deletions

File tree

‎fs/ext4/namei.c‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3602,9 +3602,6 @@ static int ext4_setent(handle_t *handle, struct ext4_renament *ent,
36023602
return retval2;
36033603
}
36043604
}
3605-
brelse(ent->bh);
3606-
ent->bh = NULL;
3607-
36083605
return retval;
36093606
}
36103607

@@ -3803,6 +3800,7 @@ static int ext4_rename(struct inode *old_dir, struct dentry *old_dentry,
38033800
}
38043801
}
38053802

3803+
old_file_type = old.de->file_type;
38063804
if (IS_DIRSYNC(old.dir) || IS_DIRSYNC(new.dir))
38073805
ext4_handle_sync(handle);
38083806

@@ -3830,7 +3828,6 @@ static int ext4_rename(struct inode *old_dir, struct dentry *old_dentry,
38303828
force_reread = (new.dir->i_ino == old.dir->i_ino &&
38313829
ext4_test_inode_flag(new.dir, EXT4_INODE_INLINE_DATA));
38323830

3833-
old_file_type = old.de->file_type;
38343831
if (whiteout) {
38353832
/*
38363833
* Do this before adding a new entry, so the old entry is sure
@@ -3928,15 +3925,19 @@ static int ext4_rename(struct inode *old_dir, struct dentry *old_dentry,
39283925
retval = 0;
39293926

39303927
end_rename:
3931-
brelse(old.dir_bh);
3932-
brelse(old.bh);
3933-
brelse(new.bh);
39343928
if (whiteout) {
3935-
if (retval)
3929+
if (retval) {
3930+
ext4_setent(handle, &old,
3931+
old.inode->i_ino, old_file_type);
39363932
drop_nlink(whiteout);
3933+
}
39373934
unlock_new_inode(whiteout);
39383935
iput(whiteout);
3936+
39393937
}
3938+
brelse(old.dir_bh);
3939+
brelse(old.bh);
3940+
brelse(new.bh);
39403941
if (handle)
39413942
ext4_journal_stop(handle);
39423943
return retval;

0 commit comments

Comments
 (0)