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