blob: 4ddb86b73c6b537034b09d2632c7d2061432a736 [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"
Nathan Scotta844f452005-11-02 14:38:42 +110020#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110022#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "xfs_trans.h"
24#include "xfs_sb.h"
Nathan Scotta844f452005-11-02 14:38:42 +110025#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "xfs_dir2.h"
27#include "xfs_alloc.h"
28#include "xfs_dmapi.h"
29#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110031#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_ialloc_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110033#include "xfs_attr_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_dir2_sf.h"
35#include "xfs_dinode.h"
36#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110037#include "xfs_btree.h"
38#include "xfs_ialloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include "xfs_rtalloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include "xfs_itable.h"
Nathan Scotta844f452005-11-02 14:38:42 +110041#include "xfs_error.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include "xfs_rw.h"
43#include "xfs_acl.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include "xfs_attr.h"
Nathan Scotta844f452005-11-02 14:38:42 +110045#include "xfs_bmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include "xfs_buf_item.h"
47#include "xfs_utils.h"
48#include "xfs_dfrag.h"
49#include "xfs_fsops.h"
Christoph Hellwig993386c2007-08-28 16:12:30 +100050#include "xfs_vnodeops.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080052#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <linux/dcache.h>
54#include <linux/mount.h>
55#include <linux/namei.h>
56#include <linux/pagemap.h>
57
58/*
59 * xfs_find_handle maps from userspace xfs_fsop_handlereq structure to
60 * a file or fs handle.
61 *
62 * XFS_IOC_PATH_TO_FSHANDLE
63 * returns fs handle for a mount point or path within that mount point
64 * XFS_IOC_FD_TO_HANDLE
65 * returns full handle for a FD opened in user space
66 * XFS_IOC_PATH_TO_HANDLE
67 * returns full handle for a path
68 */
69STATIC int
70xfs_find_handle(
71 unsigned int cmd,
72 void __user *arg)
73{
74 int hsize;
75 xfs_handle_t handle;
76 xfs_fsop_handlereq_t hreq;
77 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79 if (copy_from_user(&hreq, arg, sizeof(hreq)))
80 return -XFS_ERROR(EFAULT);
81
82 memset((char *)&handle, 0, sizeof(handle));
83
84 switch (cmd) {
85 case XFS_IOC_PATH_TO_FSHANDLE:
86 case XFS_IOC_PATH_TO_HANDLE: {
87 struct nameidata nd;
88 int error;
89
90 error = user_path_walk_link((const char __user *)hreq.path, &nd);
91 if (error)
92 return error;
93
Jan Blunck4ac91372008-02-14 19:34:32 -080094 ASSERT(nd.path.dentry);
95 ASSERT(nd.path.dentry->d_inode);
96 inode = igrab(nd.path.dentry->d_inode);
Jan Blunck1d957f92008-02-14 19:34:35 -080097 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 break;
99 }
100
101 case XFS_IOC_FD_TO_HANDLE: {
102 struct file *file;
103
104 file = fget(hreq.fd);
105 if (!file)
106 return -EBADF;
107
Josef "Jeff" Sipeke678fb02006-12-08 02:36:49 -0800108 ASSERT(file->f_path.dentry);
109 ASSERT(file->f_path.dentry->d_inode);
110 inode = igrab(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 fput(file);
112 break;
113 }
114
115 default:
116 ASSERT(0);
117 return -XFS_ERROR(EINVAL);
118 }
119
120 if (inode->i_sb->s_magic != XFS_SB_MAGIC) {
121 /* we're not in XFS anymore, Toto */
122 iput(inode);
123 return -XFS_ERROR(EINVAL);
124 }
125
Christoph Hellwig0432dab2005-09-02 16:46:51 +1000126 switch (inode->i_mode & S_IFMT) {
127 case S_IFREG:
128 case S_IFDIR:
129 case S_IFLNK:
130 break;
131 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 iput(inode);
133 return -XFS_ERROR(EBADF);
134 }
135
136 /* now we can grab the fsid */
Christoph Hellwig0ce4cfd2007-08-30 17:20:53 +1000137 memcpy(&handle.ha_fsid, XFS_I(inode)->i_mount->m_fixedfsid,
Christoph Hellwig2f6f7b32007-08-29 11:44:18 +1000138 sizeof(xfs_fsid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 hsize = sizeof(xfs_fsid_t);
140
141 if (cmd != XFS_IOC_PATH_TO_FSHANDLE) {
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000142 xfs_inode_t *ip = XFS_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 int lock_mode;
144
145 /* need to get access to the xfs_inode to read the generation */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 lock_mode = xfs_ilock_map_shared(ip);
147
148 /* fill in fid section of handle from inode */
Christoph Hellwigc6143912007-09-14 15:22:37 +1000149 handle.ha_fid.fid_len = sizeof(xfs_fid_t) -
150 sizeof(handle.ha_fid.fid_len);
151 handle.ha_fid.fid_pad = 0;
152 handle.ha_fid.fid_gen = ip->i_d.di_gen;
153 handle.ha_fid.fid_ino = ip->i_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155 xfs_iunlock_map_shared(ip, lock_mode);
156
157 hsize = XFS_HSIZE(handle);
158 }
159
160 /* now copy our handle into the user buffer & write out the size */
161 if (copy_to_user(hreq.ohandle, &handle, hsize) ||
162 copy_to_user(hreq.ohandlen, &hsize, sizeof(__s32))) {
163 iput(inode);
164 return -XFS_ERROR(EFAULT);
165 }
166
167 iput(inode);
168 return 0;
169}
170
171
172/*
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000173 * Convert userspace handle data into inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 *
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000175 * We use the fact that all the fsop_handlereq ioctl calls have a data
176 * structure argument whose first component is always a xfs_fsop_handlereq_t,
177 * so we can pass that sub structure into this handy, shared routine.
178 *
179 * If no error, caller must always iput the returned inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 */
181STATIC int
182xfs_vget_fsop_handlereq(
183 xfs_mount_t *mp,
184 struct inode *parinode, /* parent inode pointer */
185 xfs_fsop_handlereq_t *hreq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 struct inode **inode)
187{
188 void __user *hanp;
189 size_t hlen;
190 xfs_fid_t *xfid;
191 xfs_handle_t *handlep;
192 xfs_handle_t handle;
193 xfs_inode_t *ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 xfs_ino_t ino;
195 __u32 igen;
196 int error;
197
198 /*
199 * Only allow handle opens under a directory.
200 */
201 if (!S_ISDIR(parinode->i_mode))
202 return XFS_ERROR(ENOTDIR);
203
204 hanp = hreq->ihandle;
205 hlen = hreq->ihandlen;
206 handlep = &handle;
207
208 if (hlen < sizeof(handlep->ha_fsid) || hlen > sizeof(*handlep))
209 return XFS_ERROR(EINVAL);
210 if (copy_from_user(handlep, hanp, hlen))
211 return XFS_ERROR(EFAULT);
212 if (hlen < sizeof(*handlep))
213 memset(((char *)handlep) + hlen, 0, sizeof(*handlep) - hlen);
214 if (hlen > sizeof(handlep->ha_fsid)) {
Christoph Hellwigc6143912007-09-14 15:22:37 +1000215 if (handlep->ha_fid.fid_len !=
216 (hlen - sizeof(handlep->ha_fsid) -
217 sizeof(handlep->ha_fid.fid_len)) ||
218 handlep->ha_fid.fid_pad)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 return XFS_ERROR(EINVAL);
220 }
221
222 /*
223 * Crack the handle, obtain the inode # & generation #
224 */
225 xfid = (struct xfs_fid *)&handlep->ha_fid;
Christoph Hellwigc6143912007-09-14 15:22:37 +1000226 if (xfid->fid_len == sizeof(*xfid) - sizeof(xfid->fid_len)) {
227 ino = xfid->fid_ino;
228 igen = xfid->fid_gen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 } else {
230 return XFS_ERROR(EINVAL);
231 }
232
233 /*
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000234 * Get the XFS inode, building a Linux inode to go with it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 */
236 error = xfs_iget(mp, NULL, ino, 0, XFS_ILOCK_SHARED, &ip, 0);
237 if (error)
238 return error;
239 if (ip == NULL)
240 return XFS_ERROR(EIO);
241 if (ip->i_d.di_mode == 0 || ip->i_d.di_gen != igen) {
242 xfs_iput_new(ip, XFS_ILOCK_SHARED);
243 return XFS_ERROR(ENOENT);
244 }
245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 xfs_iunlock(ip, XFS_ILOCK_SHARED);
247
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000248 *inode = XFS_ITOV(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 return 0;
250}
251
252STATIC int
253xfs_open_by_handle(
254 xfs_mount_t *mp,
255 void __user *arg,
256 struct file *parfilp,
257 struct inode *parinode)
258{
259 int error;
260 int new_fd;
261 int permflag;
262 struct file *filp;
263 struct inode *inode;
264 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 xfs_fsop_handlereq_t hreq;
266
267 if (!capable(CAP_SYS_ADMIN))
268 return -XFS_ERROR(EPERM);
269 if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
270 return -XFS_ERROR(EFAULT);
271
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000272 error = xfs_vget_fsop_handlereq(mp, parinode, &hreq, &inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 if (error)
274 return -error;
275
276 /* Restrict xfs_open_by_handle to directories & regular files. */
277 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode))) {
278 iput(inode);
279 return -XFS_ERROR(EINVAL);
280 }
281
282#if BITS_PER_LONG != 32
283 hreq.oflags |= O_LARGEFILE;
284#endif
285 /* Put open permission in namei format. */
286 permflag = hreq.oflags;
287 if ((permflag+1) & O_ACCMODE)
288 permflag++;
289 if (permflag & O_TRUNC)
290 permflag |= 2;
291
292 if ((!(permflag & O_APPEND) || (permflag & O_TRUNC)) &&
293 (permflag & FMODE_WRITE) && IS_APPEND(inode)) {
294 iput(inode);
295 return -XFS_ERROR(EPERM);
296 }
297
298 if ((permflag & FMODE_WRITE) && IS_IMMUTABLE(inode)) {
299 iput(inode);
300 return -XFS_ERROR(EACCES);
301 }
302
303 /* Can't write directories. */
304 if ( S_ISDIR(inode->i_mode) && (permflag & FMODE_WRITE)) {
305 iput(inode);
306 return -XFS_ERROR(EISDIR);
307 }
308
309 if ((new_fd = get_unused_fd()) < 0) {
310 iput(inode);
311 return new_fd;
312 }
313
314 dentry = d_alloc_anon(inode);
315 if (dentry == NULL) {
316 iput(inode);
317 put_unused_fd(new_fd);
318 return -XFS_ERROR(ENOMEM);
319 }
320
321 /* Ensure umount returns EBUSY on umounts while this file is open. */
Josef "Jeff" Sipeke678fb02006-12-08 02:36:49 -0800322 mntget(parfilp->f_path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324 /* Create file pointer. */
Josef "Jeff" Sipeke678fb02006-12-08 02:36:49 -0800325 filp = dentry_open(dentry, parfilp->f_path.mnt, hreq.oflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 if (IS_ERR(filp)) {
327 put_unused_fd(new_fd);
328 return -XFS_ERROR(-PTR_ERR(filp));
329 }
Vlad Apostolov2e2e7bb2006-11-11 18:04:47 +1100330 if (inode->i_mode & S_IFREG) {
331 /* invisible operation should not change atime */
332 filp->f_flags |= O_NOATIME;
Nathan Scott3562fd42006-03-14 14:00:35 +1100333 filp->f_op = &xfs_invis_file_operations;
Vlad Apostolov2e2e7bb2006-11-11 18:04:47 +1100334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336 fd_install(new_fd, filp);
337 return new_fd;
338}
339
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000340/*
341 * This is a copy from fs/namei.c:vfs_readlink(), except for removing it's
342 * unused first argument.
343 */
344STATIC int
345do_readlink(
346 char __user *buffer,
347 int buflen,
348 const char *link)
349{
350 int len;
351
352 len = PTR_ERR(link);
353 if (IS_ERR(link))
354 goto out;
355
356 len = strlen(link);
357 if (len > (unsigned) buflen)
358 len = buflen;
359 if (copy_to_user(buffer, link, len))
360 len = -EFAULT;
361 out:
362 return len;
363}
364
365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366STATIC int
367xfs_readlink_by_handle(
368 xfs_mount_t *mp,
369 void __user *arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 struct inode *parinode)
371{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 struct inode *inode;
373 xfs_fsop_handlereq_t hreq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 __u32 olen;
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000375 void *link;
376 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
378 if (!capable(CAP_SYS_ADMIN))
379 return -XFS_ERROR(EPERM);
380 if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
381 return -XFS_ERROR(EFAULT);
382
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000383 error = xfs_vget_fsop_handlereq(mp, parinode, &hreq, &inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 if (error)
385 return -error;
386
387 /* Restrict this handle operation to symlinks only. */
Christoph Hellwig0432dab2005-09-02 16:46:51 +1000388 if (!S_ISLNK(inode->i_mode)) {
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000389 error = -XFS_ERROR(EINVAL);
390 goto out_iput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 }
392
393 if (copy_from_user(&olen, hreq.ohandlen, sizeof(__u32))) {
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000394 error = -XFS_ERROR(EFAULT);
395 goto out_iput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000398 link = kmalloc(MAXPATHLEN+1, GFP_KERNEL);
399 if (!link)
400 goto out_iput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000402 error = -xfs_readlink(XFS_I(inode), link);
Nathan Scott67fcaa72006-06-09 17:00:52 +1000403 if (error)
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000404 goto out_kfree;
405 error = do_readlink(hreq.ohandle, olen, link);
406 if (error)
407 goto out_kfree;
Nathan Scott67fcaa72006-06-09 17:00:52 +1000408
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000409 out_kfree:
410 kfree(link);
411 out_iput:
412 iput(inode);
413 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414}
415
416STATIC int
417xfs_fssetdm_by_handle(
418 xfs_mount_t *mp,
419 void __user *arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 struct inode *parinode)
421{
422 int error;
423 struct fsdmidata fsd;
424 xfs_fsop_setdm_handlereq_t dmhreq;
425 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
427 if (!capable(CAP_MKNOD))
428 return -XFS_ERROR(EPERM);
429 if (copy_from_user(&dmhreq, arg, sizeof(xfs_fsop_setdm_handlereq_t)))
430 return -XFS_ERROR(EFAULT);
431
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000432 error = xfs_vget_fsop_handlereq(mp, parinode, &dmhreq.hreq, &inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 if (error)
434 return -error;
435
436 if (IS_IMMUTABLE(inode) || IS_APPEND(inode)) {
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000437 error = -XFS_ERROR(EPERM);
438 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 }
440
441 if (copy_from_user(&fsd, dmhreq.data, sizeof(fsd))) {
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000442 error = -XFS_ERROR(EFAULT);
443 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 }
445
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000446 error = -xfs_set_dmattrs(XFS_I(inode), fsd.fsd_dmevmask,
447 fsd.fsd_dmstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000449 out:
450 iput(inode);
451 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452}
453
454STATIC int
455xfs_attrlist_by_handle(
456 xfs_mount_t *mp,
457 void __user *arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 struct inode *parinode)
459{
460 int error;
461 attrlist_cursor_kern_t *cursor;
462 xfs_fsop_attrlist_handlereq_t al_hreq;
463 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 char *kbuf;
465
466 if (!capable(CAP_SYS_ADMIN))
467 return -XFS_ERROR(EPERM);
468 if (copy_from_user(&al_hreq, arg, sizeof(xfs_fsop_attrlist_handlereq_t)))
469 return -XFS_ERROR(EFAULT);
470 if (al_hreq.buflen > XATTR_LIST_MAX)
471 return -XFS_ERROR(EINVAL);
472
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000473 error = xfs_vget_fsop_handlereq(mp, parinode, &al_hreq.hreq, &inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 if (error)
475 goto out;
476
477 kbuf = kmalloc(al_hreq.buflen, GFP_KERNEL);
478 if (!kbuf)
479 goto out_vn_rele;
480
481 cursor = (attrlist_cursor_kern_t *)&al_hreq.pos;
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000482 error = xfs_attr_list(XFS_I(inode), kbuf, al_hreq.buflen,
483 al_hreq.flags, cursor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 if (error)
485 goto out_kfree;
486
487 if (copy_to_user(al_hreq.buffer, kbuf, al_hreq.buflen))
488 error = -EFAULT;
489
490 out_kfree:
491 kfree(kbuf);
492 out_vn_rele:
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000493 iput(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 out:
495 return -error;
496}
497
498STATIC int
499xfs_attrmulti_attr_get(
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000500 struct inode *inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 char *name,
502 char __user *ubuf,
503 __uint32_t *len,
504 __uint32_t flags)
505{
506 char *kbuf;
507 int error = EFAULT;
508
509 if (*len > XATTR_SIZE_MAX)
510 return EINVAL;
511 kbuf = kmalloc(*len, GFP_KERNEL);
512 if (!kbuf)
513 return ENOMEM;
514
David Chinnera8272ce2007-11-23 16:28:09 +1100515 error = xfs_attr_get(XFS_I(inode), name, kbuf, (int *)len, flags, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 if (error)
517 goto out_kfree;
518
519 if (copy_to_user(ubuf, kbuf, *len))
520 error = EFAULT;
521
522 out_kfree:
523 kfree(kbuf);
524 return error;
525}
526
527STATIC int
528xfs_attrmulti_attr_set(
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000529 struct inode *inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 char *name,
531 const char __user *ubuf,
532 __uint32_t len,
533 __uint32_t flags)
534{
535 char *kbuf;
536 int error = EFAULT;
537
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000538 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 return EPERM;
540 if (len > XATTR_SIZE_MAX)
541 return EINVAL;
542
543 kbuf = kmalloc(len, GFP_KERNEL);
544 if (!kbuf)
545 return ENOMEM;
546
547 if (copy_from_user(kbuf, ubuf, len))
548 goto out_kfree;
549
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000550 error = xfs_attr_set(XFS_I(inode), name, kbuf, len, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
552 out_kfree:
553 kfree(kbuf);
554 return error;
555}
556
557STATIC int
558xfs_attrmulti_attr_remove(
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000559 struct inode *inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 char *name,
561 __uint32_t flags)
562{
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000563 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 return EPERM;
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000565 return xfs_attr_remove(XFS_I(inode), name, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566}
567
568STATIC int
569xfs_attrmulti_by_handle(
570 xfs_mount_t *mp,
571 void __user *arg,
Dave Hansen42a74f22008-02-15 14:37:46 -0800572 struct file *parfilp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 struct inode *parinode)
574{
575 int error;
576 xfs_attr_multiop_t *ops;
577 xfs_fsop_attrmulti_handlereq_t am_hreq;
578 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 unsigned int i, size;
580 char *attr_name;
581
582 if (!capable(CAP_SYS_ADMIN))
583 return -XFS_ERROR(EPERM);
584 if (copy_from_user(&am_hreq, arg, sizeof(xfs_fsop_attrmulti_handlereq_t)))
585 return -XFS_ERROR(EFAULT);
586
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000587 error = xfs_vget_fsop_handlereq(mp, parinode, &am_hreq.hreq, &inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 if (error)
589 goto out;
590
591 error = E2BIG;
592 size = am_hreq.opcount * sizeof(attr_multiop_t);
593 if (!size || size > 16 * PAGE_SIZE)
594 goto out_vn_rele;
595
596 error = ENOMEM;
597 ops = kmalloc(size, GFP_KERNEL);
598 if (!ops)
599 goto out_vn_rele;
600
601 error = EFAULT;
602 if (copy_from_user(ops, am_hreq.ops, size))
603 goto out_kfree_ops;
604
605 attr_name = kmalloc(MAXNAMELEN, GFP_KERNEL);
606 if (!attr_name)
607 goto out_kfree_ops;
608
609
610 error = 0;
611 for (i = 0; i < am_hreq.opcount; i++) {
612 ops[i].am_error = strncpy_from_user(attr_name,
613 ops[i].am_attrname, MAXNAMELEN);
614 if (ops[i].am_error == 0 || ops[i].am_error == MAXNAMELEN)
615 error = -ERANGE;
616 if (ops[i].am_error < 0)
617 break;
618
619 switch (ops[i].am_opcode) {
620 case ATTR_OP_GET:
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000621 ops[i].am_error = xfs_attrmulti_attr_get(inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 attr_name, ops[i].am_attrvalue,
623 &ops[i].am_length, ops[i].am_flags);
624 break;
625 case ATTR_OP_SET:
Dave Hansen42a74f22008-02-15 14:37:46 -0800626 ops[i].am_error = mnt_want_write(parfilp->f_path.mnt);
627 if (ops[i].am_error)
628 break;
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000629 ops[i].am_error = xfs_attrmulti_attr_set(inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 attr_name, ops[i].am_attrvalue,
631 ops[i].am_length, ops[i].am_flags);
Dave Hansen42a74f22008-02-15 14:37:46 -0800632 mnt_drop_write(parfilp->f_path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 break;
634 case ATTR_OP_REMOVE:
Dave Hansen42a74f22008-02-15 14:37:46 -0800635 ops[i].am_error = mnt_want_write(parfilp->f_path.mnt);
636 if (ops[i].am_error)
637 break;
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000638 ops[i].am_error = xfs_attrmulti_attr_remove(inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 attr_name, ops[i].am_flags);
Dave Hansen42a74f22008-02-15 14:37:46 -0800640 mnt_drop_write(parfilp->f_path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 break;
642 default:
643 ops[i].am_error = EINVAL;
644 }
645 }
646
647 if (copy_to_user(am_hreq.ops, ops, size))
648 error = XFS_ERROR(EFAULT);
649
650 kfree(attr_name);
651 out_kfree_ops:
652 kfree(ops);
653 out_vn_rele:
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000654 iput(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 out:
656 return -error;
657}
658
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659STATIC int
660xfs_ioc_space(
Christoph Hellwig993386c2007-08-28 16:12:30 +1000661 struct xfs_inode *ip,
Alexey Dobriyanf37ea142006-09-28 10:52:04 +1000662 struct inode *inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 struct file *filp,
664 int ioflags,
665 unsigned int cmd,
666 void __user *arg)
667{
668 xfs_flock64_t bf;
669 int attr_flags = 0;
670 int error;
671
Alexey Dobriyanf37ea142006-09-28 10:52:04 +1000672 if (inode->i_flags & (S_IMMUTABLE|S_APPEND))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 return -XFS_ERROR(EPERM);
674
Eric Sandeenad4a8ac2005-09-02 16:41:16 +1000675 if (!(filp->f_mode & FMODE_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 return -XFS_ERROR(EBADF);
677
Alexey Dobriyanf37ea142006-09-28 10:52:04 +1000678 if (!S_ISREG(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 return -XFS_ERROR(EINVAL);
680
681 if (copy_from_user(&bf, arg, sizeof(bf)))
682 return -XFS_ERROR(EFAULT);
683
684 if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
685 attr_flags |= ATTR_NONBLOCK;
686 if (ioflags & IO_INVIS)
687 attr_flags |= ATTR_DMI;
688
Christoph Hellwig993386c2007-08-28 16:12:30 +1000689 error = xfs_change_file_space(ip, cmd, &bf, filp->f_pos,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 NULL, attr_flags);
691 return -error;
692}
693
694STATIC int
695xfs_ioc_bulkstat(
696 xfs_mount_t *mp,
697 unsigned int cmd,
698 void __user *arg)
699{
700 xfs_fsop_bulkreq_t bulkreq;
701 int count; /* # of records returned */
702 xfs_ino_t inlast; /* last inode number */
703 int done;
704 int error;
705
706 /* done = 1 if there are more stats to get and if bulkstat */
707 /* should be called again (unused here, but used in dmapi) */
708
709 if (!capable(CAP_SYS_ADMIN))
710 return -EPERM;
711
712 if (XFS_FORCED_SHUTDOWN(mp))
713 return -XFS_ERROR(EIO);
714
715 if (copy_from_user(&bulkreq, arg, sizeof(xfs_fsop_bulkreq_t)))
716 return -XFS_ERROR(EFAULT);
717
718 if (copy_from_user(&inlast, bulkreq.lastip, sizeof(__s64)))
719 return -XFS_ERROR(EFAULT);
720
721 if ((count = bulkreq.icount) <= 0)
722 return -XFS_ERROR(EINVAL);
723
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100724 if (bulkreq.ubuffer == NULL)
725 return -XFS_ERROR(EINVAL);
726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 if (cmd == XFS_IOC_FSINUMBERS)
728 error = xfs_inumbers(mp, &inlast, &count,
Michal Marekfaa63e92007-07-11 11:10:19 +1000729 bulkreq.ubuffer, xfs_inumbers_fmt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 else if (cmd == XFS_IOC_FSBULKSTAT_SINGLE)
731 error = xfs_bulkstat_single(mp, &inlast,
732 bulkreq.ubuffer, &done);
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100733 else /* XFS_IOC_FSBULKSTAT */
734 error = xfs_bulkstat(mp, &inlast, &count,
735 (bulkstat_one_pf)xfs_bulkstat_one, NULL,
736 sizeof(xfs_bstat_t), bulkreq.ubuffer,
737 BULKSTAT_FG_QUICK, &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{
759 xfs_fsop_geom_v1_t fsgeo;
760 int error;
761
762 error = xfs_fs_geometry(mp, (xfs_fsop_geom_t *)&fsgeo, 3);
763 if (error)
764 return -error;
765
766 if (copy_to_user(arg, &fsgeo, sizeof(fsgeo)))
767 return -XFS_ERROR(EFAULT);
768 return 0;
769}
770
771STATIC int
772xfs_ioc_fsgeometry(
773 xfs_mount_t *mp,
774 void __user *arg)
775{
776 xfs_fsop_geom_t fsgeo;
777 int error;
778
779 error = xfs_fs_geometry(mp, &fsgeo, 4);
780 if (error)
781 return -error;
782
783 if (copy_to_user(arg, &fsgeo, sizeof(fsgeo)))
784 return -XFS_ERROR(EFAULT);
785 return 0;
786}
787
788/*
789 * Linux extended inode flags interface.
790 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
792STATIC unsigned int
793xfs_merge_ioc_xflags(
794 unsigned int flags,
795 unsigned int start)
796{
797 unsigned int xflags = start;
798
Eric Sandeen39058a02007-02-10 18:37:10 +1100799 if (flags & FS_IMMUTABLE_FL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 xflags |= XFS_XFLAG_IMMUTABLE;
801 else
802 xflags &= ~XFS_XFLAG_IMMUTABLE;
Eric Sandeen39058a02007-02-10 18:37:10 +1100803 if (flags & FS_APPEND_FL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 xflags |= XFS_XFLAG_APPEND;
805 else
806 xflags &= ~XFS_XFLAG_APPEND;
Eric Sandeen39058a02007-02-10 18:37:10 +1100807 if (flags & FS_SYNC_FL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 xflags |= XFS_XFLAG_SYNC;
809 else
810 xflags &= ~XFS_XFLAG_SYNC;
Eric Sandeen39058a02007-02-10 18:37:10 +1100811 if (flags & FS_NOATIME_FL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 xflags |= XFS_XFLAG_NOATIME;
813 else
814 xflags &= ~XFS_XFLAG_NOATIME;
Eric Sandeen39058a02007-02-10 18:37:10 +1100815 if (flags & FS_NODUMP_FL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 xflags |= XFS_XFLAG_NODUMP;
817 else
818 xflags &= ~XFS_XFLAG_NODUMP;
819
820 return xflags;
821}
822
823STATIC unsigned int
824xfs_di2lxflags(
825 __uint16_t di_flags)
826{
827 unsigned int flags = 0;
828
829 if (di_flags & XFS_DIFLAG_IMMUTABLE)
Eric Sandeen39058a02007-02-10 18:37:10 +1100830 flags |= FS_IMMUTABLE_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 if (di_flags & XFS_DIFLAG_APPEND)
Eric Sandeen39058a02007-02-10 18:37:10 +1100832 flags |= FS_APPEND_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 if (di_flags & XFS_DIFLAG_SYNC)
Eric Sandeen39058a02007-02-10 18:37:10 +1100834 flags |= FS_SYNC_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 if (di_flags & XFS_DIFLAG_NOATIME)
Eric Sandeen39058a02007-02-10 18:37:10 +1100836 flags |= FS_NOATIME_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 if (di_flags & XFS_DIFLAG_NODUMP)
Eric Sandeen39058a02007-02-10 18:37:10 +1100838 flags |= FS_NODUMP_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 return flags;
840}
841
842STATIC int
Christoph Hellwigc83bfab2007-10-11 17:47:00 +1000843xfs_ioc_fsgetxattr(
844 xfs_inode_t *ip,
845 int attr,
846 void __user *arg)
847{
848 struct fsxattr fa;
849
850 xfs_ilock(ip, XFS_ILOCK_SHARED);
851 fa.fsx_xflags = xfs_ip2xflags(ip);
852 fa.fsx_extsize = ip->i_d.di_extsize << ip->i_mount->m_sb.sb_blocklog;
853 fa.fsx_projid = ip->i_d.di_projid;
854
855 if (attr) {
856 if (ip->i_afp) {
857 if (ip->i_afp->if_flags & XFS_IFEXTENTS)
858 fa.fsx_nextents = ip->i_afp->if_bytes /
859 sizeof(xfs_bmbt_rec_t);
860 else
861 fa.fsx_nextents = ip->i_d.di_anextents;
862 } else
863 fa.fsx_nextents = 0;
864 } else {
865 if (ip->i_df.if_flags & XFS_IFEXTENTS)
866 fa.fsx_nextents = ip->i_df.if_bytes /
867 sizeof(xfs_bmbt_rec_t);
868 else
869 fa.fsx_nextents = ip->i_d.di_nextents;
870 }
871 xfs_iunlock(ip, XFS_ILOCK_SHARED);
872
873 if (copy_to_user(arg, &fa, sizeof(fa)))
874 return -EFAULT;
875 return 0;
876}
877
878STATIC int
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +1000879xfs_ioc_fssetxattr(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 xfs_inode_t *ip,
881 struct file *filp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 void __user *arg)
883{
884 struct fsxattr fa;
Nathan Scott8285fb52006-06-09 17:07:12 +1000885 struct bhv_vattr *vattr;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +1000886 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 int attr_flags;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +1000888
889 if (copy_from_user(&fa, arg, sizeof(fa)))
890 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
Nathan Scott220b5282006-03-14 13:33:36 +1100892 vattr = kmalloc(sizeof(*vattr), GFP_KERNEL);
893 if (unlikely(!vattr))
894 return -ENOMEM;
895
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +1000896 attr_flags = 0;
897 if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
898 attr_flags |= ATTR_NONBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +1000900 vattr->va_mask = XFS_AT_XFLAGS | XFS_AT_EXTSIZE | XFS_AT_PROJID;
901 vattr->va_xflags = fa.fsx_xflags;
902 vattr->va_extsize = fa.fsx_extsize;
903 vattr->va_projid = fa.fsx_projid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +1000905 error = -xfs_setattr(ip, vattr, attr_flags, NULL);
906 if (!error)
907 vn_revalidate(XFS_ITOV(ip)); /* update flags */
908 kfree(vattr);
909 return 0;
910}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +1000912STATIC int
913xfs_ioc_getxflags(
914 xfs_inode_t *ip,
915 void __user *arg)
916{
917 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +1000919 flags = xfs_di2lxflags(ip->i_d.di_flags);
920 if (copy_to_user(arg, &flags, sizeof(flags)))
921 return -EFAULT;
922 return 0;
923}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +1000925STATIC int
926xfs_ioc_setxflags(
927 xfs_inode_t *ip,
928 struct file *filp,
929 void __user *arg)
930{
931 struct bhv_vattr *vattr;
932 unsigned int flags;
933 int attr_flags;
934 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +1000936 if (copy_from_user(&flags, arg, sizeof(flags)))
937 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +1000939 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
940 FS_NOATIME_FL | FS_NODUMP_FL | \
941 FS_SYNC_FL))
942 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +1000944 vattr = kmalloc(sizeof(*vattr), GFP_KERNEL);
945 if (unlikely(!vattr))
946 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +1000948 attr_flags = 0;
949 if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
950 attr_flags |= ATTR_NONBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +1000952 vattr->va_mask = XFS_AT_XFLAGS;
953 vattr->va_xflags = xfs_merge_ioc_xflags(flags, xfs_ip2xflags(ip));
Nathan Scott220b5282006-03-14 13:33:36 +1100954
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +1000955 error = -xfs_setattr(ip, vattr, attr_flags, NULL);
956 if (likely(!error))
957 vn_revalidate(XFS_ITOV(ip)); /* update flags */
Nathan Scott220b5282006-03-14 13:33:36 +1100958 kfree(vattr);
959 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960}
961
962STATIC int
963xfs_ioc_getbmap(
Christoph Hellwig993386c2007-08-28 16:12:30 +1000964 struct xfs_inode *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 int ioflags,
966 unsigned int cmd,
967 void __user *arg)
968{
969 struct getbmap bm;
970 int iflags;
971 int error;
972
973 if (copy_from_user(&bm, arg, sizeof(bm)))
974 return -XFS_ERROR(EFAULT);
975
976 if (bm.bmv_count < 2)
977 return -XFS_ERROR(EINVAL);
978
979 iflags = (cmd == XFS_IOC_GETBMAPA ? BMV_IF_ATTRFORK : 0);
980 if (ioflags & IO_INVIS)
981 iflags |= BMV_IF_NO_DMAPI_READ;
982
Christoph Hellwig993386c2007-08-28 16:12:30 +1000983 error = xfs_getbmap(ip, &bm, (struct getbmap __user *)arg+1, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 if (error)
985 return -error;
986
987 if (copy_to_user(arg, &bm, sizeof(bm)))
988 return -XFS_ERROR(EFAULT);
989 return 0;
990}
991
992STATIC int
993xfs_ioc_getbmapx(
Christoph Hellwig993386c2007-08-28 16:12:30 +1000994 struct xfs_inode *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 void __user *arg)
996{
997 struct getbmapx bmx;
998 struct getbmap bm;
999 int iflags;
1000 int error;
1001
1002 if (copy_from_user(&bmx, arg, sizeof(bmx)))
1003 return -XFS_ERROR(EFAULT);
1004
1005 if (bmx.bmv_count < 2)
1006 return -XFS_ERROR(EINVAL);
1007
1008 /*
1009 * Map input getbmapx structure to a getbmap
1010 * structure for xfs_getbmap.
1011 */
1012 GETBMAP_CONVERT(bmx, bm);
1013
1014 iflags = bmx.bmv_iflags;
1015
1016 if (iflags & (~BMV_IF_VALID))
1017 return -XFS_ERROR(EINVAL);
1018
1019 iflags |= BMV_IF_EXTENDED;
1020
Christoph Hellwig993386c2007-08-28 16:12:30 +10001021 error = xfs_getbmap(ip, &bm, (struct getbmapx __user *)arg+1, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 if (error)
1023 return -error;
1024
1025 GETBMAP_CONVERT(bm, bmx);
1026
1027 if (copy_to_user(arg, &bmx, sizeof(bmx)))
1028 return -XFS_ERROR(EFAULT);
1029
1030 return 0;
1031}
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001032
1033int
1034xfs_ioctl(
1035 xfs_inode_t *ip,
1036 struct file *filp,
1037 int ioflags,
1038 unsigned int cmd,
1039 void __user *arg)
1040{
1041 struct inode *inode = filp->f_path.dentry->d_inode;
1042 xfs_mount_t *mp = ip->i_mount;
1043 int error;
1044
1045 xfs_itrace_entry(XFS_I(inode));
1046 switch (cmd) {
1047
1048 case XFS_IOC_ALLOCSP:
1049 case XFS_IOC_FREESP:
1050 case XFS_IOC_RESVSP:
1051 case XFS_IOC_UNRESVSP:
1052 case XFS_IOC_ALLOCSP64:
1053 case XFS_IOC_FREESP64:
1054 case XFS_IOC_RESVSP64:
1055 case XFS_IOC_UNRESVSP64:
1056 /*
1057 * Only allow the sys admin to reserve space unless
1058 * unwritten extents are enabled.
1059 */
1060 if (!xfs_sb_version_hasextflgbit(&mp->m_sb) &&
1061 !capable(CAP_SYS_ADMIN))
1062 return -EPERM;
1063
1064 return xfs_ioc_space(ip, inode, filp, ioflags, cmd, arg);
1065
1066 case XFS_IOC_DIOINFO: {
1067 struct dioattr da;
1068 xfs_buftarg_t *target =
1069 XFS_IS_REALTIME_INODE(ip) ?
1070 mp->m_rtdev_targp : mp->m_ddev_targp;
1071
1072 da.d_mem = da.d_miniosz = 1 << target->bt_sshift;
1073 da.d_maxiosz = INT_MAX & ~(da.d_miniosz - 1);
1074
1075 if (copy_to_user(arg, &da, sizeof(da)))
1076 return -XFS_ERROR(EFAULT);
1077 return 0;
1078 }
1079
1080 case XFS_IOC_FSBULKSTAT_SINGLE:
1081 case XFS_IOC_FSBULKSTAT:
1082 case XFS_IOC_FSINUMBERS:
1083 return xfs_ioc_bulkstat(mp, cmd, arg);
1084
1085 case XFS_IOC_FSGEOMETRY_V1:
1086 return xfs_ioc_fsgeometry_v1(mp, arg);
1087
1088 case XFS_IOC_FSGEOMETRY:
1089 return xfs_ioc_fsgeometry(mp, arg);
1090
1091 case XFS_IOC_GETVERSION:
1092 return put_user(inode->i_generation, (int __user *)arg);
1093
1094 case XFS_IOC_FSGETXATTR:
1095 return xfs_ioc_fsgetxattr(ip, 0, arg);
1096 case XFS_IOC_FSGETXATTRA:
1097 return xfs_ioc_fsgetxattr(ip, 1, arg);
Lachlan McIlroy3b2816b2008-04-18 12:43:35 +10001098 case XFS_IOC_FSSETXATTR:
Lachlan McIlroy65e67f52008-04-18 12:59:45 +10001099 return xfs_ioc_fssetxattr(ip, filp, arg);
1100 case XFS_IOC_GETXFLAGS:
1101 return xfs_ioc_getxflags(ip, arg);
1102 case XFS_IOC_SETXFLAGS:
1103 return xfs_ioc_setxflags(ip, filp, arg);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001104
1105 case XFS_IOC_FSSETDM: {
1106 struct fsdmidata dmi;
1107
1108 if (copy_from_user(&dmi, arg, sizeof(dmi)))
1109 return -XFS_ERROR(EFAULT);
1110
1111 error = xfs_set_dmattrs(ip, dmi.fsd_dmevmask,
1112 dmi.fsd_dmstate);
1113 return -error;
1114 }
1115
1116 case XFS_IOC_GETBMAP:
1117 case XFS_IOC_GETBMAPA:
1118 return xfs_ioc_getbmap(ip, ioflags, cmd, arg);
1119
1120 case XFS_IOC_GETBMAPX:
1121 return xfs_ioc_getbmapx(ip, arg);
1122
1123 case XFS_IOC_FD_TO_HANDLE:
1124 case XFS_IOC_PATH_TO_HANDLE:
1125 case XFS_IOC_PATH_TO_FSHANDLE:
1126 return xfs_find_handle(cmd, arg);
1127
1128 case XFS_IOC_OPEN_BY_HANDLE:
1129 return xfs_open_by_handle(mp, arg, filp, inode);
1130
1131 case XFS_IOC_FSSETDM_BY_HANDLE:
1132 return xfs_fssetdm_by_handle(mp, arg, inode);
1133
1134 case XFS_IOC_READLINK_BY_HANDLE:
1135 return xfs_readlink_by_handle(mp, arg, inode);
1136
1137 case XFS_IOC_ATTRLIST_BY_HANDLE:
1138 return xfs_attrlist_by_handle(mp, arg, inode);
1139
1140 case XFS_IOC_ATTRMULTI_BY_HANDLE:
Dave Hansen42a74f22008-02-15 14:37:46 -08001141 return xfs_attrmulti_by_handle(mp, arg, filp, inode);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001142
1143 case XFS_IOC_SWAPEXT: {
1144 error = xfs_swapext((struct xfs_swapext __user *)arg);
1145 return -error;
1146 }
1147
1148 case XFS_IOC_FSCOUNTS: {
1149 xfs_fsop_counts_t out;
1150
1151 error = xfs_fs_counts(mp, &out);
1152 if (error)
1153 return -error;
1154
1155 if (copy_to_user(arg, &out, sizeof(out)))
1156 return -XFS_ERROR(EFAULT);
1157 return 0;
1158 }
1159
1160 case XFS_IOC_SET_RESBLKS: {
1161 xfs_fsop_resblks_t inout;
1162 __uint64_t in;
1163
1164 if (!capable(CAP_SYS_ADMIN))
1165 return -EPERM;
1166
1167 if (copy_from_user(&inout, arg, sizeof(inout)))
1168 return -XFS_ERROR(EFAULT);
1169
1170 /* input parameter is passed in resblks field of structure */
1171 in = inout.resblks;
1172 error = xfs_reserve_blocks(mp, &in, &inout);
1173 if (error)
1174 return -error;
1175
1176 if (copy_to_user(arg, &inout, sizeof(inout)))
1177 return -XFS_ERROR(EFAULT);
1178 return 0;
1179 }
1180
1181 case XFS_IOC_GET_RESBLKS: {
1182 xfs_fsop_resblks_t out;
1183
1184 if (!capable(CAP_SYS_ADMIN))
1185 return -EPERM;
1186
1187 error = xfs_reserve_blocks(mp, NULL, &out);
1188 if (error)
1189 return -error;
1190
1191 if (copy_to_user(arg, &out, sizeof(out)))
1192 return -XFS_ERROR(EFAULT);
1193
1194 return 0;
1195 }
1196
1197 case XFS_IOC_FSGROWFSDATA: {
1198 xfs_growfs_data_t in;
1199
1200 if (!capable(CAP_SYS_ADMIN))
1201 return -EPERM;
1202
1203 if (copy_from_user(&in, arg, sizeof(in)))
1204 return -XFS_ERROR(EFAULT);
1205
1206 error = xfs_growfs_data(mp, &in);
1207 return -error;
1208 }
1209
1210 case XFS_IOC_FSGROWFSLOG: {
1211 xfs_growfs_log_t in;
1212
1213 if (!capable(CAP_SYS_ADMIN))
1214 return -EPERM;
1215
1216 if (copy_from_user(&in, arg, sizeof(in)))
1217 return -XFS_ERROR(EFAULT);
1218
1219 error = xfs_growfs_log(mp, &in);
1220 return -error;
1221 }
1222
1223 case XFS_IOC_FSGROWFSRT: {
1224 xfs_growfs_rt_t in;
1225
1226 if (!capable(CAP_SYS_ADMIN))
1227 return -EPERM;
1228
1229 if (copy_from_user(&in, arg, sizeof(in)))
1230 return -XFS_ERROR(EFAULT);
1231
1232 error = xfs_growfs_rt(mp, &in);
1233 return -error;
1234 }
1235
1236 case XFS_IOC_FREEZE:
1237 if (!capable(CAP_SYS_ADMIN))
1238 return -EPERM;
1239
1240 if (inode->i_sb->s_frozen == SB_UNFROZEN)
1241 freeze_bdev(inode->i_sb->s_bdev);
1242 return 0;
1243
1244 case XFS_IOC_THAW:
1245 if (!capable(CAP_SYS_ADMIN))
1246 return -EPERM;
1247 if (inode->i_sb->s_frozen != SB_UNFROZEN)
1248 thaw_bdev(inode->i_sb->s_bdev, inode->i_sb);
1249 return 0;
1250
1251 case XFS_IOC_GOINGDOWN: {
1252 __uint32_t in;
1253
1254 if (!capable(CAP_SYS_ADMIN))
1255 return -EPERM;
1256
1257 if (get_user(in, (__uint32_t __user *)arg))
1258 return -XFS_ERROR(EFAULT);
1259
1260 error = xfs_fs_goingdown(mp, in);
1261 return -error;
1262 }
1263
1264 case XFS_IOC_ERROR_INJECTION: {
1265 xfs_error_injection_t in;
1266
1267 if (!capable(CAP_SYS_ADMIN))
1268 return -EPERM;
1269
1270 if (copy_from_user(&in, arg, sizeof(in)))
1271 return -XFS_ERROR(EFAULT);
1272
1273 error = xfs_errortag_add(in.errtag, mp);
1274 return -error;
1275 }
1276
1277 case XFS_IOC_ERROR_CLEARALL:
1278 if (!capable(CAP_SYS_ADMIN))
1279 return -EPERM;
1280
1281 error = xfs_errortag_clearall(mp, 1);
1282 return -error;
1283
1284 default:
1285 return -ENOTTY;
1286 }
1287}