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>
55 #include <linux/falloc.h>
58 * Bring the atime in the XFS inode uptodate.
59 * Used before logging the inode to disk or when the Linux inode goes away.
62 xfs_synchronize_atime(
65 struct inode *inode = ip->i_vnode;
68 ip->i_d.di_atime.t_sec = (__int32_t)inode->i_atime.tv_sec;
69 ip->i_d.di_atime.t_nsec = (__int32_t)inode->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(
82 struct inode *inode = ip->i_vnode;
85 mark_inode_dirty_sync(inode);
89 * Change the requested timestamp in the given inode.
90 * We don't lock across timestamp updates, and we don't log them but
91 * we do record the fact that there is dirty information in core.
93 * NOTE -- callers MUST combine XFS_ICHGTIME_MOD or XFS_ICHGTIME_CHG
94 * with XFS_ICHGTIME_ACC to be sure that access time
95 * update will take. Calling first with XFS_ICHGTIME_ACC
96 * and then XFS_ICHGTIME_MOD may fail to modify the access
97 * timestamp if the filesystem is mounted noacctm.
104 struct inode *inode = vn_to_inode(XFS_ITOV(ip));
108 if (flags & XFS_ICHGTIME_MOD) {
110 ip->i_d.di_mtime.t_sec = (__int32_t)tv.tv_sec;
111 ip->i_d.di_mtime.t_nsec = (__int32_t)tv.tv_nsec;
113 if (flags & XFS_ICHGTIME_ACC) {
115 ip->i_d.di_atime.t_sec = (__int32_t)tv.tv_sec;
116 ip->i_d.di_atime.t_nsec = (__int32_t)tv.tv_nsec;
118 if (flags & XFS_ICHGTIME_CHG) {
120 ip->i_d.di_ctime.t_sec = (__int32_t)tv.tv_sec;
121 ip->i_d.di_ctime.t_nsec = (__int32_t)tv.tv_nsec;
125 * We update the i_update_core field _after_ changing
126 * the timestamps in order to coordinate properly with
127 * xfs_iflush() so that we don't lose timestamp updates.
128 * This keeps us from having to hold the inode lock
129 * while doing this. We use the SYNCHRONIZE macro to
130 * ensure that the compiler does not reorder the update
131 * of i_update_core above the timestamp updates above.
134 ip->i_update_core = 1;
135 if (!(inode->i_state & I_NEW))
136 mark_inode_dirty_sync(inode);
140 * Variant on the above which avoids querying the system clock
141 * in situations where we know the Linux inode timestamps have
142 * just been updated (and so we can update our inode cheaply).
153 * Atime updates for read() & friends are handled lazily now, and
154 * explicit updates must go through xfs_ichgtime()
156 ASSERT((flags & XFS_ICHGTIME_ACC) == 0);
159 * We're not supposed to change timestamps in readonly-mounted
160 * filesystems. Throw it away if anyone asks us.
162 if (unlikely(IS_RDONLY(inode)))
165 if (flags & XFS_ICHGTIME_MOD) {
166 tvp = &inode->i_mtime;
167 ip->i_d.di_mtime.t_sec = (__int32_t)tvp->tv_sec;
168 ip->i_d.di_mtime.t_nsec = (__int32_t)tvp->tv_nsec;
170 if (flags & XFS_ICHGTIME_CHG) {
171 tvp = &inode->i_ctime;
172 ip->i_d.di_ctime.t_sec = (__int32_t)tvp->tv_sec;
173 ip->i_d.di_ctime.t_nsec = (__int32_t)tvp->tv_nsec;
177 * We update the i_update_core field _after_ changing
178 * the timestamps in order to coordinate properly with
179 * xfs_iflush() so that we don't lose timestamp updates.
180 * This keeps us from having to hold the inode lock
181 * while doing this. We use the SYNCHRONIZE macro to
182 * ensure that the compiler does not reorder the update
183 * of i_update_core above the timestamp updates above.
186 ip->i_update_core = 1;
187 if (!(inode->i_state & I_NEW))
188 mark_inode_dirty_sync(inode);
193 * Pull the link count and size up from the xfs inode to the linux inode
199 struct xfs_inode *ip = XFS_I(inode);
202 /* we're under i_sem so i_size can't change under us */
203 size = XFS_ISIZE(ip);
204 if (i_size_read(inode) != size)
205 i_size_write(inode, size);
209 * Hook in SELinux. This is not quite correct yet, what we really need
210 * here (as we do for default ACLs) is a mechanism by which creation of
211 * these attrs can be journalled at inode creation time (along with the
212 * inode, of course, such that log replay can't cause these to be lost).
219 struct xfs_inode *ip = XFS_I(inode);
225 error = security_inode_init_security(inode, dir, &name,
228 if (error == -EOPNOTSUPP)
233 error = xfs_attr_set(ip, name, value, length, ATTR_SECURE);
235 xfs_iflags_set(ip, XFS_IMODIFIED);
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 = inode;
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 struct xfs_inode *ip = NULL;
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 (test_default_acl && test_default_acl(dir)) {
287 if (!_ACL_ALLOC(default_acl)) {
290 if (!_ACL_GET_DEFAULT(dir, default_acl)) {
291 _ACL_FREE(default_acl);
296 if (IS_POSIXACL(dir) && !default_acl)
297 mode &= ~current->fs->umask;
299 switch (mode & S_IFMT) {
304 rdev = sysv_encode_dev(rdev);
306 error = xfs_create(XFS_I(dir), dentry, mode, rdev, &ip, NULL);
309 error = xfs_mkdir(XFS_I(dir), dentry, mode, &ip, NULL);
321 error = xfs_init_security(inode, dir);
323 goto out_cleanup_inode;
326 error = _ACL_INHERIT(inode, mode, default_acl);
328 goto out_cleanup_inode;
329 xfs_iflags_set(ip, XFS_IMODIFIED);
330 _ACL_FREE(default_acl);
335 xfs_validate_fields(inode);
336 d_instantiate(dentry, inode);
337 xfs_validate_fields(dir);
341 xfs_cleanup_inode(dir, inode, dentry, mode);
344 _ACL_FREE(default_acl);
351 struct dentry *dentry,
353 struct nameidata *nd)
355 return xfs_vn_mknod(dir, dentry, mode, 0);
361 struct dentry *dentry,
364 return xfs_vn_mknod(dir, dentry, mode|S_IFDIR, 0);
367 STATIC struct dentry *
370 struct dentry *dentry,
371 struct nameidata *nd)
373 struct xfs_inode *cip;
376 if (dentry->d_name.len >= MAXNAMELEN)
377 return ERR_PTR(-ENAMETOOLONG);
379 error = xfs_lookup(XFS_I(dir), dentry, &cip);
380 if (unlikely(error)) {
381 if (unlikely(error != ENOENT))
382 return ERR_PTR(-error);
387 return d_splice_alias(cip->i_vnode, dentry);
392 struct dentry *old_dentry,
394 struct dentry *dentry)
396 struct inode *inode; /* inode of guy being linked to */
399 inode = old_dentry->d_inode;
402 error = xfs_link(XFS_I(dir), XFS_I(inode), dentry);
403 if (unlikely(error)) {
408 xfs_iflags_set(XFS_I(dir), XFS_IMODIFIED);
409 xfs_validate_fields(inode);
410 d_instantiate(dentry, inode);
417 struct dentry *dentry)
422 inode = dentry->d_inode;
424 error = xfs_remove(XFS_I(dir), dentry);
425 if (likely(!error)) {
426 xfs_validate_fields(dir); /* size needs update */
427 xfs_validate_fields(inode);
435 struct dentry *dentry,
439 struct xfs_inode *cip = NULL;
444 (irix_symlink_mode ? 0777 & ~current->fs->umask : S_IRWXUGO);
446 error = xfs_symlink(XFS_I(dir), dentry, (char *)symname, mode,
451 inode = cip->i_vnode;
453 error = xfs_init_security(inode, dir);
455 goto out_cleanup_inode;
457 d_instantiate(dentry, inode);
458 xfs_validate_fields(dir);
459 xfs_validate_fields(inode);
463 xfs_cleanup_inode(dir, inode, dentry, 0);
471 struct dentry *dentry)
473 struct inode *inode = dentry->d_inode;
476 error = xfs_rmdir(XFS_I(dir), dentry);
477 if (likely(!error)) {
478 xfs_validate_fields(inode);
479 xfs_validate_fields(dir);
487 struct dentry *odentry,
489 struct dentry *ndentry)
491 struct inode *new_inode = ndentry->d_inode;
494 error = xfs_rename(XFS_I(odir), odentry, XFS_I(ndir), ndentry);
495 if (likely(!error)) {
497 xfs_validate_fields(new_inode);
498 xfs_validate_fields(odir);
500 xfs_validate_fields(ndir);
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
554 struct xfs_inode *ip = XFS_I(inode);
557 xfs_itrace_entry(ip);
559 if (XFS_IFORK_Q(ip)) {
560 error = xfs_acl_iaccess(ip, mask, NULL);
572 struct nameidata *nd)
574 return generic_permission(inode, mask, xfs_check_acl);
577 #define xfs_vn_permission NULL
582 struct vfsmount *mnt,
583 struct dentry *dentry,
586 struct inode *inode = dentry->d_inode;
587 struct xfs_inode *ip = XFS_I(inode);
588 struct xfs_mount *mp = ip->i_mount;
590 xfs_itrace_entry(ip);
592 if (XFS_FORCED_SHUTDOWN(mp))
593 return XFS_ERROR(EIO);
595 stat->size = XFS_ISIZE(ip);
596 stat->dev = inode->i_sb->s_dev;
597 stat->mode = ip->i_d.di_mode;
598 stat->nlink = ip->i_d.di_nlink;
599 stat->uid = ip->i_d.di_uid;
600 stat->gid = ip->i_d.di_gid;
601 stat->ino = ip->i_ino;
603 stat->ino += mp->m_inoadd;
605 stat->atime = inode->i_atime;
606 stat->mtime.tv_sec = ip->i_d.di_mtime.t_sec;
607 stat->mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
608 stat->ctime.tv_sec = ip->i_d.di_ctime.t_sec;
609 stat->ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
611 XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
614 switch (inode->i_mode & S_IFMT) {
617 stat->blksize = BLKDEV_IOSIZE;
618 stat->rdev = MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
619 sysv_minor(ip->i_df.if_u2.if_rdev));
622 if (XFS_IS_REALTIME_INODE(ip)) {
624 * If the file blocks are being allocated from a
625 * realtime volume, then return the inode's realtime
626 * extent size or the realtime volume's extent size.
629 xfs_get_extsz_hint(ip) << mp->m_sb.sb_blocklog;
631 stat->blksize = xfs_preferred_iosize(mp);
641 struct dentry *dentry,
644 struct inode *inode = dentry->d_inode;
645 unsigned int ia_valid = attr->ia_valid;
646 bhv_vattr_t vattr = { 0 };
650 if (ia_valid & ATTR_UID) {
651 vattr.va_mask |= XFS_AT_UID;
652 vattr.va_uid = attr->ia_uid;
654 if (ia_valid & ATTR_GID) {
655 vattr.va_mask |= XFS_AT_GID;
656 vattr.va_gid = attr->ia_gid;
658 if (ia_valid & ATTR_SIZE) {
659 vattr.va_mask |= XFS_AT_SIZE;
660 vattr.va_size = attr->ia_size;
662 if (ia_valid & ATTR_ATIME) {
663 vattr.va_mask |= XFS_AT_ATIME;
664 vattr.va_atime = attr->ia_atime;
665 inode->i_atime = attr->ia_atime;
667 if (ia_valid & ATTR_MTIME) {
668 vattr.va_mask |= XFS_AT_MTIME;
669 vattr.va_mtime = attr->ia_mtime;
671 if (ia_valid & ATTR_CTIME) {
672 vattr.va_mask |= XFS_AT_CTIME;
673 vattr.va_ctime = attr->ia_ctime;
675 if (ia_valid & ATTR_MODE) {
676 vattr.va_mask |= XFS_AT_MODE;
677 vattr.va_mode = attr->ia_mode;
678 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
679 inode->i_mode &= ~S_ISGID;
682 if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))
685 if ((ia_valid & ATTR_NO_BLOCK))
686 flags |= ATTR_NONBLOCK;
689 error = xfs_setattr(XFS_I(inode), &vattr, flags, NULL);
691 vn_revalidate(vn_from_inode(inode));
696 * block_truncate_page can return an error, but we can't propagate it
697 * at all here. Leave a complaint + stack trace in the syslog because
698 * this could be bad. If it is bad, we need to propagate the error further.
705 error = block_truncate_page(inode->i_mapping, inode->i_size,
712 struct dentry *dentry,
718 bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
719 char *attr = (char *)name;
724 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
727 attr += namesp->attr_namelen;
728 error = namesp->attr_capable(vp, NULL);
732 /* Convert Linux syscall to XFS internal ATTR flags */
733 if (flags & XATTR_CREATE)
734 xflags |= ATTR_CREATE;
735 if (flags & XATTR_REPLACE)
736 xflags |= ATTR_REPLACE;
737 xflags |= namesp->attr_flag;
738 return namesp->attr_set(vp, attr, (void *)data, size, xflags);
743 struct dentry *dentry,
748 bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
749 char *attr = (char *)name;
754 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
757 attr += namesp->attr_namelen;
758 error = namesp->attr_capable(vp, NULL);
762 /* Convert Linux syscall to XFS internal ATTR flags */
764 xflags |= ATTR_KERNOVAL;
767 xflags |= namesp->attr_flag;
768 return namesp->attr_get(vp, attr, (void *)data, size, xflags);
773 struct dentry *dentry,
777 bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
778 int error, xflags = ATTR_KERNAMELS;
782 xflags |= ATTR_KERNOVAL;
783 xflags |= capable(CAP_SYS_ADMIN) ? ATTR_KERNFULLS : ATTR_KERNORMALS;
785 error = attr_generic_list(vp, data, size, xflags, &result);
793 struct dentry *dentry,
796 bhv_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);
809 xflags |= namesp->attr_flag;
810 return namesp->attr_remove(vp, attr, xflags);
823 xfs_inode_t *ip = XFS_I(inode);
825 /* preallocation on directories not yet supported */
827 if (S_ISDIR(inode->i_mode))
834 xfs_ilock(ip, XFS_IOLOCK_EXCL);
835 error = xfs_change_file_space(ip, XFS_IOC_RESVSP, &bf,
836 0, NULL, ATTR_NOLOCK);
837 if (!error && !(mode & FALLOC_FL_KEEP_SIZE) &&
838 offset + len > i_size_read(inode))
839 new_size = offset + len;
841 /* Change file size if needed */
845 va.va_mask = XFS_AT_SIZE;
846 va.va_size = new_size;
847 error = xfs_setattr(ip, &va, ATTR_NOLOCK, NULL);
850 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
855 const struct inode_operations xfs_inode_operations = {
856 .permission = xfs_vn_permission,
857 .truncate = xfs_vn_truncate,
858 .getattr = xfs_vn_getattr,
859 .setattr = xfs_vn_setattr,
860 .setxattr = xfs_vn_setxattr,
861 .getxattr = xfs_vn_getxattr,
862 .listxattr = xfs_vn_listxattr,
863 .removexattr = xfs_vn_removexattr,
864 .fallocate = xfs_vn_fallocate,
867 const struct inode_operations xfs_dir_inode_operations = {
868 .create = xfs_vn_create,
869 .lookup = xfs_vn_lookup,
871 .unlink = xfs_vn_unlink,
872 .symlink = xfs_vn_symlink,
873 .mkdir = xfs_vn_mkdir,
874 .rmdir = xfs_vn_rmdir,
875 .mknod = xfs_vn_mknod,
876 .rename = xfs_vn_rename,
877 .permission = xfs_vn_permission,
878 .getattr = xfs_vn_getattr,
879 .setattr = xfs_vn_setattr,
880 .setxattr = xfs_vn_setxattr,
881 .getxattr = xfs_vn_getxattr,
882 .listxattr = xfs_vn_listxattr,
883 .removexattr = xfs_vn_removexattr,
886 const struct inode_operations xfs_symlink_inode_operations = {
887 .readlink = generic_readlink,
888 .follow_link = xfs_vn_follow_link,
889 .put_link = xfs_vn_put_link,
890 .permission = xfs_vn_permission,
891 .getattr = xfs_vn_getattr,
892 .setattr = xfs_vn_setattr,
893 .setxattr = xfs_vn_setxattr,
894 .getxattr = xfs_vn_getxattr,
895 .listxattr = xfs_vn_listxattr,
896 .removexattr = xfs_vn_removexattr,