cryptospore / rpms / qemu-kvm

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