vfs: replace parent == dentry->d_parent by IS_ROOT()
PATCH 1/6 vfs: replace parent == dentry->d_parent by IS_ROOT()
---
fs/dcache.c | 21 ++++++++++++--------- fs/namei.c | 4 ++-- 2 files changed, 14 insertions(+), 11 deletions(-)
fs/dcache.c renamed from fs/dcache.c~dcache-cleanup-1 +12 -9
@@ -174,9 +174,12 @@ static struct dentry *d_kill(struct dent dentry_stat.nr_dentry--;/* For d_free, below */ /*drops the locks, at that point nobody can reach this dentry */ dentry_iput (dentry); - parent = dentry->d_parent; +if (IS_ROOT (dentry)) + parent =NULL ; +else + parent = dentry->d_parent;d_free (dentry); -return dentry == parent ?NULL : parent; +return parent; }/*
@@ -666,11 +669,12 @@ static void shrink_dcache_for_umount_subBUG (); } - parent = dentry->d_parent; -if (parent == dentry) +if (IS_ROOT (dentry)) parent =NULL ; -else +else { + parent = dentry->d_parent;atomic_dec (&parent->d_count); + }list_del (&dentry->d_u.d_child); detached++;
@@ -1721,7 +1725,7 @@ static int d_isparent(struct dentry *p1, {struct dentry *p; -for (p = p2; p->d_parent != p; p = p->d_parent) { +for (p = p2; !IS_ROOT (p); p = p->d_parent) {if (p->d_parent == p1)return 1 ; }
@@ -2166,10 +2170,9 @@ int is_subdir(struct dentry * new_dentry seq =read_seqbegin (&rename_lock);for (;;) {if (new_dentry != old_dentry) { -struct dentry * parent = new_dentry->d_parent; -if (parent == new_dentry) +if (IS_ROOT (new_dentry))break ; - new_dentry = parent; + new_dentry = new_dentry->d_parent;continue ; } result =1 ;
fs/namei.c renamed from fs/namei.c~dcache-cleanup-1 +2 -2
@@ -1454,7 +1454,7 @@ struct dentry *lock_rename(struct dentrymutex_lock (&p1->d_inode->i_sb->s_vfs_rename_mutex); -for (p = p1; p->d_parent != p; p = p->d_parent) { +for (p = p1; !IS_ROOT (p); p = p->d_parent) {if (p->d_parent == p2) {mutex_lock_nested (&p2->d_inode->i_mutex,I_MUTEX_PARENT );mutex_lock_nested (&p1->d_inode->i_mutex,I_MUTEX_CHILD );
@@ -1462,7 +1462,7 @@ struct dentry *lock_rename(struct dentry } } -for (p = p2; p->d_parent != p; p = p->d_parent) { +for (p = p2; !IS_ROOT (p); p = p->d_parent) {if (p->d_parent == p1) {mutex_lock_nested (&p1->d_inode->i_mutex,I_MUTEX_PARENT );mutex_lock_nested (&p2->d_inode->i_mutex,I_MUTEX_CHILD );
_
PATCH 2/6 vfs: add d_ancestor()
This adds d_ancestor() instead of d_isparent(), then use it.
If new_dentry == old_dentry, is_subdir() returns 1, looks strange. "new_dentry == old_dentry" is not subdir obviously. But I'm not checking callers for now, so this keeps current behavior.
---
fs/dcache.c | 45 +++++++++++++++++++++++---------------------- fs/namei.c | 22 ++++++++++------------ include/linux/dcache.h | 1 + 3 files changed, 34 insertions(+), 34 deletions(-)
fs/dcache.c renamed from fs/dcache.c~dcache-cleanup-2 +23 -22
@@ -1718,18 +1718,23 @@ void d_move(struct dentry * dentry, struspin_unlock (&dcache_lock); } -/* -* Helper that returns 1 if p1 is a parent of p2, else 0 +/** +* d_ancestor - search for an ancestor +* @p1: ancestor dentry +* @p2: child dentry +* +* Returns the ancestor dentry of p2 which is a child of p1, if p1 is +* an ancestor of p2, else NULL. */ -static int d_isparent (struct dentry *p1,struct dentry *p2) +struct dentry *d_ancestor (struct dentry *p1,struct dentry *p2) {struct dentry *p;for (p = p2; !IS_ROOT (p); p = p->d_parent) {if (p->d_parent == p1) -return 1 ; +return p; } -return 0 ; +return NULL ; }/*
@@ -1753,7 +1758,7 @@ static struct dentry *__d_unalias(struct/* Check for loops */ ret =ERR_PTR (-ELOOP ); -if (d_isparent (alias, dentry)) +if (d_ancestor (alias, dentry)) goto out_err;/* See lock_rename() */
@@ -2153,31 +2158,27 @@ out:* Caller must ensure that "new_dentry" is pinned before calling is_subdir() */ -int is_subdir (struct dentry * new_dentry,struct dentry * old_dentry) +int is_subdir (struct dentry *new_dentry,struct dentry *old_dentry) {int result; -struct dentry * saved = new_dentry;unsigned long seq; -/* need rcu_readlock to protect against the d_parent trashing due to -* d_move +/* FIXME: This is old behavior, needed? Please check callers. */ +if (new_dentry == old_dentry) +return 1 ; + +/* +* Need rcu_readlock to protect against the d_parent trashing +* due to d_move */ rcu_read_lock (); -do { +do {/* for restarting inner loop in case of seq retry */ - new_dentry = saved; - result =0 ; seq =read_seqbegin (&rename_lock); -for (;;) { -if (new_dentry != old_dentry) { -if (IS_ROOT (new_dentry)) -break ; - new_dentry = new_dentry->d_parent; -continue ; - } +if (d_ancestor (old_dentry, new_dentry)) result =1 ; -break ; - } +else + result =0 ; }while (read_seqretry (&rename_lock, seq));rcu_read_unlock ();
include/linux/dcache.h renamed from include/linux/dcache.h~dcache-cleanup-2 +1 -0
@@ -287,6 +287,7 @@ static inline struct dentry *d_add_uniqu/* used for rename() and baskets */ extern void d_move (struct dentry *,struct dentry *); +extern struct dentry *d_ancestor (struct dentry *,struct dentry *);/* appendix may either be NULL or be used for transname suffixes */ extern struct dentry *d_lookup (struct dentry *,struct qstr *);
fs/namei.c renamed from fs/namei.c~dcache-cleanup-2 +10 -12
@@ -1454,20 +1454,18 @@ struct dentry *lock_rename(struct dentrymutex_lock (&p1->d_inode->i_sb->s_vfs_rename_mutex); -for (p = p1; !IS_ROOT (p); p = p->d_parent) { -if (p->d_parent == p2) { -mutex_lock_nested (&p2->d_inode->i_mutex,I_MUTEX_PARENT ); -mutex_lock_nested (&p1->d_inode->i_mutex,I_MUTEX_CHILD ); -return p; - } + p =d_ancestor (p2, p1); +if (p) { +mutex_lock_nested (&p2->d_inode->i_mutex,I_MUTEX_PARENT ); +mutex_lock_nested (&p1->d_inode->i_mutex,I_MUTEX_CHILD ); +return p; } -for (p = p2; !IS_ROOT (p); p = p->d_parent) { -if (p->d_parent == p1) { -mutex_lock_nested (&p1->d_inode->i_mutex,I_MUTEX_PARENT ); -mutex_lock_nested (&p2->d_inode->i_mutex,I_MUTEX_CHILD ); -return p; - } + p =d_ancestor (p1, p2); +if (p) { +mutex_lock_nested (&p1->d_inode->i_mutex,I_MUTEX_PARENT ); +mutex_lock_nested (&p2->d_inode->i_mutex,I_MUTEX_CHILD ); +return p; }mutex_lock_nested (&p1->d_inode->i_mutex,I_MUTEX_PARENT );
_
PATCH 3/6 vfs: add __d_instantiate() helper
This adds __d_instantiate() for users which is already taking dcache_lock, and replace with it.
The part of d_add_ci() isn't equivalent. But it should be needed fsnotify_d_instantiate() actually, because the path is to add the inode to negative dentry. fsnotify_d_instantiate() should be called after change from negative to positive.
---
fs/dcache.c | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-)
fs/dcache.c renamed from fs/dcache.c~dcache-cleanup-3 +16 -15
@@ -981,6 +981,15 @@ struct dentry *d_alloc_name(struct dentrreturn d_alloc (parent, &q); } +/* the caller must hold dcache_lock */ +static void __d_instantiate (struct dentry *dentry,struct inode *inode) +{ +if (inode) +list_add (&dentry->d_alias, &inode->i_dentry); + dentry->d_inode = inode; +fsnotify_d_instantiate (dentry, inode); +} +/** * d_instantiate - fill in inode information for a dentry * @entry: dentry to complete
@@ -1000,10 +1009,7 @@ void d_instantiate(struct dentry *entry, {BUG_ON (!list_empty (&entry->d_alias));spin_lock (&dcache_lock); -if (inode) -list_add (&entry->d_alias, &inode->i_dentry); - entry->d_inode = inode; -fsnotify_d_instantiate (entry, inode); +__d_instantiate (entry, inode);spin_unlock (&dcache_lock);security_d_instantiate (entry, inode); }
@@ -1033,7 +1039,7 @@ static struct dentry *__d_instantiate_ununsigned int hash = entry->d_name.hash;if (!inode) { - entry->d_inode =NULL ; +__d_instantiate (entry,NULL );return NULL ; }
@@ -1052,9 +1058,7 @@ static struct dentry *__d_instantiate_unreturn alias; } -list_add (&entry->d_alias, &inode->i_dentry); - entry->d_inode = inode; -fsnotify_d_instantiate (entry, inode); +__d_instantiate (entry, inode);return NULL ; }
@@ -1213,10 +1217,8 @@ struct dentry *d_splice_alias(struct inod_move (new, dentry);iput (inode); }else { -/* d_instantiate takes dcache_lock, so we do it by hand */ -list_add (&dentry->d_alias, &inode->i_dentry); - dentry->d_inode = inode; -fsnotify_d_instantiate (dentry, inode); +/* already taking dcache_lock, so d_add() by hand */ +__d_instantiate (dentry, inode);spin_unlock (&dcache_lock);security_d_instantiate (dentry, inode);d_rehash (dentry);
@@ -1299,8 +1301,7 @@ struct dentry *d_add_ci(struct dentry *d* d_instantiate() by hand because it takes dcache_lock which * we already hold. */ -list_add (&found->d_alias, &inode->i_dentry); - found->d_inode = inode; +__d_instantiate (found, inode);spin_unlock (&dcache_lock);security_d_instantiate (found, inode);return found;
@@ -1831,7 +1832,7 @@ struct dentry *d_materialise_unique(struif (!inode) { actual = dentry; - dentry->d_inode =NULL ; +__d_instantiate (dentry,NULL ); goto found_lock; }
_
PATCH 4/6 vfs: remove unnecessary fsnotify_d_instantiate()
This calls d_move(), so fsnotify_d_instantiate() is unnecessary like rename path.
---
fs/dcache.c | 1 - 1 file changed, 1 deletion(-)
fs/dcache.c renamed from fs/dcache.c~dcache-cleanup-4 +0 -1
@@ -1210,7 +1210,6 @@ struct dentry *d_splice_alias(struct ino new =__d_find_alias (inode,1 );if (new) {BUG_ON (!(new->d_flags &DCACHE_DISCONNECTED )); -fsnotify_d_instantiate (new, inode);spin_unlock (&dcache_lock);security_d_instantiate (new, inode);d_rehash (dentry);
_
PATCH 5/6 vfs: remove LOOKUP_PARENT from non LOOKUP_PARENT lookup
lookup_hash() with LOOKUP_PARENT is bogus. And this prepares to add new intent on those path.
The user of LOOKUP_PARENT intent is nfs only, and it checks whether nd->flags has LOOKUP_CREATE or LOOKUP_OPEN, so the result is same.
---
fs/namei.c | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-)
fs/namei.c renamed from fs/namei.c~dcache-remove-parent +18 -9
@@ -2170,16 +2170,19 @@ static long do_rmdir(int dfd, const charreturn error;switch (nd.last_type) { -case LAST_DOTDOT : - error = -ENOTEMPTY ; - goto exit1; -case LAST_DOT : - error = -EINVAL ; - goto exit1; -case LAST_ROOT : - error = -EBUSY ; - goto exit1; +case LAST_DOTDOT : + error = -ENOTEMPTY ; + goto exit1; +case LAST_DOT : + error = -EINVAL ; + goto exit1; +case LAST_ROOT : + error = -EBUSY ; + goto exit1; } + + nd.flags &= ~LOOKUP_PARENT ; +mutex_lock_nested (&nd.path.dentry->d_inode->i_mutex,I_MUTEX_PARENT ); dentry =lookup_hash (&nd); error =PTR_ERR (dentry);
@@ -2257,6 +2260,9 @@ static long do_unlinkat(int dfd, const c error = -EISDIR ;if (nd.last_type !=LAST_NORM ) goto exit1; + + nd.flags &= ~LOOKUP_PARENT ; +mutex_lock_nested (&nd.path.dentry->d_inode->i_mutex,I_MUTEX_PARENT ); dentry =lookup_hash (&nd); error =PTR_ERR (dentry);
@@ -2646,6 +2652,9 @@ asmlinkage long sys_renameat(int olddfd,if (newnd.last_type !=LAST_NORM ) goto exit2; + oldnd.flags &= ~LOOKUP_PARENT ; + newnd.flags &= ~LOOKUP_PARENT ; + trap =lock_rename (new_dir, old_dir); old_dentry =lookup_hash (&oldnd);
_
PATCH 6/6 vfs: add LOOKUP_RENAME_TARGET intent
This adds LOOKUP_RENAME_TARGET intent for lookup of rename destination.
LOOKUP_RENAME_TARGET is going to be used like LOOKUP_CREATE. But since the destination of rename() can be existing directory entry, so it has a difference. Although that difference doesn't matter in my usage, this tells it to user of this intent.
---
fs/namei.c | 1 + include/linux/namei.h | 1 + 2 files changed, 2 insertions(+)
fs/namei.c renamed from fs/namei.c~dcache-add-rename-intent +1 -0
@@ -2654,6 +2654,7 @@ asmlinkage long sys_renameat(int olddfd, oldnd.flags &= ~LOOKUP_PARENT ; newnd.flags &= ~LOOKUP_PARENT ; + newnd.flags |=LOOKUP_RENAME_TARGET ; trap =lock_rename (new_dir, old_dir);
include/linux/namei.h renamed from include/linux/namei.h~dcache-add-rename-intent +1 -0
@@ -54,6 +54,7 @@ enum {LAST_NORM, LAST_ROOT, LAST_DOT, LA#define LOOKUP_OPEN 0x0100#define LOOKUP_CREATE 0x0200#define LOOKUP_EXCL 0x0400 +#define LOOKUP_RENAME_TARGET 0x0800extern int user_path_at (int ,const char __user *,unsigned ,struct path *);
_
Re: [PATCH vfs-2.6 6/6] vfs: add LOOKUP_RENAME_TARGET intent
I put these six patches into my tree for sending to Al when he resurfaces.
We're near the end of the merge window and a lot of trees (including git-vfs) have a lot of outstanding material:
-rw-r----- 1 akpm src 2168830 Oct 15 14:24 patches/git-firmware.patch -rw-r----- 1 akpm src 1999274 Oct 15 14:24 patches/git-bdev.patch -rw-r----- 1 akpm src 1643851 Oct 15 14:24 patches/git-tip.patch -rw-r----- 1 akpm src 1137163 Oct 15 14:24 patches/git-uwb.patch -rw-r----- 1 akpm src 1003617 Oct 15 14:24 patches/git-kvm.patch -rw-r----- 1 akpm src 992495 Oct 15 14:24 patches/git-v4l-dvb.patch -rw-r----- 1 akpm src 849673 Oct 15 14:24 patches/git-parisc.patch -rw-r----- 1 akpm src 661519 Oct 15 14:24 patches/git-scsi-post-merge.patch -rw-r----- 1 akpm src 652920 Oct 15 14:23 patches/git-arm.patch -rw-r----- 1 akpm src 589596 Oct 15 14:24 patches/git-creds.patch -rw-r----- 1 akpm src 568498 Oct 15 14:24 patches/git-scsi.patch -rw-r----- 1 akpm src 410896 Oct 15 14:24 patches/git-drm.patch -rw-r----- 1 akpm src 404578 Oct 15 14:24 patches/git-ftrace.patch -rw-r----- 1 akpm src 352603 Oct 15 14:23 patches/git-acpi.patch -rw-r----- 1 akpm src 314552 Oct 15 14:24 patches/git-sh.patch -rw-r----- 1 akpm src 254238 Oct 15 14:24 patches/git-mtd.patch -rw-r----- 1 akpm src 203336 Oct 15 14:24 patches/git-tests.patch -rw-r----- 1 akpm src 199476 Oct 15 14:24 patches/git-timers.patch -rw-r----- 1 akpm src 192928 Oct 15 14:24 patches/git-v9fs.patch -rw-r----- 1 akpm src 158605 Oct 15 14:23 patches/git-avr32.patch -rw-r----- 1 akpm src 152326 Oct 15 14:24 patches/git-input.patch -rw-r----- 1 akpm src 138826 Oct 15 14:24 patches/git-pci.patch -rw-r----- 1 akpm src 135356 Oct 15 14:24 patches/git-slab.patch -rw-r----- 1 akpm src 125258 Oct 15 14:24 patches/git-vfs.patch -rw-r----- 1 akpm src 122588 Oct 15 14:24 patches/git-nfs.patch -rw-r----- 1 akpm src 120826 Oct 15 14:24 patches/git-sparseirq.patch -rw-r----- 1 akpm src 120746 Oct 15 14:24 patches/git-cris.patch -rw-r----- 1 akpm src 105868 Oct 15 14:24 patches/git-kmemcheck.patch -rw-r----- 1 akpm src 101011 Oct 15 14:24 patches/git-mfd.patch -rw-r----- 1 akpm src 93335 Oct 15 14:24 patches/git-tip-core.patch -rw-r----- 1 akpm src 84488 Oct 15 14:24 patches/git-ubifs.patch -rw-r----- 1 akpm src 46595 Oct 15 14:24 patches/git-net-current.patch -rw-r----- 1 akpm src 30945 Oct 15 14:24 patches/git-stackprotector.patch -rw-r----- 1 akpm src 30415 Oct 15 14:24 patches/git-crypto.patch -rw-r----- 1 akpm src 30354 Oct 15 14:24 patches/git-x86.patch -rw-r----- 1 akpm src 29001 Oct 15 14:24 patches/git-fastboot.patch -rw-r----- 1 akpm src 28915 Oct 15 14:24 patches/git-hid.patch -rw-r----- 1 akpm src 26872 Oct 15 14:24 patches/git-wireless-current.patch -rw-r----- 1 akpm src 25944 Oct 15 14:23 patches/git-backlight.patch -rw-r----- 1 akpm src 23001 Oct 15 14:24 patches/git-block.patch -rw-r----- 1 akpm src 20753 Oct 15 14:24 patches/git-sound-current.patch -rw-r----- 1 akpm src 20752 Oct 15 14:24 patches/git-sound.patch -rw-r----- 1 akpm src 20616 Oct 15 14:24 patches/git-semaphore-removal.patch -rw-r----- 1 akpm src 19849 Oct 15 14:24 patches/git-trivial.patch -rw-r----- 1 akpm src 17854 Oct 15 14:24 patches/git-genirq.patch -rw-r----- 1 akpm src 17689 Oct 15 14:24 patches/git-gfs2.patch -rw-r----- 1 akpm src 17135 Oct 15 14:23 patches/git-battery.patch -rw-r----- 1 akpm src 13923 Oct 15 14:24 patches/git-s390.patch -rw-r----- 1 akpm src 12604 Oct 15 14:24 patches/git-nfsd.patch -rw-r----- 1 akpm src 12151 Oct 15 14:24 patches/git-leds.patch -rw-r----- 1 akpm src 8330 Oct 15 14:24 patches/git-cpus4096.patch -rw-r----- 1 akpm src 6612 Oct 15 14:24 patches/git-infiniband.patch -rw-r----- 1 akpm src 4421 Oct 15 14:16 patches/git-tests-git-rejects.patch -rw-r----- 1 akpm src 4270 Oct 15 14:24 patches/git-kgdb.patch -rw-r----- 1 akpm src 3898 Oct 15 14:24 patches/git-ubi.patch -rw-r----- 1 akpm src 3855 Oct 15 14:24 patches/git-safe-poison-pointers.patch -rw-r----- 1 akpm src 3402 Oct 15 14:24 patches/git-xtensa.patch -rw-r----- 1 akpm src 3326 Oct 15 14:24 patches/git-oprofile.patch
Re: [PATCH vfs-2.6 6/6] vfs: add LOOKUP_RENAME_TARGET intent
Quoted 2 lines · Andrew Morton
I put these six patches into my tree for sending to Al when he resurfaces.
Thanks. I'm happy with it.
Quoted 2 lines
We're near the end of the merge window and a lot of trees (including git-vfs) have a lot of outstanding material:
Signature · 1 line
OGAWA Hirofumi <[email protected]>