]> 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 a451026999c5657facc6d0ea55a75b8f631ba3e2..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>
@@ -29,6 +30,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <zlib.h>
+#include <libgen.h>
 #include <sys/signal.h>
 #include <sys/spa.h>
 #include <sys/stat.h>
@@ -38,6 +40,8 @@
 #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.
@@ -50,6 +54,9 @@ char hw_serial[HW_HOSTID_LEN];
 struct utsname hw_utsname;
 vmem_t *zio_arena = NULL;
 
+/* 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;
 
@@ -64,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;
@@ -83,7 +90,7 @@ thread_init(void)
        kthread_nr = 1;
 }
 
-static void
+void
 thread_fini(void)
 {
        kthread_t *kt = curthread;
@@ -99,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);
@@ -121,7 +127,7 @@ zk_thread_current(void)
 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);
 
@@ -131,7 +137,7 @@ zk_thread_helper(void *arg)
        (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();
@@ -141,13 +147,14 @@ zk_thread_helper(void *arg)
 
 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;
        char *stkstr;
 
        ASSERT0(state & ~TS_RUN);
+       ASSERT0(len);
 
        kt = umem_zalloc(sizeof (kthread_t), UMEM_NOFAIL);
        kt->t_func = func;
@@ -176,6 +183,10 @@ zk_thread_create(caddr_t stk, size_t stksize, thread_func_t func, void *arg,
 
        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));
 
@@ -194,11 +205,11 @@ zk_thread_exit(void)
 
        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);
 }
 
@@ -311,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);
        }
 }
@@ -459,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;
 }
 
@@ -476,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;
 }
 
@@ -487,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);
@@ -500,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;
@@ -514,10 +524,7 @@ top:
        if (error == ETIMEDOUT)
                return (-1);
 
-       if (error == EINTR)
-               goto top;
-
-       VERIFY3S(error, ==, 0);
+       VERIFY0(error);
 
        return (1);
 }
@@ -528,31 +535,37 @@ cv_timedwait_hires(kcondvar_t *cv, kmutex_t *mp, hrtime_t tim, hrtime_t res,
     int flag)
 {
        int error;
-       timestruc_t ts;
+       struct timeval tv;
+       struct timespec ts;
        hrtime_t delta;
 
-       ASSERT(flag == 0);
+       ASSERT(flag == 0 || flag == CALLOUT_FLAG_ABSOLUTE);
+
+       delta = tim;
+       if (flag & CALLOUT_FLAG_ABSOLUTE)
+               delta -= gethrtime();
 
-top:
-       delta = tim - gethrtime();
        if (delta <= 0)
                return (-1);
 
-       ts.tv_sec = delta / NANOSEC;
-       ts.tv_nsec = delta % NANOSEC;
+       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 = NULL;
+       mp->m_owner = MTX_INIT;
        error = pthread_cond_timedwait(&cv->cv, &mp->m_lock, &ts);
        mp->m_owner = curthread;
 
-       if (error == ETIME)
+       if (error == ETIMEDOUT)
                return (-1);
 
-       if (error == EINTR)
-               goto top;
-
-       ASSERT(error == 0);
+       VERIFY0(error);
 
        return (1);
 }
@@ -561,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));
 }
 
 /*
@@ -587,7 +600,8 @@ 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 = 0;
        char *realpath;
@@ -655,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);
        }
 
@@ -676,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);
 }
@@ -702,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
@@ -750,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));
 }
@@ -874,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"))
@@ -964,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);
 }
@@ -1002,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));
 }
 
 /*
@@ -1038,8 +1082,139 @@ highbit64(uint64_t i)
        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
+       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;
+       }
+       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)
 {
@@ -1113,27 +1288,61 @@ 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 & 0xffffffff);
+
+       return (hostid & HOSTID_MASK);
 }
 
 unsigned long
 get_system_hostid(void)
 {
        unsigned long system_hostid = get_spl_hostid();
-       if (system_hostid == 0)
-               system_hostid = gethostid() & 0xffffffff;
+       /*
+        * 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);
 }
 
@@ -1152,31 +1361,32 @@ kernel_init(int mode)
        (void) snprintf(hw_serial, sizeof (hw_serial), "%ld",
            (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
@@ -1227,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)
 {
@@ -1309,7 +1525,7 @@ zfs_onexit_cb_data(minor_t minor, uint64_t action_handle, void **data)
 fstrans_cookie_t
 spl_fstrans_mark(void)
 {
-       return ((fstrans_cookie_t) 0);
+       return ((fstrans_cookie_t)0);
 }
 
 void
@@ -1318,7 +1534,30 @@ spl_fstrans_unmark(fstrans_cookie_t cookie)
 }
 
 int
-spl_fstrans_check(void)
+__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)
+{
+}