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