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(
67 vp = XFS_ITOV_NULL(ip);
69 ip->i_d.di_atime.t_sec = (__int32_t)vp->i_atime.tv_sec;
70 ip->i_d.di_atime.t_nsec = (__int32_t)vp->i_atime.tv_nsec;
75 * If the linux inode exists, mark it dirty.
76 * Used when commiting a dirty inode into a transaction so that
77 * the inode will get written back by the linux code
80 xfs_mark_inode_dirty_sync(
85 vp = XFS_ITOV_NULL(ip);
87 mark_inode_dirty_sync(vn_to_inode(vp));
91 * Change the requested timestamp in the given inode.
92 * We don't lock across timestamp updates, and we don't log them but
93 * we do record the fact that there is dirty information in core.
95 * NOTE -- callers MUST combine XFS_ICHGTIME_MOD or XFS_ICHGTIME_CHG
96 * with XFS_ICHGTIME_ACC to be sure that access time
97 * update will take. Calling first with XFS_ICHGTIME_ACC
98 * and then XFS_ICHGTIME_MOD may fail to modify the access
99 * timestamp if the filesystem is mounted noacctm.
106 struct inode *inode = vn_to_inode(XFS_ITOV(ip));
110 if (flags & XFS_ICHGTIME_MOD) {
112 ip->i_d.di_mtime.t_sec = (__int32_t)tv.tv_sec;
113 ip->i_d.di_mtime.t_nsec = (__int32_t)tv.tv_nsec;
115 if (flags & XFS_ICHGTIME_ACC) {
117 ip->i_d.di_atime.t_sec = (__int32_t)tv.tv_sec;
118 ip->i_d.di_atime.t_nsec = (__int32_t)tv.tv_nsec;
120 if (flags & XFS_ICHGTIME_CHG) {
122 ip->i_d.di_ctime.t_sec = (__int32_t)tv.tv_sec;
123 ip->i_d.di_ctime.t_nsec = (__int32_t)tv.tv_nsec;
127 * We update the i_update_core field _after_ changing
128 * the timestamps in order to coordinate properly with
129 * xfs_iflush() so that we don't lose timestamp updates.
130 * This keeps us from having to hold the inode lock
131 * while doing this. We use the SYNCHRONIZE macro to
132 * ensure that the compiler does not reorder the update
133 * of i_update_core above the timestamp updates above.
136 ip->i_update_core = 1;
137 if (!(inode->i_state & I_NEW))
138 mark_inode_dirty_sync(inode);
142 * Variant on the above which avoids querying the system clock
143 * in situations where we know the Linux inode timestamps have
144 * just been updated (and so we can update our inode cheaply).
155 * Atime updates for read() & friends are handled lazily now, and
156 * explicit updates must go through xfs_ichgtime()
158 ASSERT((flags & XFS_ICHGTIME_ACC) == 0);
161 * We're not supposed to change timestamps in readonly-mounted
162 * filesystems. Throw it away if anyone asks us.
164 if (unlikely(IS_RDONLY(inode)))
167 if (flags & XFS_ICHGTIME_MOD) {
168 tvp = &inode->i_mtime;
169 ip->i_d.di_mtime.t_sec = (__int32_t)tvp->tv_sec;
170 ip->i_d.di_mtime.t_nsec = (__int32_t)tvp->tv_nsec;
172 if (flags & XFS_ICHGTIME_CHG) {
173 tvp = &inode->i_ctime;
174 ip->i_d.di_ctime.t_sec = (__int32_t)tvp->tv_sec;
175 ip->i_d.di_ctime.t_nsec = (__int32_t)tvp->tv_nsec;
179 * We update the i_update_core field _after_ changing
180 * the timestamps in order to coordinate properly with
181 * xfs_iflush() so that we don't lose timestamp updates.
182 * This keeps us from having to hold the inode lock
183 * while doing this. We use the SYNCHRONIZE macro to
184 * ensure that the compiler does not reorder the update
185 * of i_update_core above the timestamp updates above.
188 ip->i_update_core = 1;
189 if (!(inode->i_state & I_NEW))
190 mark_inode_dirty_sync(inode);
195 * Pull the link count and size up from the xfs inode to the linux inode
201 struct xfs_inode *ip = XFS_I(inode);
204 inode->i_nlink = ip->i_d.di_nlink;
206 XFS_FSB_TO_BB(ip->i_mount, ip->i_d.di_nblocks +
208 /* we're under i_sem so i_size can't change under us */
209 size = XFS_ISIZE(ip);
210 if (i_size_read(inode) != size)
211 i_size_write(inode, size);
215 * Hook in SELinux. This is not quite correct yet, what we really need
216 * here (as we do for default ACLs) is a mechanism by which creation of
217 * these attrs can be journalled at inode creation time (along with the
218 * inode, of course, such that log replay can't cause these to be lost).
225 struct inode *ip = vn_to_inode(vp);
231 error = security_inode_init_security(ip, dir, &name, &value, &length);
233 if (error == -EOPNOTSUPP)
238 error = xfs_attr_set(XFS_I(ip), name, value,
239 length, ATTR_SECURE);
241 xfs_iflags_set(XFS_I(ip), XFS_IMODIFIED);
249 * Determine whether a process has a valid fs_struct (kernel daemons
250 * like knfsd don't have an fs_struct).
252 * XXX(hch): nfsd is broken, better fix it instead.
255 xfs_has_fs_struct(struct task_struct *task)
257 return (task->fs != init_task.fs);
264 struct dentry *dentry,
267 struct dentry teardown = {};
270 * If we can't add the ACL or we fail in
271 * xfs_init_security we must back out.
272 * ENOSPC can hit here, among other things.
274 teardown.d_inode = vn_to_inode(vp);
275 teardown.d_name = dentry->d_name;
278 xfs_rmdir(XFS_I(dir), &teardown);
280 xfs_remove(XFS_I(dir), &teardown);
287 struct dentry *dentry,
292 bhv_vnode_t *vp = NULL, *dvp = vn_from_inode(dir);
293 xfs_acl_t *default_acl = NULL;
294 attrexists_t test_default_acl = _ACL_DEFAULT_EXISTS;
298 * Irix uses Missed'em'V split, but doesn't want to see
299 * the upper 5 bits of (14bit) major.
301 if (unlikely(!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff))
304 if (unlikely(test_default_acl && test_default_acl(dvp))) {
305 if (!_ACL_ALLOC(default_acl)) {
308 if (!_ACL_GET_DEFAULT(dvp, default_acl)) {
309 _ACL_FREE(default_acl);
314 if (IS_POSIXACL(dir) && !default_acl && xfs_has_fs_struct(current))
315 mode &= ~current->fs->umask;
317 switch (mode & S_IFMT) {
318 case S_IFCHR: case S_IFBLK: case S_IFIFO: case S_IFSOCK:
319 rdev = sysv_encode_dev(rdev);
321 error = xfs_create(XFS_I(dir), dentry, mode, rdev, &vp, NULL);
324 error = xfs_mkdir(XFS_I(dir), dentry, mode, &vp, NULL);
331 if (unlikely(!error)) {
332 error = xfs_init_security(vp, dir);
334 xfs_cleanup_inode(dir, vp, dentry, mode);
337 if (unlikely(default_acl)) {
339 error = _ACL_INHERIT(vp, mode, default_acl);
341 xfs_iflags_set(XFS_I(vp), XFS_IMODIFIED);
343 xfs_cleanup_inode(dir, vp, dentry, mode);
345 _ACL_FREE(default_acl);
348 if (likely(!error)) {
350 ip = vn_to_inode(vp);
353 xfs_validate_fields(ip);
354 d_instantiate(dentry, ip);
355 xfs_validate_fields(dir);
363 struct dentry *dentry,
365 struct nameidata *nd)
367 return xfs_vn_mknod(dir, dentry, mode, 0);
373 struct dentry *dentry,
376 return xfs_vn_mknod(dir, dentry, mode|S_IFDIR, 0);
379 STATIC struct dentry *
382 struct dentry *dentry,
383 struct nameidata *nd)
388 if (dentry->d_name.len >= MAXNAMELEN)
389 return ERR_PTR(-ENAMETOOLONG);
391 error = xfs_lookup(XFS_I(dir), dentry, &cvp);
392 if (unlikely(error)) {
393 if (unlikely(error != ENOENT))
394 return ERR_PTR(-error);
399 return d_splice_alias(vn_to_inode(cvp), dentry);
404 struct dentry *old_dentry,
406 struct dentry *dentry)
408 struct inode *ip; /* inode of guy being linked to */
409 bhv_vnode_t *vp; /* vp of name being linked */
412 ip = old_dentry->d_inode; /* inode being linked to */
413 vp = vn_from_inode(ip);
416 error = xfs_link(XFS_I(dir), vp, dentry);
417 if (unlikely(error)) {
420 xfs_iflags_set(XFS_I(dir), XFS_IMODIFIED);
421 xfs_validate_fields(ip);
422 d_instantiate(dentry, ip);
430 struct dentry *dentry)
435 inode = dentry->d_inode;
437 error = xfs_remove(XFS_I(dir), dentry);
438 if (likely(!error)) {
439 xfs_validate_fields(dir); /* size needs update */
440 xfs_validate_fields(inode);
448 struct dentry *dentry,
452 bhv_vnode_t *cvp; /* used to lookup symlink to put in dentry */
459 (irix_symlink_mode ? 0777 & ~current->fs->umask : S_IRWXUGO);
461 error = xfs_symlink(XFS_I(dir), dentry, (char *)symname, mode,
463 if (likely(!error && cvp)) {
464 error = xfs_init_security(cvp, dir);
465 if (likely(!error)) {
466 ip = vn_to_inode(cvp);
467 d_instantiate(dentry, ip);
468 xfs_validate_fields(dir);
469 xfs_validate_fields(ip);
471 xfs_cleanup_inode(dir, cvp, dentry, 0);
480 struct dentry *dentry)
482 struct inode *inode = dentry->d_inode;
485 error = xfs_rmdir(XFS_I(dir), dentry);
486 if (likely(!error)) {
487 xfs_validate_fields(inode);
488 xfs_validate_fields(dir);
496 struct dentry *odentry,
498 struct dentry *ndentry)
500 struct inode *new_inode = ndentry->d_inode;
501 bhv_vnode_t *tvp; /* target directory */
504 tvp = vn_from_inode(ndir);
506 error = xfs_rename(XFS_I(odir), odentry, tvp, ndentry);
507 if (likely(!error)) {
509 xfs_validate_fields(new_inode);
510 xfs_validate_fields(odir);
512 xfs_validate_fields(ndir);
518 * careful here - this function can get called recursively, so
519 * we need to be very careful about how much stack we use.
520 * uio is kmalloced for this reason...
524 struct dentry *dentry,
525 struct nameidata *nd)
530 link = kmalloc(MAXPATHLEN+1, GFP_KERNEL);
534 error = -xfs_readlink(XFS_I(dentry->d_inode), link);
538 nd_set_link(nd, link);
544 nd_set_link(nd, ERR_PTR(error));
550 struct dentry *dentry,
551 struct nameidata *nd,
554 char *s = nd_get_link(nd);
560 #ifdef CONFIG_XFS_POSIX_ACL
566 struct xfs_inode *ip = XFS_I(inode);
569 xfs_itrace_entry(ip);
571 if (XFS_IFORK_Q(ip)) {
572 error = xfs_acl_iaccess(ip, mask, NULL);
584 struct nameidata *nd)
586 return generic_permission(inode, mask, xfs_check_acl);
589 #define xfs_vn_permission NULL
594 struct vfsmount *mnt,
595 struct dentry *dentry,
598 struct inode *inode = dentry->d_inode;
599 struct xfs_inode *ip = XFS_I(inode);
600 struct xfs_mount *mp = ip->i_mount;
602 xfs_itrace_entry(ip);
604 if (XFS_FORCED_SHUTDOWN(mp))
605 return XFS_ERROR(EIO);
607 stat->size = XFS_ISIZE(ip);
608 stat->dev = inode->i_sb->s_dev;
609 stat->mode = ip->i_d.di_mode;
610 stat->nlink = ip->i_d.di_nlink;
611 stat->uid = ip->i_d.di_uid;
612 stat->gid = ip->i_d.di_gid;
613 stat->ino = ip->i_ino;
615 stat->ino += mp->m_inoadd;
617 stat->atime = inode->i_atime;
618 stat->mtime.tv_sec = ip->i_d.di_mtime.t_sec;
619 stat->mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
620 stat->ctime.tv_sec = ip->i_d.di_ctime.t_sec;
621 stat->ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
623 XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
626 switch (inode->i_mode & S_IFMT) {
629 stat->blksize = BLKDEV_IOSIZE;
630 stat->rdev = MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
631 sysv_minor(ip->i_df.if_u2.if_rdev));
634 if (XFS_IS_REALTIME_INODE(ip)) {
636 * If the file blocks are being allocated from a
637 * realtime volume, then return the inode's realtime
638 * extent size or the realtime volume's extent size.
641 xfs_get_extsz_hint(ip) << mp->m_sb.sb_blocklog;
643 stat->blksize = xfs_preferred_iosize(mp);
653 struct dentry *dentry,
656 struct inode *inode = dentry->d_inode;
657 unsigned int ia_valid = attr->ia_valid;
658 bhv_vattr_t vattr = { 0 };
662 if (ia_valid & ATTR_UID) {
663 vattr.va_mask |= XFS_AT_UID;
664 vattr.va_uid = attr->ia_uid;
666 if (ia_valid & ATTR_GID) {
667 vattr.va_mask |= XFS_AT_GID;
668 vattr.va_gid = attr->ia_gid;
670 if (ia_valid & ATTR_SIZE) {
671 vattr.va_mask |= XFS_AT_SIZE;
672 vattr.va_size = attr->ia_size;
674 if (ia_valid & ATTR_ATIME) {
675 vattr.va_mask |= XFS_AT_ATIME;
676 vattr.va_atime = attr->ia_atime;
677 inode->i_atime = attr->ia_atime;
679 if (ia_valid & ATTR_MTIME) {
680 vattr.va_mask |= XFS_AT_MTIME;
681 vattr.va_mtime = attr->ia_mtime;
683 if (ia_valid & ATTR_CTIME) {
684 vattr.va_mask |= XFS_AT_CTIME;
685 vattr.va_ctime = attr->ia_ctime;
687 if (ia_valid & ATTR_MODE) {
688 vattr.va_mask |= XFS_AT_MODE;
689 vattr.va_mode = attr->ia_mode;
690 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
691 inode->i_mode &= ~S_ISGID;
694 if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))
697 if ((ia_valid & ATTR_NO_BLOCK))
698 flags |= ATTR_NONBLOCK;
701 error = xfs_setattr(XFS_I(inode), &vattr, flags, NULL);
703 vn_revalidate(vn_from_inode(inode));
711 block_truncate_page(inode->i_mapping, inode->i_size, xfs_get_blocks);
716 struct dentry *dentry,
722 bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
723 char *attr = (char *)name;
728 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
731 attr += namesp->attr_namelen;
732 error = namesp->attr_capable(vp, NULL);
736 /* Convert Linux syscall to XFS internal ATTR flags */
737 if (flags & XATTR_CREATE)
738 xflags |= ATTR_CREATE;
739 if (flags & XATTR_REPLACE)
740 xflags |= ATTR_REPLACE;
741 xflags |= namesp->attr_flag;
742 return namesp->attr_set(vp, attr, (void *)data, size, xflags);
747 struct dentry *dentry,
752 bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
753 char *attr = (char *)name;
758 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
761 attr += namesp->attr_namelen;
762 error = namesp->attr_capable(vp, NULL);
766 /* Convert Linux syscall to XFS internal ATTR flags */
768 xflags |= ATTR_KERNOVAL;
771 xflags |= namesp->attr_flag;
772 return namesp->attr_get(vp, attr, (void *)data, size, xflags);
777 struct dentry *dentry,
781 bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
782 int error, xflags = ATTR_KERNAMELS;
786 xflags |= ATTR_KERNOVAL;
787 xflags |= capable(CAP_SYS_ADMIN) ? ATTR_KERNFULLS : ATTR_KERNORMALS;
789 error = attr_generic_list(vp, data, size, xflags, &result);
797 struct dentry *dentry,
800 bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
801 char *attr = (char *)name;
806 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
809 attr += namesp->attr_namelen;
810 error = namesp->attr_capable(vp, NULL);
813 xflags |= namesp->attr_flag;
814 return namesp->attr_remove(vp, attr, xflags);
827 xfs_inode_t *ip = XFS_I(inode);
829 /* preallocation on directories not yet supported */
831 if (S_ISDIR(inode->i_mode))
838 xfs_ilock(ip, XFS_IOLOCK_EXCL);
839 error = xfs_change_file_space(ip, XFS_IOC_RESVSP, &bf,
840 0, NULL, ATTR_NOLOCK);
841 if (!error && !(mode & FALLOC_FL_KEEP_SIZE) &&
842 offset + len > i_size_read(inode))
843 new_size = offset + len;
845 /* Change file size if needed */
849 va.va_mask = XFS_AT_SIZE;
850 va.va_size = new_size;
851 error = xfs_setattr(ip, &va, ATTR_NOLOCK, NULL);
854 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
859 const struct inode_operations xfs_inode_operations = {
860 .permission = xfs_vn_permission,
861 .truncate = xfs_vn_truncate,
862 .getattr = xfs_vn_getattr,
863 .setattr = xfs_vn_setattr,
864 .setxattr = xfs_vn_setxattr,
865 .getxattr = xfs_vn_getxattr,
866 .listxattr = xfs_vn_listxattr,
867 .removexattr = xfs_vn_removexattr,
868 .fallocate = xfs_vn_fallocate,
871 const struct inode_operations xfs_dir_inode_operations = {
872 .create = xfs_vn_create,
873 .lookup = xfs_vn_lookup,
875 .unlink = xfs_vn_unlink,
876 .symlink = xfs_vn_symlink,
877 .mkdir = xfs_vn_mkdir,
878 .rmdir = xfs_vn_rmdir,
879 .mknod = xfs_vn_mknod,
880 .rename = xfs_vn_rename,
881 .permission = xfs_vn_permission,
882 .getattr = xfs_vn_getattr,
883 .setattr = xfs_vn_setattr,
884 .setxattr = xfs_vn_setxattr,
885 .getxattr = xfs_vn_getxattr,
886 .listxattr = xfs_vn_listxattr,
887 .removexattr = xfs_vn_removexattr,
890 const struct inode_operations xfs_symlink_inode_operations = {
891 .readlink = generic_readlink,
892 .follow_link = xfs_vn_follow_link,
893 .put_link = xfs_vn_put_link,
894 .permission = xfs_vn_permission,
895 .getattr = xfs_vn_getattr,
896 .setattr = xfs_vn_setattr,
897 .setxattr = xfs_vn_setxattr,
898 .getxattr = xfs_vn_getxattr,
899 .listxattr = xfs_vn_listxattr,
900 .removexattr = xfs_vn_removexattr,