Merge master.kernel.org:/pub/scm/linux/kernel/git/davem/net-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_dentry);
111                 ASSERT(file->f_dentry->d_inode);
112                 inode = igrab(file->f_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_vfsmnt);
337
338         /* Create file pointer. */
339         filp = dentry_open(dentry, parfilp->f_vfsmnt, 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                 filp->f_op = &xfs_invis_file_operations;
346
347         fd_install(new_fd, filp);
348         return new_fd;
349 }
350
351 STATIC int
352 xfs_readlink_by_handle(
353         xfs_mount_t             *mp,
354         void                    __user *arg,
355         struct file             *parfilp,
356         struct inode            *parinode)
357 {
358         int                     error;
359         struct iovec            aiov;
360         struct uio              auio;
361         struct inode            *inode;
362         xfs_fsop_handlereq_t    hreq;
363         bhv_vnode_t             *vp;
364         __u32                   olen;
365
366         if (!capable(CAP_SYS_ADMIN))
367                 return -XFS_ERROR(EPERM);
368         if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
369                 return -XFS_ERROR(EFAULT);
370
371         error = xfs_vget_fsop_handlereq(mp, parinode, &hreq, &vp, &inode);
372         if (error)
373                 return -error;
374
375         /* Restrict this handle operation to symlinks only. */
376         if (!S_ISLNK(inode->i_mode)) {
377                 VN_RELE(vp);
378                 return -XFS_ERROR(EINVAL);
379         }
380
381         if (copy_from_user(&olen, hreq.ohandlen, sizeof(__u32))) {
382                 VN_RELE(vp);
383                 return -XFS_ERROR(EFAULT);
384         }
385         aiov.iov_len    = olen;
386         aiov.iov_base   = hreq.ohandle;
387
388         auio.uio_iov    = &aiov;
389         auio.uio_iovcnt = 1;
390         auio.uio_offset = 0;
391         auio.uio_segflg = UIO_USERSPACE;
392         auio.uio_resid  = olen;
393
394         error = bhv_vop_readlink(vp, &auio, IO_INVIS, NULL);
395         VN_RELE(vp);
396         if (error)
397                 return -error;
398
399         return (olen - auio.uio_resid);
400 }
401
402 STATIC int
403 xfs_fssetdm_by_handle(
404         xfs_mount_t             *mp,
405         void                    __user *arg,
406         struct file             *parfilp,
407         struct inode            *parinode)
408 {
409         int                     error;
410         struct fsdmidata        fsd;
411         xfs_fsop_setdm_handlereq_t dmhreq;
412         struct inode            *inode;
413         bhv_desc_t              *bdp;
414         bhv_vnode_t             *vp;
415
416         if (!capable(CAP_MKNOD))
417                 return -XFS_ERROR(EPERM);
418         if (copy_from_user(&dmhreq, arg, sizeof(xfs_fsop_setdm_handlereq_t)))
419                 return -XFS_ERROR(EFAULT);
420
421         error = xfs_vget_fsop_handlereq(mp, parinode, &dmhreq.hreq, &vp, &inode);
422         if (error)
423                 return -error;
424
425         if (IS_IMMUTABLE(inode) || IS_APPEND(inode)) {
426                 VN_RELE(vp);
427                 return -XFS_ERROR(EPERM);
428         }
429
430         if (copy_from_user(&fsd, dmhreq.data, sizeof(fsd))) {
431                 VN_RELE(vp);
432                 return -XFS_ERROR(EFAULT);
433         }
434
435         bdp = bhv_base_unlocked(VN_BHV_HEAD(vp));
436         error = xfs_set_dmattrs(bdp, fsd.fsd_dmevmask, fsd.fsd_dmstate, NULL);
437
438         VN_RELE(vp);
439         if (error)
440                 return -error;
441         return 0;
442 }
443
444 STATIC int
445 xfs_attrlist_by_handle(
446         xfs_mount_t             *mp,
447         void                    __user *arg,
448         struct file             *parfilp,
449         struct inode            *parinode)
450 {
451         int                     error;
452         attrlist_cursor_kern_t  *cursor;
453         xfs_fsop_attrlist_handlereq_t al_hreq;
454         struct inode            *inode;
455         bhv_vnode_t             *vp;
456         char                    *kbuf;
457
458         if (!capable(CAP_SYS_ADMIN))
459                 return -XFS_ERROR(EPERM);
460         if (copy_from_user(&al_hreq, arg, sizeof(xfs_fsop_attrlist_handlereq_t)))
461                 return -XFS_ERROR(EFAULT);
462         if (al_hreq.buflen > XATTR_LIST_MAX)
463                 return -XFS_ERROR(EINVAL);
464
465         error = xfs_vget_fsop_handlereq(mp, parinode, &al_hreq.hreq,
466                         &vp, &inode);
467         if (error)
468                 goto out;
469
470         kbuf = kmalloc(al_hreq.buflen, GFP_KERNEL);
471         if (!kbuf)
472                 goto out_vn_rele;
473
474         cursor = (attrlist_cursor_kern_t *)&al_hreq.pos;
475         error = bhv_vop_attr_list(vp, kbuf, al_hreq.buflen, al_hreq.flags,
476                                         cursor, NULL);
477         if (error)
478                 goto out_kfree;
479
480         if (copy_to_user(al_hreq.buffer, kbuf, al_hreq.buflen))
481                 error = -EFAULT;
482
483  out_kfree:
484         kfree(kbuf);
485  out_vn_rele:
486         VN_RELE(vp);
487  out:
488         return -error;
489 }
490
491 STATIC int
492 xfs_attrmulti_attr_get(
493         bhv_vnode_t             *vp,
494         char                    *name,
495         char                    __user *ubuf,
496         __uint32_t              *len,
497         __uint32_t              flags)
498 {
499         char                    *kbuf;
500         int                     error = EFAULT;
501         
502         if (*len > XATTR_SIZE_MAX)
503                 return EINVAL;
504         kbuf = kmalloc(*len, GFP_KERNEL);
505         if (!kbuf)
506                 return ENOMEM;
507
508         error = bhv_vop_attr_get(vp, name, kbuf, len, flags, NULL);
509         if (error)
510                 goto out_kfree;
511
512         if (copy_to_user(ubuf, kbuf, *len))
513                 error = EFAULT;
514
515  out_kfree:
516         kfree(kbuf);
517         return error;
518 }
519
520 STATIC int
521 xfs_attrmulti_attr_set(
522         bhv_vnode_t             *vp,
523         char                    *name,
524         const char              __user *ubuf,
525         __uint32_t              len,
526         __uint32_t              flags)
527 {
528         char                    *kbuf;
529         int                     error = EFAULT;
530
531         if (IS_RDONLY(&vp->v_inode))
532                 return -EROFS;
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         error = bhv_vop_attr_set(vp, name, kbuf, len, flags, NULL);
546
547  out_kfree:
548         kfree(kbuf);
549         return error;
550 }
551
552 STATIC int
553 xfs_attrmulti_attr_remove(
554         bhv_vnode_t             *vp,
555         char                    *name,
556         __uint32_t              flags)
557 {
558         if (IS_RDONLY(&vp->v_inode))
559                 return -EROFS;
560         if (IS_IMMUTABLE(&vp->v_inode) || IS_APPEND(&vp->v_inode))
561                 return EPERM;
562         return bhv_vop_attr_remove(vp, name, flags, NULL);
563 }
564
565 STATIC int
566 xfs_attrmulti_by_handle(
567         xfs_mount_t             *mp,
568         void                    __user *arg,
569         struct file             *parfilp,
570         struct inode            *parinode)
571 {
572         int                     error;
573         xfs_attr_multiop_t      *ops;
574         xfs_fsop_attrmulti_handlereq_t am_hreq;
575         struct inode            *inode;
576         bhv_vnode_t             *vp;
577         unsigned int            i, size;
578         char                    *attr_name;
579
580         if (!capable(CAP_SYS_ADMIN))
581                 return -XFS_ERROR(EPERM);
582         if (copy_from_user(&am_hreq, arg, sizeof(xfs_fsop_attrmulti_handlereq_t)))
583                 return -XFS_ERROR(EFAULT);
584
585         error = xfs_vget_fsop_handlereq(mp, parinode, &am_hreq.hreq, &vp, &inode);
586         if (error)
587                 goto out;
588
589         error = E2BIG;
590         size = am_hreq.opcount * sizeof(attr_multiop_t);
591         if (!size || size > 16 * PAGE_SIZE)
592                 goto out_vn_rele;
593
594         error = ENOMEM;
595         ops = kmalloc(size, GFP_KERNEL);
596         if (!ops)
597                 goto out_vn_rele;
598
599         error = EFAULT;
600         if (copy_from_user(ops, am_hreq.ops, size))
601                 goto out_kfree_ops;
602
603         attr_name = kmalloc(MAXNAMELEN, GFP_KERNEL);
604         if (!attr_name)
605                 goto out_kfree_ops;
606
607
608         error = 0;
609         for (i = 0; i < am_hreq.opcount; i++) {
610                 ops[i].am_error = strncpy_from_user(attr_name,
611                                 ops[i].am_attrname, MAXNAMELEN);
612                 if (ops[i].am_error == 0 || ops[i].am_error == MAXNAMELEN)
613                         error = -ERANGE;
614                 if (ops[i].am_error < 0)
615                         break;
616
617                 switch (ops[i].am_opcode) {
618                 case ATTR_OP_GET:
619                         ops[i].am_error = xfs_attrmulti_attr_get(vp,
620                                         attr_name, ops[i].am_attrvalue,
621                                         &ops[i].am_length, ops[i].am_flags);
622                         break;
623                 case ATTR_OP_SET:
624                         ops[i].am_error = xfs_attrmulti_attr_set(vp,
625                                         attr_name, ops[i].am_attrvalue,
626                                         ops[i].am_length, ops[i].am_flags);
627                         break;
628                 case ATTR_OP_REMOVE:
629                         ops[i].am_error = xfs_attrmulti_attr_remove(vp,
630                                         attr_name, ops[i].am_flags);
631                         break;
632                 default:
633                         ops[i].am_error = EINVAL;
634                 }
635         }
636
637         if (copy_to_user(am_hreq.ops, ops, size))
638                 error = XFS_ERROR(EFAULT);
639
640         kfree(attr_name);
641  out_kfree_ops:
642         kfree(ops);
643  out_vn_rele:
644         VN_RELE(vp);
645  out:
646         return -error;
647 }
648
649 /* prototypes for a few of the stack-hungry cases that have
650  * their own functions.  Functions are defined after their use
651  * so gcc doesn't get fancy and inline them with -03 */
652
653 STATIC int
654 xfs_ioc_space(
655         bhv_desc_t              *bdp,
656         struct inode            *inode,
657         struct file             *filp,
658         int                     flags,
659         unsigned int            cmd,
660         void                    __user *arg);
661
662 STATIC int
663 xfs_ioc_bulkstat(
664         xfs_mount_t             *mp,
665         unsigned int            cmd,
666         void                    __user *arg);
667
668 STATIC int
669 xfs_ioc_fsgeometry_v1(
670         xfs_mount_t             *mp,
671         void                    __user *arg);
672
673 STATIC int
674 xfs_ioc_fsgeometry(
675         xfs_mount_t             *mp,
676         void                    __user *arg);
677
678 STATIC int
679 xfs_ioc_xattr(
680         bhv_vnode_t             *vp,
681         xfs_inode_t             *ip,
682         struct file             *filp,
683         unsigned int            cmd,
684         void                    __user *arg);
685
686 STATIC int
687 xfs_ioc_getbmap(
688         bhv_desc_t              *bdp,
689         struct file             *filp,
690         int                     flags,
691         unsigned int            cmd,
692         void                    __user *arg);
693
694 STATIC int
695 xfs_ioc_getbmapx(
696         bhv_desc_t              *bdp,
697         void                    __user *arg);
698
699 int
700 xfs_ioctl(
701         bhv_desc_t              *bdp,
702         struct inode            *inode,
703         struct file             *filp,
704         int                     ioflags,
705         unsigned int            cmd,
706         void                    __user *arg)
707 {
708         int                     error;
709         bhv_vnode_t             *vp;
710         xfs_inode_t             *ip;
711         xfs_mount_t             *mp;
712
713         vp = vn_from_inode(inode);
714
715         vn_trace_entry(vp, "xfs_ioctl", (inst_t *)__return_address);
716
717         ip = XFS_BHVTOI(bdp);
718         mp = ip->i_mount;
719
720         switch (cmd) {
721
722         case XFS_IOC_ALLOCSP:
723         case XFS_IOC_FREESP:
724         case XFS_IOC_RESVSP:
725         case XFS_IOC_UNRESVSP:
726         case XFS_IOC_ALLOCSP64:
727         case XFS_IOC_FREESP64:
728         case XFS_IOC_RESVSP64:
729         case XFS_IOC_UNRESVSP64:
730                 /*
731                  * Only allow the sys admin to reserve space unless
732                  * unwritten extents are enabled.
733                  */
734                 if (!XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb) &&
735                     !capable(CAP_SYS_ADMIN))
736                         return -EPERM;
737
738                 return xfs_ioc_space(bdp, inode, filp, ioflags, cmd, arg);
739
740         case XFS_IOC_DIOINFO: {
741                 struct dioattr  da;
742                 xfs_buftarg_t   *target =
743                         (ip->i_d.di_flags & XFS_DIFLAG_REALTIME) ?
744                         mp->m_rtdev_targp : mp->m_ddev_targp;
745
746                 da.d_mem = da.d_miniosz = 1 << target->bt_sshift;
747                 da.d_maxiosz = INT_MAX & ~(da.d_miniosz - 1);
748
749                 if (copy_to_user(arg, &da, sizeof(da)))
750                         return -XFS_ERROR(EFAULT);
751                 return 0;
752         }
753
754         case XFS_IOC_FSBULKSTAT_SINGLE:
755         case XFS_IOC_FSBULKSTAT:
756         case XFS_IOC_FSINUMBERS:
757                 return xfs_ioc_bulkstat(mp, cmd, arg);
758
759         case XFS_IOC_FSGEOMETRY_V1:
760                 return xfs_ioc_fsgeometry_v1(mp, arg);
761
762         case XFS_IOC_FSGEOMETRY:
763                 return xfs_ioc_fsgeometry(mp, arg);
764
765         case XFS_IOC_GETVERSION:
766                 return put_user(inode->i_generation, (int __user *)arg);
767
768         case XFS_IOC_GETXFLAGS:
769         case XFS_IOC_SETXFLAGS:
770         case XFS_IOC_FSGETXATTR:
771         case XFS_IOC_FSSETXATTR:
772         case XFS_IOC_FSGETXATTRA:
773                 return xfs_ioc_xattr(vp, ip, filp, cmd, arg);
774
775         case XFS_IOC_FSSETDM: {
776                 struct fsdmidata        dmi;
777
778                 if (copy_from_user(&dmi, arg, sizeof(dmi)))
779                         return -XFS_ERROR(EFAULT);
780
781                 error = xfs_set_dmattrs(bdp, dmi.fsd_dmevmask, dmi.fsd_dmstate,
782                                                         NULL);
783                 return -error;
784         }
785
786         case XFS_IOC_GETBMAP:
787         case XFS_IOC_GETBMAPA:
788                 return xfs_ioc_getbmap(bdp, filp, ioflags, cmd, arg);
789
790         case XFS_IOC_GETBMAPX:
791                 return xfs_ioc_getbmapx(bdp, arg);
792
793         case XFS_IOC_FD_TO_HANDLE:
794         case XFS_IOC_PATH_TO_HANDLE:
795         case XFS_IOC_PATH_TO_FSHANDLE:
796                 return xfs_find_handle(cmd, arg);
797
798         case XFS_IOC_OPEN_BY_HANDLE:
799                 return xfs_open_by_handle(mp, arg, filp, inode);
800
801         case XFS_IOC_FSSETDM_BY_HANDLE:
802                 return xfs_fssetdm_by_handle(mp, arg, filp, inode);
803
804         case XFS_IOC_READLINK_BY_HANDLE:
805                 return xfs_readlink_by_handle(mp, arg, filp, inode);
806
807         case XFS_IOC_ATTRLIST_BY_HANDLE:
808                 return xfs_attrlist_by_handle(mp, arg, filp, inode);
809
810         case XFS_IOC_ATTRMULTI_BY_HANDLE:
811                 return xfs_attrmulti_by_handle(mp, arg, filp, inode);
812
813         case XFS_IOC_SWAPEXT: {
814                 error = xfs_swapext((struct xfs_swapext __user *)arg);
815                 return -error;
816         }
817
818         case XFS_IOC_FSCOUNTS: {
819                 xfs_fsop_counts_t out;
820
821                 error = xfs_fs_counts(mp, &out);
822                 if (error)
823                         return -error;
824
825                 if (copy_to_user(arg, &out, sizeof(out)))
826                         return -XFS_ERROR(EFAULT);
827                 return 0;
828         }
829
830         case XFS_IOC_SET_RESBLKS: {
831                 xfs_fsop_resblks_t inout;
832                 __uint64_t         in;
833
834                 if (!capable(CAP_SYS_ADMIN))
835                         return -EPERM;
836
837                 if (copy_from_user(&inout, arg, sizeof(inout)))
838                         return -XFS_ERROR(EFAULT);
839
840                 /* input parameter is passed in resblks field of structure */
841                 in = inout.resblks;
842                 error = xfs_reserve_blocks(mp, &in, &inout);
843                 if (error)
844                         return -error;
845
846                 if (copy_to_user(arg, &inout, sizeof(inout)))
847                         return -XFS_ERROR(EFAULT);
848                 return 0;
849         }
850
851         case XFS_IOC_GET_RESBLKS: {
852                 xfs_fsop_resblks_t out;
853
854                 if (!capable(CAP_SYS_ADMIN))
855                         return -EPERM;
856
857                 error = xfs_reserve_blocks(mp, NULL, &out);
858                 if (error)
859                         return -error;
860
861                 if (copy_to_user(arg, &out, sizeof(out)))
862                         return -XFS_ERROR(EFAULT);
863
864                 return 0;
865         }
866
867         case XFS_IOC_FSGROWFSDATA: {
868                 xfs_growfs_data_t in;
869
870                 if (!capable(CAP_SYS_ADMIN))
871                         return -EPERM;
872
873                 if (copy_from_user(&in, arg, sizeof(in)))
874                         return -XFS_ERROR(EFAULT);
875
876                 error = xfs_growfs_data(mp, &in);
877                 return -error;
878         }
879
880         case XFS_IOC_FSGROWFSLOG: {
881                 xfs_growfs_log_t in;
882
883                 if (!capable(CAP_SYS_ADMIN))
884                         return -EPERM;
885
886                 if (copy_from_user(&in, arg, sizeof(in)))
887                         return -XFS_ERROR(EFAULT);
888
889                 error = xfs_growfs_log(mp, &in);
890                 return -error;
891         }
892
893         case XFS_IOC_FSGROWFSRT: {
894                 xfs_growfs_rt_t in;
895
896                 if (!capable(CAP_SYS_ADMIN))
897                         return -EPERM;
898
899                 if (copy_from_user(&in, arg, sizeof(in)))
900                         return -XFS_ERROR(EFAULT);
901
902                 error = xfs_growfs_rt(mp, &in);
903                 return -error;
904         }
905
906         case XFS_IOC_FREEZE:
907                 if (!capable(CAP_SYS_ADMIN))
908                         return -EPERM;
909
910                 if (inode->i_sb->s_frozen == SB_UNFROZEN)
911                         freeze_bdev(inode->i_sb->s_bdev);
912                 return 0;
913
914         case XFS_IOC_THAW:
915                 if (!capable(CAP_SYS_ADMIN))
916                         return -EPERM;
917                 if (inode->i_sb->s_frozen != SB_UNFROZEN)
918                         thaw_bdev(inode->i_sb->s_bdev, inode->i_sb);
919                 return 0;
920
921         case XFS_IOC_GOINGDOWN: {
922                 __uint32_t in;
923
924                 if (!capable(CAP_SYS_ADMIN))
925                         return -EPERM;
926
927                 if (get_user(in, (__uint32_t __user *)arg))
928                         return -XFS_ERROR(EFAULT);
929
930                 error = xfs_fs_goingdown(mp, in);
931                 return -error;
932         }
933
934         case XFS_IOC_ERROR_INJECTION: {
935                 xfs_error_injection_t in;
936
937                 if (!capable(CAP_SYS_ADMIN))
938                         return -EPERM;
939
940                 if (copy_from_user(&in, arg, sizeof(in)))
941                         return -XFS_ERROR(EFAULT);
942
943                 error = xfs_errortag_add(in.errtag, mp);
944                 return -error;
945         }
946
947         case XFS_IOC_ERROR_CLEARALL:
948                 if (!capable(CAP_SYS_ADMIN))
949                         return -EPERM;
950
951                 error = xfs_errortag_clearall(mp);
952                 return -error;
953
954         default:
955                 return -ENOTTY;
956         }
957 }
958
959 STATIC int
960 xfs_ioc_space(
961         bhv_desc_t              *bdp,
962         struct inode            *inode,
963         struct file             *filp,
964         int                     ioflags,
965         unsigned int            cmd,
966         void                    __user *arg)
967 {
968         xfs_flock64_t           bf;
969         int                     attr_flags = 0;
970         int                     error;
971
972         if (inode->i_flags & (S_IMMUTABLE|S_APPEND))
973                 return -XFS_ERROR(EPERM);
974
975         if (!(filp->f_mode & FMODE_WRITE))
976                 return -XFS_ERROR(EBADF);
977
978         if (!S_ISREG(inode->i_mode))
979                 return -XFS_ERROR(EINVAL);
980
981         if (copy_from_user(&bf, arg, sizeof(bf)))
982                 return -XFS_ERROR(EFAULT);
983
984         if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
985                 attr_flags |= ATTR_NONBLOCK;
986         if (ioflags & IO_INVIS)
987                 attr_flags |= ATTR_DMI;
988
989         error = xfs_change_file_space(bdp, cmd, &bf, filp->f_pos,
990                                               NULL, attr_flags);
991         return -error;
992 }
993
994 STATIC int
995 xfs_ioc_bulkstat(
996         xfs_mount_t             *mp,
997         unsigned int            cmd,
998         void                    __user *arg)
999 {
1000         xfs_fsop_bulkreq_t      bulkreq;
1001         int                     count;  /* # of records returned */
1002         xfs_ino_t               inlast; /* last inode number */
1003         int                     done;
1004         int                     error;
1005
1006         /* done = 1 if there are more stats to get and if bulkstat */
1007         /* should be called again (unused here, but used in dmapi) */
1008
1009         if (!capable(CAP_SYS_ADMIN))
1010                 return -EPERM;
1011
1012         if (XFS_FORCED_SHUTDOWN(mp))
1013                 return -XFS_ERROR(EIO);
1014
1015         if (copy_from_user(&bulkreq, arg, sizeof(xfs_fsop_bulkreq_t)))
1016                 return -XFS_ERROR(EFAULT);
1017
1018         if (copy_from_user(&inlast, bulkreq.lastip, sizeof(__s64)))
1019                 return -XFS_ERROR(EFAULT);
1020
1021         if ((count = bulkreq.icount) <= 0)
1022                 return -XFS_ERROR(EINVAL);
1023
1024         if (cmd == XFS_IOC_FSINUMBERS)
1025                 error = xfs_inumbers(mp, &inlast, &count,
1026                                                 bulkreq.ubuffer);
1027         else if (cmd == XFS_IOC_FSBULKSTAT_SINGLE)
1028                 error = xfs_bulkstat_single(mp, &inlast,
1029                                                 bulkreq.ubuffer, &done);
1030         else {  /* XFS_IOC_FSBULKSTAT */
1031                 if (count == 1 && inlast != 0) {
1032                         inlast++;
1033                         error = xfs_bulkstat_single(mp, &inlast,
1034                                         bulkreq.ubuffer, &done);
1035                 } else {
1036                         error = xfs_bulkstat(mp, &inlast, &count,
1037                                 (bulkstat_one_pf)xfs_bulkstat_one, NULL,
1038                                 sizeof(xfs_bstat_t), bulkreq.ubuffer,
1039                                 BULKSTAT_FG_QUICK, &done);
1040                 }
1041         }
1042
1043         if (error)
1044                 return -error;
1045
1046         if (bulkreq.ocount != NULL) {
1047                 if (copy_to_user(bulkreq.lastip, &inlast,
1048                                                 sizeof(xfs_ino_t)))
1049                         return -XFS_ERROR(EFAULT);
1050
1051                 if (copy_to_user(bulkreq.ocount, &count, sizeof(count)))
1052                         return -XFS_ERROR(EFAULT);
1053         }
1054
1055         return 0;
1056 }
1057
1058 STATIC int
1059 xfs_ioc_fsgeometry_v1(
1060         xfs_mount_t             *mp,
1061         void                    __user *arg)
1062 {
1063         xfs_fsop_geom_v1_t      fsgeo;
1064         int                     error;
1065
1066         error = xfs_fs_geometry(mp, (xfs_fsop_geom_t *)&fsgeo, 3);
1067         if (error)
1068                 return -error;
1069
1070         if (copy_to_user(arg, &fsgeo, sizeof(fsgeo)))
1071                 return -XFS_ERROR(EFAULT);
1072         return 0;
1073 }
1074
1075 STATIC int
1076 xfs_ioc_fsgeometry(
1077         xfs_mount_t             *mp,
1078         void                    __user *arg)
1079 {
1080         xfs_fsop_geom_t         fsgeo;
1081         int                     error;
1082
1083         error = xfs_fs_geometry(mp, &fsgeo, 4);
1084         if (error)
1085                 return -error;
1086
1087         if (copy_to_user(arg, &fsgeo, sizeof(fsgeo)))
1088                 return -XFS_ERROR(EFAULT);
1089         return 0;
1090 }
1091
1092 /*
1093  * Linux extended inode flags interface.
1094  */
1095 #define LINUX_XFLAG_SYNC        0x00000008 /* Synchronous updates */
1096 #define LINUX_XFLAG_IMMUTABLE   0x00000010 /* Immutable file */
1097 #define LINUX_XFLAG_APPEND      0x00000020 /* writes to file may only append */
1098 #define LINUX_XFLAG_NODUMP      0x00000040 /* do not dump file */
1099 #define LINUX_XFLAG_NOATIME     0x00000080 /* do not update atime */
1100
1101 STATIC unsigned int
1102 xfs_merge_ioc_xflags(
1103         unsigned int    flags,
1104         unsigned int    start)
1105 {
1106         unsigned int    xflags = start;
1107
1108         if (flags & LINUX_XFLAG_IMMUTABLE)
1109                 xflags |= XFS_XFLAG_IMMUTABLE;
1110         else
1111                 xflags &= ~XFS_XFLAG_IMMUTABLE;
1112         if (flags & LINUX_XFLAG_APPEND)
1113                 xflags |= XFS_XFLAG_APPEND;
1114         else
1115                 xflags &= ~XFS_XFLAG_APPEND;
1116         if (flags & LINUX_XFLAG_SYNC)
1117                 xflags |= XFS_XFLAG_SYNC;
1118         else
1119                 xflags &= ~XFS_XFLAG_SYNC;
1120         if (flags & LINUX_XFLAG_NOATIME)
1121                 xflags |= XFS_XFLAG_NOATIME;
1122         else
1123                 xflags &= ~XFS_XFLAG_NOATIME;
1124         if (flags & LINUX_XFLAG_NODUMP)
1125                 xflags |= XFS_XFLAG_NODUMP;
1126         else
1127                 xflags &= ~XFS_XFLAG_NODUMP;
1128
1129         return xflags;
1130 }
1131
1132 STATIC unsigned int
1133 xfs_di2lxflags(
1134         __uint16_t      di_flags)
1135 {
1136         unsigned int    flags = 0;
1137
1138         if (di_flags & XFS_DIFLAG_IMMUTABLE)
1139                 flags |= LINUX_XFLAG_IMMUTABLE;
1140         if (di_flags & XFS_DIFLAG_APPEND)
1141                 flags |= LINUX_XFLAG_APPEND;
1142         if (di_flags & XFS_DIFLAG_SYNC)
1143                 flags |= LINUX_XFLAG_SYNC;
1144         if (di_flags & XFS_DIFLAG_NOATIME)
1145                 flags |= LINUX_XFLAG_NOATIME;
1146         if (di_flags & XFS_DIFLAG_NODUMP)
1147                 flags |= LINUX_XFLAG_NODUMP;
1148         return flags;
1149 }
1150
1151 STATIC int
1152 xfs_ioc_xattr(
1153         bhv_vnode_t             *vp,
1154         xfs_inode_t             *ip,
1155         struct file             *filp,
1156         unsigned int            cmd,
1157         void                    __user *arg)
1158 {
1159         struct fsxattr          fa;
1160         struct bhv_vattr        *vattr;
1161         int                     error = 0;
1162         int                     attr_flags;
1163         unsigned int            flags;
1164
1165         vattr = kmalloc(sizeof(*vattr), GFP_KERNEL);
1166         if (unlikely(!vattr))
1167                 return -ENOMEM;
1168
1169         switch (cmd) {
1170         case XFS_IOC_FSGETXATTR: {
1171                 vattr->va_mask = XFS_AT_XFLAGS | XFS_AT_EXTSIZE | \
1172                                  XFS_AT_NEXTENTS | XFS_AT_PROJID;
1173                 error = bhv_vop_getattr(vp, vattr, 0, NULL);
1174                 if (unlikely(error)) {
1175                         error = -error;
1176                         break;
1177                 }
1178
1179                 fa.fsx_xflags   = vattr->va_xflags;
1180                 fa.fsx_extsize  = vattr->va_extsize;
1181                 fa.fsx_nextents = vattr->va_nextents;
1182                 fa.fsx_projid   = vattr->va_projid;
1183
1184                 if (copy_to_user(arg, &fa, sizeof(fa))) {
1185                         error = -EFAULT;
1186                         break;
1187                 }
1188                 break;
1189         }
1190
1191         case XFS_IOC_FSSETXATTR: {
1192                 if (copy_from_user(&fa, arg, sizeof(fa))) {
1193                         error = -EFAULT;
1194                         break;
1195                 }
1196
1197                 attr_flags = 0;
1198                 if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
1199                         attr_flags |= ATTR_NONBLOCK;
1200
1201                 vattr->va_mask = XFS_AT_XFLAGS | XFS_AT_EXTSIZE | XFS_AT_PROJID;
1202                 vattr->va_xflags  = fa.fsx_xflags;
1203                 vattr->va_extsize = fa.fsx_extsize;
1204                 vattr->va_projid  = fa.fsx_projid;
1205
1206                 error = bhv_vop_setattr(vp, vattr, attr_flags, NULL);
1207                 if (likely(!error))
1208                         __vn_revalidate(vp, vattr);     /* update flags */
1209                 error = -error;
1210                 break;
1211         }
1212
1213         case XFS_IOC_FSGETXATTRA: {
1214                 vattr->va_mask = XFS_AT_XFLAGS | XFS_AT_EXTSIZE | \
1215                                  XFS_AT_ANEXTENTS | XFS_AT_PROJID;
1216                 error = bhv_vop_getattr(vp, vattr, 0, NULL);
1217                 if (unlikely(error)) {
1218                         error = -error;
1219                         break;
1220                 }
1221
1222                 fa.fsx_xflags   = vattr->va_xflags;
1223                 fa.fsx_extsize  = vattr->va_extsize;
1224                 fa.fsx_nextents = vattr->va_anextents;
1225                 fa.fsx_projid   = vattr->va_projid;
1226
1227                 if (copy_to_user(arg, &fa, sizeof(fa))) {
1228                         error = -EFAULT;
1229                         break;
1230                 }
1231                 break;
1232         }
1233
1234         case XFS_IOC_GETXFLAGS: {
1235                 flags = xfs_di2lxflags(ip->i_d.di_flags);
1236                 if (copy_to_user(arg, &flags, sizeof(flags)))
1237                         error = -EFAULT;
1238                 break;
1239         }
1240
1241         case XFS_IOC_SETXFLAGS: {
1242                 if (copy_from_user(&flags, arg, sizeof(flags))) {
1243                         error = -EFAULT;
1244                         break;
1245                 }
1246
1247                 if (flags & ~(LINUX_XFLAG_IMMUTABLE | LINUX_XFLAG_APPEND | \
1248                               LINUX_XFLAG_NOATIME | LINUX_XFLAG_NODUMP | \
1249                               LINUX_XFLAG_SYNC)) {
1250                         error = -EOPNOTSUPP;
1251                         break;
1252                 }
1253
1254                 attr_flags = 0;
1255                 if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
1256                         attr_flags |= ATTR_NONBLOCK;
1257
1258                 vattr->va_mask = XFS_AT_XFLAGS;
1259                 vattr->va_xflags = xfs_merge_ioc_xflags(flags,
1260                                                         xfs_ip2xflags(ip));
1261
1262                 error = bhv_vop_setattr(vp, vattr, attr_flags, NULL);
1263                 if (likely(!error))
1264                         __vn_revalidate(vp, vattr);     /* update flags */
1265                 error = -error;
1266                 break;
1267         }
1268
1269         default:
1270                 error = -ENOTTY;
1271                 break;
1272         }
1273
1274         kfree(vattr);
1275         return error;
1276 }
1277
1278 STATIC int
1279 xfs_ioc_getbmap(
1280         bhv_desc_t              *bdp,
1281         struct file             *filp,
1282         int                     ioflags,
1283         unsigned int            cmd,
1284         void                    __user *arg)
1285 {
1286         struct getbmap          bm;
1287         int                     iflags;
1288         int                     error;
1289
1290         if (copy_from_user(&bm, arg, sizeof(bm)))
1291                 return -XFS_ERROR(EFAULT);
1292
1293         if (bm.bmv_count < 2)
1294                 return -XFS_ERROR(EINVAL);
1295
1296         iflags = (cmd == XFS_IOC_GETBMAPA ? BMV_IF_ATTRFORK : 0);
1297         if (ioflags & IO_INVIS)
1298                 iflags |= BMV_IF_NO_DMAPI_READ;
1299
1300         error = xfs_getbmap(bdp, &bm, (struct getbmap __user *)arg+1, iflags);
1301         if (error)
1302                 return -error;
1303
1304         if (copy_to_user(arg, &bm, sizeof(bm)))
1305                 return -XFS_ERROR(EFAULT);
1306         return 0;
1307 }
1308
1309 STATIC int
1310 xfs_ioc_getbmapx(
1311         bhv_desc_t              *bdp,
1312         void                    __user *arg)
1313 {
1314         struct getbmapx         bmx;
1315         struct getbmap          bm;
1316         int                     iflags;
1317         int                     error;
1318
1319         if (copy_from_user(&bmx, arg, sizeof(bmx)))
1320                 return -XFS_ERROR(EFAULT);
1321
1322         if (bmx.bmv_count < 2)
1323                 return -XFS_ERROR(EINVAL);
1324
1325         /*
1326          * Map input getbmapx structure to a getbmap
1327          * structure for xfs_getbmap.
1328          */
1329         GETBMAP_CONVERT(bmx, bm);
1330
1331         iflags = bmx.bmv_iflags;
1332
1333         if (iflags & (~BMV_IF_VALID))
1334                 return -XFS_ERROR(EINVAL);
1335
1336         iflags |= BMV_IF_EXTENDED;
1337
1338         error = xfs_getbmap(bdp, &bm, (struct getbmapx __user *)arg+1, iflags);
1339         if (error)
1340                 return -error;
1341
1342         GETBMAP_CONVERT(bm, bmx);
1343
1344         if (copy_to_user(arg, &bmx, sizeof(bmx)))
1345                 return -XFS_ERROR(EFAULT);
1346
1347         return 0;
1348 }