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