virtiofsd: remove mountpoint dummy argument
[qemu/kevin.git] / tools / virtiofsd / passthrough_ll.c
blob9377718d9dbe4030860aea7ffe1c7c86ffb60374
1 /*
2 * FUSE: Filesystem in Userspace
3 * Copyright (C) 2001-2007 Miklos Szeredi <miklos@szeredi.hu>
5 * This program can be distributed under the terms of the GNU GPLv2.
6 * See the file COPYING.
7 */
9 /*
11 * This file system mirrors the existing file system hierarchy of the
12 * system, starting at the root file system. This is implemented by
13 * just "passing through" all requests to the corresponding user-space
14 * libc functions. In contrast to passthrough.c and passthrough_fh.c,
15 * this implementation uses the low-level API. Its performance should
16 * be the least bad among the three, but many operations are not
17 * implemented. In particular, it is not possible to remove files (or
18 * directories) because the code necessary to defer actual removal
19 * until the file is not opened anymore would make the example much
20 * more complicated.
22 * When writeback caching is enabled (-o writeback mount option), it
23 * is only possible to write to files for which the mounting user has
24 * read permissions. This is because the writeback cache requires the
25 * kernel to be able to issue read requests for all files (which the
26 * passthrough filesystem cannot satisfy if it can't read the file in
27 * the underlying filesystem).
29 * Compile with:
31 * gcc -Wall passthrough_ll.c `pkg-config fuse3 --cflags --libs` -o
32 * passthrough_ll
34 * ## Source code ##
35 * \include passthrough_ll.c
38 #define _GNU_SOURCE
39 #define FUSE_USE_VERSION 31
41 #include "config.h"
43 #include <assert.h>
44 #include <dirent.h>
45 #include <errno.h>
46 #include <fuse_lowlevel.h>
47 #include <inttypes.h>
48 #include <limits.h>
49 #include <pthread.h>
50 #include <stdbool.h>
51 #include <stddef.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <sys/file.h>
56 #include <sys/xattr.h>
57 #include <unistd.h>
59 #include "passthrough_helpers.h"
62 * We are re-using pointers to our `struct lo_inode` and `struct
63 * lo_dirp` elements as inodes. This means that we must be able to
64 * store uintptr_t values in a fuse_ino_t variable. The following
65 * incantation checks this condition at compile time.
67 #if defined(__GNUC__) && \
68 (__GNUC__ > 4 || __GNUC__ == 4 && __GNUC_MINOR__ >= 6) && \
69 !defined __cplusplus
70 _Static_assert(sizeof(fuse_ino_t) >= sizeof(uintptr_t),
71 "fuse_ino_t too small to hold uintptr_t values!");
72 #else
73 struct _uintptr_to_must_hold_fuse_ino_t_dummy_struct {
74 unsigned _uintptr_to_must_hold_fuse_ino_t
75 : ((sizeof(fuse_ino_t) >= sizeof(uintptr_t)) ? 1 : -1);
77 #endif
79 struct lo_inode {
80 struct lo_inode *next; /* protected by lo->mutex */
81 struct lo_inode *prev; /* protected by lo->mutex */
82 int fd;
83 bool is_symlink;
84 ino_t ino;
85 dev_t dev;
86 uint64_t refcount; /* protected by lo->mutex */
89 enum {
90 CACHE_NEVER,
91 CACHE_NORMAL,
92 CACHE_ALWAYS,
95 struct lo_data {
96 pthread_mutex_t mutex;
97 int debug;
98 int writeback;
99 int flock;
100 int xattr;
101 const char *source;
102 double timeout;
103 int cache;
104 int timeout_set;
105 struct lo_inode root; /* protected by lo->mutex */
108 static const struct fuse_opt lo_opts[] = {
109 { "writeback", offsetof(struct lo_data, writeback), 1 },
110 { "no_writeback", offsetof(struct lo_data, writeback), 0 },
111 { "source=%s", offsetof(struct lo_data, source), 0 },
112 { "flock", offsetof(struct lo_data, flock), 1 },
113 { "no_flock", offsetof(struct lo_data, flock), 0 },
114 { "xattr", offsetof(struct lo_data, xattr), 1 },
115 { "no_xattr", offsetof(struct lo_data, xattr), 0 },
116 { "timeout=%lf", offsetof(struct lo_data, timeout), 0 },
117 { "timeout=", offsetof(struct lo_data, timeout_set), 1 },
118 { "cache=never", offsetof(struct lo_data, cache), CACHE_NEVER },
119 { "cache=auto", offsetof(struct lo_data, cache), CACHE_NORMAL },
120 { "cache=always", offsetof(struct lo_data, cache), CACHE_ALWAYS },
122 FUSE_OPT_END
125 static struct lo_data *lo_data(fuse_req_t req)
127 return (struct lo_data *)fuse_req_userdata(req);
130 static struct lo_inode *lo_inode(fuse_req_t req, fuse_ino_t ino)
132 if (ino == FUSE_ROOT_ID) {
133 return &lo_data(req)->root;
134 } else {
135 return (struct lo_inode *)(uintptr_t)ino;
139 static int lo_fd(fuse_req_t req, fuse_ino_t ino)
141 return lo_inode(req, ino)->fd;
144 static bool lo_debug(fuse_req_t req)
146 return lo_data(req)->debug != 0;
149 static void lo_init(void *userdata, struct fuse_conn_info *conn)
151 struct lo_data *lo = (struct lo_data *)userdata;
153 if (conn->capable & FUSE_CAP_EXPORT_SUPPORT) {
154 conn->want |= FUSE_CAP_EXPORT_SUPPORT;
157 if (lo->writeback && conn->capable & FUSE_CAP_WRITEBACK_CACHE) {
158 if (lo->debug) {
159 fuse_log(FUSE_LOG_DEBUG, "lo_init: activating writeback\n");
161 conn->want |= FUSE_CAP_WRITEBACK_CACHE;
163 if (lo->flock && conn->capable & FUSE_CAP_FLOCK_LOCKS) {
164 if (lo->debug) {
165 fuse_log(FUSE_LOG_DEBUG, "lo_init: activating flock locks\n");
167 conn->want |= FUSE_CAP_FLOCK_LOCKS;
171 static void lo_getattr(fuse_req_t req, fuse_ino_t ino,
172 struct fuse_file_info *fi)
174 int res;
175 struct stat buf;
176 struct lo_data *lo = lo_data(req);
178 (void)fi;
180 res =
181 fstatat(lo_fd(req, ino), "", &buf, AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW);
182 if (res == -1) {
183 return (void)fuse_reply_err(req, errno);
186 fuse_reply_attr(req, &buf, lo->timeout);
189 static int utimensat_empty_nofollow(struct lo_inode *inode,
190 const struct timespec *tv)
192 int res;
193 char procname[64];
195 if (inode->is_symlink) {
196 res = utimensat(inode->fd, "", tv, AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW);
197 if (res == -1 && errno == EINVAL) {
198 /* Sorry, no race free way to set times on symlink. */
199 errno = EPERM;
201 return res;
203 sprintf(procname, "/proc/self/fd/%i", inode->fd);
205 return utimensat(AT_FDCWD, procname, tv, 0);
208 static void lo_setattr(fuse_req_t req, fuse_ino_t ino, struct stat *attr,
209 int valid, struct fuse_file_info *fi)
211 int saverr;
212 char procname[64];
213 struct lo_inode *inode = lo_inode(req, ino);
214 int ifd = inode->fd;
215 int res;
217 if (valid & FUSE_SET_ATTR_MODE) {
218 if (fi) {
219 res = fchmod(fi->fh, attr->st_mode);
220 } else {
221 sprintf(procname, "/proc/self/fd/%i", ifd);
222 res = chmod(procname, attr->st_mode);
224 if (res == -1) {
225 goto out_err;
228 if (valid & (FUSE_SET_ATTR_UID | FUSE_SET_ATTR_GID)) {
229 uid_t uid = (valid & FUSE_SET_ATTR_UID) ? attr->st_uid : (uid_t)-1;
230 gid_t gid = (valid & FUSE_SET_ATTR_GID) ? attr->st_gid : (gid_t)-1;
232 res = fchownat(ifd, "", uid, gid, AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW);
233 if (res == -1) {
234 goto out_err;
237 if (valid & FUSE_SET_ATTR_SIZE) {
238 if (fi) {
239 res = ftruncate(fi->fh, attr->st_size);
240 } else {
241 sprintf(procname, "/proc/self/fd/%i", ifd);
242 res = truncate(procname, attr->st_size);
244 if (res == -1) {
245 goto out_err;
248 if (valid & (FUSE_SET_ATTR_ATIME | FUSE_SET_ATTR_MTIME)) {
249 struct timespec tv[2];
251 tv[0].tv_sec = 0;
252 tv[1].tv_sec = 0;
253 tv[0].tv_nsec = UTIME_OMIT;
254 tv[1].tv_nsec = UTIME_OMIT;
256 if (valid & FUSE_SET_ATTR_ATIME_NOW) {
257 tv[0].tv_nsec = UTIME_NOW;
258 } else if (valid & FUSE_SET_ATTR_ATIME) {
259 tv[0] = attr->st_atim;
262 if (valid & FUSE_SET_ATTR_MTIME_NOW) {
263 tv[1].tv_nsec = UTIME_NOW;
264 } else if (valid & FUSE_SET_ATTR_MTIME) {
265 tv[1] = attr->st_mtim;
268 if (fi) {
269 res = futimens(fi->fh, tv);
270 } else {
271 res = utimensat_empty_nofollow(inode, tv);
273 if (res == -1) {
274 goto out_err;
278 return lo_getattr(req, ino, fi);
280 out_err:
281 saverr = errno;
282 fuse_reply_err(req, saverr);
285 static struct lo_inode *lo_find(struct lo_data *lo, struct stat *st)
287 struct lo_inode *p;
288 struct lo_inode *ret = NULL;
290 pthread_mutex_lock(&lo->mutex);
291 for (p = lo->root.next; p != &lo->root; p = p->next) {
292 if (p->ino == st->st_ino && p->dev == st->st_dev) {
293 assert(p->refcount > 0);
294 ret = p;
295 ret->refcount++;
296 break;
299 pthread_mutex_unlock(&lo->mutex);
300 return ret;
303 static int lo_do_lookup(fuse_req_t req, fuse_ino_t parent, const char *name,
304 struct fuse_entry_param *e)
306 int newfd;
307 int res;
308 int saverr;
309 struct lo_data *lo = lo_data(req);
310 struct lo_inode *inode;
312 memset(e, 0, sizeof(*e));
313 e->attr_timeout = lo->timeout;
314 e->entry_timeout = lo->timeout;
316 newfd = openat(lo_fd(req, parent), name, O_PATH | O_NOFOLLOW);
317 if (newfd == -1) {
318 goto out_err;
321 res = fstatat(newfd, "", &e->attr, AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW);
322 if (res == -1) {
323 goto out_err;
326 inode = lo_find(lo_data(req), &e->attr);
327 if (inode) {
328 close(newfd);
329 newfd = -1;
330 } else {
331 struct lo_inode *prev, *next;
333 saverr = ENOMEM;
334 inode = calloc(1, sizeof(struct lo_inode));
335 if (!inode) {
336 goto out_err;
339 inode->is_symlink = S_ISLNK(e->attr.st_mode);
340 inode->refcount = 1;
341 inode->fd = newfd;
342 inode->ino = e->attr.st_ino;
343 inode->dev = e->attr.st_dev;
345 pthread_mutex_lock(&lo->mutex);
346 prev = &lo->root;
347 next = prev->next;
348 next->prev = inode;
349 inode->next = next;
350 inode->prev = prev;
351 prev->next = inode;
352 pthread_mutex_unlock(&lo->mutex);
354 e->ino = (uintptr_t)inode;
356 if (lo_debug(req)) {
357 fuse_log(FUSE_LOG_DEBUG, " %lli/%s -> %lli\n",
358 (unsigned long long)parent, name, (unsigned long long)e->ino);
361 return 0;
363 out_err:
364 saverr = errno;
365 if (newfd != -1) {
366 close(newfd);
368 return saverr;
371 static void lo_lookup(fuse_req_t req, fuse_ino_t parent, const char *name)
373 struct fuse_entry_param e;
374 int err;
376 if (lo_debug(req)) {
377 fuse_log(FUSE_LOG_DEBUG, "lo_lookup(parent=%" PRIu64 ", name=%s)\n",
378 parent, name);
381 err = lo_do_lookup(req, parent, name, &e);
382 if (err) {
383 fuse_reply_err(req, err);
384 } else {
385 fuse_reply_entry(req, &e);
389 static void lo_mknod_symlink(fuse_req_t req, fuse_ino_t parent,
390 const char *name, mode_t mode, dev_t rdev,
391 const char *link)
393 int res;
394 int saverr;
395 struct lo_inode *dir = lo_inode(req, parent);
396 struct fuse_entry_param e;
398 saverr = ENOMEM;
400 res = mknod_wrapper(dir->fd, name, link, mode, rdev);
402 saverr = errno;
403 if (res == -1) {
404 goto out;
407 saverr = lo_do_lookup(req, parent, name, &e);
408 if (saverr) {
409 goto out;
412 if (lo_debug(req)) {
413 fuse_log(FUSE_LOG_DEBUG, " %lli/%s -> %lli\n",
414 (unsigned long long)parent, name, (unsigned long long)e.ino);
417 fuse_reply_entry(req, &e);
418 return;
420 out:
421 fuse_reply_err(req, saverr);
424 static void lo_mknod(fuse_req_t req, fuse_ino_t parent, const char *name,
425 mode_t mode, dev_t rdev)
427 lo_mknod_symlink(req, parent, name, mode, rdev, NULL);
430 static void lo_mkdir(fuse_req_t req, fuse_ino_t parent, const char *name,
431 mode_t mode)
433 lo_mknod_symlink(req, parent, name, S_IFDIR | mode, 0, NULL);
436 static void lo_symlink(fuse_req_t req, const char *link, fuse_ino_t parent,
437 const char *name)
439 lo_mknod_symlink(req, parent, name, S_IFLNK, 0, link);
442 static int linkat_empty_nofollow(struct lo_inode *inode, int dfd,
443 const char *name)
445 int res;
446 char procname[64];
448 if (inode->is_symlink) {
449 res = linkat(inode->fd, "", dfd, name, AT_EMPTY_PATH);
450 if (res == -1 && (errno == ENOENT || errno == EINVAL)) {
451 /* Sorry, no race free way to hard-link a symlink. */
452 errno = EPERM;
454 return res;
457 sprintf(procname, "/proc/self/fd/%i", inode->fd);
459 return linkat(AT_FDCWD, procname, dfd, name, AT_SYMLINK_FOLLOW);
462 static void lo_link(fuse_req_t req, fuse_ino_t ino, fuse_ino_t parent,
463 const char *name)
465 int res;
466 struct lo_data *lo = lo_data(req);
467 struct lo_inode *inode = lo_inode(req, ino);
468 struct fuse_entry_param e;
469 int saverr;
471 memset(&e, 0, sizeof(struct fuse_entry_param));
472 e.attr_timeout = lo->timeout;
473 e.entry_timeout = lo->timeout;
475 res = linkat_empty_nofollow(inode, lo_fd(req, parent), name);
476 if (res == -1) {
477 goto out_err;
480 res = fstatat(inode->fd, "", &e.attr, AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW);
481 if (res == -1) {
482 goto out_err;
485 pthread_mutex_lock(&lo->mutex);
486 inode->refcount++;
487 pthread_mutex_unlock(&lo->mutex);
488 e.ino = (uintptr_t)inode;
490 if (lo_debug(req)) {
491 fuse_log(FUSE_LOG_DEBUG, " %lli/%s -> %lli\n",
492 (unsigned long long)parent, name, (unsigned long long)e.ino);
495 fuse_reply_entry(req, &e);
496 return;
498 out_err:
499 saverr = errno;
500 fuse_reply_err(req, saverr);
503 static void lo_rmdir(fuse_req_t req, fuse_ino_t parent, const char *name)
505 int res;
507 res = unlinkat(lo_fd(req, parent), name, AT_REMOVEDIR);
509 fuse_reply_err(req, res == -1 ? errno : 0);
512 static void lo_rename(fuse_req_t req, fuse_ino_t parent, const char *name,
513 fuse_ino_t newparent, const char *newname,
514 unsigned int flags)
516 int res;
518 if (flags) {
519 fuse_reply_err(req, EINVAL);
520 return;
523 res = renameat(lo_fd(req, parent), name, lo_fd(req, newparent), newname);
525 fuse_reply_err(req, res == -1 ? errno : 0);
528 static void lo_unlink(fuse_req_t req, fuse_ino_t parent, const char *name)
530 int res;
532 res = unlinkat(lo_fd(req, parent), name, 0);
534 fuse_reply_err(req, res == -1 ? errno : 0);
537 static void unref_inode(struct lo_data *lo, struct lo_inode *inode, uint64_t n)
539 if (!inode) {
540 return;
543 pthread_mutex_lock(&lo->mutex);
544 assert(inode->refcount >= n);
545 inode->refcount -= n;
546 if (!inode->refcount) {
547 struct lo_inode *prev, *next;
549 prev = inode->prev;
550 next = inode->next;
551 next->prev = prev;
552 prev->next = next;
554 pthread_mutex_unlock(&lo->mutex);
555 close(inode->fd);
556 free(inode);
558 } else {
559 pthread_mutex_unlock(&lo->mutex);
563 static void lo_forget_one(fuse_req_t req, fuse_ino_t ino, uint64_t nlookup)
565 struct lo_data *lo = lo_data(req);
566 struct lo_inode *inode = lo_inode(req, ino);
568 if (lo_debug(req)) {
569 fuse_log(FUSE_LOG_DEBUG, " forget %lli %lli -%lli\n",
570 (unsigned long long)ino, (unsigned long long)inode->refcount,
571 (unsigned long long)nlookup);
574 unref_inode(lo, inode, nlookup);
577 static void lo_forget(fuse_req_t req, fuse_ino_t ino, uint64_t nlookup)
579 lo_forget_one(req, ino, nlookup);
580 fuse_reply_none(req);
583 static void lo_forget_multi(fuse_req_t req, size_t count,
584 struct fuse_forget_data *forgets)
586 int i;
588 for (i = 0; i < count; i++) {
589 lo_forget_one(req, forgets[i].ino, forgets[i].nlookup);
591 fuse_reply_none(req);
594 static void lo_readlink(fuse_req_t req, fuse_ino_t ino)
596 char buf[PATH_MAX + 1];
597 int res;
599 res = readlinkat(lo_fd(req, ino), "", buf, sizeof(buf));
600 if (res == -1) {
601 return (void)fuse_reply_err(req, errno);
604 if (res == sizeof(buf)) {
605 return (void)fuse_reply_err(req, ENAMETOOLONG);
608 buf[res] = '\0';
610 fuse_reply_readlink(req, buf);
613 struct lo_dirp {
614 DIR *dp;
615 struct dirent *entry;
616 off_t offset;
619 static struct lo_dirp *lo_dirp(struct fuse_file_info *fi)
621 return (struct lo_dirp *)(uintptr_t)fi->fh;
624 static void lo_opendir(fuse_req_t req, fuse_ino_t ino,
625 struct fuse_file_info *fi)
627 int error = ENOMEM;
628 struct lo_data *lo = lo_data(req);
629 struct lo_dirp *d;
630 int fd;
632 d = calloc(1, sizeof(struct lo_dirp));
633 if (d == NULL) {
634 goto out_err;
637 fd = openat(lo_fd(req, ino), ".", O_RDONLY);
638 if (fd == -1) {
639 goto out_errno;
642 d->dp = fdopendir(fd);
643 if (d->dp == NULL) {
644 goto out_errno;
647 d->offset = 0;
648 d->entry = NULL;
650 fi->fh = (uintptr_t)d;
651 if (lo->cache == CACHE_ALWAYS) {
652 fi->keep_cache = 1;
654 fuse_reply_open(req, fi);
655 return;
657 out_errno:
658 error = errno;
659 out_err:
660 if (d) {
661 if (fd != -1) {
662 close(fd);
664 free(d);
666 fuse_reply_err(req, error);
669 static int is_dot_or_dotdot(const char *name)
671 return name[0] == '.' &&
672 (name[1] == '\0' || (name[1] == '.' && name[2] == '\0'));
675 static void lo_do_readdir(fuse_req_t req, fuse_ino_t ino, size_t size,
676 off_t offset, struct fuse_file_info *fi, int plus)
678 struct lo_dirp *d = lo_dirp(fi);
679 char *buf;
680 char *p;
681 size_t rem = size;
682 int err;
684 (void)ino;
686 buf = calloc(1, size);
687 if (!buf) {
688 err = ENOMEM;
689 goto error;
691 p = buf;
693 if (offset != d->offset) {
694 seekdir(d->dp, offset);
695 d->entry = NULL;
696 d->offset = offset;
698 while (1) {
699 size_t entsize;
700 off_t nextoff;
701 const char *name;
703 if (!d->entry) {
704 errno = 0;
705 d->entry = readdir(d->dp);
706 if (!d->entry) {
707 if (errno) { /* Error */
708 err = errno;
709 goto error;
710 } else { /* End of stream */
711 break;
715 nextoff = d->entry->d_off;
716 name = d->entry->d_name;
717 fuse_ino_t entry_ino = 0;
718 if (plus) {
719 struct fuse_entry_param e;
720 if (is_dot_or_dotdot(name)) {
721 e = (struct fuse_entry_param){
722 .attr.st_ino = d->entry->d_ino,
723 .attr.st_mode = d->entry->d_type << 12,
725 } else {
726 err = lo_do_lookup(req, ino, name, &e);
727 if (err) {
728 goto error;
730 entry_ino = e.ino;
733 entsize = fuse_add_direntry_plus(req, p, rem, name, &e, nextoff);
734 } else {
735 struct stat st = {
736 .st_ino = d->entry->d_ino,
737 .st_mode = d->entry->d_type << 12,
739 entsize = fuse_add_direntry(req, p, rem, name, &st, nextoff);
741 if (entsize > rem) {
742 if (entry_ino != 0) {
743 lo_forget_one(req, entry_ino, 1);
745 break;
748 p += entsize;
749 rem -= entsize;
751 d->entry = NULL;
752 d->offset = nextoff;
755 err = 0;
756 error:
758 * If there's an error, we can only signal it if we haven't stored
759 * any entries yet - otherwise we'd end up with wrong lookup
760 * counts for the entries that are already in the buffer. So we
761 * return what we've collected until that point.
763 if (err && rem == size) {
764 fuse_reply_err(req, err);
765 } else {
766 fuse_reply_buf(req, buf, size - rem);
768 free(buf);
771 static void lo_readdir(fuse_req_t req, fuse_ino_t ino, size_t size,
772 off_t offset, struct fuse_file_info *fi)
774 lo_do_readdir(req, ino, size, offset, fi, 0);
777 static void lo_readdirplus(fuse_req_t req, fuse_ino_t ino, size_t size,
778 off_t offset, struct fuse_file_info *fi)
780 lo_do_readdir(req, ino, size, offset, fi, 1);
783 static void lo_releasedir(fuse_req_t req, fuse_ino_t ino,
784 struct fuse_file_info *fi)
786 struct lo_dirp *d = lo_dirp(fi);
787 (void)ino;
788 closedir(d->dp);
789 free(d);
790 fuse_reply_err(req, 0);
793 static void lo_create(fuse_req_t req, fuse_ino_t parent, const char *name,
794 mode_t mode, struct fuse_file_info *fi)
796 int fd;
797 struct lo_data *lo = lo_data(req);
798 struct fuse_entry_param e;
799 int err;
801 if (lo_debug(req)) {
802 fuse_log(FUSE_LOG_DEBUG, "lo_create(parent=%" PRIu64 ", name=%s)\n",
803 parent, name);
806 fd = openat(lo_fd(req, parent), name, (fi->flags | O_CREAT) & ~O_NOFOLLOW,
807 mode);
808 if (fd == -1) {
809 return (void)fuse_reply_err(req, errno);
812 fi->fh = fd;
813 if (lo->cache == CACHE_NEVER) {
814 fi->direct_io = 1;
815 } else if (lo->cache == CACHE_ALWAYS) {
816 fi->keep_cache = 1;
819 err = lo_do_lookup(req, parent, name, &e);
820 if (err) {
821 fuse_reply_err(req, err);
822 } else {
823 fuse_reply_create(req, &e, fi);
827 static void lo_fsyncdir(fuse_req_t req, fuse_ino_t ino, int datasync,
828 struct fuse_file_info *fi)
830 int res;
831 int fd = dirfd(lo_dirp(fi)->dp);
832 (void)ino;
833 if (datasync) {
834 res = fdatasync(fd);
835 } else {
836 res = fsync(fd);
838 fuse_reply_err(req, res == -1 ? errno : 0);
841 static void lo_open(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi)
843 int fd;
844 char buf[64];
845 struct lo_data *lo = lo_data(req);
847 if (lo_debug(req)) {
848 fuse_log(FUSE_LOG_DEBUG, "lo_open(ino=%" PRIu64 ", flags=%d)\n", ino,
849 fi->flags);
853 * With writeback cache, kernel may send read requests even
854 * when userspace opened write-only
856 if (lo->writeback && (fi->flags & O_ACCMODE) == O_WRONLY) {
857 fi->flags &= ~O_ACCMODE;
858 fi->flags |= O_RDWR;
862 * With writeback cache, O_APPEND is handled by the kernel.
863 * This breaks atomicity (since the file may change in the
864 * underlying filesystem, so that the kernel's idea of the
865 * end of the file isn't accurate anymore). In this example,
866 * we just accept that. A more rigorous filesystem may want
867 * to return an error here
869 if (lo->writeback && (fi->flags & O_APPEND)) {
870 fi->flags &= ~O_APPEND;
873 sprintf(buf, "/proc/self/fd/%i", lo_fd(req, ino));
874 fd = open(buf, fi->flags & ~O_NOFOLLOW);
875 if (fd == -1) {
876 return (void)fuse_reply_err(req, errno);
879 fi->fh = fd;
880 if (lo->cache == CACHE_NEVER) {
881 fi->direct_io = 1;
882 } else if (lo->cache == CACHE_ALWAYS) {
883 fi->keep_cache = 1;
885 fuse_reply_open(req, fi);
888 static void lo_release(fuse_req_t req, fuse_ino_t ino,
889 struct fuse_file_info *fi)
891 (void)ino;
893 close(fi->fh);
894 fuse_reply_err(req, 0);
897 static void lo_flush(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi)
899 int res;
900 (void)ino;
901 res = close(dup(fi->fh));
902 fuse_reply_err(req, res == -1 ? errno : 0);
905 static void lo_fsync(fuse_req_t req, fuse_ino_t ino, int datasync,
906 struct fuse_file_info *fi)
908 int res;
909 (void)ino;
910 if (datasync) {
911 res = fdatasync(fi->fh);
912 } else {
913 res = fsync(fi->fh);
915 fuse_reply_err(req, res == -1 ? errno : 0);
918 static void lo_read(fuse_req_t req, fuse_ino_t ino, size_t size, off_t offset,
919 struct fuse_file_info *fi)
921 struct fuse_bufvec buf = FUSE_BUFVEC_INIT(size);
923 if (lo_debug(req)) {
924 fuse_log(FUSE_LOG_DEBUG,
925 "lo_read(ino=%" PRIu64 ", size=%zd, "
926 "off=%lu)\n",
927 ino, size, (unsigned long)offset);
930 buf.buf[0].flags = FUSE_BUF_IS_FD | FUSE_BUF_FD_SEEK;
931 buf.buf[0].fd = fi->fh;
932 buf.buf[0].pos = offset;
934 fuse_reply_data(req, &buf, FUSE_BUF_SPLICE_MOVE);
937 static void lo_write_buf(fuse_req_t req, fuse_ino_t ino,
938 struct fuse_bufvec *in_buf, off_t off,
939 struct fuse_file_info *fi)
941 (void)ino;
942 ssize_t res;
943 struct fuse_bufvec out_buf = FUSE_BUFVEC_INIT(fuse_buf_size(in_buf));
945 out_buf.buf[0].flags = FUSE_BUF_IS_FD | FUSE_BUF_FD_SEEK;
946 out_buf.buf[0].fd = fi->fh;
947 out_buf.buf[0].pos = off;
949 if (lo_debug(req)) {
950 fuse_log(FUSE_LOG_DEBUG,
951 "lo_write(ino=%" PRIu64 ", size=%zd, off=%lu)\n", ino,
952 out_buf.buf[0].size, (unsigned long)off);
955 res = fuse_buf_copy(&out_buf, in_buf, 0);
956 if (res < 0) {
957 fuse_reply_err(req, -res);
958 } else {
959 fuse_reply_write(req, (size_t)res);
963 static void lo_statfs(fuse_req_t req, fuse_ino_t ino)
965 int res;
966 struct statvfs stbuf;
968 res = fstatvfs(lo_fd(req, ino), &stbuf);
969 if (res == -1) {
970 fuse_reply_err(req, errno);
971 } else {
972 fuse_reply_statfs(req, &stbuf);
976 static void lo_fallocate(fuse_req_t req, fuse_ino_t ino, int mode, off_t offset,
977 off_t length, struct fuse_file_info *fi)
979 int err = EOPNOTSUPP;
980 (void)ino;
982 #ifdef HAVE_FALLOCATE
983 err = fallocate(fi->fh, mode, offset, length);
984 if (err < 0) {
985 err = errno;
988 #elif defined(HAVE_POSIX_FALLOCATE)
989 if (mode) {
990 fuse_reply_err(req, EOPNOTSUPP);
991 return;
994 err = posix_fallocate(fi->fh, offset, length);
995 #endif
997 fuse_reply_err(req, err);
1000 static void lo_flock(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi,
1001 int op)
1003 int res;
1004 (void)ino;
1006 res = flock(fi->fh, op);
1008 fuse_reply_err(req, res == -1 ? errno : 0);
1011 static void lo_getxattr(fuse_req_t req, fuse_ino_t ino, const char *name,
1012 size_t size)
1014 char *value = NULL;
1015 char procname[64];
1016 struct lo_inode *inode = lo_inode(req, ino);
1017 ssize_t ret;
1018 int saverr;
1020 saverr = ENOSYS;
1021 if (!lo_data(req)->xattr) {
1022 goto out;
1025 if (lo_debug(req)) {
1026 fuse_log(FUSE_LOG_DEBUG,
1027 "lo_getxattr(ino=%" PRIu64 ", name=%s size=%zd)\n", ino, name,
1028 size);
1031 if (inode->is_symlink) {
1032 /* Sorry, no race free way to getxattr on symlink. */
1033 saverr = EPERM;
1034 goto out;
1037 sprintf(procname, "/proc/self/fd/%i", inode->fd);
1039 if (size) {
1040 value = malloc(size);
1041 if (!value) {
1042 goto out_err;
1045 ret = getxattr(procname, name, value, size);
1046 if (ret == -1) {
1047 goto out_err;
1049 saverr = 0;
1050 if (ret == 0) {
1051 goto out;
1054 fuse_reply_buf(req, value, ret);
1055 } else {
1056 ret = getxattr(procname, name, NULL, 0);
1057 if (ret == -1) {
1058 goto out_err;
1061 fuse_reply_xattr(req, ret);
1063 out_free:
1064 free(value);
1065 return;
1067 out_err:
1068 saverr = errno;
1069 out:
1070 fuse_reply_err(req, saverr);
1071 goto out_free;
1074 static void lo_listxattr(fuse_req_t req, fuse_ino_t ino, size_t size)
1076 char *value = NULL;
1077 char procname[64];
1078 struct lo_inode *inode = lo_inode(req, ino);
1079 ssize_t ret;
1080 int saverr;
1082 saverr = ENOSYS;
1083 if (!lo_data(req)->xattr) {
1084 goto out;
1087 if (lo_debug(req)) {
1088 fuse_log(FUSE_LOG_DEBUG, "lo_listxattr(ino=%" PRIu64 ", size=%zd)\n",
1089 ino, size);
1092 if (inode->is_symlink) {
1093 /* Sorry, no race free way to listxattr on symlink. */
1094 saverr = EPERM;
1095 goto out;
1098 sprintf(procname, "/proc/self/fd/%i", inode->fd);
1100 if (size) {
1101 value = malloc(size);
1102 if (!value) {
1103 goto out_err;
1106 ret = listxattr(procname, value, size);
1107 if (ret == -1) {
1108 goto out_err;
1110 saverr = 0;
1111 if (ret == 0) {
1112 goto out;
1115 fuse_reply_buf(req, value, ret);
1116 } else {
1117 ret = listxattr(procname, NULL, 0);
1118 if (ret == -1) {
1119 goto out_err;
1122 fuse_reply_xattr(req, ret);
1124 out_free:
1125 free(value);
1126 return;
1128 out_err:
1129 saverr = errno;
1130 out:
1131 fuse_reply_err(req, saverr);
1132 goto out_free;
1135 static void lo_setxattr(fuse_req_t req, fuse_ino_t ino, const char *name,
1136 const char *value, size_t size, int flags)
1138 char procname[64];
1139 struct lo_inode *inode = lo_inode(req, ino);
1140 ssize_t ret;
1141 int saverr;
1143 saverr = ENOSYS;
1144 if (!lo_data(req)->xattr) {
1145 goto out;
1148 if (lo_debug(req)) {
1149 fuse_log(FUSE_LOG_DEBUG,
1150 "lo_setxattr(ino=%" PRIu64 ", name=%s value=%s size=%zd)\n",
1151 ino, name, value, size);
1154 if (inode->is_symlink) {
1155 /* Sorry, no race free way to setxattr on symlink. */
1156 saverr = EPERM;
1157 goto out;
1160 sprintf(procname, "/proc/self/fd/%i", inode->fd);
1162 ret = setxattr(procname, name, value, size, flags);
1163 saverr = ret == -1 ? errno : 0;
1165 out:
1166 fuse_reply_err(req, saverr);
1169 static void lo_removexattr(fuse_req_t req, fuse_ino_t ino, const char *name)
1171 char procname[64];
1172 struct lo_inode *inode = lo_inode(req, ino);
1173 ssize_t ret;
1174 int saverr;
1176 saverr = ENOSYS;
1177 if (!lo_data(req)->xattr) {
1178 goto out;
1181 if (lo_debug(req)) {
1182 fuse_log(FUSE_LOG_DEBUG, "lo_removexattr(ino=%" PRIu64 ", name=%s)\n",
1183 ino, name);
1186 if (inode->is_symlink) {
1187 /* Sorry, no race free way to setxattr on symlink. */
1188 saverr = EPERM;
1189 goto out;
1192 sprintf(procname, "/proc/self/fd/%i", inode->fd);
1194 ret = removexattr(procname, name);
1195 saverr = ret == -1 ? errno : 0;
1197 out:
1198 fuse_reply_err(req, saverr);
1201 #ifdef HAVE_COPY_FILE_RANGE
1202 static void lo_copy_file_range(fuse_req_t req, fuse_ino_t ino_in, off_t off_in,
1203 struct fuse_file_info *fi_in, fuse_ino_t ino_out,
1204 off_t off_out, struct fuse_file_info *fi_out,
1205 size_t len, int flags)
1207 ssize_t res;
1209 if (lo_debug(req))
1210 fuse_log(FUSE_LOG_DEBUG,
1211 "lo_copy_file_range(ino=%" PRIu64 "/fd=%lu, "
1212 "off=%lu, ino=%" PRIu64 "/fd=%lu, "
1213 "off=%lu, size=%zd, flags=0x%x)\n",
1214 ino_in, fi_in->fh, off_in, ino_out, fi_out->fh, off_out, len,
1215 flags);
1217 res = copy_file_range(fi_in->fh, &off_in, fi_out->fh, &off_out, len, flags);
1218 if (res < 0) {
1219 fuse_reply_err(req, -errno);
1220 } else {
1221 fuse_reply_write(req, res);
1224 #endif
1226 static void lo_lseek(fuse_req_t req, fuse_ino_t ino, off_t off, int whence,
1227 struct fuse_file_info *fi)
1229 off_t res;
1231 (void)ino;
1232 res = lseek(fi->fh, off, whence);
1233 if (res != -1) {
1234 fuse_reply_lseek(req, res);
1235 } else {
1236 fuse_reply_err(req, errno);
1240 static struct fuse_lowlevel_ops lo_oper = {
1241 .init = lo_init,
1242 .lookup = lo_lookup,
1243 .mkdir = lo_mkdir,
1244 .mknod = lo_mknod,
1245 .symlink = lo_symlink,
1246 .link = lo_link,
1247 .unlink = lo_unlink,
1248 .rmdir = lo_rmdir,
1249 .rename = lo_rename,
1250 .forget = lo_forget,
1251 .forget_multi = lo_forget_multi,
1252 .getattr = lo_getattr,
1253 .setattr = lo_setattr,
1254 .readlink = lo_readlink,
1255 .opendir = lo_opendir,
1256 .readdir = lo_readdir,
1257 .readdirplus = lo_readdirplus,
1258 .releasedir = lo_releasedir,
1259 .fsyncdir = lo_fsyncdir,
1260 .create = lo_create,
1261 .open = lo_open,
1262 .release = lo_release,
1263 .flush = lo_flush,
1264 .fsync = lo_fsync,
1265 .read = lo_read,
1266 .write_buf = lo_write_buf,
1267 .statfs = lo_statfs,
1268 .fallocate = lo_fallocate,
1269 .flock = lo_flock,
1270 .getxattr = lo_getxattr,
1271 .listxattr = lo_listxattr,
1272 .setxattr = lo_setxattr,
1273 .removexattr = lo_removexattr,
1274 #ifdef HAVE_COPY_FILE_RANGE
1275 .copy_file_range = lo_copy_file_range,
1276 #endif
1277 .lseek = lo_lseek,
1280 int main(int argc, char *argv[])
1282 struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
1283 struct fuse_session *se;
1284 struct fuse_cmdline_opts opts;
1285 struct lo_data lo = { .debug = 0, .writeback = 0 };
1286 int ret = -1;
1288 /* Don't mask creation mode, kernel already did that */
1289 umask(0);
1291 pthread_mutex_init(&lo.mutex, NULL);
1292 lo.root.next = lo.root.prev = &lo.root;
1293 lo.root.fd = -1;
1294 lo.cache = CACHE_NORMAL;
1296 if (fuse_parse_cmdline(&args, &opts) != 0) {
1297 return 1;
1299 if (opts.show_help) {
1300 printf("usage: %s [options]\n\n", argv[0]);
1301 fuse_cmdline_help();
1302 fuse_lowlevel_help();
1303 ret = 0;
1304 goto err_out1;
1305 } else if (opts.show_version) {
1306 fuse_lowlevel_version();
1307 ret = 0;
1308 goto err_out1;
1311 if (fuse_opt_parse(&args, &lo, lo_opts, NULL) == -1) {
1312 return 1;
1315 lo.debug = opts.debug;
1316 lo.root.refcount = 2;
1317 if (lo.source) {
1318 struct stat stat;
1319 int res;
1321 res = lstat(lo.source, &stat);
1322 if (res == -1) {
1323 fuse_log(FUSE_LOG_ERR, "failed to stat source (\"%s\"): %m\n",
1324 lo.source);
1325 exit(1);
1327 if (!S_ISDIR(stat.st_mode)) {
1328 fuse_log(FUSE_LOG_ERR, "source is not a directory\n");
1329 exit(1);
1332 } else {
1333 lo.source = "/";
1335 lo.root.is_symlink = false;
1336 if (!lo.timeout_set) {
1337 switch (lo.cache) {
1338 case CACHE_NEVER:
1339 lo.timeout = 0.0;
1340 break;
1342 case CACHE_NORMAL:
1343 lo.timeout = 1.0;
1344 break;
1346 case CACHE_ALWAYS:
1347 lo.timeout = 86400.0;
1348 break;
1350 } else if (lo.timeout < 0) {
1351 fuse_log(FUSE_LOG_ERR, "timeout is negative (%lf)\n", lo.timeout);
1352 exit(1);
1355 lo.root.fd = open(lo.source, O_PATH);
1356 if (lo.root.fd == -1) {
1357 fuse_log(FUSE_LOG_ERR, "open(\"%s\", O_PATH): %m\n", lo.source);
1358 exit(1);
1361 se = fuse_session_new(&args, &lo_oper, sizeof(lo_oper), &lo);
1362 if (se == NULL) {
1363 goto err_out1;
1366 if (fuse_set_signal_handlers(se) != 0) {
1367 goto err_out2;
1370 if (fuse_session_mount(se) != 0) {
1371 goto err_out3;
1374 fuse_daemonize(opts.foreground);
1376 /* Block until ctrl+c or fusermount -u */
1377 if (opts.singlethread) {
1378 ret = fuse_session_loop(se);
1379 } else {
1380 ret = fuse_session_loop_mt(se, opts.clone_fd);
1383 fuse_session_unmount(se);
1384 err_out3:
1385 fuse_remove_signal_handlers(se);
1386 err_out2:
1387 fuse_session_destroy(se);
1388 err_out1:
1389 fuse_opt_free_args(&args);
1391 if (lo.root.fd >= 0) {
1392 close(lo.root.fd);
1395 return ret ? 1 : 0;