Merge branch 'fix/hda' into for-linus
[sfrench/cifs-2.6.git] / fs / xfs / linux-2.6 / xfs_ioctl.c
1 /*
2  * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3  * All Rights Reserved.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it would be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write the Free Software Foundation,
16  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18 #include "xfs.h"
19 #include "xfs_fs.h"
20 #include "xfs_bit.h"
21 #include "xfs_log.h"
22 #include "xfs_inum.h"
23 #include "xfs_trans.h"
24 #include "xfs_sb.h"
25 #include "xfs_ag.h"
26 #include "xfs_dir2.h"
27 #include "xfs_alloc.h"
28 #include "xfs_dmapi.h"
29 #include "xfs_mount.h"
30 #include "xfs_bmap_btree.h"
31 #include "xfs_alloc_btree.h"
32 #include "xfs_ialloc_btree.h"
33 #include "xfs_attr_sf.h"
34 #include "xfs_dir2_sf.h"
35 #include "xfs_dinode.h"
36 #include "xfs_inode.h"
37 #include "xfs_ioctl.h"
38 #include "xfs_btree.h"
39 #include "xfs_ialloc.h"
40 #include "xfs_rtalloc.h"
41 #include "xfs_itable.h"
42 #include "xfs_error.h"
43 #include "xfs_rw.h"
44 #include "xfs_acl.h"
45 #include "xfs_attr.h"
46 #include "xfs_bmap.h"
47 #include "xfs_buf_item.h"
48 #include "xfs_utils.h"
49 #include "xfs_dfrag.h"
50 #include "xfs_fsops.h"
51 #include "xfs_vnodeops.h"
52 #include "xfs_quota.h"
53 #include "xfs_inode_item.h"
54 #include "xfs_export.h"
55
56 #include <linux/capability.h>
57 #include <linux/dcache.h>
58 #include <linux/mount.h>
59 #include <linux/namei.h>
60 #include <linux/pagemap.h>
61 #include <linux/exportfs.h>
62
63 /*
64  * xfs_find_handle maps from userspace xfs_fsop_handlereq structure to
65  * a file or fs handle.
66  *
67  * XFS_IOC_PATH_TO_FSHANDLE
68  *    returns fs handle for a mount point or path within that mount point
69  * XFS_IOC_FD_TO_HANDLE
70  *    returns full handle for a FD opened in user space
71  * XFS_IOC_PATH_TO_HANDLE
72  *    returns full handle for a path
73  */
74 int
75 xfs_find_handle(
76         unsigned int            cmd,
77         xfs_fsop_handlereq_t    *hreq)
78 {
79         int                     hsize;
80         xfs_handle_t            handle;
81         struct inode            *inode;
82         struct file             *file = NULL;
83         struct path             path;
84         int                     error;
85         struct xfs_inode        *ip;
86
87         if (cmd == XFS_IOC_FD_TO_HANDLE) {
88                 file = fget(hreq->fd);
89                 if (!file)
90                         return -EBADF;
91                 inode = file->f_path.dentry->d_inode;
92         } else {
93                 error = user_lpath((const char __user *)hreq->path, &path);
94                 if (error)
95                         return error;
96                 inode = path.dentry->d_inode;
97         }
98         ip = XFS_I(inode);
99
100         /*
101          * We can only generate handles for inodes residing on a XFS filesystem,
102          * and only for regular files, directories or symbolic links.
103          */
104         error = -EINVAL;
105         if (inode->i_sb->s_magic != XFS_SB_MAGIC)
106                 goto out_put;
107
108         error = -EBADF;
109         if (!S_ISREG(inode->i_mode) &&
110             !S_ISDIR(inode->i_mode) &&
111             !S_ISLNK(inode->i_mode))
112                 goto out_put;
113
114
115         memcpy(&handle.ha_fsid, ip->i_mount->m_fixedfsid, sizeof(xfs_fsid_t));
116
117         if (cmd == XFS_IOC_PATH_TO_FSHANDLE) {
118                 /*
119                  * This handle only contains an fsid, zero the rest.
120                  */
121                 memset(&handle.ha_fid, 0, sizeof(handle.ha_fid));
122                 hsize = sizeof(xfs_fsid_t);
123         } else {
124                 int             lock_mode;
125
126                 lock_mode = xfs_ilock_map_shared(ip);
127                 handle.ha_fid.fid_len = sizeof(xfs_fid_t) -
128                                         sizeof(handle.ha_fid.fid_len);
129                 handle.ha_fid.fid_pad = 0;
130                 handle.ha_fid.fid_gen = ip->i_d.di_gen;
131                 handle.ha_fid.fid_ino = ip->i_ino;
132                 xfs_iunlock_map_shared(ip, lock_mode);
133
134                 hsize = XFS_HSIZE(handle);
135         }
136
137         error = -EFAULT;
138         if (copy_to_user(hreq->ohandle, &handle, hsize) ||
139             copy_to_user(hreq->ohandlen, &hsize, sizeof(__s32)))
140                 goto out_put;
141
142         error = 0;
143
144  out_put:
145         if (cmd == XFS_IOC_FD_TO_HANDLE)
146                 fput(file);
147         else
148                 path_put(&path);
149         return error;
150 }
151
152 /*
153  * No need to do permission checks on the various pathname components
154  * as the handle operations are privileged.
155  */
156 STATIC int
157 xfs_handle_acceptable(
158         void                    *context,
159         struct dentry           *dentry)
160 {
161         return 1;
162 }
163
164 /*
165  * Convert userspace handle data into a dentry.
166  */
167 struct dentry *
168 xfs_handle_to_dentry(
169         struct file             *parfilp,
170         void __user             *uhandle,
171         u32                     hlen)
172 {
173         xfs_handle_t            handle;
174         struct xfs_fid64        fid;
175
176         /*
177          * Only allow handle opens under a directory.
178          */
179         if (!S_ISDIR(parfilp->f_path.dentry->d_inode->i_mode))
180                 return ERR_PTR(-ENOTDIR);
181
182         if (hlen != sizeof(xfs_handle_t))
183                 return ERR_PTR(-EINVAL);
184         if (copy_from_user(&handle, uhandle, hlen))
185                 return ERR_PTR(-EFAULT);
186         if (handle.ha_fid.fid_len !=
187             sizeof(handle.ha_fid) - sizeof(handle.ha_fid.fid_len))
188                 return ERR_PTR(-EINVAL);
189
190         memset(&fid, 0, sizeof(struct fid));
191         fid.ino = handle.ha_fid.fid_ino;
192         fid.gen = handle.ha_fid.fid_gen;
193
194         return exportfs_decode_fh(parfilp->f_path.mnt, (struct fid *)&fid, 3,
195                         FILEID_INO32_GEN | XFS_FILEID_TYPE_64FLAG,
196                         xfs_handle_acceptable, NULL);
197 }
198
199 STATIC struct dentry *
200 xfs_handlereq_to_dentry(
201         struct file             *parfilp,
202         xfs_fsop_handlereq_t    *hreq)
203 {
204         return xfs_handle_to_dentry(parfilp, hreq->ihandle, hreq->ihandlen);
205 }
206
207 int
208 xfs_open_by_handle(
209         struct file             *parfilp,
210         xfs_fsop_handlereq_t    *hreq)
211 {
212         const struct cred       *cred = current_cred();
213         int                     error;
214         int                     fd;
215         int                     permflag;
216         struct file             *filp;
217         struct inode            *inode;
218         struct dentry           *dentry;
219
220         if (!capable(CAP_SYS_ADMIN))
221                 return -XFS_ERROR(EPERM);
222
223         dentry = xfs_handlereq_to_dentry(parfilp, hreq);
224         if (IS_ERR(dentry))
225                 return PTR_ERR(dentry);
226         inode = dentry->d_inode;
227
228         /* Restrict xfs_open_by_handle to directories & regular files. */
229         if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode))) {
230                 error = -XFS_ERROR(EPERM);
231                 goto out_dput;
232         }
233
234 #if BITS_PER_LONG != 32
235         hreq->oflags |= O_LARGEFILE;
236 #endif
237
238         /* Put open permission in namei format. */
239         permflag = hreq->oflags;
240         if ((permflag+1) & O_ACCMODE)
241                 permflag++;
242         if (permflag & O_TRUNC)
243                 permflag |= 2;
244
245         if ((!(permflag & O_APPEND) || (permflag & O_TRUNC)) &&
246             (permflag & FMODE_WRITE) && IS_APPEND(inode)) {
247                 error = -XFS_ERROR(EPERM);
248                 goto out_dput;
249         }
250
251         if ((permflag & FMODE_WRITE) && IS_IMMUTABLE(inode)) {
252                 error = -XFS_ERROR(EACCES);
253                 goto out_dput;
254         }
255
256         /* Can't write directories. */
257         if (S_ISDIR(inode->i_mode) && (permflag & FMODE_WRITE)) {
258                 error = -XFS_ERROR(EISDIR);
259                 goto out_dput;
260         }
261
262         fd = get_unused_fd();
263         if (fd < 0) {
264                 error = fd;
265                 goto out_dput;
266         }
267
268         filp = dentry_open(dentry, mntget(parfilp->f_path.mnt),
269                            hreq->oflags, cred);
270         if (IS_ERR(filp)) {
271                 put_unused_fd(fd);
272                 return PTR_ERR(filp);
273         }
274
275         if (inode->i_mode & S_IFREG) {
276                 filp->f_flags |= O_NOATIME;
277                 filp->f_mode |= FMODE_NOCMTIME;
278         }
279
280         fd_install(fd, filp);
281         return fd;
282
283  out_dput:
284         dput(dentry);
285         return error;
286 }
287
288 /*
289  * This is a copy from fs/namei.c:vfs_readlink(), except for removing it's
290  * unused first argument.
291  */
292 STATIC int
293 do_readlink(
294         char __user             *buffer,
295         int                     buflen,
296         const char              *link)
297 {
298         int len;
299
300         len = PTR_ERR(link);
301         if (IS_ERR(link))
302                 goto out;
303
304         len = strlen(link);
305         if (len > (unsigned) buflen)
306                 len = buflen;
307         if (copy_to_user(buffer, link, len))
308                 len = -EFAULT;
309  out:
310         return len;
311 }
312
313
314 int
315 xfs_readlink_by_handle(
316         struct file             *parfilp,
317         xfs_fsop_handlereq_t    *hreq)
318 {
319         struct dentry           *dentry;
320         __u32                   olen;
321         void                    *link;
322         int                     error;
323
324         if (!capable(CAP_SYS_ADMIN))
325                 return -XFS_ERROR(EPERM);
326
327         dentry = xfs_handlereq_to_dentry(parfilp, hreq);
328         if (IS_ERR(dentry))
329                 return PTR_ERR(dentry);
330
331         /* Restrict this handle operation to symlinks only. */
332         if (!S_ISLNK(dentry->d_inode->i_mode)) {
333                 error = -XFS_ERROR(EINVAL);
334                 goto out_dput;
335         }
336
337         if (copy_from_user(&olen, hreq->ohandlen, sizeof(__u32))) {
338                 error = -XFS_ERROR(EFAULT);
339                 goto out_dput;
340         }
341
342         link = kmalloc(MAXPATHLEN+1, GFP_KERNEL);
343         if (!link) {
344                 error = -XFS_ERROR(ENOMEM);
345                 goto out_dput;
346         }
347
348         error = -xfs_readlink(XFS_I(dentry->d_inode), link);
349         if (error)
350                 goto out_kfree;
351         error = do_readlink(hreq->ohandle, olen, link);
352         if (error)
353                 goto out_kfree;
354
355  out_kfree:
356         kfree(link);
357  out_dput:
358         dput(dentry);
359         return error;
360 }
361
362 STATIC int
363 xfs_fssetdm_by_handle(
364         struct file             *parfilp,
365         void                    __user *arg)
366 {
367         int                     error;
368         struct fsdmidata        fsd;
369         xfs_fsop_setdm_handlereq_t dmhreq;
370         struct dentry           *dentry;
371
372         if (!capable(CAP_MKNOD))
373                 return -XFS_ERROR(EPERM);
374         if (copy_from_user(&dmhreq, arg, sizeof(xfs_fsop_setdm_handlereq_t)))
375                 return -XFS_ERROR(EFAULT);
376
377         dentry = xfs_handlereq_to_dentry(parfilp, &dmhreq.hreq);
378         if (IS_ERR(dentry))
379                 return PTR_ERR(dentry);
380
381         if (IS_IMMUTABLE(dentry->d_inode) || IS_APPEND(dentry->d_inode)) {
382                 error = -XFS_ERROR(EPERM);
383                 goto out;
384         }
385
386         if (copy_from_user(&fsd, dmhreq.data, sizeof(fsd))) {
387                 error = -XFS_ERROR(EFAULT);
388                 goto out;
389         }
390
391         error = -xfs_set_dmattrs(XFS_I(dentry->d_inode), fsd.fsd_dmevmask,
392                                  fsd.fsd_dmstate);
393
394  out:
395         dput(dentry);
396         return error;
397 }
398
399 STATIC int
400 xfs_attrlist_by_handle(
401         struct file             *parfilp,
402         void                    __user *arg)
403 {
404         int                     error = -ENOMEM;
405         attrlist_cursor_kern_t  *cursor;
406         xfs_fsop_attrlist_handlereq_t al_hreq;
407         struct dentry           *dentry;
408         char                    *kbuf;
409
410         if (!capable(CAP_SYS_ADMIN))
411                 return -XFS_ERROR(EPERM);
412         if (copy_from_user(&al_hreq, arg, sizeof(xfs_fsop_attrlist_handlereq_t)))
413                 return -XFS_ERROR(EFAULT);
414         if (al_hreq.buflen > XATTR_LIST_MAX)
415                 return -XFS_ERROR(EINVAL);
416
417         /*
418          * Reject flags, only allow namespaces.
419          */
420         if (al_hreq.flags & ~(ATTR_ROOT | ATTR_SECURE))
421                 return -XFS_ERROR(EINVAL);
422
423         dentry = xfs_handlereq_to_dentry(parfilp, &al_hreq.hreq);
424         if (IS_ERR(dentry))
425                 return PTR_ERR(dentry);
426
427         kbuf = kmalloc(al_hreq.buflen, GFP_KERNEL);
428         if (!kbuf)
429                 goto out_dput;
430
431         cursor = (attrlist_cursor_kern_t *)&al_hreq.pos;
432         error = -xfs_attr_list(XFS_I(dentry->d_inode), kbuf, al_hreq.buflen,
433                                         al_hreq.flags, cursor);
434         if (error)
435                 goto out_kfree;
436
437         if (copy_to_user(al_hreq.buffer, kbuf, al_hreq.buflen))
438                 error = -EFAULT;
439
440  out_kfree:
441         kfree(kbuf);
442  out_dput:
443         dput(dentry);
444         return error;
445 }
446
447 int
448 xfs_attrmulti_attr_get(
449         struct inode            *inode,
450         char                    *name,
451         char                    __user *ubuf,
452         __uint32_t              *len,
453         __uint32_t              flags)
454 {
455         char                    *kbuf;
456         int                     error = EFAULT;
457
458         if (*len > XATTR_SIZE_MAX)
459                 return EINVAL;
460         kbuf = kmalloc(*len, GFP_KERNEL);
461         if (!kbuf)
462                 return ENOMEM;
463
464         error = xfs_attr_get(XFS_I(inode), name, kbuf, (int *)len, flags);
465         if (error)
466                 goto out_kfree;
467
468         if (copy_to_user(ubuf, kbuf, *len))
469                 error = EFAULT;
470
471  out_kfree:
472         kfree(kbuf);
473         return error;
474 }
475
476 int
477 xfs_attrmulti_attr_set(
478         struct inode            *inode,
479         char                    *name,
480         const char              __user *ubuf,
481         __uint32_t              len,
482         __uint32_t              flags)
483 {
484         char                    *kbuf;
485         int                     error = EFAULT;
486
487         if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
488                 return EPERM;
489         if (len > XATTR_SIZE_MAX)
490                 return EINVAL;
491
492         kbuf = memdup_user(ubuf, len);
493         if (IS_ERR(kbuf))
494                 return PTR_ERR(kbuf);
495
496         error = xfs_attr_set(XFS_I(inode), name, kbuf, len, flags);
497
498         return error;
499 }
500
501 int
502 xfs_attrmulti_attr_remove(
503         struct inode            *inode,
504         char                    *name,
505         __uint32_t              flags)
506 {
507         if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
508                 return EPERM;
509         return xfs_attr_remove(XFS_I(inode), name, flags);
510 }
511
512 STATIC int
513 xfs_attrmulti_by_handle(
514         struct file             *parfilp,
515         void                    __user *arg)
516 {
517         int                     error;
518         xfs_attr_multiop_t      *ops;
519         xfs_fsop_attrmulti_handlereq_t am_hreq;
520         struct dentry           *dentry;
521         unsigned int            i, size;
522         char                    *attr_name;
523
524         if (!capable(CAP_SYS_ADMIN))
525                 return -XFS_ERROR(EPERM);
526         if (copy_from_user(&am_hreq, arg, sizeof(xfs_fsop_attrmulti_handlereq_t)))
527                 return -XFS_ERROR(EFAULT);
528
529         dentry = xfs_handlereq_to_dentry(parfilp, &am_hreq.hreq);
530         if (IS_ERR(dentry))
531                 return PTR_ERR(dentry);
532
533         error = E2BIG;
534         size = am_hreq.opcount * sizeof(xfs_attr_multiop_t);
535         if (!size || size > 16 * PAGE_SIZE)
536                 goto out_dput;
537
538         ops = memdup_user(am_hreq.ops, size);
539         if (IS_ERR(ops)) {
540                 error = PTR_ERR(ops);
541                 goto out_dput;
542         }
543
544         attr_name = kmalloc(MAXNAMELEN, GFP_KERNEL);
545         if (!attr_name)
546                 goto out_kfree_ops;
547
548         error = 0;
549         for (i = 0; i < am_hreq.opcount; i++) {
550                 ops[i].am_error = strncpy_from_user(attr_name,
551                                 ops[i].am_attrname, MAXNAMELEN);
552                 if (ops[i].am_error == 0 || ops[i].am_error == MAXNAMELEN)
553                         error = -ERANGE;
554                 if (ops[i].am_error < 0)
555                         break;
556
557                 switch (ops[i].am_opcode) {
558                 case ATTR_OP_GET:
559                         ops[i].am_error = xfs_attrmulti_attr_get(
560                                         dentry->d_inode, attr_name,
561                                         ops[i].am_attrvalue, &ops[i].am_length,
562                                         ops[i].am_flags);
563                         break;
564                 case ATTR_OP_SET:
565                         ops[i].am_error = mnt_want_write(parfilp->f_path.mnt);
566                         if (ops[i].am_error)
567                                 break;
568                         ops[i].am_error = xfs_attrmulti_attr_set(
569                                         dentry->d_inode, attr_name,
570                                         ops[i].am_attrvalue, ops[i].am_length,
571                                         ops[i].am_flags);
572                         mnt_drop_write(parfilp->f_path.mnt);
573                         break;
574                 case ATTR_OP_REMOVE:
575                         ops[i].am_error = mnt_want_write(parfilp->f_path.mnt);
576                         if (ops[i].am_error)
577                                 break;
578                         ops[i].am_error = xfs_attrmulti_attr_remove(
579                                         dentry->d_inode, attr_name,
580                                         ops[i].am_flags);
581                         mnt_drop_write(parfilp->f_path.mnt);
582                         break;
583                 default:
584                         ops[i].am_error = EINVAL;
585                 }
586         }
587
588         if (copy_to_user(am_hreq.ops, ops, size))
589                 error = XFS_ERROR(EFAULT);
590
591         kfree(attr_name);
592  out_kfree_ops:
593         kfree(ops);
594  out_dput:
595         dput(dentry);
596         return -error;
597 }
598
599 int
600 xfs_ioc_space(
601         struct xfs_inode        *ip,
602         struct inode            *inode,
603         struct file             *filp,
604         int                     ioflags,
605         unsigned int            cmd,
606         xfs_flock64_t           *bf)
607 {
608         int                     attr_flags = 0;
609         int                     error;
610
611         /*
612          * Only allow the sys admin to reserve space unless
613          * unwritten extents are enabled.
614          */
615         if (!xfs_sb_version_hasextflgbit(&ip->i_mount->m_sb) &&
616             !capable(CAP_SYS_ADMIN))
617                 return -XFS_ERROR(EPERM);
618
619         if (inode->i_flags & (S_IMMUTABLE|S_APPEND))
620                 return -XFS_ERROR(EPERM);
621
622         if (!(filp->f_mode & FMODE_WRITE))
623                 return -XFS_ERROR(EBADF);
624
625         if (!S_ISREG(inode->i_mode))
626                 return -XFS_ERROR(EINVAL);
627
628         if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
629                 attr_flags |= XFS_ATTR_NONBLOCK;
630         if (ioflags & IO_INVIS)
631                 attr_flags |= XFS_ATTR_DMI;
632
633         error = xfs_change_file_space(ip, cmd, bf, filp->f_pos, attr_flags);
634         return -error;
635 }
636
637 STATIC int
638 xfs_ioc_bulkstat(
639         xfs_mount_t             *mp,
640         unsigned int            cmd,
641         void                    __user *arg)
642 {
643         xfs_fsop_bulkreq_t      bulkreq;
644         int                     count;  /* # of records returned */
645         xfs_ino_t               inlast; /* last inode number */
646         int                     done;
647         int                     error;
648
649         /* done = 1 if there are more stats to get and if bulkstat */
650         /* should be called again (unused here, but used in dmapi) */
651
652         if (!capable(CAP_SYS_ADMIN))
653                 return -EPERM;
654
655         if (XFS_FORCED_SHUTDOWN(mp))
656                 return -XFS_ERROR(EIO);
657
658         if (copy_from_user(&bulkreq, arg, sizeof(xfs_fsop_bulkreq_t)))
659                 return -XFS_ERROR(EFAULT);
660
661         if (copy_from_user(&inlast, bulkreq.lastip, sizeof(__s64)))
662                 return -XFS_ERROR(EFAULT);
663
664         if ((count = bulkreq.icount) <= 0)
665                 return -XFS_ERROR(EINVAL);
666
667         if (bulkreq.ubuffer == NULL)
668                 return -XFS_ERROR(EINVAL);
669
670         if (cmd == XFS_IOC_FSINUMBERS)
671                 error = xfs_inumbers(mp, &inlast, &count,
672                                         bulkreq.ubuffer, xfs_inumbers_fmt);
673         else if (cmd == XFS_IOC_FSBULKSTAT_SINGLE)
674                 error = xfs_bulkstat_single(mp, &inlast,
675                                                 bulkreq.ubuffer, &done);
676         else    /* XFS_IOC_FSBULKSTAT */
677                 error = xfs_bulkstat(mp, &inlast, &count,
678                         (bulkstat_one_pf)xfs_bulkstat_one, NULL,
679                         sizeof(xfs_bstat_t), bulkreq.ubuffer,
680                         BULKSTAT_FG_QUICK, &done);
681
682         if (error)
683                 return -error;
684
685         if (bulkreq.ocount != NULL) {
686                 if (copy_to_user(bulkreq.lastip, &inlast,
687                                                 sizeof(xfs_ino_t)))
688                         return -XFS_ERROR(EFAULT);
689
690                 if (copy_to_user(bulkreq.ocount, &count, sizeof(count)))
691                         return -XFS_ERROR(EFAULT);
692         }
693
694         return 0;
695 }
696
697 STATIC int
698 xfs_ioc_fsgeometry_v1(
699         xfs_mount_t             *mp,
700         void                    __user *arg)
701 {
702         xfs_fsop_geom_v1_t      fsgeo;
703         int                     error;
704
705         error = xfs_fs_geometry(mp, (xfs_fsop_geom_t *)&fsgeo, 3);
706         if (error)
707                 return -error;
708
709         if (copy_to_user(arg, &fsgeo, sizeof(fsgeo)))
710                 return -XFS_ERROR(EFAULT);
711         return 0;
712 }
713
714 STATIC int
715 xfs_ioc_fsgeometry(
716         xfs_mount_t             *mp,
717         void                    __user *arg)
718 {
719         xfs_fsop_geom_t         fsgeo;
720         int                     error;
721
722         error = xfs_fs_geometry(mp, &fsgeo, 4);
723         if (error)
724                 return -error;
725
726         if (copy_to_user(arg, &fsgeo, sizeof(fsgeo)))
727                 return -XFS_ERROR(EFAULT);
728         return 0;
729 }
730
731 /*
732  * Linux extended inode flags interface.
733  */
734
735 STATIC unsigned int
736 xfs_merge_ioc_xflags(
737         unsigned int    flags,
738         unsigned int    start)
739 {
740         unsigned int    xflags = start;
741
742         if (flags & FS_IMMUTABLE_FL)
743                 xflags |= XFS_XFLAG_IMMUTABLE;
744         else
745                 xflags &= ~XFS_XFLAG_IMMUTABLE;
746         if (flags & FS_APPEND_FL)
747                 xflags |= XFS_XFLAG_APPEND;
748         else
749                 xflags &= ~XFS_XFLAG_APPEND;
750         if (flags & FS_SYNC_FL)
751                 xflags |= XFS_XFLAG_SYNC;
752         else
753                 xflags &= ~XFS_XFLAG_SYNC;
754         if (flags & FS_NOATIME_FL)
755                 xflags |= XFS_XFLAG_NOATIME;
756         else
757                 xflags &= ~XFS_XFLAG_NOATIME;
758         if (flags & FS_NODUMP_FL)
759                 xflags |= XFS_XFLAG_NODUMP;
760         else
761                 xflags &= ~XFS_XFLAG_NODUMP;
762
763         return xflags;
764 }
765
766 STATIC unsigned int
767 xfs_di2lxflags(
768         __uint16_t      di_flags)
769 {
770         unsigned int    flags = 0;
771
772         if (di_flags & XFS_DIFLAG_IMMUTABLE)
773                 flags |= FS_IMMUTABLE_FL;
774         if (di_flags & XFS_DIFLAG_APPEND)
775                 flags |= FS_APPEND_FL;
776         if (di_flags & XFS_DIFLAG_SYNC)
777                 flags |= FS_SYNC_FL;
778         if (di_flags & XFS_DIFLAG_NOATIME)
779                 flags |= FS_NOATIME_FL;
780         if (di_flags & XFS_DIFLAG_NODUMP)
781                 flags |= FS_NODUMP_FL;
782         return flags;
783 }
784
785 STATIC int
786 xfs_ioc_fsgetxattr(
787         xfs_inode_t             *ip,
788         int                     attr,
789         void                    __user *arg)
790 {
791         struct fsxattr          fa;
792
793         xfs_ilock(ip, XFS_ILOCK_SHARED);
794         fa.fsx_xflags = xfs_ip2xflags(ip);
795         fa.fsx_extsize = ip->i_d.di_extsize << ip->i_mount->m_sb.sb_blocklog;
796         fa.fsx_projid = ip->i_d.di_projid;
797
798         if (attr) {
799                 if (ip->i_afp) {
800                         if (ip->i_afp->if_flags & XFS_IFEXTENTS)
801                                 fa.fsx_nextents = ip->i_afp->if_bytes /
802                                                         sizeof(xfs_bmbt_rec_t);
803                         else
804                                 fa.fsx_nextents = ip->i_d.di_anextents;
805                 } else
806                         fa.fsx_nextents = 0;
807         } else {
808                 if (ip->i_df.if_flags & XFS_IFEXTENTS)
809                         fa.fsx_nextents = ip->i_df.if_bytes /
810                                                 sizeof(xfs_bmbt_rec_t);
811                 else
812                         fa.fsx_nextents = ip->i_d.di_nextents;
813         }
814         xfs_iunlock(ip, XFS_ILOCK_SHARED);
815
816         if (copy_to_user(arg, &fa, sizeof(fa)))
817                 return -EFAULT;
818         return 0;
819 }
820
821 STATIC void
822 xfs_set_diflags(
823         struct xfs_inode        *ip,
824         unsigned int            xflags)
825 {
826         unsigned int            di_flags;
827
828         /* can't set PREALLOC this way, just preserve it */
829         di_flags = (ip->i_d.di_flags & XFS_DIFLAG_PREALLOC);
830         if (xflags & XFS_XFLAG_IMMUTABLE)
831                 di_flags |= XFS_DIFLAG_IMMUTABLE;
832         if (xflags & XFS_XFLAG_APPEND)
833                 di_flags |= XFS_DIFLAG_APPEND;
834         if (xflags & XFS_XFLAG_SYNC)
835                 di_flags |= XFS_DIFLAG_SYNC;
836         if (xflags & XFS_XFLAG_NOATIME)
837                 di_flags |= XFS_DIFLAG_NOATIME;
838         if (xflags & XFS_XFLAG_NODUMP)
839                 di_flags |= XFS_DIFLAG_NODUMP;
840         if (xflags & XFS_XFLAG_PROJINHERIT)
841                 di_flags |= XFS_DIFLAG_PROJINHERIT;
842         if (xflags & XFS_XFLAG_NODEFRAG)
843                 di_flags |= XFS_DIFLAG_NODEFRAG;
844         if (xflags & XFS_XFLAG_FILESTREAM)
845                 di_flags |= XFS_DIFLAG_FILESTREAM;
846         if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
847                 if (xflags & XFS_XFLAG_RTINHERIT)
848                         di_flags |= XFS_DIFLAG_RTINHERIT;
849                 if (xflags & XFS_XFLAG_NOSYMLINKS)
850                         di_flags |= XFS_DIFLAG_NOSYMLINKS;
851                 if (xflags & XFS_XFLAG_EXTSZINHERIT)
852                         di_flags |= XFS_DIFLAG_EXTSZINHERIT;
853         } else if ((ip->i_d.di_mode & S_IFMT) == S_IFREG) {
854                 if (xflags & XFS_XFLAG_REALTIME)
855                         di_flags |= XFS_DIFLAG_REALTIME;
856                 if (xflags & XFS_XFLAG_EXTSIZE)
857                         di_flags |= XFS_DIFLAG_EXTSIZE;
858         }
859
860         ip->i_d.di_flags = di_flags;
861 }
862
863 STATIC void
864 xfs_diflags_to_linux(
865         struct xfs_inode        *ip)
866 {
867         struct inode            *inode = VFS_I(ip);
868         unsigned int            xflags = xfs_ip2xflags(ip);
869
870         if (xflags & XFS_XFLAG_IMMUTABLE)
871                 inode->i_flags |= S_IMMUTABLE;
872         else
873                 inode->i_flags &= ~S_IMMUTABLE;
874         if (xflags & XFS_XFLAG_APPEND)
875                 inode->i_flags |= S_APPEND;
876         else
877                 inode->i_flags &= ~S_APPEND;
878         if (xflags & XFS_XFLAG_SYNC)
879                 inode->i_flags |= S_SYNC;
880         else
881                 inode->i_flags &= ~S_SYNC;
882         if (xflags & XFS_XFLAG_NOATIME)
883                 inode->i_flags |= S_NOATIME;
884         else
885                 inode->i_flags &= ~S_NOATIME;
886 }
887
888 #define FSX_PROJID      1
889 #define FSX_EXTSIZE     2
890 #define FSX_XFLAGS      4
891 #define FSX_NONBLOCK    8
892
893 STATIC int
894 xfs_ioctl_setattr(
895         xfs_inode_t             *ip,
896         struct fsxattr          *fa,
897         int                     mask)
898 {
899         struct xfs_mount        *mp = ip->i_mount;
900         struct xfs_trans        *tp;
901         unsigned int            lock_flags = 0;
902         struct xfs_dquot        *udqp = NULL, *gdqp = NULL;
903         struct xfs_dquot        *olddquot = NULL;
904         int                     code;
905
906         xfs_itrace_entry(ip);
907
908         if (mp->m_flags & XFS_MOUNT_RDONLY)
909                 return XFS_ERROR(EROFS);
910         if (XFS_FORCED_SHUTDOWN(mp))
911                 return XFS_ERROR(EIO);
912
913         /*
914          * If disk quotas is on, we make sure that the dquots do exist on disk,
915          * before we start any other transactions. Trying to do this later
916          * is messy. We don't care to take a readlock to look at the ids
917          * in inode here, because we can't hold it across the trans_reserve.
918          * If the IDs do change before we take the ilock, we're covered
919          * because the i_*dquot fields will get updated anyway.
920          */
921         if (XFS_IS_QUOTA_ON(mp) && (mask & FSX_PROJID)) {
922                 code = XFS_QM_DQVOPALLOC(mp, ip, ip->i_d.di_uid,
923                                          ip->i_d.di_gid, fa->fsx_projid,
924                                          XFS_QMOPT_PQUOTA, &udqp, &gdqp);
925                 if (code)
926                         return code;
927         }
928
929         /*
930          * For the other attributes, we acquire the inode lock and
931          * first do an error checking pass.
932          */
933         tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
934         code = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES(mp), 0, 0, 0);
935         if (code)
936                 goto error_return;
937
938         lock_flags = XFS_ILOCK_EXCL;
939         xfs_ilock(ip, lock_flags);
940
941         /*
942          * CAP_FOWNER overrides the following restrictions:
943          *
944          * The user ID of the calling process must be equal
945          * to the file owner ID, except in cases where the
946          * CAP_FSETID capability is applicable.
947          */
948         if (current_fsuid() != ip->i_d.di_uid && !capable(CAP_FOWNER)) {
949                 code = XFS_ERROR(EPERM);
950                 goto error_return;
951         }
952
953         /*
954          * Do a quota reservation only if projid is actually going to change.
955          */
956         if (mask & FSX_PROJID) {
957                 if (XFS_IS_PQUOTA_ON(mp) &&
958                     ip->i_d.di_projid != fa->fsx_projid) {
959                         ASSERT(tp);
960                         code = XFS_QM_DQVOPCHOWNRESV(mp, tp, ip, udqp, gdqp,
961                                                 capable(CAP_FOWNER) ?
962                                                 XFS_QMOPT_FORCE_RES : 0);
963                         if (code)       /* out of quota */
964                                 goto error_return;
965                 }
966         }
967
968         if (mask & FSX_EXTSIZE) {
969                 /*
970                  * Can't change extent size if any extents are allocated.
971                  */
972                 if (ip->i_d.di_nextents &&
973                     ((ip->i_d.di_extsize << mp->m_sb.sb_blocklog) !=
974                      fa->fsx_extsize)) {
975                         code = XFS_ERROR(EINVAL);       /* EFBIG? */
976                         goto error_return;
977                 }
978
979                 /*
980                  * Extent size must be a multiple of the appropriate block
981                  * size, if set at all.
982                  */
983                 if (fa->fsx_extsize != 0) {
984                         xfs_extlen_t    size;
985
986                         if (XFS_IS_REALTIME_INODE(ip) ||
987                             ((mask & FSX_XFLAGS) &&
988                             (fa->fsx_xflags & XFS_XFLAG_REALTIME))) {
989                                 size = mp->m_sb.sb_rextsize <<
990                                        mp->m_sb.sb_blocklog;
991                         } else {
992                                 size = mp->m_sb.sb_blocksize;
993                         }
994
995                         if (fa->fsx_extsize % size) {
996                                 code = XFS_ERROR(EINVAL);
997                                 goto error_return;
998                         }
999                 }
1000         }
1001
1002
1003         if (mask & FSX_XFLAGS) {
1004                 /*
1005                  * Can't change realtime flag if any extents are allocated.
1006                  */
1007                 if ((ip->i_d.di_nextents || ip->i_delayed_blks) &&
1008                     (XFS_IS_REALTIME_INODE(ip)) !=
1009                     (fa->fsx_xflags & XFS_XFLAG_REALTIME)) {
1010                         code = XFS_ERROR(EINVAL);       /* EFBIG? */
1011                         goto error_return;
1012                 }
1013
1014                 /*
1015                  * If realtime flag is set then must have realtime data.
1016                  */
1017                 if ((fa->fsx_xflags & XFS_XFLAG_REALTIME)) {
1018                         if ((mp->m_sb.sb_rblocks == 0) ||
1019                             (mp->m_sb.sb_rextsize == 0) ||
1020                             (ip->i_d.di_extsize % mp->m_sb.sb_rextsize)) {
1021                                 code = XFS_ERROR(EINVAL);
1022                                 goto error_return;
1023                         }
1024                 }
1025
1026                 /*
1027                  * Can't modify an immutable/append-only file unless
1028                  * we have appropriate permission.
1029                  */
1030                 if ((ip->i_d.di_flags &
1031                                 (XFS_DIFLAG_IMMUTABLE|XFS_DIFLAG_APPEND) ||
1032                      (fa->fsx_xflags &
1033                                 (XFS_XFLAG_IMMUTABLE | XFS_XFLAG_APPEND))) &&
1034                     !capable(CAP_LINUX_IMMUTABLE)) {
1035                         code = XFS_ERROR(EPERM);
1036                         goto error_return;
1037                 }
1038         }
1039
1040         xfs_trans_ijoin(tp, ip, lock_flags);
1041         xfs_trans_ihold(tp, ip);
1042
1043         /*
1044          * Change file ownership.  Must be the owner or privileged.
1045          */
1046         if (mask & FSX_PROJID) {
1047                 /*
1048                  * CAP_FSETID overrides the following restrictions:
1049                  *
1050                  * The set-user-ID and set-group-ID bits of a file will be
1051                  * cleared upon successful return from chown()
1052                  */
1053                 if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
1054                     !capable(CAP_FSETID))
1055                         ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
1056
1057                 /*
1058                  * Change the ownerships and register quota modifications
1059                  * in the transaction.
1060                  */
1061                 if (ip->i_d.di_projid != fa->fsx_projid) {
1062                         if (XFS_IS_PQUOTA_ON(mp)) {
1063                                 olddquot = XFS_QM_DQVOPCHOWN(mp, tp, ip,
1064                                                         &ip->i_gdquot, gdqp);
1065                         }
1066                         ip->i_d.di_projid = fa->fsx_projid;
1067
1068                         /*
1069                          * We may have to rev the inode as well as
1070                          * the superblock version number since projids didn't
1071                          * exist before DINODE_VERSION_2 and SB_VERSION_NLINK.
1072                          */
1073                         if (ip->i_d.di_version == 1)
1074                                 xfs_bump_ino_vers2(tp, ip);
1075                 }
1076
1077         }
1078
1079         if (mask & FSX_EXTSIZE)
1080                 ip->i_d.di_extsize = fa->fsx_extsize >> mp->m_sb.sb_blocklog;
1081         if (mask & FSX_XFLAGS) {
1082                 xfs_set_diflags(ip, fa->fsx_xflags);
1083                 xfs_diflags_to_linux(ip);
1084         }
1085
1086         xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1087         xfs_ichgtime(ip, XFS_ICHGTIME_CHG);
1088
1089         XFS_STATS_INC(xs_ig_attrchg);
1090
1091         /*
1092          * If this is a synchronous mount, make sure that the
1093          * transaction goes to disk before returning to the user.
1094          * This is slightly sub-optimal in that truncates require
1095          * two sync transactions instead of one for wsync filesystems.
1096          * One for the truncate and one for the timestamps since we
1097          * don't want to change the timestamps unless we're sure the
1098          * truncate worked.  Truncates are less than 1% of the laddis
1099          * mix so this probably isn't worth the trouble to optimize.
1100          */
1101         if (mp->m_flags & XFS_MOUNT_WSYNC)
1102                 xfs_trans_set_sync(tp);
1103         code = xfs_trans_commit(tp, 0);
1104         xfs_iunlock(ip, lock_flags);
1105
1106         /*
1107          * Release any dquot(s) the inode had kept before chown.
1108          */
1109         XFS_QM_DQRELE(mp, olddquot);
1110         XFS_QM_DQRELE(mp, udqp);
1111         XFS_QM_DQRELE(mp, gdqp);
1112
1113         if (code)
1114                 return code;
1115
1116         if (DM_EVENT_ENABLED(ip, DM_EVENT_ATTRIBUTE)) {
1117                 XFS_SEND_NAMESP(mp, DM_EVENT_ATTRIBUTE, ip, DM_RIGHT_NULL,
1118                                 NULL, DM_RIGHT_NULL, NULL, NULL, 0, 0,
1119                                 (mask & FSX_NONBLOCK) ? DM_FLAGS_NDELAY : 0);
1120         }
1121
1122         return 0;
1123
1124  error_return:
1125         XFS_QM_DQRELE(mp, udqp);
1126         XFS_QM_DQRELE(mp, gdqp);
1127         xfs_trans_cancel(tp, 0);
1128         if (lock_flags)
1129                 xfs_iunlock(ip, lock_flags);
1130         return code;
1131 }
1132
1133 STATIC int
1134 xfs_ioc_fssetxattr(
1135         xfs_inode_t             *ip,
1136         struct file             *filp,
1137         void                    __user *arg)
1138 {
1139         struct fsxattr          fa;
1140         unsigned int            mask;
1141
1142         if (copy_from_user(&fa, arg, sizeof(fa)))
1143                 return -EFAULT;
1144
1145         mask = FSX_XFLAGS | FSX_EXTSIZE | FSX_PROJID;
1146         if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
1147                 mask |= FSX_NONBLOCK;
1148
1149         return -xfs_ioctl_setattr(ip, &fa, mask);
1150 }
1151
1152 STATIC int
1153 xfs_ioc_getxflags(
1154         xfs_inode_t             *ip,
1155         void                    __user *arg)
1156 {
1157         unsigned int            flags;
1158
1159         flags = xfs_di2lxflags(ip->i_d.di_flags);
1160         if (copy_to_user(arg, &flags, sizeof(flags)))
1161                 return -EFAULT;
1162         return 0;
1163 }
1164
1165 STATIC int
1166 xfs_ioc_setxflags(
1167         xfs_inode_t             *ip,
1168         struct file             *filp,
1169         void                    __user *arg)
1170 {
1171         struct fsxattr          fa;
1172         unsigned int            flags;
1173         unsigned int            mask;
1174
1175         if (copy_from_user(&flags, arg, sizeof(flags)))
1176                 return -EFAULT;
1177
1178         if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
1179                       FS_NOATIME_FL | FS_NODUMP_FL | \
1180                       FS_SYNC_FL))
1181                 return -EOPNOTSUPP;
1182
1183         mask = FSX_XFLAGS;
1184         if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
1185                 mask |= FSX_NONBLOCK;
1186         fa.fsx_xflags = xfs_merge_ioc_xflags(flags, xfs_ip2xflags(ip));
1187
1188         return -xfs_ioctl_setattr(ip, &fa, mask);
1189 }
1190
1191 STATIC int
1192 xfs_getbmap_format(void **ap, struct getbmapx *bmv, int *full)
1193 {
1194         struct getbmap __user   *base = *ap;
1195
1196         /* copy only getbmap portion (not getbmapx) */
1197         if (copy_to_user(base, bmv, sizeof(struct getbmap)))
1198                 return XFS_ERROR(EFAULT);
1199
1200         *ap += sizeof(struct getbmap);
1201         return 0;
1202 }
1203
1204 STATIC int
1205 xfs_ioc_getbmap(
1206         struct xfs_inode        *ip,
1207         int                     ioflags,
1208         unsigned int            cmd,
1209         void                    __user *arg)
1210 {
1211         struct getbmapx         bmx;
1212         int                     error;
1213
1214         if (copy_from_user(&bmx, arg, sizeof(struct getbmapx)))
1215                 return -XFS_ERROR(EFAULT);
1216
1217         if (bmx.bmv_count < 2)
1218                 return -XFS_ERROR(EINVAL);
1219
1220         bmx.bmv_iflags = (cmd == XFS_IOC_GETBMAPA ? BMV_IF_ATTRFORK : 0);
1221         if (ioflags & IO_INVIS)
1222                 bmx.bmv_iflags |= BMV_IF_NO_DMAPI_READ;
1223
1224         error = xfs_getbmap(ip, &bmx, xfs_getbmap_format,
1225                             (struct getbmap *)arg+1);
1226         if (error)
1227                 return -error;
1228
1229         /* copy back header - only size of getbmap */
1230         if (copy_to_user(arg, &bmx, sizeof(struct getbmap)))
1231                 return -XFS_ERROR(EFAULT);
1232         return 0;
1233 }
1234
1235 STATIC int
1236 xfs_getbmapx_format(void **ap, struct getbmapx *bmv, int *full)
1237 {
1238         struct getbmapx __user  *base = *ap;
1239
1240         if (copy_to_user(base, bmv, sizeof(struct getbmapx)))
1241                 return XFS_ERROR(EFAULT);
1242
1243         *ap += sizeof(struct getbmapx);
1244         return 0;
1245 }
1246
1247 STATIC int
1248 xfs_ioc_getbmapx(
1249         struct xfs_inode        *ip,
1250         void                    __user *arg)
1251 {
1252         struct getbmapx         bmx;
1253         int                     error;
1254
1255         if (copy_from_user(&bmx, arg, sizeof(bmx)))
1256                 return -XFS_ERROR(EFAULT);
1257
1258         if (bmx.bmv_count < 2)
1259                 return -XFS_ERROR(EINVAL);
1260
1261         if (bmx.bmv_iflags & (~BMV_IF_VALID))
1262                 return -XFS_ERROR(EINVAL);
1263
1264         error = xfs_getbmap(ip, &bmx, xfs_getbmapx_format,
1265                             (struct getbmapx *)arg+1);
1266         if (error)
1267                 return -error;
1268
1269         /* copy back header */
1270         if (copy_to_user(arg, &bmx, sizeof(struct getbmapx)))
1271                 return -XFS_ERROR(EFAULT);
1272
1273         return 0;
1274 }
1275
1276 /*
1277  * Note: some of the ioctl's return positive numbers as a
1278  * byte count indicating success, such as readlink_by_handle.
1279  * So we don't "sign flip" like most other routines.  This means
1280  * true errors need to be returned as a negative value.
1281  */
1282 long
1283 xfs_file_ioctl(
1284         struct file             *filp,
1285         unsigned int            cmd,
1286         unsigned long           p)
1287 {
1288         struct inode            *inode = filp->f_path.dentry->d_inode;
1289         struct xfs_inode        *ip = XFS_I(inode);
1290         struct xfs_mount        *mp = ip->i_mount;
1291         void                    __user *arg = (void __user *)p;
1292         int                     ioflags = 0;
1293         int                     error;
1294
1295         if (filp->f_mode & FMODE_NOCMTIME)
1296                 ioflags |= IO_INVIS;
1297
1298         xfs_itrace_entry(ip);
1299
1300         switch (cmd) {
1301         case XFS_IOC_ALLOCSP:
1302         case XFS_IOC_FREESP:
1303         case XFS_IOC_RESVSP:
1304         case XFS_IOC_UNRESVSP:
1305         case XFS_IOC_ALLOCSP64:
1306         case XFS_IOC_FREESP64:
1307         case XFS_IOC_RESVSP64:
1308         case XFS_IOC_UNRESVSP64: {
1309                 xfs_flock64_t           bf;
1310
1311                 if (copy_from_user(&bf, arg, sizeof(bf)))
1312                         return -XFS_ERROR(EFAULT);
1313                 return xfs_ioc_space(ip, inode, filp, ioflags, cmd, &bf);
1314         }
1315         case XFS_IOC_DIOINFO: {
1316                 struct dioattr  da;
1317                 xfs_buftarg_t   *target =
1318                         XFS_IS_REALTIME_INODE(ip) ?
1319                         mp->m_rtdev_targp : mp->m_ddev_targp;
1320
1321                 da.d_mem = da.d_miniosz = 1 << target->bt_sshift;
1322                 da.d_maxiosz = INT_MAX & ~(da.d_miniosz - 1);
1323
1324                 if (copy_to_user(arg, &da, sizeof(da)))
1325                         return -XFS_ERROR(EFAULT);
1326                 return 0;
1327         }
1328
1329         case XFS_IOC_FSBULKSTAT_SINGLE:
1330         case XFS_IOC_FSBULKSTAT:
1331         case XFS_IOC_FSINUMBERS:
1332                 return xfs_ioc_bulkstat(mp, cmd, arg);
1333
1334         case XFS_IOC_FSGEOMETRY_V1:
1335                 return xfs_ioc_fsgeometry_v1(mp, arg);
1336
1337         case XFS_IOC_FSGEOMETRY:
1338                 return xfs_ioc_fsgeometry(mp, arg);
1339
1340         case XFS_IOC_GETVERSION:
1341                 return put_user(inode->i_generation, (int __user *)arg);
1342
1343         case XFS_IOC_FSGETXATTR:
1344                 return xfs_ioc_fsgetxattr(ip, 0, arg);
1345         case XFS_IOC_FSGETXATTRA:
1346                 return xfs_ioc_fsgetxattr(ip, 1, arg);
1347         case XFS_IOC_FSSETXATTR:
1348                 return xfs_ioc_fssetxattr(ip, filp, arg);
1349         case XFS_IOC_GETXFLAGS:
1350                 return xfs_ioc_getxflags(ip, arg);
1351         case XFS_IOC_SETXFLAGS:
1352                 return xfs_ioc_setxflags(ip, filp, arg);
1353
1354         case XFS_IOC_FSSETDM: {
1355                 struct fsdmidata        dmi;
1356
1357                 if (copy_from_user(&dmi, arg, sizeof(dmi)))
1358                         return -XFS_ERROR(EFAULT);
1359
1360                 error = xfs_set_dmattrs(ip, dmi.fsd_dmevmask,
1361                                 dmi.fsd_dmstate);
1362                 return -error;
1363         }
1364
1365         case XFS_IOC_GETBMAP:
1366         case XFS_IOC_GETBMAPA:
1367                 return xfs_ioc_getbmap(ip, ioflags, cmd, arg);
1368
1369         case XFS_IOC_GETBMAPX:
1370                 return xfs_ioc_getbmapx(ip, arg);
1371
1372         case XFS_IOC_FD_TO_HANDLE:
1373         case XFS_IOC_PATH_TO_HANDLE:
1374         case XFS_IOC_PATH_TO_FSHANDLE: {
1375                 xfs_fsop_handlereq_t    hreq;
1376
1377                 if (copy_from_user(&hreq, arg, sizeof(hreq)))
1378                         return -XFS_ERROR(EFAULT);
1379                 return xfs_find_handle(cmd, &hreq);
1380         }
1381         case XFS_IOC_OPEN_BY_HANDLE: {
1382                 xfs_fsop_handlereq_t    hreq;
1383
1384                 if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
1385                         return -XFS_ERROR(EFAULT);
1386                 return xfs_open_by_handle(filp, &hreq);
1387         }
1388         case XFS_IOC_FSSETDM_BY_HANDLE:
1389                 return xfs_fssetdm_by_handle(filp, arg);
1390
1391         case XFS_IOC_READLINK_BY_HANDLE: {
1392                 xfs_fsop_handlereq_t    hreq;
1393
1394                 if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
1395                         return -XFS_ERROR(EFAULT);
1396                 return xfs_readlink_by_handle(filp, &hreq);
1397         }
1398         case XFS_IOC_ATTRLIST_BY_HANDLE:
1399                 return xfs_attrlist_by_handle(filp, arg);
1400
1401         case XFS_IOC_ATTRMULTI_BY_HANDLE:
1402                 return xfs_attrmulti_by_handle(filp, arg);
1403
1404         case XFS_IOC_SWAPEXT: {
1405                 struct xfs_swapext      sxp;
1406
1407                 if (copy_from_user(&sxp, arg, sizeof(xfs_swapext_t)))
1408                         return -XFS_ERROR(EFAULT);
1409                 error = xfs_swapext(&sxp);
1410                 return -error;
1411         }
1412
1413         case XFS_IOC_FSCOUNTS: {
1414                 xfs_fsop_counts_t out;
1415
1416                 error = xfs_fs_counts(mp, &out);
1417                 if (error)
1418                         return -error;
1419
1420                 if (copy_to_user(arg, &out, sizeof(out)))
1421                         return -XFS_ERROR(EFAULT);
1422                 return 0;
1423         }
1424
1425         case XFS_IOC_SET_RESBLKS: {
1426                 xfs_fsop_resblks_t inout;
1427                 __uint64_t         in;
1428
1429                 if (!capable(CAP_SYS_ADMIN))
1430                         return -EPERM;
1431
1432                 if (copy_from_user(&inout, arg, sizeof(inout)))
1433                         return -XFS_ERROR(EFAULT);
1434
1435                 /* input parameter is passed in resblks field of structure */
1436                 in = inout.resblks;
1437                 error = xfs_reserve_blocks(mp, &in, &inout);
1438                 if (error)
1439                         return -error;
1440
1441                 if (copy_to_user(arg, &inout, sizeof(inout)))
1442                         return -XFS_ERROR(EFAULT);
1443                 return 0;
1444         }
1445
1446         case XFS_IOC_GET_RESBLKS: {
1447                 xfs_fsop_resblks_t out;
1448
1449                 if (!capable(CAP_SYS_ADMIN))
1450                         return -EPERM;
1451
1452                 error = xfs_reserve_blocks(mp, NULL, &out);
1453                 if (error)
1454                         return -error;
1455
1456                 if (copy_to_user(arg, &out, sizeof(out)))
1457                         return -XFS_ERROR(EFAULT);
1458
1459                 return 0;
1460         }
1461
1462         case XFS_IOC_FSGROWFSDATA: {
1463                 xfs_growfs_data_t in;
1464
1465                 if (copy_from_user(&in, arg, sizeof(in)))
1466                         return -XFS_ERROR(EFAULT);
1467
1468                 error = xfs_growfs_data(mp, &in);
1469                 return -error;
1470         }
1471
1472         case XFS_IOC_FSGROWFSLOG: {
1473                 xfs_growfs_log_t in;
1474
1475                 if (copy_from_user(&in, arg, sizeof(in)))
1476                         return -XFS_ERROR(EFAULT);
1477
1478                 error = xfs_growfs_log(mp, &in);
1479                 return -error;
1480         }
1481
1482         case XFS_IOC_FSGROWFSRT: {
1483                 xfs_growfs_rt_t in;
1484
1485                 if (copy_from_user(&in, arg, sizeof(in)))
1486                         return -XFS_ERROR(EFAULT);
1487
1488                 error = xfs_growfs_rt(mp, &in);
1489                 return -error;
1490         }
1491
1492         case XFS_IOC_GOINGDOWN: {
1493                 __uint32_t in;
1494
1495                 if (!capable(CAP_SYS_ADMIN))
1496                         return -EPERM;
1497
1498                 if (get_user(in, (__uint32_t __user *)arg))
1499                         return -XFS_ERROR(EFAULT);
1500
1501                 error = xfs_fs_goingdown(mp, in);
1502                 return -error;
1503         }
1504
1505         case XFS_IOC_ERROR_INJECTION: {
1506                 xfs_error_injection_t in;
1507
1508                 if (!capable(CAP_SYS_ADMIN))
1509                         return -EPERM;
1510
1511                 if (copy_from_user(&in, arg, sizeof(in)))
1512                         return -XFS_ERROR(EFAULT);
1513
1514                 error = xfs_errortag_add(in.errtag, mp);
1515                 return -error;
1516         }
1517
1518         case XFS_IOC_ERROR_CLEARALL:
1519                 if (!capable(CAP_SYS_ADMIN))
1520                         return -EPERM;
1521
1522                 error = xfs_errortag_clearall(mp, 1);
1523                 return -error;
1524
1525         default:
1526                 return -ENOTTY;
1527         }
1528 }