target/sparc/int32_helper: Remove duplicated 'Tag Overflow' entry
[qemu/ar7.git] / tools / virtiofsd / passthrough_ll.c
blob0b229ebd5786628ccb53c3a0e10ddce30d963379
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 "passthrough_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 writeback;
144 int flock;
145 int posix_lock;
146 int xattr;
147 char *source;
148 char *modcaps;
149 double timeout;
150 int cache;
151 int timeout_set;
152 int readdirplus_set;
153 int readdirplus_clear;
154 int allow_direct_io;
155 struct lo_inode root;
156 GHashTable *inodes; /* protected by lo->mutex */
157 struct lo_map ino_map; /* protected by lo->mutex */
158 struct lo_map dirp_map; /* protected by lo->mutex */
159 struct lo_map fd_map; /* protected by lo->mutex */
161 /* An O_PATH file descriptor to /proc/self/fd/ */
162 int proc_self_fd;
165 static const struct fuse_opt lo_opts[] = {
166 { "writeback", offsetof(struct lo_data, writeback), 1 },
167 { "no_writeback", offsetof(struct lo_data, writeback), 0 },
168 { "source=%s", offsetof(struct lo_data, source), 0 },
169 { "flock", offsetof(struct lo_data, flock), 1 },
170 { "no_flock", offsetof(struct lo_data, flock), 0 },
171 { "posix_lock", offsetof(struct lo_data, posix_lock), 1 },
172 { "no_posix_lock", offsetof(struct lo_data, posix_lock), 0 },
173 { "xattr", offsetof(struct lo_data, xattr), 1 },
174 { "no_xattr", offsetof(struct lo_data, xattr), 0 },
175 { "modcaps=%s", offsetof(struct lo_data, modcaps), 0 },
176 { "timeout=%lf", offsetof(struct lo_data, timeout), 0 },
177 { "timeout=", offsetof(struct lo_data, timeout_set), 1 },
178 { "cache=none", offsetof(struct lo_data, cache), CACHE_NONE },
179 { "cache=auto", offsetof(struct lo_data, cache), CACHE_AUTO },
180 { "cache=always", offsetof(struct lo_data, cache), CACHE_ALWAYS },
181 { "readdirplus", offsetof(struct lo_data, readdirplus_set), 1 },
182 { "no_readdirplus", offsetof(struct lo_data, readdirplus_clear), 1 },
183 { "allow_direct_io", offsetof(struct lo_data, allow_direct_io), 1 },
184 { "no_allow_direct_io", offsetof(struct lo_data, allow_direct_io), 0 },
185 FUSE_OPT_END
187 static bool use_syslog = false;
188 static int current_log_level;
189 static void unref_inode_lolocked(struct lo_data *lo, struct lo_inode *inode,
190 uint64_t n);
192 static struct {
193 pthread_mutex_t mutex;
194 void *saved;
195 } cap;
196 /* That we loaded cap-ng in the current thread from the saved */
197 static __thread bool cap_loaded = 0;
199 static struct lo_inode *lo_find(struct lo_data *lo, struct stat *st);
201 static int is_dot_or_dotdot(const char *name)
203 return name[0] == '.' &&
204 (name[1] == '\0' || (name[1] == '.' && name[2] == '\0'));
207 /* Is `path` a single path component that is not "." or ".."? */
208 static int is_safe_path_component(const char *path)
210 if (strchr(path, '/')) {
211 return 0;
214 return !is_dot_or_dotdot(path);
217 static struct lo_data *lo_data(fuse_req_t req)
219 return (struct lo_data *)fuse_req_userdata(req);
223 * Load capng's state from our saved state if the current thread
224 * hadn't previously been loaded.
225 * returns 0 on success
227 static int load_capng(void)
229 if (!cap_loaded) {
230 pthread_mutex_lock(&cap.mutex);
231 capng_restore_state(&cap.saved);
233 * restore_state free's the saved copy
234 * so make another.
236 cap.saved = capng_save_state();
237 if (!cap.saved) {
238 pthread_mutex_unlock(&cap.mutex);
239 fuse_log(FUSE_LOG_ERR, "capng_save_state (thread)\n");
240 return -EINVAL;
242 pthread_mutex_unlock(&cap.mutex);
245 * We want to use the loaded state for our pid,
246 * not the original
248 capng_setpid(syscall(SYS_gettid));
249 cap_loaded = true;
251 return 0;
255 * Helpers for dropping and regaining effective capabilities. Returns 0
256 * on success, error otherwise
258 static int drop_effective_cap(const char *cap_name, bool *cap_dropped)
260 int cap, ret;
262 cap = capng_name_to_capability(cap_name);
263 if (cap < 0) {
264 ret = errno;
265 fuse_log(FUSE_LOG_ERR, "capng_name_to_capability(%s) failed:%s\n",
266 cap_name, strerror(errno));
267 goto out;
270 if (load_capng()) {
271 ret = errno;
272 fuse_log(FUSE_LOG_ERR, "load_capng() failed\n");
273 goto out;
276 /* We dont have this capability in effective set already. */
277 if (!capng_have_capability(CAPNG_EFFECTIVE, cap)) {
278 ret = 0;
279 goto out;
282 if (capng_update(CAPNG_DROP, CAPNG_EFFECTIVE, cap)) {
283 ret = errno;
284 fuse_log(FUSE_LOG_ERR, "capng_update(DROP,) failed\n");
285 goto out;
288 if (capng_apply(CAPNG_SELECT_CAPS)) {
289 ret = errno;
290 fuse_log(FUSE_LOG_ERR, "drop:capng_apply() failed\n");
291 goto out;
294 ret = 0;
295 if (cap_dropped) {
296 *cap_dropped = true;
299 out:
300 return ret;
303 static int gain_effective_cap(const char *cap_name)
305 int cap;
306 int ret = 0;
308 cap = capng_name_to_capability(cap_name);
309 if (cap < 0) {
310 ret = errno;
311 fuse_log(FUSE_LOG_ERR, "capng_name_to_capability(%s) failed:%s\n",
312 cap_name, strerror(errno));
313 goto out;
316 if (load_capng()) {
317 ret = errno;
318 fuse_log(FUSE_LOG_ERR, "load_capng() failed\n");
319 goto out;
322 if (capng_update(CAPNG_ADD, CAPNG_EFFECTIVE, cap)) {
323 ret = errno;
324 fuse_log(FUSE_LOG_ERR, "capng_update(ADD,) failed\n");
325 goto out;
328 if (capng_apply(CAPNG_SELECT_CAPS)) {
329 ret = errno;
330 fuse_log(FUSE_LOG_ERR, "gain:capng_apply() failed\n");
331 goto out;
333 ret = 0;
335 out:
336 return ret;
339 static void lo_map_init(struct lo_map *map)
341 map->elems = NULL;
342 map->nelems = 0;
343 map->freelist = -1;
346 static void lo_map_destroy(struct lo_map *map)
348 free(map->elems);
351 static int lo_map_grow(struct lo_map *map, size_t new_nelems)
353 struct lo_map_elem *new_elems;
354 size_t i;
356 if (new_nelems <= map->nelems) {
357 return 1;
360 new_elems = realloc(map->elems, sizeof(map->elems[0]) * new_nelems);
361 if (!new_elems) {
362 return 0;
365 for (i = map->nelems; i < new_nelems; i++) {
366 new_elems[i].freelist = i + 1;
367 new_elems[i].in_use = false;
369 new_elems[new_nelems - 1].freelist = -1;
371 map->elems = new_elems;
372 map->freelist = map->nelems;
373 map->nelems = new_nelems;
374 return 1;
377 static struct lo_map_elem *lo_map_alloc_elem(struct lo_map *map)
379 struct lo_map_elem *elem;
381 if (map->freelist == -1 && !lo_map_grow(map, map->nelems + 256)) {
382 return NULL;
385 elem = &map->elems[map->freelist];
386 map->freelist = elem->freelist;
388 elem->in_use = true;
390 return elem;
393 static struct lo_map_elem *lo_map_reserve(struct lo_map *map, size_t key)
395 ssize_t *prev;
397 if (!lo_map_grow(map, key + 1)) {
398 return NULL;
401 for (prev = &map->freelist; *prev != -1;
402 prev = &map->elems[*prev].freelist) {
403 if (*prev == key) {
404 struct lo_map_elem *elem = &map->elems[key];
406 *prev = elem->freelist;
407 elem->in_use = true;
408 return elem;
411 return NULL;
414 static struct lo_map_elem *lo_map_get(struct lo_map *map, size_t key)
416 if (key >= map->nelems) {
417 return NULL;
419 if (!map->elems[key].in_use) {
420 return NULL;
422 return &map->elems[key];
425 static void lo_map_remove(struct lo_map *map, size_t key)
427 struct lo_map_elem *elem;
429 if (key >= map->nelems) {
430 return;
433 elem = &map->elems[key];
434 if (!elem->in_use) {
435 return;
438 elem->in_use = false;
440 elem->freelist = map->freelist;
441 map->freelist = key;
444 /* Assumes lo->mutex is held */
445 static ssize_t lo_add_fd_mapping(fuse_req_t req, int fd)
447 struct lo_map_elem *elem;
449 elem = lo_map_alloc_elem(&lo_data(req)->fd_map);
450 if (!elem) {
451 return -1;
454 elem->fd = fd;
455 return elem - lo_data(req)->fd_map.elems;
458 /* Assumes lo->mutex is held */
459 static ssize_t lo_add_dirp_mapping(fuse_req_t req, struct lo_dirp *dirp)
461 struct lo_map_elem *elem;
463 elem = lo_map_alloc_elem(&lo_data(req)->dirp_map);
464 if (!elem) {
465 return -1;
468 elem->dirp = dirp;
469 return elem - lo_data(req)->dirp_map.elems;
472 /* Assumes lo->mutex is held */
473 static ssize_t lo_add_inode_mapping(fuse_req_t req, struct lo_inode *inode)
475 struct lo_map_elem *elem;
477 elem = lo_map_alloc_elem(&lo_data(req)->ino_map);
478 if (!elem) {
479 return -1;
482 elem->inode = inode;
483 return elem - lo_data(req)->ino_map.elems;
486 static void lo_inode_put(struct lo_data *lo, struct lo_inode **inodep)
488 struct lo_inode *inode = *inodep;
490 if (!inode) {
491 return;
494 *inodep = NULL;
496 if (g_atomic_int_dec_and_test(&inode->refcount)) {
497 close(inode->fd);
498 free(inode);
502 /* Caller must release refcount using lo_inode_put() */
503 static struct lo_inode *lo_inode(fuse_req_t req, fuse_ino_t ino)
505 struct lo_data *lo = lo_data(req);
506 struct lo_map_elem *elem;
508 pthread_mutex_lock(&lo->mutex);
509 elem = lo_map_get(&lo->ino_map, ino);
510 if (elem) {
511 g_atomic_int_inc(&elem->inode->refcount);
513 pthread_mutex_unlock(&lo->mutex);
515 if (!elem) {
516 return NULL;
519 return elem->inode;
523 * TODO Remove this helper and force callers to hold an inode refcount until
524 * they are done with the fd. This will be done in a later patch to make
525 * review easier.
527 static int lo_fd(fuse_req_t req, fuse_ino_t ino)
529 struct lo_inode *inode = lo_inode(req, ino);
530 int fd;
532 if (!inode) {
533 return -1;
536 fd = inode->fd;
537 lo_inode_put(lo_data(req), &inode);
538 return fd;
541 static void lo_init(void *userdata, struct fuse_conn_info *conn)
543 struct lo_data *lo = (struct lo_data *)userdata;
545 if (conn->capable & FUSE_CAP_EXPORT_SUPPORT) {
546 conn->want |= FUSE_CAP_EXPORT_SUPPORT;
549 if (lo->writeback && conn->capable & FUSE_CAP_WRITEBACK_CACHE) {
550 fuse_log(FUSE_LOG_DEBUG, "lo_init: activating writeback\n");
551 conn->want |= FUSE_CAP_WRITEBACK_CACHE;
553 if (conn->capable & FUSE_CAP_FLOCK_LOCKS) {
554 if (lo->flock) {
555 fuse_log(FUSE_LOG_DEBUG, "lo_init: activating flock locks\n");
556 conn->want |= FUSE_CAP_FLOCK_LOCKS;
557 } else {
558 fuse_log(FUSE_LOG_DEBUG, "lo_init: disabling flock locks\n");
559 conn->want &= ~FUSE_CAP_FLOCK_LOCKS;
563 if (conn->capable & FUSE_CAP_POSIX_LOCKS) {
564 if (lo->posix_lock) {
565 fuse_log(FUSE_LOG_DEBUG, "lo_init: activating posix locks\n");
566 conn->want |= FUSE_CAP_POSIX_LOCKS;
567 } else {
568 fuse_log(FUSE_LOG_DEBUG, "lo_init: disabling posix locks\n");
569 conn->want &= ~FUSE_CAP_POSIX_LOCKS;
573 if ((lo->cache == CACHE_NONE && !lo->readdirplus_set) ||
574 lo->readdirplus_clear) {
575 fuse_log(FUSE_LOG_DEBUG, "lo_init: disabling readdirplus\n");
576 conn->want &= ~FUSE_CAP_READDIRPLUS;
580 static void lo_getattr(fuse_req_t req, fuse_ino_t ino,
581 struct fuse_file_info *fi)
583 int res;
584 struct stat buf;
585 struct lo_data *lo = lo_data(req);
587 (void)fi;
589 res =
590 fstatat(lo_fd(req, ino), "", &buf, AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW);
591 if (res == -1) {
592 return (void)fuse_reply_err(req, errno);
595 fuse_reply_attr(req, &buf, lo->timeout);
598 static int lo_fi_fd(fuse_req_t req, struct fuse_file_info *fi)
600 struct lo_data *lo = lo_data(req);
601 struct lo_map_elem *elem;
603 pthread_mutex_lock(&lo->mutex);
604 elem = lo_map_get(&lo->fd_map, fi->fh);
605 pthread_mutex_unlock(&lo->mutex);
607 if (!elem) {
608 return -1;
611 return elem->fd;
614 static void lo_setattr(fuse_req_t req, fuse_ino_t ino, struct stat *attr,
615 int valid, struct fuse_file_info *fi)
617 int saverr;
618 char procname[64];
619 struct lo_data *lo = lo_data(req);
620 struct lo_inode *inode;
621 int ifd;
622 int res;
623 int fd;
625 inode = lo_inode(req, ino);
626 if (!inode) {
627 fuse_reply_err(req, EBADF);
628 return;
631 ifd = inode->fd;
633 /* If fi->fh is invalid we'll report EBADF later */
634 if (fi) {
635 fd = lo_fi_fd(req, fi);
638 if (valid & FUSE_SET_ATTR_MODE) {
639 if (fi) {
640 res = fchmod(fd, attr->st_mode);
641 } else {
642 sprintf(procname, "%i", ifd);
643 res = fchmodat(lo->proc_self_fd, procname, attr->st_mode, 0);
645 if (res == -1) {
646 goto out_err;
649 if (valid & (FUSE_SET_ATTR_UID | FUSE_SET_ATTR_GID)) {
650 uid_t uid = (valid & FUSE_SET_ATTR_UID) ? attr->st_uid : (uid_t)-1;
651 gid_t gid = (valid & FUSE_SET_ATTR_GID) ? attr->st_gid : (gid_t)-1;
653 res = fchownat(ifd, "", uid, gid, AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW);
654 if (res == -1) {
655 goto out_err;
658 if (valid & FUSE_SET_ATTR_SIZE) {
659 int truncfd;
661 if (fi) {
662 truncfd = fd;
663 } else {
664 sprintf(procname, "%i", ifd);
665 truncfd = openat(lo->proc_self_fd, procname, O_RDWR);
666 if (truncfd < 0) {
667 goto out_err;
671 res = ftruncate(truncfd, attr->st_size);
672 if (!fi) {
673 saverr = errno;
674 close(truncfd);
675 errno = saverr;
677 if (res == -1) {
678 goto out_err;
681 if (valid & (FUSE_SET_ATTR_ATIME | FUSE_SET_ATTR_MTIME)) {
682 struct timespec tv[2];
684 tv[0].tv_sec = 0;
685 tv[1].tv_sec = 0;
686 tv[0].tv_nsec = UTIME_OMIT;
687 tv[1].tv_nsec = UTIME_OMIT;
689 if (valid & FUSE_SET_ATTR_ATIME_NOW) {
690 tv[0].tv_nsec = UTIME_NOW;
691 } else if (valid & FUSE_SET_ATTR_ATIME) {
692 tv[0] = attr->st_atim;
695 if (valid & FUSE_SET_ATTR_MTIME_NOW) {
696 tv[1].tv_nsec = UTIME_NOW;
697 } else if (valid & FUSE_SET_ATTR_MTIME) {
698 tv[1] = attr->st_mtim;
701 if (fi) {
702 res = futimens(fd, tv);
703 } else {
704 sprintf(procname, "%i", inode->fd);
705 res = utimensat(lo->proc_self_fd, procname, tv, 0);
707 if (res == -1) {
708 goto out_err;
711 lo_inode_put(lo, &inode);
713 return lo_getattr(req, ino, fi);
715 out_err:
716 saverr = errno;
717 lo_inode_put(lo, &inode);
718 fuse_reply_err(req, saverr);
721 static struct lo_inode *lo_find(struct lo_data *lo, struct stat *st)
723 struct lo_inode *p;
724 struct lo_key key = {
725 .ino = st->st_ino,
726 .dev = st->st_dev,
729 pthread_mutex_lock(&lo->mutex);
730 p = g_hash_table_lookup(lo->inodes, &key);
731 if (p) {
732 assert(p->nlookup > 0);
733 p->nlookup++;
734 g_atomic_int_inc(&p->refcount);
736 pthread_mutex_unlock(&lo->mutex);
738 return p;
741 /* value_destroy_func for posix_locks GHashTable */
742 static void posix_locks_value_destroy(gpointer data)
744 struct lo_inode_plock *plock = data;
747 * We had used open() for locks and had only one fd. So
748 * closing this fd should release all OFD locks.
750 close(plock->fd);
751 free(plock);
755 * Increments nlookup and caller must release refcount using
756 * lo_inode_put(&parent).
758 static int lo_do_lookup(fuse_req_t req, fuse_ino_t parent, const char *name,
759 struct fuse_entry_param *e)
761 int newfd;
762 int res;
763 int saverr;
764 struct lo_data *lo = lo_data(req);
765 struct lo_inode *inode = NULL;
766 struct lo_inode *dir = lo_inode(req, parent);
769 * name_to_handle_at() and open_by_handle_at() can reach here with fuse
770 * mount point in guest, but we don't have its inode info in the
771 * ino_map.
773 if (!dir) {
774 return ENOENT;
777 memset(e, 0, sizeof(*e));
778 e->attr_timeout = lo->timeout;
779 e->entry_timeout = lo->timeout;
781 /* Do not allow escaping root directory */
782 if (dir == &lo->root && strcmp(name, "..") == 0) {
783 name = ".";
786 newfd = openat(dir->fd, name, O_PATH | O_NOFOLLOW);
787 if (newfd == -1) {
788 goto out_err;
791 res = fstatat(newfd, "", &e->attr, AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW);
792 if (res == -1) {
793 goto out_err;
796 inode = lo_find(lo, &e->attr);
797 if (inode) {
798 close(newfd);
799 } else {
800 inode = calloc(1, sizeof(struct lo_inode));
801 if (!inode) {
802 goto out_err;
805 /* cache only filetype */
806 inode->filetype = (e->attr.st_mode & S_IFMT);
809 * One for the caller and one for nlookup (released in
810 * unref_inode_lolocked())
812 g_atomic_int_set(&inode->refcount, 2);
814 inode->nlookup = 1;
815 inode->fd = newfd;
816 inode->key.ino = e->attr.st_ino;
817 inode->key.dev = e->attr.st_dev;
818 pthread_mutex_init(&inode->plock_mutex, NULL);
819 inode->posix_locks = g_hash_table_new_full(
820 g_direct_hash, g_direct_equal, NULL, posix_locks_value_destroy);
822 pthread_mutex_lock(&lo->mutex);
823 inode->fuse_ino = lo_add_inode_mapping(req, inode);
824 g_hash_table_insert(lo->inodes, &inode->key, inode);
825 pthread_mutex_unlock(&lo->mutex);
827 e->ino = inode->fuse_ino;
828 lo_inode_put(lo, &inode);
829 lo_inode_put(lo, &dir);
831 fuse_log(FUSE_LOG_DEBUG, " %lli/%s -> %lli\n", (unsigned long long)parent,
832 name, (unsigned long long)e->ino);
834 return 0;
836 out_err:
837 saverr = errno;
838 if (newfd != -1) {
839 close(newfd);
841 lo_inode_put(lo, &inode);
842 lo_inode_put(lo, &dir);
843 return saverr;
846 static void lo_lookup(fuse_req_t req, fuse_ino_t parent, const char *name)
848 struct fuse_entry_param e;
849 int err;
851 fuse_log(FUSE_LOG_DEBUG, "lo_lookup(parent=%" PRIu64 ", name=%s)\n", parent,
852 name);
855 * Don't use is_safe_path_component(), allow "." and ".." for NFS export
856 * support.
858 if (strchr(name, '/')) {
859 fuse_reply_err(req, EINVAL);
860 return;
863 err = lo_do_lookup(req, parent, name, &e);
864 if (err) {
865 fuse_reply_err(req, err);
866 } else {
867 fuse_reply_entry(req, &e);
872 * On some archs, setres*id is limited to 2^16 but they
873 * provide setres*id32 variants that allow 2^32.
874 * Others just let setres*id do 2^32 anyway.
876 #ifdef SYS_setresgid32
877 #define OURSYS_setresgid SYS_setresgid32
878 #else
879 #define OURSYS_setresgid SYS_setresgid
880 #endif
882 #ifdef SYS_setresuid32
883 #define OURSYS_setresuid SYS_setresuid32
884 #else
885 #define OURSYS_setresuid SYS_setresuid
886 #endif
889 * Change to uid/gid of caller so that file is created with
890 * ownership of caller.
891 * TODO: What about selinux context?
893 static int lo_change_cred(fuse_req_t req, struct lo_cred *old)
895 int res;
897 old->euid = geteuid();
898 old->egid = getegid();
900 res = syscall(OURSYS_setresgid, -1, fuse_req_ctx(req)->gid, -1);
901 if (res == -1) {
902 return errno;
905 res = syscall(OURSYS_setresuid, -1, fuse_req_ctx(req)->uid, -1);
906 if (res == -1) {
907 int errno_save = errno;
909 syscall(OURSYS_setresgid, -1, old->egid, -1);
910 return errno_save;
913 return 0;
916 /* Regain Privileges */
917 static void lo_restore_cred(struct lo_cred *old)
919 int res;
921 res = syscall(OURSYS_setresuid, -1, old->euid, -1);
922 if (res == -1) {
923 fuse_log(FUSE_LOG_ERR, "seteuid(%u): %m\n", old->euid);
924 exit(1);
927 res = syscall(OURSYS_setresgid, -1, old->egid, -1);
928 if (res == -1) {
929 fuse_log(FUSE_LOG_ERR, "setegid(%u): %m\n", old->egid);
930 exit(1);
934 static void lo_mknod_symlink(fuse_req_t req, fuse_ino_t parent,
935 const char *name, mode_t mode, dev_t rdev,
936 const char *link)
938 int res;
939 int saverr;
940 struct lo_data *lo = lo_data(req);
941 struct lo_inode *dir;
942 struct fuse_entry_param e;
943 struct lo_cred old = {};
945 if (!is_safe_path_component(name)) {
946 fuse_reply_err(req, EINVAL);
947 return;
950 dir = lo_inode(req, parent);
951 if (!dir) {
952 fuse_reply_err(req, EBADF);
953 return;
956 saverr = lo_change_cred(req, &old);
957 if (saverr) {
958 goto out;
961 res = mknod_wrapper(dir->fd, name, link, mode, rdev);
963 saverr = errno;
965 lo_restore_cred(&old);
967 if (res == -1) {
968 goto out;
971 saverr = lo_do_lookup(req, parent, name, &e);
972 if (saverr) {
973 goto out;
976 fuse_log(FUSE_LOG_DEBUG, " %lli/%s -> %lli\n", (unsigned long long)parent,
977 name, (unsigned long long)e.ino);
979 fuse_reply_entry(req, &e);
980 lo_inode_put(lo, &dir);
981 return;
983 out:
984 lo_inode_put(lo, &dir);
985 fuse_reply_err(req, saverr);
988 static void lo_mknod(fuse_req_t req, fuse_ino_t parent, const char *name,
989 mode_t mode, dev_t rdev)
991 lo_mknod_symlink(req, parent, name, mode, rdev, NULL);
994 static void lo_mkdir(fuse_req_t req, fuse_ino_t parent, const char *name,
995 mode_t mode)
997 lo_mknod_symlink(req, parent, name, S_IFDIR | mode, 0, NULL);
1000 static void lo_symlink(fuse_req_t req, const char *link, fuse_ino_t parent,
1001 const char *name)
1003 lo_mknod_symlink(req, parent, name, S_IFLNK, 0, link);
1006 static void lo_link(fuse_req_t req, fuse_ino_t ino, fuse_ino_t parent,
1007 const char *name)
1009 int res;
1010 struct lo_data *lo = lo_data(req);
1011 struct lo_inode *parent_inode;
1012 struct lo_inode *inode;
1013 struct fuse_entry_param e;
1014 char procname[64];
1015 int saverr;
1017 if (!is_safe_path_component(name)) {
1018 fuse_reply_err(req, EINVAL);
1019 return;
1022 parent_inode = lo_inode(req, parent);
1023 inode = lo_inode(req, ino);
1024 if (!parent_inode || !inode) {
1025 errno = EBADF;
1026 goto out_err;
1029 memset(&e, 0, sizeof(struct fuse_entry_param));
1030 e.attr_timeout = lo->timeout;
1031 e.entry_timeout = lo->timeout;
1033 sprintf(procname, "%i", inode->fd);
1034 res = linkat(lo->proc_self_fd, procname, parent_inode->fd, name,
1035 AT_SYMLINK_FOLLOW);
1036 if (res == -1) {
1037 goto out_err;
1040 res = fstatat(inode->fd, "", &e.attr, AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW);
1041 if (res == -1) {
1042 goto out_err;
1045 pthread_mutex_lock(&lo->mutex);
1046 inode->nlookup++;
1047 pthread_mutex_unlock(&lo->mutex);
1048 e.ino = inode->fuse_ino;
1050 fuse_log(FUSE_LOG_DEBUG, " %lli/%s -> %lli\n", (unsigned long long)parent,
1051 name, (unsigned long long)e.ino);
1053 fuse_reply_entry(req, &e);
1054 lo_inode_put(lo, &parent_inode);
1055 lo_inode_put(lo, &inode);
1056 return;
1058 out_err:
1059 saverr = errno;
1060 lo_inode_put(lo, &parent_inode);
1061 lo_inode_put(lo, &inode);
1062 fuse_reply_err(req, saverr);
1065 /* Increments nlookup and caller must release refcount using lo_inode_put() */
1066 static struct lo_inode *lookup_name(fuse_req_t req, fuse_ino_t parent,
1067 const char *name)
1069 int res;
1070 struct stat attr;
1072 res = fstatat(lo_fd(req, parent), name, &attr,
1073 AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW);
1074 if (res == -1) {
1075 return NULL;
1078 return lo_find(lo_data(req), &attr);
1081 static void lo_rmdir(fuse_req_t req, fuse_ino_t parent, const char *name)
1083 int res;
1084 struct lo_inode *inode;
1085 struct lo_data *lo = lo_data(req);
1087 if (!is_safe_path_component(name)) {
1088 fuse_reply_err(req, EINVAL);
1089 return;
1092 inode = lookup_name(req, parent, name);
1093 if (!inode) {
1094 fuse_reply_err(req, EIO);
1095 return;
1098 res = unlinkat(lo_fd(req, parent), name, AT_REMOVEDIR);
1100 fuse_reply_err(req, res == -1 ? errno : 0);
1101 unref_inode_lolocked(lo, inode, 1);
1102 lo_inode_put(lo, &inode);
1105 static void lo_rename(fuse_req_t req, fuse_ino_t parent, const char *name,
1106 fuse_ino_t newparent, const char *newname,
1107 unsigned int flags)
1109 int res;
1110 struct lo_inode *parent_inode;
1111 struct lo_inode *newparent_inode;
1112 struct lo_inode *oldinode = NULL;
1113 struct lo_inode *newinode = NULL;
1114 struct lo_data *lo = lo_data(req);
1116 if (!is_safe_path_component(name) || !is_safe_path_component(newname)) {
1117 fuse_reply_err(req, EINVAL);
1118 return;
1121 parent_inode = lo_inode(req, parent);
1122 newparent_inode = lo_inode(req, newparent);
1123 if (!parent_inode || !newparent_inode) {
1124 fuse_reply_err(req, EBADF);
1125 goto out;
1128 oldinode = lookup_name(req, parent, name);
1129 newinode = lookup_name(req, newparent, newname);
1131 if (!oldinode) {
1132 fuse_reply_err(req, EIO);
1133 goto out;
1136 if (flags) {
1137 #ifndef SYS_renameat2
1138 fuse_reply_err(req, EINVAL);
1139 #else
1140 res = syscall(SYS_renameat2, parent_inode->fd, name,
1141 newparent_inode->fd, newname, flags);
1142 if (res == -1 && errno == ENOSYS) {
1143 fuse_reply_err(req, EINVAL);
1144 } else {
1145 fuse_reply_err(req, res == -1 ? errno : 0);
1147 #endif
1148 goto out;
1151 res = renameat(parent_inode->fd, name, newparent_inode->fd, newname);
1153 fuse_reply_err(req, res == -1 ? errno : 0);
1154 out:
1155 unref_inode_lolocked(lo, oldinode, 1);
1156 unref_inode_lolocked(lo, newinode, 1);
1157 lo_inode_put(lo, &oldinode);
1158 lo_inode_put(lo, &newinode);
1159 lo_inode_put(lo, &parent_inode);
1160 lo_inode_put(lo, &newparent_inode);
1163 static void lo_unlink(fuse_req_t req, fuse_ino_t parent, const char *name)
1165 int res;
1166 struct lo_inode *inode;
1167 struct lo_data *lo = lo_data(req);
1169 if (!is_safe_path_component(name)) {
1170 fuse_reply_err(req, EINVAL);
1171 return;
1174 inode = lookup_name(req, parent, name);
1175 if (!inode) {
1176 fuse_reply_err(req, EIO);
1177 return;
1180 res = unlinkat(lo_fd(req, parent), name, 0);
1182 fuse_reply_err(req, res == -1 ? errno : 0);
1183 unref_inode_lolocked(lo, inode, 1);
1184 lo_inode_put(lo, &inode);
1187 /* To be called with lo->mutex held */
1188 static void unref_inode(struct lo_data *lo, struct lo_inode *inode, uint64_t n)
1190 if (!inode) {
1191 return;
1194 assert(inode->nlookup >= n);
1195 inode->nlookup -= n;
1196 if (!inode->nlookup) {
1197 lo_map_remove(&lo->ino_map, inode->fuse_ino);
1198 g_hash_table_remove(lo->inodes, &inode->key);
1199 if (g_hash_table_size(inode->posix_locks)) {
1200 fuse_log(FUSE_LOG_WARNING, "Hash table is not empty\n");
1202 g_hash_table_destroy(inode->posix_locks);
1203 pthread_mutex_destroy(&inode->plock_mutex);
1205 /* Drop our refcount from lo_do_lookup() */
1206 lo_inode_put(lo, &inode);
1210 static void unref_inode_lolocked(struct lo_data *lo, struct lo_inode *inode,
1211 uint64_t n)
1213 if (!inode) {
1214 return;
1217 pthread_mutex_lock(&lo->mutex);
1218 unref_inode(lo, inode, n);
1219 pthread_mutex_unlock(&lo->mutex);
1222 static void lo_forget_one(fuse_req_t req, fuse_ino_t ino, uint64_t nlookup)
1224 struct lo_data *lo = lo_data(req);
1225 struct lo_inode *inode;
1227 inode = lo_inode(req, ino);
1228 if (!inode) {
1229 return;
1232 fuse_log(FUSE_LOG_DEBUG, " forget %lli %lli -%lli\n",
1233 (unsigned long long)ino, (unsigned long long)inode->nlookup,
1234 (unsigned long long)nlookup);
1236 unref_inode_lolocked(lo, inode, nlookup);
1237 lo_inode_put(lo, &inode);
1240 static void lo_forget(fuse_req_t req, fuse_ino_t ino, uint64_t nlookup)
1242 lo_forget_one(req, ino, nlookup);
1243 fuse_reply_none(req);
1246 static void lo_forget_multi(fuse_req_t req, size_t count,
1247 struct fuse_forget_data *forgets)
1249 int i;
1251 for (i = 0; i < count; i++) {
1252 lo_forget_one(req, forgets[i].ino, forgets[i].nlookup);
1254 fuse_reply_none(req);
1257 static void lo_readlink(fuse_req_t req, fuse_ino_t ino)
1259 char buf[PATH_MAX + 1];
1260 int res;
1262 res = readlinkat(lo_fd(req, ino), "", buf, sizeof(buf));
1263 if (res == -1) {
1264 return (void)fuse_reply_err(req, errno);
1267 if (res == sizeof(buf)) {
1268 return (void)fuse_reply_err(req, ENAMETOOLONG);
1271 buf[res] = '\0';
1273 fuse_reply_readlink(req, buf);
1276 struct lo_dirp {
1277 gint refcount;
1278 DIR *dp;
1279 struct dirent *entry;
1280 off_t offset;
1283 static void lo_dirp_put(struct lo_dirp **dp)
1285 struct lo_dirp *d = *dp;
1287 if (!d) {
1288 return;
1290 *dp = NULL;
1292 if (g_atomic_int_dec_and_test(&d->refcount)) {
1293 closedir(d->dp);
1294 free(d);
1298 /* Call lo_dirp_put() on the return value when no longer needed */
1299 static struct lo_dirp *lo_dirp(fuse_req_t req, struct fuse_file_info *fi)
1301 struct lo_data *lo = lo_data(req);
1302 struct lo_map_elem *elem;
1304 pthread_mutex_lock(&lo->mutex);
1305 elem = lo_map_get(&lo->dirp_map, fi->fh);
1306 if (elem) {
1307 g_atomic_int_inc(&elem->dirp->refcount);
1309 pthread_mutex_unlock(&lo->mutex);
1310 if (!elem) {
1311 return NULL;
1314 return elem->dirp;
1317 static void lo_opendir(fuse_req_t req, fuse_ino_t ino,
1318 struct fuse_file_info *fi)
1320 int error = ENOMEM;
1321 struct lo_data *lo = lo_data(req);
1322 struct lo_dirp *d;
1323 int fd;
1324 ssize_t fh;
1326 d = calloc(1, sizeof(struct lo_dirp));
1327 if (d == NULL) {
1328 goto out_err;
1331 fd = openat(lo_fd(req, ino), ".", O_RDONLY);
1332 if (fd == -1) {
1333 goto out_errno;
1336 d->dp = fdopendir(fd);
1337 if (d->dp == NULL) {
1338 goto out_errno;
1341 d->offset = 0;
1342 d->entry = NULL;
1344 g_atomic_int_set(&d->refcount, 1); /* paired with lo_releasedir() */
1345 pthread_mutex_lock(&lo->mutex);
1346 fh = lo_add_dirp_mapping(req, d);
1347 pthread_mutex_unlock(&lo->mutex);
1348 if (fh == -1) {
1349 goto out_err;
1352 fi->fh = fh;
1353 if (lo->cache == CACHE_ALWAYS) {
1354 fi->cache_readdir = 1;
1356 fuse_reply_open(req, fi);
1357 return;
1359 out_errno:
1360 error = errno;
1361 out_err:
1362 if (d) {
1363 if (d->dp) {
1364 closedir(d->dp);
1365 } else if (fd != -1) {
1366 close(fd);
1368 free(d);
1370 fuse_reply_err(req, error);
1373 static void lo_do_readdir(fuse_req_t req, fuse_ino_t ino, size_t size,
1374 off_t offset, struct fuse_file_info *fi, int plus)
1376 struct lo_data *lo = lo_data(req);
1377 struct lo_dirp *d = NULL;
1378 struct lo_inode *dinode;
1379 char *buf = NULL;
1380 char *p;
1381 size_t rem = size;
1382 int err = EBADF;
1384 dinode = lo_inode(req, ino);
1385 if (!dinode) {
1386 goto error;
1389 d = lo_dirp(req, fi);
1390 if (!d) {
1391 goto error;
1394 err = ENOMEM;
1395 buf = calloc(1, size);
1396 if (!buf) {
1397 goto error;
1399 p = buf;
1401 if (offset != d->offset) {
1402 seekdir(d->dp, offset);
1403 d->entry = NULL;
1404 d->offset = offset;
1406 while (1) {
1407 size_t entsize;
1408 off_t nextoff;
1409 const char *name;
1411 if (!d->entry) {
1412 errno = 0;
1413 d->entry = readdir(d->dp);
1414 if (!d->entry) {
1415 if (errno) { /* Error */
1416 err = errno;
1417 goto error;
1418 } else { /* End of stream */
1419 break;
1423 nextoff = d->entry->d_off;
1424 name = d->entry->d_name;
1426 fuse_ino_t entry_ino = 0;
1427 struct fuse_entry_param e = (struct fuse_entry_param){
1428 .attr.st_ino = d->entry->d_ino,
1429 .attr.st_mode = d->entry->d_type << 12,
1432 /* Hide root's parent directory */
1433 if (dinode == &lo->root && strcmp(name, "..") == 0) {
1434 e.attr.st_ino = lo->root.key.ino;
1435 e.attr.st_mode = DT_DIR << 12;
1438 if (plus) {
1439 if (!is_dot_or_dotdot(name)) {
1440 err = lo_do_lookup(req, ino, name, &e);
1441 if (err) {
1442 goto error;
1444 entry_ino = e.ino;
1447 entsize = fuse_add_direntry_plus(req, p, rem, name, &e, nextoff);
1448 } else {
1449 entsize = fuse_add_direntry(req, p, rem, name, &e.attr, nextoff);
1451 if (entsize > rem) {
1452 if (entry_ino != 0) {
1453 lo_forget_one(req, entry_ino, 1);
1455 break;
1458 p += entsize;
1459 rem -= entsize;
1461 d->entry = NULL;
1462 d->offset = nextoff;
1465 err = 0;
1466 error:
1467 lo_dirp_put(&d);
1468 lo_inode_put(lo, &dinode);
1471 * If there's an error, we can only signal it if we haven't stored
1472 * any entries yet - otherwise we'd end up with wrong lookup
1473 * counts for the entries that are already in the buffer. So we
1474 * return what we've collected until that point.
1476 if (err && rem == size) {
1477 fuse_reply_err(req, err);
1478 } else {
1479 fuse_reply_buf(req, buf, size - rem);
1481 free(buf);
1484 static void lo_readdir(fuse_req_t req, fuse_ino_t ino, size_t size,
1485 off_t offset, struct fuse_file_info *fi)
1487 lo_do_readdir(req, ino, size, offset, fi, 0);
1490 static void lo_readdirplus(fuse_req_t req, fuse_ino_t ino, size_t size,
1491 off_t offset, struct fuse_file_info *fi)
1493 lo_do_readdir(req, ino, size, offset, fi, 1);
1496 static void lo_releasedir(fuse_req_t req, fuse_ino_t ino,
1497 struct fuse_file_info *fi)
1499 struct lo_data *lo = lo_data(req);
1500 struct lo_map_elem *elem;
1501 struct lo_dirp *d;
1503 (void)ino;
1505 pthread_mutex_lock(&lo->mutex);
1506 elem = lo_map_get(&lo->dirp_map, fi->fh);
1507 if (!elem) {
1508 pthread_mutex_unlock(&lo->mutex);
1509 fuse_reply_err(req, EBADF);
1510 return;
1513 d = elem->dirp;
1514 lo_map_remove(&lo->dirp_map, fi->fh);
1515 pthread_mutex_unlock(&lo->mutex);
1517 lo_dirp_put(&d); /* paired with lo_opendir() */
1519 fuse_reply_err(req, 0);
1522 static void update_open_flags(int writeback, int allow_direct_io,
1523 struct fuse_file_info *fi)
1526 * With writeback cache, kernel may send read requests even
1527 * when userspace opened write-only
1529 if (writeback && (fi->flags & O_ACCMODE) == O_WRONLY) {
1530 fi->flags &= ~O_ACCMODE;
1531 fi->flags |= O_RDWR;
1535 * With writeback cache, O_APPEND is handled by the kernel.
1536 * This breaks atomicity (since the file may change in the
1537 * underlying filesystem, so that the kernel's idea of the
1538 * end of the file isn't accurate anymore). In this example,
1539 * we just accept that. A more rigorous filesystem may want
1540 * to return an error here
1542 if (writeback && (fi->flags & O_APPEND)) {
1543 fi->flags &= ~O_APPEND;
1547 * O_DIRECT in guest should not necessarily mean bypassing page
1548 * cache on host as well. Therefore, we discard it by default
1549 * ('-o no_allow_direct_io'). If somebody needs that behavior,
1550 * the '-o allow_direct_io' option should be set.
1552 if (!allow_direct_io) {
1553 fi->flags &= ~O_DIRECT;
1557 static void lo_create(fuse_req_t req, fuse_ino_t parent, const char *name,
1558 mode_t mode, struct fuse_file_info *fi)
1560 int fd;
1561 struct lo_data *lo = lo_data(req);
1562 struct lo_inode *parent_inode;
1563 struct fuse_entry_param e;
1564 int err;
1565 struct lo_cred old = {};
1567 fuse_log(FUSE_LOG_DEBUG, "lo_create(parent=%" PRIu64 ", name=%s)\n", parent,
1568 name);
1570 if (!is_safe_path_component(name)) {
1571 fuse_reply_err(req, EINVAL);
1572 return;
1575 parent_inode = lo_inode(req, parent);
1576 if (!parent_inode) {
1577 fuse_reply_err(req, EBADF);
1578 return;
1581 err = lo_change_cred(req, &old);
1582 if (err) {
1583 goto out;
1586 update_open_flags(lo->writeback, lo->allow_direct_io, fi);
1588 fd = openat(parent_inode->fd, name, (fi->flags | O_CREAT) & ~O_NOFOLLOW,
1589 mode);
1590 err = fd == -1 ? errno : 0;
1591 lo_restore_cred(&old);
1593 if (!err) {
1594 ssize_t fh;
1596 pthread_mutex_lock(&lo->mutex);
1597 fh = lo_add_fd_mapping(req, fd);
1598 pthread_mutex_unlock(&lo->mutex);
1599 if (fh == -1) {
1600 close(fd);
1601 err = ENOMEM;
1602 goto out;
1605 fi->fh = fh;
1606 err = lo_do_lookup(req, parent, name, &e);
1608 if (lo->cache == CACHE_NONE) {
1609 fi->direct_io = 1;
1610 } else if (lo->cache == CACHE_ALWAYS) {
1611 fi->keep_cache = 1;
1614 out:
1615 lo_inode_put(lo, &parent_inode);
1617 if (err) {
1618 fuse_reply_err(req, err);
1619 } else {
1620 fuse_reply_create(req, &e, fi);
1624 /* Should be called with inode->plock_mutex held */
1625 static struct lo_inode_plock *lookup_create_plock_ctx(struct lo_data *lo,
1626 struct lo_inode *inode,
1627 uint64_t lock_owner,
1628 pid_t pid, int *err)
1630 struct lo_inode_plock *plock;
1631 char procname[64];
1632 int fd;
1634 plock =
1635 g_hash_table_lookup(inode->posix_locks, GUINT_TO_POINTER(lock_owner));
1637 if (plock) {
1638 return plock;
1641 plock = malloc(sizeof(struct lo_inode_plock));
1642 if (!plock) {
1643 *err = ENOMEM;
1644 return NULL;
1647 /* Open another instance of file which can be used for ofd locks. */
1648 sprintf(procname, "%i", inode->fd);
1650 /* TODO: What if file is not writable? */
1651 fd = openat(lo->proc_self_fd, procname, O_RDWR);
1652 if (fd == -1) {
1653 *err = errno;
1654 free(plock);
1655 return NULL;
1658 plock->lock_owner = lock_owner;
1659 plock->fd = fd;
1660 g_hash_table_insert(inode->posix_locks, GUINT_TO_POINTER(plock->lock_owner),
1661 plock);
1662 return plock;
1665 static void lo_getlk(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi,
1666 struct flock *lock)
1668 struct lo_data *lo = lo_data(req);
1669 struct lo_inode *inode;
1670 struct lo_inode_plock *plock;
1671 int ret, saverr = 0;
1673 fuse_log(FUSE_LOG_DEBUG,
1674 "lo_getlk(ino=%" PRIu64 ", flags=%d)"
1675 " owner=0x%lx, l_type=%d l_start=0x%lx"
1676 " l_len=0x%lx\n",
1677 ino, fi->flags, fi->lock_owner, lock->l_type, lock->l_start,
1678 lock->l_len);
1680 inode = lo_inode(req, ino);
1681 if (!inode) {
1682 fuse_reply_err(req, EBADF);
1683 return;
1686 pthread_mutex_lock(&inode->plock_mutex);
1687 plock =
1688 lookup_create_plock_ctx(lo, inode, fi->lock_owner, lock->l_pid, &ret);
1689 if (!plock) {
1690 saverr = ret;
1691 goto out;
1694 ret = fcntl(plock->fd, F_OFD_GETLK, lock);
1695 if (ret == -1) {
1696 saverr = errno;
1699 out:
1700 pthread_mutex_unlock(&inode->plock_mutex);
1701 lo_inode_put(lo, &inode);
1703 if (saverr) {
1704 fuse_reply_err(req, saverr);
1705 } else {
1706 fuse_reply_lock(req, lock);
1710 static void lo_setlk(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi,
1711 struct flock *lock, int sleep)
1713 struct lo_data *lo = lo_data(req);
1714 struct lo_inode *inode;
1715 struct lo_inode_plock *plock;
1716 int ret, saverr = 0;
1718 fuse_log(FUSE_LOG_DEBUG,
1719 "lo_setlk(ino=%" PRIu64 ", flags=%d)"
1720 " cmd=%d pid=%d owner=0x%lx sleep=%d l_whence=%d"
1721 " l_start=0x%lx l_len=0x%lx\n",
1722 ino, fi->flags, lock->l_type, lock->l_pid, fi->lock_owner, sleep,
1723 lock->l_whence, lock->l_start, lock->l_len);
1725 if (sleep) {
1726 fuse_reply_err(req, EOPNOTSUPP);
1727 return;
1730 inode = lo_inode(req, ino);
1731 if (!inode) {
1732 fuse_reply_err(req, EBADF);
1733 return;
1736 pthread_mutex_lock(&inode->plock_mutex);
1737 plock =
1738 lookup_create_plock_ctx(lo, inode, fi->lock_owner, lock->l_pid, &ret);
1740 if (!plock) {
1741 saverr = ret;
1742 goto out;
1745 /* TODO: Is it alright to modify flock? */
1746 lock->l_pid = 0;
1747 ret = fcntl(plock->fd, F_OFD_SETLK, lock);
1748 if (ret == -1) {
1749 saverr = errno;
1752 out:
1753 pthread_mutex_unlock(&inode->plock_mutex);
1754 lo_inode_put(lo, &inode);
1756 fuse_reply_err(req, saverr);
1759 static void lo_fsyncdir(fuse_req_t req, fuse_ino_t ino, int datasync,
1760 struct fuse_file_info *fi)
1762 int res;
1763 struct lo_dirp *d;
1764 int fd;
1766 (void)ino;
1768 d = lo_dirp(req, fi);
1769 if (!d) {
1770 fuse_reply_err(req, EBADF);
1771 return;
1774 fd = dirfd(d->dp);
1775 if (datasync) {
1776 res = fdatasync(fd);
1777 } else {
1778 res = fsync(fd);
1781 lo_dirp_put(&d);
1783 fuse_reply_err(req, res == -1 ? errno : 0);
1786 static void lo_open(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi)
1788 int fd;
1789 ssize_t fh;
1790 char buf[64];
1791 struct lo_data *lo = lo_data(req);
1793 fuse_log(FUSE_LOG_DEBUG, "lo_open(ino=%" PRIu64 ", flags=%d)\n", ino,
1794 fi->flags);
1796 update_open_flags(lo->writeback, lo->allow_direct_io, fi);
1798 sprintf(buf, "%i", lo_fd(req, ino));
1799 fd = openat(lo->proc_self_fd, buf, fi->flags & ~O_NOFOLLOW);
1800 if (fd == -1) {
1801 return (void)fuse_reply_err(req, errno);
1804 pthread_mutex_lock(&lo->mutex);
1805 fh = lo_add_fd_mapping(req, fd);
1806 pthread_mutex_unlock(&lo->mutex);
1807 if (fh == -1) {
1808 close(fd);
1809 fuse_reply_err(req, ENOMEM);
1810 return;
1813 fi->fh = fh;
1814 if (lo->cache == CACHE_NONE) {
1815 fi->direct_io = 1;
1816 } else if (lo->cache == CACHE_ALWAYS) {
1817 fi->keep_cache = 1;
1819 fuse_reply_open(req, fi);
1822 static void lo_release(fuse_req_t req, fuse_ino_t ino,
1823 struct fuse_file_info *fi)
1825 struct lo_data *lo = lo_data(req);
1826 struct lo_map_elem *elem;
1827 int fd = -1;
1829 (void)ino;
1831 pthread_mutex_lock(&lo->mutex);
1832 elem = lo_map_get(&lo->fd_map, fi->fh);
1833 if (elem) {
1834 fd = elem->fd;
1835 elem = NULL;
1836 lo_map_remove(&lo->fd_map, fi->fh);
1838 pthread_mutex_unlock(&lo->mutex);
1840 close(fd);
1841 fuse_reply_err(req, 0);
1844 static void lo_flush(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi)
1846 int res;
1847 (void)ino;
1848 struct lo_inode *inode;
1850 inode = lo_inode(req, ino);
1851 if (!inode) {
1852 fuse_reply_err(req, EBADF);
1853 return;
1856 /* An fd is going away. Cleanup associated posix locks */
1857 pthread_mutex_lock(&inode->plock_mutex);
1858 g_hash_table_remove(inode->posix_locks, GUINT_TO_POINTER(fi->lock_owner));
1859 pthread_mutex_unlock(&inode->plock_mutex);
1861 res = close(dup(lo_fi_fd(req, fi)));
1862 lo_inode_put(lo_data(req), &inode);
1863 fuse_reply_err(req, res == -1 ? errno : 0);
1866 static void lo_fsync(fuse_req_t req, fuse_ino_t ino, int datasync,
1867 struct fuse_file_info *fi)
1869 int res;
1870 int fd;
1871 char *buf;
1873 fuse_log(FUSE_LOG_DEBUG, "lo_fsync(ino=%" PRIu64 ", fi=0x%p)\n", ino,
1874 (void *)fi);
1876 if (!fi) {
1877 struct lo_data *lo = lo_data(req);
1879 res = asprintf(&buf, "%i", lo_fd(req, ino));
1880 if (res == -1) {
1881 return (void)fuse_reply_err(req, errno);
1884 fd = openat(lo->proc_self_fd, buf, O_RDWR);
1885 free(buf);
1886 if (fd == -1) {
1887 return (void)fuse_reply_err(req, errno);
1889 } else {
1890 fd = lo_fi_fd(req, fi);
1893 if (datasync) {
1894 res = fdatasync(fd);
1895 } else {
1896 res = fsync(fd);
1898 if (!fi) {
1899 close(fd);
1901 fuse_reply_err(req, res == -1 ? errno : 0);
1904 static void lo_read(fuse_req_t req, fuse_ino_t ino, size_t size, off_t offset,
1905 struct fuse_file_info *fi)
1907 struct fuse_bufvec buf = FUSE_BUFVEC_INIT(size);
1909 fuse_log(FUSE_LOG_DEBUG,
1910 "lo_read(ino=%" PRIu64 ", size=%zd, "
1911 "off=%lu)\n",
1912 ino, size, (unsigned long)offset);
1914 buf.buf[0].flags = FUSE_BUF_IS_FD | FUSE_BUF_FD_SEEK;
1915 buf.buf[0].fd = lo_fi_fd(req, fi);
1916 buf.buf[0].pos = offset;
1918 fuse_reply_data(req, &buf);
1921 static void lo_write_buf(fuse_req_t req, fuse_ino_t ino,
1922 struct fuse_bufvec *in_buf, off_t off,
1923 struct fuse_file_info *fi)
1925 (void)ino;
1926 ssize_t res;
1927 struct fuse_bufvec out_buf = FUSE_BUFVEC_INIT(fuse_buf_size(in_buf));
1928 bool cap_fsetid_dropped = false;
1930 out_buf.buf[0].flags = FUSE_BUF_IS_FD | FUSE_BUF_FD_SEEK;
1931 out_buf.buf[0].fd = lo_fi_fd(req, fi);
1932 out_buf.buf[0].pos = off;
1934 fuse_log(FUSE_LOG_DEBUG,
1935 "lo_write_buf(ino=%" PRIu64 ", size=%zd, off=%lu)\n", ino,
1936 out_buf.buf[0].size, (unsigned long)off);
1939 * If kill_priv is set, drop CAP_FSETID which should lead to kernel
1940 * clearing setuid/setgid on file.
1942 if (fi->kill_priv) {
1943 res = drop_effective_cap("FSETID", &cap_fsetid_dropped);
1944 if (res != 0) {
1945 fuse_reply_err(req, res);
1946 return;
1950 res = fuse_buf_copy(&out_buf, in_buf);
1951 if (res < 0) {
1952 fuse_reply_err(req, -res);
1953 } else {
1954 fuse_reply_write(req, (size_t)res);
1957 if (cap_fsetid_dropped) {
1958 res = gain_effective_cap("FSETID");
1959 if (res) {
1960 fuse_log(FUSE_LOG_ERR, "Failed to gain CAP_FSETID\n");
1965 static void lo_statfs(fuse_req_t req, fuse_ino_t ino)
1967 int res;
1968 struct statvfs stbuf;
1970 res = fstatvfs(lo_fd(req, ino), &stbuf);
1971 if (res == -1) {
1972 fuse_reply_err(req, errno);
1973 } else {
1974 fuse_reply_statfs(req, &stbuf);
1978 static void lo_fallocate(fuse_req_t req, fuse_ino_t ino, int mode, off_t offset,
1979 off_t length, struct fuse_file_info *fi)
1981 int err = EOPNOTSUPP;
1982 (void)ino;
1984 #ifdef CONFIG_FALLOCATE
1985 err = fallocate(lo_fi_fd(req, fi), mode, offset, length);
1986 if (err < 0) {
1987 err = errno;
1990 #elif defined(CONFIG_POSIX_FALLOCATE)
1991 if (mode) {
1992 fuse_reply_err(req, EOPNOTSUPP);
1993 return;
1996 err = posix_fallocate(lo_fi_fd(req, fi), offset, length);
1997 #endif
1999 fuse_reply_err(req, err);
2002 static void lo_flock(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi,
2003 int op)
2005 int res;
2006 (void)ino;
2008 res = flock(lo_fi_fd(req, fi), op);
2010 fuse_reply_err(req, res == -1 ? errno : 0);
2013 static void lo_getxattr(fuse_req_t req, fuse_ino_t ino, const char *name,
2014 size_t size)
2016 struct lo_data *lo = lo_data(req);
2017 char *value = NULL;
2018 char procname[64];
2019 struct lo_inode *inode;
2020 ssize_t ret;
2021 int saverr;
2022 int fd = -1;
2024 inode = lo_inode(req, ino);
2025 if (!inode) {
2026 fuse_reply_err(req, EBADF);
2027 return;
2030 saverr = ENOSYS;
2031 if (!lo_data(req)->xattr) {
2032 goto out;
2035 fuse_log(FUSE_LOG_DEBUG, "lo_getxattr(ino=%" PRIu64 ", name=%s size=%zd)\n",
2036 ino, name, size);
2038 if (size) {
2039 value = malloc(size);
2040 if (!value) {
2041 goto out_err;
2045 sprintf(procname, "%i", inode->fd);
2047 * It is not safe to open() non-regular/non-dir files in file server
2048 * unless O_PATH is used, so use that method for regular files/dir
2049 * only (as it seems giving less performance overhead).
2050 * Otherwise, call fchdir() to avoid open().
2052 if (S_ISREG(inode->filetype) || S_ISDIR(inode->filetype)) {
2053 fd = openat(lo->proc_self_fd, procname, O_RDONLY);
2054 if (fd < 0) {
2055 goto out_err;
2057 ret = fgetxattr(fd, name, value, size);
2058 } else {
2059 /* fchdir should not fail here */
2060 assert(fchdir(lo->proc_self_fd) == 0);
2061 ret = getxattr(procname, name, value, size);
2062 assert(fchdir(lo->root.fd) == 0);
2065 if (ret == -1) {
2066 goto out_err;
2068 if (size) {
2069 saverr = 0;
2070 if (ret == 0) {
2071 goto out;
2073 fuse_reply_buf(req, value, ret);
2074 } else {
2075 fuse_reply_xattr(req, ret);
2077 out_free:
2078 free(value);
2080 if (fd >= 0) {
2081 close(fd);
2084 lo_inode_put(lo, &inode);
2085 return;
2087 out_err:
2088 saverr = errno;
2089 out:
2090 fuse_reply_err(req, saverr);
2091 goto out_free;
2094 static void lo_listxattr(fuse_req_t req, fuse_ino_t ino, size_t size)
2096 struct lo_data *lo = lo_data(req);
2097 char *value = NULL;
2098 char procname[64];
2099 struct lo_inode *inode;
2100 ssize_t ret;
2101 int saverr;
2102 int fd = -1;
2104 inode = lo_inode(req, ino);
2105 if (!inode) {
2106 fuse_reply_err(req, EBADF);
2107 return;
2110 saverr = ENOSYS;
2111 if (!lo_data(req)->xattr) {
2112 goto out;
2115 fuse_log(FUSE_LOG_DEBUG, "lo_listxattr(ino=%" PRIu64 ", size=%zd)\n", ino,
2116 size);
2118 if (size) {
2119 value = malloc(size);
2120 if (!value) {
2121 goto out_err;
2125 sprintf(procname, "%i", inode->fd);
2126 if (S_ISREG(inode->filetype) || S_ISDIR(inode->filetype)) {
2127 fd = openat(lo->proc_self_fd, procname, O_RDONLY);
2128 if (fd < 0) {
2129 goto out_err;
2131 ret = flistxattr(fd, value, size);
2132 } else {
2133 /* fchdir should not fail here */
2134 assert(fchdir(lo->proc_self_fd) == 0);
2135 ret = listxattr(procname, value, size);
2136 assert(fchdir(lo->root.fd) == 0);
2139 if (ret == -1) {
2140 goto out_err;
2142 if (size) {
2143 saverr = 0;
2144 if (ret == 0) {
2145 goto out;
2147 fuse_reply_buf(req, value, ret);
2148 } else {
2149 fuse_reply_xattr(req, ret);
2151 out_free:
2152 free(value);
2154 if (fd >= 0) {
2155 close(fd);
2158 lo_inode_put(lo, &inode);
2159 return;
2161 out_err:
2162 saverr = errno;
2163 out:
2164 fuse_reply_err(req, saverr);
2165 goto out_free;
2168 static void lo_setxattr(fuse_req_t req, fuse_ino_t ino, const char *name,
2169 const char *value, size_t size, int flags)
2171 char procname[64];
2172 struct lo_data *lo = lo_data(req);
2173 struct lo_inode *inode;
2174 ssize_t ret;
2175 int saverr;
2176 int fd = -1;
2178 inode = lo_inode(req, ino);
2179 if (!inode) {
2180 fuse_reply_err(req, EBADF);
2181 return;
2184 saverr = ENOSYS;
2185 if (!lo_data(req)->xattr) {
2186 goto out;
2189 fuse_log(FUSE_LOG_DEBUG, "lo_setxattr(ino=%" PRIu64
2190 ", name=%s value=%s size=%zd)\n", ino, name, value, size);
2192 sprintf(procname, "%i", inode->fd);
2193 if (S_ISREG(inode->filetype) || S_ISDIR(inode->filetype)) {
2194 fd = openat(lo->proc_self_fd, procname, O_RDONLY);
2195 if (fd < 0) {
2196 saverr = errno;
2197 goto out;
2199 ret = fsetxattr(fd, name, value, size, flags);
2200 } else {
2201 /* fchdir should not fail here */
2202 assert(fchdir(lo->proc_self_fd) == 0);
2203 ret = setxattr(procname, name, value, size, flags);
2204 assert(fchdir(lo->root.fd) == 0);
2207 saverr = ret == -1 ? errno : 0;
2209 out:
2210 if (fd >= 0) {
2211 close(fd);
2214 lo_inode_put(lo, &inode);
2215 fuse_reply_err(req, saverr);
2218 static void lo_removexattr(fuse_req_t req, fuse_ino_t ino, const char *name)
2220 char procname[64];
2221 struct lo_data *lo = lo_data(req);
2222 struct lo_inode *inode;
2223 ssize_t ret;
2224 int saverr;
2225 int fd = -1;
2227 inode = lo_inode(req, ino);
2228 if (!inode) {
2229 fuse_reply_err(req, EBADF);
2230 return;
2233 saverr = ENOSYS;
2234 if (!lo_data(req)->xattr) {
2235 goto out;
2238 fuse_log(FUSE_LOG_DEBUG, "lo_removexattr(ino=%" PRIu64 ", name=%s)\n", ino,
2239 name);
2241 sprintf(procname, "%i", inode->fd);
2242 if (S_ISREG(inode->filetype) || S_ISDIR(inode->filetype)) {
2243 fd = openat(lo->proc_self_fd, procname, O_RDONLY);
2244 if (fd < 0) {
2245 saverr = errno;
2246 goto out;
2248 ret = fremovexattr(fd, name);
2249 } else {
2250 /* fchdir should not fail here */
2251 assert(fchdir(lo->proc_self_fd) == 0);
2252 ret = removexattr(procname, name);
2253 assert(fchdir(lo->root.fd) == 0);
2256 saverr = ret == -1 ? errno : 0;
2258 out:
2259 if (fd >= 0) {
2260 close(fd);
2263 lo_inode_put(lo, &inode);
2264 fuse_reply_err(req, saverr);
2267 #ifdef HAVE_COPY_FILE_RANGE
2268 static void lo_copy_file_range(fuse_req_t req, fuse_ino_t ino_in, off_t off_in,
2269 struct fuse_file_info *fi_in, fuse_ino_t ino_out,
2270 off_t off_out, struct fuse_file_info *fi_out,
2271 size_t len, int flags)
2273 int in_fd, out_fd;
2274 ssize_t res;
2276 in_fd = lo_fi_fd(req, fi_in);
2277 out_fd = lo_fi_fd(req, fi_out);
2279 fuse_log(FUSE_LOG_DEBUG,
2280 "lo_copy_file_range(ino=%" PRIu64 "/fd=%d, "
2281 "off=%lu, ino=%" PRIu64 "/fd=%d, "
2282 "off=%lu, size=%zd, flags=0x%x)\n",
2283 ino_in, in_fd, off_in, ino_out, out_fd, off_out, len, flags);
2285 res = copy_file_range(in_fd, &off_in, out_fd, &off_out, len, flags);
2286 if (res < 0) {
2287 fuse_reply_err(req, errno);
2288 } else {
2289 fuse_reply_write(req, res);
2292 #endif
2294 static void lo_lseek(fuse_req_t req, fuse_ino_t ino, off_t off, int whence,
2295 struct fuse_file_info *fi)
2297 off_t res;
2299 (void)ino;
2300 res = lseek(lo_fi_fd(req, fi), off, whence);
2301 if (res != -1) {
2302 fuse_reply_lseek(req, res);
2303 } else {
2304 fuse_reply_err(req, errno);
2308 static void lo_destroy(void *userdata)
2310 struct lo_data *lo = (struct lo_data *)userdata;
2312 pthread_mutex_lock(&lo->mutex);
2313 while (true) {
2314 GHashTableIter iter;
2315 gpointer key, value;
2317 g_hash_table_iter_init(&iter, lo->inodes);
2318 if (!g_hash_table_iter_next(&iter, &key, &value)) {
2319 break;
2322 struct lo_inode *inode = value;
2323 unref_inode(lo, inode, inode->nlookup);
2325 pthread_mutex_unlock(&lo->mutex);
2328 static struct fuse_lowlevel_ops lo_oper = {
2329 .init = lo_init,
2330 .lookup = lo_lookup,
2331 .mkdir = lo_mkdir,
2332 .mknod = lo_mknod,
2333 .symlink = lo_symlink,
2334 .link = lo_link,
2335 .unlink = lo_unlink,
2336 .rmdir = lo_rmdir,
2337 .rename = lo_rename,
2338 .forget = lo_forget,
2339 .forget_multi = lo_forget_multi,
2340 .getattr = lo_getattr,
2341 .setattr = lo_setattr,
2342 .readlink = lo_readlink,
2343 .opendir = lo_opendir,
2344 .readdir = lo_readdir,
2345 .readdirplus = lo_readdirplus,
2346 .releasedir = lo_releasedir,
2347 .fsyncdir = lo_fsyncdir,
2348 .create = lo_create,
2349 .getlk = lo_getlk,
2350 .setlk = lo_setlk,
2351 .open = lo_open,
2352 .release = lo_release,
2353 .flush = lo_flush,
2354 .fsync = lo_fsync,
2355 .read = lo_read,
2356 .write_buf = lo_write_buf,
2357 .statfs = lo_statfs,
2358 .fallocate = lo_fallocate,
2359 .flock = lo_flock,
2360 .getxattr = lo_getxattr,
2361 .listxattr = lo_listxattr,
2362 .setxattr = lo_setxattr,
2363 .removexattr = lo_removexattr,
2364 #ifdef HAVE_COPY_FILE_RANGE
2365 .copy_file_range = lo_copy_file_range,
2366 #endif
2367 .lseek = lo_lseek,
2368 .destroy = lo_destroy,
2371 /* Print vhost-user.json backend program capabilities */
2372 static void print_capabilities(void)
2374 printf("{\n");
2375 printf(" \"type\": \"fs\"\n");
2376 printf("}\n");
2380 * Drop all Linux capabilities because the wait parent process only needs to
2381 * sit in waitpid(2) and terminate.
2383 static void setup_wait_parent_capabilities(void)
2385 capng_setpid(syscall(SYS_gettid));
2386 capng_clear(CAPNG_SELECT_BOTH);
2387 capng_apply(CAPNG_SELECT_BOTH);
2391 * Move to a new mount, net, and pid namespaces to isolate this process.
2393 static void setup_namespaces(struct lo_data *lo, struct fuse_session *se)
2395 pid_t child;
2396 char template[] = "virtiofsd-XXXXXX";
2397 char *tmpdir;
2400 * Create a new pid namespace for *child* processes. We'll have to
2401 * fork in order to enter the new pid namespace. A new mount namespace
2402 * is also needed so that we can remount /proc for the new pid
2403 * namespace.
2405 * Our UNIX domain sockets have been created. Now we can move to
2406 * an empty network namespace to prevent TCP/IP and other network
2407 * activity in case this process is compromised.
2409 if (unshare(CLONE_NEWPID | CLONE_NEWNS | CLONE_NEWNET) != 0) {
2410 fuse_log(FUSE_LOG_ERR, "unshare(CLONE_NEWPID | CLONE_NEWNS): %m\n");
2411 exit(1);
2414 child = fork();
2415 if (child < 0) {
2416 fuse_log(FUSE_LOG_ERR, "fork() failed: %m\n");
2417 exit(1);
2419 if (child > 0) {
2420 pid_t waited;
2421 int wstatus;
2423 setup_wait_parent_capabilities();
2425 /* The parent waits for the child */
2426 do {
2427 waited = waitpid(child, &wstatus, 0);
2428 } while (waited < 0 && errno == EINTR && !se->exited);
2430 /* We were terminated by a signal, see fuse_signals.c */
2431 if (se->exited) {
2432 exit(0);
2435 if (WIFEXITED(wstatus)) {
2436 exit(WEXITSTATUS(wstatus));
2439 exit(1);
2442 /* Send us SIGTERM when the parent thread terminates, see prctl(2) */
2443 prctl(PR_SET_PDEATHSIG, SIGTERM);
2446 * If the mounts have shared propagation then we want to opt out so our
2447 * mount changes don't affect the parent mount namespace.
2449 if (mount(NULL, "/", NULL, MS_REC | MS_SLAVE, NULL) < 0) {
2450 fuse_log(FUSE_LOG_ERR, "mount(/, MS_REC|MS_SLAVE): %m\n");
2451 exit(1);
2454 /* The child must remount /proc to use the new pid namespace */
2455 if (mount("proc", "/proc", "proc",
2456 MS_NODEV | MS_NOEXEC | MS_NOSUID | MS_RELATIME, NULL) < 0) {
2457 fuse_log(FUSE_LOG_ERR, "mount(/proc): %m\n");
2458 exit(1);
2461 tmpdir = mkdtemp(template);
2462 if (!tmpdir) {
2463 fuse_log(FUSE_LOG_ERR, "tmpdir(%s): %m\n", template);
2464 exit(1);
2467 if (mount("/proc/self/fd", tmpdir, NULL, MS_BIND, NULL) < 0) {
2468 fuse_log(FUSE_LOG_ERR, "mount(/proc/self/fd, %s, MS_BIND): %m\n",
2469 tmpdir);
2470 exit(1);
2473 /* Now we can get our /proc/self/fd directory file descriptor */
2474 lo->proc_self_fd = open(tmpdir, O_PATH);
2475 if (lo->proc_self_fd == -1) {
2476 fuse_log(FUSE_LOG_ERR, "open(%s, O_PATH): %m\n", tmpdir);
2477 exit(1);
2480 if (umount2(tmpdir, MNT_DETACH) < 0) {
2481 fuse_log(FUSE_LOG_ERR, "umount2(%s, MNT_DETACH): %m\n", tmpdir);
2482 exit(1);
2485 if (rmdir(tmpdir) < 0) {
2486 fuse_log(FUSE_LOG_ERR, "rmdir(%s): %m\n", tmpdir);
2491 * Capture the capability state, we'll need to restore this for individual
2492 * threads later; see load_capng.
2494 static void setup_capng(void)
2496 /* Note this accesses /proc so has to happen before the sandbox */
2497 if (capng_get_caps_process()) {
2498 fuse_log(FUSE_LOG_ERR, "capng_get_caps_process\n");
2499 exit(1);
2501 pthread_mutex_init(&cap.mutex, NULL);
2502 pthread_mutex_lock(&cap.mutex);
2503 cap.saved = capng_save_state();
2504 if (!cap.saved) {
2505 fuse_log(FUSE_LOG_ERR, "capng_save_state\n");
2506 exit(1);
2508 pthread_mutex_unlock(&cap.mutex);
2511 static void cleanup_capng(void)
2513 free(cap.saved);
2514 cap.saved = NULL;
2515 pthread_mutex_destroy(&cap.mutex);
2520 * Make the source directory our root so symlinks cannot escape and no other
2521 * files are accessible. Assumes unshare(CLONE_NEWNS) was already called.
2523 static void setup_mounts(const char *source)
2525 int oldroot;
2526 int newroot;
2528 if (mount(source, source, NULL, MS_BIND | MS_REC, NULL) < 0) {
2529 fuse_log(FUSE_LOG_ERR, "mount(%s, %s, MS_BIND): %m\n", source, source);
2530 exit(1);
2533 /* This magic is based on lxc's lxc_pivot_root() */
2534 oldroot = open("/", O_DIRECTORY | O_RDONLY | O_CLOEXEC);
2535 if (oldroot < 0) {
2536 fuse_log(FUSE_LOG_ERR, "open(/): %m\n");
2537 exit(1);
2540 newroot = open(source, O_DIRECTORY | O_RDONLY | O_CLOEXEC);
2541 if (newroot < 0) {
2542 fuse_log(FUSE_LOG_ERR, "open(%s): %m\n", source);
2543 exit(1);
2546 if (fchdir(newroot) < 0) {
2547 fuse_log(FUSE_LOG_ERR, "fchdir(newroot): %m\n");
2548 exit(1);
2551 if (syscall(__NR_pivot_root, ".", ".") < 0) {
2552 fuse_log(FUSE_LOG_ERR, "pivot_root(., .): %m\n");
2553 exit(1);
2556 if (fchdir(oldroot) < 0) {
2557 fuse_log(FUSE_LOG_ERR, "fchdir(oldroot): %m\n");
2558 exit(1);
2561 if (mount("", ".", "", MS_SLAVE | MS_REC, NULL) < 0) {
2562 fuse_log(FUSE_LOG_ERR, "mount(., MS_SLAVE | MS_REC): %m\n");
2563 exit(1);
2566 if (umount2(".", MNT_DETACH) < 0) {
2567 fuse_log(FUSE_LOG_ERR, "umount2(., MNT_DETACH): %m\n");
2568 exit(1);
2571 if (fchdir(newroot) < 0) {
2572 fuse_log(FUSE_LOG_ERR, "fchdir(newroot): %m\n");
2573 exit(1);
2576 close(newroot);
2577 close(oldroot);
2581 * Only keep whitelisted capabilities that are needed for file system operation
2582 * The (possibly NULL) modcaps_in string passed in is free'd before exit.
2584 static void setup_capabilities(char *modcaps_in)
2586 char *modcaps = modcaps_in;
2587 pthread_mutex_lock(&cap.mutex);
2588 capng_restore_state(&cap.saved);
2591 * Whitelist file system-related capabilities that are needed for a file
2592 * server to act like root. Drop everything else like networking and
2593 * sysadmin capabilities.
2595 * Exclusions:
2596 * 1. CAP_LINUX_IMMUTABLE is not included because it's only used via ioctl
2597 * and we don't support that.
2598 * 2. CAP_MAC_OVERRIDE is not included because it only seems to be
2599 * used by the Smack LSM. Omit it until there is demand for it.
2601 capng_setpid(syscall(SYS_gettid));
2602 capng_clear(CAPNG_SELECT_BOTH);
2603 if (capng_updatev(CAPNG_ADD, CAPNG_PERMITTED | CAPNG_EFFECTIVE,
2604 CAP_CHOWN,
2605 CAP_DAC_OVERRIDE,
2606 CAP_FOWNER,
2607 CAP_FSETID,
2608 CAP_SETGID,
2609 CAP_SETUID,
2610 CAP_MKNOD,
2611 CAP_SETFCAP,
2612 -1)) {
2613 fuse_log(FUSE_LOG_ERR, "%s: capng_updatev failed\n", __func__);
2614 exit(1);
2618 * The modcaps option is a colon separated list of caps,
2619 * each preceded by either + or -.
2621 while (modcaps) {
2622 capng_act_t action;
2623 int cap;
2625 char *next = strchr(modcaps, ':');
2626 if (next) {
2627 *next = '\0';
2628 next++;
2631 switch (modcaps[0]) {
2632 case '+':
2633 action = CAPNG_ADD;
2634 break;
2636 case '-':
2637 action = CAPNG_DROP;
2638 break;
2640 default:
2641 fuse_log(FUSE_LOG_ERR,
2642 "%s: Expecting '+'/'-' in modcaps but found '%c'\n",
2643 __func__, modcaps[0]);
2644 exit(1);
2646 cap = capng_name_to_capability(modcaps + 1);
2647 if (cap < 0) {
2648 fuse_log(FUSE_LOG_ERR, "%s: Unknown capability '%s'\n", __func__,
2649 modcaps);
2650 exit(1);
2652 if (capng_update(action, CAPNG_PERMITTED | CAPNG_EFFECTIVE, cap)) {
2653 fuse_log(FUSE_LOG_ERR, "%s: capng_update failed for '%s'\n",
2654 __func__, modcaps);
2655 exit(1);
2658 modcaps = next;
2660 g_free(modcaps_in);
2662 if (capng_apply(CAPNG_SELECT_BOTH)) {
2663 fuse_log(FUSE_LOG_ERR, "%s: capng_apply failed\n", __func__);
2664 exit(1);
2667 cap.saved = capng_save_state();
2668 if (!cap.saved) {
2669 fuse_log(FUSE_LOG_ERR, "%s: capng_save_state failed\n", __func__);
2670 exit(1);
2672 pthread_mutex_unlock(&cap.mutex);
2676 * Lock down this process to prevent access to other processes or files outside
2677 * source directory. This reduces the impact of arbitrary code execution bugs.
2679 static void setup_sandbox(struct lo_data *lo, struct fuse_session *se,
2680 bool enable_syslog)
2682 setup_namespaces(lo, se);
2683 setup_mounts(lo->source);
2684 setup_seccomp(enable_syslog);
2685 setup_capabilities(g_strdup(lo->modcaps));
2688 /* Set the maximum number of open file descriptors */
2689 static void setup_nofile_rlimit(unsigned long rlimit_nofile)
2691 struct rlimit rlim = {
2692 .rlim_cur = rlimit_nofile,
2693 .rlim_max = rlimit_nofile,
2696 if (rlimit_nofile == 0) {
2697 return; /* nothing to do */
2700 if (setrlimit(RLIMIT_NOFILE, &rlim) < 0) {
2701 /* Ignore SELinux denials */
2702 if (errno == EPERM) {
2703 return;
2706 fuse_log(FUSE_LOG_ERR, "setrlimit(RLIMIT_NOFILE): %m\n");
2707 exit(1);
2711 static void log_func(enum fuse_log_level level, const char *fmt, va_list ap)
2713 g_autofree char *localfmt = NULL;
2715 if (current_log_level < level) {
2716 return;
2719 if (current_log_level == FUSE_LOG_DEBUG) {
2720 if (!use_syslog) {
2721 localfmt = g_strdup_printf("[%" PRId64 "] [ID: %08ld] %s",
2722 get_clock(), syscall(__NR_gettid), fmt);
2723 } else {
2724 localfmt = g_strdup_printf("[ID: %08ld] %s", syscall(__NR_gettid),
2725 fmt);
2727 fmt = localfmt;
2730 if (use_syslog) {
2731 int priority = LOG_ERR;
2732 switch (level) {
2733 case FUSE_LOG_EMERG:
2734 priority = LOG_EMERG;
2735 break;
2736 case FUSE_LOG_ALERT:
2737 priority = LOG_ALERT;
2738 break;
2739 case FUSE_LOG_CRIT:
2740 priority = LOG_CRIT;
2741 break;
2742 case FUSE_LOG_ERR:
2743 priority = LOG_ERR;
2744 break;
2745 case FUSE_LOG_WARNING:
2746 priority = LOG_WARNING;
2747 break;
2748 case FUSE_LOG_NOTICE:
2749 priority = LOG_NOTICE;
2750 break;
2751 case FUSE_LOG_INFO:
2752 priority = LOG_INFO;
2753 break;
2754 case FUSE_LOG_DEBUG:
2755 priority = LOG_DEBUG;
2756 break;
2758 vsyslog(priority, fmt, ap);
2759 } else {
2760 vfprintf(stderr, fmt, ap);
2764 static void setup_root(struct lo_data *lo, struct lo_inode *root)
2766 int fd, res;
2767 struct stat stat;
2769 fd = open("/", O_PATH);
2770 if (fd == -1) {
2771 fuse_log(FUSE_LOG_ERR, "open(%s, O_PATH): %m\n", lo->source);
2772 exit(1);
2775 res = fstatat(fd, "", &stat, AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW);
2776 if (res == -1) {
2777 fuse_log(FUSE_LOG_ERR, "fstatat(%s): %m\n", lo->source);
2778 exit(1);
2781 root->filetype = S_IFDIR;
2782 root->fd = fd;
2783 root->key.ino = stat.st_ino;
2784 root->key.dev = stat.st_dev;
2785 root->nlookup = 2;
2786 g_atomic_int_set(&root->refcount, 2);
2789 static guint lo_key_hash(gconstpointer key)
2791 const struct lo_key *lkey = key;
2793 return (guint)lkey->ino + (guint)lkey->dev;
2796 static gboolean lo_key_equal(gconstpointer a, gconstpointer b)
2798 const struct lo_key *la = a;
2799 const struct lo_key *lb = b;
2801 return la->ino == lb->ino && la->dev == lb->dev;
2804 static void fuse_lo_data_cleanup(struct lo_data *lo)
2806 if (lo->inodes) {
2807 g_hash_table_destroy(lo->inodes);
2809 lo_map_destroy(&lo->fd_map);
2810 lo_map_destroy(&lo->dirp_map);
2811 lo_map_destroy(&lo->ino_map);
2813 if (lo->proc_self_fd >= 0) {
2814 close(lo->proc_self_fd);
2817 if (lo->root.fd >= 0) {
2818 close(lo->root.fd);
2821 free(lo->source);
2824 int main(int argc, char *argv[])
2826 struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
2827 struct fuse_session *se;
2828 struct fuse_cmdline_opts opts;
2829 struct lo_data lo = {
2830 .debug = 0,
2831 .writeback = 0,
2832 .posix_lock = 0,
2833 .allow_direct_io = 0,
2834 .proc_self_fd = -1,
2836 struct lo_map_elem *root_elem;
2837 int ret = -1;
2839 /* Don't mask creation mode, kernel already did that */
2840 umask(0);
2842 pthread_mutex_init(&lo.mutex, NULL);
2843 lo.inodes = g_hash_table_new(lo_key_hash, lo_key_equal);
2844 lo.root.fd = -1;
2845 lo.root.fuse_ino = FUSE_ROOT_ID;
2846 lo.cache = CACHE_AUTO;
2849 * Set up the ino map like this:
2850 * [0] Reserved (will not be used)
2851 * [1] Root inode
2853 lo_map_init(&lo.ino_map);
2854 lo_map_reserve(&lo.ino_map, 0)->in_use = false;
2855 root_elem = lo_map_reserve(&lo.ino_map, lo.root.fuse_ino);
2856 root_elem->inode = &lo.root;
2858 lo_map_init(&lo.dirp_map);
2859 lo_map_init(&lo.fd_map);
2861 if (fuse_parse_cmdline(&args, &opts) != 0) {
2862 goto err_out1;
2864 fuse_set_log_func(log_func);
2865 use_syslog = opts.syslog;
2866 if (use_syslog) {
2867 openlog("virtiofsd", LOG_PID, LOG_DAEMON);
2870 if (opts.show_help) {
2871 printf("usage: %s [options]\n\n", argv[0]);
2872 fuse_cmdline_help();
2873 printf(" -o source=PATH shared directory tree\n");
2874 fuse_lowlevel_help();
2875 ret = 0;
2876 goto err_out1;
2877 } else if (opts.show_version) {
2878 fuse_lowlevel_version();
2879 ret = 0;
2880 goto err_out1;
2881 } else if (opts.print_capabilities) {
2882 print_capabilities();
2883 ret = 0;
2884 goto err_out1;
2887 if (fuse_opt_parse(&args, &lo, lo_opts, NULL) == -1) {
2888 goto err_out1;
2892 * log_level is 0 if not configured via cmd options (0 is LOG_EMERG,
2893 * and we don't use this log level).
2895 if (opts.log_level != 0) {
2896 current_log_level = opts.log_level;
2898 lo.debug = opts.debug;
2899 if (lo.debug) {
2900 current_log_level = FUSE_LOG_DEBUG;
2902 if (lo.source) {
2903 struct stat stat;
2904 int res;
2906 res = lstat(lo.source, &stat);
2907 if (res == -1) {
2908 fuse_log(FUSE_LOG_ERR, "failed to stat source (\"%s\"): %m\n",
2909 lo.source);
2910 exit(1);
2912 if (!S_ISDIR(stat.st_mode)) {
2913 fuse_log(FUSE_LOG_ERR, "source is not a directory\n");
2914 exit(1);
2916 } else {
2917 lo.source = strdup("/");
2919 if (!lo.timeout_set) {
2920 switch (lo.cache) {
2921 case CACHE_NONE:
2922 lo.timeout = 0.0;
2923 break;
2925 case CACHE_AUTO:
2926 lo.timeout = 1.0;
2927 break;
2929 case CACHE_ALWAYS:
2930 lo.timeout = 86400.0;
2931 break;
2933 } else if (lo.timeout < 0) {
2934 fuse_log(FUSE_LOG_ERR, "timeout is negative (%lf)\n", lo.timeout);
2935 exit(1);
2938 se = fuse_session_new(&args, &lo_oper, sizeof(lo_oper), &lo);
2939 if (se == NULL) {
2940 goto err_out1;
2943 if (fuse_set_signal_handlers(se) != 0) {
2944 goto err_out2;
2947 if (fuse_session_mount(se) != 0) {
2948 goto err_out3;
2951 fuse_daemonize(opts.foreground);
2953 setup_nofile_rlimit(opts.rlimit_nofile);
2955 /* Must be before sandbox since it wants /proc */
2956 setup_capng();
2958 setup_sandbox(&lo, se, opts.syslog);
2960 setup_root(&lo, &lo.root);
2961 /* Block until ctrl+c or fusermount -u */
2962 ret = virtio_loop(se);
2964 fuse_session_unmount(se);
2965 cleanup_capng();
2966 err_out3:
2967 fuse_remove_signal_handlers(se);
2968 err_out2:
2969 fuse_session_destroy(se);
2970 err_out1:
2971 fuse_opt_free_args(&args);
2973 fuse_lo_data_cleanup(&lo);
2975 return ret ? 1 : 0;