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.
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
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).
31 * gcc -Wall passthrough_ll.c `pkg-config fuse3 --cflags --libs` -o
35 * \include passthrough_ll.c
38 #include "qemu/osdep.h"
39 #include "qemu/timer.h"
40 #include "fuse_virtio.h"
42 #include "fuse_lowlevel.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>
63 #include <sys/xattr.h>
67 #include "passthrough_helpers.h"
70 /* Keep track of inode posix locks for each owner. */
71 struct lo_inode_plock
{
73 int fd
; /* fd for OFD locks */
78 struct lo_inode
*inode
;
86 /* Maps FUSE fh or ino values to internal objects */
88 struct lo_map_elem
*elems
;
102 * Atomic reference count for this object. The nlookup field holds a
103 * reference and release it when nlookup reaches 0.
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.
123 pthread_mutex_t plock_mutex
;
124 GHashTable
*posix_locks
; /* protected by lo_inode->plock_mutex */
141 pthread_mutex_t mutex
;
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/ */
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 },
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
,
190 pthread_mutex_t mutex
;
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
, '/')) {
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)
227 pthread_mutex_lock(&cap
.mutex
);
228 capng_restore_state(&cap
.saved
);
230 * restore_state free's the saved copy
233 cap
.saved
= capng_save_state();
235 fuse_log(FUSE_LOG_ERR
, "capng_save_state (thread)\n");
238 pthread_mutex_unlock(&cap
.mutex
);
241 * We want to use the loaded state for our pid,
244 capng_setpid(syscall(SYS_gettid
));
251 * Helpers for dropping and regaining effective capabilities. Returns 0
252 * on success, error otherwise
254 static int drop_effective_cap(const char *cap_name
, bool *cap_dropped
)
258 cap
= capng_name_to_capability(cap_name
);
261 fuse_log(FUSE_LOG_ERR
, "capng_name_to_capability(%s) failed:%s\n",
262 cap_name
, strerror(errno
));
268 fuse_log(FUSE_LOG_ERR
, "load_capng() failed\n");
272 /* We dont have this capability in effective set already. */
273 if (!capng_have_capability(CAPNG_EFFECTIVE
, cap
)) {
278 if (capng_update(CAPNG_DROP
, CAPNG_EFFECTIVE
, cap
)) {
280 fuse_log(FUSE_LOG_ERR
, "capng_update(DROP,) failed\n");
284 if (capng_apply(CAPNG_SELECT_CAPS
)) {
286 fuse_log(FUSE_LOG_ERR
, "drop:capng_apply() failed\n");
299 static int gain_effective_cap(const char *cap_name
)
304 cap
= capng_name_to_capability(cap_name
);
307 fuse_log(FUSE_LOG_ERR
, "capng_name_to_capability(%s) failed:%s\n",
308 cap_name
, strerror(errno
));
314 fuse_log(FUSE_LOG_ERR
, "load_capng() failed\n");
318 if (capng_update(CAPNG_ADD
, CAPNG_EFFECTIVE
, cap
)) {
320 fuse_log(FUSE_LOG_ERR
, "capng_update(ADD,) failed\n");
324 if (capng_apply(CAPNG_SELECT_CAPS
)) {
326 fuse_log(FUSE_LOG_ERR
, "gain:capng_apply() failed\n");
335 static void lo_map_init(struct lo_map
*map
)
342 static void lo_map_destroy(struct lo_map
*map
)
347 static int lo_map_grow(struct lo_map
*map
, size_t new_nelems
)
349 struct lo_map_elem
*new_elems
;
352 if (new_nelems
<= map
->nelems
) {
356 new_elems
= realloc(map
->elems
, sizeof(map
->elems
[0]) * new_nelems
);
361 for (i
= map
->nelems
; i
< new_nelems
; i
++) {
362 new_elems
[i
].freelist
= i
+ 1;
363 new_elems
[i
].in_use
= false;
365 new_elems
[new_nelems
- 1].freelist
= -1;
367 map
->elems
= new_elems
;
368 map
->freelist
= map
->nelems
;
369 map
->nelems
= new_nelems
;
373 static struct lo_map_elem
*lo_map_alloc_elem(struct lo_map
*map
)
375 struct lo_map_elem
*elem
;
377 if (map
->freelist
== -1 && !lo_map_grow(map
, map
->nelems
+ 256)) {
381 elem
= &map
->elems
[map
->freelist
];
382 map
->freelist
= elem
->freelist
;
389 static struct lo_map_elem
*lo_map_reserve(struct lo_map
*map
, size_t key
)
393 if (!lo_map_grow(map
, key
+ 1)) {
397 for (prev
= &map
->freelist
; *prev
!= -1;
398 prev
= &map
->elems
[*prev
].freelist
) {
400 struct lo_map_elem
*elem
= &map
->elems
[key
];
402 *prev
= elem
->freelist
;
410 static struct lo_map_elem
*lo_map_get(struct lo_map
*map
, size_t key
)
412 if (key
>= map
->nelems
) {
415 if (!map
->elems
[key
].in_use
) {
418 return &map
->elems
[key
];
421 static void lo_map_remove(struct lo_map
*map
, size_t key
)
423 struct lo_map_elem
*elem
;
425 if (key
>= map
->nelems
) {
429 elem
= &map
->elems
[key
];
434 elem
->in_use
= false;
436 elem
->freelist
= map
->freelist
;
440 /* Assumes lo->mutex is held */
441 static ssize_t
lo_add_fd_mapping(fuse_req_t req
, int fd
)
443 struct lo_map_elem
*elem
;
445 elem
= lo_map_alloc_elem(&lo_data(req
)->fd_map
);
451 return elem
- lo_data(req
)->fd_map
.elems
;
454 /* Assumes lo->mutex is held */
455 static ssize_t
lo_add_dirp_mapping(fuse_req_t req
, struct lo_dirp
*dirp
)
457 struct lo_map_elem
*elem
;
459 elem
= lo_map_alloc_elem(&lo_data(req
)->dirp_map
);
465 return elem
- lo_data(req
)->dirp_map
.elems
;
468 /* Assumes lo->mutex is held */
469 static ssize_t
lo_add_inode_mapping(fuse_req_t req
, struct lo_inode
*inode
)
471 struct lo_map_elem
*elem
;
473 elem
= lo_map_alloc_elem(&lo_data(req
)->ino_map
);
479 return elem
- lo_data(req
)->ino_map
.elems
;
482 static void lo_inode_put(struct lo_data
*lo
, struct lo_inode
**inodep
)
484 struct lo_inode
*inode
= *inodep
;
492 if (g_atomic_int_dec_and_test(&inode
->refcount
)) {
498 /* Caller must release refcount using lo_inode_put() */
499 static struct lo_inode
*lo_inode(fuse_req_t req
, fuse_ino_t ino
)
501 struct lo_data
*lo
= lo_data(req
);
502 struct lo_map_elem
*elem
;
504 pthread_mutex_lock(&lo
->mutex
);
505 elem
= lo_map_get(&lo
->ino_map
, ino
);
507 g_atomic_int_inc(&elem
->inode
->refcount
);
509 pthread_mutex_unlock(&lo
->mutex
);
519 * TODO Remove this helper and force callers to hold an inode refcount until
520 * they are done with the fd. This will be done in a later patch to make
523 static int lo_fd(fuse_req_t req
, fuse_ino_t ino
)
525 struct lo_inode
*inode
= lo_inode(req
, ino
);
533 lo_inode_put(lo_data(req
), &inode
);
537 static void lo_init(void *userdata
, struct fuse_conn_info
*conn
)
539 struct lo_data
*lo
= (struct lo_data
*)userdata
;
541 if (conn
->capable
& FUSE_CAP_EXPORT_SUPPORT
) {
542 conn
->want
|= FUSE_CAP_EXPORT_SUPPORT
;
545 if (lo
->writeback
&& conn
->capable
& FUSE_CAP_WRITEBACK_CACHE
) {
546 fuse_log(FUSE_LOG_DEBUG
, "lo_init: activating writeback\n");
547 conn
->want
|= FUSE_CAP_WRITEBACK_CACHE
;
549 if (conn
->capable
& FUSE_CAP_FLOCK_LOCKS
) {
551 fuse_log(FUSE_LOG_DEBUG
, "lo_init: activating flock locks\n");
552 conn
->want
|= FUSE_CAP_FLOCK_LOCKS
;
554 fuse_log(FUSE_LOG_DEBUG
, "lo_init: disabling flock locks\n");
555 conn
->want
&= ~FUSE_CAP_FLOCK_LOCKS
;
559 if (conn
->capable
& FUSE_CAP_POSIX_LOCKS
) {
560 if (lo
->posix_lock
) {
561 fuse_log(FUSE_LOG_DEBUG
, "lo_init: activating posix locks\n");
562 conn
->want
|= FUSE_CAP_POSIX_LOCKS
;
564 fuse_log(FUSE_LOG_DEBUG
, "lo_init: disabling posix locks\n");
565 conn
->want
&= ~FUSE_CAP_POSIX_LOCKS
;
569 if ((lo
->cache
== CACHE_NONE
&& !lo
->readdirplus_set
) ||
570 lo
->readdirplus_clear
) {
571 fuse_log(FUSE_LOG_DEBUG
, "lo_init: disabling readdirplus\n");
572 conn
->want
&= ~FUSE_CAP_READDIRPLUS
;
576 static void lo_getattr(fuse_req_t req
, fuse_ino_t ino
,
577 struct fuse_file_info
*fi
)
581 struct lo_data
*lo
= lo_data(req
);
586 fstatat(lo_fd(req
, ino
), "", &buf
, AT_EMPTY_PATH
| AT_SYMLINK_NOFOLLOW
);
588 return (void)fuse_reply_err(req
, errno
);
591 fuse_reply_attr(req
, &buf
, lo
->timeout
);
595 * Increments parent->nlookup and caller must release refcount using
596 * lo_inode_put(&parent).
598 static int lo_parent_and_name(struct lo_data
*lo
, struct lo_inode
*inode
,
599 char path
[PATH_MAX
], struct lo_inode
**parent
)
609 sprintf(procname
, "%i", inode
->fd
);
611 res
= readlinkat(lo
->proc_self_fd
, procname
, path
, PATH_MAX
);
613 fuse_log(FUSE_LOG_WARNING
, "%s: readlink failed: %m\n", __func__
);
617 if (res
>= PATH_MAX
) {
618 fuse_log(FUSE_LOG_WARNING
, "%s: readlink overflowed\n", __func__
);
623 last
= strrchr(path
, '/');
625 /* Shouldn't happen */
628 "%s: INTERNAL ERROR: bad path read from proc\n", __func__
);
633 pthread_mutex_lock(&lo
->mutex
);
635 g_atomic_int_inc(&p
->refcount
);
636 pthread_mutex_unlock(&lo
->mutex
);
639 res
= fstatat(AT_FDCWD
, last
== path
? "/" : path
, &stat
, 0);
642 fuse_log(FUSE_LOG_WARNING
,
643 "%s: failed to stat parent: %m\n", __func__
);
647 p
= lo_find(lo
, &stat
);
650 fuse_log(FUSE_LOG_WARNING
,
651 "%s: failed to find parent\n", __func__
);
657 res
= fstatat(p
->fd
, last
, &stat
, AT_SYMLINK_NOFOLLOW
);
660 fuse_log(FUSE_LOG_WARNING
,
661 "%s: failed to stat last\n", __func__
);
665 if (stat
.st_dev
!= inode
->key
.dev
|| stat
.st_ino
!= inode
->key
.ino
) {
667 fuse_log(FUSE_LOG_WARNING
,
668 "%s: failed to match last\n", __func__
);
673 memmove(path
, last
, strlen(last
) + 1);
678 unref_inode_lolocked(lo
, p
, 1);
679 lo_inode_put(lo
, &p
);
690 static int utimensat_empty(struct lo_data
*lo
, struct lo_inode
*inode
,
691 const struct timespec
*tv
)
694 struct lo_inode
*parent
;
697 if (inode
->is_symlink
) {
698 res
= utimensat(inode
->fd
, "", tv
, AT_EMPTY_PATH
);
699 if (res
== -1 && errno
== EINVAL
) {
700 /* Sorry, no race free way to set times on symlink. */
709 sprintf(path
, "%i", inode
->fd
);
711 return utimensat(lo
->proc_self_fd
, path
, tv
, 0);
714 res
= lo_parent_and_name(lo
, inode
, path
, &parent
);
716 res
= utimensat(parent
->fd
, path
, tv
, AT_SYMLINK_NOFOLLOW
);
717 unref_inode_lolocked(lo
, parent
, 1);
718 lo_inode_put(lo
, &parent
);
724 static int lo_fi_fd(fuse_req_t req
, struct fuse_file_info
*fi
)
726 struct lo_data
*lo
= lo_data(req
);
727 struct lo_map_elem
*elem
;
729 pthread_mutex_lock(&lo
->mutex
);
730 elem
= lo_map_get(&lo
->fd_map
, fi
->fh
);
731 pthread_mutex_unlock(&lo
->mutex
);
740 static void lo_setattr(fuse_req_t req
, fuse_ino_t ino
, struct stat
*attr
,
741 int valid
, struct fuse_file_info
*fi
)
745 struct lo_data
*lo
= lo_data(req
);
746 struct lo_inode
*inode
;
751 inode
= lo_inode(req
, ino
);
753 fuse_reply_err(req
, EBADF
);
759 /* If fi->fh is invalid we'll report EBADF later */
761 fd
= lo_fi_fd(req
, fi
);
764 if (valid
& FUSE_SET_ATTR_MODE
) {
766 res
= fchmod(fd
, attr
->st_mode
);
768 sprintf(procname
, "%i", ifd
);
769 res
= fchmodat(lo
->proc_self_fd
, procname
, attr
->st_mode
, 0);
775 if (valid
& (FUSE_SET_ATTR_UID
| FUSE_SET_ATTR_GID
)) {
776 uid_t uid
= (valid
& FUSE_SET_ATTR_UID
) ? attr
->st_uid
: (uid_t
)-1;
777 gid_t gid
= (valid
& FUSE_SET_ATTR_GID
) ? attr
->st_gid
: (gid_t
)-1;
779 res
= fchownat(ifd
, "", uid
, gid
, AT_EMPTY_PATH
| AT_SYMLINK_NOFOLLOW
);
784 if (valid
& FUSE_SET_ATTR_SIZE
) {
790 sprintf(procname
, "%i", ifd
);
791 truncfd
= openat(lo
->proc_self_fd
, procname
, O_RDWR
);
797 res
= ftruncate(truncfd
, attr
->st_size
);
807 if (valid
& (FUSE_SET_ATTR_ATIME
| FUSE_SET_ATTR_MTIME
)) {
808 struct timespec tv
[2];
812 tv
[0].tv_nsec
= UTIME_OMIT
;
813 tv
[1].tv_nsec
= UTIME_OMIT
;
815 if (valid
& FUSE_SET_ATTR_ATIME_NOW
) {
816 tv
[0].tv_nsec
= UTIME_NOW
;
817 } else if (valid
& FUSE_SET_ATTR_ATIME
) {
818 tv
[0] = attr
->st_atim
;
821 if (valid
& FUSE_SET_ATTR_MTIME_NOW
) {
822 tv
[1].tv_nsec
= UTIME_NOW
;
823 } else if (valid
& FUSE_SET_ATTR_MTIME
) {
824 tv
[1] = attr
->st_mtim
;
828 res
= futimens(fd
, tv
);
830 res
= utimensat_empty(lo
, inode
, tv
);
836 lo_inode_put(lo
, &inode
);
838 return lo_getattr(req
, ino
, fi
);
842 lo_inode_put(lo
, &inode
);
843 fuse_reply_err(req
, saverr
);
846 static struct lo_inode
*lo_find(struct lo_data
*lo
, struct stat
*st
)
849 struct lo_key key
= {
854 pthread_mutex_lock(&lo
->mutex
);
855 p
= g_hash_table_lookup(lo
->inodes
, &key
);
857 assert(p
->nlookup
> 0);
859 g_atomic_int_inc(&p
->refcount
);
861 pthread_mutex_unlock(&lo
->mutex
);
866 /* value_destroy_func for posix_locks GHashTable */
867 static void posix_locks_value_destroy(gpointer data
)
869 struct lo_inode_plock
*plock
= data
;
872 * We had used open() for locks and had only one fd. So
873 * closing this fd should release all OFD locks.
880 * Increments nlookup and caller must release refcount using
881 * lo_inode_put(&parent).
883 static int lo_do_lookup(fuse_req_t req
, fuse_ino_t parent
, const char *name
,
884 struct fuse_entry_param
*e
)
889 struct lo_data
*lo
= lo_data(req
);
890 struct lo_inode
*inode
= NULL
;
891 struct lo_inode
*dir
= lo_inode(req
, parent
);
894 * name_to_handle_at() and open_by_handle_at() can reach here with fuse
895 * mount point in guest, but we don't have its inode info in the
902 memset(e
, 0, sizeof(*e
));
903 e
->attr_timeout
= lo
->timeout
;
904 e
->entry_timeout
= lo
->timeout
;
906 /* Do not allow escaping root directory */
907 if (dir
== &lo
->root
&& strcmp(name
, "..") == 0) {
911 newfd
= openat(dir
->fd
, name
, O_PATH
| O_NOFOLLOW
);
916 res
= fstatat(newfd
, "", &e
->attr
, AT_EMPTY_PATH
| AT_SYMLINK_NOFOLLOW
);
921 inode
= lo_find(lo
, &e
->attr
);
926 inode
= calloc(1, sizeof(struct lo_inode
));
931 inode
->is_symlink
= S_ISLNK(e
->attr
.st_mode
);
934 * One for the caller and one for nlookup (released in
935 * unref_inode_lolocked())
937 g_atomic_int_set(&inode
->refcount
, 2);
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
);
967 lo_inode_put(lo
, &inode
);
968 lo_inode_put(lo
, &dir
);
972 static void lo_lookup(fuse_req_t req
, fuse_ino_t parent
, const char *name
)
974 struct fuse_entry_param e
;
977 fuse_log(FUSE_LOG_DEBUG
, "lo_lookup(parent=%" PRIu64
", name=%s)\n", parent
,
981 * Don't use is_safe_path_component(), allow "." and ".." for NFS export
984 if (strchr(name
, '/')) {
985 fuse_reply_err(req
, EINVAL
);
989 err
= lo_do_lookup(req
, parent
, name
, &e
);
991 fuse_reply_err(req
, err
);
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
1005 #define OURSYS_setresgid SYS_setresgid
1008 #ifdef SYS_setresuid32
1009 #define OURSYS_setresuid SYS_setresuid32
1011 #define OURSYS_setresuid SYS_setresuid
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
)
1023 old
->euid
= geteuid();
1024 old
->egid
= getegid();
1026 res
= syscall(OURSYS_setresgid
, -1, fuse_req_ctx(req
)->gid
, -1);
1031 res
= syscall(OURSYS_setresuid
, -1, fuse_req_ctx(req
)->uid
, -1);
1033 int errno_save
= errno
;
1035 syscall(OURSYS_setresgid
, -1, old
->egid
, -1);
1042 /* Regain Privileges */
1043 static void lo_restore_cred(struct lo_cred
*old
)
1047 res
= syscall(OURSYS_setresuid
, -1, old
->euid
, -1);
1049 fuse_log(FUSE_LOG_ERR
, "seteuid(%u): %m\n", old
->euid
);
1053 res
= syscall(OURSYS_setresgid
, -1, old
->egid
, -1);
1055 fuse_log(FUSE_LOG_ERR
, "setegid(%u): %m\n", old
->egid
);
1060 static void lo_mknod_symlink(fuse_req_t req
, fuse_ino_t parent
,
1061 const char *name
, mode_t mode
, dev_t rdev
,
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
);
1076 dir
= lo_inode(req
, parent
);
1078 fuse_reply_err(req
, EBADF
);
1084 saverr
= lo_change_cred(req
, &old
);
1089 res
= mknod_wrapper(dir
->fd
, name
, link
, mode
, rdev
);
1093 lo_restore_cred(&old
);
1099 saverr
= lo_do_lookup(req
, parent
, name
, &e
);
1104 fuse_log(FUSE_LOG_DEBUG
, " %lli/%s -> %lli\n", (unsigned long long)parent
,
1105 name
, (unsigned long long)e
.ino
);
1107 fuse_reply_entry(req
, &e
);
1108 lo_inode_put(lo
, &dir
);
1112 lo_inode_put(lo
, &dir
);
1113 fuse_reply_err(req
, saverr
);
1116 static void lo_mknod(fuse_req_t req
, fuse_ino_t parent
, const char *name
,
1117 mode_t mode
, dev_t rdev
)
1119 lo_mknod_symlink(req
, parent
, name
, mode
, rdev
, NULL
);
1122 static void lo_mkdir(fuse_req_t req
, fuse_ino_t parent
, const char *name
,
1125 lo_mknod_symlink(req
, parent
, name
, S_IFDIR
| mode
, 0, NULL
);
1128 static void lo_symlink(fuse_req_t req
, const char *link
, fuse_ino_t parent
,
1131 lo_mknod_symlink(req
, parent
, name
, S_IFLNK
, 0, link
);
1134 static int linkat_empty_nofollow(struct lo_data
*lo
, struct lo_inode
*inode
,
1135 int dfd
, const char *name
)
1138 struct lo_inode
*parent
;
1139 char path
[PATH_MAX
];
1141 if (inode
->is_symlink
) {
1142 res
= linkat(inode
->fd
, "", dfd
, name
, AT_EMPTY_PATH
);
1143 if (res
== -1 && (errno
== ENOENT
|| errno
== EINVAL
)) {
1144 /* Sorry, no race free way to hard-link a symlink. */
1154 sprintf(path
, "%i", inode
->fd
);
1156 return linkat(lo
->proc_self_fd
, path
, dfd
, name
, AT_SYMLINK_FOLLOW
);
1159 res
= lo_parent_and_name(lo
, inode
, path
, &parent
);
1161 res
= linkat(parent
->fd
, path
, dfd
, name
, 0);
1162 unref_inode_lolocked(lo
, parent
, 1);
1163 lo_inode_put(lo
, &parent
);
1169 static void lo_link(fuse_req_t req
, fuse_ino_t ino
, fuse_ino_t parent
,
1173 struct lo_data
*lo
= lo_data(req
);
1174 struct lo_inode
*parent_inode
;
1175 struct lo_inode
*inode
;
1176 struct fuse_entry_param e
;
1179 if (!is_safe_path_component(name
)) {
1180 fuse_reply_err(req
, EINVAL
);
1184 parent_inode
= lo_inode(req
, parent
);
1185 inode
= lo_inode(req
, ino
);
1186 if (!parent_inode
|| !inode
) {
1191 memset(&e
, 0, sizeof(struct fuse_entry_param
));
1192 e
.attr_timeout
= lo
->timeout
;
1193 e
.entry_timeout
= lo
->timeout
;
1195 res
= linkat_empty_nofollow(lo
, inode
, parent_inode
->fd
, name
);
1200 res
= fstatat(inode
->fd
, "", &e
.attr
, AT_EMPTY_PATH
| AT_SYMLINK_NOFOLLOW
);
1205 pthread_mutex_lock(&lo
->mutex
);
1207 pthread_mutex_unlock(&lo
->mutex
);
1208 e
.ino
= inode
->fuse_ino
;
1210 fuse_log(FUSE_LOG_DEBUG
, " %lli/%s -> %lli\n", (unsigned long long)parent
,
1211 name
, (unsigned long long)e
.ino
);
1213 fuse_reply_entry(req
, &e
);
1214 lo_inode_put(lo
, &parent_inode
);
1215 lo_inode_put(lo
, &inode
);
1220 lo_inode_put(lo
, &parent_inode
);
1221 lo_inode_put(lo
, &inode
);
1222 fuse_reply_err(req
, saverr
);
1225 /* Increments nlookup and caller must release refcount using lo_inode_put() */
1226 static struct lo_inode
*lookup_name(fuse_req_t req
, fuse_ino_t parent
,
1232 res
= fstatat(lo_fd(req
, parent
), name
, &attr
,
1233 AT_EMPTY_PATH
| AT_SYMLINK_NOFOLLOW
);
1238 return lo_find(lo_data(req
), &attr
);
1241 static void lo_rmdir(fuse_req_t req
, fuse_ino_t parent
, const char *name
)
1244 struct lo_inode
*inode
;
1245 struct lo_data
*lo
= lo_data(req
);
1247 if (!is_safe_path_component(name
)) {
1248 fuse_reply_err(req
, EINVAL
);
1252 inode
= lookup_name(req
, parent
, name
);
1254 fuse_reply_err(req
, EIO
);
1258 res
= unlinkat(lo_fd(req
, parent
), name
, AT_REMOVEDIR
);
1260 fuse_reply_err(req
, res
== -1 ? errno
: 0);
1261 unref_inode_lolocked(lo
, inode
, 1);
1262 lo_inode_put(lo
, &inode
);
1265 static void lo_rename(fuse_req_t req
, fuse_ino_t parent
, const char *name
,
1266 fuse_ino_t newparent
, const char *newname
,
1270 struct lo_inode
*parent_inode
;
1271 struct lo_inode
*newparent_inode
;
1272 struct lo_inode
*oldinode
= NULL
;
1273 struct lo_inode
*newinode
= NULL
;
1274 struct lo_data
*lo
= lo_data(req
);
1276 if (!is_safe_path_component(name
) || !is_safe_path_component(newname
)) {
1277 fuse_reply_err(req
, EINVAL
);
1281 parent_inode
= lo_inode(req
, parent
);
1282 newparent_inode
= lo_inode(req
, newparent
);
1283 if (!parent_inode
|| !newparent_inode
) {
1284 fuse_reply_err(req
, EBADF
);
1288 oldinode
= lookup_name(req
, parent
, name
);
1289 newinode
= lookup_name(req
, newparent
, newname
);
1292 fuse_reply_err(req
, EIO
);
1297 #ifndef SYS_renameat2
1298 fuse_reply_err(req
, EINVAL
);
1300 res
= syscall(SYS_renameat2
, parent_inode
->fd
, name
,
1301 newparent_inode
->fd
, newname
, flags
);
1302 if (res
== -1 && errno
== ENOSYS
) {
1303 fuse_reply_err(req
, EINVAL
);
1305 fuse_reply_err(req
, res
== -1 ? errno
: 0);
1311 res
= renameat(parent_inode
->fd
, name
, newparent_inode
->fd
, newname
);
1313 fuse_reply_err(req
, res
== -1 ? errno
: 0);
1315 unref_inode_lolocked(lo
, oldinode
, 1);
1316 unref_inode_lolocked(lo
, newinode
, 1);
1317 lo_inode_put(lo
, &oldinode
);
1318 lo_inode_put(lo
, &newinode
);
1319 lo_inode_put(lo
, &parent_inode
);
1320 lo_inode_put(lo
, &newparent_inode
);
1323 static void lo_unlink(fuse_req_t req
, fuse_ino_t parent
, const char *name
)
1326 struct lo_inode
*inode
;
1327 struct lo_data
*lo
= lo_data(req
);
1329 if (!is_safe_path_component(name
)) {
1330 fuse_reply_err(req
, EINVAL
);
1334 inode
= lookup_name(req
, parent
, name
);
1336 fuse_reply_err(req
, EIO
);
1340 res
= unlinkat(lo_fd(req
, parent
), name
, 0);
1342 fuse_reply_err(req
, res
== -1 ? errno
: 0);
1343 unref_inode_lolocked(lo
, inode
, 1);
1344 lo_inode_put(lo
, &inode
);
1347 /* To be called with lo->mutex held */
1348 static void unref_inode(struct lo_data
*lo
, struct lo_inode
*inode
, uint64_t n
)
1354 assert(inode
->nlookup
>= n
);
1355 inode
->nlookup
-= n
;
1356 if (!inode
->nlookup
) {
1357 lo_map_remove(&lo
->ino_map
, inode
->fuse_ino
);
1358 g_hash_table_remove(lo
->inodes
, &inode
->key
);
1359 if (g_hash_table_size(inode
->posix_locks
)) {
1360 fuse_log(FUSE_LOG_WARNING
, "Hash table is not empty\n");
1362 g_hash_table_destroy(inode
->posix_locks
);
1363 pthread_mutex_destroy(&inode
->plock_mutex
);
1365 /* Drop our refcount from lo_do_lookup() */
1366 lo_inode_put(lo
, &inode
);
1370 static void unref_inode_lolocked(struct lo_data
*lo
, struct lo_inode
*inode
,
1377 pthread_mutex_lock(&lo
->mutex
);
1378 unref_inode(lo
, inode
, n
);
1379 pthread_mutex_unlock(&lo
->mutex
);
1382 static void lo_forget_one(fuse_req_t req
, fuse_ino_t ino
, uint64_t nlookup
)
1384 struct lo_data
*lo
= lo_data(req
);
1385 struct lo_inode
*inode
;
1387 inode
= lo_inode(req
, ino
);
1392 fuse_log(FUSE_LOG_DEBUG
, " forget %lli %lli -%lli\n",
1393 (unsigned long long)ino
, (unsigned long long)inode
->nlookup
,
1394 (unsigned long long)nlookup
);
1396 unref_inode_lolocked(lo
, inode
, nlookup
);
1397 lo_inode_put(lo
, &inode
);
1400 static void lo_forget(fuse_req_t req
, fuse_ino_t ino
, uint64_t nlookup
)
1402 lo_forget_one(req
, ino
, nlookup
);
1403 fuse_reply_none(req
);
1406 static void lo_forget_multi(fuse_req_t req
, size_t count
,
1407 struct fuse_forget_data
*forgets
)
1411 for (i
= 0; i
< count
; i
++) {
1412 lo_forget_one(req
, forgets
[i
].ino
, forgets
[i
].nlookup
);
1414 fuse_reply_none(req
);
1417 static void lo_readlink(fuse_req_t req
, fuse_ino_t ino
)
1419 char buf
[PATH_MAX
+ 1];
1422 res
= readlinkat(lo_fd(req
, ino
), "", buf
, sizeof(buf
));
1424 return (void)fuse_reply_err(req
, errno
);
1427 if (res
== sizeof(buf
)) {
1428 return (void)fuse_reply_err(req
, ENAMETOOLONG
);
1433 fuse_reply_readlink(req
, buf
);
1439 struct dirent
*entry
;
1443 static void lo_dirp_put(struct lo_dirp
**dp
)
1445 struct lo_dirp
*d
= *dp
;
1452 if (g_atomic_int_dec_and_test(&d
->refcount
)) {
1458 /* Call lo_dirp_put() on the return value when no longer needed */
1459 static struct lo_dirp
*lo_dirp(fuse_req_t req
, struct fuse_file_info
*fi
)
1461 struct lo_data
*lo
= lo_data(req
);
1462 struct lo_map_elem
*elem
;
1464 pthread_mutex_lock(&lo
->mutex
);
1465 elem
= lo_map_get(&lo
->dirp_map
, fi
->fh
);
1467 g_atomic_int_inc(&elem
->dirp
->refcount
);
1469 pthread_mutex_unlock(&lo
->mutex
);
1477 static void lo_opendir(fuse_req_t req
, fuse_ino_t ino
,
1478 struct fuse_file_info
*fi
)
1481 struct lo_data
*lo
= lo_data(req
);
1486 d
= calloc(1, sizeof(struct lo_dirp
));
1491 fd
= openat(lo_fd(req
, ino
), ".", O_RDONLY
);
1496 d
->dp
= fdopendir(fd
);
1497 if (d
->dp
== NULL
) {
1504 g_atomic_int_set(&d
->refcount
, 1); /* paired with lo_releasedir() */
1505 pthread_mutex_lock(&lo
->mutex
);
1506 fh
= lo_add_dirp_mapping(req
, d
);
1507 pthread_mutex_unlock(&lo
->mutex
);
1513 if (lo
->cache
== CACHE_ALWAYS
) {
1514 fi
->cache_readdir
= 1;
1516 fuse_reply_open(req
, fi
);
1531 fuse_reply_err(req
, error
);
1534 static void lo_do_readdir(fuse_req_t req
, fuse_ino_t ino
, size_t size
,
1535 off_t offset
, struct fuse_file_info
*fi
, int plus
)
1537 struct lo_data
*lo
= lo_data(req
);
1538 struct lo_dirp
*d
= NULL
;
1539 struct lo_inode
*dinode
;
1545 dinode
= lo_inode(req
, ino
);
1550 d
= lo_dirp(req
, fi
);
1556 buf
= calloc(1, size
);
1562 if (offset
!= d
->offset
) {
1563 seekdir(d
->dp
, offset
);
1574 d
->entry
= readdir(d
->dp
);
1576 if (errno
) { /* Error */
1579 } else { /* End of stream */
1584 nextoff
= d
->entry
->d_off
;
1585 name
= d
->entry
->d_name
;
1587 fuse_ino_t entry_ino
= 0;
1588 struct fuse_entry_param e
= (struct fuse_entry_param
){
1589 .attr
.st_ino
= d
->entry
->d_ino
,
1590 .attr
.st_mode
= d
->entry
->d_type
<< 12,
1593 /* Hide root's parent directory */
1594 if (dinode
== &lo
->root
&& strcmp(name
, "..") == 0) {
1595 e
.attr
.st_ino
= lo
->root
.key
.ino
;
1596 e
.attr
.st_mode
= DT_DIR
<< 12;
1600 if (!is_dot_or_dotdot(name
)) {
1601 err
= lo_do_lookup(req
, ino
, name
, &e
);
1608 entsize
= fuse_add_direntry_plus(req
, p
, rem
, name
, &e
, nextoff
);
1610 entsize
= fuse_add_direntry(req
, p
, rem
, name
, &e
.attr
, nextoff
);
1612 if (entsize
> rem
) {
1613 if (entry_ino
!= 0) {
1614 lo_forget_one(req
, entry_ino
, 1);
1623 d
->offset
= nextoff
;
1629 lo_inode_put(lo
, &dinode
);
1632 * If there's an error, we can only signal it if we haven't stored
1633 * any entries yet - otherwise we'd end up with wrong lookup
1634 * counts for the entries that are already in the buffer. So we
1635 * return what we've collected until that point.
1637 if (err
&& rem
== size
) {
1638 fuse_reply_err(req
, err
);
1640 fuse_reply_buf(req
, buf
, size
- rem
);
1645 static void lo_readdir(fuse_req_t req
, fuse_ino_t ino
, size_t size
,
1646 off_t offset
, struct fuse_file_info
*fi
)
1648 lo_do_readdir(req
, ino
, size
, offset
, fi
, 0);
1651 static void lo_readdirplus(fuse_req_t req
, fuse_ino_t ino
, size_t size
,
1652 off_t offset
, struct fuse_file_info
*fi
)
1654 lo_do_readdir(req
, ino
, size
, offset
, fi
, 1);
1657 static void lo_releasedir(fuse_req_t req
, fuse_ino_t ino
,
1658 struct fuse_file_info
*fi
)
1660 struct lo_data
*lo
= lo_data(req
);
1661 struct lo_map_elem
*elem
;
1666 pthread_mutex_lock(&lo
->mutex
);
1667 elem
= lo_map_get(&lo
->dirp_map
, fi
->fh
);
1669 pthread_mutex_unlock(&lo
->mutex
);
1670 fuse_reply_err(req
, EBADF
);
1675 lo_map_remove(&lo
->dirp_map
, fi
->fh
);
1676 pthread_mutex_unlock(&lo
->mutex
);
1678 lo_dirp_put(&d
); /* paired with lo_opendir() */
1680 fuse_reply_err(req
, 0);
1683 static void update_open_flags(int writeback
, struct fuse_file_info
*fi
)
1686 * With writeback cache, kernel may send read requests even
1687 * when userspace opened write-only
1689 if (writeback
&& (fi
->flags
& O_ACCMODE
) == O_WRONLY
) {
1690 fi
->flags
&= ~O_ACCMODE
;
1691 fi
->flags
|= O_RDWR
;
1695 * With writeback cache, O_APPEND is handled by the kernel.
1696 * This breaks atomicity (since the file may change in the
1697 * underlying filesystem, so that the kernel's idea of the
1698 * end of the file isn't accurate anymore). In this example,
1699 * we just accept that. A more rigorous filesystem may want
1700 * to return an error here
1702 if (writeback
&& (fi
->flags
& O_APPEND
)) {
1703 fi
->flags
&= ~O_APPEND
;
1707 * O_DIRECT in guest should not necessarily mean bypassing page
1708 * cache on host as well. If somebody needs that behavior, it
1709 * probably should be a configuration knob in daemon.
1711 fi
->flags
&= ~O_DIRECT
;
1714 static void lo_create(fuse_req_t req
, fuse_ino_t parent
, const char *name
,
1715 mode_t mode
, struct fuse_file_info
*fi
)
1718 struct lo_data
*lo
= lo_data(req
);
1719 struct lo_inode
*parent_inode
;
1720 struct fuse_entry_param e
;
1722 struct lo_cred old
= {};
1724 fuse_log(FUSE_LOG_DEBUG
, "lo_create(parent=%" PRIu64
", name=%s)\n", parent
,
1727 if (!is_safe_path_component(name
)) {
1728 fuse_reply_err(req
, EINVAL
);
1732 parent_inode
= lo_inode(req
, parent
);
1733 if (!parent_inode
) {
1734 fuse_reply_err(req
, EBADF
);
1738 err
= lo_change_cred(req
, &old
);
1743 update_open_flags(lo
->writeback
, fi
);
1745 fd
= openat(parent_inode
->fd
, name
, (fi
->flags
| O_CREAT
) & ~O_NOFOLLOW
,
1747 err
= fd
== -1 ? errno
: 0;
1748 lo_restore_cred(&old
);
1753 pthread_mutex_lock(&lo
->mutex
);
1754 fh
= lo_add_fd_mapping(req
, fd
);
1755 pthread_mutex_unlock(&lo
->mutex
);
1763 err
= lo_do_lookup(req
, parent
, name
, &e
);
1765 if (lo
->cache
== CACHE_NONE
) {
1767 } else if (lo
->cache
== CACHE_ALWAYS
) {
1772 lo_inode_put(lo
, &parent_inode
);
1775 fuse_reply_err(req
, err
);
1777 fuse_reply_create(req
, &e
, fi
);
1781 /* Should be called with inode->plock_mutex held */
1782 static struct lo_inode_plock
*lookup_create_plock_ctx(struct lo_data
*lo
,
1783 struct lo_inode
*inode
,
1784 uint64_t lock_owner
,
1785 pid_t pid
, int *err
)
1787 struct lo_inode_plock
*plock
;
1792 g_hash_table_lookup(inode
->posix_locks
, GUINT_TO_POINTER(lock_owner
));
1798 plock
= malloc(sizeof(struct lo_inode_plock
));
1804 /* Open another instance of file which can be used for ofd locks. */
1805 sprintf(procname
, "%i", inode
->fd
);
1807 /* TODO: What if file is not writable? */
1808 fd
= openat(lo
->proc_self_fd
, procname
, O_RDWR
);
1815 plock
->lock_owner
= lock_owner
;
1817 g_hash_table_insert(inode
->posix_locks
, GUINT_TO_POINTER(plock
->lock_owner
),
1822 static void lo_getlk(fuse_req_t req
, fuse_ino_t ino
, struct fuse_file_info
*fi
,
1825 struct lo_data
*lo
= lo_data(req
);
1826 struct lo_inode
*inode
;
1827 struct lo_inode_plock
*plock
;
1828 int ret
, saverr
= 0;
1830 fuse_log(FUSE_LOG_DEBUG
,
1831 "lo_getlk(ino=%" PRIu64
", flags=%d)"
1832 " owner=0x%lx, l_type=%d l_start=0x%lx"
1834 ino
, fi
->flags
, fi
->lock_owner
, lock
->l_type
, lock
->l_start
,
1837 inode
= lo_inode(req
, ino
);
1839 fuse_reply_err(req
, EBADF
);
1843 pthread_mutex_lock(&inode
->plock_mutex
);
1845 lookup_create_plock_ctx(lo
, inode
, fi
->lock_owner
, lock
->l_pid
, &ret
);
1851 ret
= fcntl(plock
->fd
, F_OFD_GETLK
, lock
);
1857 pthread_mutex_unlock(&inode
->plock_mutex
);
1858 lo_inode_put(lo
, &inode
);
1861 fuse_reply_err(req
, saverr
);
1863 fuse_reply_lock(req
, lock
);
1867 static void lo_setlk(fuse_req_t req
, fuse_ino_t ino
, struct fuse_file_info
*fi
,
1868 struct flock
*lock
, int sleep
)
1870 struct lo_data
*lo
= lo_data(req
);
1871 struct lo_inode
*inode
;
1872 struct lo_inode_plock
*plock
;
1873 int ret
, saverr
= 0;
1875 fuse_log(FUSE_LOG_DEBUG
,
1876 "lo_setlk(ino=%" PRIu64
", flags=%d)"
1877 " cmd=%d pid=%d owner=0x%lx sleep=%d l_whence=%d"
1878 " l_start=0x%lx l_len=0x%lx\n",
1879 ino
, fi
->flags
, lock
->l_type
, lock
->l_pid
, fi
->lock_owner
, sleep
,
1880 lock
->l_whence
, lock
->l_start
, lock
->l_len
);
1883 fuse_reply_err(req
, EOPNOTSUPP
);
1887 inode
= lo_inode(req
, ino
);
1889 fuse_reply_err(req
, EBADF
);
1893 pthread_mutex_lock(&inode
->plock_mutex
);
1895 lookup_create_plock_ctx(lo
, inode
, fi
->lock_owner
, lock
->l_pid
, &ret
);
1902 /* TODO: Is it alright to modify flock? */
1904 ret
= fcntl(plock
->fd
, F_OFD_SETLK
, lock
);
1910 pthread_mutex_unlock(&inode
->plock_mutex
);
1911 lo_inode_put(lo
, &inode
);
1913 fuse_reply_err(req
, saverr
);
1916 static void lo_fsyncdir(fuse_req_t req
, fuse_ino_t ino
, int datasync
,
1917 struct fuse_file_info
*fi
)
1925 d
= lo_dirp(req
, fi
);
1927 fuse_reply_err(req
, EBADF
);
1933 res
= fdatasync(fd
);
1940 fuse_reply_err(req
, res
== -1 ? errno
: 0);
1943 static void lo_open(fuse_req_t req
, fuse_ino_t ino
, struct fuse_file_info
*fi
)
1948 struct lo_data
*lo
= lo_data(req
);
1950 fuse_log(FUSE_LOG_DEBUG
, "lo_open(ino=%" PRIu64
", flags=%d)\n", ino
,
1953 update_open_flags(lo
->writeback
, fi
);
1955 sprintf(buf
, "%i", lo_fd(req
, ino
));
1956 fd
= openat(lo
->proc_self_fd
, buf
, fi
->flags
& ~O_NOFOLLOW
);
1958 return (void)fuse_reply_err(req
, errno
);
1961 pthread_mutex_lock(&lo
->mutex
);
1962 fh
= lo_add_fd_mapping(req
, fd
);
1963 pthread_mutex_unlock(&lo
->mutex
);
1966 fuse_reply_err(req
, ENOMEM
);
1971 if (lo
->cache
== CACHE_NONE
) {
1973 } else if (lo
->cache
== CACHE_ALWAYS
) {
1976 fuse_reply_open(req
, fi
);
1979 static void lo_release(fuse_req_t req
, fuse_ino_t ino
,
1980 struct fuse_file_info
*fi
)
1982 struct lo_data
*lo
= lo_data(req
);
1983 struct lo_map_elem
*elem
;
1988 pthread_mutex_lock(&lo
->mutex
);
1989 elem
= lo_map_get(&lo
->fd_map
, fi
->fh
);
1993 lo_map_remove(&lo
->fd_map
, fi
->fh
);
1995 pthread_mutex_unlock(&lo
->mutex
);
1998 fuse_reply_err(req
, 0);
2001 static void lo_flush(fuse_req_t req
, fuse_ino_t ino
, struct fuse_file_info
*fi
)
2005 struct lo_inode
*inode
;
2007 inode
= lo_inode(req
, ino
);
2009 fuse_reply_err(req
, EBADF
);
2013 /* An fd is going away. Cleanup associated posix locks */
2014 pthread_mutex_lock(&inode
->plock_mutex
);
2015 g_hash_table_remove(inode
->posix_locks
, GUINT_TO_POINTER(fi
->lock_owner
));
2016 pthread_mutex_unlock(&inode
->plock_mutex
);
2018 res
= close(dup(lo_fi_fd(req
, fi
)));
2019 lo_inode_put(lo_data(req
), &inode
);
2020 fuse_reply_err(req
, res
== -1 ? errno
: 0);
2023 static void lo_fsync(fuse_req_t req
, fuse_ino_t ino
, int datasync
,
2024 struct fuse_file_info
*fi
)
2030 fuse_log(FUSE_LOG_DEBUG
, "lo_fsync(ino=%" PRIu64
", fi=0x%p)\n", ino
,
2034 struct lo_data
*lo
= lo_data(req
);
2036 res
= asprintf(&buf
, "%i", lo_fd(req
, ino
));
2038 return (void)fuse_reply_err(req
, errno
);
2041 fd
= openat(lo
->proc_self_fd
, buf
, O_RDWR
);
2044 return (void)fuse_reply_err(req
, errno
);
2047 fd
= lo_fi_fd(req
, fi
);
2051 res
= fdatasync(fd
);
2058 fuse_reply_err(req
, res
== -1 ? errno
: 0);
2061 static void lo_read(fuse_req_t req
, fuse_ino_t ino
, size_t size
, off_t offset
,
2062 struct fuse_file_info
*fi
)
2064 struct fuse_bufvec buf
= FUSE_BUFVEC_INIT(size
);
2066 fuse_log(FUSE_LOG_DEBUG
,
2067 "lo_read(ino=%" PRIu64
", size=%zd, "
2069 ino
, size
, (unsigned long)offset
);
2071 buf
.buf
[0].flags
= FUSE_BUF_IS_FD
| FUSE_BUF_FD_SEEK
;
2072 buf
.buf
[0].fd
= lo_fi_fd(req
, fi
);
2073 buf
.buf
[0].pos
= offset
;
2075 fuse_reply_data(req
, &buf
);
2078 static void lo_write_buf(fuse_req_t req
, fuse_ino_t ino
,
2079 struct fuse_bufvec
*in_buf
, off_t off
,
2080 struct fuse_file_info
*fi
)
2084 struct fuse_bufvec out_buf
= FUSE_BUFVEC_INIT(fuse_buf_size(in_buf
));
2085 bool cap_fsetid_dropped
= false;
2087 out_buf
.buf
[0].flags
= FUSE_BUF_IS_FD
| FUSE_BUF_FD_SEEK
;
2088 out_buf
.buf
[0].fd
= lo_fi_fd(req
, fi
);
2089 out_buf
.buf
[0].pos
= off
;
2091 fuse_log(FUSE_LOG_DEBUG
,
2092 "lo_write_buf(ino=%" PRIu64
", size=%zd, off=%lu)\n", ino
,
2093 out_buf
.buf
[0].size
, (unsigned long)off
);
2096 * If kill_priv is set, drop CAP_FSETID which should lead to kernel
2097 * clearing setuid/setgid on file.
2099 if (fi
->kill_priv
) {
2100 res
= drop_effective_cap("FSETID", &cap_fsetid_dropped
);
2102 fuse_reply_err(req
, res
);
2107 res
= fuse_buf_copy(&out_buf
, in_buf
);
2109 fuse_reply_err(req
, -res
);
2111 fuse_reply_write(req
, (size_t)res
);
2114 if (cap_fsetid_dropped
) {
2115 res
= gain_effective_cap("FSETID");
2117 fuse_log(FUSE_LOG_ERR
, "Failed to gain CAP_FSETID\n");
2122 static void lo_statfs(fuse_req_t req
, fuse_ino_t ino
)
2125 struct statvfs stbuf
;
2127 res
= fstatvfs(lo_fd(req
, ino
), &stbuf
);
2129 fuse_reply_err(req
, errno
);
2131 fuse_reply_statfs(req
, &stbuf
);
2135 static void lo_fallocate(fuse_req_t req
, fuse_ino_t ino
, int mode
, off_t offset
,
2136 off_t length
, struct fuse_file_info
*fi
)
2138 int err
= EOPNOTSUPP
;
2141 #ifdef CONFIG_FALLOCATE
2142 err
= fallocate(lo_fi_fd(req
, fi
), mode
, offset
, length
);
2147 #elif defined(CONFIG_POSIX_FALLOCATE)
2149 fuse_reply_err(req
, EOPNOTSUPP
);
2153 err
= posix_fallocate(lo_fi_fd(req
, fi
), offset
, length
);
2156 fuse_reply_err(req
, err
);
2159 static void lo_flock(fuse_req_t req
, fuse_ino_t ino
, struct fuse_file_info
*fi
,
2165 res
= flock(lo_fi_fd(req
, fi
), op
);
2167 fuse_reply_err(req
, res
== -1 ? errno
: 0);
2170 static void lo_getxattr(fuse_req_t req
, fuse_ino_t ino
, const char *name
,
2173 struct lo_data
*lo
= lo_data(req
);
2176 struct lo_inode
*inode
;
2181 inode
= lo_inode(req
, ino
);
2183 fuse_reply_err(req
, EBADF
);
2188 if (!lo_data(req
)->xattr
) {
2192 fuse_log(FUSE_LOG_DEBUG
, "lo_getxattr(ino=%" PRIu64
", name=%s size=%zd)\n",
2195 if (inode
->is_symlink
) {
2196 /* Sorry, no race free way to getxattr on symlink. */
2201 sprintf(procname
, "%i", inode
->fd
);
2202 fd
= openat(lo
->proc_self_fd
, procname
, O_RDONLY
);
2208 value
= malloc(size
);
2213 ret
= fgetxattr(fd
, name
, value
, size
);
2222 fuse_reply_buf(req
, value
, ret
);
2224 ret
= fgetxattr(fd
, name
, NULL
, 0);
2229 fuse_reply_xattr(req
, ret
);
2238 lo_inode_put(lo
, &inode
);
2244 lo_inode_put(lo
, &inode
);
2245 fuse_reply_err(req
, saverr
);
2249 static void lo_listxattr(fuse_req_t req
, fuse_ino_t ino
, size_t size
)
2251 struct lo_data
*lo
= lo_data(req
);
2254 struct lo_inode
*inode
;
2259 inode
= lo_inode(req
, ino
);
2261 fuse_reply_err(req
, EBADF
);
2266 if (!lo_data(req
)->xattr
) {
2270 fuse_log(FUSE_LOG_DEBUG
, "lo_listxattr(ino=%" PRIu64
", size=%zd)\n", ino
,
2273 if (inode
->is_symlink
) {
2274 /* Sorry, no race free way to listxattr on symlink. */
2279 sprintf(procname
, "%i", inode
->fd
);
2280 fd
= openat(lo
->proc_self_fd
, procname
, O_RDONLY
);
2286 value
= malloc(size
);
2291 ret
= flistxattr(fd
, value
, size
);
2300 fuse_reply_buf(req
, value
, ret
);
2302 ret
= flistxattr(fd
, NULL
, 0);
2307 fuse_reply_xattr(req
, ret
);
2316 lo_inode_put(lo
, &inode
);
2322 lo_inode_put(lo
, &inode
);
2323 fuse_reply_err(req
, saverr
);
2327 static void lo_setxattr(fuse_req_t req
, fuse_ino_t ino
, const char *name
,
2328 const char *value
, size_t size
, int flags
)
2331 struct lo_data
*lo
= lo_data(req
);
2332 struct lo_inode
*inode
;
2337 inode
= lo_inode(req
, ino
);
2339 fuse_reply_err(req
, EBADF
);
2344 if (!lo_data(req
)->xattr
) {
2348 fuse_log(FUSE_LOG_DEBUG
, "lo_setxattr(ino=%" PRIu64
2349 ", name=%s value=%s size=%zd)\n", ino
, name
, value
, size
);
2351 if (inode
->is_symlink
) {
2352 /* Sorry, no race free way to setxattr on symlink. */
2357 sprintf(procname
, "%i", inode
->fd
);
2358 fd
= openat(lo
->proc_self_fd
, procname
, O_RDWR
);
2364 ret
= fsetxattr(fd
, name
, value
, size
, flags
);
2365 saverr
= ret
== -1 ? errno
: 0;
2372 lo_inode_put(lo
, &inode
);
2373 fuse_reply_err(req
, saverr
);
2376 static void lo_removexattr(fuse_req_t req
, fuse_ino_t ino
, const char *name
)
2379 struct lo_data
*lo
= lo_data(req
);
2380 struct lo_inode
*inode
;
2385 inode
= lo_inode(req
, ino
);
2387 fuse_reply_err(req
, EBADF
);
2392 if (!lo_data(req
)->xattr
) {
2396 fuse_log(FUSE_LOG_DEBUG
, "lo_removexattr(ino=%" PRIu64
", name=%s)\n", ino
,
2399 if (inode
->is_symlink
) {
2400 /* Sorry, no race free way to setxattr on symlink. */
2405 sprintf(procname
, "%i", inode
->fd
);
2406 fd
= openat(lo
->proc_self_fd
, procname
, O_RDWR
);
2412 ret
= fremovexattr(fd
, name
);
2413 saverr
= ret
== -1 ? errno
: 0;
2420 lo_inode_put(lo
, &inode
);
2421 fuse_reply_err(req
, saverr
);
2424 #ifdef HAVE_COPY_FILE_RANGE
2425 static void lo_copy_file_range(fuse_req_t req
, fuse_ino_t ino_in
, off_t off_in
,
2426 struct fuse_file_info
*fi_in
, fuse_ino_t ino_out
,
2427 off_t off_out
, struct fuse_file_info
*fi_out
,
2428 size_t len
, int flags
)
2433 in_fd
= lo_fi_fd(req
, fi_in
);
2434 out_fd
= lo_fi_fd(req
, fi_out
);
2436 fuse_log(FUSE_LOG_DEBUG
,
2437 "lo_copy_file_range(ino=%" PRIu64
"/fd=%d, "
2438 "off=%lu, ino=%" PRIu64
"/fd=%d, "
2439 "off=%lu, size=%zd, flags=0x%x)\n",
2440 ino_in
, in_fd
, off_in
, ino_out
, out_fd
, off_out
, len
, flags
);
2442 res
= copy_file_range(in_fd
, &off_in
, out_fd
, &off_out
, len
, flags
);
2444 fuse_reply_err(req
, -errno
);
2446 fuse_reply_write(req
, res
);
2451 static void lo_lseek(fuse_req_t req
, fuse_ino_t ino
, off_t off
, int whence
,
2452 struct fuse_file_info
*fi
)
2457 res
= lseek(lo_fi_fd(req
, fi
), off
, whence
);
2459 fuse_reply_lseek(req
, res
);
2461 fuse_reply_err(req
, errno
);
2465 static void lo_destroy(void *userdata
)
2467 struct lo_data
*lo
= (struct lo_data
*)userdata
;
2469 pthread_mutex_lock(&lo
->mutex
);
2471 GHashTableIter iter
;
2472 gpointer key
, value
;
2474 g_hash_table_iter_init(&iter
, lo
->inodes
);
2475 if (!g_hash_table_iter_next(&iter
, &key
, &value
)) {
2479 struct lo_inode
*inode
= value
;
2480 unref_inode(lo
, inode
, inode
->nlookup
);
2482 pthread_mutex_unlock(&lo
->mutex
);
2485 static struct fuse_lowlevel_ops lo_oper
= {
2487 .lookup
= lo_lookup
,
2490 .symlink
= lo_symlink
,
2492 .unlink
= lo_unlink
,
2494 .rename
= lo_rename
,
2495 .forget
= lo_forget
,
2496 .forget_multi
= lo_forget_multi
,
2497 .getattr
= lo_getattr
,
2498 .setattr
= lo_setattr
,
2499 .readlink
= lo_readlink
,
2500 .opendir
= lo_opendir
,
2501 .readdir
= lo_readdir
,
2502 .readdirplus
= lo_readdirplus
,
2503 .releasedir
= lo_releasedir
,
2504 .fsyncdir
= lo_fsyncdir
,
2505 .create
= lo_create
,
2509 .release
= lo_release
,
2513 .write_buf
= lo_write_buf
,
2514 .statfs
= lo_statfs
,
2515 .fallocate
= lo_fallocate
,
2517 .getxattr
= lo_getxattr
,
2518 .listxattr
= lo_listxattr
,
2519 .setxattr
= lo_setxattr
,
2520 .removexattr
= lo_removexattr
,
2521 #ifdef HAVE_COPY_FILE_RANGE
2522 .copy_file_range
= lo_copy_file_range
,
2525 .destroy
= lo_destroy
,
2528 /* Print vhost-user.json backend program capabilities */
2529 static void print_capabilities(void)
2532 printf(" \"type\": \"fs\"\n");
2537 * Move to a new mount, net, and pid namespaces to isolate this process.
2539 static void setup_namespaces(struct lo_data
*lo
, struct fuse_session
*se
)
2544 * Create a new pid namespace for *child* processes. We'll have to
2545 * fork in order to enter the new pid namespace. A new mount namespace
2546 * is also needed so that we can remount /proc for the new pid
2549 * Our UNIX domain sockets have been created. Now we can move to
2550 * an empty network namespace to prevent TCP/IP and other network
2551 * activity in case this process is compromised.
2553 if (unshare(CLONE_NEWPID
| CLONE_NEWNS
| CLONE_NEWNET
) != 0) {
2554 fuse_log(FUSE_LOG_ERR
, "unshare(CLONE_NEWPID | CLONE_NEWNS): %m\n");
2560 fuse_log(FUSE_LOG_ERR
, "fork() failed: %m\n");
2567 /* The parent waits for the child */
2569 waited
= waitpid(child
, &wstatus
, 0);
2570 } while (waited
< 0 && errno
== EINTR
&& !se
->exited
);
2572 /* We were terminated by a signal, see fuse_signals.c */
2577 if (WIFEXITED(wstatus
)) {
2578 exit(WEXITSTATUS(wstatus
));
2584 /* Send us SIGTERM when the parent thread terminates, see prctl(2) */
2585 prctl(PR_SET_PDEATHSIG
, SIGTERM
);
2588 * If the mounts have shared propagation then we want to opt out so our
2589 * mount changes don't affect the parent mount namespace.
2591 if (mount(NULL
, "/", NULL
, MS_REC
| MS_SLAVE
, NULL
) < 0) {
2592 fuse_log(FUSE_LOG_ERR
, "mount(/, MS_REC|MS_SLAVE): %m\n");
2596 /* The child must remount /proc to use the new pid namespace */
2597 if (mount("proc", "/proc", "proc",
2598 MS_NODEV
| MS_NOEXEC
| MS_NOSUID
| MS_RELATIME
, NULL
) < 0) {
2599 fuse_log(FUSE_LOG_ERR
, "mount(/proc): %m\n");
2603 /* Now we can get our /proc/self/fd directory file descriptor */
2604 lo
->proc_self_fd
= open("/proc/self/fd", O_PATH
);
2605 if (lo
->proc_self_fd
== -1) {
2606 fuse_log(FUSE_LOG_ERR
, "open(/proc/self/fd, O_PATH): %m\n");
2612 * Capture the capability state, we'll need to restore this for individual
2613 * threads later; see load_capng.
2615 static void setup_capng(void)
2617 /* Note this accesses /proc so has to happen before the sandbox */
2618 if (capng_get_caps_process()) {
2619 fuse_log(FUSE_LOG_ERR
, "capng_get_caps_process\n");
2622 pthread_mutex_init(&cap
.mutex
, NULL
);
2623 pthread_mutex_lock(&cap
.mutex
);
2624 cap
.saved
= capng_save_state();
2626 fuse_log(FUSE_LOG_ERR
, "capng_save_state\n");
2629 pthread_mutex_unlock(&cap
.mutex
);
2632 static void cleanup_capng(void)
2636 pthread_mutex_destroy(&cap
.mutex
);
2641 * Make the source directory our root so symlinks cannot escape and no other
2642 * files are accessible. Assumes unshare(CLONE_NEWNS) was already called.
2644 static void setup_mounts(const char *source
)
2649 if (mount(source
, source
, NULL
, MS_BIND
, NULL
) < 0) {
2650 fuse_log(FUSE_LOG_ERR
, "mount(%s, %s, MS_BIND): %m\n", source
, source
);
2654 /* This magic is based on lxc's lxc_pivot_root() */
2655 oldroot
= open("/", O_DIRECTORY
| O_RDONLY
| O_CLOEXEC
);
2657 fuse_log(FUSE_LOG_ERR
, "open(/): %m\n");
2661 newroot
= open(source
, O_DIRECTORY
| O_RDONLY
| O_CLOEXEC
);
2663 fuse_log(FUSE_LOG_ERR
, "open(%s): %m\n", source
);
2667 if (fchdir(newroot
) < 0) {
2668 fuse_log(FUSE_LOG_ERR
, "fchdir(newroot): %m\n");
2672 if (syscall(__NR_pivot_root
, ".", ".") < 0) {
2673 fuse_log(FUSE_LOG_ERR
, "pivot_root(., .): %m\n");
2677 if (fchdir(oldroot
) < 0) {
2678 fuse_log(FUSE_LOG_ERR
, "fchdir(oldroot): %m\n");
2682 if (mount("", ".", "", MS_SLAVE
| MS_REC
, NULL
) < 0) {
2683 fuse_log(FUSE_LOG_ERR
, "mount(., MS_SLAVE | MS_REC): %m\n");
2687 if (umount2(".", MNT_DETACH
) < 0) {
2688 fuse_log(FUSE_LOG_ERR
, "umount2(., MNT_DETACH): %m\n");
2692 if (fchdir(newroot
) < 0) {
2693 fuse_log(FUSE_LOG_ERR
, "fchdir(newroot): %m\n");
2702 * Lock down this process to prevent access to other processes or files outside
2703 * source directory. This reduces the impact of arbitrary code execution bugs.
2705 static void setup_sandbox(struct lo_data
*lo
, struct fuse_session
*se
,
2708 setup_namespaces(lo
, se
);
2709 setup_mounts(lo
->source
);
2710 setup_seccomp(enable_syslog
);
2713 /* Raise the maximum number of open file descriptors */
2714 static void setup_nofile_rlimit(void)
2716 const rlim_t max_fds
= 1000000;
2719 if (getrlimit(RLIMIT_NOFILE
, &rlim
) < 0) {
2720 fuse_log(FUSE_LOG_ERR
, "getrlimit(RLIMIT_NOFILE): %m\n");
2724 if (rlim
.rlim_cur
>= max_fds
) {
2725 return; /* nothing to do */
2728 rlim
.rlim_cur
= max_fds
;
2729 rlim
.rlim_max
= max_fds
;
2731 if (setrlimit(RLIMIT_NOFILE
, &rlim
) < 0) {
2732 /* Ignore SELinux denials */
2733 if (errno
== EPERM
) {
2737 fuse_log(FUSE_LOG_ERR
, "setrlimit(RLIMIT_NOFILE): %m\n");
2742 static void log_func(enum fuse_log_level level
, const char *fmt
, va_list ap
)
2744 g_autofree
char *localfmt
= NULL
;
2746 if (current_log_level
< level
) {
2750 if (current_log_level
== FUSE_LOG_DEBUG
) {
2752 localfmt
= g_strdup_printf("[%" PRId64
"] [ID: %08ld] %s",
2753 get_clock(), syscall(__NR_gettid
), fmt
);
2755 localfmt
= g_strdup_printf("[ID: %08ld] %s", syscall(__NR_gettid
),
2762 int priority
= LOG_ERR
;
2764 case FUSE_LOG_EMERG
:
2765 priority
= LOG_EMERG
;
2767 case FUSE_LOG_ALERT
:
2768 priority
= LOG_ALERT
;
2771 priority
= LOG_CRIT
;
2776 case FUSE_LOG_WARNING
:
2777 priority
= LOG_WARNING
;
2779 case FUSE_LOG_NOTICE
:
2780 priority
= LOG_NOTICE
;
2783 priority
= LOG_INFO
;
2785 case FUSE_LOG_DEBUG
:
2786 priority
= LOG_DEBUG
;
2789 vsyslog(priority
, fmt
, ap
);
2791 vfprintf(stderr
, fmt
, ap
);
2795 static void setup_root(struct lo_data
*lo
, struct lo_inode
*root
)
2800 fd
= open("/", O_PATH
);
2802 fuse_log(FUSE_LOG_ERR
, "open(%s, O_PATH): %m\n", lo
->source
);
2806 res
= fstatat(fd
, "", &stat
, AT_EMPTY_PATH
| AT_SYMLINK_NOFOLLOW
);
2808 fuse_log(FUSE_LOG_ERR
, "fstatat(%s): %m\n", lo
->source
);
2812 root
->is_symlink
= false;
2814 root
->key
.ino
= stat
.st_ino
;
2815 root
->key
.dev
= stat
.st_dev
;
2817 g_atomic_int_set(&root
->refcount
, 2);
2820 static guint
lo_key_hash(gconstpointer key
)
2822 const struct lo_key
*lkey
= key
;
2824 return (guint
)lkey
->ino
+ (guint
)lkey
->dev
;
2827 static gboolean
lo_key_equal(gconstpointer a
, gconstpointer b
)
2829 const struct lo_key
*la
= a
;
2830 const struct lo_key
*lb
= b
;
2832 return la
->ino
== lb
->ino
&& la
->dev
== lb
->dev
;
2835 static void fuse_lo_data_cleanup(struct lo_data
*lo
)
2838 g_hash_table_destroy(lo
->inodes
);
2840 lo_map_destroy(&lo
->fd_map
);
2841 lo_map_destroy(&lo
->dirp_map
);
2842 lo_map_destroy(&lo
->ino_map
);
2844 if (lo
->proc_self_fd
>= 0) {
2845 close(lo
->proc_self_fd
);
2848 if (lo
->root
.fd
>= 0) {
2855 int main(int argc
, char *argv
[])
2857 struct fuse_args args
= FUSE_ARGS_INIT(argc
, argv
);
2858 struct fuse_session
*se
;
2859 struct fuse_cmdline_opts opts
;
2860 struct lo_data lo
= {
2866 struct lo_map_elem
*root_elem
;
2869 /* Don't mask creation mode, kernel already did that */
2872 pthread_mutex_init(&lo
.mutex
, NULL
);
2873 lo
.inodes
= g_hash_table_new(lo_key_hash
, lo_key_equal
);
2875 lo
.root
.fuse_ino
= FUSE_ROOT_ID
;
2876 lo
.cache
= CACHE_AUTO
;
2879 * Set up the ino map like this:
2880 * [0] Reserved (will not be used)
2883 lo_map_init(&lo
.ino_map
);
2884 lo_map_reserve(&lo
.ino_map
, 0)->in_use
= false;
2885 root_elem
= lo_map_reserve(&lo
.ino_map
, lo
.root
.fuse_ino
);
2886 root_elem
->inode
= &lo
.root
;
2888 lo_map_init(&lo
.dirp_map
);
2889 lo_map_init(&lo
.fd_map
);
2891 if (fuse_parse_cmdline(&args
, &opts
) != 0) {
2894 fuse_set_log_func(log_func
);
2895 use_syslog
= opts
.syslog
;
2897 openlog("virtiofsd", LOG_PID
, LOG_DAEMON
);
2900 if (opts
.show_help
) {
2901 printf("usage: %s [options]\n\n", argv
[0]);
2902 fuse_cmdline_help();
2903 printf(" -o source=PATH shared directory tree\n");
2904 fuse_lowlevel_help();
2907 } else if (opts
.show_version
) {
2908 fuse_lowlevel_version();
2911 } else if (opts
.print_capabilities
) {
2912 print_capabilities();
2917 if (fuse_opt_parse(&args
, &lo
, lo_opts
, NULL
) == -1) {
2922 * log_level is 0 if not configured via cmd options (0 is LOG_EMERG,
2923 * and we don't use this log level).
2925 if (opts
.log_level
!= 0) {
2926 current_log_level
= opts
.log_level
;
2928 lo
.debug
= opts
.debug
;
2930 current_log_level
= FUSE_LOG_DEBUG
;
2936 res
= lstat(lo
.source
, &stat
);
2938 fuse_log(FUSE_LOG_ERR
, "failed to stat source (\"%s\"): %m\n",
2942 if (!S_ISDIR(stat
.st_mode
)) {
2943 fuse_log(FUSE_LOG_ERR
, "source is not a directory\n");
2947 lo
.source
= strdup("/");
2949 if (!lo
.timeout_set
) {
2960 lo
.timeout
= 86400.0;
2963 } else if (lo
.timeout
< 0) {
2964 fuse_log(FUSE_LOG_ERR
, "timeout is negative (%lf)\n", lo
.timeout
);
2968 se
= fuse_session_new(&args
, &lo_oper
, sizeof(lo_oper
), &lo
);
2973 if (fuse_set_signal_handlers(se
) != 0) {
2977 if (fuse_session_mount(se
) != 0) {
2981 fuse_daemonize(opts
.foreground
);
2983 setup_nofile_rlimit();
2985 /* Must be before sandbox since it wants /proc */
2988 setup_sandbox(&lo
, se
, opts
.syslog
);
2990 setup_root(&lo
, &lo
.root
);
2991 /* Block until ctrl+c or fusermount -u */
2992 ret
= virtio_loop(se
);
2994 fuse_session_unmount(se
);
2997 fuse_remove_signal_handlers(se
);
2999 fuse_session_destroy(se
);
3001 fuse_opt_free_args(&args
);
3003 fuse_lo_data_cleanup(&lo
);