#include <sys/sysmacros.h>
#include <sys/systeminfo.h>
#include <sys/vmsystm.h>
-#include <sys/vnode.h>
+#include <sys/kobj.h>
#include <sys/kmem.h>
#include <sys/mutex.h>
#include <sys/rwlock.h>
#include <sys/taskq.h>
+#include <sys/tsd.h>
+#include <sys/zmod.h>
#include <sys/debug.h>
#include <sys/proc.h>
#include <sys/kstat.h>
#include <sys/utsname.h>
#include <sys/file.h>
#include <linux/kmod.h>
+#include <linux/proc_compat.h>
+#include <spl-debug.h>
-#ifdef DEBUG_SUBSYSTEM
-#undef DEBUG_SUBSYSTEM
+#ifdef SS_DEBUG_SUBSYS
+#undef SS_DEBUG_SUBSYS
#endif
-#define DEBUG_SUBSYSTEM S_GENERIC
+#define SS_DEBUG_SUBSYS SS_GENERIC
char spl_version[16] = "SPL v" SPL_META_VERSION;
+EXPORT_SYMBOL(spl_version);
-long spl_hostid = 0;
+unsigned long spl_hostid = HW_INVALID_HOSTID;
EXPORT_SYMBOL(spl_hostid);
+module_param(spl_hostid, ulong, 0644);
+MODULE_PARM_DESC(spl_hostid, "The system hostid.");
char hw_serial[HW_HOSTID_LEN] = "<none>";
EXPORT_SYMBOL(hw_serial);
-int p0 = 0;
+proc_t p0 = { 0 };
EXPORT_SYMBOL(p0);
#ifndef HAVE_KALLSYMS_LOOKUP_NAME
highbit(unsigned long i)
{
register int h = 1;
- ENTRY;
+ SENTRY;
if (i == 0)
- RETURN(0);
+ SRETURN(0);
#if BITS_PER_LONG == 64
if (i & 0xffffffff00000000ul) {
h += 32; i >>= 32;
if (i & 0x2) {
h += 1;
}
- RETURN(h);
+ SRETURN(h);
}
EXPORT_SYMBOL(highbit);
+#if BITS_PER_LONG == 32
/*
- * Implementation of 64 bit division for 32-bit machines.
+ * Support 64/64 => 64 division on a 32-bit platform. While the kernel
+ * provides a div64_u64() function for this we do not use it because the
+ * implementation is flawed. There are cases which return incorrect
+ * results as late as linux-2.6.35. Until this is fixed upstream the
+ * spl must provide its own implementation.
+ *
+ * This implementation is a slightly modified version of the algorithm
+ * proposed by the book 'Hacker's Delight'. The original source can be
+ * found here and is available for use without restriction.
+ *
+ * http://www.hackersdelight.org/HDcode/newCode/divDouble.c
*/
-#if BITS_PER_LONG == 32
-uint64_t
-__udivdi3(uint64_t dividend, uint64_t divisor)
-{
-#if defined(HAVE_DIV64_64) /* 2.6.22 - 2.6.25 API */
- return div64_64(dividend, divisor);
-#elif defined(HAVE_DIV64_U64) /* 2.6.26 - 2.6.x API */
- return div64_u64(dividend, divisor);
-#else
- /* Implementation from 2.6.30 kernel */
- uint32_t high, d;
- high = divisor >> 32;
- if (high) {
- unsigned int shift = fls(high);
+/*
+ * Calculate number of leading of zeros for a 64-bit value.
+ */
+static int
+nlz64(uint64_t x) {
+ register int n = 0;
+
+ if (x == 0)
+ return 64;
- d = divisor >> shift;
- dividend >>= shift;
- } else
- d = divisor;
+ 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;}
- do_div(dividend, d);
+ return n;
+}
- return dividend;
-#endif /* HAVE_DIV64_64, HAVE_DIV64_U64 */
+/*
+ * Newer kernels have a div_u64() function but we define our own
+ * to simplify portibility between kernel versions.
+ */
+static inline uint64_t
+__div_u64(uint64_t u, uint32_t v)
+{
+ (void) do_div(u, v);
+ return u;
+}
+
+/*
+ * Implementation of 64-bit unsigned division for 32-bit machines.
+ *
+ * First the procedure takes care of the case in which the divisor is a
+ * 32-bit quantity. There are two subcases: (1) If the left half of the
+ * dividend is less than the divisor, one execution of do_div() is all that
+ * is required (overflow is not possible). (2) Otherwise it does two
+ * divisions, using the grade school method.
+ */
+uint64_t
+__udivdi3(uint64_t u, uint64_t v)
+{
+ uint64_t u0, u1, v1, q0, q1, k;
+ int n;
+
+ 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.
+ } else { // If u/v would overflow:
+ u1 = u >> 32; // Break u into two halves.
+ u0 = u & 0xFFFFFFFF;
+ q1 = __div_u64(u1, v); // First quotient digit.
+ k = u1 - q1 * v; // First remainder, < v.
+ u0 += (k << 32);
+ q0 = __div_u64(u0, v); // Seconds quotient digit.
+ return (q1 << 32) + q0;
+ }
+ } else { // If v >= 2**32:
+ n = nlz64(v); // 0 <= n <= 31.
+ v1 = (v << n) >> 32; // Normalize divisor, MSB is 1.
+ u1 = u >> 1; // To ensure no overflow.
+ q1 = __div_u64(u1, v1); // Get quotient from
+ q0 = (q1 << n) >> 31; // Undo normalization and
+ // division of u by 2.
+ if (q0 != 0) // Make q0 correct or
+ q0 = q0 - 1; // too small by 1.
+ if ((u - q0 * v) >= v)
+ q0 = q0 + 1; // Now q0 is correct.
+
+ return q0;
+ }
}
EXPORT_SYMBOL(__udivdi3);
/*
- * Implementation of 64 bit modulo for 32-bit machines.
+ * Implementation of 64-bit signed division for 32-bit machines.
+ */
+int64_t
+__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.
+}
+EXPORT_SYMBOL(__divdi3);
+
+/*
+ * Implementation of 64-bit unsigned modulo for 32-bit machines.
*/
uint64_t
__umoddi3(uint64_t dividend, uint64_t divisor)
return (dividend - (divisor * __udivdi3(dividend, divisor)));
}
EXPORT_SYMBOL(__umoddi3);
+
#endif /* BITS_PER_LONG */
/* NOTE: The strtoxx behavior is solely based on my reading of the Solaris
* 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 SBUG() immediately so we know about it.
+ * thing does somehow happen PANIC immediately so we know about it.
*/
void
__put_task_struct(struct task_struct *t)
{
- SBUG();
+ PANIC("Unexpectly put last reference on task %d\n", (int)t->pid);
}
EXPORT_SYMBOL(__put_task_struct);
#endif /* HAVE_PUT_TASK_STRUCT */
}
EXPORT_SYMBOL(__utsname);
+
+/*
+ * Read the unique system identifier from the /etc/hostid file.
+ *
+ * The behavior of /usr/bin/hostid on Linux systems with the
+ * regular eglibc and coreutils is:
+ *
+ * 1. Generate the value if the /etc/hostid file does not exist
+ * or if the /etc/hostid file is less than four bytes in size.
+ *
+ * 2. If the /etc/hostid file is at least 4 bytes, then return
+ * the first four bytes [0..3] in native endian order.
+ *
+ * 3. Always ignore bytes [4..] if they exist in the file.
+ *
+ * Only the first four bytes are significant, even on systems that
+ * have a 64-bit word size.
+ *
+ * See:
+ *
+ * eglibc: sysdeps/unix/sysv/linux/gethostid.c
+ * coreutils: src/hostid.c
+ *
+ * Notes:
+ *
+ * The /etc/hostid file on Solaris is a text file that often reads:
+ *
+ * # DO NOT EDIT
+ * "0123456789"
+ *
+ * Directly copying this file to Linux results in a constant
+ * hostid of 4f442023 because the default comment constitutes
+ * the first four bytes of the file.
+ *
+ */
+
+char *spl_hostid_path = HW_HOSTID_PATH;
+module_param(spl_hostid_path, charp, 0444);
+MODULE_PARM_DESC(spl_hostid_path, "The system hostid file (/etc/hostid)");
+
static int
-set_hostid(void)
+hostid_read(void)
{
- char sh_path[] = "/bin/sh";
- char *argv[] = { sh_path,
+ int result;
+ uint64_t size;
+ struct _buf *file;
+ unsigned long hostid = 0;
+
+ file = kobj_open_file(spl_hostid_path);
+
+ if (file == (struct _buf *)-1)
+ return -1;
+
+ result = kobj_get_filesize(file, &size);
+
+ if (result != 0) {
+ printk(KERN_WARNING
+ "SPL: kobj_get_filesize returned %i on %s\n",
+ result, spl_hostid_path);
+ kobj_close_file(file);
+ return -2;
+ }
+
+ 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;
+ }
+
+ /* 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);
+ kobj_close_file(file);
+ return -4;
+ }
+
+ /* Mask down to 32 bits like coreutils does. */
+ spl_hostid = hostid & HW_HOSTID_MASK;
+ kobj_close_file(file);
+ return 0;
+}
+
+#define GET_HOSTID_CMD \
+ "exec 0</dev/null " \
+ " 1>/proc/sys/kernel/spl/hostid " \
+ " 2>/dev/null; " \
+ "hostid"
+
+static int
+hostid_exec(void)
+{
+ char *argv[] = { "/bin/sh",
"-c",
- "/usr/bin/hostid >/proc/sys/kernel/spl/hostid",
+ GET_HOSTID_CMD,
NULL };
char *envp[] = { "HOME=/",
"TERM=linux",
* '/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(sh_path, argv, envp, 1);
+ 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);
#ifndef HAVE_KALLSYMS_LOOKUP_NAME
/*
- * Because kallsyms_lookup_name() is no longer exported in the
- * mainline kernel we are forced to resort to somewhat drastic
- * measures. This function replaces the functionality by performing
- * an upcall to user space where /proc/kallsyms is consulted for
- * the requested address.
+ * 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.
+ *
*/
-#define GET_KALLSYMS_ADDR_CMD \
- "awk '{ if ( $3 == \"kallsyms_lookup_name\") { print $1 } }' " \
- "/proc/kallsyms >/proc/sys/kernel/spl/kallsyms_lookup_name"
+
+#define GET_KALLSYMS_ADDR_CMD \
+ "exec 0</dev/null " \
+ " 1>/proc/sys/kernel/spl/kallsyms_lookup_name " \
+ " 2>/dev/null; " \
+ "awk '{ if ( $3 == \"kallsyms_lookup_name\" ) { print $1 } }' " \
+ " /proc/kallsyms "
static int
set_kallsyms_lookup_name(void)
{
- char sh_path[] = "/bin/sh";
- char *argv[] = { sh_path,
+ char *argv[] = { "/bin/sh",
"-c",
GET_KALLSYMS_ADDR_CMD,
NULL };
NULL };
int rc;
- rc = call_usermodehelper(sh_path, argv, envp, 1);
+ 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);
{
int rc = 0;
- if ((rc = debug_init()))
+ if ((rc = spl_debug_init()))
return rc;
if ((rc = spl_kmem_init()))
- GOTO(out1, rc);
+ SGOTO(out1, rc);
if ((rc = spl_mutex_init()))
- GOTO(out2, rc);
+ SGOTO(out2, rc);
if ((rc = spl_rw_init()))
- GOTO(out3, rc);
+ SGOTO(out3, rc);
if ((rc = spl_taskq_init()))
- GOTO(out4, rc);
+ SGOTO(out4, rc);
+
+ if ((rc = spl_vn_init()))
+ SGOTO(out5, rc);
+
+ if ((rc = spl_proc_init()))
+ SGOTO(out6, rc);
- if ((rc = vn_init()))
- GOTO(out5, rc);
+ if ((rc = spl_kstat_init()))
+ SGOTO(out7, rc);
- if ((rc = proc_init()))
- GOTO(out6, rc);
+ if ((rc = spl_tsd_init()))
+ SGOTO(out8, rc);
- if ((rc = kstat_init()))
- GOTO(out7, rc);
+ if ((rc = spl_zlib_init()))
+ SGOTO(out9, rc);
- if ((rc = set_hostid()))
- GOTO(out8, rc = -EADDRNOTAVAIL);
+ /*
+ * 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 (spl_hostid == HW_INVALID_HOSTID
+ && (rc = hostid_read()) && (rc = hostid_exec()))
+ SGOTO(out10, rc = -EADDRNOTAVAIL);
#ifndef HAVE_KALLSYMS_LOOKUP_NAME
if ((rc = set_kallsyms_lookup_name()))
- GOTO(out8, rc = -EADDRNOTAVAIL);
+ SGOTO(out10, rc = -EADDRNOTAVAIL);
#endif /* HAVE_KALLSYMS_LOOKUP_NAME */
if ((rc = spl_kmem_init_kallsyms_lookup()))
- GOTO(out8, rc);
-
- printk("SPL: Loaded Solaris Porting Layer v%s\n", SPL_META_VERSION);
- RETURN(rc);
+ SGOTO(out10, rc);
+
+ if ((rc = spl_vn_init_kallsyms_lookup()))
+ SGOTO(out10, 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:
+ spl_zlib_fini();
+out9:
+ spl_tsd_fini();
out8:
- kstat_fini();
+ spl_kstat_fini();
out7:
- proc_fini();
+ spl_proc_fini();
out6:
- vn_fini();
+ spl_vn_fini();
out5:
spl_taskq_fini();
out4:
out2:
spl_kmem_fini();
out1:
- debug_fini();
+ spl_debug_fini();
- printk("SPL: Failed to Load Solaris Porting Layer v%s, "
- "rc = %d\n", SPL_META_VERSION, 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;
}
static void
spl_fini(void)
{
- ENTRY;
-
- printk("SPL: Unloaded Solaris Porting Layer v%s\n", SPL_META_VERSION);
- kstat_fini();
- proc_fini();
- vn_fini();
+ SENTRY;
+
+ printk(KERN_NOTICE "SPL: Unloaded module v%s%s\n",
+ SPL_META_VERSION, SPL_DEBUG_STR);
+ spl_zlib_fini();
+ spl_tsd_fini();
+ spl_kstat_fini();
+ spl_proc_fini();
+ spl_vn_fini();
spl_taskq_fini();
spl_rw_fini();
spl_mutex_fini();
spl_kmem_fini();
- debug_fini();
+ spl_debug_fini();
}
/* Called when a dependent module is loaded */