blob: ea65a73d49ba3d9eebbad4f5793042545ccb66d4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include "xfs_fs.h"
Dave Chinner70a9883c2013-10-23 10:36:05 +110020#include "xfs_shared.h"
Dave Chinner239880e2013-10-23 10:50:10 +110021#include "xfs_format.h"
22#include "xfs_log_format.h"
23#include "xfs_trans_resv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_sb.h"
Nathan Scotta844f452005-11-02 14:38:42 +110025#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "xfs_alloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "xfs_dinode.h"
30#include "xfs_inode.h"
Hannes Eder7bf446f2009-03-05 15:20:25 +010031#include "xfs_ioctl.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_rtalloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "xfs_itable.h"
Nathan Scotta844f452005-11-02 14:38:42 +110034#include "xfs_error.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_attr.h"
Nathan Scotta844f452005-11-02 14:38:42 +110036#include "xfs_bmap.h"
Dave Chinner68988112013-08-12 20:49:42 +100037#include "xfs_bmap_util.h"
Dave Chinner239880e2013-10-23 10:50:10 +110038#include "xfs_trans.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include "xfs_buf_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include "xfs_fsops.h"
Christoph Hellwiga46db602011-01-07 13:02:04 +000041#include "xfs_discard.h"
Christoph Hellwig25fe55e2008-07-18 17:13:20 +100042#include "xfs_quota.h"
43#include "xfs_inode_item.h"
Christoph Hellwigd296d302009-01-19 02:02:57 +010044#include "xfs_export.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000045#include "xfs_trace.h"
Brian Foster8ca149d2012-11-07 12:21:12 -050046#include "xfs_icache.h"
Dave Chinnerc24b5df2013-08-12 20:49:45 +100047#include "xfs_symlink.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080049#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/dcache.h>
51#include <linux/mount.h>
52#include <linux/namei.h>
53#include <linux/pagemap.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090054#include <linux/slab.h>
Christoph Hellwigd296d302009-01-19 02:02:57 +010055#include <linux/exportfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57/*
58 * xfs_find_handle maps from userspace xfs_fsop_handlereq structure to
59 * a file or fs handle.
60 *
61 * XFS_IOC_PATH_TO_FSHANDLE
62 * returns fs handle for a mount point or path within that mount point
63 * XFS_IOC_FD_TO_HANDLE
64 * returns full handle for a FD opened in user space
65 * XFS_IOC_PATH_TO_HANDLE
66 * returns full handle for a path
67 */
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -060068int
Linus Torvalds1da177e2005-04-16 15:20:36 -070069xfs_find_handle(
70 unsigned int cmd,
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -060071 xfs_fsop_handlereq_t *hreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
73 int hsize;
74 xfs_handle_t handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 struct inode *inode;
Dave Chinnera30b0362013-09-02 20:49:36 +100076 struct fd f = {NULL};
Christoph Hellwig4346cdd2009-02-08 21:51:14 +010077 struct path path;
Al Viro2903ff02012-08-28 12:52:22 -040078 int error;
Christoph Hellwig4346cdd2009-02-08 21:51:14 +010079 struct xfs_inode *ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Christoph Hellwig4346cdd2009-02-08 21:51:14 +010081 if (cmd == XFS_IOC_FD_TO_HANDLE) {
Al Viro2903ff02012-08-28 12:52:22 -040082 f = fdget(hreq->fd);
83 if (!f.file)
Christoph Hellwig4346cdd2009-02-08 21:51:14 +010084 return -EBADF;
Al Viro496ad9a2013-01-23 17:07:38 -050085 inode = file_inode(f.file);
Christoph Hellwig4346cdd2009-02-08 21:51:14 +010086 } else {
87 error = user_lpath((const char __user *)hreq->path, &path);
88 if (error)
89 return error;
90 inode = path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 }
Christoph Hellwig4346cdd2009-02-08 21:51:14 +010092 ip = XFS_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Christoph Hellwig4346cdd2009-02-08 21:51:14 +010094 /*
95 * We can only generate handles for inodes residing on a XFS filesystem,
96 * and only for regular files, directories or symbolic links.
97 */
98 error = -EINVAL;
99 if (inode->i_sb->s_magic != XFS_SB_MAGIC)
100 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Christoph Hellwig4346cdd2009-02-08 21:51:14 +0100102 error = -EBADF;
103 if (!S_ISREG(inode->i_mode) &&
104 !S_ISDIR(inode->i_mode) &&
105 !S_ISLNK(inode->i_mode))
106 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Christoph Hellwig4346cdd2009-02-08 21:51:14 +0100109 memcpy(&handle.ha_fsid, ip->i_mount->m_fixedfsid, sizeof(xfs_fsid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Christoph Hellwig4346cdd2009-02-08 21:51:14 +0100111 if (cmd == XFS_IOC_PATH_TO_FSHANDLE) {
112 /*
113 * This handle only contains an fsid, zero the rest.
114 */
115 memset(&handle.ha_fid, 0, sizeof(handle.ha_fid));
116 hsize = sizeof(xfs_fsid_t);
117 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 int lock_mode;
119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 lock_mode = xfs_ilock_map_shared(ip);
Christoph Hellwigc6143912007-09-14 15:22:37 +1000121 handle.ha_fid.fid_len = sizeof(xfs_fid_t) -
122 sizeof(handle.ha_fid.fid_len);
123 handle.ha_fid.fid_pad = 0;
124 handle.ha_fid.fid_gen = ip->i_d.di_gen;
125 handle.ha_fid.fid_ino = ip->i_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 xfs_iunlock_map_shared(ip, lock_mode);
127
128 hsize = XFS_HSIZE(handle);
129 }
130
Christoph Hellwig4346cdd2009-02-08 21:51:14 +0100131 error = -EFAULT;
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -0600132 if (copy_to_user(hreq->ohandle, &handle, hsize) ||
Christoph Hellwig4346cdd2009-02-08 21:51:14 +0100133 copy_to_user(hreq->ohandlen, &hsize, sizeof(__s32)))
134 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Christoph Hellwig4346cdd2009-02-08 21:51:14 +0100136 error = 0;
137
138 out_put:
139 if (cmd == XFS_IOC_FD_TO_HANDLE)
Al Viro2903ff02012-08-28 12:52:22 -0400140 fdput(f);
Christoph Hellwig4346cdd2009-02-08 21:51:14 +0100141 else
142 path_put(&path);
143 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144}
145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146/*
Christoph Hellwigd296d302009-01-19 02:02:57 +0100147 * No need to do permission checks on the various pathname components
148 * as the handle operations are privileged.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 */
150STATIC int
Christoph Hellwigd296d302009-01-19 02:02:57 +0100151xfs_handle_acceptable(
152 void *context,
153 struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
Christoph Hellwigd296d302009-01-19 02:02:57 +0100155 return 1;
156}
157
158/*
159 * Convert userspace handle data into a dentry.
160 */
161struct dentry *
162xfs_handle_to_dentry(
163 struct file *parfilp,
164 void __user *uhandle,
165 u32 hlen)
166{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 xfs_handle_t handle;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100168 struct xfs_fid64 fid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170 /*
171 * Only allow handle opens under a directory.
172 */
Al Viro496ad9a2013-01-23 17:07:38 -0500173 if (!S_ISDIR(file_inode(parfilp)->i_mode))
Christoph Hellwigd296d302009-01-19 02:02:57 +0100174 return ERR_PTR(-ENOTDIR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Christoph Hellwigd296d302009-01-19 02:02:57 +0100176 if (hlen != sizeof(xfs_handle_t))
177 return ERR_PTR(-EINVAL);
178 if (copy_from_user(&handle, uhandle, hlen))
179 return ERR_PTR(-EFAULT);
180 if (handle.ha_fid.fid_len !=
181 sizeof(handle.ha_fid) - sizeof(handle.ha_fid.fid_len))
182 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Christoph Hellwigd296d302009-01-19 02:02:57 +0100184 memset(&fid, 0, sizeof(struct fid));
185 fid.ino = handle.ha_fid.fid_ino;
186 fid.gen = handle.ha_fid.fid_gen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Christoph Hellwigd296d302009-01-19 02:02:57 +0100188 return exportfs_decode_fh(parfilp->f_path.mnt, (struct fid *)&fid, 3,
189 FILEID_INO32_GEN | XFS_FILEID_TYPE_64FLAG,
190 xfs_handle_acceptable, NULL);
191}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Christoph Hellwigd296d302009-01-19 02:02:57 +0100193STATIC struct dentry *
194xfs_handlereq_to_dentry(
195 struct file *parfilp,
196 xfs_fsop_handlereq_t *hreq)
197{
198 return xfs_handle_to_dentry(parfilp, hreq->ihandle, hreq->ihandlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199}
200
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -0600201int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202xfs_open_by_handle(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 struct file *parfilp,
Christoph Hellwigd296d302009-01-19 02:02:57 +0100204 xfs_fsop_handlereq_t *hreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
David Howells745ca242008-11-14 10:39:22 +1100206 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 int error;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100208 int fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 int permflag;
210 struct file *filp;
211 struct inode *inode;
212 struct dentry *dentry;
Dave Chinner1a1d7722012-03-22 05:15:06 +0000213 fmode_t fmode;
Al Viro765927b2012-06-26 21:58:53 +0400214 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
216 if (!capable(CAP_SYS_ADMIN))
217 return -XFS_ERROR(EPERM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Christoph Hellwigd296d302009-01-19 02:02:57 +0100219 dentry = xfs_handlereq_to_dentry(parfilp, hreq);
220 if (IS_ERR(dentry))
221 return PTR_ERR(dentry);
222 inode = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224 /* Restrict xfs_open_by_handle to directories & regular files. */
225 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode))) {
Christoph Hellwigd296d302009-01-19 02:02:57 +0100226 error = -XFS_ERROR(EPERM);
227 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 }
229
230#if BITS_PER_LONG != 32
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -0600231 hreq->oflags |= O_LARGEFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232#endif
Christoph Hellwigd296d302009-01-19 02:02:57 +0100233
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -0600234 permflag = hreq->oflags;
Dave Chinner1a1d7722012-03-22 05:15:06 +0000235 fmode = OPEN_FMODE(permflag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 if ((!(permflag & O_APPEND) || (permflag & O_TRUNC)) &&
Dave Chinner1a1d7722012-03-22 05:15:06 +0000237 (fmode & FMODE_WRITE) && IS_APPEND(inode)) {
Christoph Hellwigd296d302009-01-19 02:02:57 +0100238 error = -XFS_ERROR(EPERM);
239 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 }
241
Dave Chinner1a1d7722012-03-22 05:15:06 +0000242 if ((fmode & FMODE_WRITE) && IS_IMMUTABLE(inode)) {
Christoph Hellwigd296d302009-01-19 02:02:57 +0100243 error = -XFS_ERROR(EACCES);
244 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 }
246
247 /* Can't write directories. */
Dave Chinner1a1d7722012-03-22 05:15:06 +0000248 if (S_ISDIR(inode->i_mode) && (fmode & FMODE_WRITE)) {
Christoph Hellwigd296d302009-01-19 02:02:57 +0100249 error = -XFS_ERROR(EISDIR);
250 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 }
252
Yann Droneaud862a6292013-07-02 18:39:34 +0200253 fd = get_unused_fd_flags(0);
Christoph Hellwigd296d302009-01-19 02:02:57 +0100254 if (fd < 0) {
255 error = fd;
256 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 }
258
Al Viro765927b2012-06-26 21:58:53 +0400259 path.mnt = parfilp->f_path.mnt;
260 path.dentry = dentry;
261 filp = dentry_open(&path, hreq->oflags, cred);
262 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 if (IS_ERR(filp)) {
Christoph Hellwigd296d302009-01-19 02:02:57 +0100264 put_unused_fd(fd);
265 return PTR_ERR(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 }
Christoph Hellwig4d4be482008-12-09 04:47:33 -0500267
Al Viro03209372011-07-25 20:54:24 -0400268 if (S_ISREG(inode->i_mode)) {
Vlad Apostolov2e2e7bb2006-11-11 18:04:47 +1100269 filp->f_flags |= O_NOATIME;
Christoph Hellwig4d4be482008-12-09 04:47:33 -0500270 filp->f_mode |= FMODE_NOCMTIME;
Vlad Apostolov2e2e7bb2006-11-11 18:04:47 +1100271 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Christoph Hellwigd296d302009-01-19 02:02:57 +0100273 fd_install(fd, filp);
274 return fd;
275
276 out_dput:
277 dput(dentry);
278 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279}
280
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000281/*
282 * This is a copy from fs/namei.c:vfs_readlink(), except for removing it's
283 * unused first argument.
284 */
285STATIC int
286do_readlink(
287 char __user *buffer,
288 int buflen,
289 const char *link)
290{
291 int len;
292
293 len = PTR_ERR(link);
294 if (IS_ERR(link))
295 goto out;
296
297 len = strlen(link);
298 if (len > (unsigned) buflen)
299 len = buflen;
300 if (copy_to_user(buffer, link, len))
301 len = -EFAULT;
302 out:
303 return len;
304}
305
306
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -0600307int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308xfs_readlink_by_handle(
Christoph Hellwigd296d302009-01-19 02:02:57 +0100309 struct file *parfilp,
310 xfs_fsop_handlereq_t *hreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
Christoph Hellwigd296d302009-01-19 02:02:57 +0100312 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 __u32 olen;
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000314 void *link;
315 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317 if (!capable(CAP_SYS_ADMIN))
318 return -XFS_ERROR(EPERM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Christoph Hellwigd296d302009-01-19 02:02:57 +0100320 dentry = xfs_handlereq_to_dentry(parfilp, hreq);
321 if (IS_ERR(dentry))
322 return PTR_ERR(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324 /* Restrict this handle operation to symlinks only. */
Christoph Hellwigd296d302009-01-19 02:02:57 +0100325 if (!S_ISLNK(dentry->d_inode->i_mode)) {
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000326 error = -XFS_ERROR(EINVAL);
Christoph Hellwigd296d302009-01-19 02:02:57 +0100327 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 }
329
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -0600330 if (copy_from_user(&olen, hreq->ohandlen, sizeof(__u32))) {
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000331 error = -XFS_ERROR(EFAULT);
Christoph Hellwigd296d302009-01-19 02:02:57 +0100332 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000335 link = kmalloc(MAXPATHLEN+1, GFP_KERNEL);
Christoph Hellwigd296d302009-01-19 02:02:57 +0100336 if (!link) {
337 error = -XFS_ERROR(ENOMEM);
338 goto out_dput;
339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Christoph Hellwigd296d302009-01-19 02:02:57 +0100341 error = -xfs_readlink(XFS_I(dentry->d_inode), link);
Nathan Scott67fcaa72006-06-09 17:00:52 +1000342 if (error)
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000343 goto out_kfree;
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -0600344 error = do_readlink(hreq->ohandle, olen, link);
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000345 if (error)
346 goto out_kfree;
Nathan Scott67fcaa72006-06-09 17:00:52 +1000347
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000348 out_kfree:
349 kfree(link);
Christoph Hellwigd296d302009-01-19 02:02:57 +0100350 out_dput:
351 dput(dentry);
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000352 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353}
354
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000355int
356xfs_set_dmattrs(
357 xfs_inode_t *ip,
358 u_int evmask,
359 u_int16_t state)
360{
361 xfs_mount_t *mp = ip->i_mount;
362 xfs_trans_t *tp;
363 int error;
364
365 if (!capable(CAP_SYS_ADMIN))
366 return XFS_ERROR(EPERM);
367
368 if (XFS_FORCED_SHUTDOWN(mp))
369 return XFS_ERROR(EIO);
370
371 tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS);
Jie Liu3d3c8b52013-08-12 20:49:59 +1000372 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_ichange, 0, 0);
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000373 if (error) {
374 xfs_trans_cancel(tp, 0);
375 return error;
376 }
377 xfs_ilock(ip, XFS_ILOCK_EXCL);
378 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
379
380 ip->i_d.di_dmevmask = evmask;
381 ip->i_d.di_dmstate = state;
382
383 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
384 error = xfs_trans_commit(tp, 0);
385
386 return error;
387}
388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389STATIC int
390xfs_fssetdm_by_handle(
Christoph Hellwigd296d302009-01-19 02:02:57 +0100391 struct file *parfilp,
392 void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
394 int error;
395 struct fsdmidata fsd;
396 xfs_fsop_setdm_handlereq_t dmhreq;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100397 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399 if (!capable(CAP_MKNOD))
400 return -XFS_ERROR(EPERM);
401 if (copy_from_user(&dmhreq, arg, sizeof(xfs_fsop_setdm_handlereq_t)))
402 return -XFS_ERROR(EFAULT);
403
Jan Karad9457dc2012-06-12 16:20:39 +0200404 error = mnt_want_write_file(parfilp);
405 if (error)
406 return error;
407
Christoph Hellwigd296d302009-01-19 02:02:57 +0100408 dentry = xfs_handlereq_to_dentry(parfilp, &dmhreq.hreq);
Jan Karad9457dc2012-06-12 16:20:39 +0200409 if (IS_ERR(dentry)) {
410 mnt_drop_write_file(parfilp);
Christoph Hellwigd296d302009-01-19 02:02:57 +0100411 return PTR_ERR(dentry);
Jan Karad9457dc2012-06-12 16:20:39 +0200412 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Christoph Hellwigd296d302009-01-19 02:02:57 +0100414 if (IS_IMMUTABLE(dentry->d_inode) || IS_APPEND(dentry->d_inode)) {
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000415 error = -XFS_ERROR(EPERM);
416 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 }
418
419 if (copy_from_user(&fsd, dmhreq.data, sizeof(fsd))) {
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000420 error = -XFS_ERROR(EFAULT);
421 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 }
423
Christoph Hellwigd296d302009-01-19 02:02:57 +0100424 error = -xfs_set_dmattrs(XFS_I(dentry->d_inode), fsd.fsd_dmevmask,
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000425 fsd.fsd_dmstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000427 out:
Jan Karad9457dc2012-06-12 16:20:39 +0200428 mnt_drop_write_file(parfilp);
Christoph Hellwigd296d302009-01-19 02:02:57 +0100429 dput(dentry);
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000430 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431}
432
433STATIC int
434xfs_attrlist_by_handle(
Christoph Hellwigd296d302009-01-19 02:02:57 +0100435 struct file *parfilp,
436 void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
Christoph Hellwigd296d302009-01-19 02:02:57 +0100438 int error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 attrlist_cursor_kern_t *cursor;
440 xfs_fsop_attrlist_handlereq_t al_hreq;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100441 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 char *kbuf;
443
444 if (!capable(CAP_SYS_ADMIN))
445 return -XFS_ERROR(EPERM);
446 if (copy_from_user(&al_hreq, arg, sizeof(xfs_fsop_attrlist_handlereq_t)))
447 return -XFS_ERROR(EFAULT);
448 if (al_hreq.buflen > XATTR_LIST_MAX)
449 return -XFS_ERROR(EINVAL);
450
Christoph Hellwig90ad58a2008-06-27 13:32:19 +1000451 /*
452 * Reject flags, only allow namespaces.
453 */
454 if (al_hreq.flags & ~(ATTR_ROOT | ATTR_SECURE))
455 return -XFS_ERROR(EINVAL);
456
Christoph Hellwigd296d302009-01-19 02:02:57 +0100457 dentry = xfs_handlereq_to_dentry(parfilp, &al_hreq.hreq);
458 if (IS_ERR(dentry))
459 return PTR_ERR(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Dave Chinnerfdd3cce2013-09-02 20:53:00 +1000461 kbuf = kmem_zalloc_large(al_hreq.buflen, KM_SLEEP);
462 if (!kbuf)
463 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
465 cursor = (attrlist_cursor_kern_t *)&al_hreq.pos;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100466 error = -xfs_attr_list(XFS_I(dentry->d_inode), kbuf, al_hreq.buflen,
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000467 al_hreq.flags, cursor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 if (error)
469 goto out_kfree;
470
471 if (copy_to_user(al_hreq.buffer, kbuf, al_hreq.buflen))
472 error = -EFAULT;
473
Dave Chinnerfdd3cce2013-09-02 20:53:00 +1000474out_kfree:
475 kmem_free(kbuf);
476out_dput:
Christoph Hellwigd296d302009-01-19 02:02:57 +0100477 dput(dentry);
478 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479}
480
sandeen@sandeen.net28750972008-11-25 21:20:15 -0600481int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482xfs_attrmulti_attr_get(
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000483 struct inode *inode,
Dave Chinnera9273ca2010-01-20 10:47:48 +1100484 unsigned char *name,
485 unsigned char __user *ubuf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 __uint32_t *len,
487 __uint32_t flags)
488{
Dave Chinnera9273ca2010-01-20 10:47:48 +1100489 unsigned char *kbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 int error = EFAULT;
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 if (*len > XATTR_SIZE_MAX)
493 return EINVAL;
Dave Chinnerfdd3cce2013-09-02 20:53:00 +1000494 kbuf = kmem_zalloc_large(*len, KM_SLEEP);
495 if (!kbuf)
496 return ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000498 error = xfs_attr_get(XFS_I(inode), name, kbuf, (int *)len, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 if (error)
500 goto out_kfree;
501
502 if (copy_to_user(ubuf, kbuf, *len))
503 error = EFAULT;
504
Dave Chinnerfdd3cce2013-09-02 20:53:00 +1000505out_kfree:
506 kmem_free(kbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 return error;
508}
509
sandeen@sandeen.net28750972008-11-25 21:20:15 -0600510int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511xfs_attrmulti_attr_set(
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000512 struct inode *inode,
Dave Chinnera9273ca2010-01-20 10:47:48 +1100513 unsigned char *name,
514 const unsigned char __user *ubuf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 __uint32_t len,
516 __uint32_t flags)
517{
Dave Chinnera9273ca2010-01-20 10:47:48 +1100518 unsigned char *kbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 int error = EFAULT;
520
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000521 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 return EPERM;
523 if (len > XATTR_SIZE_MAX)
524 return EINVAL;
525
Li Zefan0e639bd2009-04-08 15:08:04 +0800526 kbuf = memdup_user(ubuf, len);
527 if (IS_ERR(kbuf))
528 return PTR_ERR(kbuf);
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000529
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000530 error = xfs_attr_set(XFS_I(inode), name, kbuf, len, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 return error;
533}
534
sandeen@sandeen.net28750972008-11-25 21:20:15 -0600535int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536xfs_attrmulti_attr_remove(
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000537 struct inode *inode,
Dave Chinnera9273ca2010-01-20 10:47:48 +1100538 unsigned char *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 __uint32_t flags)
540{
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000541 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 return EPERM;
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000543 return xfs_attr_remove(XFS_I(inode), name, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544}
545
546STATIC int
547xfs_attrmulti_by_handle(
Dave Hansen42a74f22008-02-15 14:37:46 -0800548 struct file *parfilp,
Christoph Hellwigd296d302009-01-19 02:02:57 +0100549 void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
551 int error;
552 xfs_attr_multiop_t *ops;
553 xfs_fsop_attrmulti_handlereq_t am_hreq;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100554 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 unsigned int i, size;
Dave Chinnera9273ca2010-01-20 10:47:48 +1100556 unsigned char *attr_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
558 if (!capable(CAP_SYS_ADMIN))
559 return -XFS_ERROR(EPERM);
560 if (copy_from_user(&am_hreq, arg, sizeof(xfs_fsop_attrmulti_handlereq_t)))
561 return -XFS_ERROR(EFAULT);
562
Zhitong Wangfda168c2010-03-23 09:51:22 +1100563 /* overflow check */
564 if (am_hreq.opcount >= INT_MAX / sizeof(xfs_attr_multiop_t))
565 return -E2BIG;
566
Christoph Hellwigd296d302009-01-19 02:02:57 +0100567 dentry = xfs_handlereq_to_dentry(parfilp, &am_hreq.hreq);
568 if (IS_ERR(dentry))
569 return PTR_ERR(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
571 error = E2BIG;
Christoph Hellwige182f572008-06-27 13:32:31 +1000572 size = am_hreq.opcount * sizeof(xfs_attr_multiop_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 if (!size || size > 16 * PAGE_SIZE)
Christoph Hellwigd296d302009-01-19 02:02:57 +0100574 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Li Zefan0e639bd2009-04-08 15:08:04 +0800576 ops = memdup_user(am_hreq.ops, size);
577 if (IS_ERR(ops)) {
578 error = PTR_ERR(ops);
Christoph Hellwigd296d302009-01-19 02:02:57 +0100579 goto out_dput;
Li Zefan0e639bd2009-04-08 15:08:04 +0800580 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582 attr_name = kmalloc(MAXNAMELEN, GFP_KERNEL);
583 if (!attr_name)
584 goto out_kfree_ops;
585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 error = 0;
587 for (i = 0; i < am_hreq.opcount; i++) {
Dave Chinnera9273ca2010-01-20 10:47:48 +1100588 ops[i].am_error = strncpy_from_user((char *)attr_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 ops[i].am_attrname, MAXNAMELEN);
590 if (ops[i].am_error == 0 || ops[i].am_error == MAXNAMELEN)
591 error = -ERANGE;
592 if (ops[i].am_error < 0)
593 break;
594
595 switch (ops[i].am_opcode) {
596 case ATTR_OP_GET:
Christoph Hellwigd296d302009-01-19 02:02:57 +0100597 ops[i].am_error = xfs_attrmulti_attr_get(
598 dentry->d_inode, attr_name,
599 ops[i].am_attrvalue, &ops[i].am_length,
600 ops[i].am_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 break;
602 case ATTR_OP_SET:
Al Viroa561be72011-11-23 11:57:51 -0500603 ops[i].am_error = mnt_want_write_file(parfilp);
Dave Hansen42a74f22008-02-15 14:37:46 -0800604 if (ops[i].am_error)
605 break;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100606 ops[i].am_error = xfs_attrmulti_attr_set(
607 dentry->d_inode, attr_name,
608 ops[i].am_attrvalue, ops[i].am_length,
609 ops[i].am_flags);
Al Viro2a79f172011-12-09 08:06:57 -0500610 mnt_drop_write_file(parfilp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 break;
612 case ATTR_OP_REMOVE:
Al Viroa561be72011-11-23 11:57:51 -0500613 ops[i].am_error = mnt_want_write_file(parfilp);
Dave Hansen42a74f22008-02-15 14:37:46 -0800614 if (ops[i].am_error)
615 break;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100616 ops[i].am_error = xfs_attrmulti_attr_remove(
617 dentry->d_inode, attr_name,
618 ops[i].am_flags);
Al Viro2a79f172011-12-09 08:06:57 -0500619 mnt_drop_write_file(parfilp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 break;
621 default:
622 ops[i].am_error = EINVAL;
623 }
624 }
625
626 if (copy_to_user(am_hreq.ops, ops, size))
627 error = XFS_ERROR(EFAULT);
628
629 kfree(attr_name);
630 out_kfree_ops:
631 kfree(ops);
Christoph Hellwigd296d302009-01-19 02:02:57 +0100632 out_dput:
633 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 return -error;
635}
636
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -0600637int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638xfs_ioc_space(
Christoph Hellwig993386c2007-08-28 16:12:30 +1000639 struct xfs_inode *ip,
Alexey Dobriyanf37ea142006-09-28 10:52:04 +1000640 struct inode *inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 struct file *filp,
642 int ioflags,
643 unsigned int cmd,
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -0600644 xfs_flock64_t *bf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645{
Christoph Hellwig865e9442013-10-12 00:55:08 -0700646 struct xfs_mount *mp = ip->i_mount;
647 struct xfs_trans *tp;
648 struct iattr iattr;
649 bool setprealloc = false;
650 bool clrprealloc = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 int error;
652
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -0600653 /*
654 * Only allow the sys admin to reserve space unless
655 * unwritten extents are enabled.
656 */
657 if (!xfs_sb_version_hasextflgbit(&ip->i_mount->m_sb) &&
658 !capable(CAP_SYS_ADMIN))
659 return -XFS_ERROR(EPERM);
660
Alexey Dobriyanf37ea142006-09-28 10:52:04 +1000661 if (inode->i_flags & (S_IMMUTABLE|S_APPEND))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 return -XFS_ERROR(EPERM);
663
Eric Sandeenad4a8ac2005-09-02 16:41:16 +1000664 if (!(filp->f_mode & FMODE_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 return -XFS_ERROR(EBADF);
666
Alexey Dobriyanf37ea142006-09-28 10:52:04 +1000667 if (!S_ISREG(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 return -XFS_ERROR(EINVAL);
669
Jan Karad9457dc2012-06-12 16:20:39 +0200670 error = mnt_want_write_file(filp);
671 if (error)
672 return error;
Christoph Hellwig865e9442013-10-12 00:55:08 -0700673
Christoph Hellwig5f8aca82013-10-12 00:55:06 -0700674 xfs_ilock(ip, XFS_IOLOCK_EXCL);
Christoph Hellwig865e9442013-10-12 00:55:08 -0700675
676 switch (bf->l_whence) {
677 case 0: /*SEEK_SET*/
678 break;
679 case 1: /*SEEK_CUR*/
680 bf->l_start += filp->f_pos;
681 break;
682 case 2: /*SEEK_END*/
683 bf->l_start += XFS_ISIZE(ip);
684 break;
685 default:
686 error = XFS_ERROR(EINVAL);
687 goto out_unlock;
688 }
689
690 /*
691 * length of <= 0 for resv/unresv/zero is invalid. length for
692 * alloc/free is ignored completely and we have no idea what userspace
693 * might have set it to, so set it to zero to allow range
694 * checks to pass.
695 */
696 switch (cmd) {
697 case XFS_IOC_ZERO_RANGE:
698 case XFS_IOC_RESVSP:
699 case XFS_IOC_RESVSP64:
700 case XFS_IOC_UNRESVSP:
701 case XFS_IOC_UNRESVSP64:
702 if (bf->l_len <= 0) {
703 error = XFS_ERROR(EINVAL);
704 goto out_unlock;
705 }
706 break;
707 default:
708 bf->l_len = 0;
709 break;
710 }
711
712 if (bf->l_start < 0 ||
713 bf->l_start > mp->m_super->s_maxbytes ||
714 bf->l_start + bf->l_len < 0 ||
715 bf->l_start + bf->l_len >= mp->m_super->s_maxbytes) {
716 error = XFS_ERROR(EINVAL);
717 goto out_unlock;
718 }
719
720 switch (cmd) {
721 case XFS_IOC_ZERO_RANGE:
722 error = xfs_zero_file_space(ip, bf->l_start, bf->l_len);
723 if (!error)
724 setprealloc = true;
725 break;
726 case XFS_IOC_RESVSP:
727 case XFS_IOC_RESVSP64:
728 error = xfs_alloc_file_space(ip, bf->l_start, bf->l_len,
729 XFS_BMAPI_PREALLOC);
730 if (!error)
731 setprealloc = true;
732 break;
733 case XFS_IOC_UNRESVSP:
734 case XFS_IOC_UNRESVSP64:
735 error = xfs_free_file_space(ip, bf->l_start, bf->l_len);
736 break;
737 case XFS_IOC_ALLOCSP:
738 case XFS_IOC_ALLOCSP64:
739 case XFS_IOC_FREESP:
740 case XFS_IOC_FREESP64:
741 if (bf->l_start > XFS_ISIZE(ip)) {
742 error = xfs_alloc_file_space(ip, XFS_ISIZE(ip),
743 bf->l_start - XFS_ISIZE(ip), 0);
744 if (error)
745 goto out_unlock;
746 }
747
748 iattr.ia_valid = ATTR_SIZE;
749 iattr.ia_size = bf->l_start;
750
751 error = xfs_setattr_size(ip, &iattr);
752 if (!error)
753 clrprealloc = true;
754 break;
755 default:
756 ASSERT(0);
757 error = XFS_ERROR(EINVAL);
758 }
759
760 if (error)
761 goto out_unlock;
762
763 tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
764 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_writeid, 0, 0);
765 if (error) {
766 xfs_trans_cancel(tp, 0);
767 goto out_unlock;
768 }
769
770 xfs_ilock(ip, XFS_ILOCK_EXCL);
771 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
772
773 if (!(ioflags & IO_INVIS)) {
774 ip->i_d.di_mode &= ~S_ISUID;
775 if (ip->i_d.di_mode & S_IXGRP)
776 ip->i_d.di_mode &= ~S_ISGID;
777 xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
778 }
779
780 if (setprealloc)
781 ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
782 else if (clrprealloc)
783 ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
784
785 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
786 if (filp->f_flags & O_DSYNC)
787 xfs_trans_set_sync(tp);
788 error = xfs_trans_commit(tp, 0);
789
790out_unlock:
Christoph Hellwig5f8aca82013-10-12 00:55:06 -0700791 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Jan Karad9457dc2012-06-12 16:20:39 +0200792 mnt_drop_write_file(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 return -error;
794}
795
796STATIC int
797xfs_ioc_bulkstat(
798 xfs_mount_t *mp,
799 unsigned int cmd,
800 void __user *arg)
801{
802 xfs_fsop_bulkreq_t bulkreq;
803 int count; /* # of records returned */
804 xfs_ino_t inlast; /* last inode number */
805 int done;
806 int error;
807
808 /* done = 1 if there are more stats to get and if bulkstat */
809 /* should be called again (unused here, but used in dmapi) */
810
811 if (!capable(CAP_SYS_ADMIN))
812 return -EPERM;
813
814 if (XFS_FORCED_SHUTDOWN(mp))
815 return -XFS_ERROR(EIO);
816
817 if (copy_from_user(&bulkreq, arg, sizeof(xfs_fsop_bulkreq_t)))
818 return -XFS_ERROR(EFAULT);
819
820 if (copy_from_user(&inlast, bulkreq.lastip, sizeof(__s64)))
821 return -XFS_ERROR(EFAULT);
822
823 if ((count = bulkreq.icount) <= 0)
824 return -XFS_ERROR(EINVAL);
825
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100826 if (bulkreq.ubuffer == NULL)
827 return -XFS_ERROR(EINVAL);
828
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 if (cmd == XFS_IOC_FSINUMBERS)
830 error = xfs_inumbers(mp, &inlast, &count,
Michal Marekfaa63e92007-07-11 11:10:19 +1000831 bulkreq.ubuffer, xfs_inumbers_fmt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 else if (cmd == XFS_IOC_FSBULKSTAT_SINGLE)
833 error = xfs_bulkstat_single(mp, &inlast,
834 bulkreq.ubuffer, &done);
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100835 else /* XFS_IOC_FSBULKSTAT */
Christoph Hellwig7dce11d2010-06-23 18:11:11 +1000836 error = xfs_bulkstat(mp, &inlast, &count, xfs_bulkstat_one,
837 sizeof(xfs_bstat_t), bulkreq.ubuffer,
838 &done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
840 if (error)
841 return -error;
842
843 if (bulkreq.ocount != NULL) {
844 if (copy_to_user(bulkreq.lastip, &inlast,
845 sizeof(xfs_ino_t)))
846 return -XFS_ERROR(EFAULT);
847
848 if (copy_to_user(bulkreq.ocount, &count, sizeof(count)))
849 return -XFS_ERROR(EFAULT);
850 }
851
852 return 0;
853}
854
855STATIC int
856xfs_ioc_fsgeometry_v1(
857 xfs_mount_t *mp,
858 void __user *arg)
859{
Alex Eldereeb20362011-03-01 17:50:00 +0000860 xfs_fsop_geom_t fsgeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 int error;
862
Alex Eldereeb20362011-03-01 17:50:00 +0000863 error = xfs_fs_geometry(mp, &fsgeo, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 if (error)
865 return -error;
866
Alex Eldereeb20362011-03-01 17:50:00 +0000867 /*
868 * Caller should have passed an argument of type
869 * xfs_fsop_geom_v1_t. This is a proper subset of the
870 * xfs_fsop_geom_t that xfs_fs_geometry() fills in.
871 */
872 if (copy_to_user(arg, &fsgeo, sizeof(xfs_fsop_geom_v1_t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 return -XFS_ERROR(EFAULT);
874 return 0;
875}
876
877STATIC int
878xfs_ioc_fsgeometry(
879 xfs_mount_t *mp,
880 void __user *arg)
881{
882 xfs_fsop_geom_t fsgeo;
883 int error;
884
885 error = xfs_fs_geometry(mp, &fsgeo, 4);
886 if (error)
887 return -error;
888
889 if (copy_to_user(arg, &fsgeo, sizeof(fsgeo)))
890 return -XFS_ERROR(EFAULT);
891 return 0;
892}
893
894/*
895 * Linux extended inode flags interface.
896 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
898STATIC unsigned int
899xfs_merge_ioc_xflags(
900 unsigned int flags,
901 unsigned int start)
902{
903 unsigned int xflags = start;
904
Eric Sandeen39058a02007-02-10 18:37:10 +1100905 if (flags & FS_IMMUTABLE_FL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 xflags |= XFS_XFLAG_IMMUTABLE;
907 else
908 xflags &= ~XFS_XFLAG_IMMUTABLE;
Eric Sandeen39058a02007-02-10 18:37:10 +1100909 if (flags & FS_APPEND_FL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 xflags |= XFS_XFLAG_APPEND;
911 else
912 xflags &= ~XFS_XFLAG_APPEND;
Eric Sandeen39058a02007-02-10 18:37:10 +1100913 if (flags & FS_SYNC_FL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 xflags |= XFS_XFLAG_SYNC;
915 else
916 xflags &= ~XFS_XFLAG_SYNC;
Eric Sandeen39058a02007-02-10 18:37:10 +1100917 if (flags & FS_NOATIME_FL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 xflags |= XFS_XFLAG_NOATIME;
919 else
920 xflags &= ~XFS_XFLAG_NOATIME;
Eric Sandeen39058a02007-02-10 18:37:10 +1100921 if (flags & FS_NODUMP_FL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 xflags |= XFS_XFLAG_NODUMP;
923 else
924 xflags &= ~XFS_XFLAG_NODUMP;
925
926 return xflags;
927}
928
929STATIC unsigned int
930xfs_di2lxflags(
931 __uint16_t di_flags)
932{
933 unsigned int flags = 0;
934
935 if (di_flags & XFS_DIFLAG_IMMUTABLE)
Eric Sandeen39058a02007-02-10 18:37:10 +1100936 flags |= FS_IMMUTABLE_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 if (di_flags & XFS_DIFLAG_APPEND)
Eric Sandeen39058a02007-02-10 18:37:10 +1100938 flags |= FS_APPEND_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 if (di_flags & XFS_DIFLAG_SYNC)
Eric Sandeen39058a02007-02-10 18:37:10 +1100940 flags |= FS_SYNC_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 if (di_flags & XFS_DIFLAG_NOATIME)
Eric Sandeen39058a02007-02-10 18:37:10 +1100942 flags |= FS_NOATIME_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 if (di_flags & XFS_DIFLAG_NODUMP)
Eric Sandeen39058a02007-02-10 18:37:10 +1100944 flags |= FS_NODUMP_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 return flags;
946}
947
948STATIC int
Christoph Hellwigc83bfab2007-10-11 17:47:00 +1000949xfs_ioc_fsgetxattr(
950 xfs_inode_t *ip,
951 int attr,
952 void __user *arg)
953{
954 struct fsxattr fa;
955
Dan Rosenberga122eb22010-09-06 18:24:57 -0400956 memset(&fa, 0, sizeof(struct fsxattr));
957
Christoph Hellwigc83bfab2007-10-11 17:47:00 +1000958 xfs_ilock(ip, XFS_ILOCK_SHARED);
959 fa.fsx_xflags = xfs_ip2xflags(ip);
960 fa.fsx_extsize = ip->i_d.di_extsize << ip->i_mount->m_sb.sb_blocklog;
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +0000961 fa.fsx_projid = xfs_get_projid(ip);
Christoph Hellwigc83bfab2007-10-11 17:47:00 +1000962
963 if (attr) {
964 if (ip->i_afp) {
965 if (ip->i_afp->if_flags & XFS_IFEXTENTS)
966 fa.fsx_nextents = ip->i_afp->if_bytes /
967 sizeof(xfs_bmbt_rec_t);
968 else
969 fa.fsx_nextents = ip->i_d.di_anextents;
970 } else
971 fa.fsx_nextents = 0;
972 } else {
973 if (ip->i_df.if_flags & XFS_IFEXTENTS)
974 fa.fsx_nextents = ip->i_df.if_bytes /
975 sizeof(xfs_bmbt_rec_t);
976 else
977 fa.fsx_nextents = ip->i_d.di_nextents;
978 }
979 xfs_iunlock(ip, XFS_ILOCK_SHARED);
980
981 if (copy_to_user(arg, &fa, sizeof(fa)))
982 return -EFAULT;
983 return 0;
984}
985
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000986STATIC void
987xfs_set_diflags(
988 struct xfs_inode *ip,
989 unsigned int xflags)
990{
991 unsigned int di_flags;
992
993 /* can't set PREALLOC this way, just preserve it */
994 di_flags = (ip->i_d.di_flags & XFS_DIFLAG_PREALLOC);
995 if (xflags & XFS_XFLAG_IMMUTABLE)
996 di_flags |= XFS_DIFLAG_IMMUTABLE;
997 if (xflags & XFS_XFLAG_APPEND)
998 di_flags |= XFS_DIFLAG_APPEND;
999 if (xflags & XFS_XFLAG_SYNC)
1000 di_flags |= XFS_DIFLAG_SYNC;
1001 if (xflags & XFS_XFLAG_NOATIME)
1002 di_flags |= XFS_DIFLAG_NOATIME;
1003 if (xflags & XFS_XFLAG_NODUMP)
1004 di_flags |= XFS_DIFLAG_NODUMP;
1005 if (xflags & XFS_XFLAG_PROJINHERIT)
1006 di_flags |= XFS_DIFLAG_PROJINHERIT;
1007 if (xflags & XFS_XFLAG_NODEFRAG)
1008 di_flags |= XFS_DIFLAG_NODEFRAG;
1009 if (xflags & XFS_XFLAG_FILESTREAM)
1010 di_flags |= XFS_DIFLAG_FILESTREAM;
Al Viroabbede12011-07-26 02:31:30 -04001011 if (S_ISDIR(ip->i_d.di_mode)) {
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001012 if (xflags & XFS_XFLAG_RTINHERIT)
1013 di_flags |= XFS_DIFLAG_RTINHERIT;
1014 if (xflags & XFS_XFLAG_NOSYMLINKS)
1015 di_flags |= XFS_DIFLAG_NOSYMLINKS;
1016 if (xflags & XFS_XFLAG_EXTSZINHERIT)
1017 di_flags |= XFS_DIFLAG_EXTSZINHERIT;
Al Viroabbede12011-07-26 02:31:30 -04001018 } else if (S_ISREG(ip->i_d.di_mode)) {
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001019 if (xflags & XFS_XFLAG_REALTIME)
1020 di_flags |= XFS_DIFLAG_REALTIME;
1021 if (xflags & XFS_XFLAG_EXTSIZE)
1022 di_flags |= XFS_DIFLAG_EXTSIZE;
1023 }
1024
1025 ip->i_d.di_flags = di_flags;
1026}
1027
Christoph Hellwigf13fae22008-07-21 16:16:15 +10001028STATIC void
1029xfs_diflags_to_linux(
1030 struct xfs_inode *ip)
1031{
David Chinnere4f75292008-08-13 16:00:45 +10001032 struct inode *inode = VFS_I(ip);
Christoph Hellwigf13fae22008-07-21 16:16:15 +10001033 unsigned int xflags = xfs_ip2xflags(ip);
1034
1035 if (xflags & XFS_XFLAG_IMMUTABLE)
1036 inode->i_flags |= S_IMMUTABLE;
1037 else
1038 inode->i_flags &= ~S_IMMUTABLE;
1039 if (xflags & XFS_XFLAG_APPEND)
1040 inode->i_flags |= S_APPEND;
1041 else
1042 inode->i_flags &= ~S_APPEND;
1043 if (xflags & XFS_XFLAG_SYNC)
1044 inode->i_flags |= S_SYNC;
1045 else
1046 inode->i_flags &= ~S_SYNC;
1047 if (xflags & XFS_XFLAG_NOATIME)
1048 inode->i_flags |= S_NOATIME;
1049 else
1050 inode->i_flags &= ~S_NOATIME;
1051}
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001052
1053#define FSX_PROJID 1
1054#define FSX_EXTSIZE 2
1055#define FSX_XFLAGS 4
1056#define FSX_NONBLOCK 8
1057
1058STATIC int
1059xfs_ioctl_setattr(
1060 xfs_inode_t *ip,
1061 struct fsxattr *fa,
1062 int mask)
1063{
1064 struct xfs_mount *mp = ip->i_mount;
1065 struct xfs_trans *tp;
1066 unsigned int lock_flags = 0;
Christoph Hellwig7d095252009-06-08 15:33:32 +02001067 struct xfs_dquot *udqp = NULL;
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001068 struct xfs_dquot *pdqp = NULL;
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001069 struct xfs_dquot *olddquot = NULL;
1070 int code;
1071
Christoph Hellwigcca28fb2010-06-24 11:57:09 +10001072 trace_xfs_ioctl_setattr(ip);
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001073
1074 if (mp->m_flags & XFS_MOUNT_RDONLY)
1075 return XFS_ERROR(EROFS);
1076 if (XFS_FORCED_SHUTDOWN(mp))
1077 return XFS_ERROR(EIO);
1078
1079 /*
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +00001080 * Disallow 32bit project ids when projid32bit feature is not enabled.
Arkadiusz Mi?kiewicz23963e52010-08-26 10:19:43 +00001081 */
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +00001082 if ((mask & FSX_PROJID) && (fa->fsx_projid > (__uint16_t)-1) &&
1083 !xfs_sb_version_hasprojid32bit(&ip->i_mount->m_sb))
Arkadiusz Mi?kiewicz23963e52010-08-26 10:19:43 +00001084 return XFS_ERROR(EINVAL);
1085
1086 /*
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001087 * If disk quotas is on, we make sure that the dquots do exist on disk,
1088 * before we start any other transactions. Trying to do this later
1089 * is messy. We don't care to take a readlock to look at the ids
1090 * in inode here, because we can't hold it across the trans_reserve.
1091 * If the IDs do change before we take the ilock, we're covered
1092 * because the i_*dquot fields will get updated anyway.
1093 */
1094 if (XFS_IS_QUOTA_ON(mp) && (mask & FSX_PROJID)) {
Christoph Hellwig7d095252009-06-08 15:33:32 +02001095 code = xfs_qm_vop_dqalloc(ip, ip->i_d.di_uid,
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001096 ip->i_d.di_gid, fa->fsx_projid,
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001097 XFS_QMOPT_PQUOTA, &udqp, NULL, &pdqp);
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001098 if (code)
1099 return code;
1100 }
1101
1102 /*
1103 * For the other attributes, we acquire the inode lock and
1104 * first do an error checking pass.
1105 */
1106 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
Jie Liu3d3c8b52013-08-12 20:49:59 +10001107 code = xfs_trans_reserve(tp, &M_RES(mp)->tr_ichange, 0, 0);
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001108 if (code)
1109 goto error_return;
1110
1111 lock_flags = XFS_ILOCK_EXCL;
1112 xfs_ilock(ip, lock_flags);
1113
1114 /*
1115 * CAP_FOWNER overrides the following restrictions:
1116 *
1117 * The user ID of the calling process must be equal
1118 * to the file owner ID, except in cases where the
1119 * CAP_FSETID capability is applicable.
1120 */
Dwight Engenfd5e2aa2013-08-15 14:08:00 -04001121 if (!inode_owner_or_capable(VFS_I(ip))) {
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001122 code = XFS_ERROR(EPERM);
1123 goto error_return;
1124 }
1125
1126 /*
1127 * Do a quota reservation only if projid is actually going to change.
Dwight Engenfd5e2aa2013-08-15 14:08:00 -04001128 * Only allow changing of projid from init_user_ns since it is a
1129 * non user namespace aware identifier.
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001130 */
1131 if (mask & FSX_PROJID) {
Dwight Engenfd5e2aa2013-08-15 14:08:00 -04001132 if (current_user_ns() != &init_user_ns) {
1133 code = XFS_ERROR(EINVAL);
1134 goto error_return;
1135 }
1136
Christoph Hellwig7d095252009-06-08 15:33:32 +02001137 if (XFS_IS_QUOTA_RUNNING(mp) &&
1138 XFS_IS_PQUOTA_ON(mp) &&
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +00001139 xfs_get_projid(ip) != fa->fsx_projid) {
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001140 ASSERT(tp);
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001141 code = xfs_qm_vop_chown_reserve(tp, ip, udqp, NULL,
1142 pdqp, capable(CAP_FOWNER) ?
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001143 XFS_QMOPT_FORCE_RES : 0);
1144 if (code) /* out of quota */
1145 goto error_return;
1146 }
1147 }
1148
1149 if (mask & FSX_EXTSIZE) {
1150 /*
1151 * Can't change extent size if any extents are allocated.
1152 */
1153 if (ip->i_d.di_nextents &&
1154 ((ip->i_d.di_extsize << mp->m_sb.sb_blocklog) !=
1155 fa->fsx_extsize)) {
1156 code = XFS_ERROR(EINVAL); /* EFBIG? */
1157 goto error_return;
1158 }
1159
1160 /*
1161 * Extent size must be a multiple of the appropriate block
Dave Chinner53158372011-01-27 12:18:18 +11001162 * size, if set at all. It must also be smaller than the
1163 * maximum extent size supported by the filesystem.
1164 *
1165 * Also, for non-realtime files, limit the extent size hint to
1166 * half the size of the AGs in the filesystem so alignment
1167 * doesn't result in extents larger than an AG.
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001168 */
1169 if (fa->fsx_extsize != 0) {
Dave Chinner53158372011-01-27 12:18:18 +11001170 xfs_extlen_t size;
1171 xfs_fsblock_t extsize_fsb;
1172
1173 extsize_fsb = XFS_B_TO_FSB(mp, fa->fsx_extsize);
1174 if (extsize_fsb > MAXEXTLEN) {
1175 code = XFS_ERROR(EINVAL);
1176 goto error_return;
1177 }
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001178
1179 if (XFS_IS_REALTIME_INODE(ip) ||
1180 ((mask & FSX_XFLAGS) &&
1181 (fa->fsx_xflags & XFS_XFLAG_REALTIME))) {
1182 size = mp->m_sb.sb_rextsize <<
1183 mp->m_sb.sb_blocklog;
1184 } else {
1185 size = mp->m_sb.sb_blocksize;
Dave Chinner53158372011-01-27 12:18:18 +11001186 if (extsize_fsb > mp->m_sb.sb_agblocks / 2) {
1187 code = XFS_ERROR(EINVAL);
1188 goto error_return;
1189 }
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001190 }
1191
1192 if (fa->fsx_extsize % size) {
1193 code = XFS_ERROR(EINVAL);
1194 goto error_return;
1195 }
1196 }
1197 }
1198
1199
1200 if (mask & FSX_XFLAGS) {
1201 /*
1202 * Can't change realtime flag if any extents are allocated.
1203 */
1204 if ((ip->i_d.di_nextents || ip->i_delayed_blks) &&
1205 (XFS_IS_REALTIME_INODE(ip)) !=
1206 (fa->fsx_xflags & XFS_XFLAG_REALTIME)) {
1207 code = XFS_ERROR(EINVAL); /* EFBIG? */
1208 goto error_return;
1209 }
1210
1211 /*
1212 * If realtime flag is set then must have realtime data.
1213 */
1214 if ((fa->fsx_xflags & XFS_XFLAG_REALTIME)) {
1215 if ((mp->m_sb.sb_rblocks == 0) ||
1216 (mp->m_sb.sb_rextsize == 0) ||
1217 (ip->i_d.di_extsize % mp->m_sb.sb_rextsize)) {
1218 code = XFS_ERROR(EINVAL);
1219 goto error_return;
1220 }
1221 }
1222
1223 /*
1224 * Can't modify an immutable/append-only file unless
1225 * we have appropriate permission.
1226 */
1227 if ((ip->i_d.di_flags &
1228 (XFS_DIFLAG_IMMUTABLE|XFS_DIFLAG_APPEND) ||
1229 (fa->fsx_xflags &
1230 (XFS_XFLAG_IMMUTABLE | XFS_XFLAG_APPEND))) &&
1231 !capable(CAP_LINUX_IMMUTABLE)) {
1232 code = XFS_ERROR(EPERM);
1233 goto error_return;
1234 }
1235 }
1236
Christoph Hellwigddc34152011-09-19 15:00:54 +00001237 xfs_trans_ijoin(tp, ip, 0);
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001238
1239 /*
1240 * Change file ownership. Must be the owner or privileged.
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001241 */
1242 if (mask & FSX_PROJID) {
1243 /*
1244 * CAP_FSETID overrides the following restrictions:
1245 *
1246 * The set-user-ID and set-group-ID bits of a file will be
1247 * cleared upon successful return from chown()
1248 */
1249 if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
Dwight Engenfd5e2aa2013-08-15 14:08:00 -04001250 !inode_capable(VFS_I(ip), CAP_FSETID))
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001251 ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
1252
1253 /*
1254 * Change the ownerships and register quota modifications
1255 * in the transaction.
1256 */
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +00001257 if (xfs_get_projid(ip) != fa->fsx_projid) {
Christoph Hellwig7d095252009-06-08 15:33:32 +02001258 if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_PQUOTA_ON(mp)) {
1259 olddquot = xfs_qm_vop_chown(tp, ip,
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001260 &ip->i_pdquot, pdqp);
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001261 }
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +00001262 xfs_set_projid(ip, fa->fsx_projid);
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001263
1264 /*
1265 * We may have to rev the inode as well as
1266 * the superblock version number since projids didn't
1267 * exist before DINODE_VERSION_2 and SB_VERSION_NLINK.
1268 */
Christoph Hellwig51ce16d2008-11-28 14:23:39 +11001269 if (ip->i_d.di_version == 1)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001270 xfs_bump_ino_vers2(tp, ip);
1271 }
1272
1273 }
1274
1275 if (mask & FSX_EXTSIZE)
1276 ip->i_d.di_extsize = fa->fsx_extsize >> mp->m_sb.sb_blocklog;
Christoph Hellwigf13fae22008-07-21 16:16:15 +10001277 if (mask & FSX_XFLAGS) {
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001278 xfs_set_diflags(ip, fa->fsx_xflags);
Christoph Hellwigf13fae22008-07-21 16:16:15 +10001279 xfs_diflags_to_linux(ip);
1280 }
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001281
Dave Chinnerdcd79a12010-09-28 12:27:25 +10001282 xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001283 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001284
1285 XFS_STATS_INC(xs_ig_attrchg);
1286
1287 /*
1288 * If this is a synchronous mount, make sure that the
1289 * transaction goes to disk before returning to the user.
1290 * This is slightly sub-optimal in that truncates require
1291 * two sync transactions instead of one for wsync filesystems.
1292 * One for the truncate and one for the timestamps since we
1293 * don't want to change the timestamps unless we're sure the
1294 * truncate worked. Truncates are less than 1% of the laddis
1295 * mix so this probably isn't worth the trouble to optimize.
1296 */
1297 if (mp->m_flags & XFS_MOUNT_WSYNC)
1298 xfs_trans_set_sync(tp);
1299 code = xfs_trans_commit(tp, 0);
1300 xfs_iunlock(ip, lock_flags);
1301
1302 /*
1303 * Release any dquot(s) the inode had kept before chown.
1304 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02001305 xfs_qm_dqrele(olddquot);
1306 xfs_qm_dqrele(udqp);
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001307 xfs_qm_dqrele(pdqp);
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001308
Christoph Hellwig288699f2010-06-23 18:11:15 +10001309 return code;
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001310
1311 error_return:
Christoph Hellwig7d095252009-06-08 15:33:32 +02001312 xfs_qm_dqrele(udqp);
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001313 xfs_qm_dqrele(pdqp);
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001314 xfs_trans_cancel(tp, 0);
1315 if (lock_flags)
1316 xfs_iunlock(ip, lock_flags);
1317 return code;
1318}
1319
Christoph Hellwigc83bfab2007-10-11 17:47:00 +10001320STATIC int
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001321xfs_ioc_fssetxattr(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 xfs_inode_t *ip,
1323 struct file *filp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 void __user *arg)
1325{
1326 struct fsxattr fa;
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001327 unsigned int mask;
Jan Karad9457dc2012-06-12 16:20:39 +02001328 int error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001329
1330 if (copy_from_user(&fa, arg, sizeof(fa)))
1331 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001333 mask = FSX_XFLAGS | FSX_EXTSIZE | FSX_PROJID;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001334 if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001335 mask |= FSX_NONBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
Jan Karad9457dc2012-06-12 16:20:39 +02001337 error = mnt_want_write_file(filp);
1338 if (error)
1339 return error;
1340 error = xfs_ioctl_setattr(ip, &fa, mask);
1341 mnt_drop_write_file(filp);
1342 return -error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001343}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001345STATIC int
1346xfs_ioc_getxflags(
1347 xfs_inode_t *ip,
1348 void __user *arg)
1349{
1350 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001352 flags = xfs_di2lxflags(ip->i_d.di_flags);
1353 if (copy_to_user(arg, &flags, sizeof(flags)))
1354 return -EFAULT;
1355 return 0;
1356}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001358STATIC int
1359xfs_ioc_setxflags(
1360 xfs_inode_t *ip,
1361 struct file *filp,
1362 void __user *arg)
1363{
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001364 struct fsxattr fa;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001365 unsigned int flags;
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001366 unsigned int mask;
Jan Karad9457dc2012-06-12 16:20:39 +02001367 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001369 if (copy_from_user(&flags, arg, sizeof(flags)))
1370 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001372 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
1373 FS_NOATIME_FL | FS_NODUMP_FL | \
1374 FS_SYNC_FL))
1375 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001377 mask = FSX_XFLAGS;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001378 if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001379 mask |= FSX_NONBLOCK;
1380 fa.fsx_xflags = xfs_merge_ioc_xflags(flags, xfs_ip2xflags(ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
Jan Karad9457dc2012-06-12 16:20:39 +02001382 error = mnt_want_write_file(filp);
1383 if (error)
1384 return error;
1385 error = xfs_ioctl_setattr(ip, &fa, mask);
1386 mnt_drop_write_file(filp);
1387 return -error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388}
1389
1390STATIC int
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001391xfs_getbmap_format(void **ap, struct getbmapx *bmv, int *full)
1392{
1393 struct getbmap __user *base = *ap;
1394
1395 /* copy only getbmap portion (not getbmapx) */
1396 if (copy_to_user(base, bmv, sizeof(struct getbmap)))
1397 return XFS_ERROR(EFAULT);
1398
1399 *ap += sizeof(struct getbmap);
1400 return 0;
1401}
1402
1403STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404xfs_ioc_getbmap(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001405 struct xfs_inode *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 int ioflags,
1407 unsigned int cmd,
1408 void __user *arg)
1409{
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001410 struct getbmapx bmx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 int error;
1412
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001413 if (copy_from_user(&bmx, arg, sizeof(struct getbmapx)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 return -XFS_ERROR(EFAULT);
1415
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001416 if (bmx.bmv_count < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 return -XFS_ERROR(EINVAL);
1418
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001419 bmx.bmv_iflags = (cmd == XFS_IOC_GETBMAPA ? BMV_IF_ATTRFORK : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 if (ioflags & IO_INVIS)
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001421 bmx.bmv_iflags |= BMV_IF_NO_DMAPI_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001423 error = xfs_getbmap(ip, &bmx, xfs_getbmap_format,
1424 (struct getbmap *)arg+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 if (error)
1426 return -error;
1427
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001428 /* copy back header - only size of getbmap */
1429 if (copy_to_user(arg, &bmx, sizeof(struct getbmap)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 return -XFS_ERROR(EFAULT);
1431 return 0;
1432}
1433
1434STATIC int
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001435xfs_getbmapx_format(void **ap, struct getbmapx *bmv, int *full)
1436{
1437 struct getbmapx __user *base = *ap;
1438
1439 if (copy_to_user(base, bmv, sizeof(struct getbmapx)))
1440 return XFS_ERROR(EFAULT);
1441
1442 *ap += sizeof(struct getbmapx);
1443 return 0;
1444}
1445
1446STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447xfs_ioc_getbmapx(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001448 struct xfs_inode *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 void __user *arg)
1450{
1451 struct getbmapx bmx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 int error;
1453
1454 if (copy_from_user(&bmx, arg, sizeof(bmx)))
1455 return -XFS_ERROR(EFAULT);
1456
1457 if (bmx.bmv_count < 2)
1458 return -XFS_ERROR(EINVAL);
1459
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001460 if (bmx.bmv_iflags & (~BMV_IF_VALID))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 return -XFS_ERROR(EINVAL);
1462
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001463 error = xfs_getbmap(ip, &bmx, xfs_getbmapx_format,
1464 (struct getbmapx *)arg+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 if (error)
1466 return -error;
1467
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001468 /* copy back header */
1469 if (copy_to_user(arg, &bmx, sizeof(struct getbmapx)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 return -XFS_ERROR(EFAULT);
1471
1472 return 0;
1473}
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001474
Dave Chinnera133d952013-08-12 20:49:48 +10001475int
1476xfs_ioc_swapext(
1477 xfs_swapext_t *sxp)
1478{
1479 xfs_inode_t *ip, *tip;
1480 struct fd f, tmp;
1481 int error = 0;
1482
1483 /* Pull information for the target fd */
1484 f = fdget((int)sxp->sx_fdtarget);
1485 if (!f.file) {
1486 error = XFS_ERROR(EINVAL);
1487 goto out;
1488 }
1489
1490 if (!(f.file->f_mode & FMODE_WRITE) ||
1491 !(f.file->f_mode & FMODE_READ) ||
1492 (f.file->f_flags & O_APPEND)) {
1493 error = XFS_ERROR(EBADF);
1494 goto out_put_file;
1495 }
1496
1497 tmp = fdget((int)sxp->sx_fdtmp);
1498 if (!tmp.file) {
1499 error = XFS_ERROR(EINVAL);
1500 goto out_put_file;
1501 }
1502
1503 if (!(tmp.file->f_mode & FMODE_WRITE) ||
1504 !(tmp.file->f_mode & FMODE_READ) ||
1505 (tmp.file->f_flags & O_APPEND)) {
1506 error = XFS_ERROR(EBADF);
1507 goto out_put_tmp_file;
1508 }
1509
1510 if (IS_SWAPFILE(file_inode(f.file)) ||
1511 IS_SWAPFILE(file_inode(tmp.file))) {
1512 error = XFS_ERROR(EINVAL);
1513 goto out_put_tmp_file;
1514 }
1515
1516 ip = XFS_I(file_inode(f.file));
1517 tip = XFS_I(file_inode(tmp.file));
1518
1519 if (ip->i_mount != tip->i_mount) {
1520 error = XFS_ERROR(EINVAL);
1521 goto out_put_tmp_file;
1522 }
1523
1524 if (ip->i_ino == tip->i_ino) {
1525 error = XFS_ERROR(EINVAL);
1526 goto out_put_tmp_file;
1527 }
1528
1529 if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
1530 error = XFS_ERROR(EIO);
1531 goto out_put_tmp_file;
1532 }
1533
1534 error = xfs_swap_extents(ip, tip, sxp);
1535
1536 out_put_tmp_file:
1537 fdput(tmp);
1538 out_put_file:
1539 fdput(f);
1540 out:
1541 return error;
1542}
1543
Christoph Hellwig4d4be482008-12-09 04:47:33 -05001544/*
1545 * Note: some of the ioctl's return positive numbers as a
1546 * byte count indicating success, such as readlink_by_handle.
1547 * So we don't "sign flip" like most other routines. This means
1548 * true errors need to be returned as a negative value.
1549 */
1550long
1551xfs_file_ioctl(
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001552 struct file *filp,
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001553 unsigned int cmd,
Christoph Hellwig4d4be482008-12-09 04:47:33 -05001554 unsigned long p)
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001555{
Al Viro496ad9a2013-01-23 17:07:38 -05001556 struct inode *inode = file_inode(filp);
Christoph Hellwig4d4be482008-12-09 04:47:33 -05001557 struct xfs_inode *ip = XFS_I(inode);
1558 struct xfs_mount *mp = ip->i_mount;
1559 void __user *arg = (void __user *)p;
1560 int ioflags = 0;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001561 int error;
1562
Christoph Hellwig4d4be482008-12-09 04:47:33 -05001563 if (filp->f_mode & FMODE_NOCMTIME)
1564 ioflags |= IO_INVIS;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001565
Christoph Hellwigcca28fb2010-06-24 11:57:09 +10001566 trace_xfs_file_ioctl(ip);
Christoph Hellwig4d4be482008-12-09 04:47:33 -05001567
1568 switch (cmd) {
Christoph Hellwiga46db602011-01-07 13:02:04 +00001569 case FITRIM:
1570 return xfs_ioc_trim(mp, arg);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001571 case XFS_IOC_ALLOCSP:
1572 case XFS_IOC_FREESP:
1573 case XFS_IOC_RESVSP:
1574 case XFS_IOC_UNRESVSP:
1575 case XFS_IOC_ALLOCSP64:
1576 case XFS_IOC_FREESP64:
1577 case XFS_IOC_RESVSP64:
Dave Chinner44722352010-08-24 12:02:11 +10001578 case XFS_IOC_UNRESVSP64:
1579 case XFS_IOC_ZERO_RANGE: {
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06001580 xfs_flock64_t bf;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001581
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06001582 if (copy_from_user(&bf, arg, sizeof(bf)))
1583 return -XFS_ERROR(EFAULT);
1584 return xfs_ioc_space(ip, inode, filp, ioflags, cmd, &bf);
1585 }
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001586 case XFS_IOC_DIOINFO: {
1587 struct dioattr da;
1588 xfs_buftarg_t *target =
1589 XFS_IS_REALTIME_INODE(ip) ?
1590 mp->m_rtdev_targp : mp->m_ddev_targp;
1591
1592 da.d_mem = da.d_miniosz = 1 << target->bt_sshift;
1593 da.d_maxiosz = INT_MAX & ~(da.d_miniosz - 1);
1594
1595 if (copy_to_user(arg, &da, sizeof(da)))
1596 return -XFS_ERROR(EFAULT);
1597 return 0;
1598 }
1599
1600 case XFS_IOC_FSBULKSTAT_SINGLE:
1601 case XFS_IOC_FSBULKSTAT:
1602 case XFS_IOC_FSINUMBERS:
1603 return xfs_ioc_bulkstat(mp, cmd, arg);
1604
1605 case XFS_IOC_FSGEOMETRY_V1:
1606 return xfs_ioc_fsgeometry_v1(mp, arg);
1607
1608 case XFS_IOC_FSGEOMETRY:
1609 return xfs_ioc_fsgeometry(mp, arg);
1610
1611 case XFS_IOC_GETVERSION:
1612 return put_user(inode->i_generation, (int __user *)arg);
1613
1614 case XFS_IOC_FSGETXATTR:
1615 return xfs_ioc_fsgetxattr(ip, 0, arg);
1616 case XFS_IOC_FSGETXATTRA:
1617 return xfs_ioc_fsgetxattr(ip, 1, arg);
Lachlan McIlroy3b2816b2008-04-18 12:43:35 +10001618 case XFS_IOC_FSSETXATTR:
Lachlan McIlroy65e67f52008-04-18 12:59:45 +10001619 return xfs_ioc_fssetxattr(ip, filp, arg);
1620 case XFS_IOC_GETXFLAGS:
1621 return xfs_ioc_getxflags(ip, arg);
1622 case XFS_IOC_SETXFLAGS:
1623 return xfs_ioc_setxflags(ip, filp, arg);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001624
1625 case XFS_IOC_FSSETDM: {
1626 struct fsdmidata dmi;
1627
1628 if (copy_from_user(&dmi, arg, sizeof(dmi)))
1629 return -XFS_ERROR(EFAULT);
1630
Jan Karad9457dc2012-06-12 16:20:39 +02001631 error = mnt_want_write_file(filp);
1632 if (error)
1633 return error;
1634
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001635 error = xfs_set_dmattrs(ip, dmi.fsd_dmevmask,
1636 dmi.fsd_dmstate);
Jan Karad9457dc2012-06-12 16:20:39 +02001637 mnt_drop_write_file(filp);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001638 return -error;
1639 }
1640
1641 case XFS_IOC_GETBMAP:
1642 case XFS_IOC_GETBMAPA:
1643 return xfs_ioc_getbmap(ip, ioflags, cmd, arg);
1644
1645 case XFS_IOC_GETBMAPX:
1646 return xfs_ioc_getbmapx(ip, arg);
1647
1648 case XFS_IOC_FD_TO_HANDLE:
1649 case XFS_IOC_PATH_TO_HANDLE:
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06001650 case XFS_IOC_PATH_TO_FSHANDLE: {
1651 xfs_fsop_handlereq_t hreq;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001652
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06001653 if (copy_from_user(&hreq, arg, sizeof(hreq)))
1654 return -XFS_ERROR(EFAULT);
1655 return xfs_find_handle(cmd, &hreq);
1656 }
1657 case XFS_IOC_OPEN_BY_HANDLE: {
1658 xfs_fsop_handlereq_t hreq;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001659
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06001660 if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
1661 return -XFS_ERROR(EFAULT);
Christoph Hellwigd296d302009-01-19 02:02:57 +01001662 return xfs_open_by_handle(filp, &hreq);
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06001663 }
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001664 case XFS_IOC_FSSETDM_BY_HANDLE:
Christoph Hellwigd296d302009-01-19 02:02:57 +01001665 return xfs_fssetdm_by_handle(filp, arg);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001666
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06001667 case XFS_IOC_READLINK_BY_HANDLE: {
1668 xfs_fsop_handlereq_t hreq;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001669
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06001670 if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
1671 return -XFS_ERROR(EFAULT);
Christoph Hellwigd296d302009-01-19 02:02:57 +01001672 return xfs_readlink_by_handle(filp, &hreq);
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06001673 }
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001674 case XFS_IOC_ATTRLIST_BY_HANDLE:
Christoph Hellwigd296d302009-01-19 02:02:57 +01001675 return xfs_attrlist_by_handle(filp, arg);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001676
1677 case XFS_IOC_ATTRMULTI_BY_HANDLE:
Christoph Hellwigd296d302009-01-19 02:02:57 +01001678 return xfs_attrmulti_by_handle(filp, arg);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001679
1680 case XFS_IOC_SWAPEXT: {
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06001681 struct xfs_swapext sxp;
1682
1683 if (copy_from_user(&sxp, arg, sizeof(xfs_swapext_t)))
1684 return -XFS_ERROR(EFAULT);
Jan Karad9457dc2012-06-12 16:20:39 +02001685 error = mnt_want_write_file(filp);
1686 if (error)
1687 return error;
Dave Chinnera133d952013-08-12 20:49:48 +10001688 error = xfs_ioc_swapext(&sxp);
Jan Karad9457dc2012-06-12 16:20:39 +02001689 mnt_drop_write_file(filp);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001690 return -error;
1691 }
1692
1693 case XFS_IOC_FSCOUNTS: {
1694 xfs_fsop_counts_t out;
1695
1696 error = xfs_fs_counts(mp, &out);
1697 if (error)
1698 return -error;
1699
1700 if (copy_to_user(arg, &out, sizeof(out)))
1701 return -XFS_ERROR(EFAULT);
1702 return 0;
1703 }
1704
1705 case XFS_IOC_SET_RESBLKS: {
1706 xfs_fsop_resblks_t inout;
1707 __uint64_t in;
1708
1709 if (!capable(CAP_SYS_ADMIN))
1710 return -EPERM;
1711
Eric Sandeend5db0f92010-02-05 22:59:53 +00001712 if (mp->m_flags & XFS_MOUNT_RDONLY)
1713 return -XFS_ERROR(EROFS);
1714
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001715 if (copy_from_user(&inout, arg, sizeof(inout)))
1716 return -XFS_ERROR(EFAULT);
1717
Jan Karad9457dc2012-06-12 16:20:39 +02001718 error = mnt_want_write_file(filp);
1719 if (error)
1720 return error;
1721
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001722 /* input parameter is passed in resblks field of structure */
1723 in = inout.resblks;
1724 error = xfs_reserve_blocks(mp, &in, &inout);
Jan Karad9457dc2012-06-12 16:20:39 +02001725 mnt_drop_write_file(filp);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001726 if (error)
1727 return -error;
1728
1729 if (copy_to_user(arg, &inout, sizeof(inout)))
1730 return -XFS_ERROR(EFAULT);
1731 return 0;
1732 }
1733
1734 case XFS_IOC_GET_RESBLKS: {
1735 xfs_fsop_resblks_t out;
1736
1737 if (!capable(CAP_SYS_ADMIN))
1738 return -EPERM;
1739
1740 error = xfs_reserve_blocks(mp, NULL, &out);
1741 if (error)
1742 return -error;
1743
1744 if (copy_to_user(arg, &out, sizeof(out)))
1745 return -XFS_ERROR(EFAULT);
1746
1747 return 0;
1748 }
1749
1750 case XFS_IOC_FSGROWFSDATA: {
1751 xfs_growfs_data_t in;
1752
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001753 if (copy_from_user(&in, arg, sizeof(in)))
1754 return -XFS_ERROR(EFAULT);
1755
Jan Karad9457dc2012-06-12 16:20:39 +02001756 error = mnt_want_write_file(filp);
1757 if (error)
1758 return error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001759 error = xfs_growfs_data(mp, &in);
Jan Karad9457dc2012-06-12 16:20:39 +02001760 mnt_drop_write_file(filp);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001761 return -error;
1762 }
1763
1764 case XFS_IOC_FSGROWFSLOG: {
1765 xfs_growfs_log_t in;
1766
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001767 if (copy_from_user(&in, arg, sizeof(in)))
1768 return -XFS_ERROR(EFAULT);
1769
Jan Karad9457dc2012-06-12 16:20:39 +02001770 error = mnt_want_write_file(filp);
1771 if (error)
1772 return error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001773 error = xfs_growfs_log(mp, &in);
Jan Karad9457dc2012-06-12 16:20:39 +02001774 mnt_drop_write_file(filp);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001775 return -error;
1776 }
1777
1778 case XFS_IOC_FSGROWFSRT: {
1779 xfs_growfs_rt_t in;
1780
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001781 if (copy_from_user(&in, arg, sizeof(in)))
1782 return -XFS_ERROR(EFAULT);
1783
Jan Karad9457dc2012-06-12 16:20:39 +02001784 error = mnt_want_write_file(filp);
1785 if (error)
1786 return error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001787 error = xfs_growfs_rt(mp, &in);
Jan Karad9457dc2012-06-12 16:20:39 +02001788 mnt_drop_write_file(filp);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001789 return -error;
1790 }
1791
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001792 case XFS_IOC_GOINGDOWN: {
1793 __uint32_t in;
1794
1795 if (!capable(CAP_SYS_ADMIN))
1796 return -EPERM;
1797
1798 if (get_user(in, (__uint32_t __user *)arg))
1799 return -XFS_ERROR(EFAULT);
1800
1801 error = xfs_fs_goingdown(mp, in);
1802 return -error;
1803 }
1804
1805 case XFS_IOC_ERROR_INJECTION: {
1806 xfs_error_injection_t in;
1807
1808 if (!capable(CAP_SYS_ADMIN))
1809 return -EPERM;
1810
1811 if (copy_from_user(&in, arg, sizeof(in)))
1812 return -XFS_ERROR(EFAULT);
1813
1814 error = xfs_errortag_add(in.errtag, mp);
1815 return -error;
1816 }
1817
1818 case XFS_IOC_ERROR_CLEARALL:
1819 if (!capable(CAP_SYS_ADMIN))
1820 return -EPERM;
1821
1822 error = xfs_errortag_clearall(mp, 1);
1823 return -error;
1824
Brian Foster8ca149d2012-11-07 12:21:12 -05001825 case XFS_IOC_FREE_EOFBLOCKS: {
Dwight Engenb9fe5052013-08-15 14:08:02 -04001826 struct xfs_fs_eofblocks eofb;
1827 struct xfs_eofblocks keofb;
Brian Foster8ca149d2012-11-07 12:21:12 -05001828
Dwight Engen8c567a72013-08-15 14:08:03 -04001829 if (!capable(CAP_SYS_ADMIN))
1830 return -EPERM;
1831
1832 if (mp->m_flags & XFS_MOUNT_RDONLY)
1833 return -XFS_ERROR(EROFS);
1834
Brian Foster8ca149d2012-11-07 12:21:12 -05001835 if (copy_from_user(&eofb, arg, sizeof(eofb)))
1836 return -XFS_ERROR(EFAULT);
1837
Dwight Engenb9fe5052013-08-15 14:08:02 -04001838 error = xfs_fs_eofblocks_from_user(&eofb, &keofb);
1839 if (error)
1840 return -error;
Brian Foster8ca149d2012-11-07 12:21:12 -05001841
Dwight Engenb9fe5052013-08-15 14:08:02 -04001842 return -xfs_icache_free_eofblocks(mp, &keofb);
Brian Foster8ca149d2012-11-07 12:21:12 -05001843 }
1844
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001845 default:
1846 return -ENOTTY;
1847 }
1848}