blob: 2af73d664613dfea99da5b7f9cec2b715772acda [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"
Darrick J. Wong5f213dd2019-11-06 17:19:33 -080037#include "xfs_ioctl.h"
Christoph Hellwiga2544622020-02-26 17:30:33 -080038#include "xfs_da_format.h"
39#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/mount.h>
42#include <linux/namei.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44/*
45 * xfs_find_handle maps from userspace xfs_fsop_handlereq structure to
46 * a file or fs handle.
47 *
48 * XFS_IOC_PATH_TO_FSHANDLE
49 * returns fs handle for a mount point or path within that mount point
50 * XFS_IOC_FD_TO_HANDLE
51 * returns full handle for a FD opened in user space
52 * XFS_IOC_PATH_TO_HANDLE
53 * returns full handle for a path
54 */
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -060055int
Linus Torvalds1da177e2005-04-16 15:20:36 -070056xfs_find_handle(
57 unsigned int cmd,
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -060058 xfs_fsop_handlereq_t *hreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
60 int hsize;
61 xfs_handle_t handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 struct inode *inode;
Dave Chinnera30b0362013-09-02 20:49:36 +100063 struct fd f = {NULL};
Christoph Hellwig4346cdd2009-02-08 21:51:14 +010064 struct path path;
Al Viro2903ff02012-08-28 12:52:22 -040065 int error;
Christoph Hellwig4346cdd2009-02-08 21:51:14 +010066 struct xfs_inode *ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Christoph Hellwig4346cdd2009-02-08 21:51:14 +010068 if (cmd == XFS_IOC_FD_TO_HANDLE) {
Al Viro2903ff02012-08-28 12:52:22 -040069 f = fdget(hreq->fd);
70 if (!f.file)
Christoph Hellwig4346cdd2009-02-08 21:51:14 +010071 return -EBADF;
Al Viro496ad9a2013-01-23 17:07:38 -050072 inode = file_inode(f.file);
Christoph Hellwig4346cdd2009-02-08 21:51:14 +010073 } else {
Al Viroce6595a2019-07-14 16:42:44 -040074 error = user_path_at(AT_FDCWD, hreq->path, 0, &path);
Christoph Hellwig4346cdd2009-02-08 21:51:14 +010075 if (error)
76 return error;
David Howells2b0143b2015-03-17 22:25:59 +000077 inode = d_inode(path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 }
Christoph Hellwig4346cdd2009-02-08 21:51:14 +010079 ip = XFS_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Christoph Hellwig4346cdd2009-02-08 21:51:14 +010081 /*
82 * We can only generate handles for inodes residing on a XFS filesystem,
83 * and only for regular files, directories or symbolic links.
84 */
85 error = -EINVAL;
86 if (inode->i_sb->s_magic != XFS_SB_MAGIC)
87 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Christoph Hellwig4346cdd2009-02-08 21:51:14 +010089 error = -EBADF;
90 if (!S_ISREG(inode->i_mode) &&
91 !S_ISDIR(inode->i_mode) &&
92 !S_ISLNK(inode->i_mode))
93 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Christoph Hellwig4346cdd2009-02-08 21:51:14 +010096 memcpy(&handle.ha_fsid, ip->i_mount->m_fixedfsid, sizeof(xfs_fsid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Christoph Hellwig4346cdd2009-02-08 21:51:14 +010098 if (cmd == XFS_IOC_PATH_TO_FSHANDLE) {
99 /*
100 * This handle only contains an fsid, zero the rest.
101 */
102 memset(&handle.ha_fid, 0, sizeof(handle.ha_fid));
103 hsize = sizeof(xfs_fsid_t);
104 } else {
Christoph Hellwigc6143912007-09-14 15:22:37 +1000105 handle.ha_fid.fid_len = sizeof(xfs_fid_t) -
106 sizeof(handle.ha_fid.fid_len);
107 handle.ha_fid.fid_pad = 0;
Dave Chinner9e9a2672016-02-09 16:54:58 +1100108 handle.ha_fid.fid_gen = inode->i_generation;
Christoph Hellwigc6143912007-09-14 15:22:37 +1000109 handle.ha_fid.fid_ino = ip->i_ino;
Christoph Hellwig3398a402017-06-14 21:30:44 -0700110 hsize = sizeof(xfs_handle_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 }
112
Christoph Hellwig4346cdd2009-02-08 21:51:14 +0100113 error = -EFAULT;
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -0600114 if (copy_to_user(hreq->ohandle, &handle, hsize) ||
Christoph Hellwig4346cdd2009-02-08 21:51:14 +0100115 copy_to_user(hreq->ohandlen, &hsize, sizeof(__s32)))
116 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Christoph Hellwig4346cdd2009-02-08 21:51:14 +0100118 error = 0;
119
120 out_put:
121 if (cmd == XFS_IOC_FD_TO_HANDLE)
Al Viro2903ff02012-08-28 12:52:22 -0400122 fdput(f);
Christoph Hellwig4346cdd2009-02-08 21:51:14 +0100123 else
124 path_put(&path);
125 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}
127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128/*
Christoph Hellwigd296d302009-01-19 02:02:57 +0100129 * No need to do permission checks on the various pathname components
130 * as the handle operations are privileged.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 */
132STATIC int
Christoph Hellwigd296d302009-01-19 02:02:57 +0100133xfs_handle_acceptable(
134 void *context,
135 struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
Christoph Hellwigd296d302009-01-19 02:02:57 +0100137 return 1;
138}
139
140/*
141 * Convert userspace handle data into a dentry.
142 */
143struct dentry *
144xfs_handle_to_dentry(
145 struct file *parfilp,
146 void __user *uhandle,
147 u32 hlen)
148{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 xfs_handle_t handle;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100150 struct xfs_fid64 fid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152 /*
153 * Only allow handle opens under a directory.
154 */
Al Viro496ad9a2013-01-23 17:07:38 -0500155 if (!S_ISDIR(file_inode(parfilp)->i_mode))
Christoph Hellwigd296d302009-01-19 02:02:57 +0100156 return ERR_PTR(-ENOTDIR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Christoph Hellwigd296d302009-01-19 02:02:57 +0100158 if (hlen != sizeof(xfs_handle_t))
159 return ERR_PTR(-EINVAL);
160 if (copy_from_user(&handle, uhandle, hlen))
161 return ERR_PTR(-EFAULT);
162 if (handle.ha_fid.fid_len !=
163 sizeof(handle.ha_fid) - sizeof(handle.ha_fid.fid_len))
164 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Christoph Hellwigd296d302009-01-19 02:02:57 +0100166 memset(&fid, 0, sizeof(struct fid));
167 fid.ino = handle.ha_fid.fid_ino;
168 fid.gen = handle.ha_fid.fid_gen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Christoph Hellwigd296d302009-01-19 02:02:57 +0100170 return exportfs_decode_fh(parfilp->f_path.mnt, (struct fid *)&fid, 3,
171 FILEID_INO32_GEN | XFS_FILEID_TYPE_64FLAG,
172 xfs_handle_acceptable, NULL);
173}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Christoph Hellwigd296d302009-01-19 02:02:57 +0100175STATIC struct dentry *
176xfs_handlereq_to_dentry(
177 struct file *parfilp,
178 xfs_fsop_handlereq_t *hreq)
179{
180 return xfs_handle_to_dentry(parfilp, hreq->ihandle, hreq->ihandlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181}
182
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -0600183int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184xfs_open_by_handle(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 struct file *parfilp,
Christoph Hellwigd296d302009-01-19 02:02:57 +0100186 xfs_fsop_handlereq_t *hreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
David Howells745ca242008-11-14 10:39:22 +1100188 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 int error;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100190 int fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 int permflag;
192 struct file *filp;
193 struct inode *inode;
194 struct dentry *dentry;
Dave Chinner1a1d7722012-03-22 05:15:06 +0000195 fmode_t fmode;
Al Viro765927b2012-06-26 21:58:53 +0400196 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198 if (!capable(CAP_SYS_ADMIN))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000199 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Christoph Hellwigd296d302009-01-19 02:02:57 +0100201 dentry = xfs_handlereq_to_dentry(parfilp, hreq);
202 if (IS_ERR(dentry))
203 return PTR_ERR(dentry);
David Howells2b0143b2015-03-17 22:25:59 +0000204 inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206 /* Restrict xfs_open_by_handle to directories & regular files. */
207 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode))) {
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000208 error = -EPERM;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100209 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 }
211
212#if BITS_PER_LONG != 32
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -0600213 hreq->oflags |= O_LARGEFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214#endif
Christoph Hellwigd296d302009-01-19 02:02:57 +0100215
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -0600216 permflag = hreq->oflags;
Dave Chinner1a1d7722012-03-22 05:15:06 +0000217 fmode = OPEN_FMODE(permflag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 if ((!(permflag & O_APPEND) || (permflag & O_TRUNC)) &&
Dave Chinner1a1d7722012-03-22 05:15:06 +0000219 (fmode & FMODE_WRITE) && IS_APPEND(inode)) {
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000220 error = -EPERM;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100221 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 }
223
Dave Chinner1a1d7722012-03-22 05:15:06 +0000224 if ((fmode & FMODE_WRITE) && IS_IMMUTABLE(inode)) {
Eryu Guan337684a2016-08-02 19:58:28 +0800225 error = -EPERM;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100226 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 }
228
229 /* Can't write directories. */
Dave Chinner1a1d7722012-03-22 05:15:06 +0000230 if (S_ISDIR(inode->i_mode) && (fmode & FMODE_WRITE)) {
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000231 error = -EISDIR;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100232 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 }
234
Yann Droneaud862a6292013-07-02 18:39:34 +0200235 fd = get_unused_fd_flags(0);
Christoph Hellwigd296d302009-01-19 02:02:57 +0100236 if (fd < 0) {
237 error = fd;
238 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 }
240
Al Viro765927b2012-06-26 21:58:53 +0400241 path.mnt = parfilp->f_path.mnt;
242 path.dentry = dentry;
243 filp = dentry_open(&path, hreq->oflags, cred);
244 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 if (IS_ERR(filp)) {
Christoph Hellwigd296d302009-01-19 02:02:57 +0100246 put_unused_fd(fd);
247 return PTR_ERR(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 }
Christoph Hellwig4d4be482008-12-09 04:47:33 -0500249
Al Viro03209372011-07-25 20:54:24 -0400250 if (S_ISREG(inode->i_mode)) {
Vlad Apostolov2e2e7bb2006-11-11 18:04:47 +1100251 filp->f_flags |= O_NOATIME;
Christoph Hellwig4d4be482008-12-09 04:47:33 -0500252 filp->f_mode |= FMODE_NOCMTIME;
Vlad Apostolov2e2e7bb2006-11-11 18:04:47 +1100253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Christoph Hellwigd296d302009-01-19 02:02:57 +0100255 fd_install(fd, filp);
256 return fd;
257
258 out_dput:
259 dput(dentry);
260 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261}
262
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -0600263int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264xfs_readlink_by_handle(
Christoph Hellwigd296d302009-01-19 02:02:57 +0100265 struct file *parfilp,
266 xfs_fsop_handlereq_t *hreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
Christoph Hellwigd296d302009-01-19 02:02:57 +0100268 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 __u32 olen;
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000270 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 if (!capable(CAP_SYS_ADMIN))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000273 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Christoph Hellwigd296d302009-01-19 02:02:57 +0100275 dentry = xfs_handlereq_to_dentry(parfilp, hreq);
276 if (IS_ERR(dentry))
277 return PTR_ERR(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279 /* Restrict this handle operation to symlinks only. */
Miklos Szeredifd4a0edf2016-12-09 16:45:04 +0100280 if (!d_is_symlink(dentry)) {
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000281 error = -EINVAL;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100282 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 }
284
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -0600285 if (copy_from_user(&olen, hreq->ohandlen, sizeof(__u32))) {
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000286 error = -EFAULT;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100287 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Miklos Szeredifd4a0edf2016-12-09 16:45:04 +0100290 error = vfs_readlink(dentry, hreq->ohandle, olen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Christoph Hellwigd296d302009-01-19 02:02:57 +0100292 out_dput:
293 dput(dentry);
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000294 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295}
296
Christoph Hellwig3e7a7792020-02-26 17:30:38 -0800297/*
298 * Format an attribute and copy it out to the user's buffer.
299 * Take care to check values and protect against them changing later,
300 * we may be reading them directly out of a user buffer.
301 */
302static void
303xfs_ioc_attr_put_listent(
304 struct xfs_attr_list_context *context,
305 int flags,
306 unsigned char *name,
307 int namelen,
308 int valuelen)
309{
310 struct xfs_attrlist *alist = context->buffer;
311 struct xfs_attrlist_ent *aep;
312 int arraytop;
313
314 ASSERT(!context->seen_enough);
315 ASSERT(context->count >= 0);
316 ASSERT(context->count < (ATTR_MAX_VALUELEN/8));
317 ASSERT(context->firstu >= sizeof(*alist));
318 ASSERT(context->firstu <= context->bufsize);
319
320 /*
321 * Only list entries in the right namespace.
322 */
Christoph Hellwigd5f0f492020-02-26 17:30:42 -0800323 if (context->attr_filter != (flags & XFS_ATTR_NSP_ONDISK_MASK))
Christoph Hellwig3e7a7792020-02-26 17:30:38 -0800324 return;
325
326 arraytop = sizeof(*alist) +
327 context->count * sizeof(alist->al_offset[0]);
328
329 /* decrement by the actual bytes used by the attr */
330 context->firstu -= round_up(offsetof(struct xfs_attrlist_ent, a_name) +
331 namelen + 1, sizeof(uint32_t));
332 if (context->firstu < arraytop) {
333 trace_xfs_attr_list_full(context);
334 alist->al_more = 1;
335 context->seen_enough = 1;
336 return;
337 }
338
339 aep = context->buffer + context->firstu;
340 aep->a_valuelen = valuelen;
341 memcpy(aep->a_name, name, namelen);
342 aep->a_name[namelen] = 0;
343 alist->al_offset[context->count++] = context->firstu;
344 alist->al_count = context->count;
345 trace_xfs_attr_list_add(context);
346}
347
Christoph Hellwigd5f0f492020-02-26 17:30:42 -0800348static unsigned int
349xfs_attr_filter(
350 u32 ioc_flags)
351{
352 if (ioc_flags & XFS_IOC_ATTR_ROOT)
353 return XFS_ATTR_ROOT;
354 if (ioc_flags & XFS_IOC_ATTR_SECURE)
355 return XFS_ATTR_SECURE;
356 return 0;
357}
358
359static unsigned int
360xfs_attr_flags(
361 u32 ioc_flags)
362{
363 if (ioc_flags & XFS_IOC_ATTR_CREATE)
364 return XATTR_CREATE;
365 if (ioc_flags & XFS_IOC_ATTR_REPLACE)
366 return XATTR_REPLACE;
367 return 0;
368}
369
Christoph Hellwig3e7a7792020-02-26 17:30:38 -0800370int
371xfs_ioc_attr_list(
372 struct xfs_inode *dp,
Christoph Hellwigeb241c72020-02-26 17:30:40 -0800373 void __user *ubuf,
Christoph Hellwig3e7a7792020-02-26 17:30:38 -0800374 int bufsize,
375 int flags,
Christoph Hellwig53ac39f2020-02-26 17:30:41 -0800376 struct xfs_attrlist_cursor __user *ucursor)
Christoph Hellwig3e7a7792020-02-26 17:30:38 -0800377{
Christoph Hellwige3a19cd2020-02-26 17:30:43 -0800378 struct xfs_attr_list_context context = { };
Christoph Hellwig3e7a7792020-02-26 17:30:38 -0800379 struct xfs_attrlist *alist;
Christoph Hellwigeb241c72020-02-26 17:30:40 -0800380 void *buffer;
Christoph Hellwig3e7a7792020-02-26 17:30:38 -0800381 int error;
382
Christoph Hellwigf6046312020-02-26 17:30:40 -0800383 if (bufsize < sizeof(struct xfs_attrlist) ||
384 bufsize > XFS_XATTR_LIST_MAX)
385 return -EINVAL;
386
387 /*
388 * Reject flags, only allow namespaces.
389 */
Christoph Hellwigd5f0f492020-02-26 17:30:42 -0800390 if (flags & ~(XFS_IOC_ATTR_ROOT | XFS_IOC_ATTR_SECURE))
Christoph Hellwigf6046312020-02-26 17:30:40 -0800391 return -EINVAL;
Christoph Hellwigd5f0f492020-02-26 17:30:42 -0800392 if (flags == (XFS_IOC_ATTR_ROOT | XFS_IOC_ATTR_SECURE))
Christoph Hellwigf6046312020-02-26 17:30:40 -0800393 return -EINVAL;
394
Christoph Hellwig3e7a7792020-02-26 17:30:38 -0800395 /*
396 * Validate the cursor.
397 */
Christoph Hellwige3a19cd2020-02-26 17:30:43 -0800398 if (copy_from_user(&context.cursor, ucursor, sizeof(context.cursor)))
Christoph Hellwig53ac39f2020-02-26 17:30:41 -0800399 return -EFAULT;
Christoph Hellwige3a19cd2020-02-26 17:30:43 -0800400 if (context.cursor.pad1 || context.cursor.pad2)
Christoph Hellwig3e7a7792020-02-26 17:30:38 -0800401 return -EINVAL;
Christoph Hellwige3a19cd2020-02-26 17:30:43 -0800402 if (!context.cursor.initted &&
403 (context.cursor.hashval || context.cursor.blkno ||
404 context.cursor.offset))
Christoph Hellwig3e7a7792020-02-26 17:30:38 -0800405 return -EINVAL;
406
Christoph Hellwigeb241c72020-02-26 17:30:40 -0800407 buffer = kmem_zalloc_large(bufsize, 0);
408 if (!buffer)
409 return -ENOMEM;
Christoph Hellwig3e7a7792020-02-26 17:30:38 -0800410
411 /*
412 * Initialize the output buffer.
413 */
Christoph Hellwig3e7a7792020-02-26 17:30:38 -0800414 context.dp = dp;
Christoph Hellwig3e7a7792020-02-26 17:30:38 -0800415 context.resynch = 1;
Christoph Hellwigd5f0f492020-02-26 17:30:42 -0800416 context.attr_filter = xfs_attr_filter(flags);
Christoph Hellwig3e7a7792020-02-26 17:30:38 -0800417 context.buffer = buffer;
418 context.bufsize = (bufsize & ~(sizeof(int)-1)); /* align */
419 context.firstu = context.bufsize;
420 context.put_listent = xfs_ioc_attr_put_listent;
421
422 alist = context.buffer;
423 alist->al_count = 0;
424 alist->al_more = 0;
425 alist->al_offset[0] = context.bufsize;
426
Christoph Hellwig17e1dd82020-02-26 17:30:39 -0800427 error = xfs_attr_list(&context);
Christoph Hellwigeb241c72020-02-26 17:30:40 -0800428 if (error)
429 goto out_free;
430
Christoph Hellwig53ac39f2020-02-26 17:30:41 -0800431 if (copy_to_user(ubuf, buffer, bufsize) ||
Christoph Hellwige3a19cd2020-02-26 17:30:43 -0800432 copy_to_user(ucursor, &context.cursor, sizeof(context.cursor)))
Christoph Hellwigeb241c72020-02-26 17:30:40 -0800433 error = -EFAULT;
434out_free:
435 kmem_free(buffer);
Christoph Hellwig3e7a7792020-02-26 17:30:38 -0800436 return error;
437}
438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439STATIC int
440xfs_attrlist_by_handle(
Christoph Hellwigd296d302009-01-19 02:02:57 +0100441 struct file *parfilp,
Christoph Hellwig53ac39f2020-02-26 17:30:41 -0800442 struct xfs_fsop_attrlist_handlereq __user *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
Christoph Hellwig53ac39f2020-02-26 17:30:41 -0800444 struct xfs_fsop_attrlist_handlereq al_hreq;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100445 struct dentry *dentry;
Christoph Hellwig53ac39f2020-02-26 17:30:41 -0800446 int error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448 if (!capable(CAP_SYS_ADMIN))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000449 return -EPERM;
Christoph Hellwig53ac39f2020-02-26 17:30:41 -0800450 if (copy_from_user(&al_hreq, p, sizeof(al_hreq)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000451 return -EFAULT;
Christoph Hellwig90ad58a2008-06-27 13:32:19 +1000452
Christoph Hellwigd296d302009-01-19 02:02:57 +0100453 dentry = xfs_handlereq_to_dentry(parfilp, &al_hreq.hreq);
454 if (IS_ERR(dentry))
455 return PTR_ERR(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Christoph Hellwigeb241c72020-02-26 17:30:40 -0800457 error = xfs_ioc_attr_list(XFS_I(d_inode(dentry)), al_hreq.buffer,
Christoph Hellwig53ac39f2020-02-26 17:30:41 -0800458 al_hreq.buflen, al_hreq.flags, &p->pos);
Christoph Hellwigd296d302009-01-19 02:02:57 +0100459 dput(dentry);
460 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461}
462
Christoph Hellwigd0ce64392020-02-26 17:30:31 -0800463static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464xfs_attrmulti_attr_get(
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000465 struct inode *inode,
Dave Chinnera9273ca2010-01-20 10:47:48 +1100466 unsigned char *name,
467 unsigned char __user *ubuf,
Darrick J. Wongc8ce5402017-06-16 11:00:05 -0700468 uint32_t *len,
469 uint32_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
Christoph Hellwige5171d72020-02-26 17:30:34 -0800471 struct xfs_da_args args = {
472 .dp = XFS_I(inode),
Christoph Hellwigd5f0f492020-02-26 17:30:42 -0800473 .attr_filter = xfs_attr_filter(flags),
474 .attr_flags = xfs_attr_flags(flags),
Christoph Hellwige5171d72020-02-26 17:30:34 -0800475 .name = name,
476 .namelen = strlen(name),
477 .valuelen = *len,
478 };
479 int error;
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000480
Jan Tulak51fcbfe2015-10-12 16:03:59 +1100481 if (*len > XFS_XATTR_SIZE_MAX)
Dave Chinner24513372014-06-25 14:58:08 +1000482 return -EINVAL;
Christoph Hellwige5171d72020-02-26 17:30:34 -0800483
484 args.value = kmem_zalloc_large(*len, 0);
485 if (!args.value)
Dave Chinner24513372014-06-25 14:58:08 +1000486 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Christoph Hellwige5171d72020-02-26 17:30:34 -0800488 error = xfs_attr_get(&args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 if (error)
490 goto out_kfree;
491
Christoph Hellwige5171d72020-02-26 17:30:34 -0800492 *len = args.valuelen;
493 if (copy_to_user(ubuf, args.value, args.valuelen))
Dave Chinner24513372014-06-25 14:58:08 +1000494 error = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Dave Chinnerfdd3cce2013-09-02 20:53:00 +1000496out_kfree:
Christoph Hellwige5171d72020-02-26 17:30:34 -0800497 kmem_free(args.value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 return error;
499}
500
Christoph Hellwigd0ce64392020-02-26 17:30:31 -0800501static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502xfs_attrmulti_attr_set(
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000503 struct inode *inode,
Dave Chinnera9273ca2010-01-20 10:47:48 +1100504 unsigned char *name,
505 const unsigned char __user *ubuf,
Darrick J. Wongc8ce5402017-06-16 11:00:05 -0700506 uint32_t len,
507 uint32_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
Christoph Hellwiga2544622020-02-26 17:30:33 -0800509 struct xfs_da_args args = {
510 .dp = XFS_I(inode),
Christoph Hellwigd5f0f492020-02-26 17:30:42 -0800511 .attr_filter = xfs_attr_filter(flags),
512 .attr_flags = xfs_attr_flags(flags),
Christoph Hellwiga2544622020-02-26 17:30:33 -0800513 .name = name,
514 .namelen = strlen(name),
515 };
Andreas Gruenbacher09cb22d2015-11-03 12:53:54 +1100516 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000518 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
Dave Chinner24513372014-06-25 14:58:08 +1000519 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Christoph Hellwig6cc4f4f2020-02-26 17:30:30 -0800521 if (ubuf) {
522 if (len > XFS_XATTR_SIZE_MAX)
523 return -EINVAL;
Christoph Hellwiga2544622020-02-26 17:30:33 -0800524 args.value = memdup_user(ubuf, len);
525 if (IS_ERR(args.value))
526 return PTR_ERR(args.value);
527 args.valuelen = len;
Christoph Hellwig6cc4f4f2020-02-26 17:30:30 -0800528 }
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000529
Christoph Hellwiga2544622020-02-26 17:30:33 -0800530 error = xfs_attr_set(&args);
Christoph Hellwigd5f0f492020-02-26 17:30:42 -0800531 if (!error && (flags & XFS_IOC_ATTR_ROOT))
Christoph Hellwig5a3930e2020-02-26 17:30:41 -0800532 xfs_forget_acl(inode, name);
Christoph Hellwiga2544622020-02-26 17:30:33 -0800533 kfree(args.value);
Andreas Gruenbacher09cb22d2015-11-03 12:53:54 +1100534 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535}
536
Christoph Hellwigd0ce64392020-02-26 17:30:31 -0800537int
538xfs_ioc_attrmulti_one(
539 struct file *parfilp,
540 struct inode *inode,
541 uint32_t opcode,
542 void __user *uname,
543 void __user *value,
544 uint32_t *len,
545 uint32_t flags)
546{
547 unsigned char *name;
548 int error;
549
Christoph Hellwigd5f0f492020-02-26 17:30:42 -0800550 if ((flags & XFS_IOC_ATTR_ROOT) && (flags & XFS_IOC_ATTR_SECURE))
Christoph Hellwigd0ce64392020-02-26 17:30:31 -0800551 return -EINVAL;
Christoph Hellwigd0ce64392020-02-26 17:30:31 -0800552
553 name = strndup_user(uname, MAXNAMELEN);
554 if (IS_ERR(name))
555 return PTR_ERR(name);
556
557 switch (opcode) {
558 case ATTR_OP_GET:
559 error = xfs_attrmulti_attr_get(inode, name, value, len, flags);
560 break;
561 case ATTR_OP_REMOVE:
562 value = NULL;
563 *len = 0;
564 /* fall through */
565 case ATTR_OP_SET:
566 error = mnt_want_write_file(parfilp);
567 if (error)
568 break;
569 error = xfs_attrmulti_attr_set(inode, name, value, *len, flags);
570 mnt_drop_write_file(parfilp);
571 break;
572 default:
573 error = -EINVAL;
574 break;
575 }
576
577 kfree(name);
578 return error;
579}
580
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581STATIC int
582xfs_attrmulti_by_handle(
Dave Hansen42a74f22008-02-15 14:37:46 -0800583 struct file *parfilp,
Christoph Hellwigd296d302009-01-19 02:02:57 +0100584 void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585{
586 int error;
587 xfs_attr_multiop_t *ops;
588 xfs_fsop_attrmulti_handlereq_t am_hreq;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100589 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 unsigned int i, size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
592 if (!capable(CAP_SYS_ADMIN))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000593 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 if (copy_from_user(&am_hreq, arg, sizeof(xfs_fsop_attrmulti_handlereq_t)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000595 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Zhitong Wangfda168c2010-03-23 09:51:22 +1100597 /* overflow check */
598 if (am_hreq.opcount >= INT_MAX / sizeof(xfs_attr_multiop_t))
599 return -E2BIG;
600
Christoph Hellwigd296d302009-01-19 02:02:57 +0100601 dentry = xfs_handlereq_to_dentry(parfilp, &am_hreq.hreq);
602 if (IS_ERR(dentry))
603 return PTR_ERR(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Dave Chinner24513372014-06-25 14:58:08 +1000605 error = -E2BIG;
Christoph Hellwige182f572008-06-27 13:32:31 +1000606 size = am_hreq.opcount * sizeof(xfs_attr_multiop_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 if (!size || size > 16 * PAGE_SIZE)
Christoph Hellwigd296d302009-01-19 02:02:57 +0100608 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Li Zefan0e639bd2009-04-08 15:08:04 +0800610 ops = memdup_user(am_hreq.ops, size);
611 if (IS_ERR(ops)) {
Dave Chinner24513372014-06-25 14:58:08 +1000612 error = PTR_ERR(ops);
Christoph Hellwigd296d302009-01-19 02:02:57 +0100613 goto out_dput;
Li Zefan0e639bd2009-04-08 15:08:04 +0800614 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 error = 0;
617 for (i = 0; i < am_hreq.opcount; i++) {
Christoph Hellwigd0ce64392020-02-26 17:30:31 -0800618 ops[i].am_error = xfs_ioc_attrmulti_one(parfilp,
619 d_inode(dentry), ops[i].am_opcode,
620 ops[i].am_attrname, ops[i].am_attrvalue,
621 &ops[i].am_length, ops[i].am_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 }
623
624 if (copy_to_user(am_hreq.ops, ops, size))
Dave Chinner24513372014-06-25 14:58:08 +1000625 error = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 kfree(ops);
Christoph Hellwigd296d302009-01-19 02:02:57 +0100628 out_dput:
629 dput(dentry);
Dave Chinner24513372014-06-25 14:58:08 +1000630 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631}
632
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -0600633int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634xfs_ioc_space(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 struct file *filp,
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -0600636 xfs_flock64_t *bf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
Christoph Hellwig8f3e2052016-07-20 11:29:35 +1000638 struct inode *inode = file_inode(filp);
639 struct xfs_inode *ip = XFS_I(inode);
Christoph Hellwig865e9442013-10-12 00:55:08 -0700640 struct iattr iattr;
Christoph Hellwig837a6e72019-10-24 22:26:02 -0700641 enum xfs_prealloc_flags flags = XFS_PREALLOC_CLEAR;
Dan Williamsc63a8ea2018-03-12 14:12:29 -0700642 uint iolock = XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 int error;
644
Alexey Dobriyanf37ea142006-09-28 10:52:04 +1000645 if (inode->i_flags & (S_IMMUTABLE|S_APPEND))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000646 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Eric Sandeenad4a8ac2005-09-02 16:41:16 +1000648 if (!(filp->f_mode & FMODE_WRITE))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000649 return -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Alexey Dobriyanf37ea142006-09-28 10:52:04 +1000651 if (!S_ISREG(inode->i_mode))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000652 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Christoph Hellwig7a42c702019-10-24 22:26:27 -0700654 if (xfs_is_always_cow_inode(ip))
655 return -EOPNOTSUPP;
656
Christoph Hellwig8add71c2015-02-02 09:53:56 +1100657 if (filp->f_flags & O_DSYNC)
658 flags |= XFS_PREALLOC_SYNC;
Christoph Hellwig8f3e2052016-07-20 11:29:35 +1000659 if (filp->f_mode & FMODE_NOCMTIME)
Christoph Hellwig8add71c2015-02-02 09:53:56 +1100660 flags |= XFS_PREALLOC_INVISIBLE;
661
Jan Karad9457dc2012-06-12 16:20:39 +0200662 error = mnt_want_write_file(filp);
663 if (error)
664 return error;
Christoph Hellwig865e9442013-10-12 00:55:08 -0700665
Christoph Hellwig781355c2015-02-16 11:59:50 +1100666 xfs_ilock(ip, iolock);
Dan Williams69eb5fa2018-03-20 14:42:38 -0700667 error = xfs_break_layouts(inode, &iolock, BREAK_UNMAP);
Christoph Hellwig781355c2015-02-16 11:59:50 +1100668 if (error)
669 goto out_unlock;
Dave Chinner249bd902019-10-29 13:04:32 -0700670 inode_dio_wait(inode);
Christoph Hellwig865e9442013-10-12 00:55:08 -0700671
672 switch (bf->l_whence) {
673 case 0: /*SEEK_SET*/
674 break;
675 case 1: /*SEEK_CUR*/
676 bf->l_start += filp->f_pos;
677 break;
678 case 2: /*SEEK_END*/
679 bf->l_start += XFS_ISIZE(ip);
680 break;
681 default:
Dave Chinner24513372014-06-25 14:58:08 +1000682 error = -EINVAL;
Christoph Hellwig865e9442013-10-12 00:55:08 -0700683 goto out_unlock;
684 }
685
Christoph Hellwig837a6e72019-10-24 22:26:02 -0700686 if (bf->l_start < 0 || bf->l_start > inode->i_sb->s_maxbytes) {
Dave Chinner24513372014-06-25 14:58:08 +1000687 error = -EINVAL;
Christoph Hellwig865e9442013-10-12 00:55:08 -0700688 goto out_unlock;
689 }
690
Christoph Hellwig837a6e72019-10-24 22:26:02 -0700691 if (bf->l_start > XFS_ISIZE(ip)) {
692 error = xfs_alloc_file_space(ip, XFS_ISIZE(ip),
693 bf->l_start - XFS_ISIZE(ip), 0);
694 if (error)
695 goto out_unlock;
Christoph Hellwig865e9442013-10-12 00:55:08 -0700696 }
697
Christoph Hellwig837a6e72019-10-24 22:26:02 -0700698 iattr.ia_valid = ATTR_SIZE;
699 iattr.ia_size = bf->l_start;
700 error = xfs_vn_setattr_size(file_dentry(filp), &iattr);
Christoph Hellwig865e9442013-10-12 00:55:08 -0700701 if (error)
702 goto out_unlock;
703
Christoph Hellwig8add71c2015-02-02 09:53:56 +1100704 error = xfs_update_prealloc_flags(ip, flags);
Christoph Hellwig865e9442013-10-12 00:55:08 -0700705
706out_unlock:
Christoph Hellwig781355c2015-02-16 11:59:50 +1100707 xfs_iunlock(ip, iolock);
Jan Karad9457dc2012-06-12 16:20:39 +0200708 mnt_drop_write_file(filp);
Dave Chinner24513372014-06-25 14:58:08 +1000709 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710}
711
Darrick J. Wong2810bd62019-07-02 09:39:40 -0700712/* Return 0 on success or positive error */
713int
Darrick J. Wong8bfe9d12019-07-03 20:36:26 -0700714xfs_fsbulkstat_one_fmt(
Darrick J. Wong7035f972019-07-03 20:36:26 -0700715 struct xfs_ibulk *breq,
716 const struct xfs_bulkstat *bstat)
Darrick J. Wong2810bd62019-07-02 09:39:40 -0700717{
Darrick J. Wong7035f972019-07-03 20:36:26 -0700718 struct xfs_bstat bs1;
719
720 xfs_bulkstat_to_bstat(breq->mp, &bs1, bstat);
721 if (copy_to_user(breq->ubuffer, &bs1, sizeof(bs1)))
Darrick J. Wong2810bd62019-07-02 09:39:40 -0700722 return -EFAULT;
723 return xfs_ibulk_advance(breq, sizeof(struct xfs_bstat));
724}
725
Darrick J. Wong677717f2019-07-02 09:39:43 -0700726int
Darrick J. Wong8bfe9d12019-07-03 20:36:26 -0700727xfs_fsinumbers_fmt(
Darrick J. Wong5f19c7f2019-07-03 20:36:27 -0700728 struct xfs_ibulk *breq,
729 const struct xfs_inumbers *igrp)
Darrick J. Wong677717f2019-07-02 09:39:43 -0700730{
Darrick J. Wong5f19c7f2019-07-03 20:36:27 -0700731 struct xfs_inogrp ig1;
732
733 xfs_inumbers_to_inogrp(&ig1, igrp);
734 if (copy_to_user(breq->ubuffer, &ig1, sizeof(struct xfs_inogrp)))
Darrick J. Wong677717f2019-07-02 09:39:43 -0700735 return -EFAULT;
736 return xfs_ibulk_advance(breq, sizeof(struct xfs_inogrp));
737}
738
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739STATIC int
Darrick J. Wong8bfe9d12019-07-03 20:36:26 -0700740xfs_ioc_fsbulkstat(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 xfs_mount_t *mp,
742 unsigned int cmd,
743 void __user *arg)
744{
Darrick J. Wong2810bd62019-07-02 09:39:40 -0700745 struct xfs_fsop_bulkreq bulkreq;
746 struct xfs_ibulk breq = {
747 .mp = mp,
748 .ocount = 0,
749 };
750 xfs_ino_t lastino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 int error;
752
753 /* done = 1 if there are more stats to get and if bulkstat */
754 /* should be called again (unused here, but used in dmapi) */
755
756 if (!capable(CAP_SYS_ADMIN))
757 return -EPERM;
758
759 if (XFS_FORCED_SHUTDOWN(mp))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000760 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Darrick J. Wong6f71fb62019-07-03 20:36:25 -0700762 if (copy_from_user(&bulkreq, arg, sizeof(struct xfs_fsop_bulkreq)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000763 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
Darrick J. Wong2810bd62019-07-02 09:39:40 -0700765 if (copy_from_user(&lastino, bulkreq.lastip, sizeof(__s64)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000766 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
Darrick J. Wong2810bd62019-07-02 09:39:40 -0700768 if (bulkreq.icount <= 0)
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000769 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100771 if (bulkreq.ubuffer == NULL)
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000772 return -EINVAL;
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100773
Darrick J. Wong2810bd62019-07-02 09:39:40 -0700774 breq.ubuffer = bulkreq.ubuffer;
775 breq.icount = bulkreq.icount;
776
777 /*
778 * FSBULKSTAT_SINGLE expects that *lastip contains the inode number
779 * that we want to stat. However, FSINUMBERS and FSBULKSTAT expect
780 * that *lastip contains either zero or the number of the last inode to
781 * be examined by the previous call and return results starting with
782 * the next inode after that. The new bulk request back end functions
783 * take the inode to start with, so we have to compute the startino
784 * parameter from lastino to maintain correct function. lastino == 0
785 * is a special case because it has traditionally meant "first inode
786 * in filesystem".
787 */
788 if (cmd == XFS_IOC_FSINUMBERS) {
Darrick J. Wong677717f2019-07-02 09:39:43 -0700789 breq.startino = lastino ? lastino + 1 : 0;
Darrick J. Wong8bfe9d12019-07-03 20:36:26 -0700790 error = xfs_inumbers(&breq, xfs_fsinumbers_fmt);
Darrick J. Wong677717f2019-07-02 09:39:43 -0700791 lastino = breq.startino - 1;
Darrick J. Wong2810bd62019-07-02 09:39:40 -0700792 } else if (cmd == XFS_IOC_FSBULKSTAT_SINGLE) {
793 breq.startino = lastino;
794 breq.icount = 1;
Darrick J. Wong8bfe9d12019-07-03 20:36:26 -0700795 error = xfs_bulkstat_one(&breq, xfs_fsbulkstat_one_fmt);
Darrick J. Wong2810bd62019-07-02 09:39:40 -0700796 } else { /* XFS_IOC_FSBULKSTAT */
797 breq.startino = lastino ? lastino + 1 : 0;
Darrick J. Wong8bfe9d12019-07-03 20:36:26 -0700798 error = xfs_bulkstat(&breq, xfs_fsbulkstat_one_fmt);
Darrick J. Wong2810bd62019-07-02 09:39:40 -0700799 lastino = breq.startino - 1;
800 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
802 if (error)
Dave Chinner24513372014-06-25 14:58:08 +1000803 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Darrick J. Wongf16fe3e2019-07-02 09:39:39 -0700805 if (bulkreq.lastip != NULL &&
Darrick J. Wong2810bd62019-07-02 09:39:40 -0700806 copy_to_user(bulkreq.lastip, &lastino, sizeof(xfs_ino_t)))
Darrick J. Wongf16fe3e2019-07-02 09:39:39 -0700807 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Darrick J. Wongf16fe3e2019-07-02 09:39:39 -0700809 if (bulkreq.ocount != NULL &&
Darrick J. Wong2810bd62019-07-02 09:39:40 -0700810 copy_to_user(bulkreq.ocount, &breq.ocount, sizeof(__s32)))
Darrick J. Wongf16fe3e2019-07-02 09:39:39 -0700811 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
813 return 0;
814}
815
Darrick J. Wong0448b6f2019-07-03 20:36:27 -0700816/* Return 0 on success or positive error */
817static int
818xfs_bulkstat_fmt(
819 struct xfs_ibulk *breq,
820 const struct xfs_bulkstat *bstat)
821{
822 if (copy_to_user(breq->ubuffer, bstat, sizeof(struct xfs_bulkstat)))
823 return -EFAULT;
824 return xfs_ibulk_advance(breq, sizeof(struct xfs_bulkstat));
825}
826
827/*
828 * Check the incoming bulk request @hdr from userspace and initialize the
829 * internal @breq bulk request appropriately. Returns 0 if the bulk request
Darrick J. Wonge7ee96d2019-08-28 14:37:57 -0700830 * should proceed; -ECANCELED if there's nothing to do; or the usual
Darrick J. Wong0448b6f2019-07-03 20:36:27 -0700831 * negative error code.
832 */
833static int
834xfs_bulk_ireq_setup(
835 struct xfs_mount *mp,
836 struct xfs_bulk_ireq *hdr,
837 struct xfs_ibulk *breq,
838 void __user *ubuffer)
839{
840 if (hdr->icount == 0 ||
841 (hdr->flags & ~XFS_BULK_IREQ_FLAGS_ALL) ||
Darrick J. Wong0448b6f2019-07-03 20:36:27 -0700842 memchr_inv(hdr->reserved, 0, sizeof(hdr->reserved)))
843 return -EINVAL;
844
845 breq->startino = hdr->ino;
846 breq->ubuffer = ubuffer;
847 breq->icount = hdr->icount;
848 breq->ocount = 0;
Darrick J. Wong13d59a22019-07-03 20:36:28 -0700849 breq->flags = 0;
850
851 /*
Darrick J. Wongbf3cb392019-07-03 20:36:29 -0700852 * The @ino parameter is a special value, so we must look it up here.
853 * We're not allowed to have IREQ_AGNO, and we only return one inode
854 * worth of data.
855 */
856 if (hdr->flags & XFS_BULK_IREQ_SPECIAL) {
857 if (hdr->flags & XFS_BULK_IREQ_AGNO)
858 return -EINVAL;
859
860 switch (hdr->ino) {
861 case XFS_BULK_IREQ_SPECIAL_ROOT:
862 hdr->ino = mp->m_sb.sb_rootino;
863 break;
864 default:
865 return -EINVAL;
866 }
867 breq->icount = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 }
869
Darrick J. Wongbf3cb392019-07-03 20:36:29 -0700870 /*
Darrick J. Wong13d59a22019-07-03 20:36:28 -0700871 * The IREQ_AGNO flag means that we only want results from a given AG.
872 * If @hdr->ino is zero, we start iterating in that AG. If @hdr->ino is
873 * beyond the specified AG then we return no results.
874 */
875 if (hdr->flags & XFS_BULK_IREQ_AGNO) {
876 if (hdr->agno >= mp->m_sb.sb_agcount)
877 return -EINVAL;
878
879 if (breq->startino == 0)
880 breq->startino = XFS_AGINO_TO_INO(mp, hdr->agno, 0);
881 else if (XFS_INO_TO_AGNO(mp, breq->startino) < hdr->agno)
882 return -EINVAL;
883
884 breq->flags |= XFS_IBULK_SAME_AG;
885
886 /* Asking for an inode past the end of the AG? We're done! */
887 if (XFS_INO_TO_AGNO(mp, breq->startino) > hdr->agno)
Darrick J. Wonge7ee96d2019-08-28 14:37:57 -0700888 return -ECANCELED;
Darrick J. Wong13d59a22019-07-03 20:36:28 -0700889 } else if (hdr->agno)
890 return -EINVAL;
Darrick J. Wong0448b6f2019-07-03 20:36:27 -0700891
892 /* Asking for an inode past the end of the FS? We're done! */
893 if (XFS_INO_TO_AGNO(mp, breq->startino) >= mp->m_sb.sb_agcount)
Darrick J. Wonge7ee96d2019-08-28 14:37:57 -0700894 return -ECANCELED;
Darrick J. Wong0448b6f2019-07-03 20:36:27 -0700895
896 return 0;
897}
898
899/*
900 * Update the userspace bulk request @hdr to reflect the end state of the
901 * internal bulk request @breq.
902 */
903static void
904xfs_bulk_ireq_teardown(
905 struct xfs_bulk_ireq *hdr,
906 struct xfs_ibulk *breq)
907{
908 hdr->ino = breq->startino;
909 hdr->ocount = breq->ocount;
910}
911
912/* Handle the v5 bulkstat ioctl. */
913STATIC int
914xfs_ioc_bulkstat(
915 struct xfs_mount *mp,
916 unsigned int cmd,
917 struct xfs_bulkstat_req __user *arg)
918{
919 struct xfs_bulk_ireq hdr;
920 struct xfs_ibulk breq = {
921 .mp = mp,
922 };
923 int error;
924
925 if (!capable(CAP_SYS_ADMIN))
926 return -EPERM;
927
928 if (XFS_FORCED_SHUTDOWN(mp))
929 return -EIO;
930
931 if (copy_from_user(&hdr, &arg->hdr, sizeof(hdr)))
932 return -EFAULT;
933
934 error = xfs_bulk_ireq_setup(mp, &hdr, &breq, arg->bulkstat);
Darrick J. Wonge7ee96d2019-08-28 14:37:57 -0700935 if (error == -ECANCELED)
Darrick J. Wong0448b6f2019-07-03 20:36:27 -0700936 goto out_teardown;
937 if (error < 0)
938 return error;
939
940 error = xfs_bulkstat(&breq, xfs_bulkstat_fmt);
941 if (error)
942 return error;
943
944out_teardown:
945 xfs_bulk_ireq_teardown(&hdr, &breq);
946 if (copy_to_user(&arg->hdr, &hdr, sizeof(hdr)))
947 return -EFAULT;
948
949 return 0;
950}
951
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952STATIC int
Darrick J. Wongfba97602019-07-03 20:36:28 -0700953xfs_inumbers_fmt(
954 struct xfs_ibulk *breq,
955 const struct xfs_inumbers *igrp)
956{
957 if (copy_to_user(breq->ubuffer, igrp, sizeof(struct xfs_inumbers)))
958 return -EFAULT;
959 return xfs_ibulk_advance(breq, sizeof(struct xfs_inumbers));
960}
961
962/* Handle the v5 inumbers ioctl. */
963STATIC int
964xfs_ioc_inumbers(
965 struct xfs_mount *mp,
966 unsigned int cmd,
967 struct xfs_inumbers_req __user *arg)
968{
969 struct xfs_bulk_ireq hdr;
970 struct xfs_ibulk breq = {
971 .mp = mp,
972 };
973 int error;
974
975 if (!capable(CAP_SYS_ADMIN))
976 return -EPERM;
977
978 if (XFS_FORCED_SHUTDOWN(mp))
979 return -EIO;
980
981 if (copy_from_user(&hdr, &arg->hdr, sizeof(hdr)))
982 return -EFAULT;
983
984 error = xfs_bulk_ireq_setup(mp, &hdr, &breq, arg->inumbers);
Darrick J. Wonge7ee96d2019-08-28 14:37:57 -0700985 if (error == -ECANCELED)
Darrick J. Wongfba97602019-07-03 20:36:28 -0700986 goto out_teardown;
987 if (error < 0)
988 return error;
989
990 error = xfs_inumbers(&breq, xfs_inumbers_fmt);
991 if (error)
992 return error;
993
994out_teardown:
995 xfs_bulk_ireq_teardown(&hdr, &breq);
996 if (copy_to_user(&arg->hdr, &hdr, sizeof(hdr)))
997 return -EFAULT;
998
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 return 0;
1000}
1001
1002STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003xfs_ioc_fsgeometry(
Dave Chinner1b6d9682019-04-12 07:41:16 -07001004 struct xfs_mount *mp,
1005 void __user *arg,
1006 int struct_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007{
Dave Chinner1b6d9682019-04-12 07:41:16 -07001008 struct xfs_fsop_geom fsgeo;
1009 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
Eric Sandeen91083262019-05-01 20:26:30 -07001011 xfs_fs_geometry(&mp->m_sb, &fsgeo, struct_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
Dave Chinner1b6d9682019-04-12 07:41:16 -07001013 if (struct_version <= 3)
1014 len = sizeof(struct xfs_fsop_geom_v1);
1015 else if (struct_version == 4)
1016 len = sizeof(struct xfs_fsop_geom_v4);
Darrick J. Wongc23232d2019-04-12 07:41:17 -07001017 else {
1018 xfs_fsop_geom_health(mp, &fsgeo);
Dave Chinner1b6d9682019-04-12 07:41:16 -07001019 len = sizeof(fsgeo);
Darrick J. Wongc23232d2019-04-12 07:41:17 -07001020 }
Dave Chinner1b6d9682019-04-12 07:41:16 -07001021
1022 if (copy_to_user(arg, &fsgeo, len))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10001023 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 return 0;
1025}
1026
Darrick J. Wong7cd50062019-04-12 07:41:17 -07001027STATIC int
1028xfs_ioc_ag_geometry(
1029 struct xfs_mount *mp,
1030 void __user *arg)
1031{
1032 struct xfs_ag_geometry ageo;
1033 int error;
1034
1035 if (copy_from_user(&ageo, arg, sizeof(ageo)))
1036 return -EFAULT;
Darrick J. Wong76f17932019-08-30 16:30:22 -07001037 if (ageo.ag_flags)
1038 return -EINVAL;
1039 if (memchr_inv(&ageo.ag_reserved, 0, sizeof(ageo.ag_reserved)))
1040 return -EINVAL;
Darrick J. Wong7cd50062019-04-12 07:41:17 -07001041
1042 error = xfs_ag_get_geometry(mp, ageo.ag_number, &ageo);
1043 if (error)
1044 return error;
1045
1046 if (copy_to_user(arg, &ageo, sizeof(ageo)))
1047 return -EFAULT;
1048 return 0;
1049}
1050
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051/*
1052 * Linux extended inode flags interface.
1053 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
1055STATIC unsigned int
1056xfs_merge_ioc_xflags(
1057 unsigned int flags,
1058 unsigned int start)
1059{
1060 unsigned int xflags = start;
1061
Eric Sandeen39058a02007-02-10 18:37:10 +11001062 if (flags & FS_IMMUTABLE_FL)
Dave Chinnere7b89482016-01-04 16:44:15 +11001063 xflags |= FS_XFLAG_IMMUTABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 else
Dave Chinnere7b89482016-01-04 16:44:15 +11001065 xflags &= ~FS_XFLAG_IMMUTABLE;
Eric Sandeen39058a02007-02-10 18:37:10 +11001066 if (flags & FS_APPEND_FL)
Dave Chinnere7b89482016-01-04 16:44:15 +11001067 xflags |= FS_XFLAG_APPEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 else
Dave Chinnere7b89482016-01-04 16:44:15 +11001069 xflags &= ~FS_XFLAG_APPEND;
Eric Sandeen39058a02007-02-10 18:37:10 +11001070 if (flags & FS_SYNC_FL)
Dave Chinnere7b89482016-01-04 16:44:15 +11001071 xflags |= FS_XFLAG_SYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 else
Dave Chinnere7b89482016-01-04 16:44:15 +11001073 xflags &= ~FS_XFLAG_SYNC;
Eric Sandeen39058a02007-02-10 18:37:10 +11001074 if (flags & FS_NOATIME_FL)
Dave Chinnere7b89482016-01-04 16:44:15 +11001075 xflags |= FS_XFLAG_NOATIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 else
Dave Chinnere7b89482016-01-04 16:44:15 +11001077 xflags &= ~FS_XFLAG_NOATIME;
Eric Sandeen39058a02007-02-10 18:37:10 +11001078 if (flags & FS_NODUMP_FL)
Dave Chinnere7b89482016-01-04 16:44:15 +11001079 xflags |= FS_XFLAG_NODUMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 else
Dave Chinnere7b89482016-01-04 16:44:15 +11001081 xflags &= ~FS_XFLAG_NODUMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
1083 return xflags;
1084}
1085
1086STATIC unsigned int
1087xfs_di2lxflags(
Darrick J. Wongc8ce5402017-06-16 11:00:05 -07001088 uint16_t di_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089{
1090 unsigned int flags = 0;
1091
1092 if (di_flags & XFS_DIFLAG_IMMUTABLE)
Eric Sandeen39058a02007-02-10 18:37:10 +11001093 flags |= FS_IMMUTABLE_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 if (di_flags & XFS_DIFLAG_APPEND)
Eric Sandeen39058a02007-02-10 18:37:10 +11001095 flags |= FS_APPEND_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 if (di_flags & XFS_DIFLAG_SYNC)
Eric Sandeen39058a02007-02-10 18:37:10 +11001097 flags |= FS_SYNC_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 if (di_flags & XFS_DIFLAG_NOATIME)
Eric Sandeen39058a02007-02-10 18:37:10 +11001099 flags |= FS_NOATIME_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 if (di_flags & XFS_DIFLAG_NODUMP)
Eric Sandeen39058a02007-02-10 18:37:10 +11001101 flags |= FS_NODUMP_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 return flags;
1103}
1104
Darrick J. Wong7b0e4922019-07-01 08:25:35 -07001105static void
1106xfs_fill_fsxattr(
1107 struct xfs_inode *ip,
1108 bool attr,
1109 struct fsxattr *fa)
1110{
1111 simple_fill_fsxattr(fa, xfs_ip2xflags(ip));
1112 fa->fsx_extsize = ip->i_d.di_extsize << ip->i_mount->m_sb.sb_blocklog;
1113 fa->fsx_cowextsize = ip->i_d.di_cowextsize <<
1114 ip->i_mount->m_sb.sb_blocklog;
Christoph Hellwigde7a8662019-11-12 08:22:54 -08001115 fa->fsx_projid = ip->i_d.di_projid;
Darrick J. Wong7b0e4922019-07-01 08:25:35 -07001116
1117 if (attr) {
1118 if (ip->i_afp) {
1119 if (ip->i_afp->if_flags & XFS_IFEXTENTS)
1120 fa->fsx_nextents = xfs_iext_count(ip->i_afp);
1121 else
1122 fa->fsx_nextents = ip->i_d.di_anextents;
1123 } else
1124 fa->fsx_nextents = 0;
1125 } else {
1126 if (ip->i_df.if_flags & XFS_IFEXTENTS)
1127 fa->fsx_nextents = xfs_iext_count(&ip->i_df);
1128 else
1129 fa->fsx_nextents = ip->i_d.di_nextents;
1130 }
1131}
1132
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133STATIC int
Christoph Hellwigc83bfab2007-10-11 17:47:00 +10001134xfs_ioc_fsgetxattr(
1135 xfs_inode_t *ip,
1136 int attr,
1137 void __user *arg)
1138{
1139 struct fsxattr fa;
1140
1141 xfs_ilock(ip, XFS_ILOCK_SHARED);
Darrick J. Wong7b0e4922019-07-01 08:25:35 -07001142 xfs_fill_fsxattr(ip, attr, &fa);
Christoph Hellwigc83bfab2007-10-11 17:47:00 +10001143 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1144
1145 if (copy_to_user(arg, &fa, sizeof(fa)))
1146 return -EFAULT;
1147 return 0;
1148}
1149
Christoph Hellwigdd606872017-09-02 08:21:20 -07001150STATIC uint16_t
1151xfs_flags2diflags(
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001152 struct xfs_inode *ip,
1153 unsigned int xflags)
1154{
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001155 /* can't set PREALLOC this way, just preserve it */
Christoph Hellwigdd606872017-09-02 08:21:20 -07001156 uint16_t di_flags =
1157 (ip->i_d.di_flags & XFS_DIFLAG_PREALLOC);
1158
Dave Chinnere7b89482016-01-04 16:44:15 +11001159 if (xflags & FS_XFLAG_IMMUTABLE)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001160 di_flags |= XFS_DIFLAG_IMMUTABLE;
Dave Chinnere7b89482016-01-04 16:44:15 +11001161 if (xflags & FS_XFLAG_APPEND)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001162 di_flags |= XFS_DIFLAG_APPEND;
Dave Chinnere7b89482016-01-04 16:44:15 +11001163 if (xflags & FS_XFLAG_SYNC)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001164 di_flags |= XFS_DIFLAG_SYNC;
Dave Chinnere7b89482016-01-04 16:44:15 +11001165 if (xflags & FS_XFLAG_NOATIME)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001166 di_flags |= XFS_DIFLAG_NOATIME;
Dave Chinnere7b89482016-01-04 16:44:15 +11001167 if (xflags & FS_XFLAG_NODUMP)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001168 di_flags |= XFS_DIFLAG_NODUMP;
Dave Chinnere7b89482016-01-04 16:44:15 +11001169 if (xflags & FS_XFLAG_NODEFRAG)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001170 di_flags |= XFS_DIFLAG_NODEFRAG;
Dave Chinnere7b89482016-01-04 16:44:15 +11001171 if (xflags & FS_XFLAG_FILESTREAM)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001172 di_flags |= XFS_DIFLAG_FILESTREAM;
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001173 if (S_ISDIR(VFS_I(ip)->i_mode)) {
Dave Chinnere7b89482016-01-04 16:44:15 +11001174 if (xflags & FS_XFLAG_RTINHERIT)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001175 di_flags |= XFS_DIFLAG_RTINHERIT;
Dave Chinnere7b89482016-01-04 16:44:15 +11001176 if (xflags & FS_XFLAG_NOSYMLINKS)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001177 di_flags |= XFS_DIFLAG_NOSYMLINKS;
Dave Chinnere7b89482016-01-04 16:44:15 +11001178 if (xflags & FS_XFLAG_EXTSZINHERIT)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001179 di_flags |= XFS_DIFLAG_EXTSZINHERIT;
Dave Chinnere7b89482016-01-04 16:44:15 +11001180 if (xflags & FS_XFLAG_PROJINHERIT)
Dave Chinner9336e3a2014-10-02 09:18:40 +10001181 di_flags |= XFS_DIFLAG_PROJINHERIT;
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001182 } else if (S_ISREG(VFS_I(ip)->i_mode)) {
Dave Chinnere7b89482016-01-04 16:44:15 +11001183 if (xflags & FS_XFLAG_REALTIME)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001184 di_flags |= XFS_DIFLAG_REALTIME;
Dave Chinnere7b89482016-01-04 16:44:15 +11001185 if (xflags & FS_XFLAG_EXTSIZE)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001186 di_flags |= XFS_DIFLAG_EXTSIZE;
1187 }
Dave Chinner58f88ca2016-01-04 16:44:15 +11001188
Christoph Hellwigdd606872017-09-02 08:21:20 -07001189 return di_flags;
1190}
Dave Chinner58f88ca2016-01-04 16:44:15 +11001191
Christoph Hellwigdd606872017-09-02 08:21:20 -07001192STATIC uint64_t
1193xfs_flags2diflags2(
1194 struct xfs_inode *ip,
1195 unsigned int xflags)
1196{
1197 uint64_t di_flags2 =
1198 (ip->i_d.di_flags2 & XFS_DIFLAG2_REFLINK);
1199
Dave Chinner58f88ca2016-01-04 16:44:15 +11001200 if (xflags & FS_XFLAG_DAX)
1201 di_flags2 |= XFS_DIFLAG2_DAX;
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001202 if (xflags & FS_XFLAG_COWEXTSIZE)
1203 di_flags2 |= XFS_DIFLAG2_COWEXTSIZE;
Dave Chinner58f88ca2016-01-04 16:44:15 +11001204
Christoph Hellwigdd606872017-09-02 08:21:20 -07001205 return di_flags2;
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001206}
1207
Christoph Hellwigf13fae22008-07-21 16:16:15 +10001208STATIC void
1209xfs_diflags_to_linux(
1210 struct xfs_inode *ip)
1211{
David Chinnere4f75292008-08-13 16:00:45 +10001212 struct inode *inode = VFS_I(ip);
Christoph Hellwigf13fae22008-07-21 16:16:15 +10001213 unsigned int xflags = xfs_ip2xflags(ip);
1214
Dave Chinnere7b89482016-01-04 16:44:15 +11001215 if (xflags & FS_XFLAG_IMMUTABLE)
Christoph Hellwigf13fae22008-07-21 16:16:15 +10001216 inode->i_flags |= S_IMMUTABLE;
1217 else
1218 inode->i_flags &= ~S_IMMUTABLE;
Dave Chinnere7b89482016-01-04 16:44:15 +11001219 if (xflags & FS_XFLAG_APPEND)
Christoph Hellwigf13fae22008-07-21 16:16:15 +10001220 inode->i_flags |= S_APPEND;
1221 else
1222 inode->i_flags &= ~S_APPEND;
Dave Chinnere7b89482016-01-04 16:44:15 +11001223 if (xflags & FS_XFLAG_SYNC)
Christoph Hellwigf13fae22008-07-21 16:16:15 +10001224 inode->i_flags |= S_SYNC;
1225 else
1226 inode->i_flags &= ~S_SYNC;
Dave Chinnere7b89482016-01-04 16:44:15 +11001227 if (xflags & FS_XFLAG_NOATIME)
Christoph Hellwigf13fae22008-07-21 16:16:15 +10001228 inode->i_flags |= S_NOATIME;
1229 else
1230 inode->i_flags &= ~S_NOATIME;
Christoph Hellwig742d8422017-08-30 09:23:01 -07001231#if 0 /* disabled until the flag switching races are sorted out */
Dave Chinner58f88ca2016-01-04 16:44:15 +11001232 if (xflags & FS_XFLAG_DAX)
1233 inode->i_flags |= S_DAX;
1234 else
1235 inode->i_flags &= ~S_DAX;
Christoph Hellwig742d8422017-08-30 09:23:01 -07001236#endif
Christoph Hellwigf13fae22008-07-21 16:16:15 +10001237}
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001238
Dave Chinner29a17c02015-02-02 10:14:25 +11001239static int
1240xfs_ioctl_setattr_xflags(
1241 struct xfs_trans *tp,
1242 struct xfs_inode *ip,
1243 struct fsxattr *fa)
1244{
1245 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwigdd606872017-09-02 08:21:20 -07001246 uint64_t di_flags2;
Dave Chinner29a17c02015-02-02 10:14:25 +11001247
1248 /* Can't change realtime flag if any extents are allocated. */
1249 if ((ip->i_d.di_nextents || ip->i_delayed_blks) &&
Dave Chinnere7b89482016-01-04 16:44:15 +11001250 XFS_IS_REALTIME_INODE(ip) != (fa->fsx_xflags & FS_XFLAG_REALTIME))
Dave Chinner29a17c02015-02-02 10:14:25 +11001251 return -EINVAL;
1252
1253 /* If realtime flag is set then must have realtime device */
Dave Chinnere7b89482016-01-04 16:44:15 +11001254 if (fa->fsx_xflags & FS_XFLAG_REALTIME) {
Dave Chinner29a17c02015-02-02 10:14:25 +11001255 if (mp->m_sb.sb_rblocks == 0 || mp->m_sb.sb_rextsize == 0 ||
1256 (ip->i_d.di_extsize % mp->m_sb.sb_rextsize))
1257 return -EINVAL;
1258 }
1259
Darrick J. Wong1987fd72016-10-10 16:49:29 +11001260 /* Clear reflink if we are actually able to set the rt flag. */
Darrick J. Wongc8e156a2016-10-03 09:11:50 -07001261 if ((fa->fsx_xflags & FS_XFLAG_REALTIME) && xfs_is_reflink_inode(ip))
Darrick J. Wong1987fd72016-10-10 16:49:29 +11001262 ip->i_d.di_flags2 &= ~XFS_DIFLAG2_REFLINK;
Darrick J. Wongc8e156a2016-10-03 09:11:50 -07001263
Darrick J. Wong4f435eb2016-10-03 09:11:50 -07001264 /* Don't allow us to set DAX mode for a reflinked file for now. */
1265 if ((fa->fsx_xflags & FS_XFLAG_DAX) && xfs_is_reflink_inode(ip))
1266 return -EINVAL;
1267
Christoph Hellwigdd606872017-09-02 08:21:20 -07001268 /* diflags2 only valid for v3 inodes. */
1269 di_flags2 = xfs_flags2diflags2(ip, fa->fsx_xflags);
1270 if (di_flags2 && ip->i_d.di_version < 3)
1271 return -EINVAL;
1272
1273 ip->i_d.di_flags = xfs_flags2diflags(ip, fa->fsx_xflags);
1274 ip->i_d.di_flags2 = di_flags2;
1275
Dave Chinner29a17c02015-02-02 10:14:25 +11001276 xfs_diflags_to_linux(ip);
1277 xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
1278 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11001279 XFS_STATS_INC(mp, xs_ig_attrchg);
Dave Chinner29a17c02015-02-02 10:14:25 +11001280 return 0;
1281}
1282
Dave Chinner8f3d17a2015-02-02 10:15:35 +11001283/*
Dave Chinner3a6a8542016-03-01 09:41:33 +11001284 * If we are changing DAX flags, we have to ensure the file is clean and any
1285 * cached objects in the address space are invalidated and removed. This
1286 * requires us to lock out other IO and page faults similar to a truncate
1287 * operation. The locks need to be held until the transaction has been committed
1288 * so that the cache invalidation is atomic with respect to the DAX flag
1289 * manipulation.
1290 */
1291static int
1292xfs_ioctl_setattr_dax_invalidate(
1293 struct xfs_inode *ip,
1294 struct fsxattr *fa,
1295 int *join_flags)
1296{
1297 struct inode *inode = VFS_I(ip);
Ross Zwisler6851a3d2017-09-18 14:46:03 -07001298 struct super_block *sb = inode->i_sb;
Dave Chinner3a6a8542016-03-01 09:41:33 +11001299 int error;
1300
1301 *join_flags = 0;
1302
1303 /*
1304 * It is only valid to set the DAX flag on regular files and
Dave Chinner64485432016-03-01 09:41:33 +11001305 * directories on filesystems where the block size is equal to the page
Darrick J. Wongaaacdd22018-05-31 15:07:47 -07001306 * size. On directories it serves as an inherited hint so we don't
1307 * have to check the device for dax support or flush pagecache.
Dave Chinner3a6a8542016-03-01 09:41:33 +11001308 */
Dave Chinner64485432016-03-01 09:41:33 +11001309 if (fa->fsx_xflags & FS_XFLAG_DAX) {
Christoph Hellwig30fa5292019-10-24 22:25:38 -07001310 struct xfs_buftarg *target = xfs_inode_buftarg(ip);
1311
1312 if (!bdev_dax_supported(target->bt_bdev, sb->s_blocksize))
Dave Chinner64485432016-03-01 09:41:33 +11001313 return -EINVAL;
1314 }
Dave Chinner3a6a8542016-03-01 09:41:33 +11001315
1316 /* If the DAX state is not changing, we have nothing to do here. */
1317 if ((fa->fsx_xflags & FS_XFLAG_DAX) && IS_DAX(inode))
1318 return 0;
1319 if (!(fa->fsx_xflags & FS_XFLAG_DAX) && !IS_DAX(inode))
1320 return 0;
1321
Darrick J. Wongaaacdd22018-05-31 15:07:47 -07001322 if (S_ISDIR(inode->i_mode))
1323 return 0;
1324
Dave Chinner3a6a8542016-03-01 09:41:33 +11001325 /* lock, flush and invalidate mapping in preparation for flag change */
1326 xfs_ilock(ip, XFS_MMAPLOCK_EXCL | XFS_IOLOCK_EXCL);
1327 error = filemap_write_and_wait(inode->i_mapping);
1328 if (error)
1329 goto out_unlock;
1330 error = invalidate_inode_pages2(inode->i_mapping);
1331 if (error)
1332 goto out_unlock;
1333
1334 *join_flags = XFS_MMAPLOCK_EXCL | XFS_IOLOCK_EXCL;
1335 return 0;
1336
1337out_unlock:
1338 xfs_iunlock(ip, XFS_MMAPLOCK_EXCL | XFS_IOLOCK_EXCL);
1339 return error;
1340
1341}
1342
1343/*
Dave Chinner8f3d17a2015-02-02 10:15:35 +11001344 * Set up the transaction structure for the setattr operation, checking that we
1345 * have permission to do so. On success, return a clean transaction and the
1346 * inode locked exclusively ready for further operation specific checks. On
1347 * failure, return an error without modifying or locking the inode.
Dave Chinner3a6a8542016-03-01 09:41:33 +11001348 *
1349 * The inode might already be IO locked on call. If this is the case, it is
1350 * indicated in @join_flags and we take full responsibility for ensuring they
1351 * are unlocked from now on. Hence if we have an error here, we still have to
1352 * unlock them. Otherwise, once they are joined to the transaction, they will
1353 * be unlocked on commit/cancel.
Dave Chinner8f3d17a2015-02-02 10:15:35 +11001354 */
1355static struct xfs_trans *
1356xfs_ioctl_setattr_get_trans(
Dave Chinner3a6a8542016-03-01 09:41:33 +11001357 struct xfs_inode *ip,
1358 int join_flags)
Dave Chinner8f3d17a2015-02-02 10:15:35 +11001359{
1360 struct xfs_mount *mp = ip->i_mount;
1361 struct xfs_trans *tp;
Dave Chinner3a6a8542016-03-01 09:41:33 +11001362 int error = -EROFS;
Dave Chinner8f3d17a2015-02-02 10:15:35 +11001363
1364 if (mp->m_flags & XFS_MOUNT_RDONLY)
Dave Chinner3a6a8542016-03-01 09:41:33 +11001365 goto out_unlock;
1366 error = -EIO;
Dave Chinner8f3d17a2015-02-02 10:15:35 +11001367 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner3a6a8542016-03-01 09:41:33 +11001368 goto out_unlock;
Dave Chinner8f3d17a2015-02-02 10:15:35 +11001369
Christoph Hellwig253f4912016-04-06 09:19:55 +10001370 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
Dave Chinner8f3d17a2015-02-02 10:15:35 +11001371 if (error)
Darrick J. Wong3de5eab2019-04-22 16:28:34 -07001372 goto out_unlock;
Dave Chinner8f3d17a2015-02-02 10:15:35 +11001373
1374 xfs_ilock(ip, XFS_ILOCK_EXCL);
Dave Chinner3a6a8542016-03-01 09:41:33 +11001375 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | join_flags);
1376 join_flags = 0;
Dave Chinner8f3d17a2015-02-02 10:15:35 +11001377
1378 /*
1379 * CAP_FOWNER overrides the following restrictions:
1380 *
1381 * The user ID of the calling process must be equal to the file owner
1382 * ID, except in cases where the CAP_FSETID capability is applicable.
1383 */
1384 if (!inode_owner_or_capable(VFS_I(ip))) {
1385 error = -EPERM;
1386 goto out_cancel;
1387 }
1388
1389 if (mp->m_flags & XFS_MOUNT_WSYNC)
1390 xfs_trans_set_sync(tp);
1391
1392 return tp;
1393
1394out_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +10001395 xfs_trans_cancel(tp);
Dave Chinner3a6a8542016-03-01 09:41:33 +11001396out_unlock:
1397 if (join_flags)
1398 xfs_iunlock(ip, join_flags);
Dave Chinner8f3d17a2015-02-02 10:15:35 +11001399 return ERR_PTR(error);
1400}
1401
Iustin Pop9b94fcc2015-02-02 10:26:26 +11001402/*
1403 * extent size hint validation is somewhat cumbersome. Rules are:
1404 *
1405 * 1. extent size hint is only valid for directories and regular files
Dave Chinnere7b89482016-01-04 16:44:15 +11001406 * 2. FS_XFLAG_EXTSIZE is only valid for regular files
1407 * 3. FS_XFLAG_EXTSZINHERIT is only valid for directories.
Iustin Pop9b94fcc2015-02-02 10:26:26 +11001408 * 4. can only be changed on regular files if no extents are allocated
1409 * 5. can be changed on directories at any time
1410 * 6. extsize hint of 0 turns off hints, clears inode flags.
1411 * 7. Extent size must be a multiple of the appropriate block size.
1412 * 8. for non-realtime files, the extent size hint must be limited
1413 * to half the AG size to avoid alignment extending the extent beyond the
1414 * limits of the AG.
Darrick J. Wong80e4e122017-10-17 21:37:42 -07001415 *
1416 * Please keep this function in sync with xfs_scrub_inode_extsize.
Iustin Pop9b94fcc2015-02-02 10:26:26 +11001417 */
kbuild test robotf92090e2015-02-05 11:13:21 +11001418static int
Dave Chinnerd4388d3c2015-02-02 10:22:20 +11001419xfs_ioctl_setattr_check_extsize(
1420 struct xfs_inode *ip,
1421 struct fsxattr *fa)
1422{
1423 struct xfs_mount *mp = ip->i_mount;
Darrick J. Wongca29be72019-07-01 08:25:36 -07001424 xfs_extlen_t size;
1425 xfs_fsblock_t extsize_fsb;
Iustin Pop9b94fcc2015-02-02 10:26:26 +11001426
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001427 if (S_ISREG(VFS_I(ip)->i_mode) && ip->i_d.di_nextents &&
Dave Chinnerd4388d3c2015-02-02 10:22:20 +11001428 ((ip->i_d.di_extsize << mp->m_sb.sb_blocklog) != fa->fsx_extsize))
1429 return -EINVAL;
1430
Darrick J. Wongca29be72019-07-01 08:25:36 -07001431 if (fa->fsx_extsize == 0)
1432 return 0;
Dave Chinnerd4388d3c2015-02-02 10:22:20 +11001433
Darrick J. Wongca29be72019-07-01 08:25:36 -07001434 extsize_fsb = XFS_B_TO_FSB(mp, fa->fsx_extsize);
1435 if (extsize_fsb > MAXEXTLEN)
1436 return -EINVAL;
1437
1438 if (XFS_IS_REALTIME_INODE(ip) ||
1439 (fa->fsx_xflags & FS_XFLAG_REALTIME)) {
1440 size = mp->m_sb.sb_rextsize << mp->m_sb.sb_blocklog;
1441 } else {
1442 size = mp->m_sb.sb_blocksize;
1443 if (extsize_fsb > mp->m_sb.sb_agblocks / 2)
Dave Chinnerd4388d3c2015-02-02 10:22:20 +11001444 return -EINVAL;
Darrick J. Wongca29be72019-07-01 08:25:36 -07001445 }
Dave Chinnerd4388d3c2015-02-02 10:22:20 +11001446
Darrick J. Wongca29be72019-07-01 08:25:36 -07001447 if (fa->fsx_extsize % size)
1448 return -EINVAL;
Iustin Pop9b94fcc2015-02-02 10:26:26 +11001449
Dave Chinnerd4388d3c2015-02-02 10:22:20 +11001450 return 0;
1451}
1452
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001453/*
1454 * CoW extent size hint validation rules are:
1455 *
1456 * 1. CoW extent size hint can only be set if reflink is enabled on the fs.
1457 * The inode does not have to have any shared blocks, but it must be a v3.
1458 * 2. FS_XFLAG_COWEXTSIZE is only valid for directories and regular files;
1459 * for a directory, the hint is propagated to new files.
1460 * 3. Can be changed on files & directories at any time.
1461 * 4. CoW extsize hint of 0 turns off hints, clears inode flags.
1462 * 5. Extent size must be a multiple of the appropriate block size.
1463 * 6. The extent size hint must be limited to half the AG size to avoid
1464 * alignment extending the extent beyond the limits of the AG.
Darrick J. Wong80e4e122017-10-17 21:37:42 -07001465 *
1466 * Please keep this function in sync with xfs_scrub_inode_cowextsize.
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001467 */
1468static int
1469xfs_ioctl_setattr_check_cowextsize(
1470 struct xfs_inode *ip,
1471 struct fsxattr *fa)
1472{
1473 struct xfs_mount *mp = ip->i_mount;
Darrick J. Wongca29be72019-07-01 08:25:36 -07001474 xfs_extlen_t size;
1475 xfs_fsblock_t cowextsize_fsb;
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001476
1477 if (!(fa->fsx_xflags & FS_XFLAG_COWEXTSIZE))
1478 return 0;
1479
1480 if (!xfs_sb_version_hasreflink(&ip->i_mount->m_sb) ||
1481 ip->i_d.di_version != 3)
1482 return -EINVAL;
1483
Darrick J. Wongca29be72019-07-01 08:25:36 -07001484 if (fa->fsx_cowextsize == 0)
1485 return 0;
1486
1487 cowextsize_fsb = XFS_B_TO_FSB(mp, fa->fsx_cowextsize);
1488 if (cowextsize_fsb > MAXEXTLEN)
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001489 return -EINVAL;
1490
Darrick J. Wongca29be72019-07-01 08:25:36 -07001491 size = mp->m_sb.sb_blocksize;
1492 if (cowextsize_fsb > mp->m_sb.sb_agblocks / 2)
1493 return -EINVAL;
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001494
Darrick J. Wongca29be72019-07-01 08:25:36 -07001495 if (fa->fsx_cowextsize % size)
1496 return -EINVAL;
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001497
1498 return 0;
1499}
1500
kbuild test robotf92090e2015-02-05 11:13:21 +11001501static int
Dave Chinner23bd0732015-02-02 10:22:53 +11001502xfs_ioctl_setattr_check_projid(
1503 struct xfs_inode *ip,
1504 struct fsxattr *fa)
1505{
1506 /* Disallow 32bit project ids if projid32bit feature is not enabled. */
Darrick J. Wongc8ce5402017-06-16 11:00:05 -07001507 if (fa->fsx_projid > (uint16_t)-1 &&
Dave Chinner23bd0732015-02-02 10:22:53 +11001508 !xfs_sb_version_hasprojid32bit(&ip->i_mount->m_sb))
1509 return -EINVAL;
Dave Chinner23bd0732015-02-02 10:22:53 +11001510 return 0;
1511}
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001512
1513STATIC int
1514xfs_ioctl_setattr(
1515 xfs_inode_t *ip,
Dave Chinnerfd179b92015-02-02 10:16:25 +11001516 struct fsxattr *fa)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001517{
Darrick J. Wong7b0e4922019-07-01 08:25:35 -07001518 struct fsxattr old_fa;
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001519 struct xfs_mount *mp = ip->i_mount;
1520 struct xfs_trans *tp;
Christoph Hellwig7d095252009-06-08 15:33:32 +02001521 struct xfs_dquot *udqp = NULL;
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001522 struct xfs_dquot *pdqp = NULL;
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001523 struct xfs_dquot *olddquot = NULL;
1524 int code;
Dave Chinner3a6a8542016-03-01 09:41:33 +11001525 int join_flags = 0;
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001526
Christoph Hellwigcca28fb2010-06-24 11:57:09 +10001527 trace_xfs_ioctl_setattr(ip);
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001528
Dave Chinner23bd0732015-02-02 10:22:53 +11001529 code = xfs_ioctl_setattr_check_projid(ip, fa);
1530 if (code)
1531 return code;
Arkadiusz Mi?kiewicz23963e542010-08-26 10:19:43 +00001532
1533 /*
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001534 * If disk quotas is on, we make sure that the dquots do exist on disk,
1535 * before we start any other transactions. Trying to do this later
1536 * is messy. We don't care to take a readlock to look at the ids
1537 * in inode here, because we can't hold it across the trans_reserve.
1538 * If the IDs do change before we take the ilock, we're covered
1539 * because the i_*dquot fields will get updated anyway.
1540 */
Dave Chinnerfd179b92015-02-02 10:16:25 +11001541 if (XFS_IS_QUOTA_ON(mp)) {
Christoph Hellwig54295152020-02-21 08:31:27 -08001542 code = xfs_qm_vop_dqalloc(ip, VFS_I(ip)->i_uid,
1543 VFS_I(ip)->i_gid, fa->fsx_projid,
1544 XFS_QMOPT_PQUOTA, &udqp, NULL, &pdqp);
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001545 if (code)
1546 return code;
1547 }
1548
Dave Chinner3a6a8542016-03-01 09:41:33 +11001549 /*
1550 * Changing DAX config may require inode locking for mapping
1551 * invalidation. These need to be held all the way to transaction commit
1552 * or cancel time, so need to be passed through to
1553 * xfs_ioctl_setattr_get_trans() so it can apply them to the join call
1554 * appropriately.
1555 */
1556 code = xfs_ioctl_setattr_dax_invalidate(ip, fa, &join_flags);
1557 if (code)
1558 goto error_free_dquots;
1559
1560 tp = xfs_ioctl_setattr_get_trans(ip, join_flags);
Dave Chinner8f3d17a2015-02-02 10:15:35 +11001561 if (IS_ERR(tp)) {
1562 code = PTR_ERR(tp);
1563 goto error_free_dquots;
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001564 }
1565
Dave Chinnerfd179b92015-02-02 10:16:25 +11001566 if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_PQUOTA_ON(mp) &&
Christoph Hellwigde7a8662019-11-12 08:22:54 -08001567 ip->i_d.di_projid != fa->fsx_projid) {
Dave Chinnerfd179b92015-02-02 10:16:25 +11001568 code = xfs_qm_vop_chown_reserve(tp, ip, udqp, NULL, pdqp,
1569 capable(CAP_FOWNER) ? XFS_QMOPT_FORCE_RES : 0);
1570 if (code) /* out of quota */
Dave Chinnerd4388d3c2015-02-02 10:22:20 +11001571 goto error_trans_cancel;
Dave Chinnerfd179b92015-02-02 10:16:25 +11001572 }
1573
Darrick J. Wong7b0e4922019-07-01 08:25:35 -07001574 xfs_fill_fsxattr(ip, false, &old_fa);
1575 code = vfs_ioc_fssetxattr_check(VFS_I(ip), &old_fa, fa);
1576 if (code)
1577 goto error_trans_cancel;
1578
Dave Chinnerd4388d3c2015-02-02 10:22:20 +11001579 code = xfs_ioctl_setattr_check_extsize(ip, fa);
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001580 if (code)
Dave Chinnerd4388d3c2015-02-02 10:22:20 +11001581 goto error_trans_cancel;
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001582
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001583 code = xfs_ioctl_setattr_check_cowextsize(ip, fa);
1584 if (code)
1585 goto error_trans_cancel;
1586
Dave Chinner29a17c02015-02-02 10:14:25 +11001587 code = xfs_ioctl_setattr_xflags(tp, ip, fa);
1588 if (code)
Dave Chinnerd4388d3c2015-02-02 10:22:20 +11001589 goto error_trans_cancel;
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001590
1591 /*
Dave Chinnerfd179b92015-02-02 10:16:25 +11001592 * Change file ownership. Must be the owner or privileged. CAP_FSETID
1593 * overrides the following restrictions:
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001594 *
Dave Chinnerfd179b92015-02-02 10:16:25 +11001595 * The set-user-ID and set-group-ID bits of a file will be cleared upon
1596 * successful return from chown()
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001597 */
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001598
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001599 if ((VFS_I(ip)->i_mode & (S_ISUID|S_ISGID)) &&
Dave Chinnerfd179b92015-02-02 10:16:25 +11001600 !capable_wrt_inode_uidgid(VFS_I(ip), CAP_FSETID))
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001601 VFS_I(ip)->i_mode &= ~(S_ISUID|S_ISGID);
Dave Chinnerfd179b92015-02-02 10:16:25 +11001602
1603 /* Change the ownerships and register project quota modifications */
Christoph Hellwigde7a8662019-11-12 08:22:54 -08001604 if (ip->i_d.di_projid != fa->fsx_projid) {
Dave Chinnerfd179b92015-02-02 10:16:25 +11001605 if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_PQUOTA_ON(mp)) {
1606 olddquot = xfs_qm_vop_chown(tp, ip,
1607 &ip->i_pdquot, pdqp);
Dwight Engenfd5e2aa2013-08-15 14:08:00 -04001608 }
Dave Chinnerfd179b92015-02-02 10:16:25 +11001609 ASSERT(ip->i_d.di_version > 1);
Christoph Hellwigde7a8662019-11-12 08:22:54 -08001610 ip->i_d.di_projid = fa->fsx_projid;
Christoph Hellwigf13fae22008-07-21 16:16:15 +10001611 }
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001612
Dave Chinnera8727032014-10-02 09:20:30 +10001613 /*
1614 * Only set the extent size hint if we've already determined that the
1615 * extent size hint should be set on the inode. If no extent size flags
1616 * are set on the inode then unconditionally clear the extent size hint.
1617 */
Dave Chinnerfd179b92015-02-02 10:16:25 +11001618 if (ip->i_d.di_flags & (XFS_DIFLAG_EXTSIZE | XFS_DIFLAG_EXTSZINHERIT))
1619 ip->i_d.di_extsize = fa->fsx_extsize >> mp->m_sb.sb_blocklog;
1620 else
1621 ip->i_d.di_extsize = 0;
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001622 if (ip->i_d.di_version == 3 &&
1623 (ip->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE))
1624 ip->i_d.di_cowextsize = fa->fsx_cowextsize >>
1625 mp->m_sb.sb_blocklog;
1626 else
1627 ip->i_d.di_cowextsize = 0;
Dave Chinnera8727032014-10-02 09:20:30 +10001628
Christoph Hellwig70393312015-06-04 13:48:08 +10001629 code = xfs_trans_commit(tp);
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001630
1631 /*
1632 * Release any dquot(s) the inode had kept before chown.
1633 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02001634 xfs_qm_dqrele(olddquot);
1635 xfs_qm_dqrele(udqp);
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001636 xfs_qm_dqrele(pdqp);
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001637
Christoph Hellwig288699f2010-06-23 18:11:15 +10001638 return code;
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001639
Dave Chinnerd4388d3c2015-02-02 10:22:20 +11001640error_trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +10001641 xfs_trans_cancel(tp);
Dave Chinner8f3d17a2015-02-02 10:15:35 +11001642error_free_dquots:
Christoph Hellwig7d095252009-06-08 15:33:32 +02001643 xfs_qm_dqrele(udqp);
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001644 xfs_qm_dqrele(pdqp);
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001645 return code;
1646}
1647
Christoph Hellwigc83bfab2007-10-11 17:47:00 +10001648STATIC int
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001649xfs_ioc_fssetxattr(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 xfs_inode_t *ip,
1651 struct file *filp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 void __user *arg)
1653{
1654 struct fsxattr fa;
Jan Karad9457dc2012-06-12 16:20:39 +02001655 int error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001656
1657 if (copy_from_user(&fa, arg, sizeof(fa)))
1658 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
Jan Karad9457dc2012-06-12 16:20:39 +02001660 error = mnt_want_write_file(filp);
1661 if (error)
1662 return error;
Dave Chinnerfd179b92015-02-02 10:16:25 +11001663 error = xfs_ioctl_setattr(ip, &fa);
Jan Karad9457dc2012-06-12 16:20:39 +02001664 mnt_drop_write_file(filp);
Dave Chinner24513372014-06-25 14:58:08 +10001665 return error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001666}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001668STATIC int
1669xfs_ioc_getxflags(
1670 xfs_inode_t *ip,
1671 void __user *arg)
1672{
1673 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001675 flags = xfs_di2lxflags(ip->i_d.di_flags);
1676 if (copy_to_user(arg, &flags, sizeof(flags)))
1677 return -EFAULT;
1678 return 0;
1679}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001681STATIC int
1682xfs_ioc_setxflags(
Dave Chinnerf96291f2015-02-02 10:15:56 +11001683 struct xfs_inode *ip,
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001684 struct file *filp,
1685 void __user *arg)
1686{
Dave Chinnerf96291f2015-02-02 10:15:56 +11001687 struct xfs_trans *tp;
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001688 struct fsxattr fa;
Darrick J. Wong7b0e4922019-07-01 08:25:35 -07001689 struct fsxattr old_fa;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001690 unsigned int flags;
Dave Chinner3a6a8542016-03-01 09:41:33 +11001691 int join_flags = 0;
Dave Chinnerf96291f2015-02-02 10:15:56 +11001692 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001694 if (copy_from_user(&flags, arg, sizeof(flags)))
1695 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001697 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
1698 FS_NOATIME_FL | FS_NODUMP_FL | \
1699 FS_SYNC_FL))
1700 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001702 fa.fsx_xflags = xfs_merge_ioc_xflags(flags, xfs_ip2xflags(ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
Jan Karad9457dc2012-06-12 16:20:39 +02001704 error = mnt_want_write_file(filp);
1705 if (error)
1706 return error;
Dave Chinnerf96291f2015-02-02 10:15:56 +11001707
Dave Chinner3a6a8542016-03-01 09:41:33 +11001708 /*
1709 * Changing DAX config may require inode locking for mapping
1710 * invalidation. These need to be held all the way to transaction commit
1711 * or cancel time, so need to be passed through to
1712 * xfs_ioctl_setattr_get_trans() so it can apply them to the join call
1713 * appropriately.
1714 */
1715 error = xfs_ioctl_setattr_dax_invalidate(ip, &fa, &join_flags);
1716 if (error)
1717 goto out_drop_write;
1718
1719 tp = xfs_ioctl_setattr_get_trans(ip, join_flags);
Dave Chinnerf96291f2015-02-02 10:15:56 +11001720 if (IS_ERR(tp)) {
1721 error = PTR_ERR(tp);
1722 goto out_drop_write;
1723 }
1724
Darrick J. Wong7b0e4922019-07-01 08:25:35 -07001725 xfs_fill_fsxattr(ip, false, &old_fa);
1726 error = vfs_ioc_fssetxattr_check(VFS_I(ip), &old_fa, &fa);
1727 if (error) {
1728 xfs_trans_cancel(tp);
1729 goto out_drop_write;
1730 }
1731
Dave Chinnerf96291f2015-02-02 10:15:56 +11001732 error = xfs_ioctl_setattr_xflags(tp, ip, &fa);
1733 if (error) {
Christoph Hellwig4906e212015-06-04 13:47:56 +10001734 xfs_trans_cancel(tp);
Dave Chinnerf96291f2015-02-02 10:15:56 +11001735 goto out_drop_write;
1736 }
1737
Christoph Hellwig70393312015-06-04 13:48:08 +10001738 error = xfs_trans_commit(tp);
Dave Chinnerf96291f2015-02-02 10:15:56 +11001739out_drop_write:
Jan Karad9457dc2012-06-12 16:20:39 +02001740 mnt_drop_write_file(filp);
Dave Chinner24513372014-06-25 14:58:08 +10001741 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742}
1743
Christoph Hellwig232b51942017-10-17 14:16:19 -07001744static bool
1745xfs_getbmap_format(
1746 struct kgetbmap *p,
1747 struct getbmapx __user *u,
1748 size_t recsize)
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001749{
Christoph Hellwig232b51942017-10-17 14:16:19 -07001750 if (put_user(p->bmv_offset, &u->bmv_offset) ||
1751 put_user(p->bmv_block, &u->bmv_block) ||
1752 put_user(p->bmv_length, &u->bmv_length) ||
1753 put_user(0, &u->bmv_count) ||
1754 put_user(0, &u->bmv_entries))
1755 return false;
1756 if (recsize < sizeof(struct getbmapx))
1757 return true;
1758 if (put_user(0, &u->bmv_iflags) ||
1759 put_user(p->bmv_oflags, &u->bmv_oflags) ||
1760 put_user(0, &u->bmv_unused1) ||
1761 put_user(0, &u->bmv_unused2))
1762 return false;
1763 return true;
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001764}
1765
1766STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767xfs_ioc_getbmap(
Christoph Hellwig8f3e2052016-07-20 11:29:35 +10001768 struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 unsigned int cmd,
1770 void __user *arg)
1771{
Darrick J. Wongbe6324c2017-04-03 15:17:57 -07001772 struct getbmapx bmx = { 0 };
Christoph Hellwig232b51942017-10-17 14:16:19 -07001773 struct kgetbmap *buf;
1774 size_t recsize;
1775 int error, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776
Christoph Hellwig232b51942017-10-17 14:16:19 -07001777 switch (cmd) {
1778 case XFS_IOC_GETBMAPA:
1779 bmx.bmv_iflags = BMV_IF_ATTRFORK;
1780 /*FALLTHRU*/
1781 case XFS_IOC_GETBMAP:
1782 if (file->f_mode & FMODE_NOCMTIME)
1783 bmx.bmv_iflags |= BMV_IF_NO_DMAPI_READ;
1784 /* struct getbmap is a strict subset of struct getbmapx. */
1785 recsize = sizeof(struct getbmap);
1786 break;
1787 case XFS_IOC_GETBMAPX:
1788 recsize = sizeof(struct getbmapx);
1789 break;
1790 default:
1791 return -EINVAL;
1792 }
1793
1794 if (copy_from_user(&bmx, arg, recsize))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10001795 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001797 if (bmx.bmv_count < 2)
Eric Sandeenb474c7a2014-06-22 15:04:54 +10001798 return -EINVAL;
Christoph Hellwig232b51942017-10-17 14:16:19 -07001799 if (bmx.bmv_count > ULONG_MAX / recsize)
1800 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
Christoph Hellwig232b51942017-10-17 14:16:19 -07001802 buf = kmem_zalloc_large(bmx.bmv_count * sizeof(*buf), 0);
1803 if (!buf)
1804 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
Christoph Hellwig232b51942017-10-17 14:16:19 -07001806 error = xfs_getbmap(XFS_I(file_inode(file)), &bmx, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 if (error)
Christoph Hellwig232b51942017-10-17 14:16:19 -07001808 goto out_free_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
Christoph Hellwig232b51942017-10-17 14:16:19 -07001810 error = -EFAULT;
1811 if (copy_to_user(arg, &bmx, recsize))
1812 goto out_free_buf;
1813 arg += recsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
Christoph Hellwig232b51942017-10-17 14:16:19 -07001815 for (i = 0; i < bmx.bmv_entries; i++) {
1816 if (!xfs_getbmap_format(buf + i, arg, recsize))
1817 goto out_free_buf;
1818 arg += recsize;
1819 }
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001820
Christoph Hellwig232b51942017-10-17 14:16:19 -07001821 error = 0;
1822out_free_buf:
1823 kmem_free(buf);
Christophe JAILLET132bf672018-11-06 07:50:50 -08001824 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825}
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001826
Darrick J. Wonge89c0412017-03-28 14:56:37 -07001827struct getfsmap_info {
1828 struct xfs_mount *mp;
Christoph Hellwig9d17e142017-04-21 11:24:41 -07001829 struct fsmap_head __user *data;
1830 unsigned int idx;
Darrick J. Wonge89c0412017-03-28 14:56:37 -07001831 __u32 last_flags;
1832};
1833
1834STATIC int
1835xfs_getfsmap_format(struct xfs_fsmap *xfm, void *priv)
1836{
1837 struct getfsmap_info *info = priv;
1838 struct fsmap fm;
1839
1840 trace_xfs_getfsmap_mapping(info->mp, xfm);
1841
1842 info->last_flags = xfm->fmr_flags;
1843 xfs_fsmap_from_internal(&fm, xfm);
Christoph Hellwig9d17e142017-04-21 11:24:41 -07001844 if (copy_to_user(&info->data->fmh_recs[info->idx++], &fm,
1845 sizeof(struct fsmap)))
Darrick J. Wonge89c0412017-03-28 14:56:37 -07001846 return -EFAULT;
1847
Darrick J. Wonge89c0412017-03-28 14:56:37 -07001848 return 0;
1849}
1850
1851STATIC int
1852xfs_ioc_getfsmap(
1853 struct xfs_inode *ip,
Christoph Hellwig9d17e142017-04-21 11:24:41 -07001854 struct fsmap_head __user *arg)
Darrick J. Wonge89c0412017-03-28 14:56:37 -07001855{
Christoph Hellwigef2b67e2017-04-21 11:24:40 -07001856 struct getfsmap_info info = { NULL };
Darrick J. Wonge89c0412017-03-28 14:56:37 -07001857 struct xfs_fsmap_head xhead = {0};
1858 struct fsmap_head head;
1859 bool aborted = false;
1860 int error;
1861
1862 if (copy_from_user(&head, arg, sizeof(struct fsmap_head)))
1863 return -EFAULT;
1864 if (memchr_inv(head.fmh_reserved, 0, sizeof(head.fmh_reserved)) ||
1865 memchr_inv(head.fmh_keys[0].fmr_reserved, 0,
1866 sizeof(head.fmh_keys[0].fmr_reserved)) ||
1867 memchr_inv(head.fmh_keys[1].fmr_reserved, 0,
1868 sizeof(head.fmh_keys[1].fmr_reserved)))
1869 return -EINVAL;
1870
1871 xhead.fmh_iflags = head.fmh_iflags;
1872 xhead.fmh_count = head.fmh_count;
1873 xfs_fsmap_to_internal(&xhead.fmh_keys[0], &head.fmh_keys[0]);
1874 xfs_fsmap_to_internal(&xhead.fmh_keys[1], &head.fmh_keys[1]);
1875
1876 trace_xfs_getfsmap_low_key(ip->i_mount, &xhead.fmh_keys[0]);
1877 trace_xfs_getfsmap_high_key(ip->i_mount, &xhead.fmh_keys[1]);
1878
1879 info.mp = ip->i_mount;
Christoph Hellwig9d17e142017-04-21 11:24:41 -07001880 info.data = arg;
Darrick J. Wonge89c0412017-03-28 14:56:37 -07001881 error = xfs_getfsmap(ip->i_mount, &xhead, xfs_getfsmap_format, &info);
Darrick J. Wonge7ee96d2019-08-28 14:37:57 -07001882 if (error == -ECANCELED) {
Darrick J. Wonge89c0412017-03-28 14:56:37 -07001883 error = 0;
1884 aborted = true;
1885 } else if (error)
1886 return error;
1887
1888 /* If we didn't abort, set the "last" flag in the last fmx */
Darrick J. Wong12e4a382017-04-23 10:45:21 -07001889 if (!aborted && info.idx) {
Darrick J. Wonge89c0412017-03-28 14:56:37 -07001890 info.last_flags |= FMR_OF_LAST;
Christoph Hellwig9d17e142017-04-21 11:24:41 -07001891 if (copy_to_user(&info.data->fmh_recs[info.idx - 1].fmr_flags,
1892 &info.last_flags, sizeof(info.last_flags)))
Darrick J. Wonge89c0412017-03-28 14:56:37 -07001893 return -EFAULT;
1894 }
1895
1896 /* copy back header */
1897 head.fmh_entries = xhead.fmh_entries;
1898 head.fmh_oflags = xhead.fmh_oflags;
1899 if (copy_to_user(arg, &head, sizeof(struct fsmap_head)))
1900 return -EFAULT;
1901
1902 return 0;
1903}
1904
Darrick J. Wong36fd6e82017-10-17 21:37:34 -07001905STATIC int
1906xfs_ioc_scrub_metadata(
1907 struct xfs_inode *ip,
1908 void __user *arg)
1909{
1910 struct xfs_scrub_metadata scrub;
1911 int error;
1912
1913 if (!capable(CAP_SYS_ADMIN))
1914 return -EPERM;
1915
1916 if (copy_from_user(&scrub, arg, sizeof(scrub)))
1917 return -EFAULT;
1918
1919 error = xfs_scrub_metadata(ip, &scrub);
1920 if (error)
1921 return error;
1922
1923 if (copy_to_user(arg, &scrub, sizeof(scrub)))
1924 return -EFAULT;
1925
1926 return 0;
1927}
1928
Dave Chinnera133d952013-08-12 20:49:48 +10001929int
1930xfs_ioc_swapext(
1931 xfs_swapext_t *sxp)
1932{
1933 xfs_inode_t *ip, *tip;
1934 struct fd f, tmp;
1935 int error = 0;
1936
1937 /* Pull information for the target fd */
1938 f = fdget((int)sxp->sx_fdtarget);
1939 if (!f.file) {
Dave Chinner24513372014-06-25 14:58:08 +10001940 error = -EINVAL;
Dave Chinnera133d952013-08-12 20:49:48 +10001941 goto out;
1942 }
1943
1944 if (!(f.file->f_mode & FMODE_WRITE) ||
1945 !(f.file->f_mode & FMODE_READ) ||
1946 (f.file->f_flags & O_APPEND)) {
Dave Chinner24513372014-06-25 14:58:08 +10001947 error = -EBADF;
Dave Chinnera133d952013-08-12 20:49:48 +10001948 goto out_put_file;
1949 }
1950
1951 tmp = fdget((int)sxp->sx_fdtmp);
1952 if (!tmp.file) {
Dave Chinner24513372014-06-25 14:58:08 +10001953 error = -EINVAL;
Dave Chinnera133d952013-08-12 20:49:48 +10001954 goto out_put_file;
1955 }
1956
1957 if (!(tmp.file->f_mode & FMODE_WRITE) ||
1958 !(tmp.file->f_mode & FMODE_READ) ||
1959 (tmp.file->f_flags & O_APPEND)) {
Dave Chinner24513372014-06-25 14:58:08 +10001960 error = -EBADF;
Dave Chinnera133d952013-08-12 20:49:48 +10001961 goto out_put_tmp_file;
1962 }
1963
1964 if (IS_SWAPFILE(file_inode(f.file)) ||
1965 IS_SWAPFILE(file_inode(tmp.file))) {
Dave Chinner24513372014-06-25 14:58:08 +10001966 error = -EINVAL;
Dave Chinnera133d952013-08-12 20:49:48 +10001967 goto out_put_tmp_file;
1968 }
1969
Jann Horn7f1b6242016-07-20 10:30:30 +10001970 /*
1971 * We need to ensure that the fds passed in point to XFS inodes
1972 * before we cast and access them as XFS structures as we have no
1973 * control over what the user passes us here.
1974 */
1975 if (f.file->f_op != &xfs_file_operations ||
1976 tmp.file->f_op != &xfs_file_operations) {
1977 error = -EINVAL;
1978 goto out_put_tmp_file;
1979 }
1980
Dave Chinnera133d952013-08-12 20:49:48 +10001981 ip = XFS_I(file_inode(f.file));
1982 tip = XFS_I(file_inode(tmp.file));
1983
1984 if (ip->i_mount != tip->i_mount) {
Dave Chinner24513372014-06-25 14:58:08 +10001985 error = -EINVAL;
Dave Chinnera133d952013-08-12 20:49:48 +10001986 goto out_put_tmp_file;
1987 }
1988
1989 if (ip->i_ino == tip->i_ino) {
Dave Chinner24513372014-06-25 14:58:08 +10001990 error = -EINVAL;
Dave Chinnera133d952013-08-12 20:49:48 +10001991 goto out_put_tmp_file;
1992 }
1993
1994 if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
Dave Chinner24513372014-06-25 14:58:08 +10001995 error = -EIO;
Dave Chinnera133d952013-08-12 20:49:48 +10001996 goto out_put_tmp_file;
1997 }
1998
1999 error = xfs_swap_extents(ip, tip, sxp);
2000
2001 out_put_tmp_file:
2002 fdput(tmp);
2003 out_put_file:
2004 fdput(f);
2005 out:
2006 return error;
2007}
2008
Eric Sandeenf7664b32018-05-15 13:21:48 -07002009static int
2010xfs_ioc_getlabel(
2011 struct xfs_mount *mp,
2012 char __user *user_label)
2013{
2014 struct xfs_sb *sbp = &mp->m_sb;
2015 char label[XFSLABEL_MAX + 1];
2016
2017 /* Paranoia */
2018 BUILD_BUG_ON(sizeof(sbp->sb_fname) > FSLABEL_MAX);
2019
Arnd Bergmann4bb8b652018-06-05 19:42:45 -07002020 /* 1 larger than sb_fname, so this ensures a trailing NUL char */
2021 memset(label, 0, sizeof(label));
Eric Sandeenf7664b32018-05-15 13:21:48 -07002022 spin_lock(&mp->m_sb_lock);
Arnd Bergmann4bb8b652018-06-05 19:42:45 -07002023 strncpy(label, sbp->sb_fname, XFSLABEL_MAX);
Eric Sandeenf7664b32018-05-15 13:21:48 -07002024 spin_unlock(&mp->m_sb_lock);
2025
Arnd Bergmann4bb8b652018-06-05 19:42:45 -07002026 if (copy_to_user(user_label, label, sizeof(label)))
Eric Sandeenf7664b32018-05-15 13:21:48 -07002027 return -EFAULT;
2028 return 0;
2029}
2030
2031static int
2032xfs_ioc_setlabel(
2033 struct file *filp,
2034 struct xfs_mount *mp,
2035 char __user *newlabel)
2036{
2037 struct xfs_sb *sbp = &mp->m_sb;
2038 char label[XFSLABEL_MAX + 1];
2039 size_t len;
2040 int error;
2041
2042 if (!capable(CAP_SYS_ADMIN))
2043 return -EPERM;
2044 /*
2045 * The generic ioctl allows up to FSLABEL_MAX chars, but XFS is much
2046 * smaller, at 12 bytes. We copy one more to be sure we find the
2047 * (required) NULL character to test the incoming label length.
2048 * NB: The on disk label doesn't need to be null terminated.
2049 */
2050 if (copy_from_user(label, newlabel, XFSLABEL_MAX + 1))
2051 return -EFAULT;
2052 len = strnlen(label, XFSLABEL_MAX + 1);
2053 if (len > sizeof(sbp->sb_fname))
2054 return -EINVAL;
2055
2056 error = mnt_want_write_file(filp);
2057 if (error)
2058 return error;
2059
2060 spin_lock(&mp->m_sb_lock);
2061 memset(sbp->sb_fname, 0, sizeof(sbp->sb_fname));
Arnd Bergmann4bb8b652018-06-05 19:42:45 -07002062 memcpy(sbp->sb_fname, label, len);
Eric Sandeenf7664b32018-05-15 13:21:48 -07002063 spin_unlock(&mp->m_sb_lock);
2064
2065 /*
2066 * Now we do several things to satisfy userspace.
2067 * In addition to normal logging of the primary superblock, we also
2068 * immediately write these changes to sector zero for the primary, then
2069 * update all backup supers (as xfs_db does for a label change), then
2070 * invalidate the block device page cache. This is so that any prior
2071 * buffered reads from userspace (i.e. from blkid) are invalidated,
2072 * and userspace will see the newly-written label.
2073 */
2074 error = xfs_sync_sb_buf(mp);
2075 if (error)
2076 goto out;
2077 /*
2078 * growfs also updates backup supers so lock against that.
2079 */
2080 mutex_lock(&mp->m_growlock);
2081 error = xfs_update_secondary_sbs(mp);
2082 mutex_unlock(&mp->m_growlock);
2083
2084 invalidate_bdev(mp->m_ddev_targp->bt_bdev);
2085
2086out:
2087 mnt_drop_write_file(filp);
2088 return error;
2089}
2090
Christoph Hellwig4d4be482008-12-09 04:47:33 -05002091/*
2092 * Note: some of the ioctl's return positive numbers as a
2093 * byte count indicating success, such as readlink_by_handle.
2094 * So we don't "sign flip" like most other routines. This means
2095 * true errors need to be returned as a negative value.
2096 */
2097long
2098xfs_file_ioctl(
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002099 struct file *filp,
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002100 unsigned int cmd,
Christoph Hellwig4d4be482008-12-09 04:47:33 -05002101 unsigned long p)
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002102{
Al Viro496ad9a2013-01-23 17:07:38 -05002103 struct inode *inode = file_inode(filp);
Christoph Hellwig4d4be482008-12-09 04:47:33 -05002104 struct xfs_inode *ip = XFS_I(inode);
2105 struct xfs_mount *mp = ip->i_mount;
2106 void __user *arg = (void __user *)p;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002107 int error;
2108
Christoph Hellwigcca28fb2010-06-24 11:57:09 +10002109 trace_xfs_file_ioctl(ip);
Christoph Hellwig4d4be482008-12-09 04:47:33 -05002110
2111 switch (cmd) {
Christoph Hellwiga46db602011-01-07 13:02:04 +00002112 case FITRIM:
2113 return xfs_ioc_trim(mp, arg);
Eric Sandeenf7664b32018-05-15 13:21:48 -07002114 case FS_IOC_GETFSLABEL:
2115 return xfs_ioc_getlabel(mp, arg);
2116 case FS_IOC_SETFSLABEL:
2117 return xfs_ioc_setlabel(filp, mp, arg);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002118 case XFS_IOC_ALLOCSP:
2119 case XFS_IOC_FREESP:
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002120 case XFS_IOC_ALLOCSP64:
Christoph Hellwig837a6e72019-10-24 22:26:02 -07002121 case XFS_IOC_FREESP64: {
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06002122 xfs_flock64_t bf;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002123
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06002124 if (copy_from_user(&bf, arg, sizeof(bf)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002125 return -EFAULT;
Christoph Hellwig837a6e72019-10-24 22:26:02 -07002126 return xfs_ioc_space(filp, &bf);
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06002127 }
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002128 case XFS_IOC_DIOINFO: {
Christoph Hellwigc7d68312019-10-24 22:25:39 -07002129 struct xfs_buftarg *target = xfs_inode_buftarg(ip);
2130 struct dioattr da;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002131
Eric Sandeen7c71ee72014-01-21 16:46:23 -06002132 da.d_mem = da.d_miniosz = target->bt_logical_sectorsize;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002133 da.d_maxiosz = INT_MAX & ~(da.d_miniosz - 1);
2134
2135 if (copy_to_user(arg, &da, sizeof(da)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002136 return -EFAULT;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002137 return 0;
2138 }
2139
2140 case XFS_IOC_FSBULKSTAT_SINGLE:
2141 case XFS_IOC_FSBULKSTAT:
2142 case XFS_IOC_FSINUMBERS:
Darrick J. Wong8bfe9d12019-07-03 20:36:26 -07002143 return xfs_ioc_fsbulkstat(mp, cmd, arg);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002144
Darrick J. Wong0448b6f2019-07-03 20:36:27 -07002145 case XFS_IOC_BULKSTAT:
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002146 return xfs_ioc_bulkstat(mp, cmd, arg);
Darrick J. Wongfba97602019-07-03 20:36:28 -07002147 case XFS_IOC_INUMBERS:
2148 return xfs_ioc_inumbers(mp, cmd, arg);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002149
2150 case XFS_IOC_FSGEOMETRY_V1:
Dave Chinner1b6d9682019-04-12 07:41:16 -07002151 return xfs_ioc_fsgeometry(mp, arg, 3);
2152 case XFS_IOC_FSGEOMETRY_V4:
2153 return xfs_ioc_fsgeometry(mp, arg, 4);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002154 case XFS_IOC_FSGEOMETRY:
Dave Chinner1b6d9682019-04-12 07:41:16 -07002155 return xfs_ioc_fsgeometry(mp, arg, 5);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002156
Darrick J. Wong7cd50062019-04-12 07:41:17 -07002157 case XFS_IOC_AG_GEOMETRY:
2158 return xfs_ioc_ag_geometry(mp, arg);
2159
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002160 case XFS_IOC_GETVERSION:
2161 return put_user(inode->i_generation, (int __user *)arg);
2162
2163 case XFS_IOC_FSGETXATTR:
2164 return xfs_ioc_fsgetxattr(ip, 0, arg);
2165 case XFS_IOC_FSGETXATTRA:
2166 return xfs_ioc_fsgetxattr(ip, 1, arg);
Lachlan McIlroy3b2816b2008-04-18 12:43:35 +10002167 case XFS_IOC_FSSETXATTR:
Lachlan McIlroy65e67f52008-04-18 12:59:45 +10002168 return xfs_ioc_fssetxattr(ip, filp, arg);
2169 case XFS_IOC_GETXFLAGS:
2170 return xfs_ioc_getxflags(ip, arg);
2171 case XFS_IOC_SETXFLAGS:
2172 return xfs_ioc_setxflags(ip, filp, arg);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002173
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002174 case XFS_IOC_GETBMAP:
2175 case XFS_IOC_GETBMAPA:
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002176 case XFS_IOC_GETBMAPX:
Christoph Hellwig232b51942017-10-17 14:16:19 -07002177 return xfs_ioc_getbmap(filp, cmd, arg);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002178
Darrick J. Wonge89c0412017-03-28 14:56:37 -07002179 case FS_IOC_GETFSMAP:
2180 return xfs_ioc_getfsmap(ip, arg);
2181
Darrick J. Wong36fd6e82017-10-17 21:37:34 -07002182 case XFS_IOC_SCRUB_METADATA:
2183 return xfs_ioc_scrub_metadata(ip, arg);
2184
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002185 case XFS_IOC_FD_TO_HANDLE:
2186 case XFS_IOC_PATH_TO_HANDLE:
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06002187 case XFS_IOC_PATH_TO_FSHANDLE: {
2188 xfs_fsop_handlereq_t hreq;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002189
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06002190 if (copy_from_user(&hreq, arg, sizeof(hreq)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002191 return -EFAULT;
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06002192 return xfs_find_handle(cmd, &hreq);
2193 }
2194 case XFS_IOC_OPEN_BY_HANDLE: {
2195 xfs_fsop_handlereq_t hreq;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002196
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06002197 if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002198 return -EFAULT;
Christoph Hellwigd296d302009-01-19 02:02:57 +01002199 return xfs_open_by_handle(filp, &hreq);
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06002200 }
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002201
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06002202 case XFS_IOC_READLINK_BY_HANDLE: {
2203 xfs_fsop_handlereq_t hreq;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002204
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06002205 if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002206 return -EFAULT;
Christoph Hellwigd296d302009-01-19 02:02:57 +01002207 return xfs_readlink_by_handle(filp, &hreq);
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06002208 }
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002209 case XFS_IOC_ATTRLIST_BY_HANDLE:
Christoph Hellwigd296d302009-01-19 02:02:57 +01002210 return xfs_attrlist_by_handle(filp, arg);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002211
2212 case XFS_IOC_ATTRMULTI_BY_HANDLE:
Christoph Hellwigd296d302009-01-19 02:02:57 +01002213 return xfs_attrmulti_by_handle(filp, arg);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002214
2215 case XFS_IOC_SWAPEXT: {
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06002216 struct xfs_swapext sxp;
2217
2218 if (copy_from_user(&sxp, arg, sizeof(xfs_swapext_t)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002219 return -EFAULT;
Jan Karad9457dc2012-06-12 16:20:39 +02002220 error = mnt_want_write_file(filp);
2221 if (error)
2222 return error;
Dave Chinnera133d952013-08-12 20:49:48 +10002223 error = xfs_ioc_swapext(&sxp);
Jan Karad9457dc2012-06-12 16:20:39 +02002224 mnt_drop_write_file(filp);
Dave Chinner24513372014-06-25 14:58:08 +10002225 return error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002226 }
2227
2228 case XFS_IOC_FSCOUNTS: {
2229 xfs_fsop_counts_t out;
2230
Eric Sandeen91083262019-05-01 20:26:30 -07002231 xfs_fs_counts(mp, &out);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002232
2233 if (copy_to_user(arg, &out, sizeof(out)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002234 return -EFAULT;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002235 return 0;
2236 }
2237
2238 case XFS_IOC_SET_RESBLKS: {
2239 xfs_fsop_resblks_t inout;
Darrick J. Wongc8ce5402017-06-16 11:00:05 -07002240 uint64_t in;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002241
2242 if (!capable(CAP_SYS_ADMIN))
2243 return -EPERM;
2244
Eric Sandeend5db0f92010-02-05 22:59:53 +00002245 if (mp->m_flags & XFS_MOUNT_RDONLY)
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002246 return -EROFS;
Eric Sandeend5db0f92010-02-05 22:59:53 +00002247
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002248 if (copy_from_user(&inout, arg, sizeof(inout)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002249 return -EFAULT;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002250
Jan Karad9457dc2012-06-12 16:20:39 +02002251 error = mnt_want_write_file(filp);
2252 if (error)
2253 return error;
2254
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002255 /* input parameter is passed in resblks field of structure */
2256 in = inout.resblks;
2257 error = xfs_reserve_blocks(mp, &in, &inout);
Jan Karad9457dc2012-06-12 16:20:39 +02002258 mnt_drop_write_file(filp);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002259 if (error)
Dave Chinner24513372014-06-25 14:58:08 +10002260 return error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002261
2262 if (copy_to_user(arg, &inout, sizeof(inout)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002263 return -EFAULT;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002264 return 0;
2265 }
2266
2267 case XFS_IOC_GET_RESBLKS: {
2268 xfs_fsop_resblks_t out;
2269
2270 if (!capable(CAP_SYS_ADMIN))
2271 return -EPERM;
2272
2273 error = xfs_reserve_blocks(mp, NULL, &out);
2274 if (error)
Dave Chinner24513372014-06-25 14:58:08 +10002275 return error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002276
2277 if (copy_to_user(arg, &out, sizeof(out)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002278 return -EFAULT;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002279
2280 return 0;
2281 }
2282
2283 case XFS_IOC_FSGROWFSDATA: {
2284 xfs_growfs_data_t in;
2285
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002286 if (copy_from_user(&in, arg, sizeof(in)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002287 return -EFAULT;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002288
Jan Karad9457dc2012-06-12 16:20:39 +02002289 error = mnt_want_write_file(filp);
2290 if (error)
2291 return error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002292 error = xfs_growfs_data(mp, &in);
Jan Karad9457dc2012-06-12 16:20:39 +02002293 mnt_drop_write_file(filp);
Dave Chinner24513372014-06-25 14:58:08 +10002294 return error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002295 }
2296
2297 case XFS_IOC_FSGROWFSLOG: {
2298 xfs_growfs_log_t in;
2299
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002300 if (copy_from_user(&in, arg, sizeof(in)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002301 return -EFAULT;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002302
Jan Karad9457dc2012-06-12 16:20:39 +02002303 error = mnt_want_write_file(filp);
2304 if (error)
2305 return error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002306 error = xfs_growfs_log(mp, &in);
Jan Karad9457dc2012-06-12 16:20:39 +02002307 mnt_drop_write_file(filp);
Dave Chinner24513372014-06-25 14:58:08 +10002308 return error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002309 }
2310
2311 case XFS_IOC_FSGROWFSRT: {
2312 xfs_growfs_rt_t in;
2313
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002314 if (copy_from_user(&in, arg, sizeof(in)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002315 return -EFAULT;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002316
Jan Karad9457dc2012-06-12 16:20:39 +02002317 error = mnt_want_write_file(filp);
2318 if (error)
2319 return error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002320 error = xfs_growfs_rt(mp, &in);
Jan Karad9457dc2012-06-12 16:20:39 +02002321 mnt_drop_write_file(filp);
Dave Chinner24513372014-06-25 14:58:08 +10002322 return error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002323 }
2324
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002325 case XFS_IOC_GOINGDOWN: {
Darrick J. Wongc8ce5402017-06-16 11:00:05 -07002326 uint32_t in;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002327
2328 if (!capable(CAP_SYS_ADMIN))
2329 return -EPERM;
2330
Darrick J. Wongc8ce5402017-06-16 11:00:05 -07002331 if (get_user(in, (uint32_t __user *)arg))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002332 return -EFAULT;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002333
Dave Chinner24513372014-06-25 14:58:08 +10002334 return xfs_fs_goingdown(mp, in);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002335 }
2336
2337 case XFS_IOC_ERROR_INJECTION: {
2338 xfs_error_injection_t in;
2339
2340 if (!capable(CAP_SYS_ADMIN))
2341 return -EPERM;
2342
2343 if (copy_from_user(&in, arg, sizeof(in)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002344 return -EFAULT;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002345
Darrick J. Wong31965ef2017-06-20 17:54:46 -07002346 return xfs_errortag_add(mp, in.errtag);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002347 }
2348
2349 case XFS_IOC_ERROR_CLEARALL:
2350 if (!capable(CAP_SYS_ADMIN))
2351 return -EPERM;
2352
Darrick J. Wong31965ef2017-06-20 17:54:46 -07002353 return xfs_errortag_clearall(mp);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002354
Brian Foster8ca149d2012-11-07 12:21:12 -05002355 case XFS_IOC_FREE_EOFBLOCKS: {
Dwight Engenb9fe5052013-08-15 14:08:02 -04002356 struct xfs_fs_eofblocks eofb;
2357 struct xfs_eofblocks keofb;
Brian Foster8ca149d2012-11-07 12:21:12 -05002358
Dwight Engen8c567a72013-08-15 14:08:03 -04002359 if (!capable(CAP_SYS_ADMIN))
2360 return -EPERM;
2361
2362 if (mp->m_flags & XFS_MOUNT_RDONLY)
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002363 return -EROFS;
Dwight Engen8c567a72013-08-15 14:08:03 -04002364
Brian Foster8ca149d2012-11-07 12:21:12 -05002365 if (copy_from_user(&eofb, arg, sizeof(eofb)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002366 return -EFAULT;
Brian Foster8ca149d2012-11-07 12:21:12 -05002367
Dwight Engenb9fe5052013-08-15 14:08:02 -04002368 error = xfs_fs_eofblocks_from_user(&eofb, &keofb);
2369 if (error)
Dave Chinner24513372014-06-25 14:58:08 +10002370 return error;
Brian Foster8ca149d2012-11-07 12:21:12 -05002371
Dave Chinner24513372014-06-25 14:58:08 +10002372 return xfs_icache_free_eofblocks(mp, &keofb);
Brian Foster8ca149d2012-11-07 12:21:12 -05002373 }
2374
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002375 default:
2376 return -ENOTTY;
2377 }
2378}