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