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"
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"
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_quota.h"
43 #include "xfs_inode_item.h"
44 #include "xfs_export.h"
45 #include "xfs_trace.h"
47 #include <linux/capability.h>
48 #include <linux/dcache.h>
49 #include <linux/mount.h>
50 #include <linux/namei.h>
51 #include <linux/pagemap.h>
52 #include <linux/slab.h>
53 #include <linux/exportfs.h>
56 * xfs_find_handle maps from userspace xfs_fsop_handlereq structure to
57 * a file or fs handle.
59 * XFS_IOC_PATH_TO_FSHANDLE
60 * returns fs handle for a mount point or path within that mount point
61 * XFS_IOC_FD_TO_HANDLE
62 * returns full handle for a FD opened in user space
63 * XFS_IOC_PATH_TO_HANDLE
64 * returns full handle for a path
69 xfs_fsop_handlereq_t
*hreq
)
74 struct file
*file
= NULL
;
79 if (cmd
== XFS_IOC_FD_TO_HANDLE
) {
80 file
= fget(hreq
->fd
);
83 inode
= file
->f_path
.dentry
->d_inode
;
85 error
= user_lpath((const char __user
*)hreq
->path
, &path
);
88 inode
= path
.dentry
->d_inode
;
93 * We can only generate handles for inodes residing on a XFS filesystem,
94 * and only for regular files, directories or symbolic links.
97 if (inode
->i_sb
->s_magic
!= XFS_SB_MAGIC
)
101 if (!S_ISREG(inode
->i_mode
) &&
102 !S_ISDIR(inode
->i_mode
) &&
103 !S_ISLNK(inode
->i_mode
))
107 memcpy(&handle
.ha_fsid
, ip
->i_mount
->m_fixedfsid
, sizeof(xfs_fsid_t
));
109 if (cmd
== XFS_IOC_PATH_TO_FSHANDLE
) {
111 * This handle only contains an fsid, zero the rest.
113 memset(&handle
.ha_fid
, 0, sizeof(handle
.ha_fid
));
114 hsize
= sizeof(xfs_fsid_t
);
118 lock_mode
= xfs_ilock_map_shared(ip
);
119 handle
.ha_fid
.fid_len
= sizeof(xfs_fid_t
) -
120 sizeof(handle
.ha_fid
.fid_len
);
121 handle
.ha_fid
.fid_pad
= 0;
122 handle
.ha_fid
.fid_gen
= ip
->i_d
.di_gen
;
123 handle
.ha_fid
.fid_ino
= ip
->i_ino
;
124 xfs_iunlock_map_shared(ip
, lock_mode
);
126 hsize
= XFS_HSIZE(handle
);
130 if (copy_to_user(hreq
->ohandle
, &handle
, hsize
) ||
131 copy_to_user(hreq
->ohandlen
, &hsize
, sizeof(__s32
)))
137 if (cmd
== XFS_IOC_FD_TO_HANDLE
)
145 * No need to do permission checks on the various pathname components
146 * as the handle operations are privileged.
149 xfs_handle_acceptable(
151 struct dentry
*dentry
)
157 * Convert userspace handle data into a dentry.
160 xfs_handle_to_dentry(
161 struct file
*parfilp
,
162 void __user
*uhandle
,
166 struct xfs_fid64 fid
;
169 * Only allow handle opens under a directory.
171 if (!S_ISDIR(parfilp
->f_path
.dentry
->d_inode
->i_mode
))
172 return ERR_PTR(-ENOTDIR
);
174 if (hlen
!= sizeof(xfs_handle_t
))
175 return ERR_PTR(-EINVAL
);
176 if (copy_from_user(&handle
, uhandle
, hlen
))
177 return ERR_PTR(-EFAULT
);
178 if (handle
.ha_fid
.fid_len
!=
179 sizeof(handle
.ha_fid
) - sizeof(handle
.ha_fid
.fid_len
))
180 return ERR_PTR(-EINVAL
);
182 memset(&fid
, 0, sizeof(struct fid
));
183 fid
.ino
= handle
.ha_fid
.fid_ino
;
184 fid
.gen
= handle
.ha_fid
.fid_gen
;
186 return exportfs_decode_fh(parfilp
->f_path
.mnt
, (struct fid
*)&fid
, 3,
187 FILEID_INO32_GEN
| XFS_FILEID_TYPE_64FLAG
,
188 xfs_handle_acceptable
, NULL
);
191 STATIC
struct dentry
*
192 xfs_handlereq_to_dentry(
193 struct file
*parfilp
,
194 xfs_fsop_handlereq_t
*hreq
)
196 return xfs_handle_to_dentry(parfilp
, hreq
->ihandle
, hreq
->ihandlen
);
201 struct file
*parfilp
,
202 xfs_fsop_handlereq_t
*hreq
)
204 const struct cred
*cred
= current_cred();
210 struct dentry
*dentry
;
212 if (!capable(CAP_SYS_ADMIN
))
213 return -XFS_ERROR(EPERM
);
215 dentry
= xfs_handlereq_to_dentry(parfilp
, hreq
);
217 return PTR_ERR(dentry
);
218 inode
= dentry
->d_inode
;
220 /* Restrict xfs_open_by_handle to directories & regular files. */
221 if (!(S_ISREG(inode
->i_mode
) || S_ISDIR(inode
->i_mode
))) {
222 error
= -XFS_ERROR(EPERM
);
226 #if BITS_PER_LONG != 32
227 hreq
->oflags
|= O_LARGEFILE
;
230 /* Put open permission in namei format. */
231 permflag
= hreq
->oflags
;
232 if ((permflag
+1) & O_ACCMODE
)
234 if (permflag
& O_TRUNC
)
237 if ((!(permflag
& O_APPEND
) || (permflag
& O_TRUNC
)) &&
238 (permflag
& FMODE_WRITE
) && IS_APPEND(inode
)) {
239 error
= -XFS_ERROR(EPERM
);
243 if ((permflag
& FMODE_WRITE
) && IS_IMMUTABLE(inode
)) {
244 error
= -XFS_ERROR(EACCES
);
248 /* Can't write directories. */
249 if (S_ISDIR(inode
->i_mode
) && (permflag
& FMODE_WRITE
)) {
250 error
= -XFS_ERROR(EISDIR
);
254 fd
= get_unused_fd();
260 filp
= dentry_open(dentry
, mntget(parfilp
->f_path
.mnt
),
264 return PTR_ERR(filp
);
267 if (inode
->i_mode
& S_IFREG
) {
268 filp
->f_flags
|= O_NOATIME
;
269 filp
->f_mode
|= FMODE_NOCMTIME
;
272 fd_install(fd
, filp
);
281 * This is a copy from fs/namei.c:vfs_readlink(), except for removing it's
282 * unused first argument.
297 if (len
> (unsigned) buflen
)
299 if (copy_to_user(buffer
, link
, len
))
307 xfs_readlink_by_handle(
308 struct file
*parfilp
,
309 xfs_fsop_handlereq_t
*hreq
)
311 struct dentry
*dentry
;
316 if (!capable(CAP_SYS_ADMIN
))
317 return -XFS_ERROR(EPERM
);
319 dentry
= xfs_handlereq_to_dentry(parfilp
, hreq
);
321 return PTR_ERR(dentry
);
323 /* Restrict this handle operation to symlinks only. */
324 if (!S_ISLNK(dentry
->d_inode
->i_mode
)) {
325 error
= -XFS_ERROR(EINVAL
);
329 if (copy_from_user(&olen
, hreq
->ohandlen
, sizeof(__u32
))) {
330 error
= -XFS_ERROR(EFAULT
);
334 link
= kmalloc(MAXPATHLEN
+1, GFP_KERNEL
);
336 error
= -XFS_ERROR(ENOMEM
);
340 error
= -xfs_readlink(XFS_I(dentry
->d_inode
), link
);
343 error
= do_readlink(hreq
->ohandle
, olen
, link
);
355 xfs_fssetdm_by_handle(
356 struct file
*parfilp
,
360 struct fsdmidata fsd
;
361 xfs_fsop_setdm_handlereq_t dmhreq
;
362 struct dentry
*dentry
;
364 if (!capable(CAP_MKNOD
))
365 return -XFS_ERROR(EPERM
);
366 if (copy_from_user(&dmhreq
, arg
, sizeof(xfs_fsop_setdm_handlereq_t
)))
367 return -XFS_ERROR(EFAULT
);
369 dentry
= xfs_handlereq_to_dentry(parfilp
, &dmhreq
.hreq
);
371 return PTR_ERR(dentry
);
373 if (IS_IMMUTABLE(dentry
->d_inode
) || IS_APPEND(dentry
->d_inode
)) {
374 error
= -XFS_ERROR(EPERM
);
378 if (copy_from_user(&fsd
, dmhreq
.data
, sizeof(fsd
))) {
379 error
= -XFS_ERROR(EFAULT
);
383 error
= -xfs_set_dmattrs(XFS_I(dentry
->d_inode
), fsd
.fsd_dmevmask
,
392 xfs_attrlist_by_handle(
393 struct file
*parfilp
,
397 attrlist_cursor_kern_t
*cursor
;
398 xfs_fsop_attrlist_handlereq_t al_hreq
;
399 struct dentry
*dentry
;
402 if (!capable(CAP_SYS_ADMIN
))
403 return -XFS_ERROR(EPERM
);
404 if (copy_from_user(&al_hreq
, arg
, sizeof(xfs_fsop_attrlist_handlereq_t
)))
405 return -XFS_ERROR(EFAULT
);
406 if (al_hreq
.buflen
> XATTR_LIST_MAX
)
407 return -XFS_ERROR(EINVAL
);
410 * Reject flags, only allow namespaces.
412 if (al_hreq
.flags
& ~(ATTR_ROOT
| ATTR_SECURE
))
413 return -XFS_ERROR(EINVAL
);
415 dentry
= xfs_handlereq_to_dentry(parfilp
, &al_hreq
.hreq
);
417 return PTR_ERR(dentry
);
419 kbuf
= kmalloc(al_hreq
.buflen
, GFP_KERNEL
);
423 cursor
= (attrlist_cursor_kern_t
*)&al_hreq
.pos
;
424 error
= -xfs_attr_list(XFS_I(dentry
->d_inode
), kbuf
, al_hreq
.buflen
,
425 al_hreq
.flags
, cursor
);
429 if (copy_to_user(al_hreq
.buffer
, kbuf
, al_hreq
.buflen
))
440 xfs_attrmulti_attr_get(
443 unsigned char __user
*ubuf
,
450 if (*len
> XATTR_SIZE_MAX
)
452 kbuf
= kmalloc(*len
, GFP_KERNEL
);
456 error
= xfs_attr_get(XFS_I(inode
), name
, kbuf
, (int *)len
, flags
);
460 if (copy_to_user(ubuf
, kbuf
, *len
))
469 xfs_attrmulti_attr_set(
472 const unsigned char __user
*ubuf
,
479 if (IS_IMMUTABLE(inode
) || IS_APPEND(inode
))
481 if (len
> XATTR_SIZE_MAX
)
484 kbuf
= memdup_user(ubuf
, len
);
486 return PTR_ERR(kbuf
);
488 error
= xfs_attr_set(XFS_I(inode
), name
, kbuf
, len
, flags
);
494 xfs_attrmulti_attr_remove(
499 if (IS_IMMUTABLE(inode
) || IS_APPEND(inode
))
501 return xfs_attr_remove(XFS_I(inode
), name
, flags
);
505 xfs_attrmulti_by_handle(
506 struct file
*parfilp
,
510 xfs_attr_multiop_t
*ops
;
511 xfs_fsop_attrmulti_handlereq_t am_hreq
;
512 struct dentry
*dentry
;
513 unsigned int i
, size
;
514 unsigned char *attr_name
;
516 if (!capable(CAP_SYS_ADMIN
))
517 return -XFS_ERROR(EPERM
);
518 if (copy_from_user(&am_hreq
, arg
, sizeof(xfs_fsop_attrmulti_handlereq_t
)))
519 return -XFS_ERROR(EFAULT
);
522 if (am_hreq
.opcount
>= INT_MAX
/ sizeof(xfs_attr_multiop_t
))
525 dentry
= xfs_handlereq_to_dentry(parfilp
, &am_hreq
.hreq
);
527 return PTR_ERR(dentry
);
530 size
= am_hreq
.opcount
* sizeof(xfs_attr_multiop_t
);
531 if (!size
|| size
> 16 * PAGE_SIZE
)
534 ops
= memdup_user(am_hreq
.ops
, size
);
536 error
= PTR_ERR(ops
);
540 attr_name
= kmalloc(MAXNAMELEN
, GFP_KERNEL
);
545 for (i
= 0; i
< am_hreq
.opcount
; i
++) {
546 ops
[i
].am_error
= strncpy_from_user((char *)attr_name
,
547 ops
[i
].am_attrname
, MAXNAMELEN
);
548 if (ops
[i
].am_error
== 0 || ops
[i
].am_error
== MAXNAMELEN
)
550 if (ops
[i
].am_error
< 0)
553 switch (ops
[i
].am_opcode
) {
555 ops
[i
].am_error
= xfs_attrmulti_attr_get(
556 dentry
->d_inode
, attr_name
,
557 ops
[i
].am_attrvalue
, &ops
[i
].am_length
,
561 ops
[i
].am_error
= mnt_want_write(parfilp
->f_path
.mnt
);
564 ops
[i
].am_error
= xfs_attrmulti_attr_set(
565 dentry
->d_inode
, attr_name
,
566 ops
[i
].am_attrvalue
, ops
[i
].am_length
,
568 mnt_drop_write(parfilp
->f_path
.mnt
);
571 ops
[i
].am_error
= mnt_want_write(parfilp
->f_path
.mnt
);
574 ops
[i
].am_error
= xfs_attrmulti_attr_remove(
575 dentry
->d_inode
, attr_name
,
577 mnt_drop_write(parfilp
->f_path
.mnt
);
580 ops
[i
].am_error
= EINVAL
;
584 if (copy_to_user(am_hreq
.ops
, ops
, size
))
585 error
= XFS_ERROR(EFAULT
);
597 struct xfs_inode
*ip
,
608 * Only allow the sys admin to reserve space unless
609 * unwritten extents are enabled.
611 if (!xfs_sb_version_hasextflgbit(&ip
->i_mount
->m_sb
) &&
612 !capable(CAP_SYS_ADMIN
))
613 return -XFS_ERROR(EPERM
);
615 if (inode
->i_flags
& (S_IMMUTABLE
|S_APPEND
))
616 return -XFS_ERROR(EPERM
);
618 if (!(filp
->f_mode
& FMODE_WRITE
))
619 return -XFS_ERROR(EBADF
);
621 if (!S_ISREG(inode
->i_mode
))
622 return -XFS_ERROR(EINVAL
);
624 if (filp
->f_flags
& (O_NDELAY
|O_NONBLOCK
))
625 attr_flags
|= XFS_ATTR_NONBLOCK
;
626 if (ioflags
& IO_INVIS
)
627 attr_flags
|= XFS_ATTR_DMI
;
629 error
= xfs_change_file_space(ip
, cmd
, bf
, filp
->f_pos
, attr_flags
);
639 xfs_fsop_bulkreq_t bulkreq
;
640 int count
; /* # of records returned */
641 xfs_ino_t inlast
; /* last inode number */
645 /* done = 1 if there are more stats to get and if bulkstat */
646 /* should be called again (unused here, but used in dmapi) */
648 if (!capable(CAP_SYS_ADMIN
))
651 if (XFS_FORCED_SHUTDOWN(mp
))
652 return -XFS_ERROR(EIO
);
654 if (copy_from_user(&bulkreq
, arg
, sizeof(xfs_fsop_bulkreq_t
)))
655 return -XFS_ERROR(EFAULT
);
657 if (copy_from_user(&inlast
, bulkreq
.lastip
, sizeof(__s64
)))
658 return -XFS_ERROR(EFAULT
);
660 if ((count
= bulkreq
.icount
) <= 0)
661 return -XFS_ERROR(EINVAL
);
663 if (bulkreq
.ubuffer
== NULL
)
664 return -XFS_ERROR(EINVAL
);
666 if (cmd
== XFS_IOC_FSINUMBERS
)
667 error
= xfs_inumbers(mp
, &inlast
, &count
,
668 bulkreq
.ubuffer
, xfs_inumbers_fmt
);
669 else if (cmd
== XFS_IOC_FSBULKSTAT_SINGLE
)
670 error
= xfs_bulkstat_single(mp
, &inlast
,
671 bulkreq
.ubuffer
, &done
);
672 else /* XFS_IOC_FSBULKSTAT */
673 error
= xfs_bulkstat(mp
, &inlast
, &count
, xfs_bulkstat_one
,
674 sizeof(xfs_bstat_t
), bulkreq
.ubuffer
,
680 if (bulkreq
.ocount
!= NULL
) {
681 if (copy_to_user(bulkreq
.lastip
, &inlast
,
683 return -XFS_ERROR(EFAULT
);
685 if (copy_to_user(bulkreq
.ocount
, &count
, sizeof(count
)))
686 return -XFS_ERROR(EFAULT
);
693 xfs_ioc_fsgeometry_v1(
697 xfs_fsop_geom_v1_t fsgeo
;
700 error
= xfs_fs_geometry(mp
, (xfs_fsop_geom_t
*)&fsgeo
, 3);
704 if (copy_to_user(arg
, &fsgeo
, sizeof(fsgeo
)))
705 return -XFS_ERROR(EFAULT
);
714 xfs_fsop_geom_t fsgeo
;
717 error
= xfs_fs_geometry(mp
, &fsgeo
, 4);
721 if (copy_to_user(arg
, &fsgeo
, sizeof(fsgeo
)))
722 return -XFS_ERROR(EFAULT
);
727 * Linux extended inode flags interface.
731 xfs_merge_ioc_xflags(
735 unsigned int xflags
= start
;
737 if (flags
& FS_IMMUTABLE_FL
)
738 xflags
|= XFS_XFLAG_IMMUTABLE
;
740 xflags
&= ~XFS_XFLAG_IMMUTABLE
;
741 if (flags
& FS_APPEND_FL
)
742 xflags
|= XFS_XFLAG_APPEND
;
744 xflags
&= ~XFS_XFLAG_APPEND
;
745 if (flags
& FS_SYNC_FL
)
746 xflags
|= XFS_XFLAG_SYNC
;
748 xflags
&= ~XFS_XFLAG_SYNC
;
749 if (flags
& FS_NOATIME_FL
)
750 xflags
|= XFS_XFLAG_NOATIME
;
752 xflags
&= ~XFS_XFLAG_NOATIME
;
753 if (flags
& FS_NODUMP_FL
)
754 xflags
|= XFS_XFLAG_NODUMP
;
756 xflags
&= ~XFS_XFLAG_NODUMP
;
765 unsigned int flags
= 0;
767 if (di_flags
& XFS_DIFLAG_IMMUTABLE
)
768 flags
|= FS_IMMUTABLE_FL
;
769 if (di_flags
& XFS_DIFLAG_APPEND
)
770 flags
|= FS_APPEND_FL
;
771 if (di_flags
& XFS_DIFLAG_SYNC
)
773 if (di_flags
& XFS_DIFLAG_NOATIME
)
774 flags
|= FS_NOATIME_FL
;
775 if (di_flags
& XFS_DIFLAG_NODUMP
)
776 flags
|= FS_NODUMP_FL
;
788 xfs_ilock(ip
, XFS_ILOCK_SHARED
);
789 fa
.fsx_xflags
= xfs_ip2xflags(ip
);
790 fa
.fsx_extsize
= ip
->i_d
.di_extsize
<< ip
->i_mount
->m_sb
.sb_blocklog
;
791 fa
.fsx_projid
= ip
->i_d
.di_projid
;
795 if (ip
->i_afp
->if_flags
& XFS_IFEXTENTS
)
796 fa
.fsx_nextents
= ip
->i_afp
->if_bytes
/
797 sizeof(xfs_bmbt_rec_t
);
799 fa
.fsx_nextents
= ip
->i_d
.di_anextents
;
803 if (ip
->i_df
.if_flags
& XFS_IFEXTENTS
)
804 fa
.fsx_nextents
= ip
->i_df
.if_bytes
/
805 sizeof(xfs_bmbt_rec_t
);
807 fa
.fsx_nextents
= ip
->i_d
.di_nextents
;
809 xfs_iunlock(ip
, XFS_ILOCK_SHARED
);
811 if (copy_to_user(arg
, &fa
, sizeof(fa
)))
818 struct xfs_inode
*ip
,
821 unsigned int di_flags
;
823 /* can't set PREALLOC this way, just preserve it */
824 di_flags
= (ip
->i_d
.di_flags
& XFS_DIFLAG_PREALLOC
);
825 if (xflags
& XFS_XFLAG_IMMUTABLE
)
826 di_flags
|= XFS_DIFLAG_IMMUTABLE
;
827 if (xflags
& XFS_XFLAG_APPEND
)
828 di_flags
|= XFS_DIFLAG_APPEND
;
829 if (xflags
& XFS_XFLAG_SYNC
)
830 di_flags
|= XFS_DIFLAG_SYNC
;
831 if (xflags
& XFS_XFLAG_NOATIME
)
832 di_flags
|= XFS_DIFLAG_NOATIME
;
833 if (xflags
& XFS_XFLAG_NODUMP
)
834 di_flags
|= XFS_DIFLAG_NODUMP
;
835 if (xflags
& XFS_XFLAG_PROJINHERIT
)
836 di_flags
|= XFS_DIFLAG_PROJINHERIT
;
837 if (xflags
& XFS_XFLAG_NODEFRAG
)
838 di_flags
|= XFS_DIFLAG_NODEFRAG
;
839 if (xflags
& XFS_XFLAG_FILESTREAM
)
840 di_flags
|= XFS_DIFLAG_FILESTREAM
;
841 if ((ip
->i_d
.di_mode
& S_IFMT
) == S_IFDIR
) {
842 if (xflags
& XFS_XFLAG_RTINHERIT
)
843 di_flags
|= XFS_DIFLAG_RTINHERIT
;
844 if (xflags
& XFS_XFLAG_NOSYMLINKS
)
845 di_flags
|= XFS_DIFLAG_NOSYMLINKS
;
846 if (xflags
& XFS_XFLAG_EXTSZINHERIT
)
847 di_flags
|= XFS_DIFLAG_EXTSZINHERIT
;
848 } else if ((ip
->i_d
.di_mode
& S_IFMT
) == S_IFREG
) {
849 if (xflags
& XFS_XFLAG_REALTIME
)
850 di_flags
|= XFS_DIFLAG_REALTIME
;
851 if (xflags
& XFS_XFLAG_EXTSIZE
)
852 di_flags
|= XFS_DIFLAG_EXTSIZE
;
855 ip
->i_d
.di_flags
= di_flags
;
859 xfs_diflags_to_linux(
860 struct xfs_inode
*ip
)
862 struct inode
*inode
= VFS_I(ip
);
863 unsigned int xflags
= xfs_ip2xflags(ip
);
865 if (xflags
& XFS_XFLAG_IMMUTABLE
)
866 inode
->i_flags
|= S_IMMUTABLE
;
868 inode
->i_flags
&= ~S_IMMUTABLE
;
869 if (xflags
& XFS_XFLAG_APPEND
)
870 inode
->i_flags
|= S_APPEND
;
872 inode
->i_flags
&= ~S_APPEND
;
873 if (xflags
& XFS_XFLAG_SYNC
)
874 inode
->i_flags
|= S_SYNC
;
876 inode
->i_flags
&= ~S_SYNC
;
877 if (xflags
& XFS_XFLAG_NOATIME
)
878 inode
->i_flags
|= S_NOATIME
;
880 inode
->i_flags
&= ~S_NOATIME
;
884 #define FSX_EXTSIZE 2
886 #define FSX_NONBLOCK 8
894 struct xfs_mount
*mp
= ip
->i_mount
;
895 struct xfs_trans
*tp
;
896 unsigned int lock_flags
= 0;
897 struct xfs_dquot
*udqp
= NULL
;
898 struct xfs_dquot
*gdqp
= NULL
;
899 struct xfs_dquot
*olddquot
= NULL
;
902 xfs_itrace_entry(ip
);
904 if (mp
->m_flags
& XFS_MOUNT_RDONLY
)
905 return XFS_ERROR(EROFS
);
906 if (XFS_FORCED_SHUTDOWN(mp
))
907 return XFS_ERROR(EIO
);
910 * If disk quotas is on, we make sure that the dquots do exist on disk,
911 * before we start any other transactions. Trying to do this later
912 * is messy. We don't care to take a readlock to look at the ids
913 * in inode here, because we can't hold it across the trans_reserve.
914 * If the IDs do change before we take the ilock, we're covered
915 * because the i_*dquot fields will get updated anyway.
917 if (XFS_IS_QUOTA_ON(mp
) && (mask
& FSX_PROJID
)) {
918 code
= xfs_qm_vop_dqalloc(ip
, ip
->i_d
.di_uid
,
919 ip
->i_d
.di_gid
, fa
->fsx_projid
,
920 XFS_QMOPT_PQUOTA
, &udqp
, &gdqp
);
926 * For the other attributes, we acquire the inode lock and
927 * first do an error checking pass.
929 tp
= xfs_trans_alloc(mp
, XFS_TRANS_SETATTR_NOT_SIZE
);
930 code
= xfs_trans_reserve(tp
, 0, XFS_ICHANGE_LOG_RES(mp
), 0, 0, 0);
934 lock_flags
= XFS_ILOCK_EXCL
;
935 xfs_ilock(ip
, lock_flags
);
938 * CAP_FOWNER overrides the following restrictions:
940 * The user ID of the calling process must be equal
941 * to the file owner ID, except in cases where the
942 * CAP_FSETID capability is applicable.
944 if (current_fsuid() != ip
->i_d
.di_uid
&& !capable(CAP_FOWNER
)) {
945 code
= XFS_ERROR(EPERM
);
950 * Do a quota reservation only if projid is actually going to change.
952 if (mask
& FSX_PROJID
) {
953 if (XFS_IS_QUOTA_RUNNING(mp
) &&
954 XFS_IS_PQUOTA_ON(mp
) &&
955 ip
->i_d
.di_projid
!= fa
->fsx_projid
) {
957 code
= xfs_qm_vop_chown_reserve(tp
, ip
, udqp
, gdqp
,
958 capable(CAP_FOWNER
) ?
959 XFS_QMOPT_FORCE_RES
: 0);
960 if (code
) /* out of quota */
965 if (mask
& FSX_EXTSIZE
) {
967 * Can't change extent size if any extents are allocated.
969 if (ip
->i_d
.di_nextents
&&
970 ((ip
->i_d
.di_extsize
<< mp
->m_sb
.sb_blocklog
) !=
972 code
= XFS_ERROR(EINVAL
); /* EFBIG? */
977 * Extent size must be a multiple of the appropriate block
978 * size, if set at all.
980 if (fa
->fsx_extsize
!= 0) {
983 if (XFS_IS_REALTIME_INODE(ip
) ||
984 ((mask
& FSX_XFLAGS
) &&
985 (fa
->fsx_xflags
& XFS_XFLAG_REALTIME
))) {
986 size
= mp
->m_sb
.sb_rextsize
<<
987 mp
->m_sb
.sb_blocklog
;
989 size
= mp
->m_sb
.sb_blocksize
;
992 if (fa
->fsx_extsize
% size
) {
993 code
= XFS_ERROR(EINVAL
);
1000 if (mask
& FSX_XFLAGS
) {
1002 * Can't change realtime flag if any extents are allocated.
1004 if ((ip
->i_d
.di_nextents
|| ip
->i_delayed_blks
) &&
1005 (XFS_IS_REALTIME_INODE(ip
)) !=
1006 (fa
->fsx_xflags
& XFS_XFLAG_REALTIME
)) {
1007 code
= XFS_ERROR(EINVAL
); /* EFBIG? */
1012 * If realtime flag is set then must have realtime data.
1014 if ((fa
->fsx_xflags
& XFS_XFLAG_REALTIME
)) {
1015 if ((mp
->m_sb
.sb_rblocks
== 0) ||
1016 (mp
->m_sb
.sb_rextsize
== 0) ||
1017 (ip
->i_d
.di_extsize
% mp
->m_sb
.sb_rextsize
)) {
1018 code
= XFS_ERROR(EINVAL
);
1024 * Can't modify an immutable/append-only file unless
1025 * we have appropriate permission.
1027 if ((ip
->i_d
.di_flags
&
1028 (XFS_DIFLAG_IMMUTABLE
|XFS_DIFLAG_APPEND
) ||
1030 (XFS_XFLAG_IMMUTABLE
| XFS_XFLAG_APPEND
))) &&
1031 !capable(CAP_LINUX_IMMUTABLE
)) {
1032 code
= XFS_ERROR(EPERM
);
1037 xfs_trans_ijoin(tp
, ip
);
1040 * Change file ownership. Must be the owner or privileged.
1042 if (mask
& FSX_PROJID
) {
1044 * CAP_FSETID overrides the following restrictions:
1046 * The set-user-ID and set-group-ID bits of a file will be
1047 * cleared upon successful return from chown()
1049 if ((ip
->i_d
.di_mode
& (S_ISUID
|S_ISGID
)) &&
1050 !capable(CAP_FSETID
))
1051 ip
->i_d
.di_mode
&= ~(S_ISUID
|S_ISGID
);
1054 * Change the ownerships and register quota modifications
1055 * in the transaction.
1057 if (ip
->i_d
.di_projid
!= fa
->fsx_projid
) {
1058 if (XFS_IS_QUOTA_RUNNING(mp
) && XFS_IS_PQUOTA_ON(mp
)) {
1059 olddquot
= xfs_qm_vop_chown(tp
, ip
,
1060 &ip
->i_gdquot
, gdqp
);
1062 ip
->i_d
.di_projid
= fa
->fsx_projid
;
1065 * We may have to rev the inode as well as
1066 * the superblock version number since projids didn't
1067 * exist before DINODE_VERSION_2 and SB_VERSION_NLINK.
1069 if (ip
->i_d
.di_version
== 1)
1070 xfs_bump_ino_vers2(tp
, ip
);
1075 if (mask
& FSX_EXTSIZE
)
1076 ip
->i_d
.di_extsize
= fa
->fsx_extsize
>> mp
->m_sb
.sb_blocklog
;
1077 if (mask
& FSX_XFLAGS
) {
1078 xfs_set_diflags(ip
, fa
->fsx_xflags
);
1079 xfs_diflags_to_linux(ip
);
1082 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
1083 xfs_ichgtime(ip
, XFS_ICHGTIME_CHG
);
1085 XFS_STATS_INC(xs_ig_attrchg
);
1088 * If this is a synchronous mount, make sure that the
1089 * transaction goes to disk before returning to the user.
1090 * This is slightly sub-optimal in that truncates require
1091 * two sync transactions instead of one for wsync filesystems.
1092 * One for the truncate and one for the timestamps since we
1093 * don't want to change the timestamps unless we're sure the
1094 * truncate worked. Truncates are less than 1% of the laddis
1095 * mix so this probably isn't worth the trouble to optimize.
1097 if (mp
->m_flags
& XFS_MOUNT_WSYNC
)
1098 xfs_trans_set_sync(tp
);
1099 code
= xfs_trans_commit(tp
, 0);
1100 xfs_iunlock(ip
, lock_flags
);
1103 * Release any dquot(s) the inode had kept before chown.
1105 xfs_qm_dqrele(olddquot
);
1106 xfs_qm_dqrele(udqp
);
1107 xfs_qm_dqrele(gdqp
);
1112 xfs_qm_dqrele(udqp
);
1113 xfs_qm_dqrele(gdqp
);
1114 xfs_trans_cancel(tp
, 0);
1116 xfs_iunlock(ip
, lock_flags
);
1129 if (copy_from_user(&fa
, arg
, sizeof(fa
)))
1132 mask
= FSX_XFLAGS
| FSX_EXTSIZE
| FSX_PROJID
;
1133 if (filp
->f_flags
& (O_NDELAY
|O_NONBLOCK
))
1134 mask
|= FSX_NONBLOCK
;
1136 return -xfs_ioctl_setattr(ip
, &fa
, mask
);
1146 flags
= xfs_di2lxflags(ip
->i_d
.di_flags
);
1147 if (copy_to_user(arg
, &flags
, sizeof(flags
)))
1162 if (copy_from_user(&flags
, arg
, sizeof(flags
)))
1165 if (flags
& ~(FS_IMMUTABLE_FL
| FS_APPEND_FL
| \
1166 FS_NOATIME_FL
| FS_NODUMP_FL
| \
1171 if (filp
->f_flags
& (O_NDELAY
|O_NONBLOCK
))
1172 mask
|= FSX_NONBLOCK
;
1173 fa
.fsx_xflags
= xfs_merge_ioc_xflags(flags
, xfs_ip2xflags(ip
));
1175 return -xfs_ioctl_setattr(ip
, &fa
, mask
);
1179 xfs_getbmap_format(void **ap
, struct getbmapx
*bmv
, int *full
)
1181 struct getbmap __user
*base
= *ap
;
1183 /* copy only getbmap portion (not getbmapx) */
1184 if (copy_to_user(base
, bmv
, sizeof(struct getbmap
)))
1185 return XFS_ERROR(EFAULT
);
1187 *ap
+= sizeof(struct getbmap
);
1193 struct xfs_inode
*ip
,
1198 struct getbmapx bmx
;
1201 if (copy_from_user(&bmx
, arg
, sizeof(struct getbmapx
)))
1202 return -XFS_ERROR(EFAULT
);
1204 if (bmx
.bmv_count
< 2)
1205 return -XFS_ERROR(EINVAL
);
1207 bmx
.bmv_iflags
= (cmd
== XFS_IOC_GETBMAPA
? BMV_IF_ATTRFORK
: 0);
1208 if (ioflags
& IO_INVIS
)
1209 bmx
.bmv_iflags
|= BMV_IF_NO_DMAPI_READ
;
1211 error
= xfs_getbmap(ip
, &bmx
, xfs_getbmap_format
,
1212 (struct getbmap
*)arg
+1);
1216 /* copy back header - only size of getbmap */
1217 if (copy_to_user(arg
, &bmx
, sizeof(struct getbmap
)))
1218 return -XFS_ERROR(EFAULT
);
1223 xfs_getbmapx_format(void **ap
, struct getbmapx
*bmv
, int *full
)
1225 struct getbmapx __user
*base
= *ap
;
1227 if (copy_to_user(base
, bmv
, sizeof(struct getbmapx
)))
1228 return XFS_ERROR(EFAULT
);
1230 *ap
+= sizeof(struct getbmapx
);
1236 struct xfs_inode
*ip
,
1239 struct getbmapx bmx
;
1242 if (copy_from_user(&bmx
, arg
, sizeof(bmx
)))
1243 return -XFS_ERROR(EFAULT
);
1245 if (bmx
.bmv_count
< 2)
1246 return -XFS_ERROR(EINVAL
);
1248 if (bmx
.bmv_iflags
& (~BMV_IF_VALID
))
1249 return -XFS_ERROR(EINVAL
);
1251 error
= xfs_getbmap(ip
, &bmx
, xfs_getbmapx_format
,
1252 (struct getbmapx
*)arg
+1);
1256 /* copy back header */
1257 if (copy_to_user(arg
, &bmx
, sizeof(struct getbmapx
)))
1258 return -XFS_ERROR(EFAULT
);
1264 * Note: some of the ioctl's return positive numbers as a
1265 * byte count indicating success, such as readlink_by_handle.
1266 * So we don't "sign flip" like most other routines. This means
1267 * true errors need to be returned as a negative value.
1275 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
1276 struct xfs_inode
*ip
= XFS_I(inode
);
1277 struct xfs_mount
*mp
= ip
->i_mount
;
1278 void __user
*arg
= (void __user
*)p
;
1282 if (filp
->f_mode
& FMODE_NOCMTIME
)
1283 ioflags
|= IO_INVIS
;
1285 xfs_itrace_entry(ip
);
1288 case XFS_IOC_ALLOCSP
:
1289 case XFS_IOC_FREESP
:
1290 case XFS_IOC_RESVSP
:
1291 case XFS_IOC_UNRESVSP
:
1292 case XFS_IOC_ALLOCSP64
:
1293 case XFS_IOC_FREESP64
:
1294 case XFS_IOC_RESVSP64
:
1295 case XFS_IOC_UNRESVSP64
: {
1298 if (copy_from_user(&bf
, arg
, sizeof(bf
)))
1299 return -XFS_ERROR(EFAULT
);
1300 return xfs_ioc_space(ip
, inode
, filp
, ioflags
, cmd
, &bf
);
1302 case XFS_IOC_DIOINFO
: {
1304 xfs_buftarg_t
*target
=
1305 XFS_IS_REALTIME_INODE(ip
) ?
1306 mp
->m_rtdev_targp
: mp
->m_ddev_targp
;
1308 da
.d_mem
= da
.d_miniosz
= 1 << target
->bt_sshift
;
1309 da
.d_maxiosz
= INT_MAX
& ~(da
.d_miniosz
- 1);
1311 if (copy_to_user(arg
, &da
, sizeof(da
)))
1312 return -XFS_ERROR(EFAULT
);
1316 case XFS_IOC_FSBULKSTAT_SINGLE
:
1317 case XFS_IOC_FSBULKSTAT
:
1318 case XFS_IOC_FSINUMBERS
:
1319 return xfs_ioc_bulkstat(mp
, cmd
, arg
);
1321 case XFS_IOC_FSGEOMETRY_V1
:
1322 return xfs_ioc_fsgeometry_v1(mp
, arg
);
1324 case XFS_IOC_FSGEOMETRY
:
1325 return xfs_ioc_fsgeometry(mp
, arg
);
1327 case XFS_IOC_GETVERSION
:
1328 return put_user(inode
->i_generation
, (int __user
*)arg
);
1330 case XFS_IOC_FSGETXATTR
:
1331 return xfs_ioc_fsgetxattr(ip
, 0, arg
);
1332 case XFS_IOC_FSGETXATTRA
:
1333 return xfs_ioc_fsgetxattr(ip
, 1, arg
);
1334 case XFS_IOC_FSSETXATTR
:
1335 return xfs_ioc_fssetxattr(ip
, filp
, arg
);
1336 case XFS_IOC_GETXFLAGS
:
1337 return xfs_ioc_getxflags(ip
, arg
);
1338 case XFS_IOC_SETXFLAGS
:
1339 return xfs_ioc_setxflags(ip
, filp
, arg
);
1341 case XFS_IOC_FSSETDM
: {
1342 struct fsdmidata dmi
;
1344 if (copy_from_user(&dmi
, arg
, sizeof(dmi
)))
1345 return -XFS_ERROR(EFAULT
);
1347 error
= xfs_set_dmattrs(ip
, dmi
.fsd_dmevmask
,
1352 case XFS_IOC_GETBMAP
:
1353 case XFS_IOC_GETBMAPA
:
1354 return xfs_ioc_getbmap(ip
, ioflags
, cmd
, arg
);
1356 case XFS_IOC_GETBMAPX
:
1357 return xfs_ioc_getbmapx(ip
, arg
);
1359 case XFS_IOC_FD_TO_HANDLE
:
1360 case XFS_IOC_PATH_TO_HANDLE
:
1361 case XFS_IOC_PATH_TO_FSHANDLE
: {
1362 xfs_fsop_handlereq_t hreq
;
1364 if (copy_from_user(&hreq
, arg
, sizeof(hreq
)))
1365 return -XFS_ERROR(EFAULT
);
1366 return xfs_find_handle(cmd
, &hreq
);
1368 case XFS_IOC_OPEN_BY_HANDLE
: {
1369 xfs_fsop_handlereq_t hreq
;
1371 if (copy_from_user(&hreq
, arg
, sizeof(xfs_fsop_handlereq_t
)))
1372 return -XFS_ERROR(EFAULT
);
1373 return xfs_open_by_handle(filp
, &hreq
);
1375 case XFS_IOC_FSSETDM_BY_HANDLE
:
1376 return xfs_fssetdm_by_handle(filp
, arg
);
1378 case XFS_IOC_READLINK_BY_HANDLE
: {
1379 xfs_fsop_handlereq_t hreq
;
1381 if (copy_from_user(&hreq
, arg
, sizeof(xfs_fsop_handlereq_t
)))
1382 return -XFS_ERROR(EFAULT
);
1383 return xfs_readlink_by_handle(filp
, &hreq
);
1385 case XFS_IOC_ATTRLIST_BY_HANDLE
:
1386 return xfs_attrlist_by_handle(filp
, arg
);
1388 case XFS_IOC_ATTRMULTI_BY_HANDLE
:
1389 return xfs_attrmulti_by_handle(filp
, arg
);
1391 case XFS_IOC_SWAPEXT
: {
1392 struct xfs_swapext sxp
;
1394 if (copy_from_user(&sxp
, arg
, sizeof(xfs_swapext_t
)))
1395 return -XFS_ERROR(EFAULT
);
1396 error
= xfs_swapext(&sxp
);
1400 case XFS_IOC_FSCOUNTS
: {
1401 xfs_fsop_counts_t out
;
1403 error
= xfs_fs_counts(mp
, &out
);
1407 if (copy_to_user(arg
, &out
, sizeof(out
)))
1408 return -XFS_ERROR(EFAULT
);
1412 case XFS_IOC_SET_RESBLKS
: {
1413 xfs_fsop_resblks_t inout
;
1416 if (!capable(CAP_SYS_ADMIN
))
1419 if (mp
->m_flags
& XFS_MOUNT_RDONLY
)
1420 return -XFS_ERROR(EROFS
);
1422 if (copy_from_user(&inout
, arg
, sizeof(inout
)))
1423 return -XFS_ERROR(EFAULT
);
1425 /* input parameter is passed in resblks field of structure */
1427 error
= xfs_reserve_blocks(mp
, &in
, &inout
);
1431 if (copy_to_user(arg
, &inout
, sizeof(inout
)))
1432 return -XFS_ERROR(EFAULT
);
1436 case XFS_IOC_GET_RESBLKS
: {
1437 xfs_fsop_resblks_t out
;
1439 if (!capable(CAP_SYS_ADMIN
))
1442 error
= xfs_reserve_blocks(mp
, NULL
, &out
);
1446 if (copy_to_user(arg
, &out
, sizeof(out
)))
1447 return -XFS_ERROR(EFAULT
);
1452 case XFS_IOC_FSGROWFSDATA
: {
1453 xfs_growfs_data_t in
;
1455 if (copy_from_user(&in
, arg
, sizeof(in
)))
1456 return -XFS_ERROR(EFAULT
);
1458 error
= xfs_growfs_data(mp
, &in
);
1462 case XFS_IOC_FSGROWFSLOG
: {
1463 xfs_growfs_log_t in
;
1465 if (copy_from_user(&in
, arg
, sizeof(in
)))
1466 return -XFS_ERROR(EFAULT
);
1468 error
= xfs_growfs_log(mp
, &in
);
1472 case XFS_IOC_FSGROWFSRT
: {
1475 if (copy_from_user(&in
, arg
, sizeof(in
)))
1476 return -XFS_ERROR(EFAULT
);
1478 error
= xfs_growfs_rt(mp
, &in
);
1482 case XFS_IOC_GOINGDOWN
: {
1485 if (!capable(CAP_SYS_ADMIN
))
1488 if (get_user(in
, (__uint32_t __user
*)arg
))
1489 return -XFS_ERROR(EFAULT
);
1491 error
= xfs_fs_goingdown(mp
, in
);
1495 case XFS_IOC_ERROR_INJECTION
: {
1496 xfs_error_injection_t in
;
1498 if (!capable(CAP_SYS_ADMIN
))
1501 if (copy_from_user(&in
, arg
, sizeof(in
)))
1502 return -XFS_ERROR(EFAULT
);
1504 error
= xfs_errortag_add(in
.errtag
, mp
);
1508 case XFS_IOC_ERROR_CLEARALL
:
1509 if (!capable(CAP_SYS_ADMIN
))
1512 error
= xfs_errortag_clearall(mp
, 1);