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
74 xfs_fsop_handlereq_t *hreq)
80 memset((char *)&handle, 0, sizeof(handle));
83 case XFS_IOC_PATH_TO_FSHANDLE:
84 case XFS_IOC_PATH_TO_HANDLE: {
86 int error = user_lpath((const char __user *)hreq->path, &path);
91 ASSERT(path.dentry->d_inode);
92 inode = igrab(path.dentry->d_inode);
97 case XFS_IOC_FD_TO_HANDLE: {
100 file = fget(hreq->fd);
104 ASSERT(file->f_path.dentry);
105 ASSERT(file->f_path.dentry->d_inode);
106 inode = igrab(file->f_path.dentry->d_inode);
113 return -XFS_ERROR(EINVAL);
116 if (inode->i_sb->s_magic != XFS_SB_MAGIC) {
117 /* we're not in XFS anymore, Toto */
119 return -XFS_ERROR(EINVAL);
122 switch (inode->i_mode & S_IFMT) {
129 return -XFS_ERROR(EBADF);
132 /* now we can grab the fsid */
133 memcpy(&handle.ha_fsid, XFS_I(inode)->i_mount->m_fixedfsid,
135 hsize = sizeof(xfs_fsid_t);
137 if (cmd != XFS_IOC_PATH_TO_FSHANDLE) {
138 xfs_inode_t *ip = XFS_I(inode);
141 /* need to get access to the xfs_inode to read the generation */
142 lock_mode = xfs_ilock_map_shared(ip);
144 /* fill in fid section of handle from inode */
145 handle.ha_fid.fid_len = sizeof(xfs_fid_t) -
146 sizeof(handle.ha_fid.fid_len);
147 handle.ha_fid.fid_pad = 0;
148 handle.ha_fid.fid_gen = ip->i_d.di_gen;
149 handle.ha_fid.fid_ino = ip->i_ino;
151 xfs_iunlock_map_shared(ip, lock_mode);
153 hsize = XFS_HSIZE(handle);
156 /* now copy our handle into the user buffer & write out the size */
157 if (copy_to_user(hreq->ohandle, &handle, hsize) ||
158 copy_to_user(hreq->ohandlen, &hsize, sizeof(__s32))) {
160 return -XFS_ERROR(EFAULT);
169 * Convert userspace handle data into inode.
171 * We use the fact that all the fsop_handlereq ioctl calls have a data
172 * structure argument whose first component is always a xfs_fsop_handlereq_t,
173 * so we can pass that sub structure into this handy, shared routine.
175 * If no error, caller must always iput the returned inode.
178 xfs_vget_fsop_handlereq(
180 struct inode *parinode, /* parent inode pointer */
181 xfs_fsop_handlereq_t *hreq,
182 struct inode **inode)
187 xfs_handle_t *handlep;
195 * Only allow handle opens under a directory.
197 if (!S_ISDIR(parinode->i_mode))
198 return XFS_ERROR(ENOTDIR);
200 hanp = hreq->ihandle;
201 hlen = hreq->ihandlen;
204 if (hlen < sizeof(handlep->ha_fsid) || hlen > sizeof(*handlep))
205 return XFS_ERROR(EINVAL);
206 if (copy_from_user(handlep, hanp, hlen))
207 return XFS_ERROR(EFAULT);
208 if (hlen < sizeof(*handlep))
209 memset(((char *)handlep) + hlen, 0, sizeof(*handlep) - hlen);
210 if (hlen > sizeof(handlep->ha_fsid)) {
211 if (handlep->ha_fid.fid_len !=
212 (hlen - sizeof(handlep->ha_fsid) -
213 sizeof(handlep->ha_fid.fid_len)) ||
214 handlep->ha_fid.fid_pad)
215 return XFS_ERROR(EINVAL);
219 * Crack the handle, obtain the inode # & generation #
221 xfid = (struct xfs_fid *)&handlep->ha_fid;
222 if (xfid->fid_len == sizeof(*xfid) - sizeof(xfid->fid_len)) {
224 igen = xfid->fid_gen;
226 return XFS_ERROR(EINVAL);
230 * Get the XFS inode, building a Linux inode to go with it.
232 error = xfs_iget(mp, NULL, ino, 0, XFS_ILOCK_SHARED, &ip, 0);
236 return XFS_ERROR(EIO);
237 if (ip->i_d.di_gen != igen) {
238 xfs_iput_new(ip, XFS_ILOCK_SHARED);
239 return XFS_ERROR(ENOENT);
242 xfs_iunlock(ip, XFS_ILOCK_SHARED);
251 xfs_fsop_handlereq_t *hreq,
252 struct file *parfilp,
253 struct inode *parinode)
260 struct dentry *dentry;
262 if (!capable(CAP_SYS_ADMIN))
263 return -XFS_ERROR(EPERM);
265 error = xfs_vget_fsop_handlereq(mp, parinode, hreq, &inode);
269 /* Restrict xfs_open_by_handle to directories & regular files. */
270 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode))) {
272 return -XFS_ERROR(EINVAL);
275 #if BITS_PER_LONG != 32
276 hreq->oflags |= O_LARGEFILE;
278 /* Put open permission in namei format. */
279 permflag = hreq->oflags;
280 if ((permflag+1) & O_ACCMODE)
282 if (permflag & O_TRUNC)
285 if ((!(permflag & O_APPEND) || (permflag & O_TRUNC)) &&
286 (permflag & FMODE_WRITE) && IS_APPEND(inode)) {
288 return -XFS_ERROR(EPERM);
291 if ((permflag & FMODE_WRITE) && IS_IMMUTABLE(inode)) {
293 return -XFS_ERROR(EACCES);
296 /* Can't write directories. */
297 if ( S_ISDIR(inode->i_mode) && (permflag & FMODE_WRITE)) {
299 return -XFS_ERROR(EISDIR);
302 if ((new_fd = get_unused_fd()) < 0) {
307 dentry = d_obtain_alias(inode);
308 if (IS_ERR(dentry)) {
309 put_unused_fd(new_fd);
310 return PTR_ERR(dentry);
313 /* Ensure umount returns EBUSY on umounts while this file is open. */
314 mntget(parfilp->f_path.mnt);
316 /* Create file pointer. */
317 filp = dentry_open(dentry, parfilp->f_path.mnt, hreq->oflags);
319 put_unused_fd(new_fd);
320 return -XFS_ERROR(-PTR_ERR(filp));
323 if (inode->i_mode & S_IFREG) {
324 /* invisible operation should not change atime */
325 filp->f_flags |= O_NOATIME;
326 filp->f_mode |= FMODE_NOCMTIME;
329 fd_install(new_fd, filp);
334 * This is a copy from fs/namei.c:vfs_readlink(), except for removing it's
335 * unused first argument.
350 if (len > (unsigned) buflen)
352 if (copy_to_user(buffer, link, len))
360 xfs_readlink_by_handle(
362 xfs_fsop_handlereq_t *hreq,
363 struct inode *parinode)
370 if (!capable(CAP_SYS_ADMIN))
371 return -XFS_ERROR(EPERM);
373 error = xfs_vget_fsop_handlereq(mp, parinode, hreq, &inode);
377 /* Restrict this handle operation to symlinks only. */
378 if (!S_ISLNK(inode->i_mode)) {
379 error = -XFS_ERROR(EINVAL);
383 if (copy_from_user(&olen, hreq->ohandlen, sizeof(__u32))) {
384 error = -XFS_ERROR(EFAULT);
388 link = kmalloc(MAXPATHLEN+1, GFP_KERNEL);
392 error = -xfs_readlink(XFS_I(inode), link);
395 error = do_readlink(hreq->ohandle, olen, link);
407 xfs_fssetdm_by_handle(
410 struct inode *parinode)
413 struct fsdmidata fsd;
414 xfs_fsop_setdm_handlereq_t dmhreq;
417 if (!capable(CAP_MKNOD))
418 return -XFS_ERROR(EPERM);
419 if (copy_from_user(&dmhreq, arg, sizeof(xfs_fsop_setdm_handlereq_t)))
420 return -XFS_ERROR(EFAULT);
422 error = xfs_vget_fsop_handlereq(mp, parinode, &dmhreq.hreq, &inode);
426 if (IS_IMMUTABLE(inode) || IS_APPEND(inode)) {
427 error = -XFS_ERROR(EPERM);
431 if (copy_from_user(&fsd, dmhreq.data, sizeof(fsd))) {
432 error = -XFS_ERROR(EFAULT);
436 error = -xfs_set_dmattrs(XFS_I(inode), fsd.fsd_dmevmask,
445 xfs_attrlist_by_handle(
448 struct inode *parinode)
451 attrlist_cursor_kern_t *cursor;
452 xfs_fsop_attrlist_handlereq_t al_hreq;
456 if (!capable(CAP_SYS_ADMIN))
457 return -XFS_ERROR(EPERM);
458 if (copy_from_user(&al_hreq, arg, sizeof(xfs_fsop_attrlist_handlereq_t)))
459 return -XFS_ERROR(EFAULT);
460 if (al_hreq.buflen > XATTR_LIST_MAX)
461 return -XFS_ERROR(EINVAL);
464 * Reject flags, only allow namespaces.
466 if (al_hreq.flags & ~(ATTR_ROOT | ATTR_SECURE))
467 return -XFS_ERROR(EINVAL);
469 error = xfs_vget_fsop_handlereq(mp, parinode, &al_hreq.hreq, &inode);
473 kbuf = kmalloc(al_hreq.buflen, GFP_KERNEL);
477 cursor = (attrlist_cursor_kern_t *)&al_hreq.pos;
478 error = xfs_attr_list(XFS_I(inode), kbuf, al_hreq.buflen,
479 al_hreq.flags, cursor);
483 if (copy_to_user(al_hreq.buffer, kbuf, al_hreq.buflen))
495 xfs_attrmulti_attr_get(
505 if (*len > XATTR_SIZE_MAX)
507 kbuf = kmalloc(*len, GFP_KERNEL);
511 error = xfs_attr_get(XFS_I(inode), name, kbuf, (int *)len, flags);
515 if (copy_to_user(ubuf, kbuf, *len))
524 xfs_attrmulti_attr_set(
527 const char __user *ubuf,
534 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
536 if (len > XATTR_SIZE_MAX)
539 kbuf = kmalloc(len, GFP_KERNEL);
543 if (copy_from_user(kbuf, ubuf, len))
546 error = xfs_attr_set(XFS_I(inode), name, kbuf, len, flags);
554 xfs_attrmulti_attr_remove(
559 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
561 return xfs_attr_remove(XFS_I(inode), name, flags);
565 xfs_attrmulti_by_handle(
568 struct file *parfilp,
569 struct inode *parinode)
572 xfs_attr_multiop_t *ops;
573 xfs_fsop_attrmulti_handlereq_t am_hreq;
575 unsigned int i, size;
578 if (!capable(CAP_SYS_ADMIN))
579 return -XFS_ERROR(EPERM);
580 if (copy_from_user(&am_hreq, arg, sizeof(xfs_fsop_attrmulti_handlereq_t)))
581 return -XFS_ERROR(EFAULT);
583 error = xfs_vget_fsop_handlereq(mp, parinode, &am_hreq.hreq, &inode);
588 size = am_hreq.opcount * sizeof(xfs_attr_multiop_t);
589 if (!size || size > 16 * PAGE_SIZE)
593 ops = kmalloc(size, GFP_KERNEL);
598 if (copy_from_user(ops, am_hreq.ops, size))
601 attr_name = kmalloc(MAXNAMELEN, GFP_KERNEL);
607 for (i = 0; i < am_hreq.opcount; i++) {
608 ops[i].am_error = strncpy_from_user(attr_name,
609 ops[i].am_attrname, MAXNAMELEN);
610 if (ops[i].am_error == 0 || ops[i].am_error == MAXNAMELEN)
612 if (ops[i].am_error < 0)
615 switch (ops[i].am_opcode) {
617 ops[i].am_error = xfs_attrmulti_attr_get(inode,
618 attr_name, ops[i].am_attrvalue,
619 &ops[i].am_length, ops[i].am_flags);
622 ops[i].am_error = mnt_want_write(parfilp->f_path.mnt);
625 ops[i].am_error = xfs_attrmulti_attr_set(inode,
626 attr_name, ops[i].am_attrvalue,
627 ops[i].am_length, ops[i].am_flags);
628 mnt_drop_write(parfilp->f_path.mnt);
631 ops[i].am_error = mnt_want_write(parfilp->f_path.mnt);
634 ops[i].am_error = xfs_attrmulti_attr_remove(inode,
635 attr_name, ops[i].am_flags);
636 mnt_drop_write(parfilp->f_path.mnt);
639 ops[i].am_error = EINVAL;
643 if (copy_to_user(am_hreq.ops, ops, size))
644 error = XFS_ERROR(EFAULT);
657 struct xfs_inode *ip,
668 * Only allow the sys admin to reserve space unless
669 * unwritten extents are enabled.
671 if (!xfs_sb_version_hasextflgbit(&ip->i_mount->m_sb) &&
672 !capable(CAP_SYS_ADMIN))
673 return -XFS_ERROR(EPERM);
675 if (inode->i_flags & (S_IMMUTABLE|S_APPEND))
676 return -XFS_ERROR(EPERM);
678 if (!(filp->f_mode & FMODE_WRITE))
679 return -XFS_ERROR(EBADF);
681 if (!S_ISREG(inode->i_mode))
682 return -XFS_ERROR(EINVAL);
684 if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
685 attr_flags |= XFS_ATTR_NONBLOCK;
686 if (ioflags & IO_INVIS)
687 attr_flags |= XFS_ATTR_DMI;
689 error = xfs_change_file_space(ip, cmd, bf, filp->f_pos, attr_flags);
699 xfs_fsop_bulkreq_t bulkreq;
700 int count; /* # of records returned */
701 xfs_ino_t inlast; /* last inode number */
705 /* done = 1 if there are more stats to get and if bulkstat */
706 /* should be called again (unused here, but used in dmapi) */
708 if (!capable(CAP_SYS_ADMIN))
711 if (XFS_FORCED_SHUTDOWN(mp))
712 return -XFS_ERROR(EIO);
714 if (copy_from_user(&bulkreq, arg, sizeof(xfs_fsop_bulkreq_t)))
715 return -XFS_ERROR(EFAULT);
717 if (copy_from_user(&inlast, bulkreq.lastip, sizeof(__s64)))
718 return -XFS_ERROR(EFAULT);
720 if ((count = bulkreq.icount) <= 0)
721 return -XFS_ERROR(EINVAL);
723 if (bulkreq.ubuffer == NULL)
724 return -XFS_ERROR(EINVAL);
726 if (cmd == XFS_IOC_FSINUMBERS)
727 error = xfs_inumbers(mp, &inlast, &count,
728 bulkreq.ubuffer, xfs_inumbers_fmt);
729 else if (cmd == XFS_IOC_FSBULKSTAT_SINGLE)
730 error = xfs_bulkstat_single(mp, &inlast,
731 bulkreq.ubuffer, &done);
732 else /* XFS_IOC_FSBULKSTAT */
733 error = xfs_bulkstat(mp, &inlast, &count,
734 (bulkstat_one_pf)xfs_bulkstat_one, NULL,
735 sizeof(xfs_bstat_t), bulkreq.ubuffer,
736 BULKSTAT_FG_QUICK, &done);
741 if (bulkreq.ocount != NULL) {
742 if (copy_to_user(bulkreq.lastip, &inlast,
744 return -XFS_ERROR(EFAULT);
746 if (copy_to_user(bulkreq.ocount, &count, sizeof(count)))
747 return -XFS_ERROR(EFAULT);
754 xfs_ioc_fsgeometry_v1(
758 xfs_fsop_geom_v1_t fsgeo;
761 error = xfs_fs_geometry(mp, (xfs_fsop_geom_t *)&fsgeo, 3);
765 if (copy_to_user(arg, &fsgeo, sizeof(fsgeo)))
766 return -XFS_ERROR(EFAULT);
775 xfs_fsop_geom_t fsgeo;
778 error = xfs_fs_geometry(mp, &fsgeo, 4);
782 if (copy_to_user(arg, &fsgeo, sizeof(fsgeo)))
783 return -XFS_ERROR(EFAULT);
788 * Linux extended inode flags interface.
792 xfs_merge_ioc_xflags(
796 unsigned int xflags = start;
798 if (flags & FS_IMMUTABLE_FL)
799 xflags |= XFS_XFLAG_IMMUTABLE;
801 xflags &= ~XFS_XFLAG_IMMUTABLE;
802 if (flags & FS_APPEND_FL)
803 xflags |= XFS_XFLAG_APPEND;
805 xflags &= ~XFS_XFLAG_APPEND;
806 if (flags & FS_SYNC_FL)
807 xflags |= XFS_XFLAG_SYNC;
809 xflags &= ~XFS_XFLAG_SYNC;
810 if (flags & FS_NOATIME_FL)
811 xflags |= XFS_XFLAG_NOATIME;
813 xflags &= ~XFS_XFLAG_NOATIME;
814 if (flags & FS_NODUMP_FL)
815 xflags |= XFS_XFLAG_NODUMP;
817 xflags &= ~XFS_XFLAG_NODUMP;
826 unsigned int flags = 0;
828 if (di_flags & XFS_DIFLAG_IMMUTABLE)
829 flags |= FS_IMMUTABLE_FL;
830 if (di_flags & XFS_DIFLAG_APPEND)
831 flags |= FS_APPEND_FL;
832 if (di_flags & XFS_DIFLAG_SYNC)
834 if (di_flags & XFS_DIFLAG_NOATIME)
835 flags |= FS_NOATIME_FL;
836 if (di_flags & XFS_DIFLAG_NODUMP)
837 flags |= FS_NODUMP_FL;
849 xfs_ilock(ip, XFS_ILOCK_SHARED);
850 fa.fsx_xflags = xfs_ip2xflags(ip);
851 fa.fsx_extsize = ip->i_d.di_extsize << ip->i_mount->m_sb.sb_blocklog;
852 fa.fsx_projid = ip->i_d.di_projid;
856 if (ip->i_afp->if_flags & XFS_IFEXTENTS)
857 fa.fsx_nextents = ip->i_afp->if_bytes /
858 sizeof(xfs_bmbt_rec_t);
860 fa.fsx_nextents = ip->i_d.di_anextents;
864 if (ip->i_df.if_flags & XFS_IFEXTENTS)
865 fa.fsx_nextents = ip->i_df.if_bytes /
866 sizeof(xfs_bmbt_rec_t);
868 fa.fsx_nextents = ip->i_d.di_nextents;
870 xfs_iunlock(ip, XFS_ILOCK_SHARED);
872 if (copy_to_user(arg, &fa, sizeof(fa)))
879 struct xfs_inode *ip,
882 unsigned int di_flags;
884 /* can't set PREALLOC this way, just preserve it */
885 di_flags = (ip->i_d.di_flags & XFS_DIFLAG_PREALLOC);
886 if (xflags & XFS_XFLAG_IMMUTABLE)
887 di_flags |= XFS_DIFLAG_IMMUTABLE;
888 if (xflags & XFS_XFLAG_APPEND)
889 di_flags |= XFS_DIFLAG_APPEND;
890 if (xflags & XFS_XFLAG_SYNC)
891 di_flags |= XFS_DIFLAG_SYNC;
892 if (xflags & XFS_XFLAG_NOATIME)
893 di_flags |= XFS_DIFLAG_NOATIME;
894 if (xflags & XFS_XFLAG_NODUMP)
895 di_flags |= XFS_DIFLAG_NODUMP;
896 if (xflags & XFS_XFLAG_PROJINHERIT)
897 di_flags |= XFS_DIFLAG_PROJINHERIT;
898 if (xflags & XFS_XFLAG_NODEFRAG)
899 di_flags |= XFS_DIFLAG_NODEFRAG;
900 if (xflags & XFS_XFLAG_FILESTREAM)
901 di_flags |= XFS_DIFLAG_FILESTREAM;
902 if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
903 if (xflags & XFS_XFLAG_RTINHERIT)
904 di_flags |= XFS_DIFLAG_RTINHERIT;
905 if (xflags & XFS_XFLAG_NOSYMLINKS)
906 di_flags |= XFS_DIFLAG_NOSYMLINKS;
907 if (xflags & XFS_XFLAG_EXTSZINHERIT)
908 di_flags |= XFS_DIFLAG_EXTSZINHERIT;
909 } else if ((ip->i_d.di_mode & S_IFMT) == S_IFREG) {
910 if (xflags & XFS_XFLAG_REALTIME)
911 di_flags |= XFS_DIFLAG_REALTIME;
912 if (xflags & XFS_XFLAG_EXTSIZE)
913 di_flags |= XFS_DIFLAG_EXTSIZE;
916 ip->i_d.di_flags = di_flags;
920 xfs_diflags_to_linux(
921 struct xfs_inode *ip)
923 struct inode *inode = VFS_I(ip);
924 unsigned int xflags = xfs_ip2xflags(ip);
926 if (xflags & XFS_XFLAG_IMMUTABLE)
927 inode->i_flags |= S_IMMUTABLE;
929 inode->i_flags &= ~S_IMMUTABLE;
930 if (xflags & XFS_XFLAG_APPEND)
931 inode->i_flags |= S_APPEND;
933 inode->i_flags &= ~S_APPEND;
934 if (xflags & XFS_XFLAG_SYNC)
935 inode->i_flags |= S_SYNC;
937 inode->i_flags &= ~S_SYNC;
938 if (xflags & XFS_XFLAG_NOATIME)
939 inode->i_flags |= S_NOATIME;
941 inode->i_flags &= ~S_NOATIME;
945 #define FSX_EXTSIZE 2
947 #define FSX_NONBLOCK 8
955 struct xfs_mount *mp = ip->i_mount;
956 struct xfs_trans *tp;
957 unsigned int lock_flags = 0;
958 struct xfs_dquot *udqp = NULL, *gdqp = NULL;
959 struct xfs_dquot *olddquot = NULL;
962 xfs_itrace_entry(ip);
964 if (mp->m_flags & XFS_MOUNT_RDONLY)
965 return XFS_ERROR(EROFS);
966 if (XFS_FORCED_SHUTDOWN(mp))
967 return XFS_ERROR(EIO);
970 * If disk quotas is on, we make sure that the dquots do exist on disk,
971 * before we start any other transactions. Trying to do this later
972 * is messy. We don't care to take a readlock to look at the ids
973 * in inode here, because we can't hold it across the trans_reserve.
974 * If the IDs do change before we take the ilock, we're covered
975 * because the i_*dquot fields will get updated anyway.
977 if (XFS_IS_QUOTA_ON(mp) && (mask & FSX_PROJID)) {
978 code = XFS_QM_DQVOPALLOC(mp, ip, ip->i_d.di_uid,
979 ip->i_d.di_gid, fa->fsx_projid,
980 XFS_QMOPT_PQUOTA, &udqp, &gdqp);
986 * For the other attributes, we acquire the inode lock and
987 * first do an error checking pass.
989 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
990 code = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES(mp), 0, 0, 0);
994 lock_flags = XFS_ILOCK_EXCL;
995 xfs_ilock(ip, lock_flags);
998 * CAP_FOWNER overrides the following restrictions:
1000 * The user ID of the calling process must be equal
1001 * to the file owner ID, except in cases where the
1002 * CAP_FSETID capability is applicable.
1004 if (current_fsuid() != ip->i_d.di_uid && !capable(CAP_FOWNER)) {
1005 code = XFS_ERROR(EPERM);
1010 * Do a quota reservation only if projid is actually going to change.
1012 if (mask & FSX_PROJID) {
1013 if (XFS_IS_PQUOTA_ON(mp) &&
1014 ip->i_d.di_projid != fa->fsx_projid) {
1016 code = XFS_QM_DQVOPCHOWNRESV(mp, tp, ip, udqp, gdqp,
1017 capable(CAP_FOWNER) ?
1018 XFS_QMOPT_FORCE_RES : 0);
1019 if (code) /* out of quota */
1024 if (mask & FSX_EXTSIZE) {
1026 * Can't change extent size if any extents are allocated.
1028 if (ip->i_d.di_nextents &&
1029 ((ip->i_d.di_extsize << mp->m_sb.sb_blocklog) !=
1031 code = XFS_ERROR(EINVAL); /* EFBIG? */
1036 * Extent size must be a multiple of the appropriate block
1037 * size, if set at all.
1039 if (fa->fsx_extsize != 0) {
1042 if (XFS_IS_REALTIME_INODE(ip) ||
1043 ((mask & FSX_XFLAGS) &&
1044 (fa->fsx_xflags & XFS_XFLAG_REALTIME))) {
1045 size = mp->m_sb.sb_rextsize <<
1046 mp->m_sb.sb_blocklog;
1048 size = mp->m_sb.sb_blocksize;
1051 if (fa->fsx_extsize % size) {
1052 code = XFS_ERROR(EINVAL);
1059 if (mask & FSX_XFLAGS) {
1061 * Can't change realtime flag if any extents are allocated.
1063 if ((ip->i_d.di_nextents || ip->i_delayed_blks) &&
1064 (XFS_IS_REALTIME_INODE(ip)) !=
1065 (fa->fsx_xflags & XFS_XFLAG_REALTIME)) {
1066 code = XFS_ERROR(EINVAL); /* EFBIG? */
1071 * If realtime flag is set then must have realtime data.
1073 if ((fa->fsx_xflags & XFS_XFLAG_REALTIME)) {
1074 if ((mp->m_sb.sb_rblocks == 0) ||
1075 (mp->m_sb.sb_rextsize == 0) ||
1076 (ip->i_d.di_extsize % mp->m_sb.sb_rextsize)) {
1077 code = XFS_ERROR(EINVAL);
1083 * Can't modify an immutable/append-only file unless
1084 * we have appropriate permission.
1086 if ((ip->i_d.di_flags &
1087 (XFS_DIFLAG_IMMUTABLE|XFS_DIFLAG_APPEND) ||
1089 (XFS_XFLAG_IMMUTABLE | XFS_XFLAG_APPEND))) &&
1090 !capable(CAP_LINUX_IMMUTABLE)) {
1091 code = XFS_ERROR(EPERM);
1096 xfs_trans_ijoin(tp, ip, lock_flags);
1097 xfs_trans_ihold(tp, ip);
1100 * Change file ownership. Must be the owner or privileged.
1102 if (mask & FSX_PROJID) {
1104 * CAP_FSETID overrides the following restrictions:
1106 * The set-user-ID and set-group-ID bits of a file will be
1107 * cleared upon successful return from chown()
1109 if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
1110 !capable(CAP_FSETID))
1111 ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
1114 * Change the ownerships and register quota modifications
1115 * in the transaction.
1117 if (ip->i_d.di_projid != fa->fsx_projid) {
1118 if (XFS_IS_PQUOTA_ON(mp)) {
1119 olddquot = XFS_QM_DQVOPCHOWN(mp, tp, ip,
1120 &ip->i_gdquot, gdqp);
1122 ip->i_d.di_projid = fa->fsx_projid;
1125 * We may have to rev the inode as well as
1126 * the superblock version number since projids didn't
1127 * exist before DINODE_VERSION_2 and SB_VERSION_NLINK.
1129 if (ip->i_d.di_version == 1)
1130 xfs_bump_ino_vers2(tp, ip);
1135 if (mask & FSX_EXTSIZE)
1136 ip->i_d.di_extsize = fa->fsx_extsize >> mp->m_sb.sb_blocklog;
1137 if (mask & FSX_XFLAGS) {
1138 xfs_set_diflags(ip, fa->fsx_xflags);
1139 xfs_diflags_to_linux(ip);
1142 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1143 xfs_ichgtime(ip, XFS_ICHGTIME_CHG);
1145 XFS_STATS_INC(xs_ig_attrchg);
1148 * If this is a synchronous mount, make sure that the
1149 * transaction goes to disk before returning to the user.
1150 * This is slightly sub-optimal in that truncates require
1151 * two sync transactions instead of one for wsync filesystems.
1152 * One for the truncate and one for the timestamps since we
1153 * don't want to change the timestamps unless we're sure the
1154 * truncate worked. Truncates are less than 1% of the laddis
1155 * mix so this probably isn't worth the trouble to optimize.
1157 if (mp->m_flags & XFS_MOUNT_WSYNC)
1158 xfs_trans_set_sync(tp);
1159 code = xfs_trans_commit(tp, 0);
1160 xfs_iunlock(ip, lock_flags);
1163 * Release any dquot(s) the inode had kept before chown.
1165 XFS_QM_DQRELE(mp, olddquot);
1166 XFS_QM_DQRELE(mp, udqp);
1167 XFS_QM_DQRELE(mp, gdqp);
1172 if (DM_EVENT_ENABLED(ip, DM_EVENT_ATTRIBUTE)) {
1173 XFS_SEND_NAMESP(mp, DM_EVENT_ATTRIBUTE, ip, DM_RIGHT_NULL,
1174 NULL, DM_RIGHT_NULL, NULL, NULL, 0, 0,
1175 (mask & FSX_NONBLOCK) ? DM_FLAGS_NDELAY : 0);
1181 XFS_QM_DQRELE(mp, udqp);
1182 XFS_QM_DQRELE(mp, gdqp);
1183 xfs_trans_cancel(tp, 0);
1185 xfs_iunlock(ip, lock_flags);
1198 if (copy_from_user(&fa, arg, sizeof(fa)))
1201 mask = FSX_XFLAGS | FSX_EXTSIZE | FSX_PROJID;
1202 if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
1203 mask |= FSX_NONBLOCK;
1205 return -xfs_ioctl_setattr(ip, &fa, mask);
1215 flags = xfs_di2lxflags(ip->i_d.di_flags);
1216 if (copy_to_user(arg, &flags, sizeof(flags)))
1231 if (copy_from_user(&flags, arg, sizeof(flags)))
1234 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
1235 FS_NOATIME_FL | FS_NODUMP_FL | \
1240 if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
1241 mask |= FSX_NONBLOCK;
1242 fa.fsx_xflags = xfs_merge_ioc_xflags(flags, xfs_ip2xflags(ip));
1244 return -xfs_ioctl_setattr(ip, &fa, mask);
1248 xfs_getbmap_format(void **ap, struct getbmapx *bmv, int *full)
1250 struct getbmap __user *base = *ap;
1252 /* copy only getbmap portion (not getbmapx) */
1253 if (copy_to_user(base, bmv, sizeof(struct getbmap)))
1254 return XFS_ERROR(EFAULT);
1256 *ap += sizeof(struct getbmap);
1262 struct xfs_inode *ip,
1267 struct getbmapx bmx;
1270 if (copy_from_user(&bmx, arg, sizeof(struct getbmapx)))
1271 return -XFS_ERROR(EFAULT);
1273 if (bmx.bmv_count < 2)
1274 return -XFS_ERROR(EINVAL);
1276 bmx.bmv_iflags = (cmd == XFS_IOC_GETBMAPA ? BMV_IF_ATTRFORK : 0);
1277 if (ioflags & IO_INVIS)
1278 bmx.bmv_iflags |= BMV_IF_NO_DMAPI_READ;
1280 error = xfs_getbmap(ip, &bmx, xfs_getbmap_format,
1281 (struct getbmap *)arg+1);
1285 /* copy back header - only size of getbmap */
1286 if (copy_to_user(arg, &bmx, sizeof(struct getbmap)))
1287 return -XFS_ERROR(EFAULT);
1292 xfs_getbmapx_format(void **ap, struct getbmapx *bmv, int *full)
1294 struct getbmapx __user *base = *ap;
1296 if (copy_to_user(base, bmv, sizeof(struct getbmapx)))
1297 return XFS_ERROR(EFAULT);
1299 *ap += sizeof(struct getbmapx);
1305 struct xfs_inode *ip,
1308 struct getbmapx bmx;
1311 if (copy_from_user(&bmx, arg, sizeof(bmx)))
1312 return -XFS_ERROR(EFAULT);
1314 if (bmx.bmv_count < 2)
1315 return -XFS_ERROR(EINVAL);
1317 if (bmx.bmv_iflags & (~BMV_IF_VALID))
1318 return -XFS_ERROR(EINVAL);
1320 error = xfs_getbmap(ip, &bmx, xfs_getbmapx_format,
1321 (struct getbmapx *)arg+1);
1325 /* copy back header */
1326 if (copy_to_user(arg, &bmx, sizeof(struct getbmapx)))
1327 return -XFS_ERROR(EFAULT);
1333 * Note: some of the ioctl's return positive numbers as a
1334 * byte count indicating success, such as readlink_by_handle.
1335 * So we don't "sign flip" like most other routines. This means
1336 * true errors need to be returned as a negative value.
1344 struct inode *inode = filp->f_path.dentry->d_inode;
1345 struct xfs_inode *ip = XFS_I(inode);
1346 struct xfs_mount *mp = ip->i_mount;
1347 void __user *arg = (void __user *)p;
1351 if (filp->f_mode & FMODE_NOCMTIME)
1352 ioflags |= IO_INVIS;
1354 xfs_itrace_entry(ip);
1357 case XFS_IOC_ALLOCSP:
1358 case XFS_IOC_FREESP:
1359 case XFS_IOC_RESVSP:
1360 case XFS_IOC_UNRESVSP:
1361 case XFS_IOC_ALLOCSP64:
1362 case XFS_IOC_FREESP64:
1363 case XFS_IOC_RESVSP64:
1364 case XFS_IOC_UNRESVSP64: {
1367 if (copy_from_user(&bf, arg, sizeof(bf)))
1368 return -XFS_ERROR(EFAULT);
1369 return xfs_ioc_space(ip, inode, filp, ioflags, cmd, &bf);
1371 case XFS_IOC_DIOINFO: {
1373 xfs_buftarg_t *target =
1374 XFS_IS_REALTIME_INODE(ip) ?
1375 mp->m_rtdev_targp : mp->m_ddev_targp;
1377 da.d_mem = da.d_miniosz = 1 << target->bt_sshift;
1378 da.d_maxiosz = INT_MAX & ~(da.d_miniosz - 1);
1380 if (copy_to_user(arg, &da, sizeof(da)))
1381 return -XFS_ERROR(EFAULT);
1385 case XFS_IOC_FSBULKSTAT_SINGLE:
1386 case XFS_IOC_FSBULKSTAT:
1387 case XFS_IOC_FSINUMBERS:
1388 return xfs_ioc_bulkstat(mp, cmd, arg);
1390 case XFS_IOC_FSGEOMETRY_V1:
1391 return xfs_ioc_fsgeometry_v1(mp, arg);
1393 case XFS_IOC_FSGEOMETRY:
1394 return xfs_ioc_fsgeometry(mp, arg);
1396 case XFS_IOC_GETVERSION:
1397 return put_user(inode->i_generation, (int __user *)arg);
1399 case XFS_IOC_FSGETXATTR:
1400 return xfs_ioc_fsgetxattr(ip, 0, arg);
1401 case XFS_IOC_FSGETXATTRA:
1402 return xfs_ioc_fsgetxattr(ip, 1, arg);
1403 case XFS_IOC_FSSETXATTR:
1404 return xfs_ioc_fssetxattr(ip, filp, arg);
1405 case XFS_IOC_GETXFLAGS:
1406 return xfs_ioc_getxflags(ip, arg);
1407 case XFS_IOC_SETXFLAGS:
1408 return xfs_ioc_setxflags(ip, filp, arg);
1410 case XFS_IOC_FSSETDM: {
1411 struct fsdmidata dmi;
1413 if (copy_from_user(&dmi, arg, sizeof(dmi)))
1414 return -XFS_ERROR(EFAULT);
1416 error = xfs_set_dmattrs(ip, dmi.fsd_dmevmask,
1421 case XFS_IOC_GETBMAP:
1422 case XFS_IOC_GETBMAPA:
1423 return xfs_ioc_getbmap(ip, ioflags, cmd, arg);
1425 case XFS_IOC_GETBMAPX:
1426 return xfs_ioc_getbmapx(ip, arg);
1428 case XFS_IOC_FD_TO_HANDLE:
1429 case XFS_IOC_PATH_TO_HANDLE:
1430 case XFS_IOC_PATH_TO_FSHANDLE: {
1431 xfs_fsop_handlereq_t hreq;
1433 if (copy_from_user(&hreq, arg, sizeof(hreq)))
1434 return -XFS_ERROR(EFAULT);
1435 return xfs_find_handle(cmd, &hreq);
1437 case XFS_IOC_OPEN_BY_HANDLE: {
1438 xfs_fsop_handlereq_t hreq;
1440 if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
1441 return -XFS_ERROR(EFAULT);
1442 return xfs_open_by_handle(mp, &hreq, filp, inode);
1444 case XFS_IOC_FSSETDM_BY_HANDLE:
1445 return xfs_fssetdm_by_handle(mp, arg, inode);
1447 case XFS_IOC_READLINK_BY_HANDLE: {
1448 xfs_fsop_handlereq_t hreq;
1450 if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
1451 return -XFS_ERROR(EFAULT);
1452 return xfs_readlink_by_handle(mp, &hreq, inode);
1454 case XFS_IOC_ATTRLIST_BY_HANDLE:
1455 return xfs_attrlist_by_handle(mp, arg, inode);
1457 case XFS_IOC_ATTRMULTI_BY_HANDLE:
1458 return xfs_attrmulti_by_handle(mp, arg, filp, inode);
1460 case XFS_IOC_SWAPEXT: {
1461 struct xfs_swapext sxp;
1463 if (copy_from_user(&sxp, arg, sizeof(xfs_swapext_t)))
1464 return -XFS_ERROR(EFAULT);
1465 error = xfs_swapext(&sxp);
1469 case XFS_IOC_FSCOUNTS: {
1470 xfs_fsop_counts_t out;
1472 error = xfs_fs_counts(mp, &out);
1476 if (copy_to_user(arg, &out, sizeof(out)))
1477 return -XFS_ERROR(EFAULT);
1481 case XFS_IOC_SET_RESBLKS: {
1482 xfs_fsop_resblks_t inout;
1485 if (!capable(CAP_SYS_ADMIN))
1488 if (copy_from_user(&inout, arg, sizeof(inout)))
1489 return -XFS_ERROR(EFAULT);
1491 /* input parameter is passed in resblks field of structure */
1493 error = xfs_reserve_blocks(mp, &in, &inout);
1497 if (copy_to_user(arg, &inout, sizeof(inout)))
1498 return -XFS_ERROR(EFAULT);
1502 case XFS_IOC_GET_RESBLKS: {
1503 xfs_fsop_resblks_t out;
1505 if (!capable(CAP_SYS_ADMIN))
1508 error = xfs_reserve_blocks(mp, NULL, &out);
1512 if (copy_to_user(arg, &out, sizeof(out)))
1513 return -XFS_ERROR(EFAULT);
1518 case XFS_IOC_FSGROWFSDATA: {
1519 xfs_growfs_data_t in;
1521 if (copy_from_user(&in, arg, sizeof(in)))
1522 return -XFS_ERROR(EFAULT);
1524 error = xfs_growfs_data(mp, &in);
1528 case XFS_IOC_FSGROWFSLOG: {
1529 xfs_growfs_log_t in;
1531 if (copy_from_user(&in, arg, sizeof(in)))
1532 return -XFS_ERROR(EFAULT);
1534 error = xfs_growfs_log(mp, &in);
1538 case XFS_IOC_FSGROWFSRT: {
1541 if (copy_from_user(&in, arg, sizeof(in)))
1542 return -XFS_ERROR(EFAULT);
1544 error = xfs_growfs_rt(mp, &in);
1548 case XFS_IOC_FREEZE:
1549 if (!capable(CAP_SYS_ADMIN))
1552 if (inode->i_sb->s_frozen == SB_UNFROZEN)
1553 freeze_bdev(inode->i_sb->s_bdev);
1557 if (!capable(CAP_SYS_ADMIN))
1559 if (inode->i_sb->s_frozen != SB_UNFROZEN)
1560 thaw_bdev(inode->i_sb->s_bdev, inode->i_sb);
1563 case XFS_IOC_GOINGDOWN: {
1566 if (!capable(CAP_SYS_ADMIN))
1569 if (get_user(in, (__uint32_t __user *)arg))
1570 return -XFS_ERROR(EFAULT);
1572 error = xfs_fs_goingdown(mp, in);
1576 case XFS_IOC_ERROR_INJECTION: {
1577 xfs_error_injection_t in;
1579 if (!capable(CAP_SYS_ADMIN))
1582 if (copy_from_user(&in, arg, sizeof(in)))
1583 return -XFS_ERROR(EFAULT);
1585 error = xfs_errortag_add(in.errtag, mp);
1589 case XFS_IOC_ERROR_CLEARALL:
1590 if (!capable(CAP_SYS_ADMIN))
1593 error = xfs_errortag_clearall(mp, 1);