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>
60 * Get a XFS inode from a given vnode.
68 bdp = bhv_lookup_range(VN_BHV_HEAD(vp),
69 VNODE_POSITION_XFS, VNODE_POSITION_XFS);
70 if (unlikely(bdp == NULL))
72 return XFS_BHVTOI(bdp);
76 * Bring the atime in the XFS inode uptodate.
77 * Used before logging the inode to disk or when the Linux inode goes away.
80 xfs_synchronize_atime(
85 vp = XFS_ITOV_NULL(ip);
87 struct inode *inode = &vp->v_inode;
88 ip->i_d.di_atime.t_sec = (__int32_t)inode->i_atime.tv_sec;
89 ip->i_d.di_atime.t_nsec = (__int32_t)inode->i_atime.tv_nsec;
94 * Change the requested timestamp in the given inode.
95 * We don't lock across timestamp updates, and we don't log them but
96 * we do record the fact that there is dirty information in core.
98 * NOTE -- callers MUST combine XFS_ICHGTIME_MOD or XFS_ICHGTIME_CHG
99 * with XFS_ICHGTIME_ACC to be sure that access time
100 * update will take. Calling first with XFS_ICHGTIME_ACC
101 * and then XFS_ICHGTIME_MOD may fail to modify the access
102 * timestamp if the filesystem is mounted noacctm.
109 struct inode *inode = vn_to_inode(XFS_ITOV(ip));
113 if (flags & XFS_ICHGTIME_MOD) {
115 ip->i_d.di_mtime.t_sec = (__int32_t)tv.tv_sec;
116 ip->i_d.di_mtime.t_nsec = (__int32_t)tv.tv_nsec;
118 if (flags & XFS_ICHGTIME_ACC) {
120 ip->i_d.di_atime.t_sec = (__int32_t)tv.tv_sec;
121 ip->i_d.di_atime.t_nsec = (__int32_t)tv.tv_nsec;
123 if (flags & XFS_ICHGTIME_CHG) {
125 ip->i_d.di_ctime.t_sec = (__int32_t)tv.tv_sec;
126 ip->i_d.di_ctime.t_nsec = (__int32_t)tv.tv_nsec;
130 * We update the i_update_core field _after_ changing
131 * the timestamps in order to coordinate properly with
132 * xfs_iflush() so that we don't lose timestamp updates.
133 * This keeps us from having to hold the inode lock
134 * while doing this. We use the SYNCHRONIZE macro to
135 * ensure that the compiler does not reorder the update
136 * of i_update_core above the timestamp updates above.
139 ip->i_update_core = 1;
140 if (!(inode->i_state & I_LOCK))
141 mark_inode_dirty_sync(inode);
145 * Variant on the above which avoids querying the system clock
146 * in situations where we know the Linux inode timestamps have
147 * just been updated (and so we can update our inode cheaply).
158 * Atime updates for read() & friends are handled lazily now, and
159 * explicit updates must go through xfs_ichgtime()
161 ASSERT((flags & XFS_ICHGTIME_ACC) == 0);
164 * We're not supposed to change timestamps in readonly-mounted
165 * filesystems. Throw it away if anyone asks us.
167 if (unlikely(IS_RDONLY(inode)))
170 if (flags & XFS_ICHGTIME_MOD) {
171 tvp = &inode->i_mtime;
172 ip->i_d.di_mtime.t_sec = (__int32_t)tvp->tv_sec;
173 ip->i_d.di_mtime.t_nsec = (__int32_t)tvp->tv_nsec;
175 if (flags & XFS_ICHGTIME_CHG) {
176 tvp = &inode->i_ctime;
177 ip->i_d.di_ctime.t_sec = (__int32_t)tvp->tv_sec;
178 ip->i_d.di_ctime.t_nsec = (__int32_t)tvp->tv_nsec;
182 * We update the i_update_core field _after_ changing
183 * the timestamps in order to coordinate properly with
184 * xfs_iflush() so that we don't lose timestamp updates.
185 * This keeps us from having to hold the inode lock
186 * while doing this. We use the SYNCHRONIZE macro to
187 * ensure that the compiler does not reorder the update
188 * of i_update_core above the timestamp updates above.
191 ip->i_update_core = 1;
192 if (!(inode->i_state & I_LOCK))
193 mark_inode_dirty_sync(inode);
198 * Pull the link count and size up from the xfs inode to the linux inode
205 vnode_t *vp = vn_from_inode(ip);
208 vattr->va_mask = XFS_AT_NLINK|XFS_AT_SIZE|XFS_AT_NBLOCKS;
209 VOP_GETATTR(vp, vattr, ATTR_LAZY, NULL, error);
210 if (likely(!error)) {
211 ip->i_nlink = vattr->va_nlink;
212 ip->i_blocks = vattr->va_nblocks;
214 /* we're under i_sem so i_size can't change under us */
215 if (i_size_read(ip) != vattr->va_size)
216 i_size_write(ip, vattr->va_size);
221 * Hook in SELinux. This is not quite correct yet, what we really need
222 * here (as we do for default ACLs) is a mechanism by which creation of
223 * these attrs can be journalled at inode creation time (along with the
224 * inode, of course, such that log replay can't cause these to be lost).
231 struct inode *ip = vn_to_inode(vp);
237 error = security_inode_init_security(ip, dir, &name, &value, &length);
239 if (error == -EOPNOTSUPP)
244 VOP_ATTR_SET(vp, name, value, length, ATTR_SECURE, NULL, error);
254 * Determine whether a process has a valid fs_struct (kernel daemons
255 * like knfsd don't have an fs_struct).
257 * XXX(hch): nfsd is broken, better fix it instead.
260 xfs_has_fs_struct(struct task_struct *task)
262 return (task->fs != init_task.fs);
269 struct dentry *dentry,
272 struct dentry teardown = {};
276 * If we can't add the ACL or we fail in
277 * xfs_init_security we must back out.
278 * ENOSPC can hit here, among other things.
280 teardown.d_inode = vn_to_inode(vp);
281 teardown.d_name = dentry->d_name;
284 VOP_RMDIR(dvp, &teardown, NULL, error);
286 VOP_REMOVE(dvp, &teardown, NULL, error);
293 struct dentry *dentry,
298 vattr_t vattr = { 0 };
299 vnode_t *vp = NULL, *dvp = vn_from_inode(dir);
300 xfs_acl_t *default_acl = NULL;
301 attrexists_t test_default_acl = _ACL_DEFAULT_EXISTS;
305 * Irix uses Missed'em'V split, but doesn't want to see
306 * the upper 5 bits of (14bit) major.
308 if (unlikely(!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff))
311 if (unlikely(test_default_acl && test_default_acl(dvp))) {
312 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 && xfs_has_fs_struct(current))
322 mode &= ~current->fs->umask;
324 vattr.va_mask = XFS_AT_TYPE|XFS_AT_MODE;
325 vattr.va_mode = mode;
327 switch (mode & S_IFMT) {
328 case S_IFCHR: case S_IFBLK: case S_IFIFO: case S_IFSOCK:
329 vattr.va_rdev = sysv_encode_dev(rdev);
330 vattr.va_mask |= XFS_AT_RDEV;
333 VOP_CREATE(dvp, dentry, &vattr, &vp, NULL, error);
336 VOP_MKDIR(dvp, dentry, &vattr, &vp, NULL, error);
343 if (unlikely(!error)) {
344 error = xfs_init_security(vp, dir);
346 xfs_cleanup_inode(dvp, vp, dentry, mode);
349 if (unlikely(default_acl)) {
351 error = _ACL_INHERIT(vp, &vattr, default_acl);
355 xfs_cleanup_inode(dvp, vp, dentry, mode);
357 _ACL_FREE(default_acl);
360 if (likely(!error)) {
362 ip = vn_to_inode(vp);
364 if (S_ISCHR(mode) || S_ISBLK(mode))
366 else if (S_ISDIR(mode))
367 xfs_validate_fields(ip, &vattr);
368 d_instantiate(dentry, ip);
369 xfs_validate_fields(dir, &vattr);
377 struct dentry *dentry,
379 struct nameidata *nd)
381 return xfs_vn_mknod(dir, dentry, mode, 0);
387 struct dentry *dentry,
390 return xfs_vn_mknod(dir, dentry, mode|S_IFDIR, 0);
393 STATIC struct dentry *
396 struct dentry *dentry,
397 struct nameidata *nd)
399 struct vnode *vp = vn_from_inode(dir), *cvp;
402 if (dentry->d_name.len >= MAXNAMELEN)
403 return ERR_PTR(-ENAMETOOLONG);
405 VOP_LOOKUP(vp, dentry, &cvp, 0, NULL, NULL, error);
407 if (unlikely(error != ENOENT))
408 return ERR_PTR(-error);
413 return d_splice_alias(vn_to_inode(cvp), dentry);
418 struct dentry *old_dentry,
420 struct dentry *dentry)
422 struct inode *ip; /* inode of guy being linked to */
423 vnode_t *tdvp; /* target directory for new name/link */
424 vnode_t *vp; /* vp of name being linked */
428 ip = old_dentry->d_inode; /* inode being linked to */
429 if (S_ISDIR(ip->i_mode))
432 tdvp = vn_from_inode(dir);
433 vp = vn_from_inode(ip);
435 VOP_LINK(tdvp, vp, dentry, NULL, error);
436 if (likely(!error)) {
439 xfs_validate_fields(ip, &vattr);
440 d_instantiate(dentry, ip);
448 struct dentry *dentry)
451 vnode_t *dvp; /* directory containing name to remove */
455 inode = dentry->d_inode;
456 dvp = vn_from_inode(dir);
458 VOP_REMOVE(dvp, dentry, NULL, error);
459 if (likely(!error)) {
460 xfs_validate_fields(dir, &vattr); /* size needs update */
461 xfs_validate_fields(inode, &vattr);
469 struct dentry *dentry,
473 vattr_t vattr = { 0 };
474 vnode_t *dvp; /* directory containing name of symlink */
475 vnode_t *cvp; /* used to lookup symlink to put in dentry */
478 dvp = vn_from_inode(dir);
481 vattr.va_mode = S_IFLNK |
482 (irix_symlink_mode ? 0777 & ~current->fs->umask : S_IRWXUGO);
483 vattr.va_mask = XFS_AT_TYPE|XFS_AT_MODE;
486 VOP_SYMLINK(dvp, dentry, &vattr, (char *)symname, &cvp, NULL, error);
487 if (likely(!error && cvp)) {
488 error = xfs_init_security(cvp, dir);
489 if (likely(!error)) {
490 ip = vn_to_inode(cvp);
491 d_instantiate(dentry, ip);
492 xfs_validate_fields(dir, &vattr);
493 xfs_validate_fields(ip, &vattr);
502 struct dentry *dentry)
504 struct inode *inode = dentry->d_inode;
505 vnode_t *dvp = vn_from_inode(dir);
509 VOP_RMDIR(dvp, dentry, NULL, error);
510 if (likely(!error)) {
511 xfs_validate_fields(inode, &vattr);
512 xfs_validate_fields(dir, &vattr);
520 struct dentry *odentry,
522 struct dentry *ndentry)
524 struct inode *new_inode = ndentry->d_inode;
525 vnode_t *fvp; /* from directory */
526 vnode_t *tvp; /* target directory */
530 fvp = vn_from_inode(odir);
531 tvp = vn_from_inode(ndir);
533 VOP_RENAME(fvp, odentry, tvp, ndentry, NULL, error);
534 if (likely(!error)) {
536 xfs_validate_fields(new_inode, &vattr);
537 xfs_validate_fields(odir, &vattr);
539 xfs_validate_fields(ndir, &vattr);
545 * careful here - this function can get called recursively, so
546 * we need to be very careful about how much stack we use.
547 * uio is kmalloced for this reason...
551 struct dentry *dentry,
552 struct nameidata *nd)
563 link = (char *)kmalloc(MAXPATHLEN+1, GFP_KERNEL);
565 nd_set_link(nd, ERR_PTR(-ENOMEM));
569 uio = (uio_t *)kmalloc(sizeof(uio_t), GFP_KERNEL);
572 nd_set_link(nd, ERR_PTR(-ENOMEM));
576 vp = vn_from_inode(dentry->d_inode);
579 iov.iov_len = MAXPATHLEN;
583 uio->uio_segflg = UIO_SYSSPACE;
584 uio->uio_resid = MAXPATHLEN;
587 VOP_READLINK(vp, uio, 0, NULL, error);
590 link = ERR_PTR(-error);
592 link[MAXPATHLEN - uio->uio_resid] = '\0';
596 nd_set_link(nd, link);
602 struct dentry *dentry,
603 struct nameidata *nd,
606 char *s = nd_get_link(nd);
612 #ifdef CONFIG_XFS_POSIX_ACL
617 struct nameidata *nd)
619 vnode_t *vp = vn_from_inode(inode);
622 mode <<= 6; /* convert from linux to vnode access bits */
623 VOP_ACCESS(vp, mode, NULL, error);
627 #define xfs_vn_permission NULL
632 struct vfsmount *mnt,
633 struct dentry *dentry,
636 struct inode *inode = dentry->d_inode;
637 vnode_t *vp = vn_from_inode(inode);
640 if (unlikely(vp->v_flag & VMODIFIED))
641 error = vn_revalidate(vp);
643 generic_fillattr(inode, stat);
649 struct dentry *dentry,
652 struct inode *inode = dentry->d_inode;
653 unsigned int ia_valid = attr->ia_valid;
654 vnode_t *vp = vn_from_inode(inode);
655 vattr_t vattr = { 0 };
659 if (ia_valid & ATTR_UID) {
660 vattr.va_mask |= XFS_AT_UID;
661 vattr.va_uid = attr->ia_uid;
663 if (ia_valid & ATTR_GID) {
664 vattr.va_mask |= XFS_AT_GID;
665 vattr.va_gid = attr->ia_gid;
667 if (ia_valid & ATTR_SIZE) {
668 vattr.va_mask |= XFS_AT_SIZE;
669 vattr.va_size = attr->ia_size;
671 if (ia_valid & ATTR_ATIME) {
672 vattr.va_mask |= XFS_AT_ATIME;
673 vattr.va_atime = attr->ia_atime;
674 if (ia_valid & ATTR_ATIME_SET)
675 inode->i_atime = attr->ia_atime;
677 if (ia_valid & ATTR_MTIME) {
678 vattr.va_mask |= XFS_AT_MTIME;
679 vattr.va_mtime = attr->ia_mtime;
681 if (ia_valid & ATTR_CTIME) {
682 vattr.va_mask |= XFS_AT_CTIME;
683 vattr.va_ctime = attr->ia_ctime;
685 if (ia_valid & ATTR_MODE) {
686 vattr.va_mask |= XFS_AT_MODE;
687 vattr.va_mode = attr->ia_mode;
688 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
689 inode->i_mode &= ~S_ISGID;
692 if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))
695 if ((ia_valid & ATTR_NO_BLOCK))
696 flags |= ATTR_NONBLOCK;
699 VOP_SETATTR(vp, &vattr, flags, NULL, error);
701 __vn_revalidate(vp, &vattr);
709 block_truncate_page(inode->i_mapping, inode->i_size, xfs_get_block);
714 struct dentry *dentry,
720 vnode_t *vp = vn_from_inode(dentry->d_inode);
721 char *attr = (char *)name;
726 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
729 attr += namesp->attr_namelen;
730 error = namesp->attr_capable(vp, NULL);
734 /* Convert Linux syscall to XFS internal ATTR flags */
735 if (flags & XATTR_CREATE)
736 xflags |= ATTR_CREATE;
737 if (flags & XATTR_REPLACE)
738 xflags |= ATTR_REPLACE;
739 xflags |= namesp->attr_flag;
740 return namesp->attr_set(vp, attr, (void *)data, size, xflags);
745 struct dentry *dentry,
750 vnode_t *vp = vn_from_inode(dentry->d_inode);
751 char *attr = (char *)name;
756 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
759 attr += namesp->attr_namelen;
760 error = namesp->attr_capable(vp, NULL);
764 /* Convert Linux syscall to XFS internal ATTR flags */
766 xflags |= ATTR_KERNOVAL;
769 xflags |= namesp->attr_flag;
770 return namesp->attr_get(vp, attr, (void *)data, size, xflags);
775 struct dentry *dentry,
779 vnode_t *vp = vn_from_inode(dentry->d_inode);
780 int error, xflags = ATTR_KERNAMELS;
784 xflags |= ATTR_KERNOVAL;
785 xflags |= capable(CAP_SYS_ADMIN) ? ATTR_KERNFULLS : ATTR_KERNORMALS;
787 error = attr_generic_list(vp, data, size, xflags, &result);
795 struct dentry *dentry,
798 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);
811 xflags |= namesp->attr_flag;
812 return namesp->attr_remove(vp, attr, xflags);
816 struct inode_operations xfs_inode_operations = {
817 .permission = xfs_vn_permission,
818 .truncate = xfs_vn_truncate,
819 .getattr = xfs_vn_getattr,
820 .setattr = xfs_vn_setattr,
821 .setxattr = xfs_vn_setxattr,
822 .getxattr = xfs_vn_getxattr,
823 .listxattr = xfs_vn_listxattr,
824 .removexattr = xfs_vn_removexattr,
827 struct inode_operations xfs_dir_inode_operations = {
828 .create = xfs_vn_create,
829 .lookup = xfs_vn_lookup,
831 .unlink = xfs_vn_unlink,
832 .symlink = xfs_vn_symlink,
833 .mkdir = xfs_vn_mkdir,
834 .rmdir = xfs_vn_rmdir,
835 .mknod = xfs_vn_mknod,
836 .rename = xfs_vn_rename,
837 .permission = xfs_vn_permission,
838 .getattr = xfs_vn_getattr,
839 .setattr = xfs_vn_setattr,
840 .setxattr = xfs_vn_setxattr,
841 .getxattr = xfs_vn_getxattr,
842 .listxattr = xfs_vn_listxattr,
843 .removexattr = xfs_vn_removexattr,
846 struct inode_operations xfs_symlink_inode_operations = {
847 .readlink = generic_readlink,
848 .follow_link = xfs_vn_follow_link,
849 .put_link = xfs_vn_put_link,
850 .permission = xfs_vn_permission,
851 .getattr = xfs_vn_getattr,
852 .setattr = xfs_vn_setattr,
853 .setxattr = xfs_vn_setxattr,
854 .getxattr = xfs_vn_getxattr,
855 .listxattr = xfs_vn_listxattr,
856 .removexattr = xfs_vn_removexattr,