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 (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23  * Copyright (c) 2012, 2016 by Delphix. All rights reserved.
24  * Copyright (c) 2012, Joyent, Inc. All rights reserved.
25  */
26 /*
27  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
28  */
29 
30 #ifndef _SYS_ZFS_CONTEXT_H
31 #define	_SYS_ZFS_CONTEXT_H
32 
33 #ifdef	__cplusplus
34 extern "C" {
35 #endif
36 
37 #define	_SYS_MUTEX_H
38 #define	_SYS_RWLOCK_H
39 #define	_SYS_CONDVAR_H
40 #define	_SYS_SYSTM_H
41 #define	_SYS_T_LOCK_H
42 #define	_SYS_VNODE_H
43 #define	_SYS_VFS_H
44 #define	_SYS_SUNDDI_H
45 #define	_SYS_CALLB_H
46 #define	_SYS_SCHED_H_
47 
48 #include <solaris.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <stddef.h>
52 #include <stdarg.h>
53 #include <fcntl.h>
54 #include <unistd.h>
55 #include <errno.h>
56 #include <string.h>
57 #include <strings.h>
58 #include <thread.h>
59 #include <assert.h>
60 #include <limits.h>
61 #include <dirent.h>
62 #include <time.h>
63 #include <math.h>
64 #include <umem.h>
65 #include <inttypes.h>
66 #include <fsshare.h>
67 #include <pthread.h>
68 #include <sched.h>
69 #include <setjmp.h>
70 #include <sys/debug.h>
71 #include <sys/note.h>
72 #include <sys/types.h>
73 #include <sys/cred.h>
74 #include <sys/atomic.h>
75 #include <sys/sysmacros.h>
76 #include <sys/bitmap.h>
77 #include <sys/resource.h>
78 #include <sys/byteorder.h>
79 #include <sys/list.h>
80 #include <sys/time.h>
81 #include <sys/uio.h>
82 #include <sys/mntent.h>
83 #include <sys/mnttab.h>
84 #include <sys/zfs_debug.h>
85 #include <sys/sdt.h>
86 #include <sys/kstat.h>
87 #include <sys/u8_textprep.h>
88 #include <sys/kernel.h>
89 #include <sys/disk.h>
90 #include <sys/sysevent.h>
91 #include <sys/sysevent/eventdefs.h>
92 #include <sys/sysevent/dev.h>
93 #include <machine/atomic.h>
94 #include <sys/debug.h>
95 #ifdef illumos
96 #include "zfs.h"
97 #endif
98 
99 #define	ZFS_EXPORTS_PATH	"/etc/zfs/exports"
100 
101 /*
102  * Debugging
103  */
104 
105 /*
106  * Note that we are not using the debugging levels.
107  */
108 
109 #define	CE_CONT		0	/* continuation		*/
110 #define	CE_NOTE		1	/* notice		*/
111 #define	CE_WARN		2	/* warning		*/
112 #define	CE_PANIC	3	/* panic		*/
113 #define	CE_IGNORE	4	/* print nothing	*/
114 
115 /*
116  * ZFS debugging
117  */
118 
119 #define	ZFS_LOG(...)	do {  } while (0)
120 
121 typedef u_longlong_t      rlim64_t;
122 #define	RLIM64_INFINITY	((rlim64_t)-3)
123 
124 #ifdef ZFS_DEBUG
125 extern void dprintf_setup(int *argc, char **argv);
126 #endif /* ZFS_DEBUG */
127 
128 extern void cmn_err(int, const char *, ...);
129 extern void vcmn_err(int, const char *, __va_list);
130 extern void panic(const char *, ...)  __NORETURN;
131 extern void vpanic(const char *, __va_list)  __NORETURN;
132 
133 #define	fm_panic	panic
134 
135 extern int aok;
136 
137 /*
138  * DTrace SDT probes have different signatures in userland than they do in
139  * the kernel.  If they're being used in kernel code, re-define them out of
140  * existence for their counterparts in libzpool.
141  *
142  * Here's an example of how to use the set-error probes in userland:
143  * zfs$target:::set-error /arg0 == EBUSY/ {stack();}
144  *
145  * Here's an example of how to use DTRACE_PROBE probes in userland:
146  * If there is a probe declared as follows:
147  * DTRACE_PROBE2(zfs__probe_name, uint64_t, blkid, dnode_t *, dn);
148  * Then you can use it as follows:
149  * zfs$target:::probe2 /copyinstr(arg0) == "zfs__probe_name"/
150  *     {printf("%u %p\n", arg1, arg2);}
151  */
152 
153 #ifdef DTRACE_PROBE
154 #undef	DTRACE_PROBE
155 #endif	/* DTRACE_PROBE */
156 #ifdef illumos
157 #define	DTRACE_PROBE(a) \
158 	ZFS_PROBE0(#a)
159 #endif
160 
161 #ifdef DTRACE_PROBE1
162 #undef	DTRACE_PROBE1
163 #endif	/* DTRACE_PROBE1 */
164 #ifdef illumos
165 #define	DTRACE_PROBE1(a, b, c) \
166 	ZFS_PROBE1(#a, (unsigned long)c)
167 #endif
168 
169 #ifdef DTRACE_PROBE2
170 #undef	DTRACE_PROBE2
171 #endif	/* DTRACE_PROBE2 */
172 #ifdef illumos
173 #define	DTRACE_PROBE2(a, b, c, d, e) \
174 	ZFS_PROBE2(#a, (unsigned long)c, (unsigned long)e)
175 #endif
176 
177 #ifdef DTRACE_PROBE3
178 #undef	DTRACE_PROBE3
179 #endif	/* DTRACE_PROBE3 */
180 #ifdef illumos
181 #define	DTRACE_PROBE3(a, b, c, d, e, f, g) \
182 	ZFS_PROBE3(#a, (unsigned long)c, (unsigned long)e, (unsigned long)g)
183 #endif
184 
185 #ifdef DTRACE_PROBE4
186 #undef	DTRACE_PROBE4
187 #endif	/* DTRACE_PROBE4 */
188 #ifdef illumos
189 #define	DTRACE_PROBE4(a, b, c, d, e, f, g, h, i) \
190 	ZFS_PROBE4(#a, (unsigned long)c, (unsigned long)e, (unsigned long)g, \
191 	(unsigned long)i)
192 #endif
193 
194 #ifdef illumos
195 /*
196  * We use the comma operator so that this macro can be used without much
197  * additional code.  For example, "return (EINVAL);" becomes
198  * "return (SET_ERROR(EINVAL));".  Note that the argument will be evaluated
199  * twice, so it should not have side effects (e.g. something like:
200  * "return (SET_ERROR(log_error(EINVAL, info)));" would log the error twice).
201  */
202 #define	SET_ERROR(err)	(ZFS_SET_ERROR(err), err)
203 #else	/* !illumos */
204 
205 #define	DTRACE_PROBE(a)	((void)0)
206 #define	DTRACE_PROBE1(a, b, c)	((void)0)
207 #define	DTRACE_PROBE2(a, b, c, d, e)	((void)0)
208 #define	DTRACE_PROBE3(a, b, c, d, e, f, g)	((void)0)
209 #define	DTRACE_PROBE4(a, b, c, d, e, f, g, h, i)	((void)0)
210 
211 #define SET_ERROR(err) (err)
212 #endif	/* !illumos */
213 
214 /*
215  * Threads
216  */
217 #define	curthread	((void *)(uintptr_t)thr_self())
218 
219 #define	kpreempt(x)	sched_yield()
220 
221 typedef struct kthread kthread_t;
222 
223 #define	thread_create(stk, stksize, func, arg, len, pp, state, pri)	\
224 	zk_thread_create(func, arg)
225 #define	thread_exit() thr_exit(NULL)
226 #define	thread_join(t)	panic("libzpool cannot join threads")
227 
228 #define	newproc(f, a, cid, pri, ctp, pid)	(ENOSYS)
229 
230 /* in libzpool, p0 exists only to have its address taken */
231 struct proc {
232 	uintptr_t	this_is_never_used_dont_dereference_it;
233 };
234 
235 extern struct proc p0;
236 #define	curproc		(&p0)
237 
238 #define	PS_NONE		-1
239 
240 extern kthread_t *zk_thread_create(void (*func)(void*), void *arg);
241 
242 #define	issig(why)	(FALSE)
243 #define	ISSIG(thr, why)	(FALSE)
244 
245 /*
246  * Mutexes
247  */
248 typedef struct kmutex {
249 	void		*m_owner;
250 	boolean_t	initialized;
251 	mutex_t		m_lock;
252 } kmutex_t;
253 
254 #define	MUTEX_DEFAULT	USYNC_THREAD
255 #undef	MUTEX_HELD
256 #undef	MUTEX_NOT_HELD
257 #define	MUTEX_HELD(m)	((m)->m_owner == curthread)
258 #define	MUTEX_NOT_HELD(m) (!MUTEX_HELD(m))
259 #define	_mutex_held(m)	pthread_mutex_isowned_np(m)
260 
261 /*
262  * Argh -- we have to get cheesy here because the kernel and userland
263  * have different signatures for the same routine.
264  */
265 //extern int _mutex_init(mutex_t *mp, int type, void *arg);
266 //extern int _mutex_destroy(mutex_t *mp);
267 //extern int _mutex_owned(mutex_t *mp);
268 
269 #define	mutex_init(mp, b, c, d)		zmutex_init((kmutex_t *)(mp))
270 #define	mutex_destroy(mp)		zmutex_destroy((kmutex_t *)(mp))
271 #define	mutex_owned(mp)			zmutex_owned((kmutex_t *)(mp))
272 
273 extern void zmutex_init(kmutex_t *mp);
274 extern void zmutex_destroy(kmutex_t *mp);
275 extern int zmutex_owned(kmutex_t *mp);
276 extern void mutex_enter(kmutex_t *mp);
277 extern void mutex_exit(kmutex_t *mp);
278 extern int mutex_tryenter(kmutex_t *mp);
279 extern void *mutex_owner(kmutex_t *mp);
280 
281 /*
282  * RW locks
283  */
284 typedef struct krwlock {
285 	int		rw_count;
286 	void		*rw_owner;
287 	boolean_t	initialized;
288 	rwlock_t	rw_lock;
289 } krwlock_t;
290 
291 typedef int krw_t;
292 
293 #define	RW_READER	0
294 #define	RW_WRITER	1
295 #define	RW_DEFAULT	USYNC_THREAD
296 
297 #undef RW_READ_HELD
298 #define RW_READ_HELD(x)		((x)->rw_owner == NULL && (x)->rw_count > 0)
299 
300 #undef RW_WRITE_HELD
301 #define	RW_WRITE_HELD(x)	((x)->rw_owner == curthread)
302 #define	RW_LOCK_HELD(x)		rw_lock_held(x)
303 
304 #undef RW_LOCK_HELD
305 #define	RW_LOCK_HELD(x)		(RW_READ_HELD(x) || RW_WRITE_HELD(x))
306 
307 extern void rw_init(krwlock_t *rwlp, char *name, int type, void *arg);
308 extern void rw_destroy(krwlock_t *rwlp);
309 extern void rw_enter(krwlock_t *rwlp, krw_t rw);
310 extern int rw_tryenter(krwlock_t *rwlp, krw_t rw);
311 extern int rw_tryupgrade(krwlock_t *rwlp);
312 extern void rw_exit(krwlock_t *rwlp);
313 extern int rw_lock_held(krwlock_t *rwlp);
314 #define	rw_downgrade(rwlp) do { } while (0)
315 
316 extern uid_t crgetuid(cred_t *cr);
317 extern uid_t crgetruid(cred_t *cr);
318 extern gid_t crgetgid(cred_t *cr);
319 extern int crgetngroups(cred_t *cr);
320 extern gid_t *crgetgroups(cred_t *cr);
321 
322 /*
323  * Condition variables
324  */
325 typedef cond_t kcondvar_t;
326 
327 #define	CV_DEFAULT	USYNC_THREAD
328 #define	CALLOUT_FLAG_ABSOLUTE	0x2
329 
330 extern void cv_init(kcondvar_t *cv, char *name, int type, void *arg);
331 extern void cv_destroy(kcondvar_t *cv);
332 extern void cv_wait(kcondvar_t *cv, kmutex_t *mp);
333 extern clock_t cv_timedwait(kcondvar_t *cv, kmutex_t *mp, clock_t abstime);
334 extern clock_t cv_timedwait_hires(kcondvar_t *cvp, kmutex_t *mp, hrtime_t tim,
335     hrtime_t res, int flag);
336 extern void cv_signal(kcondvar_t *cv);
337 extern void cv_broadcast(kcondvar_t *cv);
338 
339 /*
340  * Thread-specific data
341  */
342 #define	tsd_get(k) pthread_getspecific(k)
343 #define	tsd_set(k, v) pthread_setspecific(k, v)
344 #define	tsd_create(kp, d) pthread_key_create(kp, d)
345 #define	tsd_destroy(kp) /* nothing */
346 
347 /*
348  * Kernel memory
349  */
350 #define	KM_SLEEP		UMEM_NOFAIL
351 #define	KM_PUSHPAGE		KM_SLEEP
352 #define	KM_NOSLEEP		UMEM_DEFAULT
353 #define	KM_NORMALPRI		0	/* not needed with UMEM_DEFAULT */
354 #define	KMC_NODEBUG		UMC_NODEBUG
355 #define	KMC_NOTOUCH		0	/* not needed for userland caches */
356 #define	KM_NODEBUG		0
357 #define	kmem_alloc(_s, _f)	umem_alloc(_s, _f)
358 #define	kmem_zalloc(_s, _f)	umem_zalloc(_s, _f)
359 #define	kmem_free(_b, _s)	umem_free(_b, _s)
360 #define	kmem_size()		(physmem * PAGESIZE)
361 #define	kmem_cache_create(_a, _b, _c, _d, _e, _f, _g, _h, _i) \
362 	umem_cache_create(_a, _b, _c, _d, _e, _f, _g, _h, _i)
363 #define	kmem_cache_destroy(_c)	umem_cache_destroy(_c)
364 #define	kmem_cache_alloc(_c, _f) umem_cache_alloc(_c, _f)
365 #define	kmem_cache_free(_c, _b)	umem_cache_free(_c, _b)
366 #define	kmem_debugging()	0
367 #define	kmem_cache_reap_active()	(B_FALSE)
368 #define	kmem_cache_reap_soon(_c)	/* nothing */
369 #define	kmem_cache_set_move(_c, _cb)	/* nothing */
370 #define	POINTER_INVALIDATE(_pp)		/* nothing */
371 #define	POINTER_IS_VALID(_p)	0
372 
373 typedef umem_cache_t kmem_cache_t;
374 
375 typedef enum kmem_cbrc {
376 	KMEM_CBRC_YES,
377 	KMEM_CBRC_NO,
378 	KMEM_CBRC_LATER,
379 	KMEM_CBRC_DONT_NEED,
380 	KMEM_CBRC_DONT_KNOW
381 } kmem_cbrc_t;
382 
383 /*
384  * Task queues
385  */
386 typedef struct taskq taskq_t;
387 typedef uintptr_t taskqid_t;
388 typedef void (task_func_t)(void *);
389 
390 typedef struct taskq_ent {
391 	struct taskq_ent	*tqent_next;
392 	struct taskq_ent	*tqent_prev;
393 	task_func_t		*tqent_func;
394 	void			*tqent_arg;
395 	uintptr_t		tqent_flags;
396 } taskq_ent_t;
397 
398 #define	TQENT_FLAG_PREALLOC	0x1	/* taskq_dispatch_ent used */
399 
400 #define	TASKQ_PREPOPULATE	0x0001
401 #define	TASKQ_CPR_SAFE		0x0002	/* Use CPR safe protocol */
402 #define	TASKQ_DYNAMIC		0x0004	/* Use dynamic thread scheduling */
403 #define	TASKQ_THREADS_CPU_PCT	0x0008	/* Scale # threads by # cpus */
404 #define	TASKQ_DC_BATCH		0x0010	/* Mark threads as batch */
405 
406 #define	TQ_SLEEP	KM_SLEEP	/* Can block for memory */
407 #define	TQ_NOSLEEP	KM_NOSLEEP	/* cannot block for memory; may fail */
408 #define	TQ_NOQUEUE	0x02		/* Do not enqueue if can't dispatch */
409 #define	TQ_FRONT	0x08		/* Queue in front */
410 
411 #define TASKQID_INVALID         ((taskqid_t)0)
412 
413 extern taskq_t *system_taskq;
414 
415 extern taskq_t	*taskq_create(const char *, int, pri_t, int, int, uint_t);
416 #define	taskq_create_proc(a, b, c, d, e, p, f) \
417 	    (taskq_create(a, b, c, d, e, f))
418 #define	taskq_create_sysdc(a, b, d, e, p, dc, f) \
419 	    (taskq_create(a, b, maxclsyspri, d, e, f))
420 extern taskqid_t taskq_dispatch(taskq_t *, task_func_t, void *, uint_t);
421 extern void	taskq_dispatch_ent(taskq_t *, task_func_t, void *, uint_t,
422     taskq_ent_t *);
423 extern void	taskq_destroy(taskq_t *);
424 extern void	taskq_wait(taskq_t *);
425 extern void	taskq_wait_id(taskq_t *, taskqid_t);
426 extern int	taskq_member(taskq_t *, void *);
427 extern void	system_taskq_init(void);
428 extern void	system_taskq_fini(void);
429 
430 #define	taskq_dispatch_safe(tq, func, arg, flags, task)			\
431 	taskq_dispatch((tq), (func), (arg), (flags))
432 
433 #define	XVA_MAPSIZE	3
434 #define	XVA_MAGIC	0x78766174
435 
436 /*
437  * vnodes
438  */
439 typedef struct vnode {
440 	uint64_t	v_size;
441 	int		v_fd;
442 	char		*v_path;
443 	int		v_dump_fd;
444 } vnode_t;
445 
446 extern char *vn_dumpdir;
447 #define	AV_SCANSTAMP_SZ	32		/* length of anti-virus scanstamp */
448 
449 typedef struct xoptattr {
450 	timestruc_t	xoa_createtime;	/* Create time of file */
451 	uint8_t		xoa_archive;
452 	uint8_t		xoa_system;
453 	uint8_t		xoa_readonly;
454 	uint8_t		xoa_hidden;
455 	uint8_t		xoa_nounlink;
456 	uint8_t		xoa_immutable;
457 	uint8_t		xoa_appendonly;
458 	uint8_t		xoa_nodump;
459 	uint8_t		xoa_settable;
460 	uint8_t		xoa_opaque;
461 	uint8_t		xoa_av_quarantined;
462 	uint8_t		xoa_av_modified;
463 	uint8_t		xoa_av_scanstamp[AV_SCANSTAMP_SZ];
464 	uint8_t		xoa_reparse;
465 	uint8_t		xoa_offline;
466 	uint8_t		xoa_sparse;
467 } xoptattr_t;
468 
469 typedef struct vattr {
470 	uint_t		va_mask;	/* bit-mask of attributes */
471 	u_offset_t	va_size;	/* file size in bytes */
472 } vattr_t;
473 
474 
475 typedef struct xvattr {
476 	vattr_t		xva_vattr;	/* Embedded vattr structure */
477 	uint32_t	xva_magic;	/* Magic Number */
478 	uint32_t	xva_mapsize;	/* Size of attr bitmap (32-bit words) */
479 	uint32_t	*xva_rtnattrmapp;	/* Ptr to xva_rtnattrmap[] */
480 	uint32_t	xva_reqattrmap[XVA_MAPSIZE];	/* Requested attrs */
481 	uint32_t	xva_rtnattrmap[XVA_MAPSIZE];	/* Returned attrs */
482 	xoptattr_t	xva_xoptattrs;	/* Optional attributes */
483 } xvattr_t;
484 
485 typedef struct vsecattr {
486 	uint_t		vsa_mask;	/* See below */
487 	int		vsa_aclcnt;	/* ACL entry count */
488 	void		*vsa_aclentp;	/* pointer to ACL entries */
489 	int		vsa_dfaclcnt;	/* default ACL entry count */
490 	void		*vsa_dfaclentp;	/* pointer to default ACL entries */
491 	size_t		vsa_aclentsz;	/* ACE size in bytes of vsa_aclentp */
492 } vsecattr_t;
493 
494 #define	AT_TYPE		0x00001
495 #define	AT_MODE		0x00002
496 #define	AT_UID		0x00004
497 #define	AT_GID		0x00008
498 #define	AT_FSID		0x00010
499 #define	AT_NODEID	0x00020
500 #define	AT_NLINK	0x00040
501 #define	AT_SIZE		0x00080
502 #define	AT_ATIME	0x00100
503 #define	AT_MTIME	0x00200
504 #define	AT_CTIME	0x00400
505 #define	AT_RDEV		0x00800
506 #define	AT_BLKSIZE	0x01000
507 #define	AT_NBLOCKS	0x02000
508 #define	AT_SEQ		0x08000
509 #define	AT_XVATTR	0x10000
510 
511 #define	CRCREAT		0
512 
513 extern int fop_getattr(vnode_t *vp, vattr_t *vap);
514 
515 #define	VOP_CLOSE(vp, f, c, o, cr, ct)	0
516 #define	VOP_PUTPAGE(vp, of, sz, fl, cr, ct)	0
517 #define	VOP_GETATTR(vp, vap, cr)  fop_getattr((vp), (vap));
518 
519 #define	VOP_FSYNC(vp, f, cr, ct)	fsync((vp)->v_fd)
520 
521 #define	VN_RELE(vp)			vn_close(vp, 0, NULL, NULL)
522 #define	VN_RELE_ASYNC(vp, taskq)	vn_close(vp, 0, NULL, NULL)
523 
524 #define	vn_lock(vp, type)
525 #define	VOP_UNLOCK(vp, type)
526 
527 extern int vn_open(char *path, int x1, int oflags, int mode, vnode_t **vpp,
528     int x2, int x3);
529 extern int vn_openat(char *path, int x1, int oflags, int mode, vnode_t **vpp,
530     int x2, int x3, vnode_t *vp, int fd);
531 extern int vn_rdwr(int uio, vnode_t *vp, void *addr, ssize_t len,
532     offset_t offset, int x1, int x2, rlim64_t x3, void *x4, ssize_t *residp);
533 extern void vn_close(vnode_t *vp, int openflag, cred_t *cr, kthread_t *td);
534 
535 #define	vn_remove(path, x1, x2)		remove(path)
536 #define	vn_rename(from, to, seg)	rename((from), (to))
537 #define	vn_is_readonly(vp)		B_FALSE
538 
539 extern vnode_t *rootdir;
540 
541 #include <sys/file.h>		/* for FREAD, FWRITE, etc */
542 #define	FTRUNC	O_TRUNC
543 
544 /*
545  * Random stuff
546  */
547 #define	ddi_get_lbolt()		(gethrtime() >> 23)
548 #define	ddi_get_lbolt64()	(gethrtime() >> 23)
549 #define	hz	119	/* frequency when using gethrtime() >> 23 for lbolt */
550 
551 extern void delay(clock_t ticks);
552 
553 #define	SEC_TO_TICK(sec)	((sec) * hz)
554 #define	NSEC_TO_TICK(nsec)	((nsec) / (NANOSEC / hz))
555 
556 #define	gethrestime_sec() time(NULL)
557 #define	gethrestime(t) \
558 	do {\
559 		(t)->tv_sec = gethrestime_sec();\
560 		(t)->tv_nsec = 0;\
561 	} while (0);
562 
563 #define	max_ncpus	64
564 #define	boot_ncpus	(sysconf(_SC_NPROCESSORS_ONLN))
565 
566 #define	minclsyspri	60
567 #define	maxclsyspri	99
568 
569 #define	CPU_SEQID	(thr_self() & (max_ncpus - 1))
570 
571 #define	kcred		NULL
572 #define	CRED()		NULL
573 
574 #ifndef ptob
575 #define	ptob(x)		((x) * PAGESIZE)
576 #endif
577 
578 extern uint64_t physmem;
579 
580 extern int highbit64(uint64_t i);
581 extern int random_get_bytes(uint8_t *ptr, size_t len);
582 extern int random_get_pseudo_bytes(uint8_t *ptr, size_t len);
583 
584 extern void kernel_init(int);
585 extern void kernel_fini(void);
586 
587 struct spa;
588 extern void nicenum(uint64_t num, char *buf, size_t);
589 extern void show_pool_stats(struct spa *);
590 extern int set_global_var(char *arg);
591 
592 typedef struct callb_cpr {
593 	kmutex_t	*cc_lockp;
594 } callb_cpr_t;
595 
596 #define	CALLB_CPR_INIT(cp, lockp, func, name)	{		\
597 	(cp)->cc_lockp = lockp;					\
598 }
599 
600 #define	CALLB_CPR_SAFE_BEGIN(cp) {				\
601 	ASSERT(MUTEX_HELD((cp)->cc_lockp));			\
602 }
603 
604 #define	CALLB_CPR_SAFE_END(cp, lockp) {				\
605 	ASSERT(MUTEX_HELD((cp)->cc_lockp));			\
606 }
607 
608 #define	CALLB_CPR_EXIT(cp) {					\
609 	ASSERT(MUTEX_HELD((cp)->cc_lockp));			\
610 	mutex_exit((cp)->cc_lockp);				\
611 }
612 
613 #define	zone_dataset_visible(x, y)	(1)
614 #define	INGLOBALZONE(z)			(1)
615 extern uint32_t zone_get_hostid(void *zonep);
616 
617 extern char *kmem_asprintf(const char *fmt, ...);
618 #define	strfree(str) kmem_free((str), strlen(str) + 1)
619 
620 /*
621  * Hostname information
622  */
623 extern struct utsname utsname;
624 extern char hw_serial[];	/* for userland-emulated hostid access */
625 extern int ddi_strtoul(const char *str, char **nptr, int base,
626     unsigned long *result);
627 
628 extern int ddi_strtoull(const char *str, char **nptr, int base,
629     u_longlong_t *result);
630 
631 /* ZFS Boot Related stuff. */
632 
633 struct _buf {
634 	intptr_t	_fd;
635 };
636 
637 struct bootstat {
638 	uint64_t st_size;
639 };
640 
641 typedef struct ace_object {
642 	uid_t		a_who;
643 	uint32_t	a_access_mask;
644 	uint16_t	a_flags;
645 	uint16_t	a_type;
646 	uint8_t		a_obj_type[16];
647 	uint8_t		a_inherit_obj_type[16];
648 } ace_object_t;
649 
650 
651 #define	ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE	0x05
652 #define	ACE_ACCESS_DENIED_OBJECT_ACE_TYPE	0x06
653 #define	ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE	0x07
654 #define	ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE	0x08
655 
656 extern struct _buf *kobj_open_file(char *name);
657 extern int kobj_read_file(struct _buf *file, char *buf, unsigned size,
658     unsigned off);
659 extern void kobj_close_file(struct _buf *file);
660 extern int kobj_get_filesize(struct _buf *file, uint64_t *size);
661 extern int zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr);
662 extern int zfs_secpolicy_rename_perms(const char *from, const char *to,
663     cred_t *cr);
664 extern int zfs_secpolicy_destroy_perms(const char *name, cred_t *cr);
665 extern zoneid_t getzoneid(void);
666 /* Random compatibility stuff. */
667 #define	pwrite64(d, p, n, o)	pwrite(d, p, n, o)
668 #define	readdir64(d)		readdir(d)
669 #define	SIGPENDING(td)		(0)
670 #define	root_mount_wait()	do { } while (0)
671 #define	root_mounted()		(1)
672 
673 #define	noinline	__attribute__((noinline))
674 #define	likely(x)	__builtin_expect((x), 1)
675 
676 struct file {
677 	void *dummy;
678 };
679 
680 #define	FCREAT	O_CREAT
681 #define	FOFFMAX	0x0
682 
683 /* SID stuff */
684 typedef struct ksiddomain {
685 	uint_t	kd_ref;
686 	uint_t	kd_len;
687 	char	*kd_name;
688 } ksiddomain_t;
689 
690 ksiddomain_t *ksid_lookupdomain(const char *);
691 void ksiddomain_rele(ksiddomain_t *);
692 
693 typedef	uint32_t	idmap_rid_t;
694 
695 #define	DDI_SLEEP	KM_SLEEP
696 #define	ddi_log_sysevent(_a, _b, _c, _d, _e, _f, _g)	(0)
697 
698 #define	SX_SYSINIT(name, lock, desc)
699 
700 #define SYSCTL_HANDLER_ARGS struct sysctl_oid *oidp, void *arg1,	\
701 	intptr_t arg2, struct sysctl_req *req
702 
703 /*
704  * This describes the access space for a sysctl request.  This is needed
705  * so that we can use the interface from the kernel or from user-space.
706  */
707 struct sysctl_req {
708 	struct thread	*td;		/* used for access checking */
709 	int		lock;		/* wiring state */
710 	void		*oldptr;
711 	size_t		oldlen;
712 	size_t		oldidx;
713 	int		(*oldfunc)(struct sysctl_req *, const void *, size_t);
714 	void		*newptr;
715 	size_t		newlen;
716 	size_t		newidx;
717 	int		(*newfunc)(struct sysctl_req *, void *, size_t);
718 	size_t		validlen;
719 	int		flags;
720 };
721 
722 SLIST_HEAD(sysctl_oid_list, sysctl_oid);
723 
724 /*
725  * This describes one "oid" in the MIB tree.  Potentially more nodes can
726  * be hidden behind it, expanded by the handler.
727  */
728 struct sysctl_oid {
729 	struct sysctl_oid_list *oid_parent;
730 	SLIST_ENTRY(sysctl_oid) oid_link;
731 	int		oid_number;
732 	u_int		oid_kind;
733 	void		*oid_arg1;
734 	intptr_t	oid_arg2;
735 	const char	*oid_name;
736 	int 		(*oid_handler)(SYSCTL_HANDLER_ARGS);
737 	const char	*oid_fmt;
738 	int		oid_refcnt;
739 	u_int		oid_running;
740 	const char	*oid_descr;
741 };
742 
743 #define	SYSCTL_DECL(...)
744 #define	SYSCTL_NODE(...)
745 #define	SYSCTL_INT(...)
746 #define	SYSCTL_UINT(...)
747 #define	SYSCTL_ULONG(...)
748 #define	SYSCTL_PROC(...)
749 #define	SYSCTL_QUAD(...)
750 #define	SYSCTL_UQUAD(...)
751 #ifdef TUNABLE_INT
752 #undef TUNABLE_INT
753 #undef TUNABLE_ULONG
754 #undef TUNABLE_QUAD
755 #endif
756 #define	TUNABLE_INT(...)
757 #define	TUNABLE_ULONG(...)
758 #define	TUNABLE_QUAD(...)
759 
760 int sysctl_handle_64(SYSCTL_HANDLER_ARGS);
761 
762 /* Errors */
763 
764 #ifndef	ERESTART
765 #define	ERESTART	(-1)
766 #endif
767 
768 #ifdef illumos
769 /*
770  * Cyclic information
771  */
772 extern kmutex_t cpu_lock;
773 
774 typedef uintptr_t cyclic_id_t;
775 typedef uint16_t cyc_level_t;
776 typedef void (*cyc_func_t)(void *);
777 
778 #define	CY_LOW_LEVEL	0
779 #define	CY_INFINITY	INT64_MAX
780 #define	CYCLIC_NONE	((cyclic_id_t)0)
781 
782 typedef struct cyc_time {
783 	hrtime_t cyt_when;
784 	hrtime_t cyt_interval;
785 } cyc_time_t;
786 
787 typedef struct cyc_handler {
788 	cyc_func_t cyh_func;
789 	void *cyh_arg;
790 	cyc_level_t cyh_level;
791 } cyc_handler_t;
792 
793 extern cyclic_id_t cyclic_add(cyc_handler_t *, cyc_time_t *);
794 extern void cyclic_remove(cyclic_id_t);
795 extern int cyclic_reprogram(cyclic_id_t, hrtime_t);
796 #endif	/* illumos */
797 
798 #ifdef illumos
799 /*
800  * Buf structure
801  */
802 #define	B_BUSY		0x0001
803 #define	B_DONE		0x0002
804 #define	B_ERROR		0x0004
805 #define	B_READ		0x0040	/* read when I/O occurs */
806 #define	B_WRITE		0x0100	/* non-read pseudo-flag */
807 
808 typedef struct buf {
809 	int	b_flags;
810 	size_t b_bcount;
811 	union {
812 		caddr_t b_addr;
813 	} b_un;
814 
815 	lldaddr_t	_b_blkno;
816 #define	b_lblkno	_b_blkno._f
817 	size_t	b_resid;
818 	size_t	b_bufsize;
819 	int	(*b_iodone)(struct buf *);
820 	int	b_error;
821 	void	*b_private;
822 } buf_t;
823 
824 extern void bioinit(buf_t *);
825 extern void biodone(buf_t *);
826 extern void bioerror(buf_t *, int);
827 extern int geterror(buf_t *);
828 #endif
829 
830 #ifdef	__cplusplus
831 }
832 #endif
833 
834 #endif	/* _SYS_ZFS_CONTEXT_H */
835