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 = VFS_I(ip);
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 = VFS_I(ip);
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.
98 struct inode *inode = VFS_I(ip);
102 tv = current_fs_time(inode->i_sb);
104 if ((flags & XFS_ICHGTIME_MOD) &&
105 !timespec_equal(&inode->i_mtime, &tv)) {
107 ip->i_d.di_mtime.t_sec = (__int32_t)tv.tv_sec;
108 ip->i_d.di_mtime.t_nsec = (__int32_t)tv.tv_nsec;
111 if ((flags & XFS_ICHGTIME_CHG) &&
112 !timespec_equal(&inode->i_ctime, &tv)) {
114 ip->i_d.di_ctime.t_sec = (__int32_t)tv.tv_sec;
115 ip->i_d.di_ctime.t_nsec = (__int32_t)tv.tv_nsec;
120 * We update the i_update_core field _after_ changing
121 * the timestamps in order to coordinate properly with
122 * xfs_iflush() so that we don't lose timestamp updates.
123 * This keeps us from having to hold the inode lock
124 * while doing this. We use the SYNCHRONIZE macro to
125 * ensure that the compiler does not reorder the update
126 * of i_update_core above the timestamp updates above.
130 ip->i_update_core = 1;
131 mark_inode_dirty_sync(inode);
136 * Variant on the above which avoids querying the system clock
137 * in situations where we know the Linux inode timestamps have
138 * just been updated (and so we can update our inode cheaply).
148 if (flags & XFS_ICHGTIME_MOD) {
149 tvp = &inode->i_mtime;
150 ip->i_d.di_mtime.t_sec = (__int32_t)tvp->tv_sec;
151 ip->i_d.di_mtime.t_nsec = (__int32_t)tvp->tv_nsec;
153 if (flags & XFS_ICHGTIME_CHG) {
154 tvp = &inode->i_ctime;
155 ip->i_d.di_ctime.t_sec = (__int32_t)tvp->tv_sec;
156 ip->i_d.di_ctime.t_nsec = (__int32_t)tvp->tv_nsec;
160 * We update the i_update_core field _after_ changing
161 * the timestamps in order to coordinate properly with
162 * xfs_iflush() so that we don't lose timestamp updates.
163 * This keeps us from having to hold the inode lock
164 * while doing this. We use the SYNCHRONIZE macro to
165 * ensure that the compiler does not reorder the update
166 * of i_update_core above the timestamp updates above.
169 ip->i_update_core = 1;
170 if (!(inode->i_state & I_NEW))
171 mark_inode_dirty_sync(inode);
175 * Hook in SELinux. This is not quite correct yet, what we really need
176 * here (as we do for default ACLs) is a mechanism by which creation of
177 * these attrs can be journalled at inode creation time (along with the
178 * inode, of course, such that log replay can't cause these to be lost).
185 struct xfs_inode *ip = XFS_I(inode);
191 error = security_inode_init_security(inode, dir, &name,
194 if (error == -EOPNOTSUPP)
199 error = xfs_attr_set(ip, name, value, length, ATTR_SECURE);
201 xfs_iflags_set(ip, XFS_IMODIFIED);
210 struct xfs_name *namep,
211 struct dentry *dentry)
213 namep->name = dentry->d_name.name;
214 namep->len = dentry->d_name.len;
221 struct dentry *dentry)
223 struct xfs_name teardown;
226 * If we can't add the ACL or we fail in
227 * xfs_init_security we must back out.
228 * ENOSPC can hit here, among other things.
230 xfs_dentry_to_name(&teardown, dentry);
232 xfs_remove(XFS_I(dir), &teardown, XFS_I(inode));
239 struct dentry *dentry,
244 struct xfs_inode *ip = NULL;
245 xfs_acl_t *default_acl = NULL;
246 struct xfs_name name;
247 int (*test_default_acl)(struct inode *) = _ACL_DEFAULT_EXISTS;
251 * Irix uses Missed'em'V split, but doesn't want to see
252 * the upper 5 bits of (14bit) major.
254 if (unlikely(!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff))
257 if (test_default_acl && test_default_acl(dir)) {
258 if (!_ACL_ALLOC(default_acl)) {
261 if (!_ACL_GET_DEFAULT(dir, default_acl)) {
262 _ACL_FREE(default_acl);
267 xfs_dentry_to_name(&name, dentry);
269 if (IS_POSIXACL(dir) && !default_acl)
270 mode &= ~current->fs->umask;
272 switch (mode & S_IFMT) {
277 rdev = sysv_encode_dev(rdev);
279 error = xfs_create(XFS_I(dir), &name, mode, rdev, &ip, NULL);
282 error = xfs_mkdir(XFS_I(dir), &name, mode, &ip, NULL);
294 error = xfs_init_security(inode, dir);
296 goto out_cleanup_inode;
299 error = _ACL_INHERIT(inode, mode, default_acl);
301 goto out_cleanup_inode;
302 xfs_iflags_set(ip, XFS_IMODIFIED);
303 _ACL_FREE(default_acl);
307 d_instantiate(dentry, inode);
311 xfs_cleanup_inode(dir, inode, dentry);
314 _ACL_FREE(default_acl);
321 struct dentry *dentry,
323 struct nameidata *nd)
325 return xfs_vn_mknod(dir, dentry, mode, 0);
331 struct dentry *dentry,
334 return xfs_vn_mknod(dir, dentry, mode|S_IFDIR, 0);
337 STATIC struct dentry *
340 struct dentry *dentry,
341 struct nameidata *nd)
343 struct xfs_inode *cip;
344 struct xfs_name name;
347 if (dentry->d_name.len >= MAXNAMELEN)
348 return ERR_PTR(-ENAMETOOLONG);
350 xfs_dentry_to_name(&name, dentry);
351 error = xfs_lookup(XFS_I(dir), &name, &cip, NULL);
352 if (unlikely(error)) {
353 if (unlikely(error != ENOENT))
354 return ERR_PTR(-error);
359 return d_splice_alias(VFS_I(cip), dentry);
362 STATIC struct dentry *
365 struct dentry *dentry,
366 struct nameidata *nd)
368 struct xfs_inode *ip;
369 struct xfs_name xname;
370 struct xfs_name ci_name;
374 if (dentry->d_name.len >= MAXNAMELEN)
375 return ERR_PTR(-ENAMETOOLONG);
377 xfs_dentry_to_name(&xname, dentry);
378 error = xfs_lookup(XFS_I(dir), &xname, &ip, &ci_name);
379 if (unlikely(error)) {
380 if (unlikely(error != ENOENT))
381 return ERR_PTR(-error);
383 * call d_add(dentry, NULL) here when d_drop_negative_children
384 * is called in xfs_vn_mknod (ie. allow negative dentries
385 * with CI filesystems).
390 /* if exact match, just splice and exit */
392 return d_splice_alias(VFS_I(ip), dentry);
394 /* else case-insensitive match... */
395 dname.name = ci_name.name;
396 dname.len = ci_name.len;
397 dentry = d_add_ci(VFS_I(ip), dentry, &dname);
398 kmem_free(ci_name.name);
404 struct dentry *old_dentry,
406 struct dentry *dentry)
408 struct inode *inode; /* inode of guy being linked to */
409 struct xfs_name name;
412 inode = old_dentry->d_inode;
413 xfs_dentry_to_name(&name, dentry);
416 error = xfs_link(XFS_I(dir), XFS_I(inode), &name);
417 if (unlikely(error)) {
422 xfs_iflags_set(XFS_I(dir), XFS_IMODIFIED);
423 d_instantiate(dentry, inode);
430 struct dentry *dentry)
432 struct xfs_name name;
435 xfs_dentry_to_name(&name, dentry);
437 error = -xfs_remove(XFS_I(dir), &name, XFS_I(dentry->d_inode));
442 * With unlink, the VFS makes the dentry "negative": no inode,
443 * but still hashed. This is incompatible with case-insensitive
444 * mode, so invalidate (unhash) the dentry in CI-mode.
446 if (xfs_sb_version_hasasciici(&XFS_M(dir->i_sb)->m_sb))
447 d_invalidate(dentry);
454 struct dentry *dentry,
458 struct xfs_inode *cip = NULL;
459 struct xfs_name name;
464 (irix_symlink_mode ? 0777 & ~current->fs->umask : S_IRWXUGO);
465 xfs_dentry_to_name(&name, dentry);
467 error = xfs_symlink(XFS_I(dir), &name, symname, mode, &cip, NULL);
473 error = xfs_init_security(inode, dir);
475 goto out_cleanup_inode;
477 d_instantiate(dentry, inode);
481 xfs_cleanup_inode(dir, inode, dentry);
489 struct dentry *odentry,
491 struct dentry *ndentry)
493 struct inode *new_inode = ndentry->d_inode;
494 struct xfs_name oname;
495 struct xfs_name nname;
497 xfs_dentry_to_name(&oname, odentry);
498 xfs_dentry_to_name(&nname, ndentry);
500 return -xfs_rename(XFS_I(odir), &oname, XFS_I(odentry->d_inode),
501 XFS_I(ndir), &nname, new_inode ?
502 XFS_I(new_inode) : NULL);
506 * careful here - this function can get called recursively, so
507 * we need to be very careful about how much stack we use.
508 * uio is kmalloced for this reason...
512 struct dentry *dentry,
513 struct nameidata *nd)
518 link = kmalloc(MAXPATHLEN+1, GFP_KERNEL);
522 error = -xfs_readlink(XFS_I(dentry->d_inode), link);
526 nd_set_link(nd, link);
532 nd_set_link(nd, ERR_PTR(error));
538 struct dentry *dentry,
539 struct nameidata *nd,
542 char *s = nd_get_link(nd);
548 #ifdef CONFIG_XFS_POSIX_ACL
554 struct xfs_inode *ip = XFS_I(inode);
557 xfs_itrace_entry(ip);
559 if (XFS_IFORK_Q(ip)) {
560 error = xfs_acl_iaccess(ip, mask, NULL);
573 return generic_permission(inode, mask, xfs_check_acl);
576 #define xfs_vn_permission NULL
581 struct vfsmount *mnt,
582 struct dentry *dentry,
585 struct inode *inode = dentry->d_inode;
586 struct xfs_inode *ip = XFS_I(inode);
587 struct xfs_mount *mp = ip->i_mount;
589 xfs_itrace_entry(ip);
591 if (XFS_FORCED_SHUTDOWN(mp))
592 return XFS_ERROR(EIO);
594 stat->size = XFS_ISIZE(ip);
595 stat->dev = inode->i_sb->s_dev;
596 stat->mode = ip->i_d.di_mode;
597 stat->nlink = ip->i_d.di_nlink;
598 stat->uid = ip->i_d.di_uid;
599 stat->gid = ip->i_d.di_gid;
600 stat->ino = ip->i_ino;
602 stat->ino += mp->m_inoadd;
604 stat->atime = inode->i_atime;
605 stat->mtime.tv_sec = ip->i_d.di_mtime.t_sec;
606 stat->mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
607 stat->ctime.tv_sec = ip->i_d.di_ctime.t_sec;
608 stat->ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
610 XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
613 switch (inode->i_mode & S_IFMT) {
616 stat->blksize = BLKDEV_IOSIZE;
617 stat->rdev = MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
618 sysv_minor(ip->i_df.if_u2.if_rdev));
621 if (XFS_IS_REALTIME_INODE(ip)) {
623 * If the file blocks are being allocated from a
624 * realtime volume, then return the inode's realtime
625 * extent size or the realtime volume's extent size.
628 xfs_get_extsz_hint(ip) << mp->m_sb.sb_blocklog;
630 stat->blksize = xfs_preferred_iosize(mp);
640 struct dentry *dentry,
643 return -xfs_setattr(XFS_I(dentry->d_inode), iattr, 0, NULL);
647 * block_truncate_page can return an error, but we can't propagate it
648 * at all here. Leave a complaint + stack trace in the syslog because
649 * this could be bad. If it is bad, we need to propagate the error further.
656 error = block_truncate_page(inode->i_mapping, inode->i_size,
671 xfs_inode_t *ip = XFS_I(inode);
673 /* preallocation on directories not yet supported */
675 if (S_ISDIR(inode->i_mode))
682 xfs_ilock(ip, XFS_IOLOCK_EXCL);
683 error = xfs_change_file_space(ip, XFS_IOC_RESVSP, &bf,
684 0, NULL, XFS_ATTR_NOLOCK);
685 if (!error && !(mode & FALLOC_FL_KEEP_SIZE) &&
686 offset + len > i_size_read(inode))
687 new_size = offset + len;
689 /* Change file size if needed */
693 iattr.ia_valid = ATTR_SIZE;
694 iattr.ia_size = new_size;
695 error = xfs_setattr(ip, &iattr, XFS_ATTR_NOLOCK, NULL);
698 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
703 static const struct inode_operations xfs_inode_operations = {
704 .permission = xfs_vn_permission,
705 .truncate = xfs_vn_truncate,
706 .getattr = xfs_vn_getattr,
707 .setattr = xfs_vn_setattr,
708 .setxattr = generic_setxattr,
709 .getxattr = generic_getxattr,
710 .removexattr = generic_removexattr,
711 .listxattr = xfs_vn_listxattr,
712 .fallocate = xfs_vn_fallocate,
715 static const struct inode_operations xfs_dir_inode_operations = {
716 .create = xfs_vn_create,
717 .lookup = xfs_vn_lookup,
719 .unlink = xfs_vn_unlink,
720 .symlink = xfs_vn_symlink,
721 .mkdir = xfs_vn_mkdir,
723 * Yes, XFS uses the same method for rmdir and unlink.
725 * There are some subtile differences deeper in the code,
726 * but we use S_ISDIR to check for those.
728 .rmdir = xfs_vn_unlink,
729 .mknod = xfs_vn_mknod,
730 .rename = xfs_vn_rename,
731 .permission = xfs_vn_permission,
732 .getattr = xfs_vn_getattr,
733 .setattr = xfs_vn_setattr,
734 .setxattr = generic_setxattr,
735 .getxattr = generic_getxattr,
736 .removexattr = generic_removexattr,
737 .listxattr = xfs_vn_listxattr,
740 static const struct inode_operations xfs_dir_ci_inode_operations = {
741 .create = xfs_vn_create,
742 .lookup = xfs_vn_ci_lookup,
744 .unlink = xfs_vn_unlink,
745 .symlink = xfs_vn_symlink,
746 .mkdir = xfs_vn_mkdir,
748 * Yes, XFS uses the same method for rmdir and unlink.
750 * There are some subtile differences deeper in the code,
751 * but we use S_ISDIR to check for those.
753 .rmdir = xfs_vn_unlink,
754 .mknod = xfs_vn_mknod,
755 .rename = xfs_vn_rename,
756 .permission = xfs_vn_permission,
757 .getattr = xfs_vn_getattr,
758 .setattr = xfs_vn_setattr,
759 .setxattr = generic_setxattr,
760 .getxattr = generic_getxattr,
761 .removexattr = generic_removexattr,
762 .listxattr = xfs_vn_listxattr,
765 static const struct inode_operations xfs_symlink_inode_operations = {
766 .readlink = generic_readlink,
767 .follow_link = xfs_vn_follow_link,
768 .put_link = xfs_vn_put_link,
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,
779 xfs_diflags_to_iflags(
781 struct xfs_inode *ip)
783 if (ip->i_d.di_flags & XFS_DIFLAG_IMMUTABLE)
784 inode->i_flags |= S_IMMUTABLE;
786 inode->i_flags &= ~S_IMMUTABLE;
787 if (ip->i_d.di_flags & XFS_DIFLAG_APPEND)
788 inode->i_flags |= S_APPEND;
790 inode->i_flags &= ~S_APPEND;
791 if (ip->i_d.di_flags & XFS_DIFLAG_SYNC)
792 inode->i_flags |= S_SYNC;
794 inode->i_flags &= ~S_SYNC;
795 if (ip->i_d.di_flags & XFS_DIFLAG_NOATIME)
796 inode->i_flags |= S_NOATIME;
798 inode->i_flags &= ~S_NOATIME;
802 * Initialize the Linux inode, set up the operation vectors and
805 * When reading existing inodes from disk this is called directly
806 * from xfs_iget, when creating a new inode it is called from
807 * xfs_ialloc after setting up the inode.
811 struct xfs_inode *ip)
813 struct inode *inode = ip->i_vnode;
815 inode->i_mode = ip->i_d.di_mode;
816 inode->i_nlink = ip->i_d.di_nlink;
817 inode->i_uid = ip->i_d.di_uid;
818 inode->i_gid = ip->i_d.di_gid;
820 switch (inode->i_mode & S_IFMT) {
824 MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
825 sysv_minor(ip->i_df.if_u2.if_rdev));
832 inode->i_generation = ip->i_d.di_gen;
833 i_size_write(inode, ip->i_d.di_size);
834 inode->i_atime.tv_sec = ip->i_d.di_atime.t_sec;
835 inode->i_atime.tv_nsec = ip->i_d.di_atime.t_nsec;
836 inode->i_mtime.tv_sec = ip->i_d.di_mtime.t_sec;
837 inode->i_mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
838 inode->i_ctime.tv_sec = ip->i_d.di_ctime.t_sec;
839 inode->i_ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
840 xfs_diflags_to_iflags(inode, ip);
841 xfs_iflags_clear(ip, XFS_IMODIFIED);
843 switch (inode->i_mode & S_IFMT) {
845 inode->i_op = &xfs_inode_operations;
846 inode->i_fop = &xfs_file_operations;
847 inode->i_mapping->a_ops = &xfs_address_space_operations;
850 if (xfs_sb_version_hasasciici(&XFS_M(inode->i_sb)->m_sb))
851 inode->i_op = &xfs_dir_ci_inode_operations;
853 inode->i_op = &xfs_dir_inode_operations;
854 inode->i_fop = &xfs_dir_file_operations;
857 inode->i_op = &xfs_symlink_inode_operations;
858 if (!(ip->i_df.if_flags & XFS_IFINLINE))
859 inode->i_mapping->a_ops = &xfs_address_space_operations;
862 inode->i_op = &xfs_inode_operations;
863 init_special_inode(inode, inode->i_mode, inode->i_rdev);
867 xfs_iflags_clear(ip, XFS_INEW);
870 unlock_new_inode(inode);