exfat: speed up file creation in large directories
PATCH v2 1/1 exfat: speed up file creation in large directories
Negative name lookups scan a directory from the beginning. Repeating this scan before creating each file makes bulk file creation approach O(N^2).
Add a 64 KiB per-directory Bloom filter and retain the next empty entry found during directory scans. A definite Bloom filter miss skips the directory scan, while a possible match falls back to the normal lookup path, preserving correctness in the presence of hash collisions.
To avoid overhead for small directories, build the filter lazily when a directory reaches 1024 on-disk directory entries. A filter reclaimed under memory pressure is rebuilt on demand.
Manage the filters using a per-superblock LRU and shrinker so that filters for inactive directories can be reclaimed under memory pressure.
Test environment:
QEMU TCG multi-thread, 4 vCPUs, 6 GiB RAM
4 GiB exFAT image, 32 KiB clusters
Test script:
mount -t exfat /dev/vda /mnt/test
time sh -c '
i=0
while [ "$i" -lt 20000 ]; do
: > "/mnt/test/f.$i" || exit 1
i=$((i + 1))
done'
The measured results were:
Before After
real 589.48 s 5.33 s user 4.72 s 2.28 s sys 584.63 s 3.03 s
The elapsed time was reduced by 99.10%, from 589.48 seconds to 5.33 seconds, corresponding to a 110.6-fold speedup.
---
Changes in v2:
- Move exfat_name_filter_free() from exfat_free_inode() to exfat_evict_inode(), after truncate_inode_pages_final(), because ->free_inode() may run from an RCU callback in softirq context.
- Link to v1: https://lore.kernel.org/all/[email protected]/
--- fs/exfat/dir.c | 280 ++++++++++++++++++++++++++++++++++++++++++++ fs/exfat/exfat_fs.h | 22 ++++ fs/exfat/inode.c | 1 + fs/exfat/namei.c | 44 ++++++- fs/exfat/super.c | 9 ++ 5 files changed, 355 insertions(+), 1 deletion(-)
fs/exfat/dir.c +280 -0
@@ -8,6 +8,9 @@#include <linux/bio.h> #include <linux/buffer_head.h> #include <linux/filelock.h> +#include <linux/hash.h> +#include <linux/shrinker.h> +#include <linux/stringhash.h> #include "exfat_raw.h" #include "exfat_fs.h"
@@ -65,6 +68,279 @@ static int exfat_get_uniname_from_ext_entry(struct super_block *sb,return 0 ; } +static u32 exfat_name_filter_hash (struct super_block *sb, +const struct exfat_uni_name *name) +{ +unsigned long hash =init_name_hash (NULL ); +int i; + +for (i =0 ; i < name->name_len; i++) + hash =partial_name_hash (exfat_toupper (sb, name->name[i]), hash); + +return end_name_hash (hash); +} + +static void exfat_name_filter_indexes (struct super_block *sb, +const struct exfat_uni_name *name, +unsigned int indexes[3 ]) +{ +u32 hash =exfat_name_filter_hash (sb, name); + + indexes[0 ] =hash_32 (hash,EXFAT_NAME_FILTER_ORDER ); + indexes[1 ] =hash_32 (hash ^0x9e3779b9U ,EXFAT_NAME_FILTER_ORDER ); + indexes[2 ] =hash_32 (rol32 (hash,16 ) ^0x85ebca6bU , +EXFAT_NAME_FILTER_ORDER ); +} + +static unsigned long *exfat_name_filter_detach_locked ( +struct exfat_sb_info *sbi, +struct exfat_inode_info *ei) +{ +unsigned long *filter = ei->name_filter; + +if (!filter) +return NULL ; + + ei->name_filter =NULL ; +list_del_init (&ei->name_filter_lru); + sbi->name_filter_count--; +return filter; +} + +static void exfat_name_filter_touch (struct exfat_inode_info *ei) +{ +struct exfat_sb_info *sbi =EXFAT_SB (ei->vfs_inode.i_sb); + +spin_lock (&sbi->name_filter_lock); +if (ei->name_filter) +list_move_tail (&ei->name_filter_lru, &sbi->name_filter_lru); +spin_unlock (&sbi->name_filter_lock); +} + +void exfat_name_filter_free (struct inode *inode) +{ +struct exfat_inode_info *ei =EXFAT_I (inode); +struct exfat_sb_info *sbi; +unsigned long *filter; + +if (!READ_ONCE (ei->name_filter)) +return ; + + sbi =EXFAT_SB (inode->i_sb); +spin_lock (&sbi->name_filter_lock); + filter =exfat_name_filter_detach_locked (sbi, ei); +spin_unlock (&sbi->name_filter_lock); +kvfree (filter); +} + +static unsigned long exfat_name_filter_count_objects ( +struct shrinker *shrinker, +struct shrink_control *sc) +{ +struct exfat_sb_info *sbi = shrinker->private_data; +unsigned long count; + +spin_lock (&sbi->name_filter_lock); + count = sbi->name_filter_count; +spin_unlock (&sbi->name_filter_lock); + +return count ? count :SHRINK_EMPTY ; +} + +static unsigned long exfat_name_filter_scan_objects ( +struct shrinker *shrinker, +struct shrink_control *sc) +{ +struct exfat_sb_info *sbi = shrinker->private_data; +unsigned long freed =0 ; + +/* Avoid reclaim recursion from a GFP_NOFS allocation under s_lock. */ +if (!mutex_trylock (&sbi->s_lock)) { + sc->nr_scanned =0 ; +return SHRINK_STOP ; + } + +while (freed < sc->nr_to_scan) { +struct exfat_inode_info *ei; +unsigned long *filter; + +spin_lock (&sbi->name_filter_lock); +if (list_empty (&sbi->name_filter_lru)) { +spin_unlock (&sbi->name_filter_lock); +break ; + } + + ei =list_first_entry (&sbi->name_filter_lru, + struct exfat_inode_info, + name_filter_lru); + filter =exfat_name_filter_detach_locked (sbi, ei); +spin_unlock (&sbi->name_filter_lock); + +kvfree (filter); + freed++; +cond_resched (); + } + +mutex_unlock (&sbi->s_lock); + sc->nr_scanned = freed; +return freed; +} + +void exfat_name_filter_shrinker_register (struct super_block *sb) +{ +struct exfat_sb_info *sbi =EXFAT_SB (sb); +struct shrinker *shrinker; + + shrinker =shrinker_alloc (SHRINKER_NONSLAB , +"exfat-name-filter:%s" , sb->s_id); +if (!shrinker) { +exfat_warn (sb,"failed to allocate name filter shrinker" ); +return ; + } + + shrinker->count_objects = exfat_name_filter_count_objects; + shrinker->scan_objects = exfat_name_filter_scan_objects; + shrinker->private_data = sbi; +shrinker_register (shrinker); + sbi->name_filter_shrinker = shrinker; +} + +void exfat_name_filter_shrinker_unregister (struct super_block *sb) +{ +struct exfat_sb_info *sbi =EXFAT_SB (sb); +struct shrinker *shrinker = sbi->name_filter_shrinker; + + sbi->name_filter_shrinker =NULL ; +shrinker_free (shrinker); + +for (;;) { +struct exfat_inode_info *ei; +unsigned long *filter; + +spin_lock (&sbi->name_filter_lock); +if (list_empty (&sbi->name_filter_lru)) { +spin_unlock (&sbi->name_filter_lock); +break ; + } + + ei =list_first_entry (&sbi->name_filter_lru, + struct exfat_inode_info, + name_filter_lru); + filter =exfat_name_filter_detach_locked (sbi, ei); +spin_unlock (&sbi->name_filter_lock); +kvfree (filter); + } +} + +bool exfat_name_filter_maybe_contains (struct inode *inode, +const struct exfat_uni_name *name) +{ +struct exfat_inode_info *ei =EXFAT_I (inode); +unsigned int indexes[3 ]; + +if (!ei->name_filter) +return true; + +exfat_name_filter_touch (ei); +exfat_name_filter_indexes (inode->i_sb, name, indexes); +return test_bit (indexes[0 ], ei->name_filter) && +test_bit (indexes[1 ], ei->name_filter) && +test_bit (indexes[2 ], ei->name_filter); +} + +void exfat_name_filter_add (struct inode *inode, +const struct exfat_uni_name *name) +{ +struct exfat_inode_info *ei =EXFAT_I (inode); +unsigned int indexes[3 ]; + +if (!ei->name_filter) +return ; + +exfat_name_filter_touch (ei); +exfat_name_filter_indexes (inode->i_sb, name, indexes); +__set_bit (indexes[0 ], ei->name_filter); +__set_bit (indexes[1 ], ei->name_filter); +__set_bit (indexes[2 ], ei->name_filter); +} + +/* +* Build a complete filter only after a directory becomes large enough for +* repeated negative linear lookups to matter. A filter hit is never trusted: +* it only allows definite misses to skip the on-disk scan. +*/ +static void exfat_build_name_filter (struct super_block *sb, +struct exfat_inode_info *ei, +struct exfat_chain *p_dir) +{ +unsigned long *filter; +struct exfat_chain clu; +unsigned int clu_count =0 ; +struct inode *inode = &ei->vfs_inode; +struct exfat_sb_info *sbi =EXFAT_SB (sb); +int i; + +if (!sbi->name_filter_shrinker || ei->name_filter || +exfat_bytes_to_dentries (i_size_read (inode)) < +EXFAT_NAME_FILTER_MIN_DENTRIES ) +return ; + + filter =kvzalloc (EXFAT_NAME_FILTER_BYTES ,GFP_NOFS ); +if (!filter) +return ; + +exfat_chain_dup (&clu, p_dir); +while (clu.dir !=EXFAT_EOF_CLUSTER ) { +for (i =0 ; i < sbi->dentries_per_clu; i++) { +struct exfat_uni_name name = { }; +struct exfat_dentry *ep; +struct buffer_head *bh; +unsigned int type; +unsigned int indexes[3 ]; +int len; + + ep =exfat_get_dentry (sb, &clu, i, &bh); +if (!ep) + goto abort; + + type =exfat_get_entry_type (ep); +brelse (bh); +if (type ==TYPE_UNUSED ) + goto complete; +if (type !=TYPE_FILE && type !=TYPE_DIR ) +continue ; + +if (exfat_get_uniname_from_ext_entry (sb, &clu, i, + name.name)) + goto abort; +for (len =0 ; len <MAX_NAME_LENGTH && name.name[len]; len++) + ; +if (!len || len ==MAX_NAME_LENGTH ) + goto abort; + name.name_len = len; +exfat_name_filter_indexes (sb, &name, indexes); +__set_bit (indexes[0 ], filter); +__set_bit (indexes[1 ], filter); +__set_bit (indexes[2 ], filter); + } + +if (exfat_chain_advance (sb, &clu,1 )) + goto abort; +if (unlikely (++clu_count >EXFAT_DATA_CLUSTER_COUNT (sbi))) + goto abort; + } + +complete: +spin_lock (&sbi->name_filter_lock); + ei->name_filter = filter; +list_add_tail (&ei->name_filter_lru, &sbi->name_filter_lru); + sbi->name_filter_count++; +spin_unlock (&sbi->name_filter_lock); +return ; +abort: +kvfree (filter); +} +/* read a directory entry from the opened directory */ static int exfat_readdir (struct inode *inode,loff_t *cpos,struct exfat_dir_entry *dir_entry) {
@@ -992,6 +1268,8 @@ int exfat_find_dir_entry(struct super_block *sb, struct exfat_inode_info *ei,if (num_entries <0 )return num_entries; +if (!exfat_name_filter_maybe_contains (&ei->vfs_inode, p_uniname)) +return -ENOENT ; dentries_per_clu = sbi->dentries_per_clu;
@@ -1153,6 +1431,8 @@ int exfat_find_dir_entry(struct super_block *sb, struct exfat_inode_info *ei, ei->hint_femp.count =0 ; } +exfat_build_name_filter (sb, ei, p_dir); +/* initialized hint_stat */ hint_stat->clu = p_dir->dir; hint_stat->eidx =0 ;
fs/exfat/exfat_fs.h +22 -0
@@ -14,6 +14,8 @@#include <uapi/linux/exfat.h> #include <linux/buffer_head.h> +struct shrinker ; +#define EXFAT_ROOT_INO 1/*
@@ -120,6 +122,11 @@ enum {#define DIR_CACHE_SIZE \ (DIV_ROUND_UP(ES_MAX_ENTRY_NUM << DENTRY_SIZE_BITS, SECTOR_SIZE) + 1) +#define EXFAT_NAME_FILTER_ORDER 19 +#define EXFAT_NAME_FILTER_BITS BIT(EXFAT_NAME_FILTER_ORDER) +#define EXFAT_NAME_FILTER_BYTES (EXFAT_NAME_FILTER_BITS >> 3) +#define EXFAT_NAME_FILTER_MIN_DENTRIES 1024 +/* Superblock flags */ #define EXFAT_FLAGS_SHUTDOWN 1
@@ -255,6 +262,10 @@ struct exfat_sb_info {spinlock_t inode_hash_lock;struct hlist_head inode_hashtable[EXFAT_HASH_SIZE ]; +spinlock_t name_filter_lock; +struct list_head name_filter_lru; +unsigned long name_filter_count; +struct shrinker *name_filter_shrinker;struct rcu_head rcu; };
@@ -284,6 +295,9 @@ struct exfat_inode_info {struct exfat_hint hint_stat;/* hint for first empty entry */ struct exfat_hint_femp hint_femp; +/* Complete, in-memory Bloom filter of directory names */ +unsigned long *name_filter; +struct list_head name_filter_lru;spinlock_t cache_lru_lock;struct list_head cache_lru;
@@ -619,6 +633,14 @@ int exfat_read_volume_label(struct super_block *sb,int exfat_write_volume_label (struct super_block *sb,struct exfat_uni_name *label); +bool exfat_name_filter_maybe_contains (struct inode *inode, +const struct exfat_uni_name *name); +void exfat_name_filter_add (struct inode *inode, +const struct exfat_uni_name *name); +void exfat_name_filter_free (struct inode *inode); +void exfat_name_filter_shrinker_register (struct super_block *sb); +void exfat_name_filter_shrinker_unregister (struct super_block *sb); +static inline int exfat_chain_advance (struct super_block *sb,struct exfat_chain *chain,unsigned int step) {
fs/exfat/inode.c +1 -0
@@ -445,6 +445,7 @@ struct inode *exfat_build_inode(struct super_block *sb,void exfat_evict_inode (struct inode *inode) {truncate_inode_pages_final (&inode->i_data); +exfat_name_filter_free (inode);if (!inode->i_nlink) {i_size_write (inode,0 );
fs/exfat/namei.c +43 -1
74 lines not on this page · raw message
fs/exfat/super.c +9 -0
@@ -45,6 +45,7 @@ static void exfat_put_super(struct super_block *sb) {struct exfat_sb_info *sbi =EXFAT_SB (sb); +exfat_name_filter_shrinker_unregister (sb);mutex_lock (&sbi->s_lock);exfat_clear_volume_dirty (sb);exfat_free_bitmap (sbi);
@@ -195,6 +196,8 @@ static struct inode *exfat_alloc_inode(struct super_block *sb)if (!ei)return NULL ; + ei->name_filter =NULL ; +INIT_LIST_HEAD (&ei->name_filter_lru);return &ei->vfs_inode; }
@@ -731,6 +734,8 @@ static int exfat_fill_super(struct super_block *sb, struct fs_context *fc) goto free_table; } +exfat_name_filter_shrinker_register (sb); +return 0 ; put_inode:
@@ -826,6 +831,10 @@ static int exfat_init_fs_context(struct fs_context *fc)mutex_init (&sbi->s_lock);mutex_init (&sbi->bitmap_lock); +spin_lock_init (&sbi->name_filter_lock); +INIT_LIST_HEAD (&sbi->name_filter_lru); + sbi->name_filter_count =0 ; + sbi->name_filter_shrinker =NULL ;ratelimit_state_init (&sbi->ratelimit,DEFAULT_RATELIMIT_INTERVAL ,DEFAULT_RATELIMIT_BURST );
Signature · 1 line
2.34.1
Quoted 48 lines · Yang Wen
Negative name lookups scan a directory from the beginning. Repeating this scan before creating each file makes bulk file creation approach O(N^2).
Add a 64 KiB per-directory Bloom filter and retain the next empty entry found during directory scans. A definite Bloom filter miss skips the directory scan, while a possible match falls back to the normal lookup path, preserving correctness in the presence of hash collisions.
To avoid overhead for small directories, build the filter lazily when a directory reaches 1024 on-disk directory entries. A filter reclaimed under memory pressure is rebuilt on demand.
Manage the filters using a per-superblock LRU and shrinker so that filters for inactive directories can be reclaimed under memory pressure.
Test environment:
QEMU TCG multi-thread, 4 vCPUs, 6 GiB RAM
4 GiB exFAT image, 32 KiB clustersTest script:
mount -t exfat /dev/vda /mnt/test time sh -c ' i=0 while [ "$i" -lt 20000 ]; do : > "/mnt/test/f.$i" || exit 1 i=$((i + 1)) done 'The measured results were:
Before After
real 589.48 s 5.33 s user 4.72 s 2.28 s sys 584.63 s 3.03 sThe elapsed time was reduced by 99.10%, from 589.48 seconds to 5.33 seconds, corresponding to a 110.6-fold speedup.
Signed-off-by Yang Wen <[email protected]>---
Changes in v2:
- Moveexfat_name_filter_free()fromexfat_free_inode()toexfat_evict_inode(), aftertruncate_inode_pages_final(), because ->free_inode() may run from an RCU callback in softirq context.
- Link to v1: https://lore.kernel.org/all/[email protected]/
Quoted 7 lines
--- fs/exfat/dir.c | 280 ++++++++++++++++++++++++++++++++++++++++++++ fs/exfat/exfat_fs.h | 22 ++++ fs/exfat/inode.c | 1 + fs/exfat/namei.c | 44 ++++++- fs/exfat/super.c | 9 ++ 5 files changed, 355 insertions(+), 1 deletion(-)
This is a fairly large patch, which makes the review a bit more challenging. Perhaps next time you could consider splitting it into multiple patches in a patch series :)
Quoted 5 lines · Chi Zhiling
This is a fairly large patch, which makes the review a bit more challenging. Perhaps next time you could consider splitting it into multiple patches in a patch series :)
Reviewed-by Chi Zhiling <[email protected]>
Thank you very much for the review and the suggestion.
I agree that the current patch is too large and difficult to review. In the next revision, I will split it into separate patches for the Bloom filter, empty-entry hint, and shrinker changes.
Thanks again for your time and the Reviewed-by tag.
Namjae Jeon
maintainerQuoted 16 lines
Test environment:
QEMU TCG multi-thread, 4 vCPUs, 6 GiB RAM
4 GiB exFAT image, 32 KiB clustersTest script:
mount -t exfat/dev/vda/mnt/test
time sh -c 'i=0 while [ "$i" -lt 20000 ]; do : > "/mnt/test/f.$i" || exit 1 i=$((i + 1)) done'
The measured results were:
Quoted 7 lines
Before After
real 589.48 s 5.33 s user 4.72 s 2.28 s sys 584.63 s 3.03 sThe elapsed time was reduced by 99.10%, from 589.48 seconds to 5.33 seconds, corresponding to a 110.6-fold speedup.
Looks great!
fs/exfat/dir.c static int exfat_get_uniname_from_ext_entry(struct super_block *sb, · line 319
Quoted 4 lines
fs/exfat/dir.c +4
-0+for (len =0 ; len <MAX_NAME_LENGTH && name.name[len]; len++) + ; +if (!len || len ==MAX_NAME_LENGTH ) + goto abort;
exfat allows filenames with 255 UTF16 code units, but this check aborts filter construction. Any reason ?
fs/exfat/namei.c static int exfat_find(struct inode *dir, const struct qstr *qname, · line 670
Quoted 7 lines
fs/exfat/namei.c +2
-1@@ -627,7 +666,8 @@ static int exfat_find(struct inode *dir, const struct qstr *qname, ei->hint_stat.clu = cdir.dir; ei->hint_stat.eidx =0 ; ei->version = (inode_peek_iversion_raw (dir) &0xffffffff ); - ei->hint_femp.eidx =EXFAT_HINT_NONE ; +if (!ei->name_filter) + ei->hint_femp.eidx =EXFAT_HINT_NONE ;
There is no path that moves this hint backwards. exfat_unlink(), exfat_rmdir(), __exfat_rename(), and the rollback path in exfat_add_entry() free entries without updating ei->hint_femp. So, exfat_find_empty_entry() can keep searching from a stale hint and never reuse earlier holes. Repeated create/unlink operations may grow the directory until MAX_EXFAT_DENTRIES.
Thank you for pointing these out.
You are right about the 255 UTF-16 code unit case.I overlooked this boundary condition.
The empty-entry hint can also become stale when an earlier entry is freed. I will update the relevant rollback, unlink, rmdir, rename, and move paths so that the next allocation can rediscover and reuse earlier holes.
I will fix both issues and reorganize the changes into a patch series in the next revision.
Thanks again for the careful review.