blob: ce636bf0d5504f3cbc58fe0ce31d3586d6b0fca5 [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 Chinner6ca1c902013-08-12 20:49:26 +100020#include "xfs_format.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "xfs_log.h"
22#include "xfs_trans.h"
23#include "xfs_sb.h"
Nathan Scotta844f452005-11-02 14:38:42 +110024#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "xfs_alloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_dinode.h"
29#include "xfs_inode.h"
Hannes Eder7bf446f2009-03-05 15:20:25 +010030#include "xfs_ioctl.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_rtalloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_itable.h"
Nathan Scotta844f452005-11-02 14:38:42 +110033#include "xfs_error.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_attr.h"
Nathan Scotta844f452005-11-02 14:38:42 +110035#include "xfs_bmap.h"
Dave Chinner68988112013-08-12 20:49:42 +100036#include "xfs_bmap_util.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include "xfs_buf_item.h"
38#include "xfs_utils.h"
39#include "xfs_dfrag.h"
40#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 Chinner531c3bd2012-10-25 17:22:30 +110076 struct fd f = {0};
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);
372 error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES (mp), 0, 0, 0);
373 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
Eric Sandeendd700d92013-04-04 00:05:13 -0500461 kbuf = kmem_zalloc(al_hreq.buflen, KM_SLEEP | KM_MAYFAIL);
462 if (!kbuf) {
463 kbuf = kmem_zalloc_large(al_hreq.buflen);
464 if (!kbuf)
465 goto out_dput;
466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
468 cursor = (attrlist_cursor_kern_t *)&al_hreq.pos;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100469 error = -xfs_attr_list(XFS_I(dentry->d_inode), kbuf, al_hreq.buflen,
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000470 al_hreq.flags, cursor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 if (error)
472 goto out_kfree;
473
474 if (copy_to_user(al_hreq.buffer, kbuf, al_hreq.buflen))
475 error = -EFAULT;
476
477 out_kfree:
Eric Sandeendd700d92013-04-04 00:05:13 -0500478 if (is_vmalloc_addr(kbuf))
479 kmem_free_large(kbuf);
480 else
481 kmem_free(kbuf);
Christoph Hellwigd296d302009-01-19 02:02:57 +0100482 out_dput:
483 dput(dentry);
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_get(
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000489 struct inode *inode,
Dave Chinnera9273ca2010-01-20 10:47:48 +1100490 unsigned char *name,
491 unsigned char __user *ubuf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 __uint32_t *len,
493 __uint32_t flags)
494{
Dave Chinnera9273ca2010-01-20 10:47:48 +1100495 unsigned char *kbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 int error = EFAULT;
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 if (*len > XATTR_SIZE_MAX)
499 return EINVAL;
Dave Chinnerad650f52012-03-07 04:50:21 +0000500 kbuf = kmem_zalloc(*len, KM_SLEEP | KM_MAYFAIL);
501 if (!kbuf) {
502 kbuf = kmem_zalloc_large(*len);
503 if (!kbuf)
504 return ENOMEM;
505 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000507 error = xfs_attr_get(XFS_I(inode), name, kbuf, (int *)len, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 if (error)
509 goto out_kfree;
510
511 if (copy_to_user(ubuf, kbuf, *len))
512 error = EFAULT;
513
514 out_kfree:
Dave Chinnerad650f52012-03-07 04:50:21 +0000515 if (is_vmalloc_addr(kbuf))
516 kmem_free_large(kbuf);
517 else
518 kmem_free(kbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 return error;
520}
521
sandeen@sandeen.net28750972008-11-25 21:20:15 -0600522int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523xfs_attrmulti_attr_set(
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000524 struct inode *inode,
Dave Chinnera9273ca2010-01-20 10:47:48 +1100525 unsigned char *name,
526 const unsigned char __user *ubuf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 __uint32_t len,
528 __uint32_t flags)
529{
Dave Chinnera9273ca2010-01-20 10:47:48 +1100530 unsigned char *kbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 int error = EFAULT;
532
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000533 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 return EPERM;
535 if (len > XATTR_SIZE_MAX)
536 return EINVAL;
537
Li Zefan0e639bd2009-04-08 15:08:04 +0800538 kbuf = memdup_user(ubuf, len);
539 if (IS_ERR(kbuf))
540 return PTR_ERR(kbuf);
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000541
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000542 error = xfs_attr_set(XFS_I(inode), name, kbuf, len, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 return error;
545}
546
sandeen@sandeen.net28750972008-11-25 21:20:15 -0600547int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548xfs_attrmulti_attr_remove(
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000549 struct inode *inode,
Dave Chinnera9273ca2010-01-20 10:47:48 +1100550 unsigned char *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 __uint32_t flags)
552{
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000553 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 return EPERM;
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000555 return xfs_attr_remove(XFS_I(inode), name, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556}
557
558STATIC int
559xfs_attrmulti_by_handle(
Dave Hansen42a74f22008-02-15 14:37:46 -0800560 struct file *parfilp,
Christoph Hellwigd296d302009-01-19 02:02:57 +0100561 void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
563 int error;
564 xfs_attr_multiop_t *ops;
565 xfs_fsop_attrmulti_handlereq_t am_hreq;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100566 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 unsigned int i, size;
Dave Chinnera9273ca2010-01-20 10:47:48 +1100568 unsigned char *attr_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
570 if (!capable(CAP_SYS_ADMIN))
571 return -XFS_ERROR(EPERM);
572 if (copy_from_user(&am_hreq, arg, sizeof(xfs_fsop_attrmulti_handlereq_t)))
573 return -XFS_ERROR(EFAULT);
574
Zhitong Wangfda168c2010-03-23 09:51:22 +1100575 /* overflow check */
576 if (am_hreq.opcount >= INT_MAX / sizeof(xfs_attr_multiop_t))
577 return -E2BIG;
578
Christoph Hellwigd296d302009-01-19 02:02:57 +0100579 dentry = xfs_handlereq_to_dentry(parfilp, &am_hreq.hreq);
580 if (IS_ERR(dentry))
581 return PTR_ERR(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
583 error = E2BIG;
Christoph Hellwige182f572008-06-27 13:32:31 +1000584 size = am_hreq.opcount * sizeof(xfs_attr_multiop_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 if (!size || size > 16 * PAGE_SIZE)
Christoph Hellwigd296d302009-01-19 02:02:57 +0100586 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Li Zefan0e639bd2009-04-08 15:08:04 +0800588 ops = memdup_user(am_hreq.ops, size);
589 if (IS_ERR(ops)) {
590 error = PTR_ERR(ops);
Christoph Hellwigd296d302009-01-19 02:02:57 +0100591 goto out_dput;
Li Zefan0e639bd2009-04-08 15:08:04 +0800592 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594 attr_name = kmalloc(MAXNAMELEN, GFP_KERNEL);
595 if (!attr_name)
596 goto out_kfree_ops;
597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 error = 0;
599 for (i = 0; i < am_hreq.opcount; i++) {
Dave Chinnera9273ca2010-01-20 10:47:48 +1100600 ops[i].am_error = strncpy_from_user((char *)attr_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 ops[i].am_attrname, MAXNAMELEN);
602 if (ops[i].am_error == 0 || ops[i].am_error == MAXNAMELEN)
603 error = -ERANGE;
604 if (ops[i].am_error < 0)
605 break;
606
607 switch (ops[i].am_opcode) {
608 case ATTR_OP_GET:
Christoph Hellwigd296d302009-01-19 02:02:57 +0100609 ops[i].am_error = xfs_attrmulti_attr_get(
610 dentry->d_inode, attr_name,
611 ops[i].am_attrvalue, &ops[i].am_length,
612 ops[i].am_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 break;
614 case ATTR_OP_SET:
Al Viroa561be72011-11-23 11:57:51 -0500615 ops[i].am_error = mnt_want_write_file(parfilp);
Dave Hansen42a74f22008-02-15 14:37:46 -0800616 if (ops[i].am_error)
617 break;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100618 ops[i].am_error = xfs_attrmulti_attr_set(
619 dentry->d_inode, attr_name,
620 ops[i].am_attrvalue, ops[i].am_length,
621 ops[i].am_flags);
Al Viro2a79f172011-12-09 08:06:57 -0500622 mnt_drop_write_file(parfilp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 break;
624 case ATTR_OP_REMOVE:
Al Viroa561be72011-11-23 11:57:51 -0500625 ops[i].am_error = mnt_want_write_file(parfilp);
Dave Hansen42a74f22008-02-15 14:37:46 -0800626 if (ops[i].am_error)
627 break;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100628 ops[i].am_error = xfs_attrmulti_attr_remove(
629 dentry->d_inode, attr_name,
630 ops[i].am_flags);
Al Viro2a79f172011-12-09 08:06:57 -0500631 mnt_drop_write_file(parfilp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 break;
633 default:
634 ops[i].am_error = EINVAL;
635 }
636 }
637
638 if (copy_to_user(am_hreq.ops, ops, size))
639 error = XFS_ERROR(EFAULT);
640
641 kfree(attr_name);
642 out_kfree_ops:
643 kfree(ops);
Christoph Hellwigd296d302009-01-19 02:02:57 +0100644 out_dput:
645 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 return -error;
647}
648
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -0600649int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650xfs_ioc_space(
Christoph Hellwig993386c2007-08-28 16:12:30 +1000651 struct xfs_inode *ip,
Alexey Dobriyanf37ea142006-09-28 10:52:04 +1000652 struct inode *inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 struct file *filp,
654 int ioflags,
655 unsigned int cmd,
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -0600656 xfs_flock64_t *bf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 int attr_flags = 0;
659 int error;
660
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -0600661 /*
662 * Only allow the sys admin to reserve space unless
663 * unwritten extents are enabled.
664 */
665 if (!xfs_sb_version_hasextflgbit(&ip->i_mount->m_sb) &&
666 !capable(CAP_SYS_ADMIN))
667 return -XFS_ERROR(EPERM);
668
Alexey Dobriyanf37ea142006-09-28 10:52:04 +1000669 if (inode->i_flags & (S_IMMUTABLE|S_APPEND))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 return -XFS_ERROR(EPERM);
671
Eric Sandeenad4a8ac2005-09-02 16:41:16 +1000672 if (!(filp->f_mode & FMODE_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 return -XFS_ERROR(EBADF);
674
Alexey Dobriyanf37ea142006-09-28 10:52:04 +1000675 if (!S_ISREG(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 return -XFS_ERROR(EINVAL);
677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000679 attr_flags |= XFS_ATTR_NONBLOCK;
Dave Chinner82878892011-03-26 09:13:08 +1100680
681 if (filp->f_flags & O_DSYNC)
682 attr_flags |= XFS_ATTR_SYNC;
683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 if (ioflags & IO_INVIS)
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000685 attr_flags |= XFS_ATTR_DMI;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Jan Karad9457dc2012-06-12 16:20:39 +0200687 error = mnt_want_write_file(filp);
688 if (error)
689 return error;
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -0600690 error = xfs_change_file_space(ip, cmd, bf, filp->f_pos, attr_flags);
Jan Karad9457dc2012-06-12 16:20:39 +0200691 mnt_drop_write_file(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 return -error;
693}
694
695STATIC int
696xfs_ioc_bulkstat(
697 xfs_mount_t *mp,
698 unsigned int cmd,
699 void __user *arg)
700{
701 xfs_fsop_bulkreq_t bulkreq;
702 int count; /* # of records returned */
703 xfs_ino_t inlast; /* last inode number */
704 int done;
705 int error;
706
707 /* done = 1 if there are more stats to get and if bulkstat */
708 /* should be called again (unused here, but used in dmapi) */
709
710 if (!capable(CAP_SYS_ADMIN))
711 return -EPERM;
712
713 if (XFS_FORCED_SHUTDOWN(mp))
714 return -XFS_ERROR(EIO);
715
716 if (copy_from_user(&bulkreq, arg, sizeof(xfs_fsop_bulkreq_t)))
717 return -XFS_ERROR(EFAULT);
718
719 if (copy_from_user(&inlast, bulkreq.lastip, sizeof(__s64)))
720 return -XFS_ERROR(EFAULT);
721
722 if ((count = bulkreq.icount) <= 0)
723 return -XFS_ERROR(EINVAL);
724
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100725 if (bulkreq.ubuffer == NULL)
726 return -XFS_ERROR(EINVAL);
727
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 if (cmd == XFS_IOC_FSINUMBERS)
729 error = xfs_inumbers(mp, &inlast, &count,
Michal Marekfaa63e92007-07-11 11:10:19 +1000730 bulkreq.ubuffer, xfs_inumbers_fmt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 else if (cmd == XFS_IOC_FSBULKSTAT_SINGLE)
732 error = xfs_bulkstat_single(mp, &inlast,
733 bulkreq.ubuffer, &done);
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100734 else /* XFS_IOC_FSBULKSTAT */
Christoph Hellwig7dce11d2010-06-23 18:11:11 +1000735 error = xfs_bulkstat(mp, &inlast, &count, xfs_bulkstat_one,
736 sizeof(xfs_bstat_t), bulkreq.ubuffer,
737 &done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
739 if (error)
740 return -error;
741
742 if (bulkreq.ocount != NULL) {
743 if (copy_to_user(bulkreq.lastip, &inlast,
744 sizeof(xfs_ino_t)))
745 return -XFS_ERROR(EFAULT);
746
747 if (copy_to_user(bulkreq.ocount, &count, sizeof(count)))
748 return -XFS_ERROR(EFAULT);
749 }
750
751 return 0;
752}
753
754STATIC int
755xfs_ioc_fsgeometry_v1(
756 xfs_mount_t *mp,
757 void __user *arg)
758{
Alex Eldereeb20362011-03-01 17:50:00 +0000759 xfs_fsop_geom_t fsgeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 int error;
761
Alex Eldereeb20362011-03-01 17:50:00 +0000762 error = xfs_fs_geometry(mp, &fsgeo, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 if (error)
764 return -error;
765
Alex Eldereeb20362011-03-01 17:50:00 +0000766 /*
767 * Caller should have passed an argument of type
768 * xfs_fsop_geom_v1_t. This is a proper subset of the
769 * xfs_fsop_geom_t that xfs_fs_geometry() fills in.
770 */
771 if (copy_to_user(arg, &fsgeo, sizeof(xfs_fsop_geom_v1_t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 return -XFS_ERROR(EFAULT);
773 return 0;
774}
775
776STATIC int
777xfs_ioc_fsgeometry(
778 xfs_mount_t *mp,
779 void __user *arg)
780{
781 xfs_fsop_geom_t fsgeo;
782 int error;
783
784 error = xfs_fs_geometry(mp, &fsgeo, 4);
785 if (error)
786 return -error;
787
788 if (copy_to_user(arg, &fsgeo, sizeof(fsgeo)))
789 return -XFS_ERROR(EFAULT);
790 return 0;
791}
792
793/*
794 * Linux extended inode flags interface.
795 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
797STATIC unsigned int
798xfs_merge_ioc_xflags(
799 unsigned int flags,
800 unsigned int start)
801{
802 unsigned int xflags = start;
803
Eric Sandeen39058a02007-02-10 18:37:10 +1100804 if (flags & FS_IMMUTABLE_FL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 xflags |= XFS_XFLAG_IMMUTABLE;
806 else
807 xflags &= ~XFS_XFLAG_IMMUTABLE;
Eric Sandeen39058a02007-02-10 18:37:10 +1100808 if (flags & FS_APPEND_FL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 xflags |= XFS_XFLAG_APPEND;
810 else
811 xflags &= ~XFS_XFLAG_APPEND;
Eric Sandeen39058a02007-02-10 18:37:10 +1100812 if (flags & FS_SYNC_FL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 xflags |= XFS_XFLAG_SYNC;
814 else
815 xflags &= ~XFS_XFLAG_SYNC;
Eric Sandeen39058a02007-02-10 18:37:10 +1100816 if (flags & FS_NOATIME_FL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 xflags |= XFS_XFLAG_NOATIME;
818 else
819 xflags &= ~XFS_XFLAG_NOATIME;
Eric Sandeen39058a02007-02-10 18:37:10 +1100820 if (flags & FS_NODUMP_FL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 xflags |= XFS_XFLAG_NODUMP;
822 else
823 xflags &= ~XFS_XFLAG_NODUMP;
824
825 return xflags;
826}
827
828STATIC unsigned int
829xfs_di2lxflags(
830 __uint16_t di_flags)
831{
832 unsigned int flags = 0;
833
834 if (di_flags & XFS_DIFLAG_IMMUTABLE)
Eric Sandeen39058a02007-02-10 18:37:10 +1100835 flags |= FS_IMMUTABLE_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 if (di_flags & XFS_DIFLAG_APPEND)
Eric Sandeen39058a02007-02-10 18:37:10 +1100837 flags |= FS_APPEND_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 if (di_flags & XFS_DIFLAG_SYNC)
Eric Sandeen39058a02007-02-10 18:37:10 +1100839 flags |= FS_SYNC_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 if (di_flags & XFS_DIFLAG_NOATIME)
Eric Sandeen39058a02007-02-10 18:37:10 +1100841 flags |= FS_NOATIME_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 if (di_flags & XFS_DIFLAG_NODUMP)
Eric Sandeen39058a02007-02-10 18:37:10 +1100843 flags |= FS_NODUMP_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 return flags;
845}
846
847STATIC int
Christoph Hellwigc83bfab2007-10-11 17:47:00 +1000848xfs_ioc_fsgetxattr(
849 xfs_inode_t *ip,
850 int attr,
851 void __user *arg)
852{
853 struct fsxattr fa;
854
Dan Rosenberga122eb22010-09-06 18:24:57 -0400855 memset(&fa, 0, sizeof(struct fsxattr));
856
Christoph Hellwigc83bfab2007-10-11 17:47:00 +1000857 xfs_ilock(ip, XFS_ILOCK_SHARED);
858 fa.fsx_xflags = xfs_ip2xflags(ip);
859 fa.fsx_extsize = ip->i_d.di_extsize << ip->i_mount->m_sb.sb_blocklog;
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +0000860 fa.fsx_projid = xfs_get_projid(ip);
Christoph Hellwigc83bfab2007-10-11 17:47:00 +1000861
862 if (attr) {
863 if (ip->i_afp) {
864 if (ip->i_afp->if_flags & XFS_IFEXTENTS)
865 fa.fsx_nextents = ip->i_afp->if_bytes /
866 sizeof(xfs_bmbt_rec_t);
867 else
868 fa.fsx_nextents = ip->i_d.di_anextents;
869 } else
870 fa.fsx_nextents = 0;
871 } else {
872 if (ip->i_df.if_flags & XFS_IFEXTENTS)
873 fa.fsx_nextents = ip->i_df.if_bytes /
874 sizeof(xfs_bmbt_rec_t);
875 else
876 fa.fsx_nextents = ip->i_d.di_nextents;
877 }
878 xfs_iunlock(ip, XFS_ILOCK_SHARED);
879
880 if (copy_to_user(arg, &fa, sizeof(fa)))
881 return -EFAULT;
882 return 0;
883}
884
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000885STATIC void
886xfs_set_diflags(
887 struct xfs_inode *ip,
888 unsigned int xflags)
889{
890 unsigned int di_flags;
891
892 /* can't set PREALLOC this way, just preserve it */
893 di_flags = (ip->i_d.di_flags & XFS_DIFLAG_PREALLOC);
894 if (xflags & XFS_XFLAG_IMMUTABLE)
895 di_flags |= XFS_DIFLAG_IMMUTABLE;
896 if (xflags & XFS_XFLAG_APPEND)
897 di_flags |= XFS_DIFLAG_APPEND;
898 if (xflags & XFS_XFLAG_SYNC)
899 di_flags |= XFS_DIFLAG_SYNC;
900 if (xflags & XFS_XFLAG_NOATIME)
901 di_flags |= XFS_DIFLAG_NOATIME;
902 if (xflags & XFS_XFLAG_NODUMP)
903 di_flags |= XFS_DIFLAG_NODUMP;
904 if (xflags & XFS_XFLAG_PROJINHERIT)
905 di_flags |= XFS_DIFLAG_PROJINHERIT;
906 if (xflags & XFS_XFLAG_NODEFRAG)
907 di_flags |= XFS_DIFLAG_NODEFRAG;
908 if (xflags & XFS_XFLAG_FILESTREAM)
909 di_flags |= XFS_DIFLAG_FILESTREAM;
Al Viroabbede12011-07-26 02:31:30 -0400910 if (S_ISDIR(ip->i_d.di_mode)) {
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000911 if (xflags & XFS_XFLAG_RTINHERIT)
912 di_flags |= XFS_DIFLAG_RTINHERIT;
913 if (xflags & XFS_XFLAG_NOSYMLINKS)
914 di_flags |= XFS_DIFLAG_NOSYMLINKS;
915 if (xflags & XFS_XFLAG_EXTSZINHERIT)
916 di_flags |= XFS_DIFLAG_EXTSZINHERIT;
Al Viroabbede12011-07-26 02:31:30 -0400917 } else if (S_ISREG(ip->i_d.di_mode)) {
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000918 if (xflags & XFS_XFLAG_REALTIME)
919 di_flags |= XFS_DIFLAG_REALTIME;
920 if (xflags & XFS_XFLAG_EXTSIZE)
921 di_flags |= XFS_DIFLAG_EXTSIZE;
922 }
923
924 ip->i_d.di_flags = di_flags;
925}
926
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000927STATIC void
928xfs_diflags_to_linux(
929 struct xfs_inode *ip)
930{
David Chinnere4f75292008-08-13 16:00:45 +1000931 struct inode *inode = VFS_I(ip);
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000932 unsigned int xflags = xfs_ip2xflags(ip);
933
934 if (xflags & XFS_XFLAG_IMMUTABLE)
935 inode->i_flags |= S_IMMUTABLE;
936 else
937 inode->i_flags &= ~S_IMMUTABLE;
938 if (xflags & XFS_XFLAG_APPEND)
939 inode->i_flags |= S_APPEND;
940 else
941 inode->i_flags &= ~S_APPEND;
942 if (xflags & XFS_XFLAG_SYNC)
943 inode->i_flags |= S_SYNC;
944 else
945 inode->i_flags &= ~S_SYNC;
946 if (xflags & XFS_XFLAG_NOATIME)
947 inode->i_flags |= S_NOATIME;
948 else
949 inode->i_flags &= ~S_NOATIME;
950}
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000951
952#define FSX_PROJID 1
953#define FSX_EXTSIZE 2
954#define FSX_XFLAGS 4
955#define FSX_NONBLOCK 8
956
957STATIC int
958xfs_ioctl_setattr(
959 xfs_inode_t *ip,
960 struct fsxattr *fa,
961 int mask)
962{
963 struct xfs_mount *mp = ip->i_mount;
964 struct xfs_trans *tp;
965 unsigned int lock_flags = 0;
Christoph Hellwig7d095252009-06-08 15:33:32 +0200966 struct xfs_dquot *udqp = NULL;
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500967 struct xfs_dquot *pdqp = NULL;
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000968 struct xfs_dquot *olddquot = NULL;
969 int code;
970
Christoph Hellwigcca28fb2010-06-24 11:57:09 +1000971 trace_xfs_ioctl_setattr(ip);
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000972
973 if (mp->m_flags & XFS_MOUNT_RDONLY)
974 return XFS_ERROR(EROFS);
975 if (XFS_FORCED_SHUTDOWN(mp))
976 return XFS_ERROR(EIO);
977
978 /*
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +0000979 * Disallow 32bit project ids when projid32bit feature is not enabled.
Arkadiusz Mi?kiewicz23963e52010-08-26 10:19:43 +0000980 */
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +0000981 if ((mask & FSX_PROJID) && (fa->fsx_projid > (__uint16_t)-1) &&
982 !xfs_sb_version_hasprojid32bit(&ip->i_mount->m_sb))
Arkadiusz Mi?kiewicz23963e52010-08-26 10:19:43 +0000983 return XFS_ERROR(EINVAL);
984
985 /*
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000986 * If disk quotas is on, we make sure that the dquots do exist on disk,
987 * before we start any other transactions. Trying to do this later
988 * is messy. We don't care to take a readlock to look at the ids
989 * in inode here, because we can't hold it across the trans_reserve.
990 * If the IDs do change before we take the ilock, we're covered
991 * because the i_*dquot fields will get updated anyway.
992 */
993 if (XFS_IS_QUOTA_ON(mp) && (mask & FSX_PROJID)) {
Christoph Hellwig7d095252009-06-08 15:33:32 +0200994 code = xfs_qm_vop_dqalloc(ip, ip->i_d.di_uid,
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000995 ip->i_d.di_gid, fa->fsx_projid,
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500996 XFS_QMOPT_PQUOTA, &udqp, NULL, &pdqp);
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000997 if (code)
998 return code;
999 }
1000
1001 /*
1002 * For the other attributes, we acquire the inode lock and
1003 * first do an error checking pass.
1004 */
1005 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
1006 code = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES(mp), 0, 0, 0);
1007 if (code)
1008 goto error_return;
1009
1010 lock_flags = XFS_ILOCK_EXCL;
1011 xfs_ilock(ip, lock_flags);
1012
1013 /*
1014 * CAP_FOWNER overrides the following restrictions:
1015 *
1016 * The user ID of the calling process must be equal
1017 * to the file owner ID, except in cases where the
1018 * CAP_FSETID capability is applicable.
1019 */
David Howells91b7771252008-10-31 15:50:04 +11001020 if (current_fsuid() != ip->i_d.di_uid && !capable(CAP_FOWNER)) {
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001021 code = XFS_ERROR(EPERM);
1022 goto error_return;
1023 }
1024
1025 /*
1026 * Do a quota reservation only if projid is actually going to change.
1027 */
1028 if (mask & FSX_PROJID) {
Christoph Hellwig7d095252009-06-08 15:33:32 +02001029 if (XFS_IS_QUOTA_RUNNING(mp) &&
1030 XFS_IS_PQUOTA_ON(mp) &&
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +00001031 xfs_get_projid(ip) != fa->fsx_projid) {
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001032 ASSERT(tp);
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001033 code = xfs_qm_vop_chown_reserve(tp, ip, udqp, NULL,
1034 pdqp, capable(CAP_FOWNER) ?
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001035 XFS_QMOPT_FORCE_RES : 0);
1036 if (code) /* out of quota */
1037 goto error_return;
1038 }
1039 }
1040
1041 if (mask & FSX_EXTSIZE) {
1042 /*
1043 * Can't change extent size if any extents are allocated.
1044 */
1045 if (ip->i_d.di_nextents &&
1046 ((ip->i_d.di_extsize << mp->m_sb.sb_blocklog) !=
1047 fa->fsx_extsize)) {
1048 code = XFS_ERROR(EINVAL); /* EFBIG? */
1049 goto error_return;
1050 }
1051
1052 /*
1053 * Extent size must be a multiple of the appropriate block
Dave Chinner53158372011-01-27 12:18:18 +11001054 * size, if set at all. It must also be smaller than the
1055 * maximum extent size supported by the filesystem.
1056 *
1057 * Also, for non-realtime files, limit the extent size hint to
1058 * half the size of the AGs in the filesystem so alignment
1059 * doesn't result in extents larger than an AG.
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001060 */
1061 if (fa->fsx_extsize != 0) {
Dave Chinner53158372011-01-27 12:18:18 +11001062 xfs_extlen_t size;
1063 xfs_fsblock_t extsize_fsb;
1064
1065 extsize_fsb = XFS_B_TO_FSB(mp, fa->fsx_extsize);
1066 if (extsize_fsb > MAXEXTLEN) {
1067 code = XFS_ERROR(EINVAL);
1068 goto error_return;
1069 }
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001070
1071 if (XFS_IS_REALTIME_INODE(ip) ||
1072 ((mask & FSX_XFLAGS) &&
1073 (fa->fsx_xflags & XFS_XFLAG_REALTIME))) {
1074 size = mp->m_sb.sb_rextsize <<
1075 mp->m_sb.sb_blocklog;
1076 } else {
1077 size = mp->m_sb.sb_blocksize;
Dave Chinner53158372011-01-27 12:18:18 +11001078 if (extsize_fsb > mp->m_sb.sb_agblocks / 2) {
1079 code = XFS_ERROR(EINVAL);
1080 goto error_return;
1081 }
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001082 }
1083
1084 if (fa->fsx_extsize % size) {
1085 code = XFS_ERROR(EINVAL);
1086 goto error_return;
1087 }
1088 }
1089 }
1090
1091
1092 if (mask & FSX_XFLAGS) {
1093 /*
1094 * Can't change realtime flag if any extents are allocated.
1095 */
1096 if ((ip->i_d.di_nextents || ip->i_delayed_blks) &&
1097 (XFS_IS_REALTIME_INODE(ip)) !=
1098 (fa->fsx_xflags & XFS_XFLAG_REALTIME)) {
1099 code = XFS_ERROR(EINVAL); /* EFBIG? */
1100 goto error_return;
1101 }
1102
1103 /*
1104 * If realtime flag is set then must have realtime data.
1105 */
1106 if ((fa->fsx_xflags & XFS_XFLAG_REALTIME)) {
1107 if ((mp->m_sb.sb_rblocks == 0) ||
1108 (mp->m_sb.sb_rextsize == 0) ||
1109 (ip->i_d.di_extsize % mp->m_sb.sb_rextsize)) {
1110 code = XFS_ERROR(EINVAL);
1111 goto error_return;
1112 }
1113 }
1114
1115 /*
1116 * Can't modify an immutable/append-only file unless
1117 * we have appropriate permission.
1118 */
1119 if ((ip->i_d.di_flags &
1120 (XFS_DIFLAG_IMMUTABLE|XFS_DIFLAG_APPEND) ||
1121 (fa->fsx_xflags &
1122 (XFS_XFLAG_IMMUTABLE | XFS_XFLAG_APPEND))) &&
1123 !capable(CAP_LINUX_IMMUTABLE)) {
1124 code = XFS_ERROR(EPERM);
1125 goto error_return;
1126 }
1127 }
1128
Christoph Hellwigddc34152011-09-19 15:00:54 +00001129 xfs_trans_ijoin(tp, ip, 0);
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001130
1131 /*
1132 * Change file ownership. Must be the owner or privileged.
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001133 */
1134 if (mask & FSX_PROJID) {
1135 /*
1136 * CAP_FSETID overrides the following restrictions:
1137 *
1138 * The set-user-ID and set-group-ID bits of a file will be
1139 * cleared upon successful return from chown()
1140 */
1141 if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
1142 !capable(CAP_FSETID))
1143 ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
1144
1145 /*
1146 * Change the ownerships and register quota modifications
1147 * in the transaction.
1148 */
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +00001149 if (xfs_get_projid(ip) != fa->fsx_projid) {
Christoph Hellwig7d095252009-06-08 15:33:32 +02001150 if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_PQUOTA_ON(mp)) {
1151 olddquot = xfs_qm_vop_chown(tp, ip,
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001152 &ip->i_pdquot, pdqp);
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001153 }
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +00001154 xfs_set_projid(ip, fa->fsx_projid);
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001155
1156 /*
1157 * We may have to rev the inode as well as
1158 * the superblock version number since projids didn't
1159 * exist before DINODE_VERSION_2 and SB_VERSION_NLINK.
1160 */
Christoph Hellwig51ce16d2008-11-28 14:23:39 +11001161 if (ip->i_d.di_version == 1)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001162 xfs_bump_ino_vers2(tp, ip);
1163 }
1164
1165 }
1166
1167 if (mask & FSX_EXTSIZE)
1168 ip->i_d.di_extsize = fa->fsx_extsize >> mp->m_sb.sb_blocklog;
Christoph Hellwigf13fae22008-07-21 16:16:15 +10001169 if (mask & FSX_XFLAGS) {
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001170 xfs_set_diflags(ip, fa->fsx_xflags);
Christoph Hellwigf13fae22008-07-21 16:16:15 +10001171 xfs_diflags_to_linux(ip);
1172 }
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001173
Dave Chinnerdcd79a12010-09-28 12:27:25 +10001174 xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001175 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001176
1177 XFS_STATS_INC(xs_ig_attrchg);
1178
1179 /*
1180 * If this is a synchronous mount, make sure that the
1181 * transaction goes to disk before returning to the user.
1182 * This is slightly sub-optimal in that truncates require
1183 * two sync transactions instead of one for wsync filesystems.
1184 * One for the truncate and one for the timestamps since we
1185 * don't want to change the timestamps unless we're sure the
1186 * truncate worked. Truncates are less than 1% of the laddis
1187 * mix so this probably isn't worth the trouble to optimize.
1188 */
1189 if (mp->m_flags & XFS_MOUNT_WSYNC)
1190 xfs_trans_set_sync(tp);
1191 code = xfs_trans_commit(tp, 0);
1192 xfs_iunlock(ip, lock_flags);
1193
1194 /*
1195 * Release any dquot(s) the inode had kept before chown.
1196 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02001197 xfs_qm_dqrele(olddquot);
1198 xfs_qm_dqrele(udqp);
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001199 xfs_qm_dqrele(pdqp);
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001200
Christoph Hellwig288699f2010-06-23 18:11:15 +10001201 return code;
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001202
1203 error_return:
Christoph Hellwig7d095252009-06-08 15:33:32 +02001204 xfs_qm_dqrele(udqp);
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001205 xfs_qm_dqrele(pdqp);
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001206 xfs_trans_cancel(tp, 0);
1207 if (lock_flags)
1208 xfs_iunlock(ip, lock_flags);
1209 return code;
1210}
1211
Christoph Hellwigc83bfab2007-10-11 17:47:00 +10001212STATIC int
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001213xfs_ioc_fssetxattr(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 xfs_inode_t *ip,
1215 struct file *filp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 void __user *arg)
1217{
1218 struct fsxattr fa;
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001219 unsigned int mask;
Jan Karad9457dc2012-06-12 16:20:39 +02001220 int error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001221
1222 if (copy_from_user(&fa, arg, sizeof(fa)))
1223 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001225 mask = FSX_XFLAGS | FSX_EXTSIZE | FSX_PROJID;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001226 if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001227 mask |= FSX_NONBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
Jan Karad9457dc2012-06-12 16:20:39 +02001229 error = mnt_want_write_file(filp);
1230 if (error)
1231 return error;
1232 error = xfs_ioctl_setattr(ip, &fa, mask);
1233 mnt_drop_write_file(filp);
1234 return -error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001235}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001237STATIC int
1238xfs_ioc_getxflags(
1239 xfs_inode_t *ip,
1240 void __user *arg)
1241{
1242 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001244 flags = xfs_di2lxflags(ip->i_d.di_flags);
1245 if (copy_to_user(arg, &flags, sizeof(flags)))
1246 return -EFAULT;
1247 return 0;
1248}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001250STATIC int
1251xfs_ioc_setxflags(
1252 xfs_inode_t *ip,
1253 struct file *filp,
1254 void __user *arg)
1255{
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001256 struct fsxattr fa;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001257 unsigned int flags;
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001258 unsigned int mask;
Jan Karad9457dc2012-06-12 16:20:39 +02001259 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001261 if (copy_from_user(&flags, arg, sizeof(flags)))
1262 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001264 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
1265 FS_NOATIME_FL | FS_NODUMP_FL | \
1266 FS_SYNC_FL))
1267 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001269 mask = FSX_XFLAGS;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001270 if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001271 mask |= FSX_NONBLOCK;
1272 fa.fsx_xflags = xfs_merge_ioc_xflags(flags, xfs_ip2xflags(ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
Jan Karad9457dc2012-06-12 16:20:39 +02001274 error = mnt_want_write_file(filp);
1275 if (error)
1276 return error;
1277 error = xfs_ioctl_setattr(ip, &fa, mask);
1278 mnt_drop_write_file(filp);
1279 return -error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280}
1281
1282STATIC int
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001283xfs_getbmap_format(void **ap, struct getbmapx *bmv, int *full)
1284{
1285 struct getbmap __user *base = *ap;
1286
1287 /* copy only getbmap portion (not getbmapx) */
1288 if (copy_to_user(base, bmv, sizeof(struct getbmap)))
1289 return XFS_ERROR(EFAULT);
1290
1291 *ap += sizeof(struct getbmap);
1292 return 0;
1293}
1294
1295STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296xfs_ioc_getbmap(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001297 struct xfs_inode *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 int ioflags,
1299 unsigned int cmd,
1300 void __user *arg)
1301{
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001302 struct getbmapx bmx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 int error;
1304
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001305 if (copy_from_user(&bmx, arg, sizeof(struct getbmapx)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 return -XFS_ERROR(EFAULT);
1307
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001308 if (bmx.bmv_count < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 return -XFS_ERROR(EINVAL);
1310
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001311 bmx.bmv_iflags = (cmd == XFS_IOC_GETBMAPA ? BMV_IF_ATTRFORK : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 if (ioflags & IO_INVIS)
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001313 bmx.bmv_iflags |= BMV_IF_NO_DMAPI_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001315 error = xfs_getbmap(ip, &bmx, xfs_getbmap_format,
1316 (struct getbmap *)arg+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 if (error)
1318 return -error;
1319
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001320 /* copy back header - only size of getbmap */
1321 if (copy_to_user(arg, &bmx, sizeof(struct getbmap)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 return -XFS_ERROR(EFAULT);
1323 return 0;
1324}
1325
1326STATIC int
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001327xfs_getbmapx_format(void **ap, struct getbmapx *bmv, int *full)
1328{
1329 struct getbmapx __user *base = *ap;
1330
1331 if (copy_to_user(base, bmv, sizeof(struct getbmapx)))
1332 return XFS_ERROR(EFAULT);
1333
1334 *ap += sizeof(struct getbmapx);
1335 return 0;
1336}
1337
1338STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339xfs_ioc_getbmapx(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001340 struct xfs_inode *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 void __user *arg)
1342{
1343 struct getbmapx bmx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 int error;
1345
1346 if (copy_from_user(&bmx, arg, sizeof(bmx)))
1347 return -XFS_ERROR(EFAULT);
1348
1349 if (bmx.bmv_count < 2)
1350 return -XFS_ERROR(EINVAL);
1351
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001352 if (bmx.bmv_iflags & (~BMV_IF_VALID))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 return -XFS_ERROR(EINVAL);
1354
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001355 error = xfs_getbmap(ip, &bmx, xfs_getbmapx_format,
1356 (struct getbmapx *)arg+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 if (error)
1358 return -error;
1359
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001360 /* copy back header */
1361 if (copy_to_user(arg, &bmx, sizeof(struct getbmapx)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 return -XFS_ERROR(EFAULT);
1363
1364 return 0;
1365}
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001366
Christoph Hellwig4d4be482008-12-09 04:47:33 -05001367/*
1368 * Note: some of the ioctl's return positive numbers as a
1369 * byte count indicating success, such as readlink_by_handle.
1370 * So we don't "sign flip" like most other routines. This means
1371 * true errors need to be returned as a negative value.
1372 */
1373long
1374xfs_file_ioctl(
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001375 struct file *filp,
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001376 unsigned int cmd,
Christoph Hellwig4d4be482008-12-09 04:47:33 -05001377 unsigned long p)
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001378{
Al Viro496ad9a2013-01-23 17:07:38 -05001379 struct inode *inode = file_inode(filp);
Christoph Hellwig4d4be482008-12-09 04:47:33 -05001380 struct xfs_inode *ip = XFS_I(inode);
1381 struct xfs_mount *mp = ip->i_mount;
1382 void __user *arg = (void __user *)p;
1383 int ioflags = 0;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001384 int error;
1385
Christoph Hellwig4d4be482008-12-09 04:47:33 -05001386 if (filp->f_mode & FMODE_NOCMTIME)
1387 ioflags |= IO_INVIS;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001388
Christoph Hellwigcca28fb2010-06-24 11:57:09 +10001389 trace_xfs_file_ioctl(ip);
Christoph Hellwig4d4be482008-12-09 04:47:33 -05001390
1391 switch (cmd) {
Christoph Hellwiga46db602011-01-07 13:02:04 +00001392 case FITRIM:
1393 return xfs_ioc_trim(mp, arg);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001394 case XFS_IOC_ALLOCSP:
1395 case XFS_IOC_FREESP:
1396 case XFS_IOC_RESVSP:
1397 case XFS_IOC_UNRESVSP:
1398 case XFS_IOC_ALLOCSP64:
1399 case XFS_IOC_FREESP64:
1400 case XFS_IOC_RESVSP64:
Dave Chinner44722352010-08-24 12:02:11 +10001401 case XFS_IOC_UNRESVSP64:
1402 case XFS_IOC_ZERO_RANGE: {
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06001403 xfs_flock64_t bf;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001404
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06001405 if (copy_from_user(&bf, arg, sizeof(bf)))
1406 return -XFS_ERROR(EFAULT);
1407 return xfs_ioc_space(ip, inode, filp, ioflags, cmd, &bf);
1408 }
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001409 case XFS_IOC_DIOINFO: {
1410 struct dioattr da;
1411 xfs_buftarg_t *target =
1412 XFS_IS_REALTIME_INODE(ip) ?
1413 mp->m_rtdev_targp : mp->m_ddev_targp;
1414
1415 da.d_mem = da.d_miniosz = 1 << target->bt_sshift;
1416 da.d_maxiosz = INT_MAX & ~(da.d_miniosz - 1);
1417
1418 if (copy_to_user(arg, &da, sizeof(da)))
1419 return -XFS_ERROR(EFAULT);
1420 return 0;
1421 }
1422
1423 case XFS_IOC_FSBULKSTAT_SINGLE:
1424 case XFS_IOC_FSBULKSTAT:
1425 case XFS_IOC_FSINUMBERS:
1426 return xfs_ioc_bulkstat(mp, cmd, arg);
1427
1428 case XFS_IOC_FSGEOMETRY_V1:
1429 return xfs_ioc_fsgeometry_v1(mp, arg);
1430
1431 case XFS_IOC_FSGEOMETRY:
1432 return xfs_ioc_fsgeometry(mp, arg);
1433
1434 case XFS_IOC_GETVERSION:
1435 return put_user(inode->i_generation, (int __user *)arg);
1436
1437 case XFS_IOC_FSGETXATTR:
1438 return xfs_ioc_fsgetxattr(ip, 0, arg);
1439 case XFS_IOC_FSGETXATTRA:
1440 return xfs_ioc_fsgetxattr(ip, 1, arg);
Lachlan McIlroy3b2816b2008-04-18 12:43:35 +10001441 case XFS_IOC_FSSETXATTR:
Lachlan McIlroy65e67f52008-04-18 12:59:45 +10001442 return xfs_ioc_fssetxattr(ip, filp, arg);
1443 case XFS_IOC_GETXFLAGS:
1444 return xfs_ioc_getxflags(ip, arg);
1445 case XFS_IOC_SETXFLAGS:
1446 return xfs_ioc_setxflags(ip, filp, arg);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001447
1448 case XFS_IOC_FSSETDM: {
1449 struct fsdmidata dmi;
1450
1451 if (copy_from_user(&dmi, arg, sizeof(dmi)))
1452 return -XFS_ERROR(EFAULT);
1453
Jan Karad9457dc2012-06-12 16:20:39 +02001454 error = mnt_want_write_file(filp);
1455 if (error)
1456 return error;
1457
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001458 error = xfs_set_dmattrs(ip, dmi.fsd_dmevmask,
1459 dmi.fsd_dmstate);
Jan Karad9457dc2012-06-12 16:20:39 +02001460 mnt_drop_write_file(filp);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001461 return -error;
1462 }
1463
1464 case XFS_IOC_GETBMAP:
1465 case XFS_IOC_GETBMAPA:
1466 return xfs_ioc_getbmap(ip, ioflags, cmd, arg);
1467
1468 case XFS_IOC_GETBMAPX:
1469 return xfs_ioc_getbmapx(ip, arg);
1470
1471 case XFS_IOC_FD_TO_HANDLE:
1472 case XFS_IOC_PATH_TO_HANDLE:
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06001473 case XFS_IOC_PATH_TO_FSHANDLE: {
1474 xfs_fsop_handlereq_t hreq;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001475
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06001476 if (copy_from_user(&hreq, arg, sizeof(hreq)))
1477 return -XFS_ERROR(EFAULT);
1478 return xfs_find_handle(cmd, &hreq);
1479 }
1480 case XFS_IOC_OPEN_BY_HANDLE: {
1481 xfs_fsop_handlereq_t hreq;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001482
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06001483 if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
1484 return -XFS_ERROR(EFAULT);
Christoph Hellwigd296d302009-01-19 02:02:57 +01001485 return xfs_open_by_handle(filp, &hreq);
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06001486 }
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001487 case XFS_IOC_FSSETDM_BY_HANDLE:
Christoph Hellwigd296d302009-01-19 02:02:57 +01001488 return xfs_fssetdm_by_handle(filp, arg);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001489
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06001490 case XFS_IOC_READLINK_BY_HANDLE: {
1491 xfs_fsop_handlereq_t hreq;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001492
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06001493 if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
1494 return -XFS_ERROR(EFAULT);
Christoph Hellwigd296d302009-01-19 02:02:57 +01001495 return xfs_readlink_by_handle(filp, &hreq);
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06001496 }
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001497 case XFS_IOC_ATTRLIST_BY_HANDLE:
Christoph Hellwigd296d302009-01-19 02:02:57 +01001498 return xfs_attrlist_by_handle(filp, arg);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001499
1500 case XFS_IOC_ATTRMULTI_BY_HANDLE:
Christoph Hellwigd296d302009-01-19 02:02:57 +01001501 return xfs_attrmulti_by_handle(filp, arg);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001502
1503 case XFS_IOC_SWAPEXT: {
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06001504 struct xfs_swapext sxp;
1505
1506 if (copy_from_user(&sxp, arg, sizeof(xfs_swapext_t)))
1507 return -XFS_ERROR(EFAULT);
Jan Karad9457dc2012-06-12 16:20:39 +02001508 error = mnt_want_write_file(filp);
1509 if (error)
1510 return error;
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06001511 error = xfs_swapext(&sxp);
Jan Karad9457dc2012-06-12 16:20:39 +02001512 mnt_drop_write_file(filp);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001513 return -error;
1514 }
1515
1516 case XFS_IOC_FSCOUNTS: {
1517 xfs_fsop_counts_t out;
1518
1519 error = xfs_fs_counts(mp, &out);
1520 if (error)
1521 return -error;
1522
1523 if (copy_to_user(arg, &out, sizeof(out)))
1524 return -XFS_ERROR(EFAULT);
1525 return 0;
1526 }
1527
1528 case XFS_IOC_SET_RESBLKS: {
1529 xfs_fsop_resblks_t inout;
1530 __uint64_t in;
1531
1532 if (!capable(CAP_SYS_ADMIN))
1533 return -EPERM;
1534
Eric Sandeend5db0f92010-02-05 22:59:53 +00001535 if (mp->m_flags & XFS_MOUNT_RDONLY)
1536 return -XFS_ERROR(EROFS);
1537
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001538 if (copy_from_user(&inout, arg, sizeof(inout)))
1539 return -XFS_ERROR(EFAULT);
1540
Jan Karad9457dc2012-06-12 16:20:39 +02001541 error = mnt_want_write_file(filp);
1542 if (error)
1543 return error;
1544
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001545 /* input parameter is passed in resblks field of structure */
1546 in = inout.resblks;
1547 error = xfs_reserve_blocks(mp, &in, &inout);
Jan Karad9457dc2012-06-12 16:20:39 +02001548 mnt_drop_write_file(filp);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001549 if (error)
1550 return -error;
1551
1552 if (copy_to_user(arg, &inout, sizeof(inout)))
1553 return -XFS_ERROR(EFAULT);
1554 return 0;
1555 }
1556
1557 case XFS_IOC_GET_RESBLKS: {
1558 xfs_fsop_resblks_t out;
1559
1560 if (!capable(CAP_SYS_ADMIN))
1561 return -EPERM;
1562
1563 error = xfs_reserve_blocks(mp, NULL, &out);
1564 if (error)
1565 return -error;
1566
1567 if (copy_to_user(arg, &out, sizeof(out)))
1568 return -XFS_ERROR(EFAULT);
1569
1570 return 0;
1571 }
1572
1573 case XFS_IOC_FSGROWFSDATA: {
1574 xfs_growfs_data_t in;
1575
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001576 if (copy_from_user(&in, arg, sizeof(in)))
1577 return -XFS_ERROR(EFAULT);
1578
Jan Karad9457dc2012-06-12 16:20:39 +02001579 error = mnt_want_write_file(filp);
1580 if (error)
1581 return error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001582 error = xfs_growfs_data(mp, &in);
Jan Karad9457dc2012-06-12 16:20:39 +02001583 mnt_drop_write_file(filp);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001584 return -error;
1585 }
1586
1587 case XFS_IOC_FSGROWFSLOG: {
1588 xfs_growfs_log_t in;
1589
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001590 if (copy_from_user(&in, arg, sizeof(in)))
1591 return -XFS_ERROR(EFAULT);
1592
Jan Karad9457dc2012-06-12 16:20:39 +02001593 error = mnt_want_write_file(filp);
1594 if (error)
1595 return error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001596 error = xfs_growfs_log(mp, &in);
Jan Karad9457dc2012-06-12 16:20:39 +02001597 mnt_drop_write_file(filp);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001598 return -error;
1599 }
1600
1601 case XFS_IOC_FSGROWFSRT: {
1602 xfs_growfs_rt_t in;
1603
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001604 if (copy_from_user(&in, arg, sizeof(in)))
1605 return -XFS_ERROR(EFAULT);
1606
Jan Karad9457dc2012-06-12 16:20:39 +02001607 error = mnt_want_write_file(filp);
1608 if (error)
1609 return error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001610 error = xfs_growfs_rt(mp, &in);
Jan Karad9457dc2012-06-12 16:20:39 +02001611 mnt_drop_write_file(filp);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001612 return -error;
1613 }
1614
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001615 case XFS_IOC_GOINGDOWN: {
1616 __uint32_t in;
1617
1618 if (!capable(CAP_SYS_ADMIN))
1619 return -EPERM;
1620
1621 if (get_user(in, (__uint32_t __user *)arg))
1622 return -XFS_ERROR(EFAULT);
1623
1624 error = xfs_fs_goingdown(mp, in);
1625 return -error;
1626 }
1627
1628 case XFS_IOC_ERROR_INJECTION: {
1629 xfs_error_injection_t in;
1630
1631 if (!capable(CAP_SYS_ADMIN))
1632 return -EPERM;
1633
1634 if (copy_from_user(&in, arg, sizeof(in)))
1635 return -XFS_ERROR(EFAULT);
1636
1637 error = xfs_errortag_add(in.errtag, mp);
1638 return -error;
1639 }
1640
1641 case XFS_IOC_ERROR_CLEARALL:
1642 if (!capable(CAP_SYS_ADMIN))
1643 return -EPERM;
1644
1645 error = xfs_errortag_clearall(mp, 1);
1646 return -error;
1647
Brian Foster8ca149d2012-11-07 12:21:12 -05001648 case XFS_IOC_FREE_EOFBLOCKS: {
1649 struct xfs_eofblocks eofb;
1650
1651 if (copy_from_user(&eofb, arg, sizeof(eofb)))
1652 return -XFS_ERROR(EFAULT);
1653
1654 if (eofb.eof_version != XFS_EOFBLOCKS_VERSION)
1655 return -XFS_ERROR(EINVAL);
1656
1657 if (eofb.eof_flags & ~XFS_EOF_FLAGS_VALID)
1658 return -XFS_ERROR(EINVAL);
1659
Brian Foster3e3f9f52012-11-07 12:21:13 -05001660 if (memchr_inv(&eofb.pad32, 0, sizeof(eofb.pad32)) ||
1661 memchr_inv(eofb.pad64, 0, sizeof(eofb.pad64)))
Brian Foster8ca149d2012-11-07 12:21:12 -05001662 return -XFS_ERROR(EINVAL);
1663
1664 error = xfs_icache_free_eofblocks(mp, &eofb);
1665 return -error;
1666 }
1667
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001668 default:
1669 return -ENOTTY;
1670 }
1671}