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