blob: 78b2e29ffae343fb457ca68b512aea8fc2c07593 [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"
Christoph Hellwig781355c2015-02-16 11:59:50 +110030#include "xfs_pnfs.h"
Andreas Gruenbacher47e1bf62015-11-03 12:56:17 +110031#include "xfs_acl.h"
Darrick J. Wonge89c0412017-03-28 14:56:37 -070032#include "xfs_btree.h"
33#include <linux/fsmap.h>
34#include "xfs_fsmap.h"
Darrick J. Wong36fd6e82017-10-17 21:37:34 -070035#include "scrub/xfs_scrub.h"
Darrick J. Wongc368ebc2018-01-08 10:51:27 -080036#include "xfs_sb.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080038#include <linux/capability.h>
Ingo Molnar5b825c32017-02-02 17:54:15 +010039#include <linux/cred.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/dcache.h>
41#include <linux/mount.h>
42#include <linux/namei.h>
43#include <linux/pagemap.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Christoph Hellwigd296d302009-01-19 02:02:57 +010045#include <linux/exportfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47/*
48 * xfs_find_handle maps from userspace xfs_fsop_handlereq structure to
49 * a file or fs handle.
50 *
51 * XFS_IOC_PATH_TO_FSHANDLE
52 * returns fs handle for a mount point or path within that mount point
53 * XFS_IOC_FD_TO_HANDLE
54 * returns full handle for a FD opened in user space
55 * XFS_IOC_PATH_TO_HANDLE
56 * returns full handle for a path
57 */
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -060058int
Linus Torvalds1da177e2005-04-16 15:20:36 -070059xfs_find_handle(
60 unsigned int cmd,
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -060061 xfs_fsop_handlereq_t *hreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
63 int hsize;
64 xfs_handle_t handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 struct inode *inode;
Dave Chinnera30b0362013-09-02 20:49:36 +100066 struct fd f = {NULL};
Christoph Hellwig4346cdd2009-02-08 21:51:14 +010067 struct path path;
Al Viro2903ff02012-08-28 12:52:22 -040068 int error;
Christoph Hellwig4346cdd2009-02-08 21:51:14 +010069 struct xfs_inode *ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Christoph Hellwig4346cdd2009-02-08 21:51:14 +010071 if (cmd == XFS_IOC_FD_TO_HANDLE) {
Al Viro2903ff02012-08-28 12:52:22 -040072 f = fdget(hreq->fd);
73 if (!f.file)
Christoph Hellwig4346cdd2009-02-08 21:51:14 +010074 return -EBADF;
Al Viro496ad9a2013-01-23 17:07:38 -050075 inode = file_inode(f.file);
Christoph Hellwig4346cdd2009-02-08 21:51:14 +010076 } else {
77 error = user_lpath((const char __user *)hreq->path, &path);
78 if (error)
79 return error;
David Howells2b0143b2015-03-17 22:25:59 +000080 inode = d_inode(path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 }
Christoph Hellwig4346cdd2009-02-08 21:51:14 +010082 ip = XFS_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Christoph Hellwig4346cdd2009-02-08 21:51:14 +010084 /*
85 * We can only generate handles for inodes residing on a XFS filesystem,
86 * and only for regular files, directories or symbolic links.
87 */
88 error = -EINVAL;
89 if (inode->i_sb->s_magic != XFS_SB_MAGIC)
90 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Christoph Hellwig4346cdd2009-02-08 21:51:14 +010092 error = -EBADF;
93 if (!S_ISREG(inode->i_mode) &&
94 !S_ISDIR(inode->i_mode) &&
95 !S_ISLNK(inode->i_mode))
96 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Christoph Hellwig4346cdd2009-02-08 21:51:14 +010099 memcpy(&handle.ha_fsid, ip->i_mount->m_fixedfsid, sizeof(xfs_fsid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Christoph Hellwig4346cdd2009-02-08 21:51:14 +0100101 if (cmd == XFS_IOC_PATH_TO_FSHANDLE) {
102 /*
103 * This handle only contains an fsid, zero the rest.
104 */
105 memset(&handle.ha_fid, 0, sizeof(handle.ha_fid));
106 hsize = sizeof(xfs_fsid_t);
107 } else {
Christoph Hellwigc6143912007-09-14 15:22:37 +1000108 handle.ha_fid.fid_len = sizeof(xfs_fid_t) -
109 sizeof(handle.ha_fid.fid_len);
110 handle.ha_fid.fid_pad = 0;
Dave Chinner9e9a2672016-02-09 16:54:58 +1100111 handle.ha_fid.fid_gen = inode->i_generation;
Christoph Hellwigc6143912007-09-14 15:22:37 +1000112 handle.ha_fid.fid_ino = ip->i_ino;
Christoph Hellwig3398a402017-06-14 21:30:44 -0700113 hsize = sizeof(xfs_handle_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 }
115
Christoph Hellwig4346cdd2009-02-08 21:51:14 +0100116 error = -EFAULT;
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -0600117 if (copy_to_user(hreq->ohandle, &handle, hsize) ||
Christoph Hellwig4346cdd2009-02-08 21:51:14 +0100118 copy_to_user(hreq->ohandlen, &hsize, sizeof(__s32)))
119 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Christoph Hellwig4346cdd2009-02-08 21:51:14 +0100121 error = 0;
122
123 out_put:
124 if (cmd == XFS_IOC_FD_TO_HANDLE)
Al Viro2903ff02012-08-28 12:52:22 -0400125 fdput(f);
Christoph Hellwig4346cdd2009-02-08 21:51:14 +0100126 else
127 path_put(&path);
128 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129}
130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131/*
Christoph Hellwigd296d302009-01-19 02:02:57 +0100132 * No need to do permission checks on the various pathname components
133 * as the handle operations are privileged.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 */
135STATIC int
Christoph Hellwigd296d302009-01-19 02:02:57 +0100136xfs_handle_acceptable(
137 void *context,
138 struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
Christoph Hellwigd296d302009-01-19 02:02:57 +0100140 return 1;
141}
142
143/*
144 * Convert userspace handle data into a dentry.
145 */
146struct dentry *
147xfs_handle_to_dentry(
148 struct file *parfilp,
149 void __user *uhandle,
150 u32 hlen)
151{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 xfs_handle_t handle;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100153 struct xfs_fid64 fid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155 /*
156 * Only allow handle opens under a directory.
157 */
Al Viro496ad9a2013-01-23 17:07:38 -0500158 if (!S_ISDIR(file_inode(parfilp)->i_mode))
Christoph Hellwigd296d302009-01-19 02:02:57 +0100159 return ERR_PTR(-ENOTDIR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Christoph Hellwigd296d302009-01-19 02:02:57 +0100161 if (hlen != sizeof(xfs_handle_t))
162 return ERR_PTR(-EINVAL);
163 if (copy_from_user(&handle, uhandle, hlen))
164 return ERR_PTR(-EFAULT);
165 if (handle.ha_fid.fid_len !=
166 sizeof(handle.ha_fid) - sizeof(handle.ha_fid.fid_len))
167 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Christoph Hellwigd296d302009-01-19 02:02:57 +0100169 memset(&fid, 0, sizeof(struct fid));
170 fid.ino = handle.ha_fid.fid_ino;
171 fid.gen = handle.ha_fid.fid_gen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Christoph Hellwigd296d302009-01-19 02:02:57 +0100173 return exportfs_decode_fh(parfilp->f_path.mnt, (struct fid *)&fid, 3,
174 FILEID_INO32_GEN | XFS_FILEID_TYPE_64FLAG,
175 xfs_handle_acceptable, NULL);
176}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Christoph Hellwigd296d302009-01-19 02:02:57 +0100178STATIC struct dentry *
179xfs_handlereq_to_dentry(
180 struct file *parfilp,
181 xfs_fsop_handlereq_t *hreq)
182{
183 return xfs_handle_to_dentry(parfilp, hreq->ihandle, hreq->ihandlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184}
185
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -0600186int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187xfs_open_by_handle(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 struct file *parfilp,
Christoph Hellwigd296d302009-01-19 02:02:57 +0100189 xfs_fsop_handlereq_t *hreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
David Howells745ca242008-11-14 10:39:22 +1100191 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 int error;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100193 int fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 int permflag;
195 struct file *filp;
196 struct inode *inode;
197 struct dentry *dentry;
Dave Chinner1a1d7722012-03-22 05:15:06 +0000198 fmode_t fmode;
Al Viro765927b2012-06-26 21:58:53 +0400199 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201 if (!capable(CAP_SYS_ADMIN))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000202 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Christoph Hellwigd296d302009-01-19 02:02:57 +0100204 dentry = xfs_handlereq_to_dentry(parfilp, hreq);
205 if (IS_ERR(dentry))
206 return PTR_ERR(dentry);
David Howells2b0143b2015-03-17 22:25:59 +0000207 inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 /* Restrict xfs_open_by_handle to directories & regular files. */
210 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode))) {
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000211 error = -EPERM;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100212 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 }
214
215#if BITS_PER_LONG != 32
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -0600216 hreq->oflags |= O_LARGEFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217#endif
Christoph Hellwigd296d302009-01-19 02:02:57 +0100218
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -0600219 permflag = hreq->oflags;
Dave Chinner1a1d7722012-03-22 05:15:06 +0000220 fmode = OPEN_FMODE(permflag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 if ((!(permflag & O_APPEND) || (permflag & O_TRUNC)) &&
Dave Chinner1a1d7722012-03-22 05:15:06 +0000222 (fmode & FMODE_WRITE) && IS_APPEND(inode)) {
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000223 error = -EPERM;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100224 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 }
226
Dave Chinner1a1d7722012-03-22 05:15:06 +0000227 if ((fmode & FMODE_WRITE) && IS_IMMUTABLE(inode)) {
Eryu Guan337684a2016-08-02 19:58:28 +0800228 error = -EPERM;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100229 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 }
231
232 /* Can't write directories. */
Dave Chinner1a1d7722012-03-22 05:15:06 +0000233 if (S_ISDIR(inode->i_mode) && (fmode & FMODE_WRITE)) {
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000234 error = -EISDIR;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100235 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 }
237
Yann Droneaud862a6292013-07-02 18:39:34 +0200238 fd = get_unused_fd_flags(0);
Christoph Hellwigd296d302009-01-19 02:02:57 +0100239 if (fd < 0) {
240 error = fd;
241 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 }
243
Al Viro765927b2012-06-26 21:58:53 +0400244 path.mnt = parfilp->f_path.mnt;
245 path.dentry = dentry;
246 filp = dentry_open(&path, hreq->oflags, cred);
247 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 if (IS_ERR(filp)) {
Christoph Hellwigd296d302009-01-19 02:02:57 +0100249 put_unused_fd(fd);
250 return PTR_ERR(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 }
Christoph Hellwig4d4be482008-12-09 04:47:33 -0500252
Al Viro03209372011-07-25 20:54:24 -0400253 if (S_ISREG(inode->i_mode)) {
Vlad Apostolov2e2e7bb2006-11-11 18:04:47 +1100254 filp->f_flags |= O_NOATIME;
Christoph Hellwig4d4be482008-12-09 04:47:33 -0500255 filp->f_mode |= FMODE_NOCMTIME;
Vlad Apostolov2e2e7bb2006-11-11 18:04:47 +1100256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Christoph Hellwigd296d302009-01-19 02:02:57 +0100258 fd_install(fd, filp);
259 return fd;
260
261 out_dput:
262 dput(dentry);
263 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264}
265
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -0600266int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267xfs_readlink_by_handle(
Christoph Hellwigd296d302009-01-19 02:02:57 +0100268 struct file *parfilp,
269 xfs_fsop_handlereq_t *hreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
Christoph Hellwigd296d302009-01-19 02:02:57 +0100271 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 __u32 olen;
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000273 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
275 if (!capable(CAP_SYS_ADMIN))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000276 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Christoph Hellwigd296d302009-01-19 02:02:57 +0100278 dentry = xfs_handlereq_to_dentry(parfilp, hreq);
279 if (IS_ERR(dentry))
280 return PTR_ERR(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282 /* Restrict this handle operation to symlinks only. */
Miklos Szeredifd4a0ed2016-12-09 16:45:04 +0100283 if (!d_is_symlink(dentry)) {
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000284 error = -EINVAL;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100285 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 }
287
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -0600288 if (copy_from_user(&olen, hreq->ohandlen, sizeof(__u32))) {
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000289 error = -EFAULT;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100290 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
Miklos Szeredifd4a0ed2016-12-09 16:45:04 +0100293 error = vfs_readlink(dentry, hreq->ohandle, olen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Christoph Hellwigd296d302009-01-19 02:02:57 +0100295 out_dput:
296 dput(dentry);
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000297 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
299
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000300int
301xfs_set_dmattrs(
302 xfs_inode_t *ip,
Darrick J. Wong65a79352017-11-09 09:34:28 -0800303 uint evmask,
304 uint16_t state)
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000305{
306 xfs_mount_t *mp = ip->i_mount;
307 xfs_trans_t *tp;
308 int error;
309
310 if (!capable(CAP_SYS_ADMIN))
Dave Chinner24513372014-06-25 14:58:08 +1000311 return -EPERM;
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000312
313 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +1000314 return -EIO;
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000315
Christoph Hellwig253f4912016-04-06 09:19:55 +1000316 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
317 if (error)
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000318 return error;
Christoph Hellwig253f4912016-04-06 09:19:55 +1000319
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000320 xfs_ilock(ip, XFS_ILOCK_EXCL);
321 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
322
323 ip->i_d.di_dmevmask = evmask;
324 ip->i_d.di_dmstate = state;
325
326 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Christoph Hellwig70393312015-06-04 13:48:08 +1000327 error = xfs_trans_commit(tp);
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000328
329 return error;
330}
331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332STATIC int
333xfs_fssetdm_by_handle(
Christoph Hellwigd296d302009-01-19 02:02:57 +0100334 struct file *parfilp,
335 void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
337 int error;
338 struct fsdmidata fsd;
339 xfs_fsop_setdm_handlereq_t dmhreq;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100340 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342 if (!capable(CAP_MKNOD))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000343 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 if (copy_from_user(&dmhreq, arg, sizeof(xfs_fsop_setdm_handlereq_t)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000345 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Jan Karad9457dc2012-06-12 16:20:39 +0200347 error = mnt_want_write_file(parfilp);
348 if (error)
349 return error;
350
Christoph Hellwigd296d302009-01-19 02:02:57 +0100351 dentry = xfs_handlereq_to_dentry(parfilp, &dmhreq.hreq);
Jan Karad9457dc2012-06-12 16:20:39 +0200352 if (IS_ERR(dentry)) {
353 mnt_drop_write_file(parfilp);
Christoph Hellwigd296d302009-01-19 02:02:57 +0100354 return PTR_ERR(dentry);
Jan Karad9457dc2012-06-12 16:20:39 +0200355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
David Howells2b0143b2015-03-17 22:25:59 +0000357 if (IS_IMMUTABLE(d_inode(dentry)) || IS_APPEND(d_inode(dentry))) {
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000358 error = -EPERM;
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000359 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 }
361
362 if (copy_from_user(&fsd, dmhreq.data, sizeof(fsd))) {
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000363 error = -EFAULT;
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000364 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 }
366
David Howells2b0143b2015-03-17 22:25:59 +0000367 error = xfs_set_dmattrs(XFS_I(d_inode(dentry)), fsd.fsd_dmevmask,
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000368 fsd.fsd_dmstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000370 out:
Jan Karad9457dc2012-06-12 16:20:39 +0200371 mnt_drop_write_file(parfilp);
Christoph Hellwigd296d302009-01-19 02:02:57 +0100372 dput(dentry);
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000373 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374}
375
376STATIC int
377xfs_attrlist_by_handle(
Christoph Hellwigd296d302009-01-19 02:02:57 +0100378 struct file *parfilp,
379 void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
Christoph Hellwigd296d302009-01-19 02:02:57 +0100381 int error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 attrlist_cursor_kern_t *cursor;
Darrick J. Wong0facef72016-08-03 10:58:53 +1000383 struct xfs_fsop_attrlist_handlereq __user *p = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 xfs_fsop_attrlist_handlereq_t al_hreq;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100385 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 char *kbuf;
387
388 if (!capable(CAP_SYS_ADMIN))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000389 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 if (copy_from_user(&al_hreq, arg, sizeof(xfs_fsop_attrlist_handlereq_t)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000391 return -EFAULT;
Dan Carpenter071c5292013-10-31 21:00:10 +0300392 if (al_hreq.buflen < sizeof(struct attrlist) ||
Jan Tulak4e247612015-10-12 16:02:56 +1100393 al_hreq.buflen > XFS_XATTR_LIST_MAX)
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000394 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Christoph Hellwig90ad58a2008-06-27 13:32:19 +1000396 /*
397 * Reject flags, only allow namespaces.
398 */
399 if (al_hreq.flags & ~(ATTR_ROOT | ATTR_SECURE))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000400 return -EINVAL;
Christoph Hellwig90ad58a2008-06-27 13:32:19 +1000401
Christoph Hellwigd296d302009-01-19 02:02:57 +0100402 dentry = xfs_handlereq_to_dentry(parfilp, &al_hreq.hreq);
403 if (IS_ERR(dentry))
404 return PTR_ERR(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Dave Chinnerfdd3cce2013-09-02 20:53:00 +1000406 kbuf = kmem_zalloc_large(al_hreq.buflen, KM_SLEEP);
407 if (!kbuf)
408 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410 cursor = (attrlist_cursor_kern_t *)&al_hreq.pos;
David Howells2b0143b2015-03-17 22:25:59 +0000411 error = xfs_attr_list(XFS_I(d_inode(dentry)), kbuf, al_hreq.buflen,
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000412 al_hreq.flags, cursor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 if (error)
414 goto out_kfree;
415
Darrick J. Wong0facef72016-08-03 10:58:53 +1000416 if (copy_to_user(&p->pos, cursor, sizeof(attrlist_cursor_kern_t))) {
417 error = -EFAULT;
418 goto out_kfree;
419 }
420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 if (copy_to_user(al_hreq.buffer, kbuf, al_hreq.buflen))
422 error = -EFAULT;
423
Dave Chinnerfdd3cce2013-09-02 20:53:00 +1000424out_kfree:
425 kmem_free(kbuf);
426out_dput:
Christoph Hellwigd296d302009-01-19 02:02:57 +0100427 dput(dentry);
428 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429}
430
sandeen@sandeen.net28750972008-11-25 21:20:15 -0600431int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432xfs_attrmulti_attr_get(
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000433 struct inode *inode,
Dave Chinnera9273ca2010-01-20 10:47:48 +1100434 unsigned char *name,
435 unsigned char __user *ubuf,
Darrick J. Wongc8ce5402017-06-16 11:00:05 -0700436 uint32_t *len,
437 uint32_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
Dave Chinnera9273ca2010-01-20 10:47:48 +1100439 unsigned char *kbuf;
Dave Chinner24513372014-06-25 14:58:08 +1000440 int error = -EFAULT;
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000441
Jan Tulak51fcbfe2015-10-12 16:03:59 +1100442 if (*len > XFS_XATTR_SIZE_MAX)
Dave Chinner24513372014-06-25 14:58:08 +1000443 return -EINVAL;
Dave Chinnerfdd3cce2013-09-02 20:53:00 +1000444 kbuf = kmem_zalloc_large(*len, KM_SLEEP);
445 if (!kbuf)
Dave Chinner24513372014-06-25 14:58:08 +1000446 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000448 error = xfs_attr_get(XFS_I(inode), name, kbuf, (int *)len, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 if (error)
450 goto out_kfree;
451
452 if (copy_to_user(ubuf, kbuf, *len))
Dave Chinner24513372014-06-25 14:58:08 +1000453 error = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Dave Chinnerfdd3cce2013-09-02 20:53:00 +1000455out_kfree:
456 kmem_free(kbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 return error;
458}
459
sandeen@sandeen.net28750972008-11-25 21:20:15 -0600460int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461xfs_attrmulti_attr_set(
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000462 struct inode *inode,
Dave Chinnera9273ca2010-01-20 10:47:48 +1100463 unsigned char *name,
464 const unsigned char __user *ubuf,
Darrick J. Wongc8ce5402017-06-16 11:00:05 -0700465 uint32_t len,
466 uint32_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
Dave Chinnera9273ca2010-01-20 10:47:48 +1100468 unsigned char *kbuf;
Andreas Gruenbacher09cb22d2015-11-03 12:53:54 +1100469 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000471 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
Dave Chinner24513372014-06-25 14:58:08 +1000472 return -EPERM;
Jan Tulak51fcbfe2015-10-12 16:03:59 +1100473 if (len > XFS_XATTR_SIZE_MAX)
Dave Chinner24513372014-06-25 14:58:08 +1000474 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Li Zefan0e639bd2009-04-08 15:08:04 +0800476 kbuf = memdup_user(ubuf, len);
477 if (IS_ERR(kbuf))
478 return PTR_ERR(kbuf);
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000479
Andreas Gruenbacher09cb22d2015-11-03 12:53:54 +1100480 error = xfs_attr_set(XFS_I(inode), name, kbuf, len, flags);
Andreas Gruenbacher47e1bf62015-11-03 12:56:17 +1100481 if (!error)
482 xfs_forget_acl(inode, name, flags);
Andreas Gruenbacher09cb22d2015-11-03 12:53:54 +1100483 kfree(kbuf);
484 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485}
486
sandeen@sandeen.net28750972008-11-25 21:20:15 -0600487int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488xfs_attrmulti_attr_remove(
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000489 struct inode *inode,
Dave Chinnera9273ca2010-01-20 10:47:48 +1100490 unsigned char *name,
Darrick J. Wongc8ce5402017-06-16 11:00:05 -0700491 uint32_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
Andreas Gruenbacher47e1bf62015-11-03 12:56:17 +1100493 int error;
494
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000495 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
Dave Chinner24513372014-06-25 14:58:08 +1000496 return -EPERM;
Andreas Gruenbacher47e1bf62015-11-03 12:56:17 +1100497 error = xfs_attr_remove(XFS_I(inode), name, flags);
498 if (!error)
499 xfs_forget_acl(inode, name, flags);
500 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501}
502
503STATIC int
504xfs_attrmulti_by_handle(
Dave Hansen42a74f22008-02-15 14:37:46 -0800505 struct file *parfilp,
Christoph Hellwigd296d302009-01-19 02:02:57 +0100506 void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
508 int error;
509 xfs_attr_multiop_t *ops;
510 xfs_fsop_attrmulti_handlereq_t am_hreq;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100511 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 unsigned int i, size;
Dave Chinnera9273ca2010-01-20 10:47:48 +1100513 unsigned char *attr_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515 if (!capable(CAP_SYS_ADMIN))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000516 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 if (copy_from_user(&am_hreq, arg, sizeof(xfs_fsop_attrmulti_handlereq_t)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000518 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Zhitong Wangfda168c2010-03-23 09:51:22 +1100520 /* overflow check */
521 if (am_hreq.opcount >= INT_MAX / sizeof(xfs_attr_multiop_t))
522 return -E2BIG;
523
Christoph Hellwigd296d302009-01-19 02:02:57 +0100524 dentry = xfs_handlereq_to_dentry(parfilp, &am_hreq.hreq);
525 if (IS_ERR(dentry))
526 return PTR_ERR(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Dave Chinner24513372014-06-25 14:58:08 +1000528 error = -E2BIG;
Christoph Hellwige182f572008-06-27 13:32:31 +1000529 size = am_hreq.opcount * sizeof(xfs_attr_multiop_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 if (!size || size > 16 * PAGE_SIZE)
Christoph Hellwigd296d302009-01-19 02:02:57 +0100531 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Li Zefan0e639bd2009-04-08 15:08:04 +0800533 ops = memdup_user(am_hreq.ops, size);
534 if (IS_ERR(ops)) {
Dave Chinner24513372014-06-25 14:58:08 +1000535 error = PTR_ERR(ops);
Christoph Hellwigd296d302009-01-19 02:02:57 +0100536 goto out_dput;
Li Zefan0e639bd2009-04-08 15:08:04 +0800537 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Dave Chinner24513372014-06-25 14:58:08 +1000539 error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 attr_name = kmalloc(MAXNAMELEN, GFP_KERNEL);
541 if (!attr_name)
542 goto out_kfree_ops;
543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 error = 0;
545 for (i = 0; i < am_hreq.opcount; i++) {
Dave Chinnera9273ca2010-01-20 10:47:48 +1100546 ops[i].am_error = strncpy_from_user((char *)attr_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 ops[i].am_attrname, MAXNAMELEN);
548 if (ops[i].am_error == 0 || ops[i].am_error == MAXNAMELEN)
Dave Chinner24513372014-06-25 14:58:08 +1000549 error = -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 if (ops[i].am_error < 0)
551 break;
552
553 switch (ops[i].am_opcode) {
554 case ATTR_OP_GET:
Christoph Hellwigd296d302009-01-19 02:02:57 +0100555 ops[i].am_error = xfs_attrmulti_attr_get(
David Howells2b0143b2015-03-17 22:25:59 +0000556 d_inode(dentry), attr_name,
Christoph Hellwigd296d302009-01-19 02:02:57 +0100557 ops[i].am_attrvalue, &ops[i].am_length,
558 ops[i].am_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 break;
560 case ATTR_OP_SET:
Al Viroa561be72011-11-23 11:57:51 -0500561 ops[i].am_error = mnt_want_write_file(parfilp);
Dave Hansen42a74f22008-02-15 14:37:46 -0800562 if (ops[i].am_error)
563 break;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100564 ops[i].am_error = xfs_attrmulti_attr_set(
David Howells2b0143b2015-03-17 22:25:59 +0000565 d_inode(dentry), attr_name,
Christoph Hellwigd296d302009-01-19 02:02:57 +0100566 ops[i].am_attrvalue, ops[i].am_length,
567 ops[i].am_flags);
Al Viro2a79f172011-12-09 08:06:57 -0500568 mnt_drop_write_file(parfilp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 break;
570 case ATTR_OP_REMOVE:
Al Viroa561be72011-11-23 11:57:51 -0500571 ops[i].am_error = mnt_want_write_file(parfilp);
Dave Hansen42a74f22008-02-15 14:37:46 -0800572 if (ops[i].am_error)
573 break;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100574 ops[i].am_error = xfs_attrmulti_attr_remove(
David Howells2b0143b2015-03-17 22:25:59 +0000575 d_inode(dentry), attr_name,
Christoph Hellwigd296d302009-01-19 02:02:57 +0100576 ops[i].am_flags);
Al Viro2a79f172011-12-09 08:06:57 -0500577 mnt_drop_write_file(parfilp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 break;
579 default:
Dave Chinner24513372014-06-25 14:58:08 +1000580 ops[i].am_error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 }
582 }
583
584 if (copy_to_user(am_hreq.ops, ops, size))
Dave Chinner24513372014-06-25 14:58:08 +1000585 error = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587 kfree(attr_name);
588 out_kfree_ops:
589 kfree(ops);
Christoph Hellwigd296d302009-01-19 02:02:57 +0100590 out_dput:
591 dput(dentry);
Dave Chinner24513372014-06-25 14:58:08 +1000592 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593}
594
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -0600595int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596xfs_ioc_space(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 struct file *filp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 unsigned int cmd,
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -0600599 xfs_flock64_t *bf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600{
Christoph Hellwig8f3e2052016-07-20 11:29:35 +1000601 struct inode *inode = file_inode(filp);
602 struct xfs_inode *ip = XFS_I(inode);
Christoph Hellwig865e9442013-10-12 00:55:08 -0700603 struct iattr iattr;
Christoph Hellwig8add71c2015-02-02 09:53:56 +1100604 enum xfs_prealloc_flags flags = 0;
Christoph Hellwig781355c2015-02-16 11:59:50 +1100605 uint iolock = XFS_IOLOCK_EXCL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 int error;
607
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -0600608 /*
609 * Only allow the sys admin to reserve space unless
610 * unwritten extents are enabled.
611 */
612 if (!xfs_sb_version_hasextflgbit(&ip->i_mount->m_sb) &&
613 !capable(CAP_SYS_ADMIN))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000614 return -EPERM;
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -0600615
Alexey Dobriyanf37ea142006-09-28 10:52:04 +1000616 if (inode->i_flags & (S_IMMUTABLE|S_APPEND))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000617 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Eric Sandeenad4a8ac2005-09-02 16:41:16 +1000619 if (!(filp->f_mode & FMODE_WRITE))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000620 return -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Alexey Dobriyanf37ea142006-09-28 10:52:04 +1000622 if (!S_ISREG(inode->i_mode))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000623 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Christoph Hellwig8add71c2015-02-02 09:53:56 +1100625 if (filp->f_flags & O_DSYNC)
626 flags |= XFS_PREALLOC_SYNC;
Christoph Hellwig8f3e2052016-07-20 11:29:35 +1000627 if (filp->f_mode & FMODE_NOCMTIME)
Christoph Hellwig8add71c2015-02-02 09:53:56 +1100628 flags |= XFS_PREALLOC_INVISIBLE;
629
Jan Karad9457dc2012-06-12 16:20:39 +0200630 error = mnt_want_write_file(filp);
631 if (error)
632 return error;
Christoph Hellwig865e9442013-10-12 00:55:08 -0700633
Christoph Hellwig781355c2015-02-16 11:59:50 +1100634 xfs_ilock(ip, iolock);
Christoph Hellwig65523212016-11-30 14:33:25 +1100635 error = xfs_break_layouts(inode, &iolock);
Christoph Hellwig781355c2015-02-16 11:59:50 +1100636 if (error)
637 goto out_unlock;
Christoph Hellwig865e9442013-10-12 00:55:08 -0700638
Dave Chinnere8e9ad42015-02-23 21:45:32 +1100639 xfs_ilock(ip, XFS_MMAPLOCK_EXCL);
640 iolock |= XFS_MMAPLOCK_EXCL;
641
Christoph Hellwig865e9442013-10-12 00:55:08 -0700642 switch (bf->l_whence) {
643 case 0: /*SEEK_SET*/
644 break;
645 case 1: /*SEEK_CUR*/
646 bf->l_start += filp->f_pos;
647 break;
648 case 2: /*SEEK_END*/
649 bf->l_start += XFS_ISIZE(ip);
650 break;
651 default:
Dave Chinner24513372014-06-25 14:58:08 +1000652 error = -EINVAL;
Christoph Hellwig865e9442013-10-12 00:55:08 -0700653 goto out_unlock;
654 }
655
656 /*
657 * length of <= 0 for resv/unresv/zero is invalid. length for
658 * alloc/free is ignored completely and we have no idea what userspace
659 * might have set it to, so set it to zero to allow range
660 * checks to pass.
661 */
662 switch (cmd) {
663 case XFS_IOC_ZERO_RANGE:
664 case XFS_IOC_RESVSP:
665 case XFS_IOC_RESVSP64:
666 case XFS_IOC_UNRESVSP:
667 case XFS_IOC_UNRESVSP64:
668 if (bf->l_len <= 0) {
Dave Chinner24513372014-06-25 14:58:08 +1000669 error = -EINVAL;
Christoph Hellwig865e9442013-10-12 00:55:08 -0700670 goto out_unlock;
671 }
672 break;
673 default:
674 bf->l_len = 0;
675 break;
676 }
677
678 if (bf->l_start < 0 ||
Christoph Hellwig8add71c2015-02-02 09:53:56 +1100679 bf->l_start > inode->i_sb->s_maxbytes ||
Christoph Hellwig865e9442013-10-12 00:55:08 -0700680 bf->l_start + bf->l_len < 0 ||
Christoph Hellwig8add71c2015-02-02 09:53:56 +1100681 bf->l_start + bf->l_len >= inode->i_sb->s_maxbytes) {
Dave Chinner24513372014-06-25 14:58:08 +1000682 error = -EINVAL;
Christoph Hellwig865e9442013-10-12 00:55:08 -0700683 goto out_unlock;
684 }
685
686 switch (cmd) {
687 case XFS_IOC_ZERO_RANGE:
Christoph Hellwig8add71c2015-02-02 09:53:56 +1100688 flags |= XFS_PREALLOC_SET;
Christoph Hellwig865e9442013-10-12 00:55:08 -0700689 error = xfs_zero_file_space(ip, bf->l_start, bf->l_len);
Christoph Hellwig865e9442013-10-12 00:55:08 -0700690 break;
691 case XFS_IOC_RESVSP:
692 case XFS_IOC_RESVSP64:
Christoph Hellwig8add71c2015-02-02 09:53:56 +1100693 flags |= XFS_PREALLOC_SET;
Christoph Hellwig865e9442013-10-12 00:55:08 -0700694 error = xfs_alloc_file_space(ip, bf->l_start, bf->l_len,
695 XFS_BMAPI_PREALLOC);
Christoph Hellwig865e9442013-10-12 00:55:08 -0700696 break;
697 case XFS_IOC_UNRESVSP:
698 case XFS_IOC_UNRESVSP64:
699 error = xfs_free_file_space(ip, bf->l_start, bf->l_len);
700 break;
701 case XFS_IOC_ALLOCSP:
702 case XFS_IOC_ALLOCSP64:
703 case XFS_IOC_FREESP:
704 case XFS_IOC_FREESP64:
Christoph Hellwig8add71c2015-02-02 09:53:56 +1100705 flags |= XFS_PREALLOC_CLEAR;
Christoph Hellwig865e9442013-10-12 00:55:08 -0700706 if (bf->l_start > XFS_ISIZE(ip)) {
707 error = xfs_alloc_file_space(ip, XFS_ISIZE(ip),
708 bf->l_start - XFS_ISIZE(ip), 0);
709 if (error)
710 goto out_unlock;
711 }
712
713 iattr.ia_valid = ATTR_SIZE;
714 iattr.ia_size = bf->l_start;
715
Jan Kara69bca802016-05-26 14:46:43 +0200716 error = xfs_vn_setattr_size(file_dentry(filp), &iattr);
Christoph Hellwig865e9442013-10-12 00:55:08 -0700717 break;
718 default:
719 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +1000720 error = -EINVAL;
Christoph Hellwig865e9442013-10-12 00:55:08 -0700721 }
722
723 if (error)
724 goto out_unlock;
725
Christoph Hellwig8add71c2015-02-02 09:53:56 +1100726 error = xfs_update_prealloc_flags(ip, flags);
Christoph Hellwig865e9442013-10-12 00:55:08 -0700727
728out_unlock:
Christoph Hellwig781355c2015-02-16 11:59:50 +1100729 xfs_iunlock(ip, iolock);
Jan Karad9457dc2012-06-12 16:20:39 +0200730 mnt_drop_write_file(filp);
Dave Chinner24513372014-06-25 14:58:08 +1000731 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732}
733
734STATIC int
735xfs_ioc_bulkstat(
736 xfs_mount_t *mp,
737 unsigned int cmd,
738 void __user *arg)
739{
740 xfs_fsop_bulkreq_t bulkreq;
741 int count; /* # of records returned */
742 xfs_ino_t inlast; /* last inode number */
743 int done;
744 int error;
745
746 /* done = 1 if there are more stats to get and if bulkstat */
747 /* should be called again (unused here, but used in dmapi) */
748
749 if (!capable(CAP_SYS_ADMIN))
750 return -EPERM;
751
752 if (XFS_FORCED_SHUTDOWN(mp))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000753 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
755 if (copy_from_user(&bulkreq, arg, sizeof(xfs_fsop_bulkreq_t)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000756 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
758 if (copy_from_user(&inlast, bulkreq.lastip, sizeof(__s64)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000759 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
761 if ((count = bulkreq.icount) <= 0)
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000762 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100764 if (bulkreq.ubuffer == NULL)
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000765 return -EINVAL;
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 if (cmd == XFS_IOC_FSINUMBERS)
768 error = xfs_inumbers(mp, &inlast, &count,
Michal Marekfaa63e92007-07-11 11:10:19 +1000769 bulkreq.ubuffer, xfs_inumbers_fmt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 else if (cmd == XFS_IOC_FSBULKSTAT_SINGLE)
Christoph Hellwigd716f8e2014-07-24 12:07:15 +1000771 error = xfs_bulkstat_one(mp, inlast, bulkreq.ubuffer,
772 sizeof(xfs_bstat_t), NULL, &done);
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100773 else /* XFS_IOC_FSBULKSTAT */
Christoph Hellwig7dce11d2010-06-23 18:11:11 +1000774 error = xfs_bulkstat(mp, &inlast, &count, xfs_bulkstat_one,
775 sizeof(xfs_bstat_t), bulkreq.ubuffer,
776 &done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
778 if (error)
Dave Chinner24513372014-06-25 14:58:08 +1000779 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
781 if (bulkreq.ocount != NULL) {
782 if (copy_to_user(bulkreq.lastip, &inlast,
783 sizeof(xfs_ino_t)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000784 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
786 if (copy_to_user(bulkreq.ocount, &count, sizeof(count)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000787 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 }
789
790 return 0;
791}
792
793STATIC int
794xfs_ioc_fsgeometry_v1(
795 xfs_mount_t *mp,
796 void __user *arg)
797{
Alex Eldereeb20362011-03-01 17:50:00 +0000798 xfs_fsop_geom_t fsgeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 int error;
800
Darrick J. Wongac503a42018-01-08 10:51:27 -0800801 error = xfs_fs_geometry(&mp->m_sb, &fsgeo, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 if (error)
Dave Chinner24513372014-06-25 14:58:08 +1000803 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Alex Eldereeb20362011-03-01 17:50:00 +0000805 /*
806 * Caller should have passed an argument of type
807 * xfs_fsop_geom_v1_t. This is a proper subset of the
808 * xfs_fsop_geom_t that xfs_fs_geometry() fills in.
809 */
810 if (copy_to_user(arg, &fsgeo, sizeof(xfs_fsop_geom_v1_t)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000811 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 return 0;
813}
814
815STATIC int
816xfs_ioc_fsgeometry(
817 xfs_mount_t *mp,
818 void __user *arg)
819{
820 xfs_fsop_geom_t fsgeo;
821 int error;
822
Darrick J. Wongac503a42018-01-08 10:51:27 -0800823 error = xfs_fs_geometry(&mp->m_sb, &fsgeo, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 if (error)
Dave Chinner24513372014-06-25 14:58:08 +1000825 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
827 if (copy_to_user(arg, &fsgeo, sizeof(fsgeo)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000828 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 return 0;
830}
831
832/*
833 * Linux extended inode flags interface.
834 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
836STATIC unsigned int
837xfs_merge_ioc_xflags(
838 unsigned int flags,
839 unsigned int start)
840{
841 unsigned int xflags = start;
842
Eric Sandeen39058a02007-02-10 18:37:10 +1100843 if (flags & FS_IMMUTABLE_FL)
Dave Chinnere7b89482016-01-04 16:44:15 +1100844 xflags |= FS_XFLAG_IMMUTABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 else
Dave Chinnere7b89482016-01-04 16:44:15 +1100846 xflags &= ~FS_XFLAG_IMMUTABLE;
Eric Sandeen39058a02007-02-10 18:37:10 +1100847 if (flags & FS_APPEND_FL)
Dave Chinnere7b89482016-01-04 16:44:15 +1100848 xflags |= FS_XFLAG_APPEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 else
Dave Chinnere7b89482016-01-04 16:44:15 +1100850 xflags &= ~FS_XFLAG_APPEND;
Eric Sandeen39058a02007-02-10 18:37:10 +1100851 if (flags & FS_SYNC_FL)
Dave Chinnere7b89482016-01-04 16:44:15 +1100852 xflags |= FS_XFLAG_SYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 else
Dave Chinnere7b89482016-01-04 16:44:15 +1100854 xflags &= ~FS_XFLAG_SYNC;
Eric Sandeen39058a02007-02-10 18:37:10 +1100855 if (flags & FS_NOATIME_FL)
Dave Chinnere7b89482016-01-04 16:44:15 +1100856 xflags |= FS_XFLAG_NOATIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 else
Dave Chinnere7b89482016-01-04 16:44:15 +1100858 xflags &= ~FS_XFLAG_NOATIME;
Eric Sandeen39058a02007-02-10 18:37:10 +1100859 if (flags & FS_NODUMP_FL)
Dave Chinnere7b89482016-01-04 16:44:15 +1100860 xflags |= FS_XFLAG_NODUMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 else
Dave Chinnere7b89482016-01-04 16:44:15 +1100862 xflags &= ~FS_XFLAG_NODUMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
864 return xflags;
865}
866
867STATIC unsigned int
868xfs_di2lxflags(
Darrick J. Wongc8ce5402017-06-16 11:00:05 -0700869 uint16_t di_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870{
871 unsigned int flags = 0;
872
873 if (di_flags & XFS_DIFLAG_IMMUTABLE)
Eric Sandeen39058a02007-02-10 18:37:10 +1100874 flags |= FS_IMMUTABLE_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 if (di_flags & XFS_DIFLAG_APPEND)
Eric Sandeen39058a02007-02-10 18:37:10 +1100876 flags |= FS_APPEND_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 if (di_flags & XFS_DIFLAG_SYNC)
Eric Sandeen39058a02007-02-10 18:37:10 +1100878 flags |= FS_SYNC_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 if (di_flags & XFS_DIFLAG_NOATIME)
Eric Sandeen39058a02007-02-10 18:37:10 +1100880 flags |= FS_NOATIME_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 if (di_flags & XFS_DIFLAG_NODUMP)
Eric Sandeen39058a02007-02-10 18:37:10 +1100882 flags |= FS_NODUMP_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 return flags;
884}
885
886STATIC int
Christoph Hellwigc83bfab2007-10-11 17:47:00 +1000887xfs_ioc_fsgetxattr(
888 xfs_inode_t *ip,
889 int attr,
890 void __user *arg)
891{
892 struct fsxattr fa;
893
Dan Rosenberga122eb22010-09-06 18:24:57 -0400894 memset(&fa, 0, sizeof(struct fsxattr));
895
Christoph Hellwigc83bfab2007-10-11 17:47:00 +1000896 xfs_ilock(ip, XFS_ILOCK_SHARED);
897 fa.fsx_xflags = xfs_ip2xflags(ip);
898 fa.fsx_extsize = ip->i_d.di_extsize << ip->i_mount->m_sb.sb_blocklog;
Darrick J. Wongf7ca3522016-10-03 09:11:43 -0700899 fa.fsx_cowextsize = ip->i_d.di_cowextsize <<
900 ip->i_mount->m_sb.sb_blocklog;
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +0000901 fa.fsx_projid = xfs_get_projid(ip);
Christoph Hellwigc83bfab2007-10-11 17:47:00 +1000902
903 if (attr) {
904 if (ip->i_afp) {
905 if (ip->i_afp->if_flags & XFS_IFEXTENTS)
Eric Sandeen5d829302016-11-08 12:59:42 +1100906 fa.fsx_nextents = xfs_iext_count(ip->i_afp);
Christoph Hellwigc83bfab2007-10-11 17:47:00 +1000907 else
908 fa.fsx_nextents = ip->i_d.di_anextents;
909 } else
910 fa.fsx_nextents = 0;
911 } else {
912 if (ip->i_df.if_flags & XFS_IFEXTENTS)
Eric Sandeen5d829302016-11-08 12:59:42 +1100913 fa.fsx_nextents = xfs_iext_count(&ip->i_df);
Christoph Hellwigc83bfab2007-10-11 17:47:00 +1000914 else
915 fa.fsx_nextents = ip->i_d.di_nextents;
916 }
917 xfs_iunlock(ip, XFS_ILOCK_SHARED);
918
919 if (copy_to_user(arg, &fa, sizeof(fa)))
920 return -EFAULT;
921 return 0;
922}
923
Christoph Hellwigdd606872017-09-02 08:21:20 -0700924STATIC uint16_t
925xfs_flags2diflags(
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000926 struct xfs_inode *ip,
927 unsigned int xflags)
928{
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000929 /* can't set PREALLOC this way, just preserve it */
Christoph Hellwigdd606872017-09-02 08:21:20 -0700930 uint16_t di_flags =
931 (ip->i_d.di_flags & XFS_DIFLAG_PREALLOC);
932
Dave Chinnere7b89482016-01-04 16:44:15 +1100933 if (xflags & FS_XFLAG_IMMUTABLE)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000934 di_flags |= XFS_DIFLAG_IMMUTABLE;
Dave Chinnere7b89482016-01-04 16:44:15 +1100935 if (xflags & FS_XFLAG_APPEND)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000936 di_flags |= XFS_DIFLAG_APPEND;
Dave Chinnere7b89482016-01-04 16:44:15 +1100937 if (xflags & FS_XFLAG_SYNC)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000938 di_flags |= XFS_DIFLAG_SYNC;
Dave Chinnere7b89482016-01-04 16:44:15 +1100939 if (xflags & FS_XFLAG_NOATIME)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000940 di_flags |= XFS_DIFLAG_NOATIME;
Dave Chinnere7b89482016-01-04 16:44:15 +1100941 if (xflags & FS_XFLAG_NODUMP)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000942 di_flags |= XFS_DIFLAG_NODUMP;
Dave Chinnere7b89482016-01-04 16:44:15 +1100943 if (xflags & FS_XFLAG_NODEFRAG)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000944 di_flags |= XFS_DIFLAG_NODEFRAG;
Dave Chinnere7b89482016-01-04 16:44:15 +1100945 if (xflags & FS_XFLAG_FILESTREAM)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000946 di_flags |= XFS_DIFLAG_FILESTREAM;
Dave Chinnerc19b3b052016-02-09 16:54:58 +1100947 if (S_ISDIR(VFS_I(ip)->i_mode)) {
Dave Chinnere7b89482016-01-04 16:44:15 +1100948 if (xflags & FS_XFLAG_RTINHERIT)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000949 di_flags |= XFS_DIFLAG_RTINHERIT;
Dave Chinnere7b89482016-01-04 16:44:15 +1100950 if (xflags & FS_XFLAG_NOSYMLINKS)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000951 di_flags |= XFS_DIFLAG_NOSYMLINKS;
Dave Chinnere7b89482016-01-04 16:44:15 +1100952 if (xflags & FS_XFLAG_EXTSZINHERIT)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000953 di_flags |= XFS_DIFLAG_EXTSZINHERIT;
Dave Chinnere7b89482016-01-04 16:44:15 +1100954 if (xflags & FS_XFLAG_PROJINHERIT)
Dave Chinner9336e3a2014-10-02 09:18:40 +1000955 di_flags |= XFS_DIFLAG_PROJINHERIT;
Dave Chinnerc19b3b052016-02-09 16:54:58 +1100956 } else if (S_ISREG(VFS_I(ip)->i_mode)) {
Dave Chinnere7b89482016-01-04 16:44:15 +1100957 if (xflags & FS_XFLAG_REALTIME)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000958 di_flags |= XFS_DIFLAG_REALTIME;
Dave Chinnere7b89482016-01-04 16:44:15 +1100959 if (xflags & FS_XFLAG_EXTSIZE)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000960 di_flags |= XFS_DIFLAG_EXTSIZE;
961 }
Dave Chinner58f88ca2016-01-04 16:44:15 +1100962
Christoph Hellwigdd606872017-09-02 08:21:20 -0700963 return di_flags;
964}
Dave Chinner58f88ca2016-01-04 16:44:15 +1100965
Christoph Hellwigdd606872017-09-02 08:21:20 -0700966STATIC uint64_t
967xfs_flags2diflags2(
968 struct xfs_inode *ip,
969 unsigned int xflags)
970{
971 uint64_t di_flags2 =
972 (ip->i_d.di_flags2 & XFS_DIFLAG2_REFLINK);
973
Dave Chinner58f88ca2016-01-04 16:44:15 +1100974 if (xflags & FS_XFLAG_DAX)
975 di_flags2 |= XFS_DIFLAG2_DAX;
Darrick J. Wongf7ca3522016-10-03 09:11:43 -0700976 if (xflags & FS_XFLAG_COWEXTSIZE)
977 di_flags2 |= XFS_DIFLAG2_COWEXTSIZE;
Dave Chinner58f88ca2016-01-04 16:44:15 +1100978
Christoph Hellwigdd606872017-09-02 08:21:20 -0700979 return di_flags2;
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000980}
981
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000982STATIC void
983xfs_diflags_to_linux(
984 struct xfs_inode *ip)
985{
David Chinnere4f75292008-08-13 16:00:45 +1000986 struct inode *inode = VFS_I(ip);
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000987 unsigned int xflags = xfs_ip2xflags(ip);
988
Dave Chinnere7b89482016-01-04 16:44:15 +1100989 if (xflags & FS_XFLAG_IMMUTABLE)
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000990 inode->i_flags |= S_IMMUTABLE;
991 else
992 inode->i_flags &= ~S_IMMUTABLE;
Dave Chinnere7b89482016-01-04 16:44:15 +1100993 if (xflags & FS_XFLAG_APPEND)
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000994 inode->i_flags |= S_APPEND;
995 else
996 inode->i_flags &= ~S_APPEND;
Dave Chinnere7b89482016-01-04 16:44:15 +1100997 if (xflags & FS_XFLAG_SYNC)
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000998 inode->i_flags |= S_SYNC;
999 else
1000 inode->i_flags &= ~S_SYNC;
Dave Chinnere7b89482016-01-04 16:44:15 +11001001 if (xflags & FS_XFLAG_NOATIME)
Christoph Hellwigf13fae22008-07-21 16:16:15 +10001002 inode->i_flags |= S_NOATIME;
1003 else
1004 inode->i_flags &= ~S_NOATIME;
Christoph Hellwig742d8422017-08-30 09:23:01 -07001005#if 0 /* disabled until the flag switching races are sorted out */
Dave Chinner58f88ca2016-01-04 16:44:15 +11001006 if (xflags & FS_XFLAG_DAX)
1007 inode->i_flags |= S_DAX;
1008 else
1009 inode->i_flags &= ~S_DAX;
Christoph Hellwig742d8422017-08-30 09:23:01 -07001010#endif
Christoph Hellwigf13fae22008-07-21 16:16:15 +10001011}
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001012
Dave Chinner29a17c02015-02-02 10:14:25 +11001013static int
1014xfs_ioctl_setattr_xflags(
1015 struct xfs_trans *tp,
1016 struct xfs_inode *ip,
1017 struct fsxattr *fa)
1018{
1019 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwigdd606872017-09-02 08:21:20 -07001020 uint64_t di_flags2;
Dave Chinner29a17c02015-02-02 10:14:25 +11001021
1022 /* Can't change realtime flag if any extents are allocated. */
1023 if ((ip->i_d.di_nextents || ip->i_delayed_blks) &&
Dave Chinnere7b89482016-01-04 16:44:15 +11001024 XFS_IS_REALTIME_INODE(ip) != (fa->fsx_xflags & FS_XFLAG_REALTIME))
Dave Chinner29a17c02015-02-02 10:14:25 +11001025 return -EINVAL;
1026
1027 /* If realtime flag is set then must have realtime device */
Dave Chinnere7b89482016-01-04 16:44:15 +11001028 if (fa->fsx_xflags & FS_XFLAG_REALTIME) {
Dave Chinner29a17c02015-02-02 10:14:25 +11001029 if (mp->m_sb.sb_rblocks == 0 || mp->m_sb.sb_rextsize == 0 ||
1030 (ip->i_d.di_extsize % mp->m_sb.sb_rextsize))
1031 return -EINVAL;
1032 }
1033
Darrick J. Wong1987fd72016-10-10 16:49:29 +11001034 /* Clear reflink if we are actually able to set the rt flag. */
Darrick J. Wongc8e156a2016-10-03 09:11:50 -07001035 if ((fa->fsx_xflags & FS_XFLAG_REALTIME) && xfs_is_reflink_inode(ip))
Darrick J. Wong1987fd72016-10-10 16:49:29 +11001036 ip->i_d.di_flags2 &= ~XFS_DIFLAG2_REFLINK;
Darrick J. Wongc8e156a2016-10-03 09:11:50 -07001037
Darrick J. Wong4f435eb2016-10-03 09:11:50 -07001038 /* Don't allow us to set DAX mode for a reflinked file for now. */
1039 if ((fa->fsx_xflags & FS_XFLAG_DAX) && xfs_is_reflink_inode(ip))
1040 return -EINVAL;
1041
Dave Chinner29a17c02015-02-02 10:14:25 +11001042 /*
1043 * Can't modify an immutable/append-only file unless
1044 * we have appropriate permission.
1045 */
1046 if (((ip->i_d.di_flags & (XFS_DIFLAG_IMMUTABLE | XFS_DIFLAG_APPEND)) ||
Dave Chinnere7b89482016-01-04 16:44:15 +11001047 (fa->fsx_xflags & (FS_XFLAG_IMMUTABLE | FS_XFLAG_APPEND))) &&
Dave Chinner29a17c02015-02-02 10:14:25 +11001048 !capable(CAP_LINUX_IMMUTABLE))
1049 return -EPERM;
1050
Christoph Hellwigdd606872017-09-02 08:21:20 -07001051 /* diflags2 only valid for v3 inodes. */
1052 di_flags2 = xfs_flags2diflags2(ip, fa->fsx_xflags);
1053 if (di_flags2 && ip->i_d.di_version < 3)
1054 return -EINVAL;
1055
1056 ip->i_d.di_flags = xfs_flags2diflags(ip, fa->fsx_xflags);
1057 ip->i_d.di_flags2 = di_flags2;
1058
Dave Chinner29a17c02015-02-02 10:14:25 +11001059 xfs_diflags_to_linux(ip);
1060 xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
1061 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11001062 XFS_STATS_INC(mp, xs_ig_attrchg);
Dave Chinner29a17c02015-02-02 10:14:25 +11001063 return 0;
1064}
1065
Dave Chinner8f3d17a2015-02-02 10:15:35 +11001066/*
Dave Chinner3a6a8542016-03-01 09:41:33 +11001067 * If we are changing DAX flags, we have to ensure the file is clean and any
1068 * cached objects in the address space are invalidated and removed. This
1069 * requires us to lock out other IO and page faults similar to a truncate
1070 * operation. The locks need to be held until the transaction has been committed
1071 * so that the cache invalidation is atomic with respect to the DAX flag
1072 * manipulation.
1073 */
1074static int
1075xfs_ioctl_setattr_dax_invalidate(
1076 struct xfs_inode *ip,
1077 struct fsxattr *fa,
1078 int *join_flags)
1079{
1080 struct inode *inode = VFS_I(ip);
Ross Zwisler6851a3d2017-09-18 14:46:03 -07001081 struct super_block *sb = inode->i_sb;
Dave Chinner3a6a8542016-03-01 09:41:33 +11001082 int error;
1083
1084 *join_flags = 0;
1085
1086 /*
1087 * It is only valid to set the DAX flag on regular files and
Dave Chinner64485432016-03-01 09:41:33 +11001088 * directories on filesystems where the block size is equal to the page
Darrick J. Wongaaacdd22018-05-31 15:07:47 -07001089 * size. On directories it serves as an inherited hint so we don't
1090 * have to check the device for dax support or flush pagecache.
Dave Chinner3a6a8542016-03-01 09:41:33 +11001091 */
Dave Chinner64485432016-03-01 09:41:33 +11001092 if (fa->fsx_xflags & FS_XFLAG_DAX) {
1093 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)))
1094 return -EINVAL;
Darrick J. Wongaaacdd22018-05-31 15:07:47 -07001095 if (S_ISREG(inode->i_mode) &&
1096 !bdev_dax_supported(xfs_find_bdev_for_inode(VFS_I(ip)),
Dave Jiang80660f22018-05-30 13:03:46 -07001097 sb->s_blocksize))
Dave Chinner64485432016-03-01 09:41:33 +11001098 return -EINVAL;
1099 }
Dave Chinner3a6a8542016-03-01 09:41:33 +11001100
1101 /* If the DAX state is not changing, we have nothing to do here. */
1102 if ((fa->fsx_xflags & FS_XFLAG_DAX) && IS_DAX(inode))
1103 return 0;
1104 if (!(fa->fsx_xflags & FS_XFLAG_DAX) && !IS_DAX(inode))
1105 return 0;
1106
Darrick J. Wongaaacdd22018-05-31 15:07:47 -07001107 if (S_ISDIR(inode->i_mode))
1108 return 0;
1109
Dave Chinner3a6a8542016-03-01 09:41:33 +11001110 /* lock, flush and invalidate mapping in preparation for flag change */
1111 xfs_ilock(ip, XFS_MMAPLOCK_EXCL | XFS_IOLOCK_EXCL);
1112 error = filemap_write_and_wait(inode->i_mapping);
1113 if (error)
1114 goto out_unlock;
1115 error = invalidate_inode_pages2(inode->i_mapping);
1116 if (error)
1117 goto out_unlock;
1118
1119 *join_flags = XFS_MMAPLOCK_EXCL | XFS_IOLOCK_EXCL;
1120 return 0;
1121
1122out_unlock:
1123 xfs_iunlock(ip, XFS_MMAPLOCK_EXCL | XFS_IOLOCK_EXCL);
1124 return error;
1125
1126}
1127
1128/*
Dave Chinner8f3d17a2015-02-02 10:15:35 +11001129 * Set up the transaction structure for the setattr operation, checking that we
1130 * have permission to do so. On success, return a clean transaction and the
1131 * inode locked exclusively ready for further operation specific checks. On
1132 * failure, return an error without modifying or locking the inode.
Dave Chinner3a6a8542016-03-01 09:41:33 +11001133 *
1134 * The inode might already be IO locked on call. If this is the case, it is
1135 * indicated in @join_flags and we take full responsibility for ensuring they
1136 * are unlocked from now on. Hence if we have an error here, we still have to
1137 * unlock them. Otherwise, once they are joined to the transaction, they will
1138 * be unlocked on commit/cancel.
Dave Chinner8f3d17a2015-02-02 10:15:35 +11001139 */
1140static struct xfs_trans *
1141xfs_ioctl_setattr_get_trans(
Dave Chinner3a6a8542016-03-01 09:41:33 +11001142 struct xfs_inode *ip,
1143 int join_flags)
Dave Chinner8f3d17a2015-02-02 10:15:35 +11001144{
1145 struct xfs_mount *mp = ip->i_mount;
1146 struct xfs_trans *tp;
Dave Chinner3a6a8542016-03-01 09:41:33 +11001147 int error = -EROFS;
Dave Chinner8f3d17a2015-02-02 10:15:35 +11001148
1149 if (mp->m_flags & XFS_MOUNT_RDONLY)
Dave Chinner3a6a8542016-03-01 09:41:33 +11001150 goto out_unlock;
1151 error = -EIO;
Dave Chinner8f3d17a2015-02-02 10:15:35 +11001152 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner3a6a8542016-03-01 09:41:33 +11001153 goto out_unlock;
Dave Chinner8f3d17a2015-02-02 10:15:35 +11001154
Christoph Hellwig253f4912016-04-06 09:19:55 +10001155 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
Dave Chinner8f3d17a2015-02-02 10:15:35 +11001156 if (error)
Christoph Hellwig253f4912016-04-06 09:19:55 +10001157 return ERR_PTR(error);
Dave Chinner8f3d17a2015-02-02 10:15:35 +11001158
1159 xfs_ilock(ip, XFS_ILOCK_EXCL);
Dave Chinner3a6a8542016-03-01 09:41:33 +11001160 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | join_flags);
1161 join_flags = 0;
Dave Chinner8f3d17a2015-02-02 10:15:35 +11001162
1163 /*
1164 * CAP_FOWNER overrides the following restrictions:
1165 *
1166 * The user ID of the calling process must be equal to the file owner
1167 * ID, except in cases where the CAP_FSETID capability is applicable.
1168 */
1169 if (!inode_owner_or_capable(VFS_I(ip))) {
1170 error = -EPERM;
1171 goto out_cancel;
1172 }
1173
1174 if (mp->m_flags & XFS_MOUNT_WSYNC)
1175 xfs_trans_set_sync(tp);
1176
1177 return tp;
1178
1179out_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +10001180 xfs_trans_cancel(tp);
Dave Chinner3a6a8542016-03-01 09:41:33 +11001181out_unlock:
1182 if (join_flags)
1183 xfs_iunlock(ip, join_flags);
Dave Chinner8f3d17a2015-02-02 10:15:35 +11001184 return ERR_PTR(error);
1185}
1186
Iustin Pop9b94fcc2015-02-02 10:26:26 +11001187/*
1188 * extent size hint validation is somewhat cumbersome. Rules are:
1189 *
1190 * 1. extent size hint is only valid for directories and regular files
Dave Chinnere7b89482016-01-04 16:44:15 +11001191 * 2. FS_XFLAG_EXTSIZE is only valid for regular files
1192 * 3. FS_XFLAG_EXTSZINHERIT is only valid for directories.
Iustin Pop9b94fcc2015-02-02 10:26:26 +11001193 * 4. can only be changed on regular files if no extents are allocated
1194 * 5. can be changed on directories at any time
1195 * 6. extsize hint of 0 turns off hints, clears inode flags.
1196 * 7. Extent size must be a multiple of the appropriate block size.
1197 * 8. for non-realtime files, the extent size hint must be limited
1198 * to half the AG size to avoid alignment extending the extent beyond the
1199 * limits of the AG.
Darrick J. Wong80e4e122017-10-17 21:37:42 -07001200 *
1201 * Please keep this function in sync with xfs_scrub_inode_extsize.
Iustin Pop9b94fcc2015-02-02 10:26:26 +11001202 */
kbuild test robotf92090e2015-02-05 11:13:21 +11001203static int
Dave Chinnerd4388d3c2015-02-02 10:22:20 +11001204xfs_ioctl_setattr_check_extsize(
1205 struct xfs_inode *ip,
1206 struct fsxattr *fa)
1207{
1208 struct xfs_mount *mp = ip->i_mount;
1209
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001210 if ((fa->fsx_xflags & FS_XFLAG_EXTSIZE) && !S_ISREG(VFS_I(ip)->i_mode))
Iustin Pop9b94fcc2015-02-02 10:26:26 +11001211 return -EINVAL;
1212
Dave Chinnere7b89482016-01-04 16:44:15 +11001213 if ((fa->fsx_xflags & FS_XFLAG_EXTSZINHERIT) &&
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001214 !S_ISDIR(VFS_I(ip)->i_mode))
Iustin Pop9b94fcc2015-02-02 10:26:26 +11001215 return -EINVAL;
1216
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001217 if (S_ISREG(VFS_I(ip)->i_mode) && ip->i_d.di_nextents &&
Dave Chinnerd4388d3c2015-02-02 10:22:20 +11001218 ((ip->i_d.di_extsize << mp->m_sb.sb_blocklog) != fa->fsx_extsize))
1219 return -EINVAL;
1220
Dave Chinnerd4388d3c2015-02-02 10:22:20 +11001221 if (fa->fsx_extsize != 0) {
1222 xfs_extlen_t size;
1223 xfs_fsblock_t extsize_fsb;
1224
1225 extsize_fsb = XFS_B_TO_FSB(mp, fa->fsx_extsize);
1226 if (extsize_fsb > MAXEXTLEN)
1227 return -EINVAL;
1228
1229 if (XFS_IS_REALTIME_INODE(ip) ||
Dave Chinnere7b89482016-01-04 16:44:15 +11001230 (fa->fsx_xflags & FS_XFLAG_REALTIME)) {
Dave Chinnerd4388d3c2015-02-02 10:22:20 +11001231 size = mp->m_sb.sb_rextsize << mp->m_sb.sb_blocklog;
1232 } else {
1233 size = mp->m_sb.sb_blocksize;
1234 if (extsize_fsb > mp->m_sb.sb_agblocks / 2)
1235 return -EINVAL;
1236 }
1237
1238 if (fa->fsx_extsize % size)
1239 return -EINVAL;
Iustin Pop9b94fcc2015-02-02 10:26:26 +11001240 } else
Dave Chinnere7b89482016-01-04 16:44:15 +11001241 fa->fsx_xflags &= ~(FS_XFLAG_EXTSIZE | FS_XFLAG_EXTSZINHERIT);
Iustin Pop9b94fcc2015-02-02 10:26:26 +11001242
Dave Chinnerd4388d3c2015-02-02 10:22:20 +11001243 return 0;
1244}
1245
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001246/*
1247 * CoW extent size hint validation rules are:
1248 *
1249 * 1. CoW extent size hint can only be set if reflink is enabled on the fs.
1250 * The inode does not have to have any shared blocks, but it must be a v3.
1251 * 2. FS_XFLAG_COWEXTSIZE is only valid for directories and regular files;
1252 * for a directory, the hint is propagated to new files.
1253 * 3. Can be changed on files & directories at any time.
1254 * 4. CoW extsize hint of 0 turns off hints, clears inode flags.
1255 * 5. Extent size must be a multiple of the appropriate block size.
1256 * 6. The extent size hint must be limited to half the AG size to avoid
1257 * alignment extending the extent beyond the limits of the AG.
Darrick J. Wong80e4e122017-10-17 21:37:42 -07001258 *
1259 * Please keep this function in sync with xfs_scrub_inode_cowextsize.
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001260 */
1261static int
1262xfs_ioctl_setattr_check_cowextsize(
1263 struct xfs_inode *ip,
1264 struct fsxattr *fa)
1265{
1266 struct xfs_mount *mp = ip->i_mount;
1267
1268 if (!(fa->fsx_xflags & FS_XFLAG_COWEXTSIZE))
1269 return 0;
1270
1271 if (!xfs_sb_version_hasreflink(&ip->i_mount->m_sb) ||
1272 ip->i_d.di_version != 3)
1273 return -EINVAL;
1274
1275 if (!S_ISREG(VFS_I(ip)->i_mode) && !S_ISDIR(VFS_I(ip)->i_mode))
1276 return -EINVAL;
1277
1278 if (fa->fsx_cowextsize != 0) {
1279 xfs_extlen_t size;
1280 xfs_fsblock_t cowextsize_fsb;
1281
1282 cowextsize_fsb = XFS_B_TO_FSB(mp, fa->fsx_cowextsize);
1283 if (cowextsize_fsb > MAXEXTLEN)
1284 return -EINVAL;
1285
1286 size = mp->m_sb.sb_blocksize;
1287 if (cowextsize_fsb > mp->m_sb.sb_agblocks / 2)
1288 return -EINVAL;
1289
1290 if (fa->fsx_cowextsize % size)
1291 return -EINVAL;
1292 } else
1293 fa->fsx_xflags &= ~FS_XFLAG_COWEXTSIZE;
1294
1295 return 0;
1296}
1297
kbuild test robotf92090e2015-02-05 11:13:21 +11001298static int
Dave Chinner23bd0732015-02-02 10:22:53 +11001299xfs_ioctl_setattr_check_projid(
1300 struct xfs_inode *ip,
1301 struct fsxattr *fa)
1302{
1303 /* Disallow 32bit project ids if projid32bit feature is not enabled. */
Darrick J. Wongc8ce5402017-06-16 11:00:05 -07001304 if (fa->fsx_projid > (uint16_t)-1 &&
Dave Chinner23bd0732015-02-02 10:22:53 +11001305 !xfs_sb_version_hasprojid32bit(&ip->i_mount->m_sb))
1306 return -EINVAL;
1307
1308 /*
1309 * Project Quota ID state is only allowed to change from within the init
1310 * namespace. Enforce that restriction only if we are trying to change
1311 * the quota ID state. Everything else is allowed in user namespaces.
1312 */
1313 if (current_user_ns() == &init_user_ns)
1314 return 0;
1315
1316 if (xfs_get_projid(ip) != fa->fsx_projid)
1317 return -EINVAL;
Dave Chinnere7b89482016-01-04 16:44:15 +11001318 if ((fa->fsx_xflags & FS_XFLAG_PROJINHERIT) !=
Dave Chinner23bd0732015-02-02 10:22:53 +11001319 (ip->i_d.di_flags & XFS_DIFLAG_PROJINHERIT))
1320 return -EINVAL;
1321
1322 return 0;
1323}
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001324
1325STATIC int
1326xfs_ioctl_setattr(
1327 xfs_inode_t *ip,
Dave Chinnerfd179b92015-02-02 10:16:25 +11001328 struct fsxattr *fa)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001329{
1330 struct xfs_mount *mp = ip->i_mount;
1331 struct xfs_trans *tp;
Christoph Hellwig7d095252009-06-08 15:33:32 +02001332 struct xfs_dquot *udqp = NULL;
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001333 struct xfs_dquot *pdqp = NULL;
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001334 struct xfs_dquot *olddquot = NULL;
1335 int code;
Dave Chinner3a6a8542016-03-01 09:41:33 +11001336 int join_flags = 0;
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001337
Christoph Hellwigcca28fb2010-06-24 11:57:09 +10001338 trace_xfs_ioctl_setattr(ip);
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001339
Dave Chinner23bd0732015-02-02 10:22:53 +11001340 code = xfs_ioctl_setattr_check_projid(ip, fa);
1341 if (code)
1342 return code;
Arkadiusz Mi?kiewicz23963e542010-08-26 10:19:43 +00001343
1344 /*
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001345 * If disk quotas is on, we make sure that the dquots do exist on disk,
1346 * before we start any other transactions. Trying to do this later
1347 * is messy. We don't care to take a readlock to look at the ids
1348 * in inode here, because we can't hold it across the trans_reserve.
1349 * If the IDs do change before we take the ilock, we're covered
1350 * because the i_*dquot fields will get updated anyway.
1351 */
Dave Chinnerfd179b92015-02-02 10:16:25 +11001352 if (XFS_IS_QUOTA_ON(mp)) {
Christoph Hellwig7d095252009-06-08 15:33:32 +02001353 code = xfs_qm_vop_dqalloc(ip, ip->i_d.di_uid,
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001354 ip->i_d.di_gid, fa->fsx_projid,
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001355 XFS_QMOPT_PQUOTA, &udqp, NULL, &pdqp);
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001356 if (code)
1357 return code;
1358 }
1359
Dave Chinner3a6a8542016-03-01 09:41:33 +11001360 /*
1361 * Changing DAX config may require inode locking for mapping
1362 * invalidation. These need to be held all the way to transaction commit
1363 * or cancel time, so need to be passed through to
1364 * xfs_ioctl_setattr_get_trans() so it can apply them to the join call
1365 * appropriately.
1366 */
1367 code = xfs_ioctl_setattr_dax_invalidate(ip, fa, &join_flags);
1368 if (code)
1369 goto error_free_dquots;
1370
1371 tp = xfs_ioctl_setattr_get_trans(ip, join_flags);
Dave Chinner8f3d17a2015-02-02 10:15:35 +11001372 if (IS_ERR(tp)) {
1373 code = PTR_ERR(tp);
1374 goto error_free_dquots;
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001375 }
1376
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001377
Dave Chinnerfd179b92015-02-02 10:16:25 +11001378 if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_PQUOTA_ON(mp) &&
1379 xfs_get_projid(ip) != fa->fsx_projid) {
1380 code = xfs_qm_vop_chown_reserve(tp, ip, udqp, NULL, pdqp,
1381 capable(CAP_FOWNER) ? XFS_QMOPT_FORCE_RES : 0);
1382 if (code) /* out of quota */
Dave Chinnerd4388d3c2015-02-02 10:22:20 +11001383 goto error_trans_cancel;
Dave Chinnerfd179b92015-02-02 10:16:25 +11001384 }
1385
Dave Chinnerd4388d3c2015-02-02 10:22:20 +11001386 code = xfs_ioctl_setattr_check_extsize(ip, fa);
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001387 if (code)
Dave Chinnerd4388d3c2015-02-02 10:22:20 +11001388 goto error_trans_cancel;
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001389
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001390 code = xfs_ioctl_setattr_check_cowextsize(ip, fa);
1391 if (code)
1392 goto error_trans_cancel;
1393
Dave Chinner29a17c02015-02-02 10:14:25 +11001394 code = xfs_ioctl_setattr_xflags(tp, ip, fa);
1395 if (code)
Dave Chinnerd4388d3c2015-02-02 10:22:20 +11001396 goto error_trans_cancel;
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001397
1398 /*
Dave Chinnerfd179b92015-02-02 10:16:25 +11001399 * Change file ownership. Must be the owner or privileged. CAP_FSETID
1400 * overrides the following restrictions:
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001401 *
Dave Chinnerfd179b92015-02-02 10:16:25 +11001402 * The set-user-ID and set-group-ID bits of a file will be cleared upon
1403 * successful return from chown()
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001404 */
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001405
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001406 if ((VFS_I(ip)->i_mode & (S_ISUID|S_ISGID)) &&
Dave Chinnerfd179b92015-02-02 10:16:25 +11001407 !capable_wrt_inode_uidgid(VFS_I(ip), CAP_FSETID))
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001408 VFS_I(ip)->i_mode &= ~(S_ISUID|S_ISGID);
Dave Chinnerfd179b92015-02-02 10:16:25 +11001409
1410 /* Change the ownerships and register project quota modifications */
1411 if (xfs_get_projid(ip) != fa->fsx_projid) {
1412 if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_PQUOTA_ON(mp)) {
1413 olddquot = xfs_qm_vop_chown(tp, ip,
1414 &ip->i_pdquot, pdqp);
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001415 }
Dave Chinnerfd179b92015-02-02 10:16:25 +11001416 ASSERT(ip->i_d.di_version > 1);
1417 xfs_set_projid(ip, fa->fsx_projid);
Christoph Hellwigf13fae22008-07-21 16:16:15 +10001418 }
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001419
Dave Chinnera8727032014-10-02 09:20:30 +10001420 /*
1421 * Only set the extent size hint if we've already determined that the
1422 * extent size hint should be set on the inode. If no extent size flags
1423 * are set on the inode then unconditionally clear the extent size hint.
1424 */
Dave Chinnerfd179b92015-02-02 10:16:25 +11001425 if (ip->i_d.di_flags & (XFS_DIFLAG_EXTSIZE | XFS_DIFLAG_EXTSZINHERIT))
1426 ip->i_d.di_extsize = fa->fsx_extsize >> mp->m_sb.sb_blocklog;
1427 else
1428 ip->i_d.di_extsize = 0;
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001429 if (ip->i_d.di_version == 3 &&
1430 (ip->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE))
1431 ip->i_d.di_cowextsize = fa->fsx_cowextsize >>
1432 mp->m_sb.sb_blocklog;
1433 else
1434 ip->i_d.di_cowextsize = 0;
Dave Chinnera8727032014-10-02 09:20:30 +10001435
Christoph Hellwig70393312015-06-04 13:48:08 +10001436 code = xfs_trans_commit(tp);
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001437
1438 /*
1439 * Release any dquot(s) the inode had kept before chown.
1440 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02001441 xfs_qm_dqrele(olddquot);
1442 xfs_qm_dqrele(udqp);
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001443 xfs_qm_dqrele(pdqp);
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001444
Christoph Hellwig288699f2010-06-23 18:11:15 +10001445 return code;
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001446
Dave Chinnerd4388d3c2015-02-02 10:22:20 +11001447error_trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +10001448 xfs_trans_cancel(tp);
Dave Chinner8f3d17a2015-02-02 10:15:35 +11001449error_free_dquots:
Christoph Hellwig7d095252009-06-08 15:33:32 +02001450 xfs_qm_dqrele(udqp);
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001451 xfs_qm_dqrele(pdqp);
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001452 return code;
1453}
1454
Christoph Hellwigc83bfab2007-10-11 17:47:00 +10001455STATIC int
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001456xfs_ioc_fssetxattr(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 xfs_inode_t *ip,
1458 struct file *filp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 void __user *arg)
1460{
1461 struct fsxattr fa;
Jan Karad9457dc2012-06-12 16:20:39 +02001462 int error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001463
1464 if (copy_from_user(&fa, arg, sizeof(fa)))
1465 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
Jan Karad9457dc2012-06-12 16:20:39 +02001467 error = mnt_want_write_file(filp);
1468 if (error)
1469 return error;
Dave Chinnerfd179b92015-02-02 10:16:25 +11001470 error = xfs_ioctl_setattr(ip, &fa);
Jan Karad9457dc2012-06-12 16:20:39 +02001471 mnt_drop_write_file(filp);
Dave Chinner24513372014-06-25 14:58:08 +10001472 return error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001473}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001475STATIC int
1476xfs_ioc_getxflags(
1477 xfs_inode_t *ip,
1478 void __user *arg)
1479{
1480 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001482 flags = xfs_di2lxflags(ip->i_d.di_flags);
1483 if (copy_to_user(arg, &flags, sizeof(flags)))
1484 return -EFAULT;
1485 return 0;
1486}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001488STATIC int
1489xfs_ioc_setxflags(
Dave Chinnerf96291f2015-02-02 10:15:56 +11001490 struct xfs_inode *ip,
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001491 struct file *filp,
1492 void __user *arg)
1493{
Dave Chinnerf96291f2015-02-02 10:15:56 +11001494 struct xfs_trans *tp;
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001495 struct fsxattr fa;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001496 unsigned int flags;
Dave Chinner3a6a8542016-03-01 09:41:33 +11001497 int join_flags = 0;
Dave Chinnerf96291f2015-02-02 10:15:56 +11001498 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001500 if (copy_from_user(&flags, arg, sizeof(flags)))
1501 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001503 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
1504 FS_NOATIME_FL | FS_NODUMP_FL | \
1505 FS_SYNC_FL))
1506 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001508 fa.fsx_xflags = xfs_merge_ioc_xflags(flags, xfs_ip2xflags(ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
Jan Karad9457dc2012-06-12 16:20:39 +02001510 error = mnt_want_write_file(filp);
1511 if (error)
1512 return error;
Dave Chinnerf96291f2015-02-02 10:15:56 +11001513
Dave Chinner3a6a8542016-03-01 09:41:33 +11001514 /*
1515 * Changing DAX config may require inode locking for mapping
1516 * invalidation. These need to be held all the way to transaction commit
1517 * or cancel time, so need to be passed through to
1518 * xfs_ioctl_setattr_get_trans() so it can apply them to the join call
1519 * appropriately.
1520 */
1521 error = xfs_ioctl_setattr_dax_invalidate(ip, &fa, &join_flags);
1522 if (error)
1523 goto out_drop_write;
1524
1525 tp = xfs_ioctl_setattr_get_trans(ip, join_flags);
Dave Chinnerf96291f2015-02-02 10:15:56 +11001526 if (IS_ERR(tp)) {
1527 error = PTR_ERR(tp);
1528 goto out_drop_write;
1529 }
1530
1531 error = xfs_ioctl_setattr_xflags(tp, ip, &fa);
1532 if (error) {
Christoph Hellwig4906e212015-06-04 13:47:56 +10001533 xfs_trans_cancel(tp);
Dave Chinnerf96291f2015-02-02 10:15:56 +11001534 goto out_drop_write;
1535 }
1536
Christoph Hellwig70393312015-06-04 13:48:08 +10001537 error = xfs_trans_commit(tp);
Dave Chinnerf96291f2015-02-02 10:15:56 +11001538out_drop_write:
Jan Karad9457dc2012-06-12 16:20:39 +02001539 mnt_drop_write_file(filp);
Dave Chinner24513372014-06-25 14:58:08 +10001540 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541}
1542
Christoph Hellwig232b51942017-10-17 14:16:19 -07001543static bool
1544xfs_getbmap_format(
1545 struct kgetbmap *p,
1546 struct getbmapx __user *u,
1547 size_t recsize)
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001548{
Christoph Hellwig232b51942017-10-17 14:16:19 -07001549 if (put_user(p->bmv_offset, &u->bmv_offset) ||
1550 put_user(p->bmv_block, &u->bmv_block) ||
1551 put_user(p->bmv_length, &u->bmv_length) ||
1552 put_user(0, &u->bmv_count) ||
1553 put_user(0, &u->bmv_entries))
1554 return false;
1555 if (recsize < sizeof(struct getbmapx))
1556 return true;
1557 if (put_user(0, &u->bmv_iflags) ||
1558 put_user(p->bmv_oflags, &u->bmv_oflags) ||
1559 put_user(0, &u->bmv_unused1) ||
1560 put_user(0, &u->bmv_unused2))
1561 return false;
1562 return true;
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001563}
1564
1565STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566xfs_ioc_getbmap(
Christoph Hellwig8f3e2052016-07-20 11:29:35 +10001567 struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 unsigned int cmd,
1569 void __user *arg)
1570{
Darrick J. Wongbe6324c2017-04-03 15:17:57 -07001571 struct getbmapx bmx = { 0 };
Christoph Hellwig232b51942017-10-17 14:16:19 -07001572 struct kgetbmap *buf;
1573 size_t recsize;
1574 int error, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575
Christoph Hellwig232b51942017-10-17 14:16:19 -07001576 switch (cmd) {
1577 case XFS_IOC_GETBMAPA:
1578 bmx.bmv_iflags = BMV_IF_ATTRFORK;
1579 /*FALLTHRU*/
1580 case XFS_IOC_GETBMAP:
1581 if (file->f_mode & FMODE_NOCMTIME)
1582 bmx.bmv_iflags |= BMV_IF_NO_DMAPI_READ;
1583 /* struct getbmap is a strict subset of struct getbmapx. */
1584 recsize = sizeof(struct getbmap);
1585 break;
1586 case XFS_IOC_GETBMAPX:
1587 recsize = sizeof(struct getbmapx);
1588 break;
1589 default:
1590 return -EINVAL;
1591 }
1592
1593 if (copy_from_user(&bmx, arg, recsize))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10001594 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001596 if (bmx.bmv_count < 2)
Eric Sandeenb474c7a2014-06-22 15:04:54 +10001597 return -EINVAL;
Christoph Hellwig232b51942017-10-17 14:16:19 -07001598 if (bmx.bmv_count > ULONG_MAX / recsize)
1599 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
Christoph Hellwig232b51942017-10-17 14:16:19 -07001601 buf = kmem_zalloc_large(bmx.bmv_count * sizeof(*buf), 0);
1602 if (!buf)
1603 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
Christoph Hellwig232b51942017-10-17 14:16:19 -07001605 error = xfs_getbmap(XFS_I(file_inode(file)), &bmx, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 if (error)
Christoph Hellwig232b51942017-10-17 14:16:19 -07001607 goto out_free_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
Christoph Hellwig232b51942017-10-17 14:16:19 -07001609 error = -EFAULT;
1610 if (copy_to_user(arg, &bmx, recsize))
1611 goto out_free_buf;
1612 arg += recsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
Christoph Hellwig232b51942017-10-17 14:16:19 -07001614 for (i = 0; i < bmx.bmv_entries; i++) {
1615 if (!xfs_getbmap_format(buf + i, arg, recsize))
1616 goto out_free_buf;
1617 arg += recsize;
1618 }
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001619
Christoph Hellwig232b51942017-10-17 14:16:19 -07001620 error = 0;
1621out_free_buf:
1622 kmem_free(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 return 0;
1624}
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001625
Darrick J. Wonge89c0412017-03-28 14:56:37 -07001626struct getfsmap_info {
1627 struct xfs_mount *mp;
Christoph Hellwig9d17e142017-04-21 11:24:41 -07001628 struct fsmap_head __user *data;
1629 unsigned int idx;
Darrick J. Wonge89c0412017-03-28 14:56:37 -07001630 __u32 last_flags;
1631};
1632
1633STATIC int
1634xfs_getfsmap_format(struct xfs_fsmap *xfm, void *priv)
1635{
1636 struct getfsmap_info *info = priv;
1637 struct fsmap fm;
1638
1639 trace_xfs_getfsmap_mapping(info->mp, xfm);
1640
1641 info->last_flags = xfm->fmr_flags;
1642 xfs_fsmap_from_internal(&fm, xfm);
Christoph Hellwig9d17e142017-04-21 11:24:41 -07001643 if (copy_to_user(&info->data->fmh_recs[info->idx++], &fm,
1644 sizeof(struct fsmap)))
Darrick J. Wonge89c0412017-03-28 14:56:37 -07001645 return -EFAULT;
1646
Darrick J. Wonge89c0412017-03-28 14:56:37 -07001647 return 0;
1648}
1649
1650STATIC int
1651xfs_ioc_getfsmap(
1652 struct xfs_inode *ip,
Christoph Hellwig9d17e142017-04-21 11:24:41 -07001653 struct fsmap_head __user *arg)
Darrick J. Wonge89c0412017-03-28 14:56:37 -07001654{
Christoph Hellwigef2b67e2017-04-21 11:24:40 -07001655 struct getfsmap_info info = { NULL };
Darrick J. Wonge89c0412017-03-28 14:56:37 -07001656 struct xfs_fsmap_head xhead = {0};
1657 struct fsmap_head head;
1658 bool aborted = false;
1659 int error;
1660
1661 if (copy_from_user(&head, arg, sizeof(struct fsmap_head)))
1662 return -EFAULT;
1663 if (memchr_inv(head.fmh_reserved, 0, sizeof(head.fmh_reserved)) ||
1664 memchr_inv(head.fmh_keys[0].fmr_reserved, 0,
1665 sizeof(head.fmh_keys[0].fmr_reserved)) ||
1666 memchr_inv(head.fmh_keys[1].fmr_reserved, 0,
1667 sizeof(head.fmh_keys[1].fmr_reserved)))
1668 return -EINVAL;
1669
1670 xhead.fmh_iflags = head.fmh_iflags;
1671 xhead.fmh_count = head.fmh_count;
1672 xfs_fsmap_to_internal(&xhead.fmh_keys[0], &head.fmh_keys[0]);
1673 xfs_fsmap_to_internal(&xhead.fmh_keys[1], &head.fmh_keys[1]);
1674
1675 trace_xfs_getfsmap_low_key(ip->i_mount, &xhead.fmh_keys[0]);
1676 trace_xfs_getfsmap_high_key(ip->i_mount, &xhead.fmh_keys[1]);
1677
1678 info.mp = ip->i_mount;
Christoph Hellwig9d17e142017-04-21 11:24:41 -07001679 info.data = arg;
Darrick J. Wonge89c0412017-03-28 14:56:37 -07001680 error = xfs_getfsmap(ip->i_mount, &xhead, xfs_getfsmap_format, &info);
1681 if (error == XFS_BTREE_QUERY_RANGE_ABORT) {
1682 error = 0;
1683 aborted = true;
1684 } else if (error)
1685 return error;
1686
1687 /* If we didn't abort, set the "last" flag in the last fmx */
Darrick J. Wong12e4a382017-04-23 10:45:21 -07001688 if (!aborted && info.idx) {
Darrick J. Wonge89c0412017-03-28 14:56:37 -07001689 info.last_flags |= FMR_OF_LAST;
Christoph Hellwig9d17e142017-04-21 11:24:41 -07001690 if (copy_to_user(&info.data->fmh_recs[info.idx - 1].fmr_flags,
1691 &info.last_flags, sizeof(info.last_flags)))
Darrick J. Wonge89c0412017-03-28 14:56:37 -07001692 return -EFAULT;
1693 }
1694
1695 /* copy back header */
1696 head.fmh_entries = xhead.fmh_entries;
1697 head.fmh_oflags = xhead.fmh_oflags;
1698 if (copy_to_user(arg, &head, sizeof(struct fsmap_head)))
1699 return -EFAULT;
1700
1701 return 0;
1702}
1703
Darrick J. Wong36fd6e82017-10-17 21:37:34 -07001704STATIC int
1705xfs_ioc_scrub_metadata(
1706 struct xfs_inode *ip,
1707 void __user *arg)
1708{
1709 struct xfs_scrub_metadata scrub;
1710 int error;
1711
1712 if (!capable(CAP_SYS_ADMIN))
1713 return -EPERM;
1714
1715 if (copy_from_user(&scrub, arg, sizeof(scrub)))
1716 return -EFAULT;
1717
1718 error = xfs_scrub_metadata(ip, &scrub);
1719 if (error)
1720 return error;
1721
1722 if (copy_to_user(arg, &scrub, sizeof(scrub)))
1723 return -EFAULT;
1724
1725 return 0;
1726}
1727
Dave Chinnera133d952013-08-12 20:49:48 +10001728int
1729xfs_ioc_swapext(
1730 xfs_swapext_t *sxp)
1731{
1732 xfs_inode_t *ip, *tip;
1733 struct fd f, tmp;
1734 int error = 0;
1735
1736 /* Pull information for the target fd */
1737 f = fdget((int)sxp->sx_fdtarget);
1738 if (!f.file) {
Dave Chinner24513372014-06-25 14:58:08 +10001739 error = -EINVAL;
Dave Chinnera133d952013-08-12 20:49:48 +10001740 goto out;
1741 }
1742
1743 if (!(f.file->f_mode & FMODE_WRITE) ||
1744 !(f.file->f_mode & FMODE_READ) ||
1745 (f.file->f_flags & O_APPEND)) {
Dave Chinner24513372014-06-25 14:58:08 +10001746 error = -EBADF;
Dave Chinnera133d952013-08-12 20:49:48 +10001747 goto out_put_file;
1748 }
1749
1750 tmp = fdget((int)sxp->sx_fdtmp);
1751 if (!tmp.file) {
Dave Chinner24513372014-06-25 14:58:08 +10001752 error = -EINVAL;
Dave Chinnera133d952013-08-12 20:49:48 +10001753 goto out_put_file;
1754 }
1755
1756 if (!(tmp.file->f_mode & FMODE_WRITE) ||
1757 !(tmp.file->f_mode & FMODE_READ) ||
1758 (tmp.file->f_flags & O_APPEND)) {
Dave Chinner24513372014-06-25 14:58:08 +10001759 error = -EBADF;
Dave Chinnera133d952013-08-12 20:49:48 +10001760 goto out_put_tmp_file;
1761 }
1762
1763 if (IS_SWAPFILE(file_inode(f.file)) ||
1764 IS_SWAPFILE(file_inode(tmp.file))) {
Dave Chinner24513372014-06-25 14:58:08 +10001765 error = -EINVAL;
Dave Chinnera133d952013-08-12 20:49:48 +10001766 goto out_put_tmp_file;
1767 }
1768
Jann Horn7f1b6242016-07-20 10:30:30 +10001769 /*
1770 * We need to ensure that the fds passed in point to XFS inodes
1771 * before we cast and access them as XFS structures as we have no
1772 * control over what the user passes us here.
1773 */
1774 if (f.file->f_op != &xfs_file_operations ||
1775 tmp.file->f_op != &xfs_file_operations) {
1776 error = -EINVAL;
1777 goto out_put_tmp_file;
1778 }
1779
Dave Chinnera133d952013-08-12 20:49:48 +10001780 ip = XFS_I(file_inode(f.file));
1781 tip = XFS_I(file_inode(tmp.file));
1782
1783 if (ip->i_mount != tip->i_mount) {
Dave Chinner24513372014-06-25 14:58:08 +10001784 error = -EINVAL;
Dave Chinnera133d952013-08-12 20:49:48 +10001785 goto out_put_tmp_file;
1786 }
1787
1788 if (ip->i_ino == tip->i_ino) {
Dave Chinner24513372014-06-25 14:58:08 +10001789 error = -EINVAL;
Dave Chinnera133d952013-08-12 20:49:48 +10001790 goto out_put_tmp_file;
1791 }
1792
1793 if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
Dave Chinner24513372014-06-25 14:58:08 +10001794 error = -EIO;
Dave Chinnera133d952013-08-12 20:49:48 +10001795 goto out_put_tmp_file;
1796 }
1797
1798 error = xfs_swap_extents(ip, tip, sxp);
1799
1800 out_put_tmp_file:
1801 fdput(tmp);
1802 out_put_file:
1803 fdput(f);
1804 out:
1805 return error;
1806}
1807
Eric Sandeenf7664b32018-05-15 13:21:48 -07001808static int
1809xfs_ioc_getlabel(
1810 struct xfs_mount *mp,
1811 char __user *user_label)
1812{
1813 struct xfs_sb *sbp = &mp->m_sb;
1814 char label[XFSLABEL_MAX + 1];
1815
1816 /* Paranoia */
1817 BUILD_BUG_ON(sizeof(sbp->sb_fname) > FSLABEL_MAX);
1818
1819 spin_lock(&mp->m_sb_lock);
1820 strncpy(label, sbp->sb_fname, sizeof(sbp->sb_fname));
1821 spin_unlock(&mp->m_sb_lock);
1822
1823 /* xfs on-disk label is 12 chars, be sure we send a null to user */
1824 label[XFSLABEL_MAX] = '\0';
1825 if (copy_to_user(user_label, label, sizeof(sbp->sb_fname)))
1826 return -EFAULT;
1827 return 0;
1828}
1829
1830static int
1831xfs_ioc_setlabel(
1832 struct file *filp,
1833 struct xfs_mount *mp,
1834 char __user *newlabel)
1835{
1836 struct xfs_sb *sbp = &mp->m_sb;
1837 char label[XFSLABEL_MAX + 1];
1838 size_t len;
1839 int error;
1840
1841 if (!capable(CAP_SYS_ADMIN))
1842 return -EPERM;
1843 /*
1844 * The generic ioctl allows up to FSLABEL_MAX chars, but XFS is much
1845 * smaller, at 12 bytes. We copy one more to be sure we find the
1846 * (required) NULL character to test the incoming label length.
1847 * NB: The on disk label doesn't need to be null terminated.
1848 */
1849 if (copy_from_user(label, newlabel, XFSLABEL_MAX + 1))
1850 return -EFAULT;
1851 len = strnlen(label, XFSLABEL_MAX + 1);
1852 if (len > sizeof(sbp->sb_fname))
1853 return -EINVAL;
1854
1855 error = mnt_want_write_file(filp);
1856 if (error)
1857 return error;
1858
1859 spin_lock(&mp->m_sb_lock);
1860 memset(sbp->sb_fname, 0, sizeof(sbp->sb_fname));
1861 strncpy(sbp->sb_fname, label, sizeof(sbp->sb_fname));
1862 spin_unlock(&mp->m_sb_lock);
1863
1864 /*
1865 * Now we do several things to satisfy userspace.
1866 * In addition to normal logging of the primary superblock, we also
1867 * immediately write these changes to sector zero for the primary, then
1868 * update all backup supers (as xfs_db does for a label change), then
1869 * invalidate the block device page cache. This is so that any prior
1870 * buffered reads from userspace (i.e. from blkid) are invalidated,
1871 * and userspace will see the newly-written label.
1872 */
1873 error = xfs_sync_sb_buf(mp);
1874 if (error)
1875 goto out;
1876 /*
1877 * growfs also updates backup supers so lock against that.
1878 */
1879 mutex_lock(&mp->m_growlock);
1880 error = xfs_update_secondary_sbs(mp);
1881 mutex_unlock(&mp->m_growlock);
1882
1883 invalidate_bdev(mp->m_ddev_targp->bt_bdev);
1884
1885out:
1886 mnt_drop_write_file(filp);
1887 return error;
1888}
1889
Christoph Hellwig4d4be482008-12-09 04:47:33 -05001890/*
1891 * Note: some of the ioctl's return positive numbers as a
1892 * byte count indicating success, such as readlink_by_handle.
1893 * So we don't "sign flip" like most other routines. This means
1894 * true errors need to be returned as a negative value.
1895 */
1896long
1897xfs_file_ioctl(
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001898 struct file *filp,
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001899 unsigned int cmd,
Christoph Hellwig4d4be482008-12-09 04:47:33 -05001900 unsigned long p)
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001901{
Al Viro496ad9a2013-01-23 17:07:38 -05001902 struct inode *inode = file_inode(filp);
Christoph Hellwig4d4be482008-12-09 04:47:33 -05001903 struct xfs_inode *ip = XFS_I(inode);
1904 struct xfs_mount *mp = ip->i_mount;
1905 void __user *arg = (void __user *)p;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001906 int error;
1907
Christoph Hellwigcca28fb2010-06-24 11:57:09 +10001908 trace_xfs_file_ioctl(ip);
Christoph Hellwig4d4be482008-12-09 04:47:33 -05001909
1910 switch (cmd) {
Christoph Hellwiga46db602011-01-07 13:02:04 +00001911 case FITRIM:
1912 return xfs_ioc_trim(mp, arg);
Eric Sandeenf7664b32018-05-15 13:21:48 -07001913 case FS_IOC_GETFSLABEL:
1914 return xfs_ioc_getlabel(mp, arg);
1915 case FS_IOC_SETFSLABEL:
1916 return xfs_ioc_setlabel(filp, mp, arg);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001917 case XFS_IOC_ALLOCSP:
1918 case XFS_IOC_FREESP:
1919 case XFS_IOC_RESVSP:
1920 case XFS_IOC_UNRESVSP:
1921 case XFS_IOC_ALLOCSP64:
1922 case XFS_IOC_FREESP64:
1923 case XFS_IOC_RESVSP64:
Dave Chinner44722352010-08-24 12:02:11 +10001924 case XFS_IOC_UNRESVSP64:
1925 case XFS_IOC_ZERO_RANGE: {
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06001926 xfs_flock64_t bf;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001927
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06001928 if (copy_from_user(&bf, arg, sizeof(bf)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10001929 return -EFAULT;
Christoph Hellwig8f3e2052016-07-20 11:29:35 +10001930 return xfs_ioc_space(filp, cmd, &bf);
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06001931 }
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001932 case XFS_IOC_DIOINFO: {
1933 struct dioattr da;
1934 xfs_buftarg_t *target =
1935 XFS_IS_REALTIME_INODE(ip) ?
1936 mp->m_rtdev_targp : mp->m_ddev_targp;
1937
Eric Sandeen7c71ee72014-01-21 16:46:23 -06001938 da.d_mem = da.d_miniosz = target->bt_logical_sectorsize;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001939 da.d_maxiosz = INT_MAX & ~(da.d_miniosz - 1);
1940
1941 if (copy_to_user(arg, &da, sizeof(da)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10001942 return -EFAULT;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001943 return 0;
1944 }
1945
1946 case XFS_IOC_FSBULKSTAT_SINGLE:
1947 case XFS_IOC_FSBULKSTAT:
1948 case XFS_IOC_FSINUMBERS:
1949 return xfs_ioc_bulkstat(mp, cmd, arg);
1950
1951 case XFS_IOC_FSGEOMETRY_V1:
1952 return xfs_ioc_fsgeometry_v1(mp, arg);
1953
1954 case XFS_IOC_FSGEOMETRY:
1955 return xfs_ioc_fsgeometry(mp, arg);
1956
1957 case XFS_IOC_GETVERSION:
1958 return put_user(inode->i_generation, (int __user *)arg);
1959
1960 case XFS_IOC_FSGETXATTR:
1961 return xfs_ioc_fsgetxattr(ip, 0, arg);
1962 case XFS_IOC_FSGETXATTRA:
1963 return xfs_ioc_fsgetxattr(ip, 1, arg);
Lachlan McIlroy3b2816b2008-04-18 12:43:35 +10001964 case XFS_IOC_FSSETXATTR:
Lachlan McIlroy65e67f52008-04-18 12:59:45 +10001965 return xfs_ioc_fssetxattr(ip, filp, arg);
1966 case XFS_IOC_GETXFLAGS:
1967 return xfs_ioc_getxflags(ip, arg);
1968 case XFS_IOC_SETXFLAGS:
1969 return xfs_ioc_setxflags(ip, filp, arg);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001970
1971 case XFS_IOC_FSSETDM: {
1972 struct fsdmidata dmi;
1973
1974 if (copy_from_user(&dmi, arg, sizeof(dmi)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10001975 return -EFAULT;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001976
Jan Karad9457dc2012-06-12 16:20:39 +02001977 error = mnt_want_write_file(filp);
1978 if (error)
1979 return error;
1980
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001981 error = xfs_set_dmattrs(ip, dmi.fsd_dmevmask,
1982 dmi.fsd_dmstate);
Jan Karad9457dc2012-06-12 16:20:39 +02001983 mnt_drop_write_file(filp);
Dave Chinner24513372014-06-25 14:58:08 +10001984 return error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001985 }
1986
1987 case XFS_IOC_GETBMAP:
1988 case XFS_IOC_GETBMAPA:
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001989 case XFS_IOC_GETBMAPX:
Christoph Hellwig232b51942017-10-17 14:16:19 -07001990 return xfs_ioc_getbmap(filp, cmd, arg);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001991
Darrick J. Wonge89c0412017-03-28 14:56:37 -07001992 case FS_IOC_GETFSMAP:
1993 return xfs_ioc_getfsmap(ip, arg);
1994
Darrick J. Wong36fd6e82017-10-17 21:37:34 -07001995 case XFS_IOC_SCRUB_METADATA:
1996 return xfs_ioc_scrub_metadata(ip, arg);
1997
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001998 case XFS_IOC_FD_TO_HANDLE:
1999 case XFS_IOC_PATH_TO_HANDLE:
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06002000 case XFS_IOC_PATH_TO_FSHANDLE: {
2001 xfs_fsop_handlereq_t hreq;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002002
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06002003 if (copy_from_user(&hreq, arg, sizeof(hreq)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002004 return -EFAULT;
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06002005 return xfs_find_handle(cmd, &hreq);
2006 }
2007 case XFS_IOC_OPEN_BY_HANDLE: {
2008 xfs_fsop_handlereq_t hreq;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002009
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06002010 if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002011 return -EFAULT;
Christoph Hellwigd296d302009-01-19 02:02:57 +01002012 return xfs_open_by_handle(filp, &hreq);
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06002013 }
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002014 case XFS_IOC_FSSETDM_BY_HANDLE:
Christoph Hellwigd296d302009-01-19 02:02:57 +01002015 return xfs_fssetdm_by_handle(filp, arg);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002016
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06002017 case XFS_IOC_READLINK_BY_HANDLE: {
2018 xfs_fsop_handlereq_t hreq;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002019
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06002020 if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002021 return -EFAULT;
Christoph Hellwigd296d302009-01-19 02:02:57 +01002022 return xfs_readlink_by_handle(filp, &hreq);
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06002023 }
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002024 case XFS_IOC_ATTRLIST_BY_HANDLE:
Christoph Hellwigd296d302009-01-19 02:02:57 +01002025 return xfs_attrlist_by_handle(filp, arg);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002026
2027 case XFS_IOC_ATTRMULTI_BY_HANDLE:
Christoph Hellwigd296d302009-01-19 02:02:57 +01002028 return xfs_attrmulti_by_handle(filp, arg);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002029
2030 case XFS_IOC_SWAPEXT: {
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06002031 struct xfs_swapext sxp;
2032
2033 if (copy_from_user(&sxp, arg, sizeof(xfs_swapext_t)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002034 return -EFAULT;
Jan Karad9457dc2012-06-12 16:20:39 +02002035 error = mnt_want_write_file(filp);
2036 if (error)
2037 return error;
Dave Chinnera133d952013-08-12 20:49:48 +10002038 error = xfs_ioc_swapext(&sxp);
Jan Karad9457dc2012-06-12 16:20:39 +02002039 mnt_drop_write_file(filp);
Dave Chinner24513372014-06-25 14:58:08 +10002040 return error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002041 }
2042
2043 case XFS_IOC_FSCOUNTS: {
2044 xfs_fsop_counts_t out;
2045
2046 error = xfs_fs_counts(mp, &out);
2047 if (error)
Dave Chinner24513372014-06-25 14:58:08 +10002048 return error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002049
2050 if (copy_to_user(arg, &out, sizeof(out)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002051 return -EFAULT;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002052 return 0;
2053 }
2054
2055 case XFS_IOC_SET_RESBLKS: {
2056 xfs_fsop_resblks_t inout;
Darrick J. Wongc8ce5402017-06-16 11:00:05 -07002057 uint64_t in;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002058
2059 if (!capable(CAP_SYS_ADMIN))
2060 return -EPERM;
2061
Eric Sandeend5db0f92010-02-05 22:59:53 +00002062 if (mp->m_flags & XFS_MOUNT_RDONLY)
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002063 return -EROFS;
Eric Sandeend5db0f92010-02-05 22:59:53 +00002064
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002065 if (copy_from_user(&inout, arg, sizeof(inout)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002066 return -EFAULT;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002067
Jan Karad9457dc2012-06-12 16:20:39 +02002068 error = mnt_want_write_file(filp);
2069 if (error)
2070 return error;
2071
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002072 /* input parameter is passed in resblks field of structure */
2073 in = inout.resblks;
2074 error = xfs_reserve_blocks(mp, &in, &inout);
Jan Karad9457dc2012-06-12 16:20:39 +02002075 mnt_drop_write_file(filp);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002076 if (error)
Dave Chinner24513372014-06-25 14:58:08 +10002077 return error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002078
2079 if (copy_to_user(arg, &inout, sizeof(inout)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002080 return -EFAULT;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002081 return 0;
2082 }
2083
2084 case XFS_IOC_GET_RESBLKS: {
2085 xfs_fsop_resblks_t out;
2086
2087 if (!capable(CAP_SYS_ADMIN))
2088 return -EPERM;
2089
2090 error = xfs_reserve_blocks(mp, NULL, &out);
2091 if (error)
Dave Chinner24513372014-06-25 14:58:08 +10002092 return error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002093
2094 if (copy_to_user(arg, &out, sizeof(out)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002095 return -EFAULT;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002096
2097 return 0;
2098 }
2099
2100 case XFS_IOC_FSGROWFSDATA: {
2101 xfs_growfs_data_t in;
2102
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002103 if (copy_from_user(&in, arg, sizeof(in)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002104 return -EFAULT;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002105
Jan Karad9457dc2012-06-12 16:20:39 +02002106 error = mnt_want_write_file(filp);
2107 if (error)
2108 return error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002109 error = xfs_growfs_data(mp, &in);
Jan Karad9457dc2012-06-12 16:20:39 +02002110 mnt_drop_write_file(filp);
Dave Chinner24513372014-06-25 14:58:08 +10002111 return error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002112 }
2113
2114 case XFS_IOC_FSGROWFSLOG: {
2115 xfs_growfs_log_t in;
2116
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002117 if (copy_from_user(&in, arg, sizeof(in)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002118 return -EFAULT;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002119
Jan Karad9457dc2012-06-12 16:20:39 +02002120 error = mnt_want_write_file(filp);
2121 if (error)
2122 return error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002123 error = xfs_growfs_log(mp, &in);
Jan Karad9457dc2012-06-12 16:20:39 +02002124 mnt_drop_write_file(filp);
Dave Chinner24513372014-06-25 14:58:08 +10002125 return error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002126 }
2127
2128 case XFS_IOC_FSGROWFSRT: {
2129 xfs_growfs_rt_t in;
2130
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002131 if (copy_from_user(&in, arg, sizeof(in)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002132 return -EFAULT;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002133
Jan Karad9457dc2012-06-12 16:20:39 +02002134 error = mnt_want_write_file(filp);
2135 if (error)
2136 return error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002137 error = xfs_growfs_rt(mp, &in);
Jan Karad9457dc2012-06-12 16:20:39 +02002138 mnt_drop_write_file(filp);
Dave Chinner24513372014-06-25 14:58:08 +10002139 return error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002140 }
2141
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002142 case XFS_IOC_GOINGDOWN: {
Darrick J. Wongc8ce5402017-06-16 11:00:05 -07002143 uint32_t in;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002144
2145 if (!capable(CAP_SYS_ADMIN))
2146 return -EPERM;
2147
Darrick J. Wongc8ce5402017-06-16 11:00:05 -07002148 if (get_user(in, (uint32_t __user *)arg))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002149 return -EFAULT;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002150
Dave Chinner24513372014-06-25 14:58:08 +10002151 return xfs_fs_goingdown(mp, in);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002152 }
2153
2154 case XFS_IOC_ERROR_INJECTION: {
2155 xfs_error_injection_t in;
2156
2157 if (!capable(CAP_SYS_ADMIN))
2158 return -EPERM;
2159
2160 if (copy_from_user(&in, arg, sizeof(in)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002161 return -EFAULT;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002162
Darrick J. Wong31965ef2017-06-20 17:54:46 -07002163 return xfs_errortag_add(mp, in.errtag);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002164 }
2165
2166 case XFS_IOC_ERROR_CLEARALL:
2167 if (!capable(CAP_SYS_ADMIN))
2168 return -EPERM;
2169
Darrick J. Wong31965ef2017-06-20 17:54:46 -07002170 return xfs_errortag_clearall(mp);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002171
Brian Foster8ca149d2012-11-07 12:21:12 -05002172 case XFS_IOC_FREE_EOFBLOCKS: {
Dwight Engenb9fe5052013-08-15 14:08:02 -04002173 struct xfs_fs_eofblocks eofb;
2174 struct xfs_eofblocks keofb;
Brian Foster8ca149d2012-11-07 12:21:12 -05002175
Dwight Engen8c567a72013-08-15 14:08:03 -04002176 if (!capable(CAP_SYS_ADMIN))
2177 return -EPERM;
2178
2179 if (mp->m_flags & XFS_MOUNT_RDONLY)
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002180 return -EROFS;
Dwight Engen8c567a72013-08-15 14:08:03 -04002181
Brian Foster8ca149d2012-11-07 12:21:12 -05002182 if (copy_from_user(&eofb, arg, sizeof(eofb)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002183 return -EFAULT;
Brian Foster8ca149d2012-11-07 12:21:12 -05002184
Dwight Engenb9fe5052013-08-15 14:08:02 -04002185 error = xfs_fs_eofblocks_from_user(&eofb, &keofb);
2186 if (error)
Dave Chinner24513372014-06-25 14:58:08 +10002187 return error;
Brian Foster8ca149d2012-11-07 12:21:12 -05002188
Dave Chinner24513372014-06-25 14:58:08 +10002189 return xfs_icache_free_eofblocks(mp, &keofb);
Brian Foster8ca149d2012-11-07 12:21:12 -05002190 }
2191
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10002192 default:
2193 return -ENOTTY;
2194 }
2195}