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>
57 * Bring the atime in the XFS inode uptodate.
58 * Used before logging the inode to disk or when the Linux inode goes away.
61 xfs_synchronize_atime(
66 vp
= XFS_ITOV_NULL(ip
);
68 struct inode
*inode
= &vp
->v_inode
;
69 ip
->i_d
.di_atime
.t_sec
= (__int32_t
)inode
->i_atime
.tv_sec
;
70 ip
->i_d
.di_atime
.t_nsec
= (__int32_t
)inode
->i_atime
.tv_nsec
;
75 * Change the requested timestamp in the given inode.
76 * We don't lock across timestamp updates, and we don't log them but
77 * we do record the fact that there is dirty information in core.
79 * NOTE -- callers MUST combine XFS_ICHGTIME_MOD or XFS_ICHGTIME_CHG
80 * with XFS_ICHGTIME_ACC to be sure that access time
81 * update will take. Calling first with XFS_ICHGTIME_ACC
82 * and then XFS_ICHGTIME_MOD may fail to modify the access
83 * timestamp if the filesystem is mounted noacctm.
90 struct inode
*inode
= vn_to_inode(XFS_ITOV(ip
));
94 if (flags
& XFS_ICHGTIME_MOD
) {
96 ip
->i_d
.di_mtime
.t_sec
= (__int32_t
)tv
.tv_sec
;
97 ip
->i_d
.di_mtime
.t_nsec
= (__int32_t
)tv
.tv_nsec
;
99 if (flags
& XFS_ICHGTIME_ACC
) {
101 ip
->i_d
.di_atime
.t_sec
= (__int32_t
)tv
.tv_sec
;
102 ip
->i_d
.di_atime
.t_nsec
= (__int32_t
)tv
.tv_nsec
;
104 if (flags
& XFS_ICHGTIME_CHG
) {
106 ip
->i_d
.di_ctime
.t_sec
= (__int32_t
)tv
.tv_sec
;
107 ip
->i_d
.di_ctime
.t_nsec
= (__int32_t
)tv
.tv_nsec
;
111 * We update the i_update_core field _after_ changing
112 * the timestamps in order to coordinate properly with
113 * xfs_iflush() so that we don't lose timestamp updates.
114 * This keeps us from having to hold the inode lock
115 * while doing this. We use the SYNCHRONIZE macro to
116 * ensure that the compiler does not reorder the update
117 * of i_update_core above the timestamp updates above.
120 ip
->i_update_core
= 1;
121 if (!(inode
->i_state
& I_LOCK
))
122 mark_inode_dirty_sync(inode
);
126 * Variant on the above which avoids querying the system clock
127 * in situations where we know the Linux inode timestamps have
128 * just been updated (and so we can update our inode cheaply).
139 * Atime updates for read() & friends are handled lazily now, and
140 * explicit updates must go through xfs_ichgtime()
142 ASSERT((flags
& XFS_ICHGTIME_ACC
) == 0);
145 * We're not supposed to change timestamps in readonly-mounted
146 * filesystems. Throw it away if anyone asks us.
148 if (unlikely(IS_RDONLY(inode
)))
151 if (flags
& XFS_ICHGTIME_MOD
) {
152 tvp
= &inode
->i_mtime
;
153 ip
->i_d
.di_mtime
.t_sec
= (__int32_t
)tvp
->tv_sec
;
154 ip
->i_d
.di_mtime
.t_nsec
= (__int32_t
)tvp
->tv_nsec
;
156 if (flags
& XFS_ICHGTIME_CHG
) {
157 tvp
= &inode
->i_ctime
;
158 ip
->i_d
.di_ctime
.t_sec
= (__int32_t
)tvp
->tv_sec
;
159 ip
->i_d
.di_ctime
.t_nsec
= (__int32_t
)tvp
->tv_nsec
;
163 * We update the i_update_core field _after_ changing
164 * the timestamps in order to coordinate properly with
165 * xfs_iflush() so that we don't lose timestamp updates.
166 * This keeps us from having to hold the inode lock
167 * while doing this. We use the SYNCHRONIZE macro to
168 * ensure that the compiler does not reorder the update
169 * of i_update_core above the timestamp updates above.
172 ip
->i_update_core
= 1;
173 if (!(inode
->i_state
& I_LOCK
))
174 mark_inode_dirty_sync(inode
);
179 * Pull the link count and size up from the xfs inode to the linux inode
186 vattr
->va_mask
= XFS_AT_NLINK
|XFS_AT_SIZE
|XFS_AT_NBLOCKS
;
187 if (!xfs_getattr(XFS_I(ip
), vattr
, ATTR_LAZY
)) {
188 ip
->i_nlink
= vattr
->va_nlink
;
189 ip
->i_blocks
= vattr
->va_nblocks
;
191 /* we're under i_sem so i_size can't change under us */
192 if (i_size_read(ip
) != vattr
->va_size
)
193 i_size_write(ip
, vattr
->va_size
);
198 * Hook in SELinux. This is not quite correct yet, what we really need
199 * here (as we do for default ACLs) is a mechanism by which creation of
200 * these attrs can be journalled at inode creation time (along with the
201 * inode, of course, such that log replay can't cause these to be lost).
208 struct inode
*ip
= vn_to_inode(vp
);
214 error
= security_inode_init_security(ip
, dir
, &name
, &value
, &length
);
216 if (error
== -EOPNOTSUPP
)
221 error
= xfs_attr_set(XFS_I(ip
), name
, value
,
222 length
, ATTR_SECURE
);
224 xfs_iflags_set(XFS_I(ip
), XFS_IMODIFIED
);
232 * Determine whether a process has a valid fs_struct (kernel daemons
233 * like knfsd don't have an fs_struct).
235 * XXX(hch): nfsd is broken, better fix it instead.
238 xfs_has_fs_struct(struct task_struct
*task
)
240 return (task
->fs
!= init_task
.fs
);
247 struct dentry
*dentry
,
250 struct dentry teardown
= {};
253 * If we can't add the ACL or we fail in
254 * xfs_init_security we must back out.
255 * ENOSPC can hit here, among other things.
257 teardown
.d_inode
= vn_to_inode(vp
);
258 teardown
.d_name
= dentry
->d_name
;
261 xfs_rmdir(XFS_I(dir
), &teardown
);
263 xfs_remove(XFS_I(dir
), &teardown
);
270 struct dentry
*dentry
,
275 bhv_vattr_t vattr
= { 0 };
276 bhv_vnode_t
*vp
= NULL
, *dvp
= vn_from_inode(dir
);
277 xfs_acl_t
*default_acl
= NULL
;
278 attrexists_t test_default_acl
= _ACL_DEFAULT_EXISTS
;
282 * Irix uses Missed'em'V split, but doesn't want to see
283 * the upper 5 bits of (14bit) major.
285 if (unlikely(!sysv_valid_dev(rdev
) || MAJOR(rdev
) & ~0x1ff))
288 if (unlikely(test_default_acl
&& test_default_acl(dvp
))) {
289 if (!_ACL_ALLOC(default_acl
)) {
292 if (!_ACL_GET_DEFAULT(dvp
, default_acl
)) {
293 _ACL_FREE(default_acl
);
298 if (IS_POSIXACL(dir
) && !default_acl
&& xfs_has_fs_struct(current
))
299 mode
&= ~current
->fs
->umask
;
301 vattr
.va_mask
= XFS_AT_TYPE
|XFS_AT_MODE
;
302 vattr
.va_mode
= mode
;
304 switch (mode
& S_IFMT
) {
305 case S_IFCHR
: case S_IFBLK
: case S_IFIFO
: case S_IFSOCK
:
306 vattr
.va_rdev
= sysv_encode_dev(rdev
);
307 vattr
.va_mask
|= XFS_AT_RDEV
;
310 error
= xfs_create(XFS_I(dir
), dentry
, &vattr
, &vp
, NULL
);
313 error
= xfs_mkdir(XFS_I(dir
), dentry
, &vattr
, &vp
, NULL
);
320 if (unlikely(!error
)) {
321 error
= xfs_init_security(vp
, dir
);
323 xfs_cleanup_inode(dir
, vp
, dentry
, mode
);
326 if (unlikely(default_acl
)) {
328 error
= _ACL_INHERIT(vp
, &vattr
, default_acl
);
330 xfs_iflags_set(XFS_I(&vp
->v_inode
), XFS_IMODIFIED
);
332 xfs_cleanup_inode(dir
, vp
, dentry
, mode
);
334 _ACL_FREE(default_acl
);
337 if (likely(!error
)) {
339 ip
= vn_to_inode(vp
);
341 if (S_ISCHR(mode
) || S_ISBLK(mode
))
343 else if (S_ISDIR(mode
))
344 xfs_validate_fields(ip
, &vattr
);
345 d_instantiate(dentry
, ip
);
346 xfs_validate_fields(dir
, &vattr
);
354 struct dentry
*dentry
,
356 struct nameidata
*nd
)
358 return xfs_vn_mknod(dir
, dentry
, mode
, 0);
364 struct dentry
*dentry
,
367 return xfs_vn_mknod(dir
, dentry
, mode
|S_IFDIR
, 0);
370 STATIC
struct dentry
*
373 struct dentry
*dentry
,
374 struct nameidata
*nd
)
379 if (dentry
->d_name
.len
>= MAXNAMELEN
)
380 return ERR_PTR(-ENAMETOOLONG
);
382 error
= xfs_lookup(XFS_I(dir
), dentry
, &cvp
);
383 if (unlikely(error
)) {
384 if (unlikely(error
!= ENOENT
))
385 return ERR_PTR(-error
);
390 return d_splice_alias(vn_to_inode(cvp
), dentry
);
395 struct dentry
*old_dentry
,
397 struct dentry
*dentry
)
399 struct inode
*ip
; /* inode of guy being linked to */
400 bhv_vnode_t
*vp
; /* vp of name being linked */
404 ip
= old_dentry
->d_inode
; /* inode being linked to */
405 vp
= vn_from_inode(ip
);
408 error
= xfs_link(XFS_I(dir
), vp
, dentry
);
409 if (unlikely(error
)) {
412 xfs_iflags_set(XFS_I(dir
), XFS_IMODIFIED
);
413 xfs_validate_fields(ip
, &vattr
);
414 d_instantiate(dentry
, ip
);
422 struct dentry
*dentry
)
428 inode
= dentry
->d_inode
;
430 error
= xfs_remove(XFS_I(dir
), dentry
);
431 if (likely(!error
)) {
432 xfs_validate_fields(dir
, &vattr
); /* size needs update */
433 xfs_validate_fields(inode
, &vattr
);
441 struct dentry
*dentry
,
445 bhv_vattr_t va
= { 0 };
446 bhv_vnode_t
*cvp
; /* used to lookup symlink to put in dentry */
451 va
.va_mode
= S_IFLNK
|
452 (irix_symlink_mode
? 0777 & ~current
->fs
->umask
: S_IRWXUGO
);
453 va
.va_mask
= XFS_AT_TYPE
|XFS_AT_MODE
;
455 error
= xfs_symlink(XFS_I(dir
), dentry
, &va
,
456 (char *)symname
, &cvp
, NULL
);
457 if (likely(!error
&& cvp
)) {
458 error
= xfs_init_security(cvp
, dir
);
459 if (likely(!error
)) {
460 ip
= vn_to_inode(cvp
);
461 d_instantiate(dentry
, ip
);
462 xfs_validate_fields(dir
, &va
);
463 xfs_validate_fields(ip
, &va
);
465 xfs_cleanup_inode(dir
, cvp
, dentry
, 0);
474 struct dentry
*dentry
)
476 struct inode
*inode
= dentry
->d_inode
;
480 error
= xfs_rmdir(XFS_I(dir
), dentry
);
481 if (likely(!error
)) {
482 xfs_validate_fields(inode
, &vattr
);
483 xfs_validate_fields(dir
, &vattr
);
491 struct dentry
*odentry
,
493 struct dentry
*ndentry
)
495 struct inode
*new_inode
= ndentry
->d_inode
;
496 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
, &vattr
);
506 xfs_validate_fields(odir
, &vattr
);
508 xfs_validate_fields(ndir
, &vattr
);
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
561 struct nameidata
*nd
)
563 return -xfs_access(XFS_I(inode
), mode
<< 6, NULL
);
566 #define xfs_vn_permission NULL
571 struct vfsmount
*mnt
,
572 struct dentry
*dentry
,
575 struct inode
*inode
= dentry
->d_inode
;
576 bhv_vattr_t vattr
= { .va_mask
= XFS_AT_STAT
};
579 error
= xfs_getattr(XFS_I(inode
), &vattr
, ATTR_LAZY
);
580 if (likely(!error
)) {
581 stat
->size
= i_size_read(inode
);
582 stat
->dev
= inode
->i_sb
->s_dev
;
583 stat
->rdev
= (vattr
.va_rdev
== 0) ? 0 :
584 MKDEV(sysv_major(vattr
.va_rdev
) & 0x1ff,
585 sysv_minor(vattr
.va_rdev
));
586 stat
->mode
= vattr
.va_mode
;
587 stat
->nlink
= vattr
.va_nlink
;
588 stat
->uid
= vattr
.va_uid
;
589 stat
->gid
= vattr
.va_gid
;
590 stat
->ino
= vattr
.va_nodeid
;
591 stat
->atime
= vattr
.va_atime
;
592 stat
->mtime
= vattr
.va_mtime
;
593 stat
->ctime
= vattr
.va_ctime
;
594 stat
->blocks
= vattr
.va_nblocks
;
595 stat
->blksize
= vattr
.va_blocksize
;
602 struct dentry
*dentry
,
605 struct inode
*inode
= dentry
->d_inode
;
606 unsigned int ia_valid
= attr
->ia_valid
;
607 bhv_vattr_t vattr
= { 0 };
611 if (ia_valid
& ATTR_UID
) {
612 vattr
.va_mask
|= XFS_AT_UID
;
613 vattr
.va_uid
= attr
->ia_uid
;
615 if (ia_valid
& ATTR_GID
) {
616 vattr
.va_mask
|= XFS_AT_GID
;
617 vattr
.va_gid
= attr
->ia_gid
;
619 if (ia_valid
& ATTR_SIZE
) {
620 vattr
.va_mask
|= XFS_AT_SIZE
;
621 vattr
.va_size
= attr
->ia_size
;
623 if (ia_valid
& ATTR_ATIME
) {
624 vattr
.va_mask
|= XFS_AT_ATIME
;
625 vattr
.va_atime
= attr
->ia_atime
;
626 inode
->i_atime
= attr
->ia_atime
;
628 if (ia_valid
& ATTR_MTIME
) {
629 vattr
.va_mask
|= XFS_AT_MTIME
;
630 vattr
.va_mtime
= attr
->ia_mtime
;
632 if (ia_valid
& ATTR_CTIME
) {
633 vattr
.va_mask
|= XFS_AT_CTIME
;
634 vattr
.va_ctime
= attr
->ia_ctime
;
636 if (ia_valid
& ATTR_MODE
) {
637 vattr
.va_mask
|= XFS_AT_MODE
;
638 vattr
.va_mode
= attr
->ia_mode
;
639 if (!in_group_p(inode
->i_gid
) && !capable(CAP_FSETID
))
640 inode
->i_mode
&= ~S_ISGID
;
643 if (ia_valid
& (ATTR_MTIME_SET
| ATTR_ATIME_SET
))
646 if ((ia_valid
& ATTR_NO_BLOCK
))
647 flags
|= ATTR_NONBLOCK
;
650 error
= xfs_setattr(XFS_I(inode
), &vattr
, flags
, NULL
);
652 __vn_revalidate(vn_from_inode(inode
), &vattr
);
660 block_truncate_page(inode
->i_mapping
, inode
->i_size
, xfs_get_blocks
);
665 struct dentry
*dentry
,
671 bhv_vnode_t
*vp
= vn_from_inode(dentry
->d_inode
);
672 char *attr
= (char *)name
;
677 namesp
= attr_lookup_namespace(attr
, attr_namespaces
, ATTR_NAMECOUNT
);
680 attr
+= namesp
->attr_namelen
;
681 error
= namesp
->attr_capable(vp
, NULL
);
685 /* Convert Linux syscall to XFS internal ATTR flags */
686 if (flags
& XATTR_CREATE
)
687 xflags
|= ATTR_CREATE
;
688 if (flags
& XATTR_REPLACE
)
689 xflags
|= ATTR_REPLACE
;
690 xflags
|= namesp
->attr_flag
;
691 return namesp
->attr_set(vp
, attr
, (void *)data
, size
, xflags
);
696 struct dentry
*dentry
,
701 bhv_vnode_t
*vp
= vn_from_inode(dentry
->d_inode
);
702 char *attr
= (char *)name
;
707 namesp
= attr_lookup_namespace(attr
, attr_namespaces
, ATTR_NAMECOUNT
);
710 attr
+= namesp
->attr_namelen
;
711 error
= namesp
->attr_capable(vp
, NULL
);
715 /* Convert Linux syscall to XFS internal ATTR flags */
717 xflags
|= ATTR_KERNOVAL
;
720 xflags
|= namesp
->attr_flag
;
721 return namesp
->attr_get(vp
, attr
, (void *)data
, size
, xflags
);
726 struct dentry
*dentry
,
730 bhv_vnode_t
*vp
= vn_from_inode(dentry
->d_inode
);
731 int error
, xflags
= ATTR_KERNAMELS
;
735 xflags
|= ATTR_KERNOVAL
;
736 xflags
|= capable(CAP_SYS_ADMIN
) ? ATTR_KERNFULLS
: ATTR_KERNORMALS
;
738 error
= attr_generic_list(vp
, data
, size
, xflags
, &result
);
746 struct dentry
*dentry
,
749 bhv_vnode_t
*vp
= vn_from_inode(dentry
->d_inode
);
750 char *attr
= (char *)name
;
755 namesp
= attr_lookup_namespace(attr
, attr_namespaces
, ATTR_NAMECOUNT
);
758 attr
+= namesp
->attr_namelen
;
759 error
= namesp
->attr_capable(vp
, NULL
);
762 xflags
|= namesp
->attr_flag
;
763 return namesp
->attr_remove(vp
, attr
, xflags
);
767 const struct inode_operations xfs_inode_operations
= {
768 .permission
= xfs_vn_permission
,
769 .truncate
= xfs_vn_truncate
,
770 .getattr
= xfs_vn_getattr
,
771 .setattr
= xfs_vn_setattr
,
772 .setxattr
= xfs_vn_setxattr
,
773 .getxattr
= xfs_vn_getxattr
,
774 .listxattr
= xfs_vn_listxattr
,
775 .removexattr
= xfs_vn_removexattr
,
778 const struct inode_operations xfs_dir_inode_operations
= {
779 .create
= xfs_vn_create
,
780 .lookup
= xfs_vn_lookup
,
782 .unlink
= xfs_vn_unlink
,
783 .symlink
= xfs_vn_symlink
,
784 .mkdir
= xfs_vn_mkdir
,
785 .rmdir
= xfs_vn_rmdir
,
786 .mknod
= xfs_vn_mknod
,
787 .rename
= xfs_vn_rename
,
788 .permission
= xfs_vn_permission
,
789 .getattr
= xfs_vn_getattr
,
790 .setattr
= xfs_vn_setattr
,
791 .setxattr
= xfs_vn_setxattr
,
792 .getxattr
= xfs_vn_getxattr
,
793 .listxattr
= xfs_vn_listxattr
,
794 .removexattr
= xfs_vn_removexattr
,
797 const struct inode_operations xfs_symlink_inode_operations
= {
798 .readlink
= generic_readlink
,
799 .follow_link
= xfs_vn_follow_link
,
800 .put_link
= xfs_vn_put_link
,
801 .permission
= xfs_vn_permission
,
802 .getattr
= xfs_vn_getattr
,
803 .setattr
= xfs_vn_setattr
,
804 .setxattr
= xfs_vn_setxattr
,
805 .getxattr
= xfs_vn_getxattr
,
806 .listxattr
= xfs_vn_listxattr
,
807 .removexattr
= xfs_vn_removexattr
,