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" |
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" |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 53 | #include "xfs_export.h" |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 54 | #include "xfs_trace.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 | 743bb465 | 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 | 743bb465 | 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 | 743bb465 | 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 | 743bb465 | 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 | 743bb465 | 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 | 743bb465 | 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 | 743bb465 | 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, |
Dave Chinner | a9273ca | 2010-01-20 10:47:48 +1100 | [diff] [blame] | 450 | unsigned char *name, |
| 451 | unsigned char __user *ubuf, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | __uint32_t *len, |
| 453 | __uint32_t flags) |
| 454 | { |
Dave Chinner | a9273ca | 2010-01-20 10:47:48 +1100 | [diff] [blame] | 455 | unsigned char *kbuf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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, |
Dave Chinner | a9273ca | 2010-01-20 10:47:48 +1100 | [diff] [blame] | 479 | unsigned char *name, |
| 480 | const unsigned char __user *ubuf, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | __uint32_t len, |
| 482 | __uint32_t flags) |
| 483 | { |
Dave Chinner | a9273ca | 2010-01-20 10:47:48 +1100 | [diff] [blame] | 484 | unsigned char *kbuf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Li Zefan | 0e639bd | 2009-04-08 15:08:04 +0800 | [diff] [blame] | 492 | kbuf = memdup_user(ubuf, len); |
| 493 | if (IS_ERR(kbuf)) |
| 494 | return PTR_ERR(kbuf); |
Barry Naujok | e8b0eba | 2008-04-22 17:34:31 +1000 | [diff] [blame] | 495 | |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 496 | error = xfs_attr_set(XFS_I(inode), name, kbuf, len, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | return error; |
| 499 | } |
| 500 | |
sandeen@sandeen.net | 2875097 | 2008-11-25 21:20:15 -0600 | [diff] [blame] | 501 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | xfs_attrmulti_attr_remove( |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 503 | struct inode *inode, |
Dave Chinner | a9273ca | 2010-01-20 10:47:48 +1100 | [diff] [blame] | 504 | unsigned char *name, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | __uint32_t flags) |
| 506 | { |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 507 | if (IS_IMMUTABLE(inode) || IS_APPEND(inode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | return EPERM; |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 509 | return xfs_attr_remove(XFS_I(inode), name, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | STATIC int |
| 513 | xfs_attrmulti_by_handle( |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 514 | struct file *parfilp, |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 515 | void __user *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | { |
| 517 | int error; |
| 518 | xfs_attr_multiop_t *ops; |
| 519 | xfs_fsop_attrmulti_handlereq_t am_hreq; |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 520 | struct dentry *dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | unsigned int i, size; |
Dave Chinner | a9273ca | 2010-01-20 10:47:48 +1100 | [diff] [blame] | 522 | unsigned char *attr_name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | |
| 524 | if (!capable(CAP_SYS_ADMIN)) |
| 525 | return -XFS_ERROR(EPERM); |
| 526 | if (copy_from_user(&am_hreq, arg, sizeof(xfs_fsop_attrmulti_handlereq_t))) |
| 527 | return -XFS_ERROR(EFAULT); |
| 528 | |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 529 | dentry = xfs_handlereq_to_dentry(parfilp, &am_hreq.hreq); |
| 530 | if (IS_ERR(dentry)) |
| 531 | return PTR_ERR(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | |
| 533 | error = E2BIG; |
Christoph Hellwig | e182f57 | 2008-06-27 13:32:31 +1000 | [diff] [blame] | 534 | size = am_hreq.opcount * sizeof(xfs_attr_multiop_t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | if (!size || size > 16 * PAGE_SIZE) |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 536 | goto out_dput; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | |
Li Zefan | 0e639bd | 2009-04-08 15:08:04 +0800 | [diff] [blame] | 538 | ops = memdup_user(am_hreq.ops, size); |
| 539 | if (IS_ERR(ops)) { |
| 540 | error = PTR_ERR(ops); |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 541 | goto out_dput; |
Li Zefan | 0e639bd | 2009-04-08 15:08:04 +0800 | [diff] [blame] | 542 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | |
| 544 | attr_name = kmalloc(MAXNAMELEN, GFP_KERNEL); |
| 545 | if (!attr_name) |
| 546 | goto out_kfree_ops; |
| 547 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | error = 0; |
| 549 | for (i = 0; i < am_hreq.opcount; i++) { |
Dave Chinner | a9273ca | 2010-01-20 10:47:48 +1100 | [diff] [blame] | 550 | ops[i].am_error = strncpy_from_user((char *)attr_name, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | ops[i].am_attrname, MAXNAMELEN); |
| 552 | if (ops[i].am_error == 0 || ops[i].am_error == MAXNAMELEN) |
| 553 | error = -ERANGE; |
| 554 | if (ops[i].am_error < 0) |
| 555 | break; |
| 556 | |
| 557 | switch (ops[i].am_opcode) { |
| 558 | case ATTR_OP_GET: |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 559 | ops[i].am_error = xfs_attrmulti_attr_get( |
| 560 | dentry->d_inode, attr_name, |
| 561 | ops[i].am_attrvalue, &ops[i].am_length, |
| 562 | ops[i].am_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | break; |
| 564 | case ATTR_OP_SET: |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 565 | ops[i].am_error = mnt_want_write(parfilp->f_path.mnt); |
| 566 | if (ops[i].am_error) |
| 567 | break; |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 568 | ops[i].am_error = xfs_attrmulti_attr_set( |
| 569 | dentry->d_inode, attr_name, |
| 570 | ops[i].am_attrvalue, ops[i].am_length, |
| 571 | ops[i].am_flags); |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 572 | mnt_drop_write(parfilp->f_path.mnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | break; |
| 574 | case ATTR_OP_REMOVE: |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 575 | ops[i].am_error = mnt_want_write(parfilp->f_path.mnt); |
| 576 | if (ops[i].am_error) |
| 577 | break; |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 578 | ops[i].am_error = xfs_attrmulti_attr_remove( |
| 579 | dentry->d_inode, attr_name, |
| 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 | default: |
| 584 | ops[i].am_error = EINVAL; |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | if (copy_to_user(am_hreq.ops, ops, size)) |
| 589 | error = XFS_ERROR(EFAULT); |
| 590 | |
| 591 | kfree(attr_name); |
| 592 | out_kfree_ops: |
| 593 | kfree(ops); |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 594 | out_dput: |
| 595 | dput(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | return -error; |
| 597 | } |
| 598 | |
sandeen@sandeen.net | d5547f9 | 2008-11-25 21:20:08 -0600 | [diff] [blame] | 599 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | xfs_ioc_space( |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 601 | struct xfs_inode *ip, |
Alexey Dobriyan | f37ea14 | 2006-09-28 10:52:04 +1000 | [diff] [blame] | 602 | struct inode *inode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | struct file *filp, |
| 604 | int ioflags, |
| 605 | unsigned int cmd, |
sandeen@sandeen.net | 743bb465 | 2008-11-25 21:20:06 -0600 | [diff] [blame] | 606 | xfs_flock64_t *bf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | int attr_flags = 0; |
| 609 | int error; |
| 610 | |
sandeen@sandeen.net | 743bb465 | 2008-11-25 21:20:06 -0600 | [diff] [blame] | 611 | /* |
| 612 | * Only allow the sys admin to reserve space unless |
| 613 | * unwritten extents are enabled. |
| 614 | */ |
| 615 | if (!xfs_sb_version_hasextflgbit(&ip->i_mount->m_sb) && |
| 616 | !capable(CAP_SYS_ADMIN)) |
| 617 | return -XFS_ERROR(EPERM); |
| 618 | |
Alexey Dobriyan | f37ea14 | 2006-09-28 10:52:04 +1000 | [diff] [blame] | 619 | if (inode->i_flags & (S_IMMUTABLE|S_APPEND)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | return -XFS_ERROR(EPERM); |
| 621 | |
Eric Sandeen | ad4a8ac | 2005-09-02 16:41:16 +1000 | [diff] [blame] | 622 | if (!(filp->f_mode & FMODE_WRITE)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | return -XFS_ERROR(EBADF); |
| 624 | |
Alexey Dobriyan | f37ea14 | 2006-09-28 10:52:04 +1000 | [diff] [blame] | 625 | if (!S_ISREG(inode->i_mode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | return -XFS_ERROR(EINVAL); |
| 627 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | if (filp->f_flags & (O_NDELAY|O_NONBLOCK)) |
Christoph Hellwig | 0f285c8 | 2008-07-18 17:13:28 +1000 | [diff] [blame] | 629 | attr_flags |= XFS_ATTR_NONBLOCK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | if (ioflags & IO_INVIS) |
Christoph Hellwig | 0f285c8 | 2008-07-18 17:13:28 +1000 | [diff] [blame] | 631 | attr_flags |= XFS_ATTR_DMI; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | |
sandeen@sandeen.net | 743bb465 | 2008-11-25 21:20:06 -0600 | [diff] [blame] | 633 | 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] | 634 | return -error; |
| 635 | } |
| 636 | |
| 637 | STATIC int |
| 638 | xfs_ioc_bulkstat( |
| 639 | xfs_mount_t *mp, |
| 640 | unsigned int cmd, |
| 641 | void __user *arg) |
| 642 | { |
| 643 | xfs_fsop_bulkreq_t bulkreq; |
| 644 | int count; /* # of records returned */ |
| 645 | xfs_ino_t inlast; /* last inode number */ |
| 646 | int done; |
| 647 | int error; |
| 648 | |
| 649 | /* done = 1 if there are more stats to get and if bulkstat */ |
| 650 | /* should be called again (unused here, but used in dmapi) */ |
| 651 | |
| 652 | if (!capable(CAP_SYS_ADMIN)) |
| 653 | return -EPERM; |
| 654 | |
| 655 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 656 | return -XFS_ERROR(EIO); |
| 657 | |
| 658 | if (copy_from_user(&bulkreq, arg, sizeof(xfs_fsop_bulkreq_t))) |
| 659 | return -XFS_ERROR(EFAULT); |
| 660 | |
| 661 | if (copy_from_user(&inlast, bulkreq.lastip, sizeof(__s64))) |
| 662 | return -XFS_ERROR(EFAULT); |
| 663 | |
| 664 | if ((count = bulkreq.icount) <= 0) |
| 665 | return -XFS_ERROR(EINVAL); |
| 666 | |
Lachlan McIlroy | cd57e59 | 2007-11-23 16:30:32 +1100 | [diff] [blame] | 667 | if (bulkreq.ubuffer == NULL) |
| 668 | return -XFS_ERROR(EINVAL); |
| 669 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | if (cmd == XFS_IOC_FSINUMBERS) |
| 671 | error = xfs_inumbers(mp, &inlast, &count, |
Michal Marek | faa63e9 | 2007-07-11 11:10:19 +1000 | [diff] [blame] | 672 | bulkreq.ubuffer, xfs_inumbers_fmt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | else if (cmd == XFS_IOC_FSBULKSTAT_SINGLE) |
| 674 | error = xfs_bulkstat_single(mp, &inlast, |
| 675 | bulkreq.ubuffer, &done); |
Lachlan McIlroy | cd57e59 | 2007-11-23 16:30:32 +1100 | [diff] [blame] | 676 | else /* XFS_IOC_FSBULKSTAT */ |
| 677 | error = xfs_bulkstat(mp, &inlast, &count, |
| 678 | (bulkstat_one_pf)xfs_bulkstat_one, NULL, |
| 679 | sizeof(xfs_bstat_t), bulkreq.ubuffer, |
| 680 | BULKSTAT_FG_QUICK, &done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | |
| 682 | if (error) |
| 683 | return -error; |
| 684 | |
| 685 | if (bulkreq.ocount != NULL) { |
| 686 | if (copy_to_user(bulkreq.lastip, &inlast, |
| 687 | sizeof(xfs_ino_t))) |
| 688 | return -XFS_ERROR(EFAULT); |
| 689 | |
| 690 | if (copy_to_user(bulkreq.ocount, &count, sizeof(count))) |
| 691 | return -XFS_ERROR(EFAULT); |
| 692 | } |
| 693 | |
| 694 | return 0; |
| 695 | } |
| 696 | |
| 697 | STATIC int |
| 698 | xfs_ioc_fsgeometry_v1( |
| 699 | xfs_mount_t *mp, |
| 700 | void __user *arg) |
| 701 | { |
| 702 | xfs_fsop_geom_v1_t fsgeo; |
| 703 | int error; |
| 704 | |
| 705 | error = xfs_fs_geometry(mp, (xfs_fsop_geom_t *)&fsgeo, 3); |
| 706 | if (error) |
| 707 | return -error; |
| 708 | |
| 709 | if (copy_to_user(arg, &fsgeo, sizeof(fsgeo))) |
| 710 | return -XFS_ERROR(EFAULT); |
| 711 | return 0; |
| 712 | } |
| 713 | |
| 714 | STATIC int |
| 715 | xfs_ioc_fsgeometry( |
| 716 | xfs_mount_t *mp, |
| 717 | void __user *arg) |
| 718 | { |
| 719 | xfs_fsop_geom_t fsgeo; |
| 720 | int error; |
| 721 | |
| 722 | error = xfs_fs_geometry(mp, &fsgeo, 4); |
| 723 | if (error) |
| 724 | return -error; |
| 725 | |
| 726 | if (copy_to_user(arg, &fsgeo, sizeof(fsgeo))) |
| 727 | return -XFS_ERROR(EFAULT); |
| 728 | return 0; |
| 729 | } |
| 730 | |
| 731 | /* |
| 732 | * Linux extended inode flags interface. |
| 733 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 734 | |
| 735 | STATIC unsigned int |
| 736 | xfs_merge_ioc_xflags( |
| 737 | unsigned int flags, |
| 738 | unsigned int start) |
| 739 | { |
| 740 | unsigned int xflags = start; |
| 741 | |
Eric Sandeen | 39058a0 | 2007-02-10 18:37:10 +1100 | [diff] [blame] | 742 | if (flags & FS_IMMUTABLE_FL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | xflags |= XFS_XFLAG_IMMUTABLE; |
| 744 | else |
| 745 | xflags &= ~XFS_XFLAG_IMMUTABLE; |
Eric Sandeen | 39058a0 | 2007-02-10 18:37:10 +1100 | [diff] [blame] | 746 | if (flags & FS_APPEND_FL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | xflags |= XFS_XFLAG_APPEND; |
| 748 | else |
| 749 | xflags &= ~XFS_XFLAG_APPEND; |
Eric Sandeen | 39058a0 | 2007-02-10 18:37:10 +1100 | [diff] [blame] | 750 | if (flags & FS_SYNC_FL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | xflags |= XFS_XFLAG_SYNC; |
| 752 | else |
| 753 | xflags &= ~XFS_XFLAG_SYNC; |
Eric Sandeen | 39058a0 | 2007-02-10 18:37:10 +1100 | [diff] [blame] | 754 | if (flags & FS_NOATIME_FL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | xflags |= XFS_XFLAG_NOATIME; |
| 756 | else |
| 757 | xflags &= ~XFS_XFLAG_NOATIME; |
Eric Sandeen | 39058a0 | 2007-02-10 18:37:10 +1100 | [diff] [blame] | 758 | if (flags & FS_NODUMP_FL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | xflags |= XFS_XFLAG_NODUMP; |
| 760 | else |
| 761 | xflags &= ~XFS_XFLAG_NODUMP; |
| 762 | |
| 763 | return xflags; |
| 764 | } |
| 765 | |
| 766 | STATIC unsigned int |
| 767 | xfs_di2lxflags( |
| 768 | __uint16_t di_flags) |
| 769 | { |
| 770 | unsigned int flags = 0; |
| 771 | |
| 772 | if (di_flags & XFS_DIFLAG_IMMUTABLE) |
Eric Sandeen | 39058a0 | 2007-02-10 18:37:10 +1100 | [diff] [blame] | 773 | flags |= FS_IMMUTABLE_FL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | if (di_flags & XFS_DIFLAG_APPEND) |
Eric Sandeen | 39058a0 | 2007-02-10 18:37:10 +1100 | [diff] [blame] | 775 | flags |= FS_APPEND_FL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | if (di_flags & XFS_DIFLAG_SYNC) |
Eric Sandeen | 39058a0 | 2007-02-10 18:37:10 +1100 | [diff] [blame] | 777 | flags |= FS_SYNC_FL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | if (di_flags & XFS_DIFLAG_NOATIME) |
Eric Sandeen | 39058a0 | 2007-02-10 18:37:10 +1100 | [diff] [blame] | 779 | flags |= FS_NOATIME_FL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | if (di_flags & XFS_DIFLAG_NODUMP) |
Eric Sandeen | 39058a0 | 2007-02-10 18:37:10 +1100 | [diff] [blame] | 781 | flags |= FS_NODUMP_FL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | return flags; |
| 783 | } |
| 784 | |
| 785 | STATIC int |
Christoph Hellwig | c83bfab | 2007-10-11 17:47:00 +1000 | [diff] [blame] | 786 | xfs_ioc_fsgetxattr( |
| 787 | xfs_inode_t *ip, |
| 788 | int attr, |
| 789 | void __user *arg) |
| 790 | { |
| 791 | struct fsxattr fa; |
| 792 | |
| 793 | xfs_ilock(ip, XFS_ILOCK_SHARED); |
| 794 | fa.fsx_xflags = xfs_ip2xflags(ip); |
| 795 | fa.fsx_extsize = ip->i_d.di_extsize << ip->i_mount->m_sb.sb_blocklog; |
| 796 | fa.fsx_projid = ip->i_d.di_projid; |
| 797 | |
| 798 | if (attr) { |
| 799 | if (ip->i_afp) { |
| 800 | if (ip->i_afp->if_flags & XFS_IFEXTENTS) |
| 801 | fa.fsx_nextents = ip->i_afp->if_bytes / |
| 802 | sizeof(xfs_bmbt_rec_t); |
| 803 | else |
| 804 | fa.fsx_nextents = ip->i_d.di_anextents; |
| 805 | } else |
| 806 | fa.fsx_nextents = 0; |
| 807 | } else { |
| 808 | if (ip->i_df.if_flags & XFS_IFEXTENTS) |
| 809 | fa.fsx_nextents = ip->i_df.if_bytes / |
| 810 | sizeof(xfs_bmbt_rec_t); |
| 811 | else |
| 812 | fa.fsx_nextents = ip->i_d.di_nextents; |
| 813 | } |
| 814 | xfs_iunlock(ip, XFS_ILOCK_SHARED); |
| 815 | |
| 816 | if (copy_to_user(arg, &fa, sizeof(fa))) |
| 817 | return -EFAULT; |
| 818 | return 0; |
| 819 | } |
| 820 | |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 821 | STATIC void |
| 822 | xfs_set_diflags( |
| 823 | struct xfs_inode *ip, |
| 824 | unsigned int xflags) |
| 825 | { |
| 826 | unsigned int di_flags; |
| 827 | |
| 828 | /* can't set PREALLOC this way, just preserve it */ |
| 829 | di_flags = (ip->i_d.di_flags & XFS_DIFLAG_PREALLOC); |
| 830 | if (xflags & XFS_XFLAG_IMMUTABLE) |
| 831 | di_flags |= XFS_DIFLAG_IMMUTABLE; |
| 832 | if (xflags & XFS_XFLAG_APPEND) |
| 833 | di_flags |= XFS_DIFLAG_APPEND; |
| 834 | if (xflags & XFS_XFLAG_SYNC) |
| 835 | di_flags |= XFS_DIFLAG_SYNC; |
| 836 | if (xflags & XFS_XFLAG_NOATIME) |
| 837 | di_flags |= XFS_DIFLAG_NOATIME; |
| 838 | if (xflags & XFS_XFLAG_NODUMP) |
| 839 | di_flags |= XFS_DIFLAG_NODUMP; |
| 840 | if (xflags & XFS_XFLAG_PROJINHERIT) |
| 841 | di_flags |= XFS_DIFLAG_PROJINHERIT; |
| 842 | if (xflags & XFS_XFLAG_NODEFRAG) |
| 843 | di_flags |= XFS_DIFLAG_NODEFRAG; |
| 844 | if (xflags & XFS_XFLAG_FILESTREAM) |
| 845 | di_flags |= XFS_DIFLAG_FILESTREAM; |
| 846 | if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) { |
| 847 | if (xflags & XFS_XFLAG_RTINHERIT) |
| 848 | di_flags |= XFS_DIFLAG_RTINHERIT; |
| 849 | if (xflags & XFS_XFLAG_NOSYMLINKS) |
| 850 | di_flags |= XFS_DIFLAG_NOSYMLINKS; |
| 851 | if (xflags & XFS_XFLAG_EXTSZINHERIT) |
| 852 | di_flags |= XFS_DIFLAG_EXTSZINHERIT; |
| 853 | } else if ((ip->i_d.di_mode & S_IFMT) == S_IFREG) { |
| 854 | if (xflags & XFS_XFLAG_REALTIME) |
| 855 | di_flags |= XFS_DIFLAG_REALTIME; |
| 856 | if (xflags & XFS_XFLAG_EXTSIZE) |
| 857 | di_flags |= XFS_DIFLAG_EXTSIZE; |
| 858 | } |
| 859 | |
| 860 | ip->i_d.di_flags = di_flags; |
| 861 | } |
| 862 | |
Christoph Hellwig | f13fae2 | 2008-07-21 16:16:15 +1000 | [diff] [blame] | 863 | STATIC void |
| 864 | xfs_diflags_to_linux( |
| 865 | struct xfs_inode *ip) |
| 866 | { |
David Chinner | e4f7529 | 2008-08-13 16:00:45 +1000 | [diff] [blame] | 867 | struct inode *inode = VFS_I(ip); |
Christoph Hellwig | f13fae2 | 2008-07-21 16:16:15 +1000 | [diff] [blame] | 868 | unsigned int xflags = xfs_ip2xflags(ip); |
| 869 | |
| 870 | if (xflags & XFS_XFLAG_IMMUTABLE) |
| 871 | inode->i_flags |= S_IMMUTABLE; |
| 872 | else |
| 873 | inode->i_flags &= ~S_IMMUTABLE; |
| 874 | if (xflags & XFS_XFLAG_APPEND) |
| 875 | inode->i_flags |= S_APPEND; |
| 876 | else |
| 877 | inode->i_flags &= ~S_APPEND; |
| 878 | if (xflags & XFS_XFLAG_SYNC) |
| 879 | inode->i_flags |= S_SYNC; |
| 880 | else |
| 881 | inode->i_flags &= ~S_SYNC; |
| 882 | if (xflags & XFS_XFLAG_NOATIME) |
| 883 | inode->i_flags |= S_NOATIME; |
| 884 | else |
| 885 | inode->i_flags &= ~S_NOATIME; |
| 886 | } |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 887 | |
| 888 | #define FSX_PROJID 1 |
| 889 | #define FSX_EXTSIZE 2 |
| 890 | #define FSX_XFLAGS 4 |
| 891 | #define FSX_NONBLOCK 8 |
| 892 | |
| 893 | STATIC int |
| 894 | xfs_ioctl_setattr( |
| 895 | xfs_inode_t *ip, |
| 896 | struct fsxattr *fa, |
| 897 | int mask) |
| 898 | { |
| 899 | struct xfs_mount *mp = ip->i_mount; |
| 900 | struct xfs_trans *tp; |
| 901 | unsigned int lock_flags = 0; |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 902 | struct xfs_dquot *udqp = NULL; |
| 903 | struct xfs_dquot *gdqp = NULL; |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 904 | struct xfs_dquot *olddquot = NULL; |
| 905 | int code; |
| 906 | |
| 907 | xfs_itrace_entry(ip); |
| 908 | |
| 909 | if (mp->m_flags & XFS_MOUNT_RDONLY) |
| 910 | return XFS_ERROR(EROFS); |
| 911 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 912 | return XFS_ERROR(EIO); |
| 913 | |
| 914 | /* |
| 915 | * If disk quotas is on, we make sure that the dquots do exist on disk, |
| 916 | * before we start any other transactions. Trying to do this later |
| 917 | * is messy. We don't care to take a readlock to look at the ids |
| 918 | * in inode here, because we can't hold it across the trans_reserve. |
| 919 | * If the IDs do change before we take the ilock, we're covered |
| 920 | * because the i_*dquot fields will get updated anyway. |
| 921 | */ |
| 922 | if (XFS_IS_QUOTA_ON(mp) && (mask & FSX_PROJID)) { |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 923 | code = xfs_qm_vop_dqalloc(ip, ip->i_d.di_uid, |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 924 | ip->i_d.di_gid, fa->fsx_projid, |
| 925 | XFS_QMOPT_PQUOTA, &udqp, &gdqp); |
| 926 | if (code) |
| 927 | return code; |
| 928 | } |
| 929 | |
| 930 | /* |
| 931 | * For the other attributes, we acquire the inode lock and |
| 932 | * first do an error checking pass. |
| 933 | */ |
| 934 | tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE); |
| 935 | code = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES(mp), 0, 0, 0); |
| 936 | if (code) |
| 937 | goto error_return; |
| 938 | |
| 939 | lock_flags = XFS_ILOCK_EXCL; |
| 940 | xfs_ilock(ip, lock_flags); |
| 941 | |
| 942 | /* |
| 943 | * CAP_FOWNER overrides the following restrictions: |
| 944 | * |
| 945 | * The user ID of the calling process must be equal |
| 946 | * to the file owner ID, except in cases where the |
| 947 | * CAP_FSETID capability is applicable. |
| 948 | */ |
David Howells | 91b777125 | 2008-10-31 15:50:04 +1100 | [diff] [blame] | 949 | if (current_fsuid() != ip->i_d.di_uid && !capable(CAP_FOWNER)) { |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 950 | code = XFS_ERROR(EPERM); |
| 951 | goto error_return; |
| 952 | } |
| 953 | |
| 954 | /* |
| 955 | * Do a quota reservation only if projid is actually going to change. |
| 956 | */ |
| 957 | if (mask & FSX_PROJID) { |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 958 | if (XFS_IS_QUOTA_RUNNING(mp) && |
| 959 | XFS_IS_PQUOTA_ON(mp) && |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 960 | ip->i_d.di_projid != fa->fsx_projid) { |
| 961 | ASSERT(tp); |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 962 | code = xfs_qm_vop_chown_reserve(tp, ip, udqp, gdqp, |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 963 | capable(CAP_FOWNER) ? |
| 964 | XFS_QMOPT_FORCE_RES : 0); |
| 965 | if (code) /* out of quota */ |
| 966 | goto error_return; |
| 967 | } |
| 968 | } |
| 969 | |
| 970 | if (mask & FSX_EXTSIZE) { |
| 971 | /* |
| 972 | * Can't change extent size if any extents are allocated. |
| 973 | */ |
| 974 | if (ip->i_d.di_nextents && |
| 975 | ((ip->i_d.di_extsize << mp->m_sb.sb_blocklog) != |
| 976 | fa->fsx_extsize)) { |
| 977 | code = XFS_ERROR(EINVAL); /* EFBIG? */ |
| 978 | goto error_return; |
| 979 | } |
| 980 | |
| 981 | /* |
| 982 | * Extent size must be a multiple of the appropriate block |
| 983 | * size, if set at all. |
| 984 | */ |
| 985 | if (fa->fsx_extsize != 0) { |
| 986 | xfs_extlen_t size; |
| 987 | |
| 988 | if (XFS_IS_REALTIME_INODE(ip) || |
| 989 | ((mask & FSX_XFLAGS) && |
| 990 | (fa->fsx_xflags & XFS_XFLAG_REALTIME))) { |
| 991 | size = mp->m_sb.sb_rextsize << |
| 992 | mp->m_sb.sb_blocklog; |
| 993 | } else { |
| 994 | size = mp->m_sb.sb_blocksize; |
| 995 | } |
| 996 | |
| 997 | if (fa->fsx_extsize % size) { |
| 998 | code = XFS_ERROR(EINVAL); |
| 999 | goto error_return; |
| 1000 | } |
| 1001 | } |
| 1002 | } |
| 1003 | |
| 1004 | |
| 1005 | if (mask & FSX_XFLAGS) { |
| 1006 | /* |
| 1007 | * Can't change realtime flag if any extents are allocated. |
| 1008 | */ |
| 1009 | if ((ip->i_d.di_nextents || ip->i_delayed_blks) && |
| 1010 | (XFS_IS_REALTIME_INODE(ip)) != |
| 1011 | (fa->fsx_xflags & XFS_XFLAG_REALTIME)) { |
| 1012 | code = XFS_ERROR(EINVAL); /* EFBIG? */ |
| 1013 | goto error_return; |
| 1014 | } |
| 1015 | |
| 1016 | /* |
| 1017 | * If realtime flag is set then must have realtime data. |
| 1018 | */ |
| 1019 | if ((fa->fsx_xflags & XFS_XFLAG_REALTIME)) { |
| 1020 | if ((mp->m_sb.sb_rblocks == 0) || |
| 1021 | (mp->m_sb.sb_rextsize == 0) || |
| 1022 | (ip->i_d.di_extsize % mp->m_sb.sb_rextsize)) { |
| 1023 | code = XFS_ERROR(EINVAL); |
| 1024 | goto error_return; |
| 1025 | } |
| 1026 | } |
| 1027 | |
| 1028 | /* |
| 1029 | * Can't modify an immutable/append-only file unless |
| 1030 | * we have appropriate permission. |
| 1031 | */ |
| 1032 | if ((ip->i_d.di_flags & |
| 1033 | (XFS_DIFLAG_IMMUTABLE|XFS_DIFLAG_APPEND) || |
| 1034 | (fa->fsx_xflags & |
| 1035 | (XFS_XFLAG_IMMUTABLE | XFS_XFLAG_APPEND))) && |
| 1036 | !capable(CAP_LINUX_IMMUTABLE)) { |
| 1037 | code = XFS_ERROR(EPERM); |
| 1038 | goto error_return; |
| 1039 | } |
| 1040 | } |
| 1041 | |
| 1042 | xfs_trans_ijoin(tp, ip, lock_flags); |
| 1043 | xfs_trans_ihold(tp, ip); |
| 1044 | |
| 1045 | /* |
| 1046 | * Change file ownership. Must be the owner or privileged. |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1047 | */ |
| 1048 | if (mask & FSX_PROJID) { |
| 1049 | /* |
| 1050 | * CAP_FSETID overrides the following restrictions: |
| 1051 | * |
| 1052 | * The set-user-ID and set-group-ID bits of a file will be |
| 1053 | * cleared upon successful return from chown() |
| 1054 | */ |
| 1055 | if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) && |
| 1056 | !capable(CAP_FSETID)) |
| 1057 | ip->i_d.di_mode &= ~(S_ISUID|S_ISGID); |
| 1058 | |
| 1059 | /* |
| 1060 | * Change the ownerships and register quota modifications |
| 1061 | * in the transaction. |
| 1062 | */ |
| 1063 | if (ip->i_d.di_projid != fa->fsx_projid) { |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 1064 | if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_PQUOTA_ON(mp)) { |
| 1065 | olddquot = xfs_qm_vop_chown(tp, ip, |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1066 | &ip->i_gdquot, gdqp); |
| 1067 | } |
| 1068 | ip->i_d.di_projid = fa->fsx_projid; |
| 1069 | |
| 1070 | /* |
| 1071 | * We may have to rev the inode as well as |
| 1072 | * the superblock version number since projids didn't |
| 1073 | * exist before DINODE_VERSION_2 and SB_VERSION_NLINK. |
| 1074 | */ |
Christoph Hellwig | 51ce16d | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 1075 | if (ip->i_d.di_version == 1) |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1076 | xfs_bump_ino_vers2(tp, ip); |
| 1077 | } |
| 1078 | |
| 1079 | } |
| 1080 | |
| 1081 | if (mask & FSX_EXTSIZE) |
| 1082 | 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] | 1083 | if (mask & FSX_XFLAGS) { |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1084 | xfs_set_diflags(ip, fa->fsx_xflags); |
Christoph Hellwig | f13fae2 | 2008-07-21 16:16:15 +1000 | [diff] [blame] | 1085 | xfs_diflags_to_linux(ip); |
| 1086 | } |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1087 | |
| 1088 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
| 1089 | xfs_ichgtime(ip, XFS_ICHGTIME_CHG); |
| 1090 | |
| 1091 | XFS_STATS_INC(xs_ig_attrchg); |
| 1092 | |
| 1093 | /* |
| 1094 | * If this is a synchronous mount, make sure that the |
| 1095 | * transaction goes to disk before returning to the user. |
| 1096 | * This is slightly sub-optimal in that truncates require |
| 1097 | * two sync transactions instead of one for wsync filesystems. |
| 1098 | * One for the truncate and one for the timestamps since we |
| 1099 | * don't want to change the timestamps unless we're sure the |
| 1100 | * truncate worked. Truncates are less than 1% of the laddis |
| 1101 | * mix so this probably isn't worth the trouble to optimize. |
| 1102 | */ |
| 1103 | if (mp->m_flags & XFS_MOUNT_WSYNC) |
| 1104 | xfs_trans_set_sync(tp); |
| 1105 | code = xfs_trans_commit(tp, 0); |
| 1106 | xfs_iunlock(ip, lock_flags); |
| 1107 | |
| 1108 | /* |
| 1109 | * Release any dquot(s) the inode had kept before chown. |
| 1110 | */ |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 1111 | xfs_qm_dqrele(olddquot); |
| 1112 | xfs_qm_dqrele(udqp); |
| 1113 | xfs_qm_dqrele(gdqp); |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1114 | |
| 1115 | if (code) |
| 1116 | return code; |
| 1117 | |
| 1118 | if (DM_EVENT_ENABLED(ip, DM_EVENT_ATTRIBUTE)) { |
| 1119 | XFS_SEND_NAMESP(mp, DM_EVENT_ATTRIBUTE, ip, DM_RIGHT_NULL, |
| 1120 | NULL, DM_RIGHT_NULL, NULL, NULL, 0, 0, |
| 1121 | (mask & FSX_NONBLOCK) ? DM_FLAGS_NDELAY : 0); |
| 1122 | } |
| 1123 | |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1124 | return 0; |
| 1125 | |
| 1126 | error_return: |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 1127 | xfs_qm_dqrele(udqp); |
| 1128 | xfs_qm_dqrele(gdqp); |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1129 | xfs_trans_cancel(tp, 0); |
| 1130 | if (lock_flags) |
| 1131 | xfs_iunlock(ip, lock_flags); |
| 1132 | return code; |
| 1133 | } |
| 1134 | |
Christoph Hellwig | c83bfab | 2007-10-11 17:47:00 +1000 | [diff] [blame] | 1135 | STATIC int |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1136 | xfs_ioc_fssetxattr( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1137 | xfs_inode_t *ip, |
| 1138 | struct file *filp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1139 | void __user *arg) |
| 1140 | { |
| 1141 | struct fsxattr fa; |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1142 | unsigned int mask; |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1143 | |
| 1144 | if (copy_from_user(&fa, arg, sizeof(fa))) |
| 1145 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1146 | |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1147 | mask = FSX_XFLAGS | FSX_EXTSIZE | FSX_PROJID; |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1148 | if (filp->f_flags & (O_NDELAY|O_NONBLOCK)) |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1149 | mask |= FSX_NONBLOCK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1150 | |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1151 | return -xfs_ioctl_setattr(ip, &fa, mask); |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1152 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1153 | |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1154 | STATIC int |
| 1155 | xfs_ioc_getxflags( |
| 1156 | xfs_inode_t *ip, |
| 1157 | void __user *arg) |
| 1158 | { |
| 1159 | unsigned int flags; |
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 | flags = xfs_di2lxflags(ip->i_d.di_flags); |
| 1162 | if (copy_to_user(arg, &flags, sizeof(flags))) |
| 1163 | return -EFAULT; |
| 1164 | return 0; |
| 1165 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1166 | |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1167 | STATIC int |
| 1168 | xfs_ioc_setxflags( |
| 1169 | xfs_inode_t *ip, |
| 1170 | struct file *filp, |
| 1171 | void __user *arg) |
| 1172 | { |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1173 | struct fsxattr fa; |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1174 | unsigned int flags; |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1175 | unsigned int mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1176 | |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1177 | if (copy_from_user(&flags, arg, sizeof(flags))) |
| 1178 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1179 | |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1180 | if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \ |
| 1181 | FS_NOATIME_FL | FS_NODUMP_FL | \ |
| 1182 | FS_SYNC_FL)) |
| 1183 | return -EOPNOTSUPP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1184 | |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1185 | mask = FSX_XFLAGS; |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1186 | if (filp->f_flags & (O_NDELAY|O_NONBLOCK)) |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1187 | mask |= FSX_NONBLOCK; |
| 1188 | fa.fsx_xflags = xfs_merge_ioc_xflags(flags, xfs_ip2xflags(ip)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1189 | |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1190 | return -xfs_ioctl_setattr(ip, &fa, mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1191 | } |
| 1192 | |
| 1193 | STATIC int |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 1194 | xfs_getbmap_format(void **ap, struct getbmapx *bmv, int *full) |
| 1195 | { |
| 1196 | struct getbmap __user *base = *ap; |
| 1197 | |
| 1198 | /* copy only getbmap portion (not getbmapx) */ |
| 1199 | if (copy_to_user(base, bmv, sizeof(struct getbmap))) |
| 1200 | return XFS_ERROR(EFAULT); |
| 1201 | |
| 1202 | *ap += sizeof(struct getbmap); |
| 1203 | return 0; |
| 1204 | } |
| 1205 | |
| 1206 | STATIC int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1207 | xfs_ioc_getbmap( |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 1208 | struct xfs_inode *ip, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1209 | int ioflags, |
| 1210 | unsigned int cmd, |
| 1211 | void __user *arg) |
| 1212 | { |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 1213 | struct getbmapx bmx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1214 | int error; |
| 1215 | |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 1216 | if (copy_from_user(&bmx, arg, sizeof(struct getbmapx))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1217 | return -XFS_ERROR(EFAULT); |
| 1218 | |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 1219 | if (bmx.bmv_count < 2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1220 | return -XFS_ERROR(EINVAL); |
| 1221 | |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 1222 | bmx.bmv_iflags = (cmd == XFS_IOC_GETBMAPA ? BMV_IF_ATTRFORK : 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1223 | if (ioflags & IO_INVIS) |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 1224 | bmx.bmv_iflags |= BMV_IF_NO_DMAPI_READ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1225 | |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 1226 | error = xfs_getbmap(ip, &bmx, xfs_getbmap_format, |
| 1227 | (struct getbmap *)arg+1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1228 | if (error) |
| 1229 | return -error; |
| 1230 | |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 1231 | /* copy back header - only size of getbmap */ |
| 1232 | if (copy_to_user(arg, &bmx, sizeof(struct getbmap))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1233 | return -XFS_ERROR(EFAULT); |
| 1234 | return 0; |
| 1235 | } |
| 1236 | |
| 1237 | STATIC int |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 1238 | xfs_getbmapx_format(void **ap, struct getbmapx *bmv, int *full) |
| 1239 | { |
| 1240 | struct getbmapx __user *base = *ap; |
| 1241 | |
| 1242 | if (copy_to_user(base, bmv, sizeof(struct getbmapx))) |
| 1243 | return XFS_ERROR(EFAULT); |
| 1244 | |
| 1245 | *ap += sizeof(struct getbmapx); |
| 1246 | return 0; |
| 1247 | } |
| 1248 | |
| 1249 | STATIC int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1250 | xfs_ioc_getbmapx( |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 1251 | struct xfs_inode *ip, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1252 | void __user *arg) |
| 1253 | { |
| 1254 | struct getbmapx bmx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1255 | int error; |
| 1256 | |
| 1257 | if (copy_from_user(&bmx, arg, sizeof(bmx))) |
| 1258 | return -XFS_ERROR(EFAULT); |
| 1259 | |
| 1260 | if (bmx.bmv_count < 2) |
| 1261 | return -XFS_ERROR(EINVAL); |
| 1262 | |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 1263 | if (bmx.bmv_iflags & (~BMV_IF_VALID)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1264 | return -XFS_ERROR(EINVAL); |
| 1265 | |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 1266 | error = xfs_getbmap(ip, &bmx, xfs_getbmapx_format, |
| 1267 | (struct getbmapx *)arg+1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1268 | if (error) |
| 1269 | return -error; |
| 1270 | |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 1271 | /* copy back header */ |
| 1272 | if (copy_to_user(arg, &bmx, sizeof(struct getbmapx))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1273 | return -XFS_ERROR(EFAULT); |
| 1274 | |
| 1275 | return 0; |
| 1276 | } |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1277 | |
Christoph Hellwig | 4d4be48 | 2008-12-09 04:47:33 -0500 | [diff] [blame] | 1278 | /* |
| 1279 | * Note: some of the ioctl's return positive numbers as a |
| 1280 | * byte count indicating success, such as readlink_by_handle. |
| 1281 | * So we don't "sign flip" like most other routines. This means |
| 1282 | * true errors need to be returned as a negative value. |
| 1283 | */ |
| 1284 | long |
| 1285 | xfs_file_ioctl( |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1286 | struct file *filp, |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1287 | unsigned int cmd, |
Christoph Hellwig | 4d4be48 | 2008-12-09 04:47:33 -0500 | [diff] [blame] | 1288 | unsigned long p) |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1289 | { |
| 1290 | struct inode *inode = filp->f_path.dentry->d_inode; |
Christoph Hellwig | 4d4be48 | 2008-12-09 04:47:33 -0500 | [diff] [blame] | 1291 | struct xfs_inode *ip = XFS_I(inode); |
| 1292 | struct xfs_mount *mp = ip->i_mount; |
| 1293 | void __user *arg = (void __user *)p; |
| 1294 | int ioflags = 0; |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1295 | int error; |
| 1296 | |
Christoph Hellwig | 4d4be48 | 2008-12-09 04:47:33 -0500 | [diff] [blame] | 1297 | if (filp->f_mode & FMODE_NOCMTIME) |
| 1298 | ioflags |= IO_INVIS; |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1299 | |
Christoph Hellwig | 4d4be48 | 2008-12-09 04:47:33 -0500 | [diff] [blame] | 1300 | xfs_itrace_entry(ip); |
| 1301 | |
| 1302 | switch (cmd) { |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1303 | case XFS_IOC_ALLOCSP: |
| 1304 | case XFS_IOC_FREESP: |
| 1305 | case XFS_IOC_RESVSP: |
| 1306 | case XFS_IOC_UNRESVSP: |
| 1307 | case XFS_IOC_ALLOCSP64: |
| 1308 | case XFS_IOC_FREESP64: |
| 1309 | case XFS_IOC_RESVSP64: |
sandeen@sandeen.net | 743bb465 | 2008-11-25 21:20:06 -0600 | [diff] [blame] | 1310 | case XFS_IOC_UNRESVSP64: { |
| 1311 | xfs_flock64_t bf; |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1312 | |
sandeen@sandeen.net | 743bb465 | 2008-11-25 21:20:06 -0600 | [diff] [blame] | 1313 | if (copy_from_user(&bf, arg, sizeof(bf))) |
| 1314 | return -XFS_ERROR(EFAULT); |
| 1315 | return xfs_ioc_space(ip, inode, filp, ioflags, cmd, &bf); |
| 1316 | } |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1317 | case XFS_IOC_DIOINFO: { |
| 1318 | struct dioattr da; |
| 1319 | xfs_buftarg_t *target = |
| 1320 | XFS_IS_REALTIME_INODE(ip) ? |
| 1321 | mp->m_rtdev_targp : mp->m_ddev_targp; |
| 1322 | |
| 1323 | da.d_mem = da.d_miniosz = 1 << target->bt_sshift; |
| 1324 | da.d_maxiosz = INT_MAX & ~(da.d_miniosz - 1); |
| 1325 | |
| 1326 | if (copy_to_user(arg, &da, sizeof(da))) |
| 1327 | return -XFS_ERROR(EFAULT); |
| 1328 | return 0; |
| 1329 | } |
| 1330 | |
| 1331 | case XFS_IOC_FSBULKSTAT_SINGLE: |
| 1332 | case XFS_IOC_FSBULKSTAT: |
| 1333 | case XFS_IOC_FSINUMBERS: |
| 1334 | return xfs_ioc_bulkstat(mp, cmd, arg); |
| 1335 | |
| 1336 | case XFS_IOC_FSGEOMETRY_V1: |
| 1337 | return xfs_ioc_fsgeometry_v1(mp, arg); |
| 1338 | |
| 1339 | case XFS_IOC_FSGEOMETRY: |
| 1340 | return xfs_ioc_fsgeometry(mp, arg); |
| 1341 | |
| 1342 | case XFS_IOC_GETVERSION: |
| 1343 | return put_user(inode->i_generation, (int __user *)arg); |
| 1344 | |
| 1345 | case XFS_IOC_FSGETXATTR: |
| 1346 | return xfs_ioc_fsgetxattr(ip, 0, arg); |
| 1347 | case XFS_IOC_FSGETXATTRA: |
| 1348 | return xfs_ioc_fsgetxattr(ip, 1, arg); |
Lachlan McIlroy | 3b2816b | 2008-04-18 12:43:35 +1000 | [diff] [blame] | 1349 | case XFS_IOC_FSSETXATTR: |
Lachlan McIlroy | 65e67f5 | 2008-04-18 12:59:45 +1000 | [diff] [blame] | 1350 | return xfs_ioc_fssetxattr(ip, filp, arg); |
| 1351 | case XFS_IOC_GETXFLAGS: |
| 1352 | return xfs_ioc_getxflags(ip, arg); |
| 1353 | case XFS_IOC_SETXFLAGS: |
| 1354 | return xfs_ioc_setxflags(ip, filp, arg); |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1355 | |
| 1356 | case XFS_IOC_FSSETDM: { |
| 1357 | struct fsdmidata dmi; |
| 1358 | |
| 1359 | if (copy_from_user(&dmi, arg, sizeof(dmi))) |
| 1360 | return -XFS_ERROR(EFAULT); |
| 1361 | |
| 1362 | error = xfs_set_dmattrs(ip, dmi.fsd_dmevmask, |
| 1363 | dmi.fsd_dmstate); |
| 1364 | return -error; |
| 1365 | } |
| 1366 | |
| 1367 | case XFS_IOC_GETBMAP: |
| 1368 | case XFS_IOC_GETBMAPA: |
| 1369 | return xfs_ioc_getbmap(ip, ioflags, cmd, arg); |
| 1370 | |
| 1371 | case XFS_IOC_GETBMAPX: |
| 1372 | return xfs_ioc_getbmapx(ip, arg); |
| 1373 | |
| 1374 | case XFS_IOC_FD_TO_HANDLE: |
| 1375 | case XFS_IOC_PATH_TO_HANDLE: |
sandeen@sandeen.net | 743bb465 | 2008-11-25 21:20:06 -0600 | [diff] [blame] | 1376 | case XFS_IOC_PATH_TO_FSHANDLE: { |
| 1377 | xfs_fsop_handlereq_t hreq; |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1378 | |
sandeen@sandeen.net | 743bb465 | 2008-11-25 21:20:06 -0600 | [diff] [blame] | 1379 | if (copy_from_user(&hreq, arg, sizeof(hreq))) |
| 1380 | return -XFS_ERROR(EFAULT); |
| 1381 | return xfs_find_handle(cmd, &hreq); |
| 1382 | } |
| 1383 | case XFS_IOC_OPEN_BY_HANDLE: { |
| 1384 | xfs_fsop_handlereq_t hreq; |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1385 | |
sandeen@sandeen.net | 743bb465 | 2008-11-25 21:20:06 -0600 | [diff] [blame] | 1386 | if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t))) |
| 1387 | return -XFS_ERROR(EFAULT); |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 1388 | return xfs_open_by_handle(filp, &hreq); |
sandeen@sandeen.net | 743bb465 | 2008-11-25 21:20:06 -0600 | [diff] [blame] | 1389 | } |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1390 | case XFS_IOC_FSSETDM_BY_HANDLE: |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 1391 | return xfs_fssetdm_by_handle(filp, arg); |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1392 | |
sandeen@sandeen.net | 743bb465 | 2008-11-25 21:20:06 -0600 | [diff] [blame] | 1393 | case XFS_IOC_READLINK_BY_HANDLE: { |
| 1394 | xfs_fsop_handlereq_t hreq; |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1395 | |
sandeen@sandeen.net | 743bb465 | 2008-11-25 21:20:06 -0600 | [diff] [blame] | 1396 | if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t))) |
| 1397 | return -XFS_ERROR(EFAULT); |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 1398 | return xfs_readlink_by_handle(filp, &hreq); |
sandeen@sandeen.net | 743bb465 | 2008-11-25 21:20:06 -0600 | [diff] [blame] | 1399 | } |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1400 | case XFS_IOC_ATTRLIST_BY_HANDLE: |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 1401 | return xfs_attrlist_by_handle(filp, arg); |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1402 | |
| 1403 | case XFS_IOC_ATTRMULTI_BY_HANDLE: |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 1404 | return xfs_attrmulti_by_handle(filp, arg); |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1405 | |
| 1406 | case XFS_IOC_SWAPEXT: { |
sandeen@sandeen.net | 743bb465 | 2008-11-25 21:20:06 -0600 | [diff] [blame] | 1407 | struct xfs_swapext sxp; |
| 1408 | |
| 1409 | if (copy_from_user(&sxp, arg, sizeof(xfs_swapext_t))) |
| 1410 | return -XFS_ERROR(EFAULT); |
| 1411 | error = xfs_swapext(&sxp); |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1412 | return -error; |
| 1413 | } |
| 1414 | |
| 1415 | case XFS_IOC_FSCOUNTS: { |
| 1416 | xfs_fsop_counts_t out; |
| 1417 | |
| 1418 | error = xfs_fs_counts(mp, &out); |
| 1419 | if (error) |
| 1420 | return -error; |
| 1421 | |
| 1422 | if (copy_to_user(arg, &out, sizeof(out))) |
| 1423 | return -XFS_ERROR(EFAULT); |
| 1424 | return 0; |
| 1425 | } |
| 1426 | |
| 1427 | case XFS_IOC_SET_RESBLKS: { |
| 1428 | xfs_fsop_resblks_t inout; |
| 1429 | __uint64_t in; |
| 1430 | |
| 1431 | if (!capable(CAP_SYS_ADMIN)) |
| 1432 | return -EPERM; |
| 1433 | |
Eric Sandeen | d5db0f9 | 2010-02-05 22:59:53 +0000 | [diff] [blame] | 1434 | if (mp->m_flags & XFS_MOUNT_RDONLY) |
| 1435 | return -XFS_ERROR(EROFS); |
| 1436 | |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1437 | if (copy_from_user(&inout, arg, sizeof(inout))) |
| 1438 | return -XFS_ERROR(EFAULT); |
| 1439 | |
| 1440 | /* input parameter is passed in resblks field of structure */ |
| 1441 | in = inout.resblks; |
| 1442 | error = xfs_reserve_blocks(mp, &in, &inout); |
| 1443 | if (error) |
| 1444 | return -error; |
| 1445 | |
| 1446 | if (copy_to_user(arg, &inout, sizeof(inout))) |
| 1447 | return -XFS_ERROR(EFAULT); |
| 1448 | return 0; |
| 1449 | } |
| 1450 | |
| 1451 | case XFS_IOC_GET_RESBLKS: { |
| 1452 | xfs_fsop_resblks_t out; |
| 1453 | |
| 1454 | if (!capable(CAP_SYS_ADMIN)) |
| 1455 | return -EPERM; |
| 1456 | |
| 1457 | error = xfs_reserve_blocks(mp, NULL, &out); |
| 1458 | if (error) |
| 1459 | return -error; |
| 1460 | |
| 1461 | if (copy_to_user(arg, &out, sizeof(out))) |
| 1462 | return -XFS_ERROR(EFAULT); |
| 1463 | |
| 1464 | return 0; |
| 1465 | } |
| 1466 | |
| 1467 | case XFS_IOC_FSGROWFSDATA: { |
| 1468 | xfs_growfs_data_t in; |
| 1469 | |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1470 | if (copy_from_user(&in, arg, sizeof(in))) |
| 1471 | return -XFS_ERROR(EFAULT); |
| 1472 | |
| 1473 | error = xfs_growfs_data(mp, &in); |
| 1474 | return -error; |
| 1475 | } |
| 1476 | |
| 1477 | case XFS_IOC_FSGROWFSLOG: { |
| 1478 | xfs_growfs_log_t in; |
| 1479 | |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1480 | if (copy_from_user(&in, arg, sizeof(in))) |
| 1481 | return -XFS_ERROR(EFAULT); |
| 1482 | |
| 1483 | error = xfs_growfs_log(mp, &in); |
| 1484 | return -error; |
| 1485 | } |
| 1486 | |
| 1487 | case XFS_IOC_FSGROWFSRT: { |
| 1488 | xfs_growfs_rt_t in; |
| 1489 | |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1490 | if (copy_from_user(&in, arg, sizeof(in))) |
| 1491 | return -XFS_ERROR(EFAULT); |
| 1492 | |
| 1493 | error = xfs_growfs_rt(mp, &in); |
| 1494 | return -error; |
| 1495 | } |
| 1496 | |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1497 | case XFS_IOC_GOINGDOWN: { |
| 1498 | __uint32_t in; |
| 1499 | |
| 1500 | if (!capable(CAP_SYS_ADMIN)) |
| 1501 | return -EPERM; |
| 1502 | |
| 1503 | if (get_user(in, (__uint32_t __user *)arg)) |
| 1504 | return -XFS_ERROR(EFAULT); |
| 1505 | |
| 1506 | error = xfs_fs_goingdown(mp, in); |
| 1507 | return -error; |
| 1508 | } |
| 1509 | |
| 1510 | case XFS_IOC_ERROR_INJECTION: { |
| 1511 | xfs_error_injection_t in; |
| 1512 | |
| 1513 | if (!capable(CAP_SYS_ADMIN)) |
| 1514 | return -EPERM; |
| 1515 | |
| 1516 | if (copy_from_user(&in, arg, sizeof(in))) |
| 1517 | return -XFS_ERROR(EFAULT); |
| 1518 | |
| 1519 | error = xfs_errortag_add(in.errtag, mp); |
| 1520 | return -error; |
| 1521 | } |
| 1522 | |
| 1523 | case XFS_IOC_ERROR_CLEARALL: |
| 1524 | if (!capable(CAP_SYS_ADMIN)) |
| 1525 | return -EPERM; |
| 1526 | |
| 1527 | error = xfs_errortag_clearall(mp, 1); |
| 1528 | return -error; |
| 1529 | |
| 1530 | default: |
| 1531 | return -ENOTTY; |
| 1532 | } |
| 1533 | } |