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
173 stack
= PTHREAD_STACK_MIN
+ MAX(stksize
, STACK_SIZE
) * 4;
175 VERIFY3S(pthread_attr_init(&attr
), ==, 0);
176 VERIFY3S(pthread_attr_setstacksize(&attr
, stack
), ==, 0);
177 VERIFY3S(pthread_attr_setguardsize(&attr
, PAGESIZE
), ==, 0);
178 VERIFY3S(pthread_attr_setdetachstate(&attr
, detachstate
), ==, 0);
180 VERIFY3S(pthread_create(&kt
->t_tid
, &attr
, &zk_thread_helper
, kt
),
183 VERIFY3S(pthread_attr_destroy(&attr
), ==, 0);
191 kthread_t
*kt
= curthread
;
193 ASSERT(pthread_equal(kt
->t_tid
, pthread_self()));
195 umem_free(kt
, sizeof (kthread_t
));
197 pthread_mutex_lock(&kthread_lock
);
199 pthread_mutex_unlock(&kthread_lock
);
201 pthread_cond_broadcast(&kthread_cond
);
202 pthread_exit((void *)TS_MAGIC
);
206 zk_thread_join(kt_did_t tid
)
210 pthread_join((pthread_t
)tid
, &ret
);
211 VERIFY3P(ret
, ==, (void *)TS_MAGIC
);
215 * =========================================================================
217 * =========================================================================
221 kstat_create(const char *module
, int instance
, const char *name
,
222 const char *class, uchar_t type
, ulong_t ndata
, uchar_t ks_flag
)
229 kstat_install(kstat_t
*ksp
)
234 kstat_delete(kstat_t
*ksp
)
239 kstat_waitq_enter(kstat_io_t
*kiop
)
244 kstat_waitq_exit(kstat_io_t
*kiop
)
249 kstat_runq_enter(kstat_io_t
*kiop
)
254 kstat_runq_exit(kstat_io_t
*kiop
)
259 kstat_waitq_to_runq(kstat_io_t
*kiop
)
264 kstat_runq_back_to_waitq(kstat_io_t
*kiop
)
268 kstat_set_raw_ops(kstat_t
*ksp
,
269 int (*headers
)(char *buf
, size_t size
),
270 int (*data
)(char *buf
, size_t size
, void *data
),
271 void *(*addr
)(kstat_t
*ksp
, loff_t index
))
275 * =========================================================================
277 * =========================================================================
281 mutex_init(kmutex_t
*mp
, char *name
, int type
, void *cookie
)
283 ASSERT3S(type
, ==, MUTEX_DEFAULT
);
284 ASSERT3P(cookie
, ==, NULL
);
285 mp
->m_owner
= MTX_INIT
;
286 mp
->m_magic
= MTX_MAGIC
;
287 VERIFY3S(pthread_mutex_init(&mp
->m_lock
, NULL
), ==, 0);
291 mutex_destroy(kmutex_t
*mp
)
293 ASSERT3U(mp
->m_magic
, ==, MTX_MAGIC
);
294 ASSERT3P(mp
->m_owner
, ==, MTX_INIT
);
295 VERIFY3S(pthread_mutex_destroy(&(mp
)->m_lock
), ==, 0);
296 mp
->m_owner
= MTX_DEST
;
301 mutex_enter(kmutex_t
*mp
)
303 ASSERT3U(mp
->m_magic
, ==, MTX_MAGIC
);
304 ASSERT3P(mp
->m_owner
, !=, MTX_DEST
);
305 ASSERT3P(mp
->m_owner
, !=, curthread
);
306 VERIFY3S(pthread_mutex_lock(&mp
->m_lock
), ==, 0);
307 ASSERT3P(mp
->m_owner
, ==, MTX_INIT
);
308 mp
->m_owner
= curthread
;
312 mutex_tryenter(kmutex_t
*mp
)
314 ASSERT3U(mp
->m_magic
, ==, MTX_MAGIC
);
315 ASSERT3P(mp
->m_owner
, !=, MTX_DEST
);
316 if (0 == pthread_mutex_trylock(&mp
->m_lock
)) {
317 ASSERT3P(mp
->m_owner
, ==, MTX_INIT
);
318 mp
->m_owner
= curthread
;
326 mutex_exit(kmutex_t
*mp
)
328 ASSERT3U(mp
->m_magic
, ==, MTX_MAGIC
);
329 ASSERT3P(mutex_owner(mp
), ==, curthread
);
330 mp
->m_owner
= MTX_INIT
;
331 VERIFY3S(pthread_mutex_unlock(&mp
->m_lock
), ==, 0);
335 mutex_owner(kmutex_t
*mp
)
337 ASSERT3U(mp
->m_magic
, ==, MTX_MAGIC
);
338 return (mp
->m_owner
);
342 mutex_held(kmutex_t
*mp
)
344 return (mp
->m_owner
== curthread
);
348 * =========================================================================
350 * =========================================================================
354 rw_init(krwlock_t
*rwlp
, char *name
, int type
, void *arg
)
356 ASSERT3S(type
, ==, RW_DEFAULT
);
357 ASSERT3P(arg
, ==, NULL
);
358 VERIFY3S(pthread_rwlock_init(&rwlp
->rw_lock
, NULL
), ==, 0);
359 rwlp
->rw_owner
= RW_INIT
;
360 rwlp
->rw_wr_owner
= RW_INIT
;
361 rwlp
->rw_readers
= 0;
362 rwlp
->rw_magic
= RW_MAGIC
;
366 rw_destroy(krwlock_t
*rwlp
)
368 ASSERT3U(rwlp
->rw_magic
, ==, RW_MAGIC
);
369 ASSERT(rwlp
->rw_readers
== 0 && rwlp
->rw_wr_owner
== RW_INIT
);
370 VERIFY3S(pthread_rwlock_destroy(&rwlp
->rw_lock
), ==, 0);
375 rw_enter(krwlock_t
*rwlp
, krw_t rw
)
377 ASSERT3U(rwlp
->rw_magic
, ==, RW_MAGIC
);
378 ASSERT3P(rwlp
->rw_owner
, !=, curthread
);
379 ASSERT3P(rwlp
->rw_wr_owner
, !=, curthread
);
381 if (rw
== RW_READER
) {
382 VERIFY3S(pthread_rwlock_rdlock(&rwlp
->rw_lock
), ==, 0);
383 ASSERT3P(rwlp
->rw_wr_owner
, ==, RW_INIT
);
385 atomic_inc_uint(&rwlp
->rw_readers
);
387 VERIFY3S(pthread_rwlock_wrlock(&rwlp
->rw_lock
), ==, 0);
388 ASSERT3P(rwlp
->rw_wr_owner
, ==, RW_INIT
);
389 ASSERT3U(rwlp
->rw_readers
, ==, 0);
391 rwlp
->rw_wr_owner
= curthread
;
394 rwlp
->rw_owner
= curthread
;
398 rw_exit(krwlock_t
*rwlp
)
400 ASSERT3U(rwlp
->rw_magic
, ==, RW_MAGIC
);
401 ASSERT(RW_LOCK_HELD(rwlp
));
403 if (RW_READ_HELD(rwlp
))
404 atomic_dec_uint(&rwlp
->rw_readers
);
406 rwlp
->rw_wr_owner
= RW_INIT
;
408 rwlp
->rw_owner
= RW_INIT
;
409 VERIFY3S(pthread_rwlock_unlock(&rwlp
->rw_lock
), ==, 0);
413 rw_tryenter(krwlock_t
*rwlp
, krw_t rw
)
417 ASSERT3U(rwlp
->rw_magic
, ==, RW_MAGIC
);
420 rv
= pthread_rwlock_tryrdlock(&rwlp
->rw_lock
);
422 rv
= pthread_rwlock_trywrlock(&rwlp
->rw_lock
);
425 ASSERT3P(rwlp
->rw_wr_owner
, ==, RW_INIT
);
428 atomic_inc_uint(&rwlp
->rw_readers
);
430 ASSERT3U(rwlp
->rw_readers
, ==, 0);
431 rwlp
->rw_wr_owner
= curthread
;
434 rwlp
->rw_owner
= curthread
;
438 VERIFY3S(rv
, ==, EBUSY
);
444 rw_tryupgrade(krwlock_t
*rwlp
)
446 ASSERT3U(rwlp
->rw_magic
, ==, RW_MAGIC
);
452 * =========================================================================
453 * condition variables
454 * =========================================================================
458 cv_init(kcondvar_t
*cv
, char *name
, int type
, void *arg
)
460 ASSERT3S(type
, ==, CV_DEFAULT
);
461 cv
->cv_magic
= CV_MAGIC
;
462 VERIFY3S(pthread_cond_init(&cv
->cv
, NULL
), ==, 0);
466 cv_destroy(kcondvar_t
*cv
)
468 ASSERT3U(cv
->cv_magic
, ==, CV_MAGIC
);
469 VERIFY3S(pthread_cond_destroy(&cv
->cv
), ==, 0);
474 cv_wait(kcondvar_t
*cv
, kmutex_t
*mp
)
476 ASSERT3U(cv
->cv_magic
, ==, CV_MAGIC
);
477 ASSERT3P(mutex_owner(mp
), ==, curthread
);
478 mp
->m_owner
= MTX_INIT
;
479 int ret
= pthread_cond_wait(&cv
->cv
, &mp
->m_lock
);
481 VERIFY3S(ret
, ==, EINTR
);
482 mp
->m_owner
= curthread
;
486 cv_timedwait(kcondvar_t
*cv
, kmutex_t
*mp
, clock_t abstime
)
493 ASSERT3U(cv
->cv_magic
, ==, CV_MAGIC
);
496 delta
= abstime
- ddi_get_lbolt();
500 VERIFY(gettimeofday(&tv
, NULL
) == 0);
502 ts
.tv_sec
= tv
.tv_sec
+ delta
/ hz
;
503 ts
.tv_nsec
= tv
.tv_usec
* 1000 + (delta
% hz
) * (NANOSEC
/ hz
);
504 if (ts
.tv_nsec
>= NANOSEC
) {
506 ts
.tv_nsec
-= NANOSEC
;
509 ASSERT3P(mutex_owner(mp
), ==, curthread
);
510 mp
->m_owner
= MTX_INIT
;
511 error
= pthread_cond_timedwait(&cv
->cv
, &mp
->m_lock
, &ts
);
512 mp
->m_owner
= curthread
;
514 if (error
== ETIMEDOUT
)
520 VERIFY3S(error
, ==, 0);
527 cv_timedwait_hires(kcondvar_t
*cv
, kmutex_t
*mp
, hrtime_t tim
, hrtime_t res
,
537 delta
= tim
- gethrtime();
541 ts
.tv_sec
= delta
/ NANOSEC
;
542 ts
.tv_nsec
= delta
% NANOSEC
;
544 ASSERT(mutex_owner(mp
) == curthread
);
546 error
= pthread_cond_timedwait(&cv
->cv
, &mp
->m_lock
, &ts
);
547 mp
->m_owner
= curthread
;
561 cv_signal(kcondvar_t
*cv
)
563 ASSERT3U(cv
->cv_magic
, ==, CV_MAGIC
);
564 VERIFY3S(pthread_cond_signal(&cv
->cv
), ==, 0);
568 cv_broadcast(kcondvar_t
*cv
)
570 ASSERT3U(cv
->cv_magic
, ==, CV_MAGIC
);
571 VERIFY3S(pthread_cond_broadcast(&cv
->cv
), ==, 0);
575 * =========================================================================
577 * =========================================================================
580 * Note: for the xxxat() versions of these functions, we assume that the
581 * starting vp is always rootdir (which is true for spa_directory.c, the only
582 * ZFS consumer of these interfaces). We assert this is true, and then emulate
583 * them by adding '/' in front of the path.
588 vn_open(char *path
, int x1
, int flags
, int mode
, vnode_t
**vpp
, int x2
, int x3
)
597 realpath
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
600 * If we're accessing a real disk from userland, we need to use
601 * the character interface to avoid caching. This is particularly
602 * important if we're trying to look at a real in-kernel storage
603 * pool from userland, e.g. via zdb, because otherwise we won't
604 * see the changes occurring under the segmap cache.
605 * On the other hand, the stupid character device returns zero
606 * for its size. So -- gag -- we open the block device to get
607 * its size, and remember it for subsequent VOP_GETATTR().
609 #if defined(__sun__) || defined(__sun)
610 if (strncmp(path
, "/dev/", 5) == 0) {
615 fd
= open64(path
, O_RDONLY
);
621 if (fstat64(fd
, &st
) == -1) {
628 (void) sprintf(realpath
, "%s", path
);
629 dsk
= strstr(path
, "/dsk/");
631 (void) sprintf(realpath
+ (dsk
- path
) + 1, "r%s",
634 (void) sprintf(realpath
, "%s", path
);
635 if (!(flags
& FCREAT
) && stat64(realpath
, &st
) == -1) {
642 if (!(flags
& FCREAT
) && S_ISBLK(st
.st_mode
)) {
646 /* We shouldn't be writing to block devices in userspace */
647 VERIFY(!(flags
& FWRITE
));
651 old_umask
= umask(0);
654 * The construct 'flags - FREAD' conveniently maps combinations of
655 * FREAD and FWRITE to the corresponding O_RDONLY, O_WRONLY, and O_RDWR.
657 fd
= open64(realpath
, flags
- FREAD
, mode
);
661 (void) umask(old_umask
);
666 if (fstat64_blk(fd
, &st
) == -1) {
672 (void) fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
674 *vpp
= vp
= umem_zalloc(sizeof (vnode_t
), UMEM_NOFAIL
);
677 vp
->v_size
= st
.st_size
;
678 vp
->v_path
= spa_strdup(path
);
685 vn_openat(char *path
, int x1
, int flags
, int mode
, vnode_t
**vpp
, int x2
,
686 int x3
, vnode_t
*startvp
, int fd
)
688 char *realpath
= umem_alloc(strlen(path
) + 2, UMEM_NOFAIL
);
691 ASSERT(startvp
== rootdir
);
692 (void) sprintf(realpath
, "/%s", path
);
694 /* fd ignored for now, need if want to simulate nbmand support */
695 ret
= vn_open(realpath
, x1
, flags
, mode
, vpp
, x2
, x3
);
697 umem_free(realpath
, strlen(path
) + 2);
704 vn_rdwr(int uio
, vnode_t
*vp
, void *addr
, ssize_t len
, offset_t offset
,
705 int x1
, int x2
, rlim64_t x3
, void *x4
, ssize_t
*residp
)
707 ssize_t rc
, done
= 0, split
;
709 if (uio
== UIO_READ
) {
710 rc
= pread64(vp
->v_fd
, addr
, len
, offset
);
713 * To simulate partial disk writes, we split writes into two
714 * system calls so that the process can be killed in between.
716 int sectors
= len
>> SPA_MINBLOCKSHIFT
;
717 split
= (sectors
> 0 ? rand() % sectors
: 0) <<
719 rc
= pwrite64(vp
->v_fd
, addr
, split
, offset
);
722 rc
= pwrite64(vp
->v_fd
, (char *)addr
+ split
,
723 len
- split
, offset
+ split
);
728 if (rc
== -1 && errno
== EINVAL
) {
730 * Under Linux, this most likely means an alignment issue
731 * (memory or disk) due to O_DIRECT, so we abort() in order to
732 * catch the offender.
743 *residp
= len
- done
;
744 else if (done
!= len
)
750 vn_close(vnode_t
*vp
)
753 spa_strfree(vp
->v_path
);
754 umem_free(vp
, sizeof (vnode_t
));
758 * At a minimum we need to update the size since vdev_reopen()
759 * will no longer call vn_openat().
762 fop_getattr(vnode_t
*vp
, vattr_t
*vap
)
767 if (fstat64_blk(vp
->v_fd
, &st
) == -1) {
773 vap
->va_size
= st
.st_size
;
778 * =========================================================================
779 * Figure out which debugging statements to print
780 * =========================================================================
783 static char *dprintf_string
;
784 static int dprintf_print_all
;
787 dprintf_find_string(const char *string
)
789 char *tmp_str
= dprintf_string
;
790 int len
= strlen(string
);
793 * Find out if this is a string we want to print.
794 * String format: file1.c,function_name1,file2.c,file3.c
797 while (tmp_str
!= NULL
) {
798 if (strncmp(tmp_str
, string
, len
) == 0 &&
799 (tmp_str
[len
] == ',' || tmp_str
[len
] == '\0'))
801 tmp_str
= strchr(tmp_str
, ',');
803 tmp_str
++; /* Get rid of , */
809 dprintf_setup(int *argc
, char **argv
)
814 * Debugging can be specified two ways: by setting the
815 * environment variable ZFS_DEBUG, or by including a
816 * "debug=..." argument on the command line. The command
817 * line setting overrides the environment variable.
820 for (i
= 1; i
< *argc
; i
++) {
821 int len
= strlen("debug=");
822 /* First look for a command line argument */
823 if (strncmp("debug=", argv
[i
], len
) == 0) {
824 dprintf_string
= argv
[i
] + len
;
825 /* Remove from args */
826 for (j
= i
; j
< *argc
; j
++)
833 if (dprintf_string
== NULL
) {
834 /* Look for ZFS_DEBUG environment variable */
835 dprintf_string
= getenv("ZFS_DEBUG");
839 * Are we just turning on all debugging?
841 if (dprintf_find_string("on"))
842 dprintf_print_all
= 1;
846 * =========================================================================
848 * =========================================================================
851 __dprintf(const char *file
, const char *func
, int line
, const char *fmt
, ...)
857 * Get rid of annoying "../common/" prefix to filename.
859 newfile
= strrchr(file
, '/');
860 if (newfile
!= NULL
) {
861 newfile
= newfile
+ 1; /* Get rid of leading / */
866 if (dprintf_print_all
||
867 dprintf_find_string(newfile
) ||
868 dprintf_find_string(func
)) {
869 /* Print out just the function name if requested */
871 if (dprintf_find_string("pid"))
872 (void) printf("%d ", getpid());
873 if (dprintf_find_string("tid"))
874 (void) printf("%u ", (uint_t
) pthread_self());
875 if (dprintf_find_string("cpu"))
876 (void) printf("%u ", getcpuid());
877 if (dprintf_find_string("time"))
878 (void) printf("%llu ", gethrtime());
879 if (dprintf_find_string("long"))
880 (void) printf("%s, line %d: ", newfile
, line
);
881 (void) printf("%s: ", func
);
883 (void) vprintf(fmt
, adx
);
890 * =========================================================================
891 * cmn_err() and panic()
892 * =========================================================================
894 static char ce_prefix
[CE_IGNORE
][10] = { "", "NOTICE: ", "WARNING: ", "" };
895 static char ce_suffix
[CE_IGNORE
][2] = { "", "\n", "\n", "" };
898 vpanic(const char *fmt
, va_list adx
)
900 (void) fprintf(stderr
, "error: ");
901 (void) vfprintf(stderr
, fmt
, adx
);
902 (void) fprintf(stderr
, "\n");
904 abort(); /* think of it as a "user-level crash dump" */
908 panic(const char *fmt
, ...)
918 vcmn_err(int ce
, const char *fmt
, va_list adx
)
922 if (ce
!= CE_NOTE
) { /* suppress noise in userland stress testing */
923 (void) fprintf(stderr
, "%s", ce_prefix
[ce
]);
924 (void) vfprintf(stderr
, fmt
, adx
);
925 (void) fprintf(stderr
, "%s", ce_suffix
[ce
]);
931 cmn_err(int ce
, const char *fmt
, ...)
936 vcmn_err(ce
, fmt
, adx
);
941 * =========================================================================
943 * =========================================================================
946 kobj_open_file(char *name
)
951 /* set vp as the _fd field of the file */
952 if (vn_openat(name
, UIO_SYSSPACE
, FREAD
, 0, &vp
, 0, 0, rootdir
,
954 return ((void *)-1UL);
956 file
= umem_zalloc(sizeof (struct _buf
), UMEM_NOFAIL
);
957 file
->_fd
= (intptr_t)vp
;
962 kobj_read_file(struct _buf
*file
, char *buf
, unsigned size
, unsigned off
)
966 vn_rdwr(UIO_READ
, (vnode_t
*)file
->_fd
, buf
, size
, (offset_t
)off
,
967 UIO_SYSSPACE
, 0, 0, 0, &resid
);
969 return (size
- resid
);
973 kobj_close_file(struct _buf
*file
)
975 vn_close((vnode_t
*)file
->_fd
);
976 umem_free(file
, sizeof (struct _buf
));
980 kobj_get_filesize(struct _buf
*file
, uint64_t *size
)
983 vnode_t
*vp
= (vnode_t
*)file
->_fd
;
985 if (fstat64(vp
->v_fd
, &st
) == -1) {
994 * =========================================================================
996 * =========================================================================
1000 delay(clock_t ticks
)
1002 poll(0, 0, ticks
* (1000 / hz
));
1006 * Find highest one bit set.
1007 * Returns bit number + 1 of highest bit that is set, otherwise returns 0.
1008 * High order bit is 31 (or 63 in _LP64 kernel).
1011 highbit64(uint64_t i
)
1017 if (i
& 0xffffffff00000000ULL
) {
1020 if (i
& 0xffff0000) {
1038 static int random_fd
= -1, urandom_fd
= -1;
1041 random_get_bytes_common(uint8_t *ptr
, size_t len
, int fd
)
1048 while (resid
!= 0) {
1049 bytes
= read(fd
, ptr
, resid
);
1050 ASSERT3S(bytes
, >=, 0);
1059 random_get_bytes(uint8_t *ptr
, size_t len
)
1061 return (random_get_bytes_common(ptr
, len
, random_fd
));
1065 random_get_pseudo_bytes(uint8_t *ptr
, size_t len
)
1067 return (random_get_bytes_common(ptr
, len
, urandom_fd
));
1071 ddi_strtoul(const char *hw_serial
, char **nptr
, int base
, unsigned long *result
)
1075 *result
= strtoul(hw_serial
, &end
, base
);
1082 ddi_strtoull(const char *str
, char **nptr
, int base
, u_longlong_t
*result
)
1086 *result
= strtoull(str
, &end
, base
);
1093 * =========================================================================
1094 * kernel emulation setup & teardown
1095 * =========================================================================
1098 umem_out_of_memory(void)
1100 char errmsg
[] = "out of memory -- generating core dump\n";
1102 (void) fprintf(stderr
, "%s", errmsg
);
1108 kernel_init(int mode
)
1110 extern uint_t rrw_tsd_key
;
1112 umem_nofail_callback(umem_out_of_memory
);
1114 physmem
= sysconf(_SC_PHYS_PAGES
);
1116 dprintf("physmem = %llu pages (%.2f GB)\n", physmem
,
1117 (double)physmem
* sysconf(_SC_PAGE_SIZE
) / (1ULL << 30));
1119 (void) snprintf(hw_serial
, sizeof (hw_serial
), "%ld",
1120 (mode
& FWRITE
) ? gethostid() : 0);
1122 VERIFY((random_fd
= open("/dev/random", O_RDONLY
)) != -1);
1123 VERIFY((urandom_fd
= open("/dev/urandom", O_RDONLY
)) != -1);
1126 system_taskq_init();
1130 tsd_create(&rrw_tsd_key
, rrw_tsd_destroy
);
1138 system_taskq_fini();
1149 crgetuid(cred_t
*cr
)
1155 crgetruid(cred_t
*cr
)
1161 crgetgid(cred_t
*cr
)
1167 crgetngroups(cred_t
*cr
)
1173 crgetgroups(cred_t
*cr
)
1179 zfs_secpolicy_snapshot_perms(const char *name
, cred_t
*cr
)
1185 zfs_secpolicy_rename_perms(const char *from
, const char *to
, cred_t
*cr
)
1191 zfs_secpolicy_destroy_perms(const char *name
, cred_t
*cr
)
1197 ksid_lookupdomain(const char *dom
)
1201 kd
= umem_zalloc(sizeof (ksiddomain_t
), UMEM_NOFAIL
);
1202 kd
->kd_name
= spa_strdup(dom
);
1207 ksiddomain_rele(ksiddomain_t
*ksid
)
1209 spa_strfree(ksid
->kd_name
);
1210 umem_free(ksid
, sizeof (ksiddomain_t
));
1214 kmem_vasprintf(const char *fmt
, va_list adx
)
1219 va_copy(adx_copy
, adx
);
1220 VERIFY(vasprintf(&buf
, fmt
, adx_copy
) != -1);
1227 kmem_asprintf(const char *fmt
, ...)
1233 VERIFY(vasprintf(&buf
, fmt
, adx
) != -1);
1241 zfs_onexit_fd_hold(int fd
, minor_t
*minorp
)
1249 zfs_onexit_fd_rele(int fd
)
1255 zfs_onexit_add_cb(minor_t minor
, void (*func
)(void *), void *data
,
1256 uint64_t *action_handle
)
1263 zfs_onexit_del_cb(minor_t minor
, uint64_t action_handle
, boolean_t fire
)
1270 zfs_onexit_cb_data(minor_t minor
, uint64_t action_handle
, void **data
)