]> nv-tegra.nvidia Code Review - linux-2.6.git/blobdiff - fs/namei.c
LSM: Remove unused arguments from security_path_truncate().
[linux-2.6.git] / fs / namei.c
index bff27c08134c22ba31a2da070e71a3b1a0df6717..fe34c2b879f4be26c4483f019aa85812e281dce7 100644 (file)
@@ -19,7 +19,6 @@
 #include <linux/slab.h>
 #include <linux/fs.h>
 #include <linux/namei.h>
-#include <linux/quotaops.h>
 #include <linux/pagemap.h>
 #include <linux/fsnotify.h>
 #include <linux/personality.h>
@@ -498,8 +497,6 @@ static int link_path_walk(const char *, struct nameidata *);
 
 static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
 {
-       int res = 0;
-       char *name;
        if (IS_ERR(link))
                goto fail;
 
@@ -510,22 +507,7 @@ static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *l
                path_get(&nd->root);
        }
 
-       res = link_path_walk(link, nd);
-       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_put(&nd->path);
-               return -ENOMEM;
-       }
-       strcpy(name, nd->last.name);
-       nd->last.name = name;
-       return 0;
+       return link_path_walk(link, nd);
 fail:
        path_put(&nd->path);
        return PTR_ERR(link);
@@ -541,16 +523,17 @@ static void path_put_conditional(struct path *path, struct nameidata *nd)
 static inline void path_to_nameidata(struct path *path, struct nameidata *nd)
 {
        dput(nd->path.dentry);
-       if (nd->path.mnt != path->mnt)
+       if (nd->path.mnt != path->mnt) {
                mntput(nd->path.mnt);
-       nd->path.mnt = path->mnt;
+               nd->path.mnt = path->mnt;
+       }
        nd->path.dentry = path->dentry;
 }
 
-static __always_inline int __do_follow_link(struct path *path, struct nameidata *nd)
+static __always_inline int
+__do_follow_link(struct path *path, struct nameidata *nd, void **p)
 {
        int error;
-       void *cookie;
        struct dentry *dentry = path->dentry;
 
        touch_atime(path->mnt, dentry);
@@ -562,9 +545,9 @@ static __always_inline int __do_follow_link(struct path *path, struct nameidata
        }
        mntget(path->mnt);
        nd->last_type = LAST_BIND;
-       cookie = dentry->d_inode->i_op->follow_link(dentry, nd);
-       error = PTR_ERR(cookie);
-       if (!IS_ERR(cookie)) {
+       *p = dentry->d_inode->i_op->follow_link(dentry, nd);
+       error = PTR_ERR(*p);
+       if (!IS_ERR(*p)) {
                char *s = nd_get_link(nd);
                error = 0;
                if (s)
@@ -574,8 +557,6 @@ static __always_inline int __do_follow_link(struct path *path, struct nameidata
                        if (error)
                                path_put(&nd->path);
                }
-               if (dentry->d_inode->i_op->put_link)
-                       dentry->d_inode->i_op->put_link(dentry, nd, cookie);
        }
        return error;
 }
@@ -589,6 +570,7 @@ static __always_inline int __do_follow_link(struct path *path, struct nameidata
  */
 static inline int do_follow_link(struct path *path, struct nameidata *nd)
 {
+       void *cookie;
        int err = -ELOOP;
        if (current->link_count >= MAX_NESTED_LINKS)
                goto loop;
@@ -602,7 +584,9 @@ 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, nd);
+       err = __do_follow_link(path, nd, &cookie);
+       if (!IS_ERR(cookie) && path->dentry->d_inode->i_op->put_link)
+               path->dentry->d_inode->i_op->put_link(path->dentry, nd, cookie);
        path_put(path);
        current->link_count--;
        nd->depth--;
@@ -1375,22 +1359,6 @@ static inline int may_create(struct inode *dir, struct dentry *child)
        return inode_permission(dir, MAY_WRITE | MAY_EXEC);
 }
 
-/* 
- * O_DIRECTORY translates into forcing a directory lookup.
- */
-static inline int lookup_flags(unsigned int f)
-{
-       unsigned long retval = LOOKUP_FOLLOW;
-
-       if (f & O_NOFOLLOW)
-               retval &= ~LOOKUP_FOLLOW;
-       
-       if (f & O_DIRECTORY)
-               retval |= LOOKUP_DIRECTORY;
-
-       return retval;
-}
-
 /*
  * p1 and p2 should be directories on the same fs.
  */
@@ -1448,7 +1416,6 @@ int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
        error = security_inode_create(dir, dentry, mode);
        if (error)
                return error;
-       vfs_dq_init(dir);
        error = dir->i_op->create(dir, dentry, mode, nd);
        if (!error)
                fsnotify_create(dir, dentry);
@@ -1517,8 +1484,7 @@ static int handle_truncate(struct path *path)
         */
        error = locks_verify_locked(inode);
        if (!error)
-               error = security_path_truncate(path, 0,
-                                      ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
+               error = security_path_truncate(path);
        if (!error) {
                error = do_truncate(path->dentry, 0,
                                    ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
@@ -1591,13 +1557,13 @@ static int open_will_truncate(int flag, struct inode *inode)
 }
 
 static struct file *finish_open(struct nameidata *nd,
-                               int open_flag, int flag, int acc_mode)
+                               int open_flag, int acc_mode)
 {
        struct file *filp;
        int will_truncate;
        int error;
 
-       will_truncate = open_will_truncate(flag, nd->path.dentry->d_inode);
+       will_truncate = open_will_truncate(open_flag, nd->path.dentry->d_inode);
        if (will_truncate) {
                error = mnt_want_write(nd->path.mnt);
                if (error)
@@ -1618,9 +1584,6 @@ static struct file *finish_open(struct nameidata *nd,
                }
        }
        if (!IS_ERR(filp)) {
-               if (acc_mode & MAY_WRITE)
-                       vfs_dq_init(nd->path.dentry->d_inode);
-
                if (will_truncate) {
                        error = handle_truncate(&nd->path);
                        if (error) {
@@ -1646,14 +1609,66 @@ exit:
 }
 
 static struct file *do_last(struct nameidata *nd, struct path *path,
-                           int open_flag, int flag, int acc_mode,
-                           int mode, const char *pathname,
-                           struct dentry *dir, int *is_link)
+                           int open_flag, int acc_mode,
+                           int mode, const char *pathname)
 {
+       struct dentry *dir = nd->path.dentry;
        struct file *filp;
-       int error;
+       int error = -EISDIR;
+
+       switch (nd->last_type) {
+       case LAST_DOTDOT:
+               follow_dotdot(nd);
+               dir = nd->path.dentry;
+       case LAST_DOT:
+               if (nd->path.mnt->mnt_sb->s_type->fs_flags & FS_REVAL_DOT) {
+                       if (!dir->d_op->d_revalidate(dir, nd)) {
+                               error = -ESTALE;
+                               goto exit;
+                       }
+               }
+               /* fallthrough */
+       case LAST_ROOT:
+               if (open_flag & O_CREAT)
+                       goto exit;
+               /* fallthrough */
+       case LAST_BIND:
+               audit_inode(pathname, dir);
+               goto ok;
+       }
+
+       /* trailing slashes? */
+       if (nd->last.name[nd->last.len]) {
+               if (open_flag & O_CREAT)
+                       goto exit;
+               nd->flags |= LOOKUP_DIRECTORY | LOOKUP_FOLLOW;
+       }
+
+       /* just plain open? */
+       if (!(open_flag & O_CREAT)) {
+               error = do_lookup(nd, &nd->last, path);
+               if (error)
+                       goto exit;
+               error = -ENOENT;
+               if (!path->dentry->d_inode)
+                       goto exit_dput;
+               if (path->dentry->d_inode->i_op->follow_link)
+                       return NULL;
+               error = -ENOTDIR;
+               if (nd->flags & LOOKUP_DIRECTORY) {
+                       if (!path->dentry->d_inode->i_op->lookup)
+                               goto exit_dput;
+               }
+               path_to_nameidata(path, nd);
+               audit_inode(pathname, nd->path.dentry);
+               goto ok;
+       }
+
+       /* OK, it's O_CREAT */
+       mutex_lock(&dir->d_inode->i_mutex);
 
-       *is_link = 0;
+       path->dentry = lookup_hash(nd);
+       path->mnt = nd->path.mnt;
 
        error = PTR_ERR(path->dentry);
        if (IS_ERR(path->dentry)) {
@@ -1702,28 +1717,28 @@ static struct file *do_last(struct nameidata *nd, struct path *path,
        audit_inode(pathname, path->dentry);
 
        error = -EEXIST;
-       if (flag & O_EXCL)
+       if (open_flag & O_EXCL)
                goto exit_dput;
 
        if (__follow_mount(path)) {
                error = -ELOOP;
-               if (flag & O_NOFOLLOW)
+               if (open_flag & O_NOFOLLOW)
                        goto exit_dput;
        }
 
        error = -ENOENT;
        if (!path->dentry->d_inode)
                goto exit_dput;
-       if (path->dentry->d_inode->i_op->follow_link) {
-               *is_link = 1;
+
+       if (path->dentry->d_inode->i_op->follow_link)
                return NULL;
-       }
 
        path_to_nameidata(path, nd);
        error = -EISDIR;
        if (S_ISDIR(path->dentry->d_inode->i_mode))
                goto exit;
-       filp = finish_open(nd, open_flag, flag, acc_mode);
+ok:
+       filp = finish_open(nd, open_flag, acc_mode);
        return filp;
 
 exit_mutex_unlock:
@@ -1749,11 +1764,12 @@ struct file *do_filp_open(int dfd, const char *pathname,
        struct nameidata nd;
        int error;
        struct path path;
-       struct dentry *dir;
        int count = 0;
        int flag = open_to_namei_flags(open_flag);
        int force_reval = 0;
-       int is_link;
+
+       if (!(open_flag & O_CREAT))
+               mode = 0;
 
        /*
         * O_SYNC is implemented as __O_SYNC|O_DSYNC.  As many places only
@@ -1768,66 +1784,34 @@ struct file *do_filp_open(int dfd, const char *pathname,
                acc_mode = MAY_OPEN | ACC_MODE(open_flag);
 
        /* O_TRUNC implies we need access checks for write permissions */
-       if (flag & O_TRUNC)
+       if (open_flag & O_TRUNC)
                acc_mode |= MAY_WRITE;
 
        /* Allow the LSM permission hook to distinguish append 
           access from general write access. */
-       if (flag & O_APPEND)
+       if (open_flag & O_APPEND)
                acc_mode |= MAY_APPEND;
 
-       /*
-        * The simplest case - just a plain lookup.
-        */
-       if (!(flag & O_CREAT)) {
-               filp = get_empty_filp();
-
-               if (filp == NULL)
-                       return ERR_PTR(-ENFILE);
-               nd.intent.open.file = filp;
-               filp->f_flags = open_flag;
-               nd.intent.open.flags = flag;
-               nd.intent.open.create_mode = 0;
-               error = do_path_lookup(dfd, pathname,
-                                       lookup_flags(flag)|LOOKUP_OPEN, &nd);
-               if (IS_ERR(nd.intent.open.file)) {
-                       if (error == 0) {
-                               error = PTR_ERR(nd.intent.open.file);
-                               path_put(&nd.path);
-                       }
-               } else if (error)
-                       release_open_intent(&nd);
-               if (error)
-                       return ERR_PTR(error);
-               goto ok;
-       }
-
-       /*
-        * Create - we need to know the parent.
-        */
+       /* find the parent */
 reval:
        error = path_init(dfd, pathname, LOOKUP_PARENT, &nd);
        if (error)
                return ERR_PTR(error);
        if (force_reval)
                nd.flags |= LOOKUP_REVAL;
-       error = path_walk(pathname, &nd);
+
+       current->total_link_count = 0;
+       error = link_path_walk(pathname, &nd);
        if (error) {
-               if (nd.root.mnt)
-                       path_put(&nd.root);
-               return ERR_PTR(error);
+               filp = ERR_PTR(error);
+               goto out;
        }
-       if (unlikely(!audit_dummy_context()))
+       if (unlikely(!audit_dummy_context()) && (open_flag & O_CREAT))
                audit_inode(pathname, nd.path.dentry);
 
        /*
-        * We have the parent and last component. First of all, check
-        * that we are not asked to creat(2) an obvious directory - that
-        * will not do.
+        * We have the parent and last component.
         */
-       error = -EISDIR;
-       if (nd.last_type != LAST_NORM || nd.last.name[nd.last.len])
-               goto exit_parent;
 
        error = -ENFILE;
        filp = get_empty_filp();
@@ -1837,96 +1821,77 @@ reval:
        filp->f_flags = open_flag;
        nd.intent.open.flags = flag;
        nd.intent.open.create_mode = mode;
-       dir = nd.path.dentry;
        nd.flags &= ~LOOKUP_PARENT;
-       nd.flags |= LOOKUP_CREATE | LOOKUP_OPEN;
-       if (flag & O_EXCL)
-               nd.flags |= LOOKUP_EXCL;
-       mutex_lock(&dir->d_inode->i_mutex);
-       path.dentry = lookup_hash(&nd);
-       path.mnt = nd.path.mnt;
-
-do_last:
-       filp = do_last(&nd, &path, open_flag, flag, acc_mode, mode,
-                      pathname, dir, &is_link);
-       if (is_link)
-               goto do_link;
-       if (nd.root.mnt)
-               path_put(&nd.root);
-       return filp;
-
-ok:
-       filp = finish_open(&nd, open_flag, flag, acc_mode);
+       nd.flags |= LOOKUP_OPEN;
+       if (open_flag & O_CREAT) {
+               nd.flags |= LOOKUP_CREATE;
+               if (open_flag & O_EXCL)
+                       nd.flags |= LOOKUP_EXCL;
+       }
+       if (open_flag & O_DIRECTORY)
+               nd.flags |= LOOKUP_DIRECTORY;
+       if (!(open_flag & O_NOFOLLOW))
+               nd.flags |= LOOKUP_FOLLOW;
+       filp = do_last(&nd, &path, open_flag, acc_mode, mode, pathname);
+       while (unlikely(!filp)) { /* trailing symlink */
+               struct path holder;
+               struct inode *inode = path.dentry->d_inode;
+               void *cookie;
+               error = -ELOOP;
+               /* S_ISDIR part is a temporary automount kludge */
+               if (!(nd.flags & LOOKUP_FOLLOW) && !S_ISDIR(inode->i_mode))
+                       goto exit_dput;
+               if (count++ == 32)
+                       goto exit_dput;
+               /*
+                * This is subtle. Instead of calling do_follow_link() we do
+                * the thing by hands. The reason is that this way we have zero
+                * link_count and path_walk() (called from ->follow_link)
+                * honoring LOOKUP_PARENT.  After that we have the parent and
+                * last component, i.e. we are in the same situation as after
+                * the first path_walk().  Well, almost - if the last component
+                * is normal we get its copy stored in nd->last.name and we will
+                * have to putname() it when we are done. Procfs-like symlinks
+                * just set LAST_BIND.
+                */
+               nd.flags |= LOOKUP_PARENT;
+               error = security_inode_follow_link(path.dentry, &nd);
+               if (error)
+                       goto exit_dput;
+               error = __do_follow_link(&path, &nd, &cookie);
+               if (unlikely(error)) {
+                       /* nd.path had been dropped */
+                       if (!IS_ERR(cookie) && inode->i_op->put_link)
+                               inode->i_op->put_link(path.dentry, &nd, cookie);
+                       path_put(&path);
+                       release_open_intent(&nd);
+                       filp = ERR_PTR(error);
+                       goto out;
+               }
+               holder = path;
+               nd.flags &= ~LOOKUP_PARENT;
+               filp = do_last(&nd, &path, open_flag, acc_mode, mode, pathname);
+               if (inode->i_op->put_link)
+                       inode->i_op->put_link(holder.dentry, &nd, cookie);
+               path_put(&holder);
+       }
+out:
        if (nd.root.mnt)
                path_put(&nd.root);
+       if (filp == ERR_PTR(-ESTALE) && !force_reval) {
+               force_reval = 1;
+               goto reval;
+       }
        return filp;
 
 exit_dput:
        path_put_conditional(&path, &nd);
-exit:
        if (!IS_ERR(nd.intent.open.file))
                release_open_intent(&nd);
 exit_parent:
-       if (nd.root.mnt)
-               path_put(&nd.root);
        path_put(&nd.path);
-       return ERR_PTR(error);
-
-do_link:
-       error = -ELOOP;
-       if (flag & O_NOFOLLOW)
-               goto exit_dput;
-       /*
-        * This is subtle. Instead of calling do_follow_link() we do the
-        * thing by hands. The reason is that this way we have zero link_count
-        * and path_walk() (called from ->follow_link) honoring LOOKUP_PARENT.
-        * After that we have the parent and last component, i.e.
-        * we are in the same situation as after the first path_walk().
-        * Well, almost - if the last component is normal we get its copy
-        * stored in nd->last.name and we will have to putname() it when we
-        * are done. Procfs-like symlinks just set LAST_BIND.
-        */
-       nd.flags |= LOOKUP_PARENT;
-       error = security_inode_follow_link(path.dentry, &nd);
-       if (error)
-               goto exit_dput;
-       error = __do_follow_link(&path, &nd);
-       path_put(&path);
-       if (error) {
-               /* Does someone understand code flow here? Or it is only
-                * me so stupid? Anathema to whoever designed this non-sense
-                * with "intent.open".
-                */
-               release_open_intent(&nd);
-               if (nd.root.mnt)
-                       path_put(&nd.root);
-               if (error == -ESTALE && !force_reval) {
-                       force_reval = 1;
-                       goto reval;
-               }
-               return ERR_PTR(error);
-       }
-       nd.flags &= ~LOOKUP_PARENT;
-       if (nd.last_type == LAST_BIND)
-               goto ok;
-       error = -EISDIR;
-       if (nd.last_type != LAST_NORM)
-               goto exit;
-       if (nd.last.name[nd.last.len]) {
-               __putname(nd.last.name);
-               goto exit;
-       }
-       error = -ELOOP;
-       if (count++==32) {
-               __putname(nd.last.name);
-               goto exit;
-       }
-       dir = nd.path.dentry;
-       mutex_lock(&dir->d_inode->i_mutex);
-       path.dentry = lookup_hash(&nd);
-       path.mnt = nd.path.mnt;
-       __putname(nd.last.name);
-       goto do_last;
+       filp = ERR_PTR(error);
+       goto out;
 }
 
 /**
@@ -2020,7 +1985,6 @@ int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
        if (error)
                return error;
 
-       vfs_dq_init(dir);
        error = dir->i_op->mknod(dir, dentry, mode, dev);
        if (!error)
                fsnotify_create(dir, dentry);
@@ -2119,7 +2083,6 @@ int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
        if (error)
                return error;
 
-       vfs_dq_init(dir);
        error = dir->i_op->mkdir(dir, dentry, mode);
        if (!error)
                fsnotify_mkdir(dir, dentry);
@@ -2205,8 +2168,6 @@ int vfs_rmdir(struct inode *dir, struct dentry *dentry)
        if (!dir->i_op->rmdir)
                return -EPERM;
 
-       vfs_dq_init(dir);
-
        mutex_lock(&dentry->d_inode->i_mutex);
        dentry_unhash(dentry);
        if (d_mountpoint(dentry))
@@ -2215,8 +2176,10 @@ int vfs_rmdir(struct inode *dir, struct dentry *dentry)
                error = security_inode_rmdir(dir, dentry);
                if (!error) {
                        error = dir->i_op->rmdir(dir, dentry);
-                       if (!error)
+                       if (!error) {
                                dentry->d_inode->i_flags |= S_DEAD;
+                               dont_mount(dentry);
+                       }
                }
        }
        mutex_unlock(&dentry->d_inode->i_mutex);
@@ -2292,8 +2255,6 @@ int vfs_unlink(struct inode *dir, struct dentry *dentry)
        if (!dir->i_op->unlink)
                return -EPERM;
 
-       vfs_dq_init(dir);
-
        mutex_lock(&dentry->d_inode->i_mutex);
        if (d_mountpoint(dentry))
                error = -EBUSY;
@@ -2302,7 +2263,7 @@ int vfs_unlink(struct inode *dir, struct dentry *dentry)
                if (!error) {
                        error = dir->i_op->unlink(dir, dentry);
                        if (!error)
-                               dentry->d_inode->i_flags |= S_DEAD;
+                               dont_mount(dentry);
                }
        }
        mutex_unlock(&dentry->d_inode->i_mutex);
@@ -2406,7 +2367,6 @@ int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
        if (error)
                return error;
 
-       vfs_dq_init(dir);
        error = dir->i_op->symlink(dir, dentry, oldname);
        if (!error)
                fsnotify_create(dir, dentry);
@@ -2490,7 +2450,6 @@ int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_de
                return error;
 
        mutex_lock(&inode->i_mutex);
-       vfs_dq_init(dir);
        error = dir->i_op->link(old_dentry, dir, new_dentry);
        mutex_unlock(&inode->i_mutex);
        if (!error)
@@ -2591,7 +2550,7 @@ SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname
  *     e) conversion from fhandle to dentry may come in the wrong moment - when
  *        we are removing the target. Solution: we will have to grab ->i_mutex
  *        in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
- *        ->i_mutex on parents, which works but leads to some truely excessive
+ *        ->i_mutex on parents, which works but leads to some truly excessive
  *        locking].
  */
 static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
@@ -2615,17 +2574,20 @@ static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
                return error;
 
        target = new_dentry->d_inode;
-       if (target) {
+       if (target)
                mutex_lock(&target->i_mutex);
-               dentry_unhash(new_dentry);
-       }
        if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
                error = -EBUSY;
-       else 
+       else {
+               if (target)
+                       dentry_unhash(new_dentry);
                error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
+       }
        if (target) {
-               if (!error)
+               if (!error) {
                        target->i_flags |= S_DEAD;
+                       dont_mount(new_dentry);
+               }
                mutex_unlock(&target->i_mutex);
                if (d_unhashed(new_dentry))
                        d_rehash(new_dentry);
@@ -2657,7 +2619,7 @@ static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
                error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
        if (!error) {
                if (target)
-                       target->i_flags |= S_DEAD;
+                       dont_mount(new_dentry);
                if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
                        d_move(old_dentry, new_dentry);
        }
@@ -2691,9 +2653,6 @@ int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
        if (!old_dir->i_op->rename)
                return -EPERM;
 
-       vfs_dq_init(old_dir);
-       vfs_dq_init(new_dir);
-
        old_name = fsnotify_oldname_init(old_dentry->d_name.name);
 
        if (is_dir)