]>
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 | /* | |
22 | * Copyright 2008 Sun Microsystems, Inc. All rights reserved. | |
23 | * Use is subject to license terms. | |
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> | |
33 | #include <sys/spa.h> | |
34 | #include <sys/stat.h> | |
35 | #include <sys/processor.h> | |
36 | #include <sys/zfs_context.h> | |
37 | #include <sys/zmod.h> | |
38 | #include <sys/utsname.h> | |
39 | ||
40 | /* | |
41 | * Emulation of kernel services in userland. | |
42 | */ | |
43 | ||
44 | uint64_t physmem; | |
45 | vnode_t *rootdir = (vnode_t *)0xabcd1234; | |
46 | char hw_serial[11]; | |
47 | ||
48 | struct utsname utsname = { | |
49 | "userland", "libzpool", "1", "1", "na" | |
50 | }; | |
51 | ||
52 | /* | |
53 | * ========================================================================= | |
54 | * threads | |
55 | * ========================================================================= | |
56 | */ | |
57 | /*ARGSUSED*/ | |
58 | kthread_t * | |
59 | zk_thread_create(void (*func)(), void *arg) | |
60 | { | |
61 | thread_t tid; | |
62 | ||
63 | VERIFY(thr_create(0, 0, (void *(*)(void *))func, arg, THR_DETACHED, | |
64 | &tid) == 0); | |
65 | ||
66 | return ((void *)(uintptr_t)tid); | |
67 | } | |
68 | ||
69 | /* | |
70 | * ========================================================================= | |
71 | * kstats | |
72 | * ========================================================================= | |
73 | */ | |
74 | /*ARGSUSED*/ | |
75 | kstat_t * | |
76 | kstat_create(char *module, int instance, char *name, char *class, | |
77 | uchar_t type, ulong_t ndata, uchar_t ks_flag) | |
78 | { | |
79 | return (NULL); | |
80 | } | |
81 | ||
82 | /*ARGSUSED*/ | |
83 | void | |
84 | kstat_install(kstat_t *ksp) | |
85 | {} | |
86 | ||
87 | /*ARGSUSED*/ | |
88 | void | |
89 | kstat_delete(kstat_t *ksp) | |
90 | {} | |
91 | ||
92 | /* | |
93 | * ========================================================================= | |
94 | * mutexes | |
95 | * ========================================================================= | |
96 | */ | |
97 | void | |
98 | zmutex_init(kmutex_t *mp) | |
99 | { | |
100 | mp->m_owner = NULL; | |
101 | mp->initialized = B_TRUE; | |
102 | (void) _mutex_init(&mp->m_lock, USYNC_THREAD, NULL); | |
103 | } | |
104 | ||
105 | void | |
106 | zmutex_destroy(kmutex_t *mp) | |
107 | { | |
108 | ASSERT(mp->initialized == B_TRUE); | |
109 | ASSERT(mp->m_owner == NULL); | |
110 | (void) _mutex_destroy(&(mp)->m_lock); | |
111 | mp->m_owner = (void *)-1UL; | |
112 | mp->initialized = B_FALSE; | |
113 | } | |
114 | ||
115 | void | |
116 | mutex_enter(kmutex_t *mp) | |
117 | { | |
118 | ASSERT(mp->initialized == B_TRUE); | |
119 | ASSERT(mp->m_owner != (void *)-1UL); | |
120 | ASSERT(mp->m_owner != curthread); | |
121 | VERIFY(mutex_lock(&mp->m_lock) == 0); | |
122 | ASSERT(mp->m_owner == NULL); | |
123 | mp->m_owner = curthread; | |
124 | } | |
125 | ||
126 | int | |
127 | mutex_tryenter(kmutex_t *mp) | |
128 | { | |
129 | ASSERT(mp->initialized == B_TRUE); | |
130 | ASSERT(mp->m_owner != (void *)-1UL); | |
131 | if (0 == mutex_trylock(&mp->m_lock)) { | |
132 | ASSERT(mp->m_owner == NULL); | |
133 | mp->m_owner = curthread; | |
134 | return (1); | |
135 | } else { | |
136 | return (0); | |
137 | } | |
138 | } | |
139 | ||
140 | void | |
141 | mutex_exit(kmutex_t *mp) | |
142 | { | |
143 | ASSERT(mp->initialized == B_TRUE); | |
144 | ASSERT(mutex_owner(mp) == curthread); | |
145 | mp->m_owner = NULL; | |
146 | VERIFY(mutex_unlock(&mp->m_lock) == 0); | |
147 | } | |
148 | ||
149 | void * | |
150 | mutex_owner(kmutex_t *mp) | |
151 | { | |
152 | ASSERT(mp->initialized == B_TRUE); | |
153 | return (mp->m_owner); | |
154 | } | |
155 | ||
156 | /* | |
157 | * ========================================================================= | |
158 | * rwlocks | |
159 | * ========================================================================= | |
160 | */ | |
161 | /*ARGSUSED*/ | |
162 | void | |
163 | rw_init(krwlock_t *rwlp, char *name, int type, void *arg) | |
164 | { | |
165 | rwlock_init(&rwlp->rw_lock, USYNC_THREAD, NULL); | |
166 | rwlp->rw_owner = NULL; | |
167 | rwlp->initialized = B_TRUE; | |
168 | } | |
169 | ||
170 | void | |
171 | rw_destroy(krwlock_t *rwlp) | |
172 | { | |
173 | rwlock_destroy(&rwlp->rw_lock); | |
174 | rwlp->rw_owner = (void *)-1UL; | |
175 | rwlp->initialized = B_FALSE; | |
176 | } | |
177 | ||
178 | void | |
179 | rw_enter(krwlock_t *rwlp, krw_t rw) | |
180 | { | |
181 | ASSERT(!RW_LOCK_HELD(rwlp)); | |
182 | ASSERT(rwlp->initialized == B_TRUE); | |
183 | ASSERT(rwlp->rw_owner != (void *)-1UL); | |
184 | ASSERT(rwlp->rw_owner != curthread); | |
185 | ||
186 | if (rw == RW_READER) | |
b128c09f | 187 | VERIFY(rw_rdlock(&rwlp->rw_lock) == 0); |
34dc7c2f | 188 | else |
b128c09f | 189 | VERIFY(rw_wrlock(&rwlp->rw_lock) == 0); |
34dc7c2f BB |
190 | |
191 | rwlp->rw_owner = curthread; | |
192 | } | |
193 | ||
194 | void | |
195 | rw_exit(krwlock_t *rwlp) | |
196 | { | |
197 | ASSERT(rwlp->initialized == B_TRUE); | |
198 | ASSERT(rwlp->rw_owner != (void *)-1UL); | |
199 | ||
200 | rwlp->rw_owner = NULL; | |
b128c09f | 201 | VERIFY(rw_unlock(&rwlp->rw_lock) == 0); |
34dc7c2f BB |
202 | } |
203 | ||
204 | int | |
205 | rw_tryenter(krwlock_t *rwlp, krw_t rw) | |
206 | { | |
207 | int rv; | |
208 | ||
209 | ASSERT(rwlp->initialized == B_TRUE); | |
210 | ASSERT(rwlp->rw_owner != (void *)-1UL); | |
211 | ||
212 | if (rw == RW_READER) | |
213 | rv = rw_tryrdlock(&rwlp->rw_lock); | |
214 | else | |
215 | rv = rw_trywrlock(&rwlp->rw_lock); | |
216 | ||
217 | if (rv == 0) { | |
218 | rwlp->rw_owner = curthread; | |
219 | return (1); | |
220 | } | |
221 | ||
222 | return (0); | |
223 | } | |
224 | ||
225 | /*ARGSUSED*/ | |
226 | int | |
227 | rw_tryupgrade(krwlock_t *rwlp) | |
228 | { | |
229 | ASSERT(rwlp->initialized == B_TRUE); | |
230 | ASSERT(rwlp->rw_owner != (void *)-1UL); | |
231 | ||
232 | return (0); | |
233 | } | |
234 | ||
235 | /* | |
236 | * ========================================================================= | |
237 | * condition variables | |
238 | * ========================================================================= | |
239 | */ | |
240 | /*ARGSUSED*/ | |
241 | void | |
242 | cv_init(kcondvar_t *cv, char *name, int type, void *arg) | |
243 | { | |
244 | VERIFY(cond_init(cv, type, NULL) == 0); | |
245 | } | |
246 | ||
247 | void | |
248 | cv_destroy(kcondvar_t *cv) | |
249 | { | |
250 | VERIFY(cond_destroy(cv) == 0); | |
251 | } | |
252 | ||
253 | void | |
254 | cv_wait(kcondvar_t *cv, kmutex_t *mp) | |
255 | { | |
256 | ASSERT(mutex_owner(mp) == curthread); | |
257 | mp->m_owner = NULL; | |
258 | int ret = cond_wait(cv, &mp->m_lock); | |
259 | VERIFY(ret == 0 || ret == EINTR); | |
260 | mp->m_owner = curthread; | |
261 | } | |
262 | ||
263 | clock_t | |
264 | cv_timedwait(kcondvar_t *cv, kmutex_t *mp, clock_t abstime) | |
265 | { | |
266 | int error; | |
267 | timestruc_t ts; | |
268 | clock_t delta; | |
269 | ||
270 | top: | |
271 | delta = abstime - lbolt; | |
272 | if (delta <= 0) | |
273 | return (-1); | |
274 | ||
275 | ts.tv_sec = delta / hz; | |
276 | ts.tv_nsec = (delta % hz) * (NANOSEC / hz); | |
277 | ||
278 | ASSERT(mutex_owner(mp) == curthread); | |
279 | mp->m_owner = NULL; | |
280 | error = cond_reltimedwait(cv, &mp->m_lock, &ts); | |
281 | mp->m_owner = curthread; | |
282 | ||
283 | if (error == ETIME) | |
284 | return (-1); | |
285 | ||
286 | if (error == EINTR) | |
287 | goto top; | |
288 | ||
289 | ASSERT(error == 0); | |
290 | ||
291 | return (1); | |
292 | } | |
293 | ||
294 | void | |
295 | cv_signal(kcondvar_t *cv) | |
296 | { | |
297 | VERIFY(cond_signal(cv) == 0); | |
298 | } | |
299 | ||
300 | void | |
301 | cv_broadcast(kcondvar_t *cv) | |
302 | { | |
303 | VERIFY(cond_broadcast(cv) == 0); | |
304 | } | |
305 | ||
306 | /* | |
307 | * ========================================================================= | |
308 | * vnode operations | |
309 | * ========================================================================= | |
310 | */ | |
311 | /* | |
312 | * Note: for the xxxat() versions of these functions, we assume that the | |
313 | * starting vp is always rootdir (which is true for spa_directory.c, the only | |
314 | * ZFS consumer of these interfaces). We assert this is true, and then emulate | |
315 | * them by adding '/' in front of the path. | |
316 | */ | |
317 | ||
318 | /*ARGSUSED*/ | |
319 | int | |
320 | vn_open(char *path, int x1, int flags, int mode, vnode_t **vpp, int x2, int x3) | |
321 | { | |
322 | int fd; | |
323 | vnode_t *vp; | |
324 | int old_umask; | |
325 | char realpath[MAXPATHLEN]; | |
326 | struct stat64 st; | |
327 | ||
328 | /* | |
329 | * If we're accessing a real disk from userland, we need to use | |
330 | * the character interface to avoid caching. This is particularly | |
331 | * important if we're trying to look at a real in-kernel storage | |
332 | * pool from userland, e.g. via zdb, because otherwise we won't | |
333 | * see the changes occurring under the segmap cache. | |
334 | * On the other hand, the stupid character device returns zero | |
335 | * for its size. So -- gag -- we open the block device to get | |
336 | * its size, and remember it for subsequent VOP_GETATTR(). | |
337 | */ | |
338 | if (strncmp(path, "/dev/", 5) == 0) { | |
339 | char *dsk; | |
340 | fd = open64(path, O_RDONLY); | |
341 | if (fd == -1) | |
342 | return (errno); | |
343 | if (fstat64(fd, &st) == -1) { | |
344 | close(fd); | |
345 | return (errno); | |
346 | } | |
347 | close(fd); | |
348 | (void) sprintf(realpath, "%s", path); | |
349 | dsk = strstr(path, "/dsk/"); | |
350 | if (dsk != NULL) | |
351 | (void) sprintf(realpath + (dsk - path) + 1, "r%s", | |
352 | dsk + 1); | |
353 | } else { | |
354 | (void) sprintf(realpath, "%s", path); | |
355 | if (!(flags & FCREAT) && stat64(realpath, &st) == -1) | |
356 | return (errno); | |
357 | } | |
358 | ||
359 | if (flags & FCREAT) | |
360 | old_umask = umask(0); | |
361 | ||
362 | /* | |
363 | * The construct 'flags - FREAD' conveniently maps combinations of | |
364 | * FREAD and FWRITE to the corresponding O_RDONLY, O_WRONLY, and O_RDWR. | |
365 | */ | |
366 | fd = open64(realpath, flags - FREAD, mode); | |
367 | ||
368 | if (flags & FCREAT) | |
369 | (void) umask(old_umask); | |
370 | ||
371 | if (fd == -1) | |
372 | return (errno); | |
373 | ||
374 | if (fstat64(fd, &st) == -1) { | |
375 | close(fd); | |
376 | return (errno); | |
377 | } | |
378 | ||
379 | (void) fcntl(fd, F_SETFD, FD_CLOEXEC); | |
380 | ||
381 | *vpp = vp = umem_zalloc(sizeof (vnode_t), UMEM_NOFAIL); | |
382 | ||
383 | vp->v_fd = fd; | |
384 | vp->v_size = st.st_size; | |
385 | vp->v_path = spa_strdup(path); | |
386 | ||
387 | return (0); | |
388 | } | |
389 | ||
390 | /*ARGSUSED*/ | |
391 | int | |
392 | vn_openat(char *path, int x1, int flags, int mode, vnode_t **vpp, int x2, | |
393 | int x3, vnode_t *startvp, int fd) | |
394 | { | |
395 | char *realpath = umem_alloc(strlen(path) + 2, UMEM_NOFAIL); | |
396 | int ret; | |
397 | ||
398 | ASSERT(startvp == rootdir); | |
399 | (void) sprintf(realpath, "/%s", path); | |
400 | ||
401 | /* fd ignored for now, need if want to simulate nbmand support */ | |
402 | ret = vn_open(realpath, x1, flags, mode, vpp, x2, x3); | |
403 | ||
404 | umem_free(realpath, strlen(path) + 2); | |
405 | ||
406 | return (ret); | |
407 | } | |
408 | ||
409 | /*ARGSUSED*/ | |
410 | int | |
411 | vn_rdwr(int uio, vnode_t *vp, void *addr, ssize_t len, offset_t offset, | |
412 | int x1, int x2, rlim64_t x3, void *x4, ssize_t *residp) | |
413 | { | |
414 | ssize_t iolen, split; | |
415 | ||
416 | if (uio == UIO_READ) { | |
417 | iolen = pread64(vp->v_fd, addr, len, offset); | |
418 | } else { | |
419 | /* | |
420 | * To simulate partial disk writes, we split writes into two | |
421 | * system calls so that the process can be killed in between. | |
422 | */ | |
423 | split = (len > 0 ? rand() % len : 0); | |
424 | iolen = pwrite64(vp->v_fd, addr, split, offset); | |
425 | iolen += pwrite64(vp->v_fd, (char *)addr + split, | |
426 | len - split, offset + split); | |
427 | } | |
428 | ||
b128c09f | 429 | if (iolen == -1) |
34dc7c2f BB |
430 | return (errno); |
431 | if (residp) | |
432 | *residp = len - iolen; | |
433 | else if (iolen != len) | |
434 | return (EIO); | |
435 | return (0); | |
436 | } | |
437 | ||
438 | void | |
439 | vn_close(vnode_t *vp) | |
440 | { | |
441 | close(vp->v_fd); | |
442 | spa_strfree(vp->v_path); | |
443 | umem_free(vp, sizeof (vnode_t)); | |
444 | } | |
445 | ||
446 | #ifdef ZFS_DEBUG | |
447 | ||
448 | /* | |
449 | * ========================================================================= | |
450 | * Figure out which debugging statements to print | |
451 | * ========================================================================= | |
452 | */ | |
453 | ||
454 | static char *dprintf_string; | |
455 | static int dprintf_print_all; | |
456 | ||
457 | int | |
458 | dprintf_find_string(const char *string) | |
459 | { | |
460 | char *tmp_str = dprintf_string; | |
461 | int len = strlen(string); | |
462 | ||
463 | /* | |
464 | * Find out if this is a string we want to print. | |
465 | * String format: file1.c,function_name1,file2.c,file3.c | |
466 | */ | |
467 | ||
468 | while (tmp_str != NULL) { | |
469 | if (strncmp(tmp_str, string, len) == 0 && | |
470 | (tmp_str[len] == ',' || tmp_str[len] == '\0')) | |
471 | return (1); | |
472 | tmp_str = strchr(tmp_str, ','); | |
473 | if (tmp_str != NULL) | |
474 | tmp_str++; /* Get rid of , */ | |
475 | } | |
476 | return (0); | |
477 | } | |
478 | ||
479 | void | |
480 | dprintf_setup(int *argc, char **argv) | |
481 | { | |
482 | int i, j; | |
483 | ||
484 | /* | |
485 | * Debugging can be specified two ways: by setting the | |
486 | * environment variable ZFS_DEBUG, or by including a | |
487 | * "debug=..." argument on the command line. The command | |
488 | * line setting overrides the environment variable. | |
489 | */ | |
490 | ||
491 | for (i = 1; i < *argc; i++) { | |
492 | int len = strlen("debug="); | |
493 | /* First look for a command line argument */ | |
494 | if (strncmp("debug=", argv[i], len) == 0) { | |
495 | dprintf_string = argv[i] + len; | |
496 | /* Remove from args */ | |
497 | for (j = i; j < *argc; j++) | |
498 | argv[j] = argv[j+1]; | |
499 | argv[j] = NULL; | |
500 | (*argc)--; | |
501 | } | |
502 | } | |
503 | ||
504 | if (dprintf_string == NULL) { | |
505 | /* Look for ZFS_DEBUG environment variable */ | |
506 | dprintf_string = getenv("ZFS_DEBUG"); | |
507 | } | |
508 | ||
509 | /* | |
510 | * Are we just turning on all debugging? | |
511 | */ | |
512 | if (dprintf_find_string("on")) | |
513 | dprintf_print_all = 1; | |
514 | } | |
515 | ||
516 | /* | |
517 | * ========================================================================= | |
518 | * debug printfs | |
519 | * ========================================================================= | |
520 | */ | |
521 | void | |
522 | __dprintf(const char *file, const char *func, int line, const char *fmt, ...) | |
523 | { | |
524 | const char *newfile; | |
525 | va_list adx; | |
526 | ||
527 | /* | |
528 | * Get rid of annoying "../common/" prefix to filename. | |
529 | */ | |
530 | newfile = strrchr(file, '/'); | |
531 | if (newfile != NULL) { | |
532 | newfile = newfile + 1; /* Get rid of leading / */ | |
533 | } else { | |
534 | newfile = file; | |
535 | } | |
536 | ||
537 | if (dprintf_print_all || | |
538 | dprintf_find_string(newfile) || | |
539 | dprintf_find_string(func)) { | |
540 | /* Print out just the function name if requested */ | |
541 | flockfile(stdout); | |
542 | if (dprintf_find_string("pid")) | |
543 | (void) printf("%d ", getpid()); | |
544 | if (dprintf_find_string("tid")) | |
545 | (void) printf("%u ", thr_self()); | |
546 | if (dprintf_find_string("cpu")) | |
547 | (void) printf("%u ", getcpuid()); | |
548 | if (dprintf_find_string("time")) | |
549 | (void) printf("%llu ", gethrtime()); | |
550 | if (dprintf_find_string("long")) | |
551 | (void) printf("%s, line %d: ", newfile, line); | |
552 | (void) printf("%s: ", func); | |
553 | va_start(adx, fmt); | |
554 | (void) vprintf(fmt, adx); | |
555 | va_end(adx); | |
556 | funlockfile(stdout); | |
557 | } | |
558 | } | |
559 | ||
560 | #endif /* ZFS_DEBUG */ | |
561 | ||
562 | /* | |
563 | * ========================================================================= | |
564 | * cmn_err() and panic() | |
565 | * ========================================================================= | |
566 | */ | |
567 | static char ce_prefix[CE_IGNORE][10] = { "", "NOTICE: ", "WARNING: ", "" }; | |
568 | static char ce_suffix[CE_IGNORE][2] = { "", "\n", "\n", "" }; | |
569 | ||
570 | void | |
571 | vpanic(const char *fmt, va_list adx) | |
572 | { | |
573 | (void) fprintf(stderr, "error: "); | |
574 | (void) vfprintf(stderr, fmt, adx); | |
575 | (void) fprintf(stderr, "\n"); | |
576 | ||
577 | abort(); /* think of it as a "user-level crash dump" */ | |
578 | } | |
579 | ||
580 | void | |
581 | panic(const char *fmt, ...) | |
582 | { | |
583 | va_list adx; | |
584 | ||
585 | va_start(adx, fmt); | |
586 | vpanic(fmt, adx); | |
587 | va_end(adx); | |
588 | } | |
589 | ||
590 | void | |
591 | vcmn_err(int ce, const char *fmt, va_list adx) | |
592 | { | |
593 | if (ce == CE_PANIC) | |
594 | vpanic(fmt, adx); | |
595 | if (ce != CE_NOTE) { /* suppress noise in userland stress testing */ | |
596 | (void) fprintf(stderr, "%s", ce_prefix[ce]); | |
597 | (void) vfprintf(stderr, fmt, adx); | |
598 | (void) fprintf(stderr, "%s", ce_suffix[ce]); | |
599 | } | |
600 | } | |
601 | ||
602 | /*PRINTFLIKE2*/ | |
603 | void | |
604 | cmn_err(int ce, const char *fmt, ...) | |
605 | { | |
606 | va_list adx; | |
607 | ||
608 | va_start(adx, fmt); | |
609 | vcmn_err(ce, fmt, adx); | |
610 | va_end(adx); | |
611 | } | |
612 | ||
613 | /* | |
614 | * ========================================================================= | |
615 | * kobj interfaces | |
616 | * ========================================================================= | |
617 | */ | |
618 | struct _buf * | |
619 | kobj_open_file(char *name) | |
620 | { | |
621 | struct _buf *file; | |
622 | vnode_t *vp; | |
623 | ||
624 | /* set vp as the _fd field of the file */ | |
625 | if (vn_openat(name, UIO_SYSSPACE, FREAD, 0, &vp, 0, 0, rootdir, | |
626 | -1) != 0) | |
627 | return ((void *)-1UL); | |
628 | ||
629 | file = umem_zalloc(sizeof (struct _buf), UMEM_NOFAIL); | |
630 | file->_fd = (intptr_t)vp; | |
631 | return (file); | |
632 | } | |
633 | ||
634 | int | |
635 | kobj_read_file(struct _buf *file, char *buf, unsigned size, unsigned off) | |
636 | { | |
637 | ssize_t resid; | |
638 | ||
639 | vn_rdwr(UIO_READ, (vnode_t *)file->_fd, buf, size, (offset_t)off, | |
640 | UIO_SYSSPACE, 0, 0, 0, &resid); | |
641 | ||
642 | return (size - resid); | |
643 | } | |
644 | ||
645 | void | |
646 | kobj_close_file(struct _buf *file) | |
647 | { | |
648 | vn_close((vnode_t *)file->_fd); | |
649 | umem_free(file, sizeof (struct _buf)); | |
650 | } | |
651 | ||
652 | int | |
653 | kobj_get_filesize(struct _buf *file, uint64_t *size) | |
654 | { | |
655 | struct stat64 st; | |
656 | vnode_t *vp = (vnode_t *)file->_fd; | |
657 | ||
658 | if (fstat64(vp->v_fd, &st) == -1) { | |
659 | vn_close(vp); | |
660 | return (errno); | |
661 | } | |
662 | *size = st.st_size; | |
663 | return (0); | |
664 | } | |
665 | ||
666 | /* | |
667 | * ========================================================================= | |
668 | * misc routines | |
669 | * ========================================================================= | |
670 | */ | |
671 | ||
672 | void | |
673 | delay(clock_t ticks) | |
674 | { | |
675 | poll(0, 0, ticks * (1000 / hz)); | |
676 | } | |
677 | ||
678 | /* | |
679 | * Find highest one bit set. | |
680 | * Returns bit number + 1 of highest bit that is set, otherwise returns 0. | |
681 | * High order bit is 31 (or 63 in _LP64 kernel). | |
682 | */ | |
683 | int | |
684 | highbit(ulong_t i) | |
685 | { | |
686 | register int h = 1; | |
687 | ||
688 | if (i == 0) | |
689 | return (0); | |
690 | #ifdef _LP64 | |
691 | if (i & 0xffffffff00000000ul) { | |
692 | h += 32; i >>= 32; | |
693 | } | |
694 | #endif | |
695 | if (i & 0xffff0000) { | |
696 | h += 16; i >>= 16; | |
697 | } | |
698 | if (i & 0xff00) { | |
699 | h += 8; i >>= 8; | |
700 | } | |
701 | if (i & 0xf0) { | |
702 | h += 4; i >>= 4; | |
703 | } | |
704 | if (i & 0xc) { | |
705 | h += 2; i >>= 2; | |
706 | } | |
707 | if (i & 0x2) { | |
708 | h += 1; | |
709 | } | |
710 | return (h); | |
711 | } | |
712 | ||
713 | static int random_fd = -1, urandom_fd = -1; | |
714 | ||
715 | static int | |
716 | random_get_bytes_common(uint8_t *ptr, size_t len, int fd) | |
717 | { | |
718 | size_t resid = len; | |
719 | ssize_t bytes; | |
720 | ||
721 | ASSERT(fd != -1); | |
722 | ||
723 | while (resid != 0) { | |
724 | bytes = read(fd, ptr, resid); | |
725 | ASSERT3S(bytes, >=, 0); | |
726 | ptr += bytes; | |
727 | resid -= bytes; | |
728 | } | |
729 | ||
730 | return (0); | |
731 | } | |
732 | ||
733 | int | |
734 | random_get_bytes(uint8_t *ptr, size_t len) | |
735 | { | |
736 | return (random_get_bytes_common(ptr, len, random_fd)); | |
737 | } | |
738 | ||
739 | int | |
740 | random_get_pseudo_bytes(uint8_t *ptr, size_t len) | |
741 | { | |
742 | return (random_get_bytes_common(ptr, len, urandom_fd)); | |
743 | } | |
744 | ||
745 | int | |
746 | ddi_strtoul(const char *hw_serial, char **nptr, int base, unsigned long *result) | |
747 | { | |
748 | char *end; | |
749 | ||
750 | *result = strtoul(hw_serial, &end, base); | |
751 | if (*result == 0) | |
752 | return (errno); | |
753 | return (0); | |
754 | } | |
755 | ||
756 | /* | |
757 | * ========================================================================= | |
758 | * kernel emulation setup & teardown | |
759 | * ========================================================================= | |
760 | */ | |
761 | static int | |
762 | umem_out_of_memory(void) | |
763 | { | |
764 | char errmsg[] = "out of memory -- generating core dump\n"; | |
765 | ||
766 | write(fileno(stderr), errmsg, sizeof (errmsg)); | |
767 | abort(); | |
768 | return (0); | |
769 | } | |
770 | ||
771 | void | |
772 | kernel_init(int mode) | |
773 | { | |
774 | umem_nofail_callback(umem_out_of_memory); | |
775 | ||
776 | physmem = sysconf(_SC_PHYS_PAGES); | |
777 | ||
778 | dprintf("physmem = %llu pages (%.2f GB)\n", physmem, | |
779 | (double)physmem * sysconf(_SC_PAGE_SIZE) / (1ULL << 30)); | |
780 | ||
34dc7c2f BB |
781 | snprintf(hw_serial, sizeof (hw_serial), "%ld", gethostid()); |
782 | ||
783 | VERIFY((random_fd = open("/dev/random", O_RDONLY)) != -1); | |
784 | VERIFY((urandom_fd = open("/dev/urandom", O_RDONLY)) != -1); | |
785 | ||
b128c09f BB |
786 | system_taskq_init(); |
787 | ||
34dc7c2f BB |
788 | spa_init(mode); |
789 | } | |
790 | ||
791 | void | |
792 | kernel_fini(void) | |
793 | { | |
794 | spa_fini(); | |
795 | ||
796 | close(random_fd); | |
797 | close(urandom_fd); | |
798 | ||
799 | random_fd = -1; | |
800 | urandom_fd = -1; | |
801 | } | |
802 | ||
803 | int | |
804 | z_uncompress(void *dst, size_t *dstlen, const void *src, size_t srclen) | |
805 | { | |
806 | int ret; | |
807 | uLongf len = *dstlen; | |
808 | ||
809 | if ((ret = uncompress(dst, &len, src, srclen)) == Z_OK) | |
810 | *dstlen = (size_t)len; | |
811 | ||
812 | return (ret); | |
813 | } | |
814 | ||
815 | int | |
816 | z_compress_level(void *dst, size_t *dstlen, const void *src, size_t srclen, | |
817 | int level) | |
818 | { | |
819 | int ret; | |
820 | uLongf len = *dstlen; | |
821 | ||
822 | if ((ret = compress2(dst, &len, src, srclen, level)) == Z_OK) | |
823 | *dstlen = (size_t)len; | |
824 | ||
825 | return (ret); | |
826 | } | |
827 | ||
34dc7c2f BB |
828 | uid_t |
829 | crgetuid(cred_t *cr) | |
830 | { | |
831 | return (0); | |
832 | } | |
833 | ||
834 | gid_t | |
835 | crgetgid(cred_t *cr) | |
836 | { | |
837 | return (0); | |
838 | } | |
839 | ||
840 | int | |
841 | crgetngroups(cred_t *cr) | |
842 | { | |
843 | return (0); | |
844 | } | |
845 | ||
846 | gid_t * | |
847 | crgetgroups(cred_t *cr) | |
848 | { | |
849 | return (NULL); | |
850 | } | |
851 | ||
852 | int | |
853 | zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr) | |
854 | { | |
855 | return (0); | |
856 | } | |
857 | ||
858 | int | |
859 | zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr) | |
860 | { | |
861 | return (0); | |
862 | } | |
863 | ||
864 | int | |
865 | zfs_secpolicy_destroy_perms(const char *name, cred_t *cr) | |
866 | { | |
867 | return (0); | |
868 | } | |
869 | ||
870 | ksiddomain_t * | |
871 | ksid_lookupdomain(const char *dom) | |
872 | { | |
873 | ksiddomain_t *kd; | |
874 | ||
875 | kd = umem_zalloc(sizeof (ksiddomain_t), UMEM_NOFAIL); | |
876 | kd->kd_name = spa_strdup(dom); | |
877 | return (kd); | |
878 | } | |
879 | ||
880 | void | |
881 | ksiddomain_rele(ksiddomain_t *ksid) | |
882 | { | |
883 | spa_strfree(ksid->kd_name); | |
884 | umem_free(ksid, sizeof (ksiddomain_t)); | |
885 | } |