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