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