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"
49 #include "xfs_buf_item.h"
50 #include "xfs_utils.h"
52 #include <linux/capability.h>
53 #include <linux/xattr.h>
54 #include <linux/namei.h>
55 #include <linux/security.h>
58 * Get a XFS inode from a given vnode.
66 bdp
= bhv_lookup_range(VN_BHV_HEAD(vp
),
67 VNODE_POSITION_XFS
, VNODE_POSITION_XFS
);
68 if (unlikely(bdp
== NULL
))
70 return XFS_BHVTOI(bdp
);
74 * Bring the atime in the XFS inode uptodate.
75 * Used before logging the inode to disk or when the Linux inode goes away.
78 xfs_synchronize_atime(
83 vp
= XFS_ITOV_NULL(ip
);
85 struct inode
*inode
= &vp
->v_inode
;
86 ip
->i_d
.di_atime
.t_sec
= (__int32_t
)inode
->i_atime
.tv_sec
;
87 ip
->i_d
.di_atime
.t_nsec
= (__int32_t
)inode
->i_atime
.tv_nsec
;
92 * Change the requested timestamp in the given inode.
93 * We don't lock across timestamp updates, and we don't log them but
94 * we do record the fact that there is dirty information in core.
96 * NOTE -- callers MUST combine XFS_ICHGTIME_MOD or XFS_ICHGTIME_CHG
97 * with XFS_ICHGTIME_ACC to be sure that access time
98 * update will take. Calling first with XFS_ICHGTIME_ACC
99 * and then XFS_ICHGTIME_MOD may fail to modify the access
100 * timestamp if the filesystem is mounted noacctm.
107 struct inode
*inode
= vn_to_inode(XFS_ITOV(ip
));
111 if (flags
& XFS_ICHGTIME_MOD
) {
113 ip
->i_d
.di_mtime
.t_sec
= (__int32_t
)tv
.tv_sec
;
114 ip
->i_d
.di_mtime
.t_nsec
= (__int32_t
)tv
.tv_nsec
;
116 if (flags
& XFS_ICHGTIME_ACC
) {
118 ip
->i_d
.di_atime
.t_sec
= (__int32_t
)tv
.tv_sec
;
119 ip
->i_d
.di_atime
.t_nsec
= (__int32_t
)tv
.tv_nsec
;
121 if (flags
& XFS_ICHGTIME_CHG
) {
123 ip
->i_d
.di_ctime
.t_sec
= (__int32_t
)tv
.tv_sec
;
124 ip
->i_d
.di_ctime
.t_nsec
= (__int32_t
)tv
.tv_nsec
;
128 * We update the i_update_core field _after_ changing
129 * the timestamps in order to coordinate properly with
130 * xfs_iflush() so that we don't lose timestamp updates.
131 * This keeps us from having to hold the inode lock
132 * while doing this. We use the SYNCHRONIZE macro to
133 * ensure that the compiler does not reorder the update
134 * of i_update_core above the timestamp updates above.
137 ip
->i_update_core
= 1;
138 if (!(inode
->i_state
& I_LOCK
))
139 mark_inode_dirty_sync(inode
);
143 * Variant on the above which avoids querying the system clock
144 * in situations where we know the Linux inode timestamps have
145 * just been updated (and so we can update our inode cheaply).
156 * Atime updates for read() & friends are handled lazily now, and
157 * explicit updates must go through xfs_ichgtime()
159 ASSERT((flags
& XFS_ICHGTIME_ACC
) == 0);
162 * We're not supposed to change timestamps in readonly-mounted
163 * filesystems. Throw it away if anyone asks us.
165 if (unlikely(IS_RDONLY(inode
)))
168 if (flags
& XFS_ICHGTIME_MOD
) {
169 tvp
= &inode
->i_mtime
;
170 ip
->i_d
.di_mtime
.t_sec
= (__int32_t
)tvp
->tv_sec
;
171 ip
->i_d
.di_mtime
.t_nsec
= (__int32_t
)tvp
->tv_nsec
;
173 if (flags
& XFS_ICHGTIME_CHG
) {
174 tvp
= &inode
->i_ctime
;
175 ip
->i_d
.di_ctime
.t_sec
= (__int32_t
)tvp
->tv_sec
;
176 ip
->i_d
.di_ctime
.t_nsec
= (__int32_t
)tvp
->tv_nsec
;
180 * We update the i_update_core field _after_ changing
181 * the timestamps in order to coordinate properly with
182 * xfs_iflush() so that we don't lose timestamp updates.
183 * This keeps us from having to hold the inode lock
184 * while doing this. We use the SYNCHRONIZE macro to
185 * ensure that the compiler does not reorder the update
186 * of i_update_core above the timestamp updates above.
189 ip
->i_update_core
= 1;
190 if (!(inode
->i_state
& I_LOCK
))
191 mark_inode_dirty_sync(inode
);
196 * Pull the link count and size up from the xfs inode to the linux inode
203 vattr
->va_mask
= XFS_AT_NLINK
|XFS_AT_SIZE
|XFS_AT_NBLOCKS
;
204 if (!bhv_vop_getattr(vn_from_inode(ip
), vattr
, ATTR_LAZY
, NULL
)) {
205 ip
->i_nlink
= vattr
->va_nlink
;
206 ip
->i_blocks
= vattr
->va_nblocks
;
208 /* we're under i_sem so i_size can't change under us */
209 if (i_size_read(ip
) != vattr
->va_size
)
210 i_size_write(ip
, vattr
->va_size
);
215 * Hook in SELinux. This is not quite correct yet, what we really need
216 * here (as we do for default ACLs) is a mechanism by which creation of
217 * these attrs can be journalled at inode creation time (along with the
218 * inode, of course, such that log replay can't cause these to be lost).
225 struct inode
*ip
= vn_to_inode(vp
);
231 error
= security_inode_init_security(ip
, dir
, &name
, &value
, &length
);
233 if (error
== -EOPNOTSUPP
)
238 error
= bhv_vop_attr_set(vp
, name
, value
, length
, ATTR_SECURE
, NULL
);
248 * Determine whether a process has a valid fs_struct (kernel daemons
249 * like knfsd don't have an fs_struct).
251 * XXX(hch): nfsd is broken, better fix it instead.
254 xfs_has_fs_struct(struct task_struct
*task
)
256 return (task
->fs
!= init_task
.fs
);
263 struct dentry
*dentry
,
266 struct dentry teardown
= {};
269 * If we can't add the ACL or we fail in
270 * xfs_init_security we must back out.
271 * ENOSPC can hit here, among other things.
273 teardown
.d_inode
= vn_to_inode(vp
);
274 teardown
.d_name
= dentry
->d_name
;
277 bhv_vop_rmdir(dvp
, &teardown
, NULL
);
279 bhv_vop_remove(dvp
, &teardown
, NULL
);
286 struct dentry
*dentry
,
291 bhv_vattr_t vattr
= { 0 };
292 bhv_vnode_t
*vp
= NULL
, *dvp
= vn_from_inode(dir
);
293 xfs_acl_t
*default_acl
= NULL
;
294 attrexists_t test_default_acl
= _ACL_DEFAULT_EXISTS
;
298 * Irix uses Missed'em'V split, but doesn't want to see
299 * the upper 5 bits of (14bit) major.
301 if (unlikely(!sysv_valid_dev(rdev
) || MAJOR(rdev
) & ~0x1ff))
304 if (unlikely(test_default_acl
&& test_default_acl(dvp
))) {
305 if (!_ACL_ALLOC(default_acl
)) {
308 if (!_ACL_GET_DEFAULT(dvp
, default_acl
)) {
309 _ACL_FREE(default_acl
);
314 if (IS_POSIXACL(dir
) && !default_acl
&& xfs_has_fs_struct(current
))
315 mode
&= ~current
->fs
->umask
;
317 vattr
.va_mask
= XFS_AT_TYPE
|XFS_AT_MODE
;
318 vattr
.va_mode
= mode
;
320 switch (mode
& S_IFMT
) {
321 case S_IFCHR
: case S_IFBLK
: case S_IFIFO
: case S_IFSOCK
:
322 vattr
.va_rdev
= sysv_encode_dev(rdev
);
323 vattr
.va_mask
|= XFS_AT_RDEV
;
326 error
= bhv_vop_create(dvp
, dentry
, &vattr
, &vp
, NULL
);
329 error
= bhv_vop_mkdir(dvp
, dentry
, &vattr
, &vp
, NULL
);
336 if (unlikely(!error
)) {
337 error
= xfs_init_security(vp
, dir
);
339 xfs_cleanup_inode(dvp
, vp
, dentry
, mode
);
342 if (unlikely(default_acl
)) {
344 error
= _ACL_INHERIT(vp
, &vattr
, default_acl
);
348 xfs_cleanup_inode(dvp
, vp
, dentry
, mode
);
350 _ACL_FREE(default_acl
);
353 if (likely(!error
)) {
355 ip
= vn_to_inode(vp
);
357 if (S_ISCHR(mode
) || S_ISBLK(mode
))
359 else if (S_ISDIR(mode
))
360 xfs_validate_fields(ip
, &vattr
);
361 d_instantiate(dentry
, ip
);
362 xfs_validate_fields(dir
, &vattr
);
370 struct dentry
*dentry
,
372 struct nameidata
*nd
)
374 return xfs_vn_mknod(dir
, dentry
, mode
, 0);
380 struct dentry
*dentry
,
383 return xfs_vn_mknod(dir
, dentry
, mode
|S_IFDIR
, 0);
386 STATIC
struct dentry
*
389 struct dentry
*dentry
,
390 struct nameidata
*nd
)
392 bhv_vnode_t
*vp
= vn_from_inode(dir
), *cvp
;
395 if (dentry
->d_name
.len
>= MAXNAMELEN
)
396 return ERR_PTR(-ENAMETOOLONG
);
398 error
= bhv_vop_lookup(vp
, dentry
, &cvp
, 0, NULL
, NULL
);
399 if (unlikely(error
)) {
400 if (unlikely(error
!= ENOENT
))
401 return ERR_PTR(-error
);
406 return d_splice_alias(vn_to_inode(cvp
), dentry
);
411 struct dentry
*old_dentry
,
413 struct dentry
*dentry
)
415 struct inode
*ip
; /* inode of guy being linked to */
416 bhv_vnode_t
*tdvp
; /* target directory for new name/link */
417 bhv_vnode_t
*vp
; /* vp of name being linked */
421 ip
= old_dentry
->d_inode
; /* inode being linked to */
422 tdvp
= vn_from_inode(dir
);
423 vp
= vn_from_inode(ip
);
426 error
= bhv_vop_link(tdvp
, vp
, dentry
, NULL
);
427 if (unlikely(error
)) {
431 xfs_validate_fields(ip
, &vattr
);
432 d_instantiate(dentry
, ip
);
440 struct dentry
*dentry
)
443 bhv_vnode_t
*dvp
; /* directory containing name to remove */
447 inode
= dentry
->d_inode
;
448 dvp
= vn_from_inode(dir
);
450 error
= bhv_vop_remove(dvp
, dentry
, NULL
);
451 if (likely(!error
)) {
452 xfs_validate_fields(dir
, &vattr
); /* size needs update */
453 xfs_validate_fields(inode
, &vattr
);
461 struct dentry
*dentry
,
465 bhv_vattr_t va
= { 0 };
466 bhv_vnode_t
*dvp
; /* directory containing name of symlink */
467 bhv_vnode_t
*cvp
; /* used to lookup symlink to put in dentry */
470 dvp
= vn_from_inode(dir
);
473 va
.va_mode
= S_IFLNK
|
474 (irix_symlink_mode
? 0777 & ~current
->fs
->umask
: S_IRWXUGO
);
475 va
.va_mask
= XFS_AT_TYPE
|XFS_AT_MODE
;
477 error
= bhv_vop_symlink(dvp
, dentry
, &va
, (char *)symname
, &cvp
, NULL
);
478 if (likely(!error
&& cvp
)) {
479 error
= xfs_init_security(cvp
, dir
);
480 if (likely(!error
)) {
481 ip
= vn_to_inode(cvp
);
482 d_instantiate(dentry
, ip
);
483 xfs_validate_fields(dir
, &va
);
484 xfs_validate_fields(ip
, &va
);
486 xfs_cleanup_inode(dvp
, cvp
, dentry
, 0);
495 struct dentry
*dentry
)
497 struct inode
*inode
= dentry
->d_inode
;
498 bhv_vnode_t
*dvp
= vn_from_inode(dir
);
502 error
= bhv_vop_rmdir(dvp
, dentry
, NULL
);
503 if (likely(!error
)) {
504 xfs_validate_fields(inode
, &vattr
);
505 xfs_validate_fields(dir
, &vattr
);
513 struct dentry
*odentry
,
515 struct dentry
*ndentry
)
517 struct inode
*new_inode
= ndentry
->d_inode
;
518 bhv_vnode_t
*fvp
; /* from directory */
519 bhv_vnode_t
*tvp
; /* target directory */
523 fvp
= vn_from_inode(odir
);
524 tvp
= vn_from_inode(ndir
);
526 error
= bhv_vop_rename(fvp
, odentry
, tvp
, ndentry
, NULL
);
527 if (likely(!error
)) {
529 xfs_validate_fields(new_inode
, &vattr
);
530 xfs_validate_fields(odir
, &vattr
);
532 xfs_validate_fields(ndir
, &vattr
);
538 * careful here - this function can get called recursively, so
539 * we need to be very careful about how much stack we use.
540 * uio is kmalloced for this reason...
544 struct dentry
*dentry
,
545 struct nameidata
*nd
)
556 link
= (char *)kmalloc(MAXPATHLEN
+1, GFP_KERNEL
);
558 nd_set_link(nd
, ERR_PTR(-ENOMEM
));
562 uio
= (uio_t
*)kmalloc(sizeof(uio_t
), GFP_KERNEL
);
565 nd_set_link(nd
, ERR_PTR(-ENOMEM
));
569 vp
= vn_from_inode(dentry
->d_inode
);
572 iov
.iov_len
= MAXPATHLEN
;
576 uio
->uio_segflg
= UIO_SYSSPACE
;
577 uio
->uio_resid
= MAXPATHLEN
;
580 error
= bhv_vop_readlink(vp
, uio
, 0, NULL
);
581 if (unlikely(error
)) {
583 link
= ERR_PTR(-error
);
585 link
[MAXPATHLEN
- uio
->uio_resid
] = '\0';
589 nd_set_link(nd
, link
);
595 struct dentry
*dentry
,
596 struct nameidata
*nd
,
599 char *s
= nd_get_link(nd
);
605 #ifdef CONFIG_XFS_POSIX_ACL
610 struct nameidata
*nd
)
612 return -bhv_vop_access(vn_from_inode(inode
), mode
<< 6, NULL
);
615 #define xfs_vn_permission NULL
620 struct vfsmount
*mnt
,
621 struct dentry
*dentry
,
624 struct inode
*inode
= dentry
->d_inode
;
625 bhv_vnode_t
*vp
= vn_from_inode(inode
);
628 if (unlikely(vp
->v_flag
& VMODIFIED
))
629 error
= vn_revalidate(vp
);
631 generic_fillattr(inode
, stat
);
637 struct dentry
*dentry
,
640 struct inode
*inode
= dentry
->d_inode
;
641 unsigned int ia_valid
= attr
->ia_valid
;
642 bhv_vnode_t
*vp
= vn_from_inode(inode
);
643 bhv_vattr_t vattr
= { 0 };
647 if (ia_valid
& ATTR_UID
) {
648 vattr
.va_mask
|= XFS_AT_UID
;
649 vattr
.va_uid
= attr
->ia_uid
;
651 if (ia_valid
& ATTR_GID
) {
652 vattr
.va_mask
|= XFS_AT_GID
;
653 vattr
.va_gid
= attr
->ia_gid
;
655 if (ia_valid
& ATTR_SIZE
) {
656 vattr
.va_mask
|= XFS_AT_SIZE
;
657 vattr
.va_size
= attr
->ia_size
;
659 if (ia_valid
& ATTR_ATIME
) {
660 vattr
.va_mask
|= XFS_AT_ATIME
;
661 vattr
.va_atime
= attr
->ia_atime
;
662 inode
->i_atime
= attr
->ia_atime
;
664 if (ia_valid
& ATTR_MTIME
) {
665 vattr
.va_mask
|= XFS_AT_MTIME
;
666 vattr
.va_mtime
= attr
->ia_mtime
;
668 if (ia_valid
& ATTR_CTIME
) {
669 vattr
.va_mask
|= XFS_AT_CTIME
;
670 vattr
.va_ctime
= attr
->ia_ctime
;
672 if (ia_valid
& ATTR_MODE
) {
673 vattr
.va_mask
|= XFS_AT_MODE
;
674 vattr
.va_mode
= attr
->ia_mode
;
675 if (!in_group_p(inode
->i_gid
) && !capable(CAP_FSETID
))
676 inode
->i_mode
&= ~S_ISGID
;
679 if (ia_valid
& (ATTR_MTIME_SET
| ATTR_ATIME_SET
))
682 if ((ia_valid
& ATTR_NO_BLOCK
))
683 flags
|= ATTR_NONBLOCK
;
686 error
= bhv_vop_setattr(vp
, &vattr
, flags
, NULL
);
688 __vn_revalidate(vp
, &vattr
);
696 block_truncate_page(inode
->i_mapping
, inode
->i_size
, xfs_get_blocks
);
701 struct dentry
*dentry
,
707 bhv_vnode_t
*vp
= vn_from_inode(dentry
->d_inode
);
708 char *attr
= (char *)name
;
713 namesp
= attr_lookup_namespace(attr
, attr_namespaces
, ATTR_NAMECOUNT
);
716 attr
+= namesp
->attr_namelen
;
717 error
= namesp
->attr_capable(vp
, NULL
);
721 /* Convert Linux syscall to XFS internal ATTR flags */
722 if (flags
& XATTR_CREATE
)
723 xflags
|= ATTR_CREATE
;
724 if (flags
& XATTR_REPLACE
)
725 xflags
|= ATTR_REPLACE
;
726 xflags
|= namesp
->attr_flag
;
727 return namesp
->attr_set(vp
, attr
, (void *)data
, size
, xflags
);
732 struct dentry
*dentry
,
737 bhv_vnode_t
*vp
= vn_from_inode(dentry
->d_inode
);
738 char *attr
= (char *)name
;
743 namesp
= attr_lookup_namespace(attr
, attr_namespaces
, ATTR_NAMECOUNT
);
746 attr
+= namesp
->attr_namelen
;
747 error
= namesp
->attr_capable(vp
, NULL
);
751 /* Convert Linux syscall to XFS internal ATTR flags */
753 xflags
|= ATTR_KERNOVAL
;
756 xflags
|= namesp
->attr_flag
;
757 return namesp
->attr_get(vp
, attr
, (void *)data
, size
, xflags
);
762 struct dentry
*dentry
,
766 bhv_vnode_t
*vp
= vn_from_inode(dentry
->d_inode
);
767 int error
, xflags
= ATTR_KERNAMELS
;
771 xflags
|= ATTR_KERNOVAL
;
772 xflags
|= capable(CAP_SYS_ADMIN
) ? ATTR_KERNFULLS
: ATTR_KERNORMALS
;
774 error
= attr_generic_list(vp
, data
, size
, xflags
, &result
);
782 struct dentry
*dentry
,
785 bhv_vnode_t
*vp
= vn_from_inode(dentry
->d_inode
);
786 char *attr
= (char *)name
;
791 namesp
= attr_lookup_namespace(attr
, attr_namespaces
, ATTR_NAMECOUNT
);
794 attr
+= namesp
->attr_namelen
;
795 error
= namesp
->attr_capable(vp
, NULL
);
798 xflags
|= namesp
->attr_flag
;
799 return namesp
->attr_remove(vp
, attr
, xflags
);
803 struct inode_operations xfs_inode_operations
= {
804 .permission
= xfs_vn_permission
,
805 .truncate
= xfs_vn_truncate
,
806 .getattr
= xfs_vn_getattr
,
807 .setattr
= xfs_vn_setattr
,
808 .setxattr
= xfs_vn_setxattr
,
809 .getxattr
= xfs_vn_getxattr
,
810 .listxattr
= xfs_vn_listxattr
,
811 .removexattr
= xfs_vn_removexattr
,
814 struct inode_operations xfs_dir_inode_operations
= {
815 .create
= xfs_vn_create
,
816 .lookup
= xfs_vn_lookup
,
818 .unlink
= xfs_vn_unlink
,
819 .symlink
= xfs_vn_symlink
,
820 .mkdir
= xfs_vn_mkdir
,
821 .rmdir
= xfs_vn_rmdir
,
822 .mknod
= xfs_vn_mknod
,
823 .rename
= xfs_vn_rename
,
824 .permission
= xfs_vn_permission
,
825 .getattr
= xfs_vn_getattr
,
826 .setattr
= xfs_vn_setattr
,
827 .setxattr
= xfs_vn_setxattr
,
828 .getxattr
= xfs_vn_getxattr
,
829 .listxattr
= xfs_vn_listxattr
,
830 .removexattr
= xfs_vn_removexattr
,
833 struct inode_operations xfs_symlink_inode_operations
= {
834 .readlink
= generic_readlink
,
835 .follow_link
= xfs_vn_follow_link
,
836 .put_link
= xfs_vn_put_link
,
837 .permission
= xfs_vn_permission
,
838 .getattr
= xfs_vn_getattr
,
839 .setattr
= xfs_vn_setattr
,
840 .setxattr
= xfs_vn_setxattr
,
841 .getxattr
= xfs_vn_getxattr
,
842 .listxattr
= xfs_vn_listxattr
,
843 .removexattr
= xfs_vn_removexattr
,