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" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 20 | #include "xfs_bit.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include "xfs_log.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 22 | #include "xfs_inum.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include "xfs_trans.h" |
| 24 | #include "xfs_sb.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 25 | #include "xfs_ag.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include "xfs_dir2.h" |
| 27 | #include "xfs_alloc.h" |
| 28 | #include "xfs_dmapi.h" |
| 29 | #include "xfs_mount.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include "xfs_bmap_btree.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 31 | #include "xfs_alloc_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include "xfs_ialloc_btree.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 33 | #include "xfs_attr_sf.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include "xfs_dir2_sf.h" |
| 35 | #include "xfs_dinode.h" |
| 36 | #include "xfs_inode.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 37 | #include "xfs_btree.h" |
| 38 | #include "xfs_ialloc.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include "xfs_rtalloc.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #include "xfs_itable.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 41 | #include "xfs_error.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #include "xfs_rw.h" |
| 43 | #include "xfs_acl.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | #include "xfs_attr.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 45 | #include "xfs_bmap.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | #include "xfs_buf_item.h" |
| 47 | #include "xfs_utils.h" |
| 48 | #include "xfs_dfrag.h" |
| 49 | #include "xfs_fsops.h" |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 50 | #include "xfs_vnodeops.h" |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 51 | #include "xfs_quota.h" |
| 52 | #include "xfs_inode_item.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
Randy Dunlap | 16f7e0f | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 54 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | #include <linux/dcache.h> |
| 56 | #include <linux/mount.h> |
| 57 | #include <linux/namei.h> |
| 58 | #include <linux/pagemap.h> |
| 59 | |
| 60 | /* |
| 61 | * xfs_find_handle maps from userspace xfs_fsop_handlereq structure to |
| 62 | * a file or fs handle. |
| 63 | * |
| 64 | * XFS_IOC_PATH_TO_FSHANDLE |
| 65 | * returns fs handle for a mount point or path within that mount point |
| 66 | * XFS_IOC_FD_TO_HANDLE |
| 67 | * returns full handle for a FD opened in user space |
| 68 | * XFS_IOC_PATH_TO_HANDLE |
| 69 | * returns full handle for a path |
| 70 | */ |
sandeen@sandeen.net | d5547f9 | 2008-11-25 21:20:08 -0600 | [diff] [blame] | 71 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | xfs_find_handle( |
| 73 | unsigned int cmd, |
sandeen@sandeen.net | 743bb46 | 2008-11-25 21:20:06 -0600 | [diff] [blame] | 74 | xfs_fsop_handlereq_t *hreq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | { |
| 76 | int hsize; |
| 77 | xfs_handle_t handle; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | struct inode *inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | memset((char *)&handle, 0, sizeof(handle)); |
| 81 | |
| 82 | switch (cmd) { |
| 83 | case XFS_IOC_PATH_TO_FSHANDLE: |
| 84 | case XFS_IOC_PATH_TO_HANDLE: { |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 85 | struct path path; |
sandeen@sandeen.net | 743bb46 | 2008-11-25 21:20:06 -0600 | [diff] [blame] | 86 | int error = user_lpath((const char __user *)hreq->path, &path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | if (error) |
| 88 | return error; |
| 89 | |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 90 | ASSERT(path.dentry); |
| 91 | ASSERT(path.dentry->d_inode); |
| 92 | inode = igrab(path.dentry->d_inode); |
| 93 | path_put(&path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | break; |
| 95 | } |
| 96 | |
| 97 | case XFS_IOC_FD_TO_HANDLE: { |
| 98 | struct file *file; |
| 99 | |
sandeen@sandeen.net | 743bb46 | 2008-11-25 21:20:06 -0600 | [diff] [blame] | 100 | file = fget(hreq->fd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | if (!file) |
| 102 | return -EBADF; |
| 103 | |
Josef "Jeff" Sipek | e678fb0 | 2006-12-08 02:36:49 -0800 | [diff] [blame] | 104 | ASSERT(file->f_path.dentry); |
| 105 | ASSERT(file->f_path.dentry->d_inode); |
| 106 | inode = igrab(file->f_path.dentry->d_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | fput(file); |
| 108 | break; |
| 109 | } |
| 110 | |
| 111 | default: |
| 112 | ASSERT(0); |
| 113 | return -XFS_ERROR(EINVAL); |
| 114 | } |
| 115 | |
| 116 | if (inode->i_sb->s_magic != XFS_SB_MAGIC) { |
| 117 | /* we're not in XFS anymore, Toto */ |
| 118 | iput(inode); |
| 119 | return -XFS_ERROR(EINVAL); |
| 120 | } |
| 121 | |
Christoph Hellwig | 0432dab | 2005-09-02 16:46:51 +1000 | [diff] [blame] | 122 | switch (inode->i_mode & S_IFMT) { |
| 123 | case S_IFREG: |
| 124 | case S_IFDIR: |
| 125 | case S_IFLNK: |
| 126 | break; |
| 127 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | iput(inode); |
| 129 | return -XFS_ERROR(EBADF); |
| 130 | } |
| 131 | |
| 132 | /* now we can grab the fsid */ |
Christoph Hellwig | 0ce4cfd | 2007-08-30 17:20:53 +1000 | [diff] [blame] | 133 | memcpy(&handle.ha_fsid, XFS_I(inode)->i_mount->m_fixedfsid, |
Christoph Hellwig | 2f6f7b3 | 2007-08-29 11:44:18 +1000 | [diff] [blame] | 134 | sizeof(xfs_fsid_t)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | hsize = sizeof(xfs_fsid_t); |
| 136 | |
| 137 | if (cmd != XFS_IOC_PATH_TO_FSHANDLE) { |
Christoph Hellwig | 6e7f75e | 2007-10-11 18:09:50 +1000 | [diff] [blame] | 138 | xfs_inode_t *ip = XFS_I(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | int lock_mode; |
| 140 | |
| 141 | /* need to get access to the xfs_inode to read the generation */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | lock_mode = xfs_ilock_map_shared(ip); |
| 143 | |
| 144 | /* fill in fid section of handle from inode */ |
Christoph Hellwig | c614391 | 2007-09-14 15:22:37 +1000 | [diff] [blame] | 145 | handle.ha_fid.fid_len = sizeof(xfs_fid_t) - |
| 146 | sizeof(handle.ha_fid.fid_len); |
| 147 | handle.ha_fid.fid_pad = 0; |
| 148 | handle.ha_fid.fid_gen = ip->i_d.di_gen; |
| 149 | handle.ha_fid.fid_ino = ip->i_ino; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | |
| 151 | xfs_iunlock_map_shared(ip, lock_mode); |
| 152 | |
| 153 | hsize = XFS_HSIZE(handle); |
| 154 | } |
| 155 | |
| 156 | /* now copy our handle into the user buffer & write out the size */ |
sandeen@sandeen.net | 743bb46 | 2008-11-25 21:20:06 -0600 | [diff] [blame] | 157 | if (copy_to_user(hreq->ohandle, &handle, hsize) || |
| 158 | copy_to_user(hreq->ohandlen, &hsize, sizeof(__s32))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | iput(inode); |
| 160 | return -XFS_ERROR(EFAULT); |
| 161 | } |
| 162 | |
| 163 | iput(inode); |
| 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | |
| 168 | /* |
Christoph Hellwig | 6e7f75e | 2007-10-11 18:09:50 +1000 | [diff] [blame] | 169 | * Convert userspace handle data into inode. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | * |
Christoph Hellwig | 6e7f75e | 2007-10-11 18:09:50 +1000 | [diff] [blame] | 171 | * We use the fact that all the fsop_handlereq ioctl calls have a data |
| 172 | * structure argument whose first component is always a xfs_fsop_handlereq_t, |
| 173 | * so we can pass that sub structure into this handy, shared routine. |
| 174 | * |
| 175 | * If no error, caller must always iput the returned inode. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | */ |
| 177 | STATIC int |
| 178 | xfs_vget_fsop_handlereq( |
| 179 | xfs_mount_t *mp, |
| 180 | struct inode *parinode, /* parent inode pointer */ |
| 181 | xfs_fsop_handlereq_t *hreq, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | struct inode **inode) |
| 183 | { |
| 184 | void __user *hanp; |
| 185 | size_t hlen; |
| 186 | xfs_fid_t *xfid; |
| 187 | xfs_handle_t *handlep; |
| 188 | xfs_handle_t handle; |
| 189 | xfs_inode_t *ip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | xfs_ino_t ino; |
| 191 | __u32 igen; |
| 192 | int error; |
| 193 | |
| 194 | /* |
| 195 | * Only allow handle opens under a directory. |
| 196 | */ |
| 197 | if (!S_ISDIR(parinode->i_mode)) |
| 198 | return XFS_ERROR(ENOTDIR); |
| 199 | |
| 200 | hanp = hreq->ihandle; |
| 201 | hlen = hreq->ihandlen; |
| 202 | handlep = &handle; |
| 203 | |
| 204 | if (hlen < sizeof(handlep->ha_fsid) || hlen > sizeof(*handlep)) |
| 205 | return XFS_ERROR(EINVAL); |
| 206 | if (copy_from_user(handlep, hanp, hlen)) |
| 207 | return XFS_ERROR(EFAULT); |
| 208 | if (hlen < sizeof(*handlep)) |
| 209 | memset(((char *)handlep) + hlen, 0, sizeof(*handlep) - hlen); |
| 210 | if (hlen > sizeof(handlep->ha_fsid)) { |
Christoph Hellwig | c614391 | 2007-09-14 15:22:37 +1000 | [diff] [blame] | 211 | if (handlep->ha_fid.fid_len != |
| 212 | (hlen - sizeof(handlep->ha_fsid) - |
| 213 | sizeof(handlep->ha_fid.fid_len)) || |
| 214 | handlep->ha_fid.fid_pad) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | return XFS_ERROR(EINVAL); |
| 216 | } |
| 217 | |
| 218 | /* |
| 219 | * Crack the handle, obtain the inode # & generation # |
| 220 | */ |
| 221 | xfid = (struct xfs_fid *)&handlep->ha_fid; |
Christoph Hellwig | c614391 | 2007-09-14 15:22:37 +1000 | [diff] [blame] | 222 | if (xfid->fid_len == sizeof(*xfid) - sizeof(xfid->fid_len)) { |
| 223 | ino = xfid->fid_ino; |
| 224 | igen = xfid->fid_gen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | } else { |
| 226 | return XFS_ERROR(EINVAL); |
| 227 | } |
| 228 | |
| 229 | /* |
Christoph Hellwig | 6e7f75e | 2007-10-11 18:09:50 +1000 | [diff] [blame] | 230 | * Get the XFS inode, building a Linux inode to go with it. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | */ |
| 232 | error = xfs_iget(mp, NULL, ino, 0, XFS_ILOCK_SHARED, &ip, 0); |
| 233 | if (error) |
| 234 | return error; |
| 235 | if (ip == NULL) |
| 236 | return XFS_ERROR(EIO); |
Christoph Hellwig | 6a7f422 | 2008-04-22 17:33:40 +1000 | [diff] [blame] | 237 | if (ip->i_d.di_gen != igen) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | xfs_iput_new(ip, XFS_ILOCK_SHARED); |
| 239 | return XFS_ERROR(ENOENT); |
| 240 | } |
| 241 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | xfs_iunlock(ip, XFS_ILOCK_SHARED); |
| 243 | |
David Chinner | e4f7529 | 2008-08-13 16:00:45 +1000 | [diff] [blame] | 244 | *inode = VFS_I(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | return 0; |
| 246 | } |
| 247 | |
sandeen@sandeen.net | d5547f9 | 2008-11-25 21:20:08 -0600 | [diff] [blame] | 248 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | xfs_open_by_handle( |
| 250 | xfs_mount_t *mp, |
sandeen@sandeen.net | 743bb46 | 2008-11-25 21:20:06 -0600 | [diff] [blame] | 251 | xfs_fsop_handlereq_t *hreq, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | struct file *parfilp, |
| 253 | struct inode *parinode) |
| 254 | { |
David Howells | 745ca24 | 2008-11-14 10:39:22 +1100 | [diff] [blame] | 255 | const struct cred *cred = current_cred(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | int error; |
| 257 | int new_fd; |
| 258 | int permflag; |
| 259 | struct file *filp; |
| 260 | struct inode *inode; |
| 261 | struct dentry *dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | |
| 263 | if (!capable(CAP_SYS_ADMIN)) |
| 264 | return -XFS_ERROR(EPERM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | |
sandeen@sandeen.net | 743bb46 | 2008-11-25 21:20:06 -0600 | [diff] [blame] | 266 | error = xfs_vget_fsop_handlereq(mp, parinode, hreq, &inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | if (error) |
| 268 | return -error; |
| 269 | |
| 270 | /* Restrict xfs_open_by_handle to directories & regular files. */ |
| 271 | if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode))) { |
| 272 | iput(inode); |
| 273 | return -XFS_ERROR(EINVAL); |
| 274 | } |
| 275 | |
| 276 | #if BITS_PER_LONG != 32 |
sandeen@sandeen.net | 743bb46 | 2008-11-25 21:20:06 -0600 | [diff] [blame] | 277 | hreq->oflags |= O_LARGEFILE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | #endif |
| 279 | /* Put open permission in namei format. */ |
sandeen@sandeen.net | 743bb46 | 2008-11-25 21:20:06 -0600 | [diff] [blame] | 280 | permflag = hreq->oflags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | if ((permflag+1) & O_ACCMODE) |
| 282 | permflag++; |
| 283 | if (permflag & O_TRUNC) |
| 284 | permflag |= 2; |
| 285 | |
| 286 | if ((!(permflag & O_APPEND) || (permflag & O_TRUNC)) && |
| 287 | (permflag & FMODE_WRITE) && IS_APPEND(inode)) { |
| 288 | iput(inode); |
| 289 | return -XFS_ERROR(EPERM); |
| 290 | } |
| 291 | |
| 292 | if ((permflag & FMODE_WRITE) && IS_IMMUTABLE(inode)) { |
| 293 | iput(inode); |
| 294 | return -XFS_ERROR(EACCES); |
| 295 | } |
| 296 | |
| 297 | /* Can't write directories. */ |
| 298 | if ( S_ISDIR(inode->i_mode) && (permflag & FMODE_WRITE)) { |
| 299 | iput(inode); |
| 300 | return -XFS_ERROR(EISDIR); |
| 301 | } |
| 302 | |
| 303 | if ((new_fd = get_unused_fd()) < 0) { |
| 304 | iput(inode); |
| 305 | return new_fd; |
| 306 | } |
| 307 | |
Christoph Hellwig | 4400372 | 2008-08-11 15:49:04 +0200 | [diff] [blame] | 308 | dentry = d_obtain_alias(inode); |
| 309 | if (IS_ERR(dentry)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | put_unused_fd(new_fd); |
Christoph Hellwig | 4400372 | 2008-08-11 15:49:04 +0200 | [diff] [blame] | 311 | return PTR_ERR(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | /* Ensure umount returns EBUSY on umounts while this file is open. */ |
Josef "Jeff" Sipek | e678fb0 | 2006-12-08 02:36:49 -0800 | [diff] [blame] | 315 | mntget(parfilp->f_path.mnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | |
| 317 | /* Create file pointer. */ |
Lachlan McIlroy | 0a8c539 | 2008-12-29 16:47:18 +1100 | [diff] [blame] | 318 | filp = dentry_open(dentry, parfilp->f_path.mnt, hreq->oflags, cred); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | if (IS_ERR(filp)) { |
| 320 | put_unused_fd(new_fd); |
| 321 | return -XFS_ERROR(-PTR_ERR(filp)); |
| 322 | } |
Christoph Hellwig | 4d4be48 | 2008-12-09 04:47:33 -0500 | [diff] [blame] | 323 | |
Vlad Apostolov | 2e2e7bb | 2006-11-11 18:04:47 +1100 | [diff] [blame] | 324 | if (inode->i_mode & S_IFREG) { |
| 325 | /* invisible operation should not change atime */ |
| 326 | filp->f_flags |= O_NOATIME; |
Christoph Hellwig | 4d4be48 | 2008-12-09 04:47:33 -0500 | [diff] [blame] | 327 | filp->f_mode |= FMODE_NOCMTIME; |
Vlad Apostolov | 2e2e7bb | 2006-11-11 18:04:47 +1100 | [diff] [blame] | 328 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | |
| 330 | fd_install(new_fd, filp); |
| 331 | return new_fd; |
| 332 | } |
| 333 | |
Christoph Hellwig | 804c83c | 2007-08-28 13:59:03 +1000 | [diff] [blame] | 334 | /* |
| 335 | * This is a copy from fs/namei.c:vfs_readlink(), except for removing it's |
| 336 | * unused first argument. |
| 337 | */ |
| 338 | STATIC int |
| 339 | do_readlink( |
| 340 | char __user *buffer, |
| 341 | int buflen, |
| 342 | const char *link) |
| 343 | { |
| 344 | int len; |
| 345 | |
| 346 | len = PTR_ERR(link); |
| 347 | if (IS_ERR(link)) |
| 348 | goto out; |
| 349 | |
| 350 | len = strlen(link); |
| 351 | if (len > (unsigned) buflen) |
| 352 | len = buflen; |
| 353 | if (copy_to_user(buffer, link, len)) |
| 354 | len = -EFAULT; |
| 355 | out: |
| 356 | return len; |
| 357 | } |
| 358 | |
| 359 | |
sandeen@sandeen.net | d5547f9 | 2008-11-25 21:20:08 -0600 | [diff] [blame] | 360 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | xfs_readlink_by_handle( |
| 362 | xfs_mount_t *mp, |
sandeen@sandeen.net | 743bb46 | 2008-11-25 21:20:06 -0600 | [diff] [blame] | 363 | xfs_fsop_handlereq_t *hreq, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | struct inode *parinode) |
| 365 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | struct inode *inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | __u32 olen; |
Christoph Hellwig | 804c83c | 2007-08-28 13:59:03 +1000 | [diff] [blame] | 368 | void *link; |
| 369 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | |
| 371 | if (!capable(CAP_SYS_ADMIN)) |
| 372 | return -XFS_ERROR(EPERM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | |
sandeen@sandeen.net | 743bb46 | 2008-11-25 21:20:06 -0600 | [diff] [blame] | 374 | error = xfs_vget_fsop_handlereq(mp, parinode, hreq, &inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | if (error) |
| 376 | return -error; |
| 377 | |
| 378 | /* Restrict this handle operation to symlinks only. */ |
Christoph Hellwig | 0432dab | 2005-09-02 16:46:51 +1000 | [diff] [blame] | 379 | if (!S_ISLNK(inode->i_mode)) { |
Christoph Hellwig | 804c83c | 2007-08-28 13:59:03 +1000 | [diff] [blame] | 380 | error = -XFS_ERROR(EINVAL); |
| 381 | goto out_iput; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | } |
| 383 | |
sandeen@sandeen.net | 743bb46 | 2008-11-25 21:20:06 -0600 | [diff] [blame] | 384 | if (copy_from_user(&olen, hreq->ohandlen, sizeof(__u32))) { |
Christoph Hellwig | 804c83c | 2007-08-28 13:59:03 +1000 | [diff] [blame] | 385 | error = -XFS_ERROR(EFAULT); |
| 386 | goto out_iput; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | |
Christoph Hellwig | 804c83c | 2007-08-28 13:59:03 +1000 | [diff] [blame] | 389 | link = kmalloc(MAXPATHLEN+1, GFP_KERNEL); |
| 390 | if (!link) |
| 391 | goto out_iput; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 393 | error = -xfs_readlink(XFS_I(inode), link); |
Nathan Scott | 67fcaa7 | 2006-06-09 17:00:52 +1000 | [diff] [blame] | 394 | if (error) |
Christoph Hellwig | 804c83c | 2007-08-28 13:59:03 +1000 | [diff] [blame] | 395 | goto out_kfree; |
sandeen@sandeen.net | 743bb46 | 2008-11-25 21:20:06 -0600 | [diff] [blame] | 396 | error = do_readlink(hreq->ohandle, olen, link); |
Christoph Hellwig | 804c83c | 2007-08-28 13:59:03 +1000 | [diff] [blame] | 397 | if (error) |
| 398 | goto out_kfree; |
Nathan Scott | 67fcaa7 | 2006-06-09 17:00:52 +1000 | [diff] [blame] | 399 | |
Christoph Hellwig | 804c83c | 2007-08-28 13:59:03 +1000 | [diff] [blame] | 400 | out_kfree: |
| 401 | kfree(link); |
| 402 | out_iput: |
| 403 | iput(inode); |
| 404 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | STATIC int |
| 408 | xfs_fssetdm_by_handle( |
| 409 | xfs_mount_t *mp, |
| 410 | void __user *arg, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | struct inode *parinode) |
| 412 | { |
| 413 | int error; |
| 414 | struct fsdmidata fsd; |
| 415 | xfs_fsop_setdm_handlereq_t dmhreq; |
| 416 | struct inode *inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | |
| 418 | if (!capable(CAP_MKNOD)) |
| 419 | return -XFS_ERROR(EPERM); |
| 420 | if (copy_from_user(&dmhreq, arg, sizeof(xfs_fsop_setdm_handlereq_t))) |
| 421 | return -XFS_ERROR(EFAULT); |
| 422 | |
Christoph Hellwig | 6e7f75e | 2007-10-11 18:09:50 +1000 | [diff] [blame] | 423 | error = xfs_vget_fsop_handlereq(mp, parinode, &dmhreq.hreq, &inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | if (error) |
| 425 | return -error; |
| 426 | |
| 427 | if (IS_IMMUTABLE(inode) || IS_APPEND(inode)) { |
Christoph Hellwig | 6e7f75e | 2007-10-11 18:09:50 +1000 | [diff] [blame] | 428 | error = -XFS_ERROR(EPERM); |
| 429 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | if (copy_from_user(&fsd, dmhreq.data, sizeof(fsd))) { |
Christoph Hellwig | 6e7f75e | 2007-10-11 18:09:50 +1000 | [diff] [blame] | 433 | error = -XFS_ERROR(EFAULT); |
| 434 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | } |
| 436 | |
Christoph Hellwig | 6e7f75e | 2007-10-11 18:09:50 +1000 | [diff] [blame] | 437 | error = -xfs_set_dmattrs(XFS_I(inode), fsd.fsd_dmevmask, |
| 438 | fsd.fsd_dmstate); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | |
Christoph Hellwig | 6e7f75e | 2007-10-11 18:09:50 +1000 | [diff] [blame] | 440 | out: |
| 441 | iput(inode); |
| 442 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | STATIC int |
| 446 | xfs_attrlist_by_handle( |
| 447 | xfs_mount_t *mp, |
| 448 | void __user *arg, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | struct inode *parinode) |
| 450 | { |
| 451 | int error; |
| 452 | attrlist_cursor_kern_t *cursor; |
| 453 | xfs_fsop_attrlist_handlereq_t al_hreq; |
| 454 | struct inode *inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | char *kbuf; |
| 456 | |
| 457 | if (!capable(CAP_SYS_ADMIN)) |
| 458 | return -XFS_ERROR(EPERM); |
| 459 | if (copy_from_user(&al_hreq, arg, sizeof(xfs_fsop_attrlist_handlereq_t))) |
| 460 | return -XFS_ERROR(EFAULT); |
| 461 | if (al_hreq.buflen > XATTR_LIST_MAX) |
| 462 | return -XFS_ERROR(EINVAL); |
| 463 | |
Christoph Hellwig | 90ad58a | 2008-06-27 13:32:19 +1000 | [diff] [blame] | 464 | /* |
| 465 | * Reject flags, only allow namespaces. |
| 466 | */ |
| 467 | if (al_hreq.flags & ~(ATTR_ROOT | ATTR_SECURE)) |
| 468 | return -XFS_ERROR(EINVAL); |
| 469 | |
Christoph Hellwig | 6e7f75e | 2007-10-11 18:09:50 +1000 | [diff] [blame] | 470 | error = xfs_vget_fsop_handlereq(mp, parinode, &al_hreq.hreq, &inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | if (error) |
| 472 | goto out; |
| 473 | |
| 474 | kbuf = kmalloc(al_hreq.buflen, GFP_KERNEL); |
| 475 | if (!kbuf) |
| 476 | goto out_vn_rele; |
| 477 | |
| 478 | cursor = (attrlist_cursor_kern_t *)&al_hreq.pos; |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 479 | error = xfs_attr_list(XFS_I(inode), kbuf, al_hreq.buflen, |
| 480 | al_hreq.flags, cursor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | if (error) |
| 482 | goto out_kfree; |
| 483 | |
| 484 | if (copy_to_user(al_hreq.buffer, kbuf, al_hreq.buflen)) |
| 485 | error = -EFAULT; |
| 486 | |
| 487 | out_kfree: |
| 488 | kfree(kbuf); |
| 489 | out_vn_rele: |
Christoph Hellwig | 6e7f75e | 2007-10-11 18:09:50 +1000 | [diff] [blame] | 490 | iput(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | out: |
| 492 | return -error; |
| 493 | } |
| 494 | |
sandeen@sandeen.net | 2875097 | 2008-11-25 21:20:15 -0600 | [diff] [blame] | 495 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | xfs_attrmulti_attr_get( |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 497 | struct inode *inode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | char *name, |
| 499 | char __user *ubuf, |
| 500 | __uint32_t *len, |
| 501 | __uint32_t flags) |
| 502 | { |
| 503 | char *kbuf; |
| 504 | int error = EFAULT; |
Barry Naujok | e8b0eba | 2008-04-22 17:34:31 +1000 | [diff] [blame] | 505 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | if (*len > XATTR_SIZE_MAX) |
| 507 | return EINVAL; |
| 508 | kbuf = kmalloc(*len, GFP_KERNEL); |
| 509 | if (!kbuf) |
| 510 | return ENOMEM; |
| 511 | |
Barry Naujok | e8b0eba | 2008-04-22 17:34:31 +1000 | [diff] [blame] | 512 | error = xfs_attr_get(XFS_I(inode), name, kbuf, (int *)len, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | if (error) |
| 514 | goto out_kfree; |
| 515 | |
| 516 | if (copy_to_user(ubuf, kbuf, *len)) |
| 517 | error = EFAULT; |
| 518 | |
| 519 | out_kfree: |
| 520 | kfree(kbuf); |
| 521 | return error; |
| 522 | } |
| 523 | |
sandeen@sandeen.net | 2875097 | 2008-11-25 21:20:15 -0600 | [diff] [blame] | 524 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | xfs_attrmulti_attr_set( |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 526 | struct inode *inode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | char *name, |
| 528 | const char __user *ubuf, |
| 529 | __uint32_t len, |
| 530 | __uint32_t flags) |
| 531 | { |
| 532 | char *kbuf; |
| 533 | int error = EFAULT; |
| 534 | |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 535 | if (IS_IMMUTABLE(inode) || IS_APPEND(inode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | return EPERM; |
| 537 | if (len > XATTR_SIZE_MAX) |
| 538 | return EINVAL; |
| 539 | |
| 540 | kbuf = kmalloc(len, GFP_KERNEL); |
| 541 | if (!kbuf) |
| 542 | return ENOMEM; |
| 543 | |
| 544 | if (copy_from_user(kbuf, ubuf, len)) |
| 545 | goto out_kfree; |
Barry Naujok | e8b0eba | 2008-04-22 17:34:31 +1000 | [diff] [blame] | 546 | |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 547 | error = xfs_attr_set(XFS_I(inode), name, kbuf, len, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | |
| 549 | out_kfree: |
| 550 | kfree(kbuf); |
| 551 | return error; |
| 552 | } |
| 553 | |
sandeen@sandeen.net | 2875097 | 2008-11-25 21:20:15 -0600 | [diff] [blame] | 554 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | xfs_attrmulti_attr_remove( |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 556 | struct inode *inode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | char *name, |
| 558 | __uint32_t flags) |
| 559 | { |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 560 | if (IS_IMMUTABLE(inode) || IS_APPEND(inode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | return EPERM; |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 562 | return xfs_attr_remove(XFS_I(inode), name, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | STATIC int |
| 566 | xfs_attrmulti_by_handle( |
| 567 | xfs_mount_t *mp, |
| 568 | void __user *arg, |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 569 | struct file *parfilp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | struct inode *parinode) |
| 571 | { |
| 572 | int error; |
| 573 | xfs_attr_multiop_t *ops; |
| 574 | xfs_fsop_attrmulti_handlereq_t am_hreq; |
| 575 | struct inode *inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | unsigned int i, size; |
| 577 | char *attr_name; |
| 578 | |
| 579 | if (!capable(CAP_SYS_ADMIN)) |
| 580 | return -XFS_ERROR(EPERM); |
| 581 | if (copy_from_user(&am_hreq, arg, sizeof(xfs_fsop_attrmulti_handlereq_t))) |
| 582 | return -XFS_ERROR(EFAULT); |
| 583 | |
Christoph Hellwig | 6e7f75e | 2007-10-11 18:09:50 +1000 | [diff] [blame] | 584 | error = xfs_vget_fsop_handlereq(mp, parinode, &am_hreq.hreq, &inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | if (error) |
| 586 | goto out; |
| 587 | |
| 588 | error = E2BIG; |
Christoph Hellwig | e182f57 | 2008-06-27 13:32:31 +1000 | [diff] [blame] | 589 | size = am_hreq.opcount * sizeof(xfs_attr_multiop_t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | if (!size || size > 16 * PAGE_SIZE) |
| 591 | goto out_vn_rele; |
| 592 | |
| 593 | error = ENOMEM; |
| 594 | ops = kmalloc(size, GFP_KERNEL); |
| 595 | if (!ops) |
| 596 | goto out_vn_rele; |
| 597 | |
| 598 | error = EFAULT; |
| 599 | if (copy_from_user(ops, am_hreq.ops, size)) |
| 600 | goto out_kfree_ops; |
| 601 | |
| 602 | attr_name = kmalloc(MAXNAMELEN, GFP_KERNEL); |
| 603 | if (!attr_name) |
| 604 | goto out_kfree_ops; |
| 605 | |
| 606 | |
| 607 | error = 0; |
| 608 | for (i = 0; i < am_hreq.opcount; i++) { |
| 609 | ops[i].am_error = strncpy_from_user(attr_name, |
| 610 | ops[i].am_attrname, MAXNAMELEN); |
| 611 | if (ops[i].am_error == 0 || ops[i].am_error == MAXNAMELEN) |
| 612 | error = -ERANGE; |
| 613 | if (ops[i].am_error < 0) |
| 614 | break; |
| 615 | |
| 616 | switch (ops[i].am_opcode) { |
| 617 | case ATTR_OP_GET: |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 618 | ops[i].am_error = xfs_attrmulti_attr_get(inode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | attr_name, ops[i].am_attrvalue, |
| 620 | &ops[i].am_length, ops[i].am_flags); |
| 621 | break; |
| 622 | case ATTR_OP_SET: |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 623 | ops[i].am_error = mnt_want_write(parfilp->f_path.mnt); |
| 624 | if (ops[i].am_error) |
| 625 | break; |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 626 | ops[i].am_error = xfs_attrmulti_attr_set(inode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | attr_name, ops[i].am_attrvalue, |
| 628 | ops[i].am_length, ops[i].am_flags); |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 629 | mnt_drop_write(parfilp->f_path.mnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | break; |
| 631 | case ATTR_OP_REMOVE: |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 632 | ops[i].am_error = mnt_want_write(parfilp->f_path.mnt); |
| 633 | if (ops[i].am_error) |
| 634 | break; |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 635 | ops[i].am_error = xfs_attrmulti_attr_remove(inode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | attr_name, ops[i].am_flags); |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 637 | mnt_drop_write(parfilp->f_path.mnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | break; |
| 639 | default: |
| 640 | ops[i].am_error = EINVAL; |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | if (copy_to_user(am_hreq.ops, ops, size)) |
| 645 | error = XFS_ERROR(EFAULT); |
| 646 | |
| 647 | kfree(attr_name); |
| 648 | out_kfree_ops: |
| 649 | kfree(ops); |
| 650 | out_vn_rele: |
Christoph Hellwig | 6e7f75e | 2007-10-11 18:09:50 +1000 | [diff] [blame] | 651 | iput(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | out: |
| 653 | return -error; |
| 654 | } |
| 655 | |
sandeen@sandeen.net | d5547f9 | 2008-11-25 21:20:08 -0600 | [diff] [blame] | 656 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | xfs_ioc_space( |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 658 | struct xfs_inode *ip, |
Alexey Dobriyan | f37ea14 | 2006-09-28 10:52:04 +1000 | [diff] [blame] | 659 | struct inode *inode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | struct file *filp, |
| 661 | int ioflags, |
| 662 | unsigned int cmd, |
sandeen@sandeen.net | 743bb46 | 2008-11-25 21:20:06 -0600 | [diff] [blame] | 663 | xfs_flock64_t *bf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | int attr_flags = 0; |
| 666 | int error; |
| 667 | |
sandeen@sandeen.net | 743bb46 | 2008-11-25 21:20:06 -0600 | [diff] [blame] | 668 | /* |
| 669 | * Only allow the sys admin to reserve space unless |
| 670 | * unwritten extents are enabled. |
| 671 | */ |
| 672 | if (!xfs_sb_version_hasextflgbit(&ip->i_mount->m_sb) && |
| 673 | !capable(CAP_SYS_ADMIN)) |
| 674 | return -XFS_ERROR(EPERM); |
| 675 | |
Alexey Dobriyan | f37ea14 | 2006-09-28 10:52:04 +1000 | [diff] [blame] | 676 | if (inode->i_flags & (S_IMMUTABLE|S_APPEND)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | return -XFS_ERROR(EPERM); |
| 678 | |
Eric Sandeen | ad4a8ac | 2005-09-02 16:41:16 +1000 | [diff] [blame] | 679 | if (!(filp->f_mode & FMODE_WRITE)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | return -XFS_ERROR(EBADF); |
| 681 | |
Alexey Dobriyan | f37ea14 | 2006-09-28 10:52:04 +1000 | [diff] [blame] | 682 | if (!S_ISREG(inode->i_mode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | return -XFS_ERROR(EINVAL); |
| 684 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | if (filp->f_flags & (O_NDELAY|O_NONBLOCK)) |
Christoph Hellwig | 0f285c8 | 2008-07-18 17:13:28 +1000 | [diff] [blame] | 686 | attr_flags |= XFS_ATTR_NONBLOCK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | if (ioflags & IO_INVIS) |
Christoph Hellwig | 0f285c8 | 2008-07-18 17:13:28 +1000 | [diff] [blame] | 688 | attr_flags |= XFS_ATTR_DMI; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | |
sandeen@sandeen.net | 743bb46 | 2008-11-25 21:20:06 -0600 | [diff] [blame] | 690 | error = xfs_change_file_space(ip, cmd, bf, filp->f_pos, attr_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | return -error; |
| 692 | } |
| 693 | |
| 694 | STATIC int |
| 695 | xfs_ioc_bulkstat( |
| 696 | xfs_mount_t *mp, |
| 697 | unsigned int cmd, |
| 698 | void __user *arg) |
| 699 | { |
| 700 | xfs_fsop_bulkreq_t bulkreq; |
| 701 | int count; /* # of records returned */ |
| 702 | xfs_ino_t inlast; /* last inode number */ |
| 703 | int done; |
| 704 | int error; |
| 705 | |
| 706 | /* done = 1 if there are more stats to get and if bulkstat */ |
| 707 | /* should be called again (unused here, but used in dmapi) */ |
| 708 | |
| 709 | if (!capable(CAP_SYS_ADMIN)) |
| 710 | return -EPERM; |
| 711 | |
| 712 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 713 | return -XFS_ERROR(EIO); |
| 714 | |
| 715 | if (copy_from_user(&bulkreq, arg, sizeof(xfs_fsop_bulkreq_t))) |
| 716 | return -XFS_ERROR(EFAULT); |
| 717 | |
| 718 | if (copy_from_user(&inlast, bulkreq.lastip, sizeof(__s64))) |
| 719 | return -XFS_ERROR(EFAULT); |
| 720 | |
| 721 | if ((count = bulkreq.icount) <= 0) |
| 722 | return -XFS_ERROR(EINVAL); |
| 723 | |
Lachlan McIlroy | cd57e59 | 2007-11-23 16:30:32 +1100 | [diff] [blame] | 724 | if (bulkreq.ubuffer == NULL) |
| 725 | return -XFS_ERROR(EINVAL); |
| 726 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | if (cmd == XFS_IOC_FSINUMBERS) |
| 728 | error = xfs_inumbers(mp, &inlast, &count, |
Michal Marek | faa63e9 | 2007-07-11 11:10:19 +1000 | [diff] [blame] | 729 | bulkreq.ubuffer, xfs_inumbers_fmt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | else if (cmd == XFS_IOC_FSBULKSTAT_SINGLE) |
| 731 | error = xfs_bulkstat_single(mp, &inlast, |
| 732 | bulkreq.ubuffer, &done); |
Lachlan McIlroy | cd57e59 | 2007-11-23 16:30:32 +1100 | [diff] [blame] | 733 | else /* XFS_IOC_FSBULKSTAT */ |
| 734 | error = xfs_bulkstat(mp, &inlast, &count, |
| 735 | (bulkstat_one_pf)xfs_bulkstat_one, NULL, |
| 736 | sizeof(xfs_bstat_t), bulkreq.ubuffer, |
| 737 | BULKSTAT_FG_QUICK, &done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | |
| 739 | if (error) |
| 740 | return -error; |
| 741 | |
| 742 | if (bulkreq.ocount != NULL) { |
| 743 | if (copy_to_user(bulkreq.lastip, &inlast, |
| 744 | sizeof(xfs_ino_t))) |
| 745 | return -XFS_ERROR(EFAULT); |
| 746 | |
| 747 | if (copy_to_user(bulkreq.ocount, &count, sizeof(count))) |
| 748 | return -XFS_ERROR(EFAULT); |
| 749 | } |
| 750 | |
| 751 | return 0; |
| 752 | } |
| 753 | |
| 754 | STATIC int |
| 755 | xfs_ioc_fsgeometry_v1( |
| 756 | xfs_mount_t *mp, |
| 757 | void __user *arg) |
| 758 | { |
| 759 | xfs_fsop_geom_v1_t fsgeo; |
| 760 | int error; |
| 761 | |
| 762 | error = xfs_fs_geometry(mp, (xfs_fsop_geom_t *)&fsgeo, 3); |
| 763 | if (error) |
| 764 | return -error; |
| 765 | |
| 766 | if (copy_to_user(arg, &fsgeo, sizeof(fsgeo))) |
| 767 | return -XFS_ERROR(EFAULT); |
| 768 | return 0; |
| 769 | } |
| 770 | |
| 771 | STATIC int |
| 772 | xfs_ioc_fsgeometry( |
| 773 | xfs_mount_t *mp, |
| 774 | void __user *arg) |
| 775 | { |
| 776 | xfs_fsop_geom_t fsgeo; |
| 777 | int error; |
| 778 | |
| 779 | error = xfs_fs_geometry(mp, &fsgeo, 4); |
| 780 | if (error) |
| 781 | return -error; |
| 782 | |
| 783 | if (copy_to_user(arg, &fsgeo, sizeof(fsgeo))) |
| 784 | return -XFS_ERROR(EFAULT); |
| 785 | return 0; |
| 786 | } |
| 787 | |
| 788 | /* |
| 789 | * Linux extended inode flags interface. |
| 790 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | |
| 792 | STATIC unsigned int |
| 793 | xfs_merge_ioc_xflags( |
| 794 | unsigned int flags, |
| 795 | unsigned int start) |
| 796 | { |
| 797 | unsigned int xflags = start; |
| 798 | |
Eric Sandeen | 39058a0 | 2007-02-10 18:37:10 +1100 | [diff] [blame] | 799 | if (flags & FS_IMMUTABLE_FL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | xflags |= XFS_XFLAG_IMMUTABLE; |
| 801 | else |
| 802 | xflags &= ~XFS_XFLAG_IMMUTABLE; |
Eric Sandeen | 39058a0 | 2007-02-10 18:37:10 +1100 | [diff] [blame] | 803 | if (flags & FS_APPEND_FL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | xflags |= XFS_XFLAG_APPEND; |
| 805 | else |
| 806 | xflags &= ~XFS_XFLAG_APPEND; |
Eric Sandeen | 39058a0 | 2007-02-10 18:37:10 +1100 | [diff] [blame] | 807 | if (flags & FS_SYNC_FL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | xflags |= XFS_XFLAG_SYNC; |
| 809 | else |
| 810 | xflags &= ~XFS_XFLAG_SYNC; |
Eric Sandeen | 39058a0 | 2007-02-10 18:37:10 +1100 | [diff] [blame] | 811 | if (flags & FS_NOATIME_FL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | xflags |= XFS_XFLAG_NOATIME; |
| 813 | else |
| 814 | xflags &= ~XFS_XFLAG_NOATIME; |
Eric Sandeen | 39058a0 | 2007-02-10 18:37:10 +1100 | [diff] [blame] | 815 | if (flags & FS_NODUMP_FL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 816 | xflags |= XFS_XFLAG_NODUMP; |
| 817 | else |
| 818 | xflags &= ~XFS_XFLAG_NODUMP; |
| 819 | |
| 820 | return xflags; |
| 821 | } |
| 822 | |
| 823 | STATIC unsigned int |
| 824 | xfs_di2lxflags( |
| 825 | __uint16_t di_flags) |
| 826 | { |
| 827 | unsigned int flags = 0; |
| 828 | |
| 829 | if (di_flags & XFS_DIFLAG_IMMUTABLE) |
Eric Sandeen | 39058a0 | 2007-02-10 18:37:10 +1100 | [diff] [blame] | 830 | flags |= FS_IMMUTABLE_FL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | if (di_flags & XFS_DIFLAG_APPEND) |
Eric Sandeen | 39058a0 | 2007-02-10 18:37:10 +1100 | [diff] [blame] | 832 | flags |= FS_APPEND_FL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 833 | if (di_flags & XFS_DIFLAG_SYNC) |
Eric Sandeen | 39058a0 | 2007-02-10 18:37:10 +1100 | [diff] [blame] | 834 | flags |= FS_SYNC_FL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | if (di_flags & XFS_DIFLAG_NOATIME) |
Eric Sandeen | 39058a0 | 2007-02-10 18:37:10 +1100 | [diff] [blame] | 836 | flags |= FS_NOATIME_FL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | if (di_flags & XFS_DIFLAG_NODUMP) |
Eric Sandeen | 39058a0 | 2007-02-10 18:37:10 +1100 | [diff] [blame] | 838 | flags |= FS_NODUMP_FL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | return flags; |
| 840 | } |
| 841 | |
| 842 | STATIC int |
Christoph Hellwig | c83bfab | 2007-10-11 17:47:00 +1000 | [diff] [blame] | 843 | xfs_ioc_fsgetxattr( |
| 844 | xfs_inode_t *ip, |
| 845 | int attr, |
| 846 | void __user *arg) |
| 847 | { |
| 848 | struct fsxattr fa; |
| 849 | |
| 850 | xfs_ilock(ip, XFS_ILOCK_SHARED); |
| 851 | fa.fsx_xflags = xfs_ip2xflags(ip); |
| 852 | fa.fsx_extsize = ip->i_d.di_extsize << ip->i_mount->m_sb.sb_blocklog; |
| 853 | fa.fsx_projid = ip->i_d.di_projid; |
| 854 | |
| 855 | if (attr) { |
| 856 | if (ip->i_afp) { |
| 857 | if (ip->i_afp->if_flags & XFS_IFEXTENTS) |
| 858 | fa.fsx_nextents = ip->i_afp->if_bytes / |
| 859 | sizeof(xfs_bmbt_rec_t); |
| 860 | else |
| 861 | fa.fsx_nextents = ip->i_d.di_anextents; |
| 862 | } else |
| 863 | fa.fsx_nextents = 0; |
| 864 | } else { |
| 865 | if (ip->i_df.if_flags & XFS_IFEXTENTS) |
| 866 | fa.fsx_nextents = ip->i_df.if_bytes / |
| 867 | sizeof(xfs_bmbt_rec_t); |
| 868 | else |
| 869 | fa.fsx_nextents = ip->i_d.di_nextents; |
| 870 | } |
| 871 | xfs_iunlock(ip, XFS_ILOCK_SHARED); |
| 872 | |
| 873 | if (copy_to_user(arg, &fa, sizeof(fa))) |
| 874 | return -EFAULT; |
| 875 | return 0; |
| 876 | } |
| 877 | |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 878 | STATIC void |
| 879 | xfs_set_diflags( |
| 880 | struct xfs_inode *ip, |
| 881 | unsigned int xflags) |
| 882 | { |
| 883 | unsigned int di_flags; |
| 884 | |
| 885 | /* can't set PREALLOC this way, just preserve it */ |
| 886 | di_flags = (ip->i_d.di_flags & XFS_DIFLAG_PREALLOC); |
| 887 | if (xflags & XFS_XFLAG_IMMUTABLE) |
| 888 | di_flags |= XFS_DIFLAG_IMMUTABLE; |
| 889 | if (xflags & XFS_XFLAG_APPEND) |
| 890 | di_flags |= XFS_DIFLAG_APPEND; |
| 891 | if (xflags & XFS_XFLAG_SYNC) |
| 892 | di_flags |= XFS_DIFLAG_SYNC; |
| 893 | if (xflags & XFS_XFLAG_NOATIME) |
| 894 | di_flags |= XFS_DIFLAG_NOATIME; |
| 895 | if (xflags & XFS_XFLAG_NODUMP) |
| 896 | di_flags |= XFS_DIFLAG_NODUMP; |
| 897 | if (xflags & XFS_XFLAG_PROJINHERIT) |
| 898 | di_flags |= XFS_DIFLAG_PROJINHERIT; |
| 899 | if (xflags & XFS_XFLAG_NODEFRAG) |
| 900 | di_flags |= XFS_DIFLAG_NODEFRAG; |
| 901 | if (xflags & XFS_XFLAG_FILESTREAM) |
| 902 | di_flags |= XFS_DIFLAG_FILESTREAM; |
| 903 | if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) { |
| 904 | if (xflags & XFS_XFLAG_RTINHERIT) |
| 905 | di_flags |= XFS_DIFLAG_RTINHERIT; |
| 906 | if (xflags & XFS_XFLAG_NOSYMLINKS) |
| 907 | di_flags |= XFS_DIFLAG_NOSYMLINKS; |
| 908 | if (xflags & XFS_XFLAG_EXTSZINHERIT) |
| 909 | di_flags |= XFS_DIFLAG_EXTSZINHERIT; |
| 910 | } else if ((ip->i_d.di_mode & S_IFMT) == S_IFREG) { |
| 911 | if (xflags & XFS_XFLAG_REALTIME) |
| 912 | di_flags |= XFS_DIFLAG_REALTIME; |
| 913 | if (xflags & XFS_XFLAG_EXTSIZE) |
| 914 | di_flags |= XFS_DIFLAG_EXTSIZE; |
| 915 | } |
| 916 | |
| 917 | ip->i_d.di_flags = di_flags; |
| 918 | } |
| 919 | |
Christoph Hellwig | f13fae2 | 2008-07-21 16:16:15 +1000 | [diff] [blame] | 920 | STATIC void |
| 921 | xfs_diflags_to_linux( |
| 922 | struct xfs_inode *ip) |
| 923 | { |
David Chinner | e4f7529 | 2008-08-13 16:00:45 +1000 | [diff] [blame] | 924 | struct inode *inode = VFS_I(ip); |
Christoph Hellwig | f13fae2 | 2008-07-21 16:16:15 +1000 | [diff] [blame] | 925 | unsigned int xflags = xfs_ip2xflags(ip); |
| 926 | |
| 927 | if (xflags & XFS_XFLAG_IMMUTABLE) |
| 928 | inode->i_flags |= S_IMMUTABLE; |
| 929 | else |
| 930 | inode->i_flags &= ~S_IMMUTABLE; |
| 931 | if (xflags & XFS_XFLAG_APPEND) |
| 932 | inode->i_flags |= S_APPEND; |
| 933 | else |
| 934 | inode->i_flags &= ~S_APPEND; |
| 935 | if (xflags & XFS_XFLAG_SYNC) |
| 936 | inode->i_flags |= S_SYNC; |
| 937 | else |
| 938 | inode->i_flags &= ~S_SYNC; |
| 939 | if (xflags & XFS_XFLAG_NOATIME) |
| 940 | inode->i_flags |= S_NOATIME; |
| 941 | else |
| 942 | inode->i_flags &= ~S_NOATIME; |
| 943 | } |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 944 | |
| 945 | #define FSX_PROJID 1 |
| 946 | #define FSX_EXTSIZE 2 |
| 947 | #define FSX_XFLAGS 4 |
| 948 | #define FSX_NONBLOCK 8 |
| 949 | |
| 950 | STATIC int |
| 951 | xfs_ioctl_setattr( |
| 952 | xfs_inode_t *ip, |
| 953 | struct fsxattr *fa, |
| 954 | int mask) |
| 955 | { |
| 956 | struct xfs_mount *mp = ip->i_mount; |
| 957 | struct xfs_trans *tp; |
| 958 | unsigned int lock_flags = 0; |
| 959 | struct xfs_dquot *udqp = NULL, *gdqp = NULL; |
| 960 | struct xfs_dquot *olddquot = NULL; |
| 961 | int code; |
| 962 | |
| 963 | xfs_itrace_entry(ip); |
| 964 | |
| 965 | if (mp->m_flags & XFS_MOUNT_RDONLY) |
| 966 | return XFS_ERROR(EROFS); |
| 967 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 968 | return XFS_ERROR(EIO); |
| 969 | |
| 970 | /* |
| 971 | * If disk quotas is on, we make sure that the dquots do exist on disk, |
| 972 | * before we start any other transactions. Trying to do this later |
| 973 | * is messy. We don't care to take a readlock to look at the ids |
| 974 | * in inode here, because we can't hold it across the trans_reserve. |
| 975 | * If the IDs do change before we take the ilock, we're covered |
| 976 | * because the i_*dquot fields will get updated anyway. |
| 977 | */ |
| 978 | if (XFS_IS_QUOTA_ON(mp) && (mask & FSX_PROJID)) { |
| 979 | code = XFS_QM_DQVOPALLOC(mp, ip, ip->i_d.di_uid, |
| 980 | ip->i_d.di_gid, fa->fsx_projid, |
| 981 | XFS_QMOPT_PQUOTA, &udqp, &gdqp); |
| 982 | if (code) |
| 983 | return code; |
| 984 | } |
| 985 | |
| 986 | /* |
| 987 | * For the other attributes, we acquire the inode lock and |
| 988 | * first do an error checking pass. |
| 989 | */ |
| 990 | tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE); |
| 991 | code = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES(mp), 0, 0, 0); |
| 992 | if (code) |
| 993 | goto error_return; |
| 994 | |
| 995 | lock_flags = XFS_ILOCK_EXCL; |
| 996 | xfs_ilock(ip, lock_flags); |
| 997 | |
| 998 | /* |
| 999 | * CAP_FOWNER overrides the following restrictions: |
| 1000 | * |
| 1001 | * The user ID of the calling process must be equal |
| 1002 | * to the file owner ID, except in cases where the |
| 1003 | * CAP_FSETID capability is applicable. |
| 1004 | */ |
David Howells | 91b7771 | 2008-10-31 15:50:04 +1100 | [diff] [blame] | 1005 | if (current_fsuid() != ip->i_d.di_uid && !capable(CAP_FOWNER)) { |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1006 | code = XFS_ERROR(EPERM); |
| 1007 | goto error_return; |
| 1008 | } |
| 1009 | |
| 1010 | /* |
| 1011 | * Do a quota reservation only if projid is actually going to change. |
| 1012 | */ |
| 1013 | if (mask & FSX_PROJID) { |
| 1014 | if (XFS_IS_PQUOTA_ON(mp) && |
| 1015 | ip->i_d.di_projid != fa->fsx_projid) { |
| 1016 | ASSERT(tp); |
| 1017 | code = XFS_QM_DQVOPCHOWNRESV(mp, tp, ip, udqp, gdqp, |
| 1018 | capable(CAP_FOWNER) ? |
| 1019 | XFS_QMOPT_FORCE_RES : 0); |
| 1020 | if (code) /* out of quota */ |
| 1021 | goto error_return; |
| 1022 | } |
| 1023 | } |
| 1024 | |
| 1025 | if (mask & FSX_EXTSIZE) { |
| 1026 | /* |
| 1027 | * Can't change extent size if any extents are allocated. |
| 1028 | */ |
| 1029 | if (ip->i_d.di_nextents && |
| 1030 | ((ip->i_d.di_extsize << mp->m_sb.sb_blocklog) != |
| 1031 | fa->fsx_extsize)) { |
| 1032 | code = XFS_ERROR(EINVAL); /* EFBIG? */ |
| 1033 | goto error_return; |
| 1034 | } |
| 1035 | |
| 1036 | /* |
| 1037 | * Extent size must be a multiple of the appropriate block |
| 1038 | * size, if set at all. |
| 1039 | */ |
| 1040 | if (fa->fsx_extsize != 0) { |
| 1041 | xfs_extlen_t size; |
| 1042 | |
| 1043 | if (XFS_IS_REALTIME_INODE(ip) || |
| 1044 | ((mask & FSX_XFLAGS) && |
| 1045 | (fa->fsx_xflags & XFS_XFLAG_REALTIME))) { |
| 1046 | size = mp->m_sb.sb_rextsize << |
| 1047 | mp->m_sb.sb_blocklog; |
| 1048 | } else { |
| 1049 | size = mp->m_sb.sb_blocksize; |
| 1050 | } |
| 1051 | |
| 1052 | if (fa->fsx_extsize % size) { |
| 1053 | code = XFS_ERROR(EINVAL); |
| 1054 | goto error_return; |
| 1055 | } |
| 1056 | } |
| 1057 | } |
| 1058 | |
| 1059 | |
| 1060 | if (mask & FSX_XFLAGS) { |
| 1061 | /* |
| 1062 | * Can't change realtime flag if any extents are allocated. |
| 1063 | */ |
| 1064 | if ((ip->i_d.di_nextents || ip->i_delayed_blks) && |
| 1065 | (XFS_IS_REALTIME_INODE(ip)) != |
| 1066 | (fa->fsx_xflags & XFS_XFLAG_REALTIME)) { |
| 1067 | code = XFS_ERROR(EINVAL); /* EFBIG? */ |
| 1068 | goto error_return; |
| 1069 | } |
| 1070 | |
| 1071 | /* |
| 1072 | * If realtime flag is set then must have realtime data. |
| 1073 | */ |
| 1074 | if ((fa->fsx_xflags & XFS_XFLAG_REALTIME)) { |
| 1075 | if ((mp->m_sb.sb_rblocks == 0) || |
| 1076 | (mp->m_sb.sb_rextsize == 0) || |
| 1077 | (ip->i_d.di_extsize % mp->m_sb.sb_rextsize)) { |
| 1078 | code = XFS_ERROR(EINVAL); |
| 1079 | goto error_return; |
| 1080 | } |
| 1081 | } |
| 1082 | |
| 1083 | /* |
| 1084 | * Can't modify an immutable/append-only file unless |
| 1085 | * we have appropriate permission. |
| 1086 | */ |
| 1087 | if ((ip->i_d.di_flags & |
| 1088 | (XFS_DIFLAG_IMMUTABLE|XFS_DIFLAG_APPEND) || |
| 1089 | (fa->fsx_xflags & |
| 1090 | (XFS_XFLAG_IMMUTABLE | XFS_XFLAG_APPEND))) && |
| 1091 | !capable(CAP_LINUX_IMMUTABLE)) { |
| 1092 | code = XFS_ERROR(EPERM); |
| 1093 | goto error_return; |
| 1094 | } |
| 1095 | } |
| 1096 | |
| 1097 | xfs_trans_ijoin(tp, ip, lock_flags); |
| 1098 | xfs_trans_ihold(tp, ip); |
| 1099 | |
| 1100 | /* |
| 1101 | * Change file ownership. Must be the owner or privileged. |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1102 | */ |
| 1103 | if (mask & FSX_PROJID) { |
| 1104 | /* |
| 1105 | * CAP_FSETID overrides the following restrictions: |
| 1106 | * |
| 1107 | * The set-user-ID and set-group-ID bits of a file will be |
| 1108 | * cleared upon successful return from chown() |
| 1109 | */ |
| 1110 | if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) && |
| 1111 | !capable(CAP_FSETID)) |
| 1112 | ip->i_d.di_mode &= ~(S_ISUID|S_ISGID); |
| 1113 | |
| 1114 | /* |
| 1115 | * Change the ownerships and register quota modifications |
| 1116 | * in the transaction. |
| 1117 | */ |
| 1118 | if (ip->i_d.di_projid != fa->fsx_projid) { |
| 1119 | if (XFS_IS_PQUOTA_ON(mp)) { |
| 1120 | olddquot = XFS_QM_DQVOPCHOWN(mp, tp, ip, |
| 1121 | &ip->i_gdquot, gdqp); |
| 1122 | } |
| 1123 | ip->i_d.di_projid = fa->fsx_projid; |
| 1124 | |
| 1125 | /* |
| 1126 | * We may have to rev the inode as well as |
| 1127 | * the superblock version number since projids didn't |
| 1128 | * exist before DINODE_VERSION_2 and SB_VERSION_NLINK. |
| 1129 | */ |
Christoph Hellwig | 51ce16d | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 1130 | if (ip->i_d.di_version == 1) |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1131 | xfs_bump_ino_vers2(tp, ip); |
| 1132 | } |
| 1133 | |
| 1134 | } |
| 1135 | |
| 1136 | if (mask & FSX_EXTSIZE) |
| 1137 | 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] | 1138 | if (mask & FSX_XFLAGS) { |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1139 | xfs_set_diflags(ip, fa->fsx_xflags); |
Christoph Hellwig | f13fae2 | 2008-07-21 16:16:15 +1000 | [diff] [blame] | 1140 | xfs_diflags_to_linux(ip); |
| 1141 | } |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1142 | |
| 1143 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
| 1144 | xfs_ichgtime(ip, XFS_ICHGTIME_CHG); |
| 1145 | |
| 1146 | XFS_STATS_INC(xs_ig_attrchg); |
| 1147 | |
| 1148 | /* |
| 1149 | * If this is a synchronous mount, make sure that the |
| 1150 | * transaction goes to disk before returning to the user. |
| 1151 | * This is slightly sub-optimal in that truncates require |
| 1152 | * two sync transactions instead of one for wsync filesystems. |
| 1153 | * One for the truncate and one for the timestamps since we |
| 1154 | * don't want to change the timestamps unless we're sure the |
| 1155 | * truncate worked. Truncates are less than 1% of the laddis |
| 1156 | * mix so this probably isn't worth the trouble to optimize. |
| 1157 | */ |
| 1158 | if (mp->m_flags & XFS_MOUNT_WSYNC) |
| 1159 | xfs_trans_set_sync(tp); |
| 1160 | code = xfs_trans_commit(tp, 0); |
| 1161 | xfs_iunlock(ip, lock_flags); |
| 1162 | |
| 1163 | /* |
| 1164 | * Release any dquot(s) the inode had kept before chown. |
| 1165 | */ |
| 1166 | XFS_QM_DQRELE(mp, olddquot); |
| 1167 | XFS_QM_DQRELE(mp, udqp); |
| 1168 | XFS_QM_DQRELE(mp, gdqp); |
| 1169 | |
| 1170 | if (code) |
| 1171 | return code; |
| 1172 | |
| 1173 | if (DM_EVENT_ENABLED(ip, DM_EVENT_ATTRIBUTE)) { |
| 1174 | XFS_SEND_NAMESP(mp, DM_EVENT_ATTRIBUTE, ip, DM_RIGHT_NULL, |
| 1175 | NULL, DM_RIGHT_NULL, NULL, NULL, 0, 0, |
| 1176 | (mask & FSX_NONBLOCK) ? DM_FLAGS_NDELAY : 0); |
| 1177 | } |
| 1178 | |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1179 | return 0; |
| 1180 | |
| 1181 | error_return: |
| 1182 | XFS_QM_DQRELE(mp, udqp); |
| 1183 | XFS_QM_DQRELE(mp, gdqp); |
| 1184 | xfs_trans_cancel(tp, 0); |
| 1185 | if (lock_flags) |
| 1186 | xfs_iunlock(ip, lock_flags); |
| 1187 | return code; |
| 1188 | } |
| 1189 | |
Christoph Hellwig | c83bfab | 2007-10-11 17:47:00 +1000 | [diff] [blame] | 1190 | STATIC int |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1191 | xfs_ioc_fssetxattr( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1192 | xfs_inode_t *ip, |
| 1193 | struct file *filp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1194 | void __user *arg) |
| 1195 | { |
| 1196 | struct fsxattr fa; |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1197 | unsigned int mask; |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1198 | |
| 1199 | if (copy_from_user(&fa, arg, sizeof(fa))) |
| 1200 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1201 | |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1202 | mask = FSX_XFLAGS | FSX_EXTSIZE | FSX_PROJID; |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1203 | if (filp->f_flags & (O_NDELAY|O_NONBLOCK)) |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1204 | mask |= FSX_NONBLOCK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1205 | |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1206 | return -xfs_ioctl_setattr(ip, &fa, mask); |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1207 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1208 | |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1209 | STATIC int |
| 1210 | xfs_ioc_getxflags( |
| 1211 | xfs_inode_t *ip, |
| 1212 | void __user *arg) |
| 1213 | { |
| 1214 | unsigned int flags; |
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 | flags = xfs_di2lxflags(ip->i_d.di_flags); |
| 1217 | if (copy_to_user(arg, &flags, sizeof(flags))) |
| 1218 | return -EFAULT; |
| 1219 | return 0; |
| 1220 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1221 | |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1222 | STATIC int |
| 1223 | xfs_ioc_setxflags( |
| 1224 | xfs_inode_t *ip, |
| 1225 | struct file *filp, |
| 1226 | void __user *arg) |
| 1227 | { |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1228 | struct fsxattr fa; |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1229 | unsigned int flags; |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1230 | unsigned int mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1231 | |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1232 | if (copy_from_user(&flags, arg, sizeof(flags))) |
| 1233 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1234 | |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1235 | if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \ |
| 1236 | FS_NOATIME_FL | FS_NODUMP_FL | \ |
| 1237 | FS_SYNC_FL)) |
| 1238 | return -EOPNOTSUPP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1239 | |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1240 | mask = FSX_XFLAGS; |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1241 | if (filp->f_flags & (O_NDELAY|O_NONBLOCK)) |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1242 | mask |= FSX_NONBLOCK; |
| 1243 | fa.fsx_xflags = xfs_merge_ioc_xflags(flags, xfs_ip2xflags(ip)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1244 | |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1245 | return -xfs_ioctl_setattr(ip, &fa, mask); |
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 | { |
| 1345 | struct inode *inode = filp->f_path.dentry->d_inode; |
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 | 4d4be48 | 2008-12-09 04:47:33 -0500 | [diff] [blame] | 1355 | xfs_itrace_entry(ip); |
| 1356 | |
| 1357 | switch (cmd) { |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1358 | case XFS_IOC_ALLOCSP: |
| 1359 | case XFS_IOC_FREESP: |
| 1360 | case XFS_IOC_RESVSP: |
| 1361 | case XFS_IOC_UNRESVSP: |
| 1362 | case XFS_IOC_ALLOCSP64: |
| 1363 | case XFS_IOC_FREESP64: |
| 1364 | case XFS_IOC_RESVSP64: |
sandeen@sandeen.net | 743bb46 | 2008-11-25 21:20:06 -0600 | [diff] [blame] | 1365 | case XFS_IOC_UNRESVSP64: { |
| 1366 | xfs_flock64_t bf; |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1367 | |
sandeen@sandeen.net | 743bb46 | 2008-11-25 21:20:06 -0600 | [diff] [blame] | 1368 | if (copy_from_user(&bf, arg, sizeof(bf))) |
| 1369 | return -XFS_ERROR(EFAULT); |
| 1370 | return xfs_ioc_space(ip, inode, filp, ioflags, cmd, &bf); |
| 1371 | } |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1372 | case XFS_IOC_DIOINFO: { |
| 1373 | struct dioattr da; |
| 1374 | xfs_buftarg_t *target = |
| 1375 | XFS_IS_REALTIME_INODE(ip) ? |
| 1376 | mp->m_rtdev_targp : mp->m_ddev_targp; |
| 1377 | |
| 1378 | da.d_mem = da.d_miniosz = 1 << target->bt_sshift; |
| 1379 | da.d_maxiosz = INT_MAX & ~(da.d_miniosz - 1); |
| 1380 | |
| 1381 | if (copy_to_user(arg, &da, sizeof(da))) |
| 1382 | return -XFS_ERROR(EFAULT); |
| 1383 | return 0; |
| 1384 | } |
| 1385 | |
| 1386 | case XFS_IOC_FSBULKSTAT_SINGLE: |
| 1387 | case XFS_IOC_FSBULKSTAT: |
| 1388 | case XFS_IOC_FSINUMBERS: |
| 1389 | return xfs_ioc_bulkstat(mp, cmd, arg); |
| 1390 | |
| 1391 | case XFS_IOC_FSGEOMETRY_V1: |
| 1392 | return xfs_ioc_fsgeometry_v1(mp, arg); |
| 1393 | |
| 1394 | case XFS_IOC_FSGEOMETRY: |
| 1395 | return xfs_ioc_fsgeometry(mp, arg); |
| 1396 | |
| 1397 | case XFS_IOC_GETVERSION: |
| 1398 | return put_user(inode->i_generation, (int __user *)arg); |
| 1399 | |
| 1400 | case XFS_IOC_FSGETXATTR: |
| 1401 | return xfs_ioc_fsgetxattr(ip, 0, arg); |
| 1402 | case XFS_IOC_FSGETXATTRA: |
| 1403 | return xfs_ioc_fsgetxattr(ip, 1, arg); |
Lachlan McIlroy | 3b2816b | 2008-04-18 12:43:35 +1000 | [diff] [blame] | 1404 | case XFS_IOC_FSSETXATTR: |
Lachlan McIlroy | 65e67f5 | 2008-04-18 12:59:45 +1000 | [diff] [blame] | 1405 | return xfs_ioc_fssetxattr(ip, filp, arg); |
| 1406 | case XFS_IOC_GETXFLAGS: |
| 1407 | return xfs_ioc_getxflags(ip, arg); |
| 1408 | case XFS_IOC_SETXFLAGS: |
| 1409 | return xfs_ioc_setxflags(ip, filp, arg); |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1410 | |
| 1411 | case XFS_IOC_FSSETDM: { |
| 1412 | struct fsdmidata dmi; |
| 1413 | |
| 1414 | if (copy_from_user(&dmi, arg, sizeof(dmi))) |
| 1415 | return -XFS_ERROR(EFAULT); |
| 1416 | |
| 1417 | error = xfs_set_dmattrs(ip, dmi.fsd_dmevmask, |
| 1418 | dmi.fsd_dmstate); |
| 1419 | return -error; |
| 1420 | } |
| 1421 | |
| 1422 | case XFS_IOC_GETBMAP: |
| 1423 | case XFS_IOC_GETBMAPA: |
| 1424 | return xfs_ioc_getbmap(ip, ioflags, cmd, arg); |
| 1425 | |
| 1426 | case XFS_IOC_GETBMAPX: |
| 1427 | return xfs_ioc_getbmapx(ip, arg); |
| 1428 | |
| 1429 | case XFS_IOC_FD_TO_HANDLE: |
| 1430 | case XFS_IOC_PATH_TO_HANDLE: |
sandeen@sandeen.net | 743bb46 | 2008-11-25 21:20:06 -0600 | [diff] [blame] | 1431 | case XFS_IOC_PATH_TO_FSHANDLE: { |
| 1432 | xfs_fsop_handlereq_t hreq; |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1433 | |
sandeen@sandeen.net | 743bb46 | 2008-11-25 21:20:06 -0600 | [diff] [blame] | 1434 | if (copy_from_user(&hreq, arg, sizeof(hreq))) |
| 1435 | return -XFS_ERROR(EFAULT); |
| 1436 | return xfs_find_handle(cmd, &hreq); |
| 1437 | } |
| 1438 | case XFS_IOC_OPEN_BY_HANDLE: { |
| 1439 | xfs_fsop_handlereq_t hreq; |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1440 | |
sandeen@sandeen.net | 743bb46 | 2008-11-25 21:20:06 -0600 | [diff] [blame] | 1441 | if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t))) |
| 1442 | return -XFS_ERROR(EFAULT); |
| 1443 | return xfs_open_by_handle(mp, &hreq, filp, inode); |
| 1444 | } |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1445 | case XFS_IOC_FSSETDM_BY_HANDLE: |
| 1446 | return xfs_fssetdm_by_handle(mp, arg, inode); |
| 1447 | |
sandeen@sandeen.net | 743bb46 | 2008-11-25 21:20:06 -0600 | [diff] [blame] | 1448 | case XFS_IOC_READLINK_BY_HANDLE: { |
| 1449 | xfs_fsop_handlereq_t hreq; |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1450 | |
sandeen@sandeen.net | 743bb46 | 2008-11-25 21:20:06 -0600 | [diff] [blame] | 1451 | if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t))) |
| 1452 | return -XFS_ERROR(EFAULT); |
| 1453 | return xfs_readlink_by_handle(mp, &hreq, inode); |
| 1454 | } |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1455 | case XFS_IOC_ATTRLIST_BY_HANDLE: |
| 1456 | return xfs_attrlist_by_handle(mp, arg, inode); |
| 1457 | |
| 1458 | case XFS_IOC_ATTRMULTI_BY_HANDLE: |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 1459 | return xfs_attrmulti_by_handle(mp, arg, filp, inode); |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1460 | |
| 1461 | case XFS_IOC_SWAPEXT: { |
sandeen@sandeen.net | 743bb46 | 2008-11-25 21:20:06 -0600 | [diff] [blame] | 1462 | struct xfs_swapext sxp; |
| 1463 | |
| 1464 | if (copy_from_user(&sxp, arg, sizeof(xfs_swapext_t))) |
| 1465 | return -XFS_ERROR(EFAULT); |
| 1466 | error = xfs_swapext(&sxp); |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1467 | return -error; |
| 1468 | } |
| 1469 | |
| 1470 | case XFS_IOC_FSCOUNTS: { |
| 1471 | xfs_fsop_counts_t out; |
| 1472 | |
| 1473 | error = xfs_fs_counts(mp, &out); |
| 1474 | if (error) |
| 1475 | return -error; |
| 1476 | |
| 1477 | if (copy_to_user(arg, &out, sizeof(out))) |
| 1478 | return -XFS_ERROR(EFAULT); |
| 1479 | return 0; |
| 1480 | } |
| 1481 | |
| 1482 | case XFS_IOC_SET_RESBLKS: { |
| 1483 | xfs_fsop_resblks_t inout; |
| 1484 | __uint64_t in; |
| 1485 | |
| 1486 | if (!capable(CAP_SYS_ADMIN)) |
| 1487 | return -EPERM; |
| 1488 | |
| 1489 | if (copy_from_user(&inout, arg, sizeof(inout))) |
| 1490 | return -XFS_ERROR(EFAULT); |
| 1491 | |
| 1492 | /* input parameter is passed in resblks field of structure */ |
| 1493 | in = inout.resblks; |
| 1494 | error = xfs_reserve_blocks(mp, &in, &inout); |
| 1495 | if (error) |
| 1496 | return -error; |
| 1497 | |
| 1498 | if (copy_to_user(arg, &inout, sizeof(inout))) |
| 1499 | return -XFS_ERROR(EFAULT); |
| 1500 | return 0; |
| 1501 | } |
| 1502 | |
| 1503 | case XFS_IOC_GET_RESBLKS: { |
| 1504 | xfs_fsop_resblks_t out; |
| 1505 | |
| 1506 | if (!capable(CAP_SYS_ADMIN)) |
| 1507 | return -EPERM; |
| 1508 | |
| 1509 | error = xfs_reserve_blocks(mp, NULL, &out); |
| 1510 | if (error) |
| 1511 | return -error; |
| 1512 | |
| 1513 | if (copy_to_user(arg, &out, sizeof(out))) |
| 1514 | return -XFS_ERROR(EFAULT); |
| 1515 | |
| 1516 | return 0; |
| 1517 | } |
| 1518 | |
| 1519 | case XFS_IOC_FSGROWFSDATA: { |
| 1520 | xfs_growfs_data_t in; |
| 1521 | |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1522 | if (copy_from_user(&in, arg, sizeof(in))) |
| 1523 | return -XFS_ERROR(EFAULT); |
| 1524 | |
| 1525 | error = xfs_growfs_data(mp, &in); |
| 1526 | return -error; |
| 1527 | } |
| 1528 | |
| 1529 | case XFS_IOC_FSGROWFSLOG: { |
| 1530 | xfs_growfs_log_t in; |
| 1531 | |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1532 | if (copy_from_user(&in, arg, sizeof(in))) |
| 1533 | return -XFS_ERROR(EFAULT); |
| 1534 | |
| 1535 | error = xfs_growfs_log(mp, &in); |
| 1536 | return -error; |
| 1537 | } |
| 1538 | |
| 1539 | case XFS_IOC_FSGROWFSRT: { |
| 1540 | xfs_growfs_rt_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 | |
| 1545 | error = xfs_growfs_rt(mp, &in); |
| 1546 | return -error; |
| 1547 | } |
| 1548 | |
| 1549 | case XFS_IOC_FREEZE: |
| 1550 | if (!capable(CAP_SYS_ADMIN)) |
| 1551 | return -EPERM; |
| 1552 | |
| 1553 | if (inode->i_sb->s_frozen == SB_UNFROZEN) |
| 1554 | freeze_bdev(inode->i_sb->s_bdev); |
| 1555 | return 0; |
| 1556 | |
| 1557 | case XFS_IOC_THAW: |
| 1558 | if (!capable(CAP_SYS_ADMIN)) |
| 1559 | return -EPERM; |
| 1560 | if (inode->i_sb->s_frozen != SB_UNFROZEN) |
| 1561 | thaw_bdev(inode->i_sb->s_bdev, inode->i_sb); |
| 1562 | return 0; |
| 1563 | |
| 1564 | case XFS_IOC_GOINGDOWN: { |
| 1565 | __uint32_t in; |
| 1566 | |
| 1567 | if (!capable(CAP_SYS_ADMIN)) |
| 1568 | return -EPERM; |
| 1569 | |
| 1570 | if (get_user(in, (__uint32_t __user *)arg)) |
| 1571 | return -XFS_ERROR(EFAULT); |
| 1572 | |
| 1573 | error = xfs_fs_goingdown(mp, in); |
| 1574 | return -error; |
| 1575 | } |
| 1576 | |
| 1577 | case XFS_IOC_ERROR_INJECTION: { |
| 1578 | xfs_error_injection_t in; |
| 1579 | |
| 1580 | if (!capable(CAP_SYS_ADMIN)) |
| 1581 | return -EPERM; |
| 1582 | |
| 1583 | if (copy_from_user(&in, arg, sizeof(in))) |
| 1584 | return -XFS_ERROR(EFAULT); |
| 1585 | |
| 1586 | error = xfs_errortag_add(in.errtag, mp); |
| 1587 | return -error; |
| 1588 | } |
| 1589 | |
| 1590 | case XFS_IOC_ERROR_CLEARALL: |
| 1591 | if (!capable(CAP_SYS_ADMIN)) |
| 1592 | return -EPERM; |
| 1593 | |
| 1594 | error = xfs_errortag_clearall(mp, 1); |
| 1595 | return -error; |
| 1596 | |
| 1597 | default: |
| 1598 | return -ENOTTY; |
| 1599 | } |
| 1600 | } |