1 /* 2 * Copyright (C) 2007-2010 Lawrence Livermore National Security, LLC. 3 * Copyright (C) 2007 The Regents of the University of California. 4 * Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER). 5 * Written by Brian Behlendorf <behlendorf1@llnl.gov>. 6 * UCRL-CODE-235197 7 * 8 * This file is part of the SPL, Solaris Porting Layer. 9 * For details, see <http://zfsonlinux.org/>. 10 * 11 * The SPL is free software; you can redistribute it and/or modify it 12 * under the terms of the GNU General Public License as published by the 13 * Free Software Foundation; either version 2 of the License, or (at your 14 * option) any later version. 15 * 16 * The SPL is distributed in the hope that it will be useful, but WITHOUT 17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 18 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 19 * for more details. 20 * 21 * You should have received a copy of the GNU General Public License along 22 * with the SPL. If not, see <http://www.gnu.org/licenses/>. 23 * 24 * Solaris Porting Layer (SPL) Generic Implementation. 25 */ 26 27 #include <sys/sysmacros.h> 28 #include <sys/systeminfo.h> 29 #include <sys/vmsystm.h> 30 #include <sys/kmem.h> 31 #include <sys/kmem_cache.h> 32 #include <sys/vmem.h> 33 #include <sys/mutex.h> 34 #include <sys/rwlock.h> 35 #include <sys/taskq.h> 36 #include <sys/tsd.h> 37 #include <sys/zmod.h> 38 #include <sys/debug.h> 39 #include <sys/proc.h> 40 #include <sys/kstat.h> 41 #include <sys/file.h> 42 #include <sys/sunddi.h> 43 #include <linux/ctype.h> 44 #include <sys/disp.h> 45 #include <sys/random.h> 46 #include <sys/strings.h> 47 #include <linux/kmod.h> 48 #include "zfs_gitrev.h" 49 #include <linux/mod_compat.h> 50 #include <sys/cred.h> 51 #include <sys/vnode.h> 52 53 char spl_gitrev[64] = ZFS_META_GITREV; 54 55 /* BEGIN CSTYLED */ 56 unsigned long spl_hostid = 0; 57 EXPORT_SYMBOL(spl_hostid); 58 /* BEGIN CSTYLED */ 59 module_param(spl_hostid, ulong, 0644); 60 MODULE_PARM_DESC(spl_hostid, "The system hostid."); 61 /* END CSTYLED */ 62 63 proc_t p0; 64 EXPORT_SYMBOL(p0); 65 66 /* 67 * Xorshift Pseudo Random Number Generator based on work by Sebastiano Vigna 68 * 69 * "Further scramblings of Marsaglia's xorshift generators" 70 * http://vigna.di.unimi.it/ftp/papers/xorshiftplus.pdf 71 * 72 * random_get_pseudo_bytes() is an API function on Illumos whose sole purpose 73 * is to provide bytes containing random numbers. It is mapped to /dev/urandom 74 * on Illumos, which uses a "FIPS 186-2 algorithm". No user of the SPL's 75 * random_get_pseudo_bytes() needs bytes that are of cryptographic quality, so 76 * we can implement it using a fast PRNG that we seed using Linux' actual 77 * equivalent to random_get_pseudo_bytes(). We do this by providing each CPU 78 * with an independent seed so that all calls to random_get_pseudo_bytes() are 79 * free of atomic instructions. 80 * 81 * A consequence of using a fast PRNG is that using random_get_pseudo_bytes() 82 * to generate words larger than 128 bits will paradoxically be limited to 83 * `2^128 - 1` possibilities. This is because we have a sequence of `2^128 - 1` 84 * 128-bit words and selecting the first will implicitly select the second. If 85 * a caller finds this behavior undesirable, random_get_bytes() should be used 86 * instead. 87 * 88 * XXX: Linux interrupt handlers that trigger within the critical section 89 * formed by `s[1] = xp[1];` and `xp[0] = s[0];` and call this function will 90 * see the same numbers. Nothing in the code currently calls this in an 91 * interrupt handler, so this is considered to be okay. If that becomes a 92 * problem, we could create a set of per-cpu variables for interrupt handlers 93 * and use them when in_interrupt() from linux/preempt_mask.h evaluates to 94 * true. 95 */ 96 void __percpu *spl_pseudo_entropy; 97 98 /* 99 * spl_rand_next()/spl_rand_jump() are copied from the following CC-0 licensed 100 * file: 101 * 102 * http://xorshift.di.unimi.it/xorshift128plus.c 103 */ 104 105 static inline uint64_t 106 spl_rand_next(uint64_t *s) 107 { 108 uint64_t s1 = s[0]; 109 const uint64_t s0 = s[1]; 110 s[0] = s0; 111 s1 ^= s1 << 23; // a 112 s[1] = s1 ^ s0 ^ (s1 >> 18) ^ (s0 >> 5); // b, c 113 return (s[1] + s0); 114 } 115 116 static inline void 117 spl_rand_jump(uint64_t *s) 118 { 119 static const uint64_t JUMP[] = 120 { 0x8a5cd789635d2dff, 0x121fd2155c472f96 }; 121 122 uint64_t s0 = 0; 123 uint64_t s1 = 0; 124 int i, b; 125 for (i = 0; i < sizeof (JUMP) / sizeof (*JUMP); i++) 126 for (b = 0; b < 64; b++) { 127 if (JUMP[i] & 1ULL << b) { 128 s0 ^= s[0]; 129 s1 ^= s[1]; 130 } 131 (void) spl_rand_next(s); 132 } 133 134 s[0] = s0; 135 s[1] = s1; 136 } 137 138 int 139 random_get_pseudo_bytes(uint8_t *ptr, size_t len) 140 { 141 uint64_t *xp, s[2]; 142 143 ASSERT(ptr); 144 145 xp = get_cpu_ptr(spl_pseudo_entropy); 146 147 s[0] = xp[0]; 148 s[1] = xp[1]; 149 150 while (len) { 151 union { 152 uint64_t ui64; 153 uint8_t byte[sizeof (uint64_t)]; 154 }entropy; 155 int i = MIN(len, sizeof (uint64_t)); 156 157 len -= i; 158 entropy.ui64 = spl_rand_next(s); 159 160 while (i--) 161 *ptr++ = entropy.byte[i]; 162 } 163 164 xp[0] = s[0]; 165 xp[1] = s[1]; 166 167 put_cpu_ptr(spl_pseudo_entropy); 168 169 return (0); 170 } 171 172 173 EXPORT_SYMBOL(random_get_pseudo_bytes); 174 175 #if BITS_PER_LONG == 32 176 177 /* 178 * Support 64/64 => 64 division on a 32-bit platform. While the kernel 179 * provides a div64_u64() function for this we do not use it because the 180 * implementation is flawed. There are cases which return incorrect 181 * results as late as linux-2.6.35. Until this is fixed upstream the 182 * spl must provide its own implementation. 183 * 184 * This implementation is a slightly modified version of the algorithm 185 * proposed by the book 'Hacker's Delight'. The original source can be 186 * found here and is available for use without restriction. 187 * 188 * http://www.hackersdelight.org/HDcode/newCode/divDouble.c 189 */ 190 191 /* 192 * Calculate number of leading of zeros for a 64-bit value. 193 */ 194 static int 195 nlz64(uint64_t x) 196 { 197 register int n = 0; 198 199 if (x == 0) 200 return (64); 201 202 if (x <= 0x00000000FFFFFFFFULL) { n = n + 32; x = x << 32; } 203 if (x <= 0x0000FFFFFFFFFFFFULL) { n = n + 16; x = x << 16; } 204 if (x <= 0x00FFFFFFFFFFFFFFULL) { n = n + 8; x = x << 8; } 205 if (x <= 0x0FFFFFFFFFFFFFFFULL) { n = n + 4; x = x << 4; } 206 if (x <= 0x3FFFFFFFFFFFFFFFULL) { n = n + 2; x = x << 2; } 207 if (x <= 0x7FFFFFFFFFFFFFFFULL) { n = n + 1; } 208 209 return (n); 210 } 211 212 /* 213 * Newer kernels have a div_u64() function but we define our own 214 * to simplify portability between kernel versions. 215 */ 216 static inline uint64_t 217 __div_u64(uint64_t u, uint32_t v) 218 { 219 (void) do_div(u, v); 220 return (u); 221 } 222 223 /* 224 * Turn off missing prototypes warning for these functions. They are 225 * replacements for libgcc-provided functions and will never be called 226 * directly. 227 */ 228 #pragma GCC diagnostic push 229 #pragma GCC diagnostic ignored "-Wmissing-prototypes" 230 231 /* 232 * Implementation of 64-bit unsigned division for 32-bit machines. 233 * 234 * First the procedure takes care of the case in which the divisor is a 235 * 32-bit quantity. There are two subcases: (1) If the left half of the 236 * dividend is less than the divisor, one execution of do_div() is all that 237 * is required (overflow is not possible). (2) Otherwise it does two 238 * divisions, using the grade school method. 239 */ 240 uint64_t 241 __udivdi3(uint64_t u, uint64_t v) 242 { 243 uint64_t u0, u1, v1, q0, q1, k; 244 int n; 245 246 if (v >> 32 == 0) { // If v < 2**32: 247 if (u >> 32 < v) { // If u/v cannot overflow, 248 return (__div_u64(u, v)); // just do one division. 249 } else { // If u/v would overflow: 250 u1 = u >> 32; // Break u into two halves. 251 u0 = u & 0xFFFFFFFF; 252 q1 = __div_u64(u1, v); // First quotient digit. 253 k = u1 - q1 * v; // First remainder, < v. 254 u0 += (k << 32); 255 q0 = __div_u64(u0, v); // Seconds quotient digit. 256 return ((q1 << 32) + q0); 257 } 258 } else { // If v >= 2**32: 259 n = nlz64(v); // 0 <= n <= 31. 260 v1 = (v << n) >> 32; // Normalize divisor, MSB is 1. 261 u1 = u >> 1; // To ensure no overflow. 262 q1 = __div_u64(u1, v1); // Get quotient from 263 q0 = (q1 << n) >> 31; // Undo normalization and 264 // division of u by 2. 265 if (q0 != 0) // Make q0 correct or 266 q0 = q0 - 1; // too small by 1. 267 if ((u - q0 * v) >= v) 268 q0 = q0 + 1; // Now q0 is correct. 269 270 return (q0); 271 } 272 } 273 EXPORT_SYMBOL(__udivdi3); 274 275 /* BEGIN CSTYLED */ 276 #ifndef abs64 277 #define abs64(x) ({ uint64_t t = (x) >> 63; ((x) ^ t) - t; }) 278 #endif 279 /* END CSTYLED */ 280 281 /* 282 * Implementation of 64-bit signed division for 32-bit machines. 283 */ 284 int64_t 285 __divdi3(int64_t u, int64_t v) 286 { 287 int64_t q, t; 288 // cppcheck-suppress shiftTooManyBitsSigned 289 q = __udivdi3(abs64(u), abs64(v)); 290 // cppcheck-suppress shiftTooManyBitsSigned 291 t = (u ^ v) >> 63; // If u, v have different 292 return ((q ^ t) - t); // signs, negate q. 293 } 294 EXPORT_SYMBOL(__divdi3); 295 296 /* 297 * Implementation of 64-bit unsigned modulo for 32-bit machines. 298 */ 299 uint64_t 300 __umoddi3(uint64_t dividend, uint64_t divisor) 301 { 302 return (dividend - (divisor * __udivdi3(dividend, divisor))); 303 } 304 EXPORT_SYMBOL(__umoddi3); 305 306 /* 64-bit signed modulo for 32-bit machines. */ 307 int64_t 308 __moddi3(int64_t n, int64_t d) 309 { 310 int64_t q; 311 boolean_t nn = B_FALSE; 312 313 if (n < 0) { 314 nn = B_TRUE; 315 n = -n; 316 } 317 if (d < 0) 318 d = -d; 319 320 q = __umoddi3(n, d); 321 322 return (nn ? -q : q); 323 } 324 EXPORT_SYMBOL(__moddi3); 325 326 /* 327 * Implementation of 64-bit unsigned division/modulo for 32-bit machines. 328 */ 329 uint64_t 330 __udivmoddi4(uint64_t n, uint64_t d, uint64_t *r) 331 { 332 uint64_t q = __udivdi3(n, d); 333 if (r) 334 *r = n - d * q; 335 return (q); 336 } 337 EXPORT_SYMBOL(__udivmoddi4); 338 339 /* 340 * Implementation of 64-bit signed division/modulo for 32-bit machines. 341 */ 342 int64_t 343 __divmoddi4(int64_t n, int64_t d, int64_t *r) 344 { 345 int64_t q, rr; 346 boolean_t nn = B_FALSE; 347 boolean_t nd = B_FALSE; 348 if (n < 0) { 349 nn = B_TRUE; 350 n = -n; 351 } 352 if (d < 0) { 353 nd = B_TRUE; 354 d = -d; 355 } 356 357 q = __udivmoddi4(n, d, (uint64_t *)&rr); 358 359 if (nn != nd) 360 q = -q; 361 if (nn) 362 rr = -rr; 363 if (r) 364 *r = rr; 365 return (q); 366 } 367 EXPORT_SYMBOL(__divmoddi4); 368 369 #if defined(__arm) || defined(__arm__) 370 /* 371 * Implementation of 64-bit (un)signed division for 32-bit arm machines. 372 * 373 * Run-time ABI for the ARM Architecture (page 20). A pair of (unsigned) 374 * long longs is returned in {{r0, r1}, {r2,r3}}, the quotient in {r0, r1}, 375 * and the remainder in {r2, r3}. The return type is specifically left 376 * set to 'void' to ensure the compiler does not overwrite these registers 377 * during the return. All results are in registers as per ABI 378 */ 379 void 380 __aeabi_uldivmod(uint64_t u, uint64_t v) 381 { 382 uint64_t res; 383 uint64_t mod; 384 385 res = __udivdi3(u, v); 386 mod = __umoddi3(u, v); 387 { 388 register uint32_t r0 asm("r0") = (res & 0xFFFFFFFF); 389 register uint32_t r1 asm("r1") = (res >> 32); 390 register uint32_t r2 asm("r2") = (mod & 0xFFFFFFFF); 391 register uint32_t r3 asm("r3") = (mod >> 32); 392 393 /* BEGIN CSTYLED */ 394 asm volatile("" 395 : "+r"(r0), "+r"(r1), "+r"(r2),"+r"(r3) /* output */ 396 : "r"(r0), "r"(r1), "r"(r2), "r"(r3)); /* input */ 397 /* END CSTYLED */ 398 399 return; /* r0; */ 400 } 401 } 402 EXPORT_SYMBOL(__aeabi_uldivmod); 403 404 void 405 __aeabi_ldivmod(int64_t u, int64_t v) 406 { 407 int64_t res; 408 uint64_t mod; 409 410 res = __divdi3(u, v); 411 mod = __umoddi3(u, v); 412 { 413 register uint32_t r0 asm("r0") = (res & 0xFFFFFFFF); 414 register uint32_t r1 asm("r1") = (res >> 32); 415 register uint32_t r2 asm("r2") = (mod & 0xFFFFFFFF); 416 register uint32_t r3 asm("r3") = (mod >> 32); 417 418 /* BEGIN CSTYLED */ 419 asm volatile("" 420 : "+r"(r0), "+r"(r1), "+r"(r2),"+r"(r3) /* output */ 421 : "r"(r0), "r"(r1), "r"(r2), "r"(r3)); /* input */ 422 /* END CSTYLED */ 423 424 return; /* r0; */ 425 } 426 } 427 EXPORT_SYMBOL(__aeabi_ldivmod); 428 #endif /* __arm || __arm__ */ 429 430 #pragma GCC diagnostic pop 431 432 #endif /* BITS_PER_LONG */ 433 434 /* 435 * NOTE: The strtoxx behavior is solely based on my reading of the Solaris 436 * ddi_strtol(9F) man page. I have not verified the behavior of these 437 * functions against their Solaris counterparts. It is possible that I 438 * may have misinterpreted the man page or the man page is incorrect. 439 */ 440 int ddi_strtoul(const char *, char **, int, unsigned long *); 441 int ddi_strtol(const char *, char **, int, long *); 442 int ddi_strtoull(const char *, char **, int, unsigned long long *); 443 int ddi_strtoll(const char *, char **, int, long long *); 444 445 #define define_ddi_strtoux(type, valtype) \ 446 int ddi_strtou##type(const char *str, char **endptr, \ 447 int base, valtype *result) \ 448 { \ 449 valtype last_value, value = 0; \ 450 char *ptr = (char *)str; \ 451 int flag = 1, digit; \ 452 \ 453 if (strlen(ptr) == 0) \ 454 return (EINVAL); \ 455 \ 456 /* Auto-detect base based on prefix */ \ 457 if (!base) { \ 458 if (str[0] == '0') { \ 459 if (tolower(str[1]) == 'x' && isxdigit(str[2])) { \ 460 base = 16; /* hex */ \ 461 ptr += 2; \ 462 } else if (str[1] >= '0' && str[1] < 8) { \ 463 base = 8; /* octal */ \ 464 ptr += 1; \ 465 } else { \ 466 return (EINVAL); \ 467 } \ 468 } else { \ 469 base = 10; /* decimal */ \ 470 } \ 471 } \ 472 \ 473 while (1) { \ 474 if (isdigit(*ptr)) \ 475 digit = *ptr - '0'; \ 476 else if (isalpha(*ptr)) \ 477 digit = tolower(*ptr) - 'a' + 10; \ 478 else \ 479 break; \ 480 \ 481 if (digit >= base) \ 482 break; \ 483 \ 484 last_value = value; \ 485 value = value * base + digit; \ 486 if (last_value > value) /* Overflow */ \ 487 return (ERANGE); \ 488 \ 489 flag = 1; \ 490 ptr++; \ 491 } \ 492 \ 493 if (flag) \ 494 *result = value; \ 495 \ 496 if (endptr) \ 497 *endptr = (char *)(flag ? ptr : str); \ 498 \ 499 return (0); \ 500 } \ 501 502 #define define_ddi_strtox(type, valtype) \ 503 int ddi_strto##type(const char *str, char **endptr, \ 504 int base, valtype *result) \ 505 { \ 506 int rc; \ 507 \ 508 if (*str == '-') { \ 509 rc = ddi_strtou##type(str + 1, endptr, base, result); \ 510 if (!rc) { \ 511 if (*endptr == str + 1) \ 512 *endptr = (char *)str; \ 513 else \ 514 *result = -*result; \ 515 } \ 516 } else { \ 517 rc = ddi_strtou##type(str, endptr, base, result); \ 518 } \ 519 \ 520 return (rc); \ 521 } 522 523 define_ddi_strtoux(l, unsigned long) 524 define_ddi_strtox(l, long) 525 define_ddi_strtoux(ll, unsigned long long) 526 define_ddi_strtox(ll, long long) 527 528 EXPORT_SYMBOL(ddi_strtoul); 529 EXPORT_SYMBOL(ddi_strtol); 530 EXPORT_SYMBOL(ddi_strtoll); 531 EXPORT_SYMBOL(ddi_strtoull); 532 533 int 534 ddi_copyin(const void *from, void *to, size_t len, int flags) 535 { 536 /* Fake ioctl() issued by kernel, 'from' is a kernel address */ 537 if (flags & FKIOCTL) { 538 memcpy(to, from, len); 539 return (0); 540 } 541 542 return (copyin(from, to, len)); 543 } 544 EXPORT_SYMBOL(ddi_copyin); 545 546 int 547 ddi_copyout(const void *from, void *to, size_t len, int flags) 548 { 549 /* Fake ioctl() issued by kernel, 'from' is a kernel address */ 550 if (flags & FKIOCTL) { 551 memcpy(to, from, len); 552 return (0); 553 } 554 555 return (copyout(from, to, len)); 556 } 557 EXPORT_SYMBOL(ddi_copyout); 558 559 static ssize_t 560 spl_kernel_read(struct file *file, void *buf, size_t count, loff_t *pos) 561 { 562 #if defined(HAVE_KERNEL_READ_PPOS) 563 return (kernel_read(file, buf, count, pos)); 564 #else 565 mm_segment_t saved_fs; 566 ssize_t ret; 567 568 saved_fs = get_fs(); 569 set_fs(KERNEL_DS); 570 571 ret = vfs_read(file, (void __user *)buf, count, pos); 572 573 set_fs(saved_fs); 574 575 return (ret); 576 #endif 577 } 578 579 static int 580 spl_getattr(struct file *filp, struct kstat *stat) 581 { 582 int rc; 583 584 ASSERT(filp); 585 ASSERT(stat); 586 587 #if defined(HAVE_4ARGS_VFS_GETATTR) 588 rc = vfs_getattr(&filp->f_path, stat, STATX_BASIC_STATS, 589 AT_STATX_SYNC_AS_STAT); 590 #elif defined(HAVE_2ARGS_VFS_GETATTR) 591 rc = vfs_getattr(&filp->f_path, stat); 592 #else 593 rc = vfs_getattr(filp->f_path.mnt, filp->f_dentry, stat); 594 #endif 595 if (rc) 596 return (-rc); 597 598 return (0); 599 } 600 601 /* 602 * Read the unique system identifier from the /etc/hostid file. 603 * 604 * The behavior of /usr/bin/hostid on Linux systems with the 605 * regular eglibc and coreutils is: 606 * 607 * 1. Generate the value if the /etc/hostid file does not exist 608 * or if the /etc/hostid file is less than four bytes in size. 609 * 610 * 2. If the /etc/hostid file is at least 4 bytes, then return 611 * the first four bytes [0..3] in native endian order. 612 * 613 * 3. Always ignore bytes [4..] if they exist in the file. 614 * 615 * Only the first four bytes are significant, even on systems that 616 * have a 64-bit word size. 617 * 618 * See: 619 * 620 * eglibc: sysdeps/unix/sysv/linux/gethostid.c 621 * coreutils: src/hostid.c 622 * 623 * Notes: 624 * 625 * The /etc/hostid file on Solaris is a text file that often reads: 626 * 627 * # DO NOT EDIT 628 * "0123456789" 629 * 630 * Directly copying this file to Linux results in a constant 631 * hostid of 4f442023 because the default comment constitutes 632 * the first four bytes of the file. 633 * 634 */ 635 636 char *spl_hostid_path = HW_HOSTID_PATH; 637 module_param(spl_hostid_path, charp, 0444); 638 MODULE_PARM_DESC(spl_hostid_path, "The system hostid file (/etc/hostid)"); 639 640 static int 641 hostid_read(uint32_t *hostid) 642 { 643 uint64_t size; 644 uint32_t value = 0; 645 int error; 646 loff_t off; 647 struct file *filp; 648 struct kstat stat; 649 650 filp = filp_open(spl_hostid_path, 0, 0); 651 652 if (IS_ERR(filp)) 653 return (ENOENT); 654 655 error = spl_getattr(filp, &stat); 656 if (error) { 657 filp_close(filp, 0); 658 return (error); 659 } 660 size = stat.size; 661 if (size < sizeof (HW_HOSTID_MASK)) { 662 filp_close(filp, 0); 663 return (EINVAL); 664 } 665 666 off = 0; 667 /* 668 * Read directly into the variable like eglibc does. 669 * Short reads are okay; native behavior is preserved. 670 */ 671 error = spl_kernel_read(filp, &value, sizeof (value), &off); 672 if (error < 0) { 673 filp_close(filp, 0); 674 return (EIO); 675 } 676 677 /* Mask down to 32 bits like coreutils does. */ 678 *hostid = (value & HW_HOSTID_MASK); 679 filp_close(filp, 0); 680 681 return (0); 682 } 683 684 /* 685 * Return the system hostid. Preferentially use the spl_hostid module option 686 * when set, otherwise use the value in the /etc/hostid file. 687 */ 688 uint32_t 689 zone_get_hostid(void *zone) 690 { 691 uint32_t hostid; 692 693 ASSERT3P(zone, ==, NULL); 694 695 if (spl_hostid != 0) 696 return ((uint32_t)(spl_hostid & HW_HOSTID_MASK)); 697 698 if (hostid_read(&hostid) == 0) 699 return (hostid); 700 701 return (0); 702 } 703 EXPORT_SYMBOL(zone_get_hostid); 704 705 static int 706 spl_kvmem_init(void) 707 { 708 int rc = 0; 709 710 rc = spl_kmem_init(); 711 if (rc) 712 return (rc); 713 714 rc = spl_vmem_init(); 715 if (rc) { 716 spl_kmem_fini(); 717 return (rc); 718 } 719 720 return (rc); 721 } 722 723 /* 724 * We initialize the random number generator with 128 bits of entropy from the 725 * system random number generator. In the improbable case that we have a zero 726 * seed, we fallback to the system jiffies, unless it is also zero, in which 727 * situation we use a preprogrammed seed. We step forward by 2^64 iterations to 728 * initialize each of the per-cpu seeds so that the sequences generated on each 729 * CPU are guaranteed to never overlap in practice. 730 */ 731 static void __init 732 spl_random_init(void) 733 { 734 uint64_t s[2]; 735 int i = 0; 736 737 spl_pseudo_entropy = __alloc_percpu(2 * sizeof (uint64_t), 738 sizeof (uint64_t)); 739 740 get_random_bytes(s, sizeof (s)); 741 742 if (s[0] == 0 && s[1] == 0) { 743 if (jiffies != 0) { 744 s[0] = jiffies; 745 s[1] = ~0 - jiffies; 746 } else { 747 (void) memcpy(s, "improbable seed", sizeof (s)); 748 } 749 printk("SPL: get_random_bytes() returned 0 " 750 "when generating random seed. Setting initial seed to " 751 "0x%016llx%016llx.\n", cpu_to_be64(s[0]), 752 cpu_to_be64(s[1])); 753 } 754 755 for_each_possible_cpu(i) { 756 uint64_t *wordp = per_cpu_ptr(spl_pseudo_entropy, i); 757 758 spl_rand_jump(s); 759 760 wordp[0] = s[0]; 761 wordp[1] = s[1]; 762 } 763 } 764 765 static void 766 spl_random_fini(void) 767 { 768 free_percpu(spl_pseudo_entropy); 769 } 770 771 static void 772 spl_kvmem_fini(void) 773 { 774 spl_vmem_fini(); 775 spl_kmem_fini(); 776 } 777 778 static int __init 779 spl_init(void) 780 { 781 int rc = 0; 782 783 bzero(&p0, sizeof (proc_t)); 784 spl_random_init(); 785 786 if ((rc = spl_kvmem_init())) 787 goto out1; 788 789 if ((rc = spl_tsd_init())) 790 goto out2; 791 792 if ((rc = spl_taskq_init())) 793 goto out3; 794 795 if ((rc = spl_kmem_cache_init())) 796 goto out4; 797 798 if ((rc = spl_proc_init())) 799 goto out5; 800 801 if ((rc = spl_kstat_init())) 802 goto out6; 803 804 if ((rc = spl_zlib_init())) 805 goto out7; 806 807 return (rc); 808 809 out7: 810 spl_kstat_fini(); 811 out6: 812 spl_proc_fini(); 813 out5: 814 spl_kmem_cache_fini(); 815 out4: 816 spl_taskq_fini(); 817 out3: 818 spl_tsd_fini(); 819 out2: 820 spl_kvmem_fini(); 821 out1: 822 return (rc); 823 } 824 825 static void __exit 826 spl_fini(void) 827 { 828 spl_zlib_fini(); 829 spl_kstat_fini(); 830 spl_proc_fini(); 831 spl_kmem_cache_fini(); 832 spl_taskq_fini(); 833 spl_tsd_fini(); 834 spl_kvmem_fini(); 835 spl_random_fini(); 836 } 837 838 module_init(spl_init); 839 module_exit(spl_fini); 840 841 ZFS_MODULE_DESCRIPTION("Solaris Porting Layer"); 842 ZFS_MODULE_AUTHOR(ZFS_META_AUTHOR); 843 ZFS_MODULE_LICENSE("GPL"); 844 ZFS_MODULE_VERSION(ZFS_META_VERSION "-" ZFS_META_RELEASE); 845