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