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"
38 #include <linux/mount.h>
39 #include <linux/namei.h>
42 * xfs_find_handle maps from userspace xfs_fsop_handlereq structure to
43 * a file or fs handle.
45 * XFS_IOC_PATH_TO_FSHANDLE
46 * returns fs handle for a mount point or path within that mount point
47 * XFS_IOC_FD_TO_HANDLE
48 * returns full handle for a FD opened in user space
49 * XFS_IOC_PATH_TO_HANDLE
50 * returns full handle for a path
55 xfs_fsop_handlereq_t *hreq)
65 if (cmd == XFS_IOC_FD_TO_HANDLE) {
69 inode = file_inode(f.file);
71 error = user_path_at(AT_FDCWD, hreq->path, 0, &path);
74 inode = d_inode(path.dentry);
79 * We can only generate handles for inodes residing on a XFS filesystem,
80 * and only for regular files, directories or symbolic links.
83 if (inode->i_sb->s_magic != XFS_SB_MAGIC)
87 if (!S_ISREG(inode->i_mode) &&
88 !S_ISDIR(inode->i_mode) &&
89 !S_ISLNK(inode->i_mode))
93 memcpy(&handle.ha_fsid, ip->i_mount->m_fixedfsid, sizeof(xfs_fsid_t));
95 if (cmd == XFS_IOC_PATH_TO_FSHANDLE) {
97 * This handle only contains an fsid, zero the rest.
99 memset(&handle.ha_fid, 0, sizeof(handle.ha_fid));
100 hsize = sizeof(xfs_fsid_t);
102 handle.ha_fid.fid_len = sizeof(xfs_fid_t) -
103 sizeof(handle.ha_fid.fid_len);
104 handle.ha_fid.fid_pad = 0;
105 handle.ha_fid.fid_gen = inode->i_generation;
106 handle.ha_fid.fid_ino = ip->i_ino;
107 hsize = sizeof(xfs_handle_t);
111 if (copy_to_user(hreq->ohandle, &handle, hsize) ||
112 copy_to_user(hreq->ohandlen, &hsize, sizeof(__s32)))
118 if (cmd == XFS_IOC_FD_TO_HANDLE)
126 * No need to do permission checks on the various pathname components
127 * as the handle operations are privileged.
130 xfs_handle_acceptable(
132 struct dentry *dentry)
138 * Convert userspace handle data into a dentry.
141 xfs_handle_to_dentry(
142 struct file *parfilp,
143 void __user *uhandle,
147 struct xfs_fid64 fid;
150 * Only allow handle opens under a directory.
152 if (!S_ISDIR(file_inode(parfilp)->i_mode))
153 return ERR_PTR(-ENOTDIR);
155 if (hlen != sizeof(xfs_handle_t))
156 return ERR_PTR(-EINVAL);
157 if (copy_from_user(&handle, uhandle, hlen))
158 return ERR_PTR(-EFAULT);
159 if (handle.ha_fid.fid_len !=
160 sizeof(handle.ha_fid) - sizeof(handle.ha_fid.fid_len))
161 return ERR_PTR(-EINVAL);
163 memset(&fid, 0, sizeof(struct fid));
164 fid.ino = handle.ha_fid.fid_ino;
165 fid.gen = handle.ha_fid.fid_gen;
167 return exportfs_decode_fh(parfilp->f_path.mnt, (struct fid *)&fid, 3,
168 FILEID_INO32_GEN | XFS_FILEID_TYPE_64FLAG,
169 xfs_handle_acceptable, NULL);
172 STATIC struct dentry *
173 xfs_handlereq_to_dentry(
174 struct file *parfilp,
175 xfs_fsop_handlereq_t *hreq)
177 return xfs_handle_to_dentry(parfilp, hreq->ihandle, hreq->ihandlen);
182 struct file *parfilp,
183 xfs_fsop_handlereq_t *hreq)
185 const struct cred *cred = current_cred();
191 struct dentry *dentry;
195 if (!capable(CAP_SYS_ADMIN))
198 dentry = xfs_handlereq_to_dentry(parfilp, hreq);
200 return PTR_ERR(dentry);
201 inode = d_inode(dentry);
203 /* Restrict xfs_open_by_handle to directories & regular files. */
204 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode))) {
209 #if BITS_PER_LONG != 32
210 hreq->oflags |= O_LARGEFILE;
213 permflag = hreq->oflags;
214 fmode = OPEN_FMODE(permflag);
215 if ((!(permflag & O_APPEND) || (permflag & O_TRUNC)) &&
216 (fmode & FMODE_WRITE) && IS_APPEND(inode)) {
221 if ((fmode & FMODE_WRITE) && IS_IMMUTABLE(inode)) {
226 /* Can't write directories. */
227 if (S_ISDIR(inode->i_mode) && (fmode & FMODE_WRITE)) {
232 fd = get_unused_fd_flags(0);
238 path.mnt = parfilp->f_path.mnt;
239 path.dentry = dentry;
240 filp = dentry_open(&path, hreq->oflags, cred);
244 return PTR_ERR(filp);
247 if (S_ISREG(inode->i_mode)) {
248 filp->f_flags |= O_NOATIME;
249 filp->f_mode |= FMODE_NOCMTIME;
252 fd_install(fd, filp);
261 xfs_readlink_by_handle(
262 struct file *parfilp,
263 xfs_fsop_handlereq_t *hreq)
265 struct dentry *dentry;
269 if (!capable(CAP_SYS_ADMIN))
272 dentry = xfs_handlereq_to_dentry(parfilp, hreq);
274 return PTR_ERR(dentry);
276 /* Restrict this handle operation to symlinks only. */
277 if (!d_is_symlink(dentry)) {
282 if (copy_from_user(&olen, hreq->ohandlen, sizeof(__u32))) {
287 error = vfs_readlink(dentry, hreq->ohandle, olen);
300 xfs_mount_t *mp = ip->i_mount;
304 if (!capable(CAP_SYS_ADMIN))
307 if (XFS_FORCED_SHUTDOWN(mp))
310 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
314 xfs_ilock(ip, XFS_ILOCK_EXCL);
315 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
317 ip->i_d.di_dmevmask = evmask;
318 ip->i_d.di_dmstate = state;
320 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
321 error = xfs_trans_commit(tp);
327 xfs_fssetdm_by_handle(
328 struct file *parfilp,
332 struct fsdmidata fsd;
333 xfs_fsop_setdm_handlereq_t dmhreq;
334 struct dentry *dentry;
336 if (!capable(CAP_MKNOD))
338 if (copy_from_user(&dmhreq, arg, sizeof(xfs_fsop_setdm_handlereq_t)))
341 error = mnt_want_write_file(parfilp);
345 dentry = xfs_handlereq_to_dentry(parfilp, &dmhreq.hreq);
346 if (IS_ERR(dentry)) {
347 mnt_drop_write_file(parfilp);
348 return PTR_ERR(dentry);
351 if (IS_IMMUTABLE(d_inode(dentry)) || IS_APPEND(d_inode(dentry))) {
356 if (copy_from_user(&fsd, dmhreq.data, sizeof(fsd))) {
361 error = xfs_set_dmattrs(XFS_I(d_inode(dentry)), fsd.fsd_dmevmask,
365 mnt_drop_write_file(parfilp);
371 xfs_attrlist_by_handle(
372 struct file *parfilp,
376 attrlist_cursor_kern_t *cursor;
377 struct xfs_fsop_attrlist_handlereq __user *p = arg;
378 xfs_fsop_attrlist_handlereq_t al_hreq;
379 struct dentry *dentry;
382 if (!capable(CAP_SYS_ADMIN))
384 if (copy_from_user(&al_hreq, arg, sizeof(xfs_fsop_attrlist_handlereq_t)))
386 if (al_hreq.buflen < sizeof(struct attrlist) ||
387 al_hreq.buflen > XFS_XATTR_LIST_MAX)
391 * Reject flags, only allow namespaces.
393 if (al_hreq.flags & ~(ATTR_ROOT | ATTR_SECURE))
396 dentry = xfs_handlereq_to_dentry(parfilp, &al_hreq.hreq);
398 return PTR_ERR(dentry);
400 kbuf = kmem_zalloc_large(al_hreq.buflen, 0);
404 cursor = (attrlist_cursor_kern_t *)&al_hreq.pos;
405 error = xfs_attr_list(XFS_I(d_inode(dentry)), kbuf, al_hreq.buflen,
406 al_hreq.flags, cursor);
410 if (copy_to_user(&p->pos, cursor, sizeof(attrlist_cursor_kern_t))) {
415 if (copy_to_user(al_hreq.buffer, kbuf, al_hreq.buflen))
426 xfs_attrmulti_attr_get(
429 unsigned char __user *ubuf,
436 if (*len > XFS_XATTR_SIZE_MAX)
438 kbuf = kmem_zalloc_large(*len, 0);
442 error = xfs_attr_get(XFS_I(inode), name, &kbuf, (int *)len, flags);
446 if (copy_to_user(ubuf, kbuf, *len))
455 xfs_attrmulti_attr_set(
458 const unsigned char __user *ubuf,
465 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
467 if (len > XFS_XATTR_SIZE_MAX)
470 kbuf = memdup_user(ubuf, len);
472 return PTR_ERR(kbuf);
474 error = xfs_attr_set(XFS_I(inode), name, kbuf, len, flags);
476 xfs_forget_acl(inode, name, flags);
482 xfs_attrmulti_attr_remove(
489 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
491 error = xfs_attr_remove(XFS_I(inode), name, flags);
493 xfs_forget_acl(inode, name, flags);
498 xfs_attrmulti_by_handle(
499 struct file *parfilp,
503 xfs_attr_multiop_t *ops;
504 xfs_fsop_attrmulti_handlereq_t am_hreq;
505 struct dentry *dentry;
506 unsigned int i, size;
507 unsigned char *attr_name;
509 if (!capable(CAP_SYS_ADMIN))
511 if (copy_from_user(&am_hreq, arg, sizeof(xfs_fsop_attrmulti_handlereq_t)))
515 if (am_hreq.opcount >= INT_MAX / sizeof(xfs_attr_multiop_t))
518 dentry = xfs_handlereq_to_dentry(parfilp, &am_hreq.hreq);
520 return PTR_ERR(dentry);
523 size = am_hreq.opcount * sizeof(xfs_attr_multiop_t);
524 if (!size || size > 16 * PAGE_SIZE)
527 ops = memdup_user(am_hreq.ops, size);
529 error = PTR_ERR(ops);
534 attr_name = kmalloc(MAXNAMELEN, GFP_KERNEL);
539 for (i = 0; i < am_hreq.opcount; i++) {
540 ops[i].am_error = strncpy_from_user((char *)attr_name,
541 ops[i].am_attrname, MAXNAMELEN);
542 if (ops[i].am_error == 0 || ops[i].am_error == MAXNAMELEN)
544 if (ops[i].am_error < 0)
547 switch (ops[i].am_opcode) {
549 ops[i].am_error = xfs_attrmulti_attr_get(
550 d_inode(dentry), attr_name,
551 ops[i].am_attrvalue, &ops[i].am_length,
555 ops[i].am_error = mnt_want_write_file(parfilp);
558 ops[i].am_error = xfs_attrmulti_attr_set(
559 d_inode(dentry), attr_name,
560 ops[i].am_attrvalue, ops[i].am_length,
562 mnt_drop_write_file(parfilp);
565 ops[i].am_error = mnt_want_write_file(parfilp);
568 ops[i].am_error = xfs_attrmulti_attr_remove(
569 d_inode(dentry), attr_name,
571 mnt_drop_write_file(parfilp);
574 ops[i].am_error = -EINVAL;
578 if (copy_to_user(am_hreq.ops, ops, size))
594 struct inode *inode = file_inode(filp);
595 struct xfs_inode *ip = XFS_I(inode);
597 enum xfs_prealloc_flags flags = XFS_PREALLOC_CLEAR;
598 uint iolock = XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL;
601 if (inode->i_flags & (S_IMMUTABLE|S_APPEND))
604 if (!(filp->f_mode & FMODE_WRITE))
607 if (!S_ISREG(inode->i_mode))
610 if (xfs_is_always_cow_inode(ip))
613 if (filp->f_flags & O_DSYNC)
614 flags |= XFS_PREALLOC_SYNC;
615 if (filp->f_mode & FMODE_NOCMTIME)
616 flags |= XFS_PREALLOC_INVISIBLE;
618 error = mnt_want_write_file(filp);
622 xfs_ilock(ip, iolock);
623 error = xfs_break_layouts(inode, &iolock, BREAK_UNMAP);
627 switch (bf->l_whence) {
631 bf->l_start += filp->f_pos;
634 bf->l_start += XFS_ISIZE(ip);
641 if (bf->l_start < 0 || bf->l_start > inode->i_sb->s_maxbytes) {
646 if (bf->l_start > XFS_ISIZE(ip)) {
647 error = xfs_alloc_file_space(ip, XFS_ISIZE(ip),
648 bf->l_start - XFS_ISIZE(ip), 0);
653 iattr.ia_valid = ATTR_SIZE;
654 iattr.ia_size = bf->l_start;
655 error = xfs_vn_setattr_size(file_dentry(filp), &iattr);
659 error = xfs_update_prealloc_flags(ip, flags);
662 xfs_iunlock(ip, iolock);
663 mnt_drop_write_file(filp);
667 /* Return 0 on success or positive error */
669 xfs_fsbulkstat_one_fmt(
670 struct xfs_ibulk *breq,
671 const struct xfs_bulkstat *bstat)
673 struct xfs_bstat bs1;
675 xfs_bulkstat_to_bstat(breq->mp, &bs1, bstat);
676 if (copy_to_user(breq->ubuffer, &bs1, sizeof(bs1)))
678 return xfs_ibulk_advance(breq, sizeof(struct xfs_bstat));
683 struct xfs_ibulk *breq,
684 const struct xfs_inumbers *igrp)
686 struct xfs_inogrp ig1;
688 xfs_inumbers_to_inogrp(&ig1, igrp);
689 if (copy_to_user(breq->ubuffer, &ig1, sizeof(struct xfs_inogrp)))
691 return xfs_ibulk_advance(breq, sizeof(struct xfs_inogrp));
700 struct xfs_fsop_bulkreq bulkreq;
701 struct xfs_ibulk breq = {
708 /* done = 1 if there are more stats to get and if bulkstat */
709 /* should be called again (unused here, but used in dmapi) */
711 if (!capable(CAP_SYS_ADMIN))
714 if (XFS_FORCED_SHUTDOWN(mp))
717 if (copy_from_user(&bulkreq, arg, sizeof(struct xfs_fsop_bulkreq)))
720 if (copy_from_user(&lastino, bulkreq.lastip, sizeof(__s64)))
723 if (bulkreq.icount <= 0)
726 if (bulkreq.ubuffer == NULL)
729 breq.ubuffer = bulkreq.ubuffer;
730 breq.icount = bulkreq.icount;
733 * FSBULKSTAT_SINGLE expects that *lastip contains the inode number
734 * that we want to stat. However, FSINUMBERS and FSBULKSTAT expect
735 * that *lastip contains either zero or the number of the last inode to
736 * be examined by the previous call and return results starting with
737 * the next inode after that. The new bulk request back end functions
738 * take the inode to start with, so we have to compute the startino
739 * parameter from lastino to maintain correct function. lastino == 0
740 * is a special case because it has traditionally meant "first inode
743 if (cmd == XFS_IOC_FSINUMBERS) {
744 breq.startino = lastino ? lastino + 1 : 0;
745 error = xfs_inumbers(&breq, xfs_fsinumbers_fmt);
746 lastino = breq.startino - 1;
747 } else if (cmd == XFS_IOC_FSBULKSTAT_SINGLE) {
748 breq.startino = lastino;
750 error = xfs_bulkstat_one(&breq, xfs_fsbulkstat_one_fmt);
751 } else { /* XFS_IOC_FSBULKSTAT */
752 breq.startino = lastino ? lastino + 1 : 0;
753 error = xfs_bulkstat(&breq, xfs_fsbulkstat_one_fmt);
754 lastino = breq.startino - 1;
760 if (bulkreq.lastip != NULL &&
761 copy_to_user(bulkreq.lastip, &lastino, sizeof(xfs_ino_t)))
764 if (bulkreq.ocount != NULL &&
765 copy_to_user(bulkreq.ocount, &breq.ocount, sizeof(__s32)))
771 /* Return 0 on success or positive error */
774 struct xfs_ibulk *breq,
775 const struct xfs_bulkstat *bstat)
777 if (copy_to_user(breq->ubuffer, bstat, sizeof(struct xfs_bulkstat)))
779 return xfs_ibulk_advance(breq, sizeof(struct xfs_bulkstat));
783 * Check the incoming bulk request @hdr from userspace and initialize the
784 * internal @breq bulk request appropriately. Returns 0 if the bulk request
785 * should proceed; -ECANCELED if there's nothing to do; or the usual
786 * negative error code.
790 struct xfs_mount *mp,
791 struct xfs_bulk_ireq *hdr,
792 struct xfs_ibulk *breq,
793 void __user *ubuffer)
795 if (hdr->icount == 0 ||
796 (hdr->flags & ~XFS_BULK_IREQ_FLAGS_ALL) ||
797 memchr_inv(hdr->reserved, 0, sizeof(hdr->reserved)))
800 breq->startino = hdr->ino;
801 breq->ubuffer = ubuffer;
802 breq->icount = hdr->icount;
807 * The @ino parameter is a special value, so we must look it up here.
808 * We're not allowed to have IREQ_AGNO, and we only return one inode
811 if (hdr->flags & XFS_BULK_IREQ_SPECIAL) {
812 if (hdr->flags & XFS_BULK_IREQ_AGNO)
816 case XFS_BULK_IREQ_SPECIAL_ROOT:
817 hdr->ino = mp->m_sb.sb_rootino;
826 * The IREQ_AGNO flag means that we only want results from a given AG.
827 * If @hdr->ino is zero, we start iterating in that AG. If @hdr->ino is
828 * beyond the specified AG then we return no results.
830 if (hdr->flags & XFS_BULK_IREQ_AGNO) {
831 if (hdr->agno >= mp->m_sb.sb_agcount)
834 if (breq->startino == 0)
835 breq->startino = XFS_AGINO_TO_INO(mp, hdr->agno, 0);
836 else if (XFS_INO_TO_AGNO(mp, breq->startino) < hdr->agno)
839 breq->flags |= XFS_IBULK_SAME_AG;
841 /* Asking for an inode past the end of the AG? We're done! */
842 if (XFS_INO_TO_AGNO(mp, breq->startino) > hdr->agno)
844 } else if (hdr->agno)
847 /* Asking for an inode past the end of the FS? We're done! */
848 if (XFS_INO_TO_AGNO(mp, breq->startino) >= mp->m_sb.sb_agcount)
855 * Update the userspace bulk request @hdr to reflect the end state of the
856 * internal bulk request @breq.
859 xfs_bulk_ireq_teardown(
860 struct xfs_bulk_ireq *hdr,
861 struct xfs_ibulk *breq)
863 hdr->ino = breq->startino;
864 hdr->ocount = breq->ocount;
867 /* Handle the v5 bulkstat ioctl. */
870 struct xfs_mount *mp,
872 struct xfs_bulkstat_req __user *arg)
874 struct xfs_bulk_ireq hdr;
875 struct xfs_ibulk breq = {
880 if (!capable(CAP_SYS_ADMIN))
883 if (XFS_FORCED_SHUTDOWN(mp))
886 if (copy_from_user(&hdr, &arg->hdr, sizeof(hdr)))
889 error = xfs_bulk_ireq_setup(mp, &hdr, &breq, arg->bulkstat);
890 if (error == -ECANCELED)
895 error = xfs_bulkstat(&breq, xfs_bulkstat_fmt);
900 xfs_bulk_ireq_teardown(&hdr, &breq);
901 if (copy_to_user(&arg->hdr, &hdr, sizeof(hdr)))
909 struct xfs_ibulk *breq,
910 const struct xfs_inumbers *igrp)
912 if (copy_to_user(breq->ubuffer, igrp, sizeof(struct xfs_inumbers)))
914 return xfs_ibulk_advance(breq, sizeof(struct xfs_inumbers));
917 /* Handle the v5 inumbers ioctl. */
920 struct xfs_mount *mp,
922 struct xfs_inumbers_req __user *arg)
924 struct xfs_bulk_ireq hdr;
925 struct xfs_ibulk breq = {
930 if (!capable(CAP_SYS_ADMIN))
933 if (XFS_FORCED_SHUTDOWN(mp))
936 if (copy_from_user(&hdr, &arg->hdr, sizeof(hdr)))
939 error = xfs_bulk_ireq_setup(mp, &hdr, &breq, arg->inumbers);
940 if (error == -ECANCELED)
945 error = xfs_inumbers(&breq, xfs_inumbers_fmt);
950 xfs_bulk_ireq_teardown(&hdr, &breq);
951 if (copy_to_user(&arg->hdr, &hdr, sizeof(hdr)))
959 struct xfs_mount *mp,
963 struct xfs_fsop_geom fsgeo;
966 xfs_fs_geometry(&mp->m_sb, &fsgeo, struct_version);
968 if (struct_version <= 3)
969 len = sizeof(struct xfs_fsop_geom_v1);
970 else if (struct_version == 4)
971 len = sizeof(struct xfs_fsop_geom_v4);
973 xfs_fsop_geom_health(mp, &fsgeo);
977 if (copy_to_user(arg, &fsgeo, len))
984 struct xfs_mount *mp,
987 struct xfs_ag_geometry ageo;
990 if (copy_from_user(&ageo, arg, sizeof(ageo)))
994 if (memchr_inv(&ageo.ag_reserved, 0, sizeof(ageo.ag_reserved)))
997 error = xfs_ag_get_geometry(mp, ageo.ag_number, &ageo);
1001 if (copy_to_user(arg, &ageo, sizeof(ageo)))
1007 * Linux extended inode flags interface.
1011 xfs_merge_ioc_xflags(
1015 unsigned int xflags = start;
1017 if (flags & FS_IMMUTABLE_FL)
1018 xflags |= FS_XFLAG_IMMUTABLE;
1020 xflags &= ~FS_XFLAG_IMMUTABLE;
1021 if (flags & FS_APPEND_FL)
1022 xflags |= FS_XFLAG_APPEND;
1024 xflags &= ~FS_XFLAG_APPEND;
1025 if (flags & FS_SYNC_FL)
1026 xflags |= FS_XFLAG_SYNC;
1028 xflags &= ~FS_XFLAG_SYNC;
1029 if (flags & FS_NOATIME_FL)
1030 xflags |= FS_XFLAG_NOATIME;
1032 xflags &= ~FS_XFLAG_NOATIME;
1033 if (flags & FS_NODUMP_FL)
1034 xflags |= FS_XFLAG_NODUMP;
1036 xflags &= ~FS_XFLAG_NODUMP;
1045 unsigned int flags = 0;
1047 if (di_flags & XFS_DIFLAG_IMMUTABLE)
1048 flags |= FS_IMMUTABLE_FL;
1049 if (di_flags & XFS_DIFLAG_APPEND)
1050 flags |= FS_APPEND_FL;
1051 if (di_flags & XFS_DIFLAG_SYNC)
1052 flags |= FS_SYNC_FL;
1053 if (di_flags & XFS_DIFLAG_NOATIME)
1054 flags |= FS_NOATIME_FL;
1055 if (di_flags & XFS_DIFLAG_NODUMP)
1056 flags |= FS_NODUMP_FL;
1062 struct xfs_inode *ip,
1066 simple_fill_fsxattr(fa, xfs_ip2xflags(ip));
1067 fa->fsx_extsize = ip->i_d.di_extsize << ip->i_mount->m_sb.sb_blocklog;
1068 fa->fsx_cowextsize = ip->i_d.di_cowextsize <<
1069 ip->i_mount->m_sb.sb_blocklog;
1070 fa->fsx_projid = xfs_get_projid(ip);
1074 if (ip->i_afp->if_flags & XFS_IFEXTENTS)
1075 fa->fsx_nextents = xfs_iext_count(ip->i_afp);
1077 fa->fsx_nextents = ip->i_d.di_anextents;
1079 fa->fsx_nextents = 0;
1081 if (ip->i_df.if_flags & XFS_IFEXTENTS)
1082 fa->fsx_nextents = xfs_iext_count(&ip->i_df);
1084 fa->fsx_nextents = ip->i_d.di_nextents;
1096 xfs_ilock(ip, XFS_ILOCK_SHARED);
1097 xfs_fill_fsxattr(ip, attr, &fa);
1098 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1100 if (copy_to_user(arg, &fa, sizeof(fa)))
1107 struct xfs_inode *ip,
1108 unsigned int xflags)
1110 /* can't set PREALLOC this way, just preserve it */
1112 (ip->i_d.di_flags & XFS_DIFLAG_PREALLOC);
1114 if (xflags & FS_XFLAG_IMMUTABLE)
1115 di_flags |= XFS_DIFLAG_IMMUTABLE;
1116 if (xflags & FS_XFLAG_APPEND)
1117 di_flags |= XFS_DIFLAG_APPEND;
1118 if (xflags & FS_XFLAG_SYNC)
1119 di_flags |= XFS_DIFLAG_SYNC;
1120 if (xflags & FS_XFLAG_NOATIME)
1121 di_flags |= XFS_DIFLAG_NOATIME;
1122 if (xflags & FS_XFLAG_NODUMP)
1123 di_flags |= XFS_DIFLAG_NODUMP;
1124 if (xflags & FS_XFLAG_NODEFRAG)
1125 di_flags |= XFS_DIFLAG_NODEFRAG;
1126 if (xflags & FS_XFLAG_FILESTREAM)
1127 di_flags |= XFS_DIFLAG_FILESTREAM;
1128 if (S_ISDIR(VFS_I(ip)->i_mode)) {
1129 if (xflags & FS_XFLAG_RTINHERIT)
1130 di_flags |= XFS_DIFLAG_RTINHERIT;
1131 if (xflags & FS_XFLAG_NOSYMLINKS)
1132 di_flags |= XFS_DIFLAG_NOSYMLINKS;
1133 if (xflags & FS_XFLAG_EXTSZINHERIT)
1134 di_flags |= XFS_DIFLAG_EXTSZINHERIT;
1135 if (xflags & FS_XFLAG_PROJINHERIT)
1136 di_flags |= XFS_DIFLAG_PROJINHERIT;
1137 } else if (S_ISREG(VFS_I(ip)->i_mode)) {
1138 if (xflags & FS_XFLAG_REALTIME)
1139 di_flags |= XFS_DIFLAG_REALTIME;
1140 if (xflags & FS_XFLAG_EXTSIZE)
1141 di_flags |= XFS_DIFLAG_EXTSIZE;
1149 struct xfs_inode *ip,
1150 unsigned int xflags)
1152 uint64_t di_flags2 =
1153 (ip->i_d.di_flags2 & XFS_DIFLAG2_REFLINK);
1155 if (xflags & FS_XFLAG_DAX)
1156 di_flags2 |= XFS_DIFLAG2_DAX;
1157 if (xflags & FS_XFLAG_COWEXTSIZE)
1158 di_flags2 |= XFS_DIFLAG2_COWEXTSIZE;
1164 xfs_diflags_to_linux(
1165 struct xfs_inode *ip)
1167 struct inode *inode = VFS_I(ip);
1168 unsigned int xflags = xfs_ip2xflags(ip);
1170 if (xflags & FS_XFLAG_IMMUTABLE)
1171 inode->i_flags |= S_IMMUTABLE;
1173 inode->i_flags &= ~S_IMMUTABLE;
1174 if (xflags & FS_XFLAG_APPEND)
1175 inode->i_flags |= S_APPEND;
1177 inode->i_flags &= ~S_APPEND;
1178 if (xflags & FS_XFLAG_SYNC)
1179 inode->i_flags |= S_SYNC;
1181 inode->i_flags &= ~S_SYNC;
1182 if (xflags & FS_XFLAG_NOATIME)
1183 inode->i_flags |= S_NOATIME;
1185 inode->i_flags &= ~S_NOATIME;
1186 #if 0 /* disabled until the flag switching races are sorted out */
1187 if (xflags & FS_XFLAG_DAX)
1188 inode->i_flags |= S_DAX;
1190 inode->i_flags &= ~S_DAX;
1195 xfs_ioctl_setattr_xflags(
1196 struct xfs_trans *tp,
1197 struct xfs_inode *ip,
1200 struct xfs_mount *mp = ip->i_mount;
1203 /* Can't change realtime flag if any extents are allocated. */
1204 if ((ip->i_d.di_nextents || ip->i_delayed_blks) &&
1205 XFS_IS_REALTIME_INODE(ip) != (fa->fsx_xflags & FS_XFLAG_REALTIME))
1208 /* If realtime flag is set then must have realtime device */
1209 if (fa->fsx_xflags & FS_XFLAG_REALTIME) {
1210 if (mp->m_sb.sb_rblocks == 0 || mp->m_sb.sb_rextsize == 0 ||
1211 (ip->i_d.di_extsize % mp->m_sb.sb_rextsize))
1215 /* Clear reflink if we are actually able to set the rt flag. */
1216 if ((fa->fsx_xflags & FS_XFLAG_REALTIME) && xfs_is_reflink_inode(ip))
1217 ip->i_d.di_flags2 &= ~XFS_DIFLAG2_REFLINK;
1219 /* Don't allow us to set DAX mode for a reflinked file for now. */
1220 if ((fa->fsx_xflags & FS_XFLAG_DAX) && xfs_is_reflink_inode(ip))
1223 /* diflags2 only valid for v3 inodes. */
1224 di_flags2 = xfs_flags2diflags2(ip, fa->fsx_xflags);
1225 if (di_flags2 && ip->i_d.di_version < 3)
1228 ip->i_d.di_flags = xfs_flags2diflags(ip, fa->fsx_xflags);
1229 ip->i_d.di_flags2 = di_flags2;
1231 xfs_diflags_to_linux(ip);
1232 xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
1233 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1234 XFS_STATS_INC(mp, xs_ig_attrchg);
1239 * If we are changing DAX flags, we have to ensure the file is clean and any
1240 * cached objects in the address space are invalidated and removed. This
1241 * requires us to lock out other IO and page faults similar to a truncate
1242 * operation. The locks need to be held until the transaction has been committed
1243 * so that the cache invalidation is atomic with respect to the DAX flag
1247 xfs_ioctl_setattr_dax_invalidate(
1248 struct xfs_inode *ip,
1252 struct inode *inode = VFS_I(ip);
1253 struct super_block *sb = inode->i_sb;
1259 * It is only valid to set the DAX flag on regular files and
1260 * directories on filesystems where the block size is equal to the page
1261 * size. On directories it serves as an inherited hint so we don't
1262 * have to check the device for dax support or flush pagecache.
1264 if (fa->fsx_xflags & FS_XFLAG_DAX) {
1265 struct xfs_buftarg *target = xfs_inode_buftarg(ip);
1267 if (!bdev_dax_supported(target->bt_bdev, sb->s_blocksize))
1271 /* If the DAX state is not changing, we have nothing to do here. */
1272 if ((fa->fsx_xflags & FS_XFLAG_DAX) && IS_DAX(inode))
1274 if (!(fa->fsx_xflags & FS_XFLAG_DAX) && !IS_DAX(inode))
1277 if (S_ISDIR(inode->i_mode))
1280 /* lock, flush and invalidate mapping in preparation for flag change */
1281 xfs_ilock(ip, XFS_MMAPLOCK_EXCL | XFS_IOLOCK_EXCL);
1282 error = filemap_write_and_wait(inode->i_mapping);
1285 error = invalidate_inode_pages2(inode->i_mapping);
1289 *join_flags = XFS_MMAPLOCK_EXCL | XFS_IOLOCK_EXCL;
1293 xfs_iunlock(ip, XFS_MMAPLOCK_EXCL | XFS_IOLOCK_EXCL);
1299 * Set up the transaction structure for the setattr operation, checking that we
1300 * have permission to do so. On success, return a clean transaction and the
1301 * inode locked exclusively ready for further operation specific checks. On
1302 * failure, return an error without modifying or locking the inode.
1304 * The inode might already be IO locked on call. If this is the case, it is
1305 * indicated in @join_flags and we take full responsibility for ensuring they
1306 * are unlocked from now on. Hence if we have an error here, we still have to
1307 * unlock them. Otherwise, once they are joined to the transaction, they will
1308 * be unlocked on commit/cancel.
1310 static struct xfs_trans *
1311 xfs_ioctl_setattr_get_trans(
1312 struct xfs_inode *ip,
1315 struct xfs_mount *mp = ip->i_mount;
1316 struct xfs_trans *tp;
1319 if (mp->m_flags & XFS_MOUNT_RDONLY)
1322 if (XFS_FORCED_SHUTDOWN(mp))
1325 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
1329 xfs_ilock(ip, XFS_ILOCK_EXCL);
1330 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | join_flags);
1334 * CAP_FOWNER overrides the following restrictions:
1336 * The user ID of the calling process must be equal to the file owner
1337 * ID, except in cases where the CAP_FSETID capability is applicable.
1339 if (!inode_owner_or_capable(VFS_I(ip))) {
1344 if (mp->m_flags & XFS_MOUNT_WSYNC)
1345 xfs_trans_set_sync(tp);
1350 xfs_trans_cancel(tp);
1353 xfs_iunlock(ip, join_flags);
1354 return ERR_PTR(error);
1358 * extent size hint validation is somewhat cumbersome. Rules are:
1360 * 1. extent size hint is only valid for directories and regular files
1361 * 2. FS_XFLAG_EXTSIZE is only valid for regular files
1362 * 3. FS_XFLAG_EXTSZINHERIT is only valid for directories.
1363 * 4. can only be changed on regular files if no extents are allocated
1364 * 5. can be changed on directories at any time
1365 * 6. extsize hint of 0 turns off hints, clears inode flags.
1366 * 7. Extent size must be a multiple of the appropriate block size.
1367 * 8. for non-realtime files, the extent size hint must be limited
1368 * to half the AG size to avoid alignment extending the extent beyond the
1371 * Please keep this function in sync with xfs_scrub_inode_extsize.
1374 xfs_ioctl_setattr_check_extsize(
1375 struct xfs_inode *ip,
1378 struct xfs_mount *mp = ip->i_mount;
1380 xfs_fsblock_t extsize_fsb;
1382 if (S_ISREG(VFS_I(ip)->i_mode) && ip->i_d.di_nextents &&
1383 ((ip->i_d.di_extsize << mp->m_sb.sb_blocklog) != fa->fsx_extsize))
1386 if (fa->fsx_extsize == 0)
1389 extsize_fsb = XFS_B_TO_FSB(mp, fa->fsx_extsize);
1390 if (extsize_fsb > MAXEXTLEN)
1393 if (XFS_IS_REALTIME_INODE(ip) ||
1394 (fa->fsx_xflags & FS_XFLAG_REALTIME)) {
1395 size = mp->m_sb.sb_rextsize << mp->m_sb.sb_blocklog;
1397 size = mp->m_sb.sb_blocksize;
1398 if (extsize_fsb > mp->m_sb.sb_agblocks / 2)
1402 if (fa->fsx_extsize % size)
1409 * CoW extent size hint validation rules are:
1411 * 1. CoW extent size hint can only be set if reflink is enabled on the fs.
1412 * The inode does not have to have any shared blocks, but it must be a v3.
1413 * 2. FS_XFLAG_COWEXTSIZE is only valid for directories and regular files;
1414 * for a directory, the hint is propagated to new files.
1415 * 3. Can be changed on files & directories at any time.
1416 * 4. CoW extsize hint of 0 turns off hints, clears inode flags.
1417 * 5. Extent size must be a multiple of the appropriate block size.
1418 * 6. The extent size hint must be limited to half the AG size to avoid
1419 * alignment extending the extent beyond the limits of the AG.
1421 * Please keep this function in sync with xfs_scrub_inode_cowextsize.
1424 xfs_ioctl_setattr_check_cowextsize(
1425 struct xfs_inode *ip,
1428 struct xfs_mount *mp = ip->i_mount;
1430 xfs_fsblock_t cowextsize_fsb;
1432 if (!(fa->fsx_xflags & FS_XFLAG_COWEXTSIZE))
1435 if (!xfs_sb_version_hasreflink(&ip->i_mount->m_sb) ||
1436 ip->i_d.di_version != 3)
1439 if (fa->fsx_cowextsize == 0)
1442 cowextsize_fsb = XFS_B_TO_FSB(mp, fa->fsx_cowextsize);
1443 if (cowextsize_fsb > MAXEXTLEN)
1446 size = mp->m_sb.sb_blocksize;
1447 if (cowextsize_fsb > mp->m_sb.sb_agblocks / 2)
1450 if (fa->fsx_cowextsize % size)
1457 xfs_ioctl_setattr_check_projid(
1458 struct xfs_inode *ip,
1461 /* Disallow 32bit project ids if projid32bit feature is not enabled. */
1462 if (fa->fsx_projid > (uint16_t)-1 &&
1463 !xfs_sb_version_hasprojid32bit(&ip->i_mount->m_sb))
1473 struct fsxattr old_fa;
1474 struct xfs_mount *mp = ip->i_mount;
1475 struct xfs_trans *tp;
1476 struct xfs_dquot *udqp = NULL;
1477 struct xfs_dquot *pdqp = NULL;
1478 struct xfs_dquot *olddquot = NULL;
1482 trace_xfs_ioctl_setattr(ip);
1484 code = xfs_ioctl_setattr_check_projid(ip, fa);
1489 * If disk quotas is on, we make sure that the dquots do exist on disk,
1490 * before we start any other transactions. Trying to do this later
1491 * is messy. We don't care to take a readlock to look at the ids
1492 * in inode here, because we can't hold it across the trans_reserve.
1493 * If the IDs do change before we take the ilock, we're covered
1494 * because the i_*dquot fields will get updated anyway.
1496 if (XFS_IS_QUOTA_ON(mp)) {
1497 code = xfs_qm_vop_dqalloc(ip, ip->i_d.di_uid,
1498 ip->i_d.di_gid, fa->fsx_projid,
1499 XFS_QMOPT_PQUOTA, &udqp, NULL, &pdqp);
1505 * Changing DAX config may require inode locking for mapping
1506 * invalidation. These need to be held all the way to transaction commit
1507 * or cancel time, so need to be passed through to
1508 * xfs_ioctl_setattr_get_trans() so it can apply them to the join call
1511 code = xfs_ioctl_setattr_dax_invalidate(ip, fa, &join_flags);
1513 goto error_free_dquots;
1515 tp = xfs_ioctl_setattr_get_trans(ip, join_flags);
1518 goto error_free_dquots;
1521 if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_PQUOTA_ON(mp) &&
1522 xfs_get_projid(ip) != fa->fsx_projid) {
1523 code = xfs_qm_vop_chown_reserve(tp, ip, udqp, NULL, pdqp,
1524 capable(CAP_FOWNER) ? XFS_QMOPT_FORCE_RES : 0);
1525 if (code) /* out of quota */
1526 goto error_trans_cancel;
1529 xfs_fill_fsxattr(ip, false, &old_fa);
1530 code = vfs_ioc_fssetxattr_check(VFS_I(ip), &old_fa, fa);
1532 goto error_trans_cancel;
1534 code = xfs_ioctl_setattr_check_extsize(ip, fa);
1536 goto error_trans_cancel;
1538 code = xfs_ioctl_setattr_check_cowextsize(ip, fa);
1540 goto error_trans_cancel;
1542 code = xfs_ioctl_setattr_xflags(tp, ip, fa);
1544 goto error_trans_cancel;
1547 * Change file ownership. Must be the owner or privileged. CAP_FSETID
1548 * overrides the following restrictions:
1550 * The set-user-ID and set-group-ID bits of a file will be cleared upon
1551 * successful return from chown()
1554 if ((VFS_I(ip)->i_mode & (S_ISUID|S_ISGID)) &&
1555 !capable_wrt_inode_uidgid(VFS_I(ip), CAP_FSETID))
1556 VFS_I(ip)->i_mode &= ~(S_ISUID|S_ISGID);
1558 /* Change the ownerships and register project quota modifications */
1559 if (xfs_get_projid(ip) != fa->fsx_projid) {
1560 if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_PQUOTA_ON(mp)) {
1561 olddquot = xfs_qm_vop_chown(tp, ip,
1562 &ip->i_pdquot, pdqp);
1564 ASSERT(ip->i_d.di_version > 1);
1565 xfs_set_projid(ip, fa->fsx_projid);
1569 * Only set the extent size hint if we've already determined that the
1570 * extent size hint should be set on the inode. If no extent size flags
1571 * are set on the inode then unconditionally clear the extent size hint.
1573 if (ip->i_d.di_flags & (XFS_DIFLAG_EXTSIZE | XFS_DIFLAG_EXTSZINHERIT))
1574 ip->i_d.di_extsize = fa->fsx_extsize >> mp->m_sb.sb_blocklog;
1576 ip->i_d.di_extsize = 0;
1577 if (ip->i_d.di_version == 3 &&
1578 (ip->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE))
1579 ip->i_d.di_cowextsize = fa->fsx_cowextsize >>
1580 mp->m_sb.sb_blocklog;
1582 ip->i_d.di_cowextsize = 0;
1584 code = xfs_trans_commit(tp);
1587 * Release any dquot(s) the inode had kept before chown.
1589 xfs_qm_dqrele(olddquot);
1590 xfs_qm_dqrele(udqp);
1591 xfs_qm_dqrele(pdqp);
1596 xfs_trans_cancel(tp);
1598 xfs_qm_dqrele(udqp);
1599 xfs_qm_dqrele(pdqp);
1612 if (copy_from_user(&fa, arg, sizeof(fa)))
1615 error = mnt_want_write_file(filp);
1618 error = xfs_ioctl_setattr(ip, &fa);
1619 mnt_drop_write_file(filp);
1630 flags = xfs_di2lxflags(ip->i_d.di_flags);
1631 if (copy_to_user(arg, &flags, sizeof(flags)))
1638 struct xfs_inode *ip,
1642 struct xfs_trans *tp;
1644 struct fsxattr old_fa;
1649 if (copy_from_user(&flags, arg, sizeof(flags)))
1652 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
1653 FS_NOATIME_FL | FS_NODUMP_FL | \
1657 fa.fsx_xflags = xfs_merge_ioc_xflags(flags, xfs_ip2xflags(ip));
1659 error = mnt_want_write_file(filp);
1664 * Changing DAX config may require inode locking for mapping
1665 * invalidation. These need to be held all the way to transaction commit
1666 * or cancel time, so need to be passed through to
1667 * xfs_ioctl_setattr_get_trans() so it can apply them to the join call
1670 error = xfs_ioctl_setattr_dax_invalidate(ip, &fa, &join_flags);
1672 goto out_drop_write;
1674 tp = xfs_ioctl_setattr_get_trans(ip, join_flags);
1676 error = PTR_ERR(tp);
1677 goto out_drop_write;
1680 xfs_fill_fsxattr(ip, false, &old_fa);
1681 error = vfs_ioc_fssetxattr_check(VFS_I(ip), &old_fa, &fa);
1683 xfs_trans_cancel(tp);
1684 goto out_drop_write;
1687 error = xfs_ioctl_setattr_xflags(tp, ip, &fa);
1689 xfs_trans_cancel(tp);
1690 goto out_drop_write;
1693 error = xfs_trans_commit(tp);
1695 mnt_drop_write_file(filp);
1702 struct getbmapx __user *u,
1705 if (put_user(p->bmv_offset, &u->bmv_offset) ||
1706 put_user(p->bmv_block, &u->bmv_block) ||
1707 put_user(p->bmv_length, &u->bmv_length) ||
1708 put_user(0, &u->bmv_count) ||
1709 put_user(0, &u->bmv_entries))
1711 if (recsize < sizeof(struct getbmapx))
1713 if (put_user(0, &u->bmv_iflags) ||
1714 put_user(p->bmv_oflags, &u->bmv_oflags) ||
1715 put_user(0, &u->bmv_unused1) ||
1716 put_user(0, &u->bmv_unused2))
1727 struct getbmapx bmx = { 0 };
1728 struct kgetbmap *buf;
1733 case XFS_IOC_GETBMAPA:
1734 bmx.bmv_iflags = BMV_IF_ATTRFORK;
1736 case XFS_IOC_GETBMAP:
1737 if (file->f_mode & FMODE_NOCMTIME)
1738 bmx.bmv_iflags |= BMV_IF_NO_DMAPI_READ;
1739 /* struct getbmap is a strict subset of struct getbmapx. */
1740 recsize = sizeof(struct getbmap);
1742 case XFS_IOC_GETBMAPX:
1743 recsize = sizeof(struct getbmapx);
1749 if (copy_from_user(&bmx, arg, recsize))
1752 if (bmx.bmv_count < 2)
1754 if (bmx.bmv_count > ULONG_MAX / recsize)
1757 buf = kmem_zalloc_large(bmx.bmv_count * sizeof(*buf), 0);
1761 error = xfs_getbmap(XFS_I(file_inode(file)), &bmx, buf);
1766 if (copy_to_user(arg, &bmx, recsize))
1770 for (i = 0; i < bmx.bmv_entries; i++) {
1771 if (!xfs_getbmap_format(buf + i, arg, recsize))
1782 struct getfsmap_info {
1783 struct xfs_mount *mp;
1784 struct fsmap_head __user *data;
1790 xfs_getfsmap_format(struct xfs_fsmap *xfm, void *priv)
1792 struct getfsmap_info *info = priv;
1795 trace_xfs_getfsmap_mapping(info->mp, xfm);
1797 info->last_flags = xfm->fmr_flags;
1798 xfs_fsmap_from_internal(&fm, xfm);
1799 if (copy_to_user(&info->data->fmh_recs[info->idx++], &fm,
1800 sizeof(struct fsmap)))
1808 struct xfs_inode *ip,
1809 struct fsmap_head __user *arg)
1811 struct getfsmap_info info = { NULL };
1812 struct xfs_fsmap_head xhead = {0};
1813 struct fsmap_head head;
1814 bool aborted = false;
1817 if (copy_from_user(&head, arg, sizeof(struct fsmap_head)))
1819 if (memchr_inv(head.fmh_reserved, 0, sizeof(head.fmh_reserved)) ||
1820 memchr_inv(head.fmh_keys[0].fmr_reserved, 0,
1821 sizeof(head.fmh_keys[0].fmr_reserved)) ||
1822 memchr_inv(head.fmh_keys[1].fmr_reserved, 0,
1823 sizeof(head.fmh_keys[1].fmr_reserved)))
1826 xhead.fmh_iflags = head.fmh_iflags;
1827 xhead.fmh_count = head.fmh_count;
1828 xfs_fsmap_to_internal(&xhead.fmh_keys[0], &head.fmh_keys[0]);
1829 xfs_fsmap_to_internal(&xhead.fmh_keys[1], &head.fmh_keys[1]);
1831 trace_xfs_getfsmap_low_key(ip->i_mount, &xhead.fmh_keys[0]);
1832 trace_xfs_getfsmap_high_key(ip->i_mount, &xhead.fmh_keys[1]);
1834 info.mp = ip->i_mount;
1836 error = xfs_getfsmap(ip->i_mount, &xhead, xfs_getfsmap_format, &info);
1837 if (error == -ECANCELED) {
1843 /* If we didn't abort, set the "last" flag in the last fmx */
1844 if (!aborted && info.idx) {
1845 info.last_flags |= FMR_OF_LAST;
1846 if (copy_to_user(&info.data->fmh_recs[info.idx - 1].fmr_flags,
1847 &info.last_flags, sizeof(info.last_flags)))
1851 /* copy back header */
1852 head.fmh_entries = xhead.fmh_entries;
1853 head.fmh_oflags = xhead.fmh_oflags;
1854 if (copy_to_user(arg, &head, sizeof(struct fsmap_head)))
1861 xfs_ioc_scrub_metadata(
1862 struct xfs_inode *ip,
1865 struct xfs_scrub_metadata scrub;
1868 if (!capable(CAP_SYS_ADMIN))
1871 if (copy_from_user(&scrub, arg, sizeof(scrub)))
1874 error = xfs_scrub_metadata(ip, &scrub);
1878 if (copy_to_user(arg, &scrub, sizeof(scrub)))
1888 xfs_inode_t *ip, *tip;
1892 /* Pull information for the target fd */
1893 f = fdget((int)sxp->sx_fdtarget);
1899 if (!(f.file->f_mode & FMODE_WRITE) ||
1900 !(f.file->f_mode & FMODE_READ) ||
1901 (f.file->f_flags & O_APPEND)) {
1906 tmp = fdget((int)sxp->sx_fdtmp);
1912 if (!(tmp.file->f_mode & FMODE_WRITE) ||
1913 !(tmp.file->f_mode & FMODE_READ) ||
1914 (tmp.file->f_flags & O_APPEND)) {
1916 goto out_put_tmp_file;
1919 if (IS_SWAPFILE(file_inode(f.file)) ||
1920 IS_SWAPFILE(file_inode(tmp.file))) {
1922 goto out_put_tmp_file;
1926 * We need to ensure that the fds passed in point to XFS inodes
1927 * before we cast and access them as XFS structures as we have no
1928 * control over what the user passes us here.
1930 if (f.file->f_op != &xfs_file_operations ||
1931 tmp.file->f_op != &xfs_file_operations) {
1933 goto out_put_tmp_file;
1936 ip = XFS_I(file_inode(f.file));
1937 tip = XFS_I(file_inode(tmp.file));
1939 if (ip->i_mount != tip->i_mount) {
1941 goto out_put_tmp_file;
1944 if (ip->i_ino == tip->i_ino) {
1946 goto out_put_tmp_file;
1949 if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
1951 goto out_put_tmp_file;
1954 error = xfs_swap_extents(ip, tip, sxp);
1966 struct xfs_mount *mp,
1967 char __user *user_label)
1969 struct xfs_sb *sbp = &mp->m_sb;
1970 char label[XFSLABEL_MAX + 1];
1973 BUILD_BUG_ON(sizeof(sbp->sb_fname) > FSLABEL_MAX);
1975 /* 1 larger than sb_fname, so this ensures a trailing NUL char */
1976 memset(label, 0, sizeof(label));
1977 spin_lock(&mp->m_sb_lock);
1978 strncpy(label, sbp->sb_fname, XFSLABEL_MAX);
1979 spin_unlock(&mp->m_sb_lock);
1981 if (copy_to_user(user_label, label, sizeof(label)))
1989 struct xfs_mount *mp,
1990 char __user *newlabel)
1992 struct xfs_sb *sbp = &mp->m_sb;
1993 char label[XFSLABEL_MAX + 1];
1997 if (!capable(CAP_SYS_ADMIN))
2000 * The generic ioctl allows up to FSLABEL_MAX chars, but XFS is much
2001 * smaller, at 12 bytes. We copy one more to be sure we find the
2002 * (required) NULL character to test the incoming label length.
2003 * NB: The on disk label doesn't need to be null terminated.
2005 if (copy_from_user(label, newlabel, XFSLABEL_MAX + 1))
2007 len = strnlen(label, XFSLABEL_MAX + 1);
2008 if (len > sizeof(sbp->sb_fname))
2011 error = mnt_want_write_file(filp);
2015 spin_lock(&mp->m_sb_lock);
2016 memset(sbp->sb_fname, 0, sizeof(sbp->sb_fname));
2017 memcpy(sbp->sb_fname, label, len);
2018 spin_unlock(&mp->m_sb_lock);
2021 * Now we do several things to satisfy userspace.
2022 * In addition to normal logging of the primary superblock, we also
2023 * immediately write these changes to sector zero for the primary, then
2024 * update all backup supers (as xfs_db does for a label change), then
2025 * invalidate the block device page cache. This is so that any prior
2026 * buffered reads from userspace (i.e. from blkid) are invalidated,
2027 * and userspace will see the newly-written label.
2029 error = xfs_sync_sb_buf(mp);
2033 * growfs also updates backup supers so lock against that.
2035 mutex_lock(&mp->m_growlock);
2036 error = xfs_update_secondary_sbs(mp);
2037 mutex_unlock(&mp->m_growlock);
2039 invalidate_bdev(mp->m_ddev_targp->bt_bdev);
2042 mnt_drop_write_file(filp);
2047 * Note: some of the ioctl's return positive numbers as a
2048 * byte count indicating success, such as readlink_by_handle.
2049 * So we don't "sign flip" like most other routines. This means
2050 * true errors need to be returned as a negative value.
2058 struct inode *inode = file_inode(filp);
2059 struct xfs_inode *ip = XFS_I(inode);
2060 struct xfs_mount *mp = ip->i_mount;
2061 void __user *arg = (void __user *)p;
2064 trace_xfs_file_ioctl(ip);
2068 return xfs_ioc_trim(mp, arg);
2069 case FS_IOC_GETFSLABEL:
2070 return xfs_ioc_getlabel(mp, arg);
2071 case FS_IOC_SETFSLABEL:
2072 return xfs_ioc_setlabel(filp, mp, arg);
2073 case XFS_IOC_ALLOCSP:
2074 case XFS_IOC_FREESP:
2075 case XFS_IOC_ALLOCSP64:
2076 case XFS_IOC_FREESP64: {
2079 if (copy_from_user(&bf, arg, sizeof(bf)))
2081 return xfs_ioc_space(filp, &bf);
2083 case XFS_IOC_DIOINFO: {
2084 struct xfs_buftarg *target = xfs_inode_buftarg(ip);
2087 da.d_mem = da.d_miniosz = target->bt_logical_sectorsize;
2088 da.d_maxiosz = INT_MAX & ~(da.d_miniosz - 1);
2090 if (copy_to_user(arg, &da, sizeof(da)))
2095 case XFS_IOC_FSBULKSTAT_SINGLE:
2096 case XFS_IOC_FSBULKSTAT:
2097 case XFS_IOC_FSINUMBERS:
2098 return xfs_ioc_fsbulkstat(mp, cmd, arg);
2100 case XFS_IOC_BULKSTAT:
2101 return xfs_ioc_bulkstat(mp, cmd, arg);
2102 case XFS_IOC_INUMBERS:
2103 return xfs_ioc_inumbers(mp, cmd, arg);
2105 case XFS_IOC_FSGEOMETRY_V1:
2106 return xfs_ioc_fsgeometry(mp, arg, 3);
2107 case XFS_IOC_FSGEOMETRY_V4:
2108 return xfs_ioc_fsgeometry(mp, arg, 4);
2109 case XFS_IOC_FSGEOMETRY:
2110 return xfs_ioc_fsgeometry(mp, arg, 5);
2112 case XFS_IOC_AG_GEOMETRY:
2113 return xfs_ioc_ag_geometry(mp, arg);
2115 case XFS_IOC_GETVERSION:
2116 return put_user(inode->i_generation, (int __user *)arg);
2118 case XFS_IOC_FSGETXATTR:
2119 return xfs_ioc_fsgetxattr(ip, 0, arg);
2120 case XFS_IOC_FSGETXATTRA:
2121 return xfs_ioc_fsgetxattr(ip, 1, arg);
2122 case XFS_IOC_FSSETXATTR:
2123 return xfs_ioc_fssetxattr(ip, filp, arg);
2124 case XFS_IOC_GETXFLAGS:
2125 return xfs_ioc_getxflags(ip, arg);
2126 case XFS_IOC_SETXFLAGS:
2127 return xfs_ioc_setxflags(ip, filp, arg);
2129 case XFS_IOC_FSSETDM: {
2130 struct fsdmidata dmi;
2132 if (copy_from_user(&dmi, arg, sizeof(dmi)))
2135 error = mnt_want_write_file(filp);
2139 error = xfs_set_dmattrs(ip, dmi.fsd_dmevmask,
2141 mnt_drop_write_file(filp);
2145 case XFS_IOC_GETBMAP:
2146 case XFS_IOC_GETBMAPA:
2147 case XFS_IOC_GETBMAPX:
2148 return xfs_ioc_getbmap(filp, cmd, arg);
2150 case FS_IOC_GETFSMAP:
2151 return xfs_ioc_getfsmap(ip, arg);
2153 case XFS_IOC_SCRUB_METADATA:
2154 return xfs_ioc_scrub_metadata(ip, arg);
2156 case XFS_IOC_FD_TO_HANDLE:
2157 case XFS_IOC_PATH_TO_HANDLE:
2158 case XFS_IOC_PATH_TO_FSHANDLE: {
2159 xfs_fsop_handlereq_t hreq;
2161 if (copy_from_user(&hreq, arg, sizeof(hreq)))
2163 return xfs_find_handle(cmd, &hreq);
2165 case XFS_IOC_OPEN_BY_HANDLE: {
2166 xfs_fsop_handlereq_t hreq;
2168 if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
2170 return xfs_open_by_handle(filp, &hreq);
2172 case XFS_IOC_FSSETDM_BY_HANDLE:
2173 return xfs_fssetdm_by_handle(filp, arg);
2175 case XFS_IOC_READLINK_BY_HANDLE: {
2176 xfs_fsop_handlereq_t hreq;
2178 if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
2180 return xfs_readlink_by_handle(filp, &hreq);
2182 case XFS_IOC_ATTRLIST_BY_HANDLE:
2183 return xfs_attrlist_by_handle(filp, arg);
2185 case XFS_IOC_ATTRMULTI_BY_HANDLE:
2186 return xfs_attrmulti_by_handle(filp, arg);
2188 case XFS_IOC_SWAPEXT: {
2189 struct xfs_swapext sxp;
2191 if (copy_from_user(&sxp, arg, sizeof(xfs_swapext_t)))
2193 error = mnt_want_write_file(filp);
2196 error = xfs_ioc_swapext(&sxp);
2197 mnt_drop_write_file(filp);
2201 case XFS_IOC_FSCOUNTS: {
2202 xfs_fsop_counts_t out;
2204 xfs_fs_counts(mp, &out);
2206 if (copy_to_user(arg, &out, sizeof(out)))
2211 case XFS_IOC_SET_RESBLKS: {
2212 xfs_fsop_resblks_t inout;
2215 if (!capable(CAP_SYS_ADMIN))
2218 if (mp->m_flags & XFS_MOUNT_RDONLY)
2221 if (copy_from_user(&inout, arg, sizeof(inout)))
2224 error = mnt_want_write_file(filp);
2228 /* input parameter is passed in resblks field of structure */
2230 error = xfs_reserve_blocks(mp, &in, &inout);
2231 mnt_drop_write_file(filp);
2235 if (copy_to_user(arg, &inout, sizeof(inout)))
2240 case XFS_IOC_GET_RESBLKS: {
2241 xfs_fsop_resblks_t out;
2243 if (!capable(CAP_SYS_ADMIN))
2246 error = xfs_reserve_blocks(mp, NULL, &out);
2250 if (copy_to_user(arg, &out, sizeof(out)))
2256 case XFS_IOC_FSGROWFSDATA: {
2257 xfs_growfs_data_t in;
2259 if (copy_from_user(&in, arg, sizeof(in)))
2262 error = mnt_want_write_file(filp);
2265 error = xfs_growfs_data(mp, &in);
2266 mnt_drop_write_file(filp);
2270 case XFS_IOC_FSGROWFSLOG: {
2271 xfs_growfs_log_t in;
2273 if (copy_from_user(&in, arg, sizeof(in)))
2276 error = mnt_want_write_file(filp);
2279 error = xfs_growfs_log(mp, &in);
2280 mnt_drop_write_file(filp);
2284 case XFS_IOC_FSGROWFSRT: {
2287 if (copy_from_user(&in, arg, sizeof(in)))
2290 error = mnt_want_write_file(filp);
2293 error = xfs_growfs_rt(mp, &in);
2294 mnt_drop_write_file(filp);
2298 case XFS_IOC_GOINGDOWN: {
2301 if (!capable(CAP_SYS_ADMIN))
2304 if (get_user(in, (uint32_t __user *)arg))
2307 return xfs_fs_goingdown(mp, in);
2310 case XFS_IOC_ERROR_INJECTION: {
2311 xfs_error_injection_t in;
2313 if (!capable(CAP_SYS_ADMIN))
2316 if (copy_from_user(&in, arg, sizeof(in)))
2319 return xfs_errortag_add(mp, in.errtag);
2322 case XFS_IOC_ERROR_CLEARALL:
2323 if (!capable(CAP_SYS_ADMIN))
2326 return xfs_errortag_clearall(mp);
2328 case XFS_IOC_FREE_EOFBLOCKS: {
2329 struct xfs_fs_eofblocks eofb;
2330 struct xfs_eofblocks keofb;
2332 if (!capable(CAP_SYS_ADMIN))
2335 if (mp->m_flags & XFS_MOUNT_RDONLY)
2338 if (copy_from_user(&eofb, arg, sizeof(eofb)))
2341 error = xfs_fs_eofblocks_from_user(&eofb, &keofb);
2345 return xfs_icache_free_eofblocks(mp, &keofb);