xfs: create xfs_bmap_util.[ch]
[deliverable/linux.git] / fs / xfs / xfs_ioctl.c
1 /*
2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
4 *
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
7 * published by the Free Software Foundation.
8 *
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.
13 *
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
17 */
18 #include "xfs.h"
19 #include "xfs_fs.h"
20 #include "xfs_format.h"
21 #include "xfs_log.h"
22 #include "xfs_trans.h"
23 #include "xfs_sb.h"
24 #include "xfs_ag.h"
25 #include "xfs_alloc.h"
26 #include "xfs_mount.h"
27 #include "xfs_bmap_btree.h"
28 #include "xfs_dinode.h"
29 #include "xfs_inode.h"
30 #include "xfs_ioctl.h"
31 #include "xfs_rtalloc.h"
32 #include "xfs_itable.h"
33 #include "xfs_error.h"
34 #include "xfs_attr.h"
35 #include "xfs_bmap.h"
36 #include "xfs_bmap_util.h"
37 #include "xfs_buf_item.h"
38 #include "xfs_utils.h"
39 #include "xfs_dfrag.h"
40 #include "xfs_fsops.h"
41 #include "xfs_vnodeops.h"
42 #include "xfs_discard.h"
43 #include "xfs_quota.h"
44 #include "xfs_inode_item.h"
45 #include "xfs_export.h"
46 #include "xfs_trace.h"
47 #include "xfs_icache.h"
48
49 #include <linux/capability.h>
50 #include <linux/dcache.h>
51 #include <linux/mount.h>
52 #include <linux/namei.h>
53 #include <linux/pagemap.h>
54 #include <linux/slab.h>
55 #include <linux/exportfs.h>
56
57 /*
58 * xfs_find_handle maps from userspace xfs_fsop_handlereq structure to
59 * a file or fs handle.
60 *
61 * XFS_IOC_PATH_TO_FSHANDLE
62 * returns fs handle for a mount point or path within that mount point
63 * XFS_IOC_FD_TO_HANDLE
64 * returns full handle for a FD opened in user space
65 * XFS_IOC_PATH_TO_HANDLE
66 * returns full handle for a path
67 */
68 int
69 xfs_find_handle(
70 unsigned int cmd,
71 xfs_fsop_handlereq_t *hreq)
72 {
73 int hsize;
74 xfs_handle_t handle;
75 struct inode *inode;
76 struct fd f = {0};
77 struct path path;
78 int error;
79 struct xfs_inode *ip;
80
81 if (cmd == XFS_IOC_FD_TO_HANDLE) {
82 f = fdget(hreq->fd);
83 if (!f.file)
84 return -EBADF;
85 inode = file_inode(f.file);
86 } else {
87 error = user_lpath((const char __user *)hreq->path, &path);
88 if (error)
89 return error;
90 inode = path.dentry->d_inode;
91 }
92 ip = XFS_I(inode);
93
94 /*
95 * We can only generate handles for inodes residing on a XFS filesystem,
96 * and only for regular files, directories or symbolic links.
97 */
98 error = -EINVAL;
99 if (inode->i_sb->s_magic != XFS_SB_MAGIC)
100 goto out_put;
101
102 error = -EBADF;
103 if (!S_ISREG(inode->i_mode) &&
104 !S_ISDIR(inode->i_mode) &&
105 !S_ISLNK(inode->i_mode))
106 goto out_put;
107
108
109 memcpy(&handle.ha_fsid, ip->i_mount->m_fixedfsid, sizeof(xfs_fsid_t));
110
111 if (cmd == XFS_IOC_PATH_TO_FSHANDLE) {
112 /*
113 * This handle only contains an fsid, zero the rest.
114 */
115 memset(&handle.ha_fid, 0, sizeof(handle.ha_fid));
116 hsize = sizeof(xfs_fsid_t);
117 } else {
118 int lock_mode;
119
120 lock_mode = xfs_ilock_map_shared(ip);
121 handle.ha_fid.fid_len = sizeof(xfs_fid_t) -
122 sizeof(handle.ha_fid.fid_len);
123 handle.ha_fid.fid_pad = 0;
124 handle.ha_fid.fid_gen = ip->i_d.di_gen;
125 handle.ha_fid.fid_ino = ip->i_ino;
126 xfs_iunlock_map_shared(ip, lock_mode);
127
128 hsize = XFS_HSIZE(handle);
129 }
130
131 error = -EFAULT;
132 if (copy_to_user(hreq->ohandle, &handle, hsize) ||
133 copy_to_user(hreq->ohandlen, &hsize, sizeof(__s32)))
134 goto out_put;
135
136 error = 0;
137
138 out_put:
139 if (cmd == XFS_IOC_FD_TO_HANDLE)
140 fdput(f);
141 else
142 path_put(&path);
143 return error;
144 }
145
146 /*
147 * No need to do permission checks on the various pathname components
148 * as the handle operations are privileged.
149 */
150 STATIC int
151 xfs_handle_acceptable(
152 void *context,
153 struct dentry *dentry)
154 {
155 return 1;
156 }
157
158 /*
159 * Convert userspace handle data into a dentry.
160 */
161 struct dentry *
162 xfs_handle_to_dentry(
163 struct file *parfilp,
164 void __user *uhandle,
165 u32 hlen)
166 {
167 xfs_handle_t handle;
168 struct xfs_fid64 fid;
169
170 /*
171 * Only allow handle opens under a directory.
172 */
173 if (!S_ISDIR(file_inode(parfilp)->i_mode))
174 return ERR_PTR(-ENOTDIR);
175
176 if (hlen != sizeof(xfs_handle_t))
177 return ERR_PTR(-EINVAL);
178 if (copy_from_user(&handle, uhandle, hlen))
179 return ERR_PTR(-EFAULT);
180 if (handle.ha_fid.fid_len !=
181 sizeof(handle.ha_fid) - sizeof(handle.ha_fid.fid_len))
182 return ERR_PTR(-EINVAL);
183
184 memset(&fid, 0, sizeof(struct fid));
185 fid.ino = handle.ha_fid.fid_ino;
186 fid.gen = handle.ha_fid.fid_gen;
187
188 return exportfs_decode_fh(parfilp->f_path.mnt, (struct fid *)&fid, 3,
189 FILEID_INO32_GEN | XFS_FILEID_TYPE_64FLAG,
190 xfs_handle_acceptable, NULL);
191 }
192
193 STATIC struct dentry *
194 xfs_handlereq_to_dentry(
195 struct file *parfilp,
196 xfs_fsop_handlereq_t *hreq)
197 {
198 return xfs_handle_to_dentry(parfilp, hreq->ihandle, hreq->ihandlen);
199 }
200
201 int
202 xfs_open_by_handle(
203 struct file *parfilp,
204 xfs_fsop_handlereq_t *hreq)
205 {
206 const struct cred *cred = current_cred();
207 int error;
208 int fd;
209 int permflag;
210 struct file *filp;
211 struct inode *inode;
212 struct dentry *dentry;
213 fmode_t fmode;
214 struct path path;
215
216 if (!capable(CAP_SYS_ADMIN))
217 return -XFS_ERROR(EPERM);
218
219 dentry = xfs_handlereq_to_dentry(parfilp, hreq);
220 if (IS_ERR(dentry))
221 return PTR_ERR(dentry);
222 inode = dentry->d_inode;
223
224 /* Restrict xfs_open_by_handle to directories & regular files. */
225 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode))) {
226 error = -XFS_ERROR(EPERM);
227 goto out_dput;
228 }
229
230 #if BITS_PER_LONG != 32
231 hreq->oflags |= O_LARGEFILE;
232 #endif
233
234 permflag = hreq->oflags;
235 fmode = OPEN_FMODE(permflag);
236 if ((!(permflag & O_APPEND) || (permflag & O_TRUNC)) &&
237 (fmode & FMODE_WRITE) && IS_APPEND(inode)) {
238 error = -XFS_ERROR(EPERM);
239 goto out_dput;
240 }
241
242 if ((fmode & FMODE_WRITE) && IS_IMMUTABLE(inode)) {
243 error = -XFS_ERROR(EACCES);
244 goto out_dput;
245 }
246
247 /* Can't write directories. */
248 if (S_ISDIR(inode->i_mode) && (fmode & FMODE_WRITE)) {
249 error = -XFS_ERROR(EISDIR);
250 goto out_dput;
251 }
252
253 fd = get_unused_fd_flags(0);
254 if (fd < 0) {
255 error = fd;
256 goto out_dput;
257 }
258
259 path.mnt = parfilp->f_path.mnt;
260 path.dentry = dentry;
261 filp = dentry_open(&path, hreq->oflags, cred);
262 dput(dentry);
263 if (IS_ERR(filp)) {
264 put_unused_fd(fd);
265 return PTR_ERR(filp);
266 }
267
268 if (S_ISREG(inode->i_mode)) {
269 filp->f_flags |= O_NOATIME;
270 filp->f_mode |= FMODE_NOCMTIME;
271 }
272
273 fd_install(fd, filp);
274 return fd;
275
276 out_dput:
277 dput(dentry);
278 return error;
279 }
280
281 /*
282 * This is a copy from fs/namei.c:vfs_readlink(), except for removing it's
283 * unused first argument.
284 */
285 STATIC int
286 do_readlink(
287 char __user *buffer,
288 int buflen,
289 const char *link)
290 {
291 int len;
292
293 len = PTR_ERR(link);
294 if (IS_ERR(link))
295 goto out;
296
297 len = strlen(link);
298 if (len > (unsigned) buflen)
299 len = buflen;
300 if (copy_to_user(buffer, link, len))
301 len = -EFAULT;
302 out:
303 return len;
304 }
305
306
307 int
308 xfs_readlink_by_handle(
309 struct file *parfilp,
310 xfs_fsop_handlereq_t *hreq)
311 {
312 struct dentry *dentry;
313 __u32 olen;
314 void *link;
315 int error;
316
317 if (!capable(CAP_SYS_ADMIN))
318 return -XFS_ERROR(EPERM);
319
320 dentry = xfs_handlereq_to_dentry(parfilp, hreq);
321 if (IS_ERR(dentry))
322 return PTR_ERR(dentry);
323
324 /* Restrict this handle operation to symlinks only. */
325 if (!S_ISLNK(dentry->d_inode->i_mode)) {
326 error = -XFS_ERROR(EINVAL);
327 goto out_dput;
328 }
329
330 if (copy_from_user(&olen, hreq->ohandlen, sizeof(__u32))) {
331 error = -XFS_ERROR(EFAULT);
332 goto out_dput;
333 }
334
335 link = kmalloc(MAXPATHLEN+1, GFP_KERNEL);
336 if (!link) {
337 error = -XFS_ERROR(ENOMEM);
338 goto out_dput;
339 }
340
341 error = -xfs_readlink(XFS_I(dentry->d_inode), link);
342 if (error)
343 goto out_kfree;
344 error = do_readlink(hreq->ohandle, olen, link);
345 if (error)
346 goto out_kfree;
347
348 out_kfree:
349 kfree(link);
350 out_dput:
351 dput(dentry);
352 return error;
353 }
354
355 STATIC int
356 xfs_fssetdm_by_handle(
357 struct file *parfilp,
358 void __user *arg)
359 {
360 int error;
361 struct fsdmidata fsd;
362 xfs_fsop_setdm_handlereq_t dmhreq;
363 struct dentry *dentry;
364
365 if (!capable(CAP_MKNOD))
366 return -XFS_ERROR(EPERM);
367 if (copy_from_user(&dmhreq, arg, sizeof(xfs_fsop_setdm_handlereq_t)))
368 return -XFS_ERROR(EFAULT);
369
370 error = mnt_want_write_file(parfilp);
371 if (error)
372 return error;
373
374 dentry = xfs_handlereq_to_dentry(parfilp, &dmhreq.hreq);
375 if (IS_ERR(dentry)) {
376 mnt_drop_write_file(parfilp);
377 return PTR_ERR(dentry);
378 }
379
380 if (IS_IMMUTABLE(dentry->d_inode) || IS_APPEND(dentry->d_inode)) {
381 error = -XFS_ERROR(EPERM);
382 goto out;
383 }
384
385 if (copy_from_user(&fsd, dmhreq.data, sizeof(fsd))) {
386 error = -XFS_ERROR(EFAULT);
387 goto out;
388 }
389
390 error = -xfs_set_dmattrs(XFS_I(dentry->d_inode), fsd.fsd_dmevmask,
391 fsd.fsd_dmstate);
392
393 out:
394 mnt_drop_write_file(parfilp);
395 dput(dentry);
396 return error;
397 }
398
399 STATIC int
400 xfs_attrlist_by_handle(
401 struct file *parfilp,
402 void __user *arg)
403 {
404 int error = -ENOMEM;
405 attrlist_cursor_kern_t *cursor;
406 xfs_fsop_attrlist_handlereq_t al_hreq;
407 struct dentry *dentry;
408 char *kbuf;
409
410 if (!capable(CAP_SYS_ADMIN))
411 return -XFS_ERROR(EPERM);
412 if (copy_from_user(&al_hreq, arg, sizeof(xfs_fsop_attrlist_handlereq_t)))
413 return -XFS_ERROR(EFAULT);
414 if (al_hreq.buflen > XATTR_LIST_MAX)
415 return -XFS_ERROR(EINVAL);
416
417 /*
418 * Reject flags, only allow namespaces.
419 */
420 if (al_hreq.flags & ~(ATTR_ROOT | ATTR_SECURE))
421 return -XFS_ERROR(EINVAL);
422
423 dentry = xfs_handlereq_to_dentry(parfilp, &al_hreq.hreq);
424 if (IS_ERR(dentry))
425 return PTR_ERR(dentry);
426
427 kbuf = kmem_zalloc(al_hreq.buflen, KM_SLEEP | KM_MAYFAIL);
428 if (!kbuf) {
429 kbuf = kmem_zalloc_large(al_hreq.buflen);
430 if (!kbuf)
431 goto out_dput;
432 }
433
434 cursor = (attrlist_cursor_kern_t *)&al_hreq.pos;
435 error = -xfs_attr_list(XFS_I(dentry->d_inode), kbuf, al_hreq.buflen,
436 al_hreq.flags, cursor);
437 if (error)
438 goto out_kfree;
439
440 if (copy_to_user(al_hreq.buffer, kbuf, al_hreq.buflen))
441 error = -EFAULT;
442
443 out_kfree:
444 if (is_vmalloc_addr(kbuf))
445 kmem_free_large(kbuf);
446 else
447 kmem_free(kbuf);
448 out_dput:
449 dput(dentry);
450 return error;
451 }
452
453 int
454 xfs_attrmulti_attr_get(
455 struct inode *inode,
456 unsigned char *name,
457 unsigned char __user *ubuf,
458 __uint32_t *len,
459 __uint32_t flags)
460 {
461 unsigned char *kbuf;
462 int error = EFAULT;
463
464 if (*len > XATTR_SIZE_MAX)
465 return EINVAL;
466 kbuf = kmem_zalloc(*len, KM_SLEEP | KM_MAYFAIL);
467 if (!kbuf) {
468 kbuf = kmem_zalloc_large(*len);
469 if (!kbuf)
470 return ENOMEM;
471 }
472
473 error = xfs_attr_get(XFS_I(inode), name, kbuf, (int *)len, flags);
474 if (error)
475 goto out_kfree;
476
477 if (copy_to_user(ubuf, kbuf, *len))
478 error = EFAULT;
479
480 out_kfree:
481 if (is_vmalloc_addr(kbuf))
482 kmem_free_large(kbuf);
483 else
484 kmem_free(kbuf);
485 return error;
486 }
487
488 int
489 xfs_attrmulti_attr_set(
490 struct inode *inode,
491 unsigned char *name,
492 const unsigned char __user *ubuf,
493 __uint32_t len,
494 __uint32_t flags)
495 {
496 unsigned char *kbuf;
497 int error = EFAULT;
498
499 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
500 return EPERM;
501 if (len > XATTR_SIZE_MAX)
502 return EINVAL;
503
504 kbuf = memdup_user(ubuf, len);
505 if (IS_ERR(kbuf))
506 return PTR_ERR(kbuf);
507
508 error = xfs_attr_set(XFS_I(inode), name, kbuf, len, flags);
509
510 return error;
511 }
512
513 int
514 xfs_attrmulti_attr_remove(
515 struct inode *inode,
516 unsigned char *name,
517 __uint32_t flags)
518 {
519 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
520 return EPERM;
521 return xfs_attr_remove(XFS_I(inode), name, flags);
522 }
523
524 STATIC int
525 xfs_attrmulti_by_handle(
526 struct file *parfilp,
527 void __user *arg)
528 {
529 int error;
530 xfs_attr_multiop_t *ops;
531 xfs_fsop_attrmulti_handlereq_t am_hreq;
532 struct dentry *dentry;
533 unsigned int i, size;
534 unsigned char *attr_name;
535
536 if (!capable(CAP_SYS_ADMIN))
537 return -XFS_ERROR(EPERM);
538 if (copy_from_user(&am_hreq, arg, sizeof(xfs_fsop_attrmulti_handlereq_t)))
539 return -XFS_ERROR(EFAULT);
540
541 /* overflow check */
542 if (am_hreq.opcount >= INT_MAX / sizeof(xfs_attr_multiop_t))
543 return -E2BIG;
544
545 dentry = xfs_handlereq_to_dentry(parfilp, &am_hreq.hreq);
546 if (IS_ERR(dentry))
547 return PTR_ERR(dentry);
548
549 error = E2BIG;
550 size = am_hreq.opcount * sizeof(xfs_attr_multiop_t);
551 if (!size || size > 16 * PAGE_SIZE)
552 goto out_dput;
553
554 ops = memdup_user(am_hreq.ops, size);
555 if (IS_ERR(ops)) {
556 error = PTR_ERR(ops);
557 goto out_dput;
558 }
559
560 attr_name = kmalloc(MAXNAMELEN, GFP_KERNEL);
561 if (!attr_name)
562 goto out_kfree_ops;
563
564 error = 0;
565 for (i = 0; i < am_hreq.opcount; i++) {
566 ops[i].am_error = strncpy_from_user((char *)attr_name,
567 ops[i].am_attrname, MAXNAMELEN);
568 if (ops[i].am_error == 0 || ops[i].am_error == MAXNAMELEN)
569 error = -ERANGE;
570 if (ops[i].am_error < 0)
571 break;
572
573 switch (ops[i].am_opcode) {
574 case ATTR_OP_GET:
575 ops[i].am_error = xfs_attrmulti_attr_get(
576 dentry->d_inode, attr_name,
577 ops[i].am_attrvalue, &ops[i].am_length,
578 ops[i].am_flags);
579 break;
580 case ATTR_OP_SET:
581 ops[i].am_error = mnt_want_write_file(parfilp);
582 if (ops[i].am_error)
583 break;
584 ops[i].am_error = xfs_attrmulti_attr_set(
585 dentry->d_inode, attr_name,
586 ops[i].am_attrvalue, ops[i].am_length,
587 ops[i].am_flags);
588 mnt_drop_write_file(parfilp);
589 break;
590 case ATTR_OP_REMOVE:
591 ops[i].am_error = mnt_want_write_file(parfilp);
592 if (ops[i].am_error)
593 break;
594 ops[i].am_error = xfs_attrmulti_attr_remove(
595 dentry->d_inode, attr_name,
596 ops[i].am_flags);
597 mnt_drop_write_file(parfilp);
598 break;
599 default:
600 ops[i].am_error = EINVAL;
601 }
602 }
603
604 if (copy_to_user(am_hreq.ops, ops, size))
605 error = XFS_ERROR(EFAULT);
606
607 kfree(attr_name);
608 out_kfree_ops:
609 kfree(ops);
610 out_dput:
611 dput(dentry);
612 return -error;
613 }
614
615 int
616 xfs_ioc_space(
617 struct xfs_inode *ip,
618 struct inode *inode,
619 struct file *filp,
620 int ioflags,
621 unsigned int cmd,
622 xfs_flock64_t *bf)
623 {
624 int attr_flags = 0;
625 int error;
626
627 /*
628 * Only allow the sys admin to reserve space unless
629 * unwritten extents are enabled.
630 */
631 if (!xfs_sb_version_hasextflgbit(&ip->i_mount->m_sb) &&
632 !capable(CAP_SYS_ADMIN))
633 return -XFS_ERROR(EPERM);
634
635 if (inode->i_flags & (S_IMMUTABLE|S_APPEND))
636 return -XFS_ERROR(EPERM);
637
638 if (!(filp->f_mode & FMODE_WRITE))
639 return -XFS_ERROR(EBADF);
640
641 if (!S_ISREG(inode->i_mode))
642 return -XFS_ERROR(EINVAL);
643
644 if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
645 attr_flags |= XFS_ATTR_NONBLOCK;
646
647 if (filp->f_flags & O_DSYNC)
648 attr_flags |= XFS_ATTR_SYNC;
649
650 if (ioflags & IO_INVIS)
651 attr_flags |= XFS_ATTR_DMI;
652
653 error = mnt_want_write_file(filp);
654 if (error)
655 return error;
656 error = xfs_change_file_space(ip, cmd, bf, filp->f_pos, attr_flags);
657 mnt_drop_write_file(filp);
658 return -error;
659 }
660
661 STATIC int
662 xfs_ioc_bulkstat(
663 xfs_mount_t *mp,
664 unsigned int cmd,
665 void __user *arg)
666 {
667 xfs_fsop_bulkreq_t bulkreq;
668 int count; /* # of records returned */
669 xfs_ino_t inlast; /* last inode number */
670 int done;
671 int error;
672
673 /* done = 1 if there are more stats to get and if bulkstat */
674 /* should be called again (unused here, but used in dmapi) */
675
676 if (!capable(CAP_SYS_ADMIN))
677 return -EPERM;
678
679 if (XFS_FORCED_SHUTDOWN(mp))
680 return -XFS_ERROR(EIO);
681
682 if (copy_from_user(&bulkreq, arg, sizeof(xfs_fsop_bulkreq_t)))
683 return -XFS_ERROR(EFAULT);
684
685 if (copy_from_user(&inlast, bulkreq.lastip, sizeof(__s64)))
686 return -XFS_ERROR(EFAULT);
687
688 if ((count = bulkreq.icount) <= 0)
689 return -XFS_ERROR(EINVAL);
690
691 if (bulkreq.ubuffer == NULL)
692 return -XFS_ERROR(EINVAL);
693
694 if (cmd == XFS_IOC_FSINUMBERS)
695 error = xfs_inumbers(mp, &inlast, &count,
696 bulkreq.ubuffer, xfs_inumbers_fmt);
697 else if (cmd == XFS_IOC_FSBULKSTAT_SINGLE)
698 error = xfs_bulkstat_single(mp, &inlast,
699 bulkreq.ubuffer, &done);
700 else /* XFS_IOC_FSBULKSTAT */
701 error = xfs_bulkstat(mp, &inlast, &count, xfs_bulkstat_one,
702 sizeof(xfs_bstat_t), bulkreq.ubuffer,
703 &done);
704
705 if (error)
706 return -error;
707
708 if (bulkreq.ocount != NULL) {
709 if (copy_to_user(bulkreq.lastip, &inlast,
710 sizeof(xfs_ino_t)))
711 return -XFS_ERROR(EFAULT);
712
713 if (copy_to_user(bulkreq.ocount, &count, sizeof(count)))
714 return -XFS_ERROR(EFAULT);
715 }
716
717 return 0;
718 }
719
720 STATIC int
721 xfs_ioc_fsgeometry_v1(
722 xfs_mount_t *mp,
723 void __user *arg)
724 {
725 xfs_fsop_geom_t fsgeo;
726 int error;
727
728 error = xfs_fs_geometry(mp, &fsgeo, 3);
729 if (error)
730 return -error;
731
732 /*
733 * Caller should have passed an argument of type
734 * xfs_fsop_geom_v1_t. This is a proper subset of the
735 * xfs_fsop_geom_t that xfs_fs_geometry() fills in.
736 */
737 if (copy_to_user(arg, &fsgeo, sizeof(xfs_fsop_geom_v1_t)))
738 return -XFS_ERROR(EFAULT);
739 return 0;
740 }
741
742 STATIC int
743 xfs_ioc_fsgeometry(
744 xfs_mount_t *mp,
745 void __user *arg)
746 {
747 xfs_fsop_geom_t fsgeo;
748 int error;
749
750 error = xfs_fs_geometry(mp, &fsgeo, 4);
751 if (error)
752 return -error;
753
754 if (copy_to_user(arg, &fsgeo, sizeof(fsgeo)))
755 return -XFS_ERROR(EFAULT);
756 return 0;
757 }
758
759 /*
760 * Linux extended inode flags interface.
761 */
762
763 STATIC unsigned int
764 xfs_merge_ioc_xflags(
765 unsigned int flags,
766 unsigned int start)
767 {
768 unsigned int xflags = start;
769
770 if (flags & FS_IMMUTABLE_FL)
771 xflags |= XFS_XFLAG_IMMUTABLE;
772 else
773 xflags &= ~XFS_XFLAG_IMMUTABLE;
774 if (flags & FS_APPEND_FL)
775 xflags |= XFS_XFLAG_APPEND;
776 else
777 xflags &= ~XFS_XFLAG_APPEND;
778 if (flags & FS_SYNC_FL)
779 xflags |= XFS_XFLAG_SYNC;
780 else
781 xflags &= ~XFS_XFLAG_SYNC;
782 if (flags & FS_NOATIME_FL)
783 xflags |= XFS_XFLAG_NOATIME;
784 else
785 xflags &= ~XFS_XFLAG_NOATIME;
786 if (flags & FS_NODUMP_FL)
787 xflags |= XFS_XFLAG_NODUMP;
788 else
789 xflags &= ~XFS_XFLAG_NODUMP;
790
791 return xflags;
792 }
793
794 STATIC unsigned int
795 xfs_di2lxflags(
796 __uint16_t di_flags)
797 {
798 unsigned int flags = 0;
799
800 if (di_flags & XFS_DIFLAG_IMMUTABLE)
801 flags |= FS_IMMUTABLE_FL;
802 if (di_flags & XFS_DIFLAG_APPEND)
803 flags |= FS_APPEND_FL;
804 if (di_flags & XFS_DIFLAG_SYNC)
805 flags |= FS_SYNC_FL;
806 if (di_flags & XFS_DIFLAG_NOATIME)
807 flags |= FS_NOATIME_FL;
808 if (di_flags & XFS_DIFLAG_NODUMP)
809 flags |= FS_NODUMP_FL;
810 return flags;
811 }
812
813 STATIC int
814 xfs_ioc_fsgetxattr(
815 xfs_inode_t *ip,
816 int attr,
817 void __user *arg)
818 {
819 struct fsxattr fa;
820
821 memset(&fa, 0, sizeof(struct fsxattr));
822
823 xfs_ilock(ip, XFS_ILOCK_SHARED);
824 fa.fsx_xflags = xfs_ip2xflags(ip);
825 fa.fsx_extsize = ip->i_d.di_extsize << ip->i_mount->m_sb.sb_blocklog;
826 fa.fsx_projid = xfs_get_projid(ip);
827
828 if (attr) {
829 if (ip->i_afp) {
830 if (ip->i_afp->if_flags & XFS_IFEXTENTS)
831 fa.fsx_nextents = ip->i_afp->if_bytes /
832 sizeof(xfs_bmbt_rec_t);
833 else
834 fa.fsx_nextents = ip->i_d.di_anextents;
835 } else
836 fa.fsx_nextents = 0;
837 } else {
838 if (ip->i_df.if_flags & XFS_IFEXTENTS)
839 fa.fsx_nextents = ip->i_df.if_bytes /
840 sizeof(xfs_bmbt_rec_t);
841 else
842 fa.fsx_nextents = ip->i_d.di_nextents;
843 }
844 xfs_iunlock(ip, XFS_ILOCK_SHARED);
845
846 if (copy_to_user(arg, &fa, sizeof(fa)))
847 return -EFAULT;
848 return 0;
849 }
850
851 STATIC void
852 xfs_set_diflags(
853 struct xfs_inode *ip,
854 unsigned int xflags)
855 {
856 unsigned int di_flags;
857
858 /* can't set PREALLOC this way, just preserve it */
859 di_flags = (ip->i_d.di_flags & XFS_DIFLAG_PREALLOC);
860 if (xflags & XFS_XFLAG_IMMUTABLE)
861 di_flags |= XFS_DIFLAG_IMMUTABLE;
862 if (xflags & XFS_XFLAG_APPEND)
863 di_flags |= XFS_DIFLAG_APPEND;
864 if (xflags & XFS_XFLAG_SYNC)
865 di_flags |= XFS_DIFLAG_SYNC;
866 if (xflags & XFS_XFLAG_NOATIME)
867 di_flags |= XFS_DIFLAG_NOATIME;
868 if (xflags & XFS_XFLAG_NODUMP)
869 di_flags |= XFS_DIFLAG_NODUMP;
870 if (xflags & XFS_XFLAG_PROJINHERIT)
871 di_flags |= XFS_DIFLAG_PROJINHERIT;
872 if (xflags & XFS_XFLAG_NODEFRAG)
873 di_flags |= XFS_DIFLAG_NODEFRAG;
874 if (xflags & XFS_XFLAG_FILESTREAM)
875 di_flags |= XFS_DIFLAG_FILESTREAM;
876 if (S_ISDIR(ip->i_d.di_mode)) {
877 if (xflags & XFS_XFLAG_RTINHERIT)
878 di_flags |= XFS_DIFLAG_RTINHERIT;
879 if (xflags & XFS_XFLAG_NOSYMLINKS)
880 di_flags |= XFS_DIFLAG_NOSYMLINKS;
881 if (xflags & XFS_XFLAG_EXTSZINHERIT)
882 di_flags |= XFS_DIFLAG_EXTSZINHERIT;
883 } else if (S_ISREG(ip->i_d.di_mode)) {
884 if (xflags & XFS_XFLAG_REALTIME)
885 di_flags |= XFS_DIFLAG_REALTIME;
886 if (xflags & XFS_XFLAG_EXTSIZE)
887 di_flags |= XFS_DIFLAG_EXTSIZE;
888 }
889
890 ip->i_d.di_flags = di_flags;
891 }
892
893 STATIC void
894 xfs_diflags_to_linux(
895 struct xfs_inode *ip)
896 {
897 struct inode *inode = VFS_I(ip);
898 unsigned int xflags = xfs_ip2xflags(ip);
899
900 if (xflags & XFS_XFLAG_IMMUTABLE)
901 inode->i_flags |= S_IMMUTABLE;
902 else
903 inode->i_flags &= ~S_IMMUTABLE;
904 if (xflags & XFS_XFLAG_APPEND)
905 inode->i_flags |= S_APPEND;
906 else
907 inode->i_flags &= ~S_APPEND;
908 if (xflags & XFS_XFLAG_SYNC)
909 inode->i_flags |= S_SYNC;
910 else
911 inode->i_flags &= ~S_SYNC;
912 if (xflags & XFS_XFLAG_NOATIME)
913 inode->i_flags |= S_NOATIME;
914 else
915 inode->i_flags &= ~S_NOATIME;
916 }
917
918 #define FSX_PROJID 1
919 #define FSX_EXTSIZE 2
920 #define FSX_XFLAGS 4
921 #define FSX_NONBLOCK 8
922
923 STATIC int
924 xfs_ioctl_setattr(
925 xfs_inode_t *ip,
926 struct fsxattr *fa,
927 int mask)
928 {
929 struct xfs_mount *mp = ip->i_mount;
930 struct xfs_trans *tp;
931 unsigned int lock_flags = 0;
932 struct xfs_dquot *udqp = NULL;
933 struct xfs_dquot *pdqp = NULL;
934 struct xfs_dquot *olddquot = NULL;
935 int code;
936
937 trace_xfs_ioctl_setattr(ip);
938
939 if (mp->m_flags & XFS_MOUNT_RDONLY)
940 return XFS_ERROR(EROFS);
941 if (XFS_FORCED_SHUTDOWN(mp))
942 return XFS_ERROR(EIO);
943
944 /*
945 * Disallow 32bit project ids when projid32bit feature is not enabled.
946 */
947 if ((mask & FSX_PROJID) && (fa->fsx_projid > (__uint16_t)-1) &&
948 !xfs_sb_version_hasprojid32bit(&ip->i_mount->m_sb))
949 return XFS_ERROR(EINVAL);
950
951 /*
952 * If disk quotas is on, we make sure that the dquots do exist on disk,
953 * before we start any other transactions. Trying to do this later
954 * is messy. We don't care to take a readlock to look at the ids
955 * in inode here, because we can't hold it across the trans_reserve.
956 * If the IDs do change before we take the ilock, we're covered
957 * because the i_*dquot fields will get updated anyway.
958 */
959 if (XFS_IS_QUOTA_ON(mp) && (mask & FSX_PROJID)) {
960 code = xfs_qm_vop_dqalloc(ip, ip->i_d.di_uid,
961 ip->i_d.di_gid, fa->fsx_projid,
962 XFS_QMOPT_PQUOTA, &udqp, NULL, &pdqp);
963 if (code)
964 return code;
965 }
966
967 /*
968 * For the other attributes, we acquire the inode lock and
969 * first do an error checking pass.
970 */
971 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
972 code = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES(mp), 0, 0, 0);
973 if (code)
974 goto error_return;
975
976 lock_flags = XFS_ILOCK_EXCL;
977 xfs_ilock(ip, lock_flags);
978
979 /*
980 * CAP_FOWNER overrides the following restrictions:
981 *
982 * The user ID of the calling process must be equal
983 * to the file owner ID, except in cases where the
984 * CAP_FSETID capability is applicable.
985 */
986 if (current_fsuid() != ip->i_d.di_uid && !capable(CAP_FOWNER)) {
987 code = XFS_ERROR(EPERM);
988 goto error_return;
989 }
990
991 /*
992 * Do a quota reservation only if projid is actually going to change.
993 */
994 if (mask & FSX_PROJID) {
995 if (XFS_IS_QUOTA_RUNNING(mp) &&
996 XFS_IS_PQUOTA_ON(mp) &&
997 xfs_get_projid(ip) != fa->fsx_projid) {
998 ASSERT(tp);
999 code = xfs_qm_vop_chown_reserve(tp, ip, udqp, NULL,
1000 pdqp, capable(CAP_FOWNER) ?
1001 XFS_QMOPT_FORCE_RES : 0);
1002 if (code) /* out of quota */
1003 goto error_return;
1004 }
1005 }
1006
1007 if (mask & FSX_EXTSIZE) {
1008 /*
1009 * Can't change extent size if any extents are allocated.
1010 */
1011 if (ip->i_d.di_nextents &&
1012 ((ip->i_d.di_extsize << mp->m_sb.sb_blocklog) !=
1013 fa->fsx_extsize)) {
1014 code = XFS_ERROR(EINVAL); /* EFBIG? */
1015 goto error_return;
1016 }
1017
1018 /*
1019 * Extent size must be a multiple of the appropriate block
1020 * size, if set at all. It must also be smaller than the
1021 * maximum extent size supported by the filesystem.
1022 *
1023 * Also, for non-realtime files, limit the extent size hint to
1024 * half the size of the AGs in the filesystem so alignment
1025 * doesn't result in extents larger than an AG.
1026 */
1027 if (fa->fsx_extsize != 0) {
1028 xfs_extlen_t size;
1029 xfs_fsblock_t extsize_fsb;
1030
1031 extsize_fsb = XFS_B_TO_FSB(mp, fa->fsx_extsize);
1032 if (extsize_fsb > MAXEXTLEN) {
1033 code = XFS_ERROR(EINVAL);
1034 goto error_return;
1035 }
1036
1037 if (XFS_IS_REALTIME_INODE(ip) ||
1038 ((mask & FSX_XFLAGS) &&
1039 (fa->fsx_xflags & XFS_XFLAG_REALTIME))) {
1040 size = mp->m_sb.sb_rextsize <<
1041 mp->m_sb.sb_blocklog;
1042 } else {
1043 size = mp->m_sb.sb_blocksize;
1044 if (extsize_fsb > mp->m_sb.sb_agblocks / 2) {
1045 code = XFS_ERROR(EINVAL);
1046 goto error_return;
1047 }
1048 }
1049
1050 if (fa->fsx_extsize % size) {
1051 code = XFS_ERROR(EINVAL);
1052 goto error_return;
1053 }
1054 }
1055 }
1056
1057
1058 if (mask & FSX_XFLAGS) {
1059 /*
1060 * Can't change realtime flag if any extents are allocated.
1061 */
1062 if ((ip->i_d.di_nextents || ip->i_delayed_blks) &&
1063 (XFS_IS_REALTIME_INODE(ip)) !=
1064 (fa->fsx_xflags & XFS_XFLAG_REALTIME)) {
1065 code = XFS_ERROR(EINVAL); /* EFBIG? */
1066 goto error_return;
1067 }
1068
1069 /*
1070 * If realtime flag is set then must have realtime data.
1071 */
1072 if ((fa->fsx_xflags & XFS_XFLAG_REALTIME)) {
1073 if ((mp->m_sb.sb_rblocks == 0) ||
1074 (mp->m_sb.sb_rextsize == 0) ||
1075 (ip->i_d.di_extsize % mp->m_sb.sb_rextsize)) {
1076 code = XFS_ERROR(EINVAL);
1077 goto error_return;
1078 }
1079 }
1080
1081 /*
1082 * Can't modify an immutable/append-only file unless
1083 * we have appropriate permission.
1084 */
1085 if ((ip->i_d.di_flags &
1086 (XFS_DIFLAG_IMMUTABLE|XFS_DIFLAG_APPEND) ||
1087 (fa->fsx_xflags &
1088 (XFS_XFLAG_IMMUTABLE | XFS_XFLAG_APPEND))) &&
1089 !capable(CAP_LINUX_IMMUTABLE)) {
1090 code = XFS_ERROR(EPERM);
1091 goto error_return;
1092 }
1093 }
1094
1095 xfs_trans_ijoin(tp, ip, 0);
1096
1097 /*
1098 * Change file ownership. Must be the owner or privileged.
1099 */
1100 if (mask & FSX_PROJID) {
1101 /*
1102 * CAP_FSETID overrides the following restrictions:
1103 *
1104 * The set-user-ID and set-group-ID bits of a file will be
1105 * cleared upon successful return from chown()
1106 */
1107 if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
1108 !capable(CAP_FSETID))
1109 ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
1110
1111 /*
1112 * Change the ownerships and register quota modifications
1113 * in the transaction.
1114 */
1115 if (xfs_get_projid(ip) != fa->fsx_projid) {
1116 if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_PQUOTA_ON(mp)) {
1117 olddquot = xfs_qm_vop_chown(tp, ip,
1118 &ip->i_pdquot, pdqp);
1119 }
1120 xfs_set_projid(ip, fa->fsx_projid);
1121
1122 /*
1123 * We may have to rev the inode as well as
1124 * the superblock version number since projids didn't
1125 * exist before DINODE_VERSION_2 and SB_VERSION_NLINK.
1126 */
1127 if (ip->i_d.di_version == 1)
1128 xfs_bump_ino_vers2(tp, ip);
1129 }
1130
1131 }
1132
1133 if (mask & FSX_EXTSIZE)
1134 ip->i_d.di_extsize = fa->fsx_extsize >> mp->m_sb.sb_blocklog;
1135 if (mask & FSX_XFLAGS) {
1136 xfs_set_diflags(ip, fa->fsx_xflags);
1137 xfs_diflags_to_linux(ip);
1138 }
1139
1140 xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
1141 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1142
1143 XFS_STATS_INC(xs_ig_attrchg);
1144
1145 /*
1146 * If this is a synchronous mount, make sure that the
1147 * transaction goes to disk before returning to the user.
1148 * This is slightly sub-optimal in that truncates require
1149 * two sync transactions instead of one for wsync filesystems.
1150 * One for the truncate and one for the timestamps since we
1151 * don't want to change the timestamps unless we're sure the
1152 * truncate worked. Truncates are less than 1% of the laddis
1153 * mix so this probably isn't worth the trouble to optimize.
1154 */
1155 if (mp->m_flags & XFS_MOUNT_WSYNC)
1156 xfs_trans_set_sync(tp);
1157 code = xfs_trans_commit(tp, 0);
1158 xfs_iunlock(ip, lock_flags);
1159
1160 /*
1161 * Release any dquot(s) the inode had kept before chown.
1162 */
1163 xfs_qm_dqrele(olddquot);
1164 xfs_qm_dqrele(udqp);
1165 xfs_qm_dqrele(pdqp);
1166
1167 return code;
1168
1169 error_return:
1170 xfs_qm_dqrele(udqp);
1171 xfs_qm_dqrele(pdqp);
1172 xfs_trans_cancel(tp, 0);
1173 if (lock_flags)
1174 xfs_iunlock(ip, lock_flags);
1175 return code;
1176 }
1177
1178 STATIC int
1179 xfs_ioc_fssetxattr(
1180 xfs_inode_t *ip,
1181 struct file *filp,
1182 void __user *arg)
1183 {
1184 struct fsxattr fa;
1185 unsigned int mask;
1186 int error;
1187
1188 if (copy_from_user(&fa, arg, sizeof(fa)))
1189 return -EFAULT;
1190
1191 mask = FSX_XFLAGS | FSX_EXTSIZE | FSX_PROJID;
1192 if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
1193 mask |= FSX_NONBLOCK;
1194
1195 error = mnt_want_write_file(filp);
1196 if (error)
1197 return error;
1198 error = xfs_ioctl_setattr(ip, &fa, mask);
1199 mnt_drop_write_file(filp);
1200 return -error;
1201 }
1202
1203 STATIC int
1204 xfs_ioc_getxflags(
1205 xfs_inode_t *ip,
1206 void __user *arg)
1207 {
1208 unsigned int flags;
1209
1210 flags = xfs_di2lxflags(ip->i_d.di_flags);
1211 if (copy_to_user(arg, &flags, sizeof(flags)))
1212 return -EFAULT;
1213 return 0;
1214 }
1215
1216 STATIC int
1217 xfs_ioc_setxflags(
1218 xfs_inode_t *ip,
1219 struct file *filp,
1220 void __user *arg)
1221 {
1222 struct fsxattr fa;
1223 unsigned int flags;
1224 unsigned int mask;
1225 int error;
1226
1227 if (copy_from_user(&flags, arg, sizeof(flags)))
1228 return -EFAULT;
1229
1230 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
1231 FS_NOATIME_FL | FS_NODUMP_FL | \
1232 FS_SYNC_FL))
1233 return -EOPNOTSUPP;
1234
1235 mask = FSX_XFLAGS;
1236 if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
1237 mask |= FSX_NONBLOCK;
1238 fa.fsx_xflags = xfs_merge_ioc_xflags(flags, xfs_ip2xflags(ip));
1239
1240 error = mnt_want_write_file(filp);
1241 if (error)
1242 return error;
1243 error = xfs_ioctl_setattr(ip, &fa, mask);
1244 mnt_drop_write_file(filp);
1245 return -error;
1246 }
1247
1248 STATIC int
1249 xfs_getbmap_format(void **ap, struct getbmapx *bmv, int *full)
1250 {
1251 struct getbmap __user *base = *ap;
1252
1253 /* copy only getbmap portion (not getbmapx) */
1254 if (copy_to_user(base, bmv, sizeof(struct getbmap)))
1255 return XFS_ERROR(EFAULT);
1256
1257 *ap += sizeof(struct getbmap);
1258 return 0;
1259 }
1260
1261 STATIC int
1262 xfs_ioc_getbmap(
1263 struct xfs_inode *ip,
1264 int ioflags,
1265 unsigned int cmd,
1266 void __user *arg)
1267 {
1268 struct getbmapx bmx;
1269 int error;
1270
1271 if (copy_from_user(&bmx, arg, sizeof(struct getbmapx)))
1272 return -XFS_ERROR(EFAULT);
1273
1274 if (bmx.bmv_count < 2)
1275 return -XFS_ERROR(EINVAL);
1276
1277 bmx.bmv_iflags = (cmd == XFS_IOC_GETBMAPA ? BMV_IF_ATTRFORK : 0);
1278 if (ioflags & IO_INVIS)
1279 bmx.bmv_iflags |= BMV_IF_NO_DMAPI_READ;
1280
1281 error = xfs_getbmap(ip, &bmx, xfs_getbmap_format,
1282 (struct getbmap *)arg+1);
1283 if (error)
1284 return -error;
1285
1286 /* copy back header - only size of getbmap */
1287 if (copy_to_user(arg, &bmx, sizeof(struct getbmap)))
1288 return -XFS_ERROR(EFAULT);
1289 return 0;
1290 }
1291
1292 STATIC int
1293 xfs_getbmapx_format(void **ap, struct getbmapx *bmv, int *full)
1294 {
1295 struct getbmapx __user *base = *ap;
1296
1297 if (copy_to_user(base, bmv, sizeof(struct getbmapx)))
1298 return XFS_ERROR(EFAULT);
1299
1300 *ap += sizeof(struct getbmapx);
1301 return 0;
1302 }
1303
1304 STATIC int
1305 xfs_ioc_getbmapx(
1306 struct xfs_inode *ip,
1307 void __user *arg)
1308 {
1309 struct getbmapx bmx;
1310 int error;
1311
1312 if (copy_from_user(&bmx, arg, sizeof(bmx)))
1313 return -XFS_ERROR(EFAULT);
1314
1315 if (bmx.bmv_count < 2)
1316 return -XFS_ERROR(EINVAL);
1317
1318 if (bmx.bmv_iflags & (~BMV_IF_VALID))
1319 return -XFS_ERROR(EINVAL);
1320
1321 error = xfs_getbmap(ip, &bmx, xfs_getbmapx_format,
1322 (struct getbmapx *)arg+1);
1323 if (error)
1324 return -error;
1325
1326 /* copy back header */
1327 if (copy_to_user(arg, &bmx, sizeof(struct getbmapx)))
1328 return -XFS_ERROR(EFAULT);
1329
1330 return 0;
1331 }
1332
1333 /*
1334 * Note: some of the ioctl's return positive numbers as a
1335 * byte count indicating success, such as readlink_by_handle.
1336 * So we don't "sign flip" like most other routines. This means
1337 * true errors need to be returned as a negative value.
1338 */
1339 long
1340 xfs_file_ioctl(
1341 struct file *filp,
1342 unsigned int cmd,
1343 unsigned long p)
1344 {
1345 struct inode *inode = file_inode(filp);
1346 struct xfs_inode *ip = XFS_I(inode);
1347 struct xfs_mount *mp = ip->i_mount;
1348 void __user *arg = (void __user *)p;
1349 int ioflags = 0;
1350 int error;
1351
1352 if (filp->f_mode & FMODE_NOCMTIME)
1353 ioflags |= IO_INVIS;
1354
1355 trace_xfs_file_ioctl(ip);
1356
1357 switch (cmd) {
1358 case FITRIM:
1359 return xfs_ioc_trim(mp, arg);
1360 case XFS_IOC_ALLOCSP:
1361 case XFS_IOC_FREESP:
1362 case XFS_IOC_RESVSP:
1363 case XFS_IOC_UNRESVSP:
1364 case XFS_IOC_ALLOCSP64:
1365 case XFS_IOC_FREESP64:
1366 case XFS_IOC_RESVSP64:
1367 case XFS_IOC_UNRESVSP64:
1368 case XFS_IOC_ZERO_RANGE: {
1369 xfs_flock64_t bf;
1370
1371 if (copy_from_user(&bf, arg, sizeof(bf)))
1372 return -XFS_ERROR(EFAULT);
1373 return xfs_ioc_space(ip, inode, filp, ioflags, cmd, &bf);
1374 }
1375 case XFS_IOC_DIOINFO: {
1376 struct dioattr da;
1377 xfs_buftarg_t *target =
1378 XFS_IS_REALTIME_INODE(ip) ?
1379 mp->m_rtdev_targp : mp->m_ddev_targp;
1380
1381 da.d_mem = da.d_miniosz = 1 << target->bt_sshift;
1382 da.d_maxiosz = INT_MAX & ~(da.d_miniosz - 1);
1383
1384 if (copy_to_user(arg, &da, sizeof(da)))
1385 return -XFS_ERROR(EFAULT);
1386 return 0;
1387 }
1388
1389 case XFS_IOC_FSBULKSTAT_SINGLE:
1390 case XFS_IOC_FSBULKSTAT:
1391 case XFS_IOC_FSINUMBERS:
1392 return xfs_ioc_bulkstat(mp, cmd, arg);
1393
1394 case XFS_IOC_FSGEOMETRY_V1:
1395 return xfs_ioc_fsgeometry_v1(mp, arg);
1396
1397 case XFS_IOC_FSGEOMETRY:
1398 return xfs_ioc_fsgeometry(mp, arg);
1399
1400 case XFS_IOC_GETVERSION:
1401 return put_user(inode->i_generation, (int __user *)arg);
1402
1403 case XFS_IOC_FSGETXATTR:
1404 return xfs_ioc_fsgetxattr(ip, 0, arg);
1405 case XFS_IOC_FSGETXATTRA:
1406 return xfs_ioc_fsgetxattr(ip, 1, arg);
1407 case XFS_IOC_FSSETXATTR:
1408 return xfs_ioc_fssetxattr(ip, filp, arg);
1409 case XFS_IOC_GETXFLAGS:
1410 return xfs_ioc_getxflags(ip, arg);
1411 case XFS_IOC_SETXFLAGS:
1412 return xfs_ioc_setxflags(ip, filp, arg);
1413
1414 case XFS_IOC_FSSETDM: {
1415 struct fsdmidata dmi;
1416
1417 if (copy_from_user(&dmi, arg, sizeof(dmi)))
1418 return -XFS_ERROR(EFAULT);
1419
1420 error = mnt_want_write_file(filp);
1421 if (error)
1422 return error;
1423
1424 error = xfs_set_dmattrs(ip, dmi.fsd_dmevmask,
1425 dmi.fsd_dmstate);
1426 mnt_drop_write_file(filp);
1427 return -error;
1428 }
1429
1430 case XFS_IOC_GETBMAP:
1431 case XFS_IOC_GETBMAPA:
1432 return xfs_ioc_getbmap(ip, ioflags, cmd, arg);
1433
1434 case XFS_IOC_GETBMAPX:
1435 return xfs_ioc_getbmapx(ip, arg);
1436
1437 case XFS_IOC_FD_TO_HANDLE:
1438 case XFS_IOC_PATH_TO_HANDLE:
1439 case XFS_IOC_PATH_TO_FSHANDLE: {
1440 xfs_fsop_handlereq_t hreq;
1441
1442 if (copy_from_user(&hreq, arg, sizeof(hreq)))
1443 return -XFS_ERROR(EFAULT);
1444 return xfs_find_handle(cmd, &hreq);
1445 }
1446 case XFS_IOC_OPEN_BY_HANDLE: {
1447 xfs_fsop_handlereq_t hreq;
1448
1449 if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
1450 return -XFS_ERROR(EFAULT);
1451 return xfs_open_by_handle(filp, &hreq);
1452 }
1453 case XFS_IOC_FSSETDM_BY_HANDLE:
1454 return xfs_fssetdm_by_handle(filp, arg);
1455
1456 case XFS_IOC_READLINK_BY_HANDLE: {
1457 xfs_fsop_handlereq_t hreq;
1458
1459 if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
1460 return -XFS_ERROR(EFAULT);
1461 return xfs_readlink_by_handle(filp, &hreq);
1462 }
1463 case XFS_IOC_ATTRLIST_BY_HANDLE:
1464 return xfs_attrlist_by_handle(filp, arg);
1465
1466 case XFS_IOC_ATTRMULTI_BY_HANDLE:
1467 return xfs_attrmulti_by_handle(filp, arg);
1468
1469 case XFS_IOC_SWAPEXT: {
1470 struct xfs_swapext sxp;
1471
1472 if (copy_from_user(&sxp, arg, sizeof(xfs_swapext_t)))
1473 return -XFS_ERROR(EFAULT);
1474 error = mnt_want_write_file(filp);
1475 if (error)
1476 return error;
1477 error = xfs_swapext(&sxp);
1478 mnt_drop_write_file(filp);
1479 return -error;
1480 }
1481
1482 case XFS_IOC_FSCOUNTS: {
1483 xfs_fsop_counts_t out;
1484
1485 error = xfs_fs_counts(mp, &out);
1486 if (error)
1487 return -error;
1488
1489 if (copy_to_user(arg, &out, sizeof(out)))
1490 return -XFS_ERROR(EFAULT);
1491 return 0;
1492 }
1493
1494 case XFS_IOC_SET_RESBLKS: {
1495 xfs_fsop_resblks_t inout;
1496 __uint64_t in;
1497
1498 if (!capable(CAP_SYS_ADMIN))
1499 return -EPERM;
1500
1501 if (mp->m_flags & XFS_MOUNT_RDONLY)
1502 return -XFS_ERROR(EROFS);
1503
1504 if (copy_from_user(&inout, arg, sizeof(inout)))
1505 return -XFS_ERROR(EFAULT);
1506
1507 error = mnt_want_write_file(filp);
1508 if (error)
1509 return error;
1510
1511 /* input parameter is passed in resblks field of structure */
1512 in = inout.resblks;
1513 error = xfs_reserve_blocks(mp, &in, &inout);
1514 mnt_drop_write_file(filp);
1515 if (error)
1516 return -error;
1517
1518 if (copy_to_user(arg, &inout, sizeof(inout)))
1519 return -XFS_ERROR(EFAULT);
1520 return 0;
1521 }
1522
1523 case XFS_IOC_GET_RESBLKS: {
1524 xfs_fsop_resblks_t out;
1525
1526 if (!capable(CAP_SYS_ADMIN))
1527 return -EPERM;
1528
1529 error = xfs_reserve_blocks(mp, NULL, &out);
1530 if (error)
1531 return -error;
1532
1533 if (copy_to_user(arg, &out, sizeof(out)))
1534 return -XFS_ERROR(EFAULT);
1535
1536 return 0;
1537 }
1538
1539 case XFS_IOC_FSGROWFSDATA: {
1540 xfs_growfs_data_t in;
1541
1542 if (copy_from_user(&in, arg, sizeof(in)))
1543 return -XFS_ERROR(EFAULT);
1544
1545 error = mnt_want_write_file(filp);
1546 if (error)
1547 return error;
1548 error = xfs_growfs_data(mp, &in);
1549 mnt_drop_write_file(filp);
1550 return -error;
1551 }
1552
1553 case XFS_IOC_FSGROWFSLOG: {
1554 xfs_growfs_log_t in;
1555
1556 if (copy_from_user(&in, arg, sizeof(in)))
1557 return -XFS_ERROR(EFAULT);
1558
1559 error = mnt_want_write_file(filp);
1560 if (error)
1561 return error;
1562 error = xfs_growfs_log(mp, &in);
1563 mnt_drop_write_file(filp);
1564 return -error;
1565 }
1566
1567 case XFS_IOC_FSGROWFSRT: {
1568 xfs_growfs_rt_t in;
1569
1570 if (copy_from_user(&in, arg, sizeof(in)))
1571 return -XFS_ERROR(EFAULT);
1572
1573 error = mnt_want_write_file(filp);
1574 if (error)
1575 return error;
1576 error = xfs_growfs_rt(mp, &in);
1577 mnt_drop_write_file(filp);
1578 return -error;
1579 }
1580
1581 case XFS_IOC_GOINGDOWN: {
1582 __uint32_t in;
1583
1584 if (!capable(CAP_SYS_ADMIN))
1585 return -EPERM;
1586
1587 if (get_user(in, (__uint32_t __user *)arg))
1588 return -XFS_ERROR(EFAULT);
1589
1590 error = xfs_fs_goingdown(mp, in);
1591 return -error;
1592 }
1593
1594 case XFS_IOC_ERROR_INJECTION: {
1595 xfs_error_injection_t in;
1596
1597 if (!capable(CAP_SYS_ADMIN))
1598 return -EPERM;
1599
1600 if (copy_from_user(&in, arg, sizeof(in)))
1601 return -XFS_ERROR(EFAULT);
1602
1603 error = xfs_errortag_add(in.errtag, mp);
1604 return -error;
1605 }
1606
1607 case XFS_IOC_ERROR_CLEARALL:
1608 if (!capable(CAP_SYS_ADMIN))
1609 return -EPERM;
1610
1611 error = xfs_errortag_clearall(mp, 1);
1612 return -error;
1613
1614 case XFS_IOC_FREE_EOFBLOCKS: {
1615 struct xfs_eofblocks eofb;
1616
1617 if (copy_from_user(&eofb, arg, sizeof(eofb)))
1618 return -XFS_ERROR(EFAULT);
1619
1620 if (eofb.eof_version != XFS_EOFBLOCKS_VERSION)
1621 return -XFS_ERROR(EINVAL);
1622
1623 if (eofb.eof_flags & ~XFS_EOF_FLAGS_VALID)
1624 return -XFS_ERROR(EINVAL);
1625
1626 if (memchr_inv(&eofb.pad32, 0, sizeof(eofb.pad32)) ||
1627 memchr_inv(eofb.pad64, 0, sizeof(eofb.pad64)))
1628 return -XFS_ERROR(EINVAL);
1629
1630 error = xfs_icache_free_eofblocks(mp, &eofb);
1631 return -error;
1632 }
1633
1634 default:
1635 return -ENOTTY;
1636 }
1637 }
This page took 0.093176 seconds and 5 git commands to generate.