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_rtalloc.h"
15 #include "xfs_iwalk.h"
16 #include "xfs_itable.h"
17 #include "xfs_error.h"
20 #include "xfs_bmap_util.h"
21 #include "xfs_fsops.h"
22 #include "xfs_discard.h"
23 #include "xfs_quota.h"
24 #include "xfs_export.h"
25 #include "xfs_trace.h"
26 #include "xfs_icache.h"
27 #include "xfs_trans.h"
29 #include "xfs_btree.h"
30 #include <linux/fsmap.h>
31 #include "xfs_fsmap.h"
32 #include "scrub/xfs_scrub.h"
35 #include "xfs_health.h"
36 #include "xfs_reflink.h"
37 #include "xfs_ioctl.h"
39 #include <linux/mount.h>
40 #include <linux/namei.h>
43 * xfs_find_handle maps from userspace xfs_fsop_handlereq structure to
44 * a file or fs handle.
46 * XFS_IOC_PATH_TO_FSHANDLE
47 * returns fs handle for a mount point or path within that mount point
48 * XFS_IOC_FD_TO_HANDLE
49 * returns full handle for a FD opened in user space
50 * XFS_IOC_PATH_TO_HANDLE
51 * returns full handle for a path
56 xfs_fsop_handlereq_t *hreq)
66 if (cmd == XFS_IOC_FD_TO_HANDLE) {
70 inode = file_inode(f.file);
72 error = user_path_at(AT_FDCWD, hreq->path, 0, &path);
75 inode = d_inode(path.dentry);
80 * We can only generate handles for inodes residing on a XFS filesystem,
81 * and only for regular files, directories or symbolic links.
84 if (inode->i_sb->s_magic != XFS_SB_MAGIC)
88 if (!S_ISREG(inode->i_mode) &&
89 !S_ISDIR(inode->i_mode) &&
90 !S_ISLNK(inode->i_mode))
94 memcpy(&handle.ha_fsid, ip->i_mount->m_fixedfsid, sizeof(xfs_fsid_t));
96 if (cmd == XFS_IOC_PATH_TO_FSHANDLE) {
98 * This handle only contains an fsid, zero the rest.
100 memset(&handle.ha_fid, 0, sizeof(handle.ha_fid));
101 hsize = sizeof(xfs_fsid_t);
103 handle.ha_fid.fid_len = sizeof(xfs_fid_t) -
104 sizeof(handle.ha_fid.fid_len);
105 handle.ha_fid.fid_pad = 0;
106 handle.ha_fid.fid_gen = inode->i_generation;
107 handle.ha_fid.fid_ino = ip->i_ino;
108 hsize = sizeof(xfs_handle_t);
112 if (copy_to_user(hreq->ohandle, &handle, hsize) ||
113 copy_to_user(hreq->ohandlen, &hsize, sizeof(__s32)))
119 if (cmd == XFS_IOC_FD_TO_HANDLE)
127 * No need to do permission checks on the various pathname components
128 * as the handle operations are privileged.
131 xfs_handle_acceptable(
133 struct dentry *dentry)
139 * Convert userspace handle data into a dentry.
142 xfs_handle_to_dentry(
143 struct file *parfilp,
144 void __user *uhandle,
148 struct xfs_fid64 fid;
151 * Only allow handle opens under a directory.
153 if (!S_ISDIR(file_inode(parfilp)->i_mode))
154 return ERR_PTR(-ENOTDIR);
156 if (hlen != sizeof(xfs_handle_t))
157 return ERR_PTR(-EINVAL);
158 if (copy_from_user(&handle, uhandle, hlen))
159 return ERR_PTR(-EFAULT);
160 if (handle.ha_fid.fid_len !=
161 sizeof(handle.ha_fid) - sizeof(handle.ha_fid.fid_len))
162 return ERR_PTR(-EINVAL);
164 memset(&fid, 0, sizeof(struct fid));
165 fid.ino = handle.ha_fid.fid_ino;
166 fid.gen = handle.ha_fid.fid_gen;
168 return exportfs_decode_fh(parfilp->f_path.mnt, (struct fid *)&fid, 3,
169 FILEID_INO32_GEN | XFS_FILEID_TYPE_64FLAG,
170 xfs_handle_acceptable, NULL);
173 STATIC struct dentry *
174 xfs_handlereq_to_dentry(
175 struct file *parfilp,
176 xfs_fsop_handlereq_t *hreq)
178 return xfs_handle_to_dentry(parfilp, hreq->ihandle, hreq->ihandlen);
183 struct file *parfilp,
184 xfs_fsop_handlereq_t *hreq)
186 const struct cred *cred = current_cred();
192 struct dentry *dentry;
196 if (!capable(CAP_SYS_ADMIN))
199 dentry = xfs_handlereq_to_dentry(parfilp, hreq);
201 return PTR_ERR(dentry);
202 inode = d_inode(dentry);
204 /* Restrict xfs_open_by_handle to directories & regular files. */
205 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode))) {
210 #if BITS_PER_LONG != 32
211 hreq->oflags |= O_LARGEFILE;
214 permflag = hreq->oflags;
215 fmode = OPEN_FMODE(permflag);
216 if ((!(permflag & O_APPEND) || (permflag & O_TRUNC)) &&
217 (fmode & FMODE_WRITE) && IS_APPEND(inode)) {
222 if ((fmode & FMODE_WRITE) && IS_IMMUTABLE(inode)) {
227 /* Can't write directories. */
228 if (S_ISDIR(inode->i_mode) && (fmode & FMODE_WRITE)) {
233 fd = get_unused_fd_flags(0);
239 path.mnt = parfilp->f_path.mnt;
240 path.dentry = dentry;
241 filp = dentry_open(&path, hreq->oflags, cred);
245 return PTR_ERR(filp);
248 if (S_ISREG(inode->i_mode)) {
249 filp->f_flags |= O_NOATIME;
250 filp->f_mode |= FMODE_NOCMTIME;
253 fd_install(fd, filp);
262 xfs_readlink_by_handle(
263 struct file *parfilp,
264 xfs_fsop_handlereq_t *hreq)
266 struct dentry *dentry;
270 if (!capable(CAP_SYS_ADMIN))
273 dentry = xfs_handlereq_to_dentry(parfilp, hreq);
275 return PTR_ERR(dentry);
277 /* Restrict this handle operation to symlinks only. */
278 if (!d_is_symlink(dentry)) {
283 if (copy_from_user(&olen, hreq->ohandlen, sizeof(__u32))) {
288 error = vfs_readlink(dentry, hreq->ohandle, olen);
296 xfs_attrlist_by_handle(
297 struct file *parfilp,
301 attrlist_cursor_kern_t *cursor;
302 struct xfs_fsop_attrlist_handlereq __user *p = arg;
303 xfs_fsop_attrlist_handlereq_t al_hreq;
304 struct dentry *dentry;
307 if (!capable(CAP_SYS_ADMIN))
309 if (copy_from_user(&al_hreq, arg, sizeof(xfs_fsop_attrlist_handlereq_t)))
311 if (al_hreq.buflen < sizeof(struct attrlist) ||
312 al_hreq.buflen > XFS_XATTR_LIST_MAX)
316 * Reject flags, only allow namespaces.
318 if (al_hreq.flags & ~(ATTR_ROOT | ATTR_SECURE))
321 dentry = xfs_handlereq_to_dentry(parfilp, &al_hreq.hreq);
323 return PTR_ERR(dentry);
325 kbuf = kmem_zalloc_large(al_hreq.buflen, 0);
329 cursor = (attrlist_cursor_kern_t *)&al_hreq.pos;
330 error = xfs_attr_list(XFS_I(d_inode(dentry)), kbuf, al_hreq.buflen,
331 al_hreq.flags, cursor);
335 if (copy_to_user(&p->pos, cursor, sizeof(attrlist_cursor_kern_t))) {
340 if (copy_to_user(al_hreq.buffer, kbuf, al_hreq.buflen))
351 xfs_attrmulti_attr_get(
354 unsigned char __user *ubuf,
361 if (*len > XFS_XATTR_SIZE_MAX)
363 kbuf = kmem_zalloc_large(*len, 0);
367 error = xfs_attr_get(XFS_I(inode), name, &kbuf, (int *)len, flags);
371 if (copy_to_user(ubuf, kbuf, *len))
380 xfs_attrmulti_attr_set(
383 const unsigned char __user *ubuf,
390 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
392 if (len > XFS_XATTR_SIZE_MAX)
395 kbuf = memdup_user(ubuf, len);
397 return PTR_ERR(kbuf);
399 error = xfs_attr_set(XFS_I(inode), name, kbuf, len, flags);
401 xfs_forget_acl(inode, name, flags);
407 xfs_attrmulti_attr_remove(
414 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
416 error = xfs_attr_remove(XFS_I(inode), name, flags);
418 xfs_forget_acl(inode, name, flags);
423 xfs_attrmulti_by_handle(
424 struct file *parfilp,
428 xfs_attr_multiop_t *ops;
429 xfs_fsop_attrmulti_handlereq_t am_hreq;
430 struct dentry *dentry;
431 unsigned int i, size;
432 unsigned char *attr_name;
434 if (!capable(CAP_SYS_ADMIN))
436 if (copy_from_user(&am_hreq, arg, sizeof(xfs_fsop_attrmulti_handlereq_t)))
440 if (am_hreq.opcount >= INT_MAX / sizeof(xfs_attr_multiop_t))
443 dentry = xfs_handlereq_to_dentry(parfilp, &am_hreq.hreq);
445 return PTR_ERR(dentry);
448 size = am_hreq.opcount * sizeof(xfs_attr_multiop_t);
449 if (!size || size > 16 * PAGE_SIZE)
452 ops = memdup_user(am_hreq.ops, size);
454 error = PTR_ERR(ops);
459 attr_name = kmalloc(MAXNAMELEN, GFP_KERNEL);
464 for (i = 0; i < am_hreq.opcount; i++) {
465 ops[i].am_error = strncpy_from_user((char *)attr_name,
466 ops[i].am_attrname, MAXNAMELEN);
467 if (ops[i].am_error == 0 || ops[i].am_error == MAXNAMELEN)
469 if (ops[i].am_error < 0)
472 switch (ops[i].am_opcode) {
474 ops[i].am_error = xfs_attrmulti_attr_get(
475 d_inode(dentry), attr_name,
476 ops[i].am_attrvalue, &ops[i].am_length,
480 ops[i].am_error = mnt_want_write_file(parfilp);
483 ops[i].am_error = xfs_attrmulti_attr_set(
484 d_inode(dentry), attr_name,
485 ops[i].am_attrvalue, ops[i].am_length,
487 mnt_drop_write_file(parfilp);
490 ops[i].am_error = mnt_want_write_file(parfilp);
493 ops[i].am_error = xfs_attrmulti_attr_remove(
494 d_inode(dentry), attr_name,
496 mnt_drop_write_file(parfilp);
499 ops[i].am_error = -EINVAL;
503 if (copy_to_user(am_hreq.ops, ops, size))
519 struct inode *inode = file_inode(filp);
520 struct xfs_inode *ip = XFS_I(inode);
522 enum xfs_prealloc_flags flags = XFS_PREALLOC_CLEAR;
523 uint iolock = XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL;
526 if (inode->i_flags & (S_IMMUTABLE|S_APPEND))
529 if (!(filp->f_mode & FMODE_WRITE))
532 if (!S_ISREG(inode->i_mode))
535 if (xfs_is_always_cow_inode(ip))
538 if (filp->f_flags & O_DSYNC)
539 flags |= XFS_PREALLOC_SYNC;
540 if (filp->f_mode & FMODE_NOCMTIME)
541 flags |= XFS_PREALLOC_INVISIBLE;
543 error = mnt_want_write_file(filp);
547 xfs_ilock(ip, iolock);
548 error = xfs_break_layouts(inode, &iolock, BREAK_UNMAP);
551 inode_dio_wait(inode);
553 switch (bf->l_whence) {
557 bf->l_start += filp->f_pos;
560 bf->l_start += XFS_ISIZE(ip);
567 if (bf->l_start < 0 || bf->l_start > inode->i_sb->s_maxbytes) {
572 if (bf->l_start > XFS_ISIZE(ip)) {
573 error = xfs_alloc_file_space(ip, XFS_ISIZE(ip),
574 bf->l_start - XFS_ISIZE(ip), 0);
579 iattr.ia_valid = ATTR_SIZE;
580 iattr.ia_size = bf->l_start;
581 error = xfs_vn_setattr_size(file_dentry(filp), &iattr);
585 error = xfs_update_prealloc_flags(ip, flags);
588 xfs_iunlock(ip, iolock);
589 mnt_drop_write_file(filp);
593 /* Return 0 on success or positive error */
595 xfs_fsbulkstat_one_fmt(
596 struct xfs_ibulk *breq,
597 const struct xfs_bulkstat *bstat)
599 struct xfs_bstat bs1;
601 xfs_bulkstat_to_bstat(breq->mp, &bs1, bstat);
602 if (copy_to_user(breq->ubuffer, &bs1, sizeof(bs1)))
604 return xfs_ibulk_advance(breq, sizeof(struct xfs_bstat));
609 struct xfs_ibulk *breq,
610 const struct xfs_inumbers *igrp)
612 struct xfs_inogrp ig1;
614 xfs_inumbers_to_inogrp(&ig1, igrp);
615 if (copy_to_user(breq->ubuffer, &ig1, sizeof(struct xfs_inogrp)))
617 return xfs_ibulk_advance(breq, sizeof(struct xfs_inogrp));
626 struct xfs_fsop_bulkreq bulkreq;
627 struct xfs_ibulk breq = {
634 /* done = 1 if there are more stats to get and if bulkstat */
635 /* should be called again (unused here, but used in dmapi) */
637 if (!capable(CAP_SYS_ADMIN))
640 if (XFS_FORCED_SHUTDOWN(mp))
643 if (copy_from_user(&bulkreq, arg, sizeof(struct xfs_fsop_bulkreq)))
646 if (copy_from_user(&lastino, bulkreq.lastip, sizeof(__s64)))
649 if (bulkreq.icount <= 0)
652 if (bulkreq.ubuffer == NULL)
655 breq.ubuffer = bulkreq.ubuffer;
656 breq.icount = bulkreq.icount;
659 * FSBULKSTAT_SINGLE expects that *lastip contains the inode number
660 * that we want to stat. However, FSINUMBERS and FSBULKSTAT expect
661 * that *lastip contains either zero or the number of the last inode to
662 * be examined by the previous call and return results starting with
663 * the next inode after that. The new bulk request back end functions
664 * take the inode to start with, so we have to compute the startino
665 * parameter from lastino to maintain correct function. lastino == 0
666 * is a special case because it has traditionally meant "first inode
669 if (cmd == XFS_IOC_FSINUMBERS) {
670 breq.startino = lastino ? lastino + 1 : 0;
671 error = xfs_inumbers(&breq, xfs_fsinumbers_fmt);
672 lastino = breq.startino - 1;
673 } else if (cmd == XFS_IOC_FSBULKSTAT_SINGLE) {
674 breq.startino = lastino;
676 error = xfs_bulkstat_one(&breq, xfs_fsbulkstat_one_fmt);
677 } else { /* XFS_IOC_FSBULKSTAT */
678 breq.startino = lastino ? lastino + 1 : 0;
679 error = xfs_bulkstat(&breq, xfs_fsbulkstat_one_fmt);
680 lastino = breq.startino - 1;
686 if (bulkreq.lastip != NULL &&
687 copy_to_user(bulkreq.lastip, &lastino, sizeof(xfs_ino_t)))
690 if (bulkreq.ocount != NULL &&
691 copy_to_user(bulkreq.ocount, &breq.ocount, sizeof(__s32)))
697 /* Return 0 on success or positive error */
700 struct xfs_ibulk *breq,
701 const struct xfs_bulkstat *bstat)
703 if (copy_to_user(breq->ubuffer, bstat, sizeof(struct xfs_bulkstat)))
705 return xfs_ibulk_advance(breq, sizeof(struct xfs_bulkstat));
709 * Check the incoming bulk request @hdr from userspace and initialize the
710 * internal @breq bulk request appropriately. Returns 0 if the bulk request
711 * should proceed; -ECANCELED if there's nothing to do; or the usual
712 * negative error code.
716 struct xfs_mount *mp,
717 struct xfs_bulk_ireq *hdr,
718 struct xfs_ibulk *breq,
719 void __user *ubuffer)
721 if (hdr->icount == 0 ||
722 (hdr->flags & ~XFS_BULK_IREQ_FLAGS_ALL) ||
723 memchr_inv(hdr->reserved, 0, sizeof(hdr->reserved)))
726 breq->startino = hdr->ino;
727 breq->ubuffer = ubuffer;
728 breq->icount = hdr->icount;
733 * The @ino parameter is a special value, so we must look it up here.
734 * We're not allowed to have IREQ_AGNO, and we only return one inode
737 if (hdr->flags & XFS_BULK_IREQ_SPECIAL) {
738 if (hdr->flags & XFS_BULK_IREQ_AGNO)
742 case XFS_BULK_IREQ_SPECIAL_ROOT:
743 hdr->ino = mp->m_sb.sb_rootino;
752 * The IREQ_AGNO flag means that we only want results from a given AG.
753 * If @hdr->ino is zero, we start iterating in that AG. If @hdr->ino is
754 * beyond the specified AG then we return no results.
756 if (hdr->flags & XFS_BULK_IREQ_AGNO) {
757 if (hdr->agno >= mp->m_sb.sb_agcount)
760 if (breq->startino == 0)
761 breq->startino = XFS_AGINO_TO_INO(mp, hdr->agno, 0);
762 else if (XFS_INO_TO_AGNO(mp, breq->startino) < hdr->agno)
765 breq->flags |= XFS_IBULK_SAME_AG;
767 /* Asking for an inode past the end of the AG? We're done! */
768 if (XFS_INO_TO_AGNO(mp, breq->startino) > hdr->agno)
770 } else if (hdr->agno)
773 /* Asking for an inode past the end of the FS? We're done! */
774 if (XFS_INO_TO_AGNO(mp, breq->startino) >= mp->m_sb.sb_agcount)
781 * Update the userspace bulk request @hdr to reflect the end state of the
782 * internal bulk request @breq.
785 xfs_bulk_ireq_teardown(
786 struct xfs_bulk_ireq *hdr,
787 struct xfs_ibulk *breq)
789 hdr->ino = breq->startino;
790 hdr->ocount = breq->ocount;
793 /* Handle the v5 bulkstat ioctl. */
796 struct xfs_mount *mp,
798 struct xfs_bulkstat_req __user *arg)
800 struct xfs_bulk_ireq hdr;
801 struct xfs_ibulk breq = {
806 if (!capable(CAP_SYS_ADMIN))
809 if (XFS_FORCED_SHUTDOWN(mp))
812 if (copy_from_user(&hdr, &arg->hdr, sizeof(hdr)))
815 error = xfs_bulk_ireq_setup(mp, &hdr, &breq, arg->bulkstat);
816 if (error == -ECANCELED)
821 error = xfs_bulkstat(&breq, xfs_bulkstat_fmt);
826 xfs_bulk_ireq_teardown(&hdr, &breq);
827 if (copy_to_user(&arg->hdr, &hdr, sizeof(hdr)))
835 struct xfs_ibulk *breq,
836 const struct xfs_inumbers *igrp)
838 if (copy_to_user(breq->ubuffer, igrp, sizeof(struct xfs_inumbers)))
840 return xfs_ibulk_advance(breq, sizeof(struct xfs_inumbers));
843 /* Handle the v5 inumbers ioctl. */
846 struct xfs_mount *mp,
848 struct xfs_inumbers_req __user *arg)
850 struct xfs_bulk_ireq hdr;
851 struct xfs_ibulk breq = {
856 if (!capable(CAP_SYS_ADMIN))
859 if (XFS_FORCED_SHUTDOWN(mp))
862 if (copy_from_user(&hdr, &arg->hdr, sizeof(hdr)))
865 error = xfs_bulk_ireq_setup(mp, &hdr, &breq, arg->inumbers);
866 if (error == -ECANCELED)
871 error = xfs_inumbers(&breq, xfs_inumbers_fmt);
876 xfs_bulk_ireq_teardown(&hdr, &breq);
877 if (copy_to_user(&arg->hdr, &hdr, sizeof(hdr)))
885 struct xfs_mount *mp,
889 struct xfs_fsop_geom fsgeo;
892 xfs_fs_geometry(&mp->m_sb, &fsgeo, struct_version);
894 if (struct_version <= 3)
895 len = sizeof(struct xfs_fsop_geom_v1);
896 else if (struct_version == 4)
897 len = sizeof(struct xfs_fsop_geom_v4);
899 xfs_fsop_geom_health(mp, &fsgeo);
903 if (copy_to_user(arg, &fsgeo, len))
910 struct xfs_mount *mp,
913 struct xfs_ag_geometry ageo;
916 if (copy_from_user(&ageo, arg, sizeof(ageo)))
920 if (memchr_inv(&ageo.ag_reserved, 0, sizeof(ageo.ag_reserved)))
923 error = xfs_ag_get_geometry(mp, ageo.ag_number, &ageo);
927 if (copy_to_user(arg, &ageo, sizeof(ageo)))
933 * Linux extended inode flags interface.
937 xfs_merge_ioc_xflags(
941 unsigned int xflags = start;
943 if (flags & FS_IMMUTABLE_FL)
944 xflags |= FS_XFLAG_IMMUTABLE;
946 xflags &= ~FS_XFLAG_IMMUTABLE;
947 if (flags & FS_APPEND_FL)
948 xflags |= FS_XFLAG_APPEND;
950 xflags &= ~FS_XFLAG_APPEND;
951 if (flags & FS_SYNC_FL)
952 xflags |= FS_XFLAG_SYNC;
954 xflags &= ~FS_XFLAG_SYNC;
955 if (flags & FS_NOATIME_FL)
956 xflags |= FS_XFLAG_NOATIME;
958 xflags &= ~FS_XFLAG_NOATIME;
959 if (flags & FS_NODUMP_FL)
960 xflags |= FS_XFLAG_NODUMP;
962 xflags &= ~FS_XFLAG_NODUMP;
971 unsigned int flags = 0;
973 if (di_flags & XFS_DIFLAG_IMMUTABLE)
974 flags |= FS_IMMUTABLE_FL;
975 if (di_flags & XFS_DIFLAG_APPEND)
976 flags |= FS_APPEND_FL;
977 if (di_flags & XFS_DIFLAG_SYNC)
979 if (di_flags & XFS_DIFLAG_NOATIME)
980 flags |= FS_NOATIME_FL;
981 if (di_flags & XFS_DIFLAG_NODUMP)
982 flags |= FS_NODUMP_FL;
988 struct xfs_inode *ip,
992 simple_fill_fsxattr(fa, xfs_ip2xflags(ip));
993 fa->fsx_extsize = ip->i_d.di_extsize << ip->i_mount->m_sb.sb_blocklog;
994 fa->fsx_cowextsize = ip->i_d.di_cowextsize <<
995 ip->i_mount->m_sb.sb_blocklog;
996 fa->fsx_projid = ip->i_d.di_projid;
1000 if (ip->i_afp->if_flags & XFS_IFEXTENTS)
1001 fa->fsx_nextents = xfs_iext_count(ip->i_afp);
1003 fa->fsx_nextents = ip->i_d.di_anextents;
1005 fa->fsx_nextents = 0;
1007 if (ip->i_df.if_flags & XFS_IFEXTENTS)
1008 fa->fsx_nextents = xfs_iext_count(&ip->i_df);
1010 fa->fsx_nextents = ip->i_d.di_nextents;
1022 xfs_ilock(ip, XFS_ILOCK_SHARED);
1023 xfs_fill_fsxattr(ip, attr, &fa);
1024 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1026 if (copy_to_user(arg, &fa, sizeof(fa)))
1033 struct xfs_inode *ip,
1034 unsigned int xflags)
1036 /* can't set PREALLOC this way, just preserve it */
1038 (ip->i_d.di_flags & XFS_DIFLAG_PREALLOC);
1040 if (xflags & FS_XFLAG_IMMUTABLE)
1041 di_flags |= XFS_DIFLAG_IMMUTABLE;
1042 if (xflags & FS_XFLAG_APPEND)
1043 di_flags |= XFS_DIFLAG_APPEND;
1044 if (xflags & FS_XFLAG_SYNC)
1045 di_flags |= XFS_DIFLAG_SYNC;
1046 if (xflags & FS_XFLAG_NOATIME)
1047 di_flags |= XFS_DIFLAG_NOATIME;
1048 if (xflags & FS_XFLAG_NODUMP)
1049 di_flags |= XFS_DIFLAG_NODUMP;
1050 if (xflags & FS_XFLAG_NODEFRAG)
1051 di_flags |= XFS_DIFLAG_NODEFRAG;
1052 if (xflags & FS_XFLAG_FILESTREAM)
1053 di_flags |= XFS_DIFLAG_FILESTREAM;
1054 if (S_ISDIR(VFS_I(ip)->i_mode)) {
1055 if (xflags & FS_XFLAG_RTINHERIT)
1056 di_flags |= XFS_DIFLAG_RTINHERIT;
1057 if (xflags & FS_XFLAG_NOSYMLINKS)
1058 di_flags |= XFS_DIFLAG_NOSYMLINKS;
1059 if (xflags & FS_XFLAG_EXTSZINHERIT)
1060 di_flags |= XFS_DIFLAG_EXTSZINHERIT;
1061 if (xflags & FS_XFLAG_PROJINHERIT)
1062 di_flags |= XFS_DIFLAG_PROJINHERIT;
1063 } else if (S_ISREG(VFS_I(ip)->i_mode)) {
1064 if (xflags & FS_XFLAG_REALTIME)
1065 di_flags |= XFS_DIFLAG_REALTIME;
1066 if (xflags & FS_XFLAG_EXTSIZE)
1067 di_flags |= XFS_DIFLAG_EXTSIZE;
1075 struct xfs_inode *ip,
1076 unsigned int xflags)
1078 uint64_t di_flags2 =
1079 (ip->i_d.di_flags2 & XFS_DIFLAG2_REFLINK);
1081 if (xflags & FS_XFLAG_DAX)
1082 di_flags2 |= XFS_DIFLAG2_DAX;
1083 if (xflags & FS_XFLAG_COWEXTSIZE)
1084 di_flags2 |= XFS_DIFLAG2_COWEXTSIZE;
1090 xfs_diflags_to_linux(
1091 struct xfs_inode *ip)
1093 struct inode *inode = VFS_I(ip);
1094 unsigned int xflags = xfs_ip2xflags(ip);
1096 if (xflags & FS_XFLAG_IMMUTABLE)
1097 inode->i_flags |= S_IMMUTABLE;
1099 inode->i_flags &= ~S_IMMUTABLE;
1100 if (xflags & FS_XFLAG_APPEND)
1101 inode->i_flags |= S_APPEND;
1103 inode->i_flags &= ~S_APPEND;
1104 if (xflags & FS_XFLAG_SYNC)
1105 inode->i_flags |= S_SYNC;
1107 inode->i_flags &= ~S_SYNC;
1108 if (xflags & FS_XFLAG_NOATIME)
1109 inode->i_flags |= S_NOATIME;
1111 inode->i_flags &= ~S_NOATIME;
1112 #if 0 /* disabled until the flag switching races are sorted out */
1113 if (xflags & FS_XFLAG_DAX)
1114 inode->i_flags |= S_DAX;
1116 inode->i_flags &= ~S_DAX;
1121 xfs_ioctl_setattr_xflags(
1122 struct xfs_trans *tp,
1123 struct xfs_inode *ip,
1126 struct xfs_mount *mp = ip->i_mount;
1129 /* Can't change realtime flag if any extents are allocated. */
1130 if ((ip->i_d.di_nextents || ip->i_delayed_blks) &&
1131 XFS_IS_REALTIME_INODE(ip) != (fa->fsx_xflags & FS_XFLAG_REALTIME))
1134 /* If realtime flag is set then must have realtime device */
1135 if (fa->fsx_xflags & FS_XFLAG_REALTIME) {
1136 if (mp->m_sb.sb_rblocks == 0 || mp->m_sb.sb_rextsize == 0 ||
1137 (ip->i_d.di_extsize % mp->m_sb.sb_rextsize))
1141 /* Clear reflink if we are actually able to set the rt flag. */
1142 if ((fa->fsx_xflags & FS_XFLAG_REALTIME) && xfs_is_reflink_inode(ip))
1143 ip->i_d.di_flags2 &= ~XFS_DIFLAG2_REFLINK;
1145 /* Don't allow us to set DAX mode for a reflinked file for now. */
1146 if ((fa->fsx_xflags & FS_XFLAG_DAX) && xfs_is_reflink_inode(ip))
1149 /* diflags2 only valid for v3 inodes. */
1150 di_flags2 = xfs_flags2diflags2(ip, fa->fsx_xflags);
1151 if (di_flags2 && ip->i_d.di_version < 3)
1154 ip->i_d.di_flags = xfs_flags2diflags(ip, fa->fsx_xflags);
1155 ip->i_d.di_flags2 = di_flags2;
1157 xfs_diflags_to_linux(ip);
1158 xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
1159 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1160 XFS_STATS_INC(mp, xs_ig_attrchg);
1165 * If we are changing DAX flags, we have to ensure the file is clean and any
1166 * cached objects in the address space are invalidated and removed. This
1167 * requires us to lock out other IO and page faults similar to a truncate
1168 * operation. The locks need to be held until the transaction has been committed
1169 * so that the cache invalidation is atomic with respect to the DAX flag
1173 xfs_ioctl_setattr_dax_invalidate(
1174 struct xfs_inode *ip,
1178 struct inode *inode = VFS_I(ip);
1179 struct super_block *sb = inode->i_sb;
1185 * It is only valid to set the DAX flag on regular files and
1186 * directories on filesystems where the block size is equal to the page
1187 * size. On directories it serves as an inherited hint so we don't
1188 * have to check the device for dax support or flush pagecache.
1190 if (fa->fsx_xflags & FS_XFLAG_DAX) {
1191 struct xfs_buftarg *target = xfs_inode_buftarg(ip);
1193 if (!bdev_dax_supported(target->bt_bdev, sb->s_blocksize))
1197 /* If the DAX state is not changing, we have nothing to do here. */
1198 if ((fa->fsx_xflags & FS_XFLAG_DAX) && IS_DAX(inode))
1200 if (!(fa->fsx_xflags & FS_XFLAG_DAX) && !IS_DAX(inode))
1203 if (S_ISDIR(inode->i_mode))
1206 /* lock, flush and invalidate mapping in preparation for flag change */
1207 xfs_ilock(ip, XFS_MMAPLOCK_EXCL | XFS_IOLOCK_EXCL);
1208 error = filemap_write_and_wait(inode->i_mapping);
1211 error = invalidate_inode_pages2(inode->i_mapping);
1215 *join_flags = XFS_MMAPLOCK_EXCL | XFS_IOLOCK_EXCL;
1219 xfs_iunlock(ip, XFS_MMAPLOCK_EXCL | XFS_IOLOCK_EXCL);
1225 * Set up the transaction structure for the setattr operation, checking that we
1226 * have permission to do so. On success, return a clean transaction and the
1227 * inode locked exclusively ready for further operation specific checks. On
1228 * failure, return an error without modifying or locking the inode.
1230 * The inode might already be IO locked on call. If this is the case, it is
1231 * indicated in @join_flags and we take full responsibility for ensuring they
1232 * are unlocked from now on. Hence if we have an error here, we still have to
1233 * unlock them. Otherwise, once they are joined to the transaction, they will
1234 * be unlocked on commit/cancel.
1236 static struct xfs_trans *
1237 xfs_ioctl_setattr_get_trans(
1238 struct xfs_inode *ip,
1241 struct xfs_mount *mp = ip->i_mount;
1242 struct xfs_trans *tp;
1245 if (mp->m_flags & XFS_MOUNT_RDONLY)
1248 if (XFS_FORCED_SHUTDOWN(mp))
1251 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
1255 xfs_ilock(ip, XFS_ILOCK_EXCL);
1256 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | join_flags);
1260 * CAP_FOWNER overrides the following restrictions:
1262 * The user ID of the calling process must be equal to the file owner
1263 * ID, except in cases where the CAP_FSETID capability is applicable.
1265 if (!inode_owner_or_capable(VFS_I(ip))) {
1270 if (mp->m_flags & XFS_MOUNT_WSYNC)
1271 xfs_trans_set_sync(tp);
1276 xfs_trans_cancel(tp);
1279 xfs_iunlock(ip, join_flags);
1280 return ERR_PTR(error);
1284 * extent size hint validation is somewhat cumbersome. Rules are:
1286 * 1. extent size hint is only valid for directories and regular files
1287 * 2. FS_XFLAG_EXTSIZE is only valid for regular files
1288 * 3. FS_XFLAG_EXTSZINHERIT is only valid for directories.
1289 * 4. can only be changed on regular files if no extents are allocated
1290 * 5. can be changed on directories at any time
1291 * 6. extsize hint of 0 turns off hints, clears inode flags.
1292 * 7. Extent size must be a multiple of the appropriate block size.
1293 * 8. for non-realtime files, the extent size hint must be limited
1294 * to half the AG size to avoid alignment extending the extent beyond the
1297 * Please keep this function in sync with xfs_scrub_inode_extsize.
1300 xfs_ioctl_setattr_check_extsize(
1301 struct xfs_inode *ip,
1304 struct xfs_mount *mp = ip->i_mount;
1306 xfs_fsblock_t extsize_fsb;
1308 if (S_ISREG(VFS_I(ip)->i_mode) && ip->i_d.di_nextents &&
1309 ((ip->i_d.di_extsize << mp->m_sb.sb_blocklog) != fa->fsx_extsize))
1312 if (fa->fsx_extsize == 0)
1315 extsize_fsb = XFS_B_TO_FSB(mp, fa->fsx_extsize);
1316 if (extsize_fsb > MAXEXTLEN)
1319 if (XFS_IS_REALTIME_INODE(ip) ||
1320 (fa->fsx_xflags & FS_XFLAG_REALTIME)) {
1321 size = mp->m_sb.sb_rextsize << mp->m_sb.sb_blocklog;
1323 size = mp->m_sb.sb_blocksize;
1324 if (extsize_fsb > mp->m_sb.sb_agblocks / 2)
1328 if (fa->fsx_extsize % size)
1335 * CoW extent size hint validation rules are:
1337 * 1. CoW extent size hint can only be set if reflink is enabled on the fs.
1338 * The inode does not have to have any shared blocks, but it must be a v3.
1339 * 2. FS_XFLAG_COWEXTSIZE is only valid for directories and regular files;
1340 * for a directory, the hint is propagated to new files.
1341 * 3. Can be changed on files & directories at any time.
1342 * 4. CoW extsize hint of 0 turns off hints, clears inode flags.
1343 * 5. Extent size must be a multiple of the appropriate block size.
1344 * 6. The extent size hint must be limited to half the AG size to avoid
1345 * alignment extending the extent beyond the limits of the AG.
1347 * Please keep this function in sync with xfs_scrub_inode_cowextsize.
1350 xfs_ioctl_setattr_check_cowextsize(
1351 struct xfs_inode *ip,
1354 struct xfs_mount *mp = ip->i_mount;
1356 xfs_fsblock_t cowextsize_fsb;
1358 if (!(fa->fsx_xflags & FS_XFLAG_COWEXTSIZE))
1361 if (!xfs_sb_version_hasreflink(&ip->i_mount->m_sb) ||
1362 ip->i_d.di_version != 3)
1365 if (fa->fsx_cowextsize == 0)
1368 cowextsize_fsb = XFS_B_TO_FSB(mp, fa->fsx_cowextsize);
1369 if (cowextsize_fsb > MAXEXTLEN)
1372 size = mp->m_sb.sb_blocksize;
1373 if (cowextsize_fsb > mp->m_sb.sb_agblocks / 2)
1376 if (fa->fsx_cowextsize % size)
1383 xfs_ioctl_setattr_check_projid(
1384 struct xfs_inode *ip,
1387 /* Disallow 32bit project ids if projid32bit feature is not enabled. */
1388 if (fa->fsx_projid > (uint16_t)-1 &&
1389 !xfs_sb_version_hasprojid32bit(&ip->i_mount->m_sb))
1399 struct fsxattr old_fa;
1400 struct xfs_mount *mp = ip->i_mount;
1401 struct xfs_trans *tp;
1402 struct xfs_dquot *udqp = NULL;
1403 struct xfs_dquot *pdqp = NULL;
1404 struct xfs_dquot *olddquot = NULL;
1408 trace_xfs_ioctl_setattr(ip);
1410 code = xfs_ioctl_setattr_check_projid(ip, fa);
1415 * If disk quotas is on, we make sure that the dquots do exist on disk,
1416 * before we start any other transactions. Trying to do this later
1417 * is messy. We don't care to take a readlock to look at the ids
1418 * in inode here, because we can't hold it across the trans_reserve.
1419 * If the IDs do change before we take the ilock, we're covered
1420 * because the i_*dquot fields will get updated anyway.
1422 if (XFS_IS_QUOTA_ON(mp)) {
1423 code = xfs_qm_vop_dqalloc(ip, ip->i_d.di_uid,
1424 ip->i_d.di_gid, fa->fsx_projid,
1425 XFS_QMOPT_PQUOTA, &udqp, NULL, &pdqp);
1431 * Changing DAX config may require inode locking for mapping
1432 * invalidation. These need to be held all the way to transaction commit
1433 * or cancel time, so need to be passed through to
1434 * xfs_ioctl_setattr_get_trans() so it can apply them to the join call
1437 code = xfs_ioctl_setattr_dax_invalidate(ip, fa, &join_flags);
1439 goto error_free_dquots;
1441 tp = xfs_ioctl_setattr_get_trans(ip, join_flags);
1444 goto error_free_dquots;
1447 if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_PQUOTA_ON(mp) &&
1448 ip->i_d.di_projid != fa->fsx_projid) {
1449 code = xfs_qm_vop_chown_reserve(tp, ip, udqp, NULL, pdqp,
1450 capable(CAP_FOWNER) ? XFS_QMOPT_FORCE_RES : 0);
1451 if (code) /* out of quota */
1452 goto error_trans_cancel;
1455 xfs_fill_fsxattr(ip, false, &old_fa);
1456 code = vfs_ioc_fssetxattr_check(VFS_I(ip), &old_fa, fa);
1458 goto error_trans_cancel;
1460 code = xfs_ioctl_setattr_check_extsize(ip, fa);
1462 goto error_trans_cancel;
1464 code = xfs_ioctl_setattr_check_cowextsize(ip, fa);
1466 goto error_trans_cancel;
1468 code = xfs_ioctl_setattr_xflags(tp, ip, fa);
1470 goto error_trans_cancel;
1473 * Change file ownership. Must be the owner or privileged. CAP_FSETID
1474 * overrides the following restrictions:
1476 * The set-user-ID and set-group-ID bits of a file will be cleared upon
1477 * successful return from chown()
1480 if ((VFS_I(ip)->i_mode & (S_ISUID|S_ISGID)) &&
1481 !capable_wrt_inode_uidgid(VFS_I(ip), CAP_FSETID))
1482 VFS_I(ip)->i_mode &= ~(S_ISUID|S_ISGID);
1484 /* Change the ownerships and register project quota modifications */
1485 if (ip->i_d.di_projid != fa->fsx_projid) {
1486 if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_PQUOTA_ON(mp)) {
1487 olddquot = xfs_qm_vop_chown(tp, ip,
1488 &ip->i_pdquot, pdqp);
1490 ASSERT(ip->i_d.di_version > 1);
1491 ip->i_d.di_projid = fa->fsx_projid;
1495 * Only set the extent size hint if we've already determined that the
1496 * extent size hint should be set on the inode. If no extent size flags
1497 * are set on the inode then unconditionally clear the extent size hint.
1499 if (ip->i_d.di_flags & (XFS_DIFLAG_EXTSIZE | XFS_DIFLAG_EXTSZINHERIT))
1500 ip->i_d.di_extsize = fa->fsx_extsize >> mp->m_sb.sb_blocklog;
1502 ip->i_d.di_extsize = 0;
1503 if (ip->i_d.di_version == 3 &&
1504 (ip->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE))
1505 ip->i_d.di_cowextsize = fa->fsx_cowextsize >>
1506 mp->m_sb.sb_blocklog;
1508 ip->i_d.di_cowextsize = 0;
1510 code = xfs_trans_commit(tp);
1513 * Release any dquot(s) the inode had kept before chown.
1515 xfs_qm_dqrele(olddquot);
1516 xfs_qm_dqrele(udqp);
1517 xfs_qm_dqrele(pdqp);
1522 xfs_trans_cancel(tp);
1524 xfs_qm_dqrele(udqp);
1525 xfs_qm_dqrele(pdqp);
1538 if (copy_from_user(&fa, arg, sizeof(fa)))
1541 error = mnt_want_write_file(filp);
1544 error = xfs_ioctl_setattr(ip, &fa);
1545 mnt_drop_write_file(filp);
1556 flags = xfs_di2lxflags(ip->i_d.di_flags);
1557 if (copy_to_user(arg, &flags, sizeof(flags)))
1564 struct xfs_inode *ip,
1568 struct xfs_trans *tp;
1570 struct fsxattr old_fa;
1575 if (copy_from_user(&flags, arg, sizeof(flags)))
1578 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
1579 FS_NOATIME_FL | FS_NODUMP_FL | \
1583 fa.fsx_xflags = xfs_merge_ioc_xflags(flags, xfs_ip2xflags(ip));
1585 error = mnt_want_write_file(filp);
1590 * Changing DAX config may require inode locking for mapping
1591 * invalidation. These need to be held all the way to transaction commit
1592 * or cancel time, so need to be passed through to
1593 * xfs_ioctl_setattr_get_trans() so it can apply them to the join call
1596 error = xfs_ioctl_setattr_dax_invalidate(ip, &fa, &join_flags);
1598 goto out_drop_write;
1600 tp = xfs_ioctl_setattr_get_trans(ip, join_flags);
1602 error = PTR_ERR(tp);
1603 goto out_drop_write;
1606 xfs_fill_fsxattr(ip, false, &old_fa);
1607 error = vfs_ioc_fssetxattr_check(VFS_I(ip), &old_fa, &fa);
1609 xfs_trans_cancel(tp);
1610 goto out_drop_write;
1613 error = xfs_ioctl_setattr_xflags(tp, ip, &fa);
1615 xfs_trans_cancel(tp);
1616 goto out_drop_write;
1619 error = xfs_trans_commit(tp);
1621 mnt_drop_write_file(filp);
1628 struct getbmapx __user *u,
1631 if (put_user(p->bmv_offset, &u->bmv_offset) ||
1632 put_user(p->bmv_block, &u->bmv_block) ||
1633 put_user(p->bmv_length, &u->bmv_length) ||
1634 put_user(0, &u->bmv_count) ||
1635 put_user(0, &u->bmv_entries))
1637 if (recsize < sizeof(struct getbmapx))
1639 if (put_user(0, &u->bmv_iflags) ||
1640 put_user(p->bmv_oflags, &u->bmv_oflags) ||
1641 put_user(0, &u->bmv_unused1) ||
1642 put_user(0, &u->bmv_unused2))
1653 struct getbmapx bmx = { 0 };
1654 struct kgetbmap *buf;
1659 case XFS_IOC_GETBMAPA:
1660 bmx.bmv_iflags = BMV_IF_ATTRFORK;
1662 case XFS_IOC_GETBMAP:
1663 if (file->f_mode & FMODE_NOCMTIME)
1664 bmx.bmv_iflags |= BMV_IF_NO_DMAPI_READ;
1665 /* struct getbmap is a strict subset of struct getbmapx. */
1666 recsize = sizeof(struct getbmap);
1668 case XFS_IOC_GETBMAPX:
1669 recsize = sizeof(struct getbmapx);
1675 if (copy_from_user(&bmx, arg, recsize))
1678 if (bmx.bmv_count < 2)
1680 if (bmx.bmv_count > ULONG_MAX / recsize)
1683 buf = kmem_zalloc_large(bmx.bmv_count * sizeof(*buf), 0);
1687 error = xfs_getbmap(XFS_I(file_inode(file)), &bmx, buf);
1692 if (copy_to_user(arg, &bmx, recsize))
1696 for (i = 0; i < bmx.bmv_entries; i++) {
1697 if (!xfs_getbmap_format(buf + i, arg, recsize))
1708 struct getfsmap_info {
1709 struct xfs_mount *mp;
1710 struct fsmap_head __user *data;
1716 xfs_getfsmap_format(struct xfs_fsmap *xfm, void *priv)
1718 struct getfsmap_info *info = priv;
1721 trace_xfs_getfsmap_mapping(info->mp, xfm);
1723 info->last_flags = xfm->fmr_flags;
1724 xfs_fsmap_from_internal(&fm, xfm);
1725 if (copy_to_user(&info->data->fmh_recs[info->idx++], &fm,
1726 sizeof(struct fsmap)))
1734 struct xfs_inode *ip,
1735 struct fsmap_head __user *arg)
1737 struct getfsmap_info info = { NULL };
1738 struct xfs_fsmap_head xhead = {0};
1739 struct fsmap_head head;
1740 bool aborted = false;
1743 if (copy_from_user(&head, arg, sizeof(struct fsmap_head)))
1745 if (memchr_inv(head.fmh_reserved, 0, sizeof(head.fmh_reserved)) ||
1746 memchr_inv(head.fmh_keys[0].fmr_reserved, 0,
1747 sizeof(head.fmh_keys[0].fmr_reserved)) ||
1748 memchr_inv(head.fmh_keys[1].fmr_reserved, 0,
1749 sizeof(head.fmh_keys[1].fmr_reserved)))
1752 xhead.fmh_iflags = head.fmh_iflags;
1753 xhead.fmh_count = head.fmh_count;
1754 xfs_fsmap_to_internal(&xhead.fmh_keys[0], &head.fmh_keys[0]);
1755 xfs_fsmap_to_internal(&xhead.fmh_keys[1], &head.fmh_keys[1]);
1757 trace_xfs_getfsmap_low_key(ip->i_mount, &xhead.fmh_keys[0]);
1758 trace_xfs_getfsmap_high_key(ip->i_mount, &xhead.fmh_keys[1]);
1760 info.mp = ip->i_mount;
1762 error = xfs_getfsmap(ip->i_mount, &xhead, xfs_getfsmap_format, &info);
1763 if (error == -ECANCELED) {
1769 /* If we didn't abort, set the "last" flag in the last fmx */
1770 if (!aborted && info.idx) {
1771 info.last_flags |= FMR_OF_LAST;
1772 if (copy_to_user(&info.data->fmh_recs[info.idx - 1].fmr_flags,
1773 &info.last_flags, sizeof(info.last_flags)))
1777 /* copy back header */
1778 head.fmh_entries = xhead.fmh_entries;
1779 head.fmh_oflags = xhead.fmh_oflags;
1780 if (copy_to_user(arg, &head, sizeof(struct fsmap_head)))
1787 xfs_ioc_scrub_metadata(
1788 struct xfs_inode *ip,
1791 struct xfs_scrub_metadata scrub;
1794 if (!capable(CAP_SYS_ADMIN))
1797 if (copy_from_user(&scrub, arg, sizeof(scrub)))
1800 error = xfs_scrub_metadata(ip, &scrub);
1804 if (copy_to_user(arg, &scrub, sizeof(scrub)))
1814 xfs_inode_t *ip, *tip;
1818 /* Pull information for the target fd */
1819 f = fdget((int)sxp->sx_fdtarget);
1825 if (!(f.file->f_mode & FMODE_WRITE) ||
1826 !(f.file->f_mode & FMODE_READ) ||
1827 (f.file->f_flags & O_APPEND)) {
1832 tmp = fdget((int)sxp->sx_fdtmp);
1838 if (!(tmp.file->f_mode & FMODE_WRITE) ||
1839 !(tmp.file->f_mode & FMODE_READ) ||
1840 (tmp.file->f_flags & O_APPEND)) {
1842 goto out_put_tmp_file;
1845 if (IS_SWAPFILE(file_inode(f.file)) ||
1846 IS_SWAPFILE(file_inode(tmp.file))) {
1848 goto out_put_tmp_file;
1852 * We need to ensure that the fds passed in point to XFS inodes
1853 * before we cast and access them as XFS structures as we have no
1854 * control over what the user passes us here.
1856 if (f.file->f_op != &xfs_file_operations ||
1857 tmp.file->f_op != &xfs_file_operations) {
1859 goto out_put_tmp_file;
1862 ip = XFS_I(file_inode(f.file));
1863 tip = XFS_I(file_inode(tmp.file));
1865 if (ip->i_mount != tip->i_mount) {
1867 goto out_put_tmp_file;
1870 if (ip->i_ino == tip->i_ino) {
1872 goto out_put_tmp_file;
1875 if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
1877 goto out_put_tmp_file;
1880 error = xfs_swap_extents(ip, tip, sxp);
1892 struct xfs_mount *mp,
1893 char __user *user_label)
1895 struct xfs_sb *sbp = &mp->m_sb;
1896 char label[XFSLABEL_MAX + 1];
1899 BUILD_BUG_ON(sizeof(sbp->sb_fname) > FSLABEL_MAX);
1901 /* 1 larger than sb_fname, so this ensures a trailing NUL char */
1902 memset(label, 0, sizeof(label));
1903 spin_lock(&mp->m_sb_lock);
1904 strncpy(label, sbp->sb_fname, XFSLABEL_MAX);
1905 spin_unlock(&mp->m_sb_lock);
1907 if (copy_to_user(user_label, label, sizeof(label)))
1915 struct xfs_mount *mp,
1916 char __user *newlabel)
1918 struct xfs_sb *sbp = &mp->m_sb;
1919 char label[XFSLABEL_MAX + 1];
1923 if (!capable(CAP_SYS_ADMIN))
1926 * The generic ioctl allows up to FSLABEL_MAX chars, but XFS is much
1927 * smaller, at 12 bytes. We copy one more to be sure we find the
1928 * (required) NULL character to test the incoming label length.
1929 * NB: The on disk label doesn't need to be null terminated.
1931 if (copy_from_user(label, newlabel, XFSLABEL_MAX + 1))
1933 len = strnlen(label, XFSLABEL_MAX + 1);
1934 if (len > sizeof(sbp->sb_fname))
1937 error = mnt_want_write_file(filp);
1941 spin_lock(&mp->m_sb_lock);
1942 memset(sbp->sb_fname, 0, sizeof(sbp->sb_fname));
1943 memcpy(sbp->sb_fname, label, len);
1944 spin_unlock(&mp->m_sb_lock);
1947 * Now we do several things to satisfy userspace.
1948 * In addition to normal logging of the primary superblock, we also
1949 * immediately write these changes to sector zero for the primary, then
1950 * update all backup supers (as xfs_db does for a label change), then
1951 * invalidate the block device page cache. This is so that any prior
1952 * buffered reads from userspace (i.e. from blkid) are invalidated,
1953 * and userspace will see the newly-written label.
1955 error = xfs_sync_sb_buf(mp);
1959 * growfs also updates backup supers so lock against that.
1961 mutex_lock(&mp->m_growlock);
1962 error = xfs_update_secondary_sbs(mp);
1963 mutex_unlock(&mp->m_growlock);
1965 invalidate_bdev(mp->m_ddev_targp->bt_bdev);
1968 mnt_drop_write_file(filp);
1973 * Note: some of the ioctl's return positive numbers as a
1974 * byte count indicating success, such as readlink_by_handle.
1975 * So we don't "sign flip" like most other routines. This means
1976 * true errors need to be returned as a negative value.
1984 struct inode *inode = file_inode(filp);
1985 struct xfs_inode *ip = XFS_I(inode);
1986 struct xfs_mount *mp = ip->i_mount;
1987 void __user *arg = (void __user *)p;
1990 trace_xfs_file_ioctl(ip);
1994 return xfs_ioc_trim(mp, arg);
1995 case FS_IOC_GETFSLABEL:
1996 return xfs_ioc_getlabel(mp, arg);
1997 case FS_IOC_SETFSLABEL:
1998 return xfs_ioc_setlabel(filp, mp, arg);
1999 case XFS_IOC_ALLOCSP:
2000 case XFS_IOC_FREESP:
2001 case XFS_IOC_ALLOCSP64:
2002 case XFS_IOC_FREESP64: {
2005 if (copy_from_user(&bf, arg, sizeof(bf)))
2007 return xfs_ioc_space(filp, &bf);
2009 case XFS_IOC_DIOINFO: {
2010 struct xfs_buftarg *target = xfs_inode_buftarg(ip);
2013 da.d_mem = da.d_miniosz = target->bt_logical_sectorsize;
2014 da.d_maxiosz = INT_MAX & ~(da.d_miniosz - 1);
2016 if (copy_to_user(arg, &da, sizeof(da)))
2021 case XFS_IOC_FSBULKSTAT_SINGLE:
2022 case XFS_IOC_FSBULKSTAT:
2023 case XFS_IOC_FSINUMBERS:
2024 return xfs_ioc_fsbulkstat(mp, cmd, arg);
2026 case XFS_IOC_BULKSTAT:
2027 return xfs_ioc_bulkstat(mp, cmd, arg);
2028 case XFS_IOC_INUMBERS:
2029 return xfs_ioc_inumbers(mp, cmd, arg);
2031 case XFS_IOC_FSGEOMETRY_V1:
2032 return xfs_ioc_fsgeometry(mp, arg, 3);
2033 case XFS_IOC_FSGEOMETRY_V4:
2034 return xfs_ioc_fsgeometry(mp, arg, 4);
2035 case XFS_IOC_FSGEOMETRY:
2036 return xfs_ioc_fsgeometry(mp, arg, 5);
2038 case XFS_IOC_AG_GEOMETRY:
2039 return xfs_ioc_ag_geometry(mp, arg);
2041 case XFS_IOC_GETVERSION:
2042 return put_user(inode->i_generation, (int __user *)arg);
2044 case XFS_IOC_FSGETXATTR:
2045 return xfs_ioc_fsgetxattr(ip, 0, arg);
2046 case XFS_IOC_FSGETXATTRA:
2047 return xfs_ioc_fsgetxattr(ip, 1, arg);
2048 case XFS_IOC_FSSETXATTR:
2049 return xfs_ioc_fssetxattr(ip, filp, arg);
2050 case XFS_IOC_GETXFLAGS:
2051 return xfs_ioc_getxflags(ip, arg);
2052 case XFS_IOC_SETXFLAGS:
2053 return xfs_ioc_setxflags(ip, filp, arg);
2055 case XFS_IOC_GETBMAP:
2056 case XFS_IOC_GETBMAPA:
2057 case XFS_IOC_GETBMAPX:
2058 return xfs_ioc_getbmap(filp, cmd, arg);
2060 case FS_IOC_GETFSMAP:
2061 return xfs_ioc_getfsmap(ip, arg);
2063 case XFS_IOC_SCRUB_METADATA:
2064 return xfs_ioc_scrub_metadata(ip, arg);
2066 case XFS_IOC_FD_TO_HANDLE:
2067 case XFS_IOC_PATH_TO_HANDLE:
2068 case XFS_IOC_PATH_TO_FSHANDLE: {
2069 xfs_fsop_handlereq_t hreq;
2071 if (copy_from_user(&hreq, arg, sizeof(hreq)))
2073 return xfs_find_handle(cmd, &hreq);
2075 case XFS_IOC_OPEN_BY_HANDLE: {
2076 xfs_fsop_handlereq_t hreq;
2078 if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
2080 return xfs_open_by_handle(filp, &hreq);
2083 case XFS_IOC_READLINK_BY_HANDLE: {
2084 xfs_fsop_handlereq_t hreq;
2086 if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
2088 return xfs_readlink_by_handle(filp, &hreq);
2090 case XFS_IOC_ATTRLIST_BY_HANDLE:
2091 return xfs_attrlist_by_handle(filp, arg);
2093 case XFS_IOC_ATTRMULTI_BY_HANDLE:
2094 return xfs_attrmulti_by_handle(filp, arg);
2096 case XFS_IOC_SWAPEXT: {
2097 struct xfs_swapext sxp;
2099 if (copy_from_user(&sxp, arg, sizeof(xfs_swapext_t)))
2101 error = mnt_want_write_file(filp);
2104 error = xfs_ioc_swapext(&sxp);
2105 mnt_drop_write_file(filp);
2109 case XFS_IOC_FSCOUNTS: {
2110 xfs_fsop_counts_t out;
2112 xfs_fs_counts(mp, &out);
2114 if (copy_to_user(arg, &out, sizeof(out)))
2119 case XFS_IOC_SET_RESBLKS: {
2120 xfs_fsop_resblks_t inout;
2123 if (!capable(CAP_SYS_ADMIN))
2126 if (mp->m_flags & XFS_MOUNT_RDONLY)
2129 if (copy_from_user(&inout, arg, sizeof(inout)))
2132 error = mnt_want_write_file(filp);
2136 /* input parameter is passed in resblks field of structure */
2138 error = xfs_reserve_blocks(mp, &in, &inout);
2139 mnt_drop_write_file(filp);
2143 if (copy_to_user(arg, &inout, sizeof(inout)))
2148 case XFS_IOC_GET_RESBLKS: {
2149 xfs_fsop_resblks_t out;
2151 if (!capable(CAP_SYS_ADMIN))
2154 error = xfs_reserve_blocks(mp, NULL, &out);
2158 if (copy_to_user(arg, &out, sizeof(out)))
2164 case XFS_IOC_FSGROWFSDATA: {
2165 xfs_growfs_data_t in;
2167 if (copy_from_user(&in, arg, sizeof(in)))
2170 error = mnt_want_write_file(filp);
2173 error = xfs_growfs_data(mp, &in);
2174 mnt_drop_write_file(filp);
2178 case XFS_IOC_FSGROWFSLOG: {
2179 xfs_growfs_log_t in;
2181 if (copy_from_user(&in, arg, sizeof(in)))
2184 error = mnt_want_write_file(filp);
2187 error = xfs_growfs_log(mp, &in);
2188 mnt_drop_write_file(filp);
2192 case XFS_IOC_FSGROWFSRT: {
2195 if (copy_from_user(&in, arg, sizeof(in)))
2198 error = mnt_want_write_file(filp);
2201 error = xfs_growfs_rt(mp, &in);
2202 mnt_drop_write_file(filp);
2206 case XFS_IOC_GOINGDOWN: {
2209 if (!capable(CAP_SYS_ADMIN))
2212 if (get_user(in, (uint32_t __user *)arg))
2215 return xfs_fs_goingdown(mp, in);
2218 case XFS_IOC_ERROR_INJECTION: {
2219 xfs_error_injection_t in;
2221 if (!capable(CAP_SYS_ADMIN))
2224 if (copy_from_user(&in, arg, sizeof(in)))
2227 return xfs_errortag_add(mp, in.errtag);
2230 case XFS_IOC_ERROR_CLEARALL:
2231 if (!capable(CAP_SYS_ADMIN))
2234 return xfs_errortag_clearall(mp);
2236 case XFS_IOC_FREE_EOFBLOCKS: {
2237 struct xfs_fs_eofblocks eofb;
2238 struct xfs_eofblocks keofb;
2240 if (!capable(CAP_SYS_ADMIN))
2243 if (mp->m_flags & XFS_MOUNT_RDONLY)
2246 if (copy_from_user(&eofb, arg, sizeof(eofb)))
2249 error = xfs_fs_eofblocks_from_user(&eofb, &keofb);
2253 return xfs_icache_free_eofblocks(mp, &keofb);