X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=module%2Fspl%2Fspl-generic.c;h=f6782dae73af08eec0b54c2da385ece22c405812;hb=ec06701b5473894514b25e4054dd5597e25a22e8;hp=1dedf76cf22cf78d6e84f540dd6f129e51b35984;hpb=6b3b569df30d13ed7bbbff877cffc71290a52f12;p=mirror_spl-debian.git
diff --git a/module/spl/spl-generic.c b/module/spl/spl-generic.c
index 1dedf76..f6782da 100644
--- a/module/spl/spl-generic.c
+++ b/module/spl/spl-generic.c
@@ -6,7 +6,7 @@
* UCRL-CODE-235197
*
* This file is part of the SPL, Solaris Porting Layer.
- * For details, see .
+ * For details, see .
*
* The SPL is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
@@ -29,6 +29,8 @@
#include
#include
#include
+#include
+#include
#include
#include
#include
@@ -37,67 +39,130 @@
#include
#include
#include
-#include
#include
+#include
+#include
+#include
#include
+#include
#include
-#include
-#ifdef SS_DEBUG_SUBSYS
-#undef SS_DEBUG_SUBSYS
-#endif
-
-#define SS_DEBUG_SUBSYS SS_GENERIC
-
-char spl_version[16] = "SPL v" SPL_META_VERSION;
+char spl_version[32] = "SPL v" SPL_META_VERSION "-" SPL_META_RELEASE;
EXPORT_SYMBOL(spl_version);
-unsigned long spl_hostid = HW_INVALID_HOSTID;
+unsigned long spl_hostid = 0;
EXPORT_SYMBOL(spl_hostid);
module_param(spl_hostid, ulong, 0644);
MODULE_PARM_DESC(spl_hostid, "The system hostid.");
-char hw_serial[HW_HOSTID_LEN] = "";
-EXPORT_SYMBOL(hw_serial);
-
-proc_t p0 = { 0 };
+proc_t p0;
EXPORT_SYMBOL(p0);
-#ifndef HAVE_KALLSYMS_LOOKUP_NAME
-kallsyms_lookup_name_t spl_kallsyms_lookup_name_fn = SYMBOL_POISON;
-#endif
+/*
+ * 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
-highbit(unsigned long i)
+random_get_pseudo_bytes(uint8_t *ptr, size_t len)
{
- register int h = 1;
- SENTRY;
-
- if (i == 0)
- SRETURN(0);
-#if BITS_PER_LONG == 64
- if (i & 0xffffffff00000000ul) {
- h += 32; i >>= 32;
- }
-#endif
- if (i & 0xffff0000) {
- h += 16; i >>= 16;
- }
- if (i & 0xff00) {
- h += 8; i >>= 8;
- }
- if (i & 0xf0) {
- h += 4; i >>= 4;
- }
- if (i & 0xc) {
- h += 2; i >>= 2;
- }
- if (i & 0x2) {
- h += 1;
- }
- SRETURN(h);
+ 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(highbit);
+
+
+EXPORT_SYMBOL(random_get_pseudo_bytes);
#if BITS_PER_LONG == 32
/*
@@ -183,7 +248,7 @@ __udivdi3(uint64_t u, uint64_t v)
q0 = q0 - 1; // too small by 1.
if ((u - q0 * v) >= v)
q0 = q0 + 1; // Now q0 is correct.
-
+
return q0;
}
}
@@ -212,6 +277,105 @@ __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.
+ *
+ * Run-time ABI for the ARM Architecture (page 20). A pair of (unsigned)
+ * long longs is returned in {{r0, r1}, {r2,r3}}, the quotient in {r0, r1},
+ * and the remainder in {r2, r3}. The return type is specifically left
+ * set to 'void' to ensure the compiler does not overwrite these registers
+ * during the return. All results are in registers as per ABI
+ */
+void
+__aeabi_uldivmod(uint64_t u, uint64_t v)
+{
+ uint64_t res;
+ uint64_t mod;
+
+ res = __udivdi3(u, v);
+ mod = __umoddi3(u, v);
+ {
+ register uint32_t r0 asm("r0") = (res & 0xFFFFFFFF);
+ register uint32_t r1 asm("r1") = (res >> 32);
+ register uint32_t r2 asm("r2") = (mod & 0xFFFFFFFF);
+ register uint32_t r3 asm("r3") = (mod >> 32);
+
+ asm volatile(""
+ : "+r"(r0), "+r"(r1), "+r"(r2),"+r"(r3) /* output */
+ : "r"(r0), "r"(r1), "r"(r2), "r"(r3)); /* input */
+
+ return; /* r0; */
+ }
+}
+EXPORT_SYMBOL(__aeabi_uldivmod);
+
+void
+__aeabi_ldivmod(int64_t u, int64_t v)
+{
+ int64_t res;
+ uint64_t mod;
+
+ res = __divdi3(u, v);
+ mod = __umoddi3(u, v);
+ {
+ register uint32_t r0 asm("r0") = (res & 0xFFFFFFFF);
+ register uint32_t r1 asm("r1") = (res >> 32);
+ register uint32_t r2 asm("r2") = (mod & 0xFFFFFFFF);
+ register uint32_t r3 asm("r3") = (mod >> 32);
+
+ asm volatile(""
+ : "+r"(r0), "+r"(r1), "+r"(r2),"+r"(r3) /* output */
+ : "r"(r0), "r"(r1), "r"(r2), "r"(r3)); /* input */
+
+ return; /* r0; */
+ }
+}
+EXPORT_SYMBOL(__aeabi_ldivmod);
+#endif /* __arm || __arm__ */
#endif /* BITS_PER_LONG */
/* NOTE: The strtoxx behavior is solely based on my reading of the Solaris
@@ -338,33 +502,6 @@ ddi_copyout(const void *from, void *to, size_t len, int flags)
}
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 */
-
-struct new_utsname *__utsname(void)
-{
-#ifdef HAVE_INIT_UTSNAME
- return init_utsname();
-#else
- return &system_utsname;
-#endif
-}
-EXPORT_SYMBOL(__utsname);
-
-
/*
* Read the unique system identifier from the /etc/hostid file.
*
@@ -405,270 +542,218 @@ 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;
- unsigned long 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, 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;
}
-#define GET_HOSTID_CMD \
- "exec 0/proc/sys/kernel/spl/hostid " \
- " 2>/dev/null; " \
- "hostid"
-
-static int
-hostid_exec(void)
+/*
+ * 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)
{
- char *argv[] = { "/bin/sh",
- "-c",
- GET_HOSTID_CMD,
- NULL };
- char *envp[] = { "HOME=/",
- "TERM=linux",
- "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
- NULL };
- int rc;
-
- /* Doing address resolution in the kernel is tricky and just
- * not a good idea in general. So to set the proper 'hw_serial'
- * use the usermodehelper support to ask '/bin/sh' to run
- * '/usr/bin/hostid' and redirect the result to /proc/sys/spl/hostid
- * for us to use. It's a horrific solution but it will do for now.
- */
- rc = call_usermodehelper(argv[0], argv, envp, 1);
- if (rc)
- printk("SPL: Failed user helper '%s %s %s', rc = %d\n",
- argv[0], argv[1], argv[2], rc);
+ uint32_t hostid;
+
+ ASSERT3P(zone, ==, NULL);
+
+ if (spl_hostid != 0)
+ return ((uint32_t)(spl_hostid & HW_HOSTID_MASK));
- return rc;
+ if (hostid_read(&hostid) == 0)
+ return (hostid);
+
+ return (0);
}
+EXPORT_SYMBOL(zone_get_hostid);
-uint32_t
-zone_get_hostid(void *zone)
+static int
+spl_kvmem_init(void)
{
- unsigned long hostid;
+ int rc = 0;
- /* Only the global zone is supported */
- ASSERT(zone == NULL);
+ rc = spl_kmem_init();
+ if (rc)
+ return (rc);
- if (ddi_strtoul(hw_serial, NULL, HW_HOSTID_LEN-1, &hostid) != 0)
- return HW_INVALID_HOSTID;
+ rc = spl_vmem_init();
+ if (rc) {
+ spl_kmem_fini();
+ return (rc);
+ }
- return (uint32_t)hostid;
+ return (rc);
}
-EXPORT_SYMBOL(zone_get_hostid);
-#ifndef HAVE_KALLSYMS_LOOKUP_NAME
/*
- * The kallsyms_lookup_name() kernel function is not an exported symbol in
- * Linux 2.6.19 through 2.6.32 inclusive.
- *
- * This function replaces the functionality by performing an upcall to user
- * space where /proc/kallsyms is consulted for the requested address.
- *
+ * 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;
-#define GET_KALLSYMS_ADDR_CMD \
- "exec 0/proc/sys/kernel/spl/kallsyms_lookup_name " \
- " 2>/dev/null; " \
- "awk '{ if ( $3 == \"kallsyms_lookup_name\" ) { print $1 } }' " \
- " /proc/kallsyms "
+ get_random_bytes(s, sizeof (s));
-static int
-set_kallsyms_lookup_name(void)
-{
- char *argv[] = { "/bin/sh",
- "-c",
- GET_KALLSYMS_ADDR_CMD,
- NULL };
- char *envp[] = { "HOME=/",
- "TERM=linux",
- "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
- NULL };
- int rc;
-
- rc = call_usermodehelper(argv[0], argv, envp, 1);
- if (rc)
- printk("SPL: Failed user helper '%s %s %s', rc = %d\n",
- argv[0], argv[1], argv[2], rc);
+ 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.", cpu_to_be64(s[0]), cpu_to_be64(s[1]));
+ }
- return rc;
+ 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];
+ }
}
-#endif
-static int
-__init spl_init(void)
+static void
+spl_kvmem_fini(void)
+{
+ spl_vmem_fini();
+ spl_kmem_fini();
+}
+
+static int __init
+spl_init(void)
{
int rc = 0;
- if ((rc = debug_init()))
- return rc;
+ bzero(&p0, sizeof (proc_t));
+ spl_random_init();
- if ((rc = spl_kmem_init()))
- SGOTO(out1, rc);
+ if ((rc = spl_kvmem_init()))
+ goto out1;
if ((rc = spl_mutex_init()))
- SGOTO(out2, rc);
+ goto out2;
if ((rc = spl_rw_init()))
- SGOTO(out3, rc);
-
- if ((rc = spl_taskq_init()))
- SGOTO(out4, rc);
-
- if ((rc = vn_init()))
- SGOTO(out5, rc);
+ goto out3;
- if ((rc = proc_init()))
- SGOTO(out6, rc);
+ if ((rc = spl_tsd_init()))
+ goto out4;
- if ((rc = kstat_init()))
- SGOTO(out7, rc);
+ if ((rc = spl_taskq_init()))
+ goto out5;
- if ((rc = tsd_init()))
- SGOTO(out8, rc);
+ if ((rc = spl_kmem_cache_init()))
+ goto out6;
- if ((rc = zlib_init()))
- SGOTO(out9, rc);
+ if ((rc = spl_vn_init()))
+ goto out7;
- /*
- * Get the hostid if it was not passed as a module parameter. Try
- * reading the /etc/hostid file directly, and then fall back to calling
- * the /usr/bin/hostid utility.
- */
+ if ((rc = spl_proc_init()))
+ goto out8;
- if (spl_hostid == HW_INVALID_HOSTID
- && (rc = hostid_read()) && (rc = hostid_exec()))
- SGOTO(out10, rc = -EADDRNOTAVAIL);
+ if ((rc = spl_kstat_init()))
+ goto out9;
-#ifndef HAVE_KALLSYMS_LOOKUP_NAME
- if ((rc = set_kallsyms_lookup_name()))
- SGOTO(out10, rc = -EADDRNOTAVAIL);
-#endif /* HAVE_KALLSYMS_LOOKUP_NAME */
+ if ((rc = spl_zlib_init()))
+ goto out10;
- if ((rc = spl_kmem_init_kallsyms_lookup()))
- SGOTO(out10, rc);
+ printk(KERN_NOTICE "SPL: Loaded module v%s-%s%s\n", SPL_META_VERSION,
+ SPL_META_RELEASE, SPL_DEBUG_STR);
+ return (rc);
- printk(KERN_NOTICE "SPL: Loaded module v%s%s, using hostid 0x%08x\n",
- SPL_META_VERSION, SPL_DEBUG_STR, (unsigned int) spl_hostid);
- SRETURN(rc);
out10:
- zlib_fini();
+ spl_kstat_fini();
out9:
- tsd_fini();
+ spl_proc_fini();
out8:
- kstat_fini();
+ spl_vn_fini();
out7:
- proc_fini();
+ spl_kmem_cache_fini();
out6:
- vn_fini();
-out5:
spl_taskq_fini();
+out5:
+ spl_tsd_fini();
out4:
spl_rw_fini();
out3:
spl_mutex_fini();
out2:
- spl_kmem_fini();
+ spl_kvmem_fini();
out1:
- debug_fini();
+ 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);
- printk(KERN_NOTICE "SPL: Failed to Load Solaris Porting Layer v%s%s"
- ", rc = %d\n", SPL_META_VERSION, SPL_DEBUG_STR, rc);
- return rc;
+ return (rc);
}
-static void
+static void __exit
spl_fini(void)
{
- SENTRY;
-
- printk(KERN_NOTICE "SPL: Unloaded module v%s%s\n",
- SPL_META_VERSION, SPL_DEBUG_STR);
- zlib_fini();
- tsd_fini();
- kstat_fini();
- proc_fini();
- vn_fini();
+ 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();
+ spl_vn_fini();
+ spl_kmem_cache_fini();
spl_taskq_fini();
+ spl_tsd_fini();
spl_rw_fini();
spl_mutex_fini();
- spl_kmem_fini();
- debug_fini();
-}
-
-/* Called when a dependent module is loaded */
-void
-spl_setup(void)
-{
- int rc;
-
- /*
- * At module load time the pwd is set to '/' on a Solaris system.
- * On a Linux system will be set to whatever directory the caller
- * was in when executing insmod/modprobe.
- */
- rc = vn_set_pwd("/");
- if (rc)
- printk("SPL: Warning unable to set pwd to '/': %d\n", rc);
-}
-EXPORT_SYMBOL(spl_setup);
-
-/* Called when a dependent module is unloaded */
-void
-spl_cleanup(void)
-{
+ spl_kvmem_fini();
}
-EXPORT_SYMBOL(spl_cleanup);
module_init(spl_init);
module_exit(spl_fini);
-MODULE_AUTHOR("Lawrence Livermore National Labs");
MODULE_DESCRIPTION("Solaris Porting Layer");
-MODULE_LICENSE("GPL");
+MODULE_AUTHOR(SPL_META_AUTHOR);
+MODULE_LICENSE(SPL_META_LICENSE);
+MODULE_VERSION(SPL_META_VERSION "-" SPL_META_RELEASE);