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 (lo
->flock
&& conn
->capable
& FUSE_CAP_FLOCK_LOCKS
) {
550 fuse_log(FUSE_LOG_DEBUG
, "lo_init: activating flock locks\n");
551 conn
->want
|= FUSE_CAP_FLOCK_LOCKS
;
554 if (conn
->capable
& FUSE_CAP_POSIX_LOCKS
) {
555 if (lo
->posix_lock
) {
556 fuse_log(FUSE_LOG_DEBUG
, "lo_init: activating posix locks\n");
557 conn
->want
|= FUSE_CAP_POSIX_LOCKS
;
559 fuse_log(FUSE_LOG_DEBUG
, "lo_init: disabling posix locks\n");
560 conn
->want
&= ~FUSE_CAP_POSIX_LOCKS
;
564 if ((lo
->cache
== CACHE_NONE
&& !lo
->readdirplus_set
) ||
565 lo
->readdirplus_clear
) {
566 fuse_log(FUSE_LOG_DEBUG
, "lo_init: disabling readdirplus\n");
567 conn
->want
&= ~FUSE_CAP_READDIRPLUS
;
571 static void lo_getattr(fuse_req_t req
, fuse_ino_t ino
,
572 struct fuse_file_info
*fi
)
576 struct lo_data
*lo
= lo_data(req
);
581 fstatat(lo_fd(req
, ino
), "", &buf
, AT_EMPTY_PATH
| AT_SYMLINK_NOFOLLOW
);
583 return (void)fuse_reply_err(req
, errno
);
586 fuse_reply_attr(req
, &buf
, lo
->timeout
);
590 * Increments parent->nlookup and caller must release refcount using
591 * lo_inode_put(&parent).
593 static int lo_parent_and_name(struct lo_data
*lo
, struct lo_inode
*inode
,
594 char path
[PATH_MAX
], struct lo_inode
**parent
)
604 sprintf(procname
, "%i", inode
->fd
);
606 res
= readlinkat(lo
->proc_self_fd
, procname
, path
, PATH_MAX
);
608 fuse_log(FUSE_LOG_WARNING
, "%s: readlink failed: %m\n", __func__
);
612 if (res
>= PATH_MAX
) {
613 fuse_log(FUSE_LOG_WARNING
, "%s: readlink overflowed\n", __func__
);
618 last
= strrchr(path
, '/');
620 /* Shouldn't happen */
623 "%s: INTERNAL ERROR: bad path read from proc\n", __func__
);
628 pthread_mutex_lock(&lo
->mutex
);
630 g_atomic_int_inc(&p
->refcount
);
631 pthread_mutex_unlock(&lo
->mutex
);
634 res
= fstatat(AT_FDCWD
, last
== path
? "/" : path
, &stat
, 0);
637 fuse_log(FUSE_LOG_WARNING
,
638 "%s: failed to stat parent: %m\n", __func__
);
642 p
= lo_find(lo
, &stat
);
645 fuse_log(FUSE_LOG_WARNING
,
646 "%s: failed to find parent\n", __func__
);
652 res
= fstatat(p
->fd
, last
, &stat
, AT_SYMLINK_NOFOLLOW
);
655 fuse_log(FUSE_LOG_WARNING
,
656 "%s: failed to stat last\n", __func__
);
660 if (stat
.st_dev
!= inode
->key
.dev
|| stat
.st_ino
!= inode
->key
.ino
) {
662 fuse_log(FUSE_LOG_WARNING
,
663 "%s: failed to match last\n", __func__
);
668 memmove(path
, last
, strlen(last
) + 1);
673 unref_inode_lolocked(lo
, p
, 1);
674 lo_inode_put(lo
, &p
);
685 static int utimensat_empty(struct lo_data
*lo
, struct lo_inode
*inode
,
686 const struct timespec
*tv
)
689 struct lo_inode
*parent
;
692 if (inode
->is_symlink
) {
693 res
= utimensat(inode
->fd
, "", tv
, AT_EMPTY_PATH
);
694 if (res
== -1 && errno
== EINVAL
) {
695 /* Sorry, no race free way to set times on symlink. */
704 sprintf(path
, "%i", inode
->fd
);
706 return utimensat(lo
->proc_self_fd
, path
, tv
, 0);
709 res
= lo_parent_and_name(lo
, inode
, path
, &parent
);
711 res
= utimensat(parent
->fd
, path
, tv
, AT_SYMLINK_NOFOLLOW
);
712 unref_inode_lolocked(lo
, parent
, 1);
713 lo_inode_put(lo
, &parent
);
719 static int lo_fi_fd(fuse_req_t req
, struct fuse_file_info
*fi
)
721 struct lo_data
*lo
= lo_data(req
);
722 struct lo_map_elem
*elem
;
724 pthread_mutex_lock(&lo
->mutex
);
725 elem
= lo_map_get(&lo
->fd_map
, fi
->fh
);
726 pthread_mutex_unlock(&lo
->mutex
);
735 static void lo_setattr(fuse_req_t req
, fuse_ino_t ino
, struct stat
*attr
,
736 int valid
, struct fuse_file_info
*fi
)
740 struct lo_data
*lo
= lo_data(req
);
741 struct lo_inode
*inode
;
746 inode
= lo_inode(req
, ino
);
748 fuse_reply_err(req
, EBADF
);
754 /* If fi->fh is invalid we'll report EBADF later */
756 fd
= lo_fi_fd(req
, fi
);
759 if (valid
& FUSE_SET_ATTR_MODE
) {
761 res
= fchmod(fd
, attr
->st_mode
);
763 sprintf(procname
, "%i", ifd
);
764 res
= fchmodat(lo
->proc_self_fd
, procname
, attr
->st_mode
, 0);
770 if (valid
& (FUSE_SET_ATTR_UID
| FUSE_SET_ATTR_GID
)) {
771 uid_t uid
= (valid
& FUSE_SET_ATTR_UID
) ? attr
->st_uid
: (uid_t
)-1;
772 gid_t gid
= (valid
& FUSE_SET_ATTR_GID
) ? attr
->st_gid
: (gid_t
)-1;
774 res
= fchownat(ifd
, "", uid
, gid
, AT_EMPTY_PATH
| AT_SYMLINK_NOFOLLOW
);
779 if (valid
& FUSE_SET_ATTR_SIZE
) {
785 sprintf(procname
, "%i", ifd
);
786 truncfd
= openat(lo
->proc_self_fd
, procname
, O_RDWR
);
792 res
= ftruncate(truncfd
, attr
->st_size
);
802 if (valid
& (FUSE_SET_ATTR_ATIME
| FUSE_SET_ATTR_MTIME
)) {
803 struct timespec tv
[2];
807 tv
[0].tv_nsec
= UTIME_OMIT
;
808 tv
[1].tv_nsec
= UTIME_OMIT
;
810 if (valid
& FUSE_SET_ATTR_ATIME_NOW
) {
811 tv
[0].tv_nsec
= UTIME_NOW
;
812 } else if (valid
& FUSE_SET_ATTR_ATIME
) {
813 tv
[0] = attr
->st_atim
;
816 if (valid
& FUSE_SET_ATTR_MTIME_NOW
) {
817 tv
[1].tv_nsec
= UTIME_NOW
;
818 } else if (valid
& FUSE_SET_ATTR_MTIME
) {
819 tv
[1] = attr
->st_mtim
;
823 res
= futimens(fd
, tv
);
825 res
= utimensat_empty(lo
, inode
, tv
);
831 lo_inode_put(lo
, &inode
);
833 return lo_getattr(req
, ino
, fi
);
837 lo_inode_put(lo
, &inode
);
838 fuse_reply_err(req
, saverr
);
841 static struct lo_inode
*lo_find(struct lo_data
*lo
, struct stat
*st
)
844 struct lo_key key
= {
849 pthread_mutex_lock(&lo
->mutex
);
850 p
= g_hash_table_lookup(lo
->inodes
, &key
);
852 assert(p
->nlookup
> 0);
854 g_atomic_int_inc(&p
->refcount
);
856 pthread_mutex_unlock(&lo
->mutex
);
861 /* value_destroy_func for posix_locks GHashTable */
862 static void posix_locks_value_destroy(gpointer data
)
864 struct lo_inode_plock
*plock
= data
;
867 * We had used open() for locks and had only one fd. So
868 * closing this fd should release all OFD locks.
875 * Increments nlookup and caller must release refcount using
876 * lo_inode_put(&parent).
878 static int lo_do_lookup(fuse_req_t req
, fuse_ino_t parent
, const char *name
,
879 struct fuse_entry_param
*e
)
884 struct lo_data
*lo
= lo_data(req
);
885 struct lo_inode
*inode
= NULL
;
886 struct lo_inode
*dir
= lo_inode(req
, parent
);
889 * name_to_handle_at() and open_by_handle_at() can reach here with fuse
890 * mount point in guest, but we don't have its inode info in the
897 memset(e
, 0, sizeof(*e
));
898 e
->attr_timeout
= lo
->timeout
;
899 e
->entry_timeout
= lo
->timeout
;
901 /* Do not allow escaping root directory */
902 if (dir
== &lo
->root
&& strcmp(name
, "..") == 0) {
906 newfd
= openat(dir
->fd
, name
, O_PATH
| O_NOFOLLOW
);
911 res
= fstatat(newfd
, "", &e
->attr
, AT_EMPTY_PATH
| AT_SYMLINK_NOFOLLOW
);
916 inode
= lo_find(lo
, &e
->attr
);
921 inode
= calloc(1, sizeof(struct lo_inode
));
926 inode
->is_symlink
= S_ISLNK(e
->attr
.st_mode
);
929 * One for the caller and one for nlookup (released in
930 * unref_inode_lolocked())
932 g_atomic_int_set(&inode
->refcount
, 2);
937 inode
->key
.ino
= e
->attr
.st_ino
;
938 inode
->key
.dev
= e
->attr
.st_dev
;
939 pthread_mutex_init(&inode
->plock_mutex
, NULL
);
940 inode
->posix_locks
= g_hash_table_new_full(
941 g_direct_hash
, g_direct_equal
, NULL
, posix_locks_value_destroy
);
943 pthread_mutex_lock(&lo
->mutex
);
944 inode
->fuse_ino
= lo_add_inode_mapping(req
, inode
);
945 g_hash_table_insert(lo
->inodes
, &inode
->key
, inode
);
946 pthread_mutex_unlock(&lo
->mutex
);
948 e
->ino
= inode
->fuse_ino
;
949 lo_inode_put(lo
, &inode
);
950 lo_inode_put(lo
, &dir
);
952 fuse_log(FUSE_LOG_DEBUG
, " %lli/%s -> %lli\n", (unsigned long long)parent
,
953 name
, (unsigned long long)e
->ino
);
962 lo_inode_put(lo
, &inode
);
963 lo_inode_put(lo
, &dir
);
967 static void lo_lookup(fuse_req_t req
, fuse_ino_t parent
, const char *name
)
969 struct fuse_entry_param e
;
972 fuse_log(FUSE_LOG_DEBUG
, "lo_lookup(parent=%" PRIu64
", name=%s)\n", parent
,
976 * Don't use is_safe_path_component(), allow "." and ".." for NFS export
979 if (strchr(name
, '/')) {
980 fuse_reply_err(req
, EINVAL
);
984 err
= lo_do_lookup(req
, parent
, name
, &e
);
986 fuse_reply_err(req
, err
);
988 fuse_reply_entry(req
, &e
);
993 * On some archs, setres*id is limited to 2^16 but they
994 * provide setres*id32 variants that allow 2^32.
995 * Others just let setres*id do 2^32 anyway.
997 #ifdef SYS_setresgid32
998 #define OURSYS_setresgid SYS_setresgid32
1000 #define OURSYS_setresgid SYS_setresgid
1003 #ifdef SYS_setresuid32
1004 #define OURSYS_setresuid SYS_setresuid32
1006 #define OURSYS_setresuid SYS_setresuid
1010 * Change to uid/gid of caller so that file is created with
1011 * ownership of caller.
1012 * TODO: What about selinux context?
1014 static int lo_change_cred(fuse_req_t req
, struct lo_cred
*old
)
1018 old
->euid
= geteuid();
1019 old
->egid
= getegid();
1021 res
= syscall(OURSYS_setresgid
, -1, fuse_req_ctx(req
)->gid
, -1);
1026 res
= syscall(OURSYS_setresuid
, -1, fuse_req_ctx(req
)->uid
, -1);
1028 int errno_save
= errno
;
1030 syscall(OURSYS_setresgid
, -1, old
->egid
, -1);
1037 /* Regain Privileges */
1038 static void lo_restore_cred(struct lo_cred
*old
)
1042 res
= syscall(OURSYS_setresuid
, -1, old
->euid
, -1);
1044 fuse_log(FUSE_LOG_ERR
, "seteuid(%u): %m\n", old
->euid
);
1048 res
= syscall(OURSYS_setresgid
, -1, old
->egid
, -1);
1050 fuse_log(FUSE_LOG_ERR
, "setegid(%u): %m\n", old
->egid
);
1055 static void lo_mknod_symlink(fuse_req_t req
, fuse_ino_t parent
,
1056 const char *name
, mode_t mode
, dev_t rdev
,
1061 struct lo_data
*lo
= lo_data(req
);
1062 struct lo_inode
*dir
;
1063 struct fuse_entry_param e
;
1064 struct lo_cred old
= {};
1066 if (!is_safe_path_component(name
)) {
1067 fuse_reply_err(req
, EINVAL
);
1071 dir
= lo_inode(req
, parent
);
1073 fuse_reply_err(req
, EBADF
);
1079 saverr
= lo_change_cred(req
, &old
);
1084 res
= mknod_wrapper(dir
->fd
, name
, link
, mode
, rdev
);
1088 lo_restore_cred(&old
);
1094 saverr
= lo_do_lookup(req
, parent
, name
, &e
);
1099 fuse_log(FUSE_LOG_DEBUG
, " %lli/%s -> %lli\n", (unsigned long long)parent
,
1100 name
, (unsigned long long)e
.ino
);
1102 fuse_reply_entry(req
, &e
);
1103 lo_inode_put(lo
, &dir
);
1107 lo_inode_put(lo
, &dir
);
1108 fuse_reply_err(req
, saverr
);
1111 static void lo_mknod(fuse_req_t req
, fuse_ino_t parent
, const char *name
,
1112 mode_t mode
, dev_t rdev
)
1114 lo_mknod_symlink(req
, parent
, name
, mode
, rdev
, NULL
);
1117 static void lo_mkdir(fuse_req_t req
, fuse_ino_t parent
, const char *name
,
1120 lo_mknod_symlink(req
, parent
, name
, S_IFDIR
| mode
, 0, NULL
);
1123 static void lo_symlink(fuse_req_t req
, const char *link
, fuse_ino_t parent
,
1126 lo_mknod_symlink(req
, parent
, name
, S_IFLNK
, 0, link
);
1129 static int linkat_empty_nofollow(struct lo_data
*lo
, struct lo_inode
*inode
,
1130 int dfd
, const char *name
)
1133 struct lo_inode
*parent
;
1134 char path
[PATH_MAX
];
1136 if (inode
->is_symlink
) {
1137 res
= linkat(inode
->fd
, "", dfd
, name
, AT_EMPTY_PATH
);
1138 if (res
== -1 && (errno
== ENOENT
|| errno
== EINVAL
)) {
1139 /* Sorry, no race free way to hard-link a symlink. */
1149 sprintf(path
, "%i", inode
->fd
);
1151 return linkat(lo
->proc_self_fd
, path
, dfd
, name
, AT_SYMLINK_FOLLOW
);
1154 res
= lo_parent_and_name(lo
, inode
, path
, &parent
);
1156 res
= linkat(parent
->fd
, path
, dfd
, name
, 0);
1157 unref_inode_lolocked(lo
, parent
, 1);
1158 lo_inode_put(lo
, &parent
);
1164 static void lo_link(fuse_req_t req
, fuse_ino_t ino
, fuse_ino_t parent
,
1168 struct lo_data
*lo
= lo_data(req
);
1169 struct lo_inode
*parent_inode
;
1170 struct lo_inode
*inode
;
1171 struct fuse_entry_param e
;
1174 if (!is_safe_path_component(name
)) {
1175 fuse_reply_err(req
, EINVAL
);
1179 parent_inode
= lo_inode(req
, parent
);
1180 inode
= lo_inode(req
, ino
);
1181 if (!parent_inode
|| !inode
) {
1186 memset(&e
, 0, sizeof(struct fuse_entry_param
));
1187 e
.attr_timeout
= lo
->timeout
;
1188 e
.entry_timeout
= lo
->timeout
;
1190 res
= linkat_empty_nofollow(lo
, inode
, parent_inode
->fd
, name
);
1195 res
= fstatat(inode
->fd
, "", &e
.attr
, AT_EMPTY_PATH
| AT_SYMLINK_NOFOLLOW
);
1200 pthread_mutex_lock(&lo
->mutex
);
1202 pthread_mutex_unlock(&lo
->mutex
);
1203 e
.ino
= inode
->fuse_ino
;
1205 fuse_log(FUSE_LOG_DEBUG
, " %lli/%s -> %lli\n", (unsigned long long)parent
,
1206 name
, (unsigned long long)e
.ino
);
1208 fuse_reply_entry(req
, &e
);
1209 lo_inode_put(lo
, &parent_inode
);
1210 lo_inode_put(lo
, &inode
);
1215 lo_inode_put(lo
, &parent_inode
);
1216 lo_inode_put(lo
, &inode
);
1217 fuse_reply_err(req
, saverr
);
1220 /* Increments nlookup and caller must release refcount using lo_inode_put() */
1221 static struct lo_inode
*lookup_name(fuse_req_t req
, fuse_ino_t parent
,
1227 res
= fstatat(lo_fd(req
, parent
), name
, &attr
,
1228 AT_EMPTY_PATH
| AT_SYMLINK_NOFOLLOW
);
1233 return lo_find(lo_data(req
), &attr
);
1236 static void lo_rmdir(fuse_req_t req
, fuse_ino_t parent
, const char *name
)
1239 struct lo_inode
*inode
;
1240 struct lo_data
*lo
= lo_data(req
);
1242 if (!is_safe_path_component(name
)) {
1243 fuse_reply_err(req
, EINVAL
);
1247 inode
= lookup_name(req
, parent
, name
);
1249 fuse_reply_err(req
, EIO
);
1253 res
= unlinkat(lo_fd(req
, parent
), name
, AT_REMOVEDIR
);
1255 fuse_reply_err(req
, res
== -1 ? errno
: 0);
1256 unref_inode_lolocked(lo
, inode
, 1);
1257 lo_inode_put(lo
, &inode
);
1260 static void lo_rename(fuse_req_t req
, fuse_ino_t parent
, const char *name
,
1261 fuse_ino_t newparent
, const char *newname
,
1265 struct lo_inode
*parent_inode
;
1266 struct lo_inode
*newparent_inode
;
1267 struct lo_inode
*oldinode
= NULL
;
1268 struct lo_inode
*newinode
= NULL
;
1269 struct lo_data
*lo
= lo_data(req
);
1271 if (!is_safe_path_component(name
) || !is_safe_path_component(newname
)) {
1272 fuse_reply_err(req
, EINVAL
);
1276 parent_inode
= lo_inode(req
, parent
);
1277 newparent_inode
= lo_inode(req
, newparent
);
1278 if (!parent_inode
|| !newparent_inode
) {
1279 fuse_reply_err(req
, EBADF
);
1283 oldinode
= lookup_name(req
, parent
, name
);
1284 newinode
= lookup_name(req
, newparent
, newname
);
1287 fuse_reply_err(req
, EIO
);
1292 #ifndef SYS_renameat2
1293 fuse_reply_err(req
, EINVAL
);
1295 res
= syscall(SYS_renameat2
, parent_inode
->fd
, name
,
1296 newparent_inode
->fd
, newname
, flags
);
1297 if (res
== -1 && errno
== ENOSYS
) {
1298 fuse_reply_err(req
, EINVAL
);
1300 fuse_reply_err(req
, res
== -1 ? errno
: 0);
1306 res
= renameat(parent_inode
->fd
, name
, newparent_inode
->fd
, newname
);
1308 fuse_reply_err(req
, res
== -1 ? errno
: 0);
1310 unref_inode_lolocked(lo
, oldinode
, 1);
1311 unref_inode_lolocked(lo
, newinode
, 1);
1312 lo_inode_put(lo
, &oldinode
);
1313 lo_inode_put(lo
, &newinode
);
1314 lo_inode_put(lo
, &parent_inode
);
1315 lo_inode_put(lo
, &newparent_inode
);
1318 static void lo_unlink(fuse_req_t req
, fuse_ino_t parent
, const char *name
)
1321 struct lo_inode
*inode
;
1322 struct lo_data
*lo
= lo_data(req
);
1324 if (!is_safe_path_component(name
)) {
1325 fuse_reply_err(req
, EINVAL
);
1329 inode
= lookup_name(req
, parent
, name
);
1331 fuse_reply_err(req
, EIO
);
1335 res
= unlinkat(lo_fd(req
, parent
), name
, 0);
1337 fuse_reply_err(req
, res
== -1 ? errno
: 0);
1338 unref_inode_lolocked(lo
, inode
, 1);
1339 lo_inode_put(lo
, &inode
);
1342 static void unref_inode_lolocked(struct lo_data
*lo
, struct lo_inode
*inode
,
1349 pthread_mutex_lock(&lo
->mutex
);
1350 assert(inode
->nlookup
>= n
);
1351 inode
->nlookup
-= n
;
1352 if (!inode
->nlookup
) {
1353 lo_map_remove(&lo
->ino_map
, inode
->fuse_ino
);
1354 g_hash_table_remove(lo
->inodes
, &inode
->key
);
1355 if (g_hash_table_size(inode
->posix_locks
)) {
1356 fuse_log(FUSE_LOG_WARNING
, "Hash table is not empty\n");
1358 g_hash_table_destroy(inode
->posix_locks
);
1359 pthread_mutex_destroy(&inode
->plock_mutex
);
1360 pthread_mutex_unlock(&lo
->mutex
);
1362 /* Drop our refcount from lo_do_lookup() */
1363 lo_inode_put(lo
, &inode
);
1365 pthread_mutex_unlock(&lo
->mutex
);
1369 static int unref_all_inodes_cb(gpointer key
, gpointer value
, gpointer user_data
)
1371 struct lo_inode
*inode
= value
;
1372 struct lo_data
*lo
= user_data
;
1375 lo_map_remove(&lo
->ino_map
, inode
->fuse_ino
);
1377 lo_inode_put(lo
, &inode
); /* Drop our refcount from lo_do_lookup() */
1382 static void unref_all_inodes(struct lo_data
*lo
)
1384 pthread_mutex_lock(&lo
->mutex
);
1385 g_hash_table_foreach_remove(lo
->inodes
, unref_all_inodes_cb
, lo
);
1386 pthread_mutex_unlock(&lo
->mutex
);
1389 static void lo_forget_one(fuse_req_t req
, fuse_ino_t ino
, uint64_t nlookup
)
1391 struct lo_data
*lo
= lo_data(req
);
1392 struct lo_inode
*inode
;
1394 inode
= lo_inode(req
, ino
);
1399 fuse_log(FUSE_LOG_DEBUG
, " forget %lli %lli -%lli\n",
1400 (unsigned long long)ino
, (unsigned long long)inode
->nlookup
,
1401 (unsigned long long)nlookup
);
1403 unref_inode_lolocked(lo
, inode
, nlookup
);
1404 lo_inode_put(lo
, &inode
);
1407 static void lo_forget(fuse_req_t req
, fuse_ino_t ino
, uint64_t nlookup
)
1409 lo_forget_one(req
, ino
, nlookup
);
1410 fuse_reply_none(req
);
1413 static void lo_forget_multi(fuse_req_t req
, size_t count
,
1414 struct fuse_forget_data
*forgets
)
1418 for (i
= 0; i
< count
; i
++) {
1419 lo_forget_one(req
, forgets
[i
].ino
, forgets
[i
].nlookup
);
1421 fuse_reply_none(req
);
1424 static void lo_readlink(fuse_req_t req
, fuse_ino_t ino
)
1426 char buf
[PATH_MAX
+ 1];
1429 res
= readlinkat(lo_fd(req
, ino
), "", buf
, sizeof(buf
));
1431 return (void)fuse_reply_err(req
, errno
);
1434 if (res
== sizeof(buf
)) {
1435 return (void)fuse_reply_err(req
, ENAMETOOLONG
);
1440 fuse_reply_readlink(req
, buf
);
1446 struct dirent
*entry
;
1450 static void lo_dirp_put(struct lo_dirp
**dp
)
1452 struct lo_dirp
*d
= *dp
;
1459 if (g_atomic_int_dec_and_test(&d
->refcount
)) {
1465 /* Call lo_dirp_put() on the return value when no longer needed */
1466 static struct lo_dirp
*lo_dirp(fuse_req_t req
, struct fuse_file_info
*fi
)
1468 struct lo_data
*lo
= lo_data(req
);
1469 struct lo_map_elem
*elem
;
1471 pthread_mutex_lock(&lo
->mutex
);
1472 elem
= lo_map_get(&lo
->dirp_map
, fi
->fh
);
1474 g_atomic_int_inc(&elem
->dirp
->refcount
);
1476 pthread_mutex_unlock(&lo
->mutex
);
1484 static void lo_opendir(fuse_req_t req
, fuse_ino_t ino
,
1485 struct fuse_file_info
*fi
)
1488 struct lo_data
*lo
= lo_data(req
);
1493 d
= calloc(1, sizeof(struct lo_dirp
));
1498 fd
= openat(lo_fd(req
, ino
), ".", O_RDONLY
);
1503 d
->dp
= fdopendir(fd
);
1504 if (d
->dp
== NULL
) {
1511 g_atomic_int_set(&d
->refcount
, 1); /* paired with lo_releasedir() */
1512 pthread_mutex_lock(&lo
->mutex
);
1513 fh
= lo_add_dirp_mapping(req
, d
);
1514 pthread_mutex_unlock(&lo
->mutex
);
1520 if (lo
->cache
== CACHE_ALWAYS
) {
1523 fuse_reply_open(req
, fi
);
1538 fuse_reply_err(req
, error
);
1541 static void lo_do_readdir(fuse_req_t req
, fuse_ino_t ino
, size_t size
,
1542 off_t offset
, struct fuse_file_info
*fi
, int plus
)
1544 struct lo_data
*lo
= lo_data(req
);
1545 struct lo_dirp
*d
= NULL
;
1546 struct lo_inode
*dinode
;
1552 dinode
= lo_inode(req
, ino
);
1557 d
= lo_dirp(req
, fi
);
1563 buf
= calloc(1, size
);
1569 if (offset
!= d
->offset
) {
1570 seekdir(d
->dp
, offset
);
1581 d
->entry
= readdir(d
->dp
);
1583 if (errno
) { /* Error */
1586 } else { /* End of stream */
1591 nextoff
= d
->entry
->d_off
;
1592 name
= d
->entry
->d_name
;
1594 fuse_ino_t entry_ino
= 0;
1595 struct fuse_entry_param e
= (struct fuse_entry_param
){
1596 .attr
.st_ino
= d
->entry
->d_ino
,
1597 .attr
.st_mode
= d
->entry
->d_type
<< 12,
1600 /* Hide root's parent directory */
1601 if (dinode
== &lo
->root
&& strcmp(name
, "..") == 0) {
1602 e
.attr
.st_ino
= lo
->root
.key
.ino
;
1603 e
.attr
.st_mode
= DT_DIR
<< 12;
1607 if (!is_dot_or_dotdot(name
)) {
1608 err
= lo_do_lookup(req
, ino
, name
, &e
);
1615 entsize
= fuse_add_direntry_plus(req
, p
, rem
, name
, &e
, nextoff
);
1617 entsize
= fuse_add_direntry(req
, p
, rem
, name
, &e
.attr
, nextoff
);
1619 if (entsize
> rem
) {
1620 if (entry_ino
!= 0) {
1621 lo_forget_one(req
, entry_ino
, 1);
1630 d
->offset
= nextoff
;
1636 lo_inode_put(lo
, &dinode
);
1639 * If there's an error, we can only signal it if we haven't stored
1640 * any entries yet - otherwise we'd end up with wrong lookup
1641 * counts for the entries that are already in the buffer. So we
1642 * return what we've collected until that point.
1644 if (err
&& rem
== size
) {
1645 fuse_reply_err(req
, err
);
1647 fuse_reply_buf(req
, buf
, size
- rem
);
1652 static void lo_readdir(fuse_req_t req
, fuse_ino_t ino
, size_t size
,
1653 off_t offset
, struct fuse_file_info
*fi
)
1655 lo_do_readdir(req
, ino
, size
, offset
, fi
, 0);
1658 static void lo_readdirplus(fuse_req_t req
, fuse_ino_t ino
, size_t size
,
1659 off_t offset
, struct fuse_file_info
*fi
)
1661 lo_do_readdir(req
, ino
, size
, offset
, fi
, 1);
1664 static void lo_releasedir(fuse_req_t req
, fuse_ino_t ino
,
1665 struct fuse_file_info
*fi
)
1667 struct lo_data
*lo
= lo_data(req
);
1668 struct lo_map_elem
*elem
;
1673 pthread_mutex_lock(&lo
->mutex
);
1674 elem
= lo_map_get(&lo
->dirp_map
, fi
->fh
);
1676 pthread_mutex_unlock(&lo
->mutex
);
1677 fuse_reply_err(req
, EBADF
);
1682 lo_map_remove(&lo
->dirp_map
, fi
->fh
);
1683 pthread_mutex_unlock(&lo
->mutex
);
1685 lo_dirp_put(&d
); /* paired with lo_opendir() */
1687 fuse_reply_err(req
, 0);
1690 static void lo_create(fuse_req_t req
, fuse_ino_t parent
, const char *name
,
1691 mode_t mode
, struct fuse_file_info
*fi
)
1694 struct lo_data
*lo
= lo_data(req
);
1695 struct lo_inode
*parent_inode
;
1696 struct fuse_entry_param e
;
1698 struct lo_cred old
= {};
1700 fuse_log(FUSE_LOG_DEBUG
, "lo_create(parent=%" PRIu64
", name=%s)\n", parent
,
1703 if (!is_safe_path_component(name
)) {
1704 fuse_reply_err(req
, EINVAL
);
1708 parent_inode
= lo_inode(req
, parent
);
1709 if (!parent_inode
) {
1710 fuse_reply_err(req
, EBADF
);
1714 err
= lo_change_cred(req
, &old
);
1719 fd
= openat(parent_inode
->fd
, name
, (fi
->flags
| O_CREAT
) & ~O_NOFOLLOW
,
1721 err
= fd
== -1 ? errno
: 0;
1722 lo_restore_cred(&old
);
1727 pthread_mutex_lock(&lo
->mutex
);
1728 fh
= lo_add_fd_mapping(req
, fd
);
1729 pthread_mutex_unlock(&lo
->mutex
);
1737 err
= lo_do_lookup(req
, parent
, name
, &e
);
1739 if (lo
->cache
== CACHE_NONE
) {
1741 } else if (lo
->cache
== CACHE_ALWAYS
) {
1746 lo_inode_put(lo
, &parent_inode
);
1749 fuse_reply_err(req
, err
);
1751 fuse_reply_create(req
, &e
, fi
);
1755 /* Should be called with inode->plock_mutex held */
1756 static struct lo_inode_plock
*lookup_create_plock_ctx(struct lo_data
*lo
,
1757 struct lo_inode
*inode
,
1758 uint64_t lock_owner
,
1759 pid_t pid
, int *err
)
1761 struct lo_inode_plock
*plock
;
1766 g_hash_table_lookup(inode
->posix_locks
, GUINT_TO_POINTER(lock_owner
));
1772 plock
= malloc(sizeof(struct lo_inode_plock
));
1778 /* Open another instance of file which can be used for ofd locks. */
1779 sprintf(procname
, "%i", inode
->fd
);
1781 /* TODO: What if file is not writable? */
1782 fd
= openat(lo
->proc_self_fd
, procname
, O_RDWR
);
1789 plock
->lock_owner
= lock_owner
;
1791 g_hash_table_insert(inode
->posix_locks
, GUINT_TO_POINTER(plock
->lock_owner
),
1796 static void lo_getlk(fuse_req_t req
, fuse_ino_t ino
, struct fuse_file_info
*fi
,
1799 struct lo_data
*lo
= lo_data(req
);
1800 struct lo_inode
*inode
;
1801 struct lo_inode_plock
*plock
;
1802 int ret
, saverr
= 0;
1804 fuse_log(FUSE_LOG_DEBUG
,
1805 "lo_getlk(ino=%" PRIu64
", flags=%d)"
1806 " owner=0x%lx, l_type=%d l_start=0x%lx"
1808 ino
, fi
->flags
, fi
->lock_owner
, lock
->l_type
, lock
->l_start
,
1811 inode
= lo_inode(req
, ino
);
1813 fuse_reply_err(req
, EBADF
);
1817 pthread_mutex_lock(&inode
->plock_mutex
);
1819 lookup_create_plock_ctx(lo
, inode
, fi
->lock_owner
, lock
->l_pid
, &ret
);
1825 ret
= fcntl(plock
->fd
, F_OFD_GETLK
, lock
);
1831 pthread_mutex_unlock(&inode
->plock_mutex
);
1832 lo_inode_put(lo
, &inode
);
1835 fuse_reply_err(req
, saverr
);
1837 fuse_reply_lock(req
, lock
);
1841 static void lo_setlk(fuse_req_t req
, fuse_ino_t ino
, struct fuse_file_info
*fi
,
1842 struct flock
*lock
, int sleep
)
1844 struct lo_data
*lo
= lo_data(req
);
1845 struct lo_inode
*inode
;
1846 struct lo_inode_plock
*plock
;
1847 int ret
, saverr
= 0;
1849 fuse_log(FUSE_LOG_DEBUG
,
1850 "lo_setlk(ino=%" PRIu64
", flags=%d)"
1851 " cmd=%d pid=%d owner=0x%lx sleep=%d l_whence=%d"
1852 " l_start=0x%lx l_len=0x%lx\n",
1853 ino
, fi
->flags
, lock
->l_type
, lock
->l_pid
, fi
->lock_owner
, sleep
,
1854 lock
->l_whence
, lock
->l_start
, lock
->l_len
);
1857 fuse_reply_err(req
, EOPNOTSUPP
);
1861 inode
= lo_inode(req
, ino
);
1863 fuse_reply_err(req
, EBADF
);
1867 pthread_mutex_lock(&inode
->plock_mutex
);
1869 lookup_create_plock_ctx(lo
, inode
, fi
->lock_owner
, lock
->l_pid
, &ret
);
1876 /* TODO: Is it alright to modify flock? */
1878 ret
= fcntl(plock
->fd
, F_OFD_SETLK
, lock
);
1884 pthread_mutex_unlock(&inode
->plock_mutex
);
1885 lo_inode_put(lo
, &inode
);
1887 fuse_reply_err(req
, saverr
);
1890 static void lo_fsyncdir(fuse_req_t req
, fuse_ino_t ino
, int datasync
,
1891 struct fuse_file_info
*fi
)
1899 d
= lo_dirp(req
, fi
);
1901 fuse_reply_err(req
, EBADF
);
1907 res
= fdatasync(fd
);
1914 fuse_reply_err(req
, res
== -1 ? errno
: 0);
1917 static void lo_open(fuse_req_t req
, fuse_ino_t ino
, struct fuse_file_info
*fi
)
1922 struct lo_data
*lo
= lo_data(req
);
1924 fuse_log(FUSE_LOG_DEBUG
, "lo_open(ino=%" PRIu64
", flags=%d)\n", ino
,
1928 * With writeback cache, kernel may send read requests even
1929 * when userspace opened write-only
1931 if (lo
->writeback
&& (fi
->flags
& O_ACCMODE
) == O_WRONLY
) {
1932 fi
->flags
&= ~O_ACCMODE
;
1933 fi
->flags
|= O_RDWR
;
1937 * With writeback cache, O_APPEND is handled by the kernel.
1938 * This breaks atomicity (since the file may change in the
1939 * underlying filesystem, so that the kernel's idea of the
1940 * end of the file isn't accurate anymore). In this example,
1941 * we just accept that. A more rigorous filesystem may want
1942 * to return an error here
1944 if (lo
->writeback
&& (fi
->flags
& O_APPEND
)) {
1945 fi
->flags
&= ~O_APPEND
;
1948 sprintf(buf
, "%i", lo_fd(req
, ino
));
1949 fd
= openat(lo
->proc_self_fd
, buf
, fi
->flags
& ~O_NOFOLLOW
);
1951 return (void)fuse_reply_err(req
, errno
);
1954 pthread_mutex_lock(&lo
->mutex
);
1955 fh
= lo_add_fd_mapping(req
, fd
);
1956 pthread_mutex_unlock(&lo
->mutex
);
1959 fuse_reply_err(req
, ENOMEM
);
1964 if (lo
->cache
== CACHE_NONE
) {
1966 } else if (lo
->cache
== CACHE_ALWAYS
) {
1969 fuse_reply_open(req
, fi
);
1972 static void lo_release(fuse_req_t req
, fuse_ino_t ino
,
1973 struct fuse_file_info
*fi
)
1975 struct lo_data
*lo
= lo_data(req
);
1976 struct lo_map_elem
*elem
;
1981 pthread_mutex_lock(&lo
->mutex
);
1982 elem
= lo_map_get(&lo
->fd_map
, fi
->fh
);
1986 lo_map_remove(&lo
->fd_map
, fi
->fh
);
1988 pthread_mutex_unlock(&lo
->mutex
);
1991 fuse_reply_err(req
, 0);
1994 static void lo_flush(fuse_req_t req
, fuse_ino_t ino
, struct fuse_file_info
*fi
)
1998 struct lo_inode
*inode
;
2000 inode
= lo_inode(req
, ino
);
2002 fuse_reply_err(req
, EBADF
);
2006 /* An fd is going away. Cleanup associated posix locks */
2007 pthread_mutex_lock(&inode
->plock_mutex
);
2008 g_hash_table_remove(inode
->posix_locks
, GUINT_TO_POINTER(fi
->lock_owner
));
2009 pthread_mutex_unlock(&inode
->plock_mutex
);
2011 res
= close(dup(lo_fi_fd(req
, fi
)));
2012 lo_inode_put(lo_data(req
), &inode
);
2013 fuse_reply_err(req
, res
== -1 ? errno
: 0);
2016 static void lo_fsync(fuse_req_t req
, fuse_ino_t ino
, int datasync
,
2017 struct fuse_file_info
*fi
)
2023 fuse_log(FUSE_LOG_DEBUG
, "lo_fsync(ino=%" PRIu64
", fi=0x%p)\n", ino
,
2027 struct lo_data
*lo
= lo_data(req
);
2029 res
= asprintf(&buf
, "%i", lo_fd(req
, ino
));
2031 return (void)fuse_reply_err(req
, errno
);
2034 fd
= openat(lo
->proc_self_fd
, buf
, O_RDWR
);
2037 return (void)fuse_reply_err(req
, errno
);
2040 fd
= lo_fi_fd(req
, fi
);
2044 res
= fdatasync(fd
);
2051 fuse_reply_err(req
, res
== -1 ? errno
: 0);
2054 static void lo_read(fuse_req_t req
, fuse_ino_t ino
, size_t size
, off_t offset
,
2055 struct fuse_file_info
*fi
)
2057 struct fuse_bufvec buf
= FUSE_BUFVEC_INIT(size
);
2059 fuse_log(FUSE_LOG_DEBUG
,
2060 "lo_read(ino=%" PRIu64
", size=%zd, "
2062 ino
, size
, (unsigned long)offset
);
2064 buf
.buf
[0].flags
= FUSE_BUF_IS_FD
| FUSE_BUF_FD_SEEK
;
2065 buf
.buf
[0].fd
= lo_fi_fd(req
, fi
);
2066 buf
.buf
[0].pos
= offset
;
2068 fuse_reply_data(req
, &buf
);
2071 static void lo_write_buf(fuse_req_t req
, fuse_ino_t ino
,
2072 struct fuse_bufvec
*in_buf
, off_t off
,
2073 struct fuse_file_info
*fi
)
2077 struct fuse_bufvec out_buf
= FUSE_BUFVEC_INIT(fuse_buf_size(in_buf
));
2078 bool cap_fsetid_dropped
= false;
2080 out_buf
.buf
[0].flags
= FUSE_BUF_IS_FD
| FUSE_BUF_FD_SEEK
;
2081 out_buf
.buf
[0].fd
= lo_fi_fd(req
, fi
);
2082 out_buf
.buf
[0].pos
= off
;
2084 fuse_log(FUSE_LOG_DEBUG
,
2085 "lo_write_buf(ino=%" PRIu64
", size=%zd, off=%lu)\n", ino
,
2086 out_buf
.buf
[0].size
, (unsigned long)off
);
2089 * If kill_priv is set, drop CAP_FSETID which should lead to kernel
2090 * clearing setuid/setgid on file.
2092 if (fi
->kill_priv
) {
2093 res
= drop_effective_cap("FSETID", &cap_fsetid_dropped
);
2095 fuse_reply_err(req
, res
);
2100 res
= fuse_buf_copy(&out_buf
, in_buf
);
2102 fuse_reply_err(req
, -res
);
2104 fuse_reply_write(req
, (size_t)res
);
2107 if (cap_fsetid_dropped
) {
2108 res
= gain_effective_cap("FSETID");
2110 fuse_log(FUSE_LOG_ERR
, "Failed to gain CAP_FSETID\n");
2115 static void lo_statfs(fuse_req_t req
, fuse_ino_t ino
)
2118 struct statvfs stbuf
;
2120 res
= fstatvfs(lo_fd(req
, ino
), &stbuf
);
2122 fuse_reply_err(req
, errno
);
2124 fuse_reply_statfs(req
, &stbuf
);
2128 static void lo_fallocate(fuse_req_t req
, fuse_ino_t ino
, int mode
, off_t offset
,
2129 off_t length
, struct fuse_file_info
*fi
)
2131 int err
= EOPNOTSUPP
;
2134 #ifdef CONFIG_FALLOCATE
2135 err
= fallocate(lo_fi_fd(req
, fi
), mode
, offset
, length
);
2140 #elif defined(CONFIG_POSIX_FALLOCATE)
2142 fuse_reply_err(req
, EOPNOTSUPP
);
2146 err
= posix_fallocate(lo_fi_fd(req
, fi
), offset
, length
);
2149 fuse_reply_err(req
, err
);
2152 static void lo_flock(fuse_req_t req
, fuse_ino_t ino
, struct fuse_file_info
*fi
,
2158 res
= flock(lo_fi_fd(req
, fi
), op
);
2160 fuse_reply_err(req
, res
== -1 ? errno
: 0);
2163 static void lo_getxattr(fuse_req_t req
, fuse_ino_t ino
, const char *name
,
2166 struct lo_data
*lo
= lo_data(req
);
2169 struct lo_inode
*inode
;
2174 inode
= lo_inode(req
, ino
);
2176 fuse_reply_err(req
, EBADF
);
2181 if (!lo_data(req
)->xattr
) {
2185 fuse_log(FUSE_LOG_DEBUG
, "lo_getxattr(ino=%" PRIu64
", name=%s size=%zd)\n",
2188 if (inode
->is_symlink
) {
2189 /* Sorry, no race free way to getxattr on symlink. */
2194 sprintf(procname
, "%i", inode
->fd
);
2195 fd
= openat(lo
->proc_self_fd
, procname
, O_RDONLY
);
2201 value
= malloc(size
);
2206 ret
= fgetxattr(fd
, name
, value
, size
);
2215 fuse_reply_buf(req
, value
, ret
);
2217 ret
= fgetxattr(fd
, name
, NULL
, 0);
2222 fuse_reply_xattr(req
, ret
);
2231 lo_inode_put(lo
, &inode
);
2237 lo_inode_put(lo
, &inode
);
2238 fuse_reply_err(req
, saverr
);
2242 static void lo_listxattr(fuse_req_t req
, fuse_ino_t ino
, size_t size
)
2244 struct lo_data
*lo
= lo_data(req
);
2247 struct lo_inode
*inode
;
2252 inode
= lo_inode(req
, ino
);
2254 fuse_reply_err(req
, EBADF
);
2259 if (!lo_data(req
)->xattr
) {
2263 fuse_log(FUSE_LOG_DEBUG
, "lo_listxattr(ino=%" PRIu64
", size=%zd)\n", ino
,
2266 if (inode
->is_symlink
) {
2267 /* Sorry, no race free way to listxattr on symlink. */
2272 sprintf(procname
, "%i", inode
->fd
);
2273 fd
= openat(lo
->proc_self_fd
, procname
, O_RDONLY
);
2279 value
= malloc(size
);
2284 ret
= flistxattr(fd
, value
, size
);
2293 fuse_reply_buf(req
, value
, ret
);
2295 ret
= flistxattr(fd
, NULL
, 0);
2300 fuse_reply_xattr(req
, ret
);
2309 lo_inode_put(lo
, &inode
);
2315 lo_inode_put(lo
, &inode
);
2316 fuse_reply_err(req
, saverr
);
2320 static void lo_setxattr(fuse_req_t req
, fuse_ino_t ino
, const char *name
,
2321 const char *value
, size_t size
, int flags
)
2324 struct lo_data
*lo
= lo_data(req
);
2325 struct lo_inode
*inode
;
2330 inode
= lo_inode(req
, ino
);
2332 fuse_reply_err(req
, EBADF
);
2337 if (!lo_data(req
)->xattr
) {
2341 fuse_log(FUSE_LOG_DEBUG
, "lo_setxattr(ino=%" PRIu64
2342 ", name=%s value=%s size=%zd)\n", ino
, name
, value
, size
);
2344 if (inode
->is_symlink
) {
2345 /* Sorry, no race free way to setxattr on symlink. */
2350 sprintf(procname
, "%i", inode
->fd
);
2351 fd
= openat(lo
->proc_self_fd
, procname
, O_RDWR
);
2357 ret
= fsetxattr(fd
, name
, value
, size
, flags
);
2358 saverr
= ret
== -1 ? errno
: 0;
2365 lo_inode_put(lo
, &inode
);
2366 fuse_reply_err(req
, saverr
);
2369 static void lo_removexattr(fuse_req_t req
, fuse_ino_t ino
, const char *name
)
2372 struct lo_data
*lo
= lo_data(req
);
2373 struct lo_inode
*inode
;
2378 inode
= lo_inode(req
, ino
);
2380 fuse_reply_err(req
, EBADF
);
2385 if (!lo_data(req
)->xattr
) {
2389 fuse_log(FUSE_LOG_DEBUG
, "lo_removexattr(ino=%" PRIu64
", name=%s)\n", ino
,
2392 if (inode
->is_symlink
) {
2393 /* Sorry, no race free way to setxattr on symlink. */
2398 sprintf(procname
, "%i", inode
->fd
);
2399 fd
= openat(lo
->proc_self_fd
, procname
, O_RDWR
);
2405 ret
= fremovexattr(fd
, name
);
2406 saverr
= ret
== -1 ? errno
: 0;
2413 lo_inode_put(lo
, &inode
);
2414 fuse_reply_err(req
, saverr
);
2417 #ifdef HAVE_COPY_FILE_RANGE
2418 static void lo_copy_file_range(fuse_req_t req
, fuse_ino_t ino_in
, off_t off_in
,
2419 struct fuse_file_info
*fi_in
, fuse_ino_t ino_out
,
2420 off_t off_out
, struct fuse_file_info
*fi_out
,
2421 size_t len
, int flags
)
2426 in_fd
= lo_fi_fd(req
, fi_in
);
2427 out_fd
= lo_fi_fd(req
, fi_out
);
2429 fuse_log(FUSE_LOG_DEBUG
,
2430 "lo_copy_file_range(ino=%" PRIu64
"/fd=%d, "
2431 "off=%lu, ino=%" PRIu64
"/fd=%d, "
2432 "off=%lu, size=%zd, flags=0x%x)\n",
2433 ino_in
, in_fd
, off_in
, ino_out
, out_fd
, off_out
, len
, flags
);
2435 res
= copy_file_range(in_fd
, &off_in
, out_fd
, &off_out
, len
, flags
);
2437 fuse_reply_err(req
, -errno
);
2439 fuse_reply_write(req
, res
);
2444 static void lo_lseek(fuse_req_t req
, fuse_ino_t ino
, off_t off
, int whence
,
2445 struct fuse_file_info
*fi
)
2450 res
= lseek(lo_fi_fd(req
, fi
), off
, whence
);
2452 fuse_reply_lseek(req
, res
);
2454 fuse_reply_err(req
, errno
);
2458 static void lo_destroy(void *userdata
)
2460 struct lo_data
*lo
= (struct lo_data
*)userdata
;
2461 unref_all_inodes(lo
);
2464 static struct fuse_lowlevel_ops lo_oper
= {
2466 .lookup
= lo_lookup
,
2469 .symlink
= lo_symlink
,
2471 .unlink
= lo_unlink
,
2473 .rename
= lo_rename
,
2474 .forget
= lo_forget
,
2475 .forget_multi
= lo_forget_multi
,
2476 .getattr
= lo_getattr
,
2477 .setattr
= lo_setattr
,
2478 .readlink
= lo_readlink
,
2479 .opendir
= lo_opendir
,
2480 .readdir
= lo_readdir
,
2481 .readdirplus
= lo_readdirplus
,
2482 .releasedir
= lo_releasedir
,
2483 .fsyncdir
= lo_fsyncdir
,
2484 .create
= lo_create
,
2488 .release
= lo_release
,
2492 .write_buf
= lo_write_buf
,
2493 .statfs
= lo_statfs
,
2494 .fallocate
= lo_fallocate
,
2496 .getxattr
= lo_getxattr
,
2497 .listxattr
= lo_listxattr
,
2498 .setxattr
= lo_setxattr
,
2499 .removexattr
= lo_removexattr
,
2500 #ifdef HAVE_COPY_FILE_RANGE
2501 .copy_file_range
= lo_copy_file_range
,
2504 .destroy
= lo_destroy
,
2507 /* Print vhost-user.json backend program capabilities */
2508 static void print_capabilities(void)
2511 printf(" \"type\": \"fs\"\n");
2516 * Move to a new mount, net, and pid namespaces to isolate this process.
2518 static void setup_namespaces(struct lo_data
*lo
, struct fuse_session
*se
)
2523 * Create a new pid namespace for *child* processes. We'll have to
2524 * fork in order to enter the new pid namespace. A new mount namespace
2525 * is also needed so that we can remount /proc for the new pid
2528 * Our UNIX domain sockets have been created. Now we can move to
2529 * an empty network namespace to prevent TCP/IP and other network
2530 * activity in case this process is compromised.
2532 if (unshare(CLONE_NEWPID
| CLONE_NEWNS
| CLONE_NEWNET
) != 0) {
2533 fuse_log(FUSE_LOG_ERR
, "unshare(CLONE_NEWPID | CLONE_NEWNS): %m\n");
2539 fuse_log(FUSE_LOG_ERR
, "fork() failed: %m\n");
2546 /* The parent waits for the child */
2548 waited
= waitpid(child
, &wstatus
, 0);
2549 } while (waited
< 0 && errno
== EINTR
&& !se
->exited
);
2551 /* We were terminated by a signal, see fuse_signals.c */
2556 if (WIFEXITED(wstatus
)) {
2557 exit(WEXITSTATUS(wstatus
));
2563 /* Send us SIGTERM when the parent thread terminates, see prctl(2) */
2564 prctl(PR_SET_PDEATHSIG
, SIGTERM
);
2567 * If the mounts have shared propagation then we want to opt out so our
2568 * mount changes don't affect the parent mount namespace.
2570 if (mount(NULL
, "/", NULL
, MS_REC
| MS_SLAVE
, NULL
) < 0) {
2571 fuse_log(FUSE_LOG_ERR
, "mount(/, MS_REC|MS_SLAVE): %m\n");
2575 /* The child must remount /proc to use the new pid namespace */
2576 if (mount("proc", "/proc", "proc",
2577 MS_NODEV
| MS_NOEXEC
| MS_NOSUID
| MS_RELATIME
, NULL
) < 0) {
2578 fuse_log(FUSE_LOG_ERR
, "mount(/proc): %m\n");
2582 /* Now we can get our /proc/self/fd directory file descriptor */
2583 lo
->proc_self_fd
= open("/proc/self/fd", O_PATH
);
2584 if (lo
->proc_self_fd
== -1) {
2585 fuse_log(FUSE_LOG_ERR
, "open(/proc/self/fd, O_PATH): %m\n");
2591 * Capture the capability state, we'll need to restore this for individual
2592 * threads later; see load_capng.
2594 static void setup_capng(void)
2596 /* Note this accesses /proc so has to happen before the sandbox */
2597 if (capng_get_caps_process()) {
2598 fuse_log(FUSE_LOG_ERR
, "capng_get_caps_process\n");
2601 pthread_mutex_init(&cap
.mutex
, NULL
);
2602 pthread_mutex_lock(&cap
.mutex
);
2603 cap
.saved
= capng_save_state();
2605 fuse_log(FUSE_LOG_ERR
, "capng_save_state\n");
2608 pthread_mutex_unlock(&cap
.mutex
);
2611 static void cleanup_capng(void)
2615 pthread_mutex_destroy(&cap
.mutex
);
2620 * Make the source directory our root so symlinks cannot escape and no other
2621 * files are accessible. Assumes unshare(CLONE_NEWNS) was already called.
2623 static void setup_mounts(const char *source
)
2628 if (mount(source
, source
, NULL
, MS_BIND
, NULL
) < 0) {
2629 fuse_log(FUSE_LOG_ERR
, "mount(%s, %s, MS_BIND): %m\n", source
, source
);
2633 /* This magic is based on lxc's lxc_pivot_root() */
2634 oldroot
= open("/", O_DIRECTORY
| O_RDONLY
| O_CLOEXEC
);
2636 fuse_log(FUSE_LOG_ERR
, "open(/): %m\n");
2640 newroot
= open(source
, O_DIRECTORY
| O_RDONLY
| O_CLOEXEC
);
2642 fuse_log(FUSE_LOG_ERR
, "open(%s): %m\n", source
);
2646 if (fchdir(newroot
) < 0) {
2647 fuse_log(FUSE_LOG_ERR
, "fchdir(newroot): %m\n");
2651 if (syscall(__NR_pivot_root
, ".", ".") < 0) {
2652 fuse_log(FUSE_LOG_ERR
, "pivot_root(., .): %m\n");
2656 if (fchdir(oldroot
) < 0) {
2657 fuse_log(FUSE_LOG_ERR
, "fchdir(oldroot): %m\n");
2661 if (mount("", ".", "", MS_SLAVE
| MS_REC
, NULL
) < 0) {
2662 fuse_log(FUSE_LOG_ERR
, "mount(., MS_SLAVE | MS_REC): %m\n");
2666 if (umount2(".", MNT_DETACH
) < 0) {
2667 fuse_log(FUSE_LOG_ERR
, "umount2(., MNT_DETACH): %m\n");
2671 if (fchdir(newroot
) < 0) {
2672 fuse_log(FUSE_LOG_ERR
, "fchdir(newroot): %m\n");
2681 * Lock down this process to prevent access to other processes or files outside
2682 * source directory. This reduces the impact of arbitrary code execution bugs.
2684 static void setup_sandbox(struct lo_data
*lo
, struct fuse_session
*se
,
2687 setup_namespaces(lo
, se
);
2688 setup_mounts(lo
->source
);
2689 setup_seccomp(enable_syslog
);
2692 /* Raise the maximum number of open file descriptors */
2693 static void setup_nofile_rlimit(void)
2695 const rlim_t max_fds
= 1000000;
2698 if (getrlimit(RLIMIT_NOFILE
, &rlim
) < 0) {
2699 fuse_log(FUSE_LOG_ERR
, "getrlimit(RLIMIT_NOFILE): %m\n");
2703 if (rlim
.rlim_cur
>= max_fds
) {
2704 return; /* nothing to do */
2707 rlim
.rlim_cur
= max_fds
;
2708 rlim
.rlim_max
= max_fds
;
2710 if (setrlimit(RLIMIT_NOFILE
, &rlim
) < 0) {
2711 /* Ignore SELinux denials */
2712 if (errno
== EPERM
) {
2716 fuse_log(FUSE_LOG_ERR
, "setrlimit(RLIMIT_NOFILE): %m\n");
2721 static void log_func(enum fuse_log_level level
, const char *fmt
, va_list ap
)
2723 g_autofree
char *localfmt
= NULL
;
2725 if (current_log_level
< level
) {
2729 if (current_log_level
== FUSE_LOG_DEBUG
) {
2731 localfmt
= g_strdup_printf("[%" PRId64
"] [ID: %08ld] %s",
2732 get_clock(), syscall(__NR_gettid
), fmt
);
2734 localfmt
= g_strdup_printf("[ID: %08ld] %s", syscall(__NR_gettid
),
2741 int priority
= LOG_ERR
;
2743 case FUSE_LOG_EMERG
:
2744 priority
= LOG_EMERG
;
2746 case FUSE_LOG_ALERT
:
2747 priority
= LOG_ALERT
;
2750 priority
= LOG_CRIT
;
2755 case FUSE_LOG_WARNING
:
2756 priority
= LOG_WARNING
;
2758 case FUSE_LOG_NOTICE
:
2759 priority
= LOG_NOTICE
;
2762 priority
= LOG_INFO
;
2764 case FUSE_LOG_DEBUG
:
2765 priority
= LOG_DEBUG
;
2768 vsyslog(priority
, fmt
, ap
);
2770 vfprintf(stderr
, fmt
, ap
);
2774 static void setup_root(struct lo_data
*lo
, struct lo_inode
*root
)
2779 fd
= open("/", O_PATH
);
2781 fuse_log(FUSE_LOG_ERR
, "open(%s, O_PATH): %m\n", lo
->source
);
2785 res
= fstatat(fd
, "", &stat
, AT_EMPTY_PATH
| AT_SYMLINK_NOFOLLOW
);
2787 fuse_log(FUSE_LOG_ERR
, "fstatat(%s): %m\n", lo
->source
);
2791 root
->is_symlink
= false;
2793 root
->key
.ino
= stat
.st_ino
;
2794 root
->key
.dev
= stat
.st_dev
;
2796 g_atomic_int_set(&root
->refcount
, 2);
2799 static guint
lo_key_hash(gconstpointer key
)
2801 const struct lo_key
*lkey
= key
;
2803 return (guint
)lkey
->ino
+ (guint
)lkey
->dev
;
2806 static gboolean
lo_key_equal(gconstpointer a
, gconstpointer b
)
2808 const struct lo_key
*la
= a
;
2809 const struct lo_key
*lb
= b
;
2811 return la
->ino
== lb
->ino
&& la
->dev
== lb
->dev
;
2814 static void fuse_lo_data_cleanup(struct lo_data
*lo
)
2817 g_hash_table_destroy(lo
->inodes
);
2819 lo_map_destroy(&lo
->fd_map
);
2820 lo_map_destroy(&lo
->dirp_map
);
2821 lo_map_destroy(&lo
->ino_map
);
2823 if (lo
->proc_self_fd
>= 0) {
2824 close(lo
->proc_self_fd
);
2827 if (lo
->root
.fd
>= 0) {
2834 int main(int argc
, char *argv
[])
2836 struct fuse_args args
= FUSE_ARGS_INIT(argc
, argv
);
2837 struct fuse_session
*se
;
2838 struct fuse_cmdline_opts opts
;
2839 struct lo_data lo
= {
2845 struct lo_map_elem
*root_elem
;
2848 /* Don't mask creation mode, kernel already did that */
2851 pthread_mutex_init(&lo
.mutex
, NULL
);
2852 lo
.inodes
= g_hash_table_new(lo_key_hash
, lo_key_equal
);
2854 lo
.root
.fuse_ino
= FUSE_ROOT_ID
;
2855 lo
.cache
= CACHE_AUTO
;
2858 * Set up the ino map like this:
2859 * [0] Reserved (will not be used)
2862 lo_map_init(&lo
.ino_map
);
2863 lo_map_reserve(&lo
.ino_map
, 0)->in_use
= false;
2864 root_elem
= lo_map_reserve(&lo
.ino_map
, lo
.root
.fuse_ino
);
2865 root_elem
->inode
= &lo
.root
;
2867 lo_map_init(&lo
.dirp_map
);
2868 lo_map_init(&lo
.fd_map
);
2870 if (fuse_parse_cmdline(&args
, &opts
) != 0) {
2873 fuse_set_log_func(log_func
);
2874 use_syslog
= opts
.syslog
;
2876 openlog("virtiofsd", LOG_PID
, LOG_DAEMON
);
2879 if (opts
.show_help
) {
2880 printf("usage: %s [options]\n\n", argv
[0]);
2881 fuse_cmdline_help();
2882 printf(" -o source=PATH shared directory tree\n");
2883 fuse_lowlevel_help();
2886 } else if (opts
.show_version
) {
2887 fuse_lowlevel_version();
2890 } else if (opts
.print_capabilities
) {
2891 print_capabilities();
2896 if (fuse_opt_parse(&args
, &lo
, lo_opts
, NULL
) == -1) {
2901 * log_level is 0 if not configured via cmd options (0 is LOG_EMERG,
2902 * and we don't use this log level).
2904 if (opts
.log_level
!= 0) {
2905 current_log_level
= opts
.log_level
;
2907 lo
.debug
= opts
.debug
;
2909 current_log_level
= FUSE_LOG_DEBUG
;
2915 res
= lstat(lo
.source
, &stat
);
2917 fuse_log(FUSE_LOG_ERR
, "failed to stat source (\"%s\"): %m\n",
2921 if (!S_ISDIR(stat
.st_mode
)) {
2922 fuse_log(FUSE_LOG_ERR
, "source is not a directory\n");
2926 lo
.source
= strdup("/");
2928 if (!lo
.timeout_set
) {
2939 lo
.timeout
= 86400.0;
2942 } else if (lo
.timeout
< 0) {
2943 fuse_log(FUSE_LOG_ERR
, "timeout is negative (%lf)\n", lo
.timeout
);
2947 se
= fuse_session_new(&args
, &lo_oper
, sizeof(lo_oper
), &lo
);
2952 if (fuse_set_signal_handlers(se
) != 0) {
2956 if (fuse_session_mount(se
) != 0) {
2960 fuse_daemonize(opts
.foreground
);
2962 setup_nofile_rlimit();
2964 /* Must be before sandbox since it wants /proc */
2967 setup_sandbox(&lo
, se
, opts
.syslog
);
2969 setup_root(&lo
, &lo
.root
);
2970 /* Block until ctrl+c or fusermount -u */
2971 ret
= virtio_loop(se
);
2973 fuse_session_unmount(se
);
2976 fuse_remove_signal_handlers(se
);
2978 fuse_session_destroy(se
);
2980 fuse_opt_free_args(&args
);
2982 fuse_lo_data_cleanup(&lo
);