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 * If the linux inode exists, mark it dirty.
75 * Used when commiting a dirty inode into a transaction so that
76 * the inode will get written back by the linux code
79 xfs_mark_inode_dirty_sync(
84 vp = XFS_ITOV_NULL(ip);
86 mark_inode_dirty_sync(vn_to_inode(vp));
90 * Change the requested timestamp in the given inode.
91 * We don't lock across timestamp updates, and we don't log them but
92 * we do record the fact that there is dirty information in core.
94 * NOTE -- callers MUST combine XFS_ICHGTIME_MOD or XFS_ICHGTIME_CHG
95 * with XFS_ICHGTIME_ACC to be sure that access time
96 * update will take. Calling first with XFS_ICHGTIME_ACC
97 * and then XFS_ICHGTIME_MOD may fail to modify the access
98 * timestamp if the filesystem is mounted noacctm.
105 struct inode *inode = vn_to_inode(XFS_ITOV(ip));
109 if (flags & XFS_ICHGTIME_MOD) {
111 ip->i_d.di_mtime.t_sec = (__int32_t)tv.tv_sec;
112 ip->i_d.di_mtime.t_nsec = (__int32_t)tv.tv_nsec;
114 if (flags & XFS_ICHGTIME_ACC) {
116 ip->i_d.di_atime.t_sec = (__int32_t)tv.tv_sec;
117 ip->i_d.di_atime.t_nsec = (__int32_t)tv.tv_nsec;
119 if (flags & XFS_ICHGTIME_CHG) {
121 ip->i_d.di_ctime.t_sec = (__int32_t)tv.tv_sec;
122 ip->i_d.di_ctime.t_nsec = (__int32_t)tv.tv_nsec;
126 * We update the i_update_core field _after_ changing
127 * the timestamps in order to coordinate properly with
128 * xfs_iflush() so that we don't lose timestamp updates.
129 * This keeps us from having to hold the inode lock
130 * while doing this. We use the SYNCHRONIZE macro to
131 * ensure that the compiler does not reorder the update
132 * of i_update_core above the timestamp updates above.
135 ip->i_update_core = 1;
136 if (!(inode->i_state & I_NEW))
137 mark_inode_dirty_sync(inode);
141 * Variant on the above which avoids querying the system clock
142 * in situations where we know the Linux inode timestamps have
143 * just been updated (and so we can update our inode cheaply).
154 * Atime updates for read() & friends are handled lazily now, and
155 * explicit updates must go through xfs_ichgtime()
157 ASSERT((flags & XFS_ICHGTIME_ACC) == 0);
160 * We're not supposed to change timestamps in readonly-mounted
161 * filesystems. Throw it away if anyone asks us.
163 if (unlikely(IS_RDONLY(inode)))
166 if (flags & XFS_ICHGTIME_MOD) {
167 tvp = &inode->i_mtime;
168 ip->i_d.di_mtime.t_sec = (__int32_t)tvp->tv_sec;
169 ip->i_d.di_mtime.t_nsec = (__int32_t)tvp->tv_nsec;
171 if (flags & XFS_ICHGTIME_CHG) {
172 tvp = &inode->i_ctime;
173 ip->i_d.di_ctime.t_sec = (__int32_t)tvp->tv_sec;
174 ip->i_d.di_ctime.t_nsec = (__int32_t)tvp->tv_nsec;
178 * We update the i_update_core field _after_ changing
179 * the timestamps in order to coordinate properly with
180 * xfs_iflush() so that we don't lose timestamp updates.
181 * This keeps us from having to hold the inode lock
182 * while doing this. We use the SYNCHRONIZE macro to
183 * ensure that the compiler does not reorder the update
184 * of i_update_core above the timestamp updates above.
187 ip->i_update_core = 1;
188 if (!(inode->i_state & I_NEW))
189 mark_inode_dirty_sync(inode);
194 * Pull the link count and size up from the xfs inode to the linux inode
200 struct xfs_inode *ip = XFS_I(inode);
203 inode->i_nlink = ip->i_d.di_nlink;
205 XFS_FSB_TO_BB(ip->i_mount, ip->i_d.di_nblocks +
207 /* we're under i_sem so i_size can't change under us */
208 size = XFS_ISIZE(ip);
209 if (i_size_read(inode) != size)
210 i_size_write(inode, size);
214 * Hook in SELinux. This is not quite correct yet, what we really need
215 * here (as we do for default ACLs) is a mechanism by which creation of
216 * these attrs can be journalled at inode creation time (along with the
217 * inode, of course, such that log replay can't cause these to be lost).
224 struct inode *ip = vn_to_inode(vp);
230 error = security_inode_init_security(ip, dir, &name, &value, &length);
232 if (error == -EOPNOTSUPP)
237 error = xfs_attr_set(XFS_I(ip), name, value,
238 length, ATTR_SECURE);
240 xfs_iflags_set(XFS_I(ip), XFS_IMODIFIED);
248 * Determine whether a process has a valid fs_struct (kernel daemons
249 * like knfsd don't have an fs_struct).
251 * XXX(hch): nfsd is broken, better fix it instead.
254 xfs_has_fs_struct(struct task_struct *task)
256 return (task->fs != init_task.fs);
263 struct dentry *dentry,
266 struct dentry teardown = {};
269 * If we can't add the ACL or we fail in
270 * xfs_init_security we must back out.
271 * ENOSPC can hit here, among other things.
273 teardown.d_inode = vn_to_inode(vp);
274 teardown.d_name = dentry->d_name;
277 xfs_rmdir(XFS_I(dir), &teardown);
279 xfs_remove(XFS_I(dir), &teardown);
286 struct dentry *dentry,
291 bhv_vnode_t *vp = NULL, *dvp = vn_from_inode(dir);
292 xfs_acl_t *default_acl = NULL;
293 attrexists_t test_default_acl = _ACL_DEFAULT_EXISTS;
297 * Irix uses Missed'em'V split, but doesn't want to see
298 * the upper 5 bits of (14bit) major.
300 if (unlikely(!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff))
303 if (unlikely(test_default_acl && test_default_acl(dvp))) {
304 if (!_ACL_ALLOC(default_acl)) {
307 if (!_ACL_GET_DEFAULT(dvp, default_acl)) {
308 _ACL_FREE(default_acl);
313 if (IS_POSIXACL(dir) && !default_acl && xfs_has_fs_struct(current))
314 mode &= ~current->fs->umask;
316 switch (mode & S_IFMT) {
317 case S_IFCHR: case S_IFBLK: case S_IFIFO: case S_IFSOCK:
318 rdev = sysv_encode_dev(rdev);
320 error = xfs_create(XFS_I(dir), dentry, mode, rdev, &vp, NULL);
323 error = xfs_mkdir(XFS_I(dir), dentry, mode, &vp, NULL);
330 if (unlikely(!error)) {
331 error = xfs_init_security(vp, dir);
333 xfs_cleanup_inode(dir, vp, dentry, mode);
336 if (unlikely(default_acl)) {
338 error = _ACL_INHERIT(vp, mode, default_acl);
340 xfs_iflags_set(XFS_I(vp), XFS_IMODIFIED);
342 xfs_cleanup_inode(dir, vp, dentry, mode);
344 _ACL_FREE(default_acl);
347 if (likely(!error)) {
349 ip = vn_to_inode(vp);
352 xfs_validate_fields(ip);
353 d_instantiate(dentry, ip);
354 xfs_validate_fields(dir);
362 struct dentry *dentry,
364 struct nameidata *nd)
366 return xfs_vn_mknod(dir, dentry, mode, 0);
372 struct dentry *dentry,
375 return xfs_vn_mknod(dir, dentry, mode|S_IFDIR, 0);
378 STATIC struct dentry *
381 struct dentry *dentry,
382 struct nameidata *nd)
387 if (dentry->d_name.len >= MAXNAMELEN)
388 return ERR_PTR(-ENAMETOOLONG);
390 error = xfs_lookup(XFS_I(dir), dentry, &cvp);
391 if (unlikely(error)) {
392 if (unlikely(error != ENOENT))
393 return ERR_PTR(-error);
398 return d_splice_alias(vn_to_inode(cvp), dentry);
403 struct dentry *old_dentry,
405 struct dentry *dentry)
407 struct inode *ip; /* inode of guy being linked to */
408 bhv_vnode_t *vp; /* vp of name being linked */
411 ip = old_dentry->d_inode; /* inode being linked to */
412 vp = vn_from_inode(ip);
415 error = xfs_link(XFS_I(dir), vp, dentry);
416 if (unlikely(error)) {
419 xfs_iflags_set(XFS_I(dir), XFS_IMODIFIED);
420 xfs_validate_fields(ip);
421 d_instantiate(dentry, ip);
429 struct dentry *dentry)
434 inode = dentry->d_inode;
436 error = xfs_remove(XFS_I(dir), dentry);
437 if (likely(!error)) {
438 xfs_validate_fields(dir); /* size needs update */
439 xfs_validate_fields(inode);
447 struct dentry *dentry,
451 bhv_vnode_t *cvp; /* used to lookup symlink to put in dentry */
458 (irix_symlink_mode ? 0777 & ~current->fs->umask : S_IRWXUGO);
460 error = xfs_symlink(XFS_I(dir), dentry, (char *)symname, mode,
462 if (likely(!error && cvp)) {
463 error = xfs_init_security(cvp, dir);
464 if (likely(!error)) {
465 ip = vn_to_inode(cvp);
466 d_instantiate(dentry, ip);
467 xfs_validate_fields(dir);
468 xfs_validate_fields(ip);
470 xfs_cleanup_inode(dir, cvp, dentry, 0);
479 struct dentry *dentry)
481 struct inode *inode = dentry->d_inode;
484 error = xfs_rmdir(XFS_I(dir), dentry);
485 if (likely(!error)) {
486 xfs_validate_fields(inode);
487 xfs_validate_fields(dir);
495 struct dentry *odentry,
497 struct dentry *ndentry)
499 struct inode *new_inode = ndentry->d_inode;
500 bhv_vnode_t *tvp; /* target directory */
503 tvp = vn_from_inode(ndir);
505 error = xfs_rename(XFS_I(odir), odentry, tvp, ndentry);
506 if (likely(!error)) {
508 xfs_validate_fields(new_inode);
509 xfs_validate_fields(odir);
511 xfs_validate_fields(ndir);
517 * careful here - this function can get called recursively, so
518 * we need to be very careful about how much stack we use.
519 * uio is kmalloced for this reason...
523 struct dentry *dentry,
524 struct nameidata *nd)
529 link = kmalloc(MAXPATHLEN+1, GFP_KERNEL);
533 error = -xfs_readlink(XFS_I(dentry->d_inode), link);
537 nd_set_link(nd, link);
543 nd_set_link(nd, ERR_PTR(error));
549 struct dentry *dentry,
550 struct nameidata *nd,
553 char *s = nd_get_link(nd);
559 #ifdef CONFIG_XFS_POSIX_ACL
564 struct nameidata *nd)
566 return -xfs_access(XFS_I(inode), mode << 6, NULL);
569 #define xfs_vn_permission NULL
574 struct vfsmount *mnt,
575 struct dentry *dentry,
578 struct inode *inode = dentry->d_inode;
579 struct xfs_inode *ip = XFS_I(inode);
580 struct xfs_mount *mp = ip->i_mount;
582 xfs_itrace_entry(ip);
584 if (XFS_FORCED_SHUTDOWN(mp))
585 return XFS_ERROR(EIO);
587 stat->size = XFS_ISIZE(ip);
588 stat->dev = inode->i_sb->s_dev;
589 stat->mode = ip->i_d.di_mode;
590 stat->nlink = ip->i_d.di_nlink;
591 stat->uid = ip->i_d.di_uid;
592 stat->gid = ip->i_d.di_gid;
593 stat->ino = ip->i_ino;
595 stat->ino += mp->m_inoadd;
597 stat->atime = inode->i_atime;
598 stat->mtime.tv_sec = ip->i_d.di_mtime.t_sec;
599 stat->mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
600 stat->ctime.tv_sec = ip->i_d.di_ctime.t_sec;
601 stat->ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
603 XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
606 switch (inode->i_mode & S_IFMT) {
609 stat->blksize = BLKDEV_IOSIZE;
610 stat->rdev = MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
611 sysv_minor(ip->i_df.if_u2.if_rdev));
614 if (ip->i_d.di_flags & XFS_DIFLAG_REALTIME) {
616 * If the file blocks are being allocated from a
617 * realtime volume, then return the inode's realtime
618 * extent size or the realtime volume's extent size.
621 xfs_get_extsz_hint(ip) << mp->m_sb.sb_blocklog;
623 stat->blksize = xfs_preferred_iosize(mp);
633 struct dentry *dentry,
636 struct inode *inode = dentry->d_inode;
637 unsigned int ia_valid = attr->ia_valid;
638 bhv_vattr_t vattr = { 0 };
642 if (ia_valid & ATTR_UID) {
643 vattr.va_mask |= XFS_AT_UID;
644 vattr.va_uid = attr->ia_uid;
646 if (ia_valid & ATTR_GID) {
647 vattr.va_mask |= XFS_AT_GID;
648 vattr.va_gid = attr->ia_gid;
650 if (ia_valid & ATTR_SIZE) {
651 vattr.va_mask |= XFS_AT_SIZE;
652 vattr.va_size = attr->ia_size;
654 if (ia_valid & ATTR_ATIME) {
655 vattr.va_mask |= XFS_AT_ATIME;
656 vattr.va_atime = attr->ia_atime;
657 inode->i_atime = attr->ia_atime;
659 if (ia_valid & ATTR_MTIME) {
660 vattr.va_mask |= XFS_AT_MTIME;
661 vattr.va_mtime = attr->ia_mtime;
663 if (ia_valid & ATTR_CTIME) {
664 vattr.va_mask |= XFS_AT_CTIME;
665 vattr.va_ctime = attr->ia_ctime;
667 if (ia_valid & ATTR_MODE) {
668 vattr.va_mask |= XFS_AT_MODE;
669 vattr.va_mode = attr->ia_mode;
670 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
671 inode->i_mode &= ~S_ISGID;
674 if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))
677 if ((ia_valid & ATTR_NO_BLOCK))
678 flags |= ATTR_NONBLOCK;
681 error = xfs_setattr(XFS_I(inode), &vattr, flags, NULL);
683 vn_revalidate(vn_from_inode(inode));
691 block_truncate_page(inode->i_mapping, inode->i_size, xfs_get_blocks);
696 struct dentry *dentry,
702 bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
703 char *attr = (char *)name;
708 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
711 attr += namesp->attr_namelen;
712 error = namesp->attr_capable(vp, NULL);
716 /* Convert Linux syscall to XFS internal ATTR flags */
717 if (flags & XATTR_CREATE)
718 xflags |= ATTR_CREATE;
719 if (flags & XATTR_REPLACE)
720 xflags |= ATTR_REPLACE;
721 xflags |= namesp->attr_flag;
722 return namesp->attr_set(vp, attr, (void *)data, size, xflags);
727 struct dentry *dentry,
732 bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
733 char *attr = (char *)name;
738 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
741 attr += namesp->attr_namelen;
742 error = namesp->attr_capable(vp, NULL);
746 /* Convert Linux syscall to XFS internal ATTR flags */
748 xflags |= ATTR_KERNOVAL;
751 xflags |= namesp->attr_flag;
752 return namesp->attr_get(vp, attr, (void *)data, size, xflags);
757 struct dentry *dentry,
761 bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
762 int error, xflags = ATTR_KERNAMELS;
766 xflags |= ATTR_KERNOVAL;
767 xflags |= capable(CAP_SYS_ADMIN) ? ATTR_KERNFULLS : ATTR_KERNORMALS;
769 error = attr_generic_list(vp, data, size, xflags, &result);
777 struct dentry *dentry,
780 bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
781 char *attr = (char *)name;
786 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
789 attr += namesp->attr_namelen;
790 error = namesp->attr_capable(vp, NULL);
793 xflags |= namesp->attr_flag;
794 return namesp->attr_remove(vp, attr, xflags);
798 const struct inode_operations xfs_inode_operations = {
799 .permission = xfs_vn_permission,
800 .truncate = xfs_vn_truncate,
801 .getattr = xfs_vn_getattr,
802 .setattr = xfs_vn_setattr,
803 .setxattr = xfs_vn_setxattr,
804 .getxattr = xfs_vn_getxattr,
805 .listxattr = xfs_vn_listxattr,
806 .removexattr = xfs_vn_removexattr,
809 const struct inode_operations xfs_dir_inode_operations = {
810 .create = xfs_vn_create,
811 .lookup = xfs_vn_lookup,
813 .unlink = xfs_vn_unlink,
814 .symlink = xfs_vn_symlink,
815 .mkdir = xfs_vn_mkdir,
816 .rmdir = xfs_vn_rmdir,
817 .mknod = xfs_vn_mknod,
818 .rename = xfs_vn_rename,
819 .permission = xfs_vn_permission,
820 .getattr = xfs_vn_getattr,
821 .setattr = xfs_vn_setattr,
822 .setxattr = xfs_vn_setxattr,
823 .getxattr = xfs_vn_getxattr,
824 .listxattr = xfs_vn_listxattr,
825 .removexattr = xfs_vn_removexattr,
828 const struct inode_operations xfs_symlink_inode_operations = {
829 .readlink = generic_readlink,
830 .follow_link = xfs_vn_follow_link,
831 .put_link = xfs_vn_put_link,
832 .permission = xfs_vn_permission,
833 .getattr = xfs_vn_getattr,
834 .setattr = xfs_vn_setattr,
835 .setxattr = xfs_vn_setxattr,
836 .getxattr = xfs_vn_getxattr,
837 .listxattr = xfs_vn_listxattr,
838 .removexattr = xfs_vn_removexattr,