cryptospore / rpms / qemu-kvm

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