xref: /freebsd/sys/contrib/openzfs/include/os/linux/zfs/sys/zfs_znode_impl.h (revision 22649d4dba730d46244fd2dff4fd174903c8379f)
1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3  * This file and its contents are supplied under the terms of the
4  * Common Development and Distribution License ("CDDL"), version 1.0.
5  * You may only use this file in accordance with the terms of version
6  * 1.0 of the CDDL.
7  *
8  * A full copy of the text of the CDDL should have accompanied this
9  * source.  A copy of the CDDL is also available via the Internet at
10  * https://opensource.org/license/CDDL-1.0.
11  */
12 /*
13  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
14  * Copyright (c) 2012, 2018 by Delphix. All rights reserved.
15  * Copyright 2016 Nexenta Systems, Inc. All rights reserved.
16  */
17 
18 #ifndef	_SYS_ZFS_ZNODE_IMPL_H
19 #define	_SYS_ZFS_ZNODE_IMPL_H
20 
21 #ifndef _KERNEL
22 #error "no user serviceable parts within"
23 #endif
24 
25 #include <sys/isa_defs.h>
26 #include <sys/types32.h>
27 #include <sys/list.h>
28 #include <sys/dmu.h>
29 #include <sys/sa.h>
30 #include <sys/time.h>
31 #include <sys/zfs_vfsops.h>
32 #include <sys/rrwlock.h>
33 #include <sys/zfs_sa.h>
34 #include <sys/zfs_stat.h>
35 #include <sys/zfs_rlock.h>
36 
37 #ifdef	__cplusplus
38 extern "C" {
39 #endif
40 
41 #define	ZNODE_OS_FIELDS			\
42 	inode_timespec_t z_btime; /* creation/birth time (cached) */ \
43 	struct inode	z_inode;
44 
45 /*
46  * Convert between znode pointers and inode pointers
47  */
48 #define	ZTOI(znode)	(&((znode)->z_inode))
49 #define	ITOZ(inode)	(container_of((inode), znode_t, z_inode))
50 #define	ZTOZSB(znode)	((zfsvfs_t *)(ZTOI(znode)->i_sb->s_fs_info))
51 #define	ITOZSB(inode)	((zfsvfs_t *)((inode)->i_sb->s_fs_info))
52 
53 #define	ZTOTYPE(zp)	(ZTOI(zp)->i_mode)
54 #define	ZTOGID(zp) (ZTOI(zp)->i_gid)
55 #define	ZTOUID(zp) (ZTOI(zp)->i_uid)
56 #define	ZTONLNK(zp) (ZTOI(zp)->i_nlink)
57 
58 #define	Z_ISBLK(type) S_ISBLK(type)
59 #define	Z_ISCHR(type) S_ISCHR(type)
60 #define	Z_ISLNK(type) S_ISLNK(type)
61 #define	Z_ISDEV(type)	(S_ISCHR(type) || S_ISBLK(type) || S_ISFIFO(type))
62 #define	Z_ISDIR(type)	S_ISDIR(type)
63 
64 #define	zn_has_cached_data(zp, start, end) \
65 	filemap_range_has_page(ZTOI(zp)->i_mapping, start, end)
66 
67 #define	zn_flush_cached_data(zp, sync)	write_inode_now(ZTOI(zp), sync)
68 #define	zn_rlimit_fsize(size)		(0)
69 #define	zn_rlimit_fsize_uio(zp, uio)	(0)
70 
71 /*
72  * zhold() wraps igrab() on Linux, and igrab() may fail when the
73  * inode is in the process of being deleted.  As zhold() must only be
74  * called when a ref already exists - so the inode cannot be
75  * mid-deletion - we VERIFY() this.
76  */
77 #define	zhold(zp)	VERIFY3P(igrab(ZTOI((zp))), !=, NULL)
78 #define	zrele(zp)	iput(ZTOI((zp)))
79 
80 /* Called on entry to each ZFS inode and vfs operation. */
81 static inline int
zfs_enter(zfsvfs_t * zfsvfs,const char * tag)82 zfs_enter(zfsvfs_t *zfsvfs, const char *tag)
83 {
84 	ZFS_TEARDOWN_ENTER_READ(zfsvfs, tag);
85 	if (unlikely(zfsvfs->z_unmounted)) {
86 		ZFS_TEARDOWN_EXIT_READ(zfsvfs, tag);
87 		return (SET_ERROR(EIO));
88 	}
89 	return (0);
90 }
91 
92 /* Must be called before exiting the operation. */
93 static inline void
zfs_exit(zfsvfs_t * zfsvfs,const char * tag)94 zfs_exit(zfsvfs_t *zfsvfs, const char *tag)
95 {
96 	zfs_exit_fs(zfsvfs);
97 	ZFS_TEARDOWN_EXIT_READ(zfsvfs, tag);
98 }
99 
100 static inline int
zpl_enter(zfsvfs_t * zfsvfs,const char * tag)101 zpl_enter(zfsvfs_t *zfsvfs, const char *tag)
102 {
103 	return (-zfs_enter(zfsvfs, tag));
104 }
105 
106 static inline void
zpl_exit(zfsvfs_t * zfsvfs,const char * tag)107 zpl_exit(zfsvfs_t *zfsvfs, const char *tag)
108 {
109 	ZFS_TEARDOWN_EXIT_READ(zfsvfs, tag);
110 }
111 
112 /* zfs_verify_zp and zfs_enter_verify_zp are defined in zfs_znode.h */
113 #define	zpl_verify_zp(zp)	(-zfs_verify_zp(zp))
114 #define	zpl_enter_verify_zp(zfsvfs, zp, tag)	\
115 	(-zfs_enter_verify_zp(zfsvfs, zp, tag))
116 
117 /*
118  * Macros for dealing with dmu_buf_hold
119  */
120 #define	ZFS_OBJ_MTX_SZ		64
121 #define	ZFS_OBJ_MTX_MAX		(1024 * 1024)
122 #define	ZFS_OBJ_HASH(zfsvfs, obj)	((obj) & ((zfsvfs->z_hold_size) - 1))
123 
124 extern unsigned int zfs_object_mutex_size;
125 
126 /*
127  * Encode ZFS stored time values from a struct timespec / struct timespec64.
128  */
129 #define	ZFS_TIME_ENCODE(tp, stmp)		\
130 do {						\
131 	(stmp)[0] = (uint64_t)(tp)->tv_sec;	\
132 	(stmp)[1] = (uint64_t)(tp)->tv_nsec;	\
133 } while (0)
134 
135 /*
136  * Decode ZFS stored time values to a struct timespec64
137  */
138 #define	ZFS_TIME_DECODE(tp, stmp)		\
139 do {						\
140 	(tp)->tv_sec = (time64_t)(stmp)[0];	\
141 	(tp)->tv_nsec = (long)(stmp)[1];	\
142 } while (0)
143 
144 #define	ZFS_ACCESSTIME_STAMP(zfsvfs, zp)
145 
146 struct znode;
147 
148 extern int	zfs_sync(struct super_block *, int, cred_t *);
149 extern int	zfs_inode_alloc(struct super_block *, struct inode **ip);
150 extern void	zfs_inode_free(struct inode *);
151 extern void	zfs_inode_destroy(struct inode *);
152 extern void	zfs_mark_inode_dirty(struct inode *);
153 extern boolean_t zfs_relatime_need_update(const struct inode *);
154 extern zil_replay_func_t *const zfs_replay_vector[TX_MAX_TYPE];
155 
156 #ifdef	__cplusplus
157 }
158 #endif
159 
160 #endif	/* _SYS_ZFS_ZNODE_IMPL_H */
161