blob: b3021d9b34a5e6395bfe699a0f30a6c6d21275a4 [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001// 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"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000014#include "xfs_rtalloc.h"
David Brazdil0f672f62019-12-10 10:32:29 +000015#include "xfs_iwalk.h"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000016#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"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000027#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"
David Brazdil0f672f62019-12-10 10:32:29 +000034#include "xfs_ag.h"
35#include "xfs_health.h"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000036
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000037#include <linux/mount.h>
38#include <linux/namei.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000039
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 */
51int
52xfs_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 {
David Brazdil0f672f62019-12-10 10:32:29 +000070 error = user_path_at(AT_FDCWD, hreq->path, 0, &path);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000071 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 */
128STATIC int
129xfs_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 */
139struct dentry *
140xfs_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
171STATIC struct dentry *
172xfs_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
179int
180xfs_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
259int
260xfs_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
293int
294xfs_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
325STATIC int
326xfs_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
369STATIC int
370xfs_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
David Brazdil0f672f62019-12-10 10:32:29 +0000399 kbuf = kmem_zalloc_large(al_hreq.buflen, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000400 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
417out_kfree:
418 kmem_free(kbuf);
419out_dput:
420 dput(dentry);
421 return error;
422}
423
424int
425xfs_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;
David Brazdil0f672f62019-12-10 10:32:29 +0000437 kbuf = kmem_zalloc_large(*len, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000438 if (!kbuf)
439 return -ENOMEM;
440
David Brazdil0f672f62019-12-10 10:32:29 +0000441 error = xfs_attr_get(XFS_I(inode), name, &kbuf, (int *)len, flags);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000442 if (error)
443 goto out_kfree;
444
445 if (copy_to_user(ubuf, kbuf, *len))
446 error = -EFAULT;
447
448out_kfree:
449 kmem_free(kbuf);
450 return error;
451}
452
453int
454xfs_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
480int
481xfs_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
496STATIC int
497xfs_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++) {
Olivier Deprez0e641232021-09-23 10:07:05 +0200539 ops[i].am_flags &= ~ATTR_KERNEL_FLAGS;
540
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000541 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
590int
591xfs_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
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000603 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
Olivier Deprez0e641232021-09-23 10:07:05 +0200670 /*
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
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000695 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), 0);
718 if (error)
719 goto out_unlock;
720 }
721
722 iattr.ia_valid = ATTR_SIZE;
723 iattr.ia_size = bf->l_start;
724
725 error = xfs_vn_setattr_size(file_dentry(filp), &iattr);
726 break;
727 default:
728 ASSERT(0);
729 error = -EINVAL;
730 }
731
732 if (error)
733 goto out_unlock;
734
735 error = xfs_update_prealloc_flags(ip, flags);
736
737out_unlock:
738 xfs_iunlock(ip, iolock);
739 mnt_drop_write_file(filp);
740 return error;
741}
742
David Brazdil0f672f62019-12-10 10:32:29 +0000743/* Return 0 on success or positive error */
744int
745xfs_fsbulkstat_one_fmt(
746 struct xfs_ibulk *breq,
747 const struct xfs_bulkstat *bstat)
748{
749 struct xfs_bstat bs1;
750
751 xfs_bulkstat_to_bstat(breq->mp, &bs1, bstat);
752 if (copy_to_user(breq->ubuffer, &bs1, sizeof(bs1)))
753 return -EFAULT;
754 return xfs_ibulk_advance(breq, sizeof(struct xfs_bstat));
755}
756
757int
758xfs_fsinumbers_fmt(
759 struct xfs_ibulk *breq,
760 const struct xfs_inumbers *igrp)
761{
762 struct xfs_inogrp ig1;
763
764 xfs_inumbers_to_inogrp(&ig1, igrp);
765 if (copy_to_user(breq->ubuffer, &ig1, sizeof(struct xfs_inogrp)))
766 return -EFAULT;
767 return xfs_ibulk_advance(breq, sizeof(struct xfs_inogrp));
768}
769
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000770STATIC int
David Brazdil0f672f62019-12-10 10:32:29 +0000771xfs_ioc_fsbulkstat(
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000772 xfs_mount_t *mp,
773 unsigned int cmd,
774 void __user *arg)
775{
David Brazdil0f672f62019-12-10 10:32:29 +0000776 struct xfs_fsop_bulkreq bulkreq;
777 struct xfs_ibulk breq = {
778 .mp = mp,
779 .ocount = 0,
780 };
781 xfs_ino_t lastino;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000782 int error;
783
784 /* done = 1 if there are more stats to get and if bulkstat */
785 /* should be called again (unused here, but used in dmapi) */
786
787 if (!capable(CAP_SYS_ADMIN))
788 return -EPERM;
789
790 if (XFS_FORCED_SHUTDOWN(mp))
791 return -EIO;
792
David Brazdil0f672f62019-12-10 10:32:29 +0000793 if (copy_from_user(&bulkreq, arg, sizeof(struct xfs_fsop_bulkreq)))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000794 return -EFAULT;
795
David Brazdil0f672f62019-12-10 10:32:29 +0000796 if (copy_from_user(&lastino, bulkreq.lastip, sizeof(__s64)))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000797 return -EFAULT;
798
David Brazdil0f672f62019-12-10 10:32:29 +0000799 if (bulkreq.icount <= 0)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000800 return -EINVAL;
801
802 if (bulkreq.ubuffer == NULL)
803 return -EINVAL;
804
David Brazdil0f672f62019-12-10 10:32:29 +0000805 breq.ubuffer = bulkreq.ubuffer;
806 breq.icount = bulkreq.icount;
807
808 /*
809 * FSBULKSTAT_SINGLE expects that *lastip contains the inode number
810 * that we want to stat. However, FSINUMBERS and FSBULKSTAT expect
811 * that *lastip contains either zero or the number of the last inode to
812 * be examined by the previous call and return results starting with
813 * the next inode after that. The new bulk request back end functions
814 * take the inode to start with, so we have to compute the startino
815 * parameter from lastino to maintain correct function. lastino == 0
816 * is a special case because it has traditionally meant "first inode
817 * in filesystem".
818 */
819 if (cmd == XFS_IOC_FSINUMBERS) {
820 breq.startino = lastino ? lastino + 1 : 0;
821 error = xfs_inumbers(&breq, xfs_fsinumbers_fmt);
822 lastino = breq.startino - 1;
823 } else if (cmd == XFS_IOC_FSBULKSTAT_SINGLE) {
824 breq.startino = lastino;
825 breq.icount = 1;
826 error = xfs_bulkstat_one(&breq, xfs_fsbulkstat_one_fmt);
827 } else { /* XFS_IOC_FSBULKSTAT */
828 breq.startino = lastino ? lastino + 1 : 0;
829 error = xfs_bulkstat(&breq, xfs_fsbulkstat_one_fmt);
830 lastino = breq.startino - 1;
831 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000832
833 if (error)
834 return error;
835
David Brazdil0f672f62019-12-10 10:32:29 +0000836 if (bulkreq.lastip != NULL &&
837 copy_to_user(bulkreq.lastip, &lastino, sizeof(xfs_ino_t)))
838 return -EFAULT;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000839
David Brazdil0f672f62019-12-10 10:32:29 +0000840 if (bulkreq.ocount != NULL &&
841 copy_to_user(bulkreq.ocount, &breq.ocount, sizeof(__s32)))
842 return -EFAULT;
843
844 return 0;
845}
846
847/* Return 0 on success or positive error */
848static int
849xfs_bulkstat_fmt(
850 struct xfs_ibulk *breq,
851 const struct xfs_bulkstat *bstat)
852{
853 if (copy_to_user(breq->ubuffer, bstat, sizeof(struct xfs_bulkstat)))
854 return -EFAULT;
855 return xfs_ibulk_advance(breq, sizeof(struct xfs_bulkstat));
856}
857
858/*
859 * Check the incoming bulk request @hdr from userspace and initialize the
860 * internal @breq bulk request appropriately. Returns 0 if the bulk request
861 * should proceed; -ECANCELED if there's nothing to do; or the usual
862 * negative error code.
863 */
864static int
865xfs_bulk_ireq_setup(
866 struct xfs_mount *mp,
867 struct xfs_bulk_ireq *hdr,
868 struct xfs_ibulk *breq,
869 void __user *ubuffer)
870{
871 if (hdr->icount == 0 ||
872 (hdr->flags & ~XFS_BULK_IREQ_FLAGS_ALL) ||
873 memchr_inv(hdr->reserved, 0, sizeof(hdr->reserved)))
874 return -EINVAL;
875
876 breq->startino = hdr->ino;
877 breq->ubuffer = ubuffer;
878 breq->icount = hdr->icount;
879 breq->ocount = 0;
880 breq->flags = 0;
881
882 /*
883 * The @ino parameter is a special value, so we must look it up here.
884 * We're not allowed to have IREQ_AGNO, and we only return one inode
885 * worth of data.
886 */
887 if (hdr->flags & XFS_BULK_IREQ_SPECIAL) {
888 if (hdr->flags & XFS_BULK_IREQ_AGNO)
889 return -EINVAL;
890
891 switch (hdr->ino) {
892 case XFS_BULK_IREQ_SPECIAL_ROOT:
893 hdr->ino = mp->m_sb.sb_rootino;
894 break;
895 default:
896 return -EINVAL;
897 }
898 breq->icount = 1;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000899 }
900
David Brazdil0f672f62019-12-10 10:32:29 +0000901 /*
902 * The IREQ_AGNO flag means that we only want results from a given AG.
903 * If @hdr->ino is zero, we start iterating in that AG. If @hdr->ino is
904 * beyond the specified AG then we return no results.
905 */
906 if (hdr->flags & XFS_BULK_IREQ_AGNO) {
907 if (hdr->agno >= mp->m_sb.sb_agcount)
908 return -EINVAL;
909
910 if (breq->startino == 0)
911 breq->startino = XFS_AGINO_TO_INO(mp, hdr->agno, 0);
912 else if (XFS_INO_TO_AGNO(mp, breq->startino) < hdr->agno)
913 return -EINVAL;
914
915 breq->flags |= XFS_IBULK_SAME_AG;
916
917 /* Asking for an inode past the end of the AG? We're done! */
918 if (XFS_INO_TO_AGNO(mp, breq->startino) > hdr->agno)
919 return -ECANCELED;
920 } else if (hdr->agno)
921 return -EINVAL;
922
923 /* Asking for an inode past the end of the FS? We're done! */
924 if (XFS_INO_TO_AGNO(mp, breq->startino) >= mp->m_sb.sb_agcount)
925 return -ECANCELED;
926
927 return 0;
928}
929
930/*
931 * Update the userspace bulk request @hdr to reflect the end state of the
932 * internal bulk request @breq.
933 */
934static void
935xfs_bulk_ireq_teardown(
936 struct xfs_bulk_ireq *hdr,
937 struct xfs_ibulk *breq)
938{
939 hdr->ino = breq->startino;
940 hdr->ocount = breq->ocount;
941}
942
943/* Handle the v5 bulkstat ioctl. */
944STATIC int
945xfs_ioc_bulkstat(
946 struct xfs_mount *mp,
947 unsigned int cmd,
948 struct xfs_bulkstat_req __user *arg)
949{
950 struct xfs_bulk_ireq hdr;
951 struct xfs_ibulk breq = {
952 .mp = mp,
953 };
954 int error;
955
956 if (!capable(CAP_SYS_ADMIN))
957 return -EPERM;
958
959 if (XFS_FORCED_SHUTDOWN(mp))
960 return -EIO;
961
962 if (copy_from_user(&hdr, &arg->hdr, sizeof(hdr)))
963 return -EFAULT;
964
965 error = xfs_bulk_ireq_setup(mp, &hdr, &breq, arg->bulkstat);
966 if (error == -ECANCELED)
967 goto out_teardown;
968 if (error < 0)
969 return error;
970
971 error = xfs_bulkstat(&breq, xfs_bulkstat_fmt);
972 if (error)
973 return error;
974
975out_teardown:
976 xfs_bulk_ireq_teardown(&hdr, &breq);
977 if (copy_to_user(&arg->hdr, &hdr, sizeof(hdr)))
978 return -EFAULT;
979
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000980 return 0;
981}
982
983STATIC int
David Brazdil0f672f62019-12-10 10:32:29 +0000984xfs_inumbers_fmt(
985 struct xfs_ibulk *breq,
986 const struct xfs_inumbers *igrp)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000987{
David Brazdil0f672f62019-12-10 10:32:29 +0000988 if (copy_to_user(breq->ubuffer, igrp, sizeof(struct xfs_inumbers)))
989 return -EFAULT;
990 return xfs_ibulk_advance(breq, sizeof(struct xfs_inumbers));
991}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000992
David Brazdil0f672f62019-12-10 10:32:29 +0000993/* Handle the v5 inumbers ioctl. */
994STATIC int
995xfs_ioc_inumbers(
996 struct xfs_mount *mp,
997 unsigned int cmd,
998 struct xfs_inumbers_req __user *arg)
999{
1000 struct xfs_bulk_ireq hdr;
1001 struct xfs_ibulk breq = {
1002 .mp = mp,
1003 };
1004 int error;
1005
1006 if (!capable(CAP_SYS_ADMIN))
1007 return -EPERM;
1008
1009 if (XFS_FORCED_SHUTDOWN(mp))
1010 return -EIO;
1011
1012 if (copy_from_user(&hdr, &arg->hdr, sizeof(hdr)))
1013 return -EFAULT;
1014
1015 error = xfs_bulk_ireq_setup(mp, &hdr, &breq, arg->inumbers);
1016 if (error == -ECANCELED)
1017 goto out_teardown;
1018 if (error < 0)
1019 return error;
1020
1021 error = xfs_inumbers(&breq, xfs_inumbers_fmt);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001022 if (error)
1023 return error;
1024
David Brazdil0f672f62019-12-10 10:32:29 +00001025out_teardown:
1026 xfs_bulk_ireq_teardown(&hdr, &breq);
1027 if (copy_to_user(&arg->hdr, &hdr, sizeof(hdr)))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001028 return -EFAULT;
David Brazdil0f672f62019-12-10 10:32:29 +00001029
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001030 return 0;
1031}
1032
1033STATIC int
1034xfs_ioc_fsgeometry(
David Brazdil0f672f62019-12-10 10:32:29 +00001035 struct xfs_mount *mp,
1036 void __user *arg,
1037 int struct_version)
1038{
1039 struct xfs_fsop_geom fsgeo;
1040 size_t len;
1041
1042 xfs_fs_geometry(&mp->m_sb, &fsgeo, struct_version);
1043
1044 if (struct_version <= 3)
1045 len = sizeof(struct xfs_fsop_geom_v1);
1046 else if (struct_version == 4)
1047 len = sizeof(struct xfs_fsop_geom_v4);
1048 else {
1049 xfs_fsop_geom_health(mp, &fsgeo);
1050 len = sizeof(fsgeo);
1051 }
1052
1053 if (copy_to_user(arg, &fsgeo, len))
1054 return -EFAULT;
1055 return 0;
1056}
1057
1058STATIC int
1059xfs_ioc_ag_geometry(
1060 struct xfs_mount *mp,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001061 void __user *arg)
1062{
David Brazdil0f672f62019-12-10 10:32:29 +00001063 struct xfs_ag_geometry ageo;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001064 int error;
1065
David Brazdil0f672f62019-12-10 10:32:29 +00001066 if (copy_from_user(&ageo, arg, sizeof(ageo)))
1067 return -EFAULT;
1068 if (ageo.ag_flags)
1069 return -EINVAL;
1070 if (memchr_inv(&ageo.ag_reserved, 0, sizeof(ageo.ag_reserved)))
1071 return -EINVAL;
1072
1073 error = xfs_ag_get_geometry(mp, ageo.ag_number, &ageo);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001074 if (error)
1075 return error;
1076
David Brazdil0f672f62019-12-10 10:32:29 +00001077 if (copy_to_user(arg, &ageo, sizeof(ageo)))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001078 return -EFAULT;
1079 return 0;
1080}
1081
1082/*
1083 * Linux extended inode flags interface.
1084 */
1085
1086STATIC unsigned int
1087xfs_merge_ioc_xflags(
1088 unsigned int flags,
1089 unsigned int start)
1090{
1091 unsigned int xflags = start;
1092
1093 if (flags & FS_IMMUTABLE_FL)
1094 xflags |= FS_XFLAG_IMMUTABLE;
1095 else
1096 xflags &= ~FS_XFLAG_IMMUTABLE;
1097 if (flags & FS_APPEND_FL)
1098 xflags |= FS_XFLAG_APPEND;
1099 else
1100 xflags &= ~FS_XFLAG_APPEND;
1101 if (flags & FS_SYNC_FL)
1102 xflags |= FS_XFLAG_SYNC;
1103 else
1104 xflags &= ~FS_XFLAG_SYNC;
1105 if (flags & FS_NOATIME_FL)
1106 xflags |= FS_XFLAG_NOATIME;
1107 else
1108 xflags &= ~FS_XFLAG_NOATIME;
1109 if (flags & FS_NODUMP_FL)
1110 xflags |= FS_XFLAG_NODUMP;
1111 else
1112 xflags &= ~FS_XFLAG_NODUMP;
1113
1114 return xflags;
1115}
1116
1117STATIC unsigned int
1118xfs_di2lxflags(
1119 uint16_t di_flags)
1120{
1121 unsigned int flags = 0;
1122
1123 if (di_flags & XFS_DIFLAG_IMMUTABLE)
1124 flags |= FS_IMMUTABLE_FL;
1125 if (di_flags & XFS_DIFLAG_APPEND)
1126 flags |= FS_APPEND_FL;
1127 if (di_flags & XFS_DIFLAG_SYNC)
1128 flags |= FS_SYNC_FL;
1129 if (di_flags & XFS_DIFLAG_NOATIME)
1130 flags |= FS_NOATIME_FL;
1131 if (di_flags & XFS_DIFLAG_NODUMP)
1132 flags |= FS_NODUMP_FL;
1133 return flags;
1134}
1135
David Brazdil0f672f62019-12-10 10:32:29 +00001136static void
1137xfs_fill_fsxattr(
1138 struct xfs_inode *ip,
1139 bool attr,
1140 struct fsxattr *fa)
1141{
1142 simple_fill_fsxattr(fa, xfs_ip2xflags(ip));
1143 fa->fsx_extsize = ip->i_d.di_extsize << ip->i_mount->m_sb.sb_blocklog;
1144 fa->fsx_cowextsize = ip->i_d.di_cowextsize <<
1145 ip->i_mount->m_sb.sb_blocklog;
1146 fa->fsx_projid = xfs_get_projid(ip);
1147
1148 if (attr) {
1149 if (ip->i_afp) {
1150 if (ip->i_afp->if_flags & XFS_IFEXTENTS)
1151 fa->fsx_nextents = xfs_iext_count(ip->i_afp);
1152 else
1153 fa->fsx_nextents = ip->i_d.di_anextents;
1154 } else
1155 fa->fsx_nextents = 0;
1156 } else {
1157 if (ip->i_df.if_flags & XFS_IFEXTENTS)
1158 fa->fsx_nextents = xfs_iext_count(&ip->i_df);
1159 else
1160 fa->fsx_nextents = ip->i_d.di_nextents;
1161 }
1162}
1163
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001164STATIC int
1165xfs_ioc_fsgetxattr(
1166 xfs_inode_t *ip,
1167 int attr,
1168 void __user *arg)
1169{
1170 struct fsxattr fa;
1171
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001172 xfs_ilock(ip, XFS_ILOCK_SHARED);
David Brazdil0f672f62019-12-10 10:32:29 +00001173 xfs_fill_fsxattr(ip, attr, &fa);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001174 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1175
1176 if (copy_to_user(arg, &fa, sizeof(fa)))
1177 return -EFAULT;
1178 return 0;
1179}
1180
1181STATIC uint16_t
1182xfs_flags2diflags(
1183 struct xfs_inode *ip,
1184 unsigned int xflags)
1185{
1186 /* can't set PREALLOC this way, just preserve it */
1187 uint16_t di_flags =
1188 (ip->i_d.di_flags & XFS_DIFLAG_PREALLOC);
1189
1190 if (xflags & FS_XFLAG_IMMUTABLE)
1191 di_flags |= XFS_DIFLAG_IMMUTABLE;
1192 if (xflags & FS_XFLAG_APPEND)
1193 di_flags |= XFS_DIFLAG_APPEND;
1194 if (xflags & FS_XFLAG_SYNC)
1195 di_flags |= XFS_DIFLAG_SYNC;
1196 if (xflags & FS_XFLAG_NOATIME)
1197 di_flags |= XFS_DIFLAG_NOATIME;
1198 if (xflags & FS_XFLAG_NODUMP)
1199 di_flags |= XFS_DIFLAG_NODUMP;
1200 if (xflags & FS_XFLAG_NODEFRAG)
1201 di_flags |= XFS_DIFLAG_NODEFRAG;
1202 if (xflags & FS_XFLAG_FILESTREAM)
1203 di_flags |= XFS_DIFLAG_FILESTREAM;
1204 if (S_ISDIR(VFS_I(ip)->i_mode)) {
1205 if (xflags & FS_XFLAG_RTINHERIT)
1206 di_flags |= XFS_DIFLAG_RTINHERIT;
1207 if (xflags & FS_XFLAG_NOSYMLINKS)
1208 di_flags |= XFS_DIFLAG_NOSYMLINKS;
1209 if (xflags & FS_XFLAG_EXTSZINHERIT)
1210 di_flags |= XFS_DIFLAG_EXTSZINHERIT;
1211 if (xflags & FS_XFLAG_PROJINHERIT)
1212 di_flags |= XFS_DIFLAG_PROJINHERIT;
1213 } else if (S_ISREG(VFS_I(ip)->i_mode)) {
1214 if (xflags & FS_XFLAG_REALTIME)
1215 di_flags |= XFS_DIFLAG_REALTIME;
1216 if (xflags & FS_XFLAG_EXTSIZE)
1217 di_flags |= XFS_DIFLAG_EXTSIZE;
1218 }
1219
1220 return di_flags;
1221}
1222
1223STATIC uint64_t
1224xfs_flags2diflags2(
1225 struct xfs_inode *ip,
1226 unsigned int xflags)
1227{
1228 uint64_t di_flags2 =
1229 (ip->i_d.di_flags2 & XFS_DIFLAG2_REFLINK);
1230
1231 if (xflags & FS_XFLAG_DAX)
1232 di_flags2 |= XFS_DIFLAG2_DAX;
1233 if (xflags & FS_XFLAG_COWEXTSIZE)
1234 di_flags2 |= XFS_DIFLAG2_COWEXTSIZE;
1235
1236 return di_flags2;
1237}
1238
1239STATIC void
1240xfs_diflags_to_linux(
1241 struct xfs_inode *ip)
1242{
1243 struct inode *inode = VFS_I(ip);
1244 unsigned int xflags = xfs_ip2xflags(ip);
1245
1246 if (xflags & FS_XFLAG_IMMUTABLE)
1247 inode->i_flags |= S_IMMUTABLE;
1248 else
1249 inode->i_flags &= ~S_IMMUTABLE;
1250 if (xflags & FS_XFLAG_APPEND)
1251 inode->i_flags |= S_APPEND;
1252 else
1253 inode->i_flags &= ~S_APPEND;
1254 if (xflags & FS_XFLAG_SYNC)
1255 inode->i_flags |= S_SYNC;
1256 else
1257 inode->i_flags &= ~S_SYNC;
1258 if (xflags & FS_XFLAG_NOATIME)
1259 inode->i_flags |= S_NOATIME;
1260 else
1261 inode->i_flags &= ~S_NOATIME;
1262#if 0 /* disabled until the flag switching races are sorted out */
1263 if (xflags & FS_XFLAG_DAX)
1264 inode->i_flags |= S_DAX;
1265 else
1266 inode->i_flags &= ~S_DAX;
1267#endif
1268}
1269
1270static int
1271xfs_ioctl_setattr_xflags(
1272 struct xfs_trans *tp,
1273 struct xfs_inode *ip,
1274 struct fsxattr *fa)
1275{
1276 struct xfs_mount *mp = ip->i_mount;
1277 uint64_t di_flags2;
1278
1279 /* Can't change realtime flag if any extents are allocated. */
1280 if ((ip->i_d.di_nextents || ip->i_delayed_blks) &&
1281 XFS_IS_REALTIME_INODE(ip) != (fa->fsx_xflags & FS_XFLAG_REALTIME))
1282 return -EINVAL;
1283
1284 /* If realtime flag is set then must have realtime device */
1285 if (fa->fsx_xflags & FS_XFLAG_REALTIME) {
1286 if (mp->m_sb.sb_rblocks == 0 || mp->m_sb.sb_rextsize == 0 ||
1287 (ip->i_d.di_extsize % mp->m_sb.sb_rextsize))
1288 return -EINVAL;
1289 }
1290
1291 /* Clear reflink if we are actually able to set the rt flag. */
1292 if ((fa->fsx_xflags & FS_XFLAG_REALTIME) && xfs_is_reflink_inode(ip))
1293 ip->i_d.di_flags2 &= ~XFS_DIFLAG2_REFLINK;
1294
1295 /* Don't allow us to set DAX mode for a reflinked file for now. */
1296 if ((fa->fsx_xflags & FS_XFLAG_DAX) && xfs_is_reflink_inode(ip))
1297 return -EINVAL;
1298
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001299 /* diflags2 only valid for v3 inodes. */
1300 di_flags2 = xfs_flags2diflags2(ip, fa->fsx_xflags);
1301 if (di_flags2 && ip->i_d.di_version < 3)
1302 return -EINVAL;
1303
1304 ip->i_d.di_flags = xfs_flags2diflags(ip, fa->fsx_xflags);
1305 ip->i_d.di_flags2 = di_flags2;
1306
1307 xfs_diflags_to_linux(ip);
1308 xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
1309 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1310 XFS_STATS_INC(mp, xs_ig_attrchg);
1311 return 0;
1312}
1313
1314/*
1315 * If we are changing DAX flags, we have to ensure the file is clean and any
1316 * cached objects in the address space are invalidated and removed. This
1317 * requires us to lock out other IO and page faults similar to a truncate
1318 * operation. The locks need to be held until the transaction has been committed
1319 * so that the cache invalidation is atomic with respect to the DAX flag
1320 * manipulation.
1321 */
1322static int
1323xfs_ioctl_setattr_dax_invalidate(
1324 struct xfs_inode *ip,
1325 struct fsxattr *fa,
1326 int *join_flags)
1327{
1328 struct inode *inode = VFS_I(ip);
1329 struct super_block *sb = inode->i_sb;
1330 int error;
1331
1332 *join_flags = 0;
1333
1334 /*
1335 * It is only valid to set the DAX flag on regular files and
1336 * directories on filesystems where the block size is equal to the page
1337 * size. On directories it serves as an inherited hint so we don't
1338 * have to check the device for dax support or flush pagecache.
1339 */
1340 if (fa->fsx_xflags & FS_XFLAG_DAX) {
1341 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)))
1342 return -EINVAL;
David Brazdil0f672f62019-12-10 10:32:29 +00001343 if (!bdev_dax_supported(xfs_find_bdev_for_inode(VFS_I(ip)),
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001344 sb->s_blocksize))
1345 return -EINVAL;
1346 }
1347
1348 /* If the DAX state is not changing, we have nothing to do here. */
1349 if ((fa->fsx_xflags & FS_XFLAG_DAX) && IS_DAX(inode))
1350 return 0;
1351 if (!(fa->fsx_xflags & FS_XFLAG_DAX) && !IS_DAX(inode))
1352 return 0;
1353
1354 if (S_ISDIR(inode->i_mode))
1355 return 0;
1356
1357 /* lock, flush and invalidate mapping in preparation for flag change */
1358 xfs_ilock(ip, XFS_MMAPLOCK_EXCL | XFS_IOLOCK_EXCL);
1359 error = filemap_write_and_wait(inode->i_mapping);
1360 if (error)
1361 goto out_unlock;
1362 error = invalidate_inode_pages2(inode->i_mapping);
1363 if (error)
1364 goto out_unlock;
1365
1366 *join_flags = XFS_MMAPLOCK_EXCL | XFS_IOLOCK_EXCL;
1367 return 0;
1368
1369out_unlock:
1370 xfs_iunlock(ip, XFS_MMAPLOCK_EXCL | XFS_IOLOCK_EXCL);
1371 return error;
1372
1373}
1374
1375/*
1376 * Set up the transaction structure for the setattr operation, checking that we
1377 * have permission to do so. On success, return a clean transaction and the
1378 * inode locked exclusively ready for further operation specific checks. On
1379 * failure, return an error without modifying or locking the inode.
1380 *
1381 * The inode might already be IO locked on call. If this is the case, it is
1382 * indicated in @join_flags and we take full responsibility for ensuring they
1383 * are unlocked from now on. Hence if we have an error here, we still have to
1384 * unlock them. Otherwise, once they are joined to the transaction, they will
1385 * be unlocked on commit/cancel.
1386 */
1387static struct xfs_trans *
1388xfs_ioctl_setattr_get_trans(
1389 struct xfs_inode *ip,
1390 int join_flags)
1391{
1392 struct xfs_mount *mp = ip->i_mount;
1393 struct xfs_trans *tp;
1394 int error = -EROFS;
1395
1396 if (mp->m_flags & XFS_MOUNT_RDONLY)
1397 goto out_unlock;
1398 error = -EIO;
1399 if (XFS_FORCED_SHUTDOWN(mp))
1400 goto out_unlock;
1401
1402 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
1403 if (error)
David Brazdil0f672f62019-12-10 10:32:29 +00001404 goto out_unlock;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001405
1406 xfs_ilock(ip, XFS_ILOCK_EXCL);
1407 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | join_flags);
1408 join_flags = 0;
1409
1410 /*
1411 * CAP_FOWNER overrides the following restrictions:
1412 *
1413 * The user ID of the calling process must be equal to the file owner
1414 * ID, except in cases where the CAP_FSETID capability is applicable.
1415 */
1416 if (!inode_owner_or_capable(VFS_I(ip))) {
1417 error = -EPERM;
1418 goto out_cancel;
1419 }
1420
1421 if (mp->m_flags & XFS_MOUNT_WSYNC)
1422 xfs_trans_set_sync(tp);
1423
1424 return tp;
1425
1426out_cancel:
1427 xfs_trans_cancel(tp);
1428out_unlock:
1429 if (join_flags)
1430 xfs_iunlock(ip, join_flags);
1431 return ERR_PTR(error);
1432}
1433
1434/*
1435 * extent size hint validation is somewhat cumbersome. Rules are:
1436 *
1437 * 1. extent size hint is only valid for directories and regular files
1438 * 2. FS_XFLAG_EXTSIZE is only valid for regular files
1439 * 3. FS_XFLAG_EXTSZINHERIT is only valid for directories.
1440 * 4. can only be changed on regular files if no extents are allocated
1441 * 5. can be changed on directories at any time
1442 * 6. extsize hint of 0 turns off hints, clears inode flags.
1443 * 7. Extent size must be a multiple of the appropriate block size.
1444 * 8. for non-realtime files, the extent size hint must be limited
1445 * to half the AG size to avoid alignment extending the extent beyond the
1446 * limits of the AG.
1447 *
1448 * Please keep this function in sync with xfs_scrub_inode_extsize.
1449 */
1450static int
1451xfs_ioctl_setattr_check_extsize(
1452 struct xfs_inode *ip,
1453 struct fsxattr *fa)
1454{
1455 struct xfs_mount *mp = ip->i_mount;
David Brazdil0f672f62019-12-10 10:32:29 +00001456 xfs_extlen_t size;
1457 xfs_fsblock_t extsize_fsb;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001458
1459 if (S_ISREG(VFS_I(ip)->i_mode) && ip->i_d.di_nextents &&
1460 ((ip->i_d.di_extsize << mp->m_sb.sb_blocklog) != fa->fsx_extsize))
1461 return -EINVAL;
1462
David Brazdil0f672f62019-12-10 10:32:29 +00001463 if (fa->fsx_extsize == 0)
1464 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001465
David Brazdil0f672f62019-12-10 10:32:29 +00001466 extsize_fsb = XFS_B_TO_FSB(mp, fa->fsx_extsize);
1467 if (extsize_fsb > MAXEXTLEN)
1468 return -EINVAL;
1469
1470 if (XFS_IS_REALTIME_INODE(ip) ||
1471 (fa->fsx_xflags & FS_XFLAG_REALTIME)) {
1472 size = mp->m_sb.sb_rextsize << mp->m_sb.sb_blocklog;
1473 } else {
1474 size = mp->m_sb.sb_blocksize;
1475 if (extsize_fsb > mp->m_sb.sb_agblocks / 2)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001476 return -EINVAL;
David Brazdil0f672f62019-12-10 10:32:29 +00001477 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001478
David Brazdil0f672f62019-12-10 10:32:29 +00001479 if (fa->fsx_extsize % size)
1480 return -EINVAL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001481
1482 return 0;
1483}
1484
1485/*
1486 * CoW extent size hint validation rules are:
1487 *
1488 * 1. CoW extent size hint can only be set if reflink is enabled on the fs.
1489 * The inode does not have to have any shared blocks, but it must be a v3.
1490 * 2. FS_XFLAG_COWEXTSIZE is only valid for directories and regular files;
1491 * for a directory, the hint is propagated to new files.
1492 * 3. Can be changed on files & directories at any time.
1493 * 4. CoW extsize hint of 0 turns off hints, clears inode flags.
1494 * 5. Extent size must be a multiple of the appropriate block size.
1495 * 6. The extent size hint must be limited to half the AG size to avoid
1496 * alignment extending the extent beyond the limits of the AG.
1497 *
1498 * Please keep this function in sync with xfs_scrub_inode_cowextsize.
1499 */
1500static int
1501xfs_ioctl_setattr_check_cowextsize(
1502 struct xfs_inode *ip,
1503 struct fsxattr *fa)
1504{
1505 struct xfs_mount *mp = ip->i_mount;
David Brazdil0f672f62019-12-10 10:32:29 +00001506 xfs_extlen_t size;
1507 xfs_fsblock_t cowextsize_fsb;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001508
1509 if (!(fa->fsx_xflags & FS_XFLAG_COWEXTSIZE))
1510 return 0;
1511
1512 if (!xfs_sb_version_hasreflink(&ip->i_mount->m_sb) ||
1513 ip->i_d.di_version != 3)
1514 return -EINVAL;
1515
David Brazdil0f672f62019-12-10 10:32:29 +00001516 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)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001521 return -EINVAL;
1522
David Brazdil0f672f62019-12-10 10:32:29 +00001523 size = mp->m_sb.sb_blocksize;
1524 if (cowextsize_fsb > mp->m_sb.sb_agblocks / 2)
1525 return -EINVAL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001526
David Brazdil0f672f62019-12-10 10:32:29 +00001527 if (fa->fsx_cowextsize % size)
1528 return -EINVAL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001529
1530 return 0;
1531}
1532
1533static int
1534xfs_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;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001542 return 0;
1543}
1544
1545STATIC int
1546xfs_ioctl_setattr(
1547 xfs_inode_t *ip,
1548 struct fsxattr *fa)
1549{
David Brazdil0f672f62019-12-10 10:32:29 +00001550 struct fsxattr old_fa;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001551 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, ip->i_d.di_uid,
1575 ip->i_d.di_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
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001598 if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_PQUOTA_ON(mp) &&
1599 xfs_get_projid(ip) != 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
David Brazdil0f672f62019-12-10 10:32:29 +00001606 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
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001611 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 (xfs_get_projid(ip) != 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 ASSERT(ip->i_d.di_version > 1);
1642 xfs_set_projid(ip, fa->fsx_projid);
1643 }
1644
1645 /*
1646 * Only set the extent size hint if we've already determined that the
1647 * extent size hint should be set on the inode. If no extent size flags
1648 * are set on the inode then unconditionally clear the extent size hint.
1649 */
1650 if (ip->i_d.di_flags & (XFS_DIFLAG_EXTSIZE | XFS_DIFLAG_EXTSZINHERIT))
1651 ip->i_d.di_extsize = fa->fsx_extsize >> mp->m_sb.sb_blocklog;
1652 else
1653 ip->i_d.di_extsize = 0;
1654 if (ip->i_d.di_version == 3 &&
1655 (ip->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE))
1656 ip->i_d.di_cowextsize = fa->fsx_cowextsize >>
1657 mp->m_sb.sb_blocklog;
1658 else
1659 ip->i_d.di_cowextsize = 0;
1660
1661 code = xfs_trans_commit(tp);
1662
1663 /*
1664 * Release any dquot(s) the inode had kept before chown.
1665 */
1666 xfs_qm_dqrele(olddquot);
1667 xfs_qm_dqrele(udqp);
1668 xfs_qm_dqrele(pdqp);
1669
1670 return code;
1671
1672error_trans_cancel:
1673 xfs_trans_cancel(tp);
1674error_free_dquots:
1675 xfs_qm_dqrele(udqp);
1676 xfs_qm_dqrele(pdqp);
1677 return code;
1678}
1679
1680STATIC int
1681xfs_ioc_fssetxattr(
1682 xfs_inode_t *ip,
1683 struct file *filp,
1684 void __user *arg)
1685{
1686 struct fsxattr fa;
1687 int error;
1688
1689 if (copy_from_user(&fa, arg, sizeof(fa)))
1690 return -EFAULT;
1691
1692 error = mnt_want_write_file(filp);
1693 if (error)
1694 return error;
1695 error = xfs_ioctl_setattr(ip, &fa);
1696 mnt_drop_write_file(filp);
1697 return error;
1698}
1699
1700STATIC int
1701xfs_ioc_getxflags(
1702 xfs_inode_t *ip,
1703 void __user *arg)
1704{
1705 unsigned int flags;
1706
1707 flags = xfs_di2lxflags(ip->i_d.di_flags);
1708 if (copy_to_user(arg, &flags, sizeof(flags)))
1709 return -EFAULT;
1710 return 0;
1711}
1712
1713STATIC int
1714xfs_ioc_setxflags(
1715 struct xfs_inode *ip,
1716 struct file *filp,
1717 void __user *arg)
1718{
1719 struct xfs_trans *tp;
1720 struct fsxattr fa;
David Brazdil0f672f62019-12-10 10:32:29 +00001721 struct fsxattr old_fa;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001722 unsigned int flags;
1723 int join_flags = 0;
1724 int error;
1725
1726 if (copy_from_user(&flags, arg, sizeof(flags)))
1727 return -EFAULT;
1728
1729 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
1730 FS_NOATIME_FL | FS_NODUMP_FL | \
1731 FS_SYNC_FL))
1732 return -EOPNOTSUPP;
1733
1734 fa.fsx_xflags = xfs_merge_ioc_xflags(flags, xfs_ip2xflags(ip));
1735
1736 error = mnt_want_write_file(filp);
1737 if (error)
1738 return error;
1739
1740 /*
1741 * Changing DAX config may require inode locking for mapping
1742 * invalidation. These need to be held all the way to transaction commit
1743 * or cancel time, so need to be passed through to
1744 * xfs_ioctl_setattr_get_trans() so it can apply them to the join call
1745 * appropriately.
1746 */
1747 error = xfs_ioctl_setattr_dax_invalidate(ip, &fa, &join_flags);
1748 if (error)
1749 goto out_drop_write;
1750
1751 tp = xfs_ioctl_setattr_get_trans(ip, join_flags);
1752 if (IS_ERR(tp)) {
1753 error = PTR_ERR(tp);
1754 goto out_drop_write;
1755 }
1756
David Brazdil0f672f62019-12-10 10:32:29 +00001757 xfs_fill_fsxattr(ip, false, &old_fa);
1758 error = vfs_ioc_fssetxattr_check(VFS_I(ip), &old_fa, &fa);
1759 if (error) {
1760 xfs_trans_cancel(tp);
1761 goto out_drop_write;
1762 }
1763
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001764 error = xfs_ioctl_setattr_xflags(tp, ip, &fa);
1765 if (error) {
1766 xfs_trans_cancel(tp);
1767 goto out_drop_write;
1768 }
1769
1770 error = xfs_trans_commit(tp);
1771out_drop_write:
1772 mnt_drop_write_file(filp);
1773 return error;
1774}
1775
1776static bool
1777xfs_getbmap_format(
1778 struct kgetbmap *p,
1779 struct getbmapx __user *u,
1780 size_t recsize)
1781{
1782 if (put_user(p->bmv_offset, &u->bmv_offset) ||
1783 put_user(p->bmv_block, &u->bmv_block) ||
1784 put_user(p->bmv_length, &u->bmv_length) ||
1785 put_user(0, &u->bmv_count) ||
1786 put_user(0, &u->bmv_entries))
1787 return false;
1788 if (recsize < sizeof(struct getbmapx))
1789 return true;
1790 if (put_user(0, &u->bmv_iflags) ||
1791 put_user(p->bmv_oflags, &u->bmv_oflags) ||
1792 put_user(0, &u->bmv_unused1) ||
1793 put_user(0, &u->bmv_unused2))
1794 return false;
1795 return true;
1796}
1797
1798STATIC int
1799xfs_ioc_getbmap(
1800 struct file *file,
1801 unsigned int cmd,
1802 void __user *arg)
1803{
1804 struct getbmapx bmx = { 0 };
1805 struct kgetbmap *buf;
1806 size_t recsize;
1807 int error, i;
1808
1809 switch (cmd) {
1810 case XFS_IOC_GETBMAPA:
1811 bmx.bmv_iflags = BMV_IF_ATTRFORK;
1812 /*FALLTHRU*/
1813 case XFS_IOC_GETBMAP:
1814 if (file->f_mode & FMODE_NOCMTIME)
1815 bmx.bmv_iflags |= BMV_IF_NO_DMAPI_READ;
1816 /* struct getbmap is a strict subset of struct getbmapx. */
1817 recsize = sizeof(struct getbmap);
1818 break;
1819 case XFS_IOC_GETBMAPX:
1820 recsize = sizeof(struct getbmapx);
1821 break;
1822 default:
1823 return -EINVAL;
1824 }
1825
1826 if (copy_from_user(&bmx, arg, recsize))
1827 return -EFAULT;
1828
1829 if (bmx.bmv_count < 2)
1830 return -EINVAL;
1831 if (bmx.bmv_count > ULONG_MAX / recsize)
1832 return -ENOMEM;
1833
1834 buf = kmem_zalloc_large(bmx.bmv_count * sizeof(*buf), 0);
1835 if (!buf)
1836 return -ENOMEM;
1837
1838 error = xfs_getbmap(XFS_I(file_inode(file)), &bmx, buf);
1839 if (error)
1840 goto out_free_buf;
1841
1842 error = -EFAULT;
1843 if (copy_to_user(arg, &bmx, recsize))
1844 goto out_free_buf;
1845 arg += recsize;
1846
1847 for (i = 0; i < bmx.bmv_entries; i++) {
1848 if (!xfs_getbmap_format(buf + i, arg, recsize))
1849 goto out_free_buf;
1850 arg += recsize;
1851 }
1852
1853 error = 0;
1854out_free_buf:
1855 kmem_free(buf);
David Brazdil0f672f62019-12-10 10:32:29 +00001856 return error;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001857}
1858
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001859STATIC int
1860xfs_ioc_getfsmap(
1861 struct xfs_inode *ip,
1862 struct fsmap_head __user *arg)
1863{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001864 struct xfs_fsmap_head xhead = {0};
1865 struct fsmap_head head;
Olivier Deprez0e641232021-09-23 10:07:05 +02001866 struct fsmap *recs;
1867 unsigned int count;
1868 __u32 last_flags = 0;
1869 bool done = false;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001870 int error;
1871
1872 if (copy_from_user(&head, arg, sizeof(struct fsmap_head)))
1873 return -EFAULT;
1874 if (memchr_inv(head.fmh_reserved, 0, sizeof(head.fmh_reserved)) ||
1875 memchr_inv(head.fmh_keys[0].fmr_reserved, 0,
1876 sizeof(head.fmh_keys[0].fmr_reserved)) ||
1877 memchr_inv(head.fmh_keys[1].fmr_reserved, 0,
1878 sizeof(head.fmh_keys[1].fmr_reserved)))
1879 return -EINVAL;
1880
Olivier Deprez0e641232021-09-23 10:07:05 +02001881 /*
1882 * Use an internal memory buffer so that we don't have to copy fsmap
1883 * data to userspace while holding locks. Start by trying to allocate
1884 * up to 128k for the buffer, but fall back to a single page if needed.
1885 */
1886 count = min_t(unsigned int, head.fmh_count,
1887 131072 / sizeof(struct fsmap));
1888 recs = kvzalloc(count * sizeof(struct fsmap), GFP_KERNEL);
1889 if (!recs) {
1890 count = min_t(unsigned int, head.fmh_count,
1891 PAGE_SIZE / sizeof(struct fsmap));
1892 recs = kvzalloc(count * sizeof(struct fsmap), GFP_KERNEL);
1893 if (!recs)
1894 return -ENOMEM;
1895 }
1896
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001897 xhead.fmh_iflags = head.fmh_iflags;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001898 xfs_fsmap_to_internal(&xhead.fmh_keys[0], &head.fmh_keys[0]);
1899 xfs_fsmap_to_internal(&xhead.fmh_keys[1], &head.fmh_keys[1]);
1900
1901 trace_xfs_getfsmap_low_key(ip->i_mount, &xhead.fmh_keys[0]);
1902 trace_xfs_getfsmap_high_key(ip->i_mount, &xhead.fmh_keys[1]);
1903
Olivier Deprez0e641232021-09-23 10:07:05 +02001904 head.fmh_entries = 0;
1905 do {
1906 struct fsmap __user *user_recs;
1907 struct fsmap *last_rec;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001908
Olivier Deprez0e641232021-09-23 10:07:05 +02001909 user_recs = &arg->fmh_recs[head.fmh_entries];
1910 xhead.fmh_entries = 0;
1911 xhead.fmh_count = min_t(unsigned int, count,
1912 head.fmh_count - head.fmh_entries);
1913
1914 /* Run query, record how many entries we got. */
1915 error = xfs_getfsmap(ip->i_mount, &xhead, recs);
1916 switch (error) {
1917 case 0:
1918 /*
1919 * There are no more records in the result set. Copy
1920 * whatever we got to userspace and break out.
1921 */
1922 done = true;
1923 break;
1924 case -ECANCELED:
1925 /*
1926 * The internal memory buffer is full. Copy whatever
1927 * records we got to userspace and go again if we have
1928 * not yet filled the userspace buffer.
1929 */
1930 error = 0;
1931 break;
1932 default:
1933 goto out_free;
1934 }
1935 head.fmh_entries += xhead.fmh_entries;
1936 head.fmh_oflags = xhead.fmh_oflags;
1937
1938 /*
1939 * If the caller wanted a record count or there aren't any
1940 * new records to return, we're done.
1941 */
1942 if (head.fmh_count == 0 || xhead.fmh_entries == 0)
1943 break;
1944
1945 /* Copy all the records we got out to userspace. */
1946 if (copy_to_user(user_recs, recs,
1947 xhead.fmh_entries * sizeof(struct fsmap))) {
1948 error = -EFAULT;
1949 goto out_free;
1950 }
1951
1952 /* Remember the last record flags we copied to userspace. */
1953 last_rec = &recs[xhead.fmh_entries - 1];
1954 last_flags = last_rec->fmr_flags;
1955
1956 /* Set up the low key for the next iteration. */
1957 xfs_fsmap_to_internal(&xhead.fmh_keys[0], last_rec);
1958 trace_xfs_getfsmap_low_key(ip->i_mount, &xhead.fmh_keys[0]);
1959 } while (!done && head.fmh_entries < head.fmh_count);
1960
1961 /*
1962 * If there are no more records in the query result set and we're not
1963 * in counting mode, mark the last record returned with the LAST flag.
1964 */
1965 if (done && head.fmh_count > 0 && head.fmh_entries > 0) {
1966 struct fsmap __user *user_rec;
1967
1968 last_flags |= FMR_OF_LAST;
1969 user_rec = &arg->fmh_recs[head.fmh_entries - 1];
1970
1971 if (copy_to_user(&user_rec->fmr_flags, &last_flags,
1972 sizeof(last_flags))) {
1973 error = -EFAULT;
1974 goto out_free;
1975 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001976 }
1977
1978 /* copy back header */
Olivier Deprez0e641232021-09-23 10:07:05 +02001979 if (copy_to_user(arg, &head, sizeof(struct fsmap_head))) {
1980 error = -EFAULT;
1981 goto out_free;
1982 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001983
Olivier Deprez0e641232021-09-23 10:07:05 +02001984out_free:
1985 kmem_free(recs);
1986 return error;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001987}
1988
1989STATIC int
1990xfs_ioc_scrub_metadata(
1991 struct xfs_inode *ip,
1992 void __user *arg)
1993{
1994 struct xfs_scrub_metadata scrub;
1995 int error;
1996
1997 if (!capable(CAP_SYS_ADMIN))
1998 return -EPERM;
1999
2000 if (copy_from_user(&scrub, arg, sizeof(scrub)))
2001 return -EFAULT;
2002
2003 error = xfs_scrub_metadata(ip, &scrub);
2004 if (error)
2005 return error;
2006
2007 if (copy_to_user(arg, &scrub, sizeof(scrub)))
2008 return -EFAULT;
2009
2010 return 0;
2011}
2012
2013int
2014xfs_ioc_swapext(
2015 xfs_swapext_t *sxp)
2016{
2017 xfs_inode_t *ip, *tip;
2018 struct fd f, tmp;
2019 int error = 0;
2020
2021 /* Pull information for the target fd */
2022 f = fdget((int)sxp->sx_fdtarget);
2023 if (!f.file) {
2024 error = -EINVAL;
2025 goto out;
2026 }
2027
2028 if (!(f.file->f_mode & FMODE_WRITE) ||
2029 !(f.file->f_mode & FMODE_READ) ||
2030 (f.file->f_flags & O_APPEND)) {
2031 error = -EBADF;
2032 goto out_put_file;
2033 }
2034
2035 tmp = fdget((int)sxp->sx_fdtmp);
2036 if (!tmp.file) {
2037 error = -EINVAL;
2038 goto out_put_file;
2039 }
2040
2041 if (!(tmp.file->f_mode & FMODE_WRITE) ||
2042 !(tmp.file->f_mode & FMODE_READ) ||
2043 (tmp.file->f_flags & O_APPEND)) {
2044 error = -EBADF;
2045 goto out_put_tmp_file;
2046 }
2047
2048 if (IS_SWAPFILE(file_inode(f.file)) ||
2049 IS_SWAPFILE(file_inode(tmp.file))) {
2050 error = -EINVAL;
2051 goto out_put_tmp_file;
2052 }
2053
2054 /*
2055 * We need to ensure that the fds passed in point to XFS inodes
2056 * before we cast and access them as XFS structures as we have no
2057 * control over what the user passes us here.
2058 */
2059 if (f.file->f_op != &xfs_file_operations ||
2060 tmp.file->f_op != &xfs_file_operations) {
2061 error = -EINVAL;
2062 goto out_put_tmp_file;
2063 }
2064
2065 ip = XFS_I(file_inode(f.file));
2066 tip = XFS_I(file_inode(tmp.file));
2067
2068 if (ip->i_mount != tip->i_mount) {
2069 error = -EINVAL;
2070 goto out_put_tmp_file;
2071 }
2072
2073 if (ip->i_ino == tip->i_ino) {
2074 error = -EINVAL;
2075 goto out_put_tmp_file;
2076 }
2077
2078 if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
2079 error = -EIO;
2080 goto out_put_tmp_file;
2081 }
2082
2083 error = xfs_swap_extents(ip, tip, sxp);
2084
2085 out_put_tmp_file:
2086 fdput(tmp);
2087 out_put_file:
2088 fdput(f);
2089 out:
2090 return error;
2091}
2092
2093static int
2094xfs_ioc_getlabel(
2095 struct xfs_mount *mp,
2096 char __user *user_label)
2097{
2098 struct xfs_sb *sbp = &mp->m_sb;
2099 char label[XFSLABEL_MAX + 1];
2100
2101 /* Paranoia */
2102 BUILD_BUG_ON(sizeof(sbp->sb_fname) > FSLABEL_MAX);
2103
2104 /* 1 larger than sb_fname, so this ensures a trailing NUL char */
2105 memset(label, 0, sizeof(label));
2106 spin_lock(&mp->m_sb_lock);
2107 strncpy(label, sbp->sb_fname, XFSLABEL_MAX);
2108 spin_unlock(&mp->m_sb_lock);
2109
2110 if (copy_to_user(user_label, label, sizeof(label)))
2111 return -EFAULT;
2112 return 0;
2113}
2114
2115static int
2116xfs_ioc_setlabel(
2117 struct file *filp,
2118 struct xfs_mount *mp,
2119 char __user *newlabel)
2120{
2121 struct xfs_sb *sbp = &mp->m_sb;
2122 char label[XFSLABEL_MAX + 1];
2123 size_t len;
2124 int error;
2125
2126 if (!capable(CAP_SYS_ADMIN))
2127 return -EPERM;
2128 /*
2129 * The generic ioctl allows up to FSLABEL_MAX chars, but XFS is much
2130 * smaller, at 12 bytes. We copy one more to be sure we find the
2131 * (required) NULL character to test the incoming label length.
2132 * NB: The on disk label doesn't need to be null terminated.
2133 */
2134 if (copy_from_user(label, newlabel, XFSLABEL_MAX + 1))
2135 return -EFAULT;
2136 len = strnlen(label, XFSLABEL_MAX + 1);
2137 if (len > sizeof(sbp->sb_fname))
2138 return -EINVAL;
2139
2140 error = mnt_want_write_file(filp);
2141 if (error)
2142 return error;
2143
2144 spin_lock(&mp->m_sb_lock);
2145 memset(sbp->sb_fname, 0, sizeof(sbp->sb_fname));
2146 memcpy(sbp->sb_fname, label, len);
2147 spin_unlock(&mp->m_sb_lock);
2148
2149 /*
2150 * Now we do several things to satisfy userspace.
2151 * In addition to normal logging of the primary superblock, we also
2152 * immediately write these changes to sector zero for the primary, then
2153 * update all backup supers (as xfs_db does for a label change), then
2154 * invalidate the block device page cache. This is so that any prior
2155 * buffered reads from userspace (i.e. from blkid) are invalidated,
2156 * and userspace will see the newly-written label.
2157 */
2158 error = xfs_sync_sb_buf(mp);
2159 if (error)
2160 goto out;
2161 /*
2162 * growfs also updates backup supers so lock against that.
2163 */
2164 mutex_lock(&mp->m_growlock);
2165 error = xfs_update_secondary_sbs(mp);
2166 mutex_unlock(&mp->m_growlock);
2167
2168 invalidate_bdev(mp->m_ddev_targp->bt_bdev);
2169
2170out:
2171 mnt_drop_write_file(filp);
2172 return error;
2173}
2174
2175/*
2176 * Note: some of the ioctl's return positive numbers as a
2177 * byte count indicating success, such as readlink_by_handle.
2178 * So we don't "sign flip" like most other routines. This means
2179 * true errors need to be returned as a negative value.
2180 */
2181long
2182xfs_file_ioctl(
2183 struct file *filp,
2184 unsigned int cmd,
2185 unsigned long p)
2186{
2187 struct inode *inode = file_inode(filp);
2188 struct xfs_inode *ip = XFS_I(inode);
2189 struct xfs_mount *mp = ip->i_mount;
2190 void __user *arg = (void __user *)p;
2191 int error;
2192
2193 trace_xfs_file_ioctl(ip);
2194
2195 switch (cmd) {
2196 case FITRIM:
2197 return xfs_ioc_trim(mp, arg);
2198 case FS_IOC_GETFSLABEL:
2199 return xfs_ioc_getlabel(mp, arg);
2200 case FS_IOC_SETFSLABEL:
2201 return xfs_ioc_setlabel(filp, mp, arg);
2202 case XFS_IOC_ALLOCSP:
2203 case XFS_IOC_FREESP:
2204 case XFS_IOC_RESVSP:
2205 case XFS_IOC_UNRESVSP:
2206 case XFS_IOC_ALLOCSP64:
2207 case XFS_IOC_FREESP64:
2208 case XFS_IOC_RESVSP64:
2209 case XFS_IOC_UNRESVSP64:
2210 case XFS_IOC_ZERO_RANGE: {
2211 xfs_flock64_t bf;
2212
2213 if (copy_from_user(&bf, arg, sizeof(bf)))
2214 return -EFAULT;
2215 return xfs_ioc_space(filp, cmd, &bf);
2216 }
2217 case XFS_IOC_DIOINFO: {
2218 struct dioattr da;
2219 xfs_buftarg_t *target =
2220 XFS_IS_REALTIME_INODE(ip) ?
2221 mp->m_rtdev_targp : mp->m_ddev_targp;
2222
2223 da.d_mem = da.d_miniosz = target->bt_logical_sectorsize;
2224 da.d_maxiosz = INT_MAX & ~(da.d_miniosz - 1);
2225
2226 if (copy_to_user(arg, &da, sizeof(da)))
2227 return -EFAULT;
2228 return 0;
2229 }
2230
2231 case XFS_IOC_FSBULKSTAT_SINGLE:
2232 case XFS_IOC_FSBULKSTAT:
2233 case XFS_IOC_FSINUMBERS:
David Brazdil0f672f62019-12-10 10:32:29 +00002234 return xfs_ioc_fsbulkstat(mp, cmd, arg);
2235
2236 case XFS_IOC_BULKSTAT:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002237 return xfs_ioc_bulkstat(mp, cmd, arg);
David Brazdil0f672f62019-12-10 10:32:29 +00002238 case XFS_IOC_INUMBERS:
2239 return xfs_ioc_inumbers(mp, cmd, arg);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002240
2241 case XFS_IOC_FSGEOMETRY_V1:
David Brazdil0f672f62019-12-10 10:32:29 +00002242 return xfs_ioc_fsgeometry(mp, arg, 3);
2243 case XFS_IOC_FSGEOMETRY_V4:
2244 return xfs_ioc_fsgeometry(mp, arg, 4);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002245 case XFS_IOC_FSGEOMETRY:
David Brazdil0f672f62019-12-10 10:32:29 +00002246 return xfs_ioc_fsgeometry(mp, arg, 5);
2247
2248 case XFS_IOC_AG_GEOMETRY:
2249 return xfs_ioc_ag_geometry(mp, arg);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002250
2251 case XFS_IOC_GETVERSION:
2252 return put_user(inode->i_generation, (int __user *)arg);
2253
2254 case XFS_IOC_FSGETXATTR:
2255 return xfs_ioc_fsgetxattr(ip, 0, arg);
2256 case XFS_IOC_FSGETXATTRA:
2257 return xfs_ioc_fsgetxattr(ip, 1, arg);
2258 case XFS_IOC_FSSETXATTR:
2259 return xfs_ioc_fssetxattr(ip, filp, arg);
2260 case XFS_IOC_GETXFLAGS:
2261 return xfs_ioc_getxflags(ip, arg);
2262 case XFS_IOC_SETXFLAGS:
2263 return xfs_ioc_setxflags(ip, filp, arg);
2264
2265 case XFS_IOC_FSSETDM: {
2266 struct fsdmidata dmi;
2267
2268 if (copy_from_user(&dmi, arg, sizeof(dmi)))
2269 return -EFAULT;
2270
2271 error = mnt_want_write_file(filp);
2272 if (error)
2273 return error;
2274
2275 error = xfs_set_dmattrs(ip, dmi.fsd_dmevmask,
2276 dmi.fsd_dmstate);
2277 mnt_drop_write_file(filp);
2278 return error;
2279 }
2280
2281 case XFS_IOC_GETBMAP:
2282 case XFS_IOC_GETBMAPA:
2283 case XFS_IOC_GETBMAPX:
2284 return xfs_ioc_getbmap(filp, cmd, arg);
2285
2286 case FS_IOC_GETFSMAP:
2287 return xfs_ioc_getfsmap(ip, arg);
2288
2289 case XFS_IOC_SCRUB_METADATA:
2290 return xfs_ioc_scrub_metadata(ip, arg);
2291
2292 case XFS_IOC_FD_TO_HANDLE:
2293 case XFS_IOC_PATH_TO_HANDLE:
2294 case XFS_IOC_PATH_TO_FSHANDLE: {
2295 xfs_fsop_handlereq_t hreq;
2296
2297 if (copy_from_user(&hreq, arg, sizeof(hreq)))
2298 return -EFAULT;
2299 return xfs_find_handle(cmd, &hreq);
2300 }
2301 case XFS_IOC_OPEN_BY_HANDLE: {
2302 xfs_fsop_handlereq_t hreq;
2303
2304 if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
2305 return -EFAULT;
2306 return xfs_open_by_handle(filp, &hreq);
2307 }
2308 case XFS_IOC_FSSETDM_BY_HANDLE:
2309 return xfs_fssetdm_by_handle(filp, arg);
2310
2311 case XFS_IOC_READLINK_BY_HANDLE: {
2312 xfs_fsop_handlereq_t hreq;
2313
2314 if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
2315 return -EFAULT;
2316 return xfs_readlink_by_handle(filp, &hreq);
2317 }
2318 case XFS_IOC_ATTRLIST_BY_HANDLE:
2319 return xfs_attrlist_by_handle(filp, arg);
2320
2321 case XFS_IOC_ATTRMULTI_BY_HANDLE:
2322 return xfs_attrmulti_by_handle(filp, arg);
2323
2324 case XFS_IOC_SWAPEXT: {
2325 struct xfs_swapext sxp;
2326
2327 if (copy_from_user(&sxp, arg, sizeof(xfs_swapext_t)))
2328 return -EFAULT;
2329 error = mnt_want_write_file(filp);
2330 if (error)
2331 return error;
2332 error = xfs_ioc_swapext(&sxp);
2333 mnt_drop_write_file(filp);
2334 return error;
2335 }
2336
2337 case XFS_IOC_FSCOUNTS: {
2338 xfs_fsop_counts_t out;
2339
David Brazdil0f672f62019-12-10 10:32:29 +00002340 xfs_fs_counts(mp, &out);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002341
2342 if (copy_to_user(arg, &out, sizeof(out)))
2343 return -EFAULT;
2344 return 0;
2345 }
2346
2347 case XFS_IOC_SET_RESBLKS: {
2348 xfs_fsop_resblks_t inout;
2349 uint64_t in;
2350
2351 if (!capable(CAP_SYS_ADMIN))
2352 return -EPERM;
2353
2354 if (mp->m_flags & XFS_MOUNT_RDONLY)
2355 return -EROFS;
2356
2357 if (copy_from_user(&inout, arg, sizeof(inout)))
2358 return -EFAULT;
2359
2360 error = mnt_want_write_file(filp);
2361 if (error)
2362 return error;
2363
2364 /* input parameter is passed in resblks field of structure */
2365 in = inout.resblks;
2366 error = xfs_reserve_blocks(mp, &in, &inout);
2367 mnt_drop_write_file(filp);
2368 if (error)
2369 return error;
2370
2371 if (copy_to_user(arg, &inout, sizeof(inout)))
2372 return -EFAULT;
2373 return 0;
2374 }
2375
2376 case XFS_IOC_GET_RESBLKS: {
2377 xfs_fsop_resblks_t out;
2378
2379 if (!capable(CAP_SYS_ADMIN))
2380 return -EPERM;
2381
2382 error = xfs_reserve_blocks(mp, NULL, &out);
2383 if (error)
2384 return error;
2385
2386 if (copy_to_user(arg, &out, sizeof(out)))
2387 return -EFAULT;
2388
2389 return 0;
2390 }
2391
2392 case XFS_IOC_FSGROWFSDATA: {
2393 xfs_growfs_data_t in;
2394
2395 if (copy_from_user(&in, arg, sizeof(in)))
2396 return -EFAULT;
2397
2398 error = mnt_want_write_file(filp);
2399 if (error)
2400 return error;
2401 error = xfs_growfs_data(mp, &in);
2402 mnt_drop_write_file(filp);
2403 return error;
2404 }
2405
2406 case XFS_IOC_FSGROWFSLOG: {
2407 xfs_growfs_log_t in;
2408
2409 if (copy_from_user(&in, arg, sizeof(in)))
2410 return -EFAULT;
2411
2412 error = mnt_want_write_file(filp);
2413 if (error)
2414 return error;
2415 error = xfs_growfs_log(mp, &in);
2416 mnt_drop_write_file(filp);
2417 return error;
2418 }
2419
2420 case XFS_IOC_FSGROWFSRT: {
2421 xfs_growfs_rt_t in;
2422
2423 if (copy_from_user(&in, arg, sizeof(in)))
2424 return -EFAULT;
2425
2426 error = mnt_want_write_file(filp);
2427 if (error)
2428 return error;
2429 error = xfs_growfs_rt(mp, &in);
2430 mnt_drop_write_file(filp);
2431 return error;
2432 }
2433
2434 case XFS_IOC_GOINGDOWN: {
2435 uint32_t in;
2436
2437 if (!capable(CAP_SYS_ADMIN))
2438 return -EPERM;
2439
2440 if (get_user(in, (uint32_t __user *)arg))
2441 return -EFAULT;
2442
2443 return xfs_fs_goingdown(mp, in);
2444 }
2445
2446 case XFS_IOC_ERROR_INJECTION: {
2447 xfs_error_injection_t in;
2448
2449 if (!capable(CAP_SYS_ADMIN))
2450 return -EPERM;
2451
2452 if (copy_from_user(&in, arg, sizeof(in)))
2453 return -EFAULT;
2454
2455 return xfs_errortag_add(mp, in.errtag);
2456 }
2457
2458 case XFS_IOC_ERROR_CLEARALL:
2459 if (!capable(CAP_SYS_ADMIN))
2460 return -EPERM;
2461
2462 return xfs_errortag_clearall(mp);
2463
2464 case XFS_IOC_FREE_EOFBLOCKS: {
2465 struct xfs_fs_eofblocks eofb;
2466 struct xfs_eofblocks keofb;
2467
2468 if (!capable(CAP_SYS_ADMIN))
2469 return -EPERM;
2470
2471 if (mp->m_flags & XFS_MOUNT_RDONLY)
2472 return -EROFS;
2473
2474 if (copy_from_user(&eofb, arg, sizeof(eofb)))
2475 return -EFAULT;
2476
2477 error = xfs_fs_eofblocks_from_user(&eofb, &keofb);
2478 if (error)
2479 return error;
2480
Olivier Deprez0e641232021-09-23 10:07:05 +02002481 sb_start_write(mp->m_super);
2482 error = xfs_icache_free_eofblocks(mp, &keofb);
2483 sb_end_write(mp->m_super);
2484 return error;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002485 }
2486
2487 default:
2488 return -ENOTTY;
2489 }
2490}