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/capability.h>
55 #include <linux/xattr.h>
56 #include <linux/namei.h>
57 #include <linux/security.h>
58 #include <linux/vserver/xid.h>
61 * Get a XFS inode from a given vnode.
69 bdp = bhv_lookup_range(VN_BHV_HEAD(vp),
70 VNODE_POSITION_XFS, VNODE_POSITION_XFS);
71 if (unlikely(bdp == NULL))
73 return XFS_BHVTOI(bdp);
77 * Bring the atime in the XFS inode uptodate.
78 * Used before logging the inode to disk or when the Linux inode goes away.
81 xfs_synchronize_atime(
86 vp = XFS_ITOV_NULL(ip);
88 struct inode *inode = &vp->v_inode;
89 ip->i_d.di_atime.t_sec = (__int32_t)inode->i_atime.tv_sec;
90 ip->i_d.di_atime.t_nsec = (__int32_t)inode->i_atime.tv_nsec;
95 * Change the requested timestamp in the given inode.
96 * We don't lock across timestamp updates, and we don't log them but
97 * we do record the fact that there is dirty information in core.
99 * NOTE -- callers MUST combine XFS_ICHGTIME_MOD or XFS_ICHGTIME_CHG
100 * with XFS_ICHGTIME_ACC to be sure that access time
101 * update will take. Calling first with XFS_ICHGTIME_ACC
102 * and then XFS_ICHGTIME_MOD may fail to modify the access
103 * timestamp if the filesystem is mounted noacctm.
110 struct inode *inode = LINVFS_GET_IP(XFS_ITOV(ip));
114 if (flags & XFS_ICHGTIME_MOD) {
116 ip->i_d.di_mtime.t_sec = (__int32_t)tv.tv_sec;
117 ip->i_d.di_mtime.t_nsec = (__int32_t)tv.tv_nsec;
119 if (flags & XFS_ICHGTIME_ACC) {
121 ip->i_d.di_atime.t_sec = (__int32_t)tv.tv_sec;
122 ip->i_d.di_atime.t_nsec = (__int32_t)tv.tv_nsec;
124 if (flags & XFS_ICHGTIME_CHG) {
126 ip->i_d.di_ctime.t_sec = (__int32_t)tv.tv_sec;
127 ip->i_d.di_ctime.t_nsec = (__int32_t)tv.tv_nsec;
131 * We update the i_update_core field _after_ changing
132 * the timestamps in order to coordinate properly with
133 * xfs_iflush() so that we don't lose timestamp updates.
134 * This keeps us from having to hold the inode lock
135 * while doing this. We use the SYNCHRONIZE macro to
136 * ensure that the compiler does not reorder the update
137 * of i_update_core above the timestamp updates above.
140 ip->i_update_core = 1;
141 if (!(inode->i_state & I_LOCK))
142 mark_inode_dirty_sync(inode);
146 * Variant on the above which avoids querying the system clock
147 * in situations where we know the Linux inode timestamps have
148 * just been updated (and so we can update our inode cheaply).
159 * Atime updates for read() & friends are handled lazily now, and
160 * explicit updates must go through xfs_ichgtime()
162 ASSERT((flags & XFS_ICHGTIME_ACC) == 0);
165 * We're not supposed to change timestamps in readonly-mounted
166 * filesystems. Throw it away if anyone asks us.
168 if (unlikely(IS_RDONLY(inode)))
171 if (flags & XFS_ICHGTIME_MOD) {
172 tvp = &inode->i_mtime;
173 ip->i_d.di_mtime.t_sec = (__int32_t)tvp->tv_sec;
174 ip->i_d.di_mtime.t_nsec = (__int32_t)tvp->tv_nsec;
176 if (flags & XFS_ICHGTIME_CHG) {
177 tvp = &inode->i_ctime;
178 ip->i_d.di_ctime.t_sec = (__int32_t)tvp->tv_sec;
179 ip->i_d.di_ctime.t_nsec = (__int32_t)tvp->tv_nsec;
183 * We update the i_update_core field _after_ changing
184 * the timestamps in order to coordinate properly with
185 * xfs_iflush() so that we don't lose timestamp updates.
186 * This keeps us from having to hold the inode lock
187 * while doing this. We use the SYNCHRONIZE macro to
188 * ensure that the compiler does not reorder the update
189 * of i_update_core above the timestamp updates above.
192 ip->i_update_core = 1;
193 if (!(inode->i_state & I_LOCK))
194 mark_inode_dirty_sync(inode);
199 * Pull the link count and size up from the xfs inode to the linux inode
205 vnode_t *vp = LINVFS_GET_VP(ip);
209 va.va_mask = XFS_AT_NLINK|XFS_AT_SIZE|XFS_AT_NBLOCKS;
210 VOP_GETATTR(vp, &va, ATTR_LAZY, NULL, error);
211 if (likely(!error)) {
212 ip->i_nlink = va.va_nlink;
213 ip->i_blocks = va.va_nblocks;
215 /* we're under i_mutex so i_size can't change under us */
216 if (i_size_read(ip) != va.va_size)
217 i_size_write(ip, va.va_size);
222 * Hook in SELinux. This is not quite correct yet, what we really need
223 * here (as we do for default ACLs) is a mechanism by which creation of
224 * these attrs can be journalled at inode creation time (along with the
225 * inode, of course, such that log replay can't cause these to be lost).
228 linvfs_init_security(
232 struct inode *ip = LINVFS_GET_IP(vp);
238 error = security_inode_init_security(ip, dir, &name, &value, &length);
240 if (error == -EOPNOTSUPP)
245 VOP_ATTR_SET(vp, name, value, length, ATTR_SECURE, NULL, error);
255 * Determine whether a process has a valid fs_struct (kernel daemons
256 * like knfsd don't have an fs_struct).
258 * XXX(hch): nfsd is broken, better fix it instead.
261 has_fs_struct(struct task_struct *task)
263 return (task->fs != init_task.fs);
270 struct dentry *dentry,
273 struct dentry teardown = {};
277 * If we can't add the ACL or we fail in
278 * linvfs_init_security we must back out.
279 * ENOSPC can hit here, among other things.
281 teardown.d_inode = LINVFS_GET_IP(vp);
282 teardown.d_name = dentry->d_name;
285 VOP_RMDIR(dvp, &teardown, NULL, err2);
287 VOP_REMOVE(dvp, &teardown, NULL, err2);
294 struct dentry *dentry,
300 vnode_t *vp = NULL, *dvp = LINVFS_GET_VP(dir);
301 xfs_acl_t *default_acl = NULL;
302 attrexists_t test_default_acl = _ACL_DEFAULT_EXISTS;
306 * Irix uses Missed'em'V split, but doesn't want to see
307 * the upper 5 bits of (14bit) major.
309 if (!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff)
312 if (test_default_acl && test_default_acl(dvp)) {
313 if (!_ACL_ALLOC(default_acl))
315 if (!_ACL_GET_DEFAULT(dvp, default_acl)) {
316 _ACL_FREE(default_acl);
321 if (IS_POSIXACL(dir) && !default_acl && has_fs_struct(current))
322 mode &= ~current->fs->umask;
324 memset(&va, 0, sizeof(va));
325 va.va_mask = XFS_AT_TYPE|XFS_AT_MODE;
328 switch (mode & S_IFMT) {
329 case S_IFCHR: case S_IFBLK: case S_IFIFO: case S_IFSOCK:
330 va.va_rdev = sysv_encode_dev(rdev);
331 va.va_mask |= XFS_AT_RDEV;
334 VOP_CREATE(dvp, dentry, &va, &vp, NULL, error);
337 VOP_MKDIR(dvp, dentry, &va, &vp, NULL, error);
346 error = linvfs_init_security(vp, dir);
348 cleanup_inode(dvp, vp, dentry, mode);
353 error = _ACL_INHERIT(vp, &va, default_acl);
357 cleanup_inode(dvp, vp, dentry, mode);
359 _ACL_FREE(default_acl);
364 ip = LINVFS_GET_IP(vp);
366 if (S_ISCHR(mode) || S_ISBLK(mode))
368 else if (S_ISDIR(mode))
370 d_instantiate(dentry, ip);
371 validate_fields(dir);
379 struct dentry *dentry,
381 struct nameidata *nd)
383 return linvfs_mknod(dir, dentry, mode, 0);
389 struct dentry *dentry,
392 return linvfs_mknod(dir, dentry, mode|S_IFDIR, 0);
395 STATIC struct dentry *
398 struct dentry *dentry,
399 struct nameidata *nd)
401 struct vnode *vp = LINVFS_GET_VP(dir), *cvp;
404 if (dentry->d_name.len >= MAXNAMELEN)
405 return ERR_PTR(-ENAMETOOLONG);
407 VOP_LOOKUP(vp, dentry, &cvp, 0, NULL, NULL, error);
409 if (unlikely(error != ENOENT))
410 return ERR_PTR(-error);
414 vx_propagate_xid(nd, LINVFS_GET_IP(cvp));
416 return d_splice_alias(LINVFS_GET_IP(cvp), dentry);
421 struct dentry *old_dentry,
423 struct dentry *dentry)
425 struct inode *ip; /* inode of guy being linked to */
426 vnode_t *tdvp; /* target directory for new name/link */
427 vnode_t *vp; /* vp of name being linked */
430 ip = old_dentry->d_inode; /* inode being linked to */
431 if (S_ISDIR(ip->i_mode))
434 tdvp = LINVFS_GET_VP(dir);
435 vp = LINVFS_GET_VP(ip);
437 VOP_LINK(tdvp, vp, dentry, NULL, error);
442 d_instantiate(dentry, ip);
450 struct dentry *dentry)
453 vnode_t *dvp; /* directory containing name to remove */
456 inode = dentry->d_inode;
457 dvp = LINVFS_GET_VP(dir);
459 VOP_REMOVE(dvp, dentry, NULL, error);
461 validate_fields(dir); /* For size only */
462 validate_fields(inode);
471 struct dentry *dentry,
476 vnode_t *dvp; /* directory containing name of symlink */
477 vnode_t *cvp; /* used to lookup symlink to put in dentry */
480 dvp = LINVFS_GET_VP(dir);
483 memset(&va, 0, sizeof(va));
484 va.va_mode = S_IFLNK |
485 (irix_symlink_mode ? 0777 & ~current->fs->umask : S_IRWXUGO);
486 va.va_mask = XFS_AT_TYPE|XFS_AT_MODE;
489 VOP_SYMLINK(dvp, dentry, &va, (char *)symname, &cvp, NULL, error);
490 if (likely(!error && cvp)) {
491 error = linvfs_init_security(cvp, dir);
492 if (likely(!error)) {
493 ip = LINVFS_GET_IP(cvp);
494 d_instantiate(dentry, ip);
495 validate_fields(dir);
505 struct dentry *dentry)
507 struct inode *inode = dentry->d_inode;
508 vnode_t *dvp = LINVFS_GET_VP(dir);
511 VOP_RMDIR(dvp, dentry, NULL, error);
513 validate_fields(inode);
514 validate_fields(dir);
522 struct dentry *odentry,
524 struct dentry *ndentry)
526 struct inode *new_inode = ndentry->d_inode;
527 vnode_t *fvp; /* from directory */
528 vnode_t *tvp; /* target directory */
531 fvp = LINVFS_GET_VP(odir);
532 tvp = LINVFS_GET_VP(ndir);
534 VOP_RENAME(fvp, odentry, tvp, ndentry, NULL, error);
539 validate_fields(new_inode);
541 validate_fields(odir);
543 validate_fields(ndir);
548 * careful here - this function can get called recursively, so
549 * we need to be very careful about how much stack we use.
550 * uio is kmalloced for this reason...
554 struct dentry *dentry,
555 struct nameidata *nd)
566 link = (char *)kmalloc(MAXPATHLEN+1, GFP_KERNEL);
568 nd_set_link(nd, ERR_PTR(-ENOMEM));
572 uio = (uio_t *)kmalloc(sizeof(uio_t), GFP_KERNEL);
575 nd_set_link(nd, ERR_PTR(-ENOMEM));
579 vp = LINVFS_GET_VP(dentry->d_inode);
582 iov.iov_len = MAXPATHLEN;
586 uio->uio_segflg = UIO_SYSSPACE;
587 uio->uio_resid = MAXPATHLEN;
590 VOP_READLINK(vp, uio, 0, NULL, error);
593 link = ERR_PTR(-error);
595 link[MAXPATHLEN - uio->uio_resid] = '\0';
599 nd_set_link(nd, link);
605 struct dentry *dentry,
606 struct nameidata *nd,
609 char *s = nd_get_link(nd);
615 #ifdef CONFIG_XFS_POSIX_ACL
620 struct nameidata *nd)
622 vnode_t *vp = LINVFS_GET_VP(inode);
625 mode <<= 6; /* convert from linux to vnode access bits */
626 VOP_ACCESS(vp, mode, NULL, error);
630 #define linvfs_permission NULL
635 struct vfsmount *mnt,
636 struct dentry *dentry,
639 struct inode *inode = dentry->d_inode;
640 vnode_t *vp = LINVFS_GET_VP(inode);
643 if (unlikely(vp->v_flag & VMODIFIED))
644 error = vn_revalidate(vp);
646 generic_fillattr(inode, stat);
651 linvfs_sync_flags(struct inode *inode)
653 unsigned int oldflags, newflags;
657 vnode_t *vp = LINVFS_GET_VP(inode);
659 memset(&vattr, 0, sizeof(vattr_t));
661 vattr.va_mask = XFS_AT_XFLAGS;
662 VOP_GETATTR(vp, &vattr, 0, NULL, error);
665 oldflags = vattr.va_xflags;
666 newflags = oldflags & ~(XFS_XFLAG_IMMUTABLE |
667 XFS_XFLAG_IUNLINK | XFS_XFLAG_BARRIER);
669 if (IS_IMMUTABLE(inode))
670 newflags |= XFS_XFLAG_IMMUTABLE;
671 if (IS_IUNLINK(inode))
672 newflags |= XFS_XFLAG_IUNLINK;
673 if (IS_BARRIER(inode))
674 newflags |= XFS_XFLAG_BARRIER;
676 if (oldflags ^ newflags) {
677 vattr.va_xflags = newflags;
678 vattr.va_mask |= XFS_AT_XFLAGS;
679 VOP_SETATTR(vp, &vattr, flags, NULL, error);
687 struct dentry *dentry,
690 struct inode *inode = dentry->d_inode;
691 unsigned int ia_valid = attr->ia_valid;
692 vnode_t *vp = LINVFS_GET_VP(inode);
697 error = inode_change_ok(inode, attr);
701 memset(&vattr, 0, sizeof(vattr_t));
702 if (ia_valid & ATTR_UID) {
703 vattr.va_mask |= XFS_AT_UID;
704 vattr.va_uid = attr->ia_uid;
706 if (ia_valid & ATTR_GID) {
707 vattr.va_mask |= XFS_AT_GID;
708 vattr.va_gid = attr->ia_gid;
710 if ((ia_valid & ATTR_XID) && IS_TAGXID(inode)) {
711 vattr.va_mask |= XFS_AT_XID;
712 vattr.va_xid = attr->ia_xid;
714 if (ia_valid & ATTR_SIZE) {
715 vattr.va_mask |= XFS_AT_SIZE;
716 vattr.va_size = attr->ia_size;
718 if (ia_valid & ATTR_ATIME) {
719 vattr.va_mask |= XFS_AT_ATIME;
720 vattr.va_atime = attr->ia_atime;
721 inode->i_atime = attr->ia_atime;
723 if (ia_valid & ATTR_MTIME) {
724 vattr.va_mask |= XFS_AT_MTIME;
725 vattr.va_mtime = attr->ia_mtime;
727 if (ia_valid & ATTR_CTIME) {
728 vattr.va_mask |= XFS_AT_CTIME;
729 vattr.va_ctime = attr->ia_ctime;
731 if (ia_valid & ATTR_MODE) {
732 vattr.va_mask |= XFS_AT_MODE;
733 vattr.va_mode = attr->ia_mode;
734 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
735 inode->i_mode &= ~S_ISGID;
738 if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))
741 if ((ia_valid & ATTR_NO_BLOCK))
742 flags |= ATTR_NONBLOCK;
745 VOP_SETATTR(vp, &vattr, flags, NULL, error);
756 block_truncate_page(inode->i_mapping, inode->i_size, linvfs_get_block);
761 struct dentry *dentry,
767 vnode_t *vp = LINVFS_GET_VP(dentry->d_inode);
768 char *attr = (char *)name;
773 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
776 attr += namesp->attr_namelen;
777 error = namesp->attr_capable(vp, NULL);
781 /* Convert Linux syscall to XFS internal ATTR flags */
782 if (flags & XATTR_CREATE)
783 xflags |= ATTR_CREATE;
784 if (flags & XATTR_REPLACE)
785 xflags |= ATTR_REPLACE;
786 xflags |= namesp->attr_flag;
787 return namesp->attr_set(vp, attr, (void *)data, size, xflags);
792 struct dentry *dentry,
797 vnode_t *vp = LINVFS_GET_VP(dentry->d_inode);
798 char *attr = (char *)name;
803 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
806 attr += namesp->attr_namelen;
807 error = namesp->attr_capable(vp, NULL);
811 /* Convert Linux syscall to XFS internal ATTR flags */
813 xflags |= ATTR_KERNOVAL;
816 xflags |= namesp->attr_flag;
817 return namesp->attr_get(vp, attr, (void *)data, size, xflags);
822 struct dentry *dentry,
826 vnode_t *vp = LINVFS_GET_VP(dentry->d_inode);
827 int error, xflags = ATTR_KERNAMELS;
831 xflags |= ATTR_KERNOVAL;
832 xflags |= capable(CAP_SYS_ADMIN) ? ATTR_KERNFULLS : ATTR_KERNORMALS;
834 error = attr_generic_list(vp, data, size, xflags, &result);
842 struct dentry *dentry,
845 vnode_t *vp = LINVFS_GET_VP(dentry->d_inode);
846 char *attr = (char *)name;
851 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
854 attr += namesp->attr_namelen;
855 error = namesp->attr_capable(vp, NULL);
858 xflags |= namesp->attr_flag;
859 return namesp->attr_remove(vp, attr, xflags);
863 struct inode_operations linvfs_file_inode_operations = {
864 .permission = linvfs_permission,
865 .truncate = linvfs_truncate,
866 .getattr = linvfs_getattr,
867 .setattr = linvfs_setattr,
868 .setxattr = linvfs_setxattr,
869 .getxattr = linvfs_getxattr,
870 .listxattr = linvfs_listxattr,
871 .removexattr = linvfs_removexattr,
872 .sync_flags = linvfs_sync_flags,
875 struct inode_operations linvfs_dir_inode_operations = {
876 .create = linvfs_create,
877 .lookup = linvfs_lookup,
879 .unlink = linvfs_unlink,
880 .symlink = linvfs_symlink,
881 .mkdir = linvfs_mkdir,
882 .rmdir = linvfs_rmdir,
883 .mknod = linvfs_mknod,
884 .rename = linvfs_rename,
885 .permission = linvfs_permission,
886 .getattr = linvfs_getattr,
887 .setattr = linvfs_setattr,
888 .setxattr = linvfs_setxattr,
889 .getxattr = linvfs_getxattr,
890 .listxattr = linvfs_listxattr,
891 .removexattr = linvfs_removexattr,
892 .sync_flags = linvfs_sync_flags,
895 struct inode_operations linvfs_symlink_inode_operations = {
896 .readlink = generic_readlink,
897 .follow_link = linvfs_follow_link,
898 .put_link = linvfs_put_link,
899 .permission = linvfs_permission,
900 .getattr = linvfs_getattr,
901 .setattr = linvfs_setattr,
902 .setxattr = linvfs_setxattr,
903 .getxattr = linvfs_getxattr,
904 .listxattr = linvfs_listxattr,
905 .removexattr = linvfs_removexattr,
906 .sync_flags = linvfs_sync_flags,