]>
Commit | Line | Data |
---|---|---|
77755a57 BB |
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 | /* | |
6283f55e BB |
22 | * Copyright 2009 Sun Microsystems, Inc. All rights reserved. |
23 | * Use is subject to license terms. | |
77755a57 | 24 | */ |
a38718a6 GA |
25 | /* |
26 | * Copyright 2011 Nexenta Systems, Inc. All rights reserved. | |
37abac6d | 27 | * Copyright (c) 2012, Joyent, Inc. All rights reserved. |
206971d2 | 28 | * Copyright (c) 2012, 2015 by Delphix. All rights reserved. |
a38718a6 | 29 | */ |
77755a57 BB |
30 | |
31 | #ifndef _SYS_ZFS_CONTEXT_H | |
32 | #define _SYS_ZFS_CONTEXT_H | |
33 | ||
e37e1d30 | 34 | #ifdef __KERNEL__ |
6283f55e BB |
35 | |
36 | #include <sys/note.h> | |
37 | #include <sys/types.h> | |
38 | #include <sys/t_lock.h> | |
39 | #include <sys/atomic.h> | |
40 | #include <sys/sysmacros.h> | |
41 | #include <sys/bitmap.h> | |
42 | #include <sys/cmn_err.h> | |
43 | #include <sys/kmem.h> | |
60e1eda9 | 44 | #include <sys/kmem_cache.h> |
efcd79a8 | 45 | #include <sys/vmem.h> |
6283f55e BB |
46 | #include <sys/taskq.h> |
47 | #include <sys/buf.h> | |
48 | #include <sys/param.h> | |
49 | #include <sys/systm.h> | |
50 | #include <sys/cpuvar.h> | |
51 | #include <sys/kobj.h> | |
52 | #include <sys/conf.h> | |
53 | #include <sys/disp.h> | |
54 | #include <sys/debug.h> | |
55 | #include <sys/random.h> | |
56 | #include <sys/byteorder.h> | |
57 | #include <sys/systm.h> | |
58 | #include <sys/list.h> | |
590329b5 | 59 | #include <sys/uio_impl.h> |
6283f55e BB |
60 | #include <sys/dirent.h> |
61 | #include <sys/time.h> | |
62 | #include <vm/seg_kmem.h> | |
63 | #include <sys/zone.h> | |
2e528b49 | 64 | #include <sys/sdt.h> |
0b04990a | 65 | #include <sys/kstat.h> |
6283f55e | 66 | #include <sys/zfs_debug.h> |
fb390aaf HR |
67 | #include <sys/sysevent.h> |
68 | #include <sys/sysevent/eventdefs.h> | |
e8b96c60 | 69 | #include <sys/zfs_delay.h> |
6283f55e | 70 | #include <sys/sunddi.h> |
d96eb2b1 | 71 | #include <sys/ctype.h> |
15a9e033 | 72 | #include <sys/disp.h> |
0b39b9f9 | 73 | #include <sys/trace.h> |
8299a1f4 | 74 | #include <linux/dcache_compat.h> |
f0e324f2 | 75 | #include <linux/utsname_compat.h> |
6283f55e BB |
76 | |
77 | #else /* _KERNEL */ | |
77755a57 BB |
78 | |
79 | #define _SYS_MUTEX_H | |
80 | #define _SYS_RWLOCK_H | |
81 | #define _SYS_CONDVAR_H | |
82 | #define _SYS_SYSTM_H | |
77755a57 BB |
83 | #define _SYS_T_LOCK_H |
84 | #define _SYS_VNODE_H | |
85 | #define _SYS_VFS_H | |
86 | #define _SYS_SUNDDI_H | |
87 | #define _SYS_CALLB_H | |
88 | ||
89 | #include <stdio.h> | |
90 | #include <stdlib.h> | |
91 | #include <stddef.h> | |
92 | #include <stdarg.h> | |
93 | #include <fcntl.h> | |
94 | #include <unistd.h> | |
95 | #include <errno.h> | |
96 | #include <string.h> | |
97 | #include <strings.h> | |
1e33ac1e | 98 | #include <pthread.h> |
3af3df90 | 99 | #include <synch.h> |
77755a57 BB |
100 | #include <assert.h> |
101 | #include <alloca.h> | |
102 | #include <umem.h> | |
103 | #include <limits.h> | |
104 | #include <atomic.h> | |
105 | #include <dirent.h> | |
106 | #include <time.h> | |
d96eb2b1 | 107 | #include <ctype.h> |
498877ba MA |
108 | #include <signal.h> |
109 | #include <sys/mman.h> | |
77755a57 BB |
110 | #include <sys/note.h> |
111 | #include <sys/types.h> | |
112 | #include <sys/cred.h> | |
113 | #include <sys/sysmacros.h> | |
114 | #include <sys/bitmap.h> | |
115 | #include <sys/resource.h> | |
116 | #include <sys/byteorder.h> | |
117 | #include <sys/list.h> | |
118 | #include <sys/uio.h> | |
119 | #include <sys/zfs_debug.h> | |
120 | #include <sys/sdt.h> | |
121 | #include <sys/kstat.h> | |
122 | #include <sys/u8_textprep.h> | |
fb390aaf HR |
123 | #include <sys/sysevent.h> |
124 | #include <sys/sysevent/eventdefs.h> | |
428870ff | 125 | #include <sys/sunddi.h> |
c99c9001 | 126 | #include <sys/debug.h> |
f0e324f2 | 127 | #include <sys/utsname.h> |
77755a57 | 128 | |
60948de1 BB |
129 | /* |
130 | * Stack | |
131 | */ | |
132 | ||
2e528b49 | 133 | #define noinline __attribute__((noinline)) |
ee36c709 | 134 | #define likely(x) __builtin_expect((x), 1) |
60948de1 | 135 | |
77755a57 BB |
136 | /* |
137 | * Debugging | |
138 | */ | |
139 | ||
140 | /* | |
141 | * Note that we are not using the debugging levels. | |
142 | */ | |
143 | ||
144 | #define CE_CONT 0 /* continuation */ | |
145 | #define CE_NOTE 1 /* notice */ | |
146 | #define CE_WARN 2 /* warning */ | |
147 | #define CE_PANIC 3 /* panic */ | |
148 | #define CE_IGNORE 4 /* print nothing */ | |
149 | ||
150 | /* | |
151 | * ZFS debugging | |
152 | */ | |
153 | ||
77755a57 | 154 | extern void dprintf_setup(int *argc, char **argv); |
0b39b9f9 | 155 | |
77755a57 | 156 | extern void cmn_err(int, const char *, ...); |
22a11a5b | 157 | extern void vcmn_err(int, const char *, va_list); |
77755a57 | 158 | extern void panic(const char *, ...); |
22a11a5b | 159 | extern void vpanic(const char *, va_list); |
77755a57 BB |
160 | |
161 | #define fm_panic panic | |
162 | ||
0b39b9f9 PS |
163 | extern int aok; |
164 | ||
77755a57 BB |
165 | /* |
166 | * DTrace SDT probes have different signatures in userland than they do in | |
fcff0f35 | 167 | * the kernel. If they're being used in kernel code, re-define them out of |
77755a57 | 168 | * existence for their counterparts in libzpool. |
fcff0f35 PD |
169 | * |
170 | * Here's an example of how to use the set-error probes in userland: | |
171 | * zfs$target:::set-error /arg0 == EBUSY/ {stack();} | |
172 | * | |
173 | * Here's an example of how to use DTRACE_PROBE probes in userland: | |
174 | * If there is a probe declared as follows: | |
175 | * DTRACE_PROBE2(zfs__probe_name, uint64_t, blkid, dnode_t *, dn); | |
176 | * Then you can use it as follows: | |
177 | * zfs$target:::probe2 /copyinstr(arg0) == "zfs__probe_name"/ | |
178 | * {printf("%u %p\n", arg1, arg2);} | |
77755a57 BB |
179 | */ |
180 | ||
181 | #ifdef DTRACE_PROBE | |
182 | #undef DTRACE_PROBE | |
77755a57 | 183 | #endif /* DTRACE_PROBE */ |
2e528b49 MA |
184 | #define DTRACE_PROBE(a) \ |
185 | ZFS_PROBE0(#a) | |
77755a57 BB |
186 | |
187 | #ifdef DTRACE_PROBE1 | |
188 | #undef DTRACE_PROBE1 | |
77755a57 | 189 | #endif /* DTRACE_PROBE1 */ |
2e528b49 MA |
190 | #define DTRACE_PROBE1(a, b, c) \ |
191 | ZFS_PROBE1(#a, (unsigned long)c) | |
77755a57 BB |
192 | |
193 | #ifdef DTRACE_PROBE2 | |
194 | #undef DTRACE_PROBE2 | |
77755a57 | 195 | #endif /* DTRACE_PROBE2 */ |
2e528b49 MA |
196 | #define DTRACE_PROBE2(a, b, c, d, e) \ |
197 | ZFS_PROBE2(#a, (unsigned long)c, (unsigned long)e) | |
77755a57 BB |
198 | |
199 | #ifdef DTRACE_PROBE3 | |
200 | #undef DTRACE_PROBE3 | |
77755a57 | 201 | #endif /* DTRACE_PROBE3 */ |
2e528b49 MA |
202 | #define DTRACE_PROBE3(a, b, c, d, e, f, g) \ |
203 | ZFS_PROBE3(#a, (unsigned long)c, (unsigned long)e, (unsigned long)g) | |
77755a57 BB |
204 | |
205 | #ifdef DTRACE_PROBE4 | |
206 | #undef DTRACE_PROBE4 | |
77755a57 | 207 | #endif /* DTRACE_PROBE4 */ |
2e528b49 MA |
208 | #define DTRACE_PROBE4(a, b, c, d, e, f, g, h, i) \ |
209 | ZFS_PROBE4(#a, (unsigned long)c, (unsigned long)e, (unsigned long)g, \ | |
210 | (unsigned long)i) | |
77755a57 | 211 | |
2e528b49 MA |
212 | /* |
213 | * We use the comma operator so that this macro can be used without much | |
214 | * additional code. For example, "return (EINVAL);" becomes | |
215 | * "return (SET_ERROR(EINVAL));". Note that the argument will be evaluated | |
216 | * twice, so it should not have side effects (e.g. something like: | |
217 | * "return (SET_ERROR(log_error(EINVAL, info)));" would log the error twice). | |
218 | */ | |
219 | #define SET_ERROR(err) (ZFS_SET_ERROR(err), err) | |
aa0ac7ca | 220 | |
77755a57 | 221 | /* |
aa0ac7ca BB |
222 | * Threads. TS_STACK_MIN is dictated by the minimum allowed pthread stack |
223 | * size. While TS_STACK_MAX is somewhat arbitrary, it was selected to be | |
224 | * large enough for the expected stack depth while small enough to avoid | |
225 | * exhausting address space with high thread counts. | |
77755a57 | 226 | */ |
1e33ac1e BB |
227 | #define TS_MAGIC 0x72f158ab4261e538ull |
228 | #define TS_RUN 0x00000002 | |
89666a8e | 229 | #define TS_STACK_MIN MAX(PTHREAD_STACK_MIN, 32768) |
aa0ac7ca | 230 | #define TS_STACK_MAX (256 * 1024) |
428870ff | 231 | |
428870ff | 232 | /* in libzpool, p0 exists only to have its address taken */ |
1e33ac1e | 233 | typedef struct proc { |
428870ff | 234 | uintptr_t this_is_never_used_dont_dereference_it; |
1e33ac1e | 235 | } proc_t; |
428870ff BB |
236 | |
237 | extern struct proc p0; | |
37abac6d | 238 | #define curproc (&p0) |
428870ff | 239 | |
1e33ac1e BB |
240 | typedef void (*thread_func_t)(void *); |
241 | typedef void (*thread_func_arg_t)(void *); | |
242 | typedef pthread_t kt_did_t; | |
243 | ||
d1d7e268 | 244 | #define kpreempt(x) ((void)0) |
e8b96c60 | 245 | |
1e33ac1e BB |
246 | typedef struct kthread { |
247 | kt_did_t t_tid; | |
248 | thread_func_t t_func; | |
249 | void * t_arg; | |
1229323d | 250 | pri_t t_pri; |
1e33ac1e | 251 | } kthread_t; |
77755a57 | 252 | |
1e33ac1e | 253 | #define curthread zk_thread_current() |
1421c891 | 254 | #define getcomm() "unknown" |
1e33ac1e BB |
255 | #define thread_exit zk_thread_exit |
256 | #define thread_create(stk, stksize, func, arg, len, pp, state, pri) \ | |
257 | zk_thread_create(stk, stksize, (thread_func_t)func, arg, \ | |
2e528b49 | 258 | len, NULL, state, pri, PTHREAD_CREATE_DETACHED) |
1e33ac1e | 259 | #define thread_join(t) zk_thread_join(t) |
2e528b49 | 260 | #define newproc(f, a, cid, pri, ctp, pid) (ENOSYS) |
1e33ac1e BB |
261 | |
262 | extern kthread_t *zk_thread_current(void); | |
263 | extern void zk_thread_exit(void); | |
264 | extern kthread_t *zk_thread_create(caddr_t stk, size_t stksize, | |
e2a65adb | 265 | thread_func_t func, void *arg, uint64_t len, |
0aebd4f9 | 266 | proc_t *pp, int state, pri_t pri, int detachstate); |
1e33ac1e BB |
267 | extern void zk_thread_join(kt_did_t tid); |
268 | ||
15a9e033 PS |
269 | #define kpreempt_disable() ((void)0) |
270 | #define kpreempt_enable() ((void)0) | |
271 | ||
1e33ac1e | 272 | #define PS_NONE -1 |
77755a57 BB |
273 | |
274 | #define issig(why) (FALSE) | |
275 | #define ISSIG(thr, why) (FALSE) | |
276 | ||
277 | /* | |
278 | * Mutexes | |
279 | */ | |
1e33ac1e BB |
280 | #define MTX_MAGIC 0x9522f51362a6e326ull |
281 | #define MTX_INIT ((void *)NULL) | |
282 | #define MTX_DEST ((void *)-1UL) | |
283 | ||
77755a57 BB |
284 | typedef struct kmutex { |
285 | void *m_owner; | |
1e33ac1e BB |
286 | uint64_t m_magic; |
287 | pthread_mutex_t m_lock; | |
77755a57 BB |
288 | } kmutex_t; |
289 | ||
1e33ac1e | 290 | #define MUTEX_DEFAULT 0 |
e0553a74 | 291 | #define MUTEX_NOLOCKDEP MUTEX_DEFAULT |
2e528b49 | 292 | #define MUTEX_HELD(m) ((m)->m_owner == curthread) |
572e2857 | 293 | #define MUTEX_NOT_HELD(m) (!MUTEX_HELD(m)) |
77755a57 | 294 | |
1e33ac1e BB |
295 | extern void mutex_init(kmutex_t *mp, char *name, int type, void *cookie); |
296 | extern void mutex_destroy(kmutex_t *mp); | |
77755a57 BB |
297 | extern void mutex_enter(kmutex_t *mp); |
298 | extern void mutex_exit(kmutex_t *mp); | |
299 | extern int mutex_tryenter(kmutex_t *mp); | |
300 | extern void *mutex_owner(kmutex_t *mp); | |
1e33ac1e | 301 | extern int mutex_held(kmutex_t *mp); |
77755a57 BB |
302 | |
303 | /* | |
304 | * RW locks | |
305 | */ | |
1e33ac1e BB |
306 | #define RW_MAGIC 0x4d31fb123648e78aull |
307 | #define RW_INIT ((void *)NULL) | |
308 | #define RW_DEST ((void *)-1UL) | |
309 | ||
77755a57 | 310 | typedef struct krwlock { |
1e33ac1e BB |
311 | void *rw_owner; |
312 | void *rw_wr_owner; | |
313 | uint64_t rw_magic; | |
314 | pthread_rwlock_t rw_lock; | |
315 | uint_t rw_readers; | |
77755a57 BB |
316 | } krwlock_t; |
317 | ||
318 | typedef int krw_t; | |
319 | ||
320 | #define RW_READER 0 | |
321 | #define RW_WRITER 1 | |
2e528b49 | 322 | #define RW_DEFAULT RW_READER |
e0553a74 | 323 | #define RW_NOLOCKDEP RW_READER |
77755a57 | 324 | |
1e33ac1e BB |
325 | #define RW_READ_HELD(x) ((x)->rw_readers > 0) |
326 | #define RW_WRITE_HELD(x) ((x)->rw_wr_owner == curthread) | |
327 | #define RW_LOCK_HELD(x) (RW_READ_HELD(x) || RW_WRITE_HELD(x)) | |
77755a57 | 328 | |
13fe0198 MA |
329 | #undef RW_LOCK_HELD |
330 | #define RW_LOCK_HELD(x) (RW_READ_HELD(x) || RW_WRITE_HELD(x)) | |
331 | ||
332 | #undef RW_LOCK_HELD | |
333 | #define RW_LOCK_HELD(x) (RW_READ_HELD(x) || RW_WRITE_HELD(x)) | |
334 | ||
77755a57 BB |
335 | extern void rw_init(krwlock_t *rwlp, char *name, int type, void *arg); |
336 | extern void rw_destroy(krwlock_t *rwlp); | |
337 | extern void rw_enter(krwlock_t *rwlp, krw_t rw); | |
338 | extern int rw_tryenter(krwlock_t *rwlp, krw_t rw); | |
339 | extern int rw_tryupgrade(krwlock_t *rwlp); | |
340 | extern void rw_exit(krwlock_t *rwlp); | |
341 | #define rw_downgrade(rwlp) do { } while (0) | |
342 | ||
343 | extern uid_t crgetuid(cred_t *cr); | |
6f1ffb06 | 344 | extern uid_t crgetruid(cred_t *cr); |
77755a57 BB |
345 | extern gid_t crgetgid(cred_t *cr); |
346 | extern int crgetngroups(cred_t *cr); | |
347 | extern gid_t *crgetgroups(cred_t *cr); | |
348 | ||
349 | /* | |
350 | * Condition variables | |
351 | */ | |
2e528b49 | 352 | #define CV_MAGIC 0xd31ea9a83b1b30c4ull |
1e33ac1e BB |
353 | |
354 | typedef struct kcondvar { | |
355 | uint64_t cv_magic; | |
356 | pthread_cond_t cv; | |
357 | } kcondvar_t; | |
77755a57 | 358 | |
1e33ac1e | 359 | #define CV_DEFAULT 0 |
206971d2 | 360 | #define CALLOUT_FLAG_ABSOLUTE 0x2 |
77755a57 BB |
361 | |
362 | extern void cv_init(kcondvar_t *cv, char *name, int type, void *arg); | |
363 | extern void cv_destroy(kcondvar_t *cv); | |
364 | extern void cv_wait(kcondvar_t *cv, kmutex_t *mp); | |
365 | extern clock_t cv_timedwait(kcondvar_t *cv, kmutex_t *mp, clock_t abstime); | |
63fd3c6c AL |
366 | extern clock_t cv_timedwait_hires(kcondvar_t *cvp, kmutex_t *mp, hrtime_t tim, |
367 | hrtime_t res, int flag); | |
77755a57 BB |
368 | extern void cv_signal(kcondvar_t *cv); |
369 | extern void cv_broadcast(kcondvar_t *cv); | |
b64ccd6c BB |
370 | #define cv_timedwait_sig(cv, mp, at) cv_timedwait(cv, mp, at) |
371 | #define cv_wait_sig(cv, mp) cv_wait(cv, mp) | |
2e528b49 | 372 | #define cv_wait_io(cv, mp) cv_wait(cv, mp) |
a9bb2b68 CC |
373 | #define cv_timedwait_sig_hires(cv, mp, t, r, f) \ |
374 | cv_timedwait_hires(cv, mp, t, r, f) | |
77755a57 | 375 | |
13fe0198 MA |
376 | /* |
377 | * Thread-specific data | |
378 | */ | |
379 | #define tsd_get(k) pthread_getspecific(k) | |
380 | #define tsd_set(k, v) pthread_setspecific(k, v) | |
381 | #define tsd_create(kp, d) pthread_key_create(kp, d) | |
382 | #define tsd_destroy(kp) /* nothing */ | |
383 | ||
384 | /* | |
385 | * Thread-specific data | |
386 | */ | |
387 | #define tsd_get(k) pthread_getspecific(k) | |
388 | #define tsd_set(k, v) pthread_setspecific(k, v) | |
389 | #define tsd_create(kp, d) pthread_key_create(kp, d) | |
390 | #define tsd_destroy(kp) /* nothing */ | |
391 | ||
77755a57 BB |
392 | /* |
393 | * kstat creation, installation and deletion | |
394 | */ | |
330847ff MA |
395 | extern kstat_t *kstat_create(const char *, int, |
396 | const char *, const char *, uchar_t, ulong_t, uchar_t); | |
4e21fd06 | 397 | extern void kstat_named_init(kstat_named_t *, const char *, uchar_t); |
77755a57 BB |
398 | extern void kstat_install(kstat_t *); |
399 | extern void kstat_delete(kstat_t *); | |
330847ff MA |
400 | extern void kstat_waitq_enter(kstat_io_t *); |
401 | extern void kstat_waitq_exit(kstat_io_t *); | |
402 | extern void kstat_runq_enter(kstat_io_t *); | |
403 | extern void kstat_runq_exit(kstat_io_t *); | |
404 | extern void kstat_waitq_to_runq(kstat_io_t *); | |
405 | extern void kstat_runq_back_to_waitq(kstat_io_t *); | |
1421c891 PS |
406 | extern void kstat_set_raw_ops(kstat_t *ksp, |
407 | int (*headers)(char *buf, size_t size), | |
408 | int (*data)(char *buf, size_t size, void *data), | |
409 | void *(*addr)(kstat_t *ksp, loff_t index)); | |
77755a57 BB |
410 | |
411 | /* | |
412 | * Kernel memory | |
413 | */ | |
414 | #define KM_SLEEP UMEM_NOFAIL | |
415 | #define KM_PUSHPAGE KM_SLEEP | |
416 | #define KM_NOSLEEP UMEM_DEFAULT | |
417 | #define KMC_NODEBUG UMC_NODEBUG | |
ae6ba3db BB |
418 | #define KMC_KMEM 0x0 |
419 | #define KMC_VMEM 0x0 | |
77755a57 BB |
420 | #define kmem_alloc(_s, _f) umem_alloc(_s, _f) |
421 | #define kmem_zalloc(_s, _f) umem_zalloc(_s, _f) | |
422 | #define kmem_free(_b, _s) umem_free(_b, _s) | |
00b46022 BB |
423 | #define vmem_alloc(_s, _f) kmem_alloc(_s, _f) |
424 | #define vmem_zalloc(_s, _f) kmem_zalloc(_s, _f) | |
425 | #define vmem_free(_b, _s) kmem_free(_b, _s) | |
77755a57 BB |
426 | #define kmem_cache_create(_a, _b, _c, _d, _e, _f, _g, _h, _i) \ |
427 | umem_cache_create(_a, _b, _c, _d, _e, _f, _g, _h, _i) | |
428 | #define kmem_cache_destroy(_c) umem_cache_destroy(_c) | |
429 | #define kmem_cache_alloc(_c, _f) umem_cache_alloc(_c, _f) | |
430 | #define kmem_cache_free(_c, _b) umem_cache_free(_c, _b) | |
431 | #define kmem_debugging() 0 | |
669dedb3 | 432 | #define kmem_cache_reap_now(_c) umem_cache_reap_now(_c); |
572e2857 | 433 | #define kmem_cache_set_move(_c, _cb) /* nothing */ |
ca67b33a | 434 | #define vmem_qcache_reap(_v) /* nothing */ |
572e2857 BB |
435 | #define POINTER_INVALIDATE(_pp) /* nothing */ |
436 | #define POINTER_IS_VALID(_p) 0 | |
77755a57 | 437 | |
ca67b33a MA |
438 | extern vmem_t *zio_arena; |
439 | ||
77755a57 BB |
440 | typedef umem_cache_t kmem_cache_t; |
441 | ||
572e2857 BB |
442 | typedef enum kmem_cbrc { |
443 | KMEM_CBRC_YES, | |
444 | KMEM_CBRC_NO, | |
445 | KMEM_CBRC_LATER, | |
446 | KMEM_CBRC_DONT_NEED, | |
447 | KMEM_CBRC_DONT_KNOW | |
448 | } kmem_cbrc_t; | |
449 | ||
77755a57 BB |
450 | /* |
451 | * Task queues | |
452 | */ | |
0b04990a TC |
453 | |
454 | #define TASKQ_NAMELEN 31 | |
455 | ||
77755a57 BB |
456 | typedef uintptr_t taskqid_t; |
457 | typedef void (task_func_t)(void *); | |
458 | ||
a38718a6 GA |
459 | typedef struct taskq_ent { |
460 | struct taskq_ent *tqent_next; | |
461 | struct taskq_ent *tqent_prev; | |
462 | task_func_t *tqent_func; | |
463 | void *tqent_arg; | |
464 | uintptr_t tqent_flags; | |
465 | } taskq_ent_t; | |
466 | ||
0b04990a TC |
467 | typedef struct taskq { |
468 | char tq_name[TASKQ_NAMELEN + 1]; | |
469 | kmutex_t tq_lock; | |
470 | krwlock_t tq_threadlock; | |
471 | kcondvar_t tq_dispatch_cv; | |
472 | kcondvar_t tq_wait_cv; | |
473 | kthread_t **tq_threadlist; | |
474 | int tq_flags; | |
475 | int tq_active; | |
476 | int tq_nthreads; | |
477 | int tq_nalloc; | |
478 | int tq_minalloc; | |
479 | int tq_maxalloc; | |
480 | kcondvar_t tq_maxalloc_cv; | |
481 | int tq_maxalloc_wait; | |
482 | taskq_ent_t *tq_freelist; | |
483 | taskq_ent_t tq_task; | |
484 | } taskq_t; | |
485 | ||
a38718a6 GA |
486 | #define TQENT_FLAG_PREALLOC 0x1 /* taskq_dispatch_ent used */ |
487 | ||
77755a57 BB |
488 | #define TASKQ_PREPOPULATE 0x0001 |
489 | #define TASKQ_CPR_SAFE 0x0002 /* Use CPR safe protocol */ | |
490 | #define TASKQ_DYNAMIC 0x0004 /* Use dynamic thread scheduling */ | |
428870ff BB |
491 | #define TASKQ_THREADS_CPU_PCT 0x0008 /* Scale # threads by # cpus */ |
492 | #define TASKQ_DC_BATCH 0x0010 /* Mark threads as batch */ | |
77755a57 BB |
493 | |
494 | #define TQ_SLEEP KM_SLEEP /* Can block for memory */ | |
495 | #define TQ_NOSLEEP KM_NOSLEEP /* cannot block for memory; may fail */ | |
428870ff BB |
496 | #define TQ_NOQUEUE 0x02 /* Do not enqueue if can't dispatch */ |
497 | #define TQ_FRONT 0x08 /* Queue in front */ | |
77755a57 | 498 | |
48d3eb40 BB |
499 | #define TASKQID_INVALID ((taskqid_t)0) |
500 | ||
77755a57 | 501 | extern taskq_t *system_taskq; |
57ddcda1 | 502 | extern taskq_t *system_delay_taskq; |
77755a57 BB |
503 | |
504 | extern taskq_t *taskq_create(const char *, int, pri_t, int, int, uint_t); | |
428870ff BB |
505 | #define taskq_create_proc(a, b, c, d, e, p, f) \ |
506 | (taskq_create(a, b, c, d, e, f)) | |
507 | #define taskq_create_sysdc(a, b, d, e, p, dc, f) \ | |
508 | (taskq_create(a, b, maxclsyspri, d, e, f)) | |
77755a57 | 509 | extern taskqid_t taskq_dispatch(taskq_t *, task_func_t, void *, uint_t); |
cc92e9d0 GW |
510 | extern taskqid_t taskq_dispatch_delay(taskq_t *, task_func_t, void *, uint_t, |
511 | clock_t); | |
a38718a6 GA |
512 | extern void taskq_dispatch_ent(taskq_t *, task_func_t, void *, uint_t, |
513 | taskq_ent_t *); | |
514 | extern int taskq_empty_ent(taskq_ent_t *); | |
515 | extern void taskq_init_ent(taskq_ent_t *); | |
77755a57 BB |
516 | extern void taskq_destroy(taskq_t *); |
517 | extern void taskq_wait(taskq_t *); | |
044baf00 | 518 | extern void taskq_wait_id(taskq_t *, taskqid_t); |
4f34bd97 | 519 | extern void taskq_wait_outstanding(taskq_t *, taskqid_t); |
1e33ac1e | 520 | extern int taskq_member(taskq_t *, kthread_t *); |
cc92e9d0 | 521 | extern int taskq_cancel_id(taskq_t *, taskqid_t); |
77755a57 | 522 | extern void system_taskq_init(void); |
428870ff | 523 | extern void system_taskq_fini(void); |
77755a57 BB |
524 | |
525 | #define XVA_MAPSIZE 3 | |
526 | #define XVA_MAGIC 0x78766174 | |
527 | ||
528 | /* | |
529 | * vnodes | |
530 | */ | |
531 | typedef struct vnode { | |
532 | uint64_t v_size; | |
533 | int v_fd; | |
534 | char *v_path; | |
9867e8be | 535 | int v_dump_fd; |
77755a57 BB |
536 | } vnode_t; |
537 | ||
9867e8be | 538 | extern char *vn_dumpdir; |
428870ff | 539 | #define AV_SCANSTAMP_SZ 32 /* length of anti-virus scanstamp */ |
77755a57 BB |
540 | |
541 | typedef struct xoptattr { | |
542 | timestruc_t xoa_createtime; /* Create time of file */ | |
543 | uint8_t xoa_archive; | |
544 | uint8_t xoa_system; | |
545 | uint8_t xoa_readonly; | |
546 | uint8_t xoa_hidden; | |
547 | uint8_t xoa_nounlink; | |
548 | uint8_t xoa_immutable; | |
549 | uint8_t xoa_appendonly; | |
550 | uint8_t xoa_nodump; | |
551 | uint8_t xoa_settable; | |
552 | uint8_t xoa_opaque; | |
553 | uint8_t xoa_av_quarantined; | |
554 | uint8_t xoa_av_modified; | |
428870ff BB |
555 | uint8_t xoa_av_scanstamp[AV_SCANSTAMP_SZ]; |
556 | uint8_t xoa_reparse; | |
572e2857 BB |
557 | uint8_t xoa_offline; |
558 | uint8_t xoa_sparse; | |
77755a57 BB |
559 | } xoptattr_t; |
560 | ||
561 | typedef struct vattr { | |
562 | uint_t va_mask; /* bit-mask of attributes */ | |
563 | u_offset_t va_size; /* file size in bytes */ | |
564 | } vattr_t; | |
565 | ||
566 | ||
567 | typedef struct xvattr { | |
568 | vattr_t xva_vattr; /* Embedded vattr structure */ | |
569 | uint32_t xva_magic; /* Magic Number */ | |
570 | uint32_t xva_mapsize; /* Size of attr bitmap (32-bit words) */ | |
571 | uint32_t *xva_rtnattrmapp; /* Ptr to xva_rtnattrmap[] */ | |
572 | uint32_t xva_reqattrmap[XVA_MAPSIZE]; /* Requested attrs */ | |
573 | uint32_t xva_rtnattrmap[XVA_MAPSIZE]; /* Returned attrs */ | |
574 | xoptattr_t xva_xoptattrs; /* Optional attributes */ | |
575 | } xvattr_t; | |
576 | ||
577 | typedef struct vsecattr { | |
578 | uint_t vsa_mask; /* See below */ | |
579 | int vsa_aclcnt; /* ACL entry count */ | |
580 | void *vsa_aclentp; /* pointer to ACL entries */ | |
581 | int vsa_dfaclcnt; /* default ACL entry count */ | |
582 | void *vsa_dfaclentp; /* pointer to default ACL entries */ | |
583 | size_t vsa_aclentsz; /* ACE size in bytes of vsa_aclentp */ | |
584 | } vsecattr_t; | |
585 | ||
586 | #define AT_TYPE 0x00001 | |
587 | #define AT_MODE 0x00002 | |
588 | #define AT_UID 0x00004 | |
589 | #define AT_GID 0x00008 | |
590 | #define AT_FSID 0x00010 | |
591 | #define AT_NODEID 0x00020 | |
592 | #define AT_NLINK 0x00040 | |
593 | #define AT_SIZE 0x00080 | |
594 | #define AT_ATIME 0x00100 | |
595 | #define AT_MTIME 0x00200 | |
596 | #define AT_CTIME 0x00400 | |
597 | #define AT_RDEV 0x00800 | |
598 | #define AT_BLKSIZE 0x01000 | |
599 | #define AT_NBLOCKS 0x02000 | |
600 | #define AT_SEQ 0x08000 | |
601 | #define AT_XVATTR 0x10000 | |
602 | ||
603 | #define CRCREAT 0 | |
604 | ||
428870ff BB |
605 | extern int fop_getattr(vnode_t *vp, vattr_t *vap); |
606 | ||
274091c0 | 607 | #define VOP_CLOSE(vp, f, c, o, cr, ct) vn_close(vp) |
77755a57 | 608 | #define VOP_PUTPAGE(vp, of, sz, fl, cr, ct) 0 |
428870ff | 609 | #define VOP_GETATTR(vp, vap, fl, cr, ct) fop_getattr((vp), (vap)); |
77755a57 BB |
610 | |
611 | #define VOP_FSYNC(vp, f, cr, ct) fsync((vp)->v_fd) | |
612 | ||
613 | #define VN_RELE(vp) vn_close(vp) | |
614 | ||
615 | extern int vn_open(char *path, int x1, int oflags, int mode, vnode_t **vpp, | |
616 | int x2, int x3); | |
617 | extern int vn_openat(char *path, int x1, int oflags, int mode, vnode_t **vpp, | |
618 | int x2, int x3, vnode_t *vp, int fd); | |
619 | extern int vn_rdwr(int uio, vnode_t *vp, void *addr, ssize_t len, | |
620 | offset_t offset, int x1, int x2, rlim64_t x3, void *x4, ssize_t *residp); | |
621 | extern void vn_close(vnode_t *vp); | |
622 | ||
623 | #define vn_remove(path, x1, x2) remove(path) | |
624 | #define vn_rename(from, to, seg) rename((from), (to)) | |
625 | #define vn_is_readonly(vp) B_FALSE | |
626 | ||
627 | extern vnode_t *rootdir; | |
628 | ||
629 | #include <sys/file.h> /* for FREAD, FWRITE, etc */ | |
630 | ||
631 | /* | |
632 | * Random stuff | |
633 | */ | |
428870ff BB |
634 | #define ddi_get_lbolt() (gethrtime() >> 23) |
635 | #define ddi_get_lbolt64() (gethrtime() >> 23) | |
77755a57 BB |
636 | #define hz 119 /* frequency when using gethrtime() >> 23 for lbolt */ |
637 | ||
0b75bdb3 CC |
638 | #define ddi_time_before(a, b) (a < b) |
639 | #define ddi_time_after(a, b) ddi_time_before(b, a) | |
640 | #define ddi_time_before_eq(a, b) (!ddi_time_after(a, b)) | |
641 | #define ddi_time_after_eq(a, b) ddi_time_before_eq(b, a) | |
642 | ||
643 | #define ddi_time_before64(a, b) (a < b) | |
644 | #define ddi_time_after64(a, b) ddi_time_before64(b, a) | |
645 | #define ddi_time_before_eq64(a, b) (!ddi_time_after64(a, b)) | |
646 | #define ddi_time_after_eq64(a, b) ddi_time_before_eq64(b, a) | |
647 | ||
77755a57 BB |
648 | extern void delay(clock_t ticks); |
649 | ||
cc92e9d0 GW |
650 | #define SEC_TO_TICK(sec) ((sec) * hz) |
651 | #define MSEC_TO_TICK(msec) ((msec) / (MILLISEC / hz)) | |
652 | #define USEC_TO_TICK(usec) ((usec) / (MICROSEC / hz)) | |
653 | #define NSEC_TO_TICK(usec) ((usec) / (NANOSEC / hz)) | |
654 | ||
77755a57 | 655 | #define gethrestime_sec() time(NULL) |
428870ff BB |
656 | #define gethrestime(t) \ |
657 | do {\ | |
658 | (t)->tv_sec = gethrestime_sec();\ | |
659 | (t)->tv_nsec = 0;\ | |
660 | } while (0); | |
77755a57 BB |
661 | |
662 | #define max_ncpus 64 | |
ca67b33a | 663 | #define boot_ncpus (sysconf(_SC_NPROCESSORS_ONLN)) |
77755a57 | 664 | |
1229323d BB |
665 | /* |
666 | * Process priorities as defined by setpriority(2) and getpriority(2). | |
667 | */ | |
668 | #define minclsyspri 19 | |
669 | #define maxclsyspri -20 | |
670 | #define defclsyspri 0 | |
77755a57 | 671 | |
1a01c207 | 672 | #define CPU_SEQID ((uintptr_t)pthread_self() & (max_ncpus - 1)) |
77755a57 BB |
673 | |
674 | #define kcred NULL | |
675 | #define CRED() NULL | |
676 | ||
677 | #define ptob(x) ((x) * PAGESIZE) | |
678 | ||
679 | extern uint64_t physmem; | |
680 | ||
9bd274dd | 681 | extern int highbit64(uint64_t i); |
193a37cb | 682 | extern int lowbit64(uint64_t i); |
0b04990a TC |
683 | extern int highbit(ulong_t i); |
684 | extern int lowbit(ulong_t i); | |
77755a57 BB |
685 | extern int random_get_bytes(uint8_t *ptr, size_t len); |
686 | extern int random_get_pseudo_bytes(uint8_t *ptr, size_t len); | |
687 | ||
688 | extern void kernel_init(int); | |
689 | extern void kernel_fini(void); | |
519129ff BB |
690 | extern void thread_init(void); |
691 | extern void thread_fini(void); | |
0b04990a TC |
692 | extern void random_init(void); |
693 | extern void random_fini(void); | |
77755a57 BB |
694 | |
695 | struct spa; | |
696 | extern void nicenum(uint64_t num, char *buf); | |
697 | extern void show_pool_stats(struct spa *); | |
698 | ||
699 | typedef struct callb_cpr { | |
700 | kmutex_t *cc_lockp; | |
701 | } callb_cpr_t; | |
702 | ||
703 | #define CALLB_CPR_INIT(cp, lockp, func, name) { \ | |
704 | (cp)->cc_lockp = lockp; \ | |
705 | } | |
706 | ||
707 | #define CALLB_CPR_SAFE_BEGIN(cp) { \ | |
708 | ASSERT(MUTEX_HELD((cp)->cc_lockp)); \ | |
709 | } | |
710 | ||
711 | #define CALLB_CPR_SAFE_END(cp, lockp) { \ | |
712 | ASSERT(MUTEX_HELD((cp)->cc_lockp)); \ | |
713 | } | |
714 | ||
715 | #define CALLB_CPR_EXIT(cp) { \ | |
716 | ASSERT(MUTEX_HELD((cp)->cc_lockp)); \ | |
717 | mutex_exit((cp)->cc_lockp); \ | |
718 | } | |
719 | ||
720 | #define zone_dataset_visible(x, y) (1) | |
721 | #define INGLOBALZONE(z) (1) | |
722 | ||
00b46022 | 723 | extern char *kmem_vasprintf(const char *fmt, va_list adx); |
428870ff | 724 | extern char *kmem_asprintf(const char *fmt, ...); |
13fe0198 | 725 | #define strfree(str) kmem_free((str), strlen(str) + 1) |
428870ff | 726 | |
77755a57 BB |
727 | /* |
728 | * Hostname information | |
729 | */ | |
d164b209 | 730 | extern char hw_serial[]; /* for userland-emulated hostid access */ |
77755a57 BB |
731 | extern int ddi_strtoul(const char *str, char **nptr, int base, |
732 | unsigned long *result); | |
733 | ||
428870ff BB |
734 | extern int ddi_strtoull(const char *str, char **nptr, int base, |
735 | u_longlong_t *result); | |
736 | ||
f0e324f2 BB |
737 | typedef struct utsname utsname_t; |
738 | extern utsname_t *utsname(void); | |
739 | ||
77755a57 BB |
740 | /* ZFS Boot Related stuff. */ |
741 | ||
742 | struct _buf { | |
743 | intptr_t _fd; | |
744 | }; | |
745 | ||
746 | struct bootstat { | |
747 | uint64_t st_size; | |
748 | }; | |
749 | ||
750 | typedef struct ace_object { | |
751 | uid_t a_who; | |
752 | uint32_t a_access_mask; | |
753 | uint16_t a_flags; | |
754 | uint16_t a_type; | |
755 | uint8_t a_obj_type[16]; | |
756 | uint8_t a_inherit_obj_type[16]; | |
757 | } ace_object_t; | |
758 | ||
759 | ||
760 | #define ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE 0x05 | |
761 | #define ACE_ACCESS_DENIED_OBJECT_ACE_TYPE 0x06 | |
762 | #define ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE 0x07 | |
763 | #define ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE 0x08 | |
764 | ||
765 | extern struct _buf *kobj_open_file(char *name); | |
766 | extern int kobj_read_file(struct _buf *file, char *buf, unsigned size, | |
767 | unsigned off); | |
768 | extern void kobj_close_file(struct _buf *file); | |
769 | extern int kobj_get_filesize(struct _buf *file, uint64_t *size); | |
770 | extern int zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr); | |
771 | extern int zfs_secpolicy_rename_perms(const char *from, const char *to, | |
772 | cred_t *cr); | |
773 | extern int zfs_secpolicy_destroy_perms(const char *name, cred_t *cr); | |
f74b821a | 774 | extern int secpolicy_zfs(const cred_t *cr); |
77755a57 BB |
775 | extern zoneid_t getzoneid(void); |
776 | ||
777 | /* SID stuff */ | |
778 | typedef struct ksiddomain { | |
779 | uint_t kd_ref; | |
780 | uint_t kd_len; | |
781 | char *kd_name; | |
782 | } ksiddomain_t; | |
783 | ||
784 | ksiddomain_t *ksid_lookupdomain(const char *); | |
785 | void ksiddomain_rele(ksiddomain_t *); | |
786 | ||
9babb374 BB |
787 | #define DDI_SLEEP KM_SLEEP |
788 | #define ddi_log_sysevent(_a, _b, _c, _d, _e, _f, _g) \ | |
789 | sysevent_post_event(_c, _d, _b, "libzpool", _e, _f) | |
790 | ||
d1d7e268 | 791 | #define zfs_sleep_until(wakeup) \ |
e8b96c60 MA |
792 | do { \ |
793 | hrtime_t delta = wakeup - gethrtime(); \ | |
794 | struct timespec ts; \ | |
795 | ts.tv_sec = delta / NANOSEC; \ | |
796 | ts.tv_nsec = delta % NANOSEC; \ | |
797 | (void) nanosleep(&ts, NULL); \ | |
798 | } while (0) | |
799 | ||
92119cc2 BB |
800 | typedef int fstrans_cookie_t; |
801 | ||
802 | extern fstrans_cookie_t spl_fstrans_mark(void); | |
803 | extern void spl_fstrans_unmark(fstrans_cookie_t); | |
804 | extern int spl_fstrans_check(void); | |
77755a57 | 805 | |
92119cc2 | 806 | #endif /* _KERNEL */ |
77755a57 | 807 | #endif /* _SYS_ZFS_CONTEXT_H */ |