blob: 7c275380d40a2deb1c724518a4ddc6a21eac12a7 [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 Hellwig993386c2007-08-28 16:12:30 +100041#include "xfs_vnodeops.h"
Christoph Hellwiga46db602011-01-07 13:02:04 +000042#include "xfs_discard.h"
Christoph Hellwig25fe55e2008-07-18 17:13:20 +100043#include "xfs_quota.h"
44#include "xfs_inode_item.h"
Christoph Hellwigd296d302009-01-19 02:02:57 +010045#include "xfs_export.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000046#include "xfs_trace.h"
Brian Foster8ca149d2012-11-07 12:21:12 -050047#include "xfs_icache.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
355STATIC int
356xfs_fssetdm_by_handle(
Christoph Hellwigd296d302009-01-19 02:02:57 +0100357 struct file *parfilp,
358 void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
360 int error;
361 struct fsdmidata fsd;
362 xfs_fsop_setdm_handlereq_t dmhreq;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100363 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
365 if (!capable(CAP_MKNOD))
366 return -XFS_ERROR(EPERM);
367 if (copy_from_user(&dmhreq, arg, sizeof(xfs_fsop_setdm_handlereq_t)))
368 return -XFS_ERROR(EFAULT);
369
Jan Karad9457dc2012-06-12 16:20:39 +0200370 error = mnt_want_write_file(parfilp);
371 if (error)
372 return error;
373
Christoph Hellwigd296d302009-01-19 02:02:57 +0100374 dentry = xfs_handlereq_to_dentry(parfilp, &dmhreq.hreq);
Jan Karad9457dc2012-06-12 16:20:39 +0200375 if (IS_ERR(dentry)) {
376 mnt_drop_write_file(parfilp);
Christoph Hellwigd296d302009-01-19 02:02:57 +0100377 return PTR_ERR(dentry);
Jan Karad9457dc2012-06-12 16:20:39 +0200378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Christoph Hellwigd296d302009-01-19 02:02:57 +0100380 if (IS_IMMUTABLE(dentry->d_inode) || IS_APPEND(dentry->d_inode)) {
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000381 error = -XFS_ERROR(EPERM);
382 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 }
384
385 if (copy_from_user(&fsd, dmhreq.data, sizeof(fsd))) {
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000386 error = -XFS_ERROR(EFAULT);
387 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 }
389
Christoph Hellwigd296d302009-01-19 02:02:57 +0100390 error = -xfs_set_dmattrs(XFS_I(dentry->d_inode), fsd.fsd_dmevmask,
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000391 fsd.fsd_dmstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000393 out:
Jan Karad9457dc2012-06-12 16:20:39 +0200394 mnt_drop_write_file(parfilp);
Christoph Hellwigd296d302009-01-19 02:02:57 +0100395 dput(dentry);
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000396 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397}
398
399STATIC int
400xfs_attrlist_by_handle(
Christoph Hellwigd296d302009-01-19 02:02:57 +0100401 struct file *parfilp,
402 void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
Christoph Hellwigd296d302009-01-19 02:02:57 +0100404 int error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 attrlist_cursor_kern_t *cursor;
406 xfs_fsop_attrlist_handlereq_t al_hreq;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100407 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 char *kbuf;
409
410 if (!capable(CAP_SYS_ADMIN))
411 return -XFS_ERROR(EPERM);
412 if (copy_from_user(&al_hreq, arg, sizeof(xfs_fsop_attrlist_handlereq_t)))
413 return -XFS_ERROR(EFAULT);
414 if (al_hreq.buflen > XATTR_LIST_MAX)
415 return -XFS_ERROR(EINVAL);
416
Christoph Hellwig90ad58a2008-06-27 13:32:19 +1000417 /*
418 * Reject flags, only allow namespaces.
419 */
420 if (al_hreq.flags & ~(ATTR_ROOT | ATTR_SECURE))
421 return -XFS_ERROR(EINVAL);
422
Christoph Hellwigd296d302009-01-19 02:02:57 +0100423 dentry = xfs_handlereq_to_dentry(parfilp, &al_hreq.hreq);
424 if (IS_ERR(dentry))
425 return PTR_ERR(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Eric Sandeendd700d92013-04-04 00:05:13 -0500427 kbuf = kmem_zalloc(al_hreq.buflen, KM_SLEEP | KM_MAYFAIL);
428 if (!kbuf) {
429 kbuf = kmem_zalloc_large(al_hreq.buflen);
430 if (!kbuf)
431 goto out_dput;
432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
434 cursor = (attrlist_cursor_kern_t *)&al_hreq.pos;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100435 error = -xfs_attr_list(XFS_I(dentry->d_inode), kbuf, al_hreq.buflen,
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000436 al_hreq.flags, cursor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 if (error)
438 goto out_kfree;
439
440 if (copy_to_user(al_hreq.buffer, kbuf, al_hreq.buflen))
441 error = -EFAULT;
442
443 out_kfree:
Eric Sandeendd700d92013-04-04 00:05:13 -0500444 if (is_vmalloc_addr(kbuf))
445 kmem_free_large(kbuf);
446 else
447 kmem_free(kbuf);
Christoph Hellwigd296d302009-01-19 02:02:57 +0100448 out_dput:
449 dput(dentry);
450 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451}
452
sandeen@sandeen.net28750972008-11-25 21:20:15 -0600453int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454xfs_attrmulti_attr_get(
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000455 struct inode *inode,
Dave Chinnera9273ca2010-01-20 10:47:48 +1100456 unsigned char *name,
457 unsigned char __user *ubuf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 __uint32_t *len,
459 __uint32_t flags)
460{
Dave Chinnera9273ca2010-01-20 10:47:48 +1100461 unsigned char *kbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 int error = EFAULT;
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 if (*len > XATTR_SIZE_MAX)
465 return EINVAL;
Dave Chinnerad650f52012-03-07 04:50:21 +0000466 kbuf = kmem_zalloc(*len, KM_SLEEP | KM_MAYFAIL);
467 if (!kbuf) {
468 kbuf = kmem_zalloc_large(*len);
469 if (!kbuf)
470 return ENOMEM;
471 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000473 error = xfs_attr_get(XFS_I(inode), name, kbuf, (int *)len, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 if (error)
475 goto out_kfree;
476
477 if (copy_to_user(ubuf, kbuf, *len))
478 error = EFAULT;
479
480 out_kfree:
Dave Chinnerad650f52012-03-07 04:50:21 +0000481 if (is_vmalloc_addr(kbuf))
482 kmem_free_large(kbuf);
483 else
484 kmem_free(kbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 return error;
486}
487
sandeen@sandeen.net28750972008-11-25 21:20:15 -0600488int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489xfs_attrmulti_attr_set(
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000490 struct inode *inode,
Dave Chinnera9273ca2010-01-20 10:47:48 +1100491 unsigned char *name,
492 const unsigned char __user *ubuf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 __uint32_t len,
494 __uint32_t flags)
495{
Dave Chinnera9273ca2010-01-20 10:47:48 +1100496 unsigned char *kbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 int error = EFAULT;
498
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000499 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 return EPERM;
501 if (len > XATTR_SIZE_MAX)
502 return EINVAL;
503
Li Zefan0e639bd2009-04-08 15:08:04 +0800504 kbuf = memdup_user(ubuf, len);
505 if (IS_ERR(kbuf))
506 return PTR_ERR(kbuf);
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000507
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000508 error = xfs_attr_set(XFS_I(inode), name, kbuf, len, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 return error;
511}
512
sandeen@sandeen.net28750972008-11-25 21:20:15 -0600513int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514xfs_attrmulti_attr_remove(
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000515 struct inode *inode,
Dave Chinnera9273ca2010-01-20 10:47:48 +1100516 unsigned char *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 __uint32_t flags)
518{
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000519 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 return EPERM;
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000521 return xfs_attr_remove(XFS_I(inode), name, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522}
523
524STATIC int
525xfs_attrmulti_by_handle(
Dave Hansen42a74f22008-02-15 14:37:46 -0800526 struct file *parfilp,
Christoph Hellwigd296d302009-01-19 02:02:57 +0100527 void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
529 int error;
530 xfs_attr_multiop_t *ops;
531 xfs_fsop_attrmulti_handlereq_t am_hreq;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100532 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 unsigned int i, size;
Dave Chinnera9273ca2010-01-20 10:47:48 +1100534 unsigned char *attr_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
536 if (!capable(CAP_SYS_ADMIN))
537 return -XFS_ERROR(EPERM);
538 if (copy_from_user(&am_hreq, arg, sizeof(xfs_fsop_attrmulti_handlereq_t)))
539 return -XFS_ERROR(EFAULT);
540
Zhitong Wangfda168c2010-03-23 09:51:22 +1100541 /* overflow check */
542 if (am_hreq.opcount >= INT_MAX / sizeof(xfs_attr_multiop_t))
543 return -E2BIG;
544
Christoph Hellwigd296d302009-01-19 02:02:57 +0100545 dentry = xfs_handlereq_to_dentry(parfilp, &am_hreq.hreq);
546 if (IS_ERR(dentry))
547 return PTR_ERR(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
549 error = E2BIG;
Christoph Hellwige182f572008-06-27 13:32:31 +1000550 size = am_hreq.opcount * sizeof(xfs_attr_multiop_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 if (!size || size > 16 * PAGE_SIZE)
Christoph Hellwigd296d302009-01-19 02:02:57 +0100552 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Li Zefan0e639bd2009-04-08 15:08:04 +0800554 ops = memdup_user(am_hreq.ops, size);
555 if (IS_ERR(ops)) {
556 error = PTR_ERR(ops);
Christoph Hellwigd296d302009-01-19 02:02:57 +0100557 goto out_dput;
Li Zefan0e639bd2009-04-08 15:08:04 +0800558 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560 attr_name = kmalloc(MAXNAMELEN, GFP_KERNEL);
561 if (!attr_name)
562 goto out_kfree_ops;
563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 error = 0;
565 for (i = 0; i < am_hreq.opcount; i++) {
Dave Chinnera9273ca2010-01-20 10:47:48 +1100566 ops[i].am_error = strncpy_from_user((char *)attr_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 ops[i].am_attrname, MAXNAMELEN);
568 if (ops[i].am_error == 0 || ops[i].am_error == MAXNAMELEN)
569 error = -ERANGE;
570 if (ops[i].am_error < 0)
571 break;
572
573 switch (ops[i].am_opcode) {
574 case ATTR_OP_GET:
Christoph Hellwigd296d302009-01-19 02:02:57 +0100575 ops[i].am_error = xfs_attrmulti_attr_get(
576 dentry->d_inode, attr_name,
577 ops[i].am_attrvalue, &ops[i].am_length,
578 ops[i].am_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 break;
580 case ATTR_OP_SET:
Al Viroa561be72011-11-23 11:57:51 -0500581 ops[i].am_error = mnt_want_write_file(parfilp);
Dave Hansen42a74f22008-02-15 14:37:46 -0800582 if (ops[i].am_error)
583 break;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100584 ops[i].am_error = xfs_attrmulti_attr_set(
585 dentry->d_inode, attr_name,
586 ops[i].am_attrvalue, ops[i].am_length,
587 ops[i].am_flags);
Al Viro2a79f172011-12-09 08:06:57 -0500588 mnt_drop_write_file(parfilp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 break;
590 case ATTR_OP_REMOVE:
Al Viroa561be72011-11-23 11:57:51 -0500591 ops[i].am_error = mnt_want_write_file(parfilp);
Dave Hansen42a74f22008-02-15 14:37:46 -0800592 if (ops[i].am_error)
593 break;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100594 ops[i].am_error = xfs_attrmulti_attr_remove(
595 dentry->d_inode, attr_name,
596 ops[i].am_flags);
Al Viro2a79f172011-12-09 08:06:57 -0500597 mnt_drop_write_file(parfilp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 break;
599 default:
600 ops[i].am_error = EINVAL;
601 }
602 }
603
604 if (copy_to_user(am_hreq.ops, ops, size))
605 error = XFS_ERROR(EFAULT);
606
607 kfree(attr_name);
608 out_kfree_ops:
609 kfree(ops);
Christoph Hellwigd296d302009-01-19 02:02:57 +0100610 out_dput:
611 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 return -error;
613}
614
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -0600615int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616xfs_ioc_space(
Christoph Hellwig993386c2007-08-28 16:12:30 +1000617 struct xfs_inode *ip,
Alexey Dobriyanf37ea142006-09-28 10:52:04 +1000618 struct inode *inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 struct file *filp,
620 int ioflags,
621 unsigned int cmd,
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -0600622 xfs_flock64_t *bf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 int attr_flags = 0;
625 int error;
626
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -0600627 /*
628 * Only allow the sys admin to reserve space unless
629 * unwritten extents are enabled.
630 */
631 if (!xfs_sb_version_hasextflgbit(&ip->i_mount->m_sb) &&
632 !capable(CAP_SYS_ADMIN))
633 return -XFS_ERROR(EPERM);
634
Alexey Dobriyanf37ea142006-09-28 10:52:04 +1000635 if (inode->i_flags & (S_IMMUTABLE|S_APPEND))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 return -XFS_ERROR(EPERM);
637
Eric Sandeenad4a8ac2005-09-02 16:41:16 +1000638 if (!(filp->f_mode & FMODE_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 return -XFS_ERROR(EBADF);
640
Alexey Dobriyanf37ea142006-09-28 10:52:04 +1000641 if (!S_ISREG(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 return -XFS_ERROR(EINVAL);
643
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000645 attr_flags |= XFS_ATTR_NONBLOCK;
Dave Chinner82878892011-03-26 09:13:08 +1100646
647 if (filp->f_flags & O_DSYNC)
648 attr_flags |= XFS_ATTR_SYNC;
649
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 if (ioflags & IO_INVIS)
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000651 attr_flags |= XFS_ATTR_DMI;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Jan Karad9457dc2012-06-12 16:20:39 +0200653 error = mnt_want_write_file(filp);
654 if (error)
655 return error;
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -0600656 error = xfs_change_file_space(ip, cmd, bf, filp->f_pos, attr_flags);
Jan Karad9457dc2012-06-12 16:20:39 +0200657 mnt_drop_write_file(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 return -error;
659}
660
661STATIC int
662xfs_ioc_bulkstat(
663 xfs_mount_t *mp,
664 unsigned int cmd,
665 void __user *arg)
666{
667 xfs_fsop_bulkreq_t bulkreq;
668 int count; /* # of records returned */
669 xfs_ino_t inlast; /* last inode number */
670 int done;
671 int error;
672
673 /* done = 1 if there are more stats to get and if bulkstat */
674 /* should be called again (unused here, but used in dmapi) */
675
676 if (!capable(CAP_SYS_ADMIN))
677 return -EPERM;
678
679 if (XFS_FORCED_SHUTDOWN(mp))
680 return -XFS_ERROR(EIO);
681
682 if (copy_from_user(&bulkreq, arg, sizeof(xfs_fsop_bulkreq_t)))
683 return -XFS_ERROR(EFAULT);
684
685 if (copy_from_user(&inlast, bulkreq.lastip, sizeof(__s64)))
686 return -XFS_ERROR(EFAULT);
687
688 if ((count = bulkreq.icount) <= 0)
689 return -XFS_ERROR(EINVAL);
690
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100691 if (bulkreq.ubuffer == NULL)
692 return -XFS_ERROR(EINVAL);
693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 if (cmd == XFS_IOC_FSINUMBERS)
695 error = xfs_inumbers(mp, &inlast, &count,
Michal Marekfaa63e92007-07-11 11:10:19 +1000696 bulkreq.ubuffer, xfs_inumbers_fmt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 else if (cmd == XFS_IOC_FSBULKSTAT_SINGLE)
698 error = xfs_bulkstat_single(mp, &inlast,
699 bulkreq.ubuffer, &done);
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100700 else /* XFS_IOC_FSBULKSTAT */
Christoph Hellwig7dce11d2010-06-23 18:11:11 +1000701 error = xfs_bulkstat(mp, &inlast, &count, xfs_bulkstat_one,
702 sizeof(xfs_bstat_t), bulkreq.ubuffer,
703 &done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
705 if (error)
706 return -error;
707
708 if (bulkreq.ocount != NULL) {
709 if (copy_to_user(bulkreq.lastip, &inlast,
710 sizeof(xfs_ino_t)))
711 return -XFS_ERROR(EFAULT);
712
713 if (copy_to_user(bulkreq.ocount, &count, sizeof(count)))
714 return -XFS_ERROR(EFAULT);
715 }
716
717 return 0;
718}
719
720STATIC int
721xfs_ioc_fsgeometry_v1(
722 xfs_mount_t *mp,
723 void __user *arg)
724{
Alex Eldereeb20362011-03-01 17:50:00 +0000725 xfs_fsop_geom_t fsgeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 int error;
727
Alex Eldereeb20362011-03-01 17:50:00 +0000728 error = xfs_fs_geometry(mp, &fsgeo, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 if (error)
730 return -error;
731
Alex Eldereeb20362011-03-01 17:50:00 +0000732 /*
733 * Caller should have passed an argument of type
734 * xfs_fsop_geom_v1_t. This is a proper subset of the
735 * xfs_fsop_geom_t that xfs_fs_geometry() fills in.
736 */
737 if (copy_to_user(arg, &fsgeo, sizeof(xfs_fsop_geom_v1_t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 return -XFS_ERROR(EFAULT);
739 return 0;
740}
741
742STATIC int
743xfs_ioc_fsgeometry(
744 xfs_mount_t *mp,
745 void __user *arg)
746{
747 xfs_fsop_geom_t fsgeo;
748 int error;
749
750 error = xfs_fs_geometry(mp, &fsgeo, 4);
751 if (error)
752 return -error;
753
754 if (copy_to_user(arg, &fsgeo, sizeof(fsgeo)))
755 return -XFS_ERROR(EFAULT);
756 return 0;
757}
758
759/*
760 * Linux extended inode flags interface.
761 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
763STATIC unsigned int
764xfs_merge_ioc_xflags(
765 unsigned int flags,
766 unsigned int start)
767{
768 unsigned int xflags = start;
769
Eric Sandeen39058a02007-02-10 18:37:10 +1100770 if (flags & FS_IMMUTABLE_FL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 xflags |= XFS_XFLAG_IMMUTABLE;
772 else
773 xflags &= ~XFS_XFLAG_IMMUTABLE;
Eric Sandeen39058a02007-02-10 18:37:10 +1100774 if (flags & FS_APPEND_FL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 xflags |= XFS_XFLAG_APPEND;
776 else
777 xflags &= ~XFS_XFLAG_APPEND;
Eric Sandeen39058a02007-02-10 18:37:10 +1100778 if (flags & FS_SYNC_FL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 xflags |= XFS_XFLAG_SYNC;
780 else
781 xflags &= ~XFS_XFLAG_SYNC;
Eric Sandeen39058a02007-02-10 18:37:10 +1100782 if (flags & FS_NOATIME_FL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 xflags |= XFS_XFLAG_NOATIME;
784 else
785 xflags &= ~XFS_XFLAG_NOATIME;
Eric Sandeen39058a02007-02-10 18:37:10 +1100786 if (flags & FS_NODUMP_FL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 xflags |= XFS_XFLAG_NODUMP;
788 else
789 xflags &= ~XFS_XFLAG_NODUMP;
790
791 return xflags;
792}
793
794STATIC unsigned int
795xfs_di2lxflags(
796 __uint16_t di_flags)
797{
798 unsigned int flags = 0;
799
800 if (di_flags & XFS_DIFLAG_IMMUTABLE)
Eric Sandeen39058a02007-02-10 18:37:10 +1100801 flags |= FS_IMMUTABLE_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 if (di_flags & XFS_DIFLAG_APPEND)
Eric Sandeen39058a02007-02-10 18:37:10 +1100803 flags |= FS_APPEND_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 if (di_flags & XFS_DIFLAG_SYNC)
Eric Sandeen39058a02007-02-10 18:37:10 +1100805 flags |= FS_SYNC_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 if (di_flags & XFS_DIFLAG_NOATIME)
Eric Sandeen39058a02007-02-10 18:37:10 +1100807 flags |= FS_NOATIME_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 if (di_flags & XFS_DIFLAG_NODUMP)
Eric Sandeen39058a02007-02-10 18:37:10 +1100809 flags |= FS_NODUMP_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 return flags;
811}
812
813STATIC int
Christoph Hellwigc83bfab2007-10-11 17:47:00 +1000814xfs_ioc_fsgetxattr(
815 xfs_inode_t *ip,
816 int attr,
817 void __user *arg)
818{
819 struct fsxattr fa;
820
Dan Rosenberga122eb22010-09-06 18:24:57 -0400821 memset(&fa, 0, sizeof(struct fsxattr));
822
Christoph Hellwigc83bfab2007-10-11 17:47:00 +1000823 xfs_ilock(ip, XFS_ILOCK_SHARED);
824 fa.fsx_xflags = xfs_ip2xflags(ip);
825 fa.fsx_extsize = ip->i_d.di_extsize << ip->i_mount->m_sb.sb_blocklog;
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +0000826 fa.fsx_projid = xfs_get_projid(ip);
Christoph Hellwigc83bfab2007-10-11 17:47:00 +1000827
828 if (attr) {
829 if (ip->i_afp) {
830 if (ip->i_afp->if_flags & XFS_IFEXTENTS)
831 fa.fsx_nextents = ip->i_afp->if_bytes /
832 sizeof(xfs_bmbt_rec_t);
833 else
834 fa.fsx_nextents = ip->i_d.di_anextents;
835 } else
836 fa.fsx_nextents = 0;
837 } else {
838 if (ip->i_df.if_flags & XFS_IFEXTENTS)
839 fa.fsx_nextents = ip->i_df.if_bytes /
840 sizeof(xfs_bmbt_rec_t);
841 else
842 fa.fsx_nextents = ip->i_d.di_nextents;
843 }
844 xfs_iunlock(ip, XFS_ILOCK_SHARED);
845
846 if (copy_to_user(arg, &fa, sizeof(fa)))
847 return -EFAULT;
848 return 0;
849}
850
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000851STATIC void
852xfs_set_diflags(
853 struct xfs_inode *ip,
854 unsigned int xflags)
855{
856 unsigned int di_flags;
857
858 /* can't set PREALLOC this way, just preserve it */
859 di_flags = (ip->i_d.di_flags & XFS_DIFLAG_PREALLOC);
860 if (xflags & XFS_XFLAG_IMMUTABLE)
861 di_flags |= XFS_DIFLAG_IMMUTABLE;
862 if (xflags & XFS_XFLAG_APPEND)
863 di_flags |= XFS_DIFLAG_APPEND;
864 if (xflags & XFS_XFLAG_SYNC)
865 di_flags |= XFS_DIFLAG_SYNC;
866 if (xflags & XFS_XFLAG_NOATIME)
867 di_flags |= XFS_DIFLAG_NOATIME;
868 if (xflags & XFS_XFLAG_NODUMP)
869 di_flags |= XFS_DIFLAG_NODUMP;
870 if (xflags & XFS_XFLAG_PROJINHERIT)
871 di_flags |= XFS_DIFLAG_PROJINHERIT;
872 if (xflags & XFS_XFLAG_NODEFRAG)
873 di_flags |= XFS_DIFLAG_NODEFRAG;
874 if (xflags & XFS_XFLAG_FILESTREAM)
875 di_flags |= XFS_DIFLAG_FILESTREAM;
Al Viroabbede12011-07-26 02:31:30 -0400876 if (S_ISDIR(ip->i_d.di_mode)) {
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000877 if (xflags & XFS_XFLAG_RTINHERIT)
878 di_flags |= XFS_DIFLAG_RTINHERIT;
879 if (xflags & XFS_XFLAG_NOSYMLINKS)
880 di_flags |= XFS_DIFLAG_NOSYMLINKS;
881 if (xflags & XFS_XFLAG_EXTSZINHERIT)
882 di_flags |= XFS_DIFLAG_EXTSZINHERIT;
Al Viroabbede12011-07-26 02:31:30 -0400883 } else if (S_ISREG(ip->i_d.di_mode)) {
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000884 if (xflags & XFS_XFLAG_REALTIME)
885 di_flags |= XFS_DIFLAG_REALTIME;
886 if (xflags & XFS_XFLAG_EXTSIZE)
887 di_flags |= XFS_DIFLAG_EXTSIZE;
888 }
889
890 ip->i_d.di_flags = di_flags;
891}
892
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000893STATIC void
894xfs_diflags_to_linux(
895 struct xfs_inode *ip)
896{
David Chinnere4f75292008-08-13 16:00:45 +1000897 struct inode *inode = VFS_I(ip);
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000898 unsigned int xflags = xfs_ip2xflags(ip);
899
900 if (xflags & XFS_XFLAG_IMMUTABLE)
901 inode->i_flags |= S_IMMUTABLE;
902 else
903 inode->i_flags &= ~S_IMMUTABLE;
904 if (xflags & XFS_XFLAG_APPEND)
905 inode->i_flags |= S_APPEND;
906 else
907 inode->i_flags &= ~S_APPEND;
908 if (xflags & XFS_XFLAG_SYNC)
909 inode->i_flags |= S_SYNC;
910 else
911 inode->i_flags &= ~S_SYNC;
912 if (xflags & XFS_XFLAG_NOATIME)
913 inode->i_flags |= S_NOATIME;
914 else
915 inode->i_flags &= ~S_NOATIME;
916}
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000917
918#define FSX_PROJID 1
919#define FSX_EXTSIZE 2
920#define FSX_XFLAGS 4
921#define FSX_NONBLOCK 8
922
923STATIC int
924xfs_ioctl_setattr(
925 xfs_inode_t *ip,
926 struct fsxattr *fa,
927 int mask)
928{
929 struct xfs_mount *mp = ip->i_mount;
930 struct xfs_trans *tp;
931 unsigned int lock_flags = 0;
Christoph Hellwig7d095252009-06-08 15:33:32 +0200932 struct xfs_dquot *udqp = NULL;
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500933 struct xfs_dquot *pdqp = NULL;
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000934 struct xfs_dquot *olddquot = NULL;
935 int code;
936
Christoph Hellwigcca28fb2010-06-24 11:57:09 +1000937 trace_xfs_ioctl_setattr(ip);
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000938
939 if (mp->m_flags & XFS_MOUNT_RDONLY)
940 return XFS_ERROR(EROFS);
941 if (XFS_FORCED_SHUTDOWN(mp))
942 return XFS_ERROR(EIO);
943
944 /*
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +0000945 * Disallow 32bit project ids when projid32bit feature is not enabled.
Arkadiusz Mi?kiewicz23963e52010-08-26 10:19:43 +0000946 */
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +0000947 if ((mask & FSX_PROJID) && (fa->fsx_projid > (__uint16_t)-1) &&
948 !xfs_sb_version_hasprojid32bit(&ip->i_mount->m_sb))
Arkadiusz Mi?kiewicz23963e52010-08-26 10:19:43 +0000949 return XFS_ERROR(EINVAL);
950
951 /*
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000952 * If disk quotas is on, we make sure that the dquots do exist on disk,
953 * before we start any other transactions. Trying to do this later
954 * is messy. We don't care to take a readlock to look at the ids
955 * in inode here, because we can't hold it across the trans_reserve.
956 * If the IDs do change before we take the ilock, we're covered
957 * because the i_*dquot fields will get updated anyway.
958 */
959 if (XFS_IS_QUOTA_ON(mp) && (mask & FSX_PROJID)) {
Christoph Hellwig7d095252009-06-08 15:33:32 +0200960 code = xfs_qm_vop_dqalloc(ip, ip->i_d.di_uid,
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000961 ip->i_d.di_gid, fa->fsx_projid,
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500962 XFS_QMOPT_PQUOTA, &udqp, NULL, &pdqp);
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000963 if (code)
964 return code;
965 }
966
967 /*
968 * For the other attributes, we acquire the inode lock and
969 * first do an error checking pass.
970 */
971 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
972 code = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES(mp), 0, 0, 0);
973 if (code)
974 goto error_return;
975
976 lock_flags = XFS_ILOCK_EXCL;
977 xfs_ilock(ip, lock_flags);
978
979 /*
980 * CAP_FOWNER overrides the following restrictions:
981 *
982 * The user ID of the calling process must be equal
983 * to the file owner ID, except in cases where the
984 * CAP_FSETID capability is applicable.
985 */
David Howells91b7771252008-10-31 15:50:04 +1100986 if (current_fsuid() != ip->i_d.di_uid && !capable(CAP_FOWNER)) {
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000987 code = XFS_ERROR(EPERM);
988 goto error_return;
989 }
990
991 /*
992 * Do a quota reservation only if projid is actually going to change.
993 */
994 if (mask & FSX_PROJID) {
Christoph Hellwig7d095252009-06-08 15:33:32 +0200995 if (XFS_IS_QUOTA_RUNNING(mp) &&
996 XFS_IS_PQUOTA_ON(mp) &&
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +0000997 xfs_get_projid(ip) != fa->fsx_projid) {
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000998 ASSERT(tp);
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500999 code = xfs_qm_vop_chown_reserve(tp, ip, udqp, NULL,
1000 pdqp, capable(CAP_FOWNER) ?
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001001 XFS_QMOPT_FORCE_RES : 0);
1002 if (code) /* out of quota */
1003 goto error_return;
1004 }
1005 }
1006
1007 if (mask & FSX_EXTSIZE) {
1008 /*
1009 * Can't change extent size if any extents are allocated.
1010 */
1011 if (ip->i_d.di_nextents &&
1012 ((ip->i_d.di_extsize << mp->m_sb.sb_blocklog) !=
1013 fa->fsx_extsize)) {
1014 code = XFS_ERROR(EINVAL); /* EFBIG? */
1015 goto error_return;
1016 }
1017
1018 /*
1019 * Extent size must be a multiple of the appropriate block
Dave Chinner53158372011-01-27 12:18:18 +11001020 * size, if set at all. It must also be smaller than the
1021 * maximum extent size supported by the filesystem.
1022 *
1023 * Also, for non-realtime files, limit the extent size hint to
1024 * half the size of the AGs in the filesystem so alignment
1025 * doesn't result in extents larger than an AG.
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001026 */
1027 if (fa->fsx_extsize != 0) {
Dave Chinner53158372011-01-27 12:18:18 +11001028 xfs_extlen_t size;
1029 xfs_fsblock_t extsize_fsb;
1030
1031 extsize_fsb = XFS_B_TO_FSB(mp, fa->fsx_extsize);
1032 if (extsize_fsb > MAXEXTLEN) {
1033 code = XFS_ERROR(EINVAL);
1034 goto error_return;
1035 }
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001036
1037 if (XFS_IS_REALTIME_INODE(ip) ||
1038 ((mask & FSX_XFLAGS) &&
1039 (fa->fsx_xflags & XFS_XFLAG_REALTIME))) {
1040 size = mp->m_sb.sb_rextsize <<
1041 mp->m_sb.sb_blocklog;
1042 } else {
1043 size = mp->m_sb.sb_blocksize;
Dave Chinner53158372011-01-27 12:18:18 +11001044 if (extsize_fsb > mp->m_sb.sb_agblocks / 2) {
1045 code = XFS_ERROR(EINVAL);
1046 goto error_return;
1047 }
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001048 }
1049
1050 if (fa->fsx_extsize % size) {
1051 code = XFS_ERROR(EINVAL);
1052 goto error_return;
1053 }
1054 }
1055 }
1056
1057
1058 if (mask & FSX_XFLAGS) {
1059 /*
1060 * Can't change realtime flag if any extents are allocated.
1061 */
1062 if ((ip->i_d.di_nextents || ip->i_delayed_blks) &&
1063 (XFS_IS_REALTIME_INODE(ip)) !=
1064 (fa->fsx_xflags & XFS_XFLAG_REALTIME)) {
1065 code = XFS_ERROR(EINVAL); /* EFBIG? */
1066 goto error_return;
1067 }
1068
1069 /*
1070 * If realtime flag is set then must have realtime data.
1071 */
1072 if ((fa->fsx_xflags & XFS_XFLAG_REALTIME)) {
1073 if ((mp->m_sb.sb_rblocks == 0) ||
1074 (mp->m_sb.sb_rextsize == 0) ||
1075 (ip->i_d.di_extsize % mp->m_sb.sb_rextsize)) {
1076 code = XFS_ERROR(EINVAL);
1077 goto error_return;
1078 }
1079 }
1080
1081 /*
1082 * Can't modify an immutable/append-only file unless
1083 * we have appropriate permission.
1084 */
1085 if ((ip->i_d.di_flags &
1086 (XFS_DIFLAG_IMMUTABLE|XFS_DIFLAG_APPEND) ||
1087 (fa->fsx_xflags &
1088 (XFS_XFLAG_IMMUTABLE | XFS_XFLAG_APPEND))) &&
1089 !capable(CAP_LINUX_IMMUTABLE)) {
1090 code = XFS_ERROR(EPERM);
1091 goto error_return;
1092 }
1093 }
1094
Christoph Hellwigddc34152011-09-19 15:00:54 +00001095 xfs_trans_ijoin(tp, ip, 0);
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001096
1097 /*
1098 * Change file ownership. Must be the owner or privileged.
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001099 */
1100 if (mask & FSX_PROJID) {
1101 /*
1102 * CAP_FSETID overrides the following restrictions:
1103 *
1104 * The set-user-ID and set-group-ID bits of a file will be
1105 * cleared upon successful return from chown()
1106 */
1107 if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
1108 !capable(CAP_FSETID))
1109 ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
1110
1111 /*
1112 * Change the ownerships and register quota modifications
1113 * in the transaction.
1114 */
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +00001115 if (xfs_get_projid(ip) != fa->fsx_projid) {
Christoph Hellwig7d095252009-06-08 15:33:32 +02001116 if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_PQUOTA_ON(mp)) {
1117 olddquot = xfs_qm_vop_chown(tp, ip,
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001118 &ip->i_pdquot, pdqp);
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001119 }
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +00001120 xfs_set_projid(ip, fa->fsx_projid);
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001121
1122 /*
1123 * We may have to rev the inode as well as
1124 * the superblock version number since projids didn't
1125 * exist before DINODE_VERSION_2 and SB_VERSION_NLINK.
1126 */
Christoph Hellwig51ce16d2008-11-28 14:23:39 +11001127 if (ip->i_d.di_version == 1)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001128 xfs_bump_ino_vers2(tp, ip);
1129 }
1130
1131 }
1132
1133 if (mask & FSX_EXTSIZE)
1134 ip->i_d.di_extsize = fa->fsx_extsize >> mp->m_sb.sb_blocklog;
Christoph Hellwigf13fae22008-07-21 16:16:15 +10001135 if (mask & FSX_XFLAGS) {
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001136 xfs_set_diflags(ip, fa->fsx_xflags);
Christoph Hellwigf13fae22008-07-21 16:16:15 +10001137 xfs_diflags_to_linux(ip);
1138 }
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001139
Dave Chinnerdcd79a12010-09-28 12:27:25 +10001140 xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001141 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001142
1143 XFS_STATS_INC(xs_ig_attrchg);
1144
1145 /*
1146 * If this is a synchronous mount, make sure that the
1147 * transaction goes to disk before returning to the user.
1148 * This is slightly sub-optimal in that truncates require
1149 * two sync transactions instead of one for wsync filesystems.
1150 * One for the truncate and one for the timestamps since we
1151 * don't want to change the timestamps unless we're sure the
1152 * truncate worked. Truncates are less than 1% of the laddis
1153 * mix so this probably isn't worth the trouble to optimize.
1154 */
1155 if (mp->m_flags & XFS_MOUNT_WSYNC)
1156 xfs_trans_set_sync(tp);
1157 code = xfs_trans_commit(tp, 0);
1158 xfs_iunlock(ip, lock_flags);
1159
1160 /*
1161 * Release any dquot(s) the inode had kept before chown.
1162 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02001163 xfs_qm_dqrele(olddquot);
1164 xfs_qm_dqrele(udqp);
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001165 xfs_qm_dqrele(pdqp);
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001166
Christoph Hellwig288699f2010-06-23 18:11:15 +10001167 return code;
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001168
1169 error_return:
Christoph Hellwig7d095252009-06-08 15:33:32 +02001170 xfs_qm_dqrele(udqp);
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001171 xfs_qm_dqrele(pdqp);
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001172 xfs_trans_cancel(tp, 0);
1173 if (lock_flags)
1174 xfs_iunlock(ip, lock_flags);
1175 return code;
1176}
1177
Christoph Hellwigc83bfab2007-10-11 17:47:00 +10001178STATIC int
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001179xfs_ioc_fssetxattr(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 xfs_inode_t *ip,
1181 struct file *filp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 void __user *arg)
1183{
1184 struct fsxattr fa;
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001185 unsigned int mask;
Jan Karad9457dc2012-06-12 16:20:39 +02001186 int error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001187
1188 if (copy_from_user(&fa, arg, sizeof(fa)))
1189 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001191 mask = FSX_XFLAGS | FSX_EXTSIZE | FSX_PROJID;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001192 if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001193 mask |= FSX_NONBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
Jan Karad9457dc2012-06-12 16:20:39 +02001195 error = mnt_want_write_file(filp);
1196 if (error)
1197 return error;
1198 error = xfs_ioctl_setattr(ip, &fa, mask);
1199 mnt_drop_write_file(filp);
1200 return -error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001201}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001203STATIC int
1204xfs_ioc_getxflags(
1205 xfs_inode_t *ip,
1206 void __user *arg)
1207{
1208 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001210 flags = xfs_di2lxflags(ip->i_d.di_flags);
1211 if (copy_to_user(arg, &flags, sizeof(flags)))
1212 return -EFAULT;
1213 return 0;
1214}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001216STATIC int
1217xfs_ioc_setxflags(
1218 xfs_inode_t *ip,
1219 struct file *filp,
1220 void __user *arg)
1221{
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001222 struct fsxattr fa;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001223 unsigned int flags;
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001224 unsigned int mask;
Jan Karad9457dc2012-06-12 16:20:39 +02001225 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001227 if (copy_from_user(&flags, arg, sizeof(flags)))
1228 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001230 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
1231 FS_NOATIME_FL | FS_NODUMP_FL | \
1232 FS_SYNC_FL))
1233 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001235 mask = FSX_XFLAGS;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001236 if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001237 mask |= FSX_NONBLOCK;
1238 fa.fsx_xflags = xfs_merge_ioc_xflags(flags, xfs_ip2xflags(ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
Jan Karad9457dc2012-06-12 16:20:39 +02001240 error = mnt_want_write_file(filp);
1241 if (error)
1242 return error;
1243 error = xfs_ioctl_setattr(ip, &fa, mask);
1244 mnt_drop_write_file(filp);
1245 return -error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246}
1247
1248STATIC int
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001249xfs_getbmap_format(void **ap, struct getbmapx *bmv, int *full)
1250{
1251 struct getbmap __user *base = *ap;
1252
1253 /* copy only getbmap portion (not getbmapx) */
1254 if (copy_to_user(base, bmv, sizeof(struct getbmap)))
1255 return XFS_ERROR(EFAULT);
1256
1257 *ap += sizeof(struct getbmap);
1258 return 0;
1259}
1260
1261STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262xfs_ioc_getbmap(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001263 struct xfs_inode *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 int ioflags,
1265 unsigned int cmd,
1266 void __user *arg)
1267{
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001268 struct getbmapx bmx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 int error;
1270
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001271 if (copy_from_user(&bmx, arg, sizeof(struct getbmapx)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 return -XFS_ERROR(EFAULT);
1273
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001274 if (bmx.bmv_count < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 return -XFS_ERROR(EINVAL);
1276
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001277 bmx.bmv_iflags = (cmd == XFS_IOC_GETBMAPA ? BMV_IF_ATTRFORK : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 if (ioflags & IO_INVIS)
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001279 bmx.bmv_iflags |= BMV_IF_NO_DMAPI_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001281 error = xfs_getbmap(ip, &bmx, xfs_getbmap_format,
1282 (struct getbmap *)arg+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 if (error)
1284 return -error;
1285
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001286 /* copy back header - only size of getbmap */
1287 if (copy_to_user(arg, &bmx, sizeof(struct getbmap)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 return -XFS_ERROR(EFAULT);
1289 return 0;
1290}
1291
1292STATIC int
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001293xfs_getbmapx_format(void **ap, struct getbmapx *bmv, int *full)
1294{
1295 struct getbmapx __user *base = *ap;
1296
1297 if (copy_to_user(base, bmv, sizeof(struct getbmapx)))
1298 return XFS_ERROR(EFAULT);
1299
1300 *ap += sizeof(struct getbmapx);
1301 return 0;
1302}
1303
1304STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305xfs_ioc_getbmapx(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001306 struct xfs_inode *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 void __user *arg)
1308{
1309 struct getbmapx bmx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 int error;
1311
1312 if (copy_from_user(&bmx, arg, sizeof(bmx)))
1313 return -XFS_ERROR(EFAULT);
1314
1315 if (bmx.bmv_count < 2)
1316 return -XFS_ERROR(EINVAL);
1317
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001318 if (bmx.bmv_iflags & (~BMV_IF_VALID))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 return -XFS_ERROR(EINVAL);
1320
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001321 error = xfs_getbmap(ip, &bmx, xfs_getbmapx_format,
1322 (struct getbmapx *)arg+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 if (error)
1324 return -error;
1325
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001326 /* copy back header */
1327 if (copy_to_user(arg, &bmx, sizeof(struct getbmapx)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 return -XFS_ERROR(EFAULT);
1329
1330 return 0;
1331}
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001332
Christoph Hellwig4d4be482008-12-09 04:47:33 -05001333/*
1334 * Note: some of the ioctl's return positive numbers as a
1335 * byte count indicating success, such as readlink_by_handle.
1336 * So we don't "sign flip" like most other routines. This means
1337 * true errors need to be returned as a negative value.
1338 */
1339long
1340xfs_file_ioctl(
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001341 struct file *filp,
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001342 unsigned int cmd,
Christoph Hellwig4d4be482008-12-09 04:47:33 -05001343 unsigned long p)
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001344{
Al Viro496ad9a2013-01-23 17:07:38 -05001345 struct inode *inode = file_inode(filp);
Christoph Hellwig4d4be482008-12-09 04:47:33 -05001346 struct xfs_inode *ip = XFS_I(inode);
1347 struct xfs_mount *mp = ip->i_mount;
1348 void __user *arg = (void __user *)p;
1349 int ioflags = 0;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001350 int error;
1351
Christoph Hellwig4d4be482008-12-09 04:47:33 -05001352 if (filp->f_mode & FMODE_NOCMTIME)
1353 ioflags |= IO_INVIS;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001354
Christoph Hellwigcca28fb2010-06-24 11:57:09 +10001355 trace_xfs_file_ioctl(ip);
Christoph Hellwig4d4be482008-12-09 04:47:33 -05001356
1357 switch (cmd) {
Christoph Hellwiga46db602011-01-07 13:02:04 +00001358 case FITRIM:
1359 return xfs_ioc_trim(mp, arg);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001360 case XFS_IOC_ALLOCSP:
1361 case XFS_IOC_FREESP:
1362 case XFS_IOC_RESVSP:
1363 case XFS_IOC_UNRESVSP:
1364 case XFS_IOC_ALLOCSP64:
1365 case XFS_IOC_FREESP64:
1366 case XFS_IOC_RESVSP64:
Dave Chinner44722352010-08-24 12:02:11 +10001367 case XFS_IOC_UNRESVSP64:
1368 case XFS_IOC_ZERO_RANGE: {
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06001369 xfs_flock64_t bf;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001370
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06001371 if (copy_from_user(&bf, arg, sizeof(bf)))
1372 return -XFS_ERROR(EFAULT);
1373 return xfs_ioc_space(ip, inode, filp, ioflags, cmd, &bf);
1374 }
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001375 case XFS_IOC_DIOINFO: {
1376 struct dioattr da;
1377 xfs_buftarg_t *target =
1378 XFS_IS_REALTIME_INODE(ip) ?
1379 mp->m_rtdev_targp : mp->m_ddev_targp;
1380
1381 da.d_mem = da.d_miniosz = 1 << target->bt_sshift;
1382 da.d_maxiosz = INT_MAX & ~(da.d_miniosz - 1);
1383
1384 if (copy_to_user(arg, &da, sizeof(da)))
1385 return -XFS_ERROR(EFAULT);
1386 return 0;
1387 }
1388
1389 case XFS_IOC_FSBULKSTAT_SINGLE:
1390 case XFS_IOC_FSBULKSTAT:
1391 case XFS_IOC_FSINUMBERS:
1392 return xfs_ioc_bulkstat(mp, cmd, arg);
1393
1394 case XFS_IOC_FSGEOMETRY_V1:
1395 return xfs_ioc_fsgeometry_v1(mp, arg);
1396
1397 case XFS_IOC_FSGEOMETRY:
1398 return xfs_ioc_fsgeometry(mp, arg);
1399
1400 case XFS_IOC_GETVERSION:
1401 return put_user(inode->i_generation, (int __user *)arg);
1402
1403 case XFS_IOC_FSGETXATTR:
1404 return xfs_ioc_fsgetxattr(ip, 0, arg);
1405 case XFS_IOC_FSGETXATTRA:
1406 return xfs_ioc_fsgetxattr(ip, 1, arg);
Lachlan McIlroy3b2816b2008-04-18 12:43:35 +10001407 case XFS_IOC_FSSETXATTR:
Lachlan McIlroy65e67f52008-04-18 12:59:45 +10001408 return xfs_ioc_fssetxattr(ip, filp, arg);
1409 case XFS_IOC_GETXFLAGS:
1410 return xfs_ioc_getxflags(ip, arg);
1411 case XFS_IOC_SETXFLAGS:
1412 return xfs_ioc_setxflags(ip, filp, arg);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001413
1414 case XFS_IOC_FSSETDM: {
1415 struct fsdmidata dmi;
1416
1417 if (copy_from_user(&dmi, arg, sizeof(dmi)))
1418 return -XFS_ERROR(EFAULT);
1419
Jan Karad9457dc2012-06-12 16:20:39 +02001420 error = mnt_want_write_file(filp);
1421 if (error)
1422 return error;
1423
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001424 error = xfs_set_dmattrs(ip, dmi.fsd_dmevmask,
1425 dmi.fsd_dmstate);
Jan Karad9457dc2012-06-12 16:20:39 +02001426 mnt_drop_write_file(filp);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001427 return -error;
1428 }
1429
1430 case XFS_IOC_GETBMAP:
1431 case XFS_IOC_GETBMAPA:
1432 return xfs_ioc_getbmap(ip, ioflags, cmd, arg);
1433
1434 case XFS_IOC_GETBMAPX:
1435 return xfs_ioc_getbmapx(ip, arg);
1436
1437 case XFS_IOC_FD_TO_HANDLE:
1438 case XFS_IOC_PATH_TO_HANDLE:
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06001439 case XFS_IOC_PATH_TO_FSHANDLE: {
1440 xfs_fsop_handlereq_t hreq;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001441
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06001442 if (copy_from_user(&hreq, arg, sizeof(hreq)))
1443 return -XFS_ERROR(EFAULT);
1444 return xfs_find_handle(cmd, &hreq);
1445 }
1446 case XFS_IOC_OPEN_BY_HANDLE: {
1447 xfs_fsop_handlereq_t hreq;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001448
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06001449 if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
1450 return -XFS_ERROR(EFAULT);
Christoph Hellwigd296d302009-01-19 02:02:57 +01001451 return xfs_open_by_handle(filp, &hreq);
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06001452 }
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001453 case XFS_IOC_FSSETDM_BY_HANDLE:
Christoph Hellwigd296d302009-01-19 02:02:57 +01001454 return xfs_fssetdm_by_handle(filp, arg);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001455
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06001456 case XFS_IOC_READLINK_BY_HANDLE: {
1457 xfs_fsop_handlereq_t hreq;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001458
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06001459 if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
1460 return -XFS_ERROR(EFAULT);
Christoph Hellwigd296d302009-01-19 02:02:57 +01001461 return xfs_readlink_by_handle(filp, &hreq);
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06001462 }
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001463 case XFS_IOC_ATTRLIST_BY_HANDLE:
Christoph Hellwigd296d302009-01-19 02:02:57 +01001464 return xfs_attrlist_by_handle(filp, arg);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001465
1466 case XFS_IOC_ATTRMULTI_BY_HANDLE:
Christoph Hellwigd296d302009-01-19 02:02:57 +01001467 return xfs_attrmulti_by_handle(filp, arg);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001468
1469 case XFS_IOC_SWAPEXT: {
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06001470 struct xfs_swapext sxp;
1471
1472 if (copy_from_user(&sxp, arg, sizeof(xfs_swapext_t)))
1473 return -XFS_ERROR(EFAULT);
Jan Karad9457dc2012-06-12 16:20:39 +02001474 error = mnt_want_write_file(filp);
1475 if (error)
1476 return error;
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06001477 error = xfs_swapext(&sxp);
Jan Karad9457dc2012-06-12 16:20:39 +02001478 mnt_drop_write_file(filp);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001479 return -error;
1480 }
1481
1482 case XFS_IOC_FSCOUNTS: {
1483 xfs_fsop_counts_t out;
1484
1485 error = xfs_fs_counts(mp, &out);
1486 if (error)
1487 return -error;
1488
1489 if (copy_to_user(arg, &out, sizeof(out)))
1490 return -XFS_ERROR(EFAULT);
1491 return 0;
1492 }
1493
1494 case XFS_IOC_SET_RESBLKS: {
1495 xfs_fsop_resblks_t inout;
1496 __uint64_t in;
1497
1498 if (!capable(CAP_SYS_ADMIN))
1499 return -EPERM;
1500
Eric Sandeend5db0f92010-02-05 22:59:53 +00001501 if (mp->m_flags & XFS_MOUNT_RDONLY)
1502 return -XFS_ERROR(EROFS);
1503
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001504 if (copy_from_user(&inout, arg, sizeof(inout)))
1505 return -XFS_ERROR(EFAULT);
1506
Jan Karad9457dc2012-06-12 16:20:39 +02001507 error = mnt_want_write_file(filp);
1508 if (error)
1509 return error;
1510
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001511 /* input parameter is passed in resblks field of structure */
1512 in = inout.resblks;
1513 error = xfs_reserve_blocks(mp, &in, &inout);
Jan Karad9457dc2012-06-12 16:20:39 +02001514 mnt_drop_write_file(filp);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001515 if (error)
1516 return -error;
1517
1518 if (copy_to_user(arg, &inout, sizeof(inout)))
1519 return -XFS_ERROR(EFAULT);
1520 return 0;
1521 }
1522
1523 case XFS_IOC_GET_RESBLKS: {
1524 xfs_fsop_resblks_t out;
1525
1526 if (!capable(CAP_SYS_ADMIN))
1527 return -EPERM;
1528
1529 error = xfs_reserve_blocks(mp, NULL, &out);
1530 if (error)
1531 return -error;
1532
1533 if (copy_to_user(arg, &out, sizeof(out)))
1534 return -XFS_ERROR(EFAULT);
1535
1536 return 0;
1537 }
1538
1539 case XFS_IOC_FSGROWFSDATA: {
1540 xfs_growfs_data_t in;
1541
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001542 if (copy_from_user(&in, arg, sizeof(in)))
1543 return -XFS_ERROR(EFAULT);
1544
Jan Karad9457dc2012-06-12 16:20:39 +02001545 error = mnt_want_write_file(filp);
1546 if (error)
1547 return error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001548 error = xfs_growfs_data(mp, &in);
Jan Karad9457dc2012-06-12 16:20:39 +02001549 mnt_drop_write_file(filp);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001550 return -error;
1551 }
1552
1553 case XFS_IOC_FSGROWFSLOG: {
1554 xfs_growfs_log_t in;
1555
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001556 if (copy_from_user(&in, arg, sizeof(in)))
1557 return -XFS_ERROR(EFAULT);
1558
Jan Karad9457dc2012-06-12 16:20:39 +02001559 error = mnt_want_write_file(filp);
1560 if (error)
1561 return error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001562 error = xfs_growfs_log(mp, &in);
Jan Karad9457dc2012-06-12 16:20:39 +02001563 mnt_drop_write_file(filp);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001564 return -error;
1565 }
1566
1567 case XFS_IOC_FSGROWFSRT: {
1568 xfs_growfs_rt_t in;
1569
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001570 if (copy_from_user(&in, arg, sizeof(in)))
1571 return -XFS_ERROR(EFAULT);
1572
Jan Karad9457dc2012-06-12 16:20:39 +02001573 error = mnt_want_write_file(filp);
1574 if (error)
1575 return error;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001576 error = xfs_growfs_rt(mp, &in);
Jan Karad9457dc2012-06-12 16:20:39 +02001577 mnt_drop_write_file(filp);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001578 return -error;
1579 }
1580
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001581 case XFS_IOC_GOINGDOWN: {
1582 __uint32_t in;
1583
1584 if (!capable(CAP_SYS_ADMIN))
1585 return -EPERM;
1586
1587 if (get_user(in, (__uint32_t __user *)arg))
1588 return -XFS_ERROR(EFAULT);
1589
1590 error = xfs_fs_goingdown(mp, in);
1591 return -error;
1592 }
1593
1594 case XFS_IOC_ERROR_INJECTION: {
1595 xfs_error_injection_t in;
1596
1597 if (!capable(CAP_SYS_ADMIN))
1598 return -EPERM;
1599
1600 if (copy_from_user(&in, arg, sizeof(in)))
1601 return -XFS_ERROR(EFAULT);
1602
1603 error = xfs_errortag_add(in.errtag, mp);
1604 return -error;
1605 }
1606
1607 case XFS_IOC_ERROR_CLEARALL:
1608 if (!capable(CAP_SYS_ADMIN))
1609 return -EPERM;
1610
1611 error = xfs_errortag_clearall(mp, 1);
1612 return -error;
1613
Brian Foster8ca149d2012-11-07 12:21:12 -05001614 case XFS_IOC_FREE_EOFBLOCKS: {
1615 struct xfs_eofblocks eofb;
1616
1617 if (copy_from_user(&eofb, arg, sizeof(eofb)))
1618 return -XFS_ERROR(EFAULT);
1619
1620 if (eofb.eof_version != XFS_EOFBLOCKS_VERSION)
1621 return -XFS_ERROR(EINVAL);
1622
1623 if (eofb.eof_flags & ~XFS_EOF_FLAGS_VALID)
1624 return -XFS_ERROR(EINVAL);
1625
Brian Foster3e3f9f52012-11-07 12:21:13 -05001626 if (memchr_inv(&eofb.pad32, 0, sizeof(eofb.pad32)) ||
1627 memchr_inv(eofb.pad64, 0, sizeof(eofb.pad64)))
Brian Foster8ca149d2012-11-07 12:21:12 -05001628 return -XFS_ERROR(EINVAL);
1629
1630 error = xfs_icache_free_eofblocks(mp, &eofb);
1631 return -error;
1632 }
1633
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001634 default:
1635 return -ENOTTY;
1636 }
1637}