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