1168404Spjd/*
2168404Spjd * CDDL HEADER START
3168404Spjd *
4168404Spjd * The contents of this file are subject to the terms of the
5168404Spjd * Common Development and Distribution License (the "License").
6168404Spjd * You may not use this file except in compliance with the License.
7168404Spjd *
8168404Spjd * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9168404Spjd * or http://www.opensolaris.org/os/licensing.
10168404Spjd * See the License for the specific language governing permissions
11168404Spjd * and limitations under the License.
12168404Spjd *
13168404Spjd * When distributing Covered Code, include this CDDL HEADER in each
14168404Spjd * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15168404Spjd * If applicable, add the following below this CDDL HEADER, with the
16168404Spjd * fields enclosed by brackets "[]" replaced with your own identifying
17168404Spjd * information: Portions Copyright [yyyy] [name of copyright owner]
18168404Spjd *
19168404Spjd * CDDL HEADER END
20168404Spjd */
21168404Spjd/*
22219089Spjd * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23324973Sasomers * Copyright (c) 2012, 2016 by Delphix. All rights reserved.
24235222Smm * Copyright (c) 2012, Joyent, Inc. All rights reserved.
25168404Spjd */
26258630Savg/*
27258630Savg * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
28258630Savg */
29168404Spjd
30168404Spjd#ifndef _SYS_ZFS_CONTEXT_H
31168404Spjd#define	_SYS_ZFS_CONTEXT_H
32168404Spjd
33168404Spjd#ifdef	__cplusplus
34168404Spjdextern "C" {
35168404Spjd#endif
36168404Spjd
37168404Spjd#define	_SYS_MUTEX_H
38168404Spjd#define	_SYS_RWLOCK_H
39168404Spjd#define	_SYS_CONDVAR_H
40168404Spjd#define	_SYS_SYSTM_H
41168404Spjd#define	_SYS_T_LOCK_H
42168404Spjd#define	_SYS_VNODE_H
43168404Spjd#define	_SYS_VFS_H
44168404Spjd#define	_SYS_SUNDDI_H
45168404Spjd#define	_SYS_CALLB_H
46168404Spjd#define	_SYS_SCHED_H_
47168404Spjd
48168404Spjd#include <solaris.h>
49168404Spjd#include <stdio.h>
50168404Spjd#include <stdlib.h>
51168404Spjd#include <stddef.h>
52168404Spjd#include <stdarg.h>
53168404Spjd#include <fcntl.h>
54168404Spjd#include <unistd.h>
55168404Spjd#include <errno.h>
56168404Spjd#include <string.h>
57168404Spjd#include <strings.h>
58168404Spjd#include <thread.h>
59168404Spjd#include <assert.h>
60168404Spjd#include <limits.h>
61168404Spjd#include <dirent.h>
62168404Spjd#include <time.h>
63168404Spjd#include <math.h>
64168404Spjd#include <umem.h>
65219089Spjd#include <inttypes.h>
66168404Spjd#include <fsshare.h>
67248571Smm#include <pthread.h>
68258632Savg#include <sched.h>
69325534Savg#include <setjmp.h>
70248571Smm#include <sys/debug.h>
71168404Spjd#include <sys/note.h>
72168404Spjd#include <sys/types.h>
73185029Spjd#include <sys/cred.h>
74168404Spjd#include <sys/atomic.h>
75168404Spjd#include <sys/sysmacros.h>
76168404Spjd#include <sys/bitmap.h>
77168404Spjd#include <sys/resource.h>
78168404Spjd#include <sys/byteorder.h>
79168404Spjd#include <sys/list.h>
80168404Spjd#include <sys/time.h>
81168404Spjd#include <sys/uio.h>
82168404Spjd#include <sys/mntent.h>
83168404Spjd#include <sys/mnttab.h>
84168404Spjd#include <sys/zfs_debug.h>
85168404Spjd#include <sys/sdt.h>
86168404Spjd#include <sys/kstat.h>
87185029Spjd#include <sys/u8_textprep.h>
88168404Spjd#include <sys/kernel.h>
89168722Ssimokawa#include <sys/disk.h>
90219089Spjd#include <sys/sysevent.h>
91185029Spjd#include <sys/sysevent/eventdefs.h>
92219089Spjd#include <sys/sysevent/dev.h>
93168404Spjd#include <machine/atomic.h>
94240415Smm#include <sys/debug.h>
95249195Smm#ifdef illumos
96249195Smm#include "zfs.h"
97249195Smm#endif
98168404Spjd
99168404Spjd#define	ZFS_EXPORTS_PATH	"/etc/zfs/exports"
100168404Spjd
101168404Spjd/*
102168404Spjd * Debugging
103168404Spjd */
104168404Spjd
105168404Spjd/*
106168404Spjd * Note that we are not using the debugging levels.
107168404Spjd */
108168404Spjd
109168404Spjd#define	CE_CONT		0	/* continuation		*/
110168404Spjd#define	CE_NOTE		1	/* notice		*/
111168404Spjd#define	CE_WARN		2	/* warning		*/
112168404Spjd#define	CE_PANIC	3	/* panic		*/
113168404Spjd#define	CE_IGNORE	4	/* print nothing	*/
114168404Spjd
115168404Spjd/*
116168404Spjd * ZFS debugging
117168404Spjd */
118168404Spjd
119168404Spjd#define	ZFS_LOG(...)	do {  } while (0)
120168404Spjd
121168404Spjdtypedef u_longlong_t      rlim64_t;
122168404Spjd#define	RLIM64_INFINITY	((rlim64_t)-3)
123168404Spjd
124168404Spjd#ifdef ZFS_DEBUG
125168404Spjdextern void dprintf_setup(int *argc, char **argv);
126168404Spjd#endif /* ZFS_DEBUG */
127168404Spjd
128168404Spjdextern void cmn_err(int, const char *, ...);
129168404Spjdextern void vcmn_err(int, const char *, __va_list);
130325534Savgextern void panic(const char *, ...)  __NORETURN;
131325534Savgextern void vpanic(const char *, __va_list)  __NORETURN;
132168404Spjd
133185029Spjd#define	fm_panic	panic
134185029Spjd
135219089Spjdextern int aok;
136219089Spjd
137168404Spjd/*
138185029Spjd * DTrace SDT probes have different signatures in userland than they do in
139286705Smav * the kernel.  If they're being used in kernel code, re-define them out of
140168404Spjd * existence for their counterparts in libzpool.
141286705Smav *
142286705Smav * Here's an example of how to use the set-error probes in userland:
143286705Smav * zfs$target:::set-error /arg0 == EBUSY/ {stack();}
144286705Smav *
145286705Smav * Here's an example of how to use DTRACE_PROBE probes in userland:
146286705Smav * If there is a probe declared as follows:
147286705Smav * DTRACE_PROBE2(zfs__probe_name, uint64_t, blkid, dnode_t *, dn);
148286705Smav * Then you can use it as follows:
149286705Smav * zfs$target:::probe2 /copyinstr(arg0) == "zfs__probe_name"/
150286705Smav *     {printf("%u %p\n", arg1, arg2);}
151168404Spjd */
152168404Spjd
153185029Spjd#ifdef DTRACE_PROBE
154185029Spjd#undef	DTRACE_PROBE
155185029Spjd#endif	/* DTRACE_PROBE */
156249195Smm#ifdef illumos
157249195Smm#define	DTRACE_PROBE(a) \
158249195Smm	ZFS_PROBE0(#a)
159249195Smm#endif
160185029Spjd
161168404Spjd#ifdef DTRACE_PROBE1
162168404Spjd#undef	DTRACE_PROBE1
163168404Spjd#endif	/* DTRACE_PROBE1 */
164249195Smm#ifdef illumos
165249195Smm#define	DTRACE_PROBE1(a, b, c) \
166249195Smm	ZFS_PROBE1(#a, (unsigned long)c)
167249195Smm#endif
168168404Spjd
169168404Spjd#ifdef DTRACE_PROBE2
170168404Spjd#undef	DTRACE_PROBE2
171168404Spjd#endif	/* DTRACE_PROBE2 */
172249195Smm#ifdef illumos
173249195Smm#define	DTRACE_PROBE2(a, b, c, d, e) \
174249195Smm	ZFS_PROBE2(#a, (unsigned long)c, (unsigned long)e)
175249195Smm#endif
176168404Spjd
177168404Spjd#ifdef DTRACE_PROBE3
178168404Spjd#undef	DTRACE_PROBE3
179168404Spjd#endif	/* DTRACE_PROBE3 */
180249195Smm#ifdef illumos
181249195Smm#define	DTRACE_PROBE3(a, b, c, d, e, f, g) \
182249195Smm	ZFS_PROBE3(#a, (unsigned long)c, (unsigned long)e, (unsigned long)g)
183249195Smm#endif
184168404Spjd
185168404Spjd#ifdef DTRACE_PROBE4
186168404Spjd#undef	DTRACE_PROBE4
187168404Spjd#endif	/* DTRACE_PROBE4 */
188249195Smm#ifdef illumos
189249195Smm#define	DTRACE_PROBE4(a, b, c, d, e, f, g, h, i) \
190249195Smm	ZFS_PROBE4(#a, (unsigned long)c, (unsigned long)e, (unsigned long)g, \
191249195Smm	(unsigned long)i)
192249195Smm#endif
193168404Spjd
194249195Smm#ifdef illumos
195168404Spjd/*
196249195Smm * We use the comma operator so that this macro can be used without much
197249195Smm * additional code.  For example, "return (EINVAL);" becomes
198249195Smm * "return (SET_ERROR(EINVAL));".  Note that the argument will be evaluated
199249195Smm * twice, so it should not have side effects (e.g. something like:
200249195Smm * "return (SET_ERROR(log_error(EINVAL, info)));" would log the error twice).
201249195Smm */
202249195Smm#define	SET_ERROR(err)	(ZFS_SET_ERROR(err), err)
203249195Smm#else	/* !illumos */
204249195Smm
205249195Smm#define	DTRACE_PROBE(a)	((void)0)
206249195Smm#define	DTRACE_PROBE1(a, b, c)	((void)0)
207249195Smm#define	DTRACE_PROBE2(a, b, c, d, e)	((void)0)
208249195Smm#define	DTRACE_PROBE3(a, b, c, d, e, f, g)	((void)0)
209249195Smm#define	DTRACE_PROBE4(a, b, c, d, e, f, g, h, i)	((void)0)
210249195Smm
211249195Smm#define SET_ERROR(err) (err)
212249195Smm#endif	/* !illumos */
213249195Smm
214249195Smm/*
215168404Spjd * Threads
216168404Spjd */
217168404Spjd#define	curthread	((void *)(uintptr_t)thr_self())
218168404Spjd
219258632Savg#define	kpreempt(x)	sched_yield()
220258632Savg
221168404Spjdtypedef struct kthread kthread_t;
222168404Spjd
223168404Spjd#define	thread_create(stk, stksize, func, arg, len, pp, state, pri)	\
224168404Spjd	zk_thread_create(func, arg)
225168404Spjd#define	thread_exit() thr_exit(NULL)
226219089Spjd#define	thread_join(t)	panic("libzpool cannot join threads")
227168404Spjd
228219089Spjd#define	newproc(f, a, cid, pri, ctp, pid)	(ENOSYS)
229219089Spjd
230219089Spjd/* in libzpool, p0 exists only to have its address taken */
231219089Spjdstruct proc {
232219089Spjd	uintptr_t	this_is_never_used_dont_dereference_it;
233219089Spjd};
234219089Spjd
235219089Spjdextern struct proc p0;
236235222Smm#define	curproc		(&p0)
237219089Spjd
238219089Spjd#define	PS_NONE		-1
239219089Spjd
240331382Smavextern kthread_t *zk_thread_create(void (*func)(void*), void *arg);
241168404Spjd
242168404Spjd#define	issig(why)	(FALSE)
243168404Spjd#define	ISSIG(thr, why)	(FALSE)
244168404Spjd
245168404Spjd/*
246168404Spjd * Mutexes
247168404Spjd */
248168404Spjdtypedef struct kmutex {
249185029Spjd	void		*m_owner;
250185029Spjd	boolean_t	initialized;
251185029Spjd	mutex_t		m_lock;
252168404Spjd} kmutex_t;
253168404Spjd
254168404Spjd#define	MUTEX_DEFAULT	USYNC_THREAD
255219089Spjd#undef	MUTEX_HELD
256219089Spjd#undef	MUTEX_NOT_HELD
257168404Spjd#define	MUTEX_HELD(m)	((m)->m_owner == curthread)
258219089Spjd#define	MUTEX_NOT_HELD(m) (!MUTEX_HELD(m))
259219089Spjd#define	_mutex_held(m)	pthread_mutex_isowned_np(m)
260168404Spjd
261168404Spjd/*
262168404Spjd * Argh -- we have to get cheesy here because the kernel and userland
263168404Spjd * have different signatures for the same routine.
264168404Spjd */
265168404Spjd//extern int _mutex_init(mutex_t *mp, int type, void *arg);
266168404Spjd//extern int _mutex_destroy(mutex_t *mp);
267219089Spjd//extern int _mutex_owned(mutex_t *mp);
268168404Spjd
269168404Spjd#define	mutex_init(mp, b, c, d)		zmutex_init((kmutex_t *)(mp))
270168404Spjd#define	mutex_destroy(mp)		zmutex_destroy((kmutex_t *)(mp))
271195515Spjd#define	mutex_owned(mp)			zmutex_owned((kmutex_t *)(mp))
272168404Spjd
273168404Spjdextern void zmutex_init(kmutex_t *mp);
274168404Spjdextern void zmutex_destroy(kmutex_t *mp);
275195515Spjdextern int zmutex_owned(kmutex_t *mp);
276168404Spjdextern void mutex_enter(kmutex_t *mp);
277168404Spjdextern void mutex_exit(kmutex_t *mp);
278168404Spjdextern int mutex_tryenter(kmutex_t *mp);
279168404Spjdextern void *mutex_owner(kmutex_t *mp);
280168404Spjd
281168404Spjd/*
282168404Spjd * RW locks
283168404Spjd */
284168404Spjdtypedef struct krwlock {
285168404Spjd	int		rw_count;
286168404Spjd	void		*rw_owner;
287185029Spjd	boolean_t	initialized;
288168404Spjd	rwlock_t	rw_lock;
289168404Spjd} krwlock_t;
290168404Spjd
291168404Spjdtypedef int krw_t;
292168404Spjd
293168404Spjd#define	RW_READER	0
294168404Spjd#define	RW_WRITER	1
295168404Spjd#define	RW_DEFAULT	USYNC_THREAD
296168404Spjd
297168404Spjd#undef RW_READ_HELD
298185029Spjd#define RW_READ_HELD(x)		((x)->rw_owner == NULL && (x)->rw_count > 0)
299168404Spjd
300168404Spjd#undef RW_WRITE_HELD
301168404Spjd#define	RW_WRITE_HELD(x)	((x)->rw_owner == curthread)
302168404Spjd#define	RW_LOCK_HELD(x)		rw_lock_held(x)
303168404Spjd
304248571Smm#undef RW_LOCK_HELD
305248571Smm#define	RW_LOCK_HELD(x)		(RW_READ_HELD(x) || RW_WRITE_HELD(x))
306248571Smm
307168404Spjdextern void rw_init(krwlock_t *rwlp, char *name, int type, void *arg);
308168404Spjdextern void rw_destroy(krwlock_t *rwlp);
309168404Spjdextern void rw_enter(krwlock_t *rwlp, krw_t rw);
310168404Spjdextern int rw_tryenter(krwlock_t *rwlp, krw_t rw);
311168404Spjdextern int rw_tryupgrade(krwlock_t *rwlp);
312168404Spjdextern void rw_exit(krwlock_t *rwlp);
313168404Spjdextern int rw_lock_held(krwlock_t *rwlp);
314168404Spjd#define	rw_downgrade(rwlp) do { } while (0)
315168404Spjd
316185029Spjdextern uid_t crgetuid(cred_t *cr);
317248571Smmextern uid_t crgetruid(cred_t *cr);
318185029Spjdextern gid_t crgetgid(cred_t *cr);
319185029Spjdextern int crgetngroups(cred_t *cr);
320185029Spjdextern gid_t *crgetgroups(cred_t *cr);
321185029Spjd
322168404Spjd/*
323168404Spjd * Condition variables
324168404Spjd */
325168404Spjdtypedef cond_t kcondvar_t;
326168404Spjd
327168404Spjd#define	CV_DEFAULT	USYNC_THREAD
328297508Smav#define	CALLOUT_FLAG_ABSOLUTE	0x2
329168404Spjd
330168404Spjdextern void cv_init(kcondvar_t *cv, char *name, int type, void *arg);
331168404Spjdextern void cv_destroy(kcondvar_t *cv);
332168404Spjdextern void cv_wait(kcondvar_t *cv, kmutex_t *mp);
333168404Spjdextern clock_t cv_timedwait(kcondvar_t *cv, kmutex_t *mp, clock_t abstime);
334255437Sdelphijextern clock_t cv_timedwait_hires(kcondvar_t *cvp, kmutex_t *mp, hrtime_t tim,
335255437Sdelphij    hrtime_t res, int flag);
336168404Spjdextern void cv_signal(kcondvar_t *cv);
337168404Spjdextern void cv_broadcast(kcondvar_t *cv);
338168404Spjd
339168404Spjd/*
340248571Smm * Thread-specific data
341248571Smm */
342248571Smm#define	tsd_get(k) pthread_getspecific(k)
343248571Smm#define	tsd_set(k, v) pthread_setspecific(k, v)
344248571Smm#define	tsd_create(kp, d) pthread_key_create(kp, d)
345248571Smm#define	tsd_destroy(kp) /* nothing */
346248571Smm
347248571Smm/*
348168404Spjd * Kernel memory
349168404Spjd */
350168404Spjd#define	KM_SLEEP		UMEM_NOFAIL
351185029Spjd#define	KM_PUSHPAGE		KM_SLEEP
352168404Spjd#define	KM_NOSLEEP		UMEM_DEFAULT
353325534Savg#define	KM_NORMALPRI		0	/* not needed with UMEM_DEFAULT */
354168404Spjd#define	KMC_NODEBUG		UMC_NODEBUG
355219089Spjd#define	KMC_NOTOUCH		0	/* not needed for userland caches */
356230629Skmacy#define	KM_NODEBUG		0
357168404Spjd#define	kmem_alloc(_s, _f)	umem_alloc(_s, _f)
358168404Spjd#define	kmem_zalloc(_s, _f)	umem_zalloc(_s, _f)
359168404Spjd#define	kmem_free(_b, _s)	umem_free(_b, _s)
360168584Spjd#define	kmem_size()		(physmem * PAGESIZE)
361168404Spjd#define	kmem_cache_create(_a, _b, _c, _d, _e, _f, _g, _h, _i) \
362168404Spjd	umem_cache_create(_a, _b, _c, _d, _e, _f, _g, _h, _i)
363168404Spjd#define	kmem_cache_destroy(_c)	umem_cache_destroy(_c)
364168404Spjd#define	kmem_cache_alloc(_c, _f) umem_cache_alloc(_c, _f)
365168404Spjd#define	kmem_cache_free(_c, _b)	umem_cache_free(_c, _b)
366168404Spjd#define	kmem_debugging()	0
367332528Smav#define	kmem_cache_reap_active()	(B_FALSE)
368332528Smav#define	kmem_cache_reap_soon(_c)	/* nothing */
369219089Spjd#define	kmem_cache_set_move(_c, _cb)	/* nothing */
370219089Spjd#define	POINTER_INVALIDATE(_pp)		/* nothing */
371219089Spjd#define	POINTER_IS_VALID(_p)	0
372168404Spjd
373168404Spjdtypedef umem_cache_t kmem_cache_t;
374168404Spjd
375219089Spjdtypedef enum kmem_cbrc {
376219089Spjd	KMEM_CBRC_YES,
377219089Spjd	KMEM_CBRC_NO,
378219089Spjd	KMEM_CBRC_LATER,
379219089Spjd	KMEM_CBRC_DONT_NEED,
380219089Spjd	KMEM_CBRC_DONT_KNOW
381219089Spjd} kmem_cbrc_t;
382219089Spjd
383168404Spjd/*
384168404Spjd * Task queues
385168404Spjd */
386168404Spjdtypedef struct taskq taskq_t;
387168404Spjdtypedef uintptr_t taskqid_t;
388168404Spjdtypedef void (task_func_t)(void *);
389168404Spjd
390258630Savgtypedef struct taskq_ent {
391258630Savg	struct taskq_ent	*tqent_next;
392258630Savg	struct taskq_ent	*tqent_prev;
393258630Savg	task_func_t		*tqent_func;
394258630Savg	void			*tqent_arg;
395258630Savg	uintptr_t		tqent_flags;
396258630Savg} taskq_ent_t;
397258630Savg
398258630Savg#define	TQENT_FLAG_PREALLOC	0x1	/* taskq_dispatch_ent used */
399258630Savg
400168404Spjd#define	TASKQ_PREPOPULATE	0x0001
401168404Spjd#define	TASKQ_CPR_SAFE		0x0002	/* Use CPR safe protocol */
402168404Spjd#define	TASKQ_DYNAMIC		0x0004	/* Use dynamic thread scheduling */
403219089Spjd#define	TASKQ_THREADS_CPU_PCT	0x0008	/* Scale # threads by # cpus */
404219089Spjd#define	TASKQ_DC_BATCH		0x0010	/* Mark threads as batch */
405168404Spjd
406168404Spjd#define	TQ_SLEEP	KM_SLEEP	/* Can block for memory */
407168404Spjd#define	TQ_NOSLEEP	KM_NOSLEEP	/* cannot block for memory; may fail */
408219089Spjd#define	TQ_NOQUEUE	0x02		/* Do not enqueue if can't dispatch */
409219089Spjd#define	TQ_FRONT	0x08		/* Queue in front */
410168404Spjd
411339034Ssef#define TASKQID_INVALID         ((taskqid_t)0)
412258630Savg
413208047Smmextern taskq_t *system_taskq;
414208047Smm
415168404Spjdextern taskq_t	*taskq_create(const char *, int, pri_t, int, int, uint_t);
416219089Spjd#define	taskq_create_proc(a, b, c, d, e, p, f) \
417219089Spjd	    (taskq_create(a, b, c, d, e, f))
418219089Spjd#define	taskq_create_sysdc(a, b, d, e, p, dc, f) \
419219089Spjd	    (taskq_create(a, b, maxclsyspri, d, e, f))
420168404Spjdextern taskqid_t taskq_dispatch(taskq_t *, task_func_t, void *, uint_t);
421258630Savgextern void	taskq_dispatch_ent(taskq_t *, task_func_t, void *, uint_t,
422258630Savg    taskq_ent_t *);
423168404Spjdextern void	taskq_destroy(taskq_t *);
424168404Spjdextern void	taskq_wait(taskq_t *);
425339034Ssefextern void	taskq_wait_id(taskq_t *, taskqid_t);
426168404Spjdextern int	taskq_member(taskq_t *, void *);
427208047Smmextern void	system_taskq_init(void);
428219089Spjdextern void	system_taskq_fini(void);
429168404Spjd
430219089Spjd#define	taskq_dispatch_safe(tq, func, arg, flags, task)			\
431219089Spjd	taskq_dispatch((tq), (func), (arg), (flags))
432208166Spjd
433185029Spjd#define	XVA_MAPSIZE	3
434185029Spjd#define	XVA_MAGIC	0x78766174
435185029Spjd
436168404Spjd/*
437168404Spjd * vnodes
438168404Spjd */
439168404Spjdtypedef struct vnode {
440168404Spjd	uint64_t	v_size;
441168404Spjd	int		v_fd;
442168404Spjd	char		*v_path;
443268084Sdelphij	int		v_dump_fd;
444168404Spjd} vnode_t;
445168404Spjd
446268084Sdelphijextern char *vn_dumpdir;
447219089Spjd#define	AV_SCANSTAMP_SZ	32		/* length of anti-virus scanstamp */
448185029Spjd
449185029Spjdtypedef struct xoptattr {
450185029Spjd	timestruc_t	xoa_createtime;	/* Create time of file */
451185029Spjd	uint8_t		xoa_archive;
452185029Spjd	uint8_t		xoa_system;
453185029Spjd	uint8_t		xoa_readonly;
454185029Spjd	uint8_t		xoa_hidden;
455185029Spjd	uint8_t		xoa_nounlink;
456185029Spjd	uint8_t		xoa_immutable;
457185029Spjd	uint8_t		xoa_appendonly;
458185029Spjd	uint8_t		xoa_nodump;
459185029Spjd	uint8_t		xoa_settable;
460185029Spjd	uint8_t		xoa_opaque;
461185029Spjd	uint8_t		xoa_av_quarantined;
462185029Spjd	uint8_t		xoa_av_modified;
463219089Spjd	uint8_t		xoa_av_scanstamp[AV_SCANSTAMP_SZ];
464219089Spjd	uint8_t		xoa_reparse;
465219089Spjd	uint8_t		xoa_offline;
466219089Spjd	uint8_t		xoa_sparse;
467185029Spjd} xoptattr_t;
468185029Spjd
469168404Spjdtypedef struct vattr {
470168404Spjd	uint_t		va_mask;	/* bit-mask of attributes */
471168404Spjd	u_offset_t	va_size;	/* file size in bytes */
472168404Spjd} vattr_t;
473168404Spjd
474168404Spjd
475185029Spjdtypedef struct xvattr {
476185029Spjd	vattr_t		xva_vattr;	/* Embedded vattr structure */
477185029Spjd	uint32_t	xva_magic;	/* Magic Number */
478185029Spjd	uint32_t	xva_mapsize;	/* Size of attr bitmap (32-bit words) */
479185029Spjd	uint32_t	*xva_rtnattrmapp;	/* Ptr to xva_rtnattrmap[] */
480185029Spjd	uint32_t	xva_reqattrmap[XVA_MAPSIZE];	/* Requested attrs */
481185029Spjd	uint32_t	xva_rtnattrmap[XVA_MAPSIZE];	/* Returned attrs */
482185029Spjd	xoptattr_t	xva_xoptattrs;	/* Optional attributes */
483185029Spjd} xvattr_t;
484185029Spjd
485185029Spjdtypedef struct vsecattr {
486185029Spjd	uint_t		vsa_mask;	/* See below */
487185029Spjd	int		vsa_aclcnt;	/* ACL entry count */
488185029Spjd	void		*vsa_aclentp;	/* pointer to ACL entries */
489185029Spjd	int		vsa_dfaclcnt;	/* default ACL entry count */
490185029Spjd	void		*vsa_dfaclentp;	/* pointer to default ACL entries */
491185029Spjd	size_t		vsa_aclentsz;	/* ACE size in bytes of vsa_aclentp */
492185029Spjd} vsecattr_t;
493185029Spjd
494185029Spjd#define	AT_TYPE		0x00001
495185029Spjd#define	AT_MODE		0x00002
496185029Spjd#define	AT_UID		0x00004
497185029Spjd#define	AT_GID		0x00008
498185029Spjd#define	AT_FSID		0x00010
499185029Spjd#define	AT_NODEID	0x00020
500185029Spjd#define	AT_NLINK	0x00040
501185029Spjd#define	AT_SIZE		0x00080
502185029Spjd#define	AT_ATIME	0x00100
503185029Spjd#define	AT_MTIME	0x00200
504185029Spjd#define	AT_CTIME	0x00400
505185029Spjd#define	AT_RDEV		0x00800
506185029Spjd#define	AT_BLKSIZE	0x01000
507185029Spjd#define	AT_NBLOCKS	0x02000
508185029Spjd#define	AT_SEQ		0x08000
509185029Spjd#define	AT_XVATTR	0x10000
510185029Spjd
511168404Spjd#define	CRCREAT		0
512168404Spjd
513219089Spjdextern int fop_getattr(vnode_t *vp, vattr_t *vap);
514219089Spjd
515185029Spjd#define	VOP_CLOSE(vp, f, c, o, cr, ct)	0
516185029Spjd#define	VOP_PUTPAGE(vp, of, sz, fl, cr, ct)	0
517219089Spjd#define	VOP_GETATTR(vp, vap, cr)  fop_getattr((vp), (vap));
518168404Spjd
519185029Spjd#define	VOP_FSYNC(vp, f, cr, ct)	fsync((vp)->v_fd)
520168404Spjd
521219089Spjd#define	VN_RELE(vp)			vn_close(vp, 0, NULL, NULL)
522191906Skmacy#define	VN_RELE_ASYNC(vp, taskq)	vn_close(vp, 0, NULL, NULL)
523168404Spjd
524185029Spjd#define	vn_lock(vp, type)
525185029Spjd#define	VOP_UNLOCK(vp, type)
526185029Spjd
527168404Spjdextern int vn_open(char *path, int x1, int oflags, int mode, vnode_t **vpp,
528168404Spjd    int x2, int x3);
529168404Spjdextern int vn_openat(char *path, int x1, int oflags, int mode, vnode_t **vpp,
530185029Spjd    int x2, int x3, vnode_t *vp, int fd);
531168404Spjdextern int vn_rdwr(int uio, vnode_t *vp, void *addr, ssize_t len,
532168404Spjd    offset_t offset, int x1, int x2, rlim64_t x3, void *x4, ssize_t *residp);
533185029Spjdextern void vn_close(vnode_t *vp, int openflag, cred_t *cr, kthread_t *td);
534168404Spjd
535168404Spjd#define	vn_remove(path, x1, x2)		remove(path)
536168404Spjd#define	vn_rename(from, to, seg)	rename((from), (to))
537168404Spjd#define	vn_is_readonly(vp)		B_FALSE
538168404Spjd
539168404Spjdextern vnode_t *rootdir;
540168404Spjd
541168404Spjd#include <sys/file.h>		/* for FREAD, FWRITE, etc */
542168404Spjd#define	FTRUNC	O_TRUNC
543168404Spjd
544168404Spjd/*
545168404Spjd * Random stuff
546168404Spjd */
547219089Spjd#define	ddi_get_lbolt()		(gethrtime() >> 23)
548219089Spjd#define	ddi_get_lbolt64()	(gethrtime() >> 23)
549219089Spjd#define	hz	119	/* frequency when using gethrtime() >> 23 for lbolt */
550168404Spjd
551168404Spjdextern void delay(clock_t ticks);
552168404Spjd
553247265Smm#define	SEC_TO_TICK(sec)	((sec) * hz)
554277484Sngie#define	NSEC_TO_TICK(nsec)	((nsec) / (NANOSEC / hz))
555247265Smm
556168404Spjd#define	gethrestime_sec() time(NULL)
557219089Spjd#define	gethrestime(t) \
558219089Spjd	do {\
559219089Spjd		(t)->tv_sec = gethrestime_sec();\
560219089Spjd		(t)->tv_nsec = 0;\
561219089Spjd	} while (0);
562168404Spjd
563168404Spjd#define	max_ncpus	64
564332540Smav#define	boot_ncpus	(sysconf(_SC_NPROCESSORS_ONLN))
565168404Spjd
566168404Spjd#define	minclsyspri	60
567168404Spjd#define	maxclsyspri	99
568168404Spjd
569168404Spjd#define	CPU_SEQID	(thr_self() & (max_ncpus - 1))
570168404Spjd
571219089Spjd#define	kcred		NULL
572219089Spjd#define	CRED()		NULL
573219089Spjd
574185029Spjd#ifndef ptob
575185029Spjd#define	ptob(x)		((x) * PAGESIZE)
576185029Spjd#endif
577168404Spjd
578168404Spjdextern uint64_t physmem;
579168404Spjd
580264669Sdelphijextern int highbit64(uint64_t i);
581168404Spjdextern int random_get_bytes(uint8_t *ptr, size_t len);
582168404Spjdextern int random_get_pseudo_bytes(uint8_t *ptr, size_t len);
583168404Spjd
584168404Spjdextern void kernel_init(int);
585168404Spjdextern void kernel_fini(void);
586168404Spjd
587168404Spjdstruct spa;
588325914Savgextern void nicenum(uint64_t num, char *buf, size_t);
589168404Spjdextern void show_pool_stats(struct spa *);
590324973Sasomersextern int set_global_var(char *arg);
591168404Spjd
592168404Spjdtypedef struct callb_cpr {
593168404Spjd	kmutex_t	*cc_lockp;
594168404Spjd} callb_cpr_t;
595168404Spjd
596168404Spjd#define	CALLB_CPR_INIT(cp, lockp, func, name)	{		\
597168404Spjd	(cp)->cc_lockp = lockp;					\
598168404Spjd}
599168404Spjd
600168404Spjd#define	CALLB_CPR_SAFE_BEGIN(cp) {				\
601168404Spjd	ASSERT(MUTEX_HELD((cp)->cc_lockp));			\
602168404Spjd}
603168404Spjd
604168404Spjd#define	CALLB_CPR_SAFE_END(cp, lockp) {				\
605168404Spjd	ASSERT(MUTEX_HELD((cp)->cc_lockp));			\
606168404Spjd}
607168404Spjd
608168404Spjd#define	CALLB_CPR_EXIT(cp) {					\
609168404Spjd	ASSERT(MUTEX_HELD((cp)->cc_lockp));			\
610168404Spjd	mutex_exit((cp)->cc_lockp);				\
611168404Spjd}
612168404Spjd
613168404Spjd#define	zone_dataset_visible(x, y)	(1)
614168404Spjd#define	INGLOBALZONE(z)			(1)
615332536Smavextern uint32_t zone_get_hostid(void *zonep);
616168404Spjd
617219089Spjdextern char *kmem_asprintf(const char *fmt, ...);
618248571Smm#define	strfree(str) kmem_free((str), strlen(str) + 1)
619219089Spjd
620168498Spjd/*
621168498Spjd * Hostname information
622168498Spjd */
623168498Spjdextern struct utsname utsname;
624219089Spjdextern char hw_serial[];	/* for userland-emulated hostid access */
625168498Spjdextern int ddi_strtoul(const char *str, char **nptr, int base,
626168498Spjd    unsigned long *result);
627168498Spjd
628219089Spjdextern int ddi_strtoull(const char *str, char **nptr, int base,
629219089Spjd    u_longlong_t *result);
630219089Spjd
631168404Spjd/* ZFS Boot Related stuff. */
632168404Spjd
633168404Spjdstruct _buf {
634168404Spjd	intptr_t	_fd;
635168404Spjd};
636168404Spjd
637168404Spjdstruct bootstat {
638168404Spjd	uint64_t st_size;
639168404Spjd};
640168404Spjd
641185029Spjdtypedef struct ace_object {
642185029Spjd	uid_t		a_who;
643185029Spjd	uint32_t	a_access_mask;
644185029Spjd	uint16_t	a_flags;
645185029Spjd	uint16_t	a_type;
646185029Spjd	uint8_t		a_obj_type[16];
647185029Spjd	uint8_t		a_inherit_obj_type[16];
648185029Spjd} ace_object_t;
649185029Spjd
650185029Spjd
651185029Spjd#define	ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE	0x05
652185029Spjd#define	ACE_ACCESS_DENIED_OBJECT_ACE_TYPE	0x06
653185029Spjd#define	ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE	0x07
654185029Spjd#define	ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE	0x08
655185029Spjd
656168404Spjdextern struct _buf *kobj_open_file(char *name);
657168404Spjdextern int kobj_read_file(struct _buf *file, char *buf, unsigned size,
658168404Spjd    unsigned off);
659168404Spjdextern void kobj_close_file(struct _buf *file);
660168404Spjdextern int kobj_get_filesize(struct _buf *file, uint64_t *size);
661185029Spjdextern int zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr);
662185029Spjdextern int zfs_secpolicy_rename_perms(const char *from, const char *to,
663185029Spjd    cred_t *cr);
664185029Spjdextern int zfs_secpolicy_destroy_perms(const char *name, cred_t *cr);
665185029Spjdextern zoneid_t getzoneid(void);
666168404Spjd/* Random compatibility stuff. */
667168404Spjd#define	pwrite64(d, p, n, o)	pwrite(d, p, n, o)
668168404Spjd#define	readdir64(d)		readdir(d)
669168404Spjd#define	SIGPENDING(td)		(0)
670168404Spjd#define	root_mount_wait()	do { } while (0)
671168825Spjd#define	root_mounted()		(1)
672168404Spjd
673339158Smav#define	noinline	__attribute__((noinline))
674339158Smav#define	likely(x)	__builtin_expect((x), 1)
675339158Smav
676168404Spjdstruct file {
677168404Spjd	void *dummy;
678168404Spjd};
679168404Spjd
680168404Spjd#define	FCREAT	O_CREAT
681168404Spjd#define	FOFFMAX	0x0
682168404Spjd
683185029Spjd/* SID stuff */
684185029Spjdtypedef struct ksiddomain {
685185029Spjd	uint_t	kd_ref;
686185029Spjd	uint_t	kd_len;
687185029Spjd	char	*kd_name;
688185029Spjd} ksiddomain_t;
689185029Spjd
690185029Spjdksiddomain_t *ksid_lookupdomain(const char *);
691185029Spjdvoid ksiddomain_rele(ksiddomain_t *);
692185029Spjd
693209962Smmtypedef	uint32_t	idmap_rid_t;
694209962Smm
695219089Spjd#define	DDI_SLEEP	KM_SLEEP
696219089Spjd#define	ddi_log_sysevent(_a, _b, _c, _d, _e, _f, _g)	(0)
697219089Spjd
698168404Spjd#define	SX_SYSINIT(name, lock, desc)
699168404Spjd
700254591Sgibbs#define SYSCTL_HANDLER_ARGS struct sysctl_oid *oidp, void *arg1,	\
701254591Sgibbs	intptr_t arg2, struct sysctl_req *req
702254591Sgibbs
703254591Sgibbs/*
704254591Sgibbs * This describes the access space for a sysctl request.  This is needed
705254591Sgibbs * so that we can use the interface from the kernel or from user-space.
706254591Sgibbs */
707254591Sgibbsstruct sysctl_req {
708254591Sgibbs	struct thread	*td;		/* used for access checking */
709254591Sgibbs	int		lock;		/* wiring state */
710254591Sgibbs	void		*oldptr;
711254591Sgibbs	size_t		oldlen;
712254591Sgibbs	size_t		oldidx;
713254591Sgibbs	int		(*oldfunc)(struct sysctl_req *, const void *, size_t);
714254591Sgibbs	void		*newptr;
715254591Sgibbs	size_t		newlen;
716254591Sgibbs	size_t		newidx;
717254591Sgibbs	int		(*newfunc)(struct sysctl_req *, void *, size_t);
718254591Sgibbs	size_t		validlen;
719254591Sgibbs	int		flags;
720254591Sgibbs};
721254591Sgibbs
722254591SgibbsSLIST_HEAD(sysctl_oid_list, sysctl_oid);
723254591Sgibbs
724254591Sgibbs/*
725254591Sgibbs * This describes one "oid" in the MIB tree.  Potentially more nodes can
726254591Sgibbs * be hidden behind it, expanded by the handler.
727254591Sgibbs */
728254591Sgibbsstruct sysctl_oid {
729254591Sgibbs	struct sysctl_oid_list *oid_parent;
730254591Sgibbs	SLIST_ENTRY(sysctl_oid) oid_link;
731254591Sgibbs	int		oid_number;
732254591Sgibbs	u_int		oid_kind;
733254591Sgibbs	void		*oid_arg1;
734254591Sgibbs	intptr_t	oid_arg2;
735254591Sgibbs	const char	*oid_name;
736254591Sgibbs	int 		(*oid_handler)(SYSCTL_HANDLER_ARGS);
737254591Sgibbs	const char	*oid_fmt;
738254591Sgibbs	int		oid_refcnt;
739254591Sgibbs	u_int		oid_running;
740254591Sgibbs	const char	*oid_descr;
741254591Sgibbs};
742254591Sgibbs
743168404Spjd#define	SYSCTL_DECL(...)
744168582Spjd#define	SYSCTL_NODE(...)
745168404Spjd#define	SYSCTL_INT(...)
746185029Spjd#define	SYSCTL_UINT(...)
747168582Spjd#define	SYSCTL_ULONG(...)
748254591Sgibbs#define	SYSCTL_PROC(...)
749185029Spjd#define	SYSCTL_QUAD(...)
750217367Smdf#define	SYSCTL_UQUAD(...)
751168404Spjd#ifdef TUNABLE_INT
752168404Spjd#undef TUNABLE_INT
753168582Spjd#undef TUNABLE_ULONG
754185029Spjd#undef TUNABLE_QUAD
755168404Spjd#endif
756168404Spjd#define	TUNABLE_INT(...)
757168582Spjd#define	TUNABLE_ULONG(...)
758185029Spjd#define	TUNABLE_QUAD(...)
759168404Spjd
760254591Sgibbsint sysctl_handle_64(SYSCTL_HANDLER_ARGS);
761254591Sgibbs
762168404Spjd/* Errors */
763168404Spjd
764168404Spjd#ifndef	ERESTART
765168404Spjd#define	ERESTART	(-1)
766168404Spjd#endif
767168404Spjd
768247265Smm#ifdef illumos
769247265Smm/*
770247265Smm * Cyclic information
771247265Smm */
772247265Smmextern kmutex_t cpu_lock;
773247265Smm
774247265Smmtypedef uintptr_t cyclic_id_t;
775247265Smmtypedef uint16_t cyc_level_t;
776247265Smmtypedef void (*cyc_func_t)(void *);
777247265Smm
778247265Smm#define	CY_LOW_LEVEL	0
779247265Smm#define	CY_INFINITY	INT64_MAX
780247265Smm#define	CYCLIC_NONE	((cyclic_id_t)0)
781247265Smm
782247265Smmtypedef struct cyc_time {
783247265Smm	hrtime_t cyt_when;
784247265Smm	hrtime_t cyt_interval;
785247265Smm} cyc_time_t;
786247265Smm
787247265Smmtypedef struct cyc_handler {
788247265Smm	cyc_func_t cyh_func;
789247265Smm	void *cyh_arg;
790247265Smm	cyc_level_t cyh_level;
791247265Smm} cyc_handler_t;
792247265Smm
793247265Smmextern cyclic_id_t cyclic_add(cyc_handler_t *, cyc_time_t *);
794247265Smmextern void cyclic_remove(cyclic_id_t);
795247265Smmextern int cyclic_reprogram(cyclic_id_t, hrtime_t);
796247265Smm#endif	/* illumos */
797247265Smm
798260138Sdelphij#ifdef illumos
799260138Sdelphij/*
800260138Sdelphij * Buf structure
801260138Sdelphij */
802260138Sdelphij#define	B_BUSY		0x0001
803260138Sdelphij#define	B_DONE		0x0002
804260138Sdelphij#define	B_ERROR		0x0004
805260138Sdelphij#define	B_READ		0x0040	/* read when I/O occurs */
806260138Sdelphij#define	B_WRITE		0x0100	/* non-read pseudo-flag */
807260138Sdelphij
808260138Sdelphijtypedef struct buf {
809260138Sdelphij	int	b_flags;
810260138Sdelphij	size_t b_bcount;
811260138Sdelphij	union {
812260138Sdelphij		caddr_t b_addr;
813260138Sdelphij	} b_un;
814260138Sdelphij
815260138Sdelphij	lldaddr_t	_b_blkno;
816260138Sdelphij#define	b_lblkno	_b_blkno._f
817260138Sdelphij	size_t	b_resid;
818260138Sdelphij	size_t	b_bufsize;
819260138Sdelphij	int	(*b_iodone)(struct buf *);
820260138Sdelphij	int	b_error;
821260138Sdelphij	void	*b_private;
822260138Sdelphij} buf_t;
823260138Sdelphij
824260138Sdelphijextern void bioinit(buf_t *);
825260138Sdelphijextern void biodone(buf_t *);
826260138Sdelphijextern void bioerror(buf_t *, int);
827260138Sdelphijextern int geterror(buf_t *);
828260138Sdelphij#endif
829260138Sdelphij
830169301Spjd#ifdef	__cplusplus
831169301Spjd}
832169301Spjd#endif
833169301Spjd
834168404Spjd#endif	/* _SYS_ZFS_CONTEXT_H */
835