virtiofsd: Handle hard reboot
[qemu/ar7.git] / tools / virtiofsd / fuse_lowlevel.c
blob65f91dabae214627a87afdd7cebb5d935962c866
1 /*
2 * FUSE: Filesystem in Userspace
3 * Copyright (C) 2001-2007 Miklos Szeredi <miklos@szeredi.hu>
5 * Implementation of (most of) the low-level FUSE API. The session loop
6 * functions are implemented in separate files.
8 * This program can be distributed under the terms of the GNU LGPLv2.
9 * See the file COPYING.LIB
12 #include "qemu/osdep.h"
13 #include "fuse_i.h"
14 #include "standard-headers/linux/fuse.h"
15 #include "fuse_misc.h"
16 #include "fuse_opt.h"
17 #include "fuse_virtio.h"
19 #include <assert.h>
20 #include <errno.h>
21 #include <limits.h>
22 #include <stdbool.h>
23 #include <stddef.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sys/file.h>
28 #include <unistd.h>
31 #define OFFSET_MAX 0x7fffffffffffffffLL
33 struct fuse_pollhandle {
34 uint64_t kh;
35 struct fuse_session *se;
38 static size_t pagesize;
40 static __attribute__((constructor)) void fuse_ll_init_pagesize(void)
42 pagesize = getpagesize();
45 static void convert_stat(const struct stat *stbuf, struct fuse_attr *attr)
47 *attr = (struct fuse_attr){
48 .ino = stbuf->st_ino,
49 .mode = stbuf->st_mode,
50 .nlink = stbuf->st_nlink,
51 .uid = stbuf->st_uid,
52 .gid = stbuf->st_gid,
53 .rdev = stbuf->st_rdev,
54 .size = stbuf->st_size,
55 .blksize = stbuf->st_blksize,
56 .blocks = stbuf->st_blocks,
57 .atime = stbuf->st_atime,
58 .mtime = stbuf->st_mtime,
59 .ctime = stbuf->st_ctime,
60 .atimensec = ST_ATIM_NSEC(stbuf),
61 .mtimensec = ST_MTIM_NSEC(stbuf),
62 .ctimensec = ST_CTIM_NSEC(stbuf),
66 static void convert_attr(const struct fuse_setattr_in *attr, struct stat *stbuf)
68 stbuf->st_mode = attr->mode;
69 stbuf->st_uid = attr->uid;
70 stbuf->st_gid = attr->gid;
71 stbuf->st_size = attr->size;
72 stbuf->st_atime = attr->atime;
73 stbuf->st_mtime = attr->mtime;
74 stbuf->st_ctime = attr->ctime;
75 ST_ATIM_NSEC_SET(stbuf, attr->atimensec);
76 ST_MTIM_NSEC_SET(stbuf, attr->mtimensec);
77 ST_CTIM_NSEC_SET(stbuf, attr->ctimensec);
80 static size_t iov_length(const struct iovec *iov, size_t count)
82 size_t seg;
83 size_t ret = 0;
85 for (seg = 0; seg < count; seg++) {
86 ret += iov[seg].iov_len;
88 return ret;
91 static void list_init_req(struct fuse_req *req)
93 req->next = req;
94 req->prev = req;
97 static void list_del_req(struct fuse_req *req)
99 struct fuse_req *prev = req->prev;
100 struct fuse_req *next = req->next;
101 prev->next = next;
102 next->prev = prev;
105 static void list_add_req(struct fuse_req *req, struct fuse_req *next)
107 struct fuse_req *prev = next->prev;
108 req->next = next;
109 req->prev = prev;
110 prev->next = req;
111 next->prev = req;
114 static void destroy_req(fuse_req_t req)
116 pthread_mutex_destroy(&req->lock);
117 free(req);
120 void fuse_free_req(fuse_req_t req)
122 int ctr;
123 struct fuse_session *se = req->se;
125 pthread_mutex_lock(&se->lock);
126 req->u.ni.func = NULL;
127 req->u.ni.data = NULL;
128 list_del_req(req);
129 ctr = --req->ctr;
130 req->ch = NULL;
131 pthread_mutex_unlock(&se->lock);
132 if (!ctr) {
133 destroy_req(req);
137 static struct fuse_req *fuse_ll_alloc_req(struct fuse_session *se)
139 struct fuse_req *req;
141 req = (struct fuse_req *)calloc(1, sizeof(struct fuse_req));
142 if (req == NULL) {
143 fuse_log(FUSE_LOG_ERR, "fuse: failed to allocate request\n");
144 } else {
145 req->se = se;
146 req->ctr = 1;
147 list_init_req(req);
148 fuse_mutex_init(&req->lock);
151 return req;
154 /* Send data. If *ch* is NULL, send via session master fd */
155 static int fuse_send_msg(struct fuse_session *se, struct fuse_chan *ch,
156 struct iovec *iov, int count)
158 struct fuse_out_header *out = iov[0].iov_base;
160 out->len = iov_length(iov, count);
161 if (out->unique == 0) {
162 fuse_log(FUSE_LOG_DEBUG, "NOTIFY: code=%d length=%u\n", out->error,
163 out->len);
164 } else if (out->error) {
165 fuse_log(FUSE_LOG_DEBUG,
166 " unique: %llu, error: %i (%s), outsize: %i\n",
167 (unsigned long long)out->unique, out->error,
168 strerror(-out->error), out->len);
169 } else {
170 fuse_log(FUSE_LOG_DEBUG, " unique: %llu, success, outsize: %i\n",
171 (unsigned long long)out->unique, out->len);
174 if (fuse_lowlevel_is_virtio(se)) {
175 return virtio_send_msg(se, ch, iov, count);
178 abort(); /* virtio should have taken it before here */
179 return 0;
183 int fuse_send_reply_iov_nofree(fuse_req_t req, int error, struct iovec *iov,
184 int count)
186 struct fuse_out_header out = {
187 .unique = req->unique,
188 .error = error,
191 if (error <= -1000 || error > 0) {
192 fuse_log(FUSE_LOG_ERR, "fuse: bad error value: %i\n", error);
193 error = -ERANGE;
196 iov[0].iov_base = &out;
197 iov[0].iov_len = sizeof(struct fuse_out_header);
199 return fuse_send_msg(req->se, req->ch, iov, count);
202 static int send_reply_iov(fuse_req_t req, int error, struct iovec *iov,
203 int count)
205 int res;
207 res = fuse_send_reply_iov_nofree(req, error, iov, count);
208 fuse_free_req(req);
209 return res;
212 static int send_reply(fuse_req_t req, int error, const void *arg,
213 size_t argsize)
215 struct iovec iov[2];
216 int count = 1;
217 if (argsize) {
218 iov[1].iov_base = (void *)arg;
219 iov[1].iov_len = argsize;
220 count++;
222 return send_reply_iov(req, error, iov, count);
225 int fuse_reply_iov(fuse_req_t req, const struct iovec *iov, int count)
227 int res;
228 struct iovec *padded_iov;
230 padded_iov = malloc((count + 1) * sizeof(struct iovec));
231 if (padded_iov == NULL) {
232 return fuse_reply_err(req, ENOMEM);
235 memcpy(padded_iov + 1, iov, count * sizeof(struct iovec));
236 count++;
238 res = send_reply_iov(req, 0, padded_iov, count);
239 free(padded_iov);
241 return res;
246 * 'buf` is allowed to be empty so that the proper size may be
247 * allocated by the caller
249 size_t fuse_add_direntry(fuse_req_t req, char *buf, size_t bufsize,
250 const char *name, const struct stat *stbuf, off_t off)
252 (void)req;
253 size_t namelen;
254 size_t entlen;
255 size_t entlen_padded;
256 struct fuse_dirent *dirent;
258 namelen = strlen(name);
259 entlen = FUSE_NAME_OFFSET + namelen;
260 entlen_padded = FUSE_DIRENT_ALIGN(entlen);
262 if ((buf == NULL) || (entlen_padded > bufsize)) {
263 return entlen_padded;
266 dirent = (struct fuse_dirent *)buf;
267 dirent->ino = stbuf->st_ino;
268 dirent->off = off;
269 dirent->namelen = namelen;
270 dirent->type = (stbuf->st_mode & S_IFMT) >> 12;
271 memcpy(dirent->name, name, namelen);
272 memset(dirent->name + namelen, 0, entlen_padded - entlen);
274 return entlen_padded;
277 static void convert_statfs(const struct statvfs *stbuf,
278 struct fuse_kstatfs *kstatfs)
280 *kstatfs = (struct fuse_kstatfs){
281 .bsize = stbuf->f_bsize,
282 .frsize = stbuf->f_frsize,
283 .blocks = stbuf->f_blocks,
284 .bfree = stbuf->f_bfree,
285 .bavail = stbuf->f_bavail,
286 .files = stbuf->f_files,
287 .ffree = stbuf->f_ffree,
288 .namelen = stbuf->f_namemax,
292 static int send_reply_ok(fuse_req_t req, const void *arg, size_t argsize)
294 return send_reply(req, 0, arg, argsize);
297 int fuse_reply_err(fuse_req_t req, int err)
299 return send_reply(req, -err, NULL, 0);
302 void fuse_reply_none(fuse_req_t req)
304 fuse_free_req(req);
307 static unsigned long calc_timeout_sec(double t)
309 if (t > (double)ULONG_MAX) {
310 return ULONG_MAX;
311 } else if (t < 0.0) {
312 return 0;
313 } else {
314 return (unsigned long)t;
318 static unsigned int calc_timeout_nsec(double t)
320 double f = t - (double)calc_timeout_sec(t);
321 if (f < 0.0) {
322 return 0;
323 } else if (f >= 0.999999999) {
324 return 999999999;
325 } else {
326 return (unsigned int)(f * 1.0e9);
330 static void fill_entry(struct fuse_entry_out *arg,
331 const struct fuse_entry_param *e)
333 *arg = (struct fuse_entry_out){
334 .nodeid = e->ino,
335 .generation = e->generation,
336 .entry_valid = calc_timeout_sec(e->entry_timeout),
337 .entry_valid_nsec = calc_timeout_nsec(e->entry_timeout),
338 .attr_valid = calc_timeout_sec(e->attr_timeout),
339 .attr_valid_nsec = calc_timeout_nsec(e->attr_timeout),
341 convert_stat(&e->attr, &arg->attr);
345 * `buf` is allowed to be empty so that the proper size may be
346 * allocated by the caller
348 size_t fuse_add_direntry_plus(fuse_req_t req, char *buf, size_t bufsize,
349 const char *name,
350 const struct fuse_entry_param *e, off_t off)
352 (void)req;
353 size_t namelen;
354 size_t entlen;
355 size_t entlen_padded;
357 namelen = strlen(name);
358 entlen = FUSE_NAME_OFFSET_DIRENTPLUS + namelen;
359 entlen_padded = FUSE_DIRENT_ALIGN(entlen);
360 if ((buf == NULL) || (entlen_padded > bufsize)) {
361 return entlen_padded;
364 struct fuse_direntplus *dp = (struct fuse_direntplus *)buf;
365 memset(&dp->entry_out, 0, sizeof(dp->entry_out));
366 fill_entry(&dp->entry_out, e);
368 struct fuse_dirent *dirent = &dp->dirent;
369 *dirent = (struct fuse_dirent){
370 .ino = e->attr.st_ino,
371 .off = off,
372 .namelen = namelen,
373 .type = (e->attr.st_mode & S_IFMT) >> 12,
375 memcpy(dirent->name, name, namelen);
376 memset(dirent->name + namelen, 0, entlen_padded - entlen);
378 return entlen_padded;
381 static void fill_open(struct fuse_open_out *arg, const struct fuse_file_info *f)
383 arg->fh = f->fh;
384 if (f->direct_io) {
385 arg->open_flags |= FOPEN_DIRECT_IO;
387 if (f->keep_cache) {
388 arg->open_flags |= FOPEN_KEEP_CACHE;
390 if (f->cache_readdir) {
391 arg->open_flags |= FOPEN_CACHE_DIR;
393 if (f->nonseekable) {
394 arg->open_flags |= FOPEN_NONSEEKABLE;
398 int fuse_reply_entry(fuse_req_t req, const struct fuse_entry_param *e)
400 struct fuse_entry_out arg;
401 size_t size = sizeof(arg);
403 memset(&arg, 0, sizeof(arg));
404 fill_entry(&arg, e);
405 return send_reply_ok(req, &arg, size);
408 int fuse_reply_create(fuse_req_t req, const struct fuse_entry_param *e,
409 const struct fuse_file_info *f)
411 char buf[sizeof(struct fuse_entry_out) + sizeof(struct fuse_open_out)];
412 size_t entrysize = sizeof(struct fuse_entry_out);
413 struct fuse_entry_out *earg = (struct fuse_entry_out *)buf;
414 struct fuse_open_out *oarg = (struct fuse_open_out *)(buf + entrysize);
416 memset(buf, 0, sizeof(buf));
417 fill_entry(earg, e);
418 fill_open(oarg, f);
419 return send_reply_ok(req, buf, entrysize + sizeof(struct fuse_open_out));
422 int fuse_reply_attr(fuse_req_t req, const struct stat *attr,
423 double attr_timeout)
425 struct fuse_attr_out arg;
426 size_t size = sizeof(arg);
428 memset(&arg, 0, sizeof(arg));
429 arg.attr_valid = calc_timeout_sec(attr_timeout);
430 arg.attr_valid_nsec = calc_timeout_nsec(attr_timeout);
431 convert_stat(attr, &arg.attr);
433 return send_reply_ok(req, &arg, size);
436 int fuse_reply_readlink(fuse_req_t req, const char *linkname)
438 return send_reply_ok(req, linkname, strlen(linkname));
441 int fuse_reply_open(fuse_req_t req, const struct fuse_file_info *f)
443 struct fuse_open_out arg;
445 memset(&arg, 0, sizeof(arg));
446 fill_open(&arg, f);
447 return send_reply_ok(req, &arg, sizeof(arg));
450 int fuse_reply_write(fuse_req_t req, size_t count)
452 struct fuse_write_out arg;
454 memset(&arg, 0, sizeof(arg));
455 arg.size = count;
457 return send_reply_ok(req, &arg, sizeof(arg));
460 int fuse_reply_buf(fuse_req_t req, const char *buf, size_t size)
462 return send_reply_ok(req, buf, size);
465 static int fuse_send_data_iov_fallback(struct fuse_session *se,
466 struct fuse_chan *ch, struct iovec *iov,
467 int iov_count, struct fuse_bufvec *buf,
468 size_t len)
470 /* Optimize common case */
471 if (buf->count == 1 && buf->idx == 0 && buf->off == 0 &&
472 !(buf->buf[0].flags & FUSE_BUF_IS_FD)) {
474 * FIXME: also avoid memory copy if there are multiple buffers
475 * but none of them contain an fd
478 iov[iov_count].iov_base = buf->buf[0].mem;
479 iov[iov_count].iov_len = len;
480 iov_count++;
481 return fuse_send_msg(se, ch, iov, iov_count);
484 if (fuse_lowlevel_is_virtio(se) && buf->count == 1 &&
485 buf->buf[0].flags == (FUSE_BUF_IS_FD | FUSE_BUF_FD_SEEK)) {
486 return virtio_send_data_iov(se, ch, iov, iov_count, buf, len);
489 abort(); /* Will have taken vhost path */
490 return 0;
493 static int fuse_send_data_iov(struct fuse_session *se, struct fuse_chan *ch,
494 struct iovec *iov, int iov_count,
495 struct fuse_bufvec *buf)
497 size_t len = fuse_buf_size(buf);
499 return fuse_send_data_iov_fallback(se, ch, iov, iov_count, buf, len);
502 int fuse_reply_data(fuse_req_t req, struct fuse_bufvec *bufv)
504 struct iovec iov[2];
505 struct fuse_out_header out = {
506 .unique = req->unique,
508 int res;
510 iov[0].iov_base = &out;
511 iov[0].iov_len = sizeof(struct fuse_out_header);
513 res = fuse_send_data_iov(req->se, req->ch, iov, 1, bufv);
514 if (res <= 0) {
515 fuse_free_req(req);
516 return res;
517 } else {
518 return fuse_reply_err(req, res);
522 int fuse_reply_statfs(fuse_req_t req, const struct statvfs *stbuf)
524 struct fuse_statfs_out arg;
525 size_t size = sizeof(arg);
527 memset(&arg, 0, sizeof(arg));
528 convert_statfs(stbuf, &arg.st);
530 return send_reply_ok(req, &arg, size);
533 int fuse_reply_xattr(fuse_req_t req, size_t count)
535 struct fuse_getxattr_out arg;
537 memset(&arg, 0, sizeof(arg));
538 arg.size = count;
540 return send_reply_ok(req, &arg, sizeof(arg));
543 int fuse_reply_lock(fuse_req_t req, const struct flock *lock)
545 struct fuse_lk_out arg;
547 memset(&arg, 0, sizeof(arg));
548 arg.lk.type = lock->l_type;
549 if (lock->l_type != F_UNLCK) {
550 arg.lk.start = lock->l_start;
551 if (lock->l_len == 0) {
552 arg.lk.end = OFFSET_MAX;
553 } else {
554 arg.lk.end = lock->l_start + lock->l_len - 1;
557 arg.lk.pid = lock->l_pid;
558 return send_reply_ok(req, &arg, sizeof(arg));
561 int fuse_reply_bmap(fuse_req_t req, uint64_t idx)
563 struct fuse_bmap_out arg;
565 memset(&arg, 0, sizeof(arg));
566 arg.block = idx;
568 return send_reply_ok(req, &arg, sizeof(arg));
571 static struct fuse_ioctl_iovec *fuse_ioctl_iovec_copy(const struct iovec *iov,
572 size_t count)
574 struct fuse_ioctl_iovec *fiov;
575 size_t i;
577 fiov = malloc(sizeof(fiov[0]) * count);
578 if (!fiov) {
579 return NULL;
582 for (i = 0; i < count; i++) {
583 fiov[i].base = (uintptr_t)iov[i].iov_base;
584 fiov[i].len = iov[i].iov_len;
587 return fiov;
590 int fuse_reply_ioctl_retry(fuse_req_t req, const struct iovec *in_iov,
591 size_t in_count, const struct iovec *out_iov,
592 size_t out_count)
594 struct fuse_ioctl_out arg;
595 struct fuse_ioctl_iovec *in_fiov = NULL;
596 struct fuse_ioctl_iovec *out_fiov = NULL;
597 struct iovec iov[4];
598 size_t count = 1;
599 int res;
601 memset(&arg, 0, sizeof(arg));
602 arg.flags |= FUSE_IOCTL_RETRY;
603 arg.in_iovs = in_count;
604 arg.out_iovs = out_count;
605 iov[count].iov_base = &arg;
606 iov[count].iov_len = sizeof(arg);
607 count++;
609 /* Can't handle non-compat 64bit ioctls on 32bit */
610 if (sizeof(void *) == 4 && req->ioctl_64bit) {
611 res = fuse_reply_err(req, EINVAL);
612 goto out;
615 if (in_count) {
616 in_fiov = fuse_ioctl_iovec_copy(in_iov, in_count);
617 if (!in_fiov) {
618 goto enomem;
621 iov[count].iov_base = (void *)in_fiov;
622 iov[count].iov_len = sizeof(in_fiov[0]) * in_count;
623 count++;
625 if (out_count) {
626 out_fiov = fuse_ioctl_iovec_copy(out_iov, out_count);
627 if (!out_fiov) {
628 goto enomem;
631 iov[count].iov_base = (void *)out_fiov;
632 iov[count].iov_len = sizeof(out_fiov[0]) * out_count;
633 count++;
636 res = send_reply_iov(req, 0, iov, count);
637 out:
638 free(in_fiov);
639 free(out_fiov);
641 return res;
643 enomem:
644 res = fuse_reply_err(req, ENOMEM);
645 goto out;
648 int fuse_reply_ioctl(fuse_req_t req, int result, const void *buf, size_t size)
650 struct fuse_ioctl_out arg;
651 struct iovec iov[3];
652 size_t count = 1;
654 memset(&arg, 0, sizeof(arg));
655 arg.result = result;
656 iov[count].iov_base = &arg;
657 iov[count].iov_len = sizeof(arg);
658 count++;
660 if (size) {
661 iov[count].iov_base = (char *)buf;
662 iov[count].iov_len = size;
663 count++;
666 return send_reply_iov(req, 0, iov, count);
669 int fuse_reply_ioctl_iov(fuse_req_t req, int result, const struct iovec *iov,
670 int count)
672 struct iovec *padded_iov;
673 struct fuse_ioctl_out arg;
674 int res;
676 padded_iov = malloc((count + 2) * sizeof(struct iovec));
677 if (padded_iov == NULL) {
678 return fuse_reply_err(req, ENOMEM);
681 memset(&arg, 0, sizeof(arg));
682 arg.result = result;
683 padded_iov[1].iov_base = &arg;
684 padded_iov[1].iov_len = sizeof(arg);
686 memcpy(&padded_iov[2], iov, count * sizeof(struct iovec));
688 res = send_reply_iov(req, 0, padded_iov, count + 2);
689 free(padded_iov);
691 return res;
694 int fuse_reply_poll(fuse_req_t req, unsigned revents)
696 struct fuse_poll_out arg;
698 memset(&arg, 0, sizeof(arg));
699 arg.revents = revents;
701 return send_reply_ok(req, &arg, sizeof(arg));
704 int fuse_reply_lseek(fuse_req_t req, off_t off)
706 struct fuse_lseek_out arg;
708 memset(&arg, 0, sizeof(arg));
709 arg.offset = off;
711 return send_reply_ok(req, &arg, sizeof(arg));
714 static void do_lookup(fuse_req_t req, fuse_ino_t nodeid,
715 struct fuse_mbuf_iter *iter)
717 const char *name = fuse_mbuf_iter_advance_str(iter);
718 if (!name) {
719 fuse_reply_err(req, EINVAL);
720 return;
723 if (req->se->op.lookup) {
724 req->se->op.lookup(req, nodeid, name);
725 } else {
726 fuse_reply_err(req, ENOSYS);
730 static void do_forget(fuse_req_t req, fuse_ino_t nodeid,
731 struct fuse_mbuf_iter *iter)
733 struct fuse_forget_in *arg;
735 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
736 if (!arg) {
737 fuse_reply_err(req, EINVAL);
738 return;
741 if (req->se->op.forget) {
742 req->se->op.forget(req, nodeid, arg->nlookup);
743 } else {
744 fuse_reply_none(req);
748 static void do_batch_forget(fuse_req_t req, fuse_ino_t nodeid,
749 struct fuse_mbuf_iter *iter)
751 struct fuse_batch_forget_in *arg;
752 struct fuse_forget_data *forgets;
753 size_t scount;
755 (void)nodeid;
757 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
758 if (!arg) {
759 fuse_reply_none(req);
760 return;
764 * Prevent integer overflow. The compiler emits the following warning
765 * unless we use the scount local variable:
767 * error: comparison is always false due to limited range of data type
768 * [-Werror=type-limits]
770 * This may be true on 64-bit hosts but we need this check for 32-bit
771 * hosts.
773 scount = arg->count;
774 if (scount > SIZE_MAX / sizeof(forgets[0])) {
775 fuse_reply_none(req);
776 return;
779 forgets = fuse_mbuf_iter_advance(iter, arg->count * sizeof(forgets[0]));
780 if (!forgets) {
781 fuse_reply_none(req);
782 return;
785 if (req->se->op.forget_multi) {
786 req->se->op.forget_multi(req, arg->count, forgets);
787 } else if (req->se->op.forget) {
788 unsigned int i;
790 for (i = 0; i < arg->count; i++) {
791 struct fuse_req *dummy_req;
793 dummy_req = fuse_ll_alloc_req(req->se);
794 if (dummy_req == NULL) {
795 break;
798 dummy_req->unique = req->unique;
799 dummy_req->ctx = req->ctx;
800 dummy_req->ch = NULL;
802 req->se->op.forget(dummy_req, forgets[i].ino, forgets[i].nlookup);
804 fuse_reply_none(req);
805 } else {
806 fuse_reply_none(req);
810 static void do_getattr(fuse_req_t req, fuse_ino_t nodeid,
811 struct fuse_mbuf_iter *iter)
813 struct fuse_file_info *fip = NULL;
814 struct fuse_file_info fi;
816 struct fuse_getattr_in *arg;
818 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
819 if (!arg) {
820 fuse_reply_err(req, EINVAL);
821 return;
824 if (arg->getattr_flags & FUSE_GETATTR_FH) {
825 memset(&fi, 0, sizeof(fi));
826 fi.fh = arg->fh;
827 fip = &fi;
830 if (req->se->op.getattr) {
831 req->se->op.getattr(req, nodeid, fip);
832 } else {
833 fuse_reply_err(req, ENOSYS);
837 static void do_setattr(fuse_req_t req, fuse_ino_t nodeid,
838 struct fuse_mbuf_iter *iter)
840 if (req->se->op.setattr) {
841 struct fuse_setattr_in *arg;
842 struct fuse_file_info *fi = NULL;
843 struct fuse_file_info fi_store;
844 struct stat stbuf;
846 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
847 if (!arg) {
848 fuse_reply_err(req, EINVAL);
849 return;
852 memset(&stbuf, 0, sizeof(stbuf));
853 convert_attr(arg, &stbuf);
854 if (arg->valid & FATTR_FH) {
855 arg->valid &= ~FATTR_FH;
856 memset(&fi_store, 0, sizeof(fi_store));
857 fi = &fi_store;
858 fi->fh = arg->fh;
860 arg->valid &= FUSE_SET_ATTR_MODE | FUSE_SET_ATTR_UID |
861 FUSE_SET_ATTR_GID | FUSE_SET_ATTR_SIZE |
862 FUSE_SET_ATTR_ATIME | FUSE_SET_ATTR_MTIME |
863 FUSE_SET_ATTR_ATIME_NOW | FUSE_SET_ATTR_MTIME_NOW |
864 FUSE_SET_ATTR_CTIME;
866 req->se->op.setattr(req, nodeid, &stbuf, arg->valid, fi);
867 } else {
868 fuse_reply_err(req, ENOSYS);
872 static void do_access(fuse_req_t req, fuse_ino_t nodeid,
873 struct fuse_mbuf_iter *iter)
875 struct fuse_access_in *arg;
877 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
878 if (!arg) {
879 fuse_reply_err(req, EINVAL);
880 return;
883 if (req->se->op.access) {
884 req->se->op.access(req, nodeid, arg->mask);
885 } else {
886 fuse_reply_err(req, ENOSYS);
890 static void do_readlink(fuse_req_t req, fuse_ino_t nodeid,
891 struct fuse_mbuf_iter *iter)
893 (void)iter;
895 if (req->se->op.readlink) {
896 req->se->op.readlink(req, nodeid);
897 } else {
898 fuse_reply_err(req, ENOSYS);
902 static void do_mknod(fuse_req_t req, fuse_ino_t nodeid,
903 struct fuse_mbuf_iter *iter)
905 struct fuse_mknod_in *arg;
906 const char *name;
908 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
909 name = fuse_mbuf_iter_advance_str(iter);
910 if (!arg || !name) {
911 fuse_reply_err(req, EINVAL);
912 return;
915 req->ctx.umask = arg->umask;
917 if (req->se->op.mknod) {
918 req->se->op.mknod(req, nodeid, name, arg->mode, arg->rdev);
919 } else {
920 fuse_reply_err(req, ENOSYS);
924 static void do_mkdir(fuse_req_t req, fuse_ino_t nodeid,
925 struct fuse_mbuf_iter *iter)
927 struct fuse_mkdir_in *arg;
928 const char *name;
930 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
931 name = fuse_mbuf_iter_advance_str(iter);
932 if (!arg || !name) {
933 fuse_reply_err(req, EINVAL);
934 return;
937 req->ctx.umask = arg->umask;
939 if (req->se->op.mkdir) {
940 req->se->op.mkdir(req, nodeid, name, arg->mode);
941 } else {
942 fuse_reply_err(req, ENOSYS);
946 static void do_unlink(fuse_req_t req, fuse_ino_t nodeid,
947 struct fuse_mbuf_iter *iter)
949 const char *name = fuse_mbuf_iter_advance_str(iter);
951 if (!name) {
952 fuse_reply_err(req, EINVAL);
953 return;
956 if (req->se->op.unlink) {
957 req->se->op.unlink(req, nodeid, name);
958 } else {
959 fuse_reply_err(req, ENOSYS);
963 static void do_rmdir(fuse_req_t req, fuse_ino_t nodeid,
964 struct fuse_mbuf_iter *iter)
966 const char *name = fuse_mbuf_iter_advance_str(iter);
968 if (!name) {
969 fuse_reply_err(req, EINVAL);
970 return;
973 if (req->se->op.rmdir) {
974 req->se->op.rmdir(req, nodeid, name);
975 } else {
976 fuse_reply_err(req, ENOSYS);
980 static void do_symlink(fuse_req_t req, fuse_ino_t nodeid,
981 struct fuse_mbuf_iter *iter)
983 const char *name = fuse_mbuf_iter_advance_str(iter);
984 const char *linkname = fuse_mbuf_iter_advance_str(iter);
986 if (!name || !linkname) {
987 fuse_reply_err(req, EINVAL);
988 return;
991 if (req->se->op.symlink) {
992 req->se->op.symlink(req, linkname, nodeid, name);
993 } else {
994 fuse_reply_err(req, ENOSYS);
998 static void do_rename(fuse_req_t req, fuse_ino_t nodeid,
999 struct fuse_mbuf_iter *iter)
1001 struct fuse_rename_in *arg;
1002 const char *oldname;
1003 const char *newname;
1005 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1006 oldname = fuse_mbuf_iter_advance_str(iter);
1007 newname = fuse_mbuf_iter_advance_str(iter);
1008 if (!arg || !oldname || !newname) {
1009 fuse_reply_err(req, EINVAL);
1010 return;
1013 if (req->se->op.rename) {
1014 req->se->op.rename(req, nodeid, oldname, arg->newdir, newname, 0);
1015 } else {
1016 fuse_reply_err(req, ENOSYS);
1020 static void do_rename2(fuse_req_t req, fuse_ino_t nodeid,
1021 struct fuse_mbuf_iter *iter)
1023 struct fuse_rename2_in *arg;
1024 const char *oldname;
1025 const char *newname;
1027 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1028 oldname = fuse_mbuf_iter_advance_str(iter);
1029 newname = fuse_mbuf_iter_advance_str(iter);
1030 if (!arg || !oldname || !newname) {
1031 fuse_reply_err(req, EINVAL);
1032 return;
1035 if (req->se->op.rename) {
1036 req->se->op.rename(req, nodeid, oldname, arg->newdir, newname,
1037 arg->flags);
1038 } else {
1039 fuse_reply_err(req, ENOSYS);
1043 static void do_link(fuse_req_t req, fuse_ino_t nodeid,
1044 struct fuse_mbuf_iter *iter)
1046 struct fuse_link_in *arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1047 const char *name = fuse_mbuf_iter_advance_str(iter);
1049 if (!arg || !name) {
1050 fuse_reply_err(req, EINVAL);
1051 return;
1054 if (req->se->op.link) {
1055 req->se->op.link(req, arg->oldnodeid, nodeid, name);
1056 } else {
1057 fuse_reply_err(req, ENOSYS);
1061 static void do_create(fuse_req_t req, fuse_ino_t nodeid,
1062 struct fuse_mbuf_iter *iter)
1064 if (req->se->op.create) {
1065 struct fuse_create_in *arg;
1066 struct fuse_file_info fi;
1067 const char *name;
1069 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1070 name = fuse_mbuf_iter_advance_str(iter);
1071 if (!arg || !name) {
1072 fuse_reply_err(req, EINVAL);
1073 return;
1076 memset(&fi, 0, sizeof(fi));
1077 fi.flags = arg->flags;
1079 req->ctx.umask = arg->umask;
1081 req->se->op.create(req, nodeid, name, arg->mode, &fi);
1082 } else {
1083 fuse_reply_err(req, ENOSYS);
1087 static void do_open(fuse_req_t req, fuse_ino_t nodeid,
1088 struct fuse_mbuf_iter *iter)
1090 struct fuse_open_in *arg;
1091 struct fuse_file_info fi;
1093 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1094 if (!arg) {
1095 fuse_reply_err(req, EINVAL);
1096 return;
1099 memset(&fi, 0, sizeof(fi));
1100 fi.flags = arg->flags;
1102 if (req->se->op.open) {
1103 req->se->op.open(req, nodeid, &fi);
1104 } else {
1105 fuse_reply_open(req, &fi);
1109 static void do_read(fuse_req_t req, fuse_ino_t nodeid,
1110 struct fuse_mbuf_iter *iter)
1112 if (req->se->op.read) {
1113 struct fuse_read_in *arg;
1114 struct fuse_file_info fi;
1116 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1118 memset(&fi, 0, sizeof(fi));
1119 fi.fh = arg->fh;
1120 fi.lock_owner = arg->lock_owner;
1121 fi.flags = arg->flags;
1122 req->se->op.read(req, nodeid, arg->size, arg->offset, &fi);
1123 } else {
1124 fuse_reply_err(req, ENOSYS);
1128 static void do_write(fuse_req_t req, fuse_ino_t nodeid,
1129 struct fuse_mbuf_iter *iter)
1131 struct fuse_write_in *arg;
1132 struct fuse_file_info fi;
1133 const char *param;
1135 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1136 if (!arg) {
1137 fuse_reply_err(req, EINVAL);
1138 return;
1141 param = fuse_mbuf_iter_advance(iter, arg->size);
1142 if (!param) {
1143 fuse_reply_err(req, EINVAL);
1144 return;
1147 memset(&fi, 0, sizeof(fi));
1148 fi.fh = arg->fh;
1149 fi.writepage = (arg->write_flags & FUSE_WRITE_CACHE) != 0;
1150 fi.kill_priv = !!(arg->write_flags & FUSE_WRITE_KILL_PRIV);
1152 fi.lock_owner = arg->lock_owner;
1153 fi.flags = arg->flags;
1155 if (req->se->op.write) {
1156 req->se->op.write(req, nodeid, param, arg->size, arg->offset, &fi);
1157 } else {
1158 fuse_reply_err(req, ENOSYS);
1162 static void do_write_buf(fuse_req_t req, fuse_ino_t nodeid,
1163 struct fuse_mbuf_iter *iter, struct fuse_bufvec *ibufv)
1165 struct fuse_session *se = req->se;
1166 struct fuse_bufvec *pbufv = ibufv;
1167 struct fuse_bufvec tmpbufv = {
1168 .buf[0] = ibufv->buf[0],
1169 .count = 1,
1171 struct fuse_write_in *arg;
1172 size_t arg_size = sizeof(*arg);
1173 struct fuse_file_info fi;
1175 memset(&fi, 0, sizeof(fi));
1177 arg = fuse_mbuf_iter_advance(iter, arg_size);
1178 if (!arg) {
1179 fuse_reply_err(req, EINVAL);
1180 return;
1183 fi.lock_owner = arg->lock_owner;
1184 fi.flags = arg->flags;
1185 fi.fh = arg->fh;
1186 fi.writepage = !!(arg->write_flags & FUSE_WRITE_CACHE);
1187 fi.kill_priv = !!(arg->write_flags & FUSE_WRITE_KILL_PRIV);
1189 if (ibufv->count == 1) {
1190 assert(!(tmpbufv.buf[0].flags & FUSE_BUF_IS_FD));
1191 tmpbufv.buf[0].mem = ((char *)arg) + arg_size;
1192 tmpbufv.buf[0].size -= sizeof(struct fuse_in_header) + arg_size;
1193 pbufv = &tmpbufv;
1194 } else {
1196 * Input bufv contains the headers in the first element
1197 * and the data in the rest, we need to skip that first element
1199 ibufv->buf[0].size = 0;
1202 if (fuse_buf_size(pbufv) != arg->size) {
1203 fuse_log(FUSE_LOG_ERR,
1204 "fuse: do_write_buf: buffer size doesn't match arg->size\n");
1205 fuse_reply_err(req, EIO);
1206 return;
1209 se->op.write_buf(req, nodeid, pbufv, arg->offset, &fi);
1212 static void do_flush(fuse_req_t req, fuse_ino_t nodeid,
1213 struct fuse_mbuf_iter *iter)
1215 struct fuse_flush_in *arg;
1216 struct fuse_file_info fi;
1218 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1219 if (!arg) {
1220 fuse_reply_err(req, EINVAL);
1221 return;
1224 memset(&fi, 0, sizeof(fi));
1225 fi.fh = arg->fh;
1226 fi.flush = 1;
1227 fi.lock_owner = arg->lock_owner;
1229 if (req->se->op.flush) {
1230 req->se->op.flush(req, nodeid, &fi);
1231 } else {
1232 fuse_reply_err(req, ENOSYS);
1236 static void do_release(fuse_req_t req, fuse_ino_t nodeid,
1237 struct fuse_mbuf_iter *iter)
1239 struct fuse_release_in *arg;
1240 struct fuse_file_info fi;
1242 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1243 if (!arg) {
1244 fuse_reply_err(req, EINVAL);
1245 return;
1248 memset(&fi, 0, sizeof(fi));
1249 fi.flags = arg->flags;
1250 fi.fh = arg->fh;
1251 fi.flush = (arg->release_flags & FUSE_RELEASE_FLUSH) ? 1 : 0;
1252 fi.lock_owner = arg->lock_owner;
1254 if (arg->release_flags & FUSE_RELEASE_FLOCK_UNLOCK) {
1255 fi.flock_release = 1;
1258 if (req->se->op.release) {
1259 req->se->op.release(req, nodeid, &fi);
1260 } else {
1261 fuse_reply_err(req, 0);
1265 static void do_fsync(fuse_req_t req, fuse_ino_t nodeid,
1266 struct fuse_mbuf_iter *iter)
1268 struct fuse_fsync_in *arg;
1269 struct fuse_file_info fi;
1270 int datasync;
1272 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1273 if (!arg) {
1274 fuse_reply_err(req, EINVAL);
1275 return;
1277 datasync = arg->fsync_flags & 1;
1279 memset(&fi, 0, sizeof(fi));
1280 fi.fh = arg->fh;
1282 if (req->se->op.fsync) {
1283 if (fi.fh == (uint64_t)-1) {
1284 req->se->op.fsync(req, nodeid, datasync, NULL);
1285 } else {
1286 req->se->op.fsync(req, nodeid, datasync, &fi);
1288 } else {
1289 fuse_reply_err(req, ENOSYS);
1293 static void do_opendir(fuse_req_t req, fuse_ino_t nodeid,
1294 struct fuse_mbuf_iter *iter)
1296 struct fuse_open_in *arg;
1297 struct fuse_file_info fi;
1299 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1300 if (!arg) {
1301 fuse_reply_err(req, EINVAL);
1302 return;
1305 memset(&fi, 0, sizeof(fi));
1306 fi.flags = arg->flags;
1308 if (req->se->op.opendir) {
1309 req->se->op.opendir(req, nodeid, &fi);
1310 } else {
1311 fuse_reply_open(req, &fi);
1315 static void do_readdir(fuse_req_t req, fuse_ino_t nodeid,
1316 struct fuse_mbuf_iter *iter)
1318 struct fuse_read_in *arg;
1319 struct fuse_file_info fi;
1321 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1322 if (!arg) {
1323 fuse_reply_err(req, EINVAL);
1324 return;
1327 memset(&fi, 0, sizeof(fi));
1328 fi.fh = arg->fh;
1330 if (req->se->op.readdir) {
1331 req->se->op.readdir(req, nodeid, arg->size, arg->offset, &fi);
1332 } else {
1333 fuse_reply_err(req, ENOSYS);
1337 static void do_readdirplus(fuse_req_t req, fuse_ino_t nodeid,
1338 struct fuse_mbuf_iter *iter)
1340 struct fuse_read_in *arg;
1341 struct fuse_file_info fi;
1343 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1344 if (!arg) {
1345 fuse_reply_err(req, EINVAL);
1346 return;
1349 memset(&fi, 0, sizeof(fi));
1350 fi.fh = arg->fh;
1352 if (req->se->op.readdirplus) {
1353 req->se->op.readdirplus(req, nodeid, arg->size, arg->offset, &fi);
1354 } else {
1355 fuse_reply_err(req, ENOSYS);
1359 static void do_releasedir(fuse_req_t req, fuse_ino_t nodeid,
1360 struct fuse_mbuf_iter *iter)
1362 struct fuse_release_in *arg;
1363 struct fuse_file_info fi;
1365 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1366 if (!arg) {
1367 fuse_reply_err(req, EINVAL);
1368 return;
1371 memset(&fi, 0, sizeof(fi));
1372 fi.flags = arg->flags;
1373 fi.fh = arg->fh;
1375 if (req->se->op.releasedir) {
1376 req->se->op.releasedir(req, nodeid, &fi);
1377 } else {
1378 fuse_reply_err(req, 0);
1382 static void do_fsyncdir(fuse_req_t req, fuse_ino_t nodeid,
1383 struct fuse_mbuf_iter *iter)
1385 struct fuse_fsync_in *arg;
1386 struct fuse_file_info fi;
1387 int datasync;
1389 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1390 if (!arg) {
1391 fuse_reply_err(req, EINVAL);
1392 return;
1394 datasync = arg->fsync_flags & 1;
1396 memset(&fi, 0, sizeof(fi));
1397 fi.fh = arg->fh;
1399 if (req->se->op.fsyncdir) {
1400 req->se->op.fsyncdir(req, nodeid, datasync, &fi);
1401 } else {
1402 fuse_reply_err(req, ENOSYS);
1406 static void do_statfs(fuse_req_t req, fuse_ino_t nodeid,
1407 struct fuse_mbuf_iter *iter)
1409 (void)nodeid;
1410 (void)iter;
1412 if (req->se->op.statfs) {
1413 req->se->op.statfs(req, nodeid);
1414 } else {
1415 struct statvfs buf = {
1416 .f_namemax = 255,
1417 .f_bsize = 512,
1419 fuse_reply_statfs(req, &buf);
1423 static void do_setxattr(fuse_req_t req, fuse_ino_t nodeid,
1424 struct fuse_mbuf_iter *iter)
1426 struct fuse_setxattr_in *arg;
1427 const char *name;
1428 const char *value;
1430 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1431 name = fuse_mbuf_iter_advance_str(iter);
1432 if (!arg || !name) {
1433 fuse_reply_err(req, EINVAL);
1434 return;
1437 value = fuse_mbuf_iter_advance(iter, arg->size);
1438 if (!value) {
1439 fuse_reply_err(req, EINVAL);
1440 return;
1443 if (req->se->op.setxattr) {
1444 req->se->op.setxattr(req, nodeid, name, value, arg->size, arg->flags);
1445 } else {
1446 fuse_reply_err(req, ENOSYS);
1450 static void do_getxattr(fuse_req_t req, fuse_ino_t nodeid,
1451 struct fuse_mbuf_iter *iter)
1453 struct fuse_getxattr_in *arg;
1454 const char *name;
1456 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1457 name = fuse_mbuf_iter_advance_str(iter);
1458 if (!arg || !name) {
1459 fuse_reply_err(req, EINVAL);
1460 return;
1463 if (req->se->op.getxattr) {
1464 req->se->op.getxattr(req, nodeid, name, arg->size);
1465 } else {
1466 fuse_reply_err(req, ENOSYS);
1470 static void do_listxattr(fuse_req_t req, fuse_ino_t nodeid,
1471 struct fuse_mbuf_iter *iter)
1473 struct fuse_getxattr_in *arg;
1475 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1476 if (!arg) {
1477 fuse_reply_err(req, EINVAL);
1478 return;
1481 if (req->se->op.listxattr) {
1482 req->se->op.listxattr(req, nodeid, arg->size);
1483 } else {
1484 fuse_reply_err(req, ENOSYS);
1488 static void do_removexattr(fuse_req_t req, fuse_ino_t nodeid,
1489 struct fuse_mbuf_iter *iter)
1491 const char *name = fuse_mbuf_iter_advance_str(iter);
1493 if (!name) {
1494 fuse_reply_err(req, EINVAL);
1495 return;
1498 if (req->se->op.removexattr) {
1499 req->se->op.removexattr(req, nodeid, name);
1500 } else {
1501 fuse_reply_err(req, ENOSYS);
1505 static void convert_fuse_file_lock(struct fuse_file_lock *fl,
1506 struct flock *flock)
1508 memset(flock, 0, sizeof(struct flock));
1509 flock->l_type = fl->type;
1510 flock->l_whence = SEEK_SET;
1511 flock->l_start = fl->start;
1512 if (fl->end == OFFSET_MAX) {
1513 flock->l_len = 0;
1514 } else {
1515 flock->l_len = fl->end - fl->start + 1;
1517 flock->l_pid = fl->pid;
1520 static void do_getlk(fuse_req_t req, fuse_ino_t nodeid,
1521 struct fuse_mbuf_iter *iter)
1523 struct fuse_lk_in *arg;
1524 struct fuse_file_info fi;
1525 struct flock flock;
1527 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1528 if (!arg) {
1529 fuse_reply_err(req, EINVAL);
1530 return;
1533 memset(&fi, 0, sizeof(fi));
1534 fi.fh = arg->fh;
1535 fi.lock_owner = arg->owner;
1537 convert_fuse_file_lock(&arg->lk, &flock);
1538 if (req->se->op.getlk) {
1539 req->se->op.getlk(req, nodeid, &fi, &flock);
1540 } else {
1541 fuse_reply_err(req, ENOSYS);
1545 static void do_setlk_common(fuse_req_t req, fuse_ino_t nodeid,
1546 struct fuse_mbuf_iter *iter, int sleep)
1548 struct fuse_lk_in *arg;
1549 struct fuse_file_info fi;
1550 struct flock flock;
1552 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1553 if (!arg) {
1554 fuse_reply_err(req, EINVAL);
1555 return;
1558 memset(&fi, 0, sizeof(fi));
1559 fi.fh = arg->fh;
1560 fi.lock_owner = arg->owner;
1562 if (arg->lk_flags & FUSE_LK_FLOCK) {
1563 int op = 0;
1565 switch (arg->lk.type) {
1566 case F_RDLCK:
1567 op = LOCK_SH;
1568 break;
1569 case F_WRLCK:
1570 op = LOCK_EX;
1571 break;
1572 case F_UNLCK:
1573 op = LOCK_UN;
1574 break;
1576 if (!sleep) {
1577 op |= LOCK_NB;
1580 if (req->se->op.flock) {
1581 req->se->op.flock(req, nodeid, &fi, op);
1582 } else {
1583 fuse_reply_err(req, ENOSYS);
1585 } else {
1586 convert_fuse_file_lock(&arg->lk, &flock);
1587 if (req->se->op.setlk) {
1588 req->se->op.setlk(req, nodeid, &fi, &flock, sleep);
1589 } else {
1590 fuse_reply_err(req, ENOSYS);
1595 static void do_setlk(fuse_req_t req, fuse_ino_t nodeid,
1596 struct fuse_mbuf_iter *iter)
1598 do_setlk_common(req, nodeid, iter, 0);
1601 static void do_setlkw(fuse_req_t req, fuse_ino_t nodeid,
1602 struct fuse_mbuf_iter *iter)
1604 do_setlk_common(req, nodeid, iter, 1);
1607 static int find_interrupted(struct fuse_session *se, struct fuse_req *req)
1609 struct fuse_req *curr;
1611 for (curr = se->list.next; curr != &se->list; curr = curr->next) {
1612 if (curr->unique == req->u.i.unique) {
1613 fuse_interrupt_func_t func;
1614 void *data;
1616 curr->ctr++;
1617 pthread_mutex_unlock(&se->lock);
1619 /* Ugh, ugly locking */
1620 pthread_mutex_lock(&curr->lock);
1621 pthread_mutex_lock(&se->lock);
1622 curr->interrupted = 1;
1623 func = curr->u.ni.func;
1624 data = curr->u.ni.data;
1625 pthread_mutex_unlock(&se->lock);
1626 if (func) {
1627 func(curr, data);
1629 pthread_mutex_unlock(&curr->lock);
1631 pthread_mutex_lock(&se->lock);
1632 curr->ctr--;
1633 if (!curr->ctr) {
1634 destroy_req(curr);
1637 return 1;
1640 for (curr = se->interrupts.next; curr != &se->interrupts;
1641 curr = curr->next) {
1642 if (curr->u.i.unique == req->u.i.unique) {
1643 return 1;
1646 return 0;
1649 static void do_interrupt(fuse_req_t req, fuse_ino_t nodeid,
1650 struct fuse_mbuf_iter *iter)
1652 struct fuse_interrupt_in *arg;
1653 struct fuse_session *se = req->se;
1655 (void)nodeid;
1657 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1658 if (!arg) {
1659 fuse_reply_err(req, EINVAL);
1660 return;
1663 fuse_log(FUSE_LOG_DEBUG, "INTERRUPT: %llu\n",
1664 (unsigned long long)arg->unique);
1666 req->u.i.unique = arg->unique;
1668 pthread_mutex_lock(&se->lock);
1669 if (find_interrupted(se, req)) {
1670 destroy_req(req);
1671 } else {
1672 list_add_req(req, &se->interrupts);
1674 pthread_mutex_unlock(&se->lock);
1677 static struct fuse_req *check_interrupt(struct fuse_session *se,
1678 struct fuse_req *req)
1680 struct fuse_req *curr;
1682 for (curr = se->interrupts.next; curr != &se->interrupts;
1683 curr = curr->next) {
1684 if (curr->u.i.unique == req->unique) {
1685 req->interrupted = 1;
1686 list_del_req(curr);
1687 free(curr);
1688 return NULL;
1691 curr = se->interrupts.next;
1692 if (curr != &se->interrupts) {
1693 list_del_req(curr);
1694 list_init_req(curr);
1695 return curr;
1696 } else {
1697 return NULL;
1701 static void do_bmap(fuse_req_t req, fuse_ino_t nodeid,
1702 struct fuse_mbuf_iter *iter)
1704 struct fuse_bmap_in *arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1706 if (!arg) {
1707 fuse_reply_err(req, EINVAL);
1708 return;
1711 if (req->se->op.bmap) {
1712 req->se->op.bmap(req, nodeid, arg->blocksize, arg->block);
1713 } else {
1714 fuse_reply_err(req, ENOSYS);
1718 static void do_ioctl(fuse_req_t req, fuse_ino_t nodeid,
1719 struct fuse_mbuf_iter *iter)
1721 struct fuse_ioctl_in *arg;
1722 unsigned int flags;
1723 void *in_buf = NULL;
1724 struct fuse_file_info fi;
1726 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1727 if (!arg) {
1728 fuse_reply_err(req, EINVAL);
1729 return;
1732 flags = arg->flags;
1733 if (flags & FUSE_IOCTL_DIR && !(req->se->conn.want & FUSE_CAP_IOCTL_DIR)) {
1734 fuse_reply_err(req, ENOTTY);
1735 return;
1738 if (arg->in_size) {
1739 in_buf = fuse_mbuf_iter_advance(iter, arg->in_size);
1740 if (!in_buf) {
1741 fuse_reply_err(req, EINVAL);
1742 return;
1746 memset(&fi, 0, sizeof(fi));
1747 fi.fh = arg->fh;
1749 if (sizeof(void *) == 4 && !(flags & FUSE_IOCTL_32BIT)) {
1750 req->ioctl_64bit = 1;
1753 if (req->se->op.ioctl) {
1754 req->se->op.ioctl(req, nodeid, arg->cmd, (void *)(uintptr_t)arg->arg,
1755 &fi, flags, in_buf, arg->in_size, arg->out_size);
1756 } else {
1757 fuse_reply_err(req, ENOSYS);
1761 void fuse_pollhandle_destroy(struct fuse_pollhandle *ph)
1763 free(ph);
1766 static void do_poll(fuse_req_t req, fuse_ino_t nodeid,
1767 struct fuse_mbuf_iter *iter)
1769 struct fuse_poll_in *arg;
1770 struct fuse_file_info fi;
1772 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1773 if (!arg) {
1774 fuse_reply_err(req, EINVAL);
1775 return;
1778 memset(&fi, 0, sizeof(fi));
1779 fi.fh = arg->fh;
1780 fi.poll_events = arg->events;
1782 if (req->se->op.poll) {
1783 struct fuse_pollhandle *ph = NULL;
1785 if (arg->flags & FUSE_POLL_SCHEDULE_NOTIFY) {
1786 ph = malloc(sizeof(struct fuse_pollhandle));
1787 if (ph == NULL) {
1788 fuse_reply_err(req, ENOMEM);
1789 return;
1791 ph->kh = arg->kh;
1792 ph->se = req->se;
1795 req->se->op.poll(req, nodeid, &fi, ph);
1796 } else {
1797 fuse_reply_err(req, ENOSYS);
1801 static void do_fallocate(fuse_req_t req, fuse_ino_t nodeid,
1802 struct fuse_mbuf_iter *iter)
1804 struct fuse_fallocate_in *arg;
1805 struct fuse_file_info fi;
1807 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1808 if (!arg) {
1809 fuse_reply_err(req, EINVAL);
1810 return;
1813 memset(&fi, 0, sizeof(fi));
1814 fi.fh = arg->fh;
1816 if (req->se->op.fallocate) {
1817 req->se->op.fallocate(req, nodeid, arg->mode, arg->offset, arg->length,
1818 &fi);
1819 } else {
1820 fuse_reply_err(req, ENOSYS);
1824 static void do_copy_file_range(fuse_req_t req, fuse_ino_t nodeid_in,
1825 struct fuse_mbuf_iter *iter)
1827 struct fuse_copy_file_range_in *arg;
1828 struct fuse_file_info fi_in, fi_out;
1830 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1831 if (!arg) {
1832 fuse_reply_err(req, EINVAL);
1833 return;
1836 memset(&fi_in, 0, sizeof(fi_in));
1837 fi_in.fh = arg->fh_in;
1839 memset(&fi_out, 0, sizeof(fi_out));
1840 fi_out.fh = arg->fh_out;
1843 if (req->se->op.copy_file_range) {
1844 req->se->op.copy_file_range(req, nodeid_in, arg->off_in, &fi_in,
1845 arg->nodeid_out, arg->off_out, &fi_out,
1846 arg->len, arg->flags);
1847 } else {
1848 fuse_reply_err(req, ENOSYS);
1852 static void do_lseek(fuse_req_t req, fuse_ino_t nodeid,
1853 struct fuse_mbuf_iter *iter)
1855 struct fuse_lseek_in *arg;
1856 struct fuse_file_info fi;
1858 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1859 if (!arg) {
1860 fuse_reply_err(req, EINVAL);
1861 return;
1863 memset(&fi, 0, sizeof(fi));
1864 fi.fh = arg->fh;
1866 if (req->se->op.lseek) {
1867 req->se->op.lseek(req, nodeid, arg->offset, arg->whence, &fi);
1868 } else {
1869 fuse_reply_err(req, ENOSYS);
1873 static void do_init(fuse_req_t req, fuse_ino_t nodeid,
1874 struct fuse_mbuf_iter *iter)
1876 size_t compat_size = offsetof(struct fuse_init_in, max_readahead);
1877 struct fuse_init_in *arg;
1878 struct fuse_init_out outarg;
1879 struct fuse_session *se = req->se;
1880 size_t bufsize = se->bufsize;
1881 size_t outargsize = sizeof(outarg);
1883 (void)nodeid;
1885 /* First consume the old fields... */
1886 arg = fuse_mbuf_iter_advance(iter, compat_size);
1887 if (!arg) {
1888 fuse_reply_err(req, EINVAL);
1889 return;
1892 /* ...and now consume the new fields. */
1893 if (arg->major == 7 && arg->minor >= 6) {
1894 if (!fuse_mbuf_iter_advance(iter, sizeof(*arg) - compat_size)) {
1895 fuse_reply_err(req, EINVAL);
1896 return;
1900 fuse_log(FUSE_LOG_DEBUG, "INIT: %u.%u\n", arg->major, arg->minor);
1901 if (arg->major == 7 && arg->minor >= 6) {
1902 fuse_log(FUSE_LOG_DEBUG, "flags=0x%08x\n", arg->flags);
1903 fuse_log(FUSE_LOG_DEBUG, "max_readahead=0x%08x\n", arg->max_readahead);
1905 se->conn.proto_major = arg->major;
1906 se->conn.proto_minor = arg->minor;
1907 se->conn.capable = 0;
1908 se->conn.want = 0;
1910 memset(&outarg, 0, sizeof(outarg));
1911 outarg.major = FUSE_KERNEL_VERSION;
1912 outarg.minor = FUSE_KERNEL_MINOR_VERSION;
1914 if (arg->major < 7 || (arg->major == 7 && arg->minor < 31)) {
1915 fuse_log(FUSE_LOG_ERR, "fuse: unsupported protocol version: %u.%u\n",
1916 arg->major, arg->minor);
1917 fuse_reply_err(req, EPROTO);
1918 return;
1921 if (arg->major > 7) {
1922 /* Wait for a second INIT request with a 7.X version */
1923 send_reply_ok(req, &outarg, sizeof(outarg));
1924 return;
1927 if (arg->max_readahead < se->conn.max_readahead) {
1928 se->conn.max_readahead = arg->max_readahead;
1930 if (arg->flags & FUSE_ASYNC_READ) {
1931 se->conn.capable |= FUSE_CAP_ASYNC_READ;
1933 if (arg->flags & FUSE_POSIX_LOCKS) {
1934 se->conn.capable |= FUSE_CAP_POSIX_LOCKS;
1936 if (arg->flags & FUSE_ATOMIC_O_TRUNC) {
1937 se->conn.capable |= FUSE_CAP_ATOMIC_O_TRUNC;
1939 if (arg->flags & FUSE_EXPORT_SUPPORT) {
1940 se->conn.capable |= FUSE_CAP_EXPORT_SUPPORT;
1942 if (arg->flags & FUSE_DONT_MASK) {
1943 se->conn.capable |= FUSE_CAP_DONT_MASK;
1945 if (arg->flags & FUSE_FLOCK_LOCKS) {
1946 se->conn.capable |= FUSE_CAP_FLOCK_LOCKS;
1948 if (arg->flags & FUSE_AUTO_INVAL_DATA) {
1949 se->conn.capable |= FUSE_CAP_AUTO_INVAL_DATA;
1951 if (arg->flags & FUSE_DO_READDIRPLUS) {
1952 se->conn.capable |= FUSE_CAP_READDIRPLUS;
1954 if (arg->flags & FUSE_READDIRPLUS_AUTO) {
1955 se->conn.capable |= FUSE_CAP_READDIRPLUS_AUTO;
1957 if (arg->flags & FUSE_ASYNC_DIO) {
1958 se->conn.capable |= FUSE_CAP_ASYNC_DIO;
1960 if (arg->flags & FUSE_WRITEBACK_CACHE) {
1961 se->conn.capable |= FUSE_CAP_WRITEBACK_CACHE;
1963 if (arg->flags & FUSE_NO_OPEN_SUPPORT) {
1964 se->conn.capable |= FUSE_CAP_NO_OPEN_SUPPORT;
1966 if (arg->flags & FUSE_PARALLEL_DIROPS) {
1967 se->conn.capable |= FUSE_CAP_PARALLEL_DIROPS;
1969 if (arg->flags & FUSE_POSIX_ACL) {
1970 se->conn.capable |= FUSE_CAP_POSIX_ACL;
1972 if (arg->flags & FUSE_HANDLE_KILLPRIV) {
1973 se->conn.capable |= FUSE_CAP_HANDLE_KILLPRIV;
1975 if (arg->flags & FUSE_NO_OPENDIR_SUPPORT) {
1976 se->conn.capable |= FUSE_CAP_NO_OPENDIR_SUPPORT;
1978 if (!(arg->flags & FUSE_MAX_PAGES)) {
1979 size_t max_bufsize = FUSE_DEFAULT_MAX_PAGES_PER_REQ * getpagesize() +
1980 FUSE_BUFFER_HEADER_SIZE;
1981 if (bufsize > max_bufsize) {
1982 bufsize = max_bufsize;
1985 #ifdef HAVE_SPLICE
1986 #ifdef HAVE_VMSPLICE
1987 se->conn.capable |= FUSE_CAP_SPLICE_WRITE | FUSE_CAP_SPLICE_MOVE;
1988 #endif
1989 se->conn.capable |= FUSE_CAP_SPLICE_READ;
1990 #endif
1991 se->conn.capable |= FUSE_CAP_IOCTL_DIR;
1994 * Default settings for modern filesystems.
1996 * Most of these capabilities were disabled by default in
1997 * libfuse2 for backwards compatibility reasons. In libfuse3,
1998 * we can finally enable them by default (as long as they're
1999 * supported by the kernel).
2001 #define LL_SET_DEFAULT(cond, cap) \
2002 if ((cond) && (se->conn.capable & (cap))) \
2003 se->conn.want |= (cap)
2004 LL_SET_DEFAULT(1, FUSE_CAP_ASYNC_READ);
2005 LL_SET_DEFAULT(1, FUSE_CAP_PARALLEL_DIROPS);
2006 LL_SET_DEFAULT(1, FUSE_CAP_AUTO_INVAL_DATA);
2007 LL_SET_DEFAULT(1, FUSE_CAP_HANDLE_KILLPRIV);
2008 LL_SET_DEFAULT(1, FUSE_CAP_ASYNC_DIO);
2009 LL_SET_DEFAULT(1, FUSE_CAP_IOCTL_DIR);
2010 LL_SET_DEFAULT(1, FUSE_CAP_ATOMIC_O_TRUNC);
2011 LL_SET_DEFAULT(se->op.write_buf, FUSE_CAP_SPLICE_READ);
2012 LL_SET_DEFAULT(se->op.getlk && se->op.setlk, FUSE_CAP_POSIX_LOCKS);
2013 LL_SET_DEFAULT(se->op.flock, FUSE_CAP_FLOCK_LOCKS);
2014 LL_SET_DEFAULT(se->op.readdirplus, FUSE_CAP_READDIRPLUS);
2015 LL_SET_DEFAULT(se->op.readdirplus && se->op.readdir,
2016 FUSE_CAP_READDIRPLUS_AUTO);
2017 se->conn.time_gran = 1;
2019 if (bufsize < FUSE_MIN_READ_BUFFER) {
2020 fuse_log(FUSE_LOG_ERR, "fuse: warning: buffer size too small: %zu\n",
2021 bufsize);
2022 bufsize = FUSE_MIN_READ_BUFFER;
2024 se->bufsize = bufsize;
2026 if (se->conn.max_write > bufsize - FUSE_BUFFER_HEADER_SIZE) {
2027 se->conn.max_write = bufsize - FUSE_BUFFER_HEADER_SIZE;
2030 se->got_init = 1;
2031 se->got_destroy = 0;
2032 if (se->op.init) {
2033 se->op.init(se->userdata, &se->conn);
2036 if (se->conn.want & (~se->conn.capable)) {
2037 fuse_log(FUSE_LOG_ERR,
2038 "fuse: error: filesystem requested capabilities "
2039 "0x%x that are not supported by kernel, aborting.\n",
2040 se->conn.want & (~se->conn.capable));
2041 fuse_reply_err(req, EPROTO);
2042 se->error = -EPROTO;
2043 fuse_session_exit(se);
2044 return;
2047 if (se->conn.max_write < bufsize - FUSE_BUFFER_HEADER_SIZE) {
2048 se->bufsize = se->conn.max_write + FUSE_BUFFER_HEADER_SIZE;
2050 if (arg->flags & FUSE_MAX_PAGES) {
2051 outarg.flags |= FUSE_MAX_PAGES;
2052 outarg.max_pages = (se->conn.max_write - 1) / getpagesize() + 1;
2056 * Always enable big writes, this is superseded
2057 * by the max_write option
2059 outarg.flags |= FUSE_BIG_WRITES;
2061 if (se->conn.want & FUSE_CAP_ASYNC_READ) {
2062 outarg.flags |= FUSE_ASYNC_READ;
2064 if (se->conn.want & FUSE_CAP_POSIX_LOCKS) {
2065 outarg.flags |= FUSE_POSIX_LOCKS;
2067 if (se->conn.want & FUSE_CAP_ATOMIC_O_TRUNC) {
2068 outarg.flags |= FUSE_ATOMIC_O_TRUNC;
2070 if (se->conn.want & FUSE_CAP_EXPORT_SUPPORT) {
2071 outarg.flags |= FUSE_EXPORT_SUPPORT;
2073 if (se->conn.want & FUSE_CAP_DONT_MASK) {
2074 outarg.flags |= FUSE_DONT_MASK;
2076 if (se->conn.want & FUSE_CAP_FLOCK_LOCKS) {
2077 outarg.flags |= FUSE_FLOCK_LOCKS;
2079 if (se->conn.want & FUSE_CAP_AUTO_INVAL_DATA) {
2080 outarg.flags |= FUSE_AUTO_INVAL_DATA;
2082 if (se->conn.want & FUSE_CAP_READDIRPLUS) {
2083 outarg.flags |= FUSE_DO_READDIRPLUS;
2085 if (se->conn.want & FUSE_CAP_READDIRPLUS_AUTO) {
2086 outarg.flags |= FUSE_READDIRPLUS_AUTO;
2088 if (se->conn.want & FUSE_CAP_ASYNC_DIO) {
2089 outarg.flags |= FUSE_ASYNC_DIO;
2091 if (se->conn.want & FUSE_CAP_WRITEBACK_CACHE) {
2092 outarg.flags |= FUSE_WRITEBACK_CACHE;
2094 if (se->conn.want & FUSE_CAP_POSIX_ACL) {
2095 outarg.flags |= FUSE_POSIX_ACL;
2097 outarg.max_readahead = se->conn.max_readahead;
2098 outarg.max_write = se->conn.max_write;
2099 if (se->conn.max_background >= (1 << 16)) {
2100 se->conn.max_background = (1 << 16) - 1;
2102 if (se->conn.congestion_threshold > se->conn.max_background) {
2103 se->conn.congestion_threshold = se->conn.max_background;
2105 if (!se->conn.congestion_threshold) {
2106 se->conn.congestion_threshold = se->conn.max_background * 3 / 4;
2109 outarg.max_background = se->conn.max_background;
2110 outarg.congestion_threshold = se->conn.congestion_threshold;
2111 outarg.time_gran = se->conn.time_gran;
2113 fuse_log(FUSE_LOG_DEBUG, " INIT: %u.%u\n", outarg.major, outarg.minor);
2114 fuse_log(FUSE_LOG_DEBUG, " flags=0x%08x\n", outarg.flags);
2115 fuse_log(FUSE_LOG_DEBUG, " max_readahead=0x%08x\n", outarg.max_readahead);
2116 fuse_log(FUSE_LOG_DEBUG, " max_write=0x%08x\n", outarg.max_write);
2117 fuse_log(FUSE_LOG_DEBUG, " max_background=%i\n", outarg.max_background);
2118 fuse_log(FUSE_LOG_DEBUG, " congestion_threshold=%i\n",
2119 outarg.congestion_threshold);
2120 fuse_log(FUSE_LOG_DEBUG, " time_gran=%u\n", outarg.time_gran);
2122 send_reply_ok(req, &outarg, outargsize);
2125 static void do_destroy(fuse_req_t req, fuse_ino_t nodeid,
2126 struct fuse_mbuf_iter *iter)
2128 struct fuse_session *se = req->se;
2130 (void)nodeid;
2131 (void)iter;
2133 se->got_destroy = 1;
2134 se->got_init = 0;
2135 if (se->op.destroy) {
2136 se->op.destroy(se->userdata);
2139 send_reply_ok(req, NULL, 0);
2142 static int send_notify_iov(struct fuse_session *se, int notify_code,
2143 struct iovec *iov, int count)
2145 struct fuse_out_header out = {
2146 .error = notify_code,
2149 if (!se->got_init) {
2150 return -ENOTCONN;
2153 iov[0].iov_base = &out;
2154 iov[0].iov_len = sizeof(struct fuse_out_header);
2156 return fuse_send_msg(se, NULL, iov, count);
2159 int fuse_lowlevel_notify_poll(struct fuse_pollhandle *ph)
2161 if (ph != NULL) {
2162 struct fuse_notify_poll_wakeup_out outarg = {
2163 .kh = ph->kh,
2165 struct iovec iov[2];
2167 iov[1].iov_base = &outarg;
2168 iov[1].iov_len = sizeof(outarg);
2170 return send_notify_iov(ph->se, FUSE_NOTIFY_POLL, iov, 2);
2171 } else {
2172 return 0;
2176 int fuse_lowlevel_notify_inval_inode(struct fuse_session *se, fuse_ino_t ino,
2177 off_t off, off_t len)
2179 struct fuse_notify_inval_inode_out outarg = {
2180 .ino = ino,
2181 .off = off,
2182 .len = len,
2184 struct iovec iov[2];
2186 if (!se) {
2187 return -EINVAL;
2190 iov[1].iov_base = &outarg;
2191 iov[1].iov_len = sizeof(outarg);
2193 return send_notify_iov(se, FUSE_NOTIFY_INVAL_INODE, iov, 2);
2196 int fuse_lowlevel_notify_inval_entry(struct fuse_session *se, fuse_ino_t parent,
2197 const char *name, size_t namelen)
2199 struct fuse_notify_inval_entry_out outarg = {
2200 .parent = parent,
2201 .namelen = namelen,
2203 struct iovec iov[3];
2205 if (!se) {
2206 return -EINVAL;
2209 iov[1].iov_base = &outarg;
2210 iov[1].iov_len = sizeof(outarg);
2211 iov[2].iov_base = (void *)name;
2212 iov[2].iov_len = namelen + 1;
2214 return send_notify_iov(se, FUSE_NOTIFY_INVAL_ENTRY, iov, 3);
2217 int fuse_lowlevel_notify_delete(struct fuse_session *se, fuse_ino_t parent,
2218 fuse_ino_t child, const char *name,
2219 size_t namelen)
2221 struct fuse_notify_delete_out outarg = {
2222 .parent = parent,
2223 .child = child,
2224 .namelen = namelen,
2226 struct iovec iov[3];
2228 if (!se) {
2229 return -EINVAL;
2232 iov[1].iov_base = &outarg;
2233 iov[1].iov_len = sizeof(outarg);
2234 iov[2].iov_base = (void *)name;
2235 iov[2].iov_len = namelen + 1;
2237 return send_notify_iov(se, FUSE_NOTIFY_DELETE, iov, 3);
2240 int fuse_lowlevel_notify_store(struct fuse_session *se, fuse_ino_t ino,
2241 off_t offset, struct fuse_bufvec *bufv)
2243 struct fuse_out_header out = {
2244 .error = FUSE_NOTIFY_STORE,
2246 struct fuse_notify_store_out outarg = {
2247 .nodeid = ino,
2248 .offset = offset,
2249 .size = fuse_buf_size(bufv),
2251 struct iovec iov[3];
2252 int res;
2254 if (!se) {
2255 return -EINVAL;
2258 iov[0].iov_base = &out;
2259 iov[0].iov_len = sizeof(out);
2260 iov[1].iov_base = &outarg;
2261 iov[1].iov_len = sizeof(outarg);
2263 res = fuse_send_data_iov(se, NULL, iov, 2, bufv);
2264 if (res > 0) {
2265 res = -res;
2268 return res;
2271 void *fuse_req_userdata(fuse_req_t req)
2273 return req->se->userdata;
2276 const struct fuse_ctx *fuse_req_ctx(fuse_req_t req)
2278 return &req->ctx;
2281 void fuse_req_interrupt_func(fuse_req_t req, fuse_interrupt_func_t func,
2282 void *data)
2284 pthread_mutex_lock(&req->lock);
2285 pthread_mutex_lock(&req->se->lock);
2286 req->u.ni.func = func;
2287 req->u.ni.data = data;
2288 pthread_mutex_unlock(&req->se->lock);
2289 if (req->interrupted && func) {
2290 func(req, data);
2292 pthread_mutex_unlock(&req->lock);
2295 int fuse_req_interrupted(fuse_req_t req)
2297 int interrupted;
2299 pthread_mutex_lock(&req->se->lock);
2300 interrupted = req->interrupted;
2301 pthread_mutex_unlock(&req->se->lock);
2303 return interrupted;
2306 static struct {
2307 void (*func)(fuse_req_t, fuse_ino_t, struct fuse_mbuf_iter *);
2308 const char *name;
2309 } fuse_ll_ops[] = {
2310 [FUSE_LOOKUP] = { do_lookup, "LOOKUP" },
2311 [FUSE_FORGET] = { do_forget, "FORGET" },
2312 [FUSE_GETATTR] = { do_getattr, "GETATTR" },
2313 [FUSE_SETATTR] = { do_setattr, "SETATTR" },
2314 [FUSE_READLINK] = { do_readlink, "READLINK" },
2315 [FUSE_SYMLINK] = { do_symlink, "SYMLINK" },
2316 [FUSE_MKNOD] = { do_mknod, "MKNOD" },
2317 [FUSE_MKDIR] = { do_mkdir, "MKDIR" },
2318 [FUSE_UNLINK] = { do_unlink, "UNLINK" },
2319 [FUSE_RMDIR] = { do_rmdir, "RMDIR" },
2320 [FUSE_RENAME] = { do_rename, "RENAME" },
2321 [FUSE_LINK] = { do_link, "LINK" },
2322 [FUSE_OPEN] = { do_open, "OPEN" },
2323 [FUSE_READ] = { do_read, "READ" },
2324 [FUSE_WRITE] = { do_write, "WRITE" },
2325 [FUSE_STATFS] = { do_statfs, "STATFS" },
2326 [FUSE_RELEASE] = { do_release, "RELEASE" },
2327 [FUSE_FSYNC] = { do_fsync, "FSYNC" },
2328 [FUSE_SETXATTR] = { do_setxattr, "SETXATTR" },
2329 [FUSE_GETXATTR] = { do_getxattr, "GETXATTR" },
2330 [FUSE_LISTXATTR] = { do_listxattr, "LISTXATTR" },
2331 [FUSE_REMOVEXATTR] = { do_removexattr, "REMOVEXATTR" },
2332 [FUSE_FLUSH] = { do_flush, "FLUSH" },
2333 [FUSE_INIT] = { do_init, "INIT" },
2334 [FUSE_OPENDIR] = { do_opendir, "OPENDIR" },
2335 [FUSE_READDIR] = { do_readdir, "READDIR" },
2336 [FUSE_RELEASEDIR] = { do_releasedir, "RELEASEDIR" },
2337 [FUSE_FSYNCDIR] = { do_fsyncdir, "FSYNCDIR" },
2338 [FUSE_GETLK] = { do_getlk, "GETLK" },
2339 [FUSE_SETLK] = { do_setlk, "SETLK" },
2340 [FUSE_SETLKW] = { do_setlkw, "SETLKW" },
2341 [FUSE_ACCESS] = { do_access, "ACCESS" },
2342 [FUSE_CREATE] = { do_create, "CREATE" },
2343 [FUSE_INTERRUPT] = { do_interrupt, "INTERRUPT" },
2344 [FUSE_BMAP] = { do_bmap, "BMAP" },
2345 [FUSE_IOCTL] = { do_ioctl, "IOCTL" },
2346 [FUSE_POLL] = { do_poll, "POLL" },
2347 [FUSE_FALLOCATE] = { do_fallocate, "FALLOCATE" },
2348 [FUSE_DESTROY] = { do_destroy, "DESTROY" },
2349 [FUSE_NOTIFY_REPLY] = { NULL, "NOTIFY_REPLY" },
2350 [FUSE_BATCH_FORGET] = { do_batch_forget, "BATCH_FORGET" },
2351 [FUSE_READDIRPLUS] = { do_readdirplus, "READDIRPLUS" },
2352 [FUSE_RENAME2] = { do_rename2, "RENAME2" },
2353 [FUSE_COPY_FILE_RANGE] = { do_copy_file_range, "COPY_FILE_RANGE" },
2354 [FUSE_LSEEK] = { do_lseek, "LSEEK" },
2357 #define FUSE_MAXOP (sizeof(fuse_ll_ops) / sizeof(fuse_ll_ops[0]))
2359 static const char *opname(enum fuse_opcode opcode)
2361 if (opcode >= FUSE_MAXOP || !fuse_ll_ops[opcode].name) {
2362 return "???";
2363 } else {
2364 return fuse_ll_ops[opcode].name;
2368 void fuse_session_process_buf(struct fuse_session *se,
2369 const struct fuse_buf *buf)
2371 struct fuse_bufvec bufv = { .buf[0] = *buf, .count = 1 };
2372 fuse_session_process_buf_int(se, &bufv, NULL);
2376 * Restriction:
2377 * bufv is normally a single entry buffer, except for a write
2378 * where (if it's in memory) then the bufv may be multiple entries,
2379 * where the first entry contains all headers and subsequent entries
2380 * contain data
2381 * bufv shall not use any offsets etc to make the data anything
2382 * other than contiguous starting from 0.
2384 void fuse_session_process_buf_int(struct fuse_session *se,
2385 struct fuse_bufvec *bufv,
2386 struct fuse_chan *ch)
2388 const struct fuse_buf *buf = bufv->buf;
2389 struct fuse_mbuf_iter iter = FUSE_MBUF_ITER_INIT(buf);
2390 struct fuse_in_header *in;
2391 struct fuse_req *req;
2392 int err;
2394 /* The first buffer must be a memory buffer */
2395 assert(!(buf->flags & FUSE_BUF_IS_FD));
2397 in = fuse_mbuf_iter_advance(&iter, sizeof(*in));
2398 assert(in); /* caller guarantees the input buffer is large enough */
2400 fuse_log(
2401 FUSE_LOG_DEBUG,
2402 "unique: %llu, opcode: %s (%i), nodeid: %llu, insize: %zu, pid: %u\n",
2403 (unsigned long long)in->unique, opname((enum fuse_opcode)in->opcode),
2404 in->opcode, (unsigned long long)in->nodeid, buf->size, in->pid);
2406 req = fuse_ll_alloc_req(se);
2407 if (req == NULL) {
2408 struct fuse_out_header out = {
2409 .unique = in->unique,
2410 .error = -ENOMEM,
2412 struct iovec iov = {
2413 .iov_base = &out,
2414 .iov_len = sizeof(struct fuse_out_header),
2417 fuse_send_msg(se, ch, &iov, 1);
2418 return;
2421 req->unique = in->unique;
2422 req->ctx.uid = in->uid;
2423 req->ctx.gid = in->gid;
2424 req->ctx.pid = in->pid;
2425 req->ch = ch;
2427 err = EIO;
2428 if (!se->got_init) {
2429 enum fuse_opcode expected;
2431 expected = se->cuse_data ? CUSE_INIT : FUSE_INIT;
2432 if (in->opcode != expected) {
2433 goto reply_err;
2435 } else if (in->opcode == FUSE_INIT || in->opcode == CUSE_INIT) {
2436 if (fuse_lowlevel_is_virtio(se)) {
2438 * TODO: This is after a hard reboot typically, we need to do
2439 * a destroy, but we can't reply to this request yet so
2440 * we can't use do_destroy
2442 fuse_log(FUSE_LOG_DEBUG, "%s: reinit\n", __func__);
2443 se->got_destroy = 1;
2444 se->got_init = 0;
2445 if (se->op.destroy) {
2446 se->op.destroy(se->userdata);
2448 } else {
2449 goto reply_err;
2453 err = EACCES;
2454 /* Implement -o allow_root */
2455 if (se->deny_others && in->uid != se->owner && in->uid != 0 &&
2456 in->opcode != FUSE_INIT && in->opcode != FUSE_READ &&
2457 in->opcode != FUSE_WRITE && in->opcode != FUSE_FSYNC &&
2458 in->opcode != FUSE_RELEASE && in->opcode != FUSE_READDIR &&
2459 in->opcode != FUSE_FSYNCDIR && in->opcode != FUSE_RELEASEDIR &&
2460 in->opcode != FUSE_NOTIFY_REPLY && in->opcode != FUSE_READDIRPLUS) {
2461 goto reply_err;
2464 err = ENOSYS;
2465 if (in->opcode >= FUSE_MAXOP || !fuse_ll_ops[in->opcode].func) {
2466 goto reply_err;
2468 if (in->opcode != FUSE_INTERRUPT) {
2469 struct fuse_req *intr;
2470 pthread_mutex_lock(&se->lock);
2471 intr = check_interrupt(se, req);
2472 list_add_req(req, &se->list);
2473 pthread_mutex_unlock(&se->lock);
2474 if (intr) {
2475 fuse_reply_err(intr, EAGAIN);
2479 if (in->opcode == FUSE_WRITE && se->op.write_buf) {
2480 do_write_buf(req, in->nodeid, &iter, bufv);
2481 } else {
2482 fuse_ll_ops[in->opcode].func(req, in->nodeid, &iter);
2484 return;
2486 reply_err:
2487 fuse_reply_err(req, err);
2490 #define LL_OPTION(n, o, v) \
2492 n, offsetof(struct fuse_session, o), v \
2495 static const struct fuse_opt fuse_ll_opts[] = {
2496 LL_OPTION("debug", debug, 1),
2497 LL_OPTION("-d", debug, 1),
2498 LL_OPTION("--debug", debug, 1),
2499 LL_OPTION("allow_root", deny_others, 1),
2500 LL_OPTION("--socket-path=%s", vu_socket_path, 0),
2501 LL_OPTION("--fd=%d", vu_listen_fd, 0),
2502 FUSE_OPT_END
2505 void fuse_lowlevel_version(void)
2507 printf("using FUSE kernel interface version %i.%i\n", FUSE_KERNEL_VERSION,
2508 FUSE_KERNEL_MINOR_VERSION);
2511 void fuse_lowlevel_help(void)
2514 * These are not all options, but the ones that are
2515 * potentially of interest to an end-user
2517 printf(
2518 " -o allow_root allow access by root\n"
2519 " --socket-path=PATH path for the vhost-user socket\n"
2520 " --fd=FDNUM fd number of vhost-user socket\n");
2523 void fuse_session_destroy(struct fuse_session *se)
2525 if (se->got_init && !se->got_destroy) {
2526 if (se->op.destroy) {
2527 se->op.destroy(se->userdata);
2530 pthread_mutex_destroy(&se->lock);
2531 free(se->cuse_data);
2532 if (se->fd != -1) {
2533 close(se->fd);
2535 free(se);
2539 struct fuse_session *fuse_session_new(struct fuse_args *args,
2540 const struct fuse_lowlevel_ops *op,
2541 size_t op_size, void *userdata)
2543 struct fuse_session *se;
2545 if (sizeof(struct fuse_lowlevel_ops) < op_size) {
2546 fuse_log(
2547 FUSE_LOG_ERR,
2548 "fuse: warning: library too old, some operations may not work\n");
2549 op_size = sizeof(struct fuse_lowlevel_ops);
2552 if (args->argc == 0) {
2553 fuse_log(FUSE_LOG_ERR,
2554 "fuse: empty argv passed to fuse_session_new().\n");
2555 return NULL;
2558 se = (struct fuse_session *)calloc(1, sizeof(struct fuse_session));
2559 if (se == NULL) {
2560 fuse_log(FUSE_LOG_ERR, "fuse: failed to allocate fuse object\n");
2561 goto out1;
2563 se->fd = -1;
2564 se->vu_listen_fd = -1;
2565 se->conn.max_write = UINT_MAX;
2566 se->conn.max_readahead = UINT_MAX;
2568 /* Parse options */
2569 if (fuse_opt_parse(args, se, fuse_ll_opts, NULL) == -1) {
2570 goto out2;
2572 if (args->argc == 1 && args->argv[0][0] == '-') {
2573 fuse_log(FUSE_LOG_ERR,
2574 "fuse: warning: argv[0] looks like an option, but "
2575 "will be ignored\n");
2576 } else if (args->argc != 1) {
2577 int i;
2578 fuse_log(FUSE_LOG_ERR, "fuse: unknown option(s): `");
2579 for (i = 1; i < args->argc - 1; i++) {
2580 fuse_log(FUSE_LOG_ERR, "%s ", args->argv[i]);
2582 fuse_log(FUSE_LOG_ERR, "%s'\n", args->argv[i]);
2583 goto out4;
2586 if (!se->vu_socket_path && se->vu_listen_fd < 0) {
2587 fuse_log(FUSE_LOG_ERR, "fuse: missing --socket-path or --fd option\n");
2588 goto out4;
2590 if (se->vu_socket_path && se->vu_listen_fd >= 0) {
2591 fuse_log(FUSE_LOG_ERR,
2592 "fuse: --socket-path and --fd cannot be given together\n");
2593 goto out4;
2596 se->bufsize = FUSE_MAX_MAX_PAGES * getpagesize() + FUSE_BUFFER_HEADER_SIZE;
2598 list_init_req(&se->list);
2599 list_init_req(&se->interrupts);
2600 fuse_mutex_init(&se->lock);
2602 memcpy(&se->op, op, op_size);
2603 se->owner = getuid();
2604 se->userdata = userdata;
2606 return se;
2608 out4:
2609 fuse_opt_free_args(args);
2610 out2:
2611 free(se);
2612 out1:
2613 return NULL;
2616 int fuse_session_mount(struct fuse_session *se)
2618 return virtio_session_mount(se);
2621 int fuse_session_fd(struct fuse_session *se)
2623 return se->fd;
2626 void fuse_session_unmount(struct fuse_session *se)
2630 int fuse_lowlevel_is_virtio(struct fuse_session *se)
2632 return !!se->virtio_dev;
2635 #ifdef linux
2636 int fuse_req_getgroups(fuse_req_t req, int size, gid_t list[])
2638 char *buf;
2639 size_t bufsize = 1024;
2640 char path[128];
2641 int ret;
2642 int fd;
2643 unsigned long pid = req->ctx.pid;
2644 char *s;
2646 sprintf(path, "/proc/%lu/task/%lu/status", pid, pid);
2648 retry:
2649 buf = malloc(bufsize);
2650 if (buf == NULL) {
2651 return -ENOMEM;
2654 ret = -EIO;
2655 fd = open(path, O_RDONLY);
2656 if (fd == -1) {
2657 goto out_free;
2660 ret = read(fd, buf, bufsize);
2661 close(fd);
2662 if (ret < 0) {
2663 ret = -EIO;
2664 goto out_free;
2667 if ((size_t)ret == bufsize) {
2668 free(buf);
2669 bufsize *= 4;
2670 goto retry;
2673 ret = -EIO;
2674 s = strstr(buf, "\nGroups:");
2675 if (s == NULL) {
2676 goto out_free;
2679 s += 8;
2680 ret = 0;
2681 while (1) {
2682 char *end;
2683 unsigned long val = strtoul(s, &end, 0);
2684 if (end == s) {
2685 break;
2688 s = end;
2689 if (ret < size) {
2690 list[ret] = val;
2692 ret++;
2695 out_free:
2696 free(buf);
2697 return ret;
2699 #else /* linux */
2701 * This is currently not implemented on other than Linux...
2703 int fuse_req_getgroups(fuse_req_t req, int size, gid_t list[])
2705 (void)req;
2706 (void)size;
2707 (void)list;
2708 return -ENOSYS;
2710 #endif
2712 void fuse_session_exit(struct fuse_session *se)
2714 se->exited = 1;
2717 void fuse_session_reset(struct fuse_session *se)
2719 se->exited = 0;
2720 se->error = 0;
2723 int fuse_session_exited(struct fuse_session *se)
2725 return se->exited;