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