Dave Chinner | 0b61f8a | 2018-06-05 19:42:14 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 3 | * Copyright (c) 2000-2005 Silicon Graphics, Inc. |
| 4 | * All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | #include "xfs.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | #include "xfs_fs.h" |
Dave Chinner | 70a9883 | 2013-10-23 10:36:05 +1100 | [diff] [blame] | 8 | #include "xfs_shared.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 9 | #include "xfs_format.h" |
| 10 | #include "xfs_log_format.h" |
| 11 | #include "xfs_trans_resv.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include "xfs_mount.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include "xfs_inode.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include "xfs_rtalloc.h" |
Darrick J. Wong | 2810bd6 | 2019-07-02 09:39:40 -0700 | [diff] [blame] | 15 | #include "xfs_iwalk.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include "xfs_itable.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 17 | #include "xfs_error.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include "xfs_attr.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 19 | #include "xfs_bmap.h" |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 20 | #include "xfs_bmap_util.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include "xfs_fsops.h" |
Christoph Hellwig | a46db60 | 2011-01-07 13:02:04 +0000 | [diff] [blame] | 22 | #include "xfs_discard.h" |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 23 | #include "xfs_quota.h" |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 24 | #include "xfs_export.h" |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 25 | #include "xfs_trace.h" |
Brian Foster | 8ca149d | 2012-11-07 12:21:12 -0500 | [diff] [blame] | 26 | #include "xfs_icache.h" |
Dave Chinner | a4fbe6a | 2013-10-23 10:51:50 +1100 | [diff] [blame] | 27 | #include "xfs_trans.h" |
Andreas Gruenbacher | 47e1bf6 | 2015-11-03 12:56:17 +1100 | [diff] [blame] | 28 | #include "xfs_acl.h" |
Darrick J. Wong | e89c041 | 2017-03-28 14:56:37 -0700 | [diff] [blame] | 29 | #include "xfs_btree.h" |
| 30 | #include <linux/fsmap.h> |
| 31 | #include "xfs_fsmap.h" |
Darrick J. Wong | 36fd6e8 | 2017-10-17 21:37:34 -0700 | [diff] [blame] | 32 | #include "scrub/xfs_scrub.h" |
Darrick J. Wong | c368ebc | 2018-01-08 10:51:27 -0800 | [diff] [blame] | 33 | #include "xfs_sb.h" |
Darrick J. Wong | 7cd5006 | 2019-04-12 07:41:17 -0700 | [diff] [blame] | 34 | #include "xfs_ag.h" |
Darrick J. Wong | c23232d | 2019-04-12 07:41:17 -0700 | [diff] [blame] | 35 | #include "xfs_health.h" |
Christoph Hellwig | 7a42c70 | 2019-10-24 22:26:27 -0700 | [diff] [blame] | 36 | #include "xfs_reflink.h" |
Darrick J. Wong | 5f213dd | 2019-11-06 17:19:33 -0800 | [diff] [blame] | 37 | #include "xfs_ioctl.h" |
Christoph Hellwig | a254462 | 2020-02-26 17:30:33 -0800 | [diff] [blame] | 38 | #include "xfs_da_format.h" |
| 39 | #include "xfs_da_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include <linux/mount.h> |
| 42 | #include <linux/namei.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
| 44 | /* |
| 45 | * xfs_find_handle maps from userspace xfs_fsop_handlereq structure to |
| 46 | * a file or fs handle. |
| 47 | * |
| 48 | * XFS_IOC_PATH_TO_FSHANDLE |
| 49 | * returns fs handle for a mount point or path within that mount point |
| 50 | * XFS_IOC_FD_TO_HANDLE |
| 51 | * returns full handle for a FD opened in user space |
| 52 | * XFS_IOC_PATH_TO_HANDLE |
| 53 | * returns full handle for a path |
| 54 | */ |
sandeen@sandeen.net | d5547f9 | 2008-11-25 21:20:08 -0600 | [diff] [blame] | 55 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | xfs_find_handle( |
| 57 | unsigned int cmd, |
sandeen@sandeen.net | 743bb465 | 2008-11-25 21:20:06 -0600 | [diff] [blame] | 58 | xfs_fsop_handlereq_t *hreq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | { |
| 60 | int hsize; |
| 61 | xfs_handle_t handle; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | struct inode *inode; |
Dave Chinner | a30b036 | 2013-09-02 20:49:36 +1000 | [diff] [blame] | 63 | struct fd f = {NULL}; |
Christoph Hellwig | 4346cdd | 2009-02-08 21:51:14 +0100 | [diff] [blame] | 64 | struct path path; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 65 | int error; |
Christoph Hellwig | 4346cdd | 2009-02-08 21:51:14 +0100 | [diff] [blame] | 66 | struct xfs_inode *ip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
Christoph Hellwig | 4346cdd | 2009-02-08 21:51:14 +0100 | [diff] [blame] | 68 | if (cmd == XFS_IOC_FD_TO_HANDLE) { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 69 | f = fdget(hreq->fd); |
| 70 | if (!f.file) |
Christoph Hellwig | 4346cdd | 2009-02-08 21:51:14 +0100 | [diff] [blame] | 71 | return -EBADF; |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 72 | inode = file_inode(f.file); |
Christoph Hellwig | 4346cdd | 2009-02-08 21:51:14 +0100 | [diff] [blame] | 73 | } else { |
Al Viro | ce6595a | 2019-07-14 16:42:44 -0400 | [diff] [blame] | 74 | error = user_path_at(AT_FDCWD, hreq->path, 0, &path); |
Christoph Hellwig | 4346cdd | 2009-02-08 21:51:14 +0100 | [diff] [blame] | 75 | if (error) |
| 76 | return error; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 77 | inode = d_inode(path.dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | } |
Christoph Hellwig | 4346cdd | 2009-02-08 21:51:14 +0100 | [diff] [blame] | 79 | ip = XFS_I(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | |
Christoph Hellwig | 4346cdd | 2009-02-08 21:51:14 +0100 | [diff] [blame] | 81 | /* |
| 82 | * We can only generate handles for inodes residing on a XFS filesystem, |
| 83 | * and only for regular files, directories or symbolic links. |
| 84 | */ |
| 85 | error = -EINVAL; |
| 86 | if (inode->i_sb->s_magic != XFS_SB_MAGIC) |
| 87 | goto out_put; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | |
Christoph Hellwig | 4346cdd | 2009-02-08 21:51:14 +0100 | [diff] [blame] | 89 | error = -EBADF; |
| 90 | if (!S_ISREG(inode->i_mode) && |
| 91 | !S_ISDIR(inode->i_mode) && |
| 92 | !S_ISLNK(inode->i_mode)) |
| 93 | goto out_put; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | |
Christoph Hellwig | 4346cdd | 2009-02-08 21:51:14 +0100 | [diff] [blame] | 96 | memcpy(&handle.ha_fsid, ip->i_mount->m_fixedfsid, sizeof(xfs_fsid_t)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | |
Christoph Hellwig | 4346cdd | 2009-02-08 21:51:14 +0100 | [diff] [blame] | 98 | if (cmd == XFS_IOC_PATH_TO_FSHANDLE) { |
| 99 | /* |
| 100 | * This handle only contains an fsid, zero the rest. |
| 101 | */ |
| 102 | memset(&handle.ha_fid, 0, sizeof(handle.ha_fid)); |
| 103 | hsize = sizeof(xfs_fsid_t); |
| 104 | } else { |
Christoph Hellwig | c614391 | 2007-09-14 15:22:37 +1000 | [diff] [blame] | 105 | handle.ha_fid.fid_len = sizeof(xfs_fid_t) - |
| 106 | sizeof(handle.ha_fid.fid_len); |
| 107 | handle.ha_fid.fid_pad = 0; |
Dave Chinner | 9e9a267 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 108 | handle.ha_fid.fid_gen = inode->i_generation; |
Christoph Hellwig | c614391 | 2007-09-14 15:22:37 +1000 | [diff] [blame] | 109 | handle.ha_fid.fid_ino = ip->i_ino; |
Christoph Hellwig | 3398a40 | 2017-06-14 21:30:44 -0700 | [diff] [blame] | 110 | hsize = sizeof(xfs_handle_t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Christoph Hellwig | 4346cdd | 2009-02-08 21:51:14 +0100 | [diff] [blame] | 113 | error = -EFAULT; |
sandeen@sandeen.net | 743bb465 | 2008-11-25 21:20:06 -0600 | [diff] [blame] | 114 | if (copy_to_user(hreq->ohandle, &handle, hsize) || |
Christoph Hellwig | 4346cdd | 2009-02-08 21:51:14 +0100 | [diff] [blame] | 115 | copy_to_user(hreq->ohandlen, &hsize, sizeof(__s32))) |
| 116 | goto out_put; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | |
Christoph Hellwig | 4346cdd | 2009-02-08 21:51:14 +0100 | [diff] [blame] | 118 | error = 0; |
| 119 | |
| 120 | out_put: |
| 121 | if (cmd == XFS_IOC_FD_TO_HANDLE) |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 122 | fdput(f); |
Christoph Hellwig | 4346cdd | 2009-02-08 21:51:14 +0100 | [diff] [blame] | 123 | else |
| 124 | path_put(&path); |
| 125 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | /* |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 129 | * No need to do permission checks on the various pathname components |
| 130 | * as the handle operations are privileged. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | */ |
| 132 | STATIC int |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 133 | xfs_handle_acceptable( |
| 134 | void *context, |
| 135 | struct dentry *dentry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | { |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 137 | return 1; |
| 138 | } |
| 139 | |
| 140 | /* |
| 141 | * Convert userspace handle data into a dentry. |
| 142 | */ |
| 143 | struct dentry * |
| 144 | xfs_handle_to_dentry( |
| 145 | struct file *parfilp, |
| 146 | void __user *uhandle, |
| 147 | u32 hlen) |
| 148 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | xfs_handle_t handle; |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 150 | struct xfs_fid64 fid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | |
| 152 | /* |
| 153 | * Only allow handle opens under a directory. |
| 154 | */ |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 155 | if (!S_ISDIR(file_inode(parfilp)->i_mode)) |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 156 | return ERR_PTR(-ENOTDIR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 158 | if (hlen != sizeof(xfs_handle_t)) |
| 159 | return ERR_PTR(-EINVAL); |
| 160 | if (copy_from_user(&handle, uhandle, hlen)) |
| 161 | return ERR_PTR(-EFAULT); |
| 162 | if (handle.ha_fid.fid_len != |
| 163 | sizeof(handle.ha_fid) - sizeof(handle.ha_fid.fid_len)) |
| 164 | return ERR_PTR(-EINVAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 166 | memset(&fid, 0, sizeof(struct fid)); |
| 167 | fid.ino = handle.ha_fid.fid_ino; |
| 168 | fid.gen = handle.ha_fid.fid_gen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 170 | return exportfs_decode_fh(parfilp->f_path.mnt, (struct fid *)&fid, 3, |
| 171 | FILEID_INO32_GEN | XFS_FILEID_TYPE_64FLAG, |
| 172 | xfs_handle_acceptable, NULL); |
| 173 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 175 | STATIC struct dentry * |
| 176 | xfs_handlereq_to_dentry( |
| 177 | struct file *parfilp, |
| 178 | xfs_fsop_handlereq_t *hreq) |
| 179 | { |
| 180 | return xfs_handle_to_dentry(parfilp, hreq->ihandle, hreq->ihandlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | } |
| 182 | |
sandeen@sandeen.net | d5547f9 | 2008-11-25 21:20:08 -0600 | [diff] [blame] | 183 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | xfs_open_by_handle( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | struct file *parfilp, |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 186 | xfs_fsop_handlereq_t *hreq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | { |
David Howells | 745ca24 | 2008-11-14 10:39:22 +1100 | [diff] [blame] | 188 | const struct cred *cred = current_cred(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | int error; |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 190 | int fd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | int permflag; |
| 192 | struct file *filp; |
| 193 | struct inode *inode; |
| 194 | struct dentry *dentry; |
Dave Chinner | 1a1d772 | 2012-03-22 05:15:06 +0000 | [diff] [blame] | 195 | fmode_t fmode; |
Al Viro | 765927b | 2012-06-26 21:58:53 +0400 | [diff] [blame] | 196 | struct path path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | |
| 198 | if (!capable(CAP_SYS_ADMIN)) |
Eric Sandeen | b474c7a | 2014-06-22 15:04:54 +1000 | [diff] [blame] | 199 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 201 | dentry = xfs_handlereq_to_dentry(parfilp, hreq); |
| 202 | if (IS_ERR(dentry)) |
| 203 | return PTR_ERR(dentry); |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 204 | inode = d_inode(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | |
| 206 | /* Restrict xfs_open_by_handle to directories & regular files. */ |
| 207 | if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode))) { |
Eric Sandeen | b474c7a | 2014-06-22 15:04:54 +1000 | [diff] [blame] | 208 | error = -EPERM; |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 209 | goto out_dput; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | #if BITS_PER_LONG != 32 |
sandeen@sandeen.net | 743bb465 | 2008-11-25 21:20:06 -0600 | [diff] [blame] | 213 | hreq->oflags |= O_LARGEFILE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | #endif |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 215 | |
sandeen@sandeen.net | 743bb465 | 2008-11-25 21:20:06 -0600 | [diff] [blame] | 216 | permflag = hreq->oflags; |
Dave Chinner | 1a1d772 | 2012-03-22 05:15:06 +0000 | [diff] [blame] | 217 | fmode = OPEN_FMODE(permflag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | if ((!(permflag & O_APPEND) || (permflag & O_TRUNC)) && |
Dave Chinner | 1a1d772 | 2012-03-22 05:15:06 +0000 | [diff] [blame] | 219 | (fmode & FMODE_WRITE) && IS_APPEND(inode)) { |
Eric Sandeen | b474c7a | 2014-06-22 15:04:54 +1000 | [diff] [blame] | 220 | error = -EPERM; |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 221 | goto out_dput; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | } |
| 223 | |
Dave Chinner | 1a1d772 | 2012-03-22 05:15:06 +0000 | [diff] [blame] | 224 | if ((fmode & FMODE_WRITE) && IS_IMMUTABLE(inode)) { |
Eryu Guan | 337684a | 2016-08-02 19:58:28 +0800 | [diff] [blame] | 225 | error = -EPERM; |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 226 | goto out_dput; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | /* Can't write directories. */ |
Dave Chinner | 1a1d772 | 2012-03-22 05:15:06 +0000 | [diff] [blame] | 230 | if (S_ISDIR(inode->i_mode) && (fmode & FMODE_WRITE)) { |
Eric Sandeen | b474c7a | 2014-06-22 15:04:54 +1000 | [diff] [blame] | 231 | error = -EISDIR; |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 232 | goto out_dput; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | } |
| 234 | |
Yann Droneaud | 862a629 | 2013-07-02 18:39:34 +0200 | [diff] [blame] | 235 | fd = get_unused_fd_flags(0); |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 236 | if (fd < 0) { |
| 237 | error = fd; |
| 238 | goto out_dput; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | } |
| 240 | |
Al Viro | 765927b | 2012-06-26 21:58:53 +0400 | [diff] [blame] | 241 | path.mnt = parfilp->f_path.mnt; |
| 242 | path.dentry = dentry; |
| 243 | filp = dentry_open(&path, hreq->oflags, cred); |
| 244 | dput(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | if (IS_ERR(filp)) { |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 246 | put_unused_fd(fd); |
| 247 | return PTR_ERR(filp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | } |
Christoph Hellwig | 4d4be48 | 2008-12-09 04:47:33 -0500 | [diff] [blame] | 249 | |
Al Viro | 0320937 | 2011-07-25 20:54:24 -0400 | [diff] [blame] | 250 | if (S_ISREG(inode->i_mode)) { |
Vlad Apostolov | 2e2e7bb | 2006-11-11 18:04:47 +1100 | [diff] [blame] | 251 | filp->f_flags |= O_NOATIME; |
Christoph Hellwig | 4d4be48 | 2008-12-09 04:47:33 -0500 | [diff] [blame] | 252 | filp->f_mode |= FMODE_NOCMTIME; |
Vlad Apostolov | 2e2e7bb | 2006-11-11 18:04:47 +1100 | [diff] [blame] | 253 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 255 | fd_install(fd, filp); |
| 256 | return fd; |
| 257 | |
| 258 | out_dput: |
| 259 | dput(dentry); |
| 260 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | } |
| 262 | |
sandeen@sandeen.net | d5547f9 | 2008-11-25 21:20:08 -0600 | [diff] [blame] | 263 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | xfs_readlink_by_handle( |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 265 | struct file *parfilp, |
| 266 | xfs_fsop_handlereq_t *hreq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | { |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 268 | struct dentry *dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | __u32 olen; |
Christoph Hellwig | 804c83c | 2007-08-28 13:59:03 +1000 | [diff] [blame] | 270 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | |
| 272 | if (!capable(CAP_SYS_ADMIN)) |
Eric Sandeen | b474c7a | 2014-06-22 15:04:54 +1000 | [diff] [blame] | 273 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 275 | dentry = xfs_handlereq_to_dentry(parfilp, hreq); |
| 276 | if (IS_ERR(dentry)) |
| 277 | return PTR_ERR(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | |
| 279 | /* Restrict this handle operation to symlinks only. */ |
Miklos Szeredi | fd4a0edf | 2016-12-09 16:45:04 +0100 | [diff] [blame] | 280 | if (!d_is_symlink(dentry)) { |
Eric Sandeen | b474c7a | 2014-06-22 15:04:54 +1000 | [diff] [blame] | 281 | error = -EINVAL; |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 282 | goto out_dput; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | } |
| 284 | |
sandeen@sandeen.net | 743bb465 | 2008-11-25 21:20:06 -0600 | [diff] [blame] | 285 | if (copy_from_user(&olen, hreq->ohandlen, sizeof(__u32))) { |
Eric Sandeen | b474c7a | 2014-06-22 15:04:54 +1000 | [diff] [blame] | 286 | error = -EFAULT; |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 287 | goto out_dput; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | |
Miklos Szeredi | fd4a0edf | 2016-12-09 16:45:04 +0100 | [diff] [blame] | 290 | error = vfs_readlink(dentry, hreq->ohandle, olen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 292 | out_dput: |
| 293 | dput(dentry); |
Christoph Hellwig | 804c83c | 2007-08-28 13:59:03 +1000 | [diff] [blame] | 294 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | } |
| 296 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | STATIC int |
| 298 | xfs_attrlist_by_handle( |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 299 | struct file *parfilp, |
| 300 | void __user *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | { |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 302 | int error = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | attrlist_cursor_kern_t *cursor; |
Darrick J. Wong | 0facef7 | 2016-08-03 10:58:53 +1000 | [diff] [blame] | 304 | struct xfs_fsop_attrlist_handlereq __user *p = arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | xfs_fsop_attrlist_handlereq_t al_hreq; |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 306 | struct dentry *dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | char *kbuf; |
| 308 | |
| 309 | if (!capable(CAP_SYS_ADMIN)) |
Eric Sandeen | b474c7a | 2014-06-22 15:04:54 +1000 | [diff] [blame] | 310 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | if (copy_from_user(&al_hreq, arg, sizeof(xfs_fsop_attrlist_handlereq_t))) |
Eric Sandeen | b474c7a | 2014-06-22 15:04:54 +1000 | [diff] [blame] | 312 | return -EFAULT; |
Dan Carpenter | 071c529 | 2013-10-31 21:00:10 +0300 | [diff] [blame] | 313 | if (al_hreq.buflen < sizeof(struct attrlist) || |
Jan Tulak | 4e24761 | 2015-10-12 16:02:56 +1100 | [diff] [blame] | 314 | al_hreq.buflen > XFS_XATTR_LIST_MAX) |
Eric Sandeen | b474c7a | 2014-06-22 15:04:54 +1000 | [diff] [blame] | 315 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | |
Christoph Hellwig | 90ad58a | 2008-06-27 13:32:19 +1000 | [diff] [blame] | 317 | /* |
| 318 | * Reject flags, only allow namespaces. |
| 319 | */ |
| 320 | if (al_hreq.flags & ~(ATTR_ROOT | ATTR_SECURE)) |
Eric Sandeen | b474c7a | 2014-06-22 15:04:54 +1000 | [diff] [blame] | 321 | return -EINVAL; |
Christoph Hellwig | 4d542e4 | 2020-02-26 17:30:28 -0800 | [diff] [blame] | 322 | if (al_hreq.flags == (ATTR_ROOT | ATTR_SECURE)) |
| 323 | return -EINVAL; |
Christoph Hellwig | 90ad58a | 2008-06-27 13:32:19 +1000 | [diff] [blame] | 324 | |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 325 | dentry = xfs_handlereq_to_dentry(parfilp, &al_hreq.hreq); |
| 326 | if (IS_ERR(dentry)) |
| 327 | return PTR_ERR(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | |
Tetsuo Handa | 707e0dd | 2019-08-26 12:06:22 -0700 | [diff] [blame] | 329 | kbuf = kmem_zalloc_large(al_hreq.buflen, 0); |
Dave Chinner | fdd3cce | 2013-09-02 20:53:00 +1000 | [diff] [blame] | 330 | if (!kbuf) |
| 331 | goto out_dput; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | |
| 333 | cursor = (attrlist_cursor_kern_t *)&al_hreq.pos; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 334 | error = xfs_attr_list(XFS_I(d_inode(dentry)), kbuf, al_hreq.buflen, |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 335 | al_hreq.flags, cursor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | if (error) |
| 337 | goto out_kfree; |
| 338 | |
Darrick J. Wong | 0facef7 | 2016-08-03 10:58:53 +1000 | [diff] [blame] | 339 | if (copy_to_user(&p->pos, cursor, sizeof(attrlist_cursor_kern_t))) { |
| 340 | error = -EFAULT; |
| 341 | goto out_kfree; |
| 342 | } |
| 343 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | if (copy_to_user(al_hreq.buffer, kbuf, al_hreq.buflen)) |
| 345 | error = -EFAULT; |
| 346 | |
Dave Chinner | fdd3cce | 2013-09-02 20:53:00 +1000 | [diff] [blame] | 347 | out_kfree: |
| 348 | kmem_free(kbuf); |
| 349 | out_dput: |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 350 | dput(dentry); |
| 351 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | } |
| 353 | |
Christoph Hellwig | d0ce6439 | 2020-02-26 17:30:31 -0800 | [diff] [blame] | 354 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | xfs_attrmulti_attr_get( |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 356 | struct inode *inode, |
Dave Chinner | a9273ca | 2010-01-20 10:47:48 +1100 | [diff] [blame] | 357 | unsigned char *name, |
| 358 | unsigned char __user *ubuf, |
Darrick J. Wong | c8ce540 | 2017-06-16 11:00:05 -0700 | [diff] [blame] | 359 | uint32_t *len, |
| 360 | uint32_t flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | { |
Christoph Hellwig | e5171d7 | 2020-02-26 17:30:34 -0800 | [diff] [blame^] | 362 | struct xfs_da_args args = { |
| 363 | .dp = XFS_I(inode), |
| 364 | .flags = flags, |
| 365 | .name = name, |
| 366 | .namelen = strlen(name), |
| 367 | .valuelen = *len, |
| 368 | }; |
| 369 | int error; |
Barry Naujok | e8b0eba | 2008-04-22 17:34:31 +1000 | [diff] [blame] | 370 | |
Jan Tulak | 51fcbfe | 2015-10-12 16:03:59 +1100 | [diff] [blame] | 371 | if (*len > XFS_XATTR_SIZE_MAX) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 372 | return -EINVAL; |
Christoph Hellwig | e5171d7 | 2020-02-26 17:30:34 -0800 | [diff] [blame^] | 373 | |
| 374 | args.value = kmem_zalloc_large(*len, 0); |
| 375 | if (!args.value) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 376 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | |
Christoph Hellwig | e5171d7 | 2020-02-26 17:30:34 -0800 | [diff] [blame^] | 378 | error = xfs_attr_get(&args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | if (error) |
| 380 | goto out_kfree; |
| 381 | |
Christoph Hellwig | e5171d7 | 2020-02-26 17:30:34 -0800 | [diff] [blame^] | 382 | *len = args.valuelen; |
| 383 | if (copy_to_user(ubuf, args.value, args.valuelen)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 384 | error = -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | |
Dave Chinner | fdd3cce | 2013-09-02 20:53:00 +1000 | [diff] [blame] | 386 | out_kfree: |
Christoph Hellwig | e5171d7 | 2020-02-26 17:30:34 -0800 | [diff] [blame^] | 387 | kmem_free(args.value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | return error; |
| 389 | } |
| 390 | |
Christoph Hellwig | d0ce6439 | 2020-02-26 17:30:31 -0800 | [diff] [blame] | 391 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | xfs_attrmulti_attr_set( |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 393 | struct inode *inode, |
Dave Chinner | a9273ca | 2010-01-20 10:47:48 +1100 | [diff] [blame] | 394 | unsigned char *name, |
| 395 | const unsigned char __user *ubuf, |
Darrick J. Wong | c8ce540 | 2017-06-16 11:00:05 -0700 | [diff] [blame] | 396 | uint32_t len, |
| 397 | uint32_t flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | { |
Christoph Hellwig | a254462 | 2020-02-26 17:30:33 -0800 | [diff] [blame] | 399 | struct xfs_da_args args = { |
| 400 | .dp = XFS_I(inode), |
| 401 | .flags = flags, |
| 402 | .name = name, |
| 403 | .namelen = strlen(name), |
| 404 | }; |
Andreas Gruenbacher | 09cb22d | 2015-11-03 12:53:54 +1100 | [diff] [blame] | 405 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 407 | if (IS_IMMUTABLE(inode) || IS_APPEND(inode)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 408 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | |
Christoph Hellwig | 6cc4f4f | 2020-02-26 17:30:30 -0800 | [diff] [blame] | 410 | if (ubuf) { |
| 411 | if (len > XFS_XATTR_SIZE_MAX) |
| 412 | return -EINVAL; |
Christoph Hellwig | a254462 | 2020-02-26 17:30:33 -0800 | [diff] [blame] | 413 | args.value = memdup_user(ubuf, len); |
| 414 | if (IS_ERR(args.value)) |
| 415 | return PTR_ERR(args.value); |
| 416 | args.valuelen = len; |
Christoph Hellwig | 6cc4f4f | 2020-02-26 17:30:30 -0800 | [diff] [blame] | 417 | } |
Barry Naujok | e8b0eba | 2008-04-22 17:34:31 +1000 | [diff] [blame] | 418 | |
Christoph Hellwig | a254462 | 2020-02-26 17:30:33 -0800 | [diff] [blame] | 419 | error = xfs_attr_set(&args); |
Andreas Gruenbacher | 47e1bf6 | 2015-11-03 12:56:17 +1100 | [diff] [blame] | 420 | if (!error) |
| 421 | xfs_forget_acl(inode, name, flags); |
Christoph Hellwig | a254462 | 2020-02-26 17:30:33 -0800 | [diff] [blame] | 422 | kfree(args.value); |
Andreas Gruenbacher | 09cb22d | 2015-11-03 12:53:54 +1100 | [diff] [blame] | 423 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | } |
| 425 | |
Christoph Hellwig | d0ce6439 | 2020-02-26 17:30:31 -0800 | [diff] [blame] | 426 | int |
| 427 | xfs_ioc_attrmulti_one( |
| 428 | struct file *parfilp, |
| 429 | struct inode *inode, |
| 430 | uint32_t opcode, |
| 431 | void __user *uname, |
| 432 | void __user *value, |
| 433 | uint32_t *len, |
| 434 | uint32_t flags) |
| 435 | { |
| 436 | unsigned char *name; |
| 437 | int error; |
| 438 | |
| 439 | if ((flags & ATTR_ROOT) && (flags & ATTR_SECURE)) |
| 440 | return -EINVAL; |
| 441 | flags &= ~ATTR_KERNEL_FLAGS; |
| 442 | |
| 443 | name = strndup_user(uname, MAXNAMELEN); |
| 444 | if (IS_ERR(name)) |
| 445 | return PTR_ERR(name); |
| 446 | |
| 447 | switch (opcode) { |
| 448 | case ATTR_OP_GET: |
| 449 | error = xfs_attrmulti_attr_get(inode, name, value, len, flags); |
| 450 | break; |
| 451 | case ATTR_OP_REMOVE: |
| 452 | value = NULL; |
| 453 | *len = 0; |
| 454 | /* fall through */ |
| 455 | case ATTR_OP_SET: |
| 456 | error = mnt_want_write_file(parfilp); |
| 457 | if (error) |
| 458 | break; |
| 459 | error = xfs_attrmulti_attr_set(inode, name, value, *len, flags); |
| 460 | mnt_drop_write_file(parfilp); |
| 461 | break; |
| 462 | default: |
| 463 | error = -EINVAL; |
| 464 | break; |
| 465 | } |
| 466 | |
| 467 | kfree(name); |
| 468 | return error; |
| 469 | } |
| 470 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | STATIC int |
| 472 | xfs_attrmulti_by_handle( |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 473 | struct file *parfilp, |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 474 | void __user *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | { |
| 476 | int error; |
| 477 | xfs_attr_multiop_t *ops; |
| 478 | xfs_fsop_attrmulti_handlereq_t am_hreq; |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 479 | struct dentry *dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | unsigned int i, size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | |
| 482 | if (!capable(CAP_SYS_ADMIN)) |
Eric Sandeen | b474c7a | 2014-06-22 15:04:54 +1000 | [diff] [blame] | 483 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | if (copy_from_user(&am_hreq, arg, sizeof(xfs_fsop_attrmulti_handlereq_t))) |
Eric Sandeen | b474c7a | 2014-06-22 15:04:54 +1000 | [diff] [blame] | 485 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | |
Zhitong Wang | fda168c | 2010-03-23 09:51:22 +1100 | [diff] [blame] | 487 | /* overflow check */ |
| 488 | if (am_hreq.opcount >= INT_MAX / sizeof(xfs_attr_multiop_t)) |
| 489 | return -E2BIG; |
| 490 | |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 491 | dentry = xfs_handlereq_to_dentry(parfilp, &am_hreq.hreq); |
| 492 | if (IS_ERR(dentry)) |
| 493 | return PTR_ERR(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 495 | error = -E2BIG; |
Christoph Hellwig | e182f57 | 2008-06-27 13:32:31 +1000 | [diff] [blame] | 496 | size = am_hreq.opcount * sizeof(xfs_attr_multiop_t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | if (!size || size > 16 * PAGE_SIZE) |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 498 | goto out_dput; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | |
Li Zefan | 0e639bd | 2009-04-08 15:08:04 +0800 | [diff] [blame] | 500 | ops = memdup_user(am_hreq.ops, size); |
| 501 | if (IS_ERR(ops)) { |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 502 | error = PTR_ERR(ops); |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 503 | goto out_dput; |
Li Zefan | 0e639bd | 2009-04-08 15:08:04 +0800 | [diff] [blame] | 504 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | error = 0; |
| 507 | for (i = 0; i < am_hreq.opcount; i++) { |
Christoph Hellwig | d0ce6439 | 2020-02-26 17:30:31 -0800 | [diff] [blame] | 508 | ops[i].am_error = xfs_ioc_attrmulti_one(parfilp, |
| 509 | d_inode(dentry), ops[i].am_opcode, |
| 510 | ops[i].am_attrname, ops[i].am_attrvalue, |
| 511 | &ops[i].am_length, ops[i].am_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | if (copy_to_user(am_hreq.ops, ops, size)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 515 | error = -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | kfree(ops); |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 518 | out_dput: |
| 519 | dput(dentry); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 520 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | } |
| 522 | |
sandeen@sandeen.net | d5547f9 | 2008-11-25 21:20:08 -0600 | [diff] [blame] | 523 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | xfs_ioc_space( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | struct file *filp, |
sandeen@sandeen.net | 743bb465 | 2008-11-25 21:20:06 -0600 | [diff] [blame] | 526 | xfs_flock64_t *bf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | { |
Christoph Hellwig | 8f3e205 | 2016-07-20 11:29:35 +1000 | [diff] [blame] | 528 | struct inode *inode = file_inode(filp); |
| 529 | struct xfs_inode *ip = XFS_I(inode); |
Christoph Hellwig | 865e944 | 2013-10-12 00:55:08 -0700 | [diff] [blame] | 530 | struct iattr iattr; |
Christoph Hellwig | 837a6e7 | 2019-10-24 22:26:02 -0700 | [diff] [blame] | 531 | enum xfs_prealloc_flags flags = XFS_PREALLOC_CLEAR; |
Dan Williams | c63a8ea | 2018-03-12 14:12:29 -0700 | [diff] [blame] | 532 | uint iolock = XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | int error; |
| 534 | |
Alexey Dobriyan | f37ea14 | 2006-09-28 10:52:04 +1000 | [diff] [blame] | 535 | if (inode->i_flags & (S_IMMUTABLE|S_APPEND)) |
Eric Sandeen | b474c7a | 2014-06-22 15:04:54 +1000 | [diff] [blame] | 536 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | |
Eric Sandeen | ad4a8ac | 2005-09-02 16:41:16 +1000 | [diff] [blame] | 538 | if (!(filp->f_mode & FMODE_WRITE)) |
Eric Sandeen | b474c7a | 2014-06-22 15:04:54 +1000 | [diff] [blame] | 539 | return -EBADF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | |
Alexey Dobriyan | f37ea14 | 2006-09-28 10:52:04 +1000 | [diff] [blame] | 541 | if (!S_ISREG(inode->i_mode)) |
Eric Sandeen | b474c7a | 2014-06-22 15:04:54 +1000 | [diff] [blame] | 542 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | |
Christoph Hellwig | 7a42c70 | 2019-10-24 22:26:27 -0700 | [diff] [blame] | 544 | if (xfs_is_always_cow_inode(ip)) |
| 545 | return -EOPNOTSUPP; |
| 546 | |
Christoph Hellwig | 8add71c | 2015-02-02 09:53:56 +1100 | [diff] [blame] | 547 | if (filp->f_flags & O_DSYNC) |
| 548 | flags |= XFS_PREALLOC_SYNC; |
Christoph Hellwig | 8f3e205 | 2016-07-20 11:29:35 +1000 | [diff] [blame] | 549 | if (filp->f_mode & FMODE_NOCMTIME) |
Christoph Hellwig | 8add71c | 2015-02-02 09:53:56 +1100 | [diff] [blame] | 550 | flags |= XFS_PREALLOC_INVISIBLE; |
| 551 | |
Jan Kara | d9457dc | 2012-06-12 16:20:39 +0200 | [diff] [blame] | 552 | error = mnt_want_write_file(filp); |
| 553 | if (error) |
| 554 | return error; |
Christoph Hellwig | 865e944 | 2013-10-12 00:55:08 -0700 | [diff] [blame] | 555 | |
Christoph Hellwig | 781355c | 2015-02-16 11:59:50 +1100 | [diff] [blame] | 556 | xfs_ilock(ip, iolock); |
Dan Williams | 69eb5fa | 2018-03-20 14:42:38 -0700 | [diff] [blame] | 557 | error = xfs_break_layouts(inode, &iolock, BREAK_UNMAP); |
Christoph Hellwig | 781355c | 2015-02-16 11:59:50 +1100 | [diff] [blame] | 558 | if (error) |
| 559 | goto out_unlock; |
Dave Chinner | 249bd90 | 2019-10-29 13:04:32 -0700 | [diff] [blame] | 560 | inode_dio_wait(inode); |
Christoph Hellwig | 865e944 | 2013-10-12 00:55:08 -0700 | [diff] [blame] | 561 | |
| 562 | switch (bf->l_whence) { |
| 563 | case 0: /*SEEK_SET*/ |
| 564 | break; |
| 565 | case 1: /*SEEK_CUR*/ |
| 566 | bf->l_start += filp->f_pos; |
| 567 | break; |
| 568 | case 2: /*SEEK_END*/ |
| 569 | bf->l_start += XFS_ISIZE(ip); |
| 570 | break; |
| 571 | default: |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 572 | error = -EINVAL; |
Christoph Hellwig | 865e944 | 2013-10-12 00:55:08 -0700 | [diff] [blame] | 573 | goto out_unlock; |
| 574 | } |
| 575 | |
Christoph Hellwig | 837a6e7 | 2019-10-24 22:26:02 -0700 | [diff] [blame] | 576 | if (bf->l_start < 0 || bf->l_start > inode->i_sb->s_maxbytes) { |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 577 | error = -EINVAL; |
Christoph Hellwig | 865e944 | 2013-10-12 00:55:08 -0700 | [diff] [blame] | 578 | goto out_unlock; |
| 579 | } |
| 580 | |
Christoph Hellwig | 837a6e7 | 2019-10-24 22:26:02 -0700 | [diff] [blame] | 581 | if (bf->l_start > XFS_ISIZE(ip)) { |
| 582 | error = xfs_alloc_file_space(ip, XFS_ISIZE(ip), |
| 583 | bf->l_start - XFS_ISIZE(ip), 0); |
| 584 | if (error) |
| 585 | goto out_unlock; |
Christoph Hellwig | 865e944 | 2013-10-12 00:55:08 -0700 | [diff] [blame] | 586 | } |
| 587 | |
Christoph Hellwig | 837a6e7 | 2019-10-24 22:26:02 -0700 | [diff] [blame] | 588 | iattr.ia_valid = ATTR_SIZE; |
| 589 | iattr.ia_size = bf->l_start; |
| 590 | error = xfs_vn_setattr_size(file_dentry(filp), &iattr); |
Christoph Hellwig | 865e944 | 2013-10-12 00:55:08 -0700 | [diff] [blame] | 591 | if (error) |
| 592 | goto out_unlock; |
| 593 | |
Christoph Hellwig | 8add71c | 2015-02-02 09:53:56 +1100 | [diff] [blame] | 594 | error = xfs_update_prealloc_flags(ip, flags); |
Christoph Hellwig | 865e944 | 2013-10-12 00:55:08 -0700 | [diff] [blame] | 595 | |
| 596 | out_unlock: |
Christoph Hellwig | 781355c | 2015-02-16 11:59:50 +1100 | [diff] [blame] | 597 | xfs_iunlock(ip, iolock); |
Jan Kara | d9457dc | 2012-06-12 16:20:39 +0200 | [diff] [blame] | 598 | mnt_drop_write_file(filp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 599 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | } |
| 601 | |
Darrick J. Wong | 2810bd6 | 2019-07-02 09:39:40 -0700 | [diff] [blame] | 602 | /* Return 0 on success or positive error */ |
| 603 | int |
Darrick J. Wong | 8bfe9d1 | 2019-07-03 20:36:26 -0700 | [diff] [blame] | 604 | xfs_fsbulkstat_one_fmt( |
Darrick J. Wong | 7035f97 | 2019-07-03 20:36:26 -0700 | [diff] [blame] | 605 | struct xfs_ibulk *breq, |
| 606 | const struct xfs_bulkstat *bstat) |
Darrick J. Wong | 2810bd6 | 2019-07-02 09:39:40 -0700 | [diff] [blame] | 607 | { |
Darrick J. Wong | 7035f97 | 2019-07-03 20:36:26 -0700 | [diff] [blame] | 608 | struct xfs_bstat bs1; |
| 609 | |
| 610 | xfs_bulkstat_to_bstat(breq->mp, &bs1, bstat); |
| 611 | if (copy_to_user(breq->ubuffer, &bs1, sizeof(bs1))) |
Darrick J. Wong | 2810bd6 | 2019-07-02 09:39:40 -0700 | [diff] [blame] | 612 | return -EFAULT; |
| 613 | return xfs_ibulk_advance(breq, sizeof(struct xfs_bstat)); |
| 614 | } |
| 615 | |
Darrick J. Wong | 677717f | 2019-07-02 09:39:43 -0700 | [diff] [blame] | 616 | int |
Darrick J. Wong | 8bfe9d1 | 2019-07-03 20:36:26 -0700 | [diff] [blame] | 617 | xfs_fsinumbers_fmt( |
Darrick J. Wong | 5f19c7f | 2019-07-03 20:36:27 -0700 | [diff] [blame] | 618 | struct xfs_ibulk *breq, |
| 619 | const struct xfs_inumbers *igrp) |
Darrick J. Wong | 677717f | 2019-07-02 09:39:43 -0700 | [diff] [blame] | 620 | { |
Darrick J. Wong | 5f19c7f | 2019-07-03 20:36:27 -0700 | [diff] [blame] | 621 | struct xfs_inogrp ig1; |
| 622 | |
| 623 | xfs_inumbers_to_inogrp(&ig1, igrp); |
| 624 | if (copy_to_user(breq->ubuffer, &ig1, sizeof(struct xfs_inogrp))) |
Darrick J. Wong | 677717f | 2019-07-02 09:39:43 -0700 | [diff] [blame] | 625 | return -EFAULT; |
| 626 | return xfs_ibulk_advance(breq, sizeof(struct xfs_inogrp)); |
| 627 | } |
| 628 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | STATIC int |
Darrick J. Wong | 8bfe9d1 | 2019-07-03 20:36:26 -0700 | [diff] [blame] | 630 | xfs_ioc_fsbulkstat( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | xfs_mount_t *mp, |
| 632 | unsigned int cmd, |
| 633 | void __user *arg) |
| 634 | { |
Darrick J. Wong | 2810bd6 | 2019-07-02 09:39:40 -0700 | [diff] [blame] | 635 | struct xfs_fsop_bulkreq bulkreq; |
| 636 | struct xfs_ibulk breq = { |
| 637 | .mp = mp, |
| 638 | .ocount = 0, |
| 639 | }; |
| 640 | xfs_ino_t lastino; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | int error; |
| 642 | |
| 643 | /* done = 1 if there are more stats to get and if bulkstat */ |
| 644 | /* should be called again (unused here, but used in dmapi) */ |
| 645 | |
| 646 | if (!capable(CAP_SYS_ADMIN)) |
| 647 | return -EPERM; |
| 648 | |
| 649 | if (XFS_FORCED_SHUTDOWN(mp)) |
Eric Sandeen | b474c7a | 2014-06-22 15:04:54 +1000 | [diff] [blame] | 650 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | |
Darrick J. Wong | 6f71fb6 | 2019-07-03 20:36:25 -0700 | [diff] [blame] | 652 | if (copy_from_user(&bulkreq, arg, sizeof(struct xfs_fsop_bulkreq))) |
Eric Sandeen | b474c7a | 2014-06-22 15:04:54 +1000 | [diff] [blame] | 653 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | |
Darrick J. Wong | 2810bd6 | 2019-07-02 09:39:40 -0700 | [diff] [blame] | 655 | if (copy_from_user(&lastino, bulkreq.lastip, sizeof(__s64))) |
Eric Sandeen | b474c7a | 2014-06-22 15:04:54 +1000 | [diff] [blame] | 656 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | |
Darrick J. Wong | 2810bd6 | 2019-07-02 09:39:40 -0700 | [diff] [blame] | 658 | if (bulkreq.icount <= 0) |
Eric Sandeen | b474c7a | 2014-06-22 15:04:54 +1000 | [diff] [blame] | 659 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | |
Lachlan McIlroy | cd57e59 | 2007-11-23 16:30:32 +1100 | [diff] [blame] | 661 | if (bulkreq.ubuffer == NULL) |
Eric Sandeen | b474c7a | 2014-06-22 15:04:54 +1000 | [diff] [blame] | 662 | return -EINVAL; |
Lachlan McIlroy | cd57e59 | 2007-11-23 16:30:32 +1100 | [diff] [blame] | 663 | |
Darrick J. Wong | 2810bd6 | 2019-07-02 09:39:40 -0700 | [diff] [blame] | 664 | breq.ubuffer = bulkreq.ubuffer; |
| 665 | breq.icount = bulkreq.icount; |
| 666 | |
| 667 | /* |
| 668 | * FSBULKSTAT_SINGLE expects that *lastip contains the inode number |
| 669 | * that we want to stat. However, FSINUMBERS and FSBULKSTAT expect |
| 670 | * that *lastip contains either zero or the number of the last inode to |
| 671 | * be examined by the previous call and return results starting with |
| 672 | * the next inode after that. The new bulk request back end functions |
| 673 | * take the inode to start with, so we have to compute the startino |
| 674 | * parameter from lastino to maintain correct function. lastino == 0 |
| 675 | * is a special case because it has traditionally meant "first inode |
| 676 | * in filesystem". |
| 677 | */ |
| 678 | if (cmd == XFS_IOC_FSINUMBERS) { |
Darrick J. Wong | 677717f | 2019-07-02 09:39:43 -0700 | [diff] [blame] | 679 | breq.startino = lastino ? lastino + 1 : 0; |
Darrick J. Wong | 8bfe9d1 | 2019-07-03 20:36:26 -0700 | [diff] [blame] | 680 | error = xfs_inumbers(&breq, xfs_fsinumbers_fmt); |
Darrick J. Wong | 677717f | 2019-07-02 09:39:43 -0700 | [diff] [blame] | 681 | lastino = breq.startino - 1; |
Darrick J. Wong | 2810bd6 | 2019-07-02 09:39:40 -0700 | [diff] [blame] | 682 | } else if (cmd == XFS_IOC_FSBULKSTAT_SINGLE) { |
| 683 | breq.startino = lastino; |
| 684 | breq.icount = 1; |
Darrick J. Wong | 8bfe9d1 | 2019-07-03 20:36:26 -0700 | [diff] [blame] | 685 | error = xfs_bulkstat_one(&breq, xfs_fsbulkstat_one_fmt); |
Darrick J. Wong | 2810bd6 | 2019-07-02 09:39:40 -0700 | [diff] [blame] | 686 | } else { /* XFS_IOC_FSBULKSTAT */ |
| 687 | breq.startino = lastino ? lastino + 1 : 0; |
Darrick J. Wong | 8bfe9d1 | 2019-07-03 20:36:26 -0700 | [diff] [blame] | 688 | error = xfs_bulkstat(&breq, xfs_fsbulkstat_one_fmt); |
Darrick J. Wong | 2810bd6 | 2019-07-02 09:39:40 -0700 | [diff] [blame] | 689 | lastino = breq.startino - 1; |
| 690 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | |
| 692 | if (error) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 693 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | |
Darrick J. Wong | f16fe3e | 2019-07-02 09:39:39 -0700 | [diff] [blame] | 695 | if (bulkreq.lastip != NULL && |
Darrick J. Wong | 2810bd6 | 2019-07-02 09:39:40 -0700 | [diff] [blame] | 696 | copy_to_user(bulkreq.lastip, &lastino, sizeof(xfs_ino_t))) |
Darrick J. Wong | f16fe3e | 2019-07-02 09:39:39 -0700 | [diff] [blame] | 697 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | |
Darrick J. Wong | f16fe3e | 2019-07-02 09:39:39 -0700 | [diff] [blame] | 699 | if (bulkreq.ocount != NULL && |
Darrick J. Wong | 2810bd6 | 2019-07-02 09:39:40 -0700 | [diff] [blame] | 700 | copy_to_user(bulkreq.ocount, &breq.ocount, sizeof(__s32))) |
Darrick J. Wong | f16fe3e | 2019-07-02 09:39:39 -0700 | [diff] [blame] | 701 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | |
| 703 | return 0; |
| 704 | } |
| 705 | |
Darrick J. Wong | 0448b6f | 2019-07-03 20:36:27 -0700 | [diff] [blame] | 706 | /* Return 0 on success or positive error */ |
| 707 | static int |
| 708 | xfs_bulkstat_fmt( |
| 709 | struct xfs_ibulk *breq, |
| 710 | const struct xfs_bulkstat *bstat) |
| 711 | { |
| 712 | if (copy_to_user(breq->ubuffer, bstat, sizeof(struct xfs_bulkstat))) |
| 713 | return -EFAULT; |
| 714 | return xfs_ibulk_advance(breq, sizeof(struct xfs_bulkstat)); |
| 715 | } |
| 716 | |
| 717 | /* |
| 718 | * Check the incoming bulk request @hdr from userspace and initialize the |
| 719 | * internal @breq bulk request appropriately. Returns 0 if the bulk request |
Darrick J. Wong | e7ee96d | 2019-08-28 14:37:57 -0700 | [diff] [blame] | 720 | * should proceed; -ECANCELED if there's nothing to do; or the usual |
Darrick J. Wong | 0448b6f | 2019-07-03 20:36:27 -0700 | [diff] [blame] | 721 | * negative error code. |
| 722 | */ |
| 723 | static int |
| 724 | xfs_bulk_ireq_setup( |
| 725 | struct xfs_mount *mp, |
| 726 | struct xfs_bulk_ireq *hdr, |
| 727 | struct xfs_ibulk *breq, |
| 728 | void __user *ubuffer) |
| 729 | { |
| 730 | if (hdr->icount == 0 || |
| 731 | (hdr->flags & ~XFS_BULK_IREQ_FLAGS_ALL) || |
Darrick J. Wong | 0448b6f | 2019-07-03 20:36:27 -0700 | [diff] [blame] | 732 | memchr_inv(hdr->reserved, 0, sizeof(hdr->reserved))) |
| 733 | return -EINVAL; |
| 734 | |
| 735 | breq->startino = hdr->ino; |
| 736 | breq->ubuffer = ubuffer; |
| 737 | breq->icount = hdr->icount; |
| 738 | breq->ocount = 0; |
Darrick J. Wong | 13d59a2 | 2019-07-03 20:36:28 -0700 | [diff] [blame] | 739 | breq->flags = 0; |
| 740 | |
| 741 | /* |
Darrick J. Wong | bf3cb39 | 2019-07-03 20:36:29 -0700 | [diff] [blame] | 742 | * The @ino parameter is a special value, so we must look it up here. |
| 743 | * We're not allowed to have IREQ_AGNO, and we only return one inode |
| 744 | * worth of data. |
| 745 | */ |
| 746 | if (hdr->flags & XFS_BULK_IREQ_SPECIAL) { |
| 747 | if (hdr->flags & XFS_BULK_IREQ_AGNO) |
| 748 | return -EINVAL; |
| 749 | |
| 750 | switch (hdr->ino) { |
| 751 | case XFS_BULK_IREQ_SPECIAL_ROOT: |
| 752 | hdr->ino = mp->m_sb.sb_rootino; |
| 753 | break; |
| 754 | default: |
| 755 | return -EINVAL; |
| 756 | } |
| 757 | breq->icount = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | } |
| 759 | |
Darrick J. Wong | bf3cb39 | 2019-07-03 20:36:29 -0700 | [diff] [blame] | 760 | /* |
Darrick J. Wong | 13d59a2 | 2019-07-03 20:36:28 -0700 | [diff] [blame] | 761 | * The IREQ_AGNO flag means that we only want results from a given AG. |
| 762 | * If @hdr->ino is zero, we start iterating in that AG. If @hdr->ino is |
| 763 | * beyond the specified AG then we return no results. |
| 764 | */ |
| 765 | if (hdr->flags & XFS_BULK_IREQ_AGNO) { |
| 766 | if (hdr->agno >= mp->m_sb.sb_agcount) |
| 767 | return -EINVAL; |
| 768 | |
| 769 | if (breq->startino == 0) |
| 770 | breq->startino = XFS_AGINO_TO_INO(mp, hdr->agno, 0); |
| 771 | else if (XFS_INO_TO_AGNO(mp, breq->startino) < hdr->agno) |
| 772 | return -EINVAL; |
| 773 | |
| 774 | breq->flags |= XFS_IBULK_SAME_AG; |
| 775 | |
| 776 | /* Asking for an inode past the end of the AG? We're done! */ |
| 777 | if (XFS_INO_TO_AGNO(mp, breq->startino) > hdr->agno) |
Darrick J. Wong | e7ee96d | 2019-08-28 14:37:57 -0700 | [diff] [blame] | 778 | return -ECANCELED; |
Darrick J. Wong | 13d59a2 | 2019-07-03 20:36:28 -0700 | [diff] [blame] | 779 | } else if (hdr->agno) |
| 780 | return -EINVAL; |
Darrick J. Wong | 0448b6f | 2019-07-03 20:36:27 -0700 | [diff] [blame] | 781 | |
| 782 | /* Asking for an inode past the end of the FS? We're done! */ |
| 783 | if (XFS_INO_TO_AGNO(mp, breq->startino) >= mp->m_sb.sb_agcount) |
Darrick J. Wong | e7ee96d | 2019-08-28 14:37:57 -0700 | [diff] [blame] | 784 | return -ECANCELED; |
Darrick J. Wong | 0448b6f | 2019-07-03 20:36:27 -0700 | [diff] [blame] | 785 | |
| 786 | return 0; |
| 787 | } |
| 788 | |
| 789 | /* |
| 790 | * Update the userspace bulk request @hdr to reflect the end state of the |
| 791 | * internal bulk request @breq. |
| 792 | */ |
| 793 | static void |
| 794 | xfs_bulk_ireq_teardown( |
| 795 | struct xfs_bulk_ireq *hdr, |
| 796 | struct xfs_ibulk *breq) |
| 797 | { |
| 798 | hdr->ino = breq->startino; |
| 799 | hdr->ocount = breq->ocount; |
| 800 | } |
| 801 | |
| 802 | /* Handle the v5 bulkstat ioctl. */ |
| 803 | STATIC int |
| 804 | xfs_ioc_bulkstat( |
| 805 | struct xfs_mount *mp, |
| 806 | unsigned int cmd, |
| 807 | struct xfs_bulkstat_req __user *arg) |
| 808 | { |
| 809 | struct xfs_bulk_ireq hdr; |
| 810 | struct xfs_ibulk breq = { |
| 811 | .mp = mp, |
| 812 | }; |
| 813 | int error; |
| 814 | |
| 815 | if (!capable(CAP_SYS_ADMIN)) |
| 816 | return -EPERM; |
| 817 | |
| 818 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 819 | return -EIO; |
| 820 | |
| 821 | if (copy_from_user(&hdr, &arg->hdr, sizeof(hdr))) |
| 822 | return -EFAULT; |
| 823 | |
| 824 | error = xfs_bulk_ireq_setup(mp, &hdr, &breq, arg->bulkstat); |
Darrick J. Wong | e7ee96d | 2019-08-28 14:37:57 -0700 | [diff] [blame] | 825 | if (error == -ECANCELED) |
Darrick J. Wong | 0448b6f | 2019-07-03 20:36:27 -0700 | [diff] [blame] | 826 | goto out_teardown; |
| 827 | if (error < 0) |
| 828 | return error; |
| 829 | |
| 830 | error = xfs_bulkstat(&breq, xfs_bulkstat_fmt); |
| 831 | if (error) |
| 832 | return error; |
| 833 | |
| 834 | out_teardown: |
| 835 | xfs_bulk_ireq_teardown(&hdr, &breq); |
| 836 | if (copy_to_user(&arg->hdr, &hdr, sizeof(hdr))) |
| 837 | return -EFAULT; |
| 838 | |
| 839 | return 0; |
| 840 | } |
| 841 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 842 | STATIC int |
Darrick J. Wong | fba9760 | 2019-07-03 20:36:28 -0700 | [diff] [blame] | 843 | xfs_inumbers_fmt( |
| 844 | struct xfs_ibulk *breq, |
| 845 | const struct xfs_inumbers *igrp) |
| 846 | { |
| 847 | if (copy_to_user(breq->ubuffer, igrp, sizeof(struct xfs_inumbers))) |
| 848 | return -EFAULT; |
| 849 | return xfs_ibulk_advance(breq, sizeof(struct xfs_inumbers)); |
| 850 | } |
| 851 | |
| 852 | /* Handle the v5 inumbers ioctl. */ |
| 853 | STATIC int |
| 854 | xfs_ioc_inumbers( |
| 855 | struct xfs_mount *mp, |
| 856 | unsigned int cmd, |
| 857 | struct xfs_inumbers_req __user *arg) |
| 858 | { |
| 859 | struct xfs_bulk_ireq hdr; |
| 860 | struct xfs_ibulk breq = { |
| 861 | .mp = mp, |
| 862 | }; |
| 863 | int error; |
| 864 | |
| 865 | if (!capable(CAP_SYS_ADMIN)) |
| 866 | return -EPERM; |
| 867 | |
| 868 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 869 | return -EIO; |
| 870 | |
| 871 | if (copy_from_user(&hdr, &arg->hdr, sizeof(hdr))) |
| 872 | return -EFAULT; |
| 873 | |
| 874 | error = xfs_bulk_ireq_setup(mp, &hdr, &breq, arg->inumbers); |
Darrick J. Wong | e7ee96d | 2019-08-28 14:37:57 -0700 | [diff] [blame] | 875 | if (error == -ECANCELED) |
Darrick J. Wong | fba9760 | 2019-07-03 20:36:28 -0700 | [diff] [blame] | 876 | goto out_teardown; |
| 877 | if (error < 0) |
| 878 | return error; |
| 879 | |
| 880 | error = xfs_inumbers(&breq, xfs_inumbers_fmt); |
| 881 | if (error) |
| 882 | return error; |
| 883 | |
| 884 | out_teardown: |
| 885 | xfs_bulk_ireq_teardown(&hdr, &breq); |
| 886 | if (copy_to_user(&arg->hdr, &hdr, sizeof(hdr))) |
| 887 | return -EFAULT; |
| 888 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | return 0; |
| 890 | } |
| 891 | |
| 892 | STATIC int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | xfs_ioc_fsgeometry( |
Dave Chinner | 1b6d968 | 2019-04-12 07:41:16 -0700 | [diff] [blame] | 894 | struct xfs_mount *mp, |
| 895 | void __user *arg, |
| 896 | int struct_version) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 897 | { |
Dave Chinner | 1b6d968 | 2019-04-12 07:41:16 -0700 | [diff] [blame] | 898 | struct xfs_fsop_geom fsgeo; |
| 899 | size_t len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | |
Eric Sandeen | 9108326 | 2019-05-01 20:26:30 -0700 | [diff] [blame] | 901 | xfs_fs_geometry(&mp->m_sb, &fsgeo, struct_version); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | |
Dave Chinner | 1b6d968 | 2019-04-12 07:41:16 -0700 | [diff] [blame] | 903 | if (struct_version <= 3) |
| 904 | len = sizeof(struct xfs_fsop_geom_v1); |
| 905 | else if (struct_version == 4) |
| 906 | len = sizeof(struct xfs_fsop_geom_v4); |
Darrick J. Wong | c23232d | 2019-04-12 07:41:17 -0700 | [diff] [blame] | 907 | else { |
| 908 | xfs_fsop_geom_health(mp, &fsgeo); |
Dave Chinner | 1b6d968 | 2019-04-12 07:41:16 -0700 | [diff] [blame] | 909 | len = sizeof(fsgeo); |
Darrick J. Wong | c23232d | 2019-04-12 07:41:17 -0700 | [diff] [blame] | 910 | } |
Dave Chinner | 1b6d968 | 2019-04-12 07:41:16 -0700 | [diff] [blame] | 911 | |
| 912 | if (copy_to_user(arg, &fsgeo, len)) |
Eric Sandeen | b474c7a | 2014-06-22 15:04:54 +1000 | [diff] [blame] | 913 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | return 0; |
| 915 | } |
| 916 | |
Darrick J. Wong | 7cd5006 | 2019-04-12 07:41:17 -0700 | [diff] [blame] | 917 | STATIC int |
| 918 | xfs_ioc_ag_geometry( |
| 919 | struct xfs_mount *mp, |
| 920 | void __user *arg) |
| 921 | { |
| 922 | struct xfs_ag_geometry ageo; |
| 923 | int error; |
| 924 | |
| 925 | if (copy_from_user(&ageo, arg, sizeof(ageo))) |
| 926 | return -EFAULT; |
Darrick J. Wong | 76f1793 | 2019-08-30 16:30:22 -0700 | [diff] [blame] | 927 | if (ageo.ag_flags) |
| 928 | return -EINVAL; |
| 929 | if (memchr_inv(&ageo.ag_reserved, 0, sizeof(ageo.ag_reserved))) |
| 930 | return -EINVAL; |
Darrick J. Wong | 7cd5006 | 2019-04-12 07:41:17 -0700 | [diff] [blame] | 931 | |
| 932 | error = xfs_ag_get_geometry(mp, ageo.ag_number, &ageo); |
| 933 | if (error) |
| 934 | return error; |
| 935 | |
| 936 | if (copy_to_user(arg, &ageo, sizeof(ageo))) |
| 937 | return -EFAULT; |
| 938 | return 0; |
| 939 | } |
| 940 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 941 | /* |
| 942 | * Linux extended inode flags interface. |
| 943 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | |
| 945 | STATIC unsigned int |
| 946 | xfs_merge_ioc_xflags( |
| 947 | unsigned int flags, |
| 948 | unsigned int start) |
| 949 | { |
| 950 | unsigned int xflags = start; |
| 951 | |
Eric Sandeen | 39058a0 | 2007-02-10 18:37:10 +1100 | [diff] [blame] | 952 | if (flags & FS_IMMUTABLE_FL) |
Dave Chinner | e7b8948 | 2016-01-04 16:44:15 +1100 | [diff] [blame] | 953 | xflags |= FS_XFLAG_IMMUTABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | else |
Dave Chinner | e7b8948 | 2016-01-04 16:44:15 +1100 | [diff] [blame] | 955 | xflags &= ~FS_XFLAG_IMMUTABLE; |
Eric Sandeen | 39058a0 | 2007-02-10 18:37:10 +1100 | [diff] [blame] | 956 | if (flags & FS_APPEND_FL) |
Dave Chinner | e7b8948 | 2016-01-04 16:44:15 +1100 | [diff] [blame] | 957 | xflags |= FS_XFLAG_APPEND; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | else |
Dave Chinner | e7b8948 | 2016-01-04 16:44:15 +1100 | [diff] [blame] | 959 | xflags &= ~FS_XFLAG_APPEND; |
Eric Sandeen | 39058a0 | 2007-02-10 18:37:10 +1100 | [diff] [blame] | 960 | if (flags & FS_SYNC_FL) |
Dave Chinner | e7b8948 | 2016-01-04 16:44:15 +1100 | [diff] [blame] | 961 | xflags |= FS_XFLAG_SYNC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 962 | else |
Dave Chinner | e7b8948 | 2016-01-04 16:44:15 +1100 | [diff] [blame] | 963 | xflags &= ~FS_XFLAG_SYNC; |
Eric Sandeen | 39058a0 | 2007-02-10 18:37:10 +1100 | [diff] [blame] | 964 | if (flags & FS_NOATIME_FL) |
Dave Chinner | e7b8948 | 2016-01-04 16:44:15 +1100 | [diff] [blame] | 965 | xflags |= FS_XFLAG_NOATIME; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 966 | else |
Dave Chinner | e7b8948 | 2016-01-04 16:44:15 +1100 | [diff] [blame] | 967 | xflags &= ~FS_XFLAG_NOATIME; |
Eric Sandeen | 39058a0 | 2007-02-10 18:37:10 +1100 | [diff] [blame] | 968 | if (flags & FS_NODUMP_FL) |
Dave Chinner | e7b8948 | 2016-01-04 16:44:15 +1100 | [diff] [blame] | 969 | xflags |= FS_XFLAG_NODUMP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | else |
Dave Chinner | e7b8948 | 2016-01-04 16:44:15 +1100 | [diff] [blame] | 971 | xflags &= ~FS_XFLAG_NODUMP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | |
| 973 | return xflags; |
| 974 | } |
| 975 | |
| 976 | STATIC unsigned int |
| 977 | xfs_di2lxflags( |
Darrick J. Wong | c8ce540 | 2017-06-16 11:00:05 -0700 | [diff] [blame] | 978 | uint16_t di_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 979 | { |
| 980 | unsigned int flags = 0; |
| 981 | |
| 982 | if (di_flags & XFS_DIFLAG_IMMUTABLE) |
Eric Sandeen | 39058a0 | 2007-02-10 18:37:10 +1100 | [diff] [blame] | 983 | flags |= FS_IMMUTABLE_FL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 984 | if (di_flags & XFS_DIFLAG_APPEND) |
Eric Sandeen | 39058a0 | 2007-02-10 18:37:10 +1100 | [diff] [blame] | 985 | flags |= FS_APPEND_FL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 986 | if (di_flags & XFS_DIFLAG_SYNC) |
Eric Sandeen | 39058a0 | 2007-02-10 18:37:10 +1100 | [diff] [blame] | 987 | flags |= FS_SYNC_FL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 988 | if (di_flags & XFS_DIFLAG_NOATIME) |
Eric Sandeen | 39058a0 | 2007-02-10 18:37:10 +1100 | [diff] [blame] | 989 | flags |= FS_NOATIME_FL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | if (di_flags & XFS_DIFLAG_NODUMP) |
Eric Sandeen | 39058a0 | 2007-02-10 18:37:10 +1100 | [diff] [blame] | 991 | flags |= FS_NODUMP_FL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 992 | return flags; |
| 993 | } |
| 994 | |
Darrick J. Wong | 7b0e492 | 2019-07-01 08:25:35 -0700 | [diff] [blame] | 995 | static void |
| 996 | xfs_fill_fsxattr( |
| 997 | struct xfs_inode *ip, |
| 998 | bool attr, |
| 999 | struct fsxattr *fa) |
| 1000 | { |
| 1001 | simple_fill_fsxattr(fa, xfs_ip2xflags(ip)); |
| 1002 | fa->fsx_extsize = ip->i_d.di_extsize << ip->i_mount->m_sb.sb_blocklog; |
| 1003 | fa->fsx_cowextsize = ip->i_d.di_cowextsize << |
| 1004 | ip->i_mount->m_sb.sb_blocklog; |
Christoph Hellwig | de7a866 | 2019-11-12 08:22:54 -0800 | [diff] [blame] | 1005 | fa->fsx_projid = ip->i_d.di_projid; |
Darrick J. Wong | 7b0e492 | 2019-07-01 08:25:35 -0700 | [diff] [blame] | 1006 | |
| 1007 | if (attr) { |
| 1008 | if (ip->i_afp) { |
| 1009 | if (ip->i_afp->if_flags & XFS_IFEXTENTS) |
| 1010 | fa->fsx_nextents = xfs_iext_count(ip->i_afp); |
| 1011 | else |
| 1012 | fa->fsx_nextents = ip->i_d.di_anextents; |
| 1013 | } else |
| 1014 | fa->fsx_nextents = 0; |
| 1015 | } else { |
| 1016 | if (ip->i_df.if_flags & XFS_IFEXTENTS) |
| 1017 | fa->fsx_nextents = xfs_iext_count(&ip->i_df); |
| 1018 | else |
| 1019 | fa->fsx_nextents = ip->i_d.di_nextents; |
| 1020 | } |
| 1021 | } |
| 1022 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1023 | STATIC int |
Christoph Hellwig | c83bfab | 2007-10-11 17:47:00 +1000 | [diff] [blame] | 1024 | xfs_ioc_fsgetxattr( |
| 1025 | xfs_inode_t *ip, |
| 1026 | int attr, |
| 1027 | void __user *arg) |
| 1028 | { |
| 1029 | struct fsxattr fa; |
| 1030 | |
| 1031 | xfs_ilock(ip, XFS_ILOCK_SHARED); |
Darrick J. Wong | 7b0e492 | 2019-07-01 08:25:35 -0700 | [diff] [blame] | 1032 | xfs_fill_fsxattr(ip, attr, &fa); |
Christoph Hellwig | c83bfab | 2007-10-11 17:47:00 +1000 | [diff] [blame] | 1033 | xfs_iunlock(ip, XFS_ILOCK_SHARED); |
| 1034 | |
| 1035 | if (copy_to_user(arg, &fa, sizeof(fa))) |
| 1036 | return -EFAULT; |
| 1037 | return 0; |
| 1038 | } |
| 1039 | |
Christoph Hellwig | dd60687 | 2017-09-02 08:21:20 -0700 | [diff] [blame] | 1040 | STATIC uint16_t |
| 1041 | xfs_flags2diflags( |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1042 | struct xfs_inode *ip, |
| 1043 | unsigned int xflags) |
| 1044 | { |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1045 | /* can't set PREALLOC this way, just preserve it */ |
Christoph Hellwig | dd60687 | 2017-09-02 08:21:20 -0700 | [diff] [blame] | 1046 | uint16_t di_flags = |
| 1047 | (ip->i_d.di_flags & XFS_DIFLAG_PREALLOC); |
| 1048 | |
Dave Chinner | e7b8948 | 2016-01-04 16:44:15 +1100 | [diff] [blame] | 1049 | if (xflags & FS_XFLAG_IMMUTABLE) |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1050 | di_flags |= XFS_DIFLAG_IMMUTABLE; |
Dave Chinner | e7b8948 | 2016-01-04 16:44:15 +1100 | [diff] [blame] | 1051 | if (xflags & FS_XFLAG_APPEND) |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1052 | di_flags |= XFS_DIFLAG_APPEND; |
Dave Chinner | e7b8948 | 2016-01-04 16:44:15 +1100 | [diff] [blame] | 1053 | if (xflags & FS_XFLAG_SYNC) |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1054 | di_flags |= XFS_DIFLAG_SYNC; |
Dave Chinner | e7b8948 | 2016-01-04 16:44:15 +1100 | [diff] [blame] | 1055 | if (xflags & FS_XFLAG_NOATIME) |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1056 | di_flags |= XFS_DIFLAG_NOATIME; |
Dave Chinner | e7b8948 | 2016-01-04 16:44:15 +1100 | [diff] [blame] | 1057 | if (xflags & FS_XFLAG_NODUMP) |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1058 | di_flags |= XFS_DIFLAG_NODUMP; |
Dave Chinner | e7b8948 | 2016-01-04 16:44:15 +1100 | [diff] [blame] | 1059 | if (xflags & FS_XFLAG_NODEFRAG) |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1060 | di_flags |= XFS_DIFLAG_NODEFRAG; |
Dave Chinner | e7b8948 | 2016-01-04 16:44:15 +1100 | [diff] [blame] | 1061 | if (xflags & FS_XFLAG_FILESTREAM) |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1062 | di_flags |= XFS_DIFLAG_FILESTREAM; |
Dave Chinner | c19b3b05 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 1063 | if (S_ISDIR(VFS_I(ip)->i_mode)) { |
Dave Chinner | e7b8948 | 2016-01-04 16:44:15 +1100 | [diff] [blame] | 1064 | if (xflags & FS_XFLAG_RTINHERIT) |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1065 | di_flags |= XFS_DIFLAG_RTINHERIT; |
Dave Chinner | e7b8948 | 2016-01-04 16:44:15 +1100 | [diff] [blame] | 1066 | if (xflags & FS_XFLAG_NOSYMLINKS) |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1067 | di_flags |= XFS_DIFLAG_NOSYMLINKS; |
Dave Chinner | e7b8948 | 2016-01-04 16:44:15 +1100 | [diff] [blame] | 1068 | if (xflags & FS_XFLAG_EXTSZINHERIT) |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1069 | di_flags |= XFS_DIFLAG_EXTSZINHERIT; |
Dave Chinner | e7b8948 | 2016-01-04 16:44:15 +1100 | [diff] [blame] | 1070 | if (xflags & FS_XFLAG_PROJINHERIT) |
Dave Chinner | 9336e3a | 2014-10-02 09:18:40 +1000 | [diff] [blame] | 1071 | di_flags |= XFS_DIFLAG_PROJINHERIT; |
Dave Chinner | c19b3b05 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 1072 | } else if (S_ISREG(VFS_I(ip)->i_mode)) { |
Dave Chinner | e7b8948 | 2016-01-04 16:44:15 +1100 | [diff] [blame] | 1073 | if (xflags & FS_XFLAG_REALTIME) |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1074 | di_flags |= XFS_DIFLAG_REALTIME; |
Dave Chinner | e7b8948 | 2016-01-04 16:44:15 +1100 | [diff] [blame] | 1075 | if (xflags & FS_XFLAG_EXTSIZE) |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1076 | di_flags |= XFS_DIFLAG_EXTSIZE; |
| 1077 | } |
Dave Chinner | 58f88ca | 2016-01-04 16:44:15 +1100 | [diff] [blame] | 1078 | |
Christoph Hellwig | dd60687 | 2017-09-02 08:21:20 -0700 | [diff] [blame] | 1079 | return di_flags; |
| 1080 | } |
Dave Chinner | 58f88ca | 2016-01-04 16:44:15 +1100 | [diff] [blame] | 1081 | |
Christoph Hellwig | dd60687 | 2017-09-02 08:21:20 -0700 | [diff] [blame] | 1082 | STATIC uint64_t |
| 1083 | xfs_flags2diflags2( |
| 1084 | struct xfs_inode *ip, |
| 1085 | unsigned int xflags) |
| 1086 | { |
| 1087 | uint64_t di_flags2 = |
| 1088 | (ip->i_d.di_flags2 & XFS_DIFLAG2_REFLINK); |
| 1089 | |
Dave Chinner | 58f88ca | 2016-01-04 16:44:15 +1100 | [diff] [blame] | 1090 | if (xflags & FS_XFLAG_DAX) |
| 1091 | di_flags2 |= XFS_DIFLAG2_DAX; |
Darrick J. Wong | f7ca352 | 2016-10-03 09:11:43 -0700 | [diff] [blame] | 1092 | if (xflags & FS_XFLAG_COWEXTSIZE) |
| 1093 | di_flags2 |= XFS_DIFLAG2_COWEXTSIZE; |
Dave Chinner | 58f88ca | 2016-01-04 16:44:15 +1100 | [diff] [blame] | 1094 | |
Christoph Hellwig | dd60687 | 2017-09-02 08:21:20 -0700 | [diff] [blame] | 1095 | return di_flags2; |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1096 | } |
| 1097 | |
Christoph Hellwig | f13fae2 | 2008-07-21 16:16:15 +1000 | [diff] [blame] | 1098 | STATIC void |
| 1099 | xfs_diflags_to_linux( |
| 1100 | struct xfs_inode *ip) |
| 1101 | { |
David Chinner | e4f7529 | 2008-08-13 16:00:45 +1000 | [diff] [blame] | 1102 | struct inode *inode = VFS_I(ip); |
Christoph Hellwig | f13fae2 | 2008-07-21 16:16:15 +1000 | [diff] [blame] | 1103 | unsigned int xflags = xfs_ip2xflags(ip); |
| 1104 | |
Dave Chinner | e7b8948 | 2016-01-04 16:44:15 +1100 | [diff] [blame] | 1105 | if (xflags & FS_XFLAG_IMMUTABLE) |
Christoph Hellwig | f13fae2 | 2008-07-21 16:16:15 +1000 | [diff] [blame] | 1106 | inode->i_flags |= S_IMMUTABLE; |
| 1107 | else |
| 1108 | inode->i_flags &= ~S_IMMUTABLE; |
Dave Chinner | e7b8948 | 2016-01-04 16:44:15 +1100 | [diff] [blame] | 1109 | if (xflags & FS_XFLAG_APPEND) |
Christoph Hellwig | f13fae2 | 2008-07-21 16:16:15 +1000 | [diff] [blame] | 1110 | inode->i_flags |= S_APPEND; |
| 1111 | else |
| 1112 | inode->i_flags &= ~S_APPEND; |
Dave Chinner | e7b8948 | 2016-01-04 16:44:15 +1100 | [diff] [blame] | 1113 | if (xflags & FS_XFLAG_SYNC) |
Christoph Hellwig | f13fae2 | 2008-07-21 16:16:15 +1000 | [diff] [blame] | 1114 | inode->i_flags |= S_SYNC; |
| 1115 | else |
| 1116 | inode->i_flags &= ~S_SYNC; |
Dave Chinner | e7b8948 | 2016-01-04 16:44:15 +1100 | [diff] [blame] | 1117 | if (xflags & FS_XFLAG_NOATIME) |
Christoph Hellwig | f13fae2 | 2008-07-21 16:16:15 +1000 | [diff] [blame] | 1118 | inode->i_flags |= S_NOATIME; |
| 1119 | else |
| 1120 | inode->i_flags &= ~S_NOATIME; |
Christoph Hellwig | 742d842 | 2017-08-30 09:23:01 -0700 | [diff] [blame] | 1121 | #if 0 /* disabled until the flag switching races are sorted out */ |
Dave Chinner | 58f88ca | 2016-01-04 16:44:15 +1100 | [diff] [blame] | 1122 | if (xflags & FS_XFLAG_DAX) |
| 1123 | inode->i_flags |= S_DAX; |
| 1124 | else |
| 1125 | inode->i_flags &= ~S_DAX; |
Christoph Hellwig | 742d842 | 2017-08-30 09:23:01 -0700 | [diff] [blame] | 1126 | #endif |
Christoph Hellwig | f13fae2 | 2008-07-21 16:16:15 +1000 | [diff] [blame] | 1127 | } |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1128 | |
Dave Chinner | 29a17c0 | 2015-02-02 10:14:25 +1100 | [diff] [blame] | 1129 | static int |
| 1130 | xfs_ioctl_setattr_xflags( |
| 1131 | struct xfs_trans *tp, |
| 1132 | struct xfs_inode *ip, |
| 1133 | struct fsxattr *fa) |
| 1134 | { |
| 1135 | struct xfs_mount *mp = ip->i_mount; |
Christoph Hellwig | dd60687 | 2017-09-02 08:21:20 -0700 | [diff] [blame] | 1136 | uint64_t di_flags2; |
Dave Chinner | 29a17c0 | 2015-02-02 10:14:25 +1100 | [diff] [blame] | 1137 | |
| 1138 | /* Can't change realtime flag if any extents are allocated. */ |
| 1139 | if ((ip->i_d.di_nextents || ip->i_delayed_blks) && |
Dave Chinner | e7b8948 | 2016-01-04 16:44:15 +1100 | [diff] [blame] | 1140 | XFS_IS_REALTIME_INODE(ip) != (fa->fsx_xflags & FS_XFLAG_REALTIME)) |
Dave Chinner | 29a17c0 | 2015-02-02 10:14:25 +1100 | [diff] [blame] | 1141 | return -EINVAL; |
| 1142 | |
| 1143 | /* If realtime flag is set then must have realtime device */ |
Dave Chinner | e7b8948 | 2016-01-04 16:44:15 +1100 | [diff] [blame] | 1144 | if (fa->fsx_xflags & FS_XFLAG_REALTIME) { |
Dave Chinner | 29a17c0 | 2015-02-02 10:14:25 +1100 | [diff] [blame] | 1145 | if (mp->m_sb.sb_rblocks == 0 || mp->m_sb.sb_rextsize == 0 || |
| 1146 | (ip->i_d.di_extsize % mp->m_sb.sb_rextsize)) |
| 1147 | return -EINVAL; |
| 1148 | } |
| 1149 | |
Darrick J. Wong | 1987fd7 | 2016-10-10 16:49:29 +1100 | [diff] [blame] | 1150 | /* Clear reflink if we are actually able to set the rt flag. */ |
Darrick J. Wong | c8e156a | 2016-10-03 09:11:50 -0700 | [diff] [blame] | 1151 | if ((fa->fsx_xflags & FS_XFLAG_REALTIME) && xfs_is_reflink_inode(ip)) |
Darrick J. Wong | 1987fd7 | 2016-10-10 16:49:29 +1100 | [diff] [blame] | 1152 | ip->i_d.di_flags2 &= ~XFS_DIFLAG2_REFLINK; |
Darrick J. Wong | c8e156a | 2016-10-03 09:11:50 -0700 | [diff] [blame] | 1153 | |
Darrick J. Wong | 4f435eb | 2016-10-03 09:11:50 -0700 | [diff] [blame] | 1154 | /* Don't allow us to set DAX mode for a reflinked file for now. */ |
| 1155 | if ((fa->fsx_xflags & FS_XFLAG_DAX) && xfs_is_reflink_inode(ip)) |
| 1156 | return -EINVAL; |
| 1157 | |
Christoph Hellwig | dd60687 | 2017-09-02 08:21:20 -0700 | [diff] [blame] | 1158 | /* diflags2 only valid for v3 inodes. */ |
| 1159 | di_flags2 = xfs_flags2diflags2(ip, fa->fsx_xflags); |
| 1160 | if (di_flags2 && ip->i_d.di_version < 3) |
| 1161 | return -EINVAL; |
| 1162 | |
| 1163 | ip->i_d.di_flags = xfs_flags2diflags(ip, fa->fsx_xflags); |
| 1164 | ip->i_d.di_flags2 = di_flags2; |
| 1165 | |
Dave Chinner | 29a17c0 | 2015-02-02 10:14:25 +1100 | [diff] [blame] | 1166 | xfs_diflags_to_linux(ip); |
| 1167 | xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG); |
| 1168 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 1169 | XFS_STATS_INC(mp, xs_ig_attrchg); |
Dave Chinner | 29a17c0 | 2015-02-02 10:14:25 +1100 | [diff] [blame] | 1170 | return 0; |
| 1171 | } |
| 1172 | |
Dave Chinner | 8f3d17a | 2015-02-02 10:15:35 +1100 | [diff] [blame] | 1173 | /* |
Dave Chinner | 3a6a854 | 2016-03-01 09:41:33 +1100 | [diff] [blame] | 1174 | * If we are changing DAX flags, we have to ensure the file is clean and any |
| 1175 | * cached objects in the address space are invalidated and removed. This |
| 1176 | * requires us to lock out other IO and page faults similar to a truncate |
| 1177 | * operation. The locks need to be held until the transaction has been committed |
| 1178 | * so that the cache invalidation is atomic with respect to the DAX flag |
| 1179 | * manipulation. |
| 1180 | */ |
| 1181 | static int |
| 1182 | xfs_ioctl_setattr_dax_invalidate( |
| 1183 | struct xfs_inode *ip, |
| 1184 | struct fsxattr *fa, |
| 1185 | int *join_flags) |
| 1186 | { |
| 1187 | struct inode *inode = VFS_I(ip); |
Ross Zwisler | 6851a3d | 2017-09-18 14:46:03 -0700 | [diff] [blame] | 1188 | struct super_block *sb = inode->i_sb; |
Dave Chinner | 3a6a854 | 2016-03-01 09:41:33 +1100 | [diff] [blame] | 1189 | int error; |
| 1190 | |
| 1191 | *join_flags = 0; |
| 1192 | |
| 1193 | /* |
| 1194 | * It is only valid to set the DAX flag on regular files and |
Dave Chinner | 6448543 | 2016-03-01 09:41:33 +1100 | [diff] [blame] | 1195 | * directories on filesystems where the block size is equal to the page |
Darrick J. Wong | aaacdd2 | 2018-05-31 15:07:47 -0700 | [diff] [blame] | 1196 | * size. On directories it serves as an inherited hint so we don't |
| 1197 | * have to check the device for dax support or flush pagecache. |
Dave Chinner | 3a6a854 | 2016-03-01 09:41:33 +1100 | [diff] [blame] | 1198 | */ |
Dave Chinner | 6448543 | 2016-03-01 09:41:33 +1100 | [diff] [blame] | 1199 | if (fa->fsx_xflags & FS_XFLAG_DAX) { |
Christoph Hellwig | 30fa529 | 2019-10-24 22:25:38 -0700 | [diff] [blame] | 1200 | struct xfs_buftarg *target = xfs_inode_buftarg(ip); |
| 1201 | |
| 1202 | if (!bdev_dax_supported(target->bt_bdev, sb->s_blocksize)) |
Dave Chinner | 6448543 | 2016-03-01 09:41:33 +1100 | [diff] [blame] | 1203 | return -EINVAL; |
| 1204 | } |
Dave Chinner | 3a6a854 | 2016-03-01 09:41:33 +1100 | [diff] [blame] | 1205 | |
| 1206 | /* If the DAX state is not changing, we have nothing to do here. */ |
| 1207 | if ((fa->fsx_xflags & FS_XFLAG_DAX) && IS_DAX(inode)) |
| 1208 | return 0; |
| 1209 | if (!(fa->fsx_xflags & FS_XFLAG_DAX) && !IS_DAX(inode)) |
| 1210 | return 0; |
| 1211 | |
Darrick J. Wong | aaacdd2 | 2018-05-31 15:07:47 -0700 | [diff] [blame] | 1212 | if (S_ISDIR(inode->i_mode)) |
| 1213 | return 0; |
| 1214 | |
Dave Chinner | 3a6a854 | 2016-03-01 09:41:33 +1100 | [diff] [blame] | 1215 | /* lock, flush and invalidate mapping in preparation for flag change */ |
| 1216 | xfs_ilock(ip, XFS_MMAPLOCK_EXCL | XFS_IOLOCK_EXCL); |
| 1217 | error = filemap_write_and_wait(inode->i_mapping); |
| 1218 | if (error) |
| 1219 | goto out_unlock; |
| 1220 | error = invalidate_inode_pages2(inode->i_mapping); |
| 1221 | if (error) |
| 1222 | goto out_unlock; |
| 1223 | |
| 1224 | *join_flags = XFS_MMAPLOCK_EXCL | XFS_IOLOCK_EXCL; |
| 1225 | return 0; |
| 1226 | |
| 1227 | out_unlock: |
| 1228 | xfs_iunlock(ip, XFS_MMAPLOCK_EXCL | XFS_IOLOCK_EXCL); |
| 1229 | return error; |
| 1230 | |
| 1231 | } |
| 1232 | |
| 1233 | /* |
Dave Chinner | 8f3d17a | 2015-02-02 10:15:35 +1100 | [diff] [blame] | 1234 | * Set up the transaction structure for the setattr operation, checking that we |
| 1235 | * have permission to do so. On success, return a clean transaction and the |
| 1236 | * inode locked exclusively ready for further operation specific checks. On |
| 1237 | * failure, return an error without modifying or locking the inode. |
Dave Chinner | 3a6a854 | 2016-03-01 09:41:33 +1100 | [diff] [blame] | 1238 | * |
| 1239 | * The inode might already be IO locked on call. If this is the case, it is |
| 1240 | * indicated in @join_flags and we take full responsibility for ensuring they |
| 1241 | * are unlocked from now on. Hence if we have an error here, we still have to |
| 1242 | * unlock them. Otherwise, once they are joined to the transaction, they will |
| 1243 | * be unlocked on commit/cancel. |
Dave Chinner | 8f3d17a | 2015-02-02 10:15:35 +1100 | [diff] [blame] | 1244 | */ |
| 1245 | static struct xfs_trans * |
| 1246 | xfs_ioctl_setattr_get_trans( |
Dave Chinner | 3a6a854 | 2016-03-01 09:41:33 +1100 | [diff] [blame] | 1247 | struct xfs_inode *ip, |
| 1248 | int join_flags) |
Dave Chinner | 8f3d17a | 2015-02-02 10:15:35 +1100 | [diff] [blame] | 1249 | { |
| 1250 | struct xfs_mount *mp = ip->i_mount; |
| 1251 | struct xfs_trans *tp; |
Dave Chinner | 3a6a854 | 2016-03-01 09:41:33 +1100 | [diff] [blame] | 1252 | int error = -EROFS; |
Dave Chinner | 8f3d17a | 2015-02-02 10:15:35 +1100 | [diff] [blame] | 1253 | |
| 1254 | if (mp->m_flags & XFS_MOUNT_RDONLY) |
Dave Chinner | 3a6a854 | 2016-03-01 09:41:33 +1100 | [diff] [blame] | 1255 | goto out_unlock; |
| 1256 | error = -EIO; |
Dave Chinner | 8f3d17a | 2015-02-02 10:15:35 +1100 | [diff] [blame] | 1257 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 3a6a854 | 2016-03-01 09:41:33 +1100 | [diff] [blame] | 1258 | goto out_unlock; |
Dave Chinner | 8f3d17a | 2015-02-02 10:15:35 +1100 | [diff] [blame] | 1259 | |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 1260 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp); |
Dave Chinner | 8f3d17a | 2015-02-02 10:15:35 +1100 | [diff] [blame] | 1261 | if (error) |
Darrick J. Wong | 3de5eab | 2019-04-22 16:28:34 -0700 | [diff] [blame] | 1262 | goto out_unlock; |
Dave Chinner | 8f3d17a | 2015-02-02 10:15:35 +1100 | [diff] [blame] | 1263 | |
| 1264 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
Dave Chinner | 3a6a854 | 2016-03-01 09:41:33 +1100 | [diff] [blame] | 1265 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | join_flags); |
| 1266 | join_flags = 0; |
Dave Chinner | 8f3d17a | 2015-02-02 10:15:35 +1100 | [diff] [blame] | 1267 | |
| 1268 | /* |
| 1269 | * CAP_FOWNER overrides the following restrictions: |
| 1270 | * |
| 1271 | * The user ID of the calling process must be equal to the file owner |
| 1272 | * ID, except in cases where the CAP_FSETID capability is applicable. |
| 1273 | */ |
| 1274 | if (!inode_owner_or_capable(VFS_I(ip))) { |
| 1275 | error = -EPERM; |
| 1276 | goto out_cancel; |
| 1277 | } |
| 1278 | |
| 1279 | if (mp->m_flags & XFS_MOUNT_WSYNC) |
| 1280 | xfs_trans_set_sync(tp); |
| 1281 | |
| 1282 | return tp; |
| 1283 | |
| 1284 | out_cancel: |
Christoph Hellwig | 4906e21 | 2015-06-04 13:47:56 +1000 | [diff] [blame] | 1285 | xfs_trans_cancel(tp); |
Dave Chinner | 3a6a854 | 2016-03-01 09:41:33 +1100 | [diff] [blame] | 1286 | out_unlock: |
| 1287 | if (join_flags) |
| 1288 | xfs_iunlock(ip, join_flags); |
Dave Chinner | 8f3d17a | 2015-02-02 10:15:35 +1100 | [diff] [blame] | 1289 | return ERR_PTR(error); |
| 1290 | } |
| 1291 | |
Iustin Pop | 9b94fcc | 2015-02-02 10:26:26 +1100 | [diff] [blame] | 1292 | /* |
| 1293 | * extent size hint validation is somewhat cumbersome. Rules are: |
| 1294 | * |
| 1295 | * 1. extent size hint is only valid for directories and regular files |
Dave Chinner | e7b8948 | 2016-01-04 16:44:15 +1100 | [diff] [blame] | 1296 | * 2. FS_XFLAG_EXTSIZE is only valid for regular files |
| 1297 | * 3. FS_XFLAG_EXTSZINHERIT is only valid for directories. |
Iustin Pop | 9b94fcc | 2015-02-02 10:26:26 +1100 | [diff] [blame] | 1298 | * 4. can only be changed on regular files if no extents are allocated |
| 1299 | * 5. can be changed on directories at any time |
| 1300 | * 6. extsize hint of 0 turns off hints, clears inode flags. |
| 1301 | * 7. Extent size must be a multiple of the appropriate block size. |
| 1302 | * 8. for non-realtime files, the extent size hint must be limited |
| 1303 | * to half the AG size to avoid alignment extending the extent beyond the |
| 1304 | * limits of the AG. |
Darrick J. Wong | 80e4e12 | 2017-10-17 21:37:42 -0700 | [diff] [blame] | 1305 | * |
| 1306 | * Please keep this function in sync with xfs_scrub_inode_extsize. |
Iustin Pop | 9b94fcc | 2015-02-02 10:26:26 +1100 | [diff] [blame] | 1307 | */ |
kbuild test robot | f92090e | 2015-02-05 11:13:21 +1100 | [diff] [blame] | 1308 | static int |
Dave Chinner | d4388d3c | 2015-02-02 10:22:20 +1100 | [diff] [blame] | 1309 | xfs_ioctl_setattr_check_extsize( |
| 1310 | struct xfs_inode *ip, |
| 1311 | struct fsxattr *fa) |
| 1312 | { |
| 1313 | struct xfs_mount *mp = ip->i_mount; |
Darrick J. Wong | ca29be7 | 2019-07-01 08:25:36 -0700 | [diff] [blame] | 1314 | xfs_extlen_t size; |
| 1315 | xfs_fsblock_t extsize_fsb; |
Iustin Pop | 9b94fcc | 2015-02-02 10:26:26 +1100 | [diff] [blame] | 1316 | |
Dave Chinner | c19b3b05 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 1317 | if (S_ISREG(VFS_I(ip)->i_mode) && ip->i_d.di_nextents && |
Dave Chinner | d4388d3c | 2015-02-02 10:22:20 +1100 | [diff] [blame] | 1318 | ((ip->i_d.di_extsize << mp->m_sb.sb_blocklog) != fa->fsx_extsize)) |
| 1319 | return -EINVAL; |
| 1320 | |
Darrick J. Wong | ca29be7 | 2019-07-01 08:25:36 -0700 | [diff] [blame] | 1321 | if (fa->fsx_extsize == 0) |
| 1322 | return 0; |
Dave Chinner | d4388d3c | 2015-02-02 10:22:20 +1100 | [diff] [blame] | 1323 | |
Darrick J. Wong | ca29be7 | 2019-07-01 08:25:36 -0700 | [diff] [blame] | 1324 | extsize_fsb = XFS_B_TO_FSB(mp, fa->fsx_extsize); |
| 1325 | if (extsize_fsb > MAXEXTLEN) |
| 1326 | return -EINVAL; |
| 1327 | |
| 1328 | if (XFS_IS_REALTIME_INODE(ip) || |
| 1329 | (fa->fsx_xflags & FS_XFLAG_REALTIME)) { |
| 1330 | size = mp->m_sb.sb_rextsize << mp->m_sb.sb_blocklog; |
| 1331 | } else { |
| 1332 | size = mp->m_sb.sb_blocksize; |
| 1333 | if (extsize_fsb > mp->m_sb.sb_agblocks / 2) |
Dave Chinner | d4388d3c | 2015-02-02 10:22:20 +1100 | [diff] [blame] | 1334 | return -EINVAL; |
Darrick J. Wong | ca29be7 | 2019-07-01 08:25:36 -0700 | [diff] [blame] | 1335 | } |
Dave Chinner | d4388d3c | 2015-02-02 10:22:20 +1100 | [diff] [blame] | 1336 | |
Darrick J. Wong | ca29be7 | 2019-07-01 08:25:36 -0700 | [diff] [blame] | 1337 | if (fa->fsx_extsize % size) |
| 1338 | return -EINVAL; |
Iustin Pop | 9b94fcc | 2015-02-02 10:26:26 +1100 | [diff] [blame] | 1339 | |
Dave Chinner | d4388d3c | 2015-02-02 10:22:20 +1100 | [diff] [blame] | 1340 | return 0; |
| 1341 | } |
| 1342 | |
Darrick J. Wong | f7ca352 | 2016-10-03 09:11:43 -0700 | [diff] [blame] | 1343 | /* |
| 1344 | * CoW extent size hint validation rules are: |
| 1345 | * |
| 1346 | * 1. CoW extent size hint can only be set if reflink is enabled on the fs. |
| 1347 | * The inode does not have to have any shared blocks, but it must be a v3. |
| 1348 | * 2. FS_XFLAG_COWEXTSIZE is only valid for directories and regular files; |
| 1349 | * for a directory, the hint is propagated to new files. |
| 1350 | * 3. Can be changed on files & directories at any time. |
| 1351 | * 4. CoW extsize hint of 0 turns off hints, clears inode flags. |
| 1352 | * 5. Extent size must be a multiple of the appropriate block size. |
| 1353 | * 6. The extent size hint must be limited to half the AG size to avoid |
| 1354 | * alignment extending the extent beyond the limits of the AG. |
Darrick J. Wong | 80e4e12 | 2017-10-17 21:37:42 -0700 | [diff] [blame] | 1355 | * |
| 1356 | * Please keep this function in sync with xfs_scrub_inode_cowextsize. |
Darrick J. Wong | f7ca352 | 2016-10-03 09:11:43 -0700 | [diff] [blame] | 1357 | */ |
| 1358 | static int |
| 1359 | xfs_ioctl_setattr_check_cowextsize( |
| 1360 | struct xfs_inode *ip, |
| 1361 | struct fsxattr *fa) |
| 1362 | { |
| 1363 | struct xfs_mount *mp = ip->i_mount; |
Darrick J. Wong | ca29be7 | 2019-07-01 08:25:36 -0700 | [diff] [blame] | 1364 | xfs_extlen_t size; |
| 1365 | xfs_fsblock_t cowextsize_fsb; |
Darrick J. Wong | f7ca352 | 2016-10-03 09:11:43 -0700 | [diff] [blame] | 1366 | |
| 1367 | if (!(fa->fsx_xflags & FS_XFLAG_COWEXTSIZE)) |
| 1368 | return 0; |
| 1369 | |
| 1370 | if (!xfs_sb_version_hasreflink(&ip->i_mount->m_sb) || |
| 1371 | ip->i_d.di_version != 3) |
| 1372 | return -EINVAL; |
| 1373 | |
Darrick J. Wong | ca29be7 | 2019-07-01 08:25:36 -0700 | [diff] [blame] | 1374 | if (fa->fsx_cowextsize == 0) |
| 1375 | return 0; |
| 1376 | |
| 1377 | cowextsize_fsb = XFS_B_TO_FSB(mp, fa->fsx_cowextsize); |
| 1378 | if (cowextsize_fsb > MAXEXTLEN) |
Darrick J. Wong | f7ca352 | 2016-10-03 09:11:43 -0700 | [diff] [blame] | 1379 | return -EINVAL; |
| 1380 | |
Darrick J. Wong | ca29be7 | 2019-07-01 08:25:36 -0700 | [diff] [blame] | 1381 | size = mp->m_sb.sb_blocksize; |
| 1382 | if (cowextsize_fsb > mp->m_sb.sb_agblocks / 2) |
| 1383 | return -EINVAL; |
Darrick J. Wong | f7ca352 | 2016-10-03 09:11:43 -0700 | [diff] [blame] | 1384 | |
Darrick J. Wong | ca29be7 | 2019-07-01 08:25:36 -0700 | [diff] [blame] | 1385 | if (fa->fsx_cowextsize % size) |
| 1386 | return -EINVAL; |
Darrick J. Wong | f7ca352 | 2016-10-03 09:11:43 -0700 | [diff] [blame] | 1387 | |
| 1388 | return 0; |
| 1389 | } |
| 1390 | |
kbuild test robot | f92090e | 2015-02-05 11:13:21 +1100 | [diff] [blame] | 1391 | static int |
Dave Chinner | 23bd073 | 2015-02-02 10:22:53 +1100 | [diff] [blame] | 1392 | xfs_ioctl_setattr_check_projid( |
| 1393 | struct xfs_inode *ip, |
| 1394 | struct fsxattr *fa) |
| 1395 | { |
| 1396 | /* Disallow 32bit project ids if projid32bit feature is not enabled. */ |
Darrick J. Wong | c8ce540 | 2017-06-16 11:00:05 -0700 | [diff] [blame] | 1397 | if (fa->fsx_projid > (uint16_t)-1 && |
Dave Chinner | 23bd073 | 2015-02-02 10:22:53 +1100 | [diff] [blame] | 1398 | !xfs_sb_version_hasprojid32bit(&ip->i_mount->m_sb)) |
| 1399 | return -EINVAL; |
Dave Chinner | 23bd073 | 2015-02-02 10:22:53 +1100 | [diff] [blame] | 1400 | return 0; |
| 1401 | } |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1402 | |
| 1403 | STATIC int |
| 1404 | xfs_ioctl_setattr( |
| 1405 | xfs_inode_t *ip, |
Dave Chinner | fd179b9 | 2015-02-02 10:16:25 +1100 | [diff] [blame] | 1406 | struct fsxattr *fa) |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1407 | { |
Darrick J. Wong | 7b0e492 | 2019-07-01 08:25:35 -0700 | [diff] [blame] | 1408 | struct fsxattr old_fa; |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1409 | struct xfs_mount *mp = ip->i_mount; |
| 1410 | struct xfs_trans *tp; |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 1411 | struct xfs_dquot *udqp = NULL; |
Chandra Seetharaman | 92f8ff7 | 2013-07-11 00:00:40 -0500 | [diff] [blame] | 1412 | struct xfs_dquot *pdqp = NULL; |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1413 | struct xfs_dquot *olddquot = NULL; |
| 1414 | int code; |
Dave Chinner | 3a6a854 | 2016-03-01 09:41:33 +1100 | [diff] [blame] | 1415 | int join_flags = 0; |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1416 | |
Christoph Hellwig | cca28fb | 2010-06-24 11:57:09 +1000 | [diff] [blame] | 1417 | trace_xfs_ioctl_setattr(ip); |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1418 | |
Dave Chinner | 23bd073 | 2015-02-02 10:22:53 +1100 | [diff] [blame] | 1419 | code = xfs_ioctl_setattr_check_projid(ip, fa); |
| 1420 | if (code) |
| 1421 | return code; |
Arkadiusz Mi?kiewicz | 23963e54 | 2010-08-26 10:19:43 +0000 | [diff] [blame] | 1422 | |
| 1423 | /* |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1424 | * If disk quotas is on, we make sure that the dquots do exist on disk, |
| 1425 | * before we start any other transactions. Trying to do this later |
| 1426 | * is messy. We don't care to take a readlock to look at the ids |
| 1427 | * in inode here, because we can't hold it across the trans_reserve. |
| 1428 | * If the IDs do change before we take the ilock, we're covered |
| 1429 | * because the i_*dquot fields will get updated anyway. |
| 1430 | */ |
Dave Chinner | fd179b9 | 2015-02-02 10:16:25 +1100 | [diff] [blame] | 1431 | if (XFS_IS_QUOTA_ON(mp)) { |
Christoph Hellwig | 5429515 | 2020-02-21 08:31:27 -0800 | [diff] [blame] | 1432 | code = xfs_qm_vop_dqalloc(ip, VFS_I(ip)->i_uid, |
| 1433 | VFS_I(ip)->i_gid, fa->fsx_projid, |
| 1434 | XFS_QMOPT_PQUOTA, &udqp, NULL, &pdqp); |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1435 | if (code) |
| 1436 | return code; |
| 1437 | } |
| 1438 | |
Dave Chinner | 3a6a854 | 2016-03-01 09:41:33 +1100 | [diff] [blame] | 1439 | /* |
| 1440 | * Changing DAX config may require inode locking for mapping |
| 1441 | * invalidation. These need to be held all the way to transaction commit |
| 1442 | * or cancel time, so need to be passed through to |
| 1443 | * xfs_ioctl_setattr_get_trans() so it can apply them to the join call |
| 1444 | * appropriately. |
| 1445 | */ |
| 1446 | code = xfs_ioctl_setattr_dax_invalidate(ip, fa, &join_flags); |
| 1447 | if (code) |
| 1448 | goto error_free_dquots; |
| 1449 | |
| 1450 | tp = xfs_ioctl_setattr_get_trans(ip, join_flags); |
Dave Chinner | 8f3d17a | 2015-02-02 10:15:35 +1100 | [diff] [blame] | 1451 | if (IS_ERR(tp)) { |
| 1452 | code = PTR_ERR(tp); |
| 1453 | goto error_free_dquots; |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1454 | } |
| 1455 | |
Dave Chinner | fd179b9 | 2015-02-02 10:16:25 +1100 | [diff] [blame] | 1456 | if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_PQUOTA_ON(mp) && |
Christoph Hellwig | de7a866 | 2019-11-12 08:22:54 -0800 | [diff] [blame] | 1457 | ip->i_d.di_projid != fa->fsx_projid) { |
Dave Chinner | fd179b9 | 2015-02-02 10:16:25 +1100 | [diff] [blame] | 1458 | code = xfs_qm_vop_chown_reserve(tp, ip, udqp, NULL, pdqp, |
| 1459 | capable(CAP_FOWNER) ? XFS_QMOPT_FORCE_RES : 0); |
| 1460 | if (code) /* out of quota */ |
Dave Chinner | d4388d3c | 2015-02-02 10:22:20 +1100 | [diff] [blame] | 1461 | goto error_trans_cancel; |
Dave Chinner | fd179b9 | 2015-02-02 10:16:25 +1100 | [diff] [blame] | 1462 | } |
| 1463 | |
Darrick J. Wong | 7b0e492 | 2019-07-01 08:25:35 -0700 | [diff] [blame] | 1464 | xfs_fill_fsxattr(ip, false, &old_fa); |
| 1465 | code = vfs_ioc_fssetxattr_check(VFS_I(ip), &old_fa, fa); |
| 1466 | if (code) |
| 1467 | goto error_trans_cancel; |
| 1468 | |
Dave Chinner | d4388d3c | 2015-02-02 10:22:20 +1100 | [diff] [blame] | 1469 | code = xfs_ioctl_setattr_check_extsize(ip, fa); |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1470 | if (code) |
Dave Chinner | d4388d3c | 2015-02-02 10:22:20 +1100 | [diff] [blame] | 1471 | goto error_trans_cancel; |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1472 | |
Darrick J. Wong | f7ca352 | 2016-10-03 09:11:43 -0700 | [diff] [blame] | 1473 | code = xfs_ioctl_setattr_check_cowextsize(ip, fa); |
| 1474 | if (code) |
| 1475 | goto error_trans_cancel; |
| 1476 | |
Dave Chinner | 29a17c0 | 2015-02-02 10:14:25 +1100 | [diff] [blame] | 1477 | code = xfs_ioctl_setattr_xflags(tp, ip, fa); |
| 1478 | if (code) |
Dave Chinner | d4388d3c | 2015-02-02 10:22:20 +1100 | [diff] [blame] | 1479 | goto error_trans_cancel; |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1480 | |
| 1481 | /* |
Dave Chinner | fd179b9 | 2015-02-02 10:16:25 +1100 | [diff] [blame] | 1482 | * Change file ownership. Must be the owner or privileged. CAP_FSETID |
| 1483 | * overrides the following restrictions: |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1484 | * |
Dave Chinner | fd179b9 | 2015-02-02 10:16:25 +1100 | [diff] [blame] | 1485 | * The set-user-ID and set-group-ID bits of a file will be cleared upon |
| 1486 | * successful return from chown() |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1487 | */ |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1488 | |
Dave Chinner | c19b3b05 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 1489 | if ((VFS_I(ip)->i_mode & (S_ISUID|S_ISGID)) && |
Dave Chinner | fd179b9 | 2015-02-02 10:16:25 +1100 | [diff] [blame] | 1490 | !capable_wrt_inode_uidgid(VFS_I(ip), CAP_FSETID)) |
Dave Chinner | c19b3b05 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 1491 | VFS_I(ip)->i_mode &= ~(S_ISUID|S_ISGID); |
Dave Chinner | fd179b9 | 2015-02-02 10:16:25 +1100 | [diff] [blame] | 1492 | |
| 1493 | /* Change the ownerships and register project quota modifications */ |
Christoph Hellwig | de7a866 | 2019-11-12 08:22:54 -0800 | [diff] [blame] | 1494 | if (ip->i_d.di_projid != fa->fsx_projid) { |
Dave Chinner | fd179b9 | 2015-02-02 10:16:25 +1100 | [diff] [blame] | 1495 | if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_PQUOTA_ON(mp)) { |
| 1496 | olddquot = xfs_qm_vop_chown(tp, ip, |
| 1497 | &ip->i_pdquot, pdqp); |
Dwight Engen | fd5e2aa | 2013-08-15 14:08:00 -0400 | [diff] [blame] | 1498 | } |
Dave Chinner | fd179b9 | 2015-02-02 10:16:25 +1100 | [diff] [blame] | 1499 | ASSERT(ip->i_d.di_version > 1); |
Christoph Hellwig | de7a866 | 2019-11-12 08:22:54 -0800 | [diff] [blame] | 1500 | ip->i_d.di_projid = fa->fsx_projid; |
Christoph Hellwig | f13fae2 | 2008-07-21 16:16:15 +1000 | [diff] [blame] | 1501 | } |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1502 | |
Dave Chinner | a872703 | 2014-10-02 09:20:30 +1000 | [diff] [blame] | 1503 | /* |
| 1504 | * Only set the extent size hint if we've already determined that the |
| 1505 | * extent size hint should be set on the inode. If no extent size flags |
| 1506 | * are set on the inode then unconditionally clear the extent size hint. |
| 1507 | */ |
Dave Chinner | fd179b9 | 2015-02-02 10:16:25 +1100 | [diff] [blame] | 1508 | if (ip->i_d.di_flags & (XFS_DIFLAG_EXTSIZE | XFS_DIFLAG_EXTSZINHERIT)) |
| 1509 | ip->i_d.di_extsize = fa->fsx_extsize >> mp->m_sb.sb_blocklog; |
| 1510 | else |
| 1511 | ip->i_d.di_extsize = 0; |
Darrick J. Wong | f7ca352 | 2016-10-03 09:11:43 -0700 | [diff] [blame] | 1512 | if (ip->i_d.di_version == 3 && |
| 1513 | (ip->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE)) |
| 1514 | ip->i_d.di_cowextsize = fa->fsx_cowextsize >> |
| 1515 | mp->m_sb.sb_blocklog; |
| 1516 | else |
| 1517 | ip->i_d.di_cowextsize = 0; |
Dave Chinner | a872703 | 2014-10-02 09:20:30 +1000 | [diff] [blame] | 1518 | |
Christoph Hellwig | 7039331 | 2015-06-04 13:48:08 +1000 | [diff] [blame] | 1519 | code = xfs_trans_commit(tp); |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1520 | |
| 1521 | /* |
| 1522 | * Release any dquot(s) the inode had kept before chown. |
| 1523 | */ |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 1524 | xfs_qm_dqrele(olddquot); |
| 1525 | xfs_qm_dqrele(udqp); |
Chandra Seetharaman | 92f8ff7 | 2013-07-11 00:00:40 -0500 | [diff] [blame] | 1526 | xfs_qm_dqrele(pdqp); |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1527 | |
Christoph Hellwig | 288699f | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 1528 | return code; |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1529 | |
Dave Chinner | d4388d3c | 2015-02-02 10:22:20 +1100 | [diff] [blame] | 1530 | error_trans_cancel: |
Christoph Hellwig | 4906e21 | 2015-06-04 13:47:56 +1000 | [diff] [blame] | 1531 | xfs_trans_cancel(tp); |
Dave Chinner | 8f3d17a | 2015-02-02 10:15:35 +1100 | [diff] [blame] | 1532 | error_free_dquots: |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 1533 | xfs_qm_dqrele(udqp); |
Chandra Seetharaman | 92f8ff7 | 2013-07-11 00:00:40 -0500 | [diff] [blame] | 1534 | xfs_qm_dqrele(pdqp); |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1535 | return code; |
| 1536 | } |
| 1537 | |
Christoph Hellwig | c83bfab | 2007-10-11 17:47:00 +1000 | [diff] [blame] | 1538 | STATIC int |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1539 | xfs_ioc_fssetxattr( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1540 | xfs_inode_t *ip, |
| 1541 | struct file *filp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1542 | void __user *arg) |
| 1543 | { |
| 1544 | struct fsxattr fa; |
Jan Kara | d9457dc | 2012-06-12 16:20:39 +0200 | [diff] [blame] | 1545 | int error; |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1546 | |
| 1547 | if (copy_from_user(&fa, arg, sizeof(fa))) |
| 1548 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1549 | |
Jan Kara | d9457dc | 2012-06-12 16:20:39 +0200 | [diff] [blame] | 1550 | error = mnt_want_write_file(filp); |
| 1551 | if (error) |
| 1552 | return error; |
Dave Chinner | fd179b9 | 2015-02-02 10:16:25 +1100 | [diff] [blame] | 1553 | error = xfs_ioctl_setattr(ip, &fa); |
Jan Kara | d9457dc | 2012-06-12 16:20:39 +0200 | [diff] [blame] | 1554 | mnt_drop_write_file(filp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1555 | return error; |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1556 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1557 | |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1558 | STATIC int |
| 1559 | xfs_ioc_getxflags( |
| 1560 | xfs_inode_t *ip, |
| 1561 | void __user *arg) |
| 1562 | { |
| 1563 | unsigned int flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1564 | |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1565 | flags = xfs_di2lxflags(ip->i_d.di_flags); |
| 1566 | if (copy_to_user(arg, &flags, sizeof(flags))) |
| 1567 | return -EFAULT; |
| 1568 | return 0; |
| 1569 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1570 | |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1571 | STATIC int |
| 1572 | xfs_ioc_setxflags( |
Dave Chinner | f96291f | 2015-02-02 10:15:56 +1100 | [diff] [blame] | 1573 | struct xfs_inode *ip, |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1574 | struct file *filp, |
| 1575 | void __user *arg) |
| 1576 | { |
Dave Chinner | f96291f | 2015-02-02 10:15:56 +1100 | [diff] [blame] | 1577 | struct xfs_trans *tp; |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1578 | struct fsxattr fa; |
Darrick J. Wong | 7b0e492 | 2019-07-01 08:25:35 -0700 | [diff] [blame] | 1579 | struct fsxattr old_fa; |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1580 | unsigned int flags; |
Dave Chinner | 3a6a854 | 2016-03-01 09:41:33 +1100 | [diff] [blame] | 1581 | int join_flags = 0; |
Dave Chinner | f96291f | 2015-02-02 10:15:56 +1100 | [diff] [blame] | 1582 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1583 | |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1584 | if (copy_from_user(&flags, arg, sizeof(flags))) |
| 1585 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1586 | |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1587 | if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \ |
| 1588 | FS_NOATIME_FL | FS_NODUMP_FL | \ |
| 1589 | FS_SYNC_FL)) |
| 1590 | return -EOPNOTSUPP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1591 | |
Christoph Hellwig | 25fe55e | 2008-07-18 17:13:20 +1000 | [diff] [blame] | 1592 | fa.fsx_xflags = xfs_merge_ioc_xflags(flags, xfs_ip2xflags(ip)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1593 | |
Jan Kara | d9457dc | 2012-06-12 16:20:39 +0200 | [diff] [blame] | 1594 | error = mnt_want_write_file(filp); |
| 1595 | if (error) |
| 1596 | return error; |
Dave Chinner | f96291f | 2015-02-02 10:15:56 +1100 | [diff] [blame] | 1597 | |
Dave Chinner | 3a6a854 | 2016-03-01 09:41:33 +1100 | [diff] [blame] | 1598 | /* |
| 1599 | * Changing DAX config may require inode locking for mapping |
| 1600 | * invalidation. These need to be held all the way to transaction commit |
| 1601 | * or cancel time, so need to be passed through to |
| 1602 | * xfs_ioctl_setattr_get_trans() so it can apply them to the join call |
| 1603 | * appropriately. |
| 1604 | */ |
| 1605 | error = xfs_ioctl_setattr_dax_invalidate(ip, &fa, &join_flags); |
| 1606 | if (error) |
| 1607 | goto out_drop_write; |
| 1608 | |
| 1609 | tp = xfs_ioctl_setattr_get_trans(ip, join_flags); |
Dave Chinner | f96291f | 2015-02-02 10:15:56 +1100 | [diff] [blame] | 1610 | if (IS_ERR(tp)) { |
| 1611 | error = PTR_ERR(tp); |
| 1612 | goto out_drop_write; |
| 1613 | } |
| 1614 | |
Darrick J. Wong | 7b0e492 | 2019-07-01 08:25:35 -0700 | [diff] [blame] | 1615 | xfs_fill_fsxattr(ip, false, &old_fa); |
| 1616 | error = vfs_ioc_fssetxattr_check(VFS_I(ip), &old_fa, &fa); |
| 1617 | if (error) { |
| 1618 | xfs_trans_cancel(tp); |
| 1619 | goto out_drop_write; |
| 1620 | } |
| 1621 | |
Dave Chinner | f96291f | 2015-02-02 10:15:56 +1100 | [diff] [blame] | 1622 | error = xfs_ioctl_setattr_xflags(tp, ip, &fa); |
| 1623 | if (error) { |
Christoph Hellwig | 4906e21 | 2015-06-04 13:47:56 +1000 | [diff] [blame] | 1624 | xfs_trans_cancel(tp); |
Dave Chinner | f96291f | 2015-02-02 10:15:56 +1100 | [diff] [blame] | 1625 | goto out_drop_write; |
| 1626 | } |
| 1627 | |
Christoph Hellwig | 7039331 | 2015-06-04 13:48:08 +1000 | [diff] [blame] | 1628 | error = xfs_trans_commit(tp); |
Dave Chinner | f96291f | 2015-02-02 10:15:56 +1100 | [diff] [blame] | 1629 | out_drop_write: |
Jan Kara | d9457dc | 2012-06-12 16:20:39 +0200 | [diff] [blame] | 1630 | mnt_drop_write_file(filp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1631 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1632 | } |
| 1633 | |
Christoph Hellwig | 232b5194 | 2017-10-17 14:16:19 -0700 | [diff] [blame] | 1634 | static bool |
| 1635 | xfs_getbmap_format( |
| 1636 | struct kgetbmap *p, |
| 1637 | struct getbmapx __user *u, |
| 1638 | size_t recsize) |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 1639 | { |
Christoph Hellwig | 232b5194 | 2017-10-17 14:16:19 -0700 | [diff] [blame] | 1640 | if (put_user(p->bmv_offset, &u->bmv_offset) || |
| 1641 | put_user(p->bmv_block, &u->bmv_block) || |
| 1642 | put_user(p->bmv_length, &u->bmv_length) || |
| 1643 | put_user(0, &u->bmv_count) || |
| 1644 | put_user(0, &u->bmv_entries)) |
| 1645 | return false; |
| 1646 | if (recsize < sizeof(struct getbmapx)) |
| 1647 | return true; |
| 1648 | if (put_user(0, &u->bmv_iflags) || |
| 1649 | put_user(p->bmv_oflags, &u->bmv_oflags) || |
| 1650 | put_user(0, &u->bmv_unused1) || |
| 1651 | put_user(0, &u->bmv_unused2)) |
| 1652 | return false; |
| 1653 | return true; |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 1654 | } |
| 1655 | |
| 1656 | STATIC int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1657 | xfs_ioc_getbmap( |
Christoph Hellwig | 8f3e205 | 2016-07-20 11:29:35 +1000 | [diff] [blame] | 1658 | struct file *file, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1659 | unsigned int cmd, |
| 1660 | void __user *arg) |
| 1661 | { |
Darrick J. Wong | be6324c | 2017-04-03 15:17:57 -0700 | [diff] [blame] | 1662 | struct getbmapx bmx = { 0 }; |
Christoph Hellwig | 232b5194 | 2017-10-17 14:16:19 -0700 | [diff] [blame] | 1663 | struct kgetbmap *buf; |
| 1664 | size_t recsize; |
| 1665 | int error, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1666 | |
Christoph Hellwig | 232b5194 | 2017-10-17 14:16:19 -0700 | [diff] [blame] | 1667 | switch (cmd) { |
| 1668 | case XFS_IOC_GETBMAPA: |
| 1669 | bmx.bmv_iflags = BMV_IF_ATTRFORK; |
| 1670 | /*FALLTHRU*/ |
| 1671 | case XFS_IOC_GETBMAP: |
| 1672 | if (file->f_mode & FMODE_NOCMTIME) |
| 1673 | bmx.bmv_iflags |= BMV_IF_NO_DMAPI_READ; |
| 1674 | /* struct getbmap is a strict subset of struct getbmapx. */ |
| 1675 | recsize = sizeof(struct getbmap); |
| 1676 | break; |
| 1677 | case XFS_IOC_GETBMAPX: |
| 1678 | recsize = sizeof(struct getbmapx); |
| 1679 | break; |
| 1680 | default: |
| 1681 | return -EINVAL; |
| 1682 | } |
| 1683 | |
| 1684 | if (copy_from_user(&bmx, arg, recsize)) |
Eric Sandeen | b474c7a | 2014-06-22 15:04:54 +1000 | [diff] [blame] | 1685 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1686 | |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 1687 | if (bmx.bmv_count < 2) |
Eric Sandeen | b474c7a | 2014-06-22 15:04:54 +1000 | [diff] [blame] | 1688 | return -EINVAL; |
Christoph Hellwig | 232b5194 | 2017-10-17 14:16:19 -0700 | [diff] [blame] | 1689 | if (bmx.bmv_count > ULONG_MAX / recsize) |
| 1690 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1691 | |
Christoph Hellwig | 232b5194 | 2017-10-17 14:16:19 -0700 | [diff] [blame] | 1692 | buf = kmem_zalloc_large(bmx.bmv_count * sizeof(*buf), 0); |
| 1693 | if (!buf) |
| 1694 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1695 | |
Christoph Hellwig | 232b5194 | 2017-10-17 14:16:19 -0700 | [diff] [blame] | 1696 | error = xfs_getbmap(XFS_I(file_inode(file)), &bmx, buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1697 | if (error) |
Christoph Hellwig | 232b5194 | 2017-10-17 14:16:19 -0700 | [diff] [blame] | 1698 | goto out_free_buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1699 | |
Christoph Hellwig | 232b5194 | 2017-10-17 14:16:19 -0700 | [diff] [blame] | 1700 | error = -EFAULT; |
| 1701 | if (copy_to_user(arg, &bmx, recsize)) |
| 1702 | goto out_free_buf; |
| 1703 | arg += recsize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1704 | |
Christoph Hellwig | 232b5194 | 2017-10-17 14:16:19 -0700 | [diff] [blame] | 1705 | for (i = 0; i < bmx.bmv_entries; i++) { |
| 1706 | if (!xfs_getbmap_format(buf + i, arg, recsize)) |
| 1707 | goto out_free_buf; |
| 1708 | arg += recsize; |
| 1709 | } |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 1710 | |
Christoph Hellwig | 232b5194 | 2017-10-17 14:16:19 -0700 | [diff] [blame] | 1711 | error = 0; |
| 1712 | out_free_buf: |
| 1713 | kmem_free(buf); |
Christophe JAILLET | 132bf67 | 2018-11-06 07:50:50 -0800 | [diff] [blame] | 1714 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1715 | } |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1716 | |
Darrick J. Wong | e89c041 | 2017-03-28 14:56:37 -0700 | [diff] [blame] | 1717 | struct getfsmap_info { |
| 1718 | struct xfs_mount *mp; |
Christoph Hellwig | 9d17e14 | 2017-04-21 11:24:41 -0700 | [diff] [blame] | 1719 | struct fsmap_head __user *data; |
| 1720 | unsigned int idx; |
Darrick J. Wong | e89c041 | 2017-03-28 14:56:37 -0700 | [diff] [blame] | 1721 | __u32 last_flags; |
| 1722 | }; |
| 1723 | |
| 1724 | STATIC int |
| 1725 | xfs_getfsmap_format(struct xfs_fsmap *xfm, void *priv) |
| 1726 | { |
| 1727 | struct getfsmap_info *info = priv; |
| 1728 | struct fsmap fm; |
| 1729 | |
| 1730 | trace_xfs_getfsmap_mapping(info->mp, xfm); |
| 1731 | |
| 1732 | info->last_flags = xfm->fmr_flags; |
| 1733 | xfs_fsmap_from_internal(&fm, xfm); |
Christoph Hellwig | 9d17e14 | 2017-04-21 11:24:41 -0700 | [diff] [blame] | 1734 | if (copy_to_user(&info->data->fmh_recs[info->idx++], &fm, |
| 1735 | sizeof(struct fsmap))) |
Darrick J. Wong | e89c041 | 2017-03-28 14:56:37 -0700 | [diff] [blame] | 1736 | return -EFAULT; |
| 1737 | |
Darrick J. Wong | e89c041 | 2017-03-28 14:56:37 -0700 | [diff] [blame] | 1738 | return 0; |
| 1739 | } |
| 1740 | |
| 1741 | STATIC int |
| 1742 | xfs_ioc_getfsmap( |
| 1743 | struct xfs_inode *ip, |
Christoph Hellwig | 9d17e14 | 2017-04-21 11:24:41 -0700 | [diff] [blame] | 1744 | struct fsmap_head __user *arg) |
Darrick J. Wong | e89c041 | 2017-03-28 14:56:37 -0700 | [diff] [blame] | 1745 | { |
Christoph Hellwig | ef2b67e | 2017-04-21 11:24:40 -0700 | [diff] [blame] | 1746 | struct getfsmap_info info = { NULL }; |
Darrick J. Wong | e89c041 | 2017-03-28 14:56:37 -0700 | [diff] [blame] | 1747 | struct xfs_fsmap_head xhead = {0}; |
| 1748 | struct fsmap_head head; |
| 1749 | bool aborted = false; |
| 1750 | int error; |
| 1751 | |
| 1752 | if (copy_from_user(&head, arg, sizeof(struct fsmap_head))) |
| 1753 | return -EFAULT; |
| 1754 | if (memchr_inv(head.fmh_reserved, 0, sizeof(head.fmh_reserved)) || |
| 1755 | memchr_inv(head.fmh_keys[0].fmr_reserved, 0, |
| 1756 | sizeof(head.fmh_keys[0].fmr_reserved)) || |
| 1757 | memchr_inv(head.fmh_keys[1].fmr_reserved, 0, |
| 1758 | sizeof(head.fmh_keys[1].fmr_reserved))) |
| 1759 | return -EINVAL; |
| 1760 | |
| 1761 | xhead.fmh_iflags = head.fmh_iflags; |
| 1762 | xhead.fmh_count = head.fmh_count; |
| 1763 | xfs_fsmap_to_internal(&xhead.fmh_keys[0], &head.fmh_keys[0]); |
| 1764 | xfs_fsmap_to_internal(&xhead.fmh_keys[1], &head.fmh_keys[1]); |
| 1765 | |
| 1766 | trace_xfs_getfsmap_low_key(ip->i_mount, &xhead.fmh_keys[0]); |
| 1767 | trace_xfs_getfsmap_high_key(ip->i_mount, &xhead.fmh_keys[1]); |
| 1768 | |
| 1769 | info.mp = ip->i_mount; |
Christoph Hellwig | 9d17e14 | 2017-04-21 11:24:41 -0700 | [diff] [blame] | 1770 | info.data = arg; |
Darrick J. Wong | e89c041 | 2017-03-28 14:56:37 -0700 | [diff] [blame] | 1771 | error = xfs_getfsmap(ip->i_mount, &xhead, xfs_getfsmap_format, &info); |
Darrick J. Wong | e7ee96d | 2019-08-28 14:37:57 -0700 | [diff] [blame] | 1772 | if (error == -ECANCELED) { |
Darrick J. Wong | e89c041 | 2017-03-28 14:56:37 -0700 | [diff] [blame] | 1773 | error = 0; |
| 1774 | aborted = true; |
| 1775 | } else if (error) |
| 1776 | return error; |
| 1777 | |
| 1778 | /* If we didn't abort, set the "last" flag in the last fmx */ |
Darrick J. Wong | 12e4a38 | 2017-04-23 10:45:21 -0700 | [diff] [blame] | 1779 | if (!aborted && info.idx) { |
Darrick J. Wong | e89c041 | 2017-03-28 14:56:37 -0700 | [diff] [blame] | 1780 | info.last_flags |= FMR_OF_LAST; |
Christoph Hellwig | 9d17e14 | 2017-04-21 11:24:41 -0700 | [diff] [blame] | 1781 | if (copy_to_user(&info.data->fmh_recs[info.idx - 1].fmr_flags, |
| 1782 | &info.last_flags, sizeof(info.last_flags))) |
Darrick J. Wong | e89c041 | 2017-03-28 14:56:37 -0700 | [diff] [blame] | 1783 | return -EFAULT; |
| 1784 | } |
| 1785 | |
| 1786 | /* copy back header */ |
| 1787 | head.fmh_entries = xhead.fmh_entries; |
| 1788 | head.fmh_oflags = xhead.fmh_oflags; |
| 1789 | if (copy_to_user(arg, &head, sizeof(struct fsmap_head))) |
| 1790 | return -EFAULT; |
| 1791 | |
| 1792 | return 0; |
| 1793 | } |
| 1794 | |
Darrick J. Wong | 36fd6e8 | 2017-10-17 21:37:34 -0700 | [diff] [blame] | 1795 | STATIC int |
| 1796 | xfs_ioc_scrub_metadata( |
| 1797 | struct xfs_inode *ip, |
| 1798 | void __user *arg) |
| 1799 | { |
| 1800 | struct xfs_scrub_metadata scrub; |
| 1801 | int error; |
| 1802 | |
| 1803 | if (!capable(CAP_SYS_ADMIN)) |
| 1804 | return -EPERM; |
| 1805 | |
| 1806 | if (copy_from_user(&scrub, arg, sizeof(scrub))) |
| 1807 | return -EFAULT; |
| 1808 | |
| 1809 | error = xfs_scrub_metadata(ip, &scrub); |
| 1810 | if (error) |
| 1811 | return error; |
| 1812 | |
| 1813 | if (copy_to_user(arg, &scrub, sizeof(scrub))) |
| 1814 | return -EFAULT; |
| 1815 | |
| 1816 | return 0; |
| 1817 | } |
| 1818 | |
Dave Chinner | a133d95 | 2013-08-12 20:49:48 +1000 | [diff] [blame] | 1819 | int |
| 1820 | xfs_ioc_swapext( |
| 1821 | xfs_swapext_t *sxp) |
| 1822 | { |
| 1823 | xfs_inode_t *ip, *tip; |
| 1824 | struct fd f, tmp; |
| 1825 | int error = 0; |
| 1826 | |
| 1827 | /* Pull information for the target fd */ |
| 1828 | f = fdget((int)sxp->sx_fdtarget); |
| 1829 | if (!f.file) { |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1830 | error = -EINVAL; |
Dave Chinner | a133d95 | 2013-08-12 20:49:48 +1000 | [diff] [blame] | 1831 | goto out; |
| 1832 | } |
| 1833 | |
| 1834 | if (!(f.file->f_mode & FMODE_WRITE) || |
| 1835 | !(f.file->f_mode & FMODE_READ) || |
| 1836 | (f.file->f_flags & O_APPEND)) { |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1837 | error = -EBADF; |
Dave Chinner | a133d95 | 2013-08-12 20:49:48 +1000 | [diff] [blame] | 1838 | goto out_put_file; |
| 1839 | } |
| 1840 | |
| 1841 | tmp = fdget((int)sxp->sx_fdtmp); |
| 1842 | if (!tmp.file) { |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1843 | error = -EINVAL; |
Dave Chinner | a133d95 | 2013-08-12 20:49:48 +1000 | [diff] [blame] | 1844 | goto out_put_file; |
| 1845 | } |
| 1846 | |
| 1847 | if (!(tmp.file->f_mode & FMODE_WRITE) || |
| 1848 | !(tmp.file->f_mode & FMODE_READ) || |
| 1849 | (tmp.file->f_flags & O_APPEND)) { |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1850 | error = -EBADF; |
Dave Chinner | a133d95 | 2013-08-12 20:49:48 +1000 | [diff] [blame] | 1851 | goto out_put_tmp_file; |
| 1852 | } |
| 1853 | |
| 1854 | if (IS_SWAPFILE(file_inode(f.file)) || |
| 1855 | IS_SWAPFILE(file_inode(tmp.file))) { |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1856 | error = -EINVAL; |
Dave Chinner | a133d95 | 2013-08-12 20:49:48 +1000 | [diff] [blame] | 1857 | goto out_put_tmp_file; |
| 1858 | } |
| 1859 | |
Jann Horn | 7f1b624 | 2016-07-20 10:30:30 +1000 | [diff] [blame] | 1860 | /* |
| 1861 | * We need to ensure that the fds passed in point to XFS inodes |
| 1862 | * before we cast and access them as XFS structures as we have no |
| 1863 | * control over what the user passes us here. |
| 1864 | */ |
| 1865 | if (f.file->f_op != &xfs_file_operations || |
| 1866 | tmp.file->f_op != &xfs_file_operations) { |
| 1867 | error = -EINVAL; |
| 1868 | goto out_put_tmp_file; |
| 1869 | } |
| 1870 | |
Dave Chinner | a133d95 | 2013-08-12 20:49:48 +1000 | [diff] [blame] | 1871 | ip = XFS_I(file_inode(f.file)); |
| 1872 | tip = XFS_I(file_inode(tmp.file)); |
| 1873 | |
| 1874 | if (ip->i_mount != tip->i_mount) { |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1875 | error = -EINVAL; |
Dave Chinner | a133d95 | 2013-08-12 20:49:48 +1000 | [diff] [blame] | 1876 | goto out_put_tmp_file; |
| 1877 | } |
| 1878 | |
| 1879 | if (ip->i_ino == tip->i_ino) { |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1880 | error = -EINVAL; |
Dave Chinner | a133d95 | 2013-08-12 20:49:48 +1000 | [diff] [blame] | 1881 | goto out_put_tmp_file; |
| 1882 | } |
| 1883 | |
| 1884 | if (XFS_FORCED_SHUTDOWN(ip->i_mount)) { |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1885 | error = -EIO; |
Dave Chinner | a133d95 | 2013-08-12 20:49:48 +1000 | [diff] [blame] | 1886 | goto out_put_tmp_file; |
| 1887 | } |
| 1888 | |
| 1889 | error = xfs_swap_extents(ip, tip, sxp); |
| 1890 | |
| 1891 | out_put_tmp_file: |
| 1892 | fdput(tmp); |
| 1893 | out_put_file: |
| 1894 | fdput(f); |
| 1895 | out: |
| 1896 | return error; |
| 1897 | } |
| 1898 | |
Eric Sandeen | f7664b3 | 2018-05-15 13:21:48 -0700 | [diff] [blame] | 1899 | static int |
| 1900 | xfs_ioc_getlabel( |
| 1901 | struct xfs_mount *mp, |
| 1902 | char __user *user_label) |
| 1903 | { |
| 1904 | struct xfs_sb *sbp = &mp->m_sb; |
| 1905 | char label[XFSLABEL_MAX + 1]; |
| 1906 | |
| 1907 | /* Paranoia */ |
| 1908 | BUILD_BUG_ON(sizeof(sbp->sb_fname) > FSLABEL_MAX); |
| 1909 | |
Arnd Bergmann | 4bb8b65 | 2018-06-05 19:42:45 -0700 | [diff] [blame] | 1910 | /* 1 larger than sb_fname, so this ensures a trailing NUL char */ |
| 1911 | memset(label, 0, sizeof(label)); |
Eric Sandeen | f7664b3 | 2018-05-15 13:21:48 -0700 | [diff] [blame] | 1912 | spin_lock(&mp->m_sb_lock); |
Arnd Bergmann | 4bb8b65 | 2018-06-05 19:42:45 -0700 | [diff] [blame] | 1913 | strncpy(label, sbp->sb_fname, XFSLABEL_MAX); |
Eric Sandeen | f7664b3 | 2018-05-15 13:21:48 -0700 | [diff] [blame] | 1914 | spin_unlock(&mp->m_sb_lock); |
| 1915 | |
Arnd Bergmann | 4bb8b65 | 2018-06-05 19:42:45 -0700 | [diff] [blame] | 1916 | if (copy_to_user(user_label, label, sizeof(label))) |
Eric Sandeen | f7664b3 | 2018-05-15 13:21:48 -0700 | [diff] [blame] | 1917 | return -EFAULT; |
| 1918 | return 0; |
| 1919 | } |
| 1920 | |
| 1921 | static int |
| 1922 | xfs_ioc_setlabel( |
| 1923 | struct file *filp, |
| 1924 | struct xfs_mount *mp, |
| 1925 | char __user *newlabel) |
| 1926 | { |
| 1927 | struct xfs_sb *sbp = &mp->m_sb; |
| 1928 | char label[XFSLABEL_MAX + 1]; |
| 1929 | size_t len; |
| 1930 | int error; |
| 1931 | |
| 1932 | if (!capable(CAP_SYS_ADMIN)) |
| 1933 | return -EPERM; |
| 1934 | /* |
| 1935 | * The generic ioctl allows up to FSLABEL_MAX chars, but XFS is much |
| 1936 | * smaller, at 12 bytes. We copy one more to be sure we find the |
| 1937 | * (required) NULL character to test the incoming label length. |
| 1938 | * NB: The on disk label doesn't need to be null terminated. |
| 1939 | */ |
| 1940 | if (copy_from_user(label, newlabel, XFSLABEL_MAX + 1)) |
| 1941 | return -EFAULT; |
| 1942 | len = strnlen(label, XFSLABEL_MAX + 1); |
| 1943 | if (len > sizeof(sbp->sb_fname)) |
| 1944 | return -EINVAL; |
| 1945 | |
| 1946 | error = mnt_want_write_file(filp); |
| 1947 | if (error) |
| 1948 | return error; |
| 1949 | |
| 1950 | spin_lock(&mp->m_sb_lock); |
| 1951 | memset(sbp->sb_fname, 0, sizeof(sbp->sb_fname)); |
Arnd Bergmann | 4bb8b65 | 2018-06-05 19:42:45 -0700 | [diff] [blame] | 1952 | memcpy(sbp->sb_fname, label, len); |
Eric Sandeen | f7664b3 | 2018-05-15 13:21:48 -0700 | [diff] [blame] | 1953 | spin_unlock(&mp->m_sb_lock); |
| 1954 | |
| 1955 | /* |
| 1956 | * Now we do several things to satisfy userspace. |
| 1957 | * In addition to normal logging of the primary superblock, we also |
| 1958 | * immediately write these changes to sector zero for the primary, then |
| 1959 | * update all backup supers (as xfs_db does for a label change), then |
| 1960 | * invalidate the block device page cache. This is so that any prior |
| 1961 | * buffered reads from userspace (i.e. from blkid) are invalidated, |
| 1962 | * and userspace will see the newly-written label. |
| 1963 | */ |
| 1964 | error = xfs_sync_sb_buf(mp); |
| 1965 | if (error) |
| 1966 | goto out; |
| 1967 | /* |
| 1968 | * growfs also updates backup supers so lock against that. |
| 1969 | */ |
| 1970 | mutex_lock(&mp->m_growlock); |
| 1971 | error = xfs_update_secondary_sbs(mp); |
| 1972 | mutex_unlock(&mp->m_growlock); |
| 1973 | |
| 1974 | invalidate_bdev(mp->m_ddev_targp->bt_bdev); |
| 1975 | |
| 1976 | out: |
| 1977 | mnt_drop_write_file(filp); |
| 1978 | return error; |
| 1979 | } |
| 1980 | |
Christoph Hellwig | 4d4be48 | 2008-12-09 04:47:33 -0500 | [diff] [blame] | 1981 | /* |
| 1982 | * Note: some of the ioctl's return positive numbers as a |
| 1983 | * byte count indicating success, such as readlink_by_handle. |
| 1984 | * So we don't "sign flip" like most other routines. This means |
| 1985 | * true errors need to be returned as a negative value. |
| 1986 | */ |
| 1987 | long |
| 1988 | xfs_file_ioctl( |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1989 | struct file *filp, |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1990 | unsigned int cmd, |
Christoph Hellwig | 4d4be48 | 2008-12-09 04:47:33 -0500 | [diff] [blame] | 1991 | unsigned long p) |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1992 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1993 | struct inode *inode = file_inode(filp); |
Christoph Hellwig | 4d4be48 | 2008-12-09 04:47:33 -0500 | [diff] [blame] | 1994 | struct xfs_inode *ip = XFS_I(inode); |
| 1995 | struct xfs_mount *mp = ip->i_mount; |
| 1996 | void __user *arg = (void __user *)p; |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 1997 | int error; |
| 1998 | |
Christoph Hellwig | cca28fb | 2010-06-24 11:57:09 +1000 | [diff] [blame] | 1999 | trace_xfs_file_ioctl(ip); |
Christoph Hellwig | 4d4be48 | 2008-12-09 04:47:33 -0500 | [diff] [blame] | 2000 | |
| 2001 | switch (cmd) { |
Christoph Hellwig | a46db60 | 2011-01-07 13:02:04 +0000 | [diff] [blame] | 2002 | case FITRIM: |
| 2003 | return xfs_ioc_trim(mp, arg); |
Eric Sandeen | f7664b3 | 2018-05-15 13:21:48 -0700 | [diff] [blame] | 2004 | case FS_IOC_GETFSLABEL: |
| 2005 | return xfs_ioc_getlabel(mp, arg); |
| 2006 | case FS_IOC_SETFSLABEL: |
| 2007 | return xfs_ioc_setlabel(filp, mp, arg); |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 2008 | case XFS_IOC_ALLOCSP: |
| 2009 | case XFS_IOC_FREESP: |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 2010 | case XFS_IOC_ALLOCSP64: |
Christoph Hellwig | 837a6e7 | 2019-10-24 22:26:02 -0700 | [diff] [blame] | 2011 | case XFS_IOC_FREESP64: { |
sandeen@sandeen.net | 743bb465 | 2008-11-25 21:20:06 -0600 | [diff] [blame] | 2012 | xfs_flock64_t bf; |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 2013 | |
sandeen@sandeen.net | 743bb465 | 2008-11-25 21:20:06 -0600 | [diff] [blame] | 2014 | if (copy_from_user(&bf, arg, sizeof(bf))) |
Eric Sandeen | b474c7a | 2014-06-22 15:04:54 +1000 | [diff] [blame] | 2015 | return -EFAULT; |
Christoph Hellwig | 837a6e7 | 2019-10-24 22:26:02 -0700 | [diff] [blame] | 2016 | return xfs_ioc_space(filp, &bf); |
sandeen@sandeen.net | 743bb465 | 2008-11-25 21:20:06 -0600 | [diff] [blame] | 2017 | } |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 2018 | case XFS_IOC_DIOINFO: { |
Christoph Hellwig | c7d6831 | 2019-10-24 22:25:39 -0700 | [diff] [blame] | 2019 | struct xfs_buftarg *target = xfs_inode_buftarg(ip); |
| 2020 | struct dioattr da; |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 2021 | |
Eric Sandeen | 7c71ee7 | 2014-01-21 16:46:23 -0600 | [diff] [blame] | 2022 | da.d_mem = da.d_miniosz = target->bt_logical_sectorsize; |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 2023 | da.d_maxiosz = INT_MAX & ~(da.d_miniosz - 1); |
| 2024 | |
| 2025 | if (copy_to_user(arg, &da, sizeof(da))) |
Eric Sandeen | b474c7a | 2014-06-22 15:04:54 +1000 | [diff] [blame] | 2026 | return -EFAULT; |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 2027 | return 0; |
| 2028 | } |
| 2029 | |
| 2030 | case XFS_IOC_FSBULKSTAT_SINGLE: |
| 2031 | case XFS_IOC_FSBULKSTAT: |
| 2032 | case XFS_IOC_FSINUMBERS: |
Darrick J. Wong | 8bfe9d1 | 2019-07-03 20:36:26 -0700 | [diff] [blame] | 2033 | return xfs_ioc_fsbulkstat(mp, cmd, arg); |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 2034 | |
Darrick J. Wong | 0448b6f | 2019-07-03 20:36:27 -0700 | [diff] [blame] | 2035 | case XFS_IOC_BULKSTAT: |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 2036 | return xfs_ioc_bulkstat(mp, cmd, arg); |
Darrick J. Wong | fba9760 | 2019-07-03 20:36:28 -0700 | [diff] [blame] | 2037 | case XFS_IOC_INUMBERS: |
| 2038 | return xfs_ioc_inumbers(mp, cmd, arg); |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 2039 | |
| 2040 | case XFS_IOC_FSGEOMETRY_V1: |
Dave Chinner | 1b6d968 | 2019-04-12 07:41:16 -0700 | [diff] [blame] | 2041 | return xfs_ioc_fsgeometry(mp, arg, 3); |
| 2042 | case XFS_IOC_FSGEOMETRY_V4: |
| 2043 | return xfs_ioc_fsgeometry(mp, arg, 4); |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 2044 | case XFS_IOC_FSGEOMETRY: |
Dave Chinner | 1b6d968 | 2019-04-12 07:41:16 -0700 | [diff] [blame] | 2045 | return xfs_ioc_fsgeometry(mp, arg, 5); |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 2046 | |
Darrick J. Wong | 7cd5006 | 2019-04-12 07:41:17 -0700 | [diff] [blame] | 2047 | case XFS_IOC_AG_GEOMETRY: |
| 2048 | return xfs_ioc_ag_geometry(mp, arg); |
| 2049 | |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 2050 | case XFS_IOC_GETVERSION: |
| 2051 | return put_user(inode->i_generation, (int __user *)arg); |
| 2052 | |
| 2053 | case XFS_IOC_FSGETXATTR: |
| 2054 | return xfs_ioc_fsgetxattr(ip, 0, arg); |
| 2055 | case XFS_IOC_FSGETXATTRA: |
| 2056 | return xfs_ioc_fsgetxattr(ip, 1, arg); |
Lachlan McIlroy | 3b2816b | 2008-04-18 12:43:35 +1000 | [diff] [blame] | 2057 | case XFS_IOC_FSSETXATTR: |
Lachlan McIlroy | 65e67f5 | 2008-04-18 12:59:45 +1000 | [diff] [blame] | 2058 | return xfs_ioc_fssetxattr(ip, filp, arg); |
| 2059 | case XFS_IOC_GETXFLAGS: |
| 2060 | return xfs_ioc_getxflags(ip, arg); |
| 2061 | case XFS_IOC_SETXFLAGS: |
| 2062 | return xfs_ioc_setxflags(ip, filp, arg); |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 2063 | |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 2064 | case XFS_IOC_GETBMAP: |
| 2065 | case XFS_IOC_GETBMAPA: |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 2066 | case XFS_IOC_GETBMAPX: |
Christoph Hellwig | 232b5194 | 2017-10-17 14:16:19 -0700 | [diff] [blame] | 2067 | return xfs_ioc_getbmap(filp, cmd, arg); |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 2068 | |
Darrick J. Wong | e89c041 | 2017-03-28 14:56:37 -0700 | [diff] [blame] | 2069 | case FS_IOC_GETFSMAP: |
| 2070 | return xfs_ioc_getfsmap(ip, arg); |
| 2071 | |
Darrick J. Wong | 36fd6e8 | 2017-10-17 21:37:34 -0700 | [diff] [blame] | 2072 | case XFS_IOC_SCRUB_METADATA: |
| 2073 | return xfs_ioc_scrub_metadata(ip, arg); |
| 2074 | |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 2075 | case XFS_IOC_FD_TO_HANDLE: |
| 2076 | case XFS_IOC_PATH_TO_HANDLE: |
sandeen@sandeen.net | 743bb465 | 2008-11-25 21:20:06 -0600 | [diff] [blame] | 2077 | case XFS_IOC_PATH_TO_FSHANDLE: { |
| 2078 | xfs_fsop_handlereq_t hreq; |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 2079 | |
sandeen@sandeen.net | 743bb465 | 2008-11-25 21:20:06 -0600 | [diff] [blame] | 2080 | if (copy_from_user(&hreq, arg, sizeof(hreq))) |
Eric Sandeen | b474c7a | 2014-06-22 15:04:54 +1000 | [diff] [blame] | 2081 | return -EFAULT; |
sandeen@sandeen.net | 743bb465 | 2008-11-25 21:20:06 -0600 | [diff] [blame] | 2082 | return xfs_find_handle(cmd, &hreq); |
| 2083 | } |
| 2084 | case XFS_IOC_OPEN_BY_HANDLE: { |
| 2085 | xfs_fsop_handlereq_t hreq; |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 2086 | |
sandeen@sandeen.net | 743bb465 | 2008-11-25 21:20:06 -0600 | [diff] [blame] | 2087 | if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t))) |
Eric Sandeen | b474c7a | 2014-06-22 15:04:54 +1000 | [diff] [blame] | 2088 | return -EFAULT; |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 2089 | return xfs_open_by_handle(filp, &hreq); |
sandeen@sandeen.net | 743bb465 | 2008-11-25 21:20:06 -0600 | [diff] [blame] | 2090 | } |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 2091 | |
sandeen@sandeen.net | 743bb465 | 2008-11-25 21:20:06 -0600 | [diff] [blame] | 2092 | case XFS_IOC_READLINK_BY_HANDLE: { |
| 2093 | xfs_fsop_handlereq_t hreq; |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 2094 | |
sandeen@sandeen.net | 743bb465 | 2008-11-25 21:20:06 -0600 | [diff] [blame] | 2095 | if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t))) |
Eric Sandeen | b474c7a | 2014-06-22 15:04:54 +1000 | [diff] [blame] | 2096 | return -EFAULT; |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 2097 | return xfs_readlink_by_handle(filp, &hreq); |
sandeen@sandeen.net | 743bb465 | 2008-11-25 21:20:06 -0600 | [diff] [blame] | 2098 | } |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 2099 | case XFS_IOC_ATTRLIST_BY_HANDLE: |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 2100 | return xfs_attrlist_by_handle(filp, arg); |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 2101 | |
| 2102 | case XFS_IOC_ATTRMULTI_BY_HANDLE: |
Christoph Hellwig | d296d30 | 2009-01-19 02:02:57 +0100 | [diff] [blame] | 2103 | return xfs_attrmulti_by_handle(filp, arg); |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 2104 | |
| 2105 | case XFS_IOC_SWAPEXT: { |
sandeen@sandeen.net | 743bb465 | 2008-11-25 21:20:06 -0600 | [diff] [blame] | 2106 | struct xfs_swapext sxp; |
| 2107 | |
| 2108 | if (copy_from_user(&sxp, arg, sizeof(xfs_swapext_t))) |
Eric Sandeen | b474c7a | 2014-06-22 15:04:54 +1000 | [diff] [blame] | 2109 | return -EFAULT; |
Jan Kara | d9457dc | 2012-06-12 16:20:39 +0200 | [diff] [blame] | 2110 | error = mnt_want_write_file(filp); |
| 2111 | if (error) |
| 2112 | return error; |
Dave Chinner | a133d95 | 2013-08-12 20:49:48 +1000 | [diff] [blame] | 2113 | error = xfs_ioc_swapext(&sxp); |
Jan Kara | d9457dc | 2012-06-12 16:20:39 +0200 | [diff] [blame] | 2114 | mnt_drop_write_file(filp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 2115 | return error; |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 2116 | } |
| 2117 | |
| 2118 | case XFS_IOC_FSCOUNTS: { |
| 2119 | xfs_fsop_counts_t out; |
| 2120 | |
Eric Sandeen | 9108326 | 2019-05-01 20:26:30 -0700 | [diff] [blame] | 2121 | xfs_fs_counts(mp, &out); |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 2122 | |
| 2123 | if (copy_to_user(arg, &out, sizeof(out))) |
Eric Sandeen | b474c7a | 2014-06-22 15:04:54 +1000 | [diff] [blame] | 2124 | return -EFAULT; |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 2125 | return 0; |
| 2126 | } |
| 2127 | |
| 2128 | case XFS_IOC_SET_RESBLKS: { |
| 2129 | xfs_fsop_resblks_t inout; |
Darrick J. Wong | c8ce540 | 2017-06-16 11:00:05 -0700 | [diff] [blame] | 2130 | uint64_t in; |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 2131 | |
| 2132 | if (!capable(CAP_SYS_ADMIN)) |
| 2133 | return -EPERM; |
| 2134 | |
Eric Sandeen | d5db0f9 | 2010-02-05 22:59:53 +0000 | [diff] [blame] | 2135 | if (mp->m_flags & XFS_MOUNT_RDONLY) |
Eric Sandeen | b474c7a | 2014-06-22 15:04:54 +1000 | [diff] [blame] | 2136 | return -EROFS; |
Eric Sandeen | d5db0f9 | 2010-02-05 22:59:53 +0000 | [diff] [blame] | 2137 | |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 2138 | if (copy_from_user(&inout, arg, sizeof(inout))) |
Eric Sandeen | b474c7a | 2014-06-22 15:04:54 +1000 | [diff] [blame] | 2139 | return -EFAULT; |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 2140 | |
Jan Kara | d9457dc | 2012-06-12 16:20:39 +0200 | [diff] [blame] | 2141 | error = mnt_want_write_file(filp); |
| 2142 | if (error) |
| 2143 | return error; |
| 2144 | |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 2145 | /* input parameter is passed in resblks field of structure */ |
| 2146 | in = inout.resblks; |
| 2147 | error = xfs_reserve_blocks(mp, &in, &inout); |
Jan Kara | d9457dc | 2012-06-12 16:20:39 +0200 | [diff] [blame] | 2148 | mnt_drop_write_file(filp); |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 2149 | if (error) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 2150 | return error; |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 2151 | |
| 2152 | if (copy_to_user(arg, &inout, sizeof(inout))) |
Eric Sandeen | b474c7a | 2014-06-22 15:04:54 +1000 | [diff] [blame] | 2153 | return -EFAULT; |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 2154 | return 0; |
| 2155 | } |
| 2156 | |
| 2157 | case XFS_IOC_GET_RESBLKS: { |
| 2158 | xfs_fsop_resblks_t out; |
| 2159 | |
| 2160 | if (!capable(CAP_SYS_ADMIN)) |
| 2161 | return -EPERM; |
| 2162 | |
| 2163 | error = xfs_reserve_blocks(mp, NULL, &out); |
| 2164 | if (error) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 2165 | return error; |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 2166 | |
| 2167 | if (copy_to_user(arg, &out, sizeof(out))) |
Eric Sandeen | b474c7a | 2014-06-22 15:04:54 +1000 | [diff] [blame] | 2168 | return -EFAULT; |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 2169 | |
| 2170 | return 0; |
| 2171 | } |
| 2172 | |
| 2173 | case XFS_IOC_FSGROWFSDATA: { |
| 2174 | xfs_growfs_data_t in; |
| 2175 | |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 2176 | if (copy_from_user(&in, arg, sizeof(in))) |
Eric Sandeen | b474c7a | 2014-06-22 15:04:54 +1000 | [diff] [blame] | 2177 | return -EFAULT; |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 2178 | |
Jan Kara | d9457dc | 2012-06-12 16:20:39 +0200 | [diff] [blame] | 2179 | error = mnt_want_write_file(filp); |
| 2180 | if (error) |
| 2181 | return error; |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 2182 | error = xfs_growfs_data(mp, &in); |
Jan Kara | d9457dc | 2012-06-12 16:20:39 +0200 | [diff] [blame] | 2183 | mnt_drop_write_file(filp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 2184 | return error; |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 2185 | } |
| 2186 | |
| 2187 | case XFS_IOC_FSGROWFSLOG: { |
| 2188 | xfs_growfs_log_t in; |
| 2189 | |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 2190 | if (copy_from_user(&in, arg, sizeof(in))) |
Eric Sandeen | b474c7a | 2014-06-22 15:04:54 +1000 | [diff] [blame] | 2191 | return -EFAULT; |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 2192 | |
Jan Kara | d9457dc | 2012-06-12 16:20:39 +0200 | [diff] [blame] | 2193 | error = mnt_want_write_file(filp); |
| 2194 | if (error) |
| 2195 | return error; |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 2196 | error = xfs_growfs_log(mp, &in); |
Jan Kara | d9457dc | 2012-06-12 16:20:39 +0200 | [diff] [blame] | 2197 | mnt_drop_write_file(filp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 2198 | return error; |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 2199 | } |
| 2200 | |
| 2201 | case XFS_IOC_FSGROWFSRT: { |
| 2202 | xfs_growfs_rt_t in; |
| 2203 | |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 2204 | if (copy_from_user(&in, arg, sizeof(in))) |
Eric Sandeen | b474c7a | 2014-06-22 15:04:54 +1000 | [diff] [blame] | 2205 | return -EFAULT; |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 2206 | |
Jan Kara | d9457dc | 2012-06-12 16:20:39 +0200 | [diff] [blame] | 2207 | error = mnt_want_write_file(filp); |
| 2208 | if (error) |
| 2209 | return error; |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 2210 | error = xfs_growfs_rt(mp, &in); |
Jan Kara | d9457dc | 2012-06-12 16:20:39 +0200 | [diff] [blame] | 2211 | mnt_drop_write_file(filp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 2212 | return error; |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 2213 | } |
| 2214 | |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 2215 | case XFS_IOC_GOINGDOWN: { |
Darrick J. Wong | c8ce540 | 2017-06-16 11:00:05 -0700 | [diff] [blame] | 2216 | uint32_t in; |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 2217 | |
| 2218 | if (!capable(CAP_SYS_ADMIN)) |
| 2219 | return -EPERM; |
| 2220 | |
Darrick J. Wong | c8ce540 | 2017-06-16 11:00:05 -0700 | [diff] [blame] | 2221 | if (get_user(in, (uint32_t __user *)arg)) |
Eric Sandeen | b474c7a | 2014-06-22 15:04:54 +1000 | [diff] [blame] | 2222 | return -EFAULT; |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 2223 | |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 2224 | return xfs_fs_goingdown(mp, in); |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 2225 | } |
| 2226 | |
| 2227 | case XFS_IOC_ERROR_INJECTION: { |
| 2228 | xfs_error_injection_t in; |
| 2229 | |
| 2230 | if (!capable(CAP_SYS_ADMIN)) |
| 2231 | return -EPERM; |
| 2232 | |
| 2233 | if (copy_from_user(&in, arg, sizeof(in))) |
Eric Sandeen | b474c7a | 2014-06-22 15:04:54 +1000 | [diff] [blame] | 2234 | return -EFAULT; |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 2235 | |
Darrick J. Wong | 31965ef | 2017-06-20 17:54:46 -0700 | [diff] [blame] | 2236 | return xfs_errortag_add(mp, in.errtag); |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 2237 | } |
| 2238 | |
| 2239 | case XFS_IOC_ERROR_CLEARALL: |
| 2240 | if (!capable(CAP_SYS_ADMIN)) |
| 2241 | return -EPERM; |
| 2242 | |
Darrick J. Wong | 31965ef | 2017-06-20 17:54:46 -0700 | [diff] [blame] | 2243 | return xfs_errortag_clearall(mp); |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 2244 | |
Brian Foster | 8ca149d | 2012-11-07 12:21:12 -0500 | [diff] [blame] | 2245 | case XFS_IOC_FREE_EOFBLOCKS: { |
Dwight Engen | b9fe505 | 2013-08-15 14:08:02 -0400 | [diff] [blame] | 2246 | struct xfs_fs_eofblocks eofb; |
| 2247 | struct xfs_eofblocks keofb; |
Brian Foster | 8ca149d | 2012-11-07 12:21:12 -0500 | [diff] [blame] | 2248 | |
Dwight Engen | 8c567a7 | 2013-08-15 14:08:03 -0400 | [diff] [blame] | 2249 | if (!capable(CAP_SYS_ADMIN)) |
| 2250 | return -EPERM; |
| 2251 | |
| 2252 | if (mp->m_flags & XFS_MOUNT_RDONLY) |
Eric Sandeen | b474c7a | 2014-06-22 15:04:54 +1000 | [diff] [blame] | 2253 | return -EROFS; |
Dwight Engen | 8c567a7 | 2013-08-15 14:08:03 -0400 | [diff] [blame] | 2254 | |
Brian Foster | 8ca149d | 2012-11-07 12:21:12 -0500 | [diff] [blame] | 2255 | if (copy_from_user(&eofb, arg, sizeof(eofb))) |
Eric Sandeen | b474c7a | 2014-06-22 15:04:54 +1000 | [diff] [blame] | 2256 | return -EFAULT; |
Brian Foster | 8ca149d | 2012-11-07 12:21:12 -0500 | [diff] [blame] | 2257 | |
Dwight Engen | b9fe505 | 2013-08-15 14:08:02 -0400 | [diff] [blame] | 2258 | error = xfs_fs_eofblocks_from_user(&eofb, &keofb); |
| 2259 | if (error) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 2260 | return error; |
Brian Foster | 8ca149d | 2012-11-07 12:21:12 -0500 | [diff] [blame] | 2261 | |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 2262 | return xfs_icache_free_eofblocks(mp, &keofb); |
Brian Foster | 8ca149d | 2012-11-07 12:21:12 -0500 | [diff] [blame] | 2263 | } |
| 2264 | |
Lachlan McIlroy | df26cfe | 2008-04-18 11:44:03 +1000 | [diff] [blame] | 2265 | default: |
| 2266 | return -ENOTTY; |
| 2267 | } |
| 2268 | } |