yeahuh / rpms / qemu-kvm

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