]> git.ipfire.org Git - thirdparty/qemu.git/blob - tools/virtiofsd/passthrough_ll.c
virtiofsd: add --print-capabilities option
[thirdparty/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 "fuse_virtio.h"
40 #include "fuse_lowlevel.h"
41 #include <assert.h>
42 #include <dirent.h>
43 #include <errno.h>
44 #include <inttypes.h>
45 #include <limits.h>
46 #include <pthread.h>
47 #include <stdbool.h>
48 #include <stddef.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <sys/file.h>
53 #include <sys/xattr.h>
54 #include <unistd.h>
55
56 #include "passthrough_helpers.h"
57
58 /*
59 * We are re-using pointers to our `struct lo_inode` and `struct
60 * lo_dirp` elements as inodes. This means that we must be able to
61 * store uintptr_t values in a fuse_ino_t variable. The following
62 * incantation checks this condition at compile time.
63 */
64 #if defined(__GNUC__) && \
65 (__GNUC__ > 4 || __GNUC__ == 4 && __GNUC_MINOR__ >= 6) && \
66 !defined __cplusplus
67 _Static_assert(sizeof(fuse_ino_t) >= sizeof(uintptr_t),
68 "fuse_ino_t too small to hold uintptr_t values!");
69 #else
70 struct _uintptr_to_must_hold_fuse_ino_t_dummy_struct {
71 unsigned _uintptr_to_must_hold_fuse_ino_t
72 : ((sizeof(fuse_ino_t) >= sizeof(uintptr_t)) ? 1 : -1);
73 };
74 #endif
75
76 struct lo_inode {
77 struct lo_inode *next; /* protected by lo->mutex */
78 struct lo_inode *prev; /* protected by lo->mutex */
79 int fd;
80 bool is_symlink;
81 ino_t ino;
82 dev_t dev;
83 uint64_t refcount; /* protected by lo->mutex */
84 };
85
86 enum {
87 CACHE_NEVER,
88 CACHE_NORMAL,
89 CACHE_ALWAYS,
90 };
91
92 struct lo_data {
93 pthread_mutex_t mutex;
94 int debug;
95 int writeback;
96 int flock;
97 int xattr;
98 const char *source;
99 double timeout;
100 int cache;
101 int timeout_set;
102 struct lo_inode root; /* protected by lo->mutex */
103 };
104
105 static const struct fuse_opt lo_opts[] = {
106 { "writeback", offsetof(struct lo_data, writeback), 1 },
107 { "no_writeback", offsetof(struct lo_data, writeback), 0 },
108 { "source=%s", offsetof(struct lo_data, source), 0 },
109 { "flock", offsetof(struct lo_data, flock), 1 },
110 { "no_flock", offsetof(struct lo_data, flock), 0 },
111 { "xattr", offsetof(struct lo_data, xattr), 1 },
112 { "no_xattr", offsetof(struct lo_data, xattr), 0 },
113 { "timeout=%lf", offsetof(struct lo_data, timeout), 0 },
114 { "timeout=", offsetof(struct lo_data, timeout_set), 1 },
115 { "cache=never", offsetof(struct lo_data, cache), CACHE_NEVER },
116 { "cache=auto", offsetof(struct lo_data, cache), CACHE_NORMAL },
117 { "cache=always", offsetof(struct lo_data, cache), CACHE_ALWAYS },
118
119 FUSE_OPT_END
120 };
121
122 static struct lo_data *lo_data(fuse_req_t req)
123 {
124 return (struct lo_data *)fuse_req_userdata(req);
125 }
126
127 static struct lo_inode *lo_inode(fuse_req_t req, fuse_ino_t ino)
128 {
129 if (ino == FUSE_ROOT_ID) {
130 return &lo_data(req)->root;
131 } else {
132 return (struct lo_inode *)(uintptr_t)ino;
133 }
134 }
135
136 static int lo_fd(fuse_req_t req, fuse_ino_t ino)
137 {
138 return lo_inode(req, ino)->fd;
139 }
140
141 static bool lo_debug(fuse_req_t req)
142 {
143 return lo_data(req)->debug != 0;
144 }
145
146 static void lo_init(void *userdata, struct fuse_conn_info *conn)
147 {
148 struct lo_data *lo = (struct lo_data *)userdata;
149
150 if (conn->capable & FUSE_CAP_EXPORT_SUPPORT) {
151 conn->want |= FUSE_CAP_EXPORT_SUPPORT;
152 }
153
154 if (lo->writeback && conn->capable & FUSE_CAP_WRITEBACK_CACHE) {
155 if (lo->debug) {
156 fuse_log(FUSE_LOG_DEBUG, "lo_init: activating writeback\n");
157 }
158 conn->want |= FUSE_CAP_WRITEBACK_CACHE;
159 }
160 if (lo->flock && conn->capable & FUSE_CAP_FLOCK_LOCKS) {
161 if (lo->debug) {
162 fuse_log(FUSE_LOG_DEBUG, "lo_init: activating flock locks\n");
163 }
164 conn->want |= FUSE_CAP_FLOCK_LOCKS;
165 }
166 }
167
168 static void lo_getattr(fuse_req_t req, fuse_ino_t ino,
169 struct fuse_file_info *fi)
170 {
171 int res;
172 struct stat buf;
173 struct lo_data *lo = lo_data(req);
174
175 (void)fi;
176
177 res =
178 fstatat(lo_fd(req, ino), "", &buf, AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW);
179 if (res == -1) {
180 return (void)fuse_reply_err(req, errno);
181 }
182
183 fuse_reply_attr(req, &buf, lo->timeout);
184 }
185
186 static int utimensat_empty_nofollow(struct lo_inode *inode,
187 const struct timespec *tv)
188 {
189 int res;
190 char procname[64];
191
192 if (inode->is_symlink) {
193 res = utimensat(inode->fd, "", tv, AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW);
194 if (res == -1 && errno == EINVAL) {
195 /* Sorry, no race free way to set times on symlink. */
196 errno = EPERM;
197 }
198 return res;
199 }
200 sprintf(procname, "/proc/self/fd/%i", inode->fd);
201
202 return utimensat(AT_FDCWD, procname, tv, 0);
203 }
204
205 static void lo_setattr(fuse_req_t req, fuse_ino_t ino, struct stat *attr,
206 int valid, struct fuse_file_info *fi)
207 {
208 int saverr;
209 char procname[64];
210 struct lo_inode *inode = lo_inode(req, ino);
211 int ifd = inode->fd;
212 int res;
213
214 if (valid & FUSE_SET_ATTR_MODE) {
215 if (fi) {
216 res = fchmod(fi->fh, attr->st_mode);
217 } else {
218 sprintf(procname, "/proc/self/fd/%i", ifd);
219 res = chmod(procname, attr->st_mode);
220 }
221 if (res == -1) {
222 goto out_err;
223 }
224 }
225 if (valid & (FUSE_SET_ATTR_UID | FUSE_SET_ATTR_GID)) {
226 uid_t uid = (valid & FUSE_SET_ATTR_UID) ? attr->st_uid : (uid_t)-1;
227 gid_t gid = (valid & FUSE_SET_ATTR_GID) ? attr->st_gid : (gid_t)-1;
228
229 res = fchownat(ifd, "", uid, gid, AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW);
230 if (res == -1) {
231 goto out_err;
232 }
233 }
234 if (valid & FUSE_SET_ATTR_SIZE) {
235 if (fi) {
236 res = ftruncate(fi->fh, attr->st_size);
237 } else {
238 sprintf(procname, "/proc/self/fd/%i", ifd);
239 res = truncate(procname, attr->st_size);
240 }
241 if (res == -1) {
242 goto out_err;
243 }
244 }
245 if (valid & (FUSE_SET_ATTR_ATIME | FUSE_SET_ATTR_MTIME)) {
246 struct timespec tv[2];
247
248 tv[0].tv_sec = 0;
249 tv[1].tv_sec = 0;
250 tv[0].tv_nsec = UTIME_OMIT;
251 tv[1].tv_nsec = UTIME_OMIT;
252
253 if (valid & FUSE_SET_ATTR_ATIME_NOW) {
254 tv[0].tv_nsec = UTIME_NOW;
255 } else if (valid & FUSE_SET_ATTR_ATIME) {
256 tv[0] = attr->st_atim;
257 }
258
259 if (valid & FUSE_SET_ATTR_MTIME_NOW) {
260 tv[1].tv_nsec = UTIME_NOW;
261 } else if (valid & FUSE_SET_ATTR_MTIME) {
262 tv[1] = attr->st_mtim;
263 }
264
265 if (fi) {
266 res = futimens(fi->fh, tv);
267 } else {
268 res = utimensat_empty_nofollow(inode, tv);
269 }
270 if (res == -1) {
271 goto out_err;
272 }
273 }
274
275 return lo_getattr(req, ino, fi);
276
277 out_err:
278 saverr = errno;
279 fuse_reply_err(req, saverr);
280 }
281
282 static struct lo_inode *lo_find(struct lo_data *lo, struct stat *st)
283 {
284 struct lo_inode *p;
285 struct lo_inode *ret = NULL;
286
287 pthread_mutex_lock(&lo->mutex);
288 for (p = lo->root.next; p != &lo->root; p = p->next) {
289 if (p->ino == st->st_ino && p->dev == st->st_dev) {
290 assert(p->refcount > 0);
291 ret = p;
292 ret->refcount++;
293 break;
294 }
295 }
296 pthread_mutex_unlock(&lo->mutex);
297 return ret;
298 }
299
300 static int lo_do_lookup(fuse_req_t req, fuse_ino_t parent, const char *name,
301 struct fuse_entry_param *e)
302 {
303 int newfd;
304 int res;
305 int saverr;
306 struct lo_data *lo = lo_data(req);
307 struct lo_inode *inode;
308
309 memset(e, 0, sizeof(*e));
310 e->attr_timeout = lo->timeout;
311 e->entry_timeout = lo->timeout;
312
313 newfd = openat(lo_fd(req, parent), name, O_PATH | O_NOFOLLOW);
314 if (newfd == -1) {
315 goto out_err;
316 }
317
318 res = fstatat(newfd, "", &e->attr, AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW);
319 if (res == -1) {
320 goto out_err;
321 }
322
323 inode = lo_find(lo_data(req), &e->attr);
324 if (inode) {
325 close(newfd);
326 newfd = -1;
327 } else {
328 struct lo_inode *prev, *next;
329
330 saverr = ENOMEM;
331 inode = calloc(1, sizeof(struct lo_inode));
332 if (!inode) {
333 goto out_err;
334 }
335
336 inode->is_symlink = S_ISLNK(e->attr.st_mode);
337 inode->refcount = 1;
338 inode->fd = newfd;
339 inode->ino = e->attr.st_ino;
340 inode->dev = e->attr.st_dev;
341
342 pthread_mutex_lock(&lo->mutex);
343 prev = &lo->root;
344 next = prev->next;
345 next->prev = inode;
346 inode->next = next;
347 inode->prev = prev;
348 prev->next = inode;
349 pthread_mutex_unlock(&lo->mutex);
350 }
351 e->ino = (uintptr_t)inode;
352
353 if (lo_debug(req)) {
354 fuse_log(FUSE_LOG_DEBUG, " %lli/%s -> %lli\n",
355 (unsigned long long)parent, name, (unsigned long long)e->ino);
356 }
357
358 return 0;
359
360 out_err:
361 saverr = errno;
362 if (newfd != -1) {
363 close(newfd);
364 }
365 return saverr;
366 }
367
368 static void lo_lookup(fuse_req_t req, fuse_ino_t parent, const char *name)
369 {
370 struct fuse_entry_param e;
371 int err;
372
373 if (lo_debug(req)) {
374 fuse_log(FUSE_LOG_DEBUG, "lo_lookup(parent=%" PRIu64 ", name=%s)\n",
375 parent, name);
376 }
377
378 err = lo_do_lookup(req, parent, name, &e);
379 if (err) {
380 fuse_reply_err(req, err);
381 } else {
382 fuse_reply_entry(req, &e);
383 }
384 }
385
386 static void lo_mknod_symlink(fuse_req_t req, fuse_ino_t parent,
387 const char *name, mode_t mode, dev_t rdev,
388 const char *link)
389 {
390 int res;
391 int saverr;
392 struct lo_inode *dir = lo_inode(req, parent);
393 struct fuse_entry_param e;
394
395 saverr = ENOMEM;
396
397 res = mknod_wrapper(dir->fd, name, link, mode, rdev);
398
399 saverr = errno;
400 if (res == -1) {
401 goto out;
402 }
403
404 saverr = lo_do_lookup(req, parent, name, &e);
405 if (saverr) {
406 goto out;
407 }
408
409 if (lo_debug(req)) {
410 fuse_log(FUSE_LOG_DEBUG, " %lli/%s -> %lli\n",
411 (unsigned long long)parent, name, (unsigned long long)e.ino);
412 }
413
414 fuse_reply_entry(req, &e);
415 return;
416
417 out:
418 fuse_reply_err(req, saverr);
419 }
420
421 static void lo_mknod(fuse_req_t req, fuse_ino_t parent, const char *name,
422 mode_t mode, dev_t rdev)
423 {
424 lo_mknod_symlink(req, parent, name, mode, rdev, NULL);
425 }
426
427 static void lo_mkdir(fuse_req_t req, fuse_ino_t parent, const char *name,
428 mode_t mode)
429 {
430 lo_mknod_symlink(req, parent, name, S_IFDIR | mode, 0, NULL);
431 }
432
433 static void lo_symlink(fuse_req_t req, const char *link, fuse_ino_t parent,
434 const char *name)
435 {
436 lo_mknod_symlink(req, parent, name, S_IFLNK, 0, link);
437 }
438
439 static int linkat_empty_nofollow(struct lo_inode *inode, int dfd,
440 const char *name)
441 {
442 int res;
443 char procname[64];
444
445 if (inode->is_symlink) {
446 res = linkat(inode->fd, "", dfd, name, AT_EMPTY_PATH);
447 if (res == -1 && (errno == ENOENT || errno == EINVAL)) {
448 /* Sorry, no race free way to hard-link a symlink. */
449 errno = EPERM;
450 }
451 return res;
452 }
453
454 sprintf(procname, "/proc/self/fd/%i", inode->fd);
455
456 return linkat(AT_FDCWD, procname, dfd, name, AT_SYMLINK_FOLLOW);
457 }
458
459 static void lo_link(fuse_req_t req, fuse_ino_t ino, fuse_ino_t parent,
460 const char *name)
461 {
462 int res;
463 struct lo_data *lo = lo_data(req);
464 struct lo_inode *inode = lo_inode(req, ino);
465 struct fuse_entry_param e;
466 int saverr;
467
468 memset(&e, 0, sizeof(struct fuse_entry_param));
469 e.attr_timeout = lo->timeout;
470 e.entry_timeout = lo->timeout;
471
472 res = linkat_empty_nofollow(inode, lo_fd(req, parent), name);
473 if (res == -1) {
474 goto out_err;
475 }
476
477 res = fstatat(inode->fd, "", &e.attr, AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW);
478 if (res == -1) {
479 goto out_err;
480 }
481
482 pthread_mutex_lock(&lo->mutex);
483 inode->refcount++;
484 pthread_mutex_unlock(&lo->mutex);
485 e.ino = (uintptr_t)inode;
486
487 if (lo_debug(req)) {
488 fuse_log(FUSE_LOG_DEBUG, " %lli/%s -> %lli\n",
489 (unsigned long long)parent, name, (unsigned long long)e.ino);
490 }
491
492 fuse_reply_entry(req, &e);
493 return;
494
495 out_err:
496 saverr = errno;
497 fuse_reply_err(req, saverr);
498 }
499
500 static void lo_rmdir(fuse_req_t req, fuse_ino_t parent, const char *name)
501 {
502 int res;
503
504 res = unlinkat(lo_fd(req, parent), name, AT_REMOVEDIR);
505
506 fuse_reply_err(req, res == -1 ? errno : 0);
507 }
508
509 static void lo_rename(fuse_req_t req, fuse_ino_t parent, const char *name,
510 fuse_ino_t newparent, const char *newname,
511 unsigned int flags)
512 {
513 int res;
514
515 if (flags) {
516 fuse_reply_err(req, EINVAL);
517 return;
518 }
519
520 res = renameat(lo_fd(req, parent), name, lo_fd(req, newparent), newname);
521
522 fuse_reply_err(req, res == -1 ? errno : 0);
523 }
524
525 static void lo_unlink(fuse_req_t req, fuse_ino_t parent, const char *name)
526 {
527 int res;
528
529 res = unlinkat(lo_fd(req, parent), name, 0);
530
531 fuse_reply_err(req, res == -1 ? errno : 0);
532 }
533
534 static void unref_inode(struct lo_data *lo, struct lo_inode *inode, uint64_t n)
535 {
536 if (!inode) {
537 return;
538 }
539
540 pthread_mutex_lock(&lo->mutex);
541 assert(inode->refcount >= n);
542 inode->refcount -= n;
543 if (!inode->refcount) {
544 struct lo_inode *prev, *next;
545
546 prev = inode->prev;
547 next = inode->next;
548 next->prev = prev;
549 prev->next = next;
550
551 pthread_mutex_unlock(&lo->mutex);
552 close(inode->fd);
553 free(inode);
554
555 } else {
556 pthread_mutex_unlock(&lo->mutex);
557 }
558 }
559
560 static void lo_forget_one(fuse_req_t req, fuse_ino_t ino, uint64_t nlookup)
561 {
562 struct lo_data *lo = lo_data(req);
563 struct lo_inode *inode = lo_inode(req, ino);
564
565 if (lo_debug(req)) {
566 fuse_log(FUSE_LOG_DEBUG, " forget %lli %lli -%lli\n",
567 (unsigned long long)ino, (unsigned long long)inode->refcount,
568 (unsigned long long)nlookup);
569 }
570
571 unref_inode(lo, inode, nlookup);
572 }
573
574 static void lo_forget(fuse_req_t req, fuse_ino_t ino, uint64_t nlookup)
575 {
576 lo_forget_one(req, ino, nlookup);
577 fuse_reply_none(req);
578 }
579
580 static void lo_forget_multi(fuse_req_t req, size_t count,
581 struct fuse_forget_data *forgets)
582 {
583 int i;
584
585 for (i = 0; i < count; i++) {
586 lo_forget_one(req, forgets[i].ino, forgets[i].nlookup);
587 }
588 fuse_reply_none(req);
589 }
590
591 static void lo_readlink(fuse_req_t req, fuse_ino_t ino)
592 {
593 char buf[PATH_MAX + 1];
594 int res;
595
596 res = readlinkat(lo_fd(req, ino), "", buf, sizeof(buf));
597 if (res == -1) {
598 return (void)fuse_reply_err(req, errno);
599 }
600
601 if (res == sizeof(buf)) {
602 return (void)fuse_reply_err(req, ENAMETOOLONG);
603 }
604
605 buf[res] = '\0';
606
607 fuse_reply_readlink(req, buf);
608 }
609
610 struct lo_dirp {
611 DIR *dp;
612 struct dirent *entry;
613 off_t offset;
614 };
615
616 static struct lo_dirp *lo_dirp(struct fuse_file_info *fi)
617 {
618 return (struct lo_dirp *)(uintptr_t)fi->fh;
619 }
620
621 static void lo_opendir(fuse_req_t req, fuse_ino_t ino,
622 struct fuse_file_info *fi)
623 {
624 int error = ENOMEM;
625 struct lo_data *lo = lo_data(req);
626 struct lo_dirp *d;
627 int fd;
628
629 d = calloc(1, sizeof(struct lo_dirp));
630 if (d == NULL) {
631 goto out_err;
632 }
633
634 fd = openat(lo_fd(req, ino), ".", O_RDONLY);
635 if (fd == -1) {
636 goto out_errno;
637 }
638
639 d->dp = fdopendir(fd);
640 if (d->dp == NULL) {
641 goto out_errno;
642 }
643
644 d->offset = 0;
645 d->entry = NULL;
646
647 fi->fh = (uintptr_t)d;
648 if (lo->cache == CACHE_ALWAYS) {
649 fi->keep_cache = 1;
650 }
651 fuse_reply_open(req, fi);
652 return;
653
654 out_errno:
655 error = errno;
656 out_err:
657 if (d) {
658 if (fd != -1) {
659 close(fd);
660 }
661 free(d);
662 }
663 fuse_reply_err(req, error);
664 }
665
666 static int is_dot_or_dotdot(const char *name)
667 {
668 return name[0] == '.' &&
669 (name[1] == '\0' || (name[1] == '.' && name[2] == '\0'));
670 }
671
672 static void lo_do_readdir(fuse_req_t req, fuse_ino_t ino, size_t size,
673 off_t offset, struct fuse_file_info *fi, int plus)
674 {
675 struct lo_dirp *d = lo_dirp(fi);
676 char *buf;
677 char *p;
678 size_t rem = size;
679 int err;
680
681 (void)ino;
682
683 buf = calloc(1, size);
684 if (!buf) {
685 err = ENOMEM;
686 goto error;
687 }
688 p = buf;
689
690 if (offset != d->offset) {
691 seekdir(d->dp, offset);
692 d->entry = NULL;
693 d->offset = offset;
694 }
695 while (1) {
696 size_t entsize;
697 off_t nextoff;
698 const char *name;
699
700 if (!d->entry) {
701 errno = 0;
702 d->entry = readdir(d->dp);
703 if (!d->entry) {
704 if (errno) { /* Error */
705 err = errno;
706 goto error;
707 } else { /* End of stream */
708 break;
709 }
710 }
711 }
712 nextoff = d->entry->d_off;
713 name = d->entry->d_name;
714 fuse_ino_t entry_ino = 0;
715 if (plus) {
716 struct fuse_entry_param e;
717 if (is_dot_or_dotdot(name)) {
718 e = (struct fuse_entry_param){
719 .attr.st_ino = d->entry->d_ino,
720 .attr.st_mode = d->entry->d_type << 12,
721 };
722 } else {
723 err = lo_do_lookup(req, ino, name, &e);
724 if (err) {
725 goto error;
726 }
727 entry_ino = e.ino;
728 }
729
730 entsize = fuse_add_direntry_plus(req, p, rem, name, &e, nextoff);
731 } else {
732 struct stat st = {
733 .st_ino = d->entry->d_ino,
734 .st_mode = d->entry->d_type << 12,
735 };
736 entsize = fuse_add_direntry(req, p, rem, name, &st, nextoff);
737 }
738 if (entsize > rem) {
739 if (entry_ino != 0) {
740 lo_forget_one(req, entry_ino, 1);
741 }
742 break;
743 }
744
745 p += entsize;
746 rem -= entsize;
747
748 d->entry = NULL;
749 d->offset = nextoff;
750 }
751
752 err = 0;
753 error:
754 /*
755 * If there's an error, we can only signal it if we haven't stored
756 * any entries yet - otherwise we'd end up with wrong lookup
757 * counts for the entries that are already in the buffer. So we
758 * return what we've collected until that point.
759 */
760 if (err && rem == size) {
761 fuse_reply_err(req, err);
762 } else {
763 fuse_reply_buf(req, buf, size - rem);
764 }
765 free(buf);
766 }
767
768 static void lo_readdir(fuse_req_t req, fuse_ino_t ino, size_t size,
769 off_t offset, struct fuse_file_info *fi)
770 {
771 lo_do_readdir(req, ino, size, offset, fi, 0);
772 }
773
774 static void lo_readdirplus(fuse_req_t req, fuse_ino_t ino, size_t size,
775 off_t offset, struct fuse_file_info *fi)
776 {
777 lo_do_readdir(req, ino, size, offset, fi, 1);
778 }
779
780 static void lo_releasedir(fuse_req_t req, fuse_ino_t ino,
781 struct fuse_file_info *fi)
782 {
783 struct lo_dirp *d = lo_dirp(fi);
784 (void)ino;
785 closedir(d->dp);
786 free(d);
787 fuse_reply_err(req, 0);
788 }
789
790 static void lo_create(fuse_req_t req, fuse_ino_t parent, const char *name,
791 mode_t mode, struct fuse_file_info *fi)
792 {
793 int fd;
794 struct lo_data *lo = lo_data(req);
795 struct fuse_entry_param e;
796 int err;
797
798 if (lo_debug(req)) {
799 fuse_log(FUSE_LOG_DEBUG, "lo_create(parent=%" PRIu64 ", name=%s)\n",
800 parent, name);
801 }
802
803 fd = openat(lo_fd(req, parent), name, (fi->flags | O_CREAT) & ~O_NOFOLLOW,
804 mode);
805 if (fd == -1) {
806 return (void)fuse_reply_err(req, errno);
807 }
808
809 fi->fh = fd;
810 if (lo->cache == CACHE_NEVER) {
811 fi->direct_io = 1;
812 } else if (lo->cache == CACHE_ALWAYS) {
813 fi->keep_cache = 1;
814 }
815
816 err = lo_do_lookup(req, parent, name, &e);
817 if (err) {
818 fuse_reply_err(req, err);
819 } else {
820 fuse_reply_create(req, &e, fi);
821 }
822 }
823
824 static void lo_fsyncdir(fuse_req_t req, fuse_ino_t ino, int datasync,
825 struct fuse_file_info *fi)
826 {
827 int res;
828 int fd = dirfd(lo_dirp(fi)->dp);
829 (void)ino;
830 if (datasync) {
831 res = fdatasync(fd);
832 } else {
833 res = fsync(fd);
834 }
835 fuse_reply_err(req, res == -1 ? errno : 0);
836 }
837
838 static void lo_open(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi)
839 {
840 int fd;
841 char buf[64];
842 struct lo_data *lo = lo_data(req);
843
844 if (lo_debug(req)) {
845 fuse_log(FUSE_LOG_DEBUG, "lo_open(ino=%" PRIu64 ", flags=%d)\n", ino,
846 fi->flags);
847 }
848
849 /*
850 * With writeback cache, kernel may send read requests even
851 * when userspace opened write-only
852 */
853 if (lo->writeback && (fi->flags & O_ACCMODE) == O_WRONLY) {
854 fi->flags &= ~O_ACCMODE;
855 fi->flags |= O_RDWR;
856 }
857
858 /*
859 * With writeback cache, O_APPEND is handled by the kernel.
860 * This breaks atomicity (since the file may change in the
861 * underlying filesystem, so that the kernel's idea of the
862 * end of the file isn't accurate anymore). In this example,
863 * we just accept that. A more rigorous filesystem may want
864 * to return an error here
865 */
866 if (lo->writeback && (fi->flags & O_APPEND)) {
867 fi->flags &= ~O_APPEND;
868 }
869
870 sprintf(buf, "/proc/self/fd/%i", lo_fd(req, ino));
871 fd = open(buf, fi->flags & ~O_NOFOLLOW);
872 if (fd == -1) {
873 return (void)fuse_reply_err(req, errno);
874 }
875
876 fi->fh = fd;
877 if (lo->cache == CACHE_NEVER) {
878 fi->direct_io = 1;
879 } else if (lo->cache == CACHE_ALWAYS) {
880 fi->keep_cache = 1;
881 }
882 fuse_reply_open(req, fi);
883 }
884
885 static void lo_release(fuse_req_t req, fuse_ino_t ino,
886 struct fuse_file_info *fi)
887 {
888 (void)ino;
889
890 close(fi->fh);
891 fuse_reply_err(req, 0);
892 }
893
894 static void lo_flush(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi)
895 {
896 int res;
897 (void)ino;
898 res = close(dup(fi->fh));
899 fuse_reply_err(req, res == -1 ? errno : 0);
900 }
901
902 static void lo_fsync(fuse_req_t req, fuse_ino_t ino, int datasync,
903 struct fuse_file_info *fi)
904 {
905 int res;
906 (void)ino;
907 int fd;
908 char *buf;
909
910 fuse_log(FUSE_LOG_DEBUG, "lo_fsync(ino=%" PRIu64 ", fi=0x%p)\n", ino,
911 (void *)fi);
912
913 if (!fi) {
914 res = asprintf(&buf, "/proc/self/fd/%i", lo_fd(req, ino));
915 if (res == -1) {
916 return (void)fuse_reply_err(req, errno);
917 }
918
919 fd = open(buf, O_RDWR);
920 free(buf);
921 if (fd == -1) {
922 return (void)fuse_reply_err(req, errno);
923 }
924 } else {
925 fd = fi->fh;
926 }
927
928 if (datasync) {
929 res = fdatasync(fd);
930 } else {
931 res = fsync(fd);
932 }
933 if (!fi) {
934 close(fd);
935 }
936 fuse_reply_err(req, res == -1 ? errno : 0);
937 }
938
939 static void lo_read(fuse_req_t req, fuse_ino_t ino, size_t size, off_t offset,
940 struct fuse_file_info *fi)
941 {
942 struct fuse_bufvec buf = FUSE_BUFVEC_INIT(size);
943
944 if (lo_debug(req)) {
945 fuse_log(FUSE_LOG_DEBUG,
946 "lo_read(ino=%" PRIu64 ", size=%zd, "
947 "off=%lu)\n",
948 ino, size, (unsigned long)offset);
949 }
950
951 buf.buf[0].flags = FUSE_BUF_IS_FD | FUSE_BUF_FD_SEEK;
952 buf.buf[0].fd = fi->fh;
953 buf.buf[0].pos = offset;
954
955 fuse_reply_data(req, &buf);
956 }
957
958 static void lo_write_buf(fuse_req_t req, fuse_ino_t ino,
959 struct fuse_bufvec *in_buf, off_t off,
960 struct fuse_file_info *fi)
961 {
962 (void)ino;
963 ssize_t res;
964 struct fuse_bufvec out_buf = FUSE_BUFVEC_INIT(fuse_buf_size(in_buf));
965
966 out_buf.buf[0].flags = FUSE_BUF_IS_FD | FUSE_BUF_FD_SEEK;
967 out_buf.buf[0].fd = fi->fh;
968 out_buf.buf[0].pos = off;
969
970 if (lo_debug(req)) {
971 fuse_log(FUSE_LOG_DEBUG,
972 "lo_write(ino=%" PRIu64 ", size=%zd, off=%lu)\n", ino,
973 out_buf.buf[0].size, (unsigned long)off);
974 }
975
976 res = fuse_buf_copy(&out_buf, in_buf);
977 if (res < 0) {
978 fuse_reply_err(req, -res);
979 } else {
980 fuse_reply_write(req, (size_t)res);
981 }
982 }
983
984 static void lo_statfs(fuse_req_t req, fuse_ino_t ino)
985 {
986 int res;
987 struct statvfs stbuf;
988
989 res = fstatvfs(lo_fd(req, ino), &stbuf);
990 if (res == -1) {
991 fuse_reply_err(req, errno);
992 } else {
993 fuse_reply_statfs(req, &stbuf);
994 }
995 }
996
997 static void lo_fallocate(fuse_req_t req, fuse_ino_t ino, int mode, off_t offset,
998 off_t length, struct fuse_file_info *fi)
999 {
1000 int err = EOPNOTSUPP;
1001 (void)ino;
1002
1003 #ifdef CONFIG_FALLOCATE
1004 err = fallocate(fi->fh, mode, offset, length);
1005 if (err < 0) {
1006 err = errno;
1007 }
1008
1009 #elif defined(CONFIG_POSIX_FALLOCATE)
1010 if (mode) {
1011 fuse_reply_err(req, EOPNOTSUPP);
1012 return;
1013 }
1014
1015 err = posix_fallocate(fi->fh, offset, length);
1016 #endif
1017
1018 fuse_reply_err(req, err);
1019 }
1020
1021 static void lo_flock(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi,
1022 int op)
1023 {
1024 int res;
1025 (void)ino;
1026
1027 res = flock(fi->fh, op);
1028
1029 fuse_reply_err(req, res == -1 ? errno : 0);
1030 }
1031
1032 static void lo_getxattr(fuse_req_t req, fuse_ino_t ino, const char *name,
1033 size_t size)
1034 {
1035 char *value = NULL;
1036 char procname[64];
1037 struct lo_inode *inode = lo_inode(req, ino);
1038 ssize_t ret;
1039 int saverr;
1040
1041 saverr = ENOSYS;
1042 if (!lo_data(req)->xattr) {
1043 goto out;
1044 }
1045
1046 if (lo_debug(req)) {
1047 fuse_log(FUSE_LOG_DEBUG,
1048 "lo_getxattr(ino=%" PRIu64 ", name=%s size=%zd)\n", ino, name,
1049 size);
1050 }
1051
1052 if (inode->is_symlink) {
1053 /* Sorry, no race free way to getxattr on symlink. */
1054 saverr = EPERM;
1055 goto out;
1056 }
1057
1058 sprintf(procname, "/proc/self/fd/%i", inode->fd);
1059
1060 if (size) {
1061 value = malloc(size);
1062 if (!value) {
1063 goto out_err;
1064 }
1065
1066 ret = getxattr(procname, name, value, size);
1067 if (ret == -1) {
1068 goto out_err;
1069 }
1070 saverr = 0;
1071 if (ret == 0) {
1072 goto out;
1073 }
1074
1075 fuse_reply_buf(req, value, ret);
1076 } else {
1077 ret = getxattr(procname, name, NULL, 0);
1078 if (ret == -1) {
1079 goto out_err;
1080 }
1081
1082 fuse_reply_xattr(req, ret);
1083 }
1084 out_free:
1085 free(value);
1086 return;
1087
1088 out_err:
1089 saverr = errno;
1090 out:
1091 fuse_reply_err(req, saverr);
1092 goto out_free;
1093 }
1094
1095 static void lo_listxattr(fuse_req_t req, fuse_ino_t ino, size_t size)
1096 {
1097 char *value = NULL;
1098 char procname[64];
1099 struct lo_inode *inode = lo_inode(req, ino);
1100 ssize_t ret;
1101 int saverr;
1102
1103 saverr = ENOSYS;
1104 if (!lo_data(req)->xattr) {
1105 goto out;
1106 }
1107
1108 if (lo_debug(req)) {
1109 fuse_log(FUSE_LOG_DEBUG, "lo_listxattr(ino=%" PRIu64 ", size=%zd)\n",
1110 ino, size);
1111 }
1112
1113 if (inode->is_symlink) {
1114 /* Sorry, no race free way to listxattr on symlink. */
1115 saverr = EPERM;
1116 goto out;
1117 }
1118
1119 sprintf(procname, "/proc/self/fd/%i", inode->fd);
1120
1121 if (size) {
1122 value = malloc(size);
1123 if (!value) {
1124 goto out_err;
1125 }
1126
1127 ret = listxattr(procname, value, size);
1128 if (ret == -1) {
1129 goto out_err;
1130 }
1131 saverr = 0;
1132 if (ret == 0) {
1133 goto out;
1134 }
1135
1136 fuse_reply_buf(req, value, ret);
1137 } else {
1138 ret = listxattr(procname, NULL, 0);
1139 if (ret == -1) {
1140 goto out_err;
1141 }
1142
1143 fuse_reply_xattr(req, ret);
1144 }
1145 out_free:
1146 free(value);
1147 return;
1148
1149 out_err:
1150 saverr = errno;
1151 out:
1152 fuse_reply_err(req, saverr);
1153 goto out_free;
1154 }
1155
1156 static void lo_setxattr(fuse_req_t req, fuse_ino_t ino, const char *name,
1157 const char *value, size_t size, int flags)
1158 {
1159 char procname[64];
1160 struct lo_inode *inode = lo_inode(req, ino);
1161 ssize_t ret;
1162 int saverr;
1163
1164 saverr = ENOSYS;
1165 if (!lo_data(req)->xattr) {
1166 goto out;
1167 }
1168
1169 if (lo_debug(req)) {
1170 fuse_log(FUSE_LOG_DEBUG,
1171 "lo_setxattr(ino=%" PRIu64 ", name=%s value=%s size=%zd)\n",
1172 ino, name, value, size);
1173 }
1174
1175 if (inode->is_symlink) {
1176 /* Sorry, no race free way to setxattr on symlink. */
1177 saverr = EPERM;
1178 goto out;
1179 }
1180
1181 sprintf(procname, "/proc/self/fd/%i", inode->fd);
1182
1183 ret = setxattr(procname, name, value, size, flags);
1184 saverr = ret == -1 ? errno : 0;
1185
1186 out:
1187 fuse_reply_err(req, saverr);
1188 }
1189
1190 static void lo_removexattr(fuse_req_t req, fuse_ino_t ino, const char *name)
1191 {
1192 char procname[64];
1193 struct lo_inode *inode = lo_inode(req, ino);
1194 ssize_t ret;
1195 int saverr;
1196
1197 saverr = ENOSYS;
1198 if (!lo_data(req)->xattr) {
1199 goto out;
1200 }
1201
1202 if (lo_debug(req)) {
1203 fuse_log(FUSE_LOG_DEBUG, "lo_removexattr(ino=%" PRIu64 ", name=%s)\n",
1204 ino, name);
1205 }
1206
1207 if (inode->is_symlink) {
1208 /* Sorry, no race free way to setxattr on symlink. */
1209 saverr = EPERM;
1210 goto out;
1211 }
1212
1213 sprintf(procname, "/proc/self/fd/%i", inode->fd);
1214
1215 ret = removexattr(procname, name);
1216 saverr = ret == -1 ? errno : 0;
1217
1218 out:
1219 fuse_reply_err(req, saverr);
1220 }
1221
1222 #ifdef HAVE_COPY_FILE_RANGE
1223 static void lo_copy_file_range(fuse_req_t req, fuse_ino_t ino_in, off_t off_in,
1224 struct fuse_file_info *fi_in, fuse_ino_t ino_out,
1225 off_t off_out, struct fuse_file_info *fi_out,
1226 size_t len, int flags)
1227 {
1228 ssize_t res;
1229
1230 if (lo_debug(req))
1231 fuse_log(FUSE_LOG_DEBUG,
1232 "lo_copy_file_range(ino=%" PRIu64 "/fd=%lu, "
1233 "off=%lu, ino=%" PRIu64 "/fd=%lu, "
1234 "off=%lu, size=%zd, flags=0x%x)\n",
1235 ino_in, fi_in->fh, off_in, ino_out, fi_out->fh, off_out, len,
1236 flags);
1237
1238 res = copy_file_range(fi_in->fh, &off_in, fi_out->fh, &off_out, len, flags);
1239 if (res < 0) {
1240 fuse_reply_err(req, -errno);
1241 } else {
1242 fuse_reply_write(req, res);
1243 }
1244 }
1245 #endif
1246
1247 static void lo_lseek(fuse_req_t req, fuse_ino_t ino, off_t off, int whence,
1248 struct fuse_file_info *fi)
1249 {
1250 off_t res;
1251
1252 (void)ino;
1253 res = lseek(fi->fh, off, whence);
1254 if (res != -1) {
1255 fuse_reply_lseek(req, res);
1256 } else {
1257 fuse_reply_err(req, errno);
1258 }
1259 }
1260
1261 static struct fuse_lowlevel_ops lo_oper = {
1262 .init = lo_init,
1263 .lookup = lo_lookup,
1264 .mkdir = lo_mkdir,
1265 .mknod = lo_mknod,
1266 .symlink = lo_symlink,
1267 .link = lo_link,
1268 .unlink = lo_unlink,
1269 .rmdir = lo_rmdir,
1270 .rename = lo_rename,
1271 .forget = lo_forget,
1272 .forget_multi = lo_forget_multi,
1273 .getattr = lo_getattr,
1274 .setattr = lo_setattr,
1275 .readlink = lo_readlink,
1276 .opendir = lo_opendir,
1277 .readdir = lo_readdir,
1278 .readdirplus = lo_readdirplus,
1279 .releasedir = lo_releasedir,
1280 .fsyncdir = lo_fsyncdir,
1281 .create = lo_create,
1282 .open = lo_open,
1283 .release = lo_release,
1284 .flush = lo_flush,
1285 .fsync = lo_fsync,
1286 .read = lo_read,
1287 .write_buf = lo_write_buf,
1288 .statfs = lo_statfs,
1289 .fallocate = lo_fallocate,
1290 .flock = lo_flock,
1291 .getxattr = lo_getxattr,
1292 .listxattr = lo_listxattr,
1293 .setxattr = lo_setxattr,
1294 .removexattr = lo_removexattr,
1295 #ifdef HAVE_COPY_FILE_RANGE
1296 .copy_file_range = lo_copy_file_range,
1297 #endif
1298 .lseek = lo_lseek,
1299 };
1300
1301 /* Print vhost-user.json backend program capabilities */
1302 static void print_capabilities(void)
1303 {
1304 printf("{\n");
1305 printf(" \"type\": \"fs\"\n");
1306 printf("}\n");
1307 }
1308
1309 int main(int argc, char *argv[])
1310 {
1311 struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
1312 struct fuse_session *se;
1313 struct fuse_cmdline_opts opts;
1314 struct lo_data lo = { .debug = 0, .writeback = 0 };
1315 int ret = -1;
1316
1317 /* Don't mask creation mode, kernel already did that */
1318 umask(0);
1319
1320 pthread_mutex_init(&lo.mutex, NULL);
1321 lo.root.next = lo.root.prev = &lo.root;
1322 lo.root.fd = -1;
1323 lo.cache = CACHE_NORMAL;
1324
1325 if (fuse_parse_cmdline(&args, &opts) != 0) {
1326 return 1;
1327 }
1328 if (opts.show_help) {
1329 printf("usage: %s [options]\n\n", argv[0]);
1330 fuse_cmdline_help();
1331 printf(" -o source=PATH shared directory tree\n");
1332 fuse_lowlevel_help();
1333 ret = 0;
1334 goto err_out1;
1335 } else if (opts.show_version) {
1336 fuse_lowlevel_version();
1337 ret = 0;
1338 goto err_out1;
1339 } else if (opts.print_capabilities) {
1340 print_capabilities();
1341 ret = 0;
1342 goto err_out1;
1343 }
1344
1345 if (fuse_opt_parse(&args, &lo, lo_opts, NULL) == -1) {
1346 return 1;
1347 }
1348
1349 lo.debug = opts.debug;
1350 lo.root.refcount = 2;
1351 if (lo.source) {
1352 struct stat stat;
1353 int res;
1354
1355 res = lstat(lo.source, &stat);
1356 if (res == -1) {
1357 fuse_log(FUSE_LOG_ERR, "failed to stat source (\"%s\"): %m\n",
1358 lo.source);
1359 exit(1);
1360 }
1361 if (!S_ISDIR(stat.st_mode)) {
1362 fuse_log(FUSE_LOG_ERR, "source is not a directory\n");
1363 exit(1);
1364 }
1365
1366 } else {
1367 lo.source = "/";
1368 }
1369 lo.root.is_symlink = false;
1370 if (!lo.timeout_set) {
1371 switch (lo.cache) {
1372 case CACHE_NEVER:
1373 lo.timeout = 0.0;
1374 break;
1375
1376 case CACHE_NORMAL:
1377 lo.timeout = 1.0;
1378 break;
1379
1380 case CACHE_ALWAYS:
1381 lo.timeout = 86400.0;
1382 break;
1383 }
1384 } else if (lo.timeout < 0) {
1385 fuse_log(FUSE_LOG_ERR, "timeout is negative (%lf)\n", lo.timeout);
1386 exit(1);
1387 }
1388
1389 lo.root.fd = open(lo.source, O_PATH);
1390 if (lo.root.fd == -1) {
1391 fuse_log(FUSE_LOG_ERR, "open(\"%s\", O_PATH): %m\n", lo.source);
1392 exit(1);
1393 }
1394
1395 se = fuse_session_new(&args, &lo_oper, sizeof(lo_oper), &lo);
1396 if (se == NULL) {
1397 goto err_out1;
1398 }
1399
1400 if (fuse_set_signal_handlers(se) != 0) {
1401 goto err_out2;
1402 }
1403
1404 if (fuse_session_mount(se) != 0) {
1405 goto err_out3;
1406 }
1407
1408 fuse_daemonize(opts.foreground);
1409
1410 /* Block until ctrl+c or fusermount -u */
1411 ret = virtio_loop(se);
1412
1413 fuse_session_unmount(se);
1414 err_out3:
1415 fuse_remove_signal_handlers(se);
1416 err_out2:
1417 fuse_session_destroy(se);
1418 err_out1:
1419 fuse_opt_free_args(&args);
1420
1421 if (lo.root.fd >= 0) {
1422 close(lo.root.fd);
1423 }
1424
1425 return ret ? 1 : 0;
1426 }