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"
47 #include "xfs_buf_item.h"
48 #include "xfs_utils.h"
49 #include "xfs_vnodeops.h"
51 #include <linux/capability.h>
52 #include <linux/xattr.h>
53 #include <linux/namei.h>
54 #include <linux/security.h>
57 * Bring the atime in the XFS inode uptodate.
58 * Used before logging the inode to disk or when the Linux inode goes away.
61 xfs_synchronize_atime(
66 vp = XFS_ITOV_NULL(ip);
68 ip->i_d.di_atime.t_sec = (__int32_t)vp->i_atime.tv_sec;
69 ip->i_d.di_atime.t_nsec = (__int32_t)vp->i_atime.tv_nsec;
74 * Change the requested timestamp in the given inode.
75 * We don't lock across timestamp updates, and we don't log them but
76 * we do record the fact that there is dirty information in core.
78 * NOTE -- callers MUST combine XFS_ICHGTIME_MOD or XFS_ICHGTIME_CHG
79 * with XFS_ICHGTIME_ACC to be sure that access time
80 * update will take. Calling first with XFS_ICHGTIME_ACC
81 * and then XFS_ICHGTIME_MOD may fail to modify the access
82 * timestamp if the filesystem is mounted noacctm.
89 struct inode *inode = vn_to_inode(XFS_ITOV(ip));
93 if (flags & XFS_ICHGTIME_MOD) {
95 ip->i_d.di_mtime.t_sec = (__int32_t)tv.tv_sec;
96 ip->i_d.di_mtime.t_nsec = (__int32_t)tv.tv_nsec;
98 if (flags & XFS_ICHGTIME_ACC) {
100 ip->i_d.di_atime.t_sec = (__int32_t)tv.tv_sec;
101 ip->i_d.di_atime.t_nsec = (__int32_t)tv.tv_nsec;
103 if (flags & XFS_ICHGTIME_CHG) {
105 ip->i_d.di_ctime.t_sec = (__int32_t)tv.tv_sec;
106 ip->i_d.di_ctime.t_nsec = (__int32_t)tv.tv_nsec;
110 * We update the i_update_core field _after_ changing
111 * the timestamps in order to coordinate properly with
112 * xfs_iflush() so that we don't lose timestamp updates.
113 * This keeps us from having to hold the inode lock
114 * while doing this. We use the SYNCHRONIZE macro to
115 * ensure that the compiler does not reorder the update
116 * of i_update_core above the timestamp updates above.
119 ip->i_update_core = 1;
120 if (!(inode->i_state & I_LOCK))
121 mark_inode_dirty_sync(inode);
125 * Variant on the above which avoids querying the system clock
126 * in situations where we know the Linux inode timestamps have
127 * just been updated (and so we can update our inode cheaply).
138 * Atime updates for read() & friends are handled lazily now, and
139 * explicit updates must go through xfs_ichgtime()
141 ASSERT((flags & XFS_ICHGTIME_ACC) == 0);
144 * We're not supposed to change timestamps in readonly-mounted
145 * filesystems. Throw it away if anyone asks us.
147 if (unlikely(IS_RDONLY(inode)))
150 if (flags & XFS_ICHGTIME_MOD) {
151 tvp = &inode->i_mtime;
152 ip->i_d.di_mtime.t_sec = (__int32_t)tvp->tv_sec;
153 ip->i_d.di_mtime.t_nsec = (__int32_t)tvp->tv_nsec;
155 if (flags & XFS_ICHGTIME_CHG) {
156 tvp = &inode->i_ctime;
157 ip->i_d.di_ctime.t_sec = (__int32_t)tvp->tv_sec;
158 ip->i_d.di_ctime.t_nsec = (__int32_t)tvp->tv_nsec;
162 * We update the i_update_core field _after_ changing
163 * the timestamps in order to coordinate properly with
164 * xfs_iflush() so that we don't lose timestamp updates.
165 * This keeps us from having to hold the inode lock
166 * while doing this. We use the SYNCHRONIZE macro to
167 * ensure that the compiler does not reorder the update
168 * of i_update_core above the timestamp updates above.
171 ip->i_update_core = 1;
172 if (!(inode->i_state & I_LOCK))
173 mark_inode_dirty_sync(inode);
178 * Pull the link count and size up from the xfs inode to the linux inode
185 vattr->va_mask = XFS_AT_NLINK|XFS_AT_SIZE|XFS_AT_NBLOCKS;
186 if (!xfs_getattr(XFS_I(ip), vattr, ATTR_LAZY)) {
187 ip->i_nlink = vattr->va_nlink;
188 ip->i_blocks = vattr->va_nblocks;
190 /* we're under i_sem so i_size can't change under us */
191 if (i_size_read(ip) != vattr->va_size)
192 i_size_write(ip, vattr->va_size);
197 * Hook in SELinux. This is not quite correct yet, what we really need
198 * here (as we do for default ACLs) is a mechanism by which creation of
199 * these attrs can be journalled at inode creation time (along with the
200 * inode, of course, such that log replay can't cause these to be lost).
207 struct inode *ip = vn_to_inode(vp);
213 error = security_inode_init_security(ip, dir, &name, &value, &length);
215 if (error == -EOPNOTSUPP)
220 error = xfs_attr_set(XFS_I(ip), name, value,
221 length, ATTR_SECURE);
223 xfs_iflags_set(XFS_I(ip), XFS_IMODIFIED);
231 * Determine whether a process has a valid fs_struct (kernel daemons
232 * like knfsd don't have an fs_struct).
234 * XXX(hch): nfsd is broken, better fix it instead.
237 xfs_has_fs_struct(struct task_struct *task)
239 return (task->fs != init_task.fs);
246 struct dentry *dentry,
249 struct dentry teardown = {};
252 * If we can't add the ACL or we fail in
253 * xfs_init_security we must back out.
254 * ENOSPC can hit here, among other things.
256 teardown.d_inode = vn_to_inode(vp);
257 teardown.d_name = dentry->d_name;
260 xfs_rmdir(XFS_I(dir), &teardown);
262 xfs_remove(XFS_I(dir), &teardown);
269 struct dentry *dentry,
274 bhv_vnode_t *vp = NULL, *dvp = vn_from_inode(dir);
275 xfs_acl_t *default_acl = NULL;
276 attrexists_t test_default_acl = _ACL_DEFAULT_EXISTS;
280 * Irix uses Missed'em'V split, but doesn't want to see
281 * the upper 5 bits of (14bit) major.
283 if (unlikely(!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff))
286 if (unlikely(test_default_acl && test_default_acl(dvp))) {
287 if (!_ACL_ALLOC(default_acl)) {
290 if (!_ACL_GET_DEFAULT(dvp, default_acl)) {
291 _ACL_FREE(default_acl);
296 if (IS_POSIXACL(dir) && !default_acl && xfs_has_fs_struct(current))
297 mode &= ~current->fs->umask;
299 switch (mode & S_IFMT) {
300 case S_IFCHR: case S_IFBLK: case S_IFIFO: case S_IFSOCK:
301 rdev = sysv_encode_dev(rdev);
303 error = xfs_create(XFS_I(dir), dentry, mode, rdev, &vp, NULL);
306 error = xfs_mkdir(XFS_I(dir), dentry, mode, &vp, NULL);
313 if (unlikely(!error)) {
314 error = xfs_init_security(vp, dir);
316 xfs_cleanup_inode(dir, vp, dentry, mode);
319 if (unlikely(default_acl)) {
321 error = _ACL_INHERIT(vp, mode, default_acl);
323 xfs_iflags_set(XFS_I(vp), XFS_IMODIFIED);
325 xfs_cleanup_inode(dir, vp, dentry, mode);
327 _ACL_FREE(default_acl);
330 if (likely(!error)) {
332 ip = vn_to_inode(vp);
334 if (S_ISCHR(mode) || S_ISBLK(mode))
336 else if (S_ISDIR(mode))
337 xfs_validate_fields(ip, &vattr);
338 d_instantiate(dentry, ip);
339 xfs_validate_fields(dir, &vattr);
347 struct dentry *dentry,
349 struct nameidata *nd)
351 return xfs_vn_mknod(dir, dentry, mode, 0);
357 struct dentry *dentry,
360 return xfs_vn_mknod(dir, dentry, mode|S_IFDIR, 0);
363 STATIC struct dentry *
366 struct dentry *dentry,
367 struct nameidata *nd)
372 if (dentry->d_name.len >= MAXNAMELEN)
373 return ERR_PTR(-ENAMETOOLONG);
375 error = xfs_lookup(XFS_I(dir), dentry, &cvp);
376 if (unlikely(error)) {
377 if (unlikely(error != ENOENT))
378 return ERR_PTR(-error);
383 return d_splice_alias(vn_to_inode(cvp), dentry);
388 struct dentry *old_dentry,
390 struct dentry *dentry)
392 struct inode *ip; /* inode of guy being linked to */
393 bhv_vnode_t *vp; /* vp of name being linked */
397 ip = old_dentry->d_inode; /* inode being linked to */
398 vp = vn_from_inode(ip);
401 error = xfs_link(XFS_I(dir), vp, dentry);
402 if (unlikely(error)) {
405 xfs_iflags_set(XFS_I(dir), XFS_IMODIFIED);
406 xfs_validate_fields(ip, &vattr);
407 d_instantiate(dentry, ip);
415 struct dentry *dentry)
421 inode = dentry->d_inode;
423 error = xfs_remove(XFS_I(dir), dentry);
424 if (likely(!error)) {
425 xfs_validate_fields(dir, &vattr); /* size needs update */
426 xfs_validate_fields(inode, &vattr);
434 struct dentry *dentry,
438 bhv_vnode_t *cvp; /* used to lookup symlink to put in dentry */
445 (irix_symlink_mode ? 0777 & ~current->fs->umask : S_IRWXUGO);
447 error = xfs_symlink(XFS_I(dir), dentry, (char *)symname, mode,
449 if (likely(!error && cvp)) {
450 error = xfs_init_security(cvp, dir);
451 if (likely(!error)) {
452 ip = vn_to_inode(cvp);
453 d_instantiate(dentry, ip);
454 xfs_validate_fields(dir, &va);
455 xfs_validate_fields(ip, &va);
457 xfs_cleanup_inode(dir, cvp, dentry, 0);
466 struct dentry *dentry)
468 struct inode *inode = dentry->d_inode;
472 error = xfs_rmdir(XFS_I(dir), dentry);
473 if (likely(!error)) {
474 xfs_validate_fields(inode, &vattr);
475 xfs_validate_fields(dir, &vattr);
483 struct dentry *odentry,
485 struct dentry *ndentry)
487 struct inode *new_inode = ndentry->d_inode;
488 bhv_vnode_t *tvp; /* target directory */
492 tvp = vn_from_inode(ndir);
494 error = xfs_rename(XFS_I(odir), odentry, tvp, ndentry);
495 if (likely(!error)) {
497 xfs_validate_fields(new_inode, &vattr);
498 xfs_validate_fields(odir, &vattr);
500 xfs_validate_fields(ndir, &vattr);
506 * careful here - this function can get called recursively, so
507 * we need to be very careful about how much stack we use.
508 * uio is kmalloced for this reason...
512 struct dentry *dentry,
513 struct nameidata *nd)
518 link = kmalloc(MAXPATHLEN+1, GFP_KERNEL);
522 error = -xfs_readlink(XFS_I(dentry->d_inode), link);
526 nd_set_link(nd, link);
532 nd_set_link(nd, ERR_PTR(error));
538 struct dentry *dentry,
539 struct nameidata *nd,
542 char *s = nd_get_link(nd);
548 #ifdef CONFIG_XFS_POSIX_ACL
553 struct nameidata *nd)
555 return -xfs_access(XFS_I(inode), mode << 6, NULL);
558 #define xfs_vn_permission NULL
563 struct vfsmount *mnt,
564 struct dentry *dentry,
567 struct inode *inode = dentry->d_inode;
568 bhv_vattr_t vattr = { .va_mask = XFS_AT_STAT };
571 error = xfs_getattr(XFS_I(inode), &vattr, ATTR_LAZY);
572 if (likely(!error)) {
573 stat->size = i_size_read(inode);
574 stat->dev = inode->i_sb->s_dev;
575 stat->rdev = (vattr.va_rdev == 0) ? 0 :
576 MKDEV(sysv_major(vattr.va_rdev) & 0x1ff,
577 sysv_minor(vattr.va_rdev));
578 stat->mode = vattr.va_mode;
579 stat->nlink = vattr.va_nlink;
580 stat->uid = vattr.va_uid;
581 stat->gid = vattr.va_gid;
582 stat->ino = vattr.va_nodeid;
583 stat->atime = vattr.va_atime;
584 stat->mtime = vattr.va_mtime;
585 stat->ctime = vattr.va_ctime;
586 stat->blocks = vattr.va_nblocks;
587 stat->blksize = vattr.va_blocksize;
594 struct dentry *dentry,
597 struct inode *inode = dentry->d_inode;
598 unsigned int ia_valid = attr->ia_valid;
599 bhv_vattr_t vattr = { 0 };
603 if (ia_valid & ATTR_UID) {
604 vattr.va_mask |= XFS_AT_UID;
605 vattr.va_uid = attr->ia_uid;
607 if (ia_valid & ATTR_GID) {
608 vattr.va_mask |= XFS_AT_GID;
609 vattr.va_gid = attr->ia_gid;
611 if (ia_valid & ATTR_SIZE) {
612 vattr.va_mask |= XFS_AT_SIZE;
613 vattr.va_size = attr->ia_size;
615 if (ia_valid & ATTR_ATIME) {
616 vattr.va_mask |= XFS_AT_ATIME;
617 vattr.va_atime = attr->ia_atime;
618 inode->i_atime = attr->ia_atime;
620 if (ia_valid & ATTR_MTIME) {
621 vattr.va_mask |= XFS_AT_MTIME;
622 vattr.va_mtime = attr->ia_mtime;
624 if (ia_valid & ATTR_CTIME) {
625 vattr.va_mask |= XFS_AT_CTIME;
626 vattr.va_ctime = attr->ia_ctime;
628 if (ia_valid & ATTR_MODE) {
629 vattr.va_mask |= XFS_AT_MODE;
630 vattr.va_mode = attr->ia_mode;
631 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
632 inode->i_mode &= ~S_ISGID;
635 if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))
638 if ((ia_valid & ATTR_NO_BLOCK))
639 flags |= ATTR_NONBLOCK;
642 error = xfs_setattr(XFS_I(inode), &vattr, flags, NULL);
644 __vn_revalidate(vn_from_inode(inode), &vattr);
652 block_truncate_page(inode->i_mapping, inode->i_size, xfs_get_blocks);
657 struct dentry *dentry,
663 bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
664 char *attr = (char *)name;
669 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
672 attr += namesp->attr_namelen;
673 error = namesp->attr_capable(vp, NULL);
677 /* Convert Linux syscall to XFS internal ATTR flags */
678 if (flags & XATTR_CREATE)
679 xflags |= ATTR_CREATE;
680 if (flags & XATTR_REPLACE)
681 xflags |= ATTR_REPLACE;
682 xflags |= namesp->attr_flag;
683 return namesp->attr_set(vp, attr, (void *)data, size, xflags);
688 struct dentry *dentry,
693 bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
694 char *attr = (char *)name;
699 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
702 attr += namesp->attr_namelen;
703 error = namesp->attr_capable(vp, NULL);
707 /* Convert Linux syscall to XFS internal ATTR flags */
709 xflags |= ATTR_KERNOVAL;
712 xflags |= namesp->attr_flag;
713 return namesp->attr_get(vp, attr, (void *)data, size, xflags);
718 struct dentry *dentry,
722 bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
723 int error, xflags = ATTR_KERNAMELS;
727 xflags |= ATTR_KERNOVAL;
728 xflags |= capable(CAP_SYS_ADMIN) ? ATTR_KERNFULLS : ATTR_KERNORMALS;
730 error = attr_generic_list(vp, data, size, xflags, &result);
738 struct dentry *dentry,
741 bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
742 char *attr = (char *)name;
747 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
750 attr += namesp->attr_namelen;
751 error = namesp->attr_capable(vp, NULL);
754 xflags |= namesp->attr_flag;
755 return namesp->attr_remove(vp, attr, xflags);
759 const struct inode_operations xfs_inode_operations = {
760 .permission = xfs_vn_permission,
761 .truncate = xfs_vn_truncate,
762 .getattr = xfs_vn_getattr,
763 .setattr = xfs_vn_setattr,
764 .setxattr = xfs_vn_setxattr,
765 .getxattr = xfs_vn_getxattr,
766 .listxattr = xfs_vn_listxattr,
767 .removexattr = xfs_vn_removexattr,
770 const struct inode_operations xfs_dir_inode_operations = {
771 .create = xfs_vn_create,
772 .lookup = xfs_vn_lookup,
774 .unlink = xfs_vn_unlink,
775 .symlink = xfs_vn_symlink,
776 .mkdir = xfs_vn_mkdir,
777 .rmdir = xfs_vn_rmdir,
778 .mknod = xfs_vn_mknod,
779 .rename = xfs_vn_rename,
780 .permission = xfs_vn_permission,
781 .getattr = xfs_vn_getattr,
782 .setattr = xfs_vn_setattr,
783 .setxattr = xfs_vn_setxattr,
784 .getxattr = xfs_vn_getxattr,
785 .listxattr = xfs_vn_listxattr,
786 .removexattr = xfs_vn_removexattr,
789 const struct inode_operations xfs_symlink_inode_operations = {
790 .readlink = generic_readlink,
791 .follow_link = xfs_vn_follow_link,
792 .put_link = xfs_vn_put_link,
793 .permission = xfs_vn_permission,
794 .getattr = xfs_vn_getattr,
795 .setattr = xfs_vn_setattr,
796 .setxattr = xfs_vn_setxattr,
797 .getxattr = xfs_vn_getxattr,
798 .listxattr = xfs_vn_listxattr,
799 .removexattr = xfs_vn_removexattr,