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