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 (S_ISCHR(mode) || S_ISBLK(mode)) {
215 if (unlikely(!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff))
217 rdev = sysv_encode_dev(rdev);
222 if (test_default_acl && test_default_acl(dir)) {
223 if (!_ACL_ALLOC(default_acl)) {
226 if (!_ACL_GET_DEFAULT(dir, default_acl)) {
227 _ACL_FREE(default_acl);
232 if (IS_POSIXACL(dir) && !default_acl)
233 mode &= ~current_umask();
235 xfs_dentry_to_name(&name, dentry);
236 error = xfs_create(XFS_I(dir), &name, mode, rdev, &ip, NULL);
242 error = xfs_init_security(inode, dir);
244 goto out_cleanup_inode;
247 error = _ACL_INHERIT(inode, mode, default_acl);
249 goto out_cleanup_inode;
250 _ACL_FREE(default_acl);
254 d_instantiate(dentry, inode);
258 xfs_cleanup_inode(dir, inode, dentry);
261 _ACL_FREE(default_acl);
268 struct dentry *dentry,
270 struct nameidata *nd)
272 return xfs_vn_mknod(dir, dentry, mode, 0);
278 struct dentry *dentry,
281 return xfs_vn_mknod(dir, dentry, mode|S_IFDIR, 0);
284 STATIC struct dentry *
287 struct dentry *dentry,
288 struct nameidata *nd)
290 struct xfs_inode *cip;
291 struct xfs_name name;
294 if (dentry->d_name.len >= MAXNAMELEN)
295 return ERR_PTR(-ENAMETOOLONG);
297 xfs_dentry_to_name(&name, dentry);
298 error = xfs_lookup(XFS_I(dir), &name, &cip, NULL);
299 if (unlikely(error)) {
300 if (unlikely(error != ENOENT))
301 return ERR_PTR(-error);
306 return d_splice_alias(VFS_I(cip), dentry);
309 STATIC struct dentry *
312 struct dentry *dentry,
313 struct nameidata *nd)
315 struct xfs_inode *ip;
316 struct xfs_name xname;
317 struct xfs_name ci_name;
321 if (dentry->d_name.len >= MAXNAMELEN)
322 return ERR_PTR(-ENAMETOOLONG);
324 xfs_dentry_to_name(&xname, dentry);
325 error = xfs_lookup(XFS_I(dir), &xname, &ip, &ci_name);
326 if (unlikely(error)) {
327 if (unlikely(error != ENOENT))
328 return ERR_PTR(-error);
330 * call d_add(dentry, NULL) here when d_drop_negative_children
331 * is called in xfs_vn_mknod (ie. allow negative dentries
332 * with CI filesystems).
337 /* if exact match, just splice and exit */
339 return d_splice_alias(VFS_I(ip), dentry);
341 /* else case-insensitive match... */
342 dname.name = ci_name.name;
343 dname.len = ci_name.len;
344 dentry = d_add_ci(dentry, VFS_I(ip), &dname);
345 kmem_free(ci_name.name);
351 struct dentry *old_dentry,
353 struct dentry *dentry)
355 struct inode *inode = old_dentry->d_inode;
356 struct xfs_name name;
359 xfs_dentry_to_name(&name, dentry);
361 error = xfs_link(XFS_I(dir), XFS_I(inode), &name);
365 atomic_inc(&inode->i_count);
366 d_instantiate(dentry, inode);
373 struct dentry *dentry)
375 struct xfs_name name;
378 xfs_dentry_to_name(&name, dentry);
380 error = -xfs_remove(XFS_I(dir), &name, XFS_I(dentry->d_inode));
385 * With unlink, the VFS makes the dentry "negative": no inode,
386 * but still hashed. This is incompatible with case-insensitive
387 * mode, so invalidate (unhash) the dentry in CI-mode.
389 if (xfs_sb_version_hasasciici(&XFS_M(dir->i_sb)->m_sb))
390 d_invalidate(dentry);
397 struct dentry *dentry,
401 struct xfs_inode *cip = NULL;
402 struct xfs_name name;
407 (irix_symlink_mode ? 0777 & ~current_umask() : S_IRWXUGO);
408 xfs_dentry_to_name(&name, dentry);
410 error = xfs_symlink(XFS_I(dir), &name, symname, mode, &cip, NULL);
416 error = xfs_init_security(inode, dir);
418 goto out_cleanup_inode;
420 d_instantiate(dentry, inode);
424 xfs_cleanup_inode(dir, inode, dentry);
432 struct dentry *odentry,
434 struct dentry *ndentry)
436 struct inode *new_inode = ndentry->d_inode;
437 struct xfs_name oname;
438 struct xfs_name nname;
440 xfs_dentry_to_name(&oname, odentry);
441 xfs_dentry_to_name(&nname, ndentry);
443 return -xfs_rename(XFS_I(odir), &oname, XFS_I(odentry->d_inode),
444 XFS_I(ndir), &nname, new_inode ?
445 XFS_I(new_inode) : NULL);
449 * careful here - this function can get called recursively, so
450 * we need to be very careful about how much stack we use.
451 * uio is kmalloced for this reason...
455 struct dentry *dentry,
456 struct nameidata *nd)
461 link = kmalloc(MAXPATHLEN+1, GFP_KERNEL);
465 error = -xfs_readlink(XFS_I(dentry->d_inode), link);
469 nd_set_link(nd, link);
475 nd_set_link(nd, ERR_PTR(error));
481 struct dentry *dentry,
482 struct nameidata *nd,
485 char *s = nd_get_link(nd);
491 #ifdef CONFIG_XFS_POSIX_ACL
497 struct xfs_inode *ip = XFS_I(inode);
500 xfs_itrace_entry(ip);
502 if (XFS_IFORK_Q(ip)) {
503 error = xfs_acl_iaccess(ip, mask, NULL);
516 return generic_permission(inode, mask, xfs_check_acl);
519 #define xfs_vn_permission NULL
524 struct vfsmount *mnt,
525 struct dentry *dentry,
528 struct inode *inode = dentry->d_inode;
529 struct xfs_inode *ip = XFS_I(inode);
530 struct xfs_mount *mp = ip->i_mount;
532 xfs_itrace_entry(ip);
534 if (XFS_FORCED_SHUTDOWN(mp))
535 return XFS_ERROR(EIO);
537 stat->size = XFS_ISIZE(ip);
538 stat->dev = inode->i_sb->s_dev;
539 stat->mode = ip->i_d.di_mode;
540 stat->nlink = ip->i_d.di_nlink;
541 stat->uid = ip->i_d.di_uid;
542 stat->gid = ip->i_d.di_gid;
543 stat->ino = ip->i_ino;
544 stat->atime = inode->i_atime;
545 stat->mtime.tv_sec = ip->i_d.di_mtime.t_sec;
546 stat->mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
547 stat->ctime.tv_sec = ip->i_d.di_ctime.t_sec;
548 stat->ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
550 XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
553 switch (inode->i_mode & S_IFMT) {
556 stat->blksize = BLKDEV_IOSIZE;
557 stat->rdev = MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
558 sysv_minor(ip->i_df.if_u2.if_rdev));
561 if (XFS_IS_REALTIME_INODE(ip)) {
563 * If the file blocks are being allocated from a
564 * realtime volume, then return the inode's realtime
565 * extent size or the realtime volume's extent size.
568 xfs_get_extsz_hint(ip) << mp->m_sb.sb_blocklog;
570 stat->blksize = xfs_preferred_iosize(mp);
580 struct dentry *dentry,
583 return -xfs_setattr(XFS_I(dentry->d_inode), iattr, 0);
587 * block_truncate_page can return an error, but we can't propagate it
588 * at all here. Leave a complaint + stack trace in the syslog because
589 * this could be bad. If it is bad, we need to propagate the error further.
596 error = block_truncate_page(inode->i_mapping, inode->i_size,
611 xfs_inode_t *ip = XFS_I(inode);
613 /* preallocation on directories not yet supported */
615 if (S_ISDIR(inode->i_mode))
622 xfs_ilock(ip, XFS_IOLOCK_EXCL);
623 error = xfs_change_file_space(ip, XFS_IOC_RESVSP, &bf,
625 if (!error && !(mode & FALLOC_FL_KEEP_SIZE) &&
626 offset + len > i_size_read(inode))
627 new_size = offset + len;
629 /* Change file size if needed */
633 iattr.ia_valid = ATTR_SIZE;
634 iattr.ia_size = new_size;
635 error = xfs_setattr(ip, &iattr, XFS_ATTR_NOLOCK);
638 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
643 #define XFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR)
646 * Call fiemap helper to fill in user data.
647 * Returns positive errors to xfs_getbmap.
652 struct getbmapx *bmv,
656 struct fiemap_extent_info *fieinfo = *arg;
657 u32 fiemap_flags = 0;
658 u64 logical, physical, length;
660 /* Do nothing for a hole */
661 if (bmv->bmv_block == -1LL)
664 logical = BBTOB(bmv->bmv_offset);
665 physical = BBTOB(bmv->bmv_block);
666 length = BBTOB(bmv->bmv_length);
668 if (bmv->bmv_oflags & BMV_OF_PREALLOC)
669 fiemap_flags |= FIEMAP_EXTENT_UNWRITTEN;
670 else if (bmv->bmv_oflags & BMV_OF_DELALLOC) {
671 fiemap_flags |= FIEMAP_EXTENT_DELALLOC;
672 physical = 0; /* no block yet */
674 if (bmv->bmv_oflags & BMV_OF_LAST)
675 fiemap_flags |= FIEMAP_EXTENT_LAST;
677 error = fiemap_fill_next_extent(fieinfo, logical, physical,
678 length, fiemap_flags);
681 *full = 1; /* user array now full */
690 struct fiemap_extent_info *fieinfo,
694 xfs_inode_t *ip = XFS_I(inode);
698 error = fiemap_check_flags(fieinfo, XFS_FIEMAP_FLAGS);
702 /* Set up bmap header for xfs internal routine */
703 bm.bmv_offset = BTOBB(start);
704 /* Special case for whole file */
705 if (length == FIEMAP_MAX_OFFSET)
706 bm.bmv_length = -1LL;
708 bm.bmv_length = BTOBB(length);
710 /* our formatter will tell xfs_getbmap when to stop. */
711 bm.bmv_count = MAXEXTNUM;
712 bm.bmv_iflags = BMV_IF_PREALLOC;
713 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR)
714 bm.bmv_iflags |= BMV_IF_ATTRFORK;
715 if (!(fieinfo->fi_flags & FIEMAP_FLAG_SYNC))
716 bm.bmv_iflags |= BMV_IF_DELALLOC;
718 error = xfs_getbmap(ip, &bm, xfs_fiemap_format, fieinfo);
725 static const struct inode_operations xfs_inode_operations = {
726 .permission = xfs_vn_permission,
727 .truncate = xfs_vn_truncate,
728 .getattr = xfs_vn_getattr,
729 .setattr = xfs_vn_setattr,
730 .setxattr = generic_setxattr,
731 .getxattr = generic_getxattr,
732 .removexattr = generic_removexattr,
733 .listxattr = xfs_vn_listxattr,
734 .fallocate = xfs_vn_fallocate,
735 .fiemap = xfs_vn_fiemap,
738 static const struct inode_operations xfs_dir_inode_operations = {
739 .create = xfs_vn_create,
740 .lookup = xfs_vn_lookup,
742 .unlink = xfs_vn_unlink,
743 .symlink = xfs_vn_symlink,
744 .mkdir = xfs_vn_mkdir,
746 * Yes, XFS uses the same method for rmdir and unlink.
748 * There are some subtile differences deeper in the code,
749 * but we use S_ISDIR to check for those.
751 .rmdir = xfs_vn_unlink,
752 .mknod = xfs_vn_mknod,
753 .rename = xfs_vn_rename,
754 .permission = xfs_vn_permission,
755 .getattr = xfs_vn_getattr,
756 .setattr = xfs_vn_setattr,
757 .setxattr = generic_setxattr,
758 .getxattr = generic_getxattr,
759 .removexattr = generic_removexattr,
760 .listxattr = xfs_vn_listxattr,
763 static const struct inode_operations xfs_dir_ci_inode_operations = {
764 .create = xfs_vn_create,
765 .lookup = xfs_vn_ci_lookup,
767 .unlink = xfs_vn_unlink,
768 .symlink = xfs_vn_symlink,
769 .mkdir = xfs_vn_mkdir,
771 * Yes, XFS uses the same method for rmdir and unlink.
773 * There are some subtile differences deeper in the code,
774 * but we use S_ISDIR to check for those.
776 .rmdir = xfs_vn_unlink,
777 .mknod = xfs_vn_mknod,
778 .rename = xfs_vn_rename,
779 .permission = xfs_vn_permission,
780 .getattr = xfs_vn_getattr,
781 .setattr = xfs_vn_setattr,
782 .setxattr = generic_setxattr,
783 .getxattr = generic_getxattr,
784 .removexattr = generic_removexattr,
785 .listxattr = xfs_vn_listxattr,
788 static const struct inode_operations xfs_symlink_inode_operations = {
789 .readlink = generic_readlink,
790 .follow_link = xfs_vn_follow_link,
791 .put_link = xfs_vn_put_link,
792 .permission = xfs_vn_permission,
793 .getattr = xfs_vn_getattr,
794 .setattr = xfs_vn_setattr,
795 .setxattr = generic_setxattr,
796 .getxattr = generic_getxattr,
797 .removexattr = generic_removexattr,
798 .listxattr = xfs_vn_listxattr,
802 xfs_diflags_to_iflags(
804 struct xfs_inode *ip)
806 if (ip->i_d.di_flags & XFS_DIFLAG_IMMUTABLE)
807 inode->i_flags |= S_IMMUTABLE;
809 inode->i_flags &= ~S_IMMUTABLE;
810 if (ip->i_d.di_flags & XFS_DIFLAG_APPEND)
811 inode->i_flags |= S_APPEND;
813 inode->i_flags &= ~S_APPEND;
814 if (ip->i_d.di_flags & XFS_DIFLAG_SYNC)
815 inode->i_flags |= S_SYNC;
817 inode->i_flags &= ~S_SYNC;
818 if (ip->i_d.di_flags & XFS_DIFLAG_NOATIME)
819 inode->i_flags |= S_NOATIME;
821 inode->i_flags &= ~S_NOATIME;
825 * Initialize the Linux inode, set up the operation vectors and
828 * When reading existing inodes from disk this is called directly
829 * from xfs_iget, when creating a new inode it is called from
830 * xfs_ialloc after setting up the inode.
832 * We are always called with an uninitialised linux inode here.
833 * We need to initialise the necessary fields and take a reference
838 struct xfs_inode *ip)
840 struct inode *inode = &ip->i_vnode;
842 inode->i_ino = ip->i_ino;
843 inode->i_state = I_NEW|I_LOCK;
844 inode_add_to_lists(ip->i_mount->m_super, inode);
846 inode->i_mode = ip->i_d.di_mode;
847 inode->i_nlink = ip->i_d.di_nlink;
848 inode->i_uid = ip->i_d.di_uid;
849 inode->i_gid = ip->i_d.di_gid;
851 switch (inode->i_mode & S_IFMT) {
855 MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
856 sysv_minor(ip->i_df.if_u2.if_rdev));
863 inode->i_generation = ip->i_d.di_gen;
864 i_size_write(inode, ip->i_d.di_size);
865 inode->i_atime.tv_sec = ip->i_d.di_atime.t_sec;
866 inode->i_atime.tv_nsec = ip->i_d.di_atime.t_nsec;
867 inode->i_mtime.tv_sec = ip->i_d.di_mtime.t_sec;
868 inode->i_mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
869 inode->i_ctime.tv_sec = ip->i_d.di_ctime.t_sec;
870 inode->i_ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
871 xfs_diflags_to_iflags(inode, ip);
873 switch (inode->i_mode & S_IFMT) {
875 inode->i_op = &xfs_inode_operations;
876 inode->i_fop = &xfs_file_operations;
877 inode->i_mapping->a_ops = &xfs_address_space_operations;
880 if (xfs_sb_version_hasasciici(&XFS_M(inode->i_sb)->m_sb))
881 inode->i_op = &xfs_dir_ci_inode_operations;
883 inode->i_op = &xfs_dir_inode_operations;
884 inode->i_fop = &xfs_dir_file_operations;
887 inode->i_op = &xfs_symlink_inode_operations;
888 if (!(ip->i_df.if_flags & XFS_IFINLINE))
889 inode->i_mapping->a_ops = &xfs_address_space_operations;
892 inode->i_op = &xfs_inode_operations;
893 init_special_inode(inode, inode->i_mode, inode->i_rdev);
897 xfs_iflags_clear(ip, XFS_INEW);
900 unlock_new_inode(inode);