ext4: return lblk from ext4_find_entry
PATCH v2 1/3 ext4: return lblk from ext4_find_entry
This patch makes ext4_find_entry and related routines to return logical block address of the dirent block. This logical block address is used in the directory shrinking code to perform reverse lookup and verify that the lookup was successful.
--- fs/ext4/namei.c | 54 +++++++++++++++++++++++++++++-------------------- 1 file changed, 32 insertions(+), 22 deletions(-)
fs/ext4/namei.c +32 -22
@@ -295,7 +295,7 @@ static int ext4_htree_next_block(struct inode *dir, __u32 hash, __u32 *start_hash);static struct buffer_head *ext4_dx_find_entry (struct inode *dir,struct ext4_filename *fname, -struct ext4_dir_entry_2 **res_dir); +struct ext4_dir_entry_2 **res_dir,ext4_lblk_t *lblk);static int ext4_dx_add_entry (handle_t *handle,struct ext4_filename *fname,struct inode *dir,struct inode *inode);
@@ -1443,7 +1443,7 @@ static int is_dx_internal_node(struct inode *dir, ext4_lblk_t block,static struct buffer_head *__ext4_find_entry (struct inode *dir,struct ext4_filename *fname,struct ext4_dir_entry_2 **res_dir, -int *inlined) +int *inlined,ext4_lblk_t *lblk) {struct super_block *sb;struct buffer_head *bh_use[NAMEI_RA_SIZE ];
@@ -1485,7 +1485,7 @@ static struct buffer_head *__ext4_find_entry(struct inode *dir, goto restart; }if (is_dx (dir)) { - ret =ext4_dx_find_entry (dir, fname, res_dir); + ret =ext4_dx_find_entry (dir, fname, res_dir, lblk);/* * On success, or if the error was file not found, * return. Otherwise, fall back to doing a search the
@@ -1588,7 +1588,8 @@ static struct buffer_head *__ext4_find_entry(struct inode *dir,static struct buffer_head *ext4_find_entry (struct inode *dir,const struct qstr *d_name,struct ext4_dir_entry_2 **res_dir, -int *inlined) +int *inlined, +ext4_lblk_t *lblk) {int err;struct ext4_filename fname;
@@ -1600,7 +1601,7 @@ static struct buffer_head *ext4_find_entry(struct inode *dir,if (err)return ERR_PTR (err); - bh =__ext4_find_entry (dir, &fname, res_dir, inlined); + bh =__ext4_find_entry (dir, &fname, res_dir, inlined, lblk);ext4_fname_free_filename (&fname);return bh;
@@ -1620,7 +1621,7 @@ static struct buffer_head *ext4_lookup_entry(struct inode *dir,if (err)return ERR_PTR (err); - bh =__ext4_find_entry (dir, &fname, res_dir,NULL ); + bh =__ext4_find_entry (dir, &fname, res_dir,NULL ,NULL );ext4_fname_free_filename (&fname);return bh;
@@ -1628,7 +1629,8 @@ static struct buffer_head *ext4_lookup_entry(struct inode *dir,static struct buffer_head *ext4_dx_find_entry (struct inode *dir,struct ext4_filename *fname, -struct ext4_dir_entry_2 **res_dir) +struct ext4_dir_entry_2 **res_dir, +ext4_lblk_t *lblk) {struct super_block * sb = dir->i_sb;struct dx_frame frames[EXT4_HTREE_LEVEL ], *frame;
@@ -1675,6 +1677,8 @@ static struct buffer_head * ext4_dx_find_entry(struct inode *dir, errout:dxtrace (printk (KERN_DEBUG "%s not found\n" , fname->usr_fname->name)); success: +if (lblk) + *lblk = block;dx_release (frames);return bh; }
@@ -1743,7 +1747,7 @@ struct dentry *ext4_get_parent(struct dentry *child)struct ext4_dir_entry_2 * de;struct buffer_head *bh; - bh =ext4_find_entry (d_inode (child), &dotdot, &de,NULL ); + bh =ext4_find_entry (d_inode (child), &dotdot, &de,NULL ,NULL );if (IS_ERR (bh))return ERR_CAST (bh);if (!bh)
@@ -2495,7 +2499,7 @@ int ext4_generic_delete_entry(handle_t *handle,static int ext4_delete_entry (handle_t *handle,struct inode *dir,struct ext4_dir_entry_2 *de_del, -struct buffer_head *bh) +struct buffer_head *bh,ext4_lblk_t lblk) {int err, csum_size =0 ;
@@ -3091,6 +3095,7 @@ static int ext4_rmdir(struct inode *dir, struct dentry *dentry)struct buffer_head *bh;struct ext4_dir_entry_2 *de;handle_t *handle =NULL ; +ext4_lblk_t lblk;if (unlikely (ext4_forced_shutdown (EXT4_SB (dir->i_sb))))return -EIO ;
@@ -3105,7 +3110,7 @@ static int ext4_rmdir(struct inode *dir, struct dentry *dentry)return retval; retval = -ENOENT ; - bh =ext4_find_entry (dir, &dentry->d_name, &de,NULL ); + bh =ext4_find_entry (dir, &dentry->d_name, &de,NULL , &lblk);if (IS_ERR (bh))return PTR_ERR (bh);if (!bh)
@@ -3132,7 +3137,7 @@ static int ext4_rmdir(struct inode *dir, struct dentry *dentry)if (IS_DIRSYNC (dir))ext4_handle_sync (handle); - retval =ext4_delete_entry (handle, dir, de, bh); + retval =ext4_delete_entry (handle, dir, de, bh, lblk);if (retval) goto end_rmdir;if (!EXT4_DIR_LINK_EMPTY (inode))
@@ -3178,6 +3183,7 @@ static int ext4_unlink(struct inode *dir, struct dentry *dentry)struct buffer_head *bh;struct ext4_dir_entry_2 *de;handle_t *handle =NULL ; +ext4_lblk_t lblk;if (unlikely (ext4_forced_shutdown (EXT4_SB (dir->i_sb))))return -EIO ;
@@ -3193,7 +3199,7 @@ static int ext4_unlink(struct inode *dir, struct dentry *dentry)return retval; retval = -ENOENT ; - bh =ext4_find_entry (dir, &dentry->d_name, &de,NULL ); + bh =ext4_find_entry (dir, &dentry->d_name, &de,NULL , &lblk);if (IS_ERR (bh))return PTR_ERR (bh);if (!bh)
@@ -3216,7 +3222,7 @@ static int ext4_unlink(struct inode *dir, struct dentry *dentry)if (IS_DIRSYNC (dir))ext4_handle_sync (handle); - retval =ext4_delete_entry (handle, dir, de, bh); + retval =ext4_delete_entry (handle, dir, de, bh, lblk);if (retval) goto end_unlink; dir->i_ctime = dir->i_mtime =current_time (dir);
@@ -3564,19 +3570,20 @@ static int ext4_find_delete_entry(handle_t *handle, struct inode *dir,int retval = -ENOENT ;struct buffer_head *bh;struct ext4_dir_entry_2 *de; +ext4_lblk_t lblk; - bh =ext4_find_entry (dir, d_name, &de,NULL ); + bh =ext4_find_entry (dir, d_name, &de,NULL , &lblk);if (IS_ERR (bh))return PTR_ERR (bh);if (bh) { - retval =ext4_delete_entry (handle, dir, de, bh); + retval =ext4_delete_entry (handle, dir, de, bh, lblk);brelse (bh); }return retval; }static void ext4_rename_delete (handle_t *handle,struct ext4_renament *ent, -int force_reread) +int force_reread,ext4_lblk_t lblk) {int retval;/*
@@ -3593,7 +3600,8 @@ static void ext4_rename_delete(handle_t *handle, struct ext4_renament *ent, retval =ext4_find_delete_entry (handle, ent->dir, &ent->dentry->d_name); }else { - retval =ext4_delete_entry (handle, ent->dir, ent->de, ent->bh); + retval =ext4_delete_entry (handle, ent->dir, ent->de, ent->bh, + lblk);if (retval == -ENOENT ) { retval =ext4_find_delete_entry (handle, ent->dir, &ent->dentry->d_name);
@@ -3679,6 +3687,7 @@ static int ext4_rename(struct inode *old_dir, struct dentry *old_dentry,struct inode *whiteout =NULL ;int credits;u8 old_file_type; +ext4_lblk_t lblk;if (new.inode && new.inode->i_nlink ==0 ) { EXT4_ERROR_INODE(new.inode,
@@ -3706,7 +3715,8 @@ static int ext4_rename(struct inode *old_dir, struct dentry *old_dentry,return retval; } - old.bh =ext4_find_entry (old.dir, &old.dentry->d_name, &old.de,NULL ); + old.bh =ext4_find_entry (old.dir, &old.dentry->d_name, &old.de,NULL , + &lblk);if (IS_ERR (old.bh))return PTR_ERR (old.bh);/*
@@ -3720,7 +3730,7 @@ static int ext4_rename(struct inode *old_dir, struct dentry *old_dentry, goto end_rename; new.bh =ext4_find_entry (new.dir, &new.dentry->d_name, - &new.de, &new.inlined); + &new.de, &new.inlined,NULL );if (IS_ERR (new.bh)) { retval =PTR_ERR (new.bh); new.bh =NULL ;
@@ -3817,7 +3827,7 @@ static int ext4_rename(struct inode *old_dir, struct dentry *old_dentry,/* * ok, that's it */ -ext4_rename_delete (handle, &old, force_reread); +ext4_rename_delete (handle, &old, force_reread, lblk); }if (new.inode) {
@@ -3900,7 +3910,7 @@ static int ext4_cross_rename(struct inode *old_dir, struct dentry *old_dentry,return retval; old.bh =ext4_find_entry (old.dir, &old.dentry->d_name, - &old.de, &old.inlined); + &old.de, &old.inlined,NULL );if (IS_ERR (old.bh))return PTR_ERR (old.bh);/*
@@ -3914,7 +3924,7 @@ static int ext4_cross_rename(struct inode *old_dir, struct dentry *old_dentry, goto end_rename; new.bh =ext4_find_entry (new.dir, &new.dentry->d_name, - &new.de, &new.inlined); + &new.de, &new.inlined,NULL );if (IS_ERR (new.bh)) { retval =PTR_ERR (new.bh); new.bh =NULL ;
Signature · 1 line
2.26.0.292.g33ef6b2f38-goog
PATCH v2 2/3 ext4: shrink directories on dentry delete
This patch adds shrinking support for htree based directories. The high level algorithm is as follows:
* If after dentry removal the dirent block (let's call it B) becomes empty, then remove its references in its dx parent.
* Swap its contents with that of the last block (L) in directory.
* Update L's parents to point to B instead.
* Remove L
* Repeat this for all the ancestors of B.
We add variants of dx_probe that allow us perform reverse lookups from a logical block to its dx parents.
Ran kvm-xfstests smoke and verified that no new failures are introduced. Ran shrinking for directories with following number of files and then deleted files one by one:
* 1000 (size before deletion 36K, after deletion 4K)
* 10000 (size before deletion 196K, after deletion 4K)
* 100000 (size before deletion 2.1M, after deletion 4K)
* 200000 (size before deletion 4.2M, after deletion 4K)
In all cases directory shrunk significantly. We fallback to linear directories if the directory becomes empty.
But note that most of the shrinking happens during last 1-2% deletions in an average case. Therefore, the next step here is to merge dx nodes when possible. That can be achieved by storing the fullness index in htree nodes.
Performance Testing:
-------------------
Created 1 million files and unlinked all of them. Did this with and without directory shrinking. Journalling was on. Used ftrace to measure time spent in ext4_unlink:
* Without directory shrinking
Size Before: 22M /mnt/1
1000000 files created and deleted.
Average Total Elapsed Time (across 3 runs): 43.787s
Size After: 22M /mnt/1
useconds #count
------------------------
0-9 3690
10-19 2790131
20-29 179209
30-39 14270
40-49 7981 50-59 2212 60-69 1132 70-79 703 80-89 403 90-99 274
Num samples > 40us: 12615
* With directory shrinking
Size Before: 22M /mnt/1
1000000 files created and deleted.
Average Total Elapsed Time(across 3 runs): 44.230s
Size After: 4.0K /mnt/1
useconds #count
-----------------------
0-9 3316
10-19 2786451
20-29 172015
30-39 13259
40-49 17847
50-59 4843
60-69 924 70-79 569 80-89 390 90-99 389
Num samples > 40us: 24962
We see doubled number of samples of >40us in case of directory shrinking. Because these constitute to < 1% of total samples, the overall effect of direcotry shrinking on unlink / rmdir performance is negligible. There was no notable difference in cpu usage.
This patch supersedes the other directory shrinking patch sent in Aug 2019 ("ext4: attempt to shrink directory on dentry removal").
Changes since V1:
* ext4_remove_dx_entry(), dx_probe_dx_node() fixes
* dx_probe_dirent_blk() continuation fix
* Added performance evaluation
--- fs/ext4/ext4.h | 3 +- fs/ext4/ext4_jbd2.h | 7 + fs/ext4/inline.c | 2 +- fs/ext4/namei.c | 357 ++++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 356 insertions(+), 13 deletions(-)
fs/ext4/ext4.h +2 -1
@@ -2746,7 +2746,8 @@ extern int ext4_generic_delete_entry(handle_t *handle,struct buffer_head *bh, void *entry_buf, int buf_size, - int csum_size); + int csum_size, + bool *empty);extern bool ext4_empty_dir (struct inode *inode);/* resize.c */
fs/ext4/ext4_jbd2.h +7 -0
@@ -83,6 +83,13 @@*/ #define EXT4_INDEX_EXTRA_TRANS_BLOCKS 12U +/* +* Number of credits needed to perform directory shrinking. For each htree +* level, we need 6 blocks (block in question, its parent, last block, last +* block's parent, bitmap block, bg summary). +*/ +#define EXT4_DIR_SHRINK_TRANS_BLOCKS 6U +#ifdef CONFIG_QUOTA /* Amount of blocks needed for quota update - we know that the structure was * allocated so we need to update only data block */
fs/ext4/inline.c +1 -1
@@ -1706,7 +1706,7 @@ int ext4_delete_inline_entry(handle_t *handle, goto out; err =ext4_generic_delete_entry (handle, dir, de_del, bh, - inline_start, inline_size,0 ); + inline_start, inline_size,0 ,NULL );if (err) goto out;
fs/ext4/namei.c +346 -11
441 lines not on this page · raw message
Signature · 1 line
2.26.0.292.g33ef6b2f38-goog
Re: [PATCH v2 2/3] ext4: shrink directories on dentry delete
Quoted 89 lines · Harshad Shirwadkar
This patch adds shrinking support for htree based directories. The high level algorithm is as follows:
* If after dentry removal the dirent block (let's call it B) becomes empty, then remove its references in its dx parent.
* Swap its contents with that of the last block (L) in directory.
* Update L's parents to point to B instead.
* Remove L
* Repeat this for all the ancestors of B.We add variants of
dx_probethat allow us perform reverse lookups from a logical block to its dx parents.Ran kvm-xfstests smoke and verified that no new failures are introduced. Ran shrinking for directories with following number of files and then deleted files one by one:
* 1000 (size before deletion 36K, after deletion 4K)
* 10000 (size before deletion 196K, after deletion 4K)
* 100000 (size before deletion 2.1M, after deletion 4K)
* 200000 (size before deletion 4.2M, after deletion 4K)In all cases directory shrunk significantly. We fallback to linear directories if the directory becomes empty.
But note that most of the shrinking happens during last 1-2% deletions in an average case. Therefore, the next step here is to merge dx nodes when possible. That can be achieved by storing the fullness index in htree nodes.
Performance Testing:
-------------------Created 1 million files and unlinked all of them. Did this with and without directory shrinking. Journalling was on. Used ftrace to measure time spent in
ext4_unlink:* Without directory shrinking
Size Before: 22M
/mnt/1
1000000 files created and deleted.
Average Total Elapsed Time (across 3 runs): 43.787s
Size After: 22M/mnt/1useconds #count
------------------------
0-9 3690
10-19 2790131
20-29 179209
30-39 1427040-49 7981 50-59 2212 60-69 1132 70-79 703 80-89 403 90-99 274Num samples > 40us: 12615
* With directory shrinking
Size Before: 22M
/mnt/1
1000000 files created and deleted.
Average Total Elapsed Time(across 3 runs): 44.230s
Size After: 4.0K/mnt/1useconds #count
-----------------------
0-9 3316
10-19 2786451
20-29 172015
30-39 13259
40-49 17847
50-59 484360-69 924 70-79 569 80-89 390 90-99 389Num samples > 40us: 24962
We see doubled number of samples of >40us in case of directory shrinking. Because these constitute to < 1% of total samples, the overall effect of direcotry shrinking on unlink / rmdir performance is negligible. There was no notable difference in cpu usage.
This patch supersedes the other directory shrinking patch sent in Aug 2019 ("ext4: attempt to shrink directory on dentry removal").
Quoted 6 lines
Changes since V1:
*ext4_remove_dx_entry(),dx_probe_dx_node()fixes
*dx_probe_dirent_blk()continuation fix
* Added performance evaluationSigned-off-by Harshad Shirwadkar <[email protected]>
Some *very* minor cleanups *iff* the patch is refreshed, but otherwise LGTM. Thanks also for the good performance numbers, that makes it pretty clear there is very little downside to enabling this all the time.
Quoted 8 lines
---
fs/ext4/ext4.h | 3 +- fs/ext4/ext4_jbd2.h | 7 + fs/ext4/inline.c | 2 +- fs/ext4/namei.c | 357 ++++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 356 insertions(+), 13 deletions(-)
fs/ext4/namei.c dx_probe(struct ext4_filename *fname, struct inode *dir, · line 831
Quoted 7 lines
fs/ext4/namei.c +3
-1@@ -820,7 +826,9 @@ dx_probe(struct ext4_filename *fname, struct inode *dir,dxtrace (printk ("Look up %x" , hash));while (1 ) { count =dx_get_count (entries); -if (!count || count >dx_get_limit (entries)) { +/* If we are called from the shrink path */ +if (count >dx_get_limit (entries) || + (strict && !count)) {
(minor) I was wondering if (strict && !count) should be kept first, since it is a simpler check, but then realized this whole check should be marked unlikely(), since it should never happen unless the filesystem is corrupted.
fs/ext4/namei.c dx_probe(struct ext4_filename *fname, struct inode *dir, · line 985
Quoted 2 lines
fs/ext4/namei.c +2
-0+* This function tries to remove the entry of a dirent block (which was just +* emptied by the caller) from the dx frame. It does so by reducing the count by
(very minor) could move "by" to next line to balance line length... :-)
fs/ext4/namei.c dx_probe(struct ext4_filename *fname, struct inode *dir, · line 995
Quoted 10 lines
fs/ext4/namei.c +10
-0+* 1 and left shifting all the entries after the deleted entry. +* ext4_remove_dx_entry() does NOT mark dx_frame dirty, that's left to the +* caller. The reason for doing this is that this function removes entry from +* a dx_node and can potentially leave dx_node with no entries. Since older +* kernels don't allow dx_parent nodes to have 0 count, the caller should +* only mark buffer dirty when we are sure that we won't leave dx_node +* with 0 count. +*/ +int +ext4_remove_dx_entry(handle_t *handle, struct inode *dir,
(style) normally the return type is on the same line as the function declaration, unless (like dx_probe()) it is so long as to consume a lot of space. That isn't the case here.
Quoted 19 lines
fs/ext4/namei.c +18
-0+/* +* Checks if dirent block is empty. If de is passed, we +* skip directory entries before de in the block. +*/ +static inline bool is_empty_dirent_block (struct inode *dir, +struct buffer_head *bh, +struct ext4_dir_entry_2 *de) +{ +if (!de) + de = (struct ext4_dir_entry_2 *)bh->b_data; +while ((u8 *)de < (u8 *)bh->b_data + dir->i_sb->s_blocksize) { +if (de->inode) +return false; + de =ext4_next_entry (de, dir->i_sb->s_blocksize); + } +return true; +} +
fs/ext4/namei.c int ext4_generic_delete_entry(handle_t *handle, · line 2648
Quoted 6 lines
fs/ext4/namei.c +3
-0@@ -2486,6 +2643,9 @@ int ext4_generic_delete_entry(handle_t *handle, blocksize); else de->inode = 0; +if (empty) + *empty =is_empty_dirent_block (dir, bh, + pde ? pde : de);
Nice. I was going to say that this shouldn't re-check the whole block, but it doesn't do that. is_empty_dirent_block() only checks entries after the removed entry, exits early if any non-empty dirent is found, and only checks if we care whether the block is empty or not, and which is ideal.
Cheers, Andreas
PATCH v2 3/3 ext4: reimplement ext4_empty_dir() using is_dirent_block_empty
In reply to Harshad ShirwadkarThe new function added in this patchset adds an efficient way to check if a dirent block is empty. Based on that, reimplement ext4_empty_dir().
This is a new patch added in V2.
--- fs/ext4/namei.c | 39 ++++++++++++++++----------------------- 1 file changed, 16 insertions(+), 23 deletions(-)
fs/ext4/namei.c +16 -23
@@ -3179,6 +3179,7 @@ bool ext4_empty_dir(struct inode *inode)struct buffer_head *bh;struct ext4_dir_entry_2 *de;struct super_block *sb; +ext4_lblk_t lblk;if (ext4_has_inline_data (inode)) {int has_inline_data =1 ;
@@ -3218,34 +3219,26 @@ bool ext4_empty_dir(struct inode *inode)brelse (bh);return true; } - offset +=ext4_rec_len_from_disk (de->rec_len, sb->s_blocksize); -while (offset < inode->i_size) { -if (!(offset & (sb->s_blocksize -1 ))) { -unsigned int lblock; -brelse (bh); - lblock = offset >>EXT4_BLOCK_SIZE_BITS (sb); - bh =ext4_read_dirblock (inode, lblock,EITHER ); -if (bh ==NULL ) { - offset += sb->s_blocksize; -continue ; - } -if (IS_ERR (bh)) -return true; - } - de = (struct ext4_dir_entry_2 *) (bh->b_data + - (offset & (sb->s_blocksize -1 ))); -if (ext4_check_dir_entry (inode,NULL , de, bh, - bh->b_data, bh->b_size, offset)) { - offset = (offset | (sb->s_blocksize -1 )) +1 ; + de =ext4_next_entry (de, sb->s_blocksize); +if (!is_empty_dirent_block (inode, bh, de)) { +brelse (bh); +return false; + } +brelse (bh); + +for (lblk =1 ; lblk < inode->i_size >>EXT4_BLOCK_SIZE_BITS (sb); + lblk++) { + bh =ext4_read_dirblock (inode, lblk,EITHER ); +if (bh ==NULL )continue ; - } -if (le32_to_cpu (de->inode)) { +if (IS_ERR (bh)) +return true; +if (!is_empty_dirent_block (inode, bh,NULL )) {brelse (bh);return false; } - offset +=ext4_rec_len_from_disk (de->rec_len, sb->s_blocksize); +brelse (bh); } -brelse (bh);return true; }
Signature · 1 line
2.26.0.292.g33ef6b2f38-goog
Re: [PATCH v2 3/3] ext4: reimplement ext4_empty_dir() using is_dirent_block_empty
Quoted 8 lines · Harshad Shirwadkar
The new function added in this patchset adds an efficient way to check if a dirent block is empty. Based on that, reimplement
ext4_empty_dir().This is a new patch added in V2.
Signed-off-by Harshad Shirwadkar <[email protected]>
Again, a very minor style change iff patch is refreshed.
While looking at this code, I noticed that ext4_empty_dir() considers a directory without a "." or ".." entry to be empty. I see this was changed in 64d4ce8923 ("ext4: fix ext4_empty_dir() for directories with holes"). I can understand that we want to not die on corrupted leaf blocks, but it isn't clear to me that it is a good idea to allow deleting an entire directory tree if the first block has an error (missing "." or ".." as the first and second entries) but is otherwise valid. There were definitely bugs in the past that made "." or ".." not be the first and second entries.
That isn't a problem in this patch, but seems dangerous in general. It doesn't seem like the directory shrinking would trigger this case, since it is checking for individual empty blocks rather than using ext4_empty_dir().
Quoted 4 lines
fs/ext4/namei.c +0
-0@@ -3218,34 +3219,26 @@ bool ext4_empty_dir(struct inode *inode)brelse (bh);return true;
fs/ext4/namei.c bool ext4_empty_dir(struct inode *inode) · line 3230
Quoted 10 lines
fs/ext4/namei.c +9
-0} + de =ext4_next_entry (de, sb->s_blocksize); +if (!is_empty_dirent_block (inode, bh, de)) { +brelse (bh); +return false; + } +brelse (bh); + +for (lblk =1 ; lblk < inode->i_size >>EXT4_BLOCK_SIZE_BITS (sb); + lblk++) {
(style) should be aligned after '(' on previous line
Quoted 7 lines
fs/ext4/namei.c +4
-2+ bh =ext4_read_dirblock (inode, lblk,EITHER ); +if (bh ==NULL )continue ; - } -if (le32_to_cpu (de->inode)) { +if (IS_ERR (bh)) +return true;
fs/ext4/namei.c bool ext4_empty_dir(struct inode *inode) · line 3243
Quoted 10 lines
fs/ext4/namei.c +2
-2+if (!is_empty_dirent_block (inode, bh,NULL )) {brelse (bh);return false; } - offset +=ext4_rec_len_from_disk (de->rec_len, sb->s_blocksize); +brelse (bh); } -brelse (bh);return true; }
Cheers, Andreas
Jan Kara
reviewerRe: [PATCH v2 3/3] ext4: reimplement ext4_empty_dir() using is_dirent_block_empty
Quoted 8 lines · Andreas Dilger
While looking at this code, I noticed that
ext4_empty_dir()considers a directory without a "." or ".." entry to be empty. I see this was changed in64d4ce8923("ext4: fixext4_empty_dir()for directories with holes"). I can understand that we want to not die on corrupted leaf blocks, but it isn't clear to me that it is a good idea to allow deleting an entire directory tree if the first block has an error (missing "." or ".." as the first and second entries) but is otherwise valid. There were definitely bugs in the past that made "." or ".." not be the first and second entries.
That's a good question. I'd just say that ext4_empty_dir() generally returns true when there's some problem with the directory. In commit 64d4ce8923 I just followed that convention. This behavior of ext4_empty_dir() (and empty_dir() before in ext3) dates back at least to the beginning of git history... I guess we could err on the safer side and disallow directory deletion if there is any problem with it but I guess there was some motivation for this behavior in the past? Maybe somebody remembers?
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
A message that is not in the archive (sent off-list or not yet received): [email protected]
Re: [PATCH v2 3/3] ext4: reimplement ext4_empty_dir() using is_dirent_block_empty
Quoted 86 lines · Julian Sun
Quoted 76 lines · Harshad Shirwadkar
The new function added in this patchset adds an efficient way to check if a dirent block is empty. Based on that, reimplement
ext4_empty_dir().This is a new patch added in V2.
Signed-off-by Harshad Shirwadkar <[email protected]>Reviewed-by Andreas Dilger <[email protected]>--- fs/ext4/namei.c | 39 ++++++++++++++++----------------------- 1 file changed, 16 insertions(+), 23 deletions(-)fs/ext4/namei.c +15
-22@@ -3179,6 +3179,7 @@ bool ext4_empty_dir(struct inode *inode)struct buffer_head *bh;struct ext4_dir_entry_2 *de;struct super_block *sb; +ext4_lblk_t lblk;if (ext4_has_inline_data (inode)) {int has_inline_data =1 ;@@ -3218,34 +3219,26 @@ bool ext4_empty_dir(struct inode *inode)brelse (bh);return true; } - offset +=ext4_rec_len_from_disk (de->rec_len, sb- > s_blocksize); -while (offset < inode->i_size) { -if (!(offset & (sb->s_blocksize -1 ))) { -unsigned int lblock; -brelse (bh); - lblock = offset >>EXT4_BLOCK_SIZE_BITS (sb); - bh =ext4_read_dirblock (inode, lblock, EITHER); -if (bh ==NULL ) { - offset += sb->s_blocksize; -continue ; - } -if (IS_ERR (bh)) -return true; - } - de = (struct ext4_dir_entry_2 *) (bh->b_data + - (offset & (sb->s_blocksize - 1))); -if (ext4_check_dir_entry (inode,NULL , de, bh, - bh->b_data, bh->b_size, offset)) { - offset = (offset | (sb->s_blocksize -1 )) +1 ; + de =ext4_next_entry (de, sb->s_blocksize); +if (!is_empty_dirent_block (inode, bh, de)) { +brelse (bh); +return false; + } +brelse (bh); + +for (lblk =1 ; lblk < inode->i_size >>EXT4_BLOCK_SIZE_BITS (sb); + lblk++) { + bh =ext4_read_dirblock (inode, lblk,EITHER ); +if (bh ==NULL )continue ; - } -if (le32_to_cpu (de->inode)) { +if (IS_ERR (bh)) +return true; +if (!is_empty_dirent_block (inode, bh,NULL )) {brelse (bh);return false; } - offset += ext4_rec_len_from_disk(de->rec_len, sb-Quoted 1 line
s_blocksize);fs/ext4/namei.c bool ext4_empty_dir(struct inode *inode) · line 3243
Quoted 5 lines
fs/ext4/namei.c +1
-1+brelse (bh); } -brelse (bh);return true; }Hi,
Quoted 8 lines
I’ve recently been looking into the ext4 directory shrinking problem and was considering trying to add this feature myself. To my surprise, I found that this patch set had already implemented it and even received Reviewed-by. I’m curious whether it was never merged, or if it was merged and later reverted?
If possible, is there anything I could do to contribute to moving this patch set forward toward being merged?
cc ext4 mail list.
Quoted 4 lines
Thanks.
Signature · 1 line
Julian Sun <[email protected]>
Theodore Ts'o
maintainerRe: [PATCH v2 3/3] ext4: reimplement ext4_empty_dir() using is_dirent_block_empty
Quoted 9 lines · Julian Sun
Quoted 9 lines
I’ve recently been looking into the ext4 directory shrinking problem and was considering trying to add this feature myself. To my surprise, I found that this patch set had already implemented it and even received Reviewed-by. I’m curious whether it was never merged, or if it was merged and later reverted?
If possible, is there anything I could do to contribute to moving this patch set forward toward being merged?
I *think* there was one or two test regressions that Harshad was wrking on, but real problem was the original business imperative for the project became no longer as compelling, and we moved to focus on other priorities.
So if you'd like tocontribute to moving this forward, what we'd need to do is to forward port the patch set to the latest kernel. I've taken a quick look at the patches, and predates the addition of the support of 3-level htrees (the incompat_largedir feature). There are also some hardening against maliciously fuzzed file systems that will prevent the patches from applying cleanly.
Then we'd need to run regression tests on a variety of different ext4 configurations to see if there are any regressions, and if so, they would need to be fixed.
Also, please note that this first set of changes doesn't really make a big difference for real-world use casses, since a directory block won't get dropped when it is completely empty. For example, if we assume an average directory entry size of 32, there can be up to 128 entries in a 4k block. If we assume that the average leaf block is 75% filled, there will be 96 directory entries. All 96 directory entries have to be deleted before that block can be removed. If the directory is 4MB, there will be roughly 100,000 directory entries and 1024 blocks. If we assume a random distribution and random deletion (which is a fair assumption given that we're using a hash of the file name). I will leave it as an exercise to the reader what percentage of directory entries need to be deleted before the probability that at least one 4k directory block is emptied is at least, say, 80%. But in practice, you have to delete most of the files in the directory before the directory starts shrinking.
So this this is why we really need to implement the next step (which is not in this patch series), and that is to merging two adjacent leaf blocks once they fall below to some threshold --- say, 25%. We will also need to merge two adjacent index nodes if they are mostly empty.
Cheers,
- Ted
Re: [PATCH v2 3/3] ext4: reimplement ext4_empty_dir() using is_dirent_block_empty
Hi,
Quoted 17 lines · Theodore Ts'o
Quoted 9 lines · Julian Sun
Quoted 9 lines
I’ve recently been looking into the ext4 directory shrinking problem and was considering trying to add this feature myself. To my surprise, I found that this patch set had already implemented it and even received Reviewed-by. I’m curious whether it was never merged, or if it was merged and later reverted?
If possible, is there anything I could do to contribute to moving this patch set forward toward being merged?
I *think* there was one or two test regressions that Harshad was wrking on, but real problem was the original business imperative for the project became no longer as compelling, and we moved to focus on other priorities.
Quoted 4 lines
So if you'd like tocontribute to moving this forward, what we'd need to do is to forward port the patch set to the latest kernel. I've taken a quick look at the patches, and predates the addition of the support of 3-level htrees (the
incompat_largedirfeature).
Emm. I checked the code and found that support for 3-level htrees was added in 2017 via commit e08ac99fa2a2 ("ext4: add largedir feature"), but this patch was submitted in 2020. Did I make a mistake somewhere?
Quoted 3 lines
There are
also some hardening against maliciously fuzzed file systems that will prevent the patches from applying cleanly.
Is this included in the xfstests test suite?
Quoted 3 lines
Then we'd need to run regression tests on a variety of different ext4 configurations to see if there are any regressions, and if so, they would need to be fixed.
Is testing with xfstests sufficient? Or are there any other test suites that can be used to test this patch set?
Quoted 16 lines
Also, please note that this first set of changes doesn't really make a big difference for real-world use casses, since a directory block won't get dropped when it is completely empty. For example, if we assume an average directory entry size of 32, there can be up to 128 entries in a 4k block. If we assume that the average leaf block is 75% filled, there will be 96 directory entries. All 96 directory entries have to be deleted before that block can be removed. If the directory is 4MB, there will be roughly 100,000 directory entries and 1024 blocks. If we assume a random distribution and random deletion (which is a fair assumption given that we're using a hash of the file name). I will leave it as an exercise to the reader what percentage of directory entries need to be deleted before the probability that at least one 4k directory block is emptied is at least, say, 80%. But in practice, you have to delete most of the files in the directory before the directory starts shrinking.
Yes, I think the biggest beneficiary is rm -rf-type workloads.
Quoted 5 lines
So this this is why we really need to implement the next step (which is not in this patch series), and that is to merging two adjacent leaf blocks once they fall below to some threshold --- say, 25%. We will also need to merge two adjacent index nodes if they are mostly empty.
Sounds great. Thanks for your kind and detailed explanation, Ted.
Quoted 4 lines
Cheers,
- Ted
Thanks,
Signature · 1 line
Julian Sun <[email protected]>
Theodore Ts'o
maintainerRe: [PATCH v2 3/3] ext4: reimplement ext4_empty_dir() using is_dirent_block_empty
Quoted 3 lines · Julian Sun
Emm. I checked the code and found that support for 3-level htrees was added in 2017 via commit
e08ac99fa2a2("ext4: add largedir feature"), but this patch was submitted in 2020. Did I make a mistake somewhere?
I made that statement when I tried forward porting Harshad's patches to 6.17-rc4, and one of the patch conflicts seemed to implyyy that dx_probe predated the3-level htree change. But I could have been mistaken about why the patch conflict eixsts.
Quoted 5 lines
Quoted 3 lines
There are
also some hardening against maliciously fuzzed file systems that will prevent the patches from applying cleanly.Is this included in the xfstests test suite?
For better or for worse, no. The reason for this is that xfstests tends to avoid using pre-generated file systems becase they aren't portable to different file system types. And in order to test deliberately corrupted, you generally need to include small corrupted file systems into xfstests. You *could* try to create them as an ext4-specific test using debugfs to introduce the corruption, but that's quite painful, and in some cases the only way to corrupt the file system in that very specific way would require a hex editor.
That being said, it's generally pretty obvious when bullet-proofing code has been added and it causes a merge conflict. For example, I skipped forward-porting the third patch, "ext4: reimplement ext4_empty_dir() using is_dirent_block_empty" because there was some bullet-proofing code added in ext4_empty_dir() which caused the patch application to fail, and I was too third try to deal with it, and it strictly speaking wasn't necessary for the patch shrinking functionality. That being said, the bullet proofing which was added to ext4_empty_dir(), *should* be added to the newly created i_dirent_block_empty() function introduced in the 2/3 patch in this series.
Quoted 6 lines
Quoted 3 lines
Then we'd need to run regression tests on a variety of different ext4 configurations to see if there are any regressions, and if so, they would need to be fixed.
Is testing with xfstests sufficient? Or are there any other test suites that can be used to test this patch set?
Testing with xfstests are sufficient, but we need to test multiple file system configurations, but just the default "ext4 using a 4k blocksize". So I tried to do a quick-and-dirty forward port of the patches, and they compiled and passed the 15-20 minute smoke test (running "kvm-xfstests smoke", or "gce-xfstests smoke"). But when I ran the full set of tests, this is what I found.
ext4/4k: 594 tests, 4 failures, 65 skipped, 5196 seconds
Failures: generic/426 generic/756
Flaky: generic/041: 20% (1/5) generic/049: 20% (1/5)
ext4/1k: 588 tests, 3 failures, 69 skipped, 6306 seconds
Failures: generic/426 generic/756
Flaky: generic/043: 20% (1/5)
ext4/ext3: 586 tests, 3 failures, 149 skipped, 4958 seconds
Flaky: generic/041: 40% (2/5) generic/426: 60% (3/5)
generic/756: 60% (3/5)
ext4/encrypt: 569 tests, 3 failures, 181 skipped, 3263 seconds
Failures: generic/426 generic/756
Flaky: generic/049: 40% (2/5)
ext4/nojournal: 586 tests, 3 failures, 137 skipped, 3918 seconds
Failures: ext4/045 generic/426 generic/756
ext4/ext3conv: 591 tests, 4 failures, 67 skipped, 5408 seconds
Failures: generic/426 generic/756
Flaky: ext4/045: 40% (2/5) generic/040: 60% (3/5)
ext4/adv: 587 tests, 10 failures, 73 skipped, 5487 seconds
Failures: [generic/347] generic/426 generic/756 [generic/757] [generic/764]
[generic/777]
Flaky: generic/047: 40% (2/5) generic/475: 20% (1/5)
[generic/482: 40% (2/5)] generic/547: 20% (1/5)ext4/dioread_nolock: 592 tests, 3 failures, 65 skipped, 4996 seconds
Failures: generic/426 generic/756
Flaky: generic/047: 40% (2/5)
ext4/data_journal: 587 tests, 6 failures, 141 skipped, 5151 seconds
Failures: [generic/127] generic/426 generic/756
Flaky: generic/049: 60% (3/5) generic/475: 60% (3/5)
generic/645: 40% (2/5)
ext4/bigalloc_4k: 565 tests, 4 failures, 68 skipped, 5066 seconds
Failures: ext4/045
Flaky: generic/320: 60% (3/5) generic/426: 60% (3/5)
generic/756: 60% (3/5)ext4/bigalloc_1k: 566 tests, 3 failures, 79 skipped, 5096 seconds
Failures: generic/426 generic/756
Flaky: generic/049: 20% (1/5)
ext4/dax: 578 tests, 5 failures, 170 skipped, 3198 seconds
Failures: [generic/344] [generic/363] generic/426 generic/756
Flaky: generic/043: 20% (1/5)
Totals: 7193 tests, 1264 skipped, 190 failures, 0 errors, 53862s
(Tests in square brackets were failing with stock 6.17-rc4, so don't represent regressions. Ignore them for the purposes of trying to fix up this patch set.)
Now, some of thsee may be failures caused by my making a mistake when doing the forward port. Basically, I bashed the patches until they built; and then I ran the smoke test; and then I kicked of the full test run, which takes about 2.5 hours using a dozen VM's.
I don't have time to take this any further, so with your permission, if you're OK with my handing fiishing off this project to you, I'll send the patches as a reply to this message, and then "Your mission, should you use to accept it" (quoting from the "Mission Impossible" TV Show / Movie) would be:
1) Investigate the failures deailed above, and fix them. Again, some of these failures may not be Harshads; it could be mine when I did the forward port. Either way, we need to fix them before we can merge the changes upstream.
2) Do more cleanups on the patches as necessary. While doing the forward port, I noted the following issues that should really be fixed. You might find more things that need improvement; if so, please go for it!
2a) Although we are potentially modifying more metadata blocks in ext4_try_dir_shrink(), the number of journal credits requested by callers to ext4_delete_entry() were not increased. This could result in the handle running out of credits, which will trigger a warning, and if the transaction runs out of space, could trigger a journal abort. One way would simply be to increase the credits passed to ext4_journal_start().
The other way would be to queue up the work and do the directory shrinking in workqueue, where the changes would be done in a completely separate journal handle. This has the advantage that it avoids increasing the latency to the unlink() system call, since there is no reason why the change needs to happen as part of the system call, or in the same transaction as the unlink.
2b) Error checking is missing in some of the newly added code. In particular the function calls in make_unindexed() are ignoring error returns. More generally, while we do the directory shrnk, we need to check for potential problems; and if there are any failures, we need to leave the directory unchanged, possibly calling ext4_error() if file system corruption is dicovered, or just skipping the directory shirnk operation if it is some transient error --- for example, a memory allocation failure.
2c) As mentioned above, I skipped forward porting the 3rd patch series, and there is code to rigorously check for inconsistent file system corruptions lest we trigger a BUG or WARN or worse which is causing the patch application to fail. That checking needs to be added to is_dirent_block_empty(), and while you're at it, please check all of the newly added code to make sure it won't misbehave if the file system structures have been corrupted in a particualrly inventive way.
2d) Once (2c) is done forward port patch #3.
3) As discussed in this thread, once these patches are landed, the further work to merge leaf notes; to remove empty htree nodes; and to shorten the htree depth when necessary.
Quoted 5 lines
Quoted 3 lines
Also, please note that this first set of changes doesn't really make a big difference for real-world use casses, since a directory block won't get dropped when it is completely empty....
Yes, I think the biggest beneficiary is rm -rf-type workloads.
The thing about rm -rf workloads is that if you eventually rmdir() the directory, all of the blocks will be released anyway. That's probably why this feature is one that hasn't been high priority. If you delete 75% of the files in a directory, you *could* do something like "mkdir foo.new ; mv foo/* foo.new ; rmdir foo ; mv foo.new foo", but of course that won't work if some other process is actively accessing the files when you are trying to do the "DIY directory shrink operation".
Cheers,
- Ted
Theodore Ts'o
maintainerPATCH 1/2 ext4: return lblk from ext4_find_entry
From: Harshad Shirwadkar <[email protected]>
This patch makes ext4_find_entry and related routines to return logical block address of the dirent block. This logical block address is used in the directory shrinking code to perform reverse lookup and verify that the lookup was successful.
Message-ID: <[email protected]>
--- fs/ext4/namei.c | 55 ++++++++++++++++++++++++++++--------------------- 1 file changed, 32 insertions(+), 23 deletions(-)
fs/ext4/namei.c +32 -23
@@ -293,7 +293,7 @@ struct dx_tail {static struct buffer_head *ext4_dx_find_entry (struct inode *dir,struct ext4_filename *fname, -struct ext4_dir_entry_2 **res_dir); +struct ext4_dir_entry_2 **res_dir,ext4_lblk_t *lblk);static int ext4_dx_add_entry (handle_t *handle,struct ext4_filename *fname,struct inode *dir,struct inode *inode);
@@ -1517,7 +1517,7 @@ static int is_dx_internal_node(struct inode *dir, ext4_lblk_t block,static struct buffer_head *__ext4_find_entry (struct inode *dir,struct ext4_filename *fname,struct ext4_dir_entry_2 **res_dir, -int *inlined) +int *inlined,ext4_lblk_t *lblk) {struct super_block *sb;struct buffer_head *bh_use[NAMEI_RA_SIZE ];
@@ -1558,7 +1558,7 @@ static struct buffer_head *__ext4_find_entry(struct inode *dir, goto restart; }if (is_dx (dir)) { - ret =ext4_dx_find_entry (dir, fname, res_dir); + ret =ext4_dx_find_entry (dir, fname, res_dir, lblk);/* * On success, or if the error was file not found, * return. Otherwise, fall back to doing a search the
@@ -1668,7 +1668,8 @@ static struct buffer_head *__ext4_find_entry(struct inode *dir,static struct buffer_head *ext4_find_entry (struct inode *dir,const struct qstr *d_name,struct ext4_dir_entry_2 **res_dir, -int *inlined) +int *inlined, +ext4_lblk_t *lblk) {int err;struct ext4_filename fname;
@@ -1680,7 +1681,7 @@ static struct buffer_head *ext4_find_entry(struct inode *dir,if (err)return ERR_PTR (err); - bh =__ext4_find_entry (dir, &fname, res_dir, inlined); + bh =__ext4_find_entry (dir, &fname, res_dir, inlined, lblk);ext4_fname_free_filename (&fname);return bh;
@@ -1700,7 +1701,7 @@ static struct buffer_head *ext4_lookup_entry(struct inode *dir,if (err)return ERR_PTR (err); - bh =__ext4_find_entry (dir, &fname, res_dir,NULL ); + bh =__ext4_find_entry (dir, &fname, res_dir,NULL ,NULL );ext4_fname_free_filename (&fname);return bh;
@@ -1708,7 +1709,8 @@ static struct buffer_head *ext4_lookup_entry(struct inode *dir,static struct buffer_head *ext4_dx_find_entry (struct inode *dir,struct ext4_filename *fname, -struct ext4_dir_entry_2 **res_dir) +struct ext4_dir_entry_2 **res_dir, +ext4_lblk_t *lblk) {struct super_block * sb = dir->i_sb;struct dx_frame frames[EXT4_HTREE_LEVEL ], *frame;
@@ -1755,6 +1757,8 @@ static struct buffer_head * ext4_dx_find_entry(struct inode *dir, errout:dxtrace (printk (KERN_DEBUG "%s not found\n" , fname->usr_fname->name)); success: +if (lblk) + *lblk = block;dx_release (frames);return bh; }
@@ -1821,7 +1825,7 @@ struct dentry *ext4_get_parent(struct dentry *child)struct ext4_dir_entry_2 * de;struct buffer_head *bh; - bh =ext4_find_entry (d_inode (child), &dotdot_name, &de,NULL ); + bh =ext4_find_entry (d_inode (child), &dotdot_name, &de,NULL ,NULL );if (IS_ERR (bh))return ERR_CAST (bh);if (!bh)
@@ -2702,7 +2706,7 @@ int ext4_generic_delete_entry(struct inode *dir,static int ext4_delete_entry (handle_t *handle,struct inode *dir,struct ext4_dir_entry_2 *de_del, -struct buffer_head *bh) +struct buffer_head *bh,ext4_lblk_t lblk) {int err, csum_size =0 ;
@@ -3135,6 +3139,7 @@ static int ext4_rmdir(struct inode *dir, struct dentry *dentry)struct buffer_head *bh;struct ext4_dir_entry_2 *de;handle_t *handle =NULL ; +ext4_lblk_t lblk; retval =ext4_emergency_state (dir->i_sb);if (unlikely (retval))
@@ -3150,7 +3155,7 @@ static int ext4_rmdir(struct inode *dir, struct dentry *dentry)return retval; retval = -ENOENT ; - bh =ext4_find_entry (dir, &dentry->d_name, &de,NULL ); + bh =ext4_find_entry (dir, &dentry->d_name, &de,NULL , &lblk);if (IS_ERR (bh))return PTR_ERR (bh);if (!bh)
@@ -3177,7 +3182,7 @@ static int ext4_rmdir(struct inode *dir, struct dentry *dentry)if (IS_DIRSYNC (dir))ext4_handle_sync (handle); - retval =ext4_delete_entry (handle, dir, de, bh); + retval =ext4_delete_entry (handle, dir, de, bh, lblk);if (retval) goto end_rmdir;if (!EXT4_DIR_LINK_EMPTY (inode))
@@ -3227,12 +3232,13 @@ int __ext4_unlink(struct inode *dir, const struct qstr *d_name,struct ext4_dir_entry_2 *de;handle_t *handle;int skip_remove_dentry =0 ; +ext4_lblk_t lblk;/* * Keep this outside the transaction; it may have to set up the * directory's encryption key, which isn't GFP_NOFS-safe. */ - bh =ext4_find_entry (dir, d_name, &de,NULL ); + bh =ext4_find_entry (dir, d_name, &de,NULL , &lblk);if (IS_ERR (bh))return PTR_ERR (bh);
@@ -3262,7 +3268,7 @@ int __ext4_unlink(struct inode *dir, const struct qstr *d_name,ext4_handle_sync (handle);if (!skip_remove_dentry) { - retval =ext4_delete_entry (handle, dir, de, bh); + retval =ext4_delete_entry (handle, dir, de, bh, lblk);if (retval) goto out_handle;inode_set_mtime_to_ts (dir,inode_set_ctime_current (dir));
@@ -3669,7 +3675,7 @@ static void ext4_resetent(handle_t *handle, struct ext4_renament *ent,* before reset old inode info. */ old.bh =ext4_find_entry (old.dir, &old.dentry->d_name, &old.de, - &old.inlined); + &old.inlined,NULL );if (IS_ERR (old.bh)) retval =PTR_ERR (old.bh);if (!old.bh)
@@ -3689,19 +3695,20 @@ static int ext4_find_delete_entry(handle_t *handle, struct inode *dir,int retval = -ENOENT ;struct buffer_head *bh;struct ext4_dir_entry_2 *de; +ext4_lblk_t lblk; - bh =ext4_find_entry (dir, d_name, &de,NULL ); + bh =ext4_find_entry (dir, d_name, &de,NULL , &lblk);if (IS_ERR (bh))return PTR_ERR (bh);if (bh) { - retval =ext4_delete_entry (handle, dir, de, bh); + retval =ext4_delete_entry (handle, dir, de, bh, lblk);brelse (bh); }return retval; }static void ext4_rename_delete (handle_t *handle,struct ext4_renament *ent, -int force_reread) +int force_reread,ext4_lblk_t lblk) {int retval;/*
@@ -3718,7 +3725,8 @@ static void ext4_rename_delete(handle_t *handle, struct ext4_renament *ent, retval =ext4_find_delete_entry (handle, ent->dir, &ent->dentry->d_name); }else { - retval =ext4_delete_entry (handle, ent->dir, ent->de, ent->bh); + retval =ext4_delete_entry (handle, ent->dir, ent->de, ent->bh, + lblk);if (retval == -ENOENT ) { retval =ext4_find_delete_entry (handle, ent->dir, &ent->dentry->d_name);
@@ -3806,6 +3814,7 @@ static int ext4_rename(struct mnt_idmap *idmap, struct inode *old_dir,struct inode *whiteout =NULL ;int credits;u8 old_file_type; +ext4_lblk_t lblk;if (new.inode && new.inode->i_nlink ==0 ) { EXT4_ERROR_INODE(new.inode,
@@ -3837,7 +3846,7 @@ static int ext4_rename(struct mnt_idmap *idmap, struct inode *old_dir, } old.bh =ext4_find_entry (old.dir, &old.dentry->d_name, &old.de, - &old.inlined); + &old.inlined, &lblk);if (IS_ERR (old.bh))return PTR_ERR (old.bh);
@@ -3852,7 +3861,7 @@ static int ext4_rename(struct mnt_idmap *idmap, struct inode *old_dir, goto release_bh; new.bh =ext4_find_entry (new.dir, &new.dentry->d_name, - &new.de, &new.inlined); + &new.de, &new.inlined,NULL );if (IS_ERR (new.bh)) { retval =PTR_ERR (new.bh); new.bh =NULL ;
@@ -3952,7 +3961,7 @@ static int ext4_rename(struct mnt_idmap *idmap, struct inode *old_dir,/* * ok, that's it */ -ext4_rename_delete (handle, &old, force_reread); +ext4_rename_delete (handle, &old, force_reread, lblk); }if (new.inode) {
@@ -4073,7 +4082,7 @@ static int ext4_cross_rename(struct inode *old_dir, struct dentry *old_dentry,return retval; old.bh =ext4_find_entry (old.dir, &old.dentry->d_name, - &old.de, &old.inlined); + &old.de, &old.inlined,NULL );if (IS_ERR (old.bh))return PTR_ERR (old.bh);/*
@@ -4087,7 +4096,7 @@ static int ext4_cross_rename(struct inode *old_dir, struct dentry *old_dentry, goto end_rename; new.bh =ext4_find_entry (new.dir, &new.dentry->d_name, - &new.de, &new.inlined); + &new.de, &new.inlined,NULL );if (IS_ERR (new.bh)) { retval =PTR_ERR (new.bh); new.bh =NULL ;
Signature · 1 line
2.51.0
Theodore Ts'o
maintainerPATCH 2/2 ext4: shrink directories on dentry delete
From: Harshad Shirwadkar <[email protected]>
This patch adds shrinking support for htree based directories. The high level algorithm is as follows:
* If after dentry removal the dirent block (let's call it B) becomes empty, then remove its references in its dx parent.
* Swap its contents with that of the last block (L) in directory.
* Update L's parents to point to B instead.
* Remove L
* Repeat this for all the ancestors of B.
We add variants of dx_probe that allow us perform reverse lookups from a logical block to its dx parents.
Ran kvm-xfstests smoke and verified that no new failures are introduced. Ran shrinking for directories with following number of files and then deleted files one by one:
* 1000 (size before deletion 36K, after deletion 4K)
* 10000 (size before deletion 196K, after deletion 4K)
* 100000 (size before deletion 2.1M, after deletion 4K)
* 200000 (size before deletion 4.2M, after deletion 4K)
In all cases directory shrunk significantly. We fallback to linear directories if the directory becomes empty.
But note that most of the shrinking happens during last 1-2% deletions in an average case. Therefore, the next step here is to merge dx nodes when possible. That can be achieved by storing the fullness index in htree nodes.
Performance Testing:
-------------------
Created 1 million files and unlinked all of them. Did this with and without directory shrinking. Journalling was on. Used ftrace to measure time spent in ext4_unlink:
* Without directory shrinking
Size Before: 22M /mnt/1
1000000 files created and deleted.
Average Total Elapsed Time (across 3 runs): 43.787s
Size After: 22M /mnt/1
useconds #count
------------------------
0-9 3690
10-19 2790131
20-29 179209
30-39 14270
40-49 7981 50-59 2212 60-69 1132 70-79 703 80-89 403 90-99 274
Num samples > 40us: 12615
* With directory shrinking
Size Before: 22M /mnt/1
1000000 files created and deleted.
Average Total Elapsed Time(across 3 runs): 44.230s
Size After: 4.0K /mnt/1
useconds #count
-----------------------
0-9 3316
10-19 2786451
20-29 172015
30-39 13259
40-49 17847
50-59 4843
60-69 924 70-79 569 80-89 390 90-99 389
Num samples > 40us: 24962
We see doubled number of samples of >40us in case of directory shrinking. Because these constitute to < 1% of total samples, the overall effect of direcotry shrinking on unlink / rmdir performance is negligible. There was no notable difference in cpu usage.
This patch supersedes the other directory shrinking patch sent in Aug 2019 ("ext4: attempt to shrink directory on dentry removal").
Changes since V1:
* ext4_remove_dx_entry(), dx_probe_dx_node() fixes
* dx_probe_dirent_blk() continuation fix
* Added performance evaluation
Message-ID: <[email protected]>
--- fs/ext4/ext4.h | 3 +- fs/ext4/ext4_jbd2.h | 7 + fs/ext4/inline.c | 2 +- fs/ext4/namei.c | 355 ++++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 355 insertions(+), 12 deletions(-)
fs/ext4/ext4.h +2 -1
@@ -3114,7 +3114,8 @@ extern int ext4_generic_delete_entry(struct inode *dir,struct buffer_head *bh, void *entry_buf, int buf_size, - int csum_size); + int csum_size, + bool *empty);extern bool ext4_empty_dir (struct inode *inode);/* resize.c */
fs/ext4/ext4_jbd2.h +7 -0
@@ -83,6 +83,13 @@*/ #define EXT4_INDEX_EXTRA_TRANS_BLOCKS 12U +/* +* Number of credits needed to perform directory shrinking. For each htree +* level, we need 6 blocks (block in question, its parent, last block, last +* block's parent, bitmap block, bg summary). +*/ +#define EXT4_DIR_SHRINK_TRANS_BLOCKS 6U +#ifdef CONFIG_QUOTA /* Amount of blocks needed for quota update - we know that the structure was * allocated so we need to update only data block */
fs/ext4/inline.c +1 -1
@@ -1673,7 +1673,7 @@ int ext4_delete_inline_entry(handle_t *handle, goto out; err =ext4_generic_delete_entry (dir, de_del, bh, - inline_start, inline_size,0 ); + inline_start, inline_size,0 ,NULL );if (err) goto out;
fs/ext4/namei.c +345 -10
437 lines not on this page · raw message
Signature · 1 line
2.51.0
Re: [PATCH v2 3/3] ext4: reimplement ext4_empty_dir() using is_dirent_block_empty
In reply to Theodore Ts'oHi,
I really appreciate the detailed explanation—your explanation made the current state of this patch set much clearer to me.
I’ll follow up with the work as you suggested.
Quoted 171 lines · Theodore Ts'o
Quoted 3 lines · Julian Sun
Emm. I checked the code and found that support for 3-level htrees was added in 2017 via commit
e08ac99fa2a2("ext4: add largedir feature"), but this patch was submitted in 2020. Did I make a mistake somewhere?I made that statement when I tried forward porting Harshad's patches to 6.17-rc4, and one of the patch conflicts seemed to implyyy that
dx_probepredated the3-level htree change. But I could have been mistaken about why the patch conflict eixsts.Quoted 5 lines
Quoted 3 lines
There are
also some hardening against maliciously fuzzed file systems that will prevent the patches from applying cleanly.Is this included in the xfstests test suite?
For better or for worse, no. The reason for this is that xfstests tends to avoid using pre-generated file systems becase they aren't portable to different file system types. And in order to test deliberately corrupted, you generally need to include small corrupted file systems into xfstests. You *could* try to create them as an ext4-specific test using debugfs to introduce the corruption, but that's quite painful, and in some cases the only way to corrupt the file system in that very specific way would require a hex editor.
That being said, it's generally pretty obvious when bullet-proofing code has been added and it causes a merge conflict. For example, I skipped forward-porting the third patch, "ext4: reimplement
ext4_empty_dir()usingis_dirent_block_empty" because there was some bullet-proofing code added inext4_empty_dir()which caused the patch application to fail, and I was too third try to deal with it, and it strictly speaking wasn't necessary for the patch shrinking functionality. That being said, the bullet proofing which was added toext4_empty_dir(), *should* be added to the newly createdi_dirent_block_empty()function introduced in the 2/3 patch in this series.Quoted 6 lines
Quoted 3 lines
Then we'd need to run regression tests on a variety of different ext4 configurations to see if there are any regressions, and if so, they would need to be fixed.
Is testing with xfstests sufficient? Or are there any other test suites that can be used to test this patch set?
Testing with xfstests are sufficient, but we need to test multiple file system configurations, but just the default "ext4 using a 4k blocksize". So I tried to do a quick-and-dirty forward port of the patches, and they compiled and passed the 15-20 minute smoke test (running "kvm-xfstests smoke", or "gce-xfstests smoke"). But when I ran the full set of tests, this is what I found.
ext4/4k: 594 tests, 4 failures, 65 skipped, 5196 seconds
Failures: generic/426 generic/756
Flaky: generic/041: 20% (1/5) generic/049: 20% (1/5)
ext4/1k: 588 tests, 3 failures, 69 skipped, 6306 seconds
Failures: generic/426 generic/756
Flaky: generic/043: 20% (1/5)
ext4/ext3: 586 tests, 3 failures, 149 skipped, 4958 seconds
Flaky: generic/041: 40% (2/5) generic/426: 60% (3/5)generic/756: 60% (3/5)ext4/encrypt: 569 tests, 3 failures, 181 skipped, 3263 seconds
Failures: generic/426 generic/756
Flaky: generic/049: 40% (2/5)
ext4/nojournal: 586 tests, 3 failures, 137 skipped, 3918 seconds
Failures: ext4/045 generic/426 generic/756
ext4/ext3conv: 591 tests, 4 failures, 67 skipped, 5408 seconds
Failures: generic/426 generic/756
Flaky: ext4/045: 40% (2/5) generic/040: 60% (3/5)
ext4/adv: 587 tests, 10 failures, 73 skipped, 5487 seconds
Failures: [generic/347] generic/426 generic/756 [generic/757] [generic/764][generic/777] Flaky: generic/047: 40% (2/5) generic/475: 20% (1/5) [generic/482: 40% (2/5)] generic/547: 20% (1/5)ext4/dioread_nolock: 592 tests, 3 failures, 65 skipped, 4996 seconds
Failures: generic/426 generic/756
Flaky: generic/047: 40% (2/5)
ext4/data_journal: 587 tests, 6 failures, 141 skipped, 5151 seconds
Failures: [generic/127] generic/426 generic/756
Flaky: generic/049: 60% (3/5) generic/475: 60% (3/5)generic/645: 40% (2/5) ext4/bigalloc_4k: 565 tests, 4 failures, 68 skipped, 5066 seconds Failures: ext4/045 Flaky: generic/320: 60% (3/5) generic/426: 60% (3/5) generic/756: 60% (3/5)ext4/bigalloc_1k: 566 tests, 3 failures, 79 skipped, 5096 seconds
Failures: generic/426 generic/756
Flaky: generic/049: 20% (1/5)
ext4/dax: 578 tests, 5 failures, 170 skipped, 3198 seconds
Failures: [generic/344] [generic/363] generic/426 generic/756
Flaky: generic/043: 20% (1/5)
Totals: 7193 tests, 1264 skipped, 190 failures, 0 errors, 53862s(Tests in square brackets were failing with stock 6.17-rc4, so don't represent regressions. Ignore them for the purposes of trying to fix up this patch set.)
Now, some of thsee may be failures caused by my making a mistake when doing the forward port. Basically, I bashed the patches until they built; and then I ran the smoke test; and then I kicked of the full test run, which takes about 2.5 hours using a dozen VM's.
I don't have time to take this any further, so with your permission, if you're OK with my handing fiishing off this project to you, I'll send the patches as a reply to this message, and then "Your mission, should you use to accept it" (quoting from the "Mission Impossible" TV Show / Movie) would be:
1) Investigate the failures deailed above, and fix them. Again, some of these failures may not be Harshads; it could be mine when I did the forward port. Either way, we need to fix them before we can merge the changes upstream.
2) Do more cleanups on the patches as necessary. While doing the forward port, I noted the following issues that should really be fixed. You might find more things that need improvement; if so, please go for it!
2a) Although we are potentially modifying more metadata blocks in
ext4_try_dir_shrink(), the number of journal credits requested by callers toext4_delete_entry()were not increased. This could result in the handle running out of credits, which will trigger a warning, and if the transaction runs out of space, could trigger a journal abort. One way would simply be to increase the credits passed toext4_journal_start().The other way would be to queue up the work and do the directory shrinking in workqueue, where the changes would be done in a completely separate journal handle. This has the advantage that it avoids increasing the latency to the
unlink()system call, since there is no reason why the change needs to happen as part of the system call, or in the same transaction as the unlink.2b) Error checking is missing in some of the newly added code. In particular the function calls in
make_unindexed()are ignoring error returns. More generally, while we do the directory shrnk, we need to check for potential problems; and if there are any failures, we need to leave the directory unchanged, possibly callingext4_error()if file system corruption is dicovered, or just skipping the directory shirnk operation if it is some transient error --- for example, a memory allocation failure.2c) As mentioned above, I skipped forward porting the 3rd patch series, and there is code to rigorously check for inconsistent file system corruptions lest we trigger a BUG or WARN or worse which is causing the patch application to fail. That checking needs to be added to
is_dirent_block_empty(), and while you're at it, please check all of the newly added code to make sure it won't misbehave if the file system structures have been corrupted in a particualrly inventive way.2d) Once (2c) is done forward port patch #3.
3) As discussed in this thread, once these patches are landed, the further work to merge leaf notes; to remove empty htree nodes; and to shorten the htree depth when necessary.
Quoted 5 lines
Quoted 3 lines
Also, please note that this first set of changes doesn't really make a big difference for real-world use casses, since a directory block won't get dropped when it is completely empty....
Yes, I think the biggest beneficiary is rm -rf-type workloads.
The thing about rm -rf workloads is that if you eventually
rmdir()the directory, all of the blocks will be released anyway. That's probably why this feature is one that hasn't been high priority. If you delete 75% of the files in a directory, you *could* do something like "mkdir foo.new ; mv foo/* foo.new ; rmdir foo ; mv foo.new foo", but of course that won't work if some other process is actively accessing the files when you are trying to do the "DIY directory shrink operation".
Quoted 4 lines
Cheers,
- Ted
Thanks,
Signature · 1 line
Julian Sun <[email protected]>