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
24 #include "xfs_trans.h"
27 #include "xfs_alloc.h"
28 #include "xfs_quota.h"
29 #include "xfs_mount.h"
30 #include "xfs_bmap_btree.h"
31 #include "xfs_dinode.h"
32 #include "xfs_inode.h"
34 #include "xfs_rtalloc.h"
35 #include "xfs_error.h"
36 #include "xfs_itable.h"
39 #include "xfs_buf_item.h"
40 #include "xfs_utils.h"
41 #include "xfs_vnodeops.h"
42 #include "xfs_trace.h"
44 #include <linux/capability.h>
45 #include <linux/xattr.h>
46 #include <linux/namei.h>
47 #include <linux/posix_acl.h>
48 #include <linux/security.h>
49 #include <linux/fiemap.h>
50 #include <linux/slab.h>
53 * Bring the timestamps in the XFS inode uptodate.
55 * Used before writing the inode to disk.
58 xfs_synchronize_times(
61 struct inode *inode = VFS_I(ip);
63 ip->i_d.di_atime.t_sec = (__int32_t)inode->i_atime.tv_sec;
64 ip->i_d.di_atime.t_nsec = (__int32_t)inode->i_atime.tv_nsec;
65 ip->i_d.di_ctime.t_sec = (__int32_t)inode->i_ctime.tv_sec;
66 ip->i_d.di_ctime.t_nsec = (__int32_t)inode->i_ctime.tv_nsec;
67 ip->i_d.di_mtime.t_sec = (__int32_t)inode->i_mtime.tv_sec;
68 ip->i_d.di_mtime.t_nsec = (__int32_t)inode->i_mtime.tv_nsec;
72 * If the linux inode is valid, mark it dirty.
73 * Used when commiting a dirty inode into a transaction so that
74 * the inode will get written back by the linux code
77 xfs_mark_inode_dirty_sync(
80 struct inode *inode = VFS_I(ip);
82 if (!(inode->i_state & (I_WILL_FREE|I_FREEING)))
83 mark_inode_dirty_sync(inode);
90 struct inode *inode = VFS_I(ip);
92 if (!(inode->i_state & (I_WILL_FREE|I_FREEING)))
93 mark_inode_dirty(inode);
97 * Hook in SELinux. This is not quite correct yet, what we really need
98 * here (as we do for default ACLs) is a mechanism by which creation of
99 * these attrs can be journalled at inode creation time (along with the
100 * inode, of course, such that log replay can't cause these to be lost).
107 struct xfs_inode *ip = XFS_I(inode);
113 error = security_inode_init_security(inode, dir, (char **)&name,
116 if (error == -EOPNOTSUPP)
121 error = xfs_attr_set(ip, name, value, length, ATTR_SECURE);
130 struct xfs_name *namep,
131 struct dentry *dentry)
133 namep->name = dentry->d_name.name;
134 namep->len = dentry->d_name.len;
141 struct dentry *dentry)
143 struct xfs_name teardown;
146 * If we can't add the ACL or we fail in
147 * xfs_init_security we must back out.
148 * ENOSPC can hit here, among other things.
150 xfs_dentry_to_name(&teardown, dentry);
152 xfs_remove(XFS_I(dir), &teardown, XFS_I(inode));
159 struct dentry *dentry,
164 struct xfs_inode *ip = NULL;
165 struct posix_acl *default_acl = NULL;
166 struct xfs_name name;
170 * Irix uses Missed'em'V split, but doesn't want to see
171 * the upper 5 bits of (14bit) major.
173 if (S_ISCHR(mode) || S_ISBLK(mode)) {
174 if (unlikely(!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff))
176 rdev = sysv_encode_dev(rdev);
181 if (IS_POSIXACL(dir)) {
182 default_acl = xfs_get_acl(dir, ACL_TYPE_DEFAULT);
183 if (IS_ERR(default_acl))
184 return -PTR_ERR(default_acl);
187 mode &= ~current_umask();
190 xfs_dentry_to_name(&name, dentry);
191 error = xfs_create(XFS_I(dir), &name, mode, rdev, &ip);
197 error = xfs_init_security(inode, dir);
199 goto out_cleanup_inode;
202 error = -xfs_inherit_acl(inode, default_acl);
204 goto out_cleanup_inode;
205 posix_acl_release(default_acl);
209 d_instantiate(dentry, inode);
213 xfs_cleanup_inode(dir, inode, dentry);
215 posix_acl_release(default_acl);
222 struct dentry *dentry,
224 struct nameidata *nd)
226 return xfs_vn_mknod(dir, dentry, mode, 0);
232 struct dentry *dentry,
235 return xfs_vn_mknod(dir, dentry, mode|S_IFDIR, 0);
238 STATIC struct dentry *
241 struct dentry *dentry,
242 struct nameidata *nd)
244 struct xfs_inode *cip;
245 struct xfs_name name;
248 if (dentry->d_name.len >= MAXNAMELEN)
249 return ERR_PTR(-ENAMETOOLONG);
251 xfs_dentry_to_name(&name, dentry);
252 error = xfs_lookup(XFS_I(dir), &name, &cip, NULL);
253 if (unlikely(error)) {
254 if (unlikely(error != ENOENT))
255 return ERR_PTR(-error);
260 return d_splice_alias(VFS_I(cip), dentry);
263 STATIC struct dentry *
266 struct dentry *dentry,
267 struct nameidata *nd)
269 struct xfs_inode *ip;
270 struct xfs_name xname;
271 struct xfs_name ci_name;
275 if (dentry->d_name.len >= MAXNAMELEN)
276 return ERR_PTR(-ENAMETOOLONG);
278 xfs_dentry_to_name(&xname, dentry);
279 error = xfs_lookup(XFS_I(dir), &xname, &ip, &ci_name);
280 if (unlikely(error)) {
281 if (unlikely(error != ENOENT))
282 return ERR_PTR(-error);
284 * call d_add(dentry, NULL) here when d_drop_negative_children
285 * is called in xfs_vn_mknod (ie. allow negative dentries
286 * with CI filesystems).
291 /* if exact match, just splice and exit */
293 return d_splice_alias(VFS_I(ip), dentry);
295 /* else case-insensitive match... */
296 dname.name = ci_name.name;
297 dname.len = ci_name.len;
298 dentry = d_add_ci(dentry, VFS_I(ip), &dname);
299 kmem_free(ci_name.name);
305 struct dentry *old_dentry,
307 struct dentry *dentry)
309 struct inode *inode = old_dentry->d_inode;
310 struct xfs_name name;
313 xfs_dentry_to_name(&name, dentry);
315 error = xfs_link(XFS_I(dir), XFS_I(inode), &name);
320 d_instantiate(dentry, inode);
327 struct dentry *dentry)
329 struct xfs_name name;
332 xfs_dentry_to_name(&name, dentry);
334 error = -xfs_remove(XFS_I(dir), &name, XFS_I(dentry->d_inode));
339 * With unlink, the VFS makes the dentry "negative": no inode,
340 * but still hashed. This is incompatible with case-insensitive
341 * mode, so invalidate (unhash) the dentry in CI-mode.
343 if (xfs_sb_version_hasasciici(&XFS_M(dir->i_sb)->m_sb))
344 d_invalidate(dentry);
351 struct dentry *dentry,
355 struct xfs_inode *cip = NULL;
356 struct xfs_name name;
361 (irix_symlink_mode ? 0777 & ~current_umask() : S_IRWXUGO);
362 xfs_dentry_to_name(&name, dentry);
364 error = xfs_symlink(XFS_I(dir), &name, symname, mode, &cip);
370 error = xfs_init_security(inode, dir);
372 goto out_cleanup_inode;
374 d_instantiate(dentry, inode);
378 xfs_cleanup_inode(dir, inode, dentry);
386 struct dentry *odentry,
388 struct dentry *ndentry)
390 struct inode *new_inode = ndentry->d_inode;
391 struct xfs_name oname;
392 struct xfs_name nname;
394 xfs_dentry_to_name(&oname, odentry);
395 xfs_dentry_to_name(&nname, ndentry);
397 return -xfs_rename(XFS_I(odir), &oname, XFS_I(odentry->d_inode),
398 XFS_I(ndir), &nname, new_inode ?
399 XFS_I(new_inode) : NULL);
403 * careful here - this function can get called recursively, so
404 * we need to be very careful about how much stack we use.
405 * uio is kmalloced for this reason...
409 struct dentry *dentry,
410 struct nameidata *nd)
415 link = kmalloc(MAXPATHLEN+1, GFP_KERNEL);
419 error = -xfs_readlink(XFS_I(dentry->d_inode), link);
423 nd_set_link(nd, link);
429 nd_set_link(nd, ERR_PTR(error));
435 struct dentry *dentry,
436 struct nameidata *nd,
439 char *s = nd_get_link(nd);
447 struct vfsmount *mnt,
448 struct dentry *dentry,
451 struct inode *inode = dentry->d_inode;
452 struct xfs_inode *ip = XFS_I(inode);
453 struct xfs_mount *mp = ip->i_mount;
455 trace_xfs_getattr(ip);
457 if (XFS_FORCED_SHUTDOWN(mp))
458 return XFS_ERROR(EIO);
460 stat->size = XFS_ISIZE(ip);
461 stat->dev = inode->i_sb->s_dev;
462 stat->mode = ip->i_d.di_mode;
463 stat->nlink = ip->i_d.di_nlink;
464 stat->uid = ip->i_d.di_uid;
465 stat->gid = ip->i_d.di_gid;
466 stat->ino = ip->i_ino;
467 stat->atime = inode->i_atime;
468 stat->mtime = inode->i_mtime;
469 stat->ctime = inode->i_ctime;
471 XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
474 switch (inode->i_mode & S_IFMT) {
477 stat->blksize = BLKDEV_IOSIZE;
478 stat->rdev = MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
479 sysv_minor(ip->i_df.if_u2.if_rdev));
482 if (XFS_IS_REALTIME_INODE(ip)) {
484 * If the file blocks are being allocated from a
485 * realtime volume, then return the inode's realtime
486 * extent size or the realtime volume's extent size.
489 xfs_get_extsz_hint(ip) << mp->m_sb.sb_blocklog;
491 stat->blksize = xfs_preferred_iosize(mp);
501 struct dentry *dentry,
504 return -xfs_setattr(XFS_I(dentry->d_inode), iattr, 0);
507 #define XFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR)
510 * Call fiemap helper to fill in user data.
511 * Returns positive errors to xfs_getbmap.
516 struct getbmapx *bmv,
520 struct fiemap_extent_info *fieinfo = *arg;
521 u32 fiemap_flags = 0;
522 u64 logical, physical, length;
524 /* Do nothing for a hole */
525 if (bmv->bmv_block == -1LL)
528 logical = BBTOB(bmv->bmv_offset);
529 physical = BBTOB(bmv->bmv_block);
530 length = BBTOB(bmv->bmv_length);
532 if (bmv->bmv_oflags & BMV_OF_PREALLOC)
533 fiemap_flags |= FIEMAP_EXTENT_UNWRITTEN;
534 else if (bmv->bmv_oflags & BMV_OF_DELALLOC) {
535 fiemap_flags |= FIEMAP_EXTENT_DELALLOC;
536 physical = 0; /* no block yet */
538 if (bmv->bmv_oflags & BMV_OF_LAST)
539 fiemap_flags |= FIEMAP_EXTENT_LAST;
541 error = fiemap_fill_next_extent(fieinfo, logical, physical,
542 length, fiemap_flags);
545 *full = 1; /* user array now full */
554 struct fiemap_extent_info *fieinfo,
558 xfs_inode_t *ip = XFS_I(inode);
562 error = fiemap_check_flags(fieinfo, XFS_FIEMAP_FLAGS);
566 /* Set up bmap header for xfs internal routine */
567 bm.bmv_offset = BTOBB(start);
568 /* Special case for whole file */
569 if (length == FIEMAP_MAX_OFFSET)
570 bm.bmv_length = -1LL;
572 bm.bmv_length = BTOBB(length);
574 /* We add one because in getbmap world count includes the header */
575 bm.bmv_count = !fieinfo->fi_extents_max ? MAXEXTNUM :
576 fieinfo->fi_extents_max + 1;
577 bm.bmv_count = min_t(__s32, bm.bmv_count,
578 (PAGE_SIZE * 16 / sizeof(struct getbmapx)));
579 bm.bmv_iflags = BMV_IF_PREALLOC | BMV_IF_NO_HOLES;
580 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR)
581 bm.bmv_iflags |= BMV_IF_ATTRFORK;
582 if (!(fieinfo->fi_flags & FIEMAP_FLAG_SYNC))
583 bm.bmv_iflags |= BMV_IF_DELALLOC;
585 error = xfs_getbmap(ip, &bm, xfs_fiemap_format, fieinfo);
592 static const struct inode_operations xfs_inode_operations = {
593 .check_acl = xfs_check_acl,
594 .getattr = xfs_vn_getattr,
595 .setattr = xfs_vn_setattr,
596 .setxattr = generic_setxattr,
597 .getxattr = generic_getxattr,
598 .removexattr = generic_removexattr,
599 .listxattr = xfs_vn_listxattr,
600 .fiemap = xfs_vn_fiemap,
603 static const struct inode_operations xfs_dir_inode_operations = {
604 .create = xfs_vn_create,
605 .lookup = xfs_vn_lookup,
607 .unlink = xfs_vn_unlink,
608 .symlink = xfs_vn_symlink,
609 .mkdir = xfs_vn_mkdir,
611 * Yes, XFS uses the same method for rmdir and unlink.
613 * There are some subtile differences deeper in the code,
614 * but we use S_ISDIR to check for those.
616 .rmdir = xfs_vn_unlink,
617 .mknod = xfs_vn_mknod,
618 .rename = xfs_vn_rename,
619 .check_acl = xfs_check_acl,
620 .getattr = xfs_vn_getattr,
621 .setattr = xfs_vn_setattr,
622 .setxattr = generic_setxattr,
623 .getxattr = generic_getxattr,
624 .removexattr = generic_removexattr,
625 .listxattr = xfs_vn_listxattr,
628 static const struct inode_operations xfs_dir_ci_inode_operations = {
629 .create = xfs_vn_create,
630 .lookup = xfs_vn_ci_lookup,
632 .unlink = xfs_vn_unlink,
633 .symlink = xfs_vn_symlink,
634 .mkdir = xfs_vn_mkdir,
636 * Yes, XFS uses the same method for rmdir and unlink.
638 * There are some subtile differences deeper in the code,
639 * but we use S_ISDIR to check for those.
641 .rmdir = xfs_vn_unlink,
642 .mknod = xfs_vn_mknod,
643 .rename = xfs_vn_rename,
644 .check_acl = xfs_check_acl,
645 .getattr = xfs_vn_getattr,
646 .setattr = xfs_vn_setattr,
647 .setxattr = generic_setxattr,
648 .getxattr = generic_getxattr,
649 .removexattr = generic_removexattr,
650 .listxattr = xfs_vn_listxattr,
653 static const struct inode_operations xfs_symlink_inode_operations = {
654 .readlink = generic_readlink,
655 .follow_link = xfs_vn_follow_link,
656 .put_link = xfs_vn_put_link,
657 .check_acl = xfs_check_acl,
658 .getattr = xfs_vn_getattr,
659 .setattr = xfs_vn_setattr,
660 .setxattr = generic_setxattr,
661 .getxattr = generic_getxattr,
662 .removexattr = generic_removexattr,
663 .listxattr = xfs_vn_listxattr,
667 xfs_diflags_to_iflags(
669 struct xfs_inode *ip)
671 if (ip->i_d.di_flags & XFS_DIFLAG_IMMUTABLE)
672 inode->i_flags |= S_IMMUTABLE;
674 inode->i_flags &= ~S_IMMUTABLE;
675 if (ip->i_d.di_flags & XFS_DIFLAG_APPEND)
676 inode->i_flags |= S_APPEND;
678 inode->i_flags &= ~S_APPEND;
679 if (ip->i_d.di_flags & XFS_DIFLAG_SYNC)
680 inode->i_flags |= S_SYNC;
682 inode->i_flags &= ~S_SYNC;
683 if (ip->i_d.di_flags & XFS_DIFLAG_NOATIME)
684 inode->i_flags |= S_NOATIME;
686 inode->i_flags &= ~S_NOATIME;
690 * Initialize the Linux inode, set up the operation vectors and
693 * When reading existing inodes from disk this is called directly
694 * from xfs_iget, when creating a new inode it is called from
695 * xfs_ialloc after setting up the inode.
697 * We are always called with an uninitialised linux inode here.
698 * We need to initialise the necessary fields and take a reference
703 struct xfs_inode *ip)
705 struct inode *inode = &ip->i_vnode;
707 inode->i_ino = ip->i_ino;
708 inode->i_state = I_NEW;
710 inode_sb_list_add(inode);
711 /* make the inode look hashed for the writeback code */
712 hlist_add_fake(&inode->i_hash);
714 inode->i_mode = ip->i_d.di_mode;
715 inode->i_nlink = ip->i_d.di_nlink;
716 inode->i_uid = ip->i_d.di_uid;
717 inode->i_gid = ip->i_d.di_gid;
719 switch (inode->i_mode & S_IFMT) {
723 MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
724 sysv_minor(ip->i_df.if_u2.if_rdev));
731 inode->i_generation = ip->i_d.di_gen;
732 i_size_write(inode, ip->i_d.di_size);
733 inode->i_atime.tv_sec = ip->i_d.di_atime.t_sec;
734 inode->i_atime.tv_nsec = ip->i_d.di_atime.t_nsec;
735 inode->i_mtime.tv_sec = ip->i_d.di_mtime.t_sec;
736 inode->i_mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
737 inode->i_ctime.tv_sec = ip->i_d.di_ctime.t_sec;
738 inode->i_ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
739 xfs_diflags_to_iflags(inode, ip);
741 switch (inode->i_mode & S_IFMT) {
743 inode->i_op = &xfs_inode_operations;
744 inode->i_fop = &xfs_file_operations;
745 inode->i_mapping->a_ops = &xfs_address_space_operations;
748 if (xfs_sb_version_hasasciici(&XFS_M(inode->i_sb)->m_sb))
749 inode->i_op = &xfs_dir_ci_inode_operations;
751 inode->i_op = &xfs_dir_inode_operations;
752 inode->i_fop = &xfs_dir_file_operations;
755 inode->i_op = &xfs_symlink_inode_operations;
756 if (!(ip->i_df.if_flags & XFS_IFINLINE))
757 inode->i_mapping->a_ops = &xfs_address_space_operations;
760 inode->i_op = &xfs_inode_operations;
761 init_special_inode(inode, inode->i_mode, inode->i_rdev);
765 xfs_iflags_clear(ip, XFS_INEW);
768 unlock_new_inode(inode);