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