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"
68 #include "passthrough_seccomp.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
;
155 struct lo_inode root
;
156 GHashTable
*inodes
; /* protected by lo->mutex */
157 struct lo_map ino_map
; /* protected by lo->mutex */
158 struct lo_map dirp_map
; /* protected by lo->mutex */
159 struct lo_map fd_map
; /* protected by lo->mutex */
161 /* An O_PATH file descriptor to /proc/self/fd/ */
165 static const struct fuse_opt lo_opts
[] = {
166 { "writeback", offsetof(struct lo_data
, writeback
), 1 },
167 { "no_writeback", offsetof(struct lo_data
, writeback
), 0 },
168 { "source=%s", offsetof(struct lo_data
, source
), 0 },
169 { "flock", offsetof(struct lo_data
, flock
), 1 },
170 { "no_flock", offsetof(struct lo_data
, flock
), 0 },
171 { "posix_lock", offsetof(struct lo_data
, posix_lock
), 1 },
172 { "no_posix_lock", offsetof(struct lo_data
, posix_lock
), 0 },
173 { "xattr", offsetof(struct lo_data
, xattr
), 1 },
174 { "no_xattr", offsetof(struct lo_data
, xattr
), 0 },
175 { "modcaps=%s", offsetof(struct lo_data
, modcaps
), 0 },
176 { "timeout=%lf", offsetof(struct lo_data
, timeout
), 0 },
177 { "timeout=", offsetof(struct lo_data
, timeout_set
), 1 },
178 { "cache=none", offsetof(struct lo_data
, cache
), CACHE_NONE
},
179 { "cache=auto", offsetof(struct lo_data
, cache
), CACHE_AUTO
},
180 { "cache=always", offsetof(struct lo_data
, cache
), CACHE_ALWAYS
},
181 { "readdirplus", offsetof(struct lo_data
, readdirplus_set
), 1 },
182 { "no_readdirplus", offsetof(struct lo_data
, readdirplus_clear
), 1 },
183 { "allow_direct_io", offsetof(struct lo_data
, allow_direct_io
), 1 },
184 { "no_allow_direct_io", offsetof(struct lo_data
, allow_direct_io
), 0 },
187 static bool use_syslog
= false;
188 static int current_log_level
;
189 static void unref_inode_lolocked(struct lo_data
*lo
, struct lo_inode
*inode
,
193 pthread_mutex_t mutex
;
196 /* That we loaded cap-ng in the current thread from the saved */
197 static __thread
bool cap_loaded
= 0;
199 static struct lo_inode
*lo_find(struct lo_data
*lo
, struct stat
*st
);
201 static int is_dot_or_dotdot(const char *name
)
203 return name
[0] == '.' &&
204 (name
[1] == '\0' || (name
[1] == '.' && name
[2] == '\0'));
207 /* Is `path` a single path component that is not "." or ".."? */
208 static int is_safe_path_component(const char *path
)
210 if (strchr(path
, '/')) {
214 return !is_dot_or_dotdot(path
);
217 static struct lo_data
*lo_data(fuse_req_t req
)
219 return (struct lo_data
*)fuse_req_userdata(req
);
223 * Load capng's state from our saved state if the current thread
224 * hadn't previously been loaded.
225 * returns 0 on success
227 static int load_capng(void)
230 pthread_mutex_lock(&cap
.mutex
);
231 capng_restore_state(&cap
.saved
);
233 * restore_state free's the saved copy
236 cap
.saved
= capng_save_state();
238 pthread_mutex_unlock(&cap
.mutex
);
239 fuse_log(FUSE_LOG_ERR
, "capng_save_state (thread)\n");
242 pthread_mutex_unlock(&cap
.mutex
);
245 * We want to use the loaded state for our pid,
248 capng_setpid(syscall(SYS_gettid
));
255 * Helpers for dropping and regaining effective capabilities. Returns 0
256 * on success, error otherwise
258 static int drop_effective_cap(const char *cap_name
, bool *cap_dropped
)
262 cap
= capng_name_to_capability(cap_name
);
265 fuse_log(FUSE_LOG_ERR
, "capng_name_to_capability(%s) failed:%s\n",
266 cap_name
, strerror(errno
));
272 fuse_log(FUSE_LOG_ERR
, "load_capng() failed\n");
276 /* We dont have this capability in effective set already. */
277 if (!capng_have_capability(CAPNG_EFFECTIVE
, cap
)) {
282 if (capng_update(CAPNG_DROP
, CAPNG_EFFECTIVE
, cap
)) {
284 fuse_log(FUSE_LOG_ERR
, "capng_update(DROP,) failed\n");
288 if (capng_apply(CAPNG_SELECT_CAPS
)) {
290 fuse_log(FUSE_LOG_ERR
, "drop:capng_apply() failed\n");
303 static int gain_effective_cap(const char *cap_name
)
308 cap
= capng_name_to_capability(cap_name
);
311 fuse_log(FUSE_LOG_ERR
, "capng_name_to_capability(%s) failed:%s\n",
312 cap_name
, strerror(errno
));
318 fuse_log(FUSE_LOG_ERR
, "load_capng() failed\n");
322 if (capng_update(CAPNG_ADD
, CAPNG_EFFECTIVE
, cap
)) {
324 fuse_log(FUSE_LOG_ERR
, "capng_update(ADD,) failed\n");
328 if (capng_apply(CAPNG_SELECT_CAPS
)) {
330 fuse_log(FUSE_LOG_ERR
, "gain:capng_apply() failed\n");
339 static void lo_map_init(struct lo_map
*map
)
346 static void lo_map_destroy(struct lo_map
*map
)
351 static int lo_map_grow(struct lo_map
*map
, size_t new_nelems
)
353 struct lo_map_elem
*new_elems
;
356 if (new_nelems
<= map
->nelems
) {
360 new_elems
= realloc(map
->elems
, sizeof(map
->elems
[0]) * new_nelems
);
365 for (i
= map
->nelems
; i
< new_nelems
; i
++) {
366 new_elems
[i
].freelist
= i
+ 1;
367 new_elems
[i
].in_use
= false;
369 new_elems
[new_nelems
- 1].freelist
= -1;
371 map
->elems
= new_elems
;
372 map
->freelist
= map
->nelems
;
373 map
->nelems
= new_nelems
;
377 static struct lo_map_elem
*lo_map_alloc_elem(struct lo_map
*map
)
379 struct lo_map_elem
*elem
;
381 if (map
->freelist
== -1 && !lo_map_grow(map
, map
->nelems
+ 256)) {
385 elem
= &map
->elems
[map
->freelist
];
386 map
->freelist
= elem
->freelist
;
393 static struct lo_map_elem
*lo_map_reserve(struct lo_map
*map
, size_t key
)
397 if (!lo_map_grow(map
, key
+ 1)) {
401 for (prev
= &map
->freelist
; *prev
!= -1;
402 prev
= &map
->elems
[*prev
].freelist
) {
404 struct lo_map_elem
*elem
= &map
->elems
[key
];
406 *prev
= elem
->freelist
;
414 static struct lo_map_elem
*lo_map_get(struct lo_map
*map
, size_t key
)
416 if (key
>= map
->nelems
) {
419 if (!map
->elems
[key
].in_use
) {
422 return &map
->elems
[key
];
425 static void lo_map_remove(struct lo_map
*map
, size_t key
)
427 struct lo_map_elem
*elem
;
429 if (key
>= map
->nelems
) {
433 elem
= &map
->elems
[key
];
438 elem
->in_use
= false;
440 elem
->freelist
= map
->freelist
;
444 /* Assumes lo->mutex is held */
445 static ssize_t
lo_add_fd_mapping(fuse_req_t req
, int fd
)
447 struct lo_map_elem
*elem
;
449 elem
= lo_map_alloc_elem(&lo_data(req
)->fd_map
);
455 return elem
- lo_data(req
)->fd_map
.elems
;
458 /* Assumes lo->mutex is held */
459 static ssize_t
lo_add_dirp_mapping(fuse_req_t req
, struct lo_dirp
*dirp
)
461 struct lo_map_elem
*elem
;
463 elem
= lo_map_alloc_elem(&lo_data(req
)->dirp_map
);
469 return elem
- lo_data(req
)->dirp_map
.elems
;
472 /* Assumes lo->mutex is held */
473 static ssize_t
lo_add_inode_mapping(fuse_req_t req
, struct lo_inode
*inode
)
475 struct lo_map_elem
*elem
;
477 elem
= lo_map_alloc_elem(&lo_data(req
)->ino_map
);
483 return elem
- lo_data(req
)->ino_map
.elems
;
486 static void lo_inode_put(struct lo_data
*lo
, struct lo_inode
**inodep
)
488 struct lo_inode
*inode
= *inodep
;
496 if (g_atomic_int_dec_and_test(&inode
->refcount
)) {
502 /* Caller must release refcount using lo_inode_put() */
503 static struct lo_inode
*lo_inode(fuse_req_t req
, fuse_ino_t ino
)
505 struct lo_data
*lo
= lo_data(req
);
506 struct lo_map_elem
*elem
;
508 pthread_mutex_lock(&lo
->mutex
);
509 elem
= lo_map_get(&lo
->ino_map
, ino
);
511 g_atomic_int_inc(&elem
->inode
->refcount
);
513 pthread_mutex_unlock(&lo
->mutex
);
523 * TODO Remove this helper and force callers to hold an inode refcount until
524 * they are done with the fd. This will be done in a later patch to make
527 static int lo_fd(fuse_req_t req
, fuse_ino_t ino
)
529 struct lo_inode
*inode
= lo_inode(req
, ino
);
537 lo_inode_put(lo_data(req
), &inode
);
541 static void lo_init(void *userdata
, struct fuse_conn_info
*conn
)
543 struct lo_data
*lo
= (struct lo_data
*)userdata
;
545 if (conn
->capable
& FUSE_CAP_EXPORT_SUPPORT
) {
546 conn
->want
|= FUSE_CAP_EXPORT_SUPPORT
;
549 if (lo
->writeback
&& conn
->capable
& FUSE_CAP_WRITEBACK_CACHE
) {
550 fuse_log(FUSE_LOG_DEBUG
, "lo_init: activating writeback\n");
551 conn
->want
|= FUSE_CAP_WRITEBACK_CACHE
;
553 if (conn
->capable
& FUSE_CAP_FLOCK_LOCKS
) {
555 fuse_log(FUSE_LOG_DEBUG
, "lo_init: activating flock locks\n");
556 conn
->want
|= FUSE_CAP_FLOCK_LOCKS
;
558 fuse_log(FUSE_LOG_DEBUG
, "lo_init: disabling flock locks\n");
559 conn
->want
&= ~FUSE_CAP_FLOCK_LOCKS
;
563 if (conn
->capable
& FUSE_CAP_POSIX_LOCKS
) {
564 if (lo
->posix_lock
) {
565 fuse_log(FUSE_LOG_DEBUG
, "lo_init: activating posix locks\n");
566 conn
->want
|= FUSE_CAP_POSIX_LOCKS
;
568 fuse_log(FUSE_LOG_DEBUG
, "lo_init: disabling posix locks\n");
569 conn
->want
&= ~FUSE_CAP_POSIX_LOCKS
;
573 if ((lo
->cache
== CACHE_NONE
&& !lo
->readdirplus_set
) ||
574 lo
->readdirplus_clear
) {
575 fuse_log(FUSE_LOG_DEBUG
, "lo_init: disabling readdirplus\n");
576 conn
->want
&= ~FUSE_CAP_READDIRPLUS
;
580 static void lo_getattr(fuse_req_t req
, fuse_ino_t ino
,
581 struct fuse_file_info
*fi
)
585 struct lo_data
*lo
= lo_data(req
);
590 fstatat(lo_fd(req
, ino
), "", &buf
, AT_EMPTY_PATH
| AT_SYMLINK_NOFOLLOW
);
592 return (void)fuse_reply_err(req
, errno
);
595 fuse_reply_attr(req
, &buf
, lo
->timeout
);
598 static int lo_fi_fd(fuse_req_t req
, struct fuse_file_info
*fi
)
600 struct lo_data
*lo
= lo_data(req
);
601 struct lo_map_elem
*elem
;
603 pthread_mutex_lock(&lo
->mutex
);
604 elem
= lo_map_get(&lo
->fd_map
, fi
->fh
);
605 pthread_mutex_unlock(&lo
->mutex
);
614 static void lo_setattr(fuse_req_t req
, fuse_ino_t ino
, struct stat
*attr
,
615 int valid
, struct fuse_file_info
*fi
)
619 struct lo_data
*lo
= lo_data(req
);
620 struct lo_inode
*inode
;
625 inode
= lo_inode(req
, ino
);
627 fuse_reply_err(req
, EBADF
);
633 /* If fi->fh is invalid we'll report EBADF later */
635 fd
= lo_fi_fd(req
, fi
);
638 if (valid
& FUSE_SET_ATTR_MODE
) {
640 res
= fchmod(fd
, attr
->st_mode
);
642 sprintf(procname
, "%i", ifd
);
643 res
= fchmodat(lo
->proc_self_fd
, procname
, attr
->st_mode
, 0);
649 if (valid
& (FUSE_SET_ATTR_UID
| FUSE_SET_ATTR_GID
)) {
650 uid_t uid
= (valid
& FUSE_SET_ATTR_UID
) ? attr
->st_uid
: (uid_t
)-1;
651 gid_t gid
= (valid
& FUSE_SET_ATTR_GID
) ? attr
->st_gid
: (gid_t
)-1;
653 res
= fchownat(ifd
, "", uid
, gid
, AT_EMPTY_PATH
| AT_SYMLINK_NOFOLLOW
);
658 if (valid
& FUSE_SET_ATTR_SIZE
) {
664 sprintf(procname
, "%i", ifd
);
665 truncfd
= openat(lo
->proc_self_fd
, procname
, O_RDWR
);
671 res
= ftruncate(truncfd
, attr
->st_size
);
681 if (valid
& (FUSE_SET_ATTR_ATIME
| FUSE_SET_ATTR_MTIME
)) {
682 struct timespec tv
[2];
686 tv
[0].tv_nsec
= UTIME_OMIT
;
687 tv
[1].tv_nsec
= UTIME_OMIT
;
689 if (valid
& FUSE_SET_ATTR_ATIME_NOW
) {
690 tv
[0].tv_nsec
= UTIME_NOW
;
691 } else if (valid
& FUSE_SET_ATTR_ATIME
) {
692 tv
[0] = attr
->st_atim
;
695 if (valid
& FUSE_SET_ATTR_MTIME_NOW
) {
696 tv
[1].tv_nsec
= UTIME_NOW
;
697 } else if (valid
& FUSE_SET_ATTR_MTIME
) {
698 tv
[1] = attr
->st_mtim
;
702 res
= futimens(fd
, tv
);
704 sprintf(procname
, "%i", inode
->fd
);
705 res
= utimensat(lo
->proc_self_fd
, procname
, tv
, 0);
711 lo_inode_put(lo
, &inode
);
713 return lo_getattr(req
, ino
, fi
);
717 lo_inode_put(lo
, &inode
);
718 fuse_reply_err(req
, saverr
);
721 static struct lo_inode
*lo_find(struct lo_data
*lo
, struct stat
*st
)
724 struct lo_key key
= {
729 pthread_mutex_lock(&lo
->mutex
);
730 p
= g_hash_table_lookup(lo
->inodes
, &key
);
732 assert(p
->nlookup
> 0);
734 g_atomic_int_inc(&p
->refcount
);
736 pthread_mutex_unlock(&lo
->mutex
);
741 /* value_destroy_func for posix_locks GHashTable */
742 static void posix_locks_value_destroy(gpointer data
)
744 struct lo_inode_plock
*plock
= data
;
747 * We had used open() for locks and had only one fd. So
748 * closing this fd should release all OFD locks.
755 * Increments nlookup and caller must release refcount using
756 * lo_inode_put(&parent).
758 static int lo_do_lookup(fuse_req_t req
, fuse_ino_t parent
, const char *name
,
759 struct fuse_entry_param
*e
)
764 struct lo_data
*lo
= lo_data(req
);
765 struct lo_inode
*inode
= NULL
;
766 struct lo_inode
*dir
= lo_inode(req
, parent
);
769 * name_to_handle_at() and open_by_handle_at() can reach here with fuse
770 * mount point in guest, but we don't have its inode info in the
777 memset(e
, 0, sizeof(*e
));
778 e
->attr_timeout
= lo
->timeout
;
779 e
->entry_timeout
= lo
->timeout
;
781 /* Do not allow escaping root directory */
782 if (dir
== &lo
->root
&& strcmp(name
, "..") == 0) {
786 newfd
= openat(dir
->fd
, name
, O_PATH
| O_NOFOLLOW
);
791 res
= fstatat(newfd
, "", &e
->attr
, AT_EMPTY_PATH
| AT_SYMLINK_NOFOLLOW
);
796 inode
= lo_find(lo
, &e
->attr
);
800 inode
= calloc(1, sizeof(struct lo_inode
));
805 /* cache only filetype */
806 inode
->filetype
= (e
->attr
.st_mode
& S_IFMT
);
809 * One for the caller and one for nlookup (released in
810 * unref_inode_lolocked())
812 g_atomic_int_set(&inode
->refcount
, 2);
816 inode
->key
.ino
= e
->attr
.st_ino
;
817 inode
->key
.dev
= e
->attr
.st_dev
;
818 pthread_mutex_init(&inode
->plock_mutex
, NULL
);
819 inode
->posix_locks
= g_hash_table_new_full(
820 g_direct_hash
, g_direct_equal
, NULL
, posix_locks_value_destroy
);
822 pthread_mutex_lock(&lo
->mutex
);
823 inode
->fuse_ino
= lo_add_inode_mapping(req
, inode
);
824 g_hash_table_insert(lo
->inodes
, &inode
->key
, inode
);
825 pthread_mutex_unlock(&lo
->mutex
);
827 e
->ino
= inode
->fuse_ino
;
828 lo_inode_put(lo
, &inode
);
829 lo_inode_put(lo
, &dir
);
831 fuse_log(FUSE_LOG_DEBUG
, " %lli/%s -> %lli\n", (unsigned long long)parent
,
832 name
, (unsigned long long)e
->ino
);
841 lo_inode_put(lo
, &inode
);
842 lo_inode_put(lo
, &dir
);
846 static void lo_lookup(fuse_req_t req
, fuse_ino_t parent
, const char *name
)
848 struct fuse_entry_param e
;
851 fuse_log(FUSE_LOG_DEBUG
, "lo_lookup(parent=%" PRIu64
", name=%s)\n", parent
,
855 * Don't use is_safe_path_component(), allow "." and ".." for NFS export
858 if (strchr(name
, '/')) {
859 fuse_reply_err(req
, EINVAL
);
863 err
= lo_do_lookup(req
, parent
, name
, &e
);
865 fuse_reply_err(req
, err
);
867 fuse_reply_entry(req
, &e
);
872 * On some archs, setres*id is limited to 2^16 but they
873 * provide setres*id32 variants that allow 2^32.
874 * Others just let setres*id do 2^32 anyway.
876 #ifdef SYS_setresgid32
877 #define OURSYS_setresgid SYS_setresgid32
879 #define OURSYS_setresgid SYS_setresgid
882 #ifdef SYS_setresuid32
883 #define OURSYS_setresuid SYS_setresuid32
885 #define OURSYS_setresuid SYS_setresuid
889 * Change to uid/gid of caller so that file is created with
890 * ownership of caller.
891 * TODO: What about selinux context?
893 static int lo_change_cred(fuse_req_t req
, struct lo_cred
*old
)
897 old
->euid
= geteuid();
898 old
->egid
= getegid();
900 res
= syscall(OURSYS_setresgid
, -1, fuse_req_ctx(req
)->gid
, -1);
905 res
= syscall(OURSYS_setresuid
, -1, fuse_req_ctx(req
)->uid
, -1);
907 int errno_save
= errno
;
909 syscall(OURSYS_setresgid
, -1, old
->egid
, -1);
916 /* Regain Privileges */
917 static void lo_restore_cred(struct lo_cred
*old
)
921 res
= syscall(OURSYS_setresuid
, -1, old
->euid
, -1);
923 fuse_log(FUSE_LOG_ERR
, "seteuid(%u): %m\n", old
->euid
);
927 res
= syscall(OURSYS_setresgid
, -1, old
->egid
, -1);
929 fuse_log(FUSE_LOG_ERR
, "setegid(%u): %m\n", old
->egid
);
934 static void lo_mknod_symlink(fuse_req_t req
, fuse_ino_t parent
,
935 const char *name
, mode_t mode
, dev_t rdev
,
940 struct lo_data
*lo
= lo_data(req
);
941 struct lo_inode
*dir
;
942 struct fuse_entry_param e
;
943 struct lo_cred old
= {};
945 if (!is_safe_path_component(name
)) {
946 fuse_reply_err(req
, EINVAL
);
950 dir
= lo_inode(req
, parent
);
952 fuse_reply_err(req
, EBADF
);
956 saverr
= lo_change_cred(req
, &old
);
961 res
= mknod_wrapper(dir
->fd
, name
, link
, mode
, rdev
);
965 lo_restore_cred(&old
);
971 saverr
= lo_do_lookup(req
, parent
, name
, &e
);
976 fuse_log(FUSE_LOG_DEBUG
, " %lli/%s -> %lli\n", (unsigned long long)parent
,
977 name
, (unsigned long long)e
.ino
);
979 fuse_reply_entry(req
, &e
);
980 lo_inode_put(lo
, &dir
);
984 lo_inode_put(lo
, &dir
);
985 fuse_reply_err(req
, saverr
);
988 static void lo_mknod(fuse_req_t req
, fuse_ino_t parent
, const char *name
,
989 mode_t mode
, dev_t rdev
)
991 lo_mknod_symlink(req
, parent
, name
, mode
, rdev
, NULL
);
994 static void lo_mkdir(fuse_req_t req
, fuse_ino_t parent
, const char *name
,
997 lo_mknod_symlink(req
, parent
, name
, S_IFDIR
| mode
, 0, NULL
);
1000 static void lo_symlink(fuse_req_t req
, const char *link
, fuse_ino_t parent
,
1003 lo_mknod_symlink(req
, parent
, name
, S_IFLNK
, 0, link
);
1006 static void lo_link(fuse_req_t req
, fuse_ino_t ino
, fuse_ino_t parent
,
1010 struct lo_data
*lo
= lo_data(req
);
1011 struct lo_inode
*parent_inode
;
1012 struct lo_inode
*inode
;
1013 struct fuse_entry_param e
;
1017 if (!is_safe_path_component(name
)) {
1018 fuse_reply_err(req
, EINVAL
);
1022 parent_inode
= lo_inode(req
, parent
);
1023 inode
= lo_inode(req
, ino
);
1024 if (!parent_inode
|| !inode
) {
1029 memset(&e
, 0, sizeof(struct fuse_entry_param
));
1030 e
.attr_timeout
= lo
->timeout
;
1031 e
.entry_timeout
= lo
->timeout
;
1033 sprintf(procname
, "%i", inode
->fd
);
1034 res
= linkat(lo
->proc_self_fd
, procname
, parent_inode
->fd
, name
,
1040 res
= fstatat(inode
->fd
, "", &e
.attr
, AT_EMPTY_PATH
| AT_SYMLINK_NOFOLLOW
);
1045 pthread_mutex_lock(&lo
->mutex
);
1047 pthread_mutex_unlock(&lo
->mutex
);
1048 e
.ino
= inode
->fuse_ino
;
1050 fuse_log(FUSE_LOG_DEBUG
, " %lli/%s -> %lli\n", (unsigned long long)parent
,
1051 name
, (unsigned long long)e
.ino
);
1053 fuse_reply_entry(req
, &e
);
1054 lo_inode_put(lo
, &parent_inode
);
1055 lo_inode_put(lo
, &inode
);
1060 lo_inode_put(lo
, &parent_inode
);
1061 lo_inode_put(lo
, &inode
);
1062 fuse_reply_err(req
, saverr
);
1065 /* Increments nlookup and caller must release refcount using lo_inode_put() */
1066 static struct lo_inode
*lookup_name(fuse_req_t req
, fuse_ino_t parent
,
1072 res
= fstatat(lo_fd(req
, parent
), name
, &attr
,
1073 AT_EMPTY_PATH
| AT_SYMLINK_NOFOLLOW
);
1078 return lo_find(lo_data(req
), &attr
);
1081 static void lo_rmdir(fuse_req_t req
, fuse_ino_t parent
, const char *name
)
1084 struct lo_inode
*inode
;
1085 struct lo_data
*lo
= lo_data(req
);
1087 if (!is_safe_path_component(name
)) {
1088 fuse_reply_err(req
, EINVAL
);
1092 inode
= lookup_name(req
, parent
, name
);
1094 fuse_reply_err(req
, EIO
);
1098 res
= unlinkat(lo_fd(req
, parent
), name
, AT_REMOVEDIR
);
1100 fuse_reply_err(req
, res
== -1 ? errno
: 0);
1101 unref_inode_lolocked(lo
, inode
, 1);
1102 lo_inode_put(lo
, &inode
);
1105 static void lo_rename(fuse_req_t req
, fuse_ino_t parent
, const char *name
,
1106 fuse_ino_t newparent
, const char *newname
,
1110 struct lo_inode
*parent_inode
;
1111 struct lo_inode
*newparent_inode
;
1112 struct lo_inode
*oldinode
= NULL
;
1113 struct lo_inode
*newinode
= NULL
;
1114 struct lo_data
*lo
= lo_data(req
);
1116 if (!is_safe_path_component(name
) || !is_safe_path_component(newname
)) {
1117 fuse_reply_err(req
, EINVAL
);
1121 parent_inode
= lo_inode(req
, parent
);
1122 newparent_inode
= lo_inode(req
, newparent
);
1123 if (!parent_inode
|| !newparent_inode
) {
1124 fuse_reply_err(req
, EBADF
);
1128 oldinode
= lookup_name(req
, parent
, name
);
1129 newinode
= lookup_name(req
, newparent
, newname
);
1132 fuse_reply_err(req
, EIO
);
1137 #ifndef SYS_renameat2
1138 fuse_reply_err(req
, EINVAL
);
1140 res
= syscall(SYS_renameat2
, parent_inode
->fd
, name
,
1141 newparent_inode
->fd
, newname
, flags
);
1142 if (res
== -1 && errno
== ENOSYS
) {
1143 fuse_reply_err(req
, EINVAL
);
1145 fuse_reply_err(req
, res
== -1 ? errno
: 0);
1151 res
= renameat(parent_inode
->fd
, name
, newparent_inode
->fd
, newname
);
1153 fuse_reply_err(req
, res
== -1 ? errno
: 0);
1155 unref_inode_lolocked(lo
, oldinode
, 1);
1156 unref_inode_lolocked(lo
, newinode
, 1);
1157 lo_inode_put(lo
, &oldinode
);
1158 lo_inode_put(lo
, &newinode
);
1159 lo_inode_put(lo
, &parent_inode
);
1160 lo_inode_put(lo
, &newparent_inode
);
1163 static void lo_unlink(fuse_req_t req
, fuse_ino_t parent
, const char *name
)
1166 struct lo_inode
*inode
;
1167 struct lo_data
*lo
= lo_data(req
);
1169 if (!is_safe_path_component(name
)) {
1170 fuse_reply_err(req
, EINVAL
);
1174 inode
= lookup_name(req
, parent
, name
);
1176 fuse_reply_err(req
, EIO
);
1180 res
= unlinkat(lo_fd(req
, parent
), name
, 0);
1182 fuse_reply_err(req
, res
== -1 ? errno
: 0);
1183 unref_inode_lolocked(lo
, inode
, 1);
1184 lo_inode_put(lo
, &inode
);
1187 /* To be called with lo->mutex held */
1188 static void unref_inode(struct lo_data
*lo
, struct lo_inode
*inode
, uint64_t n
)
1194 assert(inode
->nlookup
>= n
);
1195 inode
->nlookup
-= n
;
1196 if (!inode
->nlookup
) {
1197 lo_map_remove(&lo
->ino_map
, inode
->fuse_ino
);
1198 g_hash_table_remove(lo
->inodes
, &inode
->key
);
1199 if (g_hash_table_size(inode
->posix_locks
)) {
1200 fuse_log(FUSE_LOG_WARNING
, "Hash table is not empty\n");
1202 g_hash_table_destroy(inode
->posix_locks
);
1203 pthread_mutex_destroy(&inode
->plock_mutex
);
1205 /* Drop our refcount from lo_do_lookup() */
1206 lo_inode_put(lo
, &inode
);
1210 static void unref_inode_lolocked(struct lo_data
*lo
, struct lo_inode
*inode
,
1217 pthread_mutex_lock(&lo
->mutex
);
1218 unref_inode(lo
, inode
, n
);
1219 pthread_mutex_unlock(&lo
->mutex
);
1222 static void lo_forget_one(fuse_req_t req
, fuse_ino_t ino
, uint64_t nlookup
)
1224 struct lo_data
*lo
= lo_data(req
);
1225 struct lo_inode
*inode
;
1227 inode
= lo_inode(req
, ino
);
1232 fuse_log(FUSE_LOG_DEBUG
, " forget %lli %lli -%lli\n",
1233 (unsigned long long)ino
, (unsigned long long)inode
->nlookup
,
1234 (unsigned long long)nlookup
);
1236 unref_inode_lolocked(lo
, inode
, nlookup
);
1237 lo_inode_put(lo
, &inode
);
1240 static void lo_forget(fuse_req_t req
, fuse_ino_t ino
, uint64_t nlookup
)
1242 lo_forget_one(req
, ino
, nlookup
);
1243 fuse_reply_none(req
);
1246 static void lo_forget_multi(fuse_req_t req
, size_t count
,
1247 struct fuse_forget_data
*forgets
)
1251 for (i
= 0; i
< count
; i
++) {
1252 lo_forget_one(req
, forgets
[i
].ino
, forgets
[i
].nlookup
);
1254 fuse_reply_none(req
);
1257 static void lo_readlink(fuse_req_t req
, fuse_ino_t ino
)
1259 char buf
[PATH_MAX
+ 1];
1262 res
= readlinkat(lo_fd(req
, ino
), "", buf
, sizeof(buf
));
1264 return (void)fuse_reply_err(req
, errno
);
1267 if (res
== sizeof(buf
)) {
1268 return (void)fuse_reply_err(req
, ENAMETOOLONG
);
1273 fuse_reply_readlink(req
, buf
);
1279 struct dirent
*entry
;
1283 static void lo_dirp_put(struct lo_dirp
**dp
)
1285 struct lo_dirp
*d
= *dp
;
1292 if (g_atomic_int_dec_and_test(&d
->refcount
)) {
1298 /* Call lo_dirp_put() on the return value when no longer needed */
1299 static struct lo_dirp
*lo_dirp(fuse_req_t req
, struct fuse_file_info
*fi
)
1301 struct lo_data
*lo
= lo_data(req
);
1302 struct lo_map_elem
*elem
;
1304 pthread_mutex_lock(&lo
->mutex
);
1305 elem
= lo_map_get(&lo
->dirp_map
, fi
->fh
);
1307 g_atomic_int_inc(&elem
->dirp
->refcount
);
1309 pthread_mutex_unlock(&lo
->mutex
);
1317 static void lo_opendir(fuse_req_t req
, fuse_ino_t ino
,
1318 struct fuse_file_info
*fi
)
1321 struct lo_data
*lo
= lo_data(req
);
1326 d
= calloc(1, sizeof(struct lo_dirp
));
1331 fd
= openat(lo_fd(req
, ino
), ".", O_RDONLY
);
1336 d
->dp
= fdopendir(fd
);
1337 if (d
->dp
== NULL
) {
1344 g_atomic_int_set(&d
->refcount
, 1); /* paired with lo_releasedir() */
1345 pthread_mutex_lock(&lo
->mutex
);
1346 fh
= lo_add_dirp_mapping(req
, d
);
1347 pthread_mutex_unlock(&lo
->mutex
);
1353 if (lo
->cache
== CACHE_ALWAYS
) {
1354 fi
->cache_readdir
= 1;
1356 fuse_reply_open(req
, fi
);
1365 } else if (fd
!= -1) {
1370 fuse_reply_err(req
, error
);
1373 static void lo_do_readdir(fuse_req_t req
, fuse_ino_t ino
, size_t size
,
1374 off_t offset
, struct fuse_file_info
*fi
, int plus
)
1376 struct lo_data
*lo
= lo_data(req
);
1377 struct lo_dirp
*d
= NULL
;
1378 struct lo_inode
*dinode
;
1384 dinode
= lo_inode(req
, ino
);
1389 d
= lo_dirp(req
, fi
);
1395 buf
= calloc(1, size
);
1401 if (offset
!= d
->offset
) {
1402 seekdir(d
->dp
, offset
);
1413 d
->entry
= readdir(d
->dp
);
1415 if (errno
) { /* Error */
1418 } else { /* End of stream */
1423 nextoff
= d
->entry
->d_off
;
1424 name
= d
->entry
->d_name
;
1426 fuse_ino_t entry_ino
= 0;
1427 struct fuse_entry_param e
= (struct fuse_entry_param
){
1428 .attr
.st_ino
= d
->entry
->d_ino
,
1429 .attr
.st_mode
= d
->entry
->d_type
<< 12,
1432 /* Hide root's parent directory */
1433 if (dinode
== &lo
->root
&& strcmp(name
, "..") == 0) {
1434 e
.attr
.st_ino
= lo
->root
.key
.ino
;
1435 e
.attr
.st_mode
= DT_DIR
<< 12;
1439 if (!is_dot_or_dotdot(name
)) {
1440 err
= lo_do_lookup(req
, ino
, name
, &e
);
1447 entsize
= fuse_add_direntry_plus(req
, p
, rem
, name
, &e
, nextoff
);
1449 entsize
= fuse_add_direntry(req
, p
, rem
, name
, &e
.attr
, nextoff
);
1451 if (entsize
> rem
) {
1452 if (entry_ino
!= 0) {
1453 lo_forget_one(req
, entry_ino
, 1);
1462 d
->offset
= nextoff
;
1468 lo_inode_put(lo
, &dinode
);
1471 * If there's an error, we can only signal it if we haven't stored
1472 * any entries yet - otherwise we'd end up with wrong lookup
1473 * counts for the entries that are already in the buffer. So we
1474 * return what we've collected until that point.
1476 if (err
&& rem
== size
) {
1477 fuse_reply_err(req
, err
);
1479 fuse_reply_buf(req
, buf
, size
- rem
);
1484 static void lo_readdir(fuse_req_t req
, fuse_ino_t ino
, size_t size
,
1485 off_t offset
, struct fuse_file_info
*fi
)
1487 lo_do_readdir(req
, ino
, size
, offset
, fi
, 0);
1490 static void lo_readdirplus(fuse_req_t req
, fuse_ino_t ino
, size_t size
,
1491 off_t offset
, struct fuse_file_info
*fi
)
1493 lo_do_readdir(req
, ino
, size
, offset
, fi
, 1);
1496 static void lo_releasedir(fuse_req_t req
, fuse_ino_t ino
,
1497 struct fuse_file_info
*fi
)
1499 struct lo_data
*lo
= lo_data(req
);
1500 struct lo_map_elem
*elem
;
1505 pthread_mutex_lock(&lo
->mutex
);
1506 elem
= lo_map_get(&lo
->dirp_map
, fi
->fh
);
1508 pthread_mutex_unlock(&lo
->mutex
);
1509 fuse_reply_err(req
, EBADF
);
1514 lo_map_remove(&lo
->dirp_map
, fi
->fh
);
1515 pthread_mutex_unlock(&lo
->mutex
);
1517 lo_dirp_put(&d
); /* paired with lo_opendir() */
1519 fuse_reply_err(req
, 0);
1522 static void update_open_flags(int writeback
, int allow_direct_io
,
1523 struct fuse_file_info
*fi
)
1526 * With writeback cache, kernel may send read requests even
1527 * when userspace opened write-only
1529 if (writeback
&& (fi
->flags
& O_ACCMODE
) == O_WRONLY
) {
1530 fi
->flags
&= ~O_ACCMODE
;
1531 fi
->flags
|= O_RDWR
;
1535 * With writeback cache, O_APPEND is handled by the kernel.
1536 * This breaks atomicity (since the file may change in the
1537 * underlying filesystem, so that the kernel's idea of the
1538 * end of the file isn't accurate anymore). In this example,
1539 * we just accept that. A more rigorous filesystem may want
1540 * to return an error here
1542 if (writeback
&& (fi
->flags
& O_APPEND
)) {
1543 fi
->flags
&= ~O_APPEND
;
1547 * O_DIRECT in guest should not necessarily mean bypassing page
1548 * cache on host as well. Therefore, we discard it by default
1549 * ('-o no_allow_direct_io'). If somebody needs that behavior,
1550 * the '-o allow_direct_io' option should be set.
1552 if (!allow_direct_io
) {
1553 fi
->flags
&= ~O_DIRECT
;
1557 static void lo_create(fuse_req_t req
, fuse_ino_t parent
, const char *name
,
1558 mode_t mode
, struct fuse_file_info
*fi
)
1561 struct lo_data
*lo
= lo_data(req
);
1562 struct lo_inode
*parent_inode
;
1563 struct fuse_entry_param e
;
1565 struct lo_cred old
= {};
1567 fuse_log(FUSE_LOG_DEBUG
, "lo_create(parent=%" PRIu64
", name=%s)\n", parent
,
1570 if (!is_safe_path_component(name
)) {
1571 fuse_reply_err(req
, EINVAL
);
1575 parent_inode
= lo_inode(req
, parent
);
1576 if (!parent_inode
) {
1577 fuse_reply_err(req
, EBADF
);
1581 err
= lo_change_cred(req
, &old
);
1586 update_open_flags(lo
->writeback
, lo
->allow_direct_io
, fi
);
1588 fd
= openat(parent_inode
->fd
, name
, (fi
->flags
| O_CREAT
) & ~O_NOFOLLOW
,
1590 err
= fd
== -1 ? errno
: 0;
1591 lo_restore_cred(&old
);
1596 pthread_mutex_lock(&lo
->mutex
);
1597 fh
= lo_add_fd_mapping(req
, fd
);
1598 pthread_mutex_unlock(&lo
->mutex
);
1606 err
= lo_do_lookup(req
, parent
, name
, &e
);
1608 if (lo
->cache
== CACHE_NONE
) {
1610 } else if (lo
->cache
== CACHE_ALWAYS
) {
1615 lo_inode_put(lo
, &parent_inode
);
1618 fuse_reply_err(req
, err
);
1620 fuse_reply_create(req
, &e
, fi
);
1624 /* Should be called with inode->plock_mutex held */
1625 static struct lo_inode_plock
*lookup_create_plock_ctx(struct lo_data
*lo
,
1626 struct lo_inode
*inode
,
1627 uint64_t lock_owner
,
1628 pid_t pid
, int *err
)
1630 struct lo_inode_plock
*plock
;
1635 g_hash_table_lookup(inode
->posix_locks
, GUINT_TO_POINTER(lock_owner
));
1641 plock
= malloc(sizeof(struct lo_inode_plock
));
1647 /* Open another instance of file which can be used for ofd locks. */
1648 sprintf(procname
, "%i", inode
->fd
);
1650 /* TODO: What if file is not writable? */
1651 fd
= openat(lo
->proc_self_fd
, procname
, O_RDWR
);
1658 plock
->lock_owner
= lock_owner
;
1660 g_hash_table_insert(inode
->posix_locks
, GUINT_TO_POINTER(plock
->lock_owner
),
1665 static void lo_getlk(fuse_req_t req
, fuse_ino_t ino
, struct fuse_file_info
*fi
,
1668 struct lo_data
*lo
= lo_data(req
);
1669 struct lo_inode
*inode
;
1670 struct lo_inode_plock
*plock
;
1671 int ret
, saverr
= 0;
1673 fuse_log(FUSE_LOG_DEBUG
,
1674 "lo_getlk(ino=%" PRIu64
", flags=%d)"
1675 " owner=0x%lx, l_type=%d l_start=0x%lx"
1677 ino
, fi
->flags
, fi
->lock_owner
, lock
->l_type
, lock
->l_start
,
1680 inode
= lo_inode(req
, ino
);
1682 fuse_reply_err(req
, EBADF
);
1686 pthread_mutex_lock(&inode
->plock_mutex
);
1688 lookup_create_plock_ctx(lo
, inode
, fi
->lock_owner
, lock
->l_pid
, &ret
);
1694 ret
= fcntl(plock
->fd
, F_OFD_GETLK
, lock
);
1700 pthread_mutex_unlock(&inode
->plock_mutex
);
1701 lo_inode_put(lo
, &inode
);
1704 fuse_reply_err(req
, saverr
);
1706 fuse_reply_lock(req
, lock
);
1710 static void lo_setlk(fuse_req_t req
, fuse_ino_t ino
, struct fuse_file_info
*fi
,
1711 struct flock
*lock
, int sleep
)
1713 struct lo_data
*lo
= lo_data(req
);
1714 struct lo_inode
*inode
;
1715 struct lo_inode_plock
*plock
;
1716 int ret
, saverr
= 0;
1718 fuse_log(FUSE_LOG_DEBUG
,
1719 "lo_setlk(ino=%" PRIu64
", flags=%d)"
1720 " cmd=%d pid=%d owner=0x%lx sleep=%d l_whence=%d"
1721 " l_start=0x%lx l_len=0x%lx\n",
1722 ino
, fi
->flags
, lock
->l_type
, lock
->l_pid
, fi
->lock_owner
, sleep
,
1723 lock
->l_whence
, lock
->l_start
, lock
->l_len
);
1726 fuse_reply_err(req
, EOPNOTSUPP
);
1730 inode
= lo_inode(req
, ino
);
1732 fuse_reply_err(req
, EBADF
);
1736 pthread_mutex_lock(&inode
->plock_mutex
);
1738 lookup_create_plock_ctx(lo
, inode
, fi
->lock_owner
, lock
->l_pid
, &ret
);
1745 /* TODO: Is it alright to modify flock? */
1747 ret
= fcntl(plock
->fd
, F_OFD_SETLK
, lock
);
1753 pthread_mutex_unlock(&inode
->plock_mutex
);
1754 lo_inode_put(lo
, &inode
);
1756 fuse_reply_err(req
, saverr
);
1759 static void lo_fsyncdir(fuse_req_t req
, fuse_ino_t ino
, int datasync
,
1760 struct fuse_file_info
*fi
)
1768 d
= lo_dirp(req
, fi
);
1770 fuse_reply_err(req
, EBADF
);
1776 res
= fdatasync(fd
);
1783 fuse_reply_err(req
, res
== -1 ? errno
: 0);
1786 static void lo_open(fuse_req_t req
, fuse_ino_t ino
, struct fuse_file_info
*fi
)
1791 struct lo_data
*lo
= lo_data(req
);
1793 fuse_log(FUSE_LOG_DEBUG
, "lo_open(ino=%" PRIu64
", flags=%d)\n", ino
,
1796 update_open_flags(lo
->writeback
, lo
->allow_direct_io
, fi
);
1798 sprintf(buf
, "%i", lo_fd(req
, ino
));
1799 fd
= openat(lo
->proc_self_fd
, buf
, fi
->flags
& ~O_NOFOLLOW
);
1801 return (void)fuse_reply_err(req
, errno
);
1804 pthread_mutex_lock(&lo
->mutex
);
1805 fh
= lo_add_fd_mapping(req
, fd
);
1806 pthread_mutex_unlock(&lo
->mutex
);
1809 fuse_reply_err(req
, ENOMEM
);
1814 if (lo
->cache
== CACHE_NONE
) {
1816 } else if (lo
->cache
== CACHE_ALWAYS
) {
1819 fuse_reply_open(req
, fi
);
1822 static void lo_release(fuse_req_t req
, fuse_ino_t ino
,
1823 struct fuse_file_info
*fi
)
1825 struct lo_data
*lo
= lo_data(req
);
1826 struct lo_map_elem
*elem
;
1831 pthread_mutex_lock(&lo
->mutex
);
1832 elem
= lo_map_get(&lo
->fd_map
, fi
->fh
);
1836 lo_map_remove(&lo
->fd_map
, fi
->fh
);
1838 pthread_mutex_unlock(&lo
->mutex
);
1841 fuse_reply_err(req
, 0);
1844 static void lo_flush(fuse_req_t req
, fuse_ino_t ino
, struct fuse_file_info
*fi
)
1848 struct lo_inode
*inode
;
1850 inode
= lo_inode(req
, ino
);
1852 fuse_reply_err(req
, EBADF
);
1856 /* An fd is going away. Cleanup associated posix locks */
1857 pthread_mutex_lock(&inode
->plock_mutex
);
1858 g_hash_table_remove(inode
->posix_locks
, GUINT_TO_POINTER(fi
->lock_owner
));
1859 pthread_mutex_unlock(&inode
->plock_mutex
);
1861 res
= close(dup(lo_fi_fd(req
, fi
)));
1862 lo_inode_put(lo_data(req
), &inode
);
1863 fuse_reply_err(req
, res
== -1 ? errno
: 0);
1866 static void lo_fsync(fuse_req_t req
, fuse_ino_t ino
, int datasync
,
1867 struct fuse_file_info
*fi
)
1873 fuse_log(FUSE_LOG_DEBUG
, "lo_fsync(ino=%" PRIu64
", fi=0x%p)\n", ino
,
1877 struct lo_data
*lo
= lo_data(req
);
1879 res
= asprintf(&buf
, "%i", lo_fd(req
, ino
));
1881 return (void)fuse_reply_err(req
, errno
);
1884 fd
= openat(lo
->proc_self_fd
, buf
, O_RDWR
);
1887 return (void)fuse_reply_err(req
, errno
);
1890 fd
= lo_fi_fd(req
, fi
);
1894 res
= fdatasync(fd
);
1901 fuse_reply_err(req
, res
== -1 ? errno
: 0);
1904 static void lo_read(fuse_req_t req
, fuse_ino_t ino
, size_t size
, off_t offset
,
1905 struct fuse_file_info
*fi
)
1907 struct fuse_bufvec buf
= FUSE_BUFVEC_INIT(size
);
1909 fuse_log(FUSE_LOG_DEBUG
,
1910 "lo_read(ino=%" PRIu64
", size=%zd, "
1912 ino
, size
, (unsigned long)offset
);
1914 buf
.buf
[0].flags
= FUSE_BUF_IS_FD
| FUSE_BUF_FD_SEEK
;
1915 buf
.buf
[0].fd
= lo_fi_fd(req
, fi
);
1916 buf
.buf
[0].pos
= offset
;
1918 fuse_reply_data(req
, &buf
);
1921 static void lo_write_buf(fuse_req_t req
, fuse_ino_t ino
,
1922 struct fuse_bufvec
*in_buf
, off_t off
,
1923 struct fuse_file_info
*fi
)
1927 struct fuse_bufvec out_buf
= FUSE_BUFVEC_INIT(fuse_buf_size(in_buf
));
1928 bool cap_fsetid_dropped
= false;
1930 out_buf
.buf
[0].flags
= FUSE_BUF_IS_FD
| FUSE_BUF_FD_SEEK
;
1931 out_buf
.buf
[0].fd
= lo_fi_fd(req
, fi
);
1932 out_buf
.buf
[0].pos
= off
;
1934 fuse_log(FUSE_LOG_DEBUG
,
1935 "lo_write_buf(ino=%" PRIu64
", size=%zd, off=%lu)\n", ino
,
1936 out_buf
.buf
[0].size
, (unsigned long)off
);
1939 * If kill_priv is set, drop CAP_FSETID which should lead to kernel
1940 * clearing setuid/setgid on file.
1942 if (fi
->kill_priv
) {
1943 res
= drop_effective_cap("FSETID", &cap_fsetid_dropped
);
1945 fuse_reply_err(req
, res
);
1950 res
= fuse_buf_copy(&out_buf
, in_buf
);
1952 fuse_reply_err(req
, -res
);
1954 fuse_reply_write(req
, (size_t)res
);
1957 if (cap_fsetid_dropped
) {
1958 res
= gain_effective_cap("FSETID");
1960 fuse_log(FUSE_LOG_ERR
, "Failed to gain CAP_FSETID\n");
1965 static void lo_statfs(fuse_req_t req
, fuse_ino_t ino
)
1968 struct statvfs stbuf
;
1970 res
= fstatvfs(lo_fd(req
, ino
), &stbuf
);
1972 fuse_reply_err(req
, errno
);
1974 fuse_reply_statfs(req
, &stbuf
);
1978 static void lo_fallocate(fuse_req_t req
, fuse_ino_t ino
, int mode
, off_t offset
,
1979 off_t length
, struct fuse_file_info
*fi
)
1981 int err
= EOPNOTSUPP
;
1984 #ifdef CONFIG_FALLOCATE
1985 err
= fallocate(lo_fi_fd(req
, fi
), mode
, offset
, length
);
1990 #elif defined(CONFIG_POSIX_FALLOCATE)
1992 fuse_reply_err(req
, EOPNOTSUPP
);
1996 err
= posix_fallocate(lo_fi_fd(req
, fi
), offset
, length
);
1999 fuse_reply_err(req
, err
);
2002 static void lo_flock(fuse_req_t req
, fuse_ino_t ino
, struct fuse_file_info
*fi
,
2008 res
= flock(lo_fi_fd(req
, fi
), op
);
2010 fuse_reply_err(req
, res
== -1 ? errno
: 0);
2013 static void lo_getxattr(fuse_req_t req
, fuse_ino_t ino
, const char *name
,
2016 struct lo_data
*lo
= lo_data(req
);
2019 struct lo_inode
*inode
;
2024 inode
= lo_inode(req
, ino
);
2026 fuse_reply_err(req
, EBADF
);
2031 if (!lo_data(req
)->xattr
) {
2035 fuse_log(FUSE_LOG_DEBUG
, "lo_getxattr(ino=%" PRIu64
", name=%s size=%zd)\n",
2039 value
= malloc(size
);
2045 sprintf(procname
, "%i", inode
->fd
);
2047 * It is not safe to open() non-regular/non-dir files in file server
2048 * unless O_PATH is used, so use that method for regular files/dir
2049 * only (as it seems giving less performance overhead).
2050 * Otherwise, call fchdir() to avoid open().
2052 if (S_ISREG(inode
->filetype
) || S_ISDIR(inode
->filetype
)) {
2053 fd
= openat(lo
->proc_self_fd
, procname
, O_RDONLY
);
2057 ret
= fgetxattr(fd
, name
, value
, size
);
2059 /* fchdir should not fail here */
2060 assert(fchdir(lo
->proc_self_fd
) == 0);
2061 ret
= getxattr(procname
, name
, value
, size
);
2062 assert(fchdir(lo
->root
.fd
) == 0);
2073 fuse_reply_buf(req
, value
, ret
);
2075 fuse_reply_xattr(req
, ret
);
2084 lo_inode_put(lo
, &inode
);
2090 fuse_reply_err(req
, saverr
);
2094 static void lo_listxattr(fuse_req_t req
, fuse_ino_t ino
, size_t size
)
2096 struct lo_data
*lo
= lo_data(req
);
2099 struct lo_inode
*inode
;
2104 inode
= lo_inode(req
, ino
);
2106 fuse_reply_err(req
, EBADF
);
2111 if (!lo_data(req
)->xattr
) {
2115 fuse_log(FUSE_LOG_DEBUG
, "lo_listxattr(ino=%" PRIu64
", size=%zd)\n", ino
,
2119 value
= malloc(size
);
2125 sprintf(procname
, "%i", inode
->fd
);
2126 if (S_ISREG(inode
->filetype
) || S_ISDIR(inode
->filetype
)) {
2127 fd
= openat(lo
->proc_self_fd
, procname
, O_RDONLY
);
2131 ret
= flistxattr(fd
, value
, size
);
2133 /* fchdir should not fail here */
2134 assert(fchdir(lo
->proc_self_fd
) == 0);
2135 ret
= listxattr(procname
, value
, size
);
2136 assert(fchdir(lo
->root
.fd
) == 0);
2147 fuse_reply_buf(req
, value
, ret
);
2149 fuse_reply_xattr(req
, ret
);
2158 lo_inode_put(lo
, &inode
);
2164 fuse_reply_err(req
, saverr
);
2168 static void lo_setxattr(fuse_req_t req
, fuse_ino_t ino
, const char *name
,
2169 const char *value
, size_t size
, int flags
)
2172 struct lo_data
*lo
= lo_data(req
);
2173 struct lo_inode
*inode
;
2178 inode
= lo_inode(req
, ino
);
2180 fuse_reply_err(req
, EBADF
);
2185 if (!lo_data(req
)->xattr
) {
2189 fuse_log(FUSE_LOG_DEBUG
, "lo_setxattr(ino=%" PRIu64
2190 ", name=%s value=%s size=%zd)\n", ino
, name
, value
, size
);
2192 sprintf(procname
, "%i", inode
->fd
);
2193 if (S_ISREG(inode
->filetype
) || S_ISDIR(inode
->filetype
)) {
2194 fd
= openat(lo
->proc_self_fd
, procname
, O_RDONLY
);
2199 ret
= fsetxattr(fd
, name
, value
, size
, flags
);
2201 /* fchdir should not fail here */
2202 assert(fchdir(lo
->proc_self_fd
) == 0);
2203 ret
= setxattr(procname
, name
, value
, size
, flags
);
2204 assert(fchdir(lo
->root
.fd
) == 0);
2207 saverr
= ret
== -1 ? errno
: 0;
2214 lo_inode_put(lo
, &inode
);
2215 fuse_reply_err(req
, saverr
);
2218 static void lo_removexattr(fuse_req_t req
, fuse_ino_t ino
, const char *name
)
2221 struct lo_data
*lo
= lo_data(req
);
2222 struct lo_inode
*inode
;
2227 inode
= lo_inode(req
, ino
);
2229 fuse_reply_err(req
, EBADF
);
2234 if (!lo_data(req
)->xattr
) {
2238 fuse_log(FUSE_LOG_DEBUG
, "lo_removexattr(ino=%" PRIu64
", name=%s)\n", ino
,
2241 sprintf(procname
, "%i", inode
->fd
);
2242 if (S_ISREG(inode
->filetype
) || S_ISDIR(inode
->filetype
)) {
2243 fd
= openat(lo
->proc_self_fd
, procname
, O_RDONLY
);
2248 ret
= fremovexattr(fd
, name
);
2250 /* fchdir should not fail here */
2251 assert(fchdir(lo
->proc_self_fd
) == 0);
2252 ret
= removexattr(procname
, name
);
2253 assert(fchdir(lo
->root
.fd
) == 0);
2256 saverr
= ret
== -1 ? errno
: 0;
2263 lo_inode_put(lo
, &inode
);
2264 fuse_reply_err(req
, saverr
);
2267 #ifdef HAVE_COPY_FILE_RANGE
2268 static void lo_copy_file_range(fuse_req_t req
, fuse_ino_t ino_in
, off_t off_in
,
2269 struct fuse_file_info
*fi_in
, fuse_ino_t ino_out
,
2270 off_t off_out
, struct fuse_file_info
*fi_out
,
2271 size_t len
, int flags
)
2276 in_fd
= lo_fi_fd(req
, fi_in
);
2277 out_fd
= lo_fi_fd(req
, fi_out
);
2279 fuse_log(FUSE_LOG_DEBUG
,
2280 "lo_copy_file_range(ino=%" PRIu64
"/fd=%d, "
2281 "off=%lu, ino=%" PRIu64
"/fd=%d, "
2282 "off=%lu, size=%zd, flags=0x%x)\n",
2283 ino_in
, in_fd
, off_in
, ino_out
, out_fd
, off_out
, len
, flags
);
2285 res
= copy_file_range(in_fd
, &off_in
, out_fd
, &off_out
, len
, flags
);
2287 fuse_reply_err(req
, errno
);
2289 fuse_reply_write(req
, res
);
2294 static void lo_lseek(fuse_req_t req
, fuse_ino_t ino
, off_t off
, int whence
,
2295 struct fuse_file_info
*fi
)
2300 res
= lseek(lo_fi_fd(req
, fi
), off
, whence
);
2302 fuse_reply_lseek(req
, res
);
2304 fuse_reply_err(req
, errno
);
2308 static void lo_destroy(void *userdata
)
2310 struct lo_data
*lo
= (struct lo_data
*)userdata
;
2312 pthread_mutex_lock(&lo
->mutex
);
2314 GHashTableIter iter
;
2315 gpointer key
, value
;
2317 g_hash_table_iter_init(&iter
, lo
->inodes
);
2318 if (!g_hash_table_iter_next(&iter
, &key
, &value
)) {
2322 struct lo_inode
*inode
= value
;
2323 unref_inode(lo
, inode
, inode
->nlookup
);
2325 pthread_mutex_unlock(&lo
->mutex
);
2328 static struct fuse_lowlevel_ops lo_oper
= {
2330 .lookup
= lo_lookup
,
2333 .symlink
= lo_symlink
,
2335 .unlink
= lo_unlink
,
2337 .rename
= lo_rename
,
2338 .forget
= lo_forget
,
2339 .forget_multi
= lo_forget_multi
,
2340 .getattr
= lo_getattr
,
2341 .setattr
= lo_setattr
,
2342 .readlink
= lo_readlink
,
2343 .opendir
= lo_opendir
,
2344 .readdir
= lo_readdir
,
2345 .readdirplus
= lo_readdirplus
,
2346 .releasedir
= lo_releasedir
,
2347 .fsyncdir
= lo_fsyncdir
,
2348 .create
= lo_create
,
2352 .release
= lo_release
,
2356 .write_buf
= lo_write_buf
,
2357 .statfs
= lo_statfs
,
2358 .fallocate
= lo_fallocate
,
2360 .getxattr
= lo_getxattr
,
2361 .listxattr
= lo_listxattr
,
2362 .setxattr
= lo_setxattr
,
2363 .removexattr
= lo_removexattr
,
2364 #ifdef HAVE_COPY_FILE_RANGE
2365 .copy_file_range
= lo_copy_file_range
,
2368 .destroy
= lo_destroy
,
2371 /* Print vhost-user.json backend program capabilities */
2372 static void print_capabilities(void)
2375 printf(" \"type\": \"fs\"\n");
2380 * Drop all Linux capabilities because the wait parent process only needs to
2381 * sit in waitpid(2) and terminate.
2383 static void setup_wait_parent_capabilities(void)
2385 capng_setpid(syscall(SYS_gettid
));
2386 capng_clear(CAPNG_SELECT_BOTH
);
2387 capng_apply(CAPNG_SELECT_BOTH
);
2391 * Move to a new mount, net, and pid namespaces to isolate this process.
2393 static void setup_namespaces(struct lo_data
*lo
, struct fuse_session
*se
)
2396 char template[] = "virtiofsd-XXXXXX";
2400 * Create a new pid namespace for *child* processes. We'll have to
2401 * fork in order to enter the new pid namespace. A new mount namespace
2402 * is also needed so that we can remount /proc for the new pid
2405 * Our UNIX domain sockets have been created. Now we can move to
2406 * an empty network namespace to prevent TCP/IP and other network
2407 * activity in case this process is compromised.
2409 if (unshare(CLONE_NEWPID
| CLONE_NEWNS
| CLONE_NEWNET
) != 0) {
2410 fuse_log(FUSE_LOG_ERR
, "unshare(CLONE_NEWPID | CLONE_NEWNS): %m\n");
2416 fuse_log(FUSE_LOG_ERR
, "fork() failed: %m\n");
2423 setup_wait_parent_capabilities();
2425 /* The parent waits for the child */
2427 waited
= waitpid(child
, &wstatus
, 0);
2428 } while (waited
< 0 && errno
== EINTR
&& !se
->exited
);
2430 /* We were terminated by a signal, see fuse_signals.c */
2435 if (WIFEXITED(wstatus
)) {
2436 exit(WEXITSTATUS(wstatus
));
2442 /* Send us SIGTERM when the parent thread terminates, see prctl(2) */
2443 prctl(PR_SET_PDEATHSIG
, SIGTERM
);
2446 * If the mounts have shared propagation then we want to opt out so our
2447 * mount changes don't affect the parent mount namespace.
2449 if (mount(NULL
, "/", NULL
, MS_REC
| MS_SLAVE
, NULL
) < 0) {
2450 fuse_log(FUSE_LOG_ERR
, "mount(/, MS_REC|MS_SLAVE): %m\n");
2454 /* The child must remount /proc to use the new pid namespace */
2455 if (mount("proc", "/proc", "proc",
2456 MS_NODEV
| MS_NOEXEC
| MS_NOSUID
| MS_RELATIME
, NULL
) < 0) {
2457 fuse_log(FUSE_LOG_ERR
, "mount(/proc): %m\n");
2461 tmpdir
= mkdtemp(template);
2463 fuse_log(FUSE_LOG_ERR
, "tmpdir(%s): %m\n", template);
2467 if (mount("/proc/self/fd", tmpdir
, NULL
, MS_BIND
, NULL
) < 0) {
2468 fuse_log(FUSE_LOG_ERR
, "mount(/proc/self/fd, %s, MS_BIND): %m\n",
2473 /* Now we can get our /proc/self/fd directory file descriptor */
2474 lo
->proc_self_fd
= open(tmpdir
, O_PATH
);
2475 if (lo
->proc_self_fd
== -1) {
2476 fuse_log(FUSE_LOG_ERR
, "open(%s, O_PATH): %m\n", tmpdir
);
2480 if (umount2(tmpdir
, MNT_DETACH
) < 0) {
2481 fuse_log(FUSE_LOG_ERR
, "umount2(%s, MNT_DETACH): %m\n", tmpdir
);
2485 if (rmdir(tmpdir
) < 0) {
2486 fuse_log(FUSE_LOG_ERR
, "rmdir(%s): %m\n", tmpdir
);
2491 * Capture the capability state, we'll need to restore this for individual
2492 * threads later; see load_capng.
2494 static void setup_capng(void)
2496 /* Note this accesses /proc so has to happen before the sandbox */
2497 if (capng_get_caps_process()) {
2498 fuse_log(FUSE_LOG_ERR
, "capng_get_caps_process\n");
2501 pthread_mutex_init(&cap
.mutex
, NULL
);
2502 pthread_mutex_lock(&cap
.mutex
);
2503 cap
.saved
= capng_save_state();
2505 fuse_log(FUSE_LOG_ERR
, "capng_save_state\n");
2508 pthread_mutex_unlock(&cap
.mutex
);
2511 static void cleanup_capng(void)
2515 pthread_mutex_destroy(&cap
.mutex
);
2520 * Make the source directory our root so symlinks cannot escape and no other
2521 * files are accessible. Assumes unshare(CLONE_NEWNS) was already called.
2523 static void setup_mounts(const char *source
)
2528 if (mount(source
, source
, NULL
, MS_BIND
| MS_REC
, NULL
) < 0) {
2529 fuse_log(FUSE_LOG_ERR
, "mount(%s, %s, MS_BIND): %m\n", source
, source
);
2533 /* This magic is based on lxc's lxc_pivot_root() */
2534 oldroot
= open("/", O_DIRECTORY
| O_RDONLY
| O_CLOEXEC
);
2536 fuse_log(FUSE_LOG_ERR
, "open(/): %m\n");
2540 newroot
= open(source
, O_DIRECTORY
| O_RDONLY
| O_CLOEXEC
);
2542 fuse_log(FUSE_LOG_ERR
, "open(%s): %m\n", source
);
2546 if (fchdir(newroot
) < 0) {
2547 fuse_log(FUSE_LOG_ERR
, "fchdir(newroot): %m\n");
2551 if (syscall(__NR_pivot_root
, ".", ".") < 0) {
2552 fuse_log(FUSE_LOG_ERR
, "pivot_root(., .): %m\n");
2556 if (fchdir(oldroot
) < 0) {
2557 fuse_log(FUSE_LOG_ERR
, "fchdir(oldroot): %m\n");
2561 if (mount("", ".", "", MS_SLAVE
| MS_REC
, NULL
) < 0) {
2562 fuse_log(FUSE_LOG_ERR
, "mount(., MS_SLAVE | MS_REC): %m\n");
2566 if (umount2(".", MNT_DETACH
) < 0) {
2567 fuse_log(FUSE_LOG_ERR
, "umount2(., MNT_DETACH): %m\n");
2571 if (fchdir(newroot
) < 0) {
2572 fuse_log(FUSE_LOG_ERR
, "fchdir(newroot): %m\n");
2581 * Only keep whitelisted capabilities that are needed for file system operation
2582 * The (possibly NULL) modcaps_in string passed in is free'd before exit.
2584 static void setup_capabilities(char *modcaps_in
)
2586 char *modcaps
= modcaps_in
;
2587 pthread_mutex_lock(&cap
.mutex
);
2588 capng_restore_state(&cap
.saved
);
2591 * Whitelist file system-related capabilities that are needed for a file
2592 * server to act like root. Drop everything else like networking and
2593 * sysadmin capabilities.
2596 * 1. CAP_LINUX_IMMUTABLE is not included because it's only used via ioctl
2597 * and we don't support that.
2598 * 2. CAP_MAC_OVERRIDE is not included because it only seems to be
2599 * used by the Smack LSM. Omit it until there is demand for it.
2601 capng_setpid(syscall(SYS_gettid
));
2602 capng_clear(CAPNG_SELECT_BOTH
);
2603 if (capng_updatev(CAPNG_ADD
, CAPNG_PERMITTED
| CAPNG_EFFECTIVE
,
2613 fuse_log(FUSE_LOG_ERR
, "%s: capng_updatev failed\n", __func__
);
2618 * The modcaps option is a colon separated list of caps,
2619 * each preceded by either + or -.
2625 char *next
= strchr(modcaps
, ':');
2631 switch (modcaps
[0]) {
2637 action
= CAPNG_DROP
;
2641 fuse_log(FUSE_LOG_ERR
,
2642 "%s: Expecting '+'/'-' in modcaps but found '%c'\n",
2643 __func__
, modcaps
[0]);
2646 cap
= capng_name_to_capability(modcaps
+ 1);
2648 fuse_log(FUSE_LOG_ERR
, "%s: Unknown capability '%s'\n", __func__
,
2652 if (capng_update(action
, CAPNG_PERMITTED
| CAPNG_EFFECTIVE
, cap
)) {
2653 fuse_log(FUSE_LOG_ERR
, "%s: capng_update failed for '%s'\n",
2662 if (capng_apply(CAPNG_SELECT_BOTH
)) {
2663 fuse_log(FUSE_LOG_ERR
, "%s: capng_apply failed\n", __func__
);
2667 cap
.saved
= capng_save_state();
2669 fuse_log(FUSE_LOG_ERR
, "%s: capng_save_state failed\n", __func__
);
2672 pthread_mutex_unlock(&cap
.mutex
);
2676 * Lock down this process to prevent access to other processes or files outside
2677 * source directory. This reduces the impact of arbitrary code execution bugs.
2679 static void setup_sandbox(struct lo_data
*lo
, struct fuse_session
*se
,
2682 setup_namespaces(lo
, se
);
2683 setup_mounts(lo
->source
);
2684 setup_seccomp(enable_syslog
);
2685 setup_capabilities(g_strdup(lo
->modcaps
));
2688 /* Set the maximum number of open file descriptors */
2689 static void setup_nofile_rlimit(unsigned long rlimit_nofile
)
2691 struct rlimit rlim
= {
2692 .rlim_cur
= rlimit_nofile
,
2693 .rlim_max
= rlimit_nofile
,
2696 if (rlimit_nofile
== 0) {
2697 return; /* nothing to do */
2700 if (setrlimit(RLIMIT_NOFILE
, &rlim
) < 0) {
2701 /* Ignore SELinux denials */
2702 if (errno
== EPERM
) {
2706 fuse_log(FUSE_LOG_ERR
, "setrlimit(RLIMIT_NOFILE): %m\n");
2711 static void log_func(enum fuse_log_level level
, const char *fmt
, va_list ap
)
2713 g_autofree
char *localfmt
= NULL
;
2715 if (current_log_level
< level
) {
2719 if (current_log_level
== FUSE_LOG_DEBUG
) {
2721 localfmt
= g_strdup_printf("[%" PRId64
"] [ID: %08ld] %s",
2722 get_clock(), syscall(__NR_gettid
), fmt
);
2724 localfmt
= g_strdup_printf("[ID: %08ld] %s", syscall(__NR_gettid
),
2731 int priority
= LOG_ERR
;
2733 case FUSE_LOG_EMERG
:
2734 priority
= LOG_EMERG
;
2736 case FUSE_LOG_ALERT
:
2737 priority
= LOG_ALERT
;
2740 priority
= LOG_CRIT
;
2745 case FUSE_LOG_WARNING
:
2746 priority
= LOG_WARNING
;
2748 case FUSE_LOG_NOTICE
:
2749 priority
= LOG_NOTICE
;
2752 priority
= LOG_INFO
;
2754 case FUSE_LOG_DEBUG
:
2755 priority
= LOG_DEBUG
;
2758 vsyslog(priority
, fmt
, ap
);
2760 vfprintf(stderr
, fmt
, ap
);
2764 static void setup_root(struct lo_data
*lo
, struct lo_inode
*root
)
2769 fd
= open("/", O_PATH
);
2771 fuse_log(FUSE_LOG_ERR
, "open(%s, O_PATH): %m\n", lo
->source
);
2775 res
= fstatat(fd
, "", &stat
, AT_EMPTY_PATH
| AT_SYMLINK_NOFOLLOW
);
2777 fuse_log(FUSE_LOG_ERR
, "fstatat(%s): %m\n", lo
->source
);
2781 root
->filetype
= S_IFDIR
;
2783 root
->key
.ino
= stat
.st_ino
;
2784 root
->key
.dev
= stat
.st_dev
;
2786 g_atomic_int_set(&root
->refcount
, 2);
2789 static guint
lo_key_hash(gconstpointer key
)
2791 const struct lo_key
*lkey
= key
;
2793 return (guint
)lkey
->ino
+ (guint
)lkey
->dev
;
2796 static gboolean
lo_key_equal(gconstpointer a
, gconstpointer b
)
2798 const struct lo_key
*la
= a
;
2799 const struct lo_key
*lb
= b
;
2801 return la
->ino
== lb
->ino
&& la
->dev
== lb
->dev
;
2804 static void fuse_lo_data_cleanup(struct lo_data
*lo
)
2807 g_hash_table_destroy(lo
->inodes
);
2809 lo_map_destroy(&lo
->fd_map
);
2810 lo_map_destroy(&lo
->dirp_map
);
2811 lo_map_destroy(&lo
->ino_map
);
2813 if (lo
->proc_self_fd
>= 0) {
2814 close(lo
->proc_self_fd
);
2817 if (lo
->root
.fd
>= 0) {
2824 int main(int argc
, char *argv
[])
2826 struct fuse_args args
= FUSE_ARGS_INIT(argc
, argv
);
2827 struct fuse_session
*se
;
2828 struct fuse_cmdline_opts opts
;
2829 struct lo_data lo
= {
2833 .allow_direct_io
= 0,
2836 struct lo_map_elem
*root_elem
;
2839 /* Don't mask creation mode, kernel already did that */
2842 pthread_mutex_init(&lo
.mutex
, NULL
);
2843 lo
.inodes
= g_hash_table_new(lo_key_hash
, lo_key_equal
);
2845 lo
.root
.fuse_ino
= FUSE_ROOT_ID
;
2846 lo
.cache
= CACHE_AUTO
;
2849 * Set up the ino map like this:
2850 * [0] Reserved (will not be used)
2853 lo_map_init(&lo
.ino_map
);
2854 lo_map_reserve(&lo
.ino_map
, 0)->in_use
= false;
2855 root_elem
= lo_map_reserve(&lo
.ino_map
, lo
.root
.fuse_ino
);
2856 root_elem
->inode
= &lo
.root
;
2858 lo_map_init(&lo
.dirp_map
);
2859 lo_map_init(&lo
.fd_map
);
2861 if (fuse_parse_cmdline(&args
, &opts
) != 0) {
2864 fuse_set_log_func(log_func
);
2865 use_syslog
= opts
.syslog
;
2867 openlog("virtiofsd", LOG_PID
, LOG_DAEMON
);
2870 if (opts
.show_help
) {
2871 printf("usage: %s [options]\n\n", argv
[0]);
2872 fuse_cmdline_help();
2873 printf(" -o source=PATH shared directory tree\n");
2874 fuse_lowlevel_help();
2877 } else if (opts
.show_version
) {
2878 fuse_lowlevel_version();
2881 } else if (opts
.print_capabilities
) {
2882 print_capabilities();
2887 if (fuse_opt_parse(&args
, &lo
, lo_opts
, NULL
) == -1) {
2892 * log_level is 0 if not configured via cmd options (0 is LOG_EMERG,
2893 * and we don't use this log level).
2895 if (opts
.log_level
!= 0) {
2896 current_log_level
= opts
.log_level
;
2898 lo
.debug
= opts
.debug
;
2900 current_log_level
= FUSE_LOG_DEBUG
;
2906 res
= lstat(lo
.source
, &stat
);
2908 fuse_log(FUSE_LOG_ERR
, "failed to stat source (\"%s\"): %m\n",
2912 if (!S_ISDIR(stat
.st_mode
)) {
2913 fuse_log(FUSE_LOG_ERR
, "source is not a directory\n");
2917 lo
.source
= strdup("/");
2919 if (!lo
.timeout_set
) {
2930 lo
.timeout
= 86400.0;
2933 } else if (lo
.timeout
< 0) {
2934 fuse_log(FUSE_LOG_ERR
, "timeout is negative (%lf)\n", lo
.timeout
);
2938 se
= fuse_session_new(&args
, &lo_oper
, sizeof(lo_oper
), &lo
);
2943 if (fuse_set_signal_handlers(se
) != 0) {
2947 if (fuse_session_mount(se
) != 0) {
2951 fuse_daemonize(opts
.foreground
);
2953 setup_nofile_rlimit(opts
.rlimit_nofile
);
2955 /* Must be before sandbox since it wants /proc */
2958 setup_sandbox(&lo
, se
, opts
.syslog
);
2960 setup_root(&lo
, &lo
.root
);
2961 /* Block until ctrl+c or fusermount -u */
2962 ret
= virtio_loop(se
);
2964 fuse_session_unmount(se
);
2967 fuse_remove_signal_handlers(se
);
2969 fuse_session_destroy(se
);
2971 fuse_opt_free_args(&args
);
2973 fuse_lo_data_cleanup(&lo
);