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