]>
git.proxmox.com Git - mirror_zfs.git/blob - zfs/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.
37 #include <sys/processor.h>
38 #include <sys/zfs_context.h>
40 #include <sys/utsname.h>
43 * Emulation of kernel services in userland.
47 vnode_t
*rootdir
= (vnode_t
*)0xabcd1234;
50 struct utsname utsname
= {
51 "userland", "libzpool", "1", "1", "na"
55 * =========================================================================
57 * =========================================================================
61 zk_thread_create(void (*func
)(), void *arg
)
65 VERIFY(thr_create(0, 0, (void *(*)(void *))func
, arg
, THR_DETACHED
,
68 return ((void *)(uintptr_t)tid
);
72 * =========================================================================
74 * =========================================================================
78 kstat_create(char *module
, int instance
, char *name
, char *class,
79 uchar_t type
, ulong_t ndata
, uchar_t ks_flag
)
86 kstat_install(kstat_t
*ksp
)
91 kstat_delete(kstat_t
*ksp
)
95 * =========================================================================
97 * =========================================================================
100 zmutex_init(kmutex_t
*mp
)
103 mp
->initialized
= B_TRUE
;
104 (void) _mutex_init(&mp
->m_lock
, USYNC_THREAD
, NULL
);
108 zmutex_destroy(kmutex_t
*mp
)
110 ASSERT(mp
->initialized
== B_TRUE
);
111 ASSERT(mp
->m_owner
== NULL
);
112 (void) _mutex_destroy(&(mp
)->m_lock
);
113 mp
->m_owner
= (void *)-1UL;
114 mp
->initialized
= B_FALSE
;
118 mutex_enter(kmutex_t
*mp
)
120 ASSERT(mp
->initialized
== B_TRUE
);
121 ASSERT(mp
->m_owner
!= (void *)-1UL);
122 ASSERT(mp
->m_owner
!= curthread
);
123 VERIFY(mutex_lock(&mp
->m_lock
) == 0);
124 ASSERT(mp
->m_owner
== NULL
);
125 mp
->m_owner
= curthread
;
129 mutex_tryenter(kmutex_t
*mp
)
131 ASSERT(mp
->initialized
== B_TRUE
);
132 ASSERT(mp
->m_owner
!= (void *)-1UL);
133 if (0 == mutex_trylock(&mp
->m_lock
)) {
134 ASSERT(mp
->m_owner
== NULL
);
135 mp
->m_owner
= curthread
;
143 mutex_exit(kmutex_t
*mp
)
145 ASSERT(mp
->initialized
== B_TRUE
);
146 ASSERT(mutex_owner(mp
) == curthread
);
148 VERIFY(mutex_unlock(&mp
->m_lock
) == 0);
152 mutex_owner(kmutex_t
*mp
)
154 ASSERT(mp
->initialized
== B_TRUE
);
155 return (mp
->m_owner
);
159 * =========================================================================
161 * =========================================================================
165 rw_init(krwlock_t
*rwlp
, char *name
, int type
, void *arg
)
167 rwlock_init(&rwlp
->rw_lock
, USYNC_THREAD
, NULL
);
168 rwlp
->rw_owner
= NULL
;
169 rwlp
->initialized
= B_TRUE
;
173 rw_destroy(krwlock_t
*rwlp
)
175 rwlock_destroy(&rwlp
->rw_lock
);
176 rwlp
->rw_owner
= (void *)-1UL;
177 rwlp
->initialized
= B_FALSE
;
181 rw_enter(krwlock_t
*rwlp
, krw_t rw
)
183 ASSERT(!RW_LOCK_HELD(rwlp
));
184 ASSERT(rwlp
->initialized
== B_TRUE
);
185 ASSERT(rwlp
->rw_owner
!= (void *)-1UL);
186 ASSERT(rwlp
->rw_owner
!= curthread
);
189 (void) rw_rdlock(&rwlp
->rw_lock
);
191 (void) rw_wrlock(&rwlp
->rw_lock
);
193 rwlp
->rw_owner
= curthread
;
197 rw_exit(krwlock_t
*rwlp
)
199 ASSERT(rwlp
->initialized
== B_TRUE
);
200 ASSERT(rwlp
->rw_owner
!= (void *)-1UL);
202 rwlp
->rw_owner
= NULL
;
203 (void) rw_unlock(&rwlp
->rw_lock
);
207 rw_tryenter(krwlock_t
*rwlp
, krw_t rw
)
211 ASSERT(rwlp
->initialized
== B_TRUE
);
212 ASSERT(rwlp
->rw_owner
!= (void *)-1UL);
215 rv
= rw_tryrdlock(&rwlp
->rw_lock
);
217 rv
= rw_trywrlock(&rwlp
->rw_lock
);
220 rwlp
->rw_owner
= curthread
;
229 rw_tryupgrade(krwlock_t
*rwlp
)
231 ASSERT(rwlp
->initialized
== B_TRUE
);
232 ASSERT(rwlp
->rw_owner
!= (void *)-1UL);
238 * =========================================================================
239 * condition variables
240 * =========================================================================
244 cv_init(kcondvar_t
*cv
, char *name
, int type
, void *arg
)
246 VERIFY(cond_init(cv
, type
, NULL
) == 0);
250 cv_destroy(kcondvar_t
*cv
)
252 VERIFY(cond_destroy(cv
) == 0);
256 cv_wait(kcondvar_t
*cv
, kmutex_t
*mp
)
258 ASSERT(mutex_owner(mp
) == curthread
);
260 int ret
= cond_wait(cv
, &mp
->m_lock
);
261 VERIFY(ret
== 0 || ret
== EINTR
);
262 mp
->m_owner
= curthread
;
266 cv_timedwait(kcondvar_t
*cv
, kmutex_t
*mp
, clock_t abstime
)
273 delta
= abstime
- lbolt
;
277 ts
.tv_sec
= delta
/ hz
;
278 ts
.tv_nsec
= (delta
% hz
) * (NANOSEC
/ hz
);
280 ASSERT(mutex_owner(mp
) == curthread
);
282 error
= cond_reltimedwait(cv
, &mp
->m_lock
, &ts
);
283 mp
->m_owner
= curthread
;
297 cv_signal(kcondvar_t
*cv
)
299 VERIFY(cond_signal(cv
) == 0);
303 cv_broadcast(kcondvar_t
*cv
)
305 VERIFY(cond_broadcast(cv
) == 0);
309 * =========================================================================
311 * =========================================================================
314 * Note: for the xxxat() versions of these functions, we assume that the
315 * starting vp is always rootdir (which is true for spa_directory.c, the only
316 * ZFS consumer of these interfaces). We assert this is true, and then emulate
317 * them by adding '/' in front of the path.
322 vn_open(char *path
, int x1
, int flags
, int mode
, vnode_t
**vpp
, int x2
, int x3
)
327 char realpath
[MAXPATHLEN
];
331 * If we're accessing a real disk from userland, we need to use
332 * the character interface to avoid caching. This is particularly
333 * important if we're trying to look at a real in-kernel storage
334 * pool from userland, e.g. via zdb, because otherwise we won't
335 * see the changes occurring under the segmap cache.
336 * On the other hand, the stupid character device returns zero
337 * for its size. So -- gag -- we open the block device to get
338 * its size, and remember it for subsequent VOP_GETATTR().
340 if (strncmp(path
, "/dev/", 5) == 0) {
342 fd
= open64(path
, O_RDONLY
);
345 if (fstat64(fd
, &st
) == -1) {
350 (void) sprintf(realpath
, "%s", path
);
351 dsk
= strstr(path
, "/dsk/");
353 (void) sprintf(realpath
+ (dsk
- path
) + 1, "r%s",
356 (void) sprintf(realpath
, "%s", path
);
357 if (!(flags
& FCREAT
) && stat64(realpath
, &st
) == -1)
362 old_umask
= umask(0);
365 * The construct 'flags - FREAD' conveniently maps combinations of
366 * FREAD and FWRITE to the corresponding O_RDONLY, O_WRONLY, and O_RDWR.
368 fd
= open64(realpath
, flags
- FREAD
, mode
);
371 (void) umask(old_umask
);
376 if (fstat64(fd
, &st
) == -1) {
381 (void) fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
383 *vpp
= vp
= umem_zalloc(sizeof (vnode_t
), UMEM_NOFAIL
);
386 vp
->v_size
= st
.st_size
;
387 vp
->v_path
= spa_strdup(path
);
394 vn_openat(char *path
, int x1
, int flags
, int mode
, vnode_t
**vpp
, int x2
,
395 int x3
, vnode_t
*startvp
, int fd
)
397 char *realpath
= umem_alloc(strlen(path
) + 2, UMEM_NOFAIL
);
400 ASSERT(startvp
== rootdir
);
401 (void) sprintf(realpath
, "/%s", path
);
403 /* fd ignored for now, need if want to simulate nbmand support */
404 ret
= vn_open(realpath
, x1
, flags
, mode
, vpp
, x2
, x3
);
406 umem_free(realpath
, strlen(path
) + 2);
413 vn_rdwr(int uio
, vnode_t
*vp
, void *addr
, ssize_t len
, offset_t offset
,
414 int x1
, int x2
, rlim64_t x3
, void *x4
, ssize_t
*residp
)
416 ssize_t iolen
, split
;
418 if (uio
== UIO_READ
) {
419 iolen
= pread64(vp
->v_fd
, addr
, len
, offset
);
422 * To simulate partial disk writes, we split writes into two
423 * system calls so that the process can be killed in between.
425 split
= (len
> 0 ? rand() % len
: 0);
426 iolen
= pwrite64(vp
->v_fd
, addr
, split
, offset
);
427 iolen
+= pwrite64(vp
->v_fd
, (char *)addr
+ split
,
428 len
- split
, offset
+ split
);
434 *residp
= len
- iolen
;
435 else if (iolen
!= len
)
441 vn_close(vnode_t
*vp
)
444 spa_strfree(vp
->v_path
);
445 umem_free(vp
, sizeof (vnode_t
));
451 * =========================================================================
452 * Figure out which debugging statements to print
453 * =========================================================================
456 static char *dprintf_string
;
457 static int dprintf_print_all
;
460 dprintf_find_string(const char *string
)
462 char *tmp_str
= dprintf_string
;
463 int len
= strlen(string
);
466 * Find out if this is a string we want to print.
467 * String format: file1.c,function_name1,file2.c,file3.c
470 while (tmp_str
!= NULL
) {
471 if (strncmp(tmp_str
, string
, len
) == 0 &&
472 (tmp_str
[len
] == ',' || tmp_str
[len
] == '\0'))
474 tmp_str
= strchr(tmp_str
, ',');
476 tmp_str
++; /* Get rid of , */
482 dprintf_setup(int *argc
, char **argv
)
487 * Debugging can be specified two ways: by setting the
488 * environment variable ZFS_DEBUG, or by including a
489 * "debug=..." argument on the command line. The command
490 * line setting overrides the environment variable.
493 for (i
= 1; i
< *argc
; i
++) {
494 int len
= strlen("debug=");
495 /* First look for a command line argument */
496 if (strncmp("debug=", argv
[i
], len
) == 0) {
497 dprintf_string
= argv
[i
] + len
;
498 /* Remove from args */
499 for (j
= i
; j
< *argc
; j
++)
506 if (dprintf_string
== NULL
) {
507 /* Look for ZFS_DEBUG environment variable */
508 dprintf_string
= getenv("ZFS_DEBUG");
512 * Are we just turning on all debugging?
514 if (dprintf_find_string("on"))
515 dprintf_print_all
= 1;
519 * =========================================================================
521 * =========================================================================
524 __dprintf(const char *file
, const char *func
, int line
, const char *fmt
, ...)
530 * Get rid of annoying "../common/" prefix to filename.
532 newfile
= strrchr(file
, '/');
533 if (newfile
!= NULL
) {
534 newfile
= newfile
+ 1; /* Get rid of leading / */
539 if (dprintf_print_all
||
540 dprintf_find_string(newfile
) ||
541 dprintf_find_string(func
)) {
542 /* Print out just the function name if requested */
544 if (dprintf_find_string("pid"))
545 (void) printf("%d ", getpid());
546 if (dprintf_find_string("tid"))
547 (void) printf("%u ", thr_self());
548 if (dprintf_find_string("cpu"))
549 (void) printf("%u ", getcpuid());
550 if (dprintf_find_string("time"))
551 (void) printf("%llu ", gethrtime());
552 if (dprintf_find_string("long"))
553 (void) printf("%s, line %d: ", newfile
, line
);
554 (void) printf("%s: ", func
);
556 (void) vprintf(fmt
, adx
);
562 #endif /* ZFS_DEBUG */
565 * =========================================================================
566 * cmn_err() and panic()
567 * =========================================================================
569 static char ce_prefix
[CE_IGNORE
][10] = { "", "NOTICE: ", "WARNING: ", "" };
570 static char ce_suffix
[CE_IGNORE
][2] = { "", "\n", "\n", "" };
573 vpanic(const char *fmt
, va_list adx
)
575 (void) fprintf(stderr
, "error: ");
576 (void) vfprintf(stderr
, fmt
, adx
);
577 (void) fprintf(stderr
, "\n");
579 abort(); /* think of it as a "user-level crash dump" */
583 panic(const char *fmt
, ...)
593 vcmn_err(int ce
, const char *fmt
, va_list adx
)
597 if (ce
!= CE_NOTE
) { /* suppress noise in userland stress testing */
598 (void) fprintf(stderr
, "%s", ce_prefix
[ce
]);
599 (void) vfprintf(stderr
, fmt
, adx
);
600 (void) fprintf(stderr
, "%s", ce_suffix
[ce
]);
606 cmn_err(int ce
, const char *fmt
, ...)
611 vcmn_err(ce
, fmt
, adx
);
616 * =========================================================================
618 * =========================================================================
621 kobj_open_file(char *name
)
626 /* set vp as the _fd field of the file */
627 if (vn_openat(name
, UIO_SYSSPACE
, FREAD
, 0, &vp
, 0, 0, rootdir
,
629 return ((void *)-1UL);
631 file
= umem_zalloc(sizeof (struct _buf
), UMEM_NOFAIL
);
632 file
->_fd
= (intptr_t)vp
;
637 kobj_read_file(struct _buf
*file
, char *buf
, unsigned size
, unsigned off
)
641 vn_rdwr(UIO_READ
, (vnode_t
*)file
->_fd
, buf
, size
, (offset_t
)off
,
642 UIO_SYSSPACE
, 0, 0, 0, &resid
);
644 return (size
- resid
);
648 kobj_close_file(struct _buf
*file
)
650 vn_close((vnode_t
*)file
->_fd
);
651 umem_free(file
, sizeof (struct _buf
));
655 kobj_get_filesize(struct _buf
*file
, uint64_t *size
)
658 vnode_t
*vp
= (vnode_t
*)file
->_fd
;
660 if (fstat64(vp
->v_fd
, &st
) == -1) {
669 * =========================================================================
671 * =========================================================================
677 poll(0, 0, ticks
* (1000 / hz
));
681 * Find highest one bit set.
682 * Returns bit number + 1 of highest bit that is set, otherwise returns 0.
683 * High order bit is 31 (or 63 in _LP64 kernel).
693 if (i
& 0xffffffff00000000ul
) {
697 if (i
& 0xffff0000) {
715 static int random_fd
= -1, urandom_fd
= -1;
718 random_get_bytes_common(uint8_t *ptr
, size_t len
, int fd
)
726 bytes
= read(fd
, ptr
, resid
);
727 ASSERT3S(bytes
, >=, 0);
736 random_get_bytes(uint8_t *ptr
, size_t len
)
738 return (random_get_bytes_common(ptr
, len
, random_fd
));
742 random_get_pseudo_bytes(uint8_t *ptr
, size_t len
)
744 return (random_get_bytes_common(ptr
, len
, urandom_fd
));
748 ddi_strtoul(const char *hw_serial
, char **nptr
, int base
, unsigned long *result
)
752 *result
= strtoul(hw_serial
, &end
, base
);
759 * =========================================================================
760 * kernel emulation setup & teardown
761 * =========================================================================
764 umem_out_of_memory(void)
766 char errmsg
[] = "out of memory -- generating core dump\n";
768 write(fileno(stderr
), errmsg
, sizeof (errmsg
));
774 kernel_init(int mode
)
776 umem_nofail_callback(umem_out_of_memory
);
778 physmem
= sysconf(_SC_PHYS_PAGES
);
780 dprintf("physmem = %llu pages (%.2f GB)\n", physmem
,
781 (double)physmem
* sysconf(_SC_PAGE_SIZE
) / (1ULL << 30));
784 snprintf(hw_serial
, sizeof (hw_serial
), "%ld", gethostid());
786 VERIFY((random_fd
= open("/dev/random", O_RDONLY
)) != -1);
787 VERIFY((urandom_fd
= open("/dev/urandom", O_RDONLY
)) != -1);
805 z_uncompress(void *dst
, size_t *dstlen
, const void *src
, size_t srclen
)
808 uLongf len
= *dstlen
;
810 if ((ret
= uncompress(dst
, &len
, src
, srclen
)) == Z_OK
)
811 *dstlen
= (size_t)len
;
817 z_compress_level(void *dst
, size_t *dstlen
, const void *src
, size_t srclen
,
821 uLongf len
= *dstlen
;
823 if ((ret
= compress2(dst
, &len
, src
, srclen
, level
)) == Z_OK
)
824 *dstlen
= (size_t)len
;
830 size_t u8_textprep_str(char *i
, size_t *il
, char *o
, size_t *ol
, int nf
,
831 size_t vers
, int *err
)
850 crgetngroups(cred_t
*cr
)
856 crgetgroups(cred_t
*cr
)
862 zfs_secpolicy_snapshot_perms(const char *name
, cred_t
*cr
)
868 zfs_secpolicy_rename_perms(const char *from
, const char *to
, cred_t
*cr
)
874 zfs_secpolicy_destroy_perms(const char *name
, cred_t
*cr
)
880 ksid_lookupdomain(const char *dom
)
884 kd
= umem_zalloc(sizeof (ksiddomain_t
), UMEM_NOFAIL
);
885 kd
->kd_name
= spa_strdup(dom
);
890 ksiddomain_rele(ksiddomain_t
*ksid
)
892 spa_strfree(ksid
->kd_name
);
893 umem_free(ksid
, sizeof (ksiddomain_t
));