1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or https://opensource.org/licenses/CDDL-1.0.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23  * Copyright (c) 2012, 2018 by Delphix. All rights reserved.
24  * Copyright 2016 Nexenta Systems, Inc. All rights reserved.
25  */
26 
27 #ifndef	_SYS_ZFS_ZNODE_IMPL_H
28 #define	_SYS_ZFS_ZNODE_IMPL_H
29 
30 #ifndef _KERNEL
31 #error "no user serviceable parts within"
32 #endif
33 
34 #include <sys/isa_defs.h>
35 #include <sys/types32.h>
36 #include <sys/list.h>
37 #include <sys/dmu.h>
38 #include <sys/sa.h>
39 #include <sys/time.h>
40 #include <sys/zfs_vfsops.h>
41 #include <sys/rrwlock.h>
42 #include <sys/zfs_sa.h>
43 #include <sys/zfs_stat.h>
44 #include <sys/zfs_rlock.h>
45 
46 #ifdef	__cplusplus
47 extern "C" {
48 #endif
49 
50 #define	ZNODE_OS_FIELDS			\
51 	inode_timespec_t z_btime; /* creation/birth time (cached) */ \
52 	struct inode	z_inode;
53 
54 /*
55  * Convert between znode pointers and inode pointers
56  */
57 #define	ZTOI(znode)	(&((znode)->z_inode))
58 #define	ITOZ(inode)	(container_of((inode), znode_t, z_inode))
59 #define	ZTOZSB(znode)	((zfsvfs_t *)(ZTOI(znode)->i_sb->s_fs_info))
60 #define	ITOZSB(inode)	((zfsvfs_t *)((inode)->i_sb->s_fs_info))
61 
62 #define	ZTOTYPE(zp)	(ZTOI(zp)->i_mode)
63 #define	ZTOGID(zp) (ZTOI(zp)->i_gid)
64 #define	ZTOUID(zp) (ZTOI(zp)->i_uid)
65 #define	ZTONLNK(zp) (ZTOI(zp)->i_nlink)
66 
67 #define	Z_ISBLK(type) S_ISBLK(type)
68 #define	Z_ISCHR(type) S_ISCHR(type)
69 #define	Z_ISLNK(type) S_ISLNK(type)
70 #define	Z_ISDEV(type)	(S_ISCHR(type) || S_ISBLK(type) || S_ISFIFO(type))
71 #define	Z_ISDIR(type)	S_ISDIR(type)
72 
73 #define	zn_has_cached_data(zp)		((zp)->z_is_mapped)
74 #define	zn_flush_cached_data(zp, sync)	write_inode_now(ZTOI(zp), sync)
75 #define	zn_rlimit_fsize(zp, uio)	(0)
76 
77 /*
78  * zhold() wraps igrab() on Linux, and igrab() may fail when the
79  * inode is in the process of being deleted.  As zhold() must only be
80  * called when a ref already exists - so the inode cannot be
81  * mid-deletion - we VERIFY() this.
82  */
83 #define	zhold(zp)	VERIFY3P(igrab(ZTOI((zp))), !=, NULL)
84 #define	zrele(zp)	iput(ZTOI((zp)))
85 
86 /* Called on entry to each ZFS inode and vfs operation. */
87 static inline int
88 zfs_enter(zfsvfs_t *zfsvfs, const char *tag)
89 {
90 	ZFS_TEARDOWN_ENTER_READ(zfsvfs, tag);
91 	if (unlikely(zfsvfs->z_unmounted)) {
92 		ZFS_TEARDOWN_EXIT_READ(zfsvfs, tag);
93 		return (SET_ERROR(EIO));
94 	}
95 	return (0);
96 }
97 
98 /* Must be called before exiting the operation. */
99 static inline void
100 zfs_exit(zfsvfs_t *zfsvfs, const char *tag)
101 {
102 	zfs_exit_fs(zfsvfs);
103 	ZFS_TEARDOWN_EXIT_READ(zfsvfs, tag);
104 }
105 
106 static inline int
107 zpl_enter(zfsvfs_t *zfsvfs, const char *tag)
108 {
109 	return (-zfs_enter(zfsvfs, tag));
110 }
111 
112 static inline void
113 zpl_exit(zfsvfs_t *zfsvfs, const char *tag)
114 {
115 	ZFS_TEARDOWN_EXIT_READ(zfsvfs, tag);
116 }
117 
118 /* zfs_verify_zp and zfs_enter_verify_zp are defined in zfs_znode.h */
119 #define	zpl_verify_zp(zp)	(-zfs_verify_zp(zp))
120 #define	zpl_enter_verify_zp(zfsvfs, zp, tag)	\
121 	(-zfs_enter_verify_zp(zfsvfs, zp, tag))
122 
123 /*
124  * Macros for dealing with dmu_buf_hold
125  */
126 #define	ZFS_OBJ_MTX_SZ		64
127 #define	ZFS_OBJ_MTX_MAX		(1024 * 1024)
128 #define	ZFS_OBJ_HASH(zfsvfs, obj)	((obj) & ((zfsvfs->z_hold_size) - 1))
129 
130 extern unsigned int zfs_object_mutex_size;
131 
132 /*
133  * Encode ZFS stored time values from a struct timespec / struct timespec64.
134  */
135 #define	ZFS_TIME_ENCODE(tp, stmp)		\
136 do {						\
137 	(stmp)[0] = (uint64_t)(tp)->tv_sec;	\
138 	(stmp)[1] = (uint64_t)(tp)->tv_nsec;	\
139 } while (0)
140 
141 #if defined(HAVE_INODE_TIMESPEC64_TIMES)
142 /*
143  * Decode ZFS stored time values to a struct timespec64
144  * 4.18 and newer kernels.
145  */
146 #define	ZFS_TIME_DECODE(tp, stmp)		\
147 do {						\
148 	(tp)->tv_sec = (time64_t)(stmp)[0];	\
149 	(tp)->tv_nsec = (long)(stmp)[1];	\
150 } while (0)
151 #else
152 /*
153  * Decode ZFS stored time values to a struct timespec
154  * 4.17 and older kernels.
155  */
156 #define	ZFS_TIME_DECODE(tp, stmp)		\
157 do {						\
158 	(tp)->tv_sec = (time_t)(stmp)[0];	\
159 	(tp)->tv_nsec = (long)(stmp)[1];	\
160 } while (0)
161 #endif /* HAVE_INODE_TIMESPEC64_TIMES */
162 
163 #define	ZFS_ACCESSTIME_STAMP(zfsvfs, zp)
164 
165 struct znode;
166 
167 extern int	zfs_sync(struct super_block *, int, cred_t *);
168 extern int	zfs_inode_alloc(struct super_block *, struct inode **ip);
169 extern void	zfs_inode_destroy(struct inode *);
170 extern void	zfs_mark_inode_dirty(struct inode *);
171 extern boolean_t zfs_relatime_need_update(const struct inode *);
172 
173 #if defined(HAVE_UIO_RW)
174 extern caddr_t zfs_map_page(page_t *, enum seg_rw);
175 extern void zfs_unmap_page(page_t *, caddr_t);
176 #endif /* HAVE_UIO_RW */
177 
178 extern zil_replay_func_t *const zfs_replay_vector[TX_MAX_TYPE];
179 
180 #ifdef	__cplusplus
181 }
182 #endif
183 
184 #endif	/* _SYS_ZFS_ZNODE_IMPL_H */
185