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.
23265740Sdelphij * Copyright (c) 2012, 2014 by Delphix. All rights reserved.
24235222Smm * Copyright (c) 2012, Joyent, Inc. All rights reserved.
25168404Spjd */
26260742Savg/*
27260742Savg * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
28260742Savg */
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>
68260763Savg#include <sched.h>
69248571Smm#include <sys/debug.h>
70168404Spjd#include <sys/note.h>
71168404Spjd#include <sys/types.h>
72185029Spjd#include <sys/cred.h>
73168404Spjd#include <sys/atomic.h>
74168404Spjd#include <sys/sysmacros.h>
75168404Spjd#include <sys/bitmap.h>
76168404Spjd#include <sys/resource.h>
77168404Spjd#include <sys/byteorder.h>
78168404Spjd#include <sys/list.h>
79168404Spjd#include <sys/time.h>
80168404Spjd#include <sys/uio.h>
81168404Spjd#include <sys/mntent.h>
82168404Spjd#include <sys/mnttab.h>
83168404Spjd#include <sys/zfs_debug.h>
84168404Spjd#include <sys/sdt.h>
85168404Spjd#include <sys/kstat.h>
86185029Spjd#include <sys/u8_textprep.h>
87168404Spjd#include <sys/kernel.h>
88168722Ssimokawa#include <sys/disk.h>
89219089Spjd#include <sys/sysevent.h>
90185029Spjd#include <sys/sysevent/eventdefs.h>
91219089Spjd#include <sys/sysevent/dev.h>
92168404Spjd#include <machine/atomic.h>
93240415Smm#include <sys/debug.h>
94249195Smm#ifdef illumos
95249195Smm#include "zfs.h"
96249195Smm#endif
97168404Spjd
98168404Spjd#define	ZFS_EXPORTS_PATH	"/etc/zfs/exports"
99168404Spjd
100168404Spjd/*
101168404Spjd * Debugging
102168404Spjd */
103168404Spjd
104168404Spjd/*
105168404Spjd * Note that we are not using the debugging levels.
106168404Spjd */
107168404Spjd
108168404Spjd#define	CE_CONT		0	/* continuation		*/
109168404Spjd#define	CE_NOTE		1	/* notice		*/
110168404Spjd#define	CE_WARN		2	/* warning		*/
111168404Spjd#define	CE_PANIC	3	/* panic		*/
112168404Spjd#define	CE_IGNORE	4	/* print nothing	*/
113168404Spjd
114168404Spjd/*
115168404Spjd * ZFS debugging
116168404Spjd */
117168404Spjd
118168404Spjd#define	ZFS_LOG(...)	do {  } while (0)
119168404Spjd
120168404Spjdtypedef u_longlong_t      rlim64_t;
121168404Spjd#define	RLIM64_INFINITY	((rlim64_t)-3)
122168404Spjd
123168404Spjd#ifdef ZFS_DEBUG
124168404Spjdextern void dprintf_setup(int *argc, char **argv);
125168404Spjd#endif /* ZFS_DEBUG */
126168404Spjd
127168404Spjdextern void cmn_err(int, const char *, ...);
128168404Spjdextern void vcmn_err(int, const char *, __va_list);
129168404Spjdextern void panic(const char *, ...);
130168404Spjdextern void vpanic(const char *, __va_list);
131168404Spjd
132185029Spjd#define	fm_panic	panic
133185029Spjd
134219089Spjdextern int aok;
135219089Spjd
136168404Spjd/*
137185029Spjd * DTrace SDT probes have different signatures in userland than they do in
138168404Spjd * kernel.  If they're being used in kernel code, re-define them out of
139168404Spjd * existence for their counterparts in libzpool.
140168404Spjd */
141168404Spjd
142185029Spjd#ifdef DTRACE_PROBE
143185029Spjd#undef	DTRACE_PROBE
144185029Spjd#endif	/* DTRACE_PROBE */
145249195Smm#ifdef illumos
146249195Smm#define	DTRACE_PROBE(a) \
147249195Smm	ZFS_PROBE0(#a)
148249195Smm#endif
149185029Spjd
150168404Spjd#ifdef DTRACE_PROBE1
151168404Spjd#undef	DTRACE_PROBE1
152168404Spjd#endif	/* DTRACE_PROBE1 */
153249195Smm#ifdef illumos
154249195Smm#define	DTRACE_PROBE1(a, b, c) \
155249195Smm	ZFS_PROBE1(#a, (unsigned long)c)
156249195Smm#endif
157168404Spjd
158168404Spjd#ifdef DTRACE_PROBE2
159168404Spjd#undef	DTRACE_PROBE2
160168404Spjd#endif	/* DTRACE_PROBE2 */
161249195Smm#ifdef illumos
162249195Smm#define	DTRACE_PROBE2(a, b, c, d, e) \
163249195Smm	ZFS_PROBE2(#a, (unsigned long)c, (unsigned long)e)
164249195Smm#endif
165168404Spjd
166168404Spjd#ifdef DTRACE_PROBE3
167168404Spjd#undef	DTRACE_PROBE3
168168404Spjd#endif	/* DTRACE_PROBE3 */
169249195Smm#ifdef illumos
170249195Smm#define	DTRACE_PROBE3(a, b, c, d, e, f, g) \
171249195Smm	ZFS_PROBE3(#a, (unsigned long)c, (unsigned long)e, (unsigned long)g)
172249195Smm#endif
173168404Spjd
174168404Spjd#ifdef DTRACE_PROBE4
175168404Spjd#undef	DTRACE_PROBE4
176168404Spjd#endif	/* DTRACE_PROBE4 */
177249195Smm#ifdef illumos
178249195Smm#define	DTRACE_PROBE4(a, b, c, d, e, f, g, h, i) \
179249195Smm	ZFS_PROBE4(#a, (unsigned long)c, (unsigned long)e, (unsigned long)g, \
180249195Smm	(unsigned long)i)
181249195Smm#endif
182168404Spjd
183249195Smm#ifdef illumos
184168404Spjd/*
185249195Smm * We use the comma operator so that this macro can be used without much
186249195Smm * additional code.  For example, "return (EINVAL);" becomes
187249195Smm * "return (SET_ERROR(EINVAL));".  Note that the argument will be evaluated
188249195Smm * twice, so it should not have side effects (e.g. something like:
189249195Smm * "return (SET_ERROR(log_error(EINVAL, info)));" would log the error twice).
190249195Smm */
191249195Smm#define	SET_ERROR(err)	(ZFS_SET_ERROR(err), err)
192249195Smm#else	/* !illumos */
193249195Smm
194249195Smm#define	DTRACE_PROBE(a)	((void)0)
195249195Smm#define	DTRACE_PROBE1(a, b, c)	((void)0)
196249195Smm#define	DTRACE_PROBE2(a, b, c, d, e)	((void)0)
197249195Smm#define	DTRACE_PROBE3(a, b, c, d, e, f, g)	((void)0)
198249195Smm#define	DTRACE_PROBE4(a, b, c, d, e, f, g, h, i)	((void)0)
199249195Smm
200249195Smm#define SET_ERROR(err) (err)
201249195Smm#endif	/* !illumos */
202249195Smm
203249195Smm/*
204168404Spjd * Threads
205168404Spjd */
206168404Spjd#define	curthread	((void *)(uintptr_t)thr_self())
207168404Spjd
208260763Savg#define	kpreempt(x)	sched_yield()
209260763Savg
210168404Spjdtypedef struct kthread kthread_t;
211168404Spjd
212168404Spjd#define	thread_create(stk, stksize, func, arg, len, pp, state, pri)	\
213168404Spjd	zk_thread_create(func, arg)
214168404Spjd#define	thread_exit() thr_exit(NULL)
215219089Spjd#define	thread_join(t)	panic("libzpool cannot join threads")
216168404Spjd
217219089Spjd#define	newproc(f, a, cid, pri, ctp, pid)	(ENOSYS)
218219089Spjd
219219089Spjd/* in libzpool, p0 exists only to have its address taken */
220219089Spjdstruct proc {
221219089Spjd	uintptr_t	this_is_never_used_dont_dereference_it;
222219089Spjd};
223219089Spjd
224219089Spjdextern struct proc p0;
225235222Smm#define	curproc		(&p0)
226219089Spjd
227219089Spjd#define	PS_NONE		-1
228219089Spjd
229168404Spjdextern kthread_t *zk_thread_create(void (*func)(), void *arg);
230168404Spjd
231168404Spjd#define	issig(why)	(FALSE)
232168404Spjd#define	ISSIG(thr, why)	(FALSE)
233168404Spjd
234168404Spjd/*
235168404Spjd * Mutexes
236168404Spjd */
237168404Spjdtypedef struct kmutex {
238185029Spjd	void		*m_owner;
239185029Spjd	boolean_t	initialized;
240185029Spjd	mutex_t		m_lock;
241168404Spjd} kmutex_t;
242168404Spjd
243168404Spjd#define	MUTEX_DEFAULT	USYNC_THREAD
244219089Spjd#undef	MUTEX_HELD
245219089Spjd#undef	MUTEX_NOT_HELD
246168404Spjd#define	MUTEX_HELD(m)	((m)->m_owner == curthread)
247219089Spjd#define	MUTEX_NOT_HELD(m) (!MUTEX_HELD(m))
248219089Spjd#define	_mutex_held(m)	pthread_mutex_isowned_np(m)
249168404Spjd
250168404Spjd/*
251168404Spjd * Argh -- we have to get cheesy here because the kernel and userland
252168404Spjd * have different signatures for the same routine.
253168404Spjd */
254168404Spjd//extern int _mutex_init(mutex_t *mp, int type, void *arg);
255168404Spjd//extern int _mutex_destroy(mutex_t *mp);
256219089Spjd//extern int _mutex_owned(mutex_t *mp);
257168404Spjd
258168404Spjd#define	mutex_init(mp, b, c, d)		zmutex_init((kmutex_t *)(mp))
259168404Spjd#define	mutex_destroy(mp)		zmutex_destroy((kmutex_t *)(mp))
260195515Spjd#define	mutex_owned(mp)			zmutex_owned((kmutex_t *)(mp))
261168404Spjd
262168404Spjdextern void zmutex_init(kmutex_t *mp);
263168404Spjdextern void zmutex_destroy(kmutex_t *mp);
264195515Spjdextern int zmutex_owned(kmutex_t *mp);
265168404Spjdextern void mutex_enter(kmutex_t *mp);
266168404Spjdextern void mutex_exit(kmutex_t *mp);
267168404Spjdextern int mutex_tryenter(kmutex_t *mp);
268168404Spjdextern void *mutex_owner(kmutex_t *mp);
269168404Spjd
270168404Spjd/*
271168404Spjd * RW locks
272168404Spjd */
273168404Spjdtypedef struct krwlock {
274168404Spjd	int		rw_count;
275168404Spjd	void		*rw_owner;
276185029Spjd	boolean_t	initialized;
277168404Spjd	rwlock_t	rw_lock;
278168404Spjd} krwlock_t;
279168404Spjd
280168404Spjdtypedef int krw_t;
281168404Spjd
282168404Spjd#define	RW_READER	0
283168404Spjd#define	RW_WRITER	1
284168404Spjd#define	RW_DEFAULT	USYNC_THREAD
285168404Spjd
286168404Spjd#undef RW_READ_HELD
287185029Spjd#define RW_READ_HELD(x)		((x)->rw_owner == NULL && (x)->rw_count > 0)
288168404Spjd
289168404Spjd#undef RW_WRITE_HELD
290168404Spjd#define	RW_WRITE_HELD(x)	((x)->rw_owner == curthread)
291168404Spjd#define	RW_LOCK_HELD(x)		rw_lock_held(x)
292168404Spjd
293248571Smm#undef RW_LOCK_HELD
294248571Smm#define	RW_LOCK_HELD(x)		(RW_READ_HELD(x) || RW_WRITE_HELD(x))
295248571Smm
296168404Spjdextern void rw_init(krwlock_t *rwlp, char *name, int type, void *arg);
297168404Spjdextern void rw_destroy(krwlock_t *rwlp);
298168404Spjdextern void rw_enter(krwlock_t *rwlp, krw_t rw);
299168404Spjdextern int rw_tryenter(krwlock_t *rwlp, krw_t rw);
300168404Spjdextern int rw_tryupgrade(krwlock_t *rwlp);
301168404Spjdextern void rw_exit(krwlock_t *rwlp);
302168404Spjdextern int rw_lock_held(krwlock_t *rwlp);
303168404Spjd#define	rw_downgrade(rwlp) do { } while (0)
304168404Spjd
305185029Spjdextern uid_t crgetuid(cred_t *cr);
306248571Smmextern uid_t crgetruid(cred_t *cr);
307185029Spjdextern gid_t crgetgid(cred_t *cr);
308185029Spjdextern int crgetngroups(cred_t *cr);
309185029Spjdextern gid_t *crgetgroups(cred_t *cr);
310185029Spjd
311168404Spjd/*
312168404Spjd * Condition variables
313168404Spjd */
314168404Spjdtypedef cond_t kcondvar_t;
315168404Spjd
316168404Spjd#define	CV_DEFAULT	USYNC_THREAD
317168404Spjd
318168404Spjdextern void cv_init(kcondvar_t *cv, char *name, int type, void *arg);
319168404Spjdextern void cv_destroy(kcondvar_t *cv);
320168404Spjdextern void cv_wait(kcondvar_t *cv, kmutex_t *mp);
321168404Spjdextern clock_t cv_timedwait(kcondvar_t *cv, kmutex_t *mp, clock_t abstime);
322255437Sdelphijextern clock_t cv_timedwait_hires(kcondvar_t *cvp, kmutex_t *mp, hrtime_t tim,
323255437Sdelphij    hrtime_t res, int flag);
324168404Spjdextern void cv_signal(kcondvar_t *cv);
325168404Spjdextern void cv_broadcast(kcondvar_t *cv);
326168404Spjd
327168404Spjd/*
328248571Smm * Thread-specific data
329248571Smm */
330248571Smm#define	tsd_get(k) pthread_getspecific(k)
331248571Smm#define	tsd_set(k, v) pthread_setspecific(k, v)
332248571Smm#define	tsd_create(kp, d) pthread_key_create(kp, d)
333248571Smm#define	tsd_destroy(kp) /* nothing */
334248571Smm
335248571Smm/*
336168404Spjd * Kernel memory
337168404Spjd */
338168404Spjd#define	KM_SLEEP		UMEM_NOFAIL
339185029Spjd#define	KM_PUSHPAGE		KM_SLEEP
340168404Spjd#define	KM_NOSLEEP		UMEM_DEFAULT
341168404Spjd#define	KMC_NODEBUG		UMC_NODEBUG
342219089Spjd#define	KMC_NOTOUCH		0	/* not needed for userland caches */
343230629Skmacy#define	KM_NODEBUG		0
344168404Spjd#define	kmem_alloc(_s, _f)	umem_alloc(_s, _f)
345168404Spjd#define	kmem_zalloc(_s, _f)	umem_zalloc(_s, _f)
346168404Spjd#define	kmem_free(_b, _s)	umem_free(_b, _s)
347168584Spjd#define	kmem_size()		(physmem * PAGESIZE)
348168404Spjd#define	kmem_cache_create(_a, _b, _c, _d, _e, _f, _g, _h, _i) \
349168404Spjd	umem_cache_create(_a, _b, _c, _d, _e, _f, _g, _h, _i)
350168404Spjd#define	kmem_cache_destroy(_c)	umem_cache_destroy(_c)
351168404Spjd#define	kmem_cache_alloc(_c, _f) umem_cache_alloc(_c, _f)
352168404Spjd#define	kmem_cache_free(_c, _b)	umem_cache_free(_c, _b)
353168404Spjd#define	kmem_debugging()	0
354219089Spjd#define	kmem_cache_reap_now(_c)		/* nothing */
355219089Spjd#define	kmem_cache_set_move(_c, _cb)	/* nothing */
356219089Spjd#define	POINTER_INVALIDATE(_pp)		/* nothing */
357219089Spjd#define	POINTER_IS_VALID(_p)	0
358168404Spjd
359168404Spjdtypedef umem_cache_t kmem_cache_t;
360168404Spjd
361219089Spjdtypedef enum kmem_cbrc {
362219089Spjd	KMEM_CBRC_YES,
363219089Spjd	KMEM_CBRC_NO,
364219089Spjd	KMEM_CBRC_LATER,
365219089Spjd	KMEM_CBRC_DONT_NEED,
366219089Spjd	KMEM_CBRC_DONT_KNOW
367219089Spjd} kmem_cbrc_t;
368219089Spjd
369168404Spjd/*
370168404Spjd * Task queues
371168404Spjd */
372168404Spjdtypedef struct taskq taskq_t;
373168404Spjdtypedef uintptr_t taskqid_t;
374168404Spjdtypedef void (task_func_t)(void *);
375168404Spjd
376260742Savgtypedef struct taskq_ent {
377260742Savg	struct taskq_ent	*tqent_next;
378260742Savg	struct taskq_ent	*tqent_prev;
379260742Savg	task_func_t		*tqent_func;
380260742Savg	void			*tqent_arg;
381260742Savg	uintptr_t		tqent_flags;
382260742Savg} taskq_ent_t;
383260742Savg
384260742Savg#define	TQENT_FLAG_PREALLOC	0x1	/* taskq_dispatch_ent used */
385260742Savg
386168404Spjd#define	TASKQ_PREPOPULATE	0x0001
387168404Spjd#define	TASKQ_CPR_SAFE		0x0002	/* Use CPR safe protocol */
388168404Spjd#define	TASKQ_DYNAMIC		0x0004	/* Use dynamic thread scheduling */
389219089Spjd#define	TASKQ_THREADS_CPU_PCT	0x0008	/* Scale # threads by # cpus */
390219089Spjd#define	TASKQ_DC_BATCH		0x0010	/* Mark threads as batch */
391168404Spjd
392168404Spjd#define	TQ_SLEEP	KM_SLEEP	/* Can block for memory */
393168404Spjd#define	TQ_NOSLEEP	KM_NOSLEEP	/* cannot block for memory; may fail */
394219089Spjd#define	TQ_NOQUEUE	0x02		/* Do not enqueue if can't dispatch */
395219089Spjd#define	TQ_FRONT	0x08		/* Queue in front */
396168404Spjd
397260742Savg
398208047Smmextern taskq_t *system_taskq;
399208047Smm
400168404Spjdextern taskq_t	*taskq_create(const char *, int, pri_t, int, int, uint_t);
401219089Spjd#define	taskq_create_proc(a, b, c, d, e, p, f) \
402219089Spjd	    (taskq_create(a, b, c, d, e, f))
403219089Spjd#define	taskq_create_sysdc(a, b, d, e, p, dc, f) \
404219089Spjd	    (taskq_create(a, b, maxclsyspri, d, e, f))
405168404Spjdextern taskqid_t taskq_dispatch(taskq_t *, task_func_t, void *, uint_t);
406260742Savgextern void	taskq_dispatch_ent(taskq_t *, task_func_t, void *, uint_t,
407260742Savg    taskq_ent_t *);
408168404Spjdextern void	taskq_destroy(taskq_t *);
409168404Spjdextern void	taskq_wait(taskq_t *);
410168404Spjdextern int	taskq_member(taskq_t *, void *);
411208047Smmextern void	system_taskq_init(void);
412219089Spjdextern void	system_taskq_fini(void);
413168404Spjd
414219089Spjd#define	taskq_dispatch_safe(tq, func, arg, flags, task)			\
415219089Spjd	taskq_dispatch((tq), (func), (arg), (flags))
416208166Spjd
417185029Spjd#define	XVA_MAPSIZE	3
418185029Spjd#define	XVA_MAGIC	0x78766174
419185029Spjd
420168404Spjd/*
421168404Spjd * vnodes
422168404Spjd */
423168404Spjdtypedef struct vnode {
424168404Spjd	uint64_t	v_size;
425168404Spjd	int		v_fd;
426168404Spjd	char		*v_path;
427268653Sdelphij	int		v_dump_fd;
428168404Spjd} vnode_t;
429168404Spjd
430268653Sdelphijextern char *vn_dumpdir;
431219089Spjd#define	AV_SCANSTAMP_SZ	32		/* length of anti-virus scanstamp */
432185029Spjd
433185029Spjdtypedef struct xoptattr {
434185029Spjd	timestruc_t	xoa_createtime;	/* Create time of file */
435185029Spjd	uint8_t		xoa_archive;
436185029Spjd	uint8_t		xoa_system;
437185029Spjd	uint8_t		xoa_readonly;
438185029Spjd	uint8_t		xoa_hidden;
439185029Spjd	uint8_t		xoa_nounlink;
440185029Spjd	uint8_t		xoa_immutable;
441185029Spjd	uint8_t		xoa_appendonly;
442185029Spjd	uint8_t		xoa_nodump;
443185029Spjd	uint8_t		xoa_settable;
444185029Spjd	uint8_t		xoa_opaque;
445185029Spjd	uint8_t		xoa_av_quarantined;
446185029Spjd	uint8_t		xoa_av_modified;
447219089Spjd	uint8_t		xoa_av_scanstamp[AV_SCANSTAMP_SZ];
448219089Spjd	uint8_t		xoa_reparse;
449219089Spjd	uint8_t		xoa_offline;
450219089Spjd	uint8_t		xoa_sparse;
451185029Spjd} xoptattr_t;
452185029Spjd
453168404Spjdtypedef struct vattr {
454168404Spjd	uint_t		va_mask;	/* bit-mask of attributes */
455168404Spjd	u_offset_t	va_size;	/* file size in bytes */
456168404Spjd} vattr_t;
457168404Spjd
458168404Spjd
459185029Spjdtypedef struct xvattr {
460185029Spjd	vattr_t		xva_vattr;	/* Embedded vattr structure */
461185029Spjd	uint32_t	xva_magic;	/* Magic Number */
462185029Spjd	uint32_t	xva_mapsize;	/* Size of attr bitmap (32-bit words) */
463185029Spjd	uint32_t	*xva_rtnattrmapp;	/* Ptr to xva_rtnattrmap[] */
464185029Spjd	uint32_t	xva_reqattrmap[XVA_MAPSIZE];	/* Requested attrs */
465185029Spjd	uint32_t	xva_rtnattrmap[XVA_MAPSIZE];	/* Returned attrs */
466185029Spjd	xoptattr_t	xva_xoptattrs;	/* Optional attributes */
467185029Spjd} xvattr_t;
468185029Spjd
469185029Spjdtypedef struct vsecattr {
470185029Spjd	uint_t		vsa_mask;	/* See below */
471185029Spjd	int		vsa_aclcnt;	/* ACL entry count */
472185029Spjd	void		*vsa_aclentp;	/* pointer to ACL entries */
473185029Spjd	int		vsa_dfaclcnt;	/* default ACL entry count */
474185029Spjd	void		*vsa_dfaclentp;	/* pointer to default ACL entries */
475185029Spjd	size_t		vsa_aclentsz;	/* ACE size in bytes of vsa_aclentp */
476185029Spjd} vsecattr_t;
477185029Spjd
478185029Spjd#define	AT_TYPE		0x00001
479185029Spjd#define	AT_MODE		0x00002
480185029Spjd#define	AT_UID		0x00004
481185029Spjd#define	AT_GID		0x00008
482185029Spjd#define	AT_FSID		0x00010
483185029Spjd#define	AT_NODEID	0x00020
484185029Spjd#define	AT_NLINK	0x00040
485185029Spjd#define	AT_SIZE		0x00080
486185029Spjd#define	AT_ATIME	0x00100
487185029Spjd#define	AT_MTIME	0x00200
488185029Spjd#define	AT_CTIME	0x00400
489185029Spjd#define	AT_RDEV		0x00800
490185029Spjd#define	AT_BLKSIZE	0x01000
491185029Spjd#define	AT_NBLOCKS	0x02000
492185029Spjd#define	AT_SEQ		0x08000
493185029Spjd#define	AT_XVATTR	0x10000
494185029Spjd
495168404Spjd#define	CRCREAT		0
496168404Spjd
497219089Spjdextern int fop_getattr(vnode_t *vp, vattr_t *vap);
498219089Spjd
499185029Spjd#define	VOP_CLOSE(vp, f, c, o, cr, ct)	0
500185029Spjd#define	VOP_PUTPAGE(vp, of, sz, fl, cr, ct)	0
501219089Spjd#define	VOP_GETATTR(vp, vap, cr)  fop_getattr((vp), (vap));
502168404Spjd
503185029Spjd#define	VOP_FSYNC(vp, f, cr, ct)	fsync((vp)->v_fd)
504168404Spjd
505219089Spjd#define	VN_RELE(vp)			vn_close(vp, 0, NULL, NULL)
506191906Skmacy#define	VN_RELE_ASYNC(vp, taskq)	vn_close(vp, 0, NULL, NULL)
507168404Spjd
508185029Spjd#define	vn_lock(vp, type)
509185029Spjd#define	VOP_UNLOCK(vp, type)
510185029Spjd
511168404Spjdextern int vn_open(char *path, int x1, int oflags, int mode, vnode_t **vpp,
512168404Spjd    int x2, int x3);
513168404Spjdextern int vn_openat(char *path, int x1, int oflags, int mode, vnode_t **vpp,
514185029Spjd    int x2, int x3, vnode_t *vp, int fd);
515168404Spjdextern int vn_rdwr(int uio, vnode_t *vp, void *addr, ssize_t len,
516168404Spjd    offset_t offset, int x1, int x2, rlim64_t x3, void *x4, ssize_t *residp);
517185029Spjdextern void vn_close(vnode_t *vp, int openflag, cred_t *cr, kthread_t *td);
518168404Spjd
519168404Spjd#define	vn_remove(path, x1, x2)		remove(path)
520168404Spjd#define	vn_rename(from, to, seg)	rename((from), (to))
521168404Spjd#define	vn_is_readonly(vp)		B_FALSE
522168404Spjd
523168404Spjdextern vnode_t *rootdir;
524168404Spjd
525168404Spjd#include <sys/file.h>		/* for FREAD, FWRITE, etc */
526168404Spjd#define	FTRUNC	O_TRUNC
527168404Spjd
528168404Spjd/*
529168404Spjd * Random stuff
530168404Spjd */
531219089Spjd#define	ddi_get_lbolt()		(gethrtime() >> 23)
532219089Spjd#define	ddi_get_lbolt64()	(gethrtime() >> 23)
533219089Spjd#define	hz	119	/* frequency when using gethrtime() >> 23 for lbolt */
534168404Spjd
535168404Spjdextern void delay(clock_t ticks);
536168404Spjd
537247265Smm#define	SEC_TO_TICK(sec)	((sec) * hz)
538247265Smm#define	NSEC_TO_TICK(usec)	((usec) / (NANOSEC / hz))
539247265Smm
540168404Spjd#define	gethrestime_sec() time(NULL)
541219089Spjd#define	gethrestime(t) \
542219089Spjd	do {\
543219089Spjd		(t)->tv_sec = gethrestime_sec();\
544219089Spjd		(t)->tv_nsec = 0;\
545219089Spjd	} while (0);
546168404Spjd
547168404Spjd#define	max_ncpus	64
548168404Spjd
549168404Spjd#define	minclsyspri	60
550168404Spjd#define	maxclsyspri	99
551168404Spjd
552168404Spjd#define	CPU_SEQID	(thr_self() & (max_ncpus - 1))
553168404Spjd
554219089Spjd#define	kcred		NULL
555219089Spjd#define	CRED()		NULL
556219089Spjd
557185029Spjd#ifndef ptob
558185029Spjd#define	ptob(x)		((x) * PAGESIZE)
559185029Spjd#endif
560168404Spjd
561168404Spjdextern uint64_t physmem;
562168404Spjd
563265740Sdelphijextern int highbit64(uint64_t i);
564168404Spjdextern int random_get_bytes(uint8_t *ptr, size_t len);
565168404Spjdextern int random_get_pseudo_bytes(uint8_t *ptr, size_t len);
566168404Spjd
567168404Spjdextern void kernel_init(int);
568168404Spjdextern void kernel_fini(void);
569168404Spjd
570168404Spjdstruct spa;
571168404Spjdextern void nicenum(uint64_t num, char *buf);
572168404Spjdextern void show_pool_stats(struct spa *);
573168404Spjd
574168404Spjdtypedef struct callb_cpr {
575168404Spjd	kmutex_t	*cc_lockp;
576168404Spjd} callb_cpr_t;
577168404Spjd
578168404Spjd#define	CALLB_CPR_INIT(cp, lockp, func, name)	{		\
579168404Spjd	(cp)->cc_lockp = lockp;					\
580168404Spjd}
581168404Spjd
582168404Spjd#define	CALLB_CPR_SAFE_BEGIN(cp) {				\
583168404Spjd	ASSERT(MUTEX_HELD((cp)->cc_lockp));			\
584168404Spjd}
585168404Spjd
586168404Spjd#define	CALLB_CPR_SAFE_END(cp, lockp) {				\
587168404Spjd	ASSERT(MUTEX_HELD((cp)->cc_lockp));			\
588168404Spjd}
589168404Spjd
590168404Spjd#define	CALLB_CPR_EXIT(cp) {					\
591168404Spjd	ASSERT(MUTEX_HELD((cp)->cc_lockp));			\
592168404Spjd	mutex_exit((cp)->cc_lockp);				\
593168404Spjd}
594168404Spjd
595168404Spjd#define	zone_dataset_visible(x, y)	(1)
596168404Spjd#define	INGLOBALZONE(z)			(1)
597168404Spjd
598219089Spjdextern char *kmem_asprintf(const char *fmt, ...);
599248571Smm#define	strfree(str) kmem_free((str), strlen(str) + 1)
600219089Spjd
601168498Spjd/*
602168498Spjd * Hostname information
603168498Spjd */
604168498Spjdextern struct utsname utsname;
605219089Spjdextern char hw_serial[];	/* for userland-emulated hostid access */
606168498Spjdextern int ddi_strtoul(const char *str, char **nptr, int base,
607168498Spjd    unsigned long *result);
608168498Spjd
609219089Spjdextern int ddi_strtoull(const char *str, char **nptr, int base,
610219089Spjd    u_longlong_t *result);
611219089Spjd
612168404Spjd/* ZFS Boot Related stuff. */
613168404Spjd
614168404Spjdstruct _buf {
615168404Spjd	intptr_t	_fd;
616168404Spjd};
617168404Spjd
618168404Spjdstruct bootstat {
619168404Spjd	uint64_t st_size;
620168404Spjd};
621168404Spjd
622185029Spjdtypedef struct ace_object {
623185029Spjd	uid_t		a_who;
624185029Spjd	uint32_t	a_access_mask;
625185029Spjd	uint16_t	a_flags;
626185029Spjd	uint16_t	a_type;
627185029Spjd	uint8_t		a_obj_type[16];
628185029Spjd	uint8_t		a_inherit_obj_type[16];
629185029Spjd} ace_object_t;
630185029Spjd
631185029Spjd
632185029Spjd#define	ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE	0x05
633185029Spjd#define	ACE_ACCESS_DENIED_OBJECT_ACE_TYPE	0x06
634185029Spjd#define	ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE	0x07
635185029Spjd#define	ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE	0x08
636185029Spjd
637168404Spjdextern struct _buf *kobj_open_file(char *name);
638168404Spjdextern int kobj_read_file(struct _buf *file, char *buf, unsigned size,
639168404Spjd    unsigned off);
640168404Spjdextern void kobj_close_file(struct _buf *file);
641168404Spjdextern int kobj_get_filesize(struct _buf *file, uint64_t *size);
642185029Spjdextern int zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr);
643185029Spjdextern int zfs_secpolicy_rename_perms(const char *from, const char *to,
644185029Spjd    cred_t *cr);
645185029Spjdextern int zfs_secpolicy_destroy_perms(const char *name, cred_t *cr);
646185029Spjdextern zoneid_t getzoneid(void);
647168404Spjd/* Random compatibility stuff. */
648168404Spjd#define	lbolt	(gethrtime() >> 23)
649168404Spjd#define	lbolt64	(gethrtime() >> 23)
650168404Spjd
651168404Spjdextern uint64_t physmem;
652168404Spjd
653168404Spjd#define	gethrestime_sec()	time(NULL)
654168404Spjd
655168404Spjd#define	pwrite64(d, p, n, o)	pwrite(d, p, n, o)
656168404Spjd#define	readdir64(d)		readdir(d)
657168404Spjd#define	SIGPENDING(td)		(0)
658168404Spjd#define	root_mount_wait()	do { } while (0)
659168825Spjd#define	root_mounted()		(1)
660168404Spjd
661168404Spjdstruct file {
662168404Spjd	void *dummy;
663168404Spjd};
664168404Spjd
665168404Spjd#define	FCREAT	O_CREAT
666168404Spjd#define	FOFFMAX	0x0
667168404Spjd
668185029Spjd/* SID stuff */
669185029Spjdtypedef struct ksiddomain {
670185029Spjd	uint_t	kd_ref;
671185029Spjd	uint_t	kd_len;
672185029Spjd	char	*kd_name;
673185029Spjd} ksiddomain_t;
674185029Spjd
675185029Spjdksiddomain_t *ksid_lookupdomain(const char *);
676185029Spjdvoid ksiddomain_rele(ksiddomain_t *);
677185029Spjd
678209962Smmtypedef	uint32_t	idmap_rid_t;
679209962Smm
680219089Spjd#define	DDI_SLEEP	KM_SLEEP
681219089Spjd#define	ddi_log_sysevent(_a, _b, _c, _d, _e, _f, _g)	(0)
682219089Spjd
683168404Spjd#define	SX_SYSINIT(name, lock, desc)
684168404Spjd
685254591Sgibbs#define SYSCTL_HANDLER_ARGS struct sysctl_oid *oidp, void *arg1,	\
686254591Sgibbs	intptr_t arg2, struct sysctl_req *req
687254591Sgibbs
688254591Sgibbs/*
689254591Sgibbs * This describes the access space for a sysctl request.  This is needed
690254591Sgibbs * so that we can use the interface from the kernel or from user-space.
691254591Sgibbs */
692254591Sgibbsstruct sysctl_req {
693254591Sgibbs	struct thread	*td;		/* used for access checking */
694254591Sgibbs	int		lock;		/* wiring state */
695254591Sgibbs	void		*oldptr;
696254591Sgibbs	size_t		oldlen;
697254591Sgibbs	size_t		oldidx;
698254591Sgibbs	int		(*oldfunc)(struct sysctl_req *, const void *, size_t);
699254591Sgibbs	void		*newptr;
700254591Sgibbs	size_t		newlen;
701254591Sgibbs	size_t		newidx;
702254591Sgibbs	int		(*newfunc)(struct sysctl_req *, void *, size_t);
703254591Sgibbs	size_t		validlen;
704254591Sgibbs	int		flags;
705254591Sgibbs};
706254591Sgibbs
707254591SgibbsSLIST_HEAD(sysctl_oid_list, sysctl_oid);
708254591Sgibbs
709254591Sgibbs/*
710254591Sgibbs * This describes one "oid" in the MIB tree.  Potentially more nodes can
711254591Sgibbs * be hidden behind it, expanded by the handler.
712254591Sgibbs */
713254591Sgibbsstruct sysctl_oid {
714254591Sgibbs	struct sysctl_oid_list *oid_parent;
715254591Sgibbs	SLIST_ENTRY(sysctl_oid) oid_link;
716254591Sgibbs	int		oid_number;
717254591Sgibbs	u_int		oid_kind;
718254591Sgibbs	void		*oid_arg1;
719254591Sgibbs	intptr_t	oid_arg2;
720254591Sgibbs	const char	*oid_name;
721254591Sgibbs	int 		(*oid_handler)(SYSCTL_HANDLER_ARGS);
722254591Sgibbs	const char	*oid_fmt;
723254591Sgibbs	int		oid_refcnt;
724254591Sgibbs	u_int		oid_running;
725254591Sgibbs	const char	*oid_descr;
726254591Sgibbs};
727254591Sgibbs
728168404Spjd#define	SYSCTL_DECL(...)
729168582Spjd#define	SYSCTL_NODE(...)
730168404Spjd#define	SYSCTL_INT(...)
731185029Spjd#define	SYSCTL_UINT(...)
732168582Spjd#define	SYSCTL_ULONG(...)
733254591Sgibbs#define	SYSCTL_PROC(...)
734185029Spjd#define	SYSCTL_QUAD(...)
735217367Smdf#define	SYSCTL_UQUAD(...)
736168404Spjd#ifdef TUNABLE_INT
737168404Spjd#undef TUNABLE_INT
738168582Spjd#undef TUNABLE_ULONG
739185029Spjd#undef TUNABLE_QUAD
740168404Spjd#endif
741168404Spjd#define	TUNABLE_INT(...)
742168582Spjd#define	TUNABLE_ULONG(...)
743185029Spjd#define	TUNABLE_QUAD(...)
744168404Spjd
745254591Sgibbsint sysctl_handle_64(SYSCTL_HANDLER_ARGS);
746254591Sgibbs
747168404Spjd/* Errors */
748168404Spjd
749168404Spjd#ifndef	ERESTART
750168404Spjd#define	ERESTART	(-1)
751168404Spjd#endif
752168404Spjd
753247265Smm#ifdef illumos
754247265Smm/*
755247265Smm * Cyclic information
756247265Smm */
757247265Smmextern kmutex_t cpu_lock;
758247265Smm
759247265Smmtypedef uintptr_t cyclic_id_t;
760247265Smmtypedef uint16_t cyc_level_t;
761247265Smmtypedef void (*cyc_func_t)(void *);
762247265Smm
763247265Smm#define	CY_LOW_LEVEL	0
764247265Smm#define	CY_INFINITY	INT64_MAX
765247265Smm#define	CYCLIC_NONE	((cyclic_id_t)0)
766247265Smm
767247265Smmtypedef struct cyc_time {
768247265Smm	hrtime_t cyt_when;
769247265Smm	hrtime_t cyt_interval;
770247265Smm} cyc_time_t;
771247265Smm
772247265Smmtypedef struct cyc_handler {
773247265Smm	cyc_func_t cyh_func;
774247265Smm	void *cyh_arg;
775247265Smm	cyc_level_t cyh_level;
776247265Smm} cyc_handler_t;
777247265Smm
778247265Smmextern cyclic_id_t cyclic_add(cyc_handler_t *, cyc_time_t *);
779247265Smmextern void cyclic_remove(cyclic_id_t);
780247265Smmextern int cyclic_reprogram(cyclic_id_t, hrtime_t);
781247265Smm#endif	/* illumos */
782247265Smm
783263393Sdelphij#ifdef illumos
784263393Sdelphij/*
785263393Sdelphij * Buf structure
786263393Sdelphij */
787263393Sdelphij#define	B_BUSY		0x0001
788263393Sdelphij#define	B_DONE		0x0002
789263393Sdelphij#define	B_ERROR		0x0004
790263393Sdelphij#define	B_READ		0x0040	/* read when I/O occurs */
791263393Sdelphij#define	B_WRITE		0x0100	/* non-read pseudo-flag */
792263393Sdelphij
793263393Sdelphijtypedef struct buf {
794263393Sdelphij	int	b_flags;
795263393Sdelphij	size_t b_bcount;
796263393Sdelphij	union {
797263393Sdelphij		caddr_t b_addr;
798263393Sdelphij	} b_un;
799263393Sdelphij
800263393Sdelphij	lldaddr_t	_b_blkno;
801263393Sdelphij#define	b_lblkno	_b_blkno._f
802263393Sdelphij	size_t	b_resid;
803263393Sdelphij	size_t	b_bufsize;
804263393Sdelphij	int	(*b_iodone)(struct buf *);
805263393Sdelphij	int	b_error;
806263393Sdelphij	void	*b_private;
807263393Sdelphij} buf_t;
808263393Sdelphij
809263393Sdelphijextern void bioinit(buf_t *);
810263393Sdelphijextern void biodone(buf_t *);
811263393Sdelphijextern void bioerror(buf_t *, int);
812263393Sdelphijextern int geterror(buf_t *);
813263393Sdelphij#endif
814263393Sdelphij
815169301Spjd#ifdef	__cplusplus
816169301Spjd}
817169301Spjd#endif
818169301Spjd
819168404Spjd#endif	/* _SYS_ZFS_CONTEXT_H */
820