xref: /illumos-gate/usr/src/lib/libzpool/common/sys/zfs_context.h (revision 44cb6abc89aa591c23f5e58296c6d2a29302344a)
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 http://www.opensolaris.org/os/licensing.
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 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #ifndef _SYS_ZFS_CONTEXT_H
27 #define	_SYS_ZFS_CONTEXT_H
28 
29 #pragma ident	"%Z%%M%	%I%	%E% SMI"
30 
31 #ifdef	__cplusplus
32 extern "C" {
33 #endif
34 
35 #define	_SYS_MUTEX_H
36 #define	_SYS_RWLOCK_H
37 #define	_SYS_CONDVAR_H
38 #define	_SYS_SYSTM_H
39 #define	_SYS_DEBUG_H
40 #define	_SYS_T_LOCK_H
41 #define	_SYS_VNODE_H
42 #define	_SYS_VFS_H
43 #define	_SYS_SUNDDI_H
44 #define	_SYS_CALLB_H
45 
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <stddef.h>
49 #include <stdarg.h>
50 #include <fcntl.h>
51 #include <unistd.h>
52 #include <errno.h>
53 #include <string.h>
54 #include <strings.h>
55 #include <synch.h>
56 #include <thread.h>
57 #include <assert.h>
58 #include <alloca.h>
59 #include <umem.h>
60 #include <limits.h>
61 #include <atomic.h>
62 #include <dirent.h>
63 #include <time.h>
64 #include <sys/note.h>
65 #include <sys/types.h>
66 #include <sys/sysmacros.h>
67 #include <sys/bitmap.h>
68 #include <sys/resource.h>
69 #include <sys/byteorder.h>
70 #include <sys/list.h>
71 #include <sys/uio.h>
72 #include <sys/zfs_debug.h>
73 #include <sys/sdt.h>
74 #include <sys/kstat.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 #ifdef DTRACE_PROBE3
169 #undef	DTRACE_PROBE3
170 #define	DTRACE_PROBE3(a, b, c, d, e, f, g)	((void)0)
171 #endif	/* DTRACE_PROBE3 */
172 
173 #ifdef DTRACE_PROBE4
174 #undef	DTRACE_PROBE4
175 #define	DTRACE_PROBE4(a, b, c, d, e, f, g, h, i)	((void)0)
176 #endif	/* DTRACE_PROBE4 */
177 
178 /*
179  * Threads
180  */
181 #define	curthread	((void *)(uintptr_t)thr_self())
182 
183 typedef struct kthread kthread_t;
184 
185 #define	thread_create(stk, stksize, func, arg, len, pp, state, pri)	\
186 	zk_thread_create(func, arg)
187 #define	thread_exit() thr_exit(NULL)
188 
189 extern kthread_t *zk_thread_create(void (*func)(), void *arg);
190 
191 #define	issig(why)	(FALSE)
192 #define	ISSIG(thr, why)	(FALSE)
193 
194 /*
195  * Mutexes
196  */
197 typedef struct kmutex {
198 	void	*m_owner;
199 	mutex_t	m_lock;
200 } kmutex_t;
201 
202 #define	MUTEX_DEFAULT	USYNC_THREAD
203 #undef MUTEX_HELD
204 #define	MUTEX_HELD(m) _mutex_held(&(m)->m_lock)
205 
206 /*
207  * Argh -- we have to get cheesy here because the kernel and userland
208  * have different signatures for the same routine.
209  */
210 extern int _mutex_init(mutex_t *mp, int type, void *arg);
211 extern int _mutex_destroy(mutex_t *mp);
212 
213 #define	mutex_init(mp, b, c, d)		zmutex_init((kmutex_t *)(mp))
214 #define	mutex_destroy(mp)		zmutex_destroy((kmutex_t *)(mp))
215 
216 extern void zmutex_init(kmutex_t *mp);
217 extern void zmutex_destroy(kmutex_t *mp);
218 extern void mutex_enter(kmutex_t *mp);
219 extern void mutex_exit(kmutex_t *mp);
220 extern int mutex_tryenter(kmutex_t *mp);
221 extern void *mutex_owner(kmutex_t *mp);
222 
223 /*
224  * RW locks
225  */
226 typedef struct krwlock {
227 	void		*rw_owner;
228 	rwlock_t	rw_lock;
229 } krwlock_t;
230 
231 typedef int krw_t;
232 
233 #define	RW_READER	0
234 #define	RW_WRITER	1
235 #define	RW_DEFAULT	USYNC_THREAD
236 
237 #undef RW_READ_HELD
238 #define	RW_READ_HELD(x)		_rw_read_held(&(x)->rw_lock)
239 
240 #undef RW_WRITE_HELD
241 #define	RW_WRITE_HELD(x)	_rw_write_held(&(x)->rw_lock)
242 
243 extern void rw_init(krwlock_t *rwlp, char *name, int type, void *arg);
244 extern void rw_destroy(krwlock_t *rwlp);
245 extern void rw_enter(krwlock_t *rwlp, krw_t rw);
246 extern int rw_tryenter(krwlock_t *rwlp, krw_t rw);
247 extern int rw_tryupgrade(krwlock_t *rwlp);
248 extern void rw_exit(krwlock_t *rwlp);
249 #define	rw_downgrade(rwlp) do { } while (0)
250 
251 /*
252  * Condition variables
253  */
254 typedef cond_t kcondvar_t;
255 
256 #define	CV_DEFAULT	USYNC_THREAD
257 
258 extern void cv_init(kcondvar_t *cv, char *name, int type, void *arg);
259 extern void cv_destroy(kcondvar_t *cv);
260 extern void cv_wait(kcondvar_t *cv, kmutex_t *mp);
261 extern clock_t cv_timedwait(kcondvar_t *cv, kmutex_t *mp, clock_t abstime);
262 extern void cv_signal(kcondvar_t *cv);
263 extern void cv_broadcast(kcondvar_t *cv);
264 
265 /*
266  * kstat creation, installation and deletion
267  */
268 extern kstat_t *kstat_create(char *, int,
269     char *, char *, uchar_t, ulong_t, uchar_t);
270 extern void kstat_install(kstat_t *);
271 extern void kstat_delete(kstat_t *);
272 
273 /*
274  * Kernel memory
275  */
276 #define	KM_SLEEP		UMEM_NOFAIL
277 #define	KM_NOSLEEP		UMEM_DEFAULT
278 #define	KMC_NODEBUG		UMC_NODEBUG
279 #define	kmem_alloc(_s, _f)	umem_alloc(_s, _f)
280 #define	kmem_zalloc(_s, _f)	umem_zalloc(_s, _f)
281 #define	kmem_free(_b, _s)	umem_free(_b, _s)
282 #define	kmem_cache_create(_a, _b, _c, _d, _e, _f, _g, _h, _i) \
283 	umem_cache_create(_a, _b, _c, _d, _e, _f, _g, _h, _i)
284 #define	kmem_cache_destroy(_c)	umem_cache_destroy(_c)
285 #define	kmem_cache_alloc(_c, _f) umem_cache_alloc(_c, _f)
286 #define	kmem_cache_free(_c, _b)	umem_cache_free(_c, _b)
287 #define	kmem_debugging()	0
288 #define	kmem_cache_reap_now(c)
289 
290 typedef umem_cache_t kmem_cache_t;
291 
292 /*
293  * Task queues
294  */
295 typedef struct taskq taskq_t;
296 typedef uintptr_t taskqid_t;
297 typedef void (task_func_t)(void *);
298 
299 #define	TASKQ_PREPOPULATE	0x0001
300 #define	TASKQ_CPR_SAFE		0x0002	/* Use CPR safe protocol */
301 #define	TASKQ_DYNAMIC		0x0004	/* Use dynamic thread scheduling */
302 
303 #define	TQ_SLEEP	KM_SLEEP	/* Can block for memory */
304 #define	TQ_NOSLEEP	KM_NOSLEEP	/* cannot block for memory; may fail */
305 #define	TQ_NOQUEUE	0x02	/* Do not enqueue if can't dispatch */
306 
307 extern taskq_t	*taskq_create(const char *, int, pri_t, int, int, uint_t);
308 extern taskqid_t taskq_dispatch(taskq_t *, task_func_t, void *, uint_t);
309 extern void	taskq_destroy(taskq_t *);
310 extern void	taskq_wait(taskq_t *);
311 extern int	taskq_member(taskq_t *, void *);
312 
313 /*
314  * vnodes
315  */
316 typedef struct vnode {
317 	uint64_t	v_size;
318 	int		v_fd;
319 	char		*v_path;
320 } vnode_t;
321 
322 typedef struct vattr {
323 	uint_t		va_mask;	/* bit-mask of attributes */
324 	u_offset_t	va_size;	/* file size in bytes */
325 } vattr_t;
326 
327 #define	AT_TYPE		0x0001
328 #define	AT_MODE		0x0002
329 #define	AT_UID		0x0004
330 #define	AT_GID		0x0008
331 #define	AT_FSID		0x0010
332 #define	AT_NODEID	0x0020
333 #define	AT_NLINK	0x0040
334 #define	AT_SIZE		0x0080
335 #define	AT_ATIME	0x0100
336 #define	AT_MTIME	0x0200
337 #define	AT_CTIME	0x0400
338 #define	AT_RDEV		0x0800
339 #define	AT_BLKSIZE	0x1000
340 #define	AT_NBLOCKS	0x2000
341 #define	AT_SEQ		0x8000
342 
343 #define	CRCREAT		0
344 
345 #define	VOP_CLOSE(vp, f, c, o, cr)	0
346 #define	VOP_PUTPAGE(vp, of, sz, fl, cr)	0
347 #define	VOP_GETATTR(vp, vap, fl, cr)	((vap)->va_size = (vp)->v_size, 0)
348 
349 #define	VOP_FSYNC(vp, f, cr)	fsync((vp)->v_fd)
350 
351 #define	VN_RELE(vp)	vn_close(vp)
352 
353 extern int vn_open(char *path, int x1, int oflags, int mode, vnode_t **vpp,
354     int x2, int x3);
355 extern int vn_openat(char *path, int x1, int oflags, int mode, vnode_t **vpp,
356     int x2, int x3, vnode_t *vp);
357 extern int vn_rdwr(int uio, vnode_t *vp, void *addr, ssize_t len,
358     offset_t offset, int x1, int x2, rlim64_t x3, void *x4, ssize_t *residp);
359 extern void vn_close(vnode_t *vp);
360 
361 #define	vn_remove(path, x1, x2)		remove(path)
362 #define	vn_rename(from, to, seg)	rename((from), (to))
363 #define	vn_is_readonly(vp)		B_FALSE
364 
365 extern vnode_t *rootdir;
366 
367 #include <sys/file.h>		/* for FREAD, FWRITE, etc */
368 
369 /*
370  * Random stuff
371  */
372 #define	lbolt	(gethrtime() >> 23)
373 #define	lbolt64	(gethrtime() >> 23)
374 #define	hz	119	/* frequency when using gethrtime() >> 23 for lbolt */
375 
376 extern void delay(clock_t ticks);
377 
378 #define	gethrestime_sec() time(NULL)
379 
380 #define	max_ncpus	64
381 
382 #define	minclsyspri	60
383 #define	maxclsyspri	99
384 
385 #define	CPU_SEQID	(thr_self() & (max_ncpus - 1))
386 
387 #define	kcred		NULL
388 #define	CRED()		NULL
389 
390 extern uint64_t physmem;
391 
392 extern int highbit(ulong_t i);
393 extern int random_get_bytes(uint8_t *ptr, size_t len);
394 extern int random_get_pseudo_bytes(uint8_t *ptr, size_t len);
395 
396 extern void kernel_init(int);
397 extern void kernel_fini(void);
398 
399 struct spa;
400 extern void nicenum(uint64_t num, char *buf);
401 extern void show_pool_stats(struct spa *);
402 
403 typedef struct callb_cpr {
404 	kmutex_t	*cc_lockp;
405 } callb_cpr_t;
406 
407 #define	CALLB_CPR_INIT(cp, lockp, func, name)	{		\
408 	(cp)->cc_lockp = lockp;					\
409 }
410 
411 #define	CALLB_CPR_SAFE_BEGIN(cp) {				\
412 	ASSERT(MUTEX_HELD((cp)->cc_lockp));			\
413 }
414 
415 #define	CALLB_CPR_SAFE_END(cp, lockp) {				\
416 	ASSERT(MUTEX_HELD((cp)->cc_lockp));			\
417 }
418 
419 #define	CALLB_CPR_EXIT(cp) {					\
420 	ASSERT(MUTEX_HELD((cp)->cc_lockp));			\
421 	mutex_exit((cp)->cc_lockp);				\
422 }
423 
424 #define	zone_dataset_visible(x, y)	(1)
425 #define	INGLOBALZONE(z)			(1)
426 
427 #ifdef	__cplusplus
428 }
429 #endif
430 
431 /* ZFS Boot Related stuff. */
432 
433 struct _buf {
434 	intptr_t	_fd;
435 };
436 
437 struct bootstat {
438 	uint64_t st_size;
439 };
440 
441 extern struct _buf *kobj_open_file(char *name);
442 extern int kobj_read_file(struct _buf *file, char *buf, unsigned size,
443     unsigned off);
444 extern void kobj_close_file(struct _buf *file);
445 extern int kobj_fstat(intptr_t, struct bootstat *);
446 
447 
448 #endif	/* _SYS_ZFS_CONTEXT_H */
449