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
);
245 * Determine whether a process has a valid fs_struct (kernel daemons
246 * like knfsd don't have an fs_struct).
248 * XXX(hch): nfsd is broken, better fix it instead.
251 xfs_has_fs_struct(struct task_struct
*task
)
253 return (task
->fs
!= init_task
.fs
);
260 struct dentry
*dentry
,
263 struct dentry teardown
= {};
266 * If we can't add the ACL or we fail in
267 * xfs_init_security we must back out.
268 * ENOSPC can hit here, among other things.
270 teardown
.d_inode
= vn_to_inode(vp
);
271 teardown
.d_name
= dentry
->d_name
;
274 xfs_rmdir(XFS_I(dir
), &teardown
);
276 xfs_remove(XFS_I(dir
), &teardown
);
283 struct dentry
*dentry
,
288 bhv_vnode_t
*vp
= NULL
, *dvp
= vn_from_inode(dir
);
289 xfs_acl_t
*default_acl
= NULL
;
290 attrexists_t test_default_acl
= _ACL_DEFAULT_EXISTS
;
294 * Irix uses Missed'em'V split, but doesn't want to see
295 * the upper 5 bits of (14bit) major.
297 if (unlikely(!sysv_valid_dev(rdev
) || MAJOR(rdev
) & ~0x1ff))
300 if (unlikely(test_default_acl
&& test_default_acl(dvp
))) {
301 if (!_ACL_ALLOC(default_acl
)) {
304 if (!_ACL_GET_DEFAULT(dvp
, default_acl
)) {
305 _ACL_FREE(default_acl
);
310 if (IS_POSIXACL(dir
) && !default_acl
&& xfs_has_fs_struct(current
))
311 mode
&= ~current
->fs
->umask
;
313 switch (mode
& S_IFMT
) {
314 case S_IFCHR
: case S_IFBLK
: case S_IFIFO
: case S_IFSOCK
:
315 rdev
= sysv_encode_dev(rdev
);
317 error
= xfs_create(XFS_I(dir
), dentry
, mode
, rdev
, &vp
, NULL
);
320 error
= xfs_mkdir(XFS_I(dir
), dentry
, mode
, &vp
, NULL
);
327 if (unlikely(!error
)) {
328 error
= xfs_init_security(vp
, dir
);
330 xfs_cleanup_inode(dir
, vp
, dentry
, mode
);
333 if (unlikely(default_acl
)) {
335 error
= _ACL_INHERIT(vp
, mode
, default_acl
);
337 xfs_iflags_set(XFS_I(vp
), XFS_IMODIFIED
);
339 xfs_cleanup_inode(dir
, vp
, dentry
, mode
);
341 _ACL_FREE(default_acl
);
344 if (likely(!error
)) {
346 ip
= vn_to_inode(vp
);
349 xfs_validate_fields(ip
);
350 d_instantiate(dentry
, ip
);
351 xfs_validate_fields(dir
);
359 struct dentry
*dentry
,
361 struct nameidata
*nd
)
363 return xfs_vn_mknod(dir
, dentry
, mode
, 0);
369 struct dentry
*dentry
,
372 return xfs_vn_mknod(dir
, dentry
, mode
|S_IFDIR
, 0);
375 STATIC
struct dentry
*
378 struct dentry
*dentry
,
379 struct nameidata
*nd
)
384 if (dentry
->d_name
.len
>= MAXNAMELEN
)
385 return ERR_PTR(-ENAMETOOLONG
);
387 error
= xfs_lookup(XFS_I(dir
), dentry
, &cvp
);
388 if (unlikely(error
)) {
389 if (unlikely(error
!= ENOENT
))
390 return ERR_PTR(-error
);
395 return d_splice_alias(vn_to_inode(cvp
), dentry
);
400 struct dentry
*old_dentry
,
402 struct dentry
*dentry
)
404 struct inode
*ip
; /* inode of guy being linked to */
405 bhv_vnode_t
*vp
; /* vp of name being linked */
408 ip
= old_dentry
->d_inode
; /* inode being linked to */
409 vp
= vn_from_inode(ip
);
412 error
= xfs_link(XFS_I(dir
), vp
, dentry
);
413 if (unlikely(error
)) {
416 xfs_iflags_set(XFS_I(dir
), XFS_IMODIFIED
);
417 xfs_validate_fields(ip
);
418 d_instantiate(dentry
, ip
);
426 struct dentry
*dentry
)
431 inode
= dentry
->d_inode
;
433 error
= xfs_remove(XFS_I(dir
), dentry
);
434 if (likely(!error
)) {
435 xfs_validate_fields(dir
); /* size needs update */
436 xfs_validate_fields(inode
);
444 struct dentry
*dentry
,
448 bhv_vnode_t
*cvp
; /* used to lookup symlink to put in dentry */
455 (irix_symlink_mode
? 0777 & ~current
->fs
->umask
: S_IRWXUGO
);
457 error
= xfs_symlink(XFS_I(dir
), dentry
, (char *)symname
, mode
,
459 if (likely(!error
&& cvp
)) {
460 error
= xfs_init_security(cvp
, dir
);
461 if (likely(!error
)) {
462 ip
= vn_to_inode(cvp
);
463 d_instantiate(dentry
, ip
);
464 xfs_validate_fields(dir
);
465 xfs_validate_fields(ip
);
467 xfs_cleanup_inode(dir
, cvp
, dentry
, 0);
476 struct dentry
*dentry
)
478 struct inode
*inode
= dentry
->d_inode
;
481 error
= xfs_rmdir(XFS_I(dir
), dentry
);
482 if (likely(!error
)) {
483 xfs_validate_fields(inode
);
484 xfs_validate_fields(dir
);
492 struct dentry
*odentry
,
494 struct dentry
*ndentry
)
496 struct inode
*new_inode
= ndentry
->d_inode
;
497 bhv_vnode_t
*tvp
; /* target directory */
500 tvp
= vn_from_inode(ndir
);
502 error
= xfs_rename(XFS_I(odir
), odentry
, tvp
, ndentry
);
503 if (likely(!error
)) {
505 xfs_validate_fields(new_inode
);
506 xfs_validate_fields(odir
);
508 xfs_validate_fields(ndir
);
514 * careful here - this function can get called recursively, so
515 * we need to be very careful about how much stack we use.
516 * uio is kmalloced for this reason...
520 struct dentry
*dentry
,
521 struct nameidata
*nd
)
526 link
= kmalloc(MAXPATHLEN
+1, GFP_KERNEL
);
530 error
= -xfs_readlink(XFS_I(dentry
->d_inode
), link
);
534 nd_set_link(nd
, link
);
540 nd_set_link(nd
, ERR_PTR(error
));
546 struct dentry
*dentry
,
547 struct nameidata
*nd
,
550 char *s
= nd_get_link(nd
);
556 #ifdef CONFIG_XFS_POSIX_ACL
562 struct xfs_inode
*ip
= XFS_I(inode
);
565 xfs_itrace_entry(ip
);
567 if (XFS_IFORK_Q(ip
)) {
568 error
= xfs_acl_iaccess(ip
, mask
, NULL
);
580 struct nameidata
*nd
)
582 return generic_permission(inode
, mask
, xfs_check_acl
);
585 #define xfs_vn_permission NULL
590 struct vfsmount
*mnt
,
591 struct dentry
*dentry
,
594 struct inode
*inode
= dentry
->d_inode
;
595 struct xfs_inode
*ip
= XFS_I(inode
);
596 struct xfs_mount
*mp
= ip
->i_mount
;
598 xfs_itrace_entry(ip
);
600 if (XFS_FORCED_SHUTDOWN(mp
))
601 return XFS_ERROR(EIO
);
603 stat
->size
= XFS_ISIZE(ip
);
604 stat
->dev
= inode
->i_sb
->s_dev
;
605 stat
->mode
= ip
->i_d
.di_mode
;
606 stat
->nlink
= ip
->i_d
.di_nlink
;
607 stat
->uid
= ip
->i_d
.di_uid
;
608 stat
->gid
= ip
->i_d
.di_gid
;
609 stat
->ino
= ip
->i_ino
;
611 stat
->ino
+= mp
->m_inoadd
;
613 stat
->atime
= inode
->i_atime
;
614 stat
->mtime
.tv_sec
= ip
->i_d
.di_mtime
.t_sec
;
615 stat
->mtime
.tv_nsec
= ip
->i_d
.di_mtime
.t_nsec
;
616 stat
->ctime
.tv_sec
= ip
->i_d
.di_ctime
.t_sec
;
617 stat
->ctime
.tv_nsec
= ip
->i_d
.di_ctime
.t_nsec
;
619 XFS_FSB_TO_BB(mp
, ip
->i_d
.di_nblocks
+ ip
->i_delayed_blks
);
622 switch (inode
->i_mode
& S_IFMT
) {
625 stat
->blksize
= BLKDEV_IOSIZE
;
626 stat
->rdev
= MKDEV(sysv_major(ip
->i_df
.if_u2
.if_rdev
) & 0x1ff,
627 sysv_minor(ip
->i_df
.if_u2
.if_rdev
));
630 if (XFS_IS_REALTIME_INODE(ip
)) {
632 * If the file blocks are being allocated from a
633 * realtime volume, then return the inode's realtime
634 * extent size or the realtime volume's extent size.
637 xfs_get_extsz_hint(ip
) << mp
->m_sb
.sb_blocklog
;
639 stat
->blksize
= xfs_preferred_iosize(mp
);
649 struct dentry
*dentry
,
652 struct inode
*inode
= dentry
->d_inode
;
653 unsigned int ia_valid
= attr
->ia_valid
;
654 bhv_vattr_t vattr
= { 0 };
658 if (ia_valid
& ATTR_UID
) {
659 vattr
.va_mask
|= XFS_AT_UID
;
660 vattr
.va_uid
= attr
->ia_uid
;
662 if (ia_valid
& ATTR_GID
) {
663 vattr
.va_mask
|= XFS_AT_GID
;
664 vattr
.va_gid
= attr
->ia_gid
;
666 if (ia_valid
& ATTR_SIZE
) {
667 vattr
.va_mask
|= XFS_AT_SIZE
;
668 vattr
.va_size
= attr
->ia_size
;
670 if (ia_valid
& ATTR_ATIME
) {
671 vattr
.va_mask
|= XFS_AT_ATIME
;
672 vattr
.va_atime
= attr
->ia_atime
;
673 inode
->i_atime
= attr
->ia_atime
;
675 if (ia_valid
& ATTR_MTIME
) {
676 vattr
.va_mask
|= XFS_AT_MTIME
;
677 vattr
.va_mtime
= attr
->ia_mtime
;
679 if (ia_valid
& ATTR_CTIME
) {
680 vattr
.va_mask
|= XFS_AT_CTIME
;
681 vattr
.va_ctime
= attr
->ia_ctime
;
683 if (ia_valid
& ATTR_MODE
) {
684 vattr
.va_mask
|= XFS_AT_MODE
;
685 vattr
.va_mode
= attr
->ia_mode
;
686 if (!in_group_p(inode
->i_gid
) && !capable(CAP_FSETID
))
687 inode
->i_mode
&= ~S_ISGID
;
690 if (ia_valid
& (ATTR_MTIME_SET
| ATTR_ATIME_SET
))
693 if ((ia_valid
& ATTR_NO_BLOCK
))
694 flags
|= ATTR_NONBLOCK
;
697 error
= xfs_setattr(XFS_I(inode
), &vattr
, flags
, NULL
);
699 vn_revalidate(vn_from_inode(inode
));
707 block_truncate_page(inode
->i_mapping
, inode
->i_size
, xfs_get_blocks
);
712 struct dentry
*dentry
,
718 bhv_vnode_t
*vp
= vn_from_inode(dentry
->d_inode
);
719 char *attr
= (char *)name
;
724 namesp
= attr_lookup_namespace(attr
, attr_namespaces
, ATTR_NAMECOUNT
);
727 attr
+= namesp
->attr_namelen
;
728 error
= namesp
->attr_capable(vp
, NULL
);
732 /* Convert Linux syscall to XFS internal ATTR flags */
733 if (flags
& XATTR_CREATE
)
734 xflags
|= ATTR_CREATE
;
735 if (flags
& XATTR_REPLACE
)
736 xflags
|= ATTR_REPLACE
;
737 xflags
|= namesp
->attr_flag
;
738 return namesp
->attr_set(vp
, attr
, (void *)data
, size
, xflags
);
743 struct dentry
*dentry
,
748 bhv_vnode_t
*vp
= vn_from_inode(dentry
->d_inode
);
749 char *attr
= (char *)name
;
754 namesp
= attr_lookup_namespace(attr
, attr_namespaces
, ATTR_NAMECOUNT
);
757 attr
+= namesp
->attr_namelen
;
758 error
= namesp
->attr_capable(vp
, NULL
);
762 /* Convert Linux syscall to XFS internal ATTR flags */
764 xflags
|= ATTR_KERNOVAL
;
767 xflags
|= namesp
->attr_flag
;
768 return namesp
->attr_get(vp
, attr
, (void *)data
, size
, xflags
);
773 struct dentry
*dentry
,
777 bhv_vnode_t
*vp
= vn_from_inode(dentry
->d_inode
);
778 int error
, xflags
= ATTR_KERNAMELS
;
782 xflags
|= ATTR_KERNOVAL
;
783 xflags
|= capable(CAP_SYS_ADMIN
) ? ATTR_KERNFULLS
: ATTR_KERNORMALS
;
785 error
= attr_generic_list(vp
, data
, size
, xflags
, &result
);
793 struct dentry
*dentry
,
796 bhv_vnode_t
*vp
= vn_from_inode(dentry
->d_inode
);
797 char *attr
= (char *)name
;
802 namesp
= attr_lookup_namespace(attr
, attr_namespaces
, ATTR_NAMECOUNT
);
805 attr
+= namesp
->attr_namelen
;
806 error
= namesp
->attr_capable(vp
, NULL
);
809 xflags
|= namesp
->attr_flag
;
810 return namesp
->attr_remove(vp
, attr
, xflags
);
823 xfs_inode_t
*ip
= XFS_I(inode
);
825 /* preallocation on directories not yet supported */
827 if (S_ISDIR(inode
->i_mode
))
834 xfs_ilock(ip
, XFS_IOLOCK_EXCL
);
835 error
= xfs_change_file_space(ip
, XFS_IOC_RESVSP
, &bf
,
836 0, NULL
, ATTR_NOLOCK
);
837 if (!error
&& !(mode
& FALLOC_FL_KEEP_SIZE
) &&
838 offset
+ len
> i_size_read(inode
))
839 new_size
= offset
+ len
;
841 /* Change file size if needed */
845 va
.va_mask
= XFS_AT_SIZE
;
846 va
.va_size
= new_size
;
847 error
= xfs_setattr(ip
, &va
, ATTR_NOLOCK
, NULL
);
850 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
855 const struct inode_operations xfs_inode_operations
= {
856 .permission
= xfs_vn_permission
,
857 .truncate
= xfs_vn_truncate
,
858 .getattr
= xfs_vn_getattr
,
859 .setattr
= xfs_vn_setattr
,
860 .setxattr
= xfs_vn_setxattr
,
861 .getxattr
= xfs_vn_getxattr
,
862 .listxattr
= xfs_vn_listxattr
,
863 .removexattr
= xfs_vn_removexattr
,
864 .fallocate
= xfs_vn_fallocate
,
867 const struct inode_operations xfs_dir_inode_operations
= {
868 .create
= xfs_vn_create
,
869 .lookup
= xfs_vn_lookup
,
871 .unlink
= xfs_vn_unlink
,
872 .symlink
= xfs_vn_symlink
,
873 .mkdir
= xfs_vn_mkdir
,
874 .rmdir
= xfs_vn_rmdir
,
875 .mknod
= xfs_vn_mknod
,
876 .rename
= xfs_vn_rename
,
877 .permission
= xfs_vn_permission
,
878 .getattr
= xfs_vn_getattr
,
879 .setattr
= xfs_vn_setattr
,
880 .setxattr
= xfs_vn_setxattr
,
881 .getxattr
= xfs_vn_getxattr
,
882 .listxattr
= xfs_vn_listxattr
,
883 .removexattr
= xfs_vn_removexattr
,
886 const struct inode_operations xfs_symlink_inode_operations
= {
887 .readlink
= generic_readlink
,
888 .follow_link
= xfs_vn_follow_link
,
889 .put_link
= xfs_vn_put_link
,
890 .permission
= xfs_vn_permission
,
891 .getattr
= xfs_vn_getattr
,
892 .setattr
= xfs_vn_setattr
,
893 .setxattr
= xfs_vn_setxattr
,
894 .getxattr
= xfs_vn_getxattr
,
895 .listxattr
= xfs_vn_listxattr
,
896 .removexattr
= xfs_vn_removexattr
,