disas: Clean up CPUDebug initialization
[qemu.git] / tools / virtiofsd / passthrough_ll.c
1 /*
2 * FUSE: Filesystem in Userspace
3 * Copyright (C) 2001-2007 Miklos Szeredi <miklos@szeredi.hu>
4 *
5 * This program can be distributed under the terms of the GNU GPLv2.
6 * See the file COPYING.
7 */
8
9 /*
10 *
11 * This file system mirrors the existing file system hierarchy of the
12 * system, starting at the root file system. This is implemented by
13 * just "passing through" all requests to the corresponding user-space
14 * libc functions. In contrast to passthrough.c and passthrough_fh.c,
15 * this implementation uses the low-level API. Its performance should
16 * be the least bad among the three, but many operations are not
17 * implemented. In particular, it is not possible to remove files (or
18 * directories) because the code necessary to defer actual removal
19 * until the file is not opened anymore would make the example much
20 * more complicated.
21 *
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).
28 *
29 * Compile with:
30 *
31 * gcc -Wall passthrough_ll.c `pkg-config fuse3 --cflags --libs` -o
32 * passthrough_ll
33 *
34 * ## Source code ##
35 * \include passthrough_ll.c
36 */
37
38 #include "qemu/osdep.h"
39 #include "qemu/timer.h"
40 #include "fuse_virtio.h"
41 #include "fuse_log.h"
42 #include "fuse_lowlevel.h"
43 #include <assert.h>
44 #include <cap-ng.h>
45 #include <dirent.h>
46 #include <errno.h>
47 #include <glib.h>
48 #include <inttypes.h>
49 #include <limits.h>
50 #include <pthread.h>
51 #include <stdbool.h>
52 #include <stddef.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <sys/file.h>
57 #include <sys/mount.h>
58 #include <sys/prctl.h>
59 #include <sys/resource.h>
60 #include <sys/syscall.h>
61 #include <sys/types.h>
62 #include <sys/wait.h>
63 #include <sys/xattr.h>
64 #include <syslog.h>
65 #include <unistd.h>
66
67 #include "passthrough_helpers.h"
68 #include "passthrough_seccomp.h"
69
70 /* Keep track of inode posix locks for each owner. */
71 struct lo_inode_plock {
72 uint64_t lock_owner;
73 int fd; /* fd for OFD locks */
74 };
75
76 struct lo_map_elem {
77 union {
78 struct lo_inode *inode;
79 struct lo_dirp *dirp;
80 int fd;
81 ssize_t freelist;
82 };
83 bool in_use;
84 };
85
86 /* Maps FUSE fh or ino values to internal objects */
87 struct lo_map {
88 struct lo_map_elem *elems;
89 size_t nelems;
90 ssize_t freelist;
91 };
92
93 struct lo_key {
94 ino_t ino;
95 dev_t dev;
96 };
97
98 struct lo_inode {
99 int fd;
100
101 /*
102 * Atomic reference count for this object. The nlookup field holds a
103 * reference and release it when nlookup reaches 0.
104 */
105 gint refcount;
106
107 struct lo_key key;
108
109 /*
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.
114 *
115 * Note that this value is untrusted because the client can manipulate
116 * it arbitrarily using FUSE_FORGET requests.
117 *
118 * Protected by lo->mutex.
119 */
120 uint64_t nlookup;
121
122 fuse_ino_t fuse_ino;
123 pthread_mutex_t plock_mutex;
124 GHashTable *posix_locks; /* protected by lo_inode->plock_mutex */
125
126 mode_t filetype;
127 };
128
129 struct lo_cred {
130 uid_t euid;
131 gid_t egid;
132 };
133
134 enum {
135 CACHE_NONE,
136 CACHE_AUTO,
137 CACHE_ALWAYS,
138 };
139
140 struct lo_data {
141 pthread_mutex_t mutex;
142 int debug;
143 int writeback;
144 int flock;
145 int posix_lock;
146 int xattr;
147 char *source;
148 char *modcaps;
149 double timeout;
150 int cache;
151 int timeout_set;
152 int readdirplus_set;
153 int readdirplus_clear;
154 int allow_direct_io;
155 struct lo_inode root;
156 GHashTable *inodes; /* protected by lo->mutex */
157 struct lo_map ino_map; /* protected by lo->mutex */
158 struct lo_map dirp_map; /* protected by lo->mutex */
159 struct lo_map fd_map; /* protected by lo->mutex */
160
161 /* An O_PATH file descriptor to /proc/self/fd/ */
162 int proc_self_fd;
163 };
164
165 static const struct fuse_opt lo_opts[] = {
166 { "writeback", offsetof(struct lo_data, writeback), 1 },
167 { "no_writeback", offsetof(struct lo_data, writeback), 0 },
168 { "source=%s", offsetof(struct lo_data, source), 0 },
169 { "flock", offsetof(struct lo_data, flock), 1 },
170 { "no_flock", offsetof(struct lo_data, flock), 0 },
171 { "posix_lock", offsetof(struct lo_data, posix_lock), 1 },
172 { "no_posix_lock", offsetof(struct lo_data, posix_lock), 0 },
173 { "xattr", offsetof(struct lo_data, xattr), 1 },
174 { "no_xattr", offsetof(struct lo_data, xattr), 0 },
175 { "modcaps=%s", offsetof(struct lo_data, modcaps), 0 },
176 { "timeout=%lf", offsetof(struct lo_data, timeout), 0 },
177 { "timeout=", offsetof(struct lo_data, timeout_set), 1 },
178 { "cache=none", offsetof(struct lo_data, cache), CACHE_NONE },
179 { "cache=auto", offsetof(struct lo_data, cache), CACHE_AUTO },
180 { "cache=always", offsetof(struct lo_data, cache), CACHE_ALWAYS },
181 { "readdirplus", offsetof(struct lo_data, readdirplus_set), 1 },
182 { "no_readdirplus", offsetof(struct lo_data, readdirplus_clear), 1 },
183 { "allow_direct_io", offsetof(struct lo_data, allow_direct_io), 1 },
184 { "no_allow_direct_io", offsetof(struct lo_data, allow_direct_io), 0 },
185 FUSE_OPT_END
186 };
187 static bool use_syslog = false;
188 static int current_log_level;
189 static void unref_inode_lolocked(struct lo_data *lo, struct lo_inode *inode,
190 uint64_t n);
191
192 static struct {
193 pthread_mutex_t mutex;
194 void *saved;
195 } cap;
196 /* That we loaded cap-ng in the current thread from the saved */
197 static __thread bool cap_loaded = 0;
198
199 static struct lo_inode *lo_find(struct lo_data *lo, struct stat *st);
200
201 static int is_dot_or_dotdot(const char *name)
202 {
203 return name[0] == '.' &&
204 (name[1] == '\0' || (name[1] == '.' && name[2] == '\0'));
205 }
206
207 /* Is `path` a single path component that is not "." or ".."? */
208 static int is_safe_path_component(const char *path)
209 {
210 if (strchr(path, '/')) {
211 return 0;
212 }
213
214 return !is_dot_or_dotdot(path);
215 }
216
217 static struct lo_data *lo_data(fuse_req_t req)
218 {
219 return (struct lo_data *)fuse_req_userdata(req);
220 }
221
222 /*
223 * Load capng's state from our saved state if the current thread
224 * hadn't previously been loaded.
225 * returns 0 on success
226 */
227 static int load_capng(void)
228 {
229 if (!cap_loaded) {
230 pthread_mutex_lock(&cap.mutex);
231 capng_restore_state(&cap.saved);
232 /*
233 * restore_state free's the saved copy
234 * so make another.
235 */
236 cap.saved = capng_save_state();
237 if (!cap.saved) {
238 pthread_mutex_unlock(&cap.mutex);
239 fuse_log(FUSE_LOG_ERR, "capng_save_state (thread)\n");
240 return -EINVAL;
241 }
242 pthread_mutex_unlock(&cap.mutex);
243
244 /*
245 * We want to use the loaded state for our pid,
246 * not the original
247 */
248 capng_setpid(syscall(SYS_gettid));
249 cap_loaded = true;
250 }
251 return 0;
252 }
253
254 /*
255 * Helpers for dropping and regaining effective capabilities. Returns 0
256 * on success, error otherwise
257 */
258 static int drop_effective_cap(const char *cap_name, bool *cap_dropped)
259 {
260 int cap, ret;
261
262 cap = capng_name_to_capability(cap_name);
263 if (cap < 0) {
264 ret = errno;
265 fuse_log(FUSE_LOG_ERR, "capng_name_to_capability(%s) failed:%s\n",
266 cap_name, strerror(errno));
267 goto out;
268 }
269
270 if (load_capng()) {
271 ret = errno;
272 fuse_log(FUSE_LOG_ERR, "load_capng() failed\n");
273 goto out;
274 }
275
276 /* We dont have this capability in effective set already. */
277 if (!capng_have_capability(CAPNG_EFFECTIVE, cap)) {
278 ret = 0;
279 goto out;
280 }
281
282 if (capng_update(CAPNG_DROP, CAPNG_EFFECTIVE, cap)) {
283 ret = errno;
284 fuse_log(FUSE_LOG_ERR, "capng_update(DROP,) failed\n");
285 goto out;
286 }
287
288 if (capng_apply(CAPNG_SELECT_CAPS)) {
289 ret = errno;
290 fuse_log(FUSE_LOG_ERR, "drop:capng_apply() failed\n");
291 goto out;
292 }
293
294 ret = 0;
295 if (cap_dropped) {
296 *cap_dropped = true;
297 }
298
299 out:
300 return ret;
301 }
302
303 static int gain_effective_cap(const char *cap_name)
304 {
305 int cap;
306 int ret = 0;
307
308 cap = capng_name_to_capability(cap_name);
309 if (cap < 0) {
310 ret = errno;
311 fuse_log(FUSE_LOG_ERR, "capng_name_to_capability(%s) failed:%s\n",
312 cap_name, strerror(errno));
313 goto out;
314 }
315
316 if (load_capng()) {
317 ret = errno;
318 fuse_log(FUSE_LOG_ERR, "load_capng() failed\n");
319 goto out;
320 }
321
322 if (capng_update(CAPNG_ADD, CAPNG_EFFECTIVE, cap)) {
323 ret = errno;
324 fuse_log(FUSE_LOG_ERR, "capng_update(ADD,) failed\n");
325 goto out;
326 }
327
328 if (capng_apply(CAPNG_SELECT_CAPS)) {
329 ret = errno;
330 fuse_log(FUSE_LOG_ERR, "gain:capng_apply() failed\n");
331 goto out;
332 }
333 ret = 0;
334
335 out:
336 return ret;
337 }
338
339 static void lo_map_init(struct lo_map *map)
340 {
341 map->elems = NULL;
342 map->nelems = 0;
343 map->freelist = -1;
344 }
345
346 static void lo_map_destroy(struct lo_map *map)
347 {
348 free(map->elems);
349 }
350
351 static int lo_map_grow(struct lo_map *map, size_t new_nelems)
352 {
353 struct lo_map_elem *new_elems;
354 size_t i;
355
356 if (new_nelems <= map->nelems) {
357 return 1;
358 }
359
360 new_elems = realloc(map->elems, sizeof(map->elems[0]) * new_nelems);
361 if (!new_elems) {
362 return 0;
363 }
364
365 for (i = map->nelems; i < new_nelems; i++) {
366 new_elems[i].freelist = i + 1;
367 new_elems[i].in_use = false;
368 }
369 new_elems[new_nelems - 1].freelist = -1;
370
371 map->elems = new_elems;
372 map->freelist = map->nelems;
373 map->nelems = new_nelems;
374 return 1;
375 }
376
377 static struct lo_map_elem *lo_map_alloc_elem(struct lo_map *map)
378 {
379 struct lo_map_elem *elem;
380
381 if (map->freelist == -1 && !lo_map_grow(map, map->nelems + 256)) {
382 return NULL;
383 }
384
385 elem = &map->elems[map->freelist];
386 map->freelist = elem->freelist;
387
388 elem->in_use = true;
389
390 return elem;
391 }
392
393 static struct lo_map_elem *lo_map_reserve(struct lo_map *map, size_t key)
394 {
395 ssize_t *prev;
396
397 if (!lo_map_grow(map, key + 1)) {
398 return NULL;
399 }
400
401 for (prev = &map->freelist; *prev != -1;
402 prev = &map->elems[*prev].freelist) {
403 if (*prev == key) {
404 struct lo_map_elem *elem = &map->elems[key];
405
406 *prev = elem->freelist;
407 elem->in_use = true;
408 return elem;
409 }
410 }
411 return NULL;
412 }
413
414 static struct lo_map_elem *lo_map_get(struct lo_map *map, size_t key)
415 {
416 if (key >= map->nelems) {
417 return NULL;
418 }
419 if (!map->elems[key].in_use) {
420 return NULL;
421 }
422 return &map->elems[key];
423 }
424
425 static void lo_map_remove(struct lo_map *map, size_t key)
426 {
427 struct lo_map_elem *elem;
428
429 if (key >= map->nelems) {
430 return;
431 }
432
433 elem = &map->elems[key];
434 if (!elem->in_use) {
435 return;
436 }
437
438 elem->in_use = false;
439
440 elem->freelist = map->freelist;
441 map->freelist = key;
442 }
443
444 /* Assumes lo->mutex is held */
445 static ssize_t lo_add_fd_mapping(fuse_req_t req, int fd)
446 {
447 struct lo_map_elem *elem;
448
449 elem = lo_map_alloc_elem(&lo_data(req)->fd_map);
450 if (!elem) {
451 return -1;
452 }
453
454 elem->fd = fd;
455 return elem - lo_data(req)->fd_map.elems;
456 }
457
458 /* Assumes lo->mutex is held */
459 static ssize_t lo_add_dirp_mapping(fuse_req_t req, struct lo_dirp *dirp)
460 {
461 struct lo_map_elem *elem;
462
463 elem = lo_map_alloc_elem(&lo_data(req)->dirp_map);
464 if (!elem) {
465 return -1;
466 }
467
468 elem->dirp = dirp;
469 return elem - lo_data(req)->dirp_map.elems;
470 }
471
472 /* Assumes lo->mutex is held */
473 static ssize_t lo_add_inode_mapping(fuse_req_t req, struct lo_inode *inode)
474 {
475 struct lo_map_elem *elem;
476
477 elem = lo_map_alloc_elem(&lo_data(req)->ino_map);
478 if (!elem) {
479 return -1;
480 }
481
482 elem->inode = inode;
483 return elem - lo_data(req)->ino_map.elems;
484 }
485
486 static void lo_inode_put(struct lo_data *lo, struct lo_inode **inodep)
487 {
488 struct lo_inode *inode = *inodep;
489
490 if (!inode) {
491 return;
492 }
493
494 *inodep = NULL;
495
496 if (g_atomic_int_dec_and_test(&inode->refcount)) {
497 close(inode->fd);
498 free(inode);
499 }
500 }
501
502 /* Caller must release refcount using lo_inode_put() */
503 static struct lo_inode *lo_inode(fuse_req_t req, fuse_ino_t ino)
504 {
505 struct lo_data *lo = lo_data(req);
506 struct lo_map_elem *elem;
507
508 pthread_mutex_lock(&lo->mutex);
509 elem = lo_map_get(&lo->ino_map, ino);
510 if (elem) {
511 g_atomic_int_inc(&elem->inode->refcount);
512 }
513 pthread_mutex_unlock(&lo->mutex);
514
515 if (!elem) {
516 return NULL;
517 }
518
519 return elem->inode;
520 }
521
522 /*
523 * TODO Remove this helper and force callers to hold an inode refcount until
524 * they are done with the fd. This will be done in a later patch to make
525 * review easier.
526 */
527 static int lo_fd(fuse_req_t req, fuse_ino_t ino)
528 {
529 struct lo_inode *inode = lo_inode(req, ino);
530 int fd;
531
532 if (!inode) {
533 return -1;
534 }
535
536 fd = inode->fd;
537 lo_inode_put(lo_data(req), &inode);
538 return fd;
539 }
540
541 static void lo_init(void *userdata, struct fuse_conn_info *conn)
542 {
543 struct lo_data *lo = (struct lo_data *)userdata;
544
545 if (conn->capable & FUSE_CAP_EXPORT_SUPPORT) {
546 conn->want |= FUSE_CAP_EXPORT_SUPPORT;
547 }
548
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;
552 }
553 if (conn->capable & FUSE_CAP_FLOCK_LOCKS) {
554 if (lo->flock) {
555 fuse_log(FUSE_LOG_DEBUG, "lo_init: activating flock locks\n");
556 conn->want |= FUSE_CAP_FLOCK_LOCKS;
557 } else {
558 fuse_log(FUSE_LOG_DEBUG, "lo_init: disabling flock locks\n");
559 conn->want &= ~FUSE_CAP_FLOCK_LOCKS;
560 }
561 }
562
563 if (conn->capable & FUSE_CAP_POSIX_LOCKS) {
564 if (lo->posix_lock) {
565 fuse_log(FUSE_LOG_DEBUG, "lo_init: activating posix locks\n");
566 conn->want |= FUSE_CAP_POSIX_LOCKS;
567 } else {
568 fuse_log(FUSE_LOG_DEBUG, "lo_init: disabling posix locks\n");
569 conn->want &= ~FUSE_CAP_POSIX_LOCKS;
570 }
571 }
572
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;
577 }
578 }
579
580 static void lo_getattr(fuse_req_t req, fuse_ino_t ino,
581 struct fuse_file_info *fi)
582 {
583 int res;
584 struct stat buf;
585 struct lo_data *lo = lo_data(req);
586
587 (void)fi;
588
589 res =
590 fstatat(lo_fd(req, ino), "", &buf, AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW);
591 if (res == -1) {
592 return (void)fuse_reply_err(req, errno);
593 }
594
595 fuse_reply_attr(req, &buf, lo->timeout);
596 }
597
598 static int lo_fi_fd(fuse_req_t req, struct fuse_file_info *fi)
599 {
600 struct lo_data *lo = lo_data(req);
601 struct lo_map_elem *elem;
602
603 pthread_mutex_lock(&lo->mutex);
604 elem = lo_map_get(&lo->fd_map, fi->fh);
605 pthread_mutex_unlock(&lo->mutex);
606
607 if (!elem) {
608 return -1;
609 }
610
611 return elem->fd;
612 }
613
614 static void lo_setattr(fuse_req_t req, fuse_ino_t ino, struct stat *attr,
615 int valid, struct fuse_file_info *fi)
616 {
617 int saverr;
618 char procname[64];
619 struct lo_data *lo = lo_data(req);
620 struct lo_inode *inode;
621 int ifd;
622 int res;
623 int fd;
624
625 inode = lo_inode(req, ino);
626 if (!inode) {
627 fuse_reply_err(req, EBADF);
628 return;
629 }
630
631 ifd = inode->fd;
632
633 /* If fi->fh is invalid we'll report EBADF later */
634 if (fi) {
635 fd = lo_fi_fd(req, fi);
636 }
637
638 if (valid & FUSE_SET_ATTR_MODE) {
639 if (fi) {
640 res = fchmod(fd, attr->st_mode);
641 } else {
642 sprintf(procname, "%i", ifd);
643 res = fchmodat(lo->proc_self_fd, procname, attr->st_mode, 0);
644 }
645 if (res == -1) {
646 goto out_err;
647 }
648 }
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;
652
653 res = fchownat(ifd, "", uid, gid, AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW);
654 if (res == -1) {
655 goto out_err;
656 }
657 }
658 if (valid & FUSE_SET_ATTR_SIZE) {
659 int truncfd;
660
661 if (fi) {
662 truncfd = fd;
663 } else {
664 sprintf(procname, "%i", ifd);
665 truncfd = openat(lo->proc_self_fd, procname, O_RDWR);
666 if (truncfd < 0) {
667 goto out_err;
668 }
669 }
670
671 res = ftruncate(truncfd, attr->st_size);
672 if (!fi) {
673 saverr = errno;
674 close(truncfd);
675 errno = saverr;
676 }
677 if (res == -1) {
678 goto out_err;
679 }
680 }
681 if (valid & (FUSE_SET_ATTR_ATIME | FUSE_SET_ATTR_MTIME)) {
682 struct timespec tv[2];
683
684 tv[0].tv_sec = 0;
685 tv[1].tv_sec = 0;
686 tv[0].tv_nsec = UTIME_OMIT;
687 tv[1].tv_nsec = UTIME_OMIT;
688
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;
693 }
694
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;
699 }
700
701 if (fi) {
702 res = futimens(fd, tv);
703 } else {
704 sprintf(procname, "%i", inode->fd);
705 res = utimensat(lo->proc_self_fd, procname, tv, 0);
706 }
707 if (res == -1) {
708 goto out_err;
709 }
710 }
711 lo_inode_put(lo, &inode);
712
713 return lo_getattr(req, ino, fi);
714
715 out_err:
716 saverr = errno;
717 lo_inode_put(lo, &inode);
718 fuse_reply_err(req, saverr);
719 }
720
721 static struct lo_inode *lo_find(struct lo_data *lo, struct stat *st)
722 {
723 struct lo_inode *p;
724 struct lo_key key = {
725 .ino = st->st_ino,
726 .dev = st->st_dev,
727 };
728
729 pthread_mutex_lock(&lo->mutex);
730 p = g_hash_table_lookup(lo->inodes, &key);
731 if (p) {
732 assert(p->nlookup > 0);
733 p->nlookup++;
734 g_atomic_int_inc(&p->refcount);
735 }
736 pthread_mutex_unlock(&lo->mutex);
737
738 return p;
739 }
740
741 /* value_destroy_func for posix_locks GHashTable */
742 static void posix_locks_value_destroy(gpointer data)
743 {
744 struct lo_inode_plock *plock = data;
745
746 /*
747 * We had used open() for locks and had only one fd. So
748 * closing this fd should release all OFD locks.
749 */
750 close(plock->fd);
751 free(plock);
752 }
753
754 /*
755 * Increments nlookup and caller must release refcount using
756 * lo_inode_put(&parent).
757 */
758 static int lo_do_lookup(fuse_req_t req, fuse_ino_t parent, const char *name,
759 struct fuse_entry_param *e)
760 {
761 int newfd;
762 int res;
763 int saverr;
764 struct lo_data *lo = lo_data(req);
765 struct lo_inode *inode = NULL;
766 struct lo_inode *dir = lo_inode(req, parent);
767
768 /*
769 * name_to_handle_at() and open_by_handle_at() can reach here with fuse
770 * mount point in guest, but we don't have its inode info in the
771 * ino_map.
772 */
773 if (!dir) {
774 return ENOENT;
775 }
776
777 memset(e, 0, sizeof(*e));
778 e->attr_timeout = lo->timeout;
779 e->entry_timeout = lo->timeout;
780
781 /* Do not allow escaping root directory */
782 if (dir == &lo->root && strcmp(name, "..") == 0) {
783 name = ".";
784 }
785
786 newfd = openat(dir->fd, name, O_PATH | O_NOFOLLOW);
787 if (newfd == -1) {
788 goto out_err;
789 }
790
791 res = fstatat(newfd, "", &e->attr, AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW);
792 if (res == -1) {
793 goto out_err;
794 }
795
796 inode = lo_find(lo, &e->attr);
797 if (inode) {
798 close(newfd);
799 } else {
800 inode = calloc(1, sizeof(struct lo_inode));
801 if (!inode) {
802 goto out_err;
803 }
804
805 /* cache only filetype */
806 inode->filetype = (e->attr.st_mode & S_IFMT);
807
808 /*
809 * One for the caller and one for nlookup (released in
810 * unref_inode_lolocked())
811 */
812 g_atomic_int_set(&inode->refcount, 2);
813
814 inode->nlookup = 1;
815 inode->fd = newfd;
816 inode->key.ino = e->attr.st_ino;
817 inode->key.dev = e->attr.st_dev;
818 pthread_mutex_init(&inode->plock_mutex, NULL);
819 inode->posix_locks = g_hash_table_new_full(
820 g_direct_hash, g_direct_equal, NULL, posix_locks_value_destroy);
821
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);
826 }
827 e->ino = inode->fuse_ino;
828 lo_inode_put(lo, &inode);
829 lo_inode_put(lo, &dir);
830
831 fuse_log(FUSE_LOG_DEBUG, " %lli/%s -> %lli\n", (unsigned long long)parent,
832 name, (unsigned long long)e->ino);
833
834 return 0;
835
836 out_err:
837 saverr = errno;
838 if (newfd != -1) {
839 close(newfd);
840 }
841 lo_inode_put(lo, &inode);
842 lo_inode_put(lo, &dir);
843 return saverr;
844 }
845
846 static void lo_lookup(fuse_req_t req, fuse_ino_t parent, const char *name)
847 {
848 struct fuse_entry_param e;
849 int err;
850
851 fuse_log(FUSE_LOG_DEBUG, "lo_lookup(parent=%" PRIu64 ", name=%s)\n", parent,
852 name);
853
854 /*
855 * Don't use is_safe_path_component(), allow "." and ".." for NFS export
856 * support.
857 */
858 if (strchr(name, '/')) {
859 fuse_reply_err(req, EINVAL);
860 return;
861 }
862
863 err = lo_do_lookup(req, parent, name, &e);
864 if (err) {
865 fuse_reply_err(req, err);
866 } else {
867 fuse_reply_entry(req, &e);
868 }
869 }
870
871 /*
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.
875 */
876 #ifdef SYS_setresgid32
877 #define OURSYS_setresgid SYS_setresgid32
878 #else
879 #define OURSYS_setresgid SYS_setresgid
880 #endif
881
882 #ifdef SYS_setresuid32
883 #define OURSYS_setresuid SYS_setresuid32
884 #else
885 #define OURSYS_setresuid SYS_setresuid
886 #endif
887
888 /*
889 * Change to uid/gid of caller so that file is created with
890 * ownership of caller.
891 * TODO: What about selinux context?
892 */
893 static int lo_change_cred(fuse_req_t req, struct lo_cred *old)
894 {
895 int res;
896
897 old->euid = geteuid();
898 old->egid = getegid();
899
900 res = syscall(OURSYS_setresgid, -1, fuse_req_ctx(req)->gid, -1);
901 if (res == -1) {
902 return errno;
903 }
904
905 res = syscall(OURSYS_setresuid, -1, fuse_req_ctx(req)->uid, -1);
906 if (res == -1) {
907 int errno_save = errno;
908
909 syscall(OURSYS_setresgid, -1, old->egid, -1);
910 return errno_save;
911 }
912
913 return 0;
914 }
915
916 /* Regain Privileges */
917 static void lo_restore_cred(struct lo_cred *old)
918 {
919 int res;
920
921 res = syscall(OURSYS_setresuid, -1, old->euid, -1);
922 if (res == -1) {
923 fuse_log(FUSE_LOG_ERR, "seteuid(%u): %m\n", old->euid);
924 exit(1);
925 }
926
927 res = syscall(OURSYS_setresgid, -1, old->egid, -1);
928 if (res == -1) {
929 fuse_log(FUSE_LOG_ERR, "setegid(%u): %m\n", old->egid);
930 exit(1);
931 }
932 }
933
934 static void lo_mknod_symlink(fuse_req_t req, fuse_ino_t parent,
935 const char *name, mode_t mode, dev_t rdev,
936 const char *link)
937 {
938 int res;
939 int saverr;
940 struct lo_data *lo = lo_data(req);
941 struct lo_inode *dir;
942 struct fuse_entry_param e;
943 struct lo_cred old = {};
944
945 if (!is_safe_path_component(name)) {
946 fuse_reply_err(req, EINVAL);
947 return;
948 }
949
950 dir = lo_inode(req, parent);
951 if (!dir) {
952 fuse_reply_err(req, EBADF);
953 return;
954 }
955
956 saverr = lo_change_cred(req, &old);
957 if (saverr) {
958 goto out;
959 }
960
961 res = mknod_wrapper(dir->fd, name, link, mode, rdev);
962
963 saverr = errno;
964
965 lo_restore_cred(&old);
966
967 if (res == -1) {
968 goto out;
969 }
970
971 saverr = lo_do_lookup(req, parent, name, &e);
972 if (saverr) {
973 goto out;
974 }
975
976 fuse_log(FUSE_LOG_DEBUG, " %lli/%s -> %lli\n", (unsigned long long)parent,
977 name, (unsigned long long)e.ino);
978
979 fuse_reply_entry(req, &e);
980 lo_inode_put(lo, &dir);
981 return;
982
983 out:
984 lo_inode_put(lo, &dir);
985 fuse_reply_err(req, saverr);
986 }
987
988 static void lo_mknod(fuse_req_t req, fuse_ino_t parent, const char *name,
989 mode_t mode, dev_t rdev)
990 {
991 lo_mknod_symlink(req, parent, name, mode, rdev, NULL);
992 }
993
994 static void lo_mkdir(fuse_req_t req, fuse_ino_t parent, const char *name,
995 mode_t mode)
996 {
997 lo_mknod_symlink(req, parent, name, S_IFDIR | mode, 0, NULL);
998 }
999
1000 static void lo_symlink(fuse_req_t req, const char *link, fuse_ino_t parent,
1001 const char *name)
1002 {
1003 lo_mknod_symlink(req, parent, name, S_IFLNK, 0, link);
1004 }
1005
1006 static void lo_link(fuse_req_t req, fuse_ino_t ino, fuse_ino_t parent,
1007 const char *name)
1008 {
1009 int res;
1010 struct lo_data *lo = lo_data(req);
1011 struct lo_inode *parent_inode;
1012 struct lo_inode *inode;
1013 struct fuse_entry_param e;
1014 char procname[64];
1015 int saverr;
1016
1017 if (!is_safe_path_component(name)) {
1018 fuse_reply_err(req, EINVAL);
1019 return;
1020 }
1021
1022 parent_inode = lo_inode(req, parent);
1023 inode = lo_inode(req, ino);
1024 if (!parent_inode || !inode) {
1025 errno = EBADF;
1026 goto out_err;
1027 }
1028
1029 memset(&e, 0, sizeof(struct fuse_entry_param));
1030 e.attr_timeout = lo->timeout;
1031 e.entry_timeout = lo->timeout;
1032
1033 sprintf(procname, "%i", inode->fd);
1034 res = linkat(lo->proc_self_fd, procname, parent_inode->fd, name,
1035 AT_SYMLINK_FOLLOW);
1036 if (res == -1) {
1037 goto out_err;
1038 }
1039
1040 res = fstatat(inode->fd, "", &e.attr, AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW);
1041 if (res == -1) {
1042 goto out_err;
1043 }
1044
1045 pthread_mutex_lock(&lo->mutex);
1046 inode->nlookup++;
1047 pthread_mutex_unlock(&lo->mutex);
1048 e.ino = inode->fuse_ino;
1049
1050 fuse_log(FUSE_LOG_DEBUG, " %lli/%s -> %lli\n", (unsigned long long)parent,
1051 name, (unsigned long long)e.ino);
1052
1053 fuse_reply_entry(req, &e);
1054 lo_inode_put(lo, &parent_inode);
1055 lo_inode_put(lo, &inode);
1056 return;
1057
1058 out_err:
1059 saverr = errno;
1060 lo_inode_put(lo, &parent_inode);
1061 lo_inode_put(lo, &inode);
1062 fuse_reply_err(req, saverr);
1063 }
1064
1065 /* Increments nlookup and caller must release refcount using lo_inode_put() */
1066 static struct lo_inode *lookup_name(fuse_req_t req, fuse_ino_t parent,
1067 const char *name)
1068 {
1069 int res;
1070 struct stat attr;
1071
1072 res = fstatat(lo_fd(req, parent), name, &attr,
1073 AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW);
1074 if (res == -1) {
1075 return NULL;
1076 }
1077
1078 return lo_find(lo_data(req), &attr);
1079 }
1080
1081 static void lo_rmdir(fuse_req_t req, fuse_ino_t parent, const char *name)
1082 {
1083 int res;
1084 struct lo_inode *inode;
1085 struct lo_data *lo = lo_data(req);
1086
1087 if (!is_safe_path_component(name)) {
1088 fuse_reply_err(req, EINVAL);
1089 return;
1090 }
1091
1092 inode = lookup_name(req, parent, name);
1093 if (!inode) {
1094 fuse_reply_err(req, EIO);
1095 return;
1096 }
1097
1098 res = unlinkat(lo_fd(req, parent), name, AT_REMOVEDIR);
1099
1100 fuse_reply_err(req, res == -1 ? errno : 0);
1101 unref_inode_lolocked(lo, inode, 1);
1102 lo_inode_put(lo, &inode);
1103 }
1104
1105 static void lo_rename(fuse_req_t req, fuse_ino_t parent, const char *name,
1106 fuse_ino_t newparent, const char *newname,
1107 unsigned int flags)
1108 {
1109 int res;
1110 struct lo_inode *parent_inode;
1111 struct lo_inode *newparent_inode;
1112 struct lo_inode *oldinode = NULL;
1113 struct lo_inode *newinode = NULL;
1114 struct lo_data *lo = lo_data(req);
1115
1116 if (!is_safe_path_component(name) || !is_safe_path_component(newname)) {
1117 fuse_reply_err(req, EINVAL);
1118 return;
1119 }
1120
1121 parent_inode = lo_inode(req, parent);
1122 newparent_inode = lo_inode(req, newparent);
1123 if (!parent_inode || !newparent_inode) {
1124 fuse_reply_err(req, EBADF);
1125 goto out;
1126 }
1127
1128 oldinode = lookup_name(req, parent, name);
1129 newinode = lookup_name(req, newparent, newname);
1130
1131 if (!oldinode) {
1132 fuse_reply_err(req, EIO);
1133 goto out;
1134 }
1135
1136 if (flags) {
1137 #ifndef SYS_renameat2
1138 fuse_reply_err(req, EINVAL);
1139 #else
1140 res = syscall(SYS_renameat2, parent_inode->fd, name,
1141 newparent_inode->fd, newname, flags);
1142 if (res == -1 && errno == ENOSYS) {
1143 fuse_reply_err(req, EINVAL);
1144 } else {
1145 fuse_reply_err(req, res == -1 ? errno : 0);
1146 }
1147 #endif
1148 goto out;
1149 }
1150
1151 res = renameat(parent_inode->fd, name, newparent_inode->fd, newname);
1152
1153 fuse_reply_err(req, res == -1 ? errno : 0);
1154 out:
1155 unref_inode_lolocked(lo, oldinode, 1);
1156 unref_inode_lolocked(lo, newinode, 1);
1157 lo_inode_put(lo, &oldinode);
1158 lo_inode_put(lo, &newinode);
1159 lo_inode_put(lo, &parent_inode);
1160 lo_inode_put(lo, &newparent_inode);
1161 }
1162
1163 static void lo_unlink(fuse_req_t req, fuse_ino_t parent, const char *name)
1164 {
1165 int res;
1166 struct lo_inode *inode;
1167 struct lo_data *lo = lo_data(req);
1168
1169 if (!is_safe_path_component(name)) {
1170 fuse_reply_err(req, EINVAL);
1171 return;
1172 }
1173
1174 inode = lookup_name(req, parent, name);
1175 if (!inode) {
1176 fuse_reply_err(req, EIO);
1177 return;
1178 }
1179
1180 res = unlinkat(lo_fd(req, parent), name, 0);
1181
1182 fuse_reply_err(req, res == -1 ? errno : 0);
1183 unref_inode_lolocked(lo, inode, 1);
1184 lo_inode_put(lo, &inode);
1185 }
1186
1187 /* To be called with lo->mutex held */
1188 static void unref_inode(struct lo_data *lo, struct lo_inode *inode, uint64_t n)
1189 {
1190 if (!inode) {
1191 return;
1192 }
1193
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");
1201 }
1202 g_hash_table_destroy(inode->posix_locks);
1203 pthread_mutex_destroy(&inode->plock_mutex);
1204
1205 /* Drop our refcount from lo_do_lookup() */
1206 lo_inode_put(lo, &inode);
1207 }
1208 }
1209
1210 static void unref_inode_lolocked(struct lo_data *lo, struct lo_inode *inode,
1211 uint64_t n)
1212 {
1213 if (!inode) {
1214 return;
1215 }
1216
1217 pthread_mutex_lock(&lo->mutex);
1218 unref_inode(lo, inode, n);
1219 pthread_mutex_unlock(&lo->mutex);
1220 }
1221
1222 static void lo_forget_one(fuse_req_t req, fuse_ino_t ino, uint64_t nlookup)
1223 {
1224 struct lo_data *lo = lo_data(req);
1225 struct lo_inode *inode;
1226
1227 inode = lo_inode(req, ino);
1228 if (!inode) {
1229 return;
1230 }
1231
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);
1235
1236 unref_inode_lolocked(lo, inode, nlookup);
1237 lo_inode_put(lo, &inode);
1238 }
1239
1240 static void lo_forget(fuse_req_t req, fuse_ino_t ino, uint64_t nlookup)
1241 {
1242 lo_forget_one(req, ino, nlookup);
1243 fuse_reply_none(req);
1244 }
1245
1246 static void lo_forget_multi(fuse_req_t req, size_t count,
1247 struct fuse_forget_data *forgets)
1248 {
1249 int i;
1250
1251 for (i = 0; i < count; i++) {
1252 lo_forget_one(req, forgets[i].ino, forgets[i].nlookup);
1253 }
1254 fuse_reply_none(req);
1255 }
1256
1257 static void lo_readlink(fuse_req_t req, fuse_ino_t ino)
1258 {
1259 char buf[PATH_MAX + 1];
1260 int res;
1261
1262 res = readlinkat(lo_fd(req, ino), "", buf, sizeof(buf));
1263 if (res == -1) {
1264 return (void)fuse_reply_err(req, errno);
1265 }
1266
1267 if (res == sizeof(buf)) {
1268 return (void)fuse_reply_err(req, ENAMETOOLONG);
1269 }
1270
1271 buf[res] = '\0';
1272
1273 fuse_reply_readlink(req, buf);
1274 }
1275
1276 struct lo_dirp {
1277 gint refcount;
1278 DIR *dp;
1279 struct dirent *entry;
1280 off_t offset;
1281 };
1282
1283 static void lo_dirp_put(struct lo_dirp **dp)
1284 {
1285 struct lo_dirp *d = *dp;
1286
1287 if (!d) {
1288 return;
1289 }
1290 *dp = NULL;
1291
1292 if (g_atomic_int_dec_and_test(&d->refcount)) {
1293 closedir(d->dp);
1294 free(d);
1295 }
1296 }
1297
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)
1300 {
1301 struct lo_data *lo = lo_data(req);
1302 struct lo_map_elem *elem;
1303
1304 pthread_mutex_lock(&lo->mutex);
1305 elem = lo_map_get(&lo->dirp_map, fi->fh);
1306 if (elem) {
1307 g_atomic_int_inc(&elem->dirp->refcount);
1308 }
1309 pthread_mutex_unlock(&lo->mutex);
1310 if (!elem) {
1311 return NULL;
1312 }
1313
1314 return elem->dirp;
1315 }
1316
1317 static void lo_opendir(fuse_req_t req, fuse_ino_t ino,
1318 struct fuse_file_info *fi)
1319 {
1320 int error = ENOMEM;
1321 struct lo_data *lo = lo_data(req);
1322 struct lo_dirp *d;
1323 int fd;
1324 ssize_t fh;
1325
1326 d = calloc(1, sizeof(struct lo_dirp));
1327 if (d == NULL) {
1328 goto out_err;
1329 }
1330
1331 fd = openat(lo_fd(req, ino), ".", O_RDONLY);
1332 if (fd == -1) {
1333 goto out_errno;
1334 }
1335
1336 d->dp = fdopendir(fd);
1337 if (d->dp == NULL) {
1338 goto out_errno;
1339 }
1340
1341 d->offset = 0;
1342 d->entry = NULL;
1343
1344 g_atomic_int_set(&d->refcount, 1); /* paired with lo_releasedir() */
1345 pthread_mutex_lock(&lo->mutex);
1346 fh = lo_add_dirp_mapping(req, d);
1347 pthread_mutex_unlock(&lo->mutex);
1348 if (fh == -1) {
1349 goto out_err;
1350 }
1351
1352 fi->fh = fh;
1353 if (lo->cache == CACHE_ALWAYS) {
1354 fi->cache_readdir = 1;
1355 }
1356 fuse_reply_open(req, fi);
1357 return;
1358
1359 out_errno:
1360 error = errno;
1361 out_err:
1362 if (d) {
1363 if (d->dp) {
1364 closedir(d->dp);
1365 } else if (fd != -1) {
1366 close(fd);
1367 }
1368 free(d);
1369 }
1370 fuse_reply_err(req, error);
1371 }
1372
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)
1375 {
1376 struct lo_data *lo = lo_data(req);
1377 struct lo_dirp *d = NULL;
1378 struct lo_inode *dinode;
1379 char *buf = NULL;
1380 char *p;
1381 size_t rem = size;
1382 int err = EBADF;
1383
1384 dinode = lo_inode(req, ino);
1385 if (!dinode) {
1386 goto error;
1387 }
1388
1389 d = lo_dirp(req, fi);
1390 if (!d) {
1391 goto error;
1392 }
1393
1394 err = ENOMEM;
1395 buf = calloc(1, size);
1396 if (!buf) {
1397 goto error;
1398 }
1399 p = buf;
1400
1401 if (offset != d->offset) {
1402 seekdir(d->dp, offset);
1403 d->entry = NULL;
1404 d->offset = offset;
1405 }
1406 while (1) {
1407 size_t entsize;
1408 off_t nextoff;
1409 const char *name;
1410
1411 if (!d->entry) {
1412 errno = 0;
1413 d->entry = readdir(d->dp);
1414 if (!d->entry) {
1415 if (errno) { /* Error */
1416 err = errno;
1417 goto error;
1418 } else { /* End of stream */
1419 break;
1420 }
1421 }
1422 }
1423 nextoff = d->entry->d_off;
1424 name = d->entry->d_name;
1425
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,
1430 };
1431
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;
1436 }
1437
1438 if (plus) {
1439 if (!is_dot_or_dotdot(name)) {
1440 err = lo_do_lookup(req, ino, name, &e);
1441 if (err) {
1442 goto error;
1443 }
1444 entry_ino = e.ino;
1445 }
1446
1447 entsize = fuse_add_direntry_plus(req, p, rem, name, &e, nextoff);
1448 } else {
1449 entsize = fuse_add_direntry(req, p, rem, name, &e.attr, nextoff);
1450 }
1451 if (entsize > rem) {
1452 if (entry_ino != 0) {
1453 lo_forget_one(req, entry_ino, 1);
1454 }
1455 break;
1456 }
1457
1458 p += entsize;
1459 rem -= entsize;
1460
1461 d->entry = NULL;
1462 d->offset = nextoff;
1463 }
1464
1465 err = 0;
1466 error:
1467 lo_dirp_put(&d);
1468 lo_inode_put(lo, &dinode);
1469
1470 /*
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.
1475 */
1476 if (err && rem == size) {
1477 fuse_reply_err(req, err);
1478 } else {
1479 fuse_reply_buf(req, buf, size - rem);
1480 }
1481 free(buf);
1482 }
1483
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)
1486 {
1487 lo_do_readdir(req, ino, size, offset, fi, 0);
1488 }
1489
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)
1492 {
1493 lo_do_readdir(req, ino, size, offset, fi, 1);
1494 }
1495
1496 static void lo_releasedir(fuse_req_t req, fuse_ino_t ino,
1497 struct fuse_file_info *fi)
1498 {
1499 struct lo_data *lo = lo_data(req);
1500 struct lo_map_elem *elem;
1501 struct lo_dirp *d;
1502
1503 (void)ino;
1504
1505 pthread_mutex_lock(&lo->mutex);
1506 elem = lo_map_get(&lo->dirp_map, fi->fh);
1507 if (!elem) {
1508 pthread_mutex_unlock(&lo->mutex);
1509 fuse_reply_err(req, EBADF);
1510 return;
1511 }
1512
1513 d = elem->dirp;
1514 lo_map_remove(&lo->dirp_map, fi->fh);
1515 pthread_mutex_unlock(&lo->mutex);
1516
1517 lo_dirp_put(&d); /* paired with lo_opendir() */
1518
1519 fuse_reply_err(req, 0);
1520 }
1521
1522 static void update_open_flags(int writeback, int allow_direct_io,
1523 struct fuse_file_info *fi)
1524 {
1525 /*
1526 * With writeback cache, kernel may send read requests even
1527 * when userspace opened write-only
1528 */
1529 if (writeback && (fi->flags & O_ACCMODE) == O_WRONLY) {
1530 fi->flags &= ~O_ACCMODE;
1531 fi->flags |= O_RDWR;
1532 }
1533
1534 /*
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
1541 */
1542 if (writeback && (fi->flags & O_APPEND)) {
1543 fi->flags &= ~O_APPEND;
1544 }
1545
1546 /*
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.
1551 */
1552 if (!allow_direct_io) {
1553 fi->flags &= ~O_DIRECT;
1554 }
1555 }
1556
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)
1559 {
1560 int fd;
1561 struct lo_data *lo = lo_data(req);
1562 struct lo_inode *parent_inode;
1563 struct fuse_entry_param e;
1564 int err;
1565 struct lo_cred old = {};
1566
1567 fuse_log(FUSE_LOG_DEBUG, "lo_create(parent=%" PRIu64 ", name=%s)\n", parent,
1568 name);
1569
1570 if (!is_safe_path_component(name)) {
1571 fuse_reply_err(req, EINVAL);
1572 return;
1573 }
1574
1575 parent_inode = lo_inode(req, parent);
1576 if (!parent_inode) {
1577 fuse_reply_err(req, EBADF);
1578 return;
1579 }
1580
1581 err = lo_change_cred(req, &old);
1582 if (err) {
1583 goto out;
1584 }
1585
1586 update_open_flags(lo->writeback, lo->allow_direct_io, fi);
1587
1588 fd = openat(parent_inode->fd, name, (fi->flags | O_CREAT) & ~O_NOFOLLOW,
1589 mode);
1590 err = fd == -1 ? errno : 0;
1591 lo_restore_cred(&old);
1592
1593 if (!err) {
1594 ssize_t fh;
1595
1596 pthread_mutex_lock(&lo->mutex);
1597 fh = lo_add_fd_mapping(req, fd);
1598 pthread_mutex_unlock(&lo->mutex);
1599 if (fh == -1) {
1600 close(fd);
1601 err = ENOMEM;
1602 goto out;
1603 }
1604
1605 fi->fh = fh;
1606 err = lo_do_lookup(req, parent, name, &e);
1607 }
1608 if (lo->cache == CACHE_NONE) {
1609 fi->direct_io = 1;
1610 } else if (lo->cache == CACHE_ALWAYS) {
1611 fi->keep_cache = 1;
1612 }
1613
1614 out:
1615 lo_inode_put(lo, &parent_inode);
1616
1617 if (err) {
1618 fuse_reply_err(req, err);
1619 } else {
1620 fuse_reply_create(req, &e, fi);
1621 }
1622 }
1623
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)
1629 {
1630 struct lo_inode_plock *plock;
1631 char procname[64];
1632 int fd;
1633
1634 plock =
1635 g_hash_table_lookup(inode->posix_locks, GUINT_TO_POINTER(lock_owner));
1636
1637 if (plock) {
1638 return plock;
1639 }
1640
1641 plock = malloc(sizeof(struct lo_inode_plock));
1642 if (!plock) {
1643 *err = ENOMEM;
1644 return NULL;
1645 }
1646
1647 /* Open another instance of file which can be used for ofd locks. */
1648 sprintf(procname, "%i", inode->fd);
1649
1650 /* TODO: What if file is not writable? */
1651 fd = openat(lo->proc_self_fd, procname, O_RDWR);
1652 if (fd == -1) {
1653 *err = errno;
1654 free(plock);
1655 return NULL;
1656 }
1657
1658 plock->lock_owner = lock_owner;
1659 plock->fd = fd;
1660 g_hash_table_insert(inode->posix_locks, GUINT_TO_POINTER(plock->lock_owner),
1661 plock);
1662 return plock;
1663 }
1664
1665 static void lo_getlk(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi,
1666 struct flock *lock)
1667 {
1668 struct lo_data *lo = lo_data(req);
1669 struct lo_inode *inode;
1670 struct lo_inode_plock *plock;
1671 int ret, saverr = 0;
1672
1673 fuse_log(FUSE_LOG_DEBUG,
1674 "lo_getlk(ino=%" PRIu64 ", flags=%d)"
1675 " owner=0x%lx, l_type=%d l_start=0x%lx"
1676 " l_len=0x%lx\n",
1677 ino, fi->flags, fi->lock_owner, lock->l_type, lock->l_start,
1678 lock->l_len);
1679
1680 inode = lo_inode(req, ino);
1681 if (!inode) {
1682 fuse_reply_err(req, EBADF);
1683 return;
1684 }
1685
1686 pthread_mutex_lock(&inode->plock_mutex);
1687 plock =
1688 lookup_create_plock_ctx(lo, inode, fi->lock_owner, lock->l_pid, &ret);
1689 if (!plock) {
1690 saverr = ret;
1691 goto out;
1692 }
1693
1694 ret = fcntl(plock->fd, F_OFD_GETLK, lock);
1695 if (ret == -1) {
1696 saverr = errno;
1697 }
1698
1699 out:
1700 pthread_mutex_unlock(&inode->plock_mutex);
1701 lo_inode_put(lo, &inode);
1702
1703 if (saverr) {
1704 fuse_reply_err(req, saverr);
1705 } else {
1706 fuse_reply_lock(req, lock);
1707 }
1708 }
1709
1710 static void lo_setlk(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi,
1711 struct flock *lock, int sleep)
1712 {
1713 struct lo_data *lo = lo_data(req);
1714 struct lo_inode *inode;
1715 struct lo_inode_plock *plock;
1716 int ret, saverr = 0;
1717
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);
1724
1725 if (sleep) {
1726 fuse_reply_err(req, EOPNOTSUPP);
1727 return;
1728 }
1729
1730 inode = lo_inode(req, ino);
1731 if (!inode) {
1732 fuse_reply_err(req, EBADF);
1733 return;
1734 }
1735
1736 pthread_mutex_lock(&inode->plock_mutex);
1737 plock =
1738 lookup_create_plock_ctx(lo, inode, fi->lock_owner, lock->l_pid, &ret);
1739
1740 if (!plock) {
1741 saverr = ret;
1742 goto out;
1743 }
1744
1745 /* TODO: Is it alright to modify flock? */
1746 lock->l_pid = 0;
1747 ret = fcntl(plock->fd, F_OFD_SETLK, lock);
1748 if (ret == -1) {
1749 saverr = errno;
1750 }
1751
1752 out:
1753 pthread_mutex_unlock(&inode->plock_mutex);
1754 lo_inode_put(lo, &inode);
1755
1756 fuse_reply_err(req, saverr);
1757 }
1758
1759 static void lo_fsyncdir(fuse_req_t req, fuse_ino_t ino, int datasync,
1760 struct fuse_file_info *fi)
1761 {
1762 int res;
1763 struct lo_dirp *d;
1764 int fd;
1765
1766 (void)ino;
1767
1768 d = lo_dirp(req, fi);
1769 if (!d) {
1770 fuse_reply_err(req, EBADF);
1771 return;
1772 }
1773
1774 fd = dirfd(d->dp);
1775 if (datasync) {
1776 res = fdatasync(fd);
1777 } else {
1778 res = fsync(fd);
1779 }
1780
1781 lo_dirp_put(&d);
1782
1783 fuse_reply_err(req, res == -1 ? errno : 0);
1784 }
1785
1786 static void lo_open(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi)
1787 {
1788 int fd;
1789 ssize_t fh;
1790 char buf[64];
1791 struct lo_data *lo = lo_data(req);
1792
1793 fuse_log(FUSE_LOG_DEBUG, "lo_open(ino=%" PRIu64 ", flags=%d)\n", ino,
1794 fi->flags);
1795
1796 update_open_flags(lo->writeback, lo->allow_direct_io, fi);
1797
1798 sprintf(buf, "%i", lo_fd(req, ino));
1799 fd = openat(lo->proc_self_fd, buf, fi->flags & ~O_NOFOLLOW);
1800 if (fd == -1) {
1801 return (void)fuse_reply_err(req, errno);
1802 }
1803
1804 pthread_mutex_lock(&lo->mutex);
1805 fh = lo_add_fd_mapping(req, fd);
1806 pthread_mutex_unlock(&lo->mutex);
1807 if (fh == -1) {
1808 close(fd);
1809 fuse_reply_err(req, ENOMEM);
1810 return;
1811 }
1812
1813 fi->fh = fh;
1814 if (lo->cache == CACHE_NONE) {
1815 fi->direct_io = 1;
1816 } else if (lo->cache == CACHE_ALWAYS) {
1817 fi->keep_cache = 1;
1818 }
1819 fuse_reply_open(req, fi);
1820 }
1821
1822 static void lo_release(fuse_req_t req, fuse_ino_t ino,
1823 struct fuse_file_info *fi)
1824 {
1825 struct lo_data *lo = lo_data(req);
1826 struct lo_map_elem *elem;
1827 int fd = -1;
1828
1829 (void)ino;
1830
1831 pthread_mutex_lock(&lo->mutex);
1832 elem = lo_map_get(&lo->fd_map, fi->fh);
1833 if (elem) {
1834 fd = elem->fd;
1835 elem = NULL;
1836 lo_map_remove(&lo->fd_map, fi->fh);
1837 }
1838 pthread_mutex_unlock(&lo->mutex);
1839
1840 close(fd);
1841 fuse_reply_err(req, 0);
1842 }
1843
1844 static void lo_flush(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi)
1845 {
1846 int res;
1847 (void)ino;
1848 struct lo_inode *inode;
1849
1850 inode = lo_inode(req, ino);
1851 if (!inode) {
1852 fuse_reply_err(req, EBADF);
1853 return;
1854 }
1855
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);
1860
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);
1864 }
1865
1866 static void lo_fsync(fuse_req_t req, fuse_ino_t ino, int datasync,
1867 struct fuse_file_info *fi)
1868 {
1869 int res;
1870 int fd;
1871 char *buf;
1872
1873 fuse_log(FUSE_LOG_DEBUG, "lo_fsync(ino=%" PRIu64 ", fi=0x%p)\n", ino,
1874 (void *)fi);
1875
1876 if (!fi) {
1877 struct lo_data *lo = lo_data(req);
1878
1879 res = asprintf(&buf, "%i", lo_fd(req, ino));
1880 if (res == -1) {
1881 return (void)fuse_reply_err(req, errno);
1882 }
1883
1884 fd = openat(lo->proc_self_fd, buf, O_RDWR);
1885 free(buf);
1886 if (fd == -1) {
1887 return (void)fuse_reply_err(req, errno);
1888 }
1889 } else {
1890 fd = lo_fi_fd(req, fi);
1891 }
1892
1893 if (datasync) {
1894 res = fdatasync(fd);
1895 } else {
1896 res = fsync(fd);
1897 }
1898 if (!fi) {
1899 close(fd);
1900 }
1901 fuse_reply_err(req, res == -1 ? errno : 0);
1902 }
1903
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)
1906 {
1907 struct fuse_bufvec buf = FUSE_BUFVEC_INIT(size);
1908
1909 fuse_log(FUSE_LOG_DEBUG,
1910 "lo_read(ino=%" PRIu64 ", size=%zd, "
1911 "off=%lu)\n",
1912 ino, size, (unsigned long)offset);
1913
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;
1917
1918 fuse_reply_data(req, &buf);
1919 }
1920
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)
1924 {
1925 (void)ino;
1926 ssize_t res;
1927 struct fuse_bufvec out_buf = FUSE_BUFVEC_INIT(fuse_buf_size(in_buf));
1928 bool cap_fsetid_dropped = false;
1929
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;
1933
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);
1937
1938 /*
1939 * If kill_priv is set, drop CAP_FSETID which should lead to kernel
1940 * clearing setuid/setgid on file.
1941 */
1942 if (fi->kill_priv) {
1943 res = drop_effective_cap("FSETID", &cap_fsetid_dropped);
1944 if (res != 0) {
1945 fuse_reply_err(req, res);
1946 return;
1947 }
1948 }
1949
1950 res = fuse_buf_copy(&out_buf, in_buf);
1951 if (res < 0) {
1952 fuse_reply_err(req, -res);
1953 } else {
1954 fuse_reply_write(req, (size_t)res);
1955 }
1956
1957 if (cap_fsetid_dropped) {
1958 res = gain_effective_cap("FSETID");
1959 if (res) {
1960 fuse_log(FUSE_LOG_ERR, "Failed to gain CAP_FSETID\n");
1961 }
1962 }
1963 }
1964
1965 static void lo_statfs(fuse_req_t req, fuse_ino_t ino)
1966 {
1967 int res;
1968 struct statvfs stbuf;
1969
1970 res = fstatvfs(lo_fd(req, ino), &stbuf);
1971 if (res == -1) {
1972 fuse_reply_err(req, errno);
1973 } else {
1974 fuse_reply_statfs(req, &stbuf);
1975 }
1976 }
1977
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)
1980 {
1981 int err = EOPNOTSUPP;
1982 (void)ino;
1983
1984 #ifdef CONFIG_FALLOCATE
1985 err = fallocate(lo_fi_fd(req, fi), mode, offset, length);
1986 if (err < 0) {
1987 err = errno;
1988 }
1989
1990 #elif defined(CONFIG_POSIX_FALLOCATE)
1991 if (mode) {
1992 fuse_reply_err(req, EOPNOTSUPP);
1993 return;
1994 }
1995
1996 err = posix_fallocate(lo_fi_fd(req, fi), offset, length);
1997 #endif
1998
1999 fuse_reply_err(req, err);
2000 }
2001
2002 static void lo_flock(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi,
2003 int op)
2004 {
2005 int res;
2006 (void)ino;
2007
2008 res = flock(lo_fi_fd(req, fi), op);
2009
2010 fuse_reply_err(req, res == -1 ? errno : 0);
2011 }
2012
2013 static void lo_getxattr(fuse_req_t req, fuse_ino_t ino, const char *name,
2014 size_t size)
2015 {
2016 struct lo_data *lo = lo_data(req);
2017 char *value = NULL;
2018 char procname[64];
2019 struct lo_inode *inode;
2020 ssize_t ret;
2021 int saverr;
2022 int fd = -1;
2023
2024 inode = lo_inode(req, ino);
2025 if (!inode) {
2026 fuse_reply_err(req, EBADF);
2027 return;
2028 }
2029
2030 saverr = ENOSYS;
2031 if (!lo_data(req)->xattr) {
2032 goto out;
2033 }
2034
2035 fuse_log(FUSE_LOG_DEBUG, "lo_getxattr(ino=%" PRIu64 ", name=%s size=%zd)\n",
2036 ino, name, size);
2037
2038 if (size) {
2039 value = malloc(size);
2040 if (!value) {
2041 goto out_err;
2042 }
2043 }
2044
2045 sprintf(procname, "%i", inode->fd);
2046 /*
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().
2051 */
2052 if (S_ISREG(inode->filetype) || S_ISDIR(inode->filetype)) {
2053 fd = openat(lo->proc_self_fd, procname, O_RDONLY);
2054 if (fd < 0) {
2055 goto out_err;
2056 }
2057 ret = fgetxattr(fd, name, value, size);
2058 } else {
2059 /* fchdir should not fail here */
2060 assert(fchdir(lo->proc_self_fd) == 0);
2061 ret = getxattr(procname, name, value, size);
2062 assert(fchdir(lo->root.fd) == 0);
2063 }
2064
2065 if (ret == -1) {
2066 goto out_err;
2067 }
2068 if (size) {
2069 saverr = 0;
2070 if (ret == 0) {
2071 goto out;
2072 }
2073 fuse_reply_buf(req, value, ret);
2074 } else {
2075 fuse_reply_xattr(req, ret);
2076 }
2077 out_free:
2078 free(value);
2079
2080 if (fd >= 0) {
2081 close(fd);
2082 }
2083
2084 lo_inode_put(lo, &inode);
2085 return;
2086
2087 out_err:
2088 saverr = errno;
2089 out:
2090 fuse_reply_err(req, saverr);
2091 goto out_free;
2092 }
2093
2094 static void lo_listxattr(fuse_req_t req, fuse_ino_t ino, size_t size)
2095 {
2096 struct lo_data *lo = lo_data(req);
2097 char *value = NULL;
2098 char procname[64];
2099 struct lo_inode *inode;
2100 ssize_t ret;
2101 int saverr;
2102 int fd = -1;
2103
2104 inode = lo_inode(req, ino);
2105 if (!inode) {
2106 fuse_reply_err(req, EBADF);
2107 return;
2108 }
2109
2110 saverr = ENOSYS;
2111 if (!lo_data(req)->xattr) {
2112 goto out;
2113 }
2114
2115 fuse_log(FUSE_LOG_DEBUG, "lo_listxattr(ino=%" PRIu64 ", size=%zd)\n", ino,
2116 size);
2117
2118 if (size) {
2119 value = malloc(size);
2120 if (!value) {
2121 goto out_err;
2122 }
2123 }
2124
2125 sprintf(procname, "%i", inode->fd);
2126 if (S_ISREG(inode->filetype) || S_ISDIR(inode->filetype)) {
2127 fd = openat(lo->proc_self_fd, procname, O_RDONLY);
2128 if (fd < 0) {
2129 goto out_err;
2130 }
2131 ret = flistxattr(fd, value, size);
2132 } else {
2133 /* fchdir should not fail here */
2134 assert(fchdir(lo->proc_self_fd) == 0);
2135 ret = listxattr(procname, value, size);
2136 assert(fchdir(lo->root.fd) == 0);
2137 }
2138
2139 if (ret == -1) {
2140 goto out_err;
2141 }
2142 if (size) {
2143 saverr = 0;
2144 if (ret == 0) {
2145 goto out;
2146 }
2147 fuse_reply_buf(req, value, ret);
2148 } else {
2149 fuse_reply_xattr(req, ret);
2150 }
2151 out_free:
2152 free(value);
2153
2154 if (fd >= 0) {
2155 close(fd);
2156 }
2157
2158 lo_inode_put(lo, &inode);
2159 return;
2160
2161 out_err:
2162 saverr = errno;
2163 out:
2164 fuse_reply_err(req, saverr);
2165 goto out_free;
2166 }
2167
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)
2170 {
2171 char procname[64];
2172 struct lo_data *lo = lo_data(req);
2173 struct lo_inode *inode;
2174 ssize_t ret;
2175 int saverr;
2176 int fd = -1;
2177
2178 inode = lo_inode(req, ino);
2179 if (!inode) {
2180 fuse_reply_err(req, EBADF);
2181 return;
2182 }
2183
2184 saverr = ENOSYS;
2185 if (!lo_data(req)->xattr) {
2186 goto out;
2187 }
2188
2189 fuse_log(FUSE_LOG_DEBUG, "lo_setxattr(ino=%" PRIu64
2190 ", name=%s value=%s size=%zd)\n", ino, name, value, size);
2191
2192 sprintf(procname, "%i", inode->fd);
2193 if (S_ISREG(inode->filetype) || S_ISDIR(inode->filetype)) {
2194 fd = openat(lo->proc_self_fd, procname, O_RDONLY);
2195 if (fd < 0) {
2196 saverr = errno;
2197 goto out;
2198 }
2199 ret = fsetxattr(fd, name, value, size, flags);
2200 } else {
2201 /* fchdir should not fail here */
2202 assert(fchdir(lo->proc_self_fd) == 0);
2203 ret = setxattr(procname, name, value, size, flags);
2204 assert(fchdir(lo->root.fd) == 0);
2205 }
2206
2207 saverr = ret == -1 ? errno : 0;
2208
2209 out:
2210 if (fd >= 0) {
2211 close(fd);
2212 }
2213
2214 lo_inode_put(lo, &inode);
2215 fuse_reply_err(req, saverr);
2216 }
2217
2218 static void lo_removexattr(fuse_req_t req, fuse_ino_t ino, const char *name)
2219 {
2220 char procname[64];
2221 struct lo_data *lo = lo_data(req);
2222 struct lo_inode *inode;
2223 ssize_t ret;
2224 int saverr;
2225 int fd = -1;
2226
2227 inode = lo_inode(req, ino);
2228 if (!inode) {
2229 fuse_reply_err(req, EBADF);
2230 return;
2231 }
2232
2233 saverr = ENOSYS;
2234 if (!lo_data(req)->xattr) {
2235 goto out;
2236 }
2237
2238 fuse_log(FUSE_LOG_DEBUG, "lo_removexattr(ino=%" PRIu64 ", name=%s)\n", ino,
2239 name);
2240
2241 sprintf(procname, "%i", inode->fd);
2242 if (S_ISREG(inode->filetype) || S_ISDIR(inode->filetype)) {
2243 fd = openat(lo->proc_self_fd, procname, O_RDONLY);
2244 if (fd < 0) {
2245 saverr = errno;
2246 goto out;
2247 }
2248 ret = fremovexattr(fd, name);
2249 } else {
2250 /* fchdir should not fail here */
2251 assert(fchdir(lo->proc_self_fd) == 0);
2252 ret = removexattr(procname, name);
2253 assert(fchdir(lo->root.fd) == 0);
2254 }
2255
2256 saverr = ret == -1 ? errno : 0;
2257
2258 out:
2259 if (fd >= 0) {
2260 close(fd);
2261 }
2262
2263 lo_inode_put(lo, &inode);
2264 fuse_reply_err(req, saverr);
2265 }
2266
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)
2272 {
2273 int in_fd, out_fd;
2274 ssize_t res;
2275
2276 in_fd = lo_fi_fd(req, fi_in);
2277 out_fd = lo_fi_fd(req, fi_out);
2278
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);
2284
2285 res = copy_file_range(in_fd, &off_in, out_fd, &off_out, len, flags);
2286 if (res < 0) {
2287 fuse_reply_err(req, errno);
2288 } else {
2289 fuse_reply_write(req, res);
2290 }
2291 }
2292 #endif
2293
2294 static void lo_lseek(fuse_req_t req, fuse_ino_t ino, off_t off, int whence,
2295 struct fuse_file_info *fi)
2296 {
2297 off_t res;
2298
2299 (void)ino;
2300 res = lseek(lo_fi_fd(req, fi), off, whence);
2301 if (res != -1) {
2302 fuse_reply_lseek(req, res);
2303 } else {
2304 fuse_reply_err(req, errno);
2305 }
2306 }
2307
2308 static void lo_destroy(void *userdata)
2309 {
2310 struct lo_data *lo = (struct lo_data *)userdata;
2311
2312 pthread_mutex_lock(&lo->mutex);
2313 while (true) {
2314 GHashTableIter iter;
2315 gpointer key, value;
2316
2317 g_hash_table_iter_init(&iter, lo->inodes);
2318 if (!g_hash_table_iter_next(&iter, &key, &value)) {
2319 break;
2320 }
2321
2322 struct lo_inode *inode = value;
2323 unref_inode(lo, inode, inode->nlookup);
2324 }
2325 pthread_mutex_unlock(&lo->mutex);
2326 }
2327
2328 static struct fuse_lowlevel_ops lo_oper = {
2329 .init = lo_init,
2330 .lookup = lo_lookup,
2331 .mkdir = lo_mkdir,
2332 .mknod = lo_mknod,
2333 .symlink = lo_symlink,
2334 .link = lo_link,
2335 .unlink = lo_unlink,
2336 .rmdir = lo_rmdir,
2337 .rename = lo_rename,
2338 .forget = lo_forget,
2339 .forget_multi = lo_forget_multi,
2340 .getattr = lo_getattr,
2341 .setattr = lo_setattr,
2342 .readlink = lo_readlink,
2343 .opendir = lo_opendir,
2344 .readdir = lo_readdir,
2345 .readdirplus = lo_readdirplus,
2346 .releasedir = lo_releasedir,
2347 .fsyncdir = lo_fsyncdir,
2348 .create = lo_create,
2349 .getlk = lo_getlk,
2350 .setlk = lo_setlk,
2351 .open = lo_open,
2352 .release = lo_release,
2353 .flush = lo_flush,
2354 .fsync = lo_fsync,
2355 .read = lo_read,
2356 .write_buf = lo_write_buf,
2357 .statfs = lo_statfs,
2358 .fallocate = lo_fallocate,
2359 .flock = lo_flock,
2360 .getxattr = lo_getxattr,
2361 .listxattr = lo_listxattr,
2362 .setxattr = lo_setxattr,
2363 .removexattr = lo_removexattr,
2364 #ifdef HAVE_COPY_FILE_RANGE
2365 .copy_file_range = lo_copy_file_range,
2366 #endif
2367 .lseek = lo_lseek,
2368 .destroy = lo_destroy,
2369 };
2370
2371 /* Print vhost-user.json backend program capabilities */
2372 static void print_capabilities(void)
2373 {
2374 printf("{\n");
2375 printf(" \"type\": \"fs\"\n");
2376 printf("}\n");
2377 }
2378
2379 /*
2380 * Drop all Linux capabilities because the wait parent process only needs to
2381 * sit in waitpid(2) and terminate.
2382 */
2383 static void setup_wait_parent_capabilities(void)
2384 {
2385 capng_setpid(syscall(SYS_gettid));
2386 capng_clear(CAPNG_SELECT_BOTH);
2387 capng_apply(CAPNG_SELECT_BOTH);
2388 }
2389
2390 /*
2391 * Move to a new mount, net, and pid namespaces to isolate this process.
2392 */
2393 static void setup_namespaces(struct lo_data *lo, struct fuse_session *se)
2394 {
2395 pid_t child;
2396 char template[] = "virtiofsd-XXXXXX";
2397 char *tmpdir;
2398
2399 /*
2400 * Create a new pid namespace for *child* processes. We'll have to
2401 * fork in order to enter the new pid namespace. A new mount namespace
2402 * is also needed so that we can remount /proc for the new pid
2403 * namespace.
2404 *
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.
2408 */
2409 if (unshare(CLONE_NEWPID | CLONE_NEWNS | CLONE_NEWNET) != 0) {
2410 fuse_log(FUSE_LOG_ERR, "unshare(CLONE_NEWPID | CLONE_NEWNS): %m\n");
2411 exit(1);
2412 }
2413
2414 child = fork();
2415 if (child < 0) {
2416 fuse_log(FUSE_LOG_ERR, "fork() failed: %m\n");
2417 exit(1);
2418 }
2419 if (child > 0) {
2420 pid_t waited;
2421 int wstatus;
2422
2423 setup_wait_parent_capabilities();
2424
2425 /* The parent waits for the child */
2426 do {
2427 waited = waitpid(child, &wstatus, 0);
2428 } while (waited < 0 && errno == EINTR && !se->exited);
2429
2430 /* We were terminated by a signal, see fuse_signals.c */
2431 if (se->exited) {
2432 exit(0);
2433 }
2434
2435 if (WIFEXITED(wstatus)) {
2436 exit(WEXITSTATUS(wstatus));
2437 }
2438
2439 exit(1);
2440 }
2441
2442 /* Send us SIGTERM when the parent thread terminates, see prctl(2) */
2443 prctl(PR_SET_PDEATHSIG, SIGTERM);
2444
2445 /*
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.
2448 */
2449 if (mount(NULL, "/", NULL, MS_REC | MS_SLAVE, NULL) < 0) {
2450 fuse_log(FUSE_LOG_ERR, "mount(/, MS_REC|MS_SLAVE): %m\n");
2451 exit(1);
2452 }
2453
2454 /* The child must remount /proc to use the new pid namespace */
2455 if (mount("proc", "/proc", "proc",
2456 MS_NODEV | MS_NOEXEC | MS_NOSUID | MS_RELATIME, NULL) < 0) {
2457 fuse_log(FUSE_LOG_ERR, "mount(/proc): %m\n");
2458 exit(1);
2459 }
2460
2461 tmpdir = mkdtemp(template);
2462 if (!tmpdir) {
2463 fuse_log(FUSE_LOG_ERR, "tmpdir(%s): %m\n", template);
2464 exit(1);
2465 }
2466
2467 if (mount("/proc/self/fd", tmpdir, NULL, MS_BIND, NULL) < 0) {
2468 fuse_log(FUSE_LOG_ERR, "mount(/proc/self/fd, %s, MS_BIND): %m\n",
2469 tmpdir);
2470 exit(1);
2471 }
2472
2473 /* Now we can get our /proc/self/fd directory file descriptor */
2474 lo->proc_self_fd = open(tmpdir, O_PATH);
2475 if (lo->proc_self_fd == -1) {
2476 fuse_log(FUSE_LOG_ERR, "open(%s, O_PATH): %m\n", tmpdir);
2477 exit(1);
2478 }
2479
2480 if (umount2(tmpdir, MNT_DETACH) < 0) {
2481 fuse_log(FUSE_LOG_ERR, "umount2(%s, MNT_DETACH): %m\n", tmpdir);
2482 exit(1);
2483 }
2484
2485 if (rmdir(tmpdir) < 0) {
2486 fuse_log(FUSE_LOG_ERR, "rmdir(%s): %m\n", tmpdir);
2487 }
2488 }
2489
2490 /*
2491 * Capture the capability state, we'll need to restore this for individual
2492 * threads later; see load_capng.
2493 */
2494 static void setup_capng(void)
2495 {
2496 /* Note this accesses /proc so has to happen before the sandbox */
2497 if (capng_get_caps_process()) {
2498 fuse_log(FUSE_LOG_ERR, "capng_get_caps_process\n");
2499 exit(1);
2500 }
2501 pthread_mutex_init(&cap.mutex, NULL);
2502 pthread_mutex_lock(&cap.mutex);
2503 cap.saved = capng_save_state();
2504 if (!cap.saved) {
2505 fuse_log(FUSE_LOG_ERR, "capng_save_state\n");
2506 exit(1);
2507 }
2508 pthread_mutex_unlock(&cap.mutex);
2509 }
2510
2511 static void cleanup_capng(void)
2512 {
2513 free(cap.saved);
2514 cap.saved = NULL;
2515 pthread_mutex_destroy(&cap.mutex);
2516 }
2517
2518
2519 /*
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.
2522 */
2523 static void setup_mounts(const char *source)
2524 {
2525 int oldroot;
2526 int newroot;
2527
2528 if (mount(source, source, NULL, MS_BIND | MS_REC, NULL) < 0) {
2529 fuse_log(FUSE_LOG_ERR, "mount(%s, %s, MS_BIND): %m\n", source, source);
2530 exit(1);
2531 }
2532
2533 /* This magic is based on lxc's lxc_pivot_root() */
2534 oldroot = open("/", O_DIRECTORY | O_RDONLY | O_CLOEXEC);
2535 if (oldroot < 0) {
2536 fuse_log(FUSE_LOG_ERR, "open(/): %m\n");
2537 exit(1);
2538 }
2539
2540 newroot = open(source, O_DIRECTORY | O_RDONLY | O_CLOEXEC);
2541 if (newroot < 0) {
2542 fuse_log(FUSE_LOG_ERR, "open(%s): %m\n", source);
2543 exit(1);
2544 }
2545
2546 if (fchdir(newroot) < 0) {
2547 fuse_log(FUSE_LOG_ERR, "fchdir(newroot): %m\n");
2548 exit(1);
2549 }
2550
2551 if (syscall(__NR_pivot_root, ".", ".") < 0) {
2552 fuse_log(FUSE_LOG_ERR, "pivot_root(., .): %m\n");
2553 exit(1);
2554 }
2555
2556 if (fchdir(oldroot) < 0) {
2557 fuse_log(FUSE_LOG_ERR, "fchdir(oldroot): %m\n");
2558 exit(1);
2559 }
2560
2561 if (mount("", ".", "", MS_SLAVE | MS_REC, NULL) < 0) {
2562 fuse_log(FUSE_LOG_ERR, "mount(., MS_SLAVE | MS_REC): %m\n");
2563 exit(1);
2564 }
2565
2566 if (umount2(".", MNT_DETACH) < 0) {
2567 fuse_log(FUSE_LOG_ERR, "umount2(., MNT_DETACH): %m\n");
2568 exit(1);
2569 }
2570
2571 if (fchdir(newroot) < 0) {
2572 fuse_log(FUSE_LOG_ERR, "fchdir(newroot): %m\n");
2573 exit(1);
2574 }
2575
2576 close(newroot);
2577 close(oldroot);
2578 }
2579
2580 /*
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.
2583 */
2584 static void setup_capabilities(char *modcaps_in)
2585 {
2586 char *modcaps = modcaps_in;
2587 pthread_mutex_lock(&cap.mutex);
2588 capng_restore_state(&cap.saved);
2589
2590 /*
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.
2594 *
2595 * Exclusions:
2596 * 1. CAP_LINUX_IMMUTABLE is not included because it's only used via ioctl
2597 * and we don't support that.
2598 * 2. CAP_MAC_OVERRIDE is not included because it only seems to be
2599 * used by the Smack LSM. Omit it until there is demand for it.
2600 */
2601 capng_setpid(syscall(SYS_gettid));
2602 capng_clear(CAPNG_SELECT_BOTH);
2603 if (capng_updatev(CAPNG_ADD, CAPNG_PERMITTED | CAPNG_EFFECTIVE,
2604 CAP_CHOWN,
2605 CAP_DAC_OVERRIDE,
2606 CAP_FOWNER,
2607 CAP_FSETID,
2608 CAP_SETGID,
2609 CAP_SETUID,
2610 CAP_MKNOD,
2611 CAP_SETFCAP,
2612 -1)) {
2613 fuse_log(FUSE_LOG_ERR, "%s: capng_updatev failed\n", __func__);
2614 exit(1);
2615 }
2616
2617 /*
2618 * The modcaps option is a colon separated list of caps,
2619 * each preceded by either + or -.
2620 */
2621 while (modcaps) {
2622 capng_act_t action;
2623 int cap;
2624
2625 char *next = strchr(modcaps, ':');
2626 if (next) {
2627 *next = '\0';
2628 next++;
2629 }
2630
2631 switch (modcaps[0]) {
2632 case '+':
2633 action = CAPNG_ADD;
2634 break;
2635
2636 case '-':
2637 action = CAPNG_DROP;
2638 break;
2639
2640 default:
2641 fuse_log(FUSE_LOG_ERR,
2642 "%s: Expecting '+'/'-' in modcaps but found '%c'\n",
2643 __func__, modcaps[0]);
2644 exit(1);
2645 }
2646 cap = capng_name_to_capability(modcaps + 1);
2647 if (cap < 0) {
2648 fuse_log(FUSE_LOG_ERR, "%s: Unknown capability '%s'\n", __func__,
2649 modcaps);
2650 exit(1);
2651 }
2652 if (capng_update(action, CAPNG_PERMITTED | CAPNG_EFFECTIVE, cap)) {
2653 fuse_log(FUSE_LOG_ERR, "%s: capng_update failed for '%s'\n",
2654 __func__, modcaps);
2655 exit(1);
2656 }
2657
2658 modcaps = next;
2659 }
2660 g_free(modcaps_in);
2661
2662 if (capng_apply(CAPNG_SELECT_BOTH)) {
2663 fuse_log(FUSE_LOG_ERR, "%s: capng_apply failed\n", __func__);
2664 exit(1);
2665 }
2666
2667 cap.saved = capng_save_state();
2668 if (!cap.saved) {
2669 fuse_log(FUSE_LOG_ERR, "%s: capng_save_state failed\n", __func__);
2670 exit(1);
2671 }
2672 pthread_mutex_unlock(&cap.mutex);
2673 }
2674
2675 /*
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.
2678 */
2679 static void setup_sandbox(struct lo_data *lo, struct fuse_session *se,
2680 bool enable_syslog)
2681 {
2682 setup_namespaces(lo, se);
2683 setup_mounts(lo->source);
2684 setup_seccomp(enable_syslog);
2685 setup_capabilities(g_strdup(lo->modcaps));
2686 }
2687
2688 /* Set the maximum number of open file descriptors */
2689 static void setup_nofile_rlimit(unsigned long rlimit_nofile)
2690 {
2691 struct rlimit rlim = {
2692 .rlim_cur = rlimit_nofile,
2693 .rlim_max = rlimit_nofile,
2694 };
2695
2696 if (rlimit_nofile == 0) {
2697 return; /* nothing to do */
2698 }
2699
2700 if (setrlimit(RLIMIT_NOFILE, &rlim) < 0) {
2701 /* Ignore SELinux denials */
2702 if (errno == EPERM) {
2703 return;
2704 }
2705
2706 fuse_log(FUSE_LOG_ERR, "setrlimit(RLIMIT_NOFILE): %m\n");
2707 exit(1);
2708 }
2709 }
2710
2711 static void log_func(enum fuse_log_level level, const char *fmt, va_list ap)
2712 {
2713 g_autofree char *localfmt = NULL;
2714
2715 if (current_log_level < level) {
2716 return;
2717 }
2718
2719 if (current_log_level == FUSE_LOG_DEBUG) {
2720 if (!use_syslog) {
2721 localfmt = g_strdup_printf("[%" PRId64 "] [ID: %08ld] %s",
2722 get_clock(), syscall(__NR_gettid), fmt);
2723 } else {
2724 localfmt = g_strdup_printf("[ID: %08ld] %s", syscall(__NR_gettid),
2725 fmt);
2726 }
2727 fmt = localfmt;
2728 }
2729
2730 if (use_syslog) {
2731 int priority = LOG_ERR;
2732 switch (level) {
2733 case FUSE_LOG_EMERG:
2734 priority = LOG_EMERG;
2735 break;
2736 case FUSE_LOG_ALERT:
2737 priority = LOG_ALERT;
2738 break;
2739 case FUSE_LOG_CRIT:
2740 priority = LOG_CRIT;
2741 break;
2742 case FUSE_LOG_ERR:
2743 priority = LOG_ERR;
2744 break;
2745 case FUSE_LOG_WARNING:
2746 priority = LOG_WARNING;
2747 break;
2748 case FUSE_LOG_NOTICE:
2749 priority = LOG_NOTICE;
2750 break;
2751 case FUSE_LOG_INFO:
2752 priority = LOG_INFO;
2753 break;
2754 case FUSE_LOG_DEBUG:
2755 priority = LOG_DEBUG;
2756 break;
2757 }
2758 vsyslog(priority, fmt, ap);
2759 } else {
2760 vfprintf(stderr, fmt, ap);
2761 }
2762 }
2763
2764 static void setup_root(struct lo_data *lo, struct lo_inode *root)
2765 {
2766 int fd, res;
2767 struct stat stat;
2768
2769 fd = open("/", O_PATH);
2770 if (fd == -1) {
2771 fuse_log(FUSE_LOG_ERR, "open(%s, O_PATH): %m\n", lo->source);
2772 exit(1);
2773 }
2774
2775 res = fstatat(fd, "", &stat, AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW);
2776 if (res == -1) {
2777 fuse_log(FUSE_LOG_ERR, "fstatat(%s): %m\n", lo->source);
2778 exit(1);
2779 }
2780
2781 root->filetype = S_IFDIR;
2782 root->fd = fd;
2783 root->key.ino = stat.st_ino;
2784 root->key.dev = stat.st_dev;
2785 root->nlookup = 2;
2786 g_atomic_int_set(&root->refcount, 2);
2787 }
2788
2789 static guint lo_key_hash(gconstpointer key)
2790 {
2791 const struct lo_key *lkey = key;
2792
2793 return (guint)lkey->ino + (guint)lkey->dev;
2794 }
2795
2796 static gboolean lo_key_equal(gconstpointer a, gconstpointer b)
2797 {
2798 const struct lo_key *la = a;
2799 const struct lo_key *lb = b;
2800
2801 return la->ino == lb->ino && la->dev == lb->dev;
2802 }
2803
2804 static void fuse_lo_data_cleanup(struct lo_data *lo)
2805 {
2806 if (lo->inodes) {
2807 g_hash_table_destroy(lo->inodes);
2808 }
2809 lo_map_destroy(&lo->fd_map);
2810 lo_map_destroy(&lo->dirp_map);
2811 lo_map_destroy(&lo->ino_map);
2812
2813 if (lo->proc_self_fd >= 0) {
2814 close(lo->proc_self_fd);
2815 }
2816
2817 if (lo->root.fd >= 0) {
2818 close(lo->root.fd);
2819 }
2820
2821 free(lo->source);
2822 }
2823
2824 int main(int argc, char *argv[])
2825 {
2826 struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
2827 struct fuse_session *se;
2828 struct fuse_cmdline_opts opts;
2829 struct lo_data lo = {
2830 .debug = 0,
2831 .writeback = 0,
2832 .posix_lock = 0,
2833 .allow_direct_io = 0,
2834 .proc_self_fd = -1,
2835 };
2836 struct lo_map_elem *root_elem;
2837 int ret = -1;
2838
2839 /* Don't mask creation mode, kernel already did that */
2840 umask(0);
2841
2842 pthread_mutex_init(&lo.mutex, NULL);
2843 lo.inodes = g_hash_table_new(lo_key_hash, lo_key_equal);
2844 lo.root.fd = -1;
2845 lo.root.fuse_ino = FUSE_ROOT_ID;
2846 lo.cache = CACHE_AUTO;
2847
2848 /*
2849 * Set up the ino map like this:
2850 * [0] Reserved (will not be used)
2851 * [1] Root inode
2852 */
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;
2857
2858 lo_map_init(&lo.dirp_map);
2859 lo_map_init(&lo.fd_map);
2860
2861 if (fuse_parse_cmdline(&args, &opts) != 0) {
2862 goto err_out1;
2863 }
2864 fuse_set_log_func(log_func);
2865 use_syslog = opts.syslog;
2866 if (use_syslog) {
2867 openlog("virtiofsd", LOG_PID, LOG_DAEMON);
2868 }
2869
2870 if (opts.show_help) {
2871 printf("usage: %s [options]\n\n", argv[0]);
2872 fuse_cmdline_help();
2873 printf(" -o source=PATH shared directory tree\n");
2874 fuse_lowlevel_help();
2875 ret = 0;
2876 goto err_out1;
2877 } else if (opts.show_version) {
2878 fuse_lowlevel_version();
2879 ret = 0;
2880 goto err_out1;
2881 } else if (opts.print_capabilities) {
2882 print_capabilities();
2883 ret = 0;
2884 goto err_out1;
2885 }
2886
2887 if (fuse_opt_parse(&args, &lo, lo_opts, NULL) == -1) {
2888 goto err_out1;
2889 }
2890
2891 /*
2892 * log_level is 0 if not configured via cmd options (0 is LOG_EMERG,
2893 * and we don't use this log level).
2894 */
2895 if (opts.log_level != 0) {
2896 current_log_level = opts.log_level;
2897 }
2898 lo.debug = opts.debug;
2899 if (lo.debug) {
2900 current_log_level = FUSE_LOG_DEBUG;
2901 }
2902 if (lo.source) {
2903 struct stat stat;
2904 int res;
2905
2906 res = lstat(lo.source, &stat);
2907 if (res == -1) {
2908 fuse_log(FUSE_LOG_ERR, "failed to stat source (\"%s\"): %m\n",
2909 lo.source);
2910 exit(1);
2911 }
2912 if (!S_ISDIR(stat.st_mode)) {
2913 fuse_log(FUSE_LOG_ERR, "source is not a directory\n");
2914 exit(1);
2915 }
2916 } else {
2917 lo.source = strdup("/");
2918 }
2919 if (!lo.timeout_set) {
2920 switch (lo.cache) {
2921 case CACHE_NONE:
2922 lo.timeout = 0.0;
2923 break;
2924
2925 case CACHE_AUTO:
2926 lo.timeout = 1.0;
2927 break;
2928
2929 case CACHE_ALWAYS:
2930 lo.timeout = 86400.0;
2931 break;
2932 }
2933 } else if (lo.timeout < 0) {
2934 fuse_log(FUSE_LOG_ERR, "timeout is negative (%lf)\n", lo.timeout);
2935 exit(1);
2936 }
2937
2938 se = fuse_session_new(&args, &lo_oper, sizeof(lo_oper), &lo);
2939 if (se == NULL) {
2940 goto err_out1;
2941 }
2942
2943 if (fuse_set_signal_handlers(se) != 0) {
2944 goto err_out2;
2945 }
2946
2947 if (fuse_session_mount(se) != 0) {
2948 goto err_out3;
2949 }
2950
2951 fuse_daemonize(opts.foreground);
2952
2953 setup_nofile_rlimit(opts.rlimit_nofile);
2954
2955 /* Must be before sandbox since it wants /proc */
2956 setup_capng();
2957
2958 setup_sandbox(&lo, se, opts.syslog);
2959
2960 setup_root(&lo, &lo.root);
2961 /* Block until ctrl+c or fusermount -u */
2962 ret = virtio_loop(se);
2963
2964 fuse_session_unmount(se);
2965 cleanup_capng();
2966 err_out3:
2967 fuse_remove_signal_handlers(se);
2968 err_out2:
2969 fuse_session_destroy(se);
2970 err_out1:
2971 fuse_opt_free_args(&args);
2972
2973 fuse_lo_data_cleanup(&lo);
2974
2975 return ret ? 1 : 0;
2976 }