]>
git.proxmox.com Git - mirror_zfs-debian.git/blob - lib/libzpool/kernel.c
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 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
35 #include <sys/processor.h>
36 #include <sys/zfs_context.h>
38 #include <sys/utsname.h>
41 * Emulation of kernel services in userland.
45 vnode_t
*rootdir
= (vnode_t
*)0xabcd1234;
48 struct utsname utsname
= {
49 "userland", "libzpool", "1", "1", "na"
53 * =========================================================================
55 * =========================================================================
59 zk_thread_create(void (*func
)(), void *arg
)
63 VERIFY(thr_create(0, 0, (void *(*)(void *))func
, arg
, THR_DETACHED
,
66 return ((void *)(uintptr_t)tid
);
70 * =========================================================================
72 * =========================================================================
76 kstat_create(char *module
, int instance
, char *name
, char *class,
77 uchar_t type
, ulong_t ndata
, uchar_t ks_flag
)
84 kstat_install(kstat_t
*ksp
)
89 kstat_delete(kstat_t
*ksp
)
93 * =========================================================================
95 * =========================================================================
98 zmutex_init(kmutex_t
*mp
)
101 mp
->initialized
= B_TRUE
;
102 (void) _mutex_init(&mp
->m_lock
, USYNC_THREAD
, NULL
);
106 zmutex_destroy(kmutex_t
*mp
)
108 ASSERT(mp
->initialized
== B_TRUE
);
109 ASSERT(mp
->m_owner
== NULL
);
110 (void) _mutex_destroy(&(mp
)->m_lock
);
111 mp
->m_owner
= (void *)-1UL;
112 mp
->initialized
= B_FALSE
;
116 mutex_enter(kmutex_t
*mp
)
118 ASSERT(mp
->initialized
== B_TRUE
);
119 ASSERT(mp
->m_owner
!= (void *)-1UL);
120 ASSERT(mp
->m_owner
!= curthread
);
121 VERIFY(mutex_lock(&mp
->m_lock
) == 0);
122 ASSERT(mp
->m_owner
== NULL
);
123 mp
->m_owner
= curthread
;
127 mutex_tryenter(kmutex_t
*mp
)
129 ASSERT(mp
->initialized
== B_TRUE
);
130 ASSERT(mp
->m_owner
!= (void *)-1UL);
131 if (0 == mutex_trylock(&mp
->m_lock
)) {
132 ASSERT(mp
->m_owner
== NULL
);
133 mp
->m_owner
= curthread
;
141 mutex_exit(kmutex_t
*mp
)
143 ASSERT(mp
->initialized
== B_TRUE
);
144 ASSERT(mutex_owner(mp
) == curthread
);
146 VERIFY(mutex_unlock(&mp
->m_lock
) == 0);
150 mutex_owner(kmutex_t
*mp
)
152 ASSERT(mp
->initialized
== B_TRUE
);
153 return (mp
->m_owner
);
157 * =========================================================================
159 * =========================================================================
163 rw_init(krwlock_t
*rwlp
, char *name
, int type
, void *arg
)
165 rwlock_init(&rwlp
->rw_lock
, USYNC_THREAD
, NULL
);
166 rwlp
->rw_owner
= NULL
;
167 rwlp
->initialized
= B_TRUE
;
171 rw_destroy(krwlock_t
*rwlp
)
173 rwlock_destroy(&rwlp
->rw_lock
);
174 rwlp
->rw_owner
= (void *)-1UL;
175 rwlp
->initialized
= B_FALSE
;
179 rw_enter(krwlock_t
*rwlp
, krw_t rw
)
181 ASSERT(!RW_LOCK_HELD(rwlp
));
182 ASSERT(rwlp
->initialized
== B_TRUE
);
183 ASSERT(rwlp
->rw_owner
!= (void *)-1UL);
184 ASSERT(rwlp
->rw_owner
!= curthread
);
187 VERIFY(rw_rdlock(&rwlp
->rw_lock
) == 0);
189 VERIFY(rw_wrlock(&rwlp
->rw_lock
) == 0);
191 rwlp
->rw_owner
= curthread
;
195 rw_exit(krwlock_t
*rwlp
)
197 ASSERT(rwlp
->initialized
== B_TRUE
);
198 ASSERT(rwlp
->rw_owner
!= (void *)-1UL);
200 rwlp
->rw_owner
= NULL
;
201 VERIFY(rw_unlock(&rwlp
->rw_lock
) == 0);
205 rw_tryenter(krwlock_t
*rwlp
, krw_t rw
)
209 ASSERT(rwlp
->initialized
== B_TRUE
);
210 ASSERT(rwlp
->rw_owner
!= (void *)-1UL);
213 rv
= rw_tryrdlock(&rwlp
->rw_lock
);
215 rv
= rw_trywrlock(&rwlp
->rw_lock
);
218 rwlp
->rw_owner
= curthread
;
227 rw_tryupgrade(krwlock_t
*rwlp
)
229 ASSERT(rwlp
->initialized
== B_TRUE
);
230 ASSERT(rwlp
->rw_owner
!= (void *)-1UL);
236 * =========================================================================
237 * condition variables
238 * =========================================================================
242 cv_init(kcondvar_t
*cv
, char *name
, int type
, void *arg
)
244 VERIFY(cond_init(cv
, type
, NULL
) == 0);
248 cv_destroy(kcondvar_t
*cv
)
250 VERIFY(cond_destroy(cv
) == 0);
254 cv_wait(kcondvar_t
*cv
, kmutex_t
*mp
)
256 ASSERT(mutex_owner(mp
) == curthread
);
258 int ret
= cond_wait(cv
, &mp
->m_lock
);
259 VERIFY(ret
== 0 || ret
== EINTR
);
260 mp
->m_owner
= curthread
;
264 cv_timedwait(kcondvar_t
*cv
, kmutex_t
*mp
, clock_t abstime
)
271 delta
= abstime
- lbolt
;
275 ts
.tv_sec
= delta
/ hz
;
276 ts
.tv_nsec
= (delta
% hz
) * (NANOSEC
/ hz
);
278 ASSERT(mutex_owner(mp
) == curthread
);
280 error
= cond_reltimedwait(cv
, &mp
->m_lock
, &ts
);
281 mp
->m_owner
= curthread
;
295 cv_signal(kcondvar_t
*cv
)
297 VERIFY(cond_signal(cv
) == 0);
301 cv_broadcast(kcondvar_t
*cv
)
303 VERIFY(cond_broadcast(cv
) == 0);
307 * =========================================================================
309 * =========================================================================
312 * Note: for the xxxat() versions of these functions, we assume that the
313 * starting vp is always rootdir (which is true for spa_directory.c, the only
314 * ZFS consumer of these interfaces). We assert this is true, and then emulate
315 * them by adding '/' in front of the path.
320 vn_open(char *path
, int x1
, int flags
, int mode
, vnode_t
**vpp
, int x2
, int x3
)
325 char realpath
[MAXPATHLEN
];
329 * If we're accessing a real disk from userland, we need to use
330 * the character interface to avoid caching. This is particularly
331 * important if we're trying to look at a real in-kernel storage
332 * pool from userland, e.g. via zdb, because otherwise we won't
333 * see the changes occurring under the segmap cache.
334 * On the other hand, the stupid character device returns zero
335 * for its size. So -- gag -- we open the block device to get
336 * its size, and remember it for subsequent VOP_GETATTR().
338 if (strncmp(path
, "/dev/", 5) == 0) {
340 fd
= open64(path
, O_RDONLY
);
343 if (fstat64(fd
, &st
) == -1) {
348 (void) sprintf(realpath
, "%s", path
);
349 dsk
= strstr(path
, "/dsk/");
351 (void) sprintf(realpath
+ (dsk
- path
) + 1, "r%s",
354 (void) sprintf(realpath
, "%s", path
);
355 if (!(flags
& FCREAT
) && stat64(realpath
, &st
) == -1)
360 old_umask
= umask(0);
363 * The construct 'flags - FREAD' conveniently maps combinations of
364 * FREAD and FWRITE to the corresponding O_RDONLY, O_WRONLY, and O_RDWR.
366 fd
= open64(realpath
, flags
- FREAD
, mode
);
369 (void) umask(old_umask
);
374 if (fstat64(fd
, &st
) == -1) {
379 (void) fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
381 *vpp
= vp
= umem_zalloc(sizeof (vnode_t
), UMEM_NOFAIL
);
384 vp
->v_size
= st
.st_size
;
385 vp
->v_path
= spa_strdup(path
);
392 vn_openat(char *path
, int x1
, int flags
, int mode
, vnode_t
**vpp
, int x2
,
393 int x3
, vnode_t
*startvp
, int fd
)
395 char *realpath
= umem_alloc(strlen(path
) + 2, UMEM_NOFAIL
);
398 ASSERT(startvp
== rootdir
);
399 (void) sprintf(realpath
, "/%s", path
);
401 /* fd ignored for now, need if want to simulate nbmand support */
402 ret
= vn_open(realpath
, x1
, flags
, mode
, vpp
, x2
, x3
);
404 umem_free(realpath
, strlen(path
) + 2);
411 vn_rdwr(int uio
, vnode_t
*vp
, void *addr
, ssize_t len
, offset_t offset
,
412 int x1
, int x2
, rlim64_t x3
, void *x4
, ssize_t
*residp
)
414 ssize_t iolen
, split
;
416 if (uio
== UIO_READ
) {
417 iolen
= pread64(vp
->v_fd
, addr
, len
, offset
);
420 * To simulate partial disk writes, we split writes into two
421 * system calls so that the process can be killed in between.
423 split
= (len
> 0 ? rand() % len
: 0);
424 iolen
= pwrite64(vp
->v_fd
, addr
, split
, offset
);
425 iolen
+= pwrite64(vp
->v_fd
, (char *)addr
+ split
,
426 len
- split
, offset
+ split
);
432 *residp
= len
- iolen
;
433 else if (iolen
!= len
)
439 vn_close(vnode_t
*vp
)
442 spa_strfree(vp
->v_path
);
443 umem_free(vp
, sizeof (vnode_t
));
449 * =========================================================================
450 * Figure out which debugging statements to print
451 * =========================================================================
454 static char *dprintf_string
;
455 static int dprintf_print_all
;
458 dprintf_find_string(const char *string
)
460 char *tmp_str
= dprintf_string
;
461 int len
= strlen(string
);
464 * Find out if this is a string we want to print.
465 * String format: file1.c,function_name1,file2.c,file3.c
468 while (tmp_str
!= NULL
) {
469 if (strncmp(tmp_str
, string
, len
) == 0 &&
470 (tmp_str
[len
] == ',' || tmp_str
[len
] == '\0'))
472 tmp_str
= strchr(tmp_str
, ',');
474 tmp_str
++; /* Get rid of , */
480 dprintf_setup(int *argc
, char **argv
)
485 * Debugging can be specified two ways: by setting the
486 * environment variable ZFS_DEBUG, or by including a
487 * "debug=..." argument on the command line. The command
488 * line setting overrides the environment variable.
491 for (i
= 1; i
< *argc
; i
++) {
492 int len
= strlen("debug=");
493 /* First look for a command line argument */
494 if (strncmp("debug=", argv
[i
], len
) == 0) {
495 dprintf_string
= argv
[i
] + len
;
496 /* Remove from args */
497 for (j
= i
; j
< *argc
; j
++)
504 if (dprintf_string
== NULL
) {
505 /* Look for ZFS_DEBUG environment variable */
506 dprintf_string
= getenv("ZFS_DEBUG");
510 * Are we just turning on all debugging?
512 if (dprintf_find_string("on"))
513 dprintf_print_all
= 1;
517 * =========================================================================
519 * =========================================================================
522 __dprintf(const char *file
, const char *func
, int line
, const char *fmt
, ...)
528 * Get rid of annoying "../common/" prefix to filename.
530 newfile
= strrchr(file
, '/');
531 if (newfile
!= NULL
) {
532 newfile
= newfile
+ 1; /* Get rid of leading / */
537 if (dprintf_print_all
||
538 dprintf_find_string(newfile
) ||
539 dprintf_find_string(func
)) {
540 /* Print out just the function name if requested */
542 if (dprintf_find_string("pid"))
543 (void) printf("%d ", getpid());
544 if (dprintf_find_string("tid"))
545 (void) printf("%u ", thr_self());
546 if (dprintf_find_string("cpu"))
547 (void) printf("%u ", getcpuid());
548 if (dprintf_find_string("time"))
549 (void) printf("%llu ", gethrtime());
550 if (dprintf_find_string("long"))
551 (void) printf("%s, line %d: ", newfile
, line
);
552 (void) printf("%s: ", func
);
554 (void) vprintf(fmt
, adx
);
560 #endif /* ZFS_DEBUG */
563 * =========================================================================
564 * cmn_err() and panic()
565 * =========================================================================
567 static char ce_prefix
[CE_IGNORE
][10] = { "", "NOTICE: ", "WARNING: ", "" };
568 static char ce_suffix
[CE_IGNORE
][2] = { "", "\n", "\n", "" };
571 vpanic(const char *fmt
, va_list adx
)
573 (void) fprintf(stderr
, "error: ");
574 (void) vfprintf(stderr
, fmt
, adx
);
575 (void) fprintf(stderr
, "\n");
577 abort(); /* think of it as a "user-level crash dump" */
581 panic(const char *fmt
, ...)
591 vcmn_err(int ce
, const char *fmt
, va_list adx
)
595 if (ce
!= CE_NOTE
) { /* suppress noise in userland stress testing */
596 (void) fprintf(stderr
, "%s", ce_prefix
[ce
]);
597 (void) vfprintf(stderr
, fmt
, adx
);
598 (void) fprintf(stderr
, "%s", ce_suffix
[ce
]);
604 cmn_err(int ce
, const char *fmt
, ...)
609 vcmn_err(ce
, fmt
, adx
);
614 * =========================================================================
616 * =========================================================================
619 kobj_open_file(char *name
)
624 /* set vp as the _fd field of the file */
625 if (vn_openat(name
, UIO_SYSSPACE
, FREAD
, 0, &vp
, 0, 0, rootdir
,
627 return ((void *)-1UL);
629 file
= umem_zalloc(sizeof (struct _buf
), UMEM_NOFAIL
);
630 file
->_fd
= (intptr_t)vp
;
635 kobj_read_file(struct _buf
*file
, char *buf
, unsigned size
, unsigned off
)
639 vn_rdwr(UIO_READ
, (vnode_t
*)file
->_fd
, buf
, size
, (offset_t
)off
,
640 UIO_SYSSPACE
, 0, 0, 0, &resid
);
642 return (size
- resid
);
646 kobj_close_file(struct _buf
*file
)
648 vn_close((vnode_t
*)file
->_fd
);
649 umem_free(file
, sizeof (struct _buf
));
653 kobj_get_filesize(struct _buf
*file
, uint64_t *size
)
656 vnode_t
*vp
= (vnode_t
*)file
->_fd
;
658 if (fstat64(vp
->v_fd
, &st
) == -1) {
667 * =========================================================================
669 * =========================================================================
675 poll(0, 0, ticks
* (1000 / hz
));
679 * Find highest one bit set.
680 * Returns bit number + 1 of highest bit that is set, otherwise returns 0.
681 * High order bit is 31 (or 63 in _LP64 kernel).
691 if (i
& 0xffffffff00000000ul
) {
695 if (i
& 0xffff0000) {
713 static int random_fd
= -1, urandom_fd
= -1;
716 random_get_bytes_common(uint8_t *ptr
, size_t len
, int fd
)
724 bytes
= read(fd
, ptr
, resid
);
725 ASSERT3S(bytes
, >=, 0);
734 random_get_bytes(uint8_t *ptr
, size_t len
)
736 return (random_get_bytes_common(ptr
, len
, random_fd
));
740 random_get_pseudo_bytes(uint8_t *ptr
, size_t len
)
742 return (random_get_bytes_common(ptr
, len
, urandom_fd
));
746 ddi_strtoul(const char *hw_serial
, char **nptr
, int base
, unsigned long *result
)
750 *result
= strtoul(hw_serial
, &end
, base
);
757 * =========================================================================
758 * kernel emulation setup & teardown
759 * =========================================================================
762 umem_out_of_memory(void)
764 char errmsg
[] = "out of memory -- generating core dump\n";
766 write(fileno(stderr
), errmsg
, sizeof (errmsg
));
772 kernel_init(int mode
)
774 umem_nofail_callback(umem_out_of_memory
);
776 physmem
= sysconf(_SC_PHYS_PAGES
);
778 dprintf("physmem = %llu pages (%.2f GB)\n", physmem
,
779 (double)physmem
* sysconf(_SC_PAGE_SIZE
) / (1ULL << 30));
781 snprintf(hw_serial
, sizeof (hw_serial
), "%ld", gethostid());
783 VERIFY((random_fd
= open("/dev/random", O_RDONLY
)) != -1);
784 VERIFY((urandom_fd
= open("/dev/urandom", O_RDONLY
)) != -1);
804 z_uncompress(void *dst
, size_t *dstlen
, const void *src
, size_t srclen
)
807 uLongf len
= *dstlen
;
809 if ((ret
= uncompress(dst
, &len
, src
, srclen
)) == Z_OK
)
810 *dstlen
= (size_t)len
;
816 z_compress_level(void *dst
, size_t *dstlen
, const void *src
, size_t srclen
,
820 uLongf len
= *dstlen
;
822 if ((ret
= compress2(dst
, &len
, src
, srclen
, level
)) == Z_OK
)
823 *dstlen
= (size_t)len
;
841 crgetngroups(cred_t
*cr
)
847 crgetgroups(cred_t
*cr
)
853 zfs_secpolicy_snapshot_perms(const char *name
, cred_t
*cr
)
859 zfs_secpolicy_rename_perms(const char *from
, const char *to
, cred_t
*cr
)
865 zfs_secpolicy_destroy_perms(const char *name
, cred_t
*cr
)
871 ksid_lookupdomain(const char *dom
)
875 kd
= umem_zalloc(sizeof (ksiddomain_t
), UMEM_NOFAIL
);
876 kd
->kd_name
= spa_strdup(dom
);
881 ksiddomain_rele(ksiddomain_t
*ksid
)
883 spa_strfree(ksid
->kd_name
);
884 umem_free(ksid
, sizeof (ksiddomain_t
));