[XFS] cleanup vnode use in xfs_symlink and xfs_rename
[pandora-kernel.git] / fs / xfs / linux-2.6 / xfs_iops.c
index 59ba1dc..215158c 100644 (file)
@@ -52,6 +52,7 @@
 #include <linux/xattr.h>
 #include <linux/namei.h>
 #include <linux/security.h>
+#include <linux/falloc.h>
 
 /*
  * Bring the atime in the XFS inode uptodate.
@@ -70,6 +71,22 @@ xfs_synchronize_atime(
        }
 }
 
+/*
+ * If the linux inode exists, mark it dirty.
+ * Used when commiting a dirty inode into a transaction so that
+ * the inode will get written back by the linux code
+ */
+void
+xfs_mark_inode_dirty_sync(
+       xfs_inode_t     *ip)
+{
+       bhv_vnode_t     *vp;
+
+       vp = XFS_ITOV_NULL(ip);
+       if (vp)
+               mark_inode_dirty_sync(vn_to_inode(vp));
+}
+
 /*
  * Change the requested timestamp in the given inode.
  * We don't lock across timestamp updates, and we don't log them but
@@ -117,7 +134,7 @@ xfs_ichgtime(
         */
        SYNCHRONIZE();
        ip->i_update_core = 1;
-       if (!(inode->i_state & I_LOCK))
+       if (!(inode->i_state & I_NEW))
                mark_inode_dirty_sync(inode);
 }
 
@@ -169,7 +186,7 @@ xfs_ichgtime_fast(
         */
        SYNCHRONIZE();
        ip->i_update_core = 1;
-       if (!(inode->i_state & I_LOCK))
+       if (!(inode->i_state & I_NEW))
                mark_inode_dirty_sync(inode);
 }
 
@@ -179,18 +196,15 @@ xfs_ichgtime_fast(
  */
 STATIC void
 xfs_validate_fields(
-       struct inode    *ip,
-       bhv_vattr_t     *vattr)
+       struct inode            *inode)
 {
-       vattr->va_mask = XFS_AT_NLINK|XFS_AT_SIZE|XFS_AT_NBLOCKS;
-       if (!xfs_getattr(XFS_I(ip), vattr, ATTR_LAZY)) {
-               ip->i_nlink = vattr->va_nlink;
-               ip->i_blocks = vattr->va_nblocks;
-
-               /* we're under i_sem so i_size can't change under us */
-               if (i_size_read(ip) != vattr->va_size)
-                       i_size_write(ip, vattr->va_size);
-       }
+       struct xfs_inode        *ip = XFS_I(inode);
+       loff_t size;
+
+       /* we're under i_sem so i_size can't change under us */
+       size = XFS_ISIZE(ip);
+       if (i_size_read(inode) != size)
+               i_size_write(inode, size);
 }
 
 /*
@@ -227,18 +241,6 @@ xfs_init_security(
        return error;
 }
 
-/*
- * Determine whether a process has a valid fs_struct (kernel daemons
- * like knfsd don't have an fs_struct).
- *
- * XXX(hch):  nfsd is broken, better fix it instead.
- */
-STATIC_INLINE int
-xfs_has_fs_struct(struct task_struct *task)
-{
-       return (task->fs != init_task.fs);
-}
-
 STATIC void
 xfs_cleanup_inode(
        struct inode    *dir,
@@ -270,9 +272,8 @@ xfs_vn_mknod(
        int             mode,
        dev_t           rdev)
 {
-       struct inode    *ip;
-       bhv_vattr_t     vattr = { 0 };
-       bhv_vnode_t     *vp = NULL, *dvp = vn_from_inode(dir);
+       struct inode    *inode;
+       struct xfs_inode *ip = NULL;
        xfs_acl_t       *default_acl = NULL;
        attrexists_t    test_default_acl = _ACL_DEFAULT_EXISTS;
        int             error;
@@ -284,66 +285,65 @@ xfs_vn_mknod(
        if (unlikely(!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff))
                return -EINVAL;
 
-       if (unlikely(test_default_acl && test_default_acl(dvp))) {
+       if (test_default_acl && test_default_acl(dir)) {
                if (!_ACL_ALLOC(default_acl)) {
                        return -ENOMEM;
                }
-               if (!_ACL_GET_DEFAULT(dvp, default_acl)) {
+               if (!_ACL_GET_DEFAULT(dir, default_acl)) {
                        _ACL_FREE(default_acl);
                        default_acl = NULL;
                }
        }
 
-       if (IS_POSIXACL(dir) && !default_acl && xfs_has_fs_struct(current))
+       if (IS_POSIXACL(dir) && !default_acl)
                mode &= ~current->fs->umask;
 
-       vattr.va_mask = XFS_AT_TYPE|XFS_AT_MODE;
-       vattr.va_mode = mode;
-
        switch (mode & S_IFMT) {
-       case S_IFCHR: case S_IFBLK: case S_IFIFO: case S_IFSOCK:
-               vattr.va_rdev = sysv_encode_dev(rdev);
-               vattr.va_mask |= XFS_AT_RDEV;
-               /*FALLTHROUGH*/
+       case S_IFCHR:
+       case S_IFBLK:
+       case S_IFIFO:
+       case S_IFSOCK:
+               rdev = sysv_encode_dev(rdev);
        case S_IFREG:
-               error = xfs_create(XFS_I(dir), dentry, &vattr, &vp, NULL);
+               error = xfs_create(XFS_I(dir), dentry, mode, rdev, &ip, NULL);
                break;
        case S_IFDIR:
-               error = xfs_mkdir(XFS_I(dir), dentry, &vattr, &vp, NULL);
+               error = xfs_mkdir(XFS_I(dir), dentry, mode, &ip, NULL);
                break;
        default:
                error = EINVAL;
                break;
        }
 
-       if (unlikely(!error)) {
-               error = xfs_init_security(vp, dir);
-               if (error)
-                       xfs_cleanup_inode(dir, vp, dentry, mode);
-       }
+       if (unlikely(error))
+               goto out_free_acl;
 
-       if (unlikely(default_acl)) {
-               if (!error) {
-                       error = _ACL_INHERIT(vp, &vattr, default_acl);
-                       if (!error)
-                               xfs_iflags_set(XFS_I(vp), XFS_IMODIFIED);
-                       else
-                               xfs_cleanup_inode(dir, vp, dentry, mode);
-               }
+       inode = ip->i_vnode;
+
+       error = xfs_init_security(inode, dir);
+       if (unlikely(error))
+               goto out_cleanup_inode;
+
+       if (default_acl) {
+               error = _ACL_INHERIT(inode, mode, default_acl);
+               if (unlikely(error))
+                       goto out_cleanup_inode;
+               xfs_iflags_set(ip, XFS_IMODIFIED);
                _ACL_FREE(default_acl);
        }
 
-       if (likely(!error)) {
-               ASSERT(vp);
-               ip = vn_to_inode(vp);
-
-               if (S_ISCHR(mode) || S_ISBLK(mode))
-                       ip->i_rdev = rdev;
-               else if (S_ISDIR(mode))
-                       xfs_validate_fields(ip, &vattr);
-               d_instantiate(dentry, ip);
-               xfs_validate_fields(dir, &vattr);
-       }
+
+       if (S_ISDIR(mode))
+               xfs_validate_fields(inode);
+       d_instantiate(dentry, inode);
+       xfs_validate_fields(dir);
+       return -error;
+
+ out_cleanup_inode:
+       xfs_cleanup_inode(dir, inode, dentry, mode);
+ out_free_acl:
+       if (default_acl)
+               _ACL_FREE(default_acl);
        return -error;
 }
 
@@ -395,24 +395,22 @@ xfs_vn_link(
        struct inode    *dir,
        struct dentry   *dentry)
 {
-       struct inode    *ip;    /* inode of guy being linked to */
-       bhv_vnode_t     *vp;    /* vp of name being linked */
-       bhv_vattr_t     vattr;
+       struct inode    *inode; /* inode of guy being linked to */
        int             error;
 
-       ip = old_dentry->d_inode;       /* inode being linked to */
-       vp = vn_from_inode(ip);
+       inode = old_dentry->d_inode;
 
-       VN_HOLD(vp);
-       error = xfs_link(XFS_I(dir), vp, dentry);
+       igrab(inode);
+       error = xfs_link(XFS_I(dir), XFS_I(inode), dentry);
        if (unlikely(error)) {
-               VN_RELE(vp);
-       } else {
-               xfs_iflags_set(XFS_I(dir), XFS_IMODIFIED);
-               xfs_validate_fields(ip, &vattr);
-               d_instantiate(dentry, ip);
+               iput(inode);
+               return -error;
        }
-       return -error;
+
+       xfs_iflags_set(XFS_I(dir), XFS_IMODIFIED);
+       xfs_validate_fields(inode);
+       d_instantiate(dentry, inode);
+       return 0;
 }
 
 STATIC int
@@ -421,15 +419,14 @@ xfs_vn_unlink(
        struct dentry   *dentry)
 {
        struct inode    *inode;
-       bhv_vattr_t     vattr;
        int             error;
 
        inode = dentry->d_inode;
 
        error = xfs_remove(XFS_I(dir), dentry);
        if (likely(!error)) {
-               xfs_validate_fields(dir, &vattr);       /* size needs update */
-               xfs_validate_fields(inode, &vattr);
+               xfs_validate_fields(dir);       /* size needs update */
+               xfs_validate_fields(inode);
        }
        return -error;
 }
@@ -440,30 +437,33 @@ xfs_vn_symlink(
        struct dentry   *dentry,
        const char      *symname)
 {
-       struct inode    *ip;
-       bhv_vattr_t     va = { 0 };
-       bhv_vnode_t     *cvp;   /* used to lookup symlink to put in dentry */
+       struct inode    *inode;
+       struct xfs_inode *cip = NULL;
        int             error;
+       mode_t          mode;
 
-       cvp = NULL;
-
-       va.va_mode = S_IFLNK |
+       mode = S_IFLNK |
                (irix_symlink_mode ? 0777 & ~current->fs->umask : S_IRWXUGO);
-       va.va_mask = XFS_AT_TYPE|XFS_AT_MODE;
-
-       error = xfs_symlink(XFS_I(dir), dentry, &va,
-                       (char *)symname, &cvp, NULL);
-       if (likely(!error && cvp)) {
-               error = xfs_init_security(cvp, dir);
-               if (likely(!error)) {
-                       ip = vn_to_inode(cvp);
-                       d_instantiate(dentry, ip);
-                       xfs_validate_fields(dir, &va);
-                       xfs_validate_fields(ip, &va);
-               } else {
-                       xfs_cleanup_inode(dir, cvp, dentry, 0);
-               }
-       }
+
+       error = xfs_symlink(XFS_I(dir), dentry, (char *)symname, mode,
+                           &cip, NULL);
+       if (unlikely(error))
+               goto out;
+
+       inode = cip->i_vnode;
+
+       error = xfs_init_security(inode, dir);
+       if (unlikely(error))
+               goto out_cleanup_inode;
+
+       d_instantiate(dentry, inode);
+       xfs_validate_fields(dir);
+       xfs_validate_fields(inode);
+       return 0;
+
+ out_cleanup_inode:
+       xfs_cleanup_inode(dir, inode, dentry, 0);
+ out:
        return -error;
 }
 
@@ -473,13 +473,12 @@ xfs_vn_rmdir(
        struct dentry   *dentry)
 {
        struct inode    *inode = dentry->d_inode;
-       bhv_vattr_t     vattr;
        int             error;
 
        error = xfs_rmdir(XFS_I(dir), dentry);
        if (likely(!error)) {
-               xfs_validate_fields(inode, &vattr);
-               xfs_validate_fields(dir, &vattr);
+               xfs_validate_fields(inode);
+               xfs_validate_fields(dir);
        }
        return -error;
 }
@@ -492,19 +491,15 @@ xfs_vn_rename(
        struct dentry   *ndentry)
 {
        struct inode    *new_inode = ndentry->d_inode;
-       bhv_vnode_t     *tvp;   /* target directory */
-       bhv_vattr_t     vattr;
        int             error;
 
-       tvp = vn_from_inode(ndir);
-
-       error = xfs_rename(XFS_I(odir), odentry, tvp, ndentry);
+       error = xfs_rename(XFS_I(odir), odentry, XFS_I(ndir), ndentry);
        if (likely(!error)) {
                if (new_inode)
-                       xfs_validate_fields(new_inode, &vattr);
-               xfs_validate_fields(odir, &vattr);
+                       xfs_validate_fields(new_inode);
+               xfs_validate_fields(odir);
                if (ndir != odir)
-                       xfs_validate_fields(ndir, &vattr);
+                       xfs_validate_fields(ndir);
        }
        return -error;
 }
@@ -553,13 +548,32 @@ xfs_vn_put_link(
 }
 
 #ifdef CONFIG_XFS_POSIX_ACL
+STATIC int
+xfs_check_acl(
+       struct inode            *inode,
+       int                     mask)
+{
+       struct xfs_inode        *ip = XFS_I(inode);
+       int                     error;
+
+       xfs_itrace_entry(ip);
+
+       if (XFS_IFORK_Q(ip)) {
+               error = xfs_acl_iaccess(ip, mask, NULL);
+               if (error != -1)
+                       return -error;
+       }
+
+       return -EAGAIN;
+}
+
 STATIC int
 xfs_vn_permission(
-       struct inode    *inode,
-       int             mode,
-       struct nameidata *nd)
+       struct inode            *inode,
+       int                     mask,
+       struct nameidata        *nd)
 {
-       return -xfs_access(XFS_I(inode), mode << 6, NULL);
+       return generic_permission(inode, mask, xfs_check_acl);
 }
 #else
 #define xfs_vn_permission NULL
@@ -567,33 +581,61 @@ xfs_vn_permission(
 
 STATIC int
 xfs_vn_getattr(
-       struct vfsmount *mnt,
-       struct dentry   *dentry,
-       struct kstat    *stat)
+       struct vfsmount         *mnt,
+       struct dentry           *dentry,
+       struct kstat            *stat)
 {
-       struct inode    *inode = dentry->d_inode;
-       bhv_vattr_t     vattr = { .va_mask = XFS_AT_STAT };
-       int             error;
-
-       error = xfs_getattr(XFS_I(inode), &vattr, ATTR_LAZY);
-       if (likely(!error)) {
-               stat->size = i_size_read(inode);
-               stat->dev = inode->i_sb->s_dev;
-               stat->rdev = (vattr.va_rdev == 0) ? 0 :
-                               MKDEV(sysv_major(vattr.va_rdev) & 0x1ff,
-                                     sysv_minor(vattr.va_rdev));
-               stat->mode = vattr.va_mode;
-               stat->nlink = vattr.va_nlink;
-               stat->uid = vattr.va_uid;
-               stat->gid = vattr.va_gid;
-               stat->ino = vattr.va_nodeid;
-               stat->atime = vattr.va_atime;
-               stat->mtime = vattr.va_mtime;
-               stat->ctime = vattr.va_ctime;
-               stat->blocks = vattr.va_nblocks;
-               stat->blksize = vattr.va_blocksize;
+       struct inode            *inode = dentry->d_inode;
+       struct xfs_inode        *ip = XFS_I(inode);
+       struct xfs_mount        *mp = ip->i_mount;
+
+       xfs_itrace_entry(ip);
+
+       if (XFS_FORCED_SHUTDOWN(mp))
+               return XFS_ERROR(EIO);
+
+       stat->size = XFS_ISIZE(ip);
+       stat->dev = inode->i_sb->s_dev;
+       stat->mode = ip->i_d.di_mode;
+       stat->nlink = ip->i_d.di_nlink;
+       stat->uid = ip->i_d.di_uid;
+       stat->gid = ip->i_d.di_gid;
+       stat->ino = ip->i_ino;
+#if XFS_BIG_INUMS
+       stat->ino += mp->m_inoadd;
+#endif
+       stat->atime = inode->i_atime;
+       stat->mtime.tv_sec = ip->i_d.di_mtime.t_sec;
+       stat->mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
+       stat->ctime.tv_sec = ip->i_d.di_ctime.t_sec;
+       stat->ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
+       stat->blocks =
+               XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
+
+
+       switch (inode->i_mode & S_IFMT) {
+       case S_IFBLK:
+       case S_IFCHR:
+               stat->blksize = BLKDEV_IOSIZE;
+               stat->rdev = MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
+                                  sysv_minor(ip->i_df.if_u2.if_rdev));
+               break;
+       default:
+               if (XFS_IS_REALTIME_INODE(ip)) {
+                       /*
+                        * If the file blocks are being allocated from a
+                        * realtime volume, then return the inode's realtime
+                        * extent size or the realtime volume's extent size.
+                        */
+                       stat->blksize =
+                               xfs_get_extsz_hint(ip) << mp->m_sb.sb_blocklog;
+               } else
+                       stat->blksize = xfs_preferred_iosize(mp);
+               stat->rdev = 0;
+               break;
        }
-       return -error;
+
+       return 0;
 }
 
 STATIC int
@@ -648,7 +690,7 @@ xfs_vn_setattr(
 
        error = xfs_setattr(XFS_I(inode), &vattr, flags, NULL);
        if (likely(!error))
-               __vn_revalidate(vn_from_inode(inode), &vattr);
+               vn_revalidate(vn_from_inode(inode));
        return -error;
 }
 
@@ -762,6 +804,47 @@ xfs_vn_removexattr(
        return namesp->attr_remove(vp, attr, xflags);
 }
 
+STATIC long
+xfs_vn_fallocate(
+       struct inode    *inode,
+       int             mode,
+       loff_t          offset,
+       loff_t          len)
+{
+       long            error;
+       loff_t          new_size = 0;
+       xfs_flock64_t   bf;
+       xfs_inode_t     *ip = XFS_I(inode);
+
+       /* preallocation on directories not yet supported */
+       error = -ENODEV;
+       if (S_ISDIR(inode->i_mode))
+               goto out_error;
+
+       bf.l_whence = 0;
+       bf.l_start = offset;
+       bf.l_len = len;
+
+       xfs_ilock(ip, XFS_IOLOCK_EXCL);
+       error = xfs_change_file_space(ip, XFS_IOC_RESVSP, &bf,
+                                               0, NULL, ATTR_NOLOCK);
+       if (!error && !(mode & FALLOC_FL_KEEP_SIZE) &&
+           offset + len > i_size_read(inode))
+               new_size = offset + len;
+
+       /* Change file size if needed */
+       if (new_size) {
+               bhv_vattr_t     va;
+
+               va.va_mask = XFS_AT_SIZE;
+               va.va_size = new_size;
+               error = xfs_setattr(ip, &va, ATTR_NOLOCK, NULL);
+       }
+
+       xfs_iunlock(ip, XFS_IOLOCK_EXCL);
+out_error:
+       return error;
+}
 
 const struct inode_operations xfs_inode_operations = {
        .permission             = xfs_vn_permission,
@@ -772,6 +855,7 @@ const struct inode_operations xfs_inode_operations = {
        .getxattr               = xfs_vn_getxattr,
        .listxattr              = xfs_vn_listxattr,
        .removexattr            = xfs_vn_removexattr,
+       .fallocate              = xfs_vn_fallocate,
 };
 
 const struct inode_operations xfs_dir_inode_operations = {