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