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/xattr.h>
55 #include <linux/namei.h>
58 * Change the requested timestamp in the given inode.
59 * We don't lock across timestamp updates, and we don't log them but
60 * we do record the fact that there is dirty information in core.
62 * NOTE -- callers MUST combine XFS_ICHGTIME_MOD or XFS_ICHGTIME_CHG
63 * with XFS_ICHGTIME_ACC to be sure that access time
64 * update will take. Calling first with XFS_ICHGTIME_ACC
65 * and then XFS_ICHGTIME_MOD may fail to modify the access
66 * timestamp if the filesystem is mounted noacctm.
73 struct inode
*inode
= LINVFS_GET_IP(XFS_ITOV(ip
));
77 * We're not supposed to change timestamps in readonly-mounted
78 * filesystems. Throw it away if anyone asks us.
80 if (unlikely(IS_RDONLY(inode
)))
84 * Don't update access timestamps on reads if mounted "noatime".
85 * Throw it away if anyone asks us.
88 (ip
->i_mount
->m_flags
& XFS_MOUNT_NOATIME
|| IS_NOATIME(inode
)) &&
89 (flags
& (XFS_ICHGTIME_ACC
|XFS_ICHGTIME_MOD
|XFS_ICHGTIME_CHG
)) ==
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).
129 * We also skip the readonly and noatime checks here, they are
130 * also catered for already.
141 * We're not supposed to change timestamps in readonly-mounted
142 * filesystems. Throw it away if anyone asks us.
144 if (unlikely(IS_RDONLY(inode
)))
148 * Don't update access timestamps on reads if mounted "noatime".
149 * Throw it away if anyone asks us.
152 (ip
->i_mount
->m_flags
& XFS_MOUNT_NOATIME
|| IS_NOATIME(inode
)) &&
153 ((flags
& (XFS_ICHGTIME_ACC
|XFS_ICHGTIME_MOD
|XFS_ICHGTIME_CHG
)) ==
157 if (flags
& XFS_ICHGTIME_MOD
) {
158 tvp
= &inode
->i_mtime
;
159 ip
->i_d
.di_mtime
.t_sec
= (__int32_t
)tvp
->tv_sec
;
160 ip
->i_d
.di_mtime
.t_nsec
= (__int32_t
)tvp
->tv_nsec
;
162 if (flags
& XFS_ICHGTIME_ACC
) {
163 tvp
= &inode
->i_atime
;
164 ip
->i_d
.di_atime
.t_sec
= (__int32_t
)tvp
->tv_sec
;
165 ip
->i_d
.di_atime
.t_nsec
= (__int32_t
)tvp
->tv_nsec
;
167 if (flags
& XFS_ICHGTIME_CHG
) {
168 tvp
= &inode
->i_ctime
;
169 ip
->i_d
.di_ctime
.t_sec
= (__int32_t
)tvp
->tv_sec
;
170 ip
->i_d
.di_ctime
.t_nsec
= (__int32_t
)tvp
->tv_nsec
;
174 * We update the i_update_core field _after_ changing
175 * the timestamps in order to coordinate properly with
176 * xfs_iflush() so that we don't lose timestamp updates.
177 * This keeps us from having to hold the inode lock
178 * while doing this. We use the SYNCHRONIZE macro to
179 * ensure that the compiler does not reorder the update
180 * of i_update_core above the timestamp updates above.
183 ip
->i_update_core
= 1;
184 if (!(inode
->i_state
& I_LOCK
))
185 mark_inode_dirty_sync(inode
);
190 * Pull the link count and size up from the xfs inode to the linux inode
196 vnode_t
*vp
= LINVFS_GET_VP(ip
);
200 va
.va_mask
= XFS_AT_NLINK
|XFS_AT_SIZE
|XFS_AT_NBLOCKS
;
201 VOP_GETATTR(vp
, &va
, ATTR_LAZY
, NULL
, error
);
202 if (likely(!error
)) {
203 ip
->i_nlink
= va
.va_nlink
;
204 ip
->i_blocks
= va
.va_nblocks
;
206 /* we're under i_sem so i_size can't change under us */
207 if (i_size_read(ip
) != va
.va_size
)
208 i_size_write(ip
, va
.va_size
);
213 * Determine whether a process has a valid fs_struct (kernel daemons
214 * like knfsd don't have an fs_struct).
216 * XXX(hch): nfsd is broken, better fix it instead.
219 has_fs_struct(struct task_struct
*task
)
221 return (task
->fs
!= init_task
.fs
);
227 struct dentry
*dentry
,
233 vnode_t
*vp
= NULL
, *dvp
= LINVFS_GET_VP(dir
);
234 xfs_acl_t
*default_acl
= NULL
;
235 attrexists_t test_default_acl
= _ACL_DEFAULT_EXISTS
;
239 * Irix uses Missed'em'V split, but doesn't want to see
240 * the upper 5 bits of (14bit) major.
242 if (!sysv_valid_dev(rdev
) || MAJOR(rdev
) & ~0x1ff)
245 if (test_default_acl
&& test_default_acl(dvp
)) {
246 if (!_ACL_ALLOC(default_acl
))
248 if (!_ACL_GET_DEFAULT(dvp
, default_acl
)) {
249 _ACL_FREE(default_acl
);
254 if (IS_POSIXACL(dir
) && !default_acl
&& has_fs_struct(current
))
255 mode
&= ~current
->fs
->umask
;
257 memset(&va
, 0, sizeof(va
));
258 va
.va_mask
= XFS_AT_TYPE
|XFS_AT_MODE
;
261 switch (mode
& S_IFMT
) {
262 case S_IFCHR
: case S_IFBLK
: case S_IFIFO
: case S_IFSOCK
:
263 va
.va_rdev
= sysv_encode_dev(rdev
);
264 va
.va_mask
|= XFS_AT_RDEV
;
267 VOP_CREATE(dvp
, dentry
, &va
, &vp
, NULL
, error
);
270 VOP_MKDIR(dvp
, dentry
, &va
, &vp
, NULL
, error
);
279 error
= _ACL_INHERIT(vp
, &va
, default_acl
);
283 struct dentry teardown
= {};
287 * If we can't add the ACL we must back out.
288 * ENOSPC can hit here, among other things.
290 teardown
.d_inode
= ip
= LINVFS_GET_IP(vp
);
291 teardown
.d_name
= dentry
->d_name
;
296 VOP_RMDIR(dvp
, &teardown
, NULL
, err2
);
298 VOP_REMOVE(dvp
, &teardown
, NULL
, err2
);
302 _ACL_FREE(default_acl
);
307 ip
= LINVFS_GET_IP(vp
);
309 if (S_ISCHR(mode
) || S_ISBLK(mode
))
311 else if (S_ISDIR(mode
))
313 d_instantiate(dentry
, ip
);
314 validate_fields(dir
);
322 struct dentry
*dentry
,
324 struct nameidata
*nd
)
326 return linvfs_mknod(dir
, dentry
, mode
, 0);
332 struct dentry
*dentry
,
335 return linvfs_mknod(dir
, dentry
, mode
|S_IFDIR
, 0);
338 STATIC
struct dentry
*
341 struct dentry
*dentry
,
342 struct nameidata
*nd
)
344 struct vnode
*vp
= LINVFS_GET_VP(dir
), *cvp
;
347 if (dentry
->d_name
.len
>= MAXNAMELEN
)
348 return ERR_PTR(-ENAMETOOLONG
);
350 VOP_LOOKUP(vp
, dentry
, &cvp
, 0, NULL
, NULL
, error
);
352 if (unlikely(error
!= ENOENT
))
353 return ERR_PTR(-error
);
358 return d_splice_alias(LINVFS_GET_IP(cvp
), dentry
);
363 struct dentry
*old_dentry
,
365 struct dentry
*dentry
)
367 struct inode
*ip
; /* inode of guy being linked to */
368 vnode_t
*tdvp
; /* target directory for new name/link */
369 vnode_t
*vp
; /* vp of name being linked */
372 ip
= old_dentry
->d_inode
; /* inode being linked to */
373 if (S_ISDIR(ip
->i_mode
))
376 tdvp
= LINVFS_GET_VP(dir
);
377 vp
= LINVFS_GET_VP(ip
);
379 VOP_LINK(tdvp
, vp
, dentry
, NULL
, error
);
384 d_instantiate(dentry
, ip
);
392 struct dentry
*dentry
)
395 vnode_t
*dvp
; /* directory containing name to remove */
398 inode
= dentry
->d_inode
;
399 dvp
= LINVFS_GET_VP(dir
);
401 VOP_REMOVE(dvp
, dentry
, NULL
, error
);
403 validate_fields(dir
); /* For size only */
404 validate_fields(inode
);
413 struct dentry
*dentry
,
418 vnode_t
*dvp
; /* directory containing name of symlink */
419 vnode_t
*cvp
; /* used to lookup symlink to put in dentry */
422 dvp
= LINVFS_GET_VP(dir
);
425 memset(&va
, 0, sizeof(va
));
426 va
.va_mode
= S_IFLNK
|
427 (irix_symlink_mode
? 0777 & ~current
->fs
->umask
: S_IRWXUGO
);
428 va
.va_mask
= XFS_AT_TYPE
|XFS_AT_MODE
;
431 VOP_SYMLINK(dvp
, dentry
, &va
, (char *)symname
, &cvp
, NULL
, error
);
433 ip
= LINVFS_GET_IP(cvp
);
434 d_instantiate(dentry
, ip
);
435 validate_fields(dir
);
436 validate_fields(ip
); /* size needs update */
444 struct dentry
*dentry
)
446 struct inode
*inode
= dentry
->d_inode
;
447 vnode_t
*dvp
= LINVFS_GET_VP(dir
);
450 VOP_RMDIR(dvp
, dentry
, NULL
, error
);
452 validate_fields(inode
);
453 validate_fields(dir
);
461 struct dentry
*odentry
,
463 struct dentry
*ndentry
)
465 struct inode
*new_inode
= ndentry
->d_inode
;
466 vnode_t
*fvp
; /* from directory */
467 vnode_t
*tvp
; /* target directory */
470 fvp
= LINVFS_GET_VP(odir
);
471 tvp
= LINVFS_GET_VP(ndir
);
473 VOP_RENAME(fvp
, odentry
, tvp
, ndentry
, NULL
, error
);
478 validate_fields(new_inode
);
480 validate_fields(odir
);
482 validate_fields(ndir
);
487 * careful here - this function can get called recursively, so
488 * we need to be very careful about how much stack we use.
489 * uio is kmalloced for this reason...
493 struct dentry
*dentry
,
494 struct nameidata
*nd
)
505 link
= (char *)kmalloc(MAXNAMELEN
+1, GFP_KERNEL
);
507 nd_set_link(nd
, ERR_PTR(-ENOMEM
));
511 uio
= (uio_t
*)kmalloc(sizeof(uio_t
), GFP_KERNEL
);
514 nd_set_link(nd
, ERR_PTR(-ENOMEM
));
518 vp
= LINVFS_GET_VP(dentry
->d_inode
);
521 iov
.iov_len
= MAXNAMELEN
;
525 uio
->uio_segflg
= UIO_SYSSPACE
;
526 uio
->uio_resid
= MAXNAMELEN
;
529 VOP_READLINK(vp
, uio
, 0, NULL
, error
);
532 link
= ERR_PTR(-error
);
534 link
[MAXNAMELEN
- uio
->uio_resid
] = '\0';
538 nd_set_link(nd
, link
);
544 struct dentry
*dentry
,
545 struct nameidata
*nd
,
548 char *s
= nd_get_link(nd
);
554 #ifdef CONFIG_XFS_POSIX_ACL
559 struct nameidata
*nd
)
561 vnode_t
*vp
= LINVFS_GET_VP(inode
);
564 mode
<<= 6; /* convert from linux to vnode access bits */
565 VOP_ACCESS(vp
, mode
, NULL
, error
);
569 #define linvfs_permission NULL
574 struct vfsmount
*mnt
,
575 struct dentry
*dentry
,
578 struct inode
*inode
= dentry
->d_inode
;
579 vnode_t
*vp
= LINVFS_GET_VP(inode
);
582 if (unlikely(vp
->v_flag
& VMODIFIED
))
583 error
= vn_revalidate(vp
);
585 generic_fillattr(inode
, stat
);
591 struct dentry
*dentry
,
594 struct inode
*inode
= dentry
->d_inode
;
595 unsigned int ia_valid
= attr
->ia_valid
;
596 vnode_t
*vp
= LINVFS_GET_VP(inode
);
601 memset(&vattr
, 0, sizeof(vattr_t
));
602 if (ia_valid
& ATTR_UID
) {
603 vattr
.va_mask
|= XFS_AT_UID
;
604 vattr
.va_uid
= attr
->ia_uid
;
606 if (ia_valid
& ATTR_GID
) {
607 vattr
.va_mask
|= XFS_AT_GID
;
608 vattr
.va_gid
= attr
->ia_gid
;
610 if (ia_valid
& ATTR_SIZE
) {
611 vattr
.va_mask
|= XFS_AT_SIZE
;
612 vattr
.va_size
= attr
->ia_size
;
614 if (ia_valid
& ATTR_ATIME
) {
615 vattr
.va_mask
|= XFS_AT_ATIME
;
616 vattr
.va_atime
= attr
->ia_atime
;
618 if (ia_valid
& ATTR_MTIME
) {
619 vattr
.va_mask
|= XFS_AT_MTIME
;
620 vattr
.va_mtime
= attr
->ia_mtime
;
622 if (ia_valid
& ATTR_CTIME
) {
623 vattr
.va_mask
|= XFS_AT_CTIME
;
624 vattr
.va_ctime
= attr
->ia_ctime
;
626 if (ia_valid
& ATTR_MODE
) {
627 vattr
.va_mask
|= XFS_AT_MODE
;
628 vattr
.va_mode
= attr
->ia_mode
;
629 if (!in_group_p(inode
->i_gid
) && !capable(CAP_FSETID
))
630 inode
->i_mode
&= ~S_ISGID
;
633 if (ia_valid
& (ATTR_MTIME_SET
| ATTR_ATIME_SET
))
636 if ((ia_valid
& ATTR_NO_BLOCK
))
637 flags
|= ATTR_NONBLOCK
;
640 VOP_SETATTR(vp
, &vattr
, flags
, NULL
, error
);
651 block_truncate_page(inode
->i_mapping
, inode
->i_size
, linvfs_get_block
);
656 struct dentry
*dentry
,
662 vnode_t
*vp
= LINVFS_GET_VP(dentry
->d_inode
);
663 char *attr
= (char *)name
;
668 namesp
= attr_lookup_namespace(attr
, attr_namespaces
, ATTR_NAMECOUNT
);
671 attr
+= namesp
->attr_namelen
;
672 error
= namesp
->attr_capable(vp
, NULL
);
676 /* Convert Linux syscall to XFS internal ATTR flags */
677 if (flags
& XATTR_CREATE
)
678 xflags
|= ATTR_CREATE
;
679 if (flags
& XATTR_REPLACE
)
680 xflags
|= ATTR_REPLACE
;
681 xflags
|= namesp
->attr_flag
;
682 return namesp
->attr_set(vp
, attr
, (void *)data
, size
, xflags
);
687 struct dentry
*dentry
,
692 vnode_t
*vp
= LINVFS_GET_VP(dentry
->d_inode
);
693 char *attr
= (char *)name
;
698 namesp
= attr_lookup_namespace(attr
, attr_namespaces
, ATTR_NAMECOUNT
);
701 attr
+= namesp
->attr_namelen
;
702 error
= namesp
->attr_capable(vp
, NULL
);
706 /* Convert Linux syscall to XFS internal ATTR flags */
708 xflags
|= ATTR_KERNOVAL
;
711 xflags
|= namesp
->attr_flag
;
712 return namesp
->attr_get(vp
, attr
, (void *)data
, size
, xflags
);
717 struct dentry
*dentry
,
721 vnode_t
*vp
= LINVFS_GET_VP(dentry
->d_inode
);
722 int error
, xflags
= ATTR_KERNAMELS
;
726 xflags
|= ATTR_KERNOVAL
;
727 xflags
|= capable(CAP_SYS_ADMIN
) ? ATTR_KERNFULLS
: ATTR_KERNORMALS
;
729 error
= attr_generic_list(vp
, data
, size
, xflags
, &result
);
737 struct dentry
*dentry
,
740 vnode_t
*vp
= LINVFS_GET_VP(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
);
753 xflags
|= namesp
->attr_flag
;
754 return namesp
->attr_remove(vp
, attr
, xflags
);
758 struct inode_operations linvfs_file_inode_operations
= {
759 .permission
= linvfs_permission
,
760 .truncate
= linvfs_truncate
,
761 .getattr
= linvfs_getattr
,
762 .setattr
= linvfs_setattr
,
763 .setxattr
= linvfs_setxattr
,
764 .getxattr
= linvfs_getxattr
,
765 .listxattr
= linvfs_listxattr
,
766 .removexattr
= linvfs_removexattr
,
769 struct inode_operations linvfs_dir_inode_operations
= {
770 .create
= linvfs_create
,
771 .lookup
= linvfs_lookup
,
773 .unlink
= linvfs_unlink
,
774 .symlink
= linvfs_symlink
,
775 .mkdir
= linvfs_mkdir
,
776 .rmdir
= linvfs_rmdir
,
777 .mknod
= linvfs_mknod
,
778 .rename
= linvfs_rename
,
779 .permission
= linvfs_permission
,
780 .getattr
= linvfs_getattr
,
781 .setattr
= linvfs_setattr
,
782 .setxattr
= linvfs_setxattr
,
783 .getxattr
= linvfs_getxattr
,
784 .listxattr
= linvfs_listxattr
,
785 .removexattr
= linvfs_removexattr
,
788 struct inode_operations linvfs_symlink_inode_operations
= {
789 .readlink
= generic_readlink
,
790 .follow_link
= linvfs_follow_link
,
791 .put_link
= linvfs_put_link
,
792 .permission
= linvfs_permission
,
793 .getattr
= linvfs_getattr
,
794 .setattr
= linvfs_setattr
,
795 .setxattr
= linvfs_setxattr
,
796 .getxattr
= linvfs_getxattr
,
797 .listxattr
= linvfs_listxattr
,
798 .removexattr
= linvfs_removexattr
,