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 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #ifndef _SYS_ZFS_CONTEXT_H
27 #define _SYS_ZFS_CONTEXT_H
32 #include <sys/types.h>
33 #include <sys/t_lock.h>
34 #include <sys/atomic.h>
35 #include <sys/sysmacros.h>
36 #include <sys/bitmap.h>
37 #include <sys/cmn_err.h>
39 #include <sys/taskq.h>
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/cpuvar.h>
47 #include <sys/debug.h>
48 #include <sys/random.h>
49 #include <sys/byteorder.h>
50 #include <sys/systm.h>
52 #include <sys/uio_impl.h>
53 #include <sys/dirent.h>
55 #include <vm/seg_kmem.h>
57 #include <sys/zfs_debug.h>
58 #include <sys/fm/fs/zfs.h>
59 #include <sys/sunddi.h>
65 #define _SYS_CONDVAR_H
92 #include <sys/types.h>
94 #include <sys/sysmacros.h>
95 #include <sys/bitmap.h>
96 #include <sys/resource.h>
97 #include <sys/byteorder.h>
100 #include <sys/zfs_debug.h>
102 #include <sys/kstat.h>
103 #include <sys/u8_textprep.h>
104 #include <sys/fm/fs/zfs.h>
105 #include <sys/sunddi.h>
111 #define noinline __attribute__((noinline))
118 * Note that we are not using the debugging levels.
121 #define CE_CONT 0 /* continuation */
122 #define CE_NOTE 1 /* notice */
123 #define CE_WARN 2 /* warning */
124 #define CE_PANIC 3 /* panic */
125 #define CE_IGNORE 4 /* print nothing */
134 extern void dprintf_setup(int *argc
, char **argv
);
135 #endif /* ZFS_DEBUG */
137 extern void cmn_err(int, const char *, ...);
138 extern void vcmn_err(int, const char *, __va_list
);
139 extern void panic(const char *, ...);
140 extern void vpanic(const char *, __va_list
);
142 #define fm_panic panic
145 * DTrace SDT probes have different signatures in userland than they do in
146 * kernel. If they're being used in kernel code, re-define them out of
147 * existence for their counterparts in libzpool.
152 #define DTRACE_PROBE(a) ((void)0)
153 #endif /* DTRACE_PROBE */
157 #define DTRACE_PROBE1(a, b, c) ((void)0)
158 #endif /* DTRACE_PROBE1 */
162 #define DTRACE_PROBE2(a, b, c, d, e) ((void)0)
163 #endif /* DTRACE_PROBE2 */
167 #define DTRACE_PROBE3(a, b, c, d, e, f, g) ((void)0)
168 #endif /* DTRACE_PROBE3 */
172 #define DTRACE_PROBE4(a, b, c, d, e, f, g, h, i) ((void)0)
173 #endif /* DTRACE_PROBE4 */
178 #define TS_MAGIC 0x72f158ab4261e538ull
179 #define TS_RUN 0x00000002
181 #define STACK_SIZE 8192 /* Linux x86 and amd64 */
183 #define STACK_SIZE 24576 /* Solaris */
186 #ifdef NPTL_GUARD_WITHIN_STACK
187 #define EXTRA_GUARD_BYTES PAGESIZE
189 #define EXTRA_GUARD_BYTES 0
192 /* in libzpool, p0 exists only to have its address taken */
193 typedef struct proc
{
194 uintptr_t this_is_never_used_dont_dereference_it
;
197 extern struct proc p0
;
199 typedef void (*thread_func_t
)(void *);
200 typedef void (*thread_func_arg_t
)(void *);
201 typedef pthread_t kt_did_t
;
203 typedef struct kthread
{
205 thread_func_t t_func
;
209 #define tsd_get(key) pthread_getspecific(key)
210 #define tsd_set(key, val) pthread_setspecific(key, val)
211 #define curthread zk_thread_current()
212 #define thread_exit zk_thread_exit
213 #define thread_create(stk, stksize, func, arg, len, pp, state, pri) \
214 zk_thread_create(stk, stksize, (thread_func_t)func, arg, \
215 len, NULL, state, pri)
216 #define thread_join(t) zk_thread_join(t)
217 #define newproc(f,a,cid,pri,ctp,pid) (ENOSYS)
219 extern kthread_t
*zk_thread_current(void);
220 extern void zk_thread_exit(void);
221 extern kthread_t
*zk_thread_create(caddr_t stk
, size_t stksize
,
222 thread_func_t func
, void *arg
, size_t len
,
223 proc_t
*pp
, int state
, pri_t pri
);
224 extern void zk_thread_join(kt_did_t tid
);
228 #define issig(why) (FALSE)
229 #define ISSIG(thr, why) (FALSE)
234 #define MTX_MAGIC 0x9522f51362a6e326ull
235 #define MTX_INIT ((void *)NULL)
236 #define MTX_DEST ((void *)-1UL)
238 typedef struct kmutex
{
241 pthread_mutex_t m_lock
;
244 #define MUTEX_DEFAULT 0
245 #define MUTEX_HELD(m) ((m)->m_owner == curthread)
246 #define MUTEX_NOT_HELD(m) (!MUTEX_HELD(m))
248 extern void mutex_init(kmutex_t
*mp
, char *name
, int type
, void *cookie
);
249 extern void mutex_destroy(kmutex_t
*mp
);
250 extern void mutex_enter(kmutex_t
*mp
);
251 extern void mutex_exit(kmutex_t
*mp
);
252 extern int mutex_tryenter(kmutex_t
*mp
);
253 extern void *mutex_owner(kmutex_t
*mp
);
254 extern int mutex_held(kmutex_t
*mp
);
259 #define RW_MAGIC 0x4d31fb123648e78aull
260 #define RW_INIT ((void *)NULL)
261 #define RW_DEST ((void *)-1UL)
263 typedef struct krwlock
{
267 pthread_rwlock_t rw_lock
;
275 #define RW_DEFAULT RW_READER
277 #define RW_READ_HELD(x) ((x)->rw_readers > 0)
278 #define RW_WRITE_HELD(x) ((x)->rw_wr_owner == curthread)
279 #define RW_LOCK_HELD(x) (RW_READ_HELD(x) || RW_WRITE_HELD(x))
281 extern void rw_init(krwlock_t
*rwlp
, char *name
, int type
, void *arg
);
282 extern void rw_destroy(krwlock_t
*rwlp
);
283 extern void rw_enter(krwlock_t
*rwlp
, krw_t rw
);
284 extern int rw_tryenter(krwlock_t
*rwlp
, krw_t rw
);
285 extern int rw_tryupgrade(krwlock_t
*rwlp
);
286 extern void rw_exit(krwlock_t
*rwlp
);
287 #define rw_downgrade(rwlp) do { } while (0)
289 extern uid_t
crgetuid(cred_t
*cr
);
290 extern gid_t
crgetgid(cred_t
*cr
);
291 extern int crgetngroups(cred_t
*cr
);
292 extern gid_t
*crgetgroups(cred_t
*cr
);
295 * Condition variables
297 #define CV_MAGIC 0xd31ea9a83b1b30c4ull
299 typedef struct kcondvar
{
306 extern void cv_init(kcondvar_t
*cv
, char *name
, int type
, void *arg
);
307 extern void cv_destroy(kcondvar_t
*cv
);
308 extern void cv_wait(kcondvar_t
*cv
, kmutex_t
*mp
);
309 extern clock_t cv_timedwait(kcondvar_t
*cv
, kmutex_t
*mp
, clock_t abstime
);
310 extern void cv_signal(kcondvar_t
*cv
);
311 extern void cv_broadcast(kcondvar_t
*cv
);
312 #define cv_timedwait_interruptible(cv, mp, at) cv_timedwait(cv, mp, at);
315 * kstat creation, installation and deletion
317 extern kstat_t
*kstat_create(char *, int,
318 char *, char *, uchar_t
, ulong_t
, uchar_t
);
319 extern void kstat_install(kstat_t
*);
320 extern void kstat_delete(kstat_t
*);
325 #define KM_SLEEP UMEM_NOFAIL
326 #define KM_PUSHPAGE KM_SLEEP
327 #define KM_NOSLEEP UMEM_DEFAULT
328 #define KM_NODEBUG 0x0
329 #define KMC_NODEBUG UMC_NODEBUG
330 #define kmem_alloc(_s, _f) umem_alloc(_s, _f)
331 #define kmem_zalloc(_s, _f) umem_zalloc(_s, _f)
332 #define kmem_free(_b, _s) umem_free(_b, _s)
333 #define vmem_alloc(_s, _f) kmem_alloc(_s, _f)
334 #define vmem_zalloc(_s, _f) kmem_zalloc(_s, _f)
335 #define vmem_free(_b, _s) kmem_free(_b, _s)
336 #define kmem_cache_create(_a, _b, _c, _d, _e, _f, _g, _h, _i) \
337 umem_cache_create(_a, _b, _c, _d, _e, _f, _g, _h, _i)
338 #define kmem_cache_destroy(_c) umem_cache_destroy(_c)
339 #define kmem_cache_alloc(_c, _f) umem_cache_alloc(_c, _f)
340 #define kmem_cache_free(_c, _b) umem_cache_free(_c, _b)
341 #define kmem_debugging() 0
342 #define kmem_cache_reap_now(_c) /* nothing */
343 #define kmem_cache_set_move(_c, _cb) /* nothing */
344 #define POINTER_INVALIDATE(_pp) /* nothing */
345 #define POINTER_IS_VALID(_p) 0
347 typedef umem_cache_t kmem_cache_t
;
349 typedef enum kmem_cbrc
{
360 typedef struct taskq taskq_t
;
361 typedef uintptr_t taskqid_t
;
362 typedef void (task_func_t
)(void *);
364 #define TASKQ_PREPOPULATE 0x0001
365 #define TASKQ_CPR_SAFE 0x0002 /* Use CPR safe protocol */
366 #define TASKQ_DYNAMIC 0x0004 /* Use dynamic thread scheduling */
367 #define TASKQ_THREADS_CPU_PCT 0x0008 /* Scale # threads by # cpus */
368 #define TASKQ_DC_BATCH 0x0010 /* Mark threads as batch */
370 #define TQ_SLEEP KM_SLEEP /* Can block for memory */
371 #define TQ_NOSLEEP KM_NOSLEEP /* cannot block for memory; may fail */
372 #define TQ_NOQUEUE 0x02 /* Do not enqueue if can't dispatch */
373 #define TQ_FRONT 0x08 /* Queue in front */
375 extern taskq_t
*system_taskq
;
377 extern taskq_t
*taskq_create(const char *, int, pri_t
, int, int, uint_t
);
378 #define taskq_create_proc(a, b, c, d, e, p, f) \
379 (taskq_create(a, b, c, d, e, f))
380 #define taskq_create_sysdc(a, b, d, e, p, dc, f) \
381 (taskq_create(a, b, maxclsyspri, d, e, f))
382 extern taskqid_t
taskq_dispatch(taskq_t
*, task_func_t
, void *, uint_t
);
383 extern void taskq_destroy(taskq_t
*);
384 extern void taskq_wait(taskq_t
*);
385 extern int taskq_member(taskq_t
*, kthread_t
*);
386 extern void system_taskq_init(void);
387 extern void system_taskq_fini(void);
389 #define XVA_MAPSIZE 3
390 #define XVA_MAGIC 0x78766174
395 typedef struct vnode
{
401 #define AV_SCANSTAMP_SZ 32 /* length of anti-virus scanstamp */
403 typedef struct xoptattr
{
404 timestruc_t xoa_createtime
; /* Create time of file */
407 uint8_t xoa_readonly
;
409 uint8_t xoa_nounlink
;
410 uint8_t xoa_immutable
;
411 uint8_t xoa_appendonly
;
413 uint8_t xoa_settable
;
415 uint8_t xoa_av_quarantined
;
416 uint8_t xoa_av_modified
;
417 uint8_t xoa_av_scanstamp
[AV_SCANSTAMP_SZ
];
423 typedef struct vattr
{
424 uint_t va_mask
; /* bit-mask of attributes */
425 u_offset_t va_size
; /* file size in bytes */
429 typedef struct xvattr
{
430 vattr_t xva_vattr
; /* Embedded vattr structure */
431 uint32_t xva_magic
; /* Magic Number */
432 uint32_t xva_mapsize
; /* Size of attr bitmap (32-bit words) */
433 uint32_t *xva_rtnattrmapp
; /* Ptr to xva_rtnattrmap[] */
434 uint32_t xva_reqattrmap
[XVA_MAPSIZE
]; /* Requested attrs */
435 uint32_t xva_rtnattrmap
[XVA_MAPSIZE
]; /* Returned attrs */
436 xoptattr_t xva_xoptattrs
; /* Optional attributes */
439 typedef struct vsecattr
{
440 uint_t vsa_mask
; /* See below */
441 int vsa_aclcnt
; /* ACL entry count */
442 void *vsa_aclentp
; /* pointer to ACL entries */
443 int vsa_dfaclcnt
; /* default ACL entry count */
444 void *vsa_dfaclentp
; /* pointer to default ACL entries */
445 size_t vsa_aclentsz
; /* ACE size in bytes of vsa_aclentp */
448 #define AT_TYPE 0x00001
449 #define AT_MODE 0x00002
450 #define AT_UID 0x00004
451 #define AT_GID 0x00008
452 #define AT_FSID 0x00010
453 #define AT_NODEID 0x00020
454 #define AT_NLINK 0x00040
455 #define AT_SIZE 0x00080
456 #define AT_ATIME 0x00100
457 #define AT_MTIME 0x00200
458 #define AT_CTIME 0x00400
459 #define AT_RDEV 0x00800
460 #define AT_BLKSIZE 0x01000
461 #define AT_NBLOCKS 0x02000
462 #define AT_SEQ 0x08000
463 #define AT_XVATTR 0x10000
467 extern int fop_getattr(vnode_t
*vp
, vattr_t
*vap
);
469 #define VOP_CLOSE(vp, f, c, o, cr, ct) 0
470 #define VOP_PUTPAGE(vp, of, sz, fl, cr, ct) 0
471 #define VOP_GETATTR(vp, vap, fl, cr, ct) fop_getattr((vp), (vap));
473 #define VOP_FSYNC(vp, f, cr, ct) fsync((vp)->v_fd)
475 #define VN_RELE(vp) vn_close(vp)
477 extern int vn_open(char *path
, int x1
, int oflags
, int mode
, vnode_t
**vpp
,
479 extern int vn_openat(char *path
, int x1
, int oflags
, int mode
, vnode_t
**vpp
,
480 int x2
, int x3
, vnode_t
*vp
, int fd
);
481 extern int vn_rdwr(int uio
, vnode_t
*vp
, void *addr
, ssize_t len
,
482 offset_t offset
, int x1
, int x2
, rlim64_t x3
, void *x4
, ssize_t
*residp
);
483 extern void vn_close(vnode_t
*vp
);
485 #define vn_remove(path, x1, x2) remove(path)
486 #define vn_rename(from, to, seg) rename((from), (to))
487 #define vn_is_readonly(vp) B_FALSE
489 extern vnode_t
*rootdir
;
491 #include <sys/file.h> /* for FREAD, FWRITE, etc */
496 #define ddi_get_lbolt() (gethrtime() >> 23)
497 #define ddi_get_lbolt64() (gethrtime() >> 23)
498 #define hz 119 /* frequency when using gethrtime() >> 23 for lbolt */
500 extern void delay(clock_t ticks
);
502 #define gethrestime_sec() time(NULL)
503 #define gethrestime(t) \
505 (t)->tv_sec = gethrestime_sec();\
511 #define minclsyspri 60
512 #define maxclsyspri 99
514 #define CPU_SEQID (pthread_self() & (max_ncpus - 1))
519 #define ptob(x) ((x) * PAGESIZE)
521 extern uint64_t physmem
;
523 extern int highbit(ulong_t i
);
524 extern int random_get_bytes(uint8_t *ptr
, size_t len
);
525 extern int random_get_pseudo_bytes(uint8_t *ptr
, size_t len
);
527 extern void kernel_init(int);
528 extern void kernel_fini(void);
531 extern void nicenum(uint64_t num
, char *buf
);
532 extern void show_pool_stats(struct spa
*);
534 typedef struct callb_cpr
{
538 #define CALLB_CPR_INIT(cp, lockp, func, name) { \
539 (cp)->cc_lockp = lockp; \
542 #define CALLB_CPR_SAFE_BEGIN(cp) { \
543 ASSERT(MUTEX_HELD((cp)->cc_lockp)); \
546 #define CALLB_CPR_SAFE_END(cp, lockp) { \
547 ASSERT(MUTEX_HELD((cp)->cc_lockp)); \
550 #define CALLB_CPR_EXIT(cp) { \
551 ASSERT(MUTEX_HELD((cp)->cc_lockp)); \
552 mutex_exit((cp)->cc_lockp); \
555 #define zone_dataset_visible(x, y) (1)
556 #define INGLOBALZONE(z) (1)
558 extern char *kmem_vasprintf(const char *fmt
, va_list adx
);
559 extern char *kmem_asprintf(const char *fmt
, ...);
560 #define strfree(str) kmem_free((str), strlen(str)+1)
563 * Hostname information
565 extern char hw_serial
[]; /* for userland-emulated hostid access */
566 extern int ddi_strtoul(const char *str
, char **nptr
, int base
,
567 unsigned long *result
);
569 extern int ddi_strtoull(const char *str
, char **nptr
, int base
,
570 u_longlong_t
*result
);
572 /* ZFS Boot Related stuff. */
582 typedef struct ace_object
{
584 uint32_t a_access_mask
;
587 uint8_t a_obj_type
[16];
588 uint8_t a_inherit_obj_type
[16];
592 #define ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE 0x05
593 #define ACE_ACCESS_DENIED_OBJECT_ACE_TYPE 0x06
594 #define ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE 0x07
595 #define ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE 0x08
597 extern struct _buf
*kobj_open_file(char *name
);
598 extern int kobj_read_file(struct _buf
*file
, char *buf
, unsigned size
,
600 extern void kobj_close_file(struct _buf
*file
);
601 extern int kobj_get_filesize(struct _buf
*file
, uint64_t *size
);
602 extern int zfs_secpolicy_snapshot_perms(const char *name
, cred_t
*cr
);
603 extern int zfs_secpolicy_rename_perms(const char *from
, const char *to
,
605 extern int zfs_secpolicy_destroy_perms(const char *name
, cred_t
*cr
);
606 extern zoneid_t
getzoneid(void);
609 typedef struct ksiddomain
{
615 ksiddomain_t
*ksid_lookupdomain(const char *);
616 void ksiddomain_rele(ksiddomain_t
*);
618 #define DDI_SLEEP KM_SLEEP
619 #define ddi_log_sysevent(_a, _b, _c, _d, _e, _f, _g) \
620 sysevent_post_event(_c, _d, _b, "libzpool", _e, _f)
624 #endif /* _SYS_ZFS_CONTEXT_H */