]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/xfs/linux-2.6/xfs_ioctl.c
[XFS] The forward declarations for the xfs_ioctl() helpers and the
[net-next-2.6.git] / fs / xfs / linux-2.6 / xfs_ioctl.c
CommitLineData
1da177e4 1/*
7b718769
NS
2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
1da177e4 4 *
7b718769
NS
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
1da177e4
LT
7 * published by the Free Software Foundation.
8 *
7b718769
NS
9 * 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.
1da177e4 13 *
7b718769
NS
14 * 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
1da177e4 17 */
1da177e4 18#include "xfs.h"
1da177e4 19#include "xfs_fs.h"
a844f451 20#include "xfs_bit.h"
1da177e4 21#include "xfs_log.h"
a844f451 22#include "xfs_inum.h"
1da177e4
LT
23#include "xfs_trans.h"
24#include "xfs_sb.h"
a844f451 25#include "xfs_ag.h"
1da177e4
LT
26#include "xfs_dir2.h"
27#include "xfs_alloc.h"
28#include "xfs_dmapi.h"
29#include "xfs_mount.h"
1da177e4 30#include "xfs_bmap_btree.h"
a844f451 31#include "xfs_alloc_btree.h"
1da177e4 32#include "xfs_ialloc_btree.h"
a844f451 33#include "xfs_attr_sf.h"
1da177e4
LT
34#include "xfs_dir2_sf.h"
35#include "xfs_dinode.h"
36#include "xfs_inode.h"
a844f451
NS
37#include "xfs_btree.h"
38#include "xfs_ialloc.h"
1da177e4 39#include "xfs_rtalloc.h"
1da177e4 40#include "xfs_itable.h"
a844f451 41#include "xfs_error.h"
1da177e4
LT
42#include "xfs_rw.h"
43#include "xfs_acl.h"
1da177e4 44#include "xfs_attr.h"
a844f451 45#include "xfs_bmap.h"
1da177e4
LT
46#include "xfs_buf_item.h"
47#include "xfs_utils.h"
48#include "xfs_dfrag.h"
49#include "xfs_fsops.h"
993386c1 50#include "xfs_vnodeops.h"
1da177e4 51
16f7e0fe 52#include <linux/capability.h>
1da177e4
LT
53#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;
1da177e4
LT
78
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
4ac91378
JB
94 ASSERT(nd.path.dentry);
95 ASSERT(nd.path.dentry->d_inode);
96 inode = igrab(nd.path.dentry->d_inode);
1d957f9b 97 path_put(&nd.path);
1da177e4
LT
98 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
e678fb0d
JJS
108 ASSERT(file->f_path.dentry);
109 ASSERT(file->f_path.dentry->d_inode);
110 inode = igrab(file->f_path.dentry->d_inode);
1da177e4
LT
111 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
0432dab2
CH
126 switch (inode->i_mode & S_IFMT) {
127 case S_IFREG:
128 case S_IFDIR:
129 case S_IFLNK:
130 break;
131 default:
1da177e4
LT
132 iput(inode);
133 return -XFS_ERROR(EBADF);
134 }
135
136 /* now we can grab the fsid */
0ce4cfd4 137 memcpy(&handle.ha_fsid, XFS_I(inode)->i_mount->m_fixedfsid,
2f6f7b3d 138 sizeof(xfs_fsid_t));
1da177e4
LT
139 hsize = sizeof(xfs_fsid_t);
140
141 if (cmd != XFS_IOC_PATH_TO_FSHANDLE) {
6e7f75ea 142 xfs_inode_t *ip = XFS_I(inode);
1da177e4
LT
143 int lock_mode;
144
145 /* need to get access to the xfs_inode to read the generation */
1da177e4
LT
146 lock_mode = xfs_ilock_map_shared(ip);
147
148 /* fill in fid section of handle from inode */
c6143911
CH
149 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;
1da177e4
LT
154
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/*
6e7f75ea
CH
173 * Convert userspace handle data into inode.
174 *
175 * 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.
1da177e4 178 *
6e7f75ea 179 * If no error, caller must always iput the returned inode.
1da177e4
LT
180 */
181STATIC int
182xfs_vget_fsop_handlereq(
183 xfs_mount_t *mp,
184 struct inode *parinode, /* parent inode pointer */
185 xfs_fsop_handlereq_t *hreq,
1da177e4
LT
186 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;
1da177e4
LT
194 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)) {
c6143911
CH
215 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)
1da177e4
LT
219 return XFS_ERROR(EINVAL);
220 }
221
222 /*
223 * Crack the handle, obtain the inode # & generation #
224 */
225 xfid = (struct xfs_fid *)&handlep->ha_fid;
c6143911
CH
226 if (xfid->fid_len == sizeof(*xfid) - sizeof(xfid->fid_len)) {
227 ino = xfid->fid_ino;
228 igen = xfid->fid_gen;
1da177e4
LT
229 } else {
230 return XFS_ERROR(EINVAL);
231 }
232
233 /*
6e7f75ea 234 * Get the XFS inode, building a Linux inode to go with it.
1da177e4
LT
235 */
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
1da177e4
LT
246 xfs_iunlock(ip, XFS_ILOCK_SHARED);
247
6e7f75ea 248 *inode = XFS_ITOV(ip);
1da177e4
LT
249 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;
1da177e4
LT
265 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
6e7f75ea 272 error = xfs_vget_fsop_handlereq(mp, parinode, &hreq, &inode);
1da177e4
LT
273 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. */
e678fb0d 322 mntget(parfilp->f_path.mnt);
1da177e4
LT
323
324 /* Create file pointer. */
e678fb0d 325 filp = dentry_open(dentry, parfilp->f_path.mnt, hreq.oflags);
1da177e4
LT
326 if (IS_ERR(filp)) {
327 put_unused_fd(new_fd);
328 return -XFS_ERROR(-PTR_ERR(filp));
329 }
2e2e7bb1
VA
330 if (inode->i_mode & S_IFREG) {
331 /* invisible operation should not change atime */
332 filp->f_flags |= O_NOATIME;
3562fd45 333 filp->f_op = &xfs_invis_file_operations;
2e2e7bb1 334 }
1da177e4
LT
335
336 fd_install(new_fd, filp);
337 return new_fd;
338}
339
804c83c3
CH
340/*
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
1da177e4
LT
366STATIC int
367xfs_readlink_by_handle(
368 xfs_mount_t *mp,
369 void __user *arg,
1da177e4
LT
370 struct inode *parinode)
371{
1da177e4
LT
372 struct inode *inode;
373 xfs_fsop_handlereq_t hreq;
1da177e4 374 __u32 olen;
804c83c3
CH
375 void *link;
376 int error;
1da177e4
LT
377
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
6e7f75ea 383 error = xfs_vget_fsop_handlereq(mp, parinode, &hreq, &inode);
1da177e4
LT
384 if (error)
385 return -error;
386
387 /* Restrict this handle operation to symlinks only. */
0432dab2 388 if (!S_ISLNK(inode->i_mode)) {
804c83c3
CH
389 error = -XFS_ERROR(EINVAL);
390 goto out_iput;
1da177e4
LT
391 }
392
393 if (copy_from_user(&olen, hreq.ohandlen, sizeof(__u32))) {
804c83c3
CH
394 error = -XFS_ERROR(EFAULT);
395 goto out_iput;
1da177e4 396 }
1da177e4 397
804c83c3
CH
398 link = kmalloc(MAXPATHLEN+1, GFP_KERNEL);
399 if (!link)
400 goto out_iput;
1da177e4 401
739bfb2a 402 error = -xfs_readlink(XFS_I(inode), link);
67fcaa73 403 if (error)
804c83c3
CH
404 goto out_kfree;
405 error = do_readlink(hreq.ohandle, olen, link);
406 if (error)
407 goto out_kfree;
67fcaa73 408
804c83c3
CH
409 out_kfree:
410 kfree(link);
411 out_iput:
412 iput(inode);
413 return error;
1da177e4
LT
414}
415
416STATIC int
417xfs_fssetdm_by_handle(
418 xfs_mount_t *mp,
419 void __user *arg,
1da177e4
LT
420 struct inode *parinode)
421{
422 int error;
423 struct fsdmidata fsd;
424 xfs_fsop_setdm_handlereq_t dmhreq;
425 struct inode *inode;
1da177e4
LT
426
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
6e7f75ea 432 error = xfs_vget_fsop_handlereq(mp, parinode, &dmhreq.hreq, &inode);
1da177e4
LT
433 if (error)
434 return -error;
435
436 if (IS_IMMUTABLE(inode) || IS_APPEND(inode)) {
6e7f75ea
CH
437 error = -XFS_ERROR(EPERM);
438 goto out;
1da177e4
LT
439 }
440
441 if (copy_from_user(&fsd, dmhreq.data, sizeof(fsd))) {
6e7f75ea
CH
442 error = -XFS_ERROR(EFAULT);
443 goto out;
1da177e4
LT
444 }
445
6e7f75ea
CH
446 error = -xfs_set_dmattrs(XFS_I(inode), fsd.fsd_dmevmask,
447 fsd.fsd_dmstate);
1da177e4 448
6e7f75ea
CH
449 out:
450 iput(inode);
451 return error;
1da177e4
LT
452}
453
454STATIC int
455xfs_attrlist_by_handle(
456 xfs_mount_t *mp,
457 void __user *arg,
1da177e4
LT
458 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;
1da177e4
LT
464 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
6e7f75ea 473 error = xfs_vget_fsop_handlereq(mp, parinode, &al_hreq.hreq, &inode);
1da177e4
LT
474 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;
739bfb2a
CH
482 error = xfs_attr_list(XFS_I(inode), kbuf, al_hreq.buflen,
483 al_hreq.flags, cursor);
1da177e4
LT
484 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:
6e7f75ea 493 iput(inode);
1da177e4
LT
494 out:
495 return -error;
496}
497
498STATIC int
499xfs_attrmulti_attr_get(
739bfb2a 500 struct inode *inode,
1da177e4
LT
501 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
a8272ce0 515 error = xfs_attr_get(XFS_I(inode), name, kbuf, (int *)len, flags, NULL);
1da177e4
LT
516 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(
739bfb2a 529 struct inode *inode,
1da177e4
LT
530 char *name,
531 const char __user *ubuf,
532 __uint32_t len,
533 __uint32_t flags)
534{
535 char *kbuf;
536 int error = EFAULT;
537
739bfb2a 538 if (IS_RDONLY(inode))
3542c6e1 539 return -EROFS;
739bfb2a 540 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
1da177e4
LT
541 return EPERM;
542 if (len > XATTR_SIZE_MAX)
543 return EINVAL;
544
545 kbuf = kmalloc(len, GFP_KERNEL);
546 if (!kbuf)
547 return ENOMEM;
548
549 if (copy_from_user(kbuf, ubuf, len))
550 goto out_kfree;
551
739bfb2a 552 error = xfs_attr_set(XFS_I(inode), name, kbuf, len, flags);
1da177e4
LT
553
554 out_kfree:
555 kfree(kbuf);
556 return error;
557}
558
559STATIC int
560xfs_attrmulti_attr_remove(
739bfb2a 561 struct inode *inode,
1da177e4
LT
562 char *name,
563 __uint32_t flags)
564{
739bfb2a 565 if (IS_RDONLY(inode))
3542c6e1 566 return -EROFS;
739bfb2a 567 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
1da177e4 568 return EPERM;
739bfb2a 569 return xfs_attr_remove(XFS_I(inode), name, flags);
1da177e4
LT
570}
571
572STATIC int
573xfs_attrmulti_by_handle(
574 xfs_mount_t *mp,
575 void __user *arg,
1da177e4
LT
576 struct inode *parinode)
577{
578 int error;
579 xfs_attr_multiop_t *ops;
580 xfs_fsop_attrmulti_handlereq_t am_hreq;
581 struct inode *inode;
1da177e4
LT
582 unsigned int i, size;
583 char *attr_name;
584
585 if (!capable(CAP_SYS_ADMIN))
586 return -XFS_ERROR(EPERM);
587 if (copy_from_user(&am_hreq, arg, sizeof(xfs_fsop_attrmulti_handlereq_t)))
588 return -XFS_ERROR(EFAULT);
589
6e7f75ea 590 error = xfs_vget_fsop_handlereq(mp, parinode, &am_hreq.hreq, &inode);
1da177e4
LT
591 if (error)
592 goto out;
593
594 error = E2BIG;
595 size = am_hreq.opcount * sizeof(attr_multiop_t);
596 if (!size || size > 16 * PAGE_SIZE)
597 goto out_vn_rele;
598
599 error = ENOMEM;
600 ops = kmalloc(size, GFP_KERNEL);
601 if (!ops)
602 goto out_vn_rele;
603
604 error = EFAULT;
605 if (copy_from_user(ops, am_hreq.ops, size))
606 goto out_kfree_ops;
607
608 attr_name = kmalloc(MAXNAMELEN, GFP_KERNEL);
609 if (!attr_name)
610 goto out_kfree_ops;
611
612
613 error = 0;
614 for (i = 0; i < am_hreq.opcount; i++) {
615 ops[i].am_error = strncpy_from_user(attr_name,
616 ops[i].am_attrname, MAXNAMELEN);
617 if (ops[i].am_error == 0 || ops[i].am_error == MAXNAMELEN)
618 error = -ERANGE;
619 if (ops[i].am_error < 0)
620 break;
621
622 switch (ops[i].am_opcode) {
623 case ATTR_OP_GET:
739bfb2a 624 ops[i].am_error = xfs_attrmulti_attr_get(inode,
1da177e4
LT
625 attr_name, ops[i].am_attrvalue,
626 &ops[i].am_length, ops[i].am_flags);
627 break;
628 case ATTR_OP_SET:
739bfb2a 629 ops[i].am_error = xfs_attrmulti_attr_set(inode,
1da177e4
LT
630 attr_name, ops[i].am_attrvalue,
631 ops[i].am_length, ops[i].am_flags);
632 break;
633 case ATTR_OP_REMOVE:
739bfb2a 634 ops[i].am_error = xfs_attrmulti_attr_remove(inode,
1da177e4
LT
635 attr_name, ops[i].am_flags);
636 break;
637 default:
638 ops[i].am_error = EINVAL;
639 }
640 }
641
642 if (copy_to_user(am_hreq.ops, ops, size))
643 error = XFS_ERROR(EFAULT);
644
645 kfree(attr_name);
646 out_kfree_ops:
647 kfree(ops);
648 out_vn_rele:
6e7f75ea 649 iput(inode);
1da177e4
LT
650 out:
651 return -error;
652}
653
1da177e4
LT
654STATIC int
655xfs_ioc_space(
993386c1 656 struct xfs_inode *ip,
f37ea149 657 struct inode *inode,
1da177e4
LT
658 struct file *filp,
659 int ioflags,
660 unsigned int cmd,
661 void __user *arg)
662{
663 xfs_flock64_t bf;
664 int attr_flags = 0;
665 int error;
666
f37ea149 667 if (inode->i_flags & (S_IMMUTABLE|S_APPEND))
1da177e4
LT
668 return -XFS_ERROR(EPERM);
669
ad4a8ac4 670 if (!(filp->f_mode & FMODE_WRITE))
1da177e4
LT
671 return -XFS_ERROR(EBADF);
672
f37ea149 673 if (!S_ISREG(inode->i_mode))
1da177e4
LT
674 return -XFS_ERROR(EINVAL);
675
676 if (copy_from_user(&bf, arg, sizeof(bf)))
677 return -XFS_ERROR(EFAULT);
678
679 if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
680 attr_flags |= ATTR_NONBLOCK;
681 if (ioflags & IO_INVIS)
682 attr_flags |= ATTR_DMI;
683
993386c1 684 error = xfs_change_file_space(ip, cmd, &bf, filp->f_pos,
1da177e4
LT
685 NULL, attr_flags);
686 return -error;
687}
688
689STATIC int
690xfs_ioc_bulkstat(
691 xfs_mount_t *mp,
692 unsigned int cmd,
693 void __user *arg)
694{
695 xfs_fsop_bulkreq_t bulkreq;
696 int count; /* # of records returned */
697 xfs_ino_t inlast; /* last inode number */
698 int done;
699 int error;
700
701 /* done = 1 if there are more stats to get and if bulkstat */
702 /* should be called again (unused here, but used in dmapi) */
703
704 if (!capable(CAP_SYS_ADMIN))
705 return -EPERM;
706
707 if (XFS_FORCED_SHUTDOWN(mp))
708 return -XFS_ERROR(EIO);
709
710 if (copy_from_user(&bulkreq, arg, sizeof(xfs_fsop_bulkreq_t)))
711 return -XFS_ERROR(EFAULT);
712
713 if (copy_from_user(&inlast, bulkreq.lastip, sizeof(__s64)))
714 return -XFS_ERROR(EFAULT);
715
716 if ((count = bulkreq.icount) <= 0)
717 return -XFS_ERROR(EINVAL);
718
cd57e594
LM
719 if (bulkreq.ubuffer == NULL)
720 return -XFS_ERROR(EINVAL);
721
1da177e4
LT
722 if (cmd == XFS_IOC_FSINUMBERS)
723 error = xfs_inumbers(mp, &inlast, &count,
faa63e95 724 bulkreq.ubuffer, xfs_inumbers_fmt);
1da177e4
LT
725 else if (cmd == XFS_IOC_FSBULKSTAT_SINGLE)
726 error = xfs_bulkstat_single(mp, &inlast,
727 bulkreq.ubuffer, &done);
cd57e594
LM
728 else /* XFS_IOC_FSBULKSTAT */
729 error = xfs_bulkstat(mp, &inlast, &count,
730 (bulkstat_one_pf)xfs_bulkstat_one, NULL,
731 sizeof(xfs_bstat_t), bulkreq.ubuffer,
732 BULKSTAT_FG_QUICK, &done);
1da177e4
LT
733
734 if (error)
735 return -error;
736
737 if (bulkreq.ocount != NULL) {
738 if (copy_to_user(bulkreq.lastip, &inlast,
739 sizeof(xfs_ino_t)))
740 return -XFS_ERROR(EFAULT);
741
742 if (copy_to_user(bulkreq.ocount, &count, sizeof(count)))
743 return -XFS_ERROR(EFAULT);
744 }
745
746 return 0;
747}
748
749STATIC int
750xfs_ioc_fsgeometry_v1(
751 xfs_mount_t *mp,
752 void __user *arg)
753{
754 xfs_fsop_geom_v1_t fsgeo;
755 int error;
756
757 error = xfs_fs_geometry(mp, (xfs_fsop_geom_t *)&fsgeo, 3);
758 if (error)
759 return -error;
760
761 if (copy_to_user(arg, &fsgeo, sizeof(fsgeo)))
762 return -XFS_ERROR(EFAULT);
763 return 0;
764}
765
766STATIC int
767xfs_ioc_fsgeometry(
768 xfs_mount_t *mp,
769 void __user *arg)
770{
771 xfs_fsop_geom_t fsgeo;
772 int error;
773
774 error = xfs_fs_geometry(mp, &fsgeo, 4);
775 if (error)
776 return -error;
777
778 if (copy_to_user(arg, &fsgeo, sizeof(fsgeo)))
779 return -XFS_ERROR(EFAULT);
780 return 0;
781}
782
783/*
784 * Linux extended inode flags interface.
785 */
1da177e4
LT
786
787STATIC unsigned int
788xfs_merge_ioc_xflags(
789 unsigned int flags,
790 unsigned int start)
791{
792 unsigned int xflags = start;
793
39058a0e 794 if (flags & FS_IMMUTABLE_FL)
1da177e4
LT
795 xflags |= XFS_XFLAG_IMMUTABLE;
796 else
797 xflags &= ~XFS_XFLAG_IMMUTABLE;
39058a0e 798 if (flags & FS_APPEND_FL)
1da177e4
LT
799 xflags |= XFS_XFLAG_APPEND;
800 else
801 xflags &= ~XFS_XFLAG_APPEND;
39058a0e 802 if (flags & FS_SYNC_FL)
1da177e4
LT
803 xflags |= XFS_XFLAG_SYNC;
804 else
805 xflags &= ~XFS_XFLAG_SYNC;
39058a0e 806 if (flags & FS_NOATIME_FL)
1da177e4
LT
807 xflags |= XFS_XFLAG_NOATIME;
808 else
809 xflags &= ~XFS_XFLAG_NOATIME;
39058a0e 810 if (flags & FS_NODUMP_FL)
1da177e4
LT
811 xflags |= XFS_XFLAG_NODUMP;
812 else
813 xflags &= ~XFS_XFLAG_NODUMP;
814
815 return xflags;
816}
817
818STATIC unsigned int
819xfs_di2lxflags(
820 __uint16_t di_flags)
821{
822 unsigned int flags = 0;
823
824 if (di_flags & XFS_DIFLAG_IMMUTABLE)
39058a0e 825 flags |= FS_IMMUTABLE_FL;
1da177e4 826 if (di_flags & XFS_DIFLAG_APPEND)
39058a0e 827 flags |= FS_APPEND_FL;
1da177e4 828 if (di_flags & XFS_DIFLAG_SYNC)
39058a0e 829 flags |= FS_SYNC_FL;
1da177e4 830 if (di_flags & XFS_DIFLAG_NOATIME)
39058a0e 831 flags |= FS_NOATIME_FL;
1da177e4 832 if (di_flags & XFS_DIFLAG_NODUMP)
39058a0e 833 flags |= FS_NODUMP_FL;
1da177e4
LT
834 return flags;
835}
836
c83bfab1
CH
837STATIC int
838xfs_ioc_fsgetxattr(
839 xfs_inode_t *ip,
840 int attr,
841 void __user *arg)
842{
843 struct fsxattr fa;
844
845 xfs_ilock(ip, XFS_ILOCK_SHARED);
846 fa.fsx_xflags = xfs_ip2xflags(ip);
847 fa.fsx_extsize = ip->i_d.di_extsize << ip->i_mount->m_sb.sb_blocklog;
848 fa.fsx_projid = ip->i_d.di_projid;
849
850 if (attr) {
851 if (ip->i_afp) {
852 if (ip->i_afp->if_flags & XFS_IFEXTENTS)
853 fa.fsx_nextents = ip->i_afp->if_bytes /
854 sizeof(xfs_bmbt_rec_t);
855 else
856 fa.fsx_nextents = ip->i_d.di_anextents;
857 } else
858 fa.fsx_nextents = 0;
859 } else {
860 if (ip->i_df.if_flags & XFS_IFEXTENTS)
861 fa.fsx_nextents = ip->i_df.if_bytes /
862 sizeof(xfs_bmbt_rec_t);
863 else
864 fa.fsx_nextents = ip->i_d.di_nextents;
865 }
866 xfs_iunlock(ip, XFS_ILOCK_SHARED);
867
868 if (copy_to_user(arg, &fa, sizeof(fa)))
869 return -EFAULT;
870 return 0;
871}
872
1da177e4 873STATIC int
df26cfe8 874xfs_ioc_fssetxattr(
1da177e4
LT
875 xfs_inode_t *ip,
876 struct file *filp,
1da177e4
LT
877 void __user *arg)
878{
879 struct fsxattr fa;
8285fb58 880 struct bhv_vattr *vattr;
df26cfe8 881 int error;
1da177e4 882 int attr_flags;
df26cfe8
LM
883
884 if (copy_from_user(&fa, arg, sizeof(fa)))
885 return -EFAULT;
1da177e4 886
220b5284
NS
887 vattr = kmalloc(sizeof(*vattr), GFP_KERNEL);
888 if (unlikely(!vattr))
889 return -ENOMEM;
890
df26cfe8
LM
891 attr_flags = 0;
892 if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
893 attr_flags |= ATTR_NONBLOCK;
1da177e4 894
df26cfe8
LM
895 vattr->va_mask = XFS_AT_XFLAGS | XFS_AT_EXTSIZE | XFS_AT_PROJID;
896 vattr->va_xflags = fa.fsx_xflags;
897 vattr->va_extsize = fa.fsx_extsize;
898 vattr->va_projid = fa.fsx_projid;
1da177e4 899
df26cfe8
LM
900 error = -xfs_setattr(ip, vattr, attr_flags, NULL);
901 if (!error)
902 vn_revalidate(XFS_ITOV(ip)); /* update flags */
903 kfree(vattr);
904 return 0;
905}
1da177e4 906
df26cfe8
LM
907STATIC int
908xfs_ioc_getxflags(
909 xfs_inode_t *ip,
910 void __user *arg)
911{
912 unsigned int flags;
1da177e4 913
df26cfe8
LM
914 flags = xfs_di2lxflags(ip->i_d.di_flags);
915 if (copy_to_user(arg, &flags, sizeof(flags)))
916 return -EFAULT;
917 return 0;
918}
1da177e4 919
df26cfe8
LM
920STATIC int
921xfs_ioc_setxflags(
922 xfs_inode_t *ip,
923 struct file *filp,
924 void __user *arg)
925{
926 struct bhv_vattr *vattr;
927 unsigned int flags;
928 int attr_flags;
929 int error;
1da177e4 930
df26cfe8
LM
931 if (copy_from_user(&flags, arg, sizeof(flags)))
932 return -EFAULT;
1da177e4 933
df26cfe8
LM
934 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
935 FS_NOATIME_FL | FS_NODUMP_FL | \
936 FS_SYNC_FL))
937 return -EOPNOTSUPP;
1da177e4 938
df26cfe8
LM
939 vattr = kmalloc(sizeof(*vattr), GFP_KERNEL);
940 if (unlikely(!vattr))
941 return -ENOMEM;
1da177e4 942
df26cfe8
LM
943 attr_flags = 0;
944 if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
945 attr_flags |= ATTR_NONBLOCK;
1da177e4 946
df26cfe8
LM
947 vattr->va_mask = XFS_AT_XFLAGS;
948 vattr->va_xflags = xfs_merge_ioc_xflags(flags, xfs_ip2xflags(ip));
220b5284 949
df26cfe8
LM
950 error = -xfs_setattr(ip, vattr, attr_flags, NULL);
951 if (likely(!error))
952 vn_revalidate(XFS_ITOV(ip)); /* update flags */
220b5284
NS
953 kfree(vattr);
954 return error;
1da177e4
LT
955}
956
957STATIC int
958xfs_ioc_getbmap(
993386c1 959 struct xfs_inode *ip,
1da177e4
LT
960 int ioflags,
961 unsigned int cmd,
962 void __user *arg)
963{
964 struct getbmap bm;
965 int iflags;
966 int error;
967
968 if (copy_from_user(&bm, arg, sizeof(bm)))
969 return -XFS_ERROR(EFAULT);
970
971 if (bm.bmv_count < 2)
972 return -XFS_ERROR(EINVAL);
973
974 iflags = (cmd == XFS_IOC_GETBMAPA ? BMV_IF_ATTRFORK : 0);
975 if (ioflags & IO_INVIS)
976 iflags |= BMV_IF_NO_DMAPI_READ;
977
993386c1 978 error = xfs_getbmap(ip, &bm, (struct getbmap __user *)arg+1, iflags);
1da177e4
LT
979 if (error)
980 return -error;
981
982 if (copy_to_user(arg, &bm, sizeof(bm)))
983 return -XFS_ERROR(EFAULT);
984 return 0;
985}
986
987STATIC int
988xfs_ioc_getbmapx(
993386c1 989 struct xfs_inode *ip,
1da177e4
LT
990 void __user *arg)
991{
992 struct getbmapx bmx;
993 struct getbmap bm;
994 int iflags;
995 int error;
996
997 if (copy_from_user(&bmx, arg, sizeof(bmx)))
998 return -XFS_ERROR(EFAULT);
999
1000 if (bmx.bmv_count < 2)
1001 return -XFS_ERROR(EINVAL);
1002
1003 /*
1004 * Map input getbmapx structure to a getbmap
1005 * structure for xfs_getbmap.
1006 */
1007 GETBMAP_CONVERT(bmx, bm);
1008
1009 iflags = bmx.bmv_iflags;
1010
1011 if (iflags & (~BMV_IF_VALID))
1012 return -XFS_ERROR(EINVAL);
1013
1014 iflags |= BMV_IF_EXTENDED;
1015
993386c1 1016 error = xfs_getbmap(ip, &bm, (struct getbmapx __user *)arg+1, iflags);
1da177e4
LT
1017 if (error)
1018 return -error;
1019
1020 GETBMAP_CONVERT(bm, bmx);
1021
1022 if (copy_to_user(arg, &bmx, sizeof(bmx)))
1023 return -XFS_ERROR(EFAULT);
1024
1025 return 0;
1026}
df26cfe8
LM
1027
1028int
1029xfs_ioctl(
1030 xfs_inode_t *ip,
1031 struct file *filp,
1032 int ioflags,
1033 unsigned int cmd,
1034 void __user *arg)
1035{
1036 struct inode *inode = filp->f_path.dentry->d_inode;
1037 xfs_mount_t *mp = ip->i_mount;
1038 int error;
1039
1040 xfs_itrace_entry(XFS_I(inode));
1041 switch (cmd) {
1042
1043 case XFS_IOC_ALLOCSP:
1044 case XFS_IOC_FREESP:
1045 case XFS_IOC_RESVSP:
1046 case XFS_IOC_UNRESVSP:
1047 case XFS_IOC_ALLOCSP64:
1048 case XFS_IOC_FREESP64:
1049 case XFS_IOC_RESVSP64:
1050 case XFS_IOC_UNRESVSP64:
1051 /*
1052 * Only allow the sys admin to reserve space unless
1053 * unwritten extents are enabled.
1054 */
1055 if (!xfs_sb_version_hasextflgbit(&mp->m_sb) &&
1056 !capable(CAP_SYS_ADMIN))
1057 return -EPERM;
1058
1059 return xfs_ioc_space(ip, inode, filp, ioflags, cmd, arg);
1060
1061 case XFS_IOC_DIOINFO: {
1062 struct dioattr da;
1063 xfs_buftarg_t *target =
1064 XFS_IS_REALTIME_INODE(ip) ?
1065 mp->m_rtdev_targp : mp->m_ddev_targp;
1066
1067 da.d_mem = da.d_miniosz = 1 << target->bt_sshift;
1068 da.d_maxiosz = INT_MAX & ~(da.d_miniosz - 1);
1069
1070 if (copy_to_user(arg, &da, sizeof(da)))
1071 return -XFS_ERROR(EFAULT);
1072 return 0;
1073 }
1074
1075 case XFS_IOC_FSBULKSTAT_SINGLE:
1076 case XFS_IOC_FSBULKSTAT:
1077 case XFS_IOC_FSINUMBERS:
1078 return xfs_ioc_bulkstat(mp, cmd, arg);
1079
1080 case XFS_IOC_FSGEOMETRY_V1:
1081 return xfs_ioc_fsgeometry_v1(mp, arg);
1082
1083 case XFS_IOC_FSGEOMETRY:
1084 return xfs_ioc_fsgeometry(mp, arg);
1085
1086 case XFS_IOC_GETVERSION:
1087 return put_user(inode->i_generation, (int __user *)arg);
1088
1089 case XFS_IOC_FSGETXATTR:
1090 return xfs_ioc_fsgetxattr(ip, 0, arg);
1091 case XFS_IOC_FSGETXATTRA:
1092 return xfs_ioc_fsgetxattr(ip, 1, arg);
df26cfe8 1093 case XFS_IOC_GETXFLAGS:
df26cfe8 1094 case XFS_IOC_SETXFLAGS:
3b2816be
LM
1095 case XFS_IOC_FSSETXATTR:
1096 return xfs_ioc_xattr(ip, filp, cmd, arg);
df26cfe8
LM
1097
1098 case XFS_IOC_FSSETDM: {
1099 struct fsdmidata dmi;
1100
1101 if (copy_from_user(&dmi, arg, sizeof(dmi)))
1102 return -XFS_ERROR(EFAULT);
1103
1104 error = xfs_set_dmattrs(ip, dmi.fsd_dmevmask,
1105 dmi.fsd_dmstate);
1106 return -error;
1107 }
1108
1109 case XFS_IOC_GETBMAP:
1110 case XFS_IOC_GETBMAPA:
1111 return xfs_ioc_getbmap(ip, ioflags, cmd, arg);
1112
1113 case XFS_IOC_GETBMAPX:
1114 return xfs_ioc_getbmapx(ip, arg);
1115
1116 case XFS_IOC_FD_TO_HANDLE:
1117 case XFS_IOC_PATH_TO_HANDLE:
1118 case XFS_IOC_PATH_TO_FSHANDLE:
1119 return xfs_find_handle(cmd, arg);
1120
1121 case XFS_IOC_OPEN_BY_HANDLE:
1122 return xfs_open_by_handle(mp, arg, filp, inode);
1123
1124 case XFS_IOC_FSSETDM_BY_HANDLE:
1125 return xfs_fssetdm_by_handle(mp, arg, inode);
1126
1127 case XFS_IOC_READLINK_BY_HANDLE:
1128 return xfs_readlink_by_handle(mp, arg, inode);
1129
1130 case XFS_IOC_ATTRLIST_BY_HANDLE:
1131 return xfs_attrlist_by_handle(mp, arg, inode);
1132
1133 case XFS_IOC_ATTRMULTI_BY_HANDLE:
1134 return xfs_attrmulti_by_handle(mp, arg, inode);
1135
1136 case XFS_IOC_SWAPEXT: {
1137 error = xfs_swapext((struct xfs_swapext __user *)arg);
1138 return -error;
1139 }
1140
1141 case XFS_IOC_FSCOUNTS: {
1142 xfs_fsop_counts_t out;
1143
1144 error = xfs_fs_counts(mp, &out);
1145 if (error)
1146 return -error;
1147
1148 if (copy_to_user(arg, &out, sizeof(out)))
1149 return -XFS_ERROR(EFAULT);
1150 return 0;
1151 }
1152
1153 case XFS_IOC_SET_RESBLKS: {
1154 xfs_fsop_resblks_t inout;
1155 __uint64_t in;
1156
1157 if (!capable(CAP_SYS_ADMIN))
1158 return -EPERM;
1159
1160 if (copy_from_user(&inout, arg, sizeof(inout)))
1161 return -XFS_ERROR(EFAULT);
1162
1163 /* input parameter is passed in resblks field of structure */
1164 in = inout.resblks;
1165 error = xfs_reserve_blocks(mp, &in, &inout);
1166 if (error)
1167 return -error;
1168
1169 if (copy_to_user(arg, &inout, sizeof(inout)))
1170 return -XFS_ERROR(EFAULT);
1171 return 0;
1172 }
1173
1174 case XFS_IOC_GET_RESBLKS: {
1175 xfs_fsop_resblks_t out;
1176
1177 if (!capable(CAP_SYS_ADMIN))
1178 return -EPERM;
1179
1180 error = xfs_reserve_blocks(mp, NULL, &out);
1181 if (error)
1182 return -error;
1183
1184 if (copy_to_user(arg, &out, sizeof(out)))
1185 return -XFS_ERROR(EFAULT);
1186
1187 return 0;
1188 }
1189
1190 case XFS_IOC_FSGROWFSDATA: {
1191 xfs_growfs_data_t in;
1192
1193 if (!capable(CAP_SYS_ADMIN))
1194 return -EPERM;
1195
1196 if (copy_from_user(&in, arg, sizeof(in)))
1197 return -XFS_ERROR(EFAULT);
1198
1199 error = xfs_growfs_data(mp, &in);
1200 return -error;
1201 }
1202
1203 case XFS_IOC_FSGROWFSLOG: {
1204 xfs_growfs_log_t in;
1205
1206 if (!capable(CAP_SYS_ADMIN))
1207 return -EPERM;
1208
1209 if (copy_from_user(&in, arg, sizeof(in)))
1210 return -XFS_ERROR(EFAULT);
1211
1212 error = xfs_growfs_log(mp, &in);
1213 return -error;
1214 }
1215
1216 case XFS_IOC_FSGROWFSRT: {
1217 xfs_growfs_rt_t in;
1218
1219 if (!capable(CAP_SYS_ADMIN))
1220 return -EPERM;
1221
1222 if (copy_from_user(&in, arg, sizeof(in)))
1223 return -XFS_ERROR(EFAULT);
1224
1225 error = xfs_growfs_rt(mp, &in);
1226 return -error;
1227 }
1228
1229 case XFS_IOC_FREEZE:
1230 if (!capable(CAP_SYS_ADMIN))
1231 return -EPERM;
1232
1233 if (inode->i_sb->s_frozen == SB_UNFROZEN)
1234 freeze_bdev(inode->i_sb->s_bdev);
1235 return 0;
1236
1237 case XFS_IOC_THAW:
1238 if (!capable(CAP_SYS_ADMIN))
1239 return -EPERM;
1240 if (inode->i_sb->s_frozen != SB_UNFROZEN)
1241 thaw_bdev(inode->i_sb->s_bdev, inode->i_sb);
1242 return 0;
1243
1244 case XFS_IOC_GOINGDOWN: {
1245 __uint32_t in;
1246
1247 if (!capable(CAP_SYS_ADMIN))
1248 return -EPERM;
1249
1250 if (get_user(in, (__uint32_t __user *)arg))
1251 return -XFS_ERROR(EFAULT);
1252
1253 error = xfs_fs_goingdown(mp, in);
1254 return -error;
1255 }
1256
1257 case XFS_IOC_ERROR_INJECTION: {
1258 xfs_error_injection_t in;
1259
1260 if (!capable(CAP_SYS_ADMIN))
1261 return -EPERM;
1262
1263 if (copy_from_user(&in, arg, sizeof(in)))
1264 return -XFS_ERROR(EFAULT);
1265
1266 error = xfs_errortag_add(in.errtag, mp);
1267 return -error;
1268 }
1269
1270 case XFS_IOC_ERROR_CLEARALL:
1271 if (!capable(CAP_SYS_ADMIN))
1272 return -EPERM;
1273
1274 error = xfs_errortag_clearall(mp, 1);
1275 return -error;
1276
1277 default:
1278 return -ENOTTY;
1279 }
1280}