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 = vn_to_inode(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
206 vnode_t *vp = vn_from_inode(ip);
209 vattr->va_mask = XFS_AT_NLINK|XFS_AT_SIZE|XFS_AT_NBLOCKS;
210 VOP_GETATTR(vp, vattr, ATTR_LAZY, NULL, error);
211 if (likely(!error)) {
212 ip->i_nlink = vattr->va_nlink;
213 ip->i_blocks = vattr->va_nblocks;
215 /* we're under i_sem so i_size can't change under us */
216 if (i_size_read(ip) != vattr->va_size)
217 i_size_write(ip, vattr->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).
232 struct inode *ip = vn_to_inode(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 xfs_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 * xfs_init_security we must back out.
279 * ENOSPC can hit here, among other things.
281 teardown.d_inode = vn_to_inode(vp);
282 teardown.d_name = dentry->d_name;
285 VOP_RMDIR(dvp, &teardown, NULL, error);
287 VOP_REMOVE(dvp, &teardown, NULL, error);
294 struct dentry *dentry,
299 vattr_t vattr = { 0 };
300 vnode_t *vp = NULL, *dvp = vn_from_inode(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 (unlikely(!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff))
312 if (unlikely(test_default_acl && test_default_acl(dvp))) {
313 if (!_ACL_ALLOC(default_acl)) {
316 if (!_ACL_GET_DEFAULT(dvp, default_acl)) {
317 _ACL_FREE(default_acl);
322 if (IS_POSIXACL(dir) && !default_acl && xfs_has_fs_struct(current))
323 mode &= ~current->fs->umask;
325 vattr.va_mask = XFS_AT_TYPE|XFS_AT_MODE;
326 vattr.va_mode = mode;
328 switch (mode & S_IFMT) {
329 case S_IFCHR: case S_IFBLK: case S_IFIFO: case S_IFSOCK:
330 vattr.va_rdev = sysv_encode_dev(rdev);
331 vattr.va_mask |= XFS_AT_RDEV;
334 VOP_CREATE(dvp, dentry, &vattr, &vp, NULL, error);
337 VOP_MKDIR(dvp, dentry, &vattr, &vp, NULL, error);
344 if (unlikely(!error)) {
345 error = xfs_init_security(vp, dir);
347 xfs_cleanup_inode(dvp, vp, dentry, mode);
350 if (unlikely(default_acl)) {
352 error = _ACL_INHERIT(vp, &vattr, default_acl);
356 xfs_cleanup_inode(dvp, vp, dentry, mode);
358 _ACL_FREE(default_acl);
361 if (likely(!error)) {
363 ip = vn_to_inode(vp);
365 if (S_ISCHR(mode) || S_ISBLK(mode))
367 else if (S_ISDIR(mode))
368 xfs_validate_fields(ip, &vattr);
369 d_instantiate(dentry, ip);
370 xfs_validate_fields(dir, &vattr);
378 struct dentry *dentry,
380 struct nameidata *nd)
382 return xfs_vn_mknod(dir, dentry, mode, 0);
388 struct dentry *dentry,
391 return xfs_vn_mknod(dir, dentry, mode|S_IFDIR, 0);
394 STATIC struct dentry *
397 struct dentry *dentry,
398 struct nameidata *nd)
400 struct vnode *vp = vn_from_inode(dir), *cvp;
403 if (dentry->d_name.len >= MAXNAMELEN)
404 return ERR_PTR(-ENAMETOOLONG);
406 VOP_LOOKUP(vp, dentry, &cvp, 0, NULL, NULL, error);
408 if (unlikely(error != ENOENT))
409 return ERR_PTR(-error);
413 vx_propagate_xid(nd, vn_to_inode(cvp));
415 return d_splice_alias(vn_to_inode(cvp), dentry);
420 struct dentry *old_dentry,
422 struct dentry *dentry)
424 struct inode *ip; /* inode of guy being linked to */
425 vnode_t *tdvp; /* target directory for new name/link */
426 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 = vn_from_inode(dir);
435 vp = vn_from_inode(ip);
437 VOP_LINK(tdvp, vp, dentry, NULL, error);
438 if (likely(!error)) {
441 xfs_validate_fields(ip, &vattr);
442 d_instantiate(dentry, ip);
450 struct dentry *dentry)
453 vnode_t *dvp; /* directory containing name to remove */
457 inode = dentry->d_inode;
458 dvp = vn_from_inode(dir);
460 VOP_REMOVE(dvp, dentry, NULL, error);
461 if (likely(!error)) {
462 xfs_validate_fields(dir, &vattr); /* size needs update */
463 xfs_validate_fields(inode, &vattr);
471 struct dentry *dentry,
475 vattr_t vattr = { 0 };
476 vnode_t *dvp; /* directory containing name of symlink */
477 vnode_t *cvp; /* used to lookup symlink to put in dentry */
480 dvp = vn_from_inode(dir);
483 vattr.va_mode = S_IFLNK |
484 (irix_symlink_mode ? 0777 & ~current->fs->umask : S_IRWXUGO);
485 vattr.va_mask = XFS_AT_TYPE|XFS_AT_MODE;
488 VOP_SYMLINK(dvp, dentry, &vattr, (char *)symname, &cvp, NULL, error);
489 if (likely(!error && cvp)) {
490 error = xfs_init_security(cvp, dir);
491 if (likely(!error)) {
492 ip = vn_to_inode(cvp);
493 d_instantiate(dentry, ip);
494 xfs_validate_fields(dir, &vattr);
495 xfs_validate_fields(ip, &vattr);
497 xfs_cleanup_inode(dvp, cvp, dentry, 0);
506 struct dentry *dentry)
508 struct inode *inode = dentry->d_inode;
509 vnode_t *dvp = vn_from_inode(dir);
513 VOP_RMDIR(dvp, dentry, NULL, error);
514 if (likely(!error)) {
515 xfs_validate_fields(inode, &vattr);
516 xfs_validate_fields(dir, &vattr);
524 struct dentry *odentry,
526 struct dentry *ndentry)
528 struct inode *new_inode = ndentry->d_inode;
529 vnode_t *fvp; /* from directory */
530 vnode_t *tvp; /* target directory */
534 fvp = vn_from_inode(odir);
535 tvp = vn_from_inode(ndir);
537 VOP_RENAME(fvp, odentry, tvp, ndentry, NULL, error);
538 if (likely(!error)) {
540 xfs_validate_fields(new_inode, &vattr);
541 xfs_validate_fields(odir, &vattr);
543 xfs_validate_fields(ndir, &vattr);
549 * careful here - this function can get called recursively, so
550 * we need to be very careful about how much stack we use.
551 * uio is kmalloced for this reason...
555 struct dentry *dentry,
556 struct nameidata *nd)
567 link = (char *)kmalloc(MAXPATHLEN+1, GFP_KERNEL);
569 nd_set_link(nd, ERR_PTR(-ENOMEM));
573 uio = (uio_t *)kmalloc(sizeof(uio_t), GFP_KERNEL);
576 nd_set_link(nd, ERR_PTR(-ENOMEM));
580 vp = vn_from_inode(dentry->d_inode);
583 iov.iov_len = MAXPATHLEN;
587 uio->uio_segflg = UIO_SYSSPACE;
588 uio->uio_resid = MAXPATHLEN;
591 VOP_READLINK(vp, uio, 0, NULL, error);
594 link = ERR_PTR(-error);
596 link[MAXPATHLEN - uio->uio_resid] = '\0';
600 nd_set_link(nd, link);
606 struct dentry *dentry,
607 struct nameidata *nd,
610 char *s = nd_get_link(nd);
616 #ifdef CONFIG_XFS_POSIX_ACL
621 struct nameidata *nd)
623 vnode_t *vp = vn_from_inode(inode);
626 mode <<= 6; /* convert from linux to vnode access bits */
627 VOP_ACCESS(vp, mode, NULL, error);
631 #define xfs_vn_permission NULL
636 struct vfsmount *mnt,
637 struct dentry *dentry,
640 struct inode *inode = dentry->d_inode;
641 vnode_t *vp = vn_from_inode(inode);
644 if (unlikely(vp->v_flag & VMODIFIED))
645 error = vn_revalidate(vp);
647 generic_fillattr(inode, stat);
653 struct dentry *dentry,
656 struct inode *inode = dentry->d_inode;
657 unsigned int ia_valid = attr->ia_valid;
658 vnode_t *vp = vn_from_inode(inode);
659 vattr_t vattr = { 0 };
663 error = inode_change_ok(inode, attr);
667 if (ia_valid & ATTR_UID) {
668 vattr.va_mask |= XFS_AT_UID;
669 vattr.va_uid = attr->ia_uid;
671 if (ia_valid & ATTR_GID) {
672 vattr.va_mask |= XFS_AT_GID;
673 vattr.va_gid = attr->ia_gid;
675 if ((ia_valid & ATTR_XID) && IS_TAGXID(inode)) {
676 vattr.va_mask |= XFS_AT_XID;
677 vattr.va_xid = attr->ia_xid;
679 if (ia_valid & ATTR_SIZE) {
680 vattr.va_mask |= XFS_AT_SIZE;
681 vattr.va_size = attr->ia_size;
683 if (ia_valid & ATTR_ATIME) {
684 vattr.va_mask |= XFS_AT_ATIME;
685 vattr.va_atime = attr->ia_atime;
686 inode->i_atime = attr->ia_atime;
688 if (ia_valid & ATTR_MTIME) {
689 vattr.va_mask |= XFS_AT_MTIME;
690 vattr.va_mtime = attr->ia_mtime;
692 if (ia_valid & ATTR_CTIME) {
693 vattr.va_mask |= XFS_AT_CTIME;
694 vattr.va_ctime = attr->ia_ctime;
696 if (ia_valid & ATTR_MODE) {
697 vattr.va_mask |= XFS_AT_MODE;
698 vattr.va_mode = attr->ia_mode;
699 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
700 inode->i_mode &= ~S_ISGID;
703 if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))
706 if ((ia_valid & ATTR_NO_BLOCK))
707 flags |= ATTR_NONBLOCK;
710 VOP_SETATTR(vp, &vattr, flags, NULL, error);
712 __vn_revalidate(vp, &vattr);
720 block_truncate_page(inode->i_mapping, inode->i_size, xfs_get_blocks);
724 xfs_vn_sync_flags(struct inode *inode)
726 unsigned int oldflags, newflags;
730 vnode_t *vp = vn_from_inode(inode);
732 memset(&vattr, 0, sizeof(vattr_t));
734 vattr.va_mask = XFS_AT_XFLAGS;
735 VOP_GETATTR(vp, &vattr, 0, NULL, error);
738 oldflags = vattr.va_xflags;
739 newflags = oldflags & ~(XFS_XFLAG_IMMUTABLE |
740 XFS_XFLAG_IUNLINK | XFS_XFLAG_BARRIER);
742 if (IS_IMMUTABLE(inode))
743 newflags |= XFS_XFLAG_IMMUTABLE;
744 if (IS_IUNLINK(inode))
745 newflags |= XFS_XFLAG_IUNLINK;
746 if (IS_BARRIER(inode))
747 newflags |= XFS_XFLAG_BARRIER;
749 if (oldflags ^ newflags) {
750 vattr.va_xflags = newflags;
751 vattr.va_mask |= XFS_AT_XFLAGS;
752 VOP_SETATTR(vp, &vattr, flags, NULL, error);
760 struct dentry *dentry,
766 vnode_t *vp = vn_from_inode(dentry->d_inode);
767 char *attr = (char *)name;
772 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
775 attr += namesp->attr_namelen;
776 error = namesp->attr_capable(vp, NULL);
780 /* Convert Linux syscall to XFS internal ATTR flags */
781 if (flags & XATTR_CREATE)
782 xflags |= ATTR_CREATE;
783 if (flags & XATTR_REPLACE)
784 xflags |= ATTR_REPLACE;
785 xflags |= namesp->attr_flag;
786 return namesp->attr_set(vp, attr, (void *)data, size, xflags);
791 struct dentry *dentry,
796 vnode_t *vp = vn_from_inode(dentry->d_inode);
797 char *attr = (char *)name;
802 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
805 attr += namesp->attr_namelen;
806 error = namesp->attr_capable(vp, NULL);
810 /* Convert Linux syscall to XFS internal ATTR flags */
812 xflags |= ATTR_KERNOVAL;
815 xflags |= namesp->attr_flag;
816 return namesp->attr_get(vp, attr, (void *)data, size, xflags);
821 struct dentry *dentry,
825 vnode_t *vp = vn_from_inode(dentry->d_inode);
826 int error, xflags = ATTR_KERNAMELS;
830 xflags |= ATTR_KERNOVAL;
831 xflags |= capable(CAP_SYS_ADMIN) ? ATTR_KERNFULLS : ATTR_KERNORMALS;
833 error = attr_generic_list(vp, data, size, xflags, &result);
841 struct dentry *dentry,
844 vnode_t *vp = vn_from_inode(dentry->d_inode);
845 char *attr = (char *)name;
850 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
853 attr += namesp->attr_namelen;
854 error = namesp->attr_capable(vp, NULL);
857 xflags |= namesp->attr_flag;
858 return namesp->attr_remove(vp, attr, xflags);
862 struct inode_operations xfs_inode_operations = {
863 .permission = xfs_vn_permission,
864 .truncate = xfs_vn_truncate,
865 .getattr = xfs_vn_getattr,
866 .setattr = xfs_vn_setattr,
867 .setxattr = xfs_vn_setxattr,
868 .getxattr = xfs_vn_getxattr,
869 .listxattr = xfs_vn_listxattr,
870 .removexattr = xfs_vn_removexattr,
871 .sync_flags = xfs_vn_sync_flags,
874 struct inode_operations xfs_dir_inode_operations = {
875 .create = xfs_vn_create,
876 .lookup = xfs_vn_lookup,
878 .unlink = xfs_vn_unlink,
879 .symlink = xfs_vn_symlink,
880 .mkdir = xfs_vn_mkdir,
881 .rmdir = xfs_vn_rmdir,
882 .mknod = xfs_vn_mknod,
883 .rename = xfs_vn_rename,
884 .permission = xfs_vn_permission,
885 .getattr = xfs_vn_getattr,
886 .setattr = xfs_vn_setattr,
887 .setxattr = xfs_vn_setxattr,
888 .getxattr = xfs_vn_getxattr,
889 .listxattr = xfs_vn_listxattr,
890 .removexattr = xfs_vn_removexattr,
891 .sync_flags = xfs_vn_sync_flags,
894 struct inode_operations xfs_symlink_inode_operations = {
895 .readlink = generic_readlink,
896 .follow_link = xfs_vn_follow_link,
897 .put_link = xfs_vn_put_link,
898 .permission = xfs_vn_permission,
899 .getattr = xfs_vn_getattr,
900 .setattr = xfs_vn_setattr,
901 .setxattr = xfs_vn_setxattr,
902 .getxattr = xfs_vn_getxattr,
903 .listxattr = xfs_vn_listxattr,
904 .removexattr = xfs_vn_removexattr,
905 .sync_flags = xfs_vn_sync_flags,