xref: /illumos-gate/usr/src/lib/libzpool/common/kernel.c (revision 3b2aab18808792cbd248a12f1edf139b89833c13)
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.
2394dd93aeSGeorge Wilson  * Copyright (c) 2012 by Delphix. All rights reserved.
24fa9e4066Sahrens  */
25fa9e4066Sahrens 
26fa9e4066Sahrens #include <assert.h>
27c9431fa1Sahl #include <fcntl.h>
28fa9e4066Sahrens #include <poll.h>
29fa9e4066Sahrens #include <stdio.h>
30fa9e4066Sahrens #include <stdlib.h>
31c9431fa1Sahl #include <string.h>
32c9431fa1Sahl #include <zlib.h>
33fa9e4066Sahrens #include <sys/spa.h>
34c9431fa1Sahl #include <sys/stat.h>
35fa9e4066Sahrens #include <sys/processor.h>
36c9431fa1Sahl #include <sys/zfs_context.h>
37*3b2aab18SMatthew Ahrens #include <sys/rrwlock.h>
38c9431fa1Sahl #include <sys/zmod.h>
3995173954Sek #include <sys/utsname.h>
405679c89fSjv #include <sys/systeminfo.h>
415ad82045Snd 
42fa9e4066Sahrens /*
43fa9e4066Sahrens  * Emulation of kernel services in userland.
44fa9e4066Sahrens  */
45fa9e4066Sahrens 
46feef89cfSVictor Latushkin int aok;
47fa9e4066Sahrens uint64_t physmem;
48fa9e4066Sahrens vnode_t *rootdir = (vnode_t *)0xabcd1234;
495679c89fSjv char hw_serial[HW_HOSTID_LEN];
50283b8460SGeorge.Wilson kmutex_t cpu_lock;
5194dd93aeSGeorge Wilson vmem_t *zio_arena = NULL;
5295173954Sek 
5395173954Sek struct utsname utsname = {
5495173954Sek 	"userland", "libzpool", "1", "1", "na"
5595173954Sek };
56fa9e4066Sahrens 
5735a5a358SJonathan Adams /* this only exists to have its address taken */
5835a5a358SJonathan Adams struct proc p0;
5935a5a358SJonathan Adams 
60fa9e4066Sahrens /*
61fa9e4066Sahrens  * =========================================================================
62fa9e4066Sahrens  * threads
63fa9e4066Sahrens  * =========================================================================
64fa9e4066Sahrens  */
65fa9e4066Sahrens /*ARGSUSED*/
66fa9e4066Sahrens kthread_t *
67fa9e4066Sahrens zk_thread_create(void (*func)(), void *arg)
68fa9e4066Sahrens {
69fa9e4066Sahrens 	thread_t tid;
70fa9e4066Sahrens 
71fa9e4066Sahrens 	VERIFY(thr_create(0, 0, (void *(*)(void *))func, arg, THR_DETACHED,
72fa9e4066Sahrens 	    &tid) == 0);
73fa9e4066Sahrens 
74fa9e4066Sahrens 	return ((void *)(uintptr_t)tid);
75fa9e4066Sahrens }
76fa9e4066Sahrens 
7744cb6abcSbmc /*
7844cb6abcSbmc  * =========================================================================
7944cb6abcSbmc  * kstats
8044cb6abcSbmc  * =========================================================================
8144cb6abcSbmc  */
8244cb6abcSbmc /*ARGSUSED*/
8344cb6abcSbmc kstat_t *
84c3a66015SMatthew Ahrens kstat_create(const char *module, int instance, const char *name,
85c3a66015SMatthew Ahrens     const char *class, uchar_t type, ulong_t ndata, uchar_t ks_flag)
8644cb6abcSbmc {
8744cb6abcSbmc 	return (NULL);
8844cb6abcSbmc }
8944cb6abcSbmc 
9044cb6abcSbmc /*ARGSUSED*/
9144cb6abcSbmc void
9244cb6abcSbmc kstat_install(kstat_t *ksp)
9344cb6abcSbmc {}
9444cb6abcSbmc 
9544cb6abcSbmc /*ARGSUSED*/
9644cb6abcSbmc void
9744cb6abcSbmc kstat_delete(kstat_t *ksp)
9844cb6abcSbmc {}
9944cb6abcSbmc 
100c3a66015SMatthew Ahrens /*ARGSUSED*/
101c3a66015SMatthew Ahrens void
102c3a66015SMatthew Ahrens kstat_waitq_enter(kstat_io_t *kiop)
103c3a66015SMatthew Ahrens {}
104c3a66015SMatthew Ahrens 
105c3a66015SMatthew Ahrens /*ARGSUSED*/
106c3a66015SMatthew Ahrens void
107c3a66015SMatthew Ahrens kstat_waitq_exit(kstat_io_t *kiop)
108c3a66015SMatthew Ahrens {}
109c3a66015SMatthew Ahrens 
110c3a66015SMatthew Ahrens /*ARGSUSED*/
111c3a66015SMatthew Ahrens void
112c3a66015SMatthew Ahrens kstat_runq_enter(kstat_io_t *kiop)
113c3a66015SMatthew Ahrens {}
114c3a66015SMatthew Ahrens 
115c3a66015SMatthew Ahrens /*ARGSUSED*/
116c3a66015SMatthew Ahrens void
117c3a66015SMatthew Ahrens kstat_runq_exit(kstat_io_t *kiop)
118c3a66015SMatthew Ahrens {}
119c3a66015SMatthew Ahrens 
120c3a66015SMatthew Ahrens /*ARGSUSED*/
121c3a66015SMatthew Ahrens void
122c3a66015SMatthew Ahrens kstat_waitq_to_runq(kstat_io_t *kiop)
123c3a66015SMatthew Ahrens {}
124c3a66015SMatthew Ahrens 
125c3a66015SMatthew Ahrens /*ARGSUSED*/
126c3a66015SMatthew Ahrens void
127c3a66015SMatthew Ahrens kstat_runq_back_to_waitq(kstat_io_t *kiop)
128c3a66015SMatthew Ahrens {}
129c3a66015SMatthew Ahrens 
130fa9e4066Sahrens /*
131fa9e4066Sahrens  * =========================================================================
132fa9e4066Sahrens  * mutexes
133fa9e4066Sahrens  * =========================================================================
134fa9e4066Sahrens  */
135fa9e4066Sahrens void
136fa9e4066Sahrens zmutex_init(kmutex_t *mp)
137fa9e4066Sahrens {
138fa9e4066Sahrens 	mp->m_owner = NULL;
139c25056deSgw 	mp->initialized = B_TRUE;
140fa9e4066Sahrens 	(void) _mutex_init(&mp->m_lock, USYNC_THREAD, NULL);
141fa9e4066Sahrens }
142fa9e4066Sahrens 
143fa9e4066Sahrens void
144fa9e4066Sahrens zmutex_destroy(kmutex_t *mp)
145fa9e4066Sahrens {
146c25056deSgw 	ASSERT(mp->initialized == B_TRUE);
147fa9e4066Sahrens 	ASSERT(mp->m_owner == NULL);
148fa9e4066Sahrens 	(void) _mutex_destroy(&(mp)->m_lock);
149fa9e4066Sahrens 	mp->m_owner = (void *)-1UL;
150c25056deSgw 	mp->initialized = B_FALSE;
151fa9e4066Sahrens }
152fa9e4066Sahrens 
153fa9e4066Sahrens void
154fa9e4066Sahrens mutex_enter(kmutex_t *mp)
155fa9e4066Sahrens {
156c25056deSgw 	ASSERT(mp->initialized == B_TRUE);
157fa9e4066Sahrens 	ASSERT(mp->m_owner != (void *)-1UL);
158fa9e4066Sahrens 	ASSERT(mp->m_owner != curthread);
1595ad82045Snd 	VERIFY(mutex_lock(&mp->m_lock) == 0);
160fa9e4066Sahrens 	ASSERT(mp->m_owner == NULL);
161fa9e4066Sahrens 	mp->m_owner = curthread;
162fa9e4066Sahrens }
163fa9e4066Sahrens 
164fa9e4066Sahrens int
165fa9e4066Sahrens mutex_tryenter(kmutex_t *mp)
166fa9e4066Sahrens {
167c25056deSgw 	ASSERT(mp->initialized == B_TRUE);
168fa9e4066Sahrens 	ASSERT(mp->m_owner != (void *)-1UL);
169fa9e4066Sahrens 	if (0 == mutex_trylock(&mp->m_lock)) {
170fa9e4066Sahrens 		ASSERT(mp->m_owner == NULL);
171fa9e4066Sahrens 		mp->m_owner = curthread;
172fa9e4066Sahrens 		return (1);
173fa9e4066Sahrens 	} else {
174fa9e4066Sahrens 		return (0);
175fa9e4066Sahrens 	}
176fa9e4066Sahrens }
177fa9e4066Sahrens 
178fa9e4066Sahrens void
179fa9e4066Sahrens mutex_exit(kmutex_t *mp)
180fa9e4066Sahrens {
181c25056deSgw 	ASSERT(mp->initialized == B_TRUE);
182fa9e4066Sahrens 	ASSERT(mutex_owner(mp) == curthread);
183fa9e4066Sahrens 	mp->m_owner = NULL;
1845ad82045Snd 	VERIFY(mutex_unlock(&mp->m_lock) == 0);
185fa9e4066Sahrens }
186fa9e4066Sahrens 
187fa9e4066Sahrens void *
188fa9e4066Sahrens mutex_owner(kmutex_t *mp)
189fa9e4066Sahrens {
190c25056deSgw 	ASSERT(mp->initialized == B_TRUE);
191fa9e4066Sahrens 	return (mp->m_owner);
192fa9e4066Sahrens }
193fa9e4066Sahrens 
194fa9e4066Sahrens /*
195fa9e4066Sahrens  * =========================================================================
196fa9e4066Sahrens  * rwlocks
197fa9e4066Sahrens  * =========================================================================
198fa9e4066Sahrens  */
199fa9e4066Sahrens /*ARGSUSED*/
200fa9e4066Sahrens void
201fa9e4066Sahrens rw_init(krwlock_t *rwlp, char *name, int type, void *arg)
202fa9e4066Sahrens {
203fa9e4066Sahrens 	rwlock_init(&rwlp->rw_lock, USYNC_THREAD, NULL);
204fa9e4066Sahrens 	rwlp->rw_owner = NULL;
205c25056deSgw 	rwlp->initialized = B_TRUE;
206fa9e4066Sahrens }
207fa9e4066Sahrens 
208fa9e4066Sahrens void
209fa9e4066Sahrens rw_destroy(krwlock_t *rwlp)
210fa9e4066Sahrens {
211fa9e4066Sahrens 	rwlock_destroy(&rwlp->rw_lock);
212fa9e4066Sahrens 	rwlp->rw_owner = (void *)-1UL;
213c25056deSgw 	rwlp->initialized = B_FALSE;
214fa9e4066Sahrens }
215fa9e4066Sahrens 
216fa9e4066Sahrens void
217fa9e4066Sahrens rw_enter(krwlock_t *rwlp, krw_t rw)
218fa9e4066Sahrens {
219fa9e4066Sahrens 	ASSERT(!RW_LOCK_HELD(rwlp));
220c25056deSgw 	ASSERT(rwlp->initialized == B_TRUE);
221fa9e4066Sahrens 	ASSERT(rwlp->rw_owner != (void *)-1UL);
222fa9e4066Sahrens 	ASSERT(rwlp->rw_owner != curthread);
223fa9e4066Sahrens 
224fa9e4066Sahrens 	if (rw == RW_READER)
225745cd3c5Smaybee 		VERIFY(rw_rdlock(&rwlp->rw_lock) == 0);
226fa9e4066Sahrens 	else
227745cd3c5Smaybee 		VERIFY(rw_wrlock(&rwlp->rw_lock) == 0);
228fa9e4066Sahrens 
229fa9e4066Sahrens 	rwlp->rw_owner = curthread;
230fa9e4066Sahrens }
231fa9e4066Sahrens 
232fa9e4066Sahrens void
233fa9e4066Sahrens rw_exit(krwlock_t *rwlp)
234fa9e4066Sahrens {
235c25056deSgw 	ASSERT(rwlp->initialized == B_TRUE);
236fa9e4066Sahrens 	ASSERT(rwlp->rw_owner != (void *)-1UL);
237fa9e4066Sahrens 
238fa9e4066Sahrens 	rwlp->rw_owner = NULL;
239745cd3c5Smaybee 	VERIFY(rw_unlock(&rwlp->rw_lock) == 0);
240fa9e4066Sahrens }
241fa9e4066Sahrens 
242fa9e4066Sahrens int
243fa9e4066Sahrens rw_tryenter(krwlock_t *rwlp, krw_t rw)
244fa9e4066Sahrens {
245fa9e4066Sahrens 	int rv;
246fa9e4066Sahrens 
247c25056deSgw 	ASSERT(rwlp->initialized == B_TRUE);
248fa9e4066Sahrens 	ASSERT(rwlp->rw_owner != (void *)-1UL);
249fa9e4066Sahrens 
250fa9e4066Sahrens 	if (rw == RW_READER)
251fa9e4066Sahrens 		rv = rw_tryrdlock(&rwlp->rw_lock);
252fa9e4066Sahrens 	else
253fa9e4066Sahrens 		rv = rw_trywrlock(&rwlp->rw_lock);
254fa9e4066Sahrens 
255fa9e4066Sahrens 	if (rv == 0) {
256fa9e4066Sahrens 		rwlp->rw_owner = curthread;
257fa9e4066Sahrens 		return (1);
258fa9e4066Sahrens 	}
259fa9e4066Sahrens 
260fa9e4066Sahrens 	return (0);
261fa9e4066Sahrens }
262fa9e4066Sahrens 
263fa9e4066Sahrens /*ARGSUSED*/
264fa9e4066Sahrens int
265fa9e4066Sahrens rw_tryupgrade(krwlock_t *rwlp)
266fa9e4066Sahrens {
267c25056deSgw 	ASSERT(rwlp->initialized == B_TRUE);
268fa9e4066Sahrens 	ASSERT(rwlp->rw_owner != (void *)-1UL);
269fa9e4066Sahrens 
270fa9e4066Sahrens 	return (0);
271fa9e4066Sahrens }
272fa9e4066Sahrens 
273fa9e4066Sahrens /*
274fa9e4066Sahrens  * =========================================================================
275fa9e4066Sahrens  * condition variables
276fa9e4066Sahrens  * =========================================================================
277fa9e4066Sahrens  */
278fa9e4066Sahrens /*ARGSUSED*/
279fa9e4066Sahrens void
280fa9e4066Sahrens cv_init(kcondvar_t *cv, char *name, int type, void *arg)
281fa9e4066Sahrens {
2825ad82045Snd 	VERIFY(cond_init(cv, type, NULL) == 0);
283fa9e4066Sahrens }
284fa9e4066Sahrens 
285fa9e4066Sahrens void
286fa9e4066Sahrens cv_destroy(kcondvar_t *cv)
287fa9e4066Sahrens {
2885ad82045Snd 	VERIFY(cond_destroy(cv) == 0);
289fa9e4066Sahrens }
290fa9e4066Sahrens 
291fa9e4066Sahrens void
292fa9e4066Sahrens cv_wait(kcondvar_t *cv, kmutex_t *mp)
293fa9e4066Sahrens {
294fa9e4066Sahrens 	ASSERT(mutex_owner(mp) == curthread);
295fa9e4066Sahrens 	mp->m_owner = NULL;
2965ad82045Snd 	int ret = cond_wait(cv, &mp->m_lock);
2975ad82045Snd 	VERIFY(ret == 0 || ret == EINTR);
298fa9e4066Sahrens 	mp->m_owner = curthread;
299fa9e4066Sahrens }
300fa9e4066Sahrens 
301fa9e4066Sahrens clock_t
302fa9e4066Sahrens cv_timedwait(kcondvar_t *cv, kmutex_t *mp, clock_t abstime)
303fa9e4066Sahrens {
304fa9e4066Sahrens 	int error;
305fa9e4066Sahrens 	timestruc_t ts;
306fa9e4066Sahrens 	clock_t delta;
307fa9e4066Sahrens 
308fa9e4066Sahrens top:
309d3d50737SRafael Vanoni 	delta = abstime - ddi_get_lbolt();
310fa9e4066Sahrens 	if (delta <= 0)
311fa9e4066Sahrens 		return (-1);
312fa9e4066Sahrens 
313fa9e4066Sahrens 	ts.tv_sec = delta / hz;
314fa9e4066Sahrens 	ts.tv_nsec = (delta % hz) * (NANOSEC / hz);
315fa9e4066Sahrens 
316fa9e4066Sahrens 	ASSERT(mutex_owner(mp) == curthread);
317fa9e4066Sahrens 	mp->m_owner = NULL;
318fa9e4066Sahrens 	error = cond_reltimedwait(cv, &mp->m_lock, &ts);
319fa9e4066Sahrens 	mp->m_owner = curthread;
320fa9e4066Sahrens 
321fa9e4066Sahrens 	if (error == ETIME)
322fa9e4066Sahrens 		return (-1);
323fa9e4066Sahrens 
324fa9e4066Sahrens 	if (error == EINTR)
325fa9e4066Sahrens 		goto top;
326fa9e4066Sahrens 
327fa9e4066Sahrens 	ASSERT(error == 0);
328fa9e4066Sahrens 
329fa9e4066Sahrens 	return (1);
330fa9e4066Sahrens }
331fa9e4066Sahrens 
332fa9e4066Sahrens void
333fa9e4066Sahrens cv_signal(kcondvar_t *cv)
334fa9e4066Sahrens {
3355ad82045Snd 	VERIFY(cond_signal(cv) == 0);
336fa9e4066Sahrens }
337fa9e4066Sahrens 
338fa9e4066Sahrens void
339fa9e4066Sahrens cv_broadcast(kcondvar_t *cv)
340fa9e4066Sahrens {
3415ad82045Snd 	VERIFY(cond_broadcast(cv) == 0);
342fa9e4066Sahrens }
343fa9e4066Sahrens 
344fa9e4066Sahrens /*
345fa9e4066Sahrens  * =========================================================================
346fa9e4066Sahrens  * vnode operations
347fa9e4066Sahrens  * =========================================================================
348fa9e4066Sahrens  */
349fa9e4066Sahrens /*
350fa9e4066Sahrens  * Note: for the xxxat() versions of these functions, we assume that the
351fa9e4066Sahrens  * starting vp is always rootdir (which is true for spa_directory.c, the only
352fa9e4066Sahrens  * ZFS consumer of these interfaces).  We assert this is true, and then emulate
353fa9e4066Sahrens  * them by adding '/' in front of the path.
354fa9e4066Sahrens  */
355fa9e4066Sahrens 
356fa9e4066Sahrens /*ARGSUSED*/
357fa9e4066Sahrens int
358fa9e4066Sahrens vn_open(char *path, int x1, int flags, int mode, vnode_t **vpp, int x2, int x3)
359fa9e4066Sahrens {
360fa9e4066Sahrens 	int fd;
361fa9e4066Sahrens 	vnode_t *vp;
362fa9e4066Sahrens 	int old_umask;
363fa9e4066Sahrens 	char realpath[MAXPATHLEN];
364fa9e4066Sahrens 	struct stat64 st;
365fa9e4066Sahrens 
366fa9e4066Sahrens 	/*
367fa9e4066Sahrens 	 * If we're accessing a real disk from userland, we need to use
368fa9e4066Sahrens 	 * the character interface to avoid caching.  This is particularly
369fa9e4066Sahrens 	 * important if we're trying to look at a real in-kernel storage
370fa9e4066Sahrens 	 * pool from userland, e.g. via zdb, because otherwise we won't
371fa9e4066Sahrens 	 * see the changes occurring under the segmap cache.
372fa9e4066Sahrens 	 * On the other hand, the stupid character device returns zero
373fa9e4066Sahrens 	 * for its size.  So -- gag -- we open the block device to get
374fa9e4066Sahrens 	 * its size, and remember it for subsequent VOP_GETATTR().
375fa9e4066Sahrens 	 */
376fa9e4066Sahrens 	if (strncmp(path, "/dev/", 5) == 0) {
377fa9e4066Sahrens 		char *dsk;
378fa9e4066Sahrens 		fd = open64(path, O_RDONLY);
379fa9e4066Sahrens 		if (fd == -1)
380fa9e4066Sahrens 			return (errno);
381fa9e4066Sahrens 		if (fstat64(fd, &st) == -1) {
382fa9e4066Sahrens 			close(fd);
383fa9e4066Sahrens 			return (errno);
384fa9e4066Sahrens 		}
385fa9e4066Sahrens 		close(fd);
386fa9e4066Sahrens 		(void) sprintf(realpath, "%s", path);
387fa9e4066Sahrens 		dsk = strstr(path, "/dsk/");
388fa9e4066Sahrens 		if (dsk != NULL)
389fa9e4066Sahrens 			(void) sprintf(realpath + (dsk - path) + 1, "r%s",
390fa9e4066Sahrens 			    dsk + 1);
391fa9e4066Sahrens 	} else {
392fa9e4066Sahrens 		(void) sprintf(realpath, "%s", path);
393fa9e4066Sahrens 		if (!(flags & FCREAT) && stat64(realpath, &st) == -1)
394fa9e4066Sahrens 			return (errno);
395fa9e4066Sahrens 	}
396fa9e4066Sahrens 
397fa9e4066Sahrens 	if (flags & FCREAT)
398fa9e4066Sahrens 		old_umask = umask(0);
399fa9e4066Sahrens 
400fa9e4066Sahrens 	/*
401fa9e4066Sahrens 	 * The construct 'flags - FREAD' conveniently maps combinations of
402fa9e4066Sahrens 	 * FREAD and FWRITE to the corresponding O_RDONLY, O_WRONLY, and O_RDWR.
403fa9e4066Sahrens 	 */
404fa9e4066Sahrens 	fd = open64(realpath, flags - FREAD, mode);
405fa9e4066Sahrens 
406fa9e4066Sahrens 	if (flags & FCREAT)
407fa9e4066Sahrens 		(void) umask(old_umask);
408fa9e4066Sahrens 
409fa9e4066Sahrens 	if (fd == -1)
410fa9e4066Sahrens 		return (errno);
411fa9e4066Sahrens 
412fa9e4066Sahrens 	if (fstat64(fd, &st) == -1) {
413fa9e4066Sahrens 		close(fd);
414fa9e4066Sahrens 		return (errno);
415fa9e4066Sahrens 	}
416fa9e4066Sahrens 
417fa9e4066Sahrens 	(void) fcntl(fd, F_SETFD, FD_CLOEXEC);
418fa9e4066Sahrens 
419fa9e4066Sahrens 	*vpp = vp = umem_zalloc(sizeof (vnode_t), UMEM_NOFAIL);
420fa9e4066Sahrens 
421fa9e4066Sahrens 	vp->v_fd = fd;
422fa9e4066Sahrens 	vp->v_size = st.st_size;
423fa9e4066Sahrens 	vp->v_path = spa_strdup(path);
424fa9e4066Sahrens 
425fa9e4066Sahrens 	return (0);
426fa9e4066Sahrens }
427fa9e4066Sahrens 
428da6c28aaSamw /*ARGSUSED*/
429fa9e4066Sahrens int
430fa9e4066Sahrens vn_openat(char *path, int x1, int flags, int mode, vnode_t **vpp, int x2,
431da6c28aaSamw     int x3, vnode_t *startvp, int fd)
432fa9e4066Sahrens {
433fa9e4066Sahrens 	char *realpath = umem_alloc(strlen(path) + 2, UMEM_NOFAIL);
434fa9e4066Sahrens 	int ret;
435fa9e4066Sahrens 
436fa9e4066Sahrens 	ASSERT(startvp == rootdir);
437fa9e4066Sahrens 	(void) sprintf(realpath, "/%s", path);
438fa9e4066Sahrens 
439da6c28aaSamw 	/* fd ignored for now, need if want to simulate nbmand support */
440fa9e4066Sahrens 	ret = vn_open(realpath, x1, flags, mode, vpp, x2, x3);
441fa9e4066Sahrens 
442fa9e4066Sahrens 	umem_free(realpath, strlen(path) + 2);
443fa9e4066Sahrens 
444fa9e4066Sahrens 	return (ret);
445fa9e4066Sahrens }
446fa9e4066Sahrens 
447fa9e4066Sahrens /*ARGSUSED*/
448fa9e4066Sahrens int
449fa9e4066Sahrens vn_rdwr(int uio, vnode_t *vp, void *addr, ssize_t len, offset_t offset,
450fa9e4066Sahrens 	int x1, int x2, rlim64_t x3, void *x4, ssize_t *residp)
451fa9e4066Sahrens {
452fa9e4066Sahrens 	ssize_t iolen, split;
453fa9e4066Sahrens 
454fa9e4066Sahrens 	if (uio == UIO_READ) {
455fa9e4066Sahrens 		iolen = pread64(vp->v_fd, addr, len, offset);
456fa9e4066Sahrens 	} else {
457fa9e4066Sahrens 		/*
458fa9e4066Sahrens 		 * To simulate partial disk writes, we split writes into two
459fa9e4066Sahrens 		 * system calls so that the process can be killed in between.
460fa9e4066Sahrens 		 */
461ad135b5dSChristopher Siden 		int sectors = len >> SPA_MINBLOCKSHIFT;
462ad135b5dSChristopher Siden 		split = (sectors > 0 ? rand() % sectors : 0) <<
463ad135b5dSChristopher Siden 		    SPA_MINBLOCKSHIFT;
464fa9e4066Sahrens 		iolen = pwrite64(vp->v_fd, addr, split, offset);
465fa9e4066Sahrens 		iolen += pwrite64(vp->v_fd, (char *)addr + split,
466fa9e4066Sahrens 		    len - split, offset + split);
467fa9e4066Sahrens 	}
468fa9e4066Sahrens 
469fa9e4066Sahrens 	if (iolen == -1)
470fa9e4066Sahrens 		return (errno);
471fa9e4066Sahrens 	if (residp)
472fa9e4066Sahrens 		*residp = len - iolen;
473fa9e4066Sahrens 	else if (iolen != len)
474fa9e4066Sahrens 		return (EIO);
475fa9e4066Sahrens 	return (0);
476fa9e4066Sahrens }
477fa9e4066Sahrens 
478fa9e4066Sahrens void
479fa9e4066Sahrens vn_close(vnode_t *vp)
480fa9e4066Sahrens {
481fa9e4066Sahrens 	close(vp->v_fd);
482fa9e4066Sahrens 	spa_strfree(vp->v_path);
483fa9e4066Sahrens 	umem_free(vp, sizeof (vnode_t));
484fa9e4066Sahrens }
485fa9e4066Sahrens 
486095bcd66SGeorge Wilson /*
487095bcd66SGeorge Wilson  * At a minimum we need to update the size since vdev_reopen()
488095bcd66SGeorge Wilson  * will no longer call vn_openat().
489095bcd66SGeorge Wilson  */
490095bcd66SGeorge Wilson int
491095bcd66SGeorge Wilson fop_getattr(vnode_t *vp, vattr_t *vap)
492095bcd66SGeorge Wilson {
493095bcd66SGeorge Wilson 	struct stat64 st;
494095bcd66SGeorge Wilson 
495095bcd66SGeorge Wilson 	if (fstat64(vp->v_fd, &st) == -1) {
496095bcd66SGeorge Wilson 		close(vp->v_fd);
497095bcd66SGeorge Wilson 		return (errno);
498095bcd66SGeorge Wilson 	}
499095bcd66SGeorge Wilson 
500095bcd66SGeorge Wilson 	vap->va_size = st.st_size;
501095bcd66SGeorge Wilson 	return (0);
502095bcd66SGeorge Wilson }
503095bcd66SGeorge Wilson 
504fa9e4066Sahrens #ifdef ZFS_DEBUG
505fa9e4066Sahrens 
506fa9e4066Sahrens /*
507fa9e4066Sahrens  * =========================================================================
508fa9e4066Sahrens  * Figure out which debugging statements to print
509fa9e4066Sahrens  * =========================================================================
510fa9e4066Sahrens  */
511fa9e4066Sahrens 
512fa9e4066Sahrens static char *dprintf_string;
513fa9e4066Sahrens static int dprintf_print_all;
514fa9e4066Sahrens 
515fa9e4066Sahrens int
516fa9e4066Sahrens dprintf_find_string(const char *string)
517fa9e4066Sahrens {
518fa9e4066Sahrens 	char *tmp_str = dprintf_string;
519fa9e4066Sahrens 	int len = strlen(string);
520fa9e4066Sahrens 
521fa9e4066Sahrens 	/*
522fa9e4066Sahrens 	 * Find out if this is a string we want to print.
523fa9e4066Sahrens 	 * String format: file1.c,function_name1,file2.c,file3.c
524fa9e4066Sahrens 	 */
525fa9e4066Sahrens 
526fa9e4066Sahrens 	while (tmp_str != NULL) {
527fa9e4066Sahrens 		if (strncmp(tmp_str, string, len) == 0 &&
528fa9e4066Sahrens 		    (tmp_str[len] == ',' || tmp_str[len] == '\0'))
529fa9e4066Sahrens 			return (1);
530fa9e4066Sahrens 		tmp_str = strchr(tmp_str, ',');
531fa9e4066Sahrens 		if (tmp_str != NULL)
532fa9e4066Sahrens 			tmp_str++; /* Get rid of , */
533fa9e4066Sahrens 	}
534fa9e4066Sahrens 	return (0);
535fa9e4066Sahrens }
536fa9e4066Sahrens 
537fa9e4066Sahrens void
538fa9e4066Sahrens dprintf_setup(int *argc, char **argv)
539fa9e4066Sahrens {
540fa9e4066Sahrens 	int i, j;
541fa9e4066Sahrens 
542fa9e4066Sahrens 	/*
543fa9e4066Sahrens 	 * Debugging can be specified two ways: by setting the
544fa9e4066Sahrens 	 * environment variable ZFS_DEBUG, or by including a
545fa9e4066Sahrens 	 * "debug=..."  argument on the command line.  The command
546fa9e4066Sahrens 	 * line setting overrides the environment variable.
547fa9e4066Sahrens 	 */
548fa9e4066Sahrens 
549fa9e4066Sahrens 	for (i = 1; i < *argc; i++) {
550fa9e4066Sahrens 		int len = strlen("debug=");
551fa9e4066Sahrens 		/* First look for a command line argument */
552fa9e4066Sahrens 		if (strncmp("debug=", argv[i], len) == 0) {
553fa9e4066Sahrens 			dprintf_string = argv[i] + len;
554fa9e4066Sahrens 			/* Remove from args */
555fa9e4066Sahrens 			for (j = i; j < *argc; j++)
556fa9e4066Sahrens 				argv[j] = argv[j+1];
557fa9e4066Sahrens 			argv[j] = NULL;
558fa9e4066Sahrens 			(*argc)--;
559fa9e4066Sahrens 		}
560fa9e4066Sahrens 	}
561fa9e4066Sahrens 
562fa9e4066Sahrens 	if (dprintf_string == NULL) {
563fa9e4066Sahrens 		/* Look for ZFS_DEBUG environment variable */
564fa9e4066Sahrens 		dprintf_string = getenv("ZFS_DEBUG");
565fa9e4066Sahrens 	}
566fa9e4066Sahrens 
567fa9e4066Sahrens 	/*
568fa9e4066Sahrens 	 * Are we just turning on all debugging?
569fa9e4066Sahrens 	 */
570fa9e4066Sahrens 	if (dprintf_find_string("on"))
571fa9e4066Sahrens 		dprintf_print_all = 1;
572fa9e4066Sahrens }
573fa9e4066Sahrens 
574fa9e4066Sahrens /*
575fa9e4066Sahrens  * =========================================================================
576fa9e4066Sahrens  * debug printfs
577fa9e4066Sahrens  * =========================================================================
578fa9e4066Sahrens  */
579fa9e4066Sahrens void
580fa9e4066Sahrens __dprintf(const char *file, const char *func, int line, const char *fmt, ...)
581fa9e4066Sahrens {
582fa9e4066Sahrens 	const char *newfile;
583fa9e4066Sahrens 	va_list adx;
584fa9e4066Sahrens 
585fa9e4066Sahrens 	/*
586fa9e4066Sahrens 	 * Get rid of annoying "../common/" prefix to filename.
587fa9e4066Sahrens 	 */
588fa9e4066Sahrens 	newfile = strrchr(file, '/');
589fa9e4066Sahrens 	if (newfile != NULL) {
590fa9e4066Sahrens 		newfile = newfile + 1; /* Get rid of leading / */
591fa9e4066Sahrens 	} else {
592fa9e4066Sahrens 		newfile = file;
593fa9e4066Sahrens 	}
594fa9e4066Sahrens 
595fa9e4066Sahrens 	if (dprintf_print_all ||
596fa9e4066Sahrens 	    dprintf_find_string(newfile) ||
597fa9e4066Sahrens 	    dprintf_find_string(func)) {
598fa9e4066Sahrens 		/* Print out just the function name if requested */
599fa9e4066Sahrens 		flockfile(stdout);
600fa9e4066Sahrens 		if (dprintf_find_string("pid"))
601fa9e4066Sahrens 			(void) printf("%d ", getpid());
602fa9e4066Sahrens 		if (dprintf_find_string("tid"))
603fa9e4066Sahrens 			(void) printf("%u ", thr_self());
604fa9e4066Sahrens 		if (dprintf_find_string("cpu"))
605fa9e4066Sahrens 			(void) printf("%u ", getcpuid());
606fa9e4066Sahrens 		if (dprintf_find_string("time"))
607fa9e4066Sahrens 			(void) printf("%llu ", gethrtime());
608fa9e4066Sahrens 		if (dprintf_find_string("long"))
609fa9e4066Sahrens 			(void) printf("%s, line %d: ", newfile, line);
610fa9e4066Sahrens 		(void) printf("%s: ", func);
611fa9e4066Sahrens 		va_start(adx, fmt);
612fa9e4066Sahrens 		(void) vprintf(fmt, adx);
613fa9e4066Sahrens 		va_end(adx);
614fa9e4066Sahrens 		funlockfile(stdout);
615fa9e4066Sahrens 	}
616fa9e4066Sahrens }
617fa9e4066Sahrens 
618fa9e4066Sahrens #endif /* ZFS_DEBUG */
619fa9e4066Sahrens 
620fa9e4066Sahrens /*
621fa9e4066Sahrens  * =========================================================================
622fa9e4066Sahrens  * cmn_err() and panic()
623fa9e4066Sahrens  * =========================================================================
624fa9e4066Sahrens  */
625fa9e4066Sahrens static char ce_prefix[CE_IGNORE][10] = { "", "NOTICE: ", "WARNING: ", "" };
626fa9e4066Sahrens static char ce_suffix[CE_IGNORE][2] = { "", "\n", "\n", "" };
627fa9e4066Sahrens 
628fa9e4066Sahrens void
629fa9e4066Sahrens vpanic(const char *fmt, va_list adx)
630fa9e4066Sahrens {
631fa9e4066Sahrens 	(void) fprintf(stderr, "error: ");
632fa9e4066Sahrens 	(void) vfprintf(stderr, fmt, adx);
633fa9e4066Sahrens 	(void) fprintf(stderr, "\n");
634fa9e4066Sahrens 
635fa9e4066Sahrens 	abort();	/* think of it as a "user-level crash dump" */
636fa9e4066Sahrens }
637fa9e4066Sahrens 
638fa9e4066Sahrens void
639fa9e4066Sahrens panic(const char *fmt, ...)
640fa9e4066Sahrens {
641fa9e4066Sahrens 	va_list adx;
642fa9e4066Sahrens 
643fa9e4066Sahrens 	va_start(adx, fmt);
644fa9e4066Sahrens 	vpanic(fmt, adx);
645fa9e4066Sahrens 	va_end(adx);
646fa9e4066Sahrens }
647fa9e4066Sahrens 
648fa9e4066Sahrens void
6490125049cSahrens vcmn_err(int ce, const char *fmt, va_list adx)
650fa9e4066Sahrens {
651fa9e4066Sahrens 	if (ce == CE_PANIC)
652fa9e4066Sahrens 		vpanic(fmt, adx);
653fa9e4066Sahrens 	if (ce != CE_NOTE) {	/* suppress noise in userland stress testing */
654fa9e4066Sahrens 		(void) fprintf(stderr, "%s", ce_prefix[ce]);
655fa9e4066Sahrens 		(void) vfprintf(stderr, fmt, adx);
656fa9e4066Sahrens 		(void) fprintf(stderr, "%s", ce_suffix[ce]);
657fa9e4066Sahrens 	}
6580125049cSahrens }
6590125049cSahrens 
6600125049cSahrens /*PRINTFLIKE2*/
6610125049cSahrens void
6620125049cSahrens cmn_err(int ce, const char *fmt, ...)
6630125049cSahrens {
6640125049cSahrens 	va_list adx;
6650125049cSahrens 
6660125049cSahrens 	va_start(adx, fmt);
6670125049cSahrens 	vcmn_err(ce, fmt, adx);
668fa9e4066Sahrens 	va_end(adx);
669fa9e4066Sahrens }
670fa9e4066Sahrens 
671ea8dc4b6Seschrock /*
672ea8dc4b6Seschrock  * =========================================================================
673ea8dc4b6Seschrock  * kobj interfaces
674ea8dc4b6Seschrock  * =========================================================================
675ea8dc4b6Seschrock  */
676ea8dc4b6Seschrock struct _buf *
677ea8dc4b6Seschrock kobj_open_file(char *name)
678ea8dc4b6Seschrock {
679ea8dc4b6Seschrock 	struct _buf *file;
680ea8dc4b6Seschrock 	vnode_t *vp;
681ea8dc4b6Seschrock 
682ea8dc4b6Seschrock 	/* set vp as the _fd field of the file */
683da6c28aaSamw 	if (vn_openat(name, UIO_SYSSPACE, FREAD, 0, &vp, 0, 0, rootdir,
684da6c28aaSamw 	    -1) != 0)
685ea8dc4b6Seschrock 		return ((void *)-1UL);
686ea8dc4b6Seschrock 
687ea8dc4b6Seschrock 	file = umem_zalloc(sizeof (struct _buf), UMEM_NOFAIL);
688ea8dc4b6Seschrock 	file->_fd = (intptr_t)vp;
689ea8dc4b6Seschrock 	return (file);
690ea8dc4b6Seschrock }
691ea8dc4b6Seschrock 
692ea8dc4b6Seschrock int
693ea8dc4b6Seschrock kobj_read_file(struct _buf *file, char *buf, unsigned size, unsigned off)
694ea8dc4b6Seschrock {
695ea8dc4b6Seschrock 	ssize_t resid;
696ea8dc4b6Seschrock 
697ea8dc4b6Seschrock 	vn_rdwr(UIO_READ, (vnode_t *)file->_fd, buf, size, (offset_t)off,
698ea8dc4b6Seschrock 	    UIO_SYSSPACE, 0, 0, 0, &resid);
699ea8dc4b6Seschrock 
700b1b8ab34Slling 	return (size - resid);
701ea8dc4b6Seschrock }
702ea8dc4b6Seschrock 
703ea8dc4b6Seschrock void
704ea8dc4b6Seschrock kobj_close_file(struct _buf *file)
705ea8dc4b6Seschrock {
706ea8dc4b6Seschrock 	vn_close((vnode_t *)file->_fd);
707ea8dc4b6Seschrock 	umem_free(file, sizeof (struct _buf));
708ea8dc4b6Seschrock }
709ea8dc4b6Seschrock 
710ea8dc4b6Seschrock int
711b1b8ab34Slling kobj_get_filesize(struct _buf *file, uint64_t *size)
712ea8dc4b6Seschrock {
713ea8dc4b6Seschrock 	struct stat64 st;
714b1b8ab34Slling 	vnode_t *vp = (vnode_t *)file->_fd;
715b1b8ab34Slling 
716ea8dc4b6Seschrock 	if (fstat64(vp->v_fd, &st) == -1) {
717ea8dc4b6Seschrock 		vn_close(vp);
718ea8dc4b6Seschrock 		return (errno);
719ea8dc4b6Seschrock 	}
720b1b8ab34Slling 	*size = st.st_size;
721ea8dc4b6Seschrock 	return (0);
722ea8dc4b6Seschrock }
723ea8dc4b6Seschrock 
724fa9e4066Sahrens /*
725fa9e4066Sahrens  * =========================================================================
726fa9e4066Sahrens  * misc routines
727fa9e4066Sahrens  * =========================================================================
728fa9e4066Sahrens  */
729fa9e4066Sahrens 
730fa9e4066Sahrens void
731fa9e4066Sahrens delay(clock_t ticks)
732fa9e4066Sahrens {
733fa9e4066Sahrens 	poll(0, 0, ticks * (1000 / hz));
734fa9e4066Sahrens }
735fa9e4066Sahrens 
736fa9e4066Sahrens /*
737fa9e4066Sahrens  * Find highest one bit set.
738fa9e4066Sahrens  *	Returns bit number + 1 of highest bit that is set, otherwise returns 0.
739fa9e4066Sahrens  * High order bit is 31 (or 63 in _LP64 kernel).
740fa9e4066Sahrens  */
741fa9e4066Sahrens int
742fa9e4066Sahrens highbit(ulong_t i)
743fa9e4066Sahrens {
744fa9e4066Sahrens 	register int h = 1;
745fa9e4066Sahrens 
746fa9e4066Sahrens 	if (i == 0)
747fa9e4066Sahrens 		return (0);
748fa9e4066Sahrens #ifdef _LP64
749fa9e4066Sahrens 	if (i & 0xffffffff00000000ul) {
750fa9e4066Sahrens 		h += 32; i >>= 32;
751fa9e4066Sahrens 	}
752fa9e4066Sahrens #endif
753fa9e4066Sahrens 	if (i & 0xffff0000) {
754fa9e4066Sahrens 		h += 16; i >>= 16;
755fa9e4066Sahrens 	}
756fa9e4066Sahrens 	if (i & 0xff00) {
757fa9e4066Sahrens 		h += 8; i >>= 8;
758fa9e4066Sahrens 	}
759fa9e4066Sahrens 	if (i & 0xf0) {
760fa9e4066Sahrens 		h += 4; i >>= 4;
761fa9e4066Sahrens 	}
762fa9e4066Sahrens 	if (i & 0xc) {
763fa9e4066Sahrens 		h += 2; i >>= 2;
764fa9e4066Sahrens 	}
765fa9e4066Sahrens 	if (i & 0x2) {
766fa9e4066Sahrens 		h += 1;
767fa9e4066Sahrens 	}
768fa9e4066Sahrens 	return (h);
769fa9e4066Sahrens }
770fa9e4066Sahrens 
77117f17c2dSbonwick static int random_fd = -1, urandom_fd = -1;
77217f17c2dSbonwick 
773fa9e4066Sahrens static int
77417f17c2dSbonwick random_get_bytes_common(uint8_t *ptr, size_t len, int fd)
775fa9e4066Sahrens {
776fa9e4066Sahrens 	size_t resid = len;
777fa9e4066Sahrens 	ssize_t bytes;
778fa9e4066Sahrens 
779fa9e4066Sahrens 	ASSERT(fd != -1);
780fa9e4066Sahrens 
781fa9e4066Sahrens 	while (resid != 0) {
782fa9e4066Sahrens 		bytes = read(fd, ptr, resid);
78317f17c2dSbonwick 		ASSERT3S(bytes, >=, 0);
784fa9e4066Sahrens 		ptr += bytes;
785fa9e4066Sahrens 		resid -= bytes;
786fa9e4066Sahrens 	}
787fa9e4066Sahrens 
788fa9e4066Sahrens 	return (0);
789fa9e4066Sahrens }
790fa9e4066Sahrens 
791fa9e4066Sahrens int
792fa9e4066Sahrens random_get_bytes(uint8_t *ptr, size_t len)
793fa9e4066Sahrens {
79417f17c2dSbonwick 	return (random_get_bytes_common(ptr, len, random_fd));
795fa9e4066Sahrens }
796fa9e4066Sahrens 
797fa9e4066Sahrens int
798fa9e4066Sahrens random_get_pseudo_bytes(uint8_t *ptr, size_t len)
799fa9e4066Sahrens {
80017f17c2dSbonwick 	return (random_get_bytes_common(ptr, len, urandom_fd));
801fa9e4066Sahrens }
802fa9e4066Sahrens 
80395173954Sek int
80495173954Sek ddi_strtoul(const char *hw_serial, char **nptr, int base, unsigned long *result)
80595173954Sek {
80695173954Sek 	char *end;
80795173954Sek 
80895173954Sek 	*result = strtoul(hw_serial, &end, base);
80995173954Sek 	if (*result == 0)
81095173954Sek 		return (errno);
81195173954Sek 	return (0);
81295173954Sek }
81395173954Sek 
8140a586ceaSMark Shellenbaum int
8150a586ceaSMark Shellenbaum ddi_strtoull(const char *str, char **nptr, int base, u_longlong_t *result)
8160a586ceaSMark Shellenbaum {
8170a586ceaSMark Shellenbaum 	char *end;
8180a586ceaSMark Shellenbaum 
8190a586ceaSMark Shellenbaum 	*result = strtoull(str, &end, base);
8200a586ceaSMark Shellenbaum 	if (*result == 0)
8210a586ceaSMark Shellenbaum 		return (errno);
8220a586ceaSMark Shellenbaum 	return (0);
8230a586ceaSMark Shellenbaum }
8240a586ceaSMark Shellenbaum 
825283b8460SGeorge.Wilson /* ARGSUSED */
826283b8460SGeorge.Wilson cyclic_id_t
827283b8460SGeorge.Wilson cyclic_add(cyc_handler_t *hdlr, cyc_time_t *when)
828283b8460SGeorge.Wilson {
829283b8460SGeorge.Wilson 	return (1);
830283b8460SGeorge.Wilson }
831283b8460SGeorge.Wilson 
832283b8460SGeorge.Wilson /* ARGSUSED */
833283b8460SGeorge.Wilson void
834283b8460SGeorge.Wilson cyclic_remove(cyclic_id_t id)
835283b8460SGeorge.Wilson {
836283b8460SGeorge.Wilson }
837283b8460SGeorge.Wilson 
838283b8460SGeorge.Wilson /* ARGSUSED */
839283b8460SGeorge.Wilson int
840283b8460SGeorge.Wilson cyclic_reprogram(cyclic_id_t id, hrtime_t expiration)
841283b8460SGeorge.Wilson {
842283b8460SGeorge.Wilson 	return (1);
843283b8460SGeorge.Wilson }
844283b8460SGeorge.Wilson 
845fa9e4066Sahrens /*
846fa9e4066Sahrens  * =========================================================================
847fa9e4066Sahrens  * kernel emulation setup & teardown
848fa9e4066Sahrens  * =========================================================================
849fa9e4066Sahrens  */
850fa9e4066Sahrens static int
851fa9e4066Sahrens umem_out_of_memory(void)
852fa9e4066Sahrens {
853fa9e4066Sahrens 	char errmsg[] = "out of memory -- generating core dump\n";
854fa9e4066Sahrens 
855fa9e4066Sahrens 	write(fileno(stderr), errmsg, sizeof (errmsg));
856fa9e4066Sahrens 	abort();
857fa9e4066Sahrens 	return (0);
858fa9e4066Sahrens }
859fa9e4066Sahrens 
860fa9e4066Sahrens void
861fa9e4066Sahrens kernel_init(int mode)
862fa9e4066Sahrens {
863*3b2aab18SMatthew Ahrens 	extern uint_t rrw_tsd_key;
864*3b2aab18SMatthew Ahrens 
865fa9e4066Sahrens 	umem_nofail_callback(umem_out_of_memory);
866fa9e4066Sahrens 
867fa9e4066Sahrens 	physmem = sysconf(_SC_PHYS_PAGES);
868fa9e4066Sahrens 
869fa9e4066Sahrens 	dprintf("physmem = %llu pages (%.2f GB)\n", physmem,
870fa9e4066Sahrens 	    (double)physmem * sysconf(_SC_PAGE_SIZE) / (1ULL << 30));
871fa9e4066Sahrens 
8723ad6c7f9SVictor Latushkin 	(void) snprintf(hw_serial, sizeof (hw_serial), "%ld",
8733ad6c7f9SVictor Latushkin 	    (mode & FWRITE) ? gethostid() : 0);
87495173954Sek 
87517f17c2dSbonwick 	VERIFY((random_fd = open("/dev/random", O_RDONLY)) != -1);
87617f17c2dSbonwick 	VERIFY((urandom_fd = open("/dev/urandom", O_RDONLY)) != -1);
87717f17c2dSbonwick 
87888b7b0f2SMatthew Ahrens 	system_taskq_init();
87988b7b0f2SMatthew Ahrens 
880283b8460SGeorge.Wilson 	mutex_init(&cpu_lock, NULL, MUTEX_DEFAULT, NULL);
881283b8460SGeorge.Wilson 
882fa9e4066Sahrens 	spa_init(mode);
883*3b2aab18SMatthew Ahrens 
884*3b2aab18SMatthew Ahrens 	tsd_create(&rrw_tsd_key, rrw_tsd_destroy);
885fa9e4066Sahrens }
886fa9e4066Sahrens 
887fa9e4066Sahrens void
888fa9e4066Sahrens kernel_fini(void)
889fa9e4066Sahrens {
890fa9e4066Sahrens 	spa_fini();
89117f17c2dSbonwick 
892d20e665cSRicardo M. Correia 	system_taskq_fini();
893d20e665cSRicardo M. Correia 
89417f17c2dSbonwick 	close(random_fd);
89517f17c2dSbonwick 	close(urandom_fd);
89617f17c2dSbonwick 
89717f17c2dSbonwick 	random_fd = -1;
89817f17c2dSbonwick 	urandom_fd = -1;
899fa9e4066Sahrens }
900c9431fa1Sahl 
901c9431fa1Sahl int
902c9431fa1Sahl z_uncompress(void *dst, size_t *dstlen, const void *src, size_t srclen)
903c9431fa1Sahl {
904c9431fa1Sahl 	int ret;
905c9431fa1Sahl 	uLongf len = *dstlen;
906c9431fa1Sahl 
907c9431fa1Sahl 	if ((ret = uncompress(dst, &len, src, srclen)) == Z_OK)
908c9431fa1Sahl 		*dstlen = (size_t)len;
909c9431fa1Sahl 
910c9431fa1Sahl 	return (ret);
911c9431fa1Sahl }
912c9431fa1Sahl 
913c9431fa1Sahl int
914c9431fa1Sahl z_compress_level(void *dst, size_t *dstlen, const void *src, size_t srclen,
915c9431fa1Sahl     int level)
916c9431fa1Sahl {
917c9431fa1Sahl 	int ret;
918c9431fa1Sahl 	uLongf len = *dstlen;
919c9431fa1Sahl 
920c9431fa1Sahl 	if ((ret = compress2(dst, &len, src, srclen, level)) == Z_OK)
921c9431fa1Sahl 		*dstlen = (size_t)len;
922c9431fa1Sahl 
923c9431fa1Sahl 	return (ret);
924c9431fa1Sahl }
925ecd6cf80Smarks 
926ecd6cf80Smarks uid_t
927ecd6cf80Smarks crgetuid(cred_t *cr)
928ecd6cf80Smarks {
929ecd6cf80Smarks 	return (0);
930ecd6cf80Smarks }
931ecd6cf80Smarks 
9324445fffbSMatthew Ahrens uid_t
9334445fffbSMatthew Ahrens crgetruid(cred_t *cr)
9344445fffbSMatthew Ahrens {
9354445fffbSMatthew Ahrens 	return (0);
9364445fffbSMatthew Ahrens }
9374445fffbSMatthew Ahrens 
938ecd6cf80Smarks gid_t
939ecd6cf80Smarks crgetgid(cred_t *cr)
940ecd6cf80Smarks {
941ecd6cf80Smarks 	return (0);
942ecd6cf80Smarks }
943ecd6cf80Smarks 
944ecd6cf80Smarks int
945ecd6cf80Smarks crgetngroups(cred_t *cr)
946ecd6cf80Smarks {
947ecd6cf80Smarks 	return (0);
948ecd6cf80Smarks }
949ecd6cf80Smarks 
950ecd6cf80Smarks gid_t *
951ecd6cf80Smarks crgetgroups(cred_t *cr)
952ecd6cf80Smarks {
953ecd6cf80Smarks 	return (NULL);
954ecd6cf80Smarks }
955ecd6cf80Smarks 
956ecd6cf80Smarks int
957ecd6cf80Smarks zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
958ecd6cf80Smarks {
959ecd6cf80Smarks 	return (0);
960ecd6cf80Smarks }
961ecd6cf80Smarks 
962ecd6cf80Smarks int
963ecd6cf80Smarks zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
964ecd6cf80Smarks {
965ecd6cf80Smarks 	return (0);
966ecd6cf80Smarks }
967ecd6cf80Smarks 
968ecd6cf80Smarks int
969ecd6cf80Smarks zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
970ecd6cf80Smarks {
971ecd6cf80Smarks 	return (0);
972ecd6cf80Smarks }
973e0d35c44Smarks 
974e0d35c44Smarks ksiddomain_t *
975e0d35c44Smarks ksid_lookupdomain(const char *dom)
976e0d35c44Smarks {
977e0d35c44Smarks 	ksiddomain_t *kd;
978e0d35c44Smarks 
979e0d35c44Smarks 	kd = umem_zalloc(sizeof (ksiddomain_t), UMEM_NOFAIL);
980e0d35c44Smarks 	kd->kd_name = spa_strdup(dom);
981e0d35c44Smarks 	return (kd);
982e0d35c44Smarks }
983e0d35c44Smarks 
984e0d35c44Smarks void
985e0d35c44Smarks ksiddomain_rele(ksiddomain_t *ksid)
986e0d35c44Smarks {
987e0d35c44Smarks 	spa_strfree(ksid->kd_name);
988e0d35c44Smarks 	umem_free(ksid, sizeof (ksiddomain_t));
989e0d35c44Smarks }
990ae46e4c7SMatthew Ahrens 
991ae46e4c7SMatthew Ahrens /*
992ae46e4c7SMatthew Ahrens  * Do not change the length of the returned string; it must be freed
993ae46e4c7SMatthew Ahrens  * with strfree().
994ae46e4c7SMatthew Ahrens  */
995ae46e4c7SMatthew Ahrens char *
996ae46e4c7SMatthew Ahrens kmem_asprintf(const char *fmt, ...)
997ae46e4c7SMatthew Ahrens {
998ae46e4c7SMatthew Ahrens 	int size;
999ae46e4c7SMatthew Ahrens 	va_list adx;
1000ae46e4c7SMatthew Ahrens 	char *buf;
1001ae46e4c7SMatthew Ahrens 
1002ae46e4c7SMatthew Ahrens 	va_start(adx, fmt);
1003ae46e4c7SMatthew Ahrens 	size = vsnprintf(NULL, 0, fmt, adx) + 1;
1004ae46e4c7SMatthew Ahrens 	va_end(adx);
1005ae46e4c7SMatthew Ahrens 
1006ae46e4c7SMatthew Ahrens 	buf = kmem_alloc(size, KM_SLEEP);
1007ae46e4c7SMatthew Ahrens 
1008ae46e4c7SMatthew Ahrens 	va_start(adx, fmt);
1009ae46e4c7SMatthew Ahrens 	size = vsnprintf(buf, size, fmt, adx);
1010ae46e4c7SMatthew Ahrens 	va_end(adx);
1011ae46e4c7SMatthew Ahrens 
1012ae46e4c7SMatthew Ahrens 	return (buf);
1013ae46e4c7SMatthew Ahrens }
1014c99e4bdcSChris Kirby 
1015c99e4bdcSChris Kirby /* ARGSUSED */
1016c99e4bdcSChris Kirby int
1017a7f53a56SChris Kirby zfs_onexit_fd_hold(int fd, minor_t *minorp)
1018a7f53a56SChris Kirby {
1019a7f53a56SChris Kirby 	*minorp = 0;
1020a7f53a56SChris Kirby 	return (0);
1021a7f53a56SChris Kirby }
1022a7f53a56SChris Kirby 
1023a7f53a56SChris Kirby /* ARGSUSED */
1024a7f53a56SChris Kirby void
1025a7f53a56SChris Kirby zfs_onexit_fd_rele(int fd)
1026a7f53a56SChris Kirby {
1027a7f53a56SChris Kirby }
1028a7f53a56SChris Kirby 
1029a7f53a56SChris Kirby /* ARGSUSED */
1030a7f53a56SChris Kirby int
1031a7f53a56SChris Kirby zfs_onexit_add_cb(minor_t minor, void (*func)(void *), void *data,
1032c99e4bdcSChris Kirby     uint64_t *action_handle)
1033c99e4bdcSChris Kirby {
1034c99e4bdcSChris Kirby 	return (0);
1035c99e4bdcSChris Kirby }
1036c99e4bdcSChris Kirby 
1037c99e4bdcSChris Kirby /* ARGSUSED */
1038c99e4bdcSChris Kirby int
1039a7f53a56SChris Kirby zfs_onexit_del_cb(minor_t minor, uint64_t action_handle, boolean_t fire)
1040c99e4bdcSChris Kirby {
1041c99e4bdcSChris Kirby 	return (0);
1042c99e4bdcSChris Kirby }
1043c99e4bdcSChris Kirby 
1044c99e4bdcSChris Kirby /* ARGSUSED */
1045c99e4bdcSChris Kirby int
1046a7f53a56SChris Kirby zfs_onexit_cb_data(minor_t minor, uint64_t action_handle, void **data)
1047c99e4bdcSChris Kirby {
1048c99e4bdcSChris Kirby 	return (0);
1049c99e4bdcSChris Kirby }
105031d7e8faSGeorge Wilson 
105131d7e8faSGeorge Wilson void
105231d7e8faSGeorge Wilson bioinit(buf_t *bp)
105331d7e8faSGeorge Wilson {
105431d7e8faSGeorge Wilson 	bzero(bp, sizeof (buf_t));
105531d7e8faSGeorge Wilson }
105631d7e8faSGeorge Wilson 
105731d7e8faSGeorge Wilson void
105831d7e8faSGeorge Wilson biodone(buf_t *bp)
105931d7e8faSGeorge Wilson {
106031d7e8faSGeorge Wilson 	if (bp->b_iodone != NULL) {
106131d7e8faSGeorge Wilson 		(*(bp->b_iodone))(bp);
106231d7e8faSGeorge Wilson 		return;
106331d7e8faSGeorge Wilson 	}
106431d7e8faSGeorge Wilson 	ASSERT((bp->b_flags & B_DONE) == 0);
106531d7e8faSGeorge Wilson 	bp->b_flags |= B_DONE;
106631d7e8faSGeorge Wilson }
106731d7e8faSGeorge Wilson 
106831d7e8faSGeorge Wilson void
106931d7e8faSGeorge Wilson bioerror(buf_t *bp, int error)
107031d7e8faSGeorge Wilson {
107131d7e8faSGeorge Wilson 	ASSERT(bp != NULL);
107231d7e8faSGeorge Wilson 	ASSERT(error >= 0);
107331d7e8faSGeorge Wilson 
107431d7e8faSGeorge Wilson 	if (error != 0) {
107531d7e8faSGeorge Wilson 		bp->b_flags |= B_ERROR;
107631d7e8faSGeorge Wilson 	} else {
107731d7e8faSGeorge Wilson 		bp->b_flags &= ~B_ERROR;
107831d7e8faSGeorge Wilson 	}
107931d7e8faSGeorge Wilson 	bp->b_error = error;
108031d7e8faSGeorge Wilson }
108131d7e8faSGeorge Wilson 
108231d7e8faSGeorge Wilson 
108331d7e8faSGeorge Wilson int
108431d7e8faSGeorge Wilson geterror(struct buf *bp)
108531d7e8faSGeorge Wilson {
108631d7e8faSGeorge Wilson 	int error = 0;
108731d7e8faSGeorge Wilson 
108831d7e8faSGeorge Wilson 	if (bp->b_flags & B_ERROR) {
108931d7e8faSGeorge Wilson 		error = bp->b_error;
109031d7e8faSGeorge Wilson 		if (!error)
109131d7e8faSGeorge Wilson 			error = EIO;
109231d7e8faSGeorge Wilson 	}
109331d7e8faSGeorge Wilson 	return (error);
109431d7e8faSGeorge Wilson }
1095