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 2008 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 #ifdef __cplusplus 30 extern "C" { 31 #endif 32 33 #define _SYS_MUTEX_H 34 #define _SYS_RWLOCK_H 35 #define _SYS_CONDVAR_H 36 #define _SYS_SYSTM_H 37 #define _SYS_DEBUG_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 44 #include <stdio.h> 45 #include <stdlib.h> 46 #include <stddef.h> 47 #include <stdarg.h> 48 #include <fcntl.h> 49 #include <unistd.h> 50 #include <errno.h> 51 #include <string.h> 52 #include <strings.h> 53 #include <synch.h> 54 #include <thread.h> 55 #include <assert.h> 56 #include <alloca.h> 57 #include <umem.h> 58 #include <limits.h> 59 #include <atomic.h> 60 #include <dirent.h> 61 #include <time.h> 62 #include <sys/note.h> 63 #include <sys/types.h> 64 #include <sys/cred.h> 65 #include <sys/sysmacros.h> 66 #include <sys/bitmap.h> 67 #include <sys/resource.h> 68 #include <sys/byteorder.h> 69 #include <sys/list.h> 70 #include <sys/uio.h> 71 #include <sys/zfs_debug.h> 72 #include <sys/sdt.h> 73 #include <sys/kstat.h> 74 #include <sys/u8_textprep.h> 75 #include <sys/sysevent/eventdefs.h> 76 77 /* 78 * Debugging 79 */ 80 81 /* 82 * Note that we are not using the debugging levels. 83 */ 84 85 #define CE_CONT 0 /* continuation */ 86 #define CE_NOTE 1 /* notice */ 87 #define CE_WARN 2 /* warning */ 88 #define CE_PANIC 3 /* panic */ 89 #define CE_IGNORE 4 /* print nothing */ 90 91 /* 92 * ZFS debugging 93 */ 94 95 #ifdef ZFS_DEBUG 96 extern void dprintf_setup(int *argc, char **argv); 97 #endif /* ZFS_DEBUG */ 98 99 extern void cmn_err(int, const char *, ...); 100 extern void vcmn_err(int, const char *, __va_list); 101 extern void panic(const char *, ...); 102 extern void vpanic(const char *, __va_list); 103 104 #define fm_panic panic 105 106 /* This definition is copied from assert.h. */ 107 #if defined(__STDC__) 108 #if __STDC_VERSION__ - 0 >= 199901L 109 #define verify(EX) (void)((EX) || \ 110 (__assert_c99(#EX, __FILE__, __LINE__, __func__), 0)) 111 #else 112 #define verify(EX) (void)((EX) || (__assert(#EX, __FILE__, __LINE__), 0)) 113 #endif /* __STDC_VERSION__ - 0 >= 199901L */ 114 #else 115 #define verify(EX) (void)((EX) || (_assert("EX", __FILE__, __LINE__), 0)) 116 #endif /* __STDC__ */ 117 118 119 #define VERIFY verify 120 #define ASSERT assert 121 122 extern void __assert(const char *, const char *, int); 123 124 #ifdef lint 125 #define VERIFY3_IMPL(x, y, z, t) if (x == z) ((void)0) 126 #else 127 /* BEGIN CSTYLED */ 128 #define VERIFY3_IMPL(LEFT, OP, RIGHT, TYPE) do { \ 129 const TYPE __left = (TYPE)(LEFT); \ 130 const TYPE __right = (TYPE)(RIGHT); \ 131 if (!(__left OP __right)) { \ 132 char *__buf = alloca(256); \ 133 (void) snprintf(__buf, 256, "%s %s %s (0x%llx %s 0x%llx)", \ 134 #LEFT, #OP, #RIGHT, \ 135 (u_longlong_t)__left, #OP, (u_longlong_t)__right); \ 136 __assert(__buf, __FILE__, __LINE__); \ 137 } \ 138 _NOTE(CONSTCOND) } while (0) 139 /* END CSTYLED */ 140 #endif /* lint */ 141 142 #define VERIFY3S(x, y, z) VERIFY3_IMPL(x, y, z, int64_t) 143 #define VERIFY3U(x, y, z) VERIFY3_IMPL(x, y, z, uint64_t) 144 #define VERIFY3P(x, y, z) VERIFY3_IMPL(x, y, z, uintptr_t) 145 146 #ifdef NDEBUG 147 #define ASSERT3S(x, y, z) ((void)0) 148 #define ASSERT3U(x, y, z) ((void)0) 149 #define ASSERT3P(x, y, z) ((void)0) 150 #else 151 #define ASSERT3S(x, y, z) VERIFY3S(x, y, z) 152 #define ASSERT3U(x, y, z) VERIFY3U(x, y, z) 153 #define ASSERT3P(x, y, z) VERIFY3P(x, y, z) 154 #endif 155 156 /* 157 * DTrace SDT probes have different signatures in userland than they do in 158 * kernel. If they're being used in kernel code, re-define them out of 159 * existence for their counterparts in libzpool. 160 */ 161 162 #ifdef DTRACE_PROBE 163 #undef DTRACE_PROBE 164 #define DTRACE_PROBE(a) ((void)0) 165 #endif /* DTRACE_PROBE */ 166 167 #ifdef DTRACE_PROBE1 168 #undef DTRACE_PROBE1 169 #define DTRACE_PROBE1(a, b, c) ((void)0) 170 #endif /* DTRACE_PROBE1 */ 171 172 #ifdef DTRACE_PROBE2 173 #undef DTRACE_PROBE2 174 #define DTRACE_PROBE2(a, b, c, d, e) ((void)0) 175 #endif /* DTRACE_PROBE2 */ 176 177 #ifdef DTRACE_PROBE3 178 #undef DTRACE_PROBE3 179 #define DTRACE_PROBE3(a, b, c, d, e, f, g) ((void)0) 180 #endif /* DTRACE_PROBE3 */ 181 182 #ifdef DTRACE_PROBE4 183 #undef DTRACE_PROBE4 184 #define DTRACE_PROBE4(a, b, c, d, e, f, g, h, i) ((void)0) 185 #endif /* DTRACE_PROBE4 */ 186 187 /* 188 * Threads 189 */ 190 #define curthread ((void *)(uintptr_t)thr_self()) 191 192 typedef struct kthread kthread_t; 193 194 #define thread_create(stk, stksize, func, arg, len, pp, state, pri) \ 195 zk_thread_create(func, arg) 196 #define thread_exit() thr_exit(NULL) 197 198 extern kthread_t *zk_thread_create(void (*func)(), void *arg); 199 200 #define issig(why) (FALSE) 201 #define ISSIG(thr, why) (FALSE) 202 203 /* 204 * Mutexes 205 */ 206 typedef struct kmutex { 207 void *m_owner; 208 boolean_t initialized; 209 mutex_t m_lock; 210 } kmutex_t; 211 212 #define MUTEX_DEFAULT USYNC_THREAD 213 #undef MUTEX_HELD 214 #define MUTEX_HELD(m) _mutex_held(&(m)->m_lock) 215 216 /* 217 * Argh -- we have to get cheesy here because the kernel and userland 218 * have different signatures for the same routine. 219 */ 220 extern int _mutex_init(mutex_t *mp, int type, void *arg); 221 extern int _mutex_destroy(mutex_t *mp); 222 223 #define mutex_init(mp, b, c, d) zmutex_init((kmutex_t *)(mp)) 224 #define mutex_destroy(mp) zmutex_destroy((kmutex_t *)(mp)) 225 226 extern void zmutex_init(kmutex_t *mp); 227 extern void zmutex_destroy(kmutex_t *mp); 228 extern void mutex_enter(kmutex_t *mp); 229 extern void mutex_exit(kmutex_t *mp); 230 extern int mutex_tryenter(kmutex_t *mp); 231 extern void *mutex_owner(kmutex_t *mp); 232 233 /* 234 * RW locks 235 */ 236 typedef struct krwlock { 237 void *rw_owner; 238 boolean_t initialized; 239 rwlock_t rw_lock; 240 } krwlock_t; 241 242 typedef int krw_t; 243 244 #define RW_READER 0 245 #define RW_WRITER 1 246 #define RW_DEFAULT USYNC_THREAD 247 248 #undef RW_READ_HELD 249 #define RW_READ_HELD(x) _rw_read_held(&(x)->rw_lock) 250 251 #undef RW_WRITE_HELD 252 #define RW_WRITE_HELD(x) _rw_write_held(&(x)->rw_lock) 253 254 extern void rw_init(krwlock_t *rwlp, char *name, int type, void *arg); 255 extern void rw_destroy(krwlock_t *rwlp); 256 extern void rw_enter(krwlock_t *rwlp, krw_t rw); 257 extern int rw_tryenter(krwlock_t *rwlp, krw_t rw); 258 extern int rw_tryupgrade(krwlock_t *rwlp); 259 extern void rw_exit(krwlock_t *rwlp); 260 #define rw_downgrade(rwlp) do { } while (0) 261 262 extern uid_t crgetuid(cred_t *cr); 263 extern gid_t crgetgid(cred_t *cr); 264 extern int crgetngroups(cred_t *cr); 265 extern gid_t *crgetgroups(cred_t *cr); 266 267 /* 268 * Condition variables 269 */ 270 typedef cond_t kcondvar_t; 271 272 #define CV_DEFAULT USYNC_THREAD 273 274 extern void cv_init(kcondvar_t *cv, char *name, int type, void *arg); 275 extern void cv_destroy(kcondvar_t *cv); 276 extern void cv_wait(kcondvar_t *cv, kmutex_t *mp); 277 extern clock_t cv_timedwait(kcondvar_t *cv, kmutex_t *mp, clock_t abstime); 278 extern void cv_signal(kcondvar_t *cv); 279 extern void cv_broadcast(kcondvar_t *cv); 280 281 /* 282 * kstat creation, installation and deletion 283 */ 284 extern kstat_t *kstat_create(char *, int, 285 char *, char *, uchar_t, ulong_t, uchar_t); 286 extern void kstat_install(kstat_t *); 287 extern void kstat_delete(kstat_t *); 288 289 /* 290 * Kernel memory 291 */ 292 #define KM_SLEEP UMEM_NOFAIL 293 #define KM_PUSHPAGE KM_SLEEP 294 #define KM_NOSLEEP UMEM_DEFAULT 295 #define KMC_NODEBUG UMC_NODEBUG 296 #define kmem_alloc(_s, _f) umem_alloc(_s, _f) 297 #define kmem_zalloc(_s, _f) umem_zalloc(_s, _f) 298 #define kmem_free(_b, _s) umem_free(_b, _s) 299 #define kmem_cache_create(_a, _b, _c, _d, _e, _f, _g, _h, _i) \ 300 umem_cache_create(_a, _b, _c, _d, _e, _f, _g, _h, _i) 301 #define kmem_cache_destroy(_c) umem_cache_destroy(_c) 302 #define kmem_cache_alloc(_c, _f) umem_cache_alloc(_c, _f) 303 #define kmem_cache_free(_c, _b) umem_cache_free(_c, _b) 304 #define kmem_debugging() 0 305 #define kmem_cache_reap_now(c) 306 307 typedef umem_cache_t kmem_cache_t; 308 309 /* 310 * Task queues 311 */ 312 typedef struct taskq taskq_t; 313 typedef uintptr_t taskqid_t; 314 typedef void (task_func_t)(void *); 315 316 #define TASKQ_PREPOPULATE 0x0001 317 #define TASKQ_CPR_SAFE 0x0002 /* Use CPR safe protocol */ 318 #define TASKQ_DYNAMIC 0x0004 /* Use dynamic thread scheduling */ 319 320 #define TQ_SLEEP KM_SLEEP /* Can block for memory */ 321 #define TQ_NOSLEEP KM_NOSLEEP /* cannot block for memory; may fail */ 322 #define TQ_NOQUEUE 0x02 /* Do not enqueue if can't dispatch */ 323 324 extern taskq_t *system_taskq; 325 326 extern taskq_t *taskq_create(const char *, int, pri_t, int, int, uint_t); 327 extern taskqid_t taskq_dispatch(taskq_t *, task_func_t, void *, uint_t); 328 extern void taskq_destroy(taskq_t *); 329 extern void taskq_wait(taskq_t *); 330 extern int taskq_member(taskq_t *, void *); 331 extern void system_taskq_init(void); 332 333 #define XVA_MAPSIZE 3 334 #define XVA_MAGIC 0x78766174 335 336 /* 337 * vnodes 338 */ 339 typedef struct vnode { 340 uint64_t v_size; 341 int v_fd; 342 char *v_path; 343 } vnode_t; 344 345 346 typedef struct xoptattr { 347 timestruc_t xoa_createtime; /* Create time of file */ 348 uint8_t xoa_archive; 349 uint8_t xoa_system; 350 uint8_t xoa_readonly; 351 uint8_t xoa_hidden; 352 uint8_t xoa_nounlink; 353 uint8_t xoa_immutable; 354 uint8_t xoa_appendonly; 355 uint8_t xoa_nodump; 356 uint8_t xoa_settable; 357 uint8_t xoa_opaque; 358 uint8_t xoa_av_quarantined; 359 uint8_t xoa_av_modified; 360 } xoptattr_t; 361 362 typedef struct vattr { 363 uint_t va_mask; /* bit-mask of attributes */ 364 u_offset_t va_size; /* file size in bytes */ 365 } vattr_t; 366 367 368 typedef struct xvattr { 369 vattr_t xva_vattr; /* Embedded vattr structure */ 370 uint32_t xva_magic; /* Magic Number */ 371 uint32_t xva_mapsize; /* Size of attr bitmap (32-bit words) */ 372 uint32_t *xva_rtnattrmapp; /* Ptr to xva_rtnattrmap[] */ 373 uint32_t xva_reqattrmap[XVA_MAPSIZE]; /* Requested attrs */ 374 uint32_t xva_rtnattrmap[XVA_MAPSIZE]; /* Returned attrs */ 375 xoptattr_t xva_xoptattrs; /* Optional attributes */ 376 } xvattr_t; 377 378 typedef struct vsecattr { 379 uint_t vsa_mask; /* See below */ 380 int vsa_aclcnt; /* ACL entry count */ 381 void *vsa_aclentp; /* pointer to ACL entries */ 382 int vsa_dfaclcnt; /* default ACL entry count */ 383 void *vsa_dfaclentp; /* pointer to default ACL entries */ 384 size_t vsa_aclentsz; /* ACE size in bytes of vsa_aclentp */ 385 } vsecattr_t; 386 387 #define AT_TYPE 0x00001 388 #define AT_MODE 0x00002 389 #define AT_UID 0x00004 390 #define AT_GID 0x00008 391 #define AT_FSID 0x00010 392 #define AT_NODEID 0x00020 393 #define AT_NLINK 0x00040 394 #define AT_SIZE 0x00080 395 #define AT_ATIME 0x00100 396 #define AT_MTIME 0x00200 397 #define AT_CTIME 0x00400 398 #define AT_RDEV 0x00800 399 #define AT_BLKSIZE 0x01000 400 #define AT_NBLOCKS 0x02000 401 #define AT_SEQ 0x08000 402 #define AT_XVATTR 0x10000 403 404 #define CRCREAT 0 405 406 #define VOP_CLOSE(vp, f, c, o, cr, ct) 0 407 #define VOP_PUTPAGE(vp, of, sz, fl, cr, ct) 0 408 #define VOP_GETATTR(vp, vap, fl, cr, ct) ((vap)->va_size = (vp)->v_size, 0) 409 410 #define VOP_FSYNC(vp, f, cr, ct) fsync((vp)->v_fd) 411 412 #define VN_RELE(vp) vn_close(vp) 413 414 extern int vn_open(char *path, int x1, int oflags, int mode, vnode_t **vpp, 415 int x2, int x3); 416 extern int vn_openat(char *path, int x1, int oflags, int mode, vnode_t **vpp, 417 int x2, int x3, vnode_t *vp, int fd); 418 extern int vn_rdwr(int uio, vnode_t *vp, void *addr, ssize_t len, 419 offset_t offset, int x1, int x2, rlim64_t x3, void *x4, ssize_t *residp); 420 extern void vn_close(vnode_t *vp); 421 422 #define vn_remove(path, x1, x2) remove(path) 423 #define vn_rename(from, to, seg) rename((from), (to)) 424 #define vn_is_readonly(vp) B_FALSE 425 426 extern vnode_t *rootdir; 427 428 #include <sys/file.h> /* for FREAD, FWRITE, etc */ 429 430 /* 431 * Random stuff 432 */ 433 #define lbolt (gethrtime() >> 23) 434 #define lbolt64 (gethrtime() >> 23) 435 #define hz 119 /* frequency when using gethrtime() >> 23 for lbolt */ 436 437 extern void delay(clock_t ticks); 438 439 #define gethrestime_sec() time(NULL) 440 441 #define max_ncpus 64 442 443 #define minclsyspri 60 444 #define maxclsyspri 99 445 446 #define CPU_SEQID (thr_self() & (max_ncpus - 1)) 447 448 #define kcred NULL 449 #define CRED() NULL 450 451 #define ptob(x) ((x) * PAGESIZE) 452 453 extern uint64_t physmem; 454 455 extern int highbit(ulong_t i); 456 extern int random_get_bytes(uint8_t *ptr, size_t len); 457 extern int random_get_pseudo_bytes(uint8_t *ptr, size_t len); 458 459 extern void kernel_init(int); 460 extern void kernel_fini(void); 461 462 struct spa; 463 extern void nicenum(uint64_t num, char *buf); 464 extern void show_pool_stats(struct spa *); 465 466 typedef struct callb_cpr { 467 kmutex_t *cc_lockp; 468 } callb_cpr_t; 469 470 #define CALLB_CPR_INIT(cp, lockp, func, name) { \ 471 (cp)->cc_lockp = lockp; \ 472 } 473 474 #define CALLB_CPR_SAFE_BEGIN(cp) { \ 475 ASSERT(MUTEX_HELD((cp)->cc_lockp)); \ 476 } 477 478 #define CALLB_CPR_SAFE_END(cp, lockp) { \ 479 ASSERT(MUTEX_HELD((cp)->cc_lockp)); \ 480 } 481 482 #define CALLB_CPR_EXIT(cp) { \ 483 ASSERT(MUTEX_HELD((cp)->cc_lockp)); \ 484 mutex_exit((cp)->cc_lockp); \ 485 } 486 487 #define zone_dataset_visible(x, y) (1) 488 #define INGLOBALZONE(z) (1) 489 490 /* 491 * Hostname information 492 */ 493 extern char hw_serial[]; 494 extern int ddi_strtoul(const char *str, char **nptr, int base, 495 unsigned long *result); 496 497 /* ZFS Boot Related stuff. */ 498 499 struct _buf { 500 intptr_t _fd; 501 }; 502 503 struct bootstat { 504 uint64_t st_size; 505 }; 506 507 typedef struct ace_object { 508 uid_t a_who; 509 uint32_t a_access_mask; 510 uint16_t a_flags; 511 uint16_t a_type; 512 uint8_t a_obj_type[16]; 513 uint8_t a_inherit_obj_type[16]; 514 } ace_object_t; 515 516 517 #define ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE 0x05 518 #define ACE_ACCESS_DENIED_OBJECT_ACE_TYPE 0x06 519 #define ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE 0x07 520 #define ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE 0x08 521 522 extern struct _buf *kobj_open_file(char *name); 523 extern int kobj_read_file(struct _buf *file, char *buf, unsigned size, 524 unsigned off); 525 extern void kobj_close_file(struct _buf *file); 526 extern int kobj_get_filesize(struct _buf *file, uint64_t *size); 527 extern int zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr); 528 extern int zfs_secpolicy_rename_perms(const char *from, const char *to, 529 cred_t *cr); 530 extern int zfs_secpolicy_destroy_perms(const char *name, cred_t *cr); 531 extern zoneid_t getzoneid(void); 532 533 /* SID stuff */ 534 typedef struct ksiddomain { 535 uint_t kd_ref; 536 uint_t kd_len; 537 char *kd_name; 538 } ksiddomain_t; 539 540 ksiddomain_t *ksid_lookupdomain(const char *); 541 void ksiddomain_rele(ksiddomain_t *); 542 543 #ifdef __cplusplus 544 } 545 #endif 546 547 #endif /* _SYS_ZFS_CONTEXT_H */ 548