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