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);
301 xfs_mount_t *mp = ip->i_mount;
305 if (!capable(CAP_SYS_ADMIN))
308 if (XFS_FORCED_SHUTDOWN(mp))
311 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
315 xfs_ilock(ip, XFS_ILOCK_EXCL);
316 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
318 ip->i_d.di_dmevmask = evmask;
319 ip->i_d.di_dmstate = state;
321 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
322 error = xfs_trans_commit(tp);
328 xfs_fssetdm_by_handle(
329 struct file *parfilp,
333 struct fsdmidata fsd;
334 xfs_fsop_setdm_handlereq_t dmhreq;
335 struct dentry *dentry;
337 if (!capable(CAP_MKNOD))
339 if (copy_from_user(&dmhreq, arg, sizeof(xfs_fsop_setdm_handlereq_t)))
342 error = mnt_want_write_file(parfilp);
346 dentry = xfs_handlereq_to_dentry(parfilp, &dmhreq.hreq);
347 if (IS_ERR(dentry)) {
348 mnt_drop_write_file(parfilp);
349 return PTR_ERR(dentry);
352 if (IS_IMMUTABLE(d_inode(dentry)) || IS_APPEND(d_inode(dentry))) {
357 if (copy_from_user(&fsd, dmhreq.data, sizeof(fsd))) {
362 error = xfs_set_dmattrs(XFS_I(d_inode(dentry)), fsd.fsd_dmevmask,
366 mnt_drop_write_file(parfilp);
372 xfs_attrlist_by_handle(
373 struct file *parfilp,
377 attrlist_cursor_kern_t *cursor;
378 struct xfs_fsop_attrlist_handlereq __user *p = arg;
379 xfs_fsop_attrlist_handlereq_t al_hreq;
380 struct dentry *dentry;
383 if (!capable(CAP_SYS_ADMIN))
385 if (copy_from_user(&al_hreq, arg, sizeof(xfs_fsop_attrlist_handlereq_t)))
387 if (al_hreq.buflen < sizeof(struct attrlist) ||
388 al_hreq.buflen > XFS_XATTR_LIST_MAX)
392 * Reject flags, only allow namespaces.
394 if (al_hreq.flags & ~(ATTR_ROOT | ATTR_SECURE))
397 dentry = xfs_handlereq_to_dentry(parfilp, &al_hreq.hreq);
399 return PTR_ERR(dentry);
401 kbuf = kmem_zalloc_large(al_hreq.buflen, 0);
405 cursor = (attrlist_cursor_kern_t *)&al_hreq.pos;
406 error = xfs_attr_list(XFS_I(d_inode(dentry)), kbuf, al_hreq.buflen,
407 al_hreq.flags, cursor);
411 if (copy_to_user(&p->pos, cursor, sizeof(attrlist_cursor_kern_t))) {
416 if (copy_to_user(al_hreq.buffer, kbuf, al_hreq.buflen))
427 xfs_attrmulti_attr_get(
430 unsigned char __user *ubuf,
437 if (*len > XFS_XATTR_SIZE_MAX)
439 kbuf = kmem_zalloc_large(*len, 0);
443 error = xfs_attr_get(XFS_I(inode), name, &kbuf, (int *)len, flags);
447 if (copy_to_user(ubuf, kbuf, *len))
456 xfs_attrmulti_attr_set(
459 const unsigned char __user *ubuf,
466 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
468 if (len > XFS_XATTR_SIZE_MAX)
471 kbuf = memdup_user(ubuf, len);
473 return PTR_ERR(kbuf);
475 error = xfs_attr_set(XFS_I(inode), name, kbuf, len, flags);
477 xfs_forget_acl(inode, name, flags);
483 xfs_attrmulti_attr_remove(
490 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
492 error = xfs_attr_remove(XFS_I(inode), name, flags);
494 xfs_forget_acl(inode, name, flags);
499 xfs_attrmulti_by_handle(
500 struct file *parfilp,
504 xfs_attr_multiop_t *ops;
505 xfs_fsop_attrmulti_handlereq_t am_hreq;
506 struct dentry *dentry;
507 unsigned int i, size;
508 unsigned char *attr_name;
510 if (!capable(CAP_SYS_ADMIN))
512 if (copy_from_user(&am_hreq, arg, sizeof(xfs_fsop_attrmulti_handlereq_t)))
516 if (am_hreq.opcount >= INT_MAX / sizeof(xfs_attr_multiop_t))
519 dentry = xfs_handlereq_to_dentry(parfilp, &am_hreq.hreq);
521 return PTR_ERR(dentry);
524 size = am_hreq.opcount * sizeof(xfs_attr_multiop_t);
525 if (!size || size > 16 * PAGE_SIZE)
528 ops = memdup_user(am_hreq.ops, size);
530 error = PTR_ERR(ops);
535 attr_name = kmalloc(MAXNAMELEN, GFP_KERNEL);
540 for (i = 0; i < am_hreq.opcount; i++) {
541 ops[i].am_error = strncpy_from_user((char *)attr_name,
542 ops[i].am_attrname, MAXNAMELEN);
543 if (ops[i].am_error == 0 || ops[i].am_error == MAXNAMELEN)
545 if (ops[i].am_error < 0)
548 switch (ops[i].am_opcode) {
550 ops[i].am_error = xfs_attrmulti_attr_get(
551 d_inode(dentry), attr_name,
552 ops[i].am_attrvalue, &ops[i].am_length,
556 ops[i].am_error = mnt_want_write_file(parfilp);
559 ops[i].am_error = xfs_attrmulti_attr_set(
560 d_inode(dentry), attr_name,
561 ops[i].am_attrvalue, ops[i].am_length,
563 mnt_drop_write_file(parfilp);
566 ops[i].am_error = mnt_want_write_file(parfilp);
569 ops[i].am_error = xfs_attrmulti_attr_remove(
570 d_inode(dentry), attr_name,
572 mnt_drop_write_file(parfilp);
575 ops[i].am_error = -EINVAL;
579 if (copy_to_user(am_hreq.ops, ops, size))
595 struct inode *inode = file_inode(filp);
596 struct xfs_inode *ip = XFS_I(inode);
598 enum xfs_prealloc_flags flags = XFS_PREALLOC_CLEAR;
599 uint iolock = XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL;
602 if (inode->i_flags & (S_IMMUTABLE|S_APPEND))
605 if (!(filp->f_mode & FMODE_WRITE))
608 if (!S_ISREG(inode->i_mode))
611 if (xfs_is_always_cow_inode(ip))
614 if (filp->f_flags & O_DSYNC)
615 flags |= XFS_PREALLOC_SYNC;
616 if (filp->f_mode & FMODE_NOCMTIME)
617 flags |= XFS_PREALLOC_INVISIBLE;
619 error = mnt_want_write_file(filp);
623 xfs_ilock(ip, iolock);
624 error = xfs_break_layouts(inode, &iolock, BREAK_UNMAP);
627 inode_dio_wait(inode);
629 switch (bf->l_whence) {
633 bf->l_start += filp->f_pos;
636 bf->l_start += XFS_ISIZE(ip);
643 if (bf->l_start < 0 || bf->l_start > inode->i_sb->s_maxbytes) {
648 if (bf->l_start > XFS_ISIZE(ip)) {
649 error = xfs_alloc_file_space(ip, XFS_ISIZE(ip),
650 bf->l_start - XFS_ISIZE(ip), 0);
655 iattr.ia_valid = ATTR_SIZE;
656 iattr.ia_size = bf->l_start;
657 error = xfs_vn_setattr_size(file_dentry(filp), &iattr);
661 error = xfs_update_prealloc_flags(ip, flags);
664 xfs_iunlock(ip, iolock);
665 mnt_drop_write_file(filp);
669 /* Return 0 on success or positive error */
671 xfs_fsbulkstat_one_fmt(
672 struct xfs_ibulk *breq,
673 const struct xfs_bulkstat *bstat)
675 struct xfs_bstat bs1;
677 xfs_bulkstat_to_bstat(breq->mp, &bs1, bstat);
678 if (copy_to_user(breq->ubuffer, &bs1, sizeof(bs1)))
680 return xfs_ibulk_advance(breq, sizeof(struct xfs_bstat));
685 struct xfs_ibulk *breq,
686 const struct xfs_inumbers *igrp)
688 struct xfs_inogrp ig1;
690 xfs_inumbers_to_inogrp(&ig1, igrp);
691 if (copy_to_user(breq->ubuffer, &ig1, sizeof(struct xfs_inogrp)))
693 return xfs_ibulk_advance(breq, sizeof(struct xfs_inogrp));
702 struct xfs_fsop_bulkreq bulkreq;
703 struct xfs_ibulk breq = {
710 /* done = 1 if there are more stats to get and if bulkstat */
711 /* should be called again (unused here, but used in dmapi) */
713 if (!capable(CAP_SYS_ADMIN))
716 if (XFS_FORCED_SHUTDOWN(mp))
719 if (copy_from_user(&bulkreq, arg, sizeof(struct xfs_fsop_bulkreq)))
722 if (copy_from_user(&lastino, bulkreq.lastip, sizeof(__s64)))
725 if (bulkreq.icount <= 0)
728 if (bulkreq.ubuffer == NULL)
731 breq.ubuffer = bulkreq.ubuffer;
732 breq.icount = bulkreq.icount;
735 * FSBULKSTAT_SINGLE expects that *lastip contains the inode number
736 * that we want to stat. However, FSINUMBERS and FSBULKSTAT expect
737 * that *lastip contains either zero or the number of the last inode to
738 * be examined by the previous call and return results starting with
739 * the next inode after that. The new bulk request back end functions
740 * take the inode to start with, so we have to compute the startino
741 * parameter from lastino to maintain correct function. lastino == 0
742 * is a special case because it has traditionally meant "first inode
745 if (cmd == XFS_IOC_FSINUMBERS) {
746 breq.startino = lastino ? lastino + 1 : 0;
747 error = xfs_inumbers(&breq, xfs_fsinumbers_fmt);
748 lastino = breq.startino - 1;
749 } else if (cmd == XFS_IOC_FSBULKSTAT_SINGLE) {
750 breq.startino = lastino;
752 error = xfs_bulkstat_one(&breq, xfs_fsbulkstat_one_fmt);
753 } else { /* XFS_IOC_FSBULKSTAT */
754 breq.startino = lastino ? lastino + 1 : 0;
755 error = xfs_bulkstat(&breq, xfs_fsbulkstat_one_fmt);
756 lastino = breq.startino - 1;
762 if (bulkreq.lastip != NULL &&
763 copy_to_user(bulkreq.lastip, &lastino, sizeof(xfs_ino_t)))
766 if (bulkreq.ocount != NULL &&
767 copy_to_user(bulkreq.ocount, &breq.ocount, sizeof(__s32)))
773 /* Return 0 on success or positive error */
776 struct xfs_ibulk *breq,
777 const struct xfs_bulkstat *bstat)
779 if (copy_to_user(breq->ubuffer, bstat, sizeof(struct xfs_bulkstat)))
781 return xfs_ibulk_advance(breq, sizeof(struct xfs_bulkstat));
785 * Check the incoming bulk request @hdr from userspace and initialize the
786 * internal @breq bulk request appropriately. Returns 0 if the bulk request
787 * should proceed; -ECANCELED if there's nothing to do; or the usual
788 * negative error code.
792 struct xfs_mount *mp,
793 struct xfs_bulk_ireq *hdr,
794 struct xfs_ibulk *breq,
795 void __user *ubuffer)
797 if (hdr->icount == 0 ||
798 (hdr->flags & ~XFS_BULK_IREQ_FLAGS_ALL) ||
799 memchr_inv(hdr->reserved, 0, sizeof(hdr->reserved)))
802 breq->startino = hdr->ino;
803 breq->ubuffer = ubuffer;
804 breq->icount = hdr->icount;
809 * The @ino parameter is a special value, so we must look it up here.
810 * We're not allowed to have IREQ_AGNO, and we only return one inode
813 if (hdr->flags & XFS_BULK_IREQ_SPECIAL) {
814 if (hdr->flags & XFS_BULK_IREQ_AGNO)
818 case XFS_BULK_IREQ_SPECIAL_ROOT:
819 hdr->ino = mp->m_sb.sb_rootino;
828 * The IREQ_AGNO flag means that we only want results from a given AG.
829 * If @hdr->ino is zero, we start iterating in that AG. If @hdr->ino is
830 * beyond the specified AG then we return no results.
832 if (hdr->flags & XFS_BULK_IREQ_AGNO) {
833 if (hdr->agno >= mp->m_sb.sb_agcount)
836 if (breq->startino == 0)
837 breq->startino = XFS_AGINO_TO_INO(mp, hdr->agno, 0);
838 else if (XFS_INO_TO_AGNO(mp, breq->startino) < hdr->agno)
841 breq->flags |= XFS_IBULK_SAME_AG;
843 /* Asking for an inode past the end of the AG? We're done! */
844 if (XFS_INO_TO_AGNO(mp, breq->startino) > hdr->agno)
846 } else if (hdr->agno)
849 /* Asking for an inode past the end of the FS? We're done! */
850 if (XFS_INO_TO_AGNO(mp, breq->startino) >= mp->m_sb.sb_agcount)
857 * Update the userspace bulk request @hdr to reflect the end state of the
858 * internal bulk request @breq.
861 xfs_bulk_ireq_teardown(
862 struct xfs_bulk_ireq *hdr,
863 struct xfs_ibulk *breq)
865 hdr->ino = breq->startino;
866 hdr->ocount = breq->ocount;
869 /* Handle the v5 bulkstat ioctl. */
872 struct xfs_mount *mp,
874 struct xfs_bulkstat_req __user *arg)
876 struct xfs_bulk_ireq hdr;
877 struct xfs_ibulk breq = {
882 if (!capable(CAP_SYS_ADMIN))
885 if (XFS_FORCED_SHUTDOWN(mp))
888 if (copy_from_user(&hdr, &arg->hdr, sizeof(hdr)))
891 error = xfs_bulk_ireq_setup(mp, &hdr, &breq, arg->bulkstat);
892 if (error == -ECANCELED)
897 error = xfs_bulkstat(&breq, xfs_bulkstat_fmt);
902 xfs_bulk_ireq_teardown(&hdr, &breq);
903 if (copy_to_user(&arg->hdr, &hdr, sizeof(hdr)))
911 struct xfs_ibulk *breq,
912 const struct xfs_inumbers *igrp)
914 if (copy_to_user(breq->ubuffer, igrp, sizeof(struct xfs_inumbers)))
916 return xfs_ibulk_advance(breq, sizeof(struct xfs_inumbers));
919 /* Handle the v5 inumbers ioctl. */
922 struct xfs_mount *mp,
924 struct xfs_inumbers_req __user *arg)
926 struct xfs_bulk_ireq hdr;
927 struct xfs_ibulk breq = {
932 if (!capable(CAP_SYS_ADMIN))
935 if (XFS_FORCED_SHUTDOWN(mp))
938 if (copy_from_user(&hdr, &arg->hdr, sizeof(hdr)))
941 error = xfs_bulk_ireq_setup(mp, &hdr, &breq, arg->inumbers);
942 if (error == -ECANCELED)
947 error = xfs_inumbers(&breq, xfs_inumbers_fmt);
952 xfs_bulk_ireq_teardown(&hdr, &breq);
953 if (copy_to_user(&arg->hdr, &hdr, sizeof(hdr)))
961 struct xfs_mount *mp,
965 struct xfs_fsop_geom fsgeo;
968 xfs_fs_geometry(&mp->m_sb, &fsgeo, struct_version);
970 if (struct_version <= 3)
971 len = sizeof(struct xfs_fsop_geom_v1);
972 else if (struct_version == 4)
973 len = sizeof(struct xfs_fsop_geom_v4);
975 xfs_fsop_geom_health(mp, &fsgeo);
979 if (copy_to_user(arg, &fsgeo, len))
986 struct xfs_mount *mp,
989 struct xfs_ag_geometry ageo;
992 if (copy_from_user(&ageo, arg, sizeof(ageo)))
996 if (memchr_inv(&ageo.ag_reserved, 0, sizeof(ageo.ag_reserved)))
999 error = xfs_ag_get_geometry(mp, ageo.ag_number, &ageo);
1003 if (copy_to_user(arg, &ageo, sizeof(ageo)))
1009 * Linux extended inode flags interface.
1013 xfs_merge_ioc_xflags(
1017 unsigned int xflags = start;
1019 if (flags & FS_IMMUTABLE_FL)
1020 xflags |= FS_XFLAG_IMMUTABLE;
1022 xflags &= ~FS_XFLAG_IMMUTABLE;
1023 if (flags & FS_APPEND_FL)
1024 xflags |= FS_XFLAG_APPEND;
1026 xflags &= ~FS_XFLAG_APPEND;
1027 if (flags & FS_SYNC_FL)
1028 xflags |= FS_XFLAG_SYNC;
1030 xflags &= ~FS_XFLAG_SYNC;
1031 if (flags & FS_NOATIME_FL)
1032 xflags |= FS_XFLAG_NOATIME;
1034 xflags &= ~FS_XFLAG_NOATIME;
1035 if (flags & FS_NODUMP_FL)
1036 xflags |= FS_XFLAG_NODUMP;
1038 xflags &= ~FS_XFLAG_NODUMP;
1047 unsigned int flags = 0;
1049 if (di_flags & XFS_DIFLAG_IMMUTABLE)
1050 flags |= FS_IMMUTABLE_FL;
1051 if (di_flags & XFS_DIFLAG_APPEND)
1052 flags |= FS_APPEND_FL;
1053 if (di_flags & XFS_DIFLAG_SYNC)
1054 flags |= FS_SYNC_FL;
1055 if (di_flags & XFS_DIFLAG_NOATIME)
1056 flags |= FS_NOATIME_FL;
1057 if (di_flags & XFS_DIFLAG_NODUMP)
1058 flags |= FS_NODUMP_FL;
1064 struct xfs_inode *ip,
1068 simple_fill_fsxattr(fa, xfs_ip2xflags(ip));
1069 fa->fsx_extsize = ip->i_d.di_extsize << ip->i_mount->m_sb.sb_blocklog;
1070 fa->fsx_cowextsize = ip->i_d.di_cowextsize <<
1071 ip->i_mount->m_sb.sb_blocklog;
1072 fa->fsx_projid = xfs_get_projid(ip);
1076 if (ip->i_afp->if_flags & XFS_IFEXTENTS)
1077 fa->fsx_nextents = xfs_iext_count(ip->i_afp);
1079 fa->fsx_nextents = ip->i_d.di_anextents;
1081 fa->fsx_nextents = 0;
1083 if (ip->i_df.if_flags & XFS_IFEXTENTS)
1084 fa->fsx_nextents = xfs_iext_count(&ip->i_df);
1086 fa->fsx_nextents = ip->i_d.di_nextents;
1098 xfs_ilock(ip, XFS_ILOCK_SHARED);
1099 xfs_fill_fsxattr(ip, attr, &fa);
1100 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1102 if (copy_to_user(arg, &fa, sizeof(fa)))
1109 struct xfs_inode *ip,
1110 unsigned int xflags)
1112 /* can't set PREALLOC this way, just preserve it */
1114 (ip->i_d.di_flags & XFS_DIFLAG_PREALLOC);
1116 if (xflags & FS_XFLAG_IMMUTABLE)
1117 di_flags |= XFS_DIFLAG_IMMUTABLE;
1118 if (xflags & FS_XFLAG_APPEND)
1119 di_flags |= XFS_DIFLAG_APPEND;
1120 if (xflags & FS_XFLAG_SYNC)
1121 di_flags |= XFS_DIFLAG_SYNC;
1122 if (xflags & FS_XFLAG_NOATIME)
1123 di_flags |= XFS_DIFLAG_NOATIME;
1124 if (xflags & FS_XFLAG_NODUMP)
1125 di_flags |= XFS_DIFLAG_NODUMP;
1126 if (xflags & FS_XFLAG_NODEFRAG)
1127 di_flags |= XFS_DIFLAG_NODEFRAG;
1128 if (xflags & FS_XFLAG_FILESTREAM)
1129 di_flags |= XFS_DIFLAG_FILESTREAM;
1130 if (S_ISDIR(VFS_I(ip)->i_mode)) {
1131 if (xflags & FS_XFLAG_RTINHERIT)
1132 di_flags |= XFS_DIFLAG_RTINHERIT;
1133 if (xflags & FS_XFLAG_NOSYMLINKS)
1134 di_flags |= XFS_DIFLAG_NOSYMLINKS;
1135 if (xflags & FS_XFLAG_EXTSZINHERIT)
1136 di_flags |= XFS_DIFLAG_EXTSZINHERIT;
1137 if (xflags & FS_XFLAG_PROJINHERIT)
1138 di_flags |= XFS_DIFLAG_PROJINHERIT;
1139 } else if (S_ISREG(VFS_I(ip)->i_mode)) {
1140 if (xflags & FS_XFLAG_REALTIME)
1141 di_flags |= XFS_DIFLAG_REALTIME;
1142 if (xflags & FS_XFLAG_EXTSIZE)
1143 di_flags |= XFS_DIFLAG_EXTSIZE;
1151 struct xfs_inode *ip,
1152 unsigned int xflags)
1154 uint64_t di_flags2 =
1155 (ip->i_d.di_flags2 & XFS_DIFLAG2_REFLINK);
1157 if (xflags & FS_XFLAG_DAX)
1158 di_flags2 |= XFS_DIFLAG2_DAX;
1159 if (xflags & FS_XFLAG_COWEXTSIZE)
1160 di_flags2 |= XFS_DIFLAG2_COWEXTSIZE;
1166 xfs_diflags_to_linux(
1167 struct xfs_inode *ip)
1169 struct inode *inode = VFS_I(ip);
1170 unsigned int xflags = xfs_ip2xflags(ip);
1172 if (xflags & FS_XFLAG_IMMUTABLE)
1173 inode->i_flags |= S_IMMUTABLE;
1175 inode->i_flags &= ~S_IMMUTABLE;
1176 if (xflags & FS_XFLAG_APPEND)
1177 inode->i_flags |= S_APPEND;
1179 inode->i_flags &= ~S_APPEND;
1180 if (xflags & FS_XFLAG_SYNC)
1181 inode->i_flags |= S_SYNC;
1183 inode->i_flags &= ~S_SYNC;
1184 if (xflags & FS_XFLAG_NOATIME)
1185 inode->i_flags |= S_NOATIME;
1187 inode->i_flags &= ~S_NOATIME;
1188 #if 0 /* disabled until the flag switching races are sorted out */
1189 if (xflags & FS_XFLAG_DAX)
1190 inode->i_flags |= S_DAX;
1192 inode->i_flags &= ~S_DAX;
1197 xfs_ioctl_setattr_xflags(
1198 struct xfs_trans *tp,
1199 struct xfs_inode *ip,
1202 struct xfs_mount *mp = ip->i_mount;
1205 /* Can't change realtime flag if any extents are allocated. */
1206 if ((ip->i_d.di_nextents || ip->i_delayed_blks) &&
1207 XFS_IS_REALTIME_INODE(ip) != (fa->fsx_xflags & FS_XFLAG_REALTIME))
1210 /* If realtime flag is set then must have realtime device */
1211 if (fa->fsx_xflags & FS_XFLAG_REALTIME) {
1212 if (mp->m_sb.sb_rblocks == 0 || mp->m_sb.sb_rextsize == 0 ||
1213 (ip->i_d.di_extsize % mp->m_sb.sb_rextsize))
1217 /* Clear reflink if we are actually able to set the rt flag. */
1218 if ((fa->fsx_xflags & FS_XFLAG_REALTIME) && xfs_is_reflink_inode(ip))
1219 ip->i_d.di_flags2 &= ~XFS_DIFLAG2_REFLINK;
1221 /* Don't allow us to set DAX mode for a reflinked file for now. */
1222 if ((fa->fsx_xflags & FS_XFLAG_DAX) && xfs_is_reflink_inode(ip))
1225 /* diflags2 only valid for v3 inodes. */
1226 di_flags2 = xfs_flags2diflags2(ip, fa->fsx_xflags);
1227 if (di_flags2 && ip->i_d.di_version < 3)
1230 ip->i_d.di_flags = xfs_flags2diflags(ip, fa->fsx_xflags);
1231 ip->i_d.di_flags2 = di_flags2;
1233 xfs_diflags_to_linux(ip);
1234 xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
1235 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1236 XFS_STATS_INC(mp, xs_ig_attrchg);
1241 * If we are changing DAX flags, we have to ensure the file is clean and any
1242 * cached objects in the address space are invalidated and removed. This
1243 * requires us to lock out other IO and page faults similar to a truncate
1244 * operation. The locks need to be held until the transaction has been committed
1245 * so that the cache invalidation is atomic with respect to the DAX flag
1249 xfs_ioctl_setattr_dax_invalidate(
1250 struct xfs_inode *ip,
1254 struct inode *inode = VFS_I(ip);
1255 struct super_block *sb = inode->i_sb;
1261 * It is only valid to set the DAX flag on regular files and
1262 * directories on filesystems where the block size is equal to the page
1263 * size. On directories it serves as an inherited hint so we don't
1264 * have to check the device for dax support or flush pagecache.
1266 if (fa->fsx_xflags & FS_XFLAG_DAX) {
1267 struct xfs_buftarg *target = xfs_inode_buftarg(ip);
1269 if (!bdev_dax_supported(target->bt_bdev, sb->s_blocksize))
1273 /* If the DAX state is not changing, we have nothing to do here. */
1274 if ((fa->fsx_xflags & FS_XFLAG_DAX) && IS_DAX(inode))
1276 if (!(fa->fsx_xflags & FS_XFLAG_DAX) && !IS_DAX(inode))
1279 if (S_ISDIR(inode->i_mode))
1282 /* lock, flush and invalidate mapping in preparation for flag change */
1283 xfs_ilock(ip, XFS_MMAPLOCK_EXCL | XFS_IOLOCK_EXCL);
1284 error = filemap_write_and_wait(inode->i_mapping);
1287 error = invalidate_inode_pages2(inode->i_mapping);
1291 *join_flags = XFS_MMAPLOCK_EXCL | XFS_IOLOCK_EXCL;
1295 xfs_iunlock(ip, XFS_MMAPLOCK_EXCL | XFS_IOLOCK_EXCL);
1301 * Set up the transaction structure for the setattr operation, checking that we
1302 * have permission to do so. On success, return a clean transaction and the
1303 * inode locked exclusively ready for further operation specific checks. On
1304 * failure, return an error without modifying or locking the inode.
1306 * The inode might already be IO locked on call. If this is the case, it is
1307 * indicated in @join_flags and we take full responsibility for ensuring they
1308 * are unlocked from now on. Hence if we have an error here, we still have to
1309 * unlock them. Otherwise, once they are joined to the transaction, they will
1310 * be unlocked on commit/cancel.
1312 static struct xfs_trans *
1313 xfs_ioctl_setattr_get_trans(
1314 struct xfs_inode *ip,
1317 struct xfs_mount *mp = ip->i_mount;
1318 struct xfs_trans *tp;
1321 if (mp->m_flags & XFS_MOUNT_RDONLY)
1324 if (XFS_FORCED_SHUTDOWN(mp))
1327 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
1331 xfs_ilock(ip, XFS_ILOCK_EXCL);
1332 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | join_flags);
1336 * CAP_FOWNER overrides the following restrictions:
1338 * The user ID of the calling process must be equal to the file owner
1339 * ID, except in cases where the CAP_FSETID capability is applicable.
1341 if (!inode_owner_or_capable(VFS_I(ip))) {
1346 if (mp->m_flags & XFS_MOUNT_WSYNC)
1347 xfs_trans_set_sync(tp);
1352 xfs_trans_cancel(tp);
1355 xfs_iunlock(ip, join_flags);
1356 return ERR_PTR(error);
1360 * extent size hint validation is somewhat cumbersome. Rules are:
1362 * 1. extent size hint is only valid for directories and regular files
1363 * 2. FS_XFLAG_EXTSIZE is only valid for regular files
1364 * 3. FS_XFLAG_EXTSZINHERIT is only valid for directories.
1365 * 4. can only be changed on regular files if no extents are allocated
1366 * 5. can be changed on directories at any time
1367 * 6. extsize hint of 0 turns off hints, clears inode flags.
1368 * 7. Extent size must be a multiple of the appropriate block size.
1369 * 8. for non-realtime files, the extent size hint must be limited
1370 * to half the AG size to avoid alignment extending the extent beyond the
1373 * Please keep this function in sync with xfs_scrub_inode_extsize.
1376 xfs_ioctl_setattr_check_extsize(
1377 struct xfs_inode *ip,
1380 struct xfs_mount *mp = ip->i_mount;
1382 xfs_fsblock_t extsize_fsb;
1384 if (S_ISREG(VFS_I(ip)->i_mode) && ip->i_d.di_nextents &&
1385 ((ip->i_d.di_extsize << mp->m_sb.sb_blocklog) != fa->fsx_extsize))
1388 if (fa->fsx_extsize == 0)
1391 extsize_fsb = XFS_B_TO_FSB(mp, fa->fsx_extsize);
1392 if (extsize_fsb > MAXEXTLEN)
1395 if (XFS_IS_REALTIME_INODE(ip) ||
1396 (fa->fsx_xflags & FS_XFLAG_REALTIME)) {
1397 size = mp->m_sb.sb_rextsize << mp->m_sb.sb_blocklog;
1399 size = mp->m_sb.sb_blocksize;
1400 if (extsize_fsb > mp->m_sb.sb_agblocks / 2)
1404 if (fa->fsx_extsize % size)
1411 * CoW extent size hint validation rules are:
1413 * 1. CoW extent size hint can only be set if reflink is enabled on the fs.
1414 * The inode does not have to have any shared blocks, but it must be a v3.
1415 * 2. FS_XFLAG_COWEXTSIZE is only valid for directories and regular files;
1416 * for a directory, the hint is propagated to new files.
1417 * 3. Can be changed on files & directories at any time.
1418 * 4. CoW extsize hint of 0 turns off hints, clears inode flags.
1419 * 5. Extent size must be a multiple of the appropriate block size.
1420 * 6. The extent size hint must be limited to half the AG size to avoid
1421 * alignment extending the extent beyond the limits of the AG.
1423 * Please keep this function in sync with xfs_scrub_inode_cowextsize.
1426 xfs_ioctl_setattr_check_cowextsize(
1427 struct xfs_inode *ip,
1430 struct xfs_mount *mp = ip->i_mount;
1432 xfs_fsblock_t cowextsize_fsb;
1434 if (!(fa->fsx_xflags & FS_XFLAG_COWEXTSIZE))
1437 if (!xfs_sb_version_hasreflink(&ip->i_mount->m_sb) ||
1438 ip->i_d.di_version != 3)
1441 if (fa->fsx_cowextsize == 0)
1444 cowextsize_fsb = XFS_B_TO_FSB(mp, fa->fsx_cowextsize);
1445 if (cowextsize_fsb > MAXEXTLEN)
1448 size = mp->m_sb.sb_blocksize;
1449 if (cowextsize_fsb > mp->m_sb.sb_agblocks / 2)
1452 if (fa->fsx_cowextsize % size)
1459 xfs_ioctl_setattr_check_projid(
1460 struct xfs_inode *ip,
1463 /* Disallow 32bit project ids if projid32bit feature is not enabled. */
1464 if (fa->fsx_projid > (uint16_t)-1 &&
1465 !xfs_sb_version_hasprojid32bit(&ip->i_mount->m_sb))
1475 struct fsxattr old_fa;
1476 struct xfs_mount *mp = ip->i_mount;
1477 struct xfs_trans *tp;
1478 struct xfs_dquot *udqp = NULL;
1479 struct xfs_dquot *pdqp = NULL;
1480 struct xfs_dquot *olddquot = NULL;
1484 trace_xfs_ioctl_setattr(ip);
1486 code = xfs_ioctl_setattr_check_projid(ip, fa);
1491 * If disk quotas is on, we make sure that the dquots do exist on disk,
1492 * before we start any other transactions. Trying to do this later
1493 * is messy. We don't care to take a readlock to look at the ids
1494 * in inode here, because we can't hold it across the trans_reserve.
1495 * If the IDs do change before we take the ilock, we're covered
1496 * because the i_*dquot fields will get updated anyway.
1498 if (XFS_IS_QUOTA_ON(mp)) {
1499 code = xfs_qm_vop_dqalloc(ip, ip->i_d.di_uid,
1500 ip->i_d.di_gid, fa->fsx_projid,
1501 XFS_QMOPT_PQUOTA, &udqp, NULL, &pdqp);
1507 * Changing DAX config may require inode locking for mapping
1508 * invalidation. These need to be held all the way to transaction commit
1509 * or cancel time, so need to be passed through to
1510 * xfs_ioctl_setattr_get_trans() so it can apply them to the join call
1513 code = xfs_ioctl_setattr_dax_invalidate(ip, fa, &join_flags);
1515 goto error_free_dquots;
1517 tp = xfs_ioctl_setattr_get_trans(ip, join_flags);
1520 goto error_free_dquots;
1523 if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_PQUOTA_ON(mp) &&
1524 xfs_get_projid(ip) != fa->fsx_projid) {
1525 code = xfs_qm_vop_chown_reserve(tp, ip, udqp, NULL, pdqp,
1526 capable(CAP_FOWNER) ? XFS_QMOPT_FORCE_RES : 0);
1527 if (code) /* out of quota */
1528 goto error_trans_cancel;
1531 xfs_fill_fsxattr(ip, false, &old_fa);
1532 code = vfs_ioc_fssetxattr_check(VFS_I(ip), &old_fa, fa);
1534 goto error_trans_cancel;
1536 code = xfs_ioctl_setattr_check_extsize(ip, fa);
1538 goto error_trans_cancel;
1540 code = xfs_ioctl_setattr_check_cowextsize(ip, fa);
1542 goto error_trans_cancel;
1544 code = xfs_ioctl_setattr_xflags(tp, ip, fa);
1546 goto error_trans_cancel;
1549 * Change file ownership. Must be the owner or privileged. CAP_FSETID
1550 * overrides the following restrictions:
1552 * The set-user-ID and set-group-ID bits of a file will be cleared upon
1553 * successful return from chown()
1556 if ((VFS_I(ip)->i_mode & (S_ISUID|S_ISGID)) &&
1557 !capable_wrt_inode_uidgid(VFS_I(ip), CAP_FSETID))
1558 VFS_I(ip)->i_mode &= ~(S_ISUID|S_ISGID);
1560 /* Change the ownerships and register project quota modifications */
1561 if (xfs_get_projid(ip) != fa->fsx_projid) {
1562 if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_PQUOTA_ON(mp)) {
1563 olddquot = xfs_qm_vop_chown(tp, ip,
1564 &ip->i_pdquot, pdqp);
1566 ASSERT(ip->i_d.di_version > 1);
1567 xfs_set_projid(ip, fa->fsx_projid);
1571 * Only set the extent size hint if we've already determined that the
1572 * extent size hint should be set on the inode. If no extent size flags
1573 * are set on the inode then unconditionally clear the extent size hint.
1575 if (ip->i_d.di_flags & (XFS_DIFLAG_EXTSIZE | XFS_DIFLAG_EXTSZINHERIT))
1576 ip->i_d.di_extsize = fa->fsx_extsize >> mp->m_sb.sb_blocklog;
1578 ip->i_d.di_extsize = 0;
1579 if (ip->i_d.di_version == 3 &&
1580 (ip->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE))
1581 ip->i_d.di_cowextsize = fa->fsx_cowextsize >>
1582 mp->m_sb.sb_blocklog;
1584 ip->i_d.di_cowextsize = 0;
1586 code = xfs_trans_commit(tp);
1589 * Release any dquot(s) the inode had kept before chown.
1591 xfs_qm_dqrele(olddquot);
1592 xfs_qm_dqrele(udqp);
1593 xfs_qm_dqrele(pdqp);
1598 xfs_trans_cancel(tp);
1600 xfs_qm_dqrele(udqp);
1601 xfs_qm_dqrele(pdqp);
1614 if (copy_from_user(&fa, arg, sizeof(fa)))
1617 error = mnt_want_write_file(filp);
1620 error = xfs_ioctl_setattr(ip, &fa);
1621 mnt_drop_write_file(filp);
1632 flags = xfs_di2lxflags(ip->i_d.di_flags);
1633 if (copy_to_user(arg, &flags, sizeof(flags)))
1640 struct xfs_inode *ip,
1644 struct xfs_trans *tp;
1646 struct fsxattr old_fa;
1651 if (copy_from_user(&flags, arg, sizeof(flags)))
1654 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
1655 FS_NOATIME_FL | FS_NODUMP_FL | \
1659 fa.fsx_xflags = xfs_merge_ioc_xflags(flags, xfs_ip2xflags(ip));
1661 error = mnt_want_write_file(filp);
1666 * Changing DAX config may require inode locking for mapping
1667 * invalidation. These need to be held all the way to transaction commit
1668 * or cancel time, so need to be passed through to
1669 * xfs_ioctl_setattr_get_trans() so it can apply them to the join call
1672 error = xfs_ioctl_setattr_dax_invalidate(ip, &fa, &join_flags);
1674 goto out_drop_write;
1676 tp = xfs_ioctl_setattr_get_trans(ip, join_flags);
1678 error = PTR_ERR(tp);
1679 goto out_drop_write;
1682 xfs_fill_fsxattr(ip, false, &old_fa);
1683 error = vfs_ioc_fssetxattr_check(VFS_I(ip), &old_fa, &fa);
1685 xfs_trans_cancel(tp);
1686 goto out_drop_write;
1689 error = xfs_ioctl_setattr_xflags(tp, ip, &fa);
1691 xfs_trans_cancel(tp);
1692 goto out_drop_write;
1695 error = xfs_trans_commit(tp);
1697 mnt_drop_write_file(filp);
1704 struct getbmapx __user *u,
1707 if (put_user(p->bmv_offset, &u->bmv_offset) ||
1708 put_user(p->bmv_block, &u->bmv_block) ||
1709 put_user(p->bmv_length, &u->bmv_length) ||
1710 put_user(0, &u->bmv_count) ||
1711 put_user(0, &u->bmv_entries))
1713 if (recsize < sizeof(struct getbmapx))
1715 if (put_user(0, &u->bmv_iflags) ||
1716 put_user(p->bmv_oflags, &u->bmv_oflags) ||
1717 put_user(0, &u->bmv_unused1) ||
1718 put_user(0, &u->bmv_unused2))
1729 struct getbmapx bmx = { 0 };
1730 struct kgetbmap *buf;
1735 case XFS_IOC_GETBMAPA:
1736 bmx.bmv_iflags = BMV_IF_ATTRFORK;
1738 case XFS_IOC_GETBMAP:
1739 if (file->f_mode & FMODE_NOCMTIME)
1740 bmx.bmv_iflags |= BMV_IF_NO_DMAPI_READ;
1741 /* struct getbmap is a strict subset of struct getbmapx. */
1742 recsize = sizeof(struct getbmap);
1744 case XFS_IOC_GETBMAPX:
1745 recsize = sizeof(struct getbmapx);
1751 if (copy_from_user(&bmx, arg, recsize))
1754 if (bmx.bmv_count < 2)
1756 if (bmx.bmv_count > ULONG_MAX / recsize)
1759 buf = kmem_zalloc_large(bmx.bmv_count * sizeof(*buf), 0);
1763 error = xfs_getbmap(XFS_I(file_inode(file)), &bmx, buf);
1768 if (copy_to_user(arg, &bmx, recsize))
1772 for (i = 0; i < bmx.bmv_entries; i++) {
1773 if (!xfs_getbmap_format(buf + i, arg, recsize))
1784 struct getfsmap_info {
1785 struct xfs_mount *mp;
1786 struct fsmap_head __user *data;
1792 xfs_getfsmap_format(struct xfs_fsmap *xfm, void *priv)
1794 struct getfsmap_info *info = priv;
1797 trace_xfs_getfsmap_mapping(info->mp, xfm);
1799 info->last_flags = xfm->fmr_flags;
1800 xfs_fsmap_from_internal(&fm, xfm);
1801 if (copy_to_user(&info->data->fmh_recs[info->idx++], &fm,
1802 sizeof(struct fsmap)))
1810 struct xfs_inode *ip,
1811 struct fsmap_head __user *arg)
1813 struct getfsmap_info info = { NULL };
1814 struct xfs_fsmap_head xhead = {0};
1815 struct fsmap_head head;
1816 bool aborted = false;
1819 if (copy_from_user(&head, arg, sizeof(struct fsmap_head)))
1821 if (memchr_inv(head.fmh_reserved, 0, sizeof(head.fmh_reserved)) ||
1822 memchr_inv(head.fmh_keys[0].fmr_reserved, 0,
1823 sizeof(head.fmh_keys[0].fmr_reserved)) ||
1824 memchr_inv(head.fmh_keys[1].fmr_reserved, 0,
1825 sizeof(head.fmh_keys[1].fmr_reserved)))
1828 xhead.fmh_iflags = head.fmh_iflags;
1829 xhead.fmh_count = head.fmh_count;
1830 xfs_fsmap_to_internal(&xhead.fmh_keys[0], &head.fmh_keys[0]);
1831 xfs_fsmap_to_internal(&xhead.fmh_keys[1], &head.fmh_keys[1]);
1833 trace_xfs_getfsmap_low_key(ip->i_mount, &xhead.fmh_keys[0]);
1834 trace_xfs_getfsmap_high_key(ip->i_mount, &xhead.fmh_keys[1]);
1836 info.mp = ip->i_mount;
1838 error = xfs_getfsmap(ip->i_mount, &xhead, xfs_getfsmap_format, &info);
1839 if (error == -ECANCELED) {
1845 /* If we didn't abort, set the "last" flag in the last fmx */
1846 if (!aborted && info.idx) {
1847 info.last_flags |= FMR_OF_LAST;
1848 if (copy_to_user(&info.data->fmh_recs[info.idx - 1].fmr_flags,
1849 &info.last_flags, sizeof(info.last_flags)))
1853 /* copy back header */
1854 head.fmh_entries = xhead.fmh_entries;
1855 head.fmh_oflags = xhead.fmh_oflags;
1856 if (copy_to_user(arg, &head, sizeof(struct fsmap_head)))
1863 xfs_ioc_scrub_metadata(
1864 struct xfs_inode *ip,
1867 struct xfs_scrub_metadata scrub;
1870 if (!capable(CAP_SYS_ADMIN))
1873 if (copy_from_user(&scrub, arg, sizeof(scrub)))
1876 error = xfs_scrub_metadata(ip, &scrub);
1880 if (copy_to_user(arg, &scrub, sizeof(scrub)))
1890 xfs_inode_t *ip, *tip;
1894 /* Pull information for the target fd */
1895 f = fdget((int)sxp->sx_fdtarget);
1901 if (!(f.file->f_mode & FMODE_WRITE) ||
1902 !(f.file->f_mode & FMODE_READ) ||
1903 (f.file->f_flags & O_APPEND)) {
1908 tmp = fdget((int)sxp->sx_fdtmp);
1914 if (!(tmp.file->f_mode & FMODE_WRITE) ||
1915 !(tmp.file->f_mode & FMODE_READ) ||
1916 (tmp.file->f_flags & O_APPEND)) {
1918 goto out_put_tmp_file;
1921 if (IS_SWAPFILE(file_inode(f.file)) ||
1922 IS_SWAPFILE(file_inode(tmp.file))) {
1924 goto out_put_tmp_file;
1928 * We need to ensure that the fds passed in point to XFS inodes
1929 * before we cast and access them as XFS structures as we have no
1930 * control over what the user passes us here.
1932 if (f.file->f_op != &xfs_file_operations ||
1933 tmp.file->f_op != &xfs_file_operations) {
1935 goto out_put_tmp_file;
1938 ip = XFS_I(file_inode(f.file));
1939 tip = XFS_I(file_inode(tmp.file));
1941 if (ip->i_mount != tip->i_mount) {
1943 goto out_put_tmp_file;
1946 if (ip->i_ino == tip->i_ino) {
1948 goto out_put_tmp_file;
1951 if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
1953 goto out_put_tmp_file;
1956 error = xfs_swap_extents(ip, tip, sxp);
1968 struct xfs_mount *mp,
1969 char __user *user_label)
1971 struct xfs_sb *sbp = &mp->m_sb;
1972 char label[XFSLABEL_MAX + 1];
1975 BUILD_BUG_ON(sizeof(sbp->sb_fname) > FSLABEL_MAX);
1977 /* 1 larger than sb_fname, so this ensures a trailing NUL char */
1978 memset(label, 0, sizeof(label));
1979 spin_lock(&mp->m_sb_lock);
1980 strncpy(label, sbp->sb_fname, XFSLABEL_MAX);
1981 spin_unlock(&mp->m_sb_lock);
1983 if (copy_to_user(user_label, label, sizeof(label)))
1991 struct xfs_mount *mp,
1992 char __user *newlabel)
1994 struct xfs_sb *sbp = &mp->m_sb;
1995 char label[XFSLABEL_MAX + 1];
1999 if (!capable(CAP_SYS_ADMIN))
2002 * The generic ioctl allows up to FSLABEL_MAX chars, but XFS is much
2003 * smaller, at 12 bytes. We copy one more to be sure we find the
2004 * (required) NULL character to test the incoming label length.
2005 * NB: The on disk label doesn't need to be null terminated.
2007 if (copy_from_user(label, newlabel, XFSLABEL_MAX + 1))
2009 len = strnlen(label, XFSLABEL_MAX + 1);
2010 if (len > sizeof(sbp->sb_fname))
2013 error = mnt_want_write_file(filp);
2017 spin_lock(&mp->m_sb_lock);
2018 memset(sbp->sb_fname, 0, sizeof(sbp->sb_fname));
2019 memcpy(sbp->sb_fname, label, len);
2020 spin_unlock(&mp->m_sb_lock);
2023 * Now we do several things to satisfy userspace.
2024 * In addition to normal logging of the primary superblock, we also
2025 * immediately write these changes to sector zero for the primary, then
2026 * update all backup supers (as xfs_db does for a label change), then
2027 * invalidate the block device page cache. This is so that any prior
2028 * buffered reads from userspace (i.e. from blkid) are invalidated,
2029 * and userspace will see the newly-written label.
2031 error = xfs_sync_sb_buf(mp);
2035 * growfs also updates backup supers so lock against that.
2037 mutex_lock(&mp->m_growlock);
2038 error = xfs_update_secondary_sbs(mp);
2039 mutex_unlock(&mp->m_growlock);
2041 invalidate_bdev(mp->m_ddev_targp->bt_bdev);
2044 mnt_drop_write_file(filp);
2049 * Note: some of the ioctl's return positive numbers as a
2050 * byte count indicating success, such as readlink_by_handle.
2051 * So we don't "sign flip" like most other routines. This means
2052 * true errors need to be returned as a negative value.
2060 struct inode *inode = file_inode(filp);
2061 struct xfs_inode *ip = XFS_I(inode);
2062 struct xfs_mount *mp = ip->i_mount;
2063 void __user *arg = (void __user *)p;
2066 trace_xfs_file_ioctl(ip);
2070 return xfs_ioc_trim(mp, arg);
2071 case FS_IOC_GETFSLABEL:
2072 return xfs_ioc_getlabel(mp, arg);
2073 case FS_IOC_SETFSLABEL:
2074 return xfs_ioc_setlabel(filp, mp, arg);
2075 case XFS_IOC_ALLOCSP:
2076 case XFS_IOC_FREESP:
2077 case XFS_IOC_ALLOCSP64:
2078 case XFS_IOC_FREESP64: {
2081 if (copy_from_user(&bf, arg, sizeof(bf)))
2083 return xfs_ioc_space(filp, &bf);
2085 case XFS_IOC_DIOINFO: {
2086 struct xfs_buftarg *target = xfs_inode_buftarg(ip);
2089 da.d_mem = da.d_miniosz = target->bt_logical_sectorsize;
2090 da.d_maxiosz = INT_MAX & ~(da.d_miniosz - 1);
2092 if (copy_to_user(arg, &da, sizeof(da)))
2097 case XFS_IOC_FSBULKSTAT_SINGLE:
2098 case XFS_IOC_FSBULKSTAT:
2099 case XFS_IOC_FSINUMBERS:
2100 return xfs_ioc_fsbulkstat(mp, cmd, arg);
2102 case XFS_IOC_BULKSTAT:
2103 return xfs_ioc_bulkstat(mp, cmd, arg);
2104 case XFS_IOC_INUMBERS:
2105 return xfs_ioc_inumbers(mp, cmd, arg);
2107 case XFS_IOC_FSGEOMETRY_V1:
2108 return xfs_ioc_fsgeometry(mp, arg, 3);
2109 case XFS_IOC_FSGEOMETRY_V4:
2110 return xfs_ioc_fsgeometry(mp, arg, 4);
2111 case XFS_IOC_FSGEOMETRY:
2112 return xfs_ioc_fsgeometry(mp, arg, 5);
2114 case XFS_IOC_AG_GEOMETRY:
2115 return xfs_ioc_ag_geometry(mp, arg);
2117 case XFS_IOC_GETVERSION:
2118 return put_user(inode->i_generation, (int __user *)arg);
2120 case XFS_IOC_FSGETXATTR:
2121 return xfs_ioc_fsgetxattr(ip, 0, arg);
2122 case XFS_IOC_FSGETXATTRA:
2123 return xfs_ioc_fsgetxattr(ip, 1, arg);
2124 case XFS_IOC_FSSETXATTR:
2125 return xfs_ioc_fssetxattr(ip, filp, arg);
2126 case XFS_IOC_GETXFLAGS:
2127 return xfs_ioc_getxflags(ip, arg);
2128 case XFS_IOC_SETXFLAGS:
2129 return xfs_ioc_setxflags(ip, filp, arg);
2131 case XFS_IOC_FSSETDM: {
2132 struct fsdmidata dmi;
2134 if (copy_from_user(&dmi, arg, sizeof(dmi)))
2137 error = mnt_want_write_file(filp);
2141 error = xfs_set_dmattrs(ip, dmi.fsd_dmevmask,
2143 mnt_drop_write_file(filp);
2147 case XFS_IOC_GETBMAP:
2148 case XFS_IOC_GETBMAPA:
2149 case XFS_IOC_GETBMAPX:
2150 return xfs_ioc_getbmap(filp, cmd, arg);
2152 case FS_IOC_GETFSMAP:
2153 return xfs_ioc_getfsmap(ip, arg);
2155 case XFS_IOC_SCRUB_METADATA:
2156 return xfs_ioc_scrub_metadata(ip, arg);
2158 case XFS_IOC_FD_TO_HANDLE:
2159 case XFS_IOC_PATH_TO_HANDLE:
2160 case XFS_IOC_PATH_TO_FSHANDLE: {
2161 xfs_fsop_handlereq_t hreq;
2163 if (copy_from_user(&hreq, arg, sizeof(hreq)))
2165 return xfs_find_handle(cmd, &hreq);
2167 case XFS_IOC_OPEN_BY_HANDLE: {
2168 xfs_fsop_handlereq_t hreq;
2170 if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
2172 return xfs_open_by_handle(filp, &hreq);
2174 case XFS_IOC_FSSETDM_BY_HANDLE:
2175 return xfs_fssetdm_by_handle(filp, arg);
2177 case XFS_IOC_READLINK_BY_HANDLE: {
2178 xfs_fsop_handlereq_t hreq;
2180 if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
2182 return xfs_readlink_by_handle(filp, &hreq);
2184 case XFS_IOC_ATTRLIST_BY_HANDLE:
2185 return xfs_attrlist_by_handle(filp, arg);
2187 case XFS_IOC_ATTRMULTI_BY_HANDLE:
2188 return xfs_attrmulti_by_handle(filp, arg);
2190 case XFS_IOC_SWAPEXT: {
2191 struct xfs_swapext sxp;
2193 if (copy_from_user(&sxp, arg, sizeof(xfs_swapext_t)))
2195 error = mnt_want_write_file(filp);
2198 error = xfs_ioc_swapext(&sxp);
2199 mnt_drop_write_file(filp);
2203 case XFS_IOC_FSCOUNTS: {
2204 xfs_fsop_counts_t out;
2206 xfs_fs_counts(mp, &out);
2208 if (copy_to_user(arg, &out, sizeof(out)))
2213 case XFS_IOC_SET_RESBLKS: {
2214 xfs_fsop_resblks_t inout;
2217 if (!capable(CAP_SYS_ADMIN))
2220 if (mp->m_flags & XFS_MOUNT_RDONLY)
2223 if (copy_from_user(&inout, arg, sizeof(inout)))
2226 error = mnt_want_write_file(filp);
2230 /* input parameter is passed in resblks field of structure */
2232 error = xfs_reserve_blocks(mp, &in, &inout);
2233 mnt_drop_write_file(filp);
2237 if (copy_to_user(arg, &inout, sizeof(inout)))
2242 case XFS_IOC_GET_RESBLKS: {
2243 xfs_fsop_resblks_t out;
2245 if (!capable(CAP_SYS_ADMIN))
2248 error = xfs_reserve_blocks(mp, NULL, &out);
2252 if (copy_to_user(arg, &out, sizeof(out)))
2258 case XFS_IOC_FSGROWFSDATA: {
2259 xfs_growfs_data_t in;
2261 if (copy_from_user(&in, arg, sizeof(in)))
2264 error = mnt_want_write_file(filp);
2267 error = xfs_growfs_data(mp, &in);
2268 mnt_drop_write_file(filp);
2272 case XFS_IOC_FSGROWFSLOG: {
2273 xfs_growfs_log_t in;
2275 if (copy_from_user(&in, arg, sizeof(in)))
2278 error = mnt_want_write_file(filp);
2281 error = xfs_growfs_log(mp, &in);
2282 mnt_drop_write_file(filp);
2286 case XFS_IOC_FSGROWFSRT: {
2289 if (copy_from_user(&in, arg, sizeof(in)))
2292 error = mnt_want_write_file(filp);
2295 error = xfs_growfs_rt(mp, &in);
2296 mnt_drop_write_file(filp);
2300 case XFS_IOC_GOINGDOWN: {
2303 if (!capable(CAP_SYS_ADMIN))
2306 if (get_user(in, (uint32_t __user *)arg))
2309 return xfs_fs_goingdown(mp, in);
2312 case XFS_IOC_ERROR_INJECTION: {
2313 xfs_error_injection_t in;
2315 if (!capable(CAP_SYS_ADMIN))
2318 if (copy_from_user(&in, arg, sizeof(in)))
2321 return xfs_errortag_add(mp, in.errtag);
2324 case XFS_IOC_ERROR_CLEARALL:
2325 if (!capable(CAP_SYS_ADMIN))
2328 return xfs_errortag_clearall(mp);
2330 case XFS_IOC_FREE_EOFBLOCKS: {
2331 struct xfs_fs_eofblocks eofb;
2332 struct xfs_eofblocks keofb;
2334 if (!capable(CAP_SYS_ADMIN))
2337 if (mp->m_flags & XFS_MOUNT_RDONLY)
2340 if (copy_from_user(&eofb, arg, sizeof(eofb)))
2343 error = xfs_fs_eofblocks_from_user(&eofb, &keofb);
2347 return xfs_icache_free_eofblocks(mp, &keofb);