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