blob: 34f59697f8ddb58a9ee7841da522ea431c47db73 [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 */
323 if (((context->flags & ATTR_SECURE) == 0) !=
324 ((flags & XFS_ATTR_SECURE) == 0))
325 return;
326 if (((context->flags & ATTR_ROOT) == 0) !=
327 ((flags & XFS_ATTR_ROOT) == 0))
328 return;
329
330 arraytop = sizeof(*alist) +
331 context->count * sizeof(alist->al_offset[0]);
332
333 /* decrement by the actual bytes used by the attr */
334 context->firstu -= round_up(offsetof(struct xfs_attrlist_ent, a_name) +
335 namelen + 1, sizeof(uint32_t));
336 if (context->firstu < arraytop) {
337 trace_xfs_attr_list_full(context);
338 alist->al_more = 1;
339 context->seen_enough = 1;
340 return;
341 }
342
343 aep = context->buffer + context->firstu;
344 aep->a_valuelen = valuelen;
345 memcpy(aep->a_name, name, namelen);
346 aep->a_name[namelen] = 0;
347 alist->al_offset[context->count++] = context->firstu;
348 alist->al_count = context->count;
349 trace_xfs_attr_list_add(context);
350}
351
352int
353xfs_ioc_attr_list(
354 struct xfs_inode *dp,
Christoph Hellwigeb241c72020-02-26 17:30:40 -0800355 void __user *ubuf,
Christoph Hellwig3e7a7792020-02-26 17:30:38 -0800356 int bufsize,
357 int flags,
358 struct attrlist_cursor_kern *cursor)
359{
360 struct xfs_attr_list_context context;
361 struct xfs_attrlist *alist;
Christoph Hellwigeb241c72020-02-26 17:30:40 -0800362 void *buffer;
Christoph Hellwig3e7a7792020-02-26 17:30:38 -0800363 int error;
364
Christoph Hellwigf6046312020-02-26 17:30:40 -0800365 if (bufsize < sizeof(struct xfs_attrlist) ||
366 bufsize > XFS_XATTR_LIST_MAX)
367 return -EINVAL;
368
369 /*
370 * Reject flags, only allow namespaces.
371 */
372 if (flags & ~(ATTR_ROOT | ATTR_SECURE))
373 return -EINVAL;
374 if (flags == (ATTR_ROOT | ATTR_SECURE))
375 return -EINVAL;
376
Christoph Hellwig3e7a7792020-02-26 17:30:38 -0800377 /*
378 * Validate the cursor.
379 */
380 if (cursor->pad1 || cursor->pad2)
381 return -EINVAL;
382 if ((cursor->initted == 0) &&
383 (cursor->hashval || cursor->blkno || cursor->offset))
384 return -EINVAL;
385
Christoph Hellwigeb241c72020-02-26 17:30:40 -0800386 buffer = kmem_zalloc_large(bufsize, 0);
387 if (!buffer)
388 return -ENOMEM;
Christoph Hellwig3e7a7792020-02-26 17:30:38 -0800389
390 /*
391 * Initialize the output buffer.
392 */
393 memset(&context, 0, sizeof(context));
394 context.dp = dp;
395 context.cursor = cursor;
396 context.resynch = 1;
397 context.flags = flags;
398 context.buffer = buffer;
399 context.bufsize = (bufsize & ~(sizeof(int)-1)); /* align */
400 context.firstu = context.bufsize;
401 context.put_listent = xfs_ioc_attr_put_listent;
402
403 alist = context.buffer;
404 alist->al_count = 0;
405 alist->al_more = 0;
406 alist->al_offset[0] = context.bufsize;
407
Christoph Hellwig17e1dd82020-02-26 17:30:39 -0800408 error = xfs_attr_list(&context);
Christoph Hellwigeb241c72020-02-26 17:30:40 -0800409 if (error)
410 goto out_free;
411
412 if (copy_to_user(ubuf, buffer, bufsize))
413 error = -EFAULT;
414out_free:
415 kmem_free(buffer);
Christoph Hellwig3e7a7792020-02-26 17:30:38 -0800416 return error;
417}
418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419STATIC int
420xfs_attrlist_by_handle(
Christoph Hellwigd296d302009-01-19 02:02:57 +0100421 struct file *parfilp,
422 void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
Christoph Hellwigd296d302009-01-19 02:02:57 +0100424 int error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 attrlist_cursor_kern_t *cursor;
Darrick J. Wong0facef72016-08-03 10:58:53 +1000426 struct xfs_fsop_attrlist_handlereq __user *p = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 xfs_fsop_attrlist_handlereq_t al_hreq;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100428 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430 if (!capable(CAP_SYS_ADMIN))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000431 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 if (copy_from_user(&al_hreq, arg, sizeof(xfs_fsop_attrlist_handlereq_t)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000433 return -EFAULT;
Christoph Hellwig90ad58a2008-06-27 13:32:19 +1000434
Christoph Hellwigd296d302009-01-19 02:02:57 +0100435 dentry = xfs_handlereq_to_dentry(parfilp, &al_hreq.hreq);
436 if (IS_ERR(dentry))
437 return PTR_ERR(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Christoph Hellwigeb241c72020-02-26 17:30:40 -0800439 cursor = (attrlist_cursor_kern_t *)&al_hreq.pos;
440 error = xfs_ioc_attr_list(XFS_I(d_inode(dentry)), al_hreq.buffer,
441 al_hreq.buflen, al_hreq.flags, cursor);
442 if (error)
Dave Chinnerfdd3cce2013-09-02 20:53:00 +1000443 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Christoph Hellwigeb241c72020-02-26 17:30:40 -0800445 if (copy_to_user(&p->pos, cursor, sizeof(attrlist_cursor_kern_t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 error = -EFAULT;
447
Dave Chinnerfdd3cce2013-09-02 20:53:00 +1000448out_dput:
Christoph Hellwigd296d302009-01-19 02:02:57 +0100449 dput(dentry);
450 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451}
452
Christoph Hellwigd0ce64392020-02-26 17:30:31 -0800453static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454xfs_attrmulti_attr_get(
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000455 struct inode *inode,
Dave Chinnera9273ca2010-01-20 10:47:48 +1100456 unsigned char *name,
457 unsigned char __user *ubuf,
Darrick J. Wongc8ce5402017-06-16 11:00:05 -0700458 uint32_t *len,
459 uint32_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
Christoph Hellwige5171d72020-02-26 17:30:34 -0800461 struct xfs_da_args args = {
462 .dp = XFS_I(inode),
463 .flags = flags,
464 .name = name,
465 .namelen = strlen(name),
466 .valuelen = *len,
467 };
468 int error;
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000469
Jan Tulak51fcbfe2015-10-12 16:03:59 +1100470 if (*len > XFS_XATTR_SIZE_MAX)
Dave Chinner24513372014-06-25 14:58:08 +1000471 return -EINVAL;
Christoph Hellwige5171d72020-02-26 17:30:34 -0800472
473 args.value = kmem_zalloc_large(*len, 0);
474 if (!args.value)
Dave Chinner24513372014-06-25 14:58:08 +1000475 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Christoph Hellwige5171d72020-02-26 17:30:34 -0800477 error = xfs_attr_get(&args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 if (error)
479 goto out_kfree;
480
Christoph Hellwige5171d72020-02-26 17:30:34 -0800481 *len = args.valuelen;
482 if (copy_to_user(ubuf, args.value, args.valuelen))
Dave Chinner24513372014-06-25 14:58:08 +1000483 error = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Dave Chinnerfdd3cce2013-09-02 20:53:00 +1000485out_kfree:
Christoph Hellwige5171d72020-02-26 17:30:34 -0800486 kmem_free(args.value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 return error;
488}
489
Christoph Hellwigd0ce64392020-02-26 17:30:31 -0800490static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491xfs_attrmulti_attr_set(
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000492 struct inode *inode,
Dave Chinnera9273ca2010-01-20 10:47:48 +1100493 unsigned char *name,
494 const unsigned char __user *ubuf,
Darrick J. Wongc8ce5402017-06-16 11:00:05 -0700495 uint32_t len,
496 uint32_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
Christoph Hellwiga2544622020-02-26 17:30:33 -0800498 struct xfs_da_args args = {
499 .dp = XFS_I(inode),
500 .flags = flags,
501 .name = name,
502 .namelen = strlen(name),
503 };
Andreas Gruenbacher09cb22d2015-11-03 12:53:54 +1100504 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000506 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
Dave Chinner24513372014-06-25 14:58:08 +1000507 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Christoph Hellwig6cc4f4f2020-02-26 17:30:30 -0800509 if (ubuf) {
510 if (len > XFS_XATTR_SIZE_MAX)
511 return -EINVAL;
Christoph Hellwiga2544622020-02-26 17:30:33 -0800512 args.value = memdup_user(ubuf, len);
513 if (IS_ERR(args.value))
514 return PTR_ERR(args.value);
515 args.valuelen = len;
Christoph Hellwig6cc4f4f2020-02-26 17:30:30 -0800516 }
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000517
Christoph Hellwiga2544622020-02-26 17:30:33 -0800518 error = xfs_attr_set(&args);
Andreas Gruenbacher47e1bf62015-11-03 12:56:17 +1100519 if (!error)
520 xfs_forget_acl(inode, name, flags);
Christoph Hellwiga2544622020-02-26 17:30:33 -0800521 kfree(args.value);
Andreas Gruenbacher09cb22d2015-11-03 12:53:54 +1100522 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523}
524
Christoph Hellwigd0ce64392020-02-26 17:30:31 -0800525int
526xfs_ioc_attrmulti_one(
527 struct file *parfilp,
528 struct inode *inode,
529 uint32_t opcode,
530 void __user *uname,
531 void __user *value,
532 uint32_t *len,
533 uint32_t flags)
534{
535 unsigned char *name;
536 int error;
537
538 if ((flags & ATTR_ROOT) && (flags & ATTR_SECURE))
539 return -EINVAL;
Christoph Hellwigd0ce64392020-02-26 17:30:31 -0800540
541 name = strndup_user(uname, MAXNAMELEN);
542 if (IS_ERR(name))
543 return PTR_ERR(name);
544
545 switch (opcode) {
546 case ATTR_OP_GET:
547 error = xfs_attrmulti_attr_get(inode, name, value, len, flags);
548 break;
549 case ATTR_OP_REMOVE:
550 value = NULL;
551 *len = 0;
552 /* fall through */
553 case ATTR_OP_SET:
554 error = mnt_want_write_file(parfilp);
555 if (error)
556 break;
557 error = xfs_attrmulti_attr_set(inode, name, value, *len, flags);
558 mnt_drop_write_file(parfilp);
559 break;
560 default:
561 error = -EINVAL;
562 break;
563 }
564
565 kfree(name);
566 return error;
567}
568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569STATIC int
570xfs_attrmulti_by_handle(
Dave Hansen42a74f22008-02-15 14:37:46 -0800571 struct file *parfilp,
Christoph Hellwigd296d302009-01-19 02:02:57 +0100572 void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
574 int error;
575 xfs_attr_multiop_t *ops;
576 xfs_fsop_attrmulti_handlereq_t am_hreq;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100577 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 unsigned int i, size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580 if (!capable(CAP_SYS_ADMIN))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000581 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 if (copy_from_user(&am_hreq, arg, sizeof(xfs_fsop_attrmulti_handlereq_t)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000583 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Zhitong Wangfda168c2010-03-23 09:51:22 +1100585 /* overflow check */
586 if (am_hreq.opcount >= INT_MAX / sizeof(xfs_attr_multiop_t))
587 return -E2BIG;
588
Christoph Hellwigd296d302009-01-19 02:02:57 +0100589 dentry = xfs_handlereq_to_dentry(parfilp, &am_hreq.hreq);
590 if (IS_ERR(dentry))
591 return PTR_ERR(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Dave Chinner24513372014-06-25 14:58:08 +1000593 error = -E2BIG;
Christoph Hellwige182f572008-06-27 13:32:31 +1000594 size = am_hreq.opcount * sizeof(xfs_attr_multiop_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 if (!size || size > 16 * PAGE_SIZE)
Christoph Hellwigd296d302009-01-19 02:02:57 +0100596 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Li Zefan0e639bd2009-04-08 15:08:04 +0800598 ops = memdup_user(am_hreq.ops, size);
599 if (IS_ERR(ops)) {
Dave Chinner24513372014-06-25 14:58:08 +1000600 error = PTR_ERR(ops);
Christoph Hellwigd296d302009-01-19 02:02:57 +0100601 goto out_dput;
Li Zefan0e639bd2009-04-08 15:08:04 +0800602 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 error = 0;
605 for (i = 0; i < am_hreq.opcount; i++) {
Christoph Hellwigd0ce64392020-02-26 17:30:31 -0800606 ops[i].am_error = xfs_ioc_attrmulti_one(parfilp,
607 d_inode(dentry), ops[i].am_opcode,
608 ops[i].am_attrname, ops[i].am_attrvalue,
609 &ops[i].am_length, ops[i].am_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 }
611
612 if (copy_to_user(am_hreq.ops, ops, size))
Dave Chinner24513372014-06-25 14:58:08 +1000613 error = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 kfree(ops);
Christoph Hellwigd296d302009-01-19 02:02:57 +0100616 out_dput:
617 dput(dentry);
Dave Chinner24513372014-06-25 14:58:08 +1000618 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619}
620
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -0600621int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622xfs_ioc_space(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 struct file *filp,
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -0600624 xfs_flock64_t *bf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
Christoph Hellwig8f3e2052016-07-20 11:29:35 +1000626 struct inode *inode = file_inode(filp);
627 struct xfs_inode *ip = XFS_I(inode);
Christoph Hellwig865e9442013-10-12 00:55:08 -0700628 struct iattr iattr;
Christoph Hellwig837a6e72019-10-24 22:26:02 -0700629 enum xfs_prealloc_flags flags = XFS_PREALLOC_CLEAR;
Dan Williamsc63a8ea2018-03-12 14:12:29 -0700630 uint iolock = XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 int error;
632
Alexey Dobriyanf37ea142006-09-28 10:52:04 +1000633 if (inode->i_flags & (S_IMMUTABLE|S_APPEND))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000634 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Eric Sandeenad4a8ac2005-09-02 16:41:16 +1000636 if (!(filp->f_mode & FMODE_WRITE))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000637 return -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Alexey Dobriyanf37ea142006-09-28 10:52:04 +1000639 if (!S_ISREG(inode->i_mode))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000640 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Christoph Hellwig7a42c702019-10-24 22:26:27 -0700642 if (xfs_is_always_cow_inode(ip))
643 return -EOPNOTSUPP;
644
Christoph Hellwig8add71c2015-02-02 09:53:56 +1100645 if (filp->f_flags & O_DSYNC)
646 flags |= XFS_PREALLOC_SYNC;
Christoph Hellwig8f3e2052016-07-20 11:29:35 +1000647 if (filp->f_mode & FMODE_NOCMTIME)
Christoph Hellwig8add71c2015-02-02 09:53:56 +1100648 flags |= XFS_PREALLOC_INVISIBLE;
649
Jan Karad9457dc2012-06-12 16:20:39 +0200650 error = mnt_want_write_file(filp);
651 if (error)
652 return error;
Christoph Hellwig865e9442013-10-12 00:55:08 -0700653
Christoph Hellwig781355c2015-02-16 11:59:50 +1100654 xfs_ilock(ip, iolock);
Dan Williams69eb5fa2018-03-20 14:42:38 -0700655 error = xfs_break_layouts(inode, &iolock, BREAK_UNMAP);
Christoph Hellwig781355c2015-02-16 11:59:50 +1100656 if (error)
657 goto out_unlock;
Dave Chinner249bd902019-10-29 13:04:32 -0700658 inode_dio_wait(inode);
Christoph Hellwig865e9442013-10-12 00:55:08 -0700659
660 switch (bf->l_whence) {
661 case 0: /*SEEK_SET*/
662 break;
663 case 1: /*SEEK_CUR*/
664 bf->l_start += filp->f_pos;
665 break;
666 case 2: /*SEEK_END*/
667 bf->l_start += XFS_ISIZE(ip);
668 break;
669 default:
Dave Chinner24513372014-06-25 14:58:08 +1000670 error = -EINVAL;
Christoph Hellwig865e9442013-10-12 00:55:08 -0700671 goto out_unlock;
672 }
673
Christoph Hellwig837a6e72019-10-24 22:26:02 -0700674 if (bf->l_start < 0 || bf->l_start > inode->i_sb->s_maxbytes) {
Dave Chinner24513372014-06-25 14:58:08 +1000675 error = -EINVAL;
Christoph Hellwig865e9442013-10-12 00:55:08 -0700676 goto out_unlock;
677 }
678
Christoph Hellwig837a6e72019-10-24 22:26:02 -0700679 if (bf->l_start > XFS_ISIZE(ip)) {
680 error = xfs_alloc_file_space(ip, XFS_ISIZE(ip),
681 bf->l_start - XFS_ISIZE(ip), 0);
682 if (error)
683 goto out_unlock;
Christoph Hellwig865e9442013-10-12 00:55:08 -0700684 }
685
Christoph Hellwig837a6e72019-10-24 22:26:02 -0700686 iattr.ia_valid = ATTR_SIZE;
687 iattr.ia_size = bf->l_start;
688 error = xfs_vn_setattr_size(file_dentry(filp), &iattr);
Christoph Hellwig865e9442013-10-12 00:55:08 -0700689 if (error)
690 goto out_unlock;
691
Christoph Hellwig8add71c2015-02-02 09:53:56 +1100692 error = xfs_update_prealloc_flags(ip, flags);
Christoph Hellwig865e9442013-10-12 00:55:08 -0700693
694out_unlock:
Christoph Hellwig781355c2015-02-16 11:59:50 +1100695 xfs_iunlock(ip, iolock);
Jan Karad9457dc2012-06-12 16:20:39 +0200696 mnt_drop_write_file(filp);
Dave Chinner24513372014-06-25 14:58:08 +1000697 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698}
699
Darrick J. Wong2810bd62019-07-02 09:39:40 -0700700/* Return 0 on success or positive error */
701int
Darrick J. Wong8bfe9d12019-07-03 20:36:26 -0700702xfs_fsbulkstat_one_fmt(
Darrick J. Wong7035f972019-07-03 20:36:26 -0700703 struct xfs_ibulk *breq,
704 const struct xfs_bulkstat *bstat)
Darrick J. Wong2810bd62019-07-02 09:39:40 -0700705{
Darrick J. Wong7035f972019-07-03 20:36:26 -0700706 struct xfs_bstat bs1;
707
708 xfs_bulkstat_to_bstat(breq->mp, &bs1, bstat);
709 if (copy_to_user(breq->ubuffer, &bs1, sizeof(bs1)))
Darrick J. Wong2810bd62019-07-02 09:39:40 -0700710 return -EFAULT;
711 return xfs_ibulk_advance(breq, sizeof(struct xfs_bstat));
712}
713
Darrick J. Wong677717f2019-07-02 09:39:43 -0700714int
Darrick J. Wong8bfe9d12019-07-03 20:36:26 -0700715xfs_fsinumbers_fmt(
Darrick J. Wong5f19c7f2019-07-03 20:36:27 -0700716 struct xfs_ibulk *breq,
717 const struct xfs_inumbers *igrp)
Darrick J. Wong677717f2019-07-02 09:39:43 -0700718{
Darrick J. Wong5f19c7f2019-07-03 20:36:27 -0700719 struct xfs_inogrp ig1;
720
721 xfs_inumbers_to_inogrp(&ig1, igrp);
722 if (copy_to_user(breq->ubuffer, &ig1, sizeof(struct xfs_inogrp)))
Darrick J. Wong677717f2019-07-02 09:39:43 -0700723 return -EFAULT;
724 return xfs_ibulk_advance(breq, sizeof(struct xfs_inogrp));
725}
726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727STATIC int
Darrick J. Wong8bfe9d12019-07-03 20:36:26 -0700728xfs_ioc_fsbulkstat(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 xfs_mount_t *mp,
730 unsigned int cmd,
731 void __user *arg)
732{
Darrick J. Wong2810bd62019-07-02 09:39:40 -0700733 struct xfs_fsop_bulkreq bulkreq;
734 struct xfs_ibulk breq = {
735 .mp = mp,
736 .ocount = 0,
737 };
738 xfs_ino_t lastino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 int error;
740
741 /* done = 1 if there are more stats to get and if bulkstat */
742 /* should be called again (unused here, but used in dmapi) */
743
744 if (!capable(CAP_SYS_ADMIN))
745 return -EPERM;
746
747 if (XFS_FORCED_SHUTDOWN(mp))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000748 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Darrick J. Wong6f71fb62019-07-03 20:36:25 -0700750 if (copy_from_user(&bulkreq, arg, sizeof(struct xfs_fsop_bulkreq)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000751 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Darrick J. Wong2810bd62019-07-02 09:39:40 -0700753 if (copy_from_user(&lastino, bulkreq.lastip, sizeof(__s64)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000754 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Darrick J. Wong2810bd62019-07-02 09:39:40 -0700756 if (bulkreq.icount <= 0)
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000757 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100759 if (bulkreq.ubuffer == NULL)
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000760 return -EINVAL;
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100761
Darrick J. Wong2810bd62019-07-02 09:39:40 -0700762 breq.ubuffer = bulkreq.ubuffer;
763 breq.icount = bulkreq.icount;
764
765 /*
766 * FSBULKSTAT_SINGLE expects that *lastip contains the inode number
767 * that we want to stat. However, FSINUMBERS and FSBULKSTAT expect
768 * that *lastip contains either zero or the number of the last inode to
769 * be examined by the previous call and return results starting with
770 * the next inode after that. The new bulk request back end functions
771 * take the inode to start with, so we have to compute the startino
772 * parameter from lastino to maintain correct function. lastino == 0
773 * is a special case because it has traditionally meant "first inode
774 * in filesystem".
775 */
776 if (cmd == XFS_IOC_FSINUMBERS) {
Darrick J. Wong677717f2019-07-02 09:39:43 -0700777 breq.startino = lastino ? lastino + 1 : 0;
Darrick J. Wong8bfe9d12019-07-03 20:36:26 -0700778 error = xfs_inumbers(&breq, xfs_fsinumbers_fmt);
Darrick J. Wong677717f2019-07-02 09:39:43 -0700779 lastino = breq.startino - 1;
Darrick J. Wong2810bd62019-07-02 09:39:40 -0700780 } else if (cmd == XFS_IOC_FSBULKSTAT_SINGLE) {
781 breq.startino = lastino;
782 breq.icount = 1;
Darrick J. Wong8bfe9d12019-07-03 20:36:26 -0700783 error = xfs_bulkstat_one(&breq, xfs_fsbulkstat_one_fmt);
Darrick J. Wong2810bd62019-07-02 09:39:40 -0700784 } else { /* XFS_IOC_FSBULKSTAT */
785 breq.startino = lastino ? lastino + 1 : 0;
Darrick J. Wong8bfe9d12019-07-03 20:36:26 -0700786 error = xfs_bulkstat(&breq, xfs_fsbulkstat_one_fmt);
Darrick J. Wong2810bd62019-07-02 09:39:40 -0700787 lastino = breq.startino - 1;
788 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
790 if (error)
Dave Chinner24513372014-06-25 14:58:08 +1000791 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Darrick J. Wongf16fe3e2019-07-02 09:39:39 -0700793 if (bulkreq.lastip != NULL &&
Darrick J. Wong2810bd62019-07-02 09:39:40 -0700794 copy_to_user(bulkreq.lastip, &lastino, sizeof(xfs_ino_t)))
Darrick J. Wongf16fe3e2019-07-02 09:39:39 -0700795 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Darrick J. Wongf16fe3e2019-07-02 09:39:39 -0700797 if (bulkreq.ocount != NULL &&
Darrick J. Wong2810bd62019-07-02 09:39:40 -0700798 copy_to_user(bulkreq.ocount, &breq.ocount, sizeof(__s32)))
Darrick J. Wongf16fe3e2019-07-02 09:39:39 -0700799 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
801 return 0;
802}
803
Darrick J. Wong0448b6f2019-07-03 20:36:27 -0700804/* Return 0 on success or positive error */
805static int
806xfs_bulkstat_fmt(
807 struct xfs_ibulk *breq,
808 const struct xfs_bulkstat *bstat)
809{
810 if (copy_to_user(breq->ubuffer, bstat, sizeof(struct xfs_bulkstat)))
811 return -EFAULT;
812 return xfs_ibulk_advance(breq, sizeof(struct xfs_bulkstat));
813}
814
815/*
816 * Check the incoming bulk request @hdr from userspace and initialize the
817 * internal @breq bulk request appropriately. Returns 0 if the bulk request
Darrick J. Wonge7ee96d2019-08-28 14:37:57 -0700818 * should proceed; -ECANCELED if there's nothing to do; or the usual
Darrick J. Wong0448b6f2019-07-03 20:36:27 -0700819 * negative error code.
820 */
821static int
822xfs_bulk_ireq_setup(
823 struct xfs_mount *mp,
824 struct xfs_bulk_ireq *hdr,
825 struct xfs_ibulk *breq,
826 void __user *ubuffer)
827{
828 if (hdr->icount == 0 ||
829 (hdr->flags & ~XFS_BULK_IREQ_FLAGS_ALL) ||
Darrick J. Wong0448b6f2019-07-03 20:36:27 -0700830 memchr_inv(hdr->reserved, 0, sizeof(hdr->reserved)))
831 return -EINVAL;
832
833 breq->startino = hdr->ino;
834 breq->ubuffer = ubuffer;
835 breq->icount = hdr->icount;
836 breq->ocount = 0;
Darrick J. Wong13d59a22019-07-03 20:36:28 -0700837 breq->flags = 0;
838
839 /*
Darrick J. Wongbf3cb392019-07-03 20:36:29 -0700840 * The @ino parameter is a special value, so we must look it up here.
841 * We're not allowed to have IREQ_AGNO, and we only return one inode
842 * worth of data.
843 */
844 if (hdr->flags & XFS_BULK_IREQ_SPECIAL) {
845 if (hdr->flags & XFS_BULK_IREQ_AGNO)
846 return -EINVAL;
847
848 switch (hdr->ino) {
849 case XFS_BULK_IREQ_SPECIAL_ROOT:
850 hdr->ino = mp->m_sb.sb_rootino;
851 break;
852 default:
853 return -EINVAL;
854 }
855 breq->icount = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 }
857
Darrick J. Wongbf3cb392019-07-03 20:36:29 -0700858 /*
Darrick J. Wong13d59a22019-07-03 20:36:28 -0700859 * The IREQ_AGNO flag means that we only want results from a given AG.
860 * If @hdr->ino is zero, we start iterating in that AG. If @hdr->ino is
861 * beyond the specified AG then we return no results.
862 */
863 if (hdr->flags & XFS_BULK_IREQ_AGNO) {
864 if (hdr->agno >= mp->m_sb.sb_agcount)
865 return -EINVAL;
866
867 if (breq->startino == 0)
868 breq->startino = XFS_AGINO_TO_INO(mp, hdr->agno, 0);
869 else if (XFS_INO_TO_AGNO(mp, breq->startino) < hdr->agno)
870 return -EINVAL;
871
872 breq->flags |= XFS_IBULK_SAME_AG;
873
874 /* Asking for an inode past the end of the AG? We're done! */
875 if (XFS_INO_TO_AGNO(mp, breq->startino) > hdr->agno)
Darrick J. Wonge7ee96d2019-08-28 14:37:57 -0700876 return -ECANCELED;
Darrick J. Wong13d59a22019-07-03 20:36:28 -0700877 } else if (hdr->agno)
878 return -EINVAL;
Darrick J. Wong0448b6f2019-07-03 20:36:27 -0700879
880 /* Asking for an inode past the end of the FS? We're done! */
881 if (XFS_INO_TO_AGNO(mp, breq->startino) >= mp->m_sb.sb_agcount)
Darrick J. Wonge7ee96d2019-08-28 14:37:57 -0700882 return -ECANCELED;
Darrick J. Wong0448b6f2019-07-03 20:36:27 -0700883
884 return 0;
885}
886
887/*
888 * Update the userspace bulk request @hdr to reflect the end state of the
889 * internal bulk request @breq.
890 */
891static void
892xfs_bulk_ireq_teardown(
893 struct xfs_bulk_ireq *hdr,
894 struct xfs_ibulk *breq)
895{
896 hdr->ino = breq->startino;
897 hdr->ocount = breq->ocount;
898}
899
900/* Handle the v5 bulkstat ioctl. */
901STATIC int
902xfs_ioc_bulkstat(
903 struct xfs_mount *mp,
904 unsigned int cmd,
905 struct xfs_bulkstat_req __user *arg)
906{
907 struct xfs_bulk_ireq hdr;
908 struct xfs_ibulk breq = {
909 .mp = mp,
910 };
911 int error;
912
913 if (!capable(CAP_SYS_ADMIN))
914 return -EPERM;
915
916 if (XFS_FORCED_SHUTDOWN(mp))
917 return -EIO;
918
919 if (copy_from_user(&hdr, &arg->hdr, sizeof(hdr)))
920 return -EFAULT;
921
922 error = xfs_bulk_ireq_setup(mp, &hdr, &breq, arg->bulkstat);
Darrick J. Wonge7ee96d2019-08-28 14:37:57 -0700923 if (error == -ECANCELED)
Darrick J. Wong0448b6f2019-07-03 20:36:27 -0700924 goto out_teardown;
925 if (error < 0)
926 return error;
927
928 error = xfs_bulkstat(&breq, xfs_bulkstat_fmt);
929 if (error)
930 return error;
931
932out_teardown:
933 xfs_bulk_ireq_teardown(&hdr, &breq);
934 if (copy_to_user(&arg->hdr, &hdr, sizeof(hdr)))
935 return -EFAULT;
936
937 return 0;
938}
939
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940STATIC int
Darrick J. Wongfba97602019-07-03 20:36:28 -0700941xfs_inumbers_fmt(
942 struct xfs_ibulk *breq,
943 const struct xfs_inumbers *igrp)
944{
945 if (copy_to_user(breq->ubuffer, igrp, sizeof(struct xfs_inumbers)))
946 return -EFAULT;
947 return xfs_ibulk_advance(breq, sizeof(struct xfs_inumbers));
948}
949
950/* Handle the v5 inumbers ioctl. */
951STATIC int
952xfs_ioc_inumbers(
953 struct xfs_mount *mp,
954 unsigned int cmd,
955 struct xfs_inumbers_req __user *arg)
956{
957 struct xfs_bulk_ireq hdr;
958 struct xfs_ibulk breq = {
959 .mp = mp,
960 };
961 int error;
962
963 if (!capable(CAP_SYS_ADMIN))
964 return -EPERM;
965
966 if (XFS_FORCED_SHUTDOWN(mp))
967 return -EIO;
968
969 if (copy_from_user(&hdr, &arg->hdr, sizeof(hdr)))
970 return -EFAULT;
971
972 error = xfs_bulk_ireq_setup(mp, &hdr, &breq, arg->inumbers);
Darrick J. Wonge7ee96d2019-08-28 14:37:57 -0700973 if (error == -ECANCELED)
Darrick J. Wongfba97602019-07-03 20:36:28 -0700974 goto out_teardown;
975 if (error < 0)
976 return error;
977
978 error = xfs_inumbers(&breq, xfs_inumbers_fmt);
979 if (error)
980 return error;
981
982out_teardown:
983 xfs_bulk_ireq_teardown(&hdr, &breq);
984 if (copy_to_user(&arg->hdr, &hdr, sizeof(hdr)))
985 return -EFAULT;
986
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 return 0;
988}
989
990STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991xfs_ioc_fsgeometry(
Dave Chinner1b6d9682019-04-12 07:41:16 -0700992 struct xfs_mount *mp,
993 void __user *arg,
994 int struct_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995{
Dave Chinner1b6d9682019-04-12 07:41:16 -0700996 struct xfs_fsop_geom fsgeo;
997 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
Eric Sandeen91083262019-05-01 20:26:30 -0700999 xfs_fs_geometry(&mp->m_sb, &fsgeo, struct_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
Dave Chinner1b6d9682019-04-12 07:41:16 -07001001 if (struct_version <= 3)
1002 len = sizeof(struct xfs_fsop_geom_v1);
1003 else if (struct_version == 4)
1004 len = sizeof(struct xfs_fsop_geom_v4);
Darrick J. Wongc23232d2019-04-12 07:41:17 -07001005 else {
1006 xfs_fsop_geom_health(mp, &fsgeo);
Dave Chinner1b6d9682019-04-12 07:41:16 -07001007 len = sizeof(fsgeo);
Darrick J. Wongc23232d2019-04-12 07:41:17 -07001008 }
Dave Chinner1b6d9682019-04-12 07:41:16 -07001009
1010 if (copy_to_user(arg, &fsgeo, len))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10001011 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 return 0;
1013}
1014
Darrick J. Wong7cd50062019-04-12 07:41:17 -07001015STATIC int
1016xfs_ioc_ag_geometry(
1017 struct xfs_mount *mp,
1018 void __user *arg)
1019{
1020 struct xfs_ag_geometry ageo;
1021 int error;
1022
1023 if (copy_from_user(&ageo, arg, sizeof(ageo)))
1024 return -EFAULT;
Darrick J. Wong76f17932019-08-30 16:30:22 -07001025 if (ageo.ag_flags)
1026 return -EINVAL;
1027 if (memchr_inv(&ageo.ag_reserved, 0, sizeof(ageo.ag_reserved)))
1028 return -EINVAL;
Darrick J. Wong7cd50062019-04-12 07:41:17 -07001029
1030 error = xfs_ag_get_geometry(mp, ageo.ag_number, &ageo);
1031 if (error)
1032 return error;
1033
1034 if (copy_to_user(arg, &ageo, sizeof(ageo)))
1035 return -EFAULT;
1036 return 0;
1037}
1038
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039/*
1040 * Linux extended inode flags interface.
1041 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
1043STATIC unsigned int
1044xfs_merge_ioc_xflags(
1045 unsigned int flags,
1046 unsigned int start)
1047{
1048 unsigned int xflags = start;
1049
Eric Sandeen39058a02007-02-10 18:37:10 +11001050 if (flags & FS_IMMUTABLE_FL)
Dave Chinnere7b89482016-01-04 16:44:15 +11001051 xflags |= FS_XFLAG_IMMUTABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 else
Dave Chinnere7b89482016-01-04 16:44:15 +11001053 xflags &= ~FS_XFLAG_IMMUTABLE;
Eric Sandeen39058a02007-02-10 18:37:10 +11001054 if (flags & FS_APPEND_FL)
Dave Chinnere7b89482016-01-04 16:44:15 +11001055 xflags |= FS_XFLAG_APPEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 else
Dave Chinnere7b89482016-01-04 16:44:15 +11001057 xflags &= ~FS_XFLAG_APPEND;
Eric Sandeen39058a02007-02-10 18:37:10 +11001058 if (flags & FS_SYNC_FL)
Dave Chinnere7b89482016-01-04 16:44:15 +11001059 xflags |= FS_XFLAG_SYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 else
Dave Chinnere7b89482016-01-04 16:44:15 +11001061 xflags &= ~FS_XFLAG_SYNC;
Eric Sandeen39058a02007-02-10 18:37:10 +11001062 if (flags & FS_NOATIME_FL)
Dave Chinnere7b89482016-01-04 16:44:15 +11001063 xflags |= FS_XFLAG_NOATIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 else
Dave Chinnere7b89482016-01-04 16:44:15 +11001065 xflags &= ~FS_XFLAG_NOATIME;
Eric Sandeen39058a02007-02-10 18:37:10 +11001066 if (flags & FS_NODUMP_FL)
Dave Chinnere7b89482016-01-04 16:44:15 +11001067 xflags |= FS_XFLAG_NODUMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 else
Dave Chinnere7b89482016-01-04 16:44:15 +11001069 xflags &= ~FS_XFLAG_NODUMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
1071 return xflags;
1072}
1073
1074STATIC unsigned int
1075xfs_di2lxflags(
Darrick J. Wongc8ce5402017-06-16 11:00:05 -07001076 uint16_t di_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077{
1078 unsigned int flags = 0;
1079
1080 if (di_flags & XFS_DIFLAG_IMMUTABLE)
Eric Sandeen39058a02007-02-10 18:37:10 +11001081 flags |= FS_IMMUTABLE_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 if (di_flags & XFS_DIFLAG_APPEND)
Eric Sandeen39058a02007-02-10 18:37:10 +11001083 flags |= FS_APPEND_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 if (di_flags & XFS_DIFLAG_SYNC)
Eric Sandeen39058a02007-02-10 18:37:10 +11001085 flags |= FS_SYNC_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 if (di_flags & XFS_DIFLAG_NOATIME)
Eric Sandeen39058a02007-02-10 18:37:10 +11001087 flags |= FS_NOATIME_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 if (di_flags & XFS_DIFLAG_NODUMP)
Eric Sandeen39058a02007-02-10 18:37:10 +11001089 flags |= FS_NODUMP_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 return flags;
1091}
1092
Darrick J. Wong7b0e4922019-07-01 08:25:35 -07001093static void
1094xfs_fill_fsxattr(
1095 struct xfs_inode *ip,
1096 bool attr,
1097 struct fsxattr *fa)
1098{
1099 simple_fill_fsxattr(fa, xfs_ip2xflags(ip));
1100 fa->fsx_extsize = ip->i_d.di_extsize << ip->i_mount->m_sb.sb_blocklog;
1101 fa->fsx_cowextsize = ip->i_d.di_cowextsize <<
1102 ip->i_mount->m_sb.sb_blocklog;
Christoph Hellwigde7a8662019-11-12 08:22:54 -08001103 fa->fsx_projid = ip->i_d.di_projid;
Darrick J. Wong7b0e4922019-07-01 08:25:35 -07001104
1105 if (attr) {
1106 if (ip->i_afp) {
1107 if (ip->i_afp->if_flags & XFS_IFEXTENTS)
1108 fa->fsx_nextents = xfs_iext_count(ip->i_afp);
1109 else
1110 fa->fsx_nextents = ip->i_d.di_anextents;
1111 } else
1112 fa->fsx_nextents = 0;
1113 } else {
1114 if (ip->i_df.if_flags & XFS_IFEXTENTS)
1115 fa->fsx_nextents = xfs_iext_count(&ip->i_df);
1116 else
1117 fa->fsx_nextents = ip->i_d.di_nextents;
1118 }
1119}
1120
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121STATIC int
Christoph Hellwigc83bfab2007-10-11 17:47:00 +10001122xfs_ioc_fsgetxattr(
1123 xfs_inode_t *ip,
1124 int attr,
1125 void __user *arg)
1126{
1127 struct fsxattr fa;
1128
1129 xfs_ilock(ip, XFS_ILOCK_SHARED);
Darrick J. Wong7b0e4922019-07-01 08:25:35 -07001130 xfs_fill_fsxattr(ip, attr, &fa);
Christoph Hellwigc83bfab2007-10-11 17:47:00 +10001131 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1132
1133 if (copy_to_user(arg, &fa, sizeof(fa)))
1134 return -EFAULT;
1135 return 0;
1136}
1137
Christoph Hellwigdd606872017-09-02 08:21:20 -07001138STATIC uint16_t
1139xfs_flags2diflags(
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001140 struct xfs_inode *ip,
1141 unsigned int xflags)
1142{
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001143 /* can't set PREALLOC this way, just preserve it */
Christoph Hellwigdd606872017-09-02 08:21:20 -07001144 uint16_t di_flags =
1145 (ip->i_d.di_flags & XFS_DIFLAG_PREALLOC);
1146
Dave Chinnere7b89482016-01-04 16:44:15 +11001147 if (xflags & FS_XFLAG_IMMUTABLE)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001148 di_flags |= XFS_DIFLAG_IMMUTABLE;
Dave Chinnere7b89482016-01-04 16:44:15 +11001149 if (xflags & FS_XFLAG_APPEND)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001150 di_flags |= XFS_DIFLAG_APPEND;
Dave Chinnere7b89482016-01-04 16:44:15 +11001151 if (xflags & FS_XFLAG_SYNC)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001152 di_flags |= XFS_DIFLAG_SYNC;
Dave Chinnere7b89482016-01-04 16:44:15 +11001153 if (xflags & FS_XFLAG_NOATIME)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001154 di_flags |= XFS_DIFLAG_NOATIME;
Dave Chinnere7b89482016-01-04 16:44:15 +11001155 if (xflags & FS_XFLAG_NODUMP)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001156 di_flags |= XFS_DIFLAG_NODUMP;
Dave Chinnere7b89482016-01-04 16:44:15 +11001157 if (xflags & FS_XFLAG_NODEFRAG)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001158 di_flags |= XFS_DIFLAG_NODEFRAG;
Dave Chinnere7b89482016-01-04 16:44:15 +11001159 if (xflags & FS_XFLAG_FILESTREAM)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001160 di_flags |= XFS_DIFLAG_FILESTREAM;
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001161 if (S_ISDIR(VFS_I(ip)->i_mode)) {
Dave Chinnere7b89482016-01-04 16:44:15 +11001162 if (xflags & FS_XFLAG_RTINHERIT)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001163 di_flags |= XFS_DIFLAG_RTINHERIT;
Dave Chinnere7b89482016-01-04 16:44:15 +11001164 if (xflags & FS_XFLAG_NOSYMLINKS)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001165 di_flags |= XFS_DIFLAG_NOSYMLINKS;
Dave Chinnere7b89482016-01-04 16:44:15 +11001166 if (xflags & FS_XFLAG_EXTSZINHERIT)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001167 di_flags |= XFS_DIFLAG_EXTSZINHERIT;
Dave Chinnere7b89482016-01-04 16:44:15 +11001168 if (xflags & FS_XFLAG_PROJINHERIT)
Dave Chinner9336e3a2014-10-02 09:18:40 +10001169 di_flags |= XFS_DIFLAG_PROJINHERIT;
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001170 } else if (S_ISREG(VFS_I(ip)->i_mode)) {
Dave Chinnere7b89482016-01-04 16:44:15 +11001171 if (xflags & FS_XFLAG_REALTIME)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001172 di_flags |= XFS_DIFLAG_REALTIME;
Dave Chinnere7b89482016-01-04 16:44:15 +11001173 if (xflags & FS_XFLAG_EXTSIZE)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001174 di_flags |= XFS_DIFLAG_EXTSIZE;
1175 }
Dave Chinner58f88ca2016-01-04 16:44:15 +11001176
Christoph Hellwigdd606872017-09-02 08:21:20 -07001177 return di_flags;
1178}
Dave Chinner58f88ca2016-01-04 16:44:15 +11001179
Christoph Hellwigdd606872017-09-02 08:21:20 -07001180STATIC uint64_t
1181xfs_flags2diflags2(
1182 struct xfs_inode *ip,
1183 unsigned int xflags)
1184{
1185 uint64_t di_flags2 =
1186 (ip->i_d.di_flags2 & XFS_DIFLAG2_REFLINK);
1187
Dave Chinner58f88ca2016-01-04 16:44:15 +11001188 if (xflags & FS_XFLAG_DAX)
1189 di_flags2 |= XFS_DIFLAG2_DAX;
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001190 if (xflags & FS_XFLAG_COWEXTSIZE)
1191 di_flags2 |= XFS_DIFLAG2_COWEXTSIZE;
Dave Chinner58f88ca2016-01-04 16:44:15 +11001192
Christoph Hellwigdd606872017-09-02 08:21:20 -07001193 return di_flags2;
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001194}
1195
Christoph Hellwigf13fae22008-07-21 16:16:15 +10001196STATIC void
1197xfs_diflags_to_linux(
1198 struct xfs_inode *ip)
1199{
David Chinnere4f75292008-08-13 16:00:45 +10001200 struct inode *inode = VFS_I(ip);
Christoph Hellwigf13fae22008-07-21 16:16:15 +10001201 unsigned int xflags = xfs_ip2xflags(ip);
1202
Dave Chinnere7b89482016-01-04 16:44:15 +11001203 if (xflags & FS_XFLAG_IMMUTABLE)
Christoph Hellwigf13fae22008-07-21 16:16:15 +10001204 inode->i_flags |= S_IMMUTABLE;
1205 else
1206 inode->i_flags &= ~S_IMMUTABLE;
Dave Chinnere7b89482016-01-04 16:44:15 +11001207 if (xflags & FS_XFLAG_APPEND)
Christoph Hellwigf13fae22008-07-21 16:16:15 +10001208 inode->i_flags |= S_APPEND;
1209 else
1210 inode->i_flags &= ~S_APPEND;
Dave Chinnere7b89482016-01-04 16:44:15 +11001211 if (xflags & FS_XFLAG_SYNC)
Christoph Hellwigf13fae22008-07-21 16:16:15 +10001212 inode->i_flags |= S_SYNC;
1213 else
1214 inode->i_flags &= ~S_SYNC;
Dave Chinnere7b89482016-01-04 16:44:15 +11001215 if (xflags & FS_XFLAG_NOATIME)
Christoph Hellwigf13fae22008-07-21 16:16:15 +10001216 inode->i_flags |= S_NOATIME;
1217 else
1218 inode->i_flags &= ~S_NOATIME;
Christoph Hellwig742d8422017-08-30 09:23:01 -07001219#if 0 /* disabled until the flag switching races are sorted out */
Dave Chinner58f88ca2016-01-04 16:44:15 +11001220 if (xflags & FS_XFLAG_DAX)
1221 inode->i_flags |= S_DAX;
1222 else
1223 inode->i_flags &= ~S_DAX;
Christoph Hellwig742d8422017-08-30 09:23:01 -07001224#endif
Christoph Hellwigf13fae22008-07-21 16:16:15 +10001225}
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001226
Dave Chinner29a17c02015-02-02 10:14:25 +11001227static int
1228xfs_ioctl_setattr_xflags(
1229 struct xfs_trans *tp,
1230 struct xfs_inode *ip,
1231 struct fsxattr *fa)
1232{
1233 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwigdd606872017-09-02 08:21:20 -07001234 uint64_t di_flags2;
Dave Chinner29a17c02015-02-02 10:14:25 +11001235
1236 /* Can't change realtime flag if any extents are allocated. */
1237 if ((ip->i_d.di_nextents || ip->i_delayed_blks) &&
Dave Chinnere7b89482016-01-04 16:44:15 +11001238 XFS_IS_REALTIME_INODE(ip) != (fa->fsx_xflags & FS_XFLAG_REALTIME))
Dave Chinner29a17c02015-02-02 10:14:25 +11001239 return -EINVAL;
1240
1241 /* If realtime flag is set then must have realtime device */
Dave Chinnere7b89482016-01-04 16:44:15 +11001242 if (fa->fsx_xflags & FS_XFLAG_REALTIME) {
Dave Chinner29a17c02015-02-02 10:14:25 +11001243 if (mp->m_sb.sb_rblocks == 0 || mp->m_sb.sb_rextsize == 0 ||
1244 (ip->i_d.di_extsize % mp->m_sb.sb_rextsize))
1245 return -EINVAL;
1246 }
1247
Darrick J. Wong1987fd72016-10-10 16:49:29 +11001248 /* Clear reflink if we are actually able to set the rt flag. */
Darrick J. Wongc8e156a2016-10-03 09:11:50 -07001249 if ((fa->fsx_xflags & FS_XFLAG_REALTIME) && xfs_is_reflink_inode(ip))
Darrick J. Wong1987fd72016-10-10 16:49:29 +11001250 ip->i_d.di_flags2 &= ~XFS_DIFLAG2_REFLINK;
Darrick J. Wongc8e156a2016-10-03 09:11:50 -07001251
Darrick J. Wong4f435eb2016-10-03 09:11:50 -07001252 /* Don't allow us to set DAX mode for a reflinked file for now. */
1253 if ((fa->fsx_xflags & FS_XFLAG_DAX) && xfs_is_reflink_inode(ip))
1254 return -EINVAL;
1255
Christoph Hellwigdd606872017-09-02 08:21:20 -07001256 /* diflags2 only valid for v3 inodes. */
1257 di_flags2 = xfs_flags2diflags2(ip, fa->fsx_xflags);
1258 if (di_flags2 && ip->i_d.di_version < 3)
1259 return -EINVAL;
1260
1261 ip->i_d.di_flags = xfs_flags2diflags(ip, fa->fsx_xflags);
1262 ip->i_d.di_flags2 = di_flags2;
1263
Dave Chinner29a17c02015-02-02 10:14:25 +11001264 xfs_diflags_to_linux(ip);
1265 xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
1266 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11001267 XFS_STATS_INC(mp, xs_ig_attrchg);
Dave Chinner29a17c02015-02-02 10:14:25 +11001268 return 0;
1269}
1270
Dave Chinner8f3d17a2015-02-02 10:15:35 +11001271/*
Dave Chinner3a6a8542016-03-01 09:41:33 +11001272 * If we are changing DAX flags, we have to ensure the file is clean and any
1273 * cached objects in the address space are invalidated and removed. This
1274 * requires us to lock out other IO and page faults similar to a truncate
1275 * operation. The locks need to be held until the transaction has been committed
1276 * so that the cache invalidation is atomic with respect to the DAX flag
1277 * manipulation.
1278 */
1279static int
1280xfs_ioctl_setattr_dax_invalidate(
1281 struct xfs_inode *ip,
1282 struct fsxattr *fa,
1283 int *join_flags)
1284{
1285 struct inode *inode = VFS_I(ip);
Ross Zwisler6851a3d2017-09-18 14:46:03 -07001286 struct super_block *sb = inode->i_sb;
Dave Chinner3a6a8542016-03-01 09:41:33 +11001287 int error;
1288
1289 *join_flags = 0;
1290
1291 /*
1292 * It is only valid to set the DAX flag on regular files and
Dave Chinner64485432016-03-01 09:41:33 +11001293 * directories on filesystems where the block size is equal to the page
Darrick J. Wongaaacdd22018-05-31 15:07:47 -07001294 * size. On directories it serves as an inherited hint so we don't
1295 * have to check the device for dax support or flush pagecache.
Dave Chinner3a6a8542016-03-01 09:41:33 +11001296 */
Dave Chinner64485432016-03-01 09:41:33 +11001297 if (fa->fsx_xflags & FS_XFLAG_DAX) {
Christoph Hellwig30fa5292019-10-24 22:25:38 -07001298 struct xfs_buftarg *target = xfs_inode_buftarg(ip);
1299
1300 if (!bdev_dax_supported(target->bt_bdev, sb->s_blocksize))
Dave Chinner64485432016-03-01 09:41:33 +11001301 return -EINVAL;
1302 }
Dave Chinner3a6a8542016-03-01 09:41:33 +11001303
1304 /* If the DAX state is not changing, we have nothing to do here. */
1305 if ((fa->fsx_xflags & FS_XFLAG_DAX) && IS_DAX(inode))
1306 return 0;
1307 if (!(fa->fsx_xflags & FS_XFLAG_DAX) && !IS_DAX(inode))
1308 return 0;
1309
Darrick J. Wongaaacdd22018-05-31 15:07:47 -07001310 if (S_ISDIR(inode->i_mode))
1311 return 0;
1312
Dave Chinner3a6a8542016-03-01 09:41:33 +11001313 /* lock, flush and invalidate mapping in preparation for flag change */
1314 xfs_ilock(ip, XFS_MMAPLOCK_EXCL | XFS_IOLOCK_EXCL);
1315 error = filemap_write_and_wait(inode->i_mapping);
1316 if (error)
1317 goto out_unlock;
1318 error = invalidate_inode_pages2(inode->i_mapping);
1319 if (error)
1320 goto out_unlock;
1321
1322 *join_flags = XFS_MMAPLOCK_EXCL | XFS_IOLOCK_EXCL;
1323 return 0;
1324
1325out_unlock:
1326 xfs_iunlock(ip, XFS_MMAPLOCK_EXCL | XFS_IOLOCK_EXCL);
1327 return error;
1328
1329}
1330
1331/*
Dave Chinner8f3d17a2015-02-02 10:15:35 +11001332 * Set up the transaction structure for the setattr operation, checking that we
1333 * have permission to do so. On success, return a clean transaction and the
1334 * inode locked exclusively ready for further operation specific checks. On
1335 * failure, return an error without modifying or locking the inode.
Dave Chinner3a6a8542016-03-01 09:41:33 +11001336 *
1337 * The inode might already be IO locked on call. If this is the case, it is
1338 * indicated in @join_flags and we take full responsibility for ensuring they
1339 * are unlocked from now on. Hence if we have an error here, we still have to
1340 * unlock them. Otherwise, once they are joined to the transaction, they will
1341 * be unlocked on commit/cancel.
Dave Chinner8f3d17a2015-02-02 10:15:35 +11001342 */
1343static struct xfs_trans *
1344xfs_ioctl_setattr_get_trans(
Dave Chinner3a6a8542016-03-01 09:41:33 +11001345 struct xfs_inode *ip,
1346 int join_flags)
Dave Chinner8f3d17a2015-02-02 10:15:35 +11001347{
1348 struct xfs_mount *mp = ip->i_mount;
1349 struct xfs_trans *tp;
Dave Chinner3a6a8542016-03-01 09:41:33 +11001350 int error = -EROFS;
Dave Chinner8f3d17a2015-02-02 10:15:35 +11001351
1352 if (mp->m_flags & XFS_MOUNT_RDONLY)
Dave Chinner3a6a8542016-03-01 09:41:33 +11001353 goto out_unlock;
1354 error = -EIO;
Dave Chinner8f3d17a2015-02-02 10:15:35 +11001355 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner3a6a8542016-03-01 09:41:33 +11001356 goto out_unlock;
Dave Chinner8f3d17a2015-02-02 10:15:35 +11001357
Christoph Hellwig253f4912016-04-06 09:19:55 +10001358 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
Dave Chinner8f3d17a2015-02-02 10:15:35 +11001359 if (error)
Darrick J. Wong3de5eab2019-04-22 16:28:34 -07001360 goto out_unlock;
Dave Chinner8f3d17a2015-02-02 10:15:35 +11001361
1362 xfs_ilock(ip, XFS_ILOCK_EXCL);
Dave Chinner3a6a8542016-03-01 09:41:33 +11001363 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | join_flags);
1364 join_flags = 0;
Dave Chinner8f3d17a2015-02-02 10:15:35 +11001365
1366 /*
1367 * CAP_FOWNER overrides the following restrictions:
1368 *
1369 * The user ID of the calling process must be equal to the file owner
1370 * ID, except in cases where the CAP_FSETID capability is applicable.
1371 */
1372 if (!inode_owner_or_capable(VFS_I(ip))) {
1373 error = -EPERM;
1374 goto out_cancel;
1375 }
1376
1377 if (mp->m_flags & XFS_MOUNT_WSYNC)
1378 xfs_trans_set_sync(tp);
1379
1380 return tp;
1381
1382out_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +10001383 xfs_trans_cancel(tp);
Dave Chinner3a6a8542016-03-01 09:41:33 +11001384out_unlock:
1385 if (join_flags)
1386 xfs_iunlock(ip, join_flags);
Dave Chinner8f3d17a2015-02-02 10:15:35 +11001387 return ERR_PTR(error);
1388}
1389
Iustin Pop9b94fcc2015-02-02 10:26:26 +11001390/*
1391 * extent size hint validation is somewhat cumbersome. Rules are:
1392 *
1393 * 1. extent size hint is only valid for directories and regular files
Dave Chinnere7b89482016-01-04 16:44:15 +11001394 * 2. FS_XFLAG_EXTSIZE is only valid for regular files
1395 * 3. FS_XFLAG_EXTSZINHERIT is only valid for directories.
Iustin Pop9b94fcc2015-02-02 10:26:26 +11001396 * 4. can only be changed on regular files if no extents are allocated
1397 * 5. can be changed on directories at any time
1398 * 6. extsize hint of 0 turns off hints, clears inode flags.
1399 * 7. Extent size must be a multiple of the appropriate block size.
1400 * 8. for non-realtime files, the extent size hint must be limited
1401 * to half the AG size to avoid alignment extending the extent beyond the
1402 * limits of the AG.
Darrick J. Wong80e4e122017-10-17 21:37:42 -07001403 *
1404 * Please keep this function in sync with xfs_scrub_inode_extsize.
Iustin Pop9b94fcc2015-02-02 10:26:26 +11001405 */
kbuild test robotf92090e2015-02-05 11:13:21 +11001406static int
Dave Chinnerd4388d3c2015-02-02 10:22:20 +11001407xfs_ioctl_setattr_check_extsize(
1408 struct xfs_inode *ip,
1409 struct fsxattr *fa)
1410{
1411 struct xfs_mount *mp = ip->i_mount;
Darrick J. Wongca29be72019-07-01 08:25:36 -07001412 xfs_extlen_t size;
1413 xfs_fsblock_t extsize_fsb;
Iustin Pop9b94fcc2015-02-02 10:26:26 +11001414
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001415 if (S_ISREG(VFS_I(ip)->i_mode) && ip->i_d.di_nextents &&
Dave Chinnerd4388d3c2015-02-02 10:22:20 +11001416 ((ip->i_d.di_extsize << mp->m_sb.sb_blocklog) != fa->fsx_extsize))
1417 return -EINVAL;
1418
Darrick J. Wongca29be72019-07-01 08:25:36 -07001419 if (fa->fsx_extsize == 0)
1420 return 0;
Dave Chinnerd4388d3c2015-02-02 10:22:20 +11001421
Darrick J. Wongca29be72019-07-01 08:25:36 -07001422 extsize_fsb = XFS_B_TO_FSB(mp, fa->fsx_extsize);
1423 if (extsize_fsb > MAXEXTLEN)
1424 return -EINVAL;
1425
1426 if (XFS_IS_REALTIME_INODE(ip) ||
1427 (fa->fsx_xflags & FS_XFLAG_REALTIME)) {
1428 size = mp->m_sb.sb_rextsize << mp->m_sb.sb_blocklog;
1429 } else {
1430 size = mp->m_sb.sb_blocksize;
1431 if (extsize_fsb > mp->m_sb.sb_agblocks / 2)
Dave Chinnerd4388d3c2015-02-02 10:22:20 +11001432 return -EINVAL;
Darrick J. Wongca29be72019-07-01 08:25:36 -07001433 }
Dave Chinnerd4388d3c2015-02-02 10:22:20 +11001434
Darrick J. Wongca29be72019-07-01 08:25:36 -07001435 if (fa->fsx_extsize % size)
1436 return -EINVAL;
Iustin Pop9b94fcc2015-02-02 10:26:26 +11001437
Dave Chinnerd4388d3c2015-02-02 10:22:20 +11001438 return 0;
1439}
1440
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001441/*
1442 * CoW extent size hint validation rules are:
1443 *
1444 * 1. CoW extent size hint can only be set if reflink is enabled on the fs.
1445 * The inode does not have to have any shared blocks, but it must be a v3.
1446 * 2. FS_XFLAG_COWEXTSIZE is only valid for directories and regular files;
1447 * for a directory, the hint is propagated to new files.
1448 * 3. Can be changed on files & directories at any time.
1449 * 4. CoW extsize hint of 0 turns off hints, clears inode flags.
1450 * 5. Extent size must be a multiple of the appropriate block size.
1451 * 6. The extent size hint must be limited to half the AG size to avoid
1452 * alignment extending the extent beyond the limits of the AG.
Darrick J. Wong80e4e122017-10-17 21:37:42 -07001453 *
1454 * Please keep this function in sync with xfs_scrub_inode_cowextsize.
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001455 */
1456static int
1457xfs_ioctl_setattr_check_cowextsize(
1458 struct xfs_inode *ip,
1459 struct fsxattr *fa)
1460{
1461 struct xfs_mount *mp = ip->i_mount;
Darrick J. Wongca29be72019-07-01 08:25:36 -07001462 xfs_extlen_t size;
1463 xfs_fsblock_t cowextsize_fsb;
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001464
1465 if (!(fa->fsx_xflags & FS_XFLAG_COWEXTSIZE))
1466 return 0;
1467
1468 if (!xfs_sb_version_hasreflink(&ip->i_mount->m_sb) ||
1469 ip->i_d.di_version != 3)
1470 return -EINVAL;
1471
Darrick J. Wongca29be72019-07-01 08:25:36 -07001472 if (fa->fsx_cowextsize == 0)
1473 return 0;
1474
1475 cowextsize_fsb = XFS_B_TO_FSB(mp, fa->fsx_cowextsize);
1476 if (cowextsize_fsb > MAXEXTLEN)
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001477 return -EINVAL;
1478
Darrick J. Wongca29be72019-07-01 08:25:36 -07001479 size = mp->m_sb.sb_blocksize;
1480 if (cowextsize_fsb > mp->m_sb.sb_agblocks / 2)
1481 return -EINVAL;
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001482
Darrick J. Wongca29be72019-07-01 08:25:36 -07001483 if (fa->fsx_cowextsize % size)
1484 return -EINVAL;
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001485
1486 return 0;
1487}
1488
kbuild test robotf92090e2015-02-05 11:13:21 +11001489static int
Dave Chinner23bd0732015-02-02 10:22:53 +11001490xfs_ioctl_setattr_check_projid(
1491 struct xfs_inode *ip,
1492 struct fsxattr *fa)
1493{
1494 /* Disallow 32bit project ids if projid32bit feature is not enabled. */
Darrick J. Wongc8ce5402017-06-16 11:00:05 -07001495 if (fa->fsx_projid > (uint16_t)-1 &&
Dave Chinner23bd0732015-02-02 10:22:53 +11001496 !xfs_sb_version_hasprojid32bit(&ip->i_mount->m_sb))
1497 return -EINVAL;
Dave Chinner23bd0732015-02-02 10:22:53 +11001498 return 0;
1499}
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001500
1501STATIC int
1502xfs_ioctl_setattr(
1503 xfs_inode_t *ip,
Dave Chinnerfd179b92015-02-02 10:16:25 +11001504 struct fsxattr *fa)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001505{
Darrick J. Wong7b0e4922019-07-01 08:25:35 -07001506 struct fsxattr old_fa;
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001507 struct xfs_mount *mp = ip->i_mount;
1508 struct xfs_trans *tp;
Christoph Hellwig7d095252009-06-08 15:33:32 +02001509 struct xfs_dquot *udqp = NULL;
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001510 struct xfs_dquot *pdqp = NULL;
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001511 struct xfs_dquot *olddquot = NULL;
1512 int code;
Dave Chinner3a6a8542016-03-01 09:41:33 +11001513 int join_flags = 0;
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001514
Christoph Hellwigcca28fb2010-06-24 11:57:09 +10001515 trace_xfs_ioctl_setattr(ip);
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001516
Dave Chinner23bd0732015-02-02 10:22:53 +11001517 code = xfs_ioctl_setattr_check_projid(ip, fa);
1518 if (code)
1519 return code;
Arkadiusz Mi?kiewicz23963e542010-08-26 10:19:43 +00001520
1521 /*
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001522 * If disk quotas is on, we make sure that the dquots do exist on disk,
1523 * before we start any other transactions. Trying to do this later
1524 * is messy. We don't care to take a readlock to look at the ids
1525 * in inode here, because we can't hold it across the trans_reserve.
1526 * If the IDs do change before we take the ilock, we're covered
1527 * because the i_*dquot fields will get updated anyway.
1528 */
Dave Chinnerfd179b92015-02-02 10:16:25 +11001529 if (XFS_IS_QUOTA_ON(mp)) {
Christoph Hellwig54295152020-02-21 08:31:27 -08001530 code = xfs_qm_vop_dqalloc(ip, VFS_I(ip)->i_uid,
1531 VFS_I(ip)->i_gid, fa->fsx_projid,
1532 XFS_QMOPT_PQUOTA, &udqp, NULL, &pdqp);
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001533 if (code)
1534 return code;
1535 }
1536
Dave Chinner3a6a8542016-03-01 09:41:33 +11001537 /*
1538 * Changing DAX config may require inode locking for mapping
1539 * invalidation. These need to be held all the way to transaction commit
1540 * or cancel time, so need to be passed through to
1541 * xfs_ioctl_setattr_get_trans() so it can apply them to the join call
1542 * appropriately.
1543 */
1544 code = xfs_ioctl_setattr_dax_invalidate(ip, fa, &join_flags);
1545 if (code)
1546 goto error_free_dquots;
1547
1548 tp = xfs_ioctl_setattr_get_trans(ip, join_flags);
Dave Chinner8f3d17a2015-02-02 10:15:35 +11001549 if (IS_ERR(tp)) {
1550 code = PTR_ERR(tp);
1551 goto error_free_dquots;
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001552 }
1553
Dave Chinnerfd179b92015-02-02 10:16:25 +11001554 if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_PQUOTA_ON(mp) &&
Christoph Hellwigde7a8662019-11-12 08:22:54 -08001555 ip->i_d.di_projid != fa->fsx_projid) {
Dave Chinnerfd179b92015-02-02 10:16:25 +11001556 code = xfs_qm_vop_chown_reserve(tp, ip, udqp, NULL, pdqp,
1557 capable(CAP_FOWNER) ? XFS_QMOPT_FORCE_RES : 0);
1558 if (code) /* out of quota */
Dave Chinnerd4388d3c2015-02-02 10:22:20 +11001559 goto error_trans_cancel;
Dave Chinnerfd179b92015-02-02 10:16:25 +11001560 }
1561
Darrick J. Wong7b0e4922019-07-01 08:25:35 -07001562 xfs_fill_fsxattr(ip, false, &old_fa);
1563 code = vfs_ioc_fssetxattr_check(VFS_I(ip), &old_fa, fa);
1564 if (code)
1565 goto error_trans_cancel;
1566
Dave Chinnerd4388d3c2015-02-02 10:22:20 +11001567 code = xfs_ioctl_setattr_check_extsize(ip, fa);
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001568 if (code)
Dave Chinnerd4388d3c2015-02-02 10:22:20 +11001569 goto error_trans_cancel;
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001570
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001571 code = xfs_ioctl_setattr_check_cowextsize(ip, fa);
1572 if (code)
1573 goto error_trans_cancel;
1574
Dave Chinner29a17c02015-02-02 10:14:25 +11001575 code = xfs_ioctl_setattr_xflags(tp, ip, fa);
1576 if (code)
Dave Chinnerd4388d3c2015-02-02 10:22:20 +11001577 goto error_trans_cancel;
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001578
1579 /*
Dave Chinnerfd179b92015-02-02 10:16:25 +11001580 * Change file ownership. Must be the owner or privileged. CAP_FSETID
1581 * overrides the following restrictions:
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001582 *
Dave Chinnerfd179b92015-02-02 10:16:25 +11001583 * The set-user-ID and set-group-ID bits of a file will be cleared upon
1584 * successful return from chown()
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001585 */
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001586
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001587 if ((VFS_I(ip)->i_mode & (S_ISUID|S_ISGID)) &&
Dave Chinnerfd179b92015-02-02 10:16:25 +11001588 !capable_wrt_inode_uidgid(VFS_I(ip), CAP_FSETID))
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001589 VFS_I(ip)->i_mode &= ~(S_ISUID|S_ISGID);
Dave Chinnerfd179b92015-02-02 10:16:25 +11001590
1591 /* Change the ownerships and register project quota modifications */
Christoph Hellwigde7a8662019-11-12 08:22:54 -08001592 if (ip->i_d.di_projid != fa->fsx_projid) {
Dave Chinnerfd179b92015-02-02 10:16:25 +11001593 if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_PQUOTA_ON(mp)) {
1594 olddquot = xfs_qm_vop_chown(tp, ip,
1595 &ip->i_pdquot, pdqp);
Dwight Engenfd5e2aa2013-08-15 14:08:00 -04001596 }
Dave Chinnerfd179b92015-02-02 10:16:25 +11001597 ASSERT(ip->i_d.di_version > 1);
Christoph Hellwigde7a8662019-11-12 08:22:54 -08001598 ip->i_d.di_projid = fa->fsx_projid;
Christoph Hellwigf13fae22008-07-21 16:16:15 +10001599 }
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001600
Dave Chinnera8727032014-10-02 09:20:30 +10001601 /*
1602 * Only set the extent size hint if we've already determined that the
1603 * extent size hint should be set on the inode. If no extent size flags
1604 * are set on the inode then unconditionally clear the extent size hint.
1605 */
Dave Chinnerfd179b92015-02-02 10:16:25 +11001606 if (ip->i_d.di_flags & (XFS_DIFLAG_EXTSIZE | XFS_DIFLAG_EXTSZINHERIT))
1607 ip->i_d.di_extsize = fa->fsx_extsize >> mp->m_sb.sb_blocklog;
1608 else
1609 ip->i_d.di_extsize = 0;
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001610 if (ip->i_d.di_version == 3 &&
1611 (ip->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE))
1612 ip->i_d.di_cowextsize = fa->fsx_cowextsize >>
1613 mp->m_sb.sb_blocklog;
1614 else
1615 ip->i_d.di_cowextsize = 0;
Dave Chinnera8727032014-10-02 09:20:30 +10001616
Christoph Hellwig70393312015-06-04 13:48:08 +10001617 code = xfs_trans_commit(tp);
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001618
1619 /*
1620 * Release any dquot(s) the inode had kept before chown.
1621 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02001622 xfs_qm_dqrele(olddquot);
1623 xfs_qm_dqrele(udqp);
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001624 xfs_qm_dqrele(pdqp);
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001625
Christoph Hellwig288699f2010-06-23 18:11:15 +10001626 return code;
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001627
Dave Chinnerd4388d3c2015-02-02 10:22:20 +11001628error_trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +10001629 xfs_trans_cancel(tp);
Dave Chinner8f3d17a2015-02-02 10:15:35 +11001630error_free_dquots:
Christoph Hellwig7d095252009-06-08 15:33:32 +02001631 xfs_qm_dqrele(udqp);
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001632 xfs_qm_dqrele(pdqp);
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001633 return code;
1634}
1635
Christoph Hellwigc83bfab2007-10-11 17:47:00 +10001636STATIC int
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001637xfs_ioc_fssetxattr(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 xfs_inode_t *ip,
1639 struct file *filp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 void __user *arg)
1641{
1642 struct fsxattr fa;
Jan Karad9457dc2012-06-12 16:20:39 +02001643 int error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001644
1645 if (copy_from_user(&fa, arg, sizeof(fa)))
1646 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647
Jan Karad9457dc2012-06-12 16:20:39 +02001648 error = mnt_want_write_file(filp);
1649 if (error)
1650 return error;
Dave Chinnerfd179b92015-02-02 10:16:25 +11001651 error = xfs_ioctl_setattr(ip, &fa);
Jan Karad9457dc2012-06-12 16:20:39 +02001652 mnt_drop_write_file(filp);
Dave Chinner24513372014-06-25 14:58:08 +10001653 return error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001654}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001656STATIC int
1657xfs_ioc_getxflags(
1658 xfs_inode_t *ip,
1659 void __user *arg)
1660{
1661 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001663 flags = xfs_di2lxflags(ip->i_d.di_flags);
1664 if (copy_to_user(arg, &flags, sizeof(flags)))
1665 return -EFAULT;
1666 return 0;
1667}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001669STATIC int
1670xfs_ioc_setxflags(
Dave Chinnerf96291f2015-02-02 10:15:56 +11001671 struct xfs_inode *ip,
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001672 struct file *filp,
1673 void __user *arg)
1674{
Dave Chinnerf96291f2015-02-02 10:15:56 +11001675 struct xfs_trans *tp;
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001676 struct fsxattr fa;
Darrick J. Wong7b0e4922019-07-01 08:25:35 -07001677 struct fsxattr old_fa;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001678 unsigned int flags;
Dave Chinner3a6a8542016-03-01 09:41:33 +11001679 int join_flags = 0;
Dave Chinnerf96291f2015-02-02 10:15:56 +11001680 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001682 if (copy_from_user(&flags, arg, sizeof(flags)))
1683 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001685 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
1686 FS_NOATIME_FL | FS_NODUMP_FL | \
1687 FS_SYNC_FL))
1688 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001690 fa.fsx_xflags = xfs_merge_ioc_xflags(flags, xfs_ip2xflags(ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
Jan Karad9457dc2012-06-12 16:20:39 +02001692 error = mnt_want_write_file(filp);
1693 if (error)
1694 return error;
Dave Chinnerf96291f2015-02-02 10:15:56 +11001695
Dave Chinner3a6a8542016-03-01 09:41:33 +11001696 /*
1697 * Changing DAX config may require inode locking for mapping
1698 * invalidation. These need to be held all the way to transaction commit
1699 * or cancel time, so need to be passed through to
1700 * xfs_ioctl_setattr_get_trans() so it can apply them to the join call
1701 * appropriately.
1702 */
1703 error = xfs_ioctl_setattr_dax_invalidate(ip, &fa, &join_flags);
1704 if (error)
1705 goto out_drop_write;
1706
1707 tp = xfs_ioctl_setattr_get_trans(ip, join_flags);
Dave Chinnerf96291f2015-02-02 10:15:56 +11001708 if (IS_ERR(tp)) {
1709 error = PTR_ERR(tp);
1710 goto out_drop_write;
1711 }
1712
Darrick J. Wong7b0e4922019-07-01 08:25:35 -07001713 xfs_fill_fsxattr(ip, false, &old_fa);
1714 error = vfs_ioc_fssetxattr_check(VFS_I(ip), &old_fa, &fa);
1715 if (error) {
1716 xfs_trans_cancel(tp);
1717 goto out_drop_write;
1718 }
1719
Dave Chinnerf96291f2015-02-02 10:15:56 +11001720 error = xfs_ioctl_setattr_xflags(tp, ip, &fa);
1721 if (error) {
Christoph Hellwig4906e212015-06-04 13:47:56 +10001722 xfs_trans_cancel(tp);
Dave Chinnerf96291f2015-02-02 10:15:56 +11001723 goto out_drop_write;
1724 }
1725
Christoph Hellwig70393312015-06-04 13:48:08 +10001726 error = xfs_trans_commit(tp);
Dave Chinnerf96291f2015-02-02 10:15:56 +11001727out_drop_write:
Jan Karad9457dc2012-06-12 16:20:39 +02001728 mnt_drop_write_file(filp);
Dave Chinner24513372014-06-25 14:58:08 +10001729 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730}
1731
Christoph Hellwig232b51942017-10-17 14:16:19 -07001732static bool
1733xfs_getbmap_format(
1734 struct kgetbmap *p,
1735 struct getbmapx __user *u,
1736 size_t recsize)
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001737{
Christoph Hellwig232b51942017-10-17 14:16:19 -07001738 if (put_user(p->bmv_offset, &u->bmv_offset) ||
1739 put_user(p->bmv_block, &u->bmv_block) ||
1740 put_user(p->bmv_length, &u->bmv_length) ||
1741 put_user(0, &u->bmv_count) ||
1742 put_user(0, &u->bmv_entries))
1743 return false;
1744 if (recsize < sizeof(struct getbmapx))
1745 return true;
1746 if (put_user(0, &u->bmv_iflags) ||
1747 put_user(p->bmv_oflags, &u->bmv_oflags) ||
1748 put_user(0, &u->bmv_unused1) ||
1749 put_user(0, &u->bmv_unused2))
1750 return false;
1751 return true;
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001752}
1753
1754STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755xfs_ioc_getbmap(
Christoph Hellwig8f3e2052016-07-20 11:29:35 +10001756 struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 unsigned int cmd,
1758 void __user *arg)
1759{
Darrick J. Wongbe6324c2017-04-03 15:17:57 -07001760 struct getbmapx bmx = { 0 };
Christoph Hellwig232b51942017-10-17 14:16:19 -07001761 struct kgetbmap *buf;
1762 size_t recsize;
1763 int error, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
Christoph Hellwig232b51942017-10-17 14:16:19 -07001765 switch (cmd) {
1766 case XFS_IOC_GETBMAPA:
1767 bmx.bmv_iflags = BMV_IF_ATTRFORK;
1768 /*FALLTHRU*/
1769 case XFS_IOC_GETBMAP:
1770 if (file->f_mode & FMODE_NOCMTIME)
1771 bmx.bmv_iflags |= BMV_IF_NO_DMAPI_READ;
1772 /* struct getbmap is a strict subset of struct getbmapx. */
1773 recsize = sizeof(struct getbmap);
1774 break;
1775 case XFS_IOC_GETBMAPX:
1776 recsize = sizeof(struct getbmapx);
1777 break;
1778 default:
1779 return -EINVAL;
1780 }
1781
1782 if (copy_from_user(&bmx, arg, recsize))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10001783 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001785 if (bmx.bmv_count < 2)
Eric Sandeenb474c7a2014-06-22 15:04:54 +10001786 return -EINVAL;
Christoph Hellwig232b51942017-10-17 14:16:19 -07001787 if (bmx.bmv_count > ULONG_MAX / recsize)
1788 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
Christoph Hellwig232b51942017-10-17 14:16:19 -07001790 buf = kmem_zalloc_large(bmx.bmv_count * sizeof(*buf), 0);
1791 if (!buf)
1792 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
Christoph Hellwig232b51942017-10-17 14:16:19 -07001794 error = xfs_getbmap(XFS_I(file_inode(file)), &bmx, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 if (error)
Christoph Hellwig232b51942017-10-17 14:16:19 -07001796 goto out_free_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
Christoph Hellwig232b51942017-10-17 14:16:19 -07001798 error = -EFAULT;
1799 if (copy_to_user(arg, &bmx, recsize))
1800 goto out_free_buf;
1801 arg += recsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802
Christoph Hellwig232b51942017-10-17 14:16:19 -07001803 for (i = 0; i < bmx.bmv_entries; i++) {
1804 if (!xfs_getbmap_format(buf + i, arg, recsize))
1805 goto out_free_buf;
1806 arg += recsize;
1807 }
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001808
Christoph Hellwig232b51942017-10-17 14:16:19 -07001809 error = 0;
1810out_free_buf:
1811 kmem_free(buf);
Christophe JAILLET132bf672018-11-06 07:50:50 -08001812 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813}
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001814
Darrick J. Wonge89c0412017-03-28 14:56:37 -07001815struct getfsmap_info {
1816 struct xfs_mount *mp;
Christoph Hellwig9d17e142017-04-21 11:24:41 -07001817 struct fsmap_head __user *data;
1818 unsigned int idx;
Darrick J. Wonge89c0412017-03-28 14:56:37 -07001819 __u32 last_flags;
1820};
1821
1822STATIC int
1823xfs_getfsmap_format(struct xfs_fsmap *xfm, void *priv)
1824{
1825 struct getfsmap_info *info = priv;
1826 struct fsmap fm;
1827
1828 trace_xfs_getfsmap_mapping(info->mp, xfm);
1829
1830 info->last_flags = xfm->fmr_flags;
1831 xfs_fsmap_from_internal(&fm, xfm);
Christoph Hellwig9d17e142017-04-21 11:24:41 -07001832 if (copy_to_user(&info->data->fmh_recs[info->idx++], &fm,
1833 sizeof(struct fsmap)))
Darrick J. Wonge89c0412017-03-28 14:56:37 -07001834 return -EFAULT;
1835
Darrick J. Wonge89c0412017-03-28 14:56:37 -07001836 return 0;
1837}
1838
1839STATIC int
1840xfs_ioc_getfsmap(
1841 struct xfs_inode *ip,
Christoph Hellwig9d17e142017-04-21 11:24:41 -07001842 struct fsmap_head __user *arg)
Darrick J. Wonge89c0412017-03-28 14:56:37 -07001843{
Christoph Hellwigef2b67e2017-04-21 11:24:40 -07001844 struct getfsmap_info info = { NULL };
Darrick J. Wonge89c0412017-03-28 14:56:37 -07001845 struct xfs_fsmap_head xhead = {0};
1846 struct fsmap_head head;
1847 bool aborted = false;
1848 int error;
1849
1850 if (copy_from_user(&head, arg, sizeof(struct fsmap_head)))
1851 return -EFAULT;
1852 if (memchr_inv(head.fmh_reserved, 0, sizeof(head.fmh_reserved)) ||
1853 memchr_inv(head.fmh_keys[0].fmr_reserved, 0,
1854 sizeof(head.fmh_keys[0].fmr_reserved)) ||
1855 memchr_inv(head.fmh_keys[1].fmr_reserved, 0,
1856 sizeof(head.fmh_keys[1].fmr_reserved)))
1857 return -EINVAL;
1858
1859 xhead.fmh_iflags = head.fmh_iflags;
1860 xhead.fmh_count = head.fmh_count;
1861 xfs_fsmap_to_internal(&xhead.fmh_keys[0], &head.fmh_keys[0]);
1862 xfs_fsmap_to_internal(&xhead.fmh_keys[1], &head.fmh_keys[1]);
1863
1864 trace_xfs_getfsmap_low_key(ip->i_mount, &xhead.fmh_keys[0]);
1865 trace_xfs_getfsmap_high_key(ip->i_mount, &xhead.fmh_keys[1]);
1866
1867 info.mp = ip->i_mount;
Christoph Hellwig9d17e142017-04-21 11:24:41 -07001868 info.data = arg;
Darrick J. Wonge89c0412017-03-28 14:56:37 -07001869 error = xfs_getfsmap(ip->i_mount, &xhead, xfs_getfsmap_format, &info);
Darrick J. Wonge7ee96d2019-08-28 14:37:57 -07001870 if (error == -ECANCELED) {
Darrick J. Wonge89c0412017-03-28 14:56:37 -07001871 error = 0;
1872 aborted = true;
1873 } else if (error)
1874 return error;
1875
1876 /* If we didn't abort, set the "last" flag in the last fmx */
Darrick J. Wong12e4a382017-04-23 10:45:21 -07001877 if (!aborted && info.idx) {
Darrick J. Wonge89c0412017-03-28 14:56:37 -07001878 info.last_flags |= FMR_OF_LAST;
Christoph Hellwig9d17e142017-04-21 11:24:41 -07001879 if (copy_to_user(&info.data->fmh_recs[info.idx - 1].fmr_flags,
1880 &info.last_flags, sizeof(info.last_flags)))
Darrick J. Wonge89c0412017-03-28 14:56:37 -07001881 return -EFAULT;
1882 }
1883
1884 /* copy back header */
1885 head.fmh_entries = xhead.fmh_entries;
1886 head.fmh_oflags = xhead.fmh_oflags;
1887 if (copy_to_user(arg, &head, sizeof(struct fsmap_head)))
1888 return -EFAULT;
1889
1890 return 0;
1891}
1892
Darrick J. Wong36fd6e82017-10-17 21:37:34 -07001893STATIC int
1894xfs_ioc_scrub_metadata(
1895 struct xfs_inode *ip,
1896 void __user *arg)
1897{
1898 struct xfs_scrub_metadata scrub;
1899 int error;
1900
1901 if (!capable(CAP_SYS_ADMIN))
1902 return -EPERM;
1903
1904 if (copy_from_user(&scrub, arg, sizeof(scrub)))
1905 return -EFAULT;
1906
1907 error = xfs_scrub_metadata(ip, &scrub);
1908 if (error)
1909 return error;
1910
1911 if (copy_to_user(arg, &scrub, sizeof(scrub)))
1912 return -EFAULT;
1913
1914 return 0;
1915}
1916
Dave Chinnera133d952013-08-12 20:49:48 +10001917int
1918xfs_ioc_swapext(
1919 xfs_swapext_t *sxp)
1920{
1921 xfs_inode_t *ip, *tip;
1922 struct fd f, tmp;
1923 int error = 0;
1924
1925 /* Pull information for the target fd */
1926 f = fdget((int)sxp->sx_fdtarget);
1927 if (!f.file) {
Dave Chinner24513372014-06-25 14:58:08 +10001928 error = -EINVAL;
Dave Chinnera133d952013-08-12 20:49:48 +10001929 goto out;
1930 }
1931
1932 if (!(f.file->f_mode & FMODE_WRITE) ||
1933 !(f.file->f_mode & FMODE_READ) ||
1934 (f.file->f_flags & O_APPEND)) {
Dave Chinner24513372014-06-25 14:58:08 +10001935 error = -EBADF;
Dave Chinnera133d952013-08-12 20:49:48 +10001936 goto out_put_file;
1937 }
1938
1939 tmp = fdget((int)sxp->sx_fdtmp);
1940 if (!tmp.file) {
Dave Chinner24513372014-06-25 14:58:08 +10001941 error = -EINVAL;
Dave Chinnera133d952013-08-12 20:49:48 +10001942 goto out_put_file;
1943 }
1944
1945 if (!(tmp.file->f_mode & FMODE_WRITE) ||
1946 !(tmp.file->f_mode & FMODE_READ) ||
1947 (tmp.file->f_flags & O_APPEND)) {
Dave Chinner24513372014-06-25 14:58:08 +10001948 error = -EBADF;
Dave Chinnera133d952013-08-12 20:49:48 +10001949 goto out_put_tmp_file;
1950 }
1951
1952 if (IS_SWAPFILE(file_inode(f.file)) ||
1953 IS_SWAPFILE(file_inode(tmp.file))) {
Dave Chinner24513372014-06-25 14:58:08 +10001954 error = -EINVAL;
Dave Chinnera133d952013-08-12 20:49:48 +10001955 goto out_put_tmp_file;
1956 }
1957
Jann Horn7f1b6242016-07-20 10:30:30 +10001958 /*
1959 * We need to ensure that the fds passed in point to XFS inodes
1960 * before we cast and access them as XFS structures as we have no
1961 * control over what the user passes us here.
1962 */
1963 if (f.file->f_op != &xfs_file_operations ||
1964 tmp.file->f_op != &xfs_file_operations) {
1965 error = -EINVAL;
1966 goto out_put_tmp_file;
1967 }
1968
Dave Chinnera133d952013-08-12 20:49:48 +10001969 ip = XFS_I(file_inode(f.file));
1970 tip = XFS_I(file_inode(tmp.file));
1971
1972 if (ip->i_mount != tip->i_mount) {
Dave Chinner24513372014-06-25 14:58:08 +10001973 error = -EINVAL;
Dave Chinnera133d952013-08-12 20:49:48 +10001974 goto out_put_tmp_file;
1975 }
1976
1977 if (ip->i_ino == tip->i_ino) {
Dave Chinner24513372014-06-25 14:58:08 +10001978 error = -EINVAL;
Dave Chinnera133d952013-08-12 20:49:48 +10001979 goto out_put_tmp_file;
1980 }
1981
1982 if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
Dave Chinner24513372014-06-25 14:58:08 +10001983 error = -EIO;
Dave Chinnera133d952013-08-12 20:49:48 +10001984 goto out_put_tmp_file;
1985 }
1986
1987 error = xfs_swap_extents(ip, tip, sxp);
1988
1989 out_put_tmp_file:
1990 fdput(tmp);
1991 out_put_file:
1992 fdput(f);
1993 out:
1994 return error;
1995}
1996
Eric Sandeenf7664b32018-05-15 13:21:48 -07001997static int
1998xfs_ioc_getlabel(
1999 struct xfs_mount *mp,
2000 char __user *user_label)
2001{
2002 struct xfs_sb *sbp = &mp->m_sb;
2003 char label[XFSLABEL_MAX + 1];
2004
2005 /* Paranoia */
2006 BUILD_BUG_ON(sizeof(sbp->sb_fname) > FSLABEL_MAX);
2007
Arnd Bergmann4bb8b652018-06-05 19:42:45 -07002008 /* 1 larger than sb_fname, so this ensures a trailing NUL char */
2009 memset(label, 0, sizeof(label));
Eric Sandeenf7664b32018-05-15 13:21:48 -07002010 spin_lock(&mp->m_sb_lock);
Arnd Bergmann4bb8b652018-06-05 19:42:45 -07002011 strncpy(label, sbp->sb_fname, XFSLABEL_MAX);
Eric Sandeenf7664b32018-05-15 13:21:48 -07002012 spin_unlock(&mp->m_sb_lock);
2013
Arnd Bergmann4bb8b652018-06-05 19:42:45 -07002014 if (copy_to_user(user_label, label, sizeof(label)))
Eric Sandeenf7664b32018-05-15 13:21:48 -07002015 return -EFAULT;
2016 return 0;
2017}
2018
2019static int
2020xfs_ioc_setlabel(
2021 struct file *filp,
2022 struct xfs_mount *mp,
2023 char __user *newlabel)
2024{
2025 struct xfs_sb *sbp = &mp->m_sb;
2026 char label[XFSLABEL_MAX + 1];
2027 size_t len;
2028 int error;
2029
2030 if (!capable(CAP_SYS_ADMIN))
2031 return -EPERM;
2032 /*
2033 * The generic ioctl allows up to FSLABEL_MAX chars, but XFS is much
2034 * smaller, at 12 bytes. We copy one more to be sure we find the
2035 * (required) NULL character to test the incoming label length.
2036 * NB: The on disk label doesn't need to be null terminated.
2037 */
2038 if (copy_from_user(label, newlabel, XFSLABEL_MAX + 1))
2039 return -EFAULT;
2040 len = strnlen(label, XFSLABEL_MAX + 1);
2041 if (len > sizeof(sbp->sb_fname))
2042 return -EINVAL;
2043
2044 error = mnt_want_write_file(filp);
2045 if (error)
2046 return error;
2047
2048 spin_lock(&mp->m_sb_lock);
2049 memset(sbp->sb_fname, 0, sizeof(sbp->sb_fname));
Arnd Bergmann4bb8b652018-06-05 19:42:45 -07002050 memcpy(sbp->sb_fname, label, len);
Eric Sandeenf7664b32018-05-15 13:21:48 -07002051 spin_unlock(&mp->m_sb_lock);
2052
2053 /*
2054 * Now we do several things to satisfy userspace.
2055 * In addition to normal logging of the primary superblock, we also
2056 * immediately write these changes to sector zero for the primary, then
2057 * update all backup supers (as xfs_db does for a label change), then
2058 * invalidate the block device page cache. This is so that any prior
2059 * buffered reads from userspace (i.e. from blkid) are invalidated,
2060 * and userspace will see the newly-written label.
2061 */
2062 error = xfs_sync_sb_buf(mp);
2063 if (error)
2064 goto out;
2065 /*
2066 * growfs also updates backup supers so lock against that.
2067 */
2068 mutex_lock(&mp->m_growlock);
2069 error = xfs_update_secondary_sbs(mp);
2070 mutex_unlock(&mp->m_growlock);
2071
2072 invalidate_bdev(mp->m_ddev_targp->bt_bdev);
2073
2074out:
2075 mnt_drop_write_file(filp);
2076 return error;
2077}
2078
Christoph Hellwig4d4be482008-12-09 04:47:33 -05002079/*
2080 * Note: some of the ioctl's return positive numbers as a
2081 * byte count indicating success, such as readlink_by_handle.
2082 * So we don't "sign flip" like most other routines. This means
2083 * true errors need to be returned as a negative value.
2084 */
2085long
2086xfs_file_ioctl(
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002087 struct file *filp,
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002088 unsigned int cmd,
Christoph Hellwig4d4be482008-12-09 04:47:33 -05002089 unsigned long p)
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002090{
Al Viro496ad9a2013-01-23 17:07:38 -05002091 struct inode *inode = file_inode(filp);
Christoph Hellwig4d4be482008-12-09 04:47:33 -05002092 struct xfs_inode *ip = XFS_I(inode);
2093 struct xfs_mount *mp = ip->i_mount;
2094 void __user *arg = (void __user *)p;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002095 int error;
2096
Christoph Hellwigcca28fb2010-06-24 11:57:09 +10002097 trace_xfs_file_ioctl(ip);
Christoph Hellwig4d4be482008-12-09 04:47:33 -05002098
2099 switch (cmd) {
Christoph Hellwiga46db602011-01-07 13:02:04 +00002100 case FITRIM:
2101 return xfs_ioc_trim(mp, arg);
Eric Sandeenf7664b32018-05-15 13:21:48 -07002102 case FS_IOC_GETFSLABEL:
2103 return xfs_ioc_getlabel(mp, arg);
2104 case FS_IOC_SETFSLABEL:
2105 return xfs_ioc_setlabel(filp, mp, arg);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002106 case XFS_IOC_ALLOCSP:
2107 case XFS_IOC_FREESP:
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002108 case XFS_IOC_ALLOCSP64:
Christoph Hellwig837a6e72019-10-24 22:26:02 -07002109 case XFS_IOC_FREESP64: {
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06002110 xfs_flock64_t bf;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002111
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06002112 if (copy_from_user(&bf, arg, sizeof(bf)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002113 return -EFAULT;
Christoph Hellwig837a6e72019-10-24 22:26:02 -07002114 return xfs_ioc_space(filp, &bf);
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06002115 }
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002116 case XFS_IOC_DIOINFO: {
Christoph Hellwigc7d68312019-10-24 22:25:39 -07002117 struct xfs_buftarg *target = xfs_inode_buftarg(ip);
2118 struct dioattr da;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002119
Eric Sandeen7c71ee72014-01-21 16:46:23 -06002120 da.d_mem = da.d_miniosz = target->bt_logical_sectorsize;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002121 da.d_maxiosz = INT_MAX & ~(da.d_miniosz - 1);
2122
2123 if (copy_to_user(arg, &da, sizeof(da)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002124 return -EFAULT;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002125 return 0;
2126 }
2127
2128 case XFS_IOC_FSBULKSTAT_SINGLE:
2129 case XFS_IOC_FSBULKSTAT:
2130 case XFS_IOC_FSINUMBERS:
Darrick J. Wong8bfe9d12019-07-03 20:36:26 -07002131 return xfs_ioc_fsbulkstat(mp, cmd, arg);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002132
Darrick J. Wong0448b6f2019-07-03 20:36:27 -07002133 case XFS_IOC_BULKSTAT:
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002134 return xfs_ioc_bulkstat(mp, cmd, arg);
Darrick J. Wongfba97602019-07-03 20:36:28 -07002135 case XFS_IOC_INUMBERS:
2136 return xfs_ioc_inumbers(mp, cmd, arg);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002137
2138 case XFS_IOC_FSGEOMETRY_V1:
Dave Chinner1b6d9682019-04-12 07:41:16 -07002139 return xfs_ioc_fsgeometry(mp, arg, 3);
2140 case XFS_IOC_FSGEOMETRY_V4:
2141 return xfs_ioc_fsgeometry(mp, arg, 4);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002142 case XFS_IOC_FSGEOMETRY:
Dave Chinner1b6d9682019-04-12 07:41:16 -07002143 return xfs_ioc_fsgeometry(mp, arg, 5);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002144
Darrick J. Wong7cd50062019-04-12 07:41:17 -07002145 case XFS_IOC_AG_GEOMETRY:
2146 return xfs_ioc_ag_geometry(mp, arg);
2147
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002148 case XFS_IOC_GETVERSION:
2149 return put_user(inode->i_generation, (int __user *)arg);
2150
2151 case XFS_IOC_FSGETXATTR:
2152 return xfs_ioc_fsgetxattr(ip, 0, arg);
2153 case XFS_IOC_FSGETXATTRA:
2154 return xfs_ioc_fsgetxattr(ip, 1, arg);
Lachlan McIlroy3b2816b2008-04-18 12:43:35 +10002155 case XFS_IOC_FSSETXATTR:
Lachlan McIlroy65e67f52008-04-18 12:59:45 +10002156 return xfs_ioc_fssetxattr(ip, filp, arg);
2157 case XFS_IOC_GETXFLAGS:
2158 return xfs_ioc_getxflags(ip, arg);
2159 case XFS_IOC_SETXFLAGS:
2160 return xfs_ioc_setxflags(ip, filp, arg);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002161
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002162 case XFS_IOC_GETBMAP:
2163 case XFS_IOC_GETBMAPA:
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002164 case XFS_IOC_GETBMAPX:
Christoph Hellwig232b51942017-10-17 14:16:19 -07002165 return xfs_ioc_getbmap(filp, cmd, arg);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002166
Darrick J. Wonge89c0412017-03-28 14:56:37 -07002167 case FS_IOC_GETFSMAP:
2168 return xfs_ioc_getfsmap(ip, arg);
2169
Darrick J. Wong36fd6e82017-10-17 21:37:34 -07002170 case XFS_IOC_SCRUB_METADATA:
2171 return xfs_ioc_scrub_metadata(ip, arg);
2172
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002173 case XFS_IOC_FD_TO_HANDLE:
2174 case XFS_IOC_PATH_TO_HANDLE:
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06002175 case XFS_IOC_PATH_TO_FSHANDLE: {
2176 xfs_fsop_handlereq_t hreq;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002177
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06002178 if (copy_from_user(&hreq, arg, sizeof(hreq)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002179 return -EFAULT;
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06002180 return xfs_find_handle(cmd, &hreq);
2181 }
2182 case XFS_IOC_OPEN_BY_HANDLE: {
2183 xfs_fsop_handlereq_t hreq;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002184
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06002185 if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002186 return -EFAULT;
Christoph Hellwigd296d302009-01-19 02:02:57 +01002187 return xfs_open_by_handle(filp, &hreq);
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06002188 }
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002189
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06002190 case XFS_IOC_READLINK_BY_HANDLE: {
2191 xfs_fsop_handlereq_t hreq;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002192
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06002193 if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002194 return -EFAULT;
Christoph Hellwigd296d302009-01-19 02:02:57 +01002195 return xfs_readlink_by_handle(filp, &hreq);
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06002196 }
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002197 case XFS_IOC_ATTRLIST_BY_HANDLE:
Christoph Hellwigd296d302009-01-19 02:02:57 +01002198 return xfs_attrlist_by_handle(filp, arg);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002199
2200 case XFS_IOC_ATTRMULTI_BY_HANDLE:
Christoph Hellwigd296d302009-01-19 02:02:57 +01002201 return xfs_attrmulti_by_handle(filp, arg);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002202
2203 case XFS_IOC_SWAPEXT: {
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06002204 struct xfs_swapext sxp;
2205
2206 if (copy_from_user(&sxp, arg, sizeof(xfs_swapext_t)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002207 return -EFAULT;
Jan Karad9457dc2012-06-12 16:20:39 +02002208 error = mnt_want_write_file(filp);
2209 if (error)
2210 return error;
Dave Chinnera133d952013-08-12 20:49:48 +10002211 error = xfs_ioc_swapext(&sxp);
Jan Karad9457dc2012-06-12 16:20:39 +02002212 mnt_drop_write_file(filp);
Dave Chinner24513372014-06-25 14:58:08 +10002213 return error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002214 }
2215
2216 case XFS_IOC_FSCOUNTS: {
2217 xfs_fsop_counts_t out;
2218
Eric Sandeen91083262019-05-01 20:26:30 -07002219 xfs_fs_counts(mp, &out);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002220
2221 if (copy_to_user(arg, &out, sizeof(out)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002222 return -EFAULT;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002223 return 0;
2224 }
2225
2226 case XFS_IOC_SET_RESBLKS: {
2227 xfs_fsop_resblks_t inout;
Darrick J. Wongc8ce5402017-06-16 11:00:05 -07002228 uint64_t in;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002229
2230 if (!capable(CAP_SYS_ADMIN))
2231 return -EPERM;
2232
Eric Sandeend5db0f92010-02-05 22:59:53 +00002233 if (mp->m_flags & XFS_MOUNT_RDONLY)
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002234 return -EROFS;
Eric Sandeend5db0f92010-02-05 22:59:53 +00002235
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002236 if (copy_from_user(&inout, arg, sizeof(inout)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002237 return -EFAULT;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002238
Jan Karad9457dc2012-06-12 16:20:39 +02002239 error = mnt_want_write_file(filp);
2240 if (error)
2241 return error;
2242
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002243 /* input parameter is passed in resblks field of structure */
2244 in = inout.resblks;
2245 error = xfs_reserve_blocks(mp, &in, &inout);
Jan Karad9457dc2012-06-12 16:20:39 +02002246 mnt_drop_write_file(filp);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002247 if (error)
Dave Chinner24513372014-06-25 14:58:08 +10002248 return error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002249
2250 if (copy_to_user(arg, &inout, sizeof(inout)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002251 return -EFAULT;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002252 return 0;
2253 }
2254
2255 case XFS_IOC_GET_RESBLKS: {
2256 xfs_fsop_resblks_t out;
2257
2258 if (!capable(CAP_SYS_ADMIN))
2259 return -EPERM;
2260
2261 error = xfs_reserve_blocks(mp, NULL, &out);
2262 if (error)
Dave Chinner24513372014-06-25 14:58:08 +10002263 return error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002264
2265 if (copy_to_user(arg, &out, sizeof(out)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002266 return -EFAULT;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002267
2268 return 0;
2269 }
2270
2271 case XFS_IOC_FSGROWFSDATA: {
2272 xfs_growfs_data_t in;
2273
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002274 if (copy_from_user(&in, arg, sizeof(in)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002275 return -EFAULT;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002276
Jan Karad9457dc2012-06-12 16:20:39 +02002277 error = mnt_want_write_file(filp);
2278 if (error)
2279 return error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002280 error = xfs_growfs_data(mp, &in);
Jan Karad9457dc2012-06-12 16:20:39 +02002281 mnt_drop_write_file(filp);
Dave Chinner24513372014-06-25 14:58:08 +10002282 return error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002283 }
2284
2285 case XFS_IOC_FSGROWFSLOG: {
2286 xfs_growfs_log_t in;
2287
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002288 if (copy_from_user(&in, arg, sizeof(in)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002289 return -EFAULT;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002290
Jan Karad9457dc2012-06-12 16:20:39 +02002291 error = mnt_want_write_file(filp);
2292 if (error)
2293 return error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002294 error = xfs_growfs_log(mp, &in);
Jan Karad9457dc2012-06-12 16:20:39 +02002295 mnt_drop_write_file(filp);
Dave Chinner24513372014-06-25 14:58:08 +10002296 return error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002297 }
2298
2299 case XFS_IOC_FSGROWFSRT: {
2300 xfs_growfs_rt_t in;
2301
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002302 if (copy_from_user(&in, arg, sizeof(in)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002303 return -EFAULT;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002304
Jan Karad9457dc2012-06-12 16:20:39 +02002305 error = mnt_want_write_file(filp);
2306 if (error)
2307 return error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002308 error = xfs_growfs_rt(mp, &in);
Jan Karad9457dc2012-06-12 16:20:39 +02002309 mnt_drop_write_file(filp);
Dave Chinner24513372014-06-25 14:58:08 +10002310 return error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002311 }
2312
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002313 case XFS_IOC_GOINGDOWN: {
Darrick J. Wongc8ce5402017-06-16 11:00:05 -07002314 uint32_t in;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002315
2316 if (!capable(CAP_SYS_ADMIN))
2317 return -EPERM;
2318
Darrick J. Wongc8ce5402017-06-16 11:00:05 -07002319 if (get_user(in, (uint32_t __user *)arg))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002320 return -EFAULT;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002321
Dave Chinner24513372014-06-25 14:58:08 +10002322 return xfs_fs_goingdown(mp, in);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002323 }
2324
2325 case XFS_IOC_ERROR_INJECTION: {
2326 xfs_error_injection_t in;
2327
2328 if (!capable(CAP_SYS_ADMIN))
2329 return -EPERM;
2330
2331 if (copy_from_user(&in, arg, sizeof(in)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002332 return -EFAULT;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002333
Darrick J. Wong31965ef2017-06-20 17:54:46 -07002334 return xfs_errortag_add(mp, in.errtag);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002335 }
2336
2337 case XFS_IOC_ERROR_CLEARALL:
2338 if (!capable(CAP_SYS_ADMIN))
2339 return -EPERM;
2340
Darrick J. Wong31965ef2017-06-20 17:54:46 -07002341 return xfs_errortag_clearall(mp);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002342
Brian Foster8ca149d2012-11-07 12:21:12 -05002343 case XFS_IOC_FREE_EOFBLOCKS: {
Dwight Engenb9fe5052013-08-15 14:08:02 -04002344 struct xfs_fs_eofblocks eofb;
2345 struct xfs_eofblocks keofb;
Brian Foster8ca149d2012-11-07 12:21:12 -05002346
Dwight Engen8c567a72013-08-15 14:08:03 -04002347 if (!capable(CAP_SYS_ADMIN))
2348 return -EPERM;
2349
2350 if (mp->m_flags & XFS_MOUNT_RDONLY)
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002351 return -EROFS;
Dwight Engen8c567a72013-08-15 14:08:03 -04002352
Brian Foster8ca149d2012-11-07 12:21:12 -05002353 if (copy_from_user(&eofb, arg, sizeof(eofb)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002354 return -EFAULT;
Brian Foster8ca149d2012-11-07 12:21:12 -05002355
Dwight Engenb9fe5052013-08-15 14:08:02 -04002356 error = xfs_fs_eofblocks_from_user(&eofb, &keofb);
2357 if (error)
Dave Chinner24513372014-06-25 14:58:08 +10002358 return error;
Brian Foster8ca149d2012-11-07 12:21:12 -05002359
Dave Chinner24513372014-06-25 14:58:08 +10002360 return xfs_icache_free_eofblocks(mp, &keofb);
Brian Foster8ca149d2012-11-07 12:21:12 -05002361 }
2362
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002363 default:
2364 return -ENOTTY;
2365 }
2366}