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);
158 if (flags & XFS_ICHGTIME_MOD) {
159 tvp = &inode->i_mtime;
160 ip->i_d.di_mtime.t_sec = (__int32_t)tvp->tv_sec;
161 ip->i_d.di_mtime.t_nsec = (__int32_t)tvp->tv_nsec;
163 if (flags & XFS_ICHGTIME_CHG) {
164 tvp = &inode->i_ctime;
165 ip->i_d.di_ctime.t_sec = (__int32_t)tvp->tv_sec;
166 ip->i_d.di_ctime.t_nsec = (__int32_t)tvp->tv_nsec;
170 * We update the i_update_core field _after_ changing
171 * the timestamps in order to coordinate properly with
172 * xfs_iflush() so that we don't lose timestamp updates.
173 * This keeps us from having to hold the inode lock
174 * while doing this. We use the SYNCHRONIZE macro to
175 * ensure that the compiler does not reorder the update
176 * of i_update_core above the timestamp updates above.
179 ip->i_update_core = 1;
180 if (!(inode->i_state & I_NEW))
181 mark_inode_dirty_sync(inode);
186 * Pull the link count and size up from the xfs inode to the linux inode
192 struct xfs_inode *ip = XFS_I(inode);
195 /* we're under i_sem so i_size can't change under us */
196 size = XFS_ISIZE(ip);
197 if (i_size_read(inode) != size)
198 i_size_write(inode, size);
202 * Hook in SELinux. This is not quite correct yet, what we really need
203 * here (as we do for default ACLs) is a mechanism by which creation of
204 * these attrs can be journalled at inode creation time (along with the
205 * inode, of course, such that log replay can't cause these to be lost).
212 struct xfs_inode *ip = XFS_I(inode);
218 error = security_inode_init_security(inode, dir, &name,
221 if (error == -EOPNOTSUPP)
226 error = xfs_attr_set(ip, name, value, length, ATTR_SECURE);
228 xfs_iflags_set(ip, XFS_IMODIFIED);
237 struct xfs_name *namep,
238 struct dentry *dentry)
240 namep->name = dentry->d_name.name;
241 namep->len = dentry->d_name.len;
248 struct dentry *dentry,
251 struct xfs_name teardown;
254 * If we can't add the ACL or we fail in
255 * xfs_init_security we must back out.
256 * ENOSPC can hit here, among other things.
258 xfs_dentry_to_name(&teardown, dentry);
261 xfs_rmdir(XFS_I(dir), &teardown, XFS_I(inode));
263 xfs_remove(XFS_I(dir), &teardown, XFS_I(inode));
270 struct dentry *dentry,
275 struct xfs_inode *ip = NULL;
276 xfs_acl_t *default_acl = NULL;
277 struct xfs_name name;
278 attrexists_t test_default_acl = _ACL_DEFAULT_EXISTS;
282 * Irix uses Missed'em'V split, but doesn't want to see
283 * the upper 5 bits of (14bit) major.
285 if (unlikely(!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff))
288 if (test_default_acl && test_default_acl(dir)) {
289 if (!_ACL_ALLOC(default_acl)) {
292 if (!_ACL_GET_DEFAULT(dir, default_acl)) {
293 _ACL_FREE(default_acl);
298 xfs_dentry_to_name(&name, dentry);
300 if (IS_POSIXACL(dir) && !default_acl)
301 mode &= ~current->fs->umask;
303 switch (mode & S_IFMT) {
308 rdev = sysv_encode_dev(rdev);
310 error = xfs_create(XFS_I(dir), &name, mode, rdev, &ip, NULL);
313 error = xfs_mkdir(XFS_I(dir), &name, mode, &ip, NULL);
325 error = xfs_init_security(inode, dir);
327 goto out_cleanup_inode;
330 error = _ACL_INHERIT(inode, mode, default_acl);
332 goto out_cleanup_inode;
333 xfs_iflags_set(ip, XFS_IMODIFIED);
334 _ACL_FREE(default_acl);
339 xfs_validate_fields(inode);
340 d_instantiate(dentry, inode);
341 xfs_validate_fields(dir);
345 xfs_cleanup_inode(dir, inode, dentry, mode);
348 _ACL_FREE(default_acl);
355 struct dentry *dentry,
357 struct nameidata *nd)
359 return xfs_vn_mknod(dir, dentry, mode, 0);
365 struct dentry *dentry,
368 return xfs_vn_mknod(dir, dentry, mode|S_IFDIR, 0);
371 STATIC struct dentry *
374 struct dentry *dentry,
375 struct nameidata *nd)
377 struct xfs_inode *cip;
378 struct xfs_name name;
381 if (dentry->d_name.len >= MAXNAMELEN)
382 return ERR_PTR(-ENAMETOOLONG);
384 xfs_dentry_to_name(&name, dentry);
385 error = xfs_lookup(XFS_I(dir), &name, &cip);
386 if (unlikely(error)) {
387 if (unlikely(error != ENOENT))
388 return ERR_PTR(-error);
393 return d_splice_alias(cip->i_vnode, dentry);
398 struct dentry *old_dentry,
400 struct dentry *dentry)
402 struct inode *inode; /* inode of guy being linked to */
403 struct xfs_name name;
406 inode = old_dentry->d_inode;
407 xfs_dentry_to_name(&name, dentry);
410 error = xfs_link(XFS_I(dir), XFS_I(inode), &name);
411 if (unlikely(error)) {
416 xfs_iflags_set(XFS_I(dir), XFS_IMODIFIED);
417 xfs_validate_fields(inode);
418 d_instantiate(dentry, inode);
425 struct dentry *dentry)
428 struct xfs_name name;
431 inode = dentry->d_inode;
432 xfs_dentry_to_name(&name, dentry);
434 error = xfs_remove(XFS_I(dir), &name, XFS_I(inode));
435 if (likely(!error)) {
436 xfs_validate_fields(dir); /* size needs update */
437 xfs_validate_fields(inode);
445 struct dentry *dentry,
449 struct xfs_inode *cip = NULL;
450 struct xfs_name name;
455 (irix_symlink_mode ? 0777 & ~current->fs->umask : S_IRWXUGO);
456 xfs_dentry_to_name(&name, dentry);
458 error = xfs_symlink(XFS_I(dir), &name, symname, mode, &cip, NULL);
462 inode = cip->i_vnode;
464 error = xfs_init_security(inode, dir);
466 goto out_cleanup_inode;
468 d_instantiate(dentry, inode);
469 xfs_validate_fields(dir);
470 xfs_validate_fields(inode);
474 xfs_cleanup_inode(dir, inode, dentry, 0);
482 struct dentry *dentry)
484 struct inode *inode = dentry->d_inode;
485 struct xfs_name name;
488 xfs_dentry_to_name(&name, dentry);
490 error = xfs_rmdir(XFS_I(dir), &name, XFS_I(inode));
491 if (likely(!error)) {
492 xfs_validate_fields(inode);
493 xfs_validate_fields(dir);
501 struct dentry *odentry,
503 struct dentry *ndentry)
505 struct inode *new_inode = ndentry->d_inode;
506 struct xfs_name oname;
507 struct xfs_name nname;
510 xfs_dentry_to_name(&oname, odentry);
511 xfs_dentry_to_name(&nname, ndentry);
513 error = xfs_rename(XFS_I(odir), &oname, XFS_I(odentry->d_inode),
514 XFS_I(ndir), &nname);
515 if (likely(!error)) {
517 xfs_validate_fields(new_inode);
518 xfs_validate_fields(odir);
520 xfs_validate_fields(ndir);
526 * careful here - this function can get called recursively, so
527 * we need to be very careful about how much stack we use.
528 * uio is kmalloced for this reason...
532 struct dentry *dentry,
533 struct nameidata *nd)
538 link = kmalloc(MAXPATHLEN+1, GFP_KERNEL);
542 error = -xfs_readlink(XFS_I(dentry->d_inode), link);
546 nd_set_link(nd, link);
552 nd_set_link(nd, ERR_PTR(error));
558 struct dentry *dentry,
559 struct nameidata *nd,
562 char *s = nd_get_link(nd);
568 #ifdef CONFIG_XFS_POSIX_ACL
574 struct xfs_inode *ip = XFS_I(inode);
577 xfs_itrace_entry(ip);
579 if (XFS_IFORK_Q(ip)) {
580 error = xfs_acl_iaccess(ip, mask, NULL);
592 struct nameidata *nd)
594 return generic_permission(inode, mask, xfs_check_acl);
597 #define xfs_vn_permission NULL
602 struct vfsmount *mnt,
603 struct dentry *dentry,
606 struct inode *inode = dentry->d_inode;
607 struct xfs_inode *ip = XFS_I(inode);
608 struct xfs_mount *mp = ip->i_mount;
610 xfs_itrace_entry(ip);
612 if (XFS_FORCED_SHUTDOWN(mp))
613 return XFS_ERROR(EIO);
615 stat->size = XFS_ISIZE(ip);
616 stat->dev = inode->i_sb->s_dev;
617 stat->mode = ip->i_d.di_mode;
618 stat->nlink = ip->i_d.di_nlink;
619 stat->uid = ip->i_d.di_uid;
620 stat->gid = ip->i_d.di_gid;
621 stat->ino = ip->i_ino;
623 stat->ino += mp->m_inoadd;
625 stat->atime = inode->i_atime;
626 stat->mtime.tv_sec = ip->i_d.di_mtime.t_sec;
627 stat->mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
628 stat->ctime.tv_sec = ip->i_d.di_ctime.t_sec;
629 stat->ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
631 XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
634 switch (inode->i_mode & S_IFMT) {
637 stat->blksize = BLKDEV_IOSIZE;
638 stat->rdev = MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
639 sysv_minor(ip->i_df.if_u2.if_rdev));
642 if (XFS_IS_REALTIME_INODE(ip)) {
644 * If the file blocks are being allocated from a
645 * realtime volume, then return the inode's realtime
646 * extent size or the realtime volume's extent size.
649 xfs_get_extsz_hint(ip) << mp->m_sb.sb_blocklog;
651 stat->blksize = xfs_preferred_iosize(mp);
661 struct dentry *dentry,
664 struct inode *inode = dentry->d_inode;
665 unsigned int ia_valid = attr->ia_valid;
666 bhv_vattr_t vattr = { 0 };
670 if (ia_valid & ATTR_UID) {
671 vattr.va_mask |= XFS_AT_UID;
672 vattr.va_uid = attr->ia_uid;
674 if (ia_valid & ATTR_GID) {
675 vattr.va_mask |= XFS_AT_GID;
676 vattr.va_gid = attr->ia_gid;
678 if (ia_valid & ATTR_SIZE) {
679 vattr.va_mask |= XFS_AT_SIZE;
680 vattr.va_size = attr->ia_size;
682 if (ia_valid & ATTR_ATIME) {
683 vattr.va_mask |= XFS_AT_ATIME;
684 vattr.va_atime = attr->ia_atime;
685 inode->i_atime = attr->ia_atime;
687 if (ia_valid & ATTR_MTIME) {
688 vattr.va_mask |= XFS_AT_MTIME;
689 vattr.va_mtime = attr->ia_mtime;
691 if (ia_valid & ATTR_CTIME) {
692 vattr.va_mask |= XFS_AT_CTIME;
693 vattr.va_ctime = attr->ia_ctime;
695 if (ia_valid & ATTR_MODE) {
696 vattr.va_mask |= XFS_AT_MODE;
697 vattr.va_mode = attr->ia_mode;
698 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
699 inode->i_mode &= ~S_ISGID;
702 if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))
705 if ((ia_valid & ATTR_NO_BLOCK))
706 flags |= ATTR_NONBLOCK;
709 error = xfs_setattr(XFS_I(inode), &vattr, flags, NULL);
711 vn_revalidate(vn_from_inode(inode));
716 * block_truncate_page can return an error, but we can't propagate it
717 * at all here. Leave a complaint + stack trace in the syslog because
718 * this could be bad. If it is bad, we need to propagate the error further.
725 error = block_truncate_page(inode->i_mapping, inode->i_size,
732 struct dentry *dentry,
738 bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
739 char *attr = (char *)name;
744 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
747 attr += namesp->attr_namelen;
748 error = namesp->attr_capable(vp, NULL);
752 /* Convert Linux syscall to XFS internal ATTR flags */
753 if (flags & XATTR_CREATE)
754 xflags |= ATTR_CREATE;
755 if (flags & XATTR_REPLACE)
756 xflags |= ATTR_REPLACE;
757 xflags |= namesp->attr_flag;
758 return namesp->attr_set(vp, attr, (void *)data, size, xflags);
763 struct dentry *dentry,
768 bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
769 char *attr = (char *)name;
774 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
777 attr += namesp->attr_namelen;
778 error = namesp->attr_capable(vp, NULL);
782 /* Convert Linux syscall to XFS internal ATTR flags */
784 xflags |= ATTR_KERNOVAL;
787 xflags |= namesp->attr_flag;
788 return namesp->attr_get(vp, attr, (void *)data, size, xflags);
793 struct dentry *dentry,
797 bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
798 int error, xflags = ATTR_KERNAMELS;
802 xflags |= ATTR_KERNOVAL;
803 xflags |= capable(CAP_SYS_ADMIN) ? ATTR_KERNFULLS : ATTR_KERNORMALS;
805 error = attr_generic_list(vp, data, size, xflags, &result);
813 struct dentry *dentry,
816 bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
817 char *attr = (char *)name;
822 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
825 attr += namesp->attr_namelen;
826 error = namesp->attr_capable(vp, NULL);
829 xflags |= namesp->attr_flag;
830 return namesp->attr_remove(vp, attr, xflags);
843 xfs_inode_t *ip = XFS_I(inode);
845 /* preallocation on directories not yet supported */
847 if (S_ISDIR(inode->i_mode))
854 xfs_ilock(ip, XFS_IOLOCK_EXCL);
855 error = xfs_change_file_space(ip, XFS_IOC_RESVSP, &bf,
856 0, NULL, ATTR_NOLOCK);
857 if (!error && !(mode & FALLOC_FL_KEEP_SIZE) &&
858 offset + len > i_size_read(inode))
859 new_size = offset + len;
861 /* Change file size if needed */
865 va.va_mask = XFS_AT_SIZE;
866 va.va_size = new_size;
867 error = xfs_setattr(ip, &va, ATTR_NOLOCK, NULL);
870 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
875 const struct inode_operations xfs_inode_operations = {
876 .permission = xfs_vn_permission,
877 .truncate = xfs_vn_truncate,
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,
884 .fallocate = xfs_vn_fallocate,
887 const struct inode_operations xfs_dir_inode_operations = {
888 .create = xfs_vn_create,
889 .lookup = xfs_vn_lookup,
891 .unlink = xfs_vn_unlink,
892 .symlink = xfs_vn_symlink,
893 .mkdir = xfs_vn_mkdir,
894 .rmdir = xfs_vn_rmdir,
895 .mknod = xfs_vn_mknod,
896 .rename = xfs_vn_rename,
897 .permission = xfs_vn_permission,
898 .getattr = xfs_vn_getattr,
899 .setattr = xfs_vn_setattr,
900 .setxattr = xfs_vn_setxattr,
901 .getxattr = xfs_vn_getxattr,
902 .listxattr = xfs_vn_listxattr,
903 .removexattr = xfs_vn_removexattr,
906 const struct inode_operations xfs_symlink_inode_operations = {
907 .readlink = generic_readlink,
908 .follow_link = xfs_vn_follow_link,
909 .put_link = xfs_vn_put_link,
910 .permission = xfs_vn_permission,
911 .getattr = xfs_vn_getattr,
912 .setattr = xfs_vn_setattr,
913 .setxattr = xfs_vn_setxattr,
914 .getxattr = xfs_vn_getxattr,
915 .listxattr = xfs_vn_listxattr,
916 .removexattr = xfs_vn_removexattr,