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