zfs_context.h revision 168404
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
32extern "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#define	_SYS_SCHED_H_
46
47#include <solaris.h>
48#include <stdio.h>
49#include <stdlib.h>
50#include <stddef.h>
51#include <stdarg.h>
52#include <fcntl.h>
53#include <unistd.h>
54#include <errno.h>
55#include <string.h>
56#include <strings.h>
57#include <thread.h>
58#include <assert.h>
59#include <limits.h>
60#include <dirent.h>
61#include <time.h>
62#include <math.h>
63#include <umem.h>
64#include <vmem.h>
65#include <fsshare.h>
66#include <sys/note.h>
67#include <sys/types.h>
68#include <sys/atomic.h>
69#include <sys/sysmacros.h>
70#include <sys/bitmap.h>
71#include <sys/resource.h>
72#include <sys/byteorder.h>
73#include <sys/list.h>
74#include <sys/time.h>
75#include <sys/uio.h>
76#include <sys/mntent.h>
77#include <sys/mnttab.h>
78#include <sys/zfs_debug.h>
79#include <sys/debug.h>
80#include <sys/sdt.h>
81#include <sys/kstat.h>
82#include <sys/kernel.h>
83#include <machine/atomic.h>
84
85#define	ZFS_EXPORTS_PATH	"/etc/zfs/exports"
86
87/*
88 * Debugging
89 */
90
91/*
92 * Note that we are not using the debugging levels.
93 */
94
95#define	CE_CONT		0	/* continuation		*/
96#define	CE_NOTE		1	/* notice		*/
97#define	CE_WARN		2	/* warning		*/
98#define	CE_PANIC	3	/* panic		*/
99#define	CE_IGNORE	4	/* print nothing	*/
100
101/*
102 * ZFS debugging
103 */
104
105#define	ZFS_LOG(...)	do {  } while (0)
106
107typedef u_longlong_t      rlim64_t;
108#define	RLIM64_INFINITY	((rlim64_t)-3)
109
110#ifdef ZFS_DEBUG
111extern void dprintf_setup(int *argc, char **argv);
112#endif /* ZFS_DEBUG */
113
114extern void cmn_err(int, const char *, ...);
115extern void vcmn_err(int, const char *, __va_list);
116extern void panic(const char *, ...);
117extern void vpanic(const char *, __va_list);
118
119/* This definition is copied from assert.h. */
120#if defined(__STDC__)
121#if __STDC_VERSION__ - 0 >= 199901L
122#define	verify(EX) (void)((EX) || \
123	(__assert_c99(#EX, __FILE__, __LINE__, __func__), 0))
124#else
125#define	verify(EX) (void)((EX) || (__assert(#EX, __FILE__, __LINE__), 0))
126#endif /* __STDC_VERSION__ - 0 >= 199901L */
127#else
128#define	verify(EX) (void)((EX) || (_assert("EX", __FILE__, __LINE__), 0))
129#endif	/* __STDC__ */
130
131
132#define	VERIFY	verify
133#define	ASSERT	assert
134
135extern void __assert(const char *, const char *, int);
136
137#ifdef lint
138#define	VERIFY3_IMPL(x, y, z, t)	if (x == z) ((void)0)
139#else
140/* BEGIN CSTYLED */
141#define	VERIFY3_IMPL(LEFT, OP, RIGHT, TYPE) do { \
142	const TYPE __left = (TYPE)(LEFT); \
143	const TYPE __right = (TYPE)(RIGHT); \
144	if (!(__left OP __right)) { \
145		char *__buf = alloca(256); \
146		(void) snprintf(__buf, 256, "%s %s %s (0x%llx %s 0x%llx)", \
147			#LEFT, #OP, #RIGHT, \
148			(u_longlong_t)__left, #OP, (u_longlong_t)__right); \
149		__assert(__buf, __FILE__, __LINE__); \
150	} \
151_NOTE(CONSTCOND) } while (0)
152/* END CSTYLED */
153#endif /* lint */
154
155#define	VERIFY3S(x, y, z)	VERIFY3_IMPL(x, y, z, int64_t)
156#define	VERIFY3U(x, y, z)	VERIFY3_IMPL(x, y, z, uint64_t)
157#define	VERIFY3P(x, y, z)	VERIFY3_IMPL(x, y, z, uintptr_t)
158
159#ifdef NDEBUG
160#define	ASSERT3S(x, y, z)	((void)0)
161#define	ASSERT3U(x, y, z)	((void)0)
162#define	ASSERT3P(x, y, z)	((void)0)
163#else
164#define	ASSERT3S(x, y, z)	VERIFY3S(x, y, z)
165#define	ASSERT3U(x, y, z)	VERIFY3U(x, y, z)
166#define	ASSERT3P(x, y, z)	VERIFY3P(x, y, z)
167#endif
168
169/*
170 * Dtrace SDT probes have different signatures in userland than they do in
171 * kernel.  If they're being used in kernel code, re-define them out of
172 * existence for their counterparts in libzpool.
173 */
174
175#ifdef DTRACE_PROBE1
176#undef	DTRACE_PROBE1
177#define	DTRACE_PROBE1(a, b, c)	((void)0)
178#endif	/* DTRACE_PROBE1 */
179
180#ifdef DTRACE_PROBE2
181#undef	DTRACE_PROBE2
182#define	DTRACE_PROBE2(a, b, c, d, e)	((void)0)
183#endif	/* DTRACE_PROBE2 */
184
185#ifdef DTRACE_PROBE3
186#undef	DTRACE_PROBE3
187#define	DTRACE_PROBE3(a, b, c, d, e, f, g)	((void)0)
188#endif	/* DTRACE_PROBE3 */
189
190#ifdef DTRACE_PROBE4
191#undef	DTRACE_PROBE4
192#define	DTRACE_PROBE4(a, b, c, d, e, f, g, h, i)	((void)0)
193#endif	/* DTRACE_PROBE4 */
194
195/*
196 * Threads
197 */
198#define	curthread	((void *)(uintptr_t)thr_self())
199
200typedef struct kthread kthread_t;
201
202#define	thread_create(stk, stksize, func, arg, len, pp, state, pri)	\
203	zk_thread_create(func, arg)
204#define	thread_exit() thr_exit(NULL)
205
206extern kthread_t *zk_thread_create(void (*func)(), void *arg);
207
208#define	issig(why)	(FALSE)
209#define	ISSIG(thr, why)	(FALSE)
210
211/*
212 * Mutexes
213 */
214typedef struct kmutex {
215	void	*m_owner;
216	mutex_t	m_lock;
217} kmutex_t;
218
219#define	MUTEX_DEFAULT	USYNC_THREAD
220#undef MUTEX_HELD
221#define	MUTEX_HELD(m)	((m)->m_owner == curthread)
222
223/*
224 * Argh -- we have to get cheesy here because the kernel and userland
225 * have different signatures for the same routine.
226 */
227//extern int _mutex_init(mutex_t *mp, int type, void *arg);
228//extern int _mutex_destroy(mutex_t *mp);
229
230#define	mutex_init(mp, b, c, d)		zmutex_init((kmutex_t *)(mp))
231#define	mutex_destroy(mp)		zmutex_destroy((kmutex_t *)(mp))
232
233extern void zmutex_init(kmutex_t *mp);
234extern void zmutex_destroy(kmutex_t *mp);
235extern void mutex_enter(kmutex_t *mp);
236extern void mutex_exit(kmutex_t *mp);
237extern int mutex_tryenter(kmutex_t *mp);
238extern void *mutex_owner(kmutex_t *mp);
239
240/*
241 * RW locks
242 */
243typedef struct krwlock {
244	int		rw_count;
245	void		*rw_owner;
246	rwlock_t	rw_lock;
247} krwlock_t;
248
249typedef int krw_t;
250
251#define	RW_READER	0
252#define	RW_WRITER	1
253#define	RW_DEFAULT	USYNC_THREAD
254
255#undef RW_READ_HELD
256
257#undef RW_WRITE_HELD
258#define	RW_WRITE_HELD(x)	((x)->rw_owner == curthread)
259#define	RW_LOCK_HELD(x)		rw_lock_held(x)
260
261extern void rw_init(krwlock_t *rwlp, char *name, int type, void *arg);
262extern void rw_destroy(krwlock_t *rwlp);
263extern void rw_enter(krwlock_t *rwlp, krw_t rw);
264extern int rw_tryenter(krwlock_t *rwlp, krw_t rw);
265extern int rw_tryupgrade(krwlock_t *rwlp);
266extern void rw_exit(krwlock_t *rwlp);
267extern int rw_lock_held(krwlock_t *rwlp);
268#define	rw_downgrade(rwlp) do { } while (0)
269
270/*
271 * Condition variables
272 */
273typedef cond_t kcondvar_t;
274
275#define	CV_DEFAULT	USYNC_THREAD
276
277extern void cv_init(kcondvar_t *cv, char *name, int type, void *arg);
278extern void cv_destroy(kcondvar_t *cv);
279extern void cv_wait(kcondvar_t *cv, kmutex_t *mp);
280extern clock_t cv_timedwait(kcondvar_t *cv, kmutex_t *mp, clock_t abstime);
281extern void cv_signal(kcondvar_t *cv);
282extern void cv_broadcast(kcondvar_t *cv);
283
284/*
285 * Kernel memory
286 */
287#define	KM_SLEEP		UMEM_NOFAIL
288#define	KM_NOSLEEP		UMEM_DEFAULT
289#define	KMC_NODEBUG		UMC_NODEBUG
290#define	kmem_alloc(_s, _f)	umem_alloc(_s, _f)
291#define	kmem_zalloc(_s, _f)	umem_zalloc(_s, _f)
292#define	kmem_free(_b, _s)	umem_free(_b, _s)
293#define	kmem_cache_create(_a, _b, _c, _d, _e, _f, _g, _h, _i) \
294	umem_cache_create(_a, _b, _c, _d, _e, _f, _g, _h, _i)
295#define	kmem_cache_destroy(_c)	umem_cache_destroy(_c)
296#define	kmem_cache_alloc(_c, _f) umem_cache_alloc(_c, _f)
297#define	kmem_cache_free(_c, _b)	umem_cache_free(_c, _b)
298#define	kmem_debugging()	0
299#define	kmem_cache_reap_now(c)
300
301typedef umem_cache_t kmem_cache_t;
302
303/*
304 * Task queues
305 */
306typedef struct taskq taskq_t;
307typedef uintptr_t taskqid_t;
308typedef void (task_func_t)(void *);
309
310#define	TASKQ_PREPOPULATE	0x0001
311#define	TASKQ_CPR_SAFE		0x0002	/* Use CPR safe protocol */
312#define	TASKQ_DYNAMIC		0x0004	/* Use dynamic thread scheduling */
313
314#define	TQ_SLEEP	KM_SLEEP	/* Can block for memory */
315#define	TQ_NOSLEEP	KM_NOSLEEP	/* cannot block for memory; may fail */
316#define	TQ_NOQUEUE	0x02	/* Do not enqueue if can't dispatch */
317
318extern taskq_t	*taskq_create(const char *, int, pri_t, int, int, uint_t);
319extern taskqid_t taskq_dispatch(taskq_t *, task_func_t, void *, uint_t);
320extern void	taskq_destroy(taskq_t *);
321extern void	taskq_wait(taskq_t *);
322extern int	taskq_member(taskq_t *, void *);
323
324/*
325 * vnodes
326 */
327typedef struct vnode {
328	uint64_t	v_size;
329	int		v_fd;
330	char		*v_path;
331} vnode_t;
332
333typedef struct vattr {
334	uint_t		va_mask;	/* bit-mask of attributes */
335	u_offset_t	va_size;	/* file size in bytes */
336} vattr_t;
337
338#define	AT_TYPE		0x0001
339#define	AT_MODE		0x0002
340#define	AT_UID		0x0004
341#define	AT_GID		0x0008
342#define	AT_FSID		0x0010
343#define	AT_NODEID	0x0020
344#define	AT_NLINK	0x0040
345#define	AT_SIZE		0x0080
346#define	AT_ATIME	0x0100
347#define	AT_MTIME	0x0200
348#define	AT_CTIME	0x0400
349#define	AT_RDEV		0x0800
350#define	AT_BLKSIZE	0x1000
351#define	AT_NBLOCKS	0x2000
352#define	AT_SEQ		0x8000
353
354#define	CRCREAT		0
355
356#define	VOP_CLOSE(vp, f, c, o, cr)	0
357#define	VOP_PUTPAGE(vp, of, sz, fl, cr)	0
358#define	VOP_GETATTR(vp, vap, fl, cr)	((vap)->va_size = (vp)->v_size, 0)
359
360#define	VOP_FSYNC(vp, f, cr)	fsync((vp)->v_fd)
361
362#define	VN_RELE(vp)	vn_close(vp)
363
364extern int vn_open(char *path, int x1, int oflags, int mode, vnode_t **vpp,
365    int x2, int x3);
366extern int vn_openat(char *path, int x1, int oflags, int mode, vnode_t **vpp,
367    int x2, int x3, vnode_t *vp);
368extern int vn_rdwr(int uio, vnode_t *vp, void *addr, ssize_t len,
369    offset_t offset, int x1, int x2, rlim64_t x3, void *x4, ssize_t *residp);
370extern void vn_close(vnode_t *vp);
371
372#define	vn_remove(path, x1, x2)		remove(path)
373#define	vn_rename(from, to, seg)	rename((from), (to))
374#define	vn_is_readonly(vp)		B_FALSE
375
376extern vnode_t *rootdir;
377
378#include <sys/file.h>		/* for FREAD, FWRITE, etc */
379#define	FTRUNC	O_TRUNC
380
381/*
382 * Random stuff
383 */
384#define	lbolt	(gethrtime() >> 23)
385#define	lbolt64	(gethrtime() >> 23)
386//#define	hz	119	/* frequency when using gethrtime() >> 23 for lbolt */
387
388extern void delay(clock_t ticks);
389
390#define	gethrestime_sec() time(NULL)
391
392#define	max_ncpus	64
393
394#define	minclsyspri	60
395#define	maxclsyspri	99
396
397#define	CPU_SEQID	(thr_self() & (max_ncpus - 1))
398
399#define	kcred		NULL
400#define	CRED()		NULL
401
402extern uint64_t physmem;
403
404extern int highbit(ulong_t i);
405extern int random_get_bytes(uint8_t *ptr, size_t len);
406extern int random_get_pseudo_bytes(uint8_t *ptr, size_t len);
407
408extern void kernel_init(int);
409extern void kernel_fini(void);
410
411struct spa;
412extern void nicenum(uint64_t num, char *buf);
413extern void show_pool_stats(struct spa *);
414
415typedef struct callb_cpr {
416	kmutex_t	*cc_lockp;
417} callb_cpr_t;
418
419#define	CALLB_CPR_INIT(cp, lockp, func, name)	{		\
420	(cp)->cc_lockp = lockp;					\
421}
422
423#define	CALLB_CPR_SAFE_BEGIN(cp) {				\
424	ASSERT(MUTEX_HELD((cp)->cc_lockp));			\
425}
426
427#define	CALLB_CPR_SAFE_END(cp, lockp) {				\
428	ASSERT(MUTEX_HELD((cp)->cc_lockp));			\
429}
430
431#define	CALLB_CPR_EXIT(cp) {					\
432	ASSERT(MUTEX_HELD((cp)->cc_lockp));			\
433	mutex_exit((cp)->cc_lockp);				\
434}
435
436#define	zone_dataset_visible(x, y)	(1)
437#define	INGLOBALZONE(z)			(1)
438
439#ifdef	__cplusplus
440}
441#endif
442
443/* ZFS Boot Related stuff. */
444
445struct _buf {
446	intptr_t	_fd;
447};
448
449struct bootstat {
450	uint64_t st_size;
451};
452
453extern struct _buf *kobj_open_file(char *name);
454extern int kobj_read_file(struct _buf *file, char *buf, unsigned size,
455    unsigned off);
456extern void kobj_close_file(struct _buf *file);
457extern int kobj_get_filesize(struct _buf *file, uint64_t *size);
458/* Random compatibility stuff. */
459#define	lbolt	(gethrtime() >> 23)
460#define	lbolt64	(gethrtime() >> 23)
461
462extern int hz;
463extern uint64_t physmem;
464
465#define	gethrestime_sec()	time(NULL)
466
467#define	open64(...)		open(__VA_ARGS__)
468#define	pread64(d, p, n, o)	pread(d, p, n, o)
469#define	pwrite64(d, p, n, o)	pwrite(d, p, n, o)
470#define	readdir64(d)		readdir(d)
471#define	SIGPENDING(td)		(0)
472#define	root_mount_wait()	do { } while (0)
473
474struct file {
475	void *dummy;
476};
477
478#define	FCREAT	O_CREAT
479#define	FOFFMAX	0x0
480
481#define	SX_SYSINIT(name, lock, desc)
482
483#define	SYSCTL_DECL(...)
484#define	SYSCTL_INT(...)
485#define	SYSCTL_NODE(...)
486#ifdef TUNABLE_INT
487#undef TUNABLE_INT
488#endif
489#define	TUNABLE_INT(...)
490
491/* Errors */
492
493#ifndef	ERESTART
494#define	ERESTART	(-1)
495#endif
496
497#endif	/* _SYS_ZFS_CONTEXT_H */
498