Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 2 | * Copyright (c) 2000-2005 Silicon Graphics, Inc. |
| 3 | * All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License as |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * published by the Free Software Foundation. |
| 8 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 9 | * This program is distributed in the hope that it would be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write the Free Software Foundation, |
| 16 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include "xfs.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include "xfs_fs.h" |
Dave Chinner | 6ca1c90 | 2013-08-12 20:49:26 +1000 | [diff] [blame] | 20 | #include "xfs_format.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include "xfs_log.h" |
| 22 | #include "xfs_trans.h" |
| 23 | #include "xfs_sb.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 24 | #include "xfs_ag.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include "xfs_alloc.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include "xfs_mount.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include "xfs_bmap_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include "xfs_dinode.h" |
| 29 | #include "xfs_inode.h" |
Hannes Eder | 7bf446f | 2009-03-05 15:20:25 +0100 | [diff] [blame] | 30 | #include "xfs_ioctl.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include "xfs_rtalloc.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include "xfs_itable.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 33 | #include "xfs_error.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include "xfs_attr.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 35 | #include "xfs_bmap.h" |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame^] | 36 | #include "xfs_bmap_util.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include "xfs_buf_item.h" |
| 38 | #include "xfs_utils.h" |
| 39 | #include "xfs_dfrag.h" |
| 40 | #include "xfs_fsops.h" |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 41 | #include "xfs_vnodeops.h" |
Christoph Hellwig | a46db60 | 2011-01-07 13:02:04 +0000 | [diff] [blame] | 42 | #include "xfs_discard.h" |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 43 | #include "xfs_quota.h" |
| 44 | #include "xfs_inode_item.h" |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 45 | #include "xfs_export.h" |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 46 | #include "xfs_trace.h" |
Brian Foster | 8ca149d | 2012-11-07 12:21:12 -0500 | [diff] [blame] | 47 | #include "xfs_icache.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
Randy Dunlap | 16f7e0f | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 49 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | #include <linux/dcache.h> |
| 51 | #include <linux/mount.h> |
| 52 | #include <linux/namei.h> |
| 53 | #include <linux/pagemap.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 54 | #include <linux/slab.h> |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 55 | #include <linux/exportfs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
| 57 | /* |
| 58 | * xfs_find_handle maps from userspace xfs_fsop_handlereq structure to |
| 59 | * a file or fs handle. |
| 60 | * |
| 61 | * XFS_IOC_PATH_TO_FSHANDLE |
| 62 | * returns fs handle for a mount point or path within that mount point |
| 63 | * XFS_IOC_FD_TO_HANDLE |
| 64 | * returns full handle for a FD opened in user space |
| 65 | * XFS_IOC_PATH_TO_HANDLE |
| 66 | * returns full handle for a path |
| 67 | */ |
sandeen@sandeen.net | d5547f9 | 2008-11-25 21:20:08 -0600 | [diff] [blame] | 68 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | xfs_find_handle( |
| 70 | unsigned int cmd, |
sandeen@sandeen.net | 743bb465 | 2008-11-25 21:20:06 -0600 | [diff] [blame] | 71 | xfs_fsop_handlereq_t *hreq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | { |
| 73 | int hsize; |
| 74 | xfs_handle_t handle; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | struct inode *inode; |
Dave Chinner | 531c3bd | 2012-10-25 17:22:30 +1100 | [diff] [blame] | 76 | struct fd f = {0}; |
Christoph Hellwig | 4346cdd | 2009-02-08 21:51:14 +0100 | [diff] [blame] | 77 | struct path path; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 78 | int error; |
Christoph Hellwig | 4346cdd | 2009-02-08 21:51:14 +0100 | [diff] [blame] | 79 | struct xfs_inode *ip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | |
Christoph Hellwig | 4346cdd | 2009-02-08 21:51:14 +0100 | [diff] [blame] | 81 | if (cmd == XFS_IOC_FD_TO_HANDLE) { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 82 | f = fdget(hreq->fd); |
| 83 | if (!f.file) |
Christoph Hellwig | 4346cdd | 2009-02-08 21:51:14 +0100 | [diff] [blame] | 84 | return -EBADF; |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 85 | inode = file_inode(f.file); |
Christoph Hellwig | 4346cdd | 2009-02-08 21:51:14 +0100 | [diff] [blame] | 86 | } else { |
| 87 | error = user_lpath((const char __user *)hreq->path, &path); |
| 88 | if (error) |
| 89 | return error; |
| 90 | inode = path.dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | } |
Christoph Hellwig | 4346cdd | 2009-02-08 21:51:14 +0100 | [diff] [blame] | 92 | ip = XFS_I(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | |
Christoph Hellwig | 4346cdd | 2009-02-08 21:51:14 +0100 | [diff] [blame] | 94 | /* |
| 95 | * We can only generate handles for inodes residing on a XFS filesystem, |
| 96 | * and only for regular files, directories or symbolic links. |
| 97 | */ |
| 98 | error = -EINVAL; |
| 99 | if (inode->i_sb->s_magic != XFS_SB_MAGIC) |
| 100 | goto out_put; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | |
Christoph Hellwig | 4346cdd | 2009-02-08 21:51:14 +0100 | [diff] [blame] | 102 | error = -EBADF; |
| 103 | if (!S_ISREG(inode->i_mode) && |
| 104 | !S_ISDIR(inode->i_mode) && |
| 105 | !S_ISLNK(inode->i_mode)) |
| 106 | goto out_put; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | |
Christoph Hellwig | 4346cdd | 2009-02-08 21:51:14 +0100 | [diff] [blame] | 109 | memcpy(&handle.ha_fsid, ip->i_mount->m_fixedfsid, sizeof(xfs_fsid_t)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | |
Christoph Hellwig | 4346cdd | 2009-02-08 21:51:14 +0100 | [diff] [blame] | 111 | if (cmd == XFS_IOC_PATH_TO_FSHANDLE) { |
| 112 | /* |
| 113 | * This handle only contains an fsid, zero the rest. |
| 114 | */ |
| 115 | memset(&handle.ha_fid, 0, sizeof(handle.ha_fid)); |
| 116 | hsize = sizeof(xfs_fsid_t); |
| 117 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | int lock_mode; |
| 119 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | lock_mode = xfs_ilock_map_shared(ip); |
Christoph Hellwig | c614391 | 2007-09-14 15:22:37 +1000 | [diff] [blame] | 121 | handle.ha_fid.fid_len = sizeof(xfs_fid_t) - |
| 122 | sizeof(handle.ha_fid.fid_len); |
| 123 | handle.ha_fid.fid_pad = 0; |
| 124 | handle.ha_fid.fid_gen = ip->i_d.di_gen; |
| 125 | handle.ha_fid.fid_ino = ip->i_ino; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | xfs_iunlock_map_shared(ip, lock_mode); |
| 127 | |
| 128 | hsize = XFS_HSIZE(handle); |
| 129 | } |
| 130 | |
Christoph Hellwig | 4346cdd | 2009-02-08 21:51:14 +0100 | [diff] [blame] | 131 | error = -EFAULT; |
sandeen@sandeen.net | 743bb465 | 2008-11-25 21:20:06 -0600 | [diff] [blame] | 132 | if (copy_to_user(hreq->ohandle, &handle, hsize) || |
Christoph Hellwig | 4346cdd | 2009-02-08 21:51:14 +0100 | [diff] [blame] | 133 | copy_to_user(hreq->ohandlen, &hsize, sizeof(__s32))) |
| 134 | goto out_put; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | |
Christoph Hellwig | 4346cdd | 2009-02-08 21:51:14 +0100 | [diff] [blame] | 136 | error = 0; |
| 137 | |
| 138 | out_put: |
| 139 | if (cmd == XFS_IOC_FD_TO_HANDLE) |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 140 | fdput(f); |
Christoph Hellwig | 4346cdd | 2009-02-08 21:51:14 +0100 | [diff] [blame] | 141 | else |
| 142 | path_put(&path); |
| 143 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | /* |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 147 | * No need to do permission checks on the various pathname components |
| 148 | * as the handle operations are privileged. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | */ |
| 150 | STATIC int |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 151 | xfs_handle_acceptable( |
| 152 | void *context, |
| 153 | struct dentry *dentry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | { |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 155 | return 1; |
| 156 | } |
| 157 | |
| 158 | /* |
| 159 | * Convert userspace handle data into a dentry. |
| 160 | */ |
| 161 | struct dentry * |
| 162 | xfs_handle_to_dentry( |
| 163 | struct file *parfilp, |
| 164 | void __user *uhandle, |
| 165 | u32 hlen) |
| 166 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | xfs_handle_t handle; |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 168 | struct xfs_fid64 fid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | |
| 170 | /* |
| 171 | * Only allow handle opens under a directory. |
| 172 | */ |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 173 | if (!S_ISDIR(file_inode(parfilp)->i_mode)) |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 174 | return ERR_PTR(-ENOTDIR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 176 | if (hlen != sizeof(xfs_handle_t)) |
| 177 | return ERR_PTR(-EINVAL); |
| 178 | if (copy_from_user(&handle, uhandle, hlen)) |
| 179 | return ERR_PTR(-EFAULT); |
| 180 | if (handle.ha_fid.fid_len != |
| 181 | sizeof(handle.ha_fid) - sizeof(handle.ha_fid.fid_len)) |
| 182 | return ERR_PTR(-EINVAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 184 | memset(&fid, 0, sizeof(struct fid)); |
| 185 | fid.ino = handle.ha_fid.fid_ino; |
| 186 | fid.gen = handle.ha_fid.fid_gen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 188 | return exportfs_decode_fh(parfilp->f_path.mnt, (struct fid *)&fid, 3, |
| 189 | FILEID_INO32_GEN | XFS_FILEID_TYPE_64FLAG, |
| 190 | xfs_handle_acceptable, NULL); |
| 191 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 193 | STATIC struct dentry * |
| 194 | xfs_handlereq_to_dentry( |
| 195 | struct file *parfilp, |
| 196 | xfs_fsop_handlereq_t *hreq) |
| 197 | { |
| 198 | return xfs_handle_to_dentry(parfilp, hreq->ihandle, hreq->ihandlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | } |
| 200 | |
sandeen@sandeen.net | d5547f9 | 2008-11-25 21:20:08 -0600 | [diff] [blame] | 201 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | xfs_open_by_handle( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | struct file *parfilp, |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 204 | xfs_fsop_handlereq_t *hreq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | { |
David Howells | 745ca24 | 2008-11-14 10:39:22 +1100 | [diff] [blame] | 206 | const struct cred *cred = current_cred(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | int error; |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 208 | int fd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | int permflag; |
| 210 | struct file *filp; |
| 211 | struct inode *inode; |
| 212 | struct dentry *dentry; |
Dave Chinner | 1a1d772 | 2012-03-22 05:15:06 +0000 | [diff] [blame] | 213 | fmode_t fmode; |
Al Viro | 765927b | 2012-06-26 21:58:53 +0400 | [diff] [blame] | 214 | struct path path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | |
| 216 | if (!capable(CAP_SYS_ADMIN)) |
| 217 | return -XFS_ERROR(EPERM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 219 | dentry = xfs_handlereq_to_dentry(parfilp, hreq); |
| 220 | if (IS_ERR(dentry)) |
| 221 | return PTR_ERR(dentry); |
| 222 | inode = dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | |
| 224 | /* Restrict xfs_open_by_handle to directories & regular files. */ |
| 225 | if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode))) { |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 226 | error = -XFS_ERROR(EPERM); |
| 227 | goto out_dput; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | #if BITS_PER_LONG != 32 |
sandeen@sandeen.net | 743bb465 | 2008-11-25 21:20:06 -0600 | [diff] [blame] | 231 | hreq->oflags |= O_LARGEFILE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | #endif |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 233 | |
sandeen@sandeen.net | 743bb465 | 2008-11-25 21:20:06 -0600 | [diff] [blame] | 234 | permflag = hreq->oflags; |
Dave Chinner | 1a1d772 | 2012-03-22 05:15:06 +0000 | [diff] [blame] | 235 | fmode = OPEN_FMODE(permflag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | if ((!(permflag & O_APPEND) || (permflag & O_TRUNC)) && |
Dave Chinner | 1a1d772 | 2012-03-22 05:15:06 +0000 | [diff] [blame] | 237 | (fmode & FMODE_WRITE) && IS_APPEND(inode)) { |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 238 | error = -XFS_ERROR(EPERM); |
| 239 | goto out_dput; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | } |
| 241 | |
Dave Chinner | 1a1d772 | 2012-03-22 05:15:06 +0000 | [diff] [blame] | 242 | if ((fmode & FMODE_WRITE) && IS_IMMUTABLE(inode)) { |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 243 | error = -XFS_ERROR(EACCES); |
| 244 | goto out_dput; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | /* Can't write directories. */ |
Dave Chinner | 1a1d772 | 2012-03-22 05:15:06 +0000 | [diff] [blame] | 248 | if (S_ISDIR(inode->i_mode) && (fmode & FMODE_WRITE)) { |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 249 | error = -XFS_ERROR(EISDIR); |
| 250 | goto out_dput; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | } |
| 252 | |
Yann Droneaud | 862a629 | 2013-07-02 18:39:34 +0200 | [diff] [blame] | 253 | fd = get_unused_fd_flags(0); |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 254 | if (fd < 0) { |
| 255 | error = fd; |
| 256 | goto out_dput; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | } |
| 258 | |
Al Viro | 765927b | 2012-06-26 21:58:53 +0400 | [diff] [blame] | 259 | path.mnt = parfilp->f_path.mnt; |
| 260 | path.dentry = dentry; |
| 261 | filp = dentry_open(&path, hreq->oflags, cred); |
| 262 | dput(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | if (IS_ERR(filp)) { |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 264 | put_unused_fd(fd); |
| 265 | return PTR_ERR(filp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | } |
Christoph Hellwig | 4d4be48 | 2008-12-09 04:47:33 -0500 | [diff] [blame] | 267 | |
Al Viro | 0320937 | 2011-07-25 20:54:24 -0400 | [diff] [blame] | 268 | if (S_ISREG(inode->i_mode)) { |
Vlad Apostolov | 2e2e7bb | 2006-11-11 18:04:47 +1100 | [diff] [blame] | 269 | filp->f_flags |= O_NOATIME; |
Christoph Hellwig | 4d4be48 | 2008-12-09 04:47:33 -0500 | [diff] [blame] | 270 | filp->f_mode |= FMODE_NOCMTIME; |
Vlad Apostolov | 2e2e7bb | 2006-11-11 18:04:47 +1100 | [diff] [blame] | 271 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 273 | fd_install(fd, filp); |
| 274 | return fd; |
| 275 | |
| 276 | out_dput: |
| 277 | dput(dentry); |
| 278 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Christoph Hellwig | 804c83c | 2007-08-28 13:59:03 +1000 | [diff] [blame] | 281 | /* |
| 282 | * This is a copy from fs/namei.c:vfs_readlink(), except for removing it's |
| 283 | * unused first argument. |
| 284 | */ |
| 285 | STATIC int |
| 286 | do_readlink( |
| 287 | char __user *buffer, |
| 288 | int buflen, |
| 289 | const char *link) |
| 290 | { |
| 291 | int len; |
| 292 | |
| 293 | len = PTR_ERR(link); |
| 294 | if (IS_ERR(link)) |
| 295 | goto out; |
| 296 | |
| 297 | len = strlen(link); |
| 298 | if (len > (unsigned) buflen) |
| 299 | len = buflen; |
| 300 | if (copy_to_user(buffer, link, len)) |
| 301 | len = -EFAULT; |
| 302 | out: |
| 303 | return len; |
| 304 | } |
| 305 | |
| 306 | |
sandeen@sandeen.net | d5547f9 | 2008-11-25 21:20:08 -0600 | [diff] [blame] | 307 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | xfs_readlink_by_handle( |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 309 | struct file *parfilp, |
| 310 | xfs_fsop_handlereq_t *hreq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | { |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 312 | struct dentry *dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | __u32 olen; |
Christoph Hellwig | 804c83c | 2007-08-28 13:59:03 +1000 | [diff] [blame] | 314 | void *link; |
| 315 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | |
| 317 | if (!capable(CAP_SYS_ADMIN)) |
| 318 | return -XFS_ERROR(EPERM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 320 | dentry = xfs_handlereq_to_dentry(parfilp, hreq); |
| 321 | if (IS_ERR(dentry)) |
| 322 | return PTR_ERR(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | |
| 324 | /* Restrict this handle operation to symlinks only. */ |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 325 | if (!S_ISLNK(dentry->d_inode->i_mode)) { |
Christoph Hellwig | 804c83c | 2007-08-28 13:59:03 +1000 | [diff] [blame] | 326 | error = -XFS_ERROR(EINVAL); |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 327 | goto out_dput; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | } |
| 329 | |
sandeen@sandeen.net | 743bb465 | 2008-11-25 21:20:06 -0600 | [diff] [blame] | 330 | if (copy_from_user(&olen, hreq->ohandlen, sizeof(__u32))) { |
Christoph Hellwig | 804c83c | 2007-08-28 13:59:03 +1000 | [diff] [blame] | 331 | error = -XFS_ERROR(EFAULT); |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 332 | goto out_dput; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | |
Christoph Hellwig | 804c83c | 2007-08-28 13:59:03 +1000 | [diff] [blame] | 335 | link = kmalloc(MAXPATHLEN+1, GFP_KERNEL); |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 336 | if (!link) { |
| 337 | error = -XFS_ERROR(ENOMEM); |
| 338 | goto out_dput; |
| 339 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 341 | error = -xfs_readlink(XFS_I(dentry->d_inode), link); |
Nathan Scott | 67fcaa7 | 2006-06-09 17:00:52 +1000 | [diff] [blame] | 342 | if (error) |
Christoph Hellwig | 804c83c | 2007-08-28 13:59:03 +1000 | [diff] [blame] | 343 | goto out_kfree; |
sandeen@sandeen.net | 743bb465 | 2008-11-25 21:20:06 -0600 | [diff] [blame] | 344 | error = do_readlink(hreq->ohandle, olen, link); |
Christoph Hellwig | 804c83c | 2007-08-28 13:59:03 +1000 | [diff] [blame] | 345 | if (error) |
| 346 | goto out_kfree; |
Nathan Scott | 67fcaa7 | 2006-06-09 17:00:52 +1000 | [diff] [blame] | 347 | |
Christoph Hellwig | 804c83c | 2007-08-28 13:59:03 +1000 | [diff] [blame] | 348 | out_kfree: |
| 349 | kfree(link); |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 350 | out_dput: |
| 351 | dput(dentry); |
Christoph Hellwig | 804c83c | 2007-08-28 13:59:03 +1000 | [diff] [blame] | 352 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | STATIC int |
| 356 | xfs_fssetdm_by_handle( |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 357 | struct file *parfilp, |
| 358 | void __user *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | { |
| 360 | int error; |
| 361 | struct fsdmidata fsd; |
| 362 | xfs_fsop_setdm_handlereq_t dmhreq; |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 363 | struct dentry *dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | |
| 365 | if (!capable(CAP_MKNOD)) |
| 366 | return -XFS_ERROR(EPERM); |
| 367 | if (copy_from_user(&dmhreq, arg, sizeof(xfs_fsop_setdm_handlereq_t))) |
| 368 | return -XFS_ERROR(EFAULT); |
| 369 | |
Jan Kara | d9457dc | 2012-06-12 16:20:39 +0200 | [diff] [blame] | 370 | error = mnt_want_write_file(parfilp); |
| 371 | if (error) |
| 372 | return error; |
| 373 | |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 374 | dentry = xfs_handlereq_to_dentry(parfilp, &dmhreq.hreq); |
Jan Kara | d9457dc | 2012-06-12 16:20:39 +0200 | [diff] [blame] | 375 | if (IS_ERR(dentry)) { |
| 376 | mnt_drop_write_file(parfilp); |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 377 | return PTR_ERR(dentry); |
Jan Kara | d9457dc | 2012-06-12 16:20:39 +0200 | [diff] [blame] | 378 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 380 | if (IS_IMMUTABLE(dentry->d_inode) || IS_APPEND(dentry->d_inode)) { |
Christoph Hellwig | 6e7f75e | 2007-10-11 18:09:50 +1000 | [diff] [blame] | 381 | error = -XFS_ERROR(EPERM); |
| 382 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | if (copy_from_user(&fsd, dmhreq.data, sizeof(fsd))) { |
Christoph Hellwig | 6e7f75e | 2007-10-11 18:09:50 +1000 | [diff] [blame] | 386 | error = -XFS_ERROR(EFAULT); |
| 387 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | } |
| 389 | |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 390 | error = -xfs_set_dmattrs(XFS_I(dentry->d_inode), fsd.fsd_dmevmask, |
Christoph Hellwig | 6e7f75e | 2007-10-11 18:09:50 +1000 | [diff] [blame] | 391 | fsd.fsd_dmstate); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | |
Christoph Hellwig | 6e7f75e | 2007-10-11 18:09:50 +1000 | [diff] [blame] | 393 | out: |
Jan Kara | d9457dc | 2012-06-12 16:20:39 +0200 | [diff] [blame] | 394 | mnt_drop_write_file(parfilp); |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 395 | dput(dentry); |
Christoph Hellwig | 6e7f75e | 2007-10-11 18:09:50 +1000 | [diff] [blame] | 396 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | STATIC int |
| 400 | xfs_attrlist_by_handle( |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 401 | struct file *parfilp, |
| 402 | void __user *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | { |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 404 | int error = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | attrlist_cursor_kern_t *cursor; |
| 406 | xfs_fsop_attrlist_handlereq_t al_hreq; |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 407 | struct dentry *dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | char *kbuf; |
| 409 | |
| 410 | if (!capable(CAP_SYS_ADMIN)) |
| 411 | return -XFS_ERROR(EPERM); |
| 412 | if (copy_from_user(&al_hreq, arg, sizeof(xfs_fsop_attrlist_handlereq_t))) |
| 413 | return -XFS_ERROR(EFAULT); |
| 414 | if (al_hreq.buflen > XATTR_LIST_MAX) |
| 415 | return -XFS_ERROR(EINVAL); |
| 416 | |
Christoph Hellwig | 90ad58a | 2008-06-27 13:32:19 +1000 | [diff] [blame] | 417 | /* |
| 418 | * Reject flags, only allow namespaces. |
| 419 | */ |
| 420 | if (al_hreq.flags & ~(ATTR_ROOT | ATTR_SECURE)) |
| 421 | return -XFS_ERROR(EINVAL); |
| 422 | |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 423 | dentry = xfs_handlereq_to_dentry(parfilp, &al_hreq.hreq); |
| 424 | if (IS_ERR(dentry)) |
| 425 | return PTR_ERR(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | |
Eric Sandeen | dd700d9 | 2013-04-04 00:05:13 -0500 | [diff] [blame] | 427 | kbuf = kmem_zalloc(al_hreq.buflen, KM_SLEEP | KM_MAYFAIL); |
| 428 | if (!kbuf) { |
| 429 | kbuf = kmem_zalloc_large(al_hreq.buflen); |
| 430 | if (!kbuf) |
| 431 | goto out_dput; |
| 432 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | |
| 434 | cursor = (attrlist_cursor_kern_t *)&al_hreq.pos; |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 435 | error = -xfs_attr_list(XFS_I(dentry->d_inode), kbuf, al_hreq.buflen, |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 436 | al_hreq.flags, cursor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | if (error) |
| 438 | goto out_kfree; |
| 439 | |
| 440 | if (copy_to_user(al_hreq.buffer, kbuf, al_hreq.buflen)) |
| 441 | error = -EFAULT; |
| 442 | |
| 443 | out_kfree: |
Eric Sandeen | dd700d9 | 2013-04-04 00:05:13 -0500 | [diff] [blame] | 444 | if (is_vmalloc_addr(kbuf)) |
| 445 | kmem_free_large(kbuf); |
| 446 | else |
| 447 | kmem_free(kbuf); |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 448 | out_dput: |
| 449 | dput(dentry); |
| 450 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | } |
| 452 | |
sandeen@sandeen.net | 2875097 | 2008-11-25 21:20:15 -0600 | [diff] [blame] | 453 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | xfs_attrmulti_attr_get( |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 455 | struct inode *inode, |
Dave Chinner | a9273ca | 2010-01-20 10:47:48 +1100 | [diff] [blame] | 456 | unsigned char *name, |
| 457 | unsigned char __user *ubuf, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | __uint32_t *len, |
| 459 | __uint32_t flags) |
| 460 | { |
Dave Chinner | a9273ca | 2010-01-20 10:47:48 +1100 | [diff] [blame] | 461 | unsigned char *kbuf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | int error = EFAULT; |
Barry Naujok | e8b0eba | 2008-04-22 17:34:31 +1000 | [diff] [blame] | 463 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | if (*len > XATTR_SIZE_MAX) |
| 465 | return EINVAL; |
Dave Chinner | ad650f5 | 2012-03-07 04:50:21 +0000 | [diff] [blame] | 466 | kbuf = kmem_zalloc(*len, KM_SLEEP | KM_MAYFAIL); |
| 467 | if (!kbuf) { |
| 468 | kbuf = kmem_zalloc_large(*len); |
| 469 | if (!kbuf) |
| 470 | return ENOMEM; |
| 471 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | |
Barry Naujok | e8b0eba | 2008-04-22 17:34:31 +1000 | [diff] [blame] | 473 | error = xfs_attr_get(XFS_I(inode), name, kbuf, (int *)len, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | if (error) |
| 475 | goto out_kfree; |
| 476 | |
| 477 | if (copy_to_user(ubuf, kbuf, *len)) |
| 478 | error = EFAULT; |
| 479 | |
| 480 | out_kfree: |
Dave Chinner | ad650f5 | 2012-03-07 04:50:21 +0000 | [diff] [blame] | 481 | if (is_vmalloc_addr(kbuf)) |
| 482 | kmem_free_large(kbuf); |
| 483 | else |
| 484 | kmem_free(kbuf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | return error; |
| 486 | } |
| 487 | |
sandeen@sandeen.net | 2875097 | 2008-11-25 21:20:15 -0600 | [diff] [blame] | 488 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | xfs_attrmulti_attr_set( |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 490 | struct inode *inode, |
Dave Chinner | a9273ca | 2010-01-20 10:47:48 +1100 | [diff] [blame] | 491 | unsigned char *name, |
| 492 | const unsigned char __user *ubuf, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | __uint32_t len, |
| 494 | __uint32_t flags) |
| 495 | { |
Dave Chinner | a9273ca | 2010-01-20 10:47:48 +1100 | [diff] [blame] | 496 | unsigned char *kbuf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | int error = EFAULT; |
| 498 | |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 499 | if (IS_IMMUTABLE(inode) || IS_APPEND(inode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | return EPERM; |
| 501 | if (len > XATTR_SIZE_MAX) |
| 502 | return EINVAL; |
| 503 | |
Li Zefan | 0e639bd | 2009-04-08 15:08:04 +0800 | [diff] [blame] | 504 | kbuf = memdup_user(ubuf, len); |
| 505 | if (IS_ERR(kbuf)) |
| 506 | return PTR_ERR(kbuf); |
Barry Naujok | e8b0eba | 2008-04-22 17:34:31 +1000 | [diff] [blame] | 507 | |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 508 | error = xfs_attr_set(XFS_I(inode), name, kbuf, len, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | return error; |
| 511 | } |
| 512 | |
sandeen@sandeen.net | 2875097 | 2008-11-25 21:20:15 -0600 | [diff] [blame] | 513 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | xfs_attrmulti_attr_remove( |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 515 | struct inode *inode, |
Dave Chinner | a9273ca | 2010-01-20 10:47:48 +1100 | [diff] [blame] | 516 | unsigned char *name, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | __uint32_t flags) |
| 518 | { |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 519 | if (IS_IMMUTABLE(inode) || IS_APPEND(inode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | return EPERM; |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 521 | return xfs_attr_remove(XFS_I(inode), name, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | STATIC int |
| 525 | xfs_attrmulti_by_handle( |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 526 | struct file *parfilp, |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 527 | void __user *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | { |
| 529 | int error; |
| 530 | xfs_attr_multiop_t *ops; |
| 531 | xfs_fsop_attrmulti_handlereq_t am_hreq; |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 532 | struct dentry *dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | unsigned int i, size; |
Dave Chinner | a9273ca | 2010-01-20 10:47:48 +1100 | [diff] [blame] | 534 | unsigned char *attr_name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | |
| 536 | if (!capable(CAP_SYS_ADMIN)) |
| 537 | return -XFS_ERROR(EPERM); |
| 538 | if (copy_from_user(&am_hreq, arg, sizeof(xfs_fsop_attrmulti_handlereq_t))) |
| 539 | return -XFS_ERROR(EFAULT); |
| 540 | |
Zhitong Wang | fda168c | 2010-03-23 09:51:22 +1100 | [diff] [blame] | 541 | /* overflow check */ |
| 542 | if (am_hreq.opcount >= INT_MAX / sizeof(xfs_attr_multiop_t)) |
| 543 | return -E2BIG; |
| 544 | |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 545 | dentry = xfs_handlereq_to_dentry(parfilp, &am_hreq.hreq); |
| 546 | if (IS_ERR(dentry)) |
| 547 | return PTR_ERR(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | |
| 549 | error = E2BIG; |
Christoph Hellwig | e182f57 | 2008-06-27 13:32:31 +1000 | [diff] [blame] | 550 | size = am_hreq.opcount * sizeof(xfs_attr_multiop_t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | if (!size || size > 16 * PAGE_SIZE) |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 552 | goto out_dput; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | |
Li Zefan | 0e639bd | 2009-04-08 15:08:04 +0800 | [diff] [blame] | 554 | ops = memdup_user(am_hreq.ops, size); |
| 555 | if (IS_ERR(ops)) { |
| 556 | error = PTR_ERR(ops); |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 557 | goto out_dput; |
Li Zefan | 0e639bd | 2009-04-08 15:08:04 +0800 | [diff] [blame] | 558 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | |
| 560 | attr_name = kmalloc(MAXNAMELEN, GFP_KERNEL); |
| 561 | if (!attr_name) |
| 562 | goto out_kfree_ops; |
| 563 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | error = 0; |
| 565 | for (i = 0; i < am_hreq.opcount; i++) { |
Dave Chinner | a9273ca | 2010-01-20 10:47:48 +1100 | [diff] [blame] | 566 | ops[i].am_error = strncpy_from_user((char *)attr_name, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | ops[i].am_attrname, MAXNAMELEN); |
| 568 | if (ops[i].am_error == 0 || ops[i].am_error == MAXNAMELEN) |
| 569 | error = -ERANGE; |
| 570 | if (ops[i].am_error < 0) |
| 571 | break; |
| 572 | |
| 573 | switch (ops[i].am_opcode) { |
| 574 | case ATTR_OP_GET: |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 575 | ops[i].am_error = xfs_attrmulti_attr_get( |
| 576 | dentry->d_inode, attr_name, |
| 577 | ops[i].am_attrvalue, &ops[i].am_length, |
| 578 | ops[i].am_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | break; |
| 580 | case ATTR_OP_SET: |
Al Viro | a561be7 | 2011-11-23 11:57:51 -0500 | [diff] [blame] | 581 | ops[i].am_error = mnt_want_write_file(parfilp); |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 582 | if (ops[i].am_error) |
| 583 | break; |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 584 | ops[i].am_error = xfs_attrmulti_attr_set( |
| 585 | dentry->d_inode, attr_name, |
| 586 | ops[i].am_attrvalue, ops[i].am_length, |
| 587 | ops[i].am_flags); |
Al Viro | 2a79f17 | 2011-12-09 08:06:57 -0500 | [diff] [blame] | 588 | mnt_drop_write_file(parfilp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | break; |
| 590 | case ATTR_OP_REMOVE: |
Al Viro | a561be7 | 2011-11-23 11:57:51 -0500 | [diff] [blame] | 591 | ops[i].am_error = mnt_want_write_file(parfilp); |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 592 | if (ops[i].am_error) |
| 593 | break; |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 594 | ops[i].am_error = xfs_attrmulti_attr_remove( |
| 595 | dentry->d_inode, attr_name, |
| 596 | ops[i].am_flags); |
Al Viro | 2a79f17 | 2011-12-09 08:06:57 -0500 | [diff] [blame] | 597 | mnt_drop_write_file(parfilp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | break; |
| 599 | default: |
| 600 | ops[i].am_error = EINVAL; |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | if (copy_to_user(am_hreq.ops, ops, size)) |
| 605 | error = XFS_ERROR(EFAULT); |
| 606 | |
| 607 | kfree(attr_name); |
| 608 | out_kfree_ops: |
| 609 | kfree(ops); |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 610 | out_dput: |
| 611 | dput(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | return -error; |
| 613 | } |
| 614 | |
sandeen@sandeen.net | d5547f9 | 2008-11-25 21:20:08 -0600 | [diff] [blame] | 615 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | xfs_ioc_space( |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 617 | struct xfs_inode *ip, |
Alexey Dobriyan | f37ea14 | 2006-09-28 10:52:04 +1000 | [diff] [blame] | 618 | struct inode *inode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | struct file *filp, |
| 620 | int ioflags, |
| 621 | unsigned int cmd, |
sandeen@sandeen.net | 743bb465 | 2008-11-25 21:20:06 -0600 | [diff] [blame] | 622 | xfs_flock64_t *bf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | int attr_flags = 0; |
| 625 | int error; |
| 626 | |
sandeen@sandeen.net | 743bb465 | 2008-11-25 21:20:06 -0600 | [diff] [blame] | 627 | /* |
| 628 | * Only allow the sys admin to reserve space unless |
| 629 | * unwritten extents are enabled. |
| 630 | */ |
| 631 | if (!xfs_sb_version_hasextflgbit(&ip->i_mount->m_sb) && |
| 632 | !capable(CAP_SYS_ADMIN)) |
| 633 | return -XFS_ERROR(EPERM); |
| 634 | |
Alexey Dobriyan | f37ea14 | 2006-09-28 10:52:04 +1000 | [diff] [blame] | 635 | if (inode->i_flags & (S_IMMUTABLE|S_APPEND)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | return -XFS_ERROR(EPERM); |
| 637 | |
Eric Sandeen | ad4a8ac | 2005-09-02 16:41:16 +1000 | [diff] [blame] | 638 | if (!(filp->f_mode & FMODE_WRITE)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | return -XFS_ERROR(EBADF); |
| 640 | |
Alexey Dobriyan | f37ea14 | 2006-09-28 10:52:04 +1000 | [diff] [blame] | 641 | if (!S_ISREG(inode->i_mode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | return -XFS_ERROR(EINVAL); |
| 643 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | if (filp->f_flags & (O_NDELAY|O_NONBLOCK)) |
Christoph Hellwig | 0f285c8 | 2008-07-18 17:13:28 +1000 | [diff] [blame] | 645 | attr_flags |= XFS_ATTR_NONBLOCK; |
Dave Chinner | 8287889 | 2011-03-26 09:13:08 +1100 | [diff] [blame] | 646 | |
| 647 | if (filp->f_flags & O_DSYNC) |
| 648 | attr_flags |= XFS_ATTR_SYNC; |
| 649 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | if (ioflags & IO_INVIS) |
Christoph Hellwig | 0f285c8 | 2008-07-18 17:13:28 +1000 | [diff] [blame] | 651 | attr_flags |= XFS_ATTR_DMI; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | |
Jan Kara | d9457dc | 2012-06-12 16:20:39 +0200 | [diff] [blame] | 653 | error = mnt_want_write_file(filp); |
| 654 | if (error) |
| 655 | return error; |
sandeen@sandeen.net | 743bb465 | 2008-11-25 21:20:06 -0600 | [diff] [blame] | 656 | error = xfs_change_file_space(ip, cmd, bf, filp->f_pos, attr_flags); |
Jan Kara | d9457dc | 2012-06-12 16:20:39 +0200 | [diff] [blame] | 657 | mnt_drop_write_file(filp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | return -error; |
| 659 | } |
| 660 | |
| 661 | STATIC int |
| 662 | xfs_ioc_bulkstat( |
| 663 | xfs_mount_t *mp, |
| 664 | unsigned int cmd, |
| 665 | void __user *arg) |
| 666 | { |
| 667 | xfs_fsop_bulkreq_t bulkreq; |
| 668 | int count; /* # of records returned */ |
| 669 | xfs_ino_t inlast; /* last inode number */ |
| 670 | int done; |
| 671 | int error; |
| 672 | |
| 673 | /* done = 1 if there are more stats to get and if bulkstat */ |
| 674 | /* should be called again (unused here, but used in dmapi) */ |
| 675 | |
| 676 | if (!capable(CAP_SYS_ADMIN)) |
| 677 | return -EPERM; |
| 678 | |
| 679 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 680 | return -XFS_ERROR(EIO); |
| 681 | |
| 682 | if (copy_from_user(&bulkreq, arg, sizeof(xfs_fsop_bulkreq_t))) |
| 683 | return -XFS_ERROR(EFAULT); |
| 684 | |
| 685 | if (copy_from_user(&inlast, bulkreq.lastip, sizeof(__s64))) |
| 686 | return -XFS_ERROR(EFAULT); |
| 687 | |
| 688 | if ((count = bulkreq.icount) <= 0) |
| 689 | return -XFS_ERROR(EINVAL); |
| 690 | |
Lachlan McIlroy | cd57e59 | 2007-11-23 16:30:32 +1100 | [diff] [blame] | 691 | if (bulkreq.ubuffer == NULL) |
| 692 | return -XFS_ERROR(EINVAL); |
| 693 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | if (cmd == XFS_IOC_FSINUMBERS) |
| 695 | error = xfs_inumbers(mp, &inlast, &count, |
Michal Marek | faa63e9 | 2007-07-11 11:10:19 +1000 | [diff] [blame] | 696 | bulkreq.ubuffer, xfs_inumbers_fmt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | else if (cmd == XFS_IOC_FSBULKSTAT_SINGLE) |
| 698 | error = xfs_bulkstat_single(mp, &inlast, |
| 699 | bulkreq.ubuffer, &done); |
Lachlan McIlroy | cd57e59 | 2007-11-23 16:30:32 +1100 | [diff] [blame] | 700 | else /* XFS_IOC_FSBULKSTAT */ |
Christoph Hellwig | 7dce11d | 2010-06-23 18:11:11 +1000 | [diff] [blame] | 701 | error = xfs_bulkstat(mp, &inlast, &count, xfs_bulkstat_one, |
| 702 | sizeof(xfs_bstat_t), bulkreq.ubuffer, |
| 703 | &done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | |
| 705 | if (error) |
| 706 | return -error; |
| 707 | |
| 708 | if (bulkreq.ocount != NULL) { |
| 709 | if (copy_to_user(bulkreq.lastip, &inlast, |
| 710 | sizeof(xfs_ino_t))) |
| 711 | return -XFS_ERROR(EFAULT); |
| 712 | |
| 713 | if (copy_to_user(bulkreq.ocount, &count, sizeof(count))) |
| 714 | return -XFS_ERROR(EFAULT); |
| 715 | } |
| 716 | |
| 717 | return 0; |
| 718 | } |
| 719 | |
| 720 | STATIC int |
| 721 | xfs_ioc_fsgeometry_v1( |
| 722 | xfs_mount_t *mp, |
| 723 | void __user *arg) |
| 724 | { |
Alex Elder | eeb2036 | 2011-03-01 17:50:00 +0000 | [diff] [blame] | 725 | xfs_fsop_geom_t fsgeo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | int error; |
| 727 | |
Alex Elder | eeb2036 | 2011-03-01 17:50:00 +0000 | [diff] [blame] | 728 | error = xfs_fs_geometry(mp, &fsgeo, 3); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | if (error) |
| 730 | return -error; |
| 731 | |
Alex Elder | eeb2036 | 2011-03-01 17:50:00 +0000 | [diff] [blame] | 732 | /* |
| 733 | * Caller should have passed an argument of type |
| 734 | * xfs_fsop_geom_v1_t. This is a proper subset of the |
| 735 | * xfs_fsop_geom_t that xfs_fs_geometry() fills in. |
| 736 | */ |
| 737 | if (copy_to_user(arg, &fsgeo, sizeof(xfs_fsop_geom_v1_t))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | return -XFS_ERROR(EFAULT); |
| 739 | return 0; |
| 740 | } |
| 741 | |
| 742 | STATIC int |
| 743 | xfs_ioc_fsgeometry( |
| 744 | xfs_mount_t *mp, |
| 745 | void __user *arg) |
| 746 | { |
| 747 | xfs_fsop_geom_t fsgeo; |
| 748 | int error; |
| 749 | |
| 750 | error = xfs_fs_geometry(mp, &fsgeo, 4); |
| 751 | if (error) |
| 752 | return -error; |
| 753 | |
| 754 | if (copy_to_user(arg, &fsgeo, sizeof(fsgeo))) |
| 755 | return -XFS_ERROR(EFAULT); |
| 756 | return 0; |
| 757 | } |
| 758 | |
| 759 | /* |
| 760 | * Linux extended inode flags interface. |
| 761 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | |
| 763 | STATIC unsigned int |
| 764 | xfs_merge_ioc_xflags( |
| 765 | unsigned int flags, |
| 766 | unsigned int start) |
| 767 | { |
| 768 | unsigned int xflags = start; |
| 769 | |
Eric Sandeen | 39058a0 | 2007-02-10 18:37:10 +1100 | [diff] [blame] | 770 | if (flags & FS_IMMUTABLE_FL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | xflags |= XFS_XFLAG_IMMUTABLE; |
| 772 | else |
| 773 | xflags &= ~XFS_XFLAG_IMMUTABLE; |
Eric Sandeen | 39058a0 | 2007-02-10 18:37:10 +1100 | [diff] [blame] | 774 | if (flags & FS_APPEND_FL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | xflags |= XFS_XFLAG_APPEND; |
| 776 | else |
| 777 | xflags &= ~XFS_XFLAG_APPEND; |
Eric Sandeen | 39058a0 | 2007-02-10 18:37:10 +1100 | [diff] [blame] | 778 | if (flags & FS_SYNC_FL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | xflags |= XFS_XFLAG_SYNC; |
| 780 | else |
| 781 | xflags &= ~XFS_XFLAG_SYNC; |
Eric Sandeen | 39058a0 | 2007-02-10 18:37:10 +1100 | [diff] [blame] | 782 | if (flags & FS_NOATIME_FL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | xflags |= XFS_XFLAG_NOATIME; |
| 784 | else |
| 785 | xflags &= ~XFS_XFLAG_NOATIME; |
Eric Sandeen | 39058a0 | 2007-02-10 18:37:10 +1100 | [diff] [blame] | 786 | if (flags & FS_NODUMP_FL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | xflags |= XFS_XFLAG_NODUMP; |
| 788 | else |
| 789 | xflags &= ~XFS_XFLAG_NODUMP; |
| 790 | |
| 791 | return xflags; |
| 792 | } |
| 793 | |
| 794 | STATIC unsigned int |
| 795 | xfs_di2lxflags( |
| 796 | __uint16_t di_flags) |
| 797 | { |
| 798 | unsigned int flags = 0; |
| 799 | |
| 800 | if (di_flags & XFS_DIFLAG_IMMUTABLE) |
Eric Sandeen | 39058a0 | 2007-02-10 18:37:10 +1100 | [diff] [blame] | 801 | flags |= FS_IMMUTABLE_FL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | if (di_flags & XFS_DIFLAG_APPEND) |
Eric Sandeen | 39058a0 | 2007-02-10 18:37:10 +1100 | [diff] [blame] | 803 | flags |= FS_APPEND_FL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | if (di_flags & XFS_DIFLAG_SYNC) |
Eric Sandeen | 39058a0 | 2007-02-10 18:37:10 +1100 | [diff] [blame] | 805 | flags |= FS_SYNC_FL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | if (di_flags & XFS_DIFLAG_NOATIME) |
Eric Sandeen | 39058a0 | 2007-02-10 18:37:10 +1100 | [diff] [blame] | 807 | flags |= FS_NOATIME_FL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | if (di_flags & XFS_DIFLAG_NODUMP) |
Eric Sandeen | 39058a0 | 2007-02-10 18:37:10 +1100 | [diff] [blame] | 809 | flags |= FS_NODUMP_FL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | return flags; |
| 811 | } |
| 812 | |
| 813 | STATIC int |
Christoph Hellwig | c83bfab | 2007-10-11 17:47:00 +1000 | [diff] [blame] | 814 | xfs_ioc_fsgetxattr( |
| 815 | xfs_inode_t *ip, |
| 816 | int attr, |
| 817 | void __user *arg) |
| 818 | { |
| 819 | struct fsxattr fa; |
| 820 | |
Dan Rosenberg | a122eb2 | 2010-09-06 18:24:57 -0400 | [diff] [blame] | 821 | memset(&fa, 0, sizeof(struct fsxattr)); |
| 822 | |
Christoph Hellwig | c83bfab | 2007-10-11 17:47:00 +1000 | [diff] [blame] | 823 | xfs_ilock(ip, XFS_ILOCK_SHARED); |
| 824 | fa.fsx_xflags = xfs_ip2xflags(ip); |
| 825 | fa.fsx_extsize = ip->i_d.di_extsize << ip->i_mount->m_sb.sb_blocklog; |
Arkadiusz Mi?kiewicz | 6743099 | 2010-09-26 06:10:18 +0000 | [diff] [blame] | 826 | fa.fsx_projid = xfs_get_projid(ip); |
Christoph Hellwig | c83bfab | 2007-10-11 17:47:00 +1000 | [diff] [blame] | 827 | |
| 828 | if (attr) { |
| 829 | if (ip->i_afp) { |
| 830 | if (ip->i_afp->if_flags & XFS_IFEXTENTS) |
| 831 | fa.fsx_nextents = ip->i_afp->if_bytes / |
| 832 | sizeof(xfs_bmbt_rec_t); |
| 833 | else |
| 834 | fa.fsx_nextents = ip->i_d.di_anextents; |
| 835 | } else |
| 836 | fa.fsx_nextents = 0; |
| 837 | } else { |
| 838 | if (ip->i_df.if_flags & XFS_IFEXTENTS) |
| 839 | fa.fsx_nextents = ip->i_df.if_bytes / |
| 840 | sizeof(xfs_bmbt_rec_t); |
| 841 | else |
| 842 | fa.fsx_nextents = ip->i_d.di_nextents; |
| 843 | } |
| 844 | xfs_iunlock(ip, XFS_ILOCK_SHARED); |
| 845 | |
| 846 | if (copy_to_user(arg, &fa, sizeof(fa))) |
| 847 | return -EFAULT; |
| 848 | return 0; |
| 849 | } |
| 850 | |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 851 | STATIC void |
| 852 | xfs_set_diflags( |
| 853 | struct xfs_inode *ip, |
| 854 | unsigned int xflags) |
| 855 | { |
| 856 | unsigned int di_flags; |
| 857 | |
| 858 | /* can't set PREALLOC this way, just preserve it */ |
| 859 | di_flags = (ip->i_d.di_flags & XFS_DIFLAG_PREALLOC); |
| 860 | if (xflags & XFS_XFLAG_IMMUTABLE) |
| 861 | di_flags |= XFS_DIFLAG_IMMUTABLE; |
| 862 | if (xflags & XFS_XFLAG_APPEND) |
| 863 | di_flags |= XFS_DIFLAG_APPEND; |
| 864 | if (xflags & XFS_XFLAG_SYNC) |
| 865 | di_flags |= XFS_DIFLAG_SYNC; |
| 866 | if (xflags & XFS_XFLAG_NOATIME) |
| 867 | di_flags |= XFS_DIFLAG_NOATIME; |
| 868 | if (xflags & XFS_XFLAG_NODUMP) |
| 869 | di_flags |= XFS_DIFLAG_NODUMP; |
| 870 | if (xflags & XFS_XFLAG_PROJINHERIT) |
| 871 | di_flags |= XFS_DIFLAG_PROJINHERIT; |
| 872 | if (xflags & XFS_XFLAG_NODEFRAG) |
| 873 | di_flags |= XFS_DIFLAG_NODEFRAG; |
| 874 | if (xflags & XFS_XFLAG_FILESTREAM) |
| 875 | di_flags |= XFS_DIFLAG_FILESTREAM; |
Al Viro | abbede1 | 2011-07-26 02:31:30 -0400 | [diff] [blame] | 876 | if (S_ISDIR(ip->i_d.di_mode)) { |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 877 | if (xflags & XFS_XFLAG_RTINHERIT) |
| 878 | di_flags |= XFS_DIFLAG_RTINHERIT; |
| 879 | if (xflags & XFS_XFLAG_NOSYMLINKS) |
| 880 | di_flags |= XFS_DIFLAG_NOSYMLINKS; |
| 881 | if (xflags & XFS_XFLAG_EXTSZINHERIT) |
| 882 | di_flags |= XFS_DIFLAG_EXTSZINHERIT; |
Al Viro | abbede1 | 2011-07-26 02:31:30 -0400 | [diff] [blame] | 883 | } else if (S_ISREG(ip->i_d.di_mode)) { |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 884 | if (xflags & XFS_XFLAG_REALTIME) |
| 885 | di_flags |= XFS_DIFLAG_REALTIME; |
| 886 | if (xflags & XFS_XFLAG_EXTSIZE) |
| 887 | di_flags |= XFS_DIFLAG_EXTSIZE; |
| 888 | } |
| 889 | |
| 890 | ip->i_d.di_flags = di_flags; |
| 891 | } |
| 892 | |
Christoph Hellwig | f13fae2 | 2008-07-21 16:16:15 +1000 | [diff] [blame] | 893 | STATIC void |
| 894 | xfs_diflags_to_linux( |
| 895 | struct xfs_inode *ip) |
| 896 | { |
David Chinner | e4f7529 | 2008-08-13 16:00:45 +1000 | [diff] [blame] | 897 | struct inode *inode = VFS_I(ip); |
Christoph Hellwig | f13fae2 | 2008-07-21 16:16:15 +1000 | [diff] [blame] | 898 | unsigned int xflags = xfs_ip2xflags(ip); |
| 899 | |
| 900 | if (xflags & XFS_XFLAG_IMMUTABLE) |
| 901 | inode->i_flags |= S_IMMUTABLE; |
| 902 | else |
| 903 | inode->i_flags &= ~S_IMMUTABLE; |
| 904 | if (xflags & XFS_XFLAG_APPEND) |
| 905 | inode->i_flags |= S_APPEND; |
| 906 | else |
| 907 | inode->i_flags &= ~S_APPEND; |
| 908 | if (xflags & XFS_XFLAG_SYNC) |
| 909 | inode->i_flags |= S_SYNC; |
| 910 | else |
| 911 | inode->i_flags &= ~S_SYNC; |
| 912 | if (xflags & XFS_XFLAG_NOATIME) |
| 913 | inode->i_flags |= S_NOATIME; |
| 914 | else |
| 915 | inode->i_flags &= ~S_NOATIME; |
| 916 | } |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 917 | |
| 918 | #define FSX_PROJID 1 |
| 919 | #define FSX_EXTSIZE 2 |
| 920 | #define FSX_XFLAGS 4 |
| 921 | #define FSX_NONBLOCK 8 |
| 922 | |
| 923 | STATIC int |
| 924 | xfs_ioctl_setattr( |
| 925 | xfs_inode_t *ip, |
| 926 | struct fsxattr *fa, |
| 927 | int mask) |
| 928 | { |
| 929 | struct xfs_mount *mp = ip->i_mount; |
| 930 | struct xfs_trans *tp; |
| 931 | unsigned int lock_flags = 0; |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 932 | struct xfs_dquot *udqp = NULL; |
Chandra Seetharaman | 92f8ff7 | 2013-07-11 00:00:40 -0500 | [diff] [blame] | 933 | struct xfs_dquot *pdqp = NULL; |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 934 | struct xfs_dquot *olddquot = NULL; |
| 935 | int code; |
| 936 | |
Christoph Hellwig | cca28fb | 2010-06-24 11:57:09 +1000 | [diff] [blame] | 937 | trace_xfs_ioctl_setattr(ip); |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 938 | |
| 939 | if (mp->m_flags & XFS_MOUNT_RDONLY) |
| 940 | return XFS_ERROR(EROFS); |
| 941 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 942 | return XFS_ERROR(EIO); |
| 943 | |
| 944 | /* |
Arkadiusz Mi?kiewicz | 6743099 | 2010-09-26 06:10:18 +0000 | [diff] [blame] | 945 | * Disallow 32bit project ids when projid32bit feature is not enabled. |
Arkadiusz Mi?kiewicz | 23963e5 | 2010-08-26 10:19:43 +0000 | [diff] [blame] | 946 | */ |
Arkadiusz Mi?kiewicz | 6743099 | 2010-09-26 06:10:18 +0000 | [diff] [blame] | 947 | if ((mask & FSX_PROJID) && (fa->fsx_projid > (__uint16_t)-1) && |
| 948 | !xfs_sb_version_hasprojid32bit(&ip->i_mount->m_sb)) |
Arkadiusz Mi?kiewicz | 23963e5 | 2010-08-26 10:19:43 +0000 | [diff] [blame] | 949 | return XFS_ERROR(EINVAL); |
| 950 | |
| 951 | /* |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 952 | * If disk quotas is on, we make sure that the dquots do exist on disk, |
| 953 | * before we start any other transactions. Trying to do this later |
| 954 | * is messy. We don't care to take a readlock to look at the ids |
| 955 | * in inode here, because we can't hold it across the trans_reserve. |
| 956 | * If the IDs do change before we take the ilock, we're covered |
| 957 | * because the i_*dquot fields will get updated anyway. |
| 958 | */ |
| 959 | if (XFS_IS_QUOTA_ON(mp) && (mask & FSX_PROJID)) { |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 960 | code = xfs_qm_vop_dqalloc(ip, ip->i_d.di_uid, |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 961 | ip->i_d.di_gid, fa->fsx_projid, |
Chandra Seetharaman | 92f8ff7 | 2013-07-11 00:00:40 -0500 | [diff] [blame] | 962 | XFS_QMOPT_PQUOTA, &udqp, NULL, &pdqp); |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 963 | if (code) |
| 964 | return code; |
| 965 | } |
| 966 | |
| 967 | /* |
| 968 | * For the other attributes, we acquire the inode lock and |
| 969 | * first do an error checking pass. |
| 970 | */ |
| 971 | tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE); |
| 972 | code = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES(mp), 0, 0, 0); |
| 973 | if (code) |
| 974 | goto error_return; |
| 975 | |
| 976 | lock_flags = XFS_ILOCK_EXCL; |
| 977 | xfs_ilock(ip, lock_flags); |
| 978 | |
| 979 | /* |
| 980 | * CAP_FOWNER overrides the following restrictions: |
| 981 | * |
| 982 | * The user ID of the calling process must be equal |
| 983 | * to the file owner ID, except in cases where the |
| 984 | * CAP_FSETID capability is applicable. |
| 985 | */ |
David Howells | 91b777125 | 2008-10-31 15:50:04 +1100 | [diff] [blame] | 986 | if (current_fsuid() != ip->i_d.di_uid && !capable(CAP_FOWNER)) { |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 987 | code = XFS_ERROR(EPERM); |
| 988 | goto error_return; |
| 989 | } |
| 990 | |
| 991 | /* |
| 992 | * Do a quota reservation only if projid is actually going to change. |
| 993 | */ |
| 994 | if (mask & FSX_PROJID) { |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 995 | if (XFS_IS_QUOTA_RUNNING(mp) && |
| 996 | XFS_IS_PQUOTA_ON(mp) && |
Arkadiusz Mi?kiewicz | 6743099 | 2010-09-26 06:10:18 +0000 | [diff] [blame] | 997 | xfs_get_projid(ip) != fa->fsx_projid) { |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 998 | ASSERT(tp); |
Chandra Seetharaman | 92f8ff7 | 2013-07-11 00:00:40 -0500 | [diff] [blame] | 999 | code = xfs_qm_vop_chown_reserve(tp, ip, udqp, NULL, |
| 1000 | pdqp, capable(CAP_FOWNER) ? |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1001 | XFS_QMOPT_FORCE_RES : 0); |
| 1002 | if (code) /* out of quota */ |
| 1003 | goto error_return; |
| 1004 | } |
| 1005 | } |
| 1006 | |
| 1007 | if (mask & FSX_EXTSIZE) { |
| 1008 | /* |
| 1009 | * Can't change extent size if any extents are allocated. |
| 1010 | */ |
| 1011 | if (ip->i_d.di_nextents && |
| 1012 | ((ip->i_d.di_extsize << mp->m_sb.sb_blocklog) != |
| 1013 | fa->fsx_extsize)) { |
| 1014 | code = XFS_ERROR(EINVAL); /* EFBIG? */ |
| 1015 | goto error_return; |
| 1016 | } |
| 1017 | |
| 1018 | /* |
| 1019 | * Extent size must be a multiple of the appropriate block |
Dave Chinner | 5315837 | 2011-01-27 12:18:18 +1100 | [diff] [blame] | 1020 | * size, if set at all. It must also be smaller than the |
| 1021 | * maximum extent size supported by the filesystem. |
| 1022 | * |
| 1023 | * Also, for non-realtime files, limit the extent size hint to |
| 1024 | * half the size of the AGs in the filesystem so alignment |
| 1025 | * doesn't result in extents larger than an AG. |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1026 | */ |
| 1027 | if (fa->fsx_extsize != 0) { |
Dave Chinner | 5315837 | 2011-01-27 12:18:18 +1100 | [diff] [blame] | 1028 | xfs_extlen_t size; |
| 1029 | xfs_fsblock_t extsize_fsb; |
| 1030 | |
| 1031 | extsize_fsb = XFS_B_TO_FSB(mp, fa->fsx_extsize); |
| 1032 | if (extsize_fsb > MAXEXTLEN) { |
| 1033 | code = XFS_ERROR(EINVAL); |
| 1034 | goto error_return; |
| 1035 | } |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1036 | |
| 1037 | if (XFS_IS_REALTIME_INODE(ip) || |
| 1038 | ((mask & FSX_XFLAGS) && |
| 1039 | (fa->fsx_xflags & XFS_XFLAG_REALTIME))) { |
| 1040 | size = mp->m_sb.sb_rextsize << |
| 1041 | mp->m_sb.sb_blocklog; |
| 1042 | } else { |
| 1043 | size = mp->m_sb.sb_blocksize; |
Dave Chinner | 5315837 | 2011-01-27 12:18:18 +1100 | [diff] [blame] | 1044 | if (extsize_fsb > mp->m_sb.sb_agblocks / 2) { |
| 1045 | code = XFS_ERROR(EINVAL); |
| 1046 | goto error_return; |
| 1047 | } |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1048 | } |
| 1049 | |
| 1050 | if (fa->fsx_extsize % size) { |
| 1051 | code = XFS_ERROR(EINVAL); |
| 1052 | goto error_return; |
| 1053 | } |
| 1054 | } |
| 1055 | } |
| 1056 | |
| 1057 | |
| 1058 | if (mask & FSX_XFLAGS) { |
| 1059 | /* |
| 1060 | * Can't change realtime flag if any extents are allocated. |
| 1061 | */ |
| 1062 | if ((ip->i_d.di_nextents || ip->i_delayed_blks) && |
| 1063 | (XFS_IS_REALTIME_INODE(ip)) != |
| 1064 | (fa->fsx_xflags & XFS_XFLAG_REALTIME)) { |
| 1065 | code = XFS_ERROR(EINVAL); /* EFBIG? */ |
| 1066 | goto error_return; |
| 1067 | } |
| 1068 | |
| 1069 | /* |
| 1070 | * If realtime flag is set then must have realtime data. |
| 1071 | */ |
| 1072 | if ((fa->fsx_xflags & XFS_XFLAG_REALTIME)) { |
| 1073 | if ((mp->m_sb.sb_rblocks == 0) || |
| 1074 | (mp->m_sb.sb_rextsize == 0) || |
| 1075 | (ip->i_d.di_extsize % mp->m_sb.sb_rextsize)) { |
| 1076 | code = XFS_ERROR(EINVAL); |
| 1077 | goto error_return; |
| 1078 | } |
| 1079 | } |
| 1080 | |
| 1081 | /* |
| 1082 | * Can't modify an immutable/append-only file unless |
| 1083 | * we have appropriate permission. |
| 1084 | */ |
| 1085 | if ((ip->i_d.di_flags & |
| 1086 | (XFS_DIFLAG_IMMUTABLE|XFS_DIFLAG_APPEND) || |
| 1087 | (fa->fsx_xflags & |
| 1088 | (XFS_XFLAG_IMMUTABLE | XFS_XFLAG_APPEND))) && |
| 1089 | !capable(CAP_LINUX_IMMUTABLE)) { |
| 1090 | code = XFS_ERROR(EPERM); |
| 1091 | goto error_return; |
| 1092 | } |
| 1093 | } |
| 1094 | |
Christoph Hellwig | ddc3415 | 2011-09-19 15:00:54 +0000 | [diff] [blame] | 1095 | xfs_trans_ijoin(tp, ip, 0); |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1096 | |
| 1097 | /* |
| 1098 | * Change file ownership. Must be the owner or privileged. |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1099 | */ |
| 1100 | if (mask & FSX_PROJID) { |
| 1101 | /* |
| 1102 | * CAP_FSETID overrides the following restrictions: |
| 1103 | * |
| 1104 | * The set-user-ID and set-group-ID bits of a file will be |
| 1105 | * cleared upon successful return from chown() |
| 1106 | */ |
| 1107 | if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) && |
| 1108 | !capable(CAP_FSETID)) |
| 1109 | ip->i_d.di_mode &= ~(S_ISUID|S_ISGID); |
| 1110 | |
| 1111 | /* |
| 1112 | * Change the ownerships and register quota modifications |
| 1113 | * in the transaction. |
| 1114 | */ |
Arkadiusz Mi?kiewicz | 6743099 | 2010-09-26 06:10:18 +0000 | [diff] [blame] | 1115 | if (xfs_get_projid(ip) != fa->fsx_projid) { |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 1116 | if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_PQUOTA_ON(mp)) { |
| 1117 | olddquot = xfs_qm_vop_chown(tp, ip, |
Chandra Seetharaman | 92f8ff7 | 2013-07-11 00:00:40 -0500 | [diff] [blame] | 1118 | &ip->i_pdquot, pdqp); |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1119 | } |
Arkadiusz Mi?kiewicz | 6743099 | 2010-09-26 06:10:18 +0000 | [diff] [blame] | 1120 | xfs_set_projid(ip, fa->fsx_projid); |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1121 | |
| 1122 | /* |
| 1123 | * We may have to rev the inode as well as |
| 1124 | * the superblock version number since projids didn't |
| 1125 | * exist before DINODE_VERSION_2 and SB_VERSION_NLINK. |
| 1126 | */ |
Christoph Hellwig | 51ce16d | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 1127 | if (ip->i_d.di_version == 1) |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1128 | xfs_bump_ino_vers2(tp, ip); |
| 1129 | } |
| 1130 | |
| 1131 | } |
| 1132 | |
| 1133 | if (mask & FSX_EXTSIZE) |
| 1134 | ip->i_d.di_extsize = fa->fsx_extsize >> mp->m_sb.sb_blocklog; |
Christoph Hellwig | f13fae2 | 2008-07-21 16:16:15 +1000 | [diff] [blame] | 1135 | if (mask & FSX_XFLAGS) { |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1136 | xfs_set_diflags(ip, fa->fsx_xflags); |
Christoph Hellwig | f13fae2 | 2008-07-21 16:16:15 +1000 | [diff] [blame] | 1137 | xfs_diflags_to_linux(ip); |
| 1138 | } |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1139 | |
Dave Chinner | dcd79a1 | 2010-09-28 12:27:25 +1000 | [diff] [blame] | 1140 | xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG); |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1141 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1142 | |
| 1143 | XFS_STATS_INC(xs_ig_attrchg); |
| 1144 | |
| 1145 | /* |
| 1146 | * If this is a synchronous mount, make sure that the |
| 1147 | * transaction goes to disk before returning to the user. |
| 1148 | * This is slightly sub-optimal in that truncates require |
| 1149 | * two sync transactions instead of one for wsync filesystems. |
| 1150 | * One for the truncate and one for the timestamps since we |
| 1151 | * don't want to change the timestamps unless we're sure the |
| 1152 | * truncate worked. Truncates are less than 1% of the laddis |
| 1153 | * mix so this probably isn't worth the trouble to optimize. |
| 1154 | */ |
| 1155 | if (mp->m_flags & XFS_MOUNT_WSYNC) |
| 1156 | xfs_trans_set_sync(tp); |
| 1157 | code = xfs_trans_commit(tp, 0); |
| 1158 | xfs_iunlock(ip, lock_flags); |
| 1159 | |
| 1160 | /* |
| 1161 | * Release any dquot(s) the inode had kept before chown. |
| 1162 | */ |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 1163 | xfs_qm_dqrele(olddquot); |
| 1164 | xfs_qm_dqrele(udqp); |
Chandra Seetharaman | 92f8ff7 | 2013-07-11 00:00:40 -0500 | [diff] [blame] | 1165 | xfs_qm_dqrele(pdqp); |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1166 | |
Christoph Hellwig | 288699f | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 1167 | return code; |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1168 | |
| 1169 | error_return: |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 1170 | xfs_qm_dqrele(udqp); |
Chandra Seetharaman | 92f8ff7 | 2013-07-11 00:00:40 -0500 | [diff] [blame] | 1171 | xfs_qm_dqrele(pdqp); |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1172 | xfs_trans_cancel(tp, 0); |
| 1173 | if (lock_flags) |
| 1174 | xfs_iunlock(ip, lock_flags); |
| 1175 | return code; |
| 1176 | } |
| 1177 | |
Christoph Hellwig | c83bfab | 2007-10-11 17:47:00 +1000 | [diff] [blame] | 1178 | STATIC int |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1179 | xfs_ioc_fssetxattr( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1180 | xfs_inode_t *ip, |
| 1181 | struct file *filp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1182 | void __user *arg) |
| 1183 | { |
| 1184 | struct fsxattr fa; |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1185 | unsigned int mask; |
Jan Kara | d9457dc | 2012-06-12 16:20:39 +0200 | [diff] [blame] | 1186 | int error; |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1187 | |
| 1188 | if (copy_from_user(&fa, arg, sizeof(fa))) |
| 1189 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1190 | |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1191 | mask = FSX_XFLAGS | FSX_EXTSIZE | FSX_PROJID; |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1192 | if (filp->f_flags & (O_NDELAY|O_NONBLOCK)) |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1193 | mask |= FSX_NONBLOCK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1194 | |
Jan Kara | d9457dc | 2012-06-12 16:20:39 +0200 | [diff] [blame] | 1195 | error = mnt_want_write_file(filp); |
| 1196 | if (error) |
| 1197 | return error; |
| 1198 | error = xfs_ioctl_setattr(ip, &fa, mask); |
| 1199 | mnt_drop_write_file(filp); |
| 1200 | return -error; |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1201 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1202 | |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1203 | STATIC int |
| 1204 | xfs_ioc_getxflags( |
| 1205 | xfs_inode_t *ip, |
| 1206 | void __user *arg) |
| 1207 | { |
| 1208 | unsigned int flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1209 | |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1210 | flags = xfs_di2lxflags(ip->i_d.di_flags); |
| 1211 | if (copy_to_user(arg, &flags, sizeof(flags))) |
| 1212 | return -EFAULT; |
| 1213 | return 0; |
| 1214 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1215 | |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1216 | STATIC int |
| 1217 | xfs_ioc_setxflags( |
| 1218 | xfs_inode_t *ip, |
| 1219 | struct file *filp, |
| 1220 | void __user *arg) |
| 1221 | { |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1222 | struct fsxattr fa; |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1223 | unsigned int flags; |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1224 | unsigned int mask; |
Jan Kara | d9457dc | 2012-06-12 16:20:39 +0200 | [diff] [blame] | 1225 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1226 | |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1227 | if (copy_from_user(&flags, arg, sizeof(flags))) |
| 1228 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1229 | |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1230 | if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \ |
| 1231 | FS_NOATIME_FL | FS_NODUMP_FL | \ |
| 1232 | FS_SYNC_FL)) |
| 1233 | return -EOPNOTSUPP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1234 | |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1235 | mask = FSX_XFLAGS; |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1236 | if (filp->f_flags & (O_NDELAY|O_NONBLOCK)) |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1237 | mask |= FSX_NONBLOCK; |
| 1238 | fa.fsx_xflags = xfs_merge_ioc_xflags(flags, xfs_ip2xflags(ip)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1239 | |
Jan Kara | d9457dc | 2012-06-12 16:20:39 +0200 | [diff] [blame] | 1240 | error = mnt_want_write_file(filp); |
| 1241 | if (error) |
| 1242 | return error; |
| 1243 | error = xfs_ioctl_setattr(ip, &fa, mask); |
| 1244 | mnt_drop_write_file(filp); |
| 1245 | return -error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1246 | } |
| 1247 | |
| 1248 | STATIC int |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 1249 | xfs_getbmap_format(void **ap, struct getbmapx *bmv, int *full) |
| 1250 | { |
| 1251 | struct getbmap __user *base = *ap; |
| 1252 | |
| 1253 | /* copy only getbmap portion (not getbmapx) */ |
| 1254 | if (copy_to_user(base, bmv, sizeof(struct getbmap))) |
| 1255 | return XFS_ERROR(EFAULT); |
| 1256 | |
| 1257 | *ap += sizeof(struct getbmap); |
| 1258 | return 0; |
| 1259 | } |
| 1260 | |
| 1261 | STATIC int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1262 | xfs_ioc_getbmap( |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 1263 | struct xfs_inode *ip, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1264 | int ioflags, |
| 1265 | unsigned int cmd, |
| 1266 | void __user *arg) |
| 1267 | { |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 1268 | struct getbmapx bmx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1269 | int error; |
| 1270 | |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 1271 | if (copy_from_user(&bmx, arg, sizeof(struct getbmapx))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1272 | return -XFS_ERROR(EFAULT); |
| 1273 | |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 1274 | if (bmx.bmv_count < 2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1275 | return -XFS_ERROR(EINVAL); |
| 1276 | |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 1277 | bmx.bmv_iflags = (cmd == XFS_IOC_GETBMAPA ? BMV_IF_ATTRFORK : 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1278 | if (ioflags & IO_INVIS) |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 1279 | bmx.bmv_iflags |= BMV_IF_NO_DMAPI_READ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1280 | |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 1281 | error = xfs_getbmap(ip, &bmx, xfs_getbmap_format, |
| 1282 | (struct getbmap *)arg+1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1283 | if (error) |
| 1284 | return -error; |
| 1285 | |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 1286 | /* copy back header - only size of getbmap */ |
| 1287 | if (copy_to_user(arg, &bmx, sizeof(struct getbmap))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1288 | return -XFS_ERROR(EFAULT); |
| 1289 | return 0; |
| 1290 | } |
| 1291 | |
| 1292 | STATIC int |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 1293 | xfs_getbmapx_format(void **ap, struct getbmapx *bmv, int *full) |
| 1294 | { |
| 1295 | struct getbmapx __user *base = *ap; |
| 1296 | |
| 1297 | if (copy_to_user(base, bmv, sizeof(struct getbmapx))) |
| 1298 | return XFS_ERROR(EFAULT); |
| 1299 | |
| 1300 | *ap += sizeof(struct getbmapx); |
| 1301 | return 0; |
| 1302 | } |
| 1303 | |
| 1304 | STATIC int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1305 | xfs_ioc_getbmapx( |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 1306 | struct xfs_inode *ip, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1307 | void __user *arg) |
| 1308 | { |
| 1309 | struct getbmapx bmx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1310 | int error; |
| 1311 | |
| 1312 | if (copy_from_user(&bmx, arg, sizeof(bmx))) |
| 1313 | return -XFS_ERROR(EFAULT); |
| 1314 | |
| 1315 | if (bmx.bmv_count < 2) |
| 1316 | return -XFS_ERROR(EINVAL); |
| 1317 | |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 1318 | if (bmx.bmv_iflags & (~BMV_IF_VALID)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1319 | return -XFS_ERROR(EINVAL); |
| 1320 | |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 1321 | error = xfs_getbmap(ip, &bmx, xfs_getbmapx_format, |
| 1322 | (struct getbmapx *)arg+1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1323 | if (error) |
| 1324 | return -error; |
| 1325 | |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 1326 | /* copy back header */ |
| 1327 | if (copy_to_user(arg, &bmx, sizeof(struct getbmapx))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1328 | return -XFS_ERROR(EFAULT); |
| 1329 | |
| 1330 | return 0; |
| 1331 | } |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1332 | |
Christoph Hellwig | 4d4be48 | 2008-12-09 04:47:33 -0500 | [diff] [blame] | 1333 | /* |
| 1334 | * Note: some of the ioctl's return positive numbers as a |
| 1335 | * byte count indicating success, such as readlink_by_handle. |
| 1336 | * So we don't "sign flip" like most other routines. This means |
| 1337 | * true errors need to be returned as a negative value. |
| 1338 | */ |
| 1339 | long |
| 1340 | xfs_file_ioctl( |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1341 | struct file *filp, |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1342 | unsigned int cmd, |
Christoph Hellwig | 4d4be48 | 2008-12-09 04:47:33 -0500 | [diff] [blame] | 1343 | unsigned long p) |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1344 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1345 | struct inode *inode = file_inode(filp); |
Christoph Hellwig | 4d4be48 | 2008-12-09 04:47:33 -0500 | [diff] [blame] | 1346 | struct xfs_inode *ip = XFS_I(inode); |
| 1347 | struct xfs_mount *mp = ip->i_mount; |
| 1348 | void __user *arg = (void __user *)p; |
| 1349 | int ioflags = 0; |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1350 | int error; |
| 1351 | |
Christoph Hellwig | 4d4be48 | 2008-12-09 04:47:33 -0500 | [diff] [blame] | 1352 | if (filp->f_mode & FMODE_NOCMTIME) |
| 1353 | ioflags |= IO_INVIS; |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1354 | |
Christoph Hellwig | cca28fb | 2010-06-24 11:57:09 +1000 | [diff] [blame] | 1355 | trace_xfs_file_ioctl(ip); |
Christoph Hellwig | 4d4be48 | 2008-12-09 04:47:33 -0500 | [diff] [blame] | 1356 | |
| 1357 | switch (cmd) { |
Christoph Hellwig | a46db60 | 2011-01-07 13:02:04 +0000 | [diff] [blame] | 1358 | case FITRIM: |
| 1359 | return xfs_ioc_trim(mp, arg); |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1360 | case XFS_IOC_ALLOCSP: |
| 1361 | case XFS_IOC_FREESP: |
| 1362 | case XFS_IOC_RESVSP: |
| 1363 | case XFS_IOC_UNRESVSP: |
| 1364 | case XFS_IOC_ALLOCSP64: |
| 1365 | case XFS_IOC_FREESP64: |
| 1366 | case XFS_IOC_RESVSP64: |
Dave Chinner | 4472235 | 2010-08-24 12:02:11 +1000 | [diff] [blame] | 1367 | case XFS_IOC_UNRESVSP64: |
| 1368 | case XFS_IOC_ZERO_RANGE: { |
sandeen@sandeen.net | 743bb465 | 2008-11-25 21:20:06 -0600 | [diff] [blame] | 1369 | xfs_flock64_t bf; |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1370 | |
sandeen@sandeen.net | 743bb465 | 2008-11-25 21:20:06 -0600 | [diff] [blame] | 1371 | if (copy_from_user(&bf, arg, sizeof(bf))) |
| 1372 | return -XFS_ERROR(EFAULT); |
| 1373 | return xfs_ioc_space(ip, inode, filp, ioflags, cmd, &bf); |
| 1374 | } |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1375 | case XFS_IOC_DIOINFO: { |
| 1376 | struct dioattr da; |
| 1377 | xfs_buftarg_t *target = |
| 1378 | XFS_IS_REALTIME_INODE(ip) ? |
| 1379 | mp->m_rtdev_targp : mp->m_ddev_targp; |
| 1380 | |
| 1381 | da.d_mem = da.d_miniosz = 1 << target->bt_sshift; |
| 1382 | da.d_maxiosz = INT_MAX & ~(da.d_miniosz - 1); |
| 1383 | |
| 1384 | if (copy_to_user(arg, &da, sizeof(da))) |
| 1385 | return -XFS_ERROR(EFAULT); |
| 1386 | return 0; |
| 1387 | } |
| 1388 | |
| 1389 | case XFS_IOC_FSBULKSTAT_SINGLE: |
| 1390 | case XFS_IOC_FSBULKSTAT: |
| 1391 | case XFS_IOC_FSINUMBERS: |
| 1392 | return xfs_ioc_bulkstat(mp, cmd, arg); |
| 1393 | |
| 1394 | case XFS_IOC_FSGEOMETRY_V1: |
| 1395 | return xfs_ioc_fsgeometry_v1(mp, arg); |
| 1396 | |
| 1397 | case XFS_IOC_FSGEOMETRY: |
| 1398 | return xfs_ioc_fsgeometry(mp, arg); |
| 1399 | |
| 1400 | case XFS_IOC_GETVERSION: |
| 1401 | return put_user(inode->i_generation, (int __user *)arg); |
| 1402 | |
| 1403 | case XFS_IOC_FSGETXATTR: |
| 1404 | return xfs_ioc_fsgetxattr(ip, 0, arg); |
| 1405 | case XFS_IOC_FSGETXATTRA: |
| 1406 | return xfs_ioc_fsgetxattr(ip, 1, arg); |
Lachlan McIlroy | 3b2816b | 2008-04-18 12:43:35 +1000 | [diff] [blame] | 1407 | case XFS_IOC_FSSETXATTR: |
Lachlan McIlroy | 65e67f5 | 2008-04-18 12:59:45 +1000 | [diff] [blame] | 1408 | return xfs_ioc_fssetxattr(ip, filp, arg); |
| 1409 | case XFS_IOC_GETXFLAGS: |
| 1410 | return xfs_ioc_getxflags(ip, arg); |
| 1411 | case XFS_IOC_SETXFLAGS: |
| 1412 | return xfs_ioc_setxflags(ip, filp, arg); |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1413 | |
| 1414 | case XFS_IOC_FSSETDM: { |
| 1415 | struct fsdmidata dmi; |
| 1416 | |
| 1417 | if (copy_from_user(&dmi, arg, sizeof(dmi))) |
| 1418 | return -XFS_ERROR(EFAULT); |
| 1419 | |
Jan Kara | d9457dc | 2012-06-12 16:20:39 +0200 | [diff] [blame] | 1420 | error = mnt_want_write_file(filp); |
| 1421 | if (error) |
| 1422 | return error; |
| 1423 | |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1424 | error = xfs_set_dmattrs(ip, dmi.fsd_dmevmask, |
| 1425 | dmi.fsd_dmstate); |
Jan Kara | d9457dc | 2012-06-12 16:20:39 +0200 | [diff] [blame] | 1426 | mnt_drop_write_file(filp); |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1427 | return -error; |
| 1428 | } |
| 1429 | |
| 1430 | case XFS_IOC_GETBMAP: |
| 1431 | case XFS_IOC_GETBMAPA: |
| 1432 | return xfs_ioc_getbmap(ip, ioflags, cmd, arg); |
| 1433 | |
| 1434 | case XFS_IOC_GETBMAPX: |
| 1435 | return xfs_ioc_getbmapx(ip, arg); |
| 1436 | |
| 1437 | case XFS_IOC_FD_TO_HANDLE: |
| 1438 | case XFS_IOC_PATH_TO_HANDLE: |
sandeen@sandeen.net | 743bb465 | 2008-11-25 21:20:06 -0600 | [diff] [blame] | 1439 | case XFS_IOC_PATH_TO_FSHANDLE: { |
| 1440 | xfs_fsop_handlereq_t hreq; |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1441 | |
sandeen@sandeen.net | 743bb465 | 2008-11-25 21:20:06 -0600 | [diff] [blame] | 1442 | if (copy_from_user(&hreq, arg, sizeof(hreq))) |
| 1443 | return -XFS_ERROR(EFAULT); |
| 1444 | return xfs_find_handle(cmd, &hreq); |
| 1445 | } |
| 1446 | case XFS_IOC_OPEN_BY_HANDLE: { |
| 1447 | xfs_fsop_handlereq_t hreq; |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1448 | |
sandeen@sandeen.net | 743bb465 | 2008-11-25 21:20:06 -0600 | [diff] [blame] | 1449 | if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t))) |
| 1450 | return -XFS_ERROR(EFAULT); |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 1451 | return xfs_open_by_handle(filp, &hreq); |
sandeen@sandeen.net | 743bb465 | 2008-11-25 21:20:06 -0600 | [diff] [blame] | 1452 | } |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1453 | case XFS_IOC_FSSETDM_BY_HANDLE: |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 1454 | return xfs_fssetdm_by_handle(filp, arg); |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1455 | |
sandeen@sandeen.net | 743bb465 | 2008-11-25 21:20:06 -0600 | [diff] [blame] | 1456 | case XFS_IOC_READLINK_BY_HANDLE: { |
| 1457 | xfs_fsop_handlereq_t hreq; |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1458 | |
sandeen@sandeen.net | 743bb465 | 2008-11-25 21:20:06 -0600 | [diff] [blame] | 1459 | if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t))) |
| 1460 | return -XFS_ERROR(EFAULT); |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 1461 | return xfs_readlink_by_handle(filp, &hreq); |
sandeen@sandeen.net | 743bb465 | 2008-11-25 21:20:06 -0600 | [diff] [blame] | 1462 | } |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1463 | case XFS_IOC_ATTRLIST_BY_HANDLE: |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 1464 | return xfs_attrlist_by_handle(filp, arg); |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1465 | |
| 1466 | case XFS_IOC_ATTRMULTI_BY_HANDLE: |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 1467 | return xfs_attrmulti_by_handle(filp, arg); |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1468 | |
| 1469 | case XFS_IOC_SWAPEXT: { |
sandeen@sandeen.net | 743bb465 | 2008-11-25 21:20:06 -0600 | [diff] [blame] | 1470 | struct xfs_swapext sxp; |
| 1471 | |
| 1472 | if (copy_from_user(&sxp, arg, sizeof(xfs_swapext_t))) |
| 1473 | return -XFS_ERROR(EFAULT); |
Jan Kara | d9457dc | 2012-06-12 16:20:39 +0200 | [diff] [blame] | 1474 | error = mnt_want_write_file(filp); |
| 1475 | if (error) |
| 1476 | return error; |
sandeen@sandeen.net | 743bb465 | 2008-11-25 21:20:06 -0600 | [diff] [blame] | 1477 | error = xfs_swapext(&sxp); |
Jan Kara | d9457dc | 2012-06-12 16:20:39 +0200 | [diff] [blame] | 1478 | mnt_drop_write_file(filp); |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1479 | return -error; |
| 1480 | } |
| 1481 | |
| 1482 | case XFS_IOC_FSCOUNTS: { |
| 1483 | xfs_fsop_counts_t out; |
| 1484 | |
| 1485 | error = xfs_fs_counts(mp, &out); |
| 1486 | if (error) |
| 1487 | return -error; |
| 1488 | |
| 1489 | if (copy_to_user(arg, &out, sizeof(out))) |
| 1490 | return -XFS_ERROR(EFAULT); |
| 1491 | return 0; |
| 1492 | } |
| 1493 | |
| 1494 | case XFS_IOC_SET_RESBLKS: { |
| 1495 | xfs_fsop_resblks_t inout; |
| 1496 | __uint64_t in; |
| 1497 | |
| 1498 | if (!capable(CAP_SYS_ADMIN)) |
| 1499 | return -EPERM; |
| 1500 | |
Eric Sandeen | d5db0f9 | 2010-02-05 22:59:53 +0000 | [diff] [blame] | 1501 | if (mp->m_flags & XFS_MOUNT_RDONLY) |
| 1502 | return -XFS_ERROR(EROFS); |
| 1503 | |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1504 | if (copy_from_user(&inout, arg, sizeof(inout))) |
| 1505 | return -XFS_ERROR(EFAULT); |
| 1506 | |
Jan Kara | d9457dc | 2012-06-12 16:20:39 +0200 | [diff] [blame] | 1507 | error = mnt_want_write_file(filp); |
| 1508 | if (error) |
| 1509 | return error; |
| 1510 | |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1511 | /* input parameter is passed in resblks field of structure */ |
| 1512 | in = inout.resblks; |
| 1513 | error = xfs_reserve_blocks(mp, &in, &inout); |
Jan Kara | d9457dc | 2012-06-12 16:20:39 +0200 | [diff] [blame] | 1514 | mnt_drop_write_file(filp); |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1515 | if (error) |
| 1516 | return -error; |
| 1517 | |
| 1518 | if (copy_to_user(arg, &inout, sizeof(inout))) |
| 1519 | return -XFS_ERROR(EFAULT); |
| 1520 | return 0; |
| 1521 | } |
| 1522 | |
| 1523 | case XFS_IOC_GET_RESBLKS: { |
| 1524 | xfs_fsop_resblks_t out; |
| 1525 | |
| 1526 | if (!capable(CAP_SYS_ADMIN)) |
| 1527 | return -EPERM; |
| 1528 | |
| 1529 | error = xfs_reserve_blocks(mp, NULL, &out); |
| 1530 | if (error) |
| 1531 | return -error; |
| 1532 | |
| 1533 | if (copy_to_user(arg, &out, sizeof(out))) |
| 1534 | return -XFS_ERROR(EFAULT); |
| 1535 | |
| 1536 | return 0; |
| 1537 | } |
| 1538 | |
| 1539 | case XFS_IOC_FSGROWFSDATA: { |
| 1540 | xfs_growfs_data_t in; |
| 1541 | |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1542 | if (copy_from_user(&in, arg, sizeof(in))) |
| 1543 | return -XFS_ERROR(EFAULT); |
| 1544 | |
Jan Kara | d9457dc | 2012-06-12 16:20:39 +0200 | [diff] [blame] | 1545 | error = mnt_want_write_file(filp); |
| 1546 | if (error) |
| 1547 | return error; |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1548 | error = xfs_growfs_data(mp, &in); |
Jan Kara | d9457dc | 2012-06-12 16:20:39 +0200 | [diff] [blame] | 1549 | mnt_drop_write_file(filp); |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1550 | return -error; |
| 1551 | } |
| 1552 | |
| 1553 | case XFS_IOC_FSGROWFSLOG: { |
| 1554 | xfs_growfs_log_t in; |
| 1555 | |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1556 | if (copy_from_user(&in, arg, sizeof(in))) |
| 1557 | return -XFS_ERROR(EFAULT); |
| 1558 | |
Jan Kara | d9457dc | 2012-06-12 16:20:39 +0200 | [diff] [blame] | 1559 | error = mnt_want_write_file(filp); |
| 1560 | if (error) |
| 1561 | return error; |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1562 | error = xfs_growfs_log(mp, &in); |
Jan Kara | d9457dc | 2012-06-12 16:20:39 +0200 | [diff] [blame] | 1563 | mnt_drop_write_file(filp); |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1564 | return -error; |
| 1565 | } |
| 1566 | |
| 1567 | case XFS_IOC_FSGROWFSRT: { |
| 1568 | xfs_growfs_rt_t in; |
| 1569 | |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1570 | if (copy_from_user(&in, arg, sizeof(in))) |
| 1571 | return -XFS_ERROR(EFAULT); |
| 1572 | |
Jan Kara | d9457dc | 2012-06-12 16:20:39 +0200 | [diff] [blame] | 1573 | error = mnt_want_write_file(filp); |
| 1574 | if (error) |
| 1575 | return error; |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1576 | error = xfs_growfs_rt(mp, &in); |
Jan Kara | d9457dc | 2012-06-12 16:20:39 +0200 | [diff] [blame] | 1577 | mnt_drop_write_file(filp); |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1578 | return -error; |
| 1579 | } |
| 1580 | |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1581 | case XFS_IOC_GOINGDOWN: { |
| 1582 | __uint32_t in; |
| 1583 | |
| 1584 | if (!capable(CAP_SYS_ADMIN)) |
| 1585 | return -EPERM; |
| 1586 | |
| 1587 | if (get_user(in, (__uint32_t __user *)arg)) |
| 1588 | return -XFS_ERROR(EFAULT); |
| 1589 | |
| 1590 | error = xfs_fs_goingdown(mp, in); |
| 1591 | return -error; |
| 1592 | } |
| 1593 | |
| 1594 | case XFS_IOC_ERROR_INJECTION: { |
| 1595 | xfs_error_injection_t in; |
| 1596 | |
| 1597 | if (!capable(CAP_SYS_ADMIN)) |
| 1598 | return -EPERM; |
| 1599 | |
| 1600 | if (copy_from_user(&in, arg, sizeof(in))) |
| 1601 | return -XFS_ERROR(EFAULT); |
| 1602 | |
| 1603 | error = xfs_errortag_add(in.errtag, mp); |
| 1604 | return -error; |
| 1605 | } |
| 1606 | |
| 1607 | case XFS_IOC_ERROR_CLEARALL: |
| 1608 | if (!capable(CAP_SYS_ADMIN)) |
| 1609 | return -EPERM; |
| 1610 | |
| 1611 | error = xfs_errortag_clearall(mp, 1); |
| 1612 | return -error; |
| 1613 | |
Brian Foster | 8ca149d | 2012-11-07 12:21:12 -0500 | [diff] [blame] | 1614 | case XFS_IOC_FREE_EOFBLOCKS: { |
| 1615 | struct xfs_eofblocks eofb; |
| 1616 | |
| 1617 | if (copy_from_user(&eofb, arg, sizeof(eofb))) |
| 1618 | return -XFS_ERROR(EFAULT); |
| 1619 | |
| 1620 | if (eofb.eof_version != XFS_EOFBLOCKS_VERSION) |
| 1621 | return -XFS_ERROR(EINVAL); |
| 1622 | |
| 1623 | if (eofb.eof_flags & ~XFS_EOF_FLAGS_VALID) |
| 1624 | return -XFS_ERROR(EINVAL); |
| 1625 | |
Brian Foster | 3e3f9f5 | 2012-11-07 12:21:13 -0500 | [diff] [blame] | 1626 | if (memchr_inv(&eofb.pad32, 0, sizeof(eofb.pad32)) || |
| 1627 | memchr_inv(eofb.pad64, 0, sizeof(eofb.pad64))) |
Brian Foster | 8ca149d | 2012-11-07 12:21:12 -0500 | [diff] [blame] | 1628 | return -XFS_ERROR(EINVAL); |
| 1629 | |
| 1630 | error = xfs_icache_free_eofblocks(mp, &eofb); |
| 1631 | return -error; |
| 1632 | } |
| 1633 | |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1634 | default: |
| 1635 | return -ENOTTY; |
| 1636 | } |
| 1637 | } |