]> pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - fs/xfs/linux-2.6/xfs_ioctl.c
Merge branch 'x86-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-omap-h63xx.git] / fs / xfs / linux-2.6 / xfs_ioctl.c
index d3438c72dcaf555b8d18174a00d1b6eab264615f..4bd112313f3351eef0304cabf898eb96fb91f9b0 100644 (file)
 #include "xfs_vnodeops.h"
 #include "xfs_quota.h"
 #include "xfs_inode_item.h"
+#include "xfs_export.h"
 
 #include <linux/capability.h>
 #include <linux/dcache.h>
 #include <linux/mount.h>
 #include <linux/namei.h>
 #include <linux/pagemap.h>
+#include <linux/exportfs.h>
 
 /*
  * xfs_find_handle maps from userspace xfs_fsop_handlereq structure to
  * XFS_IOC_PATH_TO_HANDLE
  *    returns full handle for a path
  */
-STATIC int
+int
 xfs_find_handle(
        unsigned int            cmd,
-       void                    __user *arg)
+       xfs_fsop_handlereq_t    *hreq)
 {
        int                     hsize;
        xfs_handle_t            handle;
-       xfs_fsop_handlereq_t    hreq;
        struct inode            *inode;
 
-       if (copy_from_user(&hreq, arg, sizeof(hreq)))
-               return -XFS_ERROR(EFAULT);
-
        memset((char *)&handle, 0, sizeof(handle));
 
        switch (cmd) {
        case XFS_IOC_PATH_TO_FSHANDLE:
        case XFS_IOC_PATH_TO_HANDLE: {
                struct path path;
-               int error = user_lpath((const char __user *)hreq.path, &path);
+               int error = user_lpath((const char __user *)hreq->path, &path);
                if (error)
                        return error;
 
@@ -101,7 +99,7 @@ xfs_find_handle(
        case XFS_IOC_FD_TO_HANDLE: {
                struct file     *file;
 
-               file = fget(hreq.fd);
+               file = fget(hreq->fd);
                if (!file)
                    return -EBADF;
 
@@ -158,8 +156,8 @@ xfs_find_handle(
        }
 
        /* now copy our handle into the user buffer & write out the size */
-       if (copy_to_user(hreq.ohandle, &handle, hsize) ||
-           copy_to_user(hreq.ohandlen, &hsize, sizeof(__s32))) {
+       if (copy_to_user(hreq->ohandle, &handle, hsize) ||
+           copy_to_user(hreq->ohandlen, &hsize, sizeof(__s32))) {
                iput(inode);
                return -XFS_ERROR(EFAULT);
        }
@@ -168,122 +166,94 @@ xfs_find_handle(
        return 0;
 }
 
-
 /*
- * Convert userspace handle data into inode.
- *
- * We use the fact that all the fsop_handlereq ioctl calls have a data
- * structure argument whose first component is always a xfs_fsop_handlereq_t,
- * so we can pass that sub structure into this handy, shared routine.
- *
- * If no error, caller must always iput the returned inode.
+ * No need to do permission checks on the various pathname components
+ * as the handle operations are privileged.
  */
 STATIC int
-xfs_vget_fsop_handlereq(
-       xfs_mount_t             *mp,
-       struct inode            *parinode,      /* parent inode pointer    */
-       xfs_fsop_handlereq_t    *hreq,
-       struct inode            **inode)
+xfs_handle_acceptable(
+       void                    *context,
+       struct dentry           *dentry)
+{
+       return 1;
+}
+
+/*
+ * Convert userspace handle data into a dentry.
+ */
+struct dentry *
+xfs_handle_to_dentry(
+       struct file             *parfilp,
+       void __user             *uhandle,
+       u32                     hlen)
 {
-       void                    __user *hanp;
-       size_t                  hlen;
-       xfs_fid_t               *xfid;
-       xfs_handle_t            *handlep;
        xfs_handle_t            handle;
-       xfs_inode_t             *ip;
-       xfs_ino_t               ino;
-       __u32                   igen;
-       int                     error;
+       struct xfs_fid64        fid;
 
        /*
         * Only allow handle opens under a directory.
         */
-       if (!S_ISDIR(parinode->i_mode))
-               return XFS_ERROR(ENOTDIR);
-
-       hanp = hreq->ihandle;
-       hlen = hreq->ihandlen;
-       handlep = &handle;
-
-       if (hlen < sizeof(handlep->ha_fsid) || hlen > sizeof(*handlep))
-               return XFS_ERROR(EINVAL);
-       if (copy_from_user(handlep, hanp, hlen))
-               return XFS_ERROR(EFAULT);
-       if (hlen < sizeof(*handlep))
-               memset(((char *)handlep) + hlen, 0, sizeof(*handlep) - hlen);
-       if (hlen > sizeof(handlep->ha_fsid)) {
-               if (handlep->ha_fid.fid_len !=
-                   (hlen - sizeof(handlep->ha_fsid) -
-                           sizeof(handlep->ha_fid.fid_len)) ||
-                   handlep->ha_fid.fid_pad)
-                       return XFS_ERROR(EINVAL);
-       }
-
-       /*
-        * Crack the handle, obtain the inode # & generation #
-        */
-       xfid = (struct xfs_fid *)&handlep->ha_fid;
-       if (xfid->fid_len == sizeof(*xfid) - sizeof(xfid->fid_len)) {
-               ino  = xfid->fid_ino;
-               igen = xfid->fid_gen;
-       } else {
-               return XFS_ERROR(EINVAL);
-       }
-
-       /*
-        * Get the XFS inode, building a Linux inode to go with it.
-        */
-       error = xfs_iget(mp, NULL, ino, 0, XFS_ILOCK_SHARED, &ip, 0);
-       if (error)
-               return error;
-       if (ip == NULL)
-               return XFS_ERROR(EIO);
-       if (ip->i_d.di_gen != igen) {
-               xfs_iput_new(ip, XFS_ILOCK_SHARED);
-               return XFS_ERROR(ENOENT);
-       }
-
-       xfs_iunlock(ip, XFS_ILOCK_SHARED);
+       if (!S_ISDIR(parfilp->f_path.dentry->d_inode->i_mode))
+               return ERR_PTR(-ENOTDIR);
+
+       if (hlen != sizeof(xfs_handle_t))
+               return ERR_PTR(-EINVAL);
+       if (copy_from_user(&handle, uhandle, hlen))
+               return ERR_PTR(-EFAULT);
+       if (handle.ha_fid.fid_len !=
+           sizeof(handle.ha_fid) - sizeof(handle.ha_fid.fid_len))
+               return ERR_PTR(-EINVAL);
+
+       memset(&fid, 0, sizeof(struct fid));
+       fid.ino = handle.ha_fid.fid_ino;
+       fid.gen = handle.ha_fid.fid_gen;
+
+       return exportfs_decode_fh(parfilp->f_path.mnt, (struct fid *)&fid, 3,
+                       FILEID_INO32_GEN | XFS_FILEID_TYPE_64FLAG,
+                       xfs_handle_acceptable, NULL);
+}
 
-       *inode = VFS_I(ip);
-       return 0;
+STATIC struct dentry *
+xfs_handlereq_to_dentry(
+       struct file             *parfilp,
+       xfs_fsop_handlereq_t    *hreq)
+{
+       return xfs_handle_to_dentry(parfilp, hreq->ihandle, hreq->ihandlen);
 }
 
-STATIC int
+int
 xfs_open_by_handle(
-       xfs_mount_t             *mp,
-       void                    __user *arg,
        struct file             *parfilp,
-       struct inode            *parinode)
+       xfs_fsop_handlereq_t    *hreq)
 {
+       const struct cred       *cred = current_cred();
        int                     error;
-       int                     new_fd;
+       int                     fd;
        int                     permflag;
        struct file             *filp;
        struct inode            *inode;
        struct dentry           *dentry;
-       xfs_fsop_handlereq_t    hreq;
 
        if (!capable(CAP_SYS_ADMIN))
                return -XFS_ERROR(EPERM);
-       if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
-               return -XFS_ERROR(EFAULT);
 
-       error = xfs_vget_fsop_handlereq(mp, parinode, &hreq, &inode);
-       if (error)
-               return -error;
+       dentry = xfs_handlereq_to_dentry(parfilp, hreq);
+       if (IS_ERR(dentry))
+               return PTR_ERR(dentry);
+       inode = dentry->d_inode;
 
        /* Restrict xfs_open_by_handle to directories & regular files. */
        if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode))) {
-               iput(inode);
-               return -XFS_ERROR(EINVAL);
+               error = -XFS_ERROR(EPERM);
+               goto out_dput;
        }
 
 #if BITS_PER_LONG != 32
-       hreq.oflags |= O_LARGEFILE;
+       hreq->oflags |= O_LARGEFILE;
 #endif
+
        /* Put open permission in namei format. */
-       permflag = hreq.oflags;
+       permflag = hreq->oflags;
        if ((permflag+1) & O_ACCMODE)
                permflag++;
        if (permflag & O_TRUNC)
@@ -291,49 +261,45 @@ xfs_open_by_handle(
 
        if ((!(permflag & O_APPEND) || (permflag & O_TRUNC)) &&
            (permflag & FMODE_WRITE) && IS_APPEND(inode)) {
-               iput(inode);
-               return -XFS_ERROR(EPERM);
+               error = -XFS_ERROR(EPERM);
+               goto out_dput;
        }
 
        if ((permflag & FMODE_WRITE) && IS_IMMUTABLE(inode)) {
-               iput(inode);
-               return -XFS_ERROR(EACCES);
+               error = -XFS_ERROR(EACCES);
+               goto out_dput;
        }
 
        /* Can't write directories. */
-       if ( S_ISDIR(inode->i_mode) && (permflag & FMODE_WRITE)) {
-               iput(inode);
-               return -XFS_ERROR(EISDIR);
+       if (S_ISDIR(inode->i_mode) && (permflag & FMODE_WRITE)) {
+               error = -XFS_ERROR(EISDIR);
+               goto out_dput;
        }
 
-       if ((new_fd = get_unused_fd()) < 0) {
-               iput(inode);
-               return new_fd;
-       }
-
-       dentry = d_obtain_alias(inode);
-       if (IS_ERR(dentry)) {
-               put_unused_fd(new_fd);
-               return PTR_ERR(dentry);
+       fd = get_unused_fd();
+       if (fd < 0) {
+               error = fd;
+               goto out_dput;
        }
 
-       /* Ensure umount returns EBUSY on umounts while this file is open. */
-       mntget(parfilp->f_path.mnt);
-
-       /* Create file pointer. */
-       filp = dentry_open(dentry, parfilp->f_path.mnt, hreq.oflags);
+       filp = dentry_open(dentry, mntget(parfilp->f_path.mnt),
+                          hreq->oflags, cred);
        if (IS_ERR(filp)) {
-               put_unused_fd(new_fd);
-               return -XFS_ERROR(-PTR_ERR(filp));
+               put_unused_fd(fd);
+               return PTR_ERR(filp);
        }
+
        if (inode->i_mode & S_IFREG) {
-               /* invisible operation should not change atime */
                filp->f_flags |= O_NOATIME;
-               filp->f_op = &xfs_invis_file_operations;
+               filp->f_mode |= FMODE_NOCMTIME;
        }
 
-       fd_install(new_fd, filp);
-       return new_fd;
+       fd_install(fd, filp);
+       return fd;
+
+ out_dput:
+       dput(dentry);
+       return error;
 }
 
 /*
@@ -362,77 +328,74 @@ do_readlink(
 }
 
 
-STATIC int
+int
 xfs_readlink_by_handle(
-       xfs_mount_t             *mp,
-       void                    __user *arg,
-       struct inode            *parinode)
+       struct file             *parfilp,
+       xfs_fsop_handlereq_t    *hreq)
 {
-       struct inode            *inode;
-       xfs_fsop_handlereq_t    hreq;
+       struct dentry           *dentry;
        __u32                   olen;
        void                    *link;
        int                     error;
 
        if (!capable(CAP_SYS_ADMIN))
                return -XFS_ERROR(EPERM);
-       if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
-               return -XFS_ERROR(EFAULT);
 
-       error = xfs_vget_fsop_handlereq(mp, parinode, &hreq, &inode);
-       if (error)
-               return -error;
+       dentry = xfs_handlereq_to_dentry(parfilp, hreq);
+       if (IS_ERR(dentry))
+               return PTR_ERR(dentry);
 
        /* Restrict this handle operation to symlinks only. */
-       if (!S_ISLNK(inode->i_mode)) {
+       if (!S_ISLNK(dentry->d_inode->i_mode)) {
                error = -XFS_ERROR(EINVAL);
-               goto out_iput;
+               goto out_dput;
        }
 
-       if (copy_from_user(&olen, hreq.ohandlen, sizeof(__u32))) {
+       if (copy_from_user(&olen, hreq->ohandlen, sizeof(__u32))) {
                error = -XFS_ERROR(EFAULT);
-               goto out_iput;
+               goto out_dput;
        }
 
        link = kmalloc(MAXPATHLEN+1, GFP_KERNEL);
-       if (!link)
-               goto out_iput;
+       if (!link) {
+               error = -XFS_ERROR(ENOMEM);
+               goto out_dput;
+       }
 
-       error = -xfs_readlink(XFS_I(inode), link);
+       error = -xfs_readlink(XFS_I(dentry->d_inode), link);
        if (error)
                goto out_kfree;
-       error = do_readlink(hreq.ohandle, olen, link);
+       error = do_readlink(hreq->ohandle, olen, link);
        if (error)
                goto out_kfree;
 
  out_kfree:
        kfree(link);
- out_iput:
-       iput(inode);
+ out_dput:
+       dput(dentry);
        return error;
 }
 
 STATIC int
 xfs_fssetdm_by_handle(
-       xfs_mount_t             *mp,
-       void                    __user *arg,
-       struct inode            *parinode)
+       struct file             *parfilp,
+       void                    __user *arg)
 {
        int                     error;
        struct fsdmidata        fsd;
        xfs_fsop_setdm_handlereq_t dmhreq;
-       struct inode            *inode;
+       struct dentry           *dentry;
 
        if (!capable(CAP_MKNOD))
                return -XFS_ERROR(EPERM);
        if (copy_from_user(&dmhreq, arg, sizeof(xfs_fsop_setdm_handlereq_t)))
                return -XFS_ERROR(EFAULT);
 
-       error = xfs_vget_fsop_handlereq(mp, parinode, &dmhreq.hreq, &inode);
-       if (error)
-               return -error;
+       dentry = xfs_handlereq_to_dentry(parfilp, &dmhreq.hreq);
+       if (IS_ERR(dentry))
+               return PTR_ERR(dentry);
 
-       if (IS_IMMUTABLE(inode) || IS_APPEND(inode)) {
+       if (IS_IMMUTABLE(dentry->d_inode) || IS_APPEND(dentry->d_inode)) {
                error = -XFS_ERROR(EPERM);
                goto out;
        }
@@ -442,24 +405,23 @@ xfs_fssetdm_by_handle(
                goto out;
        }
 
-       error = -xfs_set_dmattrs(XFS_I(inode), fsd.fsd_dmevmask,
+       error = -xfs_set_dmattrs(XFS_I(dentry->d_inode), fsd.fsd_dmevmask,
                                 fsd.fsd_dmstate);
 
  out:
-       iput(inode);
+       dput(dentry);
        return error;
 }
 
 STATIC int
 xfs_attrlist_by_handle(
-       xfs_mount_t             *mp,
-       void                    __user *arg,
-       struct inode            *parinode)
+       struct file             *parfilp,
+       void                    __user *arg)
 {
-       int                     error;
+       int                     error = -ENOMEM;
        attrlist_cursor_kern_t  *cursor;
        xfs_fsop_attrlist_handlereq_t al_hreq;
-       struct inode            *inode;
+       struct dentry           *dentry;
        char                    *kbuf;
 
        if (!capable(CAP_SYS_ADMIN))
@@ -475,16 +437,16 @@ xfs_attrlist_by_handle(
        if (al_hreq.flags & ~(ATTR_ROOT | ATTR_SECURE))
                return -XFS_ERROR(EINVAL);
 
-       error = xfs_vget_fsop_handlereq(mp, parinode, &al_hreq.hreq, &inode);
-       if (error)
-               goto out;
+       dentry = xfs_handlereq_to_dentry(parfilp, &al_hreq.hreq);
+       if (IS_ERR(dentry))
+               return PTR_ERR(dentry);
 
        kbuf = kmalloc(al_hreq.buflen, GFP_KERNEL);
        if (!kbuf)
-               goto out_vn_rele;
+               goto out_dput;
 
        cursor = (attrlist_cursor_kern_t *)&al_hreq.pos;
-       error = xfs_attr_list(XFS_I(inode), kbuf, al_hreq.buflen,
+       error = -xfs_attr_list(XFS_I(dentry->d_inode), kbuf, al_hreq.buflen,
                                        al_hreq.flags, cursor);
        if (error)
                goto out_kfree;
@@ -494,13 +456,12 @@ xfs_attrlist_by_handle(
 
  out_kfree:
        kfree(kbuf);
- out_vn_rele:
-       iput(inode);
- out:
-       return -error;
+ out_dput:
+       dput(dentry);
+       return error;
 }
 
-STATIC int
+int
 xfs_attrmulti_attr_get(
        struct inode            *inode,
        char                    *name,
@@ -529,7 +490,7 @@ xfs_attrmulti_attr_get(
        return error;
 }
 
-STATIC int
+int
 xfs_attrmulti_attr_set(
        struct inode            *inode,
        char                    *name,
@@ -559,7 +520,7 @@ xfs_attrmulti_attr_set(
        return error;
 }
 
-STATIC int
+int
 xfs_attrmulti_attr_remove(
        struct inode            *inode,
        char                    *name,
@@ -572,15 +533,13 @@ xfs_attrmulti_attr_remove(
 
 STATIC int
 xfs_attrmulti_by_handle(
-       xfs_mount_t             *mp,
-       void                    __user *arg,
        struct file             *parfilp,
-       struct inode            *parinode)
+       void                    __user *arg)
 {
        int                     error;
        xfs_attr_multiop_t      *ops;
        xfs_fsop_attrmulti_handlereq_t am_hreq;
-       struct inode            *inode;
+       struct dentry           *dentry;
        unsigned int            i, size;
        char                    *attr_name;
 
@@ -589,19 +548,19 @@ xfs_attrmulti_by_handle(
        if (copy_from_user(&am_hreq, arg, sizeof(xfs_fsop_attrmulti_handlereq_t)))
                return -XFS_ERROR(EFAULT);
 
-       error = xfs_vget_fsop_handlereq(mp, parinode, &am_hreq.hreq, &inode);
-       if (error)
-               goto out;
+       dentry = xfs_handlereq_to_dentry(parfilp, &am_hreq.hreq);
+       if (IS_ERR(dentry))
+               return PTR_ERR(dentry);
 
        error = E2BIG;
        size = am_hreq.opcount * sizeof(xfs_attr_multiop_t);
        if (!size || size > 16 * PAGE_SIZE)
-               goto out_vn_rele;
+               goto out_dput;
 
        error = ENOMEM;
        ops = kmalloc(size, GFP_KERNEL);
        if (!ops)
-               goto out_vn_rele;
+               goto out_dput;
 
        error = EFAULT;
        if (copy_from_user(ops, am_hreq.ops, size))
@@ -623,25 +582,28 @@ xfs_attrmulti_by_handle(
 
                switch (ops[i].am_opcode) {
                case ATTR_OP_GET:
-                       ops[i].am_error = xfs_attrmulti_attr_get(inode,
-                                       attr_name, ops[i].am_attrvalue,
-                                       &ops[i].am_length, ops[i].am_flags);
+                       ops[i].am_error = xfs_attrmulti_attr_get(
+                                       dentry->d_inode, attr_name,
+                                       ops[i].am_attrvalue, &ops[i].am_length,
+                                       ops[i].am_flags);
                        break;
                case ATTR_OP_SET:
                        ops[i].am_error = mnt_want_write(parfilp->f_path.mnt);
                        if (ops[i].am_error)
                                break;
-                       ops[i].am_error = xfs_attrmulti_attr_set(inode,
-                                       attr_name, ops[i].am_attrvalue,
-                                       ops[i].am_length, ops[i].am_flags);
+                       ops[i].am_error = xfs_attrmulti_attr_set(
+                                       dentry->d_inode, attr_name,
+                                       ops[i].am_attrvalue, ops[i].am_length,
+                                       ops[i].am_flags);
                        mnt_drop_write(parfilp->f_path.mnt);
                        break;
                case ATTR_OP_REMOVE:
                        ops[i].am_error = mnt_want_write(parfilp->f_path.mnt);
                        if (ops[i].am_error)
                                break;
-                       ops[i].am_error = xfs_attrmulti_attr_remove(inode,
-                                       attr_name, ops[i].am_flags);
+                       ops[i].am_error = xfs_attrmulti_attr_remove(
+                                       dentry->d_inode, attr_name,
+                                       ops[i].am_flags);
                        mnt_drop_write(parfilp->f_path.mnt);
                        break;
                default:
@@ -655,25 +617,31 @@ xfs_attrmulti_by_handle(
        kfree(attr_name);
  out_kfree_ops:
        kfree(ops);
- out_vn_rele:
-       iput(inode);
- out:
+ out_dput:
+       dput(dentry);
        return -error;
 }
 
-STATIC int
+int
 xfs_ioc_space(
        struct xfs_inode        *ip,
        struct inode            *inode,
        struct file             *filp,
        int                     ioflags,
        unsigned int            cmd,
-       void                    __user *arg)
+       xfs_flock64_t           *bf)
 {
-       xfs_flock64_t           bf;
        int                     attr_flags = 0;
        int                     error;
 
+       /*
+        * Only allow the sys admin to reserve space unless
+        * unwritten extents are enabled.
+        */
+       if (!xfs_sb_version_hasextflgbit(&ip->i_mount->m_sb) &&
+           !capable(CAP_SYS_ADMIN))
+               return -XFS_ERROR(EPERM);
+
        if (inode->i_flags & (S_IMMUTABLE|S_APPEND))
                return -XFS_ERROR(EPERM);
 
@@ -683,16 +651,12 @@ xfs_ioc_space(
        if (!S_ISREG(inode->i_mode))
                return -XFS_ERROR(EINVAL);
 
-       if (copy_from_user(&bf, arg, sizeof(bf)))
-               return -XFS_ERROR(EFAULT);
-
        if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
                attr_flags |= XFS_ATTR_NONBLOCK;
        if (ioflags & IO_INVIS)
                attr_flags |= XFS_ATTR_DMI;
 
-       error = xfs_change_file_space(ip, cmd, &bf, filp->f_pos,
-                                             NULL, attr_flags);
+       error = xfs_change_file_space(ip, cmd, bf, filp->f_pos, attr_flags);
        return -error;
 }
 
@@ -1007,7 +971,7 @@ xfs_ioctl_setattr(
         * to the file owner ID, except in cases where the
         * CAP_FSETID capability is applicable.
         */
-       if (current->fsuid != ip->i_d.di_uid && !capable(CAP_FOWNER)) {
+       if (current_fsuid() != ip->i_d.di_uid && !capable(CAP_FOWNER)) {
                code = XFS_ERROR(EPERM);
                goto error_return;
        }
@@ -1104,10 +1068,6 @@ xfs_ioctl_setattr(
 
        /*
         * Change file ownership.  Must be the owner or privileged.
-        * If the system was configured with the "restricted_chown"
-        * option, the owner is not permitted to give away the file,
-        * and can change the group id only to a group of which he
-        * or she is a member.
         */
        if (mask & FSX_PROJID) {
                /*
@@ -1136,7 +1096,7 @@ xfs_ioctl_setattr(
                         * the superblock version number since projids didn't
                         * exist before DINODE_VERSION_2 and SB_VERSION_NLINK.
                         */
-                       if (ip->i_d.di_version == XFS_DINODE_VERSION_1)
+                       if (ip->i_d.di_version == 1)
                                xfs_bump_ino_vers2(tp, ip);
                }
 
@@ -1254,6 +1214,19 @@ xfs_ioc_setxflags(
        return -xfs_ioctl_setattr(ip, &fa, mask);
 }
 
+STATIC int
+xfs_getbmap_format(void **ap, struct getbmapx *bmv, int *full)
+{
+       struct getbmap __user   *base = *ap;
+
+       /* copy only getbmap portion (not getbmapx) */
+       if (copy_to_user(base, bmv, sizeof(struct getbmap)))
+               return XFS_ERROR(EFAULT);
+
+       *ap += sizeof(struct getbmap);
+       return 0;
+}
+
 STATIC int
 xfs_ioc_getbmap(
        struct xfs_inode        *ip,
@@ -1261,37 +1234,48 @@ xfs_ioc_getbmap(
        unsigned int            cmd,
        void                    __user *arg)
 {
-       struct getbmap          bm;
-       int                     iflags;
+       struct getbmapx         bmx;
        int                     error;
 
-       if (copy_from_user(&bm, arg, sizeof(bm)))
+       if (copy_from_user(&bmx, arg, sizeof(struct getbmapx)))
                return -XFS_ERROR(EFAULT);
 
-       if (bm.bmv_count < 2)
+       if (bmx.bmv_count < 2)
                return -XFS_ERROR(EINVAL);
 
-       iflags = (cmd == XFS_IOC_GETBMAPA ? BMV_IF_ATTRFORK : 0);
+       bmx.bmv_iflags = (cmd == XFS_IOC_GETBMAPA ? BMV_IF_ATTRFORK : 0);
        if (ioflags & IO_INVIS)
-               iflags |= BMV_IF_NO_DMAPI_READ;
+               bmx.bmv_iflags |= BMV_IF_NO_DMAPI_READ;
 
-       error = xfs_getbmap(ip, &bm, (struct getbmap __user *)arg+1, iflags);
+       error = xfs_getbmap(ip, &bmx, xfs_getbmap_format,
+                           (struct getbmap *)arg+1);
        if (error)
                return -error;
 
-       if (copy_to_user(arg, &bm, sizeof(bm)))
+       /* copy back header - only size of getbmap */
+       if (copy_to_user(arg, &bmx, sizeof(struct getbmap)))
                return -XFS_ERROR(EFAULT);
        return 0;
 }
 
+STATIC int
+xfs_getbmapx_format(void **ap, struct getbmapx *bmv, int *full)
+{
+       struct getbmapx __user  *base = *ap;
+
+       if (copy_to_user(base, bmv, sizeof(struct getbmapx)))
+               return XFS_ERROR(EFAULT);
+
+       *ap += sizeof(struct getbmapx);
+       return 0;
+}
+
 STATIC int
 xfs_ioc_getbmapx(
        struct xfs_inode        *ip,
        void                    __user *arg)
 {
        struct getbmapx         bmx;
-       struct getbmap          bm;
-       int                     iflags;
        int                     error;
 
        if (copy_from_user(&bmx, arg, sizeof(bmx)))
@@ -1300,46 +1284,46 @@ xfs_ioc_getbmapx(
        if (bmx.bmv_count < 2)
                return -XFS_ERROR(EINVAL);
 
-       /*
-        * Map input getbmapx structure to a getbmap
-        * structure for xfs_getbmap.
-        */
-       GETBMAP_CONVERT(bmx, bm);
-
-       iflags = bmx.bmv_iflags;
-
-       if (iflags & (~BMV_IF_VALID))
+       if (bmx.bmv_iflags & (~BMV_IF_VALID))
                return -XFS_ERROR(EINVAL);
 
-       iflags |= BMV_IF_EXTENDED;
-
-       error = xfs_getbmap(ip, &bm, (struct getbmapx __user *)arg+1, iflags);
+       error = xfs_getbmap(ip, &bmx, xfs_getbmapx_format,
+                           (struct getbmapx *)arg+1);
        if (error)
                return -error;
 
-       GETBMAP_CONVERT(bm, bmx);
-
-       if (copy_to_user(arg, &bmx, sizeof(bmx)))
+       /* copy back header */
+       if (copy_to_user(arg, &bmx, sizeof(struct getbmapx)))
                return -XFS_ERROR(EFAULT);
 
        return 0;
 }
 
-int
-xfs_ioctl(
-       xfs_inode_t             *ip,
+/*
+ * Note: some of the ioctl's return positive numbers as a
+ * byte count indicating success, such as readlink_by_handle.
+ * So we don't "sign flip" like most other routines.  This means
+ * true errors need to be returned as a negative value.
+ */
+long
+xfs_file_ioctl(
        struct file             *filp,
-       int                     ioflags,
        unsigned int            cmd,
-       void                    __user *arg)
+       unsigned long           p)
 {
        struct inode            *inode = filp->f_path.dentry->d_inode;
-       xfs_mount_t             *mp = ip->i_mount;
+       struct xfs_inode        *ip = XFS_I(inode);
+       struct xfs_mount        *mp = ip->i_mount;
+       void                    __user *arg = (void __user *)p;
+       int                     ioflags = 0;
        int                     error;
 
-       xfs_itrace_entry(XFS_I(inode));
-       switch (cmd) {
+       if (filp->f_mode & FMODE_NOCMTIME)
+               ioflags |= IO_INVIS;
+
+       xfs_itrace_entry(ip);
 
+       switch (cmd) {
        case XFS_IOC_ALLOCSP:
        case XFS_IOC_FREESP:
        case XFS_IOC_RESVSP:
@@ -1347,17 +1331,13 @@ xfs_ioctl(
        case XFS_IOC_ALLOCSP64:
        case XFS_IOC_FREESP64:
        case XFS_IOC_RESVSP64:
-       case XFS_IOC_UNRESVSP64:
-               /*
-                * Only allow the sys admin to reserve space unless
-                * unwritten extents are enabled.
-                */
-               if (!xfs_sb_version_hasextflgbit(&mp->m_sb) &&
-                   !capable(CAP_SYS_ADMIN))
-                       return -EPERM;
-
-               return xfs_ioc_space(ip, inode, filp, ioflags, cmd, arg);
+       case XFS_IOC_UNRESVSP64: {
+               xfs_flock64_t           bf;
 
+               if (copy_from_user(&bf, arg, sizeof(bf)))
+                       return -XFS_ERROR(EFAULT);
+               return xfs_ioc_space(ip, inode, filp, ioflags, cmd, &bf);
+       }
        case XFS_IOC_DIOINFO: {
                struct dioattr  da;
                xfs_buftarg_t   *target =
@@ -1417,26 +1397,42 @@ xfs_ioctl(
 
        case XFS_IOC_FD_TO_HANDLE:
        case XFS_IOC_PATH_TO_HANDLE:
-       case XFS_IOC_PATH_TO_FSHANDLE:
-               return xfs_find_handle(cmd, arg);
+       case XFS_IOC_PATH_TO_FSHANDLE: {
+               xfs_fsop_handlereq_t    hreq;
 
-       case XFS_IOC_OPEN_BY_HANDLE:
-               return xfs_open_by_handle(mp, arg, filp, inode);
+               if (copy_from_user(&hreq, arg, sizeof(hreq)))
+                       return -XFS_ERROR(EFAULT);
+               return xfs_find_handle(cmd, &hreq);
+       }
+       case XFS_IOC_OPEN_BY_HANDLE: {
+               xfs_fsop_handlereq_t    hreq;
 
+               if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
+                       return -XFS_ERROR(EFAULT);
+               return xfs_open_by_handle(filp, &hreq);
+       }
        case XFS_IOC_FSSETDM_BY_HANDLE:
-               return xfs_fssetdm_by_handle(mp, arg, inode);
+               return xfs_fssetdm_by_handle(filp, arg);
 
-       case XFS_IOC_READLINK_BY_HANDLE:
-               return xfs_readlink_by_handle(mp, arg, inode);
+       case XFS_IOC_READLINK_BY_HANDLE: {
+               xfs_fsop_handlereq_t    hreq;
 
+               if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
+                       return -XFS_ERROR(EFAULT);
+               return xfs_readlink_by_handle(filp, &hreq);
+       }
        case XFS_IOC_ATTRLIST_BY_HANDLE:
-               return xfs_attrlist_by_handle(mp, arg, inode);
+               return xfs_attrlist_by_handle(filp, arg);
 
        case XFS_IOC_ATTRMULTI_BY_HANDLE:
-               return xfs_attrmulti_by_handle(mp, arg, filp, inode);
+               return xfs_attrmulti_by_handle(filp, arg);
 
        case XFS_IOC_SWAPEXT: {
-               error = xfs_swapext((struct xfs_swapext __user *)arg);
+               struct xfs_swapext      sxp;
+
+               if (copy_from_user(&sxp, arg, sizeof(xfs_swapext_t)))
+                       return -XFS_ERROR(EFAULT);
+               error = xfs_swapext(&sxp);
                return -error;
        }
 
@@ -1492,9 +1488,6 @@ xfs_ioctl(
        case XFS_IOC_FSGROWFSDATA: {
                xfs_growfs_data_t in;
 
-               if (!capable(CAP_SYS_ADMIN))
-                       return -EPERM;
-
                if (copy_from_user(&in, arg, sizeof(in)))
                        return -XFS_ERROR(EFAULT);
 
@@ -1505,9 +1498,6 @@ xfs_ioctl(
        case XFS_IOC_FSGROWFSLOG: {
                xfs_growfs_log_t in;
 
-               if (!capable(CAP_SYS_ADMIN))
-                       return -EPERM;
-
                if (copy_from_user(&in, arg, sizeof(in)))
                        return -XFS_ERROR(EFAULT);
 
@@ -1518,9 +1508,6 @@ xfs_ioctl(
        case XFS_IOC_FSGROWFSRT: {
                xfs_growfs_rt_t in;
 
-               if (!capable(CAP_SYS_ADMIN))
-                       return -EPERM;
-
                if (copy_from_user(&in, arg, sizeof(in)))
                        return -XFS_ERROR(EFAULT);
 
@@ -1528,21 +1515,6 @@ xfs_ioctl(
                return -error;
        }
 
-       case XFS_IOC_FREEZE:
-               if (!capable(CAP_SYS_ADMIN))
-                       return -EPERM;
-
-               if (inode->i_sb->s_frozen == SB_UNFROZEN)
-                       freeze_bdev(inode->i_sb->s_bdev);
-               return 0;
-
-       case XFS_IOC_THAW:
-               if (!capable(CAP_SYS_ADMIN))
-                       return -EPERM;
-               if (inode->i_sb->s_frozen != SB_UNFROZEN)
-                       thaw_bdev(inode->i_sb->s_bdev, inode->i_sb);
-               return 0;
-
        case XFS_IOC_GOINGDOWN: {
                __uint32_t in;