X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=module%2Fspl%2Fspl-generic.c;h=cd2fa2020510ad4f6660c228ce64a6aac9ff085d;hb=b689de85e8c78b9dc03c1bd4c3ca0f4e75714e92;hp=dc3e74aa5042a42fbcc4b4093727308a42503454;hpb=16522ac29023d94bc29e97761b01b252117cbbfe;p=mirror_zfs.git diff --git a/module/spl/spl-generic.c b/module/spl/spl-generic.c index dc3e74aa5..cd2fa2020 100644 --- a/module/spl/spl-generic.c +++ b/module/spl/spl-generic.c @@ -1,4 +1,4 @@ -/*****************************************************************************\ +/* * Copyright (C) 2007-2010 Lawrence Livermore National Security, LLC. * Copyright (C) 2007 The Regents of the University of California. * Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER). @@ -20,9 +20,9 @@ * * You should have received a copy of the GNU General Public License along * with the SPL. If not, see . - ***************************************************************************** + * * Solaris Porting Layer (SPL) Generic Implementation. -\*****************************************************************************/ + */ #include #include @@ -41,21 +41,134 @@ #include #include #include +#include +#include +#include #include -#include -#include +#include "zfs_gitrev.h" -char spl_version[32] = "SPL v" SPL_META_VERSION "-" SPL_META_RELEASE; -EXPORT_SYMBOL(spl_version); +char spl_gitrev[64] = ZFS_META_GITREV; +/* BEGIN CSTYLED */ unsigned long spl_hostid = 0; EXPORT_SYMBOL(spl_hostid); +/* BEGIN CSTYLED */ module_param(spl_hostid, ulong, 0644); MODULE_PARM_DESC(spl_hostid, "The system hostid."); +/* END CSTYLED */ -proc_t p0 = { 0 }; +proc_t p0; EXPORT_SYMBOL(p0); +/* + * Xorshift Pseudo Random Number Generator based on work by Sebastiano Vigna + * + * "Further scramblings of Marsaglia's xorshift generators" + * http://vigna.di.unimi.it/ftp/papers/xorshiftplus.pdf + * + * random_get_pseudo_bytes() is an API function on Illumos whose sole purpose + * is to provide bytes containing random numbers. It is mapped to /dev/urandom + * on Illumos, which uses a "FIPS 186-2 algorithm". No user of the SPL's + * random_get_pseudo_bytes() needs bytes that are of cryptographic quality, so + * we can implement it using a fast PRNG that we seed using Linux' actual + * equivalent to random_get_pseudo_bytes(). We do this by providing each CPU + * with an independent seed so that all calls to random_get_pseudo_bytes() are + * free of atomic instructions. + * + * A consequence of using a fast PRNG is that using random_get_pseudo_bytes() + * to generate words larger than 128 bits will paradoxically be limited to + * `2^128 - 1` possibilities. This is because we have a sequence of `2^128 - 1` + * 128-bit words and selecting the first will implicitly select the second. If + * a caller finds this behavior undesireable, random_get_bytes() should be used + * instead. + * + * XXX: Linux interrupt handlers that trigger within the critical section + * formed by `s[1] = xp[1];` and `xp[0] = s[0];` and call this function will + * see the same numbers. Nothing in the code currently calls this in an + * interrupt handler, so this is considered to be okay. If that becomes a + * problem, we could create a set of per-cpu variables for interrupt handlers + * and use them when in_interrupt() from linux/preempt_mask.h evaluates to + * true. + */ +static DEFINE_PER_CPU(uint64_t[2], spl_pseudo_entropy); + +/* + * spl_rand_next()/spl_rand_jump() are copied from the following CC-0 licensed + * file: + * + * http://xorshift.di.unimi.it/xorshift128plus.c + */ + +static inline uint64_t +spl_rand_next(uint64_t *s) +{ + uint64_t s1 = s[0]; + const uint64_t s0 = s[1]; + s[0] = s0; + s1 ^= s1 << 23; // a + s[1] = s1 ^ s0 ^ (s1 >> 18) ^ (s0 >> 5); // b, c + return (s[1] + s0); +} + +static inline void +spl_rand_jump(uint64_t *s) +{ + static const uint64_t JUMP[] = + { 0x8a5cd789635d2dff, 0x121fd2155c472f96 }; + + uint64_t s0 = 0; + uint64_t s1 = 0; + int i, b; + for (i = 0; i < sizeof (JUMP) / sizeof (*JUMP); i++) + for (b = 0; b < 64; b++) { + if (JUMP[i] & 1ULL << b) { + s0 ^= s[0]; + s1 ^= s[1]; + } + (void) spl_rand_next(s); + } + + s[0] = s0; + s[1] = s1; +} + +int +random_get_pseudo_bytes(uint8_t *ptr, size_t len) +{ + uint64_t *xp, s[2]; + + ASSERT(ptr); + + xp = get_cpu_var(spl_pseudo_entropy); + + s[0] = xp[0]; + s[1] = xp[1]; + + while (len) { + union { + uint64_t ui64; + uint8_t byte[sizeof (uint64_t)]; + }entropy; + int i = MIN(len, sizeof (uint64_t)); + + len -= i; + entropy.ui64 = spl_rand_next(s); + + while (i--) + *ptr++ = entropy.byte[i]; + } + + xp[0] = s[0]; + xp[1] = s[1]; + + put_cpu_var(spl_pseudo_entropy); + + return (0); +} + + +EXPORT_SYMBOL(random_get_pseudo_bytes); + #if BITS_PER_LONG == 32 /* * Support 64/64 => 64 division on a 32-bit platform. While the kernel @@ -75,20 +188,21 @@ EXPORT_SYMBOL(p0); * Calculate number of leading of zeros for a 64-bit value. */ static int -nlz64(uint64_t x) { +nlz64(uint64_t x) +{ register int n = 0; if (x == 0) - return 64; + return (64); - if (x <= 0x00000000FFFFFFFFULL) {n = n + 32; x = x << 32;} - if (x <= 0x0000FFFFFFFFFFFFULL) {n = n + 16; x = x << 16;} - if (x <= 0x00FFFFFFFFFFFFFFULL) {n = n + 8; x = x << 8;} - if (x <= 0x0FFFFFFFFFFFFFFFULL) {n = n + 4; x = x << 4;} - if (x <= 0x3FFFFFFFFFFFFFFFULL) {n = n + 2; x = x << 2;} - if (x <= 0x7FFFFFFFFFFFFFFFULL) {n = n + 1;} + if (x <= 0x00000000FFFFFFFFULL) { n = n + 32; x = x << 32; } + if (x <= 0x0000FFFFFFFFFFFFULL) { n = n + 16; x = x << 16; } + if (x <= 0x00FFFFFFFFFFFFFFULL) { n = n + 8; x = x << 8; } + if (x <= 0x0FFFFFFFFFFFFFFFULL) { n = n + 4; x = x << 4; } + if (x <= 0x3FFFFFFFFFFFFFFFULL) { n = n + 2; x = x << 2; } + if (x <= 0x7FFFFFFFFFFFFFFFULL) { n = n + 1; } - return n; + return (n); } /* @@ -99,7 +213,7 @@ static inline uint64_t __div_u64(uint64_t u, uint32_t v) { (void) do_div(u, v); - return u; + return (u); } /* @@ -119,7 +233,7 @@ __udivdi3(uint64_t u, uint64_t v) if (v >> 32 == 0) { // If v < 2**32: if (u >> 32 < v) { // If u/v cannot overflow, - return __div_u64(u, v); // just do one division. + return (__div_u64(u, v)); // just do one division. } else { // If u/v would overflow: u1 = u >> 32; // Break u into two halves. u0 = u & 0xFFFFFFFF; @@ -127,7 +241,7 @@ __udivdi3(uint64_t u, uint64_t v) k = u1 - q1 * v; // First remainder, < v. u0 += (k << 32); q0 = __div_u64(u0, v); // Seconds quotient digit. - return (q1 << 32) + q0; + return ((q1 << 32) + q0); } } else { // If v >= 2**32: n = nlz64(v); // 0 <= n <= 31. @@ -141,11 +255,17 @@ __udivdi3(uint64_t u, uint64_t v) if ((u - q0 * v) >= v) q0 = q0 + 1; // Now q0 is correct. - return q0; + return (q0); } } EXPORT_SYMBOL(__udivdi3); +/* BEGIN CSTYLED */ +#ifndef abs64 +#define abs64(x) ({ uint64_t t = (x) >> 63; ((x) ^ t) - t; }) +#endif +/* END CSTYLED */ + /* * Implementation of 64-bit signed division for 32-bit machines. */ @@ -155,7 +275,7 @@ __divdi3(int64_t u, int64_t v) int64_t q, t; q = __udivdi3(abs64(u), abs64(v)); t = (u ^ v) >> 63; // If u, v have different - return (q ^ t) - t; // signs, negate q. + return ((q ^ t) - t); // signs, negate q. } EXPORT_SYMBOL(__divdi3); @@ -169,6 +289,49 @@ __umoddi3(uint64_t dividend, uint64_t divisor) } EXPORT_SYMBOL(__umoddi3); +/* + * Implementation of 64-bit unsigned division/modulo for 32-bit machines. + */ +uint64_t +__udivmoddi4(uint64_t n, uint64_t d, uint64_t *r) +{ + uint64_t q = __udivdi3(n, d); + if (r) + *r = n - d * q; + return (q); +} +EXPORT_SYMBOL(__udivmoddi4); + +/* + * Implementation of 64-bit signed division/modulo for 32-bit machines. + */ +int64_t +__divmoddi4(int64_t n, int64_t d, int64_t *r) +{ + int64_t q, rr; + boolean_t nn = B_FALSE; + boolean_t nd = B_FALSE; + if (n < 0) { + nn = B_TRUE; + n = -n; + } + if (d < 0) { + nd = B_TRUE; + d = -d; + } + + q = __udivmoddi4(n, d, (uint64_t *)&rr); + + if (nn != nd) + q = -q; + if (nn) + rr = -rr; + if (r) + *r = rr; + return (q); +} +EXPORT_SYMBOL(__divmoddi4); + #if defined(__arm) || defined(__arm__) /* * Implementation of 64-bit (un)signed division for 32-bit arm machines. @@ -193,9 +356,11 @@ __aeabi_uldivmod(uint64_t u, uint64_t v) register uint32_t r2 asm("r2") = (mod & 0xFFFFFFFF); register uint32_t r3 asm("r3") = (mod >> 32); + /* BEGIN CSTYLED */ asm volatile("" : "+r"(r0), "+r"(r1), "+r"(r2),"+r"(r3) /* output */ : "r"(r0), "r"(r1), "r"(r2), "r"(r3)); /* input */ + /* END CSTYLED */ return; /* r0; */ } @@ -216,9 +381,11 @@ __aeabi_ldivmod(int64_t u, int64_t v) register uint32_t r2 asm("r2") = (mod & 0xFFFFFFFF); register uint32_t r3 asm("r3") = (mod >> 32); + /* BEGIN CSTYLED */ asm volatile("" : "+r"(r0), "+r"(r1), "+r"(r2),"+r"(r3) /* output */ : "r"(r0), "r"(r1), "r"(r2), "r"(r3)); /* input */ + /* END CSTYLED */ return; /* r0; */ } @@ -227,7 +394,8 @@ EXPORT_SYMBOL(__aeabi_ldivmod); #endif /* __arm || __arm__ */ #endif /* BITS_PER_LONG */ -/* NOTE: The strtoxx behavior is solely based on my reading of the Solaris +/* + * NOTE: The strtoxx behavior is solely based on my reading of the Solaris * ddi_strtol(9F) man page. I have not verified the behavior of these * functions against their Solaris counterparts. It is possible that I * may have misinterpreted the man page or the man page is incorrect. @@ -237,28 +405,28 @@ int ddi_strtol(const char *, char **, int, long *); int ddi_strtoull(const char *, char **, int, unsigned long long *); int ddi_strtoll(const char *, char **, int, long long *); -#define define_ddi_strtoux(type, valtype) \ +#define define_ddi_strtoux(type, valtype) \ int ddi_strtou##type(const char *str, char **endptr, \ - int base, valtype *result) \ + int base, valtype *result) \ { \ valtype last_value, value = 0; \ char *ptr = (char *)str; \ int flag = 1, digit; \ \ if (strlen(ptr) == 0) \ - return EINVAL; \ + return (EINVAL); \ \ /* Auto-detect base based on prefix */ \ if (!base) { \ if (str[0] == '0') { \ - if (tolower(str[1])=='x' && isxdigit(str[2])) { \ + if (tolower(str[1]) == 'x' && isxdigit(str[2])) { \ base = 16; /* hex */ \ ptr += 2; \ } else if (str[1] >= '0' && str[1] < 8) { \ base = 8; /* octal */ \ ptr += 1; \ } else { \ - return EINVAL; \ + return (EINVAL); \ } \ } else { \ base = 10; /* decimal */ \ @@ -279,7 +447,7 @@ int ddi_strtou##type(const char *str, char **endptr, \ last_value = value; \ value = value * base + digit; \ if (last_value > value) /* Overflow */ \ - return ERANGE; \ + return (ERANGE); \ \ flag = 1; \ ptr++; \ @@ -291,12 +459,12 @@ int ddi_strtou##type(const char *str, char **endptr, \ if (endptr) \ *endptr = (char *)(flag ? ptr : str); \ \ - return 0; \ + return (0); \ } \ -#define define_ddi_strtox(type, valtype) \ +#define define_ddi_strtox(type, valtype) \ int ddi_strto##type(const char *str, char **endptr, \ - int base, valtype *result) \ + int base, valtype *result) \ { \ int rc; \ \ @@ -312,7 +480,7 @@ int ddi_strto##type(const char *str, char **endptr, \ rc = ddi_strtou##type(str, endptr, base, result); \ } \ \ - return rc; \ + return (rc); \ } define_ddi_strtoux(l, unsigned long) @@ -331,10 +499,10 @@ ddi_copyin(const void *from, void *to, size_t len, int flags) /* Fake ioctl() issued by kernel, 'from' is a kernel address */ if (flags & FKIOCTL) { memcpy(to, from, len); - return 0; + return (0); } - return copyin(from, to, len); + return (copyin(from, to, len)); } EXPORT_SYMBOL(ddi_copyin); @@ -344,29 +512,13 @@ ddi_copyout(const void *from, void *to, size_t len, int flags) /* Fake ioctl() issued by kernel, 'from' is a kernel address */ if (flags & FKIOCTL) { memcpy(to, from, len); - return 0; + return (0); } - return copyout(from, to, len); + return (copyout(from, to, len)); } EXPORT_SYMBOL(ddi_copyout); -#ifndef HAVE_PUT_TASK_STRUCT -/* - * This is only a stub function which should never be used. The SPL should - * never be putting away the last reference on a task structure so this will - * not be called. However, we still need to define it so the module does not - * have undefined symbol at load time. That all said if this impossible - * thing does somehow happen PANIC immediately so we know about it. - */ -void -__put_task_struct(struct task_struct *t) -{ - PANIC("Unexpectly put last reference on task %d\n", (int)t->pid); -} -EXPORT_SYMBOL(__put_task_struct); -#endif /* HAVE_PUT_TASK_STRUCT */ - /* * Read the unique system identifier from the /etc/hostid file. * @@ -407,80 +559,63 @@ module_param(spl_hostid_path, charp, 0444); MODULE_PARM_DESC(spl_hostid_path, "The system hostid file (/etc/hostid)"); static int -hostid_read(void) +hostid_read(uint32_t *hostid) { - int result; uint64_t size; struct _buf *file; - uint32_t hostid = 0; + uint32_t value = 0; + int error; file = kobj_open_file(spl_hostid_path); - if (file == (struct _buf *)-1) - return -1; - - result = kobj_get_filesize(file, &size); + return (ENOENT); - if (result != 0) { - printk(KERN_WARNING - "SPL: kobj_get_filesize returned %i on %s\n", - result, spl_hostid_path); + error = kobj_get_filesize(file, &size); + if (error) { kobj_close_file(file); - return -2; + return (error); } - if (size < sizeof(HW_HOSTID_MASK)) { - printk(KERN_WARNING - "SPL: Ignoring the %s file because it is %llu bytes; " - "expecting %lu bytes instead.\n", spl_hostid_path, - size, (unsigned long)sizeof(HW_HOSTID_MASK)); + if (size < sizeof (HW_HOSTID_MASK)) { kobj_close_file(file); - return -3; + return (EINVAL); } - /* Read directly into the variable like eglibc does. */ - /* Short reads are okay; native behavior is preserved. */ - result = kobj_read_file(file, (char *)&hostid, sizeof(hostid), 0); - - if (result < 0) { - printk(KERN_WARNING - "SPL: kobj_read_file returned %i on %s\n", - result, spl_hostid_path); + /* + * Read directly into the variable like eglibc does. + * Short reads are okay; native behavior is preserved. + */ + error = kobj_read_file(file, (char *)&value, sizeof (value), 0); + if (error < 0) { kobj_close_file(file); - return -4; + return (EIO); } /* Mask down to 32 bits like coreutils does. */ - spl_hostid = hostid & HW_HOSTID_MASK; + *hostid = (value & HW_HOSTID_MASK); kobj_close_file(file); - return 0; + + return (0); } +/* + * Return the system hostid. Preferentially use the spl_hostid module option + * when set, otherwise use the value in the /etc/hostid file. + */ uint32_t zone_get_hostid(void *zone) { - static int first = 1; - - /* Only the global zone is supported */ - ASSERT(zone == NULL); + uint32_t hostid; - if (first) { - first = 0; + ASSERT3P(zone, ==, NULL); - spl_hostid &= HW_HOSTID_MASK; - /* - * Get the hostid if it was not passed as a module parameter. - * Try reading the /etc/hostid file directly. - */ - if (spl_hostid == 0 && hostid_read()) - spl_hostid = 0; + if (spl_hostid != 0) + return ((uint32_t)(spl_hostid & HW_HOSTID_MASK)); + if (hostid_read(&hostid) == 0) + return (hostid); - printk(KERN_NOTICE "SPL: using hostid 0x%08x\n", - (unsigned int) spl_hostid); - } - - return spl_hostid; + return (0); } EXPORT_SYMBOL(zone_get_hostid); @@ -502,6 +637,45 @@ spl_kvmem_init(void) return (rc); } +/* + * We initialize the random number generator with 128 bits of entropy from the + * system random number generator. In the improbable case that we have a zero + * seed, we fallback to the system jiffies, unless it is also zero, in which + * situation we use a preprogrammed seed. We step forward by 2^64 iterations to + * initialize each of the per-cpu seeds so that the sequences generated on each + * CPU are guaranteed to never overlap in practice. + */ +static void __init +spl_random_init(void) +{ + uint64_t s[2]; + int i; + + get_random_bytes(s, sizeof (s)); + + if (s[0] == 0 && s[1] == 0) { + if (jiffies != 0) { + s[0] = jiffies; + s[1] = ~0 - jiffies; + } else { + (void) memcpy(s, "improbable seed", sizeof (s)); + } + printk("SPL: get_random_bytes() returned 0 " + "when generating random seed. Setting initial seed to " + "0x%016llx%016llx.\n", cpu_to_be64(s[0]), + cpu_to_be64(s[1])); + } + + for_each_possible_cpu(i) { + uint64_t *wordp = per_cpu(spl_pseudo_entropy, i); + + spl_rand_jump(s); + + wordp[0] = s[0]; + wordp[1] = s[1]; + } +} + static void spl_kvmem_fini(void) { @@ -514,6 +688,9 @@ spl_init(void) { int rc = 0; + bzero(&p0, sizeof (proc_t)); + spl_random_init(); + if ((rc = spl_kvmem_init())) goto out1; @@ -544,8 +721,6 @@ spl_init(void) if ((rc = spl_zlib_init())) goto out10; - printk(KERN_NOTICE "SPL: Loaded module v%s-%s%s\n", SPL_META_VERSION, - SPL_META_RELEASE, SPL_DEBUG_STR); return (rc); out10: @@ -567,18 +742,12 @@ out3: out2: spl_kvmem_fini(); out1: - printk(KERN_NOTICE "SPL: Failed to Load Solaris Porting Layer " - "v%s-%s%s, rc = %d\n", SPL_META_VERSION, SPL_META_RELEASE, - SPL_DEBUG_STR, rc); - return (rc); } static void __exit spl_fini(void) { - printk(KERN_NOTICE "SPL: Unloaded module v%s-%s%s\n", - SPL_META_VERSION, SPL_META_RELEASE, SPL_DEBUG_STR); spl_zlib_fini(); spl_kstat_fini(); spl_proc_fini(); @@ -595,6 +764,6 @@ module_init(spl_init); module_exit(spl_fini); MODULE_DESCRIPTION("Solaris Porting Layer"); -MODULE_AUTHOR(SPL_META_AUTHOR); -MODULE_LICENSE(SPL_META_LICENSE); -MODULE_VERSION(SPL_META_VERSION "-" SPL_META_RELEASE); +MODULE_AUTHOR(ZFS_META_AUTHOR); +MODULE_LICENSE("GPL"); +MODULE_VERSION(ZFS_META_VERSION "-" ZFS_META_RELEASE);