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
= LINVFS_GET_IP(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
201 __linvfs_validate_fields(
205 vnode_t
*vp
= LINVFS_GET_VP(ip
);
208 vattr
->va_mask
= XFS_AT_NLINK
|XFS_AT_SIZE
|XFS_AT_NBLOCKS
;
209 VOP_GETATTR(vp
, vattr
, ATTR_LAZY
, NULL
, error
);
210 if (likely(!error
)) {
211 ip
->i_nlink
= vattr
->va_nlink
;
212 ip
->i_blocks
= vattr
->va_nblocks
;
214 /* we're under i_sem so i_size can't change under us */
215 if (i_size_read(ip
) != vattr
->va_size
)
216 i_size_write(ip
, vattr
->va_size
);
221 * Hook in SELinux. This is not quite correct yet, what we really need
222 * here (as we do for default ACLs) is a mechanism by which creation of
223 * these attrs can be journalled at inode creation time (along with the
224 * inode, of course, such that log replay can't cause these to be lost).
227 __linvfs_init_security(
231 struct inode
*ip
= LINVFS_GET_IP(vp
);
237 error
= security_inode_init_security(ip
, dir
, &name
, &value
, &length
);
239 if (error
== -EOPNOTSUPP
)
244 VOP_ATTR_SET(vp
, name
, value
, length
, ATTR_SECURE
, NULL
, error
);
254 * Determine whether a process has a valid fs_struct (kernel daemons
255 * like knfsd don't have an fs_struct).
257 * XXX(hch): nfsd is broken, better fix it instead.
260 __linvfs_has_fs_struct(struct task_struct
*task
)
262 return (task
->fs
!= init_task
.fs
);
266 __linvfs_cleanup_inode(
269 struct dentry
*dentry
,
272 struct dentry teardown
= {};
276 * If we can't add the ACL or we fail in
277 * linvfs_init_security we must back out.
278 * ENOSPC can hit here, among other things.
280 teardown
.d_inode
= LINVFS_GET_IP(vp
);
281 teardown
.d_name
= dentry
->d_name
;
284 VOP_RMDIR(dvp
, &teardown
, NULL
, error
);
286 VOP_REMOVE(dvp
, &teardown
, NULL
, error
);
293 struct dentry
*dentry
,
299 vnode_t
*vp
= NULL
, *dvp
= LINVFS_GET_VP(dir
);
300 xfs_acl_t
*default_acl
= NULL
;
301 attrexists_t test_default_acl
= _ACL_DEFAULT_EXISTS
;
305 * Irix uses Missed'em'V split, but doesn't want to see
306 * the upper 5 bits of (14bit) major.
308 if (unlikely(!sysv_valid_dev(rdev
) || MAJOR(rdev
) & ~0x1ff))
311 vattr
= kmalloc(sizeof(*vattr
), GFP_KERNEL
);
312 if (unlikely(!vattr
))
315 if (unlikely(test_default_acl
&& test_default_acl(dvp
))) {
316 if (!_ACL_ALLOC(default_acl
)) {
320 if (!_ACL_GET_DEFAULT(dvp
, default_acl
)) {
321 _ACL_FREE(default_acl
);
326 if (IS_POSIXACL(dir
) && !default_acl
&& __linvfs_has_fs_struct(current
))
327 mode
&= ~current
->fs
->umask
;
329 memset(vattr
, 0, sizeof(*vattr
));
330 vattr
->va_mask
= XFS_AT_TYPE
|XFS_AT_MODE
;
331 vattr
->va_mode
= mode
;
333 switch (mode
& S_IFMT
) {
334 case S_IFCHR
: case S_IFBLK
: case S_IFIFO
: case S_IFSOCK
:
335 vattr
->va_rdev
= sysv_encode_dev(rdev
);
336 vattr
->va_mask
|= XFS_AT_RDEV
;
339 VOP_CREATE(dvp
, dentry
, vattr
, &vp
, NULL
, error
);
342 VOP_MKDIR(dvp
, dentry
, vattr
, &vp
, NULL
, error
);
349 if (unlikely(!error
)) {
350 error
= __linvfs_init_security(vp
, dir
);
352 __linvfs_cleanup_inode(dvp
, vp
, dentry
, mode
);
355 if (unlikely(default_acl
)) {
357 error
= _ACL_INHERIT(vp
, vattr
, default_acl
);
361 __linvfs_cleanup_inode(dvp
, vp
, dentry
, mode
);
363 _ACL_FREE(default_acl
);
366 if (likely(!error
)) {
368 ip
= LINVFS_GET_IP(vp
);
370 if (S_ISCHR(mode
) || S_ISBLK(mode
))
372 else if (S_ISDIR(mode
))
373 __linvfs_validate_fields(ip
, vattr
);
374 d_instantiate(dentry
, ip
);
375 __linvfs_validate_fields(dir
, vattr
);
384 struct dentry
*dentry
,
386 struct nameidata
*nd
)
388 return linvfs_mknod(dir
, dentry
, mode
, 0);
394 struct dentry
*dentry
,
397 return linvfs_mknod(dir
, dentry
, mode
|S_IFDIR
, 0);
400 STATIC
struct dentry
*
403 struct dentry
*dentry
,
404 struct nameidata
*nd
)
406 struct vnode
*vp
= LINVFS_GET_VP(dir
), *cvp
;
409 if (dentry
->d_name
.len
>= MAXNAMELEN
)
410 return ERR_PTR(-ENAMETOOLONG
);
412 VOP_LOOKUP(vp
, dentry
, &cvp
, 0, NULL
, NULL
, error
);
414 if (unlikely(error
!= ENOENT
))
415 return ERR_PTR(-error
);
420 return d_splice_alias(LINVFS_GET_IP(cvp
), dentry
);
425 struct dentry
*old_dentry
,
427 struct dentry
*dentry
)
429 struct inode
*ip
; /* inode of guy being linked to */
430 vnode_t
*tdvp
; /* target directory for new name/link */
431 vnode_t
*vp
; /* vp of name being linked */
435 ip
= old_dentry
->d_inode
; /* inode being linked to */
436 if (S_ISDIR(ip
->i_mode
))
439 vattr
= kmalloc(sizeof(*vattr
), GFP_KERNEL
);
440 if (unlikely(!vattr
))
443 tdvp
= LINVFS_GET_VP(dir
);
444 vp
= LINVFS_GET_VP(ip
);
446 VOP_LINK(tdvp
, vp
, dentry
, NULL
, error
);
447 if (likely(!error
)) {
450 __linvfs_validate_fields(ip
, vattr
);
451 d_instantiate(dentry
, ip
);
460 struct dentry
*dentry
)
463 vnode_t
*dvp
; /* directory containing name to remove */
467 vattr
= kmalloc(sizeof(*vattr
), GFP_KERNEL
);
468 if (unlikely(!vattr
))
471 inode
= dentry
->d_inode
;
472 dvp
= LINVFS_GET_VP(dir
);
474 VOP_REMOVE(dvp
, dentry
, NULL
, error
);
475 if (likely(!error
)) {
476 __linvfs_validate_fields(dir
, vattr
); /* size needs update */
477 __linvfs_validate_fields(inode
, vattr
);
486 struct dentry
*dentry
,
491 vnode_t
*dvp
; /* directory containing name of symlink */
492 vnode_t
*cvp
; /* used to lookup symlink to put in dentry */
495 dvp
= LINVFS_GET_VP(dir
);
498 vattr
= kmalloc(sizeof(*vattr
), GFP_KERNEL
);
499 if (unlikely(!vattr
))
502 memset(vattr
, 0, sizeof(*vattr
));
503 vattr
->va_mode
= S_IFLNK
|
504 (irix_symlink_mode
? 0777 & ~current
->fs
->umask
: S_IRWXUGO
);
505 vattr
->va_mask
= XFS_AT_TYPE
|XFS_AT_MODE
;
508 VOP_SYMLINK(dvp
, dentry
, vattr
, (char *)symname
, &cvp
, NULL
, error
);
509 if (likely(!error
&& cvp
)) {
510 error
= __linvfs_init_security(cvp
, dir
);
511 if (likely(!error
)) {
512 ip
= LINVFS_GET_IP(cvp
);
513 d_instantiate(dentry
, ip
);
514 __linvfs_validate_fields(dir
, vattr
);
515 __linvfs_validate_fields(ip
, vattr
);
525 struct dentry
*dentry
)
527 struct inode
*inode
= dentry
->d_inode
;
528 vnode_t
*dvp
= LINVFS_GET_VP(dir
);
532 vattr
= kmalloc(sizeof(*vattr
), GFP_KERNEL
);
533 if (unlikely(!vattr
))
536 VOP_RMDIR(dvp
, dentry
, NULL
, error
);
537 if (likely(!error
)) {
538 __linvfs_validate_fields(inode
, vattr
);
539 __linvfs_validate_fields(dir
, vattr
);
548 struct dentry
*odentry
,
550 struct dentry
*ndentry
)
552 struct inode
*new_inode
= ndentry
->d_inode
;
553 vnode_t
*fvp
; /* from directory */
554 vnode_t
*tvp
; /* target directory */
558 vattr
= kmalloc(sizeof(*vattr
), GFP_KERNEL
);
559 if (unlikely(!vattr
))
562 fvp
= LINVFS_GET_VP(odir
);
563 tvp
= LINVFS_GET_VP(ndir
);
565 VOP_RENAME(fvp
, odentry
, tvp
, ndentry
, NULL
, error
);
566 if (likely(!error
)) {
568 __linvfs_validate_fields(new_inode
, vattr
);
569 __linvfs_validate_fields(odir
, vattr
);
571 __linvfs_validate_fields(ndir
, vattr
);
578 * careful here - this function can get called recursively, so
579 * we need to be very careful about how much stack we use.
580 * uio is kmalloced for this reason...
584 struct dentry
*dentry
,
585 struct nameidata
*nd
)
596 link
= (char *)kmalloc(MAXPATHLEN
+1, GFP_KERNEL
);
598 nd_set_link(nd
, ERR_PTR(-ENOMEM
));
602 uio
= (uio_t
*)kmalloc(sizeof(uio_t
), GFP_KERNEL
);
605 nd_set_link(nd
, ERR_PTR(-ENOMEM
));
609 vp
= LINVFS_GET_VP(dentry
->d_inode
);
612 iov
.iov_len
= MAXPATHLEN
;
616 uio
->uio_segflg
= UIO_SYSSPACE
;
617 uio
->uio_resid
= MAXPATHLEN
;
620 VOP_READLINK(vp
, uio
, 0, NULL
, error
);
623 link
= ERR_PTR(-error
);
625 link
[MAXPATHLEN
- uio
->uio_resid
] = '\0';
629 nd_set_link(nd
, link
);
635 struct dentry
*dentry
,
636 struct nameidata
*nd
,
639 char *s
= nd_get_link(nd
);
645 #ifdef CONFIG_XFS_POSIX_ACL
650 struct nameidata
*nd
)
652 vnode_t
*vp
= LINVFS_GET_VP(inode
);
655 mode
<<= 6; /* convert from linux to vnode access bits */
656 VOP_ACCESS(vp
, mode
, NULL
, error
);
660 #define linvfs_permission NULL
665 struct vfsmount
*mnt
,
666 struct dentry
*dentry
,
669 struct inode
*inode
= dentry
->d_inode
;
670 vnode_t
*vp
= LINVFS_GET_VP(inode
);
673 if (unlikely(vp
->v_flag
& VMODIFIED
))
674 error
= vn_revalidate(vp
);
676 generic_fillattr(inode
, stat
);
682 struct dentry
*dentry
,
685 struct inode
*inode
= dentry
->d_inode
;
686 unsigned int ia_valid
= attr
->ia_valid
;
687 vnode_t
*vp
= LINVFS_GET_VP(inode
);
688 vattr_t vattr
= { 0 };
692 if (ia_valid
& ATTR_UID
) {
693 vattr
.va_mask
|= XFS_AT_UID
;
694 vattr
.va_uid
= attr
->ia_uid
;
696 if (ia_valid
& ATTR_GID
) {
697 vattr
.va_mask
|= XFS_AT_GID
;
698 vattr
.va_gid
= attr
->ia_gid
;
700 if (ia_valid
& ATTR_SIZE
) {
701 vattr
.va_mask
|= XFS_AT_SIZE
;
702 vattr
.va_size
= attr
->ia_size
;
704 if (ia_valid
& ATTR_ATIME
) {
705 vattr
.va_mask
|= XFS_AT_ATIME
;
706 vattr
.va_atime
= attr
->ia_atime
;
707 if (ia_valid
& ATTR_ATIME_SET
)
708 inode
->i_atime
= attr
->ia_atime
;
710 if (ia_valid
& ATTR_MTIME
) {
711 vattr
.va_mask
|= XFS_AT_MTIME
;
712 vattr
.va_mtime
= attr
->ia_mtime
;
714 if (ia_valid
& ATTR_CTIME
) {
715 vattr
.va_mask
|= XFS_AT_CTIME
;
716 vattr
.va_ctime
= attr
->ia_ctime
;
718 if (ia_valid
& ATTR_MODE
) {
719 vattr
.va_mask
|= XFS_AT_MODE
;
720 vattr
.va_mode
= attr
->ia_mode
;
721 if (!in_group_p(inode
->i_gid
) && !capable(CAP_FSETID
))
722 inode
->i_mode
&= ~S_ISGID
;
725 if (ia_valid
& (ATTR_MTIME_SET
| ATTR_ATIME_SET
))
728 if ((ia_valid
& ATTR_NO_BLOCK
))
729 flags
|= ATTR_NONBLOCK
;
732 VOP_SETATTR(vp
, &vattr
, flags
, NULL
, error
);
734 __vn_revalidate(vp
, &vattr
);
742 block_truncate_page(inode
->i_mapping
, inode
->i_size
, linvfs_get_block
);
747 struct dentry
*dentry
,
753 vnode_t
*vp
= LINVFS_GET_VP(dentry
->d_inode
);
754 char *attr
= (char *)name
;
759 namesp
= attr_lookup_namespace(attr
, attr_namespaces
, ATTR_NAMECOUNT
);
762 attr
+= namesp
->attr_namelen
;
763 error
= namesp
->attr_capable(vp
, NULL
);
767 /* Convert Linux syscall to XFS internal ATTR flags */
768 if (flags
& XATTR_CREATE
)
769 xflags
|= ATTR_CREATE
;
770 if (flags
& XATTR_REPLACE
)
771 xflags
|= ATTR_REPLACE
;
772 xflags
|= namesp
->attr_flag
;
773 return namesp
->attr_set(vp
, attr
, (void *)data
, size
, xflags
);
778 struct dentry
*dentry
,
783 vnode_t
*vp
= LINVFS_GET_VP(dentry
->d_inode
);
784 char *attr
= (char *)name
;
789 namesp
= attr_lookup_namespace(attr
, attr_namespaces
, ATTR_NAMECOUNT
);
792 attr
+= namesp
->attr_namelen
;
793 error
= namesp
->attr_capable(vp
, NULL
);
797 /* Convert Linux syscall to XFS internal ATTR flags */
799 xflags
|= ATTR_KERNOVAL
;
802 xflags
|= namesp
->attr_flag
;
803 return namesp
->attr_get(vp
, attr
, (void *)data
, size
, xflags
);
808 struct dentry
*dentry
,
812 vnode_t
*vp
= LINVFS_GET_VP(dentry
->d_inode
);
813 int error
, xflags
= ATTR_KERNAMELS
;
817 xflags
|= ATTR_KERNOVAL
;
818 xflags
|= capable(CAP_SYS_ADMIN
) ? ATTR_KERNFULLS
: ATTR_KERNORMALS
;
820 error
= attr_generic_list(vp
, data
, size
, xflags
, &result
);
828 struct dentry
*dentry
,
831 vnode_t
*vp
= LINVFS_GET_VP(dentry
->d_inode
);
832 char *attr
= (char *)name
;
837 namesp
= attr_lookup_namespace(attr
, attr_namespaces
, ATTR_NAMECOUNT
);
840 attr
+= namesp
->attr_namelen
;
841 error
= namesp
->attr_capable(vp
, NULL
);
844 xflags
|= namesp
->attr_flag
;
845 return namesp
->attr_remove(vp
, attr
, xflags
);
849 struct inode_operations linvfs_file_inode_operations
= {
850 .permission
= linvfs_permission
,
851 .truncate
= linvfs_truncate
,
852 .getattr
= linvfs_getattr
,
853 .setattr
= linvfs_setattr
,
854 .setxattr
= linvfs_setxattr
,
855 .getxattr
= linvfs_getxattr
,
856 .listxattr
= linvfs_listxattr
,
857 .removexattr
= linvfs_removexattr
,
860 struct inode_operations linvfs_dir_inode_operations
= {
861 .create
= linvfs_create
,
862 .lookup
= linvfs_lookup
,
864 .unlink
= linvfs_unlink
,
865 .symlink
= linvfs_symlink
,
866 .mkdir
= linvfs_mkdir
,
867 .rmdir
= linvfs_rmdir
,
868 .mknod
= linvfs_mknod
,
869 .rename
= linvfs_rename
,
870 .permission
= linvfs_permission
,
871 .getattr
= linvfs_getattr
,
872 .setattr
= linvfs_setattr
,
873 .setxattr
= linvfs_setxattr
,
874 .getxattr
= linvfs_getxattr
,
875 .listxattr
= linvfs_listxattr
,
876 .removexattr
= linvfs_removexattr
,
879 struct inode_operations linvfs_symlink_inode_operations
= {
880 .readlink
= generic_readlink
,
881 .follow_link
= linvfs_follow_link
,
882 .put_link
= linvfs_put_link
,
883 .permission
= linvfs_permission
,
884 .getattr
= linvfs_getattr
,
885 .setattr
= linvfs_setattr
,
886 .setxattr
= linvfs_setxattr
,
887 .getxattr
= linvfs_getxattr
,
888 .listxattr
= linvfs_listxattr
,
889 .removexattr
= linvfs_removexattr
,