[PATCH] namei fixes (14/19)
[safe/jmp/linux-2.6] / fs / namei.c
index e42f7c3..411bb32 100644 (file)
@@ -498,12 +498,15 @@ struct path {
        struct dentry *dentry;
 };
 
-static inline int __do_follow_link(struct dentry *dentry, struct nameidata *nd)
+static inline int __do_follow_link(struct path *path, struct nameidata *nd)
 {
        int error;
+       struct dentry *dentry = path->dentry;
 
        touch_atime(nd->mnt, dentry);
        nd_set_link(nd, NULL);
+
+       mntget(path->mnt);
        error = dentry->d_inode->i_op->follow_link(dentry, nd);
        if (!error) {
                char *s = nd_get_link(nd);
@@ -512,6 +515,8 @@ static inline int __do_follow_link(struct dentry *dentry, struct nameidata *nd)
                if (dentry->d_inode->i_op->put_link)
                        dentry->d_inode->i_op->put_link(dentry, nd);
        }
+       dput(dentry);
+       mntput(path->mnt);
 
        return error;
 }
@@ -526,7 +531,6 @@ static inline int __do_follow_link(struct dentry *dentry, struct nameidata *nd)
 static inline int do_follow_link(struct path *path, struct nameidata *nd)
 {
        int err = -ELOOP;
-       mntget(path->mnt);
        if (current->link_count >= MAX_NESTED_LINKS)
                goto loop;
        if (current->total_link_count >= 40)
@@ -539,16 +543,17 @@ static inline int do_follow_link(struct path *path, struct nameidata *nd)
        current->link_count++;
        current->total_link_count++;
        nd->depth++;
-       err = __do_follow_link(path->dentry, nd);
+       if (path->mnt != nd->mnt)
+               mntput(nd->mnt);
+       err = __do_follow_link(path, nd);
        current->link_count--;
        nd->depth--;
-       dput(path->dentry);
-       mntput(path->mnt);
        return err;
 loop:
-       path_release(nd);
+       if (path->mnt != nd->mnt)
+               mntput(nd->mnt);
        dput(path->dentry);
-       mntput(path->mnt);
+       path_release(nd);
        return err;
 }
 
@@ -575,6 +580,23 @@ int follow_up(struct vfsmount **mnt, struct dentry **dentry)
 /* no need for dcache_lock, as serialization is taken care in
  * namespace.c
  */
+static int __follow_mount(struct path *path)
+{
+       int res = 0;
+       while (d_mountpoint(path->dentry)) {
+               struct vfsmount *mounted = lookup_mnt(path->mnt, path->dentry);
+               if (!mounted)
+                       break;
+               dput(path->dentry);
+               if (res)
+                       mntput(path->mnt);
+               path->mnt = mounted;
+               path->dentry = dget(mounted->mnt_root);
+               res = 1;
+       }
+       return res;
+}
+
 static int follow_mount(struct vfsmount **mnt, struct dentry **dentry)
 {
        int res = 0;
@@ -594,26 +616,21 @@ static int follow_mount(struct vfsmount **mnt, struct dentry **dentry)
 /* no need for dcache_lock, as serialization is taken care in
  * namespace.c
  */
-static inline int __follow_down(struct vfsmount **mnt, struct dentry **dentry)
+int follow_down(struct vfsmount **mnt, struct dentry **dentry)
 {
        struct vfsmount *mounted;
 
        mounted = lookup_mnt(*mnt, *dentry);
        if (mounted) {
+               dput(*dentry);
                mntput(*mnt);
                *mnt = mounted;
-               dput(*dentry);
                *dentry = dget(mounted->mnt_root);
                return 1;
        }
        return 0;
 }
 
-int follow_down(struct vfsmount **mnt, struct dentry **dentry)
-{
-       return __follow_down(mnt,dentry);
-}
 static inline void follow_dotdot(struct vfsmount **mnt, struct dentry **dentry)
 {
        while(1) {
@@ -777,7 +794,7 @@ static fastcall int __link_path_walk(const char * name, struct nameidata *nd)
                if (err)
                        break;
                /* Check mountpoints.. */
-               follow_mount(&next.mnt, &next.dentry);
+               __follow_mount(&next);
 
                err = -ENOENT;
                inode = next.dentry->d_inode;
@@ -800,6 +817,8 @@ static fastcall int __link_path_walk(const char * name, struct nameidata *nd)
                                break;
                } else {
                        dput(nd->dentry);
+                       if (nd->mnt != next.mnt)
+                               mntput(nd->mnt);
                        nd->mnt = next.mnt;
                        nd->dentry = next.dentry;
                }
@@ -835,7 +854,7 @@ last_component:
                err = do_lookup(nd, &this, &next);
                if (err)
                        break;
-               follow_mount(&next.mnt, &next.dentry);
+               __follow_mount(&next);
                inode = next.dentry->d_inode;
                if ((lookup_flags & LOOKUP_FOLLOW)
                    && inode && inode->i_op && inode->i_op->follow_link) {
@@ -845,6 +864,8 @@ last_component:
                        inode = nd->dentry->d_inode;
                } else {
                        dput(nd->dentry);
+                       if (nd->mnt != next.mnt)
+                               mntput(nd->mnt);
                        nd->mnt = next.mnt;
                        nd->dentry = next.dentry;
                }
@@ -884,6 +905,8 @@ return_base:
                return 0;
 out_dput:
                dput(next.dentry);
+               if (nd->mnt != next.mnt)
+                       mntput(nd->mnt);
                break;
        }
        path_release(nd);
@@ -1476,12 +1499,10 @@ do_last:
        if (flag & O_EXCL)
                goto exit_dput;
 
-       if (d_mountpoint(path.dentry)) {
+       if (__follow_mount(&path)) {
                error = -ELOOP;
                if (flag & O_NOFOLLOW)
                        goto exit_dput;
-               while (__follow_down(&path.mnt,&path.dentry) && d_mountpoint(path.dentry));
-               nd->mnt = path.mnt;
        }
        error = -ENOENT;
        if (!path.dentry->d_inode)
@@ -1491,6 +1512,9 @@ do_last:
 
        dput(nd->dentry);
        nd->dentry = path.dentry;
+       if (nd->mnt != path.mnt)
+               mntput(nd->mnt);
+       nd->mnt = path.mnt;
        error = -EISDIR;
        if (path.dentry->d_inode && S_ISDIR(path.dentry->d_inode->i_mode))
                goto exit;
@@ -1502,6 +1526,8 @@ ok:
 
 exit_dput:
        dput(path.dentry);
+       if (nd->mnt != path.mnt)
+               mntput(path.mnt);
 exit:
        path_release(nd);
        return error;
@@ -1524,16 +1550,15 @@ do_link:
        error = security_inode_follow_link(path.dentry, nd);
        if (error)
                goto exit_dput;
-       error = __do_follow_link(path.dentry, nd);
-       dput(path.dentry);
-       path.mnt = nd->mnt;
+       if (nd->mnt != path.mnt)
+               mntput(nd->mnt);
+       nd->mnt = path.mnt;
+       error = __do_follow_link(&path, nd);
        if (error)
                return error;
        nd->flags &= ~LOOKUP_PARENT;
-       if (nd->last_type == LAST_BIND) {
-               path.dentry = nd->dentry;
+       if (nd->last_type == LAST_BIND)
                goto ok;
-       }
        error = -EISDIR;
        if (nd->last_type != LAST_NORM)
                goto exit;
@@ -1549,6 +1574,7 @@ do_link:
        dir = nd->dentry;
        down(&dir->d_inode->i_sem);
        path.dentry = __lookup_hash(&nd->last, nd->dentry, nd);
+       path.mnt = nd->mnt;
        putname(nd->last.name);
        goto do_last;
 }