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 = ip->i_vnode;
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 = ip->i_vnode;
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.
93 * NOTE -- callers MUST combine XFS_ICHGTIME_MOD or XFS_ICHGTIME_CHG
94 * with XFS_ICHGTIME_ACC to be sure that access time
95 * update will take. Calling first with XFS_ICHGTIME_ACC
96 * and then XFS_ICHGTIME_MOD may fail to modify the access
97 * timestamp if the filesystem is mounted noacctm.
104 struct inode *inode = vn_to_inode(XFS_ITOV(ip));
108 if (flags & XFS_ICHGTIME_MOD) {
110 ip->i_d.di_mtime.t_sec = (__int32_t)tv.tv_sec;
111 ip->i_d.di_mtime.t_nsec = (__int32_t)tv.tv_nsec;
113 if (flags & XFS_ICHGTIME_ACC) {
115 ip->i_d.di_atime.t_sec = (__int32_t)tv.tv_sec;
116 ip->i_d.di_atime.t_nsec = (__int32_t)tv.tv_nsec;
118 if (flags & XFS_ICHGTIME_CHG) {
120 ip->i_d.di_ctime.t_sec = (__int32_t)tv.tv_sec;
121 ip->i_d.di_ctime.t_nsec = (__int32_t)tv.tv_nsec;
125 * We update the i_update_core field _after_ changing
126 * the timestamps in order to coordinate properly with
127 * xfs_iflush() so that we don't lose timestamp updates.
128 * This keeps us from having to hold the inode lock
129 * while doing this. We use the SYNCHRONIZE macro to
130 * ensure that the compiler does not reorder the update
131 * of i_update_core above the timestamp updates above.
134 ip->i_update_core = 1;
135 if (!(inode->i_state & I_NEW))
136 mark_inode_dirty_sync(inode);
140 * Variant on the above which avoids querying the system clock
141 * in situations where we know the Linux inode timestamps have
142 * just been updated (and so we can update our inode cheaply).
153 * Atime updates for read() & friends are handled lazily now, and
154 * explicit updates must go through xfs_ichgtime()
156 ASSERT((flags & XFS_ICHGTIME_ACC) == 0);
158 if (flags & XFS_ICHGTIME_MOD) {
159 tvp = &inode->i_mtime;
160 ip->i_d.di_mtime.t_sec = (__int32_t)tvp->tv_sec;
161 ip->i_d.di_mtime.t_nsec = (__int32_t)tvp->tv_nsec;
163 if (flags & XFS_ICHGTIME_CHG) {
164 tvp = &inode->i_ctime;
165 ip->i_d.di_ctime.t_sec = (__int32_t)tvp->tv_sec;
166 ip->i_d.di_ctime.t_nsec = (__int32_t)tvp->tv_nsec;
170 * We update the i_update_core field _after_ changing
171 * the timestamps in order to coordinate properly with
172 * xfs_iflush() so that we don't lose timestamp updates.
173 * This keeps us from having to hold the inode lock
174 * while doing this. We use the SYNCHRONIZE macro to
175 * ensure that the compiler does not reorder the update
176 * of i_update_core above the timestamp updates above.
179 ip->i_update_core = 1;
180 if (!(inode->i_state & I_NEW))
181 mark_inode_dirty_sync(inode);
185 * Hook in SELinux. This is not quite correct yet, what we really need
186 * here (as we do for default ACLs) is a mechanism by which creation of
187 * these attrs can be journalled at inode creation time (along with the
188 * inode, of course, such that log replay can't cause these to be lost).
195 struct xfs_inode *ip = XFS_I(inode);
201 error = security_inode_init_security(inode, dir, &name,
204 if (error == -EOPNOTSUPP)
209 error = xfs_attr_set(ip, name, value, length, ATTR_SECURE);
211 xfs_iflags_set(ip, XFS_IMODIFIED);
220 struct xfs_name *namep,
221 struct dentry *dentry)
223 namep->name = dentry->d_name.name;
224 namep->len = dentry->d_name.len;
231 struct dentry *dentry)
233 struct xfs_name teardown;
236 * If we can't add the ACL or we fail in
237 * xfs_init_security we must back out.
238 * ENOSPC can hit here, among other things.
240 xfs_dentry_to_name(&teardown, dentry);
242 xfs_remove(XFS_I(dir), &teardown, XFS_I(inode));
249 struct dentry *dentry,
254 struct xfs_inode *ip = NULL;
255 xfs_acl_t *default_acl = NULL;
256 struct xfs_name name;
257 int (*test_default_acl)(struct inode *) = _ACL_DEFAULT_EXISTS;
261 * Irix uses Missed'em'V split, but doesn't want to see
262 * the upper 5 bits of (14bit) major.
264 if (unlikely(!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff))
267 if (test_default_acl && test_default_acl(dir)) {
268 if (!_ACL_ALLOC(default_acl)) {
271 if (!_ACL_GET_DEFAULT(dir, default_acl)) {
272 _ACL_FREE(default_acl);
277 xfs_dentry_to_name(&name, dentry);
279 if (IS_POSIXACL(dir) && !default_acl)
280 mode &= ~current->fs->umask;
282 switch (mode & S_IFMT) {
287 rdev = sysv_encode_dev(rdev);
289 error = xfs_create(XFS_I(dir), &name, mode, rdev, &ip, NULL);
292 error = xfs_mkdir(XFS_I(dir), &name, mode, &ip, NULL);
304 error = xfs_init_security(inode, dir);
306 goto out_cleanup_inode;
309 error = _ACL_INHERIT(inode, mode, default_acl);
311 goto out_cleanup_inode;
312 xfs_iflags_set(ip, XFS_IMODIFIED);
313 _ACL_FREE(default_acl);
317 d_instantiate(dentry, inode);
321 xfs_cleanup_inode(dir, inode, dentry);
324 _ACL_FREE(default_acl);
331 struct dentry *dentry,
333 struct nameidata *nd)
335 return xfs_vn_mknod(dir, dentry, mode, 0);
341 struct dentry *dentry,
344 return xfs_vn_mknod(dir, dentry, mode|S_IFDIR, 0);
347 STATIC struct dentry *
350 struct dentry *dentry,
351 struct nameidata *nd)
353 struct xfs_inode *cip;
354 struct xfs_name name;
357 if (dentry->d_name.len >= MAXNAMELEN)
358 return ERR_PTR(-ENAMETOOLONG);
360 xfs_dentry_to_name(&name, dentry);
361 error = xfs_lookup(XFS_I(dir), &name, &cip, NULL);
362 if (unlikely(error)) {
363 if (unlikely(error != ENOENT))
364 return ERR_PTR(-error);
369 return d_splice_alias(cip->i_vnode, dentry);
372 STATIC struct dentry *
375 struct dentry *dentry,
376 struct nameidata *nd)
378 struct xfs_inode *ip;
379 struct xfs_name xname;
380 struct xfs_name ci_name;
384 if (dentry->d_name.len >= MAXNAMELEN)
385 return ERR_PTR(-ENAMETOOLONG);
387 xfs_dentry_to_name(&xname, dentry);
388 error = xfs_lookup(XFS_I(dir), &xname, &ip, &ci_name);
389 if (unlikely(error)) {
390 if (unlikely(error != ENOENT))
391 return ERR_PTR(-error);
393 * call d_add(dentry, NULL) here when d_drop_negative_children
394 * is called in xfs_vn_mknod (ie. allow negative dentries
395 * with CI filesystems).
400 /* if exact match, just splice and exit */
402 return d_splice_alias(ip->i_vnode, dentry);
404 /* else case-insensitive match... */
405 dname.name = ci_name.name;
406 dname.len = ci_name.len;
407 dentry = d_add_ci(ip->i_vnode, dentry, &dname);
408 kmem_free(ci_name.name);
414 struct dentry *old_dentry,
416 struct dentry *dentry)
418 struct inode *inode; /* inode of guy being linked to */
419 struct xfs_name name;
422 inode = old_dentry->d_inode;
423 xfs_dentry_to_name(&name, dentry);
426 error = xfs_link(XFS_I(dir), XFS_I(inode), &name);
427 if (unlikely(error)) {
432 xfs_iflags_set(XFS_I(dir), XFS_IMODIFIED);
433 d_instantiate(dentry, inode);
440 struct dentry *dentry)
442 struct xfs_name name;
445 xfs_dentry_to_name(&name, dentry);
447 error = -xfs_remove(XFS_I(dir), &name, XFS_I(dentry->d_inode));
452 * With unlink, the VFS makes the dentry "negative": no inode,
453 * but still hashed. This is incompatible with case-insensitive
454 * mode, so invalidate (unhash) the dentry in CI-mode.
456 if (xfs_sb_version_hasasciici(&XFS_M(dir->i_sb)->m_sb))
457 d_invalidate(dentry);
464 struct dentry *dentry,
468 struct xfs_inode *cip = NULL;
469 struct xfs_name name;
474 (irix_symlink_mode ? 0777 & ~current->fs->umask : S_IRWXUGO);
475 xfs_dentry_to_name(&name, dentry);
477 error = xfs_symlink(XFS_I(dir), &name, symname, mode, &cip, NULL);
481 inode = cip->i_vnode;
483 error = xfs_init_security(inode, dir);
485 goto out_cleanup_inode;
487 d_instantiate(dentry, inode);
491 xfs_cleanup_inode(dir, inode, dentry);
499 struct dentry *odentry,
501 struct dentry *ndentry)
503 struct inode *new_inode = ndentry->d_inode;
504 struct xfs_name oname;
505 struct xfs_name nname;
507 xfs_dentry_to_name(&oname, odentry);
508 xfs_dentry_to_name(&nname, ndentry);
510 return -xfs_rename(XFS_I(odir), &oname, XFS_I(odentry->d_inode),
511 XFS_I(ndir), &nname, new_inode ?
512 XFS_I(new_inode) : NULL);
516 * careful here - this function can get called recursively, so
517 * we need to be very careful about how much stack we use.
518 * uio is kmalloced for this reason...
522 struct dentry *dentry,
523 struct nameidata *nd)
528 link = kmalloc(MAXPATHLEN+1, GFP_KERNEL);
532 error = -xfs_readlink(XFS_I(dentry->d_inode), link);
536 nd_set_link(nd, link);
542 nd_set_link(nd, ERR_PTR(error));
548 struct dentry *dentry,
549 struct nameidata *nd,
552 char *s = nd_get_link(nd);
558 #ifdef CONFIG_XFS_POSIX_ACL
564 struct xfs_inode *ip = XFS_I(inode);
567 xfs_itrace_entry(ip);
569 if (XFS_IFORK_Q(ip)) {
570 error = xfs_acl_iaccess(ip, mask, NULL);
583 return generic_permission(inode, mask, xfs_check_acl);
586 #define xfs_vn_permission NULL
591 struct vfsmount *mnt,
592 struct dentry *dentry,
595 struct inode *inode = dentry->d_inode;
596 struct xfs_inode *ip = XFS_I(inode);
597 struct xfs_mount *mp = ip->i_mount;
599 xfs_itrace_entry(ip);
601 if (XFS_FORCED_SHUTDOWN(mp))
602 return XFS_ERROR(EIO);
604 stat->size = XFS_ISIZE(ip);
605 stat->dev = inode->i_sb->s_dev;
606 stat->mode = ip->i_d.di_mode;
607 stat->nlink = ip->i_d.di_nlink;
608 stat->uid = ip->i_d.di_uid;
609 stat->gid = ip->i_d.di_gid;
610 stat->ino = ip->i_ino;
612 stat->ino += mp->m_inoadd;
614 stat->atime = inode->i_atime;
615 stat->mtime.tv_sec = ip->i_d.di_mtime.t_sec;
616 stat->mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
617 stat->ctime.tv_sec = ip->i_d.di_ctime.t_sec;
618 stat->ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
620 XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
623 switch (inode->i_mode & S_IFMT) {
626 stat->blksize = BLKDEV_IOSIZE;
627 stat->rdev = MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
628 sysv_minor(ip->i_df.if_u2.if_rdev));
631 if (XFS_IS_REALTIME_INODE(ip)) {
633 * If the file blocks are being allocated from a
634 * realtime volume, then return the inode's realtime
635 * extent size or the realtime volume's extent size.
638 xfs_get_extsz_hint(ip) << mp->m_sb.sb_blocklog;
640 stat->blksize = xfs_preferred_iosize(mp);
650 struct dentry *dentry,
653 return -xfs_setattr(XFS_I(dentry->d_inode), iattr, 0, NULL);
657 * block_truncate_page can return an error, but we can't propagate it
658 * at all here. Leave a complaint + stack trace in the syslog because
659 * this could be bad. If it is bad, we need to propagate the error further.
666 error = block_truncate_page(inode->i_mapping, inode->i_size,
681 xfs_inode_t *ip = XFS_I(inode);
683 /* preallocation on directories not yet supported */
685 if (S_ISDIR(inode->i_mode))
692 xfs_ilock(ip, XFS_IOLOCK_EXCL);
693 error = xfs_change_file_space(ip, XFS_IOC_RESVSP, &bf,
694 0, NULL, XFS_ATTR_NOLOCK);
695 if (!error && !(mode & FALLOC_FL_KEEP_SIZE) &&
696 offset + len > i_size_read(inode))
697 new_size = offset + len;
699 /* Change file size if needed */
703 iattr.ia_valid = ATTR_SIZE;
704 iattr.ia_size = new_size;
705 error = xfs_setattr(ip, &iattr, XFS_ATTR_NOLOCK, NULL);
708 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
713 const struct inode_operations xfs_inode_operations = {
714 .permission = xfs_vn_permission,
715 .truncate = xfs_vn_truncate,
716 .getattr = xfs_vn_getattr,
717 .setattr = xfs_vn_setattr,
718 .setxattr = generic_setxattr,
719 .getxattr = generic_getxattr,
720 .removexattr = generic_removexattr,
721 .listxattr = xfs_vn_listxattr,
722 .fallocate = xfs_vn_fallocate,
725 const struct inode_operations xfs_dir_inode_operations = {
726 .create = xfs_vn_create,
727 .lookup = xfs_vn_lookup,
729 .unlink = xfs_vn_unlink,
730 .symlink = xfs_vn_symlink,
731 .mkdir = xfs_vn_mkdir,
733 * Yes, XFS uses the same method for rmdir and unlink.
735 * There are some subtile differences deeper in the code,
736 * but we use S_ISDIR to check for those.
738 .rmdir = xfs_vn_unlink,
739 .mknod = xfs_vn_mknod,
740 .rename = xfs_vn_rename,
741 .permission = xfs_vn_permission,
742 .getattr = xfs_vn_getattr,
743 .setattr = xfs_vn_setattr,
744 .setxattr = generic_setxattr,
745 .getxattr = generic_getxattr,
746 .removexattr = generic_removexattr,
747 .listxattr = xfs_vn_listxattr,
750 const struct inode_operations xfs_dir_ci_inode_operations = {
751 .create = xfs_vn_create,
752 .lookup = xfs_vn_ci_lookup,
754 .unlink = xfs_vn_unlink,
755 .symlink = xfs_vn_symlink,
756 .mkdir = xfs_vn_mkdir,
758 * Yes, XFS uses the same method for rmdir and unlink.
760 * There are some subtile differences deeper in the code,
761 * but we use S_ISDIR to check for those.
763 .rmdir = xfs_vn_unlink,
764 .mknod = xfs_vn_mknod,
765 .rename = xfs_vn_rename,
766 .permission = xfs_vn_permission,
767 .getattr = xfs_vn_getattr,
768 .setattr = xfs_vn_setattr,
769 .setxattr = generic_setxattr,
770 .getxattr = generic_getxattr,
771 .removexattr = generic_removexattr,
772 .listxattr = xfs_vn_listxattr,
775 const struct inode_operations xfs_symlink_inode_operations = {
776 .readlink = generic_readlink,
777 .follow_link = xfs_vn_follow_link,
778 .put_link = xfs_vn_put_link,
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,