xref: /titanic_51/usr/src/lib/libzpool/common/kernel.c (revision bf16b11e8deb633dd6c4296d46e92399d1582df4)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5ea8dc4b6Seschrock  * Common Development and Distribution License (the "License").
6ea8dc4b6Seschrock  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
21fa9e4066Sahrens /*
22c99e4bdcSChris Kirby  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23*bf16b11eSMatthew Ahrens  * Copyright (c) 2012, 2014 by Delphix. All rights reserved.
24b3d32f0cSBryan Cantrill  * Copyright (c) 2013, Joyent, Inc.  All rights reserved.
25fa9e4066Sahrens  */
26fa9e4066Sahrens 
27fa9e4066Sahrens #include <assert.h>
28c9431fa1Sahl #include <fcntl.h>
29fa9e4066Sahrens #include <poll.h>
30fa9e4066Sahrens #include <stdio.h>
31fa9e4066Sahrens #include <stdlib.h>
32c9431fa1Sahl #include <string.h>
33c9431fa1Sahl #include <zlib.h>
34fa9e4066Sahrens #include <sys/spa.h>
35c9431fa1Sahl #include <sys/stat.h>
36fa9e4066Sahrens #include <sys/processor.h>
37c9431fa1Sahl #include <sys/zfs_context.h>
383b2aab18SMatthew Ahrens #include <sys/rrwlock.h>
39c9431fa1Sahl #include <sys/zmod.h>
4095173954Sek110237 #include <sys/utsname.h>
415679c89fSjv227347 #include <sys/systeminfo.h>
425ad82045Snd150628 
43fa9e4066Sahrens /*
44fa9e4066Sahrens  * Emulation of kernel services in userland.
45fa9e4066Sahrens  */
46fa9e4066Sahrens 
47feef89cfSVictor Latushkin int aok;
48fa9e4066Sahrens uint64_t physmem;
49fa9e4066Sahrens vnode_t *rootdir = (vnode_t *)0xabcd1234;
505679c89fSjv227347 char hw_serial[HW_HOSTID_LEN];
51283b8460SGeorge.Wilson kmutex_t cpu_lock;
5294dd93aeSGeorge Wilson vmem_t *zio_arena = NULL;
5395173954Sek110237 
5495173954Sek110237 struct utsname utsname = {
5595173954Sek110237 	"userland", "libzpool", "1", "1", "na"
5695173954Sek110237 };
57fa9e4066Sahrens 
5835a5a358SJonathan Adams /* this only exists to have its address taken */
5935a5a358SJonathan Adams struct proc p0;
6035a5a358SJonathan Adams 
61fa9e4066Sahrens /*
62fa9e4066Sahrens  * =========================================================================
63fa9e4066Sahrens  * threads
64fa9e4066Sahrens  * =========================================================================
65fa9e4066Sahrens  */
66fa9e4066Sahrens /*ARGSUSED*/
67fa9e4066Sahrens kthread_t *
68fa9e4066Sahrens zk_thread_create(void (*func)(), void *arg)
69fa9e4066Sahrens {
70fa9e4066Sahrens 	thread_t tid;
71fa9e4066Sahrens 
72fa9e4066Sahrens 	VERIFY(thr_create(0, 0, (void *(*)(void *))func, arg, THR_DETACHED,
73fa9e4066Sahrens 	    &tid) == 0);
74fa9e4066Sahrens 
75fa9e4066Sahrens 	return ((void *)(uintptr_t)tid);
76fa9e4066Sahrens }
77fa9e4066Sahrens 
78fa9e4066Sahrens /*
79fa9e4066Sahrens  * =========================================================================
8044cb6abcSbmc  * kstats
8144cb6abcSbmc  * =========================================================================
8244cb6abcSbmc  */
8344cb6abcSbmc /*ARGSUSED*/
8444cb6abcSbmc kstat_t *
85c3a66015SMatthew Ahrens kstat_create(const char *module, int instance, const char *name,
86c3a66015SMatthew Ahrens     const char *class, uchar_t type, ulong_t ndata, uchar_t ks_flag)
8744cb6abcSbmc {
8844cb6abcSbmc 	return (NULL);
8944cb6abcSbmc }
9044cb6abcSbmc 
9144cb6abcSbmc /*ARGSUSED*/
9244cb6abcSbmc void
9344cb6abcSbmc kstat_install(kstat_t *ksp)
9444cb6abcSbmc {}
9544cb6abcSbmc 
9644cb6abcSbmc /*ARGSUSED*/
9744cb6abcSbmc void
9844cb6abcSbmc kstat_delete(kstat_t *ksp)
9944cb6abcSbmc {}
10044cb6abcSbmc 
101c3a66015SMatthew Ahrens /*ARGSUSED*/
102c3a66015SMatthew Ahrens void
103c3a66015SMatthew Ahrens kstat_waitq_enter(kstat_io_t *kiop)
104c3a66015SMatthew Ahrens {}
105c3a66015SMatthew Ahrens 
106c3a66015SMatthew Ahrens /*ARGSUSED*/
107c3a66015SMatthew Ahrens void
108c3a66015SMatthew Ahrens kstat_waitq_exit(kstat_io_t *kiop)
109c3a66015SMatthew Ahrens {}
110c3a66015SMatthew Ahrens 
111c3a66015SMatthew Ahrens /*ARGSUSED*/
112c3a66015SMatthew Ahrens void
113c3a66015SMatthew Ahrens kstat_runq_enter(kstat_io_t *kiop)
114c3a66015SMatthew Ahrens {}
115c3a66015SMatthew Ahrens 
116c3a66015SMatthew Ahrens /*ARGSUSED*/
117c3a66015SMatthew Ahrens void
118c3a66015SMatthew Ahrens kstat_runq_exit(kstat_io_t *kiop)
119c3a66015SMatthew Ahrens {}
120c3a66015SMatthew Ahrens 
121c3a66015SMatthew Ahrens /*ARGSUSED*/
122c3a66015SMatthew Ahrens void
123c3a66015SMatthew Ahrens kstat_waitq_to_runq(kstat_io_t *kiop)
124c3a66015SMatthew Ahrens {}
125c3a66015SMatthew Ahrens 
126c3a66015SMatthew Ahrens /*ARGSUSED*/
127c3a66015SMatthew Ahrens void
128c3a66015SMatthew Ahrens kstat_runq_back_to_waitq(kstat_io_t *kiop)
129c3a66015SMatthew Ahrens {}
130c3a66015SMatthew Ahrens 
13144cb6abcSbmc /*
13244cb6abcSbmc  * =========================================================================
133fa9e4066Sahrens  * mutexes
134fa9e4066Sahrens  * =========================================================================
135fa9e4066Sahrens  */
136fa9e4066Sahrens void
137fa9e4066Sahrens zmutex_init(kmutex_t *mp)
138fa9e4066Sahrens {
139fa9e4066Sahrens 	mp->m_owner = NULL;
140c25056deSgw25295 	mp->initialized = B_TRUE;
141fa9e4066Sahrens 	(void) _mutex_init(&mp->m_lock, USYNC_THREAD, NULL);
142fa9e4066Sahrens }
143fa9e4066Sahrens 
144fa9e4066Sahrens void
145fa9e4066Sahrens zmutex_destroy(kmutex_t *mp)
146fa9e4066Sahrens {
147c25056deSgw25295 	ASSERT(mp->initialized == B_TRUE);
148fa9e4066Sahrens 	ASSERT(mp->m_owner == NULL);
149fa9e4066Sahrens 	(void) _mutex_destroy(&(mp)->m_lock);
150fa9e4066Sahrens 	mp->m_owner = (void *)-1UL;
151c25056deSgw25295 	mp->initialized = B_FALSE;
152fa9e4066Sahrens }
153fa9e4066Sahrens 
154fa9e4066Sahrens void
155fa9e4066Sahrens mutex_enter(kmutex_t *mp)
156fa9e4066Sahrens {
157c25056deSgw25295 	ASSERT(mp->initialized == B_TRUE);
158fa9e4066Sahrens 	ASSERT(mp->m_owner != (void *)-1UL);
159fa9e4066Sahrens 	ASSERT(mp->m_owner != curthread);
1605ad82045Snd150628 	VERIFY(mutex_lock(&mp->m_lock) == 0);
161fa9e4066Sahrens 	ASSERT(mp->m_owner == NULL);
162fa9e4066Sahrens 	mp->m_owner = curthread;
163fa9e4066Sahrens }
164fa9e4066Sahrens 
165fa9e4066Sahrens int
166fa9e4066Sahrens mutex_tryenter(kmutex_t *mp)
167fa9e4066Sahrens {
168c25056deSgw25295 	ASSERT(mp->initialized == B_TRUE);
169fa9e4066Sahrens 	ASSERT(mp->m_owner != (void *)-1UL);
170fa9e4066Sahrens 	if (0 == mutex_trylock(&mp->m_lock)) {
171fa9e4066Sahrens 		ASSERT(mp->m_owner == NULL);
172fa9e4066Sahrens 		mp->m_owner = curthread;
173fa9e4066Sahrens 		return (1);
174fa9e4066Sahrens 	} else {
175fa9e4066Sahrens 		return (0);
176fa9e4066Sahrens 	}
177fa9e4066Sahrens }
178fa9e4066Sahrens 
179fa9e4066Sahrens void
180fa9e4066Sahrens mutex_exit(kmutex_t *mp)
181fa9e4066Sahrens {
182c25056deSgw25295 	ASSERT(mp->initialized == B_TRUE);
183fa9e4066Sahrens 	ASSERT(mutex_owner(mp) == curthread);
184fa9e4066Sahrens 	mp->m_owner = NULL;
1855ad82045Snd150628 	VERIFY(mutex_unlock(&mp->m_lock) == 0);
186fa9e4066Sahrens }
187fa9e4066Sahrens 
188fa9e4066Sahrens void *
189fa9e4066Sahrens mutex_owner(kmutex_t *mp)
190fa9e4066Sahrens {
191c25056deSgw25295 	ASSERT(mp->initialized == B_TRUE);
192fa9e4066Sahrens 	return (mp->m_owner);
193fa9e4066Sahrens }
194fa9e4066Sahrens 
195fa9e4066Sahrens /*
196fa9e4066Sahrens  * =========================================================================
197fa9e4066Sahrens  * rwlocks
198fa9e4066Sahrens  * =========================================================================
199fa9e4066Sahrens  */
200fa9e4066Sahrens /*ARGSUSED*/
201fa9e4066Sahrens void
202fa9e4066Sahrens rw_init(krwlock_t *rwlp, char *name, int type, void *arg)
203fa9e4066Sahrens {
204fa9e4066Sahrens 	rwlock_init(&rwlp->rw_lock, USYNC_THREAD, NULL);
205fa9e4066Sahrens 	rwlp->rw_owner = NULL;
206c25056deSgw25295 	rwlp->initialized = B_TRUE;
207fa9e4066Sahrens }
208fa9e4066Sahrens 
209fa9e4066Sahrens void
210fa9e4066Sahrens rw_destroy(krwlock_t *rwlp)
211fa9e4066Sahrens {
212fa9e4066Sahrens 	rwlock_destroy(&rwlp->rw_lock);
213fa9e4066Sahrens 	rwlp->rw_owner = (void *)-1UL;
214c25056deSgw25295 	rwlp->initialized = B_FALSE;
215fa9e4066Sahrens }
216fa9e4066Sahrens 
217fa9e4066Sahrens void
218fa9e4066Sahrens rw_enter(krwlock_t *rwlp, krw_t rw)
219fa9e4066Sahrens {
220fa9e4066Sahrens 	ASSERT(!RW_LOCK_HELD(rwlp));
221c25056deSgw25295 	ASSERT(rwlp->initialized == B_TRUE);
222fa9e4066Sahrens 	ASSERT(rwlp->rw_owner != (void *)-1UL);
223fa9e4066Sahrens 	ASSERT(rwlp->rw_owner != curthread);
224fa9e4066Sahrens 
225b3d32f0cSBryan Cantrill 	if (rw == RW_WRITER)
226745cd3c5Smaybee 		VERIFY(rw_wrlock(&rwlp->rw_lock) == 0);
227b3d32f0cSBryan Cantrill 	else
228b3d32f0cSBryan Cantrill 		VERIFY(rw_rdlock(&rwlp->rw_lock) == 0);
229fa9e4066Sahrens 
230fa9e4066Sahrens 	rwlp->rw_owner = curthread;
231fa9e4066Sahrens }
232fa9e4066Sahrens 
233fa9e4066Sahrens void
234fa9e4066Sahrens rw_exit(krwlock_t *rwlp)
235fa9e4066Sahrens {
236c25056deSgw25295 	ASSERT(rwlp->initialized == B_TRUE);
237fa9e4066Sahrens 	ASSERT(rwlp->rw_owner != (void *)-1UL);
238fa9e4066Sahrens 
239fa9e4066Sahrens 	rwlp->rw_owner = NULL;
240745cd3c5Smaybee 	VERIFY(rw_unlock(&rwlp->rw_lock) == 0);
241fa9e4066Sahrens }
242fa9e4066Sahrens 
243fa9e4066Sahrens int
244fa9e4066Sahrens rw_tryenter(krwlock_t *rwlp, krw_t rw)
245fa9e4066Sahrens {
246fa9e4066Sahrens 	int rv;
247fa9e4066Sahrens 
248c25056deSgw25295 	ASSERT(rwlp->initialized == B_TRUE);
249fa9e4066Sahrens 	ASSERT(rwlp->rw_owner != (void *)-1UL);
250fa9e4066Sahrens 
251b3d32f0cSBryan Cantrill 	if (rw == RW_WRITER)
252fa9e4066Sahrens 		rv = rw_trywrlock(&rwlp->rw_lock);
253b3d32f0cSBryan Cantrill 	else
254b3d32f0cSBryan Cantrill 		rv = rw_tryrdlock(&rwlp->rw_lock);
255fa9e4066Sahrens 
256fa9e4066Sahrens 	if (rv == 0) {
257fa9e4066Sahrens 		rwlp->rw_owner = curthread;
258fa9e4066Sahrens 		return (1);
259fa9e4066Sahrens 	}
260fa9e4066Sahrens 
261fa9e4066Sahrens 	return (0);
262fa9e4066Sahrens }
263fa9e4066Sahrens 
264fa9e4066Sahrens /*ARGSUSED*/
265fa9e4066Sahrens int
266fa9e4066Sahrens rw_tryupgrade(krwlock_t *rwlp)
267fa9e4066Sahrens {
268c25056deSgw25295 	ASSERT(rwlp->initialized == B_TRUE);
269fa9e4066Sahrens 	ASSERT(rwlp->rw_owner != (void *)-1UL);
270fa9e4066Sahrens 
271fa9e4066Sahrens 	return (0);
272fa9e4066Sahrens }
273fa9e4066Sahrens 
274fa9e4066Sahrens /*
275fa9e4066Sahrens  * =========================================================================
276fa9e4066Sahrens  * condition variables
277fa9e4066Sahrens  * =========================================================================
278fa9e4066Sahrens  */
279fa9e4066Sahrens /*ARGSUSED*/
280fa9e4066Sahrens void
281fa9e4066Sahrens cv_init(kcondvar_t *cv, char *name, int type, void *arg)
282fa9e4066Sahrens {
2835ad82045Snd150628 	VERIFY(cond_init(cv, type, NULL) == 0);
284fa9e4066Sahrens }
285fa9e4066Sahrens 
286fa9e4066Sahrens void
287fa9e4066Sahrens cv_destroy(kcondvar_t *cv)
288fa9e4066Sahrens {
2895ad82045Snd150628 	VERIFY(cond_destroy(cv) == 0);
290fa9e4066Sahrens }
291fa9e4066Sahrens 
292fa9e4066Sahrens void
293fa9e4066Sahrens cv_wait(kcondvar_t *cv, kmutex_t *mp)
294fa9e4066Sahrens {
295fa9e4066Sahrens 	ASSERT(mutex_owner(mp) == curthread);
296fa9e4066Sahrens 	mp->m_owner = NULL;
2975ad82045Snd150628 	int ret = cond_wait(cv, &mp->m_lock);
2985ad82045Snd150628 	VERIFY(ret == 0 || ret == EINTR);
299fa9e4066Sahrens 	mp->m_owner = curthread;
300fa9e4066Sahrens }
301fa9e4066Sahrens 
302fa9e4066Sahrens clock_t
303fa9e4066Sahrens cv_timedwait(kcondvar_t *cv, kmutex_t *mp, clock_t abstime)
304fa9e4066Sahrens {
305fa9e4066Sahrens 	int error;
306fa9e4066Sahrens 	timestruc_t ts;
307fa9e4066Sahrens 	clock_t delta;
308fa9e4066Sahrens 
309fa9e4066Sahrens top:
310d3d50737SRafael Vanoni 	delta = abstime - ddi_get_lbolt();
311fa9e4066Sahrens 	if (delta <= 0)
312fa9e4066Sahrens 		return (-1);
313fa9e4066Sahrens 
314fa9e4066Sahrens 	ts.tv_sec = delta / hz;
315fa9e4066Sahrens 	ts.tv_nsec = (delta % hz) * (NANOSEC / hz);
316fa9e4066Sahrens 
317fa9e4066Sahrens 	ASSERT(mutex_owner(mp) == curthread);
318fa9e4066Sahrens 	mp->m_owner = NULL;
319fa9e4066Sahrens 	error = cond_reltimedwait(cv, &mp->m_lock, &ts);
320fa9e4066Sahrens 	mp->m_owner = curthread;
321fa9e4066Sahrens 
322fa9e4066Sahrens 	if (error == ETIME)
323fa9e4066Sahrens 		return (-1);
324fa9e4066Sahrens 
325fa9e4066Sahrens 	if (error == EINTR)
326fa9e4066Sahrens 		goto top;
327fa9e4066Sahrens 
328fa9e4066Sahrens 	ASSERT(error == 0);
329fa9e4066Sahrens 
330fa9e4066Sahrens 	return (1);
331fa9e4066Sahrens }
332fa9e4066Sahrens 
3330689f76cSAdam Leventhal /*ARGSUSED*/
3340689f76cSAdam Leventhal clock_t
3350689f76cSAdam Leventhal cv_timedwait_hires(kcondvar_t *cv, kmutex_t *mp, hrtime_t tim, hrtime_t res,
3360689f76cSAdam Leventhal     int flag)
3370689f76cSAdam Leventhal {
3380689f76cSAdam Leventhal 	int error;
3390689f76cSAdam Leventhal 	timestruc_t ts;
3400689f76cSAdam Leventhal 	hrtime_t delta;
3410689f76cSAdam Leventhal 
3420689f76cSAdam Leventhal 	ASSERT(flag == 0);
3430689f76cSAdam Leventhal 
3440689f76cSAdam Leventhal top:
3450689f76cSAdam Leventhal 	delta = tim - gethrtime();
3460689f76cSAdam Leventhal 	if (delta <= 0)
3470689f76cSAdam Leventhal 		return (-1);
3480689f76cSAdam Leventhal 
3490689f76cSAdam Leventhal 	ts.tv_sec = delta / NANOSEC;
3500689f76cSAdam Leventhal 	ts.tv_nsec = delta % NANOSEC;
3510689f76cSAdam Leventhal 
3520689f76cSAdam Leventhal 	ASSERT(mutex_owner(mp) == curthread);
3530689f76cSAdam Leventhal 	mp->m_owner = NULL;
3540689f76cSAdam Leventhal 	error = cond_reltimedwait(cv, &mp->m_lock, &ts);
3550689f76cSAdam Leventhal 	mp->m_owner = curthread;
3560689f76cSAdam Leventhal 
3570689f76cSAdam Leventhal 	if (error == ETIME)
3580689f76cSAdam Leventhal 		return (-1);
3590689f76cSAdam Leventhal 
3600689f76cSAdam Leventhal 	if (error == EINTR)
3610689f76cSAdam Leventhal 		goto top;
3620689f76cSAdam Leventhal 
3630689f76cSAdam Leventhal 	ASSERT(error == 0);
3640689f76cSAdam Leventhal 
3650689f76cSAdam Leventhal 	return (1);
3660689f76cSAdam Leventhal }
3670689f76cSAdam Leventhal 
368fa9e4066Sahrens void
369fa9e4066Sahrens cv_signal(kcondvar_t *cv)
370fa9e4066Sahrens {
3715ad82045Snd150628 	VERIFY(cond_signal(cv) == 0);
372fa9e4066Sahrens }
373fa9e4066Sahrens 
374fa9e4066Sahrens void
375fa9e4066Sahrens cv_broadcast(kcondvar_t *cv)
376fa9e4066Sahrens {
3775ad82045Snd150628 	VERIFY(cond_broadcast(cv) == 0);
378fa9e4066Sahrens }
379fa9e4066Sahrens 
380fa9e4066Sahrens /*
381fa9e4066Sahrens  * =========================================================================
382fa9e4066Sahrens  * vnode operations
383fa9e4066Sahrens  * =========================================================================
384fa9e4066Sahrens  */
385fa9e4066Sahrens /*
386fa9e4066Sahrens  * Note: for the xxxat() versions of these functions, we assume that the
387fa9e4066Sahrens  * starting vp is always rootdir (which is true for spa_directory.c, the only
388fa9e4066Sahrens  * ZFS consumer of these interfaces).  We assert this is true, and then emulate
389fa9e4066Sahrens  * them by adding '/' in front of the path.
390fa9e4066Sahrens  */
391fa9e4066Sahrens 
392fa9e4066Sahrens /*ARGSUSED*/
393fa9e4066Sahrens int
394fa9e4066Sahrens vn_open(char *path, int x1, int flags, int mode, vnode_t **vpp, int x2, int x3)
395fa9e4066Sahrens {
396fa9e4066Sahrens 	int fd;
397fa9e4066Sahrens 	vnode_t *vp;
398fa9e4066Sahrens 	int old_umask;
399fa9e4066Sahrens 	char realpath[MAXPATHLEN];
400fa9e4066Sahrens 	struct stat64 st;
401fa9e4066Sahrens 
402fa9e4066Sahrens 	/*
403fa9e4066Sahrens 	 * If we're accessing a real disk from userland, we need to use
404fa9e4066Sahrens 	 * the character interface to avoid caching.  This is particularly
405fa9e4066Sahrens 	 * important if we're trying to look at a real in-kernel storage
406fa9e4066Sahrens 	 * pool from userland, e.g. via zdb, because otherwise we won't
407fa9e4066Sahrens 	 * see the changes occurring under the segmap cache.
408fa9e4066Sahrens 	 * On the other hand, the stupid character device returns zero
409fa9e4066Sahrens 	 * for its size.  So -- gag -- we open the block device to get
410fa9e4066Sahrens 	 * its size, and remember it for subsequent VOP_GETATTR().
411fa9e4066Sahrens 	 */
412fa9e4066Sahrens 	if (strncmp(path, "/dev/", 5) == 0) {
413fa9e4066Sahrens 		char *dsk;
414fa9e4066Sahrens 		fd = open64(path, O_RDONLY);
415fa9e4066Sahrens 		if (fd == -1)
416fa9e4066Sahrens 			return (errno);
417fa9e4066Sahrens 		if (fstat64(fd, &st) == -1) {
418fa9e4066Sahrens 			close(fd);
419fa9e4066Sahrens 			return (errno);
420fa9e4066Sahrens 		}
421fa9e4066Sahrens 		close(fd);
422fa9e4066Sahrens 		(void) sprintf(realpath, "%s", path);
423fa9e4066Sahrens 		dsk = strstr(path, "/dsk/");
424fa9e4066Sahrens 		if (dsk != NULL)
425fa9e4066Sahrens 			(void) sprintf(realpath + (dsk - path) + 1, "r%s",
426fa9e4066Sahrens 			    dsk + 1);
427fa9e4066Sahrens 	} else {
428fa9e4066Sahrens 		(void) sprintf(realpath, "%s", path);
429fa9e4066Sahrens 		if (!(flags & FCREAT) && stat64(realpath, &st) == -1)
430fa9e4066Sahrens 			return (errno);
431fa9e4066Sahrens 	}
432fa9e4066Sahrens 
433fa9e4066Sahrens 	if (flags & FCREAT)
434fa9e4066Sahrens 		old_umask = umask(0);
435fa9e4066Sahrens 
436fa9e4066Sahrens 	/*
437fa9e4066Sahrens 	 * The construct 'flags - FREAD' conveniently maps combinations of
438fa9e4066Sahrens 	 * FREAD and FWRITE to the corresponding O_RDONLY, O_WRONLY, and O_RDWR.
439fa9e4066Sahrens 	 */
440fa9e4066Sahrens 	fd = open64(realpath, flags - FREAD, mode);
441fa9e4066Sahrens 
442fa9e4066Sahrens 	if (flags & FCREAT)
443fa9e4066Sahrens 		(void) umask(old_umask);
444fa9e4066Sahrens 
445fa9e4066Sahrens 	if (fd == -1)
446fa9e4066Sahrens 		return (errno);
447fa9e4066Sahrens 
448fa9e4066Sahrens 	if (fstat64(fd, &st) == -1) {
449fa9e4066Sahrens 		close(fd);
450fa9e4066Sahrens 		return (errno);
451fa9e4066Sahrens 	}
452fa9e4066Sahrens 
453fa9e4066Sahrens 	(void) fcntl(fd, F_SETFD, FD_CLOEXEC);
454fa9e4066Sahrens 
455fa9e4066Sahrens 	*vpp = vp = umem_zalloc(sizeof (vnode_t), UMEM_NOFAIL);
456fa9e4066Sahrens 
457fa9e4066Sahrens 	vp->v_fd = fd;
458fa9e4066Sahrens 	vp->v_size = st.st_size;
459fa9e4066Sahrens 	vp->v_path = spa_strdup(path);
460fa9e4066Sahrens 
461fa9e4066Sahrens 	return (0);
462fa9e4066Sahrens }
463fa9e4066Sahrens 
464da6c28aaSamw /*ARGSUSED*/
465fa9e4066Sahrens int
466fa9e4066Sahrens vn_openat(char *path, int x1, int flags, int mode, vnode_t **vpp, int x2,
467da6c28aaSamw     int x3, vnode_t *startvp, int fd)
468fa9e4066Sahrens {
469fa9e4066Sahrens 	char *realpath = umem_alloc(strlen(path) + 2, UMEM_NOFAIL);
470fa9e4066Sahrens 	int ret;
471fa9e4066Sahrens 
472fa9e4066Sahrens 	ASSERT(startvp == rootdir);
473fa9e4066Sahrens 	(void) sprintf(realpath, "/%s", path);
474fa9e4066Sahrens 
475da6c28aaSamw 	/* fd ignored for now, need if want to simulate nbmand support */
476fa9e4066Sahrens 	ret = vn_open(realpath, x1, flags, mode, vpp, x2, x3);
477fa9e4066Sahrens 
478fa9e4066Sahrens 	umem_free(realpath, strlen(path) + 2);
479fa9e4066Sahrens 
480fa9e4066Sahrens 	return (ret);
481fa9e4066Sahrens }
482fa9e4066Sahrens 
483fa9e4066Sahrens /*ARGSUSED*/
484fa9e4066Sahrens int
485fa9e4066Sahrens vn_rdwr(int uio, vnode_t *vp, void *addr, ssize_t len, offset_t offset,
486fa9e4066Sahrens 	int x1, int x2, rlim64_t x3, void *x4, ssize_t *residp)
487fa9e4066Sahrens {
488fa9e4066Sahrens 	ssize_t iolen, split;
489fa9e4066Sahrens 
490fa9e4066Sahrens 	if (uio == UIO_READ) {
491fa9e4066Sahrens 		iolen = pread64(vp->v_fd, addr, len, offset);
492fa9e4066Sahrens 	} else {
493fa9e4066Sahrens 		/*
494fa9e4066Sahrens 		 * To simulate partial disk writes, we split writes into two
495fa9e4066Sahrens 		 * system calls so that the process can be killed in between.
496fa9e4066Sahrens 		 */
497ad135b5dSChristopher Siden 		int sectors = len >> SPA_MINBLOCKSHIFT;
498ad135b5dSChristopher Siden 		split = (sectors > 0 ? rand() % sectors : 0) <<
499ad135b5dSChristopher Siden 		    SPA_MINBLOCKSHIFT;
500fa9e4066Sahrens 		iolen = pwrite64(vp->v_fd, addr, split, offset);
501fa9e4066Sahrens 		iolen += pwrite64(vp->v_fd, (char *)addr + split,
502fa9e4066Sahrens 		    len - split, offset + split);
503fa9e4066Sahrens 	}
504fa9e4066Sahrens 
505fa9e4066Sahrens 	if (iolen == -1)
506fa9e4066Sahrens 		return (errno);
507fa9e4066Sahrens 	if (residp)
508fa9e4066Sahrens 		*residp = len - iolen;
509fa9e4066Sahrens 	else if (iolen != len)
510fa9e4066Sahrens 		return (EIO);
511fa9e4066Sahrens 	return (0);
512fa9e4066Sahrens }
513fa9e4066Sahrens 
514fa9e4066Sahrens void
515fa9e4066Sahrens vn_close(vnode_t *vp)
516fa9e4066Sahrens {
517fa9e4066Sahrens 	close(vp->v_fd);
518fa9e4066Sahrens 	spa_strfree(vp->v_path);
519fa9e4066Sahrens 	umem_free(vp, sizeof (vnode_t));
520fa9e4066Sahrens }
521fa9e4066Sahrens 
522095bcd66SGeorge Wilson /*
523095bcd66SGeorge Wilson  * At a minimum we need to update the size since vdev_reopen()
524095bcd66SGeorge Wilson  * will no longer call vn_openat().
525095bcd66SGeorge Wilson  */
526095bcd66SGeorge Wilson int
527095bcd66SGeorge Wilson fop_getattr(vnode_t *vp, vattr_t *vap)
528095bcd66SGeorge Wilson {
529095bcd66SGeorge Wilson 	struct stat64 st;
530095bcd66SGeorge Wilson 
531095bcd66SGeorge Wilson 	if (fstat64(vp->v_fd, &st) == -1) {
532095bcd66SGeorge Wilson 		close(vp->v_fd);
533095bcd66SGeorge Wilson 		return (errno);
534095bcd66SGeorge Wilson 	}
535095bcd66SGeorge Wilson 
536095bcd66SGeorge Wilson 	vap->va_size = st.st_size;
537095bcd66SGeorge Wilson 	return (0);
538095bcd66SGeorge Wilson }
539095bcd66SGeorge Wilson 
540fa9e4066Sahrens #ifdef ZFS_DEBUG
541fa9e4066Sahrens 
542fa9e4066Sahrens /*
543fa9e4066Sahrens  * =========================================================================
544fa9e4066Sahrens  * Figure out which debugging statements to print
545fa9e4066Sahrens  * =========================================================================
546fa9e4066Sahrens  */
547fa9e4066Sahrens 
548fa9e4066Sahrens static char *dprintf_string;
549fa9e4066Sahrens static int dprintf_print_all;
550fa9e4066Sahrens 
551fa9e4066Sahrens int
552fa9e4066Sahrens dprintf_find_string(const char *string)
553fa9e4066Sahrens {
554fa9e4066Sahrens 	char *tmp_str = dprintf_string;
555fa9e4066Sahrens 	int len = strlen(string);
556fa9e4066Sahrens 
557fa9e4066Sahrens 	/*
558fa9e4066Sahrens 	 * Find out if this is a string we want to print.
559fa9e4066Sahrens 	 * String format: file1.c,function_name1,file2.c,file3.c
560fa9e4066Sahrens 	 */
561fa9e4066Sahrens 
562fa9e4066Sahrens 	while (tmp_str != NULL) {
563fa9e4066Sahrens 		if (strncmp(tmp_str, string, len) == 0 &&
564fa9e4066Sahrens 		    (tmp_str[len] == ',' || tmp_str[len] == '\0'))
565fa9e4066Sahrens 			return (1);
566fa9e4066Sahrens 		tmp_str = strchr(tmp_str, ',');
567fa9e4066Sahrens 		if (tmp_str != NULL)
568fa9e4066Sahrens 			tmp_str++; /* Get rid of , */
569fa9e4066Sahrens 	}
570fa9e4066Sahrens 	return (0);
571fa9e4066Sahrens }
572fa9e4066Sahrens 
573fa9e4066Sahrens void
574fa9e4066Sahrens dprintf_setup(int *argc, char **argv)
575fa9e4066Sahrens {
576fa9e4066Sahrens 	int i, j;
577fa9e4066Sahrens 
578fa9e4066Sahrens 	/*
579fa9e4066Sahrens 	 * Debugging can be specified two ways: by setting the
580fa9e4066Sahrens 	 * environment variable ZFS_DEBUG, or by including a
581fa9e4066Sahrens 	 * "debug=..."  argument on the command line.  The command
582fa9e4066Sahrens 	 * line setting overrides the environment variable.
583fa9e4066Sahrens 	 */
584fa9e4066Sahrens 
585fa9e4066Sahrens 	for (i = 1; i < *argc; i++) {
586fa9e4066Sahrens 		int len = strlen("debug=");
587fa9e4066Sahrens 		/* First look for a command line argument */
588fa9e4066Sahrens 		if (strncmp("debug=", argv[i], len) == 0) {
589fa9e4066Sahrens 			dprintf_string = argv[i] + len;
590fa9e4066Sahrens 			/* Remove from args */
591fa9e4066Sahrens 			for (j = i; j < *argc; j++)
592fa9e4066Sahrens 				argv[j] = argv[j+1];
593fa9e4066Sahrens 			argv[j] = NULL;
594fa9e4066Sahrens 			(*argc)--;
595fa9e4066Sahrens 		}
596fa9e4066Sahrens 	}
597fa9e4066Sahrens 
598fa9e4066Sahrens 	if (dprintf_string == NULL) {
599fa9e4066Sahrens 		/* Look for ZFS_DEBUG environment variable */
600fa9e4066Sahrens 		dprintf_string = getenv("ZFS_DEBUG");
601fa9e4066Sahrens 	}
602fa9e4066Sahrens 
603fa9e4066Sahrens 	/*
604fa9e4066Sahrens 	 * Are we just turning on all debugging?
605fa9e4066Sahrens 	 */
606fa9e4066Sahrens 	if (dprintf_find_string("on"))
607fa9e4066Sahrens 		dprintf_print_all = 1;
608fa9e4066Sahrens }
609fa9e4066Sahrens 
610fa9e4066Sahrens /*
611fa9e4066Sahrens  * =========================================================================
612fa9e4066Sahrens  * debug printfs
613fa9e4066Sahrens  * =========================================================================
614fa9e4066Sahrens  */
615fa9e4066Sahrens void
616fa9e4066Sahrens __dprintf(const char *file, const char *func, int line, const char *fmt, ...)
617fa9e4066Sahrens {
618fa9e4066Sahrens 	const char *newfile;
619fa9e4066Sahrens 	va_list adx;
620fa9e4066Sahrens 
621fa9e4066Sahrens 	/*
622fa9e4066Sahrens 	 * Get rid of annoying "../common/" prefix to filename.
623fa9e4066Sahrens 	 */
624fa9e4066Sahrens 	newfile = strrchr(file, '/');
625fa9e4066Sahrens 	if (newfile != NULL) {
626fa9e4066Sahrens 		newfile = newfile + 1; /* Get rid of leading / */
627fa9e4066Sahrens 	} else {
628fa9e4066Sahrens 		newfile = file;
629fa9e4066Sahrens 	}
630fa9e4066Sahrens 
631fa9e4066Sahrens 	if (dprintf_print_all ||
632fa9e4066Sahrens 	    dprintf_find_string(newfile) ||
633fa9e4066Sahrens 	    dprintf_find_string(func)) {
634fa9e4066Sahrens 		/* Print out just the function name if requested */
635fa9e4066Sahrens 		flockfile(stdout);
636fa9e4066Sahrens 		if (dprintf_find_string("pid"))
637fa9e4066Sahrens 			(void) printf("%d ", getpid());
638fa9e4066Sahrens 		if (dprintf_find_string("tid"))
639fa9e4066Sahrens 			(void) printf("%u ", thr_self());
640fa9e4066Sahrens 		if (dprintf_find_string("cpu"))
641fa9e4066Sahrens 			(void) printf("%u ", getcpuid());
642fa9e4066Sahrens 		if (dprintf_find_string("time"))
643fa9e4066Sahrens 			(void) printf("%llu ", gethrtime());
644fa9e4066Sahrens 		if (dprintf_find_string("long"))
645fa9e4066Sahrens 			(void) printf("%s, line %d: ", newfile, line);
646fa9e4066Sahrens 		(void) printf("%s: ", func);
647fa9e4066Sahrens 		va_start(adx, fmt);
648fa9e4066Sahrens 		(void) vprintf(fmt, adx);
649fa9e4066Sahrens 		va_end(adx);
650fa9e4066Sahrens 		funlockfile(stdout);
651fa9e4066Sahrens 	}
652fa9e4066Sahrens }
653fa9e4066Sahrens 
654fa9e4066Sahrens #endif /* ZFS_DEBUG */
655fa9e4066Sahrens 
656fa9e4066Sahrens /*
657fa9e4066Sahrens  * =========================================================================
658fa9e4066Sahrens  * cmn_err() and panic()
659fa9e4066Sahrens  * =========================================================================
660fa9e4066Sahrens  */
661fa9e4066Sahrens static char ce_prefix[CE_IGNORE][10] = { "", "NOTICE: ", "WARNING: ", "" };
662fa9e4066Sahrens static char ce_suffix[CE_IGNORE][2] = { "", "\n", "\n", "" };
663fa9e4066Sahrens 
664fa9e4066Sahrens void
665fa9e4066Sahrens vpanic(const char *fmt, va_list adx)
666fa9e4066Sahrens {
667fa9e4066Sahrens 	(void) fprintf(stderr, "error: ");
668fa9e4066Sahrens 	(void) vfprintf(stderr, fmt, adx);
669fa9e4066Sahrens 	(void) fprintf(stderr, "\n");
670fa9e4066Sahrens 
671fa9e4066Sahrens 	abort();	/* think of it as a "user-level crash dump" */
672fa9e4066Sahrens }
673fa9e4066Sahrens 
674fa9e4066Sahrens void
675fa9e4066Sahrens panic(const char *fmt, ...)
676fa9e4066Sahrens {
677fa9e4066Sahrens 	va_list adx;
678fa9e4066Sahrens 
679fa9e4066Sahrens 	va_start(adx, fmt);
680fa9e4066Sahrens 	vpanic(fmt, adx);
681fa9e4066Sahrens 	va_end(adx);
682fa9e4066Sahrens }
683fa9e4066Sahrens 
684fa9e4066Sahrens void
6850125049cSahrens vcmn_err(int ce, const char *fmt, va_list adx)
686fa9e4066Sahrens {
687fa9e4066Sahrens 	if (ce == CE_PANIC)
688fa9e4066Sahrens 		vpanic(fmt, adx);
689fa9e4066Sahrens 	if (ce != CE_NOTE) {	/* suppress noise in userland stress testing */
690fa9e4066Sahrens 		(void) fprintf(stderr, "%s", ce_prefix[ce]);
691fa9e4066Sahrens 		(void) vfprintf(stderr, fmt, adx);
692fa9e4066Sahrens 		(void) fprintf(stderr, "%s", ce_suffix[ce]);
693fa9e4066Sahrens 	}
6940125049cSahrens }
6950125049cSahrens 
6960125049cSahrens /*PRINTFLIKE2*/
6970125049cSahrens void
6980125049cSahrens cmn_err(int ce, const char *fmt, ...)
6990125049cSahrens {
7000125049cSahrens 	va_list adx;
7010125049cSahrens 
7020125049cSahrens 	va_start(adx, fmt);
7030125049cSahrens 	vcmn_err(ce, fmt, adx);
704fa9e4066Sahrens 	va_end(adx);
705fa9e4066Sahrens }
706fa9e4066Sahrens 
707fa9e4066Sahrens /*
708fa9e4066Sahrens  * =========================================================================
709ea8dc4b6Seschrock  * kobj interfaces
710ea8dc4b6Seschrock  * =========================================================================
711ea8dc4b6Seschrock  */
712ea8dc4b6Seschrock struct _buf *
713ea8dc4b6Seschrock kobj_open_file(char *name)
714ea8dc4b6Seschrock {
715ea8dc4b6Seschrock 	struct _buf *file;
716ea8dc4b6Seschrock 	vnode_t *vp;
717ea8dc4b6Seschrock 
718ea8dc4b6Seschrock 	/* set vp as the _fd field of the file */
719da6c28aaSamw 	if (vn_openat(name, UIO_SYSSPACE, FREAD, 0, &vp, 0, 0, rootdir,
720da6c28aaSamw 	    -1) != 0)
721ea8dc4b6Seschrock 		return ((void *)-1UL);
722ea8dc4b6Seschrock 
723ea8dc4b6Seschrock 	file = umem_zalloc(sizeof (struct _buf), UMEM_NOFAIL);
724ea8dc4b6Seschrock 	file->_fd = (intptr_t)vp;
725ea8dc4b6Seschrock 	return (file);
726ea8dc4b6Seschrock }
727ea8dc4b6Seschrock 
728ea8dc4b6Seschrock int
729ea8dc4b6Seschrock kobj_read_file(struct _buf *file, char *buf, unsigned size, unsigned off)
730ea8dc4b6Seschrock {
731ea8dc4b6Seschrock 	ssize_t resid;
732ea8dc4b6Seschrock 
733ea8dc4b6Seschrock 	vn_rdwr(UIO_READ, (vnode_t *)file->_fd, buf, size, (offset_t)off,
734ea8dc4b6Seschrock 	    UIO_SYSSPACE, 0, 0, 0, &resid);
735ea8dc4b6Seschrock 
736b1b8ab34Slling 	return (size - resid);
737ea8dc4b6Seschrock }
738ea8dc4b6Seschrock 
739ea8dc4b6Seschrock void
740ea8dc4b6Seschrock kobj_close_file(struct _buf *file)
741ea8dc4b6Seschrock {
742ea8dc4b6Seschrock 	vn_close((vnode_t *)file->_fd);
743ea8dc4b6Seschrock 	umem_free(file, sizeof (struct _buf));
744ea8dc4b6Seschrock }
745ea8dc4b6Seschrock 
746ea8dc4b6Seschrock int
747b1b8ab34Slling kobj_get_filesize(struct _buf *file, uint64_t *size)
748ea8dc4b6Seschrock {
749ea8dc4b6Seschrock 	struct stat64 st;
750b1b8ab34Slling 	vnode_t *vp = (vnode_t *)file->_fd;
751b1b8ab34Slling 
752ea8dc4b6Seschrock 	if (fstat64(vp->v_fd, &st) == -1) {
753ea8dc4b6Seschrock 		vn_close(vp);
754ea8dc4b6Seschrock 		return (errno);
755ea8dc4b6Seschrock 	}
756b1b8ab34Slling 	*size = st.st_size;
757ea8dc4b6Seschrock 	return (0);
758ea8dc4b6Seschrock }
759ea8dc4b6Seschrock 
760ea8dc4b6Seschrock /*
761ea8dc4b6Seschrock  * =========================================================================
762fa9e4066Sahrens  * misc routines
763fa9e4066Sahrens  * =========================================================================
764fa9e4066Sahrens  */
765fa9e4066Sahrens 
766fa9e4066Sahrens void
767fa9e4066Sahrens delay(clock_t ticks)
768fa9e4066Sahrens {
769fa9e4066Sahrens 	poll(0, 0, ticks * (1000 / hz));
770fa9e4066Sahrens }
771fa9e4066Sahrens 
772fa9e4066Sahrens /*
773fa9e4066Sahrens  * Find highest one bit set.
774fa9e4066Sahrens  *	Returns bit number + 1 of highest bit that is set, otherwise returns 0.
775fa9e4066Sahrens  */
776fa9e4066Sahrens int
777*bf16b11eSMatthew Ahrens highbit64(uint64_t i)
778fa9e4066Sahrens {
779*bf16b11eSMatthew Ahrens 	int h = 1;
780fa9e4066Sahrens 
781fa9e4066Sahrens 	if (i == 0)
782fa9e4066Sahrens 		return (0);
783*bf16b11eSMatthew Ahrens 	if (i & 0xffffffff00000000ULL) {
784fa9e4066Sahrens 		h += 32; i >>= 32;
785fa9e4066Sahrens 	}
786fa9e4066Sahrens 	if (i & 0xffff0000) {
787fa9e4066Sahrens 		h += 16; i >>= 16;
788fa9e4066Sahrens 	}
789fa9e4066Sahrens 	if (i & 0xff00) {
790fa9e4066Sahrens 		h += 8; i >>= 8;
791fa9e4066Sahrens 	}
792fa9e4066Sahrens 	if (i & 0xf0) {
793fa9e4066Sahrens 		h += 4; i >>= 4;
794fa9e4066Sahrens 	}
795fa9e4066Sahrens 	if (i & 0xc) {
796fa9e4066Sahrens 		h += 2; i >>= 2;
797fa9e4066Sahrens 	}
798fa9e4066Sahrens 	if (i & 0x2) {
799fa9e4066Sahrens 		h += 1;
800fa9e4066Sahrens 	}
801fa9e4066Sahrens 	return (h);
802fa9e4066Sahrens }
803fa9e4066Sahrens 
80417f17c2dSbonwick static int random_fd = -1, urandom_fd = -1;
80517f17c2dSbonwick 
806fa9e4066Sahrens static int
80717f17c2dSbonwick random_get_bytes_common(uint8_t *ptr, size_t len, int fd)
808fa9e4066Sahrens {
809fa9e4066Sahrens 	size_t resid = len;
810fa9e4066Sahrens 	ssize_t bytes;
811fa9e4066Sahrens 
812fa9e4066Sahrens 	ASSERT(fd != -1);
813fa9e4066Sahrens 
814fa9e4066Sahrens 	while (resid != 0) {
815fa9e4066Sahrens 		bytes = read(fd, ptr, resid);
81617f17c2dSbonwick 		ASSERT3S(bytes, >=, 0);
817fa9e4066Sahrens 		ptr += bytes;
818fa9e4066Sahrens 		resid -= bytes;
819fa9e4066Sahrens 	}
820fa9e4066Sahrens 
821fa9e4066Sahrens 	return (0);
822fa9e4066Sahrens }
823fa9e4066Sahrens 
824fa9e4066Sahrens int
825fa9e4066Sahrens random_get_bytes(uint8_t *ptr, size_t len)
826fa9e4066Sahrens {
82717f17c2dSbonwick 	return (random_get_bytes_common(ptr, len, random_fd));
828fa9e4066Sahrens }
829fa9e4066Sahrens 
830fa9e4066Sahrens int
831fa9e4066Sahrens random_get_pseudo_bytes(uint8_t *ptr, size_t len)
832fa9e4066Sahrens {
83317f17c2dSbonwick 	return (random_get_bytes_common(ptr, len, urandom_fd));
834fa9e4066Sahrens }
835fa9e4066Sahrens 
83695173954Sek110237 int
83795173954Sek110237 ddi_strtoul(const char *hw_serial, char **nptr, int base, unsigned long *result)
83895173954Sek110237 {
83995173954Sek110237 	char *end;
84095173954Sek110237 
84195173954Sek110237 	*result = strtoul(hw_serial, &end, base);
84295173954Sek110237 	if (*result == 0)
84395173954Sek110237 		return (errno);
84495173954Sek110237 	return (0);
84595173954Sek110237 }
84695173954Sek110237 
8470a586ceaSMark Shellenbaum int
8480a586ceaSMark Shellenbaum ddi_strtoull(const char *str, char **nptr, int base, u_longlong_t *result)
8490a586ceaSMark Shellenbaum {
8500a586ceaSMark Shellenbaum 	char *end;
8510a586ceaSMark Shellenbaum 
8520a586ceaSMark Shellenbaum 	*result = strtoull(str, &end, base);
8530a586ceaSMark Shellenbaum 	if (*result == 0)
8540a586ceaSMark Shellenbaum 		return (errno);
8550a586ceaSMark Shellenbaum 	return (0);
8560a586ceaSMark Shellenbaum }
8570a586ceaSMark Shellenbaum 
858283b8460SGeorge.Wilson /* ARGSUSED */
859283b8460SGeorge.Wilson cyclic_id_t
860283b8460SGeorge.Wilson cyclic_add(cyc_handler_t *hdlr, cyc_time_t *when)
861283b8460SGeorge.Wilson {
862283b8460SGeorge.Wilson 	return (1);
863283b8460SGeorge.Wilson }
864283b8460SGeorge.Wilson 
865283b8460SGeorge.Wilson /* ARGSUSED */
866283b8460SGeorge.Wilson void
867283b8460SGeorge.Wilson cyclic_remove(cyclic_id_t id)
868283b8460SGeorge.Wilson {
869283b8460SGeorge.Wilson }
870283b8460SGeorge.Wilson 
871283b8460SGeorge.Wilson /* ARGSUSED */
872283b8460SGeorge.Wilson int
873283b8460SGeorge.Wilson cyclic_reprogram(cyclic_id_t id, hrtime_t expiration)
874283b8460SGeorge.Wilson {
875283b8460SGeorge.Wilson 	return (1);
876283b8460SGeorge.Wilson }
877283b8460SGeorge.Wilson 
878fa9e4066Sahrens /*
879fa9e4066Sahrens  * =========================================================================
880fa9e4066Sahrens  * kernel emulation setup & teardown
881fa9e4066Sahrens  * =========================================================================
882fa9e4066Sahrens  */
883fa9e4066Sahrens static int
884fa9e4066Sahrens umem_out_of_memory(void)
885fa9e4066Sahrens {
886fa9e4066Sahrens 	char errmsg[] = "out of memory -- generating core dump\n";
887fa9e4066Sahrens 
888fa9e4066Sahrens 	write(fileno(stderr), errmsg, sizeof (errmsg));
889fa9e4066Sahrens 	abort();
890fa9e4066Sahrens 	return (0);
891fa9e4066Sahrens }
892fa9e4066Sahrens 
893fa9e4066Sahrens void
894fa9e4066Sahrens kernel_init(int mode)
895fa9e4066Sahrens {
8963b2aab18SMatthew Ahrens 	extern uint_t rrw_tsd_key;
8973b2aab18SMatthew Ahrens 
898fa9e4066Sahrens 	umem_nofail_callback(umem_out_of_memory);
899fa9e4066Sahrens 
900fa9e4066Sahrens 	physmem = sysconf(_SC_PHYS_PAGES);
901fa9e4066Sahrens 
902fa9e4066Sahrens 	dprintf("physmem = %llu pages (%.2f GB)\n", physmem,
903fa9e4066Sahrens 	    (double)physmem * sysconf(_SC_PAGE_SIZE) / (1ULL << 30));
904fa9e4066Sahrens 
9053ad6c7f9SVictor Latushkin 	(void) snprintf(hw_serial, sizeof (hw_serial), "%ld",
9063ad6c7f9SVictor Latushkin 	    (mode & FWRITE) ? gethostid() : 0);
90795173954Sek110237 
90817f17c2dSbonwick 	VERIFY((random_fd = open("/dev/random", O_RDONLY)) != -1);
90917f17c2dSbonwick 	VERIFY((urandom_fd = open("/dev/urandom", O_RDONLY)) != -1);
91017f17c2dSbonwick 
91188b7b0f2SMatthew Ahrens 	system_taskq_init();
91288b7b0f2SMatthew Ahrens 
913283b8460SGeorge.Wilson 	mutex_init(&cpu_lock, NULL, MUTEX_DEFAULT, NULL);
914283b8460SGeorge.Wilson 
915fa9e4066Sahrens 	spa_init(mode);
9163b2aab18SMatthew Ahrens 
9173b2aab18SMatthew Ahrens 	tsd_create(&rrw_tsd_key, rrw_tsd_destroy);
918fa9e4066Sahrens }
919fa9e4066Sahrens 
920fa9e4066Sahrens void
921fa9e4066Sahrens kernel_fini(void)
922fa9e4066Sahrens {
923fa9e4066Sahrens 	spa_fini();
92417f17c2dSbonwick 
925d20e665cSRicardo M. Correia 	system_taskq_fini();
926d20e665cSRicardo M. Correia 
92717f17c2dSbonwick 	close(random_fd);
92817f17c2dSbonwick 	close(urandom_fd);
92917f17c2dSbonwick 
93017f17c2dSbonwick 	random_fd = -1;
93117f17c2dSbonwick 	urandom_fd = -1;
932fa9e4066Sahrens }
933c9431fa1Sahl 
934c9431fa1Sahl int
935c9431fa1Sahl z_uncompress(void *dst, size_t *dstlen, const void *src, size_t srclen)
936c9431fa1Sahl {
937c9431fa1Sahl 	int ret;
938c9431fa1Sahl 	uLongf len = *dstlen;
939c9431fa1Sahl 
940c9431fa1Sahl 	if ((ret = uncompress(dst, &len, src, srclen)) == Z_OK)
941c9431fa1Sahl 		*dstlen = (size_t)len;
942c9431fa1Sahl 
943c9431fa1Sahl 	return (ret);
944c9431fa1Sahl }
945c9431fa1Sahl 
946c9431fa1Sahl int
947c9431fa1Sahl z_compress_level(void *dst, size_t *dstlen, const void *src, size_t srclen,
948c9431fa1Sahl     int level)
949c9431fa1Sahl {
950c9431fa1Sahl 	int ret;
951c9431fa1Sahl 	uLongf len = *dstlen;
952c9431fa1Sahl 
953c9431fa1Sahl 	if ((ret = compress2(dst, &len, src, srclen, level)) == Z_OK)
954c9431fa1Sahl 		*dstlen = (size_t)len;
955c9431fa1Sahl 
956c9431fa1Sahl 	return (ret);
957c9431fa1Sahl }
958ecd6cf80Smarks 
959ecd6cf80Smarks uid_t
960ecd6cf80Smarks crgetuid(cred_t *cr)
961ecd6cf80Smarks {
962ecd6cf80Smarks 	return (0);
963ecd6cf80Smarks }
964ecd6cf80Smarks 
9654445fffbSMatthew Ahrens uid_t
9664445fffbSMatthew Ahrens crgetruid(cred_t *cr)
9674445fffbSMatthew Ahrens {
9684445fffbSMatthew Ahrens 	return (0);
9694445fffbSMatthew Ahrens }
9704445fffbSMatthew Ahrens 
971ecd6cf80Smarks gid_t
972ecd6cf80Smarks crgetgid(cred_t *cr)
973ecd6cf80Smarks {
974ecd6cf80Smarks 	return (0);
975ecd6cf80Smarks }
976ecd6cf80Smarks 
977ecd6cf80Smarks int
978ecd6cf80Smarks crgetngroups(cred_t *cr)
979ecd6cf80Smarks {
980ecd6cf80Smarks 	return (0);
981ecd6cf80Smarks }
982ecd6cf80Smarks 
983ecd6cf80Smarks gid_t *
984ecd6cf80Smarks crgetgroups(cred_t *cr)
985ecd6cf80Smarks {
986ecd6cf80Smarks 	return (NULL);
987ecd6cf80Smarks }
988ecd6cf80Smarks 
989ecd6cf80Smarks int
990ecd6cf80Smarks zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
991ecd6cf80Smarks {
992ecd6cf80Smarks 	return (0);
993ecd6cf80Smarks }
994ecd6cf80Smarks 
995ecd6cf80Smarks int
996ecd6cf80Smarks zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
997ecd6cf80Smarks {
998ecd6cf80Smarks 	return (0);
999ecd6cf80Smarks }
1000ecd6cf80Smarks 
1001ecd6cf80Smarks int
1002ecd6cf80Smarks zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
1003ecd6cf80Smarks {
1004ecd6cf80Smarks 	return (0);
1005ecd6cf80Smarks }
1006e0d35c44Smarks 
1007e0d35c44Smarks ksiddomain_t *
1008e0d35c44Smarks ksid_lookupdomain(const char *dom)
1009e0d35c44Smarks {
1010e0d35c44Smarks 	ksiddomain_t *kd;
1011e0d35c44Smarks 
1012e0d35c44Smarks 	kd = umem_zalloc(sizeof (ksiddomain_t), UMEM_NOFAIL);
1013e0d35c44Smarks 	kd->kd_name = spa_strdup(dom);
1014e0d35c44Smarks 	return (kd);
1015e0d35c44Smarks }
1016e0d35c44Smarks 
1017e0d35c44Smarks void
1018e0d35c44Smarks ksiddomain_rele(ksiddomain_t *ksid)
1019e0d35c44Smarks {
1020e0d35c44Smarks 	spa_strfree(ksid->kd_name);
1021e0d35c44Smarks 	umem_free(ksid, sizeof (ksiddomain_t));
1022e0d35c44Smarks }
1023ae46e4c7SMatthew Ahrens 
1024ae46e4c7SMatthew Ahrens /*
1025ae46e4c7SMatthew Ahrens  * Do not change the length of the returned string; it must be freed
1026ae46e4c7SMatthew Ahrens  * with strfree().
1027ae46e4c7SMatthew Ahrens  */
1028ae46e4c7SMatthew Ahrens char *
1029ae46e4c7SMatthew Ahrens kmem_asprintf(const char *fmt, ...)
1030ae46e4c7SMatthew Ahrens {
1031ae46e4c7SMatthew Ahrens 	int size;
1032ae46e4c7SMatthew Ahrens 	va_list adx;
1033ae46e4c7SMatthew Ahrens 	char *buf;
1034ae46e4c7SMatthew Ahrens 
1035ae46e4c7SMatthew Ahrens 	va_start(adx, fmt);
1036ae46e4c7SMatthew Ahrens 	size = vsnprintf(NULL, 0, fmt, adx) + 1;
1037ae46e4c7SMatthew Ahrens 	va_end(adx);
1038ae46e4c7SMatthew Ahrens 
1039ae46e4c7SMatthew Ahrens 	buf = kmem_alloc(size, KM_SLEEP);
1040ae46e4c7SMatthew Ahrens 
1041ae46e4c7SMatthew Ahrens 	va_start(adx, fmt);
1042ae46e4c7SMatthew Ahrens 	size = vsnprintf(buf, size, fmt, adx);
1043ae46e4c7SMatthew Ahrens 	va_end(adx);
1044ae46e4c7SMatthew Ahrens 
1045ae46e4c7SMatthew Ahrens 	return (buf);
1046ae46e4c7SMatthew Ahrens }
1047c99e4bdcSChris Kirby 
1048c99e4bdcSChris Kirby /* ARGSUSED */
1049c99e4bdcSChris Kirby int
1050a7f53a56SChris Kirby zfs_onexit_fd_hold(int fd, minor_t *minorp)
1051a7f53a56SChris Kirby {
1052a7f53a56SChris Kirby 	*minorp = 0;
1053a7f53a56SChris Kirby 	return (0);
1054a7f53a56SChris Kirby }
1055a7f53a56SChris Kirby 
1056a7f53a56SChris Kirby /* ARGSUSED */
1057a7f53a56SChris Kirby void
1058a7f53a56SChris Kirby zfs_onexit_fd_rele(int fd)
1059a7f53a56SChris Kirby {
1060a7f53a56SChris Kirby }
1061a7f53a56SChris Kirby 
1062a7f53a56SChris Kirby /* ARGSUSED */
1063a7f53a56SChris Kirby int
1064a7f53a56SChris Kirby zfs_onexit_add_cb(minor_t minor, void (*func)(void *), void *data,
1065c99e4bdcSChris Kirby     uint64_t *action_handle)
1066c99e4bdcSChris Kirby {
1067c99e4bdcSChris Kirby 	return (0);
1068c99e4bdcSChris Kirby }
1069c99e4bdcSChris Kirby 
1070c99e4bdcSChris Kirby /* ARGSUSED */
1071c99e4bdcSChris Kirby int
1072a7f53a56SChris Kirby zfs_onexit_del_cb(minor_t minor, uint64_t action_handle, boolean_t fire)
1073c99e4bdcSChris Kirby {
1074c99e4bdcSChris Kirby 	return (0);
1075c99e4bdcSChris Kirby }
1076c99e4bdcSChris Kirby 
1077c99e4bdcSChris Kirby /* ARGSUSED */
1078c99e4bdcSChris Kirby int
1079a7f53a56SChris Kirby zfs_onexit_cb_data(minor_t minor, uint64_t action_handle, void **data)
1080c99e4bdcSChris Kirby {
1081c99e4bdcSChris Kirby 	return (0);
1082c99e4bdcSChris Kirby }
108331d7e8faSGeorge Wilson 
108431d7e8faSGeorge Wilson void
108531d7e8faSGeorge Wilson bioinit(buf_t *bp)
108631d7e8faSGeorge Wilson {
108731d7e8faSGeorge Wilson 	bzero(bp, sizeof (buf_t));
108831d7e8faSGeorge Wilson }
108931d7e8faSGeorge Wilson 
109031d7e8faSGeorge Wilson void
109131d7e8faSGeorge Wilson biodone(buf_t *bp)
109231d7e8faSGeorge Wilson {
109331d7e8faSGeorge Wilson 	if (bp->b_iodone != NULL) {
109431d7e8faSGeorge Wilson 		(*(bp->b_iodone))(bp);
109531d7e8faSGeorge Wilson 		return;
109631d7e8faSGeorge Wilson 	}
109731d7e8faSGeorge Wilson 	ASSERT((bp->b_flags & B_DONE) == 0);
109831d7e8faSGeorge Wilson 	bp->b_flags |= B_DONE;
109931d7e8faSGeorge Wilson }
110031d7e8faSGeorge Wilson 
110131d7e8faSGeorge Wilson void
110231d7e8faSGeorge Wilson bioerror(buf_t *bp, int error)
110331d7e8faSGeorge Wilson {
110431d7e8faSGeorge Wilson 	ASSERT(bp != NULL);
110531d7e8faSGeorge Wilson 	ASSERT(error >= 0);
110631d7e8faSGeorge Wilson 
110731d7e8faSGeorge Wilson 	if (error != 0) {
110831d7e8faSGeorge Wilson 		bp->b_flags |= B_ERROR;
110931d7e8faSGeorge Wilson 	} else {
111031d7e8faSGeorge Wilson 		bp->b_flags &= ~B_ERROR;
111131d7e8faSGeorge Wilson 	}
111231d7e8faSGeorge Wilson 	bp->b_error = error;
111331d7e8faSGeorge Wilson }
111431d7e8faSGeorge Wilson 
111531d7e8faSGeorge Wilson 
111631d7e8faSGeorge Wilson int
111731d7e8faSGeorge Wilson geterror(struct buf *bp)
111831d7e8faSGeorge Wilson {
111931d7e8faSGeorge Wilson 	int error = 0;
112031d7e8faSGeorge Wilson 
112131d7e8faSGeorge Wilson 	if (bp->b_flags & B_ERROR) {
112231d7e8faSGeorge Wilson 		error = bp->b_error;
112331d7e8faSGeorge Wilson 		if (!error)
112431d7e8faSGeorge Wilson 			error = EIO;
112531d7e8faSGeorge Wilson 	}
112631d7e8faSGeorge Wilson 	return (error);
112731d7e8faSGeorge Wilson }
1128