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/rrwlock.h>
38 #include <sys/utsname.h>
40 #include <sys/systeminfo.h>
43 * Emulation of kernel services in userland.
48 vnode_t *rootdir = (vnode_t *)0xabcd1234;
49 char hw_serial[HW_HOSTID_LEN];
51 struct utsname utsname = {
52 "userland", "libzpool", "1", "1", "na"
55 /* this only exists to have its address taken */
59 * =========================================================================
61 * =========================================================================
64 pthread_cond_t kthread_cond = PTHREAD_COND_INITIALIZER;
65 pthread_mutex_t kthread_lock = PTHREAD_MUTEX_INITIALIZER;
66 pthread_key_t kthread_key;
74 VERIFY3S(pthread_key_create(&kthread_key, NULL), ==, 0);
76 /* Create entry for primary kthread */
77 kt = umem_zalloc(sizeof (kthread_t), UMEM_NOFAIL);
78 kt->t_tid = pthread_self();
81 VERIFY3S(pthread_setspecific(kthread_key, kt), ==, 0);
83 /* Only the main thread should be running at the moment */
84 ASSERT3S(kthread_nr, ==, 0);
91 kthread_t *kt = curthread;
93 ASSERT(pthread_equal(kt->t_tid, pthread_self()));
94 ASSERT3P(kt->t_func, ==, NULL);
96 umem_free(kt, sizeof (kthread_t));
98 /* Wait for all threads to exit via thread_exit() */
99 VERIFY3S(pthread_mutex_lock(&kthread_lock), ==, 0);
101 kthread_nr--; /* Main thread is exiting */
103 while (kthread_nr > 0)
104 VERIFY3S(pthread_cond_wait(&kthread_cond, &kthread_lock), ==,
107 ASSERT3S(kthread_nr, ==, 0);
108 VERIFY3S(pthread_mutex_unlock(&kthread_lock), ==, 0);
110 VERIFY3S(pthread_key_delete(kthread_key), ==, 0);
114 zk_thread_current(void)
116 kthread_t *kt = pthread_getspecific(kthread_key);
118 ASSERT3P(kt, !=, NULL);
124 zk_thread_helper(void *arg)
126 kthread_t *kt = (kthread_t *) arg;
128 VERIFY3S(pthread_setspecific(kthread_key, kt), ==, 0);
130 VERIFY3S(pthread_mutex_lock(&kthread_lock), ==, 0);
132 VERIFY3S(pthread_mutex_unlock(&kthread_lock), ==, 0);
134 kt->t_tid = pthread_self();
135 ((thread_func_arg_t) kt->t_func)(kt->t_arg);
137 /* Unreachable, thread must exit with thread_exit() */
144 zk_thread_create(caddr_t stk, size_t stksize, thread_func_t func, void *arg,
145 size_t len, proc_t *pp, int state, pri_t pri, int detachstate)
151 ASSERT3S(state & ~TS_RUN, ==, 0);
153 kt = umem_zalloc(sizeof (kthread_t), UMEM_NOFAIL);
158 * The Solaris kernel stack size is 24k for x86/x86_64.
159 * The Linux kernel stack size is 8k for x86/x86_64.
161 * We reduce the default stack size in userspace, to ensure
162 * we observe stack overruns in user space as well as in
163 * kernel space. In practice we can't set the userspace stack
164 * size to 8k because differences in stack usage between kernel
165 * space and userspace could lead to spurious stack overflows
166 * (especially when debugging is enabled). Nevertheless, we try
167 * to set it to the lowest value that works (currently 8k*4).
168 * PTHREAD_STACK_MIN is the minimum stack required for a NULL
169 * procedure in user space and is added in to the stack
172 * Some buggy NPTL threading implementations include the
173 * guard area within the stack size allocations. In
174 * this case we allocate an extra page to account for the
175 * guard area since we only have two pages of usable stack
179 stack = PTHREAD_STACK_MIN + MAX(stksize, STACK_SIZE) * 4;
181 VERIFY3S(pthread_attr_init(&attr), ==, 0);
182 VERIFY3S(pthread_attr_setstacksize(&attr, stack), ==, 0);
183 VERIFY3S(pthread_attr_setguardsize(&attr, PAGESIZE), ==, 0);
184 VERIFY3S(pthread_attr_setdetachstate(&attr, detachstate), ==, 0);
186 VERIFY3S(pthread_create(&kt->t_tid, &attr, &zk_thread_helper, kt),
189 VERIFY3S(pthread_attr_destroy(&attr), ==, 0);
197 kthread_t *kt = curthread;
199 ASSERT(pthread_equal(kt->t_tid, pthread_self()));
201 umem_free(kt, sizeof (kthread_t));
203 pthread_mutex_lock(&kthread_lock);
205 pthread_mutex_unlock(&kthread_lock);
207 pthread_cond_broadcast(&kthread_cond);
208 pthread_exit((void *)TS_MAGIC);
212 zk_thread_join(kt_did_t tid)
216 pthread_join((pthread_t)tid, &ret);
217 VERIFY3P(ret, ==, (void *)TS_MAGIC);
221 * =========================================================================
223 * =========================================================================
227 kstat_create(const char *module, int instance, const char *name,
228 const char *class, uchar_t type, ulong_t ndata, uchar_t ks_flag)
235 kstat_install(kstat_t *ksp)
240 kstat_delete(kstat_t *ksp)
245 kstat_waitq_enter(kstat_io_t *kiop)
250 kstat_waitq_exit(kstat_io_t *kiop)
255 kstat_runq_enter(kstat_io_t *kiop)
260 kstat_runq_exit(kstat_io_t *kiop)
265 kstat_waitq_to_runq(kstat_io_t *kiop)
270 kstat_runq_back_to_waitq(kstat_io_t *kiop)
274 kstat_set_raw_ops(kstat_t *ksp,
275 int (*headers)(char *buf, size_t size),
276 int (*data)(char *buf, size_t size, void *data),
277 void *(*addr)(kstat_t *ksp, loff_t index))
281 * =========================================================================
283 * =========================================================================
287 mutex_init(kmutex_t *mp, char *name, int type, void *cookie)
289 ASSERT3S(type, ==, MUTEX_DEFAULT);
290 ASSERT3P(cookie, ==, NULL);
291 mp->m_owner = MTX_INIT;
292 mp->m_magic = MTX_MAGIC;
293 VERIFY3S(pthread_mutex_init(&mp->m_lock, NULL), ==, 0);
297 mutex_destroy(kmutex_t *mp)
299 ASSERT3U(mp->m_magic, ==, MTX_MAGIC);
300 ASSERT3P(mp->m_owner, ==, MTX_INIT);
301 VERIFY3S(pthread_mutex_destroy(&(mp)->m_lock), ==, 0);
302 mp->m_owner = MTX_DEST;
307 mutex_enter(kmutex_t *mp)
309 ASSERT3U(mp->m_magic, ==, MTX_MAGIC);
310 ASSERT3P(mp->m_owner, !=, MTX_DEST);
311 ASSERT3P(mp->m_owner, !=, curthread);
312 VERIFY3S(pthread_mutex_lock(&mp->m_lock), ==, 0);
313 ASSERT3P(mp->m_owner, ==, MTX_INIT);
314 mp->m_owner = curthread;
318 mutex_tryenter(kmutex_t *mp)
320 ASSERT3U(mp->m_magic, ==, MTX_MAGIC);
321 ASSERT3P(mp->m_owner, !=, MTX_DEST);
322 if (0 == pthread_mutex_trylock(&mp->m_lock)) {
323 ASSERT3P(mp->m_owner, ==, MTX_INIT);
324 mp->m_owner = curthread;
332 mutex_exit(kmutex_t *mp)
334 ASSERT3U(mp->m_magic, ==, MTX_MAGIC);
335 ASSERT3P(mutex_owner(mp), ==, curthread);
336 mp->m_owner = MTX_INIT;
337 VERIFY3S(pthread_mutex_unlock(&mp->m_lock), ==, 0);
341 mutex_owner(kmutex_t *mp)
343 ASSERT3U(mp->m_magic, ==, MTX_MAGIC);
344 return (mp->m_owner);
348 mutex_held(kmutex_t *mp)
350 return (mp->m_owner == curthread);
354 * =========================================================================
356 * =========================================================================
360 rw_init(krwlock_t *rwlp, char *name, int type, void *arg)
362 ASSERT3S(type, ==, RW_DEFAULT);
363 ASSERT3P(arg, ==, NULL);
364 VERIFY3S(pthread_rwlock_init(&rwlp->rw_lock, NULL), ==, 0);
365 rwlp->rw_owner = RW_INIT;
366 rwlp->rw_wr_owner = RW_INIT;
367 rwlp->rw_readers = 0;
368 rwlp->rw_magic = RW_MAGIC;
372 rw_destroy(krwlock_t *rwlp)
374 ASSERT3U(rwlp->rw_magic, ==, RW_MAGIC);
376 VERIFY3S(pthread_rwlock_destroy(&rwlp->rw_lock), ==, 0);
381 rw_enter(krwlock_t *rwlp, krw_t rw)
383 ASSERT3U(rwlp->rw_magic, ==, RW_MAGIC);
384 ASSERT3P(rwlp->rw_owner, !=, curthread);
385 ASSERT3P(rwlp->rw_wr_owner, !=, curthread);
387 if (rw == RW_READER) {
388 VERIFY3S(pthread_rwlock_rdlock(&rwlp->rw_lock), ==, 0);
389 ASSERT3P(rwlp->rw_wr_owner, ==, RW_INIT);
391 atomic_inc_uint(&rwlp->rw_readers);
393 VERIFY3S(pthread_rwlock_wrlock(&rwlp->rw_lock), ==, 0);
394 ASSERT3P(rwlp->rw_wr_owner, ==, RW_INIT);
395 ASSERT3U(rwlp->rw_readers, ==, 0);
397 rwlp->rw_wr_owner = curthread;
400 rwlp->rw_owner = curthread;
404 rw_exit(krwlock_t *rwlp)
406 ASSERT3U(rwlp->rw_magic, ==, RW_MAGIC);
407 ASSERT(RW_LOCK_HELD(rwlp));
409 if (RW_READ_HELD(rwlp))
410 atomic_dec_uint(&rwlp->rw_readers);
412 rwlp->rw_wr_owner = RW_INIT;
414 rwlp->rw_owner = RW_INIT;
415 VERIFY3S(pthread_rwlock_unlock(&rwlp->rw_lock), ==, 0);
419 rw_tryenter(krwlock_t *rwlp, krw_t rw)
423 ASSERT3U(rwlp->rw_magic, ==, RW_MAGIC);
426 rv = pthread_rwlock_tryrdlock(&rwlp->rw_lock);
428 rv = pthread_rwlock_trywrlock(&rwlp->rw_lock);
431 ASSERT3P(rwlp->rw_wr_owner, ==, RW_INIT);
434 atomic_inc_uint(&rwlp->rw_readers);
436 ASSERT3U(rwlp->rw_readers, ==, 0);
437 rwlp->rw_wr_owner = curthread;
440 rwlp->rw_owner = curthread;
444 VERIFY3S(rv, ==, EBUSY);
450 rw_tryupgrade(krwlock_t *rwlp)
452 ASSERT3U(rwlp->rw_magic, ==, RW_MAGIC);
458 * =========================================================================
459 * condition variables
460 * =========================================================================
464 cv_init(kcondvar_t *cv, char *name, int type, void *arg)
466 ASSERT3S(type, ==, CV_DEFAULT);
467 cv->cv_magic = CV_MAGIC;
468 VERIFY3S(pthread_cond_init(&cv->cv, NULL), ==, 0);
472 cv_destroy(kcondvar_t *cv)
474 ASSERT3U(cv->cv_magic, ==, CV_MAGIC);
475 VERIFY3S(pthread_cond_destroy(&cv->cv), ==, 0);
480 cv_wait(kcondvar_t *cv, kmutex_t *mp)
482 ASSERT3U(cv->cv_magic, ==, CV_MAGIC);
483 ASSERT3P(mutex_owner(mp), ==, curthread);
484 mp->m_owner = MTX_INIT;
485 int ret = pthread_cond_wait(&cv->cv, &mp->m_lock);
487 VERIFY3S(ret, ==, EINTR);
488 mp->m_owner = curthread;
492 cv_timedwait(kcondvar_t *cv, kmutex_t *mp, clock_t abstime)
499 ASSERT3U(cv->cv_magic, ==, CV_MAGIC);
502 delta = abstime - ddi_get_lbolt();
506 VERIFY(gettimeofday(&tv, NULL) == 0);
508 ts.tv_sec = tv.tv_sec + delta / hz;
509 ts.tv_nsec = tv.tv_usec * 1000 + (delta % hz) * (NANOSEC / hz);
510 if (ts.tv_nsec >= NANOSEC) {
512 ts.tv_nsec -= NANOSEC;
515 ASSERT3P(mutex_owner(mp), ==, curthread);
516 mp->m_owner = MTX_INIT;
517 error = pthread_cond_timedwait(&cv->cv, &mp->m_lock, &ts);
518 mp->m_owner = curthread;
520 if (error == ETIMEDOUT)
526 VERIFY3S(error, ==, 0);
533 cv_timedwait_hires(kcondvar_t *cv, kmutex_t *mp, hrtime_t tim, hrtime_t res,
543 delta = tim - gethrtime();
547 ts.tv_sec = delta / NANOSEC;
548 ts.tv_nsec = delta % NANOSEC;
550 ASSERT(mutex_owner(mp) == curthread);
552 error = pthread_cond_timedwait(&cv->cv, &mp->m_lock, &ts);
553 mp->m_owner = curthread;
567 cv_signal(kcondvar_t *cv)
569 ASSERT3U(cv->cv_magic, ==, CV_MAGIC);
570 VERIFY3S(pthread_cond_signal(&cv->cv), ==, 0);
574 cv_broadcast(kcondvar_t *cv)
576 ASSERT3U(cv->cv_magic, ==, CV_MAGIC);
577 VERIFY3S(pthread_cond_broadcast(&cv->cv), ==, 0);
581 * =========================================================================
583 * =========================================================================
586 * Note: for the xxxat() versions of these functions, we assume that the
587 * starting vp is always rootdir (which is true for spa_directory.c, the only
588 * ZFS consumer of these interfaces). We assert this is true, and then emulate
589 * them by adding '/' in front of the path.
594 vn_open(char *path, int x1, int flags, int mode, vnode_t **vpp, int x2, int x3)
603 realpath = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
606 * If we're accessing a real disk from userland, we need to use
607 * the character interface to avoid caching. This is particularly
608 * important if we're trying to look at a real in-kernel storage
609 * pool from userland, e.g. via zdb, because otherwise we won't
610 * see the changes occurring under the segmap cache.
611 * On the other hand, the stupid character device returns zero
612 * for its size. So -- gag -- we open the block device to get
613 * its size, and remember it for subsequent VOP_GETATTR().
615 #if defined(__sun__) || defined(__sun)
616 if (strncmp(path, "/dev/", 5) == 0) {
621 fd = open64(path, O_RDONLY);
627 if (fstat64(fd, &st) == -1) {
634 (void) sprintf(realpath, "%s", path);
635 dsk = strstr(path, "/dsk/");
637 (void) sprintf(realpath + (dsk - path) + 1, "r%s",
640 (void) sprintf(realpath, "%s", path);
641 if (!(flags & FCREAT) && stat64(realpath, &st) == -1) {
648 if (!(flags & FCREAT) && S_ISBLK(st.st_mode)) {
652 /* We shouldn't be writing to block devices in userspace */
653 VERIFY(!(flags & FWRITE));
657 old_umask = umask(0);
660 * The construct 'flags - FREAD' conveniently maps combinations of
661 * FREAD and FWRITE to the corresponding O_RDONLY, O_WRONLY, and O_RDWR.
663 fd = open64(realpath, flags - FREAD, mode);
667 (void) umask(old_umask);
672 if (fstat64_blk(fd, &st) == -1) {
678 (void) fcntl(fd, F_SETFD, FD_CLOEXEC);
680 *vpp = vp = umem_zalloc(sizeof (vnode_t), UMEM_NOFAIL);
683 vp->v_size = st.st_size;
684 vp->v_path = spa_strdup(path);
691 vn_openat(char *path, int x1, int flags, int mode, vnode_t **vpp, int x2,
692 int x3, vnode_t *startvp, int fd)
694 char *realpath = umem_alloc(strlen(path) + 2, UMEM_NOFAIL);
697 ASSERT(startvp == rootdir);
698 (void) sprintf(realpath, "/%s", path);
700 /* fd ignored for now, need if want to simulate nbmand support */
701 ret = vn_open(realpath, x1, flags, mode, vpp, x2, x3);
703 umem_free(realpath, strlen(path) + 2);
710 vn_rdwr(int uio, vnode_t *vp, void *addr, ssize_t len, offset_t offset,
711 int x1, int x2, rlim64_t x3, void *x4, ssize_t *residp)
713 ssize_t rc, done = 0, split;
715 if (uio == UIO_READ) {
716 rc = pread64(vp->v_fd, addr, len, offset);
719 * To simulate partial disk writes, we split writes into two
720 * system calls so that the process can be killed in between.
722 int sectors = len >> SPA_MINBLOCKSHIFT;
723 split = (sectors > 0 ? rand() % sectors : 0) <<
725 rc = pwrite64(vp->v_fd, addr, split, offset);
728 rc = pwrite64(vp->v_fd, (char *)addr + split,
729 len - split, offset + split);
734 if (rc == -1 && errno == EINVAL) {
736 * Under Linux, this most likely means an alignment issue
737 * (memory or disk) due to O_DIRECT, so we abort() in order to
738 * catch the offender.
749 *residp = len - done;
750 else if (done != len)
756 vn_close(vnode_t *vp)
759 spa_strfree(vp->v_path);
760 umem_free(vp, sizeof (vnode_t));
764 * At a minimum we need to update the size since vdev_reopen()
765 * will no longer call vn_openat().
768 fop_getattr(vnode_t *vp, vattr_t *vap)
773 if (fstat64_blk(vp->v_fd, &st) == -1) {
779 vap->va_size = st.st_size;
784 * =========================================================================
785 * Figure out which debugging statements to print
786 * =========================================================================
789 static char *dprintf_string;
790 static int dprintf_print_all;
793 dprintf_find_string(const char *string)
795 char *tmp_str = dprintf_string;
796 int len = strlen(string);
799 * Find out if this is a string we want to print.
800 * String format: file1.c,function_name1,file2.c,file3.c
803 while (tmp_str != NULL) {
804 if (strncmp(tmp_str, string, len) == 0 &&
805 (tmp_str[len] == ',' || tmp_str[len] == '\0'))
807 tmp_str = strchr(tmp_str, ',');
809 tmp_str++; /* Get rid of , */
815 dprintf_setup(int *argc, char **argv)
820 * Debugging can be specified two ways: by setting the
821 * environment variable ZFS_DEBUG, or by including a
822 * "debug=..." argument on the command line. The command
823 * line setting overrides the environment variable.
826 for (i = 1; i < *argc; i++) {
827 int len = strlen("debug=");
828 /* First look for a command line argument */
829 if (strncmp("debug=", argv[i], len) == 0) {
830 dprintf_string = argv[i] + len;
831 /* Remove from args */
832 for (j = i; j < *argc; j++)
839 if (dprintf_string == NULL) {
840 /* Look for ZFS_DEBUG environment variable */
841 dprintf_string = getenv("ZFS_DEBUG");
845 * Are we just turning on all debugging?
847 if (dprintf_find_string("on"))
848 dprintf_print_all = 1;
852 * =========================================================================
854 * =========================================================================
857 __dprintf(const char *file, const char *func, int line, const char *fmt, ...)
863 * Get rid of annoying "../common/" prefix to filename.
865 newfile = strrchr(file, '/');
866 if (newfile != NULL) {
867 newfile = newfile + 1; /* Get rid of leading / */
872 if (dprintf_print_all ||
873 dprintf_find_string(newfile) ||
874 dprintf_find_string(func)) {
875 /* Print out just the function name if requested */
877 if (dprintf_find_string("pid"))
878 (void) printf("%d ", getpid());
879 if (dprintf_find_string("tid"))
880 (void) printf("%u ", (uint_t) pthread_self());
881 if (dprintf_find_string("cpu"))
882 (void) printf("%u ", getcpuid());
883 if (dprintf_find_string("time"))
884 (void) printf("%llu ", gethrtime());
885 if (dprintf_find_string("long"))
886 (void) printf("%s, line %d: ", newfile, line);
887 (void) printf("%s: ", func);
889 (void) vprintf(fmt, adx);
896 * =========================================================================
897 * cmn_err() and panic()
898 * =========================================================================
900 static char ce_prefix[CE_IGNORE][10] = { "", "NOTICE: ", "WARNING: ", "" };
901 static char ce_suffix[CE_IGNORE][2] = { "", "\n", "\n", "" };
904 vpanic(const char *fmt, va_list adx)
906 (void) fprintf(stderr, "error: ");
907 (void) vfprintf(stderr, fmt, adx);
908 (void) fprintf(stderr, "\n");
910 abort(); /* think of it as a "user-level crash dump" */
914 panic(const char *fmt, ...)
924 vcmn_err(int ce, const char *fmt, va_list adx)
928 if (ce != CE_NOTE) { /* suppress noise in userland stress testing */
929 (void) fprintf(stderr, "%s", ce_prefix[ce]);
930 (void) vfprintf(stderr, fmt, adx);
931 (void) fprintf(stderr, "%s", ce_suffix[ce]);
937 cmn_err(int ce, const char *fmt, ...)
942 vcmn_err(ce, fmt, adx);
947 * =========================================================================
949 * =========================================================================
952 kobj_open_file(char *name)
957 /* set vp as the _fd field of the file */
958 if (vn_openat(name, UIO_SYSSPACE, FREAD, 0, &vp, 0, 0, rootdir,
960 return ((void *)-1UL);
962 file = umem_zalloc(sizeof (struct _buf), UMEM_NOFAIL);
963 file->_fd = (intptr_t)vp;
968 kobj_read_file(struct _buf *file, char *buf, unsigned size, unsigned off)
972 vn_rdwr(UIO_READ, (vnode_t *)file->_fd, buf, size, (offset_t)off,
973 UIO_SYSSPACE, 0, 0, 0, &resid);
975 return (size - resid);
979 kobj_close_file(struct _buf *file)
981 vn_close((vnode_t *)file->_fd);
982 umem_free(file, sizeof (struct _buf));
986 kobj_get_filesize(struct _buf *file, uint64_t *size)
989 vnode_t *vp = (vnode_t *)file->_fd;
991 if (fstat64(vp->v_fd, &st) == -1) {
1000 * =========================================================================
1002 * =========================================================================
1006 delay(clock_t ticks)
1008 poll(0, 0, ticks * (1000 / hz));
1012 * Find highest one bit set.
1013 * Returns bit number + 1 of highest bit that is set, otherwise returns 0.
1014 * High order bit is 31 (or 63 in _LP64 kernel).
1024 if (i & 0xffffffff00000000ul) {
1028 if (i & 0xffff0000) {
1046 static int random_fd = -1, urandom_fd = -1;
1049 random_get_bytes_common(uint8_t *ptr, size_t len, int fd)
1056 while (resid != 0) {
1057 bytes = read(fd, ptr, resid);
1058 ASSERT3S(bytes, >=, 0);
1067 random_get_bytes(uint8_t *ptr, size_t len)
1069 return (random_get_bytes_common(ptr, len, random_fd));
1073 random_get_pseudo_bytes(uint8_t *ptr, size_t len)
1075 return (random_get_bytes_common(ptr, len, urandom_fd));
1079 ddi_strtoul(const char *hw_serial, char **nptr, int base, unsigned long *result)
1083 *result = strtoul(hw_serial, &end, base);
1090 ddi_strtoull(const char *str, char **nptr, int base, u_longlong_t *result)
1094 *result = strtoull(str, &end, base);
1101 * =========================================================================
1102 * kernel emulation setup & teardown
1103 * =========================================================================
1106 umem_out_of_memory(void)
1108 char errmsg[] = "out of memory -- generating core dump\n";
1110 (void) fprintf(stderr, "%s", errmsg);
1116 kernel_init(int mode)
1118 extern uint_t rrw_tsd_key;
1120 umem_nofail_callback(umem_out_of_memory);
1122 physmem = sysconf(_SC_PHYS_PAGES);
1124 dprintf("physmem = %llu pages (%.2f GB)\n", physmem,
1125 (double)physmem * sysconf(_SC_PAGE_SIZE) / (1ULL << 30));
1127 (void) snprintf(hw_serial, sizeof (hw_serial), "%ld",
1128 (mode & FWRITE) ? gethostid() : 0);
1130 VERIFY((random_fd = open("/dev/random", O_RDONLY)) != -1);
1131 VERIFY((urandom_fd = open("/dev/urandom", O_RDONLY)) != -1);
1134 system_taskq_init();
1138 tsd_create(&rrw_tsd_key, rrw_tsd_destroy);
1146 system_taskq_fini();
1157 crgetuid(cred_t *cr)
1163 crgetruid(cred_t *cr)
1169 crgetgid(cred_t *cr)
1175 crgetngroups(cred_t *cr)
1181 crgetgroups(cred_t *cr)
1187 zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
1193 zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
1199 zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
1205 ksid_lookupdomain(const char *dom)
1209 kd = umem_zalloc(sizeof (ksiddomain_t), UMEM_NOFAIL);
1210 kd->kd_name = spa_strdup(dom);
1215 ksiddomain_rele(ksiddomain_t *ksid)
1217 spa_strfree(ksid->kd_name);
1218 umem_free(ksid, sizeof (ksiddomain_t));
1222 kmem_vasprintf(const char *fmt, va_list adx)
1227 va_copy(adx_copy, adx);
1228 VERIFY(vasprintf(&buf, fmt, adx_copy) != -1);
1235 kmem_asprintf(const char *fmt, ...)
1241 VERIFY(vasprintf(&buf, fmt, adx) != -1);
1249 zfs_onexit_fd_hold(int fd, minor_t *minorp)
1257 zfs_onexit_fd_rele(int fd)
1263 zfs_onexit_add_cb(minor_t minor, void (*func)(void *), void *data,
1264 uint64_t *action_handle)
1271 zfs_onexit_del_cb(minor_t minor, uint64_t action_handle, boolean_t fire)
1278 zfs_onexit_cb_data(minor_t minor, uint64_t action_handle, void **data)