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