*/
/*
* 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/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.
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;
pthread_key_t kthread_key;
int kthread_nr = 0;
-static void
+void
thread_init(void)
{
kthread_t *kt;
kthread_nr = 1;
}
-static void
+void
thread_fini(void)
{
kthread_t *kt = curthread;
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);
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);
(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();
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;
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));
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);
}
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);
}
}
{
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;
}
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;
}
{
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);
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;
if (error == ETIMEDOUT)
return (-1);
- if (error == EINTR)
- goto top;
-
- VERIFY3S(error, ==, 0);
+ VERIFY0(error);
return (1);
}
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);
}
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));
}
/*
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;
* 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);
}
vp->v_fd = fd;
vp->v_size = st.st_size;
vp->v_path = spa_strdup(path);
+ vp->v_dump_fd = dump_fd;
return (0);
}
/*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
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));
}
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"))
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);
}
void
delay(clock_t ticks)
{
- poll(0, 0, ticks * (1000 / hz));
+ (void) poll(0, 0, ticks * (1000 / hz));
}
/*
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)
{
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);
}
(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
return (0);
}
+int
+secpolicy_zfs(const cred_t *cr)
+{
+ return (0);
+}
+
ksiddomain_t *
ksid_lookupdomain(const char *dom)
{
fstrans_cookie_t
spl_fstrans_mark(void)
{
- return ((fstrans_cookie_t) 0);
+ return ((fstrans_cookie_t)0);
}
void
}
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)
+{
+}