]> git.proxmox.com Git - mirror_qemu.git/blob - tools/virtiofsd/passthrough_ll.c
Merge remote-tracking branch 'remotes/vivier2/tags/trivial-branch-for-5.2-pull-reques...
[mirror_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 = -1;
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
2397 /*
2398 * Create a new pid namespace for *child* processes. We'll have to
2399 * fork in order to enter the new pid namespace. A new mount namespace
2400 * is also needed so that we can remount /proc for the new pid
2401 * namespace.
2402 *
2403 * Our UNIX domain sockets have been created. Now we can move to
2404 * an empty network namespace to prevent TCP/IP and other network
2405 * activity in case this process is compromised.
2406 */
2407 if (unshare(CLONE_NEWPID | CLONE_NEWNS | CLONE_NEWNET) != 0) {
2408 fuse_log(FUSE_LOG_ERR, "unshare(CLONE_NEWPID | CLONE_NEWNS): %m\n");
2409 exit(1);
2410 }
2411
2412 child = fork();
2413 if (child < 0) {
2414 fuse_log(FUSE_LOG_ERR, "fork() failed: %m\n");
2415 exit(1);
2416 }
2417 if (child > 0) {
2418 pid_t waited;
2419 int wstatus;
2420
2421 setup_wait_parent_capabilities();
2422
2423 /* The parent waits for the child */
2424 do {
2425 waited = waitpid(child, &wstatus, 0);
2426 } while (waited < 0 && errno == EINTR && !se->exited);
2427
2428 /* We were terminated by a signal, see fuse_signals.c */
2429 if (se->exited) {
2430 exit(0);
2431 }
2432
2433 if (WIFEXITED(wstatus)) {
2434 exit(WEXITSTATUS(wstatus));
2435 }
2436
2437 exit(1);
2438 }
2439
2440 /* Send us SIGTERM when the parent thread terminates, see prctl(2) */
2441 prctl(PR_SET_PDEATHSIG, SIGTERM);
2442
2443 /*
2444 * If the mounts have shared propagation then we want to opt out so our
2445 * mount changes don't affect the parent mount namespace.
2446 */
2447 if (mount(NULL, "/", NULL, MS_REC | MS_SLAVE, NULL) < 0) {
2448 fuse_log(FUSE_LOG_ERR, "mount(/, MS_REC|MS_SLAVE): %m\n");
2449 exit(1);
2450 }
2451
2452 /* The child must remount /proc to use the new pid namespace */
2453 if (mount("proc", "/proc", "proc",
2454 MS_NODEV | MS_NOEXEC | MS_NOSUID | MS_RELATIME, NULL) < 0) {
2455 fuse_log(FUSE_LOG_ERR, "mount(/proc): %m\n");
2456 exit(1);
2457 }
2458
2459 /*
2460 * We only need /proc/self/fd. Prevent ".." from accessing parent
2461 * directories of /proc/self/fd by bind-mounting it over /proc. Since / was
2462 * previously remounted with MS_REC | MS_SLAVE this mount change only
2463 * affects our process.
2464 */
2465 if (mount("/proc/self/fd", "/proc", NULL, MS_BIND, NULL) < 0) {
2466 fuse_log(FUSE_LOG_ERR, "mount(/proc/self/fd, MS_BIND): %m\n");
2467 exit(1);
2468 }
2469
2470 /* Get the /proc (actually /proc/self/fd, see above) file descriptor */
2471 lo->proc_self_fd = open("/proc", O_PATH);
2472 if (lo->proc_self_fd == -1) {
2473 fuse_log(FUSE_LOG_ERR, "open(/proc, O_PATH): %m\n");
2474 exit(1);
2475 }
2476 }
2477
2478 /*
2479 * Capture the capability state, we'll need to restore this for individual
2480 * threads later; see load_capng.
2481 */
2482 static void setup_capng(void)
2483 {
2484 /* Note this accesses /proc so has to happen before the sandbox */
2485 if (capng_get_caps_process()) {
2486 fuse_log(FUSE_LOG_ERR, "capng_get_caps_process\n");
2487 exit(1);
2488 }
2489 pthread_mutex_init(&cap.mutex, NULL);
2490 pthread_mutex_lock(&cap.mutex);
2491 cap.saved = capng_save_state();
2492 if (!cap.saved) {
2493 fuse_log(FUSE_LOG_ERR, "capng_save_state\n");
2494 exit(1);
2495 }
2496 pthread_mutex_unlock(&cap.mutex);
2497 }
2498
2499 static void cleanup_capng(void)
2500 {
2501 free(cap.saved);
2502 cap.saved = NULL;
2503 pthread_mutex_destroy(&cap.mutex);
2504 }
2505
2506
2507 /*
2508 * Make the source directory our root so symlinks cannot escape and no other
2509 * files are accessible. Assumes unshare(CLONE_NEWNS) was already called.
2510 */
2511 static void setup_mounts(const char *source)
2512 {
2513 int oldroot;
2514 int newroot;
2515
2516 if (mount(source, source, NULL, MS_BIND | MS_REC, NULL) < 0) {
2517 fuse_log(FUSE_LOG_ERR, "mount(%s, %s, MS_BIND): %m\n", source, source);
2518 exit(1);
2519 }
2520
2521 /* This magic is based on lxc's lxc_pivot_root() */
2522 oldroot = open("/", O_DIRECTORY | O_RDONLY | O_CLOEXEC);
2523 if (oldroot < 0) {
2524 fuse_log(FUSE_LOG_ERR, "open(/): %m\n");
2525 exit(1);
2526 }
2527
2528 newroot = open(source, O_DIRECTORY | O_RDONLY | O_CLOEXEC);
2529 if (newroot < 0) {
2530 fuse_log(FUSE_LOG_ERR, "open(%s): %m\n", source);
2531 exit(1);
2532 }
2533
2534 if (fchdir(newroot) < 0) {
2535 fuse_log(FUSE_LOG_ERR, "fchdir(newroot): %m\n");
2536 exit(1);
2537 }
2538
2539 if (syscall(__NR_pivot_root, ".", ".") < 0) {
2540 fuse_log(FUSE_LOG_ERR, "pivot_root(., .): %m\n");
2541 exit(1);
2542 }
2543
2544 if (fchdir(oldroot) < 0) {
2545 fuse_log(FUSE_LOG_ERR, "fchdir(oldroot): %m\n");
2546 exit(1);
2547 }
2548
2549 if (mount("", ".", "", MS_SLAVE | MS_REC, NULL) < 0) {
2550 fuse_log(FUSE_LOG_ERR, "mount(., MS_SLAVE | MS_REC): %m\n");
2551 exit(1);
2552 }
2553
2554 if (umount2(".", MNT_DETACH) < 0) {
2555 fuse_log(FUSE_LOG_ERR, "umount2(., MNT_DETACH): %m\n");
2556 exit(1);
2557 }
2558
2559 if (fchdir(newroot) < 0) {
2560 fuse_log(FUSE_LOG_ERR, "fchdir(newroot): %m\n");
2561 exit(1);
2562 }
2563
2564 close(newroot);
2565 close(oldroot);
2566 }
2567
2568 /*
2569 * Only keep whitelisted capabilities that are needed for file system operation
2570 * The (possibly NULL) modcaps_in string passed in is free'd before exit.
2571 */
2572 static void setup_capabilities(char *modcaps_in)
2573 {
2574 char *modcaps = modcaps_in;
2575 pthread_mutex_lock(&cap.mutex);
2576 capng_restore_state(&cap.saved);
2577
2578 /*
2579 * Whitelist file system-related capabilities that are needed for a file
2580 * server to act like root. Drop everything else like networking and
2581 * sysadmin capabilities.
2582 *
2583 * Exclusions:
2584 * 1. CAP_LINUX_IMMUTABLE is not included because it's only used via ioctl
2585 * and we don't support that.
2586 * 2. CAP_MAC_OVERRIDE is not included because it only seems to be
2587 * used by the Smack LSM. Omit it until there is demand for it.
2588 */
2589 capng_setpid(syscall(SYS_gettid));
2590 capng_clear(CAPNG_SELECT_BOTH);
2591 if (capng_updatev(CAPNG_ADD, CAPNG_PERMITTED | CAPNG_EFFECTIVE,
2592 CAP_CHOWN,
2593 CAP_DAC_OVERRIDE,
2594 CAP_FOWNER,
2595 CAP_FSETID,
2596 CAP_SETGID,
2597 CAP_SETUID,
2598 CAP_MKNOD,
2599 CAP_SETFCAP,
2600 -1)) {
2601 fuse_log(FUSE_LOG_ERR, "%s: capng_updatev failed\n", __func__);
2602 exit(1);
2603 }
2604
2605 /*
2606 * The modcaps option is a colon separated list of caps,
2607 * each preceded by either + or -.
2608 */
2609 while (modcaps) {
2610 capng_act_t action;
2611 int cap;
2612
2613 char *next = strchr(modcaps, ':');
2614 if (next) {
2615 *next = '\0';
2616 next++;
2617 }
2618
2619 switch (modcaps[0]) {
2620 case '+':
2621 action = CAPNG_ADD;
2622 break;
2623
2624 case '-':
2625 action = CAPNG_DROP;
2626 break;
2627
2628 default:
2629 fuse_log(FUSE_LOG_ERR,
2630 "%s: Expecting '+'/'-' in modcaps but found '%c'\n",
2631 __func__, modcaps[0]);
2632 exit(1);
2633 }
2634 cap = capng_name_to_capability(modcaps + 1);
2635 if (cap < 0) {
2636 fuse_log(FUSE_LOG_ERR, "%s: Unknown capability '%s'\n", __func__,
2637 modcaps);
2638 exit(1);
2639 }
2640 if (capng_update(action, CAPNG_PERMITTED | CAPNG_EFFECTIVE, cap)) {
2641 fuse_log(FUSE_LOG_ERR, "%s: capng_update failed for '%s'\n",
2642 __func__, modcaps);
2643 exit(1);
2644 }
2645
2646 modcaps = next;
2647 }
2648 g_free(modcaps_in);
2649
2650 if (capng_apply(CAPNG_SELECT_BOTH)) {
2651 fuse_log(FUSE_LOG_ERR, "%s: capng_apply failed\n", __func__);
2652 exit(1);
2653 }
2654
2655 cap.saved = capng_save_state();
2656 if (!cap.saved) {
2657 fuse_log(FUSE_LOG_ERR, "%s: capng_save_state failed\n", __func__);
2658 exit(1);
2659 }
2660 pthread_mutex_unlock(&cap.mutex);
2661 }
2662
2663 /*
2664 * Lock down this process to prevent access to other processes or files outside
2665 * source directory. This reduces the impact of arbitrary code execution bugs.
2666 */
2667 static void setup_sandbox(struct lo_data *lo, struct fuse_session *se,
2668 bool enable_syslog)
2669 {
2670 setup_namespaces(lo, se);
2671 setup_mounts(lo->source);
2672 setup_seccomp(enable_syslog);
2673 setup_capabilities(g_strdup(lo->modcaps));
2674 }
2675
2676 /* Set the maximum number of open file descriptors */
2677 static void setup_nofile_rlimit(unsigned long rlimit_nofile)
2678 {
2679 struct rlimit rlim = {
2680 .rlim_cur = rlimit_nofile,
2681 .rlim_max = rlimit_nofile,
2682 };
2683
2684 if (rlimit_nofile == 0) {
2685 return; /* nothing to do */
2686 }
2687
2688 if (setrlimit(RLIMIT_NOFILE, &rlim) < 0) {
2689 /* Ignore SELinux denials */
2690 if (errno == EPERM) {
2691 return;
2692 }
2693
2694 fuse_log(FUSE_LOG_ERR, "setrlimit(RLIMIT_NOFILE): %m\n");
2695 exit(1);
2696 }
2697 }
2698
2699 static void log_func(enum fuse_log_level level, const char *fmt, va_list ap)
2700 {
2701 g_autofree char *localfmt = NULL;
2702
2703 if (current_log_level < level) {
2704 return;
2705 }
2706
2707 if (current_log_level == FUSE_LOG_DEBUG) {
2708 if (!use_syslog) {
2709 localfmt = g_strdup_printf("[%" PRId64 "] [ID: %08ld] %s",
2710 get_clock(), syscall(__NR_gettid), fmt);
2711 } else {
2712 localfmt = g_strdup_printf("[ID: %08ld] %s", syscall(__NR_gettid),
2713 fmt);
2714 }
2715 fmt = localfmt;
2716 }
2717
2718 if (use_syslog) {
2719 int priority = LOG_ERR;
2720 switch (level) {
2721 case FUSE_LOG_EMERG:
2722 priority = LOG_EMERG;
2723 break;
2724 case FUSE_LOG_ALERT:
2725 priority = LOG_ALERT;
2726 break;
2727 case FUSE_LOG_CRIT:
2728 priority = LOG_CRIT;
2729 break;
2730 case FUSE_LOG_ERR:
2731 priority = LOG_ERR;
2732 break;
2733 case FUSE_LOG_WARNING:
2734 priority = LOG_WARNING;
2735 break;
2736 case FUSE_LOG_NOTICE:
2737 priority = LOG_NOTICE;
2738 break;
2739 case FUSE_LOG_INFO:
2740 priority = LOG_INFO;
2741 break;
2742 case FUSE_LOG_DEBUG:
2743 priority = LOG_DEBUG;
2744 break;
2745 }
2746 vsyslog(priority, fmt, ap);
2747 } else {
2748 vfprintf(stderr, fmt, ap);
2749 }
2750 }
2751
2752 static void setup_root(struct lo_data *lo, struct lo_inode *root)
2753 {
2754 int fd, res;
2755 struct stat stat;
2756
2757 fd = open("/", O_PATH);
2758 if (fd == -1) {
2759 fuse_log(FUSE_LOG_ERR, "open(%s, O_PATH): %m\n", lo->source);
2760 exit(1);
2761 }
2762
2763 res = fstatat(fd, "", &stat, AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW);
2764 if (res == -1) {
2765 fuse_log(FUSE_LOG_ERR, "fstatat(%s): %m\n", lo->source);
2766 exit(1);
2767 }
2768
2769 root->filetype = S_IFDIR;
2770 root->fd = fd;
2771 root->key.ino = stat.st_ino;
2772 root->key.dev = stat.st_dev;
2773 root->nlookup = 2;
2774 g_atomic_int_set(&root->refcount, 2);
2775 }
2776
2777 static guint lo_key_hash(gconstpointer key)
2778 {
2779 const struct lo_key *lkey = key;
2780
2781 return (guint)lkey->ino + (guint)lkey->dev;
2782 }
2783
2784 static gboolean lo_key_equal(gconstpointer a, gconstpointer b)
2785 {
2786 const struct lo_key *la = a;
2787 const struct lo_key *lb = b;
2788
2789 return la->ino == lb->ino && la->dev == lb->dev;
2790 }
2791
2792 static void fuse_lo_data_cleanup(struct lo_data *lo)
2793 {
2794 if (lo->inodes) {
2795 g_hash_table_destroy(lo->inodes);
2796 }
2797 lo_map_destroy(&lo->fd_map);
2798 lo_map_destroy(&lo->dirp_map);
2799 lo_map_destroy(&lo->ino_map);
2800
2801 if (lo->proc_self_fd >= 0) {
2802 close(lo->proc_self_fd);
2803 }
2804
2805 if (lo->root.fd >= 0) {
2806 close(lo->root.fd);
2807 }
2808
2809 free(lo->source);
2810 }
2811
2812 int main(int argc, char *argv[])
2813 {
2814 struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
2815 struct fuse_session *se;
2816 struct fuse_cmdline_opts opts;
2817 struct lo_data lo = {
2818 .debug = 0,
2819 .writeback = 0,
2820 .posix_lock = 0,
2821 .allow_direct_io = 0,
2822 .proc_self_fd = -1,
2823 };
2824 struct lo_map_elem *root_elem;
2825 int ret = -1;
2826
2827 /* Don't mask creation mode, kernel already did that */
2828 umask(0);
2829
2830 qemu_init_exec_dir(argv[0]);
2831
2832 pthread_mutex_init(&lo.mutex, NULL);
2833 lo.inodes = g_hash_table_new(lo_key_hash, lo_key_equal);
2834 lo.root.fd = -1;
2835 lo.root.fuse_ino = FUSE_ROOT_ID;
2836 lo.cache = CACHE_AUTO;
2837
2838 /*
2839 * Set up the ino map like this:
2840 * [0] Reserved (will not be used)
2841 * [1] Root inode
2842 */
2843 lo_map_init(&lo.ino_map);
2844 lo_map_reserve(&lo.ino_map, 0)->in_use = false;
2845 root_elem = lo_map_reserve(&lo.ino_map, lo.root.fuse_ino);
2846 root_elem->inode = &lo.root;
2847
2848 lo_map_init(&lo.dirp_map);
2849 lo_map_init(&lo.fd_map);
2850
2851 if (fuse_parse_cmdline(&args, &opts) != 0) {
2852 goto err_out1;
2853 }
2854 fuse_set_log_func(log_func);
2855 use_syslog = opts.syslog;
2856 if (use_syslog) {
2857 openlog("virtiofsd", LOG_PID, LOG_DAEMON);
2858 }
2859
2860 if (opts.show_help) {
2861 printf("usage: %s [options]\n\n", argv[0]);
2862 fuse_cmdline_help();
2863 printf(" -o source=PATH shared directory tree\n");
2864 fuse_lowlevel_help();
2865 ret = 0;
2866 goto err_out1;
2867 } else if (opts.show_version) {
2868 fuse_lowlevel_version();
2869 ret = 0;
2870 goto err_out1;
2871 } else if (opts.print_capabilities) {
2872 print_capabilities();
2873 ret = 0;
2874 goto err_out1;
2875 }
2876
2877 if (fuse_opt_parse(&args, &lo, lo_opts, NULL) == -1) {
2878 goto err_out1;
2879 }
2880
2881 /*
2882 * log_level is 0 if not configured via cmd options (0 is LOG_EMERG,
2883 * and we don't use this log level).
2884 */
2885 if (opts.log_level != 0) {
2886 current_log_level = opts.log_level;
2887 }
2888 lo.debug = opts.debug;
2889 if (lo.debug) {
2890 current_log_level = FUSE_LOG_DEBUG;
2891 }
2892 if (lo.source) {
2893 struct stat stat;
2894 int res;
2895
2896 res = lstat(lo.source, &stat);
2897 if (res == -1) {
2898 fuse_log(FUSE_LOG_ERR, "failed to stat source (\"%s\"): %m\n",
2899 lo.source);
2900 exit(1);
2901 }
2902 if (!S_ISDIR(stat.st_mode)) {
2903 fuse_log(FUSE_LOG_ERR, "source is not a directory\n");
2904 exit(1);
2905 }
2906 } else {
2907 lo.source = strdup("/");
2908 }
2909 if (!lo.timeout_set) {
2910 switch (lo.cache) {
2911 case CACHE_NONE:
2912 lo.timeout = 0.0;
2913 break;
2914
2915 case CACHE_AUTO:
2916 lo.timeout = 1.0;
2917 break;
2918
2919 case CACHE_ALWAYS:
2920 lo.timeout = 86400.0;
2921 break;
2922 }
2923 } else if (lo.timeout < 0) {
2924 fuse_log(FUSE_LOG_ERR, "timeout is negative (%lf)\n", lo.timeout);
2925 exit(1);
2926 }
2927
2928 se = fuse_session_new(&args, &lo_oper, sizeof(lo_oper), &lo);
2929 if (se == NULL) {
2930 goto err_out1;
2931 }
2932
2933 if (fuse_set_signal_handlers(se) != 0) {
2934 goto err_out2;
2935 }
2936
2937 if (fuse_session_mount(se) != 0) {
2938 goto err_out3;
2939 }
2940
2941 fuse_daemonize(opts.foreground);
2942
2943 setup_nofile_rlimit(opts.rlimit_nofile);
2944
2945 /* Must be before sandbox since it wants /proc */
2946 setup_capng();
2947
2948 setup_sandbox(&lo, se, opts.syslog);
2949
2950 setup_root(&lo, &lo.root);
2951 /* Block until ctrl+c or fusermount -u */
2952 ret = virtio_loop(se);
2953
2954 fuse_session_unmount(se);
2955 cleanup_capng();
2956 err_out3:
2957 fuse_remove_signal_handlers(se);
2958 err_out2:
2959 fuse_session_destroy(se);
2960 err_out1:
2961 fuse_opt_free_args(&args);
2962
2963 fuse_lo_data_cleanup(&lo);
2964
2965 return ret ? 1 : 0;
2966 }