]>
git.proxmox.com Git - mirror_zfs.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 2010 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>
39 #include <sys/systeminfo.h>
42 * Emulation of kernel services in userland.
47 vnode_t
*rootdir
= (vnode_t
*)0xabcd1234;
48 char hw_serial
[HW_HOSTID_LEN
];
50 struct utsname utsname
= {
51 "userland", "libzpool", "1", "1", "na"
54 /* this only exists to have its address taken */
58 * =========================================================================
60 * =========================================================================
64 zk_thread_create(void (*func
)(), void *arg
)
68 VERIFY(thr_create(0, 0, (void *(*)(void *))func
, arg
, THR_DETACHED
,
71 return ((void *)(uintptr_t)tid
);
75 * =========================================================================
77 * =========================================================================
81 kstat_create(char *module
, int instance
, char *name
, char *class,
82 uchar_t type
, ulong_t ndata
, uchar_t ks_flag
)
89 kstat_install(kstat_t
*ksp
)
94 kstat_delete(kstat_t
*ksp
)
98 * =========================================================================
100 * =========================================================================
103 zmutex_init(kmutex_t
*mp
)
106 mp
->initialized
= B_TRUE
;
107 (void) _mutex_init(&mp
->m_lock
, USYNC_THREAD
, NULL
);
111 zmutex_destroy(kmutex_t
*mp
)
113 ASSERT(mp
->initialized
== B_TRUE
);
114 ASSERT(mp
->m_owner
== NULL
);
115 (void) _mutex_destroy(&(mp
)->m_lock
);
116 mp
->m_owner
= (void *)-1UL;
117 mp
->initialized
= B_FALSE
;
121 mutex_enter(kmutex_t
*mp
)
123 ASSERT(mp
->initialized
== B_TRUE
);
124 ASSERT(mp
->m_owner
!= (void *)-1UL);
125 ASSERT(mp
->m_owner
!= curthread
);
126 VERIFY(mutex_lock(&mp
->m_lock
) == 0);
127 ASSERT(mp
->m_owner
== NULL
);
128 mp
->m_owner
= curthread
;
132 mutex_tryenter(kmutex_t
*mp
)
134 ASSERT(mp
->initialized
== B_TRUE
);
135 ASSERT(mp
->m_owner
!= (void *)-1UL);
136 if (0 == mutex_trylock(&mp
->m_lock
)) {
137 ASSERT(mp
->m_owner
== NULL
);
138 mp
->m_owner
= curthread
;
146 mutex_exit(kmutex_t
*mp
)
148 ASSERT(mp
->initialized
== B_TRUE
);
149 ASSERT(mutex_owner(mp
) == curthread
);
151 VERIFY(mutex_unlock(&mp
->m_lock
) == 0);
155 mutex_owner(kmutex_t
*mp
)
157 ASSERT(mp
->initialized
== B_TRUE
);
158 return (mp
->m_owner
);
162 * =========================================================================
164 * =========================================================================
168 rw_init(krwlock_t
*rwlp
, char *name
, int type
, void *arg
)
170 rwlock_init(&rwlp
->rw_lock
, USYNC_THREAD
, NULL
);
171 rwlp
->rw_owner
= NULL
;
172 rwlp
->initialized
= B_TRUE
;
176 rw_destroy(krwlock_t
*rwlp
)
178 rwlock_destroy(&rwlp
->rw_lock
);
179 rwlp
->rw_owner
= (void *)-1UL;
180 rwlp
->initialized
= B_FALSE
;
184 rw_enter(krwlock_t
*rwlp
, krw_t rw
)
186 ASSERT(!RW_LOCK_HELD(rwlp
));
187 ASSERT(rwlp
->initialized
== B_TRUE
);
188 ASSERT(rwlp
->rw_owner
!= (void *)-1UL);
189 ASSERT(rwlp
->rw_owner
!= curthread
);
192 VERIFY(rw_rdlock(&rwlp
->rw_lock
) == 0);
194 VERIFY(rw_wrlock(&rwlp
->rw_lock
) == 0);
196 rwlp
->rw_owner
= curthread
;
200 rw_exit(krwlock_t
*rwlp
)
202 ASSERT(rwlp
->initialized
== B_TRUE
);
203 ASSERT(rwlp
->rw_owner
!= (void *)-1UL);
205 rwlp
->rw_owner
= NULL
;
206 VERIFY(rw_unlock(&rwlp
->rw_lock
) == 0);
210 rw_tryenter(krwlock_t
*rwlp
, krw_t rw
)
214 ASSERT(rwlp
->initialized
== B_TRUE
);
215 ASSERT(rwlp
->rw_owner
!= (void *)-1UL);
218 rv
= rw_tryrdlock(&rwlp
->rw_lock
);
220 rv
= rw_trywrlock(&rwlp
->rw_lock
);
223 rwlp
->rw_owner
= curthread
;
232 rw_tryupgrade(krwlock_t
*rwlp
)
234 ASSERT(rwlp
->initialized
== B_TRUE
);
235 ASSERT(rwlp
->rw_owner
!= (void *)-1UL);
241 * =========================================================================
242 * condition variables
243 * =========================================================================
247 cv_init(kcondvar_t
*cv
, char *name
, int type
, void *arg
)
249 VERIFY(cond_init(cv
, type
, NULL
) == 0);
253 cv_destroy(kcondvar_t
*cv
)
255 VERIFY(cond_destroy(cv
) == 0);
259 cv_wait(kcondvar_t
*cv
, kmutex_t
*mp
)
261 ASSERT(mutex_owner(mp
) == curthread
);
263 int ret
= cond_wait(cv
, &mp
->m_lock
);
264 VERIFY(ret
== 0 || ret
== EINTR
);
265 mp
->m_owner
= curthread
;
269 cv_timedwait(kcondvar_t
*cv
, kmutex_t
*mp
, clock_t abstime
)
276 delta
= abstime
- ddi_get_lbolt();
280 ts
.tv_sec
= delta
/ hz
;
281 ts
.tv_nsec
= (delta
% hz
) * (NANOSEC
/ hz
);
283 ASSERT(mutex_owner(mp
) == curthread
);
285 error
= cond_reltimedwait(cv
, &mp
->m_lock
, &ts
);
286 mp
->m_owner
= curthread
;
300 cv_signal(kcondvar_t
*cv
)
302 VERIFY(cond_signal(cv
) == 0);
306 cv_broadcast(kcondvar_t
*cv
)
308 VERIFY(cond_broadcast(cv
) == 0);
312 * =========================================================================
314 * =========================================================================
317 * Note: for the xxxat() versions of these functions, we assume that the
318 * starting vp is always rootdir (which is true for spa_directory.c, the only
319 * ZFS consumer of these interfaces). We assert this is true, and then emulate
320 * them by adding '/' in front of the path.
325 vn_open(char *path
, int x1
, int flags
, int mode
, vnode_t
**vpp
, int x2
, int x3
)
330 char realpath
[MAXPATHLEN
];
334 * If we're accessing a real disk from userland, we need to use
335 * the character interface to avoid caching. This is particularly
336 * important if we're trying to look at a real in-kernel storage
337 * pool from userland, e.g. via zdb, because otherwise we won't
338 * see the changes occurring under the segmap cache.
339 * On the other hand, the stupid character device returns zero
340 * for its size. So -- gag -- we open the block device to get
341 * its size, and remember it for subsequent VOP_GETATTR().
343 if (strncmp(path
, "/dev/", 5) == 0) {
345 fd
= open64(path
, O_RDONLY
);
348 if (fstat64(fd
, &st
) == -1) {
353 (void) sprintf(realpath
, "%s", path
);
354 dsk
= strstr(path
, "/dsk/");
356 (void) sprintf(realpath
+ (dsk
- path
) + 1, "r%s",
359 (void) sprintf(realpath
, "%s", path
);
360 if (!(flags
& FCREAT
) && stat64(realpath
, &st
) == -1)
365 old_umask
= umask(0);
368 * The construct 'flags - FREAD' conveniently maps combinations of
369 * FREAD and FWRITE to the corresponding O_RDONLY, O_WRONLY, and O_RDWR.
371 fd
= open64(realpath
, flags
- FREAD
, mode
);
374 (void) umask(old_umask
);
379 if (fstat64(fd
, &st
) == -1) {
384 (void) fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
386 *vpp
= vp
= umem_zalloc(sizeof (vnode_t
), UMEM_NOFAIL
);
389 vp
->v_size
= st
.st_size
;
390 vp
->v_path
= spa_strdup(path
);
397 vn_openat(char *path
, int x1
, int flags
, int mode
, vnode_t
**vpp
, int x2
,
398 int x3
, vnode_t
*startvp
, int fd
)
400 char *realpath
= umem_alloc(strlen(path
) + 2, UMEM_NOFAIL
);
403 ASSERT(startvp
== rootdir
);
404 (void) sprintf(realpath
, "/%s", path
);
406 /* fd ignored for now, need if want to simulate nbmand support */
407 ret
= vn_open(realpath
, x1
, flags
, mode
, vpp
, x2
, x3
);
409 umem_free(realpath
, strlen(path
) + 2);
416 vn_rdwr(int uio
, vnode_t
*vp
, void *addr
, ssize_t len
, offset_t offset
,
417 int x1
, int x2
, rlim64_t x3
, void *x4
, ssize_t
*residp
)
419 ssize_t iolen
, split
;
421 if (uio
== UIO_READ
) {
422 iolen
= pread64(vp
->v_fd
, addr
, len
, offset
);
425 * To simulate partial disk writes, we split writes into two
426 * system calls so that the process can be killed in between.
428 split
= (len
> 0 ? rand() % len
: 0);
429 iolen
= pwrite64(vp
->v_fd
, addr
, split
, offset
);
430 iolen
+= pwrite64(vp
->v_fd
, (char *)addr
+ split
,
431 len
- split
, offset
+ split
);
437 *residp
= len
- iolen
;
438 else if (iolen
!= len
)
444 vn_close(vnode_t
*vp
)
447 spa_strfree(vp
->v_path
);
448 umem_free(vp
, sizeof (vnode_t
));
452 * At a minimum we need to update the size since vdev_reopen()
453 * will no longer call vn_openat().
456 fop_getattr(vnode_t
*vp
, vattr_t
*vap
)
460 if (fstat64(vp
->v_fd
, &st
) == -1) {
465 vap
->va_size
= st
.st_size
;
472 * =========================================================================
473 * Figure out which debugging statements to print
474 * =========================================================================
477 static char *dprintf_string
;
478 static int dprintf_print_all
;
481 dprintf_find_string(const char *string
)
483 char *tmp_str
= dprintf_string
;
484 int len
= strlen(string
);
487 * Find out if this is a string we want to print.
488 * String format: file1.c,function_name1,file2.c,file3.c
491 while (tmp_str
!= NULL
) {
492 if (strncmp(tmp_str
, string
, len
) == 0 &&
493 (tmp_str
[len
] == ',' || tmp_str
[len
] == '\0'))
495 tmp_str
= strchr(tmp_str
, ',');
497 tmp_str
++; /* Get rid of , */
503 dprintf_setup(int *argc
, char **argv
)
508 * Debugging can be specified two ways: by setting the
509 * environment variable ZFS_DEBUG, or by including a
510 * "debug=..." argument on the command line. The command
511 * line setting overrides the environment variable.
514 for (i
= 1; i
< *argc
; i
++) {
515 int len
= strlen("debug=");
516 /* First look for a command line argument */
517 if (strncmp("debug=", argv
[i
], len
) == 0) {
518 dprintf_string
= argv
[i
] + len
;
519 /* Remove from args */
520 for (j
= i
; j
< *argc
; j
++)
527 if (dprintf_string
== NULL
) {
528 /* Look for ZFS_DEBUG environment variable */
529 dprintf_string
= getenv("ZFS_DEBUG");
533 * Are we just turning on all debugging?
535 if (dprintf_find_string("on"))
536 dprintf_print_all
= 1;
540 * =========================================================================
542 * =========================================================================
545 __dprintf(const char *file
, const char *func
, int line
, const char *fmt
, ...)
551 * Get rid of annoying "../common/" prefix to filename.
553 newfile
= strrchr(file
, '/');
554 if (newfile
!= NULL
) {
555 newfile
= newfile
+ 1; /* Get rid of leading / */
560 if (dprintf_print_all
||
561 dprintf_find_string(newfile
) ||
562 dprintf_find_string(func
)) {
563 /* Print out just the function name if requested */
565 if (dprintf_find_string("pid"))
566 (void) printf("%d ", getpid());
567 if (dprintf_find_string("tid"))
568 (void) printf("%u ", thr_self());
569 if (dprintf_find_string("cpu"))
570 (void) printf("%u ", getcpuid());
571 if (dprintf_find_string("time"))
572 (void) printf("%llu ", gethrtime());
573 if (dprintf_find_string("long"))
574 (void) printf("%s, line %d: ", newfile
, line
);
575 (void) printf("%s: ", func
);
577 (void) vprintf(fmt
, adx
);
583 #endif /* ZFS_DEBUG */
586 * =========================================================================
587 * cmn_err() and panic()
588 * =========================================================================
590 static char ce_prefix
[CE_IGNORE
][10] = { "", "NOTICE: ", "WARNING: ", "" };
591 static char ce_suffix
[CE_IGNORE
][2] = { "", "\n", "\n", "" };
594 vpanic(const char *fmt
, va_list adx
)
596 (void) fprintf(stderr
, "error: ");
597 (void) vfprintf(stderr
, fmt
, adx
);
598 (void) fprintf(stderr
, "\n");
600 abort(); /* think of it as a "user-level crash dump" */
604 panic(const char *fmt
, ...)
614 vcmn_err(int ce
, const char *fmt
, va_list adx
)
618 if (ce
!= CE_NOTE
) { /* suppress noise in userland stress testing */
619 (void) fprintf(stderr
, "%s", ce_prefix
[ce
]);
620 (void) vfprintf(stderr
, fmt
, adx
);
621 (void) fprintf(stderr
, "%s", ce_suffix
[ce
]);
627 cmn_err(int ce
, const char *fmt
, ...)
632 vcmn_err(ce
, fmt
, adx
);
637 * =========================================================================
639 * =========================================================================
642 kobj_open_file(char *name
)
647 /* set vp as the _fd field of the file */
648 if (vn_openat(name
, UIO_SYSSPACE
, FREAD
, 0, &vp
, 0, 0, rootdir
,
650 return ((void *)-1UL);
652 file
= umem_zalloc(sizeof (struct _buf
), UMEM_NOFAIL
);
653 file
->_fd
= (intptr_t)vp
;
658 kobj_read_file(struct _buf
*file
, char *buf
, unsigned size
, unsigned off
)
662 vn_rdwr(UIO_READ
, (vnode_t
*)file
->_fd
, buf
, size
, (offset_t
)off
,
663 UIO_SYSSPACE
, 0, 0, 0, &resid
);
665 return (size
- resid
);
669 kobj_close_file(struct _buf
*file
)
671 vn_close((vnode_t
*)file
->_fd
);
672 umem_free(file
, sizeof (struct _buf
));
676 kobj_get_filesize(struct _buf
*file
, uint64_t *size
)
679 vnode_t
*vp
= (vnode_t
*)file
->_fd
;
681 if (fstat64(vp
->v_fd
, &st
) == -1) {
690 * =========================================================================
692 * =========================================================================
698 poll(0, 0, ticks
* (1000 / hz
));
702 * Find highest one bit set.
703 * Returns bit number + 1 of highest bit that is set, otherwise returns 0.
704 * High order bit is 31 (or 63 in _LP64 kernel).
714 if (i
& 0xffffffff00000000ul
) {
718 if (i
& 0xffff0000) {
736 static int random_fd
= -1, urandom_fd
= -1;
739 random_get_bytes_common(uint8_t *ptr
, size_t len
, int fd
)
747 bytes
= read(fd
, ptr
, resid
);
748 ASSERT3S(bytes
, >=, 0);
757 random_get_bytes(uint8_t *ptr
, size_t len
)
759 return (random_get_bytes_common(ptr
, len
, random_fd
));
763 random_get_pseudo_bytes(uint8_t *ptr
, size_t len
)
765 return (random_get_bytes_common(ptr
, len
, urandom_fd
));
769 ddi_strtoul(const char *hw_serial
, char **nptr
, int base
, unsigned long *result
)
773 *result
= strtoul(hw_serial
, &end
, base
);
780 ddi_strtoull(const char *str
, char **nptr
, int base
, u_longlong_t
*result
)
784 *result
= strtoull(str
, &end
, base
);
791 * =========================================================================
792 * kernel emulation setup & teardown
793 * =========================================================================
796 umem_out_of_memory(void)
798 char errmsg
[] = "out of memory -- generating core dump\n";
800 write(fileno(stderr
), errmsg
, sizeof (errmsg
));
806 kernel_init(int mode
)
808 umem_nofail_callback(umem_out_of_memory
);
810 physmem
= sysconf(_SC_PHYS_PAGES
);
812 dprintf("physmem = %llu pages (%.2f GB)\n", physmem
,
813 (double)physmem
* sysconf(_SC_PAGE_SIZE
) / (1ULL << 30));
815 (void) snprintf(hw_serial
, sizeof (hw_serial
), "%ld",
816 (mode
& FWRITE
) ? gethostid() : 0);
818 VERIFY((random_fd
= open("/dev/random", O_RDONLY
)) != -1);
819 VERIFY((urandom_fd
= open("/dev/urandom", O_RDONLY
)) != -1);
841 z_uncompress(void *dst
, size_t *dstlen
, const void *src
, size_t srclen
)
844 uLongf len
= *dstlen
;
846 if ((ret
= uncompress(dst
, &len
, src
, srclen
)) == Z_OK
)
847 *dstlen
= (size_t)len
;
853 z_compress_level(void *dst
, size_t *dstlen
, const void *src
, size_t srclen
,
857 uLongf len
= *dstlen
;
859 if ((ret
= compress2(dst
, &len
, src
, srclen
, level
)) == Z_OK
)
860 *dstlen
= (size_t)len
;
878 crgetngroups(cred_t
*cr
)
884 crgetgroups(cred_t
*cr
)
890 zfs_secpolicy_snapshot_perms(const char *name
, cred_t
*cr
)
896 zfs_secpolicy_rename_perms(const char *from
, const char *to
, cred_t
*cr
)
902 zfs_secpolicy_destroy_perms(const char *name
, cred_t
*cr
)
908 ksid_lookupdomain(const char *dom
)
912 kd
= umem_zalloc(sizeof (ksiddomain_t
), UMEM_NOFAIL
);
913 kd
->kd_name
= spa_strdup(dom
);
918 ksiddomain_rele(ksiddomain_t
*ksid
)
920 spa_strfree(ksid
->kd_name
);
921 umem_free(ksid
, sizeof (ksiddomain_t
));
925 * Do not change the length of the returned string; it must be freed
929 kmem_asprintf(const char *fmt
, ...)
936 size
= vsnprintf(NULL
, 0, fmt
, adx
) + 1;
939 buf
= kmem_alloc(size
, KM_SLEEP
);
942 size
= vsnprintf(buf
, size
, fmt
, adx
);