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