xref: /titanic_50/usr/src/lib/libzpool/common/sys/zfs_context.h (revision afd1ac7b1c9a8cdf273c865aa5e9a14620341443)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #ifndef _SYS_ZFS_CONTEXT_H
28 #define	_SYS_ZFS_CONTEXT_H
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #ifdef	__cplusplus
33 extern "C" {
34 #endif
35 
36 #define	_SYS_MUTEX_H
37 #define	_SYS_RWLOCK_H
38 #define	_SYS_CONDVAR_H
39 #define	_SYS_SYSTM_H
40 #define	_SYS_DEBUG_H
41 #define	_SYS_T_LOCK_H
42 #define	_SYS_VNODE_H
43 #define	_SYS_VFS_H
44 #define	_SYS_SUNDDI_H
45 #define	_SYS_CALLB_H
46 
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <stddef.h>
50 #include <stdarg.h>
51 #include <fcntl.h>
52 #include <unistd.h>
53 #include <errno.h>
54 #include <string.h>
55 #include <strings.h>
56 #include <synch.h>
57 #include <thread.h>
58 #include <assert.h>
59 #include <alloca.h>
60 #include <umem.h>
61 #include <limits.h>
62 #include <atomic.h>
63 #include <dirent.h>
64 #include <time.h>
65 #include <sys/note.h>
66 #include <sys/types.h>
67 #include <sys/sysmacros.h>
68 #include <sys/bitmap.h>
69 #include <sys/resource.h>
70 #include <sys/byteorder.h>
71 #include <sys/list.h>
72 #include <sys/uio.h>
73 #include <sys/zfs_debug.h>
74 #include <sys/sdt.h>
75 
76 /*
77  * Debugging
78  */
79 
80 /*
81  * Note that we are not using the debugging levels.
82  */
83 
84 #define	CE_CONT		0	/* continuation		*/
85 #define	CE_NOTE		1	/* notice		*/
86 #define	CE_WARN		2	/* warning		*/
87 #define	CE_PANIC	3	/* panic		*/
88 #define	CE_IGNORE	4	/* print nothing	*/
89 
90 /*
91  * ZFS debugging
92  */
93 
94 #ifdef ZFS_DEBUG
95 extern void dprintf_setup(int *argc, char **argv);
96 #endif /* ZFS_DEBUG */
97 
98 extern void cmn_err(int, const char *, ...);
99 extern void panic(const char *, ...);
100 extern void vpanic(const char *, __va_list);
101 
102 /* This definition is copied from assert.h. */
103 #if defined(__STDC__)
104 #if __STDC_VERSION__ - 0 >= 199901L
105 #define	verify(EX) (void)((EX) || \
106 	(__assert_c99(#EX, __FILE__, __LINE__, __func__), 0))
107 #else
108 #define	verify(EX) (void)((EX) || (__assert(#EX, __FILE__, __LINE__), 0))
109 #endif /* __STDC_VERSION__ - 0 >= 199901L */
110 #else
111 #define	verify(EX) (void)((EX) || (_assert("EX", __FILE__, __LINE__), 0))
112 #endif	/* __STDC__ */
113 
114 
115 #define	VERIFY	verify
116 #define	ASSERT	assert
117 
118 extern void __assert(const char *, const char *, int);
119 
120 #ifdef lint
121 #define	VERIFY3_IMPL(x, y, z, t)	if (x == z) ((void)0)
122 #else
123 /* BEGIN CSTYLED */
124 #define	VERIFY3_IMPL(LEFT, OP, RIGHT, TYPE) do { \
125 	const TYPE __left = (TYPE)(LEFT); \
126 	const TYPE __right = (TYPE)(RIGHT); \
127 	if (!(__left OP __right)) { \
128 		char *__buf = alloca(256); \
129 		(void) snprintf(__buf, 256, "%s %s %s (0x%llx %s 0x%llx)", \
130 			#LEFT, #OP, #RIGHT, \
131 			(u_longlong_t)__left, #OP, (u_longlong_t)__right); \
132 		__assert(__buf, __FILE__, __LINE__); \
133 	} \
134 _NOTE(CONSTCOND) } while (0)
135 /* END CSTYLED */
136 #endif /* lint */
137 
138 #define	VERIFY3S(x, y, z)	VERIFY3_IMPL(x, y, z, int64_t)
139 #define	VERIFY3U(x, y, z)	VERIFY3_IMPL(x, y, z, uint64_t)
140 #define	VERIFY3P(x, y, z)	VERIFY3_IMPL(x, y, z, uintptr_t)
141 
142 #ifdef NDEBUG
143 #define	ASSERT3S(x, y, z)	((void)0)
144 #define	ASSERT3U(x, y, z)	((void)0)
145 #define	ASSERT3P(x, y, z)	((void)0)
146 #else
147 #define	ASSERT3S(x, y, z)	VERIFY3S(x, y, z)
148 #define	ASSERT3U(x, y, z)	VERIFY3U(x, y, z)
149 #define	ASSERT3P(x, y, z)	VERIFY3P(x, y, z)
150 #endif
151 
152 /*
153  * Dtrace SDT probes have different signatures in userland than they do in
154  * kernel.  If they're being used in kernel code, re-define them out of
155  * existence for their counterparts in libzpool.
156  */
157 
158 #ifdef DTRACE_PROBE1
159 #undef	DTRACE_PROBE1
160 #define	DTRACE_PROBE1(a, b, c)	((void)0)
161 #endif	/* DTRACE_PROBE1 */
162 
163 #ifdef DTRACE_PROBE2
164 #undef	DTRACE_PROBE2
165 #define	DTRACE_PROBE2(a, b, c, d, e)	((void)0)
166 #endif	/* DTRACE_PROBE2 */
167 
168 /*
169  * Threads
170  */
171 #define	curthread	((void *)(uintptr_t)thr_self())
172 
173 typedef struct kthread kthread_t;
174 
175 #define	thread_create(stk, stksize, func, arg, len, pp, state, pri)	\
176 	zk_thread_create(func, arg)
177 #define	thread_exit() thr_exit(0)
178 
179 extern kthread_t *zk_thread_create(void (*func)(), void *arg);
180 
181 #define	issig(why)	(FALSE)
182 #define	ISSIG(thr, why)	(FALSE)
183 
184 /*
185  * Mutexes
186  */
187 typedef struct kmutex {
188 	void	*m_owner;
189 	mutex_t	m_lock;
190 } kmutex_t;
191 
192 #define	MUTEX_DEFAULT	USYNC_THREAD
193 #undef MUTEX_HELD
194 #define	MUTEX_HELD(m) _mutex_held(&(m)->m_lock)
195 
196 /*
197  * Argh -- we have to get cheesy here because the kernel and userland
198  * have different signatures for the same routine.
199  */
200 extern int _mutex_init(mutex_t *mp, int type, void *arg);
201 extern int _mutex_destroy(mutex_t *mp);
202 
203 #define	mutex_init(mp, b, c, d)		zmutex_init((kmutex_t *)(mp))
204 #define	mutex_destroy(mp)		zmutex_destroy((kmutex_t *)(mp))
205 
206 extern void zmutex_init(kmutex_t *mp);
207 extern void zmutex_destroy(kmutex_t *mp);
208 extern void mutex_enter(kmutex_t *mp);
209 extern void mutex_exit(kmutex_t *mp);
210 extern int mutex_tryenter(kmutex_t *mp);
211 extern void *mutex_owner(kmutex_t *mp);
212 
213 /*
214  * RW locks
215  */
216 typedef struct krwlock {
217 	void		*rw_owner;
218 	rwlock_t	rw_lock;
219 } krwlock_t;
220 
221 typedef int krw_t;
222 
223 #define	RW_READER	0
224 #define	RW_WRITER	1
225 #define	RW_DEFAULT	USYNC_THREAD
226 
227 #undef RW_READ_HELD
228 #define	RW_READ_HELD(x)		_rw_read_held(&(x)->rw_lock)
229 
230 #undef RW_WRITE_HELD
231 #define	RW_WRITE_HELD(x)	_rw_write_held(&(x)->rw_lock)
232 
233 extern void rw_init(krwlock_t *rwlp, char *name, int type, void *arg);
234 extern void rw_destroy(krwlock_t *rwlp);
235 extern void rw_enter(krwlock_t *rwlp, krw_t rw);
236 extern int rw_tryenter(krwlock_t *rwlp, krw_t rw);
237 extern int rw_tryupgrade(krwlock_t *rwlp);
238 extern void rw_exit(krwlock_t *rwlp);
239 #define	rw_downgrade(rwlp) do { } while (0)
240 
241 /*
242  * Condition variables
243  */
244 typedef cond_t kcondvar_t;
245 
246 #define	CV_DEFAULT	USYNC_THREAD
247 
248 extern void cv_init(kcondvar_t *cv, char *name, int type, void *arg);
249 extern void cv_destroy(kcondvar_t *cv);
250 extern void cv_wait(kcondvar_t *cv, kmutex_t *mp);
251 extern clock_t cv_timedwait(kcondvar_t *cv, kmutex_t *mp, clock_t abstime);
252 extern void cv_signal(kcondvar_t *cv);
253 extern void cv_broadcast(kcondvar_t *cv);
254 
255 /*
256  * Kernel memory
257  */
258 #define	KM_SLEEP		UMEM_NOFAIL
259 #define	KM_NOSLEEP		UMEM_DEFAULT
260 #define	KMC_NODEBUG		UMC_NODEBUG
261 #define	kmem_alloc(_s, _f)	umem_alloc(_s, _f)
262 #define	kmem_zalloc(_s, _f)	umem_zalloc(_s, _f)
263 #define	kmem_free(_b, _s)	umem_free(_b, _s)
264 #define	kmem_cache_create(_a, _b, _c, _d, _e, _f, _g, _h, _i) \
265 	umem_cache_create(_a, _b, _c, _d, _e, _f, _g, _h, _i)
266 #define	kmem_cache_destroy(_c)	umem_cache_destroy(_c)
267 #define	kmem_cache_alloc(_c, _f) umem_cache_alloc(_c, _f)
268 #define	kmem_cache_free(_c, _b)	umem_cache_free(_c, _b)
269 #define	kmem_debugging()	0
270 #define	kmem_cache_reap_now(c)
271 
272 typedef umem_cache_t kmem_cache_t;
273 
274 /*
275  * Task queues
276  */
277 typedef struct taskq taskq_t;
278 typedef uintptr_t taskqid_t;
279 typedef void (task_func_t)(void *);
280 
281 #define	TASKQ_PREPOPULATE	0x0001
282 #define	TASKQ_CPR_SAFE		0x0002	/* Use CPR safe protocol */
283 #define	TASKQ_DYNAMIC		0x0004	/* Use dynamic thread scheduling */
284 
285 #define	TQ_SLEEP	KM_SLEEP	/* Can block for memory */
286 #define	TQ_NOSLEEP	KM_NOSLEEP	/* cannot block for memory; may fail */
287 #define	TQ_NOQUEUE	0x02	/* Do not enqueue if can't dispatch */
288 
289 extern taskq_t	*taskq_create(const char *, int, pri_t, int, int, uint_t);
290 extern taskqid_t taskq_dispatch(taskq_t *, task_func_t, void *, uint_t);
291 extern void	taskq_destroy(taskq_t *);
292 extern void	taskq_wait(taskq_t *);
293 extern int	taskq_member(taskq_t *, void *);
294 
295 /*
296  * vnodes
297  */
298 typedef struct vnode {
299 	uint64_t	v_size;
300 	int		v_fd;
301 	char		*v_path;
302 } vnode_t;
303 
304 typedef struct vattr {
305 	uint_t		va_mask;	/* bit-mask of attributes */
306 	u_offset_t	va_size;	/* file size in bytes */
307 } vattr_t;
308 
309 #define	AT_TYPE		0x0001
310 #define	AT_MODE		0x0002
311 #define	AT_UID		0x0004
312 #define	AT_GID		0x0008
313 #define	AT_FSID		0x0010
314 #define	AT_NODEID	0x0020
315 #define	AT_NLINK	0x0040
316 #define	AT_SIZE		0x0080
317 #define	AT_ATIME	0x0100
318 #define	AT_MTIME	0x0200
319 #define	AT_CTIME	0x0400
320 #define	AT_RDEV		0x0800
321 #define	AT_BLKSIZE	0x1000
322 #define	AT_NBLOCKS	0x2000
323 #define	AT_SEQ		0x8000
324 
325 #define	CRCREAT		0
326 
327 #define	VOP_CLOSE(vp, f, c, o, cr)	0
328 #define	VOP_PUTPAGE(vp, of, sz, fl, cr)	0
329 #define	VOP_GETATTR(vp, vap, fl, cr)	((vap)->va_size = (vp)->v_size, 0)
330 
331 #define	VOP_FSYNC(vp, f, cr)	fsync((vp)->v_fd)
332 
333 #define	VN_RELE(vp)	vn_close(vp)
334 
335 extern int vn_open(char *path, int x1, int oflags, int mode, vnode_t **vpp,
336     int x2, int x3);
337 extern int vn_openat(char *path, int x1, int oflags, int mode, vnode_t **vpp,
338     int x2, int x3, vnode_t *vp);
339 extern int vn_rdwr(int uio, vnode_t *vp, void *addr, ssize_t len,
340     offset_t offset, int x1, int x2, rlim64_t x3, void *x4, ssize_t *residp);
341 extern void vn_close(vnode_t *vp);
342 
343 #define	vn_remove(path, x1, x2)		remove(path)
344 #define	vn_rename(from, to, seg)	rename((from), (to))
345 
346 extern vnode_t *rootdir;
347 
348 #include <sys/file.h>		/* for FREAD, FWRITE, etc */
349 
350 /*
351  * Random stuff
352  */
353 #define	lbolt	(gethrtime() >> 23)
354 #define	lbolt64	(gethrtime() >> 23)
355 #define	hz	119	/* frequency when using gethrtime() >> 23 for lbolt */
356 
357 extern void delay(clock_t ticks);
358 
359 #define	gethrestime_sec() time(NULL)
360 
361 #define	max_ncpus	64
362 
363 #define	minclsyspri	60
364 #define	maxclsyspri	99
365 
366 #define	CPU_SEQID	(thr_self() & (max_ncpus - 1))
367 
368 #define	kcred		NULL
369 #define	CRED()		NULL
370 
371 extern uint64_t physmem;
372 
373 extern int highbit(ulong_t i);
374 extern int random_get_bytes(uint8_t *ptr, size_t len);
375 extern int random_get_pseudo_bytes(uint8_t *ptr, size_t len);
376 
377 extern void kernel_init(int);
378 extern void kernel_fini(void);
379 
380 struct spa;
381 extern void nicenum(uint64_t num, char *buf);
382 extern void show_pool_stats(struct spa *);
383 
384 typedef struct callb_cpr {
385 	kmutex_t	*cc_lockp;
386 } callb_cpr_t;
387 
388 #define	CALLB_CPR_INIT(cp, lockp, func, name)	{		\
389 	(cp)->cc_lockp = lockp;					\
390 }
391 
392 #define	CALLB_CPR_SAFE_BEGIN(cp) {				\
393 	ASSERT(MUTEX_HELD((cp)->cc_lockp));			\
394 }
395 
396 #define	CALLB_CPR_SAFE_END(cp, lockp) {				\
397 	ASSERT(MUTEX_HELD((cp)->cc_lockp));			\
398 }
399 
400 #define	CALLB_CPR_EXIT(cp) {					\
401 	ASSERT(MUTEX_HELD((cp)->cc_lockp));			\
402 	mutex_exit((cp)->cc_lockp);				\
403 }
404 
405 #define	zone_dataset_visible(x, y)	(1)
406 #define	INGLOBALZONE(z)			(1)
407 
408 #ifdef	__cplusplus
409 }
410 #endif
411 
412 #endif	/* _SYS_ZFS_CONTEXT_H */
413