2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
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.
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.
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
23 #include "xfs_trans.h"
27 #include "xfs_alloc.h"
28 #include "xfs_dmapi.h"
29 #include "xfs_mount.h"
30 #include "xfs_bmap_btree.h"
31 #include "xfs_alloc_btree.h"
32 #include "xfs_ialloc_btree.h"
33 #include "xfs_attr_sf.h"
34 #include "xfs_dir2_sf.h"
35 #include "xfs_dinode.h"
36 #include "xfs_inode.h"
37 #include "xfs_btree.h"
38 #include "xfs_ialloc.h"
39 #include "xfs_rtalloc.h"
40 #include "xfs_itable.h"
41 #include "xfs_error.h"
46 #include "xfs_buf_item.h"
47 #include "xfs_utils.h"
48 #include "xfs_dfrag.h"
49 #include "xfs_fsops.h"
50 #include "xfs_vnodeops.h"
51 #include "xfs_quota.h"
52 #include "xfs_inode_item.h"
54 #include <linux/capability.h>
55 #include <linux/dcache.h>
56 #include <linux/mount.h>
57 #include <linux/namei.h>
58 #include <linux/pagemap.h>
61 * xfs_find_handle maps from userspace xfs_fsop_handlereq structure to
62 * a file or fs handle.
64 * XFS_IOC_PATH_TO_FSHANDLE
65 * returns fs handle for a mount point or path within that mount point
66 * XFS_IOC_FD_TO_HANDLE
67 * returns full handle for a FD opened in user space
68 * XFS_IOC_PATH_TO_HANDLE
69 * returns full handle for a path
78 xfs_fsop_handlereq_t hreq
;
81 if (copy_from_user(&hreq
, arg
, sizeof(hreq
)))
82 return -XFS_ERROR(EFAULT
);
84 memset((char *)&handle
, 0, sizeof(handle
));
87 case XFS_IOC_PATH_TO_FSHANDLE
:
88 case XFS_IOC_PATH_TO_HANDLE
: {
90 int error
= user_lpath((const char __user
*)hreq
.path
, &path
);
95 ASSERT(path
.dentry
->d_inode
);
96 inode
= igrab(path
.dentry
->d_inode
);
101 case XFS_IOC_FD_TO_HANDLE
: {
104 file
= fget(hreq
.fd
);
108 ASSERT(file
->f_path
.dentry
);
109 ASSERT(file
->f_path
.dentry
->d_inode
);
110 inode
= igrab(file
->f_path
.dentry
->d_inode
);
117 return -XFS_ERROR(EINVAL
);
120 if (inode
->i_sb
->s_magic
!= XFS_SB_MAGIC
) {
121 /* we're not in XFS anymore, Toto */
123 return -XFS_ERROR(EINVAL
);
126 switch (inode
->i_mode
& S_IFMT
) {
133 return -XFS_ERROR(EBADF
);
136 /* now we can grab the fsid */
137 memcpy(&handle
.ha_fsid
, XFS_I(inode
)->i_mount
->m_fixedfsid
,
139 hsize
= sizeof(xfs_fsid_t
);
141 if (cmd
!= XFS_IOC_PATH_TO_FSHANDLE
) {
142 xfs_inode_t
*ip
= XFS_I(inode
);
145 /* need to get access to the xfs_inode to read the generation */
146 lock_mode
= xfs_ilock_map_shared(ip
);
148 /* fill in fid section of handle from inode */
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
;
155 xfs_iunlock_map_shared(ip
, lock_mode
);
157 hsize
= XFS_HSIZE(handle
);
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
))) {
164 return -XFS_ERROR(EFAULT
);
173 * Convert userspace handle data into inode.
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.
179 * If no error, caller must always iput the returned inode.
182 xfs_vget_fsop_handlereq(
184 struct inode
*parinode
, /* parent inode pointer */
185 xfs_fsop_handlereq_t
*hreq
,
186 struct inode
**inode
)
191 xfs_handle_t
*handlep
;
199 * Only allow handle opens under a directory.
201 if (!S_ISDIR(parinode
->i_mode
))
202 return XFS_ERROR(ENOTDIR
);
204 hanp
= hreq
->ihandle
;
205 hlen
= hreq
->ihandlen
;
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
)) {
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
)
219 return XFS_ERROR(EINVAL
);
223 * Crack the handle, obtain the inode # & generation #
225 xfid
= (struct xfs_fid
*)&handlep
->ha_fid
;
226 if (xfid
->fid_len
== sizeof(*xfid
) - sizeof(xfid
->fid_len
)) {
228 igen
= xfid
->fid_gen
;
230 return XFS_ERROR(EINVAL
);
234 * Get the XFS inode, building a Linux inode to go with it.
236 error
= xfs_iget(mp
, NULL
, ino
, 0, XFS_ILOCK_SHARED
, &ip
, 0);
240 return XFS_ERROR(EIO
);
241 if (ip
->i_d
.di_gen
!= igen
) {
242 xfs_iput_new(ip
, XFS_ILOCK_SHARED
);
243 return XFS_ERROR(ENOENT
);
246 xfs_iunlock(ip
, XFS_ILOCK_SHARED
);
256 struct file
*parfilp
,
257 struct inode
*parinode
)
264 struct dentry
*dentry
;
265 xfs_fsop_handlereq_t hreq
;
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
);
272 error
= xfs_vget_fsop_handlereq(mp
, parinode
, &hreq
, &inode
);
276 /* Restrict xfs_open_by_handle to directories & regular files. */
277 if (!(S_ISREG(inode
->i_mode
) || S_ISDIR(inode
->i_mode
))) {
279 return -XFS_ERROR(EINVAL
);
282 #if BITS_PER_LONG != 32
283 hreq
.oflags
|= O_LARGEFILE
;
285 /* Put open permission in namei format. */
286 permflag
= hreq
.oflags
;
287 if ((permflag
+1) & O_ACCMODE
)
289 if (permflag
& O_TRUNC
)
292 if ((!(permflag
& O_APPEND
) || (permflag
& O_TRUNC
)) &&
293 (permflag
& FMODE_WRITE
) && IS_APPEND(inode
)) {
295 return -XFS_ERROR(EPERM
);
298 if ((permflag
& FMODE_WRITE
) && IS_IMMUTABLE(inode
)) {
300 return -XFS_ERROR(EACCES
);
303 /* Can't write directories. */
304 if ( S_ISDIR(inode
->i_mode
) && (permflag
& FMODE_WRITE
)) {
306 return -XFS_ERROR(EISDIR
);
309 if ((new_fd
= get_unused_fd()) < 0) {
314 dentry
= d_alloc_anon(inode
);
315 if (dentry
== NULL
) {
317 put_unused_fd(new_fd
);
318 return -XFS_ERROR(ENOMEM
);
321 /* Ensure umount returns EBUSY on umounts while this file is open. */
322 mntget(parfilp
->f_path
.mnt
);
324 /* Create file pointer. */
325 filp
= dentry_open(dentry
, parfilp
->f_path
.mnt
, hreq
.oflags
);
327 put_unused_fd(new_fd
);
328 return -XFS_ERROR(-PTR_ERR(filp
));
330 if (inode
->i_mode
& S_IFREG
) {
331 /* invisible operation should not change atime */
332 filp
->f_flags
|= O_NOATIME
;
333 filp
->f_op
= &xfs_invis_file_operations
;
336 fd_install(new_fd
, filp
);
341 * This is a copy from fs/namei.c:vfs_readlink(), except for removing it's
342 * unused first argument.
357 if (len
> (unsigned) buflen
)
359 if (copy_to_user(buffer
, link
, len
))
367 xfs_readlink_by_handle(
370 struct inode
*parinode
)
373 xfs_fsop_handlereq_t hreq
;
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
);
383 error
= xfs_vget_fsop_handlereq(mp
, parinode
, &hreq
, &inode
);
387 /* Restrict this handle operation to symlinks only. */
388 if (!S_ISLNK(inode
->i_mode
)) {
389 error
= -XFS_ERROR(EINVAL
);
393 if (copy_from_user(&olen
, hreq
.ohandlen
, sizeof(__u32
))) {
394 error
= -XFS_ERROR(EFAULT
);
398 link
= kmalloc(MAXPATHLEN
+1, GFP_KERNEL
);
402 error
= -xfs_readlink(XFS_I(inode
), link
);
405 error
= do_readlink(hreq
.ohandle
, olen
, link
);
417 xfs_fssetdm_by_handle(
420 struct inode
*parinode
)
423 struct fsdmidata fsd
;
424 xfs_fsop_setdm_handlereq_t dmhreq
;
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
);
432 error
= xfs_vget_fsop_handlereq(mp
, parinode
, &dmhreq
.hreq
, &inode
);
436 if (IS_IMMUTABLE(inode
) || IS_APPEND(inode
)) {
437 error
= -XFS_ERROR(EPERM
);
441 if (copy_from_user(&fsd
, dmhreq
.data
, sizeof(fsd
))) {
442 error
= -XFS_ERROR(EFAULT
);
446 error
= -xfs_set_dmattrs(XFS_I(inode
), fsd
.fsd_dmevmask
,
455 xfs_attrlist_by_handle(
458 struct inode
*parinode
)
461 attrlist_cursor_kern_t
*cursor
;
462 xfs_fsop_attrlist_handlereq_t al_hreq
;
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
);
474 * Reject flags, only allow namespaces.
476 if (al_hreq
.flags
& ~(ATTR_ROOT
| ATTR_SECURE
))
477 return -XFS_ERROR(EINVAL
);
479 error
= xfs_vget_fsop_handlereq(mp
, parinode
, &al_hreq
.hreq
, &inode
);
483 kbuf
= kmalloc(al_hreq
.buflen
, GFP_KERNEL
);
487 cursor
= (attrlist_cursor_kern_t
*)&al_hreq
.pos
;
488 error
= xfs_attr_list(XFS_I(inode
), kbuf
, al_hreq
.buflen
,
489 al_hreq
.flags
, cursor
);
493 if (copy_to_user(al_hreq
.buffer
, kbuf
, al_hreq
.buflen
))
505 xfs_attrmulti_attr_get(
515 if (*len
> XATTR_SIZE_MAX
)
517 kbuf
= kmalloc(*len
, GFP_KERNEL
);
521 error
= xfs_attr_get(XFS_I(inode
), name
, kbuf
, (int *)len
, flags
);
525 if (copy_to_user(ubuf
, kbuf
, *len
))
534 xfs_attrmulti_attr_set(
537 const char __user
*ubuf
,
544 if (IS_IMMUTABLE(inode
) || IS_APPEND(inode
))
546 if (len
> XATTR_SIZE_MAX
)
549 kbuf
= kmalloc(len
, GFP_KERNEL
);
553 if (copy_from_user(kbuf
, ubuf
, len
))
556 error
= xfs_attr_set(XFS_I(inode
), name
, kbuf
, len
, flags
);
564 xfs_attrmulti_attr_remove(
569 if (IS_IMMUTABLE(inode
) || IS_APPEND(inode
))
571 return xfs_attr_remove(XFS_I(inode
), name
, flags
);
575 xfs_attrmulti_by_handle(
578 struct file
*parfilp
,
579 struct inode
*parinode
)
582 xfs_attr_multiop_t
*ops
;
583 xfs_fsop_attrmulti_handlereq_t am_hreq
;
585 unsigned int i
, size
;
588 if (!capable(CAP_SYS_ADMIN
))
589 return -XFS_ERROR(EPERM
);
590 if (copy_from_user(&am_hreq
, arg
, sizeof(xfs_fsop_attrmulti_handlereq_t
)))
591 return -XFS_ERROR(EFAULT
);
593 error
= xfs_vget_fsop_handlereq(mp
, parinode
, &am_hreq
.hreq
, &inode
);
598 size
= am_hreq
.opcount
* sizeof(xfs_attr_multiop_t
);
599 if (!size
|| size
> 16 * PAGE_SIZE
)
603 ops
= kmalloc(size
, GFP_KERNEL
);
608 if (copy_from_user(ops
, am_hreq
.ops
, size
))
611 attr_name
= kmalloc(MAXNAMELEN
, GFP_KERNEL
);
617 for (i
= 0; i
< am_hreq
.opcount
; i
++) {
618 ops
[i
].am_error
= strncpy_from_user(attr_name
,
619 ops
[i
].am_attrname
, MAXNAMELEN
);
620 if (ops
[i
].am_error
== 0 || ops
[i
].am_error
== MAXNAMELEN
)
622 if (ops
[i
].am_error
< 0)
625 switch (ops
[i
].am_opcode
) {
627 ops
[i
].am_error
= xfs_attrmulti_attr_get(inode
,
628 attr_name
, ops
[i
].am_attrvalue
,
629 &ops
[i
].am_length
, ops
[i
].am_flags
);
632 ops
[i
].am_error
= mnt_want_write(parfilp
->f_path
.mnt
);
635 ops
[i
].am_error
= xfs_attrmulti_attr_set(inode
,
636 attr_name
, ops
[i
].am_attrvalue
,
637 ops
[i
].am_length
, ops
[i
].am_flags
);
638 mnt_drop_write(parfilp
->f_path
.mnt
);
641 ops
[i
].am_error
= mnt_want_write(parfilp
->f_path
.mnt
);
644 ops
[i
].am_error
= xfs_attrmulti_attr_remove(inode
,
645 attr_name
, ops
[i
].am_flags
);
646 mnt_drop_write(parfilp
->f_path
.mnt
);
649 ops
[i
].am_error
= EINVAL
;
653 if (copy_to_user(am_hreq
.ops
, ops
, size
))
654 error
= XFS_ERROR(EFAULT
);
667 struct xfs_inode
*ip
,
678 if (inode
->i_flags
& (S_IMMUTABLE
|S_APPEND
))
679 return -XFS_ERROR(EPERM
);
681 if (!(filp
->f_mode
& FMODE_WRITE
))
682 return -XFS_ERROR(EBADF
);
684 if (!S_ISREG(inode
->i_mode
))
685 return -XFS_ERROR(EINVAL
);
687 if (copy_from_user(&bf
, arg
, sizeof(bf
)))
688 return -XFS_ERROR(EFAULT
);
690 if (filp
->f_flags
& (O_NDELAY
|O_NONBLOCK
))
691 attr_flags
|= XFS_ATTR_NONBLOCK
;
692 if (ioflags
& IO_INVIS
)
693 attr_flags
|= XFS_ATTR_DMI
;
695 error
= xfs_change_file_space(ip
, cmd
, &bf
, filp
->f_pos
,
706 xfs_fsop_bulkreq_t bulkreq
;
707 int count
; /* # of records returned */
708 xfs_ino_t inlast
; /* last inode number */
712 /* done = 1 if there are more stats to get and if bulkstat */
713 /* should be called again (unused here, but used in dmapi) */
715 if (!capable(CAP_SYS_ADMIN
))
718 if (XFS_FORCED_SHUTDOWN(mp
))
719 return -XFS_ERROR(EIO
);
721 if (copy_from_user(&bulkreq
, arg
, sizeof(xfs_fsop_bulkreq_t
)))
722 return -XFS_ERROR(EFAULT
);
724 if (copy_from_user(&inlast
, bulkreq
.lastip
, sizeof(__s64
)))
725 return -XFS_ERROR(EFAULT
);
727 if ((count
= bulkreq
.icount
) <= 0)
728 return -XFS_ERROR(EINVAL
);
730 if (bulkreq
.ubuffer
== NULL
)
731 return -XFS_ERROR(EINVAL
);
733 if (cmd
== XFS_IOC_FSINUMBERS
)
734 error
= xfs_inumbers(mp
, &inlast
, &count
,
735 bulkreq
.ubuffer
, xfs_inumbers_fmt
);
736 else if (cmd
== XFS_IOC_FSBULKSTAT_SINGLE
)
737 error
= xfs_bulkstat_single(mp
, &inlast
,
738 bulkreq
.ubuffer
, &done
);
739 else /* XFS_IOC_FSBULKSTAT */
740 error
= xfs_bulkstat(mp
, &inlast
, &count
,
741 (bulkstat_one_pf
)xfs_bulkstat_one
, NULL
,
742 sizeof(xfs_bstat_t
), bulkreq
.ubuffer
,
743 BULKSTAT_FG_QUICK
, &done
);
748 if (bulkreq
.ocount
!= NULL
) {
749 if (copy_to_user(bulkreq
.lastip
, &inlast
,
751 return -XFS_ERROR(EFAULT
);
753 if (copy_to_user(bulkreq
.ocount
, &count
, sizeof(count
)))
754 return -XFS_ERROR(EFAULT
);
761 xfs_ioc_fsgeometry_v1(
765 xfs_fsop_geom_v1_t fsgeo
;
768 error
= xfs_fs_geometry(mp
, (xfs_fsop_geom_t
*)&fsgeo
, 3);
772 if (copy_to_user(arg
, &fsgeo
, sizeof(fsgeo
)))
773 return -XFS_ERROR(EFAULT
);
782 xfs_fsop_geom_t fsgeo
;
785 error
= xfs_fs_geometry(mp
, &fsgeo
, 4);
789 if (copy_to_user(arg
, &fsgeo
, sizeof(fsgeo
)))
790 return -XFS_ERROR(EFAULT
);
795 * Linux extended inode flags interface.
799 xfs_merge_ioc_xflags(
803 unsigned int xflags
= start
;
805 if (flags
& FS_IMMUTABLE_FL
)
806 xflags
|= XFS_XFLAG_IMMUTABLE
;
808 xflags
&= ~XFS_XFLAG_IMMUTABLE
;
809 if (flags
& FS_APPEND_FL
)
810 xflags
|= XFS_XFLAG_APPEND
;
812 xflags
&= ~XFS_XFLAG_APPEND
;
813 if (flags
& FS_SYNC_FL
)
814 xflags
|= XFS_XFLAG_SYNC
;
816 xflags
&= ~XFS_XFLAG_SYNC
;
817 if (flags
& FS_NOATIME_FL
)
818 xflags
|= XFS_XFLAG_NOATIME
;
820 xflags
&= ~XFS_XFLAG_NOATIME
;
821 if (flags
& FS_NODUMP_FL
)
822 xflags
|= XFS_XFLAG_NODUMP
;
824 xflags
&= ~XFS_XFLAG_NODUMP
;
833 unsigned int flags
= 0;
835 if (di_flags
& XFS_DIFLAG_IMMUTABLE
)
836 flags
|= FS_IMMUTABLE_FL
;
837 if (di_flags
& XFS_DIFLAG_APPEND
)
838 flags
|= FS_APPEND_FL
;
839 if (di_flags
& XFS_DIFLAG_SYNC
)
841 if (di_flags
& XFS_DIFLAG_NOATIME
)
842 flags
|= FS_NOATIME_FL
;
843 if (di_flags
& XFS_DIFLAG_NODUMP
)
844 flags
|= FS_NODUMP_FL
;
856 xfs_ilock(ip
, XFS_ILOCK_SHARED
);
857 fa
.fsx_xflags
= xfs_ip2xflags(ip
);
858 fa
.fsx_extsize
= ip
->i_d
.di_extsize
<< ip
->i_mount
->m_sb
.sb_blocklog
;
859 fa
.fsx_projid
= ip
->i_d
.di_projid
;
863 if (ip
->i_afp
->if_flags
& XFS_IFEXTENTS
)
864 fa
.fsx_nextents
= ip
->i_afp
->if_bytes
/
865 sizeof(xfs_bmbt_rec_t
);
867 fa
.fsx_nextents
= ip
->i_d
.di_anextents
;
871 if (ip
->i_df
.if_flags
& XFS_IFEXTENTS
)
872 fa
.fsx_nextents
= ip
->i_df
.if_bytes
/
873 sizeof(xfs_bmbt_rec_t
);
875 fa
.fsx_nextents
= ip
->i_d
.di_nextents
;
877 xfs_iunlock(ip
, XFS_ILOCK_SHARED
);
879 if (copy_to_user(arg
, &fa
, sizeof(fa
)))
886 struct xfs_inode
*ip
,
889 unsigned int di_flags
;
891 /* can't set PREALLOC this way, just preserve it */
892 di_flags
= (ip
->i_d
.di_flags
& XFS_DIFLAG_PREALLOC
);
893 if (xflags
& XFS_XFLAG_IMMUTABLE
)
894 di_flags
|= XFS_DIFLAG_IMMUTABLE
;
895 if (xflags
& XFS_XFLAG_APPEND
)
896 di_flags
|= XFS_DIFLAG_APPEND
;
897 if (xflags
& XFS_XFLAG_SYNC
)
898 di_flags
|= XFS_DIFLAG_SYNC
;
899 if (xflags
& XFS_XFLAG_NOATIME
)
900 di_flags
|= XFS_DIFLAG_NOATIME
;
901 if (xflags
& XFS_XFLAG_NODUMP
)
902 di_flags
|= XFS_DIFLAG_NODUMP
;
903 if (xflags
& XFS_XFLAG_PROJINHERIT
)
904 di_flags
|= XFS_DIFLAG_PROJINHERIT
;
905 if (xflags
& XFS_XFLAG_NODEFRAG
)
906 di_flags
|= XFS_DIFLAG_NODEFRAG
;
907 if (xflags
& XFS_XFLAG_FILESTREAM
)
908 di_flags
|= XFS_DIFLAG_FILESTREAM
;
909 if ((ip
->i_d
.di_mode
& S_IFMT
) == S_IFDIR
) {
910 if (xflags
& XFS_XFLAG_RTINHERIT
)
911 di_flags
|= XFS_DIFLAG_RTINHERIT
;
912 if (xflags
& XFS_XFLAG_NOSYMLINKS
)
913 di_flags
|= XFS_DIFLAG_NOSYMLINKS
;
914 if (xflags
& XFS_XFLAG_EXTSZINHERIT
)
915 di_flags
|= XFS_DIFLAG_EXTSZINHERIT
;
916 } else if ((ip
->i_d
.di_mode
& S_IFMT
) == S_IFREG
) {
917 if (xflags
& XFS_XFLAG_REALTIME
)
918 di_flags
|= XFS_DIFLAG_REALTIME
;
919 if (xflags
& XFS_XFLAG_EXTSIZE
)
920 di_flags
|= XFS_DIFLAG_EXTSIZE
;
923 ip
->i_d
.di_flags
= di_flags
;
927 xfs_diflags_to_linux(
928 struct xfs_inode
*ip
)
930 struct inode
*inode
= VFS_I(ip
);
931 unsigned int xflags
= xfs_ip2xflags(ip
);
933 if (xflags
& XFS_XFLAG_IMMUTABLE
)
934 inode
->i_flags
|= S_IMMUTABLE
;
936 inode
->i_flags
&= ~S_IMMUTABLE
;
937 if (xflags
& XFS_XFLAG_APPEND
)
938 inode
->i_flags
|= S_APPEND
;
940 inode
->i_flags
&= ~S_APPEND
;
941 if (xflags
& XFS_XFLAG_SYNC
)
942 inode
->i_flags
|= S_SYNC
;
944 inode
->i_flags
&= ~S_SYNC
;
945 if (xflags
& XFS_XFLAG_NOATIME
)
946 inode
->i_flags
|= S_NOATIME
;
948 inode
->i_flags
&= ~S_NOATIME
;
952 #define FSX_EXTSIZE 2
954 #define FSX_NONBLOCK 8
962 struct xfs_mount
*mp
= ip
->i_mount
;
963 struct xfs_trans
*tp
;
964 unsigned int lock_flags
= 0;
965 struct xfs_dquot
*udqp
= NULL
, *gdqp
= NULL
;
966 struct xfs_dquot
*olddquot
= NULL
;
969 xfs_itrace_entry(ip
);
971 if (mp
->m_flags
& XFS_MOUNT_RDONLY
)
972 return XFS_ERROR(EROFS
);
973 if (XFS_FORCED_SHUTDOWN(mp
))
974 return XFS_ERROR(EIO
);
977 * If disk quotas is on, we make sure that the dquots do exist on disk,
978 * before we start any other transactions. Trying to do this later
979 * is messy. We don't care to take a readlock to look at the ids
980 * in inode here, because we can't hold it across the trans_reserve.
981 * If the IDs do change before we take the ilock, we're covered
982 * because the i_*dquot fields will get updated anyway.
984 if (XFS_IS_QUOTA_ON(mp
) && (mask
& FSX_PROJID
)) {
985 code
= XFS_QM_DQVOPALLOC(mp
, ip
, ip
->i_d
.di_uid
,
986 ip
->i_d
.di_gid
, fa
->fsx_projid
,
987 XFS_QMOPT_PQUOTA
, &udqp
, &gdqp
);
993 * For the other attributes, we acquire the inode lock and
994 * first do an error checking pass.
996 tp
= xfs_trans_alloc(mp
, XFS_TRANS_SETATTR_NOT_SIZE
);
997 code
= xfs_trans_reserve(tp
, 0, XFS_ICHANGE_LOG_RES(mp
), 0, 0, 0);
1001 lock_flags
= XFS_ILOCK_EXCL
;
1002 xfs_ilock(ip
, lock_flags
);
1005 * CAP_FOWNER overrides the following restrictions:
1007 * The user ID of the calling process must be equal
1008 * to the file owner ID, except in cases where the
1009 * CAP_FSETID capability is applicable.
1011 if (current
->fsuid
!= ip
->i_d
.di_uid
&& !capable(CAP_FOWNER
)) {
1012 code
= XFS_ERROR(EPERM
);
1017 * Do a quota reservation only if projid is actually going to change.
1019 if (mask
& FSX_PROJID
) {
1020 if (XFS_IS_PQUOTA_ON(mp
) &&
1021 ip
->i_d
.di_projid
!= fa
->fsx_projid
) {
1023 code
= XFS_QM_DQVOPCHOWNRESV(mp
, tp
, ip
, udqp
, gdqp
,
1024 capable(CAP_FOWNER
) ?
1025 XFS_QMOPT_FORCE_RES
: 0);
1026 if (code
) /* out of quota */
1031 if (mask
& FSX_EXTSIZE
) {
1033 * Can't change extent size if any extents are allocated.
1035 if (ip
->i_d
.di_nextents
&&
1036 ((ip
->i_d
.di_extsize
<< mp
->m_sb
.sb_blocklog
) !=
1038 code
= XFS_ERROR(EINVAL
); /* EFBIG? */
1043 * Extent size must be a multiple of the appropriate block
1044 * size, if set at all.
1046 if (fa
->fsx_extsize
!= 0) {
1049 if (XFS_IS_REALTIME_INODE(ip
) ||
1050 ((mask
& FSX_XFLAGS
) &&
1051 (fa
->fsx_xflags
& XFS_XFLAG_REALTIME
))) {
1052 size
= mp
->m_sb
.sb_rextsize
<<
1053 mp
->m_sb
.sb_blocklog
;
1055 size
= mp
->m_sb
.sb_blocksize
;
1058 if (fa
->fsx_extsize
% size
) {
1059 code
= XFS_ERROR(EINVAL
);
1066 if (mask
& FSX_XFLAGS
) {
1068 * Can't change realtime flag if any extents are allocated.
1070 if ((ip
->i_d
.di_nextents
|| ip
->i_delayed_blks
) &&
1071 (XFS_IS_REALTIME_INODE(ip
)) !=
1072 (fa
->fsx_xflags
& XFS_XFLAG_REALTIME
)) {
1073 code
= XFS_ERROR(EINVAL
); /* EFBIG? */
1078 * If realtime flag is set then must have realtime data.
1080 if ((fa
->fsx_xflags
& XFS_XFLAG_REALTIME
)) {
1081 if ((mp
->m_sb
.sb_rblocks
== 0) ||
1082 (mp
->m_sb
.sb_rextsize
== 0) ||
1083 (ip
->i_d
.di_extsize
% mp
->m_sb
.sb_rextsize
)) {
1084 code
= XFS_ERROR(EINVAL
);
1090 * Can't modify an immutable/append-only file unless
1091 * we have appropriate permission.
1093 if ((ip
->i_d
.di_flags
&
1094 (XFS_DIFLAG_IMMUTABLE
|XFS_DIFLAG_APPEND
) ||
1096 (XFS_XFLAG_IMMUTABLE
| XFS_XFLAG_APPEND
))) &&
1097 !capable(CAP_LINUX_IMMUTABLE
)) {
1098 code
= XFS_ERROR(EPERM
);
1103 xfs_trans_ijoin(tp
, ip
, lock_flags
);
1104 xfs_trans_ihold(tp
, ip
);
1107 * Change file ownership. Must be the owner or privileged.
1108 * If the system was configured with the "restricted_chown"
1109 * option, the owner is not permitted to give away the file,
1110 * and can change the group id only to a group of which he
1111 * or she is a member.
1113 if (mask
& FSX_PROJID
) {
1115 * CAP_FSETID overrides the following restrictions:
1117 * The set-user-ID and set-group-ID bits of a file will be
1118 * cleared upon successful return from chown()
1120 if ((ip
->i_d
.di_mode
& (S_ISUID
|S_ISGID
)) &&
1121 !capable(CAP_FSETID
))
1122 ip
->i_d
.di_mode
&= ~(S_ISUID
|S_ISGID
);
1125 * Change the ownerships and register quota modifications
1126 * in the transaction.
1128 if (ip
->i_d
.di_projid
!= fa
->fsx_projid
) {
1129 if (XFS_IS_PQUOTA_ON(mp
)) {
1130 olddquot
= XFS_QM_DQVOPCHOWN(mp
, tp
, ip
,
1131 &ip
->i_gdquot
, gdqp
);
1133 ip
->i_d
.di_projid
= fa
->fsx_projid
;
1136 * We may have to rev the inode as well as
1137 * the superblock version number since projids didn't
1138 * exist before DINODE_VERSION_2 and SB_VERSION_NLINK.
1140 if (ip
->i_d
.di_version
== XFS_DINODE_VERSION_1
)
1141 xfs_bump_ino_vers2(tp
, ip
);
1146 if (mask
& FSX_EXTSIZE
)
1147 ip
->i_d
.di_extsize
= fa
->fsx_extsize
>> mp
->m_sb
.sb_blocklog
;
1148 if (mask
& FSX_XFLAGS
) {
1149 xfs_set_diflags(ip
, fa
->fsx_xflags
);
1150 xfs_diflags_to_linux(ip
);
1153 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
1154 xfs_ichgtime(ip
, XFS_ICHGTIME_CHG
);
1156 XFS_STATS_INC(xs_ig_attrchg
);
1159 * If this is a synchronous mount, make sure that the
1160 * transaction goes to disk before returning to the user.
1161 * This is slightly sub-optimal in that truncates require
1162 * two sync transactions instead of one for wsync filesystems.
1163 * One for the truncate and one for the timestamps since we
1164 * don't want to change the timestamps unless we're sure the
1165 * truncate worked. Truncates are less than 1% of the laddis
1166 * mix so this probably isn't worth the trouble to optimize.
1168 if (mp
->m_flags
& XFS_MOUNT_WSYNC
)
1169 xfs_trans_set_sync(tp
);
1170 code
= xfs_trans_commit(tp
, 0);
1171 xfs_iunlock(ip
, lock_flags
);
1174 * Release any dquot(s) the inode had kept before chown.
1176 XFS_QM_DQRELE(mp
, olddquot
);
1177 XFS_QM_DQRELE(mp
, udqp
);
1178 XFS_QM_DQRELE(mp
, gdqp
);
1183 if (DM_EVENT_ENABLED(ip
, DM_EVENT_ATTRIBUTE
)) {
1184 XFS_SEND_NAMESP(mp
, DM_EVENT_ATTRIBUTE
, ip
, DM_RIGHT_NULL
,
1185 NULL
, DM_RIGHT_NULL
, NULL
, NULL
, 0, 0,
1186 (mask
& FSX_NONBLOCK
) ? DM_FLAGS_NDELAY
: 0);
1192 XFS_QM_DQRELE(mp
, udqp
);
1193 XFS_QM_DQRELE(mp
, gdqp
);
1194 xfs_trans_cancel(tp
, 0);
1196 xfs_iunlock(ip
, lock_flags
);
1209 if (copy_from_user(&fa
, arg
, sizeof(fa
)))
1212 mask
= FSX_XFLAGS
| FSX_EXTSIZE
| FSX_PROJID
;
1213 if (filp
->f_flags
& (O_NDELAY
|O_NONBLOCK
))
1214 mask
|= FSX_NONBLOCK
;
1216 return -xfs_ioctl_setattr(ip
, &fa
, mask
);
1226 flags
= xfs_di2lxflags(ip
->i_d
.di_flags
);
1227 if (copy_to_user(arg
, &flags
, sizeof(flags
)))
1242 if (copy_from_user(&flags
, arg
, sizeof(flags
)))
1245 if (flags
& ~(FS_IMMUTABLE_FL
| FS_APPEND_FL
| \
1246 FS_NOATIME_FL
| FS_NODUMP_FL
| \
1251 if (filp
->f_flags
& (O_NDELAY
|O_NONBLOCK
))
1252 mask
|= FSX_NONBLOCK
;
1253 fa
.fsx_xflags
= xfs_merge_ioc_xflags(flags
, xfs_ip2xflags(ip
));
1255 return -xfs_ioctl_setattr(ip
, &fa
, mask
);
1260 struct xfs_inode
*ip
,
1269 if (copy_from_user(&bm
, arg
, sizeof(bm
)))
1270 return -XFS_ERROR(EFAULT
);
1272 if (bm
.bmv_count
< 2)
1273 return -XFS_ERROR(EINVAL
);
1275 iflags
= (cmd
== XFS_IOC_GETBMAPA
? BMV_IF_ATTRFORK
: 0);
1276 if (ioflags
& IO_INVIS
)
1277 iflags
|= BMV_IF_NO_DMAPI_READ
;
1279 error
= xfs_getbmap(ip
, &bm
, (struct getbmap __user
*)arg
+1, iflags
);
1283 if (copy_to_user(arg
, &bm
, sizeof(bm
)))
1284 return -XFS_ERROR(EFAULT
);
1290 struct xfs_inode
*ip
,
1293 struct getbmapx bmx
;
1298 if (copy_from_user(&bmx
, arg
, sizeof(bmx
)))
1299 return -XFS_ERROR(EFAULT
);
1301 if (bmx
.bmv_count
< 2)
1302 return -XFS_ERROR(EINVAL
);
1305 * Map input getbmapx structure to a getbmap
1306 * structure for xfs_getbmap.
1308 GETBMAP_CONVERT(bmx
, bm
);
1310 iflags
= bmx
.bmv_iflags
;
1312 if (iflags
& (~BMV_IF_VALID
))
1313 return -XFS_ERROR(EINVAL
);
1315 iflags
|= BMV_IF_EXTENDED
;
1317 error
= xfs_getbmap(ip
, &bm
, (struct getbmapx __user
*)arg
+1, iflags
);
1321 GETBMAP_CONVERT(bm
, bmx
);
1323 if (copy_to_user(arg
, &bmx
, sizeof(bmx
)))
1324 return -XFS_ERROR(EFAULT
);
1337 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
1338 xfs_mount_t
*mp
= ip
->i_mount
;
1341 xfs_itrace_entry(XFS_I(inode
));
1344 case XFS_IOC_ALLOCSP
:
1345 case XFS_IOC_FREESP
:
1346 case XFS_IOC_RESVSP
:
1347 case XFS_IOC_UNRESVSP
:
1348 case XFS_IOC_ALLOCSP64
:
1349 case XFS_IOC_FREESP64
:
1350 case XFS_IOC_RESVSP64
:
1351 case XFS_IOC_UNRESVSP64
:
1353 * Only allow the sys admin to reserve space unless
1354 * unwritten extents are enabled.
1356 if (!xfs_sb_version_hasextflgbit(&mp
->m_sb
) &&
1357 !capable(CAP_SYS_ADMIN
))
1360 return xfs_ioc_space(ip
, inode
, filp
, ioflags
, cmd
, arg
);
1362 case XFS_IOC_DIOINFO
: {
1364 xfs_buftarg_t
*target
=
1365 XFS_IS_REALTIME_INODE(ip
) ?
1366 mp
->m_rtdev_targp
: mp
->m_ddev_targp
;
1368 da
.d_mem
= da
.d_miniosz
= 1 << target
->bt_sshift
;
1369 da
.d_maxiosz
= INT_MAX
& ~(da
.d_miniosz
- 1);
1371 if (copy_to_user(arg
, &da
, sizeof(da
)))
1372 return -XFS_ERROR(EFAULT
);
1376 case XFS_IOC_FSBULKSTAT_SINGLE
:
1377 case XFS_IOC_FSBULKSTAT
:
1378 case XFS_IOC_FSINUMBERS
:
1379 return xfs_ioc_bulkstat(mp
, cmd
, arg
);
1381 case XFS_IOC_FSGEOMETRY_V1
:
1382 return xfs_ioc_fsgeometry_v1(mp
, arg
);
1384 case XFS_IOC_FSGEOMETRY
:
1385 return xfs_ioc_fsgeometry(mp
, arg
);
1387 case XFS_IOC_GETVERSION
:
1388 return put_user(inode
->i_generation
, (int __user
*)arg
);
1390 case XFS_IOC_FSGETXATTR
:
1391 return xfs_ioc_fsgetxattr(ip
, 0, arg
);
1392 case XFS_IOC_FSGETXATTRA
:
1393 return xfs_ioc_fsgetxattr(ip
, 1, arg
);
1394 case XFS_IOC_FSSETXATTR
:
1395 return xfs_ioc_fssetxattr(ip
, filp
, arg
);
1396 case XFS_IOC_GETXFLAGS
:
1397 return xfs_ioc_getxflags(ip
, arg
);
1398 case XFS_IOC_SETXFLAGS
:
1399 return xfs_ioc_setxflags(ip
, filp
, arg
);
1401 case XFS_IOC_FSSETDM
: {
1402 struct fsdmidata dmi
;
1404 if (copy_from_user(&dmi
, arg
, sizeof(dmi
)))
1405 return -XFS_ERROR(EFAULT
);
1407 error
= xfs_set_dmattrs(ip
, dmi
.fsd_dmevmask
,
1412 case XFS_IOC_GETBMAP
:
1413 case XFS_IOC_GETBMAPA
:
1414 return xfs_ioc_getbmap(ip
, ioflags
, cmd
, arg
);
1416 case XFS_IOC_GETBMAPX
:
1417 return xfs_ioc_getbmapx(ip
, arg
);
1419 case XFS_IOC_FD_TO_HANDLE
:
1420 case XFS_IOC_PATH_TO_HANDLE
:
1421 case XFS_IOC_PATH_TO_FSHANDLE
:
1422 return xfs_find_handle(cmd
, arg
);
1424 case XFS_IOC_OPEN_BY_HANDLE
:
1425 return xfs_open_by_handle(mp
, arg
, filp
, inode
);
1427 case XFS_IOC_FSSETDM_BY_HANDLE
:
1428 return xfs_fssetdm_by_handle(mp
, arg
, inode
);
1430 case XFS_IOC_READLINK_BY_HANDLE
:
1431 return xfs_readlink_by_handle(mp
, arg
, inode
);
1433 case XFS_IOC_ATTRLIST_BY_HANDLE
:
1434 return xfs_attrlist_by_handle(mp
, arg
, inode
);
1436 case XFS_IOC_ATTRMULTI_BY_HANDLE
:
1437 return xfs_attrmulti_by_handle(mp
, arg
, filp
, inode
);
1439 case XFS_IOC_SWAPEXT
: {
1440 error
= xfs_swapext((struct xfs_swapext __user
*)arg
);
1444 case XFS_IOC_FSCOUNTS
: {
1445 xfs_fsop_counts_t out
;
1447 error
= xfs_fs_counts(mp
, &out
);
1451 if (copy_to_user(arg
, &out
, sizeof(out
)))
1452 return -XFS_ERROR(EFAULT
);
1456 case XFS_IOC_SET_RESBLKS
: {
1457 xfs_fsop_resblks_t inout
;
1460 if (!capable(CAP_SYS_ADMIN
))
1463 if (copy_from_user(&inout
, arg
, sizeof(inout
)))
1464 return -XFS_ERROR(EFAULT
);
1466 /* input parameter is passed in resblks field of structure */
1468 error
= xfs_reserve_blocks(mp
, &in
, &inout
);
1472 if (copy_to_user(arg
, &inout
, sizeof(inout
)))
1473 return -XFS_ERROR(EFAULT
);
1477 case XFS_IOC_GET_RESBLKS
: {
1478 xfs_fsop_resblks_t out
;
1480 if (!capable(CAP_SYS_ADMIN
))
1483 error
= xfs_reserve_blocks(mp
, NULL
, &out
);
1487 if (copy_to_user(arg
, &out
, sizeof(out
)))
1488 return -XFS_ERROR(EFAULT
);
1493 case XFS_IOC_FSGROWFSDATA
: {
1494 xfs_growfs_data_t in
;
1496 if (!capable(CAP_SYS_ADMIN
))
1499 if (copy_from_user(&in
, arg
, sizeof(in
)))
1500 return -XFS_ERROR(EFAULT
);
1502 error
= xfs_growfs_data(mp
, &in
);
1506 case XFS_IOC_FSGROWFSLOG
: {
1507 xfs_growfs_log_t in
;
1509 if (!capable(CAP_SYS_ADMIN
))
1512 if (copy_from_user(&in
, arg
, sizeof(in
)))
1513 return -XFS_ERROR(EFAULT
);
1515 error
= xfs_growfs_log(mp
, &in
);
1519 case XFS_IOC_FSGROWFSRT
: {
1522 if (!capable(CAP_SYS_ADMIN
))
1525 if (copy_from_user(&in
, arg
, sizeof(in
)))
1526 return -XFS_ERROR(EFAULT
);
1528 error
= xfs_growfs_rt(mp
, &in
);
1532 case XFS_IOC_FREEZE
:
1533 if (!capable(CAP_SYS_ADMIN
))
1536 if (inode
->i_sb
->s_frozen
== SB_UNFROZEN
)
1537 freeze_bdev(inode
->i_sb
->s_bdev
);
1541 if (!capable(CAP_SYS_ADMIN
))
1543 if (inode
->i_sb
->s_frozen
!= SB_UNFROZEN
)
1544 thaw_bdev(inode
->i_sb
->s_bdev
, inode
->i_sb
);
1547 case XFS_IOC_GOINGDOWN
: {
1550 if (!capable(CAP_SYS_ADMIN
))
1553 if (get_user(in
, (__uint32_t __user
*)arg
))
1554 return -XFS_ERROR(EFAULT
);
1556 error
= xfs_fs_goingdown(mp
, in
);
1560 case XFS_IOC_ERROR_INJECTION
: {
1561 xfs_error_injection_t in
;
1563 if (!capable(CAP_SYS_ADMIN
))
1566 if (copy_from_user(&in
, arg
, sizeof(in
)))
1567 return -XFS_ERROR(EFAULT
);
1569 error
= xfs_errortag_add(in
.errtag
, mp
);
1573 case XFS_IOC_ERROR_CLEARALL
:
1574 if (!capable(CAP_SYS_ADMIN
))
1577 error
= xfs_errortag_clearall(mp
, 1);