peterdelevoryas / rpms / qemu

Forked from rpms/qemu 2 years ago
Clone

Blame 0013-virtiofsd-Add-fuse_lowlevel.c.patch

1d442b
From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
1d442b
Date: Mon, 27 Jan 2020 19:00:42 +0000
1d442b
Subject: [PATCH] virtiofsd: Add fuse_lowlevel.c
1d442b
MIME-Version: 1.0
1d442b
Content-Type: text/plain; charset=UTF-8
1d442b
Content-Transfer-Encoding: 8bit
1d442b
1d442b
fuse_lowlevel is one of the largest files from the library
1d442b
and does most of the work.  Add it separately to keep the diff
1d442b
sizes small.
1d442b
Again this is 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 2de121f01e37e2fe98a4362f4abf7c0848697f76)
1d442b
---
1d442b
 tools/virtiofsd/fuse_lowlevel.c | 3129 +++++++++++++++++++++++++++++++
1d442b
 1 file changed, 3129 insertions(+)
1d442b
 create mode 100644 tools/virtiofsd/fuse_lowlevel.c
1d442b
1d442b
diff --git a/tools/virtiofsd/fuse_lowlevel.c b/tools/virtiofsd/fuse_lowlevel.c
1d442b
new file mode 100644
1d442b
index 0000000000..f2d7038e34
1d442b
--- /dev/null
1d442b
+++ b/tools/virtiofsd/fuse_lowlevel.c
1d442b
@@ -0,0 +1,3129 @@
1d442b
+/*
1d442b
+  FUSE: Filesystem in Userspace
1d442b
+  Copyright (C) 2001-2007  Miklos Szeredi <miklos@szeredi.hu>
1d442b
+
1d442b
+  Implementation of (most of) the low-level FUSE API. The session loop
1d442b
+  functions are implemented in separate files.
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_kernel.h"
1d442b
+#include "fuse_opt.h"
1d442b
+#include "fuse_misc.h"
1d442b
+#include "mount_util.h"
1d442b
+
1d442b
+#include <stdio.h>
1d442b
+#include <stdlib.h>
1d442b
+#include <stddef.h>
1d442b
+#include <string.h>
1d442b
+#include <unistd.h>
1d442b
+#include <limits.h>
1d442b
+#include <errno.h>
1d442b
+#include <assert.h>
1d442b
+#include <sys/file.h>
1d442b
+
1d442b
+#ifndef F_LINUX_SPECIFIC_BASE
1d442b
+#define F_LINUX_SPECIFIC_BASE       1024
1d442b
+#endif
1d442b
+#ifndef F_SETPIPE_SZ
1d442b
+#define F_SETPIPE_SZ	(F_LINUX_SPECIFIC_BASE + 7)
1d442b
+#endif
1d442b
+
1d442b
+
1d442b
+#define PARAM(inarg) (((char *)(inarg)) + sizeof(*(inarg)))
1d442b
+#define OFFSET_MAX 0x7fffffffffffffffLL
1d442b
+
1d442b
+#define container_of(ptr, type, member) ({				\
1d442b
+			const typeof( ((type *)0)->member ) *__mptr = (ptr); \
1d442b
+			(type *)( (char *)__mptr - offsetof(type,member) );})
1d442b
+
1d442b
+struct fuse_pollhandle {
1d442b
+	uint64_t kh;
1d442b
+	struct fuse_session *se;
1d442b
+};
1d442b
+
1d442b
+static size_t pagesize;
1d442b
+
1d442b
+static __attribute__((constructor)) void fuse_ll_init_pagesize(void)
1d442b
+{
1d442b
+	pagesize = getpagesize();
1d442b
+}
1d442b
+
1d442b
+static void convert_stat(const struct stat *stbuf, struct fuse_attr *attr)
1d442b
+{
1d442b
+	attr->ino	= stbuf->st_ino;
1d442b
+	attr->mode	= stbuf->st_mode;
1d442b
+	attr->nlink	= stbuf->st_nlink;
1d442b
+	attr->uid	= stbuf->st_uid;
1d442b
+	attr->gid	= stbuf->st_gid;
1d442b
+	attr->rdev	= stbuf->st_rdev;
1d442b
+	attr->size	= stbuf->st_size;
1d442b
+	attr->blksize	= stbuf->st_blksize;
1d442b
+	attr->blocks	= stbuf->st_blocks;
1d442b
+	attr->atime	= stbuf->st_atime;
1d442b
+	attr->mtime	= stbuf->st_mtime;
1d442b
+	attr->ctime	= stbuf->st_ctime;
1d442b
+	attr->atimensec = ST_ATIM_NSEC(stbuf);
1d442b
+	attr->mtimensec = ST_MTIM_NSEC(stbuf);
1d442b
+	attr->ctimensec = ST_CTIM_NSEC(stbuf);
1d442b
+}
1d442b
+
1d442b
+static void convert_attr(const struct fuse_setattr_in *attr, struct stat *stbuf)
1d442b
+{
1d442b
+	stbuf->st_mode	       = attr->mode;
1d442b
+	stbuf->st_uid	       = attr->uid;
1d442b
+	stbuf->st_gid	       = attr->gid;
1d442b
+	stbuf->st_size	       = attr->size;
1d442b
+	stbuf->st_atime	       = attr->atime;
1d442b
+	stbuf->st_mtime	       = attr->mtime;
1d442b
+	stbuf->st_ctime        = attr->ctime;
1d442b
+	ST_ATIM_NSEC_SET(stbuf, attr->atimensec);
1d442b
+	ST_MTIM_NSEC_SET(stbuf, attr->mtimensec);
1d442b
+	ST_CTIM_NSEC_SET(stbuf, attr->ctimensec);
1d442b
+}
1d442b
+
1d442b
+static	size_t iov_length(const struct iovec *iov, size_t count)
1d442b
+{
1d442b
+	size_t seg;
1d442b
+	size_t ret = 0;
1d442b
+
1d442b
+	for (seg = 0; seg < count; seg++)
1d442b
+		ret += iov[seg].iov_len;
1d442b
+	return ret;
1d442b
+}
1d442b
+
1d442b
+static void list_init_req(struct fuse_req *req)
1d442b
+{
1d442b
+	req->next = req;
1d442b
+	req->prev = req;
1d442b
+}
1d442b
+
1d442b
+static void list_del_req(struct fuse_req *req)
1d442b
+{
1d442b
+	struct fuse_req *prev = req->prev;
1d442b
+	struct fuse_req *next = req->next;
1d442b
+	prev->next = next;
1d442b
+	next->prev = prev;
1d442b
+}
1d442b
+
1d442b
+static void list_add_req(struct fuse_req *req, struct fuse_req *next)
1d442b
+{
1d442b
+	struct fuse_req *prev = next->prev;
1d442b
+	req->next = next;
1d442b
+	req->prev = prev;
1d442b
+	prev->next = req;
1d442b
+	next->prev = req;
1d442b
+}
1d442b
+
1d442b
+static void destroy_req(fuse_req_t req)
1d442b
+{
1d442b
+	pthread_mutex_destroy(&req->lock);
1d442b
+	free(req);
1d442b
+}
1d442b
+
1d442b
+void fuse_free_req(fuse_req_t req)
1d442b
+{
1d442b
+	int ctr;
1d442b
+	struct fuse_session *se = req->se;
1d442b
+
1d442b
+	pthread_mutex_lock(&se->lock);
1d442b
+	req->u.ni.func = NULL;
1d442b
+	req->u.ni.data = NULL;
1d442b
+	list_del_req(req);
1d442b
+	ctr = --req->ctr;
1d442b
+	fuse_chan_put(req->ch);
1d442b
+	req->ch = NULL;
1d442b
+	pthread_mutex_unlock(&se->lock);
1d442b
+	if (!ctr)
1d442b
+		destroy_req(req);
1d442b
+}
1d442b
+
1d442b
+static struct fuse_req *fuse_ll_alloc_req(struct fuse_session *se)
1d442b
+{
1d442b
+	struct fuse_req *req;
1d442b
+
1d442b
+	req = (struct fuse_req *) calloc(1, sizeof(struct fuse_req));
1d442b
+	if (req == NULL) {
1d442b
+		fuse_log(FUSE_LOG_ERR, "fuse: failed to allocate request\n");
1d442b
+	} else {
1d442b
+		req->se = se;
1d442b
+		req->ctr = 1;
1d442b
+		list_init_req(req);
1d442b
+		fuse_mutex_init(&req->lock);
1d442b
+	}
1d442b
+
1d442b
+	return req;
1d442b
+}
1d442b
+
1d442b
+/* Send data. If *ch* is NULL, send via session master fd */
1d442b
+static int fuse_send_msg(struct fuse_session *se, struct fuse_chan *ch,
1d442b
+			 struct iovec *iov, int count)
1d442b
+{
1d442b
+	struct fuse_out_header *out = iov[0].iov_base;
1d442b
+
1d442b
+	out->len = iov_length(iov, count);
1d442b
+	if (se->debug) {
1d442b
+		if (out->unique == 0) {
1d442b
+			fuse_log(FUSE_LOG_DEBUG, "NOTIFY: code=%d length=%u\n",
1d442b
+				out->error, out->len);
1d442b
+		} else if (out->error) {
1d442b
+			fuse_log(FUSE_LOG_DEBUG,
1d442b
+				"   unique: %llu, error: %i (%s), outsize: %i\n",
1d442b
+				(unsigned long long) out->unique, out->error,
1d442b
+				strerror(-out->error), out->len);
1d442b
+		} else {
1d442b
+			fuse_log(FUSE_LOG_DEBUG,
1d442b
+				"   unique: %llu, success, outsize: %i\n",
1d442b
+				(unsigned long long) out->unique, out->len);
1d442b
+		}
1d442b
+	}
1d442b
+
1d442b
+	ssize_t res = writev(ch ? ch->fd : se->fd,
1d442b
+			     iov, count);
1d442b
+	int err = errno;
1d442b
+
1d442b
+	if (res == -1) {
1d442b
+		assert(se != NULL);
1d442b
+
1d442b
+		/* ENOENT means the operation was interrupted */
1d442b
+		if (!fuse_session_exited(se) && err != ENOENT)
1d442b
+			perror("fuse: writing device");
1d442b
+		return -err;
1d442b
+	}
1d442b
+
1d442b
+	return 0;
1d442b
+}
1d442b
+
1d442b
+
1d442b
+int fuse_send_reply_iov_nofree(fuse_req_t req, int error, struct iovec *iov,
1d442b
+			       int count)
1d442b
+{
1d442b
+	struct fuse_out_header out;
1d442b
+
1d442b
+	if (error <= -1000 || error > 0) {
1d442b
+		fuse_log(FUSE_LOG_ERR, "fuse: bad error value: %i\n",	error);
1d442b
+		error = -ERANGE;
1d442b
+	}
1d442b
+
1d442b
+	out.unique = req->unique;
1d442b
+	out.error = error;
1d442b
+
1d442b
+	iov[0].iov_base = &out;
1d442b
+	iov[0].iov_len = sizeof(struct fuse_out_header);
1d442b
+
1d442b
+	return fuse_send_msg(req->se, req->ch, iov, count);
1d442b
+}
1d442b
+
1d442b
+static int send_reply_iov(fuse_req_t req, int error, struct iovec *iov,
1d442b
+			  int count)
1d442b
+{
1d442b
+	int res;
1d442b
+
1d442b
+	res = fuse_send_reply_iov_nofree(req, error, iov, count);
1d442b
+	fuse_free_req(req);
1d442b
+	return res;
1d442b
+}
1d442b
+
1d442b
+static int send_reply(fuse_req_t req, int error, const void *arg,
1d442b
+		      size_t argsize)
1d442b
+{
1d442b
+	struct iovec iov[2];
1d442b
+	int count = 1;
1d442b
+	if (argsize) {
1d442b
+		iov[1].iov_base = (void *) arg;
1d442b
+		iov[1].iov_len = argsize;
1d442b
+		count++;
1d442b
+	}
1d442b
+	return send_reply_iov(req, error, iov, count);
1d442b
+}
1d442b
+
1d442b
+int fuse_reply_iov(fuse_req_t req, const struct iovec *iov, int count)
1d442b
+{
1d442b
+	int res;
1d442b
+	struct iovec *padded_iov;
1d442b
+
1d442b
+	padded_iov = malloc((count + 1) * sizeof(struct iovec));
1d442b
+	if (padded_iov == NULL)
1d442b
+		return fuse_reply_err(req, ENOMEM);
1d442b
+
1d442b
+	memcpy(padded_iov + 1, iov, count * sizeof(struct iovec));
1d442b
+	count++;
1d442b
+
1d442b
+	res = send_reply_iov(req, 0, padded_iov, count);
1d442b
+	free(padded_iov);
1d442b
+
1d442b
+	return res;
1d442b
+}
1d442b
+
1d442b
+
1d442b
+/* `buf` is allowed to be empty so that the proper size may be
1d442b
+   allocated by the caller */
1d442b
+size_t fuse_add_direntry(fuse_req_t req, char *buf, size_t bufsize,
1d442b
+			 const char *name, const struct stat *stbuf, off_t off)
1d442b
+{
1d442b
+	(void)req;
1d442b
+	size_t namelen;
1d442b
+	size_t entlen;
1d442b
+	size_t entlen_padded;
1d442b
+	struct fuse_dirent *dirent;
1d442b
+
1d442b
+	namelen = strlen(name);
1d442b
+	entlen = FUSE_NAME_OFFSET + namelen;
1d442b
+	entlen_padded = FUSE_DIRENT_ALIGN(entlen);
1d442b
+
1d442b
+	if ((buf == NULL) || (entlen_padded > bufsize))
1d442b
+	  return entlen_padded;
1d442b
+
1d442b
+	dirent = (struct fuse_dirent*) buf;
1d442b
+	dirent->ino = stbuf->st_ino;
1d442b
+	dirent->off = off;
1d442b
+	dirent->namelen = namelen;
1d442b
+	dirent->type = (stbuf->st_mode & S_IFMT) >> 12;
1d442b
+	memcpy(dirent->name, name, namelen);
1d442b
+	memset(dirent->name + namelen, 0, entlen_padded - entlen);
1d442b
+
1d442b
+	return entlen_padded;
1d442b
+}
1d442b
+
1d442b
+static void convert_statfs(const struct statvfs *stbuf,
1d442b
+			   struct fuse_kstatfs *kstatfs)
1d442b
+{
1d442b
+	kstatfs->bsize	 = stbuf->f_bsize;
1d442b
+	kstatfs->frsize	 = stbuf->f_frsize;
1d442b
+	kstatfs->blocks	 = stbuf->f_blocks;
1d442b
+	kstatfs->bfree	 = stbuf->f_bfree;
1d442b
+	kstatfs->bavail	 = stbuf->f_bavail;
1d442b
+	kstatfs->files	 = stbuf->f_files;
1d442b
+	kstatfs->ffree	 = stbuf->f_ffree;
1d442b
+	kstatfs->namelen = stbuf->f_namemax;
1d442b
+}
1d442b
+
1d442b
+static int send_reply_ok(fuse_req_t req, const void *arg, size_t argsize)
1d442b
+{
1d442b
+	return send_reply(req, 0, arg, argsize);
1d442b
+}
1d442b
+
1d442b
+int fuse_reply_err(fuse_req_t req, int err)
1d442b
+{
1d442b
+	return send_reply(req, -err, NULL, 0);
1d442b
+}
1d442b
+
1d442b
+void fuse_reply_none(fuse_req_t req)
1d442b
+{
1d442b
+	fuse_free_req(req);
1d442b
+}
1d442b
+
1d442b
+static unsigned long calc_timeout_sec(double t)
1d442b
+{
1d442b
+	if (t > (double) ULONG_MAX)
1d442b
+		return ULONG_MAX;
1d442b
+	else if (t < 0.0)
1d442b
+		return 0;
1d442b
+	else
1d442b
+		return (unsigned long) t;
1d442b
+}
1d442b
+
1d442b
+static unsigned int calc_timeout_nsec(double t)
1d442b
+{
1d442b
+	double f = t - (double) calc_timeout_sec(t);
1d442b
+	if (f < 0.0)
1d442b
+		return 0;
1d442b
+	else if (f >= 0.999999999)
1d442b
+		return 999999999;
1d442b
+	else
1d442b
+		return (unsigned int) (f * 1.0e9);
1d442b
+}
1d442b
+
1d442b
+static void fill_entry(struct fuse_entry_out *arg,
1d442b
+		       const struct fuse_entry_param *e)
1d442b
+{
1d442b
+	arg->nodeid = e->ino;
1d442b
+	arg->generation = e->generation;
1d442b
+	arg->entry_valid = calc_timeout_sec(e->entry_timeout);
1d442b
+	arg->entry_valid_nsec = calc_timeout_nsec(e->entry_timeout);
1d442b
+	arg->attr_valid = calc_timeout_sec(e->attr_timeout);
1d442b
+	arg->attr_valid_nsec = calc_timeout_nsec(e->attr_timeout);
1d442b
+	convert_stat(&e->attr, &arg->attr);
1d442b
+}
1d442b
+
1d442b
+/* `buf` is allowed to be empty so that the proper size may be
1d442b
+   allocated by the caller */
1d442b
+size_t fuse_add_direntry_plus(fuse_req_t req, char *buf, size_t bufsize,
1d442b
+			      const char *name,
1d442b
+			      const struct fuse_entry_param *e, off_t off)
1d442b
+{
1d442b
+	(void)req;
1d442b
+	size_t namelen;
1d442b
+	size_t entlen;
1d442b
+	size_t entlen_padded;
1d442b
+
1d442b
+	namelen = strlen(name);
1d442b
+	entlen = FUSE_NAME_OFFSET_DIRENTPLUS + namelen;
1d442b
+	entlen_padded = FUSE_DIRENT_ALIGN(entlen);
1d442b
+	if ((buf == NULL) || (entlen_padded > bufsize))
1d442b
+	  return entlen_padded;
1d442b
+
1d442b
+	struct fuse_direntplus *dp = (struct fuse_direntplus *) buf;
1d442b
+	memset(&dp->entry_out, 0, sizeof(dp->entry_out));
1d442b
+	fill_entry(&dp->entry_out, e);
1d442b
+
1d442b
+	struct fuse_dirent *dirent = &dp->dirent;
1d442b
+	dirent->ino = e->attr.st_ino;
1d442b
+	dirent->off = off;
1d442b
+	dirent->namelen = namelen;
1d442b
+	dirent->type = (e->attr.st_mode & S_IFMT) >> 12;
1d442b
+	memcpy(dirent->name, name, namelen);
1d442b
+	memset(dirent->name + namelen, 0, entlen_padded - entlen);
1d442b
+
1d442b
+	return entlen_padded;
1d442b
+}
1d442b
+
1d442b
+static void fill_open(struct fuse_open_out *arg,
1d442b
+		      const struct fuse_file_info *f)
1d442b
+{
1d442b
+	arg->fh = f->fh;
1d442b
+	if (f->direct_io)
1d442b
+		arg->open_flags |= FOPEN_DIRECT_IO;
1d442b
+	if (f->keep_cache)
1d442b
+		arg->open_flags |= FOPEN_KEEP_CACHE;
1d442b
+	if (f->cache_readdir)
1d442b
+		arg->open_flags |= FOPEN_CACHE_DIR;
1d442b
+	if (f->nonseekable)
1d442b
+		arg->open_flags |= FOPEN_NONSEEKABLE;
1d442b
+}
1d442b
+
1d442b
+int fuse_reply_entry(fuse_req_t req, const struct fuse_entry_param *e)
1d442b
+{
1d442b
+	struct fuse_entry_out arg;
1d442b
+	size_t size = req->se->conn.proto_minor < 9 ?
1d442b
+		FUSE_COMPAT_ENTRY_OUT_SIZE : sizeof(arg);
1d442b
+
1d442b
+	/* before ABI 7.4 e->ino == 0 was invalid, only ENOENT meant
1d442b
+	   negative entry */
1d442b
+	if (!e->ino && req->se->conn.proto_minor < 4)
1d442b
+		return fuse_reply_err(req, ENOENT);
1d442b
+
1d442b
+	memset(&arg, 0, sizeof(arg));
1d442b
+	fill_entry(&arg, e);
1d442b
+	return send_reply_ok(req, &arg, size);
1d442b
+}
1d442b
+
1d442b
+int fuse_reply_create(fuse_req_t req, const struct fuse_entry_param *e,
1d442b
+		      const struct fuse_file_info *f)
1d442b
+{
1d442b
+	char buf[sizeof(struct fuse_entry_out) + sizeof(struct fuse_open_out)];
1d442b
+	size_t entrysize = req->se->conn.proto_minor < 9 ?
1d442b
+		FUSE_COMPAT_ENTRY_OUT_SIZE : sizeof(struct fuse_entry_out);
1d442b
+	struct fuse_entry_out *earg = (struct fuse_entry_out *) buf;
1d442b
+	struct fuse_open_out *oarg = (struct fuse_open_out *) (buf + entrysize);
1d442b
+
1d442b
+	memset(buf, 0, sizeof(buf));
1d442b
+	fill_entry(earg, e);
1d442b
+	fill_open(oarg, f);
1d442b
+	return send_reply_ok(req, buf,
1d442b
+			     entrysize + sizeof(struct fuse_open_out));
1d442b
+}
1d442b
+
1d442b
+int fuse_reply_attr(fuse_req_t req, const struct stat *attr,
1d442b
+		    double attr_timeout)
1d442b
+{
1d442b
+	struct fuse_attr_out arg;
1d442b
+	size_t size = req->se->conn.proto_minor < 9 ?
1d442b
+		FUSE_COMPAT_ATTR_OUT_SIZE : sizeof(arg);
1d442b
+
1d442b
+	memset(&arg, 0, sizeof(arg));
1d442b
+	arg.attr_valid = calc_timeout_sec(attr_timeout);
1d442b
+	arg.attr_valid_nsec = calc_timeout_nsec(attr_timeout);
1d442b
+	convert_stat(attr, &arg.attr);
1d442b
+
1d442b
+	return send_reply_ok(req, &arg, size);
1d442b
+}
1d442b
+
1d442b
+int fuse_reply_readlink(fuse_req_t req, const char *linkname)
1d442b
+{
1d442b
+	return send_reply_ok(req, linkname, strlen(linkname));
1d442b
+}
1d442b
+
1d442b
+int fuse_reply_open(fuse_req_t req, const struct fuse_file_info *f)
1d442b
+{
1d442b
+	struct fuse_open_out arg;
1d442b
+
1d442b
+	memset(&arg, 0, sizeof(arg));
1d442b
+	fill_open(&arg, f);
1d442b
+	return send_reply_ok(req, &arg, sizeof(arg));
1d442b
+}
1d442b
+
1d442b
+int fuse_reply_write(fuse_req_t req, size_t count)
1d442b
+{
1d442b
+	struct fuse_write_out arg;
1d442b
+
1d442b
+	memset(&arg, 0, sizeof(arg));
1d442b
+	arg.size = count;
1d442b
+
1d442b
+	return send_reply_ok(req, &arg, sizeof(arg));
1d442b
+}
1d442b
+
1d442b
+int fuse_reply_buf(fuse_req_t req, const char *buf, size_t size)
1d442b
+{
1d442b
+	return send_reply_ok(req, buf, size);
1d442b
+}
1d442b
+
1d442b
+static int fuse_send_data_iov_fallback(struct fuse_session *se,
1d442b
+				       struct fuse_chan *ch,
1d442b
+				       struct iovec *iov, int iov_count,
1d442b
+				       struct fuse_bufvec *buf,
1d442b
+				       size_t len)
1d442b
+{
1d442b
+	struct fuse_bufvec mem_buf = FUSE_BUFVEC_INIT(len);
1d442b
+	void *mbuf;
1d442b
+	int res;
1d442b
+
1d442b
+	/* Optimize common case */
1d442b
+	if (buf->count == 1 && buf->idx == 0 && buf->off == 0 &&
1d442b
+	    !(buf->buf[0].flags & FUSE_BUF_IS_FD)) {
1d442b
+		/* FIXME: also avoid memory copy if there are multiple buffers
1d442b
+		   but none of them contain an fd */
1d442b
+
1d442b
+		iov[iov_count].iov_base = buf->buf[0].mem;
1d442b
+		iov[iov_count].iov_len = len;
1d442b
+		iov_count++;
1d442b
+		return fuse_send_msg(se, ch, iov, iov_count);
1d442b
+	}
1d442b
+
1d442b
+	res = posix_memalign(&mbuf, pagesize, len);
1d442b
+	if (res != 0)
1d442b
+		return res;
1d442b
+
1d442b
+	mem_buf.buf[0].mem = mbuf;
1d442b
+	res = fuse_buf_copy(&mem_buf, buf, 0);
1d442b
+	if (res < 0) {
1d442b
+		free(mbuf);
1d442b
+		return -res;
1d442b
+	}
1d442b
+	len = res;
1d442b
+
1d442b
+	iov[iov_count].iov_base = mbuf;
1d442b
+	iov[iov_count].iov_len = len;
1d442b
+	iov_count++;
1d442b
+	res = fuse_send_msg(se, ch, iov, iov_count);
1d442b
+	free(mbuf);
1d442b
+
1d442b
+	return res;
1d442b
+}
1d442b
+
1d442b
+struct fuse_ll_pipe {
1d442b
+	size_t size;
1d442b
+	int can_grow;
1d442b
+	int pipe[2];
1d442b
+};
1d442b
+
1d442b
+static void fuse_ll_pipe_free(struct fuse_ll_pipe *llp)
1d442b
+{
1d442b
+	close(llp->pipe[0]);
1d442b
+	close(llp->pipe[1]);
1d442b
+	free(llp);
1d442b
+}
1d442b
+
1d442b
+#ifdef HAVE_SPLICE
1d442b
+#if !defined(HAVE_PIPE2) || !defined(O_CLOEXEC)
1d442b
+static int fuse_pipe(int fds[2])
1d442b
+{
1d442b
+	int rv = pipe(fds);
1d442b
+
1d442b
+	if (rv == -1)
1d442b
+		return rv;
1d442b
+
1d442b
+	if (fcntl(fds[0], F_SETFL, O_NONBLOCK) == -1 ||
1d442b
+	    fcntl(fds[1], F_SETFL, O_NONBLOCK) == -1 ||
1d442b
+	    fcntl(fds[0], F_SETFD, FD_CLOEXEC) == -1 ||
1d442b
+	    fcntl(fds[1], F_SETFD, FD_CLOEXEC) == -1) {
1d442b
+		close(fds[0]);
1d442b
+		close(fds[1]);
1d442b
+		rv = -1;
1d442b
+	}
1d442b
+	return rv;
1d442b
+}
1d442b
+#else
1d442b
+static int fuse_pipe(int fds[2])
1d442b
+{
1d442b
+	return pipe2(fds, O_CLOEXEC | O_NONBLOCK);
1d442b
+}
1d442b
+#endif
1d442b
+
1d442b
+static struct fuse_ll_pipe *fuse_ll_get_pipe(struct fuse_session *se)
1d442b
+{
1d442b
+	struct fuse_ll_pipe *llp = pthread_getspecific(se->pipe_key);
1d442b
+	if (llp == NULL) {
1d442b
+		int res;
1d442b
+
1d442b
+		llp = malloc(sizeof(struct fuse_ll_pipe));
1d442b
+		if (llp == NULL)
1d442b
+			return NULL;
1d442b
+
1d442b
+		res = fuse_pipe(llp->pipe);
1d442b
+		if (res == -1) {
1d442b
+			free(llp);
1d442b
+			return NULL;
1d442b
+		}
1d442b
+
1d442b
+		/*
1d442b
+		 *the default size is 16 pages on linux
1d442b
+		 */
1d442b
+		llp->size = pagesize * 16;
1d442b
+		llp->can_grow = 1;
1d442b
+
1d442b
+		pthread_setspecific(se->pipe_key, llp);
1d442b
+	}
1d442b
+
1d442b
+	return llp;
1d442b
+}
1d442b
+#endif
1d442b
+
1d442b
+static void fuse_ll_clear_pipe(struct fuse_session *se)
1d442b
+{
1d442b
+	struct fuse_ll_pipe *llp = pthread_getspecific(se->pipe_key);
1d442b
+	if (llp) {
1d442b
+		pthread_setspecific(se->pipe_key, NULL);
1d442b
+		fuse_ll_pipe_free(llp);
1d442b
+	}
1d442b
+}
1d442b
+
1d442b
+#if defined(HAVE_SPLICE) && defined(HAVE_VMSPLICE)
1d442b
+static int read_back(int fd, char *buf, size_t len)
1d442b
+{
1d442b
+	int res;
1d442b
+
1d442b
+	res = read(fd, buf, len);
1d442b
+	if (res == -1) {
1d442b
+		fuse_log(FUSE_LOG_ERR, "fuse: internal error: failed to read back from pipe: %s\n", strerror(errno));
1d442b
+		return -EIO;
1d442b
+	}
1d442b
+	if (res != len) {
1d442b
+		fuse_log(FUSE_LOG_ERR, "fuse: internal error: short read back from pipe: %i from %zi\n", res, len);
1d442b
+		return -EIO;
1d442b
+	}
1d442b
+	return 0;
1d442b
+}
1d442b
+
1d442b
+static int grow_pipe_to_max(int pipefd)
1d442b
+{
1d442b
+	int max;
1d442b
+	int res;
1d442b
+	int maxfd;
1d442b
+	char buf[32];
1d442b
+
1d442b
+	maxfd = open("/proc/sys/fs/pipe-max-size", O_RDONLY);
1d442b
+	if (maxfd < 0)
1d442b
+		return -errno;
1d442b
+
1d442b
+	res = read(maxfd, buf, sizeof(buf) - 1);
1d442b
+	if (res < 0) {
1d442b
+		int saved_errno;
1d442b
+
1d442b
+		saved_errno = errno;
1d442b
+		close(maxfd);
1d442b
+		return -saved_errno;
1d442b
+	}
1d442b
+	close(maxfd);
1d442b
+	buf[res] = '\0';
1d442b
+
1d442b
+	max = atoi(buf);
1d442b
+	res = fcntl(pipefd, F_SETPIPE_SZ, max);
1d442b
+	if (res < 0)
1d442b
+		return -errno;
1d442b
+	return max;
1d442b
+}
1d442b
+
1d442b
+static int fuse_send_data_iov(struct fuse_session *se, struct fuse_chan *ch,
1d442b
+			       struct iovec *iov, int iov_count,
1d442b
+			       struct fuse_bufvec *buf, unsigned int flags)
1d442b
+{
1d442b
+	int res;
1d442b
+	size_t len = fuse_buf_size(buf);
1d442b
+	struct fuse_out_header *out = iov[0].iov_base;
1d442b
+	struct fuse_ll_pipe *llp;
1d442b
+	int splice_flags;
1d442b
+	size_t pipesize;
1d442b
+	size_t total_fd_size;
1d442b
+	size_t idx;
1d442b
+	size_t headerlen;
1d442b
+	struct fuse_bufvec pipe_buf = FUSE_BUFVEC_INIT(len);
1d442b
+
1d442b
+	if (se->broken_splice_nonblock)
1d442b
+		goto fallback;
1d442b
+
1d442b
+	if (flags & FUSE_BUF_NO_SPLICE)
1d442b
+		goto fallback;
1d442b
+
1d442b
+	total_fd_size = 0;
1d442b
+	for (idx = buf->idx; idx < buf->count; idx++) {
1d442b
+		if (buf->buf[idx].flags & FUSE_BUF_IS_FD) {
1d442b
+			total_fd_size = buf->buf[idx].size;
1d442b
+			if (idx == buf->idx)
1d442b
+				total_fd_size -= buf->off;
1d442b
+		}
1d442b
+	}
1d442b
+	if (total_fd_size < 2 * pagesize)
1d442b
+		goto fallback;
1d442b
+
1d442b
+	if (se->conn.proto_minor < 14 ||
1d442b
+	    !(se->conn.want & FUSE_CAP_SPLICE_WRITE))
1d442b
+		goto fallback;
1d442b
+
1d442b
+	llp = fuse_ll_get_pipe(se);
1d442b
+	if (llp == NULL)
1d442b
+		goto fallback;
1d442b
+
1d442b
+
1d442b
+	headerlen = iov_length(iov, iov_count);
1d442b
+
1d442b
+	out->len = headerlen + len;
1d442b
+
1d442b
+	/*
1d442b
+	 * Heuristic for the required pipe size, does not work if the
1d442b
+	 * source contains less than page size fragments
1d442b
+	 */
1d442b
+	pipesize = pagesize * (iov_count + buf->count + 1) + out->len;
1d442b
+
1d442b
+	if (llp->size < pipesize) {
1d442b
+		if (llp->can_grow) {
1d442b
+			res = fcntl(llp->pipe[0], F_SETPIPE_SZ, pipesize);
1d442b
+			if (res == -1) {
1d442b
+				res = grow_pipe_to_max(llp->pipe[0]);
1d442b
+				if (res > 0)
1d442b
+					llp->size = res;
1d442b
+				llp->can_grow = 0;
1d442b
+				goto fallback;
1d442b
+			}
1d442b
+			llp->size = res;
1d442b
+		}
1d442b
+		if (llp->size < pipesize)
1d442b
+			goto fallback;
1d442b
+	}
1d442b
+
1d442b
+
1d442b
+	res = vmsplice(llp->pipe[1], iov, iov_count, SPLICE_F_NONBLOCK);
1d442b
+	if (res == -1)
1d442b
+		goto fallback;
1d442b
+
1d442b
+	if (res != headerlen) {
1d442b
+		res = -EIO;
1d442b
+		fuse_log(FUSE_LOG_ERR, "fuse: short vmsplice to pipe: %u/%zu\n", res,
1d442b
+			headerlen);
1d442b
+		goto clear_pipe;
1d442b
+	}
1d442b
+
1d442b
+	pipe_buf.buf[0].flags = FUSE_BUF_IS_FD;
1d442b
+	pipe_buf.buf[0].fd = llp->pipe[1];
1d442b
+
1d442b
+	res = fuse_buf_copy(&pipe_buf, buf,
1d442b
+			    FUSE_BUF_FORCE_SPLICE | FUSE_BUF_SPLICE_NONBLOCK);
1d442b
+	if (res < 0) {
1d442b
+		if (res == -EAGAIN || res == -EINVAL) {
1d442b
+			/*
1d442b
+			 * Should only get EAGAIN on kernels with
1d442b
+			 * broken SPLICE_F_NONBLOCK support (<=
1d442b
+			 * 2.6.35) where this error or a short read is
1d442b
+			 * returned even if the pipe itself is not
1d442b
+			 * full
1d442b
+			 *
1d442b
+			 * EINVAL might mean that splice can't handle
1d442b
+			 * this combination of input and output.
1d442b
+			 */
1d442b
+			if (res == -EAGAIN)
1d442b
+				se->broken_splice_nonblock = 1;
1d442b
+
1d442b
+			pthread_setspecific(se->pipe_key, NULL);
1d442b
+			fuse_ll_pipe_free(llp);
1d442b
+			goto fallback;
1d442b
+		}
1d442b
+		res = -res;
1d442b
+		goto clear_pipe;
1d442b
+	}
1d442b
+
1d442b
+	if (res != 0 && res < len) {
1d442b
+		struct fuse_bufvec mem_buf = FUSE_BUFVEC_INIT(len);
1d442b
+		void *mbuf;
1d442b
+		size_t now_len = res;
1d442b
+		/*
1d442b
+		 * For regular files a short count is either
1d442b
+		 *  1) due to EOF, or
1d442b
+		 *  2) because of broken SPLICE_F_NONBLOCK (see above)
1d442b
+		 *
1d442b
+		 * For other inputs it's possible that we overflowed
1d442b
+		 * the pipe because of small buffer fragments.
1d442b
+		 */
1d442b
+
1d442b
+		res = posix_memalign(&mbuf, pagesize, len);
1d442b
+		if (res != 0)
1d442b
+			goto clear_pipe;
1d442b
+
1d442b
+		mem_buf.buf[0].mem = mbuf;
1d442b
+		mem_buf.off = now_len;
1d442b
+		res = fuse_buf_copy(&mem_buf, buf, 0);
1d442b
+		if (res > 0) {
1d442b
+			char *tmpbuf;
1d442b
+			size_t extra_len = res;
1d442b
+			/*
1d442b
+			 * Trickiest case: got more data.  Need to get
1d442b
+			 * back the data from the pipe and then fall
1d442b
+			 * back to regular write.
1d442b
+			 */
1d442b
+			tmpbuf = malloc(headerlen);
1d442b
+			if (tmpbuf == NULL) {
1d442b
+				free(mbuf);
1d442b
+				res = ENOMEM;
1d442b
+				goto clear_pipe;
1d442b
+			}
1d442b
+			res = read_back(llp->pipe[0], tmpbuf, headerlen);
1d442b
+			free(tmpbuf);
1d442b
+			if (res != 0) {
1d442b
+				free(mbuf);
1d442b
+				goto clear_pipe;
1d442b
+			}
1d442b
+			res = read_back(llp->pipe[0], mbuf, now_len);
1d442b
+			if (res != 0) {
1d442b
+				free(mbuf);
1d442b
+				goto clear_pipe;
1d442b
+			}
1d442b
+			len = now_len + extra_len;
1d442b
+			iov[iov_count].iov_base = mbuf;
1d442b
+			iov[iov_count].iov_len = len;
1d442b
+			iov_count++;
1d442b
+			res = fuse_send_msg(se, ch, iov, iov_count);
1d442b
+			free(mbuf);
1d442b
+			return res;
1d442b
+		}
1d442b
+		free(mbuf);
1d442b
+		res = now_len;
1d442b
+	}
1d442b
+	len = res;
1d442b
+	out->len = headerlen + len;
1d442b
+
1d442b
+	if (se->debug) {
1d442b
+		fuse_log(FUSE_LOG_DEBUG,
1d442b
+			"   unique: %llu, success, outsize: %i (splice)\n",
1d442b
+			(unsigned long long) out->unique, out->len);
1d442b
+	}
1d442b
+
1d442b
+	splice_flags = 0;
1d442b
+	if ((flags & FUSE_BUF_SPLICE_MOVE) &&
1d442b
+	    (se->conn.want & FUSE_CAP_SPLICE_MOVE))
1d442b
+		splice_flags |= SPLICE_F_MOVE;
1d442b
+
1d442b
+	res = splice(llp->pipe[0], NULL, ch ? ch->fd : se->fd,
1d442b
+		     NULL, out->len, splice_flags);
1d442b
+	if (res == -1) {
1d442b
+		res = -errno;
1d442b
+		perror("fuse: splice from pipe");
1d442b
+		goto clear_pipe;
1d442b
+	}
1d442b
+	if (res != out->len) {
1d442b
+		res = -EIO;
1d442b
+		fuse_log(FUSE_LOG_ERR, "fuse: short splice from pipe: %u/%u\n",
1d442b
+			res, out->len);
1d442b
+		goto clear_pipe;
1d442b
+	}
1d442b
+	return 0;
1d442b
+
1d442b
+clear_pipe:
1d442b
+	fuse_ll_clear_pipe(se);
1d442b
+	return res;
1d442b
+
1d442b
+fallback:
1d442b
+	return fuse_send_data_iov_fallback(se, ch, iov, iov_count, buf, len);
1d442b
+}
1d442b
+#else
1d442b
+static int fuse_send_data_iov(struct fuse_session *se, struct fuse_chan *ch,
1d442b
+			       struct iovec *iov, int iov_count,
1d442b
+			       struct fuse_bufvec *buf, unsigned int flags)
1d442b
+{
1d442b
+	size_t len = fuse_buf_size(buf);
1d442b
+	(void) flags;
1d442b
+
1d442b
+	return fuse_send_data_iov_fallback(se, ch, iov, iov_count, buf, len);
1d442b
+}
1d442b
+#endif
1d442b
+
1d442b
+int fuse_reply_data(fuse_req_t req, struct fuse_bufvec *bufv,
1d442b
+		    enum fuse_buf_copy_flags flags)
1d442b
+{
1d442b
+	struct iovec iov[2];
1d442b
+	struct fuse_out_header out;
1d442b
+	int res;
1d442b
+
1d442b
+	iov[0].iov_base = &out;
1d442b
+	iov[0].iov_len = sizeof(struct fuse_out_header);
1d442b
+
1d442b
+	out.unique = req->unique;
1d442b
+	out.error = 0;
1d442b
+
1d442b
+	res = fuse_send_data_iov(req->se, req->ch, iov, 1, bufv, flags);
1d442b
+	if (res <= 0) {
1d442b
+		fuse_free_req(req);
1d442b
+		return res;
1d442b
+	} else {
1d442b
+		return fuse_reply_err(req, res);
1d442b
+	}
1d442b
+}
1d442b
+
1d442b
+int fuse_reply_statfs(fuse_req_t req, const struct statvfs *stbuf)
1d442b
+{
1d442b
+	struct fuse_statfs_out arg;
1d442b
+	size_t size = req->se->conn.proto_minor < 4 ?
1d442b
+		FUSE_COMPAT_STATFS_SIZE : sizeof(arg);
1d442b
+
1d442b
+	memset(&arg, 0, sizeof(arg));
1d442b
+	convert_statfs(stbuf, &arg.st);
1d442b
+
1d442b
+	return send_reply_ok(req, &arg, size);
1d442b
+}
1d442b
+
1d442b
+int fuse_reply_xattr(fuse_req_t req, size_t count)
1d442b
+{
1d442b
+	struct fuse_getxattr_out arg;
1d442b
+
1d442b
+	memset(&arg, 0, sizeof(arg));
1d442b
+	arg.size = count;
1d442b
+
1d442b
+	return send_reply_ok(req, &arg, sizeof(arg));
1d442b
+}
1d442b
+
1d442b
+int fuse_reply_lock(fuse_req_t req, const struct flock *lock)
1d442b
+{
1d442b
+	struct fuse_lk_out arg;
1d442b
+
1d442b
+	memset(&arg, 0, sizeof(arg));
1d442b
+	arg.lk.type = lock->l_type;
1d442b
+	if (lock->l_type != F_UNLCK) {
1d442b
+		arg.lk.start = lock->l_start;
1d442b
+		if (lock->l_len == 0)
1d442b
+			arg.lk.end = OFFSET_MAX;
1d442b
+		else
1d442b
+			arg.lk.end = lock->l_start + lock->l_len - 1;
1d442b
+	}
1d442b
+	arg.lk.pid = lock->l_pid;
1d442b
+	return send_reply_ok(req, &arg, sizeof(arg));
1d442b
+}
1d442b
+
1d442b
+int fuse_reply_bmap(fuse_req_t req, uint64_t idx)
1d442b
+{
1d442b
+	struct fuse_bmap_out arg;
1d442b
+
1d442b
+	memset(&arg, 0, sizeof(arg));
1d442b
+	arg.block = idx;
1d442b
+
1d442b
+	return send_reply_ok(req, &arg, sizeof(arg));
1d442b
+}
1d442b
+
1d442b
+static struct fuse_ioctl_iovec *fuse_ioctl_iovec_copy(const struct iovec *iov,
1d442b
+						      size_t count)
1d442b
+{
1d442b
+	struct fuse_ioctl_iovec *fiov;
1d442b
+	size_t i;
1d442b
+
1d442b
+	fiov = malloc(sizeof(fiov[0]) * count);
1d442b
+	if (!fiov)
1d442b
+		return NULL;
1d442b
+
1d442b
+	for (i = 0; i < count; i++) {
1d442b
+		fiov[i].base = (uintptr_t) iov[i].iov_base;
1d442b
+		fiov[i].len = iov[i].iov_len;
1d442b
+	}
1d442b
+
1d442b
+	return fiov;
1d442b
+}
1d442b
+
1d442b
+int fuse_reply_ioctl_retry(fuse_req_t req,
1d442b
+			   const struct iovec *in_iov, size_t in_count,
1d442b
+			   const struct iovec *out_iov, size_t out_count)
1d442b
+{
1d442b
+	struct fuse_ioctl_out arg;
1d442b
+	struct fuse_ioctl_iovec *in_fiov = NULL;
1d442b
+	struct fuse_ioctl_iovec *out_fiov = NULL;
1d442b
+	struct iovec iov[4];
1d442b
+	size_t count = 1;
1d442b
+	int res;
1d442b
+
1d442b
+	memset(&arg, 0, sizeof(arg));
1d442b
+	arg.flags |= FUSE_IOCTL_RETRY;
1d442b
+	arg.in_iovs = in_count;
1d442b
+	arg.out_iovs = out_count;
1d442b
+	iov[count].iov_base = &arg;
1d442b
+	iov[count].iov_len = sizeof(arg);
1d442b
+	count++;
1d442b
+
1d442b
+	if (req->se->conn.proto_minor < 16) {
1d442b
+		if (in_count) {
1d442b
+			iov[count].iov_base = (void *)in_iov;
1d442b
+			iov[count].iov_len = sizeof(in_iov[0]) * in_count;
1d442b
+			count++;
1d442b
+		}
1d442b
+
1d442b
+		if (out_count) {
1d442b
+			iov[count].iov_base = (void *)out_iov;
1d442b
+			iov[count].iov_len = sizeof(out_iov[0]) * out_count;
1d442b
+			count++;
1d442b
+		}
1d442b
+	} else {
1d442b
+		/* Can't handle non-compat 64bit ioctls on 32bit */
1d442b
+		if (sizeof(void *) == 4 && req->ioctl_64bit) {
1d442b
+			res = fuse_reply_err(req, EINVAL);
1d442b
+			goto out;
1d442b
+		}
1d442b
+
1d442b
+		if (in_count) {
1d442b
+			in_fiov = fuse_ioctl_iovec_copy(in_iov, in_count);
1d442b
+			if (!in_fiov)
1d442b
+				goto enomem;
1d442b
+
1d442b
+			iov[count].iov_base = (void *)in_fiov;
1d442b
+			iov[count].iov_len = sizeof(in_fiov[0]) * in_count;
1d442b
+			count++;
1d442b
+		}
1d442b
+		if (out_count) {
1d442b
+			out_fiov = fuse_ioctl_iovec_copy(out_iov, out_count);
1d442b
+			if (!out_fiov)
1d442b
+				goto enomem;
1d442b
+
1d442b
+			iov[count].iov_base = (void *)out_fiov;
1d442b
+			iov[count].iov_len = sizeof(out_fiov[0]) * out_count;
1d442b
+			count++;
1d442b
+		}
1d442b
+	}
1d442b
+
1d442b
+	res = send_reply_iov(req, 0, iov, count);
1d442b
+out:
1d442b
+	free(in_fiov);
1d442b
+	free(out_fiov);
1d442b
+
1d442b
+	return res;
1d442b
+
1d442b
+enomem:
1d442b
+	res = fuse_reply_err(req, ENOMEM);
1d442b
+	goto out;
1d442b
+}
1d442b
+
1d442b
+int fuse_reply_ioctl(fuse_req_t req, int result, const void *buf, size_t size)
1d442b
+{
1d442b
+	struct fuse_ioctl_out arg;
1d442b
+	struct iovec iov[3];
1d442b
+	size_t count = 1;
1d442b
+
1d442b
+	memset(&arg, 0, sizeof(arg));
1d442b
+	arg.result = result;
1d442b
+	iov[count].iov_base = &arg;
1d442b
+	iov[count].iov_len = sizeof(arg);
1d442b
+	count++;
1d442b
+
1d442b
+	if (size) {
1d442b
+		iov[count].iov_base = (char *) buf;
1d442b
+		iov[count].iov_len = size;
1d442b
+		count++;
1d442b
+	}
1d442b
+
1d442b
+	return send_reply_iov(req, 0, iov, count);
1d442b
+}
1d442b
+
1d442b
+int fuse_reply_ioctl_iov(fuse_req_t req, int result, const struct iovec *iov,
1d442b
+			 int count)
1d442b
+{
1d442b
+	struct iovec *padded_iov;
1d442b
+	struct fuse_ioctl_out arg;
1d442b
+	int res;
1d442b
+
1d442b
+	padded_iov = malloc((count + 2) * sizeof(struct iovec));
1d442b
+	if (padded_iov == NULL)
1d442b
+		return fuse_reply_err(req, ENOMEM);
1d442b
+
1d442b
+	memset(&arg, 0, sizeof(arg));
1d442b
+	arg.result = result;
1d442b
+	padded_iov[1].iov_base = &arg;
1d442b
+	padded_iov[1].iov_len = sizeof(arg);
1d442b
+
1d442b
+	memcpy(&padded_iov[2], iov, count * sizeof(struct iovec));
1d442b
+
1d442b
+	res = send_reply_iov(req, 0, padded_iov, count + 2);
1d442b
+	free(padded_iov);
1d442b
+
1d442b
+	return res;
1d442b
+}
1d442b
+
1d442b
+int fuse_reply_poll(fuse_req_t req, unsigned revents)
1d442b
+{
1d442b
+	struct fuse_poll_out arg;
1d442b
+
1d442b
+	memset(&arg, 0, sizeof(arg));
1d442b
+	arg.revents = revents;
1d442b
+
1d442b
+	return send_reply_ok(req, &arg, sizeof(arg));
1d442b
+}
1d442b
+
1d442b
+int fuse_reply_lseek(fuse_req_t req, off_t off)
1d442b
+{
1d442b
+	struct fuse_lseek_out arg;
1d442b
+
1d442b
+	memset(&arg, 0, sizeof(arg));
1d442b
+	arg.offset = off;
1d442b
+
1d442b
+	return send_reply_ok(req, &arg, sizeof(arg));
1d442b
+}
1d442b
+
1d442b
+static void do_lookup(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1d442b
+{
1d442b
+	char *name = (char *) inarg;
1d442b
+
1d442b
+	if (req->se->op.lookup)
1d442b
+		req->se->op.lookup(req, nodeid, name);
1d442b
+	else
1d442b
+		fuse_reply_err(req, ENOSYS);
1d442b
+}
1d442b
+
1d442b
+static void do_forget(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1d442b
+{
1d442b
+	struct fuse_forget_in *arg = (struct fuse_forget_in *) inarg;
1d442b
+
1d442b
+	if (req->se->op.forget)
1d442b
+		req->se->op.forget(req, nodeid, arg->nlookup);
1d442b
+	else
1d442b
+		fuse_reply_none(req);
1d442b
+}
1d442b
+
1d442b
+static void do_batch_forget(fuse_req_t req, fuse_ino_t nodeid,
1d442b
+			    const void *inarg)
1d442b
+{
1d442b
+	struct fuse_batch_forget_in *arg = (void *) inarg;
1d442b
+	struct fuse_forget_one *param = (void *) PARAM(arg);
1d442b
+	unsigned int i;
1d442b
+
1d442b
+	(void) nodeid;
1d442b
+
1d442b
+	if (req->se->op.forget_multi) {
1d442b
+		req->se->op.forget_multi(req, arg->count,
1d442b
+				     (struct fuse_forget_data *) param);
1d442b
+	} else if (req->se->op.forget) {
1d442b
+		for (i = 0; i < arg->count; i++) {
1d442b
+			struct fuse_forget_one *forget = &param[i];
1d442b
+			struct fuse_req *dummy_req;
1d442b
+
1d442b
+			dummy_req = fuse_ll_alloc_req(req->se);
1d442b
+			if (dummy_req == NULL)
1d442b
+				break;
1d442b
+
1d442b
+			dummy_req->unique = req->unique;
1d442b
+			dummy_req->ctx = req->ctx;
1d442b
+			dummy_req->ch = NULL;
1d442b
+
1d442b
+			req->se->op.forget(dummy_req, forget->nodeid,
1d442b
+					  forget->nlookup);
1d442b
+		}
1d442b
+		fuse_reply_none(req);
1d442b
+	} else {
1d442b
+		fuse_reply_none(req);
1d442b
+	}
1d442b
+}
1d442b
+
1d442b
+static void do_getattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1d442b
+{
1d442b
+	struct fuse_file_info *fip = NULL;
1d442b
+	struct fuse_file_info fi;
1d442b
+
1d442b
+	if (req->se->conn.proto_minor >= 9) {
1d442b
+		struct fuse_getattr_in *arg = (struct fuse_getattr_in *) inarg;
1d442b
+
1d442b
+		if (arg->getattr_flags & FUSE_GETATTR_FH) {
1d442b
+			memset(&fi, 0, sizeof(fi));
1d442b
+			fi.fh = arg->fh;
1d442b
+			fip = &fi;
1d442b
+		}
1d442b
+	}
1d442b
+
1d442b
+	if (req->se->op.getattr)
1d442b
+		req->se->op.getattr(req, nodeid, fip);
1d442b
+	else
1d442b
+		fuse_reply_err(req, ENOSYS);
1d442b
+}
1d442b
+
1d442b
+static void do_setattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1d442b
+{
1d442b
+	struct fuse_setattr_in *arg = (struct fuse_setattr_in *) inarg;
1d442b
+
1d442b
+	if (req->se->op.setattr) {
1d442b
+		struct fuse_file_info *fi = NULL;
1d442b
+		struct fuse_file_info fi_store;
1d442b
+		struct stat stbuf;
1d442b
+		memset(&stbuf, 0, sizeof(stbuf));
1d442b
+		convert_attr(arg, &stbuf);
1d442b
+		if (arg->valid & FATTR_FH) {
1d442b
+			arg->valid &= ~FATTR_FH;
1d442b
+			memset(&fi_store, 0, sizeof(fi_store));
1d442b
+			fi = &fi_store;
1d442b
+			fi->fh = arg->fh;
1d442b
+		}
1d442b
+		arg->valid &=
1d442b
+			FUSE_SET_ATTR_MODE	|
1d442b
+			FUSE_SET_ATTR_UID	|
1d442b
+			FUSE_SET_ATTR_GID	|
1d442b
+			FUSE_SET_ATTR_SIZE	|
1d442b
+			FUSE_SET_ATTR_ATIME	|
1d442b
+			FUSE_SET_ATTR_MTIME	|
1d442b
+			FUSE_SET_ATTR_ATIME_NOW	|
1d442b
+			FUSE_SET_ATTR_MTIME_NOW |
1d442b
+			FUSE_SET_ATTR_CTIME;
1d442b
+
1d442b
+		req->se->op.setattr(req, nodeid, &stbuf, arg->valid, fi);
1d442b
+	} else
1d442b
+		fuse_reply_err(req, ENOSYS);
1d442b
+}
1d442b
+
1d442b
+static void do_access(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1d442b
+{
1d442b
+	struct fuse_access_in *arg = (struct fuse_access_in *) inarg;
1d442b
+
1d442b
+	if (req->se->op.access)
1d442b
+		req->se->op.access(req, nodeid, arg->mask);
1d442b
+	else
1d442b
+		fuse_reply_err(req, ENOSYS);
1d442b
+}
1d442b
+
1d442b
+static void do_readlink(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1d442b
+{
1d442b
+	(void) inarg;
1d442b
+
1d442b
+	if (req->se->op.readlink)
1d442b
+		req->se->op.readlink(req, nodeid);
1d442b
+	else
1d442b
+		fuse_reply_err(req, ENOSYS);
1d442b
+}
1d442b
+
1d442b
+static void do_mknod(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1d442b
+{
1d442b
+	struct fuse_mknod_in *arg = (struct fuse_mknod_in *) inarg;
1d442b
+	char *name = PARAM(arg);
1d442b
+
1d442b
+	if (req->se->conn.proto_minor >= 12)
1d442b
+		req->ctx.umask = arg->umask;
1d442b
+	else
1d442b
+		name = (char *) inarg + FUSE_COMPAT_MKNOD_IN_SIZE;
1d442b
+
1d442b
+	if (req->se->op.mknod)
1d442b
+		req->se->op.mknod(req, nodeid, name, arg->mode, arg->rdev);
1d442b
+	else
1d442b
+		fuse_reply_err(req, ENOSYS);
1d442b
+}
1d442b
+
1d442b
+static void do_mkdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1d442b
+{
1d442b
+	struct fuse_mkdir_in *arg = (struct fuse_mkdir_in *) inarg;
1d442b
+
1d442b
+	if (req->se->conn.proto_minor >= 12)
1d442b
+		req->ctx.umask = arg->umask;
1d442b
+
1d442b
+	if (req->se->op.mkdir)
1d442b
+		req->se->op.mkdir(req, nodeid, PARAM(arg), arg->mode);
1d442b
+	else
1d442b
+		fuse_reply_err(req, ENOSYS);
1d442b
+}
1d442b
+
1d442b
+static void do_unlink(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1d442b
+{
1d442b
+	char *name = (char *) inarg;
1d442b
+
1d442b
+	if (req->se->op.unlink)
1d442b
+		req->se->op.unlink(req, nodeid, name);
1d442b
+	else
1d442b
+		fuse_reply_err(req, ENOSYS);
1d442b
+}
1d442b
+
1d442b
+static void do_rmdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1d442b
+{
1d442b
+	char *name = (char *) inarg;
1d442b
+
1d442b
+	if (req->se->op.rmdir)
1d442b
+		req->se->op.rmdir(req, nodeid, name);
1d442b
+	else
1d442b
+		fuse_reply_err(req, ENOSYS);
1d442b
+}
1d442b
+
1d442b
+static void do_symlink(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1d442b
+{
1d442b
+	char *name = (char *) inarg;
1d442b
+	char *linkname = ((char *) inarg) + strlen((char *) inarg) + 1;
1d442b
+
1d442b
+	if (req->se->op.symlink)
1d442b
+		req->se->op.symlink(req, linkname, nodeid, name);
1d442b
+	else
1d442b
+		fuse_reply_err(req, ENOSYS);
1d442b
+}
1d442b
+
1d442b
+static void do_rename(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1d442b
+{
1d442b
+	struct fuse_rename_in *arg = (struct fuse_rename_in *) inarg;
1d442b
+	char *oldname = PARAM(arg);
1d442b
+	char *newname = oldname + strlen(oldname) + 1;
1d442b
+
1d442b
+	if (req->se->op.rename)
1d442b
+		req->se->op.rename(req, nodeid, oldname, arg->newdir, newname,
1d442b
+				  0);
1d442b
+	else
1d442b
+		fuse_reply_err(req, ENOSYS);
1d442b
+}
1d442b
+
1d442b
+static void do_rename2(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1d442b
+{
1d442b
+	struct fuse_rename2_in *arg = (struct fuse_rename2_in *) inarg;
1d442b
+	char *oldname = PARAM(arg);
1d442b
+	char *newname = oldname + strlen(oldname) + 1;
1d442b
+
1d442b
+	if (req->se->op.rename)
1d442b
+		req->se->op.rename(req, nodeid, oldname, arg->newdir, newname,
1d442b
+				  arg->flags);
1d442b
+	else
1d442b
+		fuse_reply_err(req, ENOSYS);
1d442b
+}
1d442b
+
1d442b
+static void do_link(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1d442b
+{
1d442b
+	struct fuse_link_in *arg = (struct fuse_link_in *) inarg;
1d442b
+
1d442b
+	if (req->se->op.link)
1d442b
+		req->se->op.link(req, arg->oldnodeid, nodeid, PARAM(arg));
1d442b
+	else
1d442b
+		fuse_reply_err(req, ENOSYS);
1d442b
+}
1d442b
+
1d442b
+static void do_create(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1d442b
+{
1d442b
+	struct fuse_create_in *arg = (struct fuse_create_in *) inarg;
1d442b
+
1d442b
+	if (req->se->op.create) {
1d442b
+		struct fuse_file_info fi;
1d442b
+		char *name = PARAM(arg);
1d442b
+
1d442b
+		memset(&fi, 0, sizeof(fi));
1d442b
+		fi.flags = arg->flags;
1d442b
+
1d442b
+		if (req->se->conn.proto_minor >= 12)
1d442b
+			req->ctx.umask = arg->umask;
1d442b
+		else
1d442b
+			name = (char *) inarg + sizeof(struct fuse_open_in);
1d442b
+
1d442b
+		req->se->op.create(req, nodeid, name, arg->mode, &fi);
1d442b
+	} else
1d442b
+		fuse_reply_err(req, ENOSYS);
1d442b
+}
1d442b
+
1d442b
+static void do_open(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1d442b
+{
1d442b
+	struct fuse_open_in *arg = (struct fuse_open_in *) inarg;
1d442b
+	struct fuse_file_info fi;
1d442b
+
1d442b
+	memset(&fi, 0, sizeof(fi));
1d442b
+	fi.flags = arg->flags;
1d442b
+
1d442b
+	if (req->se->op.open)
1d442b
+		req->se->op.open(req, nodeid, &fi);
1d442b
+	else
1d442b
+		fuse_reply_open(req, &fi);
1d442b
+}
1d442b
+
1d442b
+static void do_read(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1d442b
+{
1d442b
+	struct fuse_read_in *arg = (struct fuse_read_in *) inarg;
1d442b
+
1d442b
+	if (req->se->op.read) {
1d442b
+		struct fuse_file_info fi;
1d442b
+
1d442b
+		memset(&fi, 0, sizeof(fi));
1d442b
+		fi.fh = arg->fh;
1d442b
+		if (req->se->conn.proto_minor >= 9) {
1d442b
+			fi.lock_owner = arg->lock_owner;
1d442b
+			fi.flags = arg->flags;
1d442b
+		}
1d442b
+		req->se->op.read(req, nodeid, arg->size, arg->offset, &fi);
1d442b
+	} else
1d442b
+		fuse_reply_err(req, ENOSYS);
1d442b
+}
1d442b
+
1d442b
+static void do_write(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1d442b
+{
1d442b
+	struct fuse_write_in *arg = (struct fuse_write_in *) inarg;
1d442b
+	struct fuse_file_info fi;
1d442b
+	char *param;
1d442b
+
1d442b
+	memset(&fi, 0, sizeof(fi));
1d442b
+	fi.fh = arg->fh;
1d442b
+	fi.writepage = (arg->write_flags & FUSE_WRITE_CACHE) != 0;
1d442b
+
1d442b
+	if (req->se->conn.proto_minor < 9) {
1d442b
+		param = ((char *) arg) + FUSE_COMPAT_WRITE_IN_SIZE;
1d442b
+	} else {
1d442b
+		fi.lock_owner = arg->lock_owner;
1d442b
+		fi.flags = arg->flags;
1d442b
+		param = PARAM(arg);
1d442b
+	}
1d442b
+
1d442b
+	if (req->se->op.write)
1d442b
+		req->se->op.write(req, nodeid, param, arg->size,
1d442b
+				 arg->offset, &fi);
1d442b
+	else
1d442b
+		fuse_reply_err(req, ENOSYS);
1d442b
+}
1d442b
+
1d442b
+static void do_write_buf(fuse_req_t req, fuse_ino_t nodeid, const void *inarg,
1d442b
+			 const struct fuse_buf *ibuf)
1d442b
+{
1d442b
+	struct fuse_session *se = req->se;
1d442b
+	struct fuse_bufvec bufv = {
1d442b
+		.buf[0] = *ibuf,
1d442b
+		.count = 1,
1d442b
+	};
1d442b
+	struct fuse_write_in *arg = (struct fuse_write_in *) inarg;
1d442b
+	struct fuse_file_info fi;
1d442b
+
1d442b
+	memset(&fi, 0, sizeof(fi));
1d442b
+	fi.fh = arg->fh;
1d442b
+	fi.writepage = arg->write_flags & FUSE_WRITE_CACHE;
1d442b
+
1d442b
+	if (se->conn.proto_minor < 9) {
1d442b
+		bufv.buf[0].mem = ((char *) arg) + FUSE_COMPAT_WRITE_IN_SIZE;
1d442b
+		bufv.buf[0].size -= sizeof(struct fuse_in_header) +
1d442b
+			FUSE_COMPAT_WRITE_IN_SIZE;
1d442b
+		assert(!(bufv.buf[0].flags & FUSE_BUF_IS_FD));
1d442b
+	} else {
1d442b
+		fi.lock_owner = arg->lock_owner;
1d442b
+		fi.flags = arg->flags;
1d442b
+		if (!(bufv.buf[0].flags & FUSE_BUF_IS_FD))
1d442b
+			bufv.buf[0].mem = PARAM(arg);
1d442b
+
1d442b
+		bufv.buf[0].size -= sizeof(struct fuse_in_header) +
1d442b
+			sizeof(struct fuse_write_in);
1d442b
+	}
1d442b
+	if (bufv.buf[0].size < arg->size) {
1d442b
+		fuse_log(FUSE_LOG_ERR, "fuse: do_write_buf: buffer size too small\n");
1d442b
+		fuse_reply_err(req, EIO);
1d442b
+		goto out;
1d442b
+	}
1d442b
+	bufv.buf[0].size = arg->size;
1d442b
+
1d442b
+	se->op.write_buf(req, nodeid, &bufv, arg->offset, &fi);
1d442b
+
1d442b
+out:
1d442b
+	/* Need to reset the pipe if ->write_buf() didn't consume all data */
1d442b
+	if ((ibuf->flags & FUSE_BUF_IS_FD) && bufv.idx < bufv.count)
1d442b
+		fuse_ll_clear_pipe(se);
1d442b
+}
1d442b
+
1d442b
+static void do_flush(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1d442b
+{
1d442b
+	struct fuse_flush_in *arg = (struct fuse_flush_in *) inarg;
1d442b
+	struct fuse_file_info fi;
1d442b
+
1d442b
+	memset(&fi, 0, sizeof(fi));
1d442b
+	fi.fh = arg->fh;
1d442b
+	fi.flush = 1;
1d442b
+	if (req->se->conn.proto_minor >= 7)
1d442b
+		fi.lock_owner = arg->lock_owner;
1d442b
+
1d442b
+	if (req->se->op.flush)
1d442b
+		req->se->op.flush(req, nodeid, &fi);
1d442b
+	else
1d442b
+		fuse_reply_err(req, ENOSYS);
1d442b
+}
1d442b
+
1d442b
+static void do_release(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1d442b
+{
1d442b
+	struct fuse_release_in *arg = (struct fuse_release_in *) inarg;
1d442b
+	struct fuse_file_info fi;
1d442b
+
1d442b
+	memset(&fi, 0, sizeof(fi));
1d442b
+	fi.flags = arg->flags;
1d442b
+	fi.fh = arg->fh;
1d442b
+	if (req->se->conn.proto_minor >= 8) {
1d442b
+		fi.flush = (arg->release_flags & FUSE_RELEASE_FLUSH) ? 1 : 0;
1d442b
+		fi.lock_owner = arg->lock_owner;
1d442b
+	}
1d442b
+	if (arg->release_flags & FUSE_RELEASE_FLOCK_UNLOCK) {
1d442b
+		fi.flock_release = 1;
1d442b
+		fi.lock_owner = arg->lock_owner;
1d442b
+	}
1d442b
+
1d442b
+	if (req->se->op.release)
1d442b
+		req->se->op.release(req, nodeid, &fi);
1d442b
+	else
1d442b
+		fuse_reply_err(req, 0);
1d442b
+}
1d442b
+
1d442b
+static void do_fsync(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1d442b
+{
1d442b
+	struct fuse_fsync_in *arg = (struct fuse_fsync_in *) inarg;
1d442b
+	struct fuse_file_info fi;
1d442b
+	int datasync = arg->fsync_flags & 1;
1d442b
+
1d442b
+	memset(&fi, 0, sizeof(fi));
1d442b
+	fi.fh = arg->fh;
1d442b
+
1d442b
+	if (req->se->op.fsync)
1d442b
+		req->se->op.fsync(req, nodeid, datasync, &fi);
1d442b
+	else
1d442b
+		fuse_reply_err(req, ENOSYS);
1d442b
+}
1d442b
+
1d442b
+static void do_opendir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1d442b
+{
1d442b
+	struct fuse_open_in *arg = (struct fuse_open_in *) inarg;
1d442b
+	struct fuse_file_info fi;
1d442b
+
1d442b
+	memset(&fi, 0, sizeof(fi));
1d442b
+	fi.flags = arg->flags;
1d442b
+
1d442b
+	if (req->se->op.opendir)
1d442b
+		req->se->op.opendir(req, nodeid, &fi);
1d442b
+	else
1d442b
+		fuse_reply_open(req, &fi);
1d442b
+}
1d442b
+
1d442b
+static void do_readdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1d442b
+{
1d442b
+	struct fuse_read_in *arg = (struct fuse_read_in *) inarg;
1d442b
+	struct fuse_file_info fi;
1d442b
+
1d442b
+	memset(&fi, 0, sizeof(fi));
1d442b
+	fi.fh = arg->fh;
1d442b
+
1d442b
+	if (req->se->op.readdir)
1d442b
+		req->se->op.readdir(req, nodeid, arg->size, arg->offset, &fi);
1d442b
+	else
1d442b
+		fuse_reply_err(req, ENOSYS);
1d442b
+}
1d442b
+
1d442b
+static void do_readdirplus(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1d442b
+{
1d442b
+	struct fuse_read_in *arg = (struct fuse_read_in *) inarg;
1d442b
+	struct fuse_file_info fi;
1d442b
+
1d442b
+	memset(&fi, 0, sizeof(fi));
1d442b
+	fi.fh = arg->fh;
1d442b
+
1d442b
+	if (req->se->op.readdirplus)
1d442b
+		req->se->op.readdirplus(req, nodeid, arg->size, arg->offset, &fi);
1d442b
+	else
1d442b
+		fuse_reply_err(req, ENOSYS);
1d442b
+}
1d442b
+
1d442b
+static void do_releasedir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1d442b
+{
1d442b
+	struct fuse_release_in *arg = (struct fuse_release_in *) inarg;
1d442b
+	struct fuse_file_info fi;
1d442b
+
1d442b
+	memset(&fi, 0, sizeof(fi));
1d442b
+	fi.flags = arg->flags;
1d442b
+	fi.fh = arg->fh;
1d442b
+
1d442b
+	if (req->se->op.releasedir)
1d442b
+		req->se->op.releasedir(req, nodeid, &fi);
1d442b
+	else
1d442b
+		fuse_reply_err(req, 0);
1d442b
+}
1d442b
+
1d442b
+static void do_fsyncdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1d442b
+{
1d442b
+	struct fuse_fsync_in *arg = (struct fuse_fsync_in *) inarg;
1d442b
+	struct fuse_file_info fi;
1d442b
+	int datasync = arg->fsync_flags & 1;
1d442b
+
1d442b
+	memset(&fi, 0, sizeof(fi));
1d442b
+	fi.fh = arg->fh;
1d442b
+
1d442b
+	if (req->se->op.fsyncdir)
1d442b
+		req->se->op.fsyncdir(req, nodeid, datasync, &fi);
1d442b
+	else
1d442b
+		fuse_reply_err(req, ENOSYS);
1d442b
+}
1d442b
+
1d442b
+static void do_statfs(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1d442b
+{
1d442b
+	(void) nodeid;
1d442b
+	(void) inarg;
1d442b
+
1d442b
+	if (req->se->op.statfs)
1d442b
+		req->se->op.statfs(req, nodeid);
1d442b
+	else {
1d442b
+		struct statvfs buf = {
1d442b
+			.f_namemax = 255,
1d442b
+			.f_bsize = 512,
1d442b
+		};
1d442b
+		fuse_reply_statfs(req, &buf;;
1d442b
+	}
1d442b
+}
1d442b
+
1d442b
+static void do_setxattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1d442b
+{
1d442b
+	struct fuse_setxattr_in *arg = (struct fuse_setxattr_in *) inarg;
1d442b
+	char *name = PARAM(arg);
1d442b
+	char *value = name + strlen(name) + 1;
1d442b
+
1d442b
+	if (req->se->op.setxattr)
1d442b
+		req->se->op.setxattr(req, nodeid, name, value, arg->size,
1d442b
+				    arg->flags);
1d442b
+	else
1d442b
+		fuse_reply_err(req, ENOSYS);
1d442b
+}
1d442b
+
1d442b
+static void do_getxattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1d442b
+{
1d442b
+	struct fuse_getxattr_in *arg = (struct fuse_getxattr_in *) inarg;
1d442b
+
1d442b
+	if (req->se->op.getxattr)
1d442b
+		req->se->op.getxattr(req, nodeid, PARAM(arg), arg->size);
1d442b
+	else
1d442b
+		fuse_reply_err(req, ENOSYS);
1d442b
+}
1d442b
+
1d442b
+static void do_listxattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1d442b
+{
1d442b
+	struct fuse_getxattr_in *arg = (struct fuse_getxattr_in *) inarg;
1d442b
+
1d442b
+	if (req->se->op.listxattr)
1d442b
+		req->se->op.listxattr(req, nodeid, arg->size);
1d442b
+	else
1d442b
+		fuse_reply_err(req, ENOSYS);
1d442b
+}
1d442b
+
1d442b
+static void do_removexattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1d442b
+{
1d442b
+	char *name = (char *) inarg;
1d442b
+
1d442b
+	if (req->se->op.removexattr)
1d442b
+		req->se->op.removexattr(req, nodeid, name);
1d442b
+	else
1d442b
+		fuse_reply_err(req, ENOSYS);
1d442b
+}
1d442b
+
1d442b
+static void convert_fuse_file_lock(struct fuse_file_lock *fl,
1d442b
+				   struct flock *flock)
1d442b
+{
1d442b
+	memset(flock, 0, sizeof(struct flock));
1d442b
+	flock->l_type = fl->type;
1d442b
+	flock->l_whence = SEEK_SET;
1d442b
+	flock->l_start = fl->start;
1d442b
+	if (fl->end == OFFSET_MAX)
1d442b
+		flock->l_len = 0;
1d442b
+	else
1d442b
+		flock->l_len = fl->end - fl->start + 1;
1d442b
+	flock->l_pid = fl->pid;
1d442b
+}
1d442b
+
1d442b
+static void do_getlk(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1d442b
+{
1d442b
+	struct fuse_lk_in *arg = (struct fuse_lk_in *) inarg;
1d442b
+	struct fuse_file_info fi;
1d442b
+	struct flock flock;
1d442b
+
1d442b
+	memset(&fi, 0, sizeof(fi));
1d442b
+	fi.fh = arg->fh;
1d442b
+	fi.lock_owner = arg->owner;
1d442b
+
1d442b
+	convert_fuse_file_lock(&arg->lk, &flock);
1d442b
+	if (req->se->op.getlk)
1d442b
+		req->se->op.getlk(req, nodeid, &fi, &flock);
1d442b
+	else
1d442b
+		fuse_reply_err(req, ENOSYS);
1d442b
+}
1d442b
+
1d442b
+static void do_setlk_common(fuse_req_t req, fuse_ino_t nodeid,
1d442b
+			    const void *inarg, int sleep)
1d442b
+{
1d442b
+	struct fuse_lk_in *arg = (struct fuse_lk_in *) inarg;
1d442b
+	struct fuse_file_info fi;
1d442b
+	struct flock flock;
1d442b
+
1d442b
+	memset(&fi, 0, sizeof(fi));
1d442b
+	fi.fh = arg->fh;
1d442b
+	fi.lock_owner = arg->owner;
1d442b
+
1d442b
+	if (arg->lk_flags & FUSE_LK_FLOCK) {
1d442b
+		int op = 0;
1d442b
+
1d442b
+		switch (arg->lk.type) {
1d442b
+		case F_RDLCK:
1d442b
+			op = LOCK_SH;
1d442b
+			break;
1d442b
+		case F_WRLCK:
1d442b
+			op = LOCK_EX;
1d442b
+			break;
1d442b
+		case F_UNLCK:
1d442b
+			op = LOCK_UN;
1d442b
+			break;
1d442b
+		}
1d442b
+		if (!sleep)
1d442b
+			op |= LOCK_NB;
1d442b
+
1d442b
+		if (req->se->op.flock)
1d442b
+			req->se->op.flock(req, nodeid, &fi, op);
1d442b
+		else
1d442b
+			fuse_reply_err(req, ENOSYS);
1d442b
+	} else {
1d442b
+		convert_fuse_file_lock(&arg->lk, &flock);
1d442b
+		if (req->se->op.setlk)
1d442b
+			req->se->op.setlk(req, nodeid, &fi, &flock, sleep);
1d442b
+		else
1d442b
+			fuse_reply_err(req, ENOSYS);
1d442b
+	}
1d442b
+}
1d442b
+
1d442b
+static void do_setlk(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1d442b
+{
1d442b
+	do_setlk_common(req, nodeid, inarg, 0);
1d442b
+}
1d442b
+
1d442b
+static void do_setlkw(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1d442b
+{
1d442b
+	do_setlk_common(req, nodeid, inarg, 1);
1d442b
+}
1d442b
+
1d442b
+static int find_interrupted(struct fuse_session *se, struct fuse_req *req)
1d442b
+{
1d442b
+	struct fuse_req *curr;
1d442b
+
1d442b
+	for (curr = se->list.next; curr != &se->list; curr = curr->next) {
1d442b
+		if (curr->unique == req->u.i.unique) {
1d442b
+			fuse_interrupt_func_t func;
1d442b
+			void *data;
1d442b
+
1d442b
+			curr->ctr++;
1d442b
+			pthread_mutex_unlock(&se->lock);
1d442b
+
1d442b
+			/* Ugh, ugly locking */
1d442b
+			pthread_mutex_lock(&curr->lock);
1d442b
+			pthread_mutex_lock(&se->lock);
1d442b
+			curr->interrupted = 1;
1d442b
+			func = curr->u.ni.func;
1d442b
+			data = curr->u.ni.data;
1d442b
+			pthread_mutex_unlock(&se->lock);
1d442b
+			if (func)
1d442b
+				func(curr, data);
1d442b
+			pthread_mutex_unlock(&curr->lock);
1d442b
+
1d442b
+			pthread_mutex_lock(&se->lock);
1d442b
+			curr->ctr--;
1d442b
+			if (!curr->ctr)
1d442b
+				destroy_req(curr);
1d442b
+
1d442b
+			return 1;
1d442b
+		}
1d442b
+	}
1d442b
+	for (curr = se->interrupts.next; curr != &se->interrupts;
1d442b
+	     curr = curr->next) {
1d442b
+		if (curr->u.i.unique == req->u.i.unique)
1d442b
+			return 1;
1d442b
+	}
1d442b
+	return 0;
1d442b
+}
1d442b
+
1d442b
+static void do_interrupt(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1d442b
+{
1d442b
+	struct fuse_interrupt_in *arg = (struct fuse_interrupt_in *) inarg;
1d442b
+	struct fuse_session *se = req->se;
1d442b
+
1d442b
+	(void) nodeid;
1d442b
+	if (se->debug)
1d442b
+		fuse_log(FUSE_LOG_DEBUG, "INTERRUPT: %llu\n",
1d442b
+			(unsigned long long) arg->unique);
1d442b
+
1d442b
+	req->u.i.unique = arg->unique;
1d442b
+
1d442b
+	pthread_mutex_lock(&se->lock);
1d442b
+	if (find_interrupted(se, req))
1d442b
+		destroy_req(req);
1d442b
+	else
1d442b
+		list_add_req(req, &se->interrupts);
1d442b
+	pthread_mutex_unlock(&se->lock);
1d442b
+}
1d442b
+
1d442b
+static struct fuse_req *check_interrupt(struct fuse_session *se,
1d442b
+					struct fuse_req *req)
1d442b
+{
1d442b
+	struct fuse_req *curr;
1d442b
+
1d442b
+	for (curr = se->interrupts.next; curr != &se->interrupts;
1d442b
+	     curr = curr->next) {
1d442b
+		if (curr->u.i.unique == req->unique) {
1d442b
+			req->interrupted = 1;
1d442b
+			list_del_req(curr);
1d442b
+			free(curr);
1d442b
+			return NULL;
1d442b
+		}
1d442b
+	}
1d442b
+	curr = se->interrupts.next;
1d442b
+	if (curr != &se->interrupts) {
1d442b
+		list_del_req(curr);
1d442b
+		list_init_req(curr);
1d442b
+		return curr;
1d442b
+	} else
1d442b
+		return NULL;
1d442b
+}
1d442b
+
1d442b
+static void do_bmap(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1d442b
+{
1d442b
+	struct fuse_bmap_in *arg = (struct fuse_bmap_in *) inarg;
1d442b
+
1d442b
+	if (req->se->op.bmap)
1d442b
+		req->se->op.bmap(req, nodeid, arg->blocksize, arg->block);
1d442b
+	else
1d442b
+		fuse_reply_err(req, ENOSYS);
1d442b
+}
1d442b
+
1d442b
+static void do_ioctl(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1d442b
+{
1d442b
+	struct fuse_ioctl_in *arg = (struct fuse_ioctl_in *) inarg;
1d442b
+	unsigned int flags = arg->flags;
1d442b
+	void *in_buf = arg->in_size ? PARAM(arg) : NULL;
1d442b
+	struct fuse_file_info fi;
1d442b
+
1d442b
+	if (flags & FUSE_IOCTL_DIR &&
1d442b
+	    !(req->se->conn.want & FUSE_CAP_IOCTL_DIR)) {
1d442b
+		fuse_reply_err(req, ENOTTY);
1d442b
+		return;
1d442b
+	}
1d442b
+
1d442b
+	memset(&fi, 0, sizeof(fi));
1d442b
+	fi.fh = arg->fh;
1d442b
+
1d442b
+	if (sizeof(void *) == 4 && req->se->conn.proto_minor >= 16 &&
1d442b
+	    !(flags & FUSE_IOCTL_32BIT)) {
1d442b
+		req->ioctl_64bit = 1;
1d442b
+	}
1d442b
+
1d442b
+	if (req->se->op.ioctl)
1d442b
+		req->se->op.ioctl(req, nodeid, arg->cmd,
1d442b
+				 (void *)(uintptr_t)arg->arg, &fi, flags,
1d442b
+				 in_buf, arg->in_size, arg->out_size);
1d442b
+	else
1d442b
+		fuse_reply_err(req, ENOSYS);
1d442b
+}
1d442b
+
1d442b
+void fuse_pollhandle_destroy(struct fuse_pollhandle *ph)
1d442b
+{
1d442b
+	free(ph);
1d442b
+}
1d442b
+
1d442b
+static void do_poll(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1d442b
+{
1d442b
+	struct fuse_poll_in *arg = (struct fuse_poll_in *) inarg;
1d442b
+	struct fuse_file_info fi;
1d442b
+
1d442b
+	memset(&fi, 0, sizeof(fi));
1d442b
+	fi.fh = arg->fh;
1d442b
+	fi.poll_events = arg->events;
1d442b
+
1d442b
+	if (req->se->op.poll) {
1d442b
+		struct fuse_pollhandle *ph = NULL;
1d442b
+
1d442b
+		if (arg->flags & FUSE_POLL_SCHEDULE_NOTIFY) {
1d442b
+			ph = malloc(sizeof(struct fuse_pollhandle));
1d442b
+			if (ph == NULL) {
1d442b
+				fuse_reply_err(req, ENOMEM);
1d442b
+				return;
1d442b
+			}
1d442b
+			ph->kh = arg->kh;
1d442b
+			ph->se = req->se;
1d442b
+		}
1d442b
+
1d442b
+		req->se->op.poll(req, nodeid, &fi, ph);
1d442b
+	} else {
1d442b
+		fuse_reply_err(req, ENOSYS);
1d442b
+	}
1d442b
+}
1d442b
+
1d442b
+static void do_fallocate(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1d442b
+{
1d442b
+	struct fuse_fallocate_in *arg = (struct fuse_fallocate_in *) inarg;
1d442b
+	struct fuse_file_info fi;
1d442b
+
1d442b
+	memset(&fi, 0, sizeof(fi));
1d442b
+	fi.fh = arg->fh;
1d442b
+
1d442b
+	if (req->se->op.fallocate)
1d442b
+		req->se->op.fallocate(req, nodeid, arg->mode, arg->offset, arg->length, &fi);
1d442b
+	else
1d442b
+		fuse_reply_err(req, ENOSYS);
1d442b
+}
1d442b
+
1d442b
+static void do_copy_file_range(fuse_req_t req, fuse_ino_t nodeid_in, const void *inarg)
1d442b
+{
1d442b
+	struct fuse_copy_file_range_in *arg = (struct fuse_copy_file_range_in *) inarg;
1d442b
+	struct fuse_file_info fi_in, fi_out;
1d442b
+
1d442b
+	memset(&fi_in, 0, sizeof(fi_in));
1d442b
+	fi_in.fh = arg->fh_in;
1d442b
+
1d442b
+	memset(&fi_out, 0, sizeof(fi_out));
1d442b
+	fi_out.fh = arg->fh_out;
1d442b
+
1d442b
+
1d442b
+	if (req->se->op.copy_file_range)
1d442b
+		req->se->op.copy_file_range(req, nodeid_in, arg->off_in,
1d442b
+					    &fi_in, arg->nodeid_out,
1d442b
+					    arg->off_out, &fi_out, arg->len,
1d442b
+					    arg->flags);
1d442b
+	else
1d442b
+		fuse_reply_err(req, ENOSYS);
1d442b
+}
1d442b
+
1d442b
+static void do_lseek(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1d442b
+{
1d442b
+	struct fuse_lseek_in *arg = (struct fuse_lseek_in *) inarg;
1d442b
+	struct fuse_file_info fi;
1d442b
+
1d442b
+	memset(&fi, 0, sizeof(fi));
1d442b
+	fi.fh = arg->fh;
1d442b
+
1d442b
+	if (req->se->op.lseek)
1d442b
+		req->se->op.lseek(req, nodeid, arg->offset, arg->whence, &fi);
1d442b
+	else
1d442b
+		fuse_reply_err(req, ENOSYS);
1d442b
+}
1d442b
+
1d442b
+static void do_init(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1d442b
+{
1d442b
+	struct fuse_init_in *arg = (struct fuse_init_in *) inarg;
1d442b
+	struct fuse_init_out outarg;
1d442b
+	struct fuse_session *se = req->se;
1d442b
+	size_t bufsize = se->bufsize;
1d442b
+	size_t outargsize = sizeof(outarg);
1d442b
+
1d442b
+	(void) nodeid;
1d442b
+	if (se->debug) {
1d442b
+		fuse_log(FUSE_LOG_DEBUG, "INIT: %u.%u\n", arg->major, arg->minor);
1d442b
+		if (arg->major == 7 && arg->minor >= 6) {
1d442b
+			fuse_log(FUSE_LOG_DEBUG, "flags=0x%08x\n", arg->flags);
1d442b
+			fuse_log(FUSE_LOG_DEBUG, "max_readahead=0x%08x\n",
1d442b
+				arg->max_readahead);
1d442b
+		}
1d442b
+	}
1d442b
+	se->conn.proto_major = arg->major;
1d442b
+	se->conn.proto_minor = arg->minor;
1d442b
+	se->conn.capable = 0;
1d442b
+	se->conn.want = 0;
1d442b
+
1d442b
+	memset(&outarg, 0, sizeof(outarg));
1d442b
+	outarg.major = FUSE_KERNEL_VERSION;
1d442b
+	outarg.minor = FUSE_KERNEL_MINOR_VERSION;
1d442b
+
1d442b
+	if (arg->major < 7) {
1d442b
+		fuse_log(FUSE_LOG_ERR, "fuse: unsupported protocol version: %u.%u\n",
1d442b
+			arg->major, arg->minor);
1d442b
+		fuse_reply_err(req, EPROTO);
1d442b
+		return;
1d442b
+	}
1d442b
+
1d442b
+	if (arg->major > 7) {
1d442b
+		/* Wait for a second INIT request with a 7.X version */
1d442b
+		send_reply_ok(req, &outarg, sizeof(outarg));
1d442b
+		return;
1d442b
+	}
1d442b
+
1d442b
+	if (arg->minor >= 6) {
1d442b
+		if (arg->max_readahead < se->conn.max_readahead)
1d442b
+			se->conn.max_readahead = arg->max_readahead;
1d442b
+		if (arg->flags & FUSE_ASYNC_READ)
1d442b
+			se->conn.capable |= FUSE_CAP_ASYNC_READ;
1d442b
+		if (arg->flags & FUSE_POSIX_LOCKS)
1d442b
+			se->conn.capable |= FUSE_CAP_POSIX_LOCKS;
1d442b
+		if (arg->flags & FUSE_ATOMIC_O_TRUNC)
1d442b
+			se->conn.capable |= FUSE_CAP_ATOMIC_O_TRUNC;
1d442b
+		if (arg->flags & FUSE_EXPORT_SUPPORT)
1d442b
+			se->conn.capable |= FUSE_CAP_EXPORT_SUPPORT;
1d442b
+		if (arg->flags & FUSE_DONT_MASK)
1d442b
+			se->conn.capable |= FUSE_CAP_DONT_MASK;
1d442b
+		if (arg->flags & FUSE_FLOCK_LOCKS)
1d442b
+			se->conn.capable |= FUSE_CAP_FLOCK_LOCKS;
1d442b
+		if (arg->flags & FUSE_AUTO_INVAL_DATA)
1d442b
+			se->conn.capable |= FUSE_CAP_AUTO_INVAL_DATA;
1d442b
+		if (arg->flags & FUSE_DO_READDIRPLUS)
1d442b
+			se->conn.capable |= FUSE_CAP_READDIRPLUS;
1d442b
+		if (arg->flags & FUSE_READDIRPLUS_AUTO)
1d442b
+			se->conn.capable |= FUSE_CAP_READDIRPLUS_AUTO;
1d442b
+		if (arg->flags & FUSE_ASYNC_DIO)
1d442b
+			se->conn.capable |= FUSE_CAP_ASYNC_DIO;
1d442b
+		if (arg->flags & FUSE_WRITEBACK_CACHE)
1d442b
+			se->conn.capable |= FUSE_CAP_WRITEBACK_CACHE;
1d442b
+		if (arg->flags & FUSE_NO_OPEN_SUPPORT)
1d442b
+			se->conn.capable |= FUSE_CAP_NO_OPEN_SUPPORT;
1d442b
+		if (arg->flags & FUSE_PARALLEL_DIROPS)
1d442b
+			se->conn.capable |= FUSE_CAP_PARALLEL_DIROPS;
1d442b
+		if (arg->flags & FUSE_POSIX_ACL)
1d442b
+			se->conn.capable |= FUSE_CAP_POSIX_ACL;
1d442b
+		if (arg->flags & FUSE_HANDLE_KILLPRIV)
1d442b
+			se->conn.capable |= FUSE_CAP_HANDLE_KILLPRIV;
1d442b
+		if (arg->flags & FUSE_NO_OPENDIR_SUPPORT)
1d442b
+			se->conn.capable |= FUSE_CAP_NO_OPENDIR_SUPPORT;
1d442b
+		if (!(arg->flags & FUSE_MAX_PAGES)) {
1d442b
+			size_t max_bufsize =
1d442b
+				FUSE_DEFAULT_MAX_PAGES_PER_REQ * getpagesize()
1d442b
+				+ FUSE_BUFFER_HEADER_SIZE;
1d442b
+			if (bufsize > max_bufsize) {
1d442b
+				bufsize = max_bufsize;
1d442b
+			}
1d442b
+		}
1d442b
+	} else {
1d442b
+		se->conn.max_readahead = 0;
1d442b
+	}
1d442b
+
1d442b
+	if (se->conn.proto_minor >= 14) {
1d442b
+#ifdef HAVE_SPLICE
1d442b
+#ifdef HAVE_VMSPLICE
1d442b
+		se->conn.capable |= FUSE_CAP_SPLICE_WRITE | FUSE_CAP_SPLICE_MOVE;
1d442b
+#endif
1d442b
+		se->conn.capable |= FUSE_CAP_SPLICE_READ;
1d442b
+#endif
1d442b
+	}
1d442b
+	if (se->conn.proto_minor >= 18)
1d442b
+		se->conn.capable |= FUSE_CAP_IOCTL_DIR;
1d442b
+
1d442b
+	/* Default settings for modern filesystems.
1d442b
+	 *
1d442b
+	 * Most of these capabilities were disabled by default in
1d442b
+	 * libfuse2 for backwards compatibility reasons. In libfuse3,
1d442b
+	 * we can finally enable them by default (as long as they're
1d442b
+	 * supported by the kernel).
1d442b
+	 */
1d442b
+#define LL_SET_DEFAULT(cond, cap) \
1d442b
+	if ((cond) && (se->conn.capable & (cap))) \
1d442b
+		se->conn.want |= (cap)
1d442b
+	LL_SET_DEFAULT(1, FUSE_CAP_ASYNC_READ);
1d442b
+	LL_SET_DEFAULT(1, FUSE_CAP_PARALLEL_DIROPS);
1d442b
+	LL_SET_DEFAULT(1, FUSE_CAP_AUTO_INVAL_DATA);
1d442b
+	LL_SET_DEFAULT(1, FUSE_CAP_HANDLE_KILLPRIV);
1d442b
+	LL_SET_DEFAULT(1, FUSE_CAP_ASYNC_DIO);
1d442b
+	LL_SET_DEFAULT(1, FUSE_CAP_IOCTL_DIR);
1d442b
+	LL_SET_DEFAULT(1, FUSE_CAP_ATOMIC_O_TRUNC);
1d442b
+	LL_SET_DEFAULT(se->op.write_buf, FUSE_CAP_SPLICE_READ);
1d442b
+	LL_SET_DEFAULT(se->op.getlk && se->op.setlk,
1d442b
+		       FUSE_CAP_POSIX_LOCKS);
1d442b
+	LL_SET_DEFAULT(se->op.flock, FUSE_CAP_FLOCK_LOCKS);
1d442b
+	LL_SET_DEFAULT(se->op.readdirplus, FUSE_CAP_READDIRPLUS);
1d442b
+	LL_SET_DEFAULT(se->op.readdirplus && se->op.readdir,
1d442b
+		       FUSE_CAP_READDIRPLUS_AUTO);
1d442b
+	se->conn.time_gran = 1;
1d442b
+	
1d442b
+	if (bufsize < FUSE_MIN_READ_BUFFER) {
1d442b
+		fuse_log(FUSE_LOG_ERR, "fuse: warning: buffer size too small: %zu\n",
1d442b
+			bufsize);
1d442b
+		bufsize = FUSE_MIN_READ_BUFFER;
1d442b
+	}
1d442b
+	se->bufsize = bufsize;
1d442b
+
1d442b
+	if (se->conn.max_write > bufsize - FUSE_BUFFER_HEADER_SIZE)
1d442b
+		se->conn.max_write = bufsize - FUSE_BUFFER_HEADER_SIZE;
1d442b
+
1d442b
+	se->got_init = 1;
1d442b
+	if (se->op.init)
1d442b
+		se->op.init(se->userdata, &se->conn);
1d442b
+
1d442b
+	if (se->conn.want & (~se->conn.capable)) {
1d442b
+		fuse_log(FUSE_LOG_ERR, "fuse: error: filesystem requested capabilities "
1d442b
+			"0x%x that are not supported by kernel, aborting.\n",
1d442b
+			se->conn.want & (~se->conn.capable));
1d442b
+		fuse_reply_err(req, EPROTO);
1d442b
+		se->error = -EPROTO;
1d442b
+		fuse_session_exit(se);
1d442b
+		return;
1d442b
+	}
1d442b
+
1d442b
+	unsigned max_read_mo = get_max_read(se->mo);
1d442b
+	if (se->conn.max_read != max_read_mo) {
1d442b
+		fuse_log(FUSE_LOG_ERR, "fuse: error: init() and fuse_session_new() "
1d442b
+			"requested different maximum read size (%u vs %u)\n",
1d442b
+			se->conn.max_read, max_read_mo);
1d442b
+		fuse_reply_err(req, EPROTO);
1d442b
+		se->error = -EPROTO;
1d442b
+		fuse_session_exit(se);
1d442b
+		return;
1d442b
+	}
1d442b
+
1d442b
+	if (se->conn.max_write < bufsize - FUSE_BUFFER_HEADER_SIZE) {
1d442b
+		se->bufsize = se->conn.max_write + FUSE_BUFFER_HEADER_SIZE;
1d442b
+	}
1d442b
+	if (arg->flags & FUSE_MAX_PAGES) {
1d442b
+		outarg.flags |= FUSE_MAX_PAGES;
1d442b
+		outarg.max_pages = (se->conn.max_write - 1) / getpagesize() + 1;
1d442b
+	}
1d442b
+
1d442b
+	/* Always enable big writes, this is superseded
1d442b
+	   by the max_write option */
1d442b
+	outarg.flags |= FUSE_BIG_WRITES;
1d442b
+
1d442b
+	if (se->conn.want & FUSE_CAP_ASYNC_READ)
1d442b
+		outarg.flags |= FUSE_ASYNC_READ;
1d442b
+	if (se->conn.want & FUSE_CAP_POSIX_LOCKS)
1d442b
+		outarg.flags |= FUSE_POSIX_LOCKS;
1d442b
+	if (se->conn.want & FUSE_CAP_ATOMIC_O_TRUNC)
1d442b
+		outarg.flags |= FUSE_ATOMIC_O_TRUNC;
1d442b
+	if (se->conn.want & FUSE_CAP_EXPORT_SUPPORT)
1d442b
+		outarg.flags |= FUSE_EXPORT_SUPPORT;
1d442b
+	if (se->conn.want & FUSE_CAP_DONT_MASK)
1d442b
+		outarg.flags |= FUSE_DONT_MASK;
1d442b
+	if (se->conn.want & FUSE_CAP_FLOCK_LOCKS)
1d442b
+		outarg.flags |= FUSE_FLOCK_LOCKS;
1d442b
+	if (se->conn.want & FUSE_CAP_AUTO_INVAL_DATA)
1d442b
+		outarg.flags |= FUSE_AUTO_INVAL_DATA;
1d442b
+	if (se->conn.want & FUSE_CAP_READDIRPLUS)
1d442b
+		outarg.flags |= FUSE_DO_READDIRPLUS;
1d442b
+	if (se->conn.want & FUSE_CAP_READDIRPLUS_AUTO)
1d442b
+		outarg.flags |= FUSE_READDIRPLUS_AUTO;
1d442b
+	if (se->conn.want & FUSE_CAP_ASYNC_DIO)
1d442b
+		outarg.flags |= FUSE_ASYNC_DIO;
1d442b
+	if (se->conn.want & FUSE_CAP_WRITEBACK_CACHE)
1d442b
+		outarg.flags |= FUSE_WRITEBACK_CACHE;
1d442b
+	if (se->conn.want & FUSE_CAP_POSIX_ACL)
1d442b
+		outarg.flags |= FUSE_POSIX_ACL;
1d442b
+	outarg.max_readahead = se->conn.max_readahead;
1d442b
+	outarg.max_write = se->conn.max_write;
1d442b
+	if (se->conn.proto_minor >= 13) {
1d442b
+		if (se->conn.max_background >= (1 << 16))
1d442b
+			se->conn.max_background = (1 << 16) - 1;
1d442b
+		if (se->conn.congestion_threshold > se->conn.max_background)
1d442b
+			se->conn.congestion_threshold = se->conn.max_background;
1d442b
+		if (!se->conn.congestion_threshold) {
1d442b
+			se->conn.congestion_threshold =
1d442b
+				se->conn.max_background * 3 / 4;
1d442b
+		}
1d442b
+
1d442b
+		outarg.max_background = se->conn.max_background;
1d442b
+		outarg.congestion_threshold = se->conn.congestion_threshold;
1d442b
+	}
1d442b
+	if (se->conn.proto_minor >= 23)
1d442b
+		outarg.time_gran = se->conn.time_gran;
1d442b
+
1d442b
+	if (se->debug) {
1d442b
+		fuse_log(FUSE_LOG_DEBUG, "   INIT: %u.%u\n", outarg.major, outarg.minor);
1d442b
+		fuse_log(FUSE_LOG_DEBUG, "   flags=0x%08x\n", outarg.flags);
1d442b
+		fuse_log(FUSE_LOG_DEBUG, "   max_readahead=0x%08x\n",
1d442b
+			outarg.max_readahead);
1d442b
+		fuse_log(FUSE_LOG_DEBUG, "   max_write=0x%08x\n", outarg.max_write);
1d442b
+		fuse_log(FUSE_LOG_DEBUG, "   max_background=%i\n",
1d442b
+			outarg.max_background);
1d442b
+		fuse_log(FUSE_LOG_DEBUG, "   congestion_threshold=%i\n",
1d442b
+			outarg.congestion_threshold);
1d442b
+		fuse_log(FUSE_LOG_DEBUG, "   time_gran=%u\n",
1d442b
+			outarg.time_gran);
1d442b
+	}
1d442b
+	if (arg->minor < 5)
1d442b
+		outargsize = FUSE_COMPAT_INIT_OUT_SIZE;
1d442b
+	else if (arg->minor < 23)
1d442b
+		outargsize = FUSE_COMPAT_22_INIT_OUT_SIZE;
1d442b
+
1d442b
+	send_reply_ok(req, &outarg, outargsize);
1d442b
+}
1d442b
+
1d442b
+static void do_destroy(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1d442b
+{
1d442b
+	struct fuse_session *se = req->se;
1d442b
+
1d442b
+	(void) nodeid;
1d442b
+	(void) inarg;
1d442b
+
1d442b
+	se->got_destroy = 1;
1d442b
+	if (se->op.destroy)
1d442b
+		se->op.destroy(se->userdata);
1d442b
+
1d442b
+	send_reply_ok(req, NULL, 0);
1d442b
+}
1d442b
+
1d442b
+static void list_del_nreq(struct fuse_notify_req *nreq)
1d442b
+{
1d442b
+	struct fuse_notify_req *prev = nreq->prev;
1d442b
+	struct fuse_notify_req *next = nreq->next;
1d442b
+	prev->next = next;
1d442b
+	next->prev = prev;
1d442b
+}
1d442b
+
1d442b
+static void list_add_nreq(struct fuse_notify_req *nreq,
1d442b
+			  struct fuse_notify_req *next)
1d442b
+{
1d442b
+	struct fuse_notify_req *prev = next->prev;
1d442b
+	nreq->next = next;
1d442b
+	nreq->prev = prev;
1d442b
+	prev->next = nreq;
1d442b
+	next->prev = nreq;
1d442b
+}
1d442b
+
1d442b
+static void list_init_nreq(struct fuse_notify_req *nreq)
1d442b
+{
1d442b
+	nreq->next = nreq;
1d442b
+	nreq->prev = nreq;
1d442b
+}
1d442b
+
1d442b
+static void do_notify_reply(fuse_req_t req, fuse_ino_t nodeid,
1d442b
+			    const void *inarg, const struct fuse_buf *buf)
1d442b
+{
1d442b
+	struct fuse_session *se = req->se;
1d442b
+	struct fuse_notify_req *nreq;
1d442b
+	struct fuse_notify_req *head;
1d442b
+
1d442b
+	pthread_mutex_lock(&se->lock);
1d442b
+	head = &se->notify_list;
1d442b
+	for (nreq = head->next; nreq != head; nreq = nreq->next) {
1d442b
+		if (nreq->unique == req->unique) {
1d442b
+			list_del_nreq(nreq);
1d442b
+			break;
1d442b
+		}
1d442b
+	}
1d442b
+	pthread_mutex_unlock(&se->lock);
1d442b
+
1d442b
+	if (nreq != head)
1d442b
+		nreq->reply(nreq, req, nodeid, inarg, buf);
1d442b
+}
1d442b
+
1d442b
+static int send_notify_iov(struct fuse_session *se, int notify_code,
1d442b
+			   struct iovec *iov, int count)
1d442b
+{
1d442b
+	struct fuse_out_header out;
1d442b
+
1d442b
+	if (!se->got_init)
1d442b
+		return -ENOTCONN;
1d442b
+
1d442b
+	out.unique = 0;
1d442b
+	out.error = notify_code;
1d442b
+	iov[0].iov_base = &out;
1d442b
+	iov[0].iov_len = sizeof(struct fuse_out_header);
1d442b
+
1d442b
+	return fuse_send_msg(se, NULL, iov, count);
1d442b
+}
1d442b
+
1d442b
+int fuse_lowlevel_notify_poll(struct fuse_pollhandle *ph)
1d442b
+{
1d442b
+	if (ph != NULL) {
1d442b
+		struct fuse_notify_poll_wakeup_out outarg;
1d442b
+		struct iovec iov[2];
1d442b
+
1d442b
+		outarg.kh = ph->kh;
1d442b
+
1d442b
+		iov[1].iov_base = &outarg;
1d442b
+		iov[1].iov_len = sizeof(outarg);
1d442b
+
1d442b
+		return send_notify_iov(ph->se, FUSE_NOTIFY_POLL, iov, 2);
1d442b
+	} else {
1d442b
+		return 0;
1d442b
+	}
1d442b
+}
1d442b
+
1d442b
+int fuse_lowlevel_notify_inval_inode(struct fuse_session *se, fuse_ino_t ino,
1d442b
+				     off_t off, off_t len)
1d442b
+{
1d442b
+	struct fuse_notify_inval_inode_out outarg;
1d442b
+	struct iovec iov[2];
1d442b
+
1d442b
+	if (!se)
1d442b
+		return -EINVAL;
1d442b
+
1d442b
+	if (se->conn.proto_major < 6 || se->conn.proto_minor < 12)
1d442b
+		return -ENOSYS;
1d442b
+	
1d442b
+	outarg.ino = ino;
1d442b
+	outarg.off = off;
1d442b
+	outarg.len = len;
1d442b
+
1d442b
+	iov[1].iov_base = &outarg;
1d442b
+	iov[1].iov_len = sizeof(outarg);
1d442b
+
1d442b
+	return send_notify_iov(se, FUSE_NOTIFY_INVAL_INODE, iov, 2);
1d442b
+}
1d442b
+
1d442b
+int fuse_lowlevel_notify_inval_entry(struct fuse_session *se, fuse_ino_t parent,
1d442b
+				     const char *name, size_t namelen)
1d442b
+{
1d442b
+	struct fuse_notify_inval_entry_out outarg;
1d442b
+	struct iovec iov[3];
1d442b
+
1d442b
+	if (!se)
1d442b
+		return -EINVAL;
1d442b
+	
1d442b
+	if (se->conn.proto_major < 6 || se->conn.proto_minor < 12)
1d442b
+		return -ENOSYS;
1d442b
+
1d442b
+	outarg.parent = parent;
1d442b
+	outarg.namelen = namelen;
1d442b
+	outarg.padding = 0;
1d442b
+
1d442b
+	iov[1].iov_base = &outarg;
1d442b
+	iov[1].iov_len = sizeof(outarg);
1d442b
+	iov[2].iov_base = (void *)name;
1d442b
+	iov[2].iov_len = namelen + 1;
1d442b
+
1d442b
+	return send_notify_iov(se, FUSE_NOTIFY_INVAL_ENTRY, iov, 3);
1d442b
+}
1d442b
+
1d442b
+int fuse_lowlevel_notify_delete(struct fuse_session *se,
1d442b
+				fuse_ino_t parent, fuse_ino_t child,
1d442b
+				const char *name, size_t namelen)
1d442b
+{
1d442b
+	struct fuse_notify_delete_out outarg;
1d442b
+	struct iovec iov[3];
1d442b
+
1d442b
+	if (!se)
1d442b
+		return -EINVAL;
1d442b
+
1d442b
+	if (se->conn.proto_major < 6 || se->conn.proto_minor < 18)
1d442b
+		return -ENOSYS;
1d442b
+
1d442b
+	outarg.parent = parent;
1d442b
+	outarg.child = child;
1d442b
+	outarg.namelen = namelen;
1d442b
+	outarg.padding = 0;
1d442b
+
1d442b
+	iov[1].iov_base = &outarg;
1d442b
+	iov[1].iov_len = sizeof(outarg);
1d442b
+	iov[2].iov_base = (void *)name;
1d442b
+	iov[2].iov_len = namelen + 1;
1d442b
+
1d442b
+	return send_notify_iov(se, FUSE_NOTIFY_DELETE, iov, 3);
1d442b
+}
1d442b
+
1d442b
+int fuse_lowlevel_notify_store(struct fuse_session *se, fuse_ino_t ino,
1d442b
+			       off_t offset, struct fuse_bufvec *bufv,
1d442b
+			       enum fuse_buf_copy_flags flags)
1d442b
+{
1d442b
+	struct fuse_out_header out;
1d442b
+	struct fuse_notify_store_out outarg;
1d442b
+	struct iovec iov[3];
1d442b
+	size_t size = fuse_buf_size(bufv);
1d442b
+	int res;
1d442b
+
1d442b
+	if (!se)
1d442b
+		return -EINVAL;
1d442b
+
1d442b
+	if (se->conn.proto_major < 6 || se->conn.proto_minor < 15)
1d442b
+		return -ENOSYS;
1d442b
+
1d442b
+	out.unique = 0;
1d442b
+	out.error = FUSE_NOTIFY_STORE;
1d442b
+
1d442b
+	outarg.nodeid = ino;
1d442b
+	outarg.offset = offset;
1d442b
+	outarg.size = size;
1d442b
+	outarg.padding = 0;
1d442b
+
1d442b
+	iov[0].iov_base = &out;
1d442b
+	iov[0].iov_len = sizeof(out);
1d442b
+	iov[1].iov_base = &outarg;
1d442b
+	iov[1].iov_len = sizeof(outarg);
1d442b
+
1d442b
+	res = fuse_send_data_iov(se, NULL, iov, 2, bufv, flags);
1d442b
+	if (res > 0)
1d442b
+		res = -res;
1d442b
+
1d442b
+	return res;
1d442b
+}
1d442b
+
1d442b
+struct fuse_retrieve_req {
1d442b
+	struct fuse_notify_req nreq;
1d442b
+	void *cookie;
1d442b
+};
1d442b
+
1d442b
+static void fuse_ll_retrieve_reply(struct fuse_notify_req *nreq,
1d442b
+				   fuse_req_t req, fuse_ino_t ino,
1d442b
+				   const void *inarg,
1d442b
+				   const struct fuse_buf *ibuf)
1d442b
+{
1d442b
+	struct fuse_session *se = req->se;
1d442b
+	struct fuse_retrieve_req *rreq =
1d442b
+		container_of(nreq, struct fuse_retrieve_req, nreq);
1d442b
+	const struct fuse_notify_retrieve_in *arg = inarg;
1d442b
+	struct fuse_bufvec bufv = {
1d442b
+		.buf[0] = *ibuf,
1d442b
+		.count = 1,
1d442b
+	};
1d442b
+
1d442b
+	if (!(bufv.buf[0].flags & FUSE_BUF_IS_FD))
1d442b
+		bufv.buf[0].mem = PARAM(arg);
1d442b
+
1d442b
+	bufv.buf[0].size -= sizeof(struct fuse_in_header) +
1d442b
+		sizeof(struct fuse_notify_retrieve_in);
1d442b
+
1d442b
+	if (bufv.buf[0].size < arg->size) {
1d442b
+		fuse_log(FUSE_LOG_ERR, "fuse: retrieve reply: buffer size too small\n");
1d442b
+		fuse_reply_none(req);
1d442b
+		goto out;
1d442b
+	}
1d442b
+	bufv.buf[0].size = arg->size;
1d442b
+
1d442b
+	if (se->op.retrieve_reply) {
1d442b
+		se->op.retrieve_reply(req, rreq->cookie, ino,
1d442b
+					  arg->offset, &bufv);
1d442b
+	} else {
1d442b
+		fuse_reply_none(req);
1d442b
+	}
1d442b
+out:
1d442b
+	free(rreq);
1d442b
+	if ((ibuf->flags & FUSE_BUF_IS_FD) && bufv.idx < bufv.count)
1d442b
+		fuse_ll_clear_pipe(se);
1d442b
+}
1d442b
+
1d442b
+int fuse_lowlevel_notify_retrieve(struct fuse_session *se, fuse_ino_t ino,
1d442b
+				  size_t size, off_t offset, void *cookie)
1d442b
+{
1d442b
+	struct fuse_notify_retrieve_out outarg;
1d442b
+	struct iovec iov[2];
1d442b
+	struct fuse_retrieve_req *rreq;
1d442b
+	int err;
1d442b
+
1d442b
+	if (!se)
1d442b
+		return -EINVAL;
1d442b
+
1d442b
+	if (se->conn.proto_major < 6 || se->conn.proto_minor < 15)
1d442b
+		return -ENOSYS;
1d442b
+
1d442b
+	rreq = malloc(sizeof(*rreq));
1d442b
+	if (rreq == NULL)
1d442b
+		return -ENOMEM;
1d442b
+
1d442b
+	pthread_mutex_lock(&se->lock);
1d442b
+	rreq->cookie = cookie;
1d442b
+	rreq->nreq.unique = se->notify_ctr++;
1d442b
+	rreq->nreq.reply = fuse_ll_retrieve_reply;
1d442b
+	list_add_nreq(&rreq->nreq, &se->notify_list);
1d442b
+	pthread_mutex_unlock(&se->lock);
1d442b
+
1d442b
+	outarg.notify_unique = rreq->nreq.unique;
1d442b
+	outarg.nodeid = ino;
1d442b
+	outarg.offset = offset;
1d442b
+	outarg.size = size;
1d442b
+	outarg.padding = 0;
1d442b
+
1d442b
+	iov[1].iov_base = &outarg;
1d442b
+	iov[1].iov_len = sizeof(outarg);
1d442b
+
1d442b
+	err = send_notify_iov(se, FUSE_NOTIFY_RETRIEVE, iov, 2);
1d442b
+	if (err) {
1d442b
+		pthread_mutex_lock(&se->lock);
1d442b
+		list_del_nreq(&rreq->nreq);
1d442b
+		pthread_mutex_unlock(&se->lock);
1d442b
+		free(rreq);
1d442b
+	}
1d442b
+
1d442b
+	return err;
1d442b
+}
1d442b
+
1d442b
+void *fuse_req_userdata(fuse_req_t req)
1d442b
+{
1d442b
+	return req->se->userdata;
1d442b
+}
1d442b
+
1d442b
+const struct fuse_ctx *fuse_req_ctx(fuse_req_t req)
1d442b
+{
1d442b
+	return &req->ctx;
1d442b
+}
1d442b
+
1d442b
+void fuse_req_interrupt_func(fuse_req_t req, fuse_interrupt_func_t func,
1d442b
+			     void *data)
1d442b
+{
1d442b
+	pthread_mutex_lock(&req->lock);
1d442b
+	pthread_mutex_lock(&req->se->lock);
1d442b
+	req->u.ni.func = func;
1d442b
+	req->u.ni.data = data;
1d442b
+	pthread_mutex_unlock(&req->se->lock);
1d442b
+	if (req->interrupted && func)
1d442b
+		func(req, data);
1d442b
+	pthread_mutex_unlock(&req->lock);
1d442b
+}
1d442b
+
1d442b
+int fuse_req_interrupted(fuse_req_t req)
1d442b
+{
1d442b
+	int interrupted;
1d442b
+
1d442b
+	pthread_mutex_lock(&req->se->lock);
1d442b
+	interrupted = req->interrupted;
1d442b
+	pthread_mutex_unlock(&req->se->lock);
1d442b
+
1d442b
+	return interrupted;
1d442b
+}
1d442b
+
1d442b
+static struct {
1d442b
+	void (*func)(fuse_req_t, fuse_ino_t, const void *);
1d442b
+	const char *name;
1d442b
+} fuse_ll_ops[] = {
1d442b
+	[FUSE_LOOKUP]	   = { do_lookup,      "LOOKUP"	     },
1d442b
+	[FUSE_FORGET]	   = { do_forget,      "FORGET"	     },
1d442b
+	[FUSE_GETATTR]	   = { do_getattr,     "GETATTR"     },
1d442b
+	[FUSE_SETATTR]	   = { do_setattr,     "SETATTR"     },
1d442b
+	[FUSE_READLINK]	   = { do_readlink,    "READLINK"    },
1d442b
+	[FUSE_SYMLINK]	   = { do_symlink,     "SYMLINK"     },
1d442b
+	[FUSE_MKNOD]	   = { do_mknod,       "MKNOD"	     },
1d442b
+	[FUSE_MKDIR]	   = { do_mkdir,       "MKDIR"	     },
1d442b
+	[FUSE_UNLINK]	   = { do_unlink,      "UNLINK"	     },
1d442b
+	[FUSE_RMDIR]	   = { do_rmdir,       "RMDIR"	     },
1d442b
+	[FUSE_RENAME]	   = { do_rename,      "RENAME"	     },
1d442b
+	[FUSE_LINK]	   = { do_link,	       "LINK"	     },
1d442b
+	[FUSE_OPEN]	   = { do_open,	       "OPEN"	     },
1d442b
+	[FUSE_READ]	   = { do_read,	       "READ"	     },
1d442b
+	[FUSE_WRITE]	   = { do_write,       "WRITE"	     },
1d442b
+	[FUSE_STATFS]	   = { do_statfs,      "STATFS"	     },
1d442b
+	[FUSE_RELEASE]	   = { do_release,     "RELEASE"     },
1d442b
+	[FUSE_FSYNC]	   = { do_fsync,       "FSYNC"	     },
1d442b
+	[FUSE_SETXATTR]	   = { do_setxattr,    "SETXATTR"    },
1d442b
+	[FUSE_GETXATTR]	   = { do_getxattr,    "GETXATTR"    },
1d442b
+	[FUSE_LISTXATTR]   = { do_listxattr,   "LISTXATTR"   },
1d442b
+	[FUSE_REMOVEXATTR] = { do_removexattr, "REMOVEXATTR" },
1d442b
+	[FUSE_FLUSH]	   = { do_flush,       "FLUSH"	     },
1d442b
+	[FUSE_INIT]	   = { do_init,	       "INIT"	     },
1d442b
+	[FUSE_OPENDIR]	   = { do_opendir,     "OPENDIR"     },
1d442b
+	[FUSE_READDIR]	   = { do_readdir,     "READDIR"     },
1d442b
+	[FUSE_RELEASEDIR]  = { do_releasedir,  "RELEASEDIR"  },
1d442b
+	[FUSE_FSYNCDIR]	   = { do_fsyncdir,    "FSYNCDIR"    },
1d442b
+	[FUSE_GETLK]	   = { do_getlk,       "GETLK"	     },
1d442b
+	[FUSE_SETLK]	   = { do_setlk,       "SETLK"	     },
1d442b
+	[FUSE_SETLKW]	   = { do_setlkw,      "SETLKW"	     },
1d442b
+	[FUSE_ACCESS]	   = { do_access,      "ACCESS"	     },
1d442b
+	[FUSE_CREATE]	   = { do_create,      "CREATE"	     },
1d442b
+	[FUSE_INTERRUPT]   = { do_interrupt,   "INTERRUPT"   },
1d442b
+	[FUSE_BMAP]	   = { do_bmap,	       "BMAP"	     },
1d442b
+	[FUSE_IOCTL]	   = { do_ioctl,       "IOCTL"	     },
1d442b
+	[FUSE_POLL]	   = { do_poll,        "POLL"	     },
1d442b
+	[FUSE_FALLOCATE]   = { do_fallocate,   "FALLOCATE"   },
1d442b
+	[FUSE_DESTROY]	   = { do_destroy,     "DESTROY"     },
1d442b
+	[FUSE_NOTIFY_REPLY] = { (void *) 1,    "NOTIFY_REPLY" },
1d442b
+	[FUSE_BATCH_FORGET] = { do_batch_forget, "BATCH_FORGET" },
1d442b
+	[FUSE_READDIRPLUS] = { do_readdirplus,	"READDIRPLUS"},
1d442b
+	[FUSE_RENAME2]     = { do_rename2,      "RENAME2"    },
1d442b
+	[FUSE_COPY_FILE_RANGE] = { do_copy_file_range, "COPY_FILE_RANGE" },
1d442b
+	[FUSE_LSEEK]	   = { do_lseek,       "LSEEK"	     },
1d442b
+	[CUSE_INIT]	   = { cuse_lowlevel_init, "CUSE_INIT"   },
1d442b
+};
1d442b
+
1d442b
+#define FUSE_MAXOP (sizeof(fuse_ll_ops) / sizeof(fuse_ll_ops[0]))
1d442b
+
1d442b
+static const char *opname(enum fuse_opcode opcode)
1d442b
+{
1d442b
+	if (opcode >= FUSE_MAXOP || !fuse_ll_ops[opcode].name)
1d442b
+		return "???";
1d442b
+	else
1d442b
+		return fuse_ll_ops[opcode].name;
1d442b
+}
1d442b
+
1d442b
+static int fuse_ll_copy_from_pipe(struct fuse_bufvec *dst,
1d442b
+				  struct fuse_bufvec *src)
1d442b
+{
1d442b
+	ssize_t res = fuse_buf_copy(dst, src, 0);
1d442b
+	if (res < 0) {
1d442b
+		fuse_log(FUSE_LOG_ERR, "fuse: copy from pipe: %s\n", strerror(-res));
1d442b
+		return res;
1d442b
+	}
1d442b
+	if ((size_t)res < fuse_buf_size(dst)) {
1d442b
+		fuse_log(FUSE_LOG_ERR, "fuse: copy from pipe: short read\n");
1d442b
+		return -1;
1d442b
+	}
1d442b
+	return 0;
1d442b
+}
1d442b
+
1d442b
+void fuse_session_process_buf(struct fuse_session *se,
1d442b
+			      const struct fuse_buf *buf)
1d442b
+{
1d442b
+	fuse_session_process_buf_int(se, buf, NULL);
1d442b
+}
1d442b
+
1d442b
+void fuse_session_process_buf_int(struct fuse_session *se,
1d442b
+				  const struct fuse_buf *buf, struct fuse_chan *ch)
1d442b
+{
1d442b
+	const size_t write_header_size = sizeof(struct fuse_in_header) +
1d442b
+		sizeof(struct fuse_write_in);
1d442b
+	struct fuse_bufvec bufv = { .buf[0] = *buf, .count = 1 };
1d442b
+	struct fuse_bufvec tmpbuf = FUSE_BUFVEC_INIT(write_header_size);
1d442b
+	struct fuse_in_header *in;
1d442b
+	const void *inarg;
1d442b
+	struct fuse_req *req;
1d442b
+	void *mbuf = NULL;
1d442b
+	int err;
1d442b
+	int res;
1d442b
+
1d442b
+	if (buf->flags & FUSE_BUF_IS_FD) {
1d442b
+		if (buf->size < tmpbuf.buf[0].size)
1d442b
+			tmpbuf.buf[0].size = buf->size;
1d442b
+
1d442b
+		mbuf = malloc(tmpbuf.buf[0].size);
1d442b
+		if (mbuf == NULL) {
1d442b
+			fuse_log(FUSE_LOG_ERR, "fuse: failed to allocate header\n");
1d442b
+			goto clear_pipe;
1d442b
+		}
1d442b
+		tmpbuf.buf[0].mem = mbuf;
1d442b
+
1d442b
+		res = fuse_ll_copy_from_pipe(&tmpbuf, &bufv);
1d442b
+		if (res < 0)
1d442b
+			goto clear_pipe;
1d442b
+
1d442b
+		in = mbuf;
1d442b
+	} else {
1d442b
+		in = buf->mem;
1d442b
+	}
1d442b
+
1d442b
+	if (se->debug) {
1d442b
+		fuse_log(FUSE_LOG_DEBUG,
1d442b
+			"unique: %llu, opcode: %s (%i), nodeid: %llu, insize: %zu, pid: %u\n",
1d442b
+			(unsigned long long) in->unique,
1d442b
+			opname((enum fuse_opcode) in->opcode), in->opcode,
1d442b
+			(unsigned long long) in->nodeid, buf->size, in->pid);
1d442b
+	}
1d442b
+
1d442b
+	req = fuse_ll_alloc_req(se);
1d442b
+	if (req == NULL) {
1d442b
+		struct fuse_out_header out = {
1d442b
+			.unique = in->unique,
1d442b
+			.error = -ENOMEM,
1d442b
+		};
1d442b
+		struct iovec iov = {
1d442b
+			.iov_base = &out,
1d442b
+			.iov_len = sizeof(struct fuse_out_header),
1d442b
+		};
1d442b
+
1d442b
+		fuse_send_msg(se, ch, &iov, 1);
1d442b
+		goto clear_pipe;
1d442b
+	}
1d442b
+
1d442b
+	req->unique = in->unique;
1d442b
+	req->ctx.uid = in->uid;
1d442b
+	req->ctx.gid = in->gid;
1d442b
+	req->ctx.pid = in->pid;
1d442b
+	req->ch = ch ? fuse_chan_get(ch) : NULL;
1d442b
+
1d442b
+	err = EIO;
1d442b
+	if (!se->got_init) {
1d442b
+		enum fuse_opcode expected;
1d442b
+
1d442b
+		expected = se->cuse_data ? CUSE_INIT : FUSE_INIT;
1d442b
+		if (in->opcode != expected)
1d442b
+			goto reply_err;
1d442b
+	} else if (in->opcode == FUSE_INIT || in->opcode == CUSE_INIT)
1d442b
+		goto reply_err;
1d442b
+
1d442b
+	err = EACCES;
1d442b
+	/* Implement -o allow_root */
1d442b
+	if (se->deny_others && in->uid != se->owner && in->uid != 0 &&
1d442b
+		 in->opcode != FUSE_INIT && in->opcode != FUSE_READ &&
1d442b
+		 in->opcode != FUSE_WRITE && in->opcode != FUSE_FSYNC &&
1d442b
+		 in->opcode != FUSE_RELEASE && in->opcode != FUSE_READDIR &&
1d442b
+		 in->opcode != FUSE_FSYNCDIR && in->opcode != FUSE_RELEASEDIR &&
1d442b
+		 in->opcode != FUSE_NOTIFY_REPLY &&
1d442b
+		 in->opcode != FUSE_READDIRPLUS)
1d442b
+		goto reply_err;
1d442b
+
1d442b
+	err = ENOSYS;
1d442b
+	if (in->opcode >= FUSE_MAXOP || !fuse_ll_ops[in->opcode].func)
1d442b
+		goto reply_err;
1d442b
+	if (in->opcode != FUSE_INTERRUPT) {
1d442b
+		struct fuse_req *intr;
1d442b
+		pthread_mutex_lock(&se->lock);
1d442b
+		intr = check_interrupt(se, req);
1d442b
+		list_add_req(req, &se->list);
1d442b
+		pthread_mutex_unlock(&se->lock);
1d442b
+		if (intr)
1d442b
+			fuse_reply_err(intr, EAGAIN);
1d442b
+	}
1d442b
+
1d442b
+	if ((buf->flags & FUSE_BUF_IS_FD) && write_header_size < buf->size &&
1d442b
+	    (in->opcode != FUSE_WRITE || !se->op.write_buf) &&
1d442b
+	    in->opcode != FUSE_NOTIFY_REPLY) {
1d442b
+		void *newmbuf;
1d442b
+
1d442b
+		err = ENOMEM;
1d442b
+		newmbuf = realloc(mbuf, buf->size);
1d442b
+		if (newmbuf == NULL)
1d442b
+			goto reply_err;
1d442b
+		mbuf = newmbuf;
1d442b
+
1d442b
+		tmpbuf = FUSE_BUFVEC_INIT(buf->size - write_header_size);
1d442b
+		tmpbuf.buf[0].mem = (char *)mbuf + write_header_size;
1d442b
+
1d442b
+		res = fuse_ll_copy_from_pipe(&tmpbuf, &bufv);
1d442b
+		err = -res;
1d442b
+		if (res < 0)
1d442b
+			goto reply_err;
1d442b
+
1d442b
+		in = mbuf;
1d442b
+	}
1d442b
+
1d442b
+	inarg = (void *) &in[1];
1d442b
+	if (in->opcode == FUSE_WRITE && se->op.write_buf)
1d442b
+		do_write_buf(req, in->nodeid, inarg, buf);
1d442b
+	else if (in->opcode == FUSE_NOTIFY_REPLY)
1d442b
+		do_notify_reply(req, in->nodeid, inarg, buf);
1d442b
+	else
1d442b
+		fuse_ll_ops[in->opcode].func(req, in->nodeid, inarg);
1d442b
+
1d442b
+out_free:
1d442b
+	free(mbuf);
1d442b
+	return;
1d442b
+
1d442b
+reply_err:
1d442b
+	fuse_reply_err(req, err);
1d442b
+clear_pipe:
1d442b
+	if (buf->flags & FUSE_BUF_IS_FD)
1d442b
+		fuse_ll_clear_pipe(se);
1d442b
+	goto out_free;
1d442b
+}
1d442b
+
1d442b
+#define LL_OPTION(n,o,v) \
1d442b
+	{ n, offsetof(struct fuse_session, o), v }
1d442b
+
1d442b
+static const struct fuse_opt fuse_ll_opts[] = {
1d442b
+	LL_OPTION("debug", debug, 1),
1d442b
+	LL_OPTION("-d", debug, 1),
1d442b
+	LL_OPTION("--debug", debug, 1),
1d442b
+	LL_OPTION("allow_root", deny_others, 1),
1d442b
+	FUSE_OPT_END
1d442b
+};
1d442b
+
1d442b
+void fuse_lowlevel_version(void)
1d442b
+{
1d442b
+	printf("using FUSE kernel interface version %i.%i\n",
1d442b
+	       FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
1d442b
+	fuse_mount_version();
1d442b
+}
1d442b
+
1d442b
+void fuse_lowlevel_help(void)
1d442b
+{
1d442b
+	/* These are not all options, but the ones that are
1d442b
+	   potentially of interest to an end-user */
1d442b
+	printf(
1d442b
+"    -o allow_other         allow access by all users\n"
1d442b
+"    -o allow_root          allow access by root\n"
1d442b
+"    -o auto_unmount        auto unmount on process termination\n");
1d442b
+}
1d442b
+
1d442b
+void fuse_session_destroy(struct fuse_session *se)
1d442b
+{
1d442b
+	struct fuse_ll_pipe *llp;
1d442b
+
1d442b
+	if (se->got_init && !se->got_destroy) {
1d442b
+		if (se->op.destroy)
1d442b
+			se->op.destroy(se->userdata);
1d442b
+	}
1d442b
+	llp = pthread_getspecific(se->pipe_key);
1d442b
+	if (llp != NULL)
1d442b
+		fuse_ll_pipe_free(llp);
1d442b
+	pthread_key_delete(se->pipe_key);
1d442b
+	pthread_mutex_destroy(&se->lock);
1d442b
+	free(se->cuse_data);
1d442b
+	if (se->fd != -1)
1d442b
+		close(se->fd);
1d442b
+	destroy_mount_opts(se->mo);
1d442b
+	free(se);
1d442b
+}
1d442b
+
1d442b
+
1d442b
+static void fuse_ll_pipe_destructor(void *data)
1d442b
+{
1d442b
+	struct fuse_ll_pipe *llp = data;
1d442b
+	fuse_ll_pipe_free(llp);
1d442b
+}
1d442b
+
1d442b
+int fuse_session_receive_buf(struct fuse_session *se, struct fuse_buf *buf)
1d442b
+{
1d442b
+	return fuse_session_receive_buf_int(se, buf, NULL);
1d442b
+}
1d442b
+
1d442b
+int fuse_session_receive_buf_int(struct fuse_session *se, struct fuse_buf *buf,
1d442b
+				 struct fuse_chan *ch)
1d442b
+{
1d442b
+	int err;
1d442b
+	ssize_t res;
1d442b
+#ifdef HAVE_SPLICE
1d442b
+	size_t bufsize = se->bufsize;
1d442b
+	struct fuse_ll_pipe *llp;
1d442b
+	struct fuse_buf tmpbuf;
1d442b
+
1d442b
+	if (se->conn.proto_minor < 14 || !(se->conn.want & FUSE_CAP_SPLICE_READ))
1d442b
+		goto fallback;
1d442b
+
1d442b
+	llp = fuse_ll_get_pipe(se);
1d442b
+	if (llp == NULL)
1d442b
+		goto fallback;
1d442b
+
1d442b
+	if (llp->size < bufsize) {
1d442b
+		if (llp->can_grow) {
1d442b
+			res = fcntl(llp->pipe[0], F_SETPIPE_SZ, bufsize);
1d442b
+			if (res == -1) {
1d442b
+				llp->can_grow = 0;
1d442b
+				res = grow_pipe_to_max(llp->pipe[0]);
1d442b
+				if (res > 0)
1d442b
+					llp->size = res;
1d442b
+				goto fallback;
1d442b
+			}
1d442b
+			llp->size = res;
1d442b
+		}
1d442b
+		if (llp->size < bufsize)
1d442b
+			goto fallback;
1d442b
+	}
1d442b
+
1d442b
+	res = splice(ch ? ch->fd : se->fd,
1d442b
+		     NULL, llp->pipe[1], NULL, bufsize, 0);
1d442b
+	err = errno;
1d442b
+
1d442b
+	if (fuse_session_exited(se))
1d442b
+		return 0;
1d442b
+
1d442b
+	if (res == -1) {
1d442b
+		if (err == ENODEV) {
1d442b
+			/* Filesystem was unmounted, or connection was aborted
1d442b
+			   via /sys/fs/fuse/connections */
1d442b
+			fuse_session_exit(se);
1d442b
+			return 0;
1d442b
+		}
1d442b
+		if (err != EINTR && err != EAGAIN)
1d442b
+			perror("fuse: splice from device");
1d442b
+		return -err;
1d442b
+	}
1d442b
+
1d442b
+	if (res < sizeof(struct fuse_in_header)) {
1d442b
+		fuse_log(FUSE_LOG_ERR, "short splice from fuse device\n");
1d442b
+		return -EIO;
1d442b
+	}
1d442b
+
1d442b
+	tmpbuf = (struct fuse_buf) {
1d442b
+		.size = res,
1d442b
+		.flags = FUSE_BUF_IS_FD,
1d442b
+		.fd = llp->pipe[0],
1d442b
+	};
1d442b
+
1d442b
+	/*
1d442b
+	 * Don't bother with zero copy for small requests.
1d442b
+	 * fuse_loop_mt() needs to check for FORGET so this more than
1d442b
+	 * just an optimization.
1d442b
+	 */
1d442b
+	if (res < sizeof(struct fuse_in_header) +
1d442b
+	    sizeof(struct fuse_write_in) + pagesize) {
1d442b
+		struct fuse_bufvec src = { .buf[0] = tmpbuf, .count = 1 };
1d442b
+		struct fuse_bufvec dst = { .count = 1 };
1d442b
+
1d442b
+		if (!buf->mem) {
1d442b
+			buf->mem = malloc(se->bufsize);
1d442b
+			if (!buf->mem) {
1d442b
+				fuse_log(FUSE_LOG_ERR,
1d442b
+					"fuse: failed to allocate read buffer\n");
1d442b
+				return -ENOMEM;
1d442b
+			}
1d442b
+		}
1d442b
+		buf->size = se->bufsize;
1d442b
+		buf->flags = 0;
1d442b
+		dst.buf[0] = *buf;
1d442b
+
1d442b
+		res = fuse_buf_copy(&dst, &src, 0);
1d442b
+		if (res < 0) {
1d442b
+			fuse_log(FUSE_LOG_ERR, "fuse: copy from pipe: %s\n",
1d442b
+				strerror(-res));
1d442b
+			fuse_ll_clear_pipe(se);
1d442b
+			return res;
1d442b
+		}
1d442b
+		if (res < tmpbuf.size) {
1d442b
+			fuse_log(FUSE_LOG_ERR, "fuse: copy from pipe: short read\n");
1d442b
+			fuse_ll_clear_pipe(se);
1d442b
+			return -EIO;
1d442b
+		}
1d442b
+		assert(res == tmpbuf.size);
1d442b
+
1d442b
+	} else {
1d442b
+		/* Don't overwrite buf->mem, as that would cause a leak */
1d442b
+		buf->fd = tmpbuf.fd;
1d442b
+		buf->flags = tmpbuf.flags;
1d442b
+	}
1d442b
+	buf->size = tmpbuf.size;
1d442b
+
1d442b
+	return res;
1d442b
+
1d442b
+fallback:
1d442b
+#endif
1d442b
+	if (!buf->mem) {
1d442b
+		buf->mem = malloc(se->bufsize);
1d442b
+		if (!buf->mem) {
1d442b
+			fuse_log(FUSE_LOG_ERR,
1d442b
+				"fuse: failed to allocate read buffer\n");
1d442b
+			return -ENOMEM;
1d442b
+		}
1d442b
+	}
1d442b
+
1d442b
+restart:
1d442b
+	res = read(ch ? ch->fd : se->fd, buf->mem, se->bufsize);
1d442b
+	err = errno;
1d442b
+
1d442b
+	if (fuse_session_exited(se))
1d442b
+		return 0;
1d442b
+	if (res == -1) {
1d442b
+		/* ENOENT means the operation was interrupted, it's safe
1d442b
+		   to restart */
1d442b
+		if (err == ENOENT)
1d442b
+			goto restart;
1d442b
+
1d442b
+		if (err == ENODEV) {
1d442b
+			/* Filesystem was unmounted, or connection was aborted
1d442b
+			   via /sys/fs/fuse/connections */
1d442b
+			fuse_session_exit(se);
1d442b
+			return 0;
1d442b
+		}
1d442b
+		/* Errors occurring during normal operation: EINTR (read
1d442b
+		   interrupted), EAGAIN (nonblocking I/O), ENODEV (filesystem
1d442b
+		   umounted) */
1d442b
+		if (err != EINTR && err != EAGAIN)
1d442b
+			perror("fuse: reading device");
1d442b
+		return -err;
1d442b
+	}
1d442b
+	if ((size_t) res < sizeof(struct fuse_in_header)) {
1d442b
+		fuse_log(FUSE_LOG_ERR, "short read on fuse device\n");
1d442b
+		return -EIO;
1d442b
+	}
1d442b
+
1d442b
+	buf->size = res;
1d442b
+
1d442b
+	return res;
1d442b
+}
1d442b
+
1d442b
+struct fuse_session *fuse_session_new(struct fuse_args *args,
1d442b
+				      const struct fuse_lowlevel_ops *op,
1d442b
+				      size_t op_size, void *userdata)
1d442b
+{
1d442b
+	int err;
1d442b
+	struct fuse_session *se;
1d442b
+	struct mount_opts *mo;
1d442b
+
1d442b
+	if (sizeof(struct fuse_lowlevel_ops) < op_size) {
1d442b
+		fuse_log(FUSE_LOG_ERR, "fuse: warning: library too old, some operations may not work\n");
1d442b
+		op_size = sizeof(struct fuse_lowlevel_ops);
1d442b
+	}
1d442b
+
1d442b
+	if (args->argc == 0) {
1d442b
+		fuse_log(FUSE_LOG_ERR, "fuse: empty argv passed to fuse_session_new().\n");
1d442b
+		return NULL;
1d442b
+	}
1d442b
+
1d442b
+	se = (struct fuse_session *) calloc(1, sizeof(struct fuse_session));
1d442b
+	if (se == NULL) {
1d442b
+		fuse_log(FUSE_LOG_ERR, "fuse: failed to allocate fuse object\n");
1d442b
+		goto out1;
1d442b
+	}
1d442b
+	se->fd = -1;
1d442b
+	se->conn.max_write = UINT_MAX;
1d442b
+	se->conn.max_readahead = UINT_MAX;
1d442b
+
1d442b
+	/* Parse options */
1d442b
+	if(fuse_opt_parse(args, se, fuse_ll_opts, NULL) == -1)
1d442b
+		goto out2;
1d442b
+	if(se->deny_others) {
1d442b
+		/* Allowing access only by root is done by instructing
1d442b
+		 * kernel to allow access by everyone, and then restricting
1d442b
+		 * access to root and mountpoint owner in libfuse.
1d442b
+		 */
1d442b
+		// We may be adding the option a second time, but
1d442b
+		// that doesn't hurt.
1d442b
+		if(fuse_opt_add_arg(args, "-oallow_other") == -1)
1d442b
+			goto out2;
1d442b
+	}
1d442b
+	mo = parse_mount_opts(args);
1d442b
+	if (mo == NULL)
1d442b
+		goto out3;
1d442b
+
1d442b
+	if(args->argc == 1 &&
1d442b
+	   args->argv[0][0] == '-') {
1d442b
+		fuse_log(FUSE_LOG_ERR, "fuse: warning: argv[0] looks like an option, but "
1d442b
+			"will be ignored\n");
1d442b
+	} else if (args->argc != 1) {
1d442b
+		int i;
1d442b
+		fuse_log(FUSE_LOG_ERR, "fuse: unknown option(s): `");
1d442b
+		for(i = 1; i < args->argc-1; i++)
1d442b
+			fuse_log(FUSE_LOG_ERR, "%s ", args->argv[i]);
1d442b
+		fuse_log(FUSE_LOG_ERR, "%s'\n", args->argv[i]);
1d442b
+		goto out4;
1d442b
+	}
1d442b
+
1d442b
+	if (se->debug)
1d442b
+		fuse_log(FUSE_LOG_DEBUG, "FUSE library version: %s\n", PACKAGE_VERSION);
1d442b
+
1d442b
+	se->bufsize = FUSE_MAX_MAX_PAGES * getpagesize() +
1d442b
+		FUSE_BUFFER_HEADER_SIZE;
1d442b
+
1d442b
+	list_init_req(&se->list);
1d442b
+	list_init_req(&se->interrupts);
1d442b
+	list_init_nreq(&se->notify_list);
1d442b
+	se->notify_ctr = 1;
1d442b
+	fuse_mutex_init(&se->lock);
1d442b
+
1d442b
+	err = pthread_key_create(&se->pipe_key, fuse_ll_pipe_destructor);
1d442b
+	if (err) {
1d442b
+		fuse_log(FUSE_LOG_ERR, "fuse: failed to create thread specific key: %s\n",
1d442b
+			strerror(err));
1d442b
+		goto out5;
1d442b
+	}
1d442b
+
1d442b
+	memcpy(&se->op, op, op_size);
1d442b
+	se->owner = getuid();
1d442b
+	se->userdata = userdata;
1d442b
+
1d442b
+	se->mo = mo;
1d442b
+	return se;
1d442b
+
1d442b
+out5:
1d442b
+	pthread_mutex_destroy(&se->lock);
1d442b
+out4:
1d442b
+	fuse_opt_free_args(args);
1d442b
+out3:
1d442b
+	free(mo);
1d442b
+out2:
1d442b
+	free(se);
1d442b
+out1:
1d442b
+	return NULL;
1d442b
+}
1d442b
+
1d442b
+int fuse_session_mount(struct fuse_session *se, const char *mountpoint)
1d442b
+{
1d442b
+	int fd;
1d442b
+
1d442b
+	/*
1d442b
+	 * Make sure file descriptors 0, 1 and 2 are open, otherwise chaos
1d442b
+	 * would ensue.
1d442b
+	 */
1d442b
+	do {
1d442b
+		fd = open("/dev/null", O_RDWR);
1d442b
+		if (fd > 2)
1d442b
+			close(fd);
1d442b
+	} while (fd >= 0 && fd <= 2);
1d442b
+
1d442b
+	/*
1d442b
+	 * To allow FUSE daemons to run without privileges, the caller may open
1d442b
+	 * /dev/fuse before launching the file system and pass on the file
1d442b
+	 * descriptor by specifying /dev/fd/N as the mount point. Note that the
1d442b
+	 * parent process takes care of performing the mount in this case.
1d442b
+	 */
1d442b
+	fd = fuse_mnt_parse_fuse_fd(mountpoint);
1d442b
+	if (fd != -1) {
1d442b
+		if (fcntl(fd, F_GETFD) == -1) {
1d442b
+			fuse_log(FUSE_LOG_ERR,
1d442b
+				"fuse: Invalid file descriptor /dev/fd/%u\n",
1d442b
+				fd);
1d442b
+			return -1;
1d442b
+		}
1d442b
+		se->fd = fd;
1d442b
+		return 0;
1d442b
+	}
1d442b
+
1d442b
+	/* Open channel */
1d442b
+	fd = fuse_kern_mount(mountpoint, se->mo);
1d442b
+	if (fd == -1)
1d442b
+		return -1;
1d442b
+	se->fd = fd;
1d442b
+
1d442b
+	/* Save mountpoint */
1d442b
+	se->mountpoint = strdup(mountpoint);
1d442b
+	if (se->mountpoint == NULL)
1d442b
+		goto error_out;
1d442b
+
1d442b
+	return 0;
1d442b
+
1d442b
+error_out:
1d442b
+	fuse_kern_unmount(mountpoint, fd);
1d442b
+	return -1;
1d442b
+}
1d442b
+
1d442b
+int fuse_session_fd(struct fuse_session *se)
1d442b
+{
1d442b
+	return se->fd;
1d442b
+}
1d442b
+
1d442b
+void fuse_session_unmount(struct fuse_session *se)
1d442b
+{
1d442b
+	if (se->mountpoint != NULL) {
1d442b
+		fuse_kern_unmount(se->mountpoint, se->fd);
1d442b
+		free(se->mountpoint);
1d442b
+		se->mountpoint = NULL;
1d442b
+	}
1d442b
+}
1d442b
+
1d442b
+#ifdef linux
1d442b
+int fuse_req_getgroups(fuse_req_t req, int size, gid_t list[])
1d442b
+{
1d442b
+	char *buf;
1d442b
+	size_t bufsize = 1024;
1d442b
+	char path[128];
1d442b
+	int ret;
1d442b
+	int fd;
1d442b
+	unsigned long pid = req->ctx.pid;
1d442b
+	char *s;
1d442b
+
1d442b
+	sprintf(path, "/proc/%lu/task/%lu/status", pid, pid);
1d442b
+
1d442b
+retry:
1d442b
+	buf = malloc(bufsize);
1d442b
+	if (buf == NULL)
1d442b
+		return -ENOMEM;
1d442b
+
1d442b
+	ret = -EIO;
1d442b
+	fd = open(path, O_RDONLY);
1d442b
+	if (fd == -1)
1d442b
+		goto out_free;
1d442b
+
1d442b
+	ret = read(fd, buf, bufsize);
1d442b
+	close(fd);
1d442b
+	if (ret < 0) {
1d442b
+		ret = -EIO;
1d442b
+		goto out_free;
1d442b
+	}
1d442b
+
1d442b
+	if ((size_t)ret == bufsize) {
1d442b
+		free(buf);
1d442b
+		bufsize *= 4;
1d442b
+		goto retry;
1d442b
+	}
1d442b
+
1d442b
+	ret = -EIO;
1d442b
+	s = strstr(buf, "\nGroups:");
1d442b
+	if (s == NULL)
1d442b
+		goto out_free;
1d442b
+
1d442b
+	s += 8;
1d442b
+	ret = 0;
1d442b
+	while (1) {
1d442b
+		char *end;
1d442b
+		unsigned long val = strtoul(s, &end, 0);
1d442b
+		if (end == s)
1d442b
+			break;
1d442b
+
1d442b
+		s = end;
1d442b
+		if (ret < size)
1d442b
+			list[ret] = val;
1d442b
+		ret++;
1d442b
+	}
1d442b
+
1d442b
+out_free:
1d442b
+	free(buf);
1d442b
+	return ret;
1d442b
+}
1d442b
+#else /* linux */
1d442b
+/*
1d442b
+ * This is currently not implemented on other than Linux...
1d442b
+ */
1d442b
+int fuse_req_getgroups(fuse_req_t req, int size, gid_t list[])
1d442b
+{
1d442b
+	(void) req; (void) size; (void) list;
1d442b
+	return -ENOSYS;
1d442b
+}
1d442b
+#endif
1d442b
+
1d442b
+void fuse_session_exit(struct fuse_session *se)
1d442b
+{
1d442b
+	se->exited = 1;
1d442b
+}
1d442b
+
1d442b
+void fuse_session_reset(struct fuse_session *se)
1d442b
+{
1d442b
+	se->exited = 0;
1d442b
+	se->error = 0;
1d442b
+}
1d442b
+
1d442b
+int fuse_session_exited(struct fuse_session *se)
1d442b
+{
1d442b
+	return se->exited;
1d442b
+}