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