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
;
152 int readdirplus_clear
;
153 struct lo_inode root
;
154 GHashTable
*inodes
; /* protected by lo->mutex */
155 struct lo_map ino_map
; /* protected by lo->mutex */
156 struct lo_map dirp_map
; /* protected by lo->mutex */
157 struct lo_map fd_map
; /* protected by lo->mutex */
159 /* An O_PATH file descriptor to /proc/self/fd/ */
163 static const struct fuse_opt lo_opts
[] = {
164 { "writeback", offsetof(struct lo_data
, writeback
), 1 },
165 { "no_writeback", offsetof(struct lo_data
, writeback
), 0 },
166 { "source=%s", offsetof(struct lo_data
, source
), 0 },
167 { "flock", offsetof(struct lo_data
, flock
), 1 },
168 { "no_flock", offsetof(struct lo_data
, flock
), 0 },
169 { "posix_lock", offsetof(struct lo_data
, posix_lock
), 1 },
170 { "no_posix_lock", offsetof(struct lo_data
, posix_lock
), 0 },
171 { "xattr", offsetof(struct lo_data
, xattr
), 1 },
172 { "no_xattr", offsetof(struct lo_data
, xattr
), 0 },
173 { "timeout=%lf", offsetof(struct lo_data
, timeout
), 0 },
174 { "timeout=", offsetof(struct lo_data
, timeout_set
), 1 },
175 { "cache=none", offsetof(struct lo_data
, cache
), CACHE_NONE
},
176 { "cache=auto", offsetof(struct lo_data
, cache
), CACHE_AUTO
},
177 { "cache=always", offsetof(struct lo_data
, cache
), CACHE_ALWAYS
},
178 { "readdirplus", offsetof(struct lo_data
, readdirplus_set
), 1 },
179 { "no_readdirplus", offsetof(struct lo_data
, readdirplus_clear
), 1 },
182 static bool use_syslog
= false;
183 static int current_log_level
;
184 static void unref_inode_lolocked(struct lo_data
*lo
, struct lo_inode
*inode
,
188 pthread_mutex_t mutex
;
191 /* That we loaded cap-ng in the current thread from the saved */
192 static __thread
bool cap_loaded
= 0;
194 static struct lo_inode
*lo_find(struct lo_data
*lo
, struct stat
*st
);
196 static int is_dot_or_dotdot(const char *name
)
198 return name
[0] == '.' &&
199 (name
[1] == '\0' || (name
[1] == '.' && name
[2] == '\0'));
202 /* Is `path` a single path component that is not "." or ".."? */
203 static int is_safe_path_component(const char *path
)
205 if (strchr(path
, '/')) {
209 return !is_dot_or_dotdot(path
);
212 static struct lo_data
*lo_data(fuse_req_t req
)
214 return (struct lo_data
*)fuse_req_userdata(req
);
218 * Load capng's state from our saved state if the current thread
219 * hadn't previously been loaded.
220 * returns 0 on success
222 static int load_capng(void)
225 pthread_mutex_lock(&cap
.mutex
);
226 capng_restore_state(&cap
.saved
);
228 * restore_state free's the saved copy
231 cap
.saved
= capng_save_state();
233 pthread_mutex_unlock(&cap
.mutex
);
234 fuse_log(FUSE_LOG_ERR
, "capng_save_state (thread)\n");
237 pthread_mutex_unlock(&cap
.mutex
);
240 * We want to use the loaded state for our pid,
243 capng_setpid(syscall(SYS_gettid
));
250 * Helpers for dropping and regaining effective capabilities. Returns 0
251 * on success, error otherwise
253 static int drop_effective_cap(const char *cap_name
, bool *cap_dropped
)
257 cap
= capng_name_to_capability(cap_name
);
260 fuse_log(FUSE_LOG_ERR
, "capng_name_to_capability(%s) failed:%s\n",
261 cap_name
, strerror(errno
));
267 fuse_log(FUSE_LOG_ERR
, "load_capng() failed\n");
271 /* We dont have this capability in effective set already. */
272 if (!capng_have_capability(CAPNG_EFFECTIVE
, cap
)) {
277 if (capng_update(CAPNG_DROP
, CAPNG_EFFECTIVE
, cap
)) {
279 fuse_log(FUSE_LOG_ERR
, "capng_update(DROP,) failed\n");
283 if (capng_apply(CAPNG_SELECT_CAPS
)) {
285 fuse_log(FUSE_LOG_ERR
, "drop:capng_apply() failed\n");
298 static int gain_effective_cap(const char *cap_name
)
303 cap
= capng_name_to_capability(cap_name
);
306 fuse_log(FUSE_LOG_ERR
, "capng_name_to_capability(%s) failed:%s\n",
307 cap_name
, strerror(errno
));
313 fuse_log(FUSE_LOG_ERR
, "load_capng() failed\n");
317 if (capng_update(CAPNG_ADD
, CAPNG_EFFECTIVE
, cap
)) {
319 fuse_log(FUSE_LOG_ERR
, "capng_update(ADD,) failed\n");
323 if (capng_apply(CAPNG_SELECT_CAPS
)) {
325 fuse_log(FUSE_LOG_ERR
, "gain:capng_apply() failed\n");
334 static void lo_map_init(struct lo_map
*map
)
341 static void lo_map_destroy(struct lo_map
*map
)
346 static int lo_map_grow(struct lo_map
*map
, size_t new_nelems
)
348 struct lo_map_elem
*new_elems
;
351 if (new_nelems
<= map
->nelems
) {
355 new_elems
= realloc(map
->elems
, sizeof(map
->elems
[0]) * new_nelems
);
360 for (i
= map
->nelems
; i
< new_nelems
; i
++) {
361 new_elems
[i
].freelist
= i
+ 1;
362 new_elems
[i
].in_use
= false;
364 new_elems
[new_nelems
- 1].freelist
= -1;
366 map
->elems
= new_elems
;
367 map
->freelist
= map
->nelems
;
368 map
->nelems
= new_nelems
;
372 static struct lo_map_elem
*lo_map_alloc_elem(struct lo_map
*map
)
374 struct lo_map_elem
*elem
;
376 if (map
->freelist
== -1 && !lo_map_grow(map
, map
->nelems
+ 256)) {
380 elem
= &map
->elems
[map
->freelist
];
381 map
->freelist
= elem
->freelist
;
388 static struct lo_map_elem
*lo_map_reserve(struct lo_map
*map
, size_t key
)
392 if (!lo_map_grow(map
, key
+ 1)) {
396 for (prev
= &map
->freelist
; *prev
!= -1;
397 prev
= &map
->elems
[*prev
].freelist
) {
399 struct lo_map_elem
*elem
= &map
->elems
[key
];
401 *prev
= elem
->freelist
;
409 static struct lo_map_elem
*lo_map_get(struct lo_map
*map
, size_t key
)
411 if (key
>= map
->nelems
) {
414 if (!map
->elems
[key
].in_use
) {
417 return &map
->elems
[key
];
420 static void lo_map_remove(struct lo_map
*map
, size_t key
)
422 struct lo_map_elem
*elem
;
424 if (key
>= map
->nelems
) {
428 elem
= &map
->elems
[key
];
433 elem
->in_use
= false;
435 elem
->freelist
= map
->freelist
;
439 /* Assumes lo->mutex is held */
440 static ssize_t
lo_add_fd_mapping(fuse_req_t req
, int fd
)
442 struct lo_map_elem
*elem
;
444 elem
= lo_map_alloc_elem(&lo_data(req
)->fd_map
);
450 return elem
- lo_data(req
)->fd_map
.elems
;
453 /* Assumes lo->mutex is held */
454 static ssize_t
lo_add_dirp_mapping(fuse_req_t req
, struct lo_dirp
*dirp
)
456 struct lo_map_elem
*elem
;
458 elem
= lo_map_alloc_elem(&lo_data(req
)->dirp_map
);
464 return elem
- lo_data(req
)->dirp_map
.elems
;
467 /* Assumes lo->mutex is held */
468 static ssize_t
lo_add_inode_mapping(fuse_req_t req
, struct lo_inode
*inode
)
470 struct lo_map_elem
*elem
;
472 elem
= lo_map_alloc_elem(&lo_data(req
)->ino_map
);
478 return elem
- lo_data(req
)->ino_map
.elems
;
481 static void lo_inode_put(struct lo_data
*lo
, struct lo_inode
**inodep
)
483 struct lo_inode
*inode
= *inodep
;
491 if (g_atomic_int_dec_and_test(&inode
->refcount
)) {
497 /* Caller must release refcount using lo_inode_put() */
498 static struct lo_inode
*lo_inode(fuse_req_t req
, fuse_ino_t ino
)
500 struct lo_data
*lo
= lo_data(req
);
501 struct lo_map_elem
*elem
;
503 pthread_mutex_lock(&lo
->mutex
);
504 elem
= lo_map_get(&lo
->ino_map
, ino
);
506 g_atomic_int_inc(&elem
->inode
->refcount
);
508 pthread_mutex_unlock(&lo
->mutex
);
518 * TODO Remove this helper and force callers to hold an inode refcount until
519 * they are done with the fd. This will be done in a later patch to make
522 static int lo_fd(fuse_req_t req
, fuse_ino_t ino
)
524 struct lo_inode
*inode
= lo_inode(req
, ino
);
532 lo_inode_put(lo_data(req
), &inode
);
536 static void lo_init(void *userdata
, struct fuse_conn_info
*conn
)
538 struct lo_data
*lo
= (struct lo_data
*)userdata
;
540 if (conn
->capable
& FUSE_CAP_EXPORT_SUPPORT
) {
541 conn
->want
|= FUSE_CAP_EXPORT_SUPPORT
;
544 if (lo
->writeback
&& conn
->capable
& FUSE_CAP_WRITEBACK_CACHE
) {
545 fuse_log(FUSE_LOG_DEBUG
, "lo_init: activating writeback\n");
546 conn
->want
|= FUSE_CAP_WRITEBACK_CACHE
;
548 if (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
;
553 fuse_log(FUSE_LOG_DEBUG
, "lo_init: disabling flock locks\n");
554 conn
->want
&= ~FUSE_CAP_FLOCK_LOCKS
;
558 if (conn
->capable
& FUSE_CAP_POSIX_LOCKS
) {
559 if (lo
->posix_lock
) {
560 fuse_log(FUSE_LOG_DEBUG
, "lo_init: activating posix locks\n");
561 conn
->want
|= FUSE_CAP_POSIX_LOCKS
;
563 fuse_log(FUSE_LOG_DEBUG
, "lo_init: disabling posix locks\n");
564 conn
->want
&= ~FUSE_CAP_POSIX_LOCKS
;
568 if ((lo
->cache
== CACHE_NONE
&& !lo
->readdirplus_set
) ||
569 lo
->readdirplus_clear
) {
570 fuse_log(FUSE_LOG_DEBUG
, "lo_init: disabling readdirplus\n");
571 conn
->want
&= ~FUSE_CAP_READDIRPLUS
;
575 static void lo_getattr(fuse_req_t req
, fuse_ino_t ino
,
576 struct fuse_file_info
*fi
)
580 struct lo_data
*lo
= lo_data(req
);
585 fstatat(lo_fd(req
, ino
), "", &buf
, AT_EMPTY_PATH
| AT_SYMLINK_NOFOLLOW
);
587 return (void)fuse_reply_err(req
, errno
);
590 fuse_reply_attr(req
, &buf
, lo
->timeout
);
593 static int lo_fi_fd(fuse_req_t req
, struct fuse_file_info
*fi
)
595 struct lo_data
*lo
= lo_data(req
);
596 struct lo_map_elem
*elem
;
598 pthread_mutex_lock(&lo
->mutex
);
599 elem
= lo_map_get(&lo
->fd_map
, fi
->fh
);
600 pthread_mutex_unlock(&lo
->mutex
);
609 static void lo_setattr(fuse_req_t req
, fuse_ino_t ino
, struct stat
*attr
,
610 int valid
, struct fuse_file_info
*fi
)
614 struct lo_data
*lo
= lo_data(req
);
615 struct lo_inode
*inode
;
620 inode
= lo_inode(req
, ino
);
622 fuse_reply_err(req
, EBADF
);
628 /* If fi->fh is invalid we'll report EBADF later */
630 fd
= lo_fi_fd(req
, fi
);
633 if (valid
& FUSE_SET_ATTR_MODE
) {
635 res
= fchmod(fd
, attr
->st_mode
);
637 sprintf(procname
, "%i", ifd
);
638 res
= fchmodat(lo
->proc_self_fd
, procname
, attr
->st_mode
, 0);
644 if (valid
& (FUSE_SET_ATTR_UID
| FUSE_SET_ATTR_GID
)) {
645 uid_t uid
= (valid
& FUSE_SET_ATTR_UID
) ? attr
->st_uid
: (uid_t
)-1;
646 gid_t gid
= (valid
& FUSE_SET_ATTR_GID
) ? attr
->st_gid
: (gid_t
)-1;
648 res
= fchownat(ifd
, "", uid
, gid
, AT_EMPTY_PATH
| AT_SYMLINK_NOFOLLOW
);
653 if (valid
& FUSE_SET_ATTR_SIZE
) {
659 sprintf(procname
, "%i", ifd
);
660 truncfd
= openat(lo
->proc_self_fd
, procname
, O_RDWR
);
666 res
= ftruncate(truncfd
, attr
->st_size
);
676 if (valid
& (FUSE_SET_ATTR_ATIME
| FUSE_SET_ATTR_MTIME
)) {
677 struct timespec tv
[2];
681 tv
[0].tv_nsec
= UTIME_OMIT
;
682 tv
[1].tv_nsec
= UTIME_OMIT
;
684 if (valid
& FUSE_SET_ATTR_ATIME_NOW
) {
685 tv
[0].tv_nsec
= UTIME_NOW
;
686 } else if (valid
& FUSE_SET_ATTR_ATIME
) {
687 tv
[0] = attr
->st_atim
;
690 if (valid
& FUSE_SET_ATTR_MTIME_NOW
) {
691 tv
[1].tv_nsec
= UTIME_NOW
;
692 } else if (valid
& FUSE_SET_ATTR_MTIME
) {
693 tv
[1] = attr
->st_mtim
;
697 res
= futimens(fd
, tv
);
699 sprintf(procname
, "%i", inode
->fd
);
700 res
= utimensat(lo
->proc_self_fd
, procname
, tv
, 0);
706 lo_inode_put(lo
, &inode
);
708 return lo_getattr(req
, ino
, fi
);
712 lo_inode_put(lo
, &inode
);
713 fuse_reply_err(req
, saverr
);
716 static struct lo_inode
*lo_find(struct lo_data
*lo
, struct stat
*st
)
719 struct lo_key key
= {
724 pthread_mutex_lock(&lo
->mutex
);
725 p
= g_hash_table_lookup(lo
->inodes
, &key
);
727 assert(p
->nlookup
> 0);
729 g_atomic_int_inc(&p
->refcount
);
731 pthread_mutex_unlock(&lo
->mutex
);
736 /* value_destroy_func for posix_locks GHashTable */
737 static void posix_locks_value_destroy(gpointer data
)
739 struct lo_inode_plock
*plock
= data
;
742 * We had used open() for locks and had only one fd. So
743 * closing this fd should release all OFD locks.
750 * Increments nlookup and caller must release refcount using
751 * lo_inode_put(&parent).
753 static int lo_do_lookup(fuse_req_t req
, fuse_ino_t parent
, const char *name
,
754 struct fuse_entry_param
*e
)
759 struct lo_data
*lo
= lo_data(req
);
760 struct lo_inode
*inode
= NULL
;
761 struct lo_inode
*dir
= lo_inode(req
, parent
);
764 * name_to_handle_at() and open_by_handle_at() can reach here with fuse
765 * mount point in guest, but we don't have its inode info in the
772 memset(e
, 0, sizeof(*e
));
773 e
->attr_timeout
= lo
->timeout
;
774 e
->entry_timeout
= lo
->timeout
;
776 /* Do not allow escaping root directory */
777 if (dir
== &lo
->root
&& strcmp(name
, "..") == 0) {
781 newfd
= openat(dir
->fd
, name
, O_PATH
| O_NOFOLLOW
);
786 res
= fstatat(newfd
, "", &e
->attr
, AT_EMPTY_PATH
| AT_SYMLINK_NOFOLLOW
);
791 inode
= lo_find(lo
, &e
->attr
);
795 inode
= calloc(1, sizeof(struct lo_inode
));
800 /* cache only filetype */
801 inode
->filetype
= (e
->attr
.st_mode
& S_IFMT
);
804 * One for the caller and one for nlookup (released in
805 * unref_inode_lolocked())
807 g_atomic_int_set(&inode
->refcount
, 2);
811 inode
->key
.ino
= e
->attr
.st_ino
;
812 inode
->key
.dev
= e
->attr
.st_dev
;
813 pthread_mutex_init(&inode
->plock_mutex
, NULL
);
814 inode
->posix_locks
= g_hash_table_new_full(
815 g_direct_hash
, g_direct_equal
, NULL
, posix_locks_value_destroy
);
817 pthread_mutex_lock(&lo
->mutex
);
818 inode
->fuse_ino
= lo_add_inode_mapping(req
, inode
);
819 g_hash_table_insert(lo
->inodes
, &inode
->key
, inode
);
820 pthread_mutex_unlock(&lo
->mutex
);
822 e
->ino
= inode
->fuse_ino
;
823 lo_inode_put(lo
, &inode
);
824 lo_inode_put(lo
, &dir
);
826 fuse_log(FUSE_LOG_DEBUG
, " %lli/%s -> %lli\n", (unsigned long long)parent
,
827 name
, (unsigned long long)e
->ino
);
836 lo_inode_put(lo
, &inode
);
837 lo_inode_put(lo
, &dir
);
841 static void lo_lookup(fuse_req_t req
, fuse_ino_t parent
, const char *name
)
843 struct fuse_entry_param e
;
846 fuse_log(FUSE_LOG_DEBUG
, "lo_lookup(parent=%" PRIu64
", name=%s)\n", parent
,
850 * Don't use is_safe_path_component(), allow "." and ".." for NFS export
853 if (strchr(name
, '/')) {
854 fuse_reply_err(req
, EINVAL
);
858 err
= lo_do_lookup(req
, parent
, name
, &e
);
860 fuse_reply_err(req
, err
);
862 fuse_reply_entry(req
, &e
);
867 * On some archs, setres*id is limited to 2^16 but they
868 * provide setres*id32 variants that allow 2^32.
869 * Others just let setres*id do 2^32 anyway.
871 #ifdef SYS_setresgid32
872 #define OURSYS_setresgid SYS_setresgid32
874 #define OURSYS_setresgid SYS_setresgid
877 #ifdef SYS_setresuid32
878 #define OURSYS_setresuid SYS_setresuid32
880 #define OURSYS_setresuid SYS_setresuid
884 * Change to uid/gid of caller so that file is created with
885 * ownership of caller.
886 * TODO: What about selinux context?
888 static int lo_change_cred(fuse_req_t req
, struct lo_cred
*old
)
892 old
->euid
= geteuid();
893 old
->egid
= getegid();
895 res
= syscall(OURSYS_setresgid
, -1, fuse_req_ctx(req
)->gid
, -1);
900 res
= syscall(OURSYS_setresuid
, -1, fuse_req_ctx(req
)->uid
, -1);
902 int errno_save
= errno
;
904 syscall(OURSYS_setresgid
, -1, old
->egid
, -1);
911 /* Regain Privileges */
912 static void lo_restore_cred(struct lo_cred
*old
)
916 res
= syscall(OURSYS_setresuid
, -1, old
->euid
, -1);
918 fuse_log(FUSE_LOG_ERR
, "seteuid(%u): %m\n", old
->euid
);
922 res
= syscall(OURSYS_setresgid
, -1, old
->egid
, -1);
924 fuse_log(FUSE_LOG_ERR
, "setegid(%u): %m\n", old
->egid
);
929 static void lo_mknod_symlink(fuse_req_t req
, fuse_ino_t parent
,
930 const char *name
, mode_t mode
, dev_t rdev
,
935 struct lo_data
*lo
= lo_data(req
);
936 struct lo_inode
*dir
;
937 struct fuse_entry_param e
;
938 struct lo_cred old
= {};
940 if (!is_safe_path_component(name
)) {
941 fuse_reply_err(req
, EINVAL
);
945 dir
= lo_inode(req
, parent
);
947 fuse_reply_err(req
, EBADF
);
951 saverr
= lo_change_cred(req
, &old
);
956 res
= mknod_wrapper(dir
->fd
, name
, link
, mode
, rdev
);
960 lo_restore_cred(&old
);
966 saverr
= lo_do_lookup(req
, parent
, name
, &e
);
971 fuse_log(FUSE_LOG_DEBUG
, " %lli/%s -> %lli\n", (unsigned long long)parent
,
972 name
, (unsigned long long)e
.ino
);
974 fuse_reply_entry(req
, &e
);
975 lo_inode_put(lo
, &dir
);
979 lo_inode_put(lo
, &dir
);
980 fuse_reply_err(req
, saverr
);
983 static void lo_mknod(fuse_req_t req
, fuse_ino_t parent
, const char *name
,
984 mode_t mode
, dev_t rdev
)
986 lo_mknod_symlink(req
, parent
, name
, mode
, rdev
, NULL
);
989 static void lo_mkdir(fuse_req_t req
, fuse_ino_t parent
, const char *name
,
992 lo_mknod_symlink(req
, parent
, name
, S_IFDIR
| mode
, 0, NULL
);
995 static void lo_symlink(fuse_req_t req
, const char *link
, fuse_ino_t parent
,
998 lo_mknod_symlink(req
, parent
, name
, S_IFLNK
, 0, link
);
1001 static void lo_link(fuse_req_t req
, fuse_ino_t ino
, fuse_ino_t parent
,
1005 struct lo_data
*lo
= lo_data(req
);
1006 struct lo_inode
*parent_inode
;
1007 struct lo_inode
*inode
;
1008 struct fuse_entry_param e
;
1012 if (!is_safe_path_component(name
)) {
1013 fuse_reply_err(req
, EINVAL
);
1017 parent_inode
= lo_inode(req
, parent
);
1018 inode
= lo_inode(req
, ino
);
1019 if (!parent_inode
|| !inode
) {
1024 memset(&e
, 0, sizeof(struct fuse_entry_param
));
1025 e
.attr_timeout
= lo
->timeout
;
1026 e
.entry_timeout
= lo
->timeout
;
1028 sprintf(procname
, "%i", inode
->fd
);
1029 res
= linkat(lo
->proc_self_fd
, procname
, parent_inode
->fd
, name
,
1035 res
= fstatat(inode
->fd
, "", &e
.attr
, AT_EMPTY_PATH
| AT_SYMLINK_NOFOLLOW
);
1040 pthread_mutex_lock(&lo
->mutex
);
1042 pthread_mutex_unlock(&lo
->mutex
);
1043 e
.ino
= inode
->fuse_ino
;
1045 fuse_log(FUSE_LOG_DEBUG
, " %lli/%s -> %lli\n", (unsigned long long)parent
,
1046 name
, (unsigned long long)e
.ino
);
1048 fuse_reply_entry(req
, &e
);
1049 lo_inode_put(lo
, &parent_inode
);
1050 lo_inode_put(lo
, &inode
);
1055 lo_inode_put(lo
, &parent_inode
);
1056 lo_inode_put(lo
, &inode
);
1057 fuse_reply_err(req
, saverr
);
1060 /* Increments nlookup and caller must release refcount using lo_inode_put() */
1061 static struct lo_inode
*lookup_name(fuse_req_t req
, fuse_ino_t parent
,
1067 res
= fstatat(lo_fd(req
, parent
), name
, &attr
,
1068 AT_EMPTY_PATH
| AT_SYMLINK_NOFOLLOW
);
1073 return lo_find(lo_data(req
), &attr
);
1076 static void lo_rmdir(fuse_req_t req
, fuse_ino_t parent
, const char *name
)
1079 struct lo_inode
*inode
;
1080 struct lo_data
*lo
= lo_data(req
);
1082 if (!is_safe_path_component(name
)) {
1083 fuse_reply_err(req
, EINVAL
);
1087 inode
= lookup_name(req
, parent
, name
);
1089 fuse_reply_err(req
, EIO
);
1093 res
= unlinkat(lo_fd(req
, parent
), name
, AT_REMOVEDIR
);
1095 fuse_reply_err(req
, res
== -1 ? errno
: 0);
1096 unref_inode_lolocked(lo
, inode
, 1);
1097 lo_inode_put(lo
, &inode
);
1100 static void lo_rename(fuse_req_t req
, fuse_ino_t parent
, const char *name
,
1101 fuse_ino_t newparent
, const char *newname
,
1105 struct lo_inode
*parent_inode
;
1106 struct lo_inode
*newparent_inode
;
1107 struct lo_inode
*oldinode
= NULL
;
1108 struct lo_inode
*newinode
= NULL
;
1109 struct lo_data
*lo
= lo_data(req
);
1111 if (!is_safe_path_component(name
) || !is_safe_path_component(newname
)) {
1112 fuse_reply_err(req
, EINVAL
);
1116 parent_inode
= lo_inode(req
, parent
);
1117 newparent_inode
= lo_inode(req
, newparent
);
1118 if (!parent_inode
|| !newparent_inode
) {
1119 fuse_reply_err(req
, EBADF
);
1123 oldinode
= lookup_name(req
, parent
, name
);
1124 newinode
= lookup_name(req
, newparent
, newname
);
1127 fuse_reply_err(req
, EIO
);
1132 #ifndef SYS_renameat2
1133 fuse_reply_err(req
, EINVAL
);
1135 res
= syscall(SYS_renameat2
, parent_inode
->fd
, name
,
1136 newparent_inode
->fd
, newname
, flags
);
1137 if (res
== -1 && errno
== ENOSYS
) {
1138 fuse_reply_err(req
, EINVAL
);
1140 fuse_reply_err(req
, res
== -1 ? errno
: 0);
1146 res
= renameat(parent_inode
->fd
, name
, newparent_inode
->fd
, newname
);
1148 fuse_reply_err(req
, res
== -1 ? errno
: 0);
1150 unref_inode_lolocked(lo
, oldinode
, 1);
1151 unref_inode_lolocked(lo
, newinode
, 1);
1152 lo_inode_put(lo
, &oldinode
);
1153 lo_inode_put(lo
, &newinode
);
1154 lo_inode_put(lo
, &parent_inode
);
1155 lo_inode_put(lo
, &newparent_inode
);
1158 static void lo_unlink(fuse_req_t req
, fuse_ino_t parent
, const char *name
)
1161 struct lo_inode
*inode
;
1162 struct lo_data
*lo
= lo_data(req
);
1164 if (!is_safe_path_component(name
)) {
1165 fuse_reply_err(req
, EINVAL
);
1169 inode
= lookup_name(req
, parent
, name
);
1171 fuse_reply_err(req
, EIO
);
1175 res
= unlinkat(lo_fd(req
, parent
), name
, 0);
1177 fuse_reply_err(req
, res
== -1 ? errno
: 0);
1178 unref_inode_lolocked(lo
, inode
, 1);
1179 lo_inode_put(lo
, &inode
);
1182 /* To be called with lo->mutex held */
1183 static void unref_inode(struct lo_data
*lo
, struct lo_inode
*inode
, uint64_t n
)
1189 assert(inode
->nlookup
>= n
);
1190 inode
->nlookup
-= n
;
1191 if (!inode
->nlookup
) {
1192 lo_map_remove(&lo
->ino_map
, inode
->fuse_ino
);
1193 g_hash_table_remove(lo
->inodes
, &inode
->key
);
1194 if (g_hash_table_size(inode
->posix_locks
)) {
1195 fuse_log(FUSE_LOG_WARNING
, "Hash table is not empty\n");
1197 g_hash_table_destroy(inode
->posix_locks
);
1198 pthread_mutex_destroy(&inode
->plock_mutex
);
1200 /* Drop our refcount from lo_do_lookup() */
1201 lo_inode_put(lo
, &inode
);
1205 static void unref_inode_lolocked(struct lo_data
*lo
, struct lo_inode
*inode
,
1212 pthread_mutex_lock(&lo
->mutex
);
1213 unref_inode(lo
, inode
, n
);
1214 pthread_mutex_unlock(&lo
->mutex
);
1217 static void lo_forget_one(fuse_req_t req
, fuse_ino_t ino
, uint64_t nlookup
)
1219 struct lo_data
*lo
= lo_data(req
);
1220 struct lo_inode
*inode
;
1222 inode
= lo_inode(req
, ino
);
1227 fuse_log(FUSE_LOG_DEBUG
, " forget %lli %lli -%lli\n",
1228 (unsigned long long)ino
, (unsigned long long)inode
->nlookup
,
1229 (unsigned long long)nlookup
);
1231 unref_inode_lolocked(lo
, inode
, nlookup
);
1232 lo_inode_put(lo
, &inode
);
1235 static void lo_forget(fuse_req_t req
, fuse_ino_t ino
, uint64_t nlookup
)
1237 lo_forget_one(req
, ino
, nlookup
);
1238 fuse_reply_none(req
);
1241 static void lo_forget_multi(fuse_req_t req
, size_t count
,
1242 struct fuse_forget_data
*forgets
)
1246 for (i
= 0; i
< count
; i
++) {
1247 lo_forget_one(req
, forgets
[i
].ino
, forgets
[i
].nlookup
);
1249 fuse_reply_none(req
);
1252 static void lo_readlink(fuse_req_t req
, fuse_ino_t ino
)
1254 char buf
[PATH_MAX
+ 1];
1257 res
= readlinkat(lo_fd(req
, ino
), "", buf
, sizeof(buf
));
1259 return (void)fuse_reply_err(req
, errno
);
1262 if (res
== sizeof(buf
)) {
1263 return (void)fuse_reply_err(req
, ENAMETOOLONG
);
1268 fuse_reply_readlink(req
, buf
);
1274 struct dirent
*entry
;
1278 static void lo_dirp_put(struct lo_dirp
**dp
)
1280 struct lo_dirp
*d
= *dp
;
1287 if (g_atomic_int_dec_and_test(&d
->refcount
)) {
1293 /* Call lo_dirp_put() on the return value when no longer needed */
1294 static struct lo_dirp
*lo_dirp(fuse_req_t req
, struct fuse_file_info
*fi
)
1296 struct lo_data
*lo
= lo_data(req
);
1297 struct lo_map_elem
*elem
;
1299 pthread_mutex_lock(&lo
->mutex
);
1300 elem
= lo_map_get(&lo
->dirp_map
, fi
->fh
);
1302 g_atomic_int_inc(&elem
->dirp
->refcount
);
1304 pthread_mutex_unlock(&lo
->mutex
);
1312 static void lo_opendir(fuse_req_t req
, fuse_ino_t ino
,
1313 struct fuse_file_info
*fi
)
1316 struct lo_data
*lo
= lo_data(req
);
1321 d
= calloc(1, sizeof(struct lo_dirp
));
1326 fd
= openat(lo_fd(req
, ino
), ".", O_RDONLY
);
1331 d
->dp
= fdopendir(fd
);
1332 if (d
->dp
== NULL
) {
1339 g_atomic_int_set(&d
->refcount
, 1); /* paired with lo_releasedir() */
1340 pthread_mutex_lock(&lo
->mutex
);
1341 fh
= lo_add_dirp_mapping(req
, d
);
1342 pthread_mutex_unlock(&lo
->mutex
);
1348 if (lo
->cache
== CACHE_ALWAYS
) {
1349 fi
->cache_readdir
= 1;
1351 fuse_reply_open(req
, fi
);
1360 } else if (fd
!= -1) {
1365 fuse_reply_err(req
, error
);
1368 static void lo_do_readdir(fuse_req_t req
, fuse_ino_t ino
, size_t size
,
1369 off_t offset
, struct fuse_file_info
*fi
, int plus
)
1371 struct lo_data
*lo
= lo_data(req
);
1372 struct lo_dirp
*d
= NULL
;
1373 struct lo_inode
*dinode
;
1379 dinode
= lo_inode(req
, ino
);
1384 d
= lo_dirp(req
, fi
);
1390 buf
= calloc(1, size
);
1396 if (offset
!= d
->offset
) {
1397 seekdir(d
->dp
, offset
);
1408 d
->entry
= readdir(d
->dp
);
1410 if (errno
) { /* Error */
1413 } else { /* End of stream */
1418 nextoff
= d
->entry
->d_off
;
1419 name
= d
->entry
->d_name
;
1421 fuse_ino_t entry_ino
= 0;
1422 struct fuse_entry_param e
= (struct fuse_entry_param
){
1423 .attr
.st_ino
= d
->entry
->d_ino
,
1424 .attr
.st_mode
= d
->entry
->d_type
<< 12,
1427 /* Hide root's parent directory */
1428 if (dinode
== &lo
->root
&& strcmp(name
, "..") == 0) {
1429 e
.attr
.st_ino
= lo
->root
.key
.ino
;
1430 e
.attr
.st_mode
= DT_DIR
<< 12;
1434 if (!is_dot_or_dotdot(name
)) {
1435 err
= lo_do_lookup(req
, ino
, name
, &e
);
1442 entsize
= fuse_add_direntry_plus(req
, p
, rem
, name
, &e
, nextoff
);
1444 entsize
= fuse_add_direntry(req
, p
, rem
, name
, &e
.attr
, nextoff
);
1446 if (entsize
> rem
) {
1447 if (entry_ino
!= 0) {
1448 lo_forget_one(req
, entry_ino
, 1);
1457 d
->offset
= nextoff
;
1463 lo_inode_put(lo
, &dinode
);
1466 * If there's an error, we can only signal it if we haven't stored
1467 * any entries yet - otherwise we'd end up with wrong lookup
1468 * counts for the entries that are already in the buffer. So we
1469 * return what we've collected until that point.
1471 if (err
&& rem
== size
) {
1472 fuse_reply_err(req
, err
);
1474 fuse_reply_buf(req
, buf
, size
- rem
);
1479 static void lo_readdir(fuse_req_t req
, fuse_ino_t ino
, size_t size
,
1480 off_t offset
, struct fuse_file_info
*fi
)
1482 lo_do_readdir(req
, ino
, size
, offset
, fi
, 0);
1485 static void lo_readdirplus(fuse_req_t req
, fuse_ino_t ino
, size_t size
,
1486 off_t offset
, struct fuse_file_info
*fi
)
1488 lo_do_readdir(req
, ino
, size
, offset
, fi
, 1);
1491 static void lo_releasedir(fuse_req_t req
, fuse_ino_t ino
,
1492 struct fuse_file_info
*fi
)
1494 struct lo_data
*lo
= lo_data(req
);
1495 struct lo_map_elem
*elem
;
1500 pthread_mutex_lock(&lo
->mutex
);
1501 elem
= lo_map_get(&lo
->dirp_map
, fi
->fh
);
1503 pthread_mutex_unlock(&lo
->mutex
);
1504 fuse_reply_err(req
, EBADF
);
1509 lo_map_remove(&lo
->dirp_map
, fi
->fh
);
1510 pthread_mutex_unlock(&lo
->mutex
);
1512 lo_dirp_put(&d
); /* paired with lo_opendir() */
1514 fuse_reply_err(req
, 0);
1517 static void update_open_flags(int writeback
, struct fuse_file_info
*fi
)
1520 * With writeback cache, kernel may send read requests even
1521 * when userspace opened write-only
1523 if (writeback
&& (fi
->flags
& O_ACCMODE
) == O_WRONLY
) {
1524 fi
->flags
&= ~O_ACCMODE
;
1525 fi
->flags
|= O_RDWR
;
1529 * With writeback cache, O_APPEND is handled by the kernel.
1530 * This breaks atomicity (since the file may change in the
1531 * underlying filesystem, so that the kernel's idea of the
1532 * end of the file isn't accurate anymore). In this example,
1533 * we just accept that. A more rigorous filesystem may want
1534 * to return an error here
1536 if (writeback
&& (fi
->flags
& O_APPEND
)) {
1537 fi
->flags
&= ~O_APPEND
;
1541 * O_DIRECT in guest should not necessarily mean bypassing page
1542 * cache on host as well. If somebody needs that behavior, it
1543 * probably should be a configuration knob in daemon.
1545 fi
->flags
&= ~O_DIRECT
;
1548 static void lo_create(fuse_req_t req
, fuse_ino_t parent
, const char *name
,
1549 mode_t mode
, struct fuse_file_info
*fi
)
1552 struct lo_data
*lo
= lo_data(req
);
1553 struct lo_inode
*parent_inode
;
1554 struct fuse_entry_param e
;
1556 struct lo_cred old
= {};
1558 fuse_log(FUSE_LOG_DEBUG
, "lo_create(parent=%" PRIu64
", name=%s)\n", parent
,
1561 if (!is_safe_path_component(name
)) {
1562 fuse_reply_err(req
, EINVAL
);
1566 parent_inode
= lo_inode(req
, parent
);
1567 if (!parent_inode
) {
1568 fuse_reply_err(req
, EBADF
);
1572 err
= lo_change_cred(req
, &old
);
1577 update_open_flags(lo
->writeback
, fi
);
1579 fd
= openat(parent_inode
->fd
, name
, (fi
->flags
| O_CREAT
) & ~O_NOFOLLOW
,
1581 err
= fd
== -1 ? errno
: 0;
1582 lo_restore_cred(&old
);
1587 pthread_mutex_lock(&lo
->mutex
);
1588 fh
= lo_add_fd_mapping(req
, fd
);
1589 pthread_mutex_unlock(&lo
->mutex
);
1597 err
= lo_do_lookup(req
, parent
, name
, &e
);
1599 if (lo
->cache
== CACHE_NONE
) {
1601 } else if (lo
->cache
== CACHE_ALWAYS
) {
1606 lo_inode_put(lo
, &parent_inode
);
1609 fuse_reply_err(req
, err
);
1611 fuse_reply_create(req
, &e
, fi
);
1615 /* Should be called with inode->plock_mutex held */
1616 static struct lo_inode_plock
*lookup_create_plock_ctx(struct lo_data
*lo
,
1617 struct lo_inode
*inode
,
1618 uint64_t lock_owner
,
1619 pid_t pid
, int *err
)
1621 struct lo_inode_plock
*plock
;
1626 g_hash_table_lookup(inode
->posix_locks
, GUINT_TO_POINTER(lock_owner
));
1632 plock
= malloc(sizeof(struct lo_inode_plock
));
1638 /* Open another instance of file which can be used for ofd locks. */
1639 sprintf(procname
, "%i", inode
->fd
);
1641 /* TODO: What if file is not writable? */
1642 fd
= openat(lo
->proc_self_fd
, procname
, O_RDWR
);
1649 plock
->lock_owner
= lock_owner
;
1651 g_hash_table_insert(inode
->posix_locks
, GUINT_TO_POINTER(plock
->lock_owner
),
1656 static void lo_getlk(fuse_req_t req
, fuse_ino_t ino
, struct fuse_file_info
*fi
,
1659 struct lo_data
*lo
= lo_data(req
);
1660 struct lo_inode
*inode
;
1661 struct lo_inode_plock
*plock
;
1662 int ret
, saverr
= 0;
1664 fuse_log(FUSE_LOG_DEBUG
,
1665 "lo_getlk(ino=%" PRIu64
", flags=%d)"
1666 " owner=0x%lx, l_type=%d l_start=0x%lx"
1668 ino
, fi
->flags
, fi
->lock_owner
, lock
->l_type
, lock
->l_start
,
1671 inode
= lo_inode(req
, ino
);
1673 fuse_reply_err(req
, EBADF
);
1677 pthread_mutex_lock(&inode
->plock_mutex
);
1679 lookup_create_plock_ctx(lo
, inode
, fi
->lock_owner
, lock
->l_pid
, &ret
);
1685 ret
= fcntl(plock
->fd
, F_OFD_GETLK
, lock
);
1691 pthread_mutex_unlock(&inode
->plock_mutex
);
1692 lo_inode_put(lo
, &inode
);
1695 fuse_reply_err(req
, saverr
);
1697 fuse_reply_lock(req
, lock
);
1701 static void lo_setlk(fuse_req_t req
, fuse_ino_t ino
, struct fuse_file_info
*fi
,
1702 struct flock
*lock
, int sleep
)
1704 struct lo_data
*lo
= lo_data(req
);
1705 struct lo_inode
*inode
;
1706 struct lo_inode_plock
*plock
;
1707 int ret
, saverr
= 0;
1709 fuse_log(FUSE_LOG_DEBUG
,
1710 "lo_setlk(ino=%" PRIu64
", flags=%d)"
1711 " cmd=%d pid=%d owner=0x%lx sleep=%d l_whence=%d"
1712 " l_start=0x%lx l_len=0x%lx\n",
1713 ino
, fi
->flags
, lock
->l_type
, lock
->l_pid
, fi
->lock_owner
, sleep
,
1714 lock
->l_whence
, lock
->l_start
, lock
->l_len
);
1717 fuse_reply_err(req
, EOPNOTSUPP
);
1721 inode
= lo_inode(req
, ino
);
1723 fuse_reply_err(req
, EBADF
);
1727 pthread_mutex_lock(&inode
->plock_mutex
);
1729 lookup_create_plock_ctx(lo
, inode
, fi
->lock_owner
, lock
->l_pid
, &ret
);
1736 /* TODO: Is it alright to modify flock? */
1738 ret
= fcntl(plock
->fd
, F_OFD_SETLK
, lock
);
1744 pthread_mutex_unlock(&inode
->plock_mutex
);
1745 lo_inode_put(lo
, &inode
);
1747 fuse_reply_err(req
, saverr
);
1750 static void lo_fsyncdir(fuse_req_t req
, fuse_ino_t ino
, int datasync
,
1751 struct fuse_file_info
*fi
)
1759 d
= lo_dirp(req
, fi
);
1761 fuse_reply_err(req
, EBADF
);
1767 res
= fdatasync(fd
);
1774 fuse_reply_err(req
, res
== -1 ? errno
: 0);
1777 static void lo_open(fuse_req_t req
, fuse_ino_t ino
, struct fuse_file_info
*fi
)
1782 struct lo_data
*lo
= lo_data(req
);
1784 fuse_log(FUSE_LOG_DEBUG
, "lo_open(ino=%" PRIu64
", flags=%d)\n", ino
,
1787 update_open_flags(lo
->writeback
, fi
);
1789 sprintf(buf
, "%i", lo_fd(req
, ino
));
1790 fd
= openat(lo
->proc_self_fd
, buf
, fi
->flags
& ~O_NOFOLLOW
);
1792 return (void)fuse_reply_err(req
, errno
);
1795 pthread_mutex_lock(&lo
->mutex
);
1796 fh
= lo_add_fd_mapping(req
, fd
);
1797 pthread_mutex_unlock(&lo
->mutex
);
1800 fuse_reply_err(req
, ENOMEM
);
1805 if (lo
->cache
== CACHE_NONE
) {
1807 } else if (lo
->cache
== CACHE_ALWAYS
) {
1810 fuse_reply_open(req
, fi
);
1813 static void lo_release(fuse_req_t req
, fuse_ino_t ino
,
1814 struct fuse_file_info
*fi
)
1816 struct lo_data
*lo
= lo_data(req
);
1817 struct lo_map_elem
*elem
;
1822 pthread_mutex_lock(&lo
->mutex
);
1823 elem
= lo_map_get(&lo
->fd_map
, fi
->fh
);
1827 lo_map_remove(&lo
->fd_map
, fi
->fh
);
1829 pthread_mutex_unlock(&lo
->mutex
);
1832 fuse_reply_err(req
, 0);
1835 static void lo_flush(fuse_req_t req
, fuse_ino_t ino
, struct fuse_file_info
*fi
)
1839 struct lo_inode
*inode
;
1841 inode
= lo_inode(req
, ino
);
1843 fuse_reply_err(req
, EBADF
);
1847 /* An fd is going away. Cleanup associated posix locks */
1848 pthread_mutex_lock(&inode
->plock_mutex
);
1849 g_hash_table_remove(inode
->posix_locks
, GUINT_TO_POINTER(fi
->lock_owner
));
1850 pthread_mutex_unlock(&inode
->plock_mutex
);
1852 res
= close(dup(lo_fi_fd(req
, fi
)));
1853 lo_inode_put(lo_data(req
), &inode
);
1854 fuse_reply_err(req
, res
== -1 ? errno
: 0);
1857 static void lo_fsync(fuse_req_t req
, fuse_ino_t ino
, int datasync
,
1858 struct fuse_file_info
*fi
)
1864 fuse_log(FUSE_LOG_DEBUG
, "lo_fsync(ino=%" PRIu64
", fi=0x%p)\n", ino
,
1868 struct lo_data
*lo
= lo_data(req
);
1870 res
= asprintf(&buf
, "%i", lo_fd(req
, ino
));
1872 return (void)fuse_reply_err(req
, errno
);
1875 fd
= openat(lo
->proc_self_fd
, buf
, O_RDWR
);
1878 return (void)fuse_reply_err(req
, errno
);
1881 fd
= lo_fi_fd(req
, fi
);
1885 res
= fdatasync(fd
);
1892 fuse_reply_err(req
, res
== -1 ? errno
: 0);
1895 static void lo_read(fuse_req_t req
, fuse_ino_t ino
, size_t size
, off_t offset
,
1896 struct fuse_file_info
*fi
)
1898 struct fuse_bufvec buf
= FUSE_BUFVEC_INIT(size
);
1900 fuse_log(FUSE_LOG_DEBUG
,
1901 "lo_read(ino=%" PRIu64
", size=%zd, "
1903 ino
, size
, (unsigned long)offset
);
1905 buf
.buf
[0].flags
= FUSE_BUF_IS_FD
| FUSE_BUF_FD_SEEK
;
1906 buf
.buf
[0].fd
= lo_fi_fd(req
, fi
);
1907 buf
.buf
[0].pos
= offset
;
1909 fuse_reply_data(req
, &buf
);
1912 static void lo_write_buf(fuse_req_t req
, fuse_ino_t ino
,
1913 struct fuse_bufvec
*in_buf
, off_t off
,
1914 struct fuse_file_info
*fi
)
1918 struct fuse_bufvec out_buf
= FUSE_BUFVEC_INIT(fuse_buf_size(in_buf
));
1919 bool cap_fsetid_dropped
= false;
1921 out_buf
.buf
[0].flags
= FUSE_BUF_IS_FD
| FUSE_BUF_FD_SEEK
;
1922 out_buf
.buf
[0].fd
= lo_fi_fd(req
, fi
);
1923 out_buf
.buf
[0].pos
= off
;
1925 fuse_log(FUSE_LOG_DEBUG
,
1926 "lo_write_buf(ino=%" PRIu64
", size=%zd, off=%lu)\n", ino
,
1927 out_buf
.buf
[0].size
, (unsigned long)off
);
1930 * If kill_priv is set, drop CAP_FSETID which should lead to kernel
1931 * clearing setuid/setgid on file.
1933 if (fi
->kill_priv
) {
1934 res
= drop_effective_cap("FSETID", &cap_fsetid_dropped
);
1936 fuse_reply_err(req
, res
);
1941 res
= fuse_buf_copy(&out_buf
, in_buf
);
1943 fuse_reply_err(req
, -res
);
1945 fuse_reply_write(req
, (size_t)res
);
1948 if (cap_fsetid_dropped
) {
1949 res
= gain_effective_cap("FSETID");
1951 fuse_log(FUSE_LOG_ERR
, "Failed to gain CAP_FSETID\n");
1956 static void lo_statfs(fuse_req_t req
, fuse_ino_t ino
)
1959 struct statvfs stbuf
;
1961 res
= fstatvfs(lo_fd(req
, ino
), &stbuf
);
1963 fuse_reply_err(req
, errno
);
1965 fuse_reply_statfs(req
, &stbuf
);
1969 static void lo_fallocate(fuse_req_t req
, fuse_ino_t ino
, int mode
, off_t offset
,
1970 off_t length
, struct fuse_file_info
*fi
)
1972 int err
= EOPNOTSUPP
;
1975 #ifdef CONFIG_FALLOCATE
1976 err
= fallocate(lo_fi_fd(req
, fi
), mode
, offset
, length
);
1981 #elif defined(CONFIG_POSIX_FALLOCATE)
1983 fuse_reply_err(req
, EOPNOTSUPP
);
1987 err
= posix_fallocate(lo_fi_fd(req
, fi
), offset
, length
);
1990 fuse_reply_err(req
, err
);
1993 static void lo_flock(fuse_req_t req
, fuse_ino_t ino
, struct fuse_file_info
*fi
,
1999 res
= flock(lo_fi_fd(req
, fi
), op
);
2001 fuse_reply_err(req
, res
== -1 ? errno
: 0);
2004 static void lo_getxattr(fuse_req_t req
, fuse_ino_t ino
, const char *name
,
2007 struct lo_data
*lo
= lo_data(req
);
2010 struct lo_inode
*inode
;
2015 inode
= lo_inode(req
, ino
);
2017 fuse_reply_err(req
, EBADF
);
2022 if (!lo_data(req
)->xattr
) {
2026 fuse_log(FUSE_LOG_DEBUG
, "lo_getxattr(ino=%" PRIu64
", name=%s size=%zd)\n",
2030 value
= malloc(size
);
2036 sprintf(procname
, "%i", inode
->fd
);
2038 * It is not safe to open() non-regular/non-dir files in file server
2039 * unless O_PATH is used, so use that method for regular files/dir
2040 * only (as it seems giving less performance overhead).
2041 * Otherwise, call fchdir() to avoid open().
2043 if (S_ISREG(inode
->filetype
) || S_ISDIR(inode
->filetype
)) {
2044 fd
= openat(lo
->proc_self_fd
, procname
, O_RDONLY
);
2048 ret
= fgetxattr(fd
, name
, value
, size
);
2050 /* fchdir should not fail here */
2051 assert(fchdir(lo
->proc_self_fd
) == 0);
2052 ret
= getxattr(procname
, name
, value
, size
);
2053 assert(fchdir(lo
->root
.fd
) == 0);
2064 fuse_reply_buf(req
, value
, ret
);
2066 fuse_reply_xattr(req
, ret
);
2075 lo_inode_put(lo
, &inode
);
2081 fuse_reply_err(req
, saverr
);
2085 static void lo_listxattr(fuse_req_t req
, fuse_ino_t ino
, size_t size
)
2087 struct lo_data
*lo
= lo_data(req
);
2090 struct lo_inode
*inode
;
2095 inode
= lo_inode(req
, ino
);
2097 fuse_reply_err(req
, EBADF
);
2102 if (!lo_data(req
)->xattr
) {
2106 fuse_log(FUSE_LOG_DEBUG
, "lo_listxattr(ino=%" PRIu64
", size=%zd)\n", ino
,
2110 value
= malloc(size
);
2116 sprintf(procname
, "%i", inode
->fd
);
2117 if (S_ISREG(inode
->filetype
) || S_ISDIR(inode
->filetype
)) {
2118 fd
= openat(lo
->proc_self_fd
, procname
, O_RDONLY
);
2122 ret
= flistxattr(fd
, value
, size
);
2124 /* fchdir should not fail here */
2125 assert(fchdir(lo
->proc_self_fd
) == 0);
2126 ret
= listxattr(procname
, value
, size
);
2127 assert(fchdir(lo
->root
.fd
) == 0);
2138 fuse_reply_buf(req
, value
, ret
);
2140 fuse_reply_xattr(req
, ret
);
2149 lo_inode_put(lo
, &inode
);
2155 fuse_reply_err(req
, saverr
);
2159 static void lo_setxattr(fuse_req_t req
, fuse_ino_t ino
, const char *name
,
2160 const char *value
, size_t size
, int flags
)
2163 struct lo_data
*lo
= lo_data(req
);
2164 struct lo_inode
*inode
;
2169 inode
= lo_inode(req
, ino
);
2171 fuse_reply_err(req
, EBADF
);
2176 if (!lo_data(req
)->xattr
) {
2180 fuse_log(FUSE_LOG_DEBUG
, "lo_setxattr(ino=%" PRIu64
2181 ", name=%s value=%s size=%zd)\n", ino
, name
, value
, size
);
2183 sprintf(procname
, "%i", inode
->fd
);
2184 if (S_ISREG(inode
->filetype
) || S_ISDIR(inode
->filetype
)) {
2185 fd
= openat(lo
->proc_self_fd
, procname
, O_RDONLY
);
2190 ret
= fsetxattr(fd
, name
, value
, size
, flags
);
2192 /* fchdir should not fail here */
2193 assert(fchdir(lo
->proc_self_fd
) == 0);
2194 ret
= setxattr(procname
, name
, value
, size
, flags
);
2195 assert(fchdir(lo
->root
.fd
) == 0);
2198 saverr
= ret
== -1 ? errno
: 0;
2205 lo_inode_put(lo
, &inode
);
2206 fuse_reply_err(req
, saverr
);
2209 static void lo_removexattr(fuse_req_t req
, fuse_ino_t ino
, const char *name
)
2212 struct lo_data
*lo
= lo_data(req
);
2213 struct lo_inode
*inode
;
2218 inode
= lo_inode(req
, ino
);
2220 fuse_reply_err(req
, EBADF
);
2225 if (!lo_data(req
)->xattr
) {
2229 fuse_log(FUSE_LOG_DEBUG
, "lo_removexattr(ino=%" PRIu64
", name=%s)\n", ino
,
2232 sprintf(procname
, "%i", inode
->fd
);
2233 if (S_ISREG(inode
->filetype
) || S_ISDIR(inode
->filetype
)) {
2234 fd
= openat(lo
->proc_self_fd
, procname
, O_RDONLY
);
2239 ret
= fremovexattr(fd
, name
);
2241 /* fchdir should not fail here */
2242 assert(fchdir(lo
->proc_self_fd
) == 0);
2243 ret
= removexattr(procname
, name
);
2244 assert(fchdir(lo
->root
.fd
) == 0);
2247 saverr
= ret
== -1 ? errno
: 0;
2254 lo_inode_put(lo
, &inode
);
2255 fuse_reply_err(req
, saverr
);
2258 #ifdef HAVE_COPY_FILE_RANGE
2259 static void lo_copy_file_range(fuse_req_t req
, fuse_ino_t ino_in
, off_t off_in
,
2260 struct fuse_file_info
*fi_in
, fuse_ino_t ino_out
,
2261 off_t off_out
, struct fuse_file_info
*fi_out
,
2262 size_t len
, int flags
)
2267 in_fd
= lo_fi_fd(req
, fi_in
);
2268 out_fd
= lo_fi_fd(req
, fi_out
);
2270 fuse_log(FUSE_LOG_DEBUG
,
2271 "lo_copy_file_range(ino=%" PRIu64
"/fd=%d, "
2272 "off=%lu, ino=%" PRIu64
"/fd=%d, "
2273 "off=%lu, size=%zd, flags=0x%x)\n",
2274 ino_in
, in_fd
, off_in
, ino_out
, out_fd
, off_out
, len
, flags
);
2276 res
= copy_file_range(in_fd
, &off_in
, out_fd
, &off_out
, len
, flags
);
2278 fuse_reply_err(req
, errno
);
2280 fuse_reply_write(req
, res
);
2285 static void lo_lseek(fuse_req_t req
, fuse_ino_t ino
, off_t off
, int whence
,
2286 struct fuse_file_info
*fi
)
2291 res
= lseek(lo_fi_fd(req
, fi
), off
, whence
);
2293 fuse_reply_lseek(req
, res
);
2295 fuse_reply_err(req
, errno
);
2299 static void lo_destroy(void *userdata
)
2301 struct lo_data
*lo
= (struct lo_data
*)userdata
;
2303 pthread_mutex_lock(&lo
->mutex
);
2305 GHashTableIter iter
;
2306 gpointer key
, value
;
2308 g_hash_table_iter_init(&iter
, lo
->inodes
);
2309 if (!g_hash_table_iter_next(&iter
, &key
, &value
)) {
2313 struct lo_inode
*inode
= value
;
2314 unref_inode(lo
, inode
, inode
->nlookup
);
2316 pthread_mutex_unlock(&lo
->mutex
);
2319 static struct fuse_lowlevel_ops lo_oper
= {
2321 .lookup
= lo_lookup
,
2324 .symlink
= lo_symlink
,
2326 .unlink
= lo_unlink
,
2328 .rename
= lo_rename
,
2329 .forget
= lo_forget
,
2330 .forget_multi
= lo_forget_multi
,
2331 .getattr
= lo_getattr
,
2332 .setattr
= lo_setattr
,
2333 .readlink
= lo_readlink
,
2334 .opendir
= lo_opendir
,
2335 .readdir
= lo_readdir
,
2336 .readdirplus
= lo_readdirplus
,
2337 .releasedir
= lo_releasedir
,
2338 .fsyncdir
= lo_fsyncdir
,
2339 .create
= lo_create
,
2343 .release
= lo_release
,
2347 .write_buf
= lo_write_buf
,
2348 .statfs
= lo_statfs
,
2349 .fallocate
= lo_fallocate
,
2351 .getxattr
= lo_getxattr
,
2352 .listxattr
= lo_listxattr
,
2353 .setxattr
= lo_setxattr
,
2354 .removexattr
= lo_removexattr
,
2355 #ifdef HAVE_COPY_FILE_RANGE
2356 .copy_file_range
= lo_copy_file_range
,
2359 .destroy
= lo_destroy
,
2362 /* Print vhost-user.json backend program capabilities */
2363 static void print_capabilities(void)
2366 printf(" \"type\": \"fs\"\n");
2371 * Drop all Linux capabilities because the wait parent process only needs to
2372 * sit in waitpid(2) and terminate.
2374 static void setup_wait_parent_capabilities(void)
2376 capng_setpid(syscall(SYS_gettid
));
2377 capng_clear(CAPNG_SELECT_BOTH
);
2378 capng_apply(CAPNG_SELECT_BOTH
);
2382 * Move to a new mount, net, and pid namespaces to isolate this process.
2384 static void setup_namespaces(struct lo_data
*lo
, struct fuse_session
*se
)
2387 char template[] = "virtiofsd-XXXXXX";
2391 * Create a new pid namespace for *child* processes. We'll have to
2392 * fork in order to enter the new pid namespace. A new mount namespace
2393 * is also needed so that we can remount /proc for the new pid
2396 * Our UNIX domain sockets have been created. Now we can move to
2397 * an empty network namespace to prevent TCP/IP and other network
2398 * activity in case this process is compromised.
2400 if (unshare(CLONE_NEWPID
| CLONE_NEWNS
| CLONE_NEWNET
) != 0) {
2401 fuse_log(FUSE_LOG_ERR
, "unshare(CLONE_NEWPID | CLONE_NEWNS): %m\n");
2407 fuse_log(FUSE_LOG_ERR
, "fork() failed: %m\n");
2414 setup_wait_parent_capabilities();
2416 /* The parent waits for the child */
2418 waited
= waitpid(child
, &wstatus
, 0);
2419 } while (waited
< 0 && errno
== EINTR
&& !se
->exited
);
2421 /* We were terminated by a signal, see fuse_signals.c */
2426 if (WIFEXITED(wstatus
)) {
2427 exit(WEXITSTATUS(wstatus
));
2433 /* Send us SIGTERM when the parent thread terminates, see prctl(2) */
2434 prctl(PR_SET_PDEATHSIG
, SIGTERM
);
2437 * If the mounts have shared propagation then we want to opt out so our
2438 * mount changes don't affect the parent mount namespace.
2440 if (mount(NULL
, "/", NULL
, MS_REC
| MS_SLAVE
, NULL
) < 0) {
2441 fuse_log(FUSE_LOG_ERR
, "mount(/, MS_REC|MS_SLAVE): %m\n");
2445 /* The child must remount /proc to use the new pid namespace */
2446 if (mount("proc", "/proc", "proc",
2447 MS_NODEV
| MS_NOEXEC
| MS_NOSUID
| MS_RELATIME
, NULL
) < 0) {
2448 fuse_log(FUSE_LOG_ERR
, "mount(/proc): %m\n");
2452 tmpdir
= mkdtemp(template);
2454 fuse_log(FUSE_LOG_ERR
, "tmpdir(%s): %m\n", template);
2458 if (mount("/proc/self/fd", tmpdir
, NULL
, MS_BIND
, NULL
) < 0) {
2459 fuse_log(FUSE_LOG_ERR
, "mount(/proc/self/fd, %s, MS_BIND): %m\n",
2464 /* Now we can get our /proc/self/fd directory file descriptor */
2465 lo
->proc_self_fd
= open(tmpdir
, O_PATH
);
2466 if (lo
->proc_self_fd
== -1) {
2467 fuse_log(FUSE_LOG_ERR
, "open(%s, O_PATH): %m\n", tmpdir
);
2471 if (umount2(tmpdir
, MNT_DETACH
) < 0) {
2472 fuse_log(FUSE_LOG_ERR
, "umount2(%s, MNT_DETACH): %m\n", tmpdir
);
2476 if (rmdir(tmpdir
) < 0) {
2477 fuse_log(FUSE_LOG_ERR
, "rmdir(%s): %m\n", tmpdir
);
2482 * Capture the capability state, we'll need to restore this for individual
2483 * threads later; see load_capng.
2485 static void setup_capng(void)
2487 /* Note this accesses /proc so has to happen before the sandbox */
2488 if (capng_get_caps_process()) {
2489 fuse_log(FUSE_LOG_ERR
, "capng_get_caps_process\n");
2492 pthread_mutex_init(&cap
.mutex
, NULL
);
2493 pthread_mutex_lock(&cap
.mutex
);
2494 cap
.saved
= capng_save_state();
2496 fuse_log(FUSE_LOG_ERR
, "capng_save_state\n");
2499 pthread_mutex_unlock(&cap
.mutex
);
2502 static void cleanup_capng(void)
2506 pthread_mutex_destroy(&cap
.mutex
);
2511 * Make the source directory our root so symlinks cannot escape and no other
2512 * files are accessible. Assumes unshare(CLONE_NEWNS) was already called.
2514 static void setup_mounts(const char *source
)
2519 if (mount(source
, source
, NULL
, MS_BIND
| MS_REC
, NULL
) < 0) {
2520 fuse_log(FUSE_LOG_ERR
, "mount(%s, %s, MS_BIND): %m\n", source
, source
);
2524 /* This magic is based on lxc's lxc_pivot_root() */
2525 oldroot
= open("/", O_DIRECTORY
| O_RDONLY
| O_CLOEXEC
);
2527 fuse_log(FUSE_LOG_ERR
, "open(/): %m\n");
2531 newroot
= open(source
, O_DIRECTORY
| O_RDONLY
| O_CLOEXEC
);
2533 fuse_log(FUSE_LOG_ERR
, "open(%s): %m\n", source
);
2537 if (fchdir(newroot
) < 0) {
2538 fuse_log(FUSE_LOG_ERR
, "fchdir(newroot): %m\n");
2542 if (syscall(__NR_pivot_root
, ".", ".") < 0) {
2543 fuse_log(FUSE_LOG_ERR
, "pivot_root(., .): %m\n");
2547 if (fchdir(oldroot
) < 0) {
2548 fuse_log(FUSE_LOG_ERR
, "fchdir(oldroot): %m\n");
2552 if (mount("", ".", "", MS_SLAVE
| MS_REC
, NULL
) < 0) {
2553 fuse_log(FUSE_LOG_ERR
, "mount(., MS_SLAVE | MS_REC): %m\n");
2557 if (umount2(".", MNT_DETACH
) < 0) {
2558 fuse_log(FUSE_LOG_ERR
, "umount2(., MNT_DETACH): %m\n");
2562 if (fchdir(newroot
) < 0) {
2563 fuse_log(FUSE_LOG_ERR
, "fchdir(newroot): %m\n");
2572 * Only keep whitelisted capabilities that are needed for file system operation
2574 static void setup_capabilities(void)
2576 pthread_mutex_lock(&cap
.mutex
);
2577 capng_restore_state(&cap
.saved
);
2580 * Whitelist file system-related capabilities that are needed for a file
2581 * server to act like root. Drop everything else like networking and
2582 * sysadmin capabilities.
2585 * 1. CAP_LINUX_IMMUTABLE is not included because it's only used via ioctl
2586 * and we don't support that.
2587 * 2. CAP_MAC_OVERRIDE is not included because it only seems to be
2588 * used by the Smack LSM. Omit it until there is demand for it.
2590 capng_setpid(syscall(SYS_gettid
));
2591 capng_clear(CAPNG_SELECT_BOTH
);
2592 capng_updatev(CAPNG_ADD
, CAPNG_PERMITTED
| CAPNG_EFFECTIVE
,
2595 CAP_DAC_READ_SEARCH
,
2602 capng_apply(CAPNG_SELECT_BOTH
);
2604 cap
.saved
= capng_save_state();
2605 pthread_mutex_unlock(&cap
.mutex
);
2609 * Lock down this process to prevent access to other processes or files outside
2610 * source directory. This reduces the impact of arbitrary code execution bugs.
2612 static void setup_sandbox(struct lo_data
*lo
, struct fuse_session
*se
,
2615 setup_namespaces(lo
, se
);
2616 setup_mounts(lo
->source
);
2617 setup_seccomp(enable_syslog
);
2618 setup_capabilities();
2621 /* Set the maximum number of open file descriptors */
2622 static void setup_nofile_rlimit(unsigned long rlimit_nofile
)
2624 struct rlimit rlim
= {
2625 .rlim_cur
= rlimit_nofile
,
2626 .rlim_max
= rlimit_nofile
,
2629 if (rlimit_nofile
== 0) {
2630 return; /* nothing to do */
2633 if (setrlimit(RLIMIT_NOFILE
, &rlim
) < 0) {
2634 /* Ignore SELinux denials */
2635 if (errno
== EPERM
) {
2639 fuse_log(FUSE_LOG_ERR
, "setrlimit(RLIMIT_NOFILE): %m\n");
2644 static void log_func(enum fuse_log_level level
, const char *fmt
, va_list ap
)
2646 g_autofree
char *localfmt
= NULL
;
2648 if (current_log_level
< level
) {
2652 if (current_log_level
== FUSE_LOG_DEBUG
) {
2654 localfmt
= g_strdup_printf("[%" PRId64
"] [ID: %08ld] %s",
2655 get_clock(), syscall(__NR_gettid
), fmt
);
2657 localfmt
= g_strdup_printf("[ID: %08ld] %s", syscall(__NR_gettid
),
2664 int priority
= LOG_ERR
;
2666 case FUSE_LOG_EMERG
:
2667 priority
= LOG_EMERG
;
2669 case FUSE_LOG_ALERT
:
2670 priority
= LOG_ALERT
;
2673 priority
= LOG_CRIT
;
2678 case FUSE_LOG_WARNING
:
2679 priority
= LOG_WARNING
;
2681 case FUSE_LOG_NOTICE
:
2682 priority
= LOG_NOTICE
;
2685 priority
= LOG_INFO
;
2687 case FUSE_LOG_DEBUG
:
2688 priority
= LOG_DEBUG
;
2691 vsyslog(priority
, fmt
, ap
);
2693 vfprintf(stderr
, fmt
, ap
);
2697 static void setup_root(struct lo_data
*lo
, struct lo_inode
*root
)
2702 fd
= open("/", O_PATH
);
2704 fuse_log(FUSE_LOG_ERR
, "open(%s, O_PATH): %m\n", lo
->source
);
2708 res
= fstatat(fd
, "", &stat
, AT_EMPTY_PATH
| AT_SYMLINK_NOFOLLOW
);
2710 fuse_log(FUSE_LOG_ERR
, "fstatat(%s): %m\n", lo
->source
);
2714 root
->filetype
= S_IFDIR
;
2716 root
->key
.ino
= stat
.st_ino
;
2717 root
->key
.dev
= stat
.st_dev
;
2719 g_atomic_int_set(&root
->refcount
, 2);
2722 static guint
lo_key_hash(gconstpointer key
)
2724 const struct lo_key
*lkey
= key
;
2726 return (guint
)lkey
->ino
+ (guint
)lkey
->dev
;
2729 static gboolean
lo_key_equal(gconstpointer a
, gconstpointer b
)
2731 const struct lo_key
*la
= a
;
2732 const struct lo_key
*lb
= b
;
2734 return la
->ino
== lb
->ino
&& la
->dev
== lb
->dev
;
2737 static void fuse_lo_data_cleanup(struct lo_data
*lo
)
2740 g_hash_table_destroy(lo
->inodes
);
2742 lo_map_destroy(&lo
->fd_map
);
2743 lo_map_destroy(&lo
->dirp_map
);
2744 lo_map_destroy(&lo
->ino_map
);
2746 if (lo
->proc_self_fd
>= 0) {
2747 close(lo
->proc_self_fd
);
2750 if (lo
->root
.fd
>= 0) {
2757 int main(int argc
, char *argv
[])
2759 struct fuse_args args
= FUSE_ARGS_INIT(argc
, argv
);
2760 struct fuse_session
*se
;
2761 struct fuse_cmdline_opts opts
;
2762 struct lo_data lo
= {
2768 struct lo_map_elem
*root_elem
;
2771 /* Don't mask creation mode, kernel already did that */
2774 pthread_mutex_init(&lo
.mutex
, NULL
);
2775 lo
.inodes
= g_hash_table_new(lo_key_hash
, lo_key_equal
);
2777 lo
.root
.fuse_ino
= FUSE_ROOT_ID
;
2778 lo
.cache
= CACHE_AUTO
;
2781 * Set up the ino map like this:
2782 * [0] Reserved (will not be used)
2785 lo_map_init(&lo
.ino_map
);
2786 lo_map_reserve(&lo
.ino_map
, 0)->in_use
= false;
2787 root_elem
= lo_map_reserve(&lo
.ino_map
, lo
.root
.fuse_ino
);
2788 root_elem
->inode
= &lo
.root
;
2790 lo_map_init(&lo
.dirp_map
);
2791 lo_map_init(&lo
.fd_map
);
2793 if (fuse_parse_cmdline(&args
, &opts
) != 0) {
2796 fuse_set_log_func(log_func
);
2797 use_syslog
= opts
.syslog
;
2799 openlog("virtiofsd", LOG_PID
, LOG_DAEMON
);
2802 if (opts
.show_help
) {
2803 printf("usage: %s [options]\n\n", argv
[0]);
2804 fuse_cmdline_help();
2805 printf(" -o source=PATH shared directory tree\n");
2806 fuse_lowlevel_help();
2809 } else if (opts
.show_version
) {
2810 fuse_lowlevel_version();
2813 } else if (opts
.print_capabilities
) {
2814 print_capabilities();
2819 if (fuse_opt_parse(&args
, &lo
, lo_opts
, NULL
) == -1) {
2824 * log_level is 0 if not configured via cmd options (0 is LOG_EMERG,
2825 * and we don't use this log level).
2827 if (opts
.log_level
!= 0) {
2828 current_log_level
= opts
.log_level
;
2830 lo
.debug
= opts
.debug
;
2832 current_log_level
= FUSE_LOG_DEBUG
;
2838 res
= lstat(lo
.source
, &stat
);
2840 fuse_log(FUSE_LOG_ERR
, "failed to stat source (\"%s\"): %m\n",
2844 if (!S_ISDIR(stat
.st_mode
)) {
2845 fuse_log(FUSE_LOG_ERR
, "source is not a directory\n");
2849 lo
.source
= strdup("/");
2851 if (!lo
.timeout_set
) {
2862 lo
.timeout
= 86400.0;
2865 } else if (lo
.timeout
< 0) {
2866 fuse_log(FUSE_LOG_ERR
, "timeout is negative (%lf)\n", lo
.timeout
);
2870 se
= fuse_session_new(&args
, &lo_oper
, sizeof(lo_oper
), &lo
);
2875 if (fuse_set_signal_handlers(se
) != 0) {
2879 if (fuse_session_mount(se
) != 0) {
2883 fuse_daemonize(opts
.foreground
);
2885 setup_nofile_rlimit(opts
.rlimit_nofile
);
2887 /* Must be before sandbox since it wants /proc */
2890 setup_sandbox(&lo
, se
, opts
.syslog
);
2892 setup_root(&lo
, &lo
.root
);
2893 /* Block until ctrl+c or fusermount -u */
2894 ret
= virtio_loop(se
);
2896 fuse_session_unmount(se
);
2899 fuse_remove_signal_handlers(se
);
2901 fuse_session_destroy(se
);
2903 fuse_opt_free_args(&args
);
2905 fuse_lo_data_cleanup(&lo
);