blob: 6ecdbb3af7de5c02c86a25d41ed7086ae2f845fc [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080037#include <linux/capability.h>
Ingo Molnar5b825c32017-02-02 17:54:15 +010038#include <linux/cred.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/dcache.h>
40#include <linux/mount.h>
41#include <linux/namei.h>
42#include <linux/pagemap.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090043#include <linux/slab.h>
Christoph Hellwigd296d302009-01-19 02:02:57 +010044#include <linux/exportfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46/*
47 * xfs_find_handle maps from userspace xfs_fsop_handlereq structure to
48 * a file or fs handle.
49 *
50 * XFS_IOC_PATH_TO_FSHANDLE
51 * returns fs handle for a mount point or path within that mount point
52 * XFS_IOC_FD_TO_HANDLE
53 * returns full handle for a FD opened in user space
54 * XFS_IOC_PATH_TO_HANDLE
55 * returns full handle for a path
56 */
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -060057int
Linus Torvalds1da177e2005-04-16 15:20:36 -070058xfs_find_handle(
59 unsigned int cmd,
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -060060 xfs_fsop_handlereq_t *hreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
62 int hsize;
63 xfs_handle_t handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 struct inode *inode;
Dave Chinnera30b0362013-09-02 20:49:36 +100065 struct fd f = {NULL};
Christoph Hellwig4346cdd2009-02-08 21:51:14 +010066 struct path path;
Al Viro2903ff02012-08-28 12:52:22 -040067 int error;
Christoph Hellwig4346cdd2009-02-08 21:51:14 +010068 struct xfs_inode *ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Christoph Hellwig4346cdd2009-02-08 21:51:14 +010070 if (cmd == XFS_IOC_FD_TO_HANDLE) {
Al Viro2903ff02012-08-28 12:52:22 -040071 f = fdget(hreq->fd);
72 if (!f.file)
Christoph Hellwig4346cdd2009-02-08 21:51:14 +010073 return -EBADF;
Al Viro496ad9a2013-01-23 17:07:38 -050074 inode = file_inode(f.file);
Christoph Hellwig4346cdd2009-02-08 21:51:14 +010075 } else {
76 error = user_lpath((const char __user *)hreq->path, &path);
77 if (error)
78 return error;
David Howells2b0143b2015-03-17 22:25:59 +000079 inode = d_inode(path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 }
Christoph Hellwig4346cdd2009-02-08 21:51:14 +010081 ip = XFS_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Christoph Hellwig4346cdd2009-02-08 21:51:14 +010083 /*
84 * We can only generate handles for inodes residing on a XFS filesystem,
85 * and only for regular files, directories or symbolic links.
86 */
87 error = -EINVAL;
88 if (inode->i_sb->s_magic != XFS_SB_MAGIC)
89 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Christoph Hellwig4346cdd2009-02-08 21:51:14 +010091 error = -EBADF;
92 if (!S_ISREG(inode->i_mode) &&
93 !S_ISDIR(inode->i_mode) &&
94 !S_ISLNK(inode->i_mode))
95 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Christoph Hellwig4346cdd2009-02-08 21:51:14 +010098 memcpy(&handle.ha_fsid, ip->i_mount->m_fixedfsid, sizeof(xfs_fsid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Christoph Hellwig4346cdd2009-02-08 21:51:14 +0100100 if (cmd == XFS_IOC_PATH_TO_FSHANDLE) {
101 /*
102 * This handle only contains an fsid, zero the rest.
103 */
104 memset(&handle.ha_fid, 0, sizeof(handle.ha_fid));
105 hsize = sizeof(xfs_fsid_t);
106 } else {
Christoph Hellwigc6143912007-09-14 15:22:37 +1000107 handle.ha_fid.fid_len = sizeof(xfs_fid_t) -
108 sizeof(handle.ha_fid.fid_len);
109 handle.ha_fid.fid_pad = 0;
Dave Chinner9e9a2672016-02-09 16:54:58 +1100110 handle.ha_fid.fid_gen = inode->i_generation;
Christoph Hellwigc6143912007-09-14 15:22:37 +1000111 handle.ha_fid.fid_ino = ip->i_ino;
Christoph Hellwig3398a402017-06-14 21:30:44 -0700112 hsize = sizeof(xfs_handle_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 }
114
Christoph Hellwig4346cdd2009-02-08 21:51:14 +0100115 error = -EFAULT;
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -0600116 if (copy_to_user(hreq->ohandle, &handle, hsize) ||
Christoph Hellwig4346cdd2009-02-08 21:51:14 +0100117 copy_to_user(hreq->ohandlen, &hsize, sizeof(__s32)))
118 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Christoph Hellwig4346cdd2009-02-08 21:51:14 +0100120 error = 0;
121
122 out_put:
123 if (cmd == XFS_IOC_FD_TO_HANDLE)
Al Viro2903ff02012-08-28 12:52:22 -0400124 fdput(f);
Christoph Hellwig4346cdd2009-02-08 21:51:14 +0100125 else
126 path_put(&path);
127 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128}
129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130/*
Christoph Hellwigd296d302009-01-19 02:02:57 +0100131 * No need to do permission checks on the various pathname components
132 * as the handle operations are privileged.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 */
134STATIC int
Christoph Hellwigd296d302009-01-19 02:02:57 +0100135xfs_handle_acceptable(
136 void *context,
137 struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
Christoph Hellwigd296d302009-01-19 02:02:57 +0100139 return 1;
140}
141
142/*
143 * Convert userspace handle data into a dentry.
144 */
145struct dentry *
146xfs_handle_to_dentry(
147 struct file *parfilp,
148 void __user *uhandle,
149 u32 hlen)
150{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 xfs_handle_t handle;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100152 struct xfs_fid64 fid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154 /*
155 * Only allow handle opens under a directory.
156 */
Al Viro496ad9a2013-01-23 17:07:38 -0500157 if (!S_ISDIR(file_inode(parfilp)->i_mode))
Christoph Hellwigd296d302009-01-19 02:02:57 +0100158 return ERR_PTR(-ENOTDIR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Christoph Hellwigd296d302009-01-19 02:02:57 +0100160 if (hlen != sizeof(xfs_handle_t))
161 return ERR_PTR(-EINVAL);
162 if (copy_from_user(&handle, uhandle, hlen))
163 return ERR_PTR(-EFAULT);
164 if (handle.ha_fid.fid_len !=
165 sizeof(handle.ha_fid) - sizeof(handle.ha_fid.fid_len))
166 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Christoph Hellwigd296d302009-01-19 02:02:57 +0100168 memset(&fid, 0, sizeof(struct fid));
169 fid.ino = handle.ha_fid.fid_ino;
170 fid.gen = handle.ha_fid.fid_gen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Christoph Hellwigd296d302009-01-19 02:02:57 +0100172 return exportfs_decode_fh(parfilp->f_path.mnt, (struct fid *)&fid, 3,
173 FILEID_INO32_GEN | XFS_FILEID_TYPE_64FLAG,
174 xfs_handle_acceptable, NULL);
175}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Christoph Hellwigd296d302009-01-19 02:02:57 +0100177STATIC struct dentry *
178xfs_handlereq_to_dentry(
179 struct file *parfilp,
180 xfs_fsop_handlereq_t *hreq)
181{
182 return xfs_handle_to_dentry(parfilp, hreq->ihandle, hreq->ihandlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183}
184
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -0600185int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186xfs_open_by_handle(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 struct file *parfilp,
Christoph Hellwigd296d302009-01-19 02:02:57 +0100188 xfs_fsop_handlereq_t *hreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
David Howells745ca242008-11-14 10:39:22 +1100190 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 int error;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100192 int fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 int permflag;
194 struct file *filp;
195 struct inode *inode;
196 struct dentry *dentry;
Dave Chinner1a1d7722012-03-22 05:15:06 +0000197 fmode_t fmode;
Al Viro765927b2012-06-26 21:58:53 +0400198 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200 if (!capable(CAP_SYS_ADMIN))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000201 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Christoph Hellwigd296d302009-01-19 02:02:57 +0100203 dentry = xfs_handlereq_to_dentry(parfilp, hreq);
204 if (IS_ERR(dentry))
205 return PTR_ERR(dentry);
David Howells2b0143b2015-03-17 22:25:59 +0000206 inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
208 /* Restrict xfs_open_by_handle to directories & regular files. */
209 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode))) {
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000210 error = -EPERM;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100211 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 }
213
214#if BITS_PER_LONG != 32
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -0600215 hreq->oflags |= O_LARGEFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216#endif
Christoph Hellwigd296d302009-01-19 02:02:57 +0100217
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -0600218 permflag = hreq->oflags;
Dave Chinner1a1d7722012-03-22 05:15:06 +0000219 fmode = OPEN_FMODE(permflag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 if ((!(permflag & O_APPEND) || (permflag & O_TRUNC)) &&
Dave Chinner1a1d7722012-03-22 05:15:06 +0000221 (fmode & FMODE_WRITE) && IS_APPEND(inode)) {
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000222 error = -EPERM;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100223 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 }
225
Dave Chinner1a1d7722012-03-22 05:15:06 +0000226 if ((fmode & FMODE_WRITE) && IS_IMMUTABLE(inode)) {
Eryu Guan337684a2016-08-02 19:58:28 +0800227 error = -EPERM;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100228 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 }
230
231 /* Can't write directories. */
Dave Chinner1a1d7722012-03-22 05:15:06 +0000232 if (S_ISDIR(inode->i_mode) && (fmode & FMODE_WRITE)) {
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000233 error = -EISDIR;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100234 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 }
236
Yann Droneaud862a6292013-07-02 18:39:34 +0200237 fd = get_unused_fd_flags(0);
Christoph Hellwigd296d302009-01-19 02:02:57 +0100238 if (fd < 0) {
239 error = fd;
240 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 }
242
Al Viro765927b2012-06-26 21:58:53 +0400243 path.mnt = parfilp->f_path.mnt;
244 path.dentry = dentry;
245 filp = dentry_open(&path, hreq->oflags, cred);
246 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 if (IS_ERR(filp)) {
Christoph Hellwigd296d302009-01-19 02:02:57 +0100248 put_unused_fd(fd);
249 return PTR_ERR(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 }
Christoph Hellwig4d4be482008-12-09 04:47:33 -0500251
Al Viro03209372011-07-25 20:54:24 -0400252 if (S_ISREG(inode->i_mode)) {
Vlad Apostolov2e2e7bb2006-11-11 18:04:47 +1100253 filp->f_flags |= O_NOATIME;
Christoph Hellwig4d4be482008-12-09 04:47:33 -0500254 filp->f_mode |= FMODE_NOCMTIME;
Vlad Apostolov2e2e7bb2006-11-11 18:04:47 +1100255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Christoph Hellwigd296d302009-01-19 02:02:57 +0100257 fd_install(fd, filp);
258 return fd;
259
260 out_dput:
261 dput(dentry);
262 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263}
264
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -0600265int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266xfs_readlink_by_handle(
Christoph Hellwigd296d302009-01-19 02:02:57 +0100267 struct file *parfilp,
268 xfs_fsop_handlereq_t *hreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269{
Christoph Hellwigd296d302009-01-19 02:02:57 +0100270 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 __u32 olen;
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000272 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
274 if (!capable(CAP_SYS_ADMIN))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000275 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Christoph Hellwigd296d302009-01-19 02:02:57 +0100277 dentry = xfs_handlereq_to_dentry(parfilp, hreq);
278 if (IS_ERR(dentry))
279 return PTR_ERR(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281 /* Restrict this handle operation to symlinks only. */
Miklos Szeredifd4a0edf2016-12-09 16:45:04 +0100282 if (!d_is_symlink(dentry)) {
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000283 error = -EINVAL;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100284 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 }
286
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -0600287 if (copy_from_user(&olen, hreq->ohandlen, sizeof(__u32))) {
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000288 error = -EFAULT;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100289 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Miklos Szeredifd4a0edf2016-12-09 16:45:04 +0100292 error = vfs_readlink(dentry, hreq->ohandle, olen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Christoph Hellwigd296d302009-01-19 02:02:57 +0100294 out_dput:
295 dput(dentry);
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000296 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
298
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000299int
300xfs_set_dmattrs(
301 xfs_inode_t *ip,
Darrick J. Wong65a79352017-11-09 09:34:28 -0800302 uint evmask,
303 uint16_t state)
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000304{
305 xfs_mount_t *mp = ip->i_mount;
306 xfs_trans_t *tp;
307 int error;
308
309 if (!capable(CAP_SYS_ADMIN))
Dave Chinner24513372014-06-25 14:58:08 +1000310 return -EPERM;
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000311
312 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +1000313 return -EIO;
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000314
Christoph Hellwig253f4912016-04-06 09:19:55 +1000315 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
316 if (error)
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000317 return error;
Christoph Hellwig253f4912016-04-06 09:19:55 +1000318
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000319 xfs_ilock(ip, XFS_ILOCK_EXCL);
320 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
321
322 ip->i_d.di_dmevmask = evmask;
323 ip->i_d.di_dmstate = state;
324
325 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Christoph Hellwig70393312015-06-04 13:48:08 +1000326 error = xfs_trans_commit(tp);
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000327
328 return error;
329}
330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331STATIC int
332xfs_fssetdm_by_handle(
Christoph Hellwigd296d302009-01-19 02:02:57 +0100333 struct file *parfilp,
334 void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
336 int error;
337 struct fsdmidata fsd;
338 xfs_fsop_setdm_handlereq_t dmhreq;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100339 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 if (!capable(CAP_MKNOD))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000342 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 if (copy_from_user(&dmhreq, arg, sizeof(xfs_fsop_setdm_handlereq_t)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000344 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Jan Karad9457dc2012-06-12 16:20:39 +0200346 error = mnt_want_write_file(parfilp);
347 if (error)
348 return error;
349
Christoph Hellwigd296d302009-01-19 02:02:57 +0100350 dentry = xfs_handlereq_to_dentry(parfilp, &dmhreq.hreq);
Jan Karad9457dc2012-06-12 16:20:39 +0200351 if (IS_ERR(dentry)) {
352 mnt_drop_write_file(parfilp);
Christoph Hellwigd296d302009-01-19 02:02:57 +0100353 return PTR_ERR(dentry);
Jan Karad9457dc2012-06-12 16:20:39 +0200354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
David Howells2b0143b2015-03-17 22:25:59 +0000356 if (IS_IMMUTABLE(d_inode(dentry)) || IS_APPEND(d_inode(dentry))) {
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000357 error = -EPERM;
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000358 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 }
360
361 if (copy_from_user(&fsd, dmhreq.data, sizeof(fsd))) {
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000362 error = -EFAULT;
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000363 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 }
365
David Howells2b0143b2015-03-17 22:25:59 +0000366 error = xfs_set_dmattrs(XFS_I(d_inode(dentry)), fsd.fsd_dmevmask,
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000367 fsd.fsd_dmstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000369 out:
Jan Karad9457dc2012-06-12 16:20:39 +0200370 mnt_drop_write_file(parfilp);
Christoph Hellwigd296d302009-01-19 02:02:57 +0100371 dput(dentry);
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000372 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373}
374
375STATIC int
376xfs_attrlist_by_handle(
Christoph Hellwigd296d302009-01-19 02:02:57 +0100377 struct file *parfilp,
378 void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
Christoph Hellwigd296d302009-01-19 02:02:57 +0100380 int error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 attrlist_cursor_kern_t *cursor;
Darrick J. Wong0facef72016-08-03 10:58:53 +1000382 struct xfs_fsop_attrlist_handlereq __user *p = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 xfs_fsop_attrlist_handlereq_t al_hreq;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100384 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 char *kbuf;
386
387 if (!capable(CAP_SYS_ADMIN))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000388 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 if (copy_from_user(&al_hreq, arg, sizeof(xfs_fsop_attrlist_handlereq_t)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000390 return -EFAULT;
Dan Carpenter071c5292013-10-31 21:00:10 +0300391 if (al_hreq.buflen < sizeof(struct attrlist) ||
Jan Tulak4e247612015-10-12 16:02:56 +1100392 al_hreq.buflen > XFS_XATTR_LIST_MAX)
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000393 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Christoph Hellwig90ad58a2008-06-27 13:32:19 +1000395 /*
396 * Reject flags, only allow namespaces.
397 */
398 if (al_hreq.flags & ~(ATTR_ROOT | ATTR_SECURE))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000399 return -EINVAL;
Christoph Hellwig90ad58a2008-06-27 13:32:19 +1000400
Christoph Hellwigd296d302009-01-19 02:02:57 +0100401 dentry = xfs_handlereq_to_dentry(parfilp, &al_hreq.hreq);
402 if (IS_ERR(dentry))
403 return PTR_ERR(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Dave Chinnerfdd3cce2013-09-02 20:53:00 +1000405 kbuf = kmem_zalloc_large(al_hreq.buflen, KM_SLEEP);
406 if (!kbuf)
407 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409 cursor = (attrlist_cursor_kern_t *)&al_hreq.pos;
David Howells2b0143b2015-03-17 22:25:59 +0000410 error = xfs_attr_list(XFS_I(d_inode(dentry)), kbuf, al_hreq.buflen,
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000411 al_hreq.flags, cursor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 if (error)
413 goto out_kfree;
414
Darrick J. Wong0facef72016-08-03 10:58:53 +1000415 if (copy_to_user(&p->pos, cursor, sizeof(attrlist_cursor_kern_t))) {
416 error = -EFAULT;
417 goto out_kfree;
418 }
419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 if (copy_to_user(al_hreq.buffer, kbuf, al_hreq.buflen))
421 error = -EFAULT;
422
Dave Chinnerfdd3cce2013-09-02 20:53:00 +1000423out_kfree:
424 kmem_free(kbuf);
425out_dput:
Christoph Hellwigd296d302009-01-19 02:02:57 +0100426 dput(dentry);
427 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428}
429
sandeen@sandeen.net28750972008-11-25 21:20:15 -0600430int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431xfs_attrmulti_attr_get(
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000432 struct inode *inode,
Dave Chinnera9273ca2010-01-20 10:47:48 +1100433 unsigned char *name,
434 unsigned char __user *ubuf,
Darrick J. Wongc8ce5402017-06-16 11:00:05 -0700435 uint32_t *len,
436 uint32_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
Dave Chinnera9273ca2010-01-20 10:47:48 +1100438 unsigned char *kbuf;
Dave Chinner24513372014-06-25 14:58:08 +1000439 int error = -EFAULT;
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000440
Jan Tulak51fcbfe2015-10-12 16:03:59 +1100441 if (*len > XFS_XATTR_SIZE_MAX)
Dave Chinner24513372014-06-25 14:58:08 +1000442 return -EINVAL;
Dave Chinnerfdd3cce2013-09-02 20:53:00 +1000443 kbuf = kmem_zalloc_large(*len, KM_SLEEP);
444 if (!kbuf)
Dave Chinner24513372014-06-25 14:58:08 +1000445 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000447 error = xfs_attr_get(XFS_I(inode), name, kbuf, (int *)len, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 if (error)
449 goto out_kfree;
450
451 if (copy_to_user(ubuf, kbuf, *len))
Dave Chinner24513372014-06-25 14:58:08 +1000452 error = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Dave Chinnerfdd3cce2013-09-02 20:53:00 +1000454out_kfree:
455 kmem_free(kbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 return error;
457}
458
sandeen@sandeen.net28750972008-11-25 21:20:15 -0600459int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460xfs_attrmulti_attr_set(
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000461 struct inode *inode,
Dave Chinnera9273ca2010-01-20 10:47:48 +1100462 unsigned char *name,
463 const unsigned char __user *ubuf,
Darrick J. Wongc8ce5402017-06-16 11:00:05 -0700464 uint32_t len,
465 uint32_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
Dave Chinnera9273ca2010-01-20 10:47:48 +1100467 unsigned char *kbuf;
Andreas Gruenbacher09cb22d2015-11-03 12:53:54 +1100468 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000470 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
Dave Chinner24513372014-06-25 14:58:08 +1000471 return -EPERM;
Jan Tulak51fcbfe2015-10-12 16:03:59 +1100472 if (len > XFS_XATTR_SIZE_MAX)
Dave Chinner24513372014-06-25 14:58:08 +1000473 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Li Zefan0e639bd2009-04-08 15:08:04 +0800475 kbuf = memdup_user(ubuf, len);
476 if (IS_ERR(kbuf))
477 return PTR_ERR(kbuf);
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000478
Andreas Gruenbacher09cb22d2015-11-03 12:53:54 +1100479 error = xfs_attr_set(XFS_I(inode), name, kbuf, len, flags);
Andreas Gruenbacher47e1bf62015-11-03 12:56:17 +1100480 if (!error)
481 xfs_forget_acl(inode, name, flags);
Andreas Gruenbacher09cb22d2015-11-03 12:53:54 +1100482 kfree(kbuf);
483 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484}
485
sandeen@sandeen.net28750972008-11-25 21:20:15 -0600486int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487xfs_attrmulti_attr_remove(
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000488 struct inode *inode,
Dave Chinnera9273ca2010-01-20 10:47:48 +1100489 unsigned char *name,
Darrick J. Wongc8ce5402017-06-16 11:00:05 -0700490 uint32_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
Andreas Gruenbacher47e1bf62015-11-03 12:56:17 +1100492 int error;
493
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000494 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
Dave Chinner24513372014-06-25 14:58:08 +1000495 return -EPERM;
Andreas Gruenbacher47e1bf62015-11-03 12:56:17 +1100496 error = xfs_attr_remove(XFS_I(inode), name, flags);
497 if (!error)
498 xfs_forget_acl(inode, name, flags);
499 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500}
501
502STATIC int
503xfs_attrmulti_by_handle(
Dave Hansen42a74f22008-02-15 14:37:46 -0800504 struct file *parfilp,
Christoph Hellwigd296d302009-01-19 02:02:57 +0100505 void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
507 int error;
508 xfs_attr_multiop_t *ops;
509 xfs_fsop_attrmulti_handlereq_t am_hreq;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100510 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 unsigned int i, size;
Dave Chinnera9273ca2010-01-20 10:47:48 +1100512 unsigned char *attr_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
514 if (!capable(CAP_SYS_ADMIN))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000515 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 if (copy_from_user(&am_hreq, arg, sizeof(xfs_fsop_attrmulti_handlereq_t)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000517 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Zhitong Wangfda168c2010-03-23 09:51:22 +1100519 /* overflow check */
520 if (am_hreq.opcount >= INT_MAX / sizeof(xfs_attr_multiop_t))
521 return -E2BIG;
522
Christoph Hellwigd296d302009-01-19 02:02:57 +0100523 dentry = xfs_handlereq_to_dentry(parfilp, &am_hreq.hreq);
524 if (IS_ERR(dentry))
525 return PTR_ERR(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Dave Chinner24513372014-06-25 14:58:08 +1000527 error = -E2BIG;
Christoph Hellwige182f572008-06-27 13:32:31 +1000528 size = am_hreq.opcount * sizeof(xfs_attr_multiop_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 if (!size || size > 16 * PAGE_SIZE)
Christoph Hellwigd296d302009-01-19 02:02:57 +0100530 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Li Zefan0e639bd2009-04-08 15:08:04 +0800532 ops = memdup_user(am_hreq.ops, size);
533 if (IS_ERR(ops)) {
Dave Chinner24513372014-06-25 14:58:08 +1000534 error = PTR_ERR(ops);
Christoph Hellwigd296d302009-01-19 02:02:57 +0100535 goto out_dput;
Li Zefan0e639bd2009-04-08 15:08:04 +0800536 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Dave Chinner24513372014-06-25 14:58:08 +1000538 error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 attr_name = kmalloc(MAXNAMELEN, GFP_KERNEL);
540 if (!attr_name)
541 goto out_kfree_ops;
542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 error = 0;
544 for (i = 0; i < am_hreq.opcount; i++) {
Dave Chinnera9273ca2010-01-20 10:47:48 +1100545 ops[i].am_error = strncpy_from_user((char *)attr_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 ops[i].am_attrname, MAXNAMELEN);
547 if (ops[i].am_error == 0 || ops[i].am_error == MAXNAMELEN)
Dave Chinner24513372014-06-25 14:58:08 +1000548 error = -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 if (ops[i].am_error < 0)
550 break;
551
552 switch (ops[i].am_opcode) {
553 case ATTR_OP_GET:
Christoph Hellwigd296d302009-01-19 02:02:57 +0100554 ops[i].am_error = xfs_attrmulti_attr_get(
David Howells2b0143b2015-03-17 22:25:59 +0000555 d_inode(dentry), attr_name,
Christoph Hellwigd296d302009-01-19 02:02:57 +0100556 ops[i].am_attrvalue, &ops[i].am_length,
557 ops[i].am_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 break;
559 case ATTR_OP_SET:
Al Viroa561be72011-11-23 11:57:51 -0500560 ops[i].am_error = mnt_want_write_file(parfilp);
Dave Hansen42a74f22008-02-15 14:37:46 -0800561 if (ops[i].am_error)
562 break;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100563 ops[i].am_error = xfs_attrmulti_attr_set(
David Howells2b0143b2015-03-17 22:25:59 +0000564 d_inode(dentry), attr_name,
Christoph Hellwigd296d302009-01-19 02:02:57 +0100565 ops[i].am_attrvalue, ops[i].am_length,
566 ops[i].am_flags);
Al Viro2a79f172011-12-09 08:06:57 -0500567 mnt_drop_write_file(parfilp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 break;
569 case ATTR_OP_REMOVE:
Al Viroa561be72011-11-23 11:57:51 -0500570 ops[i].am_error = mnt_want_write_file(parfilp);
Dave Hansen42a74f22008-02-15 14:37:46 -0800571 if (ops[i].am_error)
572 break;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100573 ops[i].am_error = xfs_attrmulti_attr_remove(
David Howells2b0143b2015-03-17 22:25:59 +0000574 d_inode(dentry), attr_name,
Christoph Hellwigd296d302009-01-19 02:02:57 +0100575 ops[i].am_flags);
Al Viro2a79f172011-12-09 08:06:57 -0500576 mnt_drop_write_file(parfilp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 break;
578 default:
Dave Chinner24513372014-06-25 14:58:08 +1000579 ops[i].am_error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 }
581 }
582
583 if (copy_to_user(am_hreq.ops, ops, size))
Dave Chinner24513372014-06-25 14:58:08 +1000584 error = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
586 kfree(attr_name);
587 out_kfree_ops:
588 kfree(ops);
Christoph Hellwigd296d302009-01-19 02:02:57 +0100589 out_dput:
590 dput(dentry);
Dave Chinner24513372014-06-25 14:58:08 +1000591 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592}
593
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -0600594int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595xfs_ioc_space(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 struct file *filp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 unsigned int cmd,
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -0600598 xfs_flock64_t *bf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
Christoph Hellwig8f3e2052016-07-20 11:29:35 +1000600 struct inode *inode = file_inode(filp);
601 struct xfs_inode *ip = XFS_I(inode);
Christoph Hellwig865e9442013-10-12 00:55:08 -0700602 struct iattr iattr;
Christoph Hellwig8add71c2015-02-02 09:53:56 +1100603 enum xfs_prealloc_flags flags = 0;
Dan Williamsc63a8ea2018-03-12 14:12:29 -0700604 uint iolock = XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 int error;
606
Alexey Dobriyanf37ea142006-09-28 10:52:04 +1000607 if (inode->i_flags & (S_IMMUTABLE|S_APPEND))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000608 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Eric Sandeenad4a8ac2005-09-02 16:41:16 +1000610 if (!(filp->f_mode & FMODE_WRITE))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000611 return -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Alexey Dobriyanf37ea142006-09-28 10:52:04 +1000613 if (!S_ISREG(inode->i_mode))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000614 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Christoph Hellwig8add71c2015-02-02 09:53:56 +1100616 if (filp->f_flags & O_DSYNC)
617 flags |= XFS_PREALLOC_SYNC;
Christoph Hellwig8f3e2052016-07-20 11:29:35 +1000618 if (filp->f_mode & FMODE_NOCMTIME)
Christoph Hellwig8add71c2015-02-02 09:53:56 +1100619 flags |= XFS_PREALLOC_INVISIBLE;
620
Jan Karad9457dc2012-06-12 16:20:39 +0200621 error = mnt_want_write_file(filp);
622 if (error)
623 return error;
Christoph Hellwig865e9442013-10-12 00:55:08 -0700624
Christoph Hellwig781355c2015-02-16 11:59:50 +1100625 xfs_ilock(ip, iolock);
Dan Williams69eb5fa2018-03-20 14:42:38 -0700626 error = xfs_break_layouts(inode, &iolock, BREAK_UNMAP);
Christoph Hellwig781355c2015-02-16 11:59:50 +1100627 if (error)
628 goto out_unlock;
Christoph Hellwig865e9442013-10-12 00:55:08 -0700629
630 switch (bf->l_whence) {
631 case 0: /*SEEK_SET*/
632 break;
633 case 1: /*SEEK_CUR*/
634 bf->l_start += filp->f_pos;
635 break;
636 case 2: /*SEEK_END*/
637 bf->l_start += XFS_ISIZE(ip);
638 break;
639 default:
Dave Chinner24513372014-06-25 14:58:08 +1000640 error = -EINVAL;
Christoph Hellwig865e9442013-10-12 00:55:08 -0700641 goto out_unlock;
642 }
643
644 /*
645 * length of <= 0 for resv/unresv/zero is invalid. length for
646 * alloc/free is ignored completely and we have no idea what userspace
647 * might have set it to, so set it to zero to allow range
648 * checks to pass.
649 */
650 switch (cmd) {
651 case XFS_IOC_ZERO_RANGE:
652 case XFS_IOC_RESVSP:
653 case XFS_IOC_RESVSP64:
654 case XFS_IOC_UNRESVSP:
655 case XFS_IOC_UNRESVSP64:
656 if (bf->l_len <= 0) {
Dave Chinner24513372014-06-25 14:58:08 +1000657 error = -EINVAL;
Christoph Hellwig865e9442013-10-12 00:55:08 -0700658 goto out_unlock;
659 }
660 break;
661 default:
662 bf->l_len = 0;
663 break;
664 }
665
666 if (bf->l_start < 0 ||
Christoph Hellwig8add71c2015-02-02 09:53:56 +1100667 bf->l_start > inode->i_sb->s_maxbytes ||
Christoph Hellwig865e9442013-10-12 00:55:08 -0700668 bf->l_start + bf->l_len < 0 ||
Christoph Hellwig8add71c2015-02-02 09:53:56 +1100669 bf->l_start + bf->l_len >= inode->i_sb->s_maxbytes) {
Dave Chinner24513372014-06-25 14:58:08 +1000670 error = -EINVAL;
Christoph Hellwig865e9442013-10-12 00:55:08 -0700671 goto out_unlock;
672 }
673
674 switch (cmd) {
675 case XFS_IOC_ZERO_RANGE:
Christoph Hellwig8add71c2015-02-02 09:53:56 +1100676 flags |= XFS_PREALLOC_SET;
Christoph Hellwig865e9442013-10-12 00:55:08 -0700677 error = xfs_zero_file_space(ip, bf->l_start, bf->l_len);
Christoph Hellwig865e9442013-10-12 00:55:08 -0700678 break;
679 case XFS_IOC_RESVSP:
680 case XFS_IOC_RESVSP64:
Christoph Hellwig8add71c2015-02-02 09:53:56 +1100681 flags |= XFS_PREALLOC_SET;
Christoph Hellwig865e9442013-10-12 00:55:08 -0700682 error = xfs_alloc_file_space(ip, bf->l_start, bf->l_len,
683 XFS_BMAPI_PREALLOC);
Christoph Hellwig865e9442013-10-12 00:55:08 -0700684 break;
685 case XFS_IOC_UNRESVSP:
686 case XFS_IOC_UNRESVSP64:
687 error = xfs_free_file_space(ip, bf->l_start, bf->l_len);
688 break;
689 case XFS_IOC_ALLOCSP:
690 case XFS_IOC_ALLOCSP64:
691 case XFS_IOC_FREESP:
692 case XFS_IOC_FREESP64:
Christoph Hellwig8add71c2015-02-02 09:53:56 +1100693 flags |= XFS_PREALLOC_CLEAR;
Christoph Hellwig865e9442013-10-12 00:55:08 -0700694 if (bf->l_start > XFS_ISIZE(ip)) {
695 error = xfs_alloc_file_space(ip, XFS_ISIZE(ip),
696 bf->l_start - XFS_ISIZE(ip), 0);
697 if (error)
698 goto out_unlock;
699 }
700
701 iattr.ia_valid = ATTR_SIZE;
702 iattr.ia_size = bf->l_start;
703
Jan Kara69bca802016-05-26 14:46:43 +0200704 error = xfs_vn_setattr_size(file_dentry(filp), &iattr);
Christoph Hellwig865e9442013-10-12 00:55:08 -0700705 break;
706 default:
707 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +1000708 error = -EINVAL;
Christoph Hellwig865e9442013-10-12 00:55:08 -0700709 }
710
711 if (error)
712 goto out_unlock;
713
Christoph Hellwig8add71c2015-02-02 09:53:56 +1100714 error = xfs_update_prealloc_flags(ip, flags);
Christoph Hellwig865e9442013-10-12 00:55:08 -0700715
716out_unlock:
Christoph Hellwig781355c2015-02-16 11:59:50 +1100717 xfs_iunlock(ip, iolock);
Jan Karad9457dc2012-06-12 16:20:39 +0200718 mnt_drop_write_file(filp);
Dave Chinner24513372014-06-25 14:58:08 +1000719 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720}
721
722STATIC int
723xfs_ioc_bulkstat(
724 xfs_mount_t *mp,
725 unsigned int cmd,
726 void __user *arg)
727{
728 xfs_fsop_bulkreq_t bulkreq;
729 int count; /* # of records returned */
730 xfs_ino_t inlast; /* last inode number */
731 int done;
732 int error;
733
734 /* done = 1 if there are more stats to get and if bulkstat */
735 /* should be called again (unused here, but used in dmapi) */
736
737 if (!capable(CAP_SYS_ADMIN))
738 return -EPERM;
739
740 if (XFS_FORCED_SHUTDOWN(mp))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000741 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
743 if (copy_from_user(&bulkreq, arg, sizeof(xfs_fsop_bulkreq_t)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000744 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
746 if (copy_from_user(&inlast, bulkreq.lastip, sizeof(__s64)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000747 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749 if ((count = bulkreq.icount) <= 0)
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000750 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100752 if (bulkreq.ubuffer == NULL)
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000753 return -EINVAL;
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 if (cmd == XFS_IOC_FSINUMBERS)
756 error = xfs_inumbers(mp, &inlast, &count,
Michal Marekfaa63e92007-07-11 11:10:19 +1000757 bulkreq.ubuffer, xfs_inumbers_fmt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 else if (cmd == XFS_IOC_FSBULKSTAT_SINGLE)
Christoph Hellwigd716f8e2014-07-24 12:07:15 +1000759 error = xfs_bulkstat_one(mp, inlast, bulkreq.ubuffer,
760 sizeof(xfs_bstat_t), NULL, &done);
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100761 else /* XFS_IOC_FSBULKSTAT */
Christoph Hellwig7dce11d2010-06-23 18:11:11 +1000762 error = xfs_bulkstat(mp, &inlast, &count, xfs_bulkstat_one,
763 sizeof(xfs_bstat_t), bulkreq.ubuffer,
764 &done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
766 if (error)
Dave Chinner24513372014-06-25 14:58:08 +1000767 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
769 if (bulkreq.ocount != NULL) {
770 if (copy_to_user(bulkreq.lastip, &inlast,
771 sizeof(xfs_ino_t)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000772 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
774 if (copy_to_user(bulkreq.ocount, &count, sizeof(count)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000775 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 }
777
778 return 0;
779}
780
781STATIC int
782xfs_ioc_fsgeometry_v1(
783 xfs_mount_t *mp,
784 void __user *arg)
785{
Alex Eldereeb20362011-03-01 17:50:00 +0000786 xfs_fsop_geom_t fsgeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 int error;
788
Darrick J. Wongac503a42018-01-08 10:51:27 -0800789 error = xfs_fs_geometry(&mp->m_sb, &fsgeo, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 if (error)
Dave Chinner24513372014-06-25 14:58:08 +1000791 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Alex Eldereeb20362011-03-01 17:50:00 +0000793 /*
794 * Caller should have passed an argument of type
795 * xfs_fsop_geom_v1_t. This is a proper subset of the
796 * xfs_fsop_geom_t that xfs_fs_geometry() fills in.
797 */
798 if (copy_to_user(arg, &fsgeo, sizeof(xfs_fsop_geom_v1_t)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000799 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 return 0;
801}
802
803STATIC int
804xfs_ioc_fsgeometry(
805 xfs_mount_t *mp,
806 void __user *arg)
807{
808 xfs_fsop_geom_t fsgeo;
809 int error;
810
Darrick J. Wongac503a42018-01-08 10:51:27 -0800811 error = xfs_fs_geometry(&mp->m_sb, &fsgeo, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 if (error)
Dave Chinner24513372014-06-25 14:58:08 +1000813 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
815 if (copy_to_user(arg, &fsgeo, sizeof(fsgeo)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000816 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 return 0;
818}
819
820/*
821 * Linux extended inode flags interface.
822 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
824STATIC unsigned int
825xfs_merge_ioc_xflags(
826 unsigned int flags,
827 unsigned int start)
828{
829 unsigned int xflags = start;
830
Eric Sandeen39058a02007-02-10 18:37:10 +1100831 if (flags & FS_IMMUTABLE_FL)
Dave Chinnere7b89482016-01-04 16:44:15 +1100832 xflags |= FS_XFLAG_IMMUTABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 else
Dave Chinnere7b89482016-01-04 16:44:15 +1100834 xflags &= ~FS_XFLAG_IMMUTABLE;
Eric Sandeen39058a02007-02-10 18:37:10 +1100835 if (flags & FS_APPEND_FL)
Dave Chinnere7b89482016-01-04 16:44:15 +1100836 xflags |= FS_XFLAG_APPEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 else
Dave Chinnere7b89482016-01-04 16:44:15 +1100838 xflags &= ~FS_XFLAG_APPEND;
Eric Sandeen39058a02007-02-10 18:37:10 +1100839 if (flags & FS_SYNC_FL)
Dave Chinnere7b89482016-01-04 16:44:15 +1100840 xflags |= FS_XFLAG_SYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 else
Dave Chinnere7b89482016-01-04 16:44:15 +1100842 xflags &= ~FS_XFLAG_SYNC;
Eric Sandeen39058a02007-02-10 18:37:10 +1100843 if (flags & FS_NOATIME_FL)
Dave Chinnere7b89482016-01-04 16:44:15 +1100844 xflags |= FS_XFLAG_NOATIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 else
Dave Chinnere7b89482016-01-04 16:44:15 +1100846 xflags &= ~FS_XFLAG_NOATIME;
Eric Sandeen39058a02007-02-10 18:37:10 +1100847 if (flags & FS_NODUMP_FL)
Dave Chinnere7b89482016-01-04 16:44:15 +1100848 xflags |= FS_XFLAG_NODUMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 else
Dave Chinnere7b89482016-01-04 16:44:15 +1100850 xflags &= ~FS_XFLAG_NODUMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
852 return xflags;
853}
854
855STATIC unsigned int
856xfs_di2lxflags(
Darrick J. Wongc8ce5402017-06-16 11:00:05 -0700857 uint16_t di_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858{
859 unsigned int flags = 0;
860
861 if (di_flags & XFS_DIFLAG_IMMUTABLE)
Eric Sandeen39058a02007-02-10 18:37:10 +1100862 flags |= FS_IMMUTABLE_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 if (di_flags & XFS_DIFLAG_APPEND)
Eric Sandeen39058a02007-02-10 18:37:10 +1100864 flags |= FS_APPEND_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 if (di_flags & XFS_DIFLAG_SYNC)
Eric Sandeen39058a02007-02-10 18:37:10 +1100866 flags |= FS_SYNC_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 if (di_flags & XFS_DIFLAG_NOATIME)
Eric Sandeen39058a02007-02-10 18:37:10 +1100868 flags |= FS_NOATIME_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 if (di_flags & XFS_DIFLAG_NODUMP)
Eric Sandeen39058a02007-02-10 18:37:10 +1100870 flags |= FS_NODUMP_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 return flags;
872}
873
874STATIC int
Christoph Hellwigc83bfab2007-10-11 17:47:00 +1000875xfs_ioc_fsgetxattr(
876 xfs_inode_t *ip,
877 int attr,
878 void __user *arg)
879{
880 struct fsxattr fa;
881
Dan Rosenberga122eb22010-09-06 18:24:57 -0400882 memset(&fa, 0, sizeof(struct fsxattr));
883
Christoph Hellwigc83bfab2007-10-11 17:47:00 +1000884 xfs_ilock(ip, XFS_ILOCK_SHARED);
885 fa.fsx_xflags = xfs_ip2xflags(ip);
886 fa.fsx_extsize = ip->i_d.di_extsize << ip->i_mount->m_sb.sb_blocklog;
Darrick J. Wongf7ca3522016-10-03 09:11:43 -0700887 fa.fsx_cowextsize = ip->i_d.di_cowextsize <<
888 ip->i_mount->m_sb.sb_blocklog;
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +0000889 fa.fsx_projid = xfs_get_projid(ip);
Christoph Hellwigc83bfab2007-10-11 17:47:00 +1000890
891 if (attr) {
892 if (ip->i_afp) {
893 if (ip->i_afp->if_flags & XFS_IFEXTENTS)
Eric Sandeen5d829302016-11-08 12:59:42 +1100894 fa.fsx_nextents = xfs_iext_count(ip->i_afp);
Christoph Hellwigc83bfab2007-10-11 17:47:00 +1000895 else
896 fa.fsx_nextents = ip->i_d.di_anextents;
897 } else
898 fa.fsx_nextents = 0;
899 } else {
900 if (ip->i_df.if_flags & XFS_IFEXTENTS)
Eric Sandeen5d829302016-11-08 12:59:42 +1100901 fa.fsx_nextents = xfs_iext_count(&ip->i_df);
Christoph Hellwigc83bfab2007-10-11 17:47:00 +1000902 else
903 fa.fsx_nextents = ip->i_d.di_nextents;
904 }
905 xfs_iunlock(ip, XFS_ILOCK_SHARED);
906
907 if (copy_to_user(arg, &fa, sizeof(fa)))
908 return -EFAULT;
909 return 0;
910}
911
Christoph Hellwigdd606872017-09-02 08:21:20 -0700912STATIC uint16_t
913xfs_flags2diflags(
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000914 struct xfs_inode *ip,
915 unsigned int xflags)
916{
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000917 /* can't set PREALLOC this way, just preserve it */
Christoph Hellwigdd606872017-09-02 08:21:20 -0700918 uint16_t di_flags =
919 (ip->i_d.di_flags & XFS_DIFLAG_PREALLOC);
920
Dave Chinnere7b89482016-01-04 16:44:15 +1100921 if (xflags & FS_XFLAG_IMMUTABLE)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000922 di_flags |= XFS_DIFLAG_IMMUTABLE;
Dave Chinnere7b89482016-01-04 16:44:15 +1100923 if (xflags & FS_XFLAG_APPEND)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000924 di_flags |= XFS_DIFLAG_APPEND;
Dave Chinnere7b89482016-01-04 16:44:15 +1100925 if (xflags & FS_XFLAG_SYNC)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000926 di_flags |= XFS_DIFLAG_SYNC;
Dave Chinnere7b89482016-01-04 16:44:15 +1100927 if (xflags & FS_XFLAG_NOATIME)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000928 di_flags |= XFS_DIFLAG_NOATIME;
Dave Chinnere7b89482016-01-04 16:44:15 +1100929 if (xflags & FS_XFLAG_NODUMP)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000930 di_flags |= XFS_DIFLAG_NODUMP;
Dave Chinnere7b89482016-01-04 16:44:15 +1100931 if (xflags & FS_XFLAG_NODEFRAG)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000932 di_flags |= XFS_DIFLAG_NODEFRAG;
Dave Chinnere7b89482016-01-04 16:44:15 +1100933 if (xflags & FS_XFLAG_FILESTREAM)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000934 di_flags |= XFS_DIFLAG_FILESTREAM;
Dave Chinnerc19b3b052016-02-09 16:54:58 +1100935 if (S_ISDIR(VFS_I(ip)->i_mode)) {
Dave Chinnere7b89482016-01-04 16:44:15 +1100936 if (xflags & FS_XFLAG_RTINHERIT)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000937 di_flags |= XFS_DIFLAG_RTINHERIT;
Dave Chinnere7b89482016-01-04 16:44:15 +1100938 if (xflags & FS_XFLAG_NOSYMLINKS)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000939 di_flags |= XFS_DIFLAG_NOSYMLINKS;
Dave Chinnere7b89482016-01-04 16:44:15 +1100940 if (xflags & FS_XFLAG_EXTSZINHERIT)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000941 di_flags |= XFS_DIFLAG_EXTSZINHERIT;
Dave Chinnere7b89482016-01-04 16:44:15 +1100942 if (xflags & FS_XFLAG_PROJINHERIT)
Dave Chinner9336e3a2014-10-02 09:18:40 +1000943 di_flags |= XFS_DIFLAG_PROJINHERIT;
Dave Chinnerc19b3b052016-02-09 16:54:58 +1100944 } else if (S_ISREG(VFS_I(ip)->i_mode)) {
Dave Chinnere7b89482016-01-04 16:44:15 +1100945 if (xflags & FS_XFLAG_REALTIME)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000946 di_flags |= XFS_DIFLAG_REALTIME;
Dave Chinnere7b89482016-01-04 16:44:15 +1100947 if (xflags & FS_XFLAG_EXTSIZE)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000948 di_flags |= XFS_DIFLAG_EXTSIZE;
949 }
Dave Chinner58f88ca2016-01-04 16:44:15 +1100950
Christoph Hellwigdd606872017-09-02 08:21:20 -0700951 return di_flags;
952}
Dave Chinner58f88ca2016-01-04 16:44:15 +1100953
Christoph Hellwigdd606872017-09-02 08:21:20 -0700954STATIC uint64_t
955xfs_flags2diflags2(
956 struct xfs_inode *ip,
957 unsigned int xflags)
958{
959 uint64_t di_flags2 =
960 (ip->i_d.di_flags2 & XFS_DIFLAG2_REFLINK);
961
Dave Chinner58f88ca2016-01-04 16:44:15 +1100962 if (xflags & FS_XFLAG_DAX)
963 di_flags2 |= XFS_DIFLAG2_DAX;
Darrick J. Wongf7ca3522016-10-03 09:11:43 -0700964 if (xflags & FS_XFLAG_COWEXTSIZE)
965 di_flags2 |= XFS_DIFLAG2_COWEXTSIZE;
Dave Chinner58f88ca2016-01-04 16:44:15 +1100966
Christoph Hellwigdd606872017-09-02 08:21:20 -0700967 return di_flags2;
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000968}
969
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000970STATIC void
971xfs_diflags_to_linux(
972 struct xfs_inode *ip)
973{
David Chinnere4f75292008-08-13 16:00:45 +1000974 struct inode *inode = VFS_I(ip);
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000975 unsigned int xflags = xfs_ip2xflags(ip);
976
Dave Chinnere7b89482016-01-04 16:44:15 +1100977 if (xflags & FS_XFLAG_IMMUTABLE)
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000978 inode->i_flags |= S_IMMUTABLE;
979 else
980 inode->i_flags &= ~S_IMMUTABLE;
Dave Chinnere7b89482016-01-04 16:44:15 +1100981 if (xflags & FS_XFLAG_APPEND)
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000982 inode->i_flags |= S_APPEND;
983 else
984 inode->i_flags &= ~S_APPEND;
Dave Chinnere7b89482016-01-04 16:44:15 +1100985 if (xflags & FS_XFLAG_SYNC)
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000986 inode->i_flags |= S_SYNC;
987 else
988 inode->i_flags &= ~S_SYNC;
Dave Chinnere7b89482016-01-04 16:44:15 +1100989 if (xflags & FS_XFLAG_NOATIME)
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000990 inode->i_flags |= S_NOATIME;
991 else
992 inode->i_flags &= ~S_NOATIME;
Christoph Hellwig742d8422017-08-30 09:23:01 -0700993#if 0 /* disabled until the flag switching races are sorted out */
Dave Chinner58f88ca2016-01-04 16:44:15 +1100994 if (xflags & FS_XFLAG_DAX)
995 inode->i_flags |= S_DAX;
996 else
997 inode->i_flags &= ~S_DAX;
Christoph Hellwig742d8422017-08-30 09:23:01 -0700998#endif
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000999}
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001000
Dave Chinner29a17c02015-02-02 10:14:25 +11001001static int
1002xfs_ioctl_setattr_xflags(
1003 struct xfs_trans *tp,
1004 struct xfs_inode *ip,
1005 struct fsxattr *fa)
1006{
1007 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwigdd606872017-09-02 08:21:20 -07001008 uint64_t di_flags2;
Dave Chinner29a17c02015-02-02 10:14:25 +11001009
1010 /* Can't change realtime flag if any extents are allocated. */
1011 if ((ip->i_d.di_nextents || ip->i_delayed_blks) &&
Dave Chinnere7b89482016-01-04 16:44:15 +11001012 XFS_IS_REALTIME_INODE(ip) != (fa->fsx_xflags & FS_XFLAG_REALTIME))
Dave Chinner29a17c02015-02-02 10:14:25 +11001013 return -EINVAL;
1014
1015 /* If realtime flag is set then must have realtime device */
Dave Chinnere7b89482016-01-04 16:44:15 +11001016 if (fa->fsx_xflags & FS_XFLAG_REALTIME) {
Dave Chinner29a17c02015-02-02 10:14:25 +11001017 if (mp->m_sb.sb_rblocks == 0 || mp->m_sb.sb_rextsize == 0 ||
1018 (ip->i_d.di_extsize % mp->m_sb.sb_rextsize))
1019 return -EINVAL;
1020 }
1021
Darrick J. Wong1987fd72016-10-10 16:49:29 +11001022 /* Clear reflink if we are actually able to set the rt flag. */
Darrick J. Wongc8e156a2016-10-03 09:11:50 -07001023 if ((fa->fsx_xflags & FS_XFLAG_REALTIME) && xfs_is_reflink_inode(ip))
Darrick J. Wong1987fd72016-10-10 16:49:29 +11001024 ip->i_d.di_flags2 &= ~XFS_DIFLAG2_REFLINK;
Darrick J. Wongc8e156a2016-10-03 09:11:50 -07001025
Darrick J. Wong4f435eb2016-10-03 09:11:50 -07001026 /* Don't allow us to set DAX mode for a reflinked file for now. */
1027 if ((fa->fsx_xflags & FS_XFLAG_DAX) && xfs_is_reflink_inode(ip))
1028 return -EINVAL;
1029
Dave Chinner29a17c02015-02-02 10:14:25 +11001030 /*
1031 * Can't modify an immutable/append-only file unless
1032 * we have appropriate permission.
1033 */
1034 if (((ip->i_d.di_flags & (XFS_DIFLAG_IMMUTABLE | XFS_DIFLAG_APPEND)) ||
Dave Chinnere7b89482016-01-04 16:44:15 +11001035 (fa->fsx_xflags & (FS_XFLAG_IMMUTABLE | FS_XFLAG_APPEND))) &&
Dave Chinner29a17c02015-02-02 10:14:25 +11001036 !capable(CAP_LINUX_IMMUTABLE))
1037 return -EPERM;
1038
Christoph Hellwigdd606872017-09-02 08:21:20 -07001039 /* diflags2 only valid for v3 inodes. */
1040 di_flags2 = xfs_flags2diflags2(ip, fa->fsx_xflags);
1041 if (di_flags2 && ip->i_d.di_version < 3)
1042 return -EINVAL;
1043
1044 ip->i_d.di_flags = xfs_flags2diflags(ip, fa->fsx_xflags);
1045 ip->i_d.di_flags2 = di_flags2;
1046
Dave Chinner29a17c02015-02-02 10:14:25 +11001047 xfs_diflags_to_linux(ip);
1048 xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
1049 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11001050 XFS_STATS_INC(mp, xs_ig_attrchg);
Dave Chinner29a17c02015-02-02 10:14:25 +11001051 return 0;
1052}
1053
Dave Chinner8f3d17a2015-02-02 10:15:35 +11001054/*
Dave Chinner3a6a8542016-03-01 09:41:33 +11001055 * If we are changing DAX flags, we have to ensure the file is clean and any
1056 * cached objects in the address space are invalidated and removed. This
1057 * requires us to lock out other IO and page faults similar to a truncate
1058 * operation. The locks need to be held until the transaction has been committed
1059 * so that the cache invalidation is atomic with respect to the DAX flag
1060 * manipulation.
1061 */
1062static int
1063xfs_ioctl_setattr_dax_invalidate(
1064 struct xfs_inode *ip,
1065 struct fsxattr *fa,
1066 int *join_flags)
1067{
1068 struct inode *inode = VFS_I(ip);
Ross Zwisler6851a3d2017-09-18 14:46:03 -07001069 struct super_block *sb = inode->i_sb;
Dave Chinner3a6a8542016-03-01 09:41:33 +11001070 int error;
1071
1072 *join_flags = 0;
1073
1074 /*
1075 * It is only valid to set the DAX flag on regular files and
Dave Chinner64485432016-03-01 09:41:33 +11001076 * directories on filesystems where the block size is equal to the page
Darrick J. Wongaaacdd22018-05-31 15:07:47 -07001077 * size. On directories it serves as an inherited hint so we don't
1078 * have to check the device for dax support or flush pagecache.
Dave Chinner3a6a8542016-03-01 09:41:33 +11001079 */
Dave Chinner64485432016-03-01 09:41:33 +11001080 if (fa->fsx_xflags & FS_XFLAG_DAX) {
1081 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)))
1082 return -EINVAL;
Darrick J. Wongaaacdd22018-05-31 15:07:47 -07001083 if (S_ISREG(inode->i_mode) &&
1084 !bdev_dax_supported(xfs_find_bdev_for_inode(VFS_I(ip)),
Dave Jiang80660f22018-05-30 13:03:46 -07001085 sb->s_blocksize))
Dave Chinner64485432016-03-01 09:41:33 +11001086 return -EINVAL;
1087 }
Dave Chinner3a6a8542016-03-01 09:41:33 +11001088
1089 /* If the DAX state is not changing, we have nothing to do here. */
1090 if ((fa->fsx_xflags & FS_XFLAG_DAX) && IS_DAX(inode))
1091 return 0;
1092 if (!(fa->fsx_xflags & FS_XFLAG_DAX) && !IS_DAX(inode))
1093 return 0;
1094
Darrick J. Wongaaacdd22018-05-31 15:07:47 -07001095 if (S_ISDIR(inode->i_mode))
1096 return 0;
1097
Dave Chinner3a6a8542016-03-01 09:41:33 +11001098 /* lock, flush and invalidate mapping in preparation for flag change */
1099 xfs_ilock(ip, XFS_MMAPLOCK_EXCL | XFS_IOLOCK_EXCL);
1100 error = filemap_write_and_wait(inode->i_mapping);
1101 if (error)
1102 goto out_unlock;
1103 error = invalidate_inode_pages2(inode->i_mapping);
1104 if (error)
1105 goto out_unlock;
1106
1107 *join_flags = XFS_MMAPLOCK_EXCL | XFS_IOLOCK_EXCL;
1108 return 0;
1109
1110out_unlock:
1111 xfs_iunlock(ip, XFS_MMAPLOCK_EXCL | XFS_IOLOCK_EXCL);
1112 return error;
1113
1114}
1115
1116/*
Dave Chinner8f3d17a2015-02-02 10:15:35 +11001117 * Set up the transaction structure for the setattr operation, checking that we
1118 * have permission to do so. On success, return a clean transaction and the
1119 * inode locked exclusively ready for further operation specific checks. On
1120 * failure, return an error without modifying or locking the inode.
Dave Chinner3a6a8542016-03-01 09:41:33 +11001121 *
1122 * The inode might already be IO locked on call. If this is the case, it is
1123 * indicated in @join_flags and we take full responsibility for ensuring they
1124 * are unlocked from now on. Hence if we have an error here, we still have to
1125 * unlock them. Otherwise, once they are joined to the transaction, they will
1126 * be unlocked on commit/cancel.
Dave Chinner8f3d17a2015-02-02 10:15:35 +11001127 */
1128static struct xfs_trans *
1129xfs_ioctl_setattr_get_trans(
Dave Chinner3a6a8542016-03-01 09:41:33 +11001130 struct xfs_inode *ip,
1131 int join_flags)
Dave Chinner8f3d17a2015-02-02 10:15:35 +11001132{
1133 struct xfs_mount *mp = ip->i_mount;
1134 struct xfs_trans *tp;
Dave Chinner3a6a8542016-03-01 09:41:33 +11001135 int error = -EROFS;
Dave Chinner8f3d17a2015-02-02 10:15:35 +11001136
1137 if (mp->m_flags & XFS_MOUNT_RDONLY)
Dave Chinner3a6a8542016-03-01 09:41:33 +11001138 goto out_unlock;
1139 error = -EIO;
Dave Chinner8f3d17a2015-02-02 10:15:35 +11001140 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner3a6a8542016-03-01 09:41:33 +11001141 goto out_unlock;
Dave Chinner8f3d17a2015-02-02 10:15:35 +11001142
Christoph Hellwig253f4912016-04-06 09:19:55 +10001143 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
Dave Chinner8f3d17a2015-02-02 10:15:35 +11001144 if (error)
Christoph Hellwig253f4912016-04-06 09:19:55 +10001145 return ERR_PTR(error);
Dave Chinner8f3d17a2015-02-02 10:15:35 +11001146
1147 xfs_ilock(ip, XFS_ILOCK_EXCL);
Dave Chinner3a6a8542016-03-01 09:41:33 +11001148 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | join_flags);
1149 join_flags = 0;
Dave Chinner8f3d17a2015-02-02 10:15:35 +11001150
1151 /*
1152 * CAP_FOWNER overrides the following restrictions:
1153 *
1154 * The user ID of the calling process must be equal to the file owner
1155 * ID, except in cases where the CAP_FSETID capability is applicable.
1156 */
1157 if (!inode_owner_or_capable(VFS_I(ip))) {
1158 error = -EPERM;
1159 goto out_cancel;
1160 }
1161
1162 if (mp->m_flags & XFS_MOUNT_WSYNC)
1163 xfs_trans_set_sync(tp);
1164
1165 return tp;
1166
1167out_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +10001168 xfs_trans_cancel(tp);
Dave Chinner3a6a8542016-03-01 09:41:33 +11001169out_unlock:
1170 if (join_flags)
1171 xfs_iunlock(ip, join_flags);
Dave Chinner8f3d17a2015-02-02 10:15:35 +11001172 return ERR_PTR(error);
1173}
1174
Iustin Pop9b94fcc2015-02-02 10:26:26 +11001175/*
1176 * extent size hint validation is somewhat cumbersome. Rules are:
1177 *
1178 * 1. extent size hint is only valid for directories and regular files
Dave Chinnere7b89482016-01-04 16:44:15 +11001179 * 2. FS_XFLAG_EXTSIZE is only valid for regular files
1180 * 3. FS_XFLAG_EXTSZINHERIT is only valid for directories.
Iustin Pop9b94fcc2015-02-02 10:26:26 +11001181 * 4. can only be changed on regular files if no extents are allocated
1182 * 5. can be changed on directories at any time
1183 * 6. extsize hint of 0 turns off hints, clears inode flags.
1184 * 7. Extent size must be a multiple of the appropriate block size.
1185 * 8. for non-realtime files, the extent size hint must be limited
1186 * to half the AG size to avoid alignment extending the extent beyond the
1187 * limits of the AG.
Darrick J. Wong80e4e122017-10-17 21:37:42 -07001188 *
1189 * Please keep this function in sync with xfs_scrub_inode_extsize.
Iustin Pop9b94fcc2015-02-02 10:26:26 +11001190 */
kbuild test robotf92090e2015-02-05 11:13:21 +11001191static int
Dave Chinnerd4388d3c2015-02-02 10:22:20 +11001192xfs_ioctl_setattr_check_extsize(
1193 struct xfs_inode *ip,
1194 struct fsxattr *fa)
1195{
1196 struct xfs_mount *mp = ip->i_mount;
1197
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001198 if ((fa->fsx_xflags & FS_XFLAG_EXTSIZE) && !S_ISREG(VFS_I(ip)->i_mode))
Iustin Pop9b94fcc2015-02-02 10:26:26 +11001199 return -EINVAL;
1200
Dave Chinnere7b89482016-01-04 16:44:15 +11001201 if ((fa->fsx_xflags & FS_XFLAG_EXTSZINHERIT) &&
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001202 !S_ISDIR(VFS_I(ip)->i_mode))
Iustin Pop9b94fcc2015-02-02 10:26:26 +11001203 return -EINVAL;
1204
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001205 if (S_ISREG(VFS_I(ip)->i_mode) && ip->i_d.di_nextents &&
Dave Chinnerd4388d3c2015-02-02 10:22:20 +11001206 ((ip->i_d.di_extsize << mp->m_sb.sb_blocklog) != fa->fsx_extsize))
1207 return -EINVAL;
1208
Dave Chinnerd4388d3c2015-02-02 10:22:20 +11001209 if (fa->fsx_extsize != 0) {
1210 xfs_extlen_t size;
1211 xfs_fsblock_t extsize_fsb;
1212
1213 extsize_fsb = XFS_B_TO_FSB(mp, fa->fsx_extsize);
1214 if (extsize_fsb > MAXEXTLEN)
1215 return -EINVAL;
1216
1217 if (XFS_IS_REALTIME_INODE(ip) ||
Dave Chinnere7b89482016-01-04 16:44:15 +11001218 (fa->fsx_xflags & FS_XFLAG_REALTIME)) {
Dave Chinnerd4388d3c2015-02-02 10:22:20 +11001219 size = mp->m_sb.sb_rextsize << mp->m_sb.sb_blocklog;
1220 } else {
1221 size = mp->m_sb.sb_blocksize;
1222 if (extsize_fsb > mp->m_sb.sb_agblocks / 2)
1223 return -EINVAL;
1224 }
1225
1226 if (fa->fsx_extsize % size)
1227 return -EINVAL;
Iustin Pop9b94fcc2015-02-02 10:26:26 +11001228 } else
Dave Chinnere7b89482016-01-04 16:44:15 +11001229 fa->fsx_xflags &= ~(FS_XFLAG_EXTSIZE | FS_XFLAG_EXTSZINHERIT);
Iustin Pop9b94fcc2015-02-02 10:26:26 +11001230
Dave Chinnerd4388d3c2015-02-02 10:22:20 +11001231 return 0;
1232}
1233
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001234/*
1235 * CoW extent size hint validation rules are:
1236 *
1237 * 1. CoW extent size hint can only be set if reflink is enabled on the fs.
1238 * The inode does not have to have any shared blocks, but it must be a v3.
1239 * 2. FS_XFLAG_COWEXTSIZE is only valid for directories and regular files;
1240 * for a directory, the hint is propagated to new files.
1241 * 3. Can be changed on files & directories at any time.
1242 * 4. CoW extsize hint of 0 turns off hints, clears inode flags.
1243 * 5. Extent size must be a multiple of the appropriate block size.
1244 * 6. The extent size hint must be limited to half the AG size to avoid
1245 * alignment extending the extent beyond the limits of the AG.
Darrick J. Wong80e4e122017-10-17 21:37:42 -07001246 *
1247 * Please keep this function in sync with xfs_scrub_inode_cowextsize.
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001248 */
1249static int
1250xfs_ioctl_setattr_check_cowextsize(
1251 struct xfs_inode *ip,
1252 struct fsxattr *fa)
1253{
1254 struct xfs_mount *mp = ip->i_mount;
1255
1256 if (!(fa->fsx_xflags & FS_XFLAG_COWEXTSIZE))
1257 return 0;
1258
1259 if (!xfs_sb_version_hasreflink(&ip->i_mount->m_sb) ||
1260 ip->i_d.di_version != 3)
1261 return -EINVAL;
1262
1263 if (!S_ISREG(VFS_I(ip)->i_mode) && !S_ISDIR(VFS_I(ip)->i_mode))
1264 return -EINVAL;
1265
1266 if (fa->fsx_cowextsize != 0) {
1267 xfs_extlen_t size;
1268 xfs_fsblock_t cowextsize_fsb;
1269
1270 cowextsize_fsb = XFS_B_TO_FSB(mp, fa->fsx_cowextsize);
1271 if (cowextsize_fsb > MAXEXTLEN)
1272 return -EINVAL;
1273
1274 size = mp->m_sb.sb_blocksize;
1275 if (cowextsize_fsb > mp->m_sb.sb_agblocks / 2)
1276 return -EINVAL;
1277
1278 if (fa->fsx_cowextsize % size)
1279 return -EINVAL;
1280 } else
1281 fa->fsx_xflags &= ~FS_XFLAG_COWEXTSIZE;
1282
1283 return 0;
1284}
1285
kbuild test robotf92090e2015-02-05 11:13:21 +11001286static int
Dave Chinner23bd0732015-02-02 10:22:53 +11001287xfs_ioctl_setattr_check_projid(
1288 struct xfs_inode *ip,
1289 struct fsxattr *fa)
1290{
1291 /* Disallow 32bit project ids if projid32bit feature is not enabled. */
Darrick J. Wongc8ce5402017-06-16 11:00:05 -07001292 if (fa->fsx_projid > (uint16_t)-1 &&
Dave Chinner23bd0732015-02-02 10:22:53 +11001293 !xfs_sb_version_hasprojid32bit(&ip->i_mount->m_sb))
1294 return -EINVAL;
1295
1296 /*
1297 * Project Quota ID state is only allowed to change from within the init
1298 * namespace. Enforce that restriction only if we are trying to change
1299 * the quota ID state. Everything else is allowed in user namespaces.
1300 */
1301 if (current_user_ns() == &init_user_ns)
1302 return 0;
1303
1304 if (xfs_get_projid(ip) != fa->fsx_projid)
1305 return -EINVAL;
Dave Chinnere7b89482016-01-04 16:44:15 +11001306 if ((fa->fsx_xflags & FS_XFLAG_PROJINHERIT) !=
Dave Chinner23bd0732015-02-02 10:22:53 +11001307 (ip->i_d.di_flags & XFS_DIFLAG_PROJINHERIT))
1308 return -EINVAL;
1309
1310 return 0;
1311}
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001312
1313STATIC int
1314xfs_ioctl_setattr(
1315 xfs_inode_t *ip,
Dave Chinnerfd179b92015-02-02 10:16:25 +11001316 struct fsxattr *fa)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001317{
1318 struct xfs_mount *mp = ip->i_mount;
1319 struct xfs_trans *tp;
Christoph Hellwig7d095252009-06-08 15:33:32 +02001320 struct xfs_dquot *udqp = NULL;
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001321 struct xfs_dquot *pdqp = NULL;
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001322 struct xfs_dquot *olddquot = NULL;
1323 int code;
Dave Chinner3a6a8542016-03-01 09:41:33 +11001324 int join_flags = 0;
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001325
Christoph Hellwigcca28fb2010-06-24 11:57:09 +10001326 trace_xfs_ioctl_setattr(ip);
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001327
Dave Chinner23bd0732015-02-02 10:22:53 +11001328 code = xfs_ioctl_setattr_check_projid(ip, fa);
1329 if (code)
1330 return code;
Arkadiusz Mi?kiewicz23963e542010-08-26 10:19:43 +00001331
1332 /*
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001333 * If disk quotas is on, we make sure that the dquots do exist on disk,
1334 * before we start any other transactions. Trying to do this later
1335 * is messy. We don't care to take a readlock to look at the ids
1336 * in inode here, because we can't hold it across the trans_reserve.
1337 * If the IDs do change before we take the ilock, we're covered
1338 * because the i_*dquot fields will get updated anyway.
1339 */
Dave Chinnerfd179b92015-02-02 10:16:25 +11001340 if (XFS_IS_QUOTA_ON(mp)) {
Christoph Hellwig7d095252009-06-08 15:33:32 +02001341 code = xfs_qm_vop_dqalloc(ip, ip->i_d.di_uid,
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001342 ip->i_d.di_gid, fa->fsx_projid,
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001343 XFS_QMOPT_PQUOTA, &udqp, NULL, &pdqp);
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001344 if (code)
1345 return code;
1346 }
1347
Dave Chinner3a6a8542016-03-01 09:41:33 +11001348 /*
1349 * Changing DAX config may require inode locking for mapping
1350 * invalidation. These need to be held all the way to transaction commit
1351 * or cancel time, so need to be passed through to
1352 * xfs_ioctl_setattr_get_trans() so it can apply them to the join call
1353 * appropriately.
1354 */
1355 code = xfs_ioctl_setattr_dax_invalidate(ip, fa, &join_flags);
1356 if (code)
1357 goto error_free_dquots;
1358
1359 tp = xfs_ioctl_setattr_get_trans(ip, join_flags);
Dave Chinner8f3d17a2015-02-02 10:15:35 +11001360 if (IS_ERR(tp)) {
1361 code = PTR_ERR(tp);
1362 goto error_free_dquots;
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001363 }
1364
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001365
Dave Chinnerfd179b92015-02-02 10:16:25 +11001366 if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_PQUOTA_ON(mp) &&
1367 xfs_get_projid(ip) != fa->fsx_projid) {
1368 code = xfs_qm_vop_chown_reserve(tp, ip, udqp, NULL, pdqp,
1369 capable(CAP_FOWNER) ? XFS_QMOPT_FORCE_RES : 0);
1370 if (code) /* out of quota */
Dave Chinnerd4388d3c2015-02-02 10:22:20 +11001371 goto error_trans_cancel;
Dave Chinnerfd179b92015-02-02 10:16:25 +11001372 }
1373
Dave Chinnerd4388d3c2015-02-02 10:22:20 +11001374 code = xfs_ioctl_setattr_check_extsize(ip, fa);
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001375 if (code)
Dave Chinnerd4388d3c2015-02-02 10:22:20 +11001376 goto error_trans_cancel;
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001377
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001378 code = xfs_ioctl_setattr_check_cowextsize(ip, fa);
1379 if (code)
1380 goto error_trans_cancel;
1381
Dave Chinner29a17c02015-02-02 10:14:25 +11001382 code = xfs_ioctl_setattr_xflags(tp, ip, fa);
1383 if (code)
Dave Chinnerd4388d3c2015-02-02 10:22:20 +11001384 goto error_trans_cancel;
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001385
1386 /*
Dave Chinnerfd179b92015-02-02 10:16:25 +11001387 * Change file ownership. Must be the owner or privileged. CAP_FSETID
1388 * overrides the following restrictions:
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001389 *
Dave Chinnerfd179b92015-02-02 10:16:25 +11001390 * The set-user-ID and set-group-ID bits of a file will be cleared upon
1391 * successful return from chown()
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001392 */
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001393
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001394 if ((VFS_I(ip)->i_mode & (S_ISUID|S_ISGID)) &&
Dave Chinnerfd179b92015-02-02 10:16:25 +11001395 !capable_wrt_inode_uidgid(VFS_I(ip), CAP_FSETID))
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001396 VFS_I(ip)->i_mode &= ~(S_ISUID|S_ISGID);
Dave Chinnerfd179b92015-02-02 10:16:25 +11001397
1398 /* Change the ownerships and register project quota modifications */
1399 if (xfs_get_projid(ip) != fa->fsx_projid) {
1400 if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_PQUOTA_ON(mp)) {
1401 olddquot = xfs_qm_vop_chown(tp, ip,
1402 &ip->i_pdquot, pdqp);
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001403 }
Dave Chinnerfd179b92015-02-02 10:16:25 +11001404 ASSERT(ip->i_d.di_version > 1);
1405 xfs_set_projid(ip, fa->fsx_projid);
Christoph Hellwigf13fae22008-07-21 16:16:15 +10001406 }
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001407
Dave Chinnera8727032014-10-02 09:20:30 +10001408 /*
1409 * Only set the extent size hint if we've already determined that the
1410 * extent size hint should be set on the inode. If no extent size flags
1411 * are set on the inode then unconditionally clear the extent size hint.
1412 */
Dave Chinnerfd179b92015-02-02 10:16:25 +11001413 if (ip->i_d.di_flags & (XFS_DIFLAG_EXTSIZE | XFS_DIFLAG_EXTSZINHERIT))
1414 ip->i_d.di_extsize = fa->fsx_extsize >> mp->m_sb.sb_blocklog;
1415 else
1416 ip->i_d.di_extsize = 0;
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001417 if (ip->i_d.di_version == 3 &&
1418 (ip->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE))
1419 ip->i_d.di_cowextsize = fa->fsx_cowextsize >>
1420 mp->m_sb.sb_blocklog;
1421 else
1422 ip->i_d.di_cowextsize = 0;
Dave Chinnera8727032014-10-02 09:20:30 +10001423
Christoph Hellwig70393312015-06-04 13:48:08 +10001424 code = xfs_trans_commit(tp);
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001425
1426 /*
1427 * Release any dquot(s) the inode had kept before chown.
1428 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02001429 xfs_qm_dqrele(olddquot);
1430 xfs_qm_dqrele(udqp);
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001431 xfs_qm_dqrele(pdqp);
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001432
Christoph Hellwig288699f2010-06-23 18:11:15 +10001433 return code;
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001434
Dave Chinnerd4388d3c2015-02-02 10:22:20 +11001435error_trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +10001436 xfs_trans_cancel(tp);
Dave Chinner8f3d17a2015-02-02 10:15:35 +11001437error_free_dquots:
Christoph Hellwig7d095252009-06-08 15:33:32 +02001438 xfs_qm_dqrele(udqp);
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001439 xfs_qm_dqrele(pdqp);
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001440 return code;
1441}
1442
Christoph Hellwigc83bfab2007-10-11 17:47:00 +10001443STATIC int
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001444xfs_ioc_fssetxattr(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 xfs_inode_t *ip,
1446 struct file *filp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 void __user *arg)
1448{
1449 struct fsxattr fa;
Jan Karad9457dc2012-06-12 16:20:39 +02001450 int error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001451
1452 if (copy_from_user(&fa, arg, sizeof(fa)))
1453 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
Jan Karad9457dc2012-06-12 16:20:39 +02001455 error = mnt_want_write_file(filp);
1456 if (error)
1457 return error;
Dave Chinnerfd179b92015-02-02 10:16:25 +11001458 error = xfs_ioctl_setattr(ip, &fa);
Jan Karad9457dc2012-06-12 16:20:39 +02001459 mnt_drop_write_file(filp);
Dave Chinner24513372014-06-25 14:58:08 +10001460 return error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001461}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001463STATIC int
1464xfs_ioc_getxflags(
1465 xfs_inode_t *ip,
1466 void __user *arg)
1467{
1468 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001470 flags = xfs_di2lxflags(ip->i_d.di_flags);
1471 if (copy_to_user(arg, &flags, sizeof(flags)))
1472 return -EFAULT;
1473 return 0;
1474}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001476STATIC int
1477xfs_ioc_setxflags(
Dave Chinnerf96291f2015-02-02 10:15:56 +11001478 struct xfs_inode *ip,
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001479 struct file *filp,
1480 void __user *arg)
1481{
Dave Chinnerf96291f2015-02-02 10:15:56 +11001482 struct xfs_trans *tp;
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001483 struct fsxattr fa;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001484 unsigned int flags;
Dave Chinner3a6a8542016-03-01 09:41:33 +11001485 int join_flags = 0;
Dave Chinnerf96291f2015-02-02 10:15:56 +11001486 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001488 if (copy_from_user(&flags, arg, sizeof(flags)))
1489 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001491 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
1492 FS_NOATIME_FL | FS_NODUMP_FL | \
1493 FS_SYNC_FL))
1494 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001496 fa.fsx_xflags = xfs_merge_ioc_xflags(flags, xfs_ip2xflags(ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
Jan Karad9457dc2012-06-12 16:20:39 +02001498 error = mnt_want_write_file(filp);
1499 if (error)
1500 return error;
Dave Chinnerf96291f2015-02-02 10:15:56 +11001501
Dave Chinner3a6a8542016-03-01 09:41:33 +11001502 /*
1503 * Changing DAX config may require inode locking for mapping
1504 * invalidation. These need to be held all the way to transaction commit
1505 * or cancel time, so need to be passed through to
1506 * xfs_ioctl_setattr_get_trans() so it can apply them to the join call
1507 * appropriately.
1508 */
1509 error = xfs_ioctl_setattr_dax_invalidate(ip, &fa, &join_flags);
1510 if (error)
1511 goto out_drop_write;
1512
1513 tp = xfs_ioctl_setattr_get_trans(ip, join_flags);
Dave Chinnerf96291f2015-02-02 10:15:56 +11001514 if (IS_ERR(tp)) {
1515 error = PTR_ERR(tp);
1516 goto out_drop_write;
1517 }
1518
1519 error = xfs_ioctl_setattr_xflags(tp, ip, &fa);
1520 if (error) {
Christoph Hellwig4906e212015-06-04 13:47:56 +10001521 xfs_trans_cancel(tp);
Dave Chinnerf96291f2015-02-02 10:15:56 +11001522 goto out_drop_write;
1523 }
1524
Christoph Hellwig70393312015-06-04 13:48:08 +10001525 error = xfs_trans_commit(tp);
Dave Chinnerf96291f2015-02-02 10:15:56 +11001526out_drop_write:
Jan Karad9457dc2012-06-12 16:20:39 +02001527 mnt_drop_write_file(filp);
Dave Chinner24513372014-06-25 14:58:08 +10001528 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529}
1530
Christoph Hellwig232b51942017-10-17 14:16:19 -07001531static bool
1532xfs_getbmap_format(
1533 struct kgetbmap *p,
1534 struct getbmapx __user *u,
1535 size_t recsize)
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001536{
Christoph Hellwig232b51942017-10-17 14:16:19 -07001537 if (put_user(p->bmv_offset, &u->bmv_offset) ||
1538 put_user(p->bmv_block, &u->bmv_block) ||
1539 put_user(p->bmv_length, &u->bmv_length) ||
1540 put_user(0, &u->bmv_count) ||
1541 put_user(0, &u->bmv_entries))
1542 return false;
1543 if (recsize < sizeof(struct getbmapx))
1544 return true;
1545 if (put_user(0, &u->bmv_iflags) ||
1546 put_user(p->bmv_oflags, &u->bmv_oflags) ||
1547 put_user(0, &u->bmv_unused1) ||
1548 put_user(0, &u->bmv_unused2))
1549 return false;
1550 return true;
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001551}
1552
1553STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554xfs_ioc_getbmap(
Christoph Hellwig8f3e2052016-07-20 11:29:35 +10001555 struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 unsigned int cmd,
1557 void __user *arg)
1558{
Darrick J. Wongbe6324c2017-04-03 15:17:57 -07001559 struct getbmapx bmx = { 0 };
Christoph Hellwig232b51942017-10-17 14:16:19 -07001560 struct kgetbmap *buf;
1561 size_t recsize;
1562 int error, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
Christoph Hellwig232b51942017-10-17 14:16:19 -07001564 switch (cmd) {
1565 case XFS_IOC_GETBMAPA:
1566 bmx.bmv_iflags = BMV_IF_ATTRFORK;
1567 /*FALLTHRU*/
1568 case XFS_IOC_GETBMAP:
1569 if (file->f_mode & FMODE_NOCMTIME)
1570 bmx.bmv_iflags |= BMV_IF_NO_DMAPI_READ;
1571 /* struct getbmap is a strict subset of struct getbmapx. */
1572 recsize = sizeof(struct getbmap);
1573 break;
1574 case XFS_IOC_GETBMAPX:
1575 recsize = sizeof(struct getbmapx);
1576 break;
1577 default:
1578 return -EINVAL;
1579 }
1580
1581 if (copy_from_user(&bmx, arg, recsize))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10001582 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001584 if (bmx.bmv_count < 2)
Eric Sandeenb474c7a2014-06-22 15:04:54 +10001585 return -EINVAL;
Christoph Hellwig232b51942017-10-17 14:16:19 -07001586 if (bmx.bmv_count > ULONG_MAX / recsize)
1587 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
Christoph Hellwig232b51942017-10-17 14:16:19 -07001589 buf = kmem_zalloc_large(bmx.bmv_count * sizeof(*buf), 0);
1590 if (!buf)
1591 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
Christoph Hellwig232b51942017-10-17 14:16:19 -07001593 error = xfs_getbmap(XFS_I(file_inode(file)), &bmx, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 if (error)
Christoph Hellwig232b51942017-10-17 14:16:19 -07001595 goto out_free_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
Christoph Hellwig232b51942017-10-17 14:16:19 -07001597 error = -EFAULT;
1598 if (copy_to_user(arg, &bmx, recsize))
1599 goto out_free_buf;
1600 arg += recsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
Christoph Hellwig232b51942017-10-17 14:16:19 -07001602 for (i = 0; i < bmx.bmv_entries; i++) {
1603 if (!xfs_getbmap_format(buf + i, arg, recsize))
1604 goto out_free_buf;
1605 arg += recsize;
1606 }
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001607
Christoph Hellwig232b51942017-10-17 14:16:19 -07001608 error = 0;
1609out_free_buf:
1610 kmem_free(buf);
Christophe JAILLET132bf672018-11-06 07:50:50 -08001611 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612}
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001613
Darrick J. Wonge89c0412017-03-28 14:56:37 -07001614struct getfsmap_info {
1615 struct xfs_mount *mp;
Christoph Hellwig9d17e142017-04-21 11:24:41 -07001616 struct fsmap_head __user *data;
1617 unsigned int idx;
Darrick J. Wonge89c0412017-03-28 14:56:37 -07001618 __u32 last_flags;
1619};
1620
1621STATIC int
1622xfs_getfsmap_format(struct xfs_fsmap *xfm, void *priv)
1623{
1624 struct getfsmap_info *info = priv;
1625 struct fsmap fm;
1626
1627 trace_xfs_getfsmap_mapping(info->mp, xfm);
1628
1629 info->last_flags = xfm->fmr_flags;
1630 xfs_fsmap_from_internal(&fm, xfm);
Christoph Hellwig9d17e142017-04-21 11:24:41 -07001631 if (copy_to_user(&info->data->fmh_recs[info->idx++], &fm,
1632 sizeof(struct fsmap)))
Darrick J. Wonge89c0412017-03-28 14:56:37 -07001633 return -EFAULT;
1634
Darrick J. Wonge89c0412017-03-28 14:56:37 -07001635 return 0;
1636}
1637
1638STATIC int
1639xfs_ioc_getfsmap(
1640 struct xfs_inode *ip,
Christoph Hellwig9d17e142017-04-21 11:24:41 -07001641 struct fsmap_head __user *arg)
Darrick J. Wonge89c0412017-03-28 14:56:37 -07001642{
Christoph Hellwigef2b67e2017-04-21 11:24:40 -07001643 struct getfsmap_info info = { NULL };
Darrick J. Wonge89c0412017-03-28 14:56:37 -07001644 struct xfs_fsmap_head xhead = {0};
1645 struct fsmap_head head;
1646 bool aborted = false;
1647 int error;
1648
1649 if (copy_from_user(&head, arg, sizeof(struct fsmap_head)))
1650 return -EFAULT;
1651 if (memchr_inv(head.fmh_reserved, 0, sizeof(head.fmh_reserved)) ||
1652 memchr_inv(head.fmh_keys[0].fmr_reserved, 0,
1653 sizeof(head.fmh_keys[0].fmr_reserved)) ||
1654 memchr_inv(head.fmh_keys[1].fmr_reserved, 0,
1655 sizeof(head.fmh_keys[1].fmr_reserved)))
1656 return -EINVAL;
1657
1658 xhead.fmh_iflags = head.fmh_iflags;
1659 xhead.fmh_count = head.fmh_count;
1660 xfs_fsmap_to_internal(&xhead.fmh_keys[0], &head.fmh_keys[0]);
1661 xfs_fsmap_to_internal(&xhead.fmh_keys[1], &head.fmh_keys[1]);
1662
1663 trace_xfs_getfsmap_low_key(ip->i_mount, &xhead.fmh_keys[0]);
1664 trace_xfs_getfsmap_high_key(ip->i_mount, &xhead.fmh_keys[1]);
1665
1666 info.mp = ip->i_mount;
Christoph Hellwig9d17e142017-04-21 11:24:41 -07001667 info.data = arg;
Darrick J. Wonge89c0412017-03-28 14:56:37 -07001668 error = xfs_getfsmap(ip->i_mount, &xhead, xfs_getfsmap_format, &info);
1669 if (error == XFS_BTREE_QUERY_RANGE_ABORT) {
1670 error = 0;
1671 aborted = true;
1672 } else if (error)
1673 return error;
1674
1675 /* If we didn't abort, set the "last" flag in the last fmx */
Darrick J. Wong12e4a382017-04-23 10:45:21 -07001676 if (!aborted && info.idx) {
Darrick J. Wonge89c0412017-03-28 14:56:37 -07001677 info.last_flags |= FMR_OF_LAST;
Christoph Hellwig9d17e142017-04-21 11:24:41 -07001678 if (copy_to_user(&info.data->fmh_recs[info.idx - 1].fmr_flags,
1679 &info.last_flags, sizeof(info.last_flags)))
Darrick J. Wonge89c0412017-03-28 14:56:37 -07001680 return -EFAULT;
1681 }
1682
1683 /* copy back header */
1684 head.fmh_entries = xhead.fmh_entries;
1685 head.fmh_oflags = xhead.fmh_oflags;
1686 if (copy_to_user(arg, &head, sizeof(struct fsmap_head)))
1687 return -EFAULT;
1688
1689 return 0;
1690}
1691
Darrick J. Wong36fd6e82017-10-17 21:37:34 -07001692STATIC int
1693xfs_ioc_scrub_metadata(
1694 struct xfs_inode *ip,
1695 void __user *arg)
1696{
1697 struct xfs_scrub_metadata scrub;
1698 int error;
1699
1700 if (!capable(CAP_SYS_ADMIN))
1701 return -EPERM;
1702
1703 if (copy_from_user(&scrub, arg, sizeof(scrub)))
1704 return -EFAULT;
1705
1706 error = xfs_scrub_metadata(ip, &scrub);
1707 if (error)
1708 return error;
1709
1710 if (copy_to_user(arg, &scrub, sizeof(scrub)))
1711 return -EFAULT;
1712
1713 return 0;
1714}
1715
Dave Chinnera133d952013-08-12 20:49:48 +10001716int
1717xfs_ioc_swapext(
1718 xfs_swapext_t *sxp)
1719{
1720 xfs_inode_t *ip, *tip;
1721 struct fd f, tmp;
1722 int error = 0;
1723
1724 /* Pull information for the target fd */
1725 f = fdget((int)sxp->sx_fdtarget);
1726 if (!f.file) {
Dave Chinner24513372014-06-25 14:58:08 +10001727 error = -EINVAL;
Dave Chinnera133d952013-08-12 20:49:48 +10001728 goto out;
1729 }
1730
1731 if (!(f.file->f_mode & FMODE_WRITE) ||
1732 !(f.file->f_mode & FMODE_READ) ||
1733 (f.file->f_flags & O_APPEND)) {
Dave Chinner24513372014-06-25 14:58:08 +10001734 error = -EBADF;
Dave Chinnera133d952013-08-12 20:49:48 +10001735 goto out_put_file;
1736 }
1737
1738 tmp = fdget((int)sxp->sx_fdtmp);
1739 if (!tmp.file) {
Dave Chinner24513372014-06-25 14:58:08 +10001740 error = -EINVAL;
Dave Chinnera133d952013-08-12 20:49:48 +10001741 goto out_put_file;
1742 }
1743
1744 if (!(tmp.file->f_mode & FMODE_WRITE) ||
1745 !(tmp.file->f_mode & FMODE_READ) ||
1746 (tmp.file->f_flags & O_APPEND)) {
Dave Chinner24513372014-06-25 14:58:08 +10001747 error = -EBADF;
Dave Chinnera133d952013-08-12 20:49:48 +10001748 goto out_put_tmp_file;
1749 }
1750
1751 if (IS_SWAPFILE(file_inode(f.file)) ||
1752 IS_SWAPFILE(file_inode(tmp.file))) {
Dave Chinner24513372014-06-25 14:58:08 +10001753 error = -EINVAL;
Dave Chinnera133d952013-08-12 20:49:48 +10001754 goto out_put_tmp_file;
1755 }
1756
Jann Horn7f1b6242016-07-20 10:30:30 +10001757 /*
1758 * We need to ensure that the fds passed in point to XFS inodes
1759 * before we cast and access them as XFS structures as we have no
1760 * control over what the user passes us here.
1761 */
1762 if (f.file->f_op != &xfs_file_operations ||
1763 tmp.file->f_op != &xfs_file_operations) {
1764 error = -EINVAL;
1765 goto out_put_tmp_file;
1766 }
1767
Dave Chinnera133d952013-08-12 20:49:48 +10001768 ip = XFS_I(file_inode(f.file));
1769 tip = XFS_I(file_inode(tmp.file));
1770
1771 if (ip->i_mount != tip->i_mount) {
Dave Chinner24513372014-06-25 14:58:08 +10001772 error = -EINVAL;
Dave Chinnera133d952013-08-12 20:49:48 +10001773 goto out_put_tmp_file;
1774 }
1775
1776 if (ip->i_ino == tip->i_ino) {
Dave Chinner24513372014-06-25 14:58:08 +10001777 error = -EINVAL;
Dave Chinnera133d952013-08-12 20:49:48 +10001778 goto out_put_tmp_file;
1779 }
1780
1781 if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
Dave Chinner24513372014-06-25 14:58:08 +10001782 error = -EIO;
Dave Chinnera133d952013-08-12 20:49:48 +10001783 goto out_put_tmp_file;
1784 }
1785
1786 error = xfs_swap_extents(ip, tip, sxp);
1787
1788 out_put_tmp_file:
1789 fdput(tmp);
1790 out_put_file:
1791 fdput(f);
1792 out:
1793 return error;
1794}
1795
Eric Sandeenf7664b32018-05-15 13:21:48 -07001796static int
1797xfs_ioc_getlabel(
1798 struct xfs_mount *mp,
1799 char __user *user_label)
1800{
1801 struct xfs_sb *sbp = &mp->m_sb;
1802 char label[XFSLABEL_MAX + 1];
1803
1804 /* Paranoia */
1805 BUILD_BUG_ON(sizeof(sbp->sb_fname) > FSLABEL_MAX);
1806
Arnd Bergmann4bb8b652018-06-05 19:42:45 -07001807 /* 1 larger than sb_fname, so this ensures a trailing NUL char */
1808 memset(label, 0, sizeof(label));
Eric Sandeenf7664b32018-05-15 13:21:48 -07001809 spin_lock(&mp->m_sb_lock);
Arnd Bergmann4bb8b652018-06-05 19:42:45 -07001810 strncpy(label, sbp->sb_fname, XFSLABEL_MAX);
Eric Sandeenf7664b32018-05-15 13:21:48 -07001811 spin_unlock(&mp->m_sb_lock);
1812
Arnd Bergmann4bb8b652018-06-05 19:42:45 -07001813 if (copy_to_user(user_label, label, sizeof(label)))
Eric Sandeenf7664b32018-05-15 13:21:48 -07001814 return -EFAULT;
1815 return 0;
1816}
1817
1818static int
1819xfs_ioc_setlabel(
1820 struct file *filp,
1821 struct xfs_mount *mp,
1822 char __user *newlabel)
1823{
1824 struct xfs_sb *sbp = &mp->m_sb;
1825 char label[XFSLABEL_MAX + 1];
1826 size_t len;
1827 int error;
1828
1829 if (!capable(CAP_SYS_ADMIN))
1830 return -EPERM;
1831 /*
1832 * The generic ioctl allows up to FSLABEL_MAX chars, but XFS is much
1833 * smaller, at 12 bytes. We copy one more to be sure we find the
1834 * (required) NULL character to test the incoming label length.
1835 * NB: The on disk label doesn't need to be null terminated.
1836 */
1837 if (copy_from_user(label, newlabel, XFSLABEL_MAX + 1))
1838 return -EFAULT;
1839 len = strnlen(label, XFSLABEL_MAX + 1);
1840 if (len > sizeof(sbp->sb_fname))
1841 return -EINVAL;
1842
1843 error = mnt_want_write_file(filp);
1844 if (error)
1845 return error;
1846
1847 spin_lock(&mp->m_sb_lock);
1848 memset(sbp->sb_fname, 0, sizeof(sbp->sb_fname));
Arnd Bergmann4bb8b652018-06-05 19:42:45 -07001849 memcpy(sbp->sb_fname, label, len);
Eric Sandeenf7664b32018-05-15 13:21:48 -07001850 spin_unlock(&mp->m_sb_lock);
1851
1852 /*
1853 * Now we do several things to satisfy userspace.
1854 * In addition to normal logging of the primary superblock, we also
1855 * immediately write these changes to sector zero for the primary, then
1856 * update all backup supers (as xfs_db does for a label change), then
1857 * invalidate the block device page cache. This is so that any prior
1858 * buffered reads from userspace (i.e. from blkid) are invalidated,
1859 * and userspace will see the newly-written label.
1860 */
1861 error = xfs_sync_sb_buf(mp);
1862 if (error)
1863 goto out;
1864 /*
1865 * growfs also updates backup supers so lock against that.
1866 */
1867 mutex_lock(&mp->m_growlock);
1868 error = xfs_update_secondary_sbs(mp);
1869 mutex_unlock(&mp->m_growlock);
1870
1871 invalidate_bdev(mp->m_ddev_targp->bt_bdev);
1872
1873out:
1874 mnt_drop_write_file(filp);
1875 return error;
1876}
1877
Christoph Hellwig4d4be482008-12-09 04:47:33 -05001878/*
1879 * Note: some of the ioctl's return positive numbers as a
1880 * byte count indicating success, such as readlink_by_handle.
1881 * So we don't "sign flip" like most other routines. This means
1882 * true errors need to be returned as a negative value.
1883 */
1884long
1885xfs_file_ioctl(
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001886 struct file *filp,
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001887 unsigned int cmd,
Christoph Hellwig4d4be482008-12-09 04:47:33 -05001888 unsigned long p)
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001889{
Al Viro496ad9a2013-01-23 17:07:38 -05001890 struct inode *inode = file_inode(filp);
Christoph Hellwig4d4be482008-12-09 04:47:33 -05001891 struct xfs_inode *ip = XFS_I(inode);
1892 struct xfs_mount *mp = ip->i_mount;
1893 void __user *arg = (void __user *)p;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001894 int error;
1895
Christoph Hellwigcca28fb2010-06-24 11:57:09 +10001896 trace_xfs_file_ioctl(ip);
Christoph Hellwig4d4be482008-12-09 04:47:33 -05001897
1898 switch (cmd) {
Christoph Hellwiga46db602011-01-07 13:02:04 +00001899 case FITRIM:
1900 return xfs_ioc_trim(mp, arg);
Eric Sandeenf7664b32018-05-15 13:21:48 -07001901 case FS_IOC_GETFSLABEL:
1902 return xfs_ioc_getlabel(mp, arg);
1903 case FS_IOC_SETFSLABEL:
1904 return xfs_ioc_setlabel(filp, mp, arg);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001905 case XFS_IOC_ALLOCSP:
1906 case XFS_IOC_FREESP:
1907 case XFS_IOC_RESVSP:
1908 case XFS_IOC_UNRESVSP:
1909 case XFS_IOC_ALLOCSP64:
1910 case XFS_IOC_FREESP64:
1911 case XFS_IOC_RESVSP64:
Dave Chinner44722352010-08-24 12:02:11 +10001912 case XFS_IOC_UNRESVSP64:
1913 case XFS_IOC_ZERO_RANGE: {
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06001914 xfs_flock64_t bf;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001915
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06001916 if (copy_from_user(&bf, arg, sizeof(bf)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10001917 return -EFAULT;
Christoph Hellwig8f3e2052016-07-20 11:29:35 +10001918 return xfs_ioc_space(filp, cmd, &bf);
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06001919 }
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001920 case XFS_IOC_DIOINFO: {
1921 struct dioattr da;
1922 xfs_buftarg_t *target =
1923 XFS_IS_REALTIME_INODE(ip) ?
1924 mp->m_rtdev_targp : mp->m_ddev_targp;
1925
Eric Sandeen7c71ee72014-01-21 16:46:23 -06001926 da.d_mem = da.d_miniosz = target->bt_logical_sectorsize;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001927 da.d_maxiosz = INT_MAX & ~(da.d_miniosz - 1);
1928
1929 if (copy_to_user(arg, &da, sizeof(da)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10001930 return -EFAULT;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001931 return 0;
1932 }
1933
1934 case XFS_IOC_FSBULKSTAT_SINGLE:
1935 case XFS_IOC_FSBULKSTAT:
1936 case XFS_IOC_FSINUMBERS:
1937 return xfs_ioc_bulkstat(mp, cmd, arg);
1938
1939 case XFS_IOC_FSGEOMETRY_V1:
1940 return xfs_ioc_fsgeometry_v1(mp, arg);
1941
1942 case XFS_IOC_FSGEOMETRY:
1943 return xfs_ioc_fsgeometry(mp, arg);
1944
1945 case XFS_IOC_GETVERSION:
1946 return put_user(inode->i_generation, (int __user *)arg);
1947
1948 case XFS_IOC_FSGETXATTR:
1949 return xfs_ioc_fsgetxattr(ip, 0, arg);
1950 case XFS_IOC_FSGETXATTRA:
1951 return xfs_ioc_fsgetxattr(ip, 1, arg);
Lachlan McIlroy3b2816b2008-04-18 12:43:35 +10001952 case XFS_IOC_FSSETXATTR:
Lachlan McIlroy65e67f52008-04-18 12:59:45 +10001953 return xfs_ioc_fssetxattr(ip, filp, arg);
1954 case XFS_IOC_GETXFLAGS:
1955 return xfs_ioc_getxflags(ip, arg);
1956 case XFS_IOC_SETXFLAGS:
1957 return xfs_ioc_setxflags(ip, filp, arg);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001958
1959 case XFS_IOC_FSSETDM: {
1960 struct fsdmidata dmi;
1961
1962 if (copy_from_user(&dmi, arg, sizeof(dmi)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10001963 return -EFAULT;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001964
Jan Karad9457dc2012-06-12 16:20:39 +02001965 error = mnt_want_write_file(filp);
1966 if (error)
1967 return error;
1968
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001969 error = xfs_set_dmattrs(ip, dmi.fsd_dmevmask,
1970 dmi.fsd_dmstate);
Jan Karad9457dc2012-06-12 16:20:39 +02001971 mnt_drop_write_file(filp);
Dave Chinner24513372014-06-25 14:58:08 +10001972 return error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001973 }
1974
1975 case XFS_IOC_GETBMAP:
1976 case XFS_IOC_GETBMAPA:
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001977 case XFS_IOC_GETBMAPX:
Christoph Hellwig232b51942017-10-17 14:16:19 -07001978 return xfs_ioc_getbmap(filp, cmd, arg);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001979
Darrick J. Wonge89c0412017-03-28 14:56:37 -07001980 case FS_IOC_GETFSMAP:
1981 return xfs_ioc_getfsmap(ip, arg);
1982
Darrick J. Wong36fd6e82017-10-17 21:37:34 -07001983 case XFS_IOC_SCRUB_METADATA:
1984 return xfs_ioc_scrub_metadata(ip, arg);
1985
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001986 case XFS_IOC_FD_TO_HANDLE:
1987 case XFS_IOC_PATH_TO_HANDLE:
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06001988 case XFS_IOC_PATH_TO_FSHANDLE: {
1989 xfs_fsop_handlereq_t hreq;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001990
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06001991 if (copy_from_user(&hreq, arg, sizeof(hreq)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10001992 return -EFAULT;
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06001993 return xfs_find_handle(cmd, &hreq);
1994 }
1995 case XFS_IOC_OPEN_BY_HANDLE: {
1996 xfs_fsop_handlereq_t hreq;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001997
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06001998 if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10001999 return -EFAULT;
Christoph Hellwigd296d302009-01-19 02:02:57 +01002000 return xfs_open_by_handle(filp, &hreq);
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06002001 }
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002002 case XFS_IOC_FSSETDM_BY_HANDLE:
Christoph Hellwigd296d302009-01-19 02:02:57 +01002003 return xfs_fssetdm_by_handle(filp, arg);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002004
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06002005 case XFS_IOC_READLINK_BY_HANDLE: {
2006 xfs_fsop_handlereq_t hreq;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002007
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06002008 if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002009 return -EFAULT;
Christoph Hellwigd296d302009-01-19 02:02:57 +01002010 return xfs_readlink_by_handle(filp, &hreq);
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06002011 }
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002012 case XFS_IOC_ATTRLIST_BY_HANDLE:
Christoph Hellwigd296d302009-01-19 02:02:57 +01002013 return xfs_attrlist_by_handle(filp, arg);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002014
2015 case XFS_IOC_ATTRMULTI_BY_HANDLE:
Christoph Hellwigd296d302009-01-19 02:02:57 +01002016 return xfs_attrmulti_by_handle(filp, arg);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002017
2018 case XFS_IOC_SWAPEXT: {
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06002019 struct xfs_swapext sxp;
2020
2021 if (copy_from_user(&sxp, arg, sizeof(xfs_swapext_t)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002022 return -EFAULT;
Jan Karad9457dc2012-06-12 16:20:39 +02002023 error = mnt_want_write_file(filp);
2024 if (error)
2025 return error;
Dave Chinnera133d952013-08-12 20:49:48 +10002026 error = xfs_ioc_swapext(&sxp);
Jan Karad9457dc2012-06-12 16:20:39 +02002027 mnt_drop_write_file(filp);
Dave Chinner24513372014-06-25 14:58:08 +10002028 return error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002029 }
2030
2031 case XFS_IOC_FSCOUNTS: {
2032 xfs_fsop_counts_t out;
2033
2034 error = xfs_fs_counts(mp, &out);
2035 if (error)
Dave Chinner24513372014-06-25 14:58:08 +10002036 return error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002037
2038 if (copy_to_user(arg, &out, sizeof(out)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002039 return -EFAULT;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002040 return 0;
2041 }
2042
2043 case XFS_IOC_SET_RESBLKS: {
2044 xfs_fsop_resblks_t inout;
Darrick J. Wongc8ce5402017-06-16 11:00:05 -07002045 uint64_t in;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002046
2047 if (!capable(CAP_SYS_ADMIN))
2048 return -EPERM;
2049
Eric Sandeend5db0f92010-02-05 22:59:53 +00002050 if (mp->m_flags & XFS_MOUNT_RDONLY)
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002051 return -EROFS;
Eric Sandeend5db0f92010-02-05 22:59:53 +00002052
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002053 if (copy_from_user(&inout, arg, sizeof(inout)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002054 return -EFAULT;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002055
Jan Karad9457dc2012-06-12 16:20:39 +02002056 error = mnt_want_write_file(filp);
2057 if (error)
2058 return error;
2059
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002060 /* input parameter is passed in resblks field of structure */
2061 in = inout.resblks;
2062 error = xfs_reserve_blocks(mp, &in, &inout);
Jan Karad9457dc2012-06-12 16:20:39 +02002063 mnt_drop_write_file(filp);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002064 if (error)
Dave Chinner24513372014-06-25 14:58:08 +10002065 return error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002066
2067 if (copy_to_user(arg, &inout, sizeof(inout)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002068 return -EFAULT;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002069 return 0;
2070 }
2071
2072 case XFS_IOC_GET_RESBLKS: {
2073 xfs_fsop_resblks_t out;
2074
2075 if (!capable(CAP_SYS_ADMIN))
2076 return -EPERM;
2077
2078 error = xfs_reserve_blocks(mp, NULL, &out);
2079 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, &out, sizeof(out)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002083 return -EFAULT;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002084
2085 return 0;
2086 }
2087
2088 case XFS_IOC_FSGROWFSDATA: {
2089 xfs_growfs_data_t in;
2090
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002091 if (copy_from_user(&in, arg, sizeof(in)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002092 return -EFAULT;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002093
Jan Karad9457dc2012-06-12 16:20:39 +02002094 error = mnt_want_write_file(filp);
2095 if (error)
2096 return error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002097 error = xfs_growfs_data(mp, &in);
Jan Karad9457dc2012-06-12 16:20:39 +02002098 mnt_drop_write_file(filp);
Dave Chinner24513372014-06-25 14:58:08 +10002099 return error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002100 }
2101
2102 case XFS_IOC_FSGROWFSLOG: {
2103 xfs_growfs_log_t in;
2104
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002105 if (copy_from_user(&in, arg, sizeof(in)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002106 return -EFAULT;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002107
Jan Karad9457dc2012-06-12 16:20:39 +02002108 error = mnt_want_write_file(filp);
2109 if (error)
2110 return error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002111 error = xfs_growfs_log(mp, &in);
Jan Karad9457dc2012-06-12 16:20:39 +02002112 mnt_drop_write_file(filp);
Dave Chinner24513372014-06-25 14:58:08 +10002113 return error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002114 }
2115
2116 case XFS_IOC_FSGROWFSRT: {
2117 xfs_growfs_rt_t in;
2118
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002119 if (copy_from_user(&in, arg, sizeof(in)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002120 return -EFAULT;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002121
Jan Karad9457dc2012-06-12 16:20:39 +02002122 error = mnt_want_write_file(filp);
2123 if (error)
2124 return error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002125 error = xfs_growfs_rt(mp, &in);
Jan Karad9457dc2012-06-12 16:20:39 +02002126 mnt_drop_write_file(filp);
Dave Chinner24513372014-06-25 14:58:08 +10002127 return error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002128 }
2129
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002130 case XFS_IOC_GOINGDOWN: {
Darrick J. Wongc8ce5402017-06-16 11:00:05 -07002131 uint32_t in;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002132
2133 if (!capable(CAP_SYS_ADMIN))
2134 return -EPERM;
2135
Darrick J. Wongc8ce5402017-06-16 11:00:05 -07002136 if (get_user(in, (uint32_t __user *)arg))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002137 return -EFAULT;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002138
Dave Chinner24513372014-06-25 14:58:08 +10002139 return xfs_fs_goingdown(mp, in);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002140 }
2141
2142 case XFS_IOC_ERROR_INJECTION: {
2143 xfs_error_injection_t in;
2144
2145 if (!capable(CAP_SYS_ADMIN))
2146 return -EPERM;
2147
2148 if (copy_from_user(&in, arg, sizeof(in)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002149 return -EFAULT;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002150
Darrick J. Wong31965ef2017-06-20 17:54:46 -07002151 return xfs_errortag_add(mp, in.errtag);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002152 }
2153
2154 case XFS_IOC_ERROR_CLEARALL:
2155 if (!capable(CAP_SYS_ADMIN))
2156 return -EPERM;
2157
Darrick J. Wong31965ef2017-06-20 17:54:46 -07002158 return xfs_errortag_clearall(mp);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002159
Brian Foster8ca149d2012-11-07 12:21:12 -05002160 case XFS_IOC_FREE_EOFBLOCKS: {
Dwight Engenb9fe5052013-08-15 14:08:02 -04002161 struct xfs_fs_eofblocks eofb;
2162 struct xfs_eofblocks keofb;
Brian Foster8ca149d2012-11-07 12:21:12 -05002163
Dwight Engen8c567a72013-08-15 14:08:03 -04002164 if (!capable(CAP_SYS_ADMIN))
2165 return -EPERM;
2166
2167 if (mp->m_flags & XFS_MOUNT_RDONLY)
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002168 return -EROFS;
Dwight Engen8c567a72013-08-15 14:08:03 -04002169
Brian Foster8ca149d2012-11-07 12:21:12 -05002170 if (copy_from_user(&eofb, arg, sizeof(eofb)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002171 return -EFAULT;
Brian Foster8ca149d2012-11-07 12:21:12 -05002172
Dwight Engenb9fe5052013-08-15 14:08:02 -04002173 error = xfs_fs_eofblocks_from_user(&eofb, &keofb);
2174 if (error)
Dave Chinner24513372014-06-25 14:58:08 +10002175 return error;
Brian Foster8ca149d2012-11-07 12:21:12 -05002176
Dave Chinner24513372014-06-25 14:58:08 +10002177 return xfs_icache_free_eofblocks(mp, &keofb);
Brian Foster8ca149d2012-11-07 12:21:12 -05002178 }
2179
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002180 default:
2181 return -ENOTTY;
2182 }
2183}