Merge branch 'for-linus' of git://git390.osdl.marist.edu/pub/scm/linux-2.6
[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_cap.h"
45 #include "xfs_mac.h"
46 #include "xfs_attr.h"
47 #include "xfs_bmap.h"
48 #include "xfs_buf_item.h"
49 #include "xfs_utils.h"
50 #include "xfs_dfrag.h"
51 #include "xfs_fsops.h"
52
53 #include <linux/capability.h>
54 #include <linux/dcache.h>
55 #include <linux/mount.h>
56 #include <linux/namei.h>
57 #include <linux/pagemap.h>
58
59 /*
60  * xfs_find_handle maps from userspace xfs_fsop_handlereq structure to
61  * a file or fs handle.
62  *
63  * XFS_IOC_PATH_TO_FSHANDLE
64  *    returns fs handle for a mount point or path within that mount point
65  * XFS_IOC_FD_TO_HANDLE
66  *    returns full handle for a FD opened in user space
67  * XFS_IOC_PATH_TO_HANDLE
68  *    returns full handle for a path
69  */
70 STATIC int
71 xfs_find_handle(
72         unsigned int            cmd,
73         void                    __user *arg)
74 {
75         int                     hsize;
76         xfs_handle_t            handle;
77         xfs_fsop_handlereq_t    hreq;
78         struct inode            *inode;
79         bhv_vnode_t             *vp;
80
81         if (copy_from_user(&hreq, arg, sizeof(hreq)))
82                 return -XFS_ERROR(EFAULT);
83
84         memset((char *)&handle, 0, sizeof(handle));
85
86         switch (cmd) {
87         case XFS_IOC_PATH_TO_FSHANDLE:
88         case XFS_IOC_PATH_TO_HANDLE: {
89                 struct nameidata        nd;
90                 int                     error;
91
92                 error = user_path_walk_link((const char __user *)hreq.path, &nd);
93                 if (error)
94                         return error;
95
96                 ASSERT(nd.dentry);
97                 ASSERT(nd.dentry->d_inode);
98                 inode = igrab(nd.dentry->d_inode);
99                 path_release(&nd);
100                 break;
101         }
102
103         case XFS_IOC_FD_TO_HANDLE: {
104                 struct file     *file;
105
106                 file = fget(hreq.fd);
107                 if (!file)
108                     return -EBADF;
109
110                 ASSERT(file->f_path.dentry);
111                 ASSERT(file->f_path.dentry->d_inode);
112                 inode = igrab(file->f_path.dentry->d_inode);
113                 fput(file);
114                 break;
115         }
116
117         default:
118                 ASSERT(0);
119                 return -XFS_ERROR(EINVAL);
120         }
121
122         if (inode->i_sb->s_magic != XFS_SB_MAGIC) {
123                 /* we're not in XFS anymore, Toto */
124                 iput(inode);
125                 return -XFS_ERROR(EINVAL);
126         }
127
128         switch (inode->i_mode & S_IFMT) {
129         case S_IFREG:
130         case S_IFDIR:
131         case S_IFLNK:
132                 break;
133         default:
134                 iput(inode);
135                 return -XFS_ERROR(EBADF);
136         }
137
138         /* we need the vnode */
139         vp = vn_from_inode(inode);
140
141         /* now we can grab the fsid */
142         memcpy(&handle.ha_fsid, vp->v_vfsp->vfs_altfsid, sizeof(xfs_fsid_t));
143         hsize = sizeof(xfs_fsid_t);
144
145         if (cmd != XFS_IOC_PATH_TO_FSHANDLE) {
146                 xfs_inode_t     *ip;
147                 int             lock_mode;
148
149                 /* need to get access to the xfs_inode to read the generation */
150                 ip = xfs_vtoi(vp);
151                 ASSERT(ip);
152                 lock_mode = xfs_ilock_map_shared(ip);
153
154                 /* fill in fid section of handle from inode */
155                 handle.ha_fid.xfs_fid_len = sizeof(xfs_fid_t) -
156                                             sizeof(handle.ha_fid.xfs_fid_len);
157                 handle.ha_fid.xfs_fid_pad = 0;
158                 handle.ha_fid.xfs_fid_gen = ip->i_d.di_gen;
159                 handle.ha_fid.xfs_fid_ino = ip->i_ino;
160
161                 xfs_iunlock_map_shared(ip, lock_mode);
162
163                 hsize = XFS_HSIZE(handle);
164         }
165
166         /* now copy our handle into the user buffer & write out the size */
167         if (copy_to_user(hreq.ohandle, &handle, hsize) ||
168             copy_to_user(hreq.ohandlen, &hsize, sizeof(__s32))) {
169                 iput(inode);
170                 return -XFS_ERROR(EFAULT);
171         }
172
173         iput(inode);
174         return 0;
175 }
176
177
178 /*
179  * Convert userspace handle data into vnode (and inode).
180  * We [ab]use the fact that all the fsop_handlereq ioctl calls
181  * have a data structure argument whose first component is always
182  * a xfs_fsop_handlereq_t, so we can cast to and from this type.
183  * This allows us to optimise the copy_from_user calls and gives
184  * a handy, shared routine.
185  *
186  * If no error, caller must always VN_RELE the returned vp.
187  */
188 STATIC int
189 xfs_vget_fsop_handlereq(
190         xfs_mount_t             *mp,
191         struct inode            *parinode,      /* parent inode pointer    */
192         xfs_fsop_handlereq_t    *hreq,
193         bhv_vnode_t             **vp,
194         struct inode            **inode)
195 {
196         void                    __user *hanp;
197         size_t                  hlen;
198         xfs_fid_t               *xfid;
199         xfs_handle_t            *handlep;
200         xfs_handle_t            handle;
201         xfs_inode_t             *ip;
202         struct inode            *inodep;
203         bhv_vnode_t             *vpp;
204         xfs_ino_t               ino;
205         __u32                   igen;
206         int                     error;
207
208         /*
209          * Only allow handle opens under a directory.
210          */
211         if (!S_ISDIR(parinode->i_mode))
212                 return XFS_ERROR(ENOTDIR);
213
214         hanp = hreq->ihandle;
215         hlen = hreq->ihandlen;
216         handlep = &handle;
217
218         if (hlen < sizeof(handlep->ha_fsid) || hlen > sizeof(*handlep))
219                 return XFS_ERROR(EINVAL);
220         if (copy_from_user(handlep, hanp, hlen))
221                 return XFS_ERROR(EFAULT);
222         if (hlen < sizeof(*handlep))
223                 memset(((char *)handlep) + hlen, 0, sizeof(*handlep) - hlen);
224         if (hlen > sizeof(handlep->ha_fsid)) {
225                 if (handlep->ha_fid.xfs_fid_len !=
226                                 (hlen - sizeof(handlep->ha_fsid)
227                                         - sizeof(handlep->ha_fid.xfs_fid_len))
228                     || handlep->ha_fid.xfs_fid_pad)
229                         return XFS_ERROR(EINVAL);
230         }
231
232         /*
233          * Crack the handle, obtain the inode # & generation #
234          */
235         xfid = (struct xfs_fid *)&handlep->ha_fid;
236         if (xfid->xfs_fid_len == sizeof(*xfid) - sizeof(xfid->xfs_fid_len)) {
237                 ino  = xfid->xfs_fid_ino;
238                 igen = xfid->xfs_fid_gen;
239         } else {
240                 return XFS_ERROR(EINVAL);
241         }
242
243         /*
244          * Get the XFS inode, building a vnode to go with it.
245          */
246         error = xfs_iget(mp, NULL, ino, 0, XFS_ILOCK_SHARED, &ip, 0);
247         if (error)
248                 return error;
249         if (ip == NULL)
250                 return XFS_ERROR(EIO);
251         if (ip->i_d.di_mode == 0 || ip->i_d.di_gen != igen) {
252                 xfs_iput_new(ip, XFS_ILOCK_SHARED);
253                 return XFS_ERROR(ENOENT);
254         }
255
256         vpp = XFS_ITOV(ip);
257         inodep = vn_to_inode(vpp);
258         xfs_iunlock(ip, XFS_ILOCK_SHARED);
259
260         *vp = vpp;
261         *inode = inodep;
262         return 0;
263 }
264
265 STATIC int
266 xfs_open_by_handle(
267         xfs_mount_t             *mp,
268         void                    __user *arg,
269         struct file             *parfilp,
270         struct inode            *parinode)
271 {
272         int                     error;
273         int                     new_fd;
274         int                     permflag;
275         struct file             *filp;
276         struct inode            *inode;
277         struct dentry           *dentry;
278         bhv_vnode_t             *vp;
279         xfs_fsop_handlereq_t    hreq;
280
281         if (!capable(CAP_SYS_ADMIN))
282                 return -XFS_ERROR(EPERM);
283         if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
284                 return -XFS_ERROR(EFAULT);
285
286         error = xfs_vget_fsop_handlereq(mp, parinode, &hreq, &vp, &inode);
287         if (error)
288                 return -error;
289
290         /* Restrict xfs_open_by_handle to directories & regular files. */
291         if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode))) {
292                 iput(inode);
293                 return -XFS_ERROR(EINVAL);
294         }
295
296 #if BITS_PER_LONG != 32
297         hreq.oflags |= O_LARGEFILE;
298 #endif
299         /* Put open permission in namei format. */
300         permflag = hreq.oflags;
301         if ((permflag+1) & O_ACCMODE)
302                 permflag++;
303         if (permflag & O_TRUNC)
304                 permflag |= 2;
305
306         if ((!(permflag & O_APPEND) || (permflag & O_TRUNC)) &&
307             (permflag & FMODE_WRITE) && IS_APPEND(inode)) {
308                 iput(inode);
309                 return -XFS_ERROR(EPERM);
310         }
311
312         if ((permflag & FMODE_WRITE) && IS_IMMUTABLE(inode)) {
313                 iput(inode);
314                 return -XFS_ERROR(EACCES);
315         }
316
317         /* Can't write directories. */
318         if ( S_ISDIR(inode->i_mode) && (permflag & FMODE_WRITE)) {
319                 iput(inode);
320                 return -XFS_ERROR(EISDIR);
321         }
322
323         if ((new_fd = get_unused_fd()) < 0) {
324                 iput(inode);
325                 return new_fd;
326         }
327
328         dentry = d_alloc_anon(inode);
329         if (dentry == NULL) {
330                 iput(inode);
331                 put_unused_fd(new_fd);
332                 return -XFS_ERROR(ENOMEM);
333         }
334
335         /* Ensure umount returns EBUSY on umounts while this file is open. */
336         mntget(parfilp->f_path.mnt);
337
338         /* Create file pointer. */
339         filp = dentry_open(dentry, parfilp->f_path.mnt, hreq.oflags);
340         if (IS_ERR(filp)) {
341                 put_unused_fd(new_fd);
342                 return -XFS_ERROR(-PTR_ERR(filp));
343         }
344         if (inode->i_mode & S_IFREG) {
345                 /* invisible operation should not change atime */
346                 filp->f_flags |= O_NOATIME;
347                 filp->f_op = &xfs_invis_file_operations;
348         }
349
350         fd_install(new_fd, filp);
351         return new_fd;
352 }
353
354 STATIC int
355 xfs_readlink_by_handle(
356         xfs_mount_t             *mp,
357         void                    __user *arg,
358         struct file             *parfilp,
359         struct inode            *parinode)
360 {
361         int                     error;
362         struct iovec            aiov;
363         struct uio              auio;
364         struct inode            *inode;
365         xfs_fsop_handlereq_t    hreq;
366         bhv_vnode_t             *vp;
367         __u32                   olen;
368
369         if (!capable(CAP_SYS_ADMIN))
370                 return -XFS_ERROR(EPERM);
371         if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
372                 return -XFS_ERROR(EFAULT);
373
374         error = xfs_vget_fsop_handlereq(mp, parinode, &hreq, &vp, &inode);
375         if (error)
376                 return -error;
377
378         /* Restrict this handle operation to symlinks only. */
379         if (!S_ISLNK(inode->i_mode)) {
380                 VN_RELE(vp);
381                 return -XFS_ERROR(EINVAL);
382         }
383
384         if (copy_from_user(&olen, hreq.ohandlen, sizeof(__u32))) {
385                 VN_RELE(vp);
386                 return -XFS_ERROR(EFAULT);
387         }
388         aiov.iov_len    = olen;
389         aiov.iov_base   = hreq.ohandle;
390
391         auio.uio_iov    = &aiov;
392         auio.uio_iovcnt = 1;
393         auio.uio_offset = 0;
394         auio.uio_segflg = UIO_USERSPACE;
395         auio.uio_resid  = olen;
396
397         error = bhv_vop_readlink(vp, &auio, IO_INVIS, NULL);
398         VN_RELE(vp);
399         if (error)
400                 return -error;
401
402         return (olen - auio.uio_resid);
403 }
404
405 STATIC int
406 xfs_fssetdm_by_handle(
407         xfs_mount_t             *mp,
408         void                    __user *arg,
409         struct file             *parfilp,
410         struct inode            *parinode)
411 {
412         int                     error;
413         struct fsdmidata        fsd;
414         xfs_fsop_setdm_handlereq_t dmhreq;
415         struct inode            *inode;
416         bhv_desc_t              *bdp;
417         bhv_vnode_t             *vp;
418
419         if (!capable(CAP_MKNOD))
420                 return -XFS_ERROR(EPERM);
421         if (copy_from_user(&dmhreq, arg, sizeof(xfs_fsop_setdm_handlereq_t)))
422                 return -XFS_ERROR(EFAULT);
423
424         error = xfs_vget_fsop_handlereq(mp, parinode, &dmhreq.hreq, &vp, &inode);
425         if (error)
426                 return -error;
427
428         if (IS_IMMUTABLE(inode) || IS_APPEND(inode)) {
429                 VN_RELE(vp);
430                 return -XFS_ERROR(EPERM);
431         }
432
433         if (copy_from_user(&fsd, dmhreq.data, sizeof(fsd))) {
434                 VN_RELE(vp);
435                 return -XFS_ERROR(EFAULT);
436         }
437
438         bdp = bhv_base_unlocked(VN_BHV_HEAD(vp));
439         error = xfs_set_dmattrs(bdp, fsd.fsd_dmevmask, fsd.fsd_dmstate, NULL);
440
441         VN_RELE(vp);
442         if (error)
443                 return -error;
444         return 0;
445 }
446
447 STATIC int
448 xfs_attrlist_by_handle(
449         xfs_mount_t             *mp,
450         void                    __user *arg,
451         struct file             *parfilp,
452         struct inode            *parinode)
453 {
454         int                     error;
455         attrlist_cursor_kern_t  *cursor;
456         xfs_fsop_attrlist_handlereq_t al_hreq;
457         struct inode            *inode;
458         bhv_vnode_t             *vp;
459         char                    *kbuf;
460
461         if (!capable(CAP_SYS_ADMIN))
462                 return -XFS_ERROR(EPERM);
463         if (copy_from_user(&al_hreq, arg, sizeof(xfs_fsop_attrlist_handlereq_t)))
464                 return -XFS_ERROR(EFAULT);
465         if (al_hreq.buflen > XATTR_LIST_MAX)
466                 return -XFS_ERROR(EINVAL);
467
468         error = xfs_vget_fsop_handlereq(mp, parinode, &al_hreq.hreq,
469                         &vp, &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 = bhv_vop_attr_list(vp, kbuf, al_hreq.buflen, al_hreq.flags,
479                                         cursor, NULL);
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         VN_RELE(vp);
490  out:
491         return -error;
492 }
493
494 STATIC int
495 xfs_attrmulti_attr_get(
496         bhv_vnode_t             *vp,
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 = bhv_vop_attr_get(vp, name, kbuf, len, flags, NULL);
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 STATIC int
524 xfs_attrmulti_attr_set(
525         bhv_vnode_t             *vp,
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_RDONLY(&vp->v_inode))
535                 return -EROFS;
536         if (IS_IMMUTABLE(&vp->v_inode) || IS_APPEND(&vp->v_inode))
537                 return EPERM;
538         if (len > XATTR_SIZE_MAX)
539                 return EINVAL;
540
541         kbuf = kmalloc(len, GFP_KERNEL);
542         if (!kbuf)
543                 return ENOMEM;
544
545         if (copy_from_user(kbuf, ubuf, len))
546                 goto out_kfree;
547                         
548         error = bhv_vop_attr_set(vp, name, kbuf, len, flags, NULL);
549
550  out_kfree:
551         kfree(kbuf);
552         return error;
553 }
554
555 STATIC int
556 xfs_attrmulti_attr_remove(
557         bhv_vnode_t             *vp,
558         char                    *name,
559         __uint32_t              flags)
560 {
561         if (IS_RDONLY(&vp->v_inode))
562                 return -EROFS;
563         if (IS_IMMUTABLE(&vp->v_inode) || IS_APPEND(&vp->v_inode))
564                 return EPERM;
565         return bhv_vop_attr_remove(vp, name, flags, NULL);
566 }
567
568 STATIC int
569 xfs_attrmulti_by_handle(
570         xfs_mount_t             *mp,
571         void                    __user *arg,
572         struct file             *parfilp,
573         struct inode            *parinode)
574 {
575         int                     error;
576         xfs_attr_multiop_t      *ops;
577         xfs_fsop_attrmulti_handlereq_t am_hreq;
578         struct inode            *inode;
579         bhv_vnode_t             *vp;
580         unsigned int            i, size;
581         char                    *attr_name;
582
583         if (!capable(CAP_SYS_ADMIN))
584                 return -XFS_ERROR(EPERM);
585         if (copy_from_user(&am_hreq, arg, sizeof(xfs_fsop_attrmulti_handlereq_t)))
586                 return -XFS_ERROR(EFAULT);
587
588         error = xfs_vget_fsop_handlereq(mp, parinode, &am_hreq.hreq, &vp, &inode);
589         if (error)
590                 goto out;
591
592         error = E2BIG;
593         size = am_hreq.opcount * sizeof(attr_multiop_t);
594         if (!size || size > 16 * PAGE_SIZE)
595                 goto out_vn_rele;
596
597         error = ENOMEM;
598         ops = kmalloc(size, GFP_KERNEL);
599         if (!ops)
600                 goto out_vn_rele;
601
602         error = EFAULT;
603         if (copy_from_user(ops, am_hreq.ops, size))
604                 goto out_kfree_ops;
605
606         attr_name = kmalloc(MAXNAMELEN, GFP_KERNEL);
607         if (!attr_name)
608                 goto out_kfree_ops;
609
610
611         error = 0;
612         for (i = 0; i < am_hreq.opcount; i++) {
613                 ops[i].am_error = strncpy_from_user(attr_name,
614                                 ops[i].am_attrname, MAXNAMELEN);
615                 if (ops[i].am_error == 0 || ops[i].am_error == MAXNAMELEN)
616                         error = -ERANGE;
617                 if (ops[i].am_error < 0)
618                         break;
619
620                 switch (ops[i].am_opcode) {
621                 case ATTR_OP_GET:
622                         ops[i].am_error = xfs_attrmulti_attr_get(vp,
623                                         attr_name, ops[i].am_attrvalue,
624                                         &ops[i].am_length, ops[i].am_flags);
625                         break;
626                 case ATTR_OP_SET:
627                         ops[i].am_error = xfs_attrmulti_attr_set(vp,
628                                         attr_name, ops[i].am_attrvalue,
629                                         ops[i].am_length, ops[i].am_flags);
630                         break;
631                 case ATTR_OP_REMOVE:
632                         ops[i].am_error = xfs_attrmulti_attr_remove(vp,
633                                         attr_name, ops[i].am_flags);
634                         break;
635                 default:
636                         ops[i].am_error = EINVAL;
637                 }
638         }
639
640         if (copy_to_user(am_hreq.ops, ops, size))
641                 error = XFS_ERROR(EFAULT);
642
643         kfree(attr_name);
644  out_kfree_ops:
645         kfree(ops);
646  out_vn_rele:
647         VN_RELE(vp);
648  out:
649         return -error;
650 }
651
652 /* prototypes for a few of the stack-hungry cases that have
653  * their own functions.  Functions are defined after their use
654  * so gcc doesn't get fancy and inline them with -03 */
655
656 STATIC int
657 xfs_ioc_space(
658         bhv_desc_t              *bdp,
659         struct inode            *inode,
660         struct file             *filp,
661         int                     flags,
662         unsigned int            cmd,
663         void                    __user *arg);
664
665 STATIC int
666 xfs_ioc_bulkstat(
667         xfs_mount_t             *mp,
668         unsigned int            cmd,
669         void                    __user *arg);
670
671 STATIC int
672 xfs_ioc_fsgeometry_v1(
673         xfs_mount_t             *mp,
674         void                    __user *arg);
675
676 STATIC int
677 xfs_ioc_fsgeometry(
678         xfs_mount_t             *mp,
679         void                    __user *arg);
680
681 STATIC int
682 xfs_ioc_xattr(
683         bhv_vnode_t             *vp,
684         xfs_inode_t             *ip,
685         struct file             *filp,
686         unsigned int            cmd,
687         void                    __user *arg);
688
689 STATIC int
690 xfs_ioc_getbmap(
691         bhv_desc_t              *bdp,
692         struct file             *filp,
693         int                     flags,
694         unsigned int            cmd,
695         void                    __user *arg);
696
697 STATIC int
698 xfs_ioc_getbmapx(
699         bhv_desc_t              *bdp,
700         void                    __user *arg);
701
702 int
703 xfs_ioctl(
704         bhv_desc_t              *bdp,
705         struct inode            *inode,
706         struct file             *filp,
707         int                     ioflags,
708         unsigned int            cmd,
709         void                    __user *arg)
710 {
711         int                     error;
712         bhv_vnode_t             *vp;
713         xfs_inode_t             *ip;
714         xfs_mount_t             *mp;
715
716         vp = vn_from_inode(inode);
717
718         vn_trace_entry(vp, "xfs_ioctl", (inst_t *)__return_address);
719
720         ip = XFS_BHVTOI(bdp);
721         mp = ip->i_mount;
722
723         switch (cmd) {
724
725         case XFS_IOC_ALLOCSP:
726         case XFS_IOC_FREESP:
727         case XFS_IOC_RESVSP:
728         case XFS_IOC_UNRESVSP:
729         case XFS_IOC_ALLOCSP64:
730         case XFS_IOC_FREESP64:
731         case XFS_IOC_RESVSP64:
732         case XFS_IOC_UNRESVSP64:
733                 /*
734                  * Only allow the sys admin to reserve space unless
735                  * unwritten extents are enabled.
736                  */
737                 if (!XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb) &&
738                     !capable(CAP_SYS_ADMIN))
739                         return -EPERM;
740
741                 return xfs_ioc_space(bdp, inode, filp, ioflags, cmd, arg);
742
743         case XFS_IOC_DIOINFO: {
744                 struct dioattr  da;
745                 xfs_buftarg_t   *target =
746                         (ip->i_d.di_flags & XFS_DIFLAG_REALTIME) ?
747                         mp->m_rtdev_targp : mp->m_ddev_targp;
748
749                 da.d_mem = da.d_miniosz = 1 << target->bt_sshift;
750                 da.d_maxiosz = INT_MAX & ~(da.d_miniosz - 1);
751
752                 if (copy_to_user(arg, &da, sizeof(da)))
753                         return -XFS_ERROR(EFAULT);
754                 return 0;
755         }
756
757         case XFS_IOC_FSBULKSTAT_SINGLE:
758         case XFS_IOC_FSBULKSTAT:
759         case XFS_IOC_FSINUMBERS:
760                 return xfs_ioc_bulkstat(mp, cmd, arg);
761
762         case XFS_IOC_FSGEOMETRY_V1:
763                 return xfs_ioc_fsgeometry_v1(mp, arg);
764
765         case XFS_IOC_FSGEOMETRY:
766                 return xfs_ioc_fsgeometry(mp, arg);
767
768         case XFS_IOC_GETVERSION:
769                 return put_user(inode->i_generation, (int __user *)arg);
770
771         case XFS_IOC_GETXFLAGS:
772         case XFS_IOC_SETXFLAGS:
773         case XFS_IOC_FSGETXATTR:
774         case XFS_IOC_FSSETXATTR:
775         case XFS_IOC_FSGETXATTRA:
776                 return xfs_ioc_xattr(vp, ip, filp, cmd, arg);
777
778         case XFS_IOC_FSSETDM: {
779                 struct fsdmidata        dmi;
780
781                 if (copy_from_user(&dmi, arg, sizeof(dmi)))
782                         return -XFS_ERROR(EFAULT);
783
784                 error = xfs_set_dmattrs(bdp, dmi.fsd_dmevmask, dmi.fsd_dmstate,
785                                                         NULL);
786                 return -error;
787         }
788
789         case XFS_IOC_GETBMAP:
790         case XFS_IOC_GETBMAPA:
791                 return xfs_ioc_getbmap(bdp, filp, ioflags, cmd, arg);
792
793         case XFS_IOC_GETBMAPX:
794                 return xfs_ioc_getbmapx(bdp, arg);
795
796         case XFS_IOC_FD_TO_HANDLE:
797         case XFS_IOC_PATH_TO_HANDLE:
798         case XFS_IOC_PATH_TO_FSHANDLE:
799                 return xfs_find_handle(cmd, arg);
800
801         case XFS_IOC_OPEN_BY_HANDLE:
802                 return xfs_open_by_handle(mp, arg, filp, inode);
803
804         case XFS_IOC_FSSETDM_BY_HANDLE:
805                 return xfs_fssetdm_by_handle(mp, arg, filp, inode);
806
807         case XFS_IOC_READLINK_BY_HANDLE:
808                 return xfs_readlink_by_handle(mp, arg, filp, inode);
809
810         case XFS_IOC_ATTRLIST_BY_HANDLE:
811                 return xfs_attrlist_by_handle(mp, arg, filp, inode);
812
813         case XFS_IOC_ATTRMULTI_BY_HANDLE:
814                 return xfs_attrmulti_by_handle(mp, arg, filp, inode);
815
816         case XFS_IOC_SWAPEXT: {
817                 error = xfs_swapext((struct xfs_swapext __user *)arg);
818                 return -error;
819         }
820
821         case XFS_IOC_FSCOUNTS: {
822                 xfs_fsop_counts_t out;
823
824                 error = xfs_fs_counts(mp, &out);
825                 if (error)
826                         return -error;
827
828                 if (copy_to_user(arg, &out, sizeof(out)))
829                         return -XFS_ERROR(EFAULT);
830                 return 0;
831         }
832
833         case XFS_IOC_SET_RESBLKS: {
834                 xfs_fsop_resblks_t inout;
835                 __uint64_t         in;
836
837                 if (!capable(CAP_SYS_ADMIN))
838                         return -EPERM;
839
840                 if (copy_from_user(&inout, arg, sizeof(inout)))
841                         return -XFS_ERROR(EFAULT);
842
843                 /* input parameter is passed in resblks field of structure */
844                 in = inout.resblks;
845                 error = xfs_reserve_blocks(mp, &in, &inout);
846                 if (error)
847                         return -error;
848
849                 if (copy_to_user(arg, &inout, sizeof(inout)))
850                         return -XFS_ERROR(EFAULT);
851                 return 0;
852         }
853
854         case XFS_IOC_GET_RESBLKS: {
855                 xfs_fsop_resblks_t out;
856
857                 if (!capable(CAP_SYS_ADMIN))
858                         return -EPERM;
859
860                 error = xfs_reserve_blocks(mp, NULL, &out);
861                 if (error)
862                         return -error;
863
864                 if (copy_to_user(arg, &out, sizeof(out)))
865                         return -XFS_ERROR(EFAULT);
866
867                 return 0;
868         }
869
870         case XFS_IOC_FSGROWFSDATA: {
871                 xfs_growfs_data_t in;
872
873                 if (!capable(CAP_SYS_ADMIN))
874                         return -EPERM;
875
876                 if (copy_from_user(&in, arg, sizeof(in)))
877                         return -XFS_ERROR(EFAULT);
878
879                 error = xfs_growfs_data(mp, &in);
880                 return -error;
881         }
882
883         case XFS_IOC_FSGROWFSLOG: {
884                 xfs_growfs_log_t in;
885
886                 if (!capable(CAP_SYS_ADMIN))
887                         return -EPERM;
888
889                 if (copy_from_user(&in, arg, sizeof(in)))
890                         return -XFS_ERROR(EFAULT);
891
892                 error = xfs_growfs_log(mp, &in);
893                 return -error;
894         }
895
896         case XFS_IOC_FSGROWFSRT: {
897                 xfs_growfs_rt_t in;
898
899                 if (!capable(CAP_SYS_ADMIN))
900                         return -EPERM;
901
902                 if (copy_from_user(&in, arg, sizeof(in)))
903                         return -XFS_ERROR(EFAULT);
904
905                 error = xfs_growfs_rt(mp, &in);
906                 return -error;
907         }
908
909         case XFS_IOC_FREEZE:
910                 if (!capable(CAP_SYS_ADMIN))
911                         return -EPERM;
912
913                 if (inode->i_sb->s_frozen == SB_UNFROZEN)
914                         freeze_bdev(inode->i_sb->s_bdev);
915                 return 0;
916
917         case XFS_IOC_THAW:
918                 if (!capable(CAP_SYS_ADMIN))
919                         return -EPERM;
920                 if (inode->i_sb->s_frozen != SB_UNFROZEN)
921                         thaw_bdev(inode->i_sb->s_bdev, inode->i_sb);
922                 return 0;
923
924         case XFS_IOC_GOINGDOWN: {
925                 __uint32_t in;
926
927                 if (!capable(CAP_SYS_ADMIN))
928                         return -EPERM;
929
930                 if (get_user(in, (__uint32_t __user *)arg))
931                         return -XFS_ERROR(EFAULT);
932
933                 error = xfs_fs_goingdown(mp, in);
934                 return -error;
935         }
936
937         case XFS_IOC_ERROR_INJECTION: {
938                 xfs_error_injection_t in;
939
940                 if (!capable(CAP_SYS_ADMIN))
941                         return -EPERM;
942
943                 if (copy_from_user(&in, arg, sizeof(in)))
944                         return -XFS_ERROR(EFAULT);
945
946                 error = xfs_errortag_add(in.errtag, mp);
947                 return -error;
948         }
949
950         case XFS_IOC_ERROR_CLEARALL:
951                 if (!capable(CAP_SYS_ADMIN))
952                         return -EPERM;
953
954                 error = xfs_errortag_clearall(mp);
955                 return -error;
956
957         default:
958                 return -ENOTTY;
959         }
960 }
961
962 STATIC int
963 xfs_ioc_space(
964         bhv_desc_t              *bdp,
965         struct inode            *inode,
966         struct file             *filp,
967         int                     ioflags,
968         unsigned int            cmd,
969         void                    __user *arg)
970 {
971         xfs_flock64_t           bf;
972         int                     attr_flags = 0;
973         int                     error;
974
975         if (inode->i_flags & (S_IMMUTABLE|S_APPEND))
976                 return -XFS_ERROR(EPERM);
977
978         if (!(filp->f_mode & FMODE_WRITE))
979                 return -XFS_ERROR(EBADF);
980
981         if (!S_ISREG(inode->i_mode))
982                 return -XFS_ERROR(EINVAL);
983
984         if (copy_from_user(&bf, arg, sizeof(bf)))
985                 return -XFS_ERROR(EFAULT);
986
987         if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
988                 attr_flags |= ATTR_NONBLOCK;
989         if (ioflags & IO_INVIS)
990                 attr_flags |= ATTR_DMI;
991
992         error = xfs_change_file_space(bdp, cmd, &bf, filp->f_pos,
993                                               NULL, attr_flags);
994         return -error;
995 }
996
997 STATIC int
998 xfs_ioc_bulkstat(
999         xfs_mount_t             *mp,
1000         unsigned int            cmd,
1001         void                    __user *arg)
1002 {
1003         xfs_fsop_bulkreq_t      bulkreq;
1004         int                     count;  /* # of records returned */
1005         xfs_ino_t               inlast; /* last inode number */
1006         int                     done;
1007         int                     error;
1008
1009         /* done = 1 if there are more stats to get and if bulkstat */
1010         /* should be called again (unused here, but used in dmapi) */
1011
1012         if (!capable(CAP_SYS_ADMIN))
1013                 return -EPERM;
1014
1015         if (XFS_FORCED_SHUTDOWN(mp))
1016                 return -XFS_ERROR(EIO);
1017
1018         if (copy_from_user(&bulkreq, arg, sizeof(xfs_fsop_bulkreq_t)))
1019                 return -XFS_ERROR(EFAULT);
1020
1021         if (copy_from_user(&inlast, bulkreq.lastip, sizeof(__s64)))
1022                 return -XFS_ERROR(EFAULT);
1023
1024         if ((count = bulkreq.icount) <= 0)
1025                 return -XFS_ERROR(EINVAL);
1026
1027         if (cmd == XFS_IOC_FSINUMBERS)
1028                 error = xfs_inumbers(mp, &inlast, &count,
1029                                                 bulkreq.ubuffer);
1030         else if (cmd == XFS_IOC_FSBULKSTAT_SINGLE)
1031                 error = xfs_bulkstat_single(mp, &inlast,
1032                                                 bulkreq.ubuffer, &done);
1033         else {  /* XFS_IOC_FSBULKSTAT */
1034                 if (count == 1 && inlast != 0) {
1035                         inlast++;
1036                         error = xfs_bulkstat_single(mp, &inlast,
1037                                         bulkreq.ubuffer, &done);
1038                 } else {
1039                         error = xfs_bulkstat(mp, &inlast, &count,
1040                                 (bulkstat_one_pf)xfs_bulkstat_one, NULL,
1041                                 sizeof(xfs_bstat_t), bulkreq.ubuffer,
1042                                 BULKSTAT_FG_QUICK, &done);
1043                 }
1044         }
1045
1046         if (error)
1047                 return -error;
1048
1049         if (bulkreq.ocount != NULL) {
1050                 if (copy_to_user(bulkreq.lastip, &inlast,
1051                                                 sizeof(xfs_ino_t)))
1052                         return -XFS_ERROR(EFAULT);
1053
1054                 if (copy_to_user(bulkreq.ocount, &count, sizeof(count)))
1055                         return -XFS_ERROR(EFAULT);
1056         }
1057
1058         return 0;
1059 }
1060
1061 STATIC int
1062 xfs_ioc_fsgeometry_v1(
1063         xfs_mount_t             *mp,
1064         void                    __user *arg)
1065 {
1066         xfs_fsop_geom_v1_t      fsgeo;
1067         int                     error;
1068
1069         error = xfs_fs_geometry(mp, (xfs_fsop_geom_t *)&fsgeo, 3);
1070         if (error)
1071                 return -error;
1072
1073         if (copy_to_user(arg, &fsgeo, sizeof(fsgeo)))
1074                 return -XFS_ERROR(EFAULT);
1075         return 0;
1076 }
1077
1078 STATIC int
1079 xfs_ioc_fsgeometry(
1080         xfs_mount_t             *mp,
1081         void                    __user *arg)
1082 {
1083         xfs_fsop_geom_t         fsgeo;
1084         int                     error;
1085
1086         error = xfs_fs_geometry(mp, &fsgeo, 4);
1087         if (error)
1088                 return -error;
1089
1090         if (copy_to_user(arg, &fsgeo, sizeof(fsgeo)))
1091                 return -XFS_ERROR(EFAULT);
1092         return 0;
1093 }
1094
1095 /*
1096  * Linux extended inode flags interface.
1097  */
1098 #define LINUX_XFLAG_SYNC        0x00000008 /* Synchronous updates */
1099 #define LINUX_XFLAG_IMMUTABLE   0x00000010 /* Immutable file */
1100 #define LINUX_XFLAG_APPEND      0x00000020 /* writes to file may only append */
1101 #define LINUX_XFLAG_NODUMP      0x00000040 /* do not dump file */
1102 #define LINUX_XFLAG_NOATIME     0x00000080 /* do not update atime */
1103
1104 STATIC unsigned int
1105 xfs_merge_ioc_xflags(
1106         unsigned int    flags,
1107         unsigned int    start)
1108 {
1109         unsigned int    xflags = start;
1110
1111         if (flags & LINUX_XFLAG_IMMUTABLE)
1112                 xflags |= XFS_XFLAG_IMMUTABLE;
1113         else
1114                 xflags &= ~XFS_XFLAG_IMMUTABLE;
1115         if (flags & LINUX_XFLAG_APPEND)
1116                 xflags |= XFS_XFLAG_APPEND;
1117         else
1118                 xflags &= ~XFS_XFLAG_APPEND;
1119         if (flags & LINUX_XFLAG_SYNC)
1120                 xflags |= XFS_XFLAG_SYNC;
1121         else
1122                 xflags &= ~XFS_XFLAG_SYNC;
1123         if (flags & LINUX_XFLAG_NOATIME)
1124                 xflags |= XFS_XFLAG_NOATIME;
1125         else
1126                 xflags &= ~XFS_XFLAG_NOATIME;
1127         if (flags & LINUX_XFLAG_NODUMP)
1128                 xflags |= XFS_XFLAG_NODUMP;
1129         else
1130                 xflags &= ~XFS_XFLAG_NODUMP;
1131
1132         return xflags;
1133 }
1134
1135 STATIC unsigned int
1136 xfs_di2lxflags(
1137         __uint16_t      di_flags)
1138 {
1139         unsigned int    flags = 0;
1140
1141         if (di_flags & XFS_DIFLAG_IMMUTABLE)
1142                 flags |= LINUX_XFLAG_IMMUTABLE;
1143         if (di_flags & XFS_DIFLAG_APPEND)
1144                 flags |= LINUX_XFLAG_APPEND;
1145         if (di_flags & XFS_DIFLAG_SYNC)
1146                 flags |= LINUX_XFLAG_SYNC;
1147         if (di_flags & XFS_DIFLAG_NOATIME)
1148                 flags |= LINUX_XFLAG_NOATIME;
1149         if (di_flags & XFS_DIFLAG_NODUMP)
1150                 flags |= LINUX_XFLAG_NODUMP;
1151         return flags;
1152 }
1153
1154 STATIC int
1155 xfs_ioc_xattr(
1156         bhv_vnode_t             *vp,
1157         xfs_inode_t             *ip,
1158         struct file             *filp,
1159         unsigned int            cmd,
1160         void                    __user *arg)
1161 {
1162         struct fsxattr          fa;
1163         struct bhv_vattr        *vattr;
1164         int                     error = 0;
1165         int                     attr_flags;
1166         unsigned int            flags;
1167
1168         vattr = kmalloc(sizeof(*vattr), GFP_KERNEL);
1169         if (unlikely(!vattr))
1170                 return -ENOMEM;
1171
1172         switch (cmd) {
1173         case XFS_IOC_FSGETXATTR: {
1174                 vattr->va_mask = XFS_AT_XFLAGS | XFS_AT_EXTSIZE | \
1175                                  XFS_AT_NEXTENTS | XFS_AT_PROJID;
1176                 error = bhv_vop_getattr(vp, vattr, 0, NULL);
1177                 if (unlikely(error)) {
1178                         error = -error;
1179                         break;
1180                 }
1181
1182                 fa.fsx_xflags   = vattr->va_xflags;
1183                 fa.fsx_extsize  = vattr->va_extsize;
1184                 fa.fsx_nextents = vattr->va_nextents;
1185                 fa.fsx_projid   = vattr->va_projid;
1186
1187                 if (copy_to_user(arg, &fa, sizeof(fa))) {
1188                         error = -EFAULT;
1189                         break;
1190                 }
1191                 break;
1192         }
1193
1194         case XFS_IOC_FSSETXATTR: {
1195                 if (copy_from_user(&fa, arg, sizeof(fa))) {
1196                         error = -EFAULT;
1197                         break;
1198                 }
1199
1200                 attr_flags = 0;
1201                 if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
1202                         attr_flags |= ATTR_NONBLOCK;
1203
1204                 vattr->va_mask = XFS_AT_XFLAGS | XFS_AT_EXTSIZE | XFS_AT_PROJID;
1205                 vattr->va_xflags  = fa.fsx_xflags;
1206                 vattr->va_extsize = fa.fsx_extsize;
1207                 vattr->va_projid  = fa.fsx_projid;
1208
1209                 error = bhv_vop_setattr(vp, vattr, attr_flags, NULL);
1210                 if (likely(!error))
1211                         __vn_revalidate(vp, vattr);     /* update flags */
1212                 error = -error;
1213                 break;
1214         }
1215
1216         case XFS_IOC_FSGETXATTRA: {
1217                 vattr->va_mask = XFS_AT_XFLAGS | XFS_AT_EXTSIZE | \
1218                                  XFS_AT_ANEXTENTS | XFS_AT_PROJID;
1219                 error = bhv_vop_getattr(vp, vattr, 0, NULL);
1220                 if (unlikely(error)) {
1221                         error = -error;
1222                         break;
1223                 }
1224
1225                 fa.fsx_xflags   = vattr->va_xflags;
1226                 fa.fsx_extsize  = vattr->va_extsize;
1227                 fa.fsx_nextents = vattr->va_anextents;
1228                 fa.fsx_projid   = vattr->va_projid;
1229
1230                 if (copy_to_user(arg, &fa, sizeof(fa))) {
1231                         error = -EFAULT;
1232                         break;
1233                 }
1234                 break;
1235         }
1236
1237         case XFS_IOC_GETXFLAGS: {
1238                 flags = xfs_di2lxflags(ip->i_d.di_flags);
1239                 if (copy_to_user(arg, &flags, sizeof(flags)))
1240                         error = -EFAULT;
1241                 break;
1242         }
1243
1244         case XFS_IOC_SETXFLAGS: {
1245                 if (copy_from_user(&flags, arg, sizeof(flags))) {
1246                         error = -EFAULT;
1247                         break;
1248                 }
1249
1250                 if (flags & ~(LINUX_XFLAG_IMMUTABLE | LINUX_XFLAG_APPEND | \
1251                               LINUX_XFLAG_NOATIME | LINUX_XFLAG_NODUMP | \
1252                               LINUX_XFLAG_SYNC)) {
1253                         error = -EOPNOTSUPP;
1254                         break;
1255                 }
1256
1257                 attr_flags = 0;
1258                 if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
1259                         attr_flags |= ATTR_NONBLOCK;
1260
1261                 vattr->va_mask = XFS_AT_XFLAGS;
1262                 vattr->va_xflags = xfs_merge_ioc_xflags(flags,
1263                                                         xfs_ip2xflags(ip));
1264
1265                 error = bhv_vop_setattr(vp, vattr, attr_flags, NULL);
1266                 if (likely(!error))
1267                         __vn_revalidate(vp, vattr);     /* update flags */
1268                 error = -error;
1269                 break;
1270         }
1271
1272         default:
1273                 error = -ENOTTY;
1274                 break;
1275         }
1276
1277         kfree(vattr);
1278         return error;
1279 }
1280
1281 STATIC int
1282 xfs_ioc_getbmap(
1283         bhv_desc_t              *bdp,
1284         struct file             *filp,
1285         int                     ioflags,
1286         unsigned int            cmd,
1287         void                    __user *arg)
1288 {
1289         struct getbmap          bm;
1290         int                     iflags;
1291         int                     error;
1292
1293         if (copy_from_user(&bm, arg, sizeof(bm)))
1294                 return -XFS_ERROR(EFAULT);
1295
1296         if (bm.bmv_count < 2)
1297                 return -XFS_ERROR(EINVAL);
1298
1299         iflags = (cmd == XFS_IOC_GETBMAPA ? BMV_IF_ATTRFORK : 0);
1300         if (ioflags & IO_INVIS)
1301                 iflags |= BMV_IF_NO_DMAPI_READ;
1302
1303         error = xfs_getbmap(bdp, &bm, (struct getbmap __user *)arg+1, iflags);
1304         if (error)
1305                 return -error;
1306
1307         if (copy_to_user(arg, &bm, sizeof(bm)))
1308                 return -XFS_ERROR(EFAULT);
1309         return 0;
1310 }
1311
1312 STATIC int
1313 xfs_ioc_getbmapx(
1314         bhv_desc_t              *bdp,
1315         void                    __user *arg)
1316 {
1317         struct getbmapx         bmx;
1318         struct getbmap          bm;
1319         int                     iflags;
1320         int                     error;
1321
1322         if (copy_from_user(&bmx, arg, sizeof(bmx)))
1323                 return -XFS_ERROR(EFAULT);
1324
1325         if (bmx.bmv_count < 2)
1326                 return -XFS_ERROR(EINVAL);
1327
1328         /*
1329          * Map input getbmapx structure to a getbmap
1330          * structure for xfs_getbmap.
1331          */
1332         GETBMAP_CONVERT(bmx, bm);
1333
1334         iflags = bmx.bmv_iflags;
1335
1336         if (iflags & (~BMV_IF_VALID))
1337                 return -XFS_ERROR(EINVAL);
1338
1339         iflags |= BMV_IF_EXTENDED;
1340
1341         error = xfs_getbmap(bdp, &bm, (struct getbmapx __user *)arg+1, iflags);
1342         if (error)
1343                 return -error;
1344
1345         GETBMAP_CONVERT(bm, bmx);
1346
1347         if (copy_to_user(arg, &bmx, sizeof(bmx)))
1348                 return -XFS_ERROR(EFAULT);
1349
1350         return 0;
1351 }