1fa9e4066Sahrens /* 2fa9e4066Sahrens * CDDL HEADER START 3fa9e4066Sahrens * 4fa9e4066Sahrens * The contents of this file are subject to the terms of the 5ea8dc4b6Seschrock * Common Development and Distribution License (the "License"). 6ea8dc4b6Seschrock * You may not use this file except in compliance with the License. 7fa9e4066Sahrens * 8fa9e4066Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9fa9e4066Sahrens * or http://www.opensolaris.org/os/licensing. 10fa9e4066Sahrens * See the License for the specific language governing permissions 11fa9e4066Sahrens * and limitations under the License. 12fa9e4066Sahrens * 13fa9e4066Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14fa9e4066Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15fa9e4066Sahrens * If applicable, add the following below this CDDL HEADER, with the 16fa9e4066Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17fa9e4066Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18fa9e4066Sahrens * 19fa9e4066Sahrens * CDDL HEADER END 20fa9e4066Sahrens */ 21fa9e4066Sahrens /* 22744947dcSTom Erickson * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 235aeb9474SGarrett D'Amore * Copyright 2011 Nexenta Systems, Inc. All rights reserved. 24887cdfc7SMatthew Ahrens * Copyright (c) 2012, 2014 by Delphix. All rights reserved. 254e3c9f44SBill Pijewski * Copyright (c) 2012, Joyent, Inc. All rights reserved. 265aeb9474SGarrett D'Amore */ 27fa9e4066Sahrens 28fa9e4066Sahrens #ifndef _SYS_ZFS_CONTEXT_H 29fa9e4066Sahrens #define _SYS_ZFS_CONTEXT_H 30fa9e4066Sahrens 31fa9e4066Sahrens #ifdef __cplusplus 32fa9e4066Sahrens extern "C" { 33fa9e4066Sahrens #endif 34fa9e4066Sahrens 35fa9e4066Sahrens #define _SYS_MUTEX_H 36fa9e4066Sahrens #define _SYS_RWLOCK_H 37fa9e4066Sahrens #define _SYS_CONDVAR_H 38fa9e4066Sahrens #define _SYS_SYSTM_H 39fa9e4066Sahrens #define _SYS_T_LOCK_H 40fa9e4066Sahrens #define _SYS_VNODE_H 41fa9e4066Sahrens #define _SYS_VFS_H 42fa9e4066Sahrens #define _SYS_SUNDDI_H 43fa9e4066Sahrens #define _SYS_CALLB_H 44fa9e4066Sahrens 45fa9e4066Sahrens #include <stdio.h> 46fa9e4066Sahrens #include <stdlib.h> 47fa9e4066Sahrens #include <stddef.h> 48fa9e4066Sahrens #include <stdarg.h> 49fa9e4066Sahrens #include <fcntl.h> 50fa9e4066Sahrens #include <unistd.h> 51fa9e4066Sahrens #include <errno.h> 52fa9e4066Sahrens #include <string.h> 53fa9e4066Sahrens #include <strings.h> 54fa9e4066Sahrens #include <synch.h> 55fa9e4066Sahrens #include <thread.h> 56fa9e4066Sahrens #include <assert.h> 57fa9e4066Sahrens #include <alloca.h> 58fa9e4066Sahrens #include <umem.h> 59fa9e4066Sahrens #include <limits.h> 60fa9e4066Sahrens #include <atomic.h> 61fa9e4066Sahrens #include <dirent.h> 62fa9e4066Sahrens #include <time.h> 63cd1c8b85SMatthew Ahrens #include <procfs.h> 643b2aab18SMatthew Ahrens #include <pthread.h> 653b2aab18SMatthew Ahrens #include <sys/debug.h> 66573ca77eSGeorge Wilson #include <libsysevent.h> 67fa9e4066Sahrens #include <sys/note.h> 68fa9e4066Sahrens #include <sys/types.h> 69ecd6cf80Smarks #include <sys/cred.h> 70fa9e4066Sahrens #include <sys/sysmacros.h> 71fa9e4066Sahrens #include <sys/bitmap.h> 72fa9e4066Sahrens #include <sys/resource.h> 73fa9e4066Sahrens #include <sys/byteorder.h> 74fa9e4066Sahrens #include <sys/list.h> 75fa9e4066Sahrens #include <sys/uio.h> 76fa9e4066Sahrens #include <sys/zfs_debug.h> 77fa9e4066Sahrens #include <sys/sdt.h> 7844cb6abcSbmc #include <sys/kstat.h> 79de8267e0Stimh #include <sys/u8_textprep.h> 803d7072f8Seschrock #include <sys/sysevent/eventdefs.h> 81573ca77eSGeorge Wilson #include <sys/sysevent/dev.h> 8292241e0bSTom Erickson #include <sys/sunddi.h> 83fb09f5aaSMadhav Suresh #include <sys/debug.h> 84be6fd75aSMatthew Ahrens #include "zfs.h" 85fa9e4066Sahrens 86fa9e4066Sahrens /* 87fa9e4066Sahrens * Debugging 88fa9e4066Sahrens */ 89fa9e4066Sahrens 90fa9e4066Sahrens /* 91fa9e4066Sahrens * Note that we are not using the debugging levels. 92fa9e4066Sahrens */ 93fa9e4066Sahrens 94fa9e4066Sahrens #define CE_CONT 0 /* continuation */ 95fa9e4066Sahrens #define CE_NOTE 1 /* notice */ 96fa9e4066Sahrens #define CE_WARN 2 /* warning */ 97fa9e4066Sahrens #define CE_PANIC 3 /* panic */ 98fa9e4066Sahrens #define CE_IGNORE 4 /* print nothing */ 99fa9e4066Sahrens 100fa9e4066Sahrens /* 101fa9e4066Sahrens * ZFS debugging 102fa9e4066Sahrens */ 103fa9e4066Sahrens 104fa9e4066Sahrens #ifdef ZFS_DEBUG 105fa9e4066Sahrens extern void dprintf_setup(int *argc, char **argv); 106fa9e4066Sahrens #endif /* ZFS_DEBUG */ 107fa9e4066Sahrens 108fa9e4066Sahrens extern void cmn_err(int, const char *, ...); 1090125049cSahrens extern void vcmn_err(int, const char *, __va_list); 110fa9e4066Sahrens extern void panic(const char *, ...); 111fa9e4066Sahrens extern void vpanic(const char *, __va_list); 112fa9e4066Sahrens 1130a4e9518Sgw25295 #define fm_panic panic 1140a4e9518Sgw25295 115feef89cfSVictor Latushkin extern int aok; 116feef89cfSVictor Latushkin 117fa9e4066Sahrens /* 1183d7072f8Seschrock * DTrace SDT probes have different signatures in userland than they do in 1195f9bb2f3SPaul Dagnelie * the kernel. If they're being used in kernel code, re-define them out of 120fa9e4066Sahrens * existence for their counterparts in libzpool. 1215f9bb2f3SPaul Dagnelie * 1225f9bb2f3SPaul Dagnelie * Here's an example of how to use the set-error probes in userland: 1235f9bb2f3SPaul Dagnelie * zfs$target:::set-error /arg0 == EBUSY/ {stack();} 1245f9bb2f3SPaul Dagnelie * 1255f9bb2f3SPaul Dagnelie * Here's an example of how to use DTRACE_PROBE probes in userland: 1265f9bb2f3SPaul Dagnelie * If there is a probe declared as follows: 1275f9bb2f3SPaul Dagnelie * DTRACE_PROBE2(zfs__probe_name, uint64_t, blkid, dnode_t *, dn); 1285f9bb2f3SPaul Dagnelie * Then you can use it as follows: 1295f9bb2f3SPaul Dagnelie * zfs$target:::probe2 /copyinstr(arg0) == "zfs__probe_name"/ 1305f9bb2f3SPaul Dagnelie * {printf("%u %p\n", arg1, arg2);} 131fa9e4066Sahrens */ 132fa9e4066Sahrens 13305715f94SMark Maybee #ifdef DTRACE_PROBE 13405715f94SMark Maybee #undef DTRACE_PROBE 13505715f94SMark Maybee #endif /* DTRACE_PROBE */ 136be6fd75aSMatthew Ahrens #define DTRACE_PROBE(a) \ 137be6fd75aSMatthew Ahrens ZFS_PROBE0(#a) 13805715f94SMark Maybee 139fa9e4066Sahrens #ifdef DTRACE_PROBE1 140fa9e4066Sahrens #undef DTRACE_PROBE1 141fa9e4066Sahrens #endif /* DTRACE_PROBE1 */ 142be6fd75aSMatthew Ahrens #define DTRACE_PROBE1(a, b, c) \ 143be6fd75aSMatthew Ahrens ZFS_PROBE1(#a, (unsigned long)c) 144fa9e4066Sahrens 145fa9e4066Sahrens #ifdef DTRACE_PROBE2 146fa9e4066Sahrens #undef DTRACE_PROBE2 147fa9e4066Sahrens #endif /* DTRACE_PROBE2 */ 148be6fd75aSMatthew Ahrens #define DTRACE_PROBE2(a, b, c, d, e) \ 149be6fd75aSMatthew Ahrens ZFS_PROBE2(#a, (unsigned long)c, (unsigned long)e) 150fa9e4066Sahrens 151c543ec06Sahrens #ifdef DTRACE_PROBE3 152c543ec06Sahrens #undef DTRACE_PROBE3 153c543ec06Sahrens #endif /* DTRACE_PROBE3 */ 154be6fd75aSMatthew Ahrens #define DTRACE_PROBE3(a, b, c, d, e, f, g) \ 155be6fd75aSMatthew Ahrens ZFS_PROBE3(#a, (unsigned long)c, (unsigned long)e, (unsigned long)g) 156c543ec06Sahrens 157faafa6e3Sahrens #ifdef DTRACE_PROBE4 158faafa6e3Sahrens #undef DTRACE_PROBE4 159faafa6e3Sahrens #endif /* DTRACE_PROBE4 */ 160be6fd75aSMatthew Ahrens #define DTRACE_PROBE4(a, b, c, d, e, f, g, h, i) \ 161be6fd75aSMatthew Ahrens ZFS_PROBE4(#a, (unsigned long)c, (unsigned long)e, (unsigned long)g, \ 162be6fd75aSMatthew Ahrens (unsigned long)i) 163be6fd75aSMatthew Ahrens 164be6fd75aSMatthew Ahrens /* 165be6fd75aSMatthew Ahrens * We use the comma operator so that this macro can be used without much 166be6fd75aSMatthew Ahrens * additional code. For example, "return (EINVAL);" becomes 167be6fd75aSMatthew Ahrens * "return (SET_ERROR(EINVAL));". Note that the argument will be evaluated 168be6fd75aSMatthew Ahrens * twice, so it should not have side effects (e.g. something like: 169be6fd75aSMatthew Ahrens * "return (SET_ERROR(log_error(EINVAL, info)));" would log the error twice). 170be6fd75aSMatthew Ahrens */ 171be6fd75aSMatthew Ahrens #define SET_ERROR(err) (ZFS_SET_ERROR(err), err) 172faafa6e3Sahrens 173fa9e4066Sahrens /* 174fa9e4066Sahrens * Threads 175fa9e4066Sahrens */ 176fa9e4066Sahrens #define curthread ((void *)(uintptr_t)thr_self()) 177fa9e4066Sahrens 17869962b56SMatthew Ahrens #define kpreempt(x) yield() 17969962b56SMatthew Ahrens 180fa9e4066Sahrens typedef struct kthread kthread_t; 181*d5de1674SSaso Kiselkov typedef uint64_t kt_did_t; 182fa9e4066Sahrens 183fa9e4066Sahrens #define thread_create(stk, stksize, func, arg, len, pp, state, pri) \ 184fa9e4066Sahrens zk_thread_create(func, arg) 1855ad82045Snd150628 #define thread_exit() thr_exit(NULL) 18635a5a358SJonathan Adams #define thread_join(t) panic("libzpool cannot join threads") 18735a5a358SJonathan Adams 18835a5a358SJonathan Adams #define newproc(f, a, cid, pri, ctp, pid) (ENOSYS) 18935a5a358SJonathan Adams 19035a5a358SJonathan Adams /* in libzpool, p0 exists only to have its address taken */ 19135a5a358SJonathan Adams struct proc { 19235a5a358SJonathan Adams uintptr_t this_is_never_used_dont_dereference_it; 19335a5a358SJonathan Adams }; 19435a5a358SJonathan Adams 19535a5a358SJonathan Adams extern struct proc p0; 1964e3c9f44SBill Pijewski #define curproc (&p0) 19735a5a358SJonathan Adams 19835a5a358SJonathan Adams #define PS_NONE -1 199fa9e4066Sahrens 200fa9e4066Sahrens extern kthread_t *zk_thread_create(void (*func)(), void *arg); 201fa9e4066Sahrens 202fa9e4066Sahrens #define issig(why) (FALSE) 203fa9e4066Sahrens #define ISSIG(thr, why) (FALSE) 204fa9e4066Sahrens 205fa9e4066Sahrens /* 206fa9e4066Sahrens * Mutexes 207fa9e4066Sahrens */ 208fa9e4066Sahrens typedef struct kmutex { 209fa9e4066Sahrens void *m_owner; 210c25056deSgw25295 boolean_t initialized; 211fa9e4066Sahrens mutex_t m_lock; 212fa9e4066Sahrens } kmutex_t; 213fa9e4066Sahrens 214fa9e4066Sahrens #define MUTEX_DEFAULT USYNC_THREAD 215fa9e4066Sahrens #undef MUTEX_HELD 216744947dcSTom Erickson #undef MUTEX_NOT_HELD 217fa9e4066Sahrens #define MUTEX_HELD(m) _mutex_held(&(m)->m_lock) 218744947dcSTom Erickson #define MUTEX_NOT_HELD(m) (!MUTEX_HELD(m)) 219fa9e4066Sahrens 220fa9e4066Sahrens /* 221fa9e4066Sahrens * Argh -- we have to get cheesy here because the kernel and userland 222fa9e4066Sahrens * have different signatures for the same routine. 223fa9e4066Sahrens */ 224fa9e4066Sahrens extern int _mutex_init(mutex_t *mp, int type, void *arg); 225fa9e4066Sahrens extern int _mutex_destroy(mutex_t *mp); 226fa9e4066Sahrens 227fa9e4066Sahrens #define mutex_init(mp, b, c, d) zmutex_init((kmutex_t *)(mp)) 228fa9e4066Sahrens #define mutex_destroy(mp) zmutex_destroy((kmutex_t *)(mp)) 229fa9e4066Sahrens 230fa9e4066Sahrens extern void zmutex_init(kmutex_t *mp); 231fa9e4066Sahrens extern void zmutex_destroy(kmutex_t *mp); 232fa9e4066Sahrens extern void mutex_enter(kmutex_t *mp); 233fa9e4066Sahrens extern void mutex_exit(kmutex_t *mp); 234fa9e4066Sahrens extern int mutex_tryenter(kmutex_t *mp); 235fa9e4066Sahrens extern void *mutex_owner(kmutex_t *mp); 236fa9e4066Sahrens 237fa9e4066Sahrens /* 238fa9e4066Sahrens * RW locks 239fa9e4066Sahrens */ 240fa9e4066Sahrens typedef struct krwlock { 241fa9e4066Sahrens void *rw_owner; 242c25056deSgw25295 boolean_t initialized; 243fa9e4066Sahrens rwlock_t rw_lock; 244fa9e4066Sahrens } krwlock_t; 245fa9e4066Sahrens 246fa9e4066Sahrens typedef int krw_t; 247fa9e4066Sahrens 248fa9e4066Sahrens #define RW_READER 0 249fa9e4066Sahrens #define RW_WRITER 1 250fa9e4066Sahrens #define RW_DEFAULT USYNC_THREAD 251fa9e4066Sahrens 252fa9e4066Sahrens #undef RW_READ_HELD 253fa9e4066Sahrens #define RW_READ_HELD(x) _rw_read_held(&(x)->rw_lock) 254fa9e4066Sahrens 255fa9e4066Sahrens #undef RW_WRITE_HELD 256fa9e4066Sahrens #define RW_WRITE_HELD(x) _rw_write_held(&(x)->rw_lock) 257fa9e4066Sahrens 2583b2aab18SMatthew Ahrens #undef RW_LOCK_HELD 2593b2aab18SMatthew Ahrens #define RW_LOCK_HELD(x) (RW_READ_HELD(x) || RW_WRITE_HELD(x)) 2603b2aab18SMatthew Ahrens 261fa9e4066Sahrens extern void rw_init(krwlock_t *rwlp, char *name, int type, void *arg); 262fa9e4066Sahrens extern void rw_destroy(krwlock_t *rwlp); 263fa9e4066Sahrens extern void rw_enter(krwlock_t *rwlp, krw_t rw); 264fa9e4066Sahrens extern int rw_tryenter(krwlock_t *rwlp, krw_t rw); 265fa9e4066Sahrens extern int rw_tryupgrade(krwlock_t *rwlp); 266fa9e4066Sahrens extern void rw_exit(krwlock_t *rwlp); 267fa9e4066Sahrens #define rw_downgrade(rwlp) do { } while (0) 268fa9e4066Sahrens 269ecd6cf80Smarks extern uid_t crgetuid(cred_t *cr); 2704445fffbSMatthew Ahrens extern uid_t crgetruid(cred_t *cr); 271ecd6cf80Smarks extern gid_t crgetgid(cred_t *cr); 272ecd6cf80Smarks extern int crgetngroups(cred_t *cr); 273ecd6cf80Smarks extern gid_t *crgetgroups(cred_t *cr); 274ecd6cf80Smarks 275fa9e4066Sahrens /* 276fa9e4066Sahrens * Condition variables 277fa9e4066Sahrens */ 278fa9e4066Sahrens typedef cond_t kcondvar_t; 279fa9e4066Sahrens 280fa9e4066Sahrens #define CV_DEFAULT USYNC_THREAD 281fa9e4066Sahrens 282fa9e4066Sahrens extern void cv_init(kcondvar_t *cv, char *name, int type, void *arg); 283fa9e4066Sahrens extern void cv_destroy(kcondvar_t *cv); 284fa9e4066Sahrens extern void cv_wait(kcondvar_t *cv, kmutex_t *mp); 285fa9e4066Sahrens extern clock_t cv_timedwait(kcondvar_t *cv, kmutex_t *mp, clock_t abstime); 2860689f76cSAdam Leventhal extern clock_t cv_timedwait_hires(kcondvar_t *cvp, kmutex_t *mp, hrtime_t tim, 2870689f76cSAdam Leventhal hrtime_t res, int flag); 288fa9e4066Sahrens extern void cv_signal(kcondvar_t *cv); 289fa9e4066Sahrens extern void cv_broadcast(kcondvar_t *cv); 290fa9e4066Sahrens 291fa9e4066Sahrens /* 2923b2aab18SMatthew Ahrens * Thread-specific data 2933b2aab18SMatthew Ahrens */ 2943b2aab18SMatthew Ahrens #define tsd_get(k) pthread_getspecific(k) 2953b2aab18SMatthew Ahrens #define tsd_set(k, v) pthread_setspecific(k, v) 2963b2aab18SMatthew Ahrens #define tsd_create(kp, d) pthread_key_create(kp, d) 2973b2aab18SMatthew Ahrens #define tsd_destroy(kp) /* nothing */ 2983b2aab18SMatthew Ahrens 2993b2aab18SMatthew Ahrens /* 30044cb6abcSbmc * kstat creation, installation and deletion 30144cb6abcSbmc */ 302c3a66015SMatthew Ahrens extern kstat_t *kstat_create(const char *, int, 303c3a66015SMatthew Ahrens const char *, const char *, uchar_t, ulong_t, uchar_t); 30444cb6abcSbmc extern void kstat_install(kstat_t *); 30544cb6abcSbmc extern void kstat_delete(kstat_t *); 306c3a66015SMatthew Ahrens extern void kstat_waitq_enter(kstat_io_t *); 307c3a66015SMatthew Ahrens extern void kstat_waitq_exit(kstat_io_t *); 308c3a66015SMatthew Ahrens extern void kstat_runq_enter(kstat_io_t *); 309c3a66015SMatthew Ahrens extern void kstat_runq_exit(kstat_io_t *); 310c3a66015SMatthew Ahrens extern void kstat_waitq_to_runq(kstat_io_t *); 311c3a66015SMatthew Ahrens extern void kstat_runq_back_to_waitq(kstat_io_t *); 31244cb6abcSbmc 31344cb6abcSbmc /* 314fa9e4066Sahrens * Kernel memory 315fa9e4066Sahrens */ 316fa9e4066Sahrens #define KM_SLEEP UMEM_NOFAIL 3171ab7f2deSmaybee #define KM_PUSHPAGE KM_SLEEP 318fa9e4066Sahrens #define KM_NOSLEEP UMEM_DEFAULT 319a0965f35Sbonwick #define KMC_NODEBUG UMC_NODEBUG 320e291592aSJonathan Adams #define KMC_NOTOUCH 0 /* not needed for userland caches */ 321fa9e4066Sahrens #define kmem_alloc(_s, _f) umem_alloc(_s, _f) 322fa9e4066Sahrens #define kmem_zalloc(_s, _f) umem_zalloc(_s, _f) 323fa9e4066Sahrens #define kmem_free(_b, _s) umem_free(_b, _s) 324fa9e4066Sahrens #define kmem_cache_create(_a, _b, _c, _d, _e, _f, _g, _h, _i) \ 325fa9e4066Sahrens umem_cache_create(_a, _b, _c, _d, _e, _f, _g, _h, _i) 326fa9e4066Sahrens #define kmem_cache_destroy(_c) umem_cache_destroy(_c) 327fa9e4066Sahrens #define kmem_cache_alloc(_c, _f) umem_cache_alloc(_c, _f) 328fa9e4066Sahrens #define kmem_cache_free(_c, _b) umem_cache_free(_c, _b) 329fa9e4066Sahrens #define kmem_debugging() 0 330744947dcSTom Erickson #define kmem_cache_reap_now(_c) /* nothing */ 331744947dcSTom Erickson #define kmem_cache_set_move(_c, _cb) /* nothing */ 33294dd93aeSGeorge Wilson #define vmem_qcache_reap(_v) /* nothing */ 333744947dcSTom Erickson #define POINTER_INVALIDATE(_pp) /* nothing */ 334744947dcSTom Erickson #define POINTER_IS_VALID(_p) 0 335fa9e4066Sahrens 33694dd93aeSGeorge Wilson extern vmem_t *zio_arena; 33794dd93aeSGeorge Wilson 338fa9e4066Sahrens typedef umem_cache_t kmem_cache_t; 339fa9e4066Sahrens 340744947dcSTom Erickson typedef enum kmem_cbrc { 341744947dcSTom Erickson KMEM_CBRC_YES, 342744947dcSTom Erickson KMEM_CBRC_NO, 343744947dcSTom Erickson KMEM_CBRC_LATER, 344744947dcSTom Erickson KMEM_CBRC_DONT_NEED, 345744947dcSTom Erickson KMEM_CBRC_DONT_KNOW 346744947dcSTom Erickson } kmem_cbrc_t; 347744947dcSTom Erickson 348fa9e4066Sahrens /* 349fa9e4066Sahrens * Task queues 350fa9e4066Sahrens */ 351fa9e4066Sahrens typedef struct taskq taskq_t; 352fa9e4066Sahrens typedef uintptr_t taskqid_t; 353fa9e4066Sahrens typedef void (task_func_t)(void *); 354fa9e4066Sahrens 3555aeb9474SGarrett D'Amore typedef struct taskq_ent { 3565aeb9474SGarrett D'Amore struct taskq_ent *tqent_next; 3575aeb9474SGarrett D'Amore struct taskq_ent *tqent_prev; 3585aeb9474SGarrett D'Amore task_func_t *tqent_func; 3595aeb9474SGarrett D'Amore void *tqent_arg; 3605aeb9474SGarrett D'Amore uintptr_t tqent_flags; 3615aeb9474SGarrett D'Amore } taskq_ent_t; 3625aeb9474SGarrett D'Amore 3635aeb9474SGarrett D'Amore #define TQENT_FLAG_PREALLOC 0x1 /* taskq_dispatch_ent used */ 3645aeb9474SGarrett D'Amore 365fa9e4066Sahrens #define TASKQ_PREPOPULATE 0x0001 366fa9e4066Sahrens #define TASKQ_CPR_SAFE 0x0002 /* Use CPR safe protocol */ 367fa9e4066Sahrens #define TASKQ_DYNAMIC 0x0004 /* Use dynamic thread scheduling */ 36835a5a358SJonathan Adams #define TASKQ_THREADS_CPU_PCT 0x0008 /* Scale # threads by # cpus */ 36935a5a358SJonathan Adams #define TASKQ_DC_BATCH 0x0010 /* Mark threads as batch */ 370fa9e4066Sahrens 371fa9e4066Sahrens #define TQ_SLEEP KM_SLEEP /* Can block for memory */ 372fa9e4066Sahrens #define TQ_NOSLEEP KM_NOSLEEP /* cannot block for memory; may fail */ 373fa9e4066Sahrens #define TQ_NOQUEUE 0x02 /* Do not enqueue if can't dispatch */ 37435a5a358SJonathan Adams #define TQ_FRONT 0x08 /* Queue in front */ 375fa9e4066Sahrens 3765aeb9474SGarrett D'Amore 37788b7b0f2SMatthew Ahrens extern taskq_t *system_taskq; 37888b7b0f2SMatthew Ahrens 379fa9e4066Sahrens extern taskq_t *taskq_create(const char *, int, pri_t, int, int, uint_t); 38035a5a358SJonathan Adams #define taskq_create_proc(a, b, c, d, e, p, f) \ 38135a5a358SJonathan Adams (taskq_create(a, b, c, d, e, f)) 38235a5a358SJonathan Adams #define taskq_create_sysdc(a, b, d, e, p, dc, f) \ 38335a5a358SJonathan Adams (taskq_create(a, b, maxclsyspri, d, e, f)) 384fa9e4066Sahrens extern taskqid_t taskq_dispatch(taskq_t *, task_func_t, void *, uint_t); 3855aeb9474SGarrett D'Amore extern void taskq_dispatch_ent(taskq_t *, task_func_t, void *, uint_t, 3865aeb9474SGarrett D'Amore taskq_ent_t *); 387fa9e4066Sahrens extern void taskq_destroy(taskq_t *); 388fa9e4066Sahrens extern void taskq_wait(taskq_t *); 389fa9e4066Sahrens extern int taskq_member(taskq_t *, void *); 39088b7b0f2SMatthew Ahrens extern void system_taskq_init(void); 391d20e665cSRicardo M. Correia extern void system_taskq_fini(void); 392fa9e4066Sahrens 393da6c28aaSamw #define XVA_MAPSIZE 3 394da6c28aaSamw #define XVA_MAGIC 0x78766174 395da6c28aaSamw 396fa9e4066Sahrens /* 397fa9e4066Sahrens * vnodes 398fa9e4066Sahrens */ 399fa9e4066Sahrens typedef struct vnode { 400fa9e4066Sahrens uint64_t v_size; 401fa9e4066Sahrens int v_fd; 402fa9e4066Sahrens char *v_path; 403dffb1e38SMatthew Ahrens int v_dump_fd; 404fa9e4066Sahrens } vnode_t; 405fa9e4066Sahrens 406dffb1e38SMatthew Ahrens extern char *vn_dumpdir; 4077a286c47SDai Ngo #define AV_SCANSTAMP_SZ 32 /* length of anti-virus scanstamp */ 408da6c28aaSamw 409da6c28aaSamw typedef struct xoptattr { 410da6c28aaSamw timestruc_t xoa_createtime; /* Create time of file */ 411da6c28aaSamw uint8_t xoa_archive; 412da6c28aaSamw uint8_t xoa_system; 413da6c28aaSamw uint8_t xoa_readonly; 414da6c28aaSamw uint8_t xoa_hidden; 415da6c28aaSamw uint8_t xoa_nounlink; 416da6c28aaSamw uint8_t xoa_immutable; 417da6c28aaSamw uint8_t xoa_appendonly; 418da6c28aaSamw uint8_t xoa_nodump; 419da6c28aaSamw uint8_t xoa_settable; 420da6c28aaSamw uint8_t xoa_opaque; 421da6c28aaSamw uint8_t xoa_av_quarantined; 422da6c28aaSamw uint8_t xoa_av_modified; 4237a286c47SDai Ngo uint8_t xoa_av_scanstamp[AV_SCANSTAMP_SZ]; 4247a286c47SDai Ngo uint8_t xoa_reparse; 425fd9ee8b5Sjoyce mcintosh uint8_t xoa_offline; 426fd9ee8b5Sjoyce mcintosh uint8_t xoa_sparse; 427da6c28aaSamw } xoptattr_t; 428da6c28aaSamw 429fa9e4066Sahrens typedef struct vattr { 430fa9e4066Sahrens uint_t va_mask; /* bit-mask of attributes */ 431fa9e4066Sahrens u_offset_t va_size; /* file size in bytes */ 432fa9e4066Sahrens } vattr_t; 433fa9e4066Sahrens 434da6c28aaSamw 435da6c28aaSamw typedef struct xvattr { 436da6c28aaSamw vattr_t xva_vattr; /* Embedded vattr structure */ 437da6c28aaSamw uint32_t xva_magic; /* Magic Number */ 438da6c28aaSamw uint32_t xva_mapsize; /* Size of attr bitmap (32-bit words) */ 439da6c28aaSamw uint32_t *xva_rtnattrmapp; /* Ptr to xva_rtnattrmap[] */ 440da6c28aaSamw uint32_t xva_reqattrmap[XVA_MAPSIZE]; /* Requested attrs */ 441da6c28aaSamw uint32_t xva_rtnattrmap[XVA_MAPSIZE]; /* Returned attrs */ 442da6c28aaSamw xoptattr_t xva_xoptattrs; /* Optional attributes */ 443da6c28aaSamw } xvattr_t; 444da6c28aaSamw 445da6c28aaSamw typedef struct vsecattr { 446da6c28aaSamw uint_t vsa_mask; /* See below */ 447da6c28aaSamw int vsa_aclcnt; /* ACL entry count */ 448da6c28aaSamw void *vsa_aclentp; /* pointer to ACL entries */ 449da6c28aaSamw int vsa_dfaclcnt; /* default ACL entry count */ 450da6c28aaSamw void *vsa_dfaclentp; /* pointer to default ACL entries */ 451da6c28aaSamw size_t vsa_aclentsz; /* ACE size in bytes of vsa_aclentp */ 452da6c28aaSamw } vsecattr_t; 453da6c28aaSamw 454da6c28aaSamw #define AT_TYPE 0x00001 455da6c28aaSamw #define AT_MODE 0x00002 456da6c28aaSamw #define AT_UID 0x00004 457da6c28aaSamw #define AT_GID 0x00008 458da6c28aaSamw #define AT_FSID 0x00010 459da6c28aaSamw #define AT_NODEID 0x00020 460da6c28aaSamw #define AT_NLINK 0x00040 461da6c28aaSamw #define AT_SIZE 0x00080 462da6c28aaSamw #define AT_ATIME 0x00100 463da6c28aaSamw #define AT_MTIME 0x00200 464da6c28aaSamw #define AT_CTIME 0x00400 465da6c28aaSamw #define AT_RDEV 0x00800 466da6c28aaSamw #define AT_BLKSIZE 0x01000 467da6c28aaSamw #define AT_NBLOCKS 0x02000 468da6c28aaSamw #define AT_SEQ 0x08000 469da6c28aaSamw #define AT_XVATTR 0x10000 470fa9e4066Sahrens 471fa9e4066Sahrens #define CRCREAT 0 472fa9e4066Sahrens 473095bcd66SGeorge Wilson extern int fop_getattr(vnode_t *vp, vattr_t *vap); 474095bcd66SGeorge Wilson 475da6c28aaSamw #define VOP_CLOSE(vp, f, c, o, cr, ct) 0 476da6c28aaSamw #define VOP_PUTPAGE(vp, of, sz, fl, cr, ct) 0 477095bcd66SGeorge Wilson #define VOP_GETATTR(vp, vap, fl, cr, ct) fop_getattr((vp), (vap)); 478fa9e4066Sahrens 479da6c28aaSamw #define VOP_FSYNC(vp, f, cr, ct) fsync((vp)->v_fd) 480fa9e4066Sahrens 481fa9e4066Sahrens #define VN_RELE(vp) vn_close(vp) 482fa9e4066Sahrens 483fa9e4066Sahrens extern int vn_open(char *path, int x1, int oflags, int mode, vnode_t **vpp, 484fa9e4066Sahrens int x2, int x3); 485fa9e4066Sahrens extern int vn_openat(char *path, int x1, int oflags, int mode, vnode_t **vpp, 486da6c28aaSamw int x2, int x3, vnode_t *vp, int fd); 487fa9e4066Sahrens extern int vn_rdwr(int uio, vnode_t *vp, void *addr, ssize_t len, 488fa9e4066Sahrens offset_t offset, int x1, int x2, rlim64_t x3, void *x4, ssize_t *residp); 489fa9e4066Sahrens extern void vn_close(vnode_t *vp); 490fa9e4066Sahrens 491fa9e4066Sahrens #define vn_remove(path, x1, x2) remove(path) 492fa9e4066Sahrens #define vn_rename(from, to, seg) rename((from), (to)) 4930373e76bSbonwick #define vn_is_readonly(vp) B_FALSE 494fa9e4066Sahrens 495fa9e4066Sahrens extern vnode_t *rootdir; 496fa9e4066Sahrens 497fa9e4066Sahrens #include <sys/file.h> /* for FREAD, FWRITE, etc */ 498fa9e4066Sahrens 499fa9e4066Sahrens /* 500fa9e4066Sahrens * Random stuff 501fa9e4066Sahrens */ 502d3d50737SRafael Vanoni #define ddi_get_lbolt() (gethrtime() >> 23) 503d3d50737SRafael Vanoni #define ddi_get_lbolt64() (gethrtime() >> 23) 504fa9e4066Sahrens #define hz 119 /* frequency when using gethrtime() >> 23 for lbolt */ 505fa9e4066Sahrens 506fa9e4066Sahrens extern void delay(clock_t ticks); 507fa9e4066Sahrens 508283b8460SGeorge.Wilson #define SEC_TO_TICK(sec) ((sec) * hz) 509283b8460SGeorge.Wilson #define NSEC_TO_TICK(usec) ((usec) / (NANOSEC / hz)) 510283b8460SGeorge.Wilson 511fa9e4066Sahrens #define gethrestime_sec() time(NULL) 51271eb0538SChris Kirby #define gethrestime(t) \ 51371eb0538SChris Kirby do {\ 51471eb0538SChris Kirby (t)->tv_sec = gethrestime_sec();\ 51571eb0538SChris Kirby (t)->tv_nsec = 0;\ 51671eb0538SChris Kirby } while (0); 517fa9e4066Sahrens 518fa9e4066Sahrens #define max_ncpus 64 519b959b0b3SPrakash Surya #define boot_ncpus (sysconf(_SC_NPROCESSORS_ONLN)) 520fa9e4066Sahrens 521fa9e4066Sahrens #define minclsyspri 60 522fa9e4066Sahrens #define maxclsyspri 99 523fa9e4066Sahrens 524fa9e4066Sahrens #define CPU_SEQID (thr_self() & (max_ncpus - 1)) 525fa9e4066Sahrens 526fa9e4066Sahrens #define kcred NULL 527fa9e4066Sahrens #define CRED() NULL 528fa9e4066Sahrens 52905715f94SMark Maybee #define ptob(x) ((x) * PAGESIZE) 53005715f94SMark Maybee 531fa9e4066Sahrens extern uint64_t physmem; 532fa9e4066Sahrens 533887cdfc7SMatthew Ahrens extern int highbit64(uint64_t i); 534fa9e4066Sahrens extern int random_get_bytes(uint8_t *ptr, size_t len); 535fa9e4066Sahrens extern int random_get_pseudo_bytes(uint8_t *ptr, size_t len); 536fa9e4066Sahrens 537fa9e4066Sahrens extern void kernel_init(int); 538fa9e4066Sahrens extern void kernel_fini(void); 539fa9e4066Sahrens 540fa9e4066Sahrens struct spa; 541fa9e4066Sahrens extern void nicenum(uint64_t num, char *buf); 542fa9e4066Sahrens extern void show_pool_stats(struct spa *); 543fa9e4066Sahrens 544fa9e4066Sahrens typedef struct callb_cpr { 545fa9e4066Sahrens kmutex_t *cc_lockp; 546fa9e4066Sahrens } callb_cpr_t; 547fa9e4066Sahrens 548fa9e4066Sahrens #define CALLB_CPR_INIT(cp, lockp, func, name) { \ 549fa9e4066Sahrens (cp)->cc_lockp = lockp; \ 550fa9e4066Sahrens } 551fa9e4066Sahrens 552fa9e4066Sahrens #define CALLB_CPR_SAFE_BEGIN(cp) { \ 553fa9e4066Sahrens ASSERT(MUTEX_HELD((cp)->cc_lockp)); \ 554fa9e4066Sahrens } 555fa9e4066Sahrens 556fa9e4066Sahrens #define CALLB_CPR_SAFE_END(cp, lockp) { \ 557fa9e4066Sahrens ASSERT(MUTEX_HELD((cp)->cc_lockp)); \ 558fa9e4066Sahrens } 559fa9e4066Sahrens 560fa9e4066Sahrens #define CALLB_CPR_EXIT(cp) { \ 561fa9e4066Sahrens ASSERT(MUTEX_HELD((cp)->cc_lockp)); \ 562fa9e4066Sahrens mutex_exit((cp)->cc_lockp); \ 563fa9e4066Sahrens } 564fa9e4066Sahrens 565fa9e4066Sahrens #define zone_dataset_visible(x, y) (1) 566fa9e4066Sahrens #define INGLOBALZONE(z) (1) 567fa9e4066Sahrens 568ae46e4c7SMatthew Ahrens extern char *kmem_asprintf(const char *fmt, ...); 569ae46e4c7SMatthew Ahrens #define strfree(str) kmem_free((str), strlen(str) + 1) 570ae46e4c7SMatthew Ahrens 57195173954Sek110237 /* 57295173954Sek110237 * Hostname information 57395173954Sek110237 */ 5745679c89fSjv227347 extern char hw_serial[]; /* for userland-emulated hostid access */ 57595173954Sek110237 extern int ddi_strtoul(const char *str, char **nptr, int base, 57695173954Sek110237 unsigned long *result); 57795173954Sek110237 5780a586ceaSMark Shellenbaum extern int ddi_strtoull(const char *str, char **nptr, int base, 5790a586ceaSMark Shellenbaum u_longlong_t *result); 5800a586ceaSMark Shellenbaum 581ea8dc4b6Seschrock /* ZFS Boot Related stuff. */ 582ea8dc4b6Seschrock 583ea8dc4b6Seschrock struct _buf { 584ea8dc4b6Seschrock intptr_t _fd; 585ea8dc4b6Seschrock }; 586ea8dc4b6Seschrock 587ea8dc4b6Seschrock struct bootstat { 588ea8dc4b6Seschrock uint64_t st_size; 589ea8dc4b6Seschrock }; 590ea8dc4b6Seschrock 591da6c28aaSamw typedef struct ace_object { 592da6c28aaSamw uid_t a_who; 593da6c28aaSamw uint32_t a_access_mask; 594da6c28aaSamw uint16_t a_flags; 595da6c28aaSamw uint16_t a_type; 596da6c28aaSamw uint8_t a_obj_type[16]; 597da6c28aaSamw uint8_t a_inherit_obj_type[16]; 598da6c28aaSamw } ace_object_t; 599da6c28aaSamw 600da6c28aaSamw 601da6c28aaSamw #define ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE 0x05 602da6c28aaSamw #define ACE_ACCESS_DENIED_OBJECT_ACE_TYPE 0x06 603da6c28aaSamw #define ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE 0x07 604da6c28aaSamw #define ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE 0x08 605da6c28aaSamw 606ea8dc4b6Seschrock extern struct _buf *kobj_open_file(char *name); 607ea8dc4b6Seschrock extern int kobj_read_file(struct _buf *file, char *buf, unsigned size, 608ea8dc4b6Seschrock unsigned off); 609ea8dc4b6Seschrock extern void kobj_close_file(struct _buf *file); 610b1b8ab34Slling extern int kobj_get_filesize(struct _buf *file, uint64_t *size); 611ecd6cf80Smarks extern int zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr); 612ecd6cf80Smarks extern int zfs_secpolicy_rename_perms(const char *from, const char *to, 613ecd6cf80Smarks cred_t *cr); 614ecd6cf80Smarks extern int zfs_secpolicy_destroy_perms(const char *name, cred_t *cr); 615ecd6cf80Smarks extern zoneid_t getzoneid(void); 616ea8dc4b6Seschrock 617e0d35c44Smarks /* SID stuff */ 618e0d35c44Smarks typedef struct ksiddomain { 619e0d35c44Smarks uint_t kd_ref; 620e0d35c44Smarks uint_t kd_len; 621e0d35c44Smarks char *kd_name; 622e0d35c44Smarks } ksiddomain_t; 623e0d35c44Smarks 624e0d35c44Smarks ksiddomain_t *ksid_lookupdomain(const char *); 625e0d35c44Smarks void ksiddomain_rele(ksiddomain_t *); 626e0d35c44Smarks 627573ca77eSGeorge Wilson #define DDI_SLEEP KM_SLEEP 628573ca77eSGeorge Wilson #define ddi_log_sysevent(_a, _b, _c, _d, _e, _f, _g) \ 629573ca77eSGeorge Wilson sysevent_post_event(_c, _d, _b, "libzpool", _e, _f) 630573ca77eSGeorge Wilson 631283b8460SGeorge.Wilson /* 632283b8460SGeorge.Wilson * Cyclic information 633283b8460SGeorge.Wilson */ 634283b8460SGeorge.Wilson extern kmutex_t cpu_lock; 635283b8460SGeorge.Wilson 636283b8460SGeorge.Wilson typedef uintptr_t cyclic_id_t; 637283b8460SGeorge.Wilson typedef uint16_t cyc_level_t; 638283b8460SGeorge.Wilson typedef void (*cyc_func_t)(void *); 639283b8460SGeorge.Wilson 640283b8460SGeorge.Wilson #define CY_LOW_LEVEL 0 641283b8460SGeorge.Wilson #define CY_INFINITY INT64_MAX 642283b8460SGeorge.Wilson #define CYCLIC_NONE ((cyclic_id_t)0) 643283b8460SGeorge.Wilson 644283b8460SGeorge.Wilson typedef struct cyc_time { 645283b8460SGeorge.Wilson hrtime_t cyt_when; 646283b8460SGeorge.Wilson hrtime_t cyt_interval; 647283b8460SGeorge.Wilson } cyc_time_t; 648283b8460SGeorge.Wilson 649283b8460SGeorge.Wilson typedef struct cyc_handler { 650283b8460SGeorge.Wilson cyc_func_t cyh_func; 651283b8460SGeorge.Wilson void *cyh_arg; 652283b8460SGeorge.Wilson cyc_level_t cyh_level; 653283b8460SGeorge.Wilson } cyc_handler_t; 654283b8460SGeorge.Wilson 655283b8460SGeorge.Wilson extern cyclic_id_t cyclic_add(cyc_handler_t *, cyc_time_t *); 656283b8460SGeorge.Wilson extern void cyclic_remove(cyclic_id_t); 657283b8460SGeorge.Wilson extern int cyclic_reprogram(cyclic_id_t, hrtime_t); 658283b8460SGeorge.Wilson 65931d7e8faSGeorge Wilson /* 66031d7e8faSGeorge Wilson * Buf structure 66131d7e8faSGeorge Wilson */ 66231d7e8faSGeorge Wilson #define B_BUSY 0x0001 66331d7e8faSGeorge Wilson #define B_DONE 0x0002 66431d7e8faSGeorge Wilson #define B_ERROR 0x0004 66531d7e8faSGeorge Wilson #define B_READ 0x0040 /* read when I/O occurs */ 66631d7e8faSGeorge Wilson #define B_WRITE 0x0100 /* non-read pseudo-flag */ 66731d7e8faSGeorge Wilson 66831d7e8faSGeorge Wilson typedef struct buf { 66931d7e8faSGeorge Wilson int b_flags; 67031d7e8faSGeorge Wilson size_t b_bcount; 67131d7e8faSGeorge Wilson union { 67231d7e8faSGeorge Wilson caddr_t b_addr; 67331d7e8faSGeorge Wilson } b_un; 67431d7e8faSGeorge Wilson 67531d7e8faSGeorge Wilson lldaddr_t _b_blkno; 67631d7e8faSGeorge Wilson #define b_lblkno _b_blkno._f 67731d7e8faSGeorge Wilson size_t b_resid; 67831d7e8faSGeorge Wilson size_t b_bufsize; 67931d7e8faSGeorge Wilson int (*b_iodone)(struct buf *); 68031d7e8faSGeorge Wilson int b_error; 68131d7e8faSGeorge Wilson void *b_private; 68231d7e8faSGeorge Wilson } buf_t; 68331d7e8faSGeorge Wilson 68431d7e8faSGeorge Wilson extern void bioinit(buf_t *); 68531d7e8faSGeorge Wilson extern void biodone(buf_t *); 68631d7e8faSGeorge Wilson extern void bioerror(buf_t *, int); 68731d7e8faSGeorge Wilson extern int geterror(buf_t *); 68831d7e8faSGeorge Wilson 689f0547164Slling #ifdef __cplusplus 690f0547164Slling } 691f0547164Slling #endif 692f0547164Slling 693fa9e4066Sahrens #endif /* _SYS_ZFS_CONTEXT_H */ 694