]> git.proxmox.com Git - mirror_zfs-debian.git/blobdiff - lib/libzpool/kernel.c
New upstream version 0.7.11
[mirror_zfs-debian.git] / lib / libzpool / kernel.c
index c1ce82d1b73c748c98ac149e172ea03e50c6a9a1..3ea8778b1dc82a193e39587e7f9820fa6bae022e 100644 (file)
@@ -20,6 +20,7 @@
  */
 /*
  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2016 Actifio, Inc. All rights reserved.
  */
 
 #include <assert.h>
 #include <stdlib.h>
 #include <string.h>
 #include <zlib.h>
+#include <libgen.h>
 #include <sys/signal.h>
 #include <sys/spa.h>
 #include <sys/stat.h>
 #include <sys/processor.h>
 #include <sys/zfs_context.h>
+#include <sys/rrwlock.h>
 #include <sys/utsname.h>
 #include <sys/time.h>
 #include <sys/systeminfo.h>
+#include <zfs_fletcher.h>
+#include <sys/crypto/icp.h>
 
 /*
  * Emulation of kernel services in userland.
@@ -46,10 +51,11 @@ int aok;
 uint64_t physmem;
 vnode_t *rootdir = (vnode_t *)0xabcd1234;
 char hw_serial[HW_HOSTID_LEN];
+struct utsname hw_utsname;
+vmem_t *zio_arena = NULL;
 
-struct utsname utsname = {
-       "userland", "libzpool", "1", "1", "na"
-};
+/* If set, all blocks read will be copied to the specified directory. */
+char *vn_dumpdir = NULL;
 
 /* this only exists to have its address taken */
 struct proc p0;
@@ -65,7 +71,7 @@ pthread_mutex_t kthread_lock = PTHREAD_MUTEX_INITIALIZER;
 pthread_key_t kthread_key;
 int kthread_nr = 0;
 
-static void
+void
 thread_init(void)
 {
        kthread_t *kt;
@@ -73,7 +79,7 @@ thread_init(void)
        VERIFY3S(pthread_key_create(&kthread_key, NULL), ==, 0);
 
        /* Create entry for primary kthread */
-       kt = umem_zalloc(sizeof(kthread_t), UMEM_NOFAIL);
+       kt = umem_zalloc(sizeof (kthread_t), UMEM_NOFAIL);
        kt->t_tid = pthread_self();
        kt->t_func = NULL;
 
@@ -84,7 +90,7 @@ thread_init(void)
        kthread_nr = 1;
 }
 
-static void
+void
 thread_fini(void)
 {
        kthread_t *kt = curthread;
@@ -92,7 +98,7 @@ thread_fini(void)
        ASSERT(pthread_equal(kt->t_tid, pthread_self()));
        ASSERT3P(kt->t_func, ==, NULL);
 
-       umem_free(kt, sizeof(kthread_t));
+       umem_free(kt, sizeof (kthread_t));
 
        /* Wait for all threads to exit via thread_exit() */
        VERIFY3S(pthread_mutex_lock(&kthread_lock), ==, 0);
@@ -100,8 +106,7 @@ thread_fini(void)
        kthread_nr--; /* Main thread is exiting */
 
        while (kthread_nr > 0)
-               VERIFY3S(pthread_cond_wait(&kthread_cond, &kthread_lock), ==,
-                   0);
+               VERIFY0(pthread_cond_wait(&kthread_cond, &kthread_lock));
 
        ASSERT3S(kthread_nr, ==, 0);
        VERIFY3S(pthread_mutex_unlock(&kthread_lock), ==, 0);
@@ -116,78 +121,79 @@ zk_thread_current(void)
 
        ASSERT3P(kt, !=, NULL);
 
-       return kt;
+       return (kt);
 }
 
 void *
 zk_thread_helper(void *arg)
 {
-       kthread_t *kt = (kthread_t *) arg;
+       kthread_t *kt = (kthread_t *)arg;
 
        VERIFY3S(pthread_setspecific(kthread_key, kt), ==, 0);
 
        VERIFY3S(pthread_mutex_lock(&kthread_lock), ==, 0);
        kthread_nr++;
        VERIFY3S(pthread_mutex_unlock(&kthread_lock), ==, 0);
+       (void) setpriority(PRIO_PROCESS, 0, kt->t_pri);
 
        kt->t_tid = pthread_self();
-       ((thread_func_arg_t) kt->t_func)(kt->t_arg);
+       ((thread_func_arg_t)kt->t_func)(kt->t_arg);
 
        /* Unreachable, thread must exit with thread_exit() */
        abort();
 
-       return NULL;
+       return (NULL);
 }
 
 kthread_t *
 zk_thread_create(caddr_t stk, size_t stksize, thread_func_t func, void *arg,
-             size_t len, proc_t *pp, int state, pri_t pri, int detachstate)
+    uint64_t len, proc_t *pp, int state, pri_t pri, int detachstate)
 {
        kthread_t *kt;
        pthread_attr_t attr;
-       size_t stack;
+       char *stkstr;
 
-       ASSERT3S(state & ~TS_RUN, ==, 0);
+       ASSERT0(state & ~TS_RUN);
+       ASSERT0(len);
 
-       kt = umem_zalloc(sizeof(kthread_t), UMEM_NOFAIL);
+       kt = umem_zalloc(sizeof (kthread_t), UMEM_NOFAIL);
        kt->t_func = func;
        kt->t_arg = arg;
+       kt->t_pri = pri;
+
+       VERIFY0(pthread_attr_init(&attr));
+       VERIFY0(pthread_attr_setdetachstate(&attr, detachstate));
 
        /*
-        * The Solaris kernel stack size is 24k for x86/x86_64.
-        * The Linux kernel stack size is 8k for x86/x86_64.
-        *
-        * We reduce the default stack size in userspace, to ensure
-        * we observe stack overruns in user space as well as in
-        * kernel space. In practice we can't set the userspace stack
-        * size to 8k because differences in stack usage between kernel
-        * space and userspace could lead to spurious stack overflows
-        * (especially when debugging is enabled). Nevertheless, we try
-        * to set it to the lowest value that works (currently 8k*4).
-        * PTHREAD_STACK_MIN is the minimum stack required for a NULL
-        * procedure in user space and is added in to the stack
-        * requirements.
-        *
-        * Some buggy NPTL threading implementations include the
-        * guard area within the stack size allocations.  In
-        * this case we allocate an extra page to account for the
-        * guard area since we only have two pages of usable stack
-        * on Linux.
+        * We allow the default stack size in user space to be specified by
+        * setting the ZFS_STACK_SIZE environment variable.  This allows us
+        * the convenience of observing and debugging stack overruns in
+        * user space.  Explicitly specified stack sizes will be honored.
+        * The usage of ZFS_STACK_SIZE is discussed further in the
+        * ENVIRONMENT VARIABLES sections of the ztest(1) man page.
         */
+       if (stksize == 0) {
+               stkstr = getenv("ZFS_STACK_SIZE");
 
-       stack = PTHREAD_STACK_MIN + MAX(stksize, STACK_SIZE) * 4;
-
-       VERIFY3S(pthread_attr_init(&attr), ==, 0);
-       VERIFY3S(pthread_attr_setstacksize(&attr, stack), ==, 0);
-       VERIFY3S(pthread_attr_setguardsize(&attr, PAGESIZE), ==, 0);
-       VERIFY3S(pthread_attr_setdetachstate(&attr, detachstate), ==, 0);
+               if (stkstr == NULL)
+                       stksize = TS_STACK_MAX;
+               else
+                       stksize = MAX(atoi(stkstr), TS_STACK_MIN);
+       }
 
-       VERIFY3S(pthread_create(&kt->t_tid, &attr, &zk_thread_helper, kt),
-           ==, 0);
+       VERIFY3S(stksize, >, 0);
+       stksize = P2ROUNDUP(MAX(stksize, TS_STACK_MIN), PAGESIZE);
+       /*
+        * If this ever fails, it may be because the stack size is not a
+        * multiple of system page size.
+        */
+       VERIFY0(pthread_attr_setstacksize(&attr, stksize));
+       VERIFY0(pthread_attr_setguardsize(&attr, PAGESIZE));
 
-       VERIFY3S(pthread_attr_destroy(&attr), ==, 0);
+       VERIFY0(pthread_create(&kt->t_tid, &attr, &zk_thread_helper, kt));
+       VERIFY0(pthread_attr_destroy(&attr));
 
-       return kt;
+       return (kt);
 }
 
 void
@@ -197,13 +203,13 @@ zk_thread_exit(void)
 
        ASSERT(pthread_equal(kt->t_tid, pthread_self()));
 
-       umem_free(kt, sizeof(kthread_t));
+       umem_free(kt, sizeof (kthread_t));
 
-       pthread_mutex_lock(&kthread_lock);
+       VERIFY0(pthread_mutex_lock(&kthread_lock));
        kthread_nr--;
-       pthread_mutex_unlock(&kthread_lock);
+       VERIFY0(pthread_mutex_unlock(&kthread_lock));
 
-       pthread_cond_broadcast(&kthread_cond);
+       VERIFY0(pthread_cond_broadcast(&kthread_cond));
        pthread_exit((void *)TS_MAGIC);
 }
 
@@ -223,8 +229,8 @@ zk_thread_join(kt_did_t tid)
  */
 /*ARGSUSED*/
 kstat_t *
-kstat_create(char *module, int instance, char *name, char *class,
-    uchar_t type, ulong_t ndata, uchar_t ks_flag)
+kstat_create(const char *module, int instance, const char *name,
+    const char *class, uchar_t type, ulong_t ndata, uchar_t ks_flag)
 {
        return (NULL);
 }
@@ -239,6 +245,43 @@ void
 kstat_delete(kstat_t *ksp)
 {}
 
+/*ARGSUSED*/
+void
+kstat_waitq_enter(kstat_io_t *kiop)
+{}
+
+/*ARGSUSED*/
+void
+kstat_waitq_exit(kstat_io_t *kiop)
+{}
+
+/*ARGSUSED*/
+void
+kstat_runq_enter(kstat_io_t *kiop)
+{}
+
+/*ARGSUSED*/
+void
+kstat_runq_exit(kstat_io_t *kiop)
+{}
+
+/*ARGSUSED*/
+void
+kstat_waitq_to_runq(kstat_io_t *kiop)
+{}
+
+/*ARGSUSED*/
+void
+kstat_runq_back_to_waitq(kstat_io_t *kiop)
+{}
+
+void
+kstat_set_raw_ops(kstat_t *ksp,
+    int (*headers)(char *buf, size_t size),
+    int (*data)(char *buf, size_t size, void *data),
+    void *(*addr)(kstat_t *ksp, loff_t index))
+{}
+
 /*
  * =========================================================================
  * mutexes
@@ -260,7 +303,7 @@ mutex_destroy(kmutex_t *mp)
 {
        ASSERT3U(mp->m_magic, ==, MTX_MAGIC);
        ASSERT3P(mp->m_owner, ==, MTX_INIT);
-       VERIFY3S(pthread_mutex_destroy(&(mp)->m_lock), ==, 0);
+       ASSERT0(pthread_mutex_destroy(&(mp)->m_lock));
        mp->m_owner = MTX_DEST;
        mp->m_magic = 0;
 }
@@ -279,13 +322,15 @@ mutex_enter(kmutex_t *mp)
 int
 mutex_tryenter(kmutex_t *mp)
 {
+       int err;
        ASSERT3U(mp->m_magic, ==, MTX_MAGIC);
        ASSERT3P(mp->m_owner, !=, MTX_DEST);
-       if (0 == pthread_mutex_trylock(&mp->m_lock)) {
+       if (0 == (err = pthread_mutex_trylock(&mp->m_lock))) {
                ASSERT3P(mp->m_owner, ==, MTX_INIT);
                mp->m_owner = curthread;
                return (1);
        } else {
+               VERIFY3S(err, ==, EBUSY);
                return (0);
        }
 }
@@ -334,7 +379,7 @@ void
 rw_destroy(krwlock_t *rwlp)
 {
        ASSERT3U(rwlp->rw_magic, ==, RW_MAGIC);
-
+       ASSERT(rwlp->rw_readers == 0 && rwlp->rw_wr_owner == RW_INIT);
        VERIFY3S(pthread_rwlock_destroy(&rwlp->rw_lock), ==, 0);
        rwlp->rw_magic = 0;
 }
@@ -427,14 +472,14 @@ cv_init(kcondvar_t *cv, char *name, int type, void *arg)
 {
        ASSERT3S(type, ==, CV_DEFAULT);
        cv->cv_magic = CV_MAGIC;
-       VERIFY3S(pthread_cond_init(&cv->cv, NULL), ==, 0);
+       VERIFY0(pthread_cond_init(&cv->cv, NULL));
 }
 
 void
 cv_destroy(kcondvar_t *cv)
 {
        ASSERT3U(cv->cv_magic, ==, CV_MAGIC);
-       VERIFY3S(pthread_cond_destroy(&cv->cv), ==, 0);
+       VERIFY0(pthread_cond_destroy(&cv->cv));
        cv->cv_magic = 0;
 }
 
@@ -444,9 +489,7 @@ cv_wait(kcondvar_t *cv, kmutex_t *mp)
        ASSERT3U(cv->cv_magic, ==, CV_MAGIC);
        ASSERT3P(mutex_owner(mp), ==, curthread);
        mp->m_owner = MTX_INIT;
-       int ret = pthread_cond_wait(&cv->cv, &mp->m_lock);
-       if (ret != 0)
-               VERIFY3S(ret, ==, EINTR);
+       VERIFY0(pthread_cond_wait(&cv->cv, &mp->m_lock));
        mp->m_owner = curthread;
 }
 
@@ -455,12 +498,11 @@ cv_timedwait(kcondvar_t *cv, kmutex_t *mp, clock_t abstime)
 {
        int error;
        struct timeval tv;
-       timestruc_t ts;
+       struct timespec ts;
        clock_t delta;
 
        ASSERT3U(cv->cv_magic, ==, CV_MAGIC);
 
-top:
        delta = abstime - ddi_get_lbolt();
        if (delta <= 0)
                return (-1);
@@ -468,7 +510,7 @@ top:
        VERIFY(gettimeofday(&tv, NULL) == 0);
 
        ts.tv_sec = tv.tv_sec + delta / hz;
-       ts.tv_nsec = tv.tv_usec * 1000 + (delta % hz) * (NANOSEC / hz);
+       ts.tv_nsec = tv.tv_usec * NSEC_PER_USEC + (delta % hz) * (NANOSEC / hz);
        if (ts.tv_nsec >= NANOSEC) {
                ts.tv_sec++;
                ts.tv_nsec -= NANOSEC;
@@ -482,10 +524,48 @@ top:
        if (error == ETIMEDOUT)
                return (-1);
 
-       if (error == EINTR)
-               goto top;
+       VERIFY0(error);
+
+       return (1);
+}
+
+/*ARGSUSED*/
+clock_t
+cv_timedwait_hires(kcondvar_t *cv, kmutex_t *mp, hrtime_t tim, hrtime_t res,
+    int flag)
+{
+       int error;
+       struct timeval tv;
+       struct timespec ts;
+       hrtime_t delta;
+
+       ASSERT(flag == 0 || flag == CALLOUT_FLAG_ABSOLUTE);
 
-       VERIFY3S(error, ==, 0);
+       delta = tim;
+       if (flag & CALLOUT_FLAG_ABSOLUTE)
+               delta -= gethrtime();
+
+       if (delta <= 0)
+               return (-1);
+
+       VERIFY(gettimeofday(&tv, NULL) == 0);
+
+       ts.tv_sec = tv.tv_sec + delta / NANOSEC;
+       ts.tv_nsec = tv.tv_usec * NSEC_PER_USEC + (delta % NANOSEC);
+       if (ts.tv_nsec >= NANOSEC) {
+               ts.tv_sec++;
+               ts.tv_nsec -= NANOSEC;
+       }
+
+       ASSERT(mutex_owner(mp) == curthread);
+       mp->m_owner = MTX_INIT;
+       error = pthread_cond_timedwait(&cv->cv, &mp->m_lock, &ts);
+       mp->m_owner = curthread;
+
+       if (error == ETIMEDOUT)
+               return (-1);
+
+       VERIFY0(error);
 
        return (1);
 }
@@ -494,14 +574,14 @@ void
 cv_signal(kcondvar_t *cv)
 {
        ASSERT3U(cv->cv_magic, ==, CV_MAGIC);
-       VERIFY3S(pthread_cond_signal(&cv->cv), ==, 0);
+       VERIFY0(pthread_cond_signal(&cv->cv));
 }
 
 void
 cv_broadcast(kcondvar_t *cv)
 {
        ASSERT3U(cv->cv_magic, ==, CV_MAGIC);
-       VERIFY3S(pthread_cond_broadcast(&cv->cv), ==, 0);
+       VERIFY0(pthread_cond_broadcast(&cv->cv));
 }
 
 /*
@@ -520,9 +600,10 @@ cv_broadcast(kcondvar_t *cv)
 int
 vn_open(char *path, int x1, int flags, int mode, vnode_t **vpp, int x2, int x3)
 {
-       int fd;
+       int fd = -1;
+       int dump_fd = -1;
        vnode_t *vp;
-       int old_umask;
+       int old_umask = 0;
        char *realpath;
        struct stat64 st;
        int err;
@@ -588,17 +669,38 @@ vn_open(char *path, int x1, int flags, int mode, vnode_t **vpp, int x2, int x3)
         * FREAD and FWRITE to the corresponding O_RDONLY, O_WRONLY, and O_RDWR.
         */
        fd = open64(realpath, flags - FREAD, mode);
-       free(realpath);
+       if (fd == -1) {
+               err = errno;
+               free(realpath);
+               return (err);
+       }
 
        if (flags & FCREAT)
                (void) umask(old_umask);
 
-       if (fd == -1)
-               return (errno);
+       if (vn_dumpdir != NULL) {
+               char *dumppath = umem_zalloc(MAXPATHLEN, UMEM_NOFAIL);
+               (void) snprintf(dumppath, MAXPATHLEN,
+                   "%s/%s", vn_dumpdir, basename(realpath));
+               dump_fd = open64(dumppath, O_CREAT | O_WRONLY, 0666);
+               umem_free(dumppath, MAXPATHLEN);
+               if (dump_fd == -1) {
+                       err = errno;
+                       free(realpath);
+                       close(fd);
+                       return (err);
+               }
+       } else {
+               dump_fd = -1;
+       }
+
+       free(realpath);
 
        if (fstat64_blk(fd, &st) == -1) {
                err = errno;
                close(fd);
+               if (dump_fd != -1)
+                       close(dump_fd);
                return (err);
        }
 
@@ -609,6 +711,7 @@ vn_open(char *path, int x1, int flags, int mode, vnode_t **vpp, int x2, int x3)
        vp->v_fd = fd;
        vp->v_size = st.st_size;
        vp->v_path = spa_strdup(path);
+       vp->v_dump_fd = dump_fd;
 
        return (0);
 }
@@ -635,12 +738,17 @@ vn_openat(char *path, int x1, int flags, int mode, vnode_t **vpp, int x2,
 /*ARGSUSED*/
 int
 vn_rdwr(int uio, vnode_t *vp, void *addr, ssize_t len, offset_t offset,
-       int x1, int x2, rlim64_t x3, void *x4, ssize_t *residp)
+    int x1, int x2, rlim64_t x3, void *x4, ssize_t *residp)
 {
        ssize_t rc, done = 0, split;
 
        if (uio == UIO_READ) {
                rc = pread64(vp->v_fd, addr, len, offset);
+               if (vp->v_dump_fd != -1 && rc != -1) {
+                       int status;
+                       status = pwrite64(vp->v_dump_fd, addr, rc, offset);
+                       ASSERT(status != -1);
+               }
        } else {
                /*
                 * To simulate partial disk writes, we split writes into two
@@ -664,7 +772,7 @@ vn_rdwr(int uio, vnode_t *vp, void *addr, ssize_t len, offset_t offset,
                 * (memory or disk) due to O_DIRECT, so we abort() in order to
                 * catch the offender.
                 */
-                abort();
+               abort();
        }
 #endif
        if (rc == -1)
@@ -683,6 +791,8 @@ void
 vn_close(vnode_t *vp)
 {
        close(vp->v_fd);
+       if (vp->v_dump_fd != -1)
+               close(vp->v_dump_fd);
        spa_strfree(vp->v_path);
        umem_free(vp, sizeof (vnode_t));
 }
@@ -773,6 +883,9 @@ dprintf_setup(int *argc, char **argv)
         */
        if (dprintf_find_string("on"))
                dprintf_print_all = 1;
+
+       if (dprintf_string != NULL)
+               zfs_flags |= ZFS_DEBUG_DPRINTF;
 }
 
 /*
@@ -804,7 +917,7 @@ __dprintf(const char *file, const char *func, int line, const char *fmt, ...)
                if (dprintf_find_string("pid"))
                        (void) printf("%d ", getpid());
                if (dprintf_find_string("tid"))
-                       (void) printf("%u ", (uint_t) pthread_self());
+                       (void) printf("%u ", (uint_t)pthread_self());
                if (dprintf_find_string("cpu"))
                        (void) printf("%u ", getcpuid());
                if (dprintf_find_string("time"))
@@ -894,10 +1007,11 @@ kobj_open_file(char *name)
 int
 kobj_read_file(struct _buf *file, char *buf, unsigned size, unsigned off)
 {
-       ssize_t resid;
+       ssize_t resid = 0;
 
-       vn_rdwr(UIO_READ, (vnode_t *)file->_fd, buf, size, (offset_t)off,
-           UIO_SYSSPACE, 0, 0, 0, &resid);
+       if (vn_rdwr(UIO_READ, (vnode_t *)file->_fd, buf, size, (offset_t)off,
+           UIO_SYSSPACE, 0, 0, 0, &resid) != 0)
+               return (-1);
 
        return (size - resid);
 }
@@ -932,7 +1046,7 @@ kobj_get_filesize(struct _buf *file, uint64_t *size)
 void
 delay(clock_t ticks)
 {
-       poll(0, 0, ticks * (1000 / hz));
+       (void) poll(0, 0, ticks * (1000 / hz));
 }
 
 /*
@@ -941,10 +1055,86 @@ delay(clock_t ticks)
  * High order bit is 31 (or 63 in _LP64 kernel).
  */
 int
-highbit(ulong_t i)
+highbit64(uint64_t i)
 {
        register int h = 1;
 
+       if (i == 0)
+               return (0);
+       if (i & 0xffffffff00000000ULL) {
+               h += 32; i >>= 32;
+       }
+       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;
+       }
+       return (h);
+}
+
+/*
+ * Find lowest one bit set.
+ * Returns bit number + 1 of lowest bit that is set, otherwise returns 0.
+ * This is basically a reimplementation of ffsll(), which is GNU specific.
+ */
+int
+lowbit64(uint64_t i)
+{
+       register int h = 64;
+       if (i == 0)
+               return (0);
+
+       if (i & 0x00000000ffffffffULL)
+               h -= 32;
+       else
+               i >>= 32;
+
+       if (i & 0x0000ffff)
+               h -= 16;
+       else
+               i >>= 16;
+
+       if (i & 0x00ff)
+               h -= 8;
+       else
+               i >>= 8;
+
+       if (i & 0x0f)
+               h -= 4;
+       else
+               i >>= 4;
+
+       if (i & 0x3)
+               h -= 2;
+       else
+               i >>= 2;
+
+       if (i & 0x1)
+               h -= 1;
+
+       return (h);
+}
+
+/*
+ * Find highest one bit set.
+ * Returns bit number + 1 of highest bit that is set, otherwise returns 0.
+ * High order bit is 31 (or 63 in _LP64 kernel).
+ */
+int
+highbit(ulong_t i)
+{
+register int h = 1;
+
        if (i == 0)
                return (0);
 #ifdef _LP64
@@ -970,8 +1160,61 @@ highbit(ulong_t i)
        return (h);
 }
 
+/*
+ * Find lowest one bit set.
+ *     Returns bit number + 1 of lowest bit that is set, otherwise returns 0.
+ * Low order bit is 0.
+ */
+int
+lowbit(ulong_t i)
+{
+       register int h = 1;
+
+       if (i == 0)
+               return (0);
+
+#ifdef _LP64
+       if (!(i & 0xffffffff)) {
+               h += 32; i >>= 32;
+       }
+#endif
+       if (!(i & 0xffff)) {
+               h += 16; i >>= 16;
+       }
+       if (!(i & 0xff)) {
+               h += 8; i >>= 8;
+       }
+       if (!(i & 0xf)) {
+               h += 4; i >>= 4;
+       }
+       if (!(i & 0x3)) {
+               h += 2; i >>= 2;
+       }
+       if (!(i & 0x1)) {
+               h += 1;
+       }
+       return (h);
+}
+
 static int random_fd = -1, urandom_fd = -1;
 
+void
+random_init(void)
+{
+       VERIFY((random_fd = open("/dev/random", O_RDONLY)) != -1);
+       VERIFY((urandom_fd = open("/dev/urandom", O_RDONLY)) != -1);
+}
+
+void
+random_fini(void)
+{
+       close(random_fd);
+       close(urandom_fd);
+
+       random_fd = -1;
+       urandom_fd = -1;
+}
+
 static int
 random_get_bytes_common(uint8_t *ptr, size_t len, int fd)
 {
@@ -1024,6 +1267,12 @@ ddi_strtoull(const char *str, char **nptr, int base, u_longlong_t *result)
        return (0);
 }
 
+utsname_t *
+utsname(void)
+{
+       return (&hw_utsname);
+}
+
 /*
  * =========================================================================
  * kernel emulation setup & teardown
@@ -1039,9 +1288,69 @@ umem_out_of_memory(void)
        return (0);
 }
 
+#define        HOSTID_MASK 0xffffffff
+
+static unsigned long
+get_spl_hostid(void)
+{
+       FILE *f;
+       unsigned long hostid;
+       char *env;
+
+       /*
+        * Allow the hostid to be subverted for testing.
+        */
+       env = getenv("ZFS_HOSTID");
+       if (env) {
+               hostid = strtoull(env, NULL, 0);
+               return (hostid & HOSTID_MASK);
+       }
+
+       f = fopen("/sys/module/spl/parameters/spl_hostid", "r");
+       if (!f)
+               return (0);
+
+       if (fscanf(f, "%lu", &hostid) != 1)
+               hostid = 0;
+
+       fclose(f);
+
+       return (hostid & HOSTID_MASK);
+}
+
+unsigned long
+get_system_hostid(void)
+{
+       unsigned long system_hostid = get_spl_hostid();
+       /*
+        * We do not use the library call gethostid() because
+        * it generates a hostid value that the kernel is
+        * unaware of, if the spl_hostid module parameter has not
+        * been set and there is no system hostid file (e.g.
+        * /etc/hostid).  The kernel and userspace must agree.
+        * See comments above hostid_read() in the SPL.
+        */
+       if (system_hostid == 0) {
+               int fd, rc;
+               unsigned long hostid;
+               int hostid_size = 4;  /* 4 bytes regardless of arch */
+
+               fd = open("/etc/hostid", O_RDONLY);
+               if (fd >= 0) {
+                       rc = read(fd, &hostid, hostid_size);
+                       if (rc > 0)
+                               system_hostid = (hostid & HOSTID_MASK);
+                       close(fd);
+               }
+       }
+       return (system_hostid);
+}
+
 void
 kernel_init(int mode)
 {
+       extern uint_t rrw_tsd_key;
+
        umem_nofail_callback(umem_out_of_memory);
 
        physmem = sysconf(_SC_PHYS_PAGES);
@@ -1050,30 +1359,34 @@ kernel_init(int mode)
            (double)physmem * sysconf(_SC_PAGE_SIZE) / (1ULL << 30));
 
        (void) snprintf(hw_serial, sizeof (hw_serial), "%ld",
-           (mode & FWRITE) ? gethostid() : 0);
+           (mode & FWRITE) ? get_system_hostid() : 0);
 
-       VERIFY((random_fd = open("/dev/random", O_RDONLY)) != -1);
-       VERIFY((urandom_fd = open("/dev/urandom", O_RDONLY)) != -1);
+       random_init();
+
+       VERIFY0(uname(&hw_utsname));
 
        thread_init();
        system_taskq_init();
+       icp_init();
 
        spa_init(mode);
+
+       fletcher_4_init();
+
+       tsd_create(&rrw_tsd_key, rrw_tsd_destroy);
 }
 
 void
 kernel_fini(void)
 {
+       fletcher_4_fini();
        spa_fini();
 
+       icp_fini();
        system_taskq_fini();
        thread_fini();
 
-       close(random_fd);
-       close(urandom_fd);
-
-       random_fd = -1;
-       urandom_fd = -1;
+       random_fini();
 }
 
 uid_t
@@ -1082,6 +1395,12 @@ crgetuid(cred_t *cr)
        return (0);
 }
 
+uid_t
+crgetruid(cred_t *cr)
+{
+       return (0);
+}
+
 gid_t
 crgetgid(cred_t *cr)
 {
@@ -1118,6 +1437,12 @@ zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
        return (0);
 }
 
+int
+secpolicy_zfs(const cred_t *cr)
+{
+       return (0);
+}
+
 ksiddomain_t *
 ksid_lookupdomain(const char *dom)
 {
@@ -1196,3 +1521,43 @@ zfs_onexit_cb_data(minor_t minor, uint64_t action_handle, void **data)
 {
        return (0);
 }
+
+fstrans_cookie_t
+spl_fstrans_mark(void)
+{
+       return ((fstrans_cookie_t)0);
+}
+
+void
+spl_fstrans_unmark(fstrans_cookie_t cookie)
+{
+}
+
+int
+__spl_pf_fstrans_check(void)
+{
+       return (0);
+}
+
+void *zvol_tag = "zvol_tag";
+
+void
+zvol_create_minors(spa_t *spa, const char *name, boolean_t async)
+{
+}
+
+void
+zvol_remove_minor(spa_t *spa, const char *name, boolean_t async)
+{
+}
+
+void
+zvol_remove_minors(spa_t *spa, const char *name, boolean_t async)
+{
+}
+
+void
+zvol_rename_minors(spa_t *spa, const char *oldname, const char *newname,
+    boolean_t async)
+{
+}