blob: 287f83eb791f5c025f92a2a3ff2d7cfc5dfa57f0 [file] [log] [blame]
Dave Chinner0b61f8a2018-06-05 19:42:14 -07001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Nathan Scott7b718762005-11-02 14:58:39 +11003 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include "xfs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include "xfs_fs.h"
Dave Chinner70a98832013-10-23 10:36:05 +11008#include "xfs_shared.h"
Dave Chinner239880e2013-10-23 10:50:10 +11009#include "xfs_format.h"
10#include "xfs_log_format.h"
11#include "xfs_trans_resv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include "xfs_inode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include "xfs_rtalloc.h"
Darrick J. Wong2810bd62019-07-02 09:39:40 -070015#include "xfs_iwalk.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include "xfs_itable.h"
Nathan Scotta844f452005-11-02 14:38:42 +110017#include "xfs_error.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs_attr.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_bmap.h"
Dave Chinner68988112013-08-12 20:49:42 +100020#include "xfs_bmap_util.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "xfs_fsops.h"
Christoph Hellwiga46db602011-01-07 13:02:04 +000022#include "xfs_discard.h"
Christoph Hellwig25fe55e2008-07-18 17:13:20 +100023#include "xfs_quota.h"
Christoph Hellwigd296d302009-01-19 02:02:57 +010024#include "xfs_export.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000025#include "xfs_trace.h"
Brian Foster8ca149d2012-11-07 12:21:12 -050026#include "xfs_icache.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110027#include "xfs_trans.h"
Andreas Gruenbacher47e1bf62015-11-03 12:56:17 +110028#include "xfs_acl.h"
Darrick J. Wonge89c0412017-03-28 14:56:37 -070029#include "xfs_btree.h"
30#include <linux/fsmap.h>
31#include "xfs_fsmap.h"
Darrick J. Wong36fd6e82017-10-17 21:37:34 -070032#include "scrub/xfs_scrub.h"
Darrick J. Wongc368ebc2018-01-08 10:51:27 -080033#include "xfs_sb.h"
Darrick J. Wong7cd50062019-04-12 07:41:17 -070034#include "xfs_ag.h"
Darrick J. Wongc23232d2019-04-12 07:41:17 -070035#include "xfs_health.h"
Christoph Hellwig7a42c702019-10-24 22:26:27 -070036#include "xfs_reflink.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/mount.h>
39#include <linux/namei.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41/*
42 * xfs_find_handle maps from userspace xfs_fsop_handlereq structure to
43 * a file or fs handle.
44 *
45 * XFS_IOC_PATH_TO_FSHANDLE
46 * returns fs handle for a mount point or path within that mount point
47 * XFS_IOC_FD_TO_HANDLE
48 * returns full handle for a FD opened in user space
49 * XFS_IOC_PATH_TO_HANDLE
50 * returns full handle for a path
51 */
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -060052int
Linus Torvalds1da177e2005-04-16 15:20:36 -070053xfs_find_handle(
54 unsigned int cmd,
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -060055 xfs_fsop_handlereq_t *hreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
57 int hsize;
58 xfs_handle_t handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 struct inode *inode;
Dave Chinnera30b0362013-09-02 20:49:36 +100060 struct fd f = {NULL};
Christoph Hellwig4346cdd2009-02-08 21:51:14 +010061 struct path path;
Al Viro2903ff02012-08-28 12:52:22 -040062 int error;
Christoph Hellwig4346cdd2009-02-08 21:51:14 +010063 struct xfs_inode *ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Christoph Hellwig4346cdd2009-02-08 21:51:14 +010065 if (cmd == XFS_IOC_FD_TO_HANDLE) {
Al Viro2903ff02012-08-28 12:52:22 -040066 f = fdget(hreq->fd);
67 if (!f.file)
Christoph Hellwig4346cdd2009-02-08 21:51:14 +010068 return -EBADF;
Al Viro496ad9a2013-01-23 17:07:38 -050069 inode = file_inode(f.file);
Christoph Hellwig4346cdd2009-02-08 21:51:14 +010070 } else {
Al Viroce6595a2019-07-14 16:42:44 -040071 error = user_path_at(AT_FDCWD, hreq->path, 0, &path);
Christoph Hellwig4346cdd2009-02-08 21:51:14 +010072 if (error)
73 return error;
David Howells2b0143b2015-03-17 22:25:59 +000074 inode = d_inode(path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 }
Christoph Hellwig4346cdd2009-02-08 21:51:14 +010076 ip = XFS_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Christoph Hellwig4346cdd2009-02-08 21:51:14 +010078 /*
79 * We can only generate handles for inodes residing on a XFS filesystem,
80 * and only for regular files, directories or symbolic links.
81 */
82 error = -EINVAL;
83 if (inode->i_sb->s_magic != XFS_SB_MAGIC)
84 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Christoph Hellwig4346cdd2009-02-08 21:51:14 +010086 error = -EBADF;
87 if (!S_ISREG(inode->i_mode) &&
88 !S_ISDIR(inode->i_mode) &&
89 !S_ISLNK(inode->i_mode))
90 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Christoph Hellwig4346cdd2009-02-08 21:51:14 +010093 memcpy(&handle.ha_fsid, ip->i_mount->m_fixedfsid, sizeof(xfs_fsid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Christoph Hellwig4346cdd2009-02-08 21:51:14 +010095 if (cmd == XFS_IOC_PATH_TO_FSHANDLE) {
96 /*
97 * This handle only contains an fsid, zero the rest.
98 */
99 memset(&handle.ha_fid, 0, sizeof(handle.ha_fid));
100 hsize = sizeof(xfs_fsid_t);
101 } else {
Christoph Hellwigc6143912007-09-14 15:22:37 +1000102 handle.ha_fid.fid_len = sizeof(xfs_fid_t) -
103 sizeof(handle.ha_fid.fid_len);
104 handle.ha_fid.fid_pad = 0;
Dave Chinner9e9a2672016-02-09 16:54:58 +1100105 handle.ha_fid.fid_gen = inode->i_generation;
Christoph Hellwigc6143912007-09-14 15:22:37 +1000106 handle.ha_fid.fid_ino = ip->i_ino;
Christoph Hellwig3398a402017-06-14 21:30:44 -0700107 hsize = sizeof(xfs_handle_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 }
109
Christoph Hellwig4346cdd2009-02-08 21:51:14 +0100110 error = -EFAULT;
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -0600111 if (copy_to_user(hreq->ohandle, &handle, hsize) ||
Christoph Hellwig4346cdd2009-02-08 21:51:14 +0100112 copy_to_user(hreq->ohandlen, &hsize, sizeof(__s32)))
113 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Christoph Hellwig4346cdd2009-02-08 21:51:14 +0100115 error = 0;
116
117 out_put:
118 if (cmd == XFS_IOC_FD_TO_HANDLE)
Al Viro2903ff02012-08-28 12:52:22 -0400119 fdput(f);
Christoph Hellwig4346cdd2009-02-08 21:51:14 +0100120 else
121 path_put(&path);
122 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123}
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125/*
Christoph Hellwigd296d302009-01-19 02:02:57 +0100126 * No need to do permission checks on the various pathname components
127 * as the handle operations are privileged.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 */
129STATIC int
Christoph Hellwigd296d302009-01-19 02:02:57 +0100130xfs_handle_acceptable(
131 void *context,
132 struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
Christoph Hellwigd296d302009-01-19 02:02:57 +0100134 return 1;
135}
136
137/*
138 * Convert userspace handle data into a dentry.
139 */
140struct dentry *
141xfs_handle_to_dentry(
142 struct file *parfilp,
143 void __user *uhandle,
144 u32 hlen)
145{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 xfs_handle_t handle;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100147 struct xfs_fid64 fid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149 /*
150 * Only allow handle opens under a directory.
151 */
Al Viro496ad9a2013-01-23 17:07:38 -0500152 if (!S_ISDIR(file_inode(parfilp)->i_mode))
Christoph Hellwigd296d302009-01-19 02:02:57 +0100153 return ERR_PTR(-ENOTDIR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Christoph Hellwigd296d302009-01-19 02:02:57 +0100155 if (hlen != sizeof(xfs_handle_t))
156 return ERR_PTR(-EINVAL);
157 if (copy_from_user(&handle, uhandle, hlen))
158 return ERR_PTR(-EFAULT);
159 if (handle.ha_fid.fid_len !=
160 sizeof(handle.ha_fid) - sizeof(handle.ha_fid.fid_len))
161 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Christoph Hellwigd296d302009-01-19 02:02:57 +0100163 memset(&fid, 0, sizeof(struct fid));
164 fid.ino = handle.ha_fid.fid_ino;
165 fid.gen = handle.ha_fid.fid_gen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Christoph Hellwigd296d302009-01-19 02:02:57 +0100167 return exportfs_decode_fh(parfilp->f_path.mnt, (struct fid *)&fid, 3,
168 FILEID_INO32_GEN | XFS_FILEID_TYPE_64FLAG,
169 xfs_handle_acceptable, NULL);
170}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Christoph Hellwigd296d302009-01-19 02:02:57 +0100172STATIC struct dentry *
173xfs_handlereq_to_dentry(
174 struct file *parfilp,
175 xfs_fsop_handlereq_t *hreq)
176{
177 return xfs_handle_to_dentry(parfilp, hreq->ihandle, hreq->ihandlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178}
179
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -0600180int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181xfs_open_by_handle(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 struct file *parfilp,
Christoph Hellwigd296d302009-01-19 02:02:57 +0100183 xfs_fsop_handlereq_t *hreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
David Howells745ca242008-11-14 10:39:22 +1100185 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 int error;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100187 int fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 int permflag;
189 struct file *filp;
190 struct inode *inode;
191 struct dentry *dentry;
Dave Chinner1a1d7722012-03-22 05:15:06 +0000192 fmode_t fmode;
Al Viro765927b2012-06-26 21:58:53 +0400193 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
195 if (!capable(CAP_SYS_ADMIN))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000196 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Christoph Hellwigd296d302009-01-19 02:02:57 +0100198 dentry = xfs_handlereq_to_dentry(parfilp, hreq);
199 if (IS_ERR(dentry))
200 return PTR_ERR(dentry);
David Howells2b0143b2015-03-17 22:25:59 +0000201 inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203 /* Restrict xfs_open_by_handle to directories & regular files. */
204 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode))) {
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000205 error = -EPERM;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100206 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 }
208
209#if BITS_PER_LONG != 32
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -0600210 hreq->oflags |= O_LARGEFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211#endif
Christoph Hellwigd296d302009-01-19 02:02:57 +0100212
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -0600213 permflag = hreq->oflags;
Dave Chinner1a1d7722012-03-22 05:15:06 +0000214 fmode = OPEN_FMODE(permflag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 if ((!(permflag & O_APPEND) || (permflag & O_TRUNC)) &&
Dave Chinner1a1d7722012-03-22 05:15:06 +0000216 (fmode & FMODE_WRITE) && IS_APPEND(inode)) {
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000217 error = -EPERM;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100218 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 }
220
Dave Chinner1a1d7722012-03-22 05:15:06 +0000221 if ((fmode & FMODE_WRITE) && IS_IMMUTABLE(inode)) {
Eryu Guan337684a2016-08-02 19:58:28 +0800222 error = -EPERM;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100223 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 }
225
226 /* Can't write directories. */
Dave Chinner1a1d7722012-03-22 05:15:06 +0000227 if (S_ISDIR(inode->i_mode) && (fmode & FMODE_WRITE)) {
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000228 error = -EISDIR;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100229 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 }
231
Yann Droneaud862a6292013-07-02 18:39:34 +0200232 fd = get_unused_fd_flags(0);
Christoph Hellwigd296d302009-01-19 02:02:57 +0100233 if (fd < 0) {
234 error = fd;
235 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 }
237
Al Viro765927b2012-06-26 21:58:53 +0400238 path.mnt = parfilp->f_path.mnt;
239 path.dentry = dentry;
240 filp = dentry_open(&path, hreq->oflags, cred);
241 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 if (IS_ERR(filp)) {
Christoph Hellwigd296d302009-01-19 02:02:57 +0100243 put_unused_fd(fd);
244 return PTR_ERR(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 }
Christoph Hellwig4d4be482008-12-09 04:47:33 -0500246
Al Viro03209372011-07-25 20:54:24 -0400247 if (S_ISREG(inode->i_mode)) {
Vlad Apostolov2e2e7bb2006-11-11 18:04:47 +1100248 filp->f_flags |= O_NOATIME;
Christoph Hellwig4d4be482008-12-09 04:47:33 -0500249 filp->f_mode |= FMODE_NOCMTIME;
Vlad Apostolov2e2e7bb2006-11-11 18:04:47 +1100250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Christoph Hellwigd296d302009-01-19 02:02:57 +0100252 fd_install(fd, filp);
253 return fd;
254
255 out_dput:
256 dput(dentry);
257 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258}
259
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -0600260int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261xfs_readlink_by_handle(
Christoph Hellwigd296d302009-01-19 02:02:57 +0100262 struct file *parfilp,
263 xfs_fsop_handlereq_t *hreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
Christoph Hellwigd296d302009-01-19 02:02:57 +0100265 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 __u32 olen;
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000267 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269 if (!capable(CAP_SYS_ADMIN))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000270 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Christoph Hellwigd296d302009-01-19 02:02:57 +0100272 dentry = xfs_handlereq_to_dentry(parfilp, hreq);
273 if (IS_ERR(dentry))
274 return PTR_ERR(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276 /* Restrict this handle operation to symlinks only. */
Miklos Szeredifd4a0edf2016-12-09 16:45:04 +0100277 if (!d_is_symlink(dentry)) {
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000278 error = -EINVAL;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100279 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 }
281
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -0600282 if (copy_from_user(&olen, hreq->ohandlen, sizeof(__u32))) {
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000283 error = -EFAULT;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100284 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Miklos Szeredifd4a0edf2016-12-09 16:45:04 +0100287 error = vfs_readlink(dentry, hreq->ohandle, olen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Christoph Hellwigd296d302009-01-19 02:02:57 +0100289 out_dput:
290 dput(dentry);
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000291 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292}
293
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000294int
295xfs_set_dmattrs(
296 xfs_inode_t *ip,
Darrick J. Wong65a79352017-11-09 09:34:28 -0800297 uint evmask,
298 uint16_t state)
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000299{
300 xfs_mount_t *mp = ip->i_mount;
301 xfs_trans_t *tp;
302 int error;
303
304 if (!capable(CAP_SYS_ADMIN))
Dave Chinner24513372014-06-25 14:58:08 +1000305 return -EPERM;
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000306
307 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +1000308 return -EIO;
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000309
Christoph Hellwig253f4912016-04-06 09:19:55 +1000310 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
311 if (error)
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000312 return error;
Christoph Hellwig253f4912016-04-06 09:19:55 +1000313
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000314 xfs_ilock(ip, XFS_ILOCK_EXCL);
315 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
316
317 ip->i_d.di_dmevmask = evmask;
318 ip->i_d.di_dmstate = state;
319
320 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Christoph Hellwig70393312015-06-04 13:48:08 +1000321 error = xfs_trans_commit(tp);
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000322
323 return error;
324}
325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326STATIC int
327xfs_fssetdm_by_handle(
Christoph Hellwigd296d302009-01-19 02:02:57 +0100328 struct file *parfilp,
329 void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
331 int error;
332 struct fsdmidata fsd;
333 xfs_fsop_setdm_handlereq_t dmhreq;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100334 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336 if (!capable(CAP_MKNOD))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000337 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 if (copy_from_user(&dmhreq, arg, sizeof(xfs_fsop_setdm_handlereq_t)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000339 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Jan Karad9457dc2012-06-12 16:20:39 +0200341 error = mnt_want_write_file(parfilp);
342 if (error)
343 return error;
344
Christoph Hellwigd296d302009-01-19 02:02:57 +0100345 dentry = xfs_handlereq_to_dentry(parfilp, &dmhreq.hreq);
Jan Karad9457dc2012-06-12 16:20:39 +0200346 if (IS_ERR(dentry)) {
347 mnt_drop_write_file(parfilp);
Christoph Hellwigd296d302009-01-19 02:02:57 +0100348 return PTR_ERR(dentry);
Jan Karad9457dc2012-06-12 16:20:39 +0200349 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
David Howells2b0143b2015-03-17 22:25:59 +0000351 if (IS_IMMUTABLE(d_inode(dentry)) || IS_APPEND(d_inode(dentry))) {
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000352 error = -EPERM;
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000353 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 }
355
356 if (copy_from_user(&fsd, dmhreq.data, sizeof(fsd))) {
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000357 error = -EFAULT;
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000358 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 }
360
David Howells2b0143b2015-03-17 22:25:59 +0000361 error = xfs_set_dmattrs(XFS_I(d_inode(dentry)), fsd.fsd_dmevmask,
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000362 fsd.fsd_dmstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000364 out:
Jan Karad9457dc2012-06-12 16:20:39 +0200365 mnt_drop_write_file(parfilp);
Christoph Hellwigd296d302009-01-19 02:02:57 +0100366 dput(dentry);
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000367 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368}
369
370STATIC int
371xfs_attrlist_by_handle(
Christoph Hellwigd296d302009-01-19 02:02:57 +0100372 struct file *parfilp,
373 void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
Christoph Hellwigd296d302009-01-19 02:02:57 +0100375 int error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 attrlist_cursor_kern_t *cursor;
Darrick J. Wong0facef72016-08-03 10:58:53 +1000377 struct xfs_fsop_attrlist_handlereq __user *p = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 xfs_fsop_attrlist_handlereq_t al_hreq;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100379 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 char *kbuf;
381
382 if (!capable(CAP_SYS_ADMIN))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000383 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 if (copy_from_user(&al_hreq, arg, sizeof(xfs_fsop_attrlist_handlereq_t)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000385 return -EFAULT;
Dan Carpenter071c5292013-10-31 21:00:10 +0300386 if (al_hreq.buflen < sizeof(struct attrlist) ||
Jan Tulak4e247612015-10-12 16:02:56 +1100387 al_hreq.buflen > XFS_XATTR_LIST_MAX)
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000388 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Christoph Hellwig90ad58a2008-06-27 13:32:19 +1000390 /*
391 * Reject flags, only allow namespaces.
392 */
393 if (al_hreq.flags & ~(ATTR_ROOT | ATTR_SECURE))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000394 return -EINVAL;
Christoph Hellwig90ad58a2008-06-27 13:32:19 +1000395
Christoph Hellwigd296d302009-01-19 02:02:57 +0100396 dentry = xfs_handlereq_to_dentry(parfilp, &al_hreq.hreq);
397 if (IS_ERR(dentry))
398 return PTR_ERR(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Tetsuo Handa707e0dd2019-08-26 12:06:22 -0700400 kbuf = kmem_zalloc_large(al_hreq.buflen, 0);
Dave Chinnerfdd3cce2013-09-02 20:53:00 +1000401 if (!kbuf)
402 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404 cursor = (attrlist_cursor_kern_t *)&al_hreq.pos;
David Howells2b0143b2015-03-17 22:25:59 +0000405 error = xfs_attr_list(XFS_I(d_inode(dentry)), kbuf, al_hreq.buflen,
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000406 al_hreq.flags, cursor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 if (error)
408 goto out_kfree;
409
Darrick J. Wong0facef72016-08-03 10:58:53 +1000410 if (copy_to_user(&p->pos, cursor, sizeof(attrlist_cursor_kern_t))) {
411 error = -EFAULT;
412 goto out_kfree;
413 }
414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 if (copy_to_user(al_hreq.buffer, kbuf, al_hreq.buflen))
416 error = -EFAULT;
417
Dave Chinnerfdd3cce2013-09-02 20:53:00 +1000418out_kfree:
419 kmem_free(kbuf);
420out_dput:
Christoph Hellwigd296d302009-01-19 02:02:57 +0100421 dput(dentry);
422 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423}
424
sandeen@sandeen.net28750972008-11-25 21:20:15 -0600425int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426xfs_attrmulti_attr_get(
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000427 struct inode *inode,
Dave Chinnera9273ca2010-01-20 10:47:48 +1100428 unsigned char *name,
429 unsigned char __user *ubuf,
Darrick J. Wongc8ce5402017-06-16 11:00:05 -0700430 uint32_t *len,
431 uint32_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
Dave Chinnera9273ca2010-01-20 10:47:48 +1100433 unsigned char *kbuf;
Dave Chinner24513372014-06-25 14:58:08 +1000434 int error = -EFAULT;
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000435
Jan Tulak51fcbfe2015-10-12 16:03:59 +1100436 if (*len > XFS_XATTR_SIZE_MAX)
Dave Chinner24513372014-06-25 14:58:08 +1000437 return -EINVAL;
Tetsuo Handa707e0dd2019-08-26 12:06:22 -0700438 kbuf = kmem_zalloc_large(*len, 0);
Dave Chinnerfdd3cce2013-09-02 20:53:00 +1000439 if (!kbuf)
Dave Chinner24513372014-06-25 14:58:08 +1000440 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Dave Chinnerddbca702019-08-29 09:04:10 -0700442 error = xfs_attr_get(XFS_I(inode), name, &kbuf, (int *)len, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 if (error)
444 goto out_kfree;
445
446 if (copy_to_user(ubuf, kbuf, *len))
Dave Chinner24513372014-06-25 14:58:08 +1000447 error = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Dave Chinnerfdd3cce2013-09-02 20:53:00 +1000449out_kfree:
450 kmem_free(kbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 return error;
452}
453
sandeen@sandeen.net28750972008-11-25 21:20:15 -0600454int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455xfs_attrmulti_attr_set(
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000456 struct inode *inode,
Dave Chinnera9273ca2010-01-20 10:47:48 +1100457 unsigned char *name,
458 const unsigned char __user *ubuf,
Darrick J. Wongc8ce5402017-06-16 11:00:05 -0700459 uint32_t len,
460 uint32_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
Dave Chinnera9273ca2010-01-20 10:47:48 +1100462 unsigned char *kbuf;
Andreas Gruenbacher09cb22d2015-11-03 12:53:54 +1100463 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000465 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
Dave Chinner24513372014-06-25 14:58:08 +1000466 return -EPERM;
Jan Tulak51fcbfe2015-10-12 16:03:59 +1100467 if (len > XFS_XATTR_SIZE_MAX)
Dave Chinner24513372014-06-25 14:58:08 +1000468 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Li Zefan0e639bd2009-04-08 15:08:04 +0800470 kbuf = memdup_user(ubuf, len);
471 if (IS_ERR(kbuf))
472 return PTR_ERR(kbuf);
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000473
Andreas Gruenbacher09cb22d2015-11-03 12:53:54 +1100474 error = xfs_attr_set(XFS_I(inode), name, kbuf, len, flags);
Andreas Gruenbacher47e1bf62015-11-03 12:56:17 +1100475 if (!error)
476 xfs_forget_acl(inode, name, flags);
Andreas Gruenbacher09cb22d2015-11-03 12:53:54 +1100477 kfree(kbuf);
478 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479}
480
sandeen@sandeen.net28750972008-11-25 21:20:15 -0600481int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482xfs_attrmulti_attr_remove(
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000483 struct inode *inode,
Dave Chinnera9273ca2010-01-20 10:47:48 +1100484 unsigned char *name,
Darrick J. Wongc8ce5402017-06-16 11:00:05 -0700485 uint32_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
Andreas Gruenbacher47e1bf62015-11-03 12:56:17 +1100487 int error;
488
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000489 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
Dave Chinner24513372014-06-25 14:58:08 +1000490 return -EPERM;
Andreas Gruenbacher47e1bf62015-11-03 12:56:17 +1100491 error = xfs_attr_remove(XFS_I(inode), name, flags);
492 if (!error)
493 xfs_forget_acl(inode, name, flags);
494 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495}
496
497STATIC int
498xfs_attrmulti_by_handle(
Dave Hansen42a74f22008-02-15 14:37:46 -0800499 struct file *parfilp,
Christoph Hellwigd296d302009-01-19 02:02:57 +0100500 void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
502 int error;
503 xfs_attr_multiop_t *ops;
504 xfs_fsop_attrmulti_handlereq_t am_hreq;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100505 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 unsigned int i, size;
Dave Chinnera9273ca2010-01-20 10:47:48 +1100507 unsigned char *attr_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
509 if (!capable(CAP_SYS_ADMIN))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000510 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 if (copy_from_user(&am_hreq, arg, sizeof(xfs_fsop_attrmulti_handlereq_t)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000512 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Zhitong Wangfda168c2010-03-23 09:51:22 +1100514 /* overflow check */
515 if (am_hreq.opcount >= INT_MAX / sizeof(xfs_attr_multiop_t))
516 return -E2BIG;
517
Christoph Hellwigd296d302009-01-19 02:02:57 +0100518 dentry = xfs_handlereq_to_dentry(parfilp, &am_hreq.hreq);
519 if (IS_ERR(dentry))
520 return PTR_ERR(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Dave Chinner24513372014-06-25 14:58:08 +1000522 error = -E2BIG;
Christoph Hellwige182f572008-06-27 13:32:31 +1000523 size = am_hreq.opcount * sizeof(xfs_attr_multiop_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 if (!size || size > 16 * PAGE_SIZE)
Christoph Hellwigd296d302009-01-19 02:02:57 +0100525 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Li Zefan0e639bd2009-04-08 15:08:04 +0800527 ops = memdup_user(am_hreq.ops, size);
528 if (IS_ERR(ops)) {
Dave Chinner24513372014-06-25 14:58:08 +1000529 error = PTR_ERR(ops);
Christoph Hellwigd296d302009-01-19 02:02:57 +0100530 goto out_dput;
Li Zefan0e639bd2009-04-08 15:08:04 +0800531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Dave Chinner24513372014-06-25 14:58:08 +1000533 error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 attr_name = kmalloc(MAXNAMELEN, GFP_KERNEL);
535 if (!attr_name)
536 goto out_kfree_ops;
537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 error = 0;
539 for (i = 0; i < am_hreq.opcount; i++) {
Dave Chinnera9273ca2010-01-20 10:47:48 +1100540 ops[i].am_error = strncpy_from_user((char *)attr_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 ops[i].am_attrname, MAXNAMELEN);
542 if (ops[i].am_error == 0 || ops[i].am_error == MAXNAMELEN)
Dave Chinner24513372014-06-25 14:58:08 +1000543 error = -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 if (ops[i].am_error < 0)
545 break;
546
547 switch (ops[i].am_opcode) {
548 case ATTR_OP_GET:
Christoph Hellwigd296d302009-01-19 02:02:57 +0100549 ops[i].am_error = xfs_attrmulti_attr_get(
David Howells2b0143b2015-03-17 22:25:59 +0000550 d_inode(dentry), attr_name,
Christoph Hellwigd296d302009-01-19 02:02:57 +0100551 ops[i].am_attrvalue, &ops[i].am_length,
552 ops[i].am_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 break;
554 case ATTR_OP_SET:
Al Viroa561be72011-11-23 11:57:51 -0500555 ops[i].am_error = mnt_want_write_file(parfilp);
Dave Hansen42a74f22008-02-15 14:37:46 -0800556 if (ops[i].am_error)
557 break;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100558 ops[i].am_error = xfs_attrmulti_attr_set(
David Howells2b0143b2015-03-17 22:25:59 +0000559 d_inode(dentry), attr_name,
Christoph Hellwigd296d302009-01-19 02:02:57 +0100560 ops[i].am_attrvalue, ops[i].am_length,
561 ops[i].am_flags);
Al Viro2a79f172011-12-09 08:06:57 -0500562 mnt_drop_write_file(parfilp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 break;
564 case ATTR_OP_REMOVE:
Al Viroa561be72011-11-23 11:57:51 -0500565 ops[i].am_error = mnt_want_write_file(parfilp);
Dave Hansen42a74f22008-02-15 14:37:46 -0800566 if (ops[i].am_error)
567 break;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100568 ops[i].am_error = xfs_attrmulti_attr_remove(
David Howells2b0143b2015-03-17 22:25:59 +0000569 d_inode(dentry), attr_name,
Christoph Hellwigd296d302009-01-19 02:02:57 +0100570 ops[i].am_flags);
Al Viro2a79f172011-12-09 08:06:57 -0500571 mnt_drop_write_file(parfilp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 break;
573 default:
Dave Chinner24513372014-06-25 14:58:08 +1000574 ops[i].am_error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 }
576 }
577
578 if (copy_to_user(am_hreq.ops, ops, size))
Dave Chinner24513372014-06-25 14:58:08 +1000579 error = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581 kfree(attr_name);
582 out_kfree_ops:
583 kfree(ops);
Christoph Hellwigd296d302009-01-19 02:02:57 +0100584 out_dput:
585 dput(dentry);
Dave Chinner24513372014-06-25 14:58:08 +1000586 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587}
588
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -0600589int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590xfs_ioc_space(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 struct file *filp,
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -0600592 xfs_flock64_t *bf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593{
Christoph Hellwig8f3e2052016-07-20 11:29:35 +1000594 struct inode *inode = file_inode(filp);
595 struct xfs_inode *ip = XFS_I(inode);
Christoph Hellwig865e9442013-10-12 00:55:08 -0700596 struct iattr iattr;
Christoph Hellwig837a6e72019-10-24 22:26:02 -0700597 enum xfs_prealloc_flags flags = XFS_PREALLOC_CLEAR;
Dan Williamsc63a8ea2018-03-12 14:12:29 -0700598 uint iolock = XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 int error;
600
Alexey Dobriyanf37ea142006-09-28 10:52:04 +1000601 if (inode->i_flags & (S_IMMUTABLE|S_APPEND))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000602 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Eric Sandeenad4a8ac2005-09-02 16:41:16 +1000604 if (!(filp->f_mode & FMODE_WRITE))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000605 return -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Alexey Dobriyanf37ea142006-09-28 10:52:04 +1000607 if (!S_ISREG(inode->i_mode))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000608 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Christoph Hellwig7a42c702019-10-24 22:26:27 -0700610 if (xfs_is_always_cow_inode(ip))
611 return -EOPNOTSUPP;
612
Christoph Hellwig8add71c2015-02-02 09:53:56 +1100613 if (filp->f_flags & O_DSYNC)
614 flags |= XFS_PREALLOC_SYNC;
Christoph Hellwig8f3e2052016-07-20 11:29:35 +1000615 if (filp->f_mode & FMODE_NOCMTIME)
Christoph Hellwig8add71c2015-02-02 09:53:56 +1100616 flags |= XFS_PREALLOC_INVISIBLE;
617
Jan Karad9457dc2012-06-12 16:20:39 +0200618 error = mnt_want_write_file(filp);
619 if (error)
620 return error;
Christoph Hellwig865e9442013-10-12 00:55:08 -0700621
Christoph Hellwig781355c2015-02-16 11:59:50 +1100622 xfs_ilock(ip, iolock);
Dan Williams69eb5fa2018-03-20 14:42:38 -0700623 error = xfs_break_layouts(inode, &iolock, BREAK_UNMAP);
Christoph Hellwig781355c2015-02-16 11:59:50 +1100624 if (error)
625 goto out_unlock;
Christoph Hellwig865e9442013-10-12 00:55:08 -0700626
627 switch (bf->l_whence) {
628 case 0: /*SEEK_SET*/
629 break;
630 case 1: /*SEEK_CUR*/
631 bf->l_start += filp->f_pos;
632 break;
633 case 2: /*SEEK_END*/
634 bf->l_start += XFS_ISIZE(ip);
635 break;
636 default:
Dave Chinner24513372014-06-25 14:58:08 +1000637 error = -EINVAL;
Christoph Hellwig865e9442013-10-12 00:55:08 -0700638 goto out_unlock;
639 }
640
Christoph Hellwig837a6e72019-10-24 22:26:02 -0700641 if (bf->l_start < 0 || bf->l_start > inode->i_sb->s_maxbytes) {
Dave Chinner24513372014-06-25 14:58:08 +1000642 error = -EINVAL;
Christoph Hellwig865e9442013-10-12 00:55:08 -0700643 goto out_unlock;
644 }
645
Christoph Hellwig837a6e72019-10-24 22:26:02 -0700646 if (bf->l_start > XFS_ISIZE(ip)) {
647 error = xfs_alloc_file_space(ip, XFS_ISIZE(ip),
648 bf->l_start - XFS_ISIZE(ip), 0);
649 if (error)
650 goto out_unlock;
Christoph Hellwig865e9442013-10-12 00:55:08 -0700651 }
652
Christoph Hellwig837a6e72019-10-24 22:26:02 -0700653 iattr.ia_valid = ATTR_SIZE;
654 iattr.ia_size = bf->l_start;
655 error = xfs_vn_setattr_size(file_dentry(filp), &iattr);
Christoph Hellwig865e9442013-10-12 00:55:08 -0700656 if (error)
657 goto out_unlock;
658
Christoph Hellwig8add71c2015-02-02 09:53:56 +1100659 error = xfs_update_prealloc_flags(ip, flags);
Christoph Hellwig865e9442013-10-12 00:55:08 -0700660
661out_unlock:
Christoph Hellwig781355c2015-02-16 11:59:50 +1100662 xfs_iunlock(ip, iolock);
Jan Karad9457dc2012-06-12 16:20:39 +0200663 mnt_drop_write_file(filp);
Dave Chinner24513372014-06-25 14:58:08 +1000664 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665}
666
Darrick J. Wong2810bd62019-07-02 09:39:40 -0700667/* Return 0 on success or positive error */
668int
Darrick J. Wong8bfe9d12019-07-03 20:36:26 -0700669xfs_fsbulkstat_one_fmt(
Darrick J. Wong7035f972019-07-03 20:36:26 -0700670 struct xfs_ibulk *breq,
671 const struct xfs_bulkstat *bstat)
Darrick J. Wong2810bd62019-07-02 09:39:40 -0700672{
Darrick J. Wong7035f972019-07-03 20:36:26 -0700673 struct xfs_bstat bs1;
674
675 xfs_bulkstat_to_bstat(breq->mp, &bs1, bstat);
676 if (copy_to_user(breq->ubuffer, &bs1, sizeof(bs1)))
Darrick J. Wong2810bd62019-07-02 09:39:40 -0700677 return -EFAULT;
678 return xfs_ibulk_advance(breq, sizeof(struct xfs_bstat));
679}
680
Darrick J. Wong677717f2019-07-02 09:39:43 -0700681int
Darrick J. Wong8bfe9d12019-07-03 20:36:26 -0700682xfs_fsinumbers_fmt(
Darrick J. Wong5f19c7f2019-07-03 20:36:27 -0700683 struct xfs_ibulk *breq,
684 const struct xfs_inumbers *igrp)
Darrick J. Wong677717f2019-07-02 09:39:43 -0700685{
Darrick J. Wong5f19c7f2019-07-03 20:36:27 -0700686 struct xfs_inogrp ig1;
687
688 xfs_inumbers_to_inogrp(&ig1, igrp);
689 if (copy_to_user(breq->ubuffer, &ig1, sizeof(struct xfs_inogrp)))
Darrick J. Wong677717f2019-07-02 09:39:43 -0700690 return -EFAULT;
691 return xfs_ibulk_advance(breq, sizeof(struct xfs_inogrp));
692}
693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694STATIC int
Darrick J. Wong8bfe9d12019-07-03 20:36:26 -0700695xfs_ioc_fsbulkstat(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 xfs_mount_t *mp,
697 unsigned int cmd,
698 void __user *arg)
699{
Darrick J. Wong2810bd62019-07-02 09:39:40 -0700700 struct xfs_fsop_bulkreq bulkreq;
701 struct xfs_ibulk breq = {
702 .mp = mp,
703 .ocount = 0,
704 };
705 xfs_ino_t lastino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 int error;
707
708 /* done = 1 if there are more stats to get and if bulkstat */
709 /* should be called again (unused here, but used in dmapi) */
710
711 if (!capable(CAP_SYS_ADMIN))
712 return -EPERM;
713
714 if (XFS_FORCED_SHUTDOWN(mp))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000715 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Darrick J. Wong6f71fb62019-07-03 20:36:25 -0700717 if (copy_from_user(&bulkreq, arg, sizeof(struct xfs_fsop_bulkreq)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000718 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Darrick J. Wong2810bd62019-07-02 09:39:40 -0700720 if (copy_from_user(&lastino, bulkreq.lastip, sizeof(__s64)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000721 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Darrick J. Wong2810bd62019-07-02 09:39:40 -0700723 if (bulkreq.icount <= 0)
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000724 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100726 if (bulkreq.ubuffer == NULL)
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000727 return -EINVAL;
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100728
Darrick J. Wong2810bd62019-07-02 09:39:40 -0700729 breq.ubuffer = bulkreq.ubuffer;
730 breq.icount = bulkreq.icount;
731
732 /*
733 * FSBULKSTAT_SINGLE expects that *lastip contains the inode number
734 * that we want to stat. However, FSINUMBERS and FSBULKSTAT expect
735 * that *lastip contains either zero or the number of the last inode to
736 * be examined by the previous call and return results starting with
737 * the next inode after that. The new bulk request back end functions
738 * take the inode to start with, so we have to compute the startino
739 * parameter from lastino to maintain correct function. lastino == 0
740 * is a special case because it has traditionally meant "first inode
741 * in filesystem".
742 */
743 if (cmd == XFS_IOC_FSINUMBERS) {
Darrick J. Wong677717f2019-07-02 09:39:43 -0700744 breq.startino = lastino ? lastino + 1 : 0;
Darrick J. Wong8bfe9d12019-07-03 20:36:26 -0700745 error = xfs_inumbers(&breq, xfs_fsinumbers_fmt);
Darrick J. Wong677717f2019-07-02 09:39:43 -0700746 lastino = breq.startino - 1;
Darrick J. Wong2810bd62019-07-02 09:39:40 -0700747 } else if (cmd == XFS_IOC_FSBULKSTAT_SINGLE) {
748 breq.startino = lastino;
749 breq.icount = 1;
Darrick J. Wong8bfe9d12019-07-03 20:36:26 -0700750 error = xfs_bulkstat_one(&breq, xfs_fsbulkstat_one_fmt);
Darrick J. Wong2810bd62019-07-02 09:39:40 -0700751 } else { /* XFS_IOC_FSBULKSTAT */
752 breq.startino = lastino ? lastino + 1 : 0;
Darrick J. Wong8bfe9d12019-07-03 20:36:26 -0700753 error = xfs_bulkstat(&breq, xfs_fsbulkstat_one_fmt);
Darrick J. Wong2810bd62019-07-02 09:39:40 -0700754 lastino = breq.startino - 1;
755 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
757 if (error)
Dave Chinner24513372014-06-25 14:58:08 +1000758 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Darrick J. Wongf16fe3e2019-07-02 09:39:39 -0700760 if (bulkreq.lastip != NULL &&
Darrick J. Wong2810bd62019-07-02 09:39:40 -0700761 copy_to_user(bulkreq.lastip, &lastino, sizeof(xfs_ino_t)))
Darrick J. Wongf16fe3e2019-07-02 09:39:39 -0700762 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Darrick J. Wongf16fe3e2019-07-02 09:39:39 -0700764 if (bulkreq.ocount != NULL &&
Darrick J. Wong2810bd62019-07-02 09:39:40 -0700765 copy_to_user(bulkreq.ocount, &breq.ocount, sizeof(__s32)))
Darrick J. Wongf16fe3e2019-07-02 09:39:39 -0700766 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
768 return 0;
769}
770
Darrick J. Wong0448b6f2019-07-03 20:36:27 -0700771/* Return 0 on success or positive error */
772static int
773xfs_bulkstat_fmt(
774 struct xfs_ibulk *breq,
775 const struct xfs_bulkstat *bstat)
776{
777 if (copy_to_user(breq->ubuffer, bstat, sizeof(struct xfs_bulkstat)))
778 return -EFAULT;
779 return xfs_ibulk_advance(breq, sizeof(struct xfs_bulkstat));
780}
781
782/*
783 * Check the incoming bulk request @hdr from userspace and initialize the
784 * internal @breq bulk request appropriately. Returns 0 if the bulk request
Darrick J. Wonge7ee96d2019-08-28 14:37:57 -0700785 * should proceed; -ECANCELED if there's nothing to do; or the usual
Darrick J. Wong0448b6f2019-07-03 20:36:27 -0700786 * negative error code.
787 */
788static int
789xfs_bulk_ireq_setup(
790 struct xfs_mount *mp,
791 struct xfs_bulk_ireq *hdr,
792 struct xfs_ibulk *breq,
793 void __user *ubuffer)
794{
795 if (hdr->icount == 0 ||
796 (hdr->flags & ~XFS_BULK_IREQ_FLAGS_ALL) ||
Darrick J. Wong0448b6f2019-07-03 20:36:27 -0700797 memchr_inv(hdr->reserved, 0, sizeof(hdr->reserved)))
798 return -EINVAL;
799
800 breq->startino = hdr->ino;
801 breq->ubuffer = ubuffer;
802 breq->icount = hdr->icount;
803 breq->ocount = 0;
Darrick J. Wong13d59a22019-07-03 20:36:28 -0700804 breq->flags = 0;
805
806 /*
Darrick J. Wongbf3cb392019-07-03 20:36:29 -0700807 * The @ino parameter is a special value, so we must look it up here.
808 * We're not allowed to have IREQ_AGNO, and we only return one inode
809 * worth of data.
810 */
811 if (hdr->flags & XFS_BULK_IREQ_SPECIAL) {
812 if (hdr->flags & XFS_BULK_IREQ_AGNO)
813 return -EINVAL;
814
815 switch (hdr->ino) {
816 case XFS_BULK_IREQ_SPECIAL_ROOT:
817 hdr->ino = mp->m_sb.sb_rootino;
818 break;
819 default:
820 return -EINVAL;
821 }
822 breq->icount = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 }
824
Darrick J. Wongbf3cb392019-07-03 20:36:29 -0700825 /*
Darrick J. Wong13d59a22019-07-03 20:36:28 -0700826 * The IREQ_AGNO flag means that we only want results from a given AG.
827 * If @hdr->ino is zero, we start iterating in that AG. If @hdr->ino is
828 * beyond the specified AG then we return no results.
829 */
830 if (hdr->flags & XFS_BULK_IREQ_AGNO) {
831 if (hdr->agno >= mp->m_sb.sb_agcount)
832 return -EINVAL;
833
834 if (breq->startino == 0)
835 breq->startino = XFS_AGINO_TO_INO(mp, hdr->agno, 0);
836 else if (XFS_INO_TO_AGNO(mp, breq->startino) < hdr->agno)
837 return -EINVAL;
838
839 breq->flags |= XFS_IBULK_SAME_AG;
840
841 /* Asking for an inode past the end of the AG? We're done! */
842 if (XFS_INO_TO_AGNO(mp, breq->startino) > hdr->agno)
Darrick J. Wonge7ee96d2019-08-28 14:37:57 -0700843 return -ECANCELED;
Darrick J. Wong13d59a22019-07-03 20:36:28 -0700844 } else if (hdr->agno)
845 return -EINVAL;
Darrick J. Wong0448b6f2019-07-03 20:36:27 -0700846
847 /* Asking for an inode past the end of the FS? We're done! */
848 if (XFS_INO_TO_AGNO(mp, breq->startino) >= mp->m_sb.sb_agcount)
Darrick J. Wonge7ee96d2019-08-28 14:37:57 -0700849 return -ECANCELED;
Darrick J. Wong0448b6f2019-07-03 20:36:27 -0700850
851 return 0;
852}
853
854/*
855 * Update the userspace bulk request @hdr to reflect the end state of the
856 * internal bulk request @breq.
857 */
858static void
859xfs_bulk_ireq_teardown(
860 struct xfs_bulk_ireq *hdr,
861 struct xfs_ibulk *breq)
862{
863 hdr->ino = breq->startino;
864 hdr->ocount = breq->ocount;
865}
866
867/* Handle the v5 bulkstat ioctl. */
868STATIC int
869xfs_ioc_bulkstat(
870 struct xfs_mount *mp,
871 unsigned int cmd,
872 struct xfs_bulkstat_req __user *arg)
873{
874 struct xfs_bulk_ireq hdr;
875 struct xfs_ibulk breq = {
876 .mp = mp,
877 };
878 int error;
879
880 if (!capable(CAP_SYS_ADMIN))
881 return -EPERM;
882
883 if (XFS_FORCED_SHUTDOWN(mp))
884 return -EIO;
885
886 if (copy_from_user(&hdr, &arg->hdr, sizeof(hdr)))
887 return -EFAULT;
888
889 error = xfs_bulk_ireq_setup(mp, &hdr, &breq, arg->bulkstat);
Darrick J. Wonge7ee96d2019-08-28 14:37:57 -0700890 if (error == -ECANCELED)
Darrick J. Wong0448b6f2019-07-03 20:36:27 -0700891 goto out_teardown;
892 if (error < 0)
893 return error;
894
895 error = xfs_bulkstat(&breq, xfs_bulkstat_fmt);
896 if (error)
897 return error;
898
899out_teardown:
900 xfs_bulk_ireq_teardown(&hdr, &breq);
901 if (copy_to_user(&arg->hdr, &hdr, sizeof(hdr)))
902 return -EFAULT;
903
904 return 0;
905}
906
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907STATIC int
Darrick J. Wongfba97602019-07-03 20:36:28 -0700908xfs_inumbers_fmt(
909 struct xfs_ibulk *breq,
910 const struct xfs_inumbers *igrp)
911{
912 if (copy_to_user(breq->ubuffer, igrp, sizeof(struct xfs_inumbers)))
913 return -EFAULT;
914 return xfs_ibulk_advance(breq, sizeof(struct xfs_inumbers));
915}
916
917/* Handle the v5 inumbers ioctl. */
918STATIC int
919xfs_ioc_inumbers(
920 struct xfs_mount *mp,
921 unsigned int cmd,
922 struct xfs_inumbers_req __user *arg)
923{
924 struct xfs_bulk_ireq hdr;
925 struct xfs_ibulk breq = {
926 .mp = mp,
927 };
928 int error;
929
930 if (!capable(CAP_SYS_ADMIN))
931 return -EPERM;
932
933 if (XFS_FORCED_SHUTDOWN(mp))
934 return -EIO;
935
936 if (copy_from_user(&hdr, &arg->hdr, sizeof(hdr)))
937 return -EFAULT;
938
939 error = xfs_bulk_ireq_setup(mp, &hdr, &breq, arg->inumbers);
Darrick J. Wonge7ee96d2019-08-28 14:37:57 -0700940 if (error == -ECANCELED)
Darrick J. Wongfba97602019-07-03 20:36:28 -0700941 goto out_teardown;
942 if (error < 0)
943 return error;
944
945 error = xfs_inumbers(&breq, xfs_inumbers_fmt);
946 if (error)
947 return error;
948
949out_teardown:
950 xfs_bulk_ireq_teardown(&hdr, &breq);
951 if (copy_to_user(&arg->hdr, &hdr, sizeof(hdr)))
952 return -EFAULT;
953
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 return 0;
955}
956
957STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958xfs_ioc_fsgeometry(
Dave Chinner1b6d9682019-04-12 07:41:16 -0700959 struct xfs_mount *mp,
960 void __user *arg,
961 int struct_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962{
Dave Chinner1b6d9682019-04-12 07:41:16 -0700963 struct xfs_fsop_geom fsgeo;
964 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
Eric Sandeen91083262019-05-01 20:26:30 -0700966 xfs_fs_geometry(&mp->m_sb, &fsgeo, struct_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
Dave Chinner1b6d9682019-04-12 07:41:16 -0700968 if (struct_version <= 3)
969 len = sizeof(struct xfs_fsop_geom_v1);
970 else if (struct_version == 4)
971 len = sizeof(struct xfs_fsop_geom_v4);
Darrick J. Wongc23232d2019-04-12 07:41:17 -0700972 else {
973 xfs_fsop_geom_health(mp, &fsgeo);
Dave Chinner1b6d9682019-04-12 07:41:16 -0700974 len = sizeof(fsgeo);
Darrick J. Wongc23232d2019-04-12 07:41:17 -0700975 }
Dave Chinner1b6d9682019-04-12 07:41:16 -0700976
977 if (copy_to_user(arg, &fsgeo, len))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000978 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 return 0;
980}
981
Darrick J. Wong7cd50062019-04-12 07:41:17 -0700982STATIC int
983xfs_ioc_ag_geometry(
984 struct xfs_mount *mp,
985 void __user *arg)
986{
987 struct xfs_ag_geometry ageo;
988 int error;
989
990 if (copy_from_user(&ageo, arg, sizeof(ageo)))
991 return -EFAULT;
Darrick J. Wong76f17932019-08-30 16:30:22 -0700992 if (ageo.ag_flags)
993 return -EINVAL;
994 if (memchr_inv(&ageo.ag_reserved, 0, sizeof(ageo.ag_reserved)))
995 return -EINVAL;
Darrick J. Wong7cd50062019-04-12 07:41:17 -0700996
997 error = xfs_ag_get_geometry(mp, ageo.ag_number, &ageo);
998 if (error)
999 return error;
1000
1001 if (copy_to_user(arg, &ageo, sizeof(ageo)))
1002 return -EFAULT;
1003 return 0;
1004}
1005
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006/*
1007 * Linux extended inode flags interface.
1008 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
1010STATIC unsigned int
1011xfs_merge_ioc_xflags(
1012 unsigned int flags,
1013 unsigned int start)
1014{
1015 unsigned int xflags = start;
1016
Eric Sandeen39058a02007-02-10 18:37:10 +11001017 if (flags & FS_IMMUTABLE_FL)
Dave Chinnere7b89482016-01-04 16:44:15 +11001018 xflags |= FS_XFLAG_IMMUTABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 else
Dave Chinnere7b89482016-01-04 16:44:15 +11001020 xflags &= ~FS_XFLAG_IMMUTABLE;
Eric Sandeen39058a02007-02-10 18:37:10 +11001021 if (flags & FS_APPEND_FL)
Dave Chinnere7b89482016-01-04 16:44:15 +11001022 xflags |= FS_XFLAG_APPEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 else
Dave Chinnere7b89482016-01-04 16:44:15 +11001024 xflags &= ~FS_XFLAG_APPEND;
Eric Sandeen39058a02007-02-10 18:37:10 +11001025 if (flags & FS_SYNC_FL)
Dave Chinnere7b89482016-01-04 16:44:15 +11001026 xflags |= FS_XFLAG_SYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 else
Dave Chinnere7b89482016-01-04 16:44:15 +11001028 xflags &= ~FS_XFLAG_SYNC;
Eric Sandeen39058a02007-02-10 18:37:10 +11001029 if (flags & FS_NOATIME_FL)
Dave Chinnere7b89482016-01-04 16:44:15 +11001030 xflags |= FS_XFLAG_NOATIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 else
Dave Chinnere7b89482016-01-04 16:44:15 +11001032 xflags &= ~FS_XFLAG_NOATIME;
Eric Sandeen39058a02007-02-10 18:37:10 +11001033 if (flags & FS_NODUMP_FL)
Dave Chinnere7b89482016-01-04 16:44:15 +11001034 xflags |= FS_XFLAG_NODUMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 else
Dave Chinnere7b89482016-01-04 16:44:15 +11001036 xflags &= ~FS_XFLAG_NODUMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
1038 return xflags;
1039}
1040
1041STATIC unsigned int
1042xfs_di2lxflags(
Darrick J. Wongc8ce5402017-06-16 11:00:05 -07001043 uint16_t di_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044{
1045 unsigned int flags = 0;
1046
1047 if (di_flags & XFS_DIFLAG_IMMUTABLE)
Eric Sandeen39058a02007-02-10 18:37:10 +11001048 flags |= FS_IMMUTABLE_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 if (di_flags & XFS_DIFLAG_APPEND)
Eric Sandeen39058a02007-02-10 18:37:10 +11001050 flags |= FS_APPEND_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 if (di_flags & XFS_DIFLAG_SYNC)
Eric Sandeen39058a02007-02-10 18:37:10 +11001052 flags |= FS_SYNC_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 if (di_flags & XFS_DIFLAG_NOATIME)
Eric Sandeen39058a02007-02-10 18:37:10 +11001054 flags |= FS_NOATIME_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 if (di_flags & XFS_DIFLAG_NODUMP)
Eric Sandeen39058a02007-02-10 18:37:10 +11001056 flags |= FS_NODUMP_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 return flags;
1058}
1059
Darrick J. Wong7b0e4922019-07-01 08:25:35 -07001060static void
1061xfs_fill_fsxattr(
1062 struct xfs_inode *ip,
1063 bool attr,
1064 struct fsxattr *fa)
1065{
1066 simple_fill_fsxattr(fa, xfs_ip2xflags(ip));
1067 fa->fsx_extsize = ip->i_d.di_extsize << ip->i_mount->m_sb.sb_blocklog;
1068 fa->fsx_cowextsize = ip->i_d.di_cowextsize <<
1069 ip->i_mount->m_sb.sb_blocklog;
1070 fa->fsx_projid = xfs_get_projid(ip);
1071
1072 if (attr) {
1073 if (ip->i_afp) {
1074 if (ip->i_afp->if_flags & XFS_IFEXTENTS)
1075 fa->fsx_nextents = xfs_iext_count(ip->i_afp);
1076 else
1077 fa->fsx_nextents = ip->i_d.di_anextents;
1078 } else
1079 fa->fsx_nextents = 0;
1080 } else {
1081 if (ip->i_df.if_flags & XFS_IFEXTENTS)
1082 fa->fsx_nextents = xfs_iext_count(&ip->i_df);
1083 else
1084 fa->fsx_nextents = ip->i_d.di_nextents;
1085 }
1086}
1087
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088STATIC int
Christoph Hellwigc83bfab2007-10-11 17:47:00 +10001089xfs_ioc_fsgetxattr(
1090 xfs_inode_t *ip,
1091 int attr,
1092 void __user *arg)
1093{
1094 struct fsxattr fa;
1095
1096 xfs_ilock(ip, XFS_ILOCK_SHARED);
Darrick J. Wong7b0e4922019-07-01 08:25:35 -07001097 xfs_fill_fsxattr(ip, attr, &fa);
Christoph Hellwigc83bfab2007-10-11 17:47:00 +10001098 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1099
1100 if (copy_to_user(arg, &fa, sizeof(fa)))
1101 return -EFAULT;
1102 return 0;
1103}
1104
Christoph Hellwigdd606872017-09-02 08:21:20 -07001105STATIC uint16_t
1106xfs_flags2diflags(
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001107 struct xfs_inode *ip,
1108 unsigned int xflags)
1109{
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001110 /* can't set PREALLOC this way, just preserve it */
Christoph Hellwigdd606872017-09-02 08:21:20 -07001111 uint16_t di_flags =
1112 (ip->i_d.di_flags & XFS_DIFLAG_PREALLOC);
1113
Dave Chinnere7b89482016-01-04 16:44:15 +11001114 if (xflags & FS_XFLAG_IMMUTABLE)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001115 di_flags |= XFS_DIFLAG_IMMUTABLE;
Dave Chinnere7b89482016-01-04 16:44:15 +11001116 if (xflags & FS_XFLAG_APPEND)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001117 di_flags |= XFS_DIFLAG_APPEND;
Dave Chinnere7b89482016-01-04 16:44:15 +11001118 if (xflags & FS_XFLAG_SYNC)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001119 di_flags |= XFS_DIFLAG_SYNC;
Dave Chinnere7b89482016-01-04 16:44:15 +11001120 if (xflags & FS_XFLAG_NOATIME)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001121 di_flags |= XFS_DIFLAG_NOATIME;
Dave Chinnere7b89482016-01-04 16:44:15 +11001122 if (xflags & FS_XFLAG_NODUMP)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001123 di_flags |= XFS_DIFLAG_NODUMP;
Dave Chinnere7b89482016-01-04 16:44:15 +11001124 if (xflags & FS_XFLAG_NODEFRAG)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001125 di_flags |= XFS_DIFLAG_NODEFRAG;
Dave Chinnere7b89482016-01-04 16:44:15 +11001126 if (xflags & FS_XFLAG_FILESTREAM)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001127 di_flags |= XFS_DIFLAG_FILESTREAM;
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001128 if (S_ISDIR(VFS_I(ip)->i_mode)) {
Dave Chinnere7b89482016-01-04 16:44:15 +11001129 if (xflags & FS_XFLAG_RTINHERIT)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001130 di_flags |= XFS_DIFLAG_RTINHERIT;
Dave Chinnere7b89482016-01-04 16:44:15 +11001131 if (xflags & FS_XFLAG_NOSYMLINKS)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001132 di_flags |= XFS_DIFLAG_NOSYMLINKS;
Dave Chinnere7b89482016-01-04 16:44:15 +11001133 if (xflags & FS_XFLAG_EXTSZINHERIT)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001134 di_flags |= XFS_DIFLAG_EXTSZINHERIT;
Dave Chinnere7b89482016-01-04 16:44:15 +11001135 if (xflags & FS_XFLAG_PROJINHERIT)
Dave Chinner9336e3a2014-10-02 09:18:40 +10001136 di_flags |= XFS_DIFLAG_PROJINHERIT;
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001137 } else if (S_ISREG(VFS_I(ip)->i_mode)) {
Dave Chinnere7b89482016-01-04 16:44:15 +11001138 if (xflags & FS_XFLAG_REALTIME)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001139 di_flags |= XFS_DIFLAG_REALTIME;
Dave Chinnere7b89482016-01-04 16:44:15 +11001140 if (xflags & FS_XFLAG_EXTSIZE)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001141 di_flags |= XFS_DIFLAG_EXTSIZE;
1142 }
Dave Chinner58f88ca2016-01-04 16:44:15 +11001143
Christoph Hellwigdd606872017-09-02 08:21:20 -07001144 return di_flags;
1145}
Dave Chinner58f88ca2016-01-04 16:44:15 +11001146
Christoph Hellwigdd606872017-09-02 08:21:20 -07001147STATIC uint64_t
1148xfs_flags2diflags2(
1149 struct xfs_inode *ip,
1150 unsigned int xflags)
1151{
1152 uint64_t di_flags2 =
1153 (ip->i_d.di_flags2 & XFS_DIFLAG2_REFLINK);
1154
Dave Chinner58f88ca2016-01-04 16:44:15 +11001155 if (xflags & FS_XFLAG_DAX)
1156 di_flags2 |= XFS_DIFLAG2_DAX;
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001157 if (xflags & FS_XFLAG_COWEXTSIZE)
1158 di_flags2 |= XFS_DIFLAG2_COWEXTSIZE;
Dave Chinner58f88ca2016-01-04 16:44:15 +11001159
Christoph Hellwigdd606872017-09-02 08:21:20 -07001160 return di_flags2;
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001161}
1162
Christoph Hellwigf13fae22008-07-21 16:16:15 +10001163STATIC void
1164xfs_diflags_to_linux(
1165 struct xfs_inode *ip)
1166{
David Chinnere4f75292008-08-13 16:00:45 +10001167 struct inode *inode = VFS_I(ip);
Christoph Hellwigf13fae22008-07-21 16:16:15 +10001168 unsigned int xflags = xfs_ip2xflags(ip);
1169
Dave Chinnere7b89482016-01-04 16:44:15 +11001170 if (xflags & FS_XFLAG_IMMUTABLE)
Christoph Hellwigf13fae22008-07-21 16:16:15 +10001171 inode->i_flags |= S_IMMUTABLE;
1172 else
1173 inode->i_flags &= ~S_IMMUTABLE;
Dave Chinnere7b89482016-01-04 16:44:15 +11001174 if (xflags & FS_XFLAG_APPEND)
Christoph Hellwigf13fae22008-07-21 16:16:15 +10001175 inode->i_flags |= S_APPEND;
1176 else
1177 inode->i_flags &= ~S_APPEND;
Dave Chinnere7b89482016-01-04 16:44:15 +11001178 if (xflags & FS_XFLAG_SYNC)
Christoph Hellwigf13fae22008-07-21 16:16:15 +10001179 inode->i_flags |= S_SYNC;
1180 else
1181 inode->i_flags &= ~S_SYNC;
Dave Chinnere7b89482016-01-04 16:44:15 +11001182 if (xflags & FS_XFLAG_NOATIME)
Christoph Hellwigf13fae22008-07-21 16:16:15 +10001183 inode->i_flags |= S_NOATIME;
1184 else
1185 inode->i_flags &= ~S_NOATIME;
Christoph Hellwig742d8422017-08-30 09:23:01 -07001186#if 0 /* disabled until the flag switching races are sorted out */
Dave Chinner58f88ca2016-01-04 16:44:15 +11001187 if (xflags & FS_XFLAG_DAX)
1188 inode->i_flags |= S_DAX;
1189 else
1190 inode->i_flags &= ~S_DAX;
Christoph Hellwig742d8422017-08-30 09:23:01 -07001191#endif
Christoph Hellwigf13fae22008-07-21 16:16:15 +10001192}
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001193
Dave Chinner29a17c02015-02-02 10:14:25 +11001194static int
1195xfs_ioctl_setattr_xflags(
1196 struct xfs_trans *tp,
1197 struct xfs_inode *ip,
1198 struct fsxattr *fa)
1199{
1200 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwigdd606872017-09-02 08:21:20 -07001201 uint64_t di_flags2;
Dave Chinner29a17c02015-02-02 10:14:25 +11001202
1203 /* Can't change realtime flag if any extents are allocated. */
1204 if ((ip->i_d.di_nextents || ip->i_delayed_blks) &&
Dave Chinnere7b89482016-01-04 16:44:15 +11001205 XFS_IS_REALTIME_INODE(ip) != (fa->fsx_xflags & FS_XFLAG_REALTIME))
Dave Chinner29a17c02015-02-02 10:14:25 +11001206 return -EINVAL;
1207
1208 /* If realtime flag is set then must have realtime device */
Dave Chinnere7b89482016-01-04 16:44:15 +11001209 if (fa->fsx_xflags & FS_XFLAG_REALTIME) {
Dave Chinner29a17c02015-02-02 10:14:25 +11001210 if (mp->m_sb.sb_rblocks == 0 || mp->m_sb.sb_rextsize == 0 ||
1211 (ip->i_d.di_extsize % mp->m_sb.sb_rextsize))
1212 return -EINVAL;
1213 }
1214
Darrick J. Wong1987fd72016-10-10 16:49:29 +11001215 /* Clear reflink if we are actually able to set the rt flag. */
Darrick J. Wongc8e156a2016-10-03 09:11:50 -07001216 if ((fa->fsx_xflags & FS_XFLAG_REALTIME) && xfs_is_reflink_inode(ip))
Darrick J. Wong1987fd72016-10-10 16:49:29 +11001217 ip->i_d.di_flags2 &= ~XFS_DIFLAG2_REFLINK;
Darrick J. Wongc8e156a2016-10-03 09:11:50 -07001218
Darrick J. Wong4f435eb2016-10-03 09:11:50 -07001219 /* Don't allow us to set DAX mode for a reflinked file for now. */
1220 if ((fa->fsx_xflags & FS_XFLAG_DAX) && xfs_is_reflink_inode(ip))
1221 return -EINVAL;
1222
Christoph Hellwigdd606872017-09-02 08:21:20 -07001223 /* diflags2 only valid for v3 inodes. */
1224 di_flags2 = xfs_flags2diflags2(ip, fa->fsx_xflags);
1225 if (di_flags2 && ip->i_d.di_version < 3)
1226 return -EINVAL;
1227
1228 ip->i_d.di_flags = xfs_flags2diflags(ip, fa->fsx_xflags);
1229 ip->i_d.di_flags2 = di_flags2;
1230
Dave Chinner29a17c02015-02-02 10:14:25 +11001231 xfs_diflags_to_linux(ip);
1232 xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
1233 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11001234 XFS_STATS_INC(mp, xs_ig_attrchg);
Dave Chinner29a17c02015-02-02 10:14:25 +11001235 return 0;
1236}
1237
Dave Chinner8f3d17a2015-02-02 10:15:35 +11001238/*
Dave Chinner3a6a8542016-03-01 09:41:33 +11001239 * If we are changing DAX flags, we have to ensure the file is clean and any
1240 * cached objects in the address space are invalidated and removed. This
1241 * requires us to lock out other IO and page faults similar to a truncate
1242 * operation. The locks need to be held until the transaction has been committed
1243 * so that the cache invalidation is atomic with respect to the DAX flag
1244 * manipulation.
1245 */
1246static int
1247xfs_ioctl_setattr_dax_invalidate(
1248 struct xfs_inode *ip,
1249 struct fsxattr *fa,
1250 int *join_flags)
1251{
1252 struct inode *inode = VFS_I(ip);
Ross Zwisler6851a3d2017-09-18 14:46:03 -07001253 struct super_block *sb = inode->i_sb;
Dave Chinner3a6a8542016-03-01 09:41:33 +11001254 int error;
1255
1256 *join_flags = 0;
1257
1258 /*
1259 * It is only valid to set the DAX flag on regular files and
Dave Chinner64485432016-03-01 09:41:33 +11001260 * directories on filesystems where the block size is equal to the page
Darrick J. Wongaaacdd22018-05-31 15:07:47 -07001261 * size. On directories it serves as an inherited hint so we don't
1262 * have to check the device for dax support or flush pagecache.
Dave Chinner3a6a8542016-03-01 09:41:33 +11001263 */
Dave Chinner64485432016-03-01 09:41:33 +11001264 if (fa->fsx_xflags & FS_XFLAG_DAX) {
Christoph Hellwig30fa5292019-10-24 22:25:38 -07001265 struct xfs_buftarg *target = xfs_inode_buftarg(ip);
1266
1267 if (!bdev_dax_supported(target->bt_bdev, sb->s_blocksize))
Dave Chinner64485432016-03-01 09:41:33 +11001268 return -EINVAL;
1269 }
Dave Chinner3a6a8542016-03-01 09:41:33 +11001270
1271 /* If the DAX state is not changing, we have nothing to do here. */
1272 if ((fa->fsx_xflags & FS_XFLAG_DAX) && IS_DAX(inode))
1273 return 0;
1274 if (!(fa->fsx_xflags & FS_XFLAG_DAX) && !IS_DAX(inode))
1275 return 0;
1276
Darrick J. Wongaaacdd22018-05-31 15:07:47 -07001277 if (S_ISDIR(inode->i_mode))
1278 return 0;
1279
Dave Chinner3a6a8542016-03-01 09:41:33 +11001280 /* lock, flush and invalidate mapping in preparation for flag change */
1281 xfs_ilock(ip, XFS_MMAPLOCK_EXCL | XFS_IOLOCK_EXCL);
1282 error = filemap_write_and_wait(inode->i_mapping);
1283 if (error)
1284 goto out_unlock;
1285 error = invalidate_inode_pages2(inode->i_mapping);
1286 if (error)
1287 goto out_unlock;
1288
1289 *join_flags = XFS_MMAPLOCK_EXCL | XFS_IOLOCK_EXCL;
1290 return 0;
1291
1292out_unlock:
1293 xfs_iunlock(ip, XFS_MMAPLOCK_EXCL | XFS_IOLOCK_EXCL);
1294 return error;
1295
1296}
1297
1298/*
Dave Chinner8f3d17a2015-02-02 10:15:35 +11001299 * Set up the transaction structure for the setattr operation, checking that we
1300 * have permission to do so. On success, return a clean transaction and the
1301 * inode locked exclusively ready for further operation specific checks. On
1302 * failure, return an error without modifying or locking the inode.
Dave Chinner3a6a8542016-03-01 09:41:33 +11001303 *
1304 * The inode might already be IO locked on call. If this is the case, it is
1305 * indicated in @join_flags and we take full responsibility for ensuring they
1306 * are unlocked from now on. Hence if we have an error here, we still have to
1307 * unlock them. Otherwise, once they are joined to the transaction, they will
1308 * be unlocked on commit/cancel.
Dave Chinner8f3d17a2015-02-02 10:15:35 +11001309 */
1310static struct xfs_trans *
1311xfs_ioctl_setattr_get_trans(
Dave Chinner3a6a8542016-03-01 09:41:33 +11001312 struct xfs_inode *ip,
1313 int join_flags)
Dave Chinner8f3d17a2015-02-02 10:15:35 +11001314{
1315 struct xfs_mount *mp = ip->i_mount;
1316 struct xfs_trans *tp;
Dave Chinner3a6a8542016-03-01 09:41:33 +11001317 int error = -EROFS;
Dave Chinner8f3d17a2015-02-02 10:15:35 +11001318
1319 if (mp->m_flags & XFS_MOUNT_RDONLY)
Dave Chinner3a6a8542016-03-01 09:41:33 +11001320 goto out_unlock;
1321 error = -EIO;
Dave Chinner8f3d17a2015-02-02 10:15:35 +11001322 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner3a6a8542016-03-01 09:41:33 +11001323 goto out_unlock;
Dave Chinner8f3d17a2015-02-02 10:15:35 +11001324
Christoph Hellwig253f4912016-04-06 09:19:55 +10001325 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
Dave Chinner8f3d17a2015-02-02 10:15:35 +11001326 if (error)
Darrick J. Wong3de5eab2019-04-22 16:28:34 -07001327 goto out_unlock;
Dave Chinner8f3d17a2015-02-02 10:15:35 +11001328
1329 xfs_ilock(ip, XFS_ILOCK_EXCL);
Dave Chinner3a6a8542016-03-01 09:41:33 +11001330 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | join_flags);
1331 join_flags = 0;
Dave Chinner8f3d17a2015-02-02 10:15:35 +11001332
1333 /*
1334 * CAP_FOWNER overrides the following restrictions:
1335 *
1336 * The user ID of the calling process must be equal to the file owner
1337 * ID, except in cases where the CAP_FSETID capability is applicable.
1338 */
1339 if (!inode_owner_or_capable(VFS_I(ip))) {
1340 error = -EPERM;
1341 goto out_cancel;
1342 }
1343
1344 if (mp->m_flags & XFS_MOUNT_WSYNC)
1345 xfs_trans_set_sync(tp);
1346
1347 return tp;
1348
1349out_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +10001350 xfs_trans_cancel(tp);
Dave Chinner3a6a8542016-03-01 09:41:33 +11001351out_unlock:
1352 if (join_flags)
1353 xfs_iunlock(ip, join_flags);
Dave Chinner8f3d17a2015-02-02 10:15:35 +11001354 return ERR_PTR(error);
1355}
1356
Iustin Pop9b94fcc2015-02-02 10:26:26 +11001357/*
1358 * extent size hint validation is somewhat cumbersome. Rules are:
1359 *
1360 * 1. extent size hint is only valid for directories and regular files
Dave Chinnere7b89482016-01-04 16:44:15 +11001361 * 2. FS_XFLAG_EXTSIZE is only valid for regular files
1362 * 3. FS_XFLAG_EXTSZINHERIT is only valid for directories.
Iustin Pop9b94fcc2015-02-02 10:26:26 +11001363 * 4. can only be changed on regular files if no extents are allocated
1364 * 5. can be changed on directories at any time
1365 * 6. extsize hint of 0 turns off hints, clears inode flags.
1366 * 7. Extent size must be a multiple of the appropriate block size.
1367 * 8. for non-realtime files, the extent size hint must be limited
1368 * to half the AG size to avoid alignment extending the extent beyond the
1369 * limits of the AG.
Darrick J. Wong80e4e122017-10-17 21:37:42 -07001370 *
1371 * Please keep this function in sync with xfs_scrub_inode_extsize.
Iustin Pop9b94fcc2015-02-02 10:26:26 +11001372 */
kbuild test robotf92090e2015-02-05 11:13:21 +11001373static int
Dave Chinnerd4388d3c2015-02-02 10:22:20 +11001374xfs_ioctl_setattr_check_extsize(
1375 struct xfs_inode *ip,
1376 struct fsxattr *fa)
1377{
1378 struct xfs_mount *mp = ip->i_mount;
Darrick J. Wongca29be72019-07-01 08:25:36 -07001379 xfs_extlen_t size;
1380 xfs_fsblock_t extsize_fsb;
Iustin Pop9b94fcc2015-02-02 10:26:26 +11001381
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001382 if (S_ISREG(VFS_I(ip)->i_mode) && ip->i_d.di_nextents &&
Dave Chinnerd4388d3c2015-02-02 10:22:20 +11001383 ((ip->i_d.di_extsize << mp->m_sb.sb_blocklog) != fa->fsx_extsize))
1384 return -EINVAL;
1385
Darrick J. Wongca29be72019-07-01 08:25:36 -07001386 if (fa->fsx_extsize == 0)
1387 return 0;
Dave Chinnerd4388d3c2015-02-02 10:22:20 +11001388
Darrick J. Wongca29be72019-07-01 08:25:36 -07001389 extsize_fsb = XFS_B_TO_FSB(mp, fa->fsx_extsize);
1390 if (extsize_fsb > MAXEXTLEN)
1391 return -EINVAL;
1392
1393 if (XFS_IS_REALTIME_INODE(ip) ||
1394 (fa->fsx_xflags & FS_XFLAG_REALTIME)) {
1395 size = mp->m_sb.sb_rextsize << mp->m_sb.sb_blocklog;
1396 } else {
1397 size = mp->m_sb.sb_blocksize;
1398 if (extsize_fsb > mp->m_sb.sb_agblocks / 2)
Dave Chinnerd4388d3c2015-02-02 10:22:20 +11001399 return -EINVAL;
Darrick J. Wongca29be72019-07-01 08:25:36 -07001400 }
Dave Chinnerd4388d3c2015-02-02 10:22:20 +11001401
Darrick J. Wongca29be72019-07-01 08:25:36 -07001402 if (fa->fsx_extsize % size)
1403 return -EINVAL;
Iustin Pop9b94fcc2015-02-02 10:26:26 +11001404
Dave Chinnerd4388d3c2015-02-02 10:22:20 +11001405 return 0;
1406}
1407
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001408/*
1409 * CoW extent size hint validation rules are:
1410 *
1411 * 1. CoW extent size hint can only be set if reflink is enabled on the fs.
1412 * The inode does not have to have any shared blocks, but it must be a v3.
1413 * 2. FS_XFLAG_COWEXTSIZE is only valid for directories and regular files;
1414 * for a directory, the hint is propagated to new files.
1415 * 3. Can be changed on files & directories at any time.
1416 * 4. CoW extsize hint of 0 turns off hints, clears inode flags.
1417 * 5. Extent size must be a multiple of the appropriate block size.
1418 * 6. The extent size hint must be limited to half the AG size to avoid
1419 * alignment extending the extent beyond the limits of the AG.
Darrick J. Wong80e4e122017-10-17 21:37:42 -07001420 *
1421 * Please keep this function in sync with xfs_scrub_inode_cowextsize.
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001422 */
1423static int
1424xfs_ioctl_setattr_check_cowextsize(
1425 struct xfs_inode *ip,
1426 struct fsxattr *fa)
1427{
1428 struct xfs_mount *mp = ip->i_mount;
Darrick J. Wongca29be72019-07-01 08:25:36 -07001429 xfs_extlen_t size;
1430 xfs_fsblock_t cowextsize_fsb;
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001431
1432 if (!(fa->fsx_xflags & FS_XFLAG_COWEXTSIZE))
1433 return 0;
1434
1435 if (!xfs_sb_version_hasreflink(&ip->i_mount->m_sb) ||
1436 ip->i_d.di_version != 3)
1437 return -EINVAL;
1438
Darrick J. Wongca29be72019-07-01 08:25:36 -07001439 if (fa->fsx_cowextsize == 0)
1440 return 0;
1441
1442 cowextsize_fsb = XFS_B_TO_FSB(mp, fa->fsx_cowextsize);
1443 if (cowextsize_fsb > MAXEXTLEN)
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001444 return -EINVAL;
1445
Darrick J. Wongca29be72019-07-01 08:25:36 -07001446 size = mp->m_sb.sb_blocksize;
1447 if (cowextsize_fsb > mp->m_sb.sb_agblocks / 2)
1448 return -EINVAL;
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001449
Darrick J. Wongca29be72019-07-01 08:25:36 -07001450 if (fa->fsx_cowextsize % size)
1451 return -EINVAL;
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001452
1453 return 0;
1454}
1455
kbuild test robotf92090e2015-02-05 11:13:21 +11001456static int
Dave Chinner23bd0732015-02-02 10:22:53 +11001457xfs_ioctl_setattr_check_projid(
1458 struct xfs_inode *ip,
1459 struct fsxattr *fa)
1460{
1461 /* Disallow 32bit project ids if projid32bit feature is not enabled. */
Darrick J. Wongc8ce5402017-06-16 11:00:05 -07001462 if (fa->fsx_projid > (uint16_t)-1 &&
Dave Chinner23bd0732015-02-02 10:22:53 +11001463 !xfs_sb_version_hasprojid32bit(&ip->i_mount->m_sb))
1464 return -EINVAL;
Dave Chinner23bd0732015-02-02 10:22:53 +11001465 return 0;
1466}
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001467
1468STATIC int
1469xfs_ioctl_setattr(
1470 xfs_inode_t *ip,
Dave Chinnerfd179b92015-02-02 10:16:25 +11001471 struct fsxattr *fa)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001472{
Darrick J. Wong7b0e4922019-07-01 08:25:35 -07001473 struct fsxattr old_fa;
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001474 struct xfs_mount *mp = ip->i_mount;
1475 struct xfs_trans *tp;
Christoph Hellwig7d095252009-06-08 15:33:32 +02001476 struct xfs_dquot *udqp = NULL;
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001477 struct xfs_dquot *pdqp = NULL;
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001478 struct xfs_dquot *olddquot = NULL;
1479 int code;
Dave Chinner3a6a8542016-03-01 09:41:33 +11001480 int join_flags = 0;
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001481
Christoph Hellwigcca28fb2010-06-24 11:57:09 +10001482 trace_xfs_ioctl_setattr(ip);
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001483
Dave Chinner23bd0732015-02-02 10:22:53 +11001484 code = xfs_ioctl_setattr_check_projid(ip, fa);
1485 if (code)
1486 return code;
Arkadiusz Mi?kiewicz23963e542010-08-26 10:19:43 +00001487
1488 /*
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001489 * If disk quotas is on, we make sure that the dquots do exist on disk,
1490 * before we start any other transactions. Trying to do this later
1491 * is messy. We don't care to take a readlock to look at the ids
1492 * in inode here, because we can't hold it across the trans_reserve.
1493 * If the IDs do change before we take the ilock, we're covered
1494 * because the i_*dquot fields will get updated anyway.
1495 */
Dave Chinnerfd179b92015-02-02 10:16:25 +11001496 if (XFS_IS_QUOTA_ON(mp)) {
Christoph Hellwig7d095252009-06-08 15:33:32 +02001497 code = xfs_qm_vop_dqalloc(ip, ip->i_d.di_uid,
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001498 ip->i_d.di_gid, fa->fsx_projid,
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001499 XFS_QMOPT_PQUOTA, &udqp, NULL, &pdqp);
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001500 if (code)
1501 return code;
1502 }
1503
Dave Chinner3a6a8542016-03-01 09:41:33 +11001504 /*
1505 * Changing DAX config may require inode locking for mapping
1506 * invalidation. These need to be held all the way to transaction commit
1507 * or cancel time, so need to be passed through to
1508 * xfs_ioctl_setattr_get_trans() so it can apply them to the join call
1509 * appropriately.
1510 */
1511 code = xfs_ioctl_setattr_dax_invalidate(ip, fa, &join_flags);
1512 if (code)
1513 goto error_free_dquots;
1514
1515 tp = xfs_ioctl_setattr_get_trans(ip, join_flags);
Dave Chinner8f3d17a2015-02-02 10:15:35 +11001516 if (IS_ERR(tp)) {
1517 code = PTR_ERR(tp);
1518 goto error_free_dquots;
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001519 }
1520
Dave Chinnerfd179b92015-02-02 10:16:25 +11001521 if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_PQUOTA_ON(mp) &&
1522 xfs_get_projid(ip) != fa->fsx_projid) {
1523 code = xfs_qm_vop_chown_reserve(tp, ip, udqp, NULL, pdqp,
1524 capable(CAP_FOWNER) ? XFS_QMOPT_FORCE_RES : 0);
1525 if (code) /* out of quota */
Dave Chinnerd4388d3c2015-02-02 10:22:20 +11001526 goto error_trans_cancel;
Dave Chinnerfd179b92015-02-02 10:16:25 +11001527 }
1528
Darrick J. Wong7b0e4922019-07-01 08:25:35 -07001529 xfs_fill_fsxattr(ip, false, &old_fa);
1530 code = vfs_ioc_fssetxattr_check(VFS_I(ip), &old_fa, fa);
1531 if (code)
1532 goto error_trans_cancel;
1533
Dave Chinnerd4388d3c2015-02-02 10:22:20 +11001534 code = xfs_ioctl_setattr_check_extsize(ip, fa);
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001535 if (code)
Dave Chinnerd4388d3c2015-02-02 10:22:20 +11001536 goto error_trans_cancel;
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001537
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001538 code = xfs_ioctl_setattr_check_cowextsize(ip, fa);
1539 if (code)
1540 goto error_trans_cancel;
1541
Dave Chinner29a17c02015-02-02 10:14:25 +11001542 code = xfs_ioctl_setattr_xflags(tp, ip, fa);
1543 if (code)
Dave Chinnerd4388d3c2015-02-02 10:22:20 +11001544 goto error_trans_cancel;
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001545
1546 /*
Dave Chinnerfd179b92015-02-02 10:16:25 +11001547 * Change file ownership. Must be the owner or privileged. CAP_FSETID
1548 * overrides the following restrictions:
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001549 *
Dave Chinnerfd179b92015-02-02 10:16:25 +11001550 * The set-user-ID and set-group-ID bits of a file will be cleared upon
1551 * successful return from chown()
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001552 */
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001553
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001554 if ((VFS_I(ip)->i_mode & (S_ISUID|S_ISGID)) &&
Dave Chinnerfd179b92015-02-02 10:16:25 +11001555 !capable_wrt_inode_uidgid(VFS_I(ip), CAP_FSETID))
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001556 VFS_I(ip)->i_mode &= ~(S_ISUID|S_ISGID);
Dave Chinnerfd179b92015-02-02 10:16:25 +11001557
1558 /* Change the ownerships and register project quota modifications */
1559 if (xfs_get_projid(ip) != fa->fsx_projid) {
1560 if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_PQUOTA_ON(mp)) {
1561 olddquot = xfs_qm_vop_chown(tp, ip,
1562 &ip->i_pdquot, pdqp);
Dwight Engenfd5e2aa2013-08-15 14:08:00 -04001563 }
Dave Chinnerfd179b92015-02-02 10:16:25 +11001564 ASSERT(ip->i_d.di_version > 1);
1565 xfs_set_projid(ip, fa->fsx_projid);
Christoph Hellwigf13fae22008-07-21 16:16:15 +10001566 }
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001567
Dave Chinnera8727032014-10-02 09:20:30 +10001568 /*
1569 * Only set the extent size hint if we've already determined that the
1570 * extent size hint should be set on the inode. If no extent size flags
1571 * are set on the inode then unconditionally clear the extent size hint.
1572 */
Dave Chinnerfd179b92015-02-02 10:16:25 +11001573 if (ip->i_d.di_flags & (XFS_DIFLAG_EXTSIZE | XFS_DIFLAG_EXTSZINHERIT))
1574 ip->i_d.di_extsize = fa->fsx_extsize >> mp->m_sb.sb_blocklog;
1575 else
1576 ip->i_d.di_extsize = 0;
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001577 if (ip->i_d.di_version == 3 &&
1578 (ip->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE))
1579 ip->i_d.di_cowextsize = fa->fsx_cowextsize >>
1580 mp->m_sb.sb_blocklog;
1581 else
1582 ip->i_d.di_cowextsize = 0;
Dave Chinnera8727032014-10-02 09:20:30 +10001583
Christoph Hellwig70393312015-06-04 13:48:08 +10001584 code = xfs_trans_commit(tp);
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001585
1586 /*
1587 * Release any dquot(s) the inode had kept before chown.
1588 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02001589 xfs_qm_dqrele(olddquot);
1590 xfs_qm_dqrele(udqp);
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001591 xfs_qm_dqrele(pdqp);
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001592
Christoph Hellwig288699f2010-06-23 18:11:15 +10001593 return code;
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001594
Dave Chinnerd4388d3c2015-02-02 10:22:20 +11001595error_trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +10001596 xfs_trans_cancel(tp);
Dave Chinner8f3d17a2015-02-02 10:15:35 +11001597error_free_dquots:
Christoph Hellwig7d095252009-06-08 15:33:32 +02001598 xfs_qm_dqrele(udqp);
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001599 xfs_qm_dqrele(pdqp);
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001600 return code;
1601}
1602
Christoph Hellwigc83bfab2007-10-11 17:47:00 +10001603STATIC int
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001604xfs_ioc_fssetxattr(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 xfs_inode_t *ip,
1606 struct file *filp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 void __user *arg)
1608{
1609 struct fsxattr fa;
Jan Karad9457dc2012-06-12 16:20:39 +02001610 int error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001611
1612 if (copy_from_user(&fa, arg, sizeof(fa)))
1613 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614
Jan Karad9457dc2012-06-12 16:20:39 +02001615 error = mnt_want_write_file(filp);
1616 if (error)
1617 return error;
Dave Chinnerfd179b92015-02-02 10:16:25 +11001618 error = xfs_ioctl_setattr(ip, &fa);
Jan Karad9457dc2012-06-12 16:20:39 +02001619 mnt_drop_write_file(filp);
Dave Chinner24513372014-06-25 14:58:08 +10001620 return error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001621}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001623STATIC int
1624xfs_ioc_getxflags(
1625 xfs_inode_t *ip,
1626 void __user *arg)
1627{
1628 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001630 flags = xfs_di2lxflags(ip->i_d.di_flags);
1631 if (copy_to_user(arg, &flags, sizeof(flags)))
1632 return -EFAULT;
1633 return 0;
1634}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001636STATIC int
1637xfs_ioc_setxflags(
Dave Chinnerf96291f2015-02-02 10:15:56 +11001638 struct xfs_inode *ip,
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001639 struct file *filp,
1640 void __user *arg)
1641{
Dave Chinnerf96291f2015-02-02 10:15:56 +11001642 struct xfs_trans *tp;
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001643 struct fsxattr fa;
Darrick J. Wong7b0e4922019-07-01 08:25:35 -07001644 struct fsxattr old_fa;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001645 unsigned int flags;
Dave Chinner3a6a8542016-03-01 09:41:33 +11001646 int join_flags = 0;
Dave Chinnerf96291f2015-02-02 10:15:56 +11001647 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001649 if (copy_from_user(&flags, arg, sizeof(flags)))
1650 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001652 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
1653 FS_NOATIME_FL | FS_NODUMP_FL | \
1654 FS_SYNC_FL))
1655 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001657 fa.fsx_xflags = xfs_merge_ioc_xflags(flags, xfs_ip2xflags(ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
Jan Karad9457dc2012-06-12 16:20:39 +02001659 error = mnt_want_write_file(filp);
1660 if (error)
1661 return error;
Dave Chinnerf96291f2015-02-02 10:15:56 +11001662
Dave Chinner3a6a8542016-03-01 09:41:33 +11001663 /*
1664 * Changing DAX config may require inode locking for mapping
1665 * invalidation. These need to be held all the way to transaction commit
1666 * or cancel time, so need to be passed through to
1667 * xfs_ioctl_setattr_get_trans() so it can apply them to the join call
1668 * appropriately.
1669 */
1670 error = xfs_ioctl_setattr_dax_invalidate(ip, &fa, &join_flags);
1671 if (error)
1672 goto out_drop_write;
1673
1674 tp = xfs_ioctl_setattr_get_trans(ip, join_flags);
Dave Chinnerf96291f2015-02-02 10:15:56 +11001675 if (IS_ERR(tp)) {
1676 error = PTR_ERR(tp);
1677 goto out_drop_write;
1678 }
1679
Darrick J. Wong7b0e4922019-07-01 08:25:35 -07001680 xfs_fill_fsxattr(ip, false, &old_fa);
1681 error = vfs_ioc_fssetxattr_check(VFS_I(ip), &old_fa, &fa);
1682 if (error) {
1683 xfs_trans_cancel(tp);
1684 goto out_drop_write;
1685 }
1686
Dave Chinnerf96291f2015-02-02 10:15:56 +11001687 error = xfs_ioctl_setattr_xflags(tp, ip, &fa);
1688 if (error) {
Christoph Hellwig4906e212015-06-04 13:47:56 +10001689 xfs_trans_cancel(tp);
Dave Chinnerf96291f2015-02-02 10:15:56 +11001690 goto out_drop_write;
1691 }
1692
Christoph Hellwig70393312015-06-04 13:48:08 +10001693 error = xfs_trans_commit(tp);
Dave Chinnerf96291f2015-02-02 10:15:56 +11001694out_drop_write:
Jan Karad9457dc2012-06-12 16:20:39 +02001695 mnt_drop_write_file(filp);
Dave Chinner24513372014-06-25 14:58:08 +10001696 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697}
1698
Christoph Hellwig232b51942017-10-17 14:16:19 -07001699static bool
1700xfs_getbmap_format(
1701 struct kgetbmap *p,
1702 struct getbmapx __user *u,
1703 size_t recsize)
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001704{
Christoph Hellwig232b51942017-10-17 14:16:19 -07001705 if (put_user(p->bmv_offset, &u->bmv_offset) ||
1706 put_user(p->bmv_block, &u->bmv_block) ||
1707 put_user(p->bmv_length, &u->bmv_length) ||
1708 put_user(0, &u->bmv_count) ||
1709 put_user(0, &u->bmv_entries))
1710 return false;
1711 if (recsize < sizeof(struct getbmapx))
1712 return true;
1713 if (put_user(0, &u->bmv_iflags) ||
1714 put_user(p->bmv_oflags, &u->bmv_oflags) ||
1715 put_user(0, &u->bmv_unused1) ||
1716 put_user(0, &u->bmv_unused2))
1717 return false;
1718 return true;
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001719}
1720
1721STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722xfs_ioc_getbmap(
Christoph Hellwig8f3e2052016-07-20 11:29:35 +10001723 struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 unsigned int cmd,
1725 void __user *arg)
1726{
Darrick J. Wongbe6324c2017-04-03 15:17:57 -07001727 struct getbmapx bmx = { 0 };
Christoph Hellwig232b51942017-10-17 14:16:19 -07001728 struct kgetbmap *buf;
1729 size_t recsize;
1730 int error, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
Christoph Hellwig232b51942017-10-17 14:16:19 -07001732 switch (cmd) {
1733 case XFS_IOC_GETBMAPA:
1734 bmx.bmv_iflags = BMV_IF_ATTRFORK;
1735 /*FALLTHRU*/
1736 case XFS_IOC_GETBMAP:
1737 if (file->f_mode & FMODE_NOCMTIME)
1738 bmx.bmv_iflags |= BMV_IF_NO_DMAPI_READ;
1739 /* struct getbmap is a strict subset of struct getbmapx. */
1740 recsize = sizeof(struct getbmap);
1741 break;
1742 case XFS_IOC_GETBMAPX:
1743 recsize = sizeof(struct getbmapx);
1744 break;
1745 default:
1746 return -EINVAL;
1747 }
1748
1749 if (copy_from_user(&bmx, arg, recsize))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10001750 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001752 if (bmx.bmv_count < 2)
Eric Sandeenb474c7a2014-06-22 15:04:54 +10001753 return -EINVAL;
Christoph Hellwig232b51942017-10-17 14:16:19 -07001754 if (bmx.bmv_count > ULONG_MAX / recsize)
1755 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
Christoph Hellwig232b51942017-10-17 14:16:19 -07001757 buf = kmem_zalloc_large(bmx.bmv_count * sizeof(*buf), 0);
1758 if (!buf)
1759 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760
Christoph Hellwig232b51942017-10-17 14:16:19 -07001761 error = xfs_getbmap(XFS_I(file_inode(file)), &bmx, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 if (error)
Christoph Hellwig232b51942017-10-17 14:16:19 -07001763 goto out_free_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
Christoph Hellwig232b51942017-10-17 14:16:19 -07001765 error = -EFAULT;
1766 if (copy_to_user(arg, &bmx, recsize))
1767 goto out_free_buf;
1768 arg += recsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
Christoph Hellwig232b51942017-10-17 14:16:19 -07001770 for (i = 0; i < bmx.bmv_entries; i++) {
1771 if (!xfs_getbmap_format(buf + i, arg, recsize))
1772 goto out_free_buf;
1773 arg += recsize;
1774 }
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001775
Christoph Hellwig232b51942017-10-17 14:16:19 -07001776 error = 0;
1777out_free_buf:
1778 kmem_free(buf);
Christophe JAILLET132bf672018-11-06 07:50:50 -08001779 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780}
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001781
Darrick J. Wonge89c0412017-03-28 14:56:37 -07001782struct getfsmap_info {
1783 struct xfs_mount *mp;
Christoph Hellwig9d17e142017-04-21 11:24:41 -07001784 struct fsmap_head __user *data;
1785 unsigned int idx;
Darrick J. Wonge89c0412017-03-28 14:56:37 -07001786 __u32 last_flags;
1787};
1788
1789STATIC int
1790xfs_getfsmap_format(struct xfs_fsmap *xfm, void *priv)
1791{
1792 struct getfsmap_info *info = priv;
1793 struct fsmap fm;
1794
1795 trace_xfs_getfsmap_mapping(info->mp, xfm);
1796
1797 info->last_flags = xfm->fmr_flags;
1798 xfs_fsmap_from_internal(&fm, xfm);
Christoph Hellwig9d17e142017-04-21 11:24:41 -07001799 if (copy_to_user(&info->data->fmh_recs[info->idx++], &fm,
1800 sizeof(struct fsmap)))
Darrick J. Wonge89c0412017-03-28 14:56:37 -07001801 return -EFAULT;
1802
Darrick J. Wonge89c0412017-03-28 14:56:37 -07001803 return 0;
1804}
1805
1806STATIC int
1807xfs_ioc_getfsmap(
1808 struct xfs_inode *ip,
Christoph Hellwig9d17e142017-04-21 11:24:41 -07001809 struct fsmap_head __user *arg)
Darrick J. Wonge89c0412017-03-28 14:56:37 -07001810{
Christoph Hellwigef2b67e2017-04-21 11:24:40 -07001811 struct getfsmap_info info = { NULL };
Darrick J. Wonge89c0412017-03-28 14:56:37 -07001812 struct xfs_fsmap_head xhead = {0};
1813 struct fsmap_head head;
1814 bool aborted = false;
1815 int error;
1816
1817 if (copy_from_user(&head, arg, sizeof(struct fsmap_head)))
1818 return -EFAULT;
1819 if (memchr_inv(head.fmh_reserved, 0, sizeof(head.fmh_reserved)) ||
1820 memchr_inv(head.fmh_keys[0].fmr_reserved, 0,
1821 sizeof(head.fmh_keys[0].fmr_reserved)) ||
1822 memchr_inv(head.fmh_keys[1].fmr_reserved, 0,
1823 sizeof(head.fmh_keys[1].fmr_reserved)))
1824 return -EINVAL;
1825
1826 xhead.fmh_iflags = head.fmh_iflags;
1827 xhead.fmh_count = head.fmh_count;
1828 xfs_fsmap_to_internal(&xhead.fmh_keys[0], &head.fmh_keys[0]);
1829 xfs_fsmap_to_internal(&xhead.fmh_keys[1], &head.fmh_keys[1]);
1830
1831 trace_xfs_getfsmap_low_key(ip->i_mount, &xhead.fmh_keys[0]);
1832 trace_xfs_getfsmap_high_key(ip->i_mount, &xhead.fmh_keys[1]);
1833
1834 info.mp = ip->i_mount;
Christoph Hellwig9d17e142017-04-21 11:24:41 -07001835 info.data = arg;
Darrick J. Wonge89c0412017-03-28 14:56:37 -07001836 error = xfs_getfsmap(ip->i_mount, &xhead, xfs_getfsmap_format, &info);
Darrick J. Wonge7ee96d2019-08-28 14:37:57 -07001837 if (error == -ECANCELED) {
Darrick J. Wonge89c0412017-03-28 14:56:37 -07001838 error = 0;
1839 aborted = true;
1840 } else if (error)
1841 return error;
1842
1843 /* If we didn't abort, set the "last" flag in the last fmx */
Darrick J. Wong12e4a382017-04-23 10:45:21 -07001844 if (!aborted && info.idx) {
Darrick J. Wonge89c0412017-03-28 14:56:37 -07001845 info.last_flags |= FMR_OF_LAST;
Christoph Hellwig9d17e142017-04-21 11:24:41 -07001846 if (copy_to_user(&info.data->fmh_recs[info.idx - 1].fmr_flags,
1847 &info.last_flags, sizeof(info.last_flags)))
Darrick J. Wonge89c0412017-03-28 14:56:37 -07001848 return -EFAULT;
1849 }
1850
1851 /* copy back header */
1852 head.fmh_entries = xhead.fmh_entries;
1853 head.fmh_oflags = xhead.fmh_oflags;
1854 if (copy_to_user(arg, &head, sizeof(struct fsmap_head)))
1855 return -EFAULT;
1856
1857 return 0;
1858}
1859
Darrick J. Wong36fd6e82017-10-17 21:37:34 -07001860STATIC int
1861xfs_ioc_scrub_metadata(
1862 struct xfs_inode *ip,
1863 void __user *arg)
1864{
1865 struct xfs_scrub_metadata scrub;
1866 int error;
1867
1868 if (!capable(CAP_SYS_ADMIN))
1869 return -EPERM;
1870
1871 if (copy_from_user(&scrub, arg, sizeof(scrub)))
1872 return -EFAULT;
1873
1874 error = xfs_scrub_metadata(ip, &scrub);
1875 if (error)
1876 return error;
1877
1878 if (copy_to_user(arg, &scrub, sizeof(scrub)))
1879 return -EFAULT;
1880
1881 return 0;
1882}
1883
Dave Chinnera133d952013-08-12 20:49:48 +10001884int
1885xfs_ioc_swapext(
1886 xfs_swapext_t *sxp)
1887{
1888 xfs_inode_t *ip, *tip;
1889 struct fd f, tmp;
1890 int error = 0;
1891
1892 /* Pull information for the target fd */
1893 f = fdget((int)sxp->sx_fdtarget);
1894 if (!f.file) {
Dave Chinner24513372014-06-25 14:58:08 +10001895 error = -EINVAL;
Dave Chinnera133d952013-08-12 20:49:48 +10001896 goto out;
1897 }
1898
1899 if (!(f.file->f_mode & FMODE_WRITE) ||
1900 !(f.file->f_mode & FMODE_READ) ||
1901 (f.file->f_flags & O_APPEND)) {
Dave Chinner24513372014-06-25 14:58:08 +10001902 error = -EBADF;
Dave Chinnera133d952013-08-12 20:49:48 +10001903 goto out_put_file;
1904 }
1905
1906 tmp = fdget((int)sxp->sx_fdtmp);
1907 if (!tmp.file) {
Dave Chinner24513372014-06-25 14:58:08 +10001908 error = -EINVAL;
Dave Chinnera133d952013-08-12 20:49:48 +10001909 goto out_put_file;
1910 }
1911
1912 if (!(tmp.file->f_mode & FMODE_WRITE) ||
1913 !(tmp.file->f_mode & FMODE_READ) ||
1914 (tmp.file->f_flags & O_APPEND)) {
Dave Chinner24513372014-06-25 14:58:08 +10001915 error = -EBADF;
Dave Chinnera133d952013-08-12 20:49:48 +10001916 goto out_put_tmp_file;
1917 }
1918
1919 if (IS_SWAPFILE(file_inode(f.file)) ||
1920 IS_SWAPFILE(file_inode(tmp.file))) {
Dave Chinner24513372014-06-25 14:58:08 +10001921 error = -EINVAL;
Dave Chinnera133d952013-08-12 20:49:48 +10001922 goto out_put_tmp_file;
1923 }
1924
Jann Horn7f1b6242016-07-20 10:30:30 +10001925 /*
1926 * We need to ensure that the fds passed in point to XFS inodes
1927 * before we cast and access them as XFS structures as we have no
1928 * control over what the user passes us here.
1929 */
1930 if (f.file->f_op != &xfs_file_operations ||
1931 tmp.file->f_op != &xfs_file_operations) {
1932 error = -EINVAL;
1933 goto out_put_tmp_file;
1934 }
1935
Dave Chinnera133d952013-08-12 20:49:48 +10001936 ip = XFS_I(file_inode(f.file));
1937 tip = XFS_I(file_inode(tmp.file));
1938
1939 if (ip->i_mount != tip->i_mount) {
Dave Chinner24513372014-06-25 14:58:08 +10001940 error = -EINVAL;
Dave Chinnera133d952013-08-12 20:49:48 +10001941 goto out_put_tmp_file;
1942 }
1943
1944 if (ip->i_ino == tip->i_ino) {
Dave Chinner24513372014-06-25 14:58:08 +10001945 error = -EINVAL;
Dave Chinnera133d952013-08-12 20:49:48 +10001946 goto out_put_tmp_file;
1947 }
1948
1949 if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
Dave Chinner24513372014-06-25 14:58:08 +10001950 error = -EIO;
Dave Chinnera133d952013-08-12 20:49:48 +10001951 goto out_put_tmp_file;
1952 }
1953
1954 error = xfs_swap_extents(ip, tip, sxp);
1955
1956 out_put_tmp_file:
1957 fdput(tmp);
1958 out_put_file:
1959 fdput(f);
1960 out:
1961 return error;
1962}
1963
Eric Sandeenf7664b32018-05-15 13:21:48 -07001964static int
1965xfs_ioc_getlabel(
1966 struct xfs_mount *mp,
1967 char __user *user_label)
1968{
1969 struct xfs_sb *sbp = &mp->m_sb;
1970 char label[XFSLABEL_MAX + 1];
1971
1972 /* Paranoia */
1973 BUILD_BUG_ON(sizeof(sbp->sb_fname) > FSLABEL_MAX);
1974
Arnd Bergmann4bb8b652018-06-05 19:42:45 -07001975 /* 1 larger than sb_fname, so this ensures a trailing NUL char */
1976 memset(label, 0, sizeof(label));
Eric Sandeenf7664b32018-05-15 13:21:48 -07001977 spin_lock(&mp->m_sb_lock);
Arnd Bergmann4bb8b652018-06-05 19:42:45 -07001978 strncpy(label, sbp->sb_fname, XFSLABEL_MAX);
Eric Sandeenf7664b32018-05-15 13:21:48 -07001979 spin_unlock(&mp->m_sb_lock);
1980
Arnd Bergmann4bb8b652018-06-05 19:42:45 -07001981 if (copy_to_user(user_label, label, sizeof(label)))
Eric Sandeenf7664b32018-05-15 13:21:48 -07001982 return -EFAULT;
1983 return 0;
1984}
1985
1986static int
1987xfs_ioc_setlabel(
1988 struct file *filp,
1989 struct xfs_mount *mp,
1990 char __user *newlabel)
1991{
1992 struct xfs_sb *sbp = &mp->m_sb;
1993 char label[XFSLABEL_MAX + 1];
1994 size_t len;
1995 int error;
1996
1997 if (!capable(CAP_SYS_ADMIN))
1998 return -EPERM;
1999 /*
2000 * The generic ioctl allows up to FSLABEL_MAX chars, but XFS is much
2001 * smaller, at 12 bytes. We copy one more to be sure we find the
2002 * (required) NULL character to test the incoming label length.
2003 * NB: The on disk label doesn't need to be null terminated.
2004 */
2005 if (copy_from_user(label, newlabel, XFSLABEL_MAX + 1))
2006 return -EFAULT;
2007 len = strnlen(label, XFSLABEL_MAX + 1);
2008 if (len > sizeof(sbp->sb_fname))
2009 return -EINVAL;
2010
2011 error = mnt_want_write_file(filp);
2012 if (error)
2013 return error;
2014
2015 spin_lock(&mp->m_sb_lock);
2016 memset(sbp->sb_fname, 0, sizeof(sbp->sb_fname));
Arnd Bergmann4bb8b652018-06-05 19:42:45 -07002017 memcpy(sbp->sb_fname, label, len);
Eric Sandeenf7664b32018-05-15 13:21:48 -07002018 spin_unlock(&mp->m_sb_lock);
2019
2020 /*
2021 * Now we do several things to satisfy userspace.
2022 * In addition to normal logging of the primary superblock, we also
2023 * immediately write these changes to sector zero for the primary, then
2024 * update all backup supers (as xfs_db does for a label change), then
2025 * invalidate the block device page cache. This is so that any prior
2026 * buffered reads from userspace (i.e. from blkid) are invalidated,
2027 * and userspace will see the newly-written label.
2028 */
2029 error = xfs_sync_sb_buf(mp);
2030 if (error)
2031 goto out;
2032 /*
2033 * growfs also updates backup supers so lock against that.
2034 */
2035 mutex_lock(&mp->m_growlock);
2036 error = xfs_update_secondary_sbs(mp);
2037 mutex_unlock(&mp->m_growlock);
2038
2039 invalidate_bdev(mp->m_ddev_targp->bt_bdev);
2040
2041out:
2042 mnt_drop_write_file(filp);
2043 return error;
2044}
2045
Christoph Hellwig4d4be482008-12-09 04:47:33 -05002046/*
2047 * Note: some of the ioctl's return positive numbers as a
2048 * byte count indicating success, such as readlink_by_handle.
2049 * So we don't "sign flip" like most other routines. This means
2050 * true errors need to be returned as a negative value.
2051 */
2052long
2053xfs_file_ioctl(
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002054 struct file *filp,
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002055 unsigned int cmd,
Christoph Hellwig4d4be482008-12-09 04:47:33 -05002056 unsigned long p)
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002057{
Al Viro496ad9a2013-01-23 17:07:38 -05002058 struct inode *inode = file_inode(filp);
Christoph Hellwig4d4be482008-12-09 04:47:33 -05002059 struct xfs_inode *ip = XFS_I(inode);
2060 struct xfs_mount *mp = ip->i_mount;
2061 void __user *arg = (void __user *)p;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002062 int error;
2063
Christoph Hellwigcca28fb2010-06-24 11:57:09 +10002064 trace_xfs_file_ioctl(ip);
Christoph Hellwig4d4be482008-12-09 04:47:33 -05002065
2066 switch (cmd) {
Christoph Hellwiga46db602011-01-07 13:02:04 +00002067 case FITRIM:
2068 return xfs_ioc_trim(mp, arg);
Eric Sandeenf7664b32018-05-15 13:21:48 -07002069 case FS_IOC_GETFSLABEL:
2070 return xfs_ioc_getlabel(mp, arg);
2071 case FS_IOC_SETFSLABEL:
2072 return xfs_ioc_setlabel(filp, mp, arg);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002073 case XFS_IOC_ALLOCSP:
2074 case XFS_IOC_FREESP:
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002075 case XFS_IOC_ALLOCSP64:
Christoph Hellwig837a6e72019-10-24 22:26:02 -07002076 case XFS_IOC_FREESP64: {
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06002077 xfs_flock64_t bf;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002078
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06002079 if (copy_from_user(&bf, arg, sizeof(bf)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002080 return -EFAULT;
Christoph Hellwig837a6e72019-10-24 22:26:02 -07002081 return xfs_ioc_space(filp, &bf);
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06002082 }
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002083 case XFS_IOC_DIOINFO: {
Christoph Hellwigc7d68312019-10-24 22:25:39 -07002084 struct xfs_buftarg *target = xfs_inode_buftarg(ip);
2085 struct dioattr da;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002086
Eric Sandeen7c71ee72014-01-21 16:46:23 -06002087 da.d_mem = da.d_miniosz = target->bt_logical_sectorsize;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002088 da.d_maxiosz = INT_MAX & ~(da.d_miniosz - 1);
2089
2090 if (copy_to_user(arg, &da, sizeof(da)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002091 return -EFAULT;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002092 return 0;
2093 }
2094
2095 case XFS_IOC_FSBULKSTAT_SINGLE:
2096 case XFS_IOC_FSBULKSTAT:
2097 case XFS_IOC_FSINUMBERS:
Darrick J. Wong8bfe9d12019-07-03 20:36:26 -07002098 return xfs_ioc_fsbulkstat(mp, cmd, arg);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002099
Darrick J. Wong0448b6f2019-07-03 20:36:27 -07002100 case XFS_IOC_BULKSTAT:
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002101 return xfs_ioc_bulkstat(mp, cmd, arg);
Darrick J. Wongfba97602019-07-03 20:36:28 -07002102 case XFS_IOC_INUMBERS:
2103 return xfs_ioc_inumbers(mp, cmd, arg);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002104
2105 case XFS_IOC_FSGEOMETRY_V1:
Dave Chinner1b6d9682019-04-12 07:41:16 -07002106 return xfs_ioc_fsgeometry(mp, arg, 3);
2107 case XFS_IOC_FSGEOMETRY_V4:
2108 return xfs_ioc_fsgeometry(mp, arg, 4);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002109 case XFS_IOC_FSGEOMETRY:
Dave Chinner1b6d9682019-04-12 07:41:16 -07002110 return xfs_ioc_fsgeometry(mp, arg, 5);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002111
Darrick J. Wong7cd50062019-04-12 07:41:17 -07002112 case XFS_IOC_AG_GEOMETRY:
2113 return xfs_ioc_ag_geometry(mp, arg);
2114
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002115 case XFS_IOC_GETVERSION:
2116 return put_user(inode->i_generation, (int __user *)arg);
2117
2118 case XFS_IOC_FSGETXATTR:
2119 return xfs_ioc_fsgetxattr(ip, 0, arg);
2120 case XFS_IOC_FSGETXATTRA:
2121 return xfs_ioc_fsgetxattr(ip, 1, arg);
Lachlan McIlroy3b2816b2008-04-18 12:43:35 +10002122 case XFS_IOC_FSSETXATTR:
Lachlan McIlroy65e67f52008-04-18 12:59:45 +10002123 return xfs_ioc_fssetxattr(ip, filp, arg);
2124 case XFS_IOC_GETXFLAGS:
2125 return xfs_ioc_getxflags(ip, arg);
2126 case XFS_IOC_SETXFLAGS:
2127 return xfs_ioc_setxflags(ip, filp, arg);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002128
2129 case XFS_IOC_FSSETDM: {
2130 struct fsdmidata dmi;
2131
2132 if (copy_from_user(&dmi, arg, sizeof(dmi)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002133 return -EFAULT;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002134
Jan Karad9457dc2012-06-12 16:20:39 +02002135 error = mnt_want_write_file(filp);
2136 if (error)
2137 return error;
2138
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002139 error = xfs_set_dmattrs(ip, dmi.fsd_dmevmask,
2140 dmi.fsd_dmstate);
Jan Karad9457dc2012-06-12 16:20:39 +02002141 mnt_drop_write_file(filp);
Dave Chinner24513372014-06-25 14:58:08 +10002142 return error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002143 }
2144
2145 case XFS_IOC_GETBMAP:
2146 case XFS_IOC_GETBMAPA:
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002147 case XFS_IOC_GETBMAPX:
Christoph Hellwig232b51942017-10-17 14:16:19 -07002148 return xfs_ioc_getbmap(filp, cmd, arg);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002149
Darrick J. Wonge89c0412017-03-28 14:56:37 -07002150 case FS_IOC_GETFSMAP:
2151 return xfs_ioc_getfsmap(ip, arg);
2152
Darrick J. Wong36fd6e82017-10-17 21:37:34 -07002153 case XFS_IOC_SCRUB_METADATA:
2154 return xfs_ioc_scrub_metadata(ip, arg);
2155
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002156 case XFS_IOC_FD_TO_HANDLE:
2157 case XFS_IOC_PATH_TO_HANDLE:
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06002158 case XFS_IOC_PATH_TO_FSHANDLE: {
2159 xfs_fsop_handlereq_t hreq;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002160
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06002161 if (copy_from_user(&hreq, arg, sizeof(hreq)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002162 return -EFAULT;
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06002163 return xfs_find_handle(cmd, &hreq);
2164 }
2165 case XFS_IOC_OPEN_BY_HANDLE: {
2166 xfs_fsop_handlereq_t hreq;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002167
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06002168 if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002169 return -EFAULT;
Christoph Hellwigd296d302009-01-19 02:02:57 +01002170 return xfs_open_by_handle(filp, &hreq);
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06002171 }
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002172 case XFS_IOC_FSSETDM_BY_HANDLE:
Christoph Hellwigd296d302009-01-19 02:02:57 +01002173 return xfs_fssetdm_by_handle(filp, arg);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002174
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06002175 case XFS_IOC_READLINK_BY_HANDLE: {
2176 xfs_fsop_handlereq_t hreq;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002177
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06002178 if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002179 return -EFAULT;
Christoph Hellwigd296d302009-01-19 02:02:57 +01002180 return xfs_readlink_by_handle(filp, &hreq);
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06002181 }
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002182 case XFS_IOC_ATTRLIST_BY_HANDLE:
Christoph Hellwigd296d302009-01-19 02:02:57 +01002183 return xfs_attrlist_by_handle(filp, arg);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002184
2185 case XFS_IOC_ATTRMULTI_BY_HANDLE:
Christoph Hellwigd296d302009-01-19 02:02:57 +01002186 return xfs_attrmulti_by_handle(filp, arg);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002187
2188 case XFS_IOC_SWAPEXT: {
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06002189 struct xfs_swapext sxp;
2190
2191 if (copy_from_user(&sxp, arg, sizeof(xfs_swapext_t)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002192 return -EFAULT;
Jan Karad9457dc2012-06-12 16:20:39 +02002193 error = mnt_want_write_file(filp);
2194 if (error)
2195 return error;
Dave Chinnera133d952013-08-12 20:49:48 +10002196 error = xfs_ioc_swapext(&sxp);
Jan Karad9457dc2012-06-12 16:20:39 +02002197 mnt_drop_write_file(filp);
Dave Chinner24513372014-06-25 14:58:08 +10002198 return error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002199 }
2200
2201 case XFS_IOC_FSCOUNTS: {
2202 xfs_fsop_counts_t out;
2203
Eric Sandeen91083262019-05-01 20:26:30 -07002204 xfs_fs_counts(mp, &out);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002205
2206 if (copy_to_user(arg, &out, sizeof(out)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002207 return -EFAULT;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002208 return 0;
2209 }
2210
2211 case XFS_IOC_SET_RESBLKS: {
2212 xfs_fsop_resblks_t inout;
Darrick J. Wongc8ce5402017-06-16 11:00:05 -07002213 uint64_t in;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002214
2215 if (!capable(CAP_SYS_ADMIN))
2216 return -EPERM;
2217
Eric Sandeend5db0f92010-02-05 22:59:53 +00002218 if (mp->m_flags & XFS_MOUNT_RDONLY)
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002219 return -EROFS;
Eric Sandeend5db0f92010-02-05 22:59:53 +00002220
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002221 if (copy_from_user(&inout, arg, sizeof(inout)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002222 return -EFAULT;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002223
Jan Karad9457dc2012-06-12 16:20:39 +02002224 error = mnt_want_write_file(filp);
2225 if (error)
2226 return error;
2227
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002228 /* input parameter is passed in resblks field of structure */
2229 in = inout.resblks;
2230 error = xfs_reserve_blocks(mp, &in, &inout);
Jan Karad9457dc2012-06-12 16:20:39 +02002231 mnt_drop_write_file(filp);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002232 if (error)
Dave Chinner24513372014-06-25 14:58:08 +10002233 return error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002234
2235 if (copy_to_user(arg, &inout, sizeof(inout)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002236 return -EFAULT;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002237 return 0;
2238 }
2239
2240 case XFS_IOC_GET_RESBLKS: {
2241 xfs_fsop_resblks_t out;
2242
2243 if (!capable(CAP_SYS_ADMIN))
2244 return -EPERM;
2245
2246 error = xfs_reserve_blocks(mp, NULL, &out);
2247 if (error)
Dave Chinner24513372014-06-25 14:58:08 +10002248 return error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002249
2250 if (copy_to_user(arg, &out, sizeof(out)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002251 return -EFAULT;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002252
2253 return 0;
2254 }
2255
2256 case XFS_IOC_FSGROWFSDATA: {
2257 xfs_growfs_data_t in;
2258
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002259 if (copy_from_user(&in, arg, sizeof(in)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002260 return -EFAULT;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002261
Jan Karad9457dc2012-06-12 16:20:39 +02002262 error = mnt_want_write_file(filp);
2263 if (error)
2264 return error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002265 error = xfs_growfs_data(mp, &in);
Jan Karad9457dc2012-06-12 16:20:39 +02002266 mnt_drop_write_file(filp);
Dave Chinner24513372014-06-25 14:58:08 +10002267 return error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002268 }
2269
2270 case XFS_IOC_FSGROWFSLOG: {
2271 xfs_growfs_log_t in;
2272
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002273 if (copy_from_user(&in, arg, sizeof(in)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002274 return -EFAULT;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002275
Jan Karad9457dc2012-06-12 16:20:39 +02002276 error = mnt_want_write_file(filp);
2277 if (error)
2278 return error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002279 error = xfs_growfs_log(mp, &in);
Jan Karad9457dc2012-06-12 16:20:39 +02002280 mnt_drop_write_file(filp);
Dave Chinner24513372014-06-25 14:58:08 +10002281 return error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002282 }
2283
2284 case XFS_IOC_FSGROWFSRT: {
2285 xfs_growfs_rt_t in;
2286
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002287 if (copy_from_user(&in, arg, sizeof(in)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002288 return -EFAULT;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002289
Jan Karad9457dc2012-06-12 16:20:39 +02002290 error = mnt_want_write_file(filp);
2291 if (error)
2292 return error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002293 error = xfs_growfs_rt(mp, &in);
Jan Karad9457dc2012-06-12 16:20:39 +02002294 mnt_drop_write_file(filp);
Dave Chinner24513372014-06-25 14:58:08 +10002295 return error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002296 }
2297
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002298 case XFS_IOC_GOINGDOWN: {
Darrick J. Wongc8ce5402017-06-16 11:00:05 -07002299 uint32_t in;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002300
2301 if (!capable(CAP_SYS_ADMIN))
2302 return -EPERM;
2303
Darrick J. Wongc8ce5402017-06-16 11:00:05 -07002304 if (get_user(in, (uint32_t __user *)arg))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002305 return -EFAULT;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002306
Dave Chinner24513372014-06-25 14:58:08 +10002307 return xfs_fs_goingdown(mp, in);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002308 }
2309
2310 case XFS_IOC_ERROR_INJECTION: {
2311 xfs_error_injection_t in;
2312
2313 if (!capable(CAP_SYS_ADMIN))
2314 return -EPERM;
2315
2316 if (copy_from_user(&in, arg, sizeof(in)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002317 return -EFAULT;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002318
Darrick J. Wong31965ef2017-06-20 17:54:46 -07002319 return xfs_errortag_add(mp, in.errtag);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002320 }
2321
2322 case XFS_IOC_ERROR_CLEARALL:
2323 if (!capable(CAP_SYS_ADMIN))
2324 return -EPERM;
2325
Darrick J. Wong31965ef2017-06-20 17:54:46 -07002326 return xfs_errortag_clearall(mp);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002327
Brian Foster8ca149d2012-11-07 12:21:12 -05002328 case XFS_IOC_FREE_EOFBLOCKS: {
Dwight Engenb9fe5052013-08-15 14:08:02 -04002329 struct xfs_fs_eofblocks eofb;
2330 struct xfs_eofblocks keofb;
Brian Foster8ca149d2012-11-07 12:21:12 -05002331
Dwight Engen8c567a72013-08-15 14:08:03 -04002332 if (!capable(CAP_SYS_ADMIN))
2333 return -EPERM;
2334
2335 if (mp->m_flags & XFS_MOUNT_RDONLY)
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002336 return -EROFS;
Dwight Engen8c567a72013-08-15 14:08:03 -04002337
Brian Foster8ca149d2012-11-07 12:21:12 -05002338 if (copy_from_user(&eofb, arg, sizeof(eofb)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002339 return -EFAULT;
Brian Foster8ca149d2012-11-07 12:21:12 -05002340
Dwight Engenb9fe5052013-08-15 14:08:02 -04002341 error = xfs_fs_eofblocks_from_user(&eofb, &keofb);
2342 if (error)
Dave Chinner24513372014-06-25 14:58:08 +10002343 return error;
Brian Foster8ca149d2012-11-07 12:21:12 -05002344
Dave Chinner24513372014-06-25 14:58:08 +10002345 return xfs_icache_free_eofblocks(mp, &keofb);
Brian Foster8ca149d2012-11-07 12:21:12 -05002346 }
2347
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002348 default:
2349 return -ENOTTY;
2350 }
2351}