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