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/falloc.h>
50 #include <linux/fiemap.h>
51 #include <linux/slab.h>
54 * Bring the timestamps in the XFS inode uptodate.
56 * Used before writing the inode to disk.
59 xfs_synchronize_times(
62 struct inode
*inode
= VFS_I(ip
);
64 ip
->i_d
.di_atime
.t_sec
= (__int32_t
)inode
->i_atime
.tv_sec
;
65 ip
->i_d
.di_atime
.t_nsec
= (__int32_t
)inode
->i_atime
.tv_nsec
;
66 ip
->i_d
.di_ctime
.t_sec
= (__int32_t
)inode
->i_ctime
.tv_sec
;
67 ip
->i_d
.di_ctime
.t_nsec
= (__int32_t
)inode
->i_ctime
.tv_nsec
;
68 ip
->i_d
.di_mtime
.t_sec
= (__int32_t
)inode
->i_mtime
.tv_sec
;
69 ip
->i_d
.di_mtime
.t_nsec
= (__int32_t
)inode
->i_mtime
.tv_nsec
;
73 * If the linux inode is valid, mark it dirty.
74 * Used when commiting a dirty inode into a transaction so that
75 * the inode will get written back by the linux code
78 xfs_mark_inode_dirty_sync(
81 struct inode
*inode
= VFS_I(ip
);
83 if (!(inode
->i_state
& (I_WILL_FREE
|I_FREEING
)))
84 mark_inode_dirty_sync(inode
);
91 struct inode
*inode
= VFS_I(ip
);
93 if (!(inode
->i_state
& (I_WILL_FREE
|I_FREEING
)))
94 mark_inode_dirty(inode
);
98 * Hook in SELinux. This is not quite correct yet, what we really need
99 * here (as we do for default ACLs) is a mechanism by which creation of
100 * these attrs can be journalled at inode creation time (along with the
101 * inode, of course, such that log replay can't cause these to be lost).
108 struct xfs_inode
*ip
= XFS_I(inode
);
114 error
= security_inode_init_security(inode
, dir
, (char **)&name
,
117 if (error
== -EOPNOTSUPP
)
122 error
= xfs_attr_set(ip
, name
, value
, length
, ATTR_SECURE
);
131 struct xfs_name
*namep
,
132 struct dentry
*dentry
)
134 namep
->name
= dentry
->d_name
.name
;
135 namep
->len
= dentry
->d_name
.len
;
142 struct dentry
*dentry
)
144 struct xfs_name teardown
;
147 * If we can't add the ACL or we fail in
148 * xfs_init_security we must back out.
149 * ENOSPC can hit here, among other things.
151 xfs_dentry_to_name(&teardown
, dentry
);
153 xfs_remove(XFS_I(dir
), &teardown
, XFS_I(inode
));
160 struct dentry
*dentry
,
165 struct xfs_inode
*ip
= NULL
;
166 struct posix_acl
*default_acl
= NULL
;
167 struct xfs_name name
;
171 * Irix uses Missed'em'V split, but doesn't want to see
172 * the upper 5 bits of (14bit) major.
174 if (S_ISCHR(mode
) || S_ISBLK(mode
)) {
175 if (unlikely(!sysv_valid_dev(rdev
) || MAJOR(rdev
) & ~0x1ff))
177 rdev
= sysv_encode_dev(rdev
);
182 if (IS_POSIXACL(dir
)) {
183 default_acl
= xfs_get_acl(dir
, ACL_TYPE_DEFAULT
);
184 if (IS_ERR(default_acl
))
185 return -PTR_ERR(default_acl
);
188 mode
&= ~current_umask();
191 xfs_dentry_to_name(&name
, dentry
);
192 error
= xfs_create(XFS_I(dir
), &name
, mode
, rdev
, &ip
);
198 error
= xfs_init_security(inode
, dir
);
200 goto out_cleanup_inode
;
203 error
= -xfs_inherit_acl(inode
, default_acl
);
205 goto out_cleanup_inode
;
206 posix_acl_release(default_acl
);
210 d_instantiate(dentry
, inode
);
214 xfs_cleanup_inode(dir
, inode
, dentry
);
216 posix_acl_release(default_acl
);
223 struct dentry
*dentry
,
225 struct nameidata
*nd
)
227 return xfs_vn_mknod(dir
, dentry
, mode
, 0);
233 struct dentry
*dentry
,
236 return xfs_vn_mknod(dir
, dentry
, mode
|S_IFDIR
, 0);
239 STATIC
struct dentry
*
242 struct dentry
*dentry
,
243 struct nameidata
*nd
)
245 struct xfs_inode
*cip
;
246 struct xfs_name name
;
249 if (dentry
->d_name
.len
>= MAXNAMELEN
)
250 return ERR_PTR(-ENAMETOOLONG
);
252 xfs_dentry_to_name(&name
, dentry
);
253 error
= xfs_lookup(XFS_I(dir
), &name
, &cip
, NULL
);
254 if (unlikely(error
)) {
255 if (unlikely(error
!= ENOENT
))
256 return ERR_PTR(-error
);
261 return d_splice_alias(VFS_I(cip
), dentry
);
264 STATIC
struct dentry
*
267 struct dentry
*dentry
,
268 struct nameidata
*nd
)
270 struct xfs_inode
*ip
;
271 struct xfs_name xname
;
272 struct xfs_name ci_name
;
276 if (dentry
->d_name
.len
>= MAXNAMELEN
)
277 return ERR_PTR(-ENAMETOOLONG
);
279 xfs_dentry_to_name(&xname
, dentry
);
280 error
= xfs_lookup(XFS_I(dir
), &xname
, &ip
, &ci_name
);
281 if (unlikely(error
)) {
282 if (unlikely(error
!= ENOENT
))
283 return ERR_PTR(-error
);
285 * call d_add(dentry, NULL) here when d_drop_negative_children
286 * is called in xfs_vn_mknod (ie. allow negative dentries
287 * with CI filesystems).
292 /* if exact match, just splice and exit */
294 return d_splice_alias(VFS_I(ip
), dentry
);
296 /* else case-insensitive match... */
297 dname
.name
= ci_name
.name
;
298 dname
.len
= ci_name
.len
;
299 dentry
= d_add_ci(dentry
, VFS_I(ip
), &dname
);
300 kmem_free(ci_name
.name
);
306 struct dentry
*old_dentry
,
308 struct dentry
*dentry
)
310 struct inode
*inode
= old_dentry
->d_inode
;
311 struct xfs_name name
;
314 xfs_dentry_to_name(&name
, dentry
);
316 error
= xfs_link(XFS_I(dir
), XFS_I(inode
), &name
);
321 d_instantiate(dentry
, inode
);
328 struct dentry
*dentry
)
330 struct xfs_name name
;
333 xfs_dentry_to_name(&name
, dentry
);
335 error
= -xfs_remove(XFS_I(dir
), &name
, XFS_I(dentry
->d_inode
));
340 * With unlink, the VFS makes the dentry "negative": no inode,
341 * but still hashed. This is incompatible with case-insensitive
342 * mode, so invalidate (unhash) the dentry in CI-mode.
344 if (xfs_sb_version_hasasciici(&XFS_M(dir
->i_sb
)->m_sb
))
345 d_invalidate(dentry
);
352 struct dentry
*dentry
,
356 struct xfs_inode
*cip
= NULL
;
357 struct xfs_name name
;
362 (irix_symlink_mode
? 0777 & ~current_umask() : S_IRWXUGO
);
363 xfs_dentry_to_name(&name
, dentry
);
365 error
= xfs_symlink(XFS_I(dir
), &name
, symname
, mode
, &cip
);
371 error
= xfs_init_security(inode
, dir
);
373 goto out_cleanup_inode
;
375 d_instantiate(dentry
, inode
);
379 xfs_cleanup_inode(dir
, inode
, dentry
);
387 struct dentry
*odentry
,
389 struct dentry
*ndentry
)
391 struct inode
*new_inode
= ndentry
->d_inode
;
392 struct xfs_name oname
;
393 struct xfs_name nname
;
395 xfs_dentry_to_name(&oname
, odentry
);
396 xfs_dentry_to_name(&nname
, ndentry
);
398 return -xfs_rename(XFS_I(odir
), &oname
, XFS_I(odentry
->d_inode
),
399 XFS_I(ndir
), &nname
, new_inode
?
400 XFS_I(new_inode
) : NULL
);
404 * careful here - this function can get called recursively, so
405 * we need to be very careful about how much stack we use.
406 * uio is kmalloced for this reason...
410 struct dentry
*dentry
,
411 struct nameidata
*nd
)
416 link
= kmalloc(MAXPATHLEN
+1, GFP_KERNEL
);
420 error
= -xfs_readlink(XFS_I(dentry
->d_inode
), link
);
424 nd_set_link(nd
, link
);
430 nd_set_link(nd
, ERR_PTR(error
));
436 struct dentry
*dentry
,
437 struct nameidata
*nd
,
440 char *s
= nd_get_link(nd
);
448 struct vfsmount
*mnt
,
449 struct dentry
*dentry
,
452 struct inode
*inode
= dentry
->d_inode
;
453 struct xfs_inode
*ip
= XFS_I(inode
);
454 struct xfs_mount
*mp
= ip
->i_mount
;
456 trace_xfs_getattr(ip
);
458 if (XFS_FORCED_SHUTDOWN(mp
))
459 return XFS_ERROR(EIO
);
461 stat
->size
= XFS_ISIZE(ip
);
462 stat
->dev
= inode
->i_sb
->s_dev
;
463 stat
->mode
= ip
->i_d
.di_mode
;
464 stat
->nlink
= ip
->i_d
.di_nlink
;
465 stat
->uid
= ip
->i_d
.di_uid
;
466 stat
->gid
= ip
->i_d
.di_gid
;
467 stat
->ino
= ip
->i_ino
;
468 stat
->atime
= inode
->i_atime
;
469 stat
->mtime
= inode
->i_mtime
;
470 stat
->ctime
= inode
->i_ctime
;
472 XFS_FSB_TO_BB(mp
, ip
->i_d
.di_nblocks
+ ip
->i_delayed_blks
);
475 switch (inode
->i_mode
& S_IFMT
) {
478 stat
->blksize
= BLKDEV_IOSIZE
;
479 stat
->rdev
= MKDEV(sysv_major(ip
->i_df
.if_u2
.if_rdev
) & 0x1ff,
480 sysv_minor(ip
->i_df
.if_u2
.if_rdev
));
483 if (XFS_IS_REALTIME_INODE(ip
)) {
485 * If the file blocks are being allocated from a
486 * realtime volume, then return the inode's realtime
487 * extent size or the realtime volume's extent size.
490 xfs_get_extsz_hint(ip
) << mp
->m_sb
.sb_blocklog
;
492 stat
->blksize
= xfs_preferred_iosize(mp
);
502 struct dentry
*dentry
,
505 return -xfs_setattr(XFS_I(dentry
->d_inode
), iattr
, 0);
518 xfs_inode_t
*ip
= XFS_I(inode
);
520 /* preallocation on directories not yet supported */
522 if (S_ISDIR(inode
->i_mode
))
529 xfs_ilock(ip
, XFS_IOLOCK_EXCL
);
531 /* check the new inode size is valid before allocating */
532 if (!(mode
& FALLOC_FL_KEEP_SIZE
) &&
533 offset
+ len
> i_size_read(inode
)) {
534 new_size
= offset
+ len
;
535 error
= inode_newsize_ok(inode
, new_size
);
540 error
= -xfs_change_file_space(ip
, XFS_IOC_RESVSP
, &bf
,
545 /* Change file size if needed */
549 iattr
.ia_valid
= ATTR_SIZE
;
550 iattr
.ia_size
= new_size
;
551 error
= -xfs_setattr(ip
, &iattr
, XFS_ATTR_NOLOCK
);
555 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
560 #define XFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR)
563 * Call fiemap helper to fill in user data.
564 * Returns positive errors to xfs_getbmap.
569 struct getbmapx
*bmv
,
573 struct fiemap_extent_info
*fieinfo
= *arg
;
574 u32 fiemap_flags
= 0;
575 u64 logical
, physical
, length
;
577 /* Do nothing for a hole */
578 if (bmv
->bmv_block
== -1LL)
581 logical
= BBTOB(bmv
->bmv_offset
);
582 physical
= BBTOB(bmv
->bmv_block
);
583 length
= BBTOB(bmv
->bmv_length
);
585 if (bmv
->bmv_oflags
& BMV_OF_PREALLOC
)
586 fiemap_flags
|= FIEMAP_EXTENT_UNWRITTEN
;
587 else if (bmv
->bmv_oflags
& BMV_OF_DELALLOC
) {
588 fiemap_flags
|= FIEMAP_EXTENT_DELALLOC
;
589 physical
= 0; /* no block yet */
591 if (bmv
->bmv_oflags
& BMV_OF_LAST
)
592 fiemap_flags
|= FIEMAP_EXTENT_LAST
;
594 error
= fiemap_fill_next_extent(fieinfo
, logical
, physical
,
595 length
, fiemap_flags
);
598 *full
= 1; /* user array now full */
607 struct fiemap_extent_info
*fieinfo
,
611 xfs_inode_t
*ip
= XFS_I(inode
);
615 error
= fiemap_check_flags(fieinfo
, XFS_FIEMAP_FLAGS
);
619 /* Set up bmap header for xfs internal routine */
620 bm
.bmv_offset
= BTOBB(start
);
621 /* Special case for whole file */
622 if (length
== FIEMAP_MAX_OFFSET
)
623 bm
.bmv_length
= -1LL;
625 bm
.bmv_length
= BTOBB(length
);
627 /* We add one because in getbmap world count includes the header */
628 bm
.bmv_count
= !fieinfo
->fi_extents_max
? MAXEXTNUM
:
629 fieinfo
->fi_extents_max
+ 1;
630 bm
.bmv_count
= min_t(__s32
, bm
.bmv_count
,
631 (PAGE_SIZE
* 16 / sizeof(struct getbmapx
)));
632 bm
.bmv_iflags
= BMV_IF_PREALLOC
| BMV_IF_NO_HOLES
;
633 if (fieinfo
->fi_flags
& FIEMAP_FLAG_XATTR
)
634 bm
.bmv_iflags
|= BMV_IF_ATTRFORK
;
635 if (!(fieinfo
->fi_flags
& FIEMAP_FLAG_SYNC
))
636 bm
.bmv_iflags
|= BMV_IF_DELALLOC
;
638 error
= xfs_getbmap(ip
, &bm
, xfs_fiemap_format
, fieinfo
);
645 static const struct inode_operations xfs_inode_operations
= {
646 .check_acl
= xfs_check_acl
,
647 .getattr
= xfs_vn_getattr
,
648 .setattr
= xfs_vn_setattr
,
649 .setxattr
= generic_setxattr
,
650 .getxattr
= generic_getxattr
,
651 .removexattr
= generic_removexattr
,
652 .listxattr
= xfs_vn_listxattr
,
653 .fallocate
= xfs_vn_fallocate
,
654 .fiemap
= xfs_vn_fiemap
,
657 static const struct inode_operations xfs_dir_inode_operations
= {
658 .create
= xfs_vn_create
,
659 .lookup
= xfs_vn_lookup
,
661 .unlink
= xfs_vn_unlink
,
662 .symlink
= xfs_vn_symlink
,
663 .mkdir
= xfs_vn_mkdir
,
665 * Yes, XFS uses the same method for rmdir and unlink.
667 * There are some subtile differences deeper in the code,
668 * but we use S_ISDIR to check for those.
670 .rmdir
= xfs_vn_unlink
,
671 .mknod
= xfs_vn_mknod
,
672 .rename
= xfs_vn_rename
,
673 .check_acl
= xfs_check_acl
,
674 .getattr
= xfs_vn_getattr
,
675 .setattr
= xfs_vn_setattr
,
676 .setxattr
= generic_setxattr
,
677 .getxattr
= generic_getxattr
,
678 .removexattr
= generic_removexattr
,
679 .listxattr
= xfs_vn_listxattr
,
682 static const struct inode_operations xfs_dir_ci_inode_operations
= {
683 .create
= xfs_vn_create
,
684 .lookup
= xfs_vn_ci_lookup
,
686 .unlink
= xfs_vn_unlink
,
687 .symlink
= xfs_vn_symlink
,
688 .mkdir
= xfs_vn_mkdir
,
690 * Yes, XFS uses the same method for rmdir and unlink.
692 * There are some subtile differences deeper in the code,
693 * but we use S_ISDIR to check for those.
695 .rmdir
= xfs_vn_unlink
,
696 .mknod
= xfs_vn_mknod
,
697 .rename
= xfs_vn_rename
,
698 .check_acl
= xfs_check_acl
,
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
,
707 static const struct inode_operations xfs_symlink_inode_operations
= {
708 .readlink
= generic_readlink
,
709 .follow_link
= xfs_vn_follow_link
,
710 .put_link
= xfs_vn_put_link
,
711 .check_acl
= xfs_check_acl
,
712 .getattr
= xfs_vn_getattr
,
713 .setattr
= xfs_vn_setattr
,
714 .setxattr
= generic_setxattr
,
715 .getxattr
= generic_getxattr
,
716 .removexattr
= generic_removexattr
,
717 .listxattr
= xfs_vn_listxattr
,
721 xfs_diflags_to_iflags(
723 struct xfs_inode
*ip
)
725 if (ip
->i_d
.di_flags
& XFS_DIFLAG_IMMUTABLE
)
726 inode
->i_flags
|= S_IMMUTABLE
;
728 inode
->i_flags
&= ~S_IMMUTABLE
;
729 if (ip
->i_d
.di_flags
& XFS_DIFLAG_APPEND
)
730 inode
->i_flags
|= S_APPEND
;
732 inode
->i_flags
&= ~S_APPEND
;
733 if (ip
->i_d
.di_flags
& XFS_DIFLAG_SYNC
)
734 inode
->i_flags
|= S_SYNC
;
736 inode
->i_flags
&= ~S_SYNC
;
737 if (ip
->i_d
.di_flags
& XFS_DIFLAG_NOATIME
)
738 inode
->i_flags
|= S_NOATIME
;
740 inode
->i_flags
&= ~S_NOATIME
;
744 * Initialize the Linux inode, set up the operation vectors and
747 * When reading existing inodes from disk this is called directly
748 * from xfs_iget, when creating a new inode it is called from
749 * xfs_ialloc after setting up the inode.
751 * We are always called with an uninitialised linux inode here.
752 * We need to initialise the necessary fields and take a reference
757 struct xfs_inode
*ip
)
759 struct inode
*inode
= &ip
->i_vnode
;
761 inode
->i_ino
= ip
->i_ino
;
762 inode
->i_state
= I_NEW
;
764 inode_sb_list_add(inode
);
765 insert_inode_hash(inode
);
767 inode
->i_mode
= ip
->i_d
.di_mode
;
768 inode
->i_nlink
= ip
->i_d
.di_nlink
;
769 inode
->i_uid
= ip
->i_d
.di_uid
;
770 inode
->i_gid
= ip
->i_d
.di_gid
;
772 switch (inode
->i_mode
& S_IFMT
) {
776 MKDEV(sysv_major(ip
->i_df
.if_u2
.if_rdev
) & 0x1ff,
777 sysv_minor(ip
->i_df
.if_u2
.if_rdev
));
784 inode
->i_generation
= ip
->i_d
.di_gen
;
785 i_size_write(inode
, ip
->i_d
.di_size
);
786 inode
->i_atime
.tv_sec
= ip
->i_d
.di_atime
.t_sec
;
787 inode
->i_atime
.tv_nsec
= ip
->i_d
.di_atime
.t_nsec
;
788 inode
->i_mtime
.tv_sec
= ip
->i_d
.di_mtime
.t_sec
;
789 inode
->i_mtime
.tv_nsec
= ip
->i_d
.di_mtime
.t_nsec
;
790 inode
->i_ctime
.tv_sec
= ip
->i_d
.di_ctime
.t_sec
;
791 inode
->i_ctime
.tv_nsec
= ip
->i_d
.di_ctime
.t_nsec
;
792 xfs_diflags_to_iflags(inode
, ip
);
794 switch (inode
->i_mode
& S_IFMT
) {
796 inode
->i_op
= &xfs_inode_operations
;
797 inode
->i_fop
= &xfs_file_operations
;
798 inode
->i_mapping
->a_ops
= &xfs_address_space_operations
;
801 if (xfs_sb_version_hasasciici(&XFS_M(inode
->i_sb
)->m_sb
))
802 inode
->i_op
= &xfs_dir_ci_inode_operations
;
804 inode
->i_op
= &xfs_dir_inode_operations
;
805 inode
->i_fop
= &xfs_dir_file_operations
;
808 inode
->i_op
= &xfs_symlink_inode_operations
;
809 if (!(ip
->i_df
.if_flags
& XFS_IFINLINE
))
810 inode
->i_mapping
->a_ops
= &xfs_address_space_operations
;
813 inode
->i_op
= &xfs_inode_operations
;
814 init_special_inode(inode
, inode
->i_mode
, inode
->i_rdev
);
818 xfs_iflags_clear(ip
, XFS_INEW
);
821 unlock_new_inode(inode
);