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,
362 if (*len > XFS_XATTR_SIZE_MAX)
364 kbuf = kmem_zalloc_large(*len, 0);
368 namelen = strlen(name);
369 error = xfs_attr_get(XFS_I(inode), name, namelen, &kbuf, (int *)len,
374 if (copy_to_user(ubuf, kbuf, *len))
383 xfs_attrmulti_attr_set(
386 const unsigned char __user *ubuf,
394 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
396 if (len > XFS_XATTR_SIZE_MAX)
399 kbuf = memdup_user(ubuf, len);
401 return PTR_ERR(kbuf);
403 namelen = strlen(name);
404 error = xfs_attr_set(XFS_I(inode), name, namelen, kbuf, len, flags);
406 xfs_forget_acl(inode, name, flags);
412 xfs_attrmulti_attr_remove(
420 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
422 namelen = strlen(name);
423 error = xfs_attr_remove(XFS_I(inode), name, namelen, flags);
425 xfs_forget_acl(inode, name, flags);
430 xfs_attrmulti_by_handle(
431 struct file *parfilp,
435 xfs_attr_multiop_t *ops;
436 xfs_fsop_attrmulti_handlereq_t am_hreq;
437 struct dentry *dentry;
438 unsigned int i, size;
439 unsigned char *attr_name;
441 if (!capable(CAP_SYS_ADMIN))
443 if (copy_from_user(&am_hreq, arg, sizeof(xfs_fsop_attrmulti_handlereq_t)))
447 if (am_hreq.opcount >= INT_MAX / sizeof(xfs_attr_multiop_t))
450 dentry = xfs_handlereq_to_dentry(parfilp, &am_hreq.hreq);
452 return PTR_ERR(dentry);
455 size = am_hreq.opcount * sizeof(xfs_attr_multiop_t);
456 if (!size || size > 16 * PAGE_SIZE)
459 ops = memdup_user(am_hreq.ops, size);
461 error = PTR_ERR(ops);
466 attr_name = kmalloc(MAXNAMELEN, GFP_KERNEL);
471 for (i = 0; i < am_hreq.opcount; i++) {
472 if ((ops[i].am_flags & ATTR_ROOT) &&
473 (ops[i].am_flags & ATTR_SECURE)) {
474 ops[i].am_error = -EINVAL;
477 ops[i].am_flags &= ~ATTR_KERNEL_FLAGS;
479 ops[i].am_error = strncpy_from_user((char *)attr_name,
480 ops[i].am_attrname, MAXNAMELEN);
481 if (ops[i].am_error == 0 || ops[i].am_error == MAXNAMELEN)
483 if (ops[i].am_error < 0)
486 switch (ops[i].am_opcode) {
488 ops[i].am_error = xfs_attrmulti_attr_get(
489 d_inode(dentry), attr_name,
490 ops[i].am_attrvalue, &ops[i].am_length,
494 ops[i].am_error = mnt_want_write_file(parfilp);
497 ops[i].am_error = xfs_attrmulti_attr_set(
498 d_inode(dentry), attr_name,
499 ops[i].am_attrvalue, ops[i].am_length,
501 mnt_drop_write_file(parfilp);
504 ops[i].am_error = mnt_want_write_file(parfilp);
507 ops[i].am_error = xfs_attrmulti_attr_remove(
508 d_inode(dentry), attr_name,
510 mnt_drop_write_file(parfilp);
513 ops[i].am_error = -EINVAL;
517 if (copy_to_user(am_hreq.ops, ops, size))
533 struct inode *inode = file_inode(filp);
534 struct xfs_inode *ip = XFS_I(inode);
536 enum xfs_prealloc_flags flags = XFS_PREALLOC_CLEAR;
537 uint iolock = XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL;
540 if (inode->i_flags & (S_IMMUTABLE|S_APPEND))
543 if (!(filp->f_mode & FMODE_WRITE))
546 if (!S_ISREG(inode->i_mode))
549 if (xfs_is_always_cow_inode(ip))
552 if (filp->f_flags & O_DSYNC)
553 flags |= XFS_PREALLOC_SYNC;
554 if (filp->f_mode & FMODE_NOCMTIME)
555 flags |= XFS_PREALLOC_INVISIBLE;
557 error = mnt_want_write_file(filp);
561 xfs_ilock(ip, iolock);
562 error = xfs_break_layouts(inode, &iolock, BREAK_UNMAP);
565 inode_dio_wait(inode);
567 switch (bf->l_whence) {
571 bf->l_start += filp->f_pos;
574 bf->l_start += XFS_ISIZE(ip);
581 if (bf->l_start < 0 || bf->l_start > inode->i_sb->s_maxbytes) {
586 if (bf->l_start > XFS_ISIZE(ip)) {
587 error = xfs_alloc_file_space(ip, XFS_ISIZE(ip),
588 bf->l_start - XFS_ISIZE(ip), 0);
593 iattr.ia_valid = ATTR_SIZE;
594 iattr.ia_size = bf->l_start;
595 error = xfs_vn_setattr_size(file_dentry(filp), &iattr);
599 error = xfs_update_prealloc_flags(ip, flags);
602 xfs_iunlock(ip, iolock);
603 mnt_drop_write_file(filp);
607 /* Return 0 on success or positive error */
609 xfs_fsbulkstat_one_fmt(
610 struct xfs_ibulk *breq,
611 const struct xfs_bulkstat *bstat)
613 struct xfs_bstat bs1;
615 xfs_bulkstat_to_bstat(breq->mp, &bs1, bstat);
616 if (copy_to_user(breq->ubuffer, &bs1, sizeof(bs1)))
618 return xfs_ibulk_advance(breq, sizeof(struct xfs_bstat));
623 struct xfs_ibulk *breq,
624 const struct xfs_inumbers *igrp)
626 struct xfs_inogrp ig1;
628 xfs_inumbers_to_inogrp(&ig1, igrp);
629 if (copy_to_user(breq->ubuffer, &ig1, sizeof(struct xfs_inogrp)))
631 return xfs_ibulk_advance(breq, sizeof(struct xfs_inogrp));
640 struct xfs_fsop_bulkreq bulkreq;
641 struct xfs_ibulk breq = {
648 /* done = 1 if there are more stats to get and if bulkstat */
649 /* should be called again (unused here, but used in dmapi) */
651 if (!capable(CAP_SYS_ADMIN))
654 if (XFS_FORCED_SHUTDOWN(mp))
657 if (copy_from_user(&bulkreq, arg, sizeof(struct xfs_fsop_bulkreq)))
660 if (copy_from_user(&lastino, bulkreq.lastip, sizeof(__s64)))
663 if (bulkreq.icount <= 0)
666 if (bulkreq.ubuffer == NULL)
669 breq.ubuffer = bulkreq.ubuffer;
670 breq.icount = bulkreq.icount;
673 * FSBULKSTAT_SINGLE expects that *lastip contains the inode number
674 * that we want to stat. However, FSINUMBERS and FSBULKSTAT expect
675 * that *lastip contains either zero or the number of the last inode to
676 * be examined by the previous call and return results starting with
677 * the next inode after that. The new bulk request back end functions
678 * take the inode to start with, so we have to compute the startino
679 * parameter from lastino to maintain correct function. lastino == 0
680 * is a special case because it has traditionally meant "first inode
683 if (cmd == XFS_IOC_FSINUMBERS) {
684 breq.startino = lastino ? lastino + 1 : 0;
685 error = xfs_inumbers(&breq, xfs_fsinumbers_fmt);
686 lastino = breq.startino - 1;
687 } else if (cmd == XFS_IOC_FSBULKSTAT_SINGLE) {
688 breq.startino = lastino;
690 error = xfs_bulkstat_one(&breq, xfs_fsbulkstat_one_fmt);
691 } else { /* XFS_IOC_FSBULKSTAT */
692 breq.startino = lastino ? lastino + 1 : 0;
693 error = xfs_bulkstat(&breq, xfs_fsbulkstat_one_fmt);
694 lastino = breq.startino - 1;
700 if (bulkreq.lastip != NULL &&
701 copy_to_user(bulkreq.lastip, &lastino, sizeof(xfs_ino_t)))
704 if (bulkreq.ocount != NULL &&
705 copy_to_user(bulkreq.ocount, &breq.ocount, sizeof(__s32)))
711 /* Return 0 on success or positive error */
714 struct xfs_ibulk *breq,
715 const struct xfs_bulkstat *bstat)
717 if (copy_to_user(breq->ubuffer, bstat, sizeof(struct xfs_bulkstat)))
719 return xfs_ibulk_advance(breq, sizeof(struct xfs_bulkstat));
723 * Check the incoming bulk request @hdr from userspace and initialize the
724 * internal @breq bulk request appropriately. Returns 0 if the bulk request
725 * should proceed; -ECANCELED if there's nothing to do; or the usual
726 * negative error code.
730 struct xfs_mount *mp,
731 struct xfs_bulk_ireq *hdr,
732 struct xfs_ibulk *breq,
733 void __user *ubuffer)
735 if (hdr->icount == 0 ||
736 (hdr->flags & ~XFS_BULK_IREQ_FLAGS_ALL) ||
737 memchr_inv(hdr->reserved, 0, sizeof(hdr->reserved)))
740 breq->startino = hdr->ino;
741 breq->ubuffer = ubuffer;
742 breq->icount = hdr->icount;
747 * The @ino parameter is a special value, so we must look it up here.
748 * We're not allowed to have IREQ_AGNO, and we only return one inode
751 if (hdr->flags & XFS_BULK_IREQ_SPECIAL) {
752 if (hdr->flags & XFS_BULK_IREQ_AGNO)
756 case XFS_BULK_IREQ_SPECIAL_ROOT:
757 hdr->ino = mp->m_sb.sb_rootino;
766 * The IREQ_AGNO flag means that we only want results from a given AG.
767 * If @hdr->ino is zero, we start iterating in that AG. If @hdr->ino is
768 * beyond the specified AG then we return no results.
770 if (hdr->flags & XFS_BULK_IREQ_AGNO) {
771 if (hdr->agno >= mp->m_sb.sb_agcount)
774 if (breq->startino == 0)
775 breq->startino = XFS_AGINO_TO_INO(mp, hdr->agno, 0);
776 else if (XFS_INO_TO_AGNO(mp, breq->startino) < hdr->agno)
779 breq->flags |= XFS_IBULK_SAME_AG;
781 /* Asking for an inode past the end of the AG? We're done! */
782 if (XFS_INO_TO_AGNO(mp, breq->startino) > hdr->agno)
784 } else if (hdr->agno)
787 /* Asking for an inode past the end of the FS? We're done! */
788 if (XFS_INO_TO_AGNO(mp, breq->startino) >= mp->m_sb.sb_agcount)
795 * Update the userspace bulk request @hdr to reflect the end state of the
796 * internal bulk request @breq.
799 xfs_bulk_ireq_teardown(
800 struct xfs_bulk_ireq *hdr,
801 struct xfs_ibulk *breq)
803 hdr->ino = breq->startino;
804 hdr->ocount = breq->ocount;
807 /* Handle the v5 bulkstat ioctl. */
810 struct xfs_mount *mp,
812 struct xfs_bulkstat_req __user *arg)
814 struct xfs_bulk_ireq hdr;
815 struct xfs_ibulk breq = {
820 if (!capable(CAP_SYS_ADMIN))
823 if (XFS_FORCED_SHUTDOWN(mp))
826 if (copy_from_user(&hdr, &arg->hdr, sizeof(hdr)))
829 error = xfs_bulk_ireq_setup(mp, &hdr, &breq, arg->bulkstat);
830 if (error == -ECANCELED)
835 error = xfs_bulkstat(&breq, xfs_bulkstat_fmt);
840 xfs_bulk_ireq_teardown(&hdr, &breq);
841 if (copy_to_user(&arg->hdr, &hdr, sizeof(hdr)))
849 struct xfs_ibulk *breq,
850 const struct xfs_inumbers *igrp)
852 if (copy_to_user(breq->ubuffer, igrp, sizeof(struct xfs_inumbers)))
854 return xfs_ibulk_advance(breq, sizeof(struct xfs_inumbers));
857 /* Handle the v5 inumbers ioctl. */
860 struct xfs_mount *mp,
862 struct xfs_inumbers_req __user *arg)
864 struct xfs_bulk_ireq hdr;
865 struct xfs_ibulk breq = {
870 if (!capable(CAP_SYS_ADMIN))
873 if (XFS_FORCED_SHUTDOWN(mp))
876 if (copy_from_user(&hdr, &arg->hdr, sizeof(hdr)))
879 error = xfs_bulk_ireq_setup(mp, &hdr, &breq, arg->inumbers);
880 if (error == -ECANCELED)
885 error = xfs_inumbers(&breq, xfs_inumbers_fmt);
890 xfs_bulk_ireq_teardown(&hdr, &breq);
891 if (copy_to_user(&arg->hdr, &hdr, sizeof(hdr)))
899 struct xfs_mount *mp,
903 struct xfs_fsop_geom fsgeo;
906 xfs_fs_geometry(&mp->m_sb, &fsgeo, struct_version);
908 if (struct_version <= 3)
909 len = sizeof(struct xfs_fsop_geom_v1);
910 else if (struct_version == 4)
911 len = sizeof(struct xfs_fsop_geom_v4);
913 xfs_fsop_geom_health(mp, &fsgeo);
917 if (copy_to_user(arg, &fsgeo, len))
924 struct xfs_mount *mp,
927 struct xfs_ag_geometry ageo;
930 if (copy_from_user(&ageo, arg, sizeof(ageo)))
934 if (memchr_inv(&ageo.ag_reserved, 0, sizeof(ageo.ag_reserved)))
937 error = xfs_ag_get_geometry(mp, ageo.ag_number, &ageo);
941 if (copy_to_user(arg, &ageo, sizeof(ageo)))
947 * Linux extended inode flags interface.
951 xfs_merge_ioc_xflags(
955 unsigned int xflags = start;
957 if (flags & FS_IMMUTABLE_FL)
958 xflags |= FS_XFLAG_IMMUTABLE;
960 xflags &= ~FS_XFLAG_IMMUTABLE;
961 if (flags & FS_APPEND_FL)
962 xflags |= FS_XFLAG_APPEND;
964 xflags &= ~FS_XFLAG_APPEND;
965 if (flags & FS_SYNC_FL)
966 xflags |= FS_XFLAG_SYNC;
968 xflags &= ~FS_XFLAG_SYNC;
969 if (flags & FS_NOATIME_FL)
970 xflags |= FS_XFLAG_NOATIME;
972 xflags &= ~FS_XFLAG_NOATIME;
973 if (flags & FS_NODUMP_FL)
974 xflags |= FS_XFLAG_NODUMP;
976 xflags &= ~FS_XFLAG_NODUMP;
985 unsigned int flags = 0;
987 if (di_flags & XFS_DIFLAG_IMMUTABLE)
988 flags |= FS_IMMUTABLE_FL;
989 if (di_flags & XFS_DIFLAG_APPEND)
990 flags |= FS_APPEND_FL;
991 if (di_flags & XFS_DIFLAG_SYNC)
993 if (di_flags & XFS_DIFLAG_NOATIME)
994 flags |= FS_NOATIME_FL;
995 if (di_flags & XFS_DIFLAG_NODUMP)
996 flags |= FS_NODUMP_FL;
1002 struct xfs_inode *ip,
1006 simple_fill_fsxattr(fa, xfs_ip2xflags(ip));
1007 fa->fsx_extsize = ip->i_d.di_extsize << ip->i_mount->m_sb.sb_blocklog;
1008 fa->fsx_cowextsize = ip->i_d.di_cowextsize <<
1009 ip->i_mount->m_sb.sb_blocklog;
1010 fa->fsx_projid = ip->i_d.di_projid;
1014 if (ip->i_afp->if_flags & XFS_IFEXTENTS)
1015 fa->fsx_nextents = xfs_iext_count(ip->i_afp);
1017 fa->fsx_nextents = ip->i_d.di_anextents;
1019 fa->fsx_nextents = 0;
1021 if (ip->i_df.if_flags & XFS_IFEXTENTS)
1022 fa->fsx_nextents = xfs_iext_count(&ip->i_df);
1024 fa->fsx_nextents = ip->i_d.di_nextents;
1036 xfs_ilock(ip, XFS_ILOCK_SHARED);
1037 xfs_fill_fsxattr(ip, attr, &fa);
1038 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1040 if (copy_to_user(arg, &fa, sizeof(fa)))
1047 struct xfs_inode *ip,
1048 unsigned int xflags)
1050 /* can't set PREALLOC this way, just preserve it */
1052 (ip->i_d.di_flags & XFS_DIFLAG_PREALLOC);
1054 if (xflags & FS_XFLAG_IMMUTABLE)
1055 di_flags |= XFS_DIFLAG_IMMUTABLE;
1056 if (xflags & FS_XFLAG_APPEND)
1057 di_flags |= XFS_DIFLAG_APPEND;
1058 if (xflags & FS_XFLAG_SYNC)
1059 di_flags |= XFS_DIFLAG_SYNC;
1060 if (xflags & FS_XFLAG_NOATIME)
1061 di_flags |= XFS_DIFLAG_NOATIME;
1062 if (xflags & FS_XFLAG_NODUMP)
1063 di_flags |= XFS_DIFLAG_NODUMP;
1064 if (xflags & FS_XFLAG_NODEFRAG)
1065 di_flags |= XFS_DIFLAG_NODEFRAG;
1066 if (xflags & FS_XFLAG_FILESTREAM)
1067 di_flags |= XFS_DIFLAG_FILESTREAM;
1068 if (S_ISDIR(VFS_I(ip)->i_mode)) {
1069 if (xflags & FS_XFLAG_RTINHERIT)
1070 di_flags |= XFS_DIFLAG_RTINHERIT;
1071 if (xflags & FS_XFLAG_NOSYMLINKS)
1072 di_flags |= XFS_DIFLAG_NOSYMLINKS;
1073 if (xflags & FS_XFLAG_EXTSZINHERIT)
1074 di_flags |= XFS_DIFLAG_EXTSZINHERIT;
1075 if (xflags & FS_XFLAG_PROJINHERIT)
1076 di_flags |= XFS_DIFLAG_PROJINHERIT;
1077 } else if (S_ISREG(VFS_I(ip)->i_mode)) {
1078 if (xflags & FS_XFLAG_REALTIME)
1079 di_flags |= XFS_DIFLAG_REALTIME;
1080 if (xflags & FS_XFLAG_EXTSIZE)
1081 di_flags |= XFS_DIFLAG_EXTSIZE;
1089 struct xfs_inode *ip,
1090 unsigned int xflags)
1092 uint64_t di_flags2 =
1093 (ip->i_d.di_flags2 & XFS_DIFLAG2_REFLINK);
1095 if (xflags & FS_XFLAG_DAX)
1096 di_flags2 |= XFS_DIFLAG2_DAX;
1097 if (xflags & FS_XFLAG_COWEXTSIZE)
1098 di_flags2 |= XFS_DIFLAG2_COWEXTSIZE;
1104 xfs_diflags_to_linux(
1105 struct xfs_inode *ip)
1107 struct inode *inode = VFS_I(ip);
1108 unsigned int xflags = xfs_ip2xflags(ip);
1110 if (xflags & FS_XFLAG_IMMUTABLE)
1111 inode->i_flags |= S_IMMUTABLE;
1113 inode->i_flags &= ~S_IMMUTABLE;
1114 if (xflags & FS_XFLAG_APPEND)
1115 inode->i_flags |= S_APPEND;
1117 inode->i_flags &= ~S_APPEND;
1118 if (xflags & FS_XFLAG_SYNC)
1119 inode->i_flags |= S_SYNC;
1121 inode->i_flags &= ~S_SYNC;
1122 if (xflags & FS_XFLAG_NOATIME)
1123 inode->i_flags |= S_NOATIME;
1125 inode->i_flags &= ~S_NOATIME;
1126 #if 0 /* disabled until the flag switching races are sorted out */
1127 if (xflags & FS_XFLAG_DAX)
1128 inode->i_flags |= S_DAX;
1130 inode->i_flags &= ~S_DAX;
1135 xfs_ioctl_setattr_xflags(
1136 struct xfs_trans *tp,
1137 struct xfs_inode *ip,
1140 struct xfs_mount *mp = ip->i_mount;
1143 /* Can't change realtime flag if any extents are allocated. */
1144 if ((ip->i_d.di_nextents || ip->i_delayed_blks) &&
1145 XFS_IS_REALTIME_INODE(ip) != (fa->fsx_xflags & FS_XFLAG_REALTIME))
1148 /* If realtime flag is set then must have realtime device */
1149 if (fa->fsx_xflags & FS_XFLAG_REALTIME) {
1150 if (mp->m_sb.sb_rblocks == 0 || mp->m_sb.sb_rextsize == 0 ||
1151 (ip->i_d.di_extsize % mp->m_sb.sb_rextsize))
1155 /* Clear reflink if we are actually able to set the rt flag. */
1156 if ((fa->fsx_xflags & FS_XFLAG_REALTIME) && xfs_is_reflink_inode(ip))
1157 ip->i_d.di_flags2 &= ~XFS_DIFLAG2_REFLINK;
1159 /* Don't allow us to set DAX mode for a reflinked file for now. */
1160 if ((fa->fsx_xflags & FS_XFLAG_DAX) && xfs_is_reflink_inode(ip))
1163 /* diflags2 only valid for v3 inodes. */
1164 di_flags2 = xfs_flags2diflags2(ip, fa->fsx_xflags);
1165 if (di_flags2 && ip->i_d.di_version < 3)
1168 ip->i_d.di_flags = xfs_flags2diflags(ip, fa->fsx_xflags);
1169 ip->i_d.di_flags2 = di_flags2;
1171 xfs_diflags_to_linux(ip);
1172 xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
1173 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1174 XFS_STATS_INC(mp, xs_ig_attrchg);
1179 * If we are changing DAX flags, we have to ensure the file is clean and any
1180 * cached objects in the address space are invalidated and removed. This
1181 * requires us to lock out other IO and page faults similar to a truncate
1182 * operation. The locks need to be held until the transaction has been committed
1183 * so that the cache invalidation is atomic with respect to the DAX flag
1187 xfs_ioctl_setattr_dax_invalidate(
1188 struct xfs_inode *ip,
1192 struct inode *inode = VFS_I(ip);
1193 struct super_block *sb = inode->i_sb;
1199 * It is only valid to set the DAX flag on regular files and
1200 * directories on filesystems where the block size is equal to the page
1201 * size. On directories it serves as an inherited hint so we don't
1202 * have to check the device for dax support or flush pagecache.
1204 if (fa->fsx_xflags & FS_XFLAG_DAX) {
1205 struct xfs_buftarg *target = xfs_inode_buftarg(ip);
1207 if (!bdev_dax_supported(target->bt_bdev, sb->s_blocksize))
1211 /* If the DAX state is not changing, we have nothing to do here. */
1212 if ((fa->fsx_xflags & FS_XFLAG_DAX) && IS_DAX(inode))
1214 if (!(fa->fsx_xflags & FS_XFLAG_DAX) && !IS_DAX(inode))
1217 if (S_ISDIR(inode->i_mode))
1220 /* lock, flush and invalidate mapping in preparation for flag change */
1221 xfs_ilock(ip, XFS_MMAPLOCK_EXCL | XFS_IOLOCK_EXCL);
1222 error = filemap_write_and_wait(inode->i_mapping);
1225 error = invalidate_inode_pages2(inode->i_mapping);
1229 *join_flags = XFS_MMAPLOCK_EXCL | XFS_IOLOCK_EXCL;
1233 xfs_iunlock(ip, XFS_MMAPLOCK_EXCL | XFS_IOLOCK_EXCL);
1239 * Set up the transaction structure for the setattr operation, checking that we
1240 * have permission to do so. On success, return a clean transaction and the
1241 * inode locked exclusively ready for further operation specific checks. On
1242 * failure, return an error without modifying or locking the inode.
1244 * The inode might already be IO locked on call. If this is the case, it is
1245 * indicated in @join_flags and we take full responsibility for ensuring they
1246 * are unlocked from now on. Hence if we have an error here, we still have to
1247 * unlock them. Otherwise, once they are joined to the transaction, they will
1248 * be unlocked on commit/cancel.
1250 static struct xfs_trans *
1251 xfs_ioctl_setattr_get_trans(
1252 struct xfs_inode *ip,
1255 struct xfs_mount *mp = ip->i_mount;
1256 struct xfs_trans *tp;
1259 if (mp->m_flags & XFS_MOUNT_RDONLY)
1262 if (XFS_FORCED_SHUTDOWN(mp))
1265 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
1269 xfs_ilock(ip, XFS_ILOCK_EXCL);
1270 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | join_flags);
1274 * CAP_FOWNER overrides the following restrictions:
1276 * The user ID of the calling process must be equal to the file owner
1277 * ID, except in cases where the CAP_FSETID capability is applicable.
1279 if (!inode_owner_or_capable(VFS_I(ip))) {
1284 if (mp->m_flags & XFS_MOUNT_WSYNC)
1285 xfs_trans_set_sync(tp);
1290 xfs_trans_cancel(tp);
1293 xfs_iunlock(ip, join_flags);
1294 return ERR_PTR(error);
1298 * extent size hint validation is somewhat cumbersome. Rules are:
1300 * 1. extent size hint is only valid for directories and regular files
1301 * 2. FS_XFLAG_EXTSIZE is only valid for regular files
1302 * 3. FS_XFLAG_EXTSZINHERIT is only valid for directories.
1303 * 4. can only be changed on regular files if no extents are allocated
1304 * 5. can be changed on directories at any time
1305 * 6. extsize hint of 0 turns off hints, clears inode flags.
1306 * 7. Extent size must be a multiple of the appropriate block size.
1307 * 8. for non-realtime files, the extent size hint must be limited
1308 * to half the AG size to avoid alignment extending the extent beyond the
1311 * Please keep this function in sync with xfs_scrub_inode_extsize.
1314 xfs_ioctl_setattr_check_extsize(
1315 struct xfs_inode *ip,
1318 struct xfs_mount *mp = ip->i_mount;
1320 xfs_fsblock_t extsize_fsb;
1322 if (S_ISREG(VFS_I(ip)->i_mode) && ip->i_d.di_nextents &&
1323 ((ip->i_d.di_extsize << mp->m_sb.sb_blocklog) != fa->fsx_extsize))
1326 if (fa->fsx_extsize == 0)
1329 extsize_fsb = XFS_B_TO_FSB(mp, fa->fsx_extsize);
1330 if (extsize_fsb > MAXEXTLEN)
1333 if (XFS_IS_REALTIME_INODE(ip) ||
1334 (fa->fsx_xflags & FS_XFLAG_REALTIME)) {
1335 size = mp->m_sb.sb_rextsize << mp->m_sb.sb_blocklog;
1337 size = mp->m_sb.sb_blocksize;
1338 if (extsize_fsb > mp->m_sb.sb_agblocks / 2)
1342 if (fa->fsx_extsize % size)
1349 * CoW extent size hint validation rules are:
1351 * 1. CoW extent size hint can only be set if reflink is enabled on the fs.
1352 * The inode does not have to have any shared blocks, but it must be a v3.
1353 * 2. FS_XFLAG_COWEXTSIZE is only valid for directories and regular files;
1354 * for a directory, the hint is propagated to new files.
1355 * 3. Can be changed on files & directories at any time.
1356 * 4. CoW extsize hint of 0 turns off hints, clears inode flags.
1357 * 5. Extent size must be a multiple of the appropriate block size.
1358 * 6. The extent size hint must be limited to half the AG size to avoid
1359 * alignment extending the extent beyond the limits of the AG.
1361 * Please keep this function in sync with xfs_scrub_inode_cowextsize.
1364 xfs_ioctl_setattr_check_cowextsize(
1365 struct xfs_inode *ip,
1368 struct xfs_mount *mp = ip->i_mount;
1370 xfs_fsblock_t cowextsize_fsb;
1372 if (!(fa->fsx_xflags & FS_XFLAG_COWEXTSIZE))
1375 if (!xfs_sb_version_hasreflink(&ip->i_mount->m_sb) ||
1376 ip->i_d.di_version != 3)
1379 if (fa->fsx_cowextsize == 0)
1382 cowextsize_fsb = XFS_B_TO_FSB(mp, fa->fsx_cowextsize);
1383 if (cowextsize_fsb > MAXEXTLEN)
1386 size = mp->m_sb.sb_blocksize;
1387 if (cowextsize_fsb > mp->m_sb.sb_agblocks / 2)
1390 if (fa->fsx_cowextsize % size)
1397 xfs_ioctl_setattr_check_projid(
1398 struct xfs_inode *ip,
1401 /* Disallow 32bit project ids if projid32bit feature is not enabled. */
1402 if (fa->fsx_projid > (uint16_t)-1 &&
1403 !xfs_sb_version_hasprojid32bit(&ip->i_mount->m_sb))
1413 struct fsxattr old_fa;
1414 struct xfs_mount *mp = ip->i_mount;
1415 struct xfs_trans *tp;
1416 struct xfs_dquot *udqp = NULL;
1417 struct xfs_dquot *pdqp = NULL;
1418 struct xfs_dquot *olddquot = NULL;
1422 trace_xfs_ioctl_setattr(ip);
1424 code = xfs_ioctl_setattr_check_projid(ip, fa);
1429 * If disk quotas is on, we make sure that the dquots do exist on disk,
1430 * before we start any other transactions. Trying to do this later
1431 * is messy. We don't care to take a readlock to look at the ids
1432 * in inode here, because we can't hold it across the trans_reserve.
1433 * If the IDs do change before we take the ilock, we're covered
1434 * because the i_*dquot fields will get updated anyway.
1436 if (XFS_IS_QUOTA_ON(mp)) {
1437 code = xfs_qm_vop_dqalloc(ip, ip->i_d.di_uid,
1438 ip->i_d.di_gid, fa->fsx_projid,
1439 XFS_QMOPT_PQUOTA, &udqp, NULL, &pdqp);
1445 * Changing DAX config may require inode locking for mapping
1446 * invalidation. These need to be held all the way to transaction commit
1447 * or cancel time, so need to be passed through to
1448 * xfs_ioctl_setattr_get_trans() so it can apply them to the join call
1451 code = xfs_ioctl_setattr_dax_invalidate(ip, fa, &join_flags);
1453 goto error_free_dquots;
1455 tp = xfs_ioctl_setattr_get_trans(ip, join_flags);
1458 goto error_free_dquots;
1461 if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_PQUOTA_ON(mp) &&
1462 ip->i_d.di_projid != fa->fsx_projid) {
1463 code = xfs_qm_vop_chown_reserve(tp, ip, udqp, NULL, pdqp,
1464 capable(CAP_FOWNER) ? XFS_QMOPT_FORCE_RES : 0);
1465 if (code) /* out of quota */
1466 goto error_trans_cancel;
1469 xfs_fill_fsxattr(ip, false, &old_fa);
1470 code = vfs_ioc_fssetxattr_check(VFS_I(ip), &old_fa, fa);
1472 goto error_trans_cancel;
1474 code = xfs_ioctl_setattr_check_extsize(ip, fa);
1476 goto error_trans_cancel;
1478 code = xfs_ioctl_setattr_check_cowextsize(ip, fa);
1480 goto error_trans_cancel;
1482 code = xfs_ioctl_setattr_xflags(tp, ip, fa);
1484 goto error_trans_cancel;
1487 * Change file ownership. Must be the owner or privileged. CAP_FSETID
1488 * overrides the following restrictions:
1490 * The set-user-ID and set-group-ID bits of a file will be cleared upon
1491 * successful return from chown()
1494 if ((VFS_I(ip)->i_mode & (S_ISUID|S_ISGID)) &&
1495 !capable_wrt_inode_uidgid(VFS_I(ip), CAP_FSETID))
1496 VFS_I(ip)->i_mode &= ~(S_ISUID|S_ISGID);
1498 /* Change the ownerships and register project quota modifications */
1499 if (ip->i_d.di_projid != fa->fsx_projid) {
1500 if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_PQUOTA_ON(mp)) {
1501 olddquot = xfs_qm_vop_chown(tp, ip,
1502 &ip->i_pdquot, pdqp);
1504 ASSERT(ip->i_d.di_version > 1);
1505 ip->i_d.di_projid = fa->fsx_projid;
1509 * Only set the extent size hint if we've already determined that the
1510 * extent size hint should be set on the inode. If no extent size flags
1511 * are set on the inode then unconditionally clear the extent size hint.
1513 if (ip->i_d.di_flags & (XFS_DIFLAG_EXTSIZE | XFS_DIFLAG_EXTSZINHERIT))
1514 ip->i_d.di_extsize = fa->fsx_extsize >> mp->m_sb.sb_blocklog;
1516 ip->i_d.di_extsize = 0;
1517 if (ip->i_d.di_version == 3 &&
1518 (ip->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE))
1519 ip->i_d.di_cowextsize = fa->fsx_cowextsize >>
1520 mp->m_sb.sb_blocklog;
1522 ip->i_d.di_cowextsize = 0;
1524 code = xfs_trans_commit(tp);
1527 * Release any dquot(s) the inode had kept before chown.
1529 xfs_qm_dqrele(olddquot);
1530 xfs_qm_dqrele(udqp);
1531 xfs_qm_dqrele(pdqp);
1536 xfs_trans_cancel(tp);
1538 xfs_qm_dqrele(udqp);
1539 xfs_qm_dqrele(pdqp);
1552 if (copy_from_user(&fa, arg, sizeof(fa)))
1555 error = mnt_want_write_file(filp);
1558 error = xfs_ioctl_setattr(ip, &fa);
1559 mnt_drop_write_file(filp);
1570 flags = xfs_di2lxflags(ip->i_d.di_flags);
1571 if (copy_to_user(arg, &flags, sizeof(flags)))
1578 struct xfs_inode *ip,
1582 struct xfs_trans *tp;
1584 struct fsxattr old_fa;
1589 if (copy_from_user(&flags, arg, sizeof(flags)))
1592 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
1593 FS_NOATIME_FL | FS_NODUMP_FL | \
1597 fa.fsx_xflags = xfs_merge_ioc_xflags(flags, xfs_ip2xflags(ip));
1599 error = mnt_want_write_file(filp);
1604 * Changing DAX config may require inode locking for mapping
1605 * invalidation. These need to be held all the way to transaction commit
1606 * or cancel time, so need to be passed through to
1607 * xfs_ioctl_setattr_get_trans() so it can apply them to the join call
1610 error = xfs_ioctl_setattr_dax_invalidate(ip, &fa, &join_flags);
1612 goto out_drop_write;
1614 tp = xfs_ioctl_setattr_get_trans(ip, join_flags);
1616 error = PTR_ERR(tp);
1617 goto out_drop_write;
1620 xfs_fill_fsxattr(ip, false, &old_fa);
1621 error = vfs_ioc_fssetxattr_check(VFS_I(ip), &old_fa, &fa);
1623 xfs_trans_cancel(tp);
1624 goto out_drop_write;
1627 error = xfs_ioctl_setattr_xflags(tp, ip, &fa);
1629 xfs_trans_cancel(tp);
1630 goto out_drop_write;
1633 error = xfs_trans_commit(tp);
1635 mnt_drop_write_file(filp);
1642 struct getbmapx __user *u,
1645 if (put_user(p->bmv_offset, &u->bmv_offset) ||
1646 put_user(p->bmv_block, &u->bmv_block) ||
1647 put_user(p->bmv_length, &u->bmv_length) ||
1648 put_user(0, &u->bmv_count) ||
1649 put_user(0, &u->bmv_entries))
1651 if (recsize < sizeof(struct getbmapx))
1653 if (put_user(0, &u->bmv_iflags) ||
1654 put_user(p->bmv_oflags, &u->bmv_oflags) ||
1655 put_user(0, &u->bmv_unused1) ||
1656 put_user(0, &u->bmv_unused2))
1667 struct getbmapx bmx = { 0 };
1668 struct kgetbmap *buf;
1673 case XFS_IOC_GETBMAPA:
1674 bmx.bmv_iflags = BMV_IF_ATTRFORK;
1676 case XFS_IOC_GETBMAP:
1677 if (file->f_mode & FMODE_NOCMTIME)
1678 bmx.bmv_iflags |= BMV_IF_NO_DMAPI_READ;
1679 /* struct getbmap is a strict subset of struct getbmapx. */
1680 recsize = sizeof(struct getbmap);
1682 case XFS_IOC_GETBMAPX:
1683 recsize = sizeof(struct getbmapx);
1689 if (copy_from_user(&bmx, arg, recsize))
1692 if (bmx.bmv_count < 2)
1694 if (bmx.bmv_count > ULONG_MAX / recsize)
1697 buf = kmem_zalloc_large(bmx.bmv_count * sizeof(*buf), 0);
1701 error = xfs_getbmap(XFS_I(file_inode(file)), &bmx, buf);
1706 if (copy_to_user(arg, &bmx, recsize))
1710 for (i = 0; i < bmx.bmv_entries; i++) {
1711 if (!xfs_getbmap_format(buf + i, arg, recsize))
1722 struct getfsmap_info {
1723 struct xfs_mount *mp;
1724 struct fsmap_head __user *data;
1730 xfs_getfsmap_format(struct xfs_fsmap *xfm, void *priv)
1732 struct getfsmap_info *info = priv;
1735 trace_xfs_getfsmap_mapping(info->mp, xfm);
1737 info->last_flags = xfm->fmr_flags;
1738 xfs_fsmap_from_internal(&fm, xfm);
1739 if (copy_to_user(&info->data->fmh_recs[info->idx++], &fm,
1740 sizeof(struct fsmap)))
1748 struct xfs_inode *ip,
1749 struct fsmap_head __user *arg)
1751 struct getfsmap_info info = { NULL };
1752 struct xfs_fsmap_head xhead = {0};
1753 struct fsmap_head head;
1754 bool aborted = false;
1757 if (copy_from_user(&head, arg, sizeof(struct fsmap_head)))
1759 if (memchr_inv(head.fmh_reserved, 0, sizeof(head.fmh_reserved)) ||
1760 memchr_inv(head.fmh_keys[0].fmr_reserved, 0,
1761 sizeof(head.fmh_keys[0].fmr_reserved)) ||
1762 memchr_inv(head.fmh_keys[1].fmr_reserved, 0,
1763 sizeof(head.fmh_keys[1].fmr_reserved)))
1766 xhead.fmh_iflags = head.fmh_iflags;
1767 xhead.fmh_count = head.fmh_count;
1768 xfs_fsmap_to_internal(&xhead.fmh_keys[0], &head.fmh_keys[0]);
1769 xfs_fsmap_to_internal(&xhead.fmh_keys[1], &head.fmh_keys[1]);
1771 trace_xfs_getfsmap_low_key(ip->i_mount, &xhead.fmh_keys[0]);
1772 trace_xfs_getfsmap_high_key(ip->i_mount, &xhead.fmh_keys[1]);
1774 info.mp = ip->i_mount;
1776 error = xfs_getfsmap(ip->i_mount, &xhead, xfs_getfsmap_format, &info);
1777 if (error == -ECANCELED) {
1783 /* If we didn't abort, set the "last" flag in the last fmx */
1784 if (!aborted && info.idx) {
1785 info.last_flags |= FMR_OF_LAST;
1786 if (copy_to_user(&info.data->fmh_recs[info.idx - 1].fmr_flags,
1787 &info.last_flags, sizeof(info.last_flags)))
1791 /* copy back header */
1792 head.fmh_entries = xhead.fmh_entries;
1793 head.fmh_oflags = xhead.fmh_oflags;
1794 if (copy_to_user(arg, &head, sizeof(struct fsmap_head)))
1801 xfs_ioc_scrub_metadata(
1802 struct xfs_inode *ip,
1805 struct xfs_scrub_metadata scrub;
1808 if (!capable(CAP_SYS_ADMIN))
1811 if (copy_from_user(&scrub, arg, sizeof(scrub)))
1814 error = xfs_scrub_metadata(ip, &scrub);
1818 if (copy_to_user(arg, &scrub, sizeof(scrub)))
1828 xfs_inode_t *ip, *tip;
1832 /* Pull information for the target fd */
1833 f = fdget((int)sxp->sx_fdtarget);
1839 if (!(f.file->f_mode & FMODE_WRITE) ||
1840 !(f.file->f_mode & FMODE_READ) ||
1841 (f.file->f_flags & O_APPEND)) {
1846 tmp = fdget((int)sxp->sx_fdtmp);
1852 if (!(tmp.file->f_mode & FMODE_WRITE) ||
1853 !(tmp.file->f_mode & FMODE_READ) ||
1854 (tmp.file->f_flags & O_APPEND)) {
1856 goto out_put_tmp_file;
1859 if (IS_SWAPFILE(file_inode(f.file)) ||
1860 IS_SWAPFILE(file_inode(tmp.file))) {
1862 goto out_put_tmp_file;
1866 * We need to ensure that the fds passed in point to XFS inodes
1867 * before we cast and access them as XFS structures as we have no
1868 * control over what the user passes us here.
1870 if (f.file->f_op != &xfs_file_operations ||
1871 tmp.file->f_op != &xfs_file_operations) {
1873 goto out_put_tmp_file;
1876 ip = XFS_I(file_inode(f.file));
1877 tip = XFS_I(file_inode(tmp.file));
1879 if (ip->i_mount != tip->i_mount) {
1881 goto out_put_tmp_file;
1884 if (ip->i_ino == tip->i_ino) {
1886 goto out_put_tmp_file;
1889 if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
1891 goto out_put_tmp_file;
1894 error = xfs_swap_extents(ip, tip, sxp);
1906 struct xfs_mount *mp,
1907 char __user *user_label)
1909 struct xfs_sb *sbp = &mp->m_sb;
1910 char label[XFSLABEL_MAX + 1];
1913 BUILD_BUG_ON(sizeof(sbp->sb_fname) > FSLABEL_MAX);
1915 /* 1 larger than sb_fname, so this ensures a trailing NUL char */
1916 memset(label, 0, sizeof(label));
1917 spin_lock(&mp->m_sb_lock);
1918 strncpy(label, sbp->sb_fname, XFSLABEL_MAX);
1919 spin_unlock(&mp->m_sb_lock);
1921 if (copy_to_user(user_label, label, sizeof(label)))
1929 struct xfs_mount *mp,
1930 char __user *newlabel)
1932 struct xfs_sb *sbp = &mp->m_sb;
1933 char label[XFSLABEL_MAX + 1];
1937 if (!capable(CAP_SYS_ADMIN))
1940 * The generic ioctl allows up to FSLABEL_MAX chars, but XFS is much
1941 * smaller, at 12 bytes. We copy one more to be sure we find the
1942 * (required) NULL character to test the incoming label length.
1943 * NB: The on disk label doesn't need to be null terminated.
1945 if (copy_from_user(label, newlabel, XFSLABEL_MAX + 1))
1947 len = strnlen(label, XFSLABEL_MAX + 1);
1948 if (len > sizeof(sbp->sb_fname))
1951 error = mnt_want_write_file(filp);
1955 spin_lock(&mp->m_sb_lock);
1956 memset(sbp->sb_fname, 0, sizeof(sbp->sb_fname));
1957 memcpy(sbp->sb_fname, label, len);
1958 spin_unlock(&mp->m_sb_lock);
1961 * Now we do several things to satisfy userspace.
1962 * In addition to normal logging of the primary superblock, we also
1963 * immediately write these changes to sector zero for the primary, then
1964 * update all backup supers (as xfs_db does for a label change), then
1965 * invalidate the block device page cache. This is so that any prior
1966 * buffered reads from userspace (i.e. from blkid) are invalidated,
1967 * and userspace will see the newly-written label.
1969 error = xfs_sync_sb_buf(mp);
1973 * growfs also updates backup supers so lock against that.
1975 mutex_lock(&mp->m_growlock);
1976 error = xfs_update_secondary_sbs(mp);
1977 mutex_unlock(&mp->m_growlock);
1979 invalidate_bdev(mp->m_ddev_targp->bt_bdev);
1982 mnt_drop_write_file(filp);
1987 * Note: some of the ioctl's return positive numbers as a
1988 * byte count indicating success, such as readlink_by_handle.
1989 * So we don't "sign flip" like most other routines. This means
1990 * true errors need to be returned as a negative value.
1998 struct inode *inode = file_inode(filp);
1999 struct xfs_inode *ip = XFS_I(inode);
2000 struct xfs_mount *mp = ip->i_mount;
2001 void __user *arg = (void __user *)p;
2004 trace_xfs_file_ioctl(ip);
2008 return xfs_ioc_trim(mp, arg);
2009 case FS_IOC_GETFSLABEL:
2010 return xfs_ioc_getlabel(mp, arg);
2011 case FS_IOC_SETFSLABEL:
2012 return xfs_ioc_setlabel(filp, mp, arg);
2013 case XFS_IOC_ALLOCSP:
2014 case XFS_IOC_FREESP:
2015 case XFS_IOC_ALLOCSP64:
2016 case XFS_IOC_FREESP64: {
2019 if (copy_from_user(&bf, arg, sizeof(bf)))
2021 return xfs_ioc_space(filp, &bf);
2023 case XFS_IOC_DIOINFO: {
2024 struct xfs_buftarg *target = xfs_inode_buftarg(ip);
2027 da.d_mem = da.d_miniosz = target->bt_logical_sectorsize;
2028 da.d_maxiosz = INT_MAX & ~(da.d_miniosz - 1);
2030 if (copy_to_user(arg, &da, sizeof(da)))
2035 case XFS_IOC_FSBULKSTAT_SINGLE:
2036 case XFS_IOC_FSBULKSTAT:
2037 case XFS_IOC_FSINUMBERS:
2038 return xfs_ioc_fsbulkstat(mp, cmd, arg);
2040 case XFS_IOC_BULKSTAT:
2041 return xfs_ioc_bulkstat(mp, cmd, arg);
2042 case XFS_IOC_INUMBERS:
2043 return xfs_ioc_inumbers(mp, cmd, arg);
2045 case XFS_IOC_FSGEOMETRY_V1:
2046 return xfs_ioc_fsgeometry(mp, arg, 3);
2047 case XFS_IOC_FSGEOMETRY_V4:
2048 return xfs_ioc_fsgeometry(mp, arg, 4);
2049 case XFS_IOC_FSGEOMETRY:
2050 return xfs_ioc_fsgeometry(mp, arg, 5);
2052 case XFS_IOC_AG_GEOMETRY:
2053 return xfs_ioc_ag_geometry(mp, arg);
2055 case XFS_IOC_GETVERSION:
2056 return put_user(inode->i_generation, (int __user *)arg);
2058 case XFS_IOC_FSGETXATTR:
2059 return xfs_ioc_fsgetxattr(ip, 0, arg);
2060 case XFS_IOC_FSGETXATTRA:
2061 return xfs_ioc_fsgetxattr(ip, 1, arg);
2062 case XFS_IOC_FSSETXATTR:
2063 return xfs_ioc_fssetxattr(ip, filp, arg);
2064 case XFS_IOC_GETXFLAGS:
2065 return xfs_ioc_getxflags(ip, arg);
2066 case XFS_IOC_SETXFLAGS:
2067 return xfs_ioc_setxflags(ip, filp, arg);
2069 case XFS_IOC_GETBMAP:
2070 case XFS_IOC_GETBMAPA:
2071 case XFS_IOC_GETBMAPX:
2072 return xfs_ioc_getbmap(filp, cmd, arg);
2074 case FS_IOC_GETFSMAP:
2075 return xfs_ioc_getfsmap(ip, arg);
2077 case XFS_IOC_SCRUB_METADATA:
2078 return xfs_ioc_scrub_metadata(ip, arg);
2080 case XFS_IOC_FD_TO_HANDLE:
2081 case XFS_IOC_PATH_TO_HANDLE:
2082 case XFS_IOC_PATH_TO_FSHANDLE: {
2083 xfs_fsop_handlereq_t hreq;
2085 if (copy_from_user(&hreq, arg, sizeof(hreq)))
2087 return xfs_find_handle(cmd, &hreq);
2089 case XFS_IOC_OPEN_BY_HANDLE: {
2090 xfs_fsop_handlereq_t hreq;
2092 if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
2094 return xfs_open_by_handle(filp, &hreq);
2097 case XFS_IOC_READLINK_BY_HANDLE: {
2098 xfs_fsop_handlereq_t hreq;
2100 if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
2102 return xfs_readlink_by_handle(filp, &hreq);
2104 case XFS_IOC_ATTRLIST_BY_HANDLE:
2105 return xfs_attrlist_by_handle(filp, arg);
2107 case XFS_IOC_ATTRMULTI_BY_HANDLE:
2108 return xfs_attrmulti_by_handle(filp, arg);
2110 case XFS_IOC_SWAPEXT: {
2111 struct xfs_swapext sxp;
2113 if (copy_from_user(&sxp, arg, sizeof(xfs_swapext_t)))
2115 error = mnt_want_write_file(filp);
2118 error = xfs_ioc_swapext(&sxp);
2119 mnt_drop_write_file(filp);
2123 case XFS_IOC_FSCOUNTS: {
2124 xfs_fsop_counts_t out;
2126 xfs_fs_counts(mp, &out);
2128 if (copy_to_user(arg, &out, sizeof(out)))
2133 case XFS_IOC_SET_RESBLKS: {
2134 xfs_fsop_resblks_t inout;
2137 if (!capable(CAP_SYS_ADMIN))
2140 if (mp->m_flags & XFS_MOUNT_RDONLY)
2143 if (copy_from_user(&inout, arg, sizeof(inout)))
2146 error = mnt_want_write_file(filp);
2150 /* input parameter is passed in resblks field of structure */
2152 error = xfs_reserve_blocks(mp, &in, &inout);
2153 mnt_drop_write_file(filp);
2157 if (copy_to_user(arg, &inout, sizeof(inout)))
2162 case XFS_IOC_GET_RESBLKS: {
2163 xfs_fsop_resblks_t out;
2165 if (!capable(CAP_SYS_ADMIN))
2168 error = xfs_reserve_blocks(mp, NULL, &out);
2172 if (copy_to_user(arg, &out, sizeof(out)))
2178 case XFS_IOC_FSGROWFSDATA: {
2179 xfs_growfs_data_t in;
2181 if (copy_from_user(&in, arg, sizeof(in)))
2184 error = mnt_want_write_file(filp);
2187 error = xfs_growfs_data(mp, &in);
2188 mnt_drop_write_file(filp);
2192 case XFS_IOC_FSGROWFSLOG: {
2193 xfs_growfs_log_t in;
2195 if (copy_from_user(&in, arg, sizeof(in)))
2198 error = mnt_want_write_file(filp);
2201 error = xfs_growfs_log(mp, &in);
2202 mnt_drop_write_file(filp);
2206 case XFS_IOC_FSGROWFSRT: {
2209 if (copy_from_user(&in, arg, sizeof(in)))
2212 error = mnt_want_write_file(filp);
2215 error = xfs_growfs_rt(mp, &in);
2216 mnt_drop_write_file(filp);
2220 case XFS_IOC_GOINGDOWN: {
2223 if (!capable(CAP_SYS_ADMIN))
2226 if (get_user(in, (uint32_t __user *)arg))
2229 return xfs_fs_goingdown(mp, in);
2232 case XFS_IOC_ERROR_INJECTION: {
2233 xfs_error_injection_t in;
2235 if (!capable(CAP_SYS_ADMIN))
2238 if (copy_from_user(&in, arg, sizeof(in)))
2241 return xfs_errortag_add(mp, in.errtag);
2244 case XFS_IOC_ERROR_CLEARALL:
2245 if (!capable(CAP_SYS_ADMIN))
2248 return xfs_errortag_clearall(mp);
2250 case XFS_IOC_FREE_EOFBLOCKS: {
2251 struct xfs_fs_eofblocks eofb;
2252 struct xfs_eofblocks keofb;
2254 if (!capable(CAP_SYS_ADMIN))
2257 if (mp->m_flags & XFS_MOUNT_RDONLY)
2260 if (copy_from_user(&eofb, arg, sizeof(eofb)))
2263 error = xfs_fs_eofblocks_from_user(&eofb, &keofb);
2267 return xfs_icache_free_eofblocks(mp, &keofb);