]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - fs/xfs/linux-2.6/xfs_ioctl.c
[XFS] Push some common code out of write path into core XFS code for
[linux-2.6-omap-h63xx.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/capability.h>
56 #include <linux/dcache.h>
57 #include <linux/mount.h>
58 #include <linux/namei.h>
59 #include <linux/pagemap.h>
60
61 /*
62  * xfs_find_handle maps from userspace xfs_fsop_handlereq structure to
63  * a file or fs handle.
64  *
65  * XFS_IOC_PATH_TO_FSHANDLE
66  *    returns fs handle for a mount point or path within that mount point
67  * XFS_IOC_FD_TO_HANDLE
68  *    returns full handle for a FD opened in user space
69  * XFS_IOC_PATH_TO_HANDLE
70  *    returns full handle for a path
71  */
72 STATIC int
73 xfs_find_handle(
74         unsigned int            cmd,
75         void                    __user *arg)
76 {
77         int                     hsize;
78         xfs_handle_t            handle;
79         xfs_fsop_handlereq_t    hreq;
80         struct inode            *inode;
81         bhv_vnode_t             *vp;
82
83         if (copy_from_user(&hreq, arg, sizeof(hreq)))
84                 return -XFS_ERROR(EFAULT);
85
86         memset((char *)&handle, 0, sizeof(handle));
87
88         switch (cmd) {
89         case XFS_IOC_PATH_TO_FSHANDLE:
90         case XFS_IOC_PATH_TO_HANDLE: {
91                 struct nameidata        nd;
92                 int                     error;
93
94                 error = user_path_walk_link((const char __user *)hreq.path, &nd);
95                 if (error)
96                         return error;
97
98                 ASSERT(nd.dentry);
99                 ASSERT(nd.dentry->d_inode);
100                 inode = igrab(nd.dentry->d_inode);
101                 path_release(&nd);
102                 break;
103         }
104
105         case XFS_IOC_FD_TO_HANDLE: {
106                 struct file     *file;
107
108                 file = fget(hreq.fd);
109                 if (!file)
110                     return -EBADF;
111
112                 ASSERT(file->f_dentry);
113                 ASSERT(file->f_dentry->d_inode);
114                 inode = igrab(file->f_dentry->d_inode);
115                 fput(file);
116                 break;
117         }
118
119         default:
120                 ASSERT(0);
121                 return -XFS_ERROR(EINVAL);
122         }
123
124         if (inode->i_sb->s_magic != XFS_SB_MAGIC) {
125                 /* we're not in XFS anymore, Toto */
126                 iput(inode);
127                 return -XFS_ERROR(EINVAL);
128         }
129
130         switch (inode->i_mode & S_IFMT) {
131         case S_IFREG:
132         case S_IFDIR:
133         case S_IFLNK:
134                 break;
135         default:
136                 iput(inode);
137                 return -XFS_ERROR(EBADF);
138         }
139
140         /* we need the vnode */
141         vp = vn_from_inode(inode);
142
143         /* now we can grab the fsid */
144         memcpy(&handle.ha_fsid, vp->v_vfsp->vfs_altfsid, sizeof(xfs_fsid_t));
145         hsize = sizeof(xfs_fsid_t);
146
147         if (cmd != XFS_IOC_PATH_TO_FSHANDLE) {
148                 xfs_inode_t     *ip;
149                 int             lock_mode;
150
151                 /* need to get access to the xfs_inode to read the generation */
152                 ip = xfs_vtoi(vp);
153                 ASSERT(ip);
154                 lock_mode = xfs_ilock_map_shared(ip);
155
156                 /* fill in fid section of handle from inode */
157                 handle.ha_fid.xfs_fid_len = sizeof(xfs_fid_t) -
158                                             sizeof(handle.ha_fid.xfs_fid_len);
159                 handle.ha_fid.xfs_fid_pad = 0;
160                 handle.ha_fid.xfs_fid_gen = ip->i_d.di_gen;
161                 handle.ha_fid.xfs_fid_ino = ip->i_ino;
162
163                 xfs_iunlock_map_shared(ip, lock_mode);
164
165                 hsize = XFS_HSIZE(handle);
166         }
167
168         /* now copy our handle into the user buffer & write out the size */
169         if (copy_to_user(hreq.ohandle, &handle, hsize) ||
170             copy_to_user(hreq.ohandlen, &hsize, sizeof(__s32))) {
171                 iput(inode);
172                 return -XFS_ERROR(EFAULT);
173         }
174
175         iput(inode);
176         return 0;
177 }
178
179
180 /*
181  * Convert userspace handle data into vnode (and inode).
182  * We [ab]use the fact that all the fsop_handlereq ioctl calls
183  * have a data structure argument whose first component is always
184  * a xfs_fsop_handlereq_t, so we can cast to and from this type.
185  * This allows us to optimise the copy_from_user calls and gives
186  * a handy, shared routine.
187  *
188  * If no error, caller must always VN_RELE the returned vp.
189  */
190 STATIC int
191 xfs_vget_fsop_handlereq(
192         xfs_mount_t             *mp,
193         struct inode            *parinode,      /* parent inode pointer    */
194         xfs_fsop_handlereq_t    *hreq,
195         bhv_vnode_t             **vp,
196         struct inode            **inode)
197 {
198         void                    __user *hanp;
199         size_t                  hlen;
200         xfs_fid_t               *xfid;
201         xfs_handle_t            *handlep;
202         xfs_handle_t            handle;
203         xfs_inode_t             *ip;
204         struct inode            *inodep;
205         bhv_vnode_t             *vpp;
206         xfs_ino_t               ino;
207         __u32                   igen;
208         int                     error;
209
210         /*
211          * Only allow handle opens under a directory.
212          */
213         if (!S_ISDIR(parinode->i_mode))
214                 return XFS_ERROR(ENOTDIR);
215
216         hanp = hreq->ihandle;
217         hlen = hreq->ihandlen;
218         handlep = &handle;
219
220         if (hlen < sizeof(handlep->ha_fsid) || hlen > sizeof(*handlep))
221                 return XFS_ERROR(EINVAL);
222         if (copy_from_user(handlep, hanp, hlen))
223                 return XFS_ERROR(EFAULT);
224         if (hlen < sizeof(*handlep))
225                 memset(((char *)handlep) + hlen, 0, sizeof(*handlep) - hlen);
226         if (hlen > sizeof(handlep->ha_fsid)) {
227                 if (handlep->ha_fid.xfs_fid_len !=
228                                 (hlen - sizeof(handlep->ha_fsid)
229                                         - sizeof(handlep->ha_fid.xfs_fid_len))
230                     || handlep->ha_fid.xfs_fid_pad)
231                         return XFS_ERROR(EINVAL);
232         }
233
234         /*
235          * Crack the handle, obtain the inode # & generation #
236          */
237         xfid = (struct xfs_fid *)&handlep->ha_fid;
238         if (xfid->xfs_fid_len == sizeof(*xfid) - sizeof(xfid->xfs_fid_len)) {
239                 ino  = xfid->xfs_fid_ino;
240                 igen = xfid->xfs_fid_gen;
241         } else {
242                 return XFS_ERROR(EINVAL);
243         }
244
245         /*
246          * Get the XFS inode, building a vnode to go with it.
247          */
248         error = xfs_iget(mp, NULL, ino, 0, XFS_ILOCK_SHARED, &ip, 0);
249         if (error)
250                 return error;
251         if (ip == NULL)
252                 return XFS_ERROR(EIO);
253         if (ip->i_d.di_mode == 0 || ip->i_d.di_gen != igen) {
254                 xfs_iput_new(ip, XFS_ILOCK_SHARED);
255                 return XFS_ERROR(ENOENT);
256         }
257
258         vpp = XFS_ITOV(ip);
259         inodep = vn_to_inode(vpp);
260         xfs_iunlock(ip, XFS_ILOCK_SHARED);
261
262         *vp = vpp;
263         *inode = inodep;
264         return 0;
265 }
266
267 STATIC int
268 xfs_open_by_handle(
269         xfs_mount_t             *mp,
270         void                    __user *arg,
271         struct file             *parfilp,
272         struct inode            *parinode)
273 {
274         int                     error;
275         int                     new_fd;
276         int                     permflag;
277         struct file             *filp;
278         struct inode            *inode;
279         struct dentry           *dentry;
280         bhv_vnode_t             *vp;
281         xfs_fsop_handlereq_t    hreq;
282
283         if (!capable(CAP_SYS_ADMIN))
284                 return -XFS_ERROR(EPERM);
285         if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
286                 return -XFS_ERROR(EFAULT);
287
288         error = xfs_vget_fsop_handlereq(mp, parinode, &hreq, &vp, &inode);
289         if (error)
290                 return -error;
291
292         /* Restrict xfs_open_by_handle to directories & regular files. */
293         if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode))) {
294                 iput(inode);
295                 return -XFS_ERROR(EINVAL);
296         }
297
298 #if BITS_PER_LONG != 32
299         hreq.oflags |= O_LARGEFILE;
300 #endif
301         /* Put open permission in namei format. */
302         permflag = hreq.oflags;
303         if ((permflag+1) & O_ACCMODE)
304                 permflag++;
305         if (permflag & O_TRUNC)
306                 permflag |= 2;
307
308         if ((!(permflag & O_APPEND) || (permflag & O_TRUNC)) &&
309             (permflag & FMODE_WRITE) && IS_APPEND(inode)) {
310                 iput(inode);
311                 return -XFS_ERROR(EPERM);
312         }
313
314         if ((permflag & FMODE_WRITE) && IS_IMMUTABLE(inode)) {
315                 iput(inode);
316                 return -XFS_ERROR(EACCES);
317         }
318
319         /* Can't write directories. */
320         if ( S_ISDIR(inode->i_mode) && (permflag & FMODE_WRITE)) {
321                 iput(inode);
322                 return -XFS_ERROR(EISDIR);
323         }
324
325         if ((new_fd = get_unused_fd()) < 0) {
326                 iput(inode);
327                 return new_fd;
328         }
329
330         dentry = d_alloc_anon(inode);
331         if (dentry == NULL) {
332                 iput(inode);
333                 put_unused_fd(new_fd);
334                 return -XFS_ERROR(ENOMEM);
335         }
336
337         /* Ensure umount returns EBUSY on umounts while this file is open. */
338         mntget(parfilp->f_vfsmnt);
339
340         /* Create file pointer. */
341         filp = dentry_open(dentry, parfilp->f_vfsmnt, hreq.oflags);
342         if (IS_ERR(filp)) {
343                 put_unused_fd(new_fd);
344                 return -XFS_ERROR(-PTR_ERR(filp));
345         }
346         if (inode->i_mode & S_IFREG)
347                 filp->f_op = &xfs_invis_file_operations;
348
349         fd_install(new_fd, filp);
350         return new_fd;
351 }
352
353 STATIC int
354 xfs_readlink_by_handle(
355         xfs_mount_t             *mp,
356         void                    __user *arg,
357         struct file             *parfilp,
358         struct inode            *parinode)
359 {
360         int                     error;
361         struct iovec            aiov;
362         struct uio              auio;
363         struct inode            *inode;
364         xfs_fsop_handlereq_t    hreq;
365         bhv_vnode_t             *vp;
366         __u32                   olen;
367
368         if (!capable(CAP_SYS_ADMIN))
369                 return -XFS_ERROR(EPERM);
370         if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
371                 return -XFS_ERROR(EFAULT);
372
373         error = xfs_vget_fsop_handlereq(mp, parinode, &hreq, &vp, &inode);
374         if (error)
375                 return -error;
376
377         /* Restrict this handle operation to symlinks only. */
378         if (!S_ISLNK(inode->i_mode)) {
379                 VN_RELE(vp);
380                 return -XFS_ERROR(EINVAL);
381         }
382
383         if (copy_from_user(&olen, hreq.ohandlen, sizeof(__u32))) {
384                 VN_RELE(vp);
385                 return -XFS_ERROR(EFAULT);
386         }
387         aiov.iov_len    = olen;
388         aiov.iov_base   = hreq.ohandle;
389
390         auio.uio_iov    = &aiov;
391         auio.uio_iovcnt = 1;
392         auio.uio_offset = 0;
393         auio.uio_segflg = UIO_USERSPACE;
394         auio.uio_resid  = olen;
395
396         error = bhv_vop_readlink(vp, &auio, IO_INVIS, NULL);
397         VN_RELE(vp);
398         if (error)
399                 return -error;
400
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         bhv_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         bhv_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         error = bhv_vop_attr_list(vp, kbuf, al_hreq.buflen, al_hreq.flags,
478                                         cursor, NULL);
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         bhv_vnode_t             *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         error = bhv_vop_attr_get(vp, name, kbuf, len, flags, NULL);
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         bhv_vnode_t             *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_RDONLY(&vp->v_inode))
534                 return -EROFS;
535         if (IS_IMMUTABLE(&vp->v_inode) || IS_APPEND(&vp->v_inode))
536                 return EPERM;
537         if (len > XATTR_SIZE_MAX)
538                 return EINVAL;
539
540         kbuf = kmalloc(len, GFP_KERNEL);
541         if (!kbuf)
542                 return ENOMEM;
543
544         if (copy_from_user(kbuf, ubuf, len))
545                 goto out_kfree;
546                         
547         error = bhv_vop_attr_set(vp, name, kbuf, len, flags, NULL);
548
549  out_kfree:
550         kfree(kbuf);
551         return error;
552 }
553
554 STATIC int
555 xfs_attrmulti_attr_remove(
556         bhv_vnode_t             *vp,
557         char                    *name,
558         __uint32_t              flags)
559 {
560         if (IS_RDONLY(&vp->v_inode))
561                 return -EROFS;
562         if (IS_IMMUTABLE(&vp->v_inode) || IS_APPEND(&vp->v_inode))
563                 return EPERM;
564         return bhv_vop_attr_remove(vp, name, flags, NULL);
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         bhv_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         bhv_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         bhv_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         bhv_vnode_t             *vp;
712         xfs_inode_t             *ip;
713         xfs_mount_t             *mp;
714
715         vp = vn_from_inode(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->bt_sshift;
749                 da.d_maxiosz = INT_MAX & ~(da.d_miniosz - 1);
750
751                 if (copy_to_user(arg, &da, sizeof(da)))
752                         return -XFS_ERROR(EFAULT);
753                 return 0;
754         }
755
756         case XFS_IOC_FSBULKSTAT_SINGLE:
757         case XFS_IOC_FSBULKSTAT:
758         case XFS_IOC_FSINUMBERS:
759                 return xfs_ioc_bulkstat(mp, cmd, arg);
760
761         case XFS_IOC_FSGEOMETRY_V1:
762                 return xfs_ioc_fsgeometry_v1(mp, arg);
763
764         case XFS_IOC_FSGEOMETRY:
765                 return xfs_ioc_fsgeometry(mp, arg);
766
767         case XFS_IOC_GETVERSION:
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         bhv_vnode_t             *vp,
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 (vp->v_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 (!VN_ISREG(vp))
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         case XFS_IOC_GETVERSION: {
1270                 flags = vn_to_inode(vp)->i_generation;
1271                 if (copy_to_user(arg, &flags, sizeof(flags)))
1272                         error = -EFAULT;
1273                 break;
1274         }
1275
1276         default:
1277                 error = -ENOTTY;
1278                 break;
1279         }
1280
1281         kfree(vattr);
1282         return error;
1283 }
1284
1285 STATIC int
1286 xfs_ioc_getbmap(
1287         bhv_desc_t              *bdp,
1288         struct file             *filp,
1289         int                     ioflags,
1290         unsigned int            cmd,
1291         void                    __user *arg)
1292 {
1293         struct getbmap          bm;
1294         int                     iflags;
1295         int                     error;
1296
1297         if (copy_from_user(&bm, arg, sizeof(bm)))
1298                 return -XFS_ERROR(EFAULT);
1299
1300         if (bm.bmv_count < 2)
1301                 return -XFS_ERROR(EINVAL);
1302
1303         iflags = (cmd == XFS_IOC_GETBMAPA ? BMV_IF_ATTRFORK : 0);
1304         if (ioflags & IO_INVIS)
1305                 iflags |= BMV_IF_NO_DMAPI_READ;
1306
1307         error = xfs_getbmap(bdp, &bm, (struct getbmap __user *)arg+1, iflags);
1308         if (error)
1309                 return -error;
1310
1311         if (copy_to_user(arg, &bm, sizeof(bm)))
1312                 return -XFS_ERROR(EFAULT);
1313         return 0;
1314 }
1315
1316 STATIC int
1317 xfs_ioc_getbmapx(
1318         bhv_desc_t              *bdp,
1319         void                    __user *arg)
1320 {
1321         struct getbmapx         bmx;
1322         struct getbmap          bm;
1323         int                     iflags;
1324         int                     error;
1325
1326         if (copy_from_user(&bmx, arg, sizeof(bmx)))
1327                 return -XFS_ERROR(EFAULT);
1328
1329         if (bmx.bmv_count < 2)
1330                 return -XFS_ERROR(EINVAL);
1331
1332         /*
1333          * Map input getbmapx structure to a getbmap
1334          * structure for xfs_getbmap.
1335          */
1336         GETBMAP_CONVERT(bmx, bm);
1337
1338         iflags = bmx.bmv_iflags;
1339
1340         if (iflags & (~BMV_IF_VALID))
1341                 return -XFS_ERROR(EINVAL);
1342
1343         iflags |= BMV_IF_EXTENDED;
1344
1345         error = xfs_getbmap(bdp, &bm, (struct getbmapx __user *)arg+1, iflags);
1346         if (error)
1347                 return -error;
1348
1349         GETBMAP_CONVERT(bm, bmx);
1350
1351         if (copy_to_user(arg, &bmx, sizeof(bmx)))
1352                 return -XFS_ERROR(EFAULT);
1353
1354         return 0;
1355 }