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