xfs: decouple log and transaction headers
[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_shared.h"
21 #include "xfs_format.h"
22 #include "xfs_log_format.h"
23 #include "xfs_trans_resv.h"
24 #include "xfs_sb.h"
25 #include "xfs_ag.h"
26 #include "xfs_alloc.h"
27 #include "xfs_mount.h"
28 #include "xfs_bmap_btree.h"
29 #include "xfs_dinode.h"
30 #include "xfs_inode.h"
31 #include "xfs_ioctl.h"
32 #include "xfs_rtalloc.h"
33 #include "xfs_itable.h"
34 #include "xfs_error.h"
35 #include "xfs_attr.h"
36 #include "xfs_bmap.h"
37 #include "xfs_bmap_util.h"
38 #include "xfs_trans.h"
39 #include "xfs_buf_item.h"
40 #include "xfs_fsops.h"
41 #include "xfs_discard.h"
42 #include "xfs_quota.h"
43 #include "xfs_inode_item.h"
44 #include "xfs_export.h"
45 #include "xfs_trace.h"
46 #include "xfs_icache.h"
47 #include "xfs_symlink.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 = {NULL};
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 int
356 xfs_set_dmattrs(
357 xfs_inode_t *ip,
358 u_int evmask,
359 u_int16_t state)
360 {
361 xfs_mount_t *mp = ip->i_mount;
362 xfs_trans_t *tp;
363 int error;
364
365 if (!capable(CAP_SYS_ADMIN))
366 return XFS_ERROR(EPERM);
367
368 if (XFS_FORCED_SHUTDOWN(mp))
369 return XFS_ERROR(EIO);
370
371 tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS);
372 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_ichange, 0, 0);
373 if (error) {
374 xfs_trans_cancel(tp, 0);
375 return error;
376 }
377 xfs_ilock(ip, XFS_ILOCK_EXCL);
378 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
379
380 ip->i_d.di_dmevmask = evmask;
381 ip->i_d.di_dmstate = state;
382
383 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
384 error = xfs_trans_commit(tp, 0);
385
386 return error;
387 }
388
389 STATIC int
390 xfs_fssetdm_by_handle(
391 struct file *parfilp,
392 void __user *arg)
393 {
394 int error;
395 struct fsdmidata fsd;
396 xfs_fsop_setdm_handlereq_t dmhreq;
397 struct dentry *dentry;
398
399 if (!capable(CAP_MKNOD))
400 return -XFS_ERROR(EPERM);
401 if (copy_from_user(&dmhreq, arg, sizeof(xfs_fsop_setdm_handlereq_t)))
402 return -XFS_ERROR(EFAULT);
403
404 error = mnt_want_write_file(parfilp);
405 if (error)
406 return error;
407
408 dentry = xfs_handlereq_to_dentry(parfilp, &dmhreq.hreq);
409 if (IS_ERR(dentry)) {
410 mnt_drop_write_file(parfilp);
411 return PTR_ERR(dentry);
412 }
413
414 if (IS_IMMUTABLE(dentry->d_inode) || IS_APPEND(dentry->d_inode)) {
415 error = -XFS_ERROR(EPERM);
416 goto out;
417 }
418
419 if (copy_from_user(&fsd, dmhreq.data, sizeof(fsd))) {
420 error = -XFS_ERROR(EFAULT);
421 goto out;
422 }
423
424 error = -xfs_set_dmattrs(XFS_I(dentry->d_inode), fsd.fsd_dmevmask,
425 fsd.fsd_dmstate);
426
427 out:
428 mnt_drop_write_file(parfilp);
429 dput(dentry);
430 return error;
431 }
432
433 STATIC int
434 xfs_attrlist_by_handle(
435 struct file *parfilp,
436 void __user *arg)
437 {
438 int error = -ENOMEM;
439 attrlist_cursor_kern_t *cursor;
440 xfs_fsop_attrlist_handlereq_t al_hreq;
441 struct dentry *dentry;
442 char *kbuf;
443
444 if (!capable(CAP_SYS_ADMIN))
445 return -XFS_ERROR(EPERM);
446 if (copy_from_user(&al_hreq, arg, sizeof(xfs_fsop_attrlist_handlereq_t)))
447 return -XFS_ERROR(EFAULT);
448 if (al_hreq.buflen > XATTR_LIST_MAX)
449 return -XFS_ERROR(EINVAL);
450
451 /*
452 * Reject flags, only allow namespaces.
453 */
454 if (al_hreq.flags & ~(ATTR_ROOT | ATTR_SECURE))
455 return -XFS_ERROR(EINVAL);
456
457 dentry = xfs_handlereq_to_dentry(parfilp, &al_hreq.hreq);
458 if (IS_ERR(dentry))
459 return PTR_ERR(dentry);
460
461 kbuf = kmem_zalloc_large(al_hreq.buflen, KM_SLEEP);
462 if (!kbuf)
463 goto out_dput;
464
465 cursor = (attrlist_cursor_kern_t *)&al_hreq.pos;
466 error = -xfs_attr_list(XFS_I(dentry->d_inode), kbuf, al_hreq.buflen,
467 al_hreq.flags, cursor);
468 if (error)
469 goto out_kfree;
470
471 if (copy_to_user(al_hreq.buffer, kbuf, al_hreq.buflen))
472 error = -EFAULT;
473
474 out_kfree:
475 kmem_free(kbuf);
476 out_dput:
477 dput(dentry);
478 return error;
479 }
480
481 int
482 xfs_attrmulti_attr_get(
483 struct inode *inode,
484 unsigned char *name,
485 unsigned char __user *ubuf,
486 __uint32_t *len,
487 __uint32_t flags)
488 {
489 unsigned char *kbuf;
490 int error = EFAULT;
491
492 if (*len > XATTR_SIZE_MAX)
493 return EINVAL;
494 kbuf = kmem_zalloc_large(*len, KM_SLEEP);
495 if (!kbuf)
496 return ENOMEM;
497
498 error = xfs_attr_get(XFS_I(inode), name, kbuf, (int *)len, flags);
499 if (error)
500 goto out_kfree;
501
502 if (copy_to_user(ubuf, kbuf, *len))
503 error = EFAULT;
504
505 out_kfree:
506 kmem_free(kbuf);
507 return error;
508 }
509
510 int
511 xfs_attrmulti_attr_set(
512 struct inode *inode,
513 unsigned char *name,
514 const unsigned char __user *ubuf,
515 __uint32_t len,
516 __uint32_t flags)
517 {
518 unsigned char *kbuf;
519 int error = EFAULT;
520
521 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
522 return EPERM;
523 if (len > XATTR_SIZE_MAX)
524 return EINVAL;
525
526 kbuf = memdup_user(ubuf, len);
527 if (IS_ERR(kbuf))
528 return PTR_ERR(kbuf);
529
530 error = xfs_attr_set(XFS_I(inode), name, kbuf, len, flags);
531
532 return error;
533 }
534
535 int
536 xfs_attrmulti_attr_remove(
537 struct inode *inode,
538 unsigned char *name,
539 __uint32_t flags)
540 {
541 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
542 return EPERM;
543 return xfs_attr_remove(XFS_I(inode), name, flags);
544 }
545
546 STATIC int
547 xfs_attrmulti_by_handle(
548 struct file *parfilp,
549 void __user *arg)
550 {
551 int error;
552 xfs_attr_multiop_t *ops;
553 xfs_fsop_attrmulti_handlereq_t am_hreq;
554 struct dentry *dentry;
555 unsigned int i, size;
556 unsigned char *attr_name;
557
558 if (!capable(CAP_SYS_ADMIN))
559 return -XFS_ERROR(EPERM);
560 if (copy_from_user(&am_hreq, arg, sizeof(xfs_fsop_attrmulti_handlereq_t)))
561 return -XFS_ERROR(EFAULT);
562
563 /* overflow check */
564 if (am_hreq.opcount >= INT_MAX / sizeof(xfs_attr_multiop_t))
565 return -E2BIG;
566
567 dentry = xfs_handlereq_to_dentry(parfilp, &am_hreq.hreq);
568 if (IS_ERR(dentry))
569 return PTR_ERR(dentry);
570
571 error = E2BIG;
572 size = am_hreq.opcount * sizeof(xfs_attr_multiop_t);
573 if (!size || size > 16 * PAGE_SIZE)
574 goto out_dput;
575
576 ops = memdup_user(am_hreq.ops, size);
577 if (IS_ERR(ops)) {
578 error = PTR_ERR(ops);
579 goto out_dput;
580 }
581
582 attr_name = kmalloc(MAXNAMELEN, GFP_KERNEL);
583 if (!attr_name)
584 goto out_kfree_ops;
585
586 error = 0;
587 for (i = 0; i < am_hreq.opcount; i++) {
588 ops[i].am_error = strncpy_from_user((char *)attr_name,
589 ops[i].am_attrname, MAXNAMELEN);
590 if (ops[i].am_error == 0 || ops[i].am_error == MAXNAMELEN)
591 error = -ERANGE;
592 if (ops[i].am_error < 0)
593 break;
594
595 switch (ops[i].am_opcode) {
596 case ATTR_OP_GET:
597 ops[i].am_error = xfs_attrmulti_attr_get(
598 dentry->d_inode, attr_name,
599 ops[i].am_attrvalue, &ops[i].am_length,
600 ops[i].am_flags);
601 break;
602 case ATTR_OP_SET:
603 ops[i].am_error = mnt_want_write_file(parfilp);
604 if (ops[i].am_error)
605 break;
606 ops[i].am_error = xfs_attrmulti_attr_set(
607 dentry->d_inode, attr_name,
608 ops[i].am_attrvalue, ops[i].am_length,
609 ops[i].am_flags);
610 mnt_drop_write_file(parfilp);
611 break;
612 case ATTR_OP_REMOVE:
613 ops[i].am_error = mnt_want_write_file(parfilp);
614 if (ops[i].am_error)
615 break;
616 ops[i].am_error = xfs_attrmulti_attr_remove(
617 dentry->d_inode, attr_name,
618 ops[i].am_flags);
619 mnt_drop_write_file(parfilp);
620 break;
621 default:
622 ops[i].am_error = EINVAL;
623 }
624 }
625
626 if (copy_to_user(am_hreq.ops, ops, size))
627 error = XFS_ERROR(EFAULT);
628
629 kfree(attr_name);
630 out_kfree_ops:
631 kfree(ops);
632 out_dput:
633 dput(dentry);
634 return -error;
635 }
636
637 int
638 xfs_ioc_space(
639 struct xfs_inode *ip,
640 struct inode *inode,
641 struct file *filp,
642 int ioflags,
643 unsigned int cmd,
644 xfs_flock64_t *bf)
645 {
646 struct xfs_mount *mp = ip->i_mount;
647 struct xfs_trans *tp;
648 struct iattr iattr;
649 bool setprealloc = false;
650 bool clrprealloc = false;
651 int error;
652
653 /*
654 * Only allow the sys admin to reserve space unless
655 * unwritten extents are enabled.
656 */
657 if (!xfs_sb_version_hasextflgbit(&ip->i_mount->m_sb) &&
658 !capable(CAP_SYS_ADMIN))
659 return -XFS_ERROR(EPERM);
660
661 if (inode->i_flags & (S_IMMUTABLE|S_APPEND))
662 return -XFS_ERROR(EPERM);
663
664 if (!(filp->f_mode & FMODE_WRITE))
665 return -XFS_ERROR(EBADF);
666
667 if (!S_ISREG(inode->i_mode))
668 return -XFS_ERROR(EINVAL);
669
670 error = mnt_want_write_file(filp);
671 if (error)
672 return error;
673
674 xfs_ilock(ip, XFS_IOLOCK_EXCL);
675
676 switch (bf->l_whence) {
677 case 0: /*SEEK_SET*/
678 break;
679 case 1: /*SEEK_CUR*/
680 bf->l_start += filp->f_pos;
681 break;
682 case 2: /*SEEK_END*/
683 bf->l_start += XFS_ISIZE(ip);
684 break;
685 default:
686 error = XFS_ERROR(EINVAL);
687 goto out_unlock;
688 }
689
690 /*
691 * length of <= 0 for resv/unresv/zero is invalid. length for
692 * alloc/free is ignored completely and we have no idea what userspace
693 * might have set it to, so set it to zero to allow range
694 * checks to pass.
695 */
696 switch (cmd) {
697 case XFS_IOC_ZERO_RANGE:
698 case XFS_IOC_RESVSP:
699 case XFS_IOC_RESVSP64:
700 case XFS_IOC_UNRESVSP:
701 case XFS_IOC_UNRESVSP64:
702 if (bf->l_len <= 0) {
703 error = XFS_ERROR(EINVAL);
704 goto out_unlock;
705 }
706 break;
707 default:
708 bf->l_len = 0;
709 break;
710 }
711
712 if (bf->l_start < 0 ||
713 bf->l_start > mp->m_super->s_maxbytes ||
714 bf->l_start + bf->l_len < 0 ||
715 bf->l_start + bf->l_len >= mp->m_super->s_maxbytes) {
716 error = XFS_ERROR(EINVAL);
717 goto out_unlock;
718 }
719
720 switch (cmd) {
721 case XFS_IOC_ZERO_RANGE:
722 error = xfs_zero_file_space(ip, bf->l_start, bf->l_len);
723 if (!error)
724 setprealloc = true;
725 break;
726 case XFS_IOC_RESVSP:
727 case XFS_IOC_RESVSP64:
728 error = xfs_alloc_file_space(ip, bf->l_start, bf->l_len,
729 XFS_BMAPI_PREALLOC);
730 if (!error)
731 setprealloc = true;
732 break;
733 case XFS_IOC_UNRESVSP:
734 case XFS_IOC_UNRESVSP64:
735 error = xfs_free_file_space(ip, bf->l_start, bf->l_len);
736 break;
737 case XFS_IOC_ALLOCSP:
738 case XFS_IOC_ALLOCSP64:
739 case XFS_IOC_FREESP:
740 case XFS_IOC_FREESP64:
741 if (bf->l_start > XFS_ISIZE(ip)) {
742 error = xfs_alloc_file_space(ip, XFS_ISIZE(ip),
743 bf->l_start - XFS_ISIZE(ip), 0);
744 if (error)
745 goto out_unlock;
746 }
747
748 iattr.ia_valid = ATTR_SIZE;
749 iattr.ia_size = bf->l_start;
750
751 error = xfs_setattr_size(ip, &iattr);
752 if (!error)
753 clrprealloc = true;
754 break;
755 default:
756 ASSERT(0);
757 error = XFS_ERROR(EINVAL);
758 }
759
760 if (error)
761 goto out_unlock;
762
763 tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
764 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_writeid, 0, 0);
765 if (error) {
766 xfs_trans_cancel(tp, 0);
767 goto out_unlock;
768 }
769
770 xfs_ilock(ip, XFS_ILOCK_EXCL);
771 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
772
773 if (!(ioflags & IO_INVIS)) {
774 ip->i_d.di_mode &= ~S_ISUID;
775 if (ip->i_d.di_mode & S_IXGRP)
776 ip->i_d.di_mode &= ~S_ISGID;
777 xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
778 }
779
780 if (setprealloc)
781 ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
782 else if (clrprealloc)
783 ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
784
785 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
786 if (filp->f_flags & O_DSYNC)
787 xfs_trans_set_sync(tp);
788 error = xfs_trans_commit(tp, 0);
789
790 out_unlock:
791 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
792 mnt_drop_write_file(filp);
793 return -error;
794 }
795
796 STATIC int
797 xfs_ioc_bulkstat(
798 xfs_mount_t *mp,
799 unsigned int cmd,
800 void __user *arg)
801 {
802 xfs_fsop_bulkreq_t bulkreq;
803 int count; /* # of records returned */
804 xfs_ino_t inlast; /* last inode number */
805 int done;
806 int error;
807
808 /* done = 1 if there are more stats to get and if bulkstat */
809 /* should be called again (unused here, but used in dmapi) */
810
811 if (!capable(CAP_SYS_ADMIN))
812 return -EPERM;
813
814 if (XFS_FORCED_SHUTDOWN(mp))
815 return -XFS_ERROR(EIO);
816
817 if (copy_from_user(&bulkreq, arg, sizeof(xfs_fsop_bulkreq_t)))
818 return -XFS_ERROR(EFAULT);
819
820 if (copy_from_user(&inlast, bulkreq.lastip, sizeof(__s64)))
821 return -XFS_ERROR(EFAULT);
822
823 if ((count = bulkreq.icount) <= 0)
824 return -XFS_ERROR(EINVAL);
825
826 if (bulkreq.ubuffer == NULL)
827 return -XFS_ERROR(EINVAL);
828
829 if (cmd == XFS_IOC_FSINUMBERS)
830 error = xfs_inumbers(mp, &inlast, &count,
831 bulkreq.ubuffer, xfs_inumbers_fmt);
832 else if (cmd == XFS_IOC_FSBULKSTAT_SINGLE)
833 error = xfs_bulkstat_single(mp, &inlast,
834 bulkreq.ubuffer, &done);
835 else /* XFS_IOC_FSBULKSTAT */
836 error = xfs_bulkstat(mp, &inlast, &count, xfs_bulkstat_one,
837 sizeof(xfs_bstat_t), bulkreq.ubuffer,
838 &done);
839
840 if (error)
841 return -error;
842
843 if (bulkreq.ocount != NULL) {
844 if (copy_to_user(bulkreq.lastip, &inlast,
845 sizeof(xfs_ino_t)))
846 return -XFS_ERROR(EFAULT);
847
848 if (copy_to_user(bulkreq.ocount, &count, sizeof(count)))
849 return -XFS_ERROR(EFAULT);
850 }
851
852 return 0;
853 }
854
855 STATIC int
856 xfs_ioc_fsgeometry_v1(
857 xfs_mount_t *mp,
858 void __user *arg)
859 {
860 xfs_fsop_geom_t fsgeo;
861 int error;
862
863 error = xfs_fs_geometry(mp, &fsgeo, 3);
864 if (error)
865 return -error;
866
867 /*
868 * Caller should have passed an argument of type
869 * xfs_fsop_geom_v1_t. This is a proper subset of the
870 * xfs_fsop_geom_t that xfs_fs_geometry() fills in.
871 */
872 if (copy_to_user(arg, &fsgeo, sizeof(xfs_fsop_geom_v1_t)))
873 return -XFS_ERROR(EFAULT);
874 return 0;
875 }
876
877 STATIC int
878 xfs_ioc_fsgeometry(
879 xfs_mount_t *mp,
880 void __user *arg)
881 {
882 xfs_fsop_geom_t fsgeo;
883 int error;
884
885 error = xfs_fs_geometry(mp, &fsgeo, 4);
886 if (error)
887 return -error;
888
889 if (copy_to_user(arg, &fsgeo, sizeof(fsgeo)))
890 return -XFS_ERROR(EFAULT);
891 return 0;
892 }
893
894 /*
895 * Linux extended inode flags interface.
896 */
897
898 STATIC unsigned int
899 xfs_merge_ioc_xflags(
900 unsigned int flags,
901 unsigned int start)
902 {
903 unsigned int xflags = start;
904
905 if (flags & FS_IMMUTABLE_FL)
906 xflags |= XFS_XFLAG_IMMUTABLE;
907 else
908 xflags &= ~XFS_XFLAG_IMMUTABLE;
909 if (flags & FS_APPEND_FL)
910 xflags |= XFS_XFLAG_APPEND;
911 else
912 xflags &= ~XFS_XFLAG_APPEND;
913 if (flags & FS_SYNC_FL)
914 xflags |= XFS_XFLAG_SYNC;
915 else
916 xflags &= ~XFS_XFLAG_SYNC;
917 if (flags & FS_NOATIME_FL)
918 xflags |= XFS_XFLAG_NOATIME;
919 else
920 xflags &= ~XFS_XFLAG_NOATIME;
921 if (flags & FS_NODUMP_FL)
922 xflags |= XFS_XFLAG_NODUMP;
923 else
924 xflags &= ~XFS_XFLAG_NODUMP;
925
926 return xflags;
927 }
928
929 STATIC unsigned int
930 xfs_di2lxflags(
931 __uint16_t di_flags)
932 {
933 unsigned int flags = 0;
934
935 if (di_flags & XFS_DIFLAG_IMMUTABLE)
936 flags |= FS_IMMUTABLE_FL;
937 if (di_flags & XFS_DIFLAG_APPEND)
938 flags |= FS_APPEND_FL;
939 if (di_flags & XFS_DIFLAG_SYNC)
940 flags |= FS_SYNC_FL;
941 if (di_flags & XFS_DIFLAG_NOATIME)
942 flags |= FS_NOATIME_FL;
943 if (di_flags & XFS_DIFLAG_NODUMP)
944 flags |= FS_NODUMP_FL;
945 return flags;
946 }
947
948 STATIC int
949 xfs_ioc_fsgetxattr(
950 xfs_inode_t *ip,
951 int attr,
952 void __user *arg)
953 {
954 struct fsxattr fa;
955
956 memset(&fa, 0, sizeof(struct fsxattr));
957
958 xfs_ilock(ip, XFS_ILOCK_SHARED);
959 fa.fsx_xflags = xfs_ip2xflags(ip);
960 fa.fsx_extsize = ip->i_d.di_extsize << ip->i_mount->m_sb.sb_blocklog;
961 fa.fsx_projid = xfs_get_projid(ip);
962
963 if (attr) {
964 if (ip->i_afp) {
965 if (ip->i_afp->if_flags & XFS_IFEXTENTS)
966 fa.fsx_nextents = ip->i_afp->if_bytes /
967 sizeof(xfs_bmbt_rec_t);
968 else
969 fa.fsx_nextents = ip->i_d.di_anextents;
970 } else
971 fa.fsx_nextents = 0;
972 } else {
973 if (ip->i_df.if_flags & XFS_IFEXTENTS)
974 fa.fsx_nextents = ip->i_df.if_bytes /
975 sizeof(xfs_bmbt_rec_t);
976 else
977 fa.fsx_nextents = ip->i_d.di_nextents;
978 }
979 xfs_iunlock(ip, XFS_ILOCK_SHARED);
980
981 if (copy_to_user(arg, &fa, sizeof(fa)))
982 return -EFAULT;
983 return 0;
984 }
985
986 STATIC void
987 xfs_set_diflags(
988 struct xfs_inode *ip,
989 unsigned int xflags)
990 {
991 unsigned int di_flags;
992
993 /* can't set PREALLOC this way, just preserve it */
994 di_flags = (ip->i_d.di_flags & XFS_DIFLAG_PREALLOC);
995 if (xflags & XFS_XFLAG_IMMUTABLE)
996 di_flags |= XFS_DIFLAG_IMMUTABLE;
997 if (xflags & XFS_XFLAG_APPEND)
998 di_flags |= XFS_DIFLAG_APPEND;
999 if (xflags & XFS_XFLAG_SYNC)
1000 di_flags |= XFS_DIFLAG_SYNC;
1001 if (xflags & XFS_XFLAG_NOATIME)
1002 di_flags |= XFS_DIFLAG_NOATIME;
1003 if (xflags & XFS_XFLAG_NODUMP)
1004 di_flags |= XFS_DIFLAG_NODUMP;
1005 if (xflags & XFS_XFLAG_PROJINHERIT)
1006 di_flags |= XFS_DIFLAG_PROJINHERIT;
1007 if (xflags & XFS_XFLAG_NODEFRAG)
1008 di_flags |= XFS_DIFLAG_NODEFRAG;
1009 if (xflags & XFS_XFLAG_FILESTREAM)
1010 di_flags |= XFS_DIFLAG_FILESTREAM;
1011 if (S_ISDIR(ip->i_d.di_mode)) {
1012 if (xflags & XFS_XFLAG_RTINHERIT)
1013 di_flags |= XFS_DIFLAG_RTINHERIT;
1014 if (xflags & XFS_XFLAG_NOSYMLINKS)
1015 di_flags |= XFS_DIFLAG_NOSYMLINKS;
1016 if (xflags & XFS_XFLAG_EXTSZINHERIT)
1017 di_flags |= XFS_DIFLAG_EXTSZINHERIT;
1018 } else if (S_ISREG(ip->i_d.di_mode)) {
1019 if (xflags & XFS_XFLAG_REALTIME)
1020 di_flags |= XFS_DIFLAG_REALTIME;
1021 if (xflags & XFS_XFLAG_EXTSIZE)
1022 di_flags |= XFS_DIFLAG_EXTSIZE;
1023 }
1024
1025 ip->i_d.di_flags = di_flags;
1026 }
1027
1028 STATIC void
1029 xfs_diflags_to_linux(
1030 struct xfs_inode *ip)
1031 {
1032 struct inode *inode = VFS_I(ip);
1033 unsigned int xflags = xfs_ip2xflags(ip);
1034
1035 if (xflags & XFS_XFLAG_IMMUTABLE)
1036 inode->i_flags |= S_IMMUTABLE;
1037 else
1038 inode->i_flags &= ~S_IMMUTABLE;
1039 if (xflags & XFS_XFLAG_APPEND)
1040 inode->i_flags |= S_APPEND;
1041 else
1042 inode->i_flags &= ~S_APPEND;
1043 if (xflags & XFS_XFLAG_SYNC)
1044 inode->i_flags |= S_SYNC;
1045 else
1046 inode->i_flags &= ~S_SYNC;
1047 if (xflags & XFS_XFLAG_NOATIME)
1048 inode->i_flags |= S_NOATIME;
1049 else
1050 inode->i_flags &= ~S_NOATIME;
1051 }
1052
1053 #define FSX_PROJID 1
1054 #define FSX_EXTSIZE 2
1055 #define FSX_XFLAGS 4
1056 #define FSX_NONBLOCK 8
1057
1058 STATIC int
1059 xfs_ioctl_setattr(
1060 xfs_inode_t *ip,
1061 struct fsxattr *fa,
1062 int mask)
1063 {
1064 struct xfs_mount *mp = ip->i_mount;
1065 struct xfs_trans *tp;
1066 unsigned int lock_flags = 0;
1067 struct xfs_dquot *udqp = NULL;
1068 struct xfs_dquot *pdqp = NULL;
1069 struct xfs_dquot *olddquot = NULL;
1070 int code;
1071
1072 trace_xfs_ioctl_setattr(ip);
1073
1074 if (mp->m_flags & XFS_MOUNT_RDONLY)
1075 return XFS_ERROR(EROFS);
1076 if (XFS_FORCED_SHUTDOWN(mp))
1077 return XFS_ERROR(EIO);
1078
1079 /*
1080 * Disallow 32bit project ids when projid32bit feature is not enabled.
1081 */
1082 if ((mask & FSX_PROJID) && (fa->fsx_projid > (__uint16_t)-1) &&
1083 !xfs_sb_version_hasprojid32bit(&ip->i_mount->m_sb))
1084 return XFS_ERROR(EINVAL);
1085
1086 /*
1087 * If disk quotas is on, we make sure that the dquots do exist on disk,
1088 * before we start any other transactions. Trying to do this later
1089 * is messy. We don't care to take a readlock to look at the ids
1090 * in inode here, because we can't hold it across the trans_reserve.
1091 * If the IDs do change before we take the ilock, we're covered
1092 * because the i_*dquot fields will get updated anyway.
1093 */
1094 if (XFS_IS_QUOTA_ON(mp) && (mask & FSX_PROJID)) {
1095 code = xfs_qm_vop_dqalloc(ip, ip->i_d.di_uid,
1096 ip->i_d.di_gid, fa->fsx_projid,
1097 XFS_QMOPT_PQUOTA, &udqp, NULL, &pdqp);
1098 if (code)
1099 return code;
1100 }
1101
1102 /*
1103 * For the other attributes, we acquire the inode lock and
1104 * first do an error checking pass.
1105 */
1106 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
1107 code = xfs_trans_reserve(tp, &M_RES(mp)->tr_ichange, 0, 0);
1108 if (code)
1109 goto error_return;
1110
1111 lock_flags = XFS_ILOCK_EXCL;
1112 xfs_ilock(ip, lock_flags);
1113
1114 /*
1115 * CAP_FOWNER overrides the following restrictions:
1116 *
1117 * The user ID of the calling process must be equal
1118 * to the file owner ID, except in cases where the
1119 * CAP_FSETID capability is applicable.
1120 */
1121 if (!inode_owner_or_capable(VFS_I(ip))) {
1122 code = XFS_ERROR(EPERM);
1123 goto error_return;
1124 }
1125
1126 /*
1127 * Do a quota reservation only if projid is actually going to change.
1128 * Only allow changing of projid from init_user_ns since it is a
1129 * non user namespace aware identifier.
1130 */
1131 if (mask & FSX_PROJID) {
1132 if (current_user_ns() != &init_user_ns) {
1133 code = XFS_ERROR(EINVAL);
1134 goto error_return;
1135 }
1136
1137 if (XFS_IS_QUOTA_RUNNING(mp) &&
1138 XFS_IS_PQUOTA_ON(mp) &&
1139 xfs_get_projid(ip) != fa->fsx_projid) {
1140 ASSERT(tp);
1141 code = xfs_qm_vop_chown_reserve(tp, ip, udqp, NULL,
1142 pdqp, capable(CAP_FOWNER) ?
1143 XFS_QMOPT_FORCE_RES : 0);
1144 if (code) /* out of quota */
1145 goto error_return;
1146 }
1147 }
1148
1149 if (mask & FSX_EXTSIZE) {
1150 /*
1151 * Can't change extent size if any extents are allocated.
1152 */
1153 if (ip->i_d.di_nextents &&
1154 ((ip->i_d.di_extsize << mp->m_sb.sb_blocklog) !=
1155 fa->fsx_extsize)) {
1156 code = XFS_ERROR(EINVAL); /* EFBIG? */
1157 goto error_return;
1158 }
1159
1160 /*
1161 * Extent size must be a multiple of the appropriate block
1162 * size, if set at all. It must also be smaller than the
1163 * maximum extent size supported by the filesystem.
1164 *
1165 * Also, for non-realtime files, limit the extent size hint to
1166 * half the size of the AGs in the filesystem so alignment
1167 * doesn't result in extents larger than an AG.
1168 */
1169 if (fa->fsx_extsize != 0) {
1170 xfs_extlen_t size;
1171 xfs_fsblock_t extsize_fsb;
1172
1173 extsize_fsb = XFS_B_TO_FSB(mp, fa->fsx_extsize);
1174 if (extsize_fsb > MAXEXTLEN) {
1175 code = XFS_ERROR(EINVAL);
1176 goto error_return;
1177 }
1178
1179 if (XFS_IS_REALTIME_INODE(ip) ||
1180 ((mask & FSX_XFLAGS) &&
1181 (fa->fsx_xflags & XFS_XFLAG_REALTIME))) {
1182 size = mp->m_sb.sb_rextsize <<
1183 mp->m_sb.sb_blocklog;
1184 } else {
1185 size = mp->m_sb.sb_blocksize;
1186 if (extsize_fsb > mp->m_sb.sb_agblocks / 2) {
1187 code = XFS_ERROR(EINVAL);
1188 goto error_return;
1189 }
1190 }
1191
1192 if (fa->fsx_extsize % size) {
1193 code = XFS_ERROR(EINVAL);
1194 goto error_return;
1195 }
1196 }
1197 }
1198
1199
1200 if (mask & FSX_XFLAGS) {
1201 /*
1202 * Can't change realtime flag if any extents are allocated.
1203 */
1204 if ((ip->i_d.di_nextents || ip->i_delayed_blks) &&
1205 (XFS_IS_REALTIME_INODE(ip)) !=
1206 (fa->fsx_xflags & XFS_XFLAG_REALTIME)) {
1207 code = XFS_ERROR(EINVAL); /* EFBIG? */
1208 goto error_return;
1209 }
1210
1211 /*
1212 * If realtime flag is set then must have realtime data.
1213 */
1214 if ((fa->fsx_xflags & XFS_XFLAG_REALTIME)) {
1215 if ((mp->m_sb.sb_rblocks == 0) ||
1216 (mp->m_sb.sb_rextsize == 0) ||
1217 (ip->i_d.di_extsize % mp->m_sb.sb_rextsize)) {
1218 code = XFS_ERROR(EINVAL);
1219 goto error_return;
1220 }
1221 }
1222
1223 /*
1224 * Can't modify an immutable/append-only file unless
1225 * we have appropriate permission.
1226 */
1227 if ((ip->i_d.di_flags &
1228 (XFS_DIFLAG_IMMUTABLE|XFS_DIFLAG_APPEND) ||
1229 (fa->fsx_xflags &
1230 (XFS_XFLAG_IMMUTABLE | XFS_XFLAG_APPEND))) &&
1231 !capable(CAP_LINUX_IMMUTABLE)) {
1232 code = XFS_ERROR(EPERM);
1233 goto error_return;
1234 }
1235 }
1236
1237 xfs_trans_ijoin(tp, ip, 0);
1238
1239 /*
1240 * Change file ownership. Must be the owner or privileged.
1241 */
1242 if (mask & FSX_PROJID) {
1243 /*
1244 * CAP_FSETID overrides the following restrictions:
1245 *
1246 * The set-user-ID and set-group-ID bits of a file will be
1247 * cleared upon successful return from chown()
1248 */
1249 if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
1250 !inode_capable(VFS_I(ip), CAP_FSETID))
1251 ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
1252
1253 /*
1254 * Change the ownerships and register quota modifications
1255 * in the transaction.
1256 */
1257 if (xfs_get_projid(ip) != fa->fsx_projid) {
1258 if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_PQUOTA_ON(mp)) {
1259 olddquot = xfs_qm_vop_chown(tp, ip,
1260 &ip->i_pdquot, pdqp);
1261 }
1262 xfs_set_projid(ip, fa->fsx_projid);
1263
1264 /*
1265 * We may have to rev the inode as well as
1266 * the superblock version number since projids didn't
1267 * exist before DINODE_VERSION_2 and SB_VERSION_NLINK.
1268 */
1269 if (ip->i_d.di_version == 1)
1270 xfs_bump_ino_vers2(tp, ip);
1271 }
1272
1273 }
1274
1275 if (mask & FSX_EXTSIZE)
1276 ip->i_d.di_extsize = fa->fsx_extsize >> mp->m_sb.sb_blocklog;
1277 if (mask & FSX_XFLAGS) {
1278 xfs_set_diflags(ip, fa->fsx_xflags);
1279 xfs_diflags_to_linux(ip);
1280 }
1281
1282 xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
1283 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1284
1285 XFS_STATS_INC(xs_ig_attrchg);
1286
1287 /*
1288 * If this is a synchronous mount, make sure that the
1289 * transaction goes to disk before returning to the user.
1290 * This is slightly sub-optimal in that truncates require
1291 * two sync transactions instead of one for wsync filesystems.
1292 * One for the truncate and one for the timestamps since we
1293 * don't want to change the timestamps unless we're sure the
1294 * truncate worked. Truncates are less than 1% of the laddis
1295 * mix so this probably isn't worth the trouble to optimize.
1296 */
1297 if (mp->m_flags & XFS_MOUNT_WSYNC)
1298 xfs_trans_set_sync(tp);
1299 code = xfs_trans_commit(tp, 0);
1300 xfs_iunlock(ip, lock_flags);
1301
1302 /*
1303 * Release any dquot(s) the inode had kept before chown.
1304 */
1305 xfs_qm_dqrele(olddquot);
1306 xfs_qm_dqrele(udqp);
1307 xfs_qm_dqrele(pdqp);
1308
1309 return code;
1310
1311 error_return:
1312 xfs_qm_dqrele(udqp);
1313 xfs_qm_dqrele(pdqp);
1314 xfs_trans_cancel(tp, 0);
1315 if (lock_flags)
1316 xfs_iunlock(ip, lock_flags);
1317 return code;
1318 }
1319
1320 STATIC int
1321 xfs_ioc_fssetxattr(
1322 xfs_inode_t *ip,
1323 struct file *filp,
1324 void __user *arg)
1325 {
1326 struct fsxattr fa;
1327 unsigned int mask;
1328 int error;
1329
1330 if (copy_from_user(&fa, arg, sizeof(fa)))
1331 return -EFAULT;
1332
1333 mask = FSX_XFLAGS | FSX_EXTSIZE | FSX_PROJID;
1334 if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
1335 mask |= FSX_NONBLOCK;
1336
1337 error = mnt_want_write_file(filp);
1338 if (error)
1339 return error;
1340 error = xfs_ioctl_setattr(ip, &fa, mask);
1341 mnt_drop_write_file(filp);
1342 return -error;
1343 }
1344
1345 STATIC int
1346 xfs_ioc_getxflags(
1347 xfs_inode_t *ip,
1348 void __user *arg)
1349 {
1350 unsigned int flags;
1351
1352 flags = xfs_di2lxflags(ip->i_d.di_flags);
1353 if (copy_to_user(arg, &flags, sizeof(flags)))
1354 return -EFAULT;
1355 return 0;
1356 }
1357
1358 STATIC int
1359 xfs_ioc_setxflags(
1360 xfs_inode_t *ip,
1361 struct file *filp,
1362 void __user *arg)
1363 {
1364 struct fsxattr fa;
1365 unsigned int flags;
1366 unsigned int mask;
1367 int error;
1368
1369 if (copy_from_user(&flags, arg, sizeof(flags)))
1370 return -EFAULT;
1371
1372 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
1373 FS_NOATIME_FL | FS_NODUMP_FL | \
1374 FS_SYNC_FL))
1375 return -EOPNOTSUPP;
1376
1377 mask = FSX_XFLAGS;
1378 if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
1379 mask |= FSX_NONBLOCK;
1380 fa.fsx_xflags = xfs_merge_ioc_xflags(flags, xfs_ip2xflags(ip));
1381
1382 error = mnt_want_write_file(filp);
1383 if (error)
1384 return error;
1385 error = xfs_ioctl_setattr(ip, &fa, mask);
1386 mnt_drop_write_file(filp);
1387 return -error;
1388 }
1389
1390 STATIC int
1391 xfs_getbmap_format(void **ap, struct getbmapx *bmv, int *full)
1392 {
1393 struct getbmap __user *base = *ap;
1394
1395 /* copy only getbmap portion (not getbmapx) */
1396 if (copy_to_user(base, bmv, sizeof(struct getbmap)))
1397 return XFS_ERROR(EFAULT);
1398
1399 *ap += sizeof(struct getbmap);
1400 return 0;
1401 }
1402
1403 STATIC int
1404 xfs_ioc_getbmap(
1405 struct xfs_inode *ip,
1406 int ioflags,
1407 unsigned int cmd,
1408 void __user *arg)
1409 {
1410 struct getbmapx bmx;
1411 int error;
1412
1413 if (copy_from_user(&bmx, arg, sizeof(struct getbmapx)))
1414 return -XFS_ERROR(EFAULT);
1415
1416 if (bmx.bmv_count < 2)
1417 return -XFS_ERROR(EINVAL);
1418
1419 bmx.bmv_iflags = (cmd == XFS_IOC_GETBMAPA ? BMV_IF_ATTRFORK : 0);
1420 if (ioflags & IO_INVIS)
1421 bmx.bmv_iflags |= BMV_IF_NO_DMAPI_READ;
1422
1423 error = xfs_getbmap(ip, &bmx, xfs_getbmap_format,
1424 (struct getbmap *)arg+1);
1425 if (error)
1426 return -error;
1427
1428 /* copy back header - only size of getbmap */
1429 if (copy_to_user(arg, &bmx, sizeof(struct getbmap)))
1430 return -XFS_ERROR(EFAULT);
1431 return 0;
1432 }
1433
1434 STATIC int
1435 xfs_getbmapx_format(void **ap, struct getbmapx *bmv, int *full)
1436 {
1437 struct getbmapx __user *base = *ap;
1438
1439 if (copy_to_user(base, bmv, sizeof(struct getbmapx)))
1440 return XFS_ERROR(EFAULT);
1441
1442 *ap += sizeof(struct getbmapx);
1443 return 0;
1444 }
1445
1446 STATIC int
1447 xfs_ioc_getbmapx(
1448 struct xfs_inode *ip,
1449 void __user *arg)
1450 {
1451 struct getbmapx bmx;
1452 int error;
1453
1454 if (copy_from_user(&bmx, arg, sizeof(bmx)))
1455 return -XFS_ERROR(EFAULT);
1456
1457 if (bmx.bmv_count < 2)
1458 return -XFS_ERROR(EINVAL);
1459
1460 if (bmx.bmv_iflags & (~BMV_IF_VALID))
1461 return -XFS_ERROR(EINVAL);
1462
1463 error = xfs_getbmap(ip, &bmx, xfs_getbmapx_format,
1464 (struct getbmapx *)arg+1);
1465 if (error)
1466 return -error;
1467
1468 /* copy back header */
1469 if (copy_to_user(arg, &bmx, sizeof(struct getbmapx)))
1470 return -XFS_ERROR(EFAULT);
1471
1472 return 0;
1473 }
1474
1475 int
1476 xfs_ioc_swapext(
1477 xfs_swapext_t *sxp)
1478 {
1479 xfs_inode_t *ip, *tip;
1480 struct fd f, tmp;
1481 int error = 0;
1482
1483 /* Pull information for the target fd */
1484 f = fdget((int)sxp->sx_fdtarget);
1485 if (!f.file) {
1486 error = XFS_ERROR(EINVAL);
1487 goto out;
1488 }
1489
1490 if (!(f.file->f_mode & FMODE_WRITE) ||
1491 !(f.file->f_mode & FMODE_READ) ||
1492 (f.file->f_flags & O_APPEND)) {
1493 error = XFS_ERROR(EBADF);
1494 goto out_put_file;
1495 }
1496
1497 tmp = fdget((int)sxp->sx_fdtmp);
1498 if (!tmp.file) {
1499 error = XFS_ERROR(EINVAL);
1500 goto out_put_file;
1501 }
1502
1503 if (!(tmp.file->f_mode & FMODE_WRITE) ||
1504 !(tmp.file->f_mode & FMODE_READ) ||
1505 (tmp.file->f_flags & O_APPEND)) {
1506 error = XFS_ERROR(EBADF);
1507 goto out_put_tmp_file;
1508 }
1509
1510 if (IS_SWAPFILE(file_inode(f.file)) ||
1511 IS_SWAPFILE(file_inode(tmp.file))) {
1512 error = XFS_ERROR(EINVAL);
1513 goto out_put_tmp_file;
1514 }
1515
1516 ip = XFS_I(file_inode(f.file));
1517 tip = XFS_I(file_inode(tmp.file));
1518
1519 if (ip->i_mount != tip->i_mount) {
1520 error = XFS_ERROR(EINVAL);
1521 goto out_put_tmp_file;
1522 }
1523
1524 if (ip->i_ino == tip->i_ino) {
1525 error = XFS_ERROR(EINVAL);
1526 goto out_put_tmp_file;
1527 }
1528
1529 if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
1530 error = XFS_ERROR(EIO);
1531 goto out_put_tmp_file;
1532 }
1533
1534 error = xfs_swap_extents(ip, tip, sxp);
1535
1536 out_put_tmp_file:
1537 fdput(tmp);
1538 out_put_file:
1539 fdput(f);
1540 out:
1541 return error;
1542 }
1543
1544 /*
1545 * Note: some of the ioctl's return positive numbers as a
1546 * byte count indicating success, such as readlink_by_handle.
1547 * So we don't "sign flip" like most other routines. This means
1548 * true errors need to be returned as a negative value.
1549 */
1550 long
1551 xfs_file_ioctl(
1552 struct file *filp,
1553 unsigned int cmd,
1554 unsigned long p)
1555 {
1556 struct inode *inode = file_inode(filp);
1557 struct xfs_inode *ip = XFS_I(inode);
1558 struct xfs_mount *mp = ip->i_mount;
1559 void __user *arg = (void __user *)p;
1560 int ioflags = 0;
1561 int error;
1562
1563 if (filp->f_mode & FMODE_NOCMTIME)
1564 ioflags |= IO_INVIS;
1565
1566 trace_xfs_file_ioctl(ip);
1567
1568 switch (cmd) {
1569 case FITRIM:
1570 return xfs_ioc_trim(mp, arg);
1571 case XFS_IOC_ALLOCSP:
1572 case XFS_IOC_FREESP:
1573 case XFS_IOC_RESVSP:
1574 case XFS_IOC_UNRESVSP:
1575 case XFS_IOC_ALLOCSP64:
1576 case XFS_IOC_FREESP64:
1577 case XFS_IOC_RESVSP64:
1578 case XFS_IOC_UNRESVSP64:
1579 case XFS_IOC_ZERO_RANGE: {
1580 xfs_flock64_t bf;
1581
1582 if (copy_from_user(&bf, arg, sizeof(bf)))
1583 return -XFS_ERROR(EFAULT);
1584 return xfs_ioc_space(ip, inode, filp, ioflags, cmd, &bf);
1585 }
1586 case XFS_IOC_DIOINFO: {
1587 struct dioattr da;
1588 xfs_buftarg_t *target =
1589 XFS_IS_REALTIME_INODE(ip) ?
1590 mp->m_rtdev_targp : mp->m_ddev_targp;
1591
1592 da.d_mem = da.d_miniosz = 1 << target->bt_sshift;
1593 da.d_maxiosz = INT_MAX & ~(da.d_miniosz - 1);
1594
1595 if (copy_to_user(arg, &da, sizeof(da)))
1596 return -XFS_ERROR(EFAULT);
1597 return 0;
1598 }
1599
1600 case XFS_IOC_FSBULKSTAT_SINGLE:
1601 case XFS_IOC_FSBULKSTAT:
1602 case XFS_IOC_FSINUMBERS:
1603 return xfs_ioc_bulkstat(mp, cmd, arg);
1604
1605 case XFS_IOC_FSGEOMETRY_V1:
1606 return xfs_ioc_fsgeometry_v1(mp, arg);
1607
1608 case XFS_IOC_FSGEOMETRY:
1609 return xfs_ioc_fsgeometry(mp, arg);
1610
1611 case XFS_IOC_GETVERSION:
1612 return put_user(inode->i_generation, (int __user *)arg);
1613
1614 case XFS_IOC_FSGETXATTR:
1615 return xfs_ioc_fsgetxattr(ip, 0, arg);
1616 case XFS_IOC_FSGETXATTRA:
1617 return xfs_ioc_fsgetxattr(ip, 1, arg);
1618 case XFS_IOC_FSSETXATTR:
1619 return xfs_ioc_fssetxattr(ip, filp, arg);
1620 case XFS_IOC_GETXFLAGS:
1621 return xfs_ioc_getxflags(ip, arg);
1622 case XFS_IOC_SETXFLAGS:
1623 return xfs_ioc_setxflags(ip, filp, arg);
1624
1625 case XFS_IOC_FSSETDM: {
1626 struct fsdmidata dmi;
1627
1628 if (copy_from_user(&dmi, arg, sizeof(dmi)))
1629 return -XFS_ERROR(EFAULT);
1630
1631 error = mnt_want_write_file(filp);
1632 if (error)
1633 return error;
1634
1635 error = xfs_set_dmattrs(ip, dmi.fsd_dmevmask,
1636 dmi.fsd_dmstate);
1637 mnt_drop_write_file(filp);
1638 return -error;
1639 }
1640
1641 case XFS_IOC_GETBMAP:
1642 case XFS_IOC_GETBMAPA:
1643 return xfs_ioc_getbmap(ip, ioflags, cmd, arg);
1644
1645 case XFS_IOC_GETBMAPX:
1646 return xfs_ioc_getbmapx(ip, arg);
1647
1648 case XFS_IOC_FD_TO_HANDLE:
1649 case XFS_IOC_PATH_TO_HANDLE:
1650 case XFS_IOC_PATH_TO_FSHANDLE: {
1651 xfs_fsop_handlereq_t hreq;
1652
1653 if (copy_from_user(&hreq, arg, sizeof(hreq)))
1654 return -XFS_ERROR(EFAULT);
1655 return xfs_find_handle(cmd, &hreq);
1656 }
1657 case XFS_IOC_OPEN_BY_HANDLE: {
1658 xfs_fsop_handlereq_t hreq;
1659
1660 if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
1661 return -XFS_ERROR(EFAULT);
1662 return xfs_open_by_handle(filp, &hreq);
1663 }
1664 case XFS_IOC_FSSETDM_BY_HANDLE:
1665 return xfs_fssetdm_by_handle(filp, arg);
1666
1667 case XFS_IOC_READLINK_BY_HANDLE: {
1668 xfs_fsop_handlereq_t hreq;
1669
1670 if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
1671 return -XFS_ERROR(EFAULT);
1672 return xfs_readlink_by_handle(filp, &hreq);
1673 }
1674 case XFS_IOC_ATTRLIST_BY_HANDLE:
1675 return xfs_attrlist_by_handle(filp, arg);
1676
1677 case XFS_IOC_ATTRMULTI_BY_HANDLE:
1678 return xfs_attrmulti_by_handle(filp, arg);
1679
1680 case XFS_IOC_SWAPEXT: {
1681 struct xfs_swapext sxp;
1682
1683 if (copy_from_user(&sxp, arg, sizeof(xfs_swapext_t)))
1684 return -XFS_ERROR(EFAULT);
1685 error = mnt_want_write_file(filp);
1686 if (error)
1687 return error;
1688 error = xfs_ioc_swapext(&sxp);
1689 mnt_drop_write_file(filp);
1690 return -error;
1691 }
1692
1693 case XFS_IOC_FSCOUNTS: {
1694 xfs_fsop_counts_t out;
1695
1696 error = xfs_fs_counts(mp, &out);
1697 if (error)
1698 return -error;
1699
1700 if (copy_to_user(arg, &out, sizeof(out)))
1701 return -XFS_ERROR(EFAULT);
1702 return 0;
1703 }
1704
1705 case XFS_IOC_SET_RESBLKS: {
1706 xfs_fsop_resblks_t inout;
1707 __uint64_t in;
1708
1709 if (!capable(CAP_SYS_ADMIN))
1710 return -EPERM;
1711
1712 if (mp->m_flags & XFS_MOUNT_RDONLY)
1713 return -XFS_ERROR(EROFS);
1714
1715 if (copy_from_user(&inout, arg, sizeof(inout)))
1716 return -XFS_ERROR(EFAULT);
1717
1718 error = mnt_want_write_file(filp);
1719 if (error)
1720 return error;
1721
1722 /* input parameter is passed in resblks field of structure */
1723 in = inout.resblks;
1724 error = xfs_reserve_blocks(mp, &in, &inout);
1725 mnt_drop_write_file(filp);
1726 if (error)
1727 return -error;
1728
1729 if (copy_to_user(arg, &inout, sizeof(inout)))
1730 return -XFS_ERROR(EFAULT);
1731 return 0;
1732 }
1733
1734 case XFS_IOC_GET_RESBLKS: {
1735 xfs_fsop_resblks_t out;
1736
1737 if (!capable(CAP_SYS_ADMIN))
1738 return -EPERM;
1739
1740 error = xfs_reserve_blocks(mp, NULL, &out);
1741 if (error)
1742 return -error;
1743
1744 if (copy_to_user(arg, &out, sizeof(out)))
1745 return -XFS_ERROR(EFAULT);
1746
1747 return 0;
1748 }
1749
1750 case XFS_IOC_FSGROWFSDATA: {
1751 xfs_growfs_data_t in;
1752
1753 if (copy_from_user(&in, arg, sizeof(in)))
1754 return -XFS_ERROR(EFAULT);
1755
1756 error = mnt_want_write_file(filp);
1757 if (error)
1758 return error;
1759 error = xfs_growfs_data(mp, &in);
1760 mnt_drop_write_file(filp);
1761 return -error;
1762 }
1763
1764 case XFS_IOC_FSGROWFSLOG: {
1765 xfs_growfs_log_t in;
1766
1767 if (copy_from_user(&in, arg, sizeof(in)))
1768 return -XFS_ERROR(EFAULT);
1769
1770 error = mnt_want_write_file(filp);
1771 if (error)
1772 return error;
1773 error = xfs_growfs_log(mp, &in);
1774 mnt_drop_write_file(filp);
1775 return -error;
1776 }
1777
1778 case XFS_IOC_FSGROWFSRT: {
1779 xfs_growfs_rt_t in;
1780
1781 if (copy_from_user(&in, arg, sizeof(in)))
1782 return -XFS_ERROR(EFAULT);
1783
1784 error = mnt_want_write_file(filp);
1785 if (error)
1786 return error;
1787 error = xfs_growfs_rt(mp, &in);
1788 mnt_drop_write_file(filp);
1789 return -error;
1790 }
1791
1792 case XFS_IOC_GOINGDOWN: {
1793 __uint32_t in;
1794
1795 if (!capable(CAP_SYS_ADMIN))
1796 return -EPERM;
1797
1798 if (get_user(in, (__uint32_t __user *)arg))
1799 return -XFS_ERROR(EFAULT);
1800
1801 error = xfs_fs_goingdown(mp, in);
1802 return -error;
1803 }
1804
1805 case XFS_IOC_ERROR_INJECTION: {
1806 xfs_error_injection_t in;
1807
1808 if (!capable(CAP_SYS_ADMIN))
1809 return -EPERM;
1810
1811 if (copy_from_user(&in, arg, sizeof(in)))
1812 return -XFS_ERROR(EFAULT);
1813
1814 error = xfs_errortag_add(in.errtag, mp);
1815 return -error;
1816 }
1817
1818 case XFS_IOC_ERROR_CLEARALL:
1819 if (!capable(CAP_SYS_ADMIN))
1820 return -EPERM;
1821
1822 error = xfs_errortag_clearall(mp, 1);
1823 return -error;
1824
1825 case XFS_IOC_FREE_EOFBLOCKS: {
1826 struct xfs_fs_eofblocks eofb;
1827 struct xfs_eofblocks keofb;
1828
1829 if (!capable(CAP_SYS_ADMIN))
1830 return -EPERM;
1831
1832 if (mp->m_flags & XFS_MOUNT_RDONLY)
1833 return -XFS_ERROR(EROFS);
1834
1835 if (copy_from_user(&eofb, arg, sizeof(eofb)))
1836 return -XFS_ERROR(EFAULT);
1837
1838 error = xfs_fs_eofblocks_from_user(&eofb, &keofb);
1839 if (error)
1840 return -error;
1841
1842 return -xfs_icache_free_eofblocks(mp, &keofb);
1843 }
1844
1845 default:
1846 return -ENOTTY;
1847 }
1848 }
This page took 0.204293 seconds and 5 git commands to generate.