[XFS] add helper to get xfs_inode from vnode
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / xfs / linux-2.6 / xfs_ioctl.c
blobb3b2cfda273ccbe625d79cfacf1c382a50a0c51e
1 /*
2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
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.
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.
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
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"
55 #include <linux/dcache.h>
56 #include <linux/mount.h>
57 #include <linux/namei.h>
58 #include <linux/pagemap.h>
61 * xfs_find_handle maps from userspace xfs_fsop_handlereq structure to
62 * a file or fs handle.
64 * XFS_IOC_PATH_TO_FSHANDLE
65 * returns fs handle for a mount point or path within that mount point
66 * XFS_IOC_FD_TO_HANDLE
67 * returns full handle for a FD opened in user space
68 * XFS_IOC_PATH_TO_HANDLE
69 * returns full handle for a path
71 STATIC int
72 xfs_find_handle(
73 unsigned int cmd,
74 void __user *arg)
76 int hsize;
77 xfs_handle_t handle;
78 xfs_fsop_handlereq_t hreq;
79 struct inode *inode;
80 struct vnode *vp;
82 if (copy_from_user(&hreq, arg, sizeof(hreq)))
83 return -XFS_ERROR(EFAULT);
85 memset((char *)&handle, 0, sizeof(handle));
87 switch (cmd) {
88 case XFS_IOC_PATH_TO_FSHANDLE:
89 case XFS_IOC_PATH_TO_HANDLE: {
90 struct nameidata nd;
91 int error;
93 error = user_path_walk_link((const char __user *)hreq.path, &nd);
94 if (error)
95 return error;
97 ASSERT(nd.dentry);
98 ASSERT(nd.dentry->d_inode);
99 inode = igrab(nd.dentry->d_inode);
100 path_release(&nd);
101 break;
104 case XFS_IOC_FD_TO_HANDLE: {
105 struct file *file;
107 file = fget(hreq.fd);
108 if (!file)
109 return -EBADF;
111 ASSERT(file->f_dentry);
112 ASSERT(file->f_dentry->d_inode);
113 inode = igrab(file->f_dentry->d_inode);
114 fput(file);
115 break;
118 default:
119 ASSERT(0);
120 return -XFS_ERROR(EINVAL);
123 if (inode->i_sb->s_magic != XFS_SB_MAGIC) {
124 /* we're not in XFS anymore, Toto */
125 iput(inode);
126 return -XFS_ERROR(EINVAL);
129 switch (inode->i_mode & S_IFMT) {
130 case S_IFREG:
131 case S_IFDIR:
132 case S_IFLNK:
133 break;
134 default:
135 iput(inode);
136 return -XFS_ERROR(EBADF);
139 /* we need the vnode */
140 vp = LINVFS_GET_VP(inode);
142 /* now we can grab the fsid */
143 memcpy(&handle.ha_fsid, vp->v_vfsp->vfs_altfsid, sizeof(xfs_fsid_t));
144 hsize = sizeof(xfs_fsid_t);
146 if (cmd != XFS_IOC_PATH_TO_FSHANDLE) {
147 xfs_inode_t *ip;
148 int lock_mode;
150 /* need to get access to the xfs_inode to read the generation */
151 ip = xfs_vtoi(vp);
152 ASSERT(ip);
153 lock_mode = xfs_ilock_map_shared(ip);
155 /* fill in fid section of handle from inode */
156 handle.ha_fid.xfs_fid_len = sizeof(xfs_fid_t) -
157 sizeof(handle.ha_fid.xfs_fid_len);
158 handle.ha_fid.xfs_fid_pad = 0;
159 handle.ha_fid.xfs_fid_gen = ip->i_d.di_gen;
160 handle.ha_fid.xfs_fid_ino = ip->i_ino;
162 xfs_iunlock_map_shared(ip, lock_mode);
164 hsize = XFS_HSIZE(handle);
167 /* now copy our handle into the user buffer & write out the size */
168 if (copy_to_user(hreq.ohandle, &handle, hsize) ||
169 copy_to_user(hreq.ohandlen, &hsize, sizeof(__s32))) {
170 iput(inode);
171 return -XFS_ERROR(EFAULT);
174 iput(inode);
175 return 0;
180 * Convert userspace handle data into vnode (and inode).
181 * We [ab]use the fact that all the fsop_handlereq ioctl calls
182 * have a data structure argument whose first component is always
183 * a xfs_fsop_handlereq_t, so we can cast to and from this type.
184 * This allows us to optimise the copy_from_user calls and gives
185 * a handy, shared routine.
187 * If no error, caller must always VN_RELE the returned vp.
189 STATIC int
190 xfs_vget_fsop_handlereq(
191 xfs_mount_t *mp,
192 struct inode *parinode, /* parent inode pointer */
193 xfs_fsop_handlereq_t *hreq,
194 vnode_t **vp,
195 struct inode **inode)
197 void __user *hanp;
198 size_t hlen;
199 xfs_fid_t *xfid;
200 xfs_handle_t *handlep;
201 xfs_handle_t handle;
202 xfs_inode_t *ip;
203 struct inode *inodep;
204 vnode_t *vpp;
205 xfs_ino_t ino;
206 __u32 igen;
207 int error;
210 * Only allow handle opens under a directory.
212 if (!S_ISDIR(parinode->i_mode))
213 return XFS_ERROR(ENOTDIR);
215 hanp = hreq->ihandle;
216 hlen = hreq->ihandlen;
217 handlep = &handle;
219 if (hlen < sizeof(handlep->ha_fsid) || hlen > sizeof(*handlep))
220 return XFS_ERROR(EINVAL);
221 if (copy_from_user(handlep, hanp, hlen))
222 return XFS_ERROR(EFAULT);
223 if (hlen < sizeof(*handlep))
224 memset(((char *)handlep) + hlen, 0, sizeof(*handlep) - hlen);
225 if (hlen > sizeof(handlep->ha_fsid)) {
226 if (handlep->ha_fid.xfs_fid_len !=
227 (hlen - sizeof(handlep->ha_fsid)
228 - sizeof(handlep->ha_fid.xfs_fid_len))
229 || handlep->ha_fid.xfs_fid_pad)
230 return XFS_ERROR(EINVAL);
234 * Crack the handle, obtain the inode # & generation #
236 xfid = (struct xfs_fid *)&handlep->ha_fid;
237 if (xfid->xfs_fid_len == sizeof(*xfid) - sizeof(xfid->xfs_fid_len)) {
238 ino = xfid->xfs_fid_ino;
239 igen = xfid->xfs_fid_gen;
240 } else {
241 return XFS_ERROR(EINVAL);
245 * Get the XFS inode, building a vnode to go with it.
247 error = xfs_iget(mp, NULL, ino, 0, XFS_ILOCK_SHARED, &ip, 0);
248 if (error)
249 return error;
250 if (ip == NULL)
251 return XFS_ERROR(EIO);
252 if (ip->i_d.di_mode == 0 || ip->i_d.di_gen != igen) {
253 xfs_iput_new(ip, XFS_ILOCK_SHARED);
254 return XFS_ERROR(ENOENT);
257 vpp = XFS_ITOV(ip);
258 inodep = LINVFS_GET_IP(vpp);
259 xfs_iunlock(ip, XFS_ILOCK_SHARED);
261 *vp = vpp;
262 *inode = inodep;
263 return 0;
266 STATIC int
267 xfs_open_by_handle(
268 xfs_mount_t *mp,
269 void __user *arg,
270 struct file *parfilp,
271 struct inode *parinode)
273 int error;
274 int new_fd;
275 int permflag;
276 struct file *filp;
277 struct inode *inode;
278 struct dentry *dentry;
279 vnode_t *vp;
280 xfs_fsop_handlereq_t hreq;
282 if (!capable(CAP_SYS_ADMIN))
283 return -XFS_ERROR(EPERM);
284 if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
285 return -XFS_ERROR(EFAULT);
287 error = xfs_vget_fsop_handlereq(mp, parinode, &hreq, &vp, &inode);
288 if (error)
289 return -error;
291 /* Restrict xfs_open_by_handle to directories & regular files. */
292 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode))) {
293 iput(inode);
294 return -XFS_ERROR(EINVAL);
297 #if BITS_PER_LONG != 32
298 hreq.oflags |= O_LARGEFILE;
299 #endif
300 /* Put open permission in namei format. */
301 permflag = hreq.oflags;
302 if ((permflag+1) & O_ACCMODE)
303 permflag++;
304 if (permflag & O_TRUNC)
305 permflag |= 2;
307 if ((!(permflag & O_APPEND) || (permflag & O_TRUNC)) &&
308 (permflag & FMODE_WRITE) && IS_APPEND(inode)) {
309 iput(inode);
310 return -XFS_ERROR(EPERM);
313 if ((permflag & FMODE_WRITE) && IS_IMMUTABLE(inode)) {
314 iput(inode);
315 return -XFS_ERROR(EACCES);
318 /* Can't write directories. */
319 if ( S_ISDIR(inode->i_mode) && (permflag & FMODE_WRITE)) {
320 iput(inode);
321 return -XFS_ERROR(EISDIR);
324 if ((new_fd = get_unused_fd()) < 0) {
325 iput(inode);
326 return new_fd;
329 dentry = d_alloc_anon(inode);
330 if (dentry == NULL) {
331 iput(inode);
332 put_unused_fd(new_fd);
333 return -XFS_ERROR(ENOMEM);
336 /* Ensure umount returns EBUSY on umounts while this file is open. */
337 mntget(parfilp->f_vfsmnt);
339 /* Create file pointer. */
340 filp = dentry_open(dentry, parfilp->f_vfsmnt, hreq.oflags);
341 if (IS_ERR(filp)) {
342 put_unused_fd(new_fd);
343 return -XFS_ERROR(-PTR_ERR(filp));
345 if (inode->i_mode & S_IFREG)
346 filp->f_op = &linvfs_invis_file_operations;
348 fd_install(new_fd, filp);
349 return new_fd;
352 STATIC int
353 xfs_readlink_by_handle(
354 xfs_mount_t *mp,
355 void __user *arg,
356 struct file *parfilp,
357 struct inode *parinode)
359 int error;
360 struct iovec aiov;
361 struct uio auio;
362 struct inode *inode;
363 xfs_fsop_handlereq_t hreq;
364 vnode_t *vp;
365 __u32 olen;
367 if (!capable(CAP_SYS_ADMIN))
368 return -XFS_ERROR(EPERM);
369 if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
370 return -XFS_ERROR(EFAULT);
372 error = xfs_vget_fsop_handlereq(mp, parinode, &hreq, &vp, &inode);
373 if (error)
374 return -error;
376 /* Restrict this handle operation to symlinks only. */
377 if (!S_ISLNK(inode->i_mode)) {
378 VN_RELE(vp);
379 return -XFS_ERROR(EINVAL);
382 if (copy_from_user(&olen, hreq.ohandlen, sizeof(__u32))) {
383 VN_RELE(vp);
384 return -XFS_ERROR(EFAULT);
386 aiov.iov_len = olen;
387 aiov.iov_base = hreq.ohandle;
389 auio.uio_iov = &aiov;
390 auio.uio_iovcnt = 1;
391 auio.uio_offset = 0;
392 auio.uio_segflg = UIO_USERSPACE;
393 auio.uio_resid = olen;
395 VOP_READLINK(vp, &auio, IO_INVIS, NULL, error);
397 VN_RELE(vp);
398 return (olen - auio.uio_resid);
401 STATIC int
402 xfs_fssetdm_by_handle(
403 xfs_mount_t *mp,
404 void __user *arg,
405 struct file *parfilp,
406 struct inode *parinode)
408 int error;
409 struct fsdmidata fsd;
410 xfs_fsop_setdm_handlereq_t dmhreq;
411 struct inode *inode;
412 bhv_desc_t *bdp;
413 vnode_t *vp;
415 if (!capable(CAP_MKNOD))
416 return -XFS_ERROR(EPERM);
417 if (copy_from_user(&dmhreq, arg, sizeof(xfs_fsop_setdm_handlereq_t)))
418 return -XFS_ERROR(EFAULT);
420 error = xfs_vget_fsop_handlereq(mp, parinode, &dmhreq.hreq, &vp, &inode);
421 if (error)
422 return -error;
424 if (IS_IMMUTABLE(inode) || IS_APPEND(inode)) {
425 VN_RELE(vp);
426 return -XFS_ERROR(EPERM);
429 if (copy_from_user(&fsd, dmhreq.data, sizeof(fsd))) {
430 VN_RELE(vp);
431 return -XFS_ERROR(EFAULT);
434 bdp = bhv_base_unlocked(VN_BHV_HEAD(vp));
435 error = xfs_set_dmattrs(bdp, fsd.fsd_dmevmask, fsd.fsd_dmstate, NULL);
437 VN_RELE(vp);
438 if (error)
439 return -error;
440 return 0;
443 STATIC int
444 xfs_attrlist_by_handle(
445 xfs_mount_t *mp,
446 void __user *arg,
447 struct file *parfilp,
448 struct inode *parinode)
450 int error;
451 attrlist_cursor_kern_t *cursor;
452 xfs_fsop_attrlist_handlereq_t al_hreq;
453 struct inode *inode;
454 vnode_t *vp;
455 char *kbuf;
457 if (!capable(CAP_SYS_ADMIN))
458 return -XFS_ERROR(EPERM);
459 if (copy_from_user(&al_hreq, arg, sizeof(xfs_fsop_attrlist_handlereq_t)))
460 return -XFS_ERROR(EFAULT);
461 if (al_hreq.buflen > XATTR_LIST_MAX)
462 return -XFS_ERROR(EINVAL);
464 error = xfs_vget_fsop_handlereq(mp, parinode, &al_hreq.hreq,
465 &vp, &inode);
466 if (error)
467 goto out;
469 kbuf = kmalloc(al_hreq.buflen, GFP_KERNEL);
470 if (!kbuf)
471 goto out_vn_rele;
473 cursor = (attrlist_cursor_kern_t *)&al_hreq.pos;
474 VOP_ATTR_LIST(vp, kbuf, al_hreq.buflen, al_hreq.flags,
475 cursor, NULL, error);
476 if (error)
477 goto out_kfree;
479 if (copy_to_user(al_hreq.buffer, kbuf, al_hreq.buflen))
480 error = -EFAULT;
482 out_kfree:
483 kfree(kbuf);
484 out_vn_rele:
485 VN_RELE(vp);
486 out:
487 return -error;
490 STATIC int
491 xfs_attrmulti_attr_get(
492 struct vnode *vp,
493 char *name,
494 char __user *ubuf,
495 __uint32_t *len,
496 __uint32_t flags)
498 char *kbuf;
499 int error = EFAULT;
501 if (*len > XATTR_SIZE_MAX)
502 return EINVAL;
503 kbuf = kmalloc(*len, GFP_KERNEL);
504 if (!kbuf)
505 return ENOMEM;
507 VOP_ATTR_GET(vp, name, kbuf, len, flags, NULL, error);
508 if (error)
509 goto out_kfree;
511 if (copy_to_user(ubuf, kbuf, *len))
512 error = EFAULT;
514 out_kfree:
515 kfree(kbuf);
516 return error;
519 STATIC int
520 xfs_attrmulti_attr_set(
521 struct vnode *vp,
522 char *name,
523 const char __user *ubuf,
524 __uint32_t len,
525 __uint32_t flags)
527 char *kbuf;
528 int error = EFAULT;
530 if (IS_RDONLY(&vp->v_inode))
531 return -EROFS;
532 if (IS_IMMUTABLE(&vp->v_inode) || IS_APPEND(&vp->v_inode))
533 return EPERM;
534 if (len > XATTR_SIZE_MAX)
535 return EINVAL;
537 kbuf = kmalloc(len, GFP_KERNEL);
538 if (!kbuf)
539 return ENOMEM;
541 if (copy_from_user(kbuf, ubuf, len))
542 goto out_kfree;
544 VOP_ATTR_SET(vp, name, kbuf, len, flags, NULL, error);
546 out_kfree:
547 kfree(kbuf);
548 return error;
551 STATIC int
552 xfs_attrmulti_attr_remove(
553 struct vnode *vp,
554 char *name,
555 __uint32_t flags)
557 int error;
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;
565 VOP_ATTR_REMOVE(vp, name, flags, NULL, error);
566 return error;
569 STATIC int
570 xfs_attrmulti_by_handle(
571 xfs_mount_t *mp,
572 void __user *arg,
573 struct file *parfilp,
574 struct inode *parinode)
576 int error;
577 xfs_attr_multiop_t *ops;
578 xfs_fsop_attrmulti_handlereq_t am_hreq;
579 struct inode *inode;
580 vnode_t *vp;
581 unsigned int i, size;
582 char *attr_name;
584 if (!capable(CAP_SYS_ADMIN))
585 return -XFS_ERROR(EPERM);
586 if (copy_from_user(&am_hreq, arg, sizeof(xfs_fsop_attrmulti_handlereq_t)))
587 return -XFS_ERROR(EFAULT);
589 error = xfs_vget_fsop_handlereq(mp, parinode, &am_hreq.hreq, &vp, &inode);
590 if (error)
591 goto out;
593 error = E2BIG;
594 size = am_hreq.opcount * sizeof(attr_multiop_t);
595 if (!size || size > 16 * PAGE_SIZE)
596 goto out_vn_rele;
598 error = ENOMEM;
599 ops = kmalloc(size, GFP_KERNEL);
600 if (!ops)
601 goto out_vn_rele;
603 error = EFAULT;
604 if (copy_from_user(ops, am_hreq.ops, size))
605 goto out_kfree_ops;
607 attr_name = kmalloc(MAXNAMELEN, GFP_KERNEL);
608 if (!attr_name)
609 goto out_kfree_ops;
612 error = 0;
613 for (i = 0; i < am_hreq.opcount; i++) {
614 ops[i].am_error = strncpy_from_user(attr_name,
615 ops[i].am_attrname, MAXNAMELEN);
616 if (ops[i].am_error == 0 || ops[i].am_error == MAXNAMELEN)
617 error = -ERANGE;
618 if (ops[i].am_error < 0)
619 break;
621 switch (ops[i].am_opcode) {
622 case ATTR_OP_GET:
623 ops[i].am_error = xfs_attrmulti_attr_get(vp,
624 attr_name, ops[i].am_attrvalue,
625 &ops[i].am_length, ops[i].am_flags);
626 break;
627 case ATTR_OP_SET:
628 ops[i].am_error = xfs_attrmulti_attr_set(vp,
629 attr_name, ops[i].am_attrvalue,
630 ops[i].am_length, ops[i].am_flags);
631 break;
632 case ATTR_OP_REMOVE:
633 ops[i].am_error = xfs_attrmulti_attr_remove(vp,
634 attr_name, ops[i].am_flags);
635 break;
636 default:
637 ops[i].am_error = EINVAL;
641 if (copy_to_user(am_hreq.ops, ops, size))
642 error = XFS_ERROR(EFAULT);
644 kfree(attr_name);
645 out_kfree_ops:
646 kfree(ops);
647 out_vn_rele:
648 VN_RELE(vp);
649 out:
650 return -error;
653 /* prototypes for a few of the stack-hungry cases that have
654 * their own functions. Functions are defined after their use
655 * so gcc doesn't get fancy and inline them with -03 */
657 STATIC int
658 xfs_ioc_space(
659 bhv_desc_t *bdp,
660 vnode_t *vp,
661 struct file *filp,
662 int flags,
663 unsigned int cmd,
664 void __user *arg);
666 STATIC int
667 xfs_ioc_bulkstat(
668 xfs_mount_t *mp,
669 unsigned int cmd,
670 void __user *arg);
672 STATIC int
673 xfs_ioc_fsgeometry_v1(
674 xfs_mount_t *mp,
675 void __user *arg);
677 STATIC int
678 xfs_ioc_fsgeometry(
679 xfs_mount_t *mp,
680 void __user *arg);
682 STATIC int
683 xfs_ioc_xattr(
684 vnode_t *vp,
685 xfs_inode_t *ip,
686 struct file *filp,
687 unsigned int cmd,
688 void __user *arg);
690 STATIC int
691 xfs_ioc_getbmap(
692 bhv_desc_t *bdp,
693 struct file *filp,
694 int flags,
695 unsigned int cmd,
696 void __user *arg);
698 STATIC int
699 xfs_ioc_getbmapx(
700 bhv_desc_t *bdp,
701 void __user *arg);
704 xfs_ioctl(
705 bhv_desc_t *bdp,
706 struct inode *inode,
707 struct file *filp,
708 int ioflags,
709 unsigned int cmd,
710 void __user *arg)
712 int error;
713 vnode_t *vp;
714 xfs_inode_t *ip;
715 xfs_mount_t *mp;
717 vp = LINVFS_GET_VP(inode);
719 vn_trace_entry(vp, "xfs_ioctl", (inst_t *)__return_address);
721 ip = XFS_BHVTOI(bdp);
722 mp = ip->i_mount;
724 switch (cmd) {
726 case XFS_IOC_ALLOCSP:
727 case XFS_IOC_FREESP:
728 case XFS_IOC_RESVSP:
729 case XFS_IOC_UNRESVSP:
730 case XFS_IOC_ALLOCSP64:
731 case XFS_IOC_FREESP64:
732 case XFS_IOC_RESVSP64:
733 case XFS_IOC_UNRESVSP64:
735 * Only allow the sys admin to reserve space unless
736 * unwritten extents are enabled.
738 if (!XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb) &&
739 !capable(CAP_SYS_ADMIN))
740 return -EPERM;
742 return xfs_ioc_space(bdp, vp, filp, ioflags, cmd, arg);
744 case XFS_IOC_DIOINFO: {
745 struct dioattr da;
746 xfs_buftarg_t *target =
747 (ip->i_d.di_flags & XFS_DIFLAG_REALTIME) ?
748 mp->m_rtdev_targp : mp->m_ddev_targp;
750 da.d_mem = da.d_miniosz = 1 << target->bt_sshift;
751 da.d_maxiosz = INT_MAX & ~(da.d_miniosz - 1);
753 if (copy_to_user(arg, &da, sizeof(da)))
754 return -XFS_ERROR(EFAULT);
755 return 0;
758 case XFS_IOC_FSBULKSTAT_SINGLE:
759 case XFS_IOC_FSBULKSTAT:
760 case XFS_IOC_FSINUMBERS:
761 return xfs_ioc_bulkstat(mp, cmd, arg);
763 case XFS_IOC_FSGEOMETRY_V1:
764 return xfs_ioc_fsgeometry_v1(mp, arg);
766 case XFS_IOC_FSGEOMETRY:
767 return xfs_ioc_fsgeometry(mp, arg);
769 case XFS_IOC_GETVERSION:
770 case XFS_IOC_GETXFLAGS:
771 case XFS_IOC_SETXFLAGS:
772 case XFS_IOC_FSGETXATTR:
773 case XFS_IOC_FSSETXATTR:
774 case XFS_IOC_FSGETXATTRA:
775 return xfs_ioc_xattr(vp, ip, filp, cmd, arg);
777 case XFS_IOC_FSSETDM: {
778 struct fsdmidata dmi;
780 if (copy_from_user(&dmi, arg, sizeof(dmi)))
781 return -XFS_ERROR(EFAULT);
783 error = xfs_set_dmattrs(bdp, dmi.fsd_dmevmask, dmi.fsd_dmstate,
784 NULL);
785 return -error;
788 case XFS_IOC_GETBMAP:
789 case XFS_IOC_GETBMAPA:
790 return xfs_ioc_getbmap(bdp, filp, ioflags, cmd, arg);
792 case XFS_IOC_GETBMAPX:
793 return xfs_ioc_getbmapx(bdp, arg);
795 case XFS_IOC_FD_TO_HANDLE:
796 case XFS_IOC_PATH_TO_HANDLE:
797 case XFS_IOC_PATH_TO_FSHANDLE:
798 return xfs_find_handle(cmd, arg);
800 case XFS_IOC_OPEN_BY_HANDLE:
801 return xfs_open_by_handle(mp, arg, filp, inode);
803 case XFS_IOC_FSSETDM_BY_HANDLE:
804 return xfs_fssetdm_by_handle(mp, arg, filp, inode);
806 case XFS_IOC_READLINK_BY_HANDLE:
807 return xfs_readlink_by_handle(mp, arg, filp, inode);
809 case XFS_IOC_ATTRLIST_BY_HANDLE:
810 return xfs_attrlist_by_handle(mp, arg, filp, inode);
812 case XFS_IOC_ATTRMULTI_BY_HANDLE:
813 return xfs_attrmulti_by_handle(mp, arg, filp, inode);
815 case XFS_IOC_SWAPEXT: {
816 error = xfs_swapext((struct xfs_swapext __user *)arg);
817 return -error;
820 case XFS_IOC_FSCOUNTS: {
821 xfs_fsop_counts_t out;
823 error = xfs_fs_counts(mp, &out);
824 if (error)
825 return -error;
827 if (copy_to_user(arg, &out, sizeof(out)))
828 return -XFS_ERROR(EFAULT);
829 return 0;
832 case XFS_IOC_SET_RESBLKS: {
833 xfs_fsop_resblks_t inout;
834 __uint64_t in;
836 if (!capable(CAP_SYS_ADMIN))
837 return -EPERM;
839 if (copy_from_user(&inout, arg, sizeof(inout)))
840 return -XFS_ERROR(EFAULT);
842 /* input parameter is passed in resblks field of structure */
843 in = inout.resblks;
844 error = xfs_reserve_blocks(mp, &in, &inout);
845 if (error)
846 return -error;
848 if (copy_to_user(arg, &inout, sizeof(inout)))
849 return -XFS_ERROR(EFAULT);
850 return 0;
853 case XFS_IOC_GET_RESBLKS: {
854 xfs_fsop_resblks_t out;
856 if (!capable(CAP_SYS_ADMIN))
857 return -EPERM;
859 error = xfs_reserve_blocks(mp, NULL, &out);
860 if (error)
861 return -error;
863 if (copy_to_user(arg, &out, sizeof(out)))
864 return -XFS_ERROR(EFAULT);
866 return 0;
869 case XFS_IOC_FSGROWFSDATA: {
870 xfs_growfs_data_t in;
872 if (!capable(CAP_SYS_ADMIN))
873 return -EPERM;
875 if (copy_from_user(&in, arg, sizeof(in)))
876 return -XFS_ERROR(EFAULT);
878 error = xfs_growfs_data(mp, &in);
879 return -error;
882 case XFS_IOC_FSGROWFSLOG: {
883 xfs_growfs_log_t in;
885 if (!capable(CAP_SYS_ADMIN))
886 return -EPERM;
888 if (copy_from_user(&in, arg, sizeof(in)))
889 return -XFS_ERROR(EFAULT);
891 error = xfs_growfs_log(mp, &in);
892 return -error;
895 case XFS_IOC_FSGROWFSRT: {
896 xfs_growfs_rt_t in;
898 if (!capable(CAP_SYS_ADMIN))
899 return -EPERM;
901 if (copy_from_user(&in, arg, sizeof(in)))
902 return -XFS_ERROR(EFAULT);
904 error = xfs_growfs_rt(mp, &in);
905 return -error;
908 case XFS_IOC_FREEZE:
909 if (!capable(CAP_SYS_ADMIN))
910 return -EPERM;
912 if (inode->i_sb->s_frozen == SB_UNFROZEN)
913 freeze_bdev(inode->i_sb->s_bdev);
914 return 0;
916 case XFS_IOC_THAW:
917 if (!capable(CAP_SYS_ADMIN))
918 return -EPERM;
919 if (inode->i_sb->s_frozen != SB_UNFROZEN)
920 thaw_bdev(inode->i_sb->s_bdev, inode->i_sb);
921 return 0;
923 case XFS_IOC_GOINGDOWN: {
924 __uint32_t in;
926 if (!capable(CAP_SYS_ADMIN))
927 return -EPERM;
929 if (get_user(in, (__uint32_t __user *)arg))
930 return -XFS_ERROR(EFAULT);
932 error = xfs_fs_goingdown(mp, in);
933 return -error;
936 case XFS_IOC_ERROR_INJECTION: {
937 xfs_error_injection_t in;
939 if (!capable(CAP_SYS_ADMIN))
940 return -EPERM;
942 if (copy_from_user(&in, arg, sizeof(in)))
943 return -XFS_ERROR(EFAULT);
945 error = xfs_errortag_add(in.errtag, mp);
946 return -error;
949 case XFS_IOC_ERROR_CLEARALL:
950 if (!capable(CAP_SYS_ADMIN))
951 return -EPERM;
953 error = xfs_errortag_clearall(mp);
954 return -error;
956 default:
957 return -ENOTTY;
961 STATIC int
962 xfs_ioc_space(
963 bhv_desc_t *bdp,
964 vnode_t *vp,
965 struct file *filp,
966 int ioflags,
967 unsigned int cmd,
968 void __user *arg)
970 xfs_flock64_t bf;
971 int attr_flags = 0;
972 int error;
974 if (vp->v_inode.i_flags & (S_IMMUTABLE|S_APPEND))
975 return -XFS_ERROR(EPERM);
977 if (!(filp->f_mode & FMODE_WRITE))
978 return -XFS_ERROR(EBADF);
980 if (!VN_ISREG(vp))
981 return -XFS_ERROR(EINVAL);
983 if (copy_from_user(&bf, arg, sizeof(bf)))
984 return -XFS_ERROR(EFAULT);
986 if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
987 attr_flags |= ATTR_NONBLOCK;
988 if (ioflags & IO_INVIS)
989 attr_flags |= ATTR_DMI;
991 error = xfs_change_file_space(bdp, cmd, &bf, filp->f_pos,
992 NULL, attr_flags);
993 return -error;
996 STATIC int
997 xfs_ioc_bulkstat(
998 xfs_mount_t *mp,
999 unsigned int cmd,
1000 void __user *arg)
1002 xfs_fsop_bulkreq_t bulkreq;
1003 int count; /* # of records returned */
1004 xfs_ino_t inlast; /* last inode number */
1005 int done;
1006 int error;
1008 /* done = 1 if there are more stats to get and if bulkstat */
1009 /* should be called again (unused here, but used in dmapi) */
1011 if (!capable(CAP_SYS_ADMIN))
1012 return -EPERM;
1014 if (XFS_FORCED_SHUTDOWN(mp))
1015 return -XFS_ERROR(EIO);
1017 if (copy_from_user(&bulkreq, arg, sizeof(xfs_fsop_bulkreq_t)))
1018 return -XFS_ERROR(EFAULT);
1020 if (copy_from_user(&inlast, bulkreq.lastip, sizeof(__s64)))
1021 return -XFS_ERROR(EFAULT);
1023 if ((count = bulkreq.icount) <= 0)
1024 return -XFS_ERROR(EINVAL);
1026 if (cmd == XFS_IOC_FSINUMBERS)
1027 error = xfs_inumbers(mp, &inlast, &count,
1028 bulkreq.ubuffer);
1029 else if (cmd == XFS_IOC_FSBULKSTAT_SINGLE)
1030 error = xfs_bulkstat_single(mp, &inlast,
1031 bulkreq.ubuffer, &done);
1032 else { /* XFS_IOC_FSBULKSTAT */
1033 if (count == 1 && inlast != 0) {
1034 inlast++;
1035 error = xfs_bulkstat_single(mp, &inlast,
1036 bulkreq.ubuffer, &done);
1037 } else {
1038 error = xfs_bulkstat(mp, &inlast, &count,
1039 (bulkstat_one_pf)xfs_bulkstat_one, NULL,
1040 sizeof(xfs_bstat_t), bulkreq.ubuffer,
1041 BULKSTAT_FG_QUICK, &done);
1045 if (error)
1046 return -error;
1048 if (bulkreq.ocount != NULL) {
1049 if (copy_to_user(bulkreq.lastip, &inlast,
1050 sizeof(xfs_ino_t)))
1051 return -XFS_ERROR(EFAULT);
1053 if (copy_to_user(bulkreq.ocount, &count, sizeof(count)))
1054 return -XFS_ERROR(EFAULT);
1057 return 0;
1060 STATIC int
1061 xfs_ioc_fsgeometry_v1(
1062 xfs_mount_t *mp,
1063 void __user *arg)
1065 xfs_fsop_geom_v1_t fsgeo;
1066 int error;
1068 error = xfs_fs_geometry(mp, (xfs_fsop_geom_t *)&fsgeo, 3);
1069 if (error)
1070 return -error;
1072 if (copy_to_user(arg, &fsgeo, sizeof(fsgeo)))
1073 return -XFS_ERROR(EFAULT);
1074 return 0;
1077 STATIC int
1078 xfs_ioc_fsgeometry(
1079 xfs_mount_t *mp,
1080 void __user *arg)
1082 xfs_fsop_geom_t fsgeo;
1083 int error;
1085 error = xfs_fs_geometry(mp, &fsgeo, 4);
1086 if (error)
1087 return -error;
1089 if (copy_to_user(arg, &fsgeo, sizeof(fsgeo)))
1090 return -XFS_ERROR(EFAULT);
1091 return 0;
1095 * Linux extended inode flags interface.
1097 #define LINUX_XFLAG_SYNC 0x00000008 /* Synchronous updates */
1098 #define LINUX_XFLAG_IMMUTABLE 0x00000010 /* Immutable file */
1099 #define LINUX_XFLAG_APPEND 0x00000020 /* writes to file may only append */
1100 #define LINUX_XFLAG_NODUMP 0x00000040 /* do not dump file */
1101 #define LINUX_XFLAG_NOATIME 0x00000080 /* do not update atime */
1103 STATIC unsigned int
1104 xfs_merge_ioc_xflags(
1105 unsigned int flags,
1106 unsigned int start)
1108 unsigned int xflags = start;
1110 if (flags & LINUX_XFLAG_IMMUTABLE)
1111 xflags |= XFS_XFLAG_IMMUTABLE;
1112 else
1113 xflags &= ~XFS_XFLAG_IMMUTABLE;
1114 if (flags & LINUX_XFLAG_APPEND)
1115 xflags |= XFS_XFLAG_APPEND;
1116 else
1117 xflags &= ~XFS_XFLAG_APPEND;
1118 if (flags & LINUX_XFLAG_SYNC)
1119 xflags |= XFS_XFLAG_SYNC;
1120 else
1121 xflags &= ~XFS_XFLAG_SYNC;
1122 if (flags & LINUX_XFLAG_NOATIME)
1123 xflags |= XFS_XFLAG_NOATIME;
1124 else
1125 xflags &= ~XFS_XFLAG_NOATIME;
1126 if (flags & LINUX_XFLAG_NODUMP)
1127 xflags |= XFS_XFLAG_NODUMP;
1128 else
1129 xflags &= ~XFS_XFLAG_NODUMP;
1131 return xflags;
1134 STATIC unsigned int
1135 xfs_di2lxflags(
1136 __uint16_t di_flags)
1138 unsigned int flags = 0;
1140 if (di_flags & XFS_DIFLAG_IMMUTABLE)
1141 flags |= LINUX_XFLAG_IMMUTABLE;
1142 if (di_flags & XFS_DIFLAG_APPEND)
1143 flags |= LINUX_XFLAG_APPEND;
1144 if (di_flags & XFS_DIFLAG_SYNC)
1145 flags |= LINUX_XFLAG_SYNC;
1146 if (di_flags & XFS_DIFLAG_NOATIME)
1147 flags |= LINUX_XFLAG_NOATIME;
1148 if (di_flags & XFS_DIFLAG_NODUMP)
1149 flags |= LINUX_XFLAG_NODUMP;
1150 return flags;
1153 STATIC int
1154 xfs_ioc_xattr(
1155 vnode_t *vp,
1156 xfs_inode_t *ip,
1157 struct file *filp,
1158 unsigned int cmd,
1159 void __user *arg)
1161 struct fsxattr fa;
1162 vattr_t va;
1163 int error;
1164 int attr_flags;
1165 unsigned int flags;
1167 switch (cmd) {
1168 case XFS_IOC_FSGETXATTR: {
1169 va.va_mask = XFS_AT_XFLAGS | XFS_AT_EXTSIZE | \
1170 XFS_AT_NEXTENTS | XFS_AT_PROJID;
1171 VOP_GETATTR(vp, &va, 0, NULL, error);
1172 if (error)
1173 return -error;
1175 fa.fsx_xflags = va.va_xflags;
1176 fa.fsx_extsize = va.va_extsize;
1177 fa.fsx_nextents = va.va_nextents;
1178 fa.fsx_projid = va.va_projid;
1180 if (copy_to_user(arg, &fa, sizeof(fa)))
1181 return -XFS_ERROR(EFAULT);
1182 return 0;
1185 case XFS_IOC_FSSETXATTR: {
1186 if (copy_from_user(&fa, arg, sizeof(fa)))
1187 return -XFS_ERROR(EFAULT);
1189 attr_flags = 0;
1190 if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
1191 attr_flags |= ATTR_NONBLOCK;
1193 va.va_mask = XFS_AT_XFLAGS | XFS_AT_EXTSIZE | XFS_AT_PROJID;
1194 va.va_xflags = fa.fsx_xflags;
1195 va.va_extsize = fa.fsx_extsize;
1196 va.va_projid = fa.fsx_projid;
1198 VOP_SETATTR(vp, &va, attr_flags, NULL, error);
1199 if (!error)
1200 vn_revalidate(vp); /* update Linux inode flags */
1201 return -error;
1204 case XFS_IOC_FSGETXATTRA: {
1205 va.va_mask = XFS_AT_XFLAGS | XFS_AT_EXTSIZE | \
1206 XFS_AT_ANEXTENTS | XFS_AT_PROJID;
1207 VOP_GETATTR(vp, &va, 0, NULL, error);
1208 if (error)
1209 return -error;
1211 fa.fsx_xflags = va.va_xflags;
1212 fa.fsx_extsize = va.va_extsize;
1213 fa.fsx_nextents = va.va_anextents;
1214 fa.fsx_projid = va.va_projid;
1216 if (copy_to_user(arg, &fa, sizeof(fa)))
1217 return -XFS_ERROR(EFAULT);
1218 return 0;
1221 case XFS_IOC_GETXFLAGS: {
1222 flags = xfs_di2lxflags(ip->i_d.di_flags);
1223 if (copy_to_user(arg, &flags, sizeof(flags)))
1224 return -XFS_ERROR(EFAULT);
1225 return 0;
1228 case XFS_IOC_SETXFLAGS: {
1229 if (copy_from_user(&flags, arg, sizeof(flags)))
1230 return -XFS_ERROR(EFAULT);
1232 if (flags & ~(LINUX_XFLAG_IMMUTABLE | LINUX_XFLAG_APPEND | \
1233 LINUX_XFLAG_NOATIME | LINUX_XFLAG_NODUMP | \
1234 LINUX_XFLAG_SYNC))
1235 return -XFS_ERROR(EOPNOTSUPP);
1237 attr_flags = 0;
1238 if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
1239 attr_flags |= ATTR_NONBLOCK;
1241 va.va_mask = XFS_AT_XFLAGS;
1242 va.va_xflags = xfs_merge_ioc_xflags(flags,
1243 xfs_ip2xflags(ip));
1245 VOP_SETATTR(vp, &va, attr_flags, NULL, error);
1246 if (!error)
1247 vn_revalidate(vp); /* update Linux inode flags */
1248 return -error;
1251 case XFS_IOC_GETVERSION: {
1252 flags = LINVFS_GET_IP(vp)->i_generation;
1253 if (copy_to_user(arg, &flags, sizeof(flags)))
1254 return -XFS_ERROR(EFAULT);
1255 return 0;
1258 default:
1259 return -ENOTTY;
1263 STATIC int
1264 xfs_ioc_getbmap(
1265 bhv_desc_t *bdp,
1266 struct file *filp,
1267 int ioflags,
1268 unsigned int cmd,
1269 void __user *arg)
1271 struct getbmap bm;
1272 int iflags;
1273 int error;
1275 if (copy_from_user(&bm, arg, sizeof(bm)))
1276 return -XFS_ERROR(EFAULT);
1278 if (bm.bmv_count < 2)
1279 return -XFS_ERROR(EINVAL);
1281 iflags = (cmd == XFS_IOC_GETBMAPA ? BMV_IF_ATTRFORK : 0);
1282 if (ioflags & IO_INVIS)
1283 iflags |= BMV_IF_NO_DMAPI_READ;
1285 error = xfs_getbmap(bdp, &bm, (struct getbmap __user *)arg+1, iflags);
1286 if (error)
1287 return -error;
1289 if (copy_to_user(arg, &bm, sizeof(bm)))
1290 return -XFS_ERROR(EFAULT);
1291 return 0;
1294 STATIC int
1295 xfs_ioc_getbmapx(
1296 bhv_desc_t *bdp,
1297 void __user *arg)
1299 struct getbmapx bmx;
1300 struct getbmap bm;
1301 int iflags;
1302 int error;
1304 if (copy_from_user(&bmx, arg, sizeof(bmx)))
1305 return -XFS_ERROR(EFAULT);
1307 if (bmx.bmv_count < 2)
1308 return -XFS_ERROR(EINVAL);
1311 * Map input getbmapx structure to a getbmap
1312 * structure for xfs_getbmap.
1314 GETBMAP_CONVERT(bmx, bm);
1316 iflags = bmx.bmv_iflags;
1318 if (iflags & (~BMV_IF_VALID))
1319 return -XFS_ERROR(EINVAL);
1321 iflags |= BMV_IF_EXTENDED;
1323 error = xfs_getbmap(bdp, &bm, (struct getbmapx __user *)arg+1, iflags);
1324 if (error)
1325 return -error;
1327 GETBMAP_CONVERT(bm, bmx);
1329 if (copy_to_user(arg, &bmx, sizeof(bmx)))
1330 return -XFS_ERROR(EFAULT);
1332 return 0;