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.
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.
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]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2013 by Delphix. All rights reserved.
24 * Copyright (c) 2012, Joyent, Inc. All rights reserved.
27 #ifndef _SYS_ZFS_CONTEXT_H
28 #define _SYS_ZFS_CONTEXT_H
36 #define _SYS_CONDVAR_H
65 #include <sys/debug.h>
67 #include <sys/types.h>
69 #include <sys/atomic.h>
70 #include <sys/sysmacros.h>
71 #include <sys/bitmap.h>
72 #include <sys/resource.h>
73 #include <sys/byteorder.h>
77 #include <sys/mntent.h>
78 #include <sys/mnttab.h>
79 #include <sys/zfs_debug.h>
81 #include <sys/kstat.h>
82 #include <sys/u8_textprep.h>
83 #include <sys/kernel.h>
85 #include <sys/sysevent.h>
86 #include <sys/sysevent/eventdefs.h>
87 #include <sys/sysevent/dev.h>
88 #include <machine/atomic.h>
89 #include <sys/debug.h>
94 #define ZFS_EXPORTS_PATH "/etc/zfs/exports"
101 * Note that we are not using the debugging levels.
104 #define CE_CONT 0 /* continuation */
105 #define CE_NOTE 1 /* notice */
106 #define CE_WARN 2 /* warning */
107 #define CE_PANIC 3 /* panic */
108 #define CE_IGNORE 4 /* print nothing */
114 #define ZFS_LOG(...) do { } while (0)
116 typedef u_longlong_t rlim64_t;
117 #define RLIM64_INFINITY ((rlim64_t)-3)
120 extern void dprintf_setup(int *argc, char **argv);
121 #endif /* ZFS_DEBUG */
123 extern void cmn_err(int, const char *, ...);
124 extern void vcmn_err(int, const char *, __va_list);
125 extern void panic(const char *, ...);
126 extern void vpanic(const char *, __va_list);
128 #define fm_panic panic
133 * DTrace SDT probes have different signatures in userland than they do in
134 * kernel. If they're being used in kernel code, re-define them out of
135 * existence for their counterparts in libzpool.
140 #endif /* DTRACE_PROBE */
142 #define DTRACE_PROBE(a) \
148 #endif /* DTRACE_PROBE1 */
150 #define DTRACE_PROBE1(a, b, c) \
151 ZFS_PROBE1(#a, (unsigned long)c)
156 #endif /* DTRACE_PROBE2 */
158 #define DTRACE_PROBE2(a, b, c, d, e) \
159 ZFS_PROBE2(#a, (unsigned long)c, (unsigned long)e)
164 #endif /* DTRACE_PROBE3 */
166 #define DTRACE_PROBE3(a, b, c, d, e, f, g) \
167 ZFS_PROBE3(#a, (unsigned long)c, (unsigned long)e, (unsigned long)g)
172 #endif /* DTRACE_PROBE4 */
174 #define DTRACE_PROBE4(a, b, c, d, e, f, g, h, i) \
175 ZFS_PROBE4(#a, (unsigned long)c, (unsigned long)e, (unsigned long)g, \
181 * We use the comma operator so that this macro can be used without much
182 * additional code. For example, "return (EINVAL);" becomes
183 * "return (SET_ERROR(EINVAL));". Note that the argument will be evaluated
184 * twice, so it should not have side effects (e.g. something like:
185 * "return (SET_ERROR(log_error(EINVAL, info)));" would log the error twice).
187 #define SET_ERROR(err) (ZFS_SET_ERROR(err), err)
190 #define DTRACE_PROBE(a) ((void)0)
191 #define DTRACE_PROBE1(a, b, c) ((void)0)
192 #define DTRACE_PROBE2(a, b, c, d, e) ((void)0)
193 #define DTRACE_PROBE3(a, b, c, d, e, f, g) ((void)0)
194 #define DTRACE_PROBE4(a, b, c, d, e, f, g, h, i) ((void)0)
196 #define SET_ERROR(err) (err)
197 #endif /* !illumos */
202 #define curthread ((void *)(uintptr_t)thr_self())
204 typedef struct kthread kthread_t;
206 #define thread_create(stk, stksize, func, arg, len, pp, state, pri) \
207 zk_thread_create(func, arg)
208 #define thread_exit() thr_exit(NULL)
209 #define thread_join(t) panic("libzpool cannot join threads")
211 #define newproc(f, a, cid, pri, ctp, pid) (ENOSYS)
213 /* in libzpool, p0 exists only to have its address taken */
215 uintptr_t this_is_never_used_dont_dereference_it;
218 extern struct proc p0;
219 #define curproc (&p0)
223 extern kthread_t *zk_thread_create(void (*func)(), void *arg);
225 #define issig(why) (FALSE)
226 #define ISSIG(thr, why) (FALSE)
231 typedef struct kmutex {
233 boolean_t initialized;
237 #define MUTEX_DEFAULT USYNC_THREAD
239 #undef MUTEX_NOT_HELD
240 #define MUTEX_HELD(m) ((m)->m_owner == curthread)
241 #define MUTEX_NOT_HELD(m) (!MUTEX_HELD(m))
242 #define _mutex_held(m) pthread_mutex_isowned_np(m)
245 * Argh -- we have to get cheesy here because the kernel and userland
246 * have different signatures for the same routine.
248 //extern int _mutex_init(mutex_t *mp, int type, void *arg);
249 //extern int _mutex_destroy(mutex_t *mp);
250 //extern int _mutex_owned(mutex_t *mp);
252 #define mutex_init(mp, b, c, d) zmutex_init((kmutex_t *)(mp))
253 #define mutex_destroy(mp) zmutex_destroy((kmutex_t *)(mp))
254 #define mutex_owned(mp) zmutex_owned((kmutex_t *)(mp))
256 extern void zmutex_init(kmutex_t *mp);
257 extern void zmutex_destroy(kmutex_t *mp);
258 extern int zmutex_owned(kmutex_t *mp);
259 extern void mutex_enter(kmutex_t *mp);
260 extern void mutex_exit(kmutex_t *mp);
261 extern int mutex_tryenter(kmutex_t *mp);
262 extern void *mutex_owner(kmutex_t *mp);
267 typedef struct krwlock {
270 boolean_t initialized;
278 #define RW_DEFAULT USYNC_THREAD
281 #define RW_READ_HELD(x) ((x)->rw_owner == NULL && (x)->rw_count > 0)
284 #define RW_WRITE_HELD(x) ((x)->rw_owner == curthread)
285 #define RW_LOCK_HELD(x) rw_lock_held(x)
288 #define RW_LOCK_HELD(x) (RW_READ_HELD(x) || RW_WRITE_HELD(x))
290 extern void rw_init(krwlock_t *rwlp, char *name, int type, void *arg);
291 extern void rw_destroy(krwlock_t *rwlp);
292 extern void rw_enter(krwlock_t *rwlp, krw_t rw);
293 extern int rw_tryenter(krwlock_t *rwlp, krw_t rw);
294 extern int rw_tryupgrade(krwlock_t *rwlp);
295 extern void rw_exit(krwlock_t *rwlp);
296 extern int rw_lock_held(krwlock_t *rwlp);
297 #define rw_downgrade(rwlp) do { } while (0)
299 extern uid_t crgetuid(cred_t *cr);
300 extern uid_t crgetruid(cred_t *cr);
301 extern gid_t crgetgid(cred_t *cr);
302 extern int crgetngroups(cred_t *cr);
303 extern gid_t *crgetgroups(cred_t *cr);
306 * Condition variables
308 typedef cond_t kcondvar_t;
310 #define CV_DEFAULT USYNC_THREAD
312 extern void cv_init(kcondvar_t *cv, char *name, int type, void *arg);
313 extern void cv_destroy(kcondvar_t *cv);
314 extern void cv_wait(kcondvar_t *cv, kmutex_t *mp);
315 extern clock_t cv_timedwait(kcondvar_t *cv, kmutex_t *mp, clock_t abstime);
316 extern void cv_signal(kcondvar_t *cv);
317 extern void cv_broadcast(kcondvar_t *cv);
320 * Thread-specific data
322 #define tsd_get(k) pthread_getspecific(k)
323 #define tsd_set(k, v) pthread_setspecific(k, v)
324 #define tsd_create(kp, d) pthread_key_create(kp, d)
325 #define tsd_destroy(kp) /* nothing */
330 #define KM_SLEEP UMEM_NOFAIL
331 #define KM_PUSHPAGE KM_SLEEP
332 #define KM_NOSLEEP UMEM_DEFAULT
334 #define KMC_NODEBUG UMC_NODEBUG
335 #define KMC_NOTOUCH 0 /* not needed for userland caches */
336 #define kmem_alloc(_s, _f) umem_alloc(_s, _f)
337 #define kmem_zalloc(_s, _f) umem_zalloc(_s, _f)
338 #define kmem_free(_b, _s) umem_free(_b, _s)
339 #define kmem_size() (physmem * PAGESIZE)
340 #define kmem_cache_create(_a, _b, _c, _d, _e, _f, _g, _h, _i) \
341 umem_cache_create(_a, _b, _c, _d, _e, _f, _g, _h, _i)
342 #define kmem_cache_destroy(_c) umem_cache_destroy(_c)
343 #define kmem_cache_alloc(_c, _f) umem_cache_alloc(_c, _f)
344 #define kmem_cache_free(_c, _b) umem_cache_free(_c, _b)
345 #define kmem_debugging() 0
346 #define kmem_cache_reap_now(_c) /* nothing */
347 #define kmem_cache_set_move(_c, _cb) /* nothing */
348 #define POINTER_INVALIDATE(_pp) /* nothing */
349 #define POINTER_IS_VALID(_p) 0
351 typedef umem_cache_t kmem_cache_t;
353 typedef enum kmem_cbrc {
364 typedef struct taskq taskq_t;
365 typedef uintptr_t taskqid_t;
366 typedef void (task_func_t)(void *);
368 #define TASKQ_PREPOPULATE 0x0001
369 #define TASKQ_CPR_SAFE 0x0002 /* Use CPR safe protocol */
370 #define TASKQ_DYNAMIC 0x0004 /* Use dynamic thread scheduling */
371 #define TASKQ_THREADS_CPU_PCT 0x0008 /* Scale # threads by # cpus */
372 #define TASKQ_DC_BATCH 0x0010 /* Mark threads as batch */
374 #define TQ_SLEEP KM_SLEEP /* Can block for memory */
375 #define TQ_NOSLEEP KM_NOSLEEP /* cannot block for memory; may fail */
376 #define TQ_NOQUEUE 0x02 /* Do not enqueue if can't dispatch */
377 #define TQ_FRONT 0x08 /* Queue in front */
379 extern taskq_t *system_taskq;
381 extern taskq_t *taskq_create(const char *, int, pri_t, int, int, uint_t);
382 #define taskq_create_proc(a, b, c, d, e, p, f) \
383 (taskq_create(a, b, c, d, e, f))
384 #define taskq_create_sysdc(a, b, d, e, p, dc, f) \
385 (taskq_create(a, b, maxclsyspri, d, e, f))
386 extern taskqid_t taskq_dispatch(taskq_t *, task_func_t, void *, uint_t);
387 extern void taskq_destroy(taskq_t *);
388 extern void taskq_wait(taskq_t *);
389 extern int taskq_member(taskq_t *, void *);
390 extern void system_taskq_init(void);
391 extern void system_taskq_fini(void);
393 #define taskq_dispatch_safe(tq, func, arg, flags, task) \
394 taskq_dispatch((tq), (func), (arg), (flags))
396 #define XVA_MAPSIZE 3
397 #define XVA_MAGIC 0x78766174
402 typedef struct vnode {
408 #define AV_SCANSTAMP_SZ 32 /* length of anti-virus scanstamp */
410 typedef struct xoptattr {
411 timestruc_t xoa_createtime; /* Create time of file */
414 uint8_t xoa_readonly;
416 uint8_t xoa_nounlink;
417 uint8_t xoa_immutable;
418 uint8_t xoa_appendonly;
420 uint8_t xoa_settable;
422 uint8_t xoa_av_quarantined;
423 uint8_t xoa_av_modified;
424 uint8_t xoa_av_scanstamp[AV_SCANSTAMP_SZ];
430 typedef struct vattr {
431 uint_t va_mask; /* bit-mask of attributes */
432 u_offset_t va_size; /* file size in bytes */
436 typedef struct xvattr {
437 vattr_t xva_vattr; /* Embedded vattr structure */
438 uint32_t xva_magic; /* Magic Number */
439 uint32_t xva_mapsize; /* Size of attr bitmap (32-bit words) */
440 uint32_t *xva_rtnattrmapp; /* Ptr to xva_rtnattrmap[] */
441 uint32_t xva_reqattrmap[XVA_MAPSIZE]; /* Requested attrs */
442 uint32_t xva_rtnattrmap[XVA_MAPSIZE]; /* Returned attrs */
443 xoptattr_t xva_xoptattrs; /* Optional attributes */
446 typedef struct vsecattr {
447 uint_t vsa_mask; /* See below */
448 int vsa_aclcnt; /* ACL entry count */
449 void *vsa_aclentp; /* pointer to ACL entries */
450 int vsa_dfaclcnt; /* default ACL entry count */
451 void *vsa_dfaclentp; /* pointer to default ACL entries */
452 size_t vsa_aclentsz; /* ACE size in bytes of vsa_aclentp */
455 #define AT_TYPE 0x00001
456 #define AT_MODE 0x00002
457 #define AT_UID 0x00004
458 #define AT_GID 0x00008
459 #define AT_FSID 0x00010
460 #define AT_NODEID 0x00020
461 #define AT_NLINK 0x00040
462 #define AT_SIZE 0x00080
463 #define AT_ATIME 0x00100
464 #define AT_MTIME 0x00200
465 #define AT_CTIME 0x00400
466 #define AT_RDEV 0x00800
467 #define AT_BLKSIZE 0x01000
468 #define AT_NBLOCKS 0x02000
469 #define AT_SEQ 0x08000
470 #define AT_XVATTR 0x10000
474 extern int fop_getattr(vnode_t *vp, vattr_t *vap);
476 #define VOP_CLOSE(vp, f, c, o, cr, ct) 0
477 #define VOP_PUTPAGE(vp, of, sz, fl, cr, ct) 0
478 #define VOP_GETATTR(vp, vap, cr) fop_getattr((vp), (vap));
480 #define VOP_FSYNC(vp, f, cr, ct) fsync((vp)->v_fd)
482 #define VN_RELE(vp) vn_close(vp, 0, NULL, NULL)
483 #define VN_RELE_ASYNC(vp, taskq) vn_close(vp, 0, NULL, NULL)
485 #define vn_lock(vp, type)
486 #define VOP_UNLOCK(vp, type)
487 #ifdef VFS_LOCK_GIANT
488 #undef VFS_LOCK_GIANT
490 #define VFS_LOCK_GIANT(mp) 0
491 #ifdef VFS_UNLOCK_GIANT
492 #undef VFS_UNLOCK_GIANT
494 #define VFS_UNLOCK_GIANT(vfslocked)
496 extern int vn_open(char *path, int x1, int oflags, int mode, vnode_t **vpp,
498 extern int vn_openat(char *path, int x1, int oflags, int mode, vnode_t **vpp,
499 int x2, int x3, vnode_t *vp, int fd);
500 extern int vn_rdwr(int uio, vnode_t *vp, void *addr, ssize_t len,
501 offset_t offset, int x1, int x2, rlim64_t x3, void *x4, ssize_t *residp);
502 extern void vn_close(vnode_t *vp, int openflag, cred_t *cr, kthread_t *td);
504 #define vn_remove(path, x1, x2) remove(path)
505 #define vn_rename(from, to, seg) rename((from), (to))
506 #define vn_is_readonly(vp) B_FALSE
508 extern vnode_t *rootdir;
510 #include <sys/file.h> /* for FREAD, FWRITE, etc */
511 #define FTRUNC O_TRUNC
516 #define ddi_get_lbolt() (gethrtime() >> 23)
517 #define ddi_get_lbolt64() (gethrtime() >> 23)
518 #define hz 119 /* frequency when using gethrtime() >> 23 for lbolt */
520 extern void delay(clock_t ticks);
522 #define SEC_TO_TICK(sec) ((sec) * hz)
523 #define NSEC_TO_TICK(usec) ((usec) / (NANOSEC / hz))
525 #define gethrestime_sec() time(NULL)
526 #define gethrestime(t) \
528 (t)->tv_sec = gethrestime_sec();\
534 #define minclsyspri 60
535 #define maxclsyspri 99
537 #define CPU_SEQID (thr_self() & (max_ncpus - 1))
543 #define ptob(x) ((x) * PAGESIZE)
546 extern uint64_t physmem;
548 extern int highbit(ulong_t i);
549 extern int random_get_bytes(uint8_t *ptr, size_t len);
550 extern int random_get_pseudo_bytes(uint8_t *ptr, size_t len);
552 extern void kernel_init(int);
553 extern void kernel_fini(void);
556 extern void nicenum(uint64_t num, char *buf);
557 extern void show_pool_stats(struct spa *);
559 typedef struct callb_cpr {
563 #define CALLB_CPR_INIT(cp, lockp, func, name) { \
564 (cp)->cc_lockp = lockp; \
567 #define CALLB_CPR_SAFE_BEGIN(cp) { \
568 ASSERT(MUTEX_HELD((cp)->cc_lockp)); \
571 #define CALLB_CPR_SAFE_END(cp, lockp) { \
572 ASSERT(MUTEX_HELD((cp)->cc_lockp)); \
575 #define CALLB_CPR_EXIT(cp) { \
576 ASSERT(MUTEX_HELD((cp)->cc_lockp)); \
577 mutex_exit((cp)->cc_lockp); \
580 #define zone_dataset_visible(x, y) (1)
581 #define INGLOBALZONE(z) (1)
583 extern char *kmem_asprintf(const char *fmt, ...);
584 #define strfree(str) kmem_free((str), strlen(str) + 1)
587 * Hostname information
589 extern struct utsname utsname;
590 extern char hw_serial[]; /* for userland-emulated hostid access */
591 extern int ddi_strtoul(const char *str, char **nptr, int base,
592 unsigned long *result);
594 extern int ddi_strtoull(const char *str, char **nptr, int base,
595 u_longlong_t *result);
597 /* ZFS Boot Related stuff. */
607 typedef struct ace_object {
609 uint32_t a_access_mask;
612 uint8_t a_obj_type[16];
613 uint8_t a_inherit_obj_type[16];
617 #define ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE 0x05
618 #define ACE_ACCESS_DENIED_OBJECT_ACE_TYPE 0x06
619 #define ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE 0x07
620 #define ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE 0x08
622 extern struct _buf *kobj_open_file(char *name);
623 extern int kobj_read_file(struct _buf *file, char *buf, unsigned size,
625 extern void kobj_close_file(struct _buf *file);
626 extern int kobj_get_filesize(struct _buf *file, uint64_t *size);
627 extern int zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr);
628 extern int zfs_secpolicy_rename_perms(const char *from, const char *to,
630 extern int zfs_secpolicy_destroy_perms(const char *name, cred_t *cr);
631 extern zoneid_t getzoneid(void);
632 /* Random compatibility stuff. */
633 #define lbolt (gethrtime() >> 23)
634 #define lbolt64 (gethrtime() >> 23)
636 extern uint64_t physmem;
638 #define gethrestime_sec() time(NULL)
640 #define pwrite64(d, p, n, o) pwrite(d, p, n, o)
641 #define readdir64(d) readdir(d)
642 #define SIGPENDING(td) (0)
643 #define root_mount_wait() do { } while (0)
644 #define root_mounted() (1)
650 #define FCREAT O_CREAT
654 typedef struct ksiddomain {
660 ksiddomain_t *ksid_lookupdomain(const char *);
661 void ksiddomain_rele(ksiddomain_t *);
663 typedef uint32_t idmap_rid_t;
665 #define DDI_SLEEP KM_SLEEP
666 #define ddi_log_sysevent(_a, _b, _c, _d, _e, _f, _g) (0)
668 #define SX_SYSINIT(name, lock, desc)
670 #define SYSCTL_DECL(...)
671 #define SYSCTL_NODE(...)
672 #define SYSCTL_INT(...)
673 #define SYSCTL_UINT(...)
674 #define SYSCTL_ULONG(...)
675 #define SYSCTL_QUAD(...)
676 #define SYSCTL_UQUAD(...)
682 #define TUNABLE_INT(...)
683 #define TUNABLE_ULONG(...)
684 #define TUNABLE_QUAD(...)
689 #define ERESTART (-1)
696 extern kmutex_t cpu_lock;
698 typedef uintptr_t cyclic_id_t;
699 typedef uint16_t cyc_level_t;
700 typedef void (*cyc_func_t)(void *);
702 #define CY_LOW_LEVEL 0
703 #define CY_INFINITY INT64_MAX
704 #define CYCLIC_NONE ((cyclic_id_t)0)
706 typedef struct cyc_time {
708 hrtime_t cyt_interval;
711 typedef struct cyc_handler {
714 cyc_level_t cyh_level;
717 extern cyclic_id_t cyclic_add(cyc_handler_t *, cyc_time_t *);
718 extern void cyclic_remove(cyclic_id_t);
719 extern int cyclic_reprogram(cyclic_id_t, hrtime_t);
726 #endif /* _SYS_ZFS_CONTEXT_H */