Merge rsync://rsync.kernel.org/pub/scm/linux/kernel/git/aegl/linux-2.6
[pandora-kernel.git] / fs / namei.c
index 519900d..fa8df81 100644 (file)
@@ -498,12 +498,16 @@ 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);
+       touch_atime(path->mnt, dentry);
        nd_set_link(nd, NULL);
+
+       if (path->mnt == nd->mnt)
+               mntget(path->mnt);
        error = dentry->d_inode->i_op->follow_link(dentry, nd);
        if (!error) {
                char *s = nd_get_link(nd);
@@ -512,6 +516,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;
 }
@@ -538,15 +544,14 @@ static inline int do_follow_link(struct path *path, struct nameidata *nd)
        current->link_count++;
        current->total_link_count++;
        nd->depth++;
-       mntget(path->mnt);
-       err = __do_follow_link(path->dentry, nd);
-       dput(path->dentry);
-       mntput(path->mnt);
+       err = __do_follow_link(path, nd);
        current->link_count--;
        nd->depth--;
        return err;
 loop:
        dput(path->dentry);
+       if (path->mnt != nd->mnt)
+               mntput(path->mnt);
        path_release(nd);
        return err;
 }
@@ -574,80 +579,89 @@ 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 vfsmount **mnt, struct dentry **dentry)
+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 void follow_mount(struct vfsmount **mnt, struct dentry **dentry)
+{
        while (d_mountpoint(*dentry)) {
                struct vfsmount *mounted = lookup_mnt(*mnt, *dentry);
                if (!mounted)
                        break;
+               dput(*dentry);
                mntput(*mnt);
                *mnt = mounted;
-               dput(*dentry);
                *dentry = dget(mounted->mnt_root);
-               res = 1;
        }
-       return res;
 }
 
 /* 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)
+static inline void follow_dotdot(struct nameidata *nd)
 {
        while(1) {
                struct vfsmount *parent;
-               struct dentry *old = *dentry;
+               struct dentry *old = nd->dentry;
 
                 read_lock(&current->fs->lock);
-               if (*dentry == current->fs->root &&
-                   *mnt == current->fs->rootmnt) {
+               if (nd->dentry == current->fs->root &&
+                   nd->mnt == current->fs->rootmnt) {
                         read_unlock(&current->fs->lock);
                        break;
                }
                 read_unlock(&current->fs->lock);
                spin_lock(&dcache_lock);
-               if (*dentry != (*mnt)->mnt_root) {
-                       *dentry = dget((*dentry)->d_parent);
+               if (nd->dentry != nd->mnt->mnt_root) {
+                       nd->dentry = dget(nd->dentry->d_parent);
                        spin_unlock(&dcache_lock);
                        dput(old);
                        break;
                }
                spin_unlock(&dcache_lock);
                spin_lock(&vfsmount_lock);
-               parent = (*mnt)->mnt_parent;
-               if (parent == *mnt) {
+               parent = nd->mnt->mnt_parent;
+               if (parent == nd->mnt) {
                        spin_unlock(&vfsmount_lock);
                        break;
                }
                mntget(parent);
-               *dentry = dget((*mnt)->mnt_mountpoint);
+               nd->dentry = dget(nd->mnt->mnt_mountpoint);
                spin_unlock(&vfsmount_lock);
                dput(old);
-               mntput(*mnt);
-               *mnt = parent;
+               mntput(nd->mnt);
+               nd->mnt = parent;
        }
-       follow_mount(mnt, dentry);
+       follow_mount(&nd->mnt, &nd->dentry);
 }
 
 /*
@@ -668,6 +682,7 @@ static int do_lookup(struct nameidata *nd, struct qstr *name,
 done:
        path->mnt = mnt;
        path->dentry = dentry;
+       __follow_mount(path);
        return 0;
 
 need_lookup:
@@ -755,7 +770,7 @@ static fastcall int __link_path_walk(const char * name, struct nameidata *nd)
                        case 2: 
                                if (this.name[1] != '.')
                                        break;
-                               follow_dotdot(&nd->mnt, &nd->dentry);
+                               follow_dotdot(nd);
                                inode = nd->dentry->d_inode;
                                /* fallthrough */
                        case 1:
@@ -775,8 +790,6 @@ static fastcall int __link_path_walk(const char * name, struct nameidata *nd)
                err = do_lookup(nd, &this, &next);
                if (err)
                        break;
-               /* Check mountpoints.. */
-               follow_mount(&next.mnt, &next.dentry);
 
                err = -ENOENT;
                inode = next.dentry->d_inode;
@@ -799,6 +812,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;
                }
@@ -820,7 +835,7 @@ last_component:
                        case 2: 
                                if (this.name[1] != '.')
                                        break;
-                               follow_dotdot(&nd->mnt, &nd->dentry);
+                               follow_dotdot(nd);
                                inode = nd->dentry->d_inode;
                                /* fallthrough */
                        case 1:
@@ -834,7 +849,6 @@ last_component:
                err = do_lookup(nd, &this, &next);
                if (err)
                        break;
-               follow_mount(&next.mnt, &next.dentry);
                inode = next.dentry->d_inode;
                if ((lookup_flags & LOOKUP_FOLLOW)
                    && inode && inode->i_op && inode->i_op->follow_link) {
@@ -844,6 +858,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;
                }
@@ -883,6 +899,8 @@ return_base:
                return 0;
 out_dput:
                dput(next.dentry);
+               if (nd->mnt != next.mnt)
+                       mntput(next.mnt);
                break;
        }
        path_release(nd);
@@ -1475,12 +1493,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)
@@ -1490,6 +1506,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;
@@ -1501,6 +1520,8 @@ ok:
 
 exit_dput:
        dput(path.dentry);
+       if (nd->mnt != path.mnt)
+               mntput(path.mnt);
 exit:
        path_release(nd);
        return error;
@@ -1523,18 +1544,12 @@ do_link:
        error = security_inode_follow_link(path.dentry, nd);
        if (error)
                goto exit_dput;
-       mntget(path.mnt);
-       error = __do_follow_link(path.dentry, nd);
-       dput(path.dentry);
-       mntput(path.mnt);
-       path.mnt = nd->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;
@@ -1550,6 +1565,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;
 }
@@ -1561,19 +1577,35 @@ do_link:
  *
  * Simple function to lookup and return a dentry and create it
  * if it doesn't exist.  Is SMP-safe.
+ *
+ * Returns with nd->dentry->d_inode->i_sem locked.
  */
 struct dentry *lookup_create(struct nameidata *nd, int is_dir)
 {
-       struct dentry *dentry;
+       struct dentry *dentry = ERR_PTR(-EEXIST);
 
        down(&nd->dentry->d_inode->i_sem);
-       dentry = ERR_PTR(-EEXIST);
+       /*
+        * Yucky last component or no last component at all?
+        * (foo/., foo/.., /////)
+        */
        if (nd->last_type != LAST_NORM)
                goto fail;
        nd->flags &= ~LOOKUP_PARENT;
+
+       /*
+        * Do the final lookup.
+        */
        dentry = lookup_hash(&nd->last, nd->dentry);
        if (IS_ERR(dentry))
                goto fail;
+
+       /*
+        * Special case - lookup gave negative, but... we had foo/bar/
+        * From the vfs_mknod() POV we just have a negative dentry -
+        * all is fine. Let's be bastards - you had / on the end, you've
+        * been asking for (non-existent) directory. -ENOENT for you.
+        */
        if (!is_dir && nd->last.name[nd->last.len] && !dentry->d_inode)
                goto enoent;
        return dentry;