monitor/hmp: move hmp_drive_mirror and hmp_drive_backup to block-hmp-cmds.c
[qemu/ar7.git] / tools / virtiofsd / passthrough_ll.c
blob4f259aac70ddb1b76bc4178ab8cbe8f7c364bdb4
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 #include "qemu/osdep.h"
39 #include "qemu/timer.h"
40 #include "fuse_virtio.h"
41 #include "fuse_log.h"
42 #include "fuse_lowlevel.h"
43 #include <assert.h>
44 #include <cap-ng.h>
45 #include <dirent.h>
46 #include <errno.h>
47 #include <glib.h>
48 #include <inttypes.h>
49 #include <limits.h>
50 #include <pthread.h>
51 #include <stdbool.h>
52 #include <stddef.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <sys/file.h>
57 #include <sys/mount.h>
58 #include <sys/prctl.h>
59 #include <sys/resource.h>
60 #include <sys/syscall.h>
61 #include <sys/types.h>
62 #include <sys/wait.h>
63 #include <sys/xattr.h>
64 #include <syslog.h>
65 #include <unistd.h>
67 #include "passthrough_helpers.h"
68 #include "seccomp.h"
70 /* Keep track of inode posix locks for each owner. */
71 struct lo_inode_plock {
72 uint64_t lock_owner;
73 int fd; /* fd for OFD locks */
76 struct lo_map_elem {
77 union {
78 struct lo_inode *inode;
79 struct lo_dirp *dirp;
80 int fd;
81 ssize_t freelist;
83 bool in_use;
86 /* Maps FUSE fh or ino values to internal objects */
87 struct lo_map {
88 struct lo_map_elem *elems;
89 size_t nelems;
90 ssize_t freelist;
93 struct lo_key {
94 ino_t ino;
95 dev_t dev;
98 struct lo_inode {
99 int fd;
102 * Atomic reference count for this object. The nlookup field holds a
103 * reference and release it when nlookup reaches 0.
105 gint refcount;
107 struct lo_key key;
110 * This counter keeps the inode alive during the FUSE session.
111 * Incremented when the FUSE inode number is sent in a reply
112 * (FUSE_LOOKUP, FUSE_READDIRPLUS, etc). Decremented when an inode is
113 * released by requests like FUSE_FORGET, FUSE_RMDIR, FUSE_RENAME, etc.
115 * Note that this value is untrusted because the client can manipulate
116 * it arbitrarily using FUSE_FORGET requests.
118 * Protected by lo->mutex.
120 uint64_t nlookup;
122 fuse_ino_t fuse_ino;
123 pthread_mutex_t plock_mutex;
124 GHashTable *posix_locks; /* protected by lo_inode->plock_mutex */
126 mode_t filetype;
129 struct lo_cred {
130 uid_t euid;
131 gid_t egid;
134 enum {
135 CACHE_NONE,
136 CACHE_AUTO,
137 CACHE_ALWAYS,
140 struct lo_data {
141 pthread_mutex_t mutex;
142 int debug;
143 int norace;
144 int writeback;
145 int flock;
146 int posix_lock;
147 int xattr;
148 char *source;
149 double timeout;
150 int cache;
151 int timeout_set;
152 int readdirplus_set;
153 int readdirplus_clear;
154 struct lo_inode root;
155 GHashTable *inodes; /* protected by lo->mutex */
156 struct lo_map ino_map; /* protected by lo->mutex */
157 struct lo_map dirp_map; /* protected by lo->mutex */
158 struct lo_map fd_map; /* protected by lo->mutex */
160 /* An O_PATH file descriptor to /proc/self/fd/ */
161 int proc_self_fd;
164 static const struct fuse_opt lo_opts[] = {
165 { "writeback", offsetof(struct lo_data, writeback), 1 },
166 { "no_writeback", offsetof(struct lo_data, writeback), 0 },
167 { "source=%s", offsetof(struct lo_data, source), 0 },
168 { "flock", offsetof(struct lo_data, flock), 1 },
169 { "no_flock", offsetof(struct lo_data, flock), 0 },
170 { "posix_lock", offsetof(struct lo_data, posix_lock), 1 },
171 { "no_posix_lock", offsetof(struct lo_data, posix_lock), 0 },
172 { "xattr", offsetof(struct lo_data, xattr), 1 },
173 { "no_xattr", offsetof(struct lo_data, xattr), 0 },
174 { "timeout=%lf", offsetof(struct lo_data, timeout), 0 },
175 { "timeout=", offsetof(struct lo_data, timeout_set), 1 },
176 { "cache=none", offsetof(struct lo_data, cache), CACHE_NONE },
177 { "cache=auto", offsetof(struct lo_data, cache), CACHE_AUTO },
178 { "cache=always", offsetof(struct lo_data, cache), CACHE_ALWAYS },
179 { "norace", offsetof(struct lo_data, norace), 1 },
180 { "readdirplus", offsetof(struct lo_data, readdirplus_set), 1 },
181 { "no_readdirplus", offsetof(struct lo_data, readdirplus_clear), 1 },
182 FUSE_OPT_END
184 static bool use_syslog = false;
185 static int current_log_level;
186 static void unref_inode_lolocked(struct lo_data *lo, struct lo_inode *inode,
187 uint64_t n);
189 static struct {
190 pthread_mutex_t mutex;
191 void *saved;
192 } cap;
193 /* That we loaded cap-ng in the current thread from the saved */
194 static __thread bool cap_loaded = 0;
196 static struct lo_inode *lo_find(struct lo_data *lo, struct stat *st);
198 static int is_dot_or_dotdot(const char *name)
200 return name[0] == '.' &&
201 (name[1] == '\0' || (name[1] == '.' && name[2] == '\0'));
204 /* Is `path` a single path component that is not "." or ".."? */
205 static int is_safe_path_component(const char *path)
207 if (strchr(path, '/')) {
208 return 0;
211 return !is_dot_or_dotdot(path);
214 static struct lo_data *lo_data(fuse_req_t req)
216 return (struct lo_data *)fuse_req_userdata(req);
220 * Load capng's state from our saved state if the current thread
221 * hadn't previously been loaded.
222 * returns 0 on success
224 static int load_capng(void)
226 if (!cap_loaded) {
227 pthread_mutex_lock(&cap.mutex);
228 capng_restore_state(&cap.saved);
230 * restore_state free's the saved copy
231 * so make another.
233 cap.saved = capng_save_state();
234 if (!cap.saved) {
235 pthread_mutex_unlock(&cap.mutex);
236 fuse_log(FUSE_LOG_ERR, "capng_save_state (thread)\n");
237 return -EINVAL;
239 pthread_mutex_unlock(&cap.mutex);
242 * We want to use the loaded state for our pid,
243 * not the original
245 capng_setpid(syscall(SYS_gettid));
246 cap_loaded = true;
248 return 0;
252 * Helpers for dropping and regaining effective capabilities. Returns 0
253 * on success, error otherwise
255 static int drop_effective_cap(const char *cap_name, bool *cap_dropped)
257 int cap, ret;
259 cap = capng_name_to_capability(cap_name);
260 if (cap < 0) {
261 ret = errno;
262 fuse_log(FUSE_LOG_ERR, "capng_name_to_capability(%s) failed:%s\n",
263 cap_name, strerror(errno));
264 goto out;
267 if (load_capng()) {
268 ret = errno;
269 fuse_log(FUSE_LOG_ERR, "load_capng() failed\n");
270 goto out;
273 /* We dont have this capability in effective set already. */
274 if (!capng_have_capability(CAPNG_EFFECTIVE, cap)) {
275 ret = 0;
276 goto out;
279 if (capng_update(CAPNG_DROP, CAPNG_EFFECTIVE, cap)) {
280 ret = errno;
281 fuse_log(FUSE_LOG_ERR, "capng_update(DROP,) failed\n");
282 goto out;
285 if (capng_apply(CAPNG_SELECT_CAPS)) {
286 ret = errno;
287 fuse_log(FUSE_LOG_ERR, "drop:capng_apply() failed\n");
288 goto out;
291 ret = 0;
292 if (cap_dropped) {
293 *cap_dropped = true;
296 out:
297 return ret;
300 static int gain_effective_cap(const char *cap_name)
302 int cap;
303 int ret = 0;
305 cap = capng_name_to_capability(cap_name);
306 if (cap < 0) {
307 ret = errno;
308 fuse_log(FUSE_LOG_ERR, "capng_name_to_capability(%s) failed:%s\n",
309 cap_name, strerror(errno));
310 goto out;
313 if (load_capng()) {
314 ret = errno;
315 fuse_log(FUSE_LOG_ERR, "load_capng() failed\n");
316 goto out;
319 if (capng_update(CAPNG_ADD, CAPNG_EFFECTIVE, cap)) {
320 ret = errno;
321 fuse_log(FUSE_LOG_ERR, "capng_update(ADD,) failed\n");
322 goto out;
325 if (capng_apply(CAPNG_SELECT_CAPS)) {
326 ret = errno;
327 fuse_log(FUSE_LOG_ERR, "gain:capng_apply() failed\n");
328 goto out;
330 ret = 0;
332 out:
333 return ret;
336 static void lo_map_init(struct lo_map *map)
338 map->elems = NULL;
339 map->nelems = 0;
340 map->freelist = -1;
343 static void lo_map_destroy(struct lo_map *map)
345 free(map->elems);
348 static int lo_map_grow(struct lo_map *map, size_t new_nelems)
350 struct lo_map_elem *new_elems;
351 size_t i;
353 if (new_nelems <= map->nelems) {
354 return 1;
357 new_elems = realloc(map->elems, sizeof(map->elems[0]) * new_nelems);
358 if (!new_elems) {
359 return 0;
362 for (i = map->nelems; i < new_nelems; i++) {
363 new_elems[i].freelist = i + 1;
364 new_elems[i].in_use = false;
366 new_elems[new_nelems - 1].freelist = -1;
368 map->elems = new_elems;
369 map->freelist = map->nelems;
370 map->nelems = new_nelems;
371 return 1;
374 static struct lo_map_elem *lo_map_alloc_elem(struct lo_map *map)
376 struct lo_map_elem *elem;
378 if (map->freelist == -1 && !lo_map_grow(map, map->nelems + 256)) {
379 return NULL;
382 elem = &map->elems[map->freelist];
383 map->freelist = elem->freelist;
385 elem->in_use = true;
387 return elem;
390 static struct lo_map_elem *lo_map_reserve(struct lo_map *map, size_t key)
392 ssize_t *prev;
394 if (!lo_map_grow(map, key + 1)) {
395 return NULL;
398 for (prev = &map->freelist; *prev != -1;
399 prev = &map->elems[*prev].freelist) {
400 if (*prev == key) {
401 struct lo_map_elem *elem = &map->elems[key];
403 *prev = elem->freelist;
404 elem->in_use = true;
405 return elem;
408 return NULL;
411 static struct lo_map_elem *lo_map_get(struct lo_map *map, size_t key)
413 if (key >= map->nelems) {
414 return NULL;
416 if (!map->elems[key].in_use) {
417 return NULL;
419 return &map->elems[key];
422 static void lo_map_remove(struct lo_map *map, size_t key)
424 struct lo_map_elem *elem;
426 if (key >= map->nelems) {
427 return;
430 elem = &map->elems[key];
431 if (!elem->in_use) {
432 return;
435 elem->in_use = false;
437 elem->freelist = map->freelist;
438 map->freelist = key;
441 /* Assumes lo->mutex is held */
442 static ssize_t lo_add_fd_mapping(fuse_req_t req, int fd)
444 struct lo_map_elem *elem;
446 elem = lo_map_alloc_elem(&lo_data(req)->fd_map);
447 if (!elem) {
448 return -1;
451 elem->fd = fd;
452 return elem - lo_data(req)->fd_map.elems;
455 /* Assumes lo->mutex is held */
456 static ssize_t lo_add_dirp_mapping(fuse_req_t req, struct lo_dirp *dirp)
458 struct lo_map_elem *elem;
460 elem = lo_map_alloc_elem(&lo_data(req)->dirp_map);
461 if (!elem) {
462 return -1;
465 elem->dirp = dirp;
466 return elem - lo_data(req)->dirp_map.elems;
469 /* Assumes lo->mutex is held */
470 static ssize_t lo_add_inode_mapping(fuse_req_t req, struct lo_inode *inode)
472 struct lo_map_elem *elem;
474 elem = lo_map_alloc_elem(&lo_data(req)->ino_map);
475 if (!elem) {
476 return -1;
479 elem->inode = inode;
480 return elem - lo_data(req)->ino_map.elems;
483 static void lo_inode_put(struct lo_data *lo, struct lo_inode **inodep)
485 struct lo_inode *inode = *inodep;
487 if (!inode) {
488 return;
491 *inodep = NULL;
493 if (g_atomic_int_dec_and_test(&inode->refcount)) {
494 close(inode->fd);
495 free(inode);
499 /* Caller must release refcount using lo_inode_put() */
500 static struct lo_inode *lo_inode(fuse_req_t req, fuse_ino_t ino)
502 struct lo_data *lo = lo_data(req);
503 struct lo_map_elem *elem;
505 pthread_mutex_lock(&lo->mutex);
506 elem = lo_map_get(&lo->ino_map, ino);
507 if (elem) {
508 g_atomic_int_inc(&elem->inode->refcount);
510 pthread_mutex_unlock(&lo->mutex);
512 if (!elem) {
513 return NULL;
516 return elem->inode;
520 * TODO Remove this helper and force callers to hold an inode refcount until
521 * they are done with the fd. This will be done in a later patch to make
522 * review easier.
524 static int lo_fd(fuse_req_t req, fuse_ino_t ino)
526 struct lo_inode *inode = lo_inode(req, ino);
527 int fd;
529 if (!inode) {
530 return -1;
533 fd = inode->fd;
534 lo_inode_put(lo_data(req), &inode);
535 return fd;
538 static void lo_init(void *userdata, struct fuse_conn_info *conn)
540 struct lo_data *lo = (struct lo_data *)userdata;
542 if (conn->capable & FUSE_CAP_EXPORT_SUPPORT) {
543 conn->want |= FUSE_CAP_EXPORT_SUPPORT;
546 if (lo->writeback && conn->capable & FUSE_CAP_WRITEBACK_CACHE) {
547 fuse_log(FUSE_LOG_DEBUG, "lo_init: activating writeback\n");
548 conn->want |= FUSE_CAP_WRITEBACK_CACHE;
550 if (conn->capable & FUSE_CAP_FLOCK_LOCKS) {
551 if (lo->flock) {
552 fuse_log(FUSE_LOG_DEBUG, "lo_init: activating flock locks\n");
553 conn->want |= FUSE_CAP_FLOCK_LOCKS;
554 } else {
555 fuse_log(FUSE_LOG_DEBUG, "lo_init: disabling flock locks\n");
556 conn->want &= ~FUSE_CAP_FLOCK_LOCKS;
560 if (conn->capable & FUSE_CAP_POSIX_LOCKS) {
561 if (lo->posix_lock) {
562 fuse_log(FUSE_LOG_DEBUG, "lo_init: activating posix locks\n");
563 conn->want |= FUSE_CAP_POSIX_LOCKS;
564 } else {
565 fuse_log(FUSE_LOG_DEBUG, "lo_init: disabling posix locks\n");
566 conn->want &= ~FUSE_CAP_POSIX_LOCKS;
570 if ((lo->cache == CACHE_NONE && !lo->readdirplus_set) ||
571 lo->readdirplus_clear) {
572 fuse_log(FUSE_LOG_DEBUG, "lo_init: disabling readdirplus\n");
573 conn->want &= ~FUSE_CAP_READDIRPLUS;
577 static void lo_getattr(fuse_req_t req, fuse_ino_t ino,
578 struct fuse_file_info *fi)
580 int res;
581 struct stat buf;
582 struct lo_data *lo = lo_data(req);
584 (void)fi;
586 res =
587 fstatat(lo_fd(req, ino), "", &buf, AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW);
588 if (res == -1) {
589 return (void)fuse_reply_err(req, errno);
592 fuse_reply_attr(req, &buf, lo->timeout);
596 * Increments parent->nlookup and caller must release refcount using
597 * lo_inode_put(&parent).
599 static int lo_parent_and_name(struct lo_data *lo, struct lo_inode *inode,
600 char path[PATH_MAX], struct lo_inode **parent)
602 char procname[64];
603 char *last;
604 struct stat stat;
605 struct lo_inode *p;
606 int retries = 2;
607 int res;
609 retry:
610 sprintf(procname, "%i", inode->fd);
612 res = readlinkat(lo->proc_self_fd, procname, path, PATH_MAX);
613 if (res < 0) {
614 fuse_log(FUSE_LOG_WARNING, "%s: readlink failed: %m\n", __func__);
615 goto fail_noretry;
618 if (res >= PATH_MAX) {
619 fuse_log(FUSE_LOG_WARNING, "%s: readlink overflowed\n", __func__);
620 goto fail_noretry;
622 path[res] = '\0';
624 last = strrchr(path, '/');
625 if (last == NULL) {
626 /* Shouldn't happen */
627 fuse_log(
628 FUSE_LOG_WARNING,
629 "%s: INTERNAL ERROR: bad path read from proc\n", __func__);
630 goto fail_noretry;
632 if (last == path) {
633 p = &lo->root;
634 pthread_mutex_lock(&lo->mutex);
635 p->nlookup++;
636 g_atomic_int_inc(&p->refcount);
637 pthread_mutex_unlock(&lo->mutex);
638 } else {
639 *last = '\0';
640 res = fstatat(AT_FDCWD, last == path ? "/" : path, &stat, 0);
641 if (res == -1) {
642 if (!retries) {
643 fuse_log(FUSE_LOG_WARNING,
644 "%s: failed to stat parent: %m\n", __func__);
646 goto fail;
648 p = lo_find(lo, &stat);
649 if (p == NULL) {
650 if (!retries) {
651 fuse_log(FUSE_LOG_WARNING,
652 "%s: failed to find parent\n", __func__);
654 goto fail;
657 last++;
658 res = fstatat(p->fd, last, &stat, AT_SYMLINK_NOFOLLOW);
659 if (res == -1) {
660 if (!retries) {
661 fuse_log(FUSE_LOG_WARNING,
662 "%s: failed to stat last\n", __func__);
664 goto fail_unref;
666 if (stat.st_dev != inode->key.dev || stat.st_ino != inode->key.ino) {
667 if (!retries) {
668 fuse_log(FUSE_LOG_WARNING,
669 "%s: failed to match last\n", __func__);
671 goto fail_unref;
673 *parent = p;
674 memmove(path, last, strlen(last) + 1);
676 return 0;
678 fail_unref:
679 unref_inode_lolocked(lo, p, 1);
680 lo_inode_put(lo, &p);
681 fail:
682 if (retries) {
683 retries--;
684 goto retry;
686 fail_noretry:
687 errno = EIO;
688 return -1;
691 static int utimensat_empty(struct lo_data *lo, struct lo_inode *inode,
692 const struct timespec *tv)
694 int res;
695 struct lo_inode *parent;
696 char path[PATH_MAX];
698 if (S_ISLNK(inode->filetype)) {
699 res = utimensat(inode->fd, "", tv, AT_EMPTY_PATH);
700 if (res == -1 && errno == EINVAL) {
701 /* Sorry, no race free way to set times on symlink. */
702 if (lo->norace) {
703 errno = EPERM;
704 } else {
705 goto fallback;
708 return res;
710 sprintf(path, "%i", inode->fd);
712 return utimensat(lo->proc_self_fd, path, tv, 0);
714 fallback:
715 res = lo_parent_and_name(lo, inode, path, &parent);
716 if (res != -1) {
717 res = utimensat(parent->fd, path, tv, AT_SYMLINK_NOFOLLOW);
718 unref_inode_lolocked(lo, parent, 1);
719 lo_inode_put(lo, &parent);
722 return res;
725 static int lo_fi_fd(fuse_req_t req, struct fuse_file_info *fi)
727 struct lo_data *lo = lo_data(req);
728 struct lo_map_elem *elem;
730 pthread_mutex_lock(&lo->mutex);
731 elem = lo_map_get(&lo->fd_map, fi->fh);
732 pthread_mutex_unlock(&lo->mutex);
734 if (!elem) {
735 return -1;
738 return elem->fd;
741 static void lo_setattr(fuse_req_t req, fuse_ino_t ino, struct stat *attr,
742 int valid, struct fuse_file_info *fi)
744 int saverr;
745 char procname[64];
746 struct lo_data *lo = lo_data(req);
747 struct lo_inode *inode;
748 int ifd;
749 int res;
750 int fd;
752 inode = lo_inode(req, ino);
753 if (!inode) {
754 fuse_reply_err(req, EBADF);
755 return;
758 ifd = inode->fd;
760 /* If fi->fh is invalid we'll report EBADF later */
761 if (fi) {
762 fd = lo_fi_fd(req, fi);
765 if (valid & FUSE_SET_ATTR_MODE) {
766 if (fi) {
767 res = fchmod(fd, attr->st_mode);
768 } else {
769 sprintf(procname, "%i", ifd);
770 res = fchmodat(lo->proc_self_fd, procname, attr->st_mode, 0);
772 if (res == -1) {
773 goto out_err;
776 if (valid & (FUSE_SET_ATTR_UID | FUSE_SET_ATTR_GID)) {
777 uid_t uid = (valid & FUSE_SET_ATTR_UID) ? attr->st_uid : (uid_t)-1;
778 gid_t gid = (valid & FUSE_SET_ATTR_GID) ? attr->st_gid : (gid_t)-1;
780 res = fchownat(ifd, "", uid, gid, AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW);
781 if (res == -1) {
782 goto out_err;
785 if (valid & FUSE_SET_ATTR_SIZE) {
786 int truncfd;
788 if (fi) {
789 truncfd = fd;
790 } else {
791 sprintf(procname, "%i", ifd);
792 truncfd = openat(lo->proc_self_fd, procname, O_RDWR);
793 if (truncfd < 0) {
794 goto out_err;
798 res = ftruncate(truncfd, attr->st_size);
799 if (!fi) {
800 saverr = errno;
801 close(truncfd);
802 errno = saverr;
804 if (res == -1) {
805 goto out_err;
808 if (valid & (FUSE_SET_ATTR_ATIME | FUSE_SET_ATTR_MTIME)) {
809 struct timespec tv[2];
811 tv[0].tv_sec = 0;
812 tv[1].tv_sec = 0;
813 tv[0].tv_nsec = UTIME_OMIT;
814 tv[1].tv_nsec = UTIME_OMIT;
816 if (valid & FUSE_SET_ATTR_ATIME_NOW) {
817 tv[0].tv_nsec = UTIME_NOW;
818 } else if (valid & FUSE_SET_ATTR_ATIME) {
819 tv[0] = attr->st_atim;
822 if (valid & FUSE_SET_ATTR_MTIME_NOW) {
823 tv[1].tv_nsec = UTIME_NOW;
824 } else if (valid & FUSE_SET_ATTR_MTIME) {
825 tv[1] = attr->st_mtim;
828 if (fi) {
829 res = futimens(fd, tv);
830 } else {
831 res = utimensat_empty(lo, inode, tv);
833 if (res == -1) {
834 goto out_err;
837 lo_inode_put(lo, &inode);
839 return lo_getattr(req, ino, fi);
841 out_err:
842 saverr = errno;
843 lo_inode_put(lo, &inode);
844 fuse_reply_err(req, saverr);
847 static struct lo_inode *lo_find(struct lo_data *lo, struct stat *st)
849 struct lo_inode *p;
850 struct lo_key key = {
851 .ino = st->st_ino,
852 .dev = st->st_dev,
855 pthread_mutex_lock(&lo->mutex);
856 p = g_hash_table_lookup(lo->inodes, &key);
857 if (p) {
858 assert(p->nlookup > 0);
859 p->nlookup++;
860 g_atomic_int_inc(&p->refcount);
862 pthread_mutex_unlock(&lo->mutex);
864 return p;
867 /* value_destroy_func for posix_locks GHashTable */
868 static void posix_locks_value_destroy(gpointer data)
870 struct lo_inode_plock *plock = data;
873 * We had used open() for locks and had only one fd. So
874 * closing this fd should release all OFD locks.
876 close(plock->fd);
877 free(plock);
881 * Increments nlookup and caller must release refcount using
882 * lo_inode_put(&parent).
884 static int lo_do_lookup(fuse_req_t req, fuse_ino_t parent, const char *name,
885 struct fuse_entry_param *e)
887 int newfd;
888 int res;
889 int saverr;
890 struct lo_data *lo = lo_data(req);
891 struct lo_inode *inode = NULL;
892 struct lo_inode *dir = lo_inode(req, parent);
895 * name_to_handle_at() and open_by_handle_at() can reach here with fuse
896 * mount point in guest, but we don't have its inode info in the
897 * ino_map.
899 if (!dir) {
900 return ENOENT;
903 memset(e, 0, sizeof(*e));
904 e->attr_timeout = lo->timeout;
905 e->entry_timeout = lo->timeout;
907 /* Do not allow escaping root directory */
908 if (dir == &lo->root && strcmp(name, "..") == 0) {
909 name = ".";
912 newfd = openat(dir->fd, name, O_PATH | O_NOFOLLOW);
913 if (newfd == -1) {
914 goto out_err;
917 res = fstatat(newfd, "", &e->attr, AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW);
918 if (res == -1) {
919 goto out_err;
922 inode = lo_find(lo, &e->attr);
923 if (inode) {
924 close(newfd);
925 } else {
926 inode = calloc(1, sizeof(struct lo_inode));
927 if (!inode) {
928 goto out_err;
931 /* cache only filetype */
932 inode->filetype = (e->attr.st_mode & S_IFMT);
935 * One for the caller and one for nlookup (released in
936 * unref_inode_lolocked())
938 g_atomic_int_set(&inode->refcount, 2);
940 inode->nlookup = 1;
941 inode->fd = newfd;
942 inode->key.ino = e->attr.st_ino;
943 inode->key.dev = e->attr.st_dev;
944 pthread_mutex_init(&inode->plock_mutex, NULL);
945 inode->posix_locks = g_hash_table_new_full(
946 g_direct_hash, g_direct_equal, NULL, posix_locks_value_destroy);
948 pthread_mutex_lock(&lo->mutex);
949 inode->fuse_ino = lo_add_inode_mapping(req, inode);
950 g_hash_table_insert(lo->inodes, &inode->key, inode);
951 pthread_mutex_unlock(&lo->mutex);
953 e->ino = inode->fuse_ino;
954 lo_inode_put(lo, &inode);
955 lo_inode_put(lo, &dir);
957 fuse_log(FUSE_LOG_DEBUG, " %lli/%s -> %lli\n", (unsigned long long)parent,
958 name, (unsigned long long)e->ino);
960 return 0;
962 out_err:
963 saverr = errno;
964 if (newfd != -1) {
965 close(newfd);
967 lo_inode_put(lo, &inode);
968 lo_inode_put(lo, &dir);
969 return saverr;
972 static void lo_lookup(fuse_req_t req, fuse_ino_t parent, const char *name)
974 struct fuse_entry_param e;
975 int err;
977 fuse_log(FUSE_LOG_DEBUG, "lo_lookup(parent=%" PRIu64 ", name=%s)\n", parent,
978 name);
981 * Don't use is_safe_path_component(), allow "." and ".." for NFS export
982 * support.
984 if (strchr(name, '/')) {
985 fuse_reply_err(req, EINVAL);
986 return;
989 err = lo_do_lookup(req, parent, name, &e);
990 if (err) {
991 fuse_reply_err(req, err);
992 } else {
993 fuse_reply_entry(req, &e);
998 * On some archs, setres*id is limited to 2^16 but they
999 * provide setres*id32 variants that allow 2^32.
1000 * Others just let setres*id do 2^32 anyway.
1002 #ifdef SYS_setresgid32
1003 #define OURSYS_setresgid SYS_setresgid32
1004 #else
1005 #define OURSYS_setresgid SYS_setresgid
1006 #endif
1008 #ifdef SYS_setresuid32
1009 #define OURSYS_setresuid SYS_setresuid32
1010 #else
1011 #define OURSYS_setresuid SYS_setresuid
1012 #endif
1015 * Change to uid/gid of caller so that file is created with
1016 * ownership of caller.
1017 * TODO: What about selinux context?
1019 static int lo_change_cred(fuse_req_t req, struct lo_cred *old)
1021 int res;
1023 old->euid = geteuid();
1024 old->egid = getegid();
1026 res = syscall(OURSYS_setresgid, -1, fuse_req_ctx(req)->gid, -1);
1027 if (res == -1) {
1028 return errno;
1031 res = syscall(OURSYS_setresuid, -1, fuse_req_ctx(req)->uid, -1);
1032 if (res == -1) {
1033 int errno_save = errno;
1035 syscall(OURSYS_setresgid, -1, old->egid, -1);
1036 return errno_save;
1039 return 0;
1042 /* Regain Privileges */
1043 static void lo_restore_cred(struct lo_cred *old)
1045 int res;
1047 res = syscall(OURSYS_setresuid, -1, old->euid, -1);
1048 if (res == -1) {
1049 fuse_log(FUSE_LOG_ERR, "seteuid(%u): %m\n", old->euid);
1050 exit(1);
1053 res = syscall(OURSYS_setresgid, -1, old->egid, -1);
1054 if (res == -1) {
1055 fuse_log(FUSE_LOG_ERR, "setegid(%u): %m\n", old->egid);
1056 exit(1);
1060 static void lo_mknod_symlink(fuse_req_t req, fuse_ino_t parent,
1061 const char *name, mode_t mode, dev_t rdev,
1062 const char *link)
1064 int res;
1065 int saverr;
1066 struct lo_data *lo = lo_data(req);
1067 struct lo_inode *dir;
1068 struct fuse_entry_param e;
1069 struct lo_cred old = {};
1071 if (!is_safe_path_component(name)) {
1072 fuse_reply_err(req, EINVAL);
1073 return;
1076 dir = lo_inode(req, parent);
1077 if (!dir) {
1078 fuse_reply_err(req, EBADF);
1079 return;
1082 saverr = lo_change_cred(req, &old);
1083 if (saverr) {
1084 goto out;
1087 res = mknod_wrapper(dir->fd, name, link, mode, rdev);
1089 saverr = errno;
1091 lo_restore_cred(&old);
1093 if (res == -1) {
1094 goto out;
1097 saverr = lo_do_lookup(req, parent, name, &e);
1098 if (saverr) {
1099 goto out;
1102 fuse_log(FUSE_LOG_DEBUG, " %lli/%s -> %lli\n", (unsigned long long)parent,
1103 name, (unsigned long long)e.ino);
1105 fuse_reply_entry(req, &e);
1106 lo_inode_put(lo, &dir);
1107 return;
1109 out:
1110 lo_inode_put(lo, &dir);
1111 fuse_reply_err(req, saverr);
1114 static void lo_mknod(fuse_req_t req, fuse_ino_t parent, const char *name,
1115 mode_t mode, dev_t rdev)
1117 lo_mknod_symlink(req, parent, name, mode, rdev, NULL);
1120 static void lo_mkdir(fuse_req_t req, fuse_ino_t parent, const char *name,
1121 mode_t mode)
1123 lo_mknod_symlink(req, parent, name, S_IFDIR | mode, 0, NULL);
1126 static void lo_symlink(fuse_req_t req, const char *link, fuse_ino_t parent,
1127 const char *name)
1129 lo_mknod_symlink(req, parent, name, S_IFLNK, 0, link);
1132 static int linkat_empty_nofollow(struct lo_data *lo, struct lo_inode *inode,
1133 int dfd, const char *name)
1135 int res;
1136 struct lo_inode *parent;
1137 char path[PATH_MAX];
1139 if (S_ISLNK(inode->filetype)) {
1140 res = linkat(inode->fd, "", dfd, name, AT_EMPTY_PATH);
1141 if (res == -1 && (errno == ENOENT || errno == EINVAL)) {
1142 /* Sorry, no race free way to hard-link a symlink. */
1143 if (lo->norace) {
1144 errno = EPERM;
1145 } else {
1146 goto fallback;
1149 return res;
1152 sprintf(path, "%i", inode->fd);
1154 return linkat(lo->proc_self_fd, path, dfd, name, AT_SYMLINK_FOLLOW);
1156 fallback:
1157 res = lo_parent_and_name(lo, inode, path, &parent);
1158 if (res != -1) {
1159 res = linkat(parent->fd, path, dfd, name, 0);
1160 unref_inode_lolocked(lo, parent, 1);
1161 lo_inode_put(lo, &parent);
1164 return res;
1167 static void lo_link(fuse_req_t req, fuse_ino_t ino, fuse_ino_t parent,
1168 const char *name)
1170 int res;
1171 struct lo_data *lo = lo_data(req);
1172 struct lo_inode *parent_inode;
1173 struct lo_inode *inode;
1174 struct fuse_entry_param e;
1175 int saverr;
1177 if (!is_safe_path_component(name)) {
1178 fuse_reply_err(req, EINVAL);
1179 return;
1182 parent_inode = lo_inode(req, parent);
1183 inode = lo_inode(req, ino);
1184 if (!parent_inode || !inode) {
1185 errno = EBADF;
1186 goto out_err;
1189 memset(&e, 0, sizeof(struct fuse_entry_param));
1190 e.attr_timeout = lo->timeout;
1191 e.entry_timeout = lo->timeout;
1193 res = linkat_empty_nofollow(lo, inode, parent_inode->fd, name);
1194 if (res == -1) {
1195 goto out_err;
1198 res = fstatat(inode->fd, "", &e.attr, AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW);
1199 if (res == -1) {
1200 goto out_err;
1203 pthread_mutex_lock(&lo->mutex);
1204 inode->nlookup++;
1205 pthread_mutex_unlock(&lo->mutex);
1206 e.ino = inode->fuse_ino;
1208 fuse_log(FUSE_LOG_DEBUG, " %lli/%s -> %lli\n", (unsigned long long)parent,
1209 name, (unsigned long long)e.ino);
1211 fuse_reply_entry(req, &e);
1212 lo_inode_put(lo, &parent_inode);
1213 lo_inode_put(lo, &inode);
1214 return;
1216 out_err:
1217 saverr = errno;
1218 lo_inode_put(lo, &parent_inode);
1219 lo_inode_put(lo, &inode);
1220 fuse_reply_err(req, saverr);
1223 /* Increments nlookup and caller must release refcount using lo_inode_put() */
1224 static struct lo_inode *lookup_name(fuse_req_t req, fuse_ino_t parent,
1225 const char *name)
1227 int res;
1228 struct stat attr;
1230 res = fstatat(lo_fd(req, parent), name, &attr,
1231 AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW);
1232 if (res == -1) {
1233 return NULL;
1236 return lo_find(lo_data(req), &attr);
1239 static void lo_rmdir(fuse_req_t req, fuse_ino_t parent, const char *name)
1241 int res;
1242 struct lo_inode *inode;
1243 struct lo_data *lo = lo_data(req);
1245 if (!is_safe_path_component(name)) {
1246 fuse_reply_err(req, EINVAL);
1247 return;
1250 inode = lookup_name(req, parent, name);
1251 if (!inode) {
1252 fuse_reply_err(req, EIO);
1253 return;
1256 res = unlinkat(lo_fd(req, parent), name, AT_REMOVEDIR);
1258 fuse_reply_err(req, res == -1 ? errno : 0);
1259 unref_inode_lolocked(lo, inode, 1);
1260 lo_inode_put(lo, &inode);
1263 static void lo_rename(fuse_req_t req, fuse_ino_t parent, const char *name,
1264 fuse_ino_t newparent, const char *newname,
1265 unsigned int flags)
1267 int res;
1268 struct lo_inode *parent_inode;
1269 struct lo_inode *newparent_inode;
1270 struct lo_inode *oldinode = NULL;
1271 struct lo_inode *newinode = NULL;
1272 struct lo_data *lo = lo_data(req);
1274 if (!is_safe_path_component(name) || !is_safe_path_component(newname)) {
1275 fuse_reply_err(req, EINVAL);
1276 return;
1279 parent_inode = lo_inode(req, parent);
1280 newparent_inode = lo_inode(req, newparent);
1281 if (!parent_inode || !newparent_inode) {
1282 fuse_reply_err(req, EBADF);
1283 goto out;
1286 oldinode = lookup_name(req, parent, name);
1287 newinode = lookup_name(req, newparent, newname);
1289 if (!oldinode) {
1290 fuse_reply_err(req, EIO);
1291 goto out;
1294 if (flags) {
1295 #ifndef SYS_renameat2
1296 fuse_reply_err(req, EINVAL);
1297 #else
1298 res = syscall(SYS_renameat2, parent_inode->fd, name,
1299 newparent_inode->fd, newname, flags);
1300 if (res == -1 && errno == ENOSYS) {
1301 fuse_reply_err(req, EINVAL);
1302 } else {
1303 fuse_reply_err(req, res == -1 ? errno : 0);
1305 #endif
1306 goto out;
1309 res = renameat(parent_inode->fd, name, newparent_inode->fd, newname);
1311 fuse_reply_err(req, res == -1 ? errno : 0);
1312 out:
1313 unref_inode_lolocked(lo, oldinode, 1);
1314 unref_inode_lolocked(lo, newinode, 1);
1315 lo_inode_put(lo, &oldinode);
1316 lo_inode_put(lo, &newinode);
1317 lo_inode_put(lo, &parent_inode);
1318 lo_inode_put(lo, &newparent_inode);
1321 static void lo_unlink(fuse_req_t req, fuse_ino_t parent, const char *name)
1323 int res;
1324 struct lo_inode *inode;
1325 struct lo_data *lo = lo_data(req);
1327 if (!is_safe_path_component(name)) {
1328 fuse_reply_err(req, EINVAL);
1329 return;
1332 inode = lookup_name(req, parent, name);
1333 if (!inode) {
1334 fuse_reply_err(req, EIO);
1335 return;
1338 res = unlinkat(lo_fd(req, parent), name, 0);
1340 fuse_reply_err(req, res == -1 ? errno : 0);
1341 unref_inode_lolocked(lo, inode, 1);
1342 lo_inode_put(lo, &inode);
1345 /* To be called with lo->mutex held */
1346 static void unref_inode(struct lo_data *lo, struct lo_inode *inode, uint64_t n)
1348 if (!inode) {
1349 return;
1352 assert(inode->nlookup >= n);
1353 inode->nlookup -= n;
1354 if (!inode->nlookup) {
1355 lo_map_remove(&lo->ino_map, inode->fuse_ino);
1356 g_hash_table_remove(lo->inodes, &inode->key);
1357 if (g_hash_table_size(inode->posix_locks)) {
1358 fuse_log(FUSE_LOG_WARNING, "Hash table is not empty\n");
1360 g_hash_table_destroy(inode->posix_locks);
1361 pthread_mutex_destroy(&inode->plock_mutex);
1363 /* Drop our refcount from lo_do_lookup() */
1364 lo_inode_put(lo, &inode);
1368 static void unref_inode_lolocked(struct lo_data *lo, struct lo_inode *inode,
1369 uint64_t n)
1371 if (!inode) {
1372 return;
1375 pthread_mutex_lock(&lo->mutex);
1376 unref_inode(lo, inode, n);
1377 pthread_mutex_unlock(&lo->mutex);
1380 static void lo_forget_one(fuse_req_t req, fuse_ino_t ino, uint64_t nlookup)
1382 struct lo_data *lo = lo_data(req);
1383 struct lo_inode *inode;
1385 inode = lo_inode(req, ino);
1386 if (!inode) {
1387 return;
1390 fuse_log(FUSE_LOG_DEBUG, " forget %lli %lli -%lli\n",
1391 (unsigned long long)ino, (unsigned long long)inode->nlookup,
1392 (unsigned long long)nlookup);
1394 unref_inode_lolocked(lo, inode, nlookup);
1395 lo_inode_put(lo, &inode);
1398 static void lo_forget(fuse_req_t req, fuse_ino_t ino, uint64_t nlookup)
1400 lo_forget_one(req, ino, nlookup);
1401 fuse_reply_none(req);
1404 static void lo_forget_multi(fuse_req_t req, size_t count,
1405 struct fuse_forget_data *forgets)
1407 int i;
1409 for (i = 0; i < count; i++) {
1410 lo_forget_one(req, forgets[i].ino, forgets[i].nlookup);
1412 fuse_reply_none(req);
1415 static void lo_readlink(fuse_req_t req, fuse_ino_t ino)
1417 char buf[PATH_MAX + 1];
1418 int res;
1420 res = readlinkat(lo_fd(req, ino), "", buf, sizeof(buf));
1421 if (res == -1) {
1422 return (void)fuse_reply_err(req, errno);
1425 if (res == sizeof(buf)) {
1426 return (void)fuse_reply_err(req, ENAMETOOLONG);
1429 buf[res] = '\0';
1431 fuse_reply_readlink(req, buf);
1434 struct lo_dirp {
1435 gint refcount;
1436 DIR *dp;
1437 struct dirent *entry;
1438 off_t offset;
1441 static void lo_dirp_put(struct lo_dirp **dp)
1443 struct lo_dirp *d = *dp;
1445 if (!d) {
1446 return;
1448 *dp = NULL;
1450 if (g_atomic_int_dec_and_test(&d->refcount)) {
1451 closedir(d->dp);
1452 free(d);
1456 /* Call lo_dirp_put() on the return value when no longer needed */
1457 static struct lo_dirp *lo_dirp(fuse_req_t req, struct fuse_file_info *fi)
1459 struct lo_data *lo = lo_data(req);
1460 struct lo_map_elem *elem;
1462 pthread_mutex_lock(&lo->mutex);
1463 elem = lo_map_get(&lo->dirp_map, fi->fh);
1464 if (elem) {
1465 g_atomic_int_inc(&elem->dirp->refcount);
1467 pthread_mutex_unlock(&lo->mutex);
1468 if (!elem) {
1469 return NULL;
1472 return elem->dirp;
1475 static void lo_opendir(fuse_req_t req, fuse_ino_t ino,
1476 struct fuse_file_info *fi)
1478 int error = ENOMEM;
1479 struct lo_data *lo = lo_data(req);
1480 struct lo_dirp *d;
1481 int fd;
1482 ssize_t fh;
1484 d = calloc(1, sizeof(struct lo_dirp));
1485 if (d == NULL) {
1486 goto out_err;
1489 fd = openat(lo_fd(req, ino), ".", O_RDONLY);
1490 if (fd == -1) {
1491 goto out_errno;
1494 d->dp = fdopendir(fd);
1495 if (d->dp == NULL) {
1496 goto out_errno;
1499 d->offset = 0;
1500 d->entry = NULL;
1502 g_atomic_int_set(&d->refcount, 1); /* paired with lo_releasedir() */
1503 pthread_mutex_lock(&lo->mutex);
1504 fh = lo_add_dirp_mapping(req, d);
1505 pthread_mutex_unlock(&lo->mutex);
1506 if (fh == -1) {
1507 goto out_err;
1510 fi->fh = fh;
1511 if (lo->cache == CACHE_ALWAYS) {
1512 fi->cache_readdir = 1;
1514 fuse_reply_open(req, fi);
1515 return;
1517 out_errno:
1518 error = errno;
1519 out_err:
1520 if (d) {
1521 if (d->dp) {
1522 closedir(d->dp);
1524 if (fd != -1) {
1525 close(fd);
1527 free(d);
1529 fuse_reply_err(req, error);
1532 static void lo_do_readdir(fuse_req_t req, fuse_ino_t ino, size_t size,
1533 off_t offset, struct fuse_file_info *fi, int plus)
1535 struct lo_data *lo = lo_data(req);
1536 struct lo_dirp *d = NULL;
1537 struct lo_inode *dinode;
1538 char *buf = NULL;
1539 char *p;
1540 size_t rem = size;
1541 int err = EBADF;
1543 dinode = lo_inode(req, ino);
1544 if (!dinode) {
1545 goto error;
1548 d = lo_dirp(req, fi);
1549 if (!d) {
1550 goto error;
1553 err = ENOMEM;
1554 buf = calloc(1, size);
1555 if (!buf) {
1556 goto error;
1558 p = buf;
1560 if (offset != d->offset) {
1561 seekdir(d->dp, offset);
1562 d->entry = NULL;
1563 d->offset = offset;
1565 while (1) {
1566 size_t entsize;
1567 off_t nextoff;
1568 const char *name;
1570 if (!d->entry) {
1571 errno = 0;
1572 d->entry = readdir(d->dp);
1573 if (!d->entry) {
1574 if (errno) { /* Error */
1575 err = errno;
1576 goto error;
1577 } else { /* End of stream */
1578 break;
1582 nextoff = d->entry->d_off;
1583 name = d->entry->d_name;
1585 fuse_ino_t entry_ino = 0;
1586 struct fuse_entry_param e = (struct fuse_entry_param){
1587 .attr.st_ino = d->entry->d_ino,
1588 .attr.st_mode = d->entry->d_type << 12,
1591 /* Hide root's parent directory */
1592 if (dinode == &lo->root && strcmp(name, "..") == 0) {
1593 e.attr.st_ino = lo->root.key.ino;
1594 e.attr.st_mode = DT_DIR << 12;
1597 if (plus) {
1598 if (!is_dot_or_dotdot(name)) {
1599 err = lo_do_lookup(req, ino, name, &e);
1600 if (err) {
1601 goto error;
1603 entry_ino = e.ino;
1606 entsize = fuse_add_direntry_plus(req, p, rem, name, &e, nextoff);
1607 } else {
1608 entsize = fuse_add_direntry(req, p, rem, name, &e.attr, nextoff);
1610 if (entsize > rem) {
1611 if (entry_ino != 0) {
1612 lo_forget_one(req, entry_ino, 1);
1614 break;
1617 p += entsize;
1618 rem -= entsize;
1620 d->entry = NULL;
1621 d->offset = nextoff;
1624 err = 0;
1625 error:
1626 lo_dirp_put(&d);
1627 lo_inode_put(lo, &dinode);
1630 * If there's an error, we can only signal it if we haven't stored
1631 * any entries yet - otherwise we'd end up with wrong lookup
1632 * counts for the entries that are already in the buffer. So we
1633 * return what we've collected until that point.
1635 if (err && rem == size) {
1636 fuse_reply_err(req, err);
1637 } else {
1638 fuse_reply_buf(req, buf, size - rem);
1640 free(buf);
1643 static void lo_readdir(fuse_req_t req, fuse_ino_t ino, size_t size,
1644 off_t offset, struct fuse_file_info *fi)
1646 lo_do_readdir(req, ino, size, offset, fi, 0);
1649 static void lo_readdirplus(fuse_req_t req, fuse_ino_t ino, size_t size,
1650 off_t offset, struct fuse_file_info *fi)
1652 lo_do_readdir(req, ino, size, offset, fi, 1);
1655 static void lo_releasedir(fuse_req_t req, fuse_ino_t ino,
1656 struct fuse_file_info *fi)
1658 struct lo_data *lo = lo_data(req);
1659 struct lo_map_elem *elem;
1660 struct lo_dirp *d;
1662 (void)ino;
1664 pthread_mutex_lock(&lo->mutex);
1665 elem = lo_map_get(&lo->dirp_map, fi->fh);
1666 if (!elem) {
1667 pthread_mutex_unlock(&lo->mutex);
1668 fuse_reply_err(req, EBADF);
1669 return;
1672 d = elem->dirp;
1673 lo_map_remove(&lo->dirp_map, fi->fh);
1674 pthread_mutex_unlock(&lo->mutex);
1676 lo_dirp_put(&d); /* paired with lo_opendir() */
1678 fuse_reply_err(req, 0);
1681 static void update_open_flags(int writeback, struct fuse_file_info *fi)
1684 * With writeback cache, kernel may send read requests even
1685 * when userspace opened write-only
1687 if (writeback && (fi->flags & O_ACCMODE) == O_WRONLY) {
1688 fi->flags &= ~O_ACCMODE;
1689 fi->flags |= O_RDWR;
1693 * With writeback cache, O_APPEND is handled by the kernel.
1694 * This breaks atomicity (since the file may change in the
1695 * underlying filesystem, so that the kernel's idea of the
1696 * end of the file isn't accurate anymore). In this example,
1697 * we just accept that. A more rigorous filesystem may want
1698 * to return an error here
1700 if (writeback && (fi->flags & O_APPEND)) {
1701 fi->flags &= ~O_APPEND;
1705 * O_DIRECT in guest should not necessarily mean bypassing page
1706 * cache on host as well. If somebody needs that behavior, it
1707 * probably should be a configuration knob in daemon.
1709 fi->flags &= ~O_DIRECT;
1712 static void lo_create(fuse_req_t req, fuse_ino_t parent, const char *name,
1713 mode_t mode, struct fuse_file_info *fi)
1715 int fd;
1716 struct lo_data *lo = lo_data(req);
1717 struct lo_inode *parent_inode;
1718 struct fuse_entry_param e;
1719 int err;
1720 struct lo_cred old = {};
1722 fuse_log(FUSE_LOG_DEBUG, "lo_create(parent=%" PRIu64 ", name=%s)\n", parent,
1723 name);
1725 if (!is_safe_path_component(name)) {
1726 fuse_reply_err(req, EINVAL);
1727 return;
1730 parent_inode = lo_inode(req, parent);
1731 if (!parent_inode) {
1732 fuse_reply_err(req, EBADF);
1733 return;
1736 err = lo_change_cred(req, &old);
1737 if (err) {
1738 goto out;
1741 update_open_flags(lo->writeback, fi);
1743 fd = openat(parent_inode->fd, name, (fi->flags | O_CREAT) & ~O_NOFOLLOW,
1744 mode);
1745 err = fd == -1 ? errno : 0;
1746 lo_restore_cred(&old);
1748 if (!err) {
1749 ssize_t fh;
1751 pthread_mutex_lock(&lo->mutex);
1752 fh = lo_add_fd_mapping(req, fd);
1753 pthread_mutex_unlock(&lo->mutex);
1754 if (fh == -1) {
1755 close(fd);
1756 err = ENOMEM;
1757 goto out;
1760 fi->fh = fh;
1761 err = lo_do_lookup(req, parent, name, &e);
1763 if (lo->cache == CACHE_NONE) {
1764 fi->direct_io = 1;
1765 } else if (lo->cache == CACHE_ALWAYS) {
1766 fi->keep_cache = 1;
1769 out:
1770 lo_inode_put(lo, &parent_inode);
1772 if (err) {
1773 fuse_reply_err(req, err);
1774 } else {
1775 fuse_reply_create(req, &e, fi);
1779 /* Should be called with inode->plock_mutex held */
1780 static struct lo_inode_plock *lookup_create_plock_ctx(struct lo_data *lo,
1781 struct lo_inode *inode,
1782 uint64_t lock_owner,
1783 pid_t pid, int *err)
1785 struct lo_inode_plock *plock;
1786 char procname[64];
1787 int fd;
1789 plock =
1790 g_hash_table_lookup(inode->posix_locks, GUINT_TO_POINTER(lock_owner));
1792 if (plock) {
1793 return plock;
1796 plock = malloc(sizeof(struct lo_inode_plock));
1797 if (!plock) {
1798 *err = ENOMEM;
1799 return NULL;
1802 /* Open another instance of file which can be used for ofd locks. */
1803 sprintf(procname, "%i", inode->fd);
1805 /* TODO: What if file is not writable? */
1806 fd = openat(lo->proc_self_fd, procname, O_RDWR);
1807 if (fd == -1) {
1808 *err = errno;
1809 free(plock);
1810 return NULL;
1813 plock->lock_owner = lock_owner;
1814 plock->fd = fd;
1815 g_hash_table_insert(inode->posix_locks, GUINT_TO_POINTER(plock->lock_owner),
1816 plock);
1817 return plock;
1820 static void lo_getlk(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi,
1821 struct flock *lock)
1823 struct lo_data *lo = lo_data(req);
1824 struct lo_inode *inode;
1825 struct lo_inode_plock *plock;
1826 int ret, saverr = 0;
1828 fuse_log(FUSE_LOG_DEBUG,
1829 "lo_getlk(ino=%" PRIu64 ", flags=%d)"
1830 " owner=0x%lx, l_type=%d l_start=0x%lx"
1831 " l_len=0x%lx\n",
1832 ino, fi->flags, fi->lock_owner, lock->l_type, lock->l_start,
1833 lock->l_len);
1835 inode = lo_inode(req, ino);
1836 if (!inode) {
1837 fuse_reply_err(req, EBADF);
1838 return;
1841 pthread_mutex_lock(&inode->plock_mutex);
1842 plock =
1843 lookup_create_plock_ctx(lo, inode, fi->lock_owner, lock->l_pid, &ret);
1844 if (!plock) {
1845 saverr = ret;
1846 goto out;
1849 ret = fcntl(plock->fd, F_OFD_GETLK, lock);
1850 if (ret == -1) {
1851 saverr = errno;
1854 out:
1855 pthread_mutex_unlock(&inode->plock_mutex);
1856 lo_inode_put(lo, &inode);
1858 if (saverr) {
1859 fuse_reply_err(req, saverr);
1860 } else {
1861 fuse_reply_lock(req, lock);
1865 static void lo_setlk(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi,
1866 struct flock *lock, int sleep)
1868 struct lo_data *lo = lo_data(req);
1869 struct lo_inode *inode;
1870 struct lo_inode_plock *plock;
1871 int ret, saverr = 0;
1873 fuse_log(FUSE_LOG_DEBUG,
1874 "lo_setlk(ino=%" PRIu64 ", flags=%d)"
1875 " cmd=%d pid=%d owner=0x%lx sleep=%d l_whence=%d"
1876 " l_start=0x%lx l_len=0x%lx\n",
1877 ino, fi->flags, lock->l_type, lock->l_pid, fi->lock_owner, sleep,
1878 lock->l_whence, lock->l_start, lock->l_len);
1880 if (sleep) {
1881 fuse_reply_err(req, EOPNOTSUPP);
1882 return;
1885 inode = lo_inode(req, ino);
1886 if (!inode) {
1887 fuse_reply_err(req, EBADF);
1888 return;
1891 pthread_mutex_lock(&inode->plock_mutex);
1892 plock =
1893 lookup_create_plock_ctx(lo, inode, fi->lock_owner, lock->l_pid, &ret);
1895 if (!plock) {
1896 saverr = ret;
1897 goto out;
1900 /* TODO: Is it alright to modify flock? */
1901 lock->l_pid = 0;
1902 ret = fcntl(plock->fd, F_OFD_SETLK, lock);
1903 if (ret == -1) {
1904 saverr = errno;
1907 out:
1908 pthread_mutex_unlock(&inode->plock_mutex);
1909 lo_inode_put(lo, &inode);
1911 fuse_reply_err(req, saverr);
1914 static void lo_fsyncdir(fuse_req_t req, fuse_ino_t ino, int datasync,
1915 struct fuse_file_info *fi)
1917 int res;
1918 struct lo_dirp *d;
1919 int fd;
1921 (void)ino;
1923 d = lo_dirp(req, fi);
1924 if (!d) {
1925 fuse_reply_err(req, EBADF);
1926 return;
1929 fd = dirfd(d->dp);
1930 if (datasync) {
1931 res = fdatasync(fd);
1932 } else {
1933 res = fsync(fd);
1936 lo_dirp_put(&d);
1938 fuse_reply_err(req, res == -1 ? errno : 0);
1941 static void lo_open(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi)
1943 int fd;
1944 ssize_t fh;
1945 char buf[64];
1946 struct lo_data *lo = lo_data(req);
1948 fuse_log(FUSE_LOG_DEBUG, "lo_open(ino=%" PRIu64 ", flags=%d)\n", ino,
1949 fi->flags);
1951 update_open_flags(lo->writeback, fi);
1953 sprintf(buf, "%i", lo_fd(req, ino));
1954 fd = openat(lo->proc_self_fd, buf, fi->flags & ~O_NOFOLLOW);
1955 if (fd == -1) {
1956 return (void)fuse_reply_err(req, errno);
1959 pthread_mutex_lock(&lo->mutex);
1960 fh = lo_add_fd_mapping(req, fd);
1961 pthread_mutex_unlock(&lo->mutex);
1962 if (fh == -1) {
1963 close(fd);
1964 fuse_reply_err(req, ENOMEM);
1965 return;
1968 fi->fh = fh;
1969 if (lo->cache == CACHE_NONE) {
1970 fi->direct_io = 1;
1971 } else if (lo->cache == CACHE_ALWAYS) {
1972 fi->keep_cache = 1;
1974 fuse_reply_open(req, fi);
1977 static void lo_release(fuse_req_t req, fuse_ino_t ino,
1978 struct fuse_file_info *fi)
1980 struct lo_data *lo = lo_data(req);
1981 struct lo_map_elem *elem;
1982 int fd = -1;
1984 (void)ino;
1986 pthread_mutex_lock(&lo->mutex);
1987 elem = lo_map_get(&lo->fd_map, fi->fh);
1988 if (elem) {
1989 fd = elem->fd;
1990 elem = NULL;
1991 lo_map_remove(&lo->fd_map, fi->fh);
1993 pthread_mutex_unlock(&lo->mutex);
1995 close(fd);
1996 fuse_reply_err(req, 0);
1999 static void lo_flush(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi)
2001 int res;
2002 (void)ino;
2003 struct lo_inode *inode;
2005 inode = lo_inode(req, ino);
2006 if (!inode) {
2007 fuse_reply_err(req, EBADF);
2008 return;
2011 /* An fd is going away. Cleanup associated posix locks */
2012 pthread_mutex_lock(&inode->plock_mutex);
2013 g_hash_table_remove(inode->posix_locks, GUINT_TO_POINTER(fi->lock_owner));
2014 pthread_mutex_unlock(&inode->plock_mutex);
2016 res = close(dup(lo_fi_fd(req, fi)));
2017 lo_inode_put(lo_data(req), &inode);
2018 fuse_reply_err(req, res == -1 ? errno : 0);
2021 static void lo_fsync(fuse_req_t req, fuse_ino_t ino, int datasync,
2022 struct fuse_file_info *fi)
2024 int res;
2025 int fd;
2026 char *buf;
2028 fuse_log(FUSE_LOG_DEBUG, "lo_fsync(ino=%" PRIu64 ", fi=0x%p)\n", ino,
2029 (void *)fi);
2031 if (!fi) {
2032 struct lo_data *lo = lo_data(req);
2034 res = asprintf(&buf, "%i", lo_fd(req, ino));
2035 if (res == -1) {
2036 return (void)fuse_reply_err(req, errno);
2039 fd = openat(lo->proc_self_fd, buf, O_RDWR);
2040 free(buf);
2041 if (fd == -1) {
2042 return (void)fuse_reply_err(req, errno);
2044 } else {
2045 fd = lo_fi_fd(req, fi);
2048 if (datasync) {
2049 res = fdatasync(fd);
2050 } else {
2051 res = fsync(fd);
2053 if (!fi) {
2054 close(fd);
2056 fuse_reply_err(req, res == -1 ? errno : 0);
2059 static void lo_read(fuse_req_t req, fuse_ino_t ino, size_t size, off_t offset,
2060 struct fuse_file_info *fi)
2062 struct fuse_bufvec buf = FUSE_BUFVEC_INIT(size);
2064 fuse_log(FUSE_LOG_DEBUG,
2065 "lo_read(ino=%" PRIu64 ", size=%zd, "
2066 "off=%lu)\n",
2067 ino, size, (unsigned long)offset);
2069 buf.buf[0].flags = FUSE_BUF_IS_FD | FUSE_BUF_FD_SEEK;
2070 buf.buf[0].fd = lo_fi_fd(req, fi);
2071 buf.buf[0].pos = offset;
2073 fuse_reply_data(req, &buf);
2076 static void lo_write_buf(fuse_req_t req, fuse_ino_t ino,
2077 struct fuse_bufvec *in_buf, off_t off,
2078 struct fuse_file_info *fi)
2080 (void)ino;
2081 ssize_t res;
2082 struct fuse_bufvec out_buf = FUSE_BUFVEC_INIT(fuse_buf_size(in_buf));
2083 bool cap_fsetid_dropped = false;
2085 out_buf.buf[0].flags = FUSE_BUF_IS_FD | FUSE_BUF_FD_SEEK;
2086 out_buf.buf[0].fd = lo_fi_fd(req, fi);
2087 out_buf.buf[0].pos = off;
2089 fuse_log(FUSE_LOG_DEBUG,
2090 "lo_write_buf(ino=%" PRIu64 ", size=%zd, off=%lu)\n", ino,
2091 out_buf.buf[0].size, (unsigned long)off);
2094 * If kill_priv is set, drop CAP_FSETID which should lead to kernel
2095 * clearing setuid/setgid on file.
2097 if (fi->kill_priv) {
2098 res = drop_effective_cap("FSETID", &cap_fsetid_dropped);
2099 if (res != 0) {
2100 fuse_reply_err(req, res);
2101 return;
2105 res = fuse_buf_copy(&out_buf, in_buf);
2106 if (res < 0) {
2107 fuse_reply_err(req, -res);
2108 } else {
2109 fuse_reply_write(req, (size_t)res);
2112 if (cap_fsetid_dropped) {
2113 res = gain_effective_cap("FSETID");
2114 if (res) {
2115 fuse_log(FUSE_LOG_ERR, "Failed to gain CAP_FSETID\n");
2120 static void lo_statfs(fuse_req_t req, fuse_ino_t ino)
2122 int res;
2123 struct statvfs stbuf;
2125 res = fstatvfs(lo_fd(req, ino), &stbuf);
2126 if (res == -1) {
2127 fuse_reply_err(req, errno);
2128 } else {
2129 fuse_reply_statfs(req, &stbuf);
2133 static void lo_fallocate(fuse_req_t req, fuse_ino_t ino, int mode, off_t offset,
2134 off_t length, struct fuse_file_info *fi)
2136 int err = EOPNOTSUPP;
2137 (void)ino;
2139 #ifdef CONFIG_FALLOCATE
2140 err = fallocate(lo_fi_fd(req, fi), mode, offset, length);
2141 if (err < 0) {
2142 err = errno;
2145 #elif defined(CONFIG_POSIX_FALLOCATE)
2146 if (mode) {
2147 fuse_reply_err(req, EOPNOTSUPP);
2148 return;
2151 err = posix_fallocate(lo_fi_fd(req, fi), offset, length);
2152 #endif
2154 fuse_reply_err(req, err);
2157 static void lo_flock(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi,
2158 int op)
2160 int res;
2161 (void)ino;
2163 res = flock(lo_fi_fd(req, fi), op);
2165 fuse_reply_err(req, res == -1 ? errno : 0);
2168 static void lo_getxattr(fuse_req_t req, fuse_ino_t ino, const char *name,
2169 size_t size)
2171 struct lo_data *lo = lo_data(req);
2172 char *value = NULL;
2173 char procname[64];
2174 struct lo_inode *inode;
2175 ssize_t ret;
2176 int saverr;
2177 int fd = -1;
2179 inode = lo_inode(req, ino);
2180 if (!inode) {
2181 fuse_reply_err(req, EBADF);
2182 return;
2185 saverr = ENOSYS;
2186 if (!lo_data(req)->xattr) {
2187 goto out;
2190 fuse_log(FUSE_LOG_DEBUG, "lo_getxattr(ino=%" PRIu64 ", name=%s size=%zd)\n",
2191 ino, name, size);
2193 if (size) {
2194 value = malloc(size);
2195 if (!value) {
2196 goto out_err;
2200 sprintf(procname, "%i", inode->fd);
2202 * It is not safe to open() non-regular/non-dir files in file server
2203 * unless O_PATH is used, so use that method for regular files/dir
2204 * only (as it seems giving less performance overhead).
2205 * Otherwise, call fchdir() to avoid open().
2207 if (S_ISREG(inode->filetype) || S_ISDIR(inode->filetype)) {
2208 fd = openat(lo->proc_self_fd, procname, O_RDONLY);
2209 if (fd < 0) {
2210 goto out_err;
2212 ret = fgetxattr(fd, name, value, size);
2213 } else {
2214 /* fchdir should not fail here */
2215 assert(fchdir(lo->proc_self_fd) == 0);
2216 ret = getxattr(procname, name, value, size);
2217 assert(fchdir(lo->root.fd) == 0);
2220 if (ret == -1) {
2221 goto out_err;
2223 if (size) {
2224 saverr = 0;
2225 if (ret == 0) {
2226 goto out;
2228 fuse_reply_buf(req, value, ret);
2229 } else {
2230 fuse_reply_xattr(req, ret);
2232 out_free:
2233 free(value);
2235 if (fd >= 0) {
2236 close(fd);
2239 lo_inode_put(lo, &inode);
2240 return;
2242 out_err:
2243 saverr = errno;
2244 out:
2245 fuse_reply_err(req, saverr);
2246 goto out_free;
2249 static void lo_listxattr(fuse_req_t req, fuse_ino_t ino, size_t size)
2251 struct lo_data *lo = lo_data(req);
2252 char *value = NULL;
2253 char procname[64];
2254 struct lo_inode *inode;
2255 ssize_t ret;
2256 int saverr;
2257 int fd = -1;
2259 inode = lo_inode(req, ino);
2260 if (!inode) {
2261 fuse_reply_err(req, EBADF);
2262 return;
2265 saverr = ENOSYS;
2266 if (!lo_data(req)->xattr) {
2267 goto out;
2270 fuse_log(FUSE_LOG_DEBUG, "lo_listxattr(ino=%" PRIu64 ", size=%zd)\n", ino,
2271 size);
2273 if (size) {
2274 value = malloc(size);
2275 if (!value) {
2276 goto out_err;
2280 sprintf(procname, "%i", inode->fd);
2281 if (S_ISREG(inode->filetype) || S_ISDIR(inode->filetype)) {
2282 fd = openat(lo->proc_self_fd, procname, O_RDONLY);
2283 if (fd < 0) {
2284 goto out_err;
2286 ret = flistxattr(fd, value, size);
2287 } else {
2288 /* fchdir should not fail here */
2289 assert(fchdir(lo->proc_self_fd) == 0);
2290 ret = listxattr(procname, value, size);
2291 assert(fchdir(lo->root.fd) == 0);
2294 if (ret == -1) {
2295 goto out_err;
2297 if (size) {
2298 saverr = 0;
2299 if (ret == 0) {
2300 goto out;
2302 fuse_reply_buf(req, value, ret);
2303 } else {
2304 fuse_reply_xattr(req, ret);
2306 out_free:
2307 free(value);
2309 if (fd >= 0) {
2310 close(fd);
2313 lo_inode_put(lo, &inode);
2314 return;
2316 out_err:
2317 saverr = errno;
2318 out:
2319 fuse_reply_err(req, saverr);
2320 goto out_free;
2323 static void lo_setxattr(fuse_req_t req, fuse_ino_t ino, const char *name,
2324 const char *value, size_t size, int flags)
2326 char procname[64];
2327 struct lo_data *lo = lo_data(req);
2328 struct lo_inode *inode;
2329 ssize_t ret;
2330 int saverr;
2331 int fd = -1;
2333 inode = lo_inode(req, ino);
2334 if (!inode) {
2335 fuse_reply_err(req, EBADF);
2336 return;
2339 saverr = ENOSYS;
2340 if (!lo_data(req)->xattr) {
2341 goto out;
2344 fuse_log(FUSE_LOG_DEBUG, "lo_setxattr(ino=%" PRIu64
2345 ", name=%s value=%s size=%zd)\n", ino, name, value, size);
2347 sprintf(procname, "%i", inode->fd);
2348 if (S_ISREG(inode->filetype) || S_ISDIR(inode->filetype)) {
2349 fd = openat(lo->proc_self_fd, procname, O_RDONLY);
2350 if (fd < 0) {
2351 saverr = errno;
2352 goto out;
2354 ret = fsetxattr(fd, name, value, size, flags);
2355 } else {
2356 /* fchdir should not fail here */
2357 assert(fchdir(lo->proc_self_fd) == 0);
2358 ret = setxattr(procname, name, value, size, flags);
2359 assert(fchdir(lo->root.fd) == 0);
2362 saverr = ret == -1 ? errno : 0;
2364 out:
2365 if (fd >= 0) {
2366 close(fd);
2369 lo_inode_put(lo, &inode);
2370 fuse_reply_err(req, saverr);
2373 static void lo_removexattr(fuse_req_t req, fuse_ino_t ino, const char *name)
2375 char procname[64];
2376 struct lo_data *lo = lo_data(req);
2377 struct lo_inode *inode;
2378 ssize_t ret;
2379 int saverr;
2380 int fd = -1;
2382 inode = lo_inode(req, ino);
2383 if (!inode) {
2384 fuse_reply_err(req, EBADF);
2385 return;
2388 saverr = ENOSYS;
2389 if (!lo_data(req)->xattr) {
2390 goto out;
2393 fuse_log(FUSE_LOG_DEBUG, "lo_removexattr(ino=%" PRIu64 ", name=%s)\n", ino,
2394 name);
2396 sprintf(procname, "%i", inode->fd);
2397 if (S_ISREG(inode->filetype) || S_ISDIR(inode->filetype)) {
2398 fd = openat(lo->proc_self_fd, procname, O_RDONLY);
2399 if (fd < 0) {
2400 saverr = errno;
2401 goto out;
2403 ret = fremovexattr(fd, name);
2404 } else {
2405 /* fchdir should not fail here */
2406 assert(fchdir(lo->proc_self_fd) == 0);
2407 ret = removexattr(procname, name);
2408 assert(fchdir(lo->root.fd) == 0);
2411 saverr = ret == -1 ? errno : 0;
2413 out:
2414 if (fd >= 0) {
2415 close(fd);
2418 lo_inode_put(lo, &inode);
2419 fuse_reply_err(req, saverr);
2422 #ifdef HAVE_COPY_FILE_RANGE
2423 static void lo_copy_file_range(fuse_req_t req, fuse_ino_t ino_in, off_t off_in,
2424 struct fuse_file_info *fi_in, fuse_ino_t ino_out,
2425 off_t off_out, struct fuse_file_info *fi_out,
2426 size_t len, int flags)
2428 int in_fd, out_fd;
2429 ssize_t res;
2431 in_fd = lo_fi_fd(req, fi_in);
2432 out_fd = lo_fi_fd(req, fi_out);
2434 fuse_log(FUSE_LOG_DEBUG,
2435 "lo_copy_file_range(ino=%" PRIu64 "/fd=%d, "
2436 "off=%lu, ino=%" PRIu64 "/fd=%d, "
2437 "off=%lu, size=%zd, flags=0x%x)\n",
2438 ino_in, in_fd, off_in, ino_out, out_fd, off_out, len, flags);
2440 res = copy_file_range(in_fd, &off_in, out_fd, &off_out, len, flags);
2441 if (res < 0) {
2442 fuse_reply_err(req, errno);
2443 } else {
2444 fuse_reply_write(req, res);
2447 #endif
2449 static void lo_lseek(fuse_req_t req, fuse_ino_t ino, off_t off, int whence,
2450 struct fuse_file_info *fi)
2452 off_t res;
2454 (void)ino;
2455 res = lseek(lo_fi_fd(req, fi), off, whence);
2456 if (res != -1) {
2457 fuse_reply_lseek(req, res);
2458 } else {
2459 fuse_reply_err(req, errno);
2463 static void lo_destroy(void *userdata)
2465 struct lo_data *lo = (struct lo_data *)userdata;
2467 pthread_mutex_lock(&lo->mutex);
2468 while (true) {
2469 GHashTableIter iter;
2470 gpointer key, value;
2472 g_hash_table_iter_init(&iter, lo->inodes);
2473 if (!g_hash_table_iter_next(&iter, &key, &value)) {
2474 break;
2477 struct lo_inode *inode = value;
2478 unref_inode(lo, inode, inode->nlookup);
2480 pthread_mutex_unlock(&lo->mutex);
2483 static struct fuse_lowlevel_ops lo_oper = {
2484 .init = lo_init,
2485 .lookup = lo_lookup,
2486 .mkdir = lo_mkdir,
2487 .mknod = lo_mknod,
2488 .symlink = lo_symlink,
2489 .link = lo_link,
2490 .unlink = lo_unlink,
2491 .rmdir = lo_rmdir,
2492 .rename = lo_rename,
2493 .forget = lo_forget,
2494 .forget_multi = lo_forget_multi,
2495 .getattr = lo_getattr,
2496 .setattr = lo_setattr,
2497 .readlink = lo_readlink,
2498 .opendir = lo_opendir,
2499 .readdir = lo_readdir,
2500 .readdirplus = lo_readdirplus,
2501 .releasedir = lo_releasedir,
2502 .fsyncdir = lo_fsyncdir,
2503 .create = lo_create,
2504 .getlk = lo_getlk,
2505 .setlk = lo_setlk,
2506 .open = lo_open,
2507 .release = lo_release,
2508 .flush = lo_flush,
2509 .fsync = lo_fsync,
2510 .read = lo_read,
2511 .write_buf = lo_write_buf,
2512 .statfs = lo_statfs,
2513 .fallocate = lo_fallocate,
2514 .flock = lo_flock,
2515 .getxattr = lo_getxattr,
2516 .listxattr = lo_listxattr,
2517 .setxattr = lo_setxattr,
2518 .removexattr = lo_removexattr,
2519 #ifdef HAVE_COPY_FILE_RANGE
2520 .copy_file_range = lo_copy_file_range,
2521 #endif
2522 .lseek = lo_lseek,
2523 .destroy = lo_destroy,
2526 /* Print vhost-user.json backend program capabilities */
2527 static void print_capabilities(void)
2529 printf("{\n");
2530 printf(" \"type\": \"fs\"\n");
2531 printf("}\n");
2535 * Move to a new mount, net, and pid namespaces to isolate this process.
2537 static void setup_namespaces(struct lo_data *lo, struct fuse_session *se)
2539 pid_t child;
2542 * Create a new pid namespace for *child* processes. We'll have to
2543 * fork in order to enter the new pid namespace. A new mount namespace
2544 * is also needed so that we can remount /proc for the new pid
2545 * namespace.
2547 * Our UNIX domain sockets have been created. Now we can move to
2548 * an empty network namespace to prevent TCP/IP and other network
2549 * activity in case this process is compromised.
2551 if (unshare(CLONE_NEWPID | CLONE_NEWNS | CLONE_NEWNET) != 0) {
2552 fuse_log(FUSE_LOG_ERR, "unshare(CLONE_NEWPID | CLONE_NEWNS): %m\n");
2553 exit(1);
2556 child = fork();
2557 if (child < 0) {
2558 fuse_log(FUSE_LOG_ERR, "fork() failed: %m\n");
2559 exit(1);
2561 if (child > 0) {
2562 pid_t waited;
2563 int wstatus;
2565 /* The parent waits for the child */
2566 do {
2567 waited = waitpid(child, &wstatus, 0);
2568 } while (waited < 0 && errno == EINTR && !se->exited);
2570 /* We were terminated by a signal, see fuse_signals.c */
2571 if (se->exited) {
2572 exit(0);
2575 if (WIFEXITED(wstatus)) {
2576 exit(WEXITSTATUS(wstatus));
2579 exit(1);
2582 /* Send us SIGTERM when the parent thread terminates, see prctl(2) */
2583 prctl(PR_SET_PDEATHSIG, SIGTERM);
2586 * If the mounts have shared propagation then we want to opt out so our
2587 * mount changes don't affect the parent mount namespace.
2589 if (mount(NULL, "/", NULL, MS_REC | MS_SLAVE, NULL) < 0) {
2590 fuse_log(FUSE_LOG_ERR, "mount(/, MS_REC|MS_SLAVE): %m\n");
2591 exit(1);
2594 /* The child must remount /proc to use the new pid namespace */
2595 if (mount("proc", "/proc", "proc",
2596 MS_NODEV | MS_NOEXEC | MS_NOSUID | MS_RELATIME, NULL) < 0) {
2597 fuse_log(FUSE_LOG_ERR, "mount(/proc): %m\n");
2598 exit(1);
2601 /* Now we can get our /proc/self/fd directory file descriptor */
2602 lo->proc_self_fd = open("/proc/self/fd", O_PATH);
2603 if (lo->proc_self_fd == -1) {
2604 fuse_log(FUSE_LOG_ERR, "open(/proc/self/fd, O_PATH): %m\n");
2605 exit(1);
2610 * Capture the capability state, we'll need to restore this for individual
2611 * threads later; see load_capng.
2613 static void setup_capng(void)
2615 /* Note this accesses /proc so has to happen before the sandbox */
2616 if (capng_get_caps_process()) {
2617 fuse_log(FUSE_LOG_ERR, "capng_get_caps_process\n");
2618 exit(1);
2620 pthread_mutex_init(&cap.mutex, NULL);
2621 pthread_mutex_lock(&cap.mutex);
2622 cap.saved = capng_save_state();
2623 if (!cap.saved) {
2624 fuse_log(FUSE_LOG_ERR, "capng_save_state\n");
2625 exit(1);
2627 pthread_mutex_unlock(&cap.mutex);
2630 static void cleanup_capng(void)
2632 free(cap.saved);
2633 cap.saved = NULL;
2634 pthread_mutex_destroy(&cap.mutex);
2639 * Make the source directory our root so symlinks cannot escape and no other
2640 * files are accessible. Assumes unshare(CLONE_NEWNS) was already called.
2642 static void setup_mounts(const char *source)
2644 int oldroot;
2645 int newroot;
2647 if (mount(source, source, NULL, MS_BIND, NULL) < 0) {
2648 fuse_log(FUSE_LOG_ERR, "mount(%s, %s, MS_BIND): %m\n", source, source);
2649 exit(1);
2652 /* This magic is based on lxc's lxc_pivot_root() */
2653 oldroot = open("/", O_DIRECTORY | O_RDONLY | O_CLOEXEC);
2654 if (oldroot < 0) {
2655 fuse_log(FUSE_LOG_ERR, "open(/): %m\n");
2656 exit(1);
2659 newroot = open(source, O_DIRECTORY | O_RDONLY | O_CLOEXEC);
2660 if (newroot < 0) {
2661 fuse_log(FUSE_LOG_ERR, "open(%s): %m\n", source);
2662 exit(1);
2665 if (fchdir(newroot) < 0) {
2666 fuse_log(FUSE_LOG_ERR, "fchdir(newroot): %m\n");
2667 exit(1);
2670 if (syscall(__NR_pivot_root, ".", ".") < 0) {
2671 fuse_log(FUSE_LOG_ERR, "pivot_root(., .): %m\n");
2672 exit(1);
2675 if (fchdir(oldroot) < 0) {
2676 fuse_log(FUSE_LOG_ERR, "fchdir(oldroot): %m\n");
2677 exit(1);
2680 if (mount("", ".", "", MS_SLAVE | MS_REC, NULL) < 0) {
2681 fuse_log(FUSE_LOG_ERR, "mount(., MS_SLAVE | MS_REC): %m\n");
2682 exit(1);
2685 if (umount2(".", MNT_DETACH) < 0) {
2686 fuse_log(FUSE_LOG_ERR, "umount2(., MNT_DETACH): %m\n");
2687 exit(1);
2690 if (fchdir(newroot) < 0) {
2691 fuse_log(FUSE_LOG_ERR, "fchdir(newroot): %m\n");
2692 exit(1);
2695 close(newroot);
2696 close(oldroot);
2700 * Lock down this process to prevent access to other processes or files outside
2701 * source directory. This reduces the impact of arbitrary code execution bugs.
2703 static void setup_sandbox(struct lo_data *lo, struct fuse_session *se,
2704 bool enable_syslog)
2706 setup_namespaces(lo, se);
2707 setup_mounts(lo->source);
2708 setup_seccomp(enable_syslog);
2711 /* Raise the maximum number of open file descriptors */
2712 static void setup_nofile_rlimit(void)
2714 const rlim_t max_fds = 1000000;
2715 struct rlimit rlim;
2717 if (getrlimit(RLIMIT_NOFILE, &rlim) < 0) {
2718 fuse_log(FUSE_LOG_ERR, "getrlimit(RLIMIT_NOFILE): %m\n");
2719 exit(1);
2722 if (rlim.rlim_cur >= max_fds) {
2723 return; /* nothing to do */
2726 rlim.rlim_cur = max_fds;
2727 rlim.rlim_max = max_fds;
2729 if (setrlimit(RLIMIT_NOFILE, &rlim) < 0) {
2730 /* Ignore SELinux denials */
2731 if (errno == EPERM) {
2732 return;
2735 fuse_log(FUSE_LOG_ERR, "setrlimit(RLIMIT_NOFILE): %m\n");
2736 exit(1);
2740 static void log_func(enum fuse_log_level level, const char *fmt, va_list ap)
2742 g_autofree char *localfmt = NULL;
2744 if (current_log_level < level) {
2745 return;
2748 if (current_log_level == FUSE_LOG_DEBUG) {
2749 if (!use_syslog) {
2750 localfmt = g_strdup_printf("[%" PRId64 "] [ID: %08ld] %s",
2751 get_clock(), syscall(__NR_gettid), fmt);
2752 } else {
2753 localfmt = g_strdup_printf("[ID: %08ld] %s", syscall(__NR_gettid),
2754 fmt);
2756 fmt = localfmt;
2759 if (use_syslog) {
2760 int priority = LOG_ERR;
2761 switch (level) {
2762 case FUSE_LOG_EMERG:
2763 priority = LOG_EMERG;
2764 break;
2765 case FUSE_LOG_ALERT:
2766 priority = LOG_ALERT;
2767 break;
2768 case FUSE_LOG_CRIT:
2769 priority = LOG_CRIT;
2770 break;
2771 case FUSE_LOG_ERR:
2772 priority = LOG_ERR;
2773 break;
2774 case FUSE_LOG_WARNING:
2775 priority = LOG_WARNING;
2776 break;
2777 case FUSE_LOG_NOTICE:
2778 priority = LOG_NOTICE;
2779 break;
2780 case FUSE_LOG_INFO:
2781 priority = LOG_INFO;
2782 break;
2783 case FUSE_LOG_DEBUG:
2784 priority = LOG_DEBUG;
2785 break;
2787 vsyslog(priority, fmt, ap);
2788 } else {
2789 vfprintf(stderr, fmt, ap);
2793 static void setup_root(struct lo_data *lo, struct lo_inode *root)
2795 int fd, res;
2796 struct stat stat;
2798 fd = open("/", O_PATH);
2799 if (fd == -1) {
2800 fuse_log(FUSE_LOG_ERR, "open(%s, O_PATH): %m\n", lo->source);
2801 exit(1);
2804 res = fstatat(fd, "", &stat, AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW);
2805 if (res == -1) {
2806 fuse_log(FUSE_LOG_ERR, "fstatat(%s): %m\n", lo->source);
2807 exit(1);
2810 root->filetype = S_IFDIR;
2811 root->fd = fd;
2812 root->key.ino = stat.st_ino;
2813 root->key.dev = stat.st_dev;
2814 root->nlookup = 2;
2815 g_atomic_int_set(&root->refcount, 2);
2818 static guint lo_key_hash(gconstpointer key)
2820 const struct lo_key *lkey = key;
2822 return (guint)lkey->ino + (guint)lkey->dev;
2825 static gboolean lo_key_equal(gconstpointer a, gconstpointer b)
2827 const struct lo_key *la = a;
2828 const struct lo_key *lb = b;
2830 return la->ino == lb->ino && la->dev == lb->dev;
2833 static void fuse_lo_data_cleanup(struct lo_data *lo)
2835 if (lo->inodes) {
2836 g_hash_table_destroy(lo->inodes);
2838 lo_map_destroy(&lo->fd_map);
2839 lo_map_destroy(&lo->dirp_map);
2840 lo_map_destroy(&lo->ino_map);
2842 if (lo->proc_self_fd >= 0) {
2843 close(lo->proc_self_fd);
2846 if (lo->root.fd >= 0) {
2847 close(lo->root.fd);
2850 free(lo->source);
2853 int main(int argc, char *argv[])
2855 struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
2856 struct fuse_session *se;
2857 struct fuse_cmdline_opts opts;
2858 struct lo_data lo = {
2859 .debug = 0,
2860 .writeback = 0,
2861 .posix_lock = 1,
2862 .proc_self_fd = -1,
2864 struct lo_map_elem *root_elem;
2865 int ret = -1;
2867 /* Don't mask creation mode, kernel already did that */
2868 umask(0);
2870 pthread_mutex_init(&lo.mutex, NULL);
2871 lo.inodes = g_hash_table_new(lo_key_hash, lo_key_equal);
2872 lo.root.fd = -1;
2873 lo.root.fuse_ino = FUSE_ROOT_ID;
2874 lo.cache = CACHE_AUTO;
2877 * Set up the ino map like this:
2878 * [0] Reserved (will not be used)
2879 * [1] Root inode
2881 lo_map_init(&lo.ino_map);
2882 lo_map_reserve(&lo.ino_map, 0)->in_use = false;
2883 root_elem = lo_map_reserve(&lo.ino_map, lo.root.fuse_ino);
2884 root_elem->inode = &lo.root;
2886 lo_map_init(&lo.dirp_map);
2887 lo_map_init(&lo.fd_map);
2889 if (fuse_parse_cmdline(&args, &opts) != 0) {
2890 goto err_out1;
2892 fuse_set_log_func(log_func);
2893 use_syslog = opts.syslog;
2894 if (use_syslog) {
2895 openlog("virtiofsd", LOG_PID, LOG_DAEMON);
2898 if (opts.show_help) {
2899 printf("usage: %s [options]\n\n", argv[0]);
2900 fuse_cmdline_help();
2901 printf(" -o source=PATH shared directory tree\n");
2902 fuse_lowlevel_help();
2903 ret = 0;
2904 goto err_out1;
2905 } else if (opts.show_version) {
2906 fuse_lowlevel_version();
2907 ret = 0;
2908 goto err_out1;
2909 } else if (opts.print_capabilities) {
2910 print_capabilities();
2911 ret = 0;
2912 goto err_out1;
2915 if (fuse_opt_parse(&args, &lo, lo_opts, NULL) == -1) {
2916 goto err_out1;
2920 * log_level is 0 if not configured via cmd options (0 is LOG_EMERG,
2921 * and we don't use this log level).
2923 if (opts.log_level != 0) {
2924 current_log_level = opts.log_level;
2926 lo.debug = opts.debug;
2927 if (lo.debug) {
2928 current_log_level = FUSE_LOG_DEBUG;
2930 if (lo.source) {
2931 struct stat stat;
2932 int res;
2934 res = lstat(lo.source, &stat);
2935 if (res == -1) {
2936 fuse_log(FUSE_LOG_ERR, "failed to stat source (\"%s\"): %m\n",
2937 lo.source);
2938 exit(1);
2940 if (!S_ISDIR(stat.st_mode)) {
2941 fuse_log(FUSE_LOG_ERR, "source is not a directory\n");
2942 exit(1);
2944 } else {
2945 lo.source = strdup("/");
2947 if (!lo.timeout_set) {
2948 switch (lo.cache) {
2949 case CACHE_NONE:
2950 lo.timeout = 0.0;
2951 break;
2953 case CACHE_AUTO:
2954 lo.timeout = 1.0;
2955 break;
2957 case CACHE_ALWAYS:
2958 lo.timeout = 86400.0;
2959 break;
2961 } else if (lo.timeout < 0) {
2962 fuse_log(FUSE_LOG_ERR, "timeout is negative (%lf)\n", lo.timeout);
2963 exit(1);
2966 se = fuse_session_new(&args, &lo_oper, sizeof(lo_oper), &lo);
2967 if (se == NULL) {
2968 goto err_out1;
2971 if (fuse_set_signal_handlers(se) != 0) {
2972 goto err_out2;
2975 if (fuse_session_mount(se) != 0) {
2976 goto err_out3;
2979 fuse_daemonize(opts.foreground);
2981 setup_nofile_rlimit();
2983 /* Must be before sandbox since it wants /proc */
2984 setup_capng();
2986 setup_sandbox(&lo, se, opts.syslog);
2988 setup_root(&lo, &lo.root);
2989 /* Block until ctrl+c or fusermount -u */
2990 ret = virtio_loop(se);
2992 fuse_session_unmount(se);
2993 cleanup_capng();
2994 err_out3:
2995 fuse_remove_signal_handlers(se);
2996 err_out2:
2997 fuse_session_destroy(se);
2998 err_out1:
2999 fuse_opt_free_args(&args);
3001 fuse_lo_data_cleanup(&lo);
3003 return ret ? 1 : 0;