]>
Commit | Line | Data |
---|---|---|
34dc7c2f 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 | /* | |
572e2857 | 22 | * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. |
a0bd735a | 23 | * Copyright (c) 2016 Actifio, Inc. All rights reserved. |
34dc7c2f BB |
24 | */ |
25 | ||
34dc7c2f BB |
26 | #include <assert.h> |
27 | #include <fcntl.h> | |
28 | #include <poll.h> | |
29 | #include <stdio.h> | |
30 | #include <stdlib.h> | |
31 | #include <string.h> | |
32 | #include <zlib.h> | |
9867e8be | 33 | #include <libgen.h> |
1e33ac1e | 34 | #include <sys/signal.h> |
34dc7c2f BB |
35 | #include <sys/spa.h> |
36 | #include <sys/stat.h> | |
37 | #include <sys/processor.h> | |
38 | #include <sys/zfs_context.h> | |
13fe0198 | 39 | #include <sys/rrwlock.h> |
34dc7c2f | 40 | #include <sys/utsname.h> |
d603ed6c | 41 | #include <sys/time.h> |
d164b209 | 42 | #include <sys/systeminfo.h> |
1eeb4562 | 43 | #include <zfs_fletcher.h> |
0b04990a | 44 | #include <sys/crypto/icp.h> |
34dc7c2f BB |
45 | |
46 | /* | |
47 | * Emulation of kernel services in userland. | |
48 | */ | |
49 | ||
428870ff | 50 | int aok; |
34dc7c2f BB |
51 | uint64_t physmem; |
52 | vnode_t *rootdir = (vnode_t *)0xabcd1234; | |
d164b209 | 53 | char hw_serial[HW_HOSTID_LEN]; |
f0e324f2 | 54 | struct utsname hw_utsname; |
ca67b33a | 55 | vmem_t *zio_arena = NULL; |
34dc7c2f | 56 | |
9867e8be MA |
57 | /* If set, all blocks read will be copied to the specified directory. */ |
58 | char *vn_dumpdir = NULL; | |
59 | ||
428870ff BB |
60 | /* this only exists to have its address taken */ |
61 | struct proc p0; | |
62 | ||
34dc7c2f BB |
63 | /* |
64 | * ========================================================================= | |
65 | * threads | |
66 | * ========================================================================= | |
67 | */ | |
1e33ac1e BB |
68 | |
69 | pthread_cond_t kthread_cond = PTHREAD_COND_INITIALIZER; | |
70 | pthread_mutex_t kthread_lock = PTHREAD_MUTEX_INITIALIZER; | |
71 | pthread_key_t kthread_key; | |
72 | int kthread_nr = 0; | |
73 | ||
519129ff | 74 | void |
1e33ac1e BB |
75 | thread_init(void) |
76 | { | |
77 | kthread_t *kt; | |
78 | ||
79 | VERIFY3S(pthread_key_create(&kthread_key, NULL), ==, 0); | |
80 | ||
81 | /* Create entry for primary kthread */ | |
d1d7e268 | 82 | kt = umem_zalloc(sizeof (kthread_t), UMEM_NOFAIL); |
1e33ac1e BB |
83 | kt->t_tid = pthread_self(); |
84 | kt->t_func = NULL; | |
85 | ||
86 | VERIFY3S(pthread_setspecific(kthread_key, kt), ==, 0); | |
87 | ||
88 | /* Only the main thread should be running at the moment */ | |
89 | ASSERT3S(kthread_nr, ==, 0); | |
90 | kthread_nr = 1; | |
91 | } | |
92 | ||
519129ff | 93 | void |
1e33ac1e BB |
94 | thread_fini(void) |
95 | { | |
96 | kthread_t *kt = curthread; | |
97 | ||
98 | ASSERT(pthread_equal(kt->t_tid, pthread_self())); | |
99 | ASSERT3P(kt->t_func, ==, NULL); | |
100 | ||
d1d7e268 | 101 | umem_free(kt, sizeof (kthread_t)); |
1e33ac1e BB |
102 | |
103 | /* Wait for all threads to exit via thread_exit() */ | |
104 | VERIFY3S(pthread_mutex_lock(&kthread_lock), ==, 0); | |
105 | ||
106 | kthread_nr--; /* Main thread is exiting */ | |
107 | ||
108 | while (kthread_nr > 0) | |
206971d2 | 109 | VERIFY0(pthread_cond_wait(&kthread_cond, &kthread_lock)); |
1e33ac1e BB |
110 | |
111 | ASSERT3S(kthread_nr, ==, 0); | |
112 | VERIFY3S(pthread_mutex_unlock(&kthread_lock), ==, 0); | |
113 | ||
114 | VERIFY3S(pthread_key_delete(kthread_key), ==, 0); | |
115 | } | |
116 | ||
34dc7c2f | 117 | kthread_t * |
1e33ac1e BB |
118 | zk_thread_current(void) |
119 | { | |
120 | kthread_t *kt = pthread_getspecific(kthread_key); | |
121 | ||
122 | ASSERT3P(kt, !=, NULL); | |
123 | ||
d1d7e268 | 124 | return (kt); |
1e33ac1e BB |
125 | } |
126 | ||
127 | void * | |
128 | zk_thread_helper(void *arg) | |
34dc7c2f | 129 | { |
1e33ac1e BB |
130 | kthread_t *kt = (kthread_t *) arg; |
131 | ||
132 | VERIFY3S(pthread_setspecific(kthread_key, kt), ==, 0); | |
34dc7c2f | 133 | |
1e33ac1e BB |
134 | VERIFY3S(pthread_mutex_lock(&kthread_lock), ==, 0); |
135 | kthread_nr++; | |
136 | VERIFY3S(pthread_mutex_unlock(&kthread_lock), ==, 0); | |
1229323d | 137 | (void) setpriority(PRIO_PROCESS, 0, kt->t_pri); |
34dc7c2f | 138 | |
1e33ac1e BB |
139 | kt->t_tid = pthread_self(); |
140 | ((thread_func_arg_t) kt->t_func)(kt->t_arg); | |
141 | ||
142 | /* Unreachable, thread must exit with thread_exit() */ | |
143 | abort(); | |
144 | ||
d1d7e268 | 145 | return (NULL); |
1e33ac1e BB |
146 | } |
147 | ||
148 | kthread_t * | |
149 | zk_thread_create(caddr_t stk, size_t stksize, thread_func_t func, void *arg, | |
d1d7e268 | 150 | size_t len, proc_t *pp, int state, pri_t pri, int detachstate) |
1e33ac1e BB |
151 | { |
152 | kthread_t *kt; | |
153 | pthread_attr_t attr; | |
aa0ac7ca | 154 | char *stkstr; |
1e33ac1e | 155 | |
aa0ac7ca | 156 | ASSERT0(state & ~TS_RUN); |
1e33ac1e | 157 | |
d1d7e268 | 158 | kt = umem_zalloc(sizeof (kthread_t), UMEM_NOFAIL); |
1e33ac1e BB |
159 | kt->t_func = func; |
160 | kt->t_arg = arg; | |
1229323d | 161 | kt->t_pri = pri; |
1e33ac1e | 162 | |
aa0ac7ca BB |
163 | VERIFY0(pthread_attr_init(&attr)); |
164 | VERIFY0(pthread_attr_setdetachstate(&attr, detachstate)); | |
165 | ||
1e33ac1e | 166 | /* |
aa0ac7ca BB |
167 | * We allow the default stack size in user space to be specified by |
168 | * setting the ZFS_STACK_SIZE environment variable. This allows us | |
169 | * the convenience of observing and debugging stack overruns in | |
170 | * user space. Explicitly specified stack sizes will be honored. | |
171 | * The usage of ZFS_STACK_SIZE is discussed further in the | |
172 | * ENVIRONMENT VARIABLES sections of the ztest(1) man page. | |
1e33ac1e | 173 | */ |
aa0ac7ca BB |
174 | if (stksize == 0) { |
175 | stkstr = getenv("ZFS_STACK_SIZE"); | |
176 | ||
177 | if (stkstr == NULL) | |
178 | stksize = TS_STACK_MAX; | |
179 | else | |
180 | stksize = MAX(atoi(stkstr), TS_STACK_MIN); | |
181 | } | |
182 | ||
183 | VERIFY3S(stksize, >, 0); | |
184 | stksize = P2ROUNDUP(MAX(stksize, TS_STACK_MIN), PAGESIZE); | |
206971d2 DR |
185 | /* |
186 | * If this ever fails, it may be because the stack size is not a | |
187 | * multiple of system page size. | |
188 | */ | |
aa0ac7ca BB |
189 | VERIFY0(pthread_attr_setstacksize(&attr, stksize)); |
190 | VERIFY0(pthread_attr_setguardsize(&attr, PAGESIZE)); | |
191 | ||
192 | VERIFY0(pthread_create(&kt->t_tid, &attr, &zk_thread_helper, kt)); | |
193 | VERIFY0(pthread_attr_destroy(&attr)); | |
1e33ac1e | 194 | |
d1d7e268 | 195 | return (kt); |
1e33ac1e BB |
196 | } |
197 | ||
198 | void | |
199 | zk_thread_exit(void) | |
200 | { | |
201 | kthread_t *kt = curthread; | |
202 | ||
203 | ASSERT(pthread_equal(kt->t_tid, pthread_self())); | |
204 | ||
d1d7e268 | 205 | umem_free(kt, sizeof (kthread_t)); |
1e33ac1e | 206 | |
206971d2 | 207 | VERIFY0(pthread_mutex_lock(&kthread_lock)); |
1e33ac1e | 208 | kthread_nr--; |
206971d2 | 209 | VERIFY0(pthread_mutex_unlock(&kthread_lock)); |
1e33ac1e | 210 | |
206971d2 | 211 | VERIFY0(pthread_cond_broadcast(&kthread_cond)); |
1e33ac1e BB |
212 | pthread_exit((void *)TS_MAGIC); |
213 | } | |
214 | ||
215 | void | |
216 | zk_thread_join(kt_did_t tid) | |
217 | { | |
218 | void *ret; | |
219 | ||
220 | pthread_join((pthread_t)tid, &ret); | |
221 | VERIFY3P(ret, ==, (void *)TS_MAGIC); | |
34dc7c2f BB |
222 | } |
223 | ||
224 | /* | |
225 | * ========================================================================= | |
226 | * kstats | |
227 | * ========================================================================= | |
228 | */ | |
229 | /*ARGSUSED*/ | |
230 | kstat_t * | |
330847ff MA |
231 | kstat_create(const char *module, int instance, const char *name, |
232 | const char *class, uchar_t type, ulong_t ndata, uchar_t ks_flag) | |
34dc7c2f BB |
233 | { |
234 | return (NULL); | |
235 | } | |
236 | ||
237 | /*ARGSUSED*/ | |
238 | void | |
239 | kstat_install(kstat_t *ksp) | |
240 | {} | |
241 | ||
242 | /*ARGSUSED*/ | |
243 | void | |
244 | kstat_delete(kstat_t *ksp) | |
245 | {} | |
246 | ||
1421c891 | 247 | /*ARGSUSED*/ |
330847ff MA |
248 | void |
249 | kstat_waitq_enter(kstat_io_t *kiop) | |
250 | {} | |
251 | ||
252 | /*ARGSUSED*/ | |
253 | void | |
254 | kstat_waitq_exit(kstat_io_t *kiop) | |
255 | {} | |
256 | ||
257 | /*ARGSUSED*/ | |
258 | void | |
259 | kstat_runq_enter(kstat_io_t *kiop) | |
260 | {} | |
261 | ||
262 | /*ARGSUSED*/ | |
263 | void | |
264 | kstat_runq_exit(kstat_io_t *kiop) | |
265 | {} | |
266 | ||
267 | /*ARGSUSED*/ | |
268 | void | |
269 | kstat_waitq_to_runq(kstat_io_t *kiop) | |
270 | {} | |
271 | ||
272 | /*ARGSUSED*/ | |
273 | void | |
274 | kstat_runq_back_to_waitq(kstat_io_t *kiop) | |
275 | {} | |
276 | ||
1421c891 PS |
277 | void |
278 | kstat_set_raw_ops(kstat_t *ksp, | |
279 | int (*headers)(char *buf, size_t size), | |
280 | int (*data)(char *buf, size_t size, void *data), | |
281 | void *(*addr)(kstat_t *ksp, loff_t index)) | |
282 | {} | |
283 | ||
34dc7c2f BB |
284 | /* |
285 | * ========================================================================= | |
286 | * mutexes | |
287 | * ========================================================================= | |
288 | */ | |
1e33ac1e | 289 | |
34dc7c2f | 290 | void |
1e33ac1e | 291 | mutex_init(kmutex_t *mp, char *name, int type, void *cookie) |
34dc7c2f | 292 | { |
1e33ac1e BB |
293 | ASSERT3S(type, ==, MUTEX_DEFAULT); |
294 | ASSERT3P(cookie, ==, NULL); | |
295 | mp->m_owner = MTX_INIT; | |
296 | mp->m_magic = MTX_MAGIC; | |
297 | VERIFY3S(pthread_mutex_init(&mp->m_lock, NULL), ==, 0); | |
34dc7c2f BB |
298 | } |
299 | ||
300 | void | |
1e33ac1e | 301 | mutex_destroy(kmutex_t *mp) |
34dc7c2f | 302 | { |
1e33ac1e BB |
303 | ASSERT3U(mp->m_magic, ==, MTX_MAGIC); |
304 | ASSERT3P(mp->m_owner, ==, MTX_INIT); | |
340dfbe1 | 305 | ASSERT0(pthread_mutex_destroy(&(mp)->m_lock)); |
1e33ac1e BB |
306 | mp->m_owner = MTX_DEST; |
307 | mp->m_magic = 0; | |
34dc7c2f BB |
308 | } |
309 | ||
310 | void | |
311 | mutex_enter(kmutex_t *mp) | |
312 | { | |
1e33ac1e BB |
313 | ASSERT3U(mp->m_magic, ==, MTX_MAGIC); |
314 | ASSERT3P(mp->m_owner, !=, MTX_DEST); | |
315 | ASSERT3P(mp->m_owner, !=, curthread); | |
316 | VERIFY3S(pthread_mutex_lock(&mp->m_lock), ==, 0); | |
317 | ASSERT3P(mp->m_owner, ==, MTX_INIT); | |
34dc7c2f BB |
318 | mp->m_owner = curthread; |
319 | } | |
320 | ||
321 | int | |
322 | mutex_tryenter(kmutex_t *mp) | |
323 | { | |
206971d2 | 324 | int err; |
1e33ac1e BB |
325 | ASSERT3U(mp->m_magic, ==, MTX_MAGIC); |
326 | ASSERT3P(mp->m_owner, !=, MTX_DEST); | |
206971d2 | 327 | if (0 == (err = pthread_mutex_trylock(&mp->m_lock))) { |
1e33ac1e | 328 | ASSERT3P(mp->m_owner, ==, MTX_INIT); |
34dc7c2f BB |
329 | mp->m_owner = curthread; |
330 | return (1); | |
331 | } else { | |
206971d2 | 332 | VERIFY3S(err, ==, EBUSY); |
34dc7c2f BB |
333 | return (0); |
334 | } | |
335 | } | |
336 | ||
337 | void | |
338 | mutex_exit(kmutex_t *mp) | |
339 | { | |
1e33ac1e BB |
340 | ASSERT3U(mp->m_magic, ==, MTX_MAGIC); |
341 | ASSERT3P(mutex_owner(mp), ==, curthread); | |
342 | mp->m_owner = MTX_INIT; | |
343 | VERIFY3S(pthread_mutex_unlock(&mp->m_lock), ==, 0); | |
34dc7c2f BB |
344 | } |
345 | ||
346 | void * | |
347 | mutex_owner(kmutex_t *mp) | |
348 | { | |
1e33ac1e | 349 | ASSERT3U(mp->m_magic, ==, MTX_MAGIC); |
34dc7c2f BB |
350 | return (mp->m_owner); |
351 | } | |
352 | ||
1e33ac1e BB |
353 | int |
354 | mutex_held(kmutex_t *mp) | |
355 | { | |
356 | return (mp->m_owner == curthread); | |
357 | } | |
358 | ||
34dc7c2f BB |
359 | /* |
360 | * ========================================================================= | |
361 | * rwlocks | |
362 | * ========================================================================= | |
363 | */ | |
1e33ac1e | 364 | |
34dc7c2f BB |
365 | void |
366 | rw_init(krwlock_t *rwlp, char *name, int type, void *arg) | |
367 | { | |
1e33ac1e BB |
368 | ASSERT3S(type, ==, RW_DEFAULT); |
369 | ASSERT3P(arg, ==, NULL); | |
370 | VERIFY3S(pthread_rwlock_init(&rwlp->rw_lock, NULL), ==, 0); | |
371 | rwlp->rw_owner = RW_INIT; | |
372 | rwlp->rw_wr_owner = RW_INIT; | |
373 | rwlp->rw_readers = 0; | |
374 | rwlp->rw_magic = RW_MAGIC; | |
34dc7c2f BB |
375 | } |
376 | ||
377 | void | |
378 | rw_destroy(krwlock_t *rwlp) | |
379 | { | |
1e33ac1e | 380 | ASSERT3U(rwlp->rw_magic, ==, RW_MAGIC); |
843b4aad | 381 | ASSERT(rwlp->rw_readers == 0 && rwlp->rw_wr_owner == RW_INIT); |
1e33ac1e BB |
382 | VERIFY3S(pthread_rwlock_destroy(&rwlp->rw_lock), ==, 0); |
383 | rwlp->rw_magic = 0; | |
34dc7c2f BB |
384 | } |
385 | ||
386 | void | |
387 | rw_enter(krwlock_t *rwlp, krw_t rw) | |
388 | { | |
1e33ac1e BB |
389 | ASSERT3U(rwlp->rw_magic, ==, RW_MAGIC); |
390 | ASSERT3P(rwlp->rw_owner, !=, curthread); | |
391 | ASSERT3P(rwlp->rw_wr_owner, !=, curthread); | |
34dc7c2f | 392 | |
1e33ac1e BB |
393 | if (rw == RW_READER) { |
394 | VERIFY3S(pthread_rwlock_rdlock(&rwlp->rw_lock), ==, 0); | |
395 | ASSERT3P(rwlp->rw_wr_owner, ==, RW_INIT); | |
396 | ||
397 | atomic_inc_uint(&rwlp->rw_readers); | |
398 | } else { | |
399 | VERIFY3S(pthread_rwlock_wrlock(&rwlp->rw_lock), ==, 0); | |
400 | ASSERT3P(rwlp->rw_wr_owner, ==, RW_INIT); | |
401 | ASSERT3U(rwlp->rw_readers, ==, 0); | |
402 | ||
403 | rwlp->rw_wr_owner = curthread; | |
404 | } | |
34dc7c2f BB |
405 | |
406 | rwlp->rw_owner = curthread; | |
407 | } | |
408 | ||
409 | void | |
410 | rw_exit(krwlock_t *rwlp) | |
411 | { | |
1e33ac1e BB |
412 | ASSERT3U(rwlp->rw_magic, ==, RW_MAGIC); |
413 | ASSERT(RW_LOCK_HELD(rwlp)); | |
414 | ||
415 | if (RW_READ_HELD(rwlp)) | |
416 | atomic_dec_uint(&rwlp->rw_readers); | |
417 | else | |
418 | rwlp->rw_wr_owner = RW_INIT; | |
34dc7c2f | 419 | |
1e33ac1e BB |
420 | rwlp->rw_owner = RW_INIT; |
421 | VERIFY3S(pthread_rwlock_unlock(&rwlp->rw_lock), ==, 0); | |
34dc7c2f BB |
422 | } |
423 | ||
424 | int | |
425 | rw_tryenter(krwlock_t *rwlp, krw_t rw) | |
426 | { | |
427 | int rv; | |
428 | ||
1e33ac1e | 429 | ASSERT3U(rwlp->rw_magic, ==, RW_MAGIC); |
34dc7c2f BB |
430 | |
431 | if (rw == RW_READER) | |
1e33ac1e | 432 | rv = pthread_rwlock_tryrdlock(&rwlp->rw_lock); |
34dc7c2f | 433 | else |
1e33ac1e | 434 | rv = pthread_rwlock_trywrlock(&rwlp->rw_lock); |
34dc7c2f BB |
435 | |
436 | if (rv == 0) { | |
1e33ac1e BB |
437 | ASSERT3P(rwlp->rw_wr_owner, ==, RW_INIT); |
438 | ||
439 | if (rw == RW_READER) | |
440 | atomic_inc_uint(&rwlp->rw_readers); | |
441 | else { | |
442 | ASSERT3U(rwlp->rw_readers, ==, 0); | |
443 | rwlp->rw_wr_owner = curthread; | |
444 | } | |
445 | ||
34dc7c2f BB |
446 | rwlp->rw_owner = curthread; |
447 | return (1); | |
448 | } | |
449 | ||
1e33ac1e BB |
450 | VERIFY3S(rv, ==, EBUSY); |
451 | ||
34dc7c2f BB |
452 | return (0); |
453 | } | |
454 | ||
34dc7c2f BB |
455 | int |
456 | rw_tryupgrade(krwlock_t *rwlp) | |
457 | { | |
1e33ac1e | 458 | ASSERT3U(rwlp->rw_magic, ==, RW_MAGIC); |
34dc7c2f BB |
459 | |
460 | return (0); | |
461 | } | |
462 | ||
463 | /* | |
464 | * ========================================================================= | |
465 | * condition variables | |
466 | * ========================================================================= | |
467 | */ | |
1e33ac1e | 468 | |
34dc7c2f BB |
469 | void |
470 | cv_init(kcondvar_t *cv, char *name, int type, void *arg) | |
471 | { | |
1e33ac1e BB |
472 | ASSERT3S(type, ==, CV_DEFAULT); |
473 | cv->cv_magic = CV_MAGIC; | |
206971d2 | 474 | VERIFY0(pthread_cond_init(&cv->cv, NULL)); |
34dc7c2f BB |
475 | } |
476 | ||
477 | void | |
478 | cv_destroy(kcondvar_t *cv) | |
479 | { | |
1e33ac1e | 480 | ASSERT3U(cv->cv_magic, ==, CV_MAGIC); |
206971d2 | 481 | VERIFY0(pthread_cond_destroy(&cv->cv)); |
1e33ac1e | 482 | cv->cv_magic = 0; |
34dc7c2f BB |
483 | } |
484 | ||
485 | void | |
486 | cv_wait(kcondvar_t *cv, kmutex_t *mp) | |
487 | { | |
1e33ac1e BB |
488 | ASSERT3U(cv->cv_magic, ==, CV_MAGIC); |
489 | ASSERT3P(mutex_owner(mp), ==, curthread); | |
490 | mp->m_owner = MTX_INIT; | |
206971d2 | 491 | VERIFY0(pthread_cond_wait(&cv->cv, &mp->m_lock)); |
34dc7c2f BB |
492 | mp->m_owner = curthread; |
493 | } | |
494 | ||
495 | clock_t | |
496 | cv_timedwait(kcondvar_t *cv, kmutex_t *mp, clock_t abstime) | |
497 | { | |
498 | int error; | |
1e33ac1e | 499 | struct timeval tv; |
34dc7c2f BB |
500 | timestruc_t ts; |
501 | clock_t delta; | |
502 | ||
1e33ac1e BB |
503 | ASSERT3U(cv->cv_magic, ==, CV_MAGIC); |
504 | ||
428870ff | 505 | delta = abstime - ddi_get_lbolt(); |
34dc7c2f BB |
506 | if (delta <= 0) |
507 | return (-1); | |
508 | ||
1e33ac1e BB |
509 | VERIFY(gettimeofday(&tv, NULL) == 0); |
510 | ||
511 | ts.tv_sec = tv.tv_sec + delta / hz; | |
67925abb | 512 | ts.tv_nsec = tv.tv_usec * NSEC_PER_USEC + (delta % hz) * (NANOSEC / hz); |
1e33ac1e BB |
513 | if (ts.tv_nsec >= NANOSEC) { |
514 | ts.tv_sec++; | |
515 | ts.tv_nsec -= NANOSEC; | |
516 | } | |
34dc7c2f | 517 | |
1e33ac1e BB |
518 | ASSERT3P(mutex_owner(mp), ==, curthread); |
519 | mp->m_owner = MTX_INIT; | |
520 | error = pthread_cond_timedwait(&cv->cv, &mp->m_lock, &ts); | |
34dc7c2f BB |
521 | mp->m_owner = curthread; |
522 | ||
1e33ac1e | 523 | if (error == ETIMEDOUT) |
34dc7c2f BB |
524 | return (-1); |
525 | ||
206971d2 | 526 | VERIFY0(error); |
34dc7c2f BB |
527 | |
528 | return (1); | |
529 | } | |
530 | ||
63fd3c6c AL |
531 | /*ARGSUSED*/ |
532 | clock_t | |
533 | cv_timedwait_hires(kcondvar_t *cv, kmutex_t *mp, hrtime_t tim, hrtime_t res, | |
534 | int flag) | |
535 | { | |
536 | int error; | |
67925abb | 537 | struct timeval tv; |
63fd3c6c AL |
538 | timestruc_t ts; |
539 | hrtime_t delta; | |
540 | ||
206971d2 DR |
541 | ASSERT(flag == 0 || flag == CALLOUT_FLAG_ABSOLUTE); |
542 | ||
543 | delta = tim; | |
544 | if (flag & CALLOUT_FLAG_ABSOLUTE) | |
545 | delta -= gethrtime(); | |
63fd3c6c | 546 | |
63fd3c6c AL |
547 | if (delta <= 0) |
548 | return (-1); | |
549 | ||
67925abb BB |
550 | VERIFY(gettimeofday(&tv, NULL) == 0); |
551 | ||
552 | ts.tv_sec = tv.tv_sec + delta / NANOSEC; | |
553 | ts.tv_nsec = tv.tv_usec * NSEC_PER_USEC + (delta % NANOSEC); | |
554 | if (ts.tv_nsec >= NANOSEC) { | |
555 | ts.tv_sec++; | |
556 | ts.tv_nsec -= NANOSEC; | |
557 | } | |
63fd3c6c AL |
558 | |
559 | ASSERT(mutex_owner(mp) == curthread); | |
67925abb | 560 | mp->m_owner = MTX_INIT; |
63fd3c6c AL |
561 | error = pthread_cond_timedwait(&cv->cv, &mp->m_lock, &ts); |
562 | mp->m_owner = curthread; | |
563 | ||
206971d2 | 564 | if (error == ETIMEDOUT) |
63fd3c6c AL |
565 | return (-1); |
566 | ||
206971d2 | 567 | VERIFY0(error); |
63fd3c6c AL |
568 | |
569 | return (1); | |
570 | } | |
571 | ||
34dc7c2f BB |
572 | void |
573 | cv_signal(kcondvar_t *cv) | |
574 | { | |
1e33ac1e | 575 | ASSERT3U(cv->cv_magic, ==, CV_MAGIC); |
206971d2 | 576 | VERIFY0(pthread_cond_signal(&cv->cv)); |
34dc7c2f BB |
577 | } |
578 | ||
579 | void | |
580 | cv_broadcast(kcondvar_t *cv) | |
581 | { | |
1e33ac1e | 582 | ASSERT3U(cv->cv_magic, ==, CV_MAGIC); |
206971d2 | 583 | VERIFY0(pthread_cond_broadcast(&cv->cv)); |
34dc7c2f BB |
584 | } |
585 | ||
586 | /* | |
587 | * ========================================================================= | |
588 | * vnode operations | |
589 | * ========================================================================= | |
590 | */ | |
591 | /* | |
592 | * Note: for the xxxat() versions of these functions, we assume that the | |
593 | * starting vp is always rootdir (which is true for spa_directory.c, the only | |
594 | * ZFS consumer of these interfaces). We assert this is true, and then emulate | |
595 | * them by adding '/' in front of the path. | |
596 | */ | |
597 | ||
598 | /*ARGSUSED*/ | |
599 | int | |
600 | vn_open(char *path, int x1, int flags, int mode, vnode_t **vpp, int x2, int x3) | |
601 | { | |
e2c292bb | 602 | int fd = -1; |
603 | int dump_fd = -1; | |
34dc7c2f | 604 | vnode_t *vp; |
a4914d38 | 605 | int old_umask = 0; |
5ae4e2c2 | 606 | char *realpath; |
34dc7c2f | 607 | struct stat64 st; |
4d58b69d | 608 | int err; |
34dc7c2f | 609 | |
5ae4e2c2 BB |
610 | realpath = umem_alloc(MAXPATHLEN, UMEM_NOFAIL); |
611 | ||
34dc7c2f BB |
612 | /* |
613 | * If we're accessing a real disk from userland, we need to use | |
614 | * the character interface to avoid caching. This is particularly | |
615 | * important if we're trying to look at a real in-kernel storage | |
616 | * pool from userland, e.g. via zdb, because otherwise we won't | |
617 | * see the changes occurring under the segmap cache. | |
618 | * On the other hand, the stupid character device returns zero | |
619 | * for its size. So -- gag -- we open the block device to get | |
620 | * its size, and remember it for subsequent VOP_GETATTR(). | |
621 | */ | |
d603ed6c | 622 | #if defined(__sun__) || defined(__sun) |
34dc7c2f | 623 | if (strncmp(path, "/dev/", 5) == 0) { |
d603ed6c BB |
624 | #else |
625 | if (0) { | |
626 | #endif | |
34dc7c2f BB |
627 | char *dsk; |
628 | fd = open64(path, O_RDONLY); | |
5ae4e2c2 BB |
629 | if (fd == -1) { |
630 | err = errno; | |
631 | free(realpath); | |
632 | return (err); | |
633 | } | |
34dc7c2f | 634 | if (fstat64(fd, &st) == -1) { |
5ae4e2c2 | 635 | err = errno; |
34dc7c2f | 636 | close(fd); |
5ae4e2c2 BB |
637 | free(realpath); |
638 | return (err); | |
34dc7c2f BB |
639 | } |
640 | close(fd); | |
641 | (void) sprintf(realpath, "%s", path); | |
642 | dsk = strstr(path, "/dsk/"); | |
643 | if (dsk != NULL) | |
644 | (void) sprintf(realpath + (dsk - path) + 1, "r%s", | |
645 | dsk + 1); | |
646 | } else { | |
647 | (void) sprintf(realpath, "%s", path); | |
5ae4e2c2 BB |
648 | if (!(flags & FCREAT) && stat64(realpath, &st) == -1) { |
649 | err = errno; | |
650 | free(realpath); | |
651 | return (err); | |
652 | } | |
34dc7c2f BB |
653 | } |
654 | ||
d603ed6c BB |
655 | if (!(flags & FCREAT) && S_ISBLK(st.st_mode)) { |
656 | #ifdef __linux__ | |
657 | flags |= O_DIRECT; | |
658 | #endif | |
ada82581 BB |
659 | /* We shouldn't be writing to block devices in userspace */ |
660 | VERIFY(!(flags & FWRITE)); | |
d603ed6c BB |
661 | } |
662 | ||
34dc7c2f BB |
663 | if (flags & FCREAT) |
664 | old_umask = umask(0); | |
665 | ||
666 | /* | |
667 | * The construct 'flags - FREAD' conveniently maps combinations of | |
668 | * FREAD and FWRITE to the corresponding O_RDONLY, O_WRONLY, and O_RDWR. | |
669 | */ | |
670 | fd = open64(realpath, flags - FREAD, mode); | |
470f12d6 G |
671 | if (fd == -1) { |
672 | err = errno; | |
673 | free(realpath); | |
674 | return (err); | |
675 | } | |
34dc7c2f BB |
676 | |
677 | if (flags & FCREAT) | |
678 | (void) umask(old_umask); | |
679 | ||
9867e8be MA |
680 | if (vn_dumpdir != NULL) { |
681 | char *dumppath = umem_zalloc(MAXPATHLEN, UMEM_NOFAIL); | |
682 | (void) snprintf(dumppath, MAXPATHLEN, | |
683 | "%s/%s", vn_dumpdir, basename(realpath)); | |
684 | dump_fd = open64(dumppath, O_CREAT | O_WRONLY, 0666); | |
685 | umem_free(dumppath, MAXPATHLEN); | |
686 | if (dump_fd == -1) { | |
687 | err = errno; | |
688 | free(realpath); | |
689 | close(fd); | |
690 | return (err); | |
691 | } | |
692 | } else { | |
693 | dump_fd = -1; | |
694 | } | |
695 | ||
696 | free(realpath); | |
697 | ||
8d4e8140 | 698 | if (fstat64_blk(fd, &st) == -1) { |
4d58b69d | 699 | err = errno; |
34dc7c2f | 700 | close(fd); |
e2c292bb | 701 | if (dump_fd != -1) |
702 | close(dump_fd); | |
4d58b69d | 703 | return (err); |
34dc7c2f BB |
704 | } |
705 | ||
706 | (void) fcntl(fd, F_SETFD, FD_CLOEXEC); | |
707 | ||
708 | *vpp = vp = umem_zalloc(sizeof (vnode_t), UMEM_NOFAIL); | |
709 | ||
710 | vp->v_fd = fd; | |
711 | vp->v_size = st.st_size; | |
712 | vp->v_path = spa_strdup(path); | |
9867e8be | 713 | vp->v_dump_fd = dump_fd; |
34dc7c2f BB |
714 | |
715 | return (0); | |
716 | } | |
717 | ||
718 | /*ARGSUSED*/ | |
719 | int | |
720 | vn_openat(char *path, int x1, int flags, int mode, vnode_t **vpp, int x2, | |
721 | int x3, vnode_t *startvp, int fd) | |
722 | { | |
723 | char *realpath = umem_alloc(strlen(path) + 2, UMEM_NOFAIL); | |
724 | int ret; | |
725 | ||
726 | ASSERT(startvp == rootdir); | |
727 | (void) sprintf(realpath, "/%s", path); | |
728 | ||
729 | /* fd ignored for now, need if want to simulate nbmand support */ | |
730 | ret = vn_open(realpath, x1, flags, mode, vpp, x2, x3); | |
731 | ||
732 | umem_free(realpath, strlen(path) + 2); | |
733 | ||
734 | return (ret); | |
735 | } | |
736 | ||
737 | /*ARGSUSED*/ | |
738 | int | |
739 | vn_rdwr(int uio, vnode_t *vp, void *addr, ssize_t len, offset_t offset, | |
740 | int x1, int x2, rlim64_t x3, void *x4, ssize_t *residp) | |
741 | { | |
4d58b69d | 742 | ssize_t rc, done = 0, split; |
34dc7c2f BB |
743 | |
744 | if (uio == UIO_READ) { | |
4d58b69d | 745 | rc = pread64(vp->v_fd, addr, len, offset); |
470f12d6 | 746 | if (vp->v_dump_fd != -1 && rc != -1) { |
928c58dd BB |
747 | int status; |
748 | status = pwrite64(vp->v_dump_fd, addr, rc, offset); | |
9867e8be MA |
749 | ASSERT(status != -1); |
750 | } | |
34dc7c2f BB |
751 | } else { |
752 | /* | |
753 | * To simulate partial disk writes, we split writes into two | |
754 | * system calls so that the process can be killed in between. | |
755 | */ | |
9ae529ec CS |
756 | int sectors = len >> SPA_MINBLOCKSHIFT; |
757 | split = (sectors > 0 ? rand() % sectors : 0) << | |
758 | SPA_MINBLOCKSHIFT; | |
4d58b69d RC |
759 | rc = pwrite64(vp->v_fd, addr, split, offset); |
760 | if (rc != -1) { | |
761 | done = rc; | |
762 | rc = pwrite64(vp->v_fd, (char *)addr + split, | |
763 | len - split, offset + split); | |
764 | } | |
34dc7c2f BB |
765 | } |
766 | ||
d603ed6c BB |
767 | #ifdef __linux__ |
768 | if (rc == -1 && errno == EINVAL) { | |
769 | /* | |
770 | * Under Linux, this most likely means an alignment issue | |
771 | * (memory or disk) due to O_DIRECT, so we abort() in order to | |
772 | * catch the offender. | |
773 | */ | |
d1d7e268 | 774 | abort(); |
d603ed6c BB |
775 | } |
776 | #endif | |
4d58b69d | 777 | if (rc == -1) |
34dc7c2f | 778 | return (errno); |
4d58b69d RC |
779 | |
780 | done += rc; | |
781 | ||
34dc7c2f | 782 | if (residp) |
4d58b69d RC |
783 | *residp = len - done; |
784 | else if (done != len) | |
34dc7c2f BB |
785 | return (EIO); |
786 | return (0); | |
787 | } | |
788 | ||
789 | void | |
790 | vn_close(vnode_t *vp) | |
791 | { | |
792 | close(vp->v_fd); | |
9867e8be MA |
793 | if (vp->v_dump_fd != -1) |
794 | close(vp->v_dump_fd); | |
34dc7c2f BB |
795 | spa_strfree(vp->v_path); |
796 | umem_free(vp, sizeof (vnode_t)); | |
797 | } | |
798 | ||
428870ff BB |
799 | /* |
800 | * At a minimum we need to update the size since vdev_reopen() | |
801 | * will no longer call vn_openat(). | |
802 | */ | |
803 | int | |
804 | fop_getattr(vnode_t *vp, vattr_t *vap) | |
805 | { | |
806 | struct stat64 st; | |
8d4e8140 | 807 | int err; |
428870ff | 808 | |
8d4e8140 RC |
809 | if (fstat64_blk(vp->v_fd, &st) == -1) { |
810 | err = errno; | |
428870ff | 811 | close(vp->v_fd); |
8d4e8140 | 812 | return (err); |
428870ff BB |
813 | } |
814 | ||
815 | vap->va_size = st.st_size; | |
816 | return (0); | |
817 | } | |
818 | ||
34dc7c2f BB |
819 | /* |
820 | * ========================================================================= | |
821 | * Figure out which debugging statements to print | |
822 | * ========================================================================= | |
823 | */ | |
824 | ||
825 | static char *dprintf_string; | |
826 | static int dprintf_print_all; | |
827 | ||
828 | int | |
829 | dprintf_find_string(const char *string) | |
830 | { | |
831 | char *tmp_str = dprintf_string; | |
832 | int len = strlen(string); | |
833 | ||
834 | /* | |
835 | * Find out if this is a string we want to print. | |
836 | * String format: file1.c,function_name1,file2.c,file3.c | |
837 | */ | |
838 | ||
839 | while (tmp_str != NULL) { | |
840 | if (strncmp(tmp_str, string, len) == 0 && | |
841 | (tmp_str[len] == ',' || tmp_str[len] == '\0')) | |
842 | return (1); | |
843 | tmp_str = strchr(tmp_str, ','); | |
844 | if (tmp_str != NULL) | |
845 | tmp_str++; /* Get rid of , */ | |
846 | } | |
847 | return (0); | |
848 | } | |
849 | ||
850 | void | |
851 | dprintf_setup(int *argc, char **argv) | |
852 | { | |
853 | int i, j; | |
854 | ||
855 | /* | |
856 | * Debugging can be specified two ways: by setting the | |
857 | * environment variable ZFS_DEBUG, or by including a | |
858 | * "debug=..." argument on the command line. The command | |
859 | * line setting overrides the environment variable. | |
860 | */ | |
861 | ||
862 | for (i = 1; i < *argc; i++) { | |
863 | int len = strlen("debug="); | |
864 | /* First look for a command line argument */ | |
865 | if (strncmp("debug=", argv[i], len) == 0) { | |
866 | dprintf_string = argv[i] + len; | |
867 | /* Remove from args */ | |
868 | for (j = i; j < *argc; j++) | |
869 | argv[j] = argv[j+1]; | |
870 | argv[j] = NULL; | |
871 | (*argc)--; | |
872 | } | |
873 | } | |
874 | ||
875 | if (dprintf_string == NULL) { | |
876 | /* Look for ZFS_DEBUG environment variable */ | |
877 | dprintf_string = getenv("ZFS_DEBUG"); | |
878 | } | |
879 | ||
880 | /* | |
881 | * Are we just turning on all debugging? | |
882 | */ | |
883 | if (dprintf_find_string("on")) | |
884 | dprintf_print_all = 1; | |
308a451f MA |
885 | |
886 | if (dprintf_string != NULL) | |
887 | zfs_flags |= ZFS_DEBUG_DPRINTF; | |
34dc7c2f BB |
888 | } |
889 | ||
890 | /* | |
891 | * ========================================================================= | |
892 | * debug printfs | |
893 | * ========================================================================= | |
894 | */ | |
895 | void | |
896 | __dprintf(const char *file, const char *func, int line, const char *fmt, ...) | |
897 | { | |
898 | const char *newfile; | |
899 | va_list adx; | |
900 | ||
901 | /* | |
902 | * Get rid of annoying "../common/" prefix to filename. | |
903 | */ | |
904 | newfile = strrchr(file, '/'); | |
905 | if (newfile != NULL) { | |
906 | newfile = newfile + 1; /* Get rid of leading / */ | |
907 | } else { | |
908 | newfile = file; | |
909 | } | |
910 | ||
911 | if (dprintf_print_all || | |
912 | dprintf_find_string(newfile) || | |
913 | dprintf_find_string(func)) { | |
914 | /* Print out just the function name if requested */ | |
915 | flockfile(stdout); | |
916 | if (dprintf_find_string("pid")) | |
917 | (void) printf("%d ", getpid()); | |
918 | if (dprintf_find_string("tid")) | |
1e33ac1e | 919 | (void) printf("%u ", (uint_t) pthread_self()); |
34dc7c2f BB |
920 | if (dprintf_find_string("cpu")) |
921 | (void) printf("%u ", getcpuid()); | |
922 | if (dprintf_find_string("time")) | |
923 | (void) printf("%llu ", gethrtime()); | |
924 | if (dprintf_find_string("long")) | |
925 | (void) printf("%s, line %d: ", newfile, line); | |
926 | (void) printf("%s: ", func); | |
927 | va_start(adx, fmt); | |
928 | (void) vprintf(fmt, adx); | |
929 | va_end(adx); | |
930 | funlockfile(stdout); | |
931 | } | |
932 | } | |
933 | ||
34dc7c2f BB |
934 | /* |
935 | * ========================================================================= | |
936 | * cmn_err() and panic() | |
937 | * ========================================================================= | |
938 | */ | |
939 | static char ce_prefix[CE_IGNORE][10] = { "", "NOTICE: ", "WARNING: ", "" }; | |
940 | static char ce_suffix[CE_IGNORE][2] = { "", "\n", "\n", "" }; | |
941 | ||
942 | void | |
943 | vpanic(const char *fmt, va_list adx) | |
944 | { | |
945 | (void) fprintf(stderr, "error: "); | |
946 | (void) vfprintf(stderr, fmt, adx); | |
947 | (void) fprintf(stderr, "\n"); | |
948 | ||
949 | abort(); /* think of it as a "user-level crash dump" */ | |
950 | } | |
951 | ||
952 | void | |
953 | panic(const char *fmt, ...) | |
954 | { | |
955 | va_list adx; | |
956 | ||
957 | va_start(adx, fmt); | |
958 | vpanic(fmt, adx); | |
959 | va_end(adx); | |
960 | } | |
961 | ||
962 | void | |
963 | vcmn_err(int ce, const char *fmt, va_list adx) | |
964 | { | |
965 | if (ce == CE_PANIC) | |
966 | vpanic(fmt, adx); | |
967 | if (ce != CE_NOTE) { /* suppress noise in userland stress testing */ | |
968 | (void) fprintf(stderr, "%s", ce_prefix[ce]); | |
969 | (void) vfprintf(stderr, fmt, adx); | |
970 | (void) fprintf(stderr, "%s", ce_suffix[ce]); | |
971 | } | |
972 | } | |
973 | ||
974 | /*PRINTFLIKE2*/ | |
975 | void | |
976 | cmn_err(int ce, const char *fmt, ...) | |
977 | { | |
978 | va_list adx; | |
979 | ||
980 | va_start(adx, fmt); | |
981 | vcmn_err(ce, fmt, adx); | |
982 | va_end(adx); | |
983 | } | |
984 | ||
985 | /* | |
986 | * ========================================================================= | |
987 | * kobj interfaces | |
988 | * ========================================================================= | |
989 | */ | |
990 | struct _buf * | |
991 | kobj_open_file(char *name) | |
992 | { | |
993 | struct _buf *file; | |
994 | vnode_t *vp; | |
995 | ||
996 | /* set vp as the _fd field of the file */ | |
997 | if (vn_openat(name, UIO_SYSSPACE, FREAD, 0, &vp, 0, 0, rootdir, | |
998 | -1) != 0) | |
999 | return ((void *)-1UL); | |
1000 | ||
1001 | file = umem_zalloc(sizeof (struct _buf), UMEM_NOFAIL); | |
1002 | file->_fd = (intptr_t)vp; | |
1003 | return (file); | |
1004 | } | |
1005 | ||
1006 | int | |
1007 | kobj_read_file(struct _buf *file, char *buf, unsigned size, unsigned off) | |
1008 | { | |
689f093e | 1009 | ssize_t resid = 0; |
34dc7c2f | 1010 | |
957dc932 RY |
1011 | if (vn_rdwr(UIO_READ, (vnode_t *)file->_fd, buf, size, (offset_t)off, |
1012 | UIO_SYSSPACE, 0, 0, 0, &resid) != 0) | |
1013 | return (-1); | |
34dc7c2f BB |
1014 | |
1015 | return (size - resid); | |
1016 | } | |
1017 | ||
1018 | void | |
1019 | kobj_close_file(struct _buf *file) | |
1020 | { | |
1021 | vn_close((vnode_t *)file->_fd); | |
1022 | umem_free(file, sizeof (struct _buf)); | |
1023 | } | |
1024 | ||
1025 | int | |
1026 | kobj_get_filesize(struct _buf *file, uint64_t *size) | |
1027 | { | |
1028 | struct stat64 st; | |
1029 | vnode_t *vp = (vnode_t *)file->_fd; | |
1030 | ||
1031 | if (fstat64(vp->v_fd, &st) == -1) { | |
1032 | vn_close(vp); | |
1033 | return (errno); | |
1034 | } | |
1035 | *size = st.st_size; | |
1036 | return (0); | |
1037 | } | |
1038 | ||
1039 | /* | |
1040 | * ========================================================================= | |
1041 | * misc routines | |
1042 | * ========================================================================= | |
1043 | */ | |
1044 | ||
1045 | void | |
1046 | delay(clock_t ticks) | |
1047 | { | |
1048 | poll(0, 0, ticks * (1000 / hz)); | |
1049 | } | |
1050 | ||
1051 | /* | |
1052 | * Find highest one bit set. | |
1053 | * Returns bit number + 1 of highest bit that is set, otherwise returns 0. | |
1054 | * High order bit is 31 (or 63 in _LP64 kernel). | |
1055 | */ | |
1056 | int | |
9bd274dd | 1057 | highbit64(uint64_t i) |
34dc7c2f BB |
1058 | { |
1059 | register int h = 1; | |
1060 | ||
1061 | if (i == 0) | |
1062 | return (0); | |
9bd274dd | 1063 | if (i & 0xffffffff00000000ULL) { |
34dc7c2f BB |
1064 | h += 32; i >>= 32; |
1065 | } | |
34dc7c2f BB |
1066 | if (i & 0xffff0000) { |
1067 | h += 16; i >>= 16; | |
1068 | } | |
1069 | if (i & 0xff00) { | |
1070 | h += 8; i >>= 8; | |
1071 | } | |
1072 | if (i & 0xf0) { | |
1073 | h += 4; i >>= 4; | |
1074 | } | |
1075 | if (i & 0xc) { | |
1076 | h += 2; i >>= 2; | |
1077 | } | |
1078 | if (i & 0x2) { | |
1079 | h += 1; | |
1080 | } | |
1081 | return (h); | |
1082 | } | |
1083 | ||
193a37cb TH |
1084 | /* |
1085 | * Find lowest one bit set. | |
1086 | * Returns bit number + 1 of lowest bit that is set, otherwise returns 0. | |
1087 | * This is basically a reimplementation of ffsll(), which is GNU specific. | |
1088 | */ | |
1089 | int | |
1090 | lowbit64(uint64_t i) | |
1091 | { | |
1092 | register int h = 64; | |
1093 | if (i == 0) | |
1094 | return (0); | |
1095 | ||
1096 | if (i & 0x00000000ffffffffULL) | |
1097 | h -= 32; | |
1098 | else | |
1099 | i >>= 32; | |
1100 | ||
1101 | if (i & 0x0000ffff) | |
1102 | h -= 16; | |
1103 | else | |
1104 | i >>= 16; | |
1105 | ||
1106 | if (i & 0x00ff) | |
1107 | h -= 8; | |
1108 | else | |
1109 | i >>= 8; | |
1110 | ||
1111 | if (i & 0x0f) | |
1112 | h -= 4; | |
1113 | else | |
1114 | i >>= 4; | |
1115 | ||
1116 | if (i & 0x3) | |
1117 | h -= 2; | |
1118 | else | |
1119 | i >>= 2; | |
1120 | ||
1121 | if (i & 0x1) | |
1122 | h -= 1; | |
1123 | ||
1124 | return (h); | |
1125 | } | |
1126 | ||
0b04990a TC |
1127 | /* |
1128 | * Find highest one bit set. | |
1129 | * Returns bit number + 1 of highest bit that is set, otherwise returns 0. | |
1130 | * High order bit is 31 (or 63 in _LP64 kernel). | |
1131 | */ | |
1132 | int | |
1133 | highbit(ulong_t i) | |
1134 | { | |
1135 | register int h = 1; | |
1136 | ||
1137 | if (i == 0) | |
1138 | return (0); | |
1139 | #ifdef _LP64 | |
1140 | if (i & 0xffffffff00000000ul) { | |
1141 | h += 32; i >>= 32; | |
1142 | } | |
1143 | #endif | |
1144 | if (i & 0xffff0000) { | |
1145 | h += 16; i >>= 16; | |
1146 | } | |
1147 | if (i & 0xff00) { | |
1148 | h += 8; i >>= 8; | |
1149 | } | |
1150 | if (i & 0xf0) { | |
1151 | h += 4; i >>= 4; | |
1152 | } | |
1153 | if (i & 0xc) { | |
1154 | h += 2; i >>= 2; | |
1155 | } | |
1156 | if (i & 0x2) { | |
1157 | h += 1; | |
1158 | } | |
1159 | return (h); | |
1160 | } | |
1161 | ||
1162 | /* | |
1163 | * Find lowest one bit set. | |
1164 | * Returns bit number + 1 of lowest bit that is set, otherwise returns 0. | |
1165 | * Low order bit is 0. | |
1166 | */ | |
1167 | int | |
1168 | lowbit(ulong_t i) | |
1169 | { | |
1170 | register int h = 1; | |
1171 | ||
1172 | if (i == 0) | |
1173 | return (0); | |
1174 | ||
1175 | #ifdef _LP64 | |
1176 | if (!(i & 0xffffffff)) { | |
1177 | h += 32; i >>= 32; | |
1178 | } | |
1179 | #endif | |
1180 | if (!(i & 0xffff)) { | |
1181 | h += 16; i >>= 16; | |
1182 | } | |
1183 | if (!(i & 0xff)) { | |
1184 | h += 8; i >>= 8; | |
1185 | } | |
1186 | if (!(i & 0xf)) { | |
1187 | h += 4; i >>= 4; | |
1188 | } | |
1189 | if (!(i & 0x3)) { | |
1190 | h += 2; i >>= 2; | |
1191 | } | |
1192 | if (!(i & 0x1)) { | |
1193 | h += 1; | |
1194 | } | |
1195 | return (h); | |
1196 | } | |
193a37cb | 1197 | |
34dc7c2f BB |
1198 | static int random_fd = -1, urandom_fd = -1; |
1199 | ||
0b04990a TC |
1200 | void |
1201 | random_init(void) | |
1202 | { | |
1203 | VERIFY((random_fd = open("/dev/random", O_RDONLY)) != -1); | |
1204 | VERIFY((urandom_fd = open("/dev/urandom", O_RDONLY)) != -1); | |
1205 | } | |
1206 | ||
1207 | void | |
1208 | random_fini(void) | |
1209 | { | |
1210 | close(random_fd); | |
1211 | close(urandom_fd); | |
1212 | ||
1213 | random_fd = -1; | |
1214 | urandom_fd = -1; | |
1215 | } | |
1216 | ||
34dc7c2f BB |
1217 | static int |
1218 | random_get_bytes_common(uint8_t *ptr, size_t len, int fd) | |
1219 | { | |
1220 | size_t resid = len; | |
1221 | ssize_t bytes; | |
1222 | ||
1223 | ASSERT(fd != -1); | |
1224 | ||
1225 | while (resid != 0) { | |
1226 | bytes = read(fd, ptr, resid); | |
1227 | ASSERT3S(bytes, >=, 0); | |
1228 | ptr += bytes; | |
1229 | resid -= bytes; | |
1230 | } | |
1231 | ||
1232 | return (0); | |
1233 | } | |
1234 | ||
1235 | int | |
1236 | random_get_bytes(uint8_t *ptr, size_t len) | |
1237 | { | |
1238 | return (random_get_bytes_common(ptr, len, random_fd)); | |
1239 | } | |
1240 | ||
1241 | int | |
1242 | random_get_pseudo_bytes(uint8_t *ptr, size_t len) | |
1243 | { | |
1244 | return (random_get_bytes_common(ptr, len, urandom_fd)); | |
1245 | } | |
1246 | ||
1247 | int | |
1248 | ddi_strtoul(const char *hw_serial, char **nptr, int base, unsigned long *result) | |
1249 | { | |
1250 | char *end; | |
1251 | ||
1252 | *result = strtoul(hw_serial, &end, base); | |
1253 | if (*result == 0) | |
1254 | return (errno); | |
1255 | return (0); | |
1256 | } | |
1257 | ||
428870ff BB |
1258 | int |
1259 | ddi_strtoull(const char *str, char **nptr, int base, u_longlong_t *result) | |
1260 | { | |
1261 | char *end; | |
1262 | ||
1263 | *result = strtoull(str, &end, base); | |
1264 | if (*result == 0) | |
1265 | return (errno); | |
1266 | return (0); | |
1267 | } | |
1268 | ||
f0e324f2 BB |
1269 | utsname_t * |
1270 | utsname(void) | |
1271 | { | |
1272 | return (&hw_utsname); | |
1273 | } | |
1274 | ||
34dc7c2f BB |
1275 | /* |
1276 | * ========================================================================= | |
1277 | * kernel emulation setup & teardown | |
1278 | * ========================================================================= | |
1279 | */ | |
1280 | static int | |
1281 | umem_out_of_memory(void) | |
1282 | { | |
1283 | char errmsg[] = "out of memory -- generating core dump\n"; | |
1284 | ||
0e5b68e0 | 1285 | (void) fprintf(stderr, "%s", errmsg); |
34dc7c2f BB |
1286 | abort(); |
1287 | return (0); | |
1288 | } | |
1289 | ||
53698a45 CC |
1290 | static unsigned long |
1291 | get_spl_hostid(void) | |
1292 | { | |
1293 | FILE *f; | |
1294 | unsigned long hostid; | |
1295 | ||
1296 | f = fopen("/sys/module/spl/parameters/spl_hostid", "r"); | |
1297 | if (!f) | |
1298 | return (0); | |
1299 | if (fscanf(f, "%lu", &hostid) != 1) | |
1300 | hostid = 0; | |
1301 | fclose(f); | |
1302 | return (hostid & 0xffffffff); | |
1303 | } | |
1304 | ||
1305 | unsigned long | |
1306 | get_system_hostid(void) | |
1307 | { | |
1308 | unsigned long system_hostid = get_spl_hostid(); | |
1309 | if (system_hostid == 0) | |
1310 | system_hostid = gethostid() & 0xffffffff; | |
1311 | return (system_hostid); | |
1312 | } | |
1313 | ||
34dc7c2f BB |
1314 | void |
1315 | kernel_init(int mode) | |
1316 | { | |
13fe0198 MA |
1317 | extern uint_t rrw_tsd_key; |
1318 | ||
34dc7c2f BB |
1319 | umem_nofail_callback(umem_out_of_memory); |
1320 | ||
1321 | physmem = sysconf(_SC_PHYS_PAGES); | |
1322 | ||
1323 | dprintf("physmem = %llu pages (%.2f GB)\n", physmem, | |
1324 | (double)physmem * sysconf(_SC_PAGE_SIZE) / (1ULL << 30)); | |
1325 | ||
428870ff | 1326 | (void) snprintf(hw_serial, sizeof (hw_serial), "%ld", |
53698a45 | 1327 | (mode & FWRITE) ? get_system_hostid() : 0); |
34dc7c2f | 1328 | |
0b04990a TC |
1329 | random_init(); |
1330 | ||
f0e324f2 | 1331 | VERIFY0(uname(&hw_utsname)); |
34dc7c2f | 1332 | |
1e33ac1e | 1333 | thread_init(); |
b128c09f | 1334 | system_taskq_init(); |
0b04990a | 1335 | icp_init(); |
b128c09f | 1336 | |
34dc7c2f | 1337 | spa_init(mode); |
13fe0198 | 1338 | |
1eeb4562 JX |
1339 | fletcher_4_init(); |
1340 | ||
13fe0198 | 1341 | tsd_create(&rrw_tsd_key, rrw_tsd_destroy); |
34dc7c2f BB |
1342 | } |
1343 | ||
1344 | void | |
1345 | kernel_fini(void) | |
1346 | { | |
1eeb4562 | 1347 | fletcher_4_fini(); |
34dc7c2f BB |
1348 | spa_fini(); |
1349 | ||
0b04990a | 1350 | icp_fini(); |
428870ff | 1351 | system_taskq_fini(); |
1e33ac1e | 1352 | thread_fini(); |
428870ff | 1353 | |
0b04990a | 1354 | random_fini(); |
34dc7c2f BB |
1355 | } |
1356 | ||
34dc7c2f BB |
1357 | uid_t |
1358 | crgetuid(cred_t *cr) | |
1359 | { | |
1360 | return (0); | |
1361 | } | |
1362 | ||
6f1ffb06 MA |
1363 | uid_t |
1364 | crgetruid(cred_t *cr) | |
1365 | { | |
1366 | return (0); | |
1367 | } | |
1368 | ||
34dc7c2f BB |
1369 | gid_t |
1370 | crgetgid(cred_t *cr) | |
1371 | { | |
1372 | return (0); | |
1373 | } | |
1374 | ||
1375 | int | |
1376 | crgetngroups(cred_t *cr) | |
1377 | { | |
1378 | return (0); | |
1379 | } | |
1380 | ||
1381 | gid_t * | |
1382 | crgetgroups(cred_t *cr) | |
1383 | { | |
1384 | return (NULL); | |
1385 | } | |
1386 | ||
1387 | int | |
1388 | zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr) | |
1389 | { | |
1390 | return (0); | |
1391 | } | |
1392 | ||
1393 | int | |
1394 | zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr) | |
1395 | { | |
1396 | return (0); | |
1397 | } | |
1398 | ||
1399 | int | |
1400 | zfs_secpolicy_destroy_perms(const char *name, cred_t *cr) | |
1401 | { | |
1402 | return (0); | |
1403 | } | |
1404 | ||
f74b821a BB |
1405 | int |
1406 | secpolicy_zfs(const cred_t *cr) | |
1407 | { | |
1408 | return (0); | |
1409 | } | |
1410 | ||
34dc7c2f BB |
1411 | ksiddomain_t * |
1412 | ksid_lookupdomain(const char *dom) | |
1413 | { | |
1414 | ksiddomain_t *kd; | |
1415 | ||
1416 | kd = umem_zalloc(sizeof (ksiddomain_t), UMEM_NOFAIL); | |
1417 | kd->kd_name = spa_strdup(dom); | |
1418 | return (kd); | |
1419 | } | |
1420 | ||
1421 | void | |
1422 | ksiddomain_rele(ksiddomain_t *ksid) | |
1423 | { | |
1424 | spa_strfree(ksid->kd_name); | |
1425 | umem_free(ksid, sizeof (ksiddomain_t)); | |
1426 | } | |
428870ff | 1427 | |
428870ff | 1428 | char * |
00b46022 | 1429 | kmem_vasprintf(const char *fmt, va_list adx) |
428870ff | 1430 | { |
00b46022 BB |
1431 | char *buf = NULL; |
1432 | va_list adx_copy; | |
428870ff | 1433 | |
00b46022 BB |
1434 | va_copy(adx_copy, adx); |
1435 | VERIFY(vasprintf(&buf, fmt, adx_copy) != -1); | |
1436 | va_end(adx_copy); | |
428870ff | 1437 | |
00b46022 BB |
1438 | return (buf); |
1439 | } | |
1440 | ||
1441 | char * | |
1442 | kmem_asprintf(const char *fmt, ...) | |
1443 | { | |
1444 | char *buf = NULL; | |
1445 | va_list adx; | |
428870ff BB |
1446 | |
1447 | va_start(adx, fmt); | |
00b46022 | 1448 | VERIFY(vasprintf(&buf, fmt, adx) != -1); |
428870ff BB |
1449 | va_end(adx); |
1450 | ||
1451 | return (buf); | |
1452 | } | |
572e2857 BB |
1453 | |
1454 | /* ARGSUSED */ | |
1455 | int | |
1456 | zfs_onexit_fd_hold(int fd, minor_t *minorp) | |
1457 | { | |
1458 | *minorp = 0; | |
1459 | return (0); | |
1460 | } | |
1461 | ||
1462 | /* ARGSUSED */ | |
1463 | void | |
1464 | zfs_onexit_fd_rele(int fd) | |
1465 | { | |
1466 | } | |
1467 | ||
1468 | /* ARGSUSED */ | |
1469 | int | |
1470 | zfs_onexit_add_cb(minor_t minor, void (*func)(void *), void *data, | |
1471 | uint64_t *action_handle) | |
1472 | { | |
1473 | return (0); | |
1474 | } | |
1475 | ||
1476 | /* ARGSUSED */ | |
1477 | int | |
1478 | zfs_onexit_del_cb(minor_t minor, uint64_t action_handle, boolean_t fire) | |
1479 | { | |
1480 | return (0); | |
1481 | } | |
1482 | ||
1483 | /* ARGSUSED */ | |
1484 | int | |
1485 | zfs_onexit_cb_data(minor_t minor, uint64_t action_handle, void **data) | |
1486 | { | |
1487 | return (0); | |
1488 | } | |
92119cc2 BB |
1489 | |
1490 | fstrans_cookie_t | |
1491 | spl_fstrans_mark(void) | |
1492 | { | |
1493 | return ((fstrans_cookie_t) 0); | |
1494 | } | |
1495 | ||
1496 | void | |
1497 | spl_fstrans_unmark(fstrans_cookie_t cookie) | |
1498 | { | |
1499 | } | |
1500 | ||
1501 | int | |
1502 | spl_fstrans_check(void) | |
1503 | { | |
1504 | return (0); | |
1505 | } | |
a0bd735a | 1506 | |
47dfff3b MA |
1507 | void *zvol_tag = "zvol_tag"; |
1508 | ||
a0bd735a BP |
1509 | void |
1510 | zvol_create_minors(spa_t *spa, const char *name, boolean_t async) | |
1511 | { | |
1512 | } | |
1513 | ||
1514 | void | |
1515 | zvol_remove_minor(spa_t *spa, const char *name, boolean_t async) | |
1516 | { | |
1517 | } | |
1518 | ||
1519 | void | |
1520 | zvol_remove_minors(spa_t *spa, const char *name, boolean_t async) | |
1521 | { | |
1522 | } | |
1523 | ||
1524 | void | |
1525 | zvol_rename_minors(spa_t *spa, const char *oldname, const char *newname, | |
1526 | boolean_t async) | |
1527 | { | |
1528 | } |