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