2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
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
23 #include "xfs_trans.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"
46 #include "xfs_buf_item.h"
47 #include "xfs_utils.h"
48 #include "xfs_dfrag.h"
49 #include "xfs_fsops.h"
50 #include "xfs_vnodeops.h"
51 #include "xfs_quota.h"
52 #include "xfs_inode_item.h"
54 #include <linux/capability.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
78 xfs_fsop_handlereq_t hreq;
81 if (copy_from_user(&hreq, arg, sizeof(hreq)))
82 return -XFS_ERROR(EFAULT);
84 memset((char *)&handle, 0, sizeof(handle));
87 case XFS_IOC_PATH_TO_FSHANDLE:
88 case XFS_IOC_PATH_TO_HANDLE: {
90 int error = user_lpath((const char __user *)hreq.path, &path);
95 ASSERT(path.dentry->d_inode);
96 inode = igrab(path.dentry->d_inode);
101 case XFS_IOC_FD_TO_HANDLE: {
104 file = fget(hreq.fd);
108 ASSERT(file->f_path.dentry);
109 ASSERT(file->f_path.dentry->d_inode);
110 inode = igrab(file->f_path.dentry->d_inode);
117 return -XFS_ERROR(EINVAL);
120 if (inode->i_sb->s_magic != XFS_SB_MAGIC) {
121 /* we're not in XFS anymore, Toto */
123 return -XFS_ERROR(EINVAL);
126 switch (inode->i_mode & S_IFMT) {
133 return -XFS_ERROR(EBADF);
136 /* now we can grab the fsid */
137 memcpy(&handle.ha_fsid, XFS_I(inode)->i_mount->m_fixedfsid,
139 hsize = sizeof(xfs_fsid_t);
141 if (cmd != XFS_IOC_PATH_TO_FSHANDLE) {
142 xfs_inode_t *ip = XFS_I(inode);
145 /* need to get access to the xfs_inode to read the generation */
146 lock_mode = xfs_ilock_map_shared(ip);
148 /* fill in fid section of handle from inode */
149 handle.ha_fid.fid_len = sizeof(xfs_fid_t) -
150 sizeof(handle.ha_fid.fid_len);
151 handle.ha_fid.fid_pad = 0;
152 handle.ha_fid.fid_gen = ip->i_d.di_gen;
153 handle.ha_fid.fid_ino = ip->i_ino;
155 xfs_iunlock_map_shared(ip, lock_mode);
157 hsize = XFS_HSIZE(handle);
160 /* now copy our handle into the user buffer & write out the size */
161 if (copy_to_user(hreq.ohandle, &handle, hsize) ||
162 copy_to_user(hreq.ohandlen, &hsize, sizeof(__s32))) {
164 return -XFS_ERROR(EFAULT);
173 * Convert userspace handle data into inode.
175 * We use the fact that all the fsop_handlereq ioctl calls have a data
176 * structure argument whose first component is always a xfs_fsop_handlereq_t,
177 * so we can pass that sub structure into this handy, shared routine.
179 * If no error, caller must always iput the returned inode.
182 xfs_vget_fsop_handlereq(
184 struct inode *parinode, /* parent inode pointer */
185 xfs_fsop_handlereq_t *hreq,
186 struct inode **inode)
191 xfs_handle_t *handlep;
199 * Only allow handle opens under a directory.
201 if (!S_ISDIR(parinode->i_mode))
202 return XFS_ERROR(ENOTDIR);
204 hanp = hreq->ihandle;
205 hlen = hreq->ihandlen;
208 if (hlen < sizeof(handlep->ha_fsid) || hlen > sizeof(*handlep))
209 return XFS_ERROR(EINVAL);
210 if (copy_from_user(handlep, hanp, hlen))
211 return XFS_ERROR(EFAULT);
212 if (hlen < sizeof(*handlep))
213 memset(((char *)handlep) + hlen, 0, sizeof(*handlep) - hlen);
214 if (hlen > sizeof(handlep->ha_fsid)) {
215 if (handlep->ha_fid.fid_len !=
216 (hlen - sizeof(handlep->ha_fsid) -
217 sizeof(handlep->ha_fid.fid_len)) ||
218 handlep->ha_fid.fid_pad)
219 return XFS_ERROR(EINVAL);
223 * Crack the handle, obtain the inode # & generation #
225 xfid = (struct xfs_fid *)&handlep->ha_fid;
226 if (xfid->fid_len == sizeof(*xfid) - sizeof(xfid->fid_len)) {
228 igen = xfid->fid_gen;
230 return XFS_ERROR(EINVAL);
234 * Get the XFS inode, building a Linux inode to go with it.
236 error = xfs_iget(mp, NULL, ino, 0, XFS_ILOCK_SHARED, &ip, 0);
240 return XFS_ERROR(EIO);
241 if (ip->i_d.di_gen != igen) {
242 xfs_iput_new(ip, XFS_ILOCK_SHARED);
243 return XFS_ERROR(ENOENT);
246 xfs_iunlock(ip, XFS_ILOCK_SHARED);
256 struct file *parfilp,
257 struct inode *parinode)
264 struct dentry *dentry;
265 xfs_fsop_handlereq_t hreq;
267 if (!capable(CAP_SYS_ADMIN))
268 return -XFS_ERROR(EPERM);
269 if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
270 return -XFS_ERROR(EFAULT);
272 error = xfs_vget_fsop_handlereq(mp, parinode, &hreq, &inode);
276 /* Restrict xfs_open_by_handle to directories & regular files. */
277 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode))) {
279 return -XFS_ERROR(EINVAL);
282 #if BITS_PER_LONG != 32
283 hreq.oflags |= O_LARGEFILE;
285 /* Put open permission in namei format. */
286 permflag = hreq.oflags;
287 if ((permflag+1) & O_ACCMODE)
289 if (permflag & O_TRUNC)
292 if ((!(permflag & O_APPEND) || (permflag & O_TRUNC)) &&
293 (permflag & FMODE_WRITE) && IS_APPEND(inode)) {
295 return -XFS_ERROR(EPERM);
298 if ((permflag & FMODE_WRITE) && IS_IMMUTABLE(inode)) {
300 return -XFS_ERROR(EACCES);
303 /* Can't write directories. */
304 if ( S_ISDIR(inode->i_mode) && (permflag & FMODE_WRITE)) {
306 return -XFS_ERROR(EISDIR);
309 if ((new_fd = get_unused_fd()) < 0) {
314 dentry = d_obtain_alias(inode);
315 if (IS_ERR(dentry)) {
316 put_unused_fd(new_fd);
317 return PTR_ERR(dentry);
320 /* Ensure umount returns EBUSY on umounts while this file is open. */
321 mntget(parfilp->f_path.mnt);
323 /* Create file pointer. */
324 filp = dentry_open(dentry, parfilp->f_path.mnt, hreq.oflags);
326 put_unused_fd(new_fd);
327 return -XFS_ERROR(-PTR_ERR(filp));
329 if (inode->i_mode & S_IFREG) {
330 /* invisible operation should not change atime */
331 filp->f_flags |= O_NOATIME;
332 filp->f_op = &xfs_invis_file_operations;
335 fd_install(new_fd, filp);
340 * This is a copy from fs/namei.c:vfs_readlink(), except for removing it's
341 * unused first argument.
356 if (len > (unsigned) buflen)
358 if (copy_to_user(buffer, link, len))
366 xfs_readlink_by_handle(
369 struct inode *parinode)
372 xfs_fsop_handlereq_t hreq;
377 if (!capable(CAP_SYS_ADMIN))
378 return -XFS_ERROR(EPERM);
379 if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
380 return -XFS_ERROR(EFAULT);
382 error = xfs_vget_fsop_handlereq(mp, parinode, &hreq, &inode);
386 /* Restrict this handle operation to symlinks only. */
387 if (!S_ISLNK(inode->i_mode)) {
388 error = -XFS_ERROR(EINVAL);
392 if (copy_from_user(&olen, hreq.ohandlen, sizeof(__u32))) {
393 error = -XFS_ERROR(EFAULT);
397 link = kmalloc(MAXPATHLEN+1, GFP_KERNEL);
401 error = -xfs_readlink(XFS_I(inode), link);
404 error = do_readlink(hreq.ohandle, olen, link);
416 xfs_fssetdm_by_handle(
419 struct inode *parinode)
422 struct fsdmidata fsd;
423 xfs_fsop_setdm_handlereq_t dmhreq;
426 if (!capable(CAP_MKNOD))
427 return -XFS_ERROR(EPERM);
428 if (copy_from_user(&dmhreq, arg, sizeof(xfs_fsop_setdm_handlereq_t)))
429 return -XFS_ERROR(EFAULT);
431 error = xfs_vget_fsop_handlereq(mp, parinode, &dmhreq.hreq, &inode);
435 if (IS_IMMUTABLE(inode) || IS_APPEND(inode)) {
436 error = -XFS_ERROR(EPERM);
440 if (copy_from_user(&fsd, dmhreq.data, sizeof(fsd))) {
441 error = -XFS_ERROR(EFAULT);
445 error = -xfs_set_dmattrs(XFS_I(inode), fsd.fsd_dmevmask,
454 xfs_attrlist_by_handle(
457 struct inode *parinode)
460 attrlist_cursor_kern_t *cursor;
461 xfs_fsop_attrlist_handlereq_t al_hreq;
465 if (!capable(CAP_SYS_ADMIN))
466 return -XFS_ERROR(EPERM);
467 if (copy_from_user(&al_hreq, arg, sizeof(xfs_fsop_attrlist_handlereq_t)))
468 return -XFS_ERROR(EFAULT);
469 if (al_hreq.buflen > XATTR_LIST_MAX)
470 return -XFS_ERROR(EINVAL);
473 * Reject flags, only allow namespaces.
475 if (al_hreq.flags & ~(ATTR_ROOT | ATTR_SECURE))
476 return -XFS_ERROR(EINVAL);
478 error = xfs_vget_fsop_handlereq(mp, parinode, &al_hreq.hreq, &inode);
482 kbuf = kmalloc(al_hreq.buflen, GFP_KERNEL);
486 cursor = (attrlist_cursor_kern_t *)&al_hreq.pos;
487 error = xfs_attr_list(XFS_I(inode), kbuf, al_hreq.buflen,
488 al_hreq.flags, cursor);
492 if (copy_to_user(al_hreq.buffer, kbuf, al_hreq.buflen))
504 xfs_attrmulti_attr_get(
514 if (*len > XATTR_SIZE_MAX)
516 kbuf = kmalloc(*len, GFP_KERNEL);
520 error = xfs_attr_get(XFS_I(inode), name, kbuf, (int *)len, flags);
524 if (copy_to_user(ubuf, kbuf, *len))
533 xfs_attrmulti_attr_set(
536 const char __user *ubuf,
543 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
545 if (len > XATTR_SIZE_MAX)
548 kbuf = kmalloc(len, GFP_KERNEL);
552 if (copy_from_user(kbuf, ubuf, len))
555 error = xfs_attr_set(XFS_I(inode), name, kbuf, len, flags);
563 xfs_attrmulti_attr_remove(
568 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
570 return xfs_attr_remove(XFS_I(inode), name, flags);
574 xfs_attrmulti_by_handle(
577 struct file *parfilp,
578 struct inode *parinode)
581 xfs_attr_multiop_t *ops;
582 xfs_fsop_attrmulti_handlereq_t am_hreq;
584 unsigned int i, size;
587 if (!capable(CAP_SYS_ADMIN))
588 return -XFS_ERROR(EPERM);
589 if (copy_from_user(&am_hreq, arg, sizeof(xfs_fsop_attrmulti_handlereq_t)))
590 return -XFS_ERROR(EFAULT);
592 error = xfs_vget_fsop_handlereq(mp, parinode, &am_hreq.hreq, &inode);
597 size = am_hreq.opcount * sizeof(xfs_attr_multiop_t);
598 if (!size || size > 16 * PAGE_SIZE)
602 ops = kmalloc(size, GFP_KERNEL);
607 if (copy_from_user(ops, am_hreq.ops, size))
610 attr_name = kmalloc(MAXNAMELEN, GFP_KERNEL);
616 for (i = 0; i < am_hreq.opcount; i++) {
617 ops[i].am_error = strncpy_from_user(attr_name,
618 ops[i].am_attrname, MAXNAMELEN);
619 if (ops[i].am_error == 0 || ops[i].am_error == MAXNAMELEN)
621 if (ops[i].am_error < 0)
624 switch (ops[i].am_opcode) {
626 ops[i].am_error = xfs_attrmulti_attr_get(inode,
627 attr_name, ops[i].am_attrvalue,
628 &ops[i].am_length, ops[i].am_flags);
631 ops[i].am_error = mnt_want_write(parfilp->f_path.mnt);
634 ops[i].am_error = xfs_attrmulti_attr_set(inode,
635 attr_name, ops[i].am_attrvalue,
636 ops[i].am_length, ops[i].am_flags);
637 mnt_drop_write(parfilp->f_path.mnt);
640 ops[i].am_error = mnt_want_write(parfilp->f_path.mnt);
643 ops[i].am_error = xfs_attrmulti_attr_remove(inode,
644 attr_name, ops[i].am_flags);
645 mnt_drop_write(parfilp->f_path.mnt);
648 ops[i].am_error = EINVAL;
652 if (copy_to_user(am_hreq.ops, ops, size))
653 error = XFS_ERROR(EFAULT);
666 struct xfs_inode *ip,
677 if (inode->i_flags & (S_IMMUTABLE|S_APPEND))
678 return -XFS_ERROR(EPERM);
680 if (!(filp->f_mode & FMODE_WRITE))
681 return -XFS_ERROR(EBADF);
683 if (!S_ISREG(inode->i_mode))
684 return -XFS_ERROR(EINVAL);
686 if (copy_from_user(&bf, arg, sizeof(bf)))
687 return -XFS_ERROR(EFAULT);
689 if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
690 attr_flags |= XFS_ATTR_NONBLOCK;
691 if (ioflags & IO_INVIS)
692 attr_flags |= XFS_ATTR_DMI;
694 error = xfs_change_file_space(ip, cmd, &bf, filp->f_pos, attr_flags);
704 xfs_fsop_bulkreq_t bulkreq;
705 int count; /* # of records returned */
706 xfs_ino_t inlast; /* last inode number */
710 /* done = 1 if there are more stats to get and if bulkstat */
711 /* should be called again (unused here, but used in dmapi) */
713 if (!capable(CAP_SYS_ADMIN))
716 if (XFS_FORCED_SHUTDOWN(mp))
717 return -XFS_ERROR(EIO);
719 if (copy_from_user(&bulkreq, arg, sizeof(xfs_fsop_bulkreq_t)))
720 return -XFS_ERROR(EFAULT);
722 if (copy_from_user(&inlast, bulkreq.lastip, sizeof(__s64)))
723 return -XFS_ERROR(EFAULT);
725 if ((count = bulkreq.icount) <= 0)
726 return -XFS_ERROR(EINVAL);
728 if (bulkreq.ubuffer == NULL)
729 return -XFS_ERROR(EINVAL);
731 if (cmd == XFS_IOC_FSINUMBERS)
732 error = xfs_inumbers(mp, &inlast, &count,
733 bulkreq.ubuffer, xfs_inumbers_fmt);
734 else if (cmd == XFS_IOC_FSBULKSTAT_SINGLE)
735 error = xfs_bulkstat_single(mp, &inlast,
736 bulkreq.ubuffer, &done);
737 else /* XFS_IOC_FSBULKSTAT */
738 error = xfs_bulkstat(mp, &inlast, &count,
739 (bulkstat_one_pf)xfs_bulkstat_one, NULL,
740 sizeof(xfs_bstat_t), bulkreq.ubuffer,
741 BULKSTAT_FG_QUICK, &done);
746 if (bulkreq.ocount != NULL) {
747 if (copy_to_user(bulkreq.lastip, &inlast,
749 return -XFS_ERROR(EFAULT);
751 if (copy_to_user(bulkreq.ocount, &count, sizeof(count)))
752 return -XFS_ERROR(EFAULT);
759 xfs_ioc_fsgeometry_v1(
763 xfs_fsop_geom_v1_t fsgeo;
766 error = xfs_fs_geometry(mp, (xfs_fsop_geom_t *)&fsgeo, 3);
770 if (copy_to_user(arg, &fsgeo, sizeof(fsgeo)))
771 return -XFS_ERROR(EFAULT);
780 xfs_fsop_geom_t fsgeo;
783 error = xfs_fs_geometry(mp, &fsgeo, 4);
787 if (copy_to_user(arg, &fsgeo, sizeof(fsgeo)))
788 return -XFS_ERROR(EFAULT);
793 * Linux extended inode flags interface.
797 xfs_merge_ioc_xflags(
801 unsigned int xflags = start;
803 if (flags & FS_IMMUTABLE_FL)
804 xflags |= XFS_XFLAG_IMMUTABLE;
806 xflags &= ~XFS_XFLAG_IMMUTABLE;
807 if (flags & FS_APPEND_FL)
808 xflags |= XFS_XFLAG_APPEND;
810 xflags &= ~XFS_XFLAG_APPEND;
811 if (flags & FS_SYNC_FL)
812 xflags |= XFS_XFLAG_SYNC;
814 xflags &= ~XFS_XFLAG_SYNC;
815 if (flags & FS_NOATIME_FL)
816 xflags |= XFS_XFLAG_NOATIME;
818 xflags &= ~XFS_XFLAG_NOATIME;
819 if (flags & FS_NODUMP_FL)
820 xflags |= XFS_XFLAG_NODUMP;
822 xflags &= ~XFS_XFLAG_NODUMP;
831 unsigned int flags = 0;
833 if (di_flags & XFS_DIFLAG_IMMUTABLE)
834 flags |= FS_IMMUTABLE_FL;
835 if (di_flags & XFS_DIFLAG_APPEND)
836 flags |= FS_APPEND_FL;
837 if (di_flags & XFS_DIFLAG_SYNC)
839 if (di_flags & XFS_DIFLAG_NOATIME)
840 flags |= FS_NOATIME_FL;
841 if (di_flags & XFS_DIFLAG_NODUMP)
842 flags |= FS_NODUMP_FL;
854 xfs_ilock(ip, XFS_ILOCK_SHARED);
855 fa.fsx_xflags = xfs_ip2xflags(ip);
856 fa.fsx_extsize = ip->i_d.di_extsize << ip->i_mount->m_sb.sb_blocklog;
857 fa.fsx_projid = ip->i_d.di_projid;
861 if (ip->i_afp->if_flags & XFS_IFEXTENTS)
862 fa.fsx_nextents = ip->i_afp->if_bytes /
863 sizeof(xfs_bmbt_rec_t);
865 fa.fsx_nextents = ip->i_d.di_anextents;
869 if (ip->i_df.if_flags & XFS_IFEXTENTS)
870 fa.fsx_nextents = ip->i_df.if_bytes /
871 sizeof(xfs_bmbt_rec_t);
873 fa.fsx_nextents = ip->i_d.di_nextents;
875 xfs_iunlock(ip, XFS_ILOCK_SHARED);
877 if (copy_to_user(arg, &fa, sizeof(fa)))
884 struct xfs_inode *ip,
887 unsigned int di_flags;
889 /* can't set PREALLOC this way, just preserve it */
890 di_flags = (ip->i_d.di_flags & XFS_DIFLAG_PREALLOC);
891 if (xflags & XFS_XFLAG_IMMUTABLE)
892 di_flags |= XFS_DIFLAG_IMMUTABLE;
893 if (xflags & XFS_XFLAG_APPEND)
894 di_flags |= XFS_DIFLAG_APPEND;
895 if (xflags & XFS_XFLAG_SYNC)
896 di_flags |= XFS_DIFLAG_SYNC;
897 if (xflags & XFS_XFLAG_NOATIME)
898 di_flags |= XFS_DIFLAG_NOATIME;
899 if (xflags & XFS_XFLAG_NODUMP)
900 di_flags |= XFS_DIFLAG_NODUMP;
901 if (xflags & XFS_XFLAG_PROJINHERIT)
902 di_flags |= XFS_DIFLAG_PROJINHERIT;
903 if (xflags & XFS_XFLAG_NODEFRAG)
904 di_flags |= XFS_DIFLAG_NODEFRAG;
905 if (xflags & XFS_XFLAG_FILESTREAM)
906 di_flags |= XFS_DIFLAG_FILESTREAM;
907 if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
908 if (xflags & XFS_XFLAG_RTINHERIT)
909 di_flags |= XFS_DIFLAG_RTINHERIT;
910 if (xflags & XFS_XFLAG_NOSYMLINKS)
911 di_flags |= XFS_DIFLAG_NOSYMLINKS;
912 if (xflags & XFS_XFLAG_EXTSZINHERIT)
913 di_flags |= XFS_DIFLAG_EXTSZINHERIT;
914 } else if ((ip->i_d.di_mode & S_IFMT) == S_IFREG) {
915 if (xflags & XFS_XFLAG_REALTIME)
916 di_flags |= XFS_DIFLAG_REALTIME;
917 if (xflags & XFS_XFLAG_EXTSIZE)
918 di_flags |= XFS_DIFLAG_EXTSIZE;
921 ip->i_d.di_flags = di_flags;
925 xfs_diflags_to_linux(
926 struct xfs_inode *ip)
928 struct inode *inode = VFS_I(ip);
929 unsigned int xflags = xfs_ip2xflags(ip);
931 if (xflags & XFS_XFLAG_IMMUTABLE)
932 inode->i_flags |= S_IMMUTABLE;
934 inode->i_flags &= ~S_IMMUTABLE;
935 if (xflags & XFS_XFLAG_APPEND)
936 inode->i_flags |= S_APPEND;
938 inode->i_flags &= ~S_APPEND;
939 if (xflags & XFS_XFLAG_SYNC)
940 inode->i_flags |= S_SYNC;
942 inode->i_flags &= ~S_SYNC;
943 if (xflags & XFS_XFLAG_NOATIME)
944 inode->i_flags |= S_NOATIME;
946 inode->i_flags &= ~S_NOATIME;
950 #define FSX_EXTSIZE 2
952 #define FSX_NONBLOCK 8
960 struct xfs_mount *mp = ip->i_mount;
961 struct xfs_trans *tp;
962 unsigned int lock_flags = 0;
963 struct xfs_dquot *udqp = NULL, *gdqp = NULL;
964 struct xfs_dquot *olddquot = NULL;
967 xfs_itrace_entry(ip);
969 if (mp->m_flags & XFS_MOUNT_RDONLY)
970 return XFS_ERROR(EROFS);
971 if (XFS_FORCED_SHUTDOWN(mp))
972 return XFS_ERROR(EIO);
975 * If disk quotas is on, we make sure that the dquots do exist on disk,
976 * before we start any other transactions. Trying to do this later
977 * is messy. We don't care to take a readlock to look at the ids
978 * in inode here, because we can't hold it across the trans_reserve.
979 * If the IDs do change before we take the ilock, we're covered
980 * because the i_*dquot fields will get updated anyway.
982 if (XFS_IS_QUOTA_ON(mp) && (mask & FSX_PROJID)) {
983 code = XFS_QM_DQVOPALLOC(mp, ip, ip->i_d.di_uid,
984 ip->i_d.di_gid, fa->fsx_projid,
985 XFS_QMOPT_PQUOTA, &udqp, &gdqp);
991 * For the other attributes, we acquire the inode lock and
992 * first do an error checking pass.
994 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
995 code = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES(mp), 0, 0, 0);
999 lock_flags = XFS_ILOCK_EXCL;
1000 xfs_ilock(ip, lock_flags);
1003 * CAP_FOWNER overrides the following restrictions:
1005 * The user ID of the calling process must be equal
1006 * to the file owner ID, except in cases where the
1007 * CAP_FSETID capability is applicable.
1009 if (current_fsuid() != ip->i_d.di_uid && !capable(CAP_FOWNER)) {
1010 code = XFS_ERROR(EPERM);
1015 * Do a quota reservation only if projid is actually going to change.
1017 if (mask & FSX_PROJID) {
1018 if (XFS_IS_PQUOTA_ON(mp) &&
1019 ip->i_d.di_projid != fa->fsx_projid) {
1021 code = XFS_QM_DQVOPCHOWNRESV(mp, tp, ip, udqp, gdqp,
1022 capable(CAP_FOWNER) ?
1023 XFS_QMOPT_FORCE_RES : 0);
1024 if (code) /* out of quota */
1029 if (mask & FSX_EXTSIZE) {
1031 * Can't change extent size if any extents are allocated.
1033 if (ip->i_d.di_nextents &&
1034 ((ip->i_d.di_extsize << mp->m_sb.sb_blocklog) !=
1036 code = XFS_ERROR(EINVAL); /* EFBIG? */
1041 * Extent size must be a multiple of the appropriate block
1042 * size, if set at all.
1044 if (fa->fsx_extsize != 0) {
1047 if (XFS_IS_REALTIME_INODE(ip) ||
1048 ((mask & FSX_XFLAGS) &&
1049 (fa->fsx_xflags & XFS_XFLAG_REALTIME))) {
1050 size = mp->m_sb.sb_rextsize <<
1051 mp->m_sb.sb_blocklog;
1053 size = mp->m_sb.sb_blocksize;
1056 if (fa->fsx_extsize % size) {
1057 code = XFS_ERROR(EINVAL);
1064 if (mask & FSX_XFLAGS) {
1066 * Can't change realtime flag if any extents are allocated.
1068 if ((ip->i_d.di_nextents || ip->i_delayed_blks) &&
1069 (XFS_IS_REALTIME_INODE(ip)) !=
1070 (fa->fsx_xflags & XFS_XFLAG_REALTIME)) {
1071 code = XFS_ERROR(EINVAL); /* EFBIG? */
1076 * If realtime flag is set then must have realtime data.
1078 if ((fa->fsx_xflags & XFS_XFLAG_REALTIME)) {
1079 if ((mp->m_sb.sb_rblocks == 0) ||
1080 (mp->m_sb.sb_rextsize == 0) ||
1081 (ip->i_d.di_extsize % mp->m_sb.sb_rextsize)) {
1082 code = XFS_ERROR(EINVAL);
1088 * Can't modify an immutable/append-only file unless
1089 * we have appropriate permission.
1091 if ((ip->i_d.di_flags &
1092 (XFS_DIFLAG_IMMUTABLE|XFS_DIFLAG_APPEND) ||
1094 (XFS_XFLAG_IMMUTABLE | XFS_XFLAG_APPEND))) &&
1095 !capable(CAP_LINUX_IMMUTABLE)) {
1096 code = XFS_ERROR(EPERM);
1101 xfs_trans_ijoin(tp, ip, lock_flags);
1102 xfs_trans_ihold(tp, ip);
1105 * Change file ownership. Must be the owner or privileged.
1107 if (mask & FSX_PROJID) {
1109 * CAP_FSETID overrides the following restrictions:
1111 * The set-user-ID and set-group-ID bits of a file will be
1112 * cleared upon successful return from chown()
1114 if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
1115 !capable(CAP_FSETID))
1116 ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
1119 * Change the ownerships and register quota modifications
1120 * in the transaction.
1122 if (ip->i_d.di_projid != fa->fsx_projid) {
1123 if (XFS_IS_PQUOTA_ON(mp)) {
1124 olddquot = XFS_QM_DQVOPCHOWN(mp, tp, ip,
1125 &ip->i_gdquot, gdqp);
1127 ip->i_d.di_projid = fa->fsx_projid;
1130 * We may have to rev the inode as well as
1131 * the superblock version number since projids didn't
1132 * exist before DINODE_VERSION_2 and SB_VERSION_NLINK.
1134 if (ip->i_d.di_version == XFS_DINODE_VERSION_1)
1135 xfs_bump_ino_vers2(tp, ip);
1140 if (mask & FSX_EXTSIZE)
1141 ip->i_d.di_extsize = fa->fsx_extsize >> mp->m_sb.sb_blocklog;
1142 if (mask & FSX_XFLAGS) {
1143 xfs_set_diflags(ip, fa->fsx_xflags);
1144 xfs_diflags_to_linux(ip);
1147 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1148 xfs_ichgtime(ip, XFS_ICHGTIME_CHG);
1150 XFS_STATS_INC(xs_ig_attrchg);
1153 * If this is a synchronous mount, make sure that the
1154 * transaction goes to disk before returning to the user.
1155 * This is slightly sub-optimal in that truncates require
1156 * two sync transactions instead of one for wsync filesystems.
1157 * One for the truncate and one for the timestamps since we
1158 * don't want to change the timestamps unless we're sure the
1159 * truncate worked. Truncates are less than 1% of the laddis
1160 * mix so this probably isn't worth the trouble to optimize.
1162 if (mp->m_flags & XFS_MOUNT_WSYNC)
1163 xfs_trans_set_sync(tp);
1164 code = xfs_trans_commit(tp, 0);
1165 xfs_iunlock(ip, lock_flags);
1168 * Release any dquot(s) the inode had kept before chown.
1170 XFS_QM_DQRELE(mp, olddquot);
1171 XFS_QM_DQRELE(mp, udqp);
1172 XFS_QM_DQRELE(mp, gdqp);
1177 if (DM_EVENT_ENABLED(ip, DM_EVENT_ATTRIBUTE)) {
1178 XFS_SEND_NAMESP(mp, DM_EVENT_ATTRIBUTE, ip, DM_RIGHT_NULL,
1179 NULL, DM_RIGHT_NULL, NULL, NULL, 0, 0,
1180 (mask & FSX_NONBLOCK) ? DM_FLAGS_NDELAY : 0);
1186 XFS_QM_DQRELE(mp, udqp);
1187 XFS_QM_DQRELE(mp, gdqp);
1188 xfs_trans_cancel(tp, 0);
1190 xfs_iunlock(ip, lock_flags);
1203 if (copy_from_user(&fa, arg, sizeof(fa)))
1206 mask = FSX_XFLAGS | FSX_EXTSIZE | FSX_PROJID;
1207 if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
1208 mask |= FSX_NONBLOCK;
1210 return -xfs_ioctl_setattr(ip, &fa, mask);
1220 flags = xfs_di2lxflags(ip->i_d.di_flags);
1221 if (copy_to_user(arg, &flags, sizeof(flags)))
1236 if (copy_from_user(&flags, arg, sizeof(flags)))
1239 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
1240 FS_NOATIME_FL | FS_NODUMP_FL | \
1245 if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
1246 mask |= FSX_NONBLOCK;
1247 fa.fsx_xflags = xfs_merge_ioc_xflags(flags, xfs_ip2xflags(ip));
1249 return -xfs_ioctl_setattr(ip, &fa, mask);
1254 struct xfs_inode *ip,
1263 if (copy_from_user(&bm, arg, sizeof(bm)))
1264 return -XFS_ERROR(EFAULT);
1266 if (bm.bmv_count < 2)
1267 return -XFS_ERROR(EINVAL);
1269 iflags = (cmd == XFS_IOC_GETBMAPA ? BMV_IF_ATTRFORK : 0);
1270 if (ioflags & IO_INVIS)
1271 iflags |= BMV_IF_NO_DMAPI_READ;
1273 error = xfs_getbmap(ip, &bm, (struct getbmap __user *)arg+1, iflags);
1277 if (copy_to_user(arg, &bm, sizeof(bm)))
1278 return -XFS_ERROR(EFAULT);
1284 struct xfs_inode *ip,
1287 struct getbmapx bmx;
1292 if (copy_from_user(&bmx, arg, sizeof(bmx)))
1293 return -XFS_ERROR(EFAULT);
1295 if (bmx.bmv_count < 2)
1296 return -XFS_ERROR(EINVAL);
1299 * Map input getbmapx structure to a getbmap
1300 * structure for xfs_getbmap.
1302 GETBMAP_CONVERT(bmx, bm);
1304 iflags = bmx.bmv_iflags;
1306 if (iflags & (~BMV_IF_VALID))
1307 return -XFS_ERROR(EINVAL);
1309 iflags |= BMV_IF_EXTENDED;
1311 error = xfs_getbmap(ip, &bm, (struct getbmapx __user *)arg+1, iflags);
1315 GETBMAP_CONVERT(bm, bmx);
1317 if (copy_to_user(arg, &bmx, sizeof(bmx)))
1318 return -XFS_ERROR(EFAULT);
1331 struct inode *inode = filp->f_path.dentry->d_inode;
1332 xfs_mount_t *mp = ip->i_mount;
1335 xfs_itrace_entry(XFS_I(inode));
1338 case XFS_IOC_ALLOCSP:
1339 case XFS_IOC_FREESP:
1340 case XFS_IOC_RESVSP:
1341 case XFS_IOC_UNRESVSP:
1342 case XFS_IOC_ALLOCSP64:
1343 case XFS_IOC_FREESP64:
1344 case XFS_IOC_RESVSP64:
1345 case XFS_IOC_UNRESVSP64:
1347 * Only allow the sys admin to reserve space unless
1348 * unwritten extents are enabled.
1350 if (!xfs_sb_version_hasextflgbit(&mp->m_sb) &&
1351 !capable(CAP_SYS_ADMIN))
1354 return xfs_ioc_space(ip, inode, filp, ioflags, cmd, arg);
1356 case XFS_IOC_DIOINFO: {
1358 xfs_buftarg_t *target =
1359 XFS_IS_REALTIME_INODE(ip) ?
1360 mp->m_rtdev_targp : mp->m_ddev_targp;
1362 da.d_mem = da.d_miniosz = 1 << target->bt_sshift;
1363 da.d_maxiosz = INT_MAX & ~(da.d_miniosz - 1);
1365 if (copy_to_user(arg, &da, sizeof(da)))
1366 return -XFS_ERROR(EFAULT);
1370 case XFS_IOC_FSBULKSTAT_SINGLE:
1371 case XFS_IOC_FSBULKSTAT:
1372 case XFS_IOC_FSINUMBERS:
1373 return xfs_ioc_bulkstat(mp, cmd, arg);
1375 case XFS_IOC_FSGEOMETRY_V1:
1376 return xfs_ioc_fsgeometry_v1(mp, arg);
1378 case XFS_IOC_FSGEOMETRY:
1379 return xfs_ioc_fsgeometry(mp, arg);
1381 case XFS_IOC_GETVERSION:
1382 return put_user(inode->i_generation, (int __user *)arg);
1384 case XFS_IOC_FSGETXATTR:
1385 return xfs_ioc_fsgetxattr(ip, 0, arg);
1386 case XFS_IOC_FSGETXATTRA:
1387 return xfs_ioc_fsgetxattr(ip, 1, arg);
1388 case XFS_IOC_FSSETXATTR:
1389 return xfs_ioc_fssetxattr(ip, filp, arg);
1390 case XFS_IOC_GETXFLAGS:
1391 return xfs_ioc_getxflags(ip, arg);
1392 case XFS_IOC_SETXFLAGS:
1393 return xfs_ioc_setxflags(ip, filp, arg);
1395 case XFS_IOC_FSSETDM: {
1396 struct fsdmidata dmi;
1398 if (copy_from_user(&dmi, arg, sizeof(dmi)))
1399 return -XFS_ERROR(EFAULT);
1401 error = xfs_set_dmattrs(ip, dmi.fsd_dmevmask,
1406 case XFS_IOC_GETBMAP:
1407 case XFS_IOC_GETBMAPA:
1408 return xfs_ioc_getbmap(ip, ioflags, cmd, arg);
1410 case XFS_IOC_GETBMAPX:
1411 return xfs_ioc_getbmapx(ip, arg);
1413 case XFS_IOC_FD_TO_HANDLE:
1414 case XFS_IOC_PATH_TO_HANDLE:
1415 case XFS_IOC_PATH_TO_FSHANDLE:
1416 return xfs_find_handle(cmd, arg);
1418 case XFS_IOC_OPEN_BY_HANDLE:
1419 return xfs_open_by_handle(mp, arg, filp, inode);
1421 case XFS_IOC_FSSETDM_BY_HANDLE:
1422 return xfs_fssetdm_by_handle(mp, arg, inode);
1424 case XFS_IOC_READLINK_BY_HANDLE:
1425 return xfs_readlink_by_handle(mp, arg, inode);
1427 case XFS_IOC_ATTRLIST_BY_HANDLE:
1428 return xfs_attrlist_by_handle(mp, arg, inode);
1430 case XFS_IOC_ATTRMULTI_BY_HANDLE:
1431 return xfs_attrmulti_by_handle(mp, arg, filp, inode);
1433 case XFS_IOC_SWAPEXT: {
1434 error = xfs_swapext((struct xfs_swapext __user *)arg);
1438 case XFS_IOC_FSCOUNTS: {
1439 xfs_fsop_counts_t out;
1441 error = xfs_fs_counts(mp, &out);
1445 if (copy_to_user(arg, &out, sizeof(out)))
1446 return -XFS_ERROR(EFAULT);
1450 case XFS_IOC_SET_RESBLKS: {
1451 xfs_fsop_resblks_t inout;
1454 if (!capable(CAP_SYS_ADMIN))
1457 if (copy_from_user(&inout, arg, sizeof(inout)))
1458 return -XFS_ERROR(EFAULT);
1460 /* input parameter is passed in resblks field of structure */
1462 error = xfs_reserve_blocks(mp, &in, &inout);
1466 if (copy_to_user(arg, &inout, sizeof(inout)))
1467 return -XFS_ERROR(EFAULT);
1471 case XFS_IOC_GET_RESBLKS: {
1472 xfs_fsop_resblks_t out;
1474 if (!capable(CAP_SYS_ADMIN))
1477 error = xfs_reserve_blocks(mp, NULL, &out);
1481 if (copy_to_user(arg, &out, sizeof(out)))
1482 return -XFS_ERROR(EFAULT);
1487 case XFS_IOC_FSGROWFSDATA: {
1488 xfs_growfs_data_t in;
1490 if (!capable(CAP_SYS_ADMIN))
1493 if (copy_from_user(&in, arg, sizeof(in)))
1494 return -XFS_ERROR(EFAULT);
1496 error = xfs_growfs_data(mp, &in);
1500 case XFS_IOC_FSGROWFSLOG: {
1501 xfs_growfs_log_t in;
1503 if (!capable(CAP_SYS_ADMIN))
1506 if (copy_from_user(&in, arg, sizeof(in)))
1507 return -XFS_ERROR(EFAULT);
1509 error = xfs_growfs_log(mp, &in);
1513 case XFS_IOC_FSGROWFSRT: {
1516 if (!capable(CAP_SYS_ADMIN))
1519 if (copy_from_user(&in, arg, sizeof(in)))
1520 return -XFS_ERROR(EFAULT);
1522 error = xfs_growfs_rt(mp, &in);
1526 case XFS_IOC_FREEZE:
1527 if (!capable(CAP_SYS_ADMIN))
1530 if (inode->i_sb->s_frozen == SB_UNFROZEN)
1531 freeze_bdev(inode->i_sb->s_bdev);
1535 if (!capable(CAP_SYS_ADMIN))
1537 if (inode->i_sb->s_frozen != SB_UNFROZEN)
1538 thaw_bdev(inode->i_sb->s_bdev, inode->i_sb);
1541 case XFS_IOC_GOINGDOWN: {
1544 if (!capable(CAP_SYS_ADMIN))
1547 if (get_user(in, (__uint32_t __user *)arg))
1548 return -XFS_ERROR(EFAULT);
1550 error = xfs_fs_goingdown(mp, in);
1554 case XFS_IOC_ERROR_INJECTION: {
1555 xfs_error_injection_t in;
1557 if (!capable(CAP_SYS_ADMIN))
1560 if (copy_from_user(&in, arg, sizeof(in)))
1561 return -XFS_ERROR(EFAULT);
1563 error = xfs_errortag_add(in.errtag, mp);
1567 case XFS_IOC_ERROR_CLEARALL:
1568 if (!capable(CAP_SYS_ADMIN))
1571 error = xfs_errortag_clearall(mp, 1);