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"
50 #include <linux/capability.h>
51 #include <linux/xattr.h>
52 #include <linux/namei.h>
53 #include <linux/security.h>
56 * Get a XFS inode from a given vnode.
64 bdp
= bhv_lookup_range(VN_BHV_HEAD(vp
),
65 VNODE_POSITION_XFS
, VNODE_POSITION_XFS
);
66 if (unlikely(bdp
== NULL
))
68 return XFS_BHVTOI(bdp
);
72 * Bring the atime in the XFS inode uptodate.
73 * Used before logging the inode to disk or when the Linux inode goes away.
76 xfs_synchronize_atime(
81 vp
= XFS_ITOV_NULL(ip
);
83 struct inode
*inode
= &vp
->v_inode
;
84 ip
->i_d
.di_atime
.t_sec
= (__int32_t
)inode
->i_atime
.tv_sec
;
85 ip
->i_d
.di_atime
.t_nsec
= (__int32_t
)inode
->i_atime
.tv_nsec
;
90 * Change the requested timestamp in the given inode.
91 * We don't lock across timestamp updates, and we don't log them but
92 * we do record the fact that there is dirty information in core.
94 * NOTE -- callers MUST combine XFS_ICHGTIME_MOD or XFS_ICHGTIME_CHG
95 * with XFS_ICHGTIME_ACC to be sure that access time
96 * update will take. Calling first with XFS_ICHGTIME_ACC
97 * and then XFS_ICHGTIME_MOD may fail to modify the access
98 * timestamp if the filesystem is mounted noacctm.
105 struct inode
*inode
= vn_to_inode(XFS_ITOV(ip
));
109 if (flags
& XFS_ICHGTIME_MOD
) {
111 ip
->i_d
.di_mtime
.t_sec
= (__int32_t
)tv
.tv_sec
;
112 ip
->i_d
.di_mtime
.t_nsec
= (__int32_t
)tv
.tv_nsec
;
114 if (flags
& XFS_ICHGTIME_ACC
) {
116 ip
->i_d
.di_atime
.t_sec
= (__int32_t
)tv
.tv_sec
;
117 ip
->i_d
.di_atime
.t_nsec
= (__int32_t
)tv
.tv_nsec
;
119 if (flags
& XFS_ICHGTIME_CHG
) {
121 ip
->i_d
.di_ctime
.t_sec
= (__int32_t
)tv
.tv_sec
;
122 ip
->i_d
.di_ctime
.t_nsec
= (__int32_t
)tv
.tv_nsec
;
126 * We update the i_update_core field _after_ changing
127 * the timestamps in order to coordinate properly with
128 * xfs_iflush() so that we don't lose timestamp updates.
129 * This keeps us from having to hold the inode lock
130 * while doing this. We use the SYNCHRONIZE macro to
131 * ensure that the compiler does not reorder the update
132 * of i_update_core above the timestamp updates above.
135 ip
->i_update_core
= 1;
136 if (!(inode
->i_state
& I_LOCK
))
137 mark_inode_dirty_sync(inode
);
141 * Variant on the above which avoids querying the system clock
142 * in situations where we know the Linux inode timestamps have
143 * just been updated (and so we can update our inode cheaply).
154 * Atime updates for read() & friends are handled lazily now, and
155 * explicit updates must go through xfs_ichgtime()
157 ASSERT((flags
& XFS_ICHGTIME_ACC
) == 0);
160 * We're not supposed to change timestamps in readonly-mounted
161 * filesystems. Throw it away if anyone asks us.
163 if (unlikely(IS_RDONLY(inode
)))
166 if (flags
& XFS_ICHGTIME_MOD
) {
167 tvp
= &inode
->i_mtime
;
168 ip
->i_d
.di_mtime
.t_sec
= (__int32_t
)tvp
->tv_sec
;
169 ip
->i_d
.di_mtime
.t_nsec
= (__int32_t
)tvp
->tv_nsec
;
171 if (flags
& XFS_ICHGTIME_CHG
) {
172 tvp
= &inode
->i_ctime
;
173 ip
->i_d
.di_ctime
.t_sec
= (__int32_t
)tvp
->tv_sec
;
174 ip
->i_d
.di_ctime
.t_nsec
= (__int32_t
)tvp
->tv_nsec
;
178 * We update the i_update_core field _after_ changing
179 * the timestamps in order to coordinate properly with
180 * xfs_iflush() so that we don't lose timestamp updates.
181 * This keeps us from having to hold the inode lock
182 * while doing this. We use the SYNCHRONIZE macro to
183 * ensure that the compiler does not reorder the update
184 * of i_update_core above the timestamp updates above.
187 ip
->i_update_core
= 1;
188 if (!(inode
->i_state
& I_LOCK
))
189 mark_inode_dirty_sync(inode
);
194 * Pull the link count and size up from the xfs inode to the linux inode
201 vattr
->va_mask
= XFS_AT_NLINK
|XFS_AT_SIZE
|XFS_AT_NBLOCKS
;
202 if (!bhv_vop_getattr(vn_from_inode(ip
), vattr
, ATTR_LAZY
, NULL
)) {
203 ip
->i_nlink
= vattr
->va_nlink
;
204 ip
->i_blocks
= vattr
->va_nblocks
;
206 /* we're under i_sem so i_size can't change under us */
207 if (i_size_read(ip
) != vattr
->va_size
)
208 i_size_write(ip
, vattr
->va_size
);
213 * Hook in SELinux. This is not quite correct yet, what we really need
214 * here (as we do for default ACLs) is a mechanism by which creation of
215 * these attrs can be journalled at inode creation time (along with the
216 * inode, of course, such that log replay can't cause these to be lost).
223 struct inode
*ip
= vn_to_inode(vp
);
229 error
= security_inode_init_security(ip
, dir
, &name
, &value
, &length
);
231 if (error
== -EOPNOTSUPP
)
236 error
= bhv_vop_attr_set(vp
, name
, value
, length
, ATTR_SECURE
, NULL
);
246 * Determine whether a process has a valid fs_struct (kernel daemons
247 * like knfsd don't have an fs_struct).
249 * XXX(hch): nfsd is broken, better fix it instead.
252 xfs_has_fs_struct(struct task_struct
*task
)
254 return (task
->fs
!= init_task
.fs
);
261 struct dentry
*dentry
,
264 struct dentry teardown
= {};
267 * If we can't add the ACL or we fail in
268 * xfs_init_security we must back out.
269 * ENOSPC can hit here, among other things.
271 teardown
.d_inode
= vn_to_inode(vp
);
272 teardown
.d_name
= dentry
->d_name
;
275 bhv_vop_rmdir(dvp
, &teardown
, NULL
);
277 bhv_vop_remove(dvp
, &teardown
, NULL
);
284 struct dentry
*dentry
,
289 bhv_vattr_t vattr
= { 0 };
290 bhv_vnode_t
*vp
= NULL
, *dvp
= vn_from_inode(dir
);
291 xfs_acl_t
*default_acl
= NULL
;
292 attrexists_t test_default_acl
= _ACL_DEFAULT_EXISTS
;
296 * Irix uses Missed'em'V split, but doesn't want to see
297 * the upper 5 bits of (14bit) major.
299 if (unlikely(!sysv_valid_dev(rdev
) || MAJOR(rdev
) & ~0x1ff))
302 if (unlikely(test_default_acl
&& test_default_acl(dvp
))) {
303 if (!_ACL_ALLOC(default_acl
)) {
306 if (!_ACL_GET_DEFAULT(dvp
, default_acl
)) {
307 _ACL_FREE(default_acl
);
312 if (IS_POSIXACL(dir
) && !default_acl
&& xfs_has_fs_struct(current
))
313 mode
&= ~current
->fs
->umask
;
315 vattr
.va_mask
= XFS_AT_TYPE
|XFS_AT_MODE
;
316 vattr
.va_mode
= mode
;
318 switch (mode
& S_IFMT
) {
319 case S_IFCHR
: case S_IFBLK
: case S_IFIFO
: case S_IFSOCK
:
320 vattr
.va_rdev
= sysv_encode_dev(rdev
);
321 vattr
.va_mask
|= XFS_AT_RDEV
;
324 error
= bhv_vop_create(dvp
, dentry
, &vattr
, &vp
, NULL
);
327 error
= bhv_vop_mkdir(dvp
, dentry
, &vattr
, &vp
, NULL
);
334 if (unlikely(!error
)) {
335 error
= xfs_init_security(vp
, dir
);
337 xfs_cleanup_inode(dvp
, vp
, dentry
, mode
);
340 if (unlikely(default_acl
)) {
342 error
= _ACL_INHERIT(vp
, &vattr
, default_acl
);
346 xfs_cleanup_inode(dvp
, vp
, dentry
, mode
);
348 _ACL_FREE(default_acl
);
351 if (likely(!error
)) {
353 ip
= vn_to_inode(vp
);
355 if (S_ISCHR(mode
) || S_ISBLK(mode
))
357 else if (S_ISDIR(mode
))
358 xfs_validate_fields(ip
, &vattr
);
359 d_instantiate(dentry
, ip
);
360 xfs_validate_fields(dir
, &vattr
);
368 struct dentry
*dentry
,
370 struct nameidata
*nd
)
372 return xfs_vn_mknod(dir
, dentry
, mode
, 0);
378 struct dentry
*dentry
,
381 return xfs_vn_mknod(dir
, dentry
, mode
|S_IFDIR
, 0);
384 STATIC
struct dentry
*
387 struct dentry
*dentry
,
388 struct nameidata
*nd
)
390 bhv_vnode_t
*vp
= vn_from_inode(dir
), *cvp
;
393 if (dentry
->d_name
.len
>= MAXNAMELEN
)
394 return ERR_PTR(-ENAMETOOLONG
);
396 error
= bhv_vop_lookup(vp
, dentry
, &cvp
, 0, NULL
, NULL
);
397 if (unlikely(error
)) {
398 if (unlikely(error
!= ENOENT
))
399 return ERR_PTR(-error
);
404 return d_splice_alias(vn_to_inode(cvp
), dentry
);
409 struct dentry
*old_dentry
,
411 struct dentry
*dentry
)
413 struct inode
*ip
; /* inode of guy being linked to */
414 bhv_vnode_t
*tdvp
; /* target directory for new name/link */
415 bhv_vnode_t
*vp
; /* vp of name being linked */
419 ip
= old_dentry
->d_inode
; /* inode being linked to */
420 tdvp
= vn_from_inode(dir
);
421 vp
= vn_from_inode(ip
);
424 error
= bhv_vop_link(tdvp
, vp
, dentry
, NULL
);
425 if (unlikely(error
)) {
429 xfs_validate_fields(ip
, &vattr
);
430 d_instantiate(dentry
, ip
);
438 struct dentry
*dentry
)
441 bhv_vnode_t
*dvp
; /* directory containing name to remove */
445 inode
= dentry
->d_inode
;
446 dvp
= vn_from_inode(dir
);
448 error
= bhv_vop_remove(dvp
, dentry
, NULL
);
449 if (likely(!error
)) {
450 xfs_validate_fields(dir
, &vattr
); /* size needs update */
451 xfs_validate_fields(inode
, &vattr
);
459 struct dentry
*dentry
,
463 bhv_vattr_t va
= { 0 };
464 bhv_vnode_t
*dvp
; /* directory containing name of symlink */
465 bhv_vnode_t
*cvp
; /* used to lookup symlink to put in dentry */
468 dvp
= vn_from_inode(dir
);
471 va
.va_mode
= S_IFLNK
|
472 (irix_symlink_mode
? 0777 & ~current
->fs
->umask
: S_IRWXUGO
);
473 va
.va_mask
= XFS_AT_TYPE
|XFS_AT_MODE
;
475 error
= bhv_vop_symlink(dvp
, dentry
, &va
, (char *)symname
, &cvp
, NULL
);
476 if (likely(!error
&& cvp
)) {
477 error
= xfs_init_security(cvp
, dir
);
478 if (likely(!error
)) {
479 ip
= vn_to_inode(cvp
);
480 d_instantiate(dentry
, ip
);
481 xfs_validate_fields(dir
, &va
);
482 xfs_validate_fields(ip
, &va
);
484 xfs_cleanup_inode(dvp
, cvp
, dentry
, 0);
493 struct dentry
*dentry
)
495 struct inode
*inode
= dentry
->d_inode
;
496 bhv_vnode_t
*dvp
= vn_from_inode(dir
);
500 error
= bhv_vop_rmdir(dvp
, dentry
, NULL
);
501 if (likely(!error
)) {
502 xfs_validate_fields(inode
, &vattr
);
503 xfs_validate_fields(dir
, &vattr
);
511 struct dentry
*odentry
,
513 struct dentry
*ndentry
)
515 struct inode
*new_inode
= ndentry
->d_inode
;
516 bhv_vnode_t
*fvp
; /* from directory */
517 bhv_vnode_t
*tvp
; /* target directory */
521 fvp
= vn_from_inode(odir
);
522 tvp
= vn_from_inode(ndir
);
524 error
= bhv_vop_rename(fvp
, odentry
, tvp
, ndentry
, NULL
);
525 if (likely(!error
)) {
527 xfs_validate_fields(new_inode
, &vattr
);
528 xfs_validate_fields(odir
, &vattr
);
530 xfs_validate_fields(ndir
, &vattr
);
536 * careful here - this function can get called recursively, so
537 * we need to be very careful about how much stack we use.
538 * uio is kmalloced for this reason...
542 struct dentry
*dentry
,
543 struct nameidata
*nd
)
554 link
= kmalloc(MAXPATHLEN
+1, GFP_KERNEL
);
556 nd_set_link(nd
, ERR_PTR(-ENOMEM
));
560 uio
= kmalloc(sizeof(uio_t
), GFP_KERNEL
);
563 nd_set_link(nd
, ERR_PTR(-ENOMEM
));
567 vp
= vn_from_inode(dentry
->d_inode
);
570 iov
.iov_len
= MAXPATHLEN
;
574 uio
->uio_segflg
= UIO_SYSSPACE
;
575 uio
->uio_resid
= MAXPATHLEN
;
578 error
= bhv_vop_readlink(vp
, uio
, 0, NULL
);
579 if (unlikely(error
)) {
581 link
= ERR_PTR(-error
);
583 link
[MAXPATHLEN
- uio
->uio_resid
] = '\0';
587 nd_set_link(nd
, link
);
593 struct dentry
*dentry
,
594 struct nameidata
*nd
,
597 char *s
= nd_get_link(nd
);
603 #ifdef CONFIG_XFS_POSIX_ACL
608 struct nameidata
*nd
)
610 return -bhv_vop_access(vn_from_inode(inode
), mode
<< 6, NULL
);
613 #define xfs_vn_permission NULL
618 struct vfsmount
*mnt
,
619 struct dentry
*dentry
,
622 struct inode
*inode
= dentry
->d_inode
;
623 bhv_vnode_t
*vp
= vn_from_inode(inode
);
624 bhv_vattr_t vattr
= { .va_mask
= XFS_AT_STAT
};
627 error
= bhv_vop_getattr(vp
, &vattr
, ATTR_LAZY
, NULL
);
628 if (likely(!error
)) {
629 stat
->size
= i_size_read(inode
);
630 stat
->dev
= inode
->i_sb
->s_dev
;
631 stat
->rdev
= (vattr
.va_rdev
== 0) ? 0 :
632 MKDEV(sysv_major(vattr
.va_rdev
) & 0x1ff,
633 sysv_minor(vattr
.va_rdev
));
634 stat
->mode
= vattr
.va_mode
;
635 stat
->nlink
= vattr
.va_nlink
;
636 stat
->uid
= vattr
.va_uid
;
637 stat
->gid
= vattr
.va_gid
;
638 stat
->ino
= vattr
.va_nodeid
;
639 stat
->atime
= vattr
.va_atime
;
640 stat
->mtime
= vattr
.va_mtime
;
641 stat
->ctime
= vattr
.va_ctime
;
642 stat
->blocks
= vattr
.va_nblocks
;
643 stat
->blksize
= vattr
.va_blocksize
;
650 struct dentry
*dentry
,
653 struct inode
*inode
= dentry
->d_inode
;
654 unsigned int ia_valid
= attr
->ia_valid
;
655 bhv_vnode_t
*vp
= vn_from_inode(inode
);
656 bhv_vattr_t vattr
= { 0 };
660 if (ia_valid
& ATTR_UID
) {
661 vattr
.va_mask
|= XFS_AT_UID
;
662 vattr
.va_uid
= attr
->ia_uid
;
664 if (ia_valid
& ATTR_GID
) {
665 vattr
.va_mask
|= XFS_AT_GID
;
666 vattr
.va_gid
= attr
->ia_gid
;
668 if (ia_valid
& ATTR_SIZE
) {
669 vattr
.va_mask
|= XFS_AT_SIZE
;
670 vattr
.va_size
= attr
->ia_size
;
672 if (ia_valid
& ATTR_ATIME
) {
673 vattr
.va_mask
|= XFS_AT_ATIME
;
674 vattr
.va_atime
= attr
->ia_atime
;
675 inode
->i_atime
= attr
->ia_atime
;
677 if (ia_valid
& ATTR_MTIME
) {
678 vattr
.va_mask
|= XFS_AT_MTIME
;
679 vattr
.va_mtime
= attr
->ia_mtime
;
681 if (ia_valid
& ATTR_CTIME
) {
682 vattr
.va_mask
|= XFS_AT_CTIME
;
683 vattr
.va_ctime
= attr
->ia_ctime
;
685 if (ia_valid
& ATTR_MODE
) {
686 vattr
.va_mask
|= XFS_AT_MODE
;
687 vattr
.va_mode
= attr
->ia_mode
;
688 if (!in_group_p(inode
->i_gid
) && !capable(CAP_FSETID
))
689 inode
->i_mode
&= ~S_ISGID
;
692 if (ia_valid
& (ATTR_MTIME_SET
| ATTR_ATIME_SET
))
695 if ((ia_valid
& ATTR_NO_BLOCK
))
696 flags
|= ATTR_NONBLOCK
;
699 error
= bhv_vop_setattr(vp
, &vattr
, flags
, NULL
);
701 __vn_revalidate(vp
, &vattr
);
709 block_truncate_page(inode
->i_mapping
, inode
->i_size
, xfs_get_blocks
);
714 struct dentry
*dentry
,
720 bhv_vnode_t
*vp
= vn_from_inode(dentry
->d_inode
);
721 char *attr
= (char *)name
;
726 namesp
= attr_lookup_namespace(attr
, attr_namespaces
, ATTR_NAMECOUNT
);
729 attr
+= namesp
->attr_namelen
;
730 error
= namesp
->attr_capable(vp
, NULL
);
734 /* Convert Linux syscall to XFS internal ATTR flags */
735 if (flags
& XATTR_CREATE
)
736 xflags
|= ATTR_CREATE
;
737 if (flags
& XATTR_REPLACE
)
738 xflags
|= ATTR_REPLACE
;
739 xflags
|= namesp
->attr_flag
;
740 return namesp
->attr_set(vp
, attr
, (void *)data
, size
, xflags
);
745 struct dentry
*dentry
,
750 bhv_vnode_t
*vp
= vn_from_inode(dentry
->d_inode
);
751 char *attr
= (char *)name
;
756 namesp
= attr_lookup_namespace(attr
, attr_namespaces
, ATTR_NAMECOUNT
);
759 attr
+= namesp
->attr_namelen
;
760 error
= namesp
->attr_capable(vp
, NULL
);
764 /* Convert Linux syscall to XFS internal ATTR flags */
766 xflags
|= ATTR_KERNOVAL
;
769 xflags
|= namesp
->attr_flag
;
770 return namesp
->attr_get(vp
, attr
, (void *)data
, size
, xflags
);
775 struct dentry
*dentry
,
779 bhv_vnode_t
*vp
= vn_from_inode(dentry
->d_inode
);
780 int error
, xflags
= ATTR_KERNAMELS
;
784 xflags
|= ATTR_KERNOVAL
;
785 xflags
|= capable(CAP_SYS_ADMIN
) ? ATTR_KERNFULLS
: ATTR_KERNORMALS
;
787 error
= attr_generic_list(vp
, data
, size
, xflags
, &result
);
795 struct dentry
*dentry
,
798 bhv_vnode_t
*vp
= vn_from_inode(dentry
->d_inode
);
799 char *attr
= (char *)name
;
804 namesp
= attr_lookup_namespace(attr
, attr_namespaces
, ATTR_NAMECOUNT
);
807 attr
+= namesp
->attr_namelen
;
808 error
= namesp
->attr_capable(vp
, NULL
);
811 xflags
|= namesp
->attr_flag
;
812 return namesp
->attr_remove(vp
, attr
, xflags
);
816 const struct inode_operations xfs_inode_operations
= {
817 .permission
= xfs_vn_permission
,
818 .truncate
= xfs_vn_truncate
,
819 .getattr
= xfs_vn_getattr
,
820 .setattr
= xfs_vn_setattr
,
821 .setxattr
= xfs_vn_setxattr
,
822 .getxattr
= xfs_vn_getxattr
,
823 .listxattr
= xfs_vn_listxattr
,
824 .removexattr
= xfs_vn_removexattr
,
827 const struct inode_operations xfs_dir_inode_operations
= {
828 .create
= xfs_vn_create
,
829 .lookup
= xfs_vn_lookup
,
831 .unlink
= xfs_vn_unlink
,
832 .symlink
= xfs_vn_symlink
,
833 .mkdir
= xfs_vn_mkdir
,
834 .rmdir
= xfs_vn_rmdir
,
835 .mknod
= xfs_vn_mknod
,
836 .rename
= xfs_vn_rename
,
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
,
846 const struct inode_operations xfs_symlink_inode_operations
= {
847 .readlink
= generic_readlink
,
848 .follow_link
= xfs_vn_follow_link
,
849 .put_link
= xfs_vn_put_link
,
850 .permission
= xfs_vn_permission
,
851 .getattr
= xfs_vn_getattr
,
852 .setattr
= xfs_vn_setattr
,
853 .setxattr
= xfs_vn_setxattr
,
854 .getxattr
= xfs_vn_getxattr
,
855 .listxattr
= xfs_vn_listxattr
,
856 .removexattr
= xfs_vn_removexattr
,