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