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"
27 #include "xfs_alloc.h"
28 #include "xfs_dmapi.h"
29 #include "xfs_quota.h"
30 #include "xfs_mount.h"
31 #include "xfs_bmap_btree.h"
32 #include "xfs_alloc_btree.h"
33 #include "xfs_ialloc_btree.h"
34 #include "xfs_dir2_sf.h"
35 #include "xfs_attr_sf.h"
36 #include "xfs_dinode.h"
37 #include "xfs_inode.h"
39 #include "xfs_btree.h"
40 #include "xfs_ialloc.h"
41 #include "xfs_rtalloc.h"
42 #include "xfs_error.h"
43 #include "xfs_itable.h"
49 #include "xfs_buf_item.h"
50 #include "xfs_utils.h"
52 #include <linux/capability.h>
53 #include <linux/xattr.h>
54 #include <linux/namei.h>
55 #include <linux/security.h>
56 #include <linux/vs_tag.h>
59 * Get a XFS inode from a given vnode.
67 bdp = bhv_lookup_range(VN_BHV_HEAD(vp),
68 VNODE_POSITION_XFS, VNODE_POSITION_XFS);
69 if (unlikely(bdp == NULL))
71 return XFS_BHVTOI(bdp);
75 * Bring the atime in the XFS inode uptodate.
76 * Used before logging the inode to disk or when the Linux inode goes away.
79 xfs_synchronize_atime(
84 vp = XFS_ITOV_NULL(ip);
86 struct inode *inode = &vp->v_inode;
87 ip->i_d.di_atime.t_sec = (__int32_t)inode->i_atime.tv_sec;
88 ip->i_d.di_atime.t_nsec = (__int32_t)inode->i_atime.tv_nsec;
93 * Change the requested timestamp in the given inode.
94 * We don't lock across timestamp updates, and we don't log them but
95 * we do record the fact that there is dirty information in core.
97 * NOTE -- callers MUST combine XFS_ICHGTIME_MOD or XFS_ICHGTIME_CHG
98 * with XFS_ICHGTIME_ACC to be sure that access time
99 * update will take. Calling first with XFS_ICHGTIME_ACC
100 * and then XFS_ICHGTIME_MOD may fail to modify the access
101 * timestamp if the filesystem is mounted noacctm.
108 struct inode *inode = vn_to_inode(XFS_ITOV(ip));
112 if (flags & XFS_ICHGTIME_MOD) {
114 ip->i_d.di_mtime.t_sec = (__int32_t)tv.tv_sec;
115 ip->i_d.di_mtime.t_nsec = (__int32_t)tv.tv_nsec;
117 if (flags & XFS_ICHGTIME_ACC) {
119 ip->i_d.di_atime.t_sec = (__int32_t)tv.tv_sec;
120 ip->i_d.di_atime.t_nsec = (__int32_t)tv.tv_nsec;
122 if (flags & XFS_ICHGTIME_CHG) {
124 ip->i_d.di_ctime.t_sec = (__int32_t)tv.tv_sec;
125 ip->i_d.di_ctime.t_nsec = (__int32_t)tv.tv_nsec;
129 * We update the i_update_core field _after_ changing
130 * the timestamps in order to coordinate properly with
131 * xfs_iflush() so that we don't lose timestamp updates.
132 * This keeps us from having to hold the inode lock
133 * while doing this. We use the SYNCHRONIZE macro to
134 * ensure that the compiler does not reorder the update
135 * of i_update_core above the timestamp updates above.
138 ip->i_update_core = 1;
139 if (!(inode->i_state & I_LOCK))
140 mark_inode_dirty_sync(inode);
144 * Variant on the above which avoids querying the system clock
145 * in situations where we know the Linux inode timestamps have
146 * just been updated (and so we can update our inode cheaply).
157 * Atime updates for read() & friends are handled lazily now, and
158 * explicit updates must go through xfs_ichgtime()
160 ASSERT((flags & XFS_ICHGTIME_ACC) == 0);
163 * We're not supposed to change timestamps in readonly-mounted
164 * filesystems. Throw it away if anyone asks us.
166 if (unlikely(IS_RDONLY(inode)))
169 if (flags & XFS_ICHGTIME_MOD) {
170 tvp = &inode->i_mtime;
171 ip->i_d.di_mtime.t_sec = (__int32_t)tvp->tv_sec;
172 ip->i_d.di_mtime.t_nsec = (__int32_t)tvp->tv_nsec;
174 if (flags & XFS_ICHGTIME_CHG) {
175 tvp = &inode->i_ctime;
176 ip->i_d.di_ctime.t_sec = (__int32_t)tvp->tv_sec;
177 ip->i_d.di_ctime.t_nsec = (__int32_t)tvp->tv_nsec;
181 * We update the i_update_core field _after_ changing
182 * the timestamps in order to coordinate properly with
183 * xfs_iflush() so that we don't lose timestamp updates.
184 * This keeps us from having to hold the inode lock
185 * while doing this. We use the SYNCHRONIZE macro to
186 * ensure that the compiler does not reorder the update
187 * of i_update_core above the timestamp updates above.
190 ip->i_update_core = 1;
191 if (!(inode->i_state & I_LOCK))
192 mark_inode_dirty_sync(inode);
197 * Pull the link count and size up from the xfs inode to the linux inode
204 vattr->va_mask = XFS_AT_NLINK|XFS_AT_SIZE|XFS_AT_NBLOCKS;
205 if (!bhv_vop_getattr(vn_from_inode(ip), vattr, ATTR_LAZY, NULL)) {
206 ip->i_nlink = vattr->va_nlink;
207 ip->i_blocks = vattr->va_nblocks;
209 /* we're under i_sem so i_size can't change under us */
210 if (i_size_read(ip) != vattr->va_size)
211 i_size_write(ip, vattr->va_size);
216 * Hook in SELinux. This is not quite correct yet, what we really need
217 * here (as we do for default ACLs) is a mechanism by which creation of
218 * these attrs can be journalled at inode creation time (along with the
219 * inode, of course, such that log replay can't cause these to be lost).
226 struct inode *ip = vn_to_inode(vp);
232 error = security_inode_init_security(ip, dir, &name, &value, &length);
234 if (error == -EOPNOTSUPP)
239 error = bhv_vop_attr_set(vp, name, value, length, ATTR_SECURE, NULL);
249 * Determine whether a process has a valid fs_struct (kernel daemons
250 * like knfsd don't have an fs_struct).
252 * XXX(hch): nfsd is broken, better fix it instead.
255 xfs_has_fs_struct(struct task_struct *task)
257 return (task->fs != init_task.fs);
264 struct dentry *dentry,
267 struct dentry teardown = {};
270 * If we can't add the ACL or we fail in
271 * xfs_init_security we must back out.
272 * ENOSPC can hit here, among other things.
274 teardown.d_inode = vn_to_inode(vp);
275 teardown.d_name = dentry->d_name;
278 bhv_vop_rmdir(dvp, &teardown, NULL);
280 bhv_vop_remove(dvp, &teardown, NULL);
287 struct dentry *dentry,
292 bhv_vattr_t vattr = { 0 };
293 bhv_vnode_t *vp = NULL, *dvp = vn_from_inode(dir);
294 xfs_acl_t *default_acl = NULL;
295 attrexists_t test_default_acl = _ACL_DEFAULT_EXISTS;
299 * Irix uses Missed'em'V split, but doesn't want to see
300 * the upper 5 bits of (14bit) major.
302 if (unlikely(!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff))
305 if (unlikely(test_default_acl && test_default_acl(dvp))) {
306 if (!_ACL_ALLOC(default_acl)) {
309 if (!_ACL_GET_DEFAULT(dvp, default_acl)) {
310 _ACL_FREE(default_acl);
315 if (IS_POSIXACL(dir) && !default_acl && xfs_has_fs_struct(current))
316 mode &= ~current->fs->umask;
318 vattr.va_mask = XFS_AT_TYPE|XFS_AT_MODE;
319 vattr.va_mode = mode;
321 switch (mode & S_IFMT) {
322 case S_IFCHR: case S_IFBLK: case S_IFIFO: case S_IFSOCK:
323 vattr.va_rdev = sysv_encode_dev(rdev);
324 vattr.va_mask |= XFS_AT_RDEV;
327 error = bhv_vop_create(dvp, dentry, &vattr, &vp, NULL);
330 error = bhv_vop_mkdir(dvp, dentry, &vattr, &vp, NULL);
337 if (unlikely(!error)) {
338 error = xfs_init_security(vp, dir);
340 xfs_cleanup_inode(dvp, vp, dentry, mode);
343 if (unlikely(default_acl)) {
345 error = _ACL_INHERIT(vp, &vattr, default_acl);
349 xfs_cleanup_inode(dvp, vp, dentry, mode);
351 _ACL_FREE(default_acl);
354 if (likely(!error)) {
356 ip = vn_to_inode(vp);
358 if (S_ISCHR(mode) || S_ISBLK(mode))
360 else if (S_ISDIR(mode))
361 xfs_validate_fields(ip, &vattr);
362 d_instantiate(dentry, ip);
363 xfs_validate_fields(dir, &vattr);
371 struct dentry *dentry,
373 struct nameidata *nd)
375 return xfs_vn_mknod(dir, dentry, mode, 0);
381 struct dentry *dentry,
384 return xfs_vn_mknod(dir, dentry, mode|S_IFDIR, 0);
387 STATIC struct dentry *
390 struct dentry *dentry,
391 struct nameidata *nd)
393 bhv_vnode_t *vp = vn_from_inode(dir), *cvp;
396 if (dentry->d_name.len >= MAXNAMELEN)
397 return ERR_PTR(-ENAMETOOLONG);
399 error = bhv_vop_lookup(vp, dentry, &cvp, 0, NULL, NULL);
400 if (unlikely(error)) {
401 if (unlikely(error != ENOENT))
402 return ERR_PTR(-error);
406 dx_propagate_tag(nd, vn_to_inode(cvp));
408 return d_splice_alias(vn_to_inode(cvp), dentry);
413 struct dentry *old_dentry,
415 struct dentry *dentry)
417 struct inode *ip; /* inode of guy being linked to */
418 bhv_vnode_t *tdvp; /* target directory for new name/link */
419 bhv_vnode_t *vp; /* vp of name being linked */
423 ip = old_dentry->d_inode; /* inode being linked to */
424 tdvp = vn_from_inode(dir);
425 vp = vn_from_inode(ip);
428 error = bhv_vop_link(tdvp, vp, dentry, NULL);
429 if (unlikely(error)) {
433 xfs_validate_fields(ip, &vattr);
434 d_instantiate(dentry, ip);
442 struct dentry *dentry)
445 bhv_vnode_t *dvp; /* directory containing name to remove */
449 inode = dentry->d_inode;
450 dvp = vn_from_inode(dir);
452 error = bhv_vop_remove(dvp, dentry, NULL);
453 if (likely(!error)) {
454 xfs_validate_fields(dir, &vattr); /* size needs update */
455 xfs_validate_fields(inode, &vattr);
463 struct dentry *dentry,
467 bhv_vattr_t va = { 0 };
468 bhv_vnode_t *dvp; /* directory containing name of symlink */
469 bhv_vnode_t *cvp; /* used to lookup symlink to put in dentry */
472 dvp = vn_from_inode(dir);
475 va.va_mode = S_IFLNK |
476 (irix_symlink_mode ? 0777 & ~current->fs->umask : S_IRWXUGO);
477 va.va_mask = XFS_AT_TYPE|XFS_AT_MODE;
479 error = bhv_vop_symlink(dvp, dentry, &va, (char *)symname, &cvp, NULL);
480 if (likely(!error && cvp)) {
481 error = xfs_init_security(cvp, dir);
482 if (likely(!error)) {
483 ip = vn_to_inode(cvp);
484 d_instantiate(dentry, ip);
485 xfs_validate_fields(dir, &va);
486 xfs_validate_fields(ip, &va);
488 xfs_cleanup_inode(dvp, cvp, dentry, 0);
497 struct dentry *dentry)
499 struct inode *inode = dentry->d_inode;
500 bhv_vnode_t *dvp = vn_from_inode(dir);
504 error = bhv_vop_rmdir(dvp, dentry, NULL);
505 if (likely(!error)) {
506 xfs_validate_fields(inode, &vattr);
507 xfs_validate_fields(dir, &vattr);
515 struct dentry *odentry,
517 struct dentry *ndentry)
519 struct inode *new_inode = ndentry->d_inode;
520 bhv_vnode_t *fvp; /* from directory */
521 bhv_vnode_t *tvp; /* target directory */
525 fvp = vn_from_inode(odir);
526 tvp = vn_from_inode(ndir);
528 error = bhv_vop_rename(fvp, odentry, tvp, ndentry, NULL);
529 if (likely(!error)) {
531 xfs_validate_fields(new_inode, &vattr);
532 xfs_validate_fields(odir, &vattr);
534 xfs_validate_fields(ndir, &vattr);
540 * careful here - this function can get called recursively, so
541 * we need to be very careful about how much stack we use.
542 * uio is kmalloced for this reason...
546 struct dentry *dentry,
547 struct nameidata *nd)
558 link = kmalloc(MAXPATHLEN+1, GFP_KERNEL);
560 nd_set_link(nd, ERR_PTR(-ENOMEM));
564 uio = kmalloc(sizeof(uio_t), GFP_KERNEL);
567 nd_set_link(nd, ERR_PTR(-ENOMEM));
571 vp = vn_from_inode(dentry->d_inode);
574 iov.iov_len = MAXPATHLEN;
578 uio->uio_segflg = UIO_SYSSPACE;
579 uio->uio_resid = MAXPATHLEN;
582 error = bhv_vop_readlink(vp, uio, 0, NULL);
583 if (unlikely(error)) {
585 link = ERR_PTR(-error);
587 link[MAXPATHLEN - uio->uio_resid] = '\0';
591 nd_set_link(nd, link);
597 struct dentry *dentry,
598 struct nameidata *nd,
601 char *s = nd_get_link(nd);
607 #ifdef CONFIG_XFS_POSIX_ACL
612 struct nameidata *nd)
614 return -bhv_vop_access(vn_from_inode(inode), mode << 6, NULL);
617 #define xfs_vn_permission NULL
622 struct vfsmount *mnt,
623 struct dentry *dentry,
626 struct inode *inode = dentry->d_inode;
627 bhv_vnode_t *vp = vn_from_inode(inode);
628 bhv_vattr_t vattr = { .va_mask = XFS_AT_STAT };
631 error = bhv_vop_getattr(vp, &vattr, ATTR_LAZY, NULL);
632 if (likely(!error)) {
633 stat->size = i_size_read(inode);
634 stat->dev = inode->i_sb->s_dev;
635 stat->rdev = (vattr.va_rdev == 0) ? 0 :
636 MKDEV(sysv_major(vattr.va_rdev) & 0x1ff,
637 sysv_minor(vattr.va_rdev));
638 stat->mode = vattr.va_mode;
639 stat->nlink = vattr.va_nlink;
640 stat->uid = vattr.va_uid;
641 stat->gid = vattr.va_gid;
642 stat->ino = vattr.va_nodeid;
643 stat->atime = vattr.va_atime;
644 stat->mtime = vattr.va_mtime;
645 stat->ctime = vattr.va_ctime;
646 stat->blocks = vattr.va_nblocks;
647 stat->blksize = vattr.va_blocksize;
654 struct dentry *dentry,
657 struct inode *inode = dentry->d_inode;
658 unsigned int ia_valid = attr->ia_valid;
659 bhv_vnode_t *vp = vn_from_inode(inode);
660 bhv_vattr_t vattr = { 0 };
664 error = inode_change_ok(inode, attr);
668 if (ia_valid & ATTR_UID) {
669 vattr.va_mask |= XFS_AT_UID;
670 vattr.va_uid = attr->ia_uid;
672 if (ia_valid & ATTR_GID) {
673 vattr.va_mask |= XFS_AT_GID;
674 vattr.va_gid = attr->ia_gid;
676 if ((ia_valid & ATTR_TAG) && IS_TAGGED(inode)) {
677 vattr.va_mask |= XFS_AT_TAG;
678 vattr.va_tag = attr->ia_tag;
680 if (ia_valid & ATTR_SIZE) {
681 vattr.va_mask |= XFS_AT_SIZE;
682 vattr.va_size = attr->ia_size;
684 if (ia_valid & ATTR_ATIME) {
685 vattr.va_mask |= XFS_AT_ATIME;
686 vattr.va_atime = attr->ia_atime;
687 inode->i_atime = attr->ia_atime;
689 if (ia_valid & ATTR_MTIME) {
690 vattr.va_mask |= XFS_AT_MTIME;
691 vattr.va_mtime = attr->ia_mtime;
693 if (ia_valid & ATTR_CTIME) {
694 vattr.va_mask |= XFS_AT_CTIME;
695 vattr.va_ctime = attr->ia_ctime;
697 if (ia_valid & ATTR_MODE) {
698 vattr.va_mask |= XFS_AT_MODE;
699 vattr.va_mode = attr->ia_mode;
700 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
701 inode->i_mode &= ~S_ISGID;
704 if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))
707 if ((ia_valid & ATTR_NO_BLOCK))
708 flags |= ATTR_NONBLOCK;
711 error = bhv_vop_setattr(vp, &vattr, flags, NULL);
713 __vn_revalidate(vp, &vattr);
721 block_truncate_page(inode->i_mapping, inode->i_size, xfs_get_blocks);
725 xfs_vn_sync_flags(struct inode *inode)
727 unsigned int oldflags, newflags;
731 bhv_vnode_t *vp = vn_from_inode(inode);
733 memset(&vattr, 0, sizeof vattr);
735 vattr.va_mask = XFS_AT_XFLAGS;
736 error = bhv_vop_getattr(vp, &vattr, 0, NULL);
740 oldflags = vattr.va_xflags;
741 newflags = oldflags & ~(XFS_XFLAG_IMMUTABLE |
742 XFS_XFLAG_IUNLINK | XFS_XFLAG_BARRIER);
744 if (IS_IMMUTABLE(inode))
745 newflags |= XFS_XFLAG_IMMUTABLE;
746 if (IS_IUNLINK(inode))
747 newflags |= XFS_XFLAG_IUNLINK;
748 if (IS_BARRIER(inode))
749 newflags |= XFS_XFLAG_BARRIER;
751 if (oldflags ^ newflags) {
752 vattr.va_xflags = newflags;
753 vattr.va_mask |= XFS_AT_XFLAGS;
754 error = bhv_vop_setattr(vp, &vattr, flags, NULL);
762 struct dentry *dentry,
768 bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
769 char *attr = (char *)name;
774 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
777 attr += namesp->attr_namelen;
778 error = namesp->attr_capable(vp, NULL);
782 /* Convert Linux syscall to XFS internal ATTR flags */
783 if (flags & XATTR_CREATE)
784 xflags |= ATTR_CREATE;
785 if (flags & XATTR_REPLACE)
786 xflags |= ATTR_REPLACE;
787 xflags |= namesp->attr_flag;
788 return namesp->attr_set(vp, attr, (void *)data, size, xflags);
793 struct dentry *dentry,
798 bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
799 char *attr = (char *)name;
804 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
807 attr += namesp->attr_namelen;
808 error = namesp->attr_capable(vp, NULL);
812 /* Convert Linux syscall to XFS internal ATTR flags */
814 xflags |= ATTR_KERNOVAL;
817 xflags |= namesp->attr_flag;
818 return namesp->attr_get(vp, attr, (void *)data, size, xflags);
823 struct dentry *dentry,
827 bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
828 int error, xflags = ATTR_KERNAMELS;
832 xflags |= ATTR_KERNOVAL;
833 xflags |= capable(CAP_SYS_ADMIN) ? ATTR_KERNFULLS : ATTR_KERNORMALS;
835 error = attr_generic_list(vp, data, size, xflags, &result);
843 struct dentry *dentry,
846 bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
847 char *attr = (char *)name;
852 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
855 attr += namesp->attr_namelen;
856 error = namesp->attr_capable(vp, NULL);
859 xflags |= namesp->attr_flag;
860 return namesp->attr_remove(vp, attr, xflags);
864 struct inode_operations xfs_inode_operations = {
865 .permission = xfs_vn_permission,
866 .truncate = xfs_vn_truncate,
867 .getattr = xfs_vn_getattr,
868 .setattr = xfs_vn_setattr,
869 .setxattr = xfs_vn_setxattr,
870 .getxattr = xfs_vn_getxattr,
871 .listxattr = xfs_vn_listxattr,
872 .removexattr = xfs_vn_removexattr,
873 .sync_flags = xfs_vn_sync_flags,
876 struct inode_operations xfs_dir_inode_operations = {
877 .create = xfs_vn_create,
878 .lookup = xfs_vn_lookup,
880 .unlink = xfs_vn_unlink,
881 .symlink = xfs_vn_symlink,
882 .mkdir = xfs_vn_mkdir,
883 .rmdir = xfs_vn_rmdir,
884 .mknod = xfs_vn_mknod,
885 .rename = xfs_vn_rename,
886 .permission = xfs_vn_permission,
887 .getattr = xfs_vn_getattr,
888 .setattr = xfs_vn_setattr,
889 .setxattr = xfs_vn_setxattr,
890 .getxattr = xfs_vn_getxattr,
891 .listxattr = xfs_vn_listxattr,
892 .removexattr = xfs_vn_removexattr,
893 .sync_flags = xfs_vn_sync_flags,
896 struct inode_operations xfs_symlink_inode_operations = {
897 .readlink = generic_readlink,
898 .follow_link = xfs_vn_follow_link,
899 .put_link = xfs_vn_put_link,
900 .permission = xfs_vn_permission,
901 .getattr = xfs_vn_getattr,
902 .setattr = xfs_vn_setattr,
903 .setxattr = xfs_vn_setxattr,
904 .getxattr = xfs_vn_getxattr,
905 .listxattr = xfs_vn_listxattr,
906 .removexattr = xfs_vn_removexattr,
907 .sync_flags = xfs_vn_sync_flags,