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_quota.h"
30 #include "xfs_mount.h"
31 #include "xfs_bmap_btree.h"
32 #include "xfs_alloc_btree.h"
33 #include "xfs_ialloc_btree.h"
34 #include "xfs_dir2_sf.h"
35 #include "xfs_attr_sf.h"
36 #include "xfs_dinode.h"
37 #include "xfs_inode.h"
39 #include "xfs_btree.h"
40 #include "xfs_ialloc.h"
41 #include "xfs_rtalloc.h"
42 #include "xfs_error.h"
43 #include "xfs_itable.h"
47 #include "xfs_buf_item.h"
48 #include "xfs_utils.h"
49 #include "xfs_vnodeops.h"
51 #include <linux/capability.h>
52 #include <linux/xattr.h>
53 #include <linux/namei.h>
54 #include <linux/security.h>
55 #include <linux/falloc.h>
58 * Bring the atime in the XFS inode uptodate.
59 * Used before logging the inode to disk or when the Linux inode goes away.
62 xfs_synchronize_atime(
67 vp
= XFS_ITOV_NULL(ip
);
69 ip
->i_d
.di_atime
.t_sec
= (__int32_t
)vp
->i_atime
.tv_sec
;
70 ip
->i_d
.di_atime
.t_nsec
= (__int32_t
)vp
->i_atime
.tv_nsec
;
75 * If the linux inode exists, mark it dirty.
76 * Used when commiting a dirty inode into a transaction so that
77 * the inode will get written back by the linux code
80 xfs_mark_inode_dirty_sync(
85 vp
= XFS_ITOV_NULL(ip
);
87 mark_inode_dirty_sync(vn_to_inode(vp
));
91 * Change the requested timestamp in the given inode.
92 * We don't lock across timestamp updates, and we don't log them but
93 * we do record the fact that there is dirty information in core.
95 * NOTE -- callers MUST combine XFS_ICHGTIME_MOD or XFS_ICHGTIME_CHG
96 * with XFS_ICHGTIME_ACC to be sure that access time
97 * update will take. Calling first with XFS_ICHGTIME_ACC
98 * and then XFS_ICHGTIME_MOD may fail to modify the access
99 * timestamp if the filesystem is mounted noacctm.
106 struct inode
*inode
= vn_to_inode(XFS_ITOV(ip
));
110 if (flags
& XFS_ICHGTIME_MOD
) {
112 ip
->i_d
.di_mtime
.t_sec
= (__int32_t
)tv
.tv_sec
;
113 ip
->i_d
.di_mtime
.t_nsec
= (__int32_t
)tv
.tv_nsec
;
115 if (flags
& XFS_ICHGTIME_ACC
) {
117 ip
->i_d
.di_atime
.t_sec
= (__int32_t
)tv
.tv_sec
;
118 ip
->i_d
.di_atime
.t_nsec
= (__int32_t
)tv
.tv_nsec
;
120 if (flags
& XFS_ICHGTIME_CHG
) {
122 ip
->i_d
.di_ctime
.t_sec
= (__int32_t
)tv
.tv_sec
;
123 ip
->i_d
.di_ctime
.t_nsec
= (__int32_t
)tv
.tv_nsec
;
127 * We update the i_update_core field _after_ changing
128 * the timestamps in order to coordinate properly with
129 * xfs_iflush() so that we don't lose timestamp updates.
130 * This keeps us from having to hold the inode lock
131 * while doing this. We use the SYNCHRONIZE macro to
132 * ensure that the compiler does not reorder the update
133 * of i_update_core above the timestamp updates above.
136 ip
->i_update_core
= 1;
137 if (!(inode
->i_state
& I_NEW
))
138 mark_inode_dirty_sync(inode
);
142 * Variant on the above which avoids querying the system clock
143 * in situations where we know the Linux inode timestamps have
144 * just been updated (and so we can update our inode cheaply).
155 * Atime updates for read() & friends are handled lazily now, and
156 * explicit updates must go through xfs_ichgtime()
158 ASSERT((flags
& XFS_ICHGTIME_ACC
) == 0);
161 * We're not supposed to change timestamps in readonly-mounted
162 * filesystems. Throw it away if anyone asks us.
164 if (unlikely(IS_RDONLY(inode
)))
167 if (flags
& XFS_ICHGTIME_MOD
) {
168 tvp
= &inode
->i_mtime
;
169 ip
->i_d
.di_mtime
.t_sec
= (__int32_t
)tvp
->tv_sec
;
170 ip
->i_d
.di_mtime
.t_nsec
= (__int32_t
)tvp
->tv_nsec
;
172 if (flags
& XFS_ICHGTIME_CHG
) {
173 tvp
= &inode
->i_ctime
;
174 ip
->i_d
.di_ctime
.t_sec
= (__int32_t
)tvp
->tv_sec
;
175 ip
->i_d
.di_ctime
.t_nsec
= (__int32_t
)tvp
->tv_nsec
;
179 * We update the i_update_core field _after_ changing
180 * the timestamps in order to coordinate properly with
181 * xfs_iflush() so that we don't lose timestamp updates.
182 * This keeps us from having to hold the inode lock
183 * while doing this. We use the SYNCHRONIZE macro to
184 * ensure that the compiler does not reorder the update
185 * of i_update_core above the timestamp updates above.
188 ip
->i_update_core
= 1;
189 if (!(inode
->i_state
& I_NEW
))
190 mark_inode_dirty_sync(inode
);
195 * Pull the link count and size up from the xfs inode to the linux inode
201 struct xfs_inode
*ip
= XFS_I(inode
);
204 /* we're under i_sem so i_size can't change under us */
205 size
= XFS_ISIZE(ip
);
206 if (i_size_read(inode
) != size
)
207 i_size_write(inode
, size
);
211 * Hook in SELinux. This is not quite correct yet, what we really need
212 * here (as we do for default ACLs) is a mechanism by which creation of
213 * these attrs can be journalled at inode creation time (along with the
214 * inode, of course, such that log replay can't cause these to be lost).
221 struct inode
*ip
= vn_to_inode(vp
);
227 error
= security_inode_init_security(ip
, dir
, &name
, &value
, &length
);
229 if (error
== -EOPNOTSUPP
)
234 error
= xfs_attr_set(XFS_I(ip
), name
, value
,
235 length
, ATTR_SECURE
);
237 xfs_iflags_set(XFS_I(ip
), XFS_IMODIFIED
);
248 struct dentry
*dentry
,
251 struct dentry teardown
= {};
254 * If we can't add the ACL or we fail in
255 * xfs_init_security we must back out.
256 * ENOSPC can hit here, among other things.
258 teardown
.d_inode
= vn_to_inode(vp
);
259 teardown
.d_name
= dentry
->d_name
;
262 xfs_rmdir(XFS_I(dir
), &teardown
);
264 xfs_remove(XFS_I(dir
), &teardown
);
271 struct dentry
*dentry
,
276 struct xfs_inode
*ip
= NULL
;
277 xfs_acl_t
*default_acl
= NULL
;
278 attrexists_t test_default_acl
= _ACL_DEFAULT_EXISTS
;
282 * Irix uses Missed'em'V split, but doesn't want to see
283 * the upper 5 bits of (14bit) major.
285 if (unlikely(!sysv_valid_dev(rdev
) || MAJOR(rdev
) & ~0x1ff))
288 if (test_default_acl
&& test_default_acl(dir
)) {
289 if (!_ACL_ALLOC(default_acl
)) {
292 if (!_ACL_GET_DEFAULT(dir
, default_acl
)) {
293 _ACL_FREE(default_acl
);
298 if (IS_POSIXACL(dir
) && !default_acl
)
299 mode
&= ~current
->fs
->umask
;
301 switch (mode
& S_IFMT
) {
306 rdev
= sysv_encode_dev(rdev
);
308 error
= xfs_create(XFS_I(dir
), dentry
, mode
, rdev
, &ip
, NULL
);
311 error
= xfs_mkdir(XFS_I(dir
), dentry
, mode
, &ip
, NULL
);
323 error
= xfs_init_security(inode
, dir
);
325 goto out_cleanup_inode
;
328 error
= _ACL_INHERIT(inode
, mode
, default_acl
);
330 goto out_cleanup_inode
;
331 xfs_iflags_set(ip
, XFS_IMODIFIED
);
332 _ACL_FREE(default_acl
);
337 xfs_validate_fields(inode
);
338 d_instantiate(dentry
, inode
);
339 xfs_validate_fields(dir
);
343 xfs_cleanup_inode(dir
, inode
, dentry
, mode
);
346 _ACL_FREE(default_acl
);
353 struct dentry
*dentry
,
355 struct nameidata
*nd
)
357 return xfs_vn_mknod(dir
, dentry
, mode
, 0);
363 struct dentry
*dentry
,
366 return xfs_vn_mknod(dir
, dentry
, mode
|S_IFDIR
, 0);
369 STATIC
struct dentry
*
372 struct dentry
*dentry
,
373 struct nameidata
*nd
)
375 struct xfs_inode
*cip
;
378 if (dentry
->d_name
.len
>= MAXNAMELEN
)
379 return ERR_PTR(-ENAMETOOLONG
);
381 error
= xfs_lookup(XFS_I(dir
), dentry
, &cip
);
382 if (unlikely(error
)) {
383 if (unlikely(error
!= ENOENT
))
384 return ERR_PTR(-error
);
389 return d_splice_alias(cip
->i_vnode
, dentry
);
394 struct dentry
*old_dentry
,
396 struct dentry
*dentry
)
398 struct inode
*inode
; /* inode of guy being linked to */
401 inode
= old_dentry
->d_inode
;
404 error
= xfs_link(XFS_I(dir
), XFS_I(inode
), dentry
);
405 if (unlikely(error
)) {
410 xfs_iflags_set(XFS_I(dir
), XFS_IMODIFIED
);
411 xfs_validate_fields(inode
);
412 d_instantiate(dentry
, inode
);
419 struct dentry
*dentry
)
424 inode
= dentry
->d_inode
;
426 error
= xfs_remove(XFS_I(dir
), dentry
);
427 if (likely(!error
)) {
428 xfs_validate_fields(dir
); /* size needs update */
429 xfs_validate_fields(inode
);
437 struct dentry
*dentry
,
441 struct xfs_inode
*cip
= NULL
;
446 (irix_symlink_mode
? 0777 & ~current
->fs
->umask
: S_IRWXUGO
);
448 error
= xfs_symlink(XFS_I(dir
), dentry
, (char *)symname
, mode
,
453 inode
= cip
->i_vnode
;
455 error
= xfs_init_security(inode
, dir
);
457 goto out_cleanup_inode
;
459 d_instantiate(dentry
, inode
);
460 xfs_validate_fields(dir
);
461 xfs_validate_fields(inode
);
465 xfs_cleanup_inode(dir
, inode
, dentry
, 0);
473 struct dentry
*dentry
)
475 struct inode
*inode
= dentry
->d_inode
;
478 error
= xfs_rmdir(XFS_I(dir
), dentry
);
479 if (likely(!error
)) {
480 xfs_validate_fields(inode
);
481 xfs_validate_fields(dir
);
489 struct dentry
*odentry
,
491 struct dentry
*ndentry
)
493 struct inode
*new_inode
= ndentry
->d_inode
;
496 error
= xfs_rename(XFS_I(odir
), odentry
, XFS_I(ndir
), ndentry
);
497 if (likely(!error
)) {
499 xfs_validate_fields(new_inode
);
500 xfs_validate_fields(odir
);
502 xfs_validate_fields(ndir
);
508 * careful here - this function can get called recursively, so
509 * we need to be very careful about how much stack we use.
510 * uio is kmalloced for this reason...
514 struct dentry
*dentry
,
515 struct nameidata
*nd
)
520 link
= kmalloc(MAXPATHLEN
+1, GFP_KERNEL
);
524 error
= -xfs_readlink(XFS_I(dentry
->d_inode
), link
);
528 nd_set_link(nd
, link
);
534 nd_set_link(nd
, ERR_PTR(error
));
540 struct dentry
*dentry
,
541 struct nameidata
*nd
,
544 char *s
= nd_get_link(nd
);
550 #ifdef CONFIG_XFS_POSIX_ACL
556 struct xfs_inode
*ip
= XFS_I(inode
);
559 xfs_itrace_entry(ip
);
561 if (XFS_IFORK_Q(ip
)) {
562 error
= xfs_acl_iaccess(ip
, mask
, NULL
);
574 struct nameidata
*nd
)
576 return generic_permission(inode
, mask
, xfs_check_acl
);
579 #define xfs_vn_permission NULL
584 struct vfsmount
*mnt
,
585 struct dentry
*dentry
,
588 struct inode
*inode
= dentry
->d_inode
;
589 struct xfs_inode
*ip
= XFS_I(inode
);
590 struct xfs_mount
*mp
= ip
->i_mount
;
592 xfs_itrace_entry(ip
);
594 if (XFS_FORCED_SHUTDOWN(mp
))
595 return XFS_ERROR(EIO
);
597 stat
->size
= XFS_ISIZE(ip
);
598 stat
->dev
= inode
->i_sb
->s_dev
;
599 stat
->mode
= ip
->i_d
.di_mode
;
600 stat
->nlink
= ip
->i_d
.di_nlink
;
601 stat
->uid
= ip
->i_d
.di_uid
;
602 stat
->gid
= ip
->i_d
.di_gid
;
603 stat
->ino
= ip
->i_ino
;
605 stat
->ino
+= mp
->m_inoadd
;
607 stat
->atime
= inode
->i_atime
;
608 stat
->mtime
.tv_sec
= ip
->i_d
.di_mtime
.t_sec
;
609 stat
->mtime
.tv_nsec
= ip
->i_d
.di_mtime
.t_nsec
;
610 stat
->ctime
.tv_sec
= ip
->i_d
.di_ctime
.t_sec
;
611 stat
->ctime
.tv_nsec
= ip
->i_d
.di_ctime
.t_nsec
;
613 XFS_FSB_TO_BB(mp
, ip
->i_d
.di_nblocks
+ ip
->i_delayed_blks
);
616 switch (inode
->i_mode
& S_IFMT
) {
619 stat
->blksize
= BLKDEV_IOSIZE
;
620 stat
->rdev
= MKDEV(sysv_major(ip
->i_df
.if_u2
.if_rdev
) & 0x1ff,
621 sysv_minor(ip
->i_df
.if_u2
.if_rdev
));
624 if (XFS_IS_REALTIME_INODE(ip
)) {
626 * If the file blocks are being allocated from a
627 * realtime volume, then return the inode's realtime
628 * extent size or the realtime volume's extent size.
631 xfs_get_extsz_hint(ip
) << mp
->m_sb
.sb_blocklog
;
633 stat
->blksize
= xfs_preferred_iosize(mp
);
643 struct dentry
*dentry
,
646 struct inode
*inode
= dentry
->d_inode
;
647 unsigned int ia_valid
= attr
->ia_valid
;
648 bhv_vattr_t vattr
= { 0 };
652 if (ia_valid
& ATTR_UID
) {
653 vattr
.va_mask
|= XFS_AT_UID
;
654 vattr
.va_uid
= attr
->ia_uid
;
656 if (ia_valid
& ATTR_GID
) {
657 vattr
.va_mask
|= XFS_AT_GID
;
658 vattr
.va_gid
= attr
->ia_gid
;
660 if (ia_valid
& ATTR_SIZE
) {
661 vattr
.va_mask
|= XFS_AT_SIZE
;
662 vattr
.va_size
= attr
->ia_size
;
664 if (ia_valid
& ATTR_ATIME
) {
665 vattr
.va_mask
|= XFS_AT_ATIME
;
666 vattr
.va_atime
= attr
->ia_atime
;
667 inode
->i_atime
= attr
->ia_atime
;
669 if (ia_valid
& ATTR_MTIME
) {
670 vattr
.va_mask
|= XFS_AT_MTIME
;
671 vattr
.va_mtime
= attr
->ia_mtime
;
673 if (ia_valid
& ATTR_CTIME
) {
674 vattr
.va_mask
|= XFS_AT_CTIME
;
675 vattr
.va_ctime
= attr
->ia_ctime
;
677 if (ia_valid
& ATTR_MODE
) {
678 vattr
.va_mask
|= XFS_AT_MODE
;
679 vattr
.va_mode
= attr
->ia_mode
;
680 if (!in_group_p(inode
->i_gid
) && !capable(CAP_FSETID
))
681 inode
->i_mode
&= ~S_ISGID
;
684 if (ia_valid
& (ATTR_MTIME_SET
| ATTR_ATIME_SET
))
687 if ((ia_valid
& ATTR_NO_BLOCK
))
688 flags
|= ATTR_NONBLOCK
;
691 error
= xfs_setattr(XFS_I(inode
), &vattr
, flags
, NULL
);
693 vn_revalidate(vn_from_inode(inode
));
701 block_truncate_page(inode
->i_mapping
, inode
->i_size
, xfs_get_blocks
);
706 struct dentry
*dentry
,
712 bhv_vnode_t
*vp
= vn_from_inode(dentry
->d_inode
);
713 char *attr
= (char *)name
;
718 namesp
= attr_lookup_namespace(attr
, attr_namespaces
, ATTR_NAMECOUNT
);
721 attr
+= namesp
->attr_namelen
;
722 error
= namesp
->attr_capable(vp
, NULL
);
726 /* Convert Linux syscall to XFS internal ATTR flags */
727 if (flags
& XATTR_CREATE
)
728 xflags
|= ATTR_CREATE
;
729 if (flags
& XATTR_REPLACE
)
730 xflags
|= ATTR_REPLACE
;
731 xflags
|= namesp
->attr_flag
;
732 return namesp
->attr_set(vp
, attr
, (void *)data
, size
, xflags
);
737 struct dentry
*dentry
,
742 bhv_vnode_t
*vp
= vn_from_inode(dentry
->d_inode
);
743 char *attr
= (char *)name
;
748 namesp
= attr_lookup_namespace(attr
, attr_namespaces
, ATTR_NAMECOUNT
);
751 attr
+= namesp
->attr_namelen
;
752 error
= namesp
->attr_capable(vp
, NULL
);
756 /* Convert Linux syscall to XFS internal ATTR flags */
758 xflags
|= ATTR_KERNOVAL
;
761 xflags
|= namesp
->attr_flag
;
762 return namesp
->attr_get(vp
, attr
, (void *)data
, size
, xflags
);
767 struct dentry
*dentry
,
771 bhv_vnode_t
*vp
= vn_from_inode(dentry
->d_inode
);
772 int error
, xflags
= ATTR_KERNAMELS
;
776 xflags
|= ATTR_KERNOVAL
;
777 xflags
|= capable(CAP_SYS_ADMIN
) ? ATTR_KERNFULLS
: ATTR_KERNORMALS
;
779 error
= attr_generic_list(vp
, data
, size
, xflags
, &result
);
787 struct dentry
*dentry
,
790 bhv_vnode_t
*vp
= vn_from_inode(dentry
->d_inode
);
791 char *attr
= (char *)name
;
796 namesp
= attr_lookup_namespace(attr
, attr_namespaces
, ATTR_NAMECOUNT
);
799 attr
+= namesp
->attr_namelen
;
800 error
= namesp
->attr_capable(vp
, NULL
);
803 xflags
|= namesp
->attr_flag
;
804 return namesp
->attr_remove(vp
, attr
, xflags
);
817 xfs_inode_t
*ip
= XFS_I(inode
);
819 /* preallocation on directories not yet supported */
821 if (S_ISDIR(inode
->i_mode
))
828 xfs_ilock(ip
, XFS_IOLOCK_EXCL
);
829 error
= xfs_change_file_space(ip
, XFS_IOC_RESVSP
, &bf
,
830 0, NULL
, ATTR_NOLOCK
);
831 if (!error
&& !(mode
& FALLOC_FL_KEEP_SIZE
) &&
832 offset
+ len
> i_size_read(inode
))
833 new_size
= offset
+ len
;
835 /* Change file size if needed */
839 va
.va_mask
= XFS_AT_SIZE
;
840 va
.va_size
= new_size
;
841 error
= xfs_setattr(ip
, &va
, ATTR_NOLOCK
, NULL
);
844 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
849 const struct inode_operations xfs_inode_operations
= {
850 .permission
= xfs_vn_permission
,
851 .truncate
= xfs_vn_truncate
,
852 .getattr
= xfs_vn_getattr
,
853 .setattr
= xfs_vn_setattr
,
854 .setxattr
= xfs_vn_setxattr
,
855 .getxattr
= xfs_vn_getxattr
,
856 .listxattr
= xfs_vn_listxattr
,
857 .removexattr
= xfs_vn_removexattr
,
858 .fallocate
= xfs_vn_fallocate
,
861 const struct inode_operations xfs_dir_inode_operations
= {
862 .create
= xfs_vn_create
,
863 .lookup
= xfs_vn_lookup
,
865 .unlink
= xfs_vn_unlink
,
866 .symlink
= xfs_vn_symlink
,
867 .mkdir
= xfs_vn_mkdir
,
868 .rmdir
= xfs_vn_rmdir
,
869 .mknod
= xfs_vn_mknod
,
870 .rename
= xfs_vn_rename
,
871 .permission
= xfs_vn_permission
,
872 .getattr
= xfs_vn_getattr
,
873 .setattr
= xfs_vn_setattr
,
874 .setxattr
= xfs_vn_setxattr
,
875 .getxattr
= xfs_vn_getxattr
,
876 .listxattr
= xfs_vn_listxattr
,
877 .removexattr
= xfs_vn_removexattr
,
880 const struct inode_operations xfs_symlink_inode_operations
= {
881 .readlink
= generic_readlink
,
882 .follow_link
= xfs_vn_follow_link
,
883 .put_link
= xfs_vn_put_link
,
884 .permission
= xfs_vn_permission
,
885 .getattr
= xfs_vn_getattr
,
886 .setattr
= xfs_vn_setattr
,
887 .setxattr
= xfs_vn_setxattr
,
888 .getxattr
= xfs_vn_getxattr
,
889 .listxattr
= xfs_vn_listxattr
,
890 .removexattr
= xfs_vn_removexattr
,