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_ioctl.h"
38 #include "xfs_btree.h"
39 #include "xfs_ialloc.h"
40 #include "xfs_rtalloc.h"
41 #include "xfs_itable.h"
42 #include "xfs_error.h"
47 #include "xfs_buf_item.h"
48 #include "xfs_utils.h"
49 #include "xfs_dfrag.h"
50 #include "xfs_fsops.h"
51 #include "xfs_vnodeops.h"
52 #include "xfs_quota.h"
53 #include "xfs_inode_item.h"
54 #include "xfs_export.h"
56 #include <linux/capability.h>
57 #include <linux/dcache.h>
58 #include <linux/mount.h>
59 #include <linux/namei.h>
60 #include <linux/pagemap.h>
61 #include <linux/exportfs.h>
64 * xfs_find_handle maps from userspace xfs_fsop_handlereq structure to
65 * a file or fs handle.
67 * XFS_IOC_PATH_TO_FSHANDLE
68 * returns fs handle for a mount point or path within that mount point
69 * XFS_IOC_FD_TO_HANDLE
70 * returns full handle for a FD opened in user space
71 * XFS_IOC_PATH_TO_HANDLE
72 * returns full handle for a path
77 xfs_fsop_handlereq_t *hreq)
82 struct file *file = NULL;
87 if (cmd == XFS_IOC_FD_TO_HANDLE) {
88 file = fget(hreq->fd);
91 inode = file->f_path.dentry->d_inode;
93 error = user_lpath((const char __user *)hreq->path, &path);
96 inode = path.dentry->d_inode;
101 * We can only generate handles for inodes residing on a XFS filesystem,
102 * and only for regular files, directories or symbolic links.
105 if (inode->i_sb->s_magic != XFS_SB_MAGIC)
109 if (!S_ISREG(inode->i_mode) &&
110 !S_ISDIR(inode->i_mode) &&
111 !S_ISLNK(inode->i_mode))
115 memcpy(&handle.ha_fsid, ip->i_mount->m_fixedfsid, sizeof(xfs_fsid_t));
117 if (cmd == XFS_IOC_PATH_TO_FSHANDLE) {
119 * This handle only contains an fsid, zero the rest.
121 memset(&handle.ha_fid, 0, sizeof(handle.ha_fid));
122 hsize = sizeof(xfs_fsid_t);
126 lock_mode = xfs_ilock_map_shared(ip);
127 handle.ha_fid.fid_len = sizeof(xfs_fid_t) -
128 sizeof(handle.ha_fid.fid_len);
129 handle.ha_fid.fid_pad = 0;
130 handle.ha_fid.fid_gen = ip->i_d.di_gen;
131 handle.ha_fid.fid_ino = ip->i_ino;
132 xfs_iunlock_map_shared(ip, lock_mode);
134 hsize = XFS_HSIZE(handle);
138 if (copy_to_user(hreq->ohandle, &handle, hsize) ||
139 copy_to_user(hreq->ohandlen, &hsize, sizeof(__s32)))
145 if (cmd == XFS_IOC_FD_TO_HANDLE)
153 * No need to do permission checks on the various pathname components
154 * as the handle operations are privileged.
157 xfs_handle_acceptable(
159 struct dentry *dentry)
165 * Convert userspace handle data into a dentry.
168 xfs_handle_to_dentry(
169 struct file *parfilp,
170 void __user *uhandle,
174 struct xfs_fid64 fid;
177 * Only allow handle opens under a directory.
179 if (!S_ISDIR(parfilp->f_path.dentry->d_inode->i_mode))
180 return ERR_PTR(-ENOTDIR);
182 if (hlen != sizeof(xfs_handle_t))
183 return ERR_PTR(-EINVAL);
184 if (copy_from_user(&handle, uhandle, hlen))
185 return ERR_PTR(-EFAULT);
186 if (handle.ha_fid.fid_len !=
187 sizeof(handle.ha_fid) - sizeof(handle.ha_fid.fid_len))
188 return ERR_PTR(-EINVAL);
190 memset(&fid, 0, sizeof(struct fid));
191 fid.ino = handle.ha_fid.fid_ino;
192 fid.gen = handle.ha_fid.fid_gen;
194 return exportfs_decode_fh(parfilp->f_path.mnt, (struct fid *)&fid, 3,
195 FILEID_INO32_GEN | XFS_FILEID_TYPE_64FLAG,
196 xfs_handle_acceptable, NULL);
199 STATIC struct dentry *
200 xfs_handlereq_to_dentry(
201 struct file *parfilp,
202 xfs_fsop_handlereq_t *hreq)
204 return xfs_handle_to_dentry(parfilp, hreq->ihandle, hreq->ihandlen);
209 struct file *parfilp,
210 xfs_fsop_handlereq_t *hreq)
212 const struct cred *cred = current_cred();
218 struct dentry *dentry;
220 if (!capable(CAP_SYS_ADMIN))
221 return -XFS_ERROR(EPERM);
223 dentry = xfs_handlereq_to_dentry(parfilp, hreq);
225 return PTR_ERR(dentry);
226 inode = dentry->d_inode;
228 /* Restrict xfs_open_by_handle to directories & regular files. */
229 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode))) {
230 error = -XFS_ERROR(EPERM);
234 #if BITS_PER_LONG != 32
235 hreq->oflags |= O_LARGEFILE;
238 /* Put open permission in namei format. */
239 permflag = hreq->oflags;
240 if ((permflag+1) & O_ACCMODE)
242 if (permflag & O_TRUNC)
245 if ((!(permflag & O_APPEND) || (permflag & O_TRUNC)) &&
246 (permflag & FMODE_WRITE) && IS_APPEND(inode)) {
247 error = -XFS_ERROR(EPERM);
251 if ((permflag & FMODE_WRITE) && IS_IMMUTABLE(inode)) {
252 error = -XFS_ERROR(EACCES);
256 /* Can't write directories. */
257 if (S_ISDIR(inode->i_mode) && (permflag & FMODE_WRITE)) {
258 error = -XFS_ERROR(EISDIR);
262 fd = get_unused_fd();
268 filp = dentry_open(dentry, mntget(parfilp->f_path.mnt),
272 return PTR_ERR(filp);
275 if (inode->i_mode & S_IFREG) {
276 filp->f_flags |= O_NOATIME;
277 filp->f_mode |= FMODE_NOCMTIME;
280 fd_install(fd, filp);
289 * This is a copy from fs/namei.c:vfs_readlink(), except for removing it's
290 * unused first argument.
305 if (len > (unsigned) buflen)
307 if (copy_to_user(buffer, link, len))
315 xfs_readlink_by_handle(
316 struct file *parfilp,
317 xfs_fsop_handlereq_t *hreq)
319 struct dentry *dentry;
324 if (!capable(CAP_SYS_ADMIN))
325 return -XFS_ERROR(EPERM);
327 dentry = xfs_handlereq_to_dentry(parfilp, hreq);
329 return PTR_ERR(dentry);
331 /* Restrict this handle operation to symlinks only. */
332 if (!S_ISLNK(dentry->d_inode->i_mode)) {
333 error = -XFS_ERROR(EINVAL);
337 if (copy_from_user(&olen, hreq->ohandlen, sizeof(__u32))) {
338 error = -XFS_ERROR(EFAULT);
342 link = kmalloc(MAXPATHLEN+1, GFP_KERNEL);
344 error = -XFS_ERROR(ENOMEM);
348 error = -xfs_readlink(XFS_I(dentry->d_inode), link);
351 error = do_readlink(hreq->ohandle, olen, link);
363 xfs_fssetdm_by_handle(
364 struct file *parfilp,
368 struct fsdmidata fsd;
369 xfs_fsop_setdm_handlereq_t dmhreq;
370 struct dentry *dentry;
372 if (!capable(CAP_MKNOD))
373 return -XFS_ERROR(EPERM);
374 if (copy_from_user(&dmhreq, arg, sizeof(xfs_fsop_setdm_handlereq_t)))
375 return -XFS_ERROR(EFAULT);
377 dentry = xfs_handlereq_to_dentry(parfilp, &dmhreq.hreq);
379 return PTR_ERR(dentry);
381 if (IS_IMMUTABLE(dentry->d_inode) || IS_APPEND(dentry->d_inode)) {
382 error = -XFS_ERROR(EPERM);
386 if (copy_from_user(&fsd, dmhreq.data, sizeof(fsd))) {
387 error = -XFS_ERROR(EFAULT);
391 error = -xfs_set_dmattrs(XFS_I(dentry->d_inode), fsd.fsd_dmevmask,
400 xfs_attrlist_by_handle(
401 struct file *parfilp,
405 attrlist_cursor_kern_t *cursor;
406 xfs_fsop_attrlist_handlereq_t al_hreq;
407 struct dentry *dentry;
410 if (!capable(CAP_SYS_ADMIN))
411 return -XFS_ERROR(EPERM);
412 if (copy_from_user(&al_hreq, arg, sizeof(xfs_fsop_attrlist_handlereq_t)))
413 return -XFS_ERROR(EFAULT);
414 if (al_hreq.buflen > XATTR_LIST_MAX)
415 return -XFS_ERROR(EINVAL);
418 * Reject flags, only allow namespaces.
420 if (al_hreq.flags & ~(ATTR_ROOT | ATTR_SECURE))
421 return -XFS_ERROR(EINVAL);
423 dentry = xfs_handlereq_to_dentry(parfilp, &al_hreq.hreq);
425 return PTR_ERR(dentry);
427 kbuf = kmalloc(al_hreq.buflen, GFP_KERNEL);
431 cursor = (attrlist_cursor_kern_t *)&al_hreq.pos;
432 error = -xfs_attr_list(XFS_I(dentry->d_inode), kbuf, al_hreq.buflen,
433 al_hreq.flags, cursor);
437 if (copy_to_user(al_hreq.buffer, kbuf, al_hreq.buflen))
448 xfs_attrmulti_attr_get(
458 if (*len > XATTR_SIZE_MAX)
460 kbuf = kmalloc(*len, GFP_KERNEL);
464 error = xfs_attr_get(XFS_I(inode), name, kbuf, (int *)len, flags);
468 if (copy_to_user(ubuf, kbuf, *len))
477 xfs_attrmulti_attr_set(
480 const char __user *ubuf,
487 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
489 if (len > XATTR_SIZE_MAX)
492 kbuf = kmalloc(len, GFP_KERNEL);
496 if (copy_from_user(kbuf, ubuf, len))
499 error = xfs_attr_set(XFS_I(inode), name, kbuf, len, flags);
507 xfs_attrmulti_attr_remove(
512 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
514 return xfs_attr_remove(XFS_I(inode), name, flags);
518 xfs_attrmulti_by_handle(
519 struct file *parfilp,
523 xfs_attr_multiop_t *ops;
524 xfs_fsop_attrmulti_handlereq_t am_hreq;
525 struct dentry *dentry;
526 unsigned int i, size;
529 if (!capable(CAP_SYS_ADMIN))
530 return -XFS_ERROR(EPERM);
531 if (copy_from_user(&am_hreq, arg, sizeof(xfs_fsop_attrmulti_handlereq_t)))
532 return -XFS_ERROR(EFAULT);
534 dentry = xfs_handlereq_to_dentry(parfilp, &am_hreq.hreq);
536 return PTR_ERR(dentry);
539 size = am_hreq.opcount * sizeof(xfs_attr_multiop_t);
540 if (!size || size > 16 * PAGE_SIZE)
544 ops = kmalloc(size, GFP_KERNEL);
549 if (copy_from_user(ops, am_hreq.ops, size))
552 attr_name = kmalloc(MAXNAMELEN, GFP_KERNEL);
558 for (i = 0; i < am_hreq.opcount; i++) {
559 ops[i].am_error = strncpy_from_user(attr_name,
560 ops[i].am_attrname, MAXNAMELEN);
561 if (ops[i].am_error == 0 || ops[i].am_error == MAXNAMELEN)
563 if (ops[i].am_error < 0)
566 switch (ops[i].am_opcode) {
568 ops[i].am_error = xfs_attrmulti_attr_get(
569 dentry->d_inode, attr_name,
570 ops[i].am_attrvalue, &ops[i].am_length,
574 ops[i].am_error = mnt_want_write(parfilp->f_path.mnt);
577 ops[i].am_error = xfs_attrmulti_attr_set(
578 dentry->d_inode, attr_name,
579 ops[i].am_attrvalue, ops[i].am_length,
581 mnt_drop_write(parfilp->f_path.mnt);
584 ops[i].am_error = mnt_want_write(parfilp->f_path.mnt);
587 ops[i].am_error = xfs_attrmulti_attr_remove(
588 dentry->d_inode, attr_name,
590 mnt_drop_write(parfilp->f_path.mnt);
593 ops[i].am_error = EINVAL;
597 if (copy_to_user(am_hreq.ops, ops, size))
598 error = XFS_ERROR(EFAULT);
610 struct xfs_inode *ip,
621 * Only allow the sys admin to reserve space unless
622 * unwritten extents are enabled.
624 if (!xfs_sb_version_hasextflgbit(&ip->i_mount->m_sb) &&
625 !capable(CAP_SYS_ADMIN))
626 return -XFS_ERROR(EPERM);
628 if (inode->i_flags & (S_IMMUTABLE|S_APPEND))
629 return -XFS_ERROR(EPERM);
631 if (!(filp->f_mode & FMODE_WRITE))
632 return -XFS_ERROR(EBADF);
634 if (!S_ISREG(inode->i_mode))
635 return -XFS_ERROR(EINVAL);
637 if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
638 attr_flags |= XFS_ATTR_NONBLOCK;
639 if (ioflags & IO_INVIS)
640 attr_flags |= XFS_ATTR_DMI;
642 error = xfs_change_file_space(ip, cmd, bf, filp->f_pos, attr_flags);
652 xfs_fsop_bulkreq_t bulkreq;
653 int count; /* # of records returned */
654 xfs_ino_t inlast; /* last inode number */
658 /* done = 1 if there are more stats to get and if bulkstat */
659 /* should be called again (unused here, but used in dmapi) */
661 if (!capable(CAP_SYS_ADMIN))
664 if (XFS_FORCED_SHUTDOWN(mp))
665 return -XFS_ERROR(EIO);
667 if (copy_from_user(&bulkreq, arg, sizeof(xfs_fsop_bulkreq_t)))
668 return -XFS_ERROR(EFAULT);
670 if (copy_from_user(&inlast, bulkreq.lastip, sizeof(__s64)))
671 return -XFS_ERROR(EFAULT);
673 if ((count = bulkreq.icount) <= 0)
674 return -XFS_ERROR(EINVAL);
676 if (bulkreq.ubuffer == NULL)
677 return -XFS_ERROR(EINVAL);
679 if (cmd == XFS_IOC_FSINUMBERS)
680 error = xfs_inumbers(mp, &inlast, &count,
681 bulkreq.ubuffer, xfs_inumbers_fmt);
682 else if (cmd == XFS_IOC_FSBULKSTAT_SINGLE)
683 error = xfs_bulkstat_single(mp, &inlast,
684 bulkreq.ubuffer, &done);
685 else /* XFS_IOC_FSBULKSTAT */
686 error = xfs_bulkstat(mp, &inlast, &count,
687 (bulkstat_one_pf)xfs_bulkstat_one, NULL,
688 sizeof(xfs_bstat_t), bulkreq.ubuffer,
689 BULKSTAT_FG_QUICK, &done);
694 if (bulkreq.ocount != NULL) {
695 if (copy_to_user(bulkreq.lastip, &inlast,
697 return -XFS_ERROR(EFAULT);
699 if (copy_to_user(bulkreq.ocount, &count, sizeof(count)))
700 return -XFS_ERROR(EFAULT);
707 xfs_ioc_fsgeometry_v1(
711 xfs_fsop_geom_v1_t fsgeo;
714 error = xfs_fs_geometry(mp, (xfs_fsop_geom_t *)&fsgeo, 3);
718 if (copy_to_user(arg, &fsgeo, sizeof(fsgeo)))
719 return -XFS_ERROR(EFAULT);
728 xfs_fsop_geom_t fsgeo;
731 error = xfs_fs_geometry(mp, &fsgeo, 4);
735 if (copy_to_user(arg, &fsgeo, sizeof(fsgeo)))
736 return -XFS_ERROR(EFAULT);
741 * Linux extended inode flags interface.
745 xfs_merge_ioc_xflags(
749 unsigned int xflags = start;
751 if (flags & FS_IMMUTABLE_FL)
752 xflags |= XFS_XFLAG_IMMUTABLE;
754 xflags &= ~XFS_XFLAG_IMMUTABLE;
755 if (flags & FS_APPEND_FL)
756 xflags |= XFS_XFLAG_APPEND;
758 xflags &= ~XFS_XFLAG_APPEND;
759 if (flags & FS_SYNC_FL)
760 xflags |= XFS_XFLAG_SYNC;
762 xflags &= ~XFS_XFLAG_SYNC;
763 if (flags & FS_NOATIME_FL)
764 xflags |= XFS_XFLAG_NOATIME;
766 xflags &= ~XFS_XFLAG_NOATIME;
767 if (flags & FS_NODUMP_FL)
768 xflags |= XFS_XFLAG_NODUMP;
770 xflags &= ~XFS_XFLAG_NODUMP;
779 unsigned int flags = 0;
781 if (di_flags & XFS_DIFLAG_IMMUTABLE)
782 flags |= FS_IMMUTABLE_FL;
783 if (di_flags & XFS_DIFLAG_APPEND)
784 flags |= FS_APPEND_FL;
785 if (di_flags & XFS_DIFLAG_SYNC)
787 if (di_flags & XFS_DIFLAG_NOATIME)
788 flags |= FS_NOATIME_FL;
789 if (di_flags & XFS_DIFLAG_NODUMP)
790 flags |= FS_NODUMP_FL;
802 xfs_ilock(ip, XFS_ILOCK_SHARED);
803 fa.fsx_xflags = xfs_ip2xflags(ip);
804 fa.fsx_extsize = ip->i_d.di_extsize << ip->i_mount->m_sb.sb_blocklog;
805 fa.fsx_projid = ip->i_d.di_projid;
809 if (ip->i_afp->if_flags & XFS_IFEXTENTS)
810 fa.fsx_nextents = ip->i_afp->if_bytes /
811 sizeof(xfs_bmbt_rec_t);
813 fa.fsx_nextents = ip->i_d.di_anextents;
817 if (ip->i_df.if_flags & XFS_IFEXTENTS)
818 fa.fsx_nextents = ip->i_df.if_bytes /
819 sizeof(xfs_bmbt_rec_t);
821 fa.fsx_nextents = ip->i_d.di_nextents;
823 xfs_iunlock(ip, XFS_ILOCK_SHARED);
825 if (copy_to_user(arg, &fa, sizeof(fa)))
832 struct xfs_inode *ip,
835 unsigned int di_flags;
837 /* can't set PREALLOC this way, just preserve it */
838 di_flags = (ip->i_d.di_flags & XFS_DIFLAG_PREALLOC);
839 if (xflags & XFS_XFLAG_IMMUTABLE)
840 di_flags |= XFS_DIFLAG_IMMUTABLE;
841 if (xflags & XFS_XFLAG_APPEND)
842 di_flags |= XFS_DIFLAG_APPEND;
843 if (xflags & XFS_XFLAG_SYNC)
844 di_flags |= XFS_DIFLAG_SYNC;
845 if (xflags & XFS_XFLAG_NOATIME)
846 di_flags |= XFS_DIFLAG_NOATIME;
847 if (xflags & XFS_XFLAG_NODUMP)
848 di_flags |= XFS_DIFLAG_NODUMP;
849 if (xflags & XFS_XFLAG_PROJINHERIT)
850 di_flags |= XFS_DIFLAG_PROJINHERIT;
851 if (xflags & XFS_XFLAG_NODEFRAG)
852 di_flags |= XFS_DIFLAG_NODEFRAG;
853 if (xflags & XFS_XFLAG_FILESTREAM)
854 di_flags |= XFS_DIFLAG_FILESTREAM;
855 if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
856 if (xflags & XFS_XFLAG_RTINHERIT)
857 di_flags |= XFS_DIFLAG_RTINHERIT;
858 if (xflags & XFS_XFLAG_NOSYMLINKS)
859 di_flags |= XFS_DIFLAG_NOSYMLINKS;
860 if (xflags & XFS_XFLAG_EXTSZINHERIT)
861 di_flags |= XFS_DIFLAG_EXTSZINHERIT;
862 } else if ((ip->i_d.di_mode & S_IFMT) == S_IFREG) {
863 if (xflags & XFS_XFLAG_REALTIME)
864 di_flags |= XFS_DIFLAG_REALTIME;
865 if (xflags & XFS_XFLAG_EXTSIZE)
866 di_flags |= XFS_DIFLAG_EXTSIZE;
869 ip->i_d.di_flags = di_flags;
873 xfs_diflags_to_linux(
874 struct xfs_inode *ip)
876 struct inode *inode = VFS_I(ip);
877 unsigned int xflags = xfs_ip2xflags(ip);
879 if (xflags & XFS_XFLAG_IMMUTABLE)
880 inode->i_flags |= S_IMMUTABLE;
882 inode->i_flags &= ~S_IMMUTABLE;
883 if (xflags & XFS_XFLAG_APPEND)
884 inode->i_flags |= S_APPEND;
886 inode->i_flags &= ~S_APPEND;
887 if (xflags & XFS_XFLAG_SYNC)
888 inode->i_flags |= S_SYNC;
890 inode->i_flags &= ~S_SYNC;
891 if (xflags & XFS_XFLAG_NOATIME)
892 inode->i_flags |= S_NOATIME;
894 inode->i_flags &= ~S_NOATIME;
898 #define FSX_EXTSIZE 2
900 #define FSX_NONBLOCK 8
908 struct xfs_mount *mp = ip->i_mount;
909 struct xfs_trans *tp;
910 unsigned int lock_flags = 0;
911 struct xfs_dquot *udqp = NULL;
912 struct xfs_dquot *gdqp = NULL;
913 struct xfs_dquot *olddquot = NULL;
916 xfs_itrace_entry(ip);
918 if (mp->m_flags & XFS_MOUNT_RDONLY)
919 return XFS_ERROR(EROFS);
920 if (XFS_FORCED_SHUTDOWN(mp))
921 return XFS_ERROR(EIO);
924 * If disk quotas is on, we make sure that the dquots do exist on disk,
925 * before we start any other transactions. Trying to do this later
926 * is messy. We don't care to take a readlock to look at the ids
927 * in inode here, because we can't hold it across the trans_reserve.
928 * If the IDs do change before we take the ilock, we're covered
929 * because the i_*dquot fields will get updated anyway.
931 if (XFS_IS_QUOTA_ON(mp) && (mask & FSX_PROJID)) {
932 code = xfs_qm_vop_dqalloc(ip, ip->i_d.di_uid,
933 ip->i_d.di_gid, fa->fsx_projid,
934 XFS_QMOPT_PQUOTA, &udqp, &gdqp);
940 * For the other attributes, we acquire the inode lock and
941 * first do an error checking pass.
943 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
944 code = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES(mp), 0, 0, 0);
948 lock_flags = XFS_ILOCK_EXCL;
949 xfs_ilock(ip, lock_flags);
952 * CAP_FOWNER overrides the following restrictions:
954 * The user ID of the calling process must be equal
955 * to the file owner ID, except in cases where the
956 * CAP_FSETID capability is applicable.
958 if (current_fsuid() != ip->i_d.di_uid && !capable(CAP_FOWNER)) {
959 code = XFS_ERROR(EPERM);
964 * Do a quota reservation only if projid is actually going to change.
966 if (mask & FSX_PROJID) {
967 if (XFS_IS_QUOTA_RUNNING(mp) &&
968 XFS_IS_PQUOTA_ON(mp) &&
969 ip->i_d.di_projid != fa->fsx_projid) {
971 code = xfs_qm_vop_chown_reserve(tp, ip, udqp, gdqp,
972 capable(CAP_FOWNER) ?
973 XFS_QMOPT_FORCE_RES : 0);
974 if (code) /* out of quota */
979 if (mask & FSX_EXTSIZE) {
981 * Can't change extent size if any extents are allocated.
983 if (ip->i_d.di_nextents &&
984 ((ip->i_d.di_extsize << mp->m_sb.sb_blocklog) !=
986 code = XFS_ERROR(EINVAL); /* EFBIG? */
991 * Extent size must be a multiple of the appropriate block
992 * size, if set at all.
994 if (fa->fsx_extsize != 0) {
997 if (XFS_IS_REALTIME_INODE(ip) ||
998 ((mask & FSX_XFLAGS) &&
999 (fa->fsx_xflags & XFS_XFLAG_REALTIME))) {
1000 size = mp->m_sb.sb_rextsize <<
1001 mp->m_sb.sb_blocklog;
1003 size = mp->m_sb.sb_blocksize;
1006 if (fa->fsx_extsize % size) {
1007 code = XFS_ERROR(EINVAL);
1014 if (mask & FSX_XFLAGS) {
1016 * Can't change realtime flag if any extents are allocated.
1018 if ((ip->i_d.di_nextents || ip->i_delayed_blks) &&
1019 (XFS_IS_REALTIME_INODE(ip)) !=
1020 (fa->fsx_xflags & XFS_XFLAG_REALTIME)) {
1021 code = XFS_ERROR(EINVAL); /* EFBIG? */
1026 * If realtime flag is set then must have realtime data.
1028 if ((fa->fsx_xflags & XFS_XFLAG_REALTIME)) {
1029 if ((mp->m_sb.sb_rblocks == 0) ||
1030 (mp->m_sb.sb_rextsize == 0) ||
1031 (ip->i_d.di_extsize % mp->m_sb.sb_rextsize)) {
1032 code = XFS_ERROR(EINVAL);
1038 * Can't modify an immutable/append-only file unless
1039 * we have appropriate permission.
1041 if ((ip->i_d.di_flags &
1042 (XFS_DIFLAG_IMMUTABLE|XFS_DIFLAG_APPEND) ||
1044 (XFS_XFLAG_IMMUTABLE | XFS_XFLAG_APPEND))) &&
1045 !capable(CAP_LINUX_IMMUTABLE)) {
1046 code = XFS_ERROR(EPERM);
1051 xfs_trans_ijoin(tp, ip, lock_flags);
1052 xfs_trans_ihold(tp, ip);
1055 * Change file ownership. Must be the owner or privileged.
1057 if (mask & FSX_PROJID) {
1059 * CAP_FSETID overrides the following restrictions:
1061 * The set-user-ID and set-group-ID bits of a file will be
1062 * cleared upon successful return from chown()
1064 if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
1065 !capable(CAP_FSETID))
1066 ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
1069 * Change the ownerships and register quota modifications
1070 * in the transaction.
1072 if (ip->i_d.di_projid != fa->fsx_projid) {
1073 if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_PQUOTA_ON(mp)) {
1074 olddquot = xfs_qm_vop_chown(tp, ip,
1075 &ip->i_gdquot, gdqp);
1077 ip->i_d.di_projid = fa->fsx_projid;
1080 * We may have to rev the inode as well as
1081 * the superblock version number since projids didn't
1082 * exist before DINODE_VERSION_2 and SB_VERSION_NLINK.
1084 if (ip->i_d.di_version == 1)
1085 xfs_bump_ino_vers2(tp, ip);
1090 if (mask & FSX_EXTSIZE)
1091 ip->i_d.di_extsize = fa->fsx_extsize >> mp->m_sb.sb_blocklog;
1092 if (mask & FSX_XFLAGS) {
1093 xfs_set_diflags(ip, fa->fsx_xflags);
1094 xfs_diflags_to_linux(ip);
1097 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1098 xfs_ichgtime(ip, XFS_ICHGTIME_CHG);
1100 XFS_STATS_INC(xs_ig_attrchg);
1103 * If this is a synchronous mount, make sure that the
1104 * transaction goes to disk before returning to the user.
1105 * This is slightly sub-optimal in that truncates require
1106 * two sync transactions instead of one for wsync filesystems.
1107 * One for the truncate and one for the timestamps since we
1108 * don't want to change the timestamps unless we're sure the
1109 * truncate worked. Truncates are less than 1% of the laddis
1110 * mix so this probably isn't worth the trouble to optimize.
1112 if (mp->m_flags & XFS_MOUNT_WSYNC)
1113 xfs_trans_set_sync(tp);
1114 code = xfs_trans_commit(tp, 0);
1115 xfs_iunlock(ip, lock_flags);
1118 * Release any dquot(s) the inode had kept before chown.
1120 xfs_qm_dqrele(olddquot);
1121 xfs_qm_dqrele(udqp);
1122 xfs_qm_dqrele(gdqp);
1127 if (DM_EVENT_ENABLED(ip, DM_EVENT_ATTRIBUTE)) {
1128 XFS_SEND_NAMESP(mp, DM_EVENT_ATTRIBUTE, ip, DM_RIGHT_NULL,
1129 NULL, DM_RIGHT_NULL, NULL, NULL, 0, 0,
1130 (mask & FSX_NONBLOCK) ? DM_FLAGS_NDELAY : 0);
1136 xfs_qm_dqrele(udqp);
1137 xfs_qm_dqrele(gdqp);
1138 xfs_trans_cancel(tp, 0);
1140 xfs_iunlock(ip, lock_flags);
1153 if (copy_from_user(&fa, arg, sizeof(fa)))
1156 mask = FSX_XFLAGS | FSX_EXTSIZE | FSX_PROJID;
1157 if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
1158 mask |= FSX_NONBLOCK;
1160 return -xfs_ioctl_setattr(ip, &fa, mask);
1170 flags = xfs_di2lxflags(ip->i_d.di_flags);
1171 if (copy_to_user(arg, &flags, sizeof(flags)))
1186 if (copy_from_user(&flags, arg, sizeof(flags)))
1189 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
1190 FS_NOATIME_FL | FS_NODUMP_FL | \
1195 if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
1196 mask |= FSX_NONBLOCK;
1197 fa.fsx_xflags = xfs_merge_ioc_xflags(flags, xfs_ip2xflags(ip));
1199 return -xfs_ioctl_setattr(ip, &fa, mask);
1203 xfs_getbmap_format(void **ap, struct getbmapx *bmv, int *full)
1205 struct getbmap __user *base = *ap;
1207 /* copy only getbmap portion (not getbmapx) */
1208 if (copy_to_user(base, bmv, sizeof(struct getbmap)))
1209 return XFS_ERROR(EFAULT);
1211 *ap += sizeof(struct getbmap);
1217 struct xfs_inode *ip,
1222 struct getbmapx bmx;
1225 if (copy_from_user(&bmx, arg, sizeof(struct getbmapx)))
1226 return -XFS_ERROR(EFAULT);
1228 if (bmx.bmv_count < 2)
1229 return -XFS_ERROR(EINVAL);
1231 bmx.bmv_iflags = (cmd == XFS_IOC_GETBMAPA ? BMV_IF_ATTRFORK : 0);
1232 if (ioflags & IO_INVIS)
1233 bmx.bmv_iflags |= BMV_IF_NO_DMAPI_READ;
1235 error = xfs_getbmap(ip, &bmx, xfs_getbmap_format,
1236 (struct getbmap *)arg+1);
1240 /* copy back header - only size of getbmap */
1241 if (copy_to_user(arg, &bmx, sizeof(struct getbmap)))
1242 return -XFS_ERROR(EFAULT);
1247 xfs_getbmapx_format(void **ap, struct getbmapx *bmv, int *full)
1249 struct getbmapx __user *base = *ap;
1251 if (copy_to_user(base, bmv, sizeof(struct getbmapx)))
1252 return XFS_ERROR(EFAULT);
1254 *ap += sizeof(struct getbmapx);
1260 struct xfs_inode *ip,
1263 struct getbmapx bmx;
1266 if (copy_from_user(&bmx, arg, sizeof(bmx)))
1267 return -XFS_ERROR(EFAULT);
1269 if (bmx.bmv_count < 2)
1270 return -XFS_ERROR(EINVAL);
1272 if (bmx.bmv_iflags & (~BMV_IF_VALID))
1273 return -XFS_ERROR(EINVAL);
1275 error = xfs_getbmap(ip, &bmx, xfs_getbmapx_format,
1276 (struct getbmapx *)arg+1);
1280 /* copy back header */
1281 if (copy_to_user(arg, &bmx, sizeof(struct getbmapx)))
1282 return -XFS_ERROR(EFAULT);
1288 * Note: some of the ioctl's return positive numbers as a
1289 * byte count indicating success, such as readlink_by_handle.
1290 * So we don't "sign flip" like most other routines. This means
1291 * true errors need to be returned as a negative value.
1299 struct inode *inode = filp->f_path.dentry->d_inode;
1300 struct xfs_inode *ip = XFS_I(inode);
1301 struct xfs_mount *mp = ip->i_mount;
1302 void __user *arg = (void __user *)p;
1306 if (filp->f_mode & FMODE_NOCMTIME)
1307 ioflags |= IO_INVIS;
1309 xfs_itrace_entry(ip);
1312 case XFS_IOC_ALLOCSP:
1313 case XFS_IOC_FREESP:
1314 case XFS_IOC_RESVSP:
1315 case XFS_IOC_UNRESVSP:
1316 case XFS_IOC_ALLOCSP64:
1317 case XFS_IOC_FREESP64:
1318 case XFS_IOC_RESVSP64:
1319 case XFS_IOC_UNRESVSP64: {
1322 if (copy_from_user(&bf, arg, sizeof(bf)))
1323 return -XFS_ERROR(EFAULT);
1324 return xfs_ioc_space(ip, inode, filp, ioflags, cmd, &bf);
1326 case XFS_IOC_DIOINFO: {
1328 xfs_buftarg_t *target =
1329 XFS_IS_REALTIME_INODE(ip) ?
1330 mp->m_rtdev_targp : mp->m_ddev_targp;
1332 da.d_mem = da.d_miniosz = 1 << target->bt_sshift;
1333 da.d_maxiosz = INT_MAX & ~(da.d_miniosz - 1);
1335 if (copy_to_user(arg, &da, sizeof(da)))
1336 return -XFS_ERROR(EFAULT);
1340 case XFS_IOC_FSBULKSTAT_SINGLE:
1341 case XFS_IOC_FSBULKSTAT:
1342 case XFS_IOC_FSINUMBERS:
1343 return xfs_ioc_bulkstat(mp, cmd, arg);
1345 case XFS_IOC_FSGEOMETRY_V1:
1346 return xfs_ioc_fsgeometry_v1(mp, arg);
1348 case XFS_IOC_FSGEOMETRY:
1349 return xfs_ioc_fsgeometry(mp, arg);
1351 case XFS_IOC_GETVERSION:
1352 return put_user(inode->i_generation, (int __user *)arg);
1354 case XFS_IOC_FSGETXATTR:
1355 return xfs_ioc_fsgetxattr(ip, 0, arg);
1356 case XFS_IOC_FSGETXATTRA:
1357 return xfs_ioc_fsgetxattr(ip, 1, arg);
1358 case XFS_IOC_FSSETXATTR:
1359 return xfs_ioc_fssetxattr(ip, filp, arg);
1360 case XFS_IOC_GETXFLAGS:
1361 return xfs_ioc_getxflags(ip, arg);
1362 case XFS_IOC_SETXFLAGS:
1363 return xfs_ioc_setxflags(ip, filp, arg);
1365 case XFS_IOC_FSSETDM: {
1366 struct fsdmidata dmi;
1368 if (copy_from_user(&dmi, arg, sizeof(dmi)))
1369 return -XFS_ERROR(EFAULT);
1371 error = xfs_set_dmattrs(ip, dmi.fsd_dmevmask,
1376 case XFS_IOC_GETBMAP:
1377 case XFS_IOC_GETBMAPA:
1378 return xfs_ioc_getbmap(ip, ioflags, cmd, arg);
1380 case XFS_IOC_GETBMAPX:
1381 return xfs_ioc_getbmapx(ip, arg);
1383 case XFS_IOC_FD_TO_HANDLE:
1384 case XFS_IOC_PATH_TO_HANDLE:
1385 case XFS_IOC_PATH_TO_FSHANDLE: {
1386 xfs_fsop_handlereq_t hreq;
1388 if (copy_from_user(&hreq, arg, sizeof(hreq)))
1389 return -XFS_ERROR(EFAULT);
1390 return xfs_find_handle(cmd, &hreq);
1392 case XFS_IOC_OPEN_BY_HANDLE: {
1393 xfs_fsop_handlereq_t hreq;
1395 if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
1396 return -XFS_ERROR(EFAULT);
1397 return xfs_open_by_handle(filp, &hreq);
1399 case XFS_IOC_FSSETDM_BY_HANDLE:
1400 return xfs_fssetdm_by_handle(filp, arg);
1402 case XFS_IOC_READLINK_BY_HANDLE: {
1403 xfs_fsop_handlereq_t hreq;
1405 if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
1406 return -XFS_ERROR(EFAULT);
1407 return xfs_readlink_by_handle(filp, &hreq);
1409 case XFS_IOC_ATTRLIST_BY_HANDLE:
1410 return xfs_attrlist_by_handle(filp, arg);
1412 case XFS_IOC_ATTRMULTI_BY_HANDLE:
1413 return xfs_attrmulti_by_handle(filp, arg);
1415 case XFS_IOC_SWAPEXT: {
1416 struct xfs_swapext sxp;
1418 if (copy_from_user(&sxp, arg, sizeof(xfs_swapext_t)))
1419 return -XFS_ERROR(EFAULT);
1420 error = xfs_swapext(&sxp);
1424 case XFS_IOC_FSCOUNTS: {
1425 xfs_fsop_counts_t out;
1427 error = xfs_fs_counts(mp, &out);
1431 if (copy_to_user(arg, &out, sizeof(out)))
1432 return -XFS_ERROR(EFAULT);
1436 case XFS_IOC_SET_RESBLKS: {
1437 xfs_fsop_resblks_t inout;
1440 if (!capable(CAP_SYS_ADMIN))
1443 if (copy_from_user(&inout, arg, sizeof(inout)))
1444 return -XFS_ERROR(EFAULT);
1446 /* input parameter is passed in resblks field of structure */
1448 error = xfs_reserve_blocks(mp, &in, &inout);
1452 if (copy_to_user(arg, &inout, sizeof(inout)))
1453 return -XFS_ERROR(EFAULT);
1457 case XFS_IOC_GET_RESBLKS: {
1458 xfs_fsop_resblks_t out;
1460 if (!capable(CAP_SYS_ADMIN))
1463 error = xfs_reserve_blocks(mp, NULL, &out);
1467 if (copy_to_user(arg, &out, sizeof(out)))
1468 return -XFS_ERROR(EFAULT);
1473 case XFS_IOC_FSGROWFSDATA: {
1474 xfs_growfs_data_t in;
1476 if (copy_from_user(&in, arg, sizeof(in)))
1477 return -XFS_ERROR(EFAULT);
1479 error = xfs_growfs_data(mp, &in);
1483 case XFS_IOC_FSGROWFSLOG: {
1484 xfs_growfs_log_t in;
1486 if (copy_from_user(&in, arg, sizeof(in)))
1487 return -XFS_ERROR(EFAULT);
1489 error = xfs_growfs_log(mp, &in);
1493 case XFS_IOC_FSGROWFSRT: {
1496 if (copy_from_user(&in, arg, sizeof(in)))
1497 return -XFS_ERROR(EFAULT);
1499 error = xfs_growfs_rt(mp, &in);
1503 case XFS_IOC_GOINGDOWN: {
1506 if (!capable(CAP_SYS_ADMIN))
1509 if (get_user(in, (__uint32_t __user *)arg))
1510 return -XFS_ERROR(EFAULT);
1512 error = xfs_fs_goingdown(mp, in);
1516 case XFS_IOC_ERROR_INJECTION: {
1517 xfs_error_injection_t in;
1519 if (!capable(CAP_SYS_ADMIN))
1522 if (copy_from_user(&in, arg, sizeof(in)))
1523 return -XFS_ERROR(EFAULT);
1525 error = xfs_errortag_add(in.errtag, mp);
1529 case XFS_IOC_ERROR_CLEARALL:
1530 if (!capable(CAP_SYS_ADMIN))
1533 error = xfs_errortag_clearall(mp, 1);