render / rpms / qemu

Forked from rpms/qemu 10 months ago
Clone

Blame 0012-virtiofsd-Add-auxiliary-.c-s.patch

1d442b
From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
1d442b
Date: Mon, 27 Jan 2020 19:00:41 +0000
1d442b
Subject: [PATCH] virtiofsd: Add auxiliary .c's
1d442b
MIME-Version: 1.0
1d442b
Content-Type: text/plain; charset=UTF-8
1d442b
Content-Transfer-Encoding: 8bit
1d442b
1d442b
Add most of the non-main .c files we need from upstream fuse-3.8.0
1d442b
1d442b
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
1d442b
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
1d442b
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
1d442b
(cherry picked from commit ffcf8d9f8649c6e56b1193bbbc9c9f7388920043)
1d442b
---
1d442b
 tools/virtiofsd/buffer.c       | 321 ++++++++++++++++++++++++
1d442b
 tools/virtiofsd/fuse_log.c     |  40 +++
1d442b
 tools/virtiofsd/fuse_opt.c     | 423 +++++++++++++++++++++++++++++++
1d442b
 tools/virtiofsd/fuse_signals.c |  91 +++++++
1d442b
 tools/virtiofsd/helper.c       | 440 +++++++++++++++++++++++++++++++++
1d442b
 5 files changed, 1315 insertions(+)
1d442b
 create mode 100644 tools/virtiofsd/buffer.c
1d442b
 create mode 100644 tools/virtiofsd/fuse_log.c
1d442b
 create mode 100644 tools/virtiofsd/fuse_opt.c
1d442b
 create mode 100644 tools/virtiofsd/fuse_signals.c
1d442b
 create mode 100644 tools/virtiofsd/helper.c
1d442b
1d442b
diff --git a/tools/virtiofsd/buffer.c b/tools/virtiofsd/buffer.c
1d442b
new file mode 100644
1d442b
index 0000000000..5ab9b87455
1d442b
--- /dev/null
1d442b
+++ b/tools/virtiofsd/buffer.c
1d442b
@@ -0,0 +1,321 @@
1d442b
+/*
1d442b
+  FUSE: Filesystem in Userspace
1d442b
+  Copyright (C) 2010  Miklos Szeredi <miklos@szeredi.hu>
1d442b
+
1d442b
+  Functions for dealing with `struct fuse_buf` and `struct
1d442b
+  fuse_bufvec`.
1d442b
+
1d442b
+  This program can be distributed under the terms of the GNU LGPLv2.
1d442b
+  See the file COPYING.LIB
1d442b
+*/
1d442b
+
1d442b
+#define _GNU_SOURCE
1d442b
+
1d442b
+#include "config.h"
1d442b
+#include "fuse_i.h"
1d442b
+#include "fuse_lowlevel.h"
1d442b
+#include <string.h>
1d442b
+#include <unistd.h>
1d442b
+#include <errno.h>
1d442b
+#include <assert.h>
1d442b
+
1d442b
+size_t fuse_buf_size(const struct fuse_bufvec *bufv)
1d442b
+{
1d442b
+	size_t i;
1d442b
+	size_t size = 0;
1d442b
+
1d442b
+	for (i = 0; i < bufv->count; i++) {
1d442b
+		if (bufv->buf[i].size == SIZE_MAX)
1d442b
+			size = SIZE_MAX;
1d442b
+		else
1d442b
+			size += bufv->buf[i].size;
1d442b
+	}
1d442b
+
1d442b
+	return size;
1d442b
+}
1d442b
+
1d442b
+static size_t min_size(size_t s1, size_t s2)
1d442b
+{
1d442b
+	return s1 < s2 ? s1 : s2;
1d442b
+}
1d442b
+
1d442b
+static ssize_t fuse_buf_write(const struct fuse_buf *dst, size_t dst_off,
1d442b
+			      const struct fuse_buf *src, size_t src_off,
1d442b
+			      size_t len)
1d442b
+{
1d442b
+	ssize_t res = 0;
1d442b
+	size_t copied = 0;
1d442b
+
1d442b
+	while (len) {
1d442b
+		if (dst->flags & FUSE_BUF_FD_SEEK) {
1d442b
+			res = pwrite(dst->fd, (char *)src->mem + src_off, len,
1d442b
+				     dst->pos + dst_off);
1d442b
+		} else {
1d442b
+			res = write(dst->fd, (char *)src->mem + src_off, len);
1d442b
+		}
1d442b
+		if (res == -1) {
1d442b
+			if (!copied)
1d442b
+				return -errno;
1d442b
+			break;
1d442b
+		}
1d442b
+		if (res == 0)
1d442b
+			break;
1d442b
+
1d442b
+		copied += res;
1d442b
+		if (!(dst->flags & FUSE_BUF_FD_RETRY))
1d442b
+			break;
1d442b
+
1d442b
+		src_off += res;
1d442b
+		dst_off += res;
1d442b
+		len -= res;
1d442b
+	}
1d442b
+
1d442b
+	return copied;
1d442b
+}
1d442b
+
1d442b
+static ssize_t fuse_buf_read(const struct fuse_buf *dst, size_t dst_off,
1d442b
+			     const struct fuse_buf *src, size_t src_off,
1d442b
+			     size_t len)
1d442b
+{
1d442b
+	ssize_t res = 0;
1d442b
+	size_t copied = 0;
1d442b
+
1d442b
+	while (len) {
1d442b
+		if (src->flags & FUSE_BUF_FD_SEEK) {
1d442b
+			res = pread(src->fd, (char *)dst->mem + dst_off, len,
1d442b
+				     src->pos + src_off);
1d442b
+		} else {
1d442b
+			res = read(src->fd, (char *)dst->mem + dst_off, len);
1d442b
+		}
1d442b
+		if (res == -1) {
1d442b
+			if (!copied)
1d442b
+				return -errno;
1d442b
+			break;
1d442b
+		}
1d442b
+		if (res == 0)
1d442b
+			break;
1d442b
+
1d442b
+		copied += res;
1d442b
+		if (!(src->flags & FUSE_BUF_FD_RETRY))
1d442b
+			break;
1d442b
+
1d442b
+		dst_off += res;
1d442b
+		src_off += res;
1d442b
+		len -= res;
1d442b
+	}
1d442b
+
1d442b
+	return copied;
1d442b
+}
1d442b
+
1d442b
+static ssize_t fuse_buf_fd_to_fd(const struct fuse_buf *dst, size_t dst_off,
1d442b
+				 const struct fuse_buf *src, size_t src_off,
1d442b
+				 size_t len)
1d442b
+{
1d442b
+	char buf[4096];
1d442b
+	struct fuse_buf tmp = {
1d442b
+		.size = sizeof(buf),
1d442b
+		.flags = 0,
1d442b
+	};
1d442b
+	ssize_t res;
1d442b
+	size_t copied = 0;
1d442b
+
1d442b
+	tmp.mem = buf;
1d442b
+
1d442b
+	while (len) {
1d442b
+		size_t this_len = min_size(tmp.size, len);
1d442b
+		size_t read_len;
1d442b
+
1d442b
+		res = fuse_buf_read(&tmp, 0, src, src_off, this_len);
1d442b
+		if (res < 0) {
1d442b
+			if (!copied)
1d442b
+				return res;
1d442b
+			break;
1d442b
+		}
1d442b
+		if (res == 0)
1d442b
+			break;
1d442b
+
1d442b
+		read_len = res;
1d442b
+		res = fuse_buf_write(dst, dst_off, &tmp, 0, read_len);
1d442b
+		if (res < 0) {
1d442b
+			if (!copied)
1d442b
+				return res;
1d442b
+			break;
1d442b
+		}
1d442b
+		if (res == 0)
1d442b
+			break;
1d442b
+
1d442b
+		copied += res;
1d442b
+
1d442b
+		if (res < this_len)
1d442b
+			break;
1d442b
+
1d442b
+		dst_off += res;
1d442b
+		src_off += res;
1d442b
+		len -= res;
1d442b
+	}
1d442b
+
1d442b
+	return copied;
1d442b
+}
1d442b
+
1d442b
+#ifdef HAVE_SPLICE
1d442b
+static ssize_t fuse_buf_splice(const struct fuse_buf *dst, size_t dst_off,
1d442b
+			       const struct fuse_buf *src, size_t src_off,
1d442b
+			       size_t len, enum fuse_buf_copy_flags flags)
1d442b
+{
1d442b
+	int splice_flags = 0;
1d442b
+	off_t *srcpos = NULL;
1d442b
+	off_t *dstpos = NULL;
1d442b
+	off_t srcpos_val;
1d442b
+	off_t dstpos_val;
1d442b
+	ssize_t res;
1d442b
+	size_t copied = 0;
1d442b
+
1d442b
+	if (flags & FUSE_BUF_SPLICE_MOVE)
1d442b
+		splice_flags |= SPLICE_F_MOVE;
1d442b
+	if (flags & FUSE_BUF_SPLICE_NONBLOCK)
1d442b
+		splice_flags |= SPLICE_F_NONBLOCK;
1d442b
+
1d442b
+	if (src->flags & FUSE_BUF_FD_SEEK) {
1d442b
+		srcpos_val = src->pos + src_off;
1d442b
+		srcpos = &srcpos_val;
1d442b
+	}
1d442b
+	if (dst->flags & FUSE_BUF_FD_SEEK) {
1d442b
+		dstpos_val = dst->pos + dst_off;
1d442b
+		dstpos = &dstpos_val;
1d442b
+	}
1d442b
+
1d442b
+	while (len) {
1d442b
+		res = splice(src->fd, srcpos, dst->fd, dstpos, len,
1d442b
+			     splice_flags);
1d442b
+		if (res == -1) {
1d442b
+			if (copied)
1d442b
+				break;
1d442b
+
1d442b
+			if (errno != EINVAL || (flags & FUSE_BUF_FORCE_SPLICE))
1d442b
+				return -errno;
1d442b
+
1d442b
+			/* Maybe splice is not supported for this combination */
1d442b
+			return fuse_buf_fd_to_fd(dst, dst_off, src, src_off,
1d442b
+						 len);
1d442b
+		}
1d442b
+		if (res == 0)
1d442b
+			break;
1d442b
+
1d442b
+		copied += res;
1d442b
+		if (!(src->flags & FUSE_BUF_FD_RETRY) &&
1d442b
+		    !(dst->flags & FUSE_BUF_FD_RETRY)) {
1d442b
+			break;
1d442b
+		}
1d442b
+
1d442b
+		len -= res;
1d442b
+	}
1d442b
+
1d442b
+	return copied;
1d442b
+}
1d442b
+#else
1d442b
+static ssize_t fuse_buf_splice(const struct fuse_buf *dst, size_t dst_off,
1d442b
+			       const struct fuse_buf *src, size_t src_off,
1d442b
+			       size_t len, enum fuse_buf_copy_flags flags)
1d442b
+{
1d442b
+	(void) flags;
1d442b
+
1d442b
+	return fuse_buf_fd_to_fd(dst, dst_off, src, src_off, len);
1d442b
+}
1d442b
+#endif
1d442b
+
1d442b
+
1d442b
+static ssize_t fuse_buf_copy_one(const struct fuse_buf *dst, size_t dst_off,
1d442b
+				 const struct fuse_buf *src, size_t src_off,
1d442b
+				 size_t len, enum fuse_buf_copy_flags flags)
1d442b
+{
1d442b
+	int src_is_fd = src->flags & FUSE_BUF_IS_FD;
1d442b
+	int dst_is_fd = dst->flags & FUSE_BUF_IS_FD;
1d442b
+
1d442b
+	if (!src_is_fd && !dst_is_fd) {
1d442b
+		char *dstmem = (char *)dst->mem + dst_off;
1d442b
+		char *srcmem = (char *)src->mem + src_off;
1d442b
+
1d442b
+		if (dstmem != srcmem) {
1d442b
+			if (dstmem + len <= srcmem || srcmem + len <= dstmem)
1d442b
+				memcpy(dstmem, srcmem, len);
1d442b
+			else
1d442b
+				memmove(dstmem, srcmem, len);
1d442b
+		}
1d442b
+
1d442b
+		return len;
1d442b
+	} else if (!src_is_fd) {
1d442b
+		return fuse_buf_write(dst, dst_off, src, src_off, len);
1d442b
+	} else if (!dst_is_fd) {
1d442b
+		return fuse_buf_read(dst, dst_off, src, src_off, len);
1d442b
+	} else if (flags & FUSE_BUF_NO_SPLICE) {
1d442b
+		return fuse_buf_fd_to_fd(dst, dst_off, src, src_off, len);
1d442b
+	} else {
1d442b
+		return fuse_buf_splice(dst, dst_off, src, src_off, len, flags);
1d442b
+	}
1d442b
+}
1d442b
+
1d442b
+static const struct fuse_buf *fuse_bufvec_current(struct fuse_bufvec *bufv)
1d442b
+{
1d442b
+	if (bufv->idx < bufv->count)
1d442b
+		return &bufv->buf[bufv->idx];
1d442b
+	else
1d442b
+		return NULL;
1d442b
+}
1d442b
+
1d442b
+static int fuse_bufvec_advance(struct fuse_bufvec *bufv, size_t len)
1d442b
+{
1d442b
+	const struct fuse_buf *buf = fuse_bufvec_current(bufv);
1d442b
+
1d442b
+	bufv->off += len;
1d442b
+	assert(bufv->off <= buf->size);
1d442b
+	if (bufv->off == buf->size) {
1d442b
+		assert(bufv->idx < bufv->count);
1d442b
+		bufv->idx++;
1d442b
+		if (bufv->idx == bufv->count)
1d442b
+			return 0;
1d442b
+		bufv->off = 0;
1d442b
+	}
1d442b
+	return 1;
1d442b
+}
1d442b
+
1d442b
+ssize_t fuse_buf_copy(struct fuse_bufvec *dstv, struct fuse_bufvec *srcv,
1d442b
+		      enum fuse_buf_copy_flags flags)
1d442b
+{
1d442b
+	size_t copied = 0;
1d442b
+
1d442b
+	if (dstv == srcv)
1d442b
+		return fuse_buf_size(dstv);
1d442b
+
1d442b
+	for (;;) {
1d442b
+		const struct fuse_buf *src = fuse_bufvec_current(srcv);
1d442b
+		const struct fuse_buf *dst = fuse_bufvec_current(dstv);
1d442b
+		size_t src_len;
1d442b
+		size_t dst_len;
1d442b
+		size_t len;
1d442b
+		ssize_t res;
1d442b
+
1d442b
+		if (src == NULL || dst == NULL)
1d442b
+			break;
1d442b
+
1d442b
+		src_len = src->size - srcv->off;
1d442b
+		dst_len = dst->size - dstv->off;
1d442b
+		len = min_size(src_len, dst_len);
1d442b
+
1d442b
+		res = fuse_buf_copy_one(dst, dstv->off, src, srcv->off, len, flags);
1d442b
+		if (res < 0) {
1d442b
+			if (!copied)
1d442b
+				return res;
1d442b
+			break;
1d442b
+		}
1d442b
+		copied += res;
1d442b
+
1d442b
+		if (!fuse_bufvec_advance(srcv, res) ||
1d442b
+		    !fuse_bufvec_advance(dstv, res))
1d442b
+			break;
1d442b
+
1d442b
+		if (res < len)
1d442b
+			break;
1d442b
+	}
1d442b
+
1d442b
+	return copied;
1d442b
+}
1d442b
diff --git a/tools/virtiofsd/fuse_log.c b/tools/virtiofsd/fuse_log.c
1d442b
new file mode 100644
1d442b
index 0000000000..0d268ab014
1d442b
--- /dev/null
1d442b
+++ b/tools/virtiofsd/fuse_log.c
1d442b
@@ -0,0 +1,40 @@
1d442b
+/*
1d442b
+  FUSE: Filesystem in Userspace
1d442b
+  Copyright (C) 2019  Red Hat, Inc.
1d442b
+
1d442b
+  Logging API.
1d442b
+
1d442b
+  This program can be distributed under the terms of the GNU LGPLv2.
1d442b
+  See the file COPYING.LIB
1d442b
+*/
1d442b
+
1d442b
+#include "fuse_log.h"
1d442b
+
1d442b
+#include <stdarg.h>
1d442b
+#include <stdio.h>
1d442b
+
1d442b
+static void default_log_func(
1d442b
+		__attribute__(( unused )) enum fuse_log_level level,
1d442b
+		const char *fmt, va_list ap)
1d442b
+{
1d442b
+	vfprintf(stderr, fmt, ap);
1d442b
+}
1d442b
+
1d442b
+static fuse_log_func_t log_func = default_log_func;
1d442b
+
1d442b
+void fuse_set_log_func(fuse_log_func_t func)
1d442b
+{
1d442b
+	if (!func)
1d442b
+		func = default_log_func;
1d442b
+
1d442b
+	log_func = func;
1d442b
+}
1d442b
+
1d442b
+void fuse_log(enum fuse_log_level level, const char *fmt, ...)
1d442b
+{
1d442b
+	va_list ap;
1d442b
+
1d442b
+	va_start(ap, fmt);
1d442b
+	log_func(level, fmt, ap);
1d442b
+	va_end(ap);
1d442b
+}
1d442b
diff --git a/tools/virtiofsd/fuse_opt.c b/tools/virtiofsd/fuse_opt.c
1d442b
new file mode 100644
1d442b
index 0000000000..93066b926e
1d442b
--- /dev/null
1d442b
+++ b/tools/virtiofsd/fuse_opt.c
1d442b
@@ -0,0 +1,423 @@
1d442b
+/*
1d442b
+  FUSE: Filesystem in Userspace
1d442b
+  Copyright (C) 2001-2007  Miklos Szeredi <miklos@szeredi.hu>
1d442b
+
1d442b
+  Implementation of option parsing routines (dealing with `struct
1d442b
+  fuse_args`).
1d442b
+
1d442b
+  This program can be distributed under the terms of the GNU LGPLv2.
1d442b
+  See the file COPYING.LIB
1d442b
+*/
1d442b
+
1d442b
+#include "config.h"
1d442b
+#include "fuse_i.h"
1d442b
+#include "fuse_opt.h"
1d442b
+#include "fuse_misc.h"
1d442b
+
1d442b
+#include <stdio.h>
1d442b
+#include <stdlib.h>
1d442b
+#include <string.h>
1d442b
+#include <assert.h>
1d442b
+
1d442b
+struct fuse_opt_context {
1d442b
+	void *data;
1d442b
+	const struct fuse_opt *opt;
1d442b
+	fuse_opt_proc_t proc;
1d442b
+	int argctr;
1d442b
+	int argc;
1d442b
+	char **argv;
1d442b
+	struct fuse_args outargs;
1d442b
+	char *opts;
1d442b
+	int nonopt;
1d442b
+};
1d442b
+
1d442b
+void fuse_opt_free_args(struct fuse_args *args)
1d442b
+{
1d442b
+	if (args) {
1d442b
+		if (args->argv && args->allocated) {
1d442b
+			int i;
1d442b
+			for (i = 0; i < args->argc; i++)
1d442b
+				free(args->argv[i]);
1d442b
+			free(args->argv);
1d442b
+		}
1d442b
+		args->argc = 0;
1d442b
+		args->argv = NULL;
1d442b
+		args->allocated = 0;
1d442b
+	}
1d442b
+}
1d442b
+
1d442b
+static int alloc_failed(void)
1d442b
+{
1d442b
+	fuse_log(FUSE_LOG_ERR, "fuse: memory allocation failed\n");
1d442b
+	return -1;
1d442b
+}
1d442b
+
1d442b
+int fuse_opt_add_arg(struct fuse_args *args, const char *arg)
1d442b
+{
1d442b
+	char **newargv;
1d442b
+	char *newarg;
1d442b
+
1d442b
+	assert(!args->argv || args->allocated);
1d442b
+
1d442b
+	newarg = strdup(arg);
1d442b
+	if (!newarg)
1d442b
+		return alloc_failed();
1d442b
+
1d442b
+	newargv = realloc(args->argv, (args->argc + 2) * sizeof(char *));
1d442b
+	if (!newargv) {
1d442b
+		free(newarg);
1d442b
+		return alloc_failed();
1d442b
+	}
1d442b
+
1d442b
+	args->argv = newargv;
1d442b
+	args->allocated = 1;
1d442b
+	args->argv[args->argc++] = newarg;
1d442b
+	args->argv[args->argc] = NULL;
1d442b
+	return 0;
1d442b
+}
1d442b
+
1d442b
+static int fuse_opt_insert_arg_common(struct fuse_args *args, int pos,
1d442b
+				      const char *arg)
1d442b
+{
1d442b
+	assert(pos <= args->argc);
1d442b
+	if (fuse_opt_add_arg(args, arg) == -1)
1d442b
+		return -1;
1d442b
+
1d442b
+	if (pos != args->argc - 1) {
1d442b
+		char *newarg = args->argv[args->argc - 1];
1d442b
+		memmove(&args->argv[pos + 1], &args->argv[pos],
1d442b
+			sizeof(char *) * (args->argc - pos - 1));
1d442b
+		args->argv[pos] = newarg;
1d442b
+	}
1d442b
+	return 0;
1d442b
+}
1d442b
+
1d442b
+int fuse_opt_insert_arg(struct fuse_args *args, int pos, const char *arg)
1d442b
+{
1d442b
+	return fuse_opt_insert_arg_common(args, pos, arg);
1d442b
+}
1d442b
+
1d442b
+static int next_arg(struct fuse_opt_context *ctx, const char *opt)
1d442b
+{
1d442b
+	if (ctx->argctr + 1 >= ctx->argc) {
1d442b
+		fuse_log(FUSE_LOG_ERR, "fuse: missing argument after `%s'\n", opt);
1d442b
+		return -1;
1d442b
+	}
1d442b
+	ctx->argctr++;
1d442b
+	return 0;
1d442b
+}
1d442b
+
1d442b
+static int add_arg(struct fuse_opt_context *ctx, const char *arg)
1d442b
+{
1d442b
+	return fuse_opt_add_arg(&ctx->outargs, arg);
1d442b
+}
1d442b
+
1d442b
+static int add_opt_common(char **opts, const char *opt, int esc)
1d442b
+{
1d442b
+	unsigned oldlen = *opts ? strlen(*opts) : 0;
1d442b
+	char *d = realloc(*opts, oldlen + 1 + strlen(opt) * 2 + 1);
1d442b
+
1d442b
+	if (!d)
1d442b
+		return alloc_failed();
1d442b
+
1d442b
+	*opts = d;
1d442b
+	if (oldlen) {
1d442b
+		d += oldlen;
1d442b
+		*d++ = ',';
1d442b
+	}
1d442b
+
1d442b
+	for (; *opt; opt++) {
1d442b
+		if (esc && (*opt == ',' || *opt == '\\'))
1d442b
+			*d++ = '\\';
1d442b
+		*d++ = *opt;
1d442b
+	}
1d442b
+	*d = '\0';
1d442b
+
1d442b
+	return 0;
1d442b
+}
1d442b
+
1d442b
+int fuse_opt_add_opt(char **opts, const char *opt)
1d442b
+{
1d442b
+	return add_opt_common(opts, opt, 0);
1d442b
+}
1d442b
+
1d442b
+int fuse_opt_add_opt_escaped(char **opts, const char *opt)
1d442b
+{
1d442b
+	return add_opt_common(opts, opt, 1);
1d442b
+}
1d442b
+
1d442b
+static int add_opt(struct fuse_opt_context *ctx, const char *opt)
1d442b
+{
1d442b
+	return add_opt_common(&ctx->opts, opt, 1);
1d442b
+}
1d442b
+
1d442b
+static int call_proc(struct fuse_opt_context *ctx, const char *arg, int key,
1d442b
+		     int iso)
1d442b
+{
1d442b
+	if (key == FUSE_OPT_KEY_DISCARD)
1d442b
+		return 0;
1d442b
+
1d442b
+	if (key != FUSE_OPT_KEY_KEEP && ctx->proc) {
1d442b
+		int res = ctx->proc(ctx->data, arg, key, &ctx->outargs);
1d442b
+		if (res == -1 || !res)
1d442b
+			return res;
1d442b
+	}
1d442b
+	if (iso)
1d442b
+		return add_opt(ctx, arg);
1d442b
+	else
1d442b
+		return add_arg(ctx, arg);
1d442b
+}
1d442b
+
1d442b
+static int match_template(const char *t, const char *arg, unsigned *sepp)
1d442b
+{
1d442b
+	int arglen = strlen(arg);
1d442b
+	const char *sep = strchr(t, '=');
1d442b
+	sep = sep ? sep : strchr(t, ' ');
1d442b
+	if (sep && (!sep[1] || sep[1] == '%')) {
1d442b
+		int tlen = sep - t;
1d442b
+		if (sep[0] == '=')
1d442b
+			tlen ++;
1d442b
+		if (arglen >= tlen && strncmp(arg, t, tlen) == 0) {
1d442b
+			*sepp = sep - t;
1d442b
+			return 1;
1d442b
+		}
1d442b
+	}
1d442b
+	if (strcmp(t, arg) == 0) {
1d442b
+		*sepp = 0;
1d442b
+		return 1;
1d442b
+	}
1d442b
+	return 0;
1d442b
+}
1d442b
+
1d442b
+static const struct fuse_opt *find_opt(const struct fuse_opt *opt,
1d442b
+				       const char *arg, unsigned *sepp)
1d442b
+{
1d442b
+	for (; opt && opt->templ; opt++)
1d442b
+		if (match_template(opt->templ, arg, sepp))
1d442b
+			return opt;
1d442b
+	return NULL;
1d442b
+}
1d442b
+
1d442b
+int fuse_opt_match(const struct fuse_opt *opts, const char *opt)
1d442b
+{
1d442b
+	unsigned dummy;
1d442b
+	return find_opt(opts, opt, &dummy) ? 1 : 0;
1d442b
+}
1d442b
+
1d442b
+static int process_opt_param(void *var, const char *format, const char *param,
1d442b
+			     const char *arg)
1d442b
+{
1d442b
+	assert(format[0] == '%');
1d442b
+	if (format[1] == 's') {
1d442b
+		char **s = var;
1d442b
+		char *copy = strdup(param);
1d442b
+		if (!copy)
1d442b
+			return alloc_failed();
1d442b
+
1d442b
+		free(*s);
1d442b
+		*s = copy;
1d442b
+	} else {
1d442b
+		if (sscanf(param, format, var) != 1) {
1d442b
+			fuse_log(FUSE_LOG_ERR, "fuse: invalid parameter in option `%s'\n", arg);
1d442b
+			return -1;
1d442b
+		}
1d442b
+	}
1d442b
+	return 0;
1d442b
+}
1d442b
+
1d442b
+static int process_opt(struct fuse_opt_context *ctx,
1d442b
+		       const struct fuse_opt *opt, unsigned sep,
1d442b
+		       const char *arg, int iso)
1d442b
+{
1d442b
+	if (opt->offset == -1U) {
1d442b
+		if (call_proc(ctx, arg, opt->value, iso) == -1)
1d442b
+			return -1;
1d442b
+	} else {
1d442b
+		void *var = (char *)ctx->data + opt->offset;
1d442b
+		if (sep && opt->templ[sep + 1]) {
1d442b
+			const char *param = arg + sep;
1d442b
+			if (opt->templ[sep] == '=')
1d442b
+				param ++;
1d442b
+			if (process_opt_param(var, opt->templ + sep + 1,
1d442b
+					      param, arg) == -1)
1d442b
+				return -1;
1d442b
+		} else
1d442b
+			*(int *)var = opt->value;
1d442b
+	}
1d442b
+	return 0;
1d442b
+}
1d442b
+
1d442b
+static int process_opt_sep_arg(struct fuse_opt_context *ctx,
1d442b
+			       const struct fuse_opt *opt, unsigned sep,
1d442b
+			       const char *arg, int iso)
1d442b
+{
1d442b
+	int res;
1d442b
+	char *newarg;
1d442b
+	char *param;
1d442b
+
1d442b
+	if (next_arg(ctx, arg) == -1)
1d442b
+		return -1;
1d442b
+
1d442b
+	param = ctx->argv[ctx->argctr];
1d442b
+	newarg = malloc(sep + strlen(param) + 1);
1d442b
+	if (!newarg)
1d442b
+		return alloc_failed();
1d442b
+
1d442b
+	memcpy(newarg, arg, sep);
1d442b
+	strcpy(newarg + sep, param);
1d442b
+	res = process_opt(ctx, opt, sep, newarg, iso);
1d442b
+	free(newarg);
1d442b
+
1d442b
+	return res;
1d442b
+}
1d442b
+
1d442b
+static int process_gopt(struct fuse_opt_context *ctx, const char *arg, int iso)
1d442b
+{
1d442b
+	unsigned sep;
1d442b
+	const struct fuse_opt *opt = find_opt(ctx->opt, arg, &sep;;
1d442b
+	if (opt) {
1d442b
+		for (; opt; opt = find_opt(opt + 1, arg, &sep)) {
1d442b
+			int res;
1d442b
+			if (sep && opt->templ[sep] == ' ' && !arg[sep])
1d442b
+				res = process_opt_sep_arg(ctx, opt, sep, arg,
1d442b
+							  iso);
1d442b
+			else
1d442b
+				res = process_opt(ctx, opt, sep, arg, iso);
1d442b
+			if (res == -1)
1d442b
+				return -1;
1d442b
+		}
1d442b
+		return 0;
1d442b
+	} else
1d442b
+		return call_proc(ctx, arg, FUSE_OPT_KEY_OPT, iso);
1d442b
+}
1d442b
+
1d442b
+static int process_real_option_group(struct fuse_opt_context *ctx, char *opts)
1d442b
+{
1d442b
+	char *s = opts;
1d442b
+	char *d = s;
1d442b
+	int end = 0;
1d442b
+
1d442b
+	while (!end) {
1d442b
+		if (*s == '\0')
1d442b
+			end = 1;
1d442b
+		if (*s == ',' || end) {
1d442b
+			int res;
1d442b
+
1d442b
+			*d = '\0';
1d442b
+			res = process_gopt(ctx, opts, 1);
1d442b
+			if (res == -1)
1d442b
+				return -1;
1d442b
+			d = opts;
1d442b
+		} else {
1d442b
+			if (s[0] == '\\' && s[1] != '\0') {
1d442b
+				s++;
1d442b
+				if (s[0] >= '0' && s[0] <= '3' &&
1d442b
+				    s[1] >= '0' && s[1] <= '7' &&
1d442b
+				    s[2] >= '0' && s[2] <= '7') {
1d442b
+					*d++ = (s[0] - '0') * 0100 +
1d442b
+						(s[1] - '0') * 0010 +
1d442b
+						(s[2] - '0');
1d442b
+					s += 2;
1d442b
+				} else {
1d442b
+					*d++ = *s;
1d442b
+				}
1d442b
+			} else {
1d442b
+				*d++ = *s;
1d442b
+			}
1d442b
+		}
1d442b
+		s++;
1d442b
+	}
1d442b
+
1d442b
+	return 0;
1d442b
+}
1d442b
+
1d442b
+static int process_option_group(struct fuse_opt_context *ctx, const char *opts)
1d442b
+{
1d442b
+	int res;
1d442b
+	char *copy = strdup(opts);
1d442b
+
1d442b
+	if (!copy) {
1d442b
+		fuse_log(FUSE_LOG_ERR, "fuse: memory allocation failed\n");
1d442b
+		return -1;
1d442b
+	}
1d442b
+	res = process_real_option_group(ctx, copy);
1d442b
+	free(copy);
1d442b
+	return res;
1d442b
+}
1d442b
+
1d442b
+static int process_one(struct fuse_opt_context *ctx, const char *arg)
1d442b
+{
1d442b
+	if (ctx->nonopt || arg[0] != '-')
1d442b
+		return call_proc(ctx, arg, FUSE_OPT_KEY_NONOPT, 0);
1d442b
+	else if (arg[1] == 'o') {
1d442b
+		if (arg[2])
1d442b
+			return process_option_group(ctx, arg + 2);
1d442b
+		else {
1d442b
+			if (next_arg(ctx, arg) == -1)
1d442b
+				return -1;
1d442b
+
1d442b
+			return process_option_group(ctx,
1d442b
+						    ctx->argv[ctx->argctr]);
1d442b
+		}
1d442b
+	} else if (arg[1] == '-' && !arg[2]) {
1d442b
+		if (add_arg(ctx, arg) == -1)
1d442b
+			return -1;
1d442b
+		ctx->nonopt = ctx->outargs.argc;
1d442b
+		return 0;
1d442b
+	} else
1d442b
+		return process_gopt(ctx, arg, 0);
1d442b
+}
1d442b
+
1d442b
+static int opt_parse(struct fuse_opt_context *ctx)
1d442b
+{
1d442b
+	if (ctx->argc) {
1d442b
+		if (add_arg(ctx, ctx->argv[0]) == -1)
1d442b
+			return -1;
1d442b
+	}
1d442b
+
1d442b
+	for (ctx->argctr = 1; ctx->argctr < ctx->argc; ctx->argctr++)
1d442b
+		if (process_one(ctx, ctx->argv[ctx->argctr]) == -1)
1d442b
+			return -1;
1d442b
+
1d442b
+	if (ctx->opts) {
1d442b
+		if (fuse_opt_insert_arg(&ctx->outargs, 1, "-o") == -1 ||
1d442b
+		    fuse_opt_insert_arg(&ctx->outargs, 2, ctx->opts) == -1)
1d442b
+			return -1;
1d442b
+	}
1d442b
+
1d442b
+	/* If option separator ("--") is the last argument, remove it */
1d442b
+	if (ctx->nonopt && ctx->nonopt == ctx->outargs.argc &&
1d442b
+	    strcmp(ctx->outargs.argv[ctx->outargs.argc - 1], "--") == 0) {
1d442b
+		free(ctx->outargs.argv[ctx->outargs.argc - 1]);
1d442b
+		ctx->outargs.argv[--ctx->outargs.argc] = NULL;
1d442b
+	}
1d442b
+
1d442b
+	return 0;
1d442b
+}
1d442b
+
1d442b
+int fuse_opt_parse(struct fuse_args *args, void *data,
1d442b
+		   const struct fuse_opt opts[], fuse_opt_proc_t proc)
1d442b
+{
1d442b
+	int res;
1d442b
+	struct fuse_opt_context ctx = {
1d442b
+		.data = data,
1d442b
+		.opt = opts,
1d442b
+		.proc = proc,
1d442b
+	};
1d442b
+
1d442b
+	if (!args || !args->argv || !args->argc)
1d442b
+		return 0;
1d442b
+
1d442b
+	ctx.argc = args->argc;
1d442b
+	ctx.argv = args->argv;
1d442b
+
1d442b
+	res = opt_parse(&ctx;;
1d442b
+	if (res != -1) {
1d442b
+		struct fuse_args tmp = *args;
1d442b
+		*args = ctx.outargs;
1d442b
+		ctx.outargs = tmp;
1d442b
+	}
1d442b
+	free(ctx.opts);
1d442b
+	fuse_opt_free_args(&ctx.outargs);
1d442b
+	return res;
1d442b
+}
1d442b
diff --git a/tools/virtiofsd/fuse_signals.c b/tools/virtiofsd/fuse_signals.c
1d442b
new file mode 100644
1d442b
index 0000000000..4271947bd4
1d442b
--- /dev/null
1d442b
+++ b/tools/virtiofsd/fuse_signals.c
1d442b
@@ -0,0 +1,91 @@
1d442b
+/*
1d442b
+  FUSE: Filesystem in Userspace
1d442b
+  Copyright (C) 2001-2007  Miklos Szeredi <miklos@szeredi.hu>
1d442b
+
1d442b
+  Utility functions for setting signal handlers.
1d442b
+
1d442b
+  This program can be distributed under the terms of the GNU LGPLv2.
1d442b
+  See the file COPYING.LIB
1d442b
+*/
1d442b
+
1d442b
+#include "config.h"
1d442b
+#include "fuse_lowlevel.h"
1d442b
+#include "fuse_i.h"
1d442b
+
1d442b
+#include <stdio.h>
1d442b
+#include <string.h>
1d442b
+#include <signal.h>
1d442b
+#include <stdlib.h>
1d442b
+
1d442b
+static struct fuse_session *fuse_instance;
1d442b
+
1d442b
+static void exit_handler(int sig)
1d442b
+{
1d442b
+	if (fuse_instance) {
1d442b
+		fuse_session_exit(fuse_instance);
1d442b
+		if(sig <= 0) {
1d442b
+			fuse_log(FUSE_LOG_ERR, "assertion error: signal value <= 0\n");
1d442b
+			abort();
1d442b
+		}
1d442b
+		fuse_instance->error = sig;
1d442b
+	}
1d442b
+}
1d442b
+
1d442b
+static void do_nothing(int sig)
1d442b
+{
1d442b
+	(void) sig;
1d442b
+}
1d442b
+
1d442b
+static int set_one_signal_handler(int sig, void (*handler)(int), int remove)
1d442b
+{
1d442b
+	struct sigaction sa;
1d442b
+	struct sigaction old_sa;
1d442b
+
1d442b
+	memset(&sa, 0, sizeof(struct sigaction));
1d442b
+	sa.sa_handler = remove ? SIG_DFL : handler;
1d442b
+	sigemptyset(&(sa.sa_mask));
1d442b
+	sa.sa_flags = 0;
1d442b
+
1d442b
+	if (sigaction(sig, NULL, &old_sa) == -1) {
1d442b
+		perror("fuse: cannot get old signal handler");
1d442b
+		return -1;
1d442b
+	}
1d442b
+
1d442b
+	if (old_sa.sa_handler == (remove ? handler : SIG_DFL) &&
1d442b
+	    sigaction(sig, &sa, NULL) == -1) {
1d442b
+		perror("fuse: cannot set signal handler");
1d442b
+		return -1;
1d442b
+	}
1d442b
+	return 0;
1d442b
+}
1d442b
+
1d442b
+int fuse_set_signal_handlers(struct fuse_session *se)
1d442b
+{
1d442b
+	/* If we used SIG_IGN instead of the do_nothing function,
1d442b
+	   then we would be unable to tell if we set SIG_IGN (and
1d442b
+	   thus should reset to SIG_DFL in fuse_remove_signal_handlers)
1d442b
+	   or if it was already set to SIG_IGN (and should be left
1d442b
+	   untouched. */
1d442b
+	if (set_one_signal_handler(SIGHUP, exit_handler, 0) == -1 ||
1d442b
+	    set_one_signal_handler(SIGINT, exit_handler, 0) == -1 ||
1d442b
+	    set_one_signal_handler(SIGTERM, exit_handler, 0) == -1 ||
1d442b
+	    set_one_signal_handler(SIGPIPE, do_nothing, 0) == -1)
1d442b
+		return -1;
1d442b
+
1d442b
+	fuse_instance = se;
1d442b
+	return 0;
1d442b
+}
1d442b
+
1d442b
+void fuse_remove_signal_handlers(struct fuse_session *se)
1d442b
+{
1d442b
+	if (fuse_instance != se)
1d442b
+		fuse_log(FUSE_LOG_ERR,
1d442b
+			"fuse: fuse_remove_signal_handlers: unknown session\n");
1d442b
+	else
1d442b
+		fuse_instance = NULL;
1d442b
+
1d442b
+	set_one_signal_handler(SIGHUP, exit_handler, 1);
1d442b
+	set_one_signal_handler(SIGINT, exit_handler, 1);
1d442b
+	set_one_signal_handler(SIGTERM, exit_handler, 1);
1d442b
+	set_one_signal_handler(SIGPIPE, do_nothing, 1);
1d442b
+}
1d442b
diff --git a/tools/virtiofsd/helper.c b/tools/virtiofsd/helper.c
1d442b
new file mode 100644
1d442b
index 0000000000..64ff7ad6d5
1d442b
--- /dev/null
1d442b
+++ b/tools/virtiofsd/helper.c
1d442b
@@ -0,0 +1,440 @@
1d442b
+/*
1d442b
+  FUSE: Filesystem in Userspace
1d442b
+  Copyright (C) 2001-2007  Miklos Szeredi <miklos@szeredi.hu>
1d442b
+
1d442b
+  Helper functions to create (simple) standalone programs. With the
1d442b
+  aid of these functions it should be possible to create full FUSE
1d442b
+  file system by implementing nothing but the request handlers.
1d442b
+
1d442b
+  This program can be distributed under the terms of the GNU LGPLv2.
1d442b
+  See the file COPYING.LIB.
1d442b
+*/
1d442b
+
1d442b
+#include "config.h"
1d442b
+#include "fuse_i.h"
1d442b
+#include "fuse_misc.h"
1d442b
+#include "fuse_opt.h"
1d442b
+#include "fuse_lowlevel.h"
1d442b
+#include "mount_util.h"
1d442b
+
1d442b
+#include <stdio.h>
1d442b
+#include <stdlib.h>
1d442b
+#include <stddef.h>
1d442b
+#include <unistd.h>
1d442b
+#include <string.h>
1d442b
+#include <limits.h>
1d442b
+#include <errno.h>
1d442b
+#include <sys/param.h>
1d442b
+
1d442b
+#define FUSE_HELPER_OPT(t, p) \
1d442b
+	{ t, offsetof(struct fuse_cmdline_opts, p), 1 }
1d442b
+
1d442b
+static const struct fuse_opt fuse_helper_opts[] = {
1d442b
+	FUSE_HELPER_OPT("-h",		show_help),
1d442b
+	FUSE_HELPER_OPT("--help",	show_help),
1d442b
+	FUSE_HELPER_OPT("-V",		show_version),
1d442b
+	FUSE_HELPER_OPT("--version",	show_version),
1d442b
+	FUSE_HELPER_OPT("-d",		debug),
1d442b
+	FUSE_HELPER_OPT("debug",	debug),
1d442b
+	FUSE_HELPER_OPT("-d",		foreground),
1d442b
+	FUSE_HELPER_OPT("debug",	foreground),
1d442b
+	FUSE_OPT_KEY("-d",		FUSE_OPT_KEY_KEEP),
1d442b
+	FUSE_OPT_KEY("debug",		FUSE_OPT_KEY_KEEP),
1d442b
+	FUSE_HELPER_OPT("-f",		foreground),
1d442b
+	FUSE_HELPER_OPT("-s",		singlethread),
1d442b
+	FUSE_HELPER_OPT("fsname=",	nodefault_subtype),
1d442b
+	FUSE_OPT_KEY("fsname=",		FUSE_OPT_KEY_KEEP),
1d442b
+#ifndef __FreeBSD__
1d442b
+	FUSE_HELPER_OPT("subtype=",	nodefault_subtype),
1d442b
+	FUSE_OPT_KEY("subtype=",	FUSE_OPT_KEY_KEEP),
1d442b
+#endif
1d442b
+	FUSE_HELPER_OPT("clone_fd",	clone_fd),
1d442b
+	FUSE_HELPER_OPT("max_idle_threads=%u", max_idle_threads),
1d442b
+	FUSE_OPT_END
1d442b
+};
1d442b
+
1d442b
+struct fuse_conn_info_opts {
1d442b
+	int atomic_o_trunc;
1d442b
+	int no_remote_posix_lock;
1d442b
+	int no_remote_flock;
1d442b
+	int splice_write;
1d442b
+	int splice_move;
1d442b
+	int splice_read;
1d442b
+	int no_splice_write;
1d442b
+	int no_splice_move;
1d442b
+	int no_splice_read;
1d442b
+	int auto_inval_data;
1d442b
+	int no_auto_inval_data;
1d442b
+	int no_readdirplus;
1d442b
+	int no_readdirplus_auto;
1d442b
+	int async_dio;
1d442b
+	int no_async_dio;
1d442b
+	int writeback_cache;
1d442b
+	int no_writeback_cache;
1d442b
+	int async_read;
1d442b
+	int sync_read;
1d442b
+	unsigned max_write;
1d442b
+	unsigned max_readahead;
1d442b
+	unsigned max_background;
1d442b
+	unsigned congestion_threshold;
1d442b
+	unsigned time_gran;
1d442b
+	int set_max_write;
1d442b
+	int set_max_readahead;
1d442b
+	int set_max_background;
1d442b
+	int set_congestion_threshold;
1d442b
+	int set_time_gran;
1d442b
+};
1d442b
+
1d442b
+#define CONN_OPTION(t, p, v)					\
1d442b
+	{ t, offsetof(struct fuse_conn_info_opts, p), v }
1d442b
+static const struct fuse_opt conn_info_opt_spec[] = {
1d442b
+	CONN_OPTION("max_write=%u", max_write, 0),
1d442b
+	CONN_OPTION("max_write=", set_max_write, 1),
1d442b
+	CONN_OPTION("max_readahead=%u", max_readahead, 0),
1d442b
+	CONN_OPTION("max_readahead=", set_max_readahead, 1),
1d442b
+	CONN_OPTION("max_background=%u", max_background, 0),
1d442b
+	CONN_OPTION("max_background=", set_max_background, 1),
1d442b
+	CONN_OPTION("congestion_threshold=%u", congestion_threshold, 0),
1d442b
+	CONN_OPTION("congestion_threshold=", set_congestion_threshold, 1),
1d442b
+	CONN_OPTION("sync_read", sync_read, 1),
1d442b
+	CONN_OPTION("async_read", async_read, 1),
1d442b
+	CONN_OPTION("atomic_o_trunc", atomic_o_trunc, 1),
1d442b
+	CONN_OPTION("no_remote_lock", no_remote_posix_lock, 1),
1d442b
+	CONN_OPTION("no_remote_lock", no_remote_flock, 1),
1d442b
+	CONN_OPTION("no_remote_flock", no_remote_flock, 1),
1d442b
+	CONN_OPTION("no_remote_posix_lock", no_remote_posix_lock, 1),
1d442b
+	CONN_OPTION("splice_write", splice_write, 1),
1d442b
+	CONN_OPTION("no_splice_write", no_splice_write, 1),
1d442b
+	CONN_OPTION("splice_move", splice_move, 1),
1d442b
+	CONN_OPTION("no_splice_move", no_splice_move, 1),
1d442b
+	CONN_OPTION("splice_read", splice_read, 1),
1d442b
+	CONN_OPTION("no_splice_read", no_splice_read, 1),
1d442b
+	CONN_OPTION("auto_inval_data", auto_inval_data, 1),
1d442b
+	CONN_OPTION("no_auto_inval_data", no_auto_inval_data, 1),
1d442b
+	CONN_OPTION("readdirplus=no", no_readdirplus, 1),
1d442b
+	CONN_OPTION("readdirplus=yes", no_readdirplus, 0),
1d442b
+	CONN_OPTION("readdirplus=yes", no_readdirplus_auto, 1),
1d442b
+	CONN_OPTION("readdirplus=auto", no_readdirplus, 0),
1d442b
+	CONN_OPTION("readdirplus=auto", no_readdirplus_auto, 0),
1d442b
+	CONN_OPTION("async_dio", async_dio, 1),
1d442b
+	CONN_OPTION("no_async_dio", no_async_dio, 1),
1d442b
+	CONN_OPTION("writeback_cache", writeback_cache, 1),
1d442b
+	CONN_OPTION("no_writeback_cache", no_writeback_cache, 1),
1d442b
+	CONN_OPTION("time_gran=%u", time_gran, 0),
1d442b
+	CONN_OPTION("time_gran=", set_time_gran, 1),
1d442b
+	FUSE_OPT_END
1d442b
+};
1d442b
+
1d442b
+
1d442b
+void fuse_cmdline_help(void)
1d442b
+{
1d442b
+	printf("    -h   --help            print help\n"
1d442b
+	       "    -V   --version         print version\n"
1d442b
+	       "    -d   -o debug          enable debug output (implies -f)\n"
1d442b
+	       "    -f                     foreground operation\n"
1d442b
+	       "    -s                     disable multi-threaded operation\n"
1d442b
+	       "    -o clone_fd            use separate fuse device fd for each thread\n"
1d442b
+	       "                           (may improve performance)\n"
1d442b
+	       "    -o max_idle_threads    the maximum number of idle worker threads\n"
1d442b
+	       "                           allowed (default: 10)\n");
1d442b
+}
1d442b
+
1d442b
+static int fuse_helper_opt_proc(void *data, const char *arg, int key,
1d442b
+				struct fuse_args *outargs)
1d442b
+{
1d442b
+	(void) outargs;
1d442b
+	struct fuse_cmdline_opts *opts = data;
1d442b
+
1d442b
+	switch (key) {
1d442b
+	case FUSE_OPT_KEY_NONOPT:
1d442b
+		if (!opts->mountpoint) {
1d442b
+			if (fuse_mnt_parse_fuse_fd(arg) != -1) {
1d442b
+				return fuse_opt_add_opt(&opts->mountpoint, arg);
1d442b
+			}
1d442b
+
1d442b
+			char mountpoint[PATH_MAX] = "";
1d442b
+			if (realpath(arg, mountpoint) == NULL) {
1d442b
+				fuse_log(FUSE_LOG_ERR,
1d442b
+					"fuse: bad mount point `%s': %s\n",
1d442b
+					arg, strerror(errno));
1d442b
+				return -1;
1d442b
+			}
1d442b
+			return fuse_opt_add_opt(&opts->mountpoint, mountpoint);
1d442b
+		} else {
1d442b
+			fuse_log(FUSE_LOG_ERR, "fuse: invalid argument `%s'\n", arg);
1d442b
+			return -1;
1d442b
+		}
1d442b
+
1d442b
+	default:
1d442b
+		/* Pass through unknown options */
1d442b
+		return 1;
1d442b
+	}
1d442b
+}
1d442b
+
1d442b
+/* Under FreeBSD, there is no subtype option so this
1d442b
+   function actually sets the fsname */
1d442b
+static int add_default_subtype(const char *progname, struct fuse_args *args)
1d442b
+{
1d442b
+	int res;
1d442b
+	char *subtype_opt;
1d442b
+
1d442b
+	const char *basename = strrchr(progname, '/');
1d442b
+	if (basename == NULL)
1d442b
+		basename = progname;
1d442b
+	else if (basename[1] != '\0')
1d442b
+		basename++;
1d442b
+
1d442b
+	subtype_opt = (char *) malloc(strlen(basename) + 64);
1d442b
+	if (subtype_opt == NULL) {
1d442b
+		fuse_log(FUSE_LOG_ERR, "fuse: memory allocation failed\n");
1d442b
+		return -1;
1d442b
+	}
1d442b
+#ifdef __FreeBSD__
1d442b
+	sprintf(subtype_opt, "-ofsname=%s", basename);
1d442b
+#else
1d442b
+	sprintf(subtype_opt, "-osubtype=%s", basename);
1d442b
+#endif
1d442b
+	res = fuse_opt_add_arg(args, subtype_opt);
1d442b
+	free(subtype_opt);
1d442b
+	return res;
1d442b
+}
1d442b
+
1d442b
+int fuse_parse_cmdline(struct fuse_args *args,
1d442b
+		       struct fuse_cmdline_opts *opts)
1d442b
+{
1d442b
+	memset(opts, 0, sizeof(struct fuse_cmdline_opts));
1d442b
+
1d442b
+	opts->max_idle_threads = 10;
1d442b
+
1d442b
+	if (fuse_opt_parse(args, opts, fuse_helper_opts,
1d442b
+			   fuse_helper_opt_proc) == -1)
1d442b
+		return -1;
1d442b
+
1d442b
+	/* *Linux*: if neither -o subtype nor -o fsname are specified,
1d442b
+	   set subtype to program's basename.
1d442b
+	   *FreeBSD*: if fsname is not specified, set to program's
1d442b
+	   basename. */
1d442b
+	if (!opts->nodefault_subtype)
1d442b
+		if (add_default_subtype(args->argv[0], args) == -1)
1d442b
+			return -1;
1d442b
+
1d442b
+	return 0;
1d442b
+}
1d442b
+
1d442b
+
1d442b
+int fuse_daemonize(int foreground)
1d442b
+{
1d442b
+	if (!foreground) {
1d442b
+		int nullfd;
1d442b
+		int waiter[2];
1d442b
+		char completed;
1d442b
+
1d442b
+		if (pipe(waiter)) {
1d442b
+			perror("fuse_daemonize: pipe");
1d442b
+			return -1;
1d442b
+		}
1d442b
+
1d442b
+		/*
1d442b
+		 * demonize current process by forking it and killing the
1d442b
+		 * parent.  This makes current process as a child of 'init'.
1d442b
+		 */
1d442b
+		switch(fork()) {
1d442b
+		case -1:
1d442b
+			perror("fuse_daemonize: fork");
1d442b
+			return -1;
1d442b
+		case 0:
1d442b
+			break;
1d442b
+		default:
1d442b
+			(void) read(waiter[0], &completed, sizeof(completed));
1d442b
+			_exit(0);
1d442b
+		}
1d442b
+
1d442b
+		if (setsid() == -1) {
1d442b
+			perror("fuse_daemonize: setsid");
1d442b
+			return -1;
1d442b
+		}
1d442b
+
1d442b
+		(void) chdir("/");
1d442b
+
1d442b
+		nullfd = open("/dev/null", O_RDWR, 0);
1d442b
+		if (nullfd != -1) {
1d442b
+			(void) dup2(nullfd, 0);
1d442b
+			(void) dup2(nullfd, 1);
1d442b
+			(void) dup2(nullfd, 2);
1d442b
+			if (nullfd > 2)
1d442b
+				close(nullfd);
1d442b
+		}
1d442b
+
1d442b
+		/* Propagate completion of daemon initialization */
1d442b
+		completed = 1;
1d442b
+		(void) write(waiter[1], &completed, sizeof(completed));
1d442b
+		close(waiter[0]);
1d442b
+		close(waiter[1]);
1d442b
+	} else {
1d442b
+		(void) chdir("/");
1d442b
+	}
1d442b
+	return 0;
1d442b
+}
1d442b
+
1d442b
+int fuse_main_real(int argc, char *argv[], const struct fuse_operations *op,
1d442b
+		   size_t op_size, void *user_data)
1d442b
+{
1d442b
+	struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
1d442b
+	struct fuse *fuse;
1d442b
+	struct fuse_cmdline_opts opts;
1d442b
+	int res;
1d442b
+
1d442b
+	if (fuse_parse_cmdline(&args, &opts) != 0)
1d442b
+		return 1;
1d442b
+
1d442b
+	if (opts.show_version) {
1d442b
+		printf("FUSE library version %s\n", PACKAGE_VERSION);
1d442b
+		fuse_lowlevel_version();
1d442b
+		res = 0;
1d442b
+		goto out1;
1d442b
+	}
1d442b
+
1d442b
+	if (opts.show_help) {
1d442b
+		if(args.argv[0][0] != '\0')
1d442b
+			printf("usage: %s [options] <mountpoint>\n\n",
1d442b
+			       args.argv[0]);
1d442b
+		printf("FUSE options:\n");
1d442b
+		fuse_cmdline_help();
1d442b
+		fuse_lib_help(&args);
1d442b
+		res = 0;
1d442b
+		goto out1;
1d442b
+	}
1d442b
+
1d442b
+	if (!opts.show_help &&
1d442b
+	    !opts.mountpoint) {
1d442b
+		fuse_log(FUSE_LOG_ERR, "error: no mountpoint specified\n");
1d442b
+		res = 2;
1d442b
+		goto out1;
1d442b
+	}
1d442b
+
1d442b
+
1d442b
+	fuse = fuse_new_31(&args, op, op_size, user_data);
1d442b
+	if (fuse == NULL) {
1d442b
+		res = 3;
1d442b
+		goto out1;
1d442b
+	}
1d442b
+
1d442b
+	if (fuse_mount(fuse,opts.mountpoint) != 0) {
1d442b
+		res = 4;
1d442b
+		goto out2;
1d442b
+	}
1d442b
+
1d442b
+	if (fuse_daemonize(opts.foreground) != 0) {
1d442b
+		res = 5;
1d442b
+		goto out3;
1d442b
+	}
1d442b
+
1d442b
+	struct fuse_session *se = fuse_get_session(fuse);
1d442b
+	if (fuse_set_signal_handlers(se) != 0) {
1d442b
+		res = 6;
1d442b
+		goto out3;
1d442b
+	}
1d442b
+
1d442b
+	if (opts.singlethread)
1d442b
+		res = fuse_loop(fuse);
1d442b
+	else {
1d442b
+		struct fuse_loop_config loop_config;
1d442b
+		loop_config.clone_fd = opts.clone_fd;
1d442b
+		loop_config.max_idle_threads = opts.max_idle_threads;
1d442b
+		res = fuse_loop_mt_32(fuse, &loop_config);
1d442b
+	}
1d442b
+	if (res)
1d442b
+		res = 7;
1d442b
+
1d442b
+	fuse_remove_signal_handlers(se);
1d442b
+out3:
1d442b
+	fuse_unmount(fuse);
1d442b
+out2:
1d442b
+	fuse_destroy(fuse);
1d442b
+out1:
1d442b
+	free(opts.mountpoint);
1d442b
+	fuse_opt_free_args(&args);
1d442b
+	return res;
1d442b
+}
1d442b
+
1d442b
+
1d442b
+void fuse_apply_conn_info_opts(struct fuse_conn_info_opts *opts,
1d442b
+			       struct fuse_conn_info *conn)
1d442b
+{
1d442b
+	if(opts->set_max_write)
1d442b
+		conn->max_write = opts->max_write;
1d442b
+	if(opts->set_max_background)
1d442b
+		conn->max_background = opts->max_background;
1d442b
+	if(opts->set_congestion_threshold)
1d442b
+		conn->congestion_threshold = opts->congestion_threshold;
1d442b
+	if(opts->set_time_gran)
1d442b
+		conn->time_gran = opts->time_gran;
1d442b
+	if(opts->set_max_readahead)
1d442b
+		conn->max_readahead = opts->max_readahead;
1d442b
+
1d442b
+#define LL_ENABLE(cond,cap) \
1d442b
+	if (cond) conn->want |= (cap)
1d442b
+#define LL_DISABLE(cond,cap) \
1d442b
+	if (cond) conn->want &= ~(cap)
1d442b
+
1d442b
+	LL_ENABLE(opts->splice_read, FUSE_CAP_SPLICE_READ);
1d442b
+	LL_DISABLE(opts->no_splice_read, FUSE_CAP_SPLICE_READ);
1d442b
+
1d442b
+	LL_ENABLE(opts->splice_write, FUSE_CAP_SPLICE_WRITE);
1d442b
+	LL_DISABLE(opts->no_splice_write, FUSE_CAP_SPLICE_WRITE);
1d442b
+
1d442b
+	LL_ENABLE(opts->splice_move, FUSE_CAP_SPLICE_MOVE);
1d442b
+	LL_DISABLE(opts->no_splice_move, FUSE_CAP_SPLICE_MOVE);
1d442b
+
1d442b
+	LL_ENABLE(opts->auto_inval_data, FUSE_CAP_AUTO_INVAL_DATA);
1d442b
+	LL_DISABLE(opts->no_auto_inval_data, FUSE_CAP_AUTO_INVAL_DATA);
1d442b
+
1d442b
+	LL_DISABLE(opts->no_readdirplus, FUSE_CAP_READDIRPLUS);
1d442b
+	LL_DISABLE(opts->no_readdirplus_auto, FUSE_CAP_READDIRPLUS_AUTO);
1d442b
+
1d442b
+	LL_ENABLE(opts->async_dio, FUSE_CAP_ASYNC_DIO);
1d442b
+	LL_DISABLE(opts->no_async_dio, FUSE_CAP_ASYNC_DIO);
1d442b
+
1d442b
+	LL_ENABLE(opts->writeback_cache, FUSE_CAP_WRITEBACK_CACHE);
1d442b
+	LL_DISABLE(opts->no_writeback_cache, FUSE_CAP_WRITEBACK_CACHE);
1d442b
+
1d442b
+	LL_ENABLE(opts->async_read, FUSE_CAP_ASYNC_READ);
1d442b
+	LL_DISABLE(opts->sync_read, FUSE_CAP_ASYNC_READ);
1d442b
+
1d442b
+	LL_DISABLE(opts->no_remote_posix_lock, FUSE_CAP_POSIX_LOCKS);
1d442b
+	LL_DISABLE(opts->no_remote_flock, FUSE_CAP_FLOCK_LOCKS);
1d442b
+}
1d442b
+
1d442b
+struct fuse_conn_info_opts* fuse_parse_conn_info_opts(struct fuse_args *args)
1d442b
+{
1d442b
+	struct fuse_conn_info_opts *opts;
1d442b
+
1d442b
+	opts = calloc(1, sizeof(struct fuse_conn_info_opts));
1d442b
+	if(opts == NULL) {
1d442b
+		fuse_log(FUSE_LOG_ERR, "calloc failed\n");
1d442b
+		return NULL;
1d442b
+	}
1d442b
+	if(fuse_opt_parse(args, opts, conn_info_opt_spec, NULL) == -1) {
1d442b
+		free(opts);
1d442b
+		return NULL;
1d442b
+	}
1d442b
+	return opts;
1d442b
+}
1d442b
+
1d442b
+int fuse_open_channel(const char *mountpoint, const char* options)
1d442b
+{
1d442b
+	struct mount_opts *opts = NULL;
1d442b
+	int fd = -1;
1d442b
+	const char *argv[] = { "", "-o", options };
1d442b
+	int argc = sizeof(argv) / sizeof(argv[0]);
1d442b
+	struct fuse_args args = FUSE_ARGS_INIT(argc, (char**) argv);
1d442b
+
1d442b
+	opts = parse_mount_opts(&args);
1d442b
+	if (opts == NULL)
1d442b
+		return -1;
1d442b
+
1d442b
+	fd = fuse_kern_mount(mountpoint, opts);
1d442b
+	destroy_mount_opts(opts);
1d442b
+
1d442b
+	return fd;
1d442b
+}