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