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);
170 struct xfs_name *namep,
171 struct dentry *dentry)
173 namep->name = dentry->d_name.name;
174 namep->len = dentry->d_name.len;
181 struct dentry *dentry)
183 struct xfs_name teardown;
186 * If we can't add the ACL or we fail in
187 * xfs_init_security we must back out.
188 * ENOSPC can hit here, among other things.
190 xfs_dentry_to_name(&teardown, dentry);
192 xfs_remove(XFS_I(dir), &teardown, XFS_I(inode));
199 struct dentry *dentry,
204 struct xfs_inode *ip = NULL;
205 xfs_acl_t *default_acl = NULL;
206 struct xfs_name name;
207 int (*test_default_acl)(struct inode *) = _ACL_DEFAULT_EXISTS;
211 * Irix uses Missed'em'V split, but doesn't want to see
212 * the upper 5 bits of (14bit) major.
214 if (unlikely(!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff))
217 if (test_default_acl && test_default_acl(dir)) {
218 if (!_ACL_ALLOC(default_acl)) {
221 if (!_ACL_GET_DEFAULT(dir, default_acl)) {
222 _ACL_FREE(default_acl);
227 xfs_dentry_to_name(&name, dentry);
229 if (IS_POSIXACL(dir) && !default_acl)
230 mode &= ~current->fs->umask;
232 switch (mode & S_IFMT) {
237 rdev = sysv_encode_dev(rdev);
239 error = xfs_create(XFS_I(dir), &name, mode, rdev, &ip, NULL);
242 error = xfs_mkdir(XFS_I(dir), &name, mode, &ip, NULL);
254 error = xfs_init_security(inode, dir);
256 goto out_cleanup_inode;
259 error = _ACL_INHERIT(inode, mode, default_acl);
261 goto out_cleanup_inode;
262 _ACL_FREE(default_acl);
266 d_instantiate(dentry, inode);
270 xfs_cleanup_inode(dir, inode, dentry);
273 _ACL_FREE(default_acl);
280 struct dentry *dentry,
282 struct nameidata *nd)
284 return xfs_vn_mknod(dir, dentry, mode, 0);
290 struct dentry *dentry,
293 return xfs_vn_mknod(dir, dentry, mode|S_IFDIR, 0);
296 STATIC struct dentry *
299 struct dentry *dentry,
300 struct nameidata *nd)
302 struct xfs_inode *cip;
303 struct xfs_name name;
306 if (dentry->d_name.len >= MAXNAMELEN)
307 return ERR_PTR(-ENAMETOOLONG);
309 xfs_dentry_to_name(&name, dentry);
310 error = xfs_lookup(XFS_I(dir), &name, &cip, NULL);
311 if (unlikely(error)) {
312 if (unlikely(error != ENOENT))
313 return ERR_PTR(-error);
318 return d_splice_alias(VFS_I(cip), dentry);
321 STATIC struct dentry *
324 struct dentry *dentry,
325 struct nameidata *nd)
327 struct xfs_inode *ip;
328 struct xfs_name xname;
329 struct xfs_name ci_name;
333 if (dentry->d_name.len >= MAXNAMELEN)
334 return ERR_PTR(-ENAMETOOLONG);
336 xfs_dentry_to_name(&xname, dentry);
337 error = xfs_lookup(XFS_I(dir), &xname, &ip, &ci_name);
338 if (unlikely(error)) {
339 if (unlikely(error != ENOENT))
340 return ERR_PTR(-error);
342 * call d_add(dentry, NULL) here when d_drop_negative_children
343 * is called in xfs_vn_mknod (ie. allow negative dentries
344 * with CI filesystems).
349 /* if exact match, just splice and exit */
351 return d_splice_alias(VFS_I(ip), dentry);
353 /* else case-insensitive match... */
354 dname.name = ci_name.name;
355 dname.len = ci_name.len;
356 dentry = d_add_ci(dentry, VFS_I(ip), &dname);
357 kmem_free(ci_name.name);
363 struct dentry *old_dentry,
365 struct dentry *dentry)
367 struct inode *inode = old_dentry->d_inode;
368 struct xfs_name name;
371 xfs_dentry_to_name(&name, dentry);
373 error = xfs_link(XFS_I(dir), XFS_I(inode), &name);
377 atomic_inc(&inode->i_count);
378 d_instantiate(dentry, inode);
385 struct dentry *dentry)
387 struct xfs_name name;
390 xfs_dentry_to_name(&name, dentry);
392 error = -xfs_remove(XFS_I(dir), &name, XFS_I(dentry->d_inode));
397 * With unlink, the VFS makes the dentry "negative": no inode,
398 * but still hashed. This is incompatible with case-insensitive
399 * mode, so invalidate (unhash) the dentry in CI-mode.
401 if (xfs_sb_version_hasasciici(&XFS_M(dir->i_sb)->m_sb))
402 d_invalidate(dentry);
409 struct dentry *dentry,
413 struct xfs_inode *cip = NULL;
414 struct xfs_name name;
419 (irix_symlink_mode ? 0777 & ~current->fs->umask : S_IRWXUGO);
420 xfs_dentry_to_name(&name, dentry);
422 error = xfs_symlink(XFS_I(dir), &name, symname, mode, &cip, NULL);
428 error = xfs_init_security(inode, dir);
430 goto out_cleanup_inode;
432 d_instantiate(dentry, inode);
436 xfs_cleanup_inode(dir, inode, dentry);
444 struct dentry *odentry,
446 struct dentry *ndentry)
448 struct inode *new_inode = ndentry->d_inode;
449 struct xfs_name oname;
450 struct xfs_name nname;
452 xfs_dentry_to_name(&oname, odentry);
453 xfs_dentry_to_name(&nname, ndentry);
455 return -xfs_rename(XFS_I(odir), &oname, XFS_I(odentry->d_inode),
456 XFS_I(ndir), &nname, new_inode ?
457 XFS_I(new_inode) : NULL);
461 * careful here - this function can get called recursively, so
462 * we need to be very careful about how much stack we use.
463 * uio is kmalloced for this reason...
467 struct dentry *dentry,
468 struct nameidata *nd)
473 link = kmalloc(MAXPATHLEN+1, GFP_KERNEL);
477 error = -xfs_readlink(XFS_I(dentry->d_inode), link);
481 nd_set_link(nd, link);
487 nd_set_link(nd, ERR_PTR(error));
493 struct dentry *dentry,
494 struct nameidata *nd,
497 char *s = nd_get_link(nd);
503 #ifdef CONFIG_XFS_POSIX_ACL
509 struct xfs_inode *ip = XFS_I(inode);
512 xfs_itrace_entry(ip);
514 if (XFS_IFORK_Q(ip)) {
515 error = xfs_acl_iaccess(ip, mask, NULL);
528 return generic_permission(inode, mask, xfs_check_acl);
531 #define xfs_vn_permission NULL
536 struct vfsmount *mnt,
537 struct dentry *dentry,
540 struct inode *inode = dentry->d_inode;
541 struct xfs_inode *ip = XFS_I(inode);
542 struct xfs_mount *mp = ip->i_mount;
544 xfs_itrace_entry(ip);
546 if (XFS_FORCED_SHUTDOWN(mp))
547 return XFS_ERROR(EIO);
549 stat->size = XFS_ISIZE(ip);
550 stat->dev = inode->i_sb->s_dev;
551 stat->mode = ip->i_d.di_mode;
552 stat->nlink = ip->i_d.di_nlink;
553 stat->uid = ip->i_d.di_uid;
554 stat->gid = ip->i_d.di_gid;
555 stat->ino = ip->i_ino;
557 stat->ino += mp->m_inoadd;
559 stat->atime = inode->i_atime;
560 stat->mtime.tv_sec = ip->i_d.di_mtime.t_sec;
561 stat->mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
562 stat->ctime.tv_sec = ip->i_d.di_ctime.t_sec;
563 stat->ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
565 XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
568 switch (inode->i_mode & S_IFMT) {
571 stat->blksize = BLKDEV_IOSIZE;
572 stat->rdev = MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
573 sysv_minor(ip->i_df.if_u2.if_rdev));
576 if (XFS_IS_REALTIME_INODE(ip)) {
578 * If the file blocks are being allocated from a
579 * realtime volume, then return the inode's realtime
580 * extent size or the realtime volume's extent size.
583 xfs_get_extsz_hint(ip) << mp->m_sb.sb_blocklog;
585 stat->blksize = xfs_preferred_iosize(mp);
595 struct dentry *dentry,
598 return -xfs_setattr(XFS_I(dentry->d_inode), iattr, 0);
602 * block_truncate_page can return an error, but we can't propagate it
603 * at all here. Leave a complaint + stack trace in the syslog because
604 * this could be bad. If it is bad, we need to propagate the error further.
611 error = block_truncate_page(inode->i_mapping, inode->i_size,
626 xfs_inode_t *ip = XFS_I(inode);
628 /* preallocation on directories not yet supported */
630 if (S_ISDIR(inode->i_mode))
637 xfs_ilock(ip, XFS_IOLOCK_EXCL);
638 error = xfs_change_file_space(ip, XFS_IOC_RESVSP, &bf,
640 if (!error && !(mode & FALLOC_FL_KEEP_SIZE) &&
641 offset + len > i_size_read(inode))
642 new_size = offset + len;
644 /* Change file size if needed */
648 iattr.ia_valid = ATTR_SIZE;
649 iattr.ia_size = new_size;
650 error = xfs_setattr(ip, &iattr, XFS_ATTR_NOLOCK);
653 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
658 #define XFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR)
661 * Call fiemap helper to fill in user data.
662 * Returns positive errors to xfs_getbmap.
667 struct getbmapx *bmv,
671 struct fiemap_extent_info *fieinfo = *arg;
672 u32 fiemap_flags = 0;
673 u64 logical, physical, length;
675 /* Do nothing for a hole */
676 if (bmv->bmv_block == -1LL)
679 logical = BBTOB(bmv->bmv_offset);
680 physical = BBTOB(bmv->bmv_block);
681 length = BBTOB(bmv->bmv_length);
683 if (bmv->bmv_oflags & BMV_OF_PREALLOC)
684 fiemap_flags |= FIEMAP_EXTENT_UNWRITTEN;
685 else if (bmv->bmv_oflags & BMV_OF_DELALLOC) {
686 fiemap_flags |= FIEMAP_EXTENT_DELALLOC;
687 physical = 0; /* no block yet */
689 if (bmv->bmv_oflags & BMV_OF_LAST)
690 fiemap_flags |= FIEMAP_EXTENT_LAST;
692 error = fiemap_fill_next_extent(fieinfo, logical, physical,
693 length, fiemap_flags);
696 *full = 1; /* user array now full */
705 struct fiemap_extent_info *fieinfo,
709 xfs_inode_t *ip = XFS_I(inode);
713 error = fiemap_check_flags(fieinfo, XFS_FIEMAP_FLAGS);
717 /* Set up bmap header for xfs internal routine */
718 bm.bmv_offset = BTOBB(start);
719 /* Special case for whole file */
720 if (length == FIEMAP_MAX_OFFSET)
721 bm.bmv_length = -1LL;
723 bm.bmv_length = BTOBB(length);
725 /* our formatter will tell xfs_getbmap when to stop. */
726 bm.bmv_count = MAXEXTNUM;
727 bm.bmv_iflags = BMV_IF_PREALLOC;
728 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR)
729 bm.bmv_iflags |= BMV_IF_ATTRFORK;
730 if (!(fieinfo->fi_flags & FIEMAP_FLAG_SYNC))
731 bm.bmv_iflags |= BMV_IF_DELALLOC;
733 error = xfs_getbmap(ip, &bm, xfs_fiemap_format, fieinfo);
740 static const struct inode_operations xfs_inode_operations = {
741 .permission = xfs_vn_permission,
742 .truncate = xfs_vn_truncate,
743 .getattr = xfs_vn_getattr,
744 .setattr = xfs_vn_setattr,
745 .setxattr = generic_setxattr,
746 .getxattr = generic_getxattr,
747 .removexattr = generic_removexattr,
748 .listxattr = xfs_vn_listxattr,
749 .fallocate = xfs_vn_fallocate,
750 .fiemap = xfs_vn_fiemap,
753 static const struct inode_operations xfs_dir_inode_operations = {
754 .create = xfs_vn_create,
755 .lookup = xfs_vn_lookup,
757 .unlink = xfs_vn_unlink,
758 .symlink = xfs_vn_symlink,
759 .mkdir = xfs_vn_mkdir,
761 * Yes, XFS uses the same method for rmdir and unlink.
763 * There are some subtile differences deeper in the code,
764 * but we use S_ISDIR to check for those.
766 .rmdir = xfs_vn_unlink,
767 .mknod = xfs_vn_mknod,
768 .rename = xfs_vn_rename,
769 .permission = xfs_vn_permission,
770 .getattr = xfs_vn_getattr,
771 .setattr = xfs_vn_setattr,
772 .setxattr = generic_setxattr,
773 .getxattr = generic_getxattr,
774 .removexattr = generic_removexattr,
775 .listxattr = xfs_vn_listxattr,
778 static const struct inode_operations xfs_dir_ci_inode_operations = {
779 .create = xfs_vn_create,
780 .lookup = xfs_vn_ci_lookup,
782 .unlink = xfs_vn_unlink,
783 .symlink = xfs_vn_symlink,
784 .mkdir = xfs_vn_mkdir,
786 * Yes, XFS uses the same method for rmdir and unlink.
788 * There are some subtile differences deeper in the code,
789 * but we use S_ISDIR to check for those.
791 .rmdir = xfs_vn_unlink,
792 .mknod = xfs_vn_mknod,
793 .rename = xfs_vn_rename,
794 .permission = xfs_vn_permission,
795 .getattr = xfs_vn_getattr,
796 .setattr = xfs_vn_setattr,
797 .setxattr = generic_setxattr,
798 .getxattr = generic_getxattr,
799 .removexattr = generic_removexattr,
800 .listxattr = xfs_vn_listxattr,
803 static const struct inode_operations xfs_symlink_inode_operations = {
804 .readlink = generic_readlink,
805 .follow_link = xfs_vn_follow_link,
806 .put_link = xfs_vn_put_link,
807 .permission = xfs_vn_permission,
808 .getattr = xfs_vn_getattr,
809 .setattr = xfs_vn_setattr,
810 .setxattr = generic_setxattr,
811 .getxattr = generic_getxattr,
812 .removexattr = generic_removexattr,
813 .listxattr = xfs_vn_listxattr,
817 xfs_diflags_to_iflags(
819 struct xfs_inode *ip)
821 if (ip->i_d.di_flags & XFS_DIFLAG_IMMUTABLE)
822 inode->i_flags |= S_IMMUTABLE;
824 inode->i_flags &= ~S_IMMUTABLE;
825 if (ip->i_d.di_flags & XFS_DIFLAG_APPEND)
826 inode->i_flags |= S_APPEND;
828 inode->i_flags &= ~S_APPEND;
829 if (ip->i_d.di_flags & XFS_DIFLAG_SYNC)
830 inode->i_flags |= S_SYNC;
832 inode->i_flags &= ~S_SYNC;
833 if (ip->i_d.di_flags & XFS_DIFLAG_NOATIME)
834 inode->i_flags |= S_NOATIME;
836 inode->i_flags &= ~S_NOATIME;
840 * Initialize the Linux inode, set up the operation vectors and
843 * When reading existing inodes from disk this is called directly
844 * from xfs_iget, when creating a new inode it is called from
845 * xfs_ialloc after setting up the inode.
847 * We are always called with an uninitialised linux inode here.
848 * We need to initialise the necessary fields and take a reference
853 struct xfs_inode *ip)
855 struct inode *inode = &ip->i_vnode;
857 inode->i_ino = ip->i_ino;
858 inode->i_state = I_NEW|I_LOCK;
859 inode_add_to_lists(ip->i_mount->m_super, inode);
861 inode->i_mode = ip->i_d.di_mode;
862 inode->i_nlink = ip->i_d.di_nlink;
863 inode->i_uid = ip->i_d.di_uid;
864 inode->i_gid = ip->i_d.di_gid;
866 switch (inode->i_mode & S_IFMT) {
870 MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
871 sysv_minor(ip->i_df.if_u2.if_rdev));
878 inode->i_generation = ip->i_d.di_gen;
879 i_size_write(inode, ip->i_d.di_size);
880 inode->i_atime.tv_sec = ip->i_d.di_atime.t_sec;
881 inode->i_atime.tv_nsec = ip->i_d.di_atime.t_nsec;
882 inode->i_mtime.tv_sec = ip->i_d.di_mtime.t_sec;
883 inode->i_mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
884 inode->i_ctime.tv_sec = ip->i_d.di_ctime.t_sec;
885 inode->i_ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
886 xfs_diflags_to_iflags(inode, ip);
888 switch (inode->i_mode & S_IFMT) {
890 inode->i_op = &xfs_inode_operations;
891 inode->i_fop = &xfs_file_operations;
892 inode->i_mapping->a_ops = &xfs_address_space_operations;
895 if (xfs_sb_version_hasasciici(&XFS_M(inode->i_sb)->m_sb))
896 inode->i_op = &xfs_dir_ci_inode_operations;
898 inode->i_op = &xfs_dir_inode_operations;
899 inode->i_fop = &xfs_dir_file_operations;
902 inode->i_op = &xfs_symlink_inode_operations;
903 if (!(ip->i_df.if_flags & XFS_IFINLINE))
904 inode->i_mapping->a_ops = &xfs_address_space_operations;
907 inode->i_op = &xfs_inode_operations;
908 init_special_inode(inode, inode->i_mode, inode->i_rdev);
912 xfs_iflags_clear(ip, XFS_INEW);
915 unlock_new_inode(inode);