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>
56 #include <linux/fiemap.h>
59 * Bring the atime in the XFS inode uptodate.
60 * Used before logging the inode to disk or when the Linux inode goes away.
63 xfs_synchronize_atime(
66 struct inode *inode = VFS_I(ip);
68 if (!(inode->i_state & I_CLEAR)) {
69 ip->i_d.di_atime.t_sec = (__int32_t)inode->i_atime.tv_sec;
70 ip->i_d.di_atime.t_nsec = (__int32_t)inode->i_atime.tv_nsec;
75 * If the linux inode is valid, 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(
83 struct inode *inode = VFS_I(ip);
85 if (!(inode->i_state & (I_WILL_FREE|I_FREEING|I_CLEAR)))
86 mark_inode_dirty_sync(inode);
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.
99 struct inode *inode = VFS_I(ip);
103 tv = current_fs_time(inode->i_sb);
105 if ((flags & XFS_ICHGTIME_MOD) &&
106 !timespec_equal(&inode->i_mtime, &tv)) {
108 ip->i_d.di_mtime.t_sec = (__int32_t)tv.tv_sec;
109 ip->i_d.di_mtime.t_nsec = (__int32_t)tv.tv_nsec;
112 if ((flags & XFS_ICHGTIME_CHG) &&
113 !timespec_equal(&inode->i_ctime, &tv)) {
115 ip->i_d.di_ctime.t_sec = (__int32_t)tv.tv_sec;
116 ip->i_d.di_ctime.t_nsec = (__int32_t)tv.tv_nsec;
121 * We update the i_update_core field _after_ changing
122 * the timestamps in order to coordinate properly with
123 * xfs_iflush() so that we don't lose timestamp updates.
124 * This keeps us from having to hold the inode lock
125 * while doing this. We use the SYNCHRONIZE macro to
126 * ensure that the compiler does not reorder the update
127 * of i_update_core above the timestamp updates above.
131 ip->i_update_core = 1;
132 xfs_mark_inode_dirty_sync(ip);
137 * Hook in SELinux. This is not quite correct yet, what we really need
138 * here (as we do for default ACLs) is a mechanism by which creation of
139 * these attrs can be journalled at inode creation time (along with the
140 * inode, of course, such that log replay can't cause these to be lost).
147 struct xfs_inode *ip = XFS_I(inode);
153 error = security_inode_init_security(inode, dir, &name,
156 if (error == -EOPNOTSUPP)
161 error = xfs_attr_set(ip, name, value, length, ATTR_SECURE);
163 xfs_iflags_set(ip, XFS_IMODIFIED);
172 struct xfs_name *namep,
173 struct dentry *dentry)
175 namep->name = dentry->d_name.name;
176 namep->len = dentry->d_name.len;
183 struct dentry *dentry)
185 struct xfs_name teardown;
188 * If we can't add the ACL or we fail in
189 * xfs_init_security we must back out.
190 * ENOSPC can hit here, among other things.
192 xfs_dentry_to_name(&teardown, dentry);
194 xfs_remove(XFS_I(dir), &teardown, XFS_I(inode));
201 struct dentry *dentry,
206 struct xfs_inode *ip = NULL;
207 xfs_acl_t *default_acl = NULL;
208 struct xfs_name name;
209 int (*test_default_acl)(struct inode *) = _ACL_DEFAULT_EXISTS;
213 * Irix uses Missed'em'V split, but doesn't want to see
214 * the upper 5 bits of (14bit) major.
216 if (unlikely(!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff))
219 if (test_default_acl && test_default_acl(dir)) {
220 if (!_ACL_ALLOC(default_acl)) {
223 if (!_ACL_GET_DEFAULT(dir, default_acl)) {
224 _ACL_FREE(default_acl);
229 xfs_dentry_to_name(&name, dentry);
231 if (IS_POSIXACL(dir) && !default_acl)
232 mode &= ~current->fs->umask;
234 switch (mode & S_IFMT) {
239 rdev = sysv_encode_dev(rdev);
241 error = xfs_create(XFS_I(dir), &name, mode, rdev, &ip, NULL);
244 error = xfs_mkdir(XFS_I(dir), &name, mode, &ip, NULL);
256 error = xfs_init_security(inode, dir);
258 goto out_cleanup_inode;
261 error = _ACL_INHERIT(inode, mode, default_acl);
263 goto out_cleanup_inode;
264 xfs_iflags_set(ip, XFS_IMODIFIED);
265 _ACL_FREE(default_acl);
269 d_instantiate(dentry, inode);
273 xfs_cleanup_inode(dir, inode, dentry);
276 _ACL_FREE(default_acl);
283 struct dentry *dentry,
285 struct nameidata *nd)
287 return xfs_vn_mknod(dir, dentry, mode, 0);
293 struct dentry *dentry,
296 return xfs_vn_mknod(dir, dentry, mode|S_IFDIR, 0);
299 STATIC struct dentry *
302 struct dentry *dentry,
303 struct nameidata *nd)
305 struct xfs_inode *cip;
306 struct xfs_name name;
309 if (dentry->d_name.len >= MAXNAMELEN)
310 return ERR_PTR(-ENAMETOOLONG);
312 xfs_dentry_to_name(&name, dentry);
313 error = xfs_lookup(XFS_I(dir), &name, &cip, NULL);
314 if (unlikely(error)) {
315 if (unlikely(error != ENOENT))
316 return ERR_PTR(-error);
321 return d_splice_alias(VFS_I(cip), dentry);
324 STATIC struct dentry *
327 struct dentry *dentry,
328 struct nameidata *nd)
330 struct xfs_inode *ip;
331 struct xfs_name xname;
332 struct xfs_name ci_name;
336 if (dentry->d_name.len >= MAXNAMELEN)
337 return ERR_PTR(-ENAMETOOLONG);
339 xfs_dentry_to_name(&xname, dentry);
340 error = xfs_lookup(XFS_I(dir), &xname, &ip, &ci_name);
341 if (unlikely(error)) {
342 if (unlikely(error != ENOENT))
343 return ERR_PTR(-error);
345 * call d_add(dentry, NULL) here when d_drop_negative_children
346 * is called in xfs_vn_mknod (ie. allow negative dentries
347 * with CI filesystems).
352 /* if exact match, just splice and exit */
354 return d_splice_alias(VFS_I(ip), dentry);
356 /* else case-insensitive match... */
357 dname.name = ci_name.name;
358 dname.len = ci_name.len;
359 dentry = d_add_ci(dentry, VFS_I(ip), &dname);
360 kmem_free(ci_name.name);
366 struct dentry *old_dentry,
368 struct dentry *dentry)
370 struct inode *inode = old_dentry->d_inode;
371 struct xfs_name name;
374 xfs_dentry_to_name(&name, dentry);
376 error = xfs_link(XFS_I(dir), XFS_I(inode), &name);
380 xfs_iflags_set(XFS_I(dir), XFS_IMODIFIED);
381 atomic_inc(&inode->i_count);
382 d_instantiate(dentry, inode);
389 struct dentry *dentry)
391 struct xfs_name name;
394 xfs_dentry_to_name(&name, dentry);
396 error = -xfs_remove(XFS_I(dir), &name, XFS_I(dentry->d_inode));
401 * With unlink, the VFS makes the dentry "negative": no inode,
402 * but still hashed. This is incompatible with case-insensitive
403 * mode, so invalidate (unhash) the dentry in CI-mode.
405 if (xfs_sb_version_hasasciici(&XFS_M(dir->i_sb)->m_sb))
406 d_invalidate(dentry);
413 struct dentry *dentry,
417 struct xfs_inode *cip = NULL;
418 struct xfs_name name;
423 (irix_symlink_mode ? 0777 & ~current->fs->umask : S_IRWXUGO);
424 xfs_dentry_to_name(&name, dentry);
426 error = xfs_symlink(XFS_I(dir), &name, symname, mode, &cip, NULL);
432 error = xfs_init_security(inode, dir);
434 goto out_cleanup_inode;
436 d_instantiate(dentry, inode);
440 xfs_cleanup_inode(dir, inode, dentry);
448 struct dentry *odentry,
450 struct dentry *ndentry)
452 struct inode *new_inode = ndentry->d_inode;
453 struct xfs_name oname;
454 struct xfs_name nname;
456 xfs_dentry_to_name(&oname, odentry);
457 xfs_dentry_to_name(&nname, ndentry);
459 return -xfs_rename(XFS_I(odir), &oname, XFS_I(odentry->d_inode),
460 XFS_I(ndir), &nname, new_inode ?
461 XFS_I(new_inode) : NULL);
465 * careful here - this function can get called recursively, so
466 * we need to be very careful about how much stack we use.
467 * uio is kmalloced for this reason...
471 struct dentry *dentry,
472 struct nameidata *nd)
477 link = kmalloc(MAXPATHLEN+1, GFP_KERNEL);
481 error = -xfs_readlink(XFS_I(dentry->d_inode), link);
485 nd_set_link(nd, link);
491 nd_set_link(nd, ERR_PTR(error));
497 struct dentry *dentry,
498 struct nameidata *nd,
501 char *s = nd_get_link(nd);
507 #ifdef CONFIG_XFS_POSIX_ACL
513 struct xfs_inode *ip = XFS_I(inode);
516 xfs_itrace_entry(ip);
518 if (XFS_IFORK_Q(ip)) {
519 error = xfs_acl_iaccess(ip, mask, NULL);
532 return generic_permission(inode, mask, xfs_check_acl);
535 #define xfs_vn_permission NULL
540 struct vfsmount *mnt,
541 struct dentry *dentry,
544 struct inode *inode = dentry->d_inode;
545 struct xfs_inode *ip = XFS_I(inode);
546 struct xfs_mount *mp = ip->i_mount;
548 xfs_itrace_entry(ip);
550 if (XFS_FORCED_SHUTDOWN(mp))
551 return XFS_ERROR(EIO);
553 stat->size = XFS_ISIZE(ip);
554 stat->dev = inode->i_sb->s_dev;
555 stat->mode = ip->i_d.di_mode;
556 stat->nlink = ip->i_d.di_nlink;
557 stat->uid = ip->i_d.di_uid;
558 stat->gid = ip->i_d.di_gid;
559 stat->ino = ip->i_ino;
561 stat->ino += mp->m_inoadd;
563 stat->atime = inode->i_atime;
564 stat->mtime.tv_sec = ip->i_d.di_mtime.t_sec;
565 stat->mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
566 stat->ctime.tv_sec = ip->i_d.di_ctime.t_sec;
567 stat->ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
569 XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
572 switch (inode->i_mode & S_IFMT) {
575 stat->blksize = BLKDEV_IOSIZE;
576 stat->rdev = MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
577 sysv_minor(ip->i_df.if_u2.if_rdev));
580 if (XFS_IS_REALTIME_INODE(ip)) {
582 * If the file blocks are being allocated from a
583 * realtime volume, then return the inode's realtime
584 * extent size or the realtime volume's extent size.
587 xfs_get_extsz_hint(ip) << mp->m_sb.sb_blocklog;
589 stat->blksize = xfs_preferred_iosize(mp);
599 struct dentry *dentry,
602 return -xfs_setattr(XFS_I(dentry->d_inode), iattr, 0);
606 * block_truncate_page can return an error, but we can't propagate it
607 * at all here. Leave a complaint + stack trace in the syslog because
608 * this could be bad. If it is bad, we need to propagate the error further.
615 error = block_truncate_page(inode->i_mapping, inode->i_size,
630 xfs_inode_t *ip = XFS_I(inode);
632 /* preallocation on directories not yet supported */
634 if (S_ISDIR(inode->i_mode))
641 xfs_ilock(ip, XFS_IOLOCK_EXCL);
642 error = xfs_change_file_space(ip, XFS_IOC_RESVSP, &bf,
644 if (!error && !(mode & FALLOC_FL_KEEP_SIZE) &&
645 offset + len > i_size_read(inode))
646 new_size = offset + len;
648 /* Change file size if needed */
652 iattr.ia_valid = ATTR_SIZE;
653 iattr.ia_size = new_size;
654 error = xfs_setattr(ip, &iattr, XFS_ATTR_NOLOCK);
657 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
662 #define XFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR)
665 * Call fiemap helper to fill in user data.
666 * Returns positive errors to xfs_getbmap.
671 struct getbmapx *bmv,
675 struct fiemap_extent_info *fieinfo = *arg;
676 u32 fiemap_flags = 0;
677 u64 logical, physical, length;
679 /* Do nothing for a hole */
680 if (bmv->bmv_block == -1LL)
683 logical = BBTOB(bmv->bmv_offset);
684 physical = BBTOB(bmv->bmv_block);
685 length = BBTOB(bmv->bmv_length);
687 if (bmv->bmv_oflags & BMV_OF_PREALLOC)
688 fiemap_flags |= FIEMAP_EXTENT_UNWRITTEN;
689 else if (bmv->bmv_oflags & BMV_OF_DELALLOC) {
690 fiemap_flags |= FIEMAP_EXTENT_DELALLOC;
691 physical = 0; /* no block yet */
693 if (bmv->bmv_oflags & BMV_OF_LAST)
694 fiemap_flags |= FIEMAP_EXTENT_LAST;
696 error = fiemap_fill_next_extent(fieinfo, logical, physical,
697 length, fiemap_flags);
700 *full = 1; /* user array now full */
709 struct fiemap_extent_info *fieinfo,
713 xfs_inode_t *ip = XFS_I(inode);
717 error = fiemap_check_flags(fieinfo, XFS_FIEMAP_FLAGS);
721 /* Set up bmap header for xfs internal routine */
722 bm.bmv_offset = BTOBB(start);
723 /* Special case for whole file */
724 if (length == FIEMAP_MAX_OFFSET)
725 bm.bmv_length = -1LL;
727 bm.bmv_length = BTOBB(length);
729 /* our formatter will tell xfs_getbmap when to stop. */
730 bm.bmv_count = MAXEXTNUM;
731 bm.bmv_iflags = BMV_IF_PREALLOC;
732 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR)
733 bm.bmv_iflags |= BMV_IF_ATTRFORK;
734 if (!(fieinfo->fi_flags & FIEMAP_FLAG_SYNC))
735 bm.bmv_iflags |= BMV_IF_DELALLOC;
737 error = xfs_getbmap(ip, &bm, xfs_fiemap_format, fieinfo);
744 static const struct inode_operations xfs_inode_operations = {
745 .permission = xfs_vn_permission,
746 .truncate = xfs_vn_truncate,
747 .getattr = xfs_vn_getattr,
748 .setattr = xfs_vn_setattr,
749 .setxattr = generic_setxattr,
750 .getxattr = generic_getxattr,
751 .removexattr = generic_removexattr,
752 .listxattr = xfs_vn_listxattr,
753 .fallocate = xfs_vn_fallocate,
754 .fiemap = xfs_vn_fiemap,
757 static const struct inode_operations xfs_dir_inode_operations = {
758 .create = xfs_vn_create,
759 .lookup = xfs_vn_lookup,
761 .unlink = xfs_vn_unlink,
762 .symlink = xfs_vn_symlink,
763 .mkdir = xfs_vn_mkdir,
765 * Yes, XFS uses the same method for rmdir and unlink.
767 * There are some subtile differences deeper in the code,
768 * but we use S_ISDIR to check for those.
770 .rmdir = xfs_vn_unlink,
771 .mknod = xfs_vn_mknod,
772 .rename = xfs_vn_rename,
773 .permission = xfs_vn_permission,
774 .getattr = xfs_vn_getattr,
775 .setattr = xfs_vn_setattr,
776 .setxattr = generic_setxattr,
777 .getxattr = generic_getxattr,
778 .removexattr = generic_removexattr,
779 .listxattr = xfs_vn_listxattr,
782 static const struct inode_operations xfs_dir_ci_inode_operations = {
783 .create = xfs_vn_create,
784 .lookup = xfs_vn_ci_lookup,
786 .unlink = xfs_vn_unlink,
787 .symlink = xfs_vn_symlink,
788 .mkdir = xfs_vn_mkdir,
790 * Yes, XFS uses the same method for rmdir and unlink.
792 * There are some subtile differences deeper in the code,
793 * but we use S_ISDIR to check for those.
795 .rmdir = xfs_vn_unlink,
796 .mknod = xfs_vn_mknod,
797 .rename = xfs_vn_rename,
798 .permission = xfs_vn_permission,
799 .getattr = xfs_vn_getattr,
800 .setattr = xfs_vn_setattr,
801 .setxattr = generic_setxattr,
802 .getxattr = generic_getxattr,
803 .removexattr = generic_removexattr,
804 .listxattr = xfs_vn_listxattr,
807 static const struct inode_operations xfs_symlink_inode_operations = {
808 .readlink = generic_readlink,
809 .follow_link = xfs_vn_follow_link,
810 .put_link = xfs_vn_put_link,
811 .permission = xfs_vn_permission,
812 .getattr = xfs_vn_getattr,
813 .setattr = xfs_vn_setattr,
814 .setxattr = generic_setxattr,
815 .getxattr = generic_getxattr,
816 .removexattr = generic_removexattr,
817 .listxattr = xfs_vn_listxattr,
821 xfs_diflags_to_iflags(
823 struct xfs_inode *ip)
825 if (ip->i_d.di_flags & XFS_DIFLAG_IMMUTABLE)
826 inode->i_flags |= S_IMMUTABLE;
828 inode->i_flags &= ~S_IMMUTABLE;
829 if (ip->i_d.di_flags & XFS_DIFLAG_APPEND)
830 inode->i_flags |= S_APPEND;
832 inode->i_flags &= ~S_APPEND;
833 if (ip->i_d.di_flags & XFS_DIFLAG_SYNC)
834 inode->i_flags |= S_SYNC;
836 inode->i_flags &= ~S_SYNC;
837 if (ip->i_d.di_flags & XFS_DIFLAG_NOATIME)
838 inode->i_flags |= S_NOATIME;
840 inode->i_flags &= ~S_NOATIME;
844 * Initialize the Linux inode, set up the operation vectors and
847 * When reading existing inodes from disk this is called directly
848 * from xfs_iget, when creating a new inode it is called from
849 * xfs_ialloc after setting up the inode.
851 * We are always called with an uninitialised linux inode here.
852 * We need to initialise the necessary fields and take a reference
857 struct xfs_inode *ip)
859 struct inode *inode = &ip->i_vnode;
861 inode->i_ino = ip->i_ino;
862 inode->i_state = I_NEW|I_LOCK;
863 inode_add_to_lists(ip->i_mount->m_super, inode);
865 inode->i_mode = ip->i_d.di_mode;
866 inode->i_nlink = ip->i_d.di_nlink;
867 inode->i_uid = ip->i_d.di_uid;
868 inode->i_gid = ip->i_d.di_gid;
870 switch (inode->i_mode & S_IFMT) {
874 MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
875 sysv_minor(ip->i_df.if_u2.if_rdev));
882 inode->i_generation = ip->i_d.di_gen;
883 i_size_write(inode, ip->i_d.di_size);
884 inode->i_atime.tv_sec = ip->i_d.di_atime.t_sec;
885 inode->i_atime.tv_nsec = ip->i_d.di_atime.t_nsec;
886 inode->i_mtime.tv_sec = ip->i_d.di_mtime.t_sec;
887 inode->i_mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
888 inode->i_ctime.tv_sec = ip->i_d.di_ctime.t_sec;
889 inode->i_ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
890 xfs_diflags_to_iflags(inode, ip);
891 xfs_iflags_clear(ip, XFS_IMODIFIED);
893 switch (inode->i_mode & S_IFMT) {
895 inode->i_op = &xfs_inode_operations;
896 inode->i_fop = &xfs_file_operations;
897 inode->i_mapping->a_ops = &xfs_address_space_operations;
900 if (xfs_sb_version_hasasciici(&XFS_M(inode->i_sb)->m_sb))
901 inode->i_op = &xfs_dir_ci_inode_operations;
903 inode->i_op = &xfs_dir_inode_operations;
904 inode->i_fop = &xfs_dir_file_operations;
907 inode->i_op = &xfs_symlink_inode_operations;
908 if (!(ip->i_df.if_flags & XFS_IFINLINE))
909 inode->i_mapping->a_ops = &xfs_address_space_operations;
912 inode->i_op = &xfs_inode_operations;
913 init_special_inode(inode, inode->i_mode, inode->i_rdev);
917 xfs_iflags_clear(ip, XFS_INEW);
920 unlock_new_inode(inode);