902636
From 18ef831cac81a6bd2336c73dda357d9d69f8fd25 Mon Sep 17 00:00:00 2001
902636
From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
902636
Date: Mon, 27 Jan 2020 19:00:43 +0100
902636
Subject: [PATCH 012/116] virtiofsd: Add passthrough_ll
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-9-dgilbert@redhat.com>
902636
Patchwork-id: 93462
902636
O-Subject: [RHEL-AV-8.2 qemu-kvm PATCH 008/112] virtiofsd: Add passthrough_ll
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
passthrough_ll is one of the examples in the upstream fuse project
902636
and is the main part of our daemon here.  It passes through requests
902636
from fuse to the underlying filesystem, using syscalls as directly
902636
as possible.
902636
902636
>From libfuse fuse-3.8.0
902636
902636
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
902636
  Fixed up 'GPL' to 'GPLv2' as per Dan's comments and consistent
902636
  with the 'LICENSE' file in libfuse;  patch sent to libfuse to fix
902636
  it upstream.
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 7c6b66027241f41720240fc6ee1021cdbd975b2e)
902636
902636
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
902636
---
902636
 tools/virtiofsd/passthrough_ll.c | 1338 ++++++++++++++++++++++++++++++++++++++
902636
 1 file changed, 1338 insertions(+)
902636
 create mode 100644 tools/virtiofsd/passthrough_ll.c
902636
902636
diff --git a/tools/virtiofsd/passthrough_ll.c b/tools/virtiofsd/passthrough_ll.c
902636
new file mode 100644
902636
index 0000000..e1a6056
902636
--- /dev/null
902636
+++ b/tools/virtiofsd/passthrough_ll.c
902636
@@ -0,0 +1,1338 @@
902636
+/*
902636
+  FUSE: Filesystem in Userspace
902636
+  Copyright (C) 2001-2007  Miklos Szeredi <miklos@szeredi.hu>
902636
+
902636
+  This program can be distributed under the terms of the GNU GPLv2.
902636
+  See the file COPYING.
902636
+*/
902636
+
902636
+/** @file
902636
+ *
902636
+ * This file system mirrors the existing file system hierarchy of the
902636
+ * system, starting at the root file system. This is implemented by
902636
+ * just "passing through" all requests to the corresponding user-space
902636
+ * libc functions. In contrast to passthrough.c and passthrough_fh.c,
902636
+ * this implementation uses the low-level API. Its performance should
902636
+ * be the least bad among the three, but many operations are not
902636
+ * implemented. In particular, it is not possible to remove files (or
902636
+ * directories) because the code necessary to defer actual removal
902636
+ * until the file is not opened anymore would make the example much
902636
+ * more complicated.
902636
+ *
902636
+ * When writeback caching is enabled (-o writeback mount option), it
902636
+ * is only possible to write to files for which the mounting user has
902636
+ * read permissions. This is because the writeback cache requires the
902636
+ * kernel to be able to issue read requests for all files (which the
902636
+ * passthrough filesystem cannot satisfy if it can't read the file in
902636
+ * the underlying filesystem).
902636
+ *
902636
+ * Compile with:
902636
+ *
902636
+ *     gcc -Wall passthrough_ll.c `pkg-config fuse3 --cflags --libs` -o passthrough_ll
902636
+ *
902636
+ * ## Source code ##
902636
+ * \include passthrough_ll.c
902636
+ */
902636
+
902636
+#define _GNU_SOURCE
902636
+#define FUSE_USE_VERSION 31
902636
+
902636
+#include "config.h"
902636
+
902636
+#include <fuse_lowlevel.h>
902636
+#include <unistd.h>
902636
+#include <stdlib.h>
902636
+#include <stdio.h>
902636
+#include <stddef.h>
902636
+#include <stdbool.h>
902636
+#include <string.h>
902636
+#include <limits.h>
902636
+#include <dirent.h>
902636
+#include <assert.h>
902636
+#include <errno.h>
902636
+#include <inttypes.h>
902636
+#include <pthread.h>
902636
+#include <sys/file.h>
902636
+#include <sys/xattr.h>
902636
+
902636
+#include "passthrough_helpers.h"
902636
+
902636
+/* We are re-using pointers to our `struct lo_inode` and `struct
902636
+   lo_dirp` elements as inodes. This means that we must be able to
902636
+   store uintptr_t values in a fuse_ino_t variable. The following
902636
+   incantation checks this condition at compile time. */
902636
+#if defined(__GNUC__) && (__GNUC__ > 4 || __GNUC__ == 4 && __GNUC_MINOR__ >= 6) && !defined __cplusplus
902636
+_Static_assert(sizeof(fuse_ino_t) >= sizeof(uintptr_t),
902636
+	       "fuse_ino_t too small to hold uintptr_t values!");
902636
+#else
902636
+struct _uintptr_to_must_hold_fuse_ino_t_dummy_struct \
902636
+	{ unsigned _uintptr_to_must_hold_fuse_ino_t:
902636
+			((sizeof(fuse_ino_t) >= sizeof(uintptr_t)) ? 1 : -1); };
902636
+#endif
902636
+
902636
+struct lo_inode {
902636
+	struct lo_inode *next; /* protected by lo->mutex */
902636
+	struct lo_inode *prev; /* protected by lo->mutex */
902636
+	int fd;
902636
+	bool is_symlink;
902636
+	ino_t ino;
902636
+	dev_t dev;
902636
+	uint64_t refcount; /* protected by lo->mutex */
902636
+};
902636
+
902636
+enum {
902636
+	CACHE_NEVER,
902636
+	CACHE_NORMAL,
902636
+	CACHE_ALWAYS,
902636
+};
902636
+
902636
+struct lo_data {
902636
+	pthread_mutex_t mutex;
902636
+	int debug;
902636
+	int writeback;
902636
+	int flock;
902636
+	int xattr;
902636
+	const char *source;
902636
+	double timeout;
902636
+	int cache;
902636
+	int timeout_set;
902636
+	struct lo_inode root; /* protected by lo->mutex */
902636
+};
902636
+
902636
+static const struct fuse_opt lo_opts[] = {
902636
+	{ "writeback",
902636
+	  offsetof(struct lo_data, writeback), 1 },
902636
+	{ "no_writeback",
902636
+	  offsetof(struct lo_data, writeback), 0 },
902636
+	{ "source=%s",
902636
+	  offsetof(struct lo_data, source), 0 },
902636
+	{ "flock",
902636
+	  offsetof(struct lo_data, flock), 1 },
902636
+	{ "no_flock",
902636
+	  offsetof(struct lo_data, flock), 0 },
902636
+	{ "xattr",
902636
+	  offsetof(struct lo_data, xattr), 1 },
902636
+	{ "no_xattr",
902636
+	  offsetof(struct lo_data, xattr), 0 },
902636
+	{ "timeout=%lf",
902636
+	  offsetof(struct lo_data, timeout), 0 },
902636
+	{ "timeout=",
902636
+	  offsetof(struct lo_data, timeout_set), 1 },
902636
+	{ "cache=never",
902636
+	  offsetof(struct lo_data, cache), CACHE_NEVER },
902636
+	{ "cache=auto",
902636
+	  offsetof(struct lo_data, cache), CACHE_NORMAL },
902636
+	{ "cache=always",
902636
+	  offsetof(struct lo_data, cache), CACHE_ALWAYS },
902636
+
902636
+	FUSE_OPT_END
902636
+};
902636
+
902636
+static struct lo_data *lo_data(fuse_req_t req)
902636
+{
902636
+	return (struct lo_data *) fuse_req_userdata(req);
902636
+}
902636
+
902636
+static struct lo_inode *lo_inode(fuse_req_t req, fuse_ino_t ino)
902636
+{
902636
+	if (ino == FUSE_ROOT_ID)
902636
+		return &lo_data(req)->root;
902636
+	else
902636
+		return (struct lo_inode *) (uintptr_t) ino;
902636
+}
902636
+
902636
+static int lo_fd(fuse_req_t req, fuse_ino_t ino)
902636
+{
902636
+	return lo_inode(req, ino)->fd;
902636
+}
902636
+
902636
+static bool lo_debug(fuse_req_t req)
902636
+{
902636
+	return lo_data(req)->debug != 0;
902636
+}
902636
+
902636
+static void lo_init(void *userdata,
902636
+		    struct fuse_conn_info *conn)
902636
+{
902636
+	struct lo_data *lo = (struct lo_data*) userdata;
902636
+
902636
+	if(conn->capable & FUSE_CAP_EXPORT_SUPPORT)
902636
+		conn->want |= FUSE_CAP_EXPORT_SUPPORT;
902636
+
902636
+	if (lo->writeback &&
902636
+	    conn->capable & FUSE_CAP_WRITEBACK_CACHE) {
902636
+		if (lo->debug)
902636
+			fuse_log(FUSE_LOG_DEBUG, "lo_init: activating writeback\n");
902636
+		conn->want |= FUSE_CAP_WRITEBACK_CACHE;
902636
+	}
902636
+	if (lo->flock && conn->capable & FUSE_CAP_FLOCK_LOCKS) {
902636
+		if (lo->debug)
902636
+			fuse_log(FUSE_LOG_DEBUG, "lo_init: activating flock locks\n");
902636
+		conn->want |= FUSE_CAP_FLOCK_LOCKS;
902636
+	}
902636
+}
902636
+
902636
+static void lo_getattr(fuse_req_t req, fuse_ino_t ino,
902636
+			     struct fuse_file_info *fi)
902636
+{
902636
+	int res;
902636
+	struct stat buf;
902636
+	struct lo_data *lo = lo_data(req);
902636
+
902636
+	(void) fi;
902636
+
902636
+	res = fstatat(lo_fd(req, ino), "", &buf, AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW);
902636
+	if (res == -1)
902636
+		return (void) fuse_reply_err(req, errno);
902636
+
902636
+	fuse_reply_attr(req, &buf, lo->timeout);
902636
+}
902636
+
902636
+static int utimensat_empty_nofollow(struct lo_inode *inode,
902636
+				    const struct timespec *tv)
902636
+{
902636
+	int res;
902636
+	char procname[64];
902636
+
902636
+	if (inode->is_symlink) {
902636
+		res = utimensat(inode->fd, "", tv,
902636
+				AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW);
902636
+		if (res == -1 && errno == EINVAL) {
902636
+			/* Sorry, no race free way to set times on symlink. */
902636
+			errno = EPERM;
902636
+		}
902636
+		return res;
902636
+	}
902636
+	sprintf(procname, "/proc/self/fd/%i", inode->fd);
902636
+
902636
+	return utimensat(AT_FDCWD, procname, tv, 0);
902636
+}
902636
+
902636
+static void lo_setattr(fuse_req_t req, fuse_ino_t ino, struct stat *attr,
902636
+		       int valid, struct fuse_file_info *fi)
902636
+{
902636
+	int saverr;
902636
+	char procname[64];
902636
+	struct lo_inode *inode = lo_inode(req, ino);
902636
+	int ifd = inode->fd;
902636
+	int res;
902636
+
902636
+	if (valid & FUSE_SET_ATTR_MODE) {
902636
+		if (fi) {
902636
+			res = fchmod(fi->fh, attr->st_mode);
902636
+		} else {
902636
+			sprintf(procname, "/proc/self/fd/%i", ifd);
902636
+			res = chmod(procname, attr->st_mode);
902636
+		}
902636
+		if (res == -1)
902636
+			goto out_err;
902636
+	}
902636
+	if (valid & (FUSE_SET_ATTR_UID | FUSE_SET_ATTR_GID)) {
902636
+		uid_t uid = (valid & FUSE_SET_ATTR_UID) ?
902636
+			attr->st_uid : (uid_t) -1;
902636
+		gid_t gid = (valid & FUSE_SET_ATTR_GID) ?
902636
+			attr->st_gid : (gid_t) -1;
902636
+
902636
+		res = fchownat(ifd, "", uid, gid,
902636
+			       AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW);
902636
+		if (res == -1)
902636
+			goto out_err;
902636
+	}
902636
+	if (valid & FUSE_SET_ATTR_SIZE) {
902636
+		if (fi) {
902636
+			res = ftruncate(fi->fh, attr->st_size);
902636
+		} else {
902636
+			sprintf(procname, "/proc/self/fd/%i", ifd);
902636
+			res = truncate(procname, attr->st_size);
902636
+		}
902636
+		if (res == -1)
902636
+			goto out_err;
902636
+	}
902636
+	if (valid & (FUSE_SET_ATTR_ATIME | FUSE_SET_ATTR_MTIME)) {
902636
+		struct timespec tv[2];
902636
+
902636
+		tv[0].tv_sec = 0;
902636
+		tv[1].tv_sec = 0;
902636
+		tv[0].tv_nsec = UTIME_OMIT;
902636
+		tv[1].tv_nsec = UTIME_OMIT;
902636
+
902636
+		if (valid & FUSE_SET_ATTR_ATIME_NOW)
902636
+			tv[0].tv_nsec = UTIME_NOW;
902636
+		else if (valid & FUSE_SET_ATTR_ATIME)
902636
+			tv[0] = attr->st_atim;
902636
+
902636
+		if (valid & FUSE_SET_ATTR_MTIME_NOW)
902636
+			tv[1].tv_nsec = UTIME_NOW;
902636
+		else if (valid & FUSE_SET_ATTR_MTIME)
902636
+			tv[1] = attr->st_mtim;
902636
+
902636
+		if (fi)
902636
+			res = futimens(fi->fh, tv);
902636
+		else
902636
+			res = utimensat_empty_nofollow(inode, tv);
902636
+		if (res == -1)
902636
+			goto out_err;
902636
+	}
902636
+
902636
+	return lo_getattr(req, ino, fi);
902636
+
902636
+out_err:
902636
+	saverr = errno;
902636
+	fuse_reply_err(req, saverr);
902636
+}
902636
+
902636
+static struct lo_inode *lo_find(struct lo_data *lo, struct stat *st)
902636
+{
902636
+	struct lo_inode *p;
902636
+	struct lo_inode *ret = NULL;
902636
+
902636
+	pthread_mutex_lock(&lo->mutex);
902636
+	for (p = lo->root.next; p != &lo->root; p = p->next) {
902636
+		if (p->ino == st->st_ino && p->dev == st->st_dev) {
902636
+			assert(p->refcount > 0);
902636
+			ret = p;
902636
+			ret->refcount++;
902636
+			break;
902636
+		}
902636
+	}
902636
+	pthread_mutex_unlock(&lo->mutex);
902636
+	return ret;
902636
+}
902636
+
902636
+static int lo_do_lookup(fuse_req_t req, fuse_ino_t parent, const char *name,
902636
+			 struct fuse_entry_param *e)
902636
+{
902636
+	int newfd;
902636
+	int res;
902636
+	int saverr;
902636
+	struct lo_data *lo = lo_data(req);
902636
+	struct lo_inode *inode;
902636
+
902636
+	memset(e, 0, sizeof(*e));
902636
+	e->attr_timeout = lo->timeout;
902636
+	e->entry_timeout = lo->timeout;
902636
+
902636
+	newfd = openat(lo_fd(req, parent), name, O_PATH | O_NOFOLLOW);
902636
+	if (newfd == -1)
902636
+		goto out_err;
902636
+
902636
+	res = fstatat(newfd, "", &e->attr, AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW);
902636
+	if (res == -1)
902636
+		goto out_err;
902636
+
902636
+	inode = lo_find(lo_data(req), &e->attr);
902636
+	if (inode) {
902636
+		close(newfd);
902636
+		newfd = -1;
902636
+	} else {
902636
+		struct lo_inode *prev, *next;
902636
+
902636
+		saverr = ENOMEM;
902636
+		inode = calloc(1, sizeof(struct lo_inode));
902636
+		if (!inode)
902636
+			goto out_err;
902636
+
902636
+		inode->is_symlink = S_ISLNK(e->attr.st_mode);
902636
+		inode->refcount = 1;
902636
+		inode->fd = newfd;
902636
+		inode->ino = e->attr.st_ino;
902636
+		inode->dev = e->attr.st_dev;
902636
+
902636
+		pthread_mutex_lock(&lo->mutex);
902636
+		prev = &lo->root;
902636
+		next = prev->next;
902636
+		next->prev = inode;
902636
+		inode->next = next;
902636
+		inode->prev = prev;
902636
+		prev->next = inode;
902636
+		pthread_mutex_unlock(&lo->mutex);
902636
+	}
902636
+	e->ino = (uintptr_t) inode;
902636
+
902636
+	if (lo_debug(req))
902636
+		fuse_log(FUSE_LOG_DEBUG, "  %lli/%s -> %lli\n",
902636
+			(unsigned long long) parent, name, (unsigned long long) e->ino);
902636
+
902636
+	return 0;
902636
+
902636
+out_err:
902636
+	saverr = errno;
902636
+	if (newfd != -1)
902636
+		close(newfd);
902636
+	return saverr;
902636
+}
902636
+
902636
+static void lo_lookup(fuse_req_t req, fuse_ino_t parent, const char *name)
902636
+{
902636
+	struct fuse_entry_param e;
902636
+	int err;
902636
+
902636
+	if (lo_debug(req))
902636
+		fuse_log(FUSE_LOG_DEBUG, "lo_lookup(parent=%" PRIu64 ", name=%s)\n",
902636
+			parent, name);
902636
+
902636
+	err = lo_do_lookup(req, parent, name, &e);
902636
+	if (err)
902636
+		fuse_reply_err(req, err);
902636
+	else
902636
+		fuse_reply_entry(req, &e);
902636
+}
902636
+
902636
+static void lo_mknod_symlink(fuse_req_t req, fuse_ino_t parent,
902636
+			     const char *name, mode_t mode, dev_t rdev,
902636
+			     const char *link)
902636
+{
902636
+	int res;
902636
+	int saverr;
902636
+	struct lo_inode *dir = lo_inode(req, parent);
902636
+	struct fuse_entry_param e;
902636
+
902636
+	saverr = ENOMEM;
902636
+
902636
+	res = mknod_wrapper(dir->fd, name, link, mode, rdev);
902636
+
902636
+	saverr = errno;
902636
+	if (res == -1)
902636
+		goto out;
902636
+
902636
+	saverr = lo_do_lookup(req, parent, name, &e);
902636
+	if (saverr)
902636
+		goto out;
902636
+
902636
+	if (lo_debug(req))
902636
+		fuse_log(FUSE_LOG_DEBUG, "  %lli/%s -> %lli\n",
902636
+			(unsigned long long) parent, name, (unsigned long long) e.ino);
902636
+
902636
+	fuse_reply_entry(req, &e);
902636
+	return;
902636
+
902636
+out:
902636
+	fuse_reply_err(req, saverr);
902636
+}
902636
+
902636
+static void lo_mknod(fuse_req_t req, fuse_ino_t parent,
902636
+		     const char *name, mode_t mode, dev_t rdev)
902636
+{
902636
+	lo_mknod_symlink(req, parent, name, mode, rdev, NULL);
902636
+}
902636
+
902636
+static void lo_mkdir(fuse_req_t req, fuse_ino_t parent, const char *name,
902636
+		     mode_t mode)
902636
+{
902636
+	lo_mknod_symlink(req, parent, name, S_IFDIR | mode, 0, NULL);
902636
+}
902636
+
902636
+static void lo_symlink(fuse_req_t req, const char *link,
902636
+		       fuse_ino_t parent, const char *name)
902636
+{
902636
+	lo_mknod_symlink(req, parent, name, S_IFLNK, 0, link);
902636
+}
902636
+
902636
+static int linkat_empty_nofollow(struct lo_inode *inode, int dfd,
902636
+				 const char *name)
902636
+{
902636
+	int res;
902636
+	char procname[64];
902636
+
902636
+	if (inode->is_symlink) {
902636
+		res = linkat(inode->fd, "", dfd, name, AT_EMPTY_PATH);
902636
+		if (res == -1 && (errno == ENOENT || errno == EINVAL)) {
902636
+			/* Sorry, no race free way to hard-link a symlink. */
902636
+			errno = EPERM;
902636
+		}
902636
+		return res;
902636
+	}
902636
+
902636
+	sprintf(procname, "/proc/self/fd/%i", inode->fd);
902636
+
902636
+	return linkat(AT_FDCWD, procname, dfd, name, AT_SYMLINK_FOLLOW);
902636
+}
902636
+
902636
+static void lo_link(fuse_req_t req, fuse_ino_t ino, fuse_ino_t parent,
902636
+		    const char *name)
902636
+{
902636
+	int res;
902636
+	struct lo_data *lo = lo_data(req);
902636
+	struct lo_inode *inode = lo_inode(req, ino);
902636
+	struct fuse_entry_param e;
902636
+	int saverr;
902636
+
902636
+	memset(&e, 0, sizeof(struct fuse_entry_param));
902636
+	e.attr_timeout = lo->timeout;
902636
+	e.entry_timeout = lo->timeout;
902636
+
902636
+	res = linkat_empty_nofollow(inode, lo_fd(req, parent), name);
902636
+	if (res == -1)
902636
+		goto out_err;
902636
+
902636
+	res = fstatat(inode->fd, "", &e.attr, AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW);
902636
+	if (res == -1)
902636
+		goto out_err;
902636
+
902636
+	pthread_mutex_lock(&lo->mutex);
902636
+	inode->refcount++;
902636
+	pthread_mutex_unlock(&lo->mutex);
902636
+	e.ino = (uintptr_t) inode;
902636
+
902636
+	if (lo_debug(req))
902636
+		fuse_log(FUSE_LOG_DEBUG, "  %lli/%s -> %lli\n",
902636
+			(unsigned long long) parent, name,
902636
+			(unsigned long long) e.ino);
902636
+
902636
+	fuse_reply_entry(req, &e);
902636
+	return;
902636
+
902636
+out_err:
902636
+	saverr = errno;
902636
+	fuse_reply_err(req, saverr);
902636
+}
902636
+
902636
+static void lo_rmdir(fuse_req_t req, fuse_ino_t parent, const char *name)
902636
+{
902636
+	int res;
902636
+
902636
+	res = unlinkat(lo_fd(req, parent), name, AT_REMOVEDIR);
902636
+
902636
+	fuse_reply_err(req, res == -1 ? errno : 0);
902636
+}
902636
+
902636
+static void lo_rename(fuse_req_t req, fuse_ino_t parent, const char *name,
902636
+		      fuse_ino_t newparent, const char *newname,
902636
+		      unsigned int flags)
902636
+{
902636
+	int res;
902636
+
902636
+	if (flags) {
902636
+		fuse_reply_err(req, EINVAL);
902636
+		return;
902636
+	}
902636
+
902636
+	res = renameat(lo_fd(req, parent), name,
902636
+			lo_fd(req, newparent), newname);
902636
+
902636
+	fuse_reply_err(req, res == -1 ? errno : 0);
902636
+}
902636
+
902636
+static void lo_unlink(fuse_req_t req, fuse_ino_t parent, const char *name)
902636
+{
902636
+	int res;
902636
+
902636
+	res = unlinkat(lo_fd(req, parent), name, 0);
902636
+
902636
+	fuse_reply_err(req, res == -1 ? errno : 0);
902636
+}
902636
+
902636
+static void unref_inode(struct lo_data *lo, struct lo_inode *inode, uint64_t n)
902636
+{
902636
+	if (!inode)
902636
+		return;
902636
+
902636
+	pthread_mutex_lock(&lo->mutex);
902636
+	assert(inode->refcount >= n);
902636
+	inode->refcount -= n;
902636
+	if (!inode->refcount) {
902636
+		struct lo_inode *prev, *next;
902636
+
902636
+		prev = inode->prev;
902636
+		next = inode->next;
902636
+		next->prev = prev;
902636
+		prev->next = next;
902636
+
902636
+		pthread_mutex_unlock(&lo->mutex);
902636
+		close(inode->fd);
902636
+		free(inode);
902636
+
902636
+	} else {
902636
+		pthread_mutex_unlock(&lo->mutex);
902636
+	}
902636
+}
902636
+
902636
+static void lo_forget_one(fuse_req_t req, fuse_ino_t ino, uint64_t nlookup)
902636
+{
902636
+	struct lo_data *lo = lo_data(req);
902636
+	struct lo_inode *inode = lo_inode(req, ino);
902636
+
902636
+	if (lo_debug(req)) {
902636
+		fuse_log(FUSE_LOG_DEBUG, "  forget %lli %lli -%lli\n",
902636
+			(unsigned long long) ino,
902636
+			(unsigned long long) inode->refcount,
902636
+			(unsigned long long) nlookup);
902636
+	}
902636
+
902636
+	unref_inode(lo, inode, nlookup);
902636
+}
902636
+
902636
+static void lo_forget(fuse_req_t req, fuse_ino_t ino, uint64_t nlookup)
902636
+{
902636
+	lo_forget_one(req, ino, nlookup);
902636
+	fuse_reply_none(req);
902636
+}
902636
+
902636
+static void lo_forget_multi(fuse_req_t req, size_t count,
902636
+				struct fuse_forget_data *forgets)
902636
+{
902636
+	int i;
902636
+
902636
+	for (i = 0; i < count; i++)
902636
+		lo_forget_one(req, forgets[i].ino, forgets[i].nlookup);
902636
+	fuse_reply_none(req);
902636
+}
902636
+
902636
+static void lo_readlink(fuse_req_t req, fuse_ino_t ino)
902636
+{
902636
+	char buf[PATH_MAX + 1];
902636
+	int res;
902636
+
902636
+	res = readlinkat(lo_fd(req, ino), "", buf, sizeof(buf));
902636
+	if (res == -1)
902636
+		return (void) fuse_reply_err(req, errno);
902636
+
902636
+	if (res == sizeof(buf))
902636
+		return (void) fuse_reply_err(req, ENAMETOOLONG);
902636
+
902636
+	buf[res] = '\0';
902636
+
902636
+	fuse_reply_readlink(req, buf);
902636
+}
902636
+
902636
+struct lo_dirp {
902636
+	DIR *dp;
902636
+	struct dirent *entry;
902636
+	off_t offset;
902636
+};
902636
+
902636
+static struct lo_dirp *lo_dirp(struct fuse_file_info *fi)
902636
+{
902636
+	return (struct lo_dirp *) (uintptr_t) fi->fh;
902636
+}
902636
+
902636
+static void lo_opendir(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi)
902636
+{
902636
+	int error = ENOMEM;
902636
+	struct lo_data *lo = lo_data(req);
902636
+	struct lo_dirp *d;
902636
+	int fd;
902636
+
902636
+	d = calloc(1, sizeof(struct lo_dirp));
902636
+	if (d == NULL)
902636
+		goto out_err;
902636
+
902636
+	fd = openat(lo_fd(req, ino), ".", O_RDONLY);
902636
+	if (fd == -1)
902636
+		goto out_errno;
902636
+
902636
+	d->dp = fdopendir(fd);
902636
+	if (d->dp == NULL)
902636
+		goto out_errno;
902636
+
902636
+	d->offset = 0;
902636
+	d->entry = NULL;
902636
+
902636
+	fi->fh = (uintptr_t) d;
902636
+	if (lo->cache == CACHE_ALWAYS)
902636
+		fi->keep_cache = 1;
902636
+	fuse_reply_open(req, fi);
902636
+	return;
902636
+
902636
+out_errno:
902636
+	error = errno;
902636
+out_err:
902636
+	if (d) {
902636
+		if (fd != -1)
902636
+			close(fd);
902636
+		free(d);
902636
+	}
902636
+	fuse_reply_err(req, error);
902636
+}
902636
+
902636
+static int is_dot_or_dotdot(const char *name)
902636
+{
902636
+	return name[0] == '.' && (name[1] == '\0' ||
902636
+				  (name[1] == '.' && name[2] == '\0'));
902636
+}
902636
+
902636
+static void lo_do_readdir(fuse_req_t req, fuse_ino_t ino, size_t size,
902636
+			  off_t offset, struct fuse_file_info *fi, int plus)
902636
+{
902636
+	struct lo_dirp *d = lo_dirp(fi);
902636
+	char *buf;
902636
+	char *p;
902636
+	size_t rem = size;
902636
+	int err;
902636
+
902636
+	(void) ino;
902636
+
902636
+	buf = calloc(1, size);
902636
+	if (!buf) {
902636
+		err = ENOMEM;
902636
+		goto error;
902636
+	}
902636
+	p = buf;
902636
+
902636
+	if (offset != d->offset) {
902636
+		seekdir(d->dp, offset);
902636
+		d->entry = NULL;
902636
+		d->offset = offset;
902636
+	}
902636
+	while (1) {
902636
+		size_t entsize;
902636
+		off_t nextoff;
902636
+		const char *name;
902636
+
902636
+		if (!d->entry) {
902636
+			errno = 0;
902636
+			d->entry = readdir(d->dp);
902636
+			if (!d->entry) {
902636
+				if (errno) {  // Error
902636
+					err = errno;
902636
+					goto error;
902636
+				} else {  // End of stream
902636
+					break; 
902636
+				}
902636
+			}
902636
+		}
902636
+		nextoff = d->entry->d_off;
902636
+		name = d->entry->d_name;
902636
+		fuse_ino_t entry_ino = 0;
902636
+		if (plus) {
902636
+			struct fuse_entry_param e;
902636
+			if (is_dot_or_dotdot(name)) {
902636
+				e = (struct fuse_entry_param) {
902636
+					.attr.st_ino = d->entry->d_ino,
902636
+					.attr.st_mode = d->entry->d_type << 12,
902636
+				};
902636
+			} else {
902636
+				err = lo_do_lookup(req, ino, name, &e);
902636
+				if (err)
902636
+					goto error;
902636
+				entry_ino = e.ino;
902636
+			}
902636
+
902636
+			entsize = fuse_add_direntry_plus(req, p, rem, name,
902636
+							 &e, nextoff);
902636
+		} else {
902636
+			struct stat st = {
902636
+				.st_ino = d->entry->d_ino,
902636
+				.st_mode = d->entry->d_type << 12,
902636
+			};
902636
+			entsize = fuse_add_direntry(req, p, rem, name,
902636
+						    &st, nextoff);
902636
+		}
902636
+		if (entsize > rem) {
902636
+			if (entry_ino != 0) 
902636
+				lo_forget_one(req, entry_ino, 1);
902636
+			break;
902636
+		}
902636
+		
902636
+		p += entsize;
902636
+		rem -= entsize;
902636
+
902636
+		d->entry = NULL;
902636
+		d->offset = nextoff;
902636
+	}
902636
+
902636
+    err = 0;
902636
+error:
902636
+    // If there's an error, we can only signal it if we haven't stored
902636
+    // any entries yet - otherwise we'd end up with wrong lookup
902636
+    // counts for the entries that are already in the buffer. So we
902636
+    // return what we've collected until that point.
902636
+    if (err && rem == size)
902636
+	    fuse_reply_err(req, err);
902636
+    else
902636
+	    fuse_reply_buf(req, buf, size - rem);
902636
+    free(buf);
902636
+}
902636
+
902636
+static void lo_readdir(fuse_req_t req, fuse_ino_t ino, size_t size,
902636
+		       off_t offset, struct fuse_file_info *fi)
902636
+{
902636
+	lo_do_readdir(req, ino, size, offset, fi, 0);
902636
+}
902636
+
902636
+static void lo_readdirplus(fuse_req_t req, fuse_ino_t ino, size_t size,
902636
+			   off_t offset, struct fuse_file_info *fi)
902636
+{
902636
+	lo_do_readdir(req, ino, size, offset, fi, 1);
902636
+}
902636
+
902636
+static void lo_releasedir(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi)
902636
+{
902636
+	struct lo_dirp *d = lo_dirp(fi);
902636
+	(void) ino;
902636
+	closedir(d->dp);
902636
+	free(d);
902636
+	fuse_reply_err(req, 0);
902636
+}
902636
+
902636
+static void lo_create(fuse_req_t req, fuse_ino_t parent, const char *name,
902636
+		      mode_t mode, struct fuse_file_info *fi)
902636
+{
902636
+	int fd;
902636
+	struct lo_data *lo = lo_data(req);
902636
+	struct fuse_entry_param e;
902636
+	int err;
902636
+
902636
+	if (lo_debug(req))
902636
+		fuse_log(FUSE_LOG_DEBUG, "lo_create(parent=%" PRIu64 ", name=%s)\n",
902636
+			parent, name);
902636
+
902636
+	fd = openat(lo_fd(req, parent), name,
902636
+		    (fi->flags | O_CREAT) & ~O_NOFOLLOW, mode);
902636
+	if (fd == -1)
902636
+		return (void) fuse_reply_err(req, errno);
902636
+
902636
+	fi->fh = fd;
902636
+	if (lo->cache == CACHE_NEVER)
902636
+		fi->direct_io = 1;
902636
+	else if (lo->cache == CACHE_ALWAYS)
902636
+		fi->keep_cache = 1;
902636
+
902636
+	err = lo_do_lookup(req, parent, name, &e);
902636
+	if (err)
902636
+		fuse_reply_err(req, err);
902636
+	else
902636
+		fuse_reply_create(req, &e, fi);
902636
+}
902636
+
902636
+static void lo_fsyncdir(fuse_req_t req, fuse_ino_t ino, int datasync,
902636
+			struct fuse_file_info *fi)
902636
+{
902636
+	int res;
902636
+	int fd = dirfd(lo_dirp(fi)->dp);
902636
+	(void) ino;
902636
+	if (datasync)
902636
+		res = fdatasync(fd);
902636
+	else
902636
+		res = fsync(fd);
902636
+	fuse_reply_err(req, res == -1 ? errno : 0);
902636
+}
902636
+
902636
+static void lo_open(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi)
902636
+{
902636
+	int fd;
902636
+	char buf[64];
902636
+	struct lo_data *lo = lo_data(req);
902636
+
902636
+	if (lo_debug(req))
902636
+		fuse_log(FUSE_LOG_DEBUG, "lo_open(ino=%" PRIu64 ", flags=%d)\n",
902636
+			ino, fi->flags);
902636
+
902636
+	/* With writeback cache, kernel may send read requests even
902636
+	   when userspace opened write-only */
902636
+	if (lo->writeback && (fi->flags & O_ACCMODE) == O_WRONLY) {
902636
+		fi->flags &= ~O_ACCMODE;
902636
+		fi->flags |= O_RDWR;
902636
+	}
902636
+
902636
+	/* With writeback cache, O_APPEND is handled by the kernel.
902636
+	   This breaks atomicity (since the file may change in the
902636
+	   underlying filesystem, so that the kernel's idea of the
902636
+	   end of the file isn't accurate anymore). In this example,
902636
+	   we just accept that. A more rigorous filesystem may want
902636
+	   to return an error here */
902636
+	if (lo->writeback && (fi->flags & O_APPEND))
902636
+		fi->flags &= ~O_APPEND;
902636
+
902636
+	sprintf(buf, "/proc/self/fd/%i", lo_fd(req, ino));
902636
+	fd = open(buf, fi->flags & ~O_NOFOLLOW);
902636
+	if (fd == -1)
902636
+		return (void) fuse_reply_err(req, errno);
902636
+
902636
+	fi->fh = fd;
902636
+	if (lo->cache == CACHE_NEVER)
902636
+		fi->direct_io = 1;
902636
+	else if (lo->cache == CACHE_ALWAYS)
902636
+		fi->keep_cache = 1;
902636
+	fuse_reply_open(req, fi);
902636
+}
902636
+
902636
+static void lo_release(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi)
902636
+{
902636
+	(void) ino;
902636
+
902636
+	close(fi->fh);
902636
+	fuse_reply_err(req, 0);
902636
+}
902636
+
902636
+static void lo_flush(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi)
902636
+{
902636
+	int res;
902636
+	(void) ino;
902636
+	res = close(dup(fi->fh));
902636
+	fuse_reply_err(req, res == -1 ? errno : 0);
902636
+}
902636
+
902636
+static void lo_fsync(fuse_req_t req, fuse_ino_t ino, int datasync,
902636
+		     struct fuse_file_info *fi)
902636
+{
902636
+	int res;
902636
+	(void) ino;
902636
+	if (datasync)
902636
+		res = fdatasync(fi->fh);
902636
+	else
902636
+		res = fsync(fi->fh);
902636
+	fuse_reply_err(req, res == -1 ? errno : 0);
902636
+}
902636
+
902636
+static void lo_read(fuse_req_t req, fuse_ino_t ino, size_t size,
902636
+		    off_t offset, struct fuse_file_info *fi)
902636
+{
902636
+	struct fuse_bufvec buf = FUSE_BUFVEC_INIT(size);
902636
+
902636
+	if (lo_debug(req))
902636
+		fuse_log(FUSE_LOG_DEBUG, "lo_read(ino=%" PRIu64 ", size=%zd, "
902636
+			"off=%lu)\n", ino, size, (unsigned long) offset);
902636
+
902636
+	buf.buf[0].flags = FUSE_BUF_IS_FD | FUSE_BUF_FD_SEEK;
902636
+	buf.buf[0].fd = fi->fh;
902636
+	buf.buf[0].pos = offset;
902636
+
902636
+	fuse_reply_data(req, &buf, FUSE_BUF_SPLICE_MOVE);
902636
+}
902636
+
902636
+static void lo_write_buf(fuse_req_t req, fuse_ino_t ino,
902636
+			 struct fuse_bufvec *in_buf, off_t off,
902636
+			 struct fuse_file_info *fi)
902636
+{
902636
+	(void) ino;
902636
+	ssize_t res;
902636
+	struct fuse_bufvec out_buf = FUSE_BUFVEC_INIT(fuse_buf_size(in_buf));
902636
+
902636
+	out_buf.buf[0].flags = FUSE_BUF_IS_FD | FUSE_BUF_FD_SEEK;
902636
+	out_buf.buf[0].fd = fi->fh;
902636
+	out_buf.buf[0].pos = off;
902636
+
902636
+	if (lo_debug(req))
902636
+		fuse_log(FUSE_LOG_DEBUG, "lo_write(ino=%" PRIu64 ", size=%zd, off=%lu)\n",
902636
+			ino, out_buf.buf[0].size, (unsigned long) off);
902636
+
902636
+	res = fuse_buf_copy(&out_buf, in_buf, 0);
902636
+	if(res < 0)
902636
+		fuse_reply_err(req, -res);
902636
+	else
902636
+		fuse_reply_write(req, (size_t) res);
902636
+}
902636
+
902636
+static void lo_statfs(fuse_req_t req, fuse_ino_t ino)
902636
+{
902636
+	int res;
902636
+	struct statvfs stbuf;
902636
+
902636
+	res = fstatvfs(lo_fd(req, ino), &stbuf);
902636
+	if (res == -1)
902636
+		fuse_reply_err(req, errno);
902636
+	else
902636
+		fuse_reply_statfs(req, &stbuf);
902636
+}
902636
+
902636
+static void lo_fallocate(fuse_req_t req, fuse_ino_t ino, int mode,
902636
+			 off_t offset, off_t length, struct fuse_file_info *fi)
902636
+{
902636
+	int err = EOPNOTSUPP;
902636
+	(void) ino;
902636
+
902636
+#ifdef HAVE_FALLOCATE
902636
+	err = fallocate(fi->fh, mode, offset, length);
902636
+	if (err < 0)
902636
+		err = errno;
902636
+
902636
+#elif defined(HAVE_POSIX_FALLOCATE)
902636
+	if (mode) {
902636
+		fuse_reply_err(req, EOPNOTSUPP);
902636
+		return;
902636
+	}
902636
+
902636
+	err = posix_fallocate(fi->fh, offset, length);
902636
+#endif
902636
+
902636
+	fuse_reply_err(req, err);
902636
+}
902636
+
902636
+static void lo_flock(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi,
902636
+		     int op)
902636
+{
902636
+	int res;
902636
+	(void) ino;
902636
+
902636
+	res = flock(fi->fh, op);
902636
+
902636
+	fuse_reply_err(req, res == -1 ? errno : 0);
902636
+}
902636
+
902636
+static void lo_getxattr(fuse_req_t req, fuse_ino_t ino, const char *name,
902636
+			size_t size)
902636
+{
902636
+	char *value = NULL;
902636
+	char procname[64];
902636
+	struct lo_inode *inode = lo_inode(req, ino);
902636
+	ssize_t ret;
902636
+	int saverr;
902636
+
902636
+	saverr = ENOSYS;
902636
+	if (!lo_data(req)->xattr)
902636
+		goto out;
902636
+
902636
+	if (lo_debug(req)) {
902636
+		fuse_log(FUSE_LOG_DEBUG, "lo_getxattr(ino=%" PRIu64 ", name=%s size=%zd)\n",
902636
+			ino, name, size);
902636
+	}
902636
+
902636
+	if (inode->is_symlink) {
902636
+		/* Sorry, no race free way to getxattr on symlink. */
902636
+		saverr = EPERM;
902636
+		goto out;
902636
+	}
902636
+
902636
+	sprintf(procname, "/proc/self/fd/%i", inode->fd);
902636
+
902636
+	if (size) {
902636
+		value = malloc(size);
902636
+		if (!value)
902636
+			goto out_err;
902636
+
902636
+		ret = getxattr(procname, name, value, size);
902636
+		if (ret == -1)
902636
+			goto out_err;
902636
+		saverr = 0;
902636
+		if (ret == 0)
902636
+			goto out;
902636
+
902636
+		fuse_reply_buf(req, value, ret);
902636
+	} else {
902636
+		ret = getxattr(procname, name, NULL, 0);
902636
+		if (ret == -1)
902636
+			goto out_err;
902636
+
902636
+		fuse_reply_xattr(req, ret);
902636
+	}
902636
+out_free:
902636
+	free(value);
902636
+	return;
902636
+
902636
+out_err:
902636
+	saverr = errno;
902636
+out:
902636
+	fuse_reply_err(req, saverr);
902636
+	goto out_free;
902636
+}
902636
+
902636
+static void lo_listxattr(fuse_req_t req, fuse_ino_t ino, size_t size)
902636
+{
902636
+	char *value = NULL;
902636
+	char procname[64];
902636
+	struct lo_inode *inode = lo_inode(req, ino);
902636
+	ssize_t ret;
902636
+	int saverr;
902636
+
902636
+	saverr = ENOSYS;
902636
+	if (!lo_data(req)->xattr)
902636
+		goto out;
902636
+
902636
+	if (lo_debug(req)) {
902636
+		fuse_log(FUSE_LOG_DEBUG, "lo_listxattr(ino=%" PRIu64 ", size=%zd)\n",
902636
+			ino, size);
902636
+	}
902636
+
902636
+	if (inode->is_symlink) {
902636
+		/* Sorry, no race free way to listxattr on symlink. */
902636
+		saverr = EPERM;
902636
+		goto out;
902636
+	}
902636
+
902636
+	sprintf(procname, "/proc/self/fd/%i", inode->fd);
902636
+
902636
+	if (size) {
902636
+		value = malloc(size);
902636
+		if (!value)
902636
+			goto out_err;
902636
+
902636
+		ret = listxattr(procname, value, size);
902636
+		if (ret == -1)
902636
+			goto out_err;
902636
+		saverr = 0;
902636
+		if (ret == 0)
902636
+			goto out;
902636
+
902636
+		fuse_reply_buf(req, value, ret);
902636
+	} else {
902636
+		ret = listxattr(procname, NULL, 0);
902636
+		if (ret == -1)
902636
+			goto out_err;
902636
+
902636
+		fuse_reply_xattr(req, ret);
902636
+	}
902636
+out_free:
902636
+	free(value);
902636
+	return;
902636
+
902636
+out_err:
902636
+	saverr = errno;
902636
+out:
902636
+	fuse_reply_err(req, saverr);
902636
+	goto out_free;
902636
+}
902636
+
902636
+static void lo_setxattr(fuse_req_t req, fuse_ino_t ino, const char *name,
902636
+			const char *value, size_t size, int flags)
902636
+{
902636
+	char procname[64];
902636
+	struct lo_inode *inode = lo_inode(req, ino);
902636
+	ssize_t ret;
902636
+	int saverr;
902636
+
902636
+	saverr = ENOSYS;
902636
+	if (!lo_data(req)->xattr)
902636
+		goto out;
902636
+
902636
+	if (lo_debug(req)) {
902636
+		fuse_log(FUSE_LOG_DEBUG, "lo_setxattr(ino=%" PRIu64 ", name=%s value=%s size=%zd)\n",
902636
+			ino, name, value, size);
902636
+	}
902636
+
902636
+	if (inode->is_symlink) {
902636
+		/* Sorry, no race free way to setxattr on symlink. */
902636
+		saverr = EPERM;
902636
+		goto out;
902636
+	}
902636
+
902636
+	sprintf(procname, "/proc/self/fd/%i", inode->fd);
902636
+
902636
+	ret = setxattr(procname, name, value, size, flags);
902636
+	saverr = ret == -1 ? errno : 0;
902636
+
902636
+out:
902636
+	fuse_reply_err(req, saverr);
902636
+}
902636
+
902636
+static void lo_removexattr(fuse_req_t req, fuse_ino_t ino, const char *name)
902636
+{
902636
+	char procname[64];
902636
+	struct lo_inode *inode = lo_inode(req, ino);
902636
+	ssize_t ret;
902636
+	int saverr;
902636
+
902636
+	saverr = ENOSYS;
902636
+	if (!lo_data(req)->xattr)
902636
+		goto out;
902636
+
902636
+	if (lo_debug(req)) {
902636
+		fuse_log(FUSE_LOG_DEBUG, "lo_removexattr(ino=%" PRIu64 ", name=%s)\n",
902636
+			ino, name);
902636
+	}
902636
+
902636
+	if (inode->is_symlink) {
902636
+		/* Sorry, no race free way to setxattr on symlink. */
902636
+		saverr = EPERM;
902636
+		goto out;
902636
+	}
902636
+
902636
+	sprintf(procname, "/proc/self/fd/%i", inode->fd);
902636
+
902636
+	ret = removexattr(procname, name);
902636
+	saverr = ret == -1 ? errno : 0;
902636
+
902636
+out:
902636
+	fuse_reply_err(req, saverr);
902636
+}
902636
+
902636
+#ifdef HAVE_COPY_FILE_RANGE
902636
+static void lo_copy_file_range(fuse_req_t req, fuse_ino_t ino_in, off_t off_in,
902636
+			       struct fuse_file_info *fi_in,
902636
+			       fuse_ino_t ino_out, off_t off_out,
902636
+			       struct fuse_file_info *fi_out, size_t len,
902636
+			       int flags)
902636
+{
902636
+	ssize_t res;
902636
+
902636
+	if (lo_debug(req))
902636
+		fuse_log(FUSE_LOG_DEBUG, "lo_copy_file_range(ino=%" PRIu64 "/fd=%lu, "
902636
+				"off=%lu, ino=%" PRIu64 "/fd=%lu, "
902636
+				"off=%lu, size=%zd, flags=0x%x)\n",
902636
+			ino_in, fi_in->fh, off_in, ino_out, fi_out->fh, off_out,
902636
+			len, flags);
902636
+
902636
+	res = copy_file_range(fi_in->fh, &off_in, fi_out->fh, &off_out, len,
902636
+			      flags);
902636
+	if (res < 0)
902636
+		fuse_reply_err(req, -errno);
902636
+	else
902636
+		fuse_reply_write(req, res);
902636
+}
902636
+#endif
902636
+
902636
+static void lo_lseek(fuse_req_t req, fuse_ino_t ino, off_t off, int whence,
902636
+		     struct fuse_file_info *fi)
902636
+{
902636
+	off_t res;
902636
+
902636
+	(void)ino;
902636
+	res = lseek(fi->fh, off, whence);
902636
+	if (res != -1)
902636
+		fuse_reply_lseek(req, res);
902636
+	else
902636
+		fuse_reply_err(req, errno);
902636
+}
902636
+
902636
+static struct fuse_lowlevel_ops lo_oper = {
902636
+	.init		= lo_init,
902636
+	.lookup		= lo_lookup,
902636
+	.mkdir		= lo_mkdir,
902636
+	.mknod		= lo_mknod,
902636
+	.symlink	= lo_symlink,
902636
+	.link		= lo_link,
902636
+	.unlink		= lo_unlink,
902636
+	.rmdir		= lo_rmdir,
902636
+	.rename		= lo_rename,
902636
+	.forget		= lo_forget,
902636
+	.forget_multi	= lo_forget_multi,
902636
+	.getattr	= lo_getattr,
902636
+	.setattr	= lo_setattr,
902636
+	.readlink	= lo_readlink,
902636
+	.opendir	= lo_opendir,
902636
+	.readdir	= lo_readdir,
902636
+	.readdirplus	= lo_readdirplus,
902636
+	.releasedir	= lo_releasedir,
902636
+	.fsyncdir	= lo_fsyncdir,
902636
+	.create		= lo_create,
902636
+	.open		= lo_open,
902636
+	.release	= lo_release,
902636
+	.flush		= lo_flush,
902636
+	.fsync		= lo_fsync,
902636
+	.read		= lo_read,
902636
+	.write_buf      = lo_write_buf,
902636
+	.statfs		= lo_statfs,
902636
+	.fallocate	= lo_fallocate,
902636
+	.flock		= lo_flock,
902636
+	.getxattr	= lo_getxattr,
902636
+	.listxattr	= lo_listxattr,
902636
+	.setxattr	= lo_setxattr,
902636
+	.removexattr	= lo_removexattr,
902636
+#ifdef HAVE_COPY_FILE_RANGE
902636
+	.copy_file_range = lo_copy_file_range,
902636
+#endif
902636
+	.lseek		= lo_lseek,
902636
+};
902636
+
902636
+int main(int argc, char *argv[])
902636
+{
902636
+	struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
902636
+	struct fuse_session *se;
902636
+	struct fuse_cmdline_opts opts;
902636
+	struct lo_data lo = { .debug = 0,
902636
+	                      .writeback = 0 };
902636
+	int ret = -1;
902636
+
902636
+	/* Don't mask creation mode, kernel already did that */
902636
+	umask(0);
902636
+
902636
+	pthread_mutex_init(&lo.mutex, NULL);
902636
+	lo.root.next = lo.root.prev = &lo.root;
902636
+	lo.root.fd = -1;
902636
+	lo.cache = CACHE_NORMAL;
902636
+
902636
+	if (fuse_parse_cmdline(&args, &opts) != 0)
902636
+		return 1;
902636
+	if (opts.show_help) {
902636
+		printf("usage: %s [options] <mountpoint>\n\n", argv[0]);
902636
+		fuse_cmdline_help();
902636
+		fuse_lowlevel_help();
902636
+		ret = 0;
902636
+		goto err_out1;
902636
+	} else if (opts.show_version) {
902636
+		printf("FUSE library version %s\n", fuse_pkgversion());
902636
+		fuse_lowlevel_version();
902636
+		ret = 0;
902636
+		goto err_out1;
902636
+	}
902636
+
902636
+	if(opts.mountpoint == NULL) {
902636
+		printf("usage: %s [options] <mountpoint>\n", argv[0]);
902636
+		printf("       %s --help\n", argv[0]);
902636
+		ret = 1;
902636
+		goto err_out1;
902636
+	}
902636
+
902636
+	if (fuse_opt_parse(&args, &lo, lo_opts, NULL)== -1)
902636
+		return 1;
902636
+
902636
+	lo.debug = opts.debug;
902636
+	lo.root.refcount = 2;
902636
+	if (lo.source) {
902636
+		struct stat stat;
902636
+		int res;
902636
+
902636
+		res = lstat(lo.source, &stat;;
902636
+		if (res == -1) {
902636
+			fuse_log(FUSE_LOG_ERR, "failed to stat source (\"%s\"): %m\n",
902636
+				 lo.source);
902636
+			exit(1);
902636
+		}
902636
+		if (!S_ISDIR(stat.st_mode)) {
902636
+			fuse_log(FUSE_LOG_ERR, "source is not a directory\n");
902636
+			exit(1);
902636
+		}
902636
+
902636
+	} else {
902636
+		lo.source = "/";
902636
+	}
902636
+	lo.root.is_symlink = false;
902636
+	if (!lo.timeout_set) {
902636
+		switch (lo.cache) {
902636
+		case CACHE_NEVER:
902636
+			lo.timeout = 0.0;
902636
+			break;
902636
+
902636
+		case CACHE_NORMAL:
902636
+			lo.timeout = 1.0;
902636
+			break;
902636
+
902636
+		case CACHE_ALWAYS:
902636
+			lo.timeout = 86400.0;
902636
+			break;
902636
+		}
902636
+	} else if (lo.timeout < 0) {
902636
+		fuse_log(FUSE_LOG_ERR, "timeout is negative (%lf)\n",
902636
+			 lo.timeout);
902636
+		exit(1);
902636
+	}
902636
+
902636
+	lo.root.fd = open(lo.source, O_PATH);
902636
+	if (lo.root.fd == -1) {
902636
+		fuse_log(FUSE_LOG_ERR, "open(\"%s\", O_PATH): %m\n",
902636
+			 lo.source);
902636
+		exit(1);
902636
+	}
902636
+
902636
+	se = fuse_session_new(&args, &lo_oper, sizeof(lo_oper), &lo);
902636
+	if (se == NULL)
902636
+	    goto err_out1;
902636
+
902636
+	if (fuse_set_signal_handlers(se) != 0)
902636
+	    goto err_out2;
902636
+
902636
+	if (fuse_session_mount(se, opts.mountpoint) != 0)
902636
+	    goto err_out3;
902636
+
902636
+	fuse_daemonize(opts.foreground);
902636
+
902636
+	/* Block until ctrl+c or fusermount -u */
902636
+	if (opts.singlethread)
902636
+		ret = fuse_session_loop(se);
902636
+	else
902636
+		ret = fuse_session_loop_mt(se, opts.clone_fd);
902636
+
902636
+	fuse_session_unmount(se);
902636
+err_out3:
902636
+	fuse_remove_signal_handlers(se);
902636
+err_out2:
902636
+	fuse_session_destroy(se);
902636
+err_out1:
902636
+	free(opts.mountpoint);
902636
+	fuse_opt_free_args(&args);
902636
+
902636
+	if (lo.root.fd >= 0)
902636
+		close(lo.root.fd);
902636
+
902636
+	return ret ? 1 : 0;
902636
+}
902636
-- 
902636
1.8.3.1
902636