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 if (flags & XFS_ICHGTIME_MOD) {
104 ip->i_d.di_mtime.t_sec = (__int32_t)tv.tv_sec;
105 ip->i_d.di_mtime.t_nsec = (__int32_t)tv.tv_nsec;
107 if (flags & XFS_ICHGTIME_CHG) {
109 ip->i_d.di_ctime.t_sec = (__int32_t)tv.tv_sec;
110 ip->i_d.di_ctime.t_nsec = (__int32_t)tv.tv_nsec;
114 * We update the i_update_core field _after_ changing
115 * the timestamps in order to coordinate properly with
116 * xfs_iflush() so that we don't lose timestamp updates.
117 * This keeps us from having to hold the inode lock
118 * while doing this. We use the SYNCHRONIZE macro to
119 * ensure that the compiler does not reorder the update
120 * of i_update_core above the timestamp updates above.
123 ip->i_update_core = 1;
124 if (!(inode->i_state & I_NEW))
125 mark_inode_dirty_sync(inode);
129 * Variant on the above which avoids querying the system clock
130 * in situations where we know the Linux inode timestamps have
131 * just been updated (and so we can update our inode cheaply).
141 if (flags & XFS_ICHGTIME_MOD) {
142 tvp = &inode->i_mtime;
143 ip->i_d.di_mtime.t_sec = (__int32_t)tvp->tv_sec;
144 ip->i_d.di_mtime.t_nsec = (__int32_t)tvp->tv_nsec;
146 if (flags & XFS_ICHGTIME_CHG) {
147 tvp = &inode->i_ctime;
148 ip->i_d.di_ctime.t_sec = (__int32_t)tvp->tv_sec;
149 ip->i_d.di_ctime.t_nsec = (__int32_t)tvp->tv_nsec;
153 * We update the i_update_core field _after_ changing
154 * the timestamps in order to coordinate properly with
155 * xfs_iflush() so that we don't lose timestamp updates.
156 * This keeps us from having to hold the inode lock
157 * while doing this. We use the SYNCHRONIZE macro to
158 * ensure that the compiler does not reorder the update
159 * of i_update_core above the timestamp updates above.
162 ip->i_update_core = 1;
163 if (!(inode->i_state & I_NEW))
164 mark_inode_dirty_sync(inode);
168 * Hook in SELinux. This is not quite correct yet, what we really need
169 * here (as we do for default ACLs) is a mechanism by which creation of
170 * these attrs can be journalled at inode creation time (along with the
171 * inode, of course, such that log replay can't cause these to be lost).
178 struct xfs_inode *ip = XFS_I(inode);
184 error = security_inode_init_security(inode, dir, &name,
187 if (error == -EOPNOTSUPP)
192 error = xfs_attr_set(ip, name, value, length, ATTR_SECURE);
194 xfs_iflags_set(ip, XFS_IMODIFIED);
203 struct xfs_name *namep,
204 struct dentry *dentry)
206 namep->name = dentry->d_name.name;
207 namep->len = dentry->d_name.len;
214 struct dentry *dentry)
216 struct xfs_name teardown;
219 * If we can't add the ACL or we fail in
220 * xfs_init_security we must back out.
221 * ENOSPC can hit here, among other things.
223 xfs_dentry_to_name(&teardown, dentry);
225 xfs_remove(XFS_I(dir), &teardown, XFS_I(inode));
232 struct dentry *dentry,
237 struct xfs_inode *ip = NULL;
238 xfs_acl_t *default_acl = NULL;
239 struct xfs_name name;
240 int (*test_default_acl)(struct inode *) = _ACL_DEFAULT_EXISTS;
244 * Irix uses Missed'em'V split, but doesn't want to see
245 * the upper 5 bits of (14bit) major.
247 if (unlikely(!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff))
250 if (test_default_acl && test_default_acl(dir)) {
251 if (!_ACL_ALLOC(default_acl)) {
254 if (!_ACL_GET_DEFAULT(dir, default_acl)) {
255 _ACL_FREE(default_acl);
260 xfs_dentry_to_name(&name, dentry);
262 if (IS_POSIXACL(dir) && !default_acl)
263 mode &= ~current->fs->umask;
265 switch (mode & S_IFMT) {
270 rdev = sysv_encode_dev(rdev);
272 error = xfs_create(XFS_I(dir), &name, mode, rdev, &ip, NULL);
275 error = xfs_mkdir(XFS_I(dir), &name, mode, &ip, NULL);
287 error = xfs_init_security(inode, dir);
289 goto out_cleanup_inode;
292 error = _ACL_INHERIT(inode, mode, default_acl);
294 goto out_cleanup_inode;
295 xfs_iflags_set(ip, XFS_IMODIFIED);
296 _ACL_FREE(default_acl);
300 d_instantiate(dentry, inode);
304 xfs_cleanup_inode(dir, inode, dentry);
307 _ACL_FREE(default_acl);
314 struct dentry *dentry,
316 struct nameidata *nd)
318 return xfs_vn_mknod(dir, dentry, mode, 0);
324 struct dentry *dentry,
327 return xfs_vn_mknod(dir, dentry, mode|S_IFDIR, 0);
330 STATIC struct dentry *
333 struct dentry *dentry,
334 struct nameidata *nd)
336 struct xfs_inode *cip;
337 struct xfs_name name;
340 if (dentry->d_name.len >= MAXNAMELEN)
341 return ERR_PTR(-ENAMETOOLONG);
343 xfs_dentry_to_name(&name, dentry);
344 error = xfs_lookup(XFS_I(dir), &name, &cip, NULL);
345 if (unlikely(error)) {
346 if (unlikely(error != ENOENT))
347 return ERR_PTR(-error);
352 return d_splice_alias(VFS_I(cip), dentry);
355 STATIC struct dentry *
358 struct dentry *dentry,
359 struct nameidata *nd)
361 struct xfs_inode *ip;
362 struct xfs_name xname;
363 struct xfs_name ci_name;
367 if (dentry->d_name.len >= MAXNAMELEN)
368 return ERR_PTR(-ENAMETOOLONG);
370 xfs_dentry_to_name(&xname, dentry);
371 error = xfs_lookup(XFS_I(dir), &xname, &ip, &ci_name);
372 if (unlikely(error)) {
373 if (unlikely(error != ENOENT))
374 return ERR_PTR(-error);
376 * call d_add(dentry, NULL) here when d_drop_negative_children
377 * is called in xfs_vn_mknod (ie. allow negative dentries
378 * with CI filesystems).
383 /* if exact match, just splice and exit */
385 return d_splice_alias(VFS_I(ip), dentry);
387 /* else case-insensitive match... */
388 dname.name = ci_name.name;
389 dname.len = ci_name.len;
390 dentry = d_add_ci(VFS_I(ip), dentry, &dname);
391 kmem_free(ci_name.name);
397 struct dentry *old_dentry,
399 struct dentry *dentry)
401 struct inode *inode; /* inode of guy being linked to */
402 struct xfs_name name;
405 inode = old_dentry->d_inode;
406 xfs_dentry_to_name(&name, dentry);
409 error = xfs_link(XFS_I(dir), XFS_I(inode), &name);
410 if (unlikely(error)) {
415 xfs_iflags_set(XFS_I(dir), XFS_IMODIFIED);
416 d_instantiate(dentry, inode);
423 struct dentry *dentry)
425 struct xfs_name name;
428 xfs_dentry_to_name(&name, dentry);
430 error = -xfs_remove(XFS_I(dir), &name, XFS_I(dentry->d_inode));
435 * With unlink, the VFS makes the dentry "negative": no inode,
436 * but still hashed. This is incompatible with case-insensitive
437 * mode, so invalidate (unhash) the dentry in CI-mode.
439 if (xfs_sb_version_hasasciici(&XFS_M(dir->i_sb)->m_sb))
440 d_invalidate(dentry);
447 struct dentry *dentry,
451 struct xfs_inode *cip = NULL;
452 struct xfs_name name;
457 (irix_symlink_mode ? 0777 & ~current->fs->umask : S_IRWXUGO);
458 xfs_dentry_to_name(&name, dentry);
460 error = xfs_symlink(XFS_I(dir), &name, symname, mode, &cip, NULL);
466 error = xfs_init_security(inode, dir);
468 goto out_cleanup_inode;
470 d_instantiate(dentry, inode);
474 xfs_cleanup_inode(dir, inode, dentry);
482 struct dentry *odentry,
484 struct dentry *ndentry)
486 struct inode *new_inode = ndentry->d_inode;
487 struct xfs_name oname;
488 struct xfs_name nname;
490 xfs_dentry_to_name(&oname, odentry);
491 xfs_dentry_to_name(&nname, ndentry);
493 return -xfs_rename(XFS_I(odir), &oname, XFS_I(odentry->d_inode),
494 XFS_I(ndir), &nname, new_inode ?
495 XFS_I(new_inode) : NULL);
499 * careful here - this function can get called recursively, so
500 * we need to be very careful about how much stack we use.
501 * uio is kmalloced for this reason...
505 struct dentry *dentry,
506 struct nameidata *nd)
511 link = kmalloc(MAXPATHLEN+1, GFP_KERNEL);
515 error = -xfs_readlink(XFS_I(dentry->d_inode), link);
519 nd_set_link(nd, link);
525 nd_set_link(nd, ERR_PTR(error));
531 struct dentry *dentry,
532 struct nameidata *nd,
535 char *s = nd_get_link(nd);
541 #ifdef CONFIG_XFS_POSIX_ACL
547 struct xfs_inode *ip = XFS_I(inode);
550 xfs_itrace_entry(ip);
552 if (XFS_IFORK_Q(ip)) {
553 error = xfs_acl_iaccess(ip, mask, NULL);
566 return generic_permission(inode, mask, xfs_check_acl);
569 #define xfs_vn_permission NULL
574 struct vfsmount *mnt,
575 struct dentry *dentry,
578 struct inode *inode = dentry->d_inode;
579 struct xfs_inode *ip = XFS_I(inode);
580 struct xfs_mount *mp = ip->i_mount;
582 xfs_itrace_entry(ip);
584 if (XFS_FORCED_SHUTDOWN(mp))
585 return XFS_ERROR(EIO);
587 stat->size = XFS_ISIZE(ip);
588 stat->dev = inode->i_sb->s_dev;
589 stat->mode = ip->i_d.di_mode;
590 stat->nlink = ip->i_d.di_nlink;
591 stat->uid = ip->i_d.di_uid;
592 stat->gid = ip->i_d.di_gid;
593 stat->ino = ip->i_ino;
595 stat->ino += mp->m_inoadd;
597 stat->atime = inode->i_atime;
598 stat->mtime.tv_sec = ip->i_d.di_mtime.t_sec;
599 stat->mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
600 stat->ctime.tv_sec = ip->i_d.di_ctime.t_sec;
601 stat->ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
603 XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
606 switch (inode->i_mode & S_IFMT) {
609 stat->blksize = BLKDEV_IOSIZE;
610 stat->rdev = MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
611 sysv_minor(ip->i_df.if_u2.if_rdev));
614 if (XFS_IS_REALTIME_INODE(ip)) {
616 * If the file blocks are being allocated from a
617 * realtime volume, then return the inode's realtime
618 * extent size or the realtime volume's extent size.
621 xfs_get_extsz_hint(ip) << mp->m_sb.sb_blocklog;
623 stat->blksize = xfs_preferred_iosize(mp);
633 struct dentry *dentry,
636 return -xfs_setattr(XFS_I(dentry->d_inode), iattr, 0, NULL);
640 * block_truncate_page can return an error, but we can't propagate it
641 * at all here. Leave a complaint + stack trace in the syslog because
642 * this could be bad. If it is bad, we need to propagate the error further.
649 error = block_truncate_page(inode->i_mapping, inode->i_size,
664 xfs_inode_t *ip = XFS_I(inode);
666 /* preallocation on directories not yet supported */
668 if (S_ISDIR(inode->i_mode))
675 xfs_ilock(ip, XFS_IOLOCK_EXCL);
676 error = xfs_change_file_space(ip, XFS_IOC_RESVSP, &bf,
677 0, NULL, XFS_ATTR_NOLOCK);
678 if (!error && !(mode & FALLOC_FL_KEEP_SIZE) &&
679 offset + len > i_size_read(inode))
680 new_size = offset + len;
682 /* Change file size if needed */
686 iattr.ia_valid = ATTR_SIZE;
687 iattr.ia_size = new_size;
688 error = xfs_setattr(ip, &iattr, XFS_ATTR_NOLOCK, NULL);
691 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
696 static const struct inode_operations xfs_inode_operations = {
697 .permission = xfs_vn_permission,
698 .truncate = xfs_vn_truncate,
699 .getattr = xfs_vn_getattr,
700 .setattr = xfs_vn_setattr,
701 .setxattr = generic_setxattr,
702 .getxattr = generic_getxattr,
703 .removexattr = generic_removexattr,
704 .listxattr = xfs_vn_listxattr,
705 .fallocate = xfs_vn_fallocate,
708 static const struct inode_operations xfs_dir_inode_operations = {
709 .create = xfs_vn_create,
710 .lookup = xfs_vn_lookup,
712 .unlink = xfs_vn_unlink,
713 .symlink = xfs_vn_symlink,
714 .mkdir = xfs_vn_mkdir,
716 * Yes, XFS uses the same method for rmdir and unlink.
718 * There are some subtile differences deeper in the code,
719 * but we use S_ISDIR to check for those.
721 .rmdir = xfs_vn_unlink,
722 .mknod = xfs_vn_mknod,
723 .rename = xfs_vn_rename,
724 .permission = xfs_vn_permission,
725 .getattr = xfs_vn_getattr,
726 .setattr = xfs_vn_setattr,
727 .setxattr = generic_setxattr,
728 .getxattr = generic_getxattr,
729 .removexattr = generic_removexattr,
730 .listxattr = xfs_vn_listxattr,
733 static const struct inode_operations xfs_dir_ci_inode_operations = {
734 .create = xfs_vn_create,
735 .lookup = xfs_vn_ci_lookup,
737 .unlink = xfs_vn_unlink,
738 .symlink = xfs_vn_symlink,
739 .mkdir = xfs_vn_mkdir,
741 * Yes, XFS uses the same method for rmdir and unlink.
743 * There are some subtile differences deeper in the code,
744 * but we use S_ISDIR to check for those.
746 .rmdir = xfs_vn_unlink,
747 .mknod = xfs_vn_mknod,
748 .rename = xfs_vn_rename,
749 .permission = xfs_vn_permission,
750 .getattr = xfs_vn_getattr,
751 .setattr = xfs_vn_setattr,
752 .setxattr = generic_setxattr,
753 .getxattr = generic_getxattr,
754 .removexattr = generic_removexattr,
755 .listxattr = xfs_vn_listxattr,
758 static const struct inode_operations xfs_symlink_inode_operations = {
759 .readlink = generic_readlink,
760 .follow_link = xfs_vn_follow_link,
761 .put_link = xfs_vn_put_link,
762 .permission = xfs_vn_permission,
763 .getattr = xfs_vn_getattr,
764 .setattr = xfs_vn_setattr,
765 .setxattr = generic_setxattr,
766 .getxattr = generic_getxattr,
767 .removexattr = generic_removexattr,
768 .listxattr = xfs_vn_listxattr,
772 xfs_diflags_to_iflags(
774 struct xfs_inode *ip)
776 if (ip->i_d.di_flags & XFS_DIFLAG_IMMUTABLE)
777 inode->i_flags |= S_IMMUTABLE;
779 inode->i_flags &= ~S_IMMUTABLE;
780 if (ip->i_d.di_flags & XFS_DIFLAG_APPEND)
781 inode->i_flags |= S_APPEND;
783 inode->i_flags &= ~S_APPEND;
784 if (ip->i_d.di_flags & XFS_DIFLAG_SYNC)
785 inode->i_flags |= S_SYNC;
787 inode->i_flags &= ~S_SYNC;
788 if (ip->i_d.di_flags & XFS_DIFLAG_NOATIME)
789 inode->i_flags |= S_NOATIME;
791 inode->i_flags &= ~S_NOATIME;
795 * Initialize the Linux inode, set up the operation vectors and
798 * When reading existing inodes from disk this is called directly
799 * from xfs_iget, when creating a new inode it is called from
800 * xfs_ialloc after setting up the inode.
804 struct xfs_inode *ip)
806 struct inode *inode = ip->i_vnode;
808 inode->i_mode = ip->i_d.di_mode;
809 inode->i_nlink = ip->i_d.di_nlink;
810 inode->i_uid = ip->i_d.di_uid;
811 inode->i_gid = ip->i_d.di_gid;
813 switch (inode->i_mode & S_IFMT) {
817 MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
818 sysv_minor(ip->i_df.if_u2.if_rdev));
825 inode->i_generation = ip->i_d.di_gen;
826 i_size_write(inode, ip->i_d.di_size);
827 inode->i_atime.tv_sec = ip->i_d.di_atime.t_sec;
828 inode->i_atime.tv_nsec = ip->i_d.di_atime.t_nsec;
829 inode->i_mtime.tv_sec = ip->i_d.di_mtime.t_sec;
830 inode->i_mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
831 inode->i_ctime.tv_sec = ip->i_d.di_ctime.t_sec;
832 inode->i_ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
833 xfs_diflags_to_iflags(inode, ip);
834 xfs_iflags_clear(ip, XFS_IMODIFIED);
836 switch (inode->i_mode & S_IFMT) {
838 inode->i_op = &xfs_inode_operations;
839 inode->i_fop = &xfs_file_operations;
840 inode->i_mapping->a_ops = &xfs_address_space_operations;
843 if (xfs_sb_version_hasasciici(&XFS_M(inode->i_sb)->m_sb))
844 inode->i_op = &xfs_dir_ci_inode_operations;
846 inode->i_op = &xfs_dir_inode_operations;
847 inode->i_fop = &xfs_dir_file_operations;
850 inode->i_op = &xfs_symlink_inode_operations;
851 if (!(ip->i_df.if_flags & XFS_IFINLINE))
852 inode->i_mapping->a_ops = &xfs_address_space_operations;
855 inode->i_op = &xfs_inode_operations;
856 init_special_inode(inode, inode->i_mode, inode->i_rdev);
860 xfs_iflags_clear(ip, XFS_INEW);
863 unlock_new_inode(inode);