fs: add a DCACHE_NEED_LOOKUP flag for d_flags V2
Subsystems (2)
PATCH 1/2 fs: add a DCACHE_NEED_LOOKUP flag for d_flags V2
Btrfs (and I'd venture most other fs's) stores its indexes in nice disk order for readdir, but unfortunately in the case of anything that stats the files in order that readdir spits back (like oh say ls) that means we still have to do the normal lookup of the file, which means looking up our other index and then looking up the inode. What I want is a way to create dummy dentries when we find them in readdir so that when ls or anything else subsequently does a stat(), we already have the location information in the dentry and can go straight to the inode itself. The lookup stuff just assumes that if it finds a dentry it is done, it doesn't perform a lookup. So add a DCACHE_NEED_LOOKUP flag so that the lookup code knows it still needs to run i_op->lookup() on the parent to get the inode for the dentry. I have tested this with btrfs and I went from something that looks like this
http://people.redhat.com/jwhiter/ls-noreada.png
To this
http://people.redhat.com/jwhiter/ls-good.png
Or 2100 seconds to 900 seconds. This was on a 40gb fs that was completely full of empty files. Thanks,
---V1->V2: Add a d_clear_need_lookup helper to clear the need lookup flag. This also does it _after_ unhashing the dentry so we don't race with lookups.
fs/dcache.c | 25 ++++++++++++++++++++ fs/namei.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++ include/linux/dcache.h | 7 +++++ 3 files changed, 90 insertions(+), 0 deletions(-)
fs/dcache.c +25 -0
@@ -343,6 +343,24 @@ void d_drop(struct dentry *dentry)EXPORT_SYMBOL (d_drop);/* +* d_clear_need_lookup - drop a dentry from cache and clear the need lookup flag +* @dentry: dentry to drop +* +* This is called when we do a lookup on a placeholder dentry that needed to be +* looked up. The dentry should have been hashed in order for it to be found by +* the lookup code, but now needs to be unhashed while we do the actual lookup +* and clear the DCACHE_NEED_LOOKUP flag. +*/ +void d_clear_need_lookup (struct dentry *dentry) +{ +spin_lock (&dentry->d_lock); +__d_drop (dentry); + dentry->d_flags &= ~DCACHE_NEED_LOOKUP ; +spin_unlock (&dentry->d_lock); +} +EXPORT_SYMBOL (d_clear_need_lookup); + +/* * Finish off a dentry we've decided to kill. * dentry->d_lock must be held, returns with it unlocked. * If ref is non-zero, then decrement the refcount too.
@@ -1703,6 +1721,13 @@ struct dentry *d_add_ci(struct dentry *dentry, struct inode *inode, }/* +* We are going to instantiate this dentry, unhash it and clear the +* lookup flag so we can do that. +*/ +if (unlikely (d_need_lookup (found))) +d_clear_need_lookup (found); + +/* * Negative dentry: instantiate it unless the inode is a directory and * already has a dentry. */
fs/namei.c +58 -0
88 lines not on this page · raw message
include/linux/dcache.h +7 -0
@@ -216,6 +216,7 @@ struct dentry_operations {#define DCACHE_MOUNTED 0x10000/* is a mountpoint */ #define DCACHE_NEED_AUTOMOUNT 0x20000/* handle automount on this dir */ #define DCACHE_MANAGE_TRANSIT 0x40000/* manage transit from this dirent */ +#define DCACHE_NEED_LOOKUP 0x80000/* dentry requires i_op->lookup */ #define DCACHE_MANAGED_DENTRY \ (DCACHE_MOUNTED|DCACHE_NEED_AUTOMOUNT|DCACHE_MANAGE_TRANSIT)
@@ -416,6 +417,12 @@ static inline bool d_mountpoint(struct dentry *dentry)return dentry->d_flags &DCACHE_MOUNTED ; } +static inline bool d_need_lookup (struct dentry *dentry) +{ +return dentry->d_flags &DCACHE_NEED_LOOKUP ; +} + +extern void d_clear_need_lookup (struct dentry *dentry);extern struct dentry *lookup_create (struct nameidata *nd,int is_dir);extern int sysctl_vfs_cache_pressure;
Signature · 1 line
1.7.2.3
PATCH 2/2 Btrfs: load the key from the dir item in readdir into a fake dentry V2
In btrfs we have 2 indexes for inodes. One is for readdir, it's in this nice sequential order and works out brilliantly for readdir. However if you use ls, it usually stat's each file it gets from readdir. This is where the second index comes in, which is based on a hash of the name of the file. So then the lookup has to lookup this index, and then lookup the inode. The index lookup is going to be in random order (since its based on the name hash), which gives us less than stellar performance. Since we know the inode location from the readdir index, I create a dummy dentry and copy the location key into dentry->d_fsdata. Then on lookup if we have d_fsdata we use that location to lookup the inode, avoiding looking up the other directory index. Thanks,
---V1->V2: use the d_clear_need_lookup() helper instead of d_drop.
fs/btrfs/inode.c | 47 +++++++++++++++++++++++++++++++++++++++++++++-- 1 files changed, 45 insertions(+), 2 deletions(-)
fs/btrfs/inode.c +45 -2
@@ -4120,12 +4120,19 @@ struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)struct btrfs_root *sub_root = root;struct btrfs_key location;int index; -int ret; +int ret =0 ;if (dentry->d_name.len >BTRFS_NAME_LEN )return ERR_PTR (-ENAMETOOLONG ); - ret =btrfs_inode_by_name (dir, dentry, &location); +if (unlikely (d_need_lookup (dentry))) { +memcpy (&location, dentry->d_fsdata,sizeof (struct btrfs_key )); +kfree (dentry->d_fsdata); + dentry->d_fsdata =NULL ; +d_clear_need_lookup (dentry); + }else { + ret =btrfs_inode_by_name (dir, dentry, &location); + }if (ret <0 )return ERR_PTR (ret);
@@ -4180,6 +4187,12 @@ static int btrfs_dentry_delete(const struct dentry *dentry)return 0 ; } +static void btrfs_dentry_release (struct dentry *dentry) +{ +if (dentry->d_fsdata) +kfree (dentry->d_fsdata); +} +static struct dentry *btrfs_lookup (struct inode *dir,struct dentry *dentry,struct nameidata *nd) {
@@ -4206,6 +4219,7 @@ static int btrfs_real_readdir(struct file *filp, void *dirent,struct btrfs_key key;struct btrfs_key found_key;struct btrfs_path *path; +struct qstr q;int ret;struct extent_buffer *leaf;int slot;
@@ -4284,6 +4298,7 @@ static int btrfs_real_readdir(struct file *filp, void *dirent,while (di_cur < di_total) {struct btrfs_key location; +struct dentry *tmp;if (verify_dir_item (root, leaf, di))break ;
@@ -4304,6 +4319,33 @@ static int btrfs_real_readdir(struct file *filp, void *dirent, d_type = btrfs_filetype_table[btrfs_dir_type (leaf, di)];btrfs_dir_item_key_to_cpu (leaf, di, &location); + q.name = name_ptr; + q.len = name_len; + q.hash =full_name_hash (q.name, q.len); + tmp =d_lookup (filp->f_dentry, &q); +if (!tmp) { +struct btrfs_key *newkey; + + newkey =kzalloc (sizeof (struct btrfs_key ), +GFP_NOFS ); +if (!newkey) + goto no_dentry; + tmp =d_alloc (filp->f_dentry, &q); +if (!tmp) { +kfree (newkey); +dput (tmp); + goto no_dentry; + } +memcpy (newkey, &location, +sizeof (struct btrfs_key )); + tmp->d_fsdata = newkey; + tmp->d_flags |=DCACHE_NEED_LOOKUP ; +d_rehash (tmp); +dput (tmp); + }else { +dput (tmp); + } +no_dentry:/* is this a reference to our own snapshot? If so * skip it */
@@ -7566,4 +7608,5 @@ static const struct inode_operations btrfs_symlink_inode_operations = {const struct dentry_operations btrfs_dentry_operations = { .d_delete = btrfs_dentry_delete, + .d_release = btrfs_dentry_release, };
Signature · 1 line
1.7.2.3
Al Viro
maintainerfs/namei.c static int do_lookup(struct nameidata *nd, struct qstr *name, · line 1268
Quoted 6 lines · Josef Bacik
fs/namei.c +6
-0+if (unlikely (d_need_lookup (dentry))) { +if (nameidata_dentry_drop_rcu (nd, dentry)) +return -ECHILD ; +dput (dentry); + dentry =NULL ; + goto retry;
Yecchhh... How about simple goto unlazy; here instead and doing the rest there? Especially since you have the same kind of thing elsewhere in the same sucker. It had been bloody painful to untangle that thing; let's not add to the rat's nest that still remains...
Quoted 12 lines · Al Viro
fs/namei.c static int do_lookup(struct nameidata *nd, struct qstr *name, · line 1268
Quoted 6 lines · Josef Bacik
fs/namei.c +6
-0+if (unlikely (d_need_lookup (dentry))) { +if (nameidata_dentry_drop_rcu (nd, dentry)) +return -ECHILD ; +dput (dentry); + dentry =NULL ; + goto retry;Yecchhh... How about simple goto unlazy; here instead and doing the rest there? Especially since you have the same kind of thing elsewhere in the same sucker. It had been bloody painful to untangle that thing; let's not add to the rat's nest that still remains...
This is where I was a little confused, which is why I added this code. It seems that having goto unlazy; will mean that we will come down to this section
if (unlikely(status <= 0 )) {
if (status < 0) {
dput(dentry);
return status;
}
if (!d_invalidate(dentry)) {
dput(dentry);
dentry = NULL;
need_reval = 1;
goto retry;
}
}and d_invalidate will unhash us so we won't find our new dentry in the cache which makes this whole exercise useless. Is there a different way you'd like
this cleaned up? Thanks,
Josef
Al Viro
maintainerQuoted 31 lines · Josef Bacik
Quoted 12 lines · Al Viro
fs/namei.c static int do_lookup(struct nameidata *nd, struct qstr *name, · line 1268
Quoted 6 lines · Josef Bacik
fs/namei.c +6
-0+if (unlikely (d_need_lookup (dentry))) { +if (nameidata_dentry_drop_rcu (nd, dentry)) +return -ECHILD ; +dput (dentry); + dentry =NULL ; + goto retry;Yecchhh... How about simple goto unlazy; here instead and doing the rest there? Especially since you have the same kind of thing elsewhere in the same sucker. It had been bloody painful to untangle that thing; let's not add to the rat's nest that still remains...
This is where I was a little confused, which is why I added this code. It seems that having goto unlazy; will mean that we will come down to this section
if (unlikely(status <= 0 )) { if (status < 0) { dput(dentry); return status; } if (!d_invalidate(dentry)) { dput(dentry); dentry = NULL; need_reval = 1; goto retry; } }
Quoted 4 lines
and
d_invalidatewill unhash us so we won't find our new dentry in the cache which makes this whole exercise useless. Is there a different way you'd like
this cleaned up? Thanks,
Not *into* the loop; just before the beginning of that loop. IOW, put
if (dentry && unlikely(dentry->d_flags & DCACHE_NEED_LOOKUP)) {
dput(dentry);
dentry = NULL;
}
just before retry: instead of doing it in non-lazy branch. Voila - your
code in the lazy branch becomes
if (unlikely(dentry->d_flags & DCACHE_NEED_LOOKUP))
goto unlazy;and that's it. Can you resend it with such modifications?
ObMemoryPressureIssues: I really hoped to get Dave's patch (per-sb shrinkers) in that cycle, but it'll probably have to wait for the next one...