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;
177 remove_inode_hash(ip);
180 VOP_RMDIR(dvp, &teardown, NULL, err2);
182 VOP_REMOVE(dvp, &teardown, NULL, err2);
186 _ACL_FREE(default_acl);
191 ip = LINVFS_GET_IP(vp);
193 if (S_ISCHR(mode) || S_ISBLK(mode))
195 else if (S_ISDIR(mode))
197 d_instantiate(dentry, ip);
198 validate_fields(dir);
206 struct dentry *dentry,
208 struct nameidata *nd)
210 return linvfs_mknod(dir, dentry, mode, 0);
216 struct dentry *dentry,
219 return linvfs_mknod(dir, dentry, mode|S_IFDIR, 0);
222 STATIC struct dentry *
225 struct dentry *dentry,
226 struct nameidata *nd)
228 struct inode *ip = NULL;
229 vnode_t *vp, *cvp = NULL;
232 if (dentry->d_name.len >= MAXNAMELEN)
233 return ERR_PTR(-ENAMETOOLONG);
235 vp = LINVFS_GET_VP(dir);
236 VOP_LOOKUP(vp, dentry, &cvp, 0, NULL, NULL, error);
239 ip = LINVFS_GET_IP(cvp);
242 return ERR_PTR(-EACCES);
245 if (error && (error != ENOENT))
246 return ERR_PTR(-error);
247 return d_splice_alias(ip, dentry);
252 struct dentry *old_dentry,
254 struct dentry *dentry)
256 struct inode *ip; /* inode of guy being linked to */
257 vnode_t *tdvp; /* target directory for new name/link */
258 vnode_t *vp; /* vp of name being linked */
261 ip = old_dentry->d_inode; /* inode being linked to */
262 if (S_ISDIR(ip->i_mode))
265 tdvp = LINVFS_GET_VP(dir);
266 vp = LINVFS_GET_VP(ip);
268 VOP_LINK(tdvp, vp, dentry, NULL, error);
273 d_instantiate(dentry, ip);
281 struct dentry *dentry)
284 vnode_t *dvp; /* directory containing name to remove */
287 inode = dentry->d_inode;
288 dvp = LINVFS_GET_VP(dir);
290 VOP_REMOVE(dvp, dentry, NULL, error);
292 validate_fields(dir); /* For size only */
293 validate_fields(inode);
302 struct dentry *dentry,
307 vnode_t *dvp; /* directory containing name to remove */
308 vnode_t *cvp; /* used to lookup symlink to put in dentry */
311 dvp = LINVFS_GET_VP(dir);
314 memset(&va, 0, sizeof(va));
316 va.va_mode = irix_symlink_mode ? 0777 & ~current->fs->umask : S_IRWXUGO;
317 va.va_mask = XFS_AT_TYPE|XFS_AT_MODE;
320 VOP_SYMLINK(dvp, dentry, &va, (char *)symname, &cvp, NULL, error);
322 ASSERT(cvp->v_type == VLNK);
323 ip = LINVFS_GET_IP(cvp);
324 d_instantiate(dentry, ip);
325 validate_fields(dir);
326 validate_fields(ip); /* size needs update */
334 struct dentry *dentry)
336 struct inode *inode = dentry->d_inode;
337 vnode_t *dvp = LINVFS_GET_VP(dir);
340 VOP_RMDIR(dvp, dentry, NULL, error);
342 validate_fields(inode);
343 validate_fields(dir);
351 struct dentry *odentry,
353 struct dentry *ndentry)
355 struct inode *new_inode = ndentry->d_inode;
356 vnode_t *fvp; /* from directory */
357 vnode_t *tvp; /* target directory */
360 fvp = LINVFS_GET_VP(odir);
361 tvp = LINVFS_GET_VP(ndir);
363 VOP_RENAME(fvp, odentry, tvp, ndentry, NULL, error);
368 validate_fields(new_inode);
370 validate_fields(odir);
372 validate_fields(ndir);
378 struct dentry *dentry,
382 vnode_t *vp = LINVFS_GET_VP(dentry->d_inode);
392 uio.uio_segflg = UIO_USERSPACE;
393 uio.uio_resid = size;
396 VOP_READLINK(vp, &uio, 0, NULL, error);
400 return (size - uio.uio_resid);
404 * careful here - this function can get called recursively, so
405 * we need to be very careful about how much stack we use.
406 * uio is kmalloced for this reason...
410 struct dentry *dentry,
411 struct nameidata *nd)
422 link = (char *)kmalloc(MAXNAMELEN+1, GFP_KERNEL);
424 nd_set_link(nd, ERR_PTR(-ENOMEM));
428 uio = (uio_t *)kmalloc(sizeof(uio_t), GFP_KERNEL);
431 nd_set_link(nd, ERR_PTR(-ENOMEM));
435 vp = LINVFS_GET_VP(dentry->d_inode);
438 iov.iov_len = MAXNAMELEN;
442 uio->uio_segflg = UIO_SYSSPACE;
443 uio->uio_resid = MAXNAMELEN;
446 VOP_READLINK(vp, uio, 0, NULL, error);
449 link = ERR_PTR(-error);
451 link[MAXNAMELEN - uio->uio_resid] = '\0';
455 nd_set_link(nd, link);
459 static void linvfs_put_link(struct dentry *dentry, struct nameidata *nd)
461 char *s = nd_get_link(nd);
466 #ifdef CONFIG_XFS_POSIX_ACL
471 struct nameidata *nd)
473 vnode_t *vp = LINVFS_GET_VP(inode);
476 mode <<= 6; /* convert from linux to vnode access bits */
477 VOP_ACCESS(vp, mode, NULL, error);
481 #define linvfs_permission NULL
486 struct vfsmount *mnt,
487 struct dentry *dentry,
490 struct inode *inode = dentry->d_inode;
491 vnode_t *vp = LINVFS_GET_VP(inode);
494 if (unlikely(vp->v_flag & VMODIFIED))
495 error = vn_revalidate(vp);
497 generic_fillattr(inode, stat);
502 linvfs_setattr_flags(
506 unsigned int oldflags, newflags;
508 oldflags = vap->va_xflags;
509 newflags = oldflags & ~(XFS_XFLAG_IMMUTABLE |
510 XFS_XFLAG_IUNLINK | XFS_XFLAG_BARRIER);
511 if (flags & ATTR_FLAG_IMMUTABLE)
512 newflags |= XFS_XFLAG_IMMUTABLE;
513 if (flags & ATTR_FLAG_IUNLINK)
514 newflags |= XFS_XFLAG_IUNLINK;
515 if (flags & ATTR_FLAG_BARRIER)
516 newflags |= XFS_XFLAG_BARRIER;
518 if (oldflags ^ newflags)
519 vap->va_xflags = newflags;
525 struct dentry *dentry,
528 struct inode *inode = dentry->d_inode;
529 unsigned int ia_valid = attr->ia_valid;
530 vnode_t *vp = LINVFS_GET_VP(inode);
535 memset(&vattr, 0, sizeof(vattr_t));
536 if (ia_valid & ATTR_UID) {
537 vattr.va_mask |= XFS_AT_UID;
538 vattr.va_uid = attr->ia_uid;
540 if (ia_valid & ATTR_GID) {
541 vattr.va_mask |= XFS_AT_GID;
542 vattr.va_gid = attr->ia_gid;
544 if (ia_valid & ATTR_SIZE) {
545 vattr.va_mask |= XFS_AT_SIZE;
546 vattr.va_size = attr->ia_size;
548 if (ia_valid & ATTR_ATIME) {
549 vattr.va_mask |= XFS_AT_ATIME;
550 vattr.va_atime = attr->ia_atime;
552 if (ia_valid & ATTR_MTIME) {
553 vattr.va_mask |= XFS_AT_MTIME;
554 vattr.va_mtime = attr->ia_mtime;
556 if (ia_valid & ATTR_CTIME) {
557 vattr.va_mask |= XFS_AT_CTIME;
558 vattr.va_ctime = attr->ia_ctime;
560 if (ia_valid & ATTR_MODE) {
561 vattr.va_mask |= XFS_AT_MODE;
562 vattr.va_mode = attr->ia_mode;
563 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
564 inode->i_mode &= ~S_ISGID;
567 if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))
570 if ((ia_valid & ATTR_NO_BLOCK))
571 flags |= ATTR_NONBLOCK;
574 if (ia_valid & ATTR_ATTR_FLAG) {
575 vattr.va_mask |= XFS_AT_XFLAGS;
576 linvfs_setattr_flags(&vattr, attr->ia_attr_flags);
579 VOP_SETATTR(vp, &vattr, flags, NULL, error);
590 block_truncate_page(inode->i_mapping, inode->i_size, linvfs_get_block);
595 struct dentry *dentry,
601 vnode_t *vp = LINVFS_GET_VP(dentry->d_inode);
602 char *attr = (char *)name;
607 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
610 attr += namesp->attr_namelen;
611 error = namesp->attr_capable(vp, NULL);
615 /* Convert Linux syscall to XFS internal ATTR flags */
616 if (flags & XATTR_CREATE)
617 xflags |= ATTR_CREATE;
618 if (flags & XATTR_REPLACE)
619 xflags |= ATTR_REPLACE;
620 xflags |= namesp->attr_flag;
621 return namesp->attr_set(vp, attr, (void *)data, size, xflags);
626 struct dentry *dentry,
631 vnode_t *vp = LINVFS_GET_VP(dentry->d_inode);
632 char *attr = (char *)name;
637 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
640 attr += namesp->attr_namelen;
641 error = namesp->attr_capable(vp, NULL);
645 /* Convert Linux syscall to XFS internal ATTR flags */
647 xflags |= ATTR_KERNOVAL;
650 xflags |= namesp->attr_flag;
651 return namesp->attr_get(vp, attr, (void *)data, size, xflags);
656 struct dentry *dentry,
660 vnode_t *vp = LINVFS_GET_VP(dentry->d_inode);
661 int error, xflags = ATTR_KERNAMELS;
665 xflags |= ATTR_KERNOVAL;
666 xflags |= capable(CAP_SYS_ADMIN) ? ATTR_KERNFULLS : ATTR_KERNORMALS;
668 error = attr_generic_list(vp, data, size, xflags, &result);
676 struct dentry *dentry,
679 vnode_t *vp = LINVFS_GET_VP(dentry->d_inode);
680 char *attr = (char *)name;
685 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
688 attr += namesp->attr_namelen;
689 error = namesp->attr_capable(vp, NULL);
692 xflags |= namesp->attr_flag;
693 return namesp->attr_remove(vp, attr, xflags);
697 struct inode_operations linvfs_file_inode_operations = {
698 .permission = linvfs_permission,
699 .truncate = linvfs_truncate,
700 .getattr = linvfs_getattr,
701 .setattr = linvfs_setattr,
702 .setxattr = linvfs_setxattr,
703 .getxattr = linvfs_getxattr,
704 .listxattr = linvfs_listxattr,
705 .removexattr = linvfs_removexattr,
708 struct inode_operations linvfs_dir_inode_operations = {
709 .create = linvfs_create,
710 .lookup = linvfs_lookup,
712 .unlink = linvfs_unlink,
713 .symlink = linvfs_symlink,
714 .mkdir = linvfs_mkdir,
715 .rmdir = linvfs_rmdir,
716 .mknod = linvfs_mknod,
717 .rename = linvfs_rename,
718 .permission = linvfs_permission,
719 .getattr = linvfs_getattr,
720 .setattr = linvfs_setattr,
721 .setxattr = linvfs_setxattr,
722 .getxattr = linvfs_getxattr,
723 .listxattr = linvfs_listxattr,
724 .removexattr = linvfs_removexattr,
727 struct inode_operations linvfs_symlink_inode_operations = {
728 .readlink = linvfs_readlink,
729 .follow_link = linvfs_follow_link,
730 .put_link = linvfs_put_link,
731 .permission = linvfs_permission,
732 .getattr = linvfs_getattr,
733 .setattr = linvfs_setattr,
734 .setxattr = linvfs_setxattr,
735 .getxattr = linvfs_getxattr,
736 .listxattr = linvfs_listxattr,
737 .removexattr = linvfs_removexattr,