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);
244 struct xfs_name *namep,
245 struct dentry *dentry)
247 namep->name = dentry->d_name.name;
248 namep->len = dentry->d_name.len;
255 struct dentry *dentry,
258 struct xfs_name teardown;
261 * If we can't add the ACL or we fail in
262 * xfs_init_security we must back out.
263 * ENOSPC can hit here, among other things.
265 xfs_dentry_to_name(&teardown, dentry);
268 xfs_rmdir(XFS_I(dir), &teardown, XFS_I(inode));
270 xfs_remove(XFS_I(dir), &teardown, XFS_I(inode));
277 struct dentry *dentry,
282 struct xfs_inode *ip = NULL;
283 xfs_acl_t *default_acl = NULL;
284 struct xfs_name name;
285 attrexists_t test_default_acl = _ACL_DEFAULT_EXISTS;
289 * Irix uses Missed'em'V split, but doesn't want to see
290 * the upper 5 bits of (14bit) major.
292 if (unlikely(!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff))
295 if (test_default_acl && test_default_acl(dir)) {
296 if (!_ACL_ALLOC(default_acl)) {
299 if (!_ACL_GET_DEFAULT(dir, default_acl)) {
300 _ACL_FREE(default_acl);
305 xfs_dentry_to_name(&name, dentry);
307 if (IS_POSIXACL(dir) && !default_acl)
308 mode &= ~current->fs->umask;
310 switch (mode & S_IFMT) {
315 rdev = sysv_encode_dev(rdev);
317 error = xfs_create(XFS_I(dir), &name, mode, rdev, &ip, NULL);
320 error = xfs_mkdir(XFS_I(dir), &name, mode, &ip, NULL);
332 error = xfs_init_security(inode, dir);
334 goto out_cleanup_inode;
337 error = _ACL_INHERIT(inode, mode, default_acl);
339 goto out_cleanup_inode;
340 xfs_iflags_set(ip, XFS_IMODIFIED);
341 _ACL_FREE(default_acl);
346 xfs_validate_fields(inode);
347 d_instantiate(dentry, inode);
348 xfs_validate_fields(dir);
352 xfs_cleanup_inode(dir, inode, dentry, mode);
355 _ACL_FREE(default_acl);
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)
384 struct xfs_inode *cip;
385 struct xfs_name name;
388 if (dentry->d_name.len >= MAXNAMELEN)
389 return ERR_PTR(-ENAMETOOLONG);
391 xfs_dentry_to_name(&name, dentry);
392 error = xfs_lookup(XFS_I(dir), &name, &cip);
393 if (unlikely(error)) {
394 if (unlikely(error != ENOENT))
395 return ERR_PTR(-error);
400 return d_splice_alias(cip->i_vnode, dentry);
405 struct dentry *old_dentry,
407 struct dentry *dentry)
409 struct inode *inode; /* inode of guy being linked to */
410 struct xfs_name name;
413 inode = old_dentry->d_inode;
414 xfs_dentry_to_name(&name, dentry);
417 error = xfs_link(XFS_I(dir), XFS_I(inode), &name);
418 if (unlikely(error)) {
423 xfs_iflags_set(XFS_I(dir), XFS_IMODIFIED);
424 xfs_validate_fields(inode);
425 d_instantiate(dentry, inode);
432 struct dentry *dentry)
435 struct xfs_name name;
438 inode = dentry->d_inode;
439 xfs_dentry_to_name(&name, dentry);
441 error = xfs_remove(XFS_I(dir), &name, XFS_I(inode));
442 if (likely(!error)) {
443 xfs_validate_fields(dir); /* size needs update */
444 xfs_validate_fields(inode);
452 struct dentry *dentry,
456 struct xfs_inode *cip = NULL;
457 struct xfs_name name;
462 (irix_symlink_mode ? 0777 & ~current->fs->umask : S_IRWXUGO);
463 xfs_dentry_to_name(&name, dentry);
465 error = xfs_symlink(XFS_I(dir), &name, symname, mode, &cip, NULL);
469 inode = cip->i_vnode;
471 error = xfs_init_security(inode, dir);
473 goto out_cleanup_inode;
475 d_instantiate(dentry, inode);
476 xfs_validate_fields(dir);
477 xfs_validate_fields(inode);
481 xfs_cleanup_inode(dir, inode, dentry, 0);
489 struct dentry *dentry)
491 struct inode *inode = dentry->d_inode;
492 struct xfs_name name;
495 xfs_dentry_to_name(&name, dentry);
497 error = xfs_rmdir(XFS_I(dir), &name, XFS_I(inode));
498 if (likely(!error)) {
499 xfs_validate_fields(inode);
500 xfs_validate_fields(dir);
508 struct dentry *odentry,
510 struct dentry *ndentry)
512 struct inode *new_inode = ndentry->d_inode;
513 struct xfs_name oname;
514 struct xfs_name nname;
517 xfs_dentry_to_name(&oname, odentry);
518 xfs_dentry_to_name(&nname, ndentry);
520 error = xfs_rename(XFS_I(odir), &oname, XFS_I(odentry->d_inode),
521 XFS_I(ndir), &nname);
522 if (likely(!error)) {
524 xfs_validate_fields(new_inode);
525 xfs_validate_fields(odir);
527 xfs_validate_fields(ndir);
533 * careful here - this function can get called recursively, so
534 * we need to be very careful about how much stack we use.
535 * uio is kmalloced for this reason...
539 struct dentry *dentry,
540 struct nameidata *nd)
545 link = kmalloc(MAXPATHLEN+1, GFP_KERNEL);
549 error = -xfs_readlink(XFS_I(dentry->d_inode), link);
553 nd_set_link(nd, link);
559 nd_set_link(nd, ERR_PTR(error));
565 struct dentry *dentry,
566 struct nameidata *nd,
569 char *s = nd_get_link(nd);
575 #ifdef CONFIG_XFS_POSIX_ACL
581 struct xfs_inode *ip = XFS_I(inode);
584 xfs_itrace_entry(ip);
586 if (XFS_IFORK_Q(ip)) {
587 error = xfs_acl_iaccess(ip, mask, NULL);
599 struct nameidata *nd)
601 return generic_permission(inode, mask, xfs_check_acl);
604 #define xfs_vn_permission NULL
609 struct vfsmount *mnt,
610 struct dentry *dentry,
613 struct inode *inode = dentry->d_inode;
614 struct xfs_inode *ip = XFS_I(inode);
615 struct xfs_mount *mp = ip->i_mount;
617 xfs_itrace_entry(ip);
619 if (XFS_FORCED_SHUTDOWN(mp))
620 return XFS_ERROR(EIO);
622 stat->size = XFS_ISIZE(ip);
623 stat->dev = inode->i_sb->s_dev;
624 stat->mode = ip->i_d.di_mode;
625 stat->nlink = ip->i_d.di_nlink;
626 stat->uid = ip->i_d.di_uid;
627 stat->gid = ip->i_d.di_gid;
628 stat->ino = ip->i_ino;
630 stat->ino += mp->m_inoadd;
632 stat->atime = inode->i_atime;
633 stat->mtime.tv_sec = ip->i_d.di_mtime.t_sec;
634 stat->mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
635 stat->ctime.tv_sec = ip->i_d.di_ctime.t_sec;
636 stat->ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
638 XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
641 switch (inode->i_mode & S_IFMT) {
644 stat->blksize = BLKDEV_IOSIZE;
645 stat->rdev = MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
646 sysv_minor(ip->i_df.if_u2.if_rdev));
649 if (XFS_IS_REALTIME_INODE(ip)) {
651 * If the file blocks are being allocated from a
652 * realtime volume, then return the inode's realtime
653 * extent size or the realtime volume's extent size.
656 xfs_get_extsz_hint(ip) << mp->m_sb.sb_blocklog;
658 stat->blksize = xfs_preferred_iosize(mp);
668 struct dentry *dentry,
671 struct inode *inode = dentry->d_inode;
672 unsigned int ia_valid = attr->ia_valid;
673 bhv_vattr_t vattr = { 0 };
677 if (ia_valid & ATTR_UID) {
678 vattr.va_mask |= XFS_AT_UID;
679 vattr.va_uid = attr->ia_uid;
681 if (ia_valid & ATTR_GID) {
682 vattr.va_mask |= XFS_AT_GID;
683 vattr.va_gid = attr->ia_gid;
685 if (ia_valid & ATTR_SIZE) {
686 vattr.va_mask |= XFS_AT_SIZE;
687 vattr.va_size = attr->ia_size;
689 if (ia_valid & ATTR_ATIME) {
690 vattr.va_mask |= XFS_AT_ATIME;
691 vattr.va_atime = attr->ia_atime;
692 inode->i_atime = attr->ia_atime;
694 if (ia_valid & ATTR_MTIME) {
695 vattr.va_mask |= XFS_AT_MTIME;
696 vattr.va_mtime = attr->ia_mtime;
698 if (ia_valid & ATTR_CTIME) {
699 vattr.va_mask |= XFS_AT_CTIME;
700 vattr.va_ctime = attr->ia_ctime;
702 if (ia_valid & ATTR_MODE) {
703 vattr.va_mask |= XFS_AT_MODE;
704 vattr.va_mode = attr->ia_mode;
705 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
706 inode->i_mode &= ~S_ISGID;
709 if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))
712 if ((ia_valid & ATTR_NO_BLOCK))
713 flags |= ATTR_NONBLOCK;
716 error = xfs_setattr(XFS_I(inode), &vattr, flags, NULL);
718 vn_revalidate(vn_from_inode(inode));
723 * block_truncate_page can return an error, but we can't propagate it
724 * at all here. Leave a complaint + stack trace in the syslog because
725 * this could be bad. If it is bad, we need to propagate the error further.
732 error = block_truncate_page(inode->i_mapping, inode->i_size,
739 struct dentry *dentry,
745 bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
746 char *attr = (char *)name;
751 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
754 attr += namesp->attr_namelen;
755 error = namesp->attr_capable(vp, NULL);
759 /* Convert Linux syscall to XFS internal ATTR flags */
760 if (flags & XATTR_CREATE)
761 xflags |= ATTR_CREATE;
762 if (flags & XATTR_REPLACE)
763 xflags |= ATTR_REPLACE;
764 xflags |= namesp->attr_flag;
765 return namesp->attr_set(vp, attr, (void *)data, size, xflags);
770 struct dentry *dentry,
775 bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
776 char *attr = (char *)name;
781 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
784 attr += namesp->attr_namelen;
785 error = namesp->attr_capable(vp, NULL);
789 /* Convert Linux syscall to XFS internal ATTR flags */
791 xflags |= ATTR_KERNOVAL;
794 xflags |= namesp->attr_flag;
795 return namesp->attr_get(vp, attr, (void *)data, size, xflags);
800 struct dentry *dentry,
804 bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
805 int error, xflags = ATTR_KERNAMELS;
809 xflags |= ATTR_KERNOVAL;
810 xflags |= capable(CAP_SYS_ADMIN) ? ATTR_KERNFULLS : ATTR_KERNORMALS;
812 error = attr_generic_list(vp, data, size, xflags, &result);
820 struct dentry *dentry,
823 bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
824 char *attr = (char *)name;
829 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
832 attr += namesp->attr_namelen;
833 error = namesp->attr_capable(vp, NULL);
836 xflags |= namesp->attr_flag;
837 return namesp->attr_remove(vp, attr, xflags);
850 xfs_inode_t *ip = XFS_I(inode);
852 /* preallocation on directories not yet supported */
854 if (S_ISDIR(inode->i_mode))
861 xfs_ilock(ip, XFS_IOLOCK_EXCL);
862 error = xfs_change_file_space(ip, XFS_IOC_RESVSP, &bf,
863 0, NULL, ATTR_NOLOCK);
864 if (!error && !(mode & FALLOC_FL_KEEP_SIZE) &&
865 offset + len > i_size_read(inode))
866 new_size = offset + len;
868 /* Change file size if needed */
872 va.va_mask = XFS_AT_SIZE;
873 va.va_size = new_size;
874 error = xfs_setattr(ip, &va, ATTR_NOLOCK, NULL);
877 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
882 const struct inode_operations xfs_inode_operations = {
883 .permission = xfs_vn_permission,
884 .truncate = xfs_vn_truncate,
885 .getattr = xfs_vn_getattr,
886 .setattr = xfs_vn_setattr,
887 .setxattr = xfs_vn_setxattr,
888 .getxattr = xfs_vn_getxattr,
889 .listxattr = xfs_vn_listxattr,
890 .removexattr = xfs_vn_removexattr,
891 .fallocate = xfs_vn_fallocate,
894 const struct inode_operations xfs_dir_inode_operations = {
895 .create = xfs_vn_create,
896 .lookup = xfs_vn_lookup,
898 .unlink = xfs_vn_unlink,
899 .symlink = xfs_vn_symlink,
900 .mkdir = xfs_vn_mkdir,
901 .rmdir = xfs_vn_rmdir,
902 .mknod = xfs_vn_mknod,
903 .rename = xfs_vn_rename,
904 .permission = xfs_vn_permission,
905 .getattr = xfs_vn_getattr,
906 .setattr = xfs_vn_setattr,
907 .setxattr = xfs_vn_setxattr,
908 .getxattr = xfs_vn_getxattr,
909 .listxattr = xfs_vn_listxattr,
910 .removexattr = xfs_vn_removexattr,
913 const struct inode_operations xfs_symlink_inode_operations = {
914 .readlink = generic_readlink,
915 .follow_link = xfs_vn_follow_link,
916 .put_link = xfs_vn_put_link,
917 .permission = xfs_vn_permission,
918 .getattr = xfs_vn_getattr,
919 .setattr = xfs_vn_setattr,
920 .setxattr = xfs_vn_setxattr,
921 .getxattr = xfs_vn_getxattr,
922 .listxattr = xfs_vn_listxattr,
923 .removexattr = xfs_vn_removexattr,