This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / fs / namei.c
index 45b138d..30d31d2 100644 (file)
 #include <linux/smp_lock.h>
 #include <linux/personality.h>
 #include <linux/security.h>
+#include <linux/syscalls.h>
 #include <linux/mount.h>
 #include <linux/audit.h>
+#include <linux/proc_fs.h>
+#include <linux/vserver/inode.h>
+#include <linux/vserver/debug.h>
 #include <asm/namei.h>
 #include <asm/uaccess.h>
 
@@ -115,13 +119,14 @@ static inline int do_getname(const char __user *filename, char *page)
        int retval;
        unsigned long len = PATH_MAX;
 
-       if ((unsigned long) filename >= TASK_SIZE) {
-               if (!segment_eq(get_fs(), KERNEL_DS))
+       if (!segment_eq(get_fs(), KERNEL_DS)) {
+               if ((unsigned long) filename >= TASK_SIZE)
                        return -EFAULT;
-       } else if (TASK_SIZE - (unsigned long) filename < PATH_MAX)
-               len = TASK_SIZE - (unsigned long) filename;
+               if (TASK_SIZE - (unsigned long) filename < PATH_MAX)
+                       len = TASK_SIZE - (unsigned long) filename;
+       }
 
-       retval = strncpy_from_user((char *)page, filename, len);
+       retval = strncpy_from_user(page, filename, len);
        if (retval > 0) {
                if (retval < len)
                        return 0;
@@ -146,45 +151,52 @@ char * getname(const char __user * filename)
                        result = ERR_PTR(retval);
                }
        }
-       if (unlikely(current->audit_context) && !IS_ERR(result) && result)
-               audit_getname(result);
+       audit_getname(result);
        return result;
 }
 
-/*
- *     vfs_permission()
+#ifdef CONFIG_AUDITSYSCALL
+void putname(const char *name)
+{
+       if (unlikely(current->audit_context))
+               audit_putname(name);
+       else
+               __putname(name);
+}
+EXPORT_SYMBOL(putname);
+#endif
+
+
+/**
+ * generic_permission  -  check for access rights on a Posix-like filesystem
+ * @inode:     inode to check access rights for
+ * @mask:      right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
+ * @check_acl: optional callback to check for Posix ACLs
  *
- * is used to check for read/write/execute permissions on a file.
+ * Used to check for read/write/execute permissions on a file.
  * We use "fsuid" for this, letting us set arbitrary permissions
  * for filesystem access without changing the "normal" uids which
  * are used for other things..
  */
-int vfs_permission(struct inode * inode, int mask)
+int generic_permission(struct inode *inode, int mask,
+               int (*check_acl)(struct inode *inode, int mask))
 {
        umode_t                 mode = inode->i_mode;
 
-       if (IS_BARRIER(inode) && !vx_check(0, VX_ADMIN|VX_WATCH))
-               return -EACCES;
-
-       if (mask & MAY_WRITE) {
-               /*
-                * Nobody gets write access to a read-only fs.
-                */
-               if (IS_RDONLY(inode) &&
-                   (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
-                       return -EROFS;
-
-               /*
-                * Nobody gets write access to an immutable file.
-                */
-               if (IS_IMMUTABLE(inode))
-                       return -EACCES;
-       }
-
        if (current->fsuid == inode->i_uid)
                mode >>= 6;
-       else if (in_group_p(inode->i_gid))
-               mode >>= 3;
+       else {
+               if (IS_POSIXACL(inode) && (mode & S_IRWXG) && check_acl) {
+                       int error = check_acl(inode, mask);
+                       if (error == -EACCES)
+                               goto check_capabilities;
+                       else if (error != -EAGAIN)
+                               return error;
+               }
+
+               if (in_group_p(inode->i_gid))
+                       mode >>= 3;
+       }
 
        /*
         * If the DACs are ok we don't need any capability check.
@@ -192,6 +204,7 @@ int vfs_permission(struct inode * inode, int mask)
        if (((mode & mask & (MAY_READ|MAY_WRITE|MAY_EXEC)) == mask))
                return 0;
 
+ check_capabilities:
        /*
         * Read/write DACs are always overridable.
         * Executable DACs are overridable if at least one exec bit is set.
@@ -211,29 +224,54 @@ int vfs_permission(struct inode * inode, int mask)
        return -EACCES;
 }
 
-static inline int xid_permission(struct inode *inode)
+static inline int xid_permission(struct inode *inode, int mask, struct nameidata *nd)
 {
+       if (IS_BARRIER(inode) && !vx_check(0, VX_ADMIN)) {
+               vxwprintk(1, "xid=%d did hit the barrier.",
+                       vx_current_xid());
+               return -EACCES;
+       }
        if (inode->i_xid == 0)
                return 0;
        if (vx_check(inode->i_xid, VX_ADMIN|VX_WATCH|VX_IDENT))
                return 0;
+
+       vxwprintk(1, "xid=%d denied access to %p[#%d,%lu] »%s«.",
+               vx_current_xid(), inode, inode->i_xid, inode->i_ino,
+               vxd_path(nd->dentry, nd->mnt));
        return -EACCES;
 }
 
-int permission(struct inode * inode,int mask, struct nameidata *nd)
+int permission(struct inode *inode, int mask, struct nameidata *nd)
 {
-       int retval;
-       int submask;
+       int retval, submask;
+
+       if (mask & MAY_WRITE) {
+               umode_t mode = inode->i_mode;
+
+               /*
+                * Nobody gets write access to a read-only fs.
+                */
+               if (IS_RDONLY(inode) &&
+                   (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
+                       return -EROFS;
+
+               /*
+                * Nobody gets write access to an immutable file.
+                */
+               if (IS_IMMUTABLE(inode))
+                       return -EACCES;
+       }
+
 
        /* Ordinary permission routines do not understand MAY_APPEND. */
        submask = mask & ~MAY_APPEND;
-
-       if ((retval = xid_permission(inode)))
+       if ((retval = xid_permission(inode, mask, nd)))
                return retval;
        if (inode->i_op && inode->i_op->permission)
                retval = inode->i_op->permission(inode, submask, nd);
        else
-               retval = vfs_permission(inode, submask);
+               retval = generic_permission(inode, submask, NULL);
        if (retval)
                return retval;
 
@@ -292,6 +330,16 @@ void path_release(struct nameidata *nd)
        mntput(nd->mnt);
 }
 
+/*
+ * umount() mustn't call path_release()/mntput() as that would clear
+ * mnt_expiry_mark
+ */
+void path_release_on_umount(struct nameidata *nd)
+{
+       dput(nd->dentry);
+       _mntput(nd->mnt);
+}
+
 /*
  * Internal lookup() using the new generic dcache.
  * SMP-safe
@@ -318,7 +366,7 @@ static struct dentry * cached_lookup(struct dentry * parent, struct qstr * name,
 /*
  * Short-cut version of permission(), for calling by
  * path_walk(), when dcache lock is held.  Combines parts
- * of permission() and vfs_permission(), and tests ONLY for
+ * of permission() and generic_permission(), and tests ONLY for
  * MAY_EXEC permission.
  *
  * If appropriate, check DAC only.  If not appropriate, or
@@ -330,7 +378,7 @@ static inline int exec_permission_lite(struct inode *inode,
 {
        umode_t mode = inode->i_mode;
 
-       if ((inode->i_op && inode->i_op->permission))
+       if (inode->i_op && inode->i_op->permission)
                return -EAGAIN;
 
        if (current->fsuid == inode->i_uid)
@@ -344,6 +392,9 @@ static inline int exec_permission_lite(struct inode *inode,
        if ((inode->i_mode & S_IXUGO) && capable(CAP_DAC_OVERRIDE))
                goto ok;
 
+       if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_OVERRIDE))
+               goto ok;
+
        if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_READ_SEARCH))
                goto ok;
 
@@ -409,6 +460,91 @@ static struct dentry * real_lookup(struct dentry * parent, struct qstr * name, s
        return result;
 }
 
+static int __emul_lookup_dentry(const char *, struct nameidata *);
+
+/* SMP-safe */
+static inline int
+walk_init_root(const char *name, struct nameidata *nd)
+{
+       read_lock(&current->fs->lock);
+       if (current->fs->altroot && !(nd->flags & LOOKUP_NOALT)) {
+               nd->mnt = mntget(current->fs->altrootmnt);
+               nd->dentry = dget(current->fs->altroot);
+               read_unlock(&current->fs->lock);
+               if (__emul_lookup_dentry(name,nd))
+                       return 0;
+               read_lock(&current->fs->lock);
+       }
+       nd->mnt = mntget(current->fs->rootmnt);
+       nd->dentry = dget(current->fs->root);
+       read_unlock(&current->fs->lock);
+       return 1;
+}
+
+static inline int __vfs_follow_link(struct nameidata *nd, const char *link)
+{
+       int res = 0;
+       char *name;
+       if (IS_ERR(link))
+               goto fail;
+
+       if (*link == '/') {
+               path_release(nd);
+               if (!walk_init_root(link, nd))
+                       /* weird __emul_prefix() stuff did it */
+                       goto out;
+       }
+       res = link_path_walk(link, nd);
+out:
+       if (nd->depth || res || nd->last_type!=LAST_NORM)
+               return res;
+       /*
+        * If it is an iterative symlinks resolution in open_namei() we
+        * have to copy the last component. And all that crap because of
+        * bloody create() on broken symlinks. Furrfu...
+        */
+       name = __getname();
+       if (unlikely(!name)) {
+               path_release(nd);
+               return -ENOMEM;
+       }
+       strcpy(name, nd->last.name);
+       nd->last.name = name;
+       return 0;
+fail:
+       path_release(nd);
+       return PTR_ERR(link);
+}
+
+struct path {
+       struct vfsmount *mnt;
+       struct dentry *dentry;
+};
+
+static inline int __do_follow_link(struct path *path, struct nameidata *nd)
+{
+       int error;
+       struct dentry *dentry = path->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);
+               if (s)
+                       error = __vfs_follow_link(nd, s);
+               if (dentry->d_inode->i_op->put_link)
+                       dentry->d_inode->i_op->put_link(dentry, nd);
+       }
+       dput(dentry);
+       mntput(path->mnt);
+
+       return error;
+}
+
 /*
  * This limits recursive symlink follows to 8, while
  * limiting consecutive symlinks to 40.
@@ -416,24 +552,29 @@ static struct dentry * real_lookup(struct dentry * parent, struct qstr * name, s
  * Without that kind of total limit, nasty chains of consecutive
  * symlinks can cause almost arbitrarily long lookups. 
  */
-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;
-       if (current->link_count >= 5)
+       if (current->link_count >= MAX_NESTED_LINKS)
                goto loop;
        if (current->total_link_count >= 40)
                goto loop;
+       BUG_ON(nd->depth >= MAX_NESTED_LINKS);
        cond_resched();
-       err = security_inode_follow_link(dentry, nd);
+       err = security_inode_follow_link(path->dentry, nd);
        if (err)
                goto loop;
        current->link_count++;
        current->total_link_count++;
-       touch_atime(nd->mnt, dentry);
-       err = dentry->d_inode->i_op->follow_link(dentry, nd);
+       nd->depth++;
+       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;
 }
@@ -461,87 +602,92 @@ 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;
+                       /* for sane '/' avoid follow_mount() */
+                       return;
                }
                 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);
 }
 
-struct path {
-       struct vfsmount *mnt;
-       struct dentry *dentry;
-};
-
 /*
  *  It's more convoluted than I'd like it to be, but... it's still fairly
  *  small and for now I'd prefer to have fast path as straight as possible.
@@ -552,15 +698,34 @@ static int do_lookup(struct nameidata *nd, struct qstr *name,
 {
        struct vfsmount *mnt = nd->mnt;
        struct dentry *dentry = __d_lookup(nd->dentry, name);
+       struct inode *inode;
 
        if (!dentry)
                goto need_lookup;
        if (dentry->d_op && dentry->d_op->d_revalidate)
                goto need_revalidate;
+       inode = dentry->d_inode;
+       if (!inode)
+               goto done;
+       if (!vx_check(inode->i_xid, VX_WATCH|VX_ADMIN|VX_HOSTID|VX_IDENT))
+               goto hidden;
+       if (inode->i_sb->s_magic == PROC_SUPER_MAGIC) {
+               struct proc_dir_entry *de = PDE(inode);
+
+               if (de && !vx_hide_check(0, de->vx_flags))
+                       goto hidden;
+       }
 done:
        path->mnt = mnt;
        path->dentry = dentry;
+       __follow_mount(path);
        return 0;
+hidden:
+       vxwprintk(1, "xid=%d did lookup hidden %p[#%d,%lu] »%s«.",
+               vx_current_xid(), inode, inode->i_xid, inode->i_ino,
+               vxd_path(dentry, mnt));
+       dput(dentry);
+       return -ENOENT;
 
 need_lookup:
        dentry = real_lookup(nd->dentry, name, nd);
@@ -582,13 +747,13 @@ fail:
 
 /*
  * Name resolution.
+ * This is the basic name resolution function, turning a pathname into
+ * the final dentry. We expect 'base' to be positive and a directory.
  *
- * This is the basic name resolution function, turning a pathname
- * into the final dentry.
- *
- * We expect 'base' to be positive and a directory.
+ * Returns 0 and nd will have valid dentry and mnt on success.
+ * Returns error and drops reference to input namei data on failure.
  */
-int fastcall link_path_walk(const char * name, struct nameidata *nd)
+static fastcall int __link_path_walk(const char * name, struct nameidata *nd)
 {
        struct path next;
        struct inode *inode;
@@ -601,7 +766,7 @@ int fastcall link_path_walk(const char * name, struct nameidata *nd)
                goto return_reval;
 
        inode = nd->dentry->d_inode;
-       if (current->link_count)
+       if (nd->depth)
                lookup_flags = LOOKUP_FOLLOW;
 
        /* At this point we know we have a real path component. */
@@ -647,7 +812,7 @@ int fastcall 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:
@@ -667,8 +832,6 @@ int fastcall 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;
@@ -679,10 +842,7 @@ int fastcall link_path_walk(const char * name, struct nameidata *nd)
                        goto out_dput;
 
                if (inode->i_op->follow_link) {
-                       mntget(next.mnt);
-                       err = do_follow_link(next.dentry, nd);
-                       dput(next.dentry);
-                       mntput(next.mnt);
+                       err = do_follow_link(&next, nd);
                        if (err)
                                goto return_err;
                        err = -ENOENT;
@@ -694,6 +854,8 @@ int fastcall 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;
                }
@@ -715,7 +877,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:
@@ -729,19 +891,17 @@ 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) {
-                       mntget(next.mnt);
-                       err = do_follow_link(next.dentry, nd);
-                       dput(next.dentry);
-                       mntput(next.mnt);
+                       err = do_follow_link(&next, nd);
                        if (err)
                                goto return_err;
                        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;
                }
@@ -781,6 +941,8 @@ return_base:
                return 0;
 out_dput:
                dput(next.dentry);
+               if (nd->mnt != next.mnt)
+                       mntput(next.mnt);
                break;
        }
        path_release(nd);
@@ -788,42 +950,78 @@ return_err:
        return err;
 }
 
+/*
+ * Wrapper to retry pathname resolution whenever the underlying
+ * file system returns an ESTALE.
+ *
+ * Retry the whole path once, forcing real lookup requests
+ * instead of relying on the dcache.
+ */
+int fastcall link_path_walk(const char *name, struct nameidata *nd)
+{
+       struct nameidata save = *nd;
+       int result;
+
+       /* make sure the stuff we saved doesn't go away */
+       dget(save.dentry);
+       mntget(save.mnt);
+
+       result = __link_path_walk(name, nd);
+       if (result == -ESTALE) {
+               *nd = save;
+               dget(nd->dentry);
+               mntget(nd->mnt);
+               nd->flags |= LOOKUP_REVAL;
+               result = __link_path_walk(name, nd);
+       }
+
+       dput(save.dentry);
+       mntput(save.mnt);
+
+       return result;
+}
+
 int fastcall path_walk(const char * name, struct nameidata *nd)
 {
        current->total_link_count = 0;
        return link_path_walk(name, nd);
 }
 
-/* SMP-safe */
-/* returns 1 if everything is done */
+/* 
+ * SMP-safe: Returns 1 and nd will have valid dentry and mnt, if
+ * everything is done. Returns 0 and drops input nd, if lookup failed;
+ */
 static int __emul_lookup_dentry(const char *name, struct nameidata *nd)
 {
        if (path_walk(name, nd))
                return 0;               /* something went wrong... */
 
        if (!nd->dentry->d_inode || S_ISDIR(nd->dentry->d_inode->i_mode)) {
-               struct nameidata nd_root;
+               struct dentry *old_dentry = nd->dentry;
+               struct vfsmount *old_mnt = nd->mnt;
+               struct qstr last = nd->last;
+               int last_type = nd->last_type;
                /*
                 * NAME was not found in alternate root or it's a directory.  Try to find
                 * it in the normal root:
                 */
-               nd_root.last_type = LAST_ROOT;
-               nd_root.flags = nd->flags;
-               memcpy(&nd_root.intent, &nd->intent, sizeof(nd_root.intent));
+               nd->last_type = LAST_ROOT;
                read_lock(&current->fs->lock);
-               nd_root.mnt = mntget(current->fs->rootmnt);
-               nd_root.dentry = dget(current->fs->root);
+               nd->mnt = mntget(current->fs->rootmnt);
+               nd->dentry = dget(current->fs->root);
                read_unlock(&current->fs->lock);
-               if (path_walk(name, &nd_root))
-                       return 1;
-               if (nd_root.dentry->d_inode) {
+               if (path_walk(name, nd) == 0) {
+                       if (nd->dentry->d_inode) {
+                               dput(old_dentry);
+                               mntput(old_mnt);
+                               return 1;
+                       }
                        path_release(nd);
-                       nd->dentry = nd_root.dentry;
-                       nd->mnt = nd_root.mnt;
-                       nd->last = nd_root.last;
-                       return 1;
                }
-               path_release(&nd_root);
+               nd->dentry = old_dentry;
+               nd->mnt = old_mnt;
+               nd->last = last;
+               nd->last_type = last_type;
        }
        return 1;
 }
@@ -856,31 +1054,14 @@ set_it:
        }
 }
 
-/* SMP-safe */
-static inline int
-walk_init_root(const char *name, struct nameidata *nd)
-{
-       read_lock(&current->fs->lock);
-       if (current->fs->altroot && !(nd->flags & LOOKUP_NOALT)) {
-               nd->mnt = mntget(current->fs->altrootmnt);
-               nd->dentry = dget(current->fs->altroot);
-               read_unlock(&current->fs->lock);
-               if (__emul_lookup_dentry(name,nd))
-                       return 0;
-               read_lock(&current->fs->lock);
-       }
-       nd->mnt = mntget(current->fs->rootmnt);
-       nd->dentry = dget(current->fs->root);
-       read_unlock(&current->fs->lock);
-       return 1;
-}
-
+/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
 int fastcall path_lookup(const char *name, unsigned int flags, struct nameidata *nd)
 {
-       int retval;
+       int retval = 0;
 
        nd->last_type = LAST_ROOT; /* if there are only slashes... */
        nd->flags = flags;
+       nd->depth = 0;
 
        read_lock(&current->fs->lock);
        if (*name=='/') {
@@ -889,24 +1070,22 @@ int fastcall path_lookup(const char *name, unsigned int flags, struct nameidata
                        nd->dentry = dget(current->fs->altroot);
                        read_unlock(&current->fs->lock);
                        if (__emul_lookup_dentry(name,nd))
-                               return 0;
+                               goto out; /* found in altroot */
                        read_lock(&current->fs->lock);
                }
                nd->mnt = mntget(current->fs->rootmnt);
                nd->dentry = dget(current->fs->root);
-       }
-       else{
+       } else {
                nd->mnt = mntget(current->fs->pwdmnt);
                nd->dentry = dget(current->fs->pwd);
        }
        read_unlock(&current->fs->lock);
        current->total_link_count = 0;
        retval = link_path_walk(name, nd);
+out:
        if (unlikely(current->audit_context
                     && nd && nd->dentry && nd->dentry->d_inode))
-               audit_inode(name,
-                           nd->dentry->d_inode->i_ino,
-                           nd->dentry->d_inode->i_rdev);
+               audit_inode(name, nd->dentry->d_inode);
        return retval;
 }
 
@@ -1045,8 +1224,12 @@ static inline int check_sticky(struct inode *dir, struct inode *inode)
 static inline int may_delete(struct inode *dir,struct dentry *victim,int isdir)
 {
        int error;
-       if (!victim->d_inode || victim->d_parent->d_inode != dir)
+
+       if (!victim->d_inode)
                return -ENOENT;
+
+       BUG_ON(victim->d_parent->d_inode != dir);
+
        error = permission(dir,MAY_WRITE | MAY_EXEC, NULL);
        if (error)
                return error;
@@ -1220,6 +1403,11 @@ int may_open(struct nameidata *nd, int acc_mode, int flag)
                        return -EPERM;
        }
 
+       /* O_NOATIME can only be set by the owner or superuser */
+       if (flag & O_NOATIME)
+               if (current->fsuid != inode->i_uid && !capable(CAP_FOWNER))
+                       return -EPERM;
+
        /*
         * Ensure there are no outstanding leases on the file.
         */
@@ -1268,7 +1456,7 @@ int may_open(struct nameidata *nd, int acc_mode, int flag)
 int open_namei(const char * pathname, int flag, int mode, struct nameidata *nd)
 {
        int acc_mode, error = 0;
-       struct dentry *dentry;
+       struct path path;
        struct dentry *dir;
        int count = 0;
 
@@ -1312,23 +1500,24 @@ int open_namei(const char * pathname, int flag, int mode, struct nameidata *nd)
        dir = nd->dentry;
        nd->flags &= ~LOOKUP_PARENT;
        down(&dir->d_inode->i_sem);
-       dentry = __lookup_hash(&nd->last, nd->dentry, nd);
+       path.dentry = __lookup_hash(&nd->last, nd->dentry, nd);
+       path.mnt = nd->mnt;
 
 do_last:
-       error = PTR_ERR(dentry);
-       if (IS_ERR(dentry)) {
+       error = PTR_ERR(path.dentry);
+       if (IS_ERR(path.dentry)) {
                up(&dir->d_inode->i_sem);
                goto exit;
        }
 
        /* Negative dentry, just create the file */
-       if (!dentry->d_inode) {
+       if (!path.dentry->d_inode) {
                if (!IS_POSIXACL(dir->d_inode))
                        mode &= ~current->fs->umask;
-               error = vfs_create(dir->d_inode, dentry, mode, nd);
+               error = vfs_create(dir->d_inode, path.dentry, mode, nd);
                up(&dir->d_inode->i_sem);
                dput(nd->dentry);
-               nd->dentry = dentry;
+               nd->dentry = path.dentry;
                if (error)
                        goto exit;
                /* Don't check for write permission, don't truncate */
@@ -1346,22 +1535,24 @@ do_last:
        if (flag & O_EXCL)
                goto exit_dput;
 
-       if (d_mountpoint(dentry)) {
+       if (__follow_mount(&path)) {
                error = -ELOOP;
                if (flag & O_NOFOLLOW)
                        goto exit_dput;
-               while (__follow_down(&nd->mnt,&dentry) && d_mountpoint(dentry));
        }
        error = -ENOENT;
-       if (!dentry->d_inode)
+       if (!path.dentry->d_inode)
                goto exit_dput;
-       if (dentry->d_inode->i_op && dentry->d_inode->i_op->follow_link)
+       if (path.dentry->d_inode->i_op && path.dentry->d_inode->i_op->follow_link)
                goto do_link;
 
        dput(nd->dentry);
-       nd->dentry = dentry;
+       nd->dentry = path.dentry;
+       if (nd->mnt != path.mnt)
+               mntput(nd->mnt);
+       nd->mnt = path.mnt;
        error = -EISDIR;
-       if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode))
+       if (path.dentry->d_inode && S_ISDIR(path.dentry->d_inode->i_mode))
                goto exit;
 ok:
        error = may_open(nd, acc_mode, flag);
@@ -1370,7 +1561,9 @@ ok:
        return 0;
 
 exit_dput:
-       dput(dentry);
+       dput(path.dentry);
+       if (nd->mnt != path.mnt)
+               mntput(path.mnt);
 exit:
        path_release(nd);
        return error;
@@ -1390,19 +1583,15 @@ do_link:
         * are done. Procfs-like symlinks just set LAST_BIND.
         */
        nd->flags |= LOOKUP_PARENT;
-       error = security_inode_follow_link(dentry, nd);
+       error = security_inode_follow_link(path.dentry, nd);
        if (error)
                goto exit_dput;
-       touch_atime(nd->mnt, dentry);
-       error = dentry->d_inode->i_op->follow_link(dentry, nd);
-       dput(dentry);
+       error = __do_follow_link(&path, nd);
        if (error)
                return error;
        nd->flags &= ~LOOKUP_PARENT;
-       if (nd->last_type == LAST_BIND) {
-               dentry = nd->dentry;
+       if (nd->last_type == LAST_BIND)
                goto ok;
-       }
        error = -EISDIR;
        if (nd->last_type != LAST_NORM)
                goto exit;
@@ -1417,7 +1606,8 @@ do_link:
        }
        dir = nd->dentry;
        down(&dir->d_inode->i_sem);
-       dentry = __lookup_hash(&nd->last, nd->dentry, nd);
+       path.dentry = __lookup_hash(&nd->last, nd->dentry, nd);
+       path.mnt = nd->mnt;
        putname(nd->last.name);
        goto do_last;
 }
@@ -1451,6 +1641,7 @@ enoent:
 fail:
        return dentry;
 }
+EXPORT_SYMBOL_GPL(lookup_create);
 
 int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
 {
@@ -1597,20 +1788,16 @@ out:
  * if it cannot handle the case of removing a directory
  * that is still in use by something else..
  */
-static void d_unhash(struct dentry *dentry)
+void dentry_unhash(struct dentry *dentry)
 {
        dget(dentry);
-       spin_lock(&dcache_lock);
-       switch (atomic_read(&dentry->d_count)) {
-       default:
-               spin_unlock(&dcache_lock);
+       if (atomic_read(&dentry->d_count))
                shrink_dcache_parent(dentry);
-               spin_lock(&dcache_lock);
-               if (atomic_read(&dentry->d_count) != 2)
-                       break;
-       case 2:
+       spin_lock(&dcache_lock);
+       spin_lock(&dentry->d_lock);
+       if (atomic_read(&dentry->d_count) == 2)
                __d_drop(dentry);
-       }
+       spin_unlock(&dentry->d_lock);
        spin_unlock(&dcache_lock);
 }
 
@@ -1627,7 +1814,7 @@ int vfs_rmdir(struct inode *dir, struct dentry *dentry)
        DQUOT_INIT(dir);
 
        down(&dentry->d_inode->i_sem);
-       d_unhash(dentry);
+       dentry_unhash(dentry);
        if (d_mountpoint(dentry))
                error = -EBUSY;
        else {
@@ -1758,13 +1945,12 @@ asmlinkage long sys_unlink(const char __user * pathname)
                dput(dentry);
        }
        up(&nd.dentry->d_inode->i_sem);
+       if (inode)
+               iput(inode);    /* truncate the inode here */
 exit1:
        path_release(&nd);
 exit:
        putname(name);
-
-       if (inode)
-               iput(inode);    /* truncate the inode here */
        return error;
 
 slashes:
@@ -1773,7 +1959,7 @@ slashes:
        goto exit2;
 }
 
-int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
+int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname, int mode)
 {
        int error = may_create(dir, dentry, NULL);
 
@@ -1817,7 +2003,7 @@ asmlinkage long sys_symlink(const char __user * oldname, const char __user * new
                dentry = lookup_create(&nd, 0);
                error = PTR_ERR(dentry);
                if (!IS_ERR(dentry)) {
-                       error = vfs_symlink(nd.dentry->d_inode, dentry, from);
+                       error = vfs_symlink(nd.dentry->d_inode, dentry, from, S_IALLUGO);
                        dput(dentry);
                }
                up(&nd.dentry->d_inode->i_sem);
@@ -1947,8 +2133,8 @@ exit:
  *        ->i_sem on parents, which works but leads to some truely excessive
  *        locking].
  */
-int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
-              struct inode *new_dir, struct dentry *new_dentry)
+static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
+                         struct inode *new_dir, struct dentry *new_dentry)
 {
        int error = 0;
        struct inode *target;
@@ -1970,7 +2156,7 @@ int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
        target = new_dentry->d_inode;
        if (target) {
                down(&target->i_sem);
-               d_unhash(new_dentry);
+               dentry_unhash(new_dentry);
        }
        if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
                error = -EBUSY;
@@ -1992,8 +2178,8 @@ int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
        return error;
 }
 
-int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
-              struct inode *new_dir, struct dentry *new_dentry)
+static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
+                           struct inode *new_dir, struct dentry *new_dentry)
 {
        struct inode *target;
        int error;
@@ -2175,40 +2361,23 @@ out:
        return len;
 }
 
-static inline int
-__vfs_follow_link(struct nameidata *nd, const char *link)
+/*
+ * A helper for ->readlink().  This should be used *ONLY* for symlinks that
+ * have ->follow_link() touching nd only in nd_set_link().  Using (or not
+ * using) it for any given inode is up to filesystem.
+ */
+int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
 {
-       int res = 0;
-       char *name;
-       if (IS_ERR(link))
-               goto fail;
-
-       if (*link == '/') {
-               path_release(nd);
-               if (!walk_init_root(link, nd))
-                       /* weird __emul_prefix() stuff did it */
-                       goto out;
-       }
-       res = link_path_walk(link, nd);
-out:
-       if (current->link_count || res || nd->last_type!=LAST_NORM)
-               return res;
-       /*
-        * If it is an iterative symlinks resolution in open_namei() we
-        * have to copy the last component. And all that crap because of
-        * bloody create() on broken symlinks. Furrfu...
-        */
-       name = __getname();
-       if (unlikely(!name)) {
-               path_release(nd);
-               return -ENOMEM;
+       struct nameidata nd;
+       int res;
+       nd.depth = 0;
+       res = dentry->d_inode->i_op->follow_link(dentry, &nd);
+       if (!res) {
+               res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
+               if (dentry->d_inode->i_op->put_link)
+                       dentry->d_inode->i_op->put_link(dentry, &nd);
        }
-       strcpy(name, nd->last.name);
-       nd->last.name = name;
-       return 0;
-fail:
-       path_release(nd);
-       return PTR_ERR(link);
+       return res;
 }
 
 int vfs_follow_link(struct nameidata *nd, const char *link)
@@ -2251,16 +2420,24 @@ int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
        return res;
 }
 
-int page_follow_link(struct dentry *dentry, struct nameidata *nd)
+int page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
 {
-       struct page *page = NULL;
-       char *s = page_getlink(dentry, &page);
-       int res = __vfs_follow_link(nd, s);
-       if (page) {
+       struct page *page;
+       nd_set_link(nd, page_getlink(dentry, &page));
+       return 0;
+}
+
+void page_put_link(struct dentry *dentry, struct nameidata *nd)
+{
+       if (!IS_ERR(nd_get_link(nd))) {
+               struct page *page;
+               page = find_get_page(dentry->d_inode->i_mapping, 0);
+               if (!page)
+                       BUG();
                kunmap(page);
                page_cache_release(page);
+               page_cache_release(page);
        }
-       return res;
 }
 
 int page_symlink(struct inode *inode, const char *symname, int len)
@@ -2305,8 +2482,9 @@ fail:
 }
 
 struct inode_operations page_symlink_inode_operations = {
-       .readlink       = page_readlink,
-       .follow_link    = page_follow_link,
+       .readlink       = generic_readlink,
+       .follow_link    = page_follow_link_light,
+       .put_link       = page_put_link,
 };
 
 EXPORT_SYMBOL(__user_walk);
@@ -2315,10 +2493,10 @@ EXPORT_SYMBOL(follow_up);
 EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
 EXPORT_SYMBOL(getname);
 EXPORT_SYMBOL(lock_rename);
-EXPORT_SYMBOL(lookup_create);
 EXPORT_SYMBOL(lookup_hash);
 EXPORT_SYMBOL(lookup_one_len);
-EXPORT_SYMBOL(page_follow_link);
+EXPORT_SYMBOL(page_follow_link_light);
+EXPORT_SYMBOL(page_put_link);
 EXPORT_SYMBOL(page_readlink);
 EXPORT_SYMBOL(page_symlink);
 EXPORT_SYMBOL(page_symlink_inode_operations);
@@ -2332,9 +2510,11 @@ EXPORT_SYMBOL(vfs_follow_link);
 EXPORT_SYMBOL(vfs_link);
 EXPORT_SYMBOL(vfs_mkdir);
 EXPORT_SYMBOL(vfs_mknod);
-EXPORT_SYMBOL(vfs_permission);
+EXPORT_SYMBOL(generic_permission);
 EXPORT_SYMBOL(vfs_readlink);
 EXPORT_SYMBOL(vfs_rename);
 EXPORT_SYMBOL(vfs_rmdir);
 EXPORT_SYMBOL(vfs_symlink);
 EXPORT_SYMBOL(vfs_unlink);
+EXPORT_SYMBOL(dentry_unhash);
+EXPORT_SYMBOL(generic_readlink);