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 2006 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #ifndef _SYS_ZFS_CONTEXT_H 27 #define _SYS_ZFS_CONTEXT_H 28 29 #pragma ident "%Z%%M% %I% %E% SMI" 30 31 #ifdef __cplusplus 32 extern "C" { 33 #endif 34 35 #define _SYS_MUTEX_H 36 #define _SYS_RWLOCK_H 37 #define _SYS_CONDVAR_H 38 #define _SYS_SYSTM_H 39 #define _SYS_DEBUG_H 40 #define _SYS_T_LOCK_H 41 #define _SYS_VNODE_H 42 #define _SYS_VFS_H 43 #define _SYS_SUNDDI_H 44 #define _SYS_CALLB_H 45 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 <synch.h> 56 #include <thread.h> 57 #include <assert.h> 58 #include <alloca.h> 59 #include <umem.h> 60 #include <limits.h> 61 #include <atomic.h> 62 #include <dirent.h> 63 #include <time.h> 64 #include <sys/note.h> 65 #include <sys/types.h> 66 #include <sys/sysmacros.h> 67 #include <sys/bitmap.h> 68 #include <sys/resource.h> 69 #include <sys/byteorder.h> 70 #include <sys/list.h> 71 #include <sys/uio.h> 72 #include <sys/zfs_debug.h> 73 #include <sys/sdt.h> 74 75 /* 76 * Debugging 77 */ 78 79 /* 80 * Note that we are not using the debugging levels. 81 */ 82 83 #define CE_CONT 0 /* continuation */ 84 #define CE_NOTE 1 /* notice */ 85 #define CE_WARN 2 /* warning */ 86 #define CE_PANIC 3 /* panic */ 87 #define CE_IGNORE 4 /* print nothing */ 88 89 /* 90 * ZFS debugging 91 */ 92 93 #ifdef ZFS_DEBUG 94 extern void dprintf_setup(int *argc, char **argv); 95 #endif /* ZFS_DEBUG */ 96 97 extern void cmn_err(int, const char *, ...); 98 extern void panic(const char *, ...); 99 extern void vpanic(const char *, __va_list); 100 101 /* This definition is copied from assert.h. */ 102 #if defined(__STDC__) 103 #if __STDC_VERSION__ - 0 >= 199901L 104 #define verify(EX) (void)((EX) || \ 105 (__assert_c99(#EX, __FILE__, __LINE__, __func__), 0)) 106 #else 107 #define verify(EX) (void)((EX) || (__assert(#EX, __FILE__, __LINE__), 0)) 108 #endif /* __STDC_VERSION__ - 0 >= 199901L */ 109 #else 110 #define verify(EX) (void)((EX) || (_assert("EX", __FILE__, __LINE__), 0)) 111 #endif /* __STDC__ */ 112 113 114 #define VERIFY verify 115 #define ASSERT assert 116 117 extern void __assert(const char *, const char *, int); 118 119 #ifdef lint 120 #define VERIFY3_IMPL(x, y, z, t) if (x == z) ((void)0) 121 #else 122 /* BEGIN CSTYLED */ 123 #define VERIFY3_IMPL(LEFT, OP, RIGHT, TYPE) do { \ 124 const TYPE __left = (TYPE)(LEFT); \ 125 const TYPE __right = (TYPE)(RIGHT); \ 126 if (!(__left OP __right)) { \ 127 char *__buf = alloca(256); \ 128 (void) snprintf(__buf, 256, "%s %s %s (0x%llx %s 0x%llx)", \ 129 #LEFT, #OP, #RIGHT, \ 130 (u_longlong_t)__left, #OP, (u_longlong_t)__right); \ 131 __assert(__buf, __FILE__, __LINE__); \ 132 } \ 133 _NOTE(CONSTCOND) } while (0) 134 /* END CSTYLED */ 135 #endif /* lint */ 136 137 #define VERIFY3S(x, y, z) VERIFY3_IMPL(x, y, z, int64_t) 138 #define VERIFY3U(x, y, z) VERIFY3_IMPL(x, y, z, uint64_t) 139 #define VERIFY3P(x, y, z) VERIFY3_IMPL(x, y, z, uintptr_t) 140 141 #ifdef NDEBUG 142 #define ASSERT3S(x, y, z) ((void)0) 143 #define ASSERT3U(x, y, z) ((void)0) 144 #define ASSERT3P(x, y, z) ((void)0) 145 #else 146 #define ASSERT3S(x, y, z) VERIFY3S(x, y, z) 147 #define ASSERT3U(x, y, z) VERIFY3U(x, y, z) 148 #define ASSERT3P(x, y, z) VERIFY3P(x, y, z) 149 #endif 150 151 /* 152 * Dtrace SDT probes have different signatures in userland than they do in 153 * kernel. If they're being used in kernel code, re-define them out of 154 * existence for their counterparts in libzpool. 155 */ 156 157 #ifdef DTRACE_PROBE1 158 #undef DTRACE_PROBE1 159 #define DTRACE_PROBE1(a, b, c) ((void)0) 160 #endif /* DTRACE_PROBE1 */ 161 162 #ifdef DTRACE_PROBE2 163 #undef DTRACE_PROBE2 164 #define DTRACE_PROBE2(a, b, c, d, e) ((void)0) 165 #endif /* DTRACE_PROBE2 */ 166 167 #ifdef DTRACE_PROBE3 168 #undef DTRACE_PROBE3 169 #define DTRACE_PROBE3(a, b, c, d, e, f, g) ((void)0) 170 #endif /* DTRACE_PROBE3 */ 171 172 /* 173 * Threads 174 */ 175 #define curthread ((void *)(uintptr_t)thr_self()) 176 177 typedef struct kthread kthread_t; 178 179 #define thread_create(stk, stksize, func, arg, len, pp, state, pri) \ 180 zk_thread_create(func, arg) 181 #define thread_exit() thr_exit(0) 182 183 extern kthread_t *zk_thread_create(void (*func)(), void *arg); 184 185 #define issig(why) (FALSE) 186 #define ISSIG(thr, why) (FALSE) 187 188 /* 189 * Mutexes 190 */ 191 typedef struct kmutex { 192 void *m_owner; 193 mutex_t m_lock; 194 } kmutex_t; 195 196 #define MUTEX_DEFAULT USYNC_THREAD 197 #undef MUTEX_HELD 198 #define MUTEX_HELD(m) _mutex_held(&(m)->m_lock) 199 200 /* 201 * Argh -- we have to get cheesy here because the kernel and userland 202 * have different signatures for the same routine. 203 */ 204 extern int _mutex_init(mutex_t *mp, int type, void *arg); 205 extern int _mutex_destroy(mutex_t *mp); 206 207 #define mutex_init(mp, b, c, d) zmutex_init((kmutex_t *)(mp)) 208 #define mutex_destroy(mp) zmutex_destroy((kmutex_t *)(mp)) 209 210 extern void zmutex_init(kmutex_t *mp); 211 extern void zmutex_destroy(kmutex_t *mp); 212 extern void mutex_enter(kmutex_t *mp); 213 extern void mutex_exit(kmutex_t *mp); 214 extern int mutex_tryenter(kmutex_t *mp); 215 extern void *mutex_owner(kmutex_t *mp); 216 217 /* 218 * RW locks 219 */ 220 typedef struct krwlock { 221 void *rw_owner; 222 rwlock_t rw_lock; 223 } krwlock_t; 224 225 typedef int krw_t; 226 227 #define RW_READER 0 228 #define RW_WRITER 1 229 #define RW_DEFAULT USYNC_THREAD 230 231 #undef RW_READ_HELD 232 #define RW_READ_HELD(x) _rw_read_held(&(x)->rw_lock) 233 234 #undef RW_WRITE_HELD 235 #define RW_WRITE_HELD(x) _rw_write_held(&(x)->rw_lock) 236 237 extern void rw_init(krwlock_t *rwlp, char *name, int type, void *arg); 238 extern void rw_destroy(krwlock_t *rwlp); 239 extern void rw_enter(krwlock_t *rwlp, krw_t rw); 240 extern int rw_tryenter(krwlock_t *rwlp, krw_t rw); 241 extern int rw_tryupgrade(krwlock_t *rwlp); 242 extern void rw_exit(krwlock_t *rwlp); 243 #define rw_downgrade(rwlp) do { } while (0) 244 245 /* 246 * Condition variables 247 */ 248 typedef cond_t kcondvar_t; 249 250 #define CV_DEFAULT USYNC_THREAD 251 252 extern void cv_init(kcondvar_t *cv, char *name, int type, void *arg); 253 extern void cv_destroy(kcondvar_t *cv); 254 extern void cv_wait(kcondvar_t *cv, kmutex_t *mp); 255 extern clock_t cv_timedwait(kcondvar_t *cv, kmutex_t *mp, clock_t abstime); 256 extern void cv_signal(kcondvar_t *cv); 257 extern void cv_broadcast(kcondvar_t *cv); 258 259 /* 260 * Kernel memory 261 */ 262 #define KM_SLEEP UMEM_NOFAIL 263 #define KM_NOSLEEP UMEM_DEFAULT 264 #define KMC_NODEBUG UMC_NODEBUG 265 #define kmem_alloc(_s, _f) umem_alloc(_s, _f) 266 #define kmem_zalloc(_s, _f) umem_zalloc(_s, _f) 267 #define kmem_free(_b, _s) umem_free(_b, _s) 268 #define kmem_cache_create(_a, _b, _c, _d, _e, _f, _g, _h, _i) \ 269 umem_cache_create(_a, _b, _c, _d, _e, _f, _g, _h, _i) 270 #define kmem_cache_destroy(_c) umem_cache_destroy(_c) 271 #define kmem_cache_alloc(_c, _f) umem_cache_alloc(_c, _f) 272 #define kmem_cache_free(_c, _b) umem_cache_free(_c, _b) 273 #define kmem_debugging() 0 274 #define kmem_cache_reap_now(c) 275 276 typedef umem_cache_t kmem_cache_t; 277 278 /* 279 * Task queues 280 */ 281 typedef struct taskq taskq_t; 282 typedef uintptr_t taskqid_t; 283 typedef void (task_func_t)(void *); 284 285 #define TASKQ_PREPOPULATE 0x0001 286 #define TASKQ_CPR_SAFE 0x0002 /* Use CPR safe protocol */ 287 #define TASKQ_DYNAMIC 0x0004 /* Use dynamic thread scheduling */ 288 289 #define TQ_SLEEP KM_SLEEP /* Can block for memory */ 290 #define TQ_NOSLEEP KM_NOSLEEP /* cannot block for memory; may fail */ 291 #define TQ_NOQUEUE 0x02 /* Do not enqueue if can't dispatch */ 292 293 extern taskq_t *taskq_create(const char *, int, pri_t, int, int, uint_t); 294 extern taskqid_t taskq_dispatch(taskq_t *, task_func_t, void *, uint_t); 295 extern void taskq_destroy(taskq_t *); 296 extern void taskq_wait(taskq_t *); 297 extern int taskq_member(taskq_t *, void *); 298 299 /* 300 * vnodes 301 */ 302 typedef struct vnode { 303 uint64_t v_size; 304 int v_fd; 305 char *v_path; 306 } vnode_t; 307 308 typedef struct vattr { 309 uint_t va_mask; /* bit-mask of attributes */ 310 u_offset_t va_size; /* file size in bytes */ 311 } vattr_t; 312 313 #define AT_TYPE 0x0001 314 #define AT_MODE 0x0002 315 #define AT_UID 0x0004 316 #define AT_GID 0x0008 317 #define AT_FSID 0x0010 318 #define AT_NODEID 0x0020 319 #define AT_NLINK 0x0040 320 #define AT_SIZE 0x0080 321 #define AT_ATIME 0x0100 322 #define AT_MTIME 0x0200 323 #define AT_CTIME 0x0400 324 #define AT_RDEV 0x0800 325 #define AT_BLKSIZE 0x1000 326 #define AT_NBLOCKS 0x2000 327 #define AT_SEQ 0x8000 328 329 #define CRCREAT 0 330 331 #define VOP_CLOSE(vp, f, c, o, cr) 0 332 #define VOP_PUTPAGE(vp, of, sz, fl, cr) 0 333 #define VOP_GETATTR(vp, vap, fl, cr) ((vap)->va_size = (vp)->v_size, 0) 334 335 #define VOP_FSYNC(vp, f, cr) fsync((vp)->v_fd) 336 337 #define VN_RELE(vp) vn_close(vp) 338 339 extern int vn_open(char *path, int x1, int oflags, int mode, vnode_t **vpp, 340 int x2, int x3); 341 extern int vn_openat(char *path, int x1, int oflags, int mode, vnode_t **vpp, 342 int x2, int x3, vnode_t *vp); 343 extern int vn_rdwr(int uio, vnode_t *vp, void *addr, ssize_t len, 344 offset_t offset, int x1, int x2, rlim64_t x3, void *x4, ssize_t *residp); 345 extern void vn_close(vnode_t *vp); 346 347 #define vn_remove(path, x1, x2) remove(path) 348 #define vn_rename(from, to, seg) rename((from), (to)) 349 350 extern vnode_t *rootdir; 351 352 #include <sys/file.h> /* for FREAD, FWRITE, etc */ 353 354 /* 355 * Random stuff 356 */ 357 #define lbolt (gethrtime() >> 23) 358 #define lbolt64 (gethrtime() >> 23) 359 #define hz 119 /* frequency when using gethrtime() >> 23 for lbolt */ 360 361 extern void delay(clock_t ticks); 362 363 #define gethrestime_sec() time(NULL) 364 365 #define max_ncpus 64 366 367 #define minclsyspri 60 368 #define maxclsyspri 99 369 370 #define CPU_SEQID (thr_self() & (max_ncpus - 1)) 371 372 #define kcred NULL 373 #define CRED() NULL 374 375 extern uint64_t physmem; 376 377 extern int highbit(ulong_t i); 378 extern int random_get_bytes(uint8_t *ptr, size_t len); 379 extern int random_get_pseudo_bytes(uint8_t *ptr, size_t len); 380 381 extern void kernel_init(int); 382 extern void kernel_fini(void); 383 384 struct spa; 385 extern void nicenum(uint64_t num, char *buf); 386 extern void show_pool_stats(struct spa *); 387 388 typedef struct callb_cpr { 389 kmutex_t *cc_lockp; 390 } callb_cpr_t; 391 392 #define CALLB_CPR_INIT(cp, lockp, func, name) { \ 393 (cp)->cc_lockp = lockp; \ 394 } 395 396 #define CALLB_CPR_SAFE_BEGIN(cp) { \ 397 ASSERT(MUTEX_HELD((cp)->cc_lockp)); \ 398 } 399 400 #define CALLB_CPR_SAFE_END(cp, lockp) { \ 401 ASSERT(MUTEX_HELD((cp)->cc_lockp)); \ 402 } 403 404 #define CALLB_CPR_EXIT(cp) { \ 405 ASSERT(MUTEX_HELD((cp)->cc_lockp)); \ 406 mutex_exit((cp)->cc_lockp); \ 407 } 408 409 #define zone_dataset_visible(x, y) (1) 410 #define INGLOBALZONE(z) (1) 411 412 #ifdef __cplusplus 413 } 414 #endif 415 416 /* ZFS Boot Related stuff. */ 417 418 struct _buf { 419 intptr_t _fd; 420 }; 421 422 struct bootstat { 423 uint64_t st_size; 424 }; 425 426 extern struct _buf *kobj_open_file(char *name); 427 extern int kobj_read_file(struct _buf *file, char *buf, unsigned size, 428 unsigned off); 429 extern void kobj_close_file(struct _buf *file); 430 extern int kobj_fstat(intptr_t, struct bootstat *); 431 432 433 #endif /* _SYS_ZFS_CONTEXT_H */ 434