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