]> git.proxmox.com Git - mirror_zfs.git/blob - lib/libzpool/kernel.c
f22ad56b5335565aefda18942419f33d74a3d368
[mirror_zfs.git] / lib / libzpool / kernel.c
1 /*
2 * CDDL HEADER START
3 *
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.
7 *
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.
12 *
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]
18 *
19 * CDDL HEADER END
20 */
21 /*
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2016 Actifio, Inc. All rights reserved.
24 */
25
26 #include <assert.h>
27 #include <fcntl.h>
28 #include <poll.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <zlib.h>
33 #include <libgen.h>
34 #include <sys/signal.h>
35 #include <sys/spa.h>
36 #include <sys/stat.h>
37 #include <sys/processor.h>
38 #include <sys/zfs_context.h>
39 #include <sys/rrwlock.h>
40 #include <sys/utsname.h>
41 #include <sys/time.h>
42 #include <sys/systeminfo.h>
43 #include <zfs_fletcher.h>
44 #include <sys/crypto/icp.h>
45
46 /*
47 * Emulation of kernel services in userland.
48 */
49
50 int aok;
51 uint64_t physmem;
52 vnode_t *rootdir = (vnode_t *)0xabcd1234;
53 char hw_serial[HW_HOSTID_LEN];
54 struct utsname hw_utsname;
55 vmem_t *zio_arena = NULL;
56
57 /* If set, all blocks read will be copied to the specified directory. */
58 char *vn_dumpdir = NULL;
59
60 /* this only exists to have its address taken */
61 struct proc p0;
62
63 /*
64 * =========================================================================
65 * threads
66 * =========================================================================
67 *
68 * TS_STACK_MIN is dictated by the minimum allowed pthread stack size. While
69 * TS_STACK_MAX is somewhat arbitrary, it was selected to be large enough for
70 * the expected stack depth while small enough to avoid exhausting address
71 * space with high thread counts.
72 */
73 #define TS_STACK_MIN MAX(PTHREAD_STACK_MIN, 32768)
74 #define TS_STACK_MAX (256 * 1024)
75
76 /*ARGSUSED*/
77 kthread_t *
78 zk_thread_create(void (*func)(void *), void *arg, size_t stksize, int state)
79 {
80 pthread_attr_t attr;
81 pthread_t tid;
82 char *stkstr;
83 int detachstate = PTHREAD_CREATE_DETACHED;
84
85 VERIFY0(pthread_attr_init(&attr));
86
87 if (state & TS_JOINABLE)
88 detachstate = PTHREAD_CREATE_JOINABLE;
89
90 VERIFY0(pthread_attr_setdetachstate(&attr, detachstate));
91
92 /*
93 * We allow the default stack size in user space to be specified by
94 * setting the ZFS_STACK_SIZE environment variable. This allows us
95 * the convenience of observing and debugging stack overruns in
96 * user space. Explicitly specified stack sizes will be honored.
97 * The usage of ZFS_STACK_SIZE is discussed further in the
98 * ENVIRONMENT VARIABLES sections of the ztest(1) man page.
99 */
100 if (stksize == 0) {
101 stkstr = getenv("ZFS_STACK_SIZE");
102
103 if (stkstr == NULL)
104 stksize = TS_STACK_MAX;
105 else
106 stksize = MAX(atoi(stkstr), TS_STACK_MIN);
107 }
108
109 VERIFY3S(stksize, >, 0);
110 stksize = P2ROUNDUP(MAX(stksize, TS_STACK_MIN), PAGESIZE);
111
112 /*
113 * If this ever fails, it may be because the stack size is not a
114 * multiple of system page size.
115 */
116 VERIFY0(pthread_attr_setstacksize(&attr, stksize));
117 VERIFY0(pthread_attr_setguardsize(&attr, PAGESIZE));
118
119 VERIFY0(pthread_create(&tid, &attr, (void *(*)(void *))func, arg));
120 VERIFY0(pthread_attr_destroy(&attr));
121
122 return ((void *)(uintptr_t)tid);
123 }
124
125 /*
126 * =========================================================================
127 * kstats
128 * =========================================================================
129 */
130 /*ARGSUSED*/
131 kstat_t *
132 kstat_create(const char *module, int instance, const char *name,
133 const char *class, uchar_t type, ulong_t ndata, uchar_t ks_flag)
134 {
135 return (NULL);
136 }
137
138 /*ARGSUSED*/
139 void
140 kstat_install(kstat_t *ksp)
141 {}
142
143 /*ARGSUSED*/
144 void
145 kstat_delete(kstat_t *ksp)
146 {}
147
148 /*ARGSUSED*/
149 void
150 kstat_waitq_enter(kstat_io_t *kiop)
151 {}
152
153 /*ARGSUSED*/
154 void
155 kstat_waitq_exit(kstat_io_t *kiop)
156 {}
157
158 /*ARGSUSED*/
159 void
160 kstat_runq_enter(kstat_io_t *kiop)
161 {}
162
163 /*ARGSUSED*/
164 void
165 kstat_runq_exit(kstat_io_t *kiop)
166 {}
167
168 /*ARGSUSED*/
169 void
170 kstat_waitq_to_runq(kstat_io_t *kiop)
171 {}
172
173 /*ARGSUSED*/
174 void
175 kstat_runq_back_to_waitq(kstat_io_t *kiop)
176 {}
177
178 void
179 kstat_set_raw_ops(kstat_t *ksp,
180 int (*headers)(char *buf, size_t size),
181 int (*data)(char *buf, size_t size, void *data),
182 void *(*addr)(kstat_t *ksp, loff_t index))
183 {}
184
185 /*
186 * =========================================================================
187 * mutexes
188 * =========================================================================
189 */
190
191 void
192 mutex_init(kmutex_t *mp, char *name, int type, void *cookie)
193 {
194 VERIFY0(pthread_mutex_init(&mp->m_lock, NULL));
195 memset(&mp->m_owner, 0, sizeof (pthread_t));
196 }
197
198 void
199 mutex_destroy(kmutex_t *mp)
200 {
201 VERIFY0(pthread_mutex_destroy(&mp->m_lock));
202 }
203
204 void
205 mutex_enter(kmutex_t *mp)
206 {
207 VERIFY0(pthread_mutex_lock(&mp->m_lock));
208 mp->m_owner = pthread_self();
209 }
210
211 int
212 mutex_tryenter(kmutex_t *mp)
213 {
214 int error;
215
216 error = pthread_mutex_trylock(&mp->m_lock);
217 if (error == 0) {
218 mp->m_owner = pthread_self();
219 return (1);
220 } else {
221 VERIFY3S(error, ==, EBUSY);
222 return (0);
223 }
224 }
225
226 void
227 mutex_exit(kmutex_t *mp)
228 {
229 memset(&mp->m_owner, 0, sizeof (pthread_t));
230 VERIFY0(pthread_mutex_unlock(&mp->m_lock));
231 }
232
233 /*
234 * =========================================================================
235 * rwlocks
236 * =========================================================================
237 */
238
239 void
240 rw_init(krwlock_t *rwlp, char *name, int type, void *arg)
241 {
242 VERIFY0(pthread_rwlock_init(&rwlp->rw_lock, NULL));
243 rwlp->rw_readers = 0;
244 rwlp->rw_owner = 0;
245 }
246
247 void
248 rw_destroy(krwlock_t *rwlp)
249 {
250 VERIFY0(pthread_rwlock_destroy(&rwlp->rw_lock));
251 }
252
253 void
254 rw_enter(krwlock_t *rwlp, krw_t rw)
255 {
256 if (rw == RW_READER) {
257 VERIFY0(pthread_rwlock_rdlock(&rwlp->rw_lock));
258 atomic_inc_uint(&rwlp->rw_readers);
259 } else {
260 VERIFY0(pthread_rwlock_wrlock(&rwlp->rw_lock));
261 rwlp->rw_owner = pthread_self();
262 }
263 }
264
265 void
266 rw_exit(krwlock_t *rwlp)
267 {
268 if (RW_READ_HELD(rwlp))
269 atomic_dec_uint(&rwlp->rw_readers);
270 else
271 rwlp->rw_owner = 0;
272
273 VERIFY0(pthread_rwlock_unlock(&rwlp->rw_lock));
274 }
275
276 int
277 rw_tryenter(krwlock_t *rwlp, krw_t rw)
278 {
279 int error;
280
281 if (rw == RW_READER)
282 error = pthread_rwlock_tryrdlock(&rwlp->rw_lock);
283 else
284 error = pthread_rwlock_trywrlock(&rwlp->rw_lock);
285
286 if (error == 0) {
287 if (rw == RW_READER)
288 atomic_inc_uint(&rwlp->rw_readers);
289 else
290 rwlp->rw_owner = pthread_self();
291
292 return (1);
293 }
294
295 VERIFY3S(error, ==, EBUSY);
296
297 return (0);
298 }
299
300 /* ARGSUSED */
301 uint32_t
302 zone_get_hostid(void *zonep)
303 {
304 /*
305 * We're emulating the system's hostid in userland.
306 */
307 return (strtoul(hw_serial, NULL, 10));
308 }
309
310 int
311 rw_tryupgrade(krwlock_t *rwlp)
312 {
313 return (0);
314 }
315
316 /*
317 * =========================================================================
318 * condition variables
319 * =========================================================================
320 */
321
322 void
323 cv_init(kcondvar_t *cv, char *name, int type, void *arg)
324 {
325 VERIFY0(pthread_cond_init(cv, NULL));
326 }
327
328 void
329 cv_destroy(kcondvar_t *cv)
330 {
331 VERIFY0(pthread_cond_destroy(cv));
332 }
333
334 void
335 cv_wait(kcondvar_t *cv, kmutex_t *mp)
336 {
337 memset(&mp->m_owner, 0, sizeof (pthread_t));
338 VERIFY0(pthread_cond_wait(cv, &mp->m_lock));
339 mp->m_owner = pthread_self();
340 }
341
342 clock_t
343 cv_timedwait(kcondvar_t *cv, kmutex_t *mp, clock_t abstime)
344 {
345 int error;
346 struct timeval tv;
347 struct timespec ts;
348 clock_t delta;
349
350 delta = abstime - ddi_get_lbolt();
351 if (delta <= 0)
352 return (-1);
353
354 VERIFY(gettimeofday(&tv, NULL) == 0);
355
356 ts.tv_sec = tv.tv_sec + delta / hz;
357 ts.tv_nsec = tv.tv_usec * NSEC_PER_USEC + (delta % hz) * (NANOSEC / hz);
358 if (ts.tv_nsec >= NANOSEC) {
359 ts.tv_sec++;
360 ts.tv_nsec -= NANOSEC;
361 }
362
363 memset(&mp->m_owner, 0, sizeof (pthread_t));
364 error = pthread_cond_timedwait(cv, &mp->m_lock, &ts);
365 mp->m_owner = pthread_self();
366
367 if (error == ETIMEDOUT)
368 return (-1);
369
370 VERIFY0(error);
371
372 return (1);
373 }
374
375 /*ARGSUSED*/
376 clock_t
377 cv_timedwait_hires(kcondvar_t *cv, kmutex_t *mp, hrtime_t tim, hrtime_t res,
378 int flag)
379 {
380 int error;
381 struct timeval tv;
382 struct timespec ts;
383 hrtime_t delta;
384
385 ASSERT(flag == 0 || flag == CALLOUT_FLAG_ABSOLUTE);
386
387 delta = tim;
388 if (flag & CALLOUT_FLAG_ABSOLUTE)
389 delta -= gethrtime();
390
391 if (delta <= 0)
392 return (-1);
393
394 VERIFY0(gettimeofday(&tv, NULL));
395
396 ts.tv_sec = tv.tv_sec + delta / NANOSEC;
397 ts.tv_nsec = tv.tv_usec * NSEC_PER_USEC + (delta % NANOSEC);
398 if (ts.tv_nsec >= NANOSEC) {
399 ts.tv_sec++;
400 ts.tv_nsec -= NANOSEC;
401 }
402
403 memset(&mp->m_owner, 0, sizeof (pthread_t));
404 error = pthread_cond_timedwait(cv, &mp->m_lock, &ts);
405 mp->m_owner = pthread_self();
406
407 if (error == ETIMEDOUT)
408 return (-1);
409
410 VERIFY0(error);
411
412 return (1);
413 }
414
415 void
416 cv_signal(kcondvar_t *cv)
417 {
418 VERIFY0(pthread_cond_signal(cv));
419 }
420
421 void
422 cv_broadcast(kcondvar_t *cv)
423 {
424 VERIFY0(pthread_cond_broadcast(cv));
425 }
426
427 /*
428 * =========================================================================
429 * procfs list
430 * =========================================================================
431 */
432
433 void
434 seq_printf(struct seq_file *m, const char *fmt, ...)
435 {}
436
437 void
438 procfs_list_install(const char *module,
439 const char *name,
440 mode_t mode,
441 procfs_list_t *procfs_list,
442 int (*show)(struct seq_file *f, void *p),
443 int (*show_header)(struct seq_file *f),
444 int (*clear)(procfs_list_t *procfs_list),
445 size_t procfs_list_node_off)
446 {
447 mutex_init(&procfs_list->pl_lock, NULL, MUTEX_DEFAULT, NULL);
448 list_create(&procfs_list->pl_list,
449 procfs_list_node_off + sizeof (procfs_list_node_t),
450 procfs_list_node_off + offsetof(procfs_list_node_t, pln_link));
451 procfs_list->pl_next_id = 1;
452 procfs_list->pl_node_offset = procfs_list_node_off;
453 }
454
455 void
456 procfs_list_uninstall(procfs_list_t *procfs_list)
457 {}
458
459 void
460 procfs_list_destroy(procfs_list_t *procfs_list)
461 {
462 ASSERT(list_is_empty(&procfs_list->pl_list));
463 list_destroy(&procfs_list->pl_list);
464 mutex_destroy(&procfs_list->pl_lock);
465 }
466
467 #define NODE_ID(procfs_list, obj) \
468 (((procfs_list_node_t *)(((char *)obj) + \
469 (procfs_list)->pl_node_offset))->pln_id)
470
471 void
472 procfs_list_add(procfs_list_t *procfs_list, void *p)
473 {
474 ASSERT(MUTEX_HELD(&procfs_list->pl_lock));
475 NODE_ID(procfs_list, p) = procfs_list->pl_next_id++;
476 list_insert_tail(&procfs_list->pl_list, p);
477 }
478
479 /*
480 * =========================================================================
481 * vnode operations
482 * =========================================================================
483 */
484 /*
485 * Note: for the xxxat() versions of these functions, we assume that the
486 * starting vp is always rootdir (which is true for spa_directory.c, the only
487 * ZFS consumer of these interfaces). We assert this is true, and then emulate
488 * them by adding '/' in front of the path.
489 */
490
491 /*ARGSUSED*/
492 int
493 vn_open(char *path, int x1, int flags, int mode, vnode_t **vpp, int x2, int x3)
494 {
495 int fd = -1;
496 int dump_fd = -1;
497 vnode_t *vp;
498 int old_umask = 0;
499 char *realpath;
500 struct stat64 st;
501 int err;
502
503 realpath = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
504
505 /*
506 * If we're accessing a real disk from userland, we need to use
507 * the character interface to avoid caching. This is particularly
508 * important if we're trying to look at a real in-kernel storage
509 * pool from userland, e.g. via zdb, because otherwise we won't
510 * see the changes occurring under the segmap cache.
511 * On the other hand, the stupid character device returns zero
512 * for its size. So -- gag -- we open the block device to get
513 * its size, and remember it for subsequent VOP_GETATTR().
514 */
515 #if defined(__sun__) || defined(__sun)
516 if (strncmp(path, "/dev/", 5) == 0) {
517 #else
518 if (0) {
519 #endif
520 char *dsk;
521 fd = open64(path, O_RDONLY);
522 if (fd == -1) {
523 err = errno;
524 free(realpath);
525 return (err);
526 }
527 if (fstat64(fd, &st) == -1) {
528 err = errno;
529 close(fd);
530 free(realpath);
531 return (err);
532 }
533 close(fd);
534 (void) sprintf(realpath, "%s", path);
535 dsk = strstr(path, "/dsk/");
536 if (dsk != NULL)
537 (void) sprintf(realpath + (dsk - path) + 1, "r%s",
538 dsk + 1);
539 } else {
540 (void) sprintf(realpath, "%s", path);
541 if (!(flags & FCREAT) && stat64(realpath, &st) == -1) {
542 err = errno;
543 free(realpath);
544 return (err);
545 }
546 }
547
548 if (!(flags & FCREAT) && S_ISBLK(st.st_mode)) {
549 #ifdef __linux__
550 flags |= O_DIRECT;
551 #endif
552 }
553
554 if (flags & FCREAT)
555 old_umask = umask(0);
556
557 /*
558 * The construct 'flags - FREAD' conveniently maps combinations of
559 * FREAD and FWRITE to the corresponding O_RDONLY, O_WRONLY, and O_RDWR.
560 */
561 fd = open64(realpath, flags - FREAD, mode);
562 if (fd == -1) {
563 err = errno;
564 free(realpath);
565 return (err);
566 }
567
568 if (flags & FCREAT)
569 (void) umask(old_umask);
570
571 if (vn_dumpdir != NULL) {
572 char *dumppath = umem_zalloc(MAXPATHLEN, UMEM_NOFAIL);
573 (void) snprintf(dumppath, MAXPATHLEN,
574 "%s/%s", vn_dumpdir, basename(realpath));
575 dump_fd = open64(dumppath, O_CREAT | O_WRONLY, 0666);
576 umem_free(dumppath, MAXPATHLEN);
577 if (dump_fd == -1) {
578 err = errno;
579 free(realpath);
580 close(fd);
581 return (err);
582 }
583 } else {
584 dump_fd = -1;
585 }
586
587 free(realpath);
588
589 if (fstat64_blk(fd, &st) == -1) {
590 err = errno;
591 close(fd);
592 if (dump_fd != -1)
593 close(dump_fd);
594 return (err);
595 }
596
597 (void) fcntl(fd, F_SETFD, FD_CLOEXEC);
598
599 *vpp = vp = umem_zalloc(sizeof (vnode_t), UMEM_NOFAIL);
600
601 vp->v_fd = fd;
602 vp->v_size = st.st_size;
603 vp->v_path = spa_strdup(path);
604 vp->v_dump_fd = dump_fd;
605
606 return (0);
607 }
608
609 /*ARGSUSED*/
610 int
611 vn_openat(char *path, int x1, int flags, int mode, vnode_t **vpp, int x2,
612 int x3, vnode_t *startvp, int fd)
613 {
614 char *realpath = umem_alloc(strlen(path) + 2, UMEM_NOFAIL);
615 int ret;
616
617 ASSERT(startvp == rootdir);
618 (void) sprintf(realpath, "/%s", path);
619
620 /* fd ignored for now, need if want to simulate nbmand support */
621 ret = vn_open(realpath, x1, flags, mode, vpp, x2, x3);
622
623 umem_free(realpath, strlen(path) + 2);
624
625 return (ret);
626 }
627
628 /*ARGSUSED*/
629 int
630 vn_rdwr(int uio, vnode_t *vp, void *addr, ssize_t len, offset_t offset,
631 int x1, int x2, rlim64_t x3, void *x4, ssize_t *residp)
632 {
633 ssize_t rc, done = 0, split;
634
635 if (uio == UIO_READ) {
636 rc = pread64(vp->v_fd, addr, len, offset);
637 if (vp->v_dump_fd != -1 && rc != -1) {
638 int status;
639 status = pwrite64(vp->v_dump_fd, addr, rc, offset);
640 ASSERT(status != -1);
641 }
642 } else {
643 /*
644 * To simulate partial disk writes, we split writes into two
645 * system calls so that the process can be killed in between.
646 */
647 int sectors = len >> SPA_MINBLOCKSHIFT;
648 split = (sectors > 0 ? rand() % sectors : 0) <<
649 SPA_MINBLOCKSHIFT;
650 rc = pwrite64(vp->v_fd, addr, split, offset);
651 if (rc != -1) {
652 done = rc;
653 rc = pwrite64(vp->v_fd, (char *)addr + split,
654 len - split, offset + split);
655 }
656 }
657
658 #ifdef __linux__
659 if (rc == -1 && errno == EINVAL) {
660 /*
661 * Under Linux, this most likely means an alignment issue
662 * (memory or disk) due to O_DIRECT, so we abort() in order to
663 * catch the offender.
664 */
665 abort();
666 }
667 #endif
668 if (rc == -1)
669 return (errno);
670
671 done += rc;
672
673 if (residp)
674 *residp = len - done;
675 else if (done != len)
676 return (EIO);
677 return (0);
678 }
679
680 void
681 vn_close(vnode_t *vp)
682 {
683 close(vp->v_fd);
684 if (vp->v_dump_fd != -1)
685 close(vp->v_dump_fd);
686 spa_strfree(vp->v_path);
687 umem_free(vp, sizeof (vnode_t));
688 }
689
690 /*
691 * At a minimum we need to update the size since vdev_reopen()
692 * will no longer call vn_openat().
693 */
694 int
695 fop_getattr(vnode_t *vp, vattr_t *vap)
696 {
697 struct stat64 st;
698 int err;
699
700 if (fstat64_blk(vp->v_fd, &st) == -1) {
701 err = errno;
702 close(vp->v_fd);
703 return (err);
704 }
705
706 vap->va_size = st.st_size;
707 return (0);
708 }
709
710 /*
711 * =========================================================================
712 * Figure out which debugging statements to print
713 * =========================================================================
714 */
715
716 static char *dprintf_string;
717 static int dprintf_print_all;
718
719 int
720 dprintf_find_string(const char *string)
721 {
722 char *tmp_str = dprintf_string;
723 int len = strlen(string);
724
725 /*
726 * Find out if this is a string we want to print.
727 * String format: file1.c,function_name1,file2.c,file3.c
728 */
729
730 while (tmp_str != NULL) {
731 if (strncmp(tmp_str, string, len) == 0 &&
732 (tmp_str[len] == ',' || tmp_str[len] == '\0'))
733 return (1);
734 tmp_str = strchr(tmp_str, ',');
735 if (tmp_str != NULL)
736 tmp_str++; /* Get rid of , */
737 }
738 return (0);
739 }
740
741 void
742 dprintf_setup(int *argc, char **argv)
743 {
744 int i, j;
745
746 /*
747 * Debugging can be specified two ways: by setting the
748 * environment variable ZFS_DEBUG, or by including a
749 * "debug=..." argument on the command line. The command
750 * line setting overrides the environment variable.
751 */
752
753 for (i = 1; i < *argc; i++) {
754 int len = strlen("debug=");
755 /* First look for a command line argument */
756 if (strncmp("debug=", argv[i], len) == 0) {
757 dprintf_string = argv[i] + len;
758 /* Remove from args */
759 for (j = i; j < *argc; j++)
760 argv[j] = argv[j+1];
761 argv[j] = NULL;
762 (*argc)--;
763 }
764 }
765
766 if (dprintf_string == NULL) {
767 /* Look for ZFS_DEBUG environment variable */
768 dprintf_string = getenv("ZFS_DEBUG");
769 }
770
771 /*
772 * Are we just turning on all debugging?
773 */
774 if (dprintf_find_string("on"))
775 dprintf_print_all = 1;
776
777 if (dprintf_string != NULL)
778 zfs_flags |= ZFS_DEBUG_DPRINTF;
779 }
780
781 /*
782 * =========================================================================
783 * debug printfs
784 * =========================================================================
785 */
786 void
787 __dprintf(boolean_t dprint, const char *file, const char *func,
788 int line, const char *fmt, ...)
789 {
790 const char *newfile;
791 va_list adx;
792
793 /*
794 * Get rid of annoying "../common/" prefix to filename.
795 */
796 newfile = strrchr(file, '/');
797 if (newfile != NULL) {
798 newfile = newfile + 1; /* Get rid of leading / */
799 } else {
800 newfile = file;
801 }
802
803 if (dprint) {
804 /* dprintf messages are printed immediately */
805
806 if (!dprintf_print_all &&
807 !dprintf_find_string(newfile) &&
808 !dprintf_find_string(func))
809 return;
810
811 /* Print out just the function name if requested */
812 flockfile(stdout);
813 if (dprintf_find_string("pid"))
814 (void) printf("%d ", getpid());
815 if (dprintf_find_string("tid"))
816 (void) printf("%u ", (uint_t)pthread_self());
817 if (dprintf_find_string("cpu"))
818 (void) printf("%u ", getcpuid());
819 if (dprintf_find_string("time"))
820 (void) printf("%llu ", gethrtime());
821 if (dprintf_find_string("long"))
822 (void) printf("%s, line %d: ", newfile, line);
823 (void) printf("dprintf: %s: ", func);
824 va_start(adx, fmt);
825 (void) vprintf(fmt, adx);
826 va_end(adx);
827 funlockfile(stdout);
828 } else {
829 /* zfs_dbgmsg is logged for dumping later */
830 size_t size;
831 char *buf;
832 int i;
833
834 size = 1024;
835 buf = umem_alloc(size, UMEM_NOFAIL);
836 i = snprintf(buf, size, "%s:%d:%s(): ", newfile, line, func);
837
838 if (i < size) {
839 va_start(adx, fmt);
840 (void) vsnprintf(buf + i, size - i, fmt, adx);
841 va_end(adx);
842 }
843
844 __zfs_dbgmsg(buf);
845
846 umem_free(buf, size);
847 }
848 }
849
850 /*
851 * =========================================================================
852 * cmn_err() and panic()
853 * =========================================================================
854 */
855 static char ce_prefix[CE_IGNORE][10] = { "", "NOTICE: ", "WARNING: ", "" };
856 static char ce_suffix[CE_IGNORE][2] = { "", "\n", "\n", "" };
857
858 void
859 vpanic(const char *fmt, va_list adx)
860 {
861 (void) fprintf(stderr, "error: ");
862 (void) vfprintf(stderr, fmt, adx);
863 (void) fprintf(stderr, "\n");
864
865 abort(); /* think of it as a "user-level crash dump" */
866 }
867
868 void
869 panic(const char *fmt, ...)
870 {
871 va_list adx;
872
873 va_start(adx, fmt);
874 vpanic(fmt, adx);
875 va_end(adx);
876 }
877
878 void
879 vcmn_err(int ce, const char *fmt, va_list adx)
880 {
881 if (ce == CE_PANIC)
882 vpanic(fmt, adx);
883 if (ce != CE_NOTE) { /* suppress noise in userland stress testing */
884 (void) fprintf(stderr, "%s", ce_prefix[ce]);
885 (void) vfprintf(stderr, fmt, adx);
886 (void) fprintf(stderr, "%s", ce_suffix[ce]);
887 }
888 }
889
890 /*PRINTFLIKE2*/
891 void
892 cmn_err(int ce, const char *fmt, ...)
893 {
894 va_list adx;
895
896 va_start(adx, fmt);
897 vcmn_err(ce, fmt, adx);
898 va_end(adx);
899 }
900
901 /*
902 * =========================================================================
903 * kobj interfaces
904 * =========================================================================
905 */
906 struct _buf *
907 kobj_open_file(char *name)
908 {
909 struct _buf *file;
910 vnode_t *vp;
911
912 /* set vp as the _fd field of the file */
913 if (vn_openat(name, UIO_SYSSPACE, FREAD, 0, &vp, 0, 0, rootdir,
914 -1) != 0)
915 return ((void *)-1UL);
916
917 file = umem_zalloc(sizeof (struct _buf), UMEM_NOFAIL);
918 file->_fd = (intptr_t)vp;
919 return (file);
920 }
921
922 int
923 kobj_read_file(struct _buf *file, char *buf, unsigned size, unsigned off)
924 {
925 ssize_t resid = 0;
926
927 if (vn_rdwr(UIO_READ, (vnode_t *)file->_fd, buf, size, (offset_t)off,
928 UIO_SYSSPACE, 0, 0, 0, &resid) != 0)
929 return (-1);
930
931 return (size - resid);
932 }
933
934 void
935 kobj_close_file(struct _buf *file)
936 {
937 vn_close((vnode_t *)file->_fd);
938 umem_free(file, sizeof (struct _buf));
939 }
940
941 int
942 kobj_get_filesize(struct _buf *file, uint64_t *size)
943 {
944 struct stat64 st;
945 vnode_t *vp = (vnode_t *)file->_fd;
946
947 if (fstat64(vp->v_fd, &st) == -1) {
948 vn_close(vp);
949 return (errno);
950 }
951 *size = st.st_size;
952 return (0);
953 }
954
955 /*
956 * =========================================================================
957 * misc routines
958 * =========================================================================
959 */
960
961 void
962 delay(clock_t ticks)
963 {
964 (void) poll(0, 0, ticks * (1000 / hz));
965 }
966
967 /*
968 * Find highest one bit set.
969 * Returns bit number + 1 of highest bit that is set, otherwise returns 0.
970 * The __builtin_clzll() function is supported by both GCC and Clang.
971 */
972 int
973 highbit64(uint64_t i)
974 {
975 if (i == 0)
976 return (0);
977
978 return (NBBY * sizeof (uint64_t) - __builtin_clzll(i));
979 }
980
981 /*
982 * Find lowest one bit set.
983 * Returns bit number + 1 of lowest bit that is set, otherwise returns 0.
984 * The __builtin_ffsll() function is supported by both GCC and Clang.
985 */
986 int
987 lowbit64(uint64_t i)
988 {
989 if (i == 0)
990 return (0);
991
992 return (__builtin_ffsll(i));
993 }
994
995 char *random_path = "/dev/random";
996 char *urandom_path = "/dev/urandom";
997 static int random_fd = -1, urandom_fd = -1;
998
999 void
1000 random_init(void)
1001 {
1002 VERIFY((random_fd = open(random_path, O_RDONLY)) != -1);
1003 VERIFY((urandom_fd = open(urandom_path, O_RDONLY)) != -1);
1004 }
1005
1006 void
1007 random_fini(void)
1008 {
1009 close(random_fd);
1010 close(urandom_fd);
1011
1012 random_fd = -1;
1013 urandom_fd = -1;
1014 }
1015
1016 static int
1017 random_get_bytes_common(uint8_t *ptr, size_t len, int fd)
1018 {
1019 size_t resid = len;
1020 ssize_t bytes;
1021
1022 ASSERT(fd != -1);
1023
1024 while (resid != 0) {
1025 bytes = read(fd, ptr, resid);
1026 ASSERT3S(bytes, >=, 0);
1027 ptr += bytes;
1028 resid -= bytes;
1029 }
1030
1031 return (0);
1032 }
1033
1034 int
1035 random_get_bytes(uint8_t *ptr, size_t len)
1036 {
1037 return (random_get_bytes_common(ptr, len, random_fd));
1038 }
1039
1040 int
1041 random_get_pseudo_bytes(uint8_t *ptr, size_t len)
1042 {
1043 return (random_get_bytes_common(ptr, len, urandom_fd));
1044 }
1045
1046 int
1047 ddi_strtoul(const char *hw_serial, char **nptr, int base, unsigned long *result)
1048 {
1049 char *end;
1050
1051 *result = strtoul(hw_serial, &end, base);
1052 if (*result == 0)
1053 return (errno);
1054 return (0);
1055 }
1056
1057 int
1058 ddi_strtoull(const char *str, char **nptr, int base, u_longlong_t *result)
1059 {
1060 char *end;
1061
1062 *result = strtoull(str, &end, base);
1063 if (*result == 0)
1064 return (errno);
1065 return (0);
1066 }
1067
1068 utsname_t *
1069 utsname(void)
1070 {
1071 return (&hw_utsname);
1072 }
1073
1074 /*
1075 * =========================================================================
1076 * kernel emulation setup & teardown
1077 * =========================================================================
1078 */
1079 static int
1080 umem_out_of_memory(void)
1081 {
1082 char errmsg[] = "out of memory -- generating core dump\n";
1083
1084 (void) fprintf(stderr, "%s", errmsg);
1085 abort();
1086 return (0);
1087 }
1088
1089 void
1090 kernel_init(int mode)
1091 {
1092 extern uint_t rrw_tsd_key;
1093
1094 umem_nofail_callback(umem_out_of_memory);
1095
1096 physmem = sysconf(_SC_PHYS_PAGES);
1097
1098 dprintf("physmem = %llu pages (%.2f GB)\n", physmem,
1099 (double)physmem * sysconf(_SC_PAGE_SIZE) / (1ULL << 30));
1100
1101 (void) snprintf(hw_serial, sizeof (hw_serial), "%ld",
1102 (mode & FWRITE) ? get_system_hostid() : 0);
1103
1104 random_init();
1105
1106 VERIFY0(uname(&hw_utsname));
1107
1108 system_taskq_init();
1109 icp_init();
1110
1111 spa_init(mode);
1112
1113 fletcher_4_init();
1114
1115 tsd_create(&rrw_tsd_key, rrw_tsd_destroy);
1116 }
1117
1118 void
1119 kernel_fini(void)
1120 {
1121 fletcher_4_fini();
1122 spa_fini();
1123
1124 icp_fini();
1125 system_taskq_fini();
1126
1127 random_fini();
1128 }
1129
1130 uid_t
1131 crgetuid(cred_t *cr)
1132 {
1133 return (0);
1134 }
1135
1136 uid_t
1137 crgetruid(cred_t *cr)
1138 {
1139 return (0);
1140 }
1141
1142 gid_t
1143 crgetgid(cred_t *cr)
1144 {
1145 return (0);
1146 }
1147
1148 int
1149 crgetngroups(cred_t *cr)
1150 {
1151 return (0);
1152 }
1153
1154 gid_t *
1155 crgetgroups(cred_t *cr)
1156 {
1157 return (NULL);
1158 }
1159
1160 int
1161 zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
1162 {
1163 return (0);
1164 }
1165
1166 int
1167 zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
1168 {
1169 return (0);
1170 }
1171
1172 int
1173 zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
1174 {
1175 return (0);
1176 }
1177
1178 int
1179 secpolicy_zfs(const cred_t *cr)
1180 {
1181 return (0);
1182 }
1183
1184 ksiddomain_t *
1185 ksid_lookupdomain(const char *dom)
1186 {
1187 ksiddomain_t *kd;
1188
1189 kd = umem_zalloc(sizeof (ksiddomain_t), UMEM_NOFAIL);
1190 kd->kd_name = spa_strdup(dom);
1191 return (kd);
1192 }
1193
1194 void
1195 ksiddomain_rele(ksiddomain_t *ksid)
1196 {
1197 spa_strfree(ksid->kd_name);
1198 umem_free(ksid, sizeof (ksiddomain_t));
1199 }
1200
1201 char *
1202 kmem_vasprintf(const char *fmt, va_list adx)
1203 {
1204 char *buf = NULL;
1205 va_list adx_copy;
1206
1207 va_copy(adx_copy, adx);
1208 VERIFY(vasprintf(&buf, fmt, adx_copy) != -1);
1209 va_end(adx_copy);
1210
1211 return (buf);
1212 }
1213
1214 char *
1215 kmem_asprintf(const char *fmt, ...)
1216 {
1217 char *buf = NULL;
1218 va_list adx;
1219
1220 va_start(adx, fmt);
1221 VERIFY(vasprintf(&buf, fmt, adx) != -1);
1222 va_end(adx);
1223
1224 return (buf);
1225 }
1226
1227 /* ARGSUSED */
1228 int
1229 zfs_onexit_fd_hold(int fd, minor_t *minorp)
1230 {
1231 *minorp = 0;
1232 return (0);
1233 }
1234
1235 /* ARGSUSED */
1236 void
1237 zfs_onexit_fd_rele(int fd)
1238 {
1239 }
1240
1241 /* ARGSUSED */
1242 int
1243 zfs_onexit_add_cb(minor_t minor, void (*func)(void *), void *data,
1244 uint64_t *action_handle)
1245 {
1246 return (0);
1247 }
1248
1249 /* ARGSUSED */
1250 int
1251 zfs_onexit_del_cb(minor_t minor, uint64_t action_handle, boolean_t fire)
1252 {
1253 return (0);
1254 }
1255
1256 /* ARGSUSED */
1257 int
1258 zfs_onexit_cb_data(minor_t minor, uint64_t action_handle, void **data)
1259 {
1260 return (0);
1261 }
1262
1263 fstrans_cookie_t
1264 spl_fstrans_mark(void)
1265 {
1266 return ((fstrans_cookie_t)0);
1267 }
1268
1269 void
1270 spl_fstrans_unmark(fstrans_cookie_t cookie)
1271 {
1272 }
1273
1274 int
1275 __spl_pf_fstrans_check(void)
1276 {
1277 return (0);
1278 }
1279
1280 int
1281 kmem_cache_reap_active(void)
1282 {
1283 return (0);
1284 }
1285
1286 void *zvol_tag = "zvol_tag";
1287
1288 void
1289 zvol_create_minors(spa_t *spa, const char *name, boolean_t async)
1290 {
1291 }
1292
1293 void
1294 zvol_remove_minor(spa_t *spa, const char *name, boolean_t async)
1295 {
1296 }
1297
1298 void
1299 zvol_remove_minors(spa_t *spa, const char *name, boolean_t async)
1300 {
1301 }
1302
1303 void
1304 zvol_rename_minors(spa_t *spa, const char *oldname, const char *newname,
1305 boolean_t async)
1306 {
1307 }