zfs_context.h revision 182383
1224090Sdougb/*
2224090Sdougb * CDDL HEADER START
3224090Sdougb *
4224090Sdougb * The contents of this file are subject to the terms of the
5224090Sdougb * Common Development and Distribution License (the "License").
6224090Sdougb * You may not use this file except in compliance with the License.
7224090Sdougb *
8224090Sdougb * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9224090Sdougb * or http://www.opensolaris.org/os/licensing.
10224090Sdougb * See the License for the specific language governing permissions
11224090Sdougb * and limitations under the License.
12224090Sdougb *
13224090Sdougb * When distributing Covered Code, include this CDDL HEADER in each
14224090Sdougb * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15224090Sdougb * If applicable, add the following below this CDDL HEADER, with the
16234010Sdougb * fields enclosed by brackets "[]" replaced with your own identifying
17224090Sdougb * information: Portions Copyright [yyyy] [name of copyright owner]
18224090Sdougb *
19224090Sdougb * CDDL HEADER END
20224090Sdougb */
21224090Sdougb/*
22224090Sdougb * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23224090Sdougb * Use is subject to license terms.
24224090Sdougb */
25224090Sdougb
26224090Sdougb#ifndef _SYS_ZFS_CONTEXT_H
27224090Sdougb#define	_SYS_ZFS_CONTEXT_H
28224090Sdougb
29224090Sdougb#pragma ident	"%Z%%M%	%I%	%E% SMI"
30224090Sdougb
31224090Sdougb#ifdef	__cplusplus
32224090Sdougbextern "C" {
33224090Sdougb#endif
34234010Sdougb
35224090Sdougb#define	_SYS_MUTEX_H
36224090Sdougb#define	_SYS_RWLOCK_H
37224090Sdougb#define	_SYS_CONDVAR_H
38224090Sdougb#define	_SYS_SYSTM_H
39224090Sdougb#define	_SYS_DEBUG_H
40224090Sdougb#define	_SYS_T_LOCK_H
41234010Sdougb#define	_SYS_VNODE_H
42224090Sdougb#define	_SYS_VFS_H
43224090Sdougb#define	_SYS_SUNDDI_H
44224090Sdougb#define	_SYS_CALLB_H
45224090Sdougb#define	_SYS_SCHED_H_
46224090Sdougb
47234010Sdougb#include <solaris.h>
48224090Sdougb#include <stdio.h>
49224090Sdougb#include <stdlib.h>
50224090Sdougb#include <stddef.h>
51224090Sdougb#include <stdarg.h>
52224090Sdougb#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 <fsshare.h>
65#include <sys/note.h>
66#include <sys/types.h>
67#include <sys/atomic.h>
68#include <sys/sysmacros.h>
69#include <sys/bitmap.h>
70#include <sys/resource.h>
71#include <sys/byteorder.h>
72#include <sys/list.h>
73#include <sys/time.h>
74#include <sys/uio.h>
75#include <sys/mntent.h>
76#include <sys/mnttab.h>
77#include <sys/zfs_debug.h>
78#include <sys/debug.h>
79#include <sys/sdt.h>
80#include <sys/kstat.h>
81#include <sys/kernel.h>
82#include <sys/disk.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_size()		(physmem * PAGESIZE)
294#define	kmem_cache_create(_a, _b, _c, _d, _e, _f, _g, _h, _i) \
295	umem_cache_create(_a, _b, _c, _d, _e, _f, _g, _h, _i)
296#define	kmem_cache_destroy(_c)	umem_cache_destroy(_c)
297#define	kmem_cache_alloc(_c, _f) umem_cache_alloc(_c, _f)
298#define	kmem_cache_free(_c, _b)	umem_cache_free(_c, _b)
299#define	kmem_debugging()	0
300#define	kmem_cache_reap_now(c)
301
302typedef umem_cache_t kmem_cache_t;
303
304/*
305 * Task queues
306 */
307typedef struct taskq taskq_t;
308typedef uintptr_t taskqid_t;
309typedef void (task_func_t)(void *);
310
311#define	TASKQ_PREPOPULATE	0x0001
312#define	TASKQ_CPR_SAFE		0x0002	/* Use CPR safe protocol */
313#define	TASKQ_DYNAMIC		0x0004	/* Use dynamic thread scheduling */
314
315#define	TQ_SLEEP	KM_SLEEP	/* Can block for memory */
316#define	TQ_NOSLEEP	KM_NOSLEEP	/* cannot block for memory; may fail */
317#define	TQ_NOQUEUE	0x02	/* Do not enqueue if can't dispatch */
318
319extern taskq_t	*taskq_create(const char *, int, pri_t, int, int, uint_t);
320extern taskqid_t taskq_dispatch(taskq_t *, task_func_t, void *, uint_t);
321extern void	taskq_destroy(taskq_t *);
322extern void	taskq_wait(taskq_t *);
323extern int	taskq_member(taskq_t *, void *);
324
325/*
326 * vnodes
327 */
328typedef struct vnode {
329	uint64_t	v_size;
330	int		v_fd;
331	char		*v_path;
332} vnode_t;
333
334typedef struct vattr {
335	uint_t		va_mask;	/* bit-mask of attributes */
336	u_offset_t	va_size;	/* file size in bytes */
337} vattr_t;
338
339#define	AT_TYPE		0x0001
340#define	AT_MODE		0x0002
341#define	AT_UID		0x0004
342#define	AT_GID		0x0008
343#define	AT_FSID		0x0010
344#define	AT_NODEID	0x0020
345#define	AT_NLINK	0x0040
346#define	AT_SIZE		0x0080
347#define	AT_ATIME	0x0100
348#define	AT_MTIME	0x0200
349#define	AT_CTIME	0x0400
350#define	AT_RDEV		0x0800
351#define	AT_BLKSIZE	0x1000
352#define	AT_NBLOCKS	0x2000
353#define	AT_SEQ		0x8000
354
355#define	CRCREAT		0
356
357#define	VOP_CLOSE(vp, f, c, o, cr)	0
358#define	VOP_PUTPAGE(vp, of, sz, fl, cr)	0
359#define	VOP_GETATTR(vp, vap, fl)	((vap)->va_size = (vp)->v_size, 0)
360
361#define	VOP_FSYNC(vp, f, cr)	fsync((vp)->v_fd)
362
363#define	VN_RELE(vp)	vn_close(vp)
364
365extern int vn_open(char *path, int x1, int oflags, int mode, vnode_t **vpp,
366    int x2, int x3);
367extern int vn_openat(char *path, int x1, int oflags, int mode, vnode_t **vpp,
368    int x2, int x3, vnode_t *vp);
369extern int vn_rdwr(int uio, vnode_t *vp, void *addr, ssize_t len,
370    offset_t offset, int x1, int x2, rlim64_t x3, void *x4, ssize_t *residp);
371extern void vn_close(vnode_t *vp);
372
373#define	vn_remove(path, x1, x2)		remove(path)
374#define	vn_rename(from, to, seg)	rename((from), (to))
375#define	vn_is_readonly(vp)		B_FALSE
376
377extern vnode_t *rootdir;
378
379#include <sys/file.h>		/* for FREAD, FWRITE, etc */
380#define	FTRUNC	O_TRUNC
381
382/*
383 * Random stuff
384 */
385#define	lbolt	(gethrtime() >> 23)
386#define	lbolt64	(gethrtime() >> 23)
387//#define	hz	119	/* frequency when using gethrtime() >> 23 for lbolt */
388
389extern void delay(clock_t ticks);
390
391#define	gethrestime_sec() time(NULL)
392
393#define	max_ncpus	64
394
395#define	minclsyspri	60
396#define	maxclsyspri	99
397
398#define	CPU_SEQID	(thr_self() & (max_ncpus - 1))
399
400#define	kcred		NULL
401#define	CRED()		NULL
402
403extern uint64_t physmem;
404
405extern int highbit(ulong_t i);
406extern int random_get_bytes(uint8_t *ptr, size_t len);
407extern int random_get_pseudo_bytes(uint8_t *ptr, size_t len);
408
409extern void kernel_init(int);
410extern void kernel_fini(void);
411
412struct spa;
413extern void nicenum(uint64_t num, char *buf);
414extern void show_pool_stats(struct spa *);
415
416typedef struct callb_cpr {
417	kmutex_t	*cc_lockp;
418} callb_cpr_t;
419
420#define	CALLB_CPR_INIT(cp, lockp, func, name)	{		\
421	(cp)->cc_lockp = lockp;					\
422}
423
424#define	CALLB_CPR_SAFE_BEGIN(cp) {				\
425	ASSERT(MUTEX_HELD((cp)->cc_lockp));			\
426}
427
428#define	CALLB_CPR_SAFE_END(cp, lockp) {				\
429	ASSERT(MUTEX_HELD((cp)->cc_lockp));			\
430}
431
432#define	CALLB_CPR_EXIT(cp) {					\
433	ASSERT(MUTEX_HELD((cp)->cc_lockp));			\
434	mutex_exit((cp)->cc_lockp);				\
435}
436
437#define	zone_dataset_visible(x, y)	(1)
438#define	INGLOBALZONE(z)			(1)
439
440/*
441 * Hostname information
442 */
443extern struct utsname utsname;
444extern char hw_serial[];
445extern int ddi_strtoul(const char *str, char **nptr, int base,
446    unsigned long *result);
447
448/* ZFS Boot Related stuff. */
449
450struct _buf {
451	intptr_t	_fd;
452};
453
454struct bootstat {
455	uint64_t st_size;
456};
457
458extern struct _buf *kobj_open_file(char *name);
459extern int kobj_read_file(struct _buf *file, char *buf, unsigned size,
460    unsigned off);
461extern void kobj_close_file(struct _buf *file);
462extern int kobj_get_filesize(struct _buf *file, uint64_t *size);
463/* Random compatibility stuff. */
464#define	lbolt	(gethrtime() >> 23)
465#define	lbolt64	(gethrtime() >> 23)
466
467extern int hz;
468extern uint64_t physmem;
469
470#define	gethrestime_sec()	time(NULL)
471
472#define	pwrite64(d, p, n, o)	pwrite(d, p, n, o)
473#define	readdir64(d)		readdir(d)
474#define	SIGPENDING(td)		(0)
475#define	root_mount_wait()	do { } while (0)
476#define	root_mounted()		(1)
477
478struct file {
479	void *dummy;
480};
481
482#define	FCREAT	O_CREAT
483#define	FOFFMAX	0x0
484
485#define	SX_SYSINIT(name, lock, desc)
486
487#define	SYSCTL_DECL(...)
488#define	SYSCTL_NODE(...)
489#define	SYSCTL_INT(...)
490#define	SYSCTL_ULONG(...)
491#ifdef TUNABLE_INT
492#undef TUNABLE_INT
493#undef TUNABLE_ULONG
494#endif
495#define	TUNABLE_INT(...)
496#define	TUNABLE_ULONG(...)
497
498/* Errors */
499
500#ifndef	ERESTART
501#define	ERESTART	(-1)
502#endif
503
504#ifdef	__cplusplus
505}
506#endif
507
508#endif	/* _SYS_ZFS_CONTEXT_H */
509