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