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