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
];
50 struct utsname hw_utsname
;
52 /* this only exists to have its address taken */
56 * =========================================================================
58 * =========================================================================
61 pthread_cond_t kthread_cond
= PTHREAD_COND_INITIALIZER
;
62 pthread_mutex_t kthread_lock
= PTHREAD_MUTEX_INITIALIZER
;
63 pthread_key_t kthread_key
;
71 VERIFY3S(pthread_key_create(&kthread_key
, NULL
), ==, 0);
73 /* Create entry for primary kthread */
74 kt
= umem_zalloc(sizeof (kthread_t
), UMEM_NOFAIL
);
75 kt
->t_tid
= pthread_self();
78 VERIFY3S(pthread_setspecific(kthread_key
, kt
), ==, 0);
80 /* Only the main thread should be running at the moment */
81 ASSERT3S(kthread_nr
, ==, 0);
88 kthread_t
*kt
= curthread
;
90 ASSERT(pthread_equal(kt
->t_tid
, pthread_self()));
91 ASSERT3P(kt
->t_func
, ==, NULL
);
93 umem_free(kt
, sizeof (kthread_t
));
95 /* Wait for all threads to exit via thread_exit() */
96 VERIFY3S(pthread_mutex_lock(&kthread_lock
), ==, 0);
98 kthread_nr
--; /* Main thread is exiting */
100 while (kthread_nr
> 0)
101 VERIFY3S(pthread_cond_wait(&kthread_cond
, &kthread_lock
), ==,
104 ASSERT3S(kthread_nr
, ==, 0);
105 VERIFY3S(pthread_mutex_unlock(&kthread_lock
), ==, 0);
107 VERIFY3S(pthread_key_delete(kthread_key
), ==, 0);
111 zk_thread_current(void)
113 kthread_t
*kt
= pthread_getspecific(kthread_key
);
115 ASSERT3P(kt
, !=, NULL
);
121 zk_thread_helper(void *arg
)
123 kthread_t
*kt
= (kthread_t
*) arg
;
125 VERIFY3S(pthread_setspecific(kthread_key
, kt
), ==, 0);
127 VERIFY3S(pthread_mutex_lock(&kthread_lock
), ==, 0);
129 VERIFY3S(pthread_mutex_unlock(&kthread_lock
), ==, 0);
131 kt
->t_tid
= pthread_self();
132 ((thread_func_arg_t
) kt
->t_func
)(kt
->t_arg
);
134 /* Unreachable, thread must exit with thread_exit() */
141 zk_thread_create(caddr_t stk
, size_t stksize
, thread_func_t func
, void *arg
,
142 size_t len
, proc_t
*pp
, int state
, pri_t pri
, int detachstate
)
148 ASSERT0(state
& ~TS_RUN
);
150 kt
= umem_zalloc(sizeof (kthread_t
), UMEM_NOFAIL
);
154 VERIFY0(pthread_attr_init(&attr
));
155 VERIFY0(pthread_attr_setdetachstate(&attr
, detachstate
));
158 * We allow the default stack size in user space to be specified by
159 * setting the ZFS_STACK_SIZE environment variable. This allows us
160 * the convenience of observing and debugging stack overruns in
161 * user space. Explicitly specified stack sizes will be honored.
162 * The usage of ZFS_STACK_SIZE is discussed further in the
163 * ENVIRONMENT VARIABLES sections of the ztest(1) man page.
166 stkstr
= getenv("ZFS_STACK_SIZE");
169 stksize
= TS_STACK_MAX
;
171 stksize
= MAX(atoi(stkstr
), TS_STACK_MIN
);
174 VERIFY3S(stksize
, >, 0);
175 stksize
= P2ROUNDUP(MAX(stksize
, TS_STACK_MIN
), PAGESIZE
);
176 VERIFY0(pthread_attr_setstacksize(&attr
, stksize
));
177 VERIFY0(pthread_attr_setguardsize(&attr
, PAGESIZE
));
179 VERIFY0(pthread_create(&kt
->t_tid
, &attr
, &zk_thread_helper
, kt
));
180 VERIFY0(pthread_attr_destroy(&attr
));
188 kthread_t
*kt
= curthread
;
190 ASSERT(pthread_equal(kt
->t_tid
, pthread_self()));
192 umem_free(kt
, sizeof (kthread_t
));
194 pthread_mutex_lock(&kthread_lock
);
196 pthread_mutex_unlock(&kthread_lock
);
198 pthread_cond_broadcast(&kthread_cond
);
199 pthread_exit((void *)TS_MAGIC
);
203 zk_thread_join(kt_did_t tid
)
207 pthread_join((pthread_t
)tid
, &ret
);
208 VERIFY3P(ret
, ==, (void *)TS_MAGIC
);
212 * =========================================================================
214 * =========================================================================
218 kstat_create(const char *module
, int instance
, const char *name
,
219 const char *class, uchar_t type
, ulong_t ndata
, uchar_t ks_flag
)
226 kstat_install(kstat_t
*ksp
)
231 kstat_delete(kstat_t
*ksp
)
236 kstat_waitq_enter(kstat_io_t
*kiop
)
241 kstat_waitq_exit(kstat_io_t
*kiop
)
246 kstat_runq_enter(kstat_io_t
*kiop
)
251 kstat_runq_exit(kstat_io_t
*kiop
)
256 kstat_waitq_to_runq(kstat_io_t
*kiop
)
261 kstat_runq_back_to_waitq(kstat_io_t
*kiop
)
265 kstat_set_raw_ops(kstat_t
*ksp
,
266 int (*headers
)(char *buf
, size_t size
),
267 int (*data
)(char *buf
, size_t size
, void *data
),
268 void *(*addr
)(kstat_t
*ksp
, loff_t index
))
272 * =========================================================================
274 * =========================================================================
278 mutex_init(kmutex_t
*mp
, char *name
, int type
, void *cookie
)
280 ASSERT3S(type
, ==, MUTEX_DEFAULT
);
281 ASSERT3P(cookie
, ==, NULL
);
282 mp
->m_owner
= MTX_INIT
;
283 mp
->m_magic
= MTX_MAGIC
;
284 VERIFY3S(pthread_mutex_init(&mp
->m_lock
, NULL
), ==, 0);
288 mutex_destroy(kmutex_t
*mp
)
290 ASSERT3U(mp
->m_magic
, ==, MTX_MAGIC
);
291 ASSERT3P(mp
->m_owner
, ==, MTX_INIT
);
292 ASSERT0(pthread_mutex_destroy(&(mp
)->m_lock
));
293 mp
->m_owner
= MTX_DEST
;
298 mutex_enter(kmutex_t
*mp
)
300 ASSERT3U(mp
->m_magic
, ==, MTX_MAGIC
);
301 ASSERT3P(mp
->m_owner
, !=, MTX_DEST
);
302 ASSERT3P(mp
->m_owner
, !=, curthread
);
303 VERIFY3S(pthread_mutex_lock(&mp
->m_lock
), ==, 0);
304 ASSERT3P(mp
->m_owner
, ==, MTX_INIT
);
305 mp
->m_owner
= curthread
;
309 mutex_tryenter(kmutex_t
*mp
)
311 ASSERT3U(mp
->m_magic
, ==, MTX_MAGIC
);
312 ASSERT3P(mp
->m_owner
, !=, MTX_DEST
);
313 if (0 == pthread_mutex_trylock(&mp
->m_lock
)) {
314 ASSERT3P(mp
->m_owner
, ==, MTX_INIT
);
315 mp
->m_owner
= curthread
;
323 mutex_exit(kmutex_t
*mp
)
325 ASSERT3U(mp
->m_magic
, ==, MTX_MAGIC
);
326 ASSERT3P(mutex_owner(mp
), ==, curthread
);
327 mp
->m_owner
= MTX_INIT
;
328 VERIFY3S(pthread_mutex_unlock(&mp
->m_lock
), ==, 0);
332 mutex_owner(kmutex_t
*mp
)
334 ASSERT3U(mp
->m_magic
, ==, MTX_MAGIC
);
335 return (mp
->m_owner
);
339 mutex_held(kmutex_t
*mp
)
341 return (mp
->m_owner
== curthread
);
345 * =========================================================================
347 * =========================================================================
351 rw_init(krwlock_t
*rwlp
, char *name
, int type
, void *arg
)
353 ASSERT3S(type
, ==, RW_DEFAULT
);
354 ASSERT3P(arg
, ==, NULL
);
355 VERIFY3S(pthread_rwlock_init(&rwlp
->rw_lock
, NULL
), ==, 0);
356 rwlp
->rw_owner
= RW_INIT
;
357 rwlp
->rw_wr_owner
= RW_INIT
;
358 rwlp
->rw_readers
= 0;
359 rwlp
->rw_magic
= RW_MAGIC
;
363 rw_destroy(krwlock_t
*rwlp
)
365 ASSERT3U(rwlp
->rw_magic
, ==, RW_MAGIC
);
366 ASSERT(rwlp
->rw_readers
== 0 && rwlp
->rw_wr_owner
== RW_INIT
);
367 VERIFY3S(pthread_rwlock_destroy(&rwlp
->rw_lock
), ==, 0);
372 rw_enter(krwlock_t
*rwlp
, krw_t rw
)
374 ASSERT3U(rwlp
->rw_magic
, ==, RW_MAGIC
);
375 ASSERT3P(rwlp
->rw_owner
, !=, curthread
);
376 ASSERT3P(rwlp
->rw_wr_owner
, !=, curthread
);
378 if (rw
== RW_READER
) {
379 VERIFY3S(pthread_rwlock_rdlock(&rwlp
->rw_lock
), ==, 0);
380 ASSERT3P(rwlp
->rw_wr_owner
, ==, RW_INIT
);
382 atomic_inc_uint(&rwlp
->rw_readers
);
384 VERIFY3S(pthread_rwlock_wrlock(&rwlp
->rw_lock
), ==, 0);
385 ASSERT3P(rwlp
->rw_wr_owner
, ==, RW_INIT
);
386 ASSERT3U(rwlp
->rw_readers
, ==, 0);
388 rwlp
->rw_wr_owner
= curthread
;
391 rwlp
->rw_owner
= curthread
;
395 rw_exit(krwlock_t
*rwlp
)
397 ASSERT3U(rwlp
->rw_magic
, ==, RW_MAGIC
);
398 ASSERT(RW_LOCK_HELD(rwlp
));
400 if (RW_READ_HELD(rwlp
))
401 atomic_dec_uint(&rwlp
->rw_readers
);
403 rwlp
->rw_wr_owner
= RW_INIT
;
405 rwlp
->rw_owner
= RW_INIT
;
406 VERIFY3S(pthread_rwlock_unlock(&rwlp
->rw_lock
), ==, 0);
410 rw_tryenter(krwlock_t
*rwlp
, krw_t rw
)
414 ASSERT3U(rwlp
->rw_magic
, ==, RW_MAGIC
);
417 rv
= pthread_rwlock_tryrdlock(&rwlp
->rw_lock
);
419 rv
= pthread_rwlock_trywrlock(&rwlp
->rw_lock
);
422 ASSERT3P(rwlp
->rw_wr_owner
, ==, RW_INIT
);
425 atomic_inc_uint(&rwlp
->rw_readers
);
427 ASSERT3U(rwlp
->rw_readers
, ==, 0);
428 rwlp
->rw_wr_owner
= curthread
;
431 rwlp
->rw_owner
= curthread
;
435 VERIFY3S(rv
, ==, EBUSY
);
441 rw_tryupgrade(krwlock_t
*rwlp
)
443 ASSERT3U(rwlp
->rw_magic
, ==, RW_MAGIC
);
449 * =========================================================================
450 * condition variables
451 * =========================================================================
455 cv_init(kcondvar_t
*cv
, char *name
, int type
, void *arg
)
457 ASSERT3S(type
, ==, CV_DEFAULT
);
458 cv
->cv_magic
= CV_MAGIC
;
459 VERIFY3S(pthread_cond_init(&cv
->cv
, NULL
), ==, 0);
463 cv_destroy(kcondvar_t
*cv
)
465 ASSERT3U(cv
->cv_magic
, ==, CV_MAGIC
);
466 VERIFY3S(pthread_cond_destroy(&cv
->cv
), ==, 0);
471 cv_wait(kcondvar_t
*cv
, kmutex_t
*mp
)
473 ASSERT3U(cv
->cv_magic
, ==, CV_MAGIC
);
474 ASSERT3P(mutex_owner(mp
), ==, curthread
);
475 mp
->m_owner
= MTX_INIT
;
476 int ret
= pthread_cond_wait(&cv
->cv
, &mp
->m_lock
);
478 VERIFY3S(ret
, ==, EINTR
);
479 mp
->m_owner
= curthread
;
483 cv_timedwait(kcondvar_t
*cv
, kmutex_t
*mp
, clock_t abstime
)
490 ASSERT3U(cv
->cv_magic
, ==, CV_MAGIC
);
493 delta
= abstime
- ddi_get_lbolt();
497 VERIFY(gettimeofday(&tv
, NULL
) == 0);
499 ts
.tv_sec
= tv
.tv_sec
+ delta
/ hz
;
500 ts
.tv_nsec
= tv
.tv_usec
* 1000 + (delta
% hz
) * (NANOSEC
/ hz
);
501 if (ts
.tv_nsec
>= NANOSEC
) {
503 ts
.tv_nsec
-= NANOSEC
;
506 ASSERT3P(mutex_owner(mp
), ==, curthread
);
507 mp
->m_owner
= MTX_INIT
;
508 error
= pthread_cond_timedwait(&cv
->cv
, &mp
->m_lock
, &ts
);
509 mp
->m_owner
= curthread
;
511 if (error
== ETIMEDOUT
)
517 VERIFY3S(error
, ==, 0);
524 cv_timedwait_hires(kcondvar_t
*cv
, kmutex_t
*mp
, hrtime_t tim
, hrtime_t res
,
534 delta
= tim
- gethrtime();
538 ts
.tv_sec
= delta
/ NANOSEC
;
539 ts
.tv_nsec
= delta
% NANOSEC
;
541 ASSERT(mutex_owner(mp
) == curthread
);
543 error
= pthread_cond_timedwait(&cv
->cv
, &mp
->m_lock
, &ts
);
544 mp
->m_owner
= curthread
;
558 cv_signal(kcondvar_t
*cv
)
560 ASSERT3U(cv
->cv_magic
, ==, CV_MAGIC
);
561 VERIFY3S(pthread_cond_signal(&cv
->cv
), ==, 0);
565 cv_broadcast(kcondvar_t
*cv
)
567 ASSERT3U(cv
->cv_magic
, ==, CV_MAGIC
);
568 VERIFY3S(pthread_cond_broadcast(&cv
->cv
), ==, 0);
572 * =========================================================================
574 * =========================================================================
577 * Note: for the xxxat() versions of these functions, we assume that the
578 * starting vp is always rootdir (which is true for spa_directory.c, the only
579 * ZFS consumer of these interfaces). We assert this is true, and then emulate
580 * them by adding '/' in front of the path.
585 vn_open(char *path
, int x1
, int flags
, int mode
, vnode_t
**vpp
, int x2
, int x3
)
594 realpath
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
597 * If we're accessing a real disk from userland, we need to use
598 * the character interface to avoid caching. This is particularly
599 * important if we're trying to look at a real in-kernel storage
600 * pool from userland, e.g. via zdb, because otherwise we won't
601 * see the changes occurring under the segmap cache.
602 * On the other hand, the stupid character device returns zero
603 * for its size. So -- gag -- we open the block device to get
604 * its size, and remember it for subsequent VOP_GETATTR().
606 #if defined(__sun__) || defined(__sun)
607 if (strncmp(path
, "/dev/", 5) == 0) {
612 fd
= open64(path
, O_RDONLY
);
618 if (fstat64(fd
, &st
) == -1) {
625 (void) sprintf(realpath
, "%s", path
);
626 dsk
= strstr(path
, "/dsk/");
628 (void) sprintf(realpath
+ (dsk
- path
) + 1, "r%s",
631 (void) sprintf(realpath
, "%s", path
);
632 if (!(flags
& FCREAT
) && stat64(realpath
, &st
) == -1) {
639 if (!(flags
& FCREAT
) && S_ISBLK(st
.st_mode
)) {
643 /* We shouldn't be writing to block devices in userspace */
644 VERIFY(!(flags
& FWRITE
));
648 old_umask
= umask(0);
651 * The construct 'flags - FREAD' conveniently maps combinations of
652 * FREAD and FWRITE to the corresponding O_RDONLY, O_WRONLY, and O_RDWR.
654 fd
= open64(realpath
, flags
- FREAD
, mode
);
658 (void) umask(old_umask
);
663 if (fstat64_blk(fd
, &st
) == -1) {
669 (void) fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
671 *vpp
= vp
= umem_zalloc(sizeof (vnode_t
), UMEM_NOFAIL
);
674 vp
->v_size
= st
.st_size
;
675 vp
->v_path
= spa_strdup(path
);
682 vn_openat(char *path
, int x1
, int flags
, int mode
, vnode_t
**vpp
, int x2
,
683 int x3
, vnode_t
*startvp
, int fd
)
685 char *realpath
= umem_alloc(strlen(path
) + 2, UMEM_NOFAIL
);
688 ASSERT(startvp
== rootdir
);
689 (void) sprintf(realpath
, "/%s", path
);
691 /* fd ignored for now, need if want to simulate nbmand support */
692 ret
= vn_open(realpath
, x1
, flags
, mode
, vpp
, x2
, x3
);
694 umem_free(realpath
, strlen(path
) + 2);
701 vn_rdwr(int uio
, vnode_t
*vp
, void *addr
, ssize_t len
, offset_t offset
,
702 int x1
, int x2
, rlim64_t x3
, void *x4
, ssize_t
*residp
)
704 ssize_t rc
, done
= 0, split
;
706 if (uio
== UIO_READ
) {
707 rc
= pread64(vp
->v_fd
, addr
, len
, offset
);
710 * To simulate partial disk writes, we split writes into two
711 * system calls so that the process can be killed in between.
713 int sectors
= len
>> SPA_MINBLOCKSHIFT
;
714 split
= (sectors
> 0 ? rand() % sectors
: 0) <<
716 rc
= pwrite64(vp
->v_fd
, addr
, split
, offset
);
719 rc
= pwrite64(vp
->v_fd
, (char *)addr
+ split
,
720 len
- split
, offset
+ split
);
725 if (rc
== -1 && errno
== EINVAL
) {
727 * Under Linux, this most likely means an alignment issue
728 * (memory or disk) due to O_DIRECT, so we abort() in order to
729 * catch the offender.
740 *residp
= len
- done
;
741 else if (done
!= len
)
747 vn_close(vnode_t
*vp
)
750 spa_strfree(vp
->v_path
);
751 umem_free(vp
, sizeof (vnode_t
));
755 * At a minimum we need to update the size since vdev_reopen()
756 * will no longer call vn_openat().
759 fop_getattr(vnode_t
*vp
, vattr_t
*vap
)
764 if (fstat64_blk(vp
->v_fd
, &st
) == -1) {
770 vap
->va_size
= st
.st_size
;
775 * =========================================================================
776 * Figure out which debugging statements to print
777 * =========================================================================
780 static char *dprintf_string
;
781 static int dprintf_print_all
;
784 dprintf_find_string(const char *string
)
786 char *tmp_str
= dprintf_string
;
787 int len
= strlen(string
);
790 * Find out if this is a string we want to print.
791 * String format: file1.c,function_name1,file2.c,file3.c
794 while (tmp_str
!= NULL
) {
795 if (strncmp(tmp_str
, string
, len
) == 0 &&
796 (tmp_str
[len
] == ',' || tmp_str
[len
] == '\0'))
798 tmp_str
= strchr(tmp_str
, ',');
800 tmp_str
++; /* Get rid of , */
806 dprintf_setup(int *argc
, char **argv
)
811 * Debugging can be specified two ways: by setting the
812 * environment variable ZFS_DEBUG, or by including a
813 * "debug=..." argument on the command line. The command
814 * line setting overrides the environment variable.
817 for (i
= 1; i
< *argc
; i
++) {
818 int len
= strlen("debug=");
819 /* First look for a command line argument */
820 if (strncmp("debug=", argv
[i
], len
) == 0) {
821 dprintf_string
= argv
[i
] + len
;
822 /* Remove from args */
823 for (j
= i
; j
< *argc
; j
++)
830 if (dprintf_string
== NULL
) {
831 /* Look for ZFS_DEBUG environment variable */
832 dprintf_string
= getenv("ZFS_DEBUG");
836 * Are we just turning on all debugging?
838 if (dprintf_find_string("on"))
839 dprintf_print_all
= 1;
843 * =========================================================================
845 * =========================================================================
848 __dprintf(const char *file
, const char *func
, int line
, const char *fmt
, ...)
854 * Get rid of annoying "../common/" prefix to filename.
856 newfile
= strrchr(file
, '/');
857 if (newfile
!= NULL
) {
858 newfile
= newfile
+ 1; /* Get rid of leading / */
863 if (dprintf_print_all
||
864 dprintf_find_string(newfile
) ||
865 dprintf_find_string(func
)) {
866 /* Print out just the function name if requested */
868 if (dprintf_find_string("pid"))
869 (void) printf("%d ", getpid());
870 if (dprintf_find_string("tid"))
871 (void) printf("%u ", (uint_t
) pthread_self());
872 if (dprintf_find_string("cpu"))
873 (void) printf("%u ", getcpuid());
874 if (dprintf_find_string("time"))
875 (void) printf("%llu ", gethrtime());
876 if (dprintf_find_string("long"))
877 (void) printf("%s, line %d: ", newfile
, line
);
878 (void) printf("%s: ", func
);
880 (void) vprintf(fmt
, adx
);
887 * =========================================================================
888 * cmn_err() and panic()
889 * =========================================================================
891 static char ce_prefix
[CE_IGNORE
][10] = { "", "NOTICE: ", "WARNING: ", "" };
892 static char ce_suffix
[CE_IGNORE
][2] = { "", "\n", "\n", "" };
895 vpanic(const char *fmt
, va_list adx
)
897 (void) fprintf(stderr
, "error: ");
898 (void) vfprintf(stderr
, fmt
, adx
);
899 (void) fprintf(stderr
, "\n");
901 abort(); /* think of it as a "user-level crash dump" */
905 panic(const char *fmt
, ...)
915 vcmn_err(int ce
, const char *fmt
, va_list adx
)
919 if (ce
!= CE_NOTE
) { /* suppress noise in userland stress testing */
920 (void) fprintf(stderr
, "%s", ce_prefix
[ce
]);
921 (void) vfprintf(stderr
, fmt
, adx
);
922 (void) fprintf(stderr
, "%s", ce_suffix
[ce
]);
928 cmn_err(int ce
, const char *fmt
, ...)
933 vcmn_err(ce
, fmt
, adx
);
938 * =========================================================================
940 * =========================================================================
943 kobj_open_file(char *name
)
948 /* set vp as the _fd field of the file */
949 if (vn_openat(name
, UIO_SYSSPACE
, FREAD
, 0, &vp
, 0, 0, rootdir
,
951 return ((void *)-1UL);
953 file
= umem_zalloc(sizeof (struct _buf
), UMEM_NOFAIL
);
954 file
->_fd
= (intptr_t)vp
;
959 kobj_read_file(struct _buf
*file
, char *buf
, unsigned size
, unsigned off
)
963 vn_rdwr(UIO_READ
, (vnode_t
*)file
->_fd
, buf
, size
, (offset_t
)off
,
964 UIO_SYSSPACE
, 0, 0, 0, &resid
);
966 return (size
- resid
);
970 kobj_close_file(struct _buf
*file
)
972 vn_close((vnode_t
*)file
->_fd
);
973 umem_free(file
, sizeof (struct _buf
));
977 kobj_get_filesize(struct _buf
*file
, uint64_t *size
)
980 vnode_t
*vp
= (vnode_t
*)file
->_fd
;
982 if (fstat64(vp
->v_fd
, &st
) == -1) {
991 * =========================================================================
993 * =========================================================================
999 poll(0, 0, ticks
* (1000 / hz
));
1003 * Find highest one bit set.
1004 * Returns bit number + 1 of highest bit that is set, otherwise returns 0.
1005 * High order bit is 31 (or 63 in _LP64 kernel).
1008 highbit64(uint64_t i
)
1014 if (i
& 0xffffffff00000000ULL
) {
1017 if (i
& 0xffff0000) {
1035 static int random_fd
= -1, urandom_fd
= -1;
1038 random_get_bytes_common(uint8_t *ptr
, size_t len
, int fd
)
1045 while (resid
!= 0) {
1046 bytes
= read(fd
, ptr
, resid
);
1047 ASSERT3S(bytes
, >=, 0);
1056 random_get_bytes(uint8_t *ptr
, size_t len
)
1058 return (random_get_bytes_common(ptr
, len
, random_fd
));
1062 random_get_pseudo_bytes(uint8_t *ptr
, size_t len
)
1064 return (random_get_bytes_common(ptr
, len
, urandom_fd
));
1068 ddi_strtoul(const char *hw_serial
, char **nptr
, int base
, unsigned long *result
)
1072 *result
= strtoul(hw_serial
, &end
, base
);
1079 ddi_strtoull(const char *str
, char **nptr
, int base
, u_longlong_t
*result
)
1083 *result
= strtoull(str
, &end
, base
);
1092 return (&hw_utsname
);
1096 * =========================================================================
1097 * kernel emulation setup & teardown
1098 * =========================================================================
1101 umem_out_of_memory(void)
1103 char errmsg
[] = "out of memory -- generating core dump\n";
1105 (void) fprintf(stderr
, "%s", errmsg
);
1110 static unsigned long
1111 get_spl_hostid(void)
1114 unsigned long hostid
;
1116 f
= fopen("/sys/module/spl/parameters/spl_hostid", "r");
1119 if (fscanf(f
, "%lu", &hostid
) != 1)
1122 return (hostid
& 0xffffffff);
1126 get_system_hostid(void)
1128 unsigned long system_hostid
= get_spl_hostid();
1129 if (system_hostid
== 0)
1130 system_hostid
= gethostid() & 0xffffffff;
1131 return (system_hostid
);
1135 kernel_init(int mode
)
1137 extern uint_t rrw_tsd_key
;
1139 umem_nofail_callback(umem_out_of_memory
);
1141 physmem
= sysconf(_SC_PHYS_PAGES
);
1143 dprintf("physmem = %llu pages (%.2f GB)\n", physmem
,
1144 (double)physmem
* sysconf(_SC_PAGE_SIZE
) / (1ULL << 30));
1146 (void) snprintf(hw_serial
, sizeof (hw_serial
), "%ld",
1147 (mode
& FWRITE
) ? get_system_hostid() : 0);
1149 VERIFY((random_fd
= open("/dev/random", O_RDONLY
)) != -1);
1150 VERIFY((urandom_fd
= open("/dev/urandom", O_RDONLY
)) != -1);
1151 VERIFY0(uname(&hw_utsname
));
1154 system_taskq_init();
1158 tsd_create(&rrw_tsd_key
, rrw_tsd_destroy
);
1166 system_taskq_fini();
1177 crgetuid(cred_t
*cr
)
1183 crgetruid(cred_t
*cr
)
1189 crgetgid(cred_t
*cr
)
1195 crgetngroups(cred_t
*cr
)
1201 crgetgroups(cred_t
*cr
)
1207 zfs_secpolicy_snapshot_perms(const char *name
, cred_t
*cr
)
1213 zfs_secpolicy_rename_perms(const char *from
, const char *to
, cred_t
*cr
)
1219 zfs_secpolicy_destroy_perms(const char *name
, cred_t
*cr
)
1225 ksid_lookupdomain(const char *dom
)
1229 kd
= umem_zalloc(sizeof (ksiddomain_t
), UMEM_NOFAIL
);
1230 kd
->kd_name
= spa_strdup(dom
);
1235 ksiddomain_rele(ksiddomain_t
*ksid
)
1237 spa_strfree(ksid
->kd_name
);
1238 umem_free(ksid
, sizeof (ksiddomain_t
));
1242 kmem_vasprintf(const char *fmt
, va_list adx
)
1247 va_copy(adx_copy
, adx
);
1248 VERIFY(vasprintf(&buf
, fmt
, adx_copy
) != -1);
1255 kmem_asprintf(const char *fmt
, ...)
1261 VERIFY(vasprintf(&buf
, fmt
, adx
) != -1);
1269 zfs_onexit_fd_hold(int fd
, minor_t
*minorp
)
1277 zfs_onexit_fd_rele(int fd
)
1283 zfs_onexit_add_cb(minor_t minor
, void (*func
)(void *), void *data
,
1284 uint64_t *action_handle
)
1291 zfs_onexit_del_cb(minor_t minor
, uint64_t action_handle
, boolean_t fire
)
1298 zfs_onexit_cb_data(minor_t minor
, uint64_t action_handle
, void **data
)
1304 spl_fstrans_mark(void)
1306 return ((fstrans_cookie_t
) 0);
1310 spl_fstrans_unmark(fstrans_cookie_t cookie
)
1315 spl_fstrans_check(void)