1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4 * All Rights Reserved.
5 */
6 #include "xfs.h"
7 #include "xfs_fs.h"
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_log_format.h"
11 #include "xfs_trans_resv.h"
12 #include "xfs_mount.h"
13 #include "xfs_inode.h"
14 #include "xfs_rtalloc.h"
15 #include "xfs_iwalk.h"
16 #include "xfs_itable.h"
17 #include "xfs_error.h"
18 #include "xfs_attr.h"
19 #include "xfs_bmap.h"
20 #include "xfs_bmap_util.h"
21 #include "xfs_fsops.h"
22 #include "xfs_discard.h"
23 #include "xfs_quota.h"
24 #include "xfs_export.h"
25 #include "xfs_trace.h"
26 #include "xfs_icache.h"
27 #include "xfs_trans.h"
28 #include "xfs_acl.h"
29 #include "xfs_btree.h"
30 #include <linux/fsmap.h>
31 #include "xfs_fsmap.h"
32 #include "scrub/xfs_scrub.h"
33 #include "xfs_sb.h"
34 #include "xfs_ag.h"
35 #include "xfs_health.h"
36
37 #include <linux/mount.h>
38 #include <linux/namei.h>
39
40 /*
41 * xfs_find_handle maps from userspace xfs_fsop_handlereq structure to
42 * a file or fs handle.
43 *
44 * XFS_IOC_PATH_TO_FSHANDLE
45 * returns fs handle for a mount point or path within that mount point
46 * XFS_IOC_FD_TO_HANDLE
47 * returns full handle for a FD opened in user space
48 * XFS_IOC_PATH_TO_HANDLE
49 * returns full handle for a path
50 */
51 int
xfs_find_handle(unsigned int cmd,xfs_fsop_handlereq_t * hreq)52 xfs_find_handle(
53 unsigned int cmd,
54 xfs_fsop_handlereq_t *hreq)
55 {
56 int hsize;
57 xfs_handle_t handle;
58 struct inode *inode;
59 struct fd f = {NULL};
60 struct path path;
61 int error;
62 struct xfs_inode *ip;
63
64 if (cmd == XFS_IOC_FD_TO_HANDLE) {
65 f = fdget(hreq->fd);
66 if (!f.file)
67 return -EBADF;
68 inode = file_inode(f.file);
69 } else {
70 error = user_path_at(AT_FDCWD, hreq->path, 0, &path);
71 if (error)
72 return error;
73 inode = d_inode(path.dentry);
74 }
75 ip = XFS_I(inode);
76
77 /*
78 * We can only generate handles for inodes residing on a XFS filesystem,
79 * and only for regular files, directories or symbolic links.
80 */
81 error = -EINVAL;
82 if (inode->i_sb->s_magic != XFS_SB_MAGIC)
83 goto out_put;
84
85 error = -EBADF;
86 if (!S_ISREG(inode->i_mode) &&
87 !S_ISDIR(inode->i_mode) &&
88 !S_ISLNK(inode->i_mode))
89 goto out_put;
90
91
92 memcpy(&handle.ha_fsid, ip->i_mount->m_fixedfsid, sizeof(xfs_fsid_t));
93
94 if (cmd == XFS_IOC_PATH_TO_FSHANDLE) {
95 /*
96 * This handle only contains an fsid, zero the rest.
97 */
98 memset(&handle.ha_fid, 0, sizeof(handle.ha_fid));
99 hsize = sizeof(xfs_fsid_t);
100 } else {
101 handle.ha_fid.fid_len = sizeof(xfs_fid_t) -
102 sizeof(handle.ha_fid.fid_len);
103 handle.ha_fid.fid_pad = 0;
104 handle.ha_fid.fid_gen = inode->i_generation;
105 handle.ha_fid.fid_ino = ip->i_ino;
106 hsize = sizeof(xfs_handle_t);
107 }
108
109 error = -EFAULT;
110 if (copy_to_user(hreq->ohandle, &handle, hsize) ||
111 copy_to_user(hreq->ohandlen, &hsize, sizeof(__s32)))
112 goto out_put;
113
114 error = 0;
115
116 out_put:
117 if (cmd == XFS_IOC_FD_TO_HANDLE)
118 fdput(f);
119 else
120 path_put(&path);
121 return error;
122 }
123
124 /*
125 * No need to do permission checks on the various pathname components
126 * as the handle operations are privileged.
127 */
128 STATIC int
xfs_handle_acceptable(void * context,struct dentry * dentry)129 xfs_handle_acceptable(
130 void *context,
131 struct dentry *dentry)
132 {
133 return 1;
134 }
135
136 /*
137 * Convert userspace handle data into a dentry.
138 */
139 struct dentry *
xfs_handle_to_dentry(struct file * parfilp,void __user * uhandle,u32 hlen)140 xfs_handle_to_dentry(
141 struct file *parfilp,
142 void __user *uhandle,
143 u32 hlen)
144 {
145 xfs_handle_t handle;
146 struct xfs_fid64 fid;
147
148 /*
149 * Only allow handle opens under a directory.
150 */
151 if (!S_ISDIR(file_inode(parfilp)->i_mode))
152 return ERR_PTR(-ENOTDIR);
153
154 if (hlen != sizeof(xfs_handle_t))
155 return ERR_PTR(-EINVAL);
156 if (copy_from_user(&handle, uhandle, hlen))
157 return ERR_PTR(-EFAULT);
158 if (handle.ha_fid.fid_len !=
159 sizeof(handle.ha_fid) - sizeof(handle.ha_fid.fid_len))
160 return ERR_PTR(-EINVAL);
161
162 memset(&fid, 0, sizeof(struct fid));
163 fid.ino = handle.ha_fid.fid_ino;
164 fid.gen = handle.ha_fid.fid_gen;
165
166 return exportfs_decode_fh(parfilp->f_path.mnt, (struct fid *)&fid, 3,
167 FILEID_INO32_GEN | XFS_FILEID_TYPE_64FLAG,
168 xfs_handle_acceptable, NULL);
169 }
170
171 STATIC struct dentry *
xfs_handlereq_to_dentry(struct file * parfilp,xfs_fsop_handlereq_t * hreq)172 xfs_handlereq_to_dentry(
173 struct file *parfilp,
174 xfs_fsop_handlereq_t *hreq)
175 {
176 return xfs_handle_to_dentry(parfilp, hreq->ihandle, hreq->ihandlen);
177 }
178
179 int
xfs_open_by_handle(struct file * parfilp,xfs_fsop_handlereq_t * hreq)180 xfs_open_by_handle(
181 struct file *parfilp,
182 xfs_fsop_handlereq_t *hreq)
183 {
184 const struct cred *cred = current_cred();
185 int error;
186 int fd;
187 int permflag;
188 struct file *filp;
189 struct inode *inode;
190 struct dentry *dentry;
191 fmode_t fmode;
192 struct path path;
193
194 if (!capable(CAP_SYS_ADMIN))
195 return -EPERM;
196
197 dentry = xfs_handlereq_to_dentry(parfilp, hreq);
198 if (IS_ERR(dentry))
199 return PTR_ERR(dentry);
200 inode = d_inode(dentry);
201
202 /* Restrict xfs_open_by_handle to directories & regular files. */
203 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode))) {
204 error = -EPERM;
205 goto out_dput;
206 }
207
208 #if BITS_PER_LONG != 32
209 hreq->oflags |= O_LARGEFILE;
210 #endif
211
212 permflag = hreq->oflags;
213 fmode = OPEN_FMODE(permflag);
214 if ((!(permflag & O_APPEND) || (permflag & O_TRUNC)) &&
215 (fmode & FMODE_WRITE) && IS_APPEND(inode)) {
216 error = -EPERM;
217 goto out_dput;
218 }
219
220 if ((fmode & FMODE_WRITE) && IS_IMMUTABLE(inode)) {
221 error = -EPERM;
222 goto out_dput;
223 }
224
225 /* Can't write directories. */
226 if (S_ISDIR(inode->i_mode) && (fmode & FMODE_WRITE)) {
227 error = -EISDIR;
228 goto out_dput;
229 }
230
231 fd = get_unused_fd_flags(0);
232 if (fd < 0) {
233 error = fd;
234 goto out_dput;
235 }
236
237 path.mnt = parfilp->f_path.mnt;
238 path.dentry = dentry;
239 filp = dentry_open(&path, hreq->oflags, cred);
240 dput(dentry);
241 if (IS_ERR(filp)) {
242 put_unused_fd(fd);
243 return PTR_ERR(filp);
244 }
245
246 if (S_ISREG(inode->i_mode)) {
247 filp->f_flags |= O_NOATIME;
248 filp->f_mode |= FMODE_NOCMTIME;
249 }
250
251 fd_install(fd, filp);
252 return fd;
253
254 out_dput:
255 dput(dentry);
256 return error;
257 }
258
259 int
xfs_readlink_by_handle(struct file * parfilp,xfs_fsop_handlereq_t * hreq)260 xfs_readlink_by_handle(
261 struct file *parfilp,
262 xfs_fsop_handlereq_t *hreq)
263 {
264 struct dentry *dentry;
265 __u32 olen;
266 int error;
267
268 if (!capable(CAP_SYS_ADMIN))
269 return -EPERM;
270
271 dentry = xfs_handlereq_to_dentry(parfilp, hreq);
272 if (IS_ERR(dentry))
273 return PTR_ERR(dentry);
274
275 /* Restrict this handle operation to symlinks only. */
276 if (!d_is_symlink(dentry)) {
277 error = -EINVAL;
278 goto out_dput;
279 }
280
281 if (copy_from_user(&olen, hreq->ohandlen, sizeof(__u32))) {
282 error = -EFAULT;
283 goto out_dput;
284 }
285
286 error = vfs_readlink(dentry, hreq->ohandle, olen);
287
288 out_dput:
289 dput(dentry);
290 return error;
291 }
292
293 int
xfs_set_dmattrs(xfs_inode_t * ip,uint evmask,uint16_t state)294 xfs_set_dmattrs(
295 xfs_inode_t *ip,
296 uint evmask,
297 uint16_t state)
298 {
299 xfs_mount_t *mp = ip->i_mount;
300 xfs_trans_t *tp;
301 int error;
302
303 if (!capable(CAP_SYS_ADMIN))
304 return -EPERM;
305
306 if (XFS_FORCED_SHUTDOWN(mp))
307 return -EIO;
308
309 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
310 if (error)
311 return error;
312
313 xfs_ilock(ip, XFS_ILOCK_EXCL);
314 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
315
316 ip->i_d.di_dmevmask = evmask;
317 ip->i_d.di_dmstate = state;
318
319 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
320 error = xfs_trans_commit(tp);
321
322 return error;
323 }
324
325 STATIC int
xfs_fssetdm_by_handle(struct file * parfilp,void __user * arg)326 xfs_fssetdm_by_handle(
327 struct file *parfilp,
328 void __user *arg)
329 {
330 int error;
331 struct fsdmidata fsd;
332 xfs_fsop_setdm_handlereq_t dmhreq;
333 struct dentry *dentry;
334
335 if (!capable(CAP_MKNOD))
336 return -EPERM;
337 if (copy_from_user(&dmhreq, arg, sizeof(xfs_fsop_setdm_handlereq_t)))
338 return -EFAULT;
339
340 error = mnt_want_write_file(parfilp);
341 if (error)
342 return error;
343
344 dentry = xfs_handlereq_to_dentry(parfilp, &dmhreq.hreq);
345 if (IS_ERR(dentry)) {
346 mnt_drop_write_file(parfilp);
347 return PTR_ERR(dentry);
348 }
349
350 if (IS_IMMUTABLE(d_inode(dentry)) || IS_APPEND(d_inode(dentry))) {
351 error = -EPERM;
352 goto out;
353 }
354
355 if (copy_from_user(&fsd, dmhreq.data, sizeof(fsd))) {
356 error = -EFAULT;
357 goto out;
358 }
359
360 error = xfs_set_dmattrs(XFS_I(d_inode(dentry)), fsd.fsd_dmevmask,
361 fsd.fsd_dmstate);
362
363 out:
364 mnt_drop_write_file(parfilp);
365 dput(dentry);
366 return error;
367 }
368
369 STATIC int
xfs_attrlist_by_handle(struct file * parfilp,void __user * arg)370 xfs_attrlist_by_handle(
371 struct file *parfilp,
372 void __user *arg)
373 {
374 int error = -ENOMEM;
375 attrlist_cursor_kern_t *cursor;
376 struct xfs_fsop_attrlist_handlereq __user *p = arg;
377 xfs_fsop_attrlist_handlereq_t al_hreq;
378 struct dentry *dentry;
379 char *kbuf;
380
381 if (!capable(CAP_SYS_ADMIN))
382 return -EPERM;
383 if (copy_from_user(&al_hreq, arg, sizeof(xfs_fsop_attrlist_handlereq_t)))
384 return -EFAULT;
385 if (al_hreq.buflen < sizeof(struct attrlist) ||
386 al_hreq.buflen > XFS_XATTR_LIST_MAX)
387 return -EINVAL;
388
389 /*
390 * Reject flags, only allow namespaces.
391 */
392 if (al_hreq.flags & ~(ATTR_ROOT | ATTR_SECURE))
393 return -EINVAL;
394
395 dentry = xfs_handlereq_to_dentry(parfilp, &al_hreq.hreq);
396 if (IS_ERR(dentry))
397 return PTR_ERR(dentry);
398
399 kbuf = kmem_zalloc_large(al_hreq.buflen, 0);
400 if (!kbuf)
401 goto out_dput;
402
403 cursor = (attrlist_cursor_kern_t *)&al_hreq.pos;
404 error = xfs_attr_list(XFS_I(d_inode(dentry)), kbuf, al_hreq.buflen,
405 al_hreq.flags, cursor);
406 if (error)
407 goto out_kfree;
408
409 if (copy_to_user(&p->pos, cursor, sizeof(attrlist_cursor_kern_t))) {
410 error = -EFAULT;
411 goto out_kfree;
412 }
413
414 if (copy_to_user(al_hreq.buffer, kbuf, al_hreq.buflen))
415 error = -EFAULT;
416
417 out_kfree:
418 kmem_free(kbuf);
419 out_dput:
420 dput(dentry);
421 return error;
422 }
423
424 int
xfs_attrmulti_attr_get(struct inode * inode,unsigned char * name,unsigned char __user * ubuf,uint32_t * len,uint32_t flags)425 xfs_attrmulti_attr_get(
426 struct inode *inode,
427 unsigned char *name,
428 unsigned char __user *ubuf,
429 uint32_t *len,
430 uint32_t flags)
431 {
432 unsigned char *kbuf;
433 int error = -EFAULT;
434
435 if (*len > XFS_XATTR_SIZE_MAX)
436 return -EINVAL;
437 kbuf = kmem_zalloc_large(*len, 0);
438 if (!kbuf)
439 return -ENOMEM;
440
441 error = xfs_attr_get(XFS_I(inode), name, &kbuf, (int *)len, flags);
442 if (error)
443 goto out_kfree;
444
445 if (copy_to_user(ubuf, kbuf, *len))
446 error = -EFAULT;
447
448 out_kfree:
449 kmem_free(kbuf);
450 return error;
451 }
452
453 int
xfs_attrmulti_attr_set(struct inode * inode,unsigned char * name,const unsigned char __user * ubuf,uint32_t len,uint32_t flags)454 xfs_attrmulti_attr_set(
455 struct inode *inode,
456 unsigned char *name,
457 const unsigned char __user *ubuf,
458 uint32_t len,
459 uint32_t flags)
460 {
461 unsigned char *kbuf;
462 int error;
463
464 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
465 return -EPERM;
466 if (len > XFS_XATTR_SIZE_MAX)
467 return -EINVAL;
468
469 kbuf = memdup_user(ubuf, len);
470 if (IS_ERR(kbuf))
471 return PTR_ERR(kbuf);
472
473 error = xfs_attr_set(XFS_I(inode), name, kbuf, len, flags);
474 if (!error)
475 xfs_forget_acl(inode, name, flags);
476 kfree(kbuf);
477 return error;
478 }
479
480 int
xfs_attrmulti_attr_remove(struct inode * inode,unsigned char * name,uint32_t flags)481 xfs_attrmulti_attr_remove(
482 struct inode *inode,
483 unsigned char *name,
484 uint32_t flags)
485 {
486 int error;
487
488 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
489 return -EPERM;
490 error = xfs_attr_remove(XFS_I(inode), name, flags);
491 if (!error)
492 xfs_forget_acl(inode, name, flags);
493 return error;
494 }
495
496 STATIC int
xfs_attrmulti_by_handle(struct file * parfilp,void __user * arg)497 xfs_attrmulti_by_handle(
498 struct file *parfilp,
499 void __user *arg)
500 {
501 int error;
502 xfs_attr_multiop_t *ops;
503 xfs_fsop_attrmulti_handlereq_t am_hreq;
504 struct dentry *dentry;
505 unsigned int i, size;
506 unsigned char *attr_name;
507
508 if (!capable(CAP_SYS_ADMIN))
509 return -EPERM;
510 if (copy_from_user(&am_hreq, arg, sizeof(xfs_fsop_attrmulti_handlereq_t)))
511 return -EFAULT;
512
513 /* overflow check */
514 if (am_hreq.opcount >= INT_MAX / sizeof(xfs_attr_multiop_t))
515 return -E2BIG;
516
517 dentry = xfs_handlereq_to_dentry(parfilp, &am_hreq.hreq);
518 if (IS_ERR(dentry))
519 return PTR_ERR(dentry);
520
521 error = -E2BIG;
522 size = am_hreq.opcount * sizeof(xfs_attr_multiop_t);
523 if (!size || size > 16 * PAGE_SIZE)
524 goto out_dput;
525
526 ops = memdup_user(am_hreq.ops, size);
527 if (IS_ERR(ops)) {
528 error = PTR_ERR(ops);
529 goto out_dput;
530 }
531
532 error = -ENOMEM;
533 attr_name = kmalloc(MAXNAMELEN, GFP_KERNEL);
534 if (!attr_name)
535 goto out_kfree_ops;
536
537 error = 0;
538 for (i = 0; i < am_hreq.opcount; i++) {
539 ops[i].am_flags &= ~ATTR_KERNEL_FLAGS;
540
541 ops[i].am_error = strncpy_from_user((char *)attr_name,
542 ops[i].am_attrname, MAXNAMELEN);
543 if (ops[i].am_error == 0 || ops[i].am_error == MAXNAMELEN)
544 error = -ERANGE;
545 if (ops[i].am_error < 0)
546 break;
547
548 switch (ops[i].am_opcode) {
549 case ATTR_OP_GET:
550 ops[i].am_error = xfs_attrmulti_attr_get(
551 d_inode(dentry), attr_name,
552 ops[i].am_attrvalue, &ops[i].am_length,
553 ops[i].am_flags);
554 break;
555 case ATTR_OP_SET:
556 ops[i].am_error = mnt_want_write_file(parfilp);
557 if (ops[i].am_error)
558 break;
559 ops[i].am_error = xfs_attrmulti_attr_set(
560 d_inode(dentry), attr_name,
561 ops[i].am_attrvalue, ops[i].am_length,
562 ops[i].am_flags);
563 mnt_drop_write_file(parfilp);
564 break;
565 case ATTR_OP_REMOVE:
566 ops[i].am_error = mnt_want_write_file(parfilp);
567 if (ops[i].am_error)
568 break;
569 ops[i].am_error = xfs_attrmulti_attr_remove(
570 d_inode(dentry), attr_name,
571 ops[i].am_flags);
572 mnt_drop_write_file(parfilp);
573 break;
574 default:
575 ops[i].am_error = -EINVAL;
576 }
577 }
578
579 if (copy_to_user(am_hreq.ops, ops, size))
580 error = -EFAULT;
581
582 kfree(attr_name);
583 out_kfree_ops:
584 kfree(ops);
585 out_dput:
586 dput(dentry);
587 return error;
588 }
589
590 int
xfs_ioc_space(struct file * filp,unsigned int cmd,xfs_flock64_t * bf)591 xfs_ioc_space(
592 struct file *filp,
593 unsigned int cmd,
594 xfs_flock64_t *bf)
595 {
596 struct inode *inode = file_inode(filp);
597 struct xfs_inode *ip = XFS_I(inode);
598 struct iattr iattr;
599 enum xfs_prealloc_flags flags = 0;
600 uint iolock = XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL;
601 int error;
602
603 if (inode->i_flags & (S_IMMUTABLE|S_APPEND))
604 return -EPERM;
605
606 if (!(filp->f_mode & FMODE_WRITE))
607 return -EBADF;
608
609 if (!S_ISREG(inode->i_mode))
610 return -EINVAL;
611
612 if (filp->f_flags & O_DSYNC)
613 flags |= XFS_PREALLOC_SYNC;
614 if (filp->f_mode & FMODE_NOCMTIME)
615 flags |= XFS_PREALLOC_INVISIBLE;
616
617 error = mnt_want_write_file(filp);
618 if (error)
619 return error;
620
621 xfs_ilock(ip, iolock);
622 error = xfs_break_layouts(inode, &iolock, BREAK_UNMAP);
623 if (error)
624 goto out_unlock;
625
626 switch (bf->l_whence) {
627 case 0: /*SEEK_SET*/
628 break;
629 case 1: /*SEEK_CUR*/
630 bf->l_start += filp->f_pos;
631 break;
632 case 2: /*SEEK_END*/
633 bf->l_start += XFS_ISIZE(ip);
634 break;
635 default:
636 error = -EINVAL;
637 goto out_unlock;
638 }
639
640 /*
641 * length of <= 0 for resv/unresv/zero is invalid. length for
642 * alloc/free is ignored completely and we have no idea what userspace
643 * might have set it to, so set it to zero to allow range
644 * checks to pass.
645 */
646 switch (cmd) {
647 case XFS_IOC_ZERO_RANGE:
648 case XFS_IOC_RESVSP:
649 case XFS_IOC_RESVSP64:
650 case XFS_IOC_UNRESVSP:
651 case XFS_IOC_UNRESVSP64:
652 if (bf->l_len <= 0) {
653 error = -EINVAL;
654 goto out_unlock;
655 }
656 break;
657 default:
658 bf->l_len = 0;
659 break;
660 }
661
662 if (bf->l_start < 0 ||
663 bf->l_start > inode->i_sb->s_maxbytes ||
664 bf->l_start + bf->l_len < 0 ||
665 bf->l_start + bf->l_len >= inode->i_sb->s_maxbytes) {
666 error = -EINVAL;
667 goto out_unlock;
668 }
669
670 /*
671 * Must wait for all AIO to complete before we continue as AIO can
672 * change the file size on completion without holding any locks we
673 * currently hold. We must do this first because AIO can update both
674 * the on disk and in memory inode sizes, and the operations that follow
675 * require the in-memory size to be fully up-to-date.
676 */
677 inode_dio_wait(inode);
678
679 /*
680 * Now that AIO and DIO has drained we can flush and (if necessary)
681 * invalidate the cached range over the first operation we are about to
682 * run. We include zero range here because it starts with a hole punch
683 * over the target range.
684 */
685 switch (cmd) {
686 case XFS_IOC_ZERO_RANGE:
687 case XFS_IOC_UNRESVSP:
688 case XFS_IOC_UNRESVSP64:
689 error = xfs_flush_unmap_range(ip, bf->l_start, bf->l_len);
690 if (error)
691 goto out_unlock;
692 break;
693 }
694
695 switch (cmd) {
696 case XFS_IOC_ZERO_RANGE:
697 flags |= XFS_PREALLOC_SET;
698 error = xfs_zero_file_space(ip, bf->l_start, bf->l_len);
699 break;
700 case XFS_IOC_RESVSP:
701 case XFS_IOC_RESVSP64:
702 flags |= XFS_PREALLOC_SET;
703 error = xfs_alloc_file_space(ip, bf->l_start, bf->l_len,
704 XFS_BMAPI_PREALLOC);
705 break;
706 case XFS_IOC_UNRESVSP:
707 case XFS_IOC_UNRESVSP64:
708 error = xfs_free_file_space(ip, bf->l_start, bf->l_len);
709 break;
710 case XFS_IOC_ALLOCSP:
711 case XFS_IOC_ALLOCSP64:
712 case XFS_IOC_FREESP:
713 case XFS_IOC_FREESP64:
714 flags |= XFS_PREALLOC_CLEAR;
715 if (bf->l_start > XFS_ISIZE(ip)) {
716 error = xfs_alloc_file_space(ip, XFS_ISIZE(ip),
717 bf->l_start - XFS_ISIZE(ip),
718 XFS_BMAPI_PREALLOC);
719 if (error)
720 goto out_unlock;
721 }
722
723 iattr.ia_valid = ATTR_SIZE;
724 iattr.ia_size = bf->l_start;
725
726 error = xfs_vn_setattr_size(file_dentry(filp), &iattr);
727 break;
728 default:
729 ASSERT(0);
730 error = -EINVAL;
731 }
732
733 if (error)
734 goto out_unlock;
735
736 error = xfs_update_prealloc_flags(ip, flags);
737
738 out_unlock:
739 xfs_iunlock(ip, iolock);
740 mnt_drop_write_file(filp);
741 return error;
742 }
743
744 /* Return 0 on success or positive error */
745 int
xfs_fsbulkstat_one_fmt(struct xfs_ibulk * breq,const struct xfs_bulkstat * bstat)746 xfs_fsbulkstat_one_fmt(
747 struct xfs_ibulk *breq,
748 const struct xfs_bulkstat *bstat)
749 {
750 struct xfs_bstat bs1;
751
752 xfs_bulkstat_to_bstat(breq->mp, &bs1, bstat);
753 if (copy_to_user(breq->ubuffer, &bs1, sizeof(bs1)))
754 return -EFAULT;
755 return xfs_ibulk_advance(breq, sizeof(struct xfs_bstat));
756 }
757
758 int
xfs_fsinumbers_fmt(struct xfs_ibulk * breq,const struct xfs_inumbers * igrp)759 xfs_fsinumbers_fmt(
760 struct xfs_ibulk *breq,
761 const struct xfs_inumbers *igrp)
762 {
763 struct xfs_inogrp ig1;
764
765 xfs_inumbers_to_inogrp(&ig1, igrp);
766 if (copy_to_user(breq->ubuffer, &ig1, sizeof(struct xfs_inogrp)))
767 return -EFAULT;
768 return xfs_ibulk_advance(breq, sizeof(struct xfs_inogrp));
769 }
770
771 STATIC int
xfs_ioc_fsbulkstat(xfs_mount_t * mp,unsigned int cmd,void __user * arg)772 xfs_ioc_fsbulkstat(
773 xfs_mount_t *mp,
774 unsigned int cmd,
775 void __user *arg)
776 {
777 struct xfs_fsop_bulkreq bulkreq;
778 struct xfs_ibulk breq = {
779 .mp = mp,
780 .ocount = 0,
781 };
782 xfs_ino_t lastino;
783 int error;
784
785 /* done = 1 if there are more stats to get and if bulkstat */
786 /* should be called again (unused here, but used in dmapi) */
787
788 if (!capable(CAP_SYS_ADMIN))
789 return -EPERM;
790
791 if (XFS_FORCED_SHUTDOWN(mp))
792 return -EIO;
793
794 if (copy_from_user(&bulkreq, arg, sizeof(struct xfs_fsop_bulkreq)))
795 return -EFAULT;
796
797 if (copy_from_user(&lastino, bulkreq.lastip, sizeof(__s64)))
798 return -EFAULT;
799
800 if (bulkreq.icount <= 0)
801 return -EINVAL;
802
803 if (bulkreq.ubuffer == NULL)
804 return -EINVAL;
805
806 breq.ubuffer = bulkreq.ubuffer;
807 breq.icount = bulkreq.icount;
808
809 /*
810 * FSBULKSTAT_SINGLE expects that *lastip contains the inode number
811 * that we want to stat. However, FSINUMBERS and FSBULKSTAT expect
812 * that *lastip contains either zero or the number of the last inode to
813 * be examined by the previous call and return results starting with
814 * the next inode after that. The new bulk request back end functions
815 * take the inode to start with, so we have to compute the startino
816 * parameter from lastino to maintain correct function. lastino == 0
817 * is a special case because it has traditionally meant "first inode
818 * in filesystem".
819 */
820 if (cmd == XFS_IOC_FSINUMBERS) {
821 breq.startino = lastino ? lastino + 1 : 0;
822 error = xfs_inumbers(&breq, xfs_fsinumbers_fmt);
823 lastino = breq.startino - 1;
824 } else if (cmd == XFS_IOC_FSBULKSTAT_SINGLE) {
825 breq.startino = lastino;
826 breq.icount = 1;
827 error = xfs_bulkstat_one(&breq, xfs_fsbulkstat_one_fmt);
828 } else { /* XFS_IOC_FSBULKSTAT */
829 breq.startino = lastino ? lastino + 1 : 0;
830 error = xfs_bulkstat(&breq, xfs_fsbulkstat_one_fmt);
831 lastino = breq.startino - 1;
832 }
833
834 if (error)
835 return error;
836
837 if (bulkreq.lastip != NULL &&
838 copy_to_user(bulkreq.lastip, &lastino, sizeof(xfs_ino_t)))
839 return -EFAULT;
840
841 if (bulkreq.ocount != NULL &&
842 copy_to_user(bulkreq.ocount, &breq.ocount, sizeof(__s32)))
843 return -EFAULT;
844
845 return 0;
846 }
847
848 /* Return 0 on success or positive error */
849 static int
xfs_bulkstat_fmt(struct xfs_ibulk * breq,const struct xfs_bulkstat * bstat)850 xfs_bulkstat_fmt(
851 struct xfs_ibulk *breq,
852 const struct xfs_bulkstat *bstat)
853 {
854 if (copy_to_user(breq->ubuffer, bstat, sizeof(struct xfs_bulkstat)))
855 return -EFAULT;
856 return xfs_ibulk_advance(breq, sizeof(struct xfs_bulkstat));
857 }
858
859 /*
860 * Check the incoming bulk request @hdr from userspace and initialize the
861 * internal @breq bulk request appropriately. Returns 0 if the bulk request
862 * should proceed; -ECANCELED if there's nothing to do; or the usual
863 * negative error code.
864 */
865 static int
xfs_bulk_ireq_setup(struct xfs_mount * mp,struct xfs_bulk_ireq * hdr,struct xfs_ibulk * breq,void __user * ubuffer)866 xfs_bulk_ireq_setup(
867 struct xfs_mount *mp,
868 struct xfs_bulk_ireq *hdr,
869 struct xfs_ibulk *breq,
870 void __user *ubuffer)
871 {
872 if (hdr->icount == 0 ||
873 (hdr->flags & ~XFS_BULK_IREQ_FLAGS_ALL) ||
874 memchr_inv(hdr->reserved, 0, sizeof(hdr->reserved)))
875 return -EINVAL;
876
877 breq->startino = hdr->ino;
878 breq->ubuffer = ubuffer;
879 breq->icount = hdr->icount;
880 breq->ocount = 0;
881 breq->flags = 0;
882
883 /*
884 * The @ino parameter is a special value, so we must look it up here.
885 * We're not allowed to have IREQ_AGNO, and we only return one inode
886 * worth of data.
887 */
888 if (hdr->flags & XFS_BULK_IREQ_SPECIAL) {
889 if (hdr->flags & XFS_BULK_IREQ_AGNO)
890 return -EINVAL;
891
892 switch (hdr->ino) {
893 case XFS_BULK_IREQ_SPECIAL_ROOT:
894 hdr->ino = mp->m_sb.sb_rootino;
895 break;
896 default:
897 return -EINVAL;
898 }
899 breq->icount = 1;
900 }
901
902 /*
903 * The IREQ_AGNO flag means that we only want results from a given AG.
904 * If @hdr->ino is zero, we start iterating in that AG. If @hdr->ino is
905 * beyond the specified AG then we return no results.
906 */
907 if (hdr->flags & XFS_BULK_IREQ_AGNO) {
908 if (hdr->agno >= mp->m_sb.sb_agcount)
909 return -EINVAL;
910
911 if (breq->startino == 0)
912 breq->startino = XFS_AGINO_TO_INO(mp, hdr->agno, 0);
913 else if (XFS_INO_TO_AGNO(mp, breq->startino) < hdr->agno)
914 return -EINVAL;
915
916 breq->flags |= XFS_IBULK_SAME_AG;
917
918 /* Asking for an inode past the end of the AG? We're done! */
919 if (XFS_INO_TO_AGNO(mp, breq->startino) > hdr->agno)
920 return -ECANCELED;
921 } else if (hdr->agno)
922 return -EINVAL;
923
924 /* Asking for an inode past the end of the FS? We're done! */
925 if (XFS_INO_TO_AGNO(mp, breq->startino) >= mp->m_sb.sb_agcount)
926 return -ECANCELED;
927
928 return 0;
929 }
930
931 /*
932 * Update the userspace bulk request @hdr to reflect the end state of the
933 * internal bulk request @breq.
934 */
935 static void
xfs_bulk_ireq_teardown(struct xfs_bulk_ireq * hdr,struct xfs_ibulk * breq)936 xfs_bulk_ireq_teardown(
937 struct xfs_bulk_ireq *hdr,
938 struct xfs_ibulk *breq)
939 {
940 hdr->ino = breq->startino;
941 hdr->ocount = breq->ocount;
942 }
943
944 /* Handle the v5 bulkstat ioctl. */
945 STATIC int
xfs_ioc_bulkstat(struct xfs_mount * mp,unsigned int cmd,struct xfs_bulkstat_req __user * arg)946 xfs_ioc_bulkstat(
947 struct xfs_mount *mp,
948 unsigned int cmd,
949 struct xfs_bulkstat_req __user *arg)
950 {
951 struct xfs_bulk_ireq hdr;
952 struct xfs_ibulk breq = {
953 .mp = mp,
954 };
955 int error;
956
957 if (!capable(CAP_SYS_ADMIN))
958 return -EPERM;
959
960 if (XFS_FORCED_SHUTDOWN(mp))
961 return -EIO;
962
963 if (copy_from_user(&hdr, &arg->hdr, sizeof(hdr)))
964 return -EFAULT;
965
966 error = xfs_bulk_ireq_setup(mp, &hdr, &breq, arg->bulkstat);
967 if (error == -ECANCELED)
968 goto out_teardown;
969 if (error < 0)
970 return error;
971
972 error = xfs_bulkstat(&breq, xfs_bulkstat_fmt);
973 if (error)
974 return error;
975
976 out_teardown:
977 xfs_bulk_ireq_teardown(&hdr, &breq);
978 if (copy_to_user(&arg->hdr, &hdr, sizeof(hdr)))
979 return -EFAULT;
980
981 return 0;
982 }
983
984 STATIC int
xfs_inumbers_fmt(struct xfs_ibulk * breq,const struct xfs_inumbers * igrp)985 xfs_inumbers_fmt(
986 struct xfs_ibulk *breq,
987 const struct xfs_inumbers *igrp)
988 {
989 if (copy_to_user(breq->ubuffer, igrp, sizeof(struct xfs_inumbers)))
990 return -EFAULT;
991 return xfs_ibulk_advance(breq, sizeof(struct xfs_inumbers));
992 }
993
994 /* Handle the v5 inumbers ioctl. */
995 STATIC int
xfs_ioc_inumbers(struct xfs_mount * mp,unsigned int cmd,struct xfs_inumbers_req __user * arg)996 xfs_ioc_inumbers(
997 struct xfs_mount *mp,
998 unsigned int cmd,
999 struct xfs_inumbers_req __user *arg)
1000 {
1001 struct xfs_bulk_ireq hdr;
1002 struct xfs_ibulk breq = {
1003 .mp = mp,
1004 };
1005 int error;
1006
1007 if (!capable(CAP_SYS_ADMIN))
1008 return -EPERM;
1009
1010 if (XFS_FORCED_SHUTDOWN(mp))
1011 return -EIO;
1012
1013 if (copy_from_user(&hdr, &arg->hdr, sizeof(hdr)))
1014 return -EFAULT;
1015
1016 error = xfs_bulk_ireq_setup(mp, &hdr, &breq, arg->inumbers);
1017 if (error == -ECANCELED)
1018 goto out_teardown;
1019 if (error < 0)
1020 return error;
1021
1022 error = xfs_inumbers(&breq, xfs_inumbers_fmt);
1023 if (error)
1024 return error;
1025
1026 out_teardown:
1027 xfs_bulk_ireq_teardown(&hdr, &breq);
1028 if (copy_to_user(&arg->hdr, &hdr, sizeof(hdr)))
1029 return -EFAULT;
1030
1031 return 0;
1032 }
1033
1034 STATIC int
xfs_ioc_fsgeometry(struct xfs_mount * mp,void __user * arg,int struct_version)1035 xfs_ioc_fsgeometry(
1036 struct xfs_mount *mp,
1037 void __user *arg,
1038 int struct_version)
1039 {
1040 struct xfs_fsop_geom fsgeo;
1041 size_t len;
1042
1043 xfs_fs_geometry(&mp->m_sb, &fsgeo, struct_version);
1044
1045 if (struct_version <= 3)
1046 len = sizeof(struct xfs_fsop_geom_v1);
1047 else if (struct_version == 4)
1048 len = sizeof(struct xfs_fsop_geom_v4);
1049 else {
1050 xfs_fsop_geom_health(mp, &fsgeo);
1051 len = sizeof(fsgeo);
1052 }
1053
1054 if (copy_to_user(arg, &fsgeo, len))
1055 return -EFAULT;
1056 return 0;
1057 }
1058
1059 STATIC int
xfs_ioc_ag_geometry(struct xfs_mount * mp,void __user * arg)1060 xfs_ioc_ag_geometry(
1061 struct xfs_mount *mp,
1062 void __user *arg)
1063 {
1064 struct xfs_ag_geometry ageo;
1065 int error;
1066
1067 if (copy_from_user(&ageo, arg, sizeof(ageo)))
1068 return -EFAULT;
1069 if (ageo.ag_flags)
1070 return -EINVAL;
1071 if (memchr_inv(&ageo.ag_reserved, 0, sizeof(ageo.ag_reserved)))
1072 return -EINVAL;
1073
1074 error = xfs_ag_get_geometry(mp, ageo.ag_number, &ageo);
1075 if (error)
1076 return error;
1077
1078 if (copy_to_user(arg, &ageo, sizeof(ageo)))
1079 return -EFAULT;
1080 return 0;
1081 }
1082
1083 /*
1084 * Linux extended inode flags interface.
1085 */
1086
1087 STATIC unsigned int
xfs_merge_ioc_xflags(unsigned int flags,unsigned int start)1088 xfs_merge_ioc_xflags(
1089 unsigned int flags,
1090 unsigned int start)
1091 {
1092 unsigned int xflags = start;
1093
1094 if (flags & FS_IMMUTABLE_FL)
1095 xflags |= FS_XFLAG_IMMUTABLE;
1096 else
1097 xflags &= ~FS_XFLAG_IMMUTABLE;
1098 if (flags & FS_APPEND_FL)
1099 xflags |= FS_XFLAG_APPEND;
1100 else
1101 xflags &= ~FS_XFLAG_APPEND;
1102 if (flags & FS_SYNC_FL)
1103 xflags |= FS_XFLAG_SYNC;
1104 else
1105 xflags &= ~FS_XFLAG_SYNC;
1106 if (flags & FS_NOATIME_FL)
1107 xflags |= FS_XFLAG_NOATIME;
1108 else
1109 xflags &= ~FS_XFLAG_NOATIME;
1110 if (flags & FS_NODUMP_FL)
1111 xflags |= FS_XFLAG_NODUMP;
1112 else
1113 xflags &= ~FS_XFLAG_NODUMP;
1114
1115 return xflags;
1116 }
1117
1118 STATIC unsigned int
xfs_di2lxflags(uint16_t di_flags)1119 xfs_di2lxflags(
1120 uint16_t di_flags)
1121 {
1122 unsigned int flags = 0;
1123
1124 if (di_flags & XFS_DIFLAG_IMMUTABLE)
1125 flags |= FS_IMMUTABLE_FL;
1126 if (di_flags & XFS_DIFLAG_APPEND)
1127 flags |= FS_APPEND_FL;
1128 if (di_flags & XFS_DIFLAG_SYNC)
1129 flags |= FS_SYNC_FL;
1130 if (di_flags & XFS_DIFLAG_NOATIME)
1131 flags |= FS_NOATIME_FL;
1132 if (di_flags & XFS_DIFLAG_NODUMP)
1133 flags |= FS_NODUMP_FL;
1134 return flags;
1135 }
1136
1137 static void
xfs_fill_fsxattr(struct xfs_inode * ip,bool attr,struct fsxattr * fa)1138 xfs_fill_fsxattr(
1139 struct xfs_inode *ip,
1140 bool attr,
1141 struct fsxattr *fa)
1142 {
1143 simple_fill_fsxattr(fa, xfs_ip2xflags(ip));
1144 fa->fsx_extsize = ip->i_d.di_extsize << ip->i_mount->m_sb.sb_blocklog;
1145 fa->fsx_cowextsize = ip->i_d.di_cowextsize <<
1146 ip->i_mount->m_sb.sb_blocklog;
1147 fa->fsx_projid = ip->i_d.di_projid;
1148
1149 if (attr) {
1150 if (ip->i_afp) {
1151 if (ip->i_afp->if_flags & XFS_IFEXTENTS)
1152 fa->fsx_nextents = xfs_iext_count(ip->i_afp);
1153 else
1154 fa->fsx_nextents = ip->i_d.di_anextents;
1155 } else
1156 fa->fsx_nextents = 0;
1157 } else {
1158 if (ip->i_df.if_flags & XFS_IFEXTENTS)
1159 fa->fsx_nextents = xfs_iext_count(&ip->i_df);
1160 else
1161 fa->fsx_nextents = ip->i_d.di_nextents;
1162 }
1163 }
1164
1165 STATIC int
xfs_ioc_fsgetxattr(xfs_inode_t * ip,int attr,void __user * arg)1166 xfs_ioc_fsgetxattr(
1167 xfs_inode_t *ip,
1168 int attr,
1169 void __user *arg)
1170 {
1171 struct fsxattr fa;
1172
1173 xfs_ilock(ip, XFS_ILOCK_SHARED);
1174 xfs_fill_fsxattr(ip, attr, &fa);
1175 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1176
1177 if (copy_to_user(arg, &fa, sizeof(fa)))
1178 return -EFAULT;
1179 return 0;
1180 }
1181
1182 STATIC uint16_t
xfs_flags2diflags(struct xfs_inode * ip,unsigned int xflags)1183 xfs_flags2diflags(
1184 struct xfs_inode *ip,
1185 unsigned int xflags)
1186 {
1187 /* can't set PREALLOC this way, just preserve it */
1188 uint16_t di_flags =
1189 (ip->i_d.di_flags & XFS_DIFLAG_PREALLOC);
1190
1191 if (xflags & FS_XFLAG_IMMUTABLE)
1192 di_flags |= XFS_DIFLAG_IMMUTABLE;
1193 if (xflags & FS_XFLAG_APPEND)
1194 di_flags |= XFS_DIFLAG_APPEND;
1195 if (xflags & FS_XFLAG_SYNC)
1196 di_flags |= XFS_DIFLAG_SYNC;
1197 if (xflags & FS_XFLAG_NOATIME)
1198 di_flags |= XFS_DIFLAG_NOATIME;
1199 if (xflags & FS_XFLAG_NODUMP)
1200 di_flags |= XFS_DIFLAG_NODUMP;
1201 if (xflags & FS_XFLAG_NODEFRAG)
1202 di_flags |= XFS_DIFLAG_NODEFRAG;
1203 if (xflags & FS_XFLAG_FILESTREAM)
1204 di_flags |= XFS_DIFLAG_FILESTREAM;
1205 if (S_ISDIR(VFS_I(ip)->i_mode)) {
1206 if (xflags & FS_XFLAG_RTINHERIT)
1207 di_flags |= XFS_DIFLAG_RTINHERIT;
1208 if (xflags & FS_XFLAG_NOSYMLINKS)
1209 di_flags |= XFS_DIFLAG_NOSYMLINKS;
1210 if (xflags & FS_XFLAG_EXTSZINHERIT)
1211 di_flags |= XFS_DIFLAG_EXTSZINHERIT;
1212 if (xflags & FS_XFLAG_PROJINHERIT)
1213 di_flags |= XFS_DIFLAG_PROJINHERIT;
1214 } else if (S_ISREG(VFS_I(ip)->i_mode)) {
1215 if (xflags & FS_XFLAG_REALTIME)
1216 di_flags |= XFS_DIFLAG_REALTIME;
1217 if (xflags & FS_XFLAG_EXTSIZE)
1218 di_flags |= XFS_DIFLAG_EXTSIZE;
1219 }
1220
1221 return di_flags;
1222 }
1223
1224 STATIC uint64_t
xfs_flags2diflags2(struct xfs_inode * ip,unsigned int xflags)1225 xfs_flags2diflags2(
1226 struct xfs_inode *ip,
1227 unsigned int xflags)
1228 {
1229 uint64_t di_flags2 =
1230 (ip->i_d.di_flags2 & XFS_DIFLAG2_REFLINK);
1231
1232 if (xflags & FS_XFLAG_DAX)
1233 di_flags2 |= XFS_DIFLAG2_DAX;
1234 if (xflags & FS_XFLAG_COWEXTSIZE)
1235 di_flags2 |= XFS_DIFLAG2_COWEXTSIZE;
1236
1237 return di_flags2;
1238 }
1239
1240 STATIC void
xfs_diflags_to_linux(struct xfs_inode * ip)1241 xfs_diflags_to_linux(
1242 struct xfs_inode *ip)
1243 {
1244 struct inode *inode = VFS_I(ip);
1245 unsigned int xflags = xfs_ip2xflags(ip);
1246
1247 if (xflags & FS_XFLAG_IMMUTABLE)
1248 inode->i_flags |= S_IMMUTABLE;
1249 else
1250 inode->i_flags &= ~S_IMMUTABLE;
1251 if (xflags & FS_XFLAG_APPEND)
1252 inode->i_flags |= S_APPEND;
1253 else
1254 inode->i_flags &= ~S_APPEND;
1255 if (xflags & FS_XFLAG_SYNC)
1256 inode->i_flags |= S_SYNC;
1257 else
1258 inode->i_flags &= ~S_SYNC;
1259 if (xflags & FS_XFLAG_NOATIME)
1260 inode->i_flags |= S_NOATIME;
1261 else
1262 inode->i_flags &= ~S_NOATIME;
1263 #if 0 /* disabled until the flag switching races are sorted out */
1264 if (xflags & FS_XFLAG_DAX)
1265 inode->i_flags |= S_DAX;
1266 else
1267 inode->i_flags &= ~S_DAX;
1268 #endif
1269 }
1270
1271 static int
xfs_ioctl_setattr_xflags(struct xfs_trans * tp,struct xfs_inode * ip,struct fsxattr * fa)1272 xfs_ioctl_setattr_xflags(
1273 struct xfs_trans *tp,
1274 struct xfs_inode *ip,
1275 struct fsxattr *fa)
1276 {
1277 struct xfs_mount *mp = ip->i_mount;
1278 uint64_t di_flags2;
1279
1280 /* Can't change realtime flag if any extents are allocated. */
1281 if ((ip->i_d.di_nextents || ip->i_delayed_blks) &&
1282 XFS_IS_REALTIME_INODE(ip) != (fa->fsx_xflags & FS_XFLAG_REALTIME))
1283 return -EINVAL;
1284
1285 /* If realtime flag is set then must have realtime device */
1286 if (fa->fsx_xflags & FS_XFLAG_REALTIME) {
1287 if (mp->m_sb.sb_rblocks == 0 || mp->m_sb.sb_rextsize == 0 ||
1288 (ip->i_d.di_extsize % mp->m_sb.sb_rextsize))
1289 return -EINVAL;
1290 }
1291
1292 /* Clear reflink if we are actually able to set the rt flag. */
1293 if ((fa->fsx_xflags & FS_XFLAG_REALTIME) && xfs_is_reflink_inode(ip))
1294 ip->i_d.di_flags2 &= ~XFS_DIFLAG2_REFLINK;
1295
1296 /* Don't allow us to set DAX mode for a reflinked file for now. */
1297 if ((fa->fsx_xflags & FS_XFLAG_DAX) && xfs_is_reflink_inode(ip))
1298 return -EINVAL;
1299
1300 /* diflags2 only valid for v3 inodes. */
1301 di_flags2 = xfs_flags2diflags2(ip, fa->fsx_xflags);
1302 if (di_flags2 && !xfs_sb_version_has_v3inode(&mp->m_sb))
1303 return -EINVAL;
1304
1305 ip->i_d.di_flags = xfs_flags2diflags(ip, fa->fsx_xflags);
1306 ip->i_d.di_flags2 = di_flags2;
1307
1308 xfs_diflags_to_linux(ip);
1309 xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
1310 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1311 XFS_STATS_INC(mp, xs_ig_attrchg);
1312 return 0;
1313 }
1314
1315 /*
1316 * If we are changing DAX flags, we have to ensure the file is clean and any
1317 * cached objects in the address space are invalidated and removed. This
1318 * requires us to lock out other IO and page faults similar to a truncate
1319 * operation. The locks need to be held until the transaction has been committed
1320 * so that the cache invalidation is atomic with respect to the DAX flag
1321 * manipulation.
1322 */
1323 static int
xfs_ioctl_setattr_dax_invalidate(struct xfs_inode * ip,struct fsxattr * fa,int * join_flags)1324 xfs_ioctl_setattr_dax_invalidate(
1325 struct xfs_inode *ip,
1326 struct fsxattr *fa,
1327 int *join_flags)
1328 {
1329 struct inode *inode = VFS_I(ip);
1330 struct super_block *sb = inode->i_sb;
1331 int error;
1332
1333 *join_flags = 0;
1334
1335 /*
1336 * It is only valid to set the DAX flag on regular files and
1337 * directories on filesystems where the block size is equal to the page
1338 * size. On directories it serves as an inherited hint so we don't
1339 * have to check the device for dax support or flush pagecache.
1340 */
1341 if (fa->fsx_xflags & FS_XFLAG_DAX) {
1342 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)))
1343 return -EINVAL;
1344 if (!bdev_dax_supported(xfs_find_bdev_for_inode(VFS_I(ip)),
1345 sb->s_blocksize))
1346 return -EINVAL;
1347 }
1348
1349 /* If the DAX state is not changing, we have nothing to do here. */
1350 if ((fa->fsx_xflags & FS_XFLAG_DAX) && IS_DAX(inode))
1351 return 0;
1352 if (!(fa->fsx_xflags & FS_XFLAG_DAX) && !IS_DAX(inode))
1353 return 0;
1354
1355 if (S_ISDIR(inode->i_mode))
1356 return 0;
1357
1358 /* lock, flush and invalidate mapping in preparation for flag change */
1359 xfs_ilock(ip, XFS_MMAPLOCK_EXCL | XFS_IOLOCK_EXCL);
1360 error = filemap_write_and_wait(inode->i_mapping);
1361 if (error)
1362 goto out_unlock;
1363 error = invalidate_inode_pages2(inode->i_mapping);
1364 if (error)
1365 goto out_unlock;
1366
1367 *join_flags = XFS_MMAPLOCK_EXCL | XFS_IOLOCK_EXCL;
1368 return 0;
1369
1370 out_unlock:
1371 xfs_iunlock(ip, XFS_MMAPLOCK_EXCL | XFS_IOLOCK_EXCL);
1372 return error;
1373
1374 }
1375
1376 /*
1377 * Set up the transaction structure for the setattr operation, checking that we
1378 * have permission to do so. On success, return a clean transaction and the
1379 * inode locked exclusively ready for further operation specific checks. On
1380 * failure, return an error without modifying or locking the inode.
1381 *
1382 * The inode might already be IO locked on call. If this is the case, it is
1383 * indicated in @join_flags and we take full responsibility for ensuring they
1384 * are unlocked from now on. Hence if we have an error here, we still have to
1385 * unlock them. Otherwise, once they are joined to the transaction, they will
1386 * be unlocked on commit/cancel.
1387 */
1388 static struct xfs_trans *
xfs_ioctl_setattr_get_trans(struct xfs_inode * ip,int join_flags)1389 xfs_ioctl_setattr_get_trans(
1390 struct xfs_inode *ip,
1391 int join_flags)
1392 {
1393 struct xfs_mount *mp = ip->i_mount;
1394 struct xfs_trans *tp;
1395 int error = -EROFS;
1396
1397 if (mp->m_flags & XFS_MOUNT_RDONLY)
1398 goto out_unlock;
1399 error = -EIO;
1400 if (XFS_FORCED_SHUTDOWN(mp))
1401 goto out_unlock;
1402
1403 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
1404 if (error)
1405 goto out_unlock;
1406
1407 xfs_ilock(ip, XFS_ILOCK_EXCL);
1408 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | join_flags);
1409 join_flags = 0;
1410
1411 /*
1412 * CAP_FOWNER overrides the following restrictions:
1413 *
1414 * The user ID of the calling process must be equal to the file owner
1415 * ID, except in cases where the CAP_FSETID capability is applicable.
1416 */
1417 if (!inode_owner_or_capable(VFS_I(ip))) {
1418 error = -EPERM;
1419 goto out_cancel;
1420 }
1421
1422 if (mp->m_flags & XFS_MOUNT_WSYNC)
1423 xfs_trans_set_sync(tp);
1424
1425 return tp;
1426
1427 out_cancel:
1428 xfs_trans_cancel(tp);
1429 out_unlock:
1430 if (join_flags)
1431 xfs_iunlock(ip, join_flags);
1432 return ERR_PTR(error);
1433 }
1434
1435 /*
1436 * extent size hint validation is somewhat cumbersome. Rules are:
1437 *
1438 * 1. extent size hint is only valid for directories and regular files
1439 * 2. FS_XFLAG_EXTSIZE is only valid for regular files
1440 * 3. FS_XFLAG_EXTSZINHERIT is only valid for directories.
1441 * 4. can only be changed on regular files if no extents are allocated
1442 * 5. can be changed on directories at any time
1443 * 6. extsize hint of 0 turns off hints, clears inode flags.
1444 * 7. Extent size must be a multiple of the appropriate block size.
1445 * 8. for non-realtime files, the extent size hint must be limited
1446 * to half the AG size to avoid alignment extending the extent beyond the
1447 * limits of the AG.
1448 *
1449 * Please keep this function in sync with xfs_scrub_inode_extsize.
1450 */
1451 static int
xfs_ioctl_setattr_check_extsize(struct xfs_inode * ip,struct fsxattr * fa)1452 xfs_ioctl_setattr_check_extsize(
1453 struct xfs_inode *ip,
1454 struct fsxattr *fa)
1455 {
1456 struct xfs_mount *mp = ip->i_mount;
1457 xfs_extlen_t size;
1458 xfs_fsblock_t extsize_fsb;
1459
1460 if (S_ISREG(VFS_I(ip)->i_mode) && ip->i_d.di_nextents &&
1461 ((ip->i_d.di_extsize << mp->m_sb.sb_blocklog) != fa->fsx_extsize))
1462 return -EINVAL;
1463
1464 if (fa->fsx_extsize == 0)
1465 return 0;
1466
1467 extsize_fsb = XFS_B_TO_FSB(mp, fa->fsx_extsize);
1468 if (extsize_fsb > MAXEXTLEN)
1469 return -EINVAL;
1470
1471 if (XFS_IS_REALTIME_INODE(ip) ||
1472 (fa->fsx_xflags & FS_XFLAG_REALTIME)) {
1473 size = mp->m_sb.sb_rextsize << mp->m_sb.sb_blocklog;
1474 } else {
1475 size = mp->m_sb.sb_blocksize;
1476 if (extsize_fsb > mp->m_sb.sb_agblocks / 2)
1477 return -EINVAL;
1478 }
1479
1480 if (fa->fsx_extsize % size)
1481 return -EINVAL;
1482
1483 return 0;
1484 }
1485
1486 /*
1487 * CoW extent size hint validation rules are:
1488 *
1489 * 1. CoW extent size hint can only be set if reflink is enabled on the fs.
1490 * The inode does not have to have any shared blocks, but it must be a v3.
1491 * 2. FS_XFLAG_COWEXTSIZE is only valid for directories and regular files;
1492 * for a directory, the hint is propagated to new files.
1493 * 3. Can be changed on files & directories at any time.
1494 * 4. CoW extsize hint of 0 turns off hints, clears inode flags.
1495 * 5. Extent size must be a multiple of the appropriate block size.
1496 * 6. The extent size hint must be limited to half the AG size to avoid
1497 * alignment extending the extent beyond the limits of the AG.
1498 *
1499 * Please keep this function in sync with xfs_scrub_inode_cowextsize.
1500 */
1501 static int
xfs_ioctl_setattr_check_cowextsize(struct xfs_inode * ip,struct fsxattr * fa)1502 xfs_ioctl_setattr_check_cowextsize(
1503 struct xfs_inode *ip,
1504 struct fsxattr *fa)
1505 {
1506 struct xfs_mount *mp = ip->i_mount;
1507 xfs_extlen_t size;
1508 xfs_fsblock_t cowextsize_fsb;
1509
1510 if (!(fa->fsx_xflags & FS_XFLAG_COWEXTSIZE))
1511 return 0;
1512
1513 if (!xfs_sb_version_hasreflink(&ip->i_mount->m_sb))
1514 return -EINVAL;
1515
1516 if (fa->fsx_cowextsize == 0)
1517 return 0;
1518
1519 cowextsize_fsb = XFS_B_TO_FSB(mp, fa->fsx_cowextsize);
1520 if (cowextsize_fsb > MAXEXTLEN)
1521 return -EINVAL;
1522
1523 size = mp->m_sb.sb_blocksize;
1524 if (cowextsize_fsb > mp->m_sb.sb_agblocks / 2)
1525 return -EINVAL;
1526
1527 if (fa->fsx_cowextsize % size)
1528 return -EINVAL;
1529
1530 return 0;
1531 }
1532
1533 static int
xfs_ioctl_setattr_check_projid(struct xfs_inode * ip,struct fsxattr * fa)1534 xfs_ioctl_setattr_check_projid(
1535 struct xfs_inode *ip,
1536 struct fsxattr *fa)
1537 {
1538 /* Disallow 32bit project ids if projid32bit feature is not enabled. */
1539 if (fa->fsx_projid > (uint16_t)-1 &&
1540 !xfs_sb_version_hasprojid32bit(&ip->i_mount->m_sb))
1541 return -EINVAL;
1542 return 0;
1543 }
1544
1545 STATIC int
xfs_ioctl_setattr(xfs_inode_t * ip,struct fsxattr * fa)1546 xfs_ioctl_setattr(
1547 xfs_inode_t *ip,
1548 struct fsxattr *fa)
1549 {
1550 struct fsxattr old_fa;
1551 struct xfs_mount *mp = ip->i_mount;
1552 struct xfs_trans *tp;
1553 struct xfs_dquot *udqp = NULL;
1554 struct xfs_dquot *pdqp = NULL;
1555 struct xfs_dquot *olddquot = NULL;
1556 int code;
1557 int join_flags = 0;
1558
1559 trace_xfs_ioctl_setattr(ip);
1560
1561 code = xfs_ioctl_setattr_check_projid(ip, fa);
1562 if (code)
1563 return code;
1564
1565 /*
1566 * If disk quotas is on, we make sure that the dquots do exist on disk,
1567 * before we start any other transactions. Trying to do this later
1568 * is messy. We don't care to take a readlock to look at the ids
1569 * in inode here, because we can't hold it across the trans_reserve.
1570 * If the IDs do change before we take the ilock, we're covered
1571 * because the i_*dquot fields will get updated anyway.
1572 */
1573 if (XFS_IS_QUOTA_ON(mp)) {
1574 code = xfs_qm_vop_dqalloc(ip, VFS_I(ip)->i_uid,
1575 VFS_I(ip)->i_gid, fa->fsx_projid,
1576 XFS_QMOPT_PQUOTA, &udqp, NULL, &pdqp);
1577 if (code)
1578 return code;
1579 }
1580
1581 /*
1582 * Changing DAX config may require inode locking for mapping
1583 * invalidation. These need to be held all the way to transaction commit
1584 * or cancel time, so need to be passed through to
1585 * xfs_ioctl_setattr_get_trans() so it can apply them to the join call
1586 * appropriately.
1587 */
1588 code = xfs_ioctl_setattr_dax_invalidate(ip, fa, &join_flags);
1589 if (code)
1590 goto error_free_dquots;
1591
1592 tp = xfs_ioctl_setattr_get_trans(ip, join_flags);
1593 if (IS_ERR(tp)) {
1594 code = PTR_ERR(tp);
1595 goto error_free_dquots;
1596 }
1597
1598 if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_PQUOTA_ON(mp) &&
1599 ip->i_d.di_projid != fa->fsx_projid) {
1600 code = xfs_qm_vop_chown_reserve(tp, ip, udqp, NULL, pdqp,
1601 capable(CAP_FOWNER) ? XFS_QMOPT_FORCE_RES : 0);
1602 if (code) /* out of quota */
1603 goto error_trans_cancel;
1604 }
1605
1606 xfs_fill_fsxattr(ip, false, &old_fa);
1607 code = vfs_ioc_fssetxattr_check(VFS_I(ip), &old_fa, fa);
1608 if (code)
1609 goto error_trans_cancel;
1610
1611 code = xfs_ioctl_setattr_check_extsize(ip, fa);
1612 if (code)
1613 goto error_trans_cancel;
1614
1615 code = xfs_ioctl_setattr_check_cowextsize(ip, fa);
1616 if (code)
1617 goto error_trans_cancel;
1618
1619 code = xfs_ioctl_setattr_xflags(tp, ip, fa);
1620 if (code)
1621 goto error_trans_cancel;
1622
1623 /*
1624 * Change file ownership. Must be the owner or privileged. CAP_FSETID
1625 * overrides the following restrictions:
1626 *
1627 * The set-user-ID and set-group-ID bits of a file will be cleared upon
1628 * successful return from chown()
1629 */
1630
1631 if ((VFS_I(ip)->i_mode & (S_ISUID|S_ISGID)) &&
1632 !capable_wrt_inode_uidgid(VFS_I(ip), CAP_FSETID))
1633 VFS_I(ip)->i_mode &= ~(S_ISUID|S_ISGID);
1634
1635 /* Change the ownerships and register project quota modifications */
1636 if (ip->i_d.di_projid != fa->fsx_projid) {
1637 if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_PQUOTA_ON(mp)) {
1638 olddquot = xfs_qm_vop_chown(tp, ip,
1639 &ip->i_pdquot, pdqp);
1640 }
1641 ip->i_d.di_projid = fa->fsx_projid;
1642 }
1643
1644 /*
1645 * Only set the extent size hint if we've already determined that the
1646 * extent size hint should be set on the inode. If no extent size flags
1647 * are set on the inode then unconditionally clear the extent size hint.
1648 */
1649 if (ip->i_d.di_flags & (XFS_DIFLAG_EXTSIZE | XFS_DIFLAG_EXTSZINHERIT))
1650 ip->i_d.di_extsize = fa->fsx_extsize >> mp->m_sb.sb_blocklog;
1651 else
1652 ip->i_d.di_extsize = 0;
1653 if (xfs_sb_version_has_v3inode(&mp->m_sb) &&
1654 (ip->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE))
1655 ip->i_d.di_cowextsize = fa->fsx_cowextsize >>
1656 mp->m_sb.sb_blocklog;
1657 else
1658 ip->i_d.di_cowextsize = 0;
1659
1660 code = xfs_trans_commit(tp);
1661
1662 /*
1663 * Release any dquot(s) the inode had kept before chown.
1664 */
1665 xfs_qm_dqrele(olddquot);
1666 xfs_qm_dqrele(udqp);
1667 xfs_qm_dqrele(pdqp);
1668
1669 return code;
1670
1671 error_trans_cancel:
1672 xfs_trans_cancel(tp);
1673 error_free_dquots:
1674 xfs_qm_dqrele(udqp);
1675 xfs_qm_dqrele(pdqp);
1676 return code;
1677 }
1678
1679 STATIC int
xfs_ioc_fssetxattr(xfs_inode_t * ip,struct file * filp,void __user * arg)1680 xfs_ioc_fssetxattr(
1681 xfs_inode_t *ip,
1682 struct file *filp,
1683 void __user *arg)
1684 {
1685 struct fsxattr fa;
1686 int error;
1687
1688 if (copy_from_user(&fa, arg, sizeof(fa)))
1689 return -EFAULT;
1690
1691 error = mnt_want_write_file(filp);
1692 if (error)
1693 return error;
1694 error = xfs_ioctl_setattr(ip, &fa);
1695 mnt_drop_write_file(filp);
1696 return error;
1697 }
1698
1699 STATIC int
xfs_ioc_getxflags(xfs_inode_t * ip,void __user * arg)1700 xfs_ioc_getxflags(
1701 xfs_inode_t *ip,
1702 void __user *arg)
1703 {
1704 unsigned int flags;
1705
1706 flags = xfs_di2lxflags(ip->i_d.di_flags);
1707 if (copy_to_user(arg, &flags, sizeof(flags)))
1708 return -EFAULT;
1709 return 0;
1710 }
1711
1712 STATIC int
xfs_ioc_setxflags(struct xfs_inode * ip,struct file * filp,void __user * arg)1713 xfs_ioc_setxflags(
1714 struct xfs_inode *ip,
1715 struct file *filp,
1716 void __user *arg)
1717 {
1718 struct xfs_trans *tp;
1719 struct fsxattr fa;
1720 struct fsxattr old_fa;
1721 unsigned int flags;
1722 int join_flags = 0;
1723 int error;
1724
1725 if (copy_from_user(&flags, arg, sizeof(flags)))
1726 return -EFAULT;
1727
1728 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
1729 FS_NOATIME_FL | FS_NODUMP_FL | \
1730 FS_SYNC_FL))
1731 return -EOPNOTSUPP;
1732
1733 fa.fsx_xflags = xfs_merge_ioc_xflags(flags, xfs_ip2xflags(ip));
1734
1735 error = mnt_want_write_file(filp);
1736 if (error)
1737 return error;
1738
1739 /*
1740 * Changing DAX config may require inode locking for mapping
1741 * invalidation. These need to be held all the way to transaction commit
1742 * or cancel time, so need to be passed through to
1743 * xfs_ioctl_setattr_get_trans() so it can apply them to the join call
1744 * appropriately.
1745 */
1746 error = xfs_ioctl_setattr_dax_invalidate(ip, &fa, &join_flags);
1747 if (error)
1748 goto out_drop_write;
1749
1750 tp = xfs_ioctl_setattr_get_trans(ip, join_flags);
1751 if (IS_ERR(tp)) {
1752 error = PTR_ERR(tp);
1753 goto out_drop_write;
1754 }
1755
1756 xfs_fill_fsxattr(ip, false, &old_fa);
1757 error = vfs_ioc_fssetxattr_check(VFS_I(ip), &old_fa, &fa);
1758 if (error) {
1759 xfs_trans_cancel(tp);
1760 goto out_drop_write;
1761 }
1762
1763 error = xfs_ioctl_setattr_xflags(tp, ip, &fa);
1764 if (error) {
1765 xfs_trans_cancel(tp);
1766 goto out_drop_write;
1767 }
1768
1769 error = xfs_trans_commit(tp);
1770 out_drop_write:
1771 mnt_drop_write_file(filp);
1772 return error;
1773 }
1774
1775 static bool
xfs_getbmap_format(struct kgetbmap * p,struct getbmapx __user * u,size_t recsize)1776 xfs_getbmap_format(
1777 struct kgetbmap *p,
1778 struct getbmapx __user *u,
1779 size_t recsize)
1780 {
1781 if (put_user(p->bmv_offset, &u->bmv_offset) ||
1782 put_user(p->bmv_block, &u->bmv_block) ||
1783 put_user(p->bmv_length, &u->bmv_length) ||
1784 put_user(0, &u->bmv_count) ||
1785 put_user(0, &u->bmv_entries))
1786 return false;
1787 if (recsize < sizeof(struct getbmapx))
1788 return true;
1789 if (put_user(0, &u->bmv_iflags) ||
1790 put_user(p->bmv_oflags, &u->bmv_oflags) ||
1791 put_user(0, &u->bmv_unused1) ||
1792 put_user(0, &u->bmv_unused2))
1793 return false;
1794 return true;
1795 }
1796
1797 STATIC int
xfs_ioc_getbmap(struct file * file,unsigned int cmd,void __user * arg)1798 xfs_ioc_getbmap(
1799 struct file *file,
1800 unsigned int cmd,
1801 void __user *arg)
1802 {
1803 struct getbmapx bmx = { 0 };
1804 struct kgetbmap *buf;
1805 size_t recsize;
1806 int error, i;
1807
1808 switch (cmd) {
1809 case XFS_IOC_GETBMAPA:
1810 bmx.bmv_iflags = BMV_IF_ATTRFORK;
1811 /*FALLTHRU*/
1812 case XFS_IOC_GETBMAP:
1813 if (file->f_mode & FMODE_NOCMTIME)
1814 bmx.bmv_iflags |= BMV_IF_NO_DMAPI_READ;
1815 /* struct getbmap is a strict subset of struct getbmapx. */
1816 recsize = sizeof(struct getbmap);
1817 break;
1818 case XFS_IOC_GETBMAPX:
1819 recsize = sizeof(struct getbmapx);
1820 break;
1821 default:
1822 return -EINVAL;
1823 }
1824
1825 if (copy_from_user(&bmx, arg, recsize))
1826 return -EFAULT;
1827
1828 if (bmx.bmv_count < 2)
1829 return -EINVAL;
1830 if (bmx.bmv_count > ULONG_MAX / recsize)
1831 return -ENOMEM;
1832
1833 buf = kmem_zalloc_large(bmx.bmv_count * sizeof(*buf), 0);
1834 if (!buf)
1835 return -ENOMEM;
1836
1837 error = xfs_getbmap(XFS_I(file_inode(file)), &bmx, buf);
1838 if (error)
1839 goto out_free_buf;
1840
1841 error = -EFAULT;
1842 if (copy_to_user(arg, &bmx, recsize))
1843 goto out_free_buf;
1844 arg += recsize;
1845
1846 for (i = 0; i < bmx.bmv_entries; i++) {
1847 if (!xfs_getbmap_format(buf + i, arg, recsize))
1848 goto out_free_buf;
1849 arg += recsize;
1850 }
1851
1852 error = 0;
1853 out_free_buf:
1854 kmem_free(buf);
1855 return error;
1856 }
1857
1858 STATIC int
xfs_ioc_getfsmap(struct xfs_inode * ip,struct fsmap_head __user * arg)1859 xfs_ioc_getfsmap(
1860 struct xfs_inode *ip,
1861 struct fsmap_head __user *arg)
1862 {
1863 struct xfs_fsmap_head xhead = {0};
1864 struct fsmap_head head;
1865 struct fsmap *recs;
1866 unsigned int count;
1867 __u32 last_flags = 0;
1868 bool done = false;
1869 int error;
1870
1871 if (copy_from_user(&head, arg, sizeof(struct fsmap_head)))
1872 return -EFAULT;
1873 if (memchr_inv(head.fmh_reserved, 0, sizeof(head.fmh_reserved)) ||
1874 memchr_inv(head.fmh_keys[0].fmr_reserved, 0,
1875 sizeof(head.fmh_keys[0].fmr_reserved)) ||
1876 memchr_inv(head.fmh_keys[1].fmr_reserved, 0,
1877 sizeof(head.fmh_keys[1].fmr_reserved)))
1878 return -EINVAL;
1879
1880 /*
1881 * Use an internal memory buffer so that we don't have to copy fsmap
1882 * data to userspace while holding locks. Start by trying to allocate
1883 * up to 128k for the buffer, but fall back to a single page if needed.
1884 */
1885 count = min_t(unsigned int, head.fmh_count,
1886 131072 / sizeof(struct fsmap));
1887 recs = kvzalloc(count * sizeof(struct fsmap), GFP_KERNEL);
1888 if (!recs) {
1889 count = min_t(unsigned int, head.fmh_count,
1890 PAGE_SIZE / sizeof(struct fsmap));
1891 recs = kvzalloc(count * sizeof(struct fsmap), GFP_KERNEL);
1892 if (!recs)
1893 return -ENOMEM;
1894 }
1895
1896 xhead.fmh_iflags = head.fmh_iflags;
1897 xfs_fsmap_to_internal(&xhead.fmh_keys[0], &head.fmh_keys[0]);
1898 xfs_fsmap_to_internal(&xhead.fmh_keys[1], &head.fmh_keys[1]);
1899
1900 trace_xfs_getfsmap_low_key(ip->i_mount, &xhead.fmh_keys[0]);
1901 trace_xfs_getfsmap_high_key(ip->i_mount, &xhead.fmh_keys[1]);
1902
1903 head.fmh_entries = 0;
1904 do {
1905 struct fsmap __user *user_recs;
1906 struct fsmap *last_rec;
1907
1908 user_recs = &arg->fmh_recs[head.fmh_entries];
1909 xhead.fmh_entries = 0;
1910 xhead.fmh_count = min_t(unsigned int, count,
1911 head.fmh_count - head.fmh_entries);
1912
1913 /* Run query, record how many entries we got. */
1914 error = xfs_getfsmap(ip->i_mount, &xhead, recs);
1915 switch (error) {
1916 case 0:
1917 /*
1918 * There are no more records in the result set. Copy
1919 * whatever we got to userspace and break out.
1920 */
1921 done = true;
1922 break;
1923 case -ECANCELED:
1924 /*
1925 * The internal memory buffer is full. Copy whatever
1926 * records we got to userspace and go again if we have
1927 * not yet filled the userspace buffer.
1928 */
1929 error = 0;
1930 break;
1931 default:
1932 goto out_free;
1933 }
1934 head.fmh_entries += xhead.fmh_entries;
1935 head.fmh_oflags = xhead.fmh_oflags;
1936
1937 /*
1938 * If the caller wanted a record count or there aren't any
1939 * new records to return, we're done.
1940 */
1941 if (head.fmh_count == 0 || xhead.fmh_entries == 0)
1942 break;
1943
1944 /* Copy all the records we got out to userspace. */
1945 if (copy_to_user(user_recs, recs,
1946 xhead.fmh_entries * sizeof(struct fsmap))) {
1947 error = -EFAULT;
1948 goto out_free;
1949 }
1950
1951 /* Remember the last record flags we copied to userspace. */
1952 last_rec = &recs[xhead.fmh_entries - 1];
1953 last_flags = last_rec->fmr_flags;
1954
1955 /* Set up the low key for the next iteration. */
1956 xfs_fsmap_to_internal(&xhead.fmh_keys[0], last_rec);
1957 trace_xfs_getfsmap_low_key(ip->i_mount, &xhead.fmh_keys[0]);
1958 } while (!done && head.fmh_entries < head.fmh_count);
1959
1960 /*
1961 * If there are no more records in the query result set and we're not
1962 * in counting mode, mark the last record returned with the LAST flag.
1963 */
1964 if (done && head.fmh_count > 0 && head.fmh_entries > 0) {
1965 struct fsmap __user *user_rec;
1966
1967 last_flags |= FMR_OF_LAST;
1968 user_rec = &arg->fmh_recs[head.fmh_entries - 1];
1969
1970 if (copy_to_user(&user_rec->fmr_flags, &last_flags,
1971 sizeof(last_flags))) {
1972 error = -EFAULT;
1973 goto out_free;
1974 }
1975 }
1976
1977 /* copy back header */
1978 if (copy_to_user(arg, &head, sizeof(struct fsmap_head))) {
1979 error = -EFAULT;
1980 goto out_free;
1981 }
1982
1983 out_free:
1984 kmem_free(recs);
1985 return error;
1986 }
1987
1988 STATIC int
xfs_ioc_scrub_metadata(struct xfs_inode * ip,void __user * arg)1989 xfs_ioc_scrub_metadata(
1990 struct xfs_inode *ip,
1991 void __user *arg)
1992 {
1993 struct xfs_scrub_metadata scrub;
1994 int error;
1995
1996 if (!capable(CAP_SYS_ADMIN))
1997 return -EPERM;
1998
1999 if (copy_from_user(&scrub, arg, sizeof(scrub)))
2000 return -EFAULT;
2001
2002 error = xfs_scrub_metadata(ip, &scrub);
2003 if (error)
2004 return error;
2005
2006 if (copy_to_user(arg, &scrub, sizeof(scrub)))
2007 return -EFAULT;
2008
2009 return 0;
2010 }
2011
2012 int
xfs_ioc_swapext(xfs_swapext_t * sxp)2013 xfs_ioc_swapext(
2014 xfs_swapext_t *sxp)
2015 {
2016 xfs_inode_t *ip, *tip;
2017 struct fd f, tmp;
2018 int error = 0;
2019
2020 /* Pull information for the target fd */
2021 f = fdget((int)sxp->sx_fdtarget);
2022 if (!f.file) {
2023 error = -EINVAL;
2024 goto out;
2025 }
2026
2027 if (!(f.file->f_mode & FMODE_WRITE) ||
2028 !(f.file->f_mode & FMODE_READ) ||
2029 (f.file->f_flags & O_APPEND)) {
2030 error = -EBADF;
2031 goto out_put_file;
2032 }
2033
2034 tmp = fdget((int)sxp->sx_fdtmp);
2035 if (!tmp.file) {
2036 error = -EINVAL;
2037 goto out_put_file;
2038 }
2039
2040 if (!(tmp.file->f_mode & FMODE_WRITE) ||
2041 !(tmp.file->f_mode & FMODE_READ) ||
2042 (tmp.file->f_flags & O_APPEND)) {
2043 error = -EBADF;
2044 goto out_put_tmp_file;
2045 }
2046
2047 if (IS_SWAPFILE(file_inode(f.file)) ||
2048 IS_SWAPFILE(file_inode(tmp.file))) {
2049 error = -EINVAL;
2050 goto out_put_tmp_file;
2051 }
2052
2053 /*
2054 * We need to ensure that the fds passed in point to XFS inodes
2055 * before we cast and access them as XFS structures as we have no
2056 * control over what the user passes us here.
2057 */
2058 if (f.file->f_op != &xfs_file_operations ||
2059 tmp.file->f_op != &xfs_file_operations) {
2060 error = -EINVAL;
2061 goto out_put_tmp_file;
2062 }
2063
2064 ip = XFS_I(file_inode(f.file));
2065 tip = XFS_I(file_inode(tmp.file));
2066
2067 if (ip->i_mount != tip->i_mount) {
2068 error = -EINVAL;
2069 goto out_put_tmp_file;
2070 }
2071
2072 if (ip->i_ino == tip->i_ino) {
2073 error = -EINVAL;
2074 goto out_put_tmp_file;
2075 }
2076
2077 if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
2078 error = -EIO;
2079 goto out_put_tmp_file;
2080 }
2081
2082 error = xfs_swap_extents(ip, tip, sxp);
2083
2084 out_put_tmp_file:
2085 fdput(tmp);
2086 out_put_file:
2087 fdput(f);
2088 out:
2089 return error;
2090 }
2091
2092 static int
xfs_ioc_getlabel(struct xfs_mount * mp,char __user * user_label)2093 xfs_ioc_getlabel(
2094 struct xfs_mount *mp,
2095 char __user *user_label)
2096 {
2097 struct xfs_sb *sbp = &mp->m_sb;
2098 char label[XFSLABEL_MAX + 1];
2099
2100 /* Paranoia */
2101 BUILD_BUG_ON(sizeof(sbp->sb_fname) > FSLABEL_MAX);
2102
2103 /* 1 larger than sb_fname, so this ensures a trailing NUL char */
2104 memset(label, 0, sizeof(label));
2105 spin_lock(&mp->m_sb_lock);
2106 strncpy(label, sbp->sb_fname, XFSLABEL_MAX);
2107 spin_unlock(&mp->m_sb_lock);
2108
2109 if (copy_to_user(user_label, label, sizeof(label)))
2110 return -EFAULT;
2111 return 0;
2112 }
2113
2114 static int
xfs_ioc_setlabel(struct file * filp,struct xfs_mount * mp,char __user * newlabel)2115 xfs_ioc_setlabel(
2116 struct file *filp,
2117 struct xfs_mount *mp,
2118 char __user *newlabel)
2119 {
2120 struct xfs_sb *sbp = &mp->m_sb;
2121 char label[XFSLABEL_MAX + 1];
2122 size_t len;
2123 int error;
2124
2125 if (!capable(CAP_SYS_ADMIN))
2126 return -EPERM;
2127 /*
2128 * The generic ioctl allows up to FSLABEL_MAX chars, but XFS is much
2129 * smaller, at 12 bytes. We copy one more to be sure we find the
2130 * (required) NULL character to test the incoming label length.
2131 * NB: The on disk label doesn't need to be null terminated.
2132 */
2133 if (copy_from_user(label, newlabel, XFSLABEL_MAX + 1))
2134 return -EFAULT;
2135 len = strnlen(label, XFSLABEL_MAX + 1);
2136 if (len > sizeof(sbp->sb_fname))
2137 return -EINVAL;
2138
2139 error = mnt_want_write_file(filp);
2140 if (error)
2141 return error;
2142
2143 spin_lock(&mp->m_sb_lock);
2144 memset(sbp->sb_fname, 0, sizeof(sbp->sb_fname));
2145 memcpy(sbp->sb_fname, label, len);
2146 spin_unlock(&mp->m_sb_lock);
2147
2148 /*
2149 * Now we do several things to satisfy userspace.
2150 * In addition to normal logging of the primary superblock, we also
2151 * immediately write these changes to sector zero for the primary, then
2152 * update all backup supers (as xfs_db does for a label change), then
2153 * invalidate the block device page cache. This is so that any prior
2154 * buffered reads from userspace (i.e. from blkid) are invalidated,
2155 * and userspace will see the newly-written label.
2156 */
2157 error = xfs_sync_sb_buf(mp);
2158 if (error)
2159 goto out;
2160 /*
2161 * growfs also updates backup supers so lock against that.
2162 */
2163 mutex_lock(&mp->m_growlock);
2164 error = xfs_update_secondary_sbs(mp);
2165 mutex_unlock(&mp->m_growlock);
2166
2167 invalidate_bdev(mp->m_ddev_targp->bt_bdev);
2168
2169 out:
2170 mnt_drop_write_file(filp);
2171 return error;
2172 }
2173
2174 /*
2175 * Note: some of the ioctl's return positive numbers as a
2176 * byte count indicating success, such as readlink_by_handle.
2177 * So we don't "sign flip" like most other routines. This means
2178 * true errors need to be returned as a negative value.
2179 */
2180 long
xfs_file_ioctl(struct file * filp,unsigned int cmd,unsigned long p)2181 xfs_file_ioctl(
2182 struct file *filp,
2183 unsigned int cmd,
2184 unsigned long p)
2185 {
2186 struct inode *inode = file_inode(filp);
2187 struct xfs_inode *ip = XFS_I(inode);
2188 struct xfs_mount *mp = ip->i_mount;
2189 void __user *arg = (void __user *)p;
2190 int error;
2191
2192 trace_xfs_file_ioctl(ip);
2193
2194 switch (cmd) {
2195 case FITRIM:
2196 return xfs_ioc_trim(mp, arg);
2197 case FS_IOC_GETFSLABEL:
2198 return xfs_ioc_getlabel(mp, arg);
2199 case FS_IOC_SETFSLABEL:
2200 return xfs_ioc_setlabel(filp, mp, arg);
2201 case XFS_IOC_ALLOCSP:
2202 case XFS_IOC_FREESP:
2203 case XFS_IOC_RESVSP:
2204 case XFS_IOC_UNRESVSP:
2205 case XFS_IOC_ALLOCSP64:
2206 case XFS_IOC_FREESP64:
2207 case XFS_IOC_RESVSP64:
2208 case XFS_IOC_UNRESVSP64:
2209 case XFS_IOC_ZERO_RANGE: {
2210 xfs_flock64_t bf;
2211
2212 if (copy_from_user(&bf, arg, sizeof(bf)))
2213 return -EFAULT;
2214 return xfs_ioc_space(filp, cmd, &bf);
2215 }
2216 case XFS_IOC_DIOINFO: {
2217 struct dioattr da;
2218 xfs_buftarg_t *target =
2219 XFS_IS_REALTIME_INODE(ip) ?
2220 mp->m_rtdev_targp : mp->m_ddev_targp;
2221
2222 da.d_mem = da.d_miniosz = target->bt_logical_sectorsize;
2223 da.d_maxiosz = INT_MAX & ~(da.d_miniosz - 1);
2224
2225 if (copy_to_user(arg, &da, sizeof(da)))
2226 return -EFAULT;
2227 return 0;
2228 }
2229
2230 case XFS_IOC_FSBULKSTAT_SINGLE:
2231 case XFS_IOC_FSBULKSTAT:
2232 case XFS_IOC_FSINUMBERS:
2233 return xfs_ioc_fsbulkstat(mp, cmd, arg);
2234
2235 case XFS_IOC_BULKSTAT:
2236 return xfs_ioc_bulkstat(mp, cmd, arg);
2237 case XFS_IOC_INUMBERS:
2238 return xfs_ioc_inumbers(mp, cmd, arg);
2239
2240 case XFS_IOC_FSGEOMETRY_V1:
2241 return xfs_ioc_fsgeometry(mp, arg, 3);
2242 case XFS_IOC_FSGEOMETRY_V4:
2243 return xfs_ioc_fsgeometry(mp, arg, 4);
2244 case XFS_IOC_FSGEOMETRY:
2245 return xfs_ioc_fsgeometry(mp, arg, 5);
2246
2247 case XFS_IOC_AG_GEOMETRY:
2248 return xfs_ioc_ag_geometry(mp, arg);
2249
2250 case XFS_IOC_GETVERSION:
2251 return put_user(inode->i_generation, (int __user *)arg);
2252
2253 case XFS_IOC_FSGETXATTR:
2254 return xfs_ioc_fsgetxattr(ip, 0, arg);
2255 case XFS_IOC_FSGETXATTRA:
2256 return xfs_ioc_fsgetxattr(ip, 1, arg);
2257 case XFS_IOC_FSSETXATTR:
2258 return xfs_ioc_fssetxattr(ip, filp, arg);
2259 case XFS_IOC_GETXFLAGS:
2260 return xfs_ioc_getxflags(ip, arg);
2261 case XFS_IOC_SETXFLAGS:
2262 return xfs_ioc_setxflags(ip, filp, arg);
2263
2264 case XFS_IOC_FSSETDM: {
2265 struct fsdmidata dmi;
2266
2267 if (copy_from_user(&dmi, arg, sizeof(dmi)))
2268 return -EFAULT;
2269
2270 error = mnt_want_write_file(filp);
2271 if (error)
2272 return error;
2273
2274 error = xfs_set_dmattrs(ip, dmi.fsd_dmevmask,
2275 dmi.fsd_dmstate);
2276 mnt_drop_write_file(filp);
2277 return error;
2278 }
2279
2280 case XFS_IOC_GETBMAP:
2281 case XFS_IOC_GETBMAPA:
2282 case XFS_IOC_GETBMAPX:
2283 return xfs_ioc_getbmap(filp, cmd, arg);
2284
2285 case FS_IOC_GETFSMAP:
2286 return xfs_ioc_getfsmap(ip, arg);
2287
2288 case XFS_IOC_SCRUB_METADATA:
2289 return xfs_ioc_scrub_metadata(ip, arg);
2290
2291 case XFS_IOC_FD_TO_HANDLE:
2292 case XFS_IOC_PATH_TO_HANDLE:
2293 case XFS_IOC_PATH_TO_FSHANDLE: {
2294 xfs_fsop_handlereq_t hreq;
2295
2296 if (copy_from_user(&hreq, arg, sizeof(hreq)))
2297 return -EFAULT;
2298 return xfs_find_handle(cmd, &hreq);
2299 }
2300 case XFS_IOC_OPEN_BY_HANDLE: {
2301 xfs_fsop_handlereq_t hreq;
2302
2303 if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
2304 return -EFAULT;
2305 return xfs_open_by_handle(filp, &hreq);
2306 }
2307 case XFS_IOC_FSSETDM_BY_HANDLE:
2308 return xfs_fssetdm_by_handle(filp, arg);
2309
2310 case XFS_IOC_READLINK_BY_HANDLE: {
2311 xfs_fsop_handlereq_t hreq;
2312
2313 if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
2314 return -EFAULT;
2315 return xfs_readlink_by_handle(filp, &hreq);
2316 }
2317 case XFS_IOC_ATTRLIST_BY_HANDLE:
2318 return xfs_attrlist_by_handle(filp, arg);
2319
2320 case XFS_IOC_ATTRMULTI_BY_HANDLE:
2321 return xfs_attrmulti_by_handle(filp, arg);
2322
2323 case XFS_IOC_SWAPEXT: {
2324 struct xfs_swapext sxp;
2325
2326 if (copy_from_user(&sxp, arg, sizeof(xfs_swapext_t)))
2327 return -EFAULT;
2328 error = mnt_want_write_file(filp);
2329 if (error)
2330 return error;
2331 error = xfs_ioc_swapext(&sxp);
2332 mnt_drop_write_file(filp);
2333 return error;
2334 }
2335
2336 case XFS_IOC_FSCOUNTS: {
2337 xfs_fsop_counts_t out;
2338
2339 xfs_fs_counts(mp, &out);
2340
2341 if (copy_to_user(arg, &out, sizeof(out)))
2342 return -EFAULT;
2343 return 0;
2344 }
2345
2346 case XFS_IOC_SET_RESBLKS: {
2347 xfs_fsop_resblks_t inout;
2348 uint64_t in;
2349
2350 if (!capable(CAP_SYS_ADMIN))
2351 return -EPERM;
2352
2353 if (mp->m_flags & XFS_MOUNT_RDONLY)
2354 return -EROFS;
2355
2356 if (copy_from_user(&inout, arg, sizeof(inout)))
2357 return -EFAULT;
2358
2359 error = mnt_want_write_file(filp);
2360 if (error)
2361 return error;
2362
2363 /* input parameter is passed in resblks field of structure */
2364 in = inout.resblks;
2365 error = xfs_reserve_blocks(mp, &in, &inout);
2366 mnt_drop_write_file(filp);
2367 if (error)
2368 return error;
2369
2370 if (copy_to_user(arg, &inout, sizeof(inout)))
2371 return -EFAULT;
2372 return 0;
2373 }
2374
2375 case XFS_IOC_GET_RESBLKS: {
2376 xfs_fsop_resblks_t out;
2377
2378 if (!capable(CAP_SYS_ADMIN))
2379 return -EPERM;
2380
2381 error = xfs_reserve_blocks(mp, NULL, &out);
2382 if (error)
2383 return error;
2384
2385 if (copy_to_user(arg, &out, sizeof(out)))
2386 return -EFAULT;
2387
2388 return 0;
2389 }
2390
2391 case XFS_IOC_FSGROWFSDATA: {
2392 xfs_growfs_data_t in;
2393
2394 if (copy_from_user(&in, arg, sizeof(in)))
2395 return -EFAULT;
2396
2397 error = mnt_want_write_file(filp);
2398 if (error)
2399 return error;
2400 error = xfs_growfs_data(mp, &in);
2401 mnt_drop_write_file(filp);
2402 return error;
2403 }
2404
2405 case XFS_IOC_FSGROWFSLOG: {
2406 xfs_growfs_log_t in;
2407
2408 if (copy_from_user(&in, arg, sizeof(in)))
2409 return -EFAULT;
2410
2411 error = mnt_want_write_file(filp);
2412 if (error)
2413 return error;
2414 error = xfs_growfs_log(mp, &in);
2415 mnt_drop_write_file(filp);
2416 return error;
2417 }
2418
2419 case XFS_IOC_FSGROWFSRT: {
2420 xfs_growfs_rt_t in;
2421
2422 if (copy_from_user(&in, arg, sizeof(in)))
2423 return -EFAULT;
2424
2425 error = mnt_want_write_file(filp);
2426 if (error)
2427 return error;
2428 error = xfs_growfs_rt(mp, &in);
2429 mnt_drop_write_file(filp);
2430 return error;
2431 }
2432
2433 case XFS_IOC_GOINGDOWN: {
2434 uint32_t in;
2435
2436 if (!capable(CAP_SYS_ADMIN))
2437 return -EPERM;
2438
2439 if (get_user(in, (uint32_t __user *)arg))
2440 return -EFAULT;
2441
2442 return xfs_fs_goingdown(mp, in);
2443 }
2444
2445 case XFS_IOC_ERROR_INJECTION: {
2446 xfs_error_injection_t in;
2447
2448 if (!capable(CAP_SYS_ADMIN))
2449 return -EPERM;
2450
2451 if (copy_from_user(&in, arg, sizeof(in)))
2452 return -EFAULT;
2453
2454 return xfs_errortag_add(mp, in.errtag);
2455 }
2456
2457 case XFS_IOC_ERROR_CLEARALL:
2458 if (!capable(CAP_SYS_ADMIN))
2459 return -EPERM;
2460
2461 return xfs_errortag_clearall(mp);
2462
2463 case XFS_IOC_FREE_EOFBLOCKS: {
2464 struct xfs_fs_eofblocks eofb;
2465 struct xfs_eofblocks keofb;
2466
2467 if (!capable(CAP_SYS_ADMIN))
2468 return -EPERM;
2469
2470 if (mp->m_flags & XFS_MOUNT_RDONLY)
2471 return -EROFS;
2472
2473 if (copy_from_user(&eofb, arg, sizeof(eofb)))
2474 return -EFAULT;
2475
2476 error = xfs_fs_eofblocks_from_user(&eofb, &keofb);
2477 if (error)
2478 return error;
2479
2480 sb_start_write(mp->m_super);
2481 error = xfs_icache_free_eofblocks(mp, &keofb);
2482 sb_end_write(mp->m_super);
2483 return error;
2484 }
2485
2486 default:
2487 return -ENOTTY;
2488 }
2489 }
2490