4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
32 #include <sys/signal.h>
35 #include <sys/processor.h>
36 #include <sys/zfs_context.h>
37 #include <sys/utsname.h>
39 #include <sys/systeminfo.h>
42 * Emulation of kernel services in userland.
47 vnode_t
*rootdir
= (vnode_t
*)0xabcd1234;
48 char hw_serial
[HW_HOSTID_LEN
];
50 struct utsname utsname
= {
51 "userland", "libzpool", "1", "1", "na"
54 /* this only exists to have its address taken */
58 * =========================================================================
60 * =========================================================================
63 pthread_cond_t kthread_cond
= PTHREAD_COND_INITIALIZER
;
64 pthread_mutex_t kthread_lock
= PTHREAD_MUTEX_INITIALIZER
;
65 pthread_key_t kthread_key
;
73 VERIFY3S(pthread_key_create(&kthread_key
, NULL
), ==, 0);
75 /* Create entry for primary kthread */
76 kt
= umem_zalloc(sizeof(kthread_t
), UMEM_NOFAIL
);
77 kt
->t_tid
= pthread_self();
80 VERIFY3S(pthread_setspecific(kthread_key
, kt
), ==, 0);
82 /* Only the main thread should be running at the moment */
83 ASSERT3S(kthread_nr
, ==, 0);
90 kthread_t
*kt
= curthread
;
92 ASSERT(pthread_equal(kt
->t_tid
, pthread_self()));
93 ASSERT3P(kt
->t_func
, ==, NULL
);
95 umem_free(kt
, sizeof(kthread_t
));
97 /* Wait for all threads to exit via thread_exit() */
98 VERIFY3S(pthread_mutex_lock(&kthread_lock
), ==, 0);
100 kthread_nr
--; /* Main thread is exiting */
102 while (kthread_nr
> 0)
103 VERIFY3S(pthread_cond_wait(&kthread_cond
, &kthread_lock
), ==,
106 ASSERT3S(kthread_nr
, ==, 0);
107 VERIFY3S(pthread_mutex_unlock(&kthread_lock
), ==, 0);
109 VERIFY3S(pthread_key_delete(kthread_key
), ==, 0);
113 zk_thread_current(void)
115 kthread_t
*kt
= pthread_getspecific(kthread_key
);
117 ASSERT3P(kt
, !=, NULL
);
123 zk_thread_helper(void *arg
)
125 kthread_t
*kt
= (kthread_t
*) arg
;
127 VERIFY3S(pthread_setspecific(kthread_key
, kt
), ==, 0);
129 VERIFY3S(pthread_mutex_lock(&kthread_lock
), ==, 0);
131 VERIFY3S(pthread_mutex_unlock(&kthread_lock
), ==, 0);
133 kt
->t_tid
= pthread_self();
134 ((thread_func_arg_t
) kt
->t_func
)(kt
->t_arg
);
136 /* Unreachable, thread must exit with thread_exit() */
143 zk_thread_create(caddr_t stk
, size_t stksize
, thread_func_t func
, void *arg
,
144 size_t len
, proc_t
*pp
, int state
, pri_t pri
, int detachstate
)
150 ASSERT3S(state
& ~TS_RUN
, ==, 0);
152 kt
= umem_zalloc(sizeof(kthread_t
), UMEM_NOFAIL
);
157 * The Solaris kernel stack size is 24k for x86/x86_64.
158 * The Linux kernel stack size is 8k for x86/x86_64.
160 * We reduce the default stack size in userspace, to ensure
161 * we observe stack overruns in user space as well as in
162 * kernel space. In practice we can't set the userspace stack
163 * size to 8k because differences in stack usage between kernel
164 * space and userspace could lead to spurious stack overflows
165 * (especially when debugging is enabled). Nevertheless, we try
166 * to set it to the lowest value that works (currently 8k*4).
167 * PTHREAD_STACK_MIN is the minimum stack required for a NULL
168 * procedure in user space and is added in to the stack
171 * Some buggy NPTL threading implementations include the
172 * guard area within the stack size allocations. In
173 * this case we allocate an extra page to account for the
174 * guard area since we only have two pages of usable stack
178 stack
= PTHREAD_STACK_MIN
+ MAX(stksize
, STACK_SIZE
) * 4;
180 VERIFY3S(pthread_attr_init(&attr
), ==, 0);
181 VERIFY3S(pthread_attr_setstacksize(&attr
, stack
), ==, 0);
182 VERIFY3S(pthread_attr_setguardsize(&attr
, PAGESIZE
), ==, 0);
183 VERIFY3S(pthread_attr_setdetachstate(&attr
, detachstate
), ==, 0);
185 VERIFY3S(pthread_create(&kt
->t_tid
, &attr
, &zk_thread_helper
, kt
),
188 VERIFY3S(pthread_attr_destroy(&attr
), ==, 0);
196 kthread_t
*kt
= curthread
;
198 ASSERT(pthread_equal(kt
->t_tid
, pthread_self()));
200 umem_free(kt
, sizeof(kthread_t
));
202 pthread_mutex_lock(&kthread_lock
);
204 pthread_mutex_unlock(&kthread_lock
);
206 pthread_cond_broadcast(&kthread_cond
);
207 pthread_exit((void *)TS_MAGIC
);
211 zk_thread_join(kt_did_t tid
)
215 pthread_join((pthread_t
)tid
, &ret
);
216 VERIFY3P(ret
, ==, (void *)TS_MAGIC
);
220 * =========================================================================
222 * =========================================================================
226 kstat_create(char *module
, int instance
, char *name
, char *class,
227 uchar_t type
, ulong_t ndata
, uchar_t ks_flag
)
234 kstat_install(kstat_t
*ksp
)
239 kstat_delete(kstat_t
*ksp
)
243 * =========================================================================
245 * =========================================================================
249 mutex_init(kmutex_t
*mp
, char *name
, int type
, void *cookie
)
251 ASSERT3S(type
, ==, MUTEX_DEFAULT
);
252 ASSERT3P(cookie
, ==, NULL
);
253 mp
->m_owner
= MTX_INIT
;
254 mp
->m_magic
= MTX_MAGIC
;
255 VERIFY3S(pthread_mutex_init(&mp
->m_lock
, NULL
), ==, 0);
259 mutex_destroy(kmutex_t
*mp
)
261 ASSERT3U(mp
->m_magic
, ==, MTX_MAGIC
);
262 ASSERT3P(mp
->m_owner
, ==, MTX_INIT
);
263 VERIFY3S(pthread_mutex_destroy(&(mp
)->m_lock
), ==, 0);
264 mp
->m_owner
= MTX_DEST
;
269 mutex_enter(kmutex_t
*mp
)
271 ASSERT3U(mp
->m_magic
, ==, MTX_MAGIC
);
272 ASSERT3P(mp
->m_owner
, !=, MTX_DEST
);
273 ASSERT3P(mp
->m_owner
, !=, curthread
);
274 VERIFY3S(pthread_mutex_lock(&mp
->m_lock
), ==, 0);
275 ASSERT3P(mp
->m_owner
, ==, MTX_INIT
);
276 mp
->m_owner
= curthread
;
280 mutex_tryenter(kmutex_t
*mp
)
282 ASSERT3U(mp
->m_magic
, ==, MTX_MAGIC
);
283 ASSERT3P(mp
->m_owner
, !=, MTX_DEST
);
284 if (0 == pthread_mutex_trylock(&mp
->m_lock
)) {
285 ASSERT3P(mp
->m_owner
, ==, MTX_INIT
);
286 mp
->m_owner
= curthread
;
294 mutex_exit(kmutex_t
*mp
)
296 ASSERT3U(mp
->m_magic
, ==, MTX_MAGIC
);
297 ASSERT3P(mutex_owner(mp
), ==, curthread
);
298 mp
->m_owner
= MTX_INIT
;
299 VERIFY3S(pthread_mutex_unlock(&mp
->m_lock
), ==, 0);
303 mutex_owner(kmutex_t
*mp
)
305 ASSERT3U(mp
->m_magic
, ==, MTX_MAGIC
);
306 return (mp
->m_owner
);
310 mutex_held(kmutex_t
*mp
)
312 return (mp
->m_owner
== curthread
);
316 * =========================================================================
318 * =========================================================================
322 rw_init(krwlock_t
*rwlp
, char *name
, int type
, void *arg
)
324 ASSERT3S(type
, ==, RW_DEFAULT
);
325 ASSERT3P(arg
, ==, NULL
);
326 VERIFY3S(pthread_rwlock_init(&rwlp
->rw_lock
, NULL
), ==, 0);
327 rwlp
->rw_owner
= RW_INIT
;
328 rwlp
->rw_wr_owner
= RW_INIT
;
329 rwlp
->rw_readers
= 0;
330 rwlp
->rw_magic
= RW_MAGIC
;
334 rw_destroy(krwlock_t
*rwlp
)
336 ASSERT3U(rwlp
->rw_magic
, ==, RW_MAGIC
);
338 VERIFY3S(pthread_rwlock_destroy(&rwlp
->rw_lock
), ==, 0);
343 rw_enter(krwlock_t
*rwlp
, krw_t rw
)
345 ASSERT3U(rwlp
->rw_magic
, ==, RW_MAGIC
);
346 ASSERT3P(rwlp
->rw_owner
, !=, curthread
);
347 ASSERT3P(rwlp
->rw_wr_owner
, !=, curthread
);
349 if (rw
== RW_READER
) {
350 VERIFY3S(pthread_rwlock_rdlock(&rwlp
->rw_lock
), ==, 0);
351 ASSERT3P(rwlp
->rw_wr_owner
, ==, RW_INIT
);
353 atomic_inc_uint(&rwlp
->rw_readers
);
355 VERIFY3S(pthread_rwlock_wrlock(&rwlp
->rw_lock
), ==, 0);
356 ASSERT3P(rwlp
->rw_wr_owner
, ==, RW_INIT
);
357 ASSERT3U(rwlp
->rw_readers
, ==, 0);
359 rwlp
->rw_wr_owner
= curthread
;
362 rwlp
->rw_owner
= curthread
;
366 rw_exit(krwlock_t
*rwlp
)
368 ASSERT3U(rwlp
->rw_magic
, ==, RW_MAGIC
);
369 ASSERT(RW_LOCK_HELD(rwlp
));
371 if (RW_READ_HELD(rwlp
))
372 atomic_dec_uint(&rwlp
->rw_readers
);
374 rwlp
->rw_wr_owner
= RW_INIT
;
376 rwlp
->rw_owner
= RW_INIT
;
377 VERIFY3S(pthread_rwlock_unlock(&rwlp
->rw_lock
), ==, 0);
381 rw_tryenter(krwlock_t
*rwlp
, krw_t rw
)
385 ASSERT3U(rwlp
->rw_magic
, ==, RW_MAGIC
);
388 rv
= pthread_rwlock_tryrdlock(&rwlp
->rw_lock
);
390 rv
= pthread_rwlock_trywrlock(&rwlp
->rw_lock
);
393 ASSERT3P(rwlp
->rw_wr_owner
, ==, RW_INIT
);
396 atomic_inc_uint(&rwlp
->rw_readers
);
398 ASSERT3U(rwlp
->rw_readers
, ==, 0);
399 rwlp
->rw_wr_owner
= curthread
;
402 rwlp
->rw_owner
= curthread
;
406 VERIFY3S(rv
, ==, EBUSY
);
412 rw_tryupgrade(krwlock_t
*rwlp
)
414 ASSERT3U(rwlp
->rw_magic
, ==, RW_MAGIC
);
420 * =========================================================================
421 * condition variables
422 * =========================================================================
426 cv_init(kcondvar_t
*cv
, char *name
, int type
, void *arg
)
428 ASSERT3S(type
, ==, CV_DEFAULT
);
429 cv
->cv_magic
= CV_MAGIC
;
430 VERIFY3S(pthread_cond_init(&cv
->cv
, NULL
), ==, 0);
434 cv_destroy(kcondvar_t
*cv
)
436 ASSERT3U(cv
->cv_magic
, ==, CV_MAGIC
);
437 VERIFY3S(pthread_cond_destroy(&cv
->cv
), ==, 0);
442 cv_wait(kcondvar_t
*cv
, kmutex_t
*mp
)
444 ASSERT3U(cv
->cv_magic
, ==, CV_MAGIC
);
445 ASSERT3P(mutex_owner(mp
), ==, curthread
);
446 mp
->m_owner
= MTX_INIT
;
447 int ret
= pthread_cond_wait(&cv
->cv
, &mp
->m_lock
);
449 VERIFY3S(ret
, ==, EINTR
);
450 mp
->m_owner
= curthread
;
454 cv_timedwait(kcondvar_t
*cv
, kmutex_t
*mp
, clock_t abstime
)
461 ASSERT3U(cv
->cv_magic
, ==, CV_MAGIC
);
464 delta
= abstime
- ddi_get_lbolt();
468 VERIFY(gettimeofday(&tv
, NULL
) == 0);
470 ts
.tv_sec
= tv
.tv_sec
+ delta
/ hz
;
471 ts
.tv_nsec
= tv
.tv_usec
* 1000 + (delta
% hz
) * (NANOSEC
/ hz
);
472 if (ts
.tv_nsec
>= NANOSEC
) {
474 ts
.tv_nsec
-= NANOSEC
;
477 ASSERT3P(mutex_owner(mp
), ==, curthread
);
478 mp
->m_owner
= MTX_INIT
;
479 error
= pthread_cond_timedwait(&cv
->cv
, &mp
->m_lock
, &ts
);
480 mp
->m_owner
= curthread
;
482 if (error
== ETIMEDOUT
)
488 VERIFY3S(error
, ==, 0);
494 cv_signal(kcondvar_t
*cv
)
496 ASSERT3U(cv
->cv_magic
, ==, CV_MAGIC
);
497 VERIFY3S(pthread_cond_signal(&cv
->cv
), ==, 0);
501 cv_broadcast(kcondvar_t
*cv
)
503 ASSERT3U(cv
->cv_magic
, ==, CV_MAGIC
);
504 VERIFY3S(pthread_cond_broadcast(&cv
->cv
), ==, 0);
508 * =========================================================================
510 * =========================================================================
513 * Note: for the xxxat() versions of these functions, we assume that the
514 * starting vp is always rootdir (which is true for spa_directory.c, the only
515 * ZFS consumer of these interfaces). We assert this is true, and then emulate
516 * them by adding '/' in front of the path.
521 vn_open(char *path
, int x1
, int flags
, int mode
, vnode_t
**vpp
, int x2
, int x3
)
530 realpath
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
533 * If we're accessing a real disk from userland, we need to use
534 * the character interface to avoid caching. This is particularly
535 * important if we're trying to look at a real in-kernel storage
536 * pool from userland, e.g. via zdb, because otherwise we won't
537 * see the changes occurring under the segmap cache.
538 * On the other hand, the stupid character device returns zero
539 * for its size. So -- gag -- we open the block device to get
540 * its size, and remember it for subsequent VOP_GETATTR().
542 #if defined(__sun__) || defined(__sun)
543 if (strncmp(path
, "/dev/", 5) == 0) {
548 fd
= open64(path
, O_RDONLY
);
554 if (fstat64(fd
, &st
) == -1) {
561 (void) sprintf(realpath
, "%s", path
);
562 dsk
= strstr(path
, "/dsk/");
564 (void) sprintf(realpath
+ (dsk
- path
) + 1, "r%s",
567 (void) sprintf(realpath
, "%s", path
);
568 if (!(flags
& FCREAT
) && stat64(realpath
, &st
) == -1) {
575 if (!(flags
& FCREAT
) && S_ISBLK(st
.st_mode
)) {
579 /* We shouldn't be writing to block devices in userspace */
580 VERIFY(!(flags
& FWRITE
));
584 old_umask
= umask(0);
587 * The construct 'flags - FREAD' conveniently maps combinations of
588 * FREAD and FWRITE to the corresponding O_RDONLY, O_WRONLY, and O_RDWR.
590 fd
= open64(realpath
, flags
- FREAD
, mode
);
594 (void) umask(old_umask
);
599 if (fstat64_blk(fd
, &st
) == -1) {
605 (void) fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
607 *vpp
= vp
= umem_zalloc(sizeof (vnode_t
), UMEM_NOFAIL
);
610 vp
->v_size
= st
.st_size
;
611 vp
->v_path
= spa_strdup(path
);
618 vn_openat(char *path
, int x1
, int flags
, int mode
, vnode_t
**vpp
, int x2
,
619 int x3
, vnode_t
*startvp
, int fd
)
621 char *realpath
= umem_alloc(strlen(path
) + 2, UMEM_NOFAIL
);
624 ASSERT(startvp
== rootdir
);
625 (void) sprintf(realpath
, "/%s", path
);
627 /* fd ignored for now, need if want to simulate nbmand support */
628 ret
= vn_open(realpath
, x1
, flags
, mode
, vpp
, x2
, x3
);
630 umem_free(realpath
, strlen(path
) + 2);
637 vn_rdwr(int uio
, vnode_t
*vp
, void *addr
, ssize_t len
, offset_t offset
,
638 int x1
, int x2
, rlim64_t x3
, void *x4
, ssize_t
*residp
)
640 ssize_t rc
, done
= 0, split
;
642 if (uio
== UIO_READ
) {
643 rc
= pread64(vp
->v_fd
, addr
, len
, offset
);
646 * To simulate partial disk writes, we split writes into two
647 * system calls so that the process can be killed in between.
649 int sectors
= len
>> SPA_MINBLOCKSHIFT
;
650 split
= (sectors
> 0 ? rand() % sectors
: 0) <<
652 rc
= pwrite64(vp
->v_fd
, addr
, split
, offset
);
655 rc
= pwrite64(vp
->v_fd
, (char *)addr
+ split
,
656 len
- split
, offset
+ split
);
661 if (rc
== -1 && errno
== EINVAL
) {
663 * Under Linux, this most likely means an alignment issue
664 * (memory or disk) due to O_DIRECT, so we abort() in order to
665 * catch the offender.
676 *residp
= len
- done
;
677 else if (done
!= len
)
683 vn_close(vnode_t
*vp
)
686 spa_strfree(vp
->v_path
);
687 umem_free(vp
, sizeof (vnode_t
));
691 * At a minimum we need to update the size since vdev_reopen()
692 * will no longer call vn_openat().
695 fop_getattr(vnode_t
*vp
, vattr_t
*vap
)
700 if (fstat64_blk(vp
->v_fd
, &st
) == -1) {
706 vap
->va_size
= st
.st_size
;
711 * =========================================================================
712 * Figure out which debugging statements to print
713 * =========================================================================
716 static char *dprintf_string
;
717 static int dprintf_print_all
;
720 dprintf_find_string(const char *string
)
722 char *tmp_str
= dprintf_string
;
723 int len
= strlen(string
);
726 * Find out if this is a string we want to print.
727 * String format: file1.c,function_name1,file2.c,file3.c
730 while (tmp_str
!= NULL
) {
731 if (strncmp(tmp_str
, string
, len
) == 0 &&
732 (tmp_str
[len
] == ',' || tmp_str
[len
] == '\0'))
734 tmp_str
= strchr(tmp_str
, ',');
736 tmp_str
++; /* Get rid of , */
742 dprintf_setup(int *argc
, char **argv
)
747 * Debugging can be specified two ways: by setting the
748 * environment variable ZFS_DEBUG, or by including a
749 * "debug=..." argument on the command line. The command
750 * line setting overrides the environment variable.
753 for (i
= 1; i
< *argc
; i
++) {
754 int len
= strlen("debug=");
755 /* First look for a command line argument */
756 if (strncmp("debug=", argv
[i
], len
) == 0) {
757 dprintf_string
= argv
[i
] + len
;
758 /* Remove from args */
759 for (j
= i
; j
< *argc
; j
++)
766 if (dprintf_string
== NULL
) {
767 /* Look for ZFS_DEBUG environment variable */
768 dprintf_string
= getenv("ZFS_DEBUG");
772 * Are we just turning on all debugging?
774 if (dprintf_find_string("on"))
775 dprintf_print_all
= 1;
779 * =========================================================================
781 * =========================================================================
784 __dprintf(const char *file
, const char *func
, int line
, const char *fmt
, ...)
790 * Get rid of annoying "../common/" prefix to filename.
792 newfile
= strrchr(file
, '/');
793 if (newfile
!= NULL
) {
794 newfile
= newfile
+ 1; /* Get rid of leading / */
799 if (dprintf_print_all
||
800 dprintf_find_string(newfile
) ||
801 dprintf_find_string(func
)) {
802 /* Print out just the function name if requested */
804 if (dprintf_find_string("pid"))
805 (void) printf("%d ", getpid());
806 if (dprintf_find_string("tid"))
807 (void) printf("%u ", (uint_t
) pthread_self());
808 if (dprintf_find_string("cpu"))
809 (void) printf("%u ", getcpuid());
810 if (dprintf_find_string("time"))
811 (void) printf("%llu ", gethrtime());
812 if (dprintf_find_string("long"))
813 (void) printf("%s, line %d: ", newfile
, line
);
814 (void) printf("%s: ", func
);
816 (void) vprintf(fmt
, adx
);
823 * =========================================================================
824 * cmn_err() and panic()
825 * =========================================================================
827 static char ce_prefix
[CE_IGNORE
][10] = { "", "NOTICE: ", "WARNING: ", "" };
828 static char ce_suffix
[CE_IGNORE
][2] = { "", "\n", "\n", "" };
831 vpanic(const char *fmt
, va_list adx
)
833 (void) fprintf(stderr
, "error: ");
834 (void) vfprintf(stderr
, fmt
, adx
);
835 (void) fprintf(stderr
, "\n");
837 abort(); /* think of it as a "user-level crash dump" */
841 panic(const char *fmt
, ...)
851 vcmn_err(int ce
, const char *fmt
, va_list adx
)
855 if (ce
!= CE_NOTE
) { /* suppress noise in userland stress testing */
856 (void) fprintf(stderr
, "%s", ce_prefix
[ce
]);
857 (void) vfprintf(stderr
, fmt
, adx
);
858 (void) fprintf(stderr
, "%s", ce_suffix
[ce
]);
864 cmn_err(int ce
, const char *fmt
, ...)
869 vcmn_err(ce
, fmt
, adx
);
874 * =========================================================================
876 * =========================================================================
879 kobj_open_file(char *name
)
884 /* set vp as the _fd field of the file */
885 if (vn_openat(name
, UIO_SYSSPACE
, FREAD
, 0, &vp
, 0, 0, rootdir
,
887 return ((void *)-1UL);
889 file
= umem_zalloc(sizeof (struct _buf
), UMEM_NOFAIL
);
890 file
->_fd
= (intptr_t)vp
;
895 kobj_read_file(struct _buf
*file
, char *buf
, unsigned size
, unsigned off
)
899 vn_rdwr(UIO_READ
, (vnode_t
*)file
->_fd
, buf
, size
, (offset_t
)off
,
900 UIO_SYSSPACE
, 0, 0, 0, &resid
);
902 return (size
- resid
);
906 kobj_close_file(struct _buf
*file
)
908 vn_close((vnode_t
*)file
->_fd
);
909 umem_free(file
, sizeof (struct _buf
));
913 kobj_get_filesize(struct _buf
*file
, uint64_t *size
)
916 vnode_t
*vp
= (vnode_t
*)file
->_fd
;
918 if (fstat64(vp
->v_fd
, &st
) == -1) {
927 * =========================================================================
929 * =========================================================================
935 poll(0, 0, ticks
* (1000 / hz
));
939 * Find highest one bit set.
940 * Returns bit number + 1 of highest bit that is set, otherwise returns 0.
941 * High order bit is 31 (or 63 in _LP64 kernel).
951 if (i
& 0xffffffff00000000ul
) {
955 if (i
& 0xffff0000) {
973 static int random_fd
= -1, urandom_fd
= -1;
976 random_get_bytes_common(uint8_t *ptr
, size_t len
, int fd
)
984 bytes
= read(fd
, ptr
, resid
);
985 ASSERT3S(bytes
, >=, 0);
994 random_get_bytes(uint8_t *ptr
, size_t len
)
996 return (random_get_bytes_common(ptr
, len
, random_fd
));
1000 random_get_pseudo_bytes(uint8_t *ptr
, size_t len
)
1002 return (random_get_bytes_common(ptr
, len
, urandom_fd
));
1006 ddi_strtoul(const char *hw_serial
, char **nptr
, int base
, unsigned long *result
)
1010 *result
= strtoul(hw_serial
, &end
, base
);
1017 ddi_strtoull(const char *str
, char **nptr
, int base
, u_longlong_t
*result
)
1021 *result
= strtoull(str
, &end
, base
);
1028 * =========================================================================
1029 * kernel emulation setup & teardown
1030 * =========================================================================
1033 umem_out_of_memory(void)
1035 char errmsg
[] = "out of memory -- generating core dump\n";
1037 (void) fprintf(stderr
, "%s", errmsg
);
1043 kernel_init(int mode
)
1045 umem_nofail_callback(umem_out_of_memory
);
1047 physmem
= sysconf(_SC_PHYS_PAGES
);
1049 dprintf("physmem = %llu pages (%.2f GB)\n", physmem
,
1050 (double)physmem
* sysconf(_SC_PAGE_SIZE
) / (1ULL << 30));
1052 (void) snprintf(hw_serial
, sizeof (hw_serial
), "%ld",
1053 (mode
& FWRITE
) ? gethostid() : 0);
1055 VERIFY((random_fd
= open("/dev/random", O_RDONLY
)) != -1);
1056 VERIFY((urandom_fd
= open("/dev/urandom", O_RDONLY
)) != -1);
1059 system_taskq_init();
1069 system_taskq_fini();
1080 crgetuid(cred_t
*cr
)
1086 crgetgid(cred_t
*cr
)
1092 crgetngroups(cred_t
*cr
)
1098 crgetgroups(cred_t
*cr
)
1104 zfs_secpolicy_snapshot_perms(const char *name
, cred_t
*cr
)
1110 zfs_secpolicy_rename_perms(const char *from
, const char *to
, cred_t
*cr
)
1116 zfs_secpolicy_destroy_perms(const char *name
, cred_t
*cr
)
1122 ksid_lookupdomain(const char *dom
)
1126 kd
= umem_zalloc(sizeof (ksiddomain_t
), UMEM_NOFAIL
);
1127 kd
->kd_name
= spa_strdup(dom
);
1132 ksiddomain_rele(ksiddomain_t
*ksid
)
1134 spa_strfree(ksid
->kd_name
);
1135 umem_free(ksid
, sizeof (ksiddomain_t
));
1139 kmem_vasprintf(const char *fmt
, va_list adx
)
1144 va_copy(adx_copy
, adx
);
1145 VERIFY(vasprintf(&buf
, fmt
, adx_copy
) != -1);
1152 kmem_asprintf(const char *fmt
, ...)
1158 VERIFY(vasprintf(&buf
, fmt
, adx
) != -1);
1166 zfs_onexit_fd_hold(int fd
, minor_t
*minorp
)
1174 zfs_onexit_fd_rele(int fd
)
1180 zfs_onexit_add_cb(minor_t minor
, void (*func
)(void *), void *data
,
1181 uint64_t *action_handle
)
1188 zfs_onexit_del_cb(minor_t minor
, uint64_t action_handle
, boolean_t fire
)
1195 zfs_onexit_cb_data(minor_t minor
, uint64_t action_handle
, void **data
)