2 * Copyright (c) 2000-2004 Silicon Graphics, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
37 #include "xfs_trans.h"
42 #include "xfs_alloc.h"
43 #include "xfs_dmapi.h"
44 #include "xfs_quota.h"
45 #include "xfs_mount.h"
46 #include "xfs_alloc_btree.h"
47 #include "xfs_bmap_btree.h"
48 #include "xfs_ialloc_btree.h"
49 #include "xfs_btree.h"
50 #include "xfs_ialloc.h"
51 #include "xfs_attr_sf.h"
52 #include "xfs_dir_sf.h"
53 #include "xfs_dir2_sf.h"
54 #include "xfs_dinode.h"
55 #include "xfs_inode.h"
58 #include "xfs_rtalloc.h"
59 #include "xfs_error.h"
60 #include "xfs_itable.h"
66 #include "xfs_buf_item.h"
67 #include "xfs_utils.h"
69 #include <linux/xattr.h>
70 #include <linux/namei.h>
74 * Pull the link count and size up from the xfs inode to the linux inode
80 vnode_t
*vp
= LINVFS_GET_VP(ip
);
84 va
.va_mask
= XFS_AT_NLINK
|XFS_AT_SIZE
|XFS_AT_NBLOCKS
;
85 VOP_GETATTR(vp
, &va
, ATTR_LAZY
, NULL
, error
);
87 ip
->i_nlink
= va
.va_nlink
;
88 ip
->i_blocks
= va
.va_nblocks
;
90 /* we're under i_sem so i_size can't change under us */
91 if (i_size_read(ip
) != va
.va_size
)
92 i_size_write(ip
, va
.va_size
);
97 * Determine whether a process has a valid fs_struct (kernel daemons
98 * like knfsd don't have an fs_struct).
100 * XXX(hch): nfsd is broken, better fix it instead.
103 has_fs_struct(struct task_struct
*task
)
105 return (task
->fs
!= init_task
.fs
);
111 struct dentry
*dentry
,
117 vnode_t
*vp
= NULL
, *dvp
= LINVFS_GET_VP(dir
);
118 xfs_acl_t
*default_acl
= NULL
;
119 attrexists_t test_default_acl
= _ACL_DEFAULT_EXISTS
;
123 * Irix uses Missed'em'V split, but doesn't want to see
124 * the upper 5 bits of (14bit) major.
126 if (!sysv_valid_dev(rdev
) || MAJOR(rdev
) & ~0x1ff)
129 if (test_default_acl
&& test_default_acl(dvp
)) {
130 if (!_ACL_ALLOC(default_acl
))
132 if (!_ACL_GET_DEFAULT(dvp
, default_acl
)) {
133 _ACL_FREE(default_acl
);
138 if (IS_POSIXACL(dir
) && !default_acl
&& has_fs_struct(current
))
139 mode
&= ~current
->fs
->umask
;
141 memset(&va
, 0, sizeof(va
));
142 va
.va_mask
= XFS_AT_TYPE
|XFS_AT_MODE
;
143 va
.va_type
= IFTOVT(mode
);
146 switch (mode
& S_IFMT
) {
147 case S_IFCHR
: case S_IFBLK
: case S_IFIFO
: case S_IFSOCK
:
148 va
.va_rdev
= sysv_encode_dev(rdev
);
149 va
.va_mask
|= XFS_AT_RDEV
;
152 VOP_CREATE(dvp
, dentry
, &va
, &vp
, NULL
, error
);
155 VOP_MKDIR(dvp
, dentry
, &va
, &vp
, NULL
, error
);
164 error
= _ACL_INHERIT(vp
, &va
, default_acl
);
168 struct dentry teardown
= {};
172 * If we can't add the ACL we must back out.
173 * ENOSPC can hit here, among other things.
175 teardown
.d_inode
= ip
= LINVFS_GET_IP(vp
);
176 teardown
.d_name
= dentry
->d_name
;
181 VOP_RMDIR(dvp
, &teardown
, NULL
, err2
);
183 VOP_REMOVE(dvp
, &teardown
, NULL
, err2
);
187 _ACL_FREE(default_acl
);
192 ip
= LINVFS_GET_IP(vp
);
194 if (S_ISCHR(mode
) || S_ISBLK(mode
))
196 else if (S_ISDIR(mode
))
198 d_instantiate(dentry
, ip
);
199 validate_fields(dir
);
207 struct dentry
*dentry
,
209 struct nameidata
*nd
)
211 return linvfs_mknod(dir
, dentry
, mode
, 0);
217 struct dentry
*dentry
,
220 return linvfs_mknod(dir
, dentry
, mode
|S_IFDIR
, 0);
223 STATIC
struct dentry
*
226 struct dentry
*dentry
,
227 struct nameidata
*nd
)
229 struct vnode
*vp
= LINVFS_GET_VP(dir
), *cvp
;
232 if (dentry
->d_name
.len
>= MAXNAMELEN
)
233 return ERR_PTR(-ENAMETOOLONG
);
235 VOP_LOOKUP(vp
, dentry
, &cvp
, 0, NULL
, NULL
, error
);
237 if (unlikely(error
!= ENOENT
))
238 return ERR_PTR(-error
);
243 return d_splice_alias(LINVFS_GET_IP(cvp
), dentry
);
248 struct dentry
*old_dentry
,
250 struct dentry
*dentry
)
252 struct inode
*ip
; /* inode of guy being linked to */
253 vnode_t
*tdvp
; /* target directory for new name/link */
254 vnode_t
*vp
; /* vp of name being linked */
257 ip
= old_dentry
->d_inode
; /* inode being linked to */
258 if (S_ISDIR(ip
->i_mode
))
261 tdvp
= LINVFS_GET_VP(dir
);
262 vp
= LINVFS_GET_VP(ip
);
264 VOP_LINK(tdvp
, vp
, dentry
, NULL
, error
);
269 d_instantiate(dentry
, ip
);
277 struct dentry
*dentry
)
280 vnode_t
*dvp
; /* directory containing name to remove */
283 inode
= dentry
->d_inode
;
284 dvp
= LINVFS_GET_VP(dir
);
286 VOP_REMOVE(dvp
, dentry
, NULL
, error
);
288 validate_fields(dir
); /* For size only */
289 validate_fields(inode
);
298 struct dentry
*dentry
,
303 vnode_t
*dvp
; /* directory containing name of symlink */
304 vnode_t
*cvp
; /* used to lookup symlink to put in dentry */
307 dvp
= LINVFS_GET_VP(dir
);
310 memset(&va
, 0, sizeof(va
));
312 va
.va_mode
= irix_symlink_mode
? 0777 & ~current
->fs
->umask
: S_IRWXUGO
;
313 va
.va_mask
= XFS_AT_TYPE
|XFS_AT_MODE
;
316 VOP_SYMLINK(dvp
, dentry
, &va
, (char *)symname
, &cvp
, NULL
, error
);
318 ASSERT(cvp
->v_type
== VLNK
);
319 ip
= LINVFS_GET_IP(cvp
);
320 d_instantiate(dentry
, ip
);
321 validate_fields(dir
);
322 validate_fields(ip
); /* size needs update */
330 struct dentry
*dentry
)
332 struct inode
*inode
= dentry
->d_inode
;
333 vnode_t
*dvp
= LINVFS_GET_VP(dir
);
336 VOP_RMDIR(dvp
, dentry
, NULL
, error
);
338 validate_fields(inode
);
339 validate_fields(dir
);
347 struct dentry
*odentry
,
349 struct dentry
*ndentry
)
351 struct inode
*new_inode
= ndentry
->d_inode
;
352 vnode_t
*fvp
; /* from directory */
353 vnode_t
*tvp
; /* target directory */
356 fvp
= LINVFS_GET_VP(odir
);
357 tvp
= LINVFS_GET_VP(ndir
);
359 VOP_RENAME(fvp
, odentry
, tvp
, ndentry
, NULL
, error
);
364 validate_fields(new_inode
);
366 validate_fields(odir
);
368 validate_fields(ndir
);
374 struct dentry
*dentry
,
378 vnode_t
*vp
= LINVFS_GET_VP(dentry
->d_inode
);
388 uio
.uio_segflg
= UIO_USERSPACE
;
389 uio
.uio_resid
= size
;
392 VOP_READLINK(vp
, &uio
, 0, NULL
, error
);
396 return (size
- uio
.uio_resid
);
400 * careful here - this function can get called recursively, so
401 * we need to be very careful about how much stack we use.
402 * uio is kmalloced for this reason...
406 struct dentry
*dentry
,
407 struct nameidata
*nd
)
418 link
= (char *)kmalloc(MAXNAMELEN
+1, GFP_KERNEL
);
420 nd_set_link(nd
, ERR_PTR(-ENOMEM
));
424 uio
= (uio_t
*)kmalloc(sizeof(uio_t
), GFP_KERNEL
);
427 nd_set_link(nd
, ERR_PTR(-ENOMEM
));
431 vp
= LINVFS_GET_VP(dentry
->d_inode
);
434 iov
.iov_len
= MAXNAMELEN
;
438 uio
->uio_segflg
= UIO_SYSSPACE
;
439 uio
->uio_resid
= MAXNAMELEN
;
442 VOP_READLINK(vp
, uio
, 0, NULL
, error
);
445 link
= ERR_PTR(-error
);
447 link
[MAXNAMELEN
- uio
->uio_resid
] = '\0';
451 nd_set_link(nd
, link
);
455 static void linvfs_put_link(struct dentry
*dentry
, struct nameidata
*nd
)
457 char *s
= nd_get_link(nd
);
462 #ifdef CONFIG_XFS_POSIX_ACL
467 struct nameidata
*nd
)
469 vnode_t
*vp
= LINVFS_GET_VP(inode
);
472 mode
<<= 6; /* convert from linux to vnode access bits */
473 VOP_ACCESS(vp
, mode
, NULL
, error
);
477 #define linvfs_permission NULL
482 struct vfsmount
*mnt
,
483 struct dentry
*dentry
,
486 struct inode
*inode
= dentry
->d_inode
;
487 vnode_t
*vp
= LINVFS_GET_VP(inode
);
490 if (unlikely(vp
->v_flag
& VMODIFIED
))
491 error
= vn_revalidate(vp
);
493 generic_fillattr(inode
, stat
);
499 struct dentry
*dentry
,
502 struct inode
*inode
= dentry
->d_inode
;
503 unsigned int ia_valid
= attr
->ia_valid
;
504 vnode_t
*vp
= LINVFS_GET_VP(inode
);
509 memset(&vattr
, 0, sizeof(vattr_t
));
510 if (ia_valid
& ATTR_UID
) {
511 vattr
.va_mask
|= XFS_AT_UID
;
512 vattr
.va_uid
= attr
->ia_uid
;
514 if (ia_valid
& ATTR_GID
) {
515 vattr
.va_mask
|= XFS_AT_GID
;
516 vattr
.va_gid
= attr
->ia_gid
;
518 if (ia_valid
& ATTR_SIZE
) {
519 vattr
.va_mask
|= XFS_AT_SIZE
;
520 vattr
.va_size
= attr
->ia_size
;
522 if (ia_valid
& ATTR_ATIME
) {
523 vattr
.va_mask
|= XFS_AT_ATIME
;
524 vattr
.va_atime
= attr
->ia_atime
;
526 if (ia_valid
& ATTR_MTIME
) {
527 vattr
.va_mask
|= XFS_AT_MTIME
;
528 vattr
.va_mtime
= attr
->ia_mtime
;
530 if (ia_valid
& ATTR_CTIME
) {
531 vattr
.va_mask
|= XFS_AT_CTIME
;
532 vattr
.va_ctime
= attr
->ia_ctime
;
534 if (ia_valid
& ATTR_MODE
) {
535 vattr
.va_mask
|= XFS_AT_MODE
;
536 vattr
.va_mode
= attr
->ia_mode
;
537 if (!in_group_p(inode
->i_gid
) && !capable(CAP_FSETID
))
538 inode
->i_mode
&= ~S_ISGID
;
541 if (ia_valid
& (ATTR_MTIME_SET
| ATTR_ATIME_SET
))
544 if ((ia_valid
& ATTR_NO_BLOCK
))
545 flags
|= ATTR_NONBLOCK
;
548 VOP_SETATTR(vp
, &vattr
, flags
, NULL
, error
);
559 block_truncate_page(inode
->i_mapping
, inode
->i_size
, linvfs_get_block
);
564 struct dentry
*dentry
,
570 vnode_t
*vp
= LINVFS_GET_VP(dentry
->d_inode
);
571 char *attr
= (char *)name
;
576 namesp
= attr_lookup_namespace(attr
, attr_namespaces
, ATTR_NAMECOUNT
);
579 attr
+= namesp
->attr_namelen
;
580 error
= namesp
->attr_capable(vp
, NULL
);
584 /* Convert Linux syscall to XFS internal ATTR flags */
585 if (flags
& XATTR_CREATE
)
586 xflags
|= ATTR_CREATE
;
587 if (flags
& XATTR_REPLACE
)
588 xflags
|= ATTR_REPLACE
;
589 xflags
|= namesp
->attr_flag
;
590 return namesp
->attr_set(vp
, attr
, (void *)data
, size
, xflags
);
595 struct dentry
*dentry
,
600 vnode_t
*vp
= LINVFS_GET_VP(dentry
->d_inode
);
601 char *attr
= (char *)name
;
606 namesp
= attr_lookup_namespace(attr
, attr_namespaces
, ATTR_NAMECOUNT
);
609 attr
+= namesp
->attr_namelen
;
610 error
= namesp
->attr_capable(vp
, NULL
);
614 /* Convert Linux syscall to XFS internal ATTR flags */
616 xflags
|= ATTR_KERNOVAL
;
619 xflags
|= namesp
->attr_flag
;
620 return namesp
->attr_get(vp
, attr
, (void *)data
, size
, xflags
);
625 struct dentry
*dentry
,
629 vnode_t
*vp
= LINVFS_GET_VP(dentry
->d_inode
);
630 int error
, xflags
= ATTR_KERNAMELS
;
634 xflags
|= ATTR_KERNOVAL
;
635 xflags
|= capable(CAP_SYS_ADMIN
) ? ATTR_KERNFULLS
: ATTR_KERNORMALS
;
637 error
= attr_generic_list(vp
, data
, size
, xflags
, &result
);
645 struct dentry
*dentry
,
648 vnode_t
*vp
= LINVFS_GET_VP(dentry
->d_inode
);
649 char *attr
= (char *)name
;
654 namesp
= attr_lookup_namespace(attr
, attr_namespaces
, ATTR_NAMECOUNT
);
657 attr
+= namesp
->attr_namelen
;
658 error
= namesp
->attr_capable(vp
, NULL
);
661 xflags
|= namesp
->attr_flag
;
662 return namesp
->attr_remove(vp
, attr
, xflags
);
666 struct inode_operations linvfs_file_inode_operations
= {
667 .permission
= linvfs_permission
,
668 .truncate
= linvfs_truncate
,
669 .getattr
= linvfs_getattr
,
670 .setattr
= linvfs_setattr
,
671 .setxattr
= linvfs_setxattr
,
672 .getxattr
= linvfs_getxattr
,
673 .listxattr
= linvfs_listxattr
,
674 .removexattr
= linvfs_removexattr
,
677 struct inode_operations linvfs_dir_inode_operations
= {
678 .create
= linvfs_create
,
679 .lookup
= linvfs_lookup
,
681 .unlink
= linvfs_unlink
,
682 .symlink
= linvfs_symlink
,
683 .mkdir
= linvfs_mkdir
,
684 .rmdir
= linvfs_rmdir
,
685 .mknod
= linvfs_mknod
,
686 .rename
= linvfs_rename
,
687 .permission
= linvfs_permission
,
688 .getattr
= linvfs_getattr
,
689 .setattr
= linvfs_setattr
,
690 .setxattr
= linvfs_setxattr
,
691 .getxattr
= linvfs_getxattr
,
692 .listxattr
= linvfs_listxattr
,
693 .removexattr
= linvfs_removexattr
,
696 struct inode_operations linvfs_symlink_inode_operations
= {
697 .readlink
= linvfs_readlink
,
698 .follow_link
= linvfs_follow_link
,
699 .put_link
= linvfs_put_link
,
700 .permission
= linvfs_permission
,
701 .getattr
= linvfs_getattr
,
702 .setattr
= linvfs_setattr
,
703 .setxattr
= linvfs_setxattr
,
704 .getxattr
= linvfs_getxattr
,
705 .listxattr
= linvfs_listxattr
,
706 .removexattr
= linvfs_removexattr
,