blob: 7ad7b680e9966518bd32877e138b2df922dc6ec0 [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_dir.h"
27#include "xfs_dir2.h"
28#include "xfs_alloc.h"
29#include "xfs_dmapi.h"
30#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110032#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "xfs_ialloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_dir_sf.h"
Nathan Scotta844f452005-11-02 14:38:42 +110035#include "xfs_attr_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "xfs_dir2_sf.h"
37#include "xfs_dinode.h"
38#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110039#include "xfs_btree.h"
40#include "xfs_ialloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include "xfs_rtalloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include "xfs_itable.h"
Nathan Scotta844f452005-11-02 14:38:42 +110043#include "xfs_error.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include "xfs_rw.h"
45#include "xfs_acl.h"
46#include "xfs_cap.h"
47#include "xfs_mac.h"
48#include "xfs_attr.h"
Nathan Scotta844f452005-11-02 14:38:42 +110049#include "xfs_bmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include "xfs_buf_item.h"
51#include "xfs_utils.h"
52#include "xfs_dfrag.h"
53#include "xfs_fsops.h"
54
55#include <linux/dcache.h>
56#include <linux/mount.h>
57#include <linux/namei.h>
58#include <linux/pagemap.h>
59
60/*
61 * xfs_find_handle maps from userspace xfs_fsop_handlereq structure to
62 * a file or fs handle.
63 *
64 * XFS_IOC_PATH_TO_FSHANDLE
65 * returns fs handle for a mount point or path within that mount point
66 * XFS_IOC_FD_TO_HANDLE
67 * returns full handle for a FD opened in user space
68 * XFS_IOC_PATH_TO_HANDLE
69 * returns full handle for a path
70 */
71STATIC int
72xfs_find_handle(
73 unsigned int cmd,
74 void __user *arg)
75{
76 int hsize;
77 xfs_handle_t handle;
78 xfs_fsop_handlereq_t hreq;
79 struct inode *inode;
80 struct vnode *vp;
81
82 if (copy_from_user(&hreq, arg, sizeof(hreq)))
83 return -XFS_ERROR(EFAULT);
84
85 memset((char *)&handle, 0, sizeof(handle));
86
87 switch (cmd) {
88 case XFS_IOC_PATH_TO_FSHANDLE:
89 case XFS_IOC_PATH_TO_HANDLE: {
90 struct nameidata nd;
91 int error;
92
93 error = user_path_walk_link((const char __user *)hreq.path, &nd);
94 if (error)
95 return error;
96
97 ASSERT(nd.dentry);
98 ASSERT(nd.dentry->d_inode);
99 inode = igrab(nd.dentry->d_inode);
100 path_release(&nd);
101 break;
102 }
103
104 case XFS_IOC_FD_TO_HANDLE: {
105 struct file *file;
106
107 file = fget(hreq.fd);
108 if (!file)
109 return -EBADF;
110
111 ASSERT(file->f_dentry);
112 ASSERT(file->f_dentry->d_inode);
113 inode = igrab(file->f_dentry->d_inode);
114 fput(file);
115 break;
116 }
117
118 default:
119 ASSERT(0);
120 return -XFS_ERROR(EINVAL);
121 }
122
123 if (inode->i_sb->s_magic != XFS_SB_MAGIC) {
124 /* we're not in XFS anymore, Toto */
125 iput(inode);
126 return -XFS_ERROR(EINVAL);
127 }
128
Christoph Hellwig0432dab2005-09-02 16:46:51 +1000129 switch (inode->i_mode & S_IFMT) {
130 case S_IFREG:
131 case S_IFDIR:
132 case S_IFLNK:
133 break;
134 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 iput(inode);
136 return -XFS_ERROR(EBADF);
137 }
138
Christoph Hellwig0432dab2005-09-02 16:46:51 +1000139 /* we need the vnode */
140 vp = LINVFS_GET_VP(inode);
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 /* now we can grab the fsid */
143 memcpy(&handle.ha_fsid, vp->v_vfsp->vfs_altfsid, sizeof(xfs_fsid_t));
144 hsize = sizeof(xfs_fsid_t);
145
146 if (cmd != XFS_IOC_PATH_TO_FSHANDLE) {
147 xfs_inode_t *ip;
148 bhv_desc_t *bhv;
149 int lock_mode;
150
151 /* need to get access to the xfs_inode to read the generation */
152 bhv = vn_bhv_lookup_unlocked(VN_BHV_HEAD(vp), &xfs_vnodeops);
153 ASSERT(bhv);
154 ip = XFS_BHVTOI(bhv);
155 ASSERT(ip);
156 lock_mode = xfs_ilock_map_shared(ip);
157
158 /* fill in fid section of handle from inode */
159 handle.ha_fid.xfs_fid_len = sizeof(xfs_fid_t) -
160 sizeof(handle.ha_fid.xfs_fid_len);
161 handle.ha_fid.xfs_fid_pad = 0;
162 handle.ha_fid.xfs_fid_gen = ip->i_d.di_gen;
163 handle.ha_fid.xfs_fid_ino = ip->i_ino;
164
165 xfs_iunlock_map_shared(ip, lock_mode);
166
167 hsize = XFS_HSIZE(handle);
168 }
169
170 /* now copy our handle into the user buffer & write out the size */
171 if (copy_to_user(hreq.ohandle, &handle, hsize) ||
172 copy_to_user(hreq.ohandlen, &hsize, sizeof(__s32))) {
173 iput(inode);
174 return -XFS_ERROR(EFAULT);
175 }
176
177 iput(inode);
178 return 0;
179}
180
181
182/*
183 * Convert userspace handle data into vnode (and inode).
184 * We [ab]use the fact that all the fsop_handlereq ioctl calls
185 * have a data structure argument whose first component is always
186 * a xfs_fsop_handlereq_t, so we can cast to and from this type.
187 * This allows us to optimise the copy_from_user calls and gives
188 * a handy, shared routine.
189 *
190 * If no error, caller must always VN_RELE the returned vp.
191 */
192STATIC int
193xfs_vget_fsop_handlereq(
194 xfs_mount_t *mp,
195 struct inode *parinode, /* parent inode pointer */
196 xfs_fsop_handlereq_t *hreq,
197 vnode_t **vp,
198 struct inode **inode)
199{
200 void __user *hanp;
201 size_t hlen;
202 xfs_fid_t *xfid;
203 xfs_handle_t *handlep;
204 xfs_handle_t handle;
205 xfs_inode_t *ip;
206 struct inode *inodep;
207 vnode_t *vpp;
208 xfs_ino_t ino;
209 __u32 igen;
210 int error;
211
212 /*
213 * Only allow handle opens under a directory.
214 */
215 if (!S_ISDIR(parinode->i_mode))
216 return XFS_ERROR(ENOTDIR);
217
218 hanp = hreq->ihandle;
219 hlen = hreq->ihandlen;
220 handlep = &handle;
221
222 if (hlen < sizeof(handlep->ha_fsid) || hlen > sizeof(*handlep))
223 return XFS_ERROR(EINVAL);
224 if (copy_from_user(handlep, hanp, hlen))
225 return XFS_ERROR(EFAULT);
226 if (hlen < sizeof(*handlep))
227 memset(((char *)handlep) + hlen, 0, sizeof(*handlep) - hlen);
228 if (hlen > sizeof(handlep->ha_fsid)) {
229 if (handlep->ha_fid.xfs_fid_len !=
230 (hlen - sizeof(handlep->ha_fsid)
231 - sizeof(handlep->ha_fid.xfs_fid_len))
232 || handlep->ha_fid.xfs_fid_pad)
233 return XFS_ERROR(EINVAL);
234 }
235
236 /*
237 * Crack the handle, obtain the inode # & generation #
238 */
239 xfid = (struct xfs_fid *)&handlep->ha_fid;
240 if (xfid->xfs_fid_len == sizeof(*xfid) - sizeof(xfid->xfs_fid_len)) {
241 ino = xfid->xfs_fid_ino;
242 igen = xfid->xfs_fid_gen;
243 } else {
244 return XFS_ERROR(EINVAL);
245 }
246
247 /*
248 * Get the XFS inode, building a vnode to go with it.
249 */
250 error = xfs_iget(mp, NULL, ino, 0, XFS_ILOCK_SHARED, &ip, 0);
251 if (error)
252 return error;
253 if (ip == NULL)
254 return XFS_ERROR(EIO);
255 if (ip->i_d.di_mode == 0 || ip->i_d.di_gen != igen) {
256 xfs_iput_new(ip, XFS_ILOCK_SHARED);
257 return XFS_ERROR(ENOENT);
258 }
259
260 vpp = XFS_ITOV(ip);
261 inodep = LINVFS_GET_IP(vpp);
262 xfs_iunlock(ip, XFS_ILOCK_SHARED);
263
264 *vp = vpp;
265 *inode = inodep;
266 return 0;
267}
268
269STATIC int
270xfs_open_by_handle(
271 xfs_mount_t *mp,
272 void __user *arg,
273 struct file *parfilp,
274 struct inode *parinode)
275{
276 int error;
277 int new_fd;
278 int permflag;
279 struct file *filp;
280 struct inode *inode;
281 struct dentry *dentry;
282 vnode_t *vp;
283 xfs_fsop_handlereq_t hreq;
284
285 if (!capable(CAP_SYS_ADMIN))
286 return -XFS_ERROR(EPERM);
287 if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
288 return -XFS_ERROR(EFAULT);
289
290 error = xfs_vget_fsop_handlereq(mp, parinode, &hreq, &vp, &inode);
291 if (error)
292 return -error;
293
294 /* Restrict xfs_open_by_handle to directories & regular files. */
295 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode))) {
296 iput(inode);
297 return -XFS_ERROR(EINVAL);
298 }
299
300#if BITS_PER_LONG != 32
301 hreq.oflags |= O_LARGEFILE;
302#endif
303 /* Put open permission in namei format. */
304 permflag = hreq.oflags;
305 if ((permflag+1) & O_ACCMODE)
306 permflag++;
307 if (permflag & O_TRUNC)
308 permflag |= 2;
309
310 if ((!(permflag & O_APPEND) || (permflag & O_TRUNC)) &&
311 (permflag & FMODE_WRITE) && IS_APPEND(inode)) {
312 iput(inode);
313 return -XFS_ERROR(EPERM);
314 }
315
316 if ((permflag & FMODE_WRITE) && IS_IMMUTABLE(inode)) {
317 iput(inode);
318 return -XFS_ERROR(EACCES);
319 }
320
321 /* Can't write directories. */
322 if ( S_ISDIR(inode->i_mode) && (permflag & FMODE_WRITE)) {
323 iput(inode);
324 return -XFS_ERROR(EISDIR);
325 }
326
327 if ((new_fd = get_unused_fd()) < 0) {
328 iput(inode);
329 return new_fd;
330 }
331
332 dentry = d_alloc_anon(inode);
333 if (dentry == NULL) {
334 iput(inode);
335 put_unused_fd(new_fd);
336 return -XFS_ERROR(ENOMEM);
337 }
338
339 /* Ensure umount returns EBUSY on umounts while this file is open. */
340 mntget(parfilp->f_vfsmnt);
341
342 /* Create file pointer. */
343 filp = dentry_open(dentry, parfilp->f_vfsmnt, hreq.oflags);
344 if (IS_ERR(filp)) {
345 put_unused_fd(new_fd);
346 return -XFS_ERROR(-PTR_ERR(filp));
347 }
348 if (inode->i_mode & S_IFREG)
349 filp->f_op = &linvfs_invis_file_operations;
350
351 fd_install(new_fd, filp);
352 return new_fd;
353}
354
355STATIC int
356xfs_readlink_by_handle(
357 xfs_mount_t *mp,
358 void __user *arg,
359 struct file *parfilp,
360 struct inode *parinode)
361{
362 int error;
363 struct iovec aiov;
364 struct uio auio;
365 struct inode *inode;
366 xfs_fsop_handlereq_t hreq;
367 vnode_t *vp;
368 __u32 olen;
369
370 if (!capable(CAP_SYS_ADMIN))
371 return -XFS_ERROR(EPERM);
372 if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
373 return -XFS_ERROR(EFAULT);
374
375 error = xfs_vget_fsop_handlereq(mp, parinode, &hreq, &vp, &inode);
376 if (error)
377 return -error;
378
379 /* Restrict this handle operation to symlinks only. */
Christoph Hellwig0432dab2005-09-02 16:46:51 +1000380 if (!S_ISLNK(inode->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 VN_RELE(vp);
382 return -XFS_ERROR(EINVAL);
383 }
384
385 if (copy_from_user(&olen, hreq.ohandlen, sizeof(__u32))) {
386 VN_RELE(vp);
387 return -XFS_ERROR(EFAULT);
388 }
389 aiov.iov_len = olen;
390 aiov.iov_base = hreq.ohandle;
391
392 auio.uio_iov = &aiov;
393 auio.uio_iovcnt = 1;
394 auio.uio_offset = 0;
395 auio.uio_segflg = UIO_USERSPACE;
396 auio.uio_resid = olen;
397
398 VOP_READLINK(vp, &auio, IO_INVIS, NULL, error);
399
400 VN_RELE(vp);
401 return (olen - auio.uio_resid);
402}
403
404STATIC int
405xfs_fssetdm_by_handle(
406 xfs_mount_t *mp,
407 void __user *arg,
408 struct file *parfilp,
409 struct inode *parinode)
410{
411 int error;
412 struct fsdmidata fsd;
413 xfs_fsop_setdm_handlereq_t dmhreq;
414 struct inode *inode;
415 bhv_desc_t *bdp;
416 vnode_t *vp;
417
418 if (!capable(CAP_MKNOD))
419 return -XFS_ERROR(EPERM);
420 if (copy_from_user(&dmhreq, arg, sizeof(xfs_fsop_setdm_handlereq_t)))
421 return -XFS_ERROR(EFAULT);
422
423 error = xfs_vget_fsop_handlereq(mp, parinode, &dmhreq.hreq, &vp, &inode);
424 if (error)
425 return -error;
426
427 if (IS_IMMUTABLE(inode) || IS_APPEND(inode)) {
428 VN_RELE(vp);
429 return -XFS_ERROR(EPERM);
430 }
431
432 if (copy_from_user(&fsd, dmhreq.data, sizeof(fsd))) {
433 VN_RELE(vp);
434 return -XFS_ERROR(EFAULT);
435 }
436
437 bdp = bhv_base_unlocked(VN_BHV_HEAD(vp));
438 error = xfs_set_dmattrs(bdp, fsd.fsd_dmevmask, fsd.fsd_dmstate, NULL);
439
440 VN_RELE(vp);
441 if (error)
442 return -error;
443 return 0;
444}
445
446STATIC int
447xfs_attrlist_by_handle(
448 xfs_mount_t *mp,
449 void __user *arg,
450 struct file *parfilp,
451 struct inode *parinode)
452{
453 int error;
454 attrlist_cursor_kern_t *cursor;
455 xfs_fsop_attrlist_handlereq_t al_hreq;
456 struct inode *inode;
457 vnode_t *vp;
458 char *kbuf;
459
460 if (!capable(CAP_SYS_ADMIN))
461 return -XFS_ERROR(EPERM);
462 if (copy_from_user(&al_hreq, arg, sizeof(xfs_fsop_attrlist_handlereq_t)))
463 return -XFS_ERROR(EFAULT);
464 if (al_hreq.buflen > XATTR_LIST_MAX)
465 return -XFS_ERROR(EINVAL);
466
467 error = xfs_vget_fsop_handlereq(mp, parinode, &al_hreq.hreq,
468 &vp, &inode);
469 if (error)
470 goto out;
471
472 kbuf = kmalloc(al_hreq.buflen, GFP_KERNEL);
473 if (!kbuf)
474 goto out_vn_rele;
475
476 cursor = (attrlist_cursor_kern_t *)&al_hreq.pos;
477 VOP_ATTR_LIST(vp, kbuf, al_hreq.buflen, al_hreq.flags,
478 cursor, NULL, error);
479 if (error)
480 goto out_kfree;
481
482 if (copy_to_user(al_hreq.buffer, kbuf, al_hreq.buflen))
483 error = -EFAULT;
484
485 out_kfree:
486 kfree(kbuf);
487 out_vn_rele:
488 VN_RELE(vp);
489 out:
490 return -error;
491}
492
493STATIC int
494xfs_attrmulti_attr_get(
495 struct vnode *vp,
496 char *name,
497 char __user *ubuf,
498 __uint32_t *len,
499 __uint32_t flags)
500{
501 char *kbuf;
502 int error = EFAULT;
503
504 if (*len > XATTR_SIZE_MAX)
505 return EINVAL;
506 kbuf = kmalloc(*len, GFP_KERNEL);
507 if (!kbuf)
508 return ENOMEM;
509
510 VOP_ATTR_GET(vp, name, kbuf, len, flags, NULL, error);
511 if (error)
512 goto out_kfree;
513
514 if (copy_to_user(ubuf, kbuf, *len))
515 error = EFAULT;
516
517 out_kfree:
518 kfree(kbuf);
519 return error;
520}
521
522STATIC int
523xfs_attrmulti_attr_set(
524 struct vnode *vp,
525 char *name,
526 const char __user *ubuf,
527 __uint32_t len,
528 __uint32_t flags)
529{
530 char *kbuf;
531 int error = EFAULT;
532
Christoph Hellwig3542c6e2006-01-09 20:52:00 -0800533 if (IS_RDONLY(&vp->v_inode))
534 return -EROFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 if (IS_IMMUTABLE(&vp->v_inode) || IS_APPEND(&vp->v_inode))
536 return EPERM;
537 if (len > XATTR_SIZE_MAX)
538 return EINVAL;
539
540 kbuf = kmalloc(len, GFP_KERNEL);
541 if (!kbuf)
542 return ENOMEM;
543
544 if (copy_from_user(kbuf, ubuf, len))
545 goto out_kfree;
546
547 VOP_ATTR_SET(vp, name, kbuf, len, flags, NULL, error);
548
549 out_kfree:
550 kfree(kbuf);
551 return error;
552}
553
554STATIC int
555xfs_attrmulti_attr_remove(
556 struct vnode *vp,
557 char *name,
558 __uint32_t flags)
559{
560 int error;
561
Christoph Hellwig3542c6e2006-01-09 20:52:00 -0800562
563 if (IS_RDONLY(&vp->v_inode))
564 return -EROFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 if (IS_IMMUTABLE(&vp->v_inode) || IS_APPEND(&vp->v_inode))
566 return EPERM;
567
568 VOP_ATTR_REMOVE(vp, name, flags, NULL, error);
569 return error;
570}
571
572STATIC int
573xfs_attrmulti_by_handle(
574 xfs_mount_t *mp,
575 void __user *arg,
576 struct file *parfilp,
577 struct inode *parinode)
578{
579 int error;
580 xfs_attr_multiop_t *ops;
581 xfs_fsop_attrmulti_handlereq_t am_hreq;
582 struct inode *inode;
583 vnode_t *vp;
584 unsigned int i, size;
585 char *attr_name;
586
587 if (!capable(CAP_SYS_ADMIN))
588 return -XFS_ERROR(EPERM);
589 if (copy_from_user(&am_hreq, arg, sizeof(xfs_fsop_attrmulti_handlereq_t)))
590 return -XFS_ERROR(EFAULT);
591
592 error = xfs_vget_fsop_handlereq(mp, parinode, &am_hreq.hreq, &vp, &inode);
593 if (error)
594 goto out;
595
596 error = E2BIG;
597 size = am_hreq.opcount * sizeof(attr_multiop_t);
598 if (!size || size > 16 * PAGE_SIZE)
599 goto out_vn_rele;
600
601 error = ENOMEM;
602 ops = kmalloc(size, GFP_KERNEL);
603 if (!ops)
604 goto out_vn_rele;
605
606 error = EFAULT;
607 if (copy_from_user(ops, am_hreq.ops, size))
608 goto out_kfree_ops;
609
610 attr_name = kmalloc(MAXNAMELEN, GFP_KERNEL);
611 if (!attr_name)
612 goto out_kfree_ops;
613
614
615 error = 0;
616 for (i = 0; i < am_hreq.opcount; i++) {
617 ops[i].am_error = strncpy_from_user(attr_name,
618 ops[i].am_attrname, MAXNAMELEN);
619 if (ops[i].am_error == 0 || ops[i].am_error == MAXNAMELEN)
620 error = -ERANGE;
621 if (ops[i].am_error < 0)
622 break;
623
624 switch (ops[i].am_opcode) {
625 case ATTR_OP_GET:
626 ops[i].am_error = xfs_attrmulti_attr_get(vp,
627 attr_name, ops[i].am_attrvalue,
628 &ops[i].am_length, ops[i].am_flags);
629 break;
630 case ATTR_OP_SET:
631 ops[i].am_error = xfs_attrmulti_attr_set(vp,
632 attr_name, ops[i].am_attrvalue,
633 ops[i].am_length, ops[i].am_flags);
634 break;
635 case ATTR_OP_REMOVE:
636 ops[i].am_error = xfs_attrmulti_attr_remove(vp,
637 attr_name, ops[i].am_flags);
638 break;
639 default:
640 ops[i].am_error = EINVAL;
641 }
642 }
643
644 if (copy_to_user(am_hreq.ops, ops, size))
645 error = XFS_ERROR(EFAULT);
646
647 kfree(attr_name);
648 out_kfree_ops:
649 kfree(ops);
650 out_vn_rele:
651 VN_RELE(vp);
652 out:
653 return -error;
654}
655
656/* prototypes for a few of the stack-hungry cases that have
657 * their own functions. Functions are defined after their use
658 * so gcc doesn't get fancy and inline them with -03 */
659
660STATIC int
661xfs_ioc_space(
662 bhv_desc_t *bdp,
663 vnode_t *vp,
664 struct file *filp,
665 int flags,
666 unsigned int cmd,
667 void __user *arg);
668
669STATIC int
670xfs_ioc_bulkstat(
671 xfs_mount_t *mp,
672 unsigned int cmd,
673 void __user *arg);
674
675STATIC int
676xfs_ioc_fsgeometry_v1(
677 xfs_mount_t *mp,
678 void __user *arg);
679
680STATIC int
681xfs_ioc_fsgeometry(
682 xfs_mount_t *mp,
683 void __user *arg);
684
685STATIC int
686xfs_ioc_xattr(
687 vnode_t *vp,
688 xfs_inode_t *ip,
689 struct file *filp,
690 unsigned int cmd,
691 void __user *arg);
692
693STATIC int
694xfs_ioc_getbmap(
695 bhv_desc_t *bdp,
696 struct file *filp,
697 int flags,
698 unsigned int cmd,
699 void __user *arg);
700
701STATIC int
702xfs_ioc_getbmapx(
703 bhv_desc_t *bdp,
704 void __user *arg);
705
706int
707xfs_ioctl(
708 bhv_desc_t *bdp,
709 struct inode *inode,
710 struct file *filp,
711 int ioflags,
712 unsigned int cmd,
713 void __user *arg)
714{
715 int error;
716 vnode_t *vp;
717 xfs_inode_t *ip;
718 xfs_mount_t *mp;
719
720 vp = LINVFS_GET_VP(inode);
721
722 vn_trace_entry(vp, "xfs_ioctl", (inst_t *)__return_address);
723
724 ip = XFS_BHVTOI(bdp);
725 mp = ip->i_mount;
726
727 switch (cmd) {
728
729 case XFS_IOC_ALLOCSP:
730 case XFS_IOC_FREESP:
731 case XFS_IOC_RESVSP:
732 case XFS_IOC_UNRESVSP:
733 case XFS_IOC_ALLOCSP64:
734 case XFS_IOC_FREESP64:
735 case XFS_IOC_RESVSP64:
736 case XFS_IOC_UNRESVSP64:
737 /*
738 * Only allow the sys admin to reserve space unless
739 * unwritten extents are enabled.
740 */
741 if (!XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb) &&
742 !capable(CAP_SYS_ADMIN))
743 return -EPERM;
744
745 return xfs_ioc_space(bdp, vp, filp, ioflags, cmd, arg);
746
747 case XFS_IOC_DIOINFO: {
748 struct dioattr da;
749 xfs_buftarg_t *target =
750 (ip->i_d.di_flags & XFS_DIFLAG_REALTIME) ?
751 mp->m_rtdev_targp : mp->m_ddev_targp;
752
Nathan Scottce8e9222006-01-11 15:39:08 +1100753 da.d_mem = da.d_miniosz = 1 << target->bt_sshift;
Nathan Scott0d148242006-01-11 15:33:51 +1100754 da.d_maxiosz = INT_MAX & ~(da.d_miniosz - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
756 if (copy_to_user(arg, &da, sizeof(da)))
757 return -XFS_ERROR(EFAULT);
758 return 0;
759 }
760
761 case XFS_IOC_FSBULKSTAT_SINGLE:
762 case XFS_IOC_FSBULKSTAT:
763 case XFS_IOC_FSINUMBERS:
764 return xfs_ioc_bulkstat(mp, cmd, arg);
765
766 case XFS_IOC_FSGEOMETRY_V1:
767 return xfs_ioc_fsgeometry_v1(mp, arg);
768
769 case XFS_IOC_FSGEOMETRY:
770 return xfs_ioc_fsgeometry(mp, arg);
771
772 case XFS_IOC_GETVERSION:
773 case XFS_IOC_GETXFLAGS:
774 case XFS_IOC_SETXFLAGS:
775 case XFS_IOC_FSGETXATTR:
776 case XFS_IOC_FSSETXATTR:
777 case XFS_IOC_FSGETXATTRA:
778 return xfs_ioc_xattr(vp, ip, filp, cmd, arg);
779
780 case XFS_IOC_FSSETDM: {
781 struct fsdmidata dmi;
782
783 if (copy_from_user(&dmi, arg, sizeof(dmi)))
784 return -XFS_ERROR(EFAULT);
785
786 error = xfs_set_dmattrs(bdp, dmi.fsd_dmevmask, dmi.fsd_dmstate,
787 NULL);
788 return -error;
789 }
790
791 case XFS_IOC_GETBMAP:
792 case XFS_IOC_GETBMAPA:
793 return xfs_ioc_getbmap(bdp, filp, ioflags, cmd, arg);
794
795 case XFS_IOC_GETBMAPX:
796 return xfs_ioc_getbmapx(bdp, arg);
797
798 case XFS_IOC_FD_TO_HANDLE:
799 case XFS_IOC_PATH_TO_HANDLE:
800 case XFS_IOC_PATH_TO_FSHANDLE:
801 return xfs_find_handle(cmd, arg);
802
803 case XFS_IOC_OPEN_BY_HANDLE:
804 return xfs_open_by_handle(mp, arg, filp, inode);
805
806 case XFS_IOC_FSSETDM_BY_HANDLE:
807 return xfs_fssetdm_by_handle(mp, arg, filp, inode);
808
809 case XFS_IOC_READLINK_BY_HANDLE:
810 return xfs_readlink_by_handle(mp, arg, filp, inode);
811
812 case XFS_IOC_ATTRLIST_BY_HANDLE:
813 return xfs_attrlist_by_handle(mp, arg, filp, inode);
814
815 case XFS_IOC_ATTRMULTI_BY_HANDLE:
816 return xfs_attrmulti_by_handle(mp, arg, filp, inode);
817
818 case XFS_IOC_SWAPEXT: {
819 error = xfs_swapext((struct xfs_swapext __user *)arg);
820 return -error;
821 }
822
823 case XFS_IOC_FSCOUNTS: {
824 xfs_fsop_counts_t out;
825
826 error = xfs_fs_counts(mp, &out);
827 if (error)
828 return -error;
829
830 if (copy_to_user(arg, &out, sizeof(out)))
831 return -XFS_ERROR(EFAULT);
832 return 0;
833 }
834
835 case XFS_IOC_SET_RESBLKS: {
836 xfs_fsop_resblks_t inout;
837 __uint64_t in;
838
839 if (!capable(CAP_SYS_ADMIN))
840 return -EPERM;
841
842 if (copy_from_user(&inout, arg, sizeof(inout)))
843 return -XFS_ERROR(EFAULT);
844
845 /* input parameter is passed in resblks field of structure */
846 in = inout.resblks;
847 error = xfs_reserve_blocks(mp, &in, &inout);
848 if (error)
849 return -error;
850
851 if (copy_to_user(arg, &inout, sizeof(inout)))
852 return -XFS_ERROR(EFAULT);
853 return 0;
854 }
855
856 case XFS_IOC_GET_RESBLKS: {
857 xfs_fsop_resblks_t out;
858
859 if (!capable(CAP_SYS_ADMIN))
860 return -EPERM;
861
862 error = xfs_reserve_blocks(mp, NULL, &out);
863 if (error)
864 return -error;
865
866 if (copy_to_user(arg, &out, sizeof(out)))
867 return -XFS_ERROR(EFAULT);
868
869 return 0;
870 }
871
872 case XFS_IOC_FSGROWFSDATA: {
873 xfs_growfs_data_t in;
874
875 if (!capable(CAP_SYS_ADMIN))
876 return -EPERM;
877
878 if (copy_from_user(&in, arg, sizeof(in)))
879 return -XFS_ERROR(EFAULT);
880
881 error = xfs_growfs_data(mp, &in);
882 return -error;
883 }
884
885 case XFS_IOC_FSGROWFSLOG: {
886 xfs_growfs_log_t in;
887
888 if (!capable(CAP_SYS_ADMIN))
889 return -EPERM;
890
891 if (copy_from_user(&in, arg, sizeof(in)))
892 return -XFS_ERROR(EFAULT);
893
894 error = xfs_growfs_log(mp, &in);
895 return -error;
896 }
897
898 case XFS_IOC_FSGROWFSRT: {
899 xfs_growfs_rt_t in;
900
901 if (!capable(CAP_SYS_ADMIN))
902 return -EPERM;
903
904 if (copy_from_user(&in, arg, sizeof(in)))
905 return -XFS_ERROR(EFAULT);
906
907 error = xfs_growfs_rt(mp, &in);
908 return -error;
909 }
910
911 case XFS_IOC_FREEZE:
912 if (!capable(CAP_SYS_ADMIN))
913 return -EPERM;
914
915 if (inode->i_sb->s_frozen == SB_UNFROZEN)
916 freeze_bdev(inode->i_sb->s_bdev);
917 return 0;
918
919 case XFS_IOC_THAW:
920 if (!capable(CAP_SYS_ADMIN))
921 return -EPERM;
922 if (inode->i_sb->s_frozen != SB_UNFROZEN)
923 thaw_bdev(inode->i_sb->s_bdev, inode->i_sb);
924 return 0;
925
926 case XFS_IOC_GOINGDOWN: {
927 __uint32_t in;
928
929 if (!capable(CAP_SYS_ADMIN))
930 return -EPERM;
931
932 if (get_user(in, (__uint32_t __user *)arg))
933 return -XFS_ERROR(EFAULT);
934
935 error = xfs_fs_goingdown(mp, in);
936 return -error;
937 }
938
939 case XFS_IOC_ERROR_INJECTION: {
940 xfs_error_injection_t in;
941
942 if (!capable(CAP_SYS_ADMIN))
943 return -EPERM;
944
945 if (copy_from_user(&in, arg, sizeof(in)))
946 return -XFS_ERROR(EFAULT);
947
948 error = xfs_errortag_add(in.errtag, mp);
949 return -error;
950 }
951
952 case XFS_IOC_ERROR_CLEARALL:
953 if (!capable(CAP_SYS_ADMIN))
954 return -EPERM;
955
956 error = xfs_errortag_clearall(mp);
957 return -error;
958
959 default:
960 return -ENOTTY;
961 }
962}
963
964STATIC int
965xfs_ioc_space(
966 bhv_desc_t *bdp,
967 vnode_t *vp,
968 struct file *filp,
969 int ioflags,
970 unsigned int cmd,
971 void __user *arg)
972{
973 xfs_flock64_t bf;
974 int attr_flags = 0;
975 int error;
976
977 if (vp->v_inode.i_flags & (S_IMMUTABLE|S_APPEND))
978 return -XFS_ERROR(EPERM);
979
Eric Sandeenad4a8ac2005-09-02 16:41:16 +1000980 if (!(filp->f_mode & FMODE_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 return -XFS_ERROR(EBADF);
982
Christoph Hellwig0432dab2005-09-02 16:46:51 +1000983 if (!VN_ISREG(vp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 return -XFS_ERROR(EINVAL);
985
986 if (copy_from_user(&bf, arg, sizeof(bf)))
987 return -XFS_ERROR(EFAULT);
988
989 if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
990 attr_flags |= ATTR_NONBLOCK;
991 if (ioflags & IO_INVIS)
992 attr_flags |= ATTR_DMI;
993
994 error = xfs_change_file_space(bdp, cmd, &bf, filp->f_pos,
995 NULL, attr_flags);
996 return -error;
997}
998
999STATIC int
1000xfs_ioc_bulkstat(
1001 xfs_mount_t *mp,
1002 unsigned int cmd,
1003 void __user *arg)
1004{
1005 xfs_fsop_bulkreq_t bulkreq;
1006 int count; /* # of records returned */
1007 xfs_ino_t inlast; /* last inode number */
1008 int done;
1009 int error;
1010
1011 /* done = 1 if there are more stats to get and if bulkstat */
1012 /* should be called again (unused here, but used in dmapi) */
1013
1014 if (!capable(CAP_SYS_ADMIN))
1015 return -EPERM;
1016
1017 if (XFS_FORCED_SHUTDOWN(mp))
1018 return -XFS_ERROR(EIO);
1019
1020 if (copy_from_user(&bulkreq, arg, sizeof(xfs_fsop_bulkreq_t)))
1021 return -XFS_ERROR(EFAULT);
1022
1023 if (copy_from_user(&inlast, bulkreq.lastip, sizeof(__s64)))
1024 return -XFS_ERROR(EFAULT);
1025
1026 if ((count = bulkreq.icount) <= 0)
1027 return -XFS_ERROR(EINVAL);
1028
1029 if (cmd == XFS_IOC_FSINUMBERS)
1030 error = xfs_inumbers(mp, &inlast, &count,
1031 bulkreq.ubuffer);
1032 else if (cmd == XFS_IOC_FSBULKSTAT_SINGLE)
1033 error = xfs_bulkstat_single(mp, &inlast,
1034 bulkreq.ubuffer, &done);
1035 else { /* XFS_IOC_FSBULKSTAT */
1036 if (count == 1 && inlast != 0) {
1037 inlast++;
1038 error = xfs_bulkstat_single(mp, &inlast,
1039 bulkreq.ubuffer, &done);
1040 } else {
1041 error = xfs_bulkstat(mp, &inlast, &count,
1042 (bulkstat_one_pf)xfs_bulkstat_one, NULL,
1043 sizeof(xfs_bstat_t), bulkreq.ubuffer,
1044 BULKSTAT_FG_QUICK, &done);
1045 }
1046 }
1047
1048 if (error)
1049 return -error;
1050
1051 if (bulkreq.ocount != NULL) {
1052 if (copy_to_user(bulkreq.lastip, &inlast,
1053 sizeof(xfs_ino_t)))
1054 return -XFS_ERROR(EFAULT);
1055
1056 if (copy_to_user(bulkreq.ocount, &count, sizeof(count)))
1057 return -XFS_ERROR(EFAULT);
1058 }
1059
1060 return 0;
1061}
1062
1063STATIC int
1064xfs_ioc_fsgeometry_v1(
1065 xfs_mount_t *mp,
1066 void __user *arg)
1067{
1068 xfs_fsop_geom_v1_t fsgeo;
1069 int error;
1070
1071 error = xfs_fs_geometry(mp, (xfs_fsop_geom_t *)&fsgeo, 3);
1072 if (error)
1073 return -error;
1074
1075 if (copy_to_user(arg, &fsgeo, sizeof(fsgeo)))
1076 return -XFS_ERROR(EFAULT);
1077 return 0;
1078}
1079
1080STATIC int
1081xfs_ioc_fsgeometry(
1082 xfs_mount_t *mp,
1083 void __user *arg)
1084{
1085 xfs_fsop_geom_t fsgeo;
1086 int error;
1087
1088 error = xfs_fs_geometry(mp, &fsgeo, 4);
1089 if (error)
1090 return -error;
1091
1092 if (copy_to_user(arg, &fsgeo, sizeof(fsgeo)))
1093 return -XFS_ERROR(EFAULT);
1094 return 0;
1095}
1096
1097/*
1098 * Linux extended inode flags interface.
1099 */
1100#define LINUX_XFLAG_SYNC 0x00000008 /* Synchronous updates */
1101#define LINUX_XFLAG_IMMUTABLE 0x00000010 /* Immutable file */
1102#define LINUX_XFLAG_APPEND 0x00000020 /* writes to file may only append */
1103#define LINUX_XFLAG_NODUMP 0x00000040 /* do not dump file */
1104#define LINUX_XFLAG_NOATIME 0x00000080 /* do not update atime */
1105
1106STATIC unsigned int
1107xfs_merge_ioc_xflags(
1108 unsigned int flags,
1109 unsigned int start)
1110{
1111 unsigned int xflags = start;
1112
1113 if (flags & LINUX_XFLAG_IMMUTABLE)
1114 xflags |= XFS_XFLAG_IMMUTABLE;
1115 else
1116 xflags &= ~XFS_XFLAG_IMMUTABLE;
1117 if (flags & LINUX_XFLAG_APPEND)
1118 xflags |= XFS_XFLAG_APPEND;
1119 else
1120 xflags &= ~XFS_XFLAG_APPEND;
1121 if (flags & LINUX_XFLAG_SYNC)
1122 xflags |= XFS_XFLAG_SYNC;
1123 else
1124 xflags &= ~XFS_XFLAG_SYNC;
1125 if (flags & LINUX_XFLAG_NOATIME)
1126 xflags |= XFS_XFLAG_NOATIME;
1127 else
1128 xflags &= ~XFS_XFLAG_NOATIME;
1129 if (flags & LINUX_XFLAG_NODUMP)
1130 xflags |= XFS_XFLAG_NODUMP;
1131 else
1132 xflags &= ~XFS_XFLAG_NODUMP;
1133
1134 return xflags;
1135}
1136
1137STATIC unsigned int
1138xfs_di2lxflags(
1139 __uint16_t di_flags)
1140{
1141 unsigned int flags = 0;
1142
1143 if (di_flags & XFS_DIFLAG_IMMUTABLE)
1144 flags |= LINUX_XFLAG_IMMUTABLE;
1145 if (di_flags & XFS_DIFLAG_APPEND)
1146 flags |= LINUX_XFLAG_APPEND;
1147 if (di_flags & XFS_DIFLAG_SYNC)
1148 flags |= LINUX_XFLAG_SYNC;
1149 if (di_flags & XFS_DIFLAG_NOATIME)
1150 flags |= LINUX_XFLAG_NOATIME;
1151 if (di_flags & XFS_DIFLAG_NODUMP)
1152 flags |= LINUX_XFLAG_NODUMP;
1153 return flags;
1154}
1155
1156STATIC int
1157xfs_ioc_xattr(
1158 vnode_t *vp,
1159 xfs_inode_t *ip,
1160 struct file *filp,
1161 unsigned int cmd,
1162 void __user *arg)
1163{
1164 struct fsxattr fa;
1165 vattr_t va;
1166 int error;
1167 int attr_flags;
1168 unsigned int flags;
1169
1170 switch (cmd) {
1171 case XFS_IOC_FSGETXATTR: {
Nathan Scott77bc5be2005-06-21 15:48:04 +10001172 va.va_mask = XFS_AT_XFLAGS | XFS_AT_EXTSIZE | \
1173 XFS_AT_NEXTENTS | XFS_AT_PROJID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 VOP_GETATTR(vp, &va, 0, NULL, error);
1175 if (error)
1176 return -error;
1177
1178 fa.fsx_xflags = va.va_xflags;
1179 fa.fsx_extsize = va.va_extsize;
1180 fa.fsx_nextents = va.va_nextents;
Nathan Scott77bc5be2005-06-21 15:48:04 +10001181 fa.fsx_projid = va.va_projid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
1183 if (copy_to_user(arg, &fa, sizeof(fa)))
1184 return -XFS_ERROR(EFAULT);
1185 return 0;
1186 }
1187
1188 case XFS_IOC_FSSETXATTR: {
1189 if (copy_from_user(&fa, arg, sizeof(fa)))
1190 return -XFS_ERROR(EFAULT);
1191
1192 attr_flags = 0;
1193 if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
1194 attr_flags |= ATTR_NONBLOCK;
1195
Nathan Scott77bc5be2005-06-21 15:48:04 +10001196 va.va_mask = XFS_AT_XFLAGS | XFS_AT_EXTSIZE | XFS_AT_PROJID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 va.va_xflags = fa.fsx_xflags;
1198 va.va_extsize = fa.fsx_extsize;
Nathan Scott77bc5be2005-06-21 15:48:04 +10001199 va.va_projid = fa.fsx_projid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
1201 VOP_SETATTR(vp, &va, attr_flags, NULL, error);
1202 if (!error)
1203 vn_revalidate(vp); /* update Linux inode flags */
1204 return -error;
1205 }
1206
1207 case XFS_IOC_FSGETXATTRA: {
Nathan Scott77bc5be2005-06-21 15:48:04 +10001208 va.va_mask = XFS_AT_XFLAGS | XFS_AT_EXTSIZE | \
1209 XFS_AT_ANEXTENTS | XFS_AT_PROJID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 VOP_GETATTR(vp, &va, 0, NULL, error);
1211 if (error)
1212 return -error;
1213
1214 fa.fsx_xflags = va.va_xflags;
1215 fa.fsx_extsize = va.va_extsize;
1216 fa.fsx_nextents = va.va_anextents;
Nathan Scott77bc5be2005-06-21 15:48:04 +10001217 fa.fsx_projid = va.va_projid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
1219 if (copy_to_user(arg, &fa, sizeof(fa)))
1220 return -XFS_ERROR(EFAULT);
1221 return 0;
1222 }
1223
1224 case XFS_IOC_GETXFLAGS: {
1225 flags = xfs_di2lxflags(ip->i_d.di_flags);
1226 if (copy_to_user(arg, &flags, sizeof(flags)))
1227 return -XFS_ERROR(EFAULT);
1228 return 0;
1229 }
1230
1231 case XFS_IOC_SETXFLAGS: {
1232 if (copy_from_user(&flags, arg, sizeof(flags)))
1233 return -XFS_ERROR(EFAULT);
1234
1235 if (flags & ~(LINUX_XFLAG_IMMUTABLE | LINUX_XFLAG_APPEND | \
1236 LINUX_XFLAG_NOATIME | LINUX_XFLAG_NODUMP | \
1237 LINUX_XFLAG_SYNC))
1238 return -XFS_ERROR(EOPNOTSUPP);
1239
1240 attr_flags = 0;
1241 if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
1242 attr_flags |= ATTR_NONBLOCK;
1243
1244 va.va_mask = XFS_AT_XFLAGS;
1245 va.va_xflags = xfs_merge_ioc_xflags(flags,
1246 xfs_ip2xflags(ip));
1247
1248 VOP_SETATTR(vp, &va, attr_flags, NULL, error);
1249 if (!error)
1250 vn_revalidate(vp); /* update Linux inode flags */
1251 return -error;
1252 }
1253
1254 case XFS_IOC_GETVERSION: {
1255 flags = LINVFS_GET_IP(vp)->i_generation;
1256 if (copy_to_user(arg, &flags, sizeof(flags)))
1257 return -XFS_ERROR(EFAULT);
1258 return 0;
1259 }
1260
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 default:
1262 return -ENOTTY;
1263 }
1264}
1265
1266STATIC int
1267xfs_ioc_getbmap(
1268 bhv_desc_t *bdp,
1269 struct file *filp,
1270 int ioflags,
1271 unsigned int cmd,
1272 void __user *arg)
1273{
1274 struct getbmap bm;
1275 int iflags;
1276 int error;
1277
1278 if (copy_from_user(&bm, arg, sizeof(bm)))
1279 return -XFS_ERROR(EFAULT);
1280
1281 if (bm.bmv_count < 2)
1282 return -XFS_ERROR(EINVAL);
1283
1284 iflags = (cmd == XFS_IOC_GETBMAPA ? BMV_IF_ATTRFORK : 0);
1285 if (ioflags & IO_INVIS)
1286 iflags |= BMV_IF_NO_DMAPI_READ;
1287
1288 error = xfs_getbmap(bdp, &bm, (struct getbmap __user *)arg+1, iflags);
1289 if (error)
1290 return -error;
1291
1292 if (copy_to_user(arg, &bm, sizeof(bm)))
1293 return -XFS_ERROR(EFAULT);
1294 return 0;
1295}
1296
1297STATIC int
1298xfs_ioc_getbmapx(
1299 bhv_desc_t *bdp,
1300 void __user *arg)
1301{
1302 struct getbmapx bmx;
1303 struct getbmap bm;
1304 int iflags;
1305 int error;
1306
1307 if (copy_from_user(&bmx, arg, sizeof(bmx)))
1308 return -XFS_ERROR(EFAULT);
1309
1310 if (bmx.bmv_count < 2)
1311 return -XFS_ERROR(EINVAL);
1312
1313 /*
1314 * Map input getbmapx structure to a getbmap
1315 * structure for xfs_getbmap.
1316 */
1317 GETBMAP_CONVERT(bmx, bm);
1318
1319 iflags = bmx.bmv_iflags;
1320
1321 if (iflags & (~BMV_IF_VALID))
1322 return -XFS_ERROR(EINVAL);
1323
1324 iflags |= BMV_IF_EXTENDED;
1325
1326 error = xfs_getbmap(bdp, &bm, (struct getbmapx __user *)arg+1, iflags);
1327 if (error)
1328 return -error;
1329
1330 GETBMAP_CONVERT(bm, bmx);
1331
1332 if (copy_to_user(arg, &bmx, sizeof(bmx)))
1333 return -XFS_ERROR(EFAULT);
1334
1335 return 0;
1336}