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 * Hook in SELinux. This is not quite correct yet, what we really need
137 * here (as we do for default ACLs) is a mechanism by which creation of
138 * these attrs can be journalled at inode creation time (along with the
139 * inode, of course, such that log replay can't cause these to be lost).
146 struct xfs_inode *ip = XFS_I(inode);
152 error = security_inode_init_security(inode, dir, &name,
155 if (error == -EOPNOTSUPP)
160 error = xfs_attr_set(ip, name, value, length, ATTR_SECURE);
162 xfs_iflags_set(ip, XFS_IMODIFIED);
171 struct xfs_name *namep,
172 struct dentry *dentry)
174 namep->name = dentry->d_name.name;
175 namep->len = dentry->d_name.len;
182 struct dentry *dentry)
184 struct xfs_name teardown;
187 * If we can't add the ACL or we fail in
188 * xfs_init_security we must back out.
189 * ENOSPC can hit here, among other things.
191 xfs_dentry_to_name(&teardown, dentry);
193 xfs_remove(XFS_I(dir), &teardown, XFS_I(inode));
200 struct dentry *dentry,
205 struct xfs_inode *ip = NULL;
206 xfs_acl_t *default_acl = NULL;
207 struct xfs_name name;
208 int (*test_default_acl)(struct inode *) = _ACL_DEFAULT_EXISTS;
212 * Irix uses Missed'em'V split, but doesn't want to see
213 * the upper 5 bits of (14bit) major.
215 if (unlikely(!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff))
218 if (test_default_acl && test_default_acl(dir)) {
219 if (!_ACL_ALLOC(default_acl)) {
222 if (!_ACL_GET_DEFAULT(dir, default_acl)) {
223 _ACL_FREE(default_acl);
228 xfs_dentry_to_name(&name, dentry);
230 if (IS_POSIXACL(dir) && !default_acl)
231 mode &= ~current->fs->umask;
233 switch (mode & S_IFMT) {
238 rdev = sysv_encode_dev(rdev);
240 error = xfs_create(XFS_I(dir), &name, mode, rdev, &ip, NULL);
243 error = xfs_mkdir(XFS_I(dir), &name, mode, &ip, NULL);
255 error = xfs_init_security(inode, dir);
257 goto out_cleanup_inode;
260 error = _ACL_INHERIT(inode, mode, default_acl);
262 goto out_cleanup_inode;
263 xfs_iflags_set(ip, XFS_IMODIFIED);
264 _ACL_FREE(default_acl);
268 d_instantiate(dentry, inode);
272 xfs_cleanup_inode(dir, inode, dentry);
275 _ACL_FREE(default_acl);
282 struct dentry *dentry,
284 struct nameidata *nd)
286 return xfs_vn_mknod(dir, dentry, mode, 0);
292 struct dentry *dentry,
295 return xfs_vn_mknod(dir, dentry, mode|S_IFDIR, 0);
298 STATIC struct dentry *
301 struct dentry *dentry,
302 struct nameidata *nd)
304 struct xfs_inode *cip;
305 struct xfs_name name;
308 if (dentry->d_name.len >= MAXNAMELEN)
309 return ERR_PTR(-ENAMETOOLONG);
311 xfs_dentry_to_name(&name, dentry);
312 error = xfs_lookup(XFS_I(dir), &name, &cip, NULL);
313 if (unlikely(error)) {
314 if (unlikely(error != ENOENT))
315 return ERR_PTR(-error);
320 return d_splice_alias(VFS_I(cip), dentry);
323 STATIC struct dentry *
326 struct dentry *dentry,
327 struct nameidata *nd)
329 struct xfs_inode *ip;
330 struct xfs_name xname;
331 struct xfs_name ci_name;
335 if (dentry->d_name.len >= MAXNAMELEN)
336 return ERR_PTR(-ENAMETOOLONG);
338 xfs_dentry_to_name(&xname, dentry);
339 error = xfs_lookup(XFS_I(dir), &xname, &ip, &ci_name);
340 if (unlikely(error)) {
341 if (unlikely(error != ENOENT))
342 return ERR_PTR(-error);
344 * call d_add(dentry, NULL) here when d_drop_negative_children
345 * is called in xfs_vn_mknod (ie. allow negative dentries
346 * with CI filesystems).
351 /* if exact match, just splice and exit */
353 return d_splice_alias(VFS_I(ip), dentry);
355 /* else case-insensitive match... */
356 dname.name = ci_name.name;
357 dname.len = ci_name.len;
358 dentry = d_add_ci(VFS_I(ip), dentry, &dname);
359 kmem_free(ci_name.name);
365 struct dentry *old_dentry,
367 struct dentry *dentry)
369 struct inode *inode; /* inode of guy being linked to */
370 struct xfs_name name;
373 inode = old_dentry->d_inode;
374 xfs_dentry_to_name(&name, dentry);
377 error = xfs_link(XFS_I(dir), XFS_I(inode), &name);
378 if (unlikely(error)) {
383 xfs_iflags_set(XFS_I(dir), XFS_IMODIFIED);
384 d_instantiate(dentry, inode);
391 struct dentry *dentry)
393 struct xfs_name name;
396 xfs_dentry_to_name(&name, dentry);
398 error = -xfs_remove(XFS_I(dir), &name, XFS_I(dentry->d_inode));
403 * With unlink, the VFS makes the dentry "negative": no inode,
404 * but still hashed. This is incompatible with case-insensitive
405 * mode, so invalidate (unhash) the dentry in CI-mode.
407 if (xfs_sb_version_hasasciici(&XFS_M(dir->i_sb)->m_sb))
408 d_invalidate(dentry);
415 struct dentry *dentry,
419 struct xfs_inode *cip = NULL;
420 struct xfs_name name;
425 (irix_symlink_mode ? 0777 & ~current->fs->umask : S_IRWXUGO);
426 xfs_dentry_to_name(&name, dentry);
428 error = xfs_symlink(XFS_I(dir), &name, symname, mode, &cip, NULL);
434 error = xfs_init_security(inode, dir);
436 goto out_cleanup_inode;
438 d_instantiate(dentry, inode);
442 xfs_cleanup_inode(dir, inode, dentry);
450 struct dentry *odentry,
452 struct dentry *ndentry)
454 struct inode *new_inode = ndentry->d_inode;
455 struct xfs_name oname;
456 struct xfs_name nname;
458 xfs_dentry_to_name(&oname, odentry);
459 xfs_dentry_to_name(&nname, ndentry);
461 return -xfs_rename(XFS_I(odir), &oname, XFS_I(odentry->d_inode),
462 XFS_I(ndir), &nname, new_inode ?
463 XFS_I(new_inode) : NULL);
467 * careful here - this function can get called recursively, so
468 * we need to be very careful about how much stack we use.
469 * uio is kmalloced for this reason...
473 struct dentry *dentry,
474 struct nameidata *nd)
479 link = kmalloc(MAXPATHLEN+1, GFP_KERNEL);
483 error = -xfs_readlink(XFS_I(dentry->d_inode), link);
487 nd_set_link(nd, link);
493 nd_set_link(nd, ERR_PTR(error));
499 struct dentry *dentry,
500 struct nameidata *nd,
503 char *s = nd_get_link(nd);
509 #ifdef CONFIG_XFS_POSIX_ACL
515 struct xfs_inode *ip = XFS_I(inode);
518 xfs_itrace_entry(ip);
520 if (XFS_IFORK_Q(ip)) {
521 error = xfs_acl_iaccess(ip, mask, NULL);
534 return generic_permission(inode, mask, xfs_check_acl);
537 #define xfs_vn_permission NULL
542 struct vfsmount *mnt,
543 struct dentry *dentry,
546 struct inode *inode = dentry->d_inode;
547 struct xfs_inode *ip = XFS_I(inode);
548 struct xfs_mount *mp = ip->i_mount;
550 xfs_itrace_entry(ip);
552 if (XFS_FORCED_SHUTDOWN(mp))
553 return XFS_ERROR(EIO);
555 stat->size = XFS_ISIZE(ip);
556 stat->dev = inode->i_sb->s_dev;
557 stat->mode = ip->i_d.di_mode;
558 stat->nlink = ip->i_d.di_nlink;
559 stat->uid = ip->i_d.di_uid;
560 stat->gid = ip->i_d.di_gid;
561 stat->ino = ip->i_ino;
563 stat->ino += mp->m_inoadd;
565 stat->atime = inode->i_atime;
566 stat->mtime.tv_sec = ip->i_d.di_mtime.t_sec;
567 stat->mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
568 stat->ctime.tv_sec = ip->i_d.di_ctime.t_sec;
569 stat->ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
571 XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
574 switch (inode->i_mode & S_IFMT) {
577 stat->blksize = BLKDEV_IOSIZE;
578 stat->rdev = MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
579 sysv_minor(ip->i_df.if_u2.if_rdev));
582 if (XFS_IS_REALTIME_INODE(ip)) {
584 * If the file blocks are being allocated from a
585 * realtime volume, then return the inode's realtime
586 * extent size or the realtime volume's extent size.
589 xfs_get_extsz_hint(ip) << mp->m_sb.sb_blocklog;
591 stat->blksize = xfs_preferred_iosize(mp);
601 struct dentry *dentry,
604 return -xfs_setattr(XFS_I(dentry->d_inode), iattr, 0, NULL);
608 * block_truncate_page can return an error, but we can't propagate it
609 * at all here. Leave a complaint + stack trace in the syslog because
610 * this could be bad. If it is bad, we need to propagate the error further.
617 error = block_truncate_page(inode->i_mapping, inode->i_size,
632 xfs_inode_t *ip = XFS_I(inode);
634 /* preallocation on directories not yet supported */
636 if (S_ISDIR(inode->i_mode))
643 xfs_ilock(ip, XFS_IOLOCK_EXCL);
644 error = xfs_change_file_space(ip, XFS_IOC_RESVSP, &bf,
645 0, NULL, XFS_ATTR_NOLOCK);
646 if (!error && !(mode & FALLOC_FL_KEEP_SIZE) &&
647 offset + len > i_size_read(inode))
648 new_size = offset + len;
650 /* Change file size if needed */
654 iattr.ia_valid = ATTR_SIZE;
655 iattr.ia_size = new_size;
656 error = xfs_setattr(ip, &iattr, XFS_ATTR_NOLOCK, NULL);
659 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
664 static const struct inode_operations xfs_inode_operations = {
665 .permission = xfs_vn_permission,
666 .truncate = xfs_vn_truncate,
667 .getattr = xfs_vn_getattr,
668 .setattr = xfs_vn_setattr,
669 .setxattr = generic_setxattr,
670 .getxattr = generic_getxattr,
671 .removexattr = generic_removexattr,
672 .listxattr = xfs_vn_listxattr,
673 .fallocate = xfs_vn_fallocate,
676 static const struct inode_operations xfs_dir_inode_operations = {
677 .create = xfs_vn_create,
678 .lookup = xfs_vn_lookup,
680 .unlink = xfs_vn_unlink,
681 .symlink = xfs_vn_symlink,
682 .mkdir = xfs_vn_mkdir,
684 * Yes, XFS uses the same method for rmdir and unlink.
686 * There are some subtile differences deeper in the code,
687 * but we use S_ISDIR to check for those.
689 .rmdir = xfs_vn_unlink,
690 .mknod = xfs_vn_mknod,
691 .rename = xfs_vn_rename,
692 .permission = xfs_vn_permission,
693 .getattr = xfs_vn_getattr,
694 .setattr = xfs_vn_setattr,
695 .setxattr = generic_setxattr,
696 .getxattr = generic_getxattr,
697 .removexattr = generic_removexattr,
698 .listxattr = xfs_vn_listxattr,
701 static const struct inode_operations xfs_dir_ci_inode_operations = {
702 .create = xfs_vn_create,
703 .lookup = xfs_vn_ci_lookup,
705 .unlink = xfs_vn_unlink,
706 .symlink = xfs_vn_symlink,
707 .mkdir = xfs_vn_mkdir,
709 * Yes, XFS uses the same method for rmdir and unlink.
711 * There are some subtile differences deeper in the code,
712 * but we use S_ISDIR to check for those.
714 .rmdir = xfs_vn_unlink,
715 .mknod = xfs_vn_mknod,
716 .rename = xfs_vn_rename,
717 .permission = xfs_vn_permission,
718 .getattr = xfs_vn_getattr,
719 .setattr = xfs_vn_setattr,
720 .setxattr = generic_setxattr,
721 .getxattr = generic_getxattr,
722 .removexattr = generic_removexattr,
723 .listxattr = xfs_vn_listxattr,
726 static const struct inode_operations xfs_symlink_inode_operations = {
727 .readlink = generic_readlink,
728 .follow_link = xfs_vn_follow_link,
729 .put_link = xfs_vn_put_link,
730 .permission = xfs_vn_permission,
731 .getattr = xfs_vn_getattr,
732 .setattr = xfs_vn_setattr,
733 .setxattr = generic_setxattr,
734 .getxattr = generic_getxattr,
735 .removexattr = generic_removexattr,
736 .listxattr = xfs_vn_listxattr,
740 xfs_diflags_to_iflags(
742 struct xfs_inode *ip)
744 if (ip->i_d.di_flags & XFS_DIFLAG_IMMUTABLE)
745 inode->i_flags |= S_IMMUTABLE;
747 inode->i_flags &= ~S_IMMUTABLE;
748 if (ip->i_d.di_flags & XFS_DIFLAG_APPEND)
749 inode->i_flags |= S_APPEND;
751 inode->i_flags &= ~S_APPEND;
752 if (ip->i_d.di_flags & XFS_DIFLAG_SYNC)
753 inode->i_flags |= S_SYNC;
755 inode->i_flags &= ~S_SYNC;
756 if (ip->i_d.di_flags & XFS_DIFLAG_NOATIME)
757 inode->i_flags |= S_NOATIME;
759 inode->i_flags &= ~S_NOATIME;
763 * Initialize the Linux inode, set up the operation vectors and
766 * When reading existing inodes from disk this is called directly
767 * from xfs_iget, when creating a new inode it is called from
768 * xfs_ialloc after setting up the inode.
772 struct xfs_inode *ip)
774 struct inode *inode = ip->i_vnode;
776 inode->i_mode = ip->i_d.di_mode;
777 inode->i_nlink = ip->i_d.di_nlink;
778 inode->i_uid = ip->i_d.di_uid;
779 inode->i_gid = ip->i_d.di_gid;
781 switch (inode->i_mode & S_IFMT) {
785 MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
786 sysv_minor(ip->i_df.if_u2.if_rdev));
793 inode->i_generation = ip->i_d.di_gen;
794 i_size_write(inode, ip->i_d.di_size);
795 inode->i_atime.tv_sec = ip->i_d.di_atime.t_sec;
796 inode->i_atime.tv_nsec = ip->i_d.di_atime.t_nsec;
797 inode->i_mtime.tv_sec = ip->i_d.di_mtime.t_sec;
798 inode->i_mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
799 inode->i_ctime.tv_sec = ip->i_d.di_ctime.t_sec;
800 inode->i_ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
801 xfs_diflags_to_iflags(inode, ip);
802 xfs_iflags_clear(ip, XFS_IMODIFIED);
804 switch (inode->i_mode & S_IFMT) {
806 inode->i_op = &xfs_inode_operations;
807 inode->i_fop = &xfs_file_operations;
808 inode->i_mapping->a_ops = &xfs_address_space_operations;
811 if (xfs_sb_version_hasasciici(&XFS_M(inode->i_sb)->m_sb))
812 inode->i_op = &xfs_dir_ci_inode_operations;
814 inode->i_op = &xfs_dir_inode_operations;
815 inode->i_fop = &xfs_dir_file_operations;
818 inode->i_op = &xfs_symlink_inode_operations;
819 if (!(ip->i_df.if_flags & XFS_IFINLINE))
820 inode->i_mapping->a_ops = &xfs_address_space_operations;
823 inode->i_op = &xfs_inode_operations;
824 init_special_inode(inode, inode->i_mode, inode->i_rdev);
828 xfs_iflags_clear(ip, XFS_INEW);
831 unlock_new_inode(inode);