1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_log_format.h"
11 #include "xfs_trans_resv.h"
12 #include "xfs_mount.h"
13 #include "xfs_inode.h"
14 #include "xfs_ioctl.h"
15 #include "xfs_alloc.h"
16 #include "xfs_rtalloc.h"
17 #include "xfs_itable.h"
18 #include "xfs_error.h"
21 #include "xfs_bmap_util.h"
22 #include "xfs_fsops.h"
23 #include "xfs_discard.h"
24 #include "xfs_quota.h"
25 #include "xfs_export.h"
26 #include "xfs_trace.h"
27 #include "xfs_icache.h"
28 #include "xfs_symlink.h"
29 #include "xfs_trans.h"
32 #include "xfs_btree.h"
33 #include <linux/fsmap.h>
34 #include "xfs_fsmap.h"
35 #include "scrub/xfs_scrub.h"
38 #include <linux/capability.h>
39 #include <linux/cred.h>
40 #include <linux/dcache.h>
41 #include <linux/mount.h>
42 #include <linux/namei.h>
43 #include <linux/pagemap.h>
44 #include <linux/slab.h>
45 #include <linux/exportfs.h>
48 * xfs_find_handle maps from userspace xfs_fsop_handlereq structure to
49 * a file or fs handle.
51 * XFS_IOC_PATH_TO_FSHANDLE
52 * returns fs handle for a mount point or path within that mount point
53 * XFS_IOC_FD_TO_HANDLE
54 * returns full handle for a FD opened in user space
55 * XFS_IOC_PATH_TO_HANDLE
56 * returns full handle for a path
61 xfs_fsop_handlereq_t *hreq)
71 if (cmd == XFS_IOC_FD_TO_HANDLE) {
75 inode = file_inode(f.file);
77 error = user_lpath((const char __user *)hreq->path, &path);
80 inode = d_inode(path.dentry);
85 * We can only generate handles for inodes residing on a XFS filesystem,
86 * and only for regular files, directories or symbolic links.
89 if (inode->i_sb->s_magic != XFS_SB_MAGIC)
93 if (!S_ISREG(inode->i_mode) &&
94 !S_ISDIR(inode->i_mode) &&
95 !S_ISLNK(inode->i_mode))
99 memcpy(&handle.ha_fsid, ip->i_mount->m_fixedfsid, sizeof(xfs_fsid_t));
101 if (cmd == XFS_IOC_PATH_TO_FSHANDLE) {
103 * This handle only contains an fsid, zero the rest.
105 memset(&handle.ha_fid, 0, sizeof(handle.ha_fid));
106 hsize = sizeof(xfs_fsid_t);
108 handle.ha_fid.fid_len = sizeof(xfs_fid_t) -
109 sizeof(handle.ha_fid.fid_len);
110 handle.ha_fid.fid_pad = 0;
111 handle.ha_fid.fid_gen = inode->i_generation;
112 handle.ha_fid.fid_ino = ip->i_ino;
113 hsize = sizeof(xfs_handle_t);
117 if (copy_to_user(hreq->ohandle, &handle, hsize) ||
118 copy_to_user(hreq->ohandlen, &hsize, sizeof(__s32)))
124 if (cmd == XFS_IOC_FD_TO_HANDLE)
132 * No need to do permission checks on the various pathname components
133 * as the handle operations are privileged.
136 xfs_handle_acceptable(
138 struct dentry *dentry)
144 * Convert userspace handle data into a dentry.
147 xfs_handle_to_dentry(
148 struct file *parfilp,
149 void __user *uhandle,
153 struct xfs_fid64 fid;
156 * Only allow handle opens under a directory.
158 if (!S_ISDIR(file_inode(parfilp)->i_mode))
159 return ERR_PTR(-ENOTDIR);
161 if (hlen != sizeof(xfs_handle_t))
162 return ERR_PTR(-EINVAL);
163 if (copy_from_user(&handle, uhandle, hlen))
164 return ERR_PTR(-EFAULT);
165 if (handle.ha_fid.fid_len !=
166 sizeof(handle.ha_fid) - sizeof(handle.ha_fid.fid_len))
167 return ERR_PTR(-EINVAL);
169 memset(&fid, 0, sizeof(struct fid));
170 fid.ino = handle.ha_fid.fid_ino;
171 fid.gen = handle.ha_fid.fid_gen;
173 return exportfs_decode_fh(parfilp->f_path.mnt, (struct fid *)&fid, 3,
174 FILEID_INO32_GEN | XFS_FILEID_TYPE_64FLAG,
175 xfs_handle_acceptable, NULL);
178 STATIC struct dentry *
179 xfs_handlereq_to_dentry(
180 struct file *parfilp,
181 xfs_fsop_handlereq_t *hreq)
183 return xfs_handle_to_dentry(parfilp, hreq->ihandle, hreq->ihandlen);
188 struct file *parfilp,
189 xfs_fsop_handlereq_t *hreq)
191 const struct cred *cred = current_cred();
197 struct dentry *dentry;
201 if (!capable(CAP_SYS_ADMIN))
204 dentry = xfs_handlereq_to_dentry(parfilp, hreq);
206 return PTR_ERR(dentry);
207 inode = d_inode(dentry);
209 /* Restrict xfs_open_by_handle to directories & regular files. */
210 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode))) {
215 #if BITS_PER_LONG != 32
216 hreq->oflags |= O_LARGEFILE;
219 permflag = hreq->oflags;
220 fmode = OPEN_FMODE(permflag);
221 if ((!(permflag & O_APPEND) || (permflag & O_TRUNC)) &&
222 (fmode & FMODE_WRITE) && IS_APPEND(inode)) {
227 if ((fmode & FMODE_WRITE) && IS_IMMUTABLE(inode)) {
232 /* Can't write directories. */
233 if (S_ISDIR(inode->i_mode) && (fmode & FMODE_WRITE)) {
238 fd = get_unused_fd_flags(0);
244 path.mnt = parfilp->f_path.mnt;
245 path.dentry = dentry;
246 filp = dentry_open(&path, hreq->oflags, cred);
250 return PTR_ERR(filp);
253 if (S_ISREG(inode->i_mode)) {
254 filp->f_flags |= O_NOATIME;
255 filp->f_mode |= FMODE_NOCMTIME;
258 fd_install(fd, filp);
267 xfs_readlink_by_handle(
268 struct file *parfilp,
269 xfs_fsop_handlereq_t *hreq)
271 struct dentry *dentry;
275 if (!capable(CAP_SYS_ADMIN))
278 dentry = xfs_handlereq_to_dentry(parfilp, hreq);
280 return PTR_ERR(dentry);
282 /* Restrict this handle operation to symlinks only. */
283 if (!d_is_symlink(dentry)) {
288 if (copy_from_user(&olen, hreq->ohandlen, sizeof(__u32))) {
293 error = vfs_readlink(dentry, hreq->ohandle, olen);
306 xfs_mount_t *mp = ip->i_mount;
310 if (!capable(CAP_SYS_ADMIN))
313 if (XFS_FORCED_SHUTDOWN(mp))
316 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
320 xfs_ilock(ip, XFS_ILOCK_EXCL);
321 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
323 ip->i_d.di_dmevmask = evmask;
324 ip->i_d.di_dmstate = state;
326 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
327 error = xfs_trans_commit(tp);
333 xfs_fssetdm_by_handle(
334 struct file *parfilp,
338 struct fsdmidata fsd;
339 xfs_fsop_setdm_handlereq_t dmhreq;
340 struct dentry *dentry;
342 if (!capable(CAP_MKNOD))
344 if (copy_from_user(&dmhreq, arg, sizeof(xfs_fsop_setdm_handlereq_t)))
347 error = mnt_want_write_file(parfilp);
351 dentry = xfs_handlereq_to_dentry(parfilp, &dmhreq.hreq);
352 if (IS_ERR(dentry)) {
353 mnt_drop_write_file(parfilp);
354 return PTR_ERR(dentry);
357 if (IS_IMMUTABLE(d_inode(dentry)) || IS_APPEND(d_inode(dentry))) {
362 if (copy_from_user(&fsd, dmhreq.data, sizeof(fsd))) {
367 error = xfs_set_dmattrs(XFS_I(d_inode(dentry)), fsd.fsd_dmevmask,
371 mnt_drop_write_file(parfilp);
377 xfs_attrlist_by_handle(
378 struct file *parfilp,
382 attrlist_cursor_kern_t *cursor;
383 struct xfs_fsop_attrlist_handlereq __user *p = arg;
384 xfs_fsop_attrlist_handlereq_t al_hreq;
385 struct dentry *dentry;
388 if (!capable(CAP_SYS_ADMIN))
390 if (copy_from_user(&al_hreq, arg, sizeof(xfs_fsop_attrlist_handlereq_t)))
392 if (al_hreq.buflen < sizeof(struct attrlist) ||
393 al_hreq.buflen > XFS_XATTR_LIST_MAX)
397 * Reject flags, only allow namespaces.
399 if (al_hreq.flags & ~(ATTR_ROOT | ATTR_SECURE))
402 dentry = xfs_handlereq_to_dentry(parfilp, &al_hreq.hreq);
404 return PTR_ERR(dentry);
406 kbuf = kmem_zalloc_large(al_hreq.buflen, KM_SLEEP);
410 cursor = (attrlist_cursor_kern_t *)&al_hreq.pos;
411 error = xfs_attr_list(XFS_I(d_inode(dentry)), kbuf, al_hreq.buflen,
412 al_hreq.flags, cursor);
416 if (copy_to_user(&p->pos, cursor, sizeof(attrlist_cursor_kern_t))) {
421 if (copy_to_user(al_hreq.buffer, kbuf, al_hreq.buflen))
432 xfs_attrmulti_attr_get(
435 unsigned char __user *ubuf,
442 if (*len > XFS_XATTR_SIZE_MAX)
444 kbuf = kmem_zalloc_large(*len, KM_SLEEP);
448 error = xfs_attr_get(XFS_I(inode), name, kbuf, (int *)len, flags);
452 if (copy_to_user(ubuf, kbuf, *len))
461 xfs_attrmulti_attr_set(
464 const unsigned char __user *ubuf,
471 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
473 if (len > XFS_XATTR_SIZE_MAX)
476 kbuf = memdup_user(ubuf, len);
478 return PTR_ERR(kbuf);
480 error = xfs_attr_set(XFS_I(inode), name, kbuf, len, flags);
482 xfs_forget_acl(inode, name, flags);
488 xfs_attrmulti_attr_remove(
495 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
497 error = xfs_attr_remove(XFS_I(inode), name, flags);
499 xfs_forget_acl(inode, name, flags);
504 xfs_attrmulti_by_handle(
505 struct file *parfilp,
509 xfs_attr_multiop_t *ops;
510 xfs_fsop_attrmulti_handlereq_t am_hreq;
511 struct dentry *dentry;
512 unsigned int i, size;
513 unsigned char *attr_name;
515 if (!capable(CAP_SYS_ADMIN))
517 if (copy_from_user(&am_hreq, arg, sizeof(xfs_fsop_attrmulti_handlereq_t)))
521 if (am_hreq.opcount >= INT_MAX / sizeof(xfs_attr_multiop_t))
524 dentry = xfs_handlereq_to_dentry(parfilp, &am_hreq.hreq);
526 return PTR_ERR(dentry);
529 size = am_hreq.opcount * sizeof(xfs_attr_multiop_t);
530 if (!size || size > 16 * PAGE_SIZE)
533 ops = memdup_user(am_hreq.ops, size);
535 error = PTR_ERR(ops);
540 attr_name = kmalloc(MAXNAMELEN, GFP_KERNEL);
545 for (i = 0; i < am_hreq.opcount; i++) {
546 ops[i].am_error = strncpy_from_user((char *)attr_name,
547 ops[i].am_attrname, MAXNAMELEN);
548 if (ops[i].am_error == 0 || ops[i].am_error == MAXNAMELEN)
550 if (ops[i].am_error < 0)
553 switch (ops[i].am_opcode) {
555 ops[i].am_error = xfs_attrmulti_attr_get(
556 d_inode(dentry), attr_name,
557 ops[i].am_attrvalue, &ops[i].am_length,
561 ops[i].am_error = mnt_want_write_file(parfilp);
564 ops[i].am_error = xfs_attrmulti_attr_set(
565 d_inode(dentry), attr_name,
566 ops[i].am_attrvalue, ops[i].am_length,
568 mnt_drop_write_file(parfilp);
571 ops[i].am_error = mnt_want_write_file(parfilp);
574 ops[i].am_error = xfs_attrmulti_attr_remove(
575 d_inode(dentry), attr_name,
577 mnt_drop_write_file(parfilp);
580 ops[i].am_error = -EINVAL;
584 if (copy_to_user(am_hreq.ops, ops, size))
601 struct inode *inode = file_inode(filp);
602 struct xfs_inode *ip = XFS_I(inode);
604 enum xfs_prealloc_flags flags = 0;
605 uint iolock = XFS_IOLOCK_EXCL;
609 * Only allow the sys admin to reserve space unless
610 * unwritten extents are enabled.
612 if (!xfs_sb_version_hasextflgbit(&ip->i_mount->m_sb) &&
613 !capable(CAP_SYS_ADMIN))
616 if (inode->i_flags & (S_IMMUTABLE|S_APPEND))
619 if (!(filp->f_mode & FMODE_WRITE))
622 if (!S_ISREG(inode->i_mode))
625 if (filp->f_flags & O_DSYNC)
626 flags |= XFS_PREALLOC_SYNC;
627 if (filp->f_mode & FMODE_NOCMTIME)
628 flags |= XFS_PREALLOC_INVISIBLE;
630 error = mnt_want_write_file(filp);
634 xfs_ilock(ip, iolock);
635 error = xfs_break_layouts(inode, &iolock);
639 xfs_ilock(ip, XFS_MMAPLOCK_EXCL);
640 iolock |= XFS_MMAPLOCK_EXCL;
642 switch (bf->l_whence) {
646 bf->l_start += filp->f_pos;
649 bf->l_start += XFS_ISIZE(ip);
657 * length of <= 0 for resv/unresv/zero is invalid. length for
658 * alloc/free is ignored completely and we have no idea what userspace
659 * might have set it to, so set it to zero to allow range
663 case XFS_IOC_ZERO_RANGE:
665 case XFS_IOC_RESVSP64:
666 case XFS_IOC_UNRESVSP:
667 case XFS_IOC_UNRESVSP64:
668 if (bf->l_len <= 0) {
678 if (bf->l_start < 0 ||
679 bf->l_start > inode->i_sb->s_maxbytes ||
680 bf->l_start + bf->l_len < 0 ||
681 bf->l_start + bf->l_len >= inode->i_sb->s_maxbytes) {
687 case XFS_IOC_ZERO_RANGE:
688 flags |= XFS_PREALLOC_SET;
689 error = xfs_zero_file_space(ip, bf->l_start, bf->l_len);
692 case XFS_IOC_RESVSP64:
693 flags |= XFS_PREALLOC_SET;
694 error = xfs_alloc_file_space(ip, bf->l_start, bf->l_len,
697 case XFS_IOC_UNRESVSP:
698 case XFS_IOC_UNRESVSP64:
699 error = xfs_free_file_space(ip, bf->l_start, bf->l_len);
701 case XFS_IOC_ALLOCSP:
702 case XFS_IOC_ALLOCSP64:
704 case XFS_IOC_FREESP64:
705 flags |= XFS_PREALLOC_CLEAR;
706 if (bf->l_start > XFS_ISIZE(ip)) {
707 error = xfs_alloc_file_space(ip, XFS_ISIZE(ip),
708 bf->l_start - XFS_ISIZE(ip), 0);
713 iattr.ia_valid = ATTR_SIZE;
714 iattr.ia_size = bf->l_start;
716 error = xfs_vn_setattr_size(file_dentry(filp), &iattr);
726 error = xfs_update_prealloc_flags(ip, flags);
729 xfs_iunlock(ip, iolock);
730 mnt_drop_write_file(filp);
740 xfs_fsop_bulkreq_t bulkreq;
741 int count; /* # of records returned */
742 xfs_ino_t inlast; /* last inode number */
746 /* done = 1 if there are more stats to get and if bulkstat */
747 /* should be called again (unused here, but used in dmapi) */
749 if (!capable(CAP_SYS_ADMIN))
752 if (XFS_FORCED_SHUTDOWN(mp))
755 if (copy_from_user(&bulkreq, arg, sizeof(xfs_fsop_bulkreq_t)))
758 if (copy_from_user(&inlast, bulkreq.lastip, sizeof(__s64)))
761 if ((count = bulkreq.icount) <= 0)
764 if (bulkreq.ubuffer == NULL)
767 if (cmd == XFS_IOC_FSINUMBERS)
768 error = xfs_inumbers(mp, &inlast, &count,
769 bulkreq.ubuffer, xfs_inumbers_fmt);
770 else if (cmd == XFS_IOC_FSBULKSTAT_SINGLE)
771 error = xfs_bulkstat_one(mp, inlast, bulkreq.ubuffer,
772 sizeof(xfs_bstat_t), NULL, &done);
773 else /* XFS_IOC_FSBULKSTAT */
774 error = xfs_bulkstat(mp, &inlast, &count, xfs_bulkstat_one,
775 sizeof(xfs_bstat_t), bulkreq.ubuffer,
781 if (bulkreq.ocount != NULL) {
782 if (copy_to_user(bulkreq.lastip, &inlast,
786 if (copy_to_user(bulkreq.ocount, &count, sizeof(count)))
794 xfs_ioc_fsgeometry_v1(
798 xfs_fsop_geom_t fsgeo;
801 error = xfs_fs_geometry(&mp->m_sb, &fsgeo, 3);
806 * Caller should have passed an argument of type
807 * xfs_fsop_geom_v1_t. This is a proper subset of the
808 * xfs_fsop_geom_t that xfs_fs_geometry() fills in.
810 if (copy_to_user(arg, &fsgeo, sizeof(xfs_fsop_geom_v1_t)))
820 xfs_fsop_geom_t fsgeo;
823 error = xfs_fs_geometry(&mp->m_sb, &fsgeo, 4);
827 if (copy_to_user(arg, &fsgeo, sizeof(fsgeo)))
833 * Linux extended inode flags interface.
837 xfs_merge_ioc_xflags(
841 unsigned int xflags = start;
843 if (flags & FS_IMMUTABLE_FL)
844 xflags |= FS_XFLAG_IMMUTABLE;
846 xflags &= ~FS_XFLAG_IMMUTABLE;
847 if (flags & FS_APPEND_FL)
848 xflags |= FS_XFLAG_APPEND;
850 xflags &= ~FS_XFLAG_APPEND;
851 if (flags & FS_SYNC_FL)
852 xflags |= FS_XFLAG_SYNC;
854 xflags &= ~FS_XFLAG_SYNC;
855 if (flags & FS_NOATIME_FL)
856 xflags |= FS_XFLAG_NOATIME;
858 xflags &= ~FS_XFLAG_NOATIME;
859 if (flags & FS_NODUMP_FL)
860 xflags |= FS_XFLAG_NODUMP;
862 xflags &= ~FS_XFLAG_NODUMP;
871 unsigned int flags = 0;
873 if (di_flags & XFS_DIFLAG_IMMUTABLE)
874 flags |= FS_IMMUTABLE_FL;
875 if (di_flags & XFS_DIFLAG_APPEND)
876 flags |= FS_APPEND_FL;
877 if (di_flags & XFS_DIFLAG_SYNC)
879 if (di_flags & XFS_DIFLAG_NOATIME)
880 flags |= FS_NOATIME_FL;
881 if (di_flags & XFS_DIFLAG_NODUMP)
882 flags |= FS_NODUMP_FL;
894 memset(&fa, 0, sizeof(struct fsxattr));
896 xfs_ilock(ip, XFS_ILOCK_SHARED);
897 fa.fsx_xflags = xfs_ip2xflags(ip);
898 fa.fsx_extsize = ip->i_d.di_extsize << ip->i_mount->m_sb.sb_blocklog;
899 fa.fsx_cowextsize = ip->i_d.di_cowextsize <<
900 ip->i_mount->m_sb.sb_blocklog;
901 fa.fsx_projid = xfs_get_projid(ip);
905 if (ip->i_afp->if_flags & XFS_IFEXTENTS)
906 fa.fsx_nextents = xfs_iext_count(ip->i_afp);
908 fa.fsx_nextents = ip->i_d.di_anextents;
912 if (ip->i_df.if_flags & XFS_IFEXTENTS)
913 fa.fsx_nextents = xfs_iext_count(&ip->i_df);
915 fa.fsx_nextents = ip->i_d.di_nextents;
917 xfs_iunlock(ip, XFS_ILOCK_SHARED);
919 if (copy_to_user(arg, &fa, sizeof(fa)))
926 struct xfs_inode *ip,
929 /* can't set PREALLOC this way, just preserve it */
931 (ip->i_d.di_flags & XFS_DIFLAG_PREALLOC);
933 if (xflags & FS_XFLAG_IMMUTABLE)
934 di_flags |= XFS_DIFLAG_IMMUTABLE;
935 if (xflags & FS_XFLAG_APPEND)
936 di_flags |= XFS_DIFLAG_APPEND;
937 if (xflags & FS_XFLAG_SYNC)
938 di_flags |= XFS_DIFLAG_SYNC;
939 if (xflags & FS_XFLAG_NOATIME)
940 di_flags |= XFS_DIFLAG_NOATIME;
941 if (xflags & FS_XFLAG_NODUMP)
942 di_flags |= XFS_DIFLAG_NODUMP;
943 if (xflags & FS_XFLAG_NODEFRAG)
944 di_flags |= XFS_DIFLAG_NODEFRAG;
945 if (xflags & FS_XFLAG_FILESTREAM)
946 di_flags |= XFS_DIFLAG_FILESTREAM;
947 if (S_ISDIR(VFS_I(ip)->i_mode)) {
948 if (xflags & FS_XFLAG_RTINHERIT)
949 di_flags |= XFS_DIFLAG_RTINHERIT;
950 if (xflags & FS_XFLAG_NOSYMLINKS)
951 di_flags |= XFS_DIFLAG_NOSYMLINKS;
952 if (xflags & FS_XFLAG_EXTSZINHERIT)
953 di_flags |= XFS_DIFLAG_EXTSZINHERIT;
954 if (xflags & FS_XFLAG_PROJINHERIT)
955 di_flags |= XFS_DIFLAG_PROJINHERIT;
956 } else if (S_ISREG(VFS_I(ip)->i_mode)) {
957 if (xflags & FS_XFLAG_REALTIME)
958 di_flags |= XFS_DIFLAG_REALTIME;
959 if (xflags & FS_XFLAG_EXTSIZE)
960 di_flags |= XFS_DIFLAG_EXTSIZE;
968 struct xfs_inode *ip,
972 (ip->i_d.di_flags2 & XFS_DIFLAG2_REFLINK);
974 if (xflags & FS_XFLAG_DAX)
975 di_flags2 |= XFS_DIFLAG2_DAX;
976 if (xflags & FS_XFLAG_COWEXTSIZE)
977 di_flags2 |= XFS_DIFLAG2_COWEXTSIZE;
983 xfs_diflags_to_linux(
984 struct xfs_inode *ip)
986 struct inode *inode = VFS_I(ip);
987 unsigned int xflags = xfs_ip2xflags(ip);
989 if (xflags & FS_XFLAG_IMMUTABLE)
990 inode->i_flags |= S_IMMUTABLE;
992 inode->i_flags &= ~S_IMMUTABLE;
993 if (xflags & FS_XFLAG_APPEND)
994 inode->i_flags |= S_APPEND;
996 inode->i_flags &= ~S_APPEND;
997 if (xflags & FS_XFLAG_SYNC)
998 inode->i_flags |= S_SYNC;
1000 inode->i_flags &= ~S_SYNC;
1001 if (xflags & FS_XFLAG_NOATIME)
1002 inode->i_flags |= S_NOATIME;
1004 inode->i_flags &= ~S_NOATIME;
1005 #if 0 /* disabled until the flag switching races are sorted out */
1006 if (xflags & FS_XFLAG_DAX)
1007 inode->i_flags |= S_DAX;
1009 inode->i_flags &= ~S_DAX;
1014 xfs_ioctl_setattr_xflags(
1015 struct xfs_trans *tp,
1016 struct xfs_inode *ip,
1019 struct xfs_mount *mp = ip->i_mount;
1022 /* Can't change realtime flag if any extents are allocated. */
1023 if ((ip->i_d.di_nextents || ip->i_delayed_blks) &&
1024 XFS_IS_REALTIME_INODE(ip) != (fa->fsx_xflags & FS_XFLAG_REALTIME))
1027 /* If realtime flag is set then must have realtime device */
1028 if (fa->fsx_xflags & FS_XFLAG_REALTIME) {
1029 if (mp->m_sb.sb_rblocks == 0 || mp->m_sb.sb_rextsize == 0 ||
1030 (ip->i_d.di_extsize % mp->m_sb.sb_rextsize))
1034 /* Clear reflink if we are actually able to set the rt flag. */
1035 if ((fa->fsx_xflags & FS_XFLAG_REALTIME) && xfs_is_reflink_inode(ip))
1036 ip->i_d.di_flags2 &= ~XFS_DIFLAG2_REFLINK;
1038 /* Don't allow us to set DAX mode for a reflinked file for now. */
1039 if ((fa->fsx_xflags & FS_XFLAG_DAX) && xfs_is_reflink_inode(ip))
1043 * Can't modify an immutable/append-only file unless
1044 * we have appropriate permission.
1046 if (((ip->i_d.di_flags & (XFS_DIFLAG_IMMUTABLE | XFS_DIFLAG_APPEND)) ||
1047 (fa->fsx_xflags & (FS_XFLAG_IMMUTABLE | FS_XFLAG_APPEND))) &&
1048 !capable(CAP_LINUX_IMMUTABLE))
1051 /* diflags2 only valid for v3 inodes. */
1052 di_flags2 = xfs_flags2diflags2(ip, fa->fsx_xflags);
1053 if (di_flags2 && ip->i_d.di_version < 3)
1056 ip->i_d.di_flags = xfs_flags2diflags(ip, fa->fsx_xflags);
1057 ip->i_d.di_flags2 = di_flags2;
1059 xfs_diflags_to_linux(ip);
1060 xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
1061 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1062 XFS_STATS_INC(mp, xs_ig_attrchg);
1067 * If we are changing DAX flags, we have to ensure the file is clean and any
1068 * cached objects in the address space are invalidated and removed. This
1069 * requires us to lock out other IO and page faults similar to a truncate
1070 * operation. The locks need to be held until the transaction has been committed
1071 * so that the cache invalidation is atomic with respect to the DAX flag
1075 xfs_ioctl_setattr_dax_invalidate(
1076 struct xfs_inode *ip,
1080 struct inode *inode = VFS_I(ip);
1081 struct super_block *sb = inode->i_sb;
1087 * It is only valid to set the DAX flag on regular files and
1088 * directories on filesystems where the block size is equal to the page
1089 * size. On directories it serves as an inherited hint so we don't
1090 * have to check the device for dax support or flush pagecache.
1092 if (fa->fsx_xflags & FS_XFLAG_DAX) {
1093 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)))
1095 if (S_ISREG(inode->i_mode) &&
1096 !bdev_dax_supported(xfs_find_bdev_for_inode(VFS_I(ip)),
1101 /* If the DAX state is not changing, we have nothing to do here. */
1102 if ((fa->fsx_xflags & FS_XFLAG_DAX) && IS_DAX(inode))
1104 if (!(fa->fsx_xflags & FS_XFLAG_DAX) && !IS_DAX(inode))
1107 if (S_ISDIR(inode->i_mode))
1110 /* lock, flush and invalidate mapping in preparation for flag change */
1111 xfs_ilock(ip, XFS_MMAPLOCK_EXCL | XFS_IOLOCK_EXCL);
1112 error = filemap_write_and_wait(inode->i_mapping);
1115 error = invalidate_inode_pages2(inode->i_mapping);
1119 *join_flags = XFS_MMAPLOCK_EXCL | XFS_IOLOCK_EXCL;
1123 xfs_iunlock(ip, XFS_MMAPLOCK_EXCL | XFS_IOLOCK_EXCL);
1129 * Set up the transaction structure for the setattr operation, checking that we
1130 * have permission to do so. On success, return a clean transaction and the
1131 * inode locked exclusively ready for further operation specific checks. On
1132 * failure, return an error without modifying or locking the inode.
1134 * The inode might already be IO locked on call. If this is the case, it is
1135 * indicated in @join_flags and we take full responsibility for ensuring they
1136 * are unlocked from now on. Hence if we have an error here, we still have to
1137 * unlock them. Otherwise, once they are joined to the transaction, they will
1138 * be unlocked on commit/cancel.
1140 static struct xfs_trans *
1141 xfs_ioctl_setattr_get_trans(
1142 struct xfs_inode *ip,
1145 struct xfs_mount *mp = ip->i_mount;
1146 struct xfs_trans *tp;
1149 if (mp->m_flags & XFS_MOUNT_RDONLY)
1152 if (XFS_FORCED_SHUTDOWN(mp))
1155 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
1157 return ERR_PTR(error);
1159 xfs_ilock(ip, XFS_ILOCK_EXCL);
1160 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | join_flags);
1164 * CAP_FOWNER overrides the following restrictions:
1166 * The user ID of the calling process must be equal to the file owner
1167 * ID, except in cases where the CAP_FSETID capability is applicable.
1169 if (!inode_owner_or_capable(VFS_I(ip))) {
1174 if (mp->m_flags & XFS_MOUNT_WSYNC)
1175 xfs_trans_set_sync(tp);
1180 xfs_trans_cancel(tp);
1183 xfs_iunlock(ip, join_flags);
1184 return ERR_PTR(error);
1188 * extent size hint validation is somewhat cumbersome. Rules are:
1190 * 1. extent size hint is only valid for directories and regular files
1191 * 2. FS_XFLAG_EXTSIZE is only valid for regular files
1192 * 3. FS_XFLAG_EXTSZINHERIT is only valid for directories.
1193 * 4. can only be changed on regular files if no extents are allocated
1194 * 5. can be changed on directories at any time
1195 * 6. extsize hint of 0 turns off hints, clears inode flags.
1196 * 7. Extent size must be a multiple of the appropriate block size.
1197 * 8. for non-realtime files, the extent size hint must be limited
1198 * to half the AG size to avoid alignment extending the extent beyond the
1201 * Please keep this function in sync with xfs_scrub_inode_extsize.
1204 xfs_ioctl_setattr_check_extsize(
1205 struct xfs_inode *ip,
1208 struct xfs_mount *mp = ip->i_mount;
1210 if ((fa->fsx_xflags & FS_XFLAG_EXTSIZE) && !S_ISREG(VFS_I(ip)->i_mode))
1213 if ((fa->fsx_xflags & FS_XFLAG_EXTSZINHERIT) &&
1214 !S_ISDIR(VFS_I(ip)->i_mode))
1217 if (S_ISREG(VFS_I(ip)->i_mode) && ip->i_d.di_nextents &&
1218 ((ip->i_d.di_extsize << mp->m_sb.sb_blocklog) != fa->fsx_extsize))
1221 if (fa->fsx_extsize != 0) {
1223 xfs_fsblock_t extsize_fsb;
1225 extsize_fsb = XFS_B_TO_FSB(mp, fa->fsx_extsize);
1226 if (extsize_fsb > MAXEXTLEN)
1229 if (XFS_IS_REALTIME_INODE(ip) ||
1230 (fa->fsx_xflags & FS_XFLAG_REALTIME)) {
1231 size = mp->m_sb.sb_rextsize << mp->m_sb.sb_blocklog;
1233 size = mp->m_sb.sb_blocksize;
1234 if (extsize_fsb > mp->m_sb.sb_agblocks / 2)
1238 if (fa->fsx_extsize % size)
1241 fa->fsx_xflags &= ~(FS_XFLAG_EXTSIZE | FS_XFLAG_EXTSZINHERIT);
1247 * CoW extent size hint validation rules are:
1249 * 1. CoW extent size hint can only be set if reflink is enabled on the fs.
1250 * The inode does not have to have any shared blocks, but it must be a v3.
1251 * 2. FS_XFLAG_COWEXTSIZE is only valid for directories and regular files;
1252 * for a directory, the hint is propagated to new files.
1253 * 3. Can be changed on files & directories at any time.
1254 * 4. CoW extsize hint of 0 turns off hints, clears inode flags.
1255 * 5. Extent size must be a multiple of the appropriate block size.
1256 * 6. The extent size hint must be limited to half the AG size to avoid
1257 * alignment extending the extent beyond the limits of the AG.
1259 * Please keep this function in sync with xfs_scrub_inode_cowextsize.
1262 xfs_ioctl_setattr_check_cowextsize(
1263 struct xfs_inode *ip,
1266 struct xfs_mount *mp = ip->i_mount;
1268 if (!(fa->fsx_xflags & FS_XFLAG_COWEXTSIZE))
1271 if (!xfs_sb_version_hasreflink(&ip->i_mount->m_sb) ||
1272 ip->i_d.di_version != 3)
1275 if (!S_ISREG(VFS_I(ip)->i_mode) && !S_ISDIR(VFS_I(ip)->i_mode))
1278 if (fa->fsx_cowextsize != 0) {
1280 xfs_fsblock_t cowextsize_fsb;
1282 cowextsize_fsb = XFS_B_TO_FSB(mp, fa->fsx_cowextsize);
1283 if (cowextsize_fsb > MAXEXTLEN)
1286 size = mp->m_sb.sb_blocksize;
1287 if (cowextsize_fsb > mp->m_sb.sb_agblocks / 2)
1290 if (fa->fsx_cowextsize % size)
1293 fa->fsx_xflags &= ~FS_XFLAG_COWEXTSIZE;
1299 xfs_ioctl_setattr_check_projid(
1300 struct xfs_inode *ip,
1303 /* Disallow 32bit project ids if projid32bit feature is not enabled. */
1304 if (fa->fsx_projid > (uint16_t)-1 &&
1305 !xfs_sb_version_hasprojid32bit(&ip->i_mount->m_sb))
1309 * Project Quota ID state is only allowed to change from within the init
1310 * namespace. Enforce that restriction only if we are trying to change
1311 * the quota ID state. Everything else is allowed in user namespaces.
1313 if (current_user_ns() == &init_user_ns)
1316 if (xfs_get_projid(ip) != fa->fsx_projid)
1318 if ((fa->fsx_xflags & FS_XFLAG_PROJINHERIT) !=
1319 (ip->i_d.di_flags & XFS_DIFLAG_PROJINHERIT))
1330 struct xfs_mount *mp = ip->i_mount;
1331 struct xfs_trans *tp;
1332 struct xfs_dquot *udqp = NULL;
1333 struct xfs_dquot *pdqp = NULL;
1334 struct xfs_dquot *olddquot = NULL;
1338 trace_xfs_ioctl_setattr(ip);
1340 code = xfs_ioctl_setattr_check_projid(ip, fa);
1345 * If disk quotas is on, we make sure that the dquots do exist on disk,
1346 * before we start any other transactions. Trying to do this later
1347 * is messy. We don't care to take a readlock to look at the ids
1348 * in inode here, because we can't hold it across the trans_reserve.
1349 * If the IDs do change before we take the ilock, we're covered
1350 * because the i_*dquot fields will get updated anyway.
1352 if (XFS_IS_QUOTA_ON(mp)) {
1353 code = xfs_qm_vop_dqalloc(ip, ip->i_d.di_uid,
1354 ip->i_d.di_gid, fa->fsx_projid,
1355 XFS_QMOPT_PQUOTA, &udqp, NULL, &pdqp);
1361 * Changing DAX config may require inode locking for mapping
1362 * invalidation. These need to be held all the way to transaction commit
1363 * or cancel time, so need to be passed through to
1364 * xfs_ioctl_setattr_get_trans() so it can apply them to the join call
1367 code = xfs_ioctl_setattr_dax_invalidate(ip, fa, &join_flags);
1369 goto error_free_dquots;
1371 tp = xfs_ioctl_setattr_get_trans(ip, join_flags);
1374 goto error_free_dquots;
1378 if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_PQUOTA_ON(mp) &&
1379 xfs_get_projid(ip) != fa->fsx_projid) {
1380 code = xfs_qm_vop_chown_reserve(tp, ip, udqp, NULL, pdqp,
1381 capable(CAP_FOWNER) ? XFS_QMOPT_FORCE_RES : 0);
1382 if (code) /* out of quota */
1383 goto error_trans_cancel;
1386 code = xfs_ioctl_setattr_check_extsize(ip, fa);
1388 goto error_trans_cancel;
1390 code = xfs_ioctl_setattr_check_cowextsize(ip, fa);
1392 goto error_trans_cancel;
1394 code = xfs_ioctl_setattr_xflags(tp, ip, fa);
1396 goto error_trans_cancel;
1399 * Change file ownership. Must be the owner or privileged. CAP_FSETID
1400 * overrides the following restrictions:
1402 * The set-user-ID and set-group-ID bits of a file will be cleared upon
1403 * successful return from chown()
1406 if ((VFS_I(ip)->i_mode & (S_ISUID|S_ISGID)) &&
1407 !capable_wrt_inode_uidgid(VFS_I(ip), CAP_FSETID))
1408 VFS_I(ip)->i_mode &= ~(S_ISUID|S_ISGID);
1410 /* Change the ownerships and register project quota modifications */
1411 if (xfs_get_projid(ip) != fa->fsx_projid) {
1412 if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_PQUOTA_ON(mp)) {
1413 olddquot = xfs_qm_vop_chown(tp, ip,
1414 &ip->i_pdquot, pdqp);
1416 ASSERT(ip->i_d.di_version > 1);
1417 xfs_set_projid(ip, fa->fsx_projid);
1421 * Only set the extent size hint if we've already determined that the
1422 * extent size hint should be set on the inode. If no extent size flags
1423 * are set on the inode then unconditionally clear the extent size hint.
1425 if (ip->i_d.di_flags & (XFS_DIFLAG_EXTSIZE | XFS_DIFLAG_EXTSZINHERIT))
1426 ip->i_d.di_extsize = fa->fsx_extsize >> mp->m_sb.sb_blocklog;
1428 ip->i_d.di_extsize = 0;
1429 if (ip->i_d.di_version == 3 &&
1430 (ip->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE))
1431 ip->i_d.di_cowextsize = fa->fsx_cowextsize >>
1432 mp->m_sb.sb_blocklog;
1434 ip->i_d.di_cowextsize = 0;
1436 code = xfs_trans_commit(tp);
1439 * Release any dquot(s) the inode had kept before chown.
1441 xfs_qm_dqrele(olddquot);
1442 xfs_qm_dqrele(udqp);
1443 xfs_qm_dqrele(pdqp);
1448 xfs_trans_cancel(tp);
1450 xfs_qm_dqrele(udqp);
1451 xfs_qm_dqrele(pdqp);
1464 if (copy_from_user(&fa, arg, sizeof(fa)))
1467 error = mnt_want_write_file(filp);
1470 error = xfs_ioctl_setattr(ip, &fa);
1471 mnt_drop_write_file(filp);
1482 flags = xfs_di2lxflags(ip->i_d.di_flags);
1483 if (copy_to_user(arg, &flags, sizeof(flags)))
1490 struct xfs_inode *ip,
1494 struct xfs_trans *tp;
1500 if (copy_from_user(&flags, arg, sizeof(flags)))
1503 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
1504 FS_NOATIME_FL | FS_NODUMP_FL | \
1508 fa.fsx_xflags = xfs_merge_ioc_xflags(flags, xfs_ip2xflags(ip));
1510 error = mnt_want_write_file(filp);
1515 * Changing DAX config may require inode locking for mapping
1516 * invalidation. These need to be held all the way to transaction commit
1517 * or cancel time, so need to be passed through to
1518 * xfs_ioctl_setattr_get_trans() so it can apply them to the join call
1521 error = xfs_ioctl_setattr_dax_invalidate(ip, &fa, &join_flags);
1523 goto out_drop_write;
1525 tp = xfs_ioctl_setattr_get_trans(ip, join_flags);
1527 error = PTR_ERR(tp);
1528 goto out_drop_write;
1531 error = xfs_ioctl_setattr_xflags(tp, ip, &fa);
1533 xfs_trans_cancel(tp);
1534 goto out_drop_write;
1537 error = xfs_trans_commit(tp);
1539 mnt_drop_write_file(filp);
1546 struct getbmapx __user *u,
1549 if (put_user(p->bmv_offset, &u->bmv_offset) ||
1550 put_user(p->bmv_block, &u->bmv_block) ||
1551 put_user(p->bmv_length, &u->bmv_length) ||
1552 put_user(0, &u->bmv_count) ||
1553 put_user(0, &u->bmv_entries))
1555 if (recsize < sizeof(struct getbmapx))
1557 if (put_user(0, &u->bmv_iflags) ||
1558 put_user(p->bmv_oflags, &u->bmv_oflags) ||
1559 put_user(0, &u->bmv_unused1) ||
1560 put_user(0, &u->bmv_unused2))
1571 struct getbmapx bmx = { 0 };
1572 struct kgetbmap *buf;
1577 case XFS_IOC_GETBMAPA:
1578 bmx.bmv_iflags = BMV_IF_ATTRFORK;
1580 case XFS_IOC_GETBMAP:
1581 if (file->f_mode & FMODE_NOCMTIME)
1582 bmx.bmv_iflags |= BMV_IF_NO_DMAPI_READ;
1583 /* struct getbmap is a strict subset of struct getbmapx. */
1584 recsize = sizeof(struct getbmap);
1586 case XFS_IOC_GETBMAPX:
1587 recsize = sizeof(struct getbmapx);
1593 if (copy_from_user(&bmx, arg, recsize))
1596 if (bmx.bmv_count < 2)
1598 if (bmx.bmv_count > ULONG_MAX / recsize)
1601 buf = kmem_zalloc_large(bmx.bmv_count * sizeof(*buf), 0);
1605 error = xfs_getbmap(XFS_I(file_inode(file)), &bmx, buf);
1610 if (copy_to_user(arg, &bmx, recsize))
1614 for (i = 0; i < bmx.bmv_entries; i++) {
1615 if (!xfs_getbmap_format(buf + i, arg, recsize))
1626 struct getfsmap_info {
1627 struct xfs_mount *mp;
1628 struct fsmap_head __user *data;
1634 xfs_getfsmap_format(struct xfs_fsmap *xfm, void *priv)
1636 struct getfsmap_info *info = priv;
1639 trace_xfs_getfsmap_mapping(info->mp, xfm);
1641 info->last_flags = xfm->fmr_flags;
1642 xfs_fsmap_from_internal(&fm, xfm);
1643 if (copy_to_user(&info->data->fmh_recs[info->idx++], &fm,
1644 sizeof(struct fsmap)))
1652 struct xfs_inode *ip,
1653 struct fsmap_head __user *arg)
1655 struct getfsmap_info info = { NULL };
1656 struct xfs_fsmap_head xhead = {0};
1657 struct fsmap_head head;
1658 bool aborted = false;
1661 if (copy_from_user(&head, arg, sizeof(struct fsmap_head)))
1663 if (memchr_inv(head.fmh_reserved, 0, sizeof(head.fmh_reserved)) ||
1664 memchr_inv(head.fmh_keys[0].fmr_reserved, 0,
1665 sizeof(head.fmh_keys[0].fmr_reserved)) ||
1666 memchr_inv(head.fmh_keys[1].fmr_reserved, 0,
1667 sizeof(head.fmh_keys[1].fmr_reserved)))
1670 xhead.fmh_iflags = head.fmh_iflags;
1671 xhead.fmh_count = head.fmh_count;
1672 xfs_fsmap_to_internal(&xhead.fmh_keys[0], &head.fmh_keys[0]);
1673 xfs_fsmap_to_internal(&xhead.fmh_keys[1], &head.fmh_keys[1]);
1675 trace_xfs_getfsmap_low_key(ip->i_mount, &xhead.fmh_keys[0]);
1676 trace_xfs_getfsmap_high_key(ip->i_mount, &xhead.fmh_keys[1]);
1678 info.mp = ip->i_mount;
1680 error = xfs_getfsmap(ip->i_mount, &xhead, xfs_getfsmap_format, &info);
1681 if (error == XFS_BTREE_QUERY_RANGE_ABORT) {
1687 /* If we didn't abort, set the "last" flag in the last fmx */
1688 if (!aborted && info.idx) {
1689 info.last_flags |= FMR_OF_LAST;
1690 if (copy_to_user(&info.data->fmh_recs[info.idx - 1].fmr_flags,
1691 &info.last_flags, sizeof(info.last_flags)))
1695 /* copy back header */
1696 head.fmh_entries = xhead.fmh_entries;
1697 head.fmh_oflags = xhead.fmh_oflags;
1698 if (copy_to_user(arg, &head, sizeof(struct fsmap_head)))
1705 xfs_ioc_scrub_metadata(
1706 struct xfs_inode *ip,
1709 struct xfs_scrub_metadata scrub;
1712 if (!capable(CAP_SYS_ADMIN))
1715 if (copy_from_user(&scrub, arg, sizeof(scrub)))
1718 error = xfs_scrub_metadata(ip, &scrub);
1722 if (copy_to_user(arg, &scrub, sizeof(scrub)))
1732 xfs_inode_t *ip, *tip;
1736 /* Pull information for the target fd */
1737 f = fdget((int)sxp->sx_fdtarget);
1743 if (!(f.file->f_mode & FMODE_WRITE) ||
1744 !(f.file->f_mode & FMODE_READ) ||
1745 (f.file->f_flags & O_APPEND)) {
1750 tmp = fdget((int)sxp->sx_fdtmp);
1756 if (!(tmp.file->f_mode & FMODE_WRITE) ||
1757 !(tmp.file->f_mode & FMODE_READ) ||
1758 (tmp.file->f_flags & O_APPEND)) {
1760 goto out_put_tmp_file;
1763 if (IS_SWAPFILE(file_inode(f.file)) ||
1764 IS_SWAPFILE(file_inode(tmp.file))) {
1766 goto out_put_tmp_file;
1770 * We need to ensure that the fds passed in point to XFS inodes
1771 * before we cast and access them as XFS structures as we have no
1772 * control over what the user passes us here.
1774 if (f.file->f_op != &xfs_file_operations ||
1775 tmp.file->f_op != &xfs_file_operations) {
1777 goto out_put_tmp_file;
1780 ip = XFS_I(file_inode(f.file));
1781 tip = XFS_I(file_inode(tmp.file));
1783 if (ip->i_mount != tip->i_mount) {
1785 goto out_put_tmp_file;
1788 if (ip->i_ino == tip->i_ino) {
1790 goto out_put_tmp_file;
1793 if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
1795 goto out_put_tmp_file;
1798 error = xfs_swap_extents(ip, tip, sxp);
1810 struct xfs_mount *mp,
1811 char __user *user_label)
1813 struct xfs_sb *sbp = &mp->m_sb;
1814 char label[XFSLABEL_MAX + 1];
1817 BUILD_BUG_ON(sizeof(sbp->sb_fname) > FSLABEL_MAX);
1819 spin_lock(&mp->m_sb_lock);
1820 strncpy(label, sbp->sb_fname, sizeof(sbp->sb_fname));
1821 spin_unlock(&mp->m_sb_lock);
1823 /* xfs on-disk label is 12 chars, be sure we send a null to user */
1824 label[XFSLABEL_MAX] = '\0';
1825 if (copy_to_user(user_label, label, sizeof(sbp->sb_fname)))
1833 struct xfs_mount *mp,
1834 char __user *newlabel)
1836 struct xfs_sb *sbp = &mp->m_sb;
1837 char label[XFSLABEL_MAX + 1];
1841 if (!capable(CAP_SYS_ADMIN))
1844 * The generic ioctl allows up to FSLABEL_MAX chars, but XFS is much
1845 * smaller, at 12 bytes. We copy one more to be sure we find the
1846 * (required) NULL character to test the incoming label length.
1847 * NB: The on disk label doesn't need to be null terminated.
1849 if (copy_from_user(label, newlabel, XFSLABEL_MAX + 1))
1851 len = strnlen(label, XFSLABEL_MAX + 1);
1852 if (len > sizeof(sbp->sb_fname))
1855 error = mnt_want_write_file(filp);
1859 spin_lock(&mp->m_sb_lock);
1860 memset(sbp->sb_fname, 0, sizeof(sbp->sb_fname));
1861 strncpy(sbp->sb_fname, label, sizeof(sbp->sb_fname));
1862 spin_unlock(&mp->m_sb_lock);
1865 * Now we do several things to satisfy userspace.
1866 * In addition to normal logging of the primary superblock, we also
1867 * immediately write these changes to sector zero for the primary, then
1868 * update all backup supers (as xfs_db does for a label change), then
1869 * invalidate the block device page cache. This is so that any prior
1870 * buffered reads from userspace (i.e. from blkid) are invalidated,
1871 * and userspace will see the newly-written label.
1873 error = xfs_sync_sb_buf(mp);
1877 * growfs also updates backup supers so lock against that.
1879 mutex_lock(&mp->m_growlock);
1880 error = xfs_update_secondary_sbs(mp);
1881 mutex_unlock(&mp->m_growlock);
1883 invalidate_bdev(mp->m_ddev_targp->bt_bdev);
1886 mnt_drop_write_file(filp);
1891 * Note: some of the ioctl's return positive numbers as a
1892 * byte count indicating success, such as readlink_by_handle.
1893 * So we don't "sign flip" like most other routines. This means
1894 * true errors need to be returned as a negative value.
1902 struct inode *inode = file_inode(filp);
1903 struct xfs_inode *ip = XFS_I(inode);
1904 struct xfs_mount *mp = ip->i_mount;
1905 void __user *arg = (void __user *)p;
1908 trace_xfs_file_ioctl(ip);
1912 return xfs_ioc_trim(mp, arg);
1913 case FS_IOC_GETFSLABEL:
1914 return xfs_ioc_getlabel(mp, arg);
1915 case FS_IOC_SETFSLABEL:
1916 return xfs_ioc_setlabel(filp, mp, arg);
1917 case XFS_IOC_ALLOCSP:
1918 case XFS_IOC_FREESP:
1919 case XFS_IOC_RESVSP:
1920 case XFS_IOC_UNRESVSP:
1921 case XFS_IOC_ALLOCSP64:
1922 case XFS_IOC_FREESP64:
1923 case XFS_IOC_RESVSP64:
1924 case XFS_IOC_UNRESVSP64:
1925 case XFS_IOC_ZERO_RANGE: {
1928 if (copy_from_user(&bf, arg, sizeof(bf)))
1930 return xfs_ioc_space(filp, cmd, &bf);
1932 case XFS_IOC_DIOINFO: {
1934 xfs_buftarg_t *target =
1935 XFS_IS_REALTIME_INODE(ip) ?
1936 mp->m_rtdev_targp : mp->m_ddev_targp;
1938 da.d_mem = da.d_miniosz = target->bt_logical_sectorsize;
1939 da.d_maxiosz = INT_MAX & ~(da.d_miniosz - 1);
1941 if (copy_to_user(arg, &da, sizeof(da)))
1946 case XFS_IOC_FSBULKSTAT_SINGLE:
1947 case XFS_IOC_FSBULKSTAT:
1948 case XFS_IOC_FSINUMBERS:
1949 return xfs_ioc_bulkstat(mp, cmd, arg);
1951 case XFS_IOC_FSGEOMETRY_V1:
1952 return xfs_ioc_fsgeometry_v1(mp, arg);
1954 case XFS_IOC_FSGEOMETRY:
1955 return xfs_ioc_fsgeometry(mp, arg);
1957 case XFS_IOC_GETVERSION:
1958 return put_user(inode->i_generation, (int __user *)arg);
1960 case XFS_IOC_FSGETXATTR:
1961 return xfs_ioc_fsgetxattr(ip, 0, arg);
1962 case XFS_IOC_FSGETXATTRA:
1963 return xfs_ioc_fsgetxattr(ip, 1, arg);
1964 case XFS_IOC_FSSETXATTR:
1965 return xfs_ioc_fssetxattr(ip, filp, arg);
1966 case XFS_IOC_GETXFLAGS:
1967 return xfs_ioc_getxflags(ip, arg);
1968 case XFS_IOC_SETXFLAGS:
1969 return xfs_ioc_setxflags(ip, filp, arg);
1971 case XFS_IOC_FSSETDM: {
1972 struct fsdmidata dmi;
1974 if (copy_from_user(&dmi, arg, sizeof(dmi)))
1977 error = mnt_want_write_file(filp);
1981 error = xfs_set_dmattrs(ip, dmi.fsd_dmevmask,
1983 mnt_drop_write_file(filp);
1987 case XFS_IOC_GETBMAP:
1988 case XFS_IOC_GETBMAPA:
1989 case XFS_IOC_GETBMAPX:
1990 return xfs_ioc_getbmap(filp, cmd, arg);
1992 case FS_IOC_GETFSMAP:
1993 return xfs_ioc_getfsmap(ip, arg);
1995 case XFS_IOC_SCRUB_METADATA:
1996 return xfs_ioc_scrub_metadata(ip, arg);
1998 case XFS_IOC_FD_TO_HANDLE:
1999 case XFS_IOC_PATH_TO_HANDLE:
2000 case XFS_IOC_PATH_TO_FSHANDLE: {
2001 xfs_fsop_handlereq_t hreq;
2003 if (copy_from_user(&hreq, arg, sizeof(hreq)))
2005 return xfs_find_handle(cmd, &hreq);
2007 case XFS_IOC_OPEN_BY_HANDLE: {
2008 xfs_fsop_handlereq_t hreq;
2010 if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
2012 return xfs_open_by_handle(filp, &hreq);
2014 case XFS_IOC_FSSETDM_BY_HANDLE:
2015 return xfs_fssetdm_by_handle(filp, arg);
2017 case XFS_IOC_READLINK_BY_HANDLE: {
2018 xfs_fsop_handlereq_t hreq;
2020 if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
2022 return xfs_readlink_by_handle(filp, &hreq);
2024 case XFS_IOC_ATTRLIST_BY_HANDLE:
2025 return xfs_attrlist_by_handle(filp, arg);
2027 case XFS_IOC_ATTRMULTI_BY_HANDLE:
2028 return xfs_attrmulti_by_handle(filp, arg);
2030 case XFS_IOC_SWAPEXT: {
2031 struct xfs_swapext sxp;
2033 if (copy_from_user(&sxp, arg, sizeof(xfs_swapext_t)))
2035 error = mnt_want_write_file(filp);
2038 error = xfs_ioc_swapext(&sxp);
2039 mnt_drop_write_file(filp);
2043 case XFS_IOC_FSCOUNTS: {
2044 xfs_fsop_counts_t out;
2046 error = xfs_fs_counts(mp, &out);
2050 if (copy_to_user(arg, &out, sizeof(out)))
2055 case XFS_IOC_SET_RESBLKS: {
2056 xfs_fsop_resblks_t inout;
2059 if (!capable(CAP_SYS_ADMIN))
2062 if (mp->m_flags & XFS_MOUNT_RDONLY)
2065 if (copy_from_user(&inout, arg, sizeof(inout)))
2068 error = mnt_want_write_file(filp);
2072 /* input parameter is passed in resblks field of structure */
2074 error = xfs_reserve_blocks(mp, &in, &inout);
2075 mnt_drop_write_file(filp);
2079 if (copy_to_user(arg, &inout, sizeof(inout)))
2084 case XFS_IOC_GET_RESBLKS: {
2085 xfs_fsop_resblks_t out;
2087 if (!capable(CAP_SYS_ADMIN))
2090 error = xfs_reserve_blocks(mp, NULL, &out);
2094 if (copy_to_user(arg, &out, sizeof(out)))
2100 case XFS_IOC_FSGROWFSDATA: {
2101 xfs_growfs_data_t in;
2103 if (copy_from_user(&in, arg, sizeof(in)))
2106 error = mnt_want_write_file(filp);
2109 error = xfs_growfs_data(mp, &in);
2110 mnt_drop_write_file(filp);
2114 case XFS_IOC_FSGROWFSLOG: {
2115 xfs_growfs_log_t in;
2117 if (copy_from_user(&in, arg, sizeof(in)))
2120 error = mnt_want_write_file(filp);
2123 error = xfs_growfs_log(mp, &in);
2124 mnt_drop_write_file(filp);
2128 case XFS_IOC_FSGROWFSRT: {
2131 if (copy_from_user(&in, arg, sizeof(in)))
2134 error = mnt_want_write_file(filp);
2137 error = xfs_growfs_rt(mp, &in);
2138 mnt_drop_write_file(filp);
2142 case XFS_IOC_GOINGDOWN: {
2145 if (!capable(CAP_SYS_ADMIN))
2148 if (get_user(in, (uint32_t __user *)arg))
2151 return xfs_fs_goingdown(mp, in);
2154 case XFS_IOC_ERROR_INJECTION: {
2155 xfs_error_injection_t in;
2157 if (!capable(CAP_SYS_ADMIN))
2160 if (copy_from_user(&in, arg, sizeof(in)))
2163 return xfs_errortag_add(mp, in.errtag);
2166 case XFS_IOC_ERROR_CLEARALL:
2167 if (!capable(CAP_SYS_ADMIN))
2170 return xfs_errortag_clearall(mp);
2172 case XFS_IOC_FREE_EOFBLOCKS: {
2173 struct xfs_fs_eofblocks eofb;
2174 struct xfs_eofblocks keofb;
2176 if (!capable(CAP_SYS_ADMIN))
2179 if (mp->m_flags & XFS_MOUNT_RDONLY)
2182 if (copy_from_user(&eofb, arg, sizeof(eofb)))
2185 error = xfs_fs_eofblocks_from_user(&eofb, &keofb);
2189 return xfs_icache_free_eofblocks(mp, &keofb);