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"
28 #include "xfs_alloc.h"
29 #include "xfs_dmapi.h"
30 #include "xfs_quota.h"
31 #include "xfs_mount.h"
32 #include "xfs_bmap_btree.h"
33 #include "xfs_alloc_btree.h"
34 #include "xfs_ialloc_btree.h"
35 #include "xfs_dir_sf.h"
36 #include "xfs_dir2_sf.h"
37 #include "xfs_attr_sf.h"
38 #include "xfs_dinode.h"
39 #include "xfs_inode.h"
41 #include "xfs_btree.h"
42 #include "xfs_ialloc.h"
43 #include "xfs_rtalloc.h"
44 #include "xfs_error.h"
45 #include "xfs_itable.h"
51 #include "xfs_buf_item.h"
52 #include "xfs_utils.h"
54 #include <linux/capability.h>
55 #include <linux/xattr.h>
56 #include <linux/namei.h>
57 #include <linux/security.h>
60 * Get a XFS inode from a given vnode.
68 bdp
= bhv_lookup_range(VN_BHV_HEAD(vp
),
69 VNODE_POSITION_XFS
, VNODE_POSITION_XFS
);
70 if (unlikely(bdp
== NULL
))
72 return XFS_BHVTOI(bdp
);
76 * Bring the atime in the XFS inode uptodate.
77 * Used before logging the inode to disk or when the Linux inode goes away.
80 xfs_synchronize_atime(
85 vp
= XFS_ITOV_NULL(ip
);
87 struct inode
*inode
= &vp
->v_inode
;
88 ip
->i_d
.di_atime
.t_sec
= (__int32_t
)inode
->i_atime
.tv_sec
;
89 ip
->i_d
.di_atime
.t_nsec
= (__int32_t
)inode
->i_atime
.tv_nsec
;
94 * Change the requested timestamp in the given inode.
95 * We don't lock across timestamp updates, and we don't log them but
96 * we do record the fact that there is dirty information in core.
98 * NOTE -- callers MUST combine XFS_ICHGTIME_MOD or XFS_ICHGTIME_CHG
99 * with XFS_ICHGTIME_ACC to be sure that access time
100 * update will take. Calling first with XFS_ICHGTIME_ACC
101 * and then XFS_ICHGTIME_MOD may fail to modify the access
102 * timestamp if the filesystem is mounted noacctm.
109 struct inode
*inode
= vn_to_inode(XFS_ITOV(ip
));
113 if (flags
& XFS_ICHGTIME_MOD
) {
115 ip
->i_d
.di_mtime
.t_sec
= (__int32_t
)tv
.tv_sec
;
116 ip
->i_d
.di_mtime
.t_nsec
= (__int32_t
)tv
.tv_nsec
;
118 if (flags
& XFS_ICHGTIME_ACC
) {
120 ip
->i_d
.di_atime
.t_sec
= (__int32_t
)tv
.tv_sec
;
121 ip
->i_d
.di_atime
.t_nsec
= (__int32_t
)tv
.tv_nsec
;
123 if (flags
& XFS_ICHGTIME_CHG
) {
125 ip
->i_d
.di_ctime
.t_sec
= (__int32_t
)tv
.tv_sec
;
126 ip
->i_d
.di_ctime
.t_nsec
= (__int32_t
)tv
.tv_nsec
;
130 * We update the i_update_core field _after_ changing
131 * the timestamps in order to coordinate properly with
132 * xfs_iflush() so that we don't lose timestamp updates.
133 * This keeps us from having to hold the inode lock
134 * while doing this. We use the SYNCHRONIZE macro to
135 * ensure that the compiler does not reorder the update
136 * of i_update_core above the timestamp updates above.
139 ip
->i_update_core
= 1;
140 if (!(inode
->i_state
& I_LOCK
))
141 mark_inode_dirty_sync(inode
);
145 * Variant on the above which avoids querying the system clock
146 * in situations where we know the Linux inode timestamps have
147 * just been updated (and so we can update our inode cheaply).
158 * Atime updates for read() & friends are handled lazily now, and
159 * explicit updates must go through xfs_ichgtime()
161 ASSERT((flags
& XFS_ICHGTIME_ACC
) == 0);
164 * We're not supposed to change timestamps in readonly-mounted
165 * filesystems. Throw it away if anyone asks us.
167 if (unlikely(IS_RDONLY(inode
)))
170 if (flags
& XFS_ICHGTIME_MOD
) {
171 tvp
= &inode
->i_mtime
;
172 ip
->i_d
.di_mtime
.t_sec
= (__int32_t
)tvp
->tv_sec
;
173 ip
->i_d
.di_mtime
.t_nsec
= (__int32_t
)tvp
->tv_nsec
;
175 if (flags
& XFS_ICHGTIME_CHG
) {
176 tvp
= &inode
->i_ctime
;
177 ip
->i_d
.di_ctime
.t_sec
= (__int32_t
)tvp
->tv_sec
;
178 ip
->i_d
.di_ctime
.t_nsec
= (__int32_t
)tvp
->tv_nsec
;
182 * We update the i_update_core field _after_ changing
183 * the timestamps in order to coordinate properly with
184 * xfs_iflush() so that we don't lose timestamp updates.
185 * This keeps us from having to hold the inode lock
186 * while doing this. We use the SYNCHRONIZE macro to
187 * ensure that the compiler does not reorder the update
188 * of i_update_core above the timestamp updates above.
191 ip
->i_update_core
= 1;
192 if (!(inode
->i_state
& I_LOCK
))
193 mark_inode_dirty_sync(inode
);
198 * Pull the link count and size up from the xfs inode to the linux inode
205 vattr
->va_mask
= XFS_AT_NLINK
|XFS_AT_SIZE
|XFS_AT_NBLOCKS
;
206 if (!bhv_vop_getattr(vn_from_inode(ip
), vattr
, ATTR_LAZY
, NULL
)) {
207 ip
->i_nlink
= vattr
->va_nlink
;
208 ip
->i_blocks
= vattr
->va_nblocks
;
210 /* we're under i_sem so i_size can't change under us */
211 if (i_size_read(ip
) != vattr
->va_size
)
212 i_size_write(ip
, vattr
->va_size
);
217 * Hook in SELinux. This is not quite correct yet, what we really need
218 * here (as we do for default ACLs) is a mechanism by which creation of
219 * these attrs can be journalled at inode creation time (along with the
220 * inode, of course, such that log replay can't cause these to be lost).
227 struct inode
*ip
= vn_to_inode(vp
);
233 error
= security_inode_init_security(ip
, dir
, &name
, &value
, &length
);
235 if (error
== -EOPNOTSUPP
)
240 error
= bhv_vop_attr_set(vp
, name
, value
, length
, ATTR_SECURE
, NULL
);
250 * Determine whether a process has a valid fs_struct (kernel daemons
251 * like knfsd don't have an fs_struct).
253 * XXX(hch): nfsd is broken, better fix it instead.
256 xfs_has_fs_struct(struct task_struct
*task
)
258 return (task
->fs
!= init_task
.fs
);
265 struct dentry
*dentry
,
268 struct dentry teardown
= {};
271 * If we can't add the ACL or we fail in
272 * xfs_init_security we must back out.
273 * ENOSPC can hit here, among other things.
275 teardown
.d_inode
= vn_to_inode(vp
);
276 teardown
.d_name
= dentry
->d_name
;
279 bhv_vop_rmdir(dvp
, &teardown
, NULL
);
281 bhv_vop_remove(dvp
, &teardown
, NULL
);
288 struct dentry
*dentry
,
293 bhv_vattr_t vattr
= { 0 };
294 bhv_vnode_t
*vp
= NULL
, *dvp
= vn_from_inode(dir
);
295 xfs_acl_t
*default_acl
= NULL
;
296 attrexists_t test_default_acl
= _ACL_DEFAULT_EXISTS
;
300 * Irix uses Missed'em'V split, but doesn't want to see
301 * the upper 5 bits of (14bit) major.
303 if (unlikely(!sysv_valid_dev(rdev
) || MAJOR(rdev
) & ~0x1ff))
306 if (unlikely(test_default_acl
&& test_default_acl(dvp
))) {
307 if (!_ACL_ALLOC(default_acl
)) {
310 if (!_ACL_GET_DEFAULT(dvp
, default_acl
)) {
311 _ACL_FREE(default_acl
);
316 if (IS_POSIXACL(dir
) && !default_acl
&& xfs_has_fs_struct(current
))
317 mode
&= ~current
->fs
->umask
;
319 vattr
.va_mask
= XFS_AT_TYPE
|XFS_AT_MODE
;
320 vattr
.va_mode
= mode
;
322 switch (mode
& S_IFMT
) {
323 case S_IFCHR
: case S_IFBLK
: case S_IFIFO
: case S_IFSOCK
:
324 vattr
.va_rdev
= sysv_encode_dev(rdev
);
325 vattr
.va_mask
|= XFS_AT_RDEV
;
328 error
= bhv_vop_create(dvp
, dentry
, &vattr
, &vp
, NULL
);
331 error
= bhv_vop_mkdir(dvp
, dentry
, &vattr
, &vp
, NULL
);
338 if (unlikely(!error
)) {
339 error
= xfs_init_security(vp
, dir
);
341 xfs_cleanup_inode(dvp
, vp
, dentry
, mode
);
344 if (unlikely(default_acl
)) {
346 error
= _ACL_INHERIT(vp
, &vattr
, default_acl
);
350 xfs_cleanup_inode(dvp
, vp
, dentry
, mode
);
352 _ACL_FREE(default_acl
);
355 if (likely(!error
)) {
357 ip
= vn_to_inode(vp
);
359 if (S_ISCHR(mode
) || S_ISBLK(mode
))
361 else if (S_ISDIR(mode
))
362 xfs_validate_fields(ip
, &vattr
);
363 d_instantiate(dentry
, ip
);
364 xfs_validate_fields(dir
, &vattr
);
372 struct dentry
*dentry
,
374 struct nameidata
*nd
)
376 return xfs_vn_mknod(dir
, dentry
, mode
, 0);
382 struct dentry
*dentry
,
385 return xfs_vn_mknod(dir
, dentry
, mode
|S_IFDIR
, 0);
388 STATIC
struct dentry
*
391 struct dentry
*dentry
,
392 struct nameidata
*nd
)
394 bhv_vnode_t
*vp
= vn_from_inode(dir
), *cvp
;
397 if (dentry
->d_name
.len
>= MAXNAMELEN
)
398 return ERR_PTR(-ENAMETOOLONG
);
400 error
= bhv_vop_lookup(vp
, dentry
, &cvp
, 0, NULL
, NULL
);
401 if (unlikely(error
)) {
402 if (unlikely(error
!= ENOENT
))
403 return ERR_PTR(-error
);
408 return d_splice_alias(vn_to_inode(cvp
), dentry
);
413 struct dentry
*old_dentry
,
415 struct dentry
*dentry
)
417 struct inode
*ip
; /* inode of guy being linked to */
418 bhv_vnode_t
*tdvp
; /* target directory for new name/link */
419 bhv_vnode_t
*vp
; /* vp of name being linked */
423 ip
= old_dentry
->d_inode
; /* inode being linked to */
424 if (S_ISDIR(ip
->i_mode
))
427 tdvp
= vn_from_inode(dir
);
428 vp
= vn_from_inode(ip
);
430 error
= bhv_vop_link(tdvp
, vp
, dentry
, NULL
);
431 if (likely(!error
)) {
434 xfs_validate_fields(ip
, &vattr
);
435 d_instantiate(dentry
, ip
);
443 struct dentry
*dentry
)
446 bhv_vnode_t
*dvp
; /* directory containing name to remove */
450 inode
= dentry
->d_inode
;
451 dvp
= vn_from_inode(dir
);
453 error
= bhv_vop_remove(dvp
, dentry
, NULL
);
454 if (likely(!error
)) {
455 xfs_validate_fields(dir
, &vattr
); /* size needs update */
456 xfs_validate_fields(inode
, &vattr
);
464 struct dentry
*dentry
,
468 bhv_vattr_t va
= { 0 };
469 bhv_vnode_t
*dvp
; /* directory containing name of symlink */
470 bhv_vnode_t
*cvp
; /* used to lookup symlink to put in dentry */
473 dvp
= vn_from_inode(dir
);
476 va
.va_mode
= S_IFLNK
|
477 (irix_symlink_mode
? 0777 & ~current
->fs
->umask
: S_IRWXUGO
);
478 va
.va_mask
= XFS_AT_TYPE
|XFS_AT_MODE
;
480 error
= bhv_vop_symlink(dvp
, dentry
, &va
, (char *)symname
, &cvp
, NULL
);
481 if (likely(!error
&& cvp
)) {
482 error
= xfs_init_security(cvp
, dir
);
483 if (likely(!error
)) {
484 ip
= vn_to_inode(cvp
);
485 d_instantiate(dentry
, ip
);
486 xfs_validate_fields(dir
, &va
);
487 xfs_validate_fields(ip
, &va
);
489 xfs_cleanup_inode(dvp
, cvp
, dentry
, 0);
498 struct dentry
*dentry
)
500 struct inode
*inode
= dentry
->d_inode
;
501 bhv_vnode_t
*dvp
= vn_from_inode(dir
);
505 error
= bhv_vop_rmdir(dvp
, dentry
, NULL
);
506 if (likely(!error
)) {
507 xfs_validate_fields(inode
, &vattr
);
508 xfs_validate_fields(dir
, &vattr
);
516 struct dentry
*odentry
,
518 struct dentry
*ndentry
)
520 struct inode
*new_inode
= ndentry
->d_inode
;
521 bhv_vnode_t
*fvp
; /* from directory */
522 bhv_vnode_t
*tvp
; /* target directory */
526 fvp
= vn_from_inode(odir
);
527 tvp
= vn_from_inode(ndir
);
529 error
= bhv_vop_rename(fvp
, odentry
, tvp
, ndentry
, NULL
);
530 if (likely(!error
)) {
532 xfs_validate_fields(new_inode
, &vattr
);
533 xfs_validate_fields(odir
, &vattr
);
535 xfs_validate_fields(ndir
, &vattr
);
541 * careful here - this function can get called recursively, so
542 * we need to be very careful about how much stack we use.
543 * uio is kmalloced for this reason...
547 struct dentry
*dentry
,
548 struct nameidata
*nd
)
559 link
= (char *)kmalloc(MAXPATHLEN
+1, GFP_KERNEL
);
561 nd_set_link(nd
, ERR_PTR(-ENOMEM
));
565 uio
= (uio_t
*)kmalloc(sizeof(uio_t
), GFP_KERNEL
);
568 nd_set_link(nd
, ERR_PTR(-ENOMEM
));
572 vp
= vn_from_inode(dentry
->d_inode
);
575 iov
.iov_len
= MAXPATHLEN
;
579 uio
->uio_segflg
= UIO_SYSSPACE
;
580 uio
->uio_resid
= MAXPATHLEN
;
583 error
= bhv_vop_readlink(vp
, uio
, 0, NULL
);
584 if (unlikely(error
)) {
586 link
= ERR_PTR(-error
);
588 link
[MAXPATHLEN
- uio
->uio_resid
] = '\0';
592 nd_set_link(nd
, link
);
598 struct dentry
*dentry
,
599 struct nameidata
*nd
,
602 char *s
= nd_get_link(nd
);
608 #ifdef CONFIG_XFS_POSIX_ACL
613 struct nameidata
*nd
)
615 return -bhv_vop_access(vn_from_inode(inode
), mode
<< 6, NULL
);
618 #define xfs_vn_permission NULL
623 struct vfsmount
*mnt
,
624 struct dentry
*dentry
,
627 struct inode
*inode
= dentry
->d_inode
;
628 bhv_vnode_t
*vp
= vn_from_inode(inode
);
631 if (unlikely(vp
->v_flag
& VMODIFIED
))
632 error
= vn_revalidate(vp
);
634 generic_fillattr(inode
, stat
);
640 struct dentry
*dentry
,
643 struct inode
*inode
= dentry
->d_inode
;
644 unsigned int ia_valid
= attr
->ia_valid
;
645 bhv_vnode_t
*vp
= vn_from_inode(inode
);
646 bhv_vattr_t vattr
= { 0 };
650 if (ia_valid
& ATTR_UID
) {
651 vattr
.va_mask
|= XFS_AT_UID
;
652 vattr
.va_uid
= attr
->ia_uid
;
654 if (ia_valid
& ATTR_GID
) {
655 vattr
.va_mask
|= XFS_AT_GID
;
656 vattr
.va_gid
= attr
->ia_gid
;
658 if (ia_valid
& ATTR_SIZE
) {
659 vattr
.va_mask
|= XFS_AT_SIZE
;
660 vattr
.va_size
= attr
->ia_size
;
662 if (ia_valid
& ATTR_ATIME
) {
663 vattr
.va_mask
|= XFS_AT_ATIME
;
664 vattr
.va_atime
= attr
->ia_atime
;
665 inode
->i_atime
= attr
->ia_atime
;
667 if (ia_valid
& ATTR_MTIME
) {
668 vattr
.va_mask
|= XFS_AT_MTIME
;
669 vattr
.va_mtime
= attr
->ia_mtime
;
671 if (ia_valid
& ATTR_CTIME
) {
672 vattr
.va_mask
|= XFS_AT_CTIME
;
673 vattr
.va_ctime
= attr
->ia_ctime
;
675 if (ia_valid
& ATTR_MODE
) {
676 vattr
.va_mask
|= XFS_AT_MODE
;
677 vattr
.va_mode
= attr
->ia_mode
;
678 if (!in_group_p(inode
->i_gid
) && !capable(CAP_FSETID
))
679 inode
->i_mode
&= ~S_ISGID
;
682 if (ia_valid
& (ATTR_MTIME_SET
| ATTR_ATIME_SET
))
685 if ((ia_valid
& ATTR_NO_BLOCK
))
686 flags
|= ATTR_NONBLOCK
;
689 error
= bhv_vop_setattr(vp
, &vattr
, flags
, NULL
);
691 __vn_revalidate(vp
, &vattr
);
699 block_truncate_page(inode
->i_mapping
, inode
->i_size
, xfs_get_blocks
);
704 struct dentry
*dentry
,
710 bhv_vnode_t
*vp
= vn_from_inode(dentry
->d_inode
);
711 char *attr
= (char *)name
;
716 namesp
= attr_lookup_namespace(attr
, attr_namespaces
, ATTR_NAMECOUNT
);
719 attr
+= namesp
->attr_namelen
;
720 error
= namesp
->attr_capable(vp
, NULL
);
724 /* Convert Linux syscall to XFS internal ATTR flags */
725 if (flags
& XATTR_CREATE
)
726 xflags
|= ATTR_CREATE
;
727 if (flags
& XATTR_REPLACE
)
728 xflags
|= ATTR_REPLACE
;
729 xflags
|= namesp
->attr_flag
;
730 return namesp
->attr_set(vp
, attr
, (void *)data
, size
, xflags
);
735 struct dentry
*dentry
,
740 bhv_vnode_t
*vp
= vn_from_inode(dentry
->d_inode
);
741 char *attr
= (char *)name
;
746 namesp
= attr_lookup_namespace(attr
, attr_namespaces
, ATTR_NAMECOUNT
);
749 attr
+= namesp
->attr_namelen
;
750 error
= namesp
->attr_capable(vp
, NULL
);
754 /* Convert Linux syscall to XFS internal ATTR flags */
756 xflags
|= ATTR_KERNOVAL
;
759 xflags
|= namesp
->attr_flag
;
760 return namesp
->attr_get(vp
, attr
, (void *)data
, size
, xflags
);
765 struct dentry
*dentry
,
769 bhv_vnode_t
*vp
= vn_from_inode(dentry
->d_inode
);
770 int error
, xflags
= ATTR_KERNAMELS
;
774 xflags
|= ATTR_KERNOVAL
;
775 xflags
|= capable(CAP_SYS_ADMIN
) ? ATTR_KERNFULLS
: ATTR_KERNORMALS
;
777 error
= attr_generic_list(vp
, data
, size
, xflags
, &result
);
785 struct dentry
*dentry
,
788 bhv_vnode_t
*vp
= vn_from_inode(dentry
->d_inode
);
789 char *attr
= (char *)name
;
794 namesp
= attr_lookup_namespace(attr
, attr_namespaces
, ATTR_NAMECOUNT
);
797 attr
+= namesp
->attr_namelen
;
798 error
= namesp
->attr_capable(vp
, NULL
);
801 xflags
|= namesp
->attr_flag
;
802 return namesp
->attr_remove(vp
, attr
, xflags
);
806 struct inode_operations xfs_inode_operations
= {
807 .permission
= xfs_vn_permission
,
808 .truncate
= xfs_vn_truncate
,
809 .getattr
= xfs_vn_getattr
,
810 .setattr
= xfs_vn_setattr
,
811 .setxattr
= xfs_vn_setxattr
,
812 .getxattr
= xfs_vn_getxattr
,
813 .listxattr
= xfs_vn_listxattr
,
814 .removexattr
= xfs_vn_removexattr
,
817 struct inode_operations xfs_dir_inode_operations
= {
818 .create
= xfs_vn_create
,
819 .lookup
= xfs_vn_lookup
,
821 .unlink
= xfs_vn_unlink
,
822 .symlink
= xfs_vn_symlink
,
823 .mkdir
= xfs_vn_mkdir
,
824 .rmdir
= xfs_vn_rmdir
,
825 .mknod
= xfs_vn_mknod
,
826 .rename
= xfs_vn_rename
,
827 .permission
= xfs_vn_permission
,
828 .getattr
= xfs_vn_getattr
,
829 .setattr
= xfs_vn_setattr
,
830 .setxattr
= xfs_vn_setxattr
,
831 .getxattr
= xfs_vn_getxattr
,
832 .listxattr
= xfs_vn_listxattr
,
833 .removexattr
= xfs_vn_removexattr
,
836 struct inode_operations xfs_symlink_inode_operations
= {
837 .readlink
= generic_readlink
,
838 .follow_link
= xfs_vn_follow_link
,
839 .put_link
= xfs_vn_put_link
,
840 .permission
= xfs_vn_permission
,
841 .getattr
= xfs_vn_getattr
,
842 .setattr
= xfs_vn_setattr
,
843 .setxattr
= xfs_vn_setxattr
,
844 .getxattr
= xfs_vn_getxattr
,
845 .listxattr
= xfs_vn_listxattr
,
846 .removexattr
= xfs_vn_removexattr
,