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