2 * Copyright (c) 2000-2003 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>
73 * Pull the link count and size up from the xfs inode to the linux inode
79 vnode_t *vp = LINVFS_GET_VP(ip);
83 va.va_mask = XFS_AT_NLINK|XFS_AT_SIZE|XFS_AT_NBLOCKS;
84 VOP_GETATTR(vp, &va, ATTR_LAZY, NULL, error);
86 ip->i_nlink = va.va_nlink;
87 ip->i_blocks = va.va_nblocks;
89 /* we're under i_sem so i_size can't change under us */
90 if (i_size_read(ip) != va.va_size)
91 i_size_write(ip, va.va_size);
96 * Determine whether a process has a valid fs_struct (kernel daemons
97 * like knfsd don't have an fs_struct).
99 * XXX(hch): nfsd is broken, better fix it instead.
102 has_fs_struct(struct task_struct *task)
104 return (task->fs != init_task.fs);
110 struct dentry *dentry,
116 vnode_t *vp = NULL, *dvp = LINVFS_GET_VP(dir);
117 xfs_acl_t *default_acl = NULL;
118 attrexists_t test_default_acl = _ACL_DEFAULT_EXISTS;
122 * Irix uses Missed'em'V split, but doesn't want to see
123 * the upper 5 bits of (14bit) major.
125 if (!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff)
128 if (test_default_acl && test_default_acl(dvp)) {
129 if (!_ACL_ALLOC(default_acl))
131 if (!_ACL_GET_DEFAULT(dvp, default_acl)) {
132 _ACL_FREE(default_acl);
137 if (IS_POSIXACL(dir) && !default_acl && has_fs_struct(current))
138 mode &= ~current->fs->umask;
140 memset(&va, 0, sizeof(va));
141 va.va_mask = XFS_AT_TYPE|XFS_AT_MODE;
142 va.va_type = IFTOVT(mode);
145 switch (mode & S_IFMT) {
146 case S_IFCHR: case S_IFBLK: case S_IFIFO: case S_IFSOCK:
147 va.va_rdev = sysv_encode_dev(rdev);
148 va.va_mask |= XFS_AT_RDEV;
151 VOP_CREATE(dvp, dentry, &va, &vp, NULL, error);
154 VOP_MKDIR(dvp, dentry, &va, &vp, NULL, error);
163 error = _ACL_INHERIT(vp, &va, default_acl);
167 struct dentry teardown = {};
171 * If we can't add the ACL we must back out.
172 * ENOSPC can hit here, among other things.
174 teardown.d_inode = ip = LINVFS_GET_IP(vp);
175 teardown.d_name = dentry->d_name;
176 remove_inode_hash(ip);
179 VOP_RMDIR(dvp, &teardown, NULL, err2);
181 VOP_REMOVE(dvp, &teardown, NULL, err2);
185 _ACL_FREE(default_acl);
190 ip = LINVFS_GET_IP(vp);
192 if (S_ISCHR(mode) || S_ISBLK(mode))
194 else if (S_ISDIR(mode))
196 d_instantiate(dentry, ip);
197 validate_fields(dir);
205 struct dentry *dentry,
207 struct nameidata *nd)
209 return linvfs_mknod(dir, dentry, mode, 0);
215 struct dentry *dentry,
218 return linvfs_mknod(dir, dentry, mode|S_IFDIR, 0);
221 STATIC struct dentry *
224 struct dentry *dentry,
225 struct nameidata *nd)
227 struct inode *ip = NULL;
228 vnode_t *vp, *cvp = NULL;
231 if (dentry->d_name.len >= MAXNAMELEN)
232 return ERR_PTR(-ENAMETOOLONG);
234 vp = LINVFS_GET_VP(dir);
235 VOP_LOOKUP(vp, dentry, &cvp, 0, NULL, NULL, error);
238 ip = LINVFS_GET_IP(cvp);
241 return ERR_PTR(-EACCES);
244 if (error && (error != ENOENT))
245 return ERR_PTR(-error);
246 return d_splice_alias(ip, dentry);
251 struct dentry *old_dentry,
253 struct dentry *dentry)
255 struct inode *ip; /* inode of guy being linked to */
256 vnode_t *tdvp; /* target directory for new name/link */
257 vnode_t *vp; /* vp of name being linked */
260 ip = old_dentry->d_inode; /* inode being linked to */
261 if (S_ISDIR(ip->i_mode))
264 tdvp = LINVFS_GET_VP(dir);
265 vp = LINVFS_GET_VP(ip);
267 VOP_LINK(tdvp, vp, dentry, NULL, error);
272 d_instantiate(dentry, ip);
280 struct dentry *dentry)
283 vnode_t *dvp; /* directory containing name to remove */
286 inode = dentry->d_inode;
287 dvp = LINVFS_GET_VP(dir);
289 VOP_REMOVE(dvp, dentry, NULL, error);
291 validate_fields(dir); /* For size only */
292 validate_fields(inode);
301 struct dentry *dentry,
306 vnode_t *dvp; /* directory containing name to remove */
307 vnode_t *cvp; /* used to lookup symlink to put in dentry */
310 dvp = LINVFS_GET_VP(dir);
313 memset(&va, 0, sizeof(va));
315 va.va_mode = irix_symlink_mode ? 0777 & ~current->fs->umask : S_IRWXUGO;
316 va.va_mask = XFS_AT_TYPE|XFS_AT_MODE;
319 VOP_SYMLINK(dvp, dentry, &va, (char *)symname, &cvp, NULL, error);
321 ASSERT(cvp->v_type == VLNK);
322 ip = LINVFS_GET_IP(cvp);
323 d_instantiate(dentry, ip);
324 validate_fields(dir);
325 validate_fields(ip); /* size needs update */
333 struct dentry *dentry)
335 struct inode *inode = dentry->d_inode;
336 vnode_t *dvp = LINVFS_GET_VP(dir);
339 VOP_RMDIR(dvp, dentry, NULL, error);
341 validate_fields(inode);
342 validate_fields(dir);
350 struct dentry *odentry,
352 struct dentry *ndentry)
354 struct inode *new_inode = ndentry->d_inode;
355 vnode_t *fvp; /* from directory */
356 vnode_t *tvp; /* target directory */
359 fvp = LINVFS_GET_VP(odir);
360 tvp = LINVFS_GET_VP(ndir);
362 VOP_RENAME(fvp, odentry, tvp, ndentry, NULL, error);
367 validate_fields(new_inode);
369 validate_fields(odir);
371 validate_fields(ndir);
377 struct dentry *dentry,
381 vnode_t *vp = LINVFS_GET_VP(dentry->d_inode);
391 uio.uio_segflg = UIO_USERSPACE;
392 uio.uio_resid = size;
395 VOP_READLINK(vp, &uio, 0, NULL, error);
399 return (size - uio.uio_resid);
403 * careful here - this function can get called recursively, so
404 * we need to be very careful about how much stack we use.
405 * uio is kmalloced for this reason...
409 struct dentry *dentry,
410 struct nameidata *nd)
421 link = (char *)kmalloc(MAXNAMELEN+1, GFP_KERNEL);
425 uio = (uio_t *)kmalloc(sizeof(uio_t), GFP_KERNEL);
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);
449 link[MAXNAMELEN - uio->uio_resid] = '\0';
452 /* vfs_follow_link returns (-) errors */
453 error = vfs_follow_link(nd, link);
458 #ifdef CONFIG_XFS_POSIX_ACL
463 struct nameidata *nd)
465 vnode_t *vp = LINVFS_GET_VP(inode);
468 mode <<= 6; /* convert from linux to vnode access bits */
469 VOP_ACCESS(vp, mode, NULL, error);
473 #define linvfs_permission NULL
478 struct vfsmount *mnt,
479 struct dentry *dentry,
482 struct inode *inode = dentry->d_inode;
483 vnode_t *vp = LINVFS_GET_VP(inode);
486 if (unlikely(vp->v_flag & VMODIFIED))
487 error = vn_revalidate(vp);
489 generic_fillattr(inode, stat);
495 struct dentry *dentry,
498 struct inode *inode = dentry->d_inode;
499 unsigned int ia_valid = attr->ia_valid;
500 vnode_t *vp = LINVFS_GET_VP(inode);
505 memset(&vattr, 0, sizeof(vattr_t));
506 if (ia_valid & ATTR_UID) {
507 vattr.va_mask |= XFS_AT_UID;
508 vattr.va_uid = attr->ia_uid;
510 if (ia_valid & ATTR_GID) {
511 vattr.va_mask |= XFS_AT_GID;
512 vattr.va_gid = attr->ia_gid;
514 if (ia_valid & ATTR_SIZE) {
515 vattr.va_mask |= XFS_AT_SIZE;
516 vattr.va_size = attr->ia_size;
518 if (ia_valid & ATTR_ATIME) {
519 vattr.va_mask |= XFS_AT_ATIME;
520 vattr.va_atime = attr->ia_atime;
522 if (ia_valid & ATTR_MTIME) {
523 vattr.va_mask |= XFS_AT_MTIME;
524 vattr.va_mtime = attr->ia_mtime;
526 if (ia_valid & ATTR_CTIME) {
527 vattr.va_mask |= XFS_AT_CTIME;
528 vattr.va_ctime = attr->ia_ctime;
530 if (ia_valid & ATTR_MODE) {
531 vattr.va_mask |= XFS_AT_MODE;
532 vattr.va_mode = attr->ia_mode;
533 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
534 inode->i_mode &= ~S_ISGID;
537 if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))
540 if ((ia_valid & ATTR_NO_BLOCK))
541 flags |= ATTR_NONBLOCK;
544 VOP_SETATTR(vp, &vattr, flags, NULL, error);
546 return(-error); /* Positive error up from XFS */
547 if (ia_valid & ATTR_SIZE) {
548 error = vmtruncate(inode, attr->ia_size);
561 block_truncate_page(inode->i_mapping, inode->i_size, linvfs_get_block);
566 struct dentry *dentry,
572 vnode_t *vp = LINVFS_GET_VP(dentry->d_inode);
573 char *attr = (char *)name;
578 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
581 attr += namesp->attr_namelen;
582 error = namesp->attr_capable(vp, NULL);
586 /* Convert Linux syscall to XFS internal ATTR flags */
587 if (flags & XATTR_CREATE)
588 xflags |= ATTR_CREATE;
589 if (flags & XATTR_REPLACE)
590 xflags |= ATTR_REPLACE;
591 xflags |= namesp->attr_flag;
592 return namesp->attr_set(vp, attr, (void *)data, size, xflags);
597 struct dentry *dentry,
602 vnode_t *vp = LINVFS_GET_VP(dentry->d_inode);
603 char *attr = (char *)name;
608 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
611 attr += namesp->attr_namelen;
612 error = namesp->attr_capable(vp, NULL);
616 /* Convert Linux syscall to XFS internal ATTR flags */
618 xflags |= ATTR_KERNOVAL;
621 xflags |= namesp->attr_flag;
622 return namesp->attr_get(vp, attr, (void *)data, size, xflags);
627 struct dentry *dentry,
631 vnode_t *vp = LINVFS_GET_VP(dentry->d_inode);
632 int error, xflags = ATTR_KERNAMELS;
636 xflags |= ATTR_KERNOVAL;
637 xflags |= capable(CAP_SYS_ADMIN) ? ATTR_KERNFULLS : ATTR_KERNORMALS;
639 error = attr_generic_list(vp, data, size, xflags, &result);
647 struct dentry *dentry,
650 vnode_t *vp = LINVFS_GET_VP(dentry->d_inode);
651 char *attr = (char *)name;
656 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
659 attr += namesp->attr_namelen;
660 error = namesp->attr_capable(vp, NULL);
663 xflags |= namesp->attr_flag;
664 return namesp->attr_remove(vp, attr, xflags);
668 struct inode_operations linvfs_file_inode_operations = {
669 .permission = linvfs_permission,
670 .truncate = linvfs_truncate,
671 .getattr = linvfs_getattr,
672 .setattr = linvfs_setattr,
673 .setxattr = linvfs_setxattr,
674 .getxattr = linvfs_getxattr,
675 .listxattr = linvfs_listxattr,
676 .removexattr = linvfs_removexattr,
679 struct inode_operations linvfs_dir_inode_operations = {
680 .create = linvfs_create,
681 .lookup = linvfs_lookup,
683 .unlink = linvfs_unlink,
684 .symlink = linvfs_symlink,
685 .mkdir = linvfs_mkdir,
686 .rmdir = linvfs_rmdir,
687 .mknod = linvfs_mknod,
688 .rename = linvfs_rename,
689 .permission = linvfs_permission,
690 .getattr = linvfs_getattr,
691 .setattr = linvfs_setattr,
692 .setxattr = linvfs_setxattr,
693 .getxattr = linvfs_getxattr,
694 .listxattr = linvfs_listxattr,
695 .removexattr = linvfs_removexattr,
698 struct inode_operations linvfs_symlink_inode_operations = {
699 .readlink = linvfs_readlink,
700 .follow_link = linvfs_follow_link,
701 .permission = linvfs_permission,
702 .getattr = linvfs_getattr,
703 .setattr = linvfs_setattr,
704 .setxattr = linvfs_setxattr,
705 .getxattr = linvfs_getxattr,
706 .listxattr = linvfs_listxattr,
707 .removexattr = linvfs_removexattr,