]>
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 | { | |
602 | int fd; | |
9867e8be | 603 | int dump_fd; |
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); |
4d58b69d | 701 | return (err); |
34dc7c2f BB |
702 | } |
703 | ||
704 | (void) fcntl(fd, F_SETFD, FD_CLOEXEC); | |
705 | ||
706 | *vpp = vp = umem_zalloc(sizeof (vnode_t), UMEM_NOFAIL); | |
707 | ||
708 | vp->v_fd = fd; | |
709 | vp->v_size = st.st_size; | |
710 | vp->v_path = spa_strdup(path); | |
9867e8be | 711 | vp->v_dump_fd = dump_fd; |
34dc7c2f BB |
712 | |
713 | return (0); | |
714 | } | |
715 | ||
716 | /*ARGSUSED*/ | |
717 | int | |
718 | vn_openat(char *path, int x1, int flags, int mode, vnode_t **vpp, int x2, | |
719 | int x3, vnode_t *startvp, int fd) | |
720 | { | |
721 | char *realpath = umem_alloc(strlen(path) + 2, UMEM_NOFAIL); | |
722 | int ret; | |
723 | ||
724 | ASSERT(startvp == rootdir); | |
725 | (void) sprintf(realpath, "/%s", path); | |
726 | ||
727 | /* fd ignored for now, need if want to simulate nbmand support */ | |
728 | ret = vn_open(realpath, x1, flags, mode, vpp, x2, x3); | |
729 | ||
730 | umem_free(realpath, strlen(path) + 2); | |
731 | ||
732 | return (ret); | |
733 | } | |
734 | ||
735 | /*ARGSUSED*/ | |
736 | int | |
737 | vn_rdwr(int uio, vnode_t *vp, void *addr, ssize_t len, offset_t offset, | |
738 | int x1, int x2, rlim64_t x3, void *x4, ssize_t *residp) | |
739 | { | |
4d58b69d | 740 | ssize_t rc, done = 0, split; |
34dc7c2f BB |
741 | |
742 | if (uio == UIO_READ) { | |
4d58b69d | 743 | rc = pread64(vp->v_fd, addr, len, offset); |
470f12d6 | 744 | if (vp->v_dump_fd != -1 && rc != -1) { |
928c58dd BB |
745 | int status; |
746 | status = pwrite64(vp->v_dump_fd, addr, rc, offset); | |
9867e8be MA |
747 | ASSERT(status != -1); |
748 | } | |
34dc7c2f BB |
749 | } else { |
750 | /* | |
751 | * To simulate partial disk writes, we split writes into two | |
752 | * system calls so that the process can be killed in between. | |
753 | */ | |
9ae529ec CS |
754 | int sectors = len >> SPA_MINBLOCKSHIFT; |
755 | split = (sectors > 0 ? rand() % sectors : 0) << | |
756 | SPA_MINBLOCKSHIFT; | |
4d58b69d RC |
757 | rc = pwrite64(vp->v_fd, addr, split, offset); |
758 | if (rc != -1) { | |
759 | done = rc; | |
760 | rc = pwrite64(vp->v_fd, (char *)addr + split, | |
761 | len - split, offset + split); | |
762 | } | |
34dc7c2f BB |
763 | } |
764 | ||
d603ed6c BB |
765 | #ifdef __linux__ |
766 | if (rc == -1 && errno == EINVAL) { | |
767 | /* | |
768 | * Under Linux, this most likely means an alignment issue | |
769 | * (memory or disk) due to O_DIRECT, so we abort() in order to | |
770 | * catch the offender. | |
771 | */ | |
d1d7e268 | 772 | abort(); |
d603ed6c BB |
773 | } |
774 | #endif | |
4d58b69d | 775 | if (rc == -1) |
34dc7c2f | 776 | return (errno); |
4d58b69d RC |
777 | |
778 | done += rc; | |
779 | ||
34dc7c2f | 780 | if (residp) |
4d58b69d RC |
781 | *residp = len - done; |
782 | else if (done != len) | |
34dc7c2f BB |
783 | return (EIO); |
784 | return (0); | |
785 | } | |
786 | ||
787 | void | |
788 | vn_close(vnode_t *vp) | |
789 | { | |
790 | close(vp->v_fd); | |
9867e8be MA |
791 | if (vp->v_dump_fd != -1) |
792 | close(vp->v_dump_fd); | |
34dc7c2f BB |
793 | spa_strfree(vp->v_path); |
794 | umem_free(vp, sizeof (vnode_t)); | |
795 | } | |
796 | ||
428870ff BB |
797 | /* |
798 | * At a minimum we need to update the size since vdev_reopen() | |
799 | * will no longer call vn_openat(). | |
800 | */ | |
801 | int | |
802 | fop_getattr(vnode_t *vp, vattr_t *vap) | |
803 | { | |
804 | struct stat64 st; | |
8d4e8140 | 805 | int err; |
428870ff | 806 | |
8d4e8140 RC |
807 | if (fstat64_blk(vp->v_fd, &st) == -1) { |
808 | err = errno; | |
428870ff | 809 | close(vp->v_fd); |
8d4e8140 | 810 | return (err); |
428870ff BB |
811 | } |
812 | ||
813 | vap->va_size = st.st_size; | |
814 | return (0); | |
815 | } | |
816 | ||
34dc7c2f BB |
817 | /* |
818 | * ========================================================================= | |
819 | * Figure out which debugging statements to print | |
820 | * ========================================================================= | |
821 | */ | |
822 | ||
823 | static char *dprintf_string; | |
824 | static int dprintf_print_all; | |
825 | ||
826 | int | |
827 | dprintf_find_string(const char *string) | |
828 | { | |
829 | char *tmp_str = dprintf_string; | |
830 | int len = strlen(string); | |
831 | ||
832 | /* | |
833 | * Find out if this is a string we want to print. | |
834 | * String format: file1.c,function_name1,file2.c,file3.c | |
835 | */ | |
836 | ||
837 | while (tmp_str != NULL) { | |
838 | if (strncmp(tmp_str, string, len) == 0 && | |
839 | (tmp_str[len] == ',' || tmp_str[len] == '\0')) | |
840 | return (1); | |
841 | tmp_str = strchr(tmp_str, ','); | |
842 | if (tmp_str != NULL) | |
843 | tmp_str++; /* Get rid of , */ | |
844 | } | |
845 | return (0); | |
846 | } | |
847 | ||
848 | void | |
849 | dprintf_setup(int *argc, char **argv) | |
850 | { | |
851 | int i, j; | |
852 | ||
853 | /* | |
854 | * Debugging can be specified two ways: by setting the | |
855 | * environment variable ZFS_DEBUG, or by including a | |
856 | * "debug=..." argument on the command line. The command | |
857 | * line setting overrides the environment variable. | |
858 | */ | |
859 | ||
860 | for (i = 1; i < *argc; i++) { | |
861 | int len = strlen("debug="); | |
862 | /* First look for a command line argument */ | |
863 | if (strncmp("debug=", argv[i], len) == 0) { | |
864 | dprintf_string = argv[i] + len; | |
865 | /* Remove from args */ | |
866 | for (j = i; j < *argc; j++) | |
867 | argv[j] = argv[j+1]; | |
868 | argv[j] = NULL; | |
869 | (*argc)--; | |
870 | } | |
871 | } | |
872 | ||
873 | if (dprintf_string == NULL) { | |
874 | /* Look for ZFS_DEBUG environment variable */ | |
875 | dprintf_string = getenv("ZFS_DEBUG"); | |
876 | } | |
877 | ||
878 | /* | |
879 | * Are we just turning on all debugging? | |
880 | */ | |
881 | if (dprintf_find_string("on")) | |
882 | dprintf_print_all = 1; | |
308a451f MA |
883 | |
884 | if (dprintf_string != NULL) | |
885 | zfs_flags |= ZFS_DEBUG_DPRINTF; | |
34dc7c2f BB |
886 | } |
887 | ||
888 | /* | |
889 | * ========================================================================= | |
890 | * debug printfs | |
891 | * ========================================================================= | |
892 | */ | |
893 | void | |
894 | __dprintf(const char *file, const char *func, int line, const char *fmt, ...) | |
895 | { | |
896 | const char *newfile; | |
897 | va_list adx; | |
898 | ||
899 | /* | |
900 | * Get rid of annoying "../common/" prefix to filename. | |
901 | */ | |
902 | newfile = strrchr(file, '/'); | |
903 | if (newfile != NULL) { | |
904 | newfile = newfile + 1; /* Get rid of leading / */ | |
905 | } else { | |
906 | newfile = file; | |
907 | } | |
908 | ||
909 | if (dprintf_print_all || | |
910 | dprintf_find_string(newfile) || | |
911 | dprintf_find_string(func)) { | |
912 | /* Print out just the function name if requested */ | |
913 | flockfile(stdout); | |
914 | if (dprintf_find_string("pid")) | |
915 | (void) printf("%d ", getpid()); | |
916 | if (dprintf_find_string("tid")) | |
1e33ac1e | 917 | (void) printf("%u ", (uint_t) pthread_self()); |
34dc7c2f BB |
918 | if (dprintf_find_string("cpu")) |
919 | (void) printf("%u ", getcpuid()); | |
920 | if (dprintf_find_string("time")) | |
921 | (void) printf("%llu ", gethrtime()); | |
922 | if (dprintf_find_string("long")) | |
923 | (void) printf("%s, line %d: ", newfile, line); | |
924 | (void) printf("%s: ", func); | |
925 | va_start(adx, fmt); | |
926 | (void) vprintf(fmt, adx); | |
927 | va_end(adx); | |
928 | funlockfile(stdout); | |
929 | } | |
930 | } | |
931 | ||
34dc7c2f BB |
932 | /* |
933 | * ========================================================================= | |
934 | * cmn_err() and panic() | |
935 | * ========================================================================= | |
936 | */ | |
937 | static char ce_prefix[CE_IGNORE][10] = { "", "NOTICE: ", "WARNING: ", "" }; | |
938 | static char ce_suffix[CE_IGNORE][2] = { "", "\n", "\n", "" }; | |
939 | ||
940 | void | |
941 | vpanic(const char *fmt, va_list adx) | |
942 | { | |
943 | (void) fprintf(stderr, "error: "); | |
944 | (void) vfprintf(stderr, fmt, adx); | |
945 | (void) fprintf(stderr, "\n"); | |
946 | ||
947 | abort(); /* think of it as a "user-level crash dump" */ | |
948 | } | |
949 | ||
950 | void | |
951 | panic(const char *fmt, ...) | |
952 | { | |
953 | va_list adx; | |
954 | ||
955 | va_start(adx, fmt); | |
956 | vpanic(fmt, adx); | |
957 | va_end(adx); | |
958 | } | |
959 | ||
960 | void | |
961 | vcmn_err(int ce, const char *fmt, va_list adx) | |
962 | { | |
963 | if (ce == CE_PANIC) | |
964 | vpanic(fmt, adx); | |
965 | if (ce != CE_NOTE) { /* suppress noise in userland stress testing */ | |
966 | (void) fprintf(stderr, "%s", ce_prefix[ce]); | |
967 | (void) vfprintf(stderr, fmt, adx); | |
968 | (void) fprintf(stderr, "%s", ce_suffix[ce]); | |
969 | } | |
970 | } | |
971 | ||
972 | /*PRINTFLIKE2*/ | |
973 | void | |
974 | cmn_err(int ce, const char *fmt, ...) | |
975 | { | |
976 | va_list adx; | |
977 | ||
978 | va_start(adx, fmt); | |
979 | vcmn_err(ce, fmt, adx); | |
980 | va_end(adx); | |
981 | } | |
982 | ||
983 | /* | |
984 | * ========================================================================= | |
985 | * kobj interfaces | |
986 | * ========================================================================= | |
987 | */ | |
988 | struct _buf * | |
989 | kobj_open_file(char *name) | |
990 | { | |
991 | struct _buf *file; | |
992 | vnode_t *vp; | |
993 | ||
994 | /* set vp as the _fd field of the file */ | |
995 | if (vn_openat(name, UIO_SYSSPACE, FREAD, 0, &vp, 0, 0, rootdir, | |
996 | -1) != 0) | |
997 | return ((void *)-1UL); | |
998 | ||
999 | file = umem_zalloc(sizeof (struct _buf), UMEM_NOFAIL); | |
1000 | file->_fd = (intptr_t)vp; | |
1001 | return (file); | |
1002 | } | |
1003 | ||
1004 | int | |
1005 | kobj_read_file(struct _buf *file, char *buf, unsigned size, unsigned off) | |
1006 | { | |
689f093e | 1007 | ssize_t resid = 0; |
34dc7c2f | 1008 | |
957dc932 RY |
1009 | if (vn_rdwr(UIO_READ, (vnode_t *)file->_fd, buf, size, (offset_t)off, |
1010 | UIO_SYSSPACE, 0, 0, 0, &resid) != 0) | |
1011 | return (-1); | |
34dc7c2f BB |
1012 | |
1013 | return (size - resid); | |
1014 | } | |
1015 | ||
1016 | void | |
1017 | kobj_close_file(struct _buf *file) | |
1018 | { | |
1019 | vn_close((vnode_t *)file->_fd); | |
1020 | umem_free(file, sizeof (struct _buf)); | |
1021 | } | |
1022 | ||
1023 | int | |
1024 | kobj_get_filesize(struct _buf *file, uint64_t *size) | |
1025 | { | |
1026 | struct stat64 st; | |
1027 | vnode_t *vp = (vnode_t *)file->_fd; | |
1028 | ||
1029 | if (fstat64(vp->v_fd, &st) == -1) { | |
1030 | vn_close(vp); | |
1031 | return (errno); | |
1032 | } | |
1033 | *size = st.st_size; | |
1034 | return (0); | |
1035 | } | |
1036 | ||
1037 | /* | |
1038 | * ========================================================================= | |
1039 | * misc routines | |
1040 | * ========================================================================= | |
1041 | */ | |
1042 | ||
1043 | void | |
1044 | delay(clock_t ticks) | |
1045 | { | |
1046 | poll(0, 0, ticks * (1000 / hz)); | |
1047 | } | |
1048 | ||
1049 | /* | |
1050 | * Find highest one bit set. | |
1051 | * Returns bit number + 1 of highest bit that is set, otherwise returns 0. | |
1052 | * High order bit is 31 (or 63 in _LP64 kernel). | |
1053 | */ | |
1054 | int | |
9bd274dd | 1055 | highbit64(uint64_t i) |
34dc7c2f BB |
1056 | { |
1057 | register int h = 1; | |
1058 | ||
1059 | if (i == 0) | |
1060 | return (0); | |
9bd274dd | 1061 | if (i & 0xffffffff00000000ULL) { |
34dc7c2f BB |
1062 | h += 32; i >>= 32; |
1063 | } | |
34dc7c2f BB |
1064 | if (i & 0xffff0000) { |
1065 | h += 16; i >>= 16; | |
1066 | } | |
1067 | if (i & 0xff00) { | |
1068 | h += 8; i >>= 8; | |
1069 | } | |
1070 | if (i & 0xf0) { | |
1071 | h += 4; i >>= 4; | |
1072 | } | |
1073 | if (i & 0xc) { | |
1074 | h += 2; i >>= 2; | |
1075 | } | |
1076 | if (i & 0x2) { | |
1077 | h += 1; | |
1078 | } | |
1079 | return (h); | |
1080 | } | |
1081 | ||
193a37cb TH |
1082 | /* |
1083 | * Find lowest one bit set. | |
1084 | * Returns bit number + 1 of lowest bit that is set, otherwise returns 0. | |
1085 | * This is basically a reimplementation of ffsll(), which is GNU specific. | |
1086 | */ | |
1087 | int | |
1088 | lowbit64(uint64_t i) | |
1089 | { | |
1090 | register int h = 64; | |
1091 | if (i == 0) | |
1092 | return (0); | |
1093 | ||
1094 | if (i & 0x00000000ffffffffULL) | |
1095 | h -= 32; | |
1096 | else | |
1097 | i >>= 32; | |
1098 | ||
1099 | if (i & 0x0000ffff) | |
1100 | h -= 16; | |
1101 | else | |
1102 | i >>= 16; | |
1103 | ||
1104 | if (i & 0x00ff) | |
1105 | h -= 8; | |
1106 | else | |
1107 | i >>= 8; | |
1108 | ||
1109 | if (i & 0x0f) | |
1110 | h -= 4; | |
1111 | else | |
1112 | i >>= 4; | |
1113 | ||
1114 | if (i & 0x3) | |
1115 | h -= 2; | |
1116 | else | |
1117 | i >>= 2; | |
1118 | ||
1119 | if (i & 0x1) | |
1120 | h -= 1; | |
1121 | ||
1122 | return (h); | |
1123 | } | |
1124 | ||
0b04990a TC |
1125 | /* |
1126 | * Find highest one bit set. | |
1127 | * Returns bit number + 1 of highest bit that is set, otherwise returns 0. | |
1128 | * High order bit is 31 (or 63 in _LP64 kernel). | |
1129 | */ | |
1130 | int | |
1131 | highbit(ulong_t i) | |
1132 | { | |
1133 | register int h = 1; | |
1134 | ||
1135 | if (i == 0) | |
1136 | return (0); | |
1137 | #ifdef _LP64 | |
1138 | if (i & 0xffffffff00000000ul) { | |
1139 | h += 32; i >>= 32; | |
1140 | } | |
1141 | #endif | |
1142 | if (i & 0xffff0000) { | |
1143 | h += 16; i >>= 16; | |
1144 | } | |
1145 | if (i & 0xff00) { | |
1146 | h += 8; i >>= 8; | |
1147 | } | |
1148 | if (i & 0xf0) { | |
1149 | h += 4; i >>= 4; | |
1150 | } | |
1151 | if (i & 0xc) { | |
1152 | h += 2; i >>= 2; | |
1153 | } | |
1154 | if (i & 0x2) { | |
1155 | h += 1; | |
1156 | } | |
1157 | return (h); | |
1158 | } | |
1159 | ||
1160 | /* | |
1161 | * Find lowest one bit set. | |
1162 | * Returns bit number + 1 of lowest bit that is set, otherwise returns 0. | |
1163 | * Low order bit is 0. | |
1164 | */ | |
1165 | int | |
1166 | lowbit(ulong_t i) | |
1167 | { | |
1168 | register int h = 1; | |
1169 | ||
1170 | if (i == 0) | |
1171 | return (0); | |
1172 | ||
1173 | #ifdef _LP64 | |
1174 | if (!(i & 0xffffffff)) { | |
1175 | h += 32; i >>= 32; | |
1176 | } | |
1177 | #endif | |
1178 | if (!(i & 0xffff)) { | |
1179 | h += 16; i >>= 16; | |
1180 | } | |
1181 | if (!(i & 0xff)) { | |
1182 | h += 8; i >>= 8; | |
1183 | } | |
1184 | if (!(i & 0xf)) { | |
1185 | h += 4; i >>= 4; | |
1186 | } | |
1187 | if (!(i & 0x3)) { | |
1188 | h += 2; i >>= 2; | |
1189 | } | |
1190 | if (!(i & 0x1)) { | |
1191 | h += 1; | |
1192 | } | |
1193 | return (h); | |
1194 | } | |
193a37cb | 1195 | |
34dc7c2f BB |
1196 | static int random_fd = -1, urandom_fd = -1; |
1197 | ||
0b04990a TC |
1198 | void |
1199 | random_init(void) | |
1200 | { | |
1201 | VERIFY((random_fd = open("/dev/random", O_RDONLY)) != -1); | |
1202 | VERIFY((urandom_fd = open("/dev/urandom", O_RDONLY)) != -1); | |
1203 | } | |
1204 | ||
1205 | void | |
1206 | random_fini(void) | |
1207 | { | |
1208 | close(random_fd); | |
1209 | close(urandom_fd); | |
1210 | ||
1211 | random_fd = -1; | |
1212 | urandom_fd = -1; | |
1213 | } | |
1214 | ||
34dc7c2f BB |
1215 | static int |
1216 | random_get_bytes_common(uint8_t *ptr, size_t len, int fd) | |
1217 | { | |
1218 | size_t resid = len; | |
1219 | ssize_t bytes; | |
1220 | ||
1221 | ASSERT(fd != -1); | |
1222 | ||
1223 | while (resid != 0) { | |
1224 | bytes = read(fd, ptr, resid); | |
1225 | ASSERT3S(bytes, >=, 0); | |
1226 | ptr += bytes; | |
1227 | resid -= bytes; | |
1228 | } | |
1229 | ||
1230 | return (0); | |
1231 | } | |
1232 | ||
1233 | int | |
1234 | random_get_bytes(uint8_t *ptr, size_t len) | |
1235 | { | |
1236 | return (random_get_bytes_common(ptr, len, random_fd)); | |
1237 | } | |
1238 | ||
1239 | int | |
1240 | random_get_pseudo_bytes(uint8_t *ptr, size_t len) | |
1241 | { | |
1242 | return (random_get_bytes_common(ptr, len, urandom_fd)); | |
1243 | } | |
1244 | ||
1245 | int | |
1246 | ddi_strtoul(const char *hw_serial, char **nptr, int base, unsigned long *result) | |
1247 | { | |
1248 | char *end; | |
1249 | ||
1250 | *result = strtoul(hw_serial, &end, base); | |
1251 | if (*result == 0) | |
1252 | return (errno); | |
1253 | return (0); | |
1254 | } | |
1255 | ||
428870ff BB |
1256 | int |
1257 | ddi_strtoull(const char *str, char **nptr, int base, u_longlong_t *result) | |
1258 | { | |
1259 | char *end; | |
1260 | ||
1261 | *result = strtoull(str, &end, base); | |
1262 | if (*result == 0) | |
1263 | return (errno); | |
1264 | return (0); | |
1265 | } | |
1266 | ||
f0e324f2 BB |
1267 | utsname_t * |
1268 | utsname(void) | |
1269 | { | |
1270 | return (&hw_utsname); | |
1271 | } | |
1272 | ||
34dc7c2f BB |
1273 | /* |
1274 | * ========================================================================= | |
1275 | * kernel emulation setup & teardown | |
1276 | * ========================================================================= | |
1277 | */ | |
1278 | static int | |
1279 | umem_out_of_memory(void) | |
1280 | { | |
1281 | char errmsg[] = "out of memory -- generating core dump\n"; | |
1282 | ||
0e5b68e0 | 1283 | (void) fprintf(stderr, "%s", errmsg); |
34dc7c2f BB |
1284 | abort(); |
1285 | return (0); | |
1286 | } | |
1287 | ||
53698a45 CC |
1288 | static unsigned long |
1289 | get_spl_hostid(void) | |
1290 | { | |
1291 | FILE *f; | |
1292 | unsigned long hostid; | |
1293 | ||
1294 | f = fopen("/sys/module/spl/parameters/spl_hostid", "r"); | |
1295 | if (!f) | |
1296 | return (0); | |
1297 | if (fscanf(f, "%lu", &hostid) != 1) | |
1298 | hostid = 0; | |
1299 | fclose(f); | |
1300 | return (hostid & 0xffffffff); | |
1301 | } | |
1302 | ||
1303 | unsigned long | |
1304 | get_system_hostid(void) | |
1305 | { | |
1306 | unsigned long system_hostid = get_spl_hostid(); | |
1307 | if (system_hostid == 0) | |
1308 | system_hostid = gethostid() & 0xffffffff; | |
1309 | return (system_hostid); | |
1310 | } | |
1311 | ||
34dc7c2f BB |
1312 | void |
1313 | kernel_init(int mode) | |
1314 | { | |
13fe0198 MA |
1315 | extern uint_t rrw_tsd_key; |
1316 | ||
34dc7c2f BB |
1317 | umem_nofail_callback(umem_out_of_memory); |
1318 | ||
1319 | physmem = sysconf(_SC_PHYS_PAGES); | |
1320 | ||
1321 | dprintf("physmem = %llu pages (%.2f GB)\n", physmem, | |
1322 | (double)physmem * sysconf(_SC_PAGE_SIZE) / (1ULL << 30)); | |
1323 | ||
428870ff | 1324 | (void) snprintf(hw_serial, sizeof (hw_serial), "%ld", |
53698a45 | 1325 | (mode & FWRITE) ? get_system_hostid() : 0); |
34dc7c2f | 1326 | |
0b04990a TC |
1327 | random_init(); |
1328 | ||
f0e324f2 | 1329 | VERIFY0(uname(&hw_utsname)); |
34dc7c2f | 1330 | |
1e33ac1e | 1331 | thread_init(); |
b128c09f | 1332 | system_taskq_init(); |
0b04990a | 1333 | icp_init(); |
b128c09f | 1334 | |
34dc7c2f | 1335 | spa_init(mode); |
13fe0198 | 1336 | |
1eeb4562 JX |
1337 | fletcher_4_init(); |
1338 | ||
13fe0198 | 1339 | tsd_create(&rrw_tsd_key, rrw_tsd_destroy); |
34dc7c2f BB |
1340 | } |
1341 | ||
1342 | void | |
1343 | kernel_fini(void) | |
1344 | { | |
1eeb4562 | 1345 | fletcher_4_fini(); |
34dc7c2f BB |
1346 | spa_fini(); |
1347 | ||
0b04990a | 1348 | icp_fini(); |
428870ff | 1349 | system_taskq_fini(); |
1e33ac1e | 1350 | thread_fini(); |
428870ff | 1351 | |
0b04990a | 1352 | random_fini(); |
34dc7c2f BB |
1353 | } |
1354 | ||
34dc7c2f BB |
1355 | uid_t |
1356 | crgetuid(cred_t *cr) | |
1357 | { | |
1358 | return (0); | |
1359 | } | |
1360 | ||
6f1ffb06 MA |
1361 | uid_t |
1362 | crgetruid(cred_t *cr) | |
1363 | { | |
1364 | return (0); | |
1365 | } | |
1366 | ||
34dc7c2f BB |
1367 | gid_t |
1368 | crgetgid(cred_t *cr) | |
1369 | { | |
1370 | return (0); | |
1371 | } | |
1372 | ||
1373 | int | |
1374 | crgetngroups(cred_t *cr) | |
1375 | { | |
1376 | return (0); | |
1377 | } | |
1378 | ||
1379 | gid_t * | |
1380 | crgetgroups(cred_t *cr) | |
1381 | { | |
1382 | return (NULL); | |
1383 | } | |
1384 | ||
1385 | int | |
1386 | zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr) | |
1387 | { | |
1388 | return (0); | |
1389 | } | |
1390 | ||
1391 | int | |
1392 | zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr) | |
1393 | { | |
1394 | return (0); | |
1395 | } | |
1396 | ||
1397 | int | |
1398 | zfs_secpolicy_destroy_perms(const char *name, cred_t *cr) | |
1399 | { | |
1400 | return (0); | |
1401 | } | |
1402 | ||
f74b821a BB |
1403 | int |
1404 | secpolicy_zfs(const cred_t *cr) | |
1405 | { | |
1406 | return (0); | |
1407 | } | |
1408 | ||
34dc7c2f BB |
1409 | ksiddomain_t * |
1410 | ksid_lookupdomain(const char *dom) | |
1411 | { | |
1412 | ksiddomain_t *kd; | |
1413 | ||
1414 | kd = umem_zalloc(sizeof (ksiddomain_t), UMEM_NOFAIL); | |
1415 | kd->kd_name = spa_strdup(dom); | |
1416 | return (kd); | |
1417 | } | |
1418 | ||
1419 | void | |
1420 | ksiddomain_rele(ksiddomain_t *ksid) | |
1421 | { | |
1422 | spa_strfree(ksid->kd_name); | |
1423 | umem_free(ksid, sizeof (ksiddomain_t)); | |
1424 | } | |
428870ff | 1425 | |
428870ff | 1426 | char * |
00b46022 | 1427 | kmem_vasprintf(const char *fmt, va_list adx) |
428870ff | 1428 | { |
00b46022 BB |
1429 | char *buf = NULL; |
1430 | va_list adx_copy; | |
428870ff | 1431 | |
00b46022 BB |
1432 | va_copy(adx_copy, adx); |
1433 | VERIFY(vasprintf(&buf, fmt, adx_copy) != -1); | |
1434 | va_end(adx_copy); | |
428870ff | 1435 | |
00b46022 BB |
1436 | return (buf); |
1437 | } | |
1438 | ||
1439 | char * | |
1440 | kmem_asprintf(const char *fmt, ...) | |
1441 | { | |
1442 | char *buf = NULL; | |
1443 | va_list adx; | |
428870ff BB |
1444 | |
1445 | va_start(adx, fmt); | |
00b46022 | 1446 | VERIFY(vasprintf(&buf, fmt, adx) != -1); |
428870ff BB |
1447 | va_end(adx); |
1448 | ||
1449 | return (buf); | |
1450 | } | |
572e2857 BB |
1451 | |
1452 | /* ARGSUSED */ | |
1453 | int | |
1454 | zfs_onexit_fd_hold(int fd, minor_t *minorp) | |
1455 | { | |
1456 | *minorp = 0; | |
1457 | return (0); | |
1458 | } | |
1459 | ||
1460 | /* ARGSUSED */ | |
1461 | void | |
1462 | zfs_onexit_fd_rele(int fd) | |
1463 | { | |
1464 | } | |
1465 | ||
1466 | /* ARGSUSED */ | |
1467 | int | |
1468 | zfs_onexit_add_cb(minor_t minor, void (*func)(void *), void *data, | |
1469 | uint64_t *action_handle) | |
1470 | { | |
1471 | return (0); | |
1472 | } | |
1473 | ||
1474 | /* ARGSUSED */ | |
1475 | int | |
1476 | zfs_onexit_del_cb(minor_t minor, uint64_t action_handle, boolean_t fire) | |
1477 | { | |
1478 | return (0); | |
1479 | } | |
1480 | ||
1481 | /* ARGSUSED */ | |
1482 | int | |
1483 | zfs_onexit_cb_data(minor_t minor, uint64_t action_handle, void **data) | |
1484 | { | |
1485 | return (0); | |
1486 | } | |
92119cc2 BB |
1487 | |
1488 | fstrans_cookie_t | |
1489 | spl_fstrans_mark(void) | |
1490 | { | |
1491 | return ((fstrans_cookie_t) 0); | |
1492 | } | |
1493 | ||
1494 | void | |
1495 | spl_fstrans_unmark(fstrans_cookie_t cookie) | |
1496 | { | |
1497 | } | |
1498 | ||
1499 | int | |
1500 | spl_fstrans_check(void) | |
1501 | { | |
1502 | return (0); | |
1503 | } | |
a0bd735a | 1504 | |
47dfff3b MA |
1505 | void *zvol_tag = "zvol_tag"; |
1506 | ||
a0bd735a BP |
1507 | void |
1508 | zvol_create_minors(spa_t *spa, const char *name, boolean_t async) | |
1509 | { | |
1510 | } | |
1511 | ||
1512 | void | |
1513 | zvol_remove_minor(spa_t *spa, const char *name, boolean_t async) | |
1514 | { | |
1515 | } | |
1516 | ||
1517 | void | |
1518 | zvol_remove_minors(spa_t *spa, const char *name, boolean_t async) | |
1519 | { | |
1520 | } | |
1521 | ||
1522 | void | |
1523 | zvol_rename_minors(spa_t *spa, const char *oldname, const char *newname, | |
1524 | boolean_t async) | |
1525 | { | |
1526 | } |