blob: 21d6f433c37549bc53e5ef364bfa4f0d1ab23880 [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"
Hannes Eder7bf446f2009-03-05 15:20:25 +010014#include "xfs_ioctl.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110015#include "xfs_alloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include "xfs_rtalloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include "xfs_itable.h"
Nathan Scotta844f452005-11-02 14:38:42 +110018#include "xfs_error.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include "xfs_attr.h"
Nathan Scotta844f452005-11-02 14:38:42 +110020#include "xfs_bmap.h"
Dave Chinner68988112013-08-12 20:49:42 +100021#include "xfs_bmap_util.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_fsops.h"
Christoph Hellwiga46db602011-01-07 13:02:04 +000023#include "xfs_discard.h"
Christoph Hellwig25fe55e2008-07-18 17:13:20 +100024#include "xfs_quota.h"
Christoph Hellwigd296d302009-01-19 02:02:57 +010025#include "xfs_export.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000026#include "xfs_trace.h"
Brian Foster8ca149d2012-11-07 12:21:12 -050027#include "xfs_icache.h"
Dave Chinnerc24b5df2013-08-12 20:49:45 +100028#include "xfs_symlink.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110029#include "xfs_trans.h"
Andreas Gruenbacher47e1bf62015-11-03 12:56:17 +110030#include "xfs_acl.h"
Darrick J. Wonge89c0412017-03-28 14:56:37 -070031#include "xfs_btree.h"
32#include <linux/fsmap.h>
33#include "xfs_fsmap.h"
Darrick J. Wong36fd6e82017-10-17 21:37:34 -070034#include "scrub/xfs_scrub.h"
Darrick J. Wongc368ebc2018-01-08 10:51:27 -080035#include "xfs_sb.h"
Darrick J. Wong7cd50062019-04-12 07:41:17 -070036#include "xfs_ag.h"
Darrick J. Wongc23232d2019-04-12 07:41:17 -070037#include "xfs_health.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080039#include <linux/capability.h>
Ingo Molnar5b825c32017-02-02 17:54:15 +010040#include <linux/cred.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/dcache.h>
42#include <linux/mount.h>
43#include <linux/namei.h>
44#include <linux/pagemap.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090045#include <linux/slab.h>
Christoph Hellwigd296d302009-01-19 02:02:57 +010046#include <linux/exportfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48/*
49 * xfs_find_handle maps from userspace xfs_fsop_handlereq structure to
50 * a file or fs handle.
51 *
52 * XFS_IOC_PATH_TO_FSHANDLE
53 * returns fs handle for a mount point or path within that mount point
54 * XFS_IOC_FD_TO_HANDLE
55 * returns full handle for a FD opened in user space
56 * XFS_IOC_PATH_TO_HANDLE
57 * returns full handle for a path
58 */
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -060059int
Linus Torvalds1da177e2005-04-16 15:20:36 -070060xfs_find_handle(
61 unsigned int cmd,
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -060062 xfs_fsop_handlereq_t *hreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
64 int hsize;
65 xfs_handle_t handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 struct inode *inode;
Dave Chinnera30b0362013-09-02 20:49:36 +100067 struct fd f = {NULL};
Christoph Hellwig4346cdd2009-02-08 21:51:14 +010068 struct path path;
Al Viro2903ff02012-08-28 12:52:22 -040069 int error;
Christoph Hellwig4346cdd2009-02-08 21:51:14 +010070 struct xfs_inode *ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Christoph Hellwig4346cdd2009-02-08 21:51:14 +010072 if (cmd == XFS_IOC_FD_TO_HANDLE) {
Al Viro2903ff02012-08-28 12:52:22 -040073 f = fdget(hreq->fd);
74 if (!f.file)
Christoph Hellwig4346cdd2009-02-08 21:51:14 +010075 return -EBADF;
Al Viro496ad9a2013-01-23 17:07:38 -050076 inode = file_inode(f.file);
Christoph Hellwig4346cdd2009-02-08 21:51:14 +010077 } else {
78 error = user_lpath((const char __user *)hreq->path, &path);
79 if (error)
80 return error;
David Howells2b0143b2015-03-17 22:25:59 +000081 inode = d_inode(path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 }
Christoph Hellwig4346cdd2009-02-08 21:51:14 +010083 ip = XFS_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Christoph Hellwig4346cdd2009-02-08 21:51:14 +010085 /*
86 * We can only generate handles for inodes residing on a XFS filesystem,
87 * and only for regular files, directories or symbolic links.
88 */
89 error = -EINVAL;
90 if (inode->i_sb->s_magic != XFS_SB_MAGIC)
91 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Christoph Hellwig4346cdd2009-02-08 21:51:14 +010093 error = -EBADF;
94 if (!S_ISREG(inode->i_mode) &&
95 !S_ISDIR(inode->i_mode) &&
96 !S_ISLNK(inode->i_mode))
97 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Christoph Hellwig4346cdd2009-02-08 21:51:14 +0100100 memcpy(&handle.ha_fsid, ip->i_mount->m_fixedfsid, sizeof(xfs_fsid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Christoph Hellwig4346cdd2009-02-08 21:51:14 +0100102 if (cmd == XFS_IOC_PATH_TO_FSHANDLE) {
103 /*
104 * This handle only contains an fsid, zero the rest.
105 */
106 memset(&handle.ha_fid, 0, sizeof(handle.ha_fid));
107 hsize = sizeof(xfs_fsid_t);
108 } else {
Christoph Hellwigc6143912007-09-14 15:22:37 +1000109 handle.ha_fid.fid_len = sizeof(xfs_fid_t) -
110 sizeof(handle.ha_fid.fid_len);
111 handle.ha_fid.fid_pad = 0;
Dave Chinner9e9a2672016-02-09 16:54:58 +1100112 handle.ha_fid.fid_gen = inode->i_generation;
Christoph Hellwigc6143912007-09-14 15:22:37 +1000113 handle.ha_fid.fid_ino = ip->i_ino;
Christoph Hellwig3398a402017-06-14 21:30:44 -0700114 hsize = sizeof(xfs_handle_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 }
116
Christoph Hellwig4346cdd2009-02-08 21:51:14 +0100117 error = -EFAULT;
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -0600118 if (copy_to_user(hreq->ohandle, &handle, hsize) ||
Christoph Hellwig4346cdd2009-02-08 21:51:14 +0100119 copy_to_user(hreq->ohandlen, &hsize, sizeof(__s32)))
120 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Christoph Hellwig4346cdd2009-02-08 21:51:14 +0100122 error = 0;
123
124 out_put:
125 if (cmd == XFS_IOC_FD_TO_HANDLE)
Al Viro2903ff02012-08-28 12:52:22 -0400126 fdput(f);
Christoph Hellwig4346cdd2009-02-08 21:51:14 +0100127 else
128 path_put(&path);
129 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132/*
Christoph Hellwigd296d302009-01-19 02:02:57 +0100133 * No need to do permission checks on the various pathname components
134 * as the handle operations are privileged.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 */
136STATIC int
Christoph Hellwigd296d302009-01-19 02:02:57 +0100137xfs_handle_acceptable(
138 void *context,
139 struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
Christoph Hellwigd296d302009-01-19 02:02:57 +0100141 return 1;
142}
143
144/*
145 * Convert userspace handle data into a dentry.
146 */
147struct dentry *
148xfs_handle_to_dentry(
149 struct file *parfilp,
150 void __user *uhandle,
151 u32 hlen)
152{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 xfs_handle_t handle;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100154 struct xfs_fid64 fid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 /*
157 * Only allow handle opens under a directory.
158 */
Al Viro496ad9a2013-01-23 17:07:38 -0500159 if (!S_ISDIR(file_inode(parfilp)->i_mode))
Christoph Hellwigd296d302009-01-19 02:02:57 +0100160 return ERR_PTR(-ENOTDIR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Christoph Hellwigd296d302009-01-19 02:02:57 +0100162 if (hlen != sizeof(xfs_handle_t))
163 return ERR_PTR(-EINVAL);
164 if (copy_from_user(&handle, uhandle, hlen))
165 return ERR_PTR(-EFAULT);
166 if (handle.ha_fid.fid_len !=
167 sizeof(handle.ha_fid) - sizeof(handle.ha_fid.fid_len))
168 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Christoph Hellwigd296d302009-01-19 02:02:57 +0100170 memset(&fid, 0, sizeof(struct fid));
171 fid.ino = handle.ha_fid.fid_ino;
172 fid.gen = handle.ha_fid.fid_gen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Christoph Hellwigd296d302009-01-19 02:02:57 +0100174 return exportfs_decode_fh(parfilp->f_path.mnt, (struct fid *)&fid, 3,
175 FILEID_INO32_GEN | XFS_FILEID_TYPE_64FLAG,
176 xfs_handle_acceptable, NULL);
177}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Christoph Hellwigd296d302009-01-19 02:02:57 +0100179STATIC struct dentry *
180xfs_handlereq_to_dentry(
181 struct file *parfilp,
182 xfs_fsop_handlereq_t *hreq)
183{
184 return xfs_handle_to_dentry(parfilp, hreq->ihandle, hreq->ihandlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185}
186
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -0600187int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188xfs_open_by_handle(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 struct file *parfilp,
Christoph Hellwigd296d302009-01-19 02:02:57 +0100190 xfs_fsop_handlereq_t *hreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
David Howells745ca242008-11-14 10:39:22 +1100192 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 int error;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100194 int fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 int permflag;
196 struct file *filp;
197 struct inode *inode;
198 struct dentry *dentry;
Dave Chinner1a1d7722012-03-22 05:15:06 +0000199 fmode_t fmode;
Al Viro765927b2012-06-26 21:58:53 +0400200 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202 if (!capable(CAP_SYS_ADMIN))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000203 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Christoph Hellwigd296d302009-01-19 02:02:57 +0100205 dentry = xfs_handlereq_to_dentry(parfilp, hreq);
206 if (IS_ERR(dentry))
207 return PTR_ERR(dentry);
David Howells2b0143b2015-03-17 22:25:59 +0000208 inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210 /* Restrict xfs_open_by_handle to directories & regular files. */
211 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode))) {
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000212 error = -EPERM;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100213 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 }
215
216#if BITS_PER_LONG != 32
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -0600217 hreq->oflags |= O_LARGEFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218#endif
Christoph Hellwigd296d302009-01-19 02:02:57 +0100219
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -0600220 permflag = hreq->oflags;
Dave Chinner1a1d7722012-03-22 05:15:06 +0000221 fmode = OPEN_FMODE(permflag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 if ((!(permflag & O_APPEND) || (permflag & O_TRUNC)) &&
Dave Chinner1a1d7722012-03-22 05:15:06 +0000223 (fmode & FMODE_WRITE) && IS_APPEND(inode)) {
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000224 error = -EPERM;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100225 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 }
227
Dave Chinner1a1d7722012-03-22 05:15:06 +0000228 if ((fmode & FMODE_WRITE) && IS_IMMUTABLE(inode)) {
Eryu Guan337684a2016-08-02 19:58:28 +0800229 error = -EPERM;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100230 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 }
232
233 /* Can't write directories. */
Dave Chinner1a1d7722012-03-22 05:15:06 +0000234 if (S_ISDIR(inode->i_mode) && (fmode & FMODE_WRITE)) {
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000235 error = -EISDIR;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100236 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 }
238
Yann Droneaud862a6292013-07-02 18:39:34 +0200239 fd = get_unused_fd_flags(0);
Christoph Hellwigd296d302009-01-19 02:02:57 +0100240 if (fd < 0) {
241 error = fd;
242 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 }
244
Al Viro765927b2012-06-26 21:58:53 +0400245 path.mnt = parfilp->f_path.mnt;
246 path.dentry = dentry;
247 filp = dentry_open(&path, hreq->oflags, cred);
248 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 if (IS_ERR(filp)) {
Christoph Hellwigd296d302009-01-19 02:02:57 +0100250 put_unused_fd(fd);
251 return PTR_ERR(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 }
Christoph Hellwig4d4be482008-12-09 04:47:33 -0500253
Al Viro03209372011-07-25 20:54:24 -0400254 if (S_ISREG(inode->i_mode)) {
Vlad Apostolov2e2e7bb2006-11-11 18:04:47 +1100255 filp->f_flags |= O_NOATIME;
Christoph Hellwig4d4be482008-12-09 04:47:33 -0500256 filp->f_mode |= FMODE_NOCMTIME;
Vlad Apostolov2e2e7bb2006-11-11 18:04:47 +1100257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Christoph Hellwigd296d302009-01-19 02:02:57 +0100259 fd_install(fd, filp);
260 return fd;
261
262 out_dput:
263 dput(dentry);
264 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265}
266
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -0600267int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268xfs_readlink_by_handle(
Christoph Hellwigd296d302009-01-19 02:02:57 +0100269 struct file *parfilp,
270 xfs_fsop_handlereq_t *hreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
Christoph Hellwigd296d302009-01-19 02:02:57 +0100272 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 __u32 olen;
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000274 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276 if (!capable(CAP_SYS_ADMIN))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000277 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Christoph Hellwigd296d302009-01-19 02:02:57 +0100279 dentry = xfs_handlereq_to_dentry(parfilp, hreq);
280 if (IS_ERR(dentry))
281 return PTR_ERR(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283 /* Restrict this handle operation to symlinks only. */
Miklos Szeredifd4a0edf2016-12-09 16:45:04 +0100284 if (!d_is_symlink(dentry)) {
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000285 error = -EINVAL;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100286 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 }
288
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -0600289 if (copy_from_user(&olen, hreq->ohandlen, sizeof(__u32))) {
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000290 error = -EFAULT;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100291 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Miklos Szeredifd4a0edf2016-12-09 16:45:04 +0100294 error = vfs_readlink(dentry, hreq->ohandle, olen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Christoph Hellwigd296d302009-01-19 02:02:57 +0100296 out_dput:
297 dput(dentry);
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000298 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299}
300
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000301int
302xfs_set_dmattrs(
303 xfs_inode_t *ip,
Darrick J. Wong65a79352017-11-09 09:34:28 -0800304 uint evmask,
305 uint16_t state)
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000306{
307 xfs_mount_t *mp = ip->i_mount;
308 xfs_trans_t *tp;
309 int error;
310
311 if (!capable(CAP_SYS_ADMIN))
Dave Chinner24513372014-06-25 14:58:08 +1000312 return -EPERM;
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000313
314 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +1000315 return -EIO;
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000316
Christoph Hellwig253f4912016-04-06 09:19:55 +1000317 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
318 if (error)
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000319 return error;
Christoph Hellwig253f4912016-04-06 09:19:55 +1000320
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000321 xfs_ilock(ip, XFS_ILOCK_EXCL);
322 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
323
324 ip->i_d.di_dmevmask = evmask;
325 ip->i_d.di_dmstate = state;
326
327 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Christoph Hellwig70393312015-06-04 13:48:08 +1000328 error = xfs_trans_commit(tp);
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000329
330 return error;
331}
332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333STATIC int
334xfs_fssetdm_by_handle(
Christoph Hellwigd296d302009-01-19 02:02:57 +0100335 struct file *parfilp,
336 void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
338 int error;
339 struct fsdmidata fsd;
340 xfs_fsop_setdm_handlereq_t dmhreq;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100341 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
343 if (!capable(CAP_MKNOD))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000344 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 if (copy_from_user(&dmhreq, arg, sizeof(xfs_fsop_setdm_handlereq_t)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000346 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Jan Karad9457dc2012-06-12 16:20:39 +0200348 error = mnt_want_write_file(parfilp);
349 if (error)
350 return error;
351
Christoph Hellwigd296d302009-01-19 02:02:57 +0100352 dentry = xfs_handlereq_to_dentry(parfilp, &dmhreq.hreq);
Jan Karad9457dc2012-06-12 16:20:39 +0200353 if (IS_ERR(dentry)) {
354 mnt_drop_write_file(parfilp);
Christoph Hellwigd296d302009-01-19 02:02:57 +0100355 return PTR_ERR(dentry);
Jan Karad9457dc2012-06-12 16:20:39 +0200356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
David Howells2b0143b2015-03-17 22:25:59 +0000358 if (IS_IMMUTABLE(d_inode(dentry)) || IS_APPEND(d_inode(dentry))) {
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000359 error = -EPERM;
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000360 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 }
362
363 if (copy_from_user(&fsd, dmhreq.data, sizeof(fsd))) {
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000364 error = -EFAULT;
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000365 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 }
367
David Howells2b0143b2015-03-17 22:25:59 +0000368 error = xfs_set_dmattrs(XFS_I(d_inode(dentry)), fsd.fsd_dmevmask,
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000369 fsd.fsd_dmstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000371 out:
Jan Karad9457dc2012-06-12 16:20:39 +0200372 mnt_drop_write_file(parfilp);
Christoph Hellwigd296d302009-01-19 02:02:57 +0100373 dput(dentry);
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000374 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375}
376
377STATIC int
378xfs_attrlist_by_handle(
Christoph Hellwigd296d302009-01-19 02:02:57 +0100379 struct file *parfilp,
380 void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
Christoph Hellwigd296d302009-01-19 02:02:57 +0100382 int error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 attrlist_cursor_kern_t *cursor;
Darrick J. Wong0facef72016-08-03 10:58:53 +1000384 struct xfs_fsop_attrlist_handlereq __user *p = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 xfs_fsop_attrlist_handlereq_t al_hreq;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100386 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 char *kbuf;
388
389 if (!capable(CAP_SYS_ADMIN))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000390 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 if (copy_from_user(&al_hreq, arg, sizeof(xfs_fsop_attrlist_handlereq_t)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000392 return -EFAULT;
Dan Carpenter071c5292013-10-31 21:00:10 +0300393 if (al_hreq.buflen < sizeof(struct attrlist) ||
Jan Tulak4e247612015-10-12 16:02:56 +1100394 al_hreq.buflen > XFS_XATTR_LIST_MAX)
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000395 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Christoph Hellwig90ad58a2008-06-27 13:32:19 +1000397 /*
398 * Reject flags, only allow namespaces.
399 */
400 if (al_hreq.flags & ~(ATTR_ROOT | ATTR_SECURE))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000401 return -EINVAL;
Christoph Hellwig90ad58a2008-06-27 13:32:19 +1000402
Christoph Hellwigd296d302009-01-19 02:02:57 +0100403 dentry = xfs_handlereq_to_dentry(parfilp, &al_hreq.hreq);
404 if (IS_ERR(dentry))
405 return PTR_ERR(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Dave Chinnerfdd3cce2013-09-02 20:53:00 +1000407 kbuf = kmem_zalloc_large(al_hreq.buflen, KM_SLEEP);
408 if (!kbuf)
409 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411 cursor = (attrlist_cursor_kern_t *)&al_hreq.pos;
David Howells2b0143b2015-03-17 22:25:59 +0000412 error = xfs_attr_list(XFS_I(d_inode(dentry)), kbuf, al_hreq.buflen,
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000413 al_hreq.flags, cursor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 if (error)
415 goto out_kfree;
416
Darrick J. Wong0facef72016-08-03 10:58:53 +1000417 if (copy_to_user(&p->pos, cursor, sizeof(attrlist_cursor_kern_t))) {
418 error = -EFAULT;
419 goto out_kfree;
420 }
421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 if (copy_to_user(al_hreq.buffer, kbuf, al_hreq.buflen))
423 error = -EFAULT;
424
Dave Chinnerfdd3cce2013-09-02 20:53:00 +1000425out_kfree:
426 kmem_free(kbuf);
427out_dput:
Christoph Hellwigd296d302009-01-19 02:02:57 +0100428 dput(dentry);
429 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430}
431
sandeen@sandeen.net28750972008-11-25 21:20:15 -0600432int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433xfs_attrmulti_attr_get(
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000434 struct inode *inode,
Dave Chinnera9273ca2010-01-20 10:47:48 +1100435 unsigned char *name,
436 unsigned char __user *ubuf,
Darrick J. Wongc8ce5402017-06-16 11:00:05 -0700437 uint32_t *len,
438 uint32_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
Dave Chinnera9273ca2010-01-20 10:47:48 +1100440 unsigned char *kbuf;
Dave Chinner24513372014-06-25 14:58:08 +1000441 int error = -EFAULT;
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000442
Jan Tulak51fcbfe2015-10-12 16:03:59 +1100443 if (*len > XFS_XATTR_SIZE_MAX)
Dave Chinner24513372014-06-25 14:58:08 +1000444 return -EINVAL;
Dave Chinnerfdd3cce2013-09-02 20:53:00 +1000445 kbuf = kmem_zalloc_large(*len, KM_SLEEP);
446 if (!kbuf)
Dave Chinner24513372014-06-25 14:58:08 +1000447 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000449 error = xfs_attr_get(XFS_I(inode), name, kbuf, (int *)len, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 if (error)
451 goto out_kfree;
452
453 if (copy_to_user(ubuf, kbuf, *len))
Dave Chinner24513372014-06-25 14:58:08 +1000454 error = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Dave Chinnerfdd3cce2013-09-02 20:53:00 +1000456out_kfree:
457 kmem_free(kbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 return error;
459}
460
sandeen@sandeen.net28750972008-11-25 21:20:15 -0600461int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462xfs_attrmulti_attr_set(
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000463 struct inode *inode,
Dave Chinnera9273ca2010-01-20 10:47:48 +1100464 unsigned char *name,
465 const unsigned char __user *ubuf,
Darrick J. Wongc8ce5402017-06-16 11:00:05 -0700466 uint32_t len,
467 uint32_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
Dave Chinnera9273ca2010-01-20 10:47:48 +1100469 unsigned char *kbuf;
Andreas Gruenbacher09cb22d2015-11-03 12:53:54 +1100470 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000472 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
Dave Chinner24513372014-06-25 14:58:08 +1000473 return -EPERM;
Jan Tulak51fcbfe2015-10-12 16:03:59 +1100474 if (len > XFS_XATTR_SIZE_MAX)
Dave Chinner24513372014-06-25 14:58:08 +1000475 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Li Zefan0e639bd2009-04-08 15:08:04 +0800477 kbuf = memdup_user(ubuf, len);
478 if (IS_ERR(kbuf))
479 return PTR_ERR(kbuf);
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000480
Andreas Gruenbacher09cb22d2015-11-03 12:53:54 +1100481 error = xfs_attr_set(XFS_I(inode), name, kbuf, len, flags);
Andreas Gruenbacher47e1bf62015-11-03 12:56:17 +1100482 if (!error)
483 xfs_forget_acl(inode, name, flags);
Andreas Gruenbacher09cb22d2015-11-03 12:53:54 +1100484 kfree(kbuf);
485 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486}
487
sandeen@sandeen.net28750972008-11-25 21:20:15 -0600488int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489xfs_attrmulti_attr_remove(
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000490 struct inode *inode,
Dave Chinnera9273ca2010-01-20 10:47:48 +1100491 unsigned char *name,
Darrick J. Wongc8ce5402017-06-16 11:00:05 -0700492 uint32_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
Andreas Gruenbacher47e1bf62015-11-03 12:56:17 +1100494 int error;
495
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000496 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
Dave Chinner24513372014-06-25 14:58:08 +1000497 return -EPERM;
Andreas Gruenbacher47e1bf62015-11-03 12:56:17 +1100498 error = xfs_attr_remove(XFS_I(inode), name, flags);
499 if (!error)
500 xfs_forget_acl(inode, name, flags);
501 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502}
503
504STATIC int
505xfs_attrmulti_by_handle(
Dave Hansen42a74f22008-02-15 14:37:46 -0800506 struct file *parfilp,
Christoph Hellwigd296d302009-01-19 02:02:57 +0100507 void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
509 int error;
510 xfs_attr_multiop_t *ops;
511 xfs_fsop_attrmulti_handlereq_t am_hreq;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100512 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 unsigned int i, size;
Dave Chinnera9273ca2010-01-20 10:47:48 +1100514 unsigned char *attr_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
516 if (!capable(CAP_SYS_ADMIN))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000517 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 if (copy_from_user(&am_hreq, arg, sizeof(xfs_fsop_attrmulti_handlereq_t)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000519 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Zhitong Wangfda168c2010-03-23 09:51:22 +1100521 /* overflow check */
522 if (am_hreq.opcount >= INT_MAX / sizeof(xfs_attr_multiop_t))
523 return -E2BIG;
524
Christoph Hellwigd296d302009-01-19 02:02:57 +0100525 dentry = xfs_handlereq_to_dentry(parfilp, &am_hreq.hreq);
526 if (IS_ERR(dentry))
527 return PTR_ERR(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Dave Chinner24513372014-06-25 14:58:08 +1000529 error = -E2BIG;
Christoph Hellwige182f572008-06-27 13:32:31 +1000530 size = am_hreq.opcount * sizeof(xfs_attr_multiop_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 if (!size || size > 16 * PAGE_SIZE)
Christoph Hellwigd296d302009-01-19 02:02:57 +0100532 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Li Zefan0e639bd2009-04-08 15:08:04 +0800534 ops = memdup_user(am_hreq.ops, size);
535 if (IS_ERR(ops)) {
Dave Chinner24513372014-06-25 14:58:08 +1000536 error = PTR_ERR(ops);
Christoph Hellwigd296d302009-01-19 02:02:57 +0100537 goto out_dput;
Li Zefan0e639bd2009-04-08 15:08:04 +0800538 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Dave Chinner24513372014-06-25 14:58:08 +1000540 error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 attr_name = kmalloc(MAXNAMELEN, GFP_KERNEL);
542 if (!attr_name)
543 goto out_kfree_ops;
544
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 error = 0;
546 for (i = 0; i < am_hreq.opcount; i++) {
Dave Chinnera9273ca2010-01-20 10:47:48 +1100547 ops[i].am_error = strncpy_from_user((char *)attr_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 ops[i].am_attrname, MAXNAMELEN);
549 if (ops[i].am_error == 0 || ops[i].am_error == MAXNAMELEN)
Dave Chinner24513372014-06-25 14:58:08 +1000550 error = -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 if (ops[i].am_error < 0)
552 break;
553
554 switch (ops[i].am_opcode) {
555 case ATTR_OP_GET:
Christoph Hellwigd296d302009-01-19 02:02:57 +0100556 ops[i].am_error = xfs_attrmulti_attr_get(
David Howells2b0143b2015-03-17 22:25:59 +0000557 d_inode(dentry), attr_name,
Christoph Hellwigd296d302009-01-19 02:02:57 +0100558 ops[i].am_attrvalue, &ops[i].am_length,
559 ops[i].am_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 break;
561 case ATTR_OP_SET:
Al Viroa561be72011-11-23 11:57:51 -0500562 ops[i].am_error = mnt_want_write_file(parfilp);
Dave Hansen42a74f22008-02-15 14:37:46 -0800563 if (ops[i].am_error)
564 break;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100565 ops[i].am_error = xfs_attrmulti_attr_set(
David Howells2b0143b2015-03-17 22:25:59 +0000566 d_inode(dentry), attr_name,
Christoph Hellwigd296d302009-01-19 02:02:57 +0100567 ops[i].am_attrvalue, ops[i].am_length,
568 ops[i].am_flags);
Al Viro2a79f172011-12-09 08:06:57 -0500569 mnt_drop_write_file(parfilp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 break;
571 case ATTR_OP_REMOVE:
Al Viroa561be72011-11-23 11:57:51 -0500572 ops[i].am_error = mnt_want_write_file(parfilp);
Dave Hansen42a74f22008-02-15 14:37:46 -0800573 if (ops[i].am_error)
574 break;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100575 ops[i].am_error = xfs_attrmulti_attr_remove(
David Howells2b0143b2015-03-17 22:25:59 +0000576 d_inode(dentry), attr_name,
Christoph Hellwigd296d302009-01-19 02:02:57 +0100577 ops[i].am_flags);
Al Viro2a79f172011-12-09 08:06:57 -0500578 mnt_drop_write_file(parfilp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 break;
580 default:
Dave Chinner24513372014-06-25 14:58:08 +1000581 ops[i].am_error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 }
583 }
584
585 if (copy_to_user(am_hreq.ops, ops, size))
Dave Chinner24513372014-06-25 14:58:08 +1000586 error = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588 kfree(attr_name);
589 out_kfree_ops:
590 kfree(ops);
Christoph Hellwigd296d302009-01-19 02:02:57 +0100591 out_dput:
592 dput(dentry);
Dave Chinner24513372014-06-25 14:58:08 +1000593 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594}
595
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -0600596int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597xfs_ioc_space(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 struct file *filp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 unsigned int cmd,
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -0600600 xfs_flock64_t *bf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
Christoph Hellwig8f3e2052016-07-20 11:29:35 +1000602 struct inode *inode = file_inode(filp);
603 struct xfs_inode *ip = XFS_I(inode);
Christoph Hellwig865e9442013-10-12 00:55:08 -0700604 struct iattr iattr;
Christoph Hellwig8add71c2015-02-02 09:53:56 +1100605 enum xfs_prealloc_flags flags = 0;
Dan Williamsc63a8ea2018-03-12 14:12:29 -0700606 uint iolock = XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 int error;
608
Alexey Dobriyanf37ea142006-09-28 10:52:04 +1000609 if (inode->i_flags & (S_IMMUTABLE|S_APPEND))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000610 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Eric Sandeenad4a8ac2005-09-02 16:41:16 +1000612 if (!(filp->f_mode & FMODE_WRITE))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000613 return -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Alexey Dobriyanf37ea142006-09-28 10:52:04 +1000615 if (!S_ISREG(inode->i_mode))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000616 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Christoph Hellwig8add71c2015-02-02 09:53:56 +1100618 if (filp->f_flags & O_DSYNC)
619 flags |= XFS_PREALLOC_SYNC;
Christoph Hellwig8f3e2052016-07-20 11:29:35 +1000620 if (filp->f_mode & FMODE_NOCMTIME)
Christoph Hellwig8add71c2015-02-02 09:53:56 +1100621 flags |= XFS_PREALLOC_INVISIBLE;
622
Jan Karad9457dc2012-06-12 16:20:39 +0200623 error = mnt_want_write_file(filp);
624 if (error)
625 return error;
Christoph Hellwig865e9442013-10-12 00:55:08 -0700626
Christoph Hellwig781355c2015-02-16 11:59:50 +1100627 xfs_ilock(ip, iolock);
Dan Williams69eb5fa2018-03-20 14:42:38 -0700628 error = xfs_break_layouts(inode, &iolock, BREAK_UNMAP);
Christoph Hellwig781355c2015-02-16 11:59:50 +1100629 if (error)
630 goto out_unlock;
Christoph Hellwig865e9442013-10-12 00:55:08 -0700631
632 switch (bf->l_whence) {
633 case 0: /*SEEK_SET*/
634 break;
635 case 1: /*SEEK_CUR*/
636 bf->l_start += filp->f_pos;
637 break;
638 case 2: /*SEEK_END*/
639 bf->l_start += XFS_ISIZE(ip);
640 break;
641 default:
Dave Chinner24513372014-06-25 14:58:08 +1000642 error = -EINVAL;
Christoph Hellwig865e9442013-10-12 00:55:08 -0700643 goto out_unlock;
644 }
645
646 /*
647 * length of <= 0 for resv/unresv/zero is invalid. length for
648 * alloc/free is ignored completely and we have no idea what userspace
649 * might have set it to, so set it to zero to allow range
650 * checks to pass.
651 */
652 switch (cmd) {
653 case XFS_IOC_ZERO_RANGE:
654 case XFS_IOC_RESVSP:
655 case XFS_IOC_RESVSP64:
656 case XFS_IOC_UNRESVSP:
657 case XFS_IOC_UNRESVSP64:
658 if (bf->l_len <= 0) {
Dave Chinner24513372014-06-25 14:58:08 +1000659 error = -EINVAL;
Christoph Hellwig865e9442013-10-12 00:55:08 -0700660 goto out_unlock;
661 }
662 break;
663 default:
664 bf->l_len = 0;
665 break;
666 }
667
668 if (bf->l_start < 0 ||
Christoph Hellwig8add71c2015-02-02 09:53:56 +1100669 bf->l_start > inode->i_sb->s_maxbytes ||
Christoph Hellwig865e9442013-10-12 00:55:08 -0700670 bf->l_start + bf->l_len < 0 ||
Christoph Hellwig8add71c2015-02-02 09:53:56 +1100671 bf->l_start + bf->l_len >= inode->i_sb->s_maxbytes) {
Dave Chinner24513372014-06-25 14:58:08 +1000672 error = -EINVAL;
Christoph Hellwig865e9442013-10-12 00:55:08 -0700673 goto out_unlock;
674 }
675
676 switch (cmd) {
677 case XFS_IOC_ZERO_RANGE:
Christoph Hellwig8add71c2015-02-02 09:53:56 +1100678 flags |= XFS_PREALLOC_SET;
Christoph Hellwig865e9442013-10-12 00:55:08 -0700679 error = xfs_zero_file_space(ip, bf->l_start, bf->l_len);
Christoph Hellwig865e9442013-10-12 00:55:08 -0700680 break;
681 case XFS_IOC_RESVSP:
682 case XFS_IOC_RESVSP64:
Christoph Hellwig8add71c2015-02-02 09:53:56 +1100683 flags |= XFS_PREALLOC_SET;
Christoph Hellwig865e9442013-10-12 00:55:08 -0700684 error = xfs_alloc_file_space(ip, bf->l_start, bf->l_len,
685 XFS_BMAPI_PREALLOC);
Christoph Hellwig865e9442013-10-12 00:55:08 -0700686 break;
687 case XFS_IOC_UNRESVSP:
688 case XFS_IOC_UNRESVSP64:
689 error = xfs_free_file_space(ip, bf->l_start, bf->l_len);
690 break;
691 case XFS_IOC_ALLOCSP:
692 case XFS_IOC_ALLOCSP64:
693 case XFS_IOC_FREESP:
694 case XFS_IOC_FREESP64:
Christoph Hellwig8add71c2015-02-02 09:53:56 +1100695 flags |= XFS_PREALLOC_CLEAR;
Christoph Hellwig865e9442013-10-12 00:55:08 -0700696 if (bf->l_start > XFS_ISIZE(ip)) {
697 error = xfs_alloc_file_space(ip, XFS_ISIZE(ip),
698 bf->l_start - XFS_ISIZE(ip), 0);
699 if (error)
700 goto out_unlock;
701 }
702
703 iattr.ia_valid = ATTR_SIZE;
704 iattr.ia_size = bf->l_start;
705
Jan Kara69bca802016-05-26 14:46:43 +0200706 error = xfs_vn_setattr_size(file_dentry(filp), &iattr);
Christoph Hellwig865e9442013-10-12 00:55:08 -0700707 break;
708 default:
709 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +1000710 error = -EINVAL;
Christoph Hellwig865e9442013-10-12 00:55:08 -0700711 }
712
713 if (error)
714 goto out_unlock;
715
Christoph Hellwig8add71c2015-02-02 09:53:56 +1100716 error = xfs_update_prealloc_flags(ip, flags);
Christoph Hellwig865e9442013-10-12 00:55:08 -0700717
718out_unlock:
Christoph Hellwig781355c2015-02-16 11:59:50 +1100719 xfs_iunlock(ip, iolock);
Jan Karad9457dc2012-06-12 16:20:39 +0200720 mnt_drop_write_file(filp);
Dave Chinner24513372014-06-25 14:58:08 +1000721 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722}
723
724STATIC int
725xfs_ioc_bulkstat(
726 xfs_mount_t *mp,
727 unsigned int cmd,
728 void __user *arg)
729{
730 xfs_fsop_bulkreq_t bulkreq;
731 int count; /* # of records returned */
732 xfs_ino_t inlast; /* last inode number */
733 int done;
734 int error;
735
736 /* done = 1 if there are more stats to get and if bulkstat */
737 /* should be called again (unused here, but used in dmapi) */
738
739 if (!capable(CAP_SYS_ADMIN))
740 return -EPERM;
741
742 if (XFS_FORCED_SHUTDOWN(mp))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000743 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
745 if (copy_from_user(&bulkreq, arg, sizeof(xfs_fsop_bulkreq_t)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000746 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
748 if (copy_from_user(&inlast, bulkreq.lastip, sizeof(__s64)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000749 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
751 if ((count = bulkreq.icount) <= 0)
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000752 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100754 if (bulkreq.ubuffer == NULL)
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000755 return -EINVAL;
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100756
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 if (cmd == XFS_IOC_FSINUMBERS)
758 error = xfs_inumbers(mp, &inlast, &count,
Michal Marekfaa63e92007-07-11 11:10:19 +1000759 bulkreq.ubuffer, xfs_inumbers_fmt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 else if (cmd == XFS_IOC_FSBULKSTAT_SINGLE)
Christoph Hellwigd716f8e2014-07-24 12:07:15 +1000761 error = xfs_bulkstat_one(mp, inlast, bulkreq.ubuffer,
762 sizeof(xfs_bstat_t), NULL, &done);
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100763 else /* XFS_IOC_FSBULKSTAT */
Christoph Hellwig7dce11d2010-06-23 18:11:11 +1000764 error = xfs_bulkstat(mp, &inlast, &count, xfs_bulkstat_one,
765 sizeof(xfs_bstat_t), bulkreq.ubuffer,
766 &done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
768 if (error)
Dave Chinner24513372014-06-25 14:58:08 +1000769 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
771 if (bulkreq.ocount != NULL) {
772 if (copy_to_user(bulkreq.lastip, &inlast,
773 sizeof(xfs_ino_t)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000774 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
776 if (copy_to_user(bulkreq.ocount, &count, sizeof(count)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000777 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 }
779
780 return 0;
781}
782
783STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784xfs_ioc_fsgeometry(
Dave Chinner1b6d9682019-04-12 07:41:16 -0700785 struct xfs_mount *mp,
786 void __user *arg,
787 int struct_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788{
Dave Chinner1b6d9682019-04-12 07:41:16 -0700789 struct xfs_fsop_geom fsgeo;
790 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 int error;
792
Dave Chinner1b6d9682019-04-12 07:41:16 -0700793 error = xfs_fs_geometry(&mp->m_sb, &fsgeo, struct_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 if (error)
Dave Chinner24513372014-06-25 14:58:08 +1000795 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Dave Chinner1b6d9682019-04-12 07:41:16 -0700797 if (struct_version <= 3)
798 len = sizeof(struct xfs_fsop_geom_v1);
799 else if (struct_version == 4)
800 len = sizeof(struct xfs_fsop_geom_v4);
Darrick J. Wongc23232d2019-04-12 07:41:17 -0700801 else {
802 xfs_fsop_geom_health(mp, &fsgeo);
Dave Chinner1b6d9682019-04-12 07:41:16 -0700803 len = sizeof(fsgeo);
Darrick J. Wongc23232d2019-04-12 07:41:17 -0700804 }
Dave Chinner1b6d9682019-04-12 07:41:16 -0700805
806 if (copy_to_user(arg, &fsgeo, len))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000807 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 return 0;
809}
810
Darrick J. Wong7cd50062019-04-12 07:41:17 -0700811STATIC int
812xfs_ioc_ag_geometry(
813 struct xfs_mount *mp,
814 void __user *arg)
815{
816 struct xfs_ag_geometry ageo;
817 int error;
818
819 if (copy_from_user(&ageo, arg, sizeof(ageo)))
820 return -EFAULT;
821
822 error = xfs_ag_get_geometry(mp, ageo.ag_number, &ageo);
823 if (error)
824 return error;
825
826 if (copy_to_user(arg, &ageo, sizeof(ageo)))
827 return -EFAULT;
828 return 0;
829}
830
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831/*
832 * Linux extended inode flags interface.
833 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
835STATIC unsigned int
836xfs_merge_ioc_xflags(
837 unsigned int flags,
838 unsigned int start)
839{
840 unsigned int xflags = start;
841
Eric Sandeen39058a02007-02-10 18:37:10 +1100842 if (flags & FS_IMMUTABLE_FL)
Dave Chinnere7b89482016-01-04 16:44:15 +1100843 xflags |= FS_XFLAG_IMMUTABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 else
Dave Chinnere7b89482016-01-04 16:44:15 +1100845 xflags &= ~FS_XFLAG_IMMUTABLE;
Eric Sandeen39058a02007-02-10 18:37:10 +1100846 if (flags & FS_APPEND_FL)
Dave Chinnere7b89482016-01-04 16:44:15 +1100847 xflags |= FS_XFLAG_APPEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 else
Dave Chinnere7b89482016-01-04 16:44:15 +1100849 xflags &= ~FS_XFLAG_APPEND;
Eric Sandeen39058a02007-02-10 18:37:10 +1100850 if (flags & FS_SYNC_FL)
Dave Chinnere7b89482016-01-04 16:44:15 +1100851 xflags |= FS_XFLAG_SYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 else
Dave Chinnere7b89482016-01-04 16:44:15 +1100853 xflags &= ~FS_XFLAG_SYNC;
Eric Sandeen39058a02007-02-10 18:37:10 +1100854 if (flags & FS_NOATIME_FL)
Dave Chinnere7b89482016-01-04 16:44:15 +1100855 xflags |= FS_XFLAG_NOATIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 else
Dave Chinnere7b89482016-01-04 16:44:15 +1100857 xflags &= ~FS_XFLAG_NOATIME;
Eric Sandeen39058a02007-02-10 18:37:10 +1100858 if (flags & FS_NODUMP_FL)
Dave Chinnere7b89482016-01-04 16:44:15 +1100859 xflags |= FS_XFLAG_NODUMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 else
Dave Chinnere7b89482016-01-04 16:44:15 +1100861 xflags &= ~FS_XFLAG_NODUMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
863 return xflags;
864}
865
866STATIC unsigned int
867xfs_di2lxflags(
Darrick J. Wongc8ce5402017-06-16 11:00:05 -0700868 uint16_t di_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869{
870 unsigned int flags = 0;
871
872 if (di_flags & XFS_DIFLAG_IMMUTABLE)
Eric Sandeen39058a02007-02-10 18:37:10 +1100873 flags |= FS_IMMUTABLE_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 if (di_flags & XFS_DIFLAG_APPEND)
Eric Sandeen39058a02007-02-10 18:37:10 +1100875 flags |= FS_APPEND_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 if (di_flags & XFS_DIFLAG_SYNC)
Eric Sandeen39058a02007-02-10 18:37:10 +1100877 flags |= FS_SYNC_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 if (di_flags & XFS_DIFLAG_NOATIME)
Eric Sandeen39058a02007-02-10 18:37:10 +1100879 flags |= FS_NOATIME_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 if (di_flags & XFS_DIFLAG_NODUMP)
Eric Sandeen39058a02007-02-10 18:37:10 +1100881 flags |= FS_NODUMP_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 return flags;
883}
884
885STATIC int
Christoph Hellwigc83bfab2007-10-11 17:47:00 +1000886xfs_ioc_fsgetxattr(
887 xfs_inode_t *ip,
888 int attr,
889 void __user *arg)
890{
891 struct fsxattr fa;
892
Dan Rosenberga122eb22010-09-06 18:24:57 -0400893 memset(&fa, 0, sizeof(struct fsxattr));
894
Christoph Hellwigc83bfab2007-10-11 17:47:00 +1000895 xfs_ilock(ip, XFS_ILOCK_SHARED);
896 fa.fsx_xflags = xfs_ip2xflags(ip);
897 fa.fsx_extsize = ip->i_d.di_extsize << ip->i_mount->m_sb.sb_blocklog;
Darrick J. Wongf7ca3522016-10-03 09:11:43 -0700898 fa.fsx_cowextsize = ip->i_d.di_cowextsize <<
899 ip->i_mount->m_sb.sb_blocklog;
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +0000900 fa.fsx_projid = xfs_get_projid(ip);
Christoph Hellwigc83bfab2007-10-11 17:47:00 +1000901
902 if (attr) {
903 if (ip->i_afp) {
904 if (ip->i_afp->if_flags & XFS_IFEXTENTS)
Eric Sandeen5d829302016-11-08 12:59:42 +1100905 fa.fsx_nextents = xfs_iext_count(ip->i_afp);
Christoph Hellwigc83bfab2007-10-11 17:47:00 +1000906 else
907 fa.fsx_nextents = ip->i_d.di_anextents;
908 } else
909 fa.fsx_nextents = 0;
910 } else {
911 if (ip->i_df.if_flags & XFS_IFEXTENTS)
Eric Sandeen5d829302016-11-08 12:59:42 +1100912 fa.fsx_nextents = xfs_iext_count(&ip->i_df);
Christoph Hellwigc83bfab2007-10-11 17:47:00 +1000913 else
914 fa.fsx_nextents = ip->i_d.di_nextents;
915 }
916 xfs_iunlock(ip, XFS_ILOCK_SHARED);
917
918 if (copy_to_user(arg, &fa, sizeof(fa)))
919 return -EFAULT;
920 return 0;
921}
922
Christoph Hellwigdd606872017-09-02 08:21:20 -0700923STATIC uint16_t
924xfs_flags2diflags(
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000925 struct xfs_inode *ip,
926 unsigned int xflags)
927{
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000928 /* can't set PREALLOC this way, just preserve it */
Christoph Hellwigdd606872017-09-02 08:21:20 -0700929 uint16_t di_flags =
930 (ip->i_d.di_flags & XFS_DIFLAG_PREALLOC);
931
Dave Chinnere7b89482016-01-04 16:44:15 +1100932 if (xflags & FS_XFLAG_IMMUTABLE)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000933 di_flags |= XFS_DIFLAG_IMMUTABLE;
Dave Chinnere7b89482016-01-04 16:44:15 +1100934 if (xflags & FS_XFLAG_APPEND)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000935 di_flags |= XFS_DIFLAG_APPEND;
Dave Chinnere7b89482016-01-04 16:44:15 +1100936 if (xflags & FS_XFLAG_SYNC)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000937 di_flags |= XFS_DIFLAG_SYNC;
Dave Chinnere7b89482016-01-04 16:44:15 +1100938 if (xflags & FS_XFLAG_NOATIME)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000939 di_flags |= XFS_DIFLAG_NOATIME;
Dave Chinnere7b89482016-01-04 16:44:15 +1100940 if (xflags & FS_XFLAG_NODUMP)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000941 di_flags |= XFS_DIFLAG_NODUMP;
Dave Chinnere7b89482016-01-04 16:44:15 +1100942 if (xflags & FS_XFLAG_NODEFRAG)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000943 di_flags |= XFS_DIFLAG_NODEFRAG;
Dave Chinnere7b89482016-01-04 16:44:15 +1100944 if (xflags & FS_XFLAG_FILESTREAM)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000945 di_flags |= XFS_DIFLAG_FILESTREAM;
Dave Chinnerc19b3b052016-02-09 16:54:58 +1100946 if (S_ISDIR(VFS_I(ip)->i_mode)) {
Dave Chinnere7b89482016-01-04 16:44:15 +1100947 if (xflags & FS_XFLAG_RTINHERIT)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000948 di_flags |= XFS_DIFLAG_RTINHERIT;
Dave Chinnere7b89482016-01-04 16:44:15 +1100949 if (xflags & FS_XFLAG_NOSYMLINKS)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000950 di_flags |= XFS_DIFLAG_NOSYMLINKS;
Dave Chinnere7b89482016-01-04 16:44:15 +1100951 if (xflags & FS_XFLAG_EXTSZINHERIT)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000952 di_flags |= XFS_DIFLAG_EXTSZINHERIT;
Dave Chinnere7b89482016-01-04 16:44:15 +1100953 if (xflags & FS_XFLAG_PROJINHERIT)
Dave Chinner9336e3a2014-10-02 09:18:40 +1000954 di_flags |= XFS_DIFLAG_PROJINHERIT;
Dave Chinnerc19b3b052016-02-09 16:54:58 +1100955 } else if (S_ISREG(VFS_I(ip)->i_mode)) {
Dave Chinnere7b89482016-01-04 16:44:15 +1100956 if (xflags & FS_XFLAG_REALTIME)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000957 di_flags |= XFS_DIFLAG_REALTIME;
Dave Chinnere7b89482016-01-04 16:44:15 +1100958 if (xflags & FS_XFLAG_EXTSIZE)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000959 di_flags |= XFS_DIFLAG_EXTSIZE;
960 }
Dave Chinner58f88ca2016-01-04 16:44:15 +1100961
Christoph Hellwigdd606872017-09-02 08:21:20 -0700962 return di_flags;
963}
Dave Chinner58f88ca2016-01-04 16:44:15 +1100964
Christoph Hellwigdd606872017-09-02 08:21:20 -0700965STATIC uint64_t
966xfs_flags2diflags2(
967 struct xfs_inode *ip,
968 unsigned int xflags)
969{
970 uint64_t di_flags2 =
971 (ip->i_d.di_flags2 & XFS_DIFLAG2_REFLINK);
972
Dave Chinner58f88ca2016-01-04 16:44:15 +1100973 if (xflags & FS_XFLAG_DAX)
974 di_flags2 |= XFS_DIFLAG2_DAX;
Darrick J. Wongf7ca3522016-10-03 09:11:43 -0700975 if (xflags & FS_XFLAG_COWEXTSIZE)
976 di_flags2 |= XFS_DIFLAG2_COWEXTSIZE;
Dave Chinner58f88ca2016-01-04 16:44:15 +1100977
Christoph Hellwigdd606872017-09-02 08:21:20 -0700978 return di_flags2;
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000979}
980
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000981STATIC void
982xfs_diflags_to_linux(
983 struct xfs_inode *ip)
984{
David Chinnere4f75292008-08-13 16:00:45 +1000985 struct inode *inode = VFS_I(ip);
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000986 unsigned int xflags = xfs_ip2xflags(ip);
987
Dave Chinnere7b89482016-01-04 16:44:15 +1100988 if (xflags & FS_XFLAG_IMMUTABLE)
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000989 inode->i_flags |= S_IMMUTABLE;
990 else
991 inode->i_flags &= ~S_IMMUTABLE;
Dave Chinnere7b89482016-01-04 16:44:15 +1100992 if (xflags & FS_XFLAG_APPEND)
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000993 inode->i_flags |= S_APPEND;
994 else
995 inode->i_flags &= ~S_APPEND;
Dave Chinnere7b89482016-01-04 16:44:15 +1100996 if (xflags & FS_XFLAG_SYNC)
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000997 inode->i_flags |= S_SYNC;
998 else
999 inode->i_flags &= ~S_SYNC;
Dave Chinnere7b89482016-01-04 16:44:15 +11001000 if (xflags & FS_XFLAG_NOATIME)
Christoph Hellwigf13fae22008-07-21 16:16:15 +10001001 inode->i_flags |= S_NOATIME;
1002 else
1003 inode->i_flags &= ~S_NOATIME;
Christoph Hellwig742d8422017-08-30 09:23:01 -07001004#if 0 /* disabled until the flag switching races are sorted out */
Dave Chinner58f88ca2016-01-04 16:44:15 +11001005 if (xflags & FS_XFLAG_DAX)
1006 inode->i_flags |= S_DAX;
1007 else
1008 inode->i_flags &= ~S_DAX;
Christoph Hellwig742d8422017-08-30 09:23:01 -07001009#endif
Christoph Hellwigf13fae22008-07-21 16:16:15 +10001010}
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001011
Dave Chinner29a17c02015-02-02 10:14:25 +11001012static int
1013xfs_ioctl_setattr_xflags(
1014 struct xfs_trans *tp,
1015 struct xfs_inode *ip,
1016 struct fsxattr *fa)
1017{
1018 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwigdd606872017-09-02 08:21:20 -07001019 uint64_t di_flags2;
Dave Chinner29a17c02015-02-02 10:14:25 +11001020
1021 /* Can't change realtime flag if any extents are allocated. */
1022 if ((ip->i_d.di_nextents || ip->i_delayed_blks) &&
Dave Chinnere7b89482016-01-04 16:44:15 +11001023 XFS_IS_REALTIME_INODE(ip) != (fa->fsx_xflags & FS_XFLAG_REALTIME))
Dave Chinner29a17c02015-02-02 10:14:25 +11001024 return -EINVAL;
1025
1026 /* If realtime flag is set then must have realtime device */
Dave Chinnere7b89482016-01-04 16:44:15 +11001027 if (fa->fsx_xflags & FS_XFLAG_REALTIME) {
Dave Chinner29a17c02015-02-02 10:14:25 +11001028 if (mp->m_sb.sb_rblocks == 0 || mp->m_sb.sb_rextsize == 0 ||
1029 (ip->i_d.di_extsize % mp->m_sb.sb_rextsize))
1030 return -EINVAL;
1031 }
1032
Darrick J. Wong1987fd72016-10-10 16:49:29 +11001033 /* Clear reflink if we are actually able to set the rt flag. */
Darrick J. Wongc8e156a2016-10-03 09:11:50 -07001034 if ((fa->fsx_xflags & FS_XFLAG_REALTIME) && xfs_is_reflink_inode(ip))
Darrick J. Wong1987fd72016-10-10 16:49:29 +11001035 ip->i_d.di_flags2 &= ~XFS_DIFLAG2_REFLINK;
Darrick J. Wongc8e156a2016-10-03 09:11:50 -07001036
Darrick J. Wong4f435eb2016-10-03 09:11:50 -07001037 /* Don't allow us to set DAX mode for a reflinked file for now. */
1038 if ((fa->fsx_xflags & FS_XFLAG_DAX) && xfs_is_reflink_inode(ip))
1039 return -EINVAL;
1040
Dave Chinner29a17c02015-02-02 10:14:25 +11001041 /*
1042 * Can't modify an immutable/append-only file unless
1043 * we have appropriate permission.
1044 */
1045 if (((ip->i_d.di_flags & (XFS_DIFLAG_IMMUTABLE | XFS_DIFLAG_APPEND)) ||
Dave Chinnere7b89482016-01-04 16:44:15 +11001046 (fa->fsx_xflags & (FS_XFLAG_IMMUTABLE | FS_XFLAG_APPEND))) &&
Dave Chinner29a17c02015-02-02 10:14:25 +11001047 !capable(CAP_LINUX_IMMUTABLE))
1048 return -EPERM;
1049
Christoph Hellwigdd606872017-09-02 08:21:20 -07001050 /* diflags2 only valid for v3 inodes. */
1051 di_flags2 = xfs_flags2diflags2(ip, fa->fsx_xflags);
1052 if (di_flags2 && ip->i_d.di_version < 3)
1053 return -EINVAL;
1054
1055 ip->i_d.di_flags = xfs_flags2diflags(ip, fa->fsx_xflags);
1056 ip->i_d.di_flags2 = di_flags2;
1057
Dave Chinner29a17c02015-02-02 10:14:25 +11001058 xfs_diflags_to_linux(ip);
1059 xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
1060 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11001061 XFS_STATS_INC(mp, xs_ig_attrchg);
Dave Chinner29a17c02015-02-02 10:14:25 +11001062 return 0;
1063}
1064
Dave Chinner8f3d17a2015-02-02 10:15:35 +11001065/*
Dave Chinner3a6a8542016-03-01 09:41:33 +11001066 * If we are changing DAX flags, we have to ensure the file is clean and any
1067 * cached objects in the address space are invalidated and removed. This
1068 * requires us to lock out other IO and page faults similar to a truncate
1069 * operation. The locks need to be held until the transaction has been committed
1070 * so that the cache invalidation is atomic with respect to the DAX flag
1071 * manipulation.
1072 */
1073static int
1074xfs_ioctl_setattr_dax_invalidate(
1075 struct xfs_inode *ip,
1076 struct fsxattr *fa,
1077 int *join_flags)
1078{
1079 struct inode *inode = VFS_I(ip);
Ross Zwisler6851a3d2017-09-18 14:46:03 -07001080 struct super_block *sb = inode->i_sb;
Dave Chinner3a6a8542016-03-01 09:41:33 +11001081 int error;
1082
1083 *join_flags = 0;
1084
1085 /*
1086 * It is only valid to set the DAX flag on regular files and
Dave Chinner64485432016-03-01 09:41:33 +11001087 * directories on filesystems where the block size is equal to the page
Darrick J. Wongaaacdd22018-05-31 15:07:47 -07001088 * size. On directories it serves as an inherited hint so we don't
1089 * have to check the device for dax support or flush pagecache.
Dave Chinner3a6a8542016-03-01 09:41:33 +11001090 */
Dave Chinner64485432016-03-01 09:41:33 +11001091 if (fa->fsx_xflags & FS_XFLAG_DAX) {
1092 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)))
1093 return -EINVAL;
Darrick J. Wongaaacdd22018-05-31 15:07:47 -07001094 if (S_ISREG(inode->i_mode) &&
1095 !bdev_dax_supported(xfs_find_bdev_for_inode(VFS_I(ip)),
Dave Jiang80660f22018-05-30 13:03:46 -07001096 sb->s_blocksize))
Dave Chinner64485432016-03-01 09:41:33 +11001097 return -EINVAL;
1098 }
Dave Chinner3a6a8542016-03-01 09:41:33 +11001099
1100 /* If the DAX state is not changing, we have nothing to do here. */
1101 if ((fa->fsx_xflags & FS_XFLAG_DAX) && IS_DAX(inode))
1102 return 0;
1103 if (!(fa->fsx_xflags & FS_XFLAG_DAX) && !IS_DAX(inode))
1104 return 0;
1105
Darrick J. Wongaaacdd22018-05-31 15:07:47 -07001106 if (S_ISDIR(inode->i_mode))
1107 return 0;
1108
Dave Chinner3a6a8542016-03-01 09:41:33 +11001109 /* lock, flush and invalidate mapping in preparation for flag change */
1110 xfs_ilock(ip, XFS_MMAPLOCK_EXCL | XFS_IOLOCK_EXCL);
1111 error = filemap_write_and_wait(inode->i_mapping);
1112 if (error)
1113 goto out_unlock;
1114 error = invalidate_inode_pages2(inode->i_mapping);
1115 if (error)
1116 goto out_unlock;
1117
1118 *join_flags = XFS_MMAPLOCK_EXCL | XFS_IOLOCK_EXCL;
1119 return 0;
1120
1121out_unlock:
1122 xfs_iunlock(ip, XFS_MMAPLOCK_EXCL | XFS_IOLOCK_EXCL);
1123 return error;
1124
1125}
1126
1127/*
Dave Chinner8f3d17a2015-02-02 10:15:35 +11001128 * Set up the transaction structure for the setattr operation, checking that we
1129 * have permission to do so. On success, return a clean transaction and the
1130 * inode locked exclusively ready for further operation specific checks. On
1131 * failure, return an error without modifying or locking the inode.
Dave Chinner3a6a8542016-03-01 09:41:33 +11001132 *
1133 * The inode might already be IO locked on call. If this is the case, it is
1134 * indicated in @join_flags and we take full responsibility for ensuring they
1135 * are unlocked from now on. Hence if we have an error here, we still have to
1136 * unlock them. Otherwise, once they are joined to the transaction, they will
1137 * be unlocked on commit/cancel.
Dave Chinner8f3d17a2015-02-02 10:15:35 +11001138 */
1139static struct xfs_trans *
1140xfs_ioctl_setattr_get_trans(
Dave Chinner3a6a8542016-03-01 09:41:33 +11001141 struct xfs_inode *ip,
1142 int join_flags)
Dave Chinner8f3d17a2015-02-02 10:15:35 +11001143{
1144 struct xfs_mount *mp = ip->i_mount;
1145 struct xfs_trans *tp;
Dave Chinner3a6a8542016-03-01 09:41:33 +11001146 int error = -EROFS;
Dave Chinner8f3d17a2015-02-02 10:15:35 +11001147
1148 if (mp->m_flags & XFS_MOUNT_RDONLY)
Dave Chinner3a6a8542016-03-01 09:41:33 +11001149 goto out_unlock;
1150 error = -EIO;
Dave Chinner8f3d17a2015-02-02 10:15:35 +11001151 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner3a6a8542016-03-01 09:41:33 +11001152 goto out_unlock;
Dave Chinner8f3d17a2015-02-02 10:15:35 +11001153
Christoph Hellwig253f4912016-04-06 09:19:55 +10001154 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
Dave Chinner8f3d17a2015-02-02 10:15:35 +11001155 if (error)
Darrick J. Wong3de5eab2019-04-22 16:28:34 -07001156 goto out_unlock;
Dave Chinner8f3d17a2015-02-02 10:15:35 +11001157
1158 xfs_ilock(ip, XFS_ILOCK_EXCL);
Dave Chinner3a6a8542016-03-01 09:41:33 +11001159 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | join_flags);
1160 join_flags = 0;
Dave Chinner8f3d17a2015-02-02 10:15:35 +11001161
1162 /*
1163 * CAP_FOWNER overrides the following restrictions:
1164 *
1165 * The user ID of the calling process must be equal to the file owner
1166 * ID, except in cases where the CAP_FSETID capability is applicable.
1167 */
1168 if (!inode_owner_or_capable(VFS_I(ip))) {
1169 error = -EPERM;
1170 goto out_cancel;
1171 }
1172
1173 if (mp->m_flags & XFS_MOUNT_WSYNC)
1174 xfs_trans_set_sync(tp);
1175
1176 return tp;
1177
1178out_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +10001179 xfs_trans_cancel(tp);
Dave Chinner3a6a8542016-03-01 09:41:33 +11001180out_unlock:
1181 if (join_flags)
1182 xfs_iunlock(ip, join_flags);
Dave Chinner8f3d17a2015-02-02 10:15:35 +11001183 return ERR_PTR(error);
1184}
1185
Iustin Pop9b94fcc2015-02-02 10:26:26 +11001186/*
1187 * extent size hint validation is somewhat cumbersome. Rules are:
1188 *
1189 * 1. extent size hint is only valid for directories and regular files
Dave Chinnere7b89482016-01-04 16:44:15 +11001190 * 2. FS_XFLAG_EXTSIZE is only valid for regular files
1191 * 3. FS_XFLAG_EXTSZINHERIT is only valid for directories.
Iustin Pop9b94fcc2015-02-02 10:26:26 +11001192 * 4. can only be changed on regular files if no extents are allocated
1193 * 5. can be changed on directories at any time
1194 * 6. extsize hint of 0 turns off hints, clears inode flags.
1195 * 7. Extent size must be a multiple of the appropriate block size.
1196 * 8. for non-realtime files, the extent size hint must be limited
1197 * to half the AG size to avoid alignment extending the extent beyond the
1198 * limits of the AG.
Darrick J. Wong80e4e122017-10-17 21:37:42 -07001199 *
1200 * Please keep this function in sync with xfs_scrub_inode_extsize.
Iustin Pop9b94fcc2015-02-02 10:26:26 +11001201 */
kbuild test robotf92090e2015-02-05 11:13:21 +11001202static int
Dave Chinnerd4388d3c2015-02-02 10:22:20 +11001203xfs_ioctl_setattr_check_extsize(
1204 struct xfs_inode *ip,
1205 struct fsxattr *fa)
1206{
1207 struct xfs_mount *mp = ip->i_mount;
1208
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001209 if ((fa->fsx_xflags & FS_XFLAG_EXTSIZE) && !S_ISREG(VFS_I(ip)->i_mode))
Iustin Pop9b94fcc2015-02-02 10:26:26 +11001210 return -EINVAL;
1211
Dave Chinnere7b89482016-01-04 16:44:15 +11001212 if ((fa->fsx_xflags & FS_XFLAG_EXTSZINHERIT) &&
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001213 !S_ISDIR(VFS_I(ip)->i_mode))
Iustin Pop9b94fcc2015-02-02 10:26:26 +11001214 return -EINVAL;
1215
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001216 if (S_ISREG(VFS_I(ip)->i_mode) && ip->i_d.di_nextents &&
Dave Chinnerd4388d3c2015-02-02 10:22:20 +11001217 ((ip->i_d.di_extsize << mp->m_sb.sb_blocklog) != fa->fsx_extsize))
1218 return -EINVAL;
1219
Dave Chinnerd4388d3c2015-02-02 10:22:20 +11001220 if (fa->fsx_extsize != 0) {
1221 xfs_extlen_t size;
1222 xfs_fsblock_t extsize_fsb;
1223
1224 extsize_fsb = XFS_B_TO_FSB(mp, fa->fsx_extsize);
1225 if (extsize_fsb > MAXEXTLEN)
1226 return -EINVAL;
1227
1228 if (XFS_IS_REALTIME_INODE(ip) ||
Dave Chinnere7b89482016-01-04 16:44:15 +11001229 (fa->fsx_xflags & FS_XFLAG_REALTIME)) {
Dave Chinnerd4388d3c2015-02-02 10:22:20 +11001230 size = mp->m_sb.sb_rextsize << mp->m_sb.sb_blocklog;
1231 } else {
1232 size = mp->m_sb.sb_blocksize;
1233 if (extsize_fsb > mp->m_sb.sb_agblocks / 2)
1234 return -EINVAL;
1235 }
1236
1237 if (fa->fsx_extsize % size)
1238 return -EINVAL;
Iustin Pop9b94fcc2015-02-02 10:26:26 +11001239 } else
Dave Chinnere7b89482016-01-04 16:44:15 +11001240 fa->fsx_xflags &= ~(FS_XFLAG_EXTSIZE | FS_XFLAG_EXTSZINHERIT);
Iustin Pop9b94fcc2015-02-02 10:26:26 +11001241
Dave Chinnerd4388d3c2015-02-02 10:22:20 +11001242 return 0;
1243}
1244
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001245/*
1246 * CoW extent size hint validation rules are:
1247 *
1248 * 1. CoW extent size hint can only be set if reflink is enabled on the fs.
1249 * The inode does not have to have any shared blocks, but it must be a v3.
1250 * 2. FS_XFLAG_COWEXTSIZE is only valid for directories and regular files;
1251 * for a directory, the hint is propagated to new files.
1252 * 3. Can be changed on files & directories at any time.
1253 * 4. CoW extsize hint of 0 turns off hints, clears inode flags.
1254 * 5. Extent size must be a multiple of the appropriate block size.
1255 * 6. The extent size hint must be limited to half the AG size to avoid
1256 * alignment extending the extent beyond the limits of the AG.
Darrick J. Wong80e4e122017-10-17 21:37:42 -07001257 *
1258 * Please keep this function in sync with xfs_scrub_inode_cowextsize.
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001259 */
1260static int
1261xfs_ioctl_setattr_check_cowextsize(
1262 struct xfs_inode *ip,
1263 struct fsxattr *fa)
1264{
1265 struct xfs_mount *mp = ip->i_mount;
1266
1267 if (!(fa->fsx_xflags & FS_XFLAG_COWEXTSIZE))
1268 return 0;
1269
1270 if (!xfs_sb_version_hasreflink(&ip->i_mount->m_sb) ||
1271 ip->i_d.di_version != 3)
1272 return -EINVAL;
1273
1274 if (!S_ISREG(VFS_I(ip)->i_mode) && !S_ISDIR(VFS_I(ip)->i_mode))
1275 return -EINVAL;
1276
1277 if (fa->fsx_cowextsize != 0) {
1278 xfs_extlen_t size;
1279 xfs_fsblock_t cowextsize_fsb;
1280
1281 cowextsize_fsb = XFS_B_TO_FSB(mp, fa->fsx_cowextsize);
1282 if (cowextsize_fsb > MAXEXTLEN)
1283 return -EINVAL;
1284
1285 size = mp->m_sb.sb_blocksize;
1286 if (cowextsize_fsb > mp->m_sb.sb_agblocks / 2)
1287 return -EINVAL;
1288
1289 if (fa->fsx_cowextsize % size)
1290 return -EINVAL;
1291 } else
1292 fa->fsx_xflags &= ~FS_XFLAG_COWEXTSIZE;
1293
1294 return 0;
1295}
1296
kbuild test robotf92090e2015-02-05 11:13:21 +11001297static int
Dave Chinner23bd0732015-02-02 10:22:53 +11001298xfs_ioctl_setattr_check_projid(
1299 struct xfs_inode *ip,
1300 struct fsxattr *fa)
1301{
1302 /* Disallow 32bit project ids if projid32bit feature is not enabled. */
Darrick J. Wongc8ce5402017-06-16 11:00:05 -07001303 if (fa->fsx_projid > (uint16_t)-1 &&
Dave Chinner23bd0732015-02-02 10:22:53 +11001304 !xfs_sb_version_hasprojid32bit(&ip->i_mount->m_sb))
1305 return -EINVAL;
1306
1307 /*
1308 * Project Quota ID state is only allowed to change from within the init
1309 * namespace. Enforce that restriction only if we are trying to change
1310 * the quota ID state. Everything else is allowed in user namespaces.
1311 */
1312 if (current_user_ns() == &init_user_ns)
1313 return 0;
1314
1315 if (xfs_get_projid(ip) != fa->fsx_projid)
1316 return -EINVAL;
Dave Chinnere7b89482016-01-04 16:44:15 +11001317 if ((fa->fsx_xflags & FS_XFLAG_PROJINHERIT) !=
Dave Chinner23bd0732015-02-02 10:22:53 +11001318 (ip->i_d.di_flags & XFS_DIFLAG_PROJINHERIT))
1319 return -EINVAL;
1320
1321 return 0;
1322}
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001323
1324STATIC int
1325xfs_ioctl_setattr(
1326 xfs_inode_t *ip,
Dave Chinnerfd179b92015-02-02 10:16:25 +11001327 struct fsxattr *fa)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001328{
1329 struct xfs_mount *mp = ip->i_mount;
1330 struct xfs_trans *tp;
Christoph Hellwig7d095252009-06-08 15:33:32 +02001331 struct xfs_dquot *udqp = NULL;
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001332 struct xfs_dquot *pdqp = NULL;
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001333 struct xfs_dquot *olddquot = NULL;
1334 int code;
Dave Chinner3a6a8542016-03-01 09:41:33 +11001335 int join_flags = 0;
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001336
Christoph Hellwigcca28fb2010-06-24 11:57:09 +10001337 trace_xfs_ioctl_setattr(ip);
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001338
Dave Chinner23bd0732015-02-02 10:22:53 +11001339 code = xfs_ioctl_setattr_check_projid(ip, fa);
1340 if (code)
1341 return code;
Arkadiusz Mi?kiewicz23963e542010-08-26 10:19:43 +00001342
1343 /*
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001344 * If disk quotas is on, we make sure that the dquots do exist on disk,
1345 * before we start any other transactions. Trying to do this later
1346 * is messy. We don't care to take a readlock to look at the ids
1347 * in inode here, because we can't hold it across the trans_reserve.
1348 * If the IDs do change before we take the ilock, we're covered
1349 * because the i_*dquot fields will get updated anyway.
1350 */
Dave Chinnerfd179b92015-02-02 10:16:25 +11001351 if (XFS_IS_QUOTA_ON(mp)) {
Christoph Hellwig7d095252009-06-08 15:33:32 +02001352 code = xfs_qm_vop_dqalloc(ip, ip->i_d.di_uid,
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001353 ip->i_d.di_gid, fa->fsx_projid,
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001354 XFS_QMOPT_PQUOTA, &udqp, NULL, &pdqp);
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001355 if (code)
1356 return code;
1357 }
1358
Dave Chinner3a6a8542016-03-01 09:41:33 +11001359 /*
1360 * Changing DAX config may require inode locking for mapping
1361 * invalidation. These need to be held all the way to transaction commit
1362 * or cancel time, so need to be passed through to
1363 * xfs_ioctl_setattr_get_trans() so it can apply them to the join call
1364 * appropriately.
1365 */
1366 code = xfs_ioctl_setattr_dax_invalidate(ip, fa, &join_flags);
1367 if (code)
1368 goto error_free_dquots;
1369
1370 tp = xfs_ioctl_setattr_get_trans(ip, join_flags);
Dave Chinner8f3d17a2015-02-02 10:15:35 +11001371 if (IS_ERR(tp)) {
1372 code = PTR_ERR(tp);
1373 goto error_free_dquots;
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001374 }
1375
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001376
Dave Chinnerfd179b92015-02-02 10:16:25 +11001377 if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_PQUOTA_ON(mp) &&
1378 xfs_get_projid(ip) != fa->fsx_projid) {
1379 code = xfs_qm_vop_chown_reserve(tp, ip, udqp, NULL, pdqp,
1380 capable(CAP_FOWNER) ? XFS_QMOPT_FORCE_RES : 0);
1381 if (code) /* out of quota */
Dave Chinnerd4388d3c2015-02-02 10:22:20 +11001382 goto error_trans_cancel;
Dave Chinnerfd179b92015-02-02 10:16:25 +11001383 }
1384
Dave Chinnerd4388d3c2015-02-02 10:22:20 +11001385 code = xfs_ioctl_setattr_check_extsize(ip, fa);
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001386 if (code)
Dave Chinnerd4388d3c2015-02-02 10:22:20 +11001387 goto error_trans_cancel;
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001388
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001389 code = xfs_ioctl_setattr_check_cowextsize(ip, fa);
1390 if (code)
1391 goto error_trans_cancel;
1392
Dave Chinner29a17c02015-02-02 10:14:25 +11001393 code = xfs_ioctl_setattr_xflags(tp, ip, fa);
1394 if (code)
Dave Chinnerd4388d3c2015-02-02 10:22:20 +11001395 goto error_trans_cancel;
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001396
1397 /*
Dave Chinnerfd179b92015-02-02 10:16:25 +11001398 * Change file ownership. Must be the owner or privileged. CAP_FSETID
1399 * overrides the following restrictions:
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001400 *
Dave Chinnerfd179b92015-02-02 10:16:25 +11001401 * The set-user-ID and set-group-ID bits of a file will be cleared upon
1402 * successful return from chown()
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001403 */
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001404
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001405 if ((VFS_I(ip)->i_mode & (S_ISUID|S_ISGID)) &&
Dave Chinnerfd179b92015-02-02 10:16:25 +11001406 !capable_wrt_inode_uidgid(VFS_I(ip), CAP_FSETID))
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001407 VFS_I(ip)->i_mode &= ~(S_ISUID|S_ISGID);
Dave Chinnerfd179b92015-02-02 10:16:25 +11001408
1409 /* Change the ownerships and register project quota modifications */
1410 if (xfs_get_projid(ip) != fa->fsx_projid) {
1411 if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_PQUOTA_ON(mp)) {
1412 olddquot = xfs_qm_vop_chown(tp, ip,
1413 &ip->i_pdquot, pdqp);
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001414 }
Dave Chinnerfd179b92015-02-02 10:16:25 +11001415 ASSERT(ip->i_d.di_version > 1);
1416 xfs_set_projid(ip, fa->fsx_projid);
Christoph Hellwigf13fae22008-07-21 16:16:15 +10001417 }
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001418
Dave Chinnera8727032014-10-02 09:20:30 +10001419 /*
1420 * Only set the extent size hint if we've already determined that the
1421 * extent size hint should be set on the inode. If no extent size flags
1422 * are set on the inode then unconditionally clear the extent size hint.
1423 */
Dave Chinnerfd179b92015-02-02 10:16:25 +11001424 if (ip->i_d.di_flags & (XFS_DIFLAG_EXTSIZE | XFS_DIFLAG_EXTSZINHERIT))
1425 ip->i_d.di_extsize = fa->fsx_extsize >> mp->m_sb.sb_blocklog;
1426 else
1427 ip->i_d.di_extsize = 0;
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001428 if (ip->i_d.di_version == 3 &&
1429 (ip->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE))
1430 ip->i_d.di_cowextsize = fa->fsx_cowextsize >>
1431 mp->m_sb.sb_blocklog;
1432 else
1433 ip->i_d.di_cowextsize = 0;
Dave Chinnera8727032014-10-02 09:20:30 +10001434
Christoph Hellwig70393312015-06-04 13:48:08 +10001435 code = xfs_trans_commit(tp);
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001436
1437 /*
1438 * Release any dquot(s) the inode had kept before chown.
1439 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02001440 xfs_qm_dqrele(olddquot);
1441 xfs_qm_dqrele(udqp);
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001442 xfs_qm_dqrele(pdqp);
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001443
Christoph Hellwig288699f2010-06-23 18:11:15 +10001444 return code;
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001445
Dave Chinnerd4388d3c2015-02-02 10:22:20 +11001446error_trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +10001447 xfs_trans_cancel(tp);
Dave Chinner8f3d17a2015-02-02 10:15:35 +11001448error_free_dquots:
Christoph Hellwig7d095252009-06-08 15:33:32 +02001449 xfs_qm_dqrele(udqp);
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001450 xfs_qm_dqrele(pdqp);
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001451 return code;
1452}
1453
Christoph Hellwigc83bfab2007-10-11 17:47:00 +10001454STATIC int
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001455xfs_ioc_fssetxattr(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 xfs_inode_t *ip,
1457 struct file *filp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 void __user *arg)
1459{
1460 struct fsxattr fa;
Jan Karad9457dc2012-06-12 16:20:39 +02001461 int error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001462
1463 if (copy_from_user(&fa, arg, sizeof(fa)))
1464 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
Jan Karad9457dc2012-06-12 16:20:39 +02001466 error = mnt_want_write_file(filp);
1467 if (error)
1468 return error;
Dave Chinnerfd179b92015-02-02 10:16:25 +11001469 error = xfs_ioctl_setattr(ip, &fa);
Jan Karad9457dc2012-06-12 16:20:39 +02001470 mnt_drop_write_file(filp);
Dave Chinner24513372014-06-25 14:58:08 +10001471 return error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001472}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001474STATIC int
1475xfs_ioc_getxflags(
1476 xfs_inode_t *ip,
1477 void __user *arg)
1478{
1479 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001481 flags = xfs_di2lxflags(ip->i_d.di_flags);
1482 if (copy_to_user(arg, &flags, sizeof(flags)))
1483 return -EFAULT;
1484 return 0;
1485}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001487STATIC int
1488xfs_ioc_setxflags(
Dave Chinnerf96291f2015-02-02 10:15:56 +11001489 struct xfs_inode *ip,
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001490 struct file *filp,
1491 void __user *arg)
1492{
Dave Chinnerf96291f2015-02-02 10:15:56 +11001493 struct xfs_trans *tp;
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001494 struct fsxattr fa;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001495 unsigned int flags;
Dave Chinner3a6a8542016-03-01 09:41:33 +11001496 int join_flags = 0;
Dave Chinnerf96291f2015-02-02 10:15:56 +11001497 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001499 if (copy_from_user(&flags, arg, sizeof(flags)))
1500 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001502 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
1503 FS_NOATIME_FL | FS_NODUMP_FL | \
1504 FS_SYNC_FL))
1505 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001507 fa.fsx_xflags = xfs_merge_ioc_xflags(flags, xfs_ip2xflags(ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
Jan Karad9457dc2012-06-12 16:20:39 +02001509 error = mnt_want_write_file(filp);
1510 if (error)
1511 return error;
Dave Chinnerf96291f2015-02-02 10:15:56 +11001512
Dave Chinner3a6a8542016-03-01 09:41:33 +11001513 /*
1514 * Changing DAX config may require inode locking for mapping
1515 * invalidation. These need to be held all the way to transaction commit
1516 * or cancel time, so need to be passed through to
1517 * xfs_ioctl_setattr_get_trans() so it can apply them to the join call
1518 * appropriately.
1519 */
1520 error = xfs_ioctl_setattr_dax_invalidate(ip, &fa, &join_flags);
1521 if (error)
1522 goto out_drop_write;
1523
1524 tp = xfs_ioctl_setattr_get_trans(ip, join_flags);
Dave Chinnerf96291f2015-02-02 10:15:56 +11001525 if (IS_ERR(tp)) {
1526 error = PTR_ERR(tp);
1527 goto out_drop_write;
1528 }
1529
1530 error = xfs_ioctl_setattr_xflags(tp, ip, &fa);
1531 if (error) {
Christoph Hellwig4906e212015-06-04 13:47:56 +10001532 xfs_trans_cancel(tp);
Dave Chinnerf96291f2015-02-02 10:15:56 +11001533 goto out_drop_write;
1534 }
1535
Christoph Hellwig70393312015-06-04 13:48:08 +10001536 error = xfs_trans_commit(tp);
Dave Chinnerf96291f2015-02-02 10:15:56 +11001537out_drop_write:
Jan Karad9457dc2012-06-12 16:20:39 +02001538 mnt_drop_write_file(filp);
Dave Chinner24513372014-06-25 14:58:08 +10001539 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540}
1541
Christoph Hellwig232b51942017-10-17 14:16:19 -07001542static bool
1543xfs_getbmap_format(
1544 struct kgetbmap *p,
1545 struct getbmapx __user *u,
1546 size_t recsize)
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001547{
Christoph Hellwig232b51942017-10-17 14:16:19 -07001548 if (put_user(p->bmv_offset, &u->bmv_offset) ||
1549 put_user(p->bmv_block, &u->bmv_block) ||
1550 put_user(p->bmv_length, &u->bmv_length) ||
1551 put_user(0, &u->bmv_count) ||
1552 put_user(0, &u->bmv_entries))
1553 return false;
1554 if (recsize < sizeof(struct getbmapx))
1555 return true;
1556 if (put_user(0, &u->bmv_iflags) ||
1557 put_user(p->bmv_oflags, &u->bmv_oflags) ||
1558 put_user(0, &u->bmv_unused1) ||
1559 put_user(0, &u->bmv_unused2))
1560 return false;
1561 return true;
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001562}
1563
1564STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565xfs_ioc_getbmap(
Christoph Hellwig8f3e2052016-07-20 11:29:35 +10001566 struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 unsigned int cmd,
1568 void __user *arg)
1569{
Darrick J. Wongbe6324c2017-04-03 15:17:57 -07001570 struct getbmapx bmx = { 0 };
Christoph Hellwig232b51942017-10-17 14:16:19 -07001571 struct kgetbmap *buf;
1572 size_t recsize;
1573 int error, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
Christoph Hellwig232b51942017-10-17 14:16:19 -07001575 switch (cmd) {
1576 case XFS_IOC_GETBMAPA:
1577 bmx.bmv_iflags = BMV_IF_ATTRFORK;
1578 /*FALLTHRU*/
1579 case XFS_IOC_GETBMAP:
1580 if (file->f_mode & FMODE_NOCMTIME)
1581 bmx.bmv_iflags |= BMV_IF_NO_DMAPI_READ;
1582 /* struct getbmap is a strict subset of struct getbmapx. */
1583 recsize = sizeof(struct getbmap);
1584 break;
1585 case XFS_IOC_GETBMAPX:
1586 recsize = sizeof(struct getbmapx);
1587 break;
1588 default:
1589 return -EINVAL;
1590 }
1591
1592 if (copy_from_user(&bmx, arg, recsize))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10001593 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001595 if (bmx.bmv_count < 2)
Eric Sandeenb474c7a2014-06-22 15:04:54 +10001596 return -EINVAL;
Christoph Hellwig232b51942017-10-17 14:16:19 -07001597 if (bmx.bmv_count > ULONG_MAX / recsize)
1598 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
Christoph Hellwig232b51942017-10-17 14:16:19 -07001600 buf = kmem_zalloc_large(bmx.bmv_count * sizeof(*buf), 0);
1601 if (!buf)
1602 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
Christoph Hellwig232b51942017-10-17 14:16:19 -07001604 error = xfs_getbmap(XFS_I(file_inode(file)), &bmx, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 if (error)
Christoph Hellwig232b51942017-10-17 14:16:19 -07001606 goto out_free_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607
Christoph Hellwig232b51942017-10-17 14:16:19 -07001608 error = -EFAULT;
1609 if (copy_to_user(arg, &bmx, recsize))
1610 goto out_free_buf;
1611 arg += recsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
Christoph Hellwig232b51942017-10-17 14:16:19 -07001613 for (i = 0; i < bmx.bmv_entries; i++) {
1614 if (!xfs_getbmap_format(buf + i, arg, recsize))
1615 goto out_free_buf;
1616 arg += recsize;
1617 }
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001618
Christoph Hellwig232b51942017-10-17 14:16:19 -07001619 error = 0;
1620out_free_buf:
1621 kmem_free(buf);
Christophe JAILLET132bf672018-11-06 07:50:50 -08001622 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623}
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001624
Darrick J. Wonge89c0412017-03-28 14:56:37 -07001625struct getfsmap_info {
1626 struct xfs_mount *mp;
Christoph Hellwig9d17e142017-04-21 11:24:41 -07001627 struct fsmap_head __user *data;
1628 unsigned int idx;
Darrick J. Wonge89c0412017-03-28 14:56:37 -07001629 __u32 last_flags;
1630};
1631
1632STATIC int
1633xfs_getfsmap_format(struct xfs_fsmap *xfm, void *priv)
1634{
1635 struct getfsmap_info *info = priv;
1636 struct fsmap fm;
1637
1638 trace_xfs_getfsmap_mapping(info->mp, xfm);
1639
1640 info->last_flags = xfm->fmr_flags;
1641 xfs_fsmap_from_internal(&fm, xfm);
Christoph Hellwig9d17e142017-04-21 11:24:41 -07001642 if (copy_to_user(&info->data->fmh_recs[info->idx++], &fm,
1643 sizeof(struct fsmap)))
Darrick J. Wonge89c0412017-03-28 14:56:37 -07001644 return -EFAULT;
1645
Darrick J. Wonge89c0412017-03-28 14:56:37 -07001646 return 0;
1647}
1648
1649STATIC int
1650xfs_ioc_getfsmap(
1651 struct xfs_inode *ip,
Christoph Hellwig9d17e142017-04-21 11:24:41 -07001652 struct fsmap_head __user *arg)
Darrick J. Wonge89c0412017-03-28 14:56:37 -07001653{
Christoph Hellwigef2b67e2017-04-21 11:24:40 -07001654 struct getfsmap_info info = { NULL };
Darrick J. Wonge89c0412017-03-28 14:56:37 -07001655 struct xfs_fsmap_head xhead = {0};
1656 struct fsmap_head head;
1657 bool aborted = false;
1658 int error;
1659
1660 if (copy_from_user(&head, arg, sizeof(struct fsmap_head)))
1661 return -EFAULT;
1662 if (memchr_inv(head.fmh_reserved, 0, sizeof(head.fmh_reserved)) ||
1663 memchr_inv(head.fmh_keys[0].fmr_reserved, 0,
1664 sizeof(head.fmh_keys[0].fmr_reserved)) ||
1665 memchr_inv(head.fmh_keys[1].fmr_reserved, 0,
1666 sizeof(head.fmh_keys[1].fmr_reserved)))
1667 return -EINVAL;
1668
1669 xhead.fmh_iflags = head.fmh_iflags;
1670 xhead.fmh_count = head.fmh_count;
1671 xfs_fsmap_to_internal(&xhead.fmh_keys[0], &head.fmh_keys[0]);
1672 xfs_fsmap_to_internal(&xhead.fmh_keys[1], &head.fmh_keys[1]);
1673
1674 trace_xfs_getfsmap_low_key(ip->i_mount, &xhead.fmh_keys[0]);
1675 trace_xfs_getfsmap_high_key(ip->i_mount, &xhead.fmh_keys[1]);
1676
1677 info.mp = ip->i_mount;
Christoph Hellwig9d17e142017-04-21 11:24:41 -07001678 info.data = arg;
Darrick J. Wonge89c0412017-03-28 14:56:37 -07001679 error = xfs_getfsmap(ip->i_mount, &xhead, xfs_getfsmap_format, &info);
1680 if (error == XFS_BTREE_QUERY_RANGE_ABORT) {
1681 error = 0;
1682 aborted = true;
1683 } else if (error)
1684 return error;
1685
1686 /* If we didn't abort, set the "last" flag in the last fmx */
Darrick J. Wong12e4a382017-04-23 10:45:21 -07001687 if (!aborted && info.idx) {
Darrick J. Wonge89c0412017-03-28 14:56:37 -07001688 info.last_flags |= FMR_OF_LAST;
Christoph Hellwig9d17e142017-04-21 11:24:41 -07001689 if (copy_to_user(&info.data->fmh_recs[info.idx - 1].fmr_flags,
1690 &info.last_flags, sizeof(info.last_flags)))
Darrick J. Wonge89c0412017-03-28 14:56:37 -07001691 return -EFAULT;
1692 }
1693
1694 /* copy back header */
1695 head.fmh_entries = xhead.fmh_entries;
1696 head.fmh_oflags = xhead.fmh_oflags;
1697 if (copy_to_user(arg, &head, sizeof(struct fsmap_head)))
1698 return -EFAULT;
1699
1700 return 0;
1701}
1702
Darrick J. Wong36fd6e82017-10-17 21:37:34 -07001703STATIC int
1704xfs_ioc_scrub_metadata(
1705 struct xfs_inode *ip,
1706 void __user *arg)
1707{
1708 struct xfs_scrub_metadata scrub;
1709 int error;
1710
1711 if (!capable(CAP_SYS_ADMIN))
1712 return -EPERM;
1713
1714 if (copy_from_user(&scrub, arg, sizeof(scrub)))
1715 return -EFAULT;
1716
1717 error = xfs_scrub_metadata(ip, &scrub);
1718 if (error)
1719 return error;
1720
1721 if (copy_to_user(arg, &scrub, sizeof(scrub)))
1722 return -EFAULT;
1723
1724 return 0;
1725}
1726
Dave Chinnera133d952013-08-12 20:49:48 +10001727int
1728xfs_ioc_swapext(
1729 xfs_swapext_t *sxp)
1730{
1731 xfs_inode_t *ip, *tip;
1732 struct fd f, tmp;
1733 int error = 0;
1734
1735 /* Pull information for the target fd */
1736 f = fdget((int)sxp->sx_fdtarget);
1737 if (!f.file) {
Dave Chinner24513372014-06-25 14:58:08 +10001738 error = -EINVAL;
Dave Chinnera133d952013-08-12 20:49:48 +10001739 goto out;
1740 }
1741
1742 if (!(f.file->f_mode & FMODE_WRITE) ||
1743 !(f.file->f_mode & FMODE_READ) ||
1744 (f.file->f_flags & O_APPEND)) {
Dave Chinner24513372014-06-25 14:58:08 +10001745 error = -EBADF;
Dave Chinnera133d952013-08-12 20:49:48 +10001746 goto out_put_file;
1747 }
1748
1749 tmp = fdget((int)sxp->sx_fdtmp);
1750 if (!tmp.file) {
Dave Chinner24513372014-06-25 14:58:08 +10001751 error = -EINVAL;
Dave Chinnera133d952013-08-12 20:49:48 +10001752 goto out_put_file;
1753 }
1754
1755 if (!(tmp.file->f_mode & FMODE_WRITE) ||
1756 !(tmp.file->f_mode & FMODE_READ) ||
1757 (tmp.file->f_flags & O_APPEND)) {
Dave Chinner24513372014-06-25 14:58:08 +10001758 error = -EBADF;
Dave Chinnera133d952013-08-12 20:49:48 +10001759 goto out_put_tmp_file;
1760 }
1761
1762 if (IS_SWAPFILE(file_inode(f.file)) ||
1763 IS_SWAPFILE(file_inode(tmp.file))) {
Dave Chinner24513372014-06-25 14:58:08 +10001764 error = -EINVAL;
Dave Chinnera133d952013-08-12 20:49:48 +10001765 goto out_put_tmp_file;
1766 }
1767
Jann Horn7f1b6242016-07-20 10:30:30 +10001768 /*
1769 * We need to ensure that the fds passed in point to XFS inodes
1770 * before we cast and access them as XFS structures as we have no
1771 * control over what the user passes us here.
1772 */
1773 if (f.file->f_op != &xfs_file_operations ||
1774 tmp.file->f_op != &xfs_file_operations) {
1775 error = -EINVAL;
1776 goto out_put_tmp_file;
1777 }
1778
Dave Chinnera133d952013-08-12 20:49:48 +10001779 ip = XFS_I(file_inode(f.file));
1780 tip = XFS_I(file_inode(tmp.file));
1781
1782 if (ip->i_mount != tip->i_mount) {
Dave Chinner24513372014-06-25 14:58:08 +10001783 error = -EINVAL;
Dave Chinnera133d952013-08-12 20:49:48 +10001784 goto out_put_tmp_file;
1785 }
1786
1787 if (ip->i_ino == tip->i_ino) {
Dave Chinner24513372014-06-25 14:58:08 +10001788 error = -EINVAL;
Dave Chinnera133d952013-08-12 20:49:48 +10001789 goto out_put_tmp_file;
1790 }
1791
1792 if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
Dave Chinner24513372014-06-25 14:58:08 +10001793 error = -EIO;
Dave Chinnera133d952013-08-12 20:49:48 +10001794 goto out_put_tmp_file;
1795 }
1796
1797 error = xfs_swap_extents(ip, tip, sxp);
1798
1799 out_put_tmp_file:
1800 fdput(tmp);
1801 out_put_file:
1802 fdput(f);
1803 out:
1804 return error;
1805}
1806
Eric Sandeenf7664b32018-05-15 13:21:48 -07001807static int
1808xfs_ioc_getlabel(
1809 struct xfs_mount *mp,
1810 char __user *user_label)
1811{
1812 struct xfs_sb *sbp = &mp->m_sb;
1813 char label[XFSLABEL_MAX + 1];
1814
1815 /* Paranoia */
1816 BUILD_BUG_ON(sizeof(sbp->sb_fname) > FSLABEL_MAX);
1817
Arnd Bergmann4bb8b652018-06-05 19:42:45 -07001818 /* 1 larger than sb_fname, so this ensures a trailing NUL char */
1819 memset(label, 0, sizeof(label));
Eric Sandeenf7664b32018-05-15 13:21:48 -07001820 spin_lock(&mp->m_sb_lock);
Arnd Bergmann4bb8b652018-06-05 19:42:45 -07001821 strncpy(label, sbp->sb_fname, XFSLABEL_MAX);
Eric Sandeenf7664b32018-05-15 13:21:48 -07001822 spin_unlock(&mp->m_sb_lock);
1823
Arnd Bergmann4bb8b652018-06-05 19:42:45 -07001824 if (copy_to_user(user_label, label, sizeof(label)))
Eric Sandeenf7664b32018-05-15 13:21:48 -07001825 return -EFAULT;
1826 return 0;
1827}
1828
1829static int
1830xfs_ioc_setlabel(
1831 struct file *filp,
1832 struct xfs_mount *mp,
1833 char __user *newlabel)
1834{
1835 struct xfs_sb *sbp = &mp->m_sb;
1836 char label[XFSLABEL_MAX + 1];
1837 size_t len;
1838 int error;
1839
1840 if (!capable(CAP_SYS_ADMIN))
1841 return -EPERM;
1842 /*
1843 * The generic ioctl allows up to FSLABEL_MAX chars, but XFS is much
1844 * smaller, at 12 bytes. We copy one more to be sure we find the
1845 * (required) NULL character to test the incoming label length.
1846 * NB: The on disk label doesn't need to be null terminated.
1847 */
1848 if (copy_from_user(label, newlabel, XFSLABEL_MAX + 1))
1849 return -EFAULT;
1850 len = strnlen(label, XFSLABEL_MAX + 1);
1851 if (len > sizeof(sbp->sb_fname))
1852 return -EINVAL;
1853
1854 error = mnt_want_write_file(filp);
1855 if (error)
1856 return error;
1857
1858 spin_lock(&mp->m_sb_lock);
1859 memset(sbp->sb_fname, 0, sizeof(sbp->sb_fname));
Arnd Bergmann4bb8b652018-06-05 19:42:45 -07001860 memcpy(sbp->sb_fname, label, len);
Eric Sandeenf7664b32018-05-15 13:21:48 -07001861 spin_unlock(&mp->m_sb_lock);
1862
1863 /*
1864 * Now we do several things to satisfy userspace.
1865 * In addition to normal logging of the primary superblock, we also
1866 * immediately write these changes to sector zero for the primary, then
1867 * update all backup supers (as xfs_db does for a label change), then
1868 * invalidate the block device page cache. This is so that any prior
1869 * buffered reads from userspace (i.e. from blkid) are invalidated,
1870 * and userspace will see the newly-written label.
1871 */
1872 error = xfs_sync_sb_buf(mp);
1873 if (error)
1874 goto out;
1875 /*
1876 * growfs also updates backup supers so lock against that.
1877 */
1878 mutex_lock(&mp->m_growlock);
1879 error = xfs_update_secondary_sbs(mp);
1880 mutex_unlock(&mp->m_growlock);
1881
1882 invalidate_bdev(mp->m_ddev_targp->bt_bdev);
1883
1884out:
1885 mnt_drop_write_file(filp);
1886 return error;
1887}
1888
Christoph Hellwig4d4be482008-12-09 04:47:33 -05001889/*
1890 * Note: some of the ioctl's return positive numbers as a
1891 * byte count indicating success, such as readlink_by_handle.
1892 * So we don't "sign flip" like most other routines. This means
1893 * true errors need to be returned as a negative value.
1894 */
1895long
1896xfs_file_ioctl(
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001897 struct file *filp,
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001898 unsigned int cmd,
Christoph Hellwig4d4be482008-12-09 04:47:33 -05001899 unsigned long p)
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001900{
Al Viro496ad9a2013-01-23 17:07:38 -05001901 struct inode *inode = file_inode(filp);
Christoph Hellwig4d4be482008-12-09 04:47:33 -05001902 struct xfs_inode *ip = XFS_I(inode);
1903 struct xfs_mount *mp = ip->i_mount;
1904 void __user *arg = (void __user *)p;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001905 int error;
1906
Christoph Hellwigcca28fb2010-06-24 11:57:09 +10001907 trace_xfs_file_ioctl(ip);
Christoph Hellwig4d4be482008-12-09 04:47:33 -05001908
1909 switch (cmd) {
Christoph Hellwiga46db602011-01-07 13:02:04 +00001910 case FITRIM:
1911 return xfs_ioc_trim(mp, arg);
Eric Sandeenf7664b32018-05-15 13:21:48 -07001912 case FS_IOC_GETFSLABEL:
1913 return xfs_ioc_getlabel(mp, arg);
1914 case FS_IOC_SETFSLABEL:
1915 return xfs_ioc_setlabel(filp, mp, arg);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001916 case XFS_IOC_ALLOCSP:
1917 case XFS_IOC_FREESP:
1918 case XFS_IOC_RESVSP:
1919 case XFS_IOC_UNRESVSP:
1920 case XFS_IOC_ALLOCSP64:
1921 case XFS_IOC_FREESP64:
1922 case XFS_IOC_RESVSP64:
Dave Chinner44722352010-08-24 12:02:11 +10001923 case XFS_IOC_UNRESVSP64:
1924 case XFS_IOC_ZERO_RANGE: {
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06001925 xfs_flock64_t bf;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001926
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06001927 if (copy_from_user(&bf, arg, sizeof(bf)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10001928 return -EFAULT;
Christoph Hellwig8f3e2052016-07-20 11:29:35 +10001929 return xfs_ioc_space(filp, cmd, &bf);
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06001930 }
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001931 case XFS_IOC_DIOINFO: {
1932 struct dioattr da;
1933 xfs_buftarg_t *target =
1934 XFS_IS_REALTIME_INODE(ip) ?
1935 mp->m_rtdev_targp : mp->m_ddev_targp;
1936
Eric Sandeen7c71ee72014-01-21 16:46:23 -06001937 da.d_mem = da.d_miniosz = target->bt_logical_sectorsize;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001938 da.d_maxiosz = INT_MAX & ~(da.d_miniosz - 1);
1939
1940 if (copy_to_user(arg, &da, sizeof(da)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10001941 return -EFAULT;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001942 return 0;
1943 }
1944
1945 case XFS_IOC_FSBULKSTAT_SINGLE:
1946 case XFS_IOC_FSBULKSTAT:
1947 case XFS_IOC_FSINUMBERS:
1948 return xfs_ioc_bulkstat(mp, cmd, arg);
1949
1950 case XFS_IOC_FSGEOMETRY_V1:
Dave Chinner1b6d9682019-04-12 07:41:16 -07001951 return xfs_ioc_fsgeometry(mp, arg, 3);
1952 case XFS_IOC_FSGEOMETRY_V4:
1953 return xfs_ioc_fsgeometry(mp, arg, 4);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001954 case XFS_IOC_FSGEOMETRY:
Dave Chinner1b6d9682019-04-12 07:41:16 -07001955 return xfs_ioc_fsgeometry(mp, arg, 5);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001956
Darrick J. Wong7cd50062019-04-12 07:41:17 -07001957 case XFS_IOC_AG_GEOMETRY:
1958 return xfs_ioc_ag_geometry(mp, arg);
1959
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001960 case XFS_IOC_GETVERSION:
1961 return put_user(inode->i_generation, (int __user *)arg);
1962
1963 case XFS_IOC_FSGETXATTR:
1964 return xfs_ioc_fsgetxattr(ip, 0, arg);
1965 case XFS_IOC_FSGETXATTRA:
1966 return xfs_ioc_fsgetxattr(ip, 1, arg);
Lachlan McIlroy3b2816b2008-04-18 12:43:35 +10001967 case XFS_IOC_FSSETXATTR:
Lachlan McIlroy65e67f52008-04-18 12:59:45 +10001968 return xfs_ioc_fssetxattr(ip, filp, arg);
1969 case XFS_IOC_GETXFLAGS:
1970 return xfs_ioc_getxflags(ip, arg);
1971 case XFS_IOC_SETXFLAGS:
1972 return xfs_ioc_setxflags(ip, filp, arg);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001973
1974 case XFS_IOC_FSSETDM: {
1975 struct fsdmidata dmi;
1976
1977 if (copy_from_user(&dmi, arg, sizeof(dmi)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10001978 return -EFAULT;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001979
Jan Karad9457dc2012-06-12 16:20:39 +02001980 error = mnt_want_write_file(filp);
1981 if (error)
1982 return error;
1983
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001984 error = xfs_set_dmattrs(ip, dmi.fsd_dmevmask,
1985 dmi.fsd_dmstate);
Jan Karad9457dc2012-06-12 16:20:39 +02001986 mnt_drop_write_file(filp);
Dave Chinner24513372014-06-25 14:58:08 +10001987 return error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001988 }
1989
1990 case XFS_IOC_GETBMAP:
1991 case XFS_IOC_GETBMAPA:
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001992 case XFS_IOC_GETBMAPX:
Christoph Hellwig232b51942017-10-17 14:16:19 -07001993 return xfs_ioc_getbmap(filp, cmd, arg);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001994
Darrick J. Wonge89c0412017-03-28 14:56:37 -07001995 case FS_IOC_GETFSMAP:
1996 return xfs_ioc_getfsmap(ip, arg);
1997
Darrick J. Wong36fd6e82017-10-17 21:37:34 -07001998 case XFS_IOC_SCRUB_METADATA:
1999 return xfs_ioc_scrub_metadata(ip, arg);
2000
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002001 case XFS_IOC_FD_TO_HANDLE:
2002 case XFS_IOC_PATH_TO_HANDLE:
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06002003 case XFS_IOC_PATH_TO_FSHANDLE: {
2004 xfs_fsop_handlereq_t hreq;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002005
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06002006 if (copy_from_user(&hreq, arg, sizeof(hreq)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002007 return -EFAULT;
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06002008 return xfs_find_handle(cmd, &hreq);
2009 }
2010 case XFS_IOC_OPEN_BY_HANDLE: {
2011 xfs_fsop_handlereq_t hreq;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002012
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06002013 if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002014 return -EFAULT;
Christoph Hellwigd296d302009-01-19 02:02:57 +01002015 return xfs_open_by_handle(filp, &hreq);
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06002016 }
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002017 case XFS_IOC_FSSETDM_BY_HANDLE:
Christoph Hellwigd296d302009-01-19 02:02:57 +01002018 return xfs_fssetdm_by_handle(filp, arg);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002019
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06002020 case XFS_IOC_READLINK_BY_HANDLE: {
2021 xfs_fsop_handlereq_t hreq;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002022
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06002023 if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002024 return -EFAULT;
Christoph Hellwigd296d302009-01-19 02:02:57 +01002025 return xfs_readlink_by_handle(filp, &hreq);
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06002026 }
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002027 case XFS_IOC_ATTRLIST_BY_HANDLE:
Christoph Hellwigd296d302009-01-19 02:02:57 +01002028 return xfs_attrlist_by_handle(filp, arg);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002029
2030 case XFS_IOC_ATTRMULTI_BY_HANDLE:
Christoph Hellwigd296d302009-01-19 02:02:57 +01002031 return xfs_attrmulti_by_handle(filp, arg);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002032
2033 case XFS_IOC_SWAPEXT: {
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06002034 struct xfs_swapext sxp;
2035
2036 if (copy_from_user(&sxp, arg, sizeof(xfs_swapext_t)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002037 return -EFAULT;
Jan Karad9457dc2012-06-12 16:20:39 +02002038 error = mnt_want_write_file(filp);
2039 if (error)
2040 return error;
Dave Chinnera133d952013-08-12 20:49:48 +10002041 error = xfs_ioc_swapext(&sxp);
Jan Karad9457dc2012-06-12 16:20:39 +02002042 mnt_drop_write_file(filp);
Dave Chinner24513372014-06-25 14:58:08 +10002043 return error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002044 }
2045
2046 case XFS_IOC_FSCOUNTS: {
2047 xfs_fsop_counts_t out;
2048
2049 error = xfs_fs_counts(mp, &out);
2050 if (error)
Dave Chinner24513372014-06-25 14:58:08 +10002051 return error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002052
2053 if (copy_to_user(arg, &out, sizeof(out)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002054 return -EFAULT;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002055 return 0;
2056 }
2057
2058 case XFS_IOC_SET_RESBLKS: {
2059 xfs_fsop_resblks_t inout;
Darrick J. Wongc8ce5402017-06-16 11:00:05 -07002060 uint64_t in;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002061
2062 if (!capable(CAP_SYS_ADMIN))
2063 return -EPERM;
2064
Eric Sandeend5db0f92010-02-05 22:59:53 +00002065 if (mp->m_flags & XFS_MOUNT_RDONLY)
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002066 return -EROFS;
Eric Sandeend5db0f92010-02-05 22:59:53 +00002067
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002068 if (copy_from_user(&inout, arg, sizeof(inout)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002069 return -EFAULT;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002070
Jan Karad9457dc2012-06-12 16:20:39 +02002071 error = mnt_want_write_file(filp);
2072 if (error)
2073 return error;
2074
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002075 /* input parameter is passed in resblks field of structure */
2076 in = inout.resblks;
2077 error = xfs_reserve_blocks(mp, &in, &inout);
Jan Karad9457dc2012-06-12 16:20:39 +02002078 mnt_drop_write_file(filp);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002079 if (error)
Dave Chinner24513372014-06-25 14:58:08 +10002080 return error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002081
2082 if (copy_to_user(arg, &inout, sizeof(inout)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002083 return -EFAULT;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002084 return 0;
2085 }
2086
2087 case XFS_IOC_GET_RESBLKS: {
2088 xfs_fsop_resblks_t out;
2089
2090 if (!capable(CAP_SYS_ADMIN))
2091 return -EPERM;
2092
2093 error = xfs_reserve_blocks(mp, NULL, &out);
2094 if (error)
Dave Chinner24513372014-06-25 14:58:08 +10002095 return error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002096
2097 if (copy_to_user(arg, &out, sizeof(out)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002098 return -EFAULT;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002099
2100 return 0;
2101 }
2102
2103 case XFS_IOC_FSGROWFSDATA: {
2104 xfs_growfs_data_t in;
2105
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002106 if (copy_from_user(&in, arg, sizeof(in)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002107 return -EFAULT;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002108
Jan Karad9457dc2012-06-12 16:20:39 +02002109 error = mnt_want_write_file(filp);
2110 if (error)
2111 return error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002112 error = xfs_growfs_data(mp, &in);
Jan Karad9457dc2012-06-12 16:20:39 +02002113 mnt_drop_write_file(filp);
Dave Chinner24513372014-06-25 14:58:08 +10002114 return error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002115 }
2116
2117 case XFS_IOC_FSGROWFSLOG: {
2118 xfs_growfs_log_t in;
2119
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002120 if (copy_from_user(&in, arg, sizeof(in)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002121 return -EFAULT;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002122
Jan Karad9457dc2012-06-12 16:20:39 +02002123 error = mnt_want_write_file(filp);
2124 if (error)
2125 return error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002126 error = xfs_growfs_log(mp, &in);
Jan Karad9457dc2012-06-12 16:20:39 +02002127 mnt_drop_write_file(filp);
Dave Chinner24513372014-06-25 14:58:08 +10002128 return error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002129 }
2130
2131 case XFS_IOC_FSGROWFSRT: {
2132 xfs_growfs_rt_t in;
2133
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002134 if (copy_from_user(&in, arg, sizeof(in)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002135 return -EFAULT;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002136
Jan Karad9457dc2012-06-12 16:20:39 +02002137 error = mnt_want_write_file(filp);
2138 if (error)
2139 return error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002140 error = xfs_growfs_rt(mp, &in);
Jan Karad9457dc2012-06-12 16:20:39 +02002141 mnt_drop_write_file(filp);
Dave Chinner24513372014-06-25 14:58:08 +10002142 return error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002143 }
2144
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002145 case XFS_IOC_GOINGDOWN: {
Darrick J. Wongc8ce5402017-06-16 11:00:05 -07002146 uint32_t in;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002147
2148 if (!capable(CAP_SYS_ADMIN))
2149 return -EPERM;
2150
Darrick J. Wongc8ce5402017-06-16 11:00:05 -07002151 if (get_user(in, (uint32_t __user *)arg))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002152 return -EFAULT;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002153
Dave Chinner24513372014-06-25 14:58:08 +10002154 return xfs_fs_goingdown(mp, in);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002155 }
2156
2157 case XFS_IOC_ERROR_INJECTION: {
2158 xfs_error_injection_t in;
2159
2160 if (!capable(CAP_SYS_ADMIN))
2161 return -EPERM;
2162
2163 if (copy_from_user(&in, arg, sizeof(in)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002164 return -EFAULT;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002165
Darrick J. Wong31965ef2017-06-20 17:54:46 -07002166 return xfs_errortag_add(mp, in.errtag);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002167 }
2168
2169 case XFS_IOC_ERROR_CLEARALL:
2170 if (!capable(CAP_SYS_ADMIN))
2171 return -EPERM;
2172
Darrick J. Wong31965ef2017-06-20 17:54:46 -07002173 return xfs_errortag_clearall(mp);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002174
Brian Foster8ca149d2012-11-07 12:21:12 -05002175 case XFS_IOC_FREE_EOFBLOCKS: {
Dwight Engenb9fe5052013-08-15 14:08:02 -04002176 struct xfs_fs_eofblocks eofb;
2177 struct xfs_eofblocks keofb;
Brian Foster8ca149d2012-11-07 12:21:12 -05002178
Dwight Engen8c567a72013-08-15 14:08:03 -04002179 if (!capable(CAP_SYS_ADMIN))
2180 return -EPERM;
2181
2182 if (mp->m_flags & XFS_MOUNT_RDONLY)
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002183 return -EROFS;
Dwight Engen8c567a72013-08-15 14:08:03 -04002184
Brian Foster8ca149d2012-11-07 12:21:12 -05002185 if (copy_from_user(&eofb, arg, sizeof(eofb)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002186 return -EFAULT;
Brian Foster8ca149d2012-11-07 12:21:12 -05002187
Dwight Engenb9fe5052013-08-15 14:08:02 -04002188 error = xfs_fs_eofblocks_from_user(&eofb, &keofb);
2189 if (error)
Dave Chinner24513372014-06-25 14:58:08 +10002190 return error;
Brian Foster8ca149d2012-11-07 12:21:12 -05002191
Dave Chinner24513372014-06-25 14:58:08 +10002192 return xfs_icache_free_eofblocks(mp, &keofb);
Brian Foster8ca149d2012-11-07 12:21:12 -05002193 }
2194
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002195 default:
2196 return -ENOTTY;
2197 }
2198}