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