hw/arm/armsse: Move PPUs into data-driven framework
[qemu/ar7.git] / tools / virtiofsd / fuse_lowlevel.c
blob1aa26c63330a2da95563cfd3147b7054ee314b61
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 <sys/file.h>
21 #define THREAD_POOL_SIZE 0
23 #define OFFSET_MAX 0x7fffffffffffffffLL
25 struct fuse_pollhandle {
26 uint64_t kh;
27 struct fuse_session *se;
30 static size_t pagesize;
32 static __attribute__((constructor)) void fuse_ll_init_pagesize(void)
34 pagesize = getpagesize();
37 static void convert_stat(const struct stat *stbuf, struct fuse_attr *attr)
39 *attr = (struct fuse_attr){
40 .ino = stbuf->st_ino,
41 .mode = stbuf->st_mode,
42 .nlink = stbuf->st_nlink,
43 .uid = stbuf->st_uid,
44 .gid = stbuf->st_gid,
45 .rdev = stbuf->st_rdev,
46 .size = stbuf->st_size,
47 .blksize = stbuf->st_blksize,
48 .blocks = stbuf->st_blocks,
49 .atime = stbuf->st_atime,
50 .mtime = stbuf->st_mtime,
51 .ctime = stbuf->st_ctime,
52 .atimensec = ST_ATIM_NSEC(stbuf),
53 .mtimensec = ST_MTIM_NSEC(stbuf),
54 .ctimensec = ST_CTIM_NSEC(stbuf),
58 static void convert_attr(const struct fuse_setattr_in *attr, struct stat *stbuf)
60 stbuf->st_mode = attr->mode;
61 stbuf->st_uid = attr->uid;
62 stbuf->st_gid = attr->gid;
63 stbuf->st_size = attr->size;
64 stbuf->st_atime = attr->atime;
65 stbuf->st_mtime = attr->mtime;
66 stbuf->st_ctime = attr->ctime;
67 ST_ATIM_NSEC_SET(stbuf, attr->atimensec);
68 ST_MTIM_NSEC_SET(stbuf, attr->mtimensec);
69 ST_CTIM_NSEC_SET(stbuf, attr->ctimensec);
72 static size_t iov_length(const struct iovec *iov, size_t count)
74 size_t seg;
75 size_t ret = 0;
77 for (seg = 0; seg < count; seg++) {
78 ret += iov[seg].iov_len;
80 return ret;
83 static void list_init_req(struct fuse_req *req)
85 req->next = req;
86 req->prev = req;
89 static void list_del_req(struct fuse_req *req)
91 struct fuse_req *prev = req->prev;
92 struct fuse_req *next = req->next;
93 prev->next = next;
94 next->prev = prev;
97 static void list_add_req(struct fuse_req *req, struct fuse_req *next)
99 struct fuse_req *prev = next->prev;
100 req->next = next;
101 req->prev = prev;
102 prev->next = req;
103 next->prev = req;
106 static void destroy_req(fuse_req_t req)
108 pthread_mutex_destroy(&req->lock);
109 free(req);
112 void fuse_free_req(fuse_req_t req)
114 int ctr;
115 struct fuse_session *se = req->se;
117 pthread_mutex_lock(&se->lock);
118 req->u.ni.func = NULL;
119 req->u.ni.data = NULL;
120 list_del_req(req);
121 ctr = --req->ctr;
122 req->ch = NULL;
123 pthread_mutex_unlock(&se->lock);
124 if (!ctr) {
125 destroy_req(req);
129 static struct fuse_req *fuse_ll_alloc_req(struct fuse_session *se)
131 struct fuse_req *req;
133 req = (struct fuse_req *)calloc(1, sizeof(struct fuse_req));
134 if (req == NULL) {
135 fuse_log(FUSE_LOG_ERR, "fuse: failed to allocate request\n");
136 } else {
137 req->se = se;
138 req->ctr = 1;
139 list_init_req(req);
140 fuse_mutex_init(&req->lock);
143 return req;
146 /* Send data. If *ch* is NULL, send via session master fd */
147 static int fuse_send_msg(struct fuse_session *se, struct fuse_chan *ch,
148 struct iovec *iov, int count)
150 struct fuse_out_header *out = iov[0].iov_base;
152 out->len = iov_length(iov, count);
153 if (out->unique == 0) {
154 fuse_log(FUSE_LOG_DEBUG, "NOTIFY: code=%d length=%u\n", out->error,
155 out->len);
156 } else if (out->error) {
157 fuse_log(FUSE_LOG_DEBUG,
158 " unique: %llu, error: %i (%s), outsize: %i\n",
159 (unsigned long long)out->unique, out->error,
160 strerror(-out->error), out->len);
161 } else {
162 fuse_log(FUSE_LOG_DEBUG, " unique: %llu, success, outsize: %i\n",
163 (unsigned long long)out->unique, out->len);
166 if (fuse_lowlevel_is_virtio(se)) {
167 return virtio_send_msg(se, ch, iov, count);
170 abort(); /* virtio should have taken it before here */
171 return 0;
175 int fuse_send_reply_iov_nofree(fuse_req_t req, int error, struct iovec *iov,
176 int count)
178 struct fuse_out_header out = {
179 .unique = req->unique,
180 .error = error,
183 if (error <= -1000 || error > 0) {
184 fuse_log(FUSE_LOG_ERR, "fuse: bad error value: %i\n", error);
185 out.error = -ERANGE;
188 iov[0].iov_base = &out;
189 iov[0].iov_len = sizeof(struct fuse_out_header);
191 return fuse_send_msg(req->se, req->ch, iov, count);
194 static int send_reply_iov(fuse_req_t req, int error, struct iovec *iov,
195 int count)
197 int res;
199 res = fuse_send_reply_iov_nofree(req, error, iov, count);
200 fuse_free_req(req);
201 return res;
204 static int send_reply(fuse_req_t req, int error, const void *arg,
205 size_t argsize)
207 struct iovec iov[2];
208 int count = 1;
209 if (argsize) {
210 iov[1].iov_base = (void *)arg;
211 iov[1].iov_len = argsize;
212 count++;
214 return send_reply_iov(req, error, iov, count);
217 int fuse_reply_iov(fuse_req_t req, const struct iovec *iov, int count)
219 int res;
220 struct iovec *padded_iov;
222 padded_iov = malloc((count + 1) * sizeof(struct iovec));
223 if (padded_iov == NULL) {
224 return fuse_reply_err(req, ENOMEM);
227 memcpy(padded_iov + 1, iov, count * sizeof(struct iovec));
228 count++;
230 res = send_reply_iov(req, 0, padded_iov, count);
231 free(padded_iov);
233 return res;
238 * 'buf` is allowed to be empty so that the proper size may be
239 * allocated by the caller
241 size_t fuse_add_direntry(fuse_req_t req, char *buf, size_t bufsize,
242 const char *name, const struct stat *stbuf, off_t off)
244 (void)req;
245 size_t namelen;
246 size_t entlen;
247 size_t entlen_padded;
248 struct fuse_dirent *dirent;
250 namelen = strlen(name);
251 entlen = FUSE_NAME_OFFSET + namelen;
252 entlen_padded = FUSE_DIRENT_ALIGN(entlen);
254 if ((buf == NULL) || (entlen_padded > bufsize)) {
255 return entlen_padded;
258 dirent = (struct fuse_dirent *)buf;
259 dirent->ino = stbuf->st_ino;
260 dirent->off = off;
261 dirent->namelen = namelen;
262 dirent->type = (stbuf->st_mode & S_IFMT) >> 12;
263 memcpy(dirent->name, name, namelen);
264 memset(dirent->name + namelen, 0, entlen_padded - entlen);
266 return entlen_padded;
269 static void convert_statfs(const struct statvfs *stbuf,
270 struct fuse_kstatfs *kstatfs)
272 *kstatfs = (struct fuse_kstatfs){
273 .bsize = stbuf->f_bsize,
274 .frsize = stbuf->f_frsize,
275 .blocks = stbuf->f_blocks,
276 .bfree = stbuf->f_bfree,
277 .bavail = stbuf->f_bavail,
278 .files = stbuf->f_files,
279 .ffree = stbuf->f_ffree,
280 .namelen = stbuf->f_namemax,
284 static int send_reply_ok(fuse_req_t req, const void *arg, size_t argsize)
286 return send_reply(req, 0, arg, argsize);
289 int fuse_reply_err(fuse_req_t req, int err)
291 return send_reply(req, -err, NULL, 0);
294 void fuse_reply_none(fuse_req_t req)
296 fuse_free_req(req);
299 static unsigned long calc_timeout_sec(double t)
301 if (t > (double)ULONG_MAX) {
302 return ULONG_MAX;
303 } else if (t < 0.0) {
304 return 0;
305 } else {
306 return (unsigned long)t;
310 static unsigned int calc_timeout_nsec(double t)
312 double f = t - (double)calc_timeout_sec(t);
313 if (f < 0.0) {
314 return 0;
315 } else if (f >= 0.999999999) {
316 return 999999999;
317 } else {
318 return (unsigned int)(f * 1.0e9);
322 static void fill_entry(struct fuse_entry_out *arg,
323 const struct fuse_entry_param *e)
325 *arg = (struct fuse_entry_out){
326 .nodeid = e->ino,
327 .generation = e->generation,
328 .entry_valid = calc_timeout_sec(e->entry_timeout),
329 .entry_valid_nsec = calc_timeout_nsec(e->entry_timeout),
330 .attr_valid = calc_timeout_sec(e->attr_timeout),
331 .attr_valid_nsec = calc_timeout_nsec(e->attr_timeout),
333 convert_stat(&e->attr, &arg->attr);
335 arg->attr.flags = e->attr_flags;
339 * `buf` is allowed to be empty so that the proper size may be
340 * allocated by the caller
342 size_t fuse_add_direntry_plus(fuse_req_t req, char *buf, size_t bufsize,
343 const char *name,
344 const struct fuse_entry_param *e, off_t off)
346 (void)req;
347 size_t namelen;
348 size_t entlen;
349 size_t entlen_padded;
351 namelen = strlen(name);
352 entlen = FUSE_NAME_OFFSET_DIRENTPLUS + namelen;
353 entlen_padded = FUSE_DIRENT_ALIGN(entlen);
354 if ((buf == NULL) || (entlen_padded > bufsize)) {
355 return entlen_padded;
358 struct fuse_direntplus *dp = (struct fuse_direntplus *)buf;
359 memset(&dp->entry_out, 0, sizeof(dp->entry_out));
360 fill_entry(&dp->entry_out, e);
362 struct fuse_dirent *dirent = &dp->dirent;
363 *dirent = (struct fuse_dirent){
364 .ino = e->attr.st_ino,
365 .off = off,
366 .namelen = namelen,
367 .type = (e->attr.st_mode & S_IFMT) >> 12,
369 memcpy(dirent->name, name, namelen);
370 memset(dirent->name + namelen, 0, entlen_padded - entlen);
372 return entlen_padded;
375 static void fill_open(struct fuse_open_out *arg, const struct fuse_file_info *f)
377 arg->fh = f->fh;
378 if (f->direct_io) {
379 arg->open_flags |= FOPEN_DIRECT_IO;
381 if (f->keep_cache) {
382 arg->open_flags |= FOPEN_KEEP_CACHE;
384 if (f->cache_readdir) {
385 arg->open_flags |= FOPEN_CACHE_DIR;
387 if (f->nonseekable) {
388 arg->open_flags |= FOPEN_NONSEEKABLE;
392 int fuse_reply_entry(fuse_req_t req, const struct fuse_entry_param *e)
394 struct fuse_entry_out arg;
395 size_t size = sizeof(arg);
397 memset(&arg, 0, sizeof(arg));
398 fill_entry(&arg, e);
399 return send_reply_ok(req, &arg, size);
402 int fuse_reply_create(fuse_req_t req, const struct fuse_entry_param *e,
403 const struct fuse_file_info *f)
405 char buf[sizeof(struct fuse_entry_out) + sizeof(struct fuse_open_out)];
406 size_t entrysize = sizeof(struct fuse_entry_out);
407 struct fuse_entry_out *earg = (struct fuse_entry_out *)buf;
408 struct fuse_open_out *oarg = (struct fuse_open_out *)(buf + entrysize);
410 memset(buf, 0, sizeof(buf));
411 fill_entry(earg, e);
412 fill_open(oarg, f);
413 return send_reply_ok(req, buf, entrysize + sizeof(struct fuse_open_out));
416 int fuse_reply_attr(fuse_req_t req, const struct stat *attr,
417 double attr_timeout)
419 struct fuse_attr_out arg;
420 size_t size = sizeof(arg);
422 memset(&arg, 0, sizeof(arg));
423 arg.attr_valid = calc_timeout_sec(attr_timeout);
424 arg.attr_valid_nsec = calc_timeout_nsec(attr_timeout);
425 convert_stat(attr, &arg.attr);
427 return send_reply_ok(req, &arg, size);
430 int fuse_reply_readlink(fuse_req_t req, const char *linkname)
432 return send_reply_ok(req, linkname, strlen(linkname));
435 int fuse_reply_open(fuse_req_t req, const struct fuse_file_info *f)
437 struct fuse_open_out arg;
439 memset(&arg, 0, sizeof(arg));
440 fill_open(&arg, f);
441 return send_reply_ok(req, &arg, sizeof(arg));
444 int fuse_reply_write(fuse_req_t req, size_t count)
446 struct fuse_write_out arg;
448 memset(&arg, 0, sizeof(arg));
449 arg.size = count;
451 return send_reply_ok(req, &arg, sizeof(arg));
454 int fuse_reply_buf(fuse_req_t req, const char *buf, size_t size)
456 return send_reply_ok(req, buf, size);
459 static int fuse_send_data_iov_fallback(struct fuse_session *se,
460 struct fuse_chan *ch, struct iovec *iov,
461 int iov_count, struct fuse_bufvec *buf,
462 size_t len)
464 /* Optimize common case */
465 if (buf->count == 1 && buf->idx == 0 && buf->off == 0 &&
466 !(buf->buf[0].flags & FUSE_BUF_IS_FD)) {
468 * FIXME: also avoid memory copy if there are multiple buffers
469 * but none of them contain an fd
472 iov[iov_count].iov_base = buf->buf[0].mem;
473 iov[iov_count].iov_len = len;
474 iov_count++;
475 return fuse_send_msg(se, ch, iov, iov_count);
478 if (fuse_lowlevel_is_virtio(se) && buf->count == 1 &&
479 buf->buf[0].flags == (FUSE_BUF_IS_FD | FUSE_BUF_FD_SEEK)) {
480 return virtio_send_data_iov(se, ch, iov, iov_count, buf, len);
483 abort(); /* Will have taken vhost path */
484 return 0;
487 static int fuse_send_data_iov(struct fuse_session *se, struct fuse_chan *ch,
488 struct iovec *iov, int iov_count,
489 struct fuse_bufvec *buf)
491 size_t len = fuse_buf_size(buf);
493 return fuse_send_data_iov_fallback(se, ch, iov, iov_count, buf, len);
496 int fuse_reply_data(fuse_req_t req, struct fuse_bufvec *bufv)
498 struct iovec iov[2];
499 struct fuse_out_header out = {
500 .unique = req->unique,
502 int res;
504 iov[0].iov_base = &out;
505 iov[0].iov_len = sizeof(struct fuse_out_header);
507 res = fuse_send_data_iov(req->se, req->ch, iov, 1, bufv);
508 if (res <= 0) {
509 fuse_free_req(req);
510 return res;
511 } else {
512 return fuse_reply_err(req, res);
516 int fuse_reply_statfs(fuse_req_t req, const struct statvfs *stbuf)
518 struct fuse_statfs_out arg;
519 size_t size = sizeof(arg);
521 memset(&arg, 0, sizeof(arg));
522 convert_statfs(stbuf, &arg.st);
524 return send_reply_ok(req, &arg, size);
527 int fuse_reply_xattr(fuse_req_t req, size_t count)
529 struct fuse_getxattr_out arg;
531 memset(&arg, 0, sizeof(arg));
532 arg.size = count;
534 return send_reply_ok(req, &arg, sizeof(arg));
537 int fuse_reply_lock(fuse_req_t req, const struct flock *lock)
539 struct fuse_lk_out arg;
541 memset(&arg, 0, sizeof(arg));
542 arg.lk.type = lock->l_type;
543 if (lock->l_type != F_UNLCK) {
544 arg.lk.start = lock->l_start;
545 if (lock->l_len == 0) {
546 arg.lk.end = OFFSET_MAX;
547 } else {
548 arg.lk.end = lock->l_start + lock->l_len - 1;
551 arg.lk.pid = lock->l_pid;
552 return send_reply_ok(req, &arg, sizeof(arg));
555 int fuse_reply_bmap(fuse_req_t req, uint64_t idx)
557 struct fuse_bmap_out arg;
559 memset(&arg, 0, sizeof(arg));
560 arg.block = idx;
562 return send_reply_ok(req, &arg, sizeof(arg));
565 static struct fuse_ioctl_iovec *fuse_ioctl_iovec_copy(const struct iovec *iov,
566 size_t count)
568 struct fuse_ioctl_iovec *fiov;
569 size_t i;
571 fiov = malloc(sizeof(fiov[0]) * count);
572 if (!fiov) {
573 return NULL;
576 for (i = 0; i < count; i++) {
577 fiov[i].base = (uintptr_t)iov[i].iov_base;
578 fiov[i].len = iov[i].iov_len;
581 return fiov;
584 int fuse_reply_ioctl_retry(fuse_req_t req, const struct iovec *in_iov,
585 size_t in_count, const struct iovec *out_iov,
586 size_t out_count)
588 struct fuse_ioctl_out arg;
589 struct fuse_ioctl_iovec *in_fiov = NULL;
590 struct fuse_ioctl_iovec *out_fiov = NULL;
591 struct iovec iov[4];
592 size_t count = 1;
593 int res;
595 memset(&arg, 0, sizeof(arg));
596 arg.flags |= FUSE_IOCTL_RETRY;
597 arg.in_iovs = in_count;
598 arg.out_iovs = out_count;
599 iov[count].iov_base = &arg;
600 iov[count].iov_len = sizeof(arg);
601 count++;
603 /* Can't handle non-compat 64bit ioctls on 32bit */
604 if (sizeof(void *) == 4 && req->ioctl_64bit) {
605 res = fuse_reply_err(req, EINVAL);
606 goto out;
609 if (in_count) {
610 in_fiov = fuse_ioctl_iovec_copy(in_iov, in_count);
611 if (!in_fiov) {
612 goto enomem;
615 iov[count].iov_base = (void *)in_fiov;
616 iov[count].iov_len = sizeof(in_fiov[0]) * in_count;
617 count++;
619 if (out_count) {
620 out_fiov = fuse_ioctl_iovec_copy(out_iov, out_count);
621 if (!out_fiov) {
622 goto enomem;
625 iov[count].iov_base = (void *)out_fiov;
626 iov[count].iov_len = sizeof(out_fiov[0]) * out_count;
627 count++;
630 res = send_reply_iov(req, 0, iov, count);
631 out:
632 free(in_fiov);
633 free(out_fiov);
635 return res;
637 enomem:
638 res = fuse_reply_err(req, ENOMEM);
639 goto out;
642 int fuse_reply_ioctl(fuse_req_t req, int result, const void *buf, size_t size)
644 struct fuse_ioctl_out arg;
645 struct iovec iov[3];
646 size_t count = 1;
648 memset(&arg, 0, sizeof(arg));
649 arg.result = result;
650 iov[count].iov_base = &arg;
651 iov[count].iov_len = sizeof(arg);
652 count++;
654 if (size) {
655 iov[count].iov_base = (char *)buf;
656 iov[count].iov_len = size;
657 count++;
660 return send_reply_iov(req, 0, iov, count);
663 int fuse_reply_ioctl_iov(fuse_req_t req, int result, const struct iovec *iov,
664 int count)
666 struct iovec *padded_iov;
667 struct fuse_ioctl_out arg;
668 int res;
670 padded_iov = malloc((count + 2) * sizeof(struct iovec));
671 if (padded_iov == NULL) {
672 return fuse_reply_err(req, ENOMEM);
675 memset(&arg, 0, sizeof(arg));
676 arg.result = result;
677 padded_iov[1].iov_base = &arg;
678 padded_iov[1].iov_len = sizeof(arg);
680 memcpy(&padded_iov[2], iov, count * sizeof(struct iovec));
682 res = send_reply_iov(req, 0, padded_iov, count + 2);
683 free(padded_iov);
685 return res;
688 int fuse_reply_poll(fuse_req_t req, unsigned revents)
690 struct fuse_poll_out arg;
692 memset(&arg, 0, sizeof(arg));
693 arg.revents = revents;
695 return send_reply_ok(req, &arg, sizeof(arg));
698 int fuse_reply_lseek(fuse_req_t req, off_t off)
700 struct fuse_lseek_out arg;
702 memset(&arg, 0, sizeof(arg));
703 arg.offset = off;
705 return send_reply_ok(req, &arg, sizeof(arg));
708 static void do_lookup(fuse_req_t req, fuse_ino_t nodeid,
709 struct fuse_mbuf_iter *iter)
711 const char *name = fuse_mbuf_iter_advance_str(iter);
712 if (!name) {
713 fuse_reply_err(req, EINVAL);
714 return;
717 if (req->se->op.lookup) {
718 req->se->op.lookup(req, nodeid, name);
719 } else {
720 fuse_reply_err(req, ENOSYS);
724 static void do_forget(fuse_req_t req, fuse_ino_t nodeid,
725 struct fuse_mbuf_iter *iter)
727 struct fuse_forget_in *arg;
729 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
730 if (!arg) {
731 fuse_reply_err(req, EINVAL);
732 return;
735 if (req->se->op.forget) {
736 req->se->op.forget(req, nodeid, arg->nlookup);
737 } else {
738 fuse_reply_none(req);
742 static void do_batch_forget(fuse_req_t req, fuse_ino_t nodeid,
743 struct fuse_mbuf_iter *iter)
745 struct fuse_batch_forget_in *arg;
746 struct fuse_forget_data *forgets;
747 size_t scount;
749 (void)nodeid;
751 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
752 if (!arg) {
753 fuse_reply_none(req);
754 return;
758 * Prevent integer overflow. The compiler emits the following warning
759 * unless we use the scount local variable:
761 * error: comparison is always false due to limited range of data type
762 * [-Werror=type-limits]
764 * This may be true on 64-bit hosts but we need this check for 32-bit
765 * hosts.
767 scount = arg->count;
768 if (scount > SIZE_MAX / sizeof(forgets[0])) {
769 fuse_reply_none(req);
770 return;
773 forgets = fuse_mbuf_iter_advance(iter, arg->count * sizeof(forgets[0]));
774 if (!forgets) {
775 fuse_reply_none(req);
776 return;
779 if (req->se->op.forget_multi) {
780 req->se->op.forget_multi(req, arg->count, forgets);
781 } else if (req->se->op.forget) {
782 unsigned int i;
784 for (i = 0; i < arg->count; i++) {
785 struct fuse_req *dummy_req;
787 dummy_req = fuse_ll_alloc_req(req->se);
788 if (dummy_req == NULL) {
789 break;
792 dummy_req->unique = req->unique;
793 dummy_req->ctx = req->ctx;
794 dummy_req->ch = NULL;
796 req->se->op.forget(dummy_req, forgets[i].ino, forgets[i].nlookup);
798 fuse_reply_none(req);
799 } else {
800 fuse_reply_none(req);
804 static void do_getattr(fuse_req_t req, fuse_ino_t nodeid,
805 struct fuse_mbuf_iter *iter)
807 struct fuse_file_info *fip = NULL;
808 struct fuse_file_info fi;
810 struct fuse_getattr_in *arg;
812 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
813 if (!arg) {
814 fuse_reply_err(req, EINVAL);
815 return;
818 if (arg->getattr_flags & FUSE_GETATTR_FH) {
819 memset(&fi, 0, sizeof(fi));
820 fi.fh = arg->fh;
821 fip = &fi;
824 if (req->se->op.getattr) {
825 req->se->op.getattr(req, nodeid, fip);
826 } else {
827 fuse_reply_err(req, ENOSYS);
831 static void do_setattr(fuse_req_t req, fuse_ino_t nodeid,
832 struct fuse_mbuf_iter *iter)
834 if (req->se->op.setattr) {
835 struct fuse_setattr_in *arg;
836 struct fuse_file_info *fi = NULL;
837 struct fuse_file_info fi_store;
838 struct stat stbuf;
840 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
841 if (!arg) {
842 fuse_reply_err(req, EINVAL);
843 return;
846 memset(&stbuf, 0, sizeof(stbuf));
847 convert_attr(arg, &stbuf);
848 if (arg->valid & FATTR_FH) {
849 arg->valid &= ~FATTR_FH;
850 memset(&fi_store, 0, sizeof(fi_store));
851 fi = &fi_store;
852 fi->fh = arg->fh;
854 arg->valid &= FUSE_SET_ATTR_MODE | FUSE_SET_ATTR_UID |
855 FUSE_SET_ATTR_GID | FUSE_SET_ATTR_SIZE |
856 FUSE_SET_ATTR_ATIME | FUSE_SET_ATTR_MTIME |
857 FUSE_SET_ATTR_ATIME_NOW | FUSE_SET_ATTR_MTIME_NOW |
858 FUSE_SET_ATTR_CTIME | FUSE_SET_ATTR_KILL_SUIDGID;
860 req->se->op.setattr(req, nodeid, &stbuf, arg->valid, fi);
861 } else {
862 fuse_reply_err(req, ENOSYS);
866 static void do_access(fuse_req_t req, fuse_ino_t nodeid,
867 struct fuse_mbuf_iter *iter)
869 struct fuse_access_in *arg;
871 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
872 if (!arg) {
873 fuse_reply_err(req, EINVAL);
874 return;
877 if (req->se->op.access) {
878 req->se->op.access(req, nodeid, arg->mask);
879 } else {
880 fuse_reply_err(req, ENOSYS);
884 static void do_readlink(fuse_req_t req, fuse_ino_t nodeid,
885 struct fuse_mbuf_iter *iter)
887 (void)iter;
889 if (req->se->op.readlink) {
890 req->se->op.readlink(req, nodeid);
891 } else {
892 fuse_reply_err(req, ENOSYS);
896 static void do_mknod(fuse_req_t req, fuse_ino_t nodeid,
897 struct fuse_mbuf_iter *iter)
899 struct fuse_mknod_in *arg;
900 const char *name;
902 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
903 name = fuse_mbuf_iter_advance_str(iter);
904 if (!arg || !name) {
905 fuse_reply_err(req, EINVAL);
906 return;
909 req->ctx.umask = arg->umask;
911 if (req->se->op.mknod) {
912 req->se->op.mknod(req, nodeid, name, arg->mode, arg->rdev);
913 } else {
914 fuse_reply_err(req, ENOSYS);
918 static void do_mkdir(fuse_req_t req, fuse_ino_t nodeid,
919 struct fuse_mbuf_iter *iter)
921 struct fuse_mkdir_in *arg;
922 const char *name;
924 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
925 name = fuse_mbuf_iter_advance_str(iter);
926 if (!arg || !name) {
927 fuse_reply_err(req, EINVAL);
928 return;
931 req->ctx.umask = arg->umask;
933 if (req->se->op.mkdir) {
934 req->se->op.mkdir(req, nodeid, name, arg->mode);
935 } else {
936 fuse_reply_err(req, ENOSYS);
940 static void do_unlink(fuse_req_t req, fuse_ino_t nodeid,
941 struct fuse_mbuf_iter *iter)
943 const char *name = fuse_mbuf_iter_advance_str(iter);
945 if (!name) {
946 fuse_reply_err(req, EINVAL);
947 return;
950 if (req->se->op.unlink) {
951 req->se->op.unlink(req, nodeid, name);
952 } else {
953 fuse_reply_err(req, ENOSYS);
957 static void do_rmdir(fuse_req_t req, fuse_ino_t nodeid,
958 struct fuse_mbuf_iter *iter)
960 const char *name = fuse_mbuf_iter_advance_str(iter);
962 if (!name) {
963 fuse_reply_err(req, EINVAL);
964 return;
967 if (req->se->op.rmdir) {
968 req->se->op.rmdir(req, nodeid, name);
969 } else {
970 fuse_reply_err(req, ENOSYS);
974 static void do_symlink(fuse_req_t req, fuse_ino_t nodeid,
975 struct fuse_mbuf_iter *iter)
977 const char *name = fuse_mbuf_iter_advance_str(iter);
978 const char *linkname = fuse_mbuf_iter_advance_str(iter);
980 if (!name || !linkname) {
981 fuse_reply_err(req, EINVAL);
982 return;
985 if (req->se->op.symlink) {
986 req->se->op.symlink(req, linkname, nodeid, name);
987 } else {
988 fuse_reply_err(req, ENOSYS);
992 static void do_rename(fuse_req_t req, fuse_ino_t nodeid,
993 struct fuse_mbuf_iter *iter)
995 struct fuse_rename_in *arg;
996 const char *oldname;
997 const char *newname;
999 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1000 oldname = fuse_mbuf_iter_advance_str(iter);
1001 newname = fuse_mbuf_iter_advance_str(iter);
1002 if (!arg || !oldname || !newname) {
1003 fuse_reply_err(req, EINVAL);
1004 return;
1007 if (req->se->op.rename) {
1008 req->se->op.rename(req, nodeid, oldname, arg->newdir, newname, 0);
1009 } else {
1010 fuse_reply_err(req, ENOSYS);
1014 static void do_rename2(fuse_req_t req, fuse_ino_t nodeid,
1015 struct fuse_mbuf_iter *iter)
1017 struct fuse_rename2_in *arg;
1018 const char *oldname;
1019 const char *newname;
1021 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1022 oldname = fuse_mbuf_iter_advance_str(iter);
1023 newname = fuse_mbuf_iter_advance_str(iter);
1024 if (!arg || !oldname || !newname) {
1025 fuse_reply_err(req, EINVAL);
1026 return;
1029 if (req->se->op.rename) {
1030 req->se->op.rename(req, nodeid, oldname, arg->newdir, newname,
1031 arg->flags);
1032 } else {
1033 fuse_reply_err(req, ENOSYS);
1037 static void do_link(fuse_req_t req, fuse_ino_t nodeid,
1038 struct fuse_mbuf_iter *iter)
1040 struct fuse_link_in *arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1041 const char *name = fuse_mbuf_iter_advance_str(iter);
1043 if (!arg || !name) {
1044 fuse_reply_err(req, EINVAL);
1045 return;
1048 if (req->se->op.link) {
1049 req->se->op.link(req, arg->oldnodeid, nodeid, name);
1050 } else {
1051 fuse_reply_err(req, ENOSYS);
1055 static void do_create(fuse_req_t req, fuse_ino_t nodeid,
1056 struct fuse_mbuf_iter *iter)
1058 if (req->se->op.create) {
1059 struct fuse_create_in *arg;
1060 struct fuse_file_info fi;
1061 const char *name;
1063 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1064 name = fuse_mbuf_iter_advance_str(iter);
1065 if (!arg || !name) {
1066 fuse_reply_err(req, EINVAL);
1067 return;
1070 memset(&fi, 0, sizeof(fi));
1071 fi.flags = arg->flags;
1072 fi.kill_priv = arg->open_flags & FUSE_OPEN_KILL_SUIDGID;
1074 req->ctx.umask = arg->umask;
1076 req->se->op.create(req, nodeid, name, arg->mode, &fi);
1077 } else {
1078 fuse_reply_err(req, ENOSYS);
1082 static void do_open(fuse_req_t req, fuse_ino_t nodeid,
1083 struct fuse_mbuf_iter *iter)
1085 struct fuse_open_in *arg;
1086 struct fuse_file_info fi;
1088 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1089 if (!arg) {
1090 fuse_reply_err(req, EINVAL);
1091 return;
1094 memset(&fi, 0, sizeof(fi));
1095 fi.flags = arg->flags;
1096 fi.kill_priv = arg->open_flags & FUSE_OPEN_KILL_SUIDGID;
1098 if (req->se->op.open) {
1099 req->se->op.open(req, nodeid, &fi);
1100 } else {
1101 fuse_reply_open(req, &fi);
1105 static void do_read(fuse_req_t req, fuse_ino_t nodeid,
1106 struct fuse_mbuf_iter *iter)
1108 if (req->se->op.read) {
1109 struct fuse_read_in *arg;
1110 struct fuse_file_info fi;
1112 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1113 if (!arg) {
1114 fuse_reply_err(req, EINVAL);
1115 return;
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 if (arg->flags & FUSE_SUBMOUNTS) {
1986 se->conn.capable |= FUSE_CAP_SUBMOUNTS;
1988 if (arg->flags & FUSE_HANDLE_KILLPRIV_V2) {
1989 se->conn.capable |= FUSE_CAP_HANDLE_KILLPRIV_V2;
1991 #ifdef HAVE_SPLICE
1992 #ifdef HAVE_VMSPLICE
1993 se->conn.capable |= FUSE_CAP_SPLICE_WRITE | FUSE_CAP_SPLICE_MOVE;
1994 #endif
1995 se->conn.capable |= FUSE_CAP_SPLICE_READ;
1996 #endif
1997 se->conn.capable |= FUSE_CAP_IOCTL_DIR;
2000 * Default settings for modern filesystems.
2002 * Most of these capabilities were disabled by default in
2003 * libfuse2 for backwards compatibility reasons. In libfuse3,
2004 * we can finally enable them by default (as long as they're
2005 * supported by the kernel).
2007 #define LL_SET_DEFAULT(cond, cap) \
2008 if ((cond) && (se->conn.capable & (cap))) \
2009 se->conn.want |= (cap)
2010 LL_SET_DEFAULT(1, FUSE_CAP_ASYNC_READ);
2011 LL_SET_DEFAULT(1, FUSE_CAP_PARALLEL_DIROPS);
2012 LL_SET_DEFAULT(1, FUSE_CAP_AUTO_INVAL_DATA);
2013 LL_SET_DEFAULT(1, FUSE_CAP_HANDLE_KILLPRIV);
2014 LL_SET_DEFAULT(1, FUSE_CAP_ASYNC_DIO);
2015 LL_SET_DEFAULT(1, FUSE_CAP_IOCTL_DIR);
2016 LL_SET_DEFAULT(1, FUSE_CAP_ATOMIC_O_TRUNC);
2017 LL_SET_DEFAULT(se->op.write_buf, FUSE_CAP_SPLICE_READ);
2018 LL_SET_DEFAULT(se->op.getlk && se->op.setlk, FUSE_CAP_POSIX_LOCKS);
2019 LL_SET_DEFAULT(se->op.flock, FUSE_CAP_FLOCK_LOCKS);
2020 LL_SET_DEFAULT(se->op.readdirplus, FUSE_CAP_READDIRPLUS);
2021 LL_SET_DEFAULT(se->op.readdirplus && se->op.readdir,
2022 FUSE_CAP_READDIRPLUS_AUTO);
2023 se->conn.time_gran = 1;
2025 if (bufsize < FUSE_MIN_READ_BUFFER) {
2026 fuse_log(FUSE_LOG_ERR, "fuse: warning: buffer size too small: %zu\n",
2027 bufsize);
2028 bufsize = FUSE_MIN_READ_BUFFER;
2030 se->bufsize = bufsize;
2032 if (se->conn.max_write > bufsize - FUSE_BUFFER_HEADER_SIZE) {
2033 se->conn.max_write = bufsize - FUSE_BUFFER_HEADER_SIZE;
2036 se->got_init = 1;
2037 se->got_destroy = 0;
2038 if (se->op.init) {
2039 se->op.init(se->userdata, &se->conn);
2042 if (se->conn.want & (~se->conn.capable)) {
2043 fuse_log(FUSE_LOG_ERR,
2044 "fuse: error: filesystem requested capabilities "
2045 "0x%x that are not supported by kernel, aborting.\n",
2046 se->conn.want & (~se->conn.capable));
2047 fuse_reply_err(req, EPROTO);
2048 se->error = -EPROTO;
2049 fuse_session_exit(se);
2050 return;
2053 if (se->conn.max_write < bufsize - FUSE_BUFFER_HEADER_SIZE) {
2054 se->bufsize = se->conn.max_write + FUSE_BUFFER_HEADER_SIZE;
2056 if (arg->flags & FUSE_MAX_PAGES) {
2057 outarg.flags |= FUSE_MAX_PAGES;
2058 outarg.max_pages = (se->conn.max_write - 1) / getpagesize() + 1;
2062 * Always enable big writes, this is superseded
2063 * by the max_write option
2065 outarg.flags |= FUSE_BIG_WRITES;
2067 if (se->conn.want & FUSE_CAP_ASYNC_READ) {
2068 outarg.flags |= FUSE_ASYNC_READ;
2070 if (se->conn.want & FUSE_CAP_PARALLEL_DIROPS) {
2071 outarg.flags |= FUSE_PARALLEL_DIROPS;
2073 if (se->conn.want & FUSE_CAP_POSIX_LOCKS) {
2074 outarg.flags |= FUSE_POSIX_LOCKS;
2076 if (se->conn.want & FUSE_CAP_ATOMIC_O_TRUNC) {
2077 outarg.flags |= FUSE_ATOMIC_O_TRUNC;
2079 if (se->conn.want & FUSE_CAP_EXPORT_SUPPORT) {
2080 outarg.flags |= FUSE_EXPORT_SUPPORT;
2082 if (se->conn.want & FUSE_CAP_DONT_MASK) {
2083 outarg.flags |= FUSE_DONT_MASK;
2085 if (se->conn.want & FUSE_CAP_FLOCK_LOCKS) {
2086 outarg.flags |= FUSE_FLOCK_LOCKS;
2088 if (se->conn.want & FUSE_CAP_AUTO_INVAL_DATA) {
2089 outarg.flags |= FUSE_AUTO_INVAL_DATA;
2091 if (se->conn.want & FUSE_CAP_READDIRPLUS) {
2092 outarg.flags |= FUSE_DO_READDIRPLUS;
2094 if (se->conn.want & FUSE_CAP_READDIRPLUS_AUTO) {
2095 outarg.flags |= FUSE_READDIRPLUS_AUTO;
2097 if (se->conn.want & FUSE_CAP_ASYNC_DIO) {
2098 outarg.flags |= FUSE_ASYNC_DIO;
2100 if (se->conn.want & FUSE_CAP_WRITEBACK_CACHE) {
2101 outarg.flags |= FUSE_WRITEBACK_CACHE;
2103 if (se->conn.want & FUSE_CAP_POSIX_ACL) {
2104 outarg.flags |= FUSE_POSIX_ACL;
2106 outarg.max_readahead = se->conn.max_readahead;
2107 outarg.max_write = se->conn.max_write;
2108 if (se->conn.max_background >= (1 << 16)) {
2109 se->conn.max_background = (1 << 16) - 1;
2111 if (se->conn.congestion_threshold > se->conn.max_background) {
2112 se->conn.congestion_threshold = se->conn.max_background;
2114 if (!se->conn.congestion_threshold) {
2115 se->conn.congestion_threshold = se->conn.max_background * 3 / 4;
2118 outarg.max_background = se->conn.max_background;
2119 outarg.congestion_threshold = se->conn.congestion_threshold;
2120 outarg.time_gran = se->conn.time_gran;
2122 if (se->conn.want & FUSE_CAP_HANDLE_KILLPRIV_V2) {
2123 outarg.flags |= FUSE_HANDLE_KILLPRIV_V2;
2126 fuse_log(FUSE_LOG_DEBUG, " INIT: %u.%u\n", outarg.major, outarg.minor);
2127 fuse_log(FUSE_LOG_DEBUG, " flags=0x%08x\n", outarg.flags);
2128 fuse_log(FUSE_LOG_DEBUG, " max_readahead=0x%08x\n", outarg.max_readahead);
2129 fuse_log(FUSE_LOG_DEBUG, " max_write=0x%08x\n", outarg.max_write);
2130 fuse_log(FUSE_LOG_DEBUG, " max_background=%i\n", outarg.max_background);
2131 fuse_log(FUSE_LOG_DEBUG, " congestion_threshold=%i\n",
2132 outarg.congestion_threshold);
2133 fuse_log(FUSE_LOG_DEBUG, " time_gran=%u\n", outarg.time_gran);
2135 send_reply_ok(req, &outarg, outargsize);
2138 static void do_destroy(fuse_req_t req, fuse_ino_t nodeid,
2139 struct fuse_mbuf_iter *iter)
2141 struct fuse_session *se = req->se;
2143 (void)nodeid;
2144 (void)iter;
2146 se->got_destroy = 1;
2147 se->got_init = 0;
2148 if (se->op.destroy) {
2149 se->op.destroy(se->userdata);
2152 send_reply_ok(req, NULL, 0);
2155 int fuse_lowlevel_notify_store(struct fuse_session *se, fuse_ino_t ino,
2156 off_t offset, struct fuse_bufvec *bufv)
2158 struct fuse_out_header out = {
2159 .error = FUSE_NOTIFY_STORE,
2161 struct fuse_notify_store_out outarg = {
2162 .nodeid = ino,
2163 .offset = offset,
2164 .size = fuse_buf_size(bufv),
2166 struct iovec iov[3];
2167 int res;
2169 if (!se) {
2170 return -EINVAL;
2173 iov[0].iov_base = &out;
2174 iov[0].iov_len = sizeof(out);
2175 iov[1].iov_base = &outarg;
2176 iov[1].iov_len = sizeof(outarg);
2178 res = fuse_send_data_iov(se, NULL, iov, 2, bufv);
2179 if (res > 0) {
2180 res = -res;
2183 return res;
2186 void *fuse_req_userdata(fuse_req_t req)
2188 return req->se->userdata;
2191 const struct fuse_ctx *fuse_req_ctx(fuse_req_t req)
2193 return &req->ctx;
2196 void fuse_req_interrupt_func(fuse_req_t req, fuse_interrupt_func_t func,
2197 void *data)
2199 pthread_mutex_lock(&req->lock);
2200 pthread_mutex_lock(&req->se->lock);
2201 req->u.ni.func = func;
2202 req->u.ni.data = data;
2203 pthread_mutex_unlock(&req->se->lock);
2204 if (req->interrupted && func) {
2205 func(req, data);
2207 pthread_mutex_unlock(&req->lock);
2210 int fuse_req_interrupted(fuse_req_t req)
2212 int interrupted;
2214 pthread_mutex_lock(&req->se->lock);
2215 interrupted = req->interrupted;
2216 pthread_mutex_unlock(&req->se->lock);
2218 return interrupted;
2221 static struct {
2222 void (*func)(fuse_req_t, fuse_ino_t, struct fuse_mbuf_iter *);
2223 const char *name;
2224 } fuse_ll_ops[] = {
2225 [FUSE_LOOKUP] = { do_lookup, "LOOKUP" },
2226 [FUSE_FORGET] = { do_forget, "FORGET" },
2227 [FUSE_GETATTR] = { do_getattr, "GETATTR" },
2228 [FUSE_SETATTR] = { do_setattr, "SETATTR" },
2229 [FUSE_READLINK] = { do_readlink, "READLINK" },
2230 [FUSE_SYMLINK] = { do_symlink, "SYMLINK" },
2231 [FUSE_MKNOD] = { do_mknod, "MKNOD" },
2232 [FUSE_MKDIR] = { do_mkdir, "MKDIR" },
2233 [FUSE_UNLINK] = { do_unlink, "UNLINK" },
2234 [FUSE_RMDIR] = { do_rmdir, "RMDIR" },
2235 [FUSE_RENAME] = { do_rename, "RENAME" },
2236 [FUSE_LINK] = { do_link, "LINK" },
2237 [FUSE_OPEN] = { do_open, "OPEN" },
2238 [FUSE_READ] = { do_read, "READ" },
2239 [FUSE_WRITE] = { do_write, "WRITE" },
2240 [FUSE_STATFS] = { do_statfs, "STATFS" },
2241 [FUSE_RELEASE] = { do_release, "RELEASE" },
2242 [FUSE_FSYNC] = { do_fsync, "FSYNC" },
2243 [FUSE_SETXATTR] = { do_setxattr, "SETXATTR" },
2244 [FUSE_GETXATTR] = { do_getxattr, "GETXATTR" },
2245 [FUSE_LISTXATTR] = { do_listxattr, "LISTXATTR" },
2246 [FUSE_REMOVEXATTR] = { do_removexattr, "REMOVEXATTR" },
2247 [FUSE_FLUSH] = { do_flush, "FLUSH" },
2248 [FUSE_INIT] = { do_init, "INIT" },
2249 [FUSE_OPENDIR] = { do_opendir, "OPENDIR" },
2250 [FUSE_READDIR] = { do_readdir, "READDIR" },
2251 [FUSE_RELEASEDIR] = { do_releasedir, "RELEASEDIR" },
2252 [FUSE_FSYNCDIR] = { do_fsyncdir, "FSYNCDIR" },
2253 [FUSE_GETLK] = { do_getlk, "GETLK" },
2254 [FUSE_SETLK] = { do_setlk, "SETLK" },
2255 [FUSE_SETLKW] = { do_setlkw, "SETLKW" },
2256 [FUSE_ACCESS] = { do_access, "ACCESS" },
2257 [FUSE_CREATE] = { do_create, "CREATE" },
2258 [FUSE_INTERRUPT] = { do_interrupt, "INTERRUPT" },
2259 [FUSE_BMAP] = { do_bmap, "BMAP" },
2260 [FUSE_IOCTL] = { do_ioctl, "IOCTL" },
2261 [FUSE_POLL] = { do_poll, "POLL" },
2262 [FUSE_FALLOCATE] = { do_fallocate, "FALLOCATE" },
2263 [FUSE_DESTROY] = { do_destroy, "DESTROY" },
2264 [FUSE_NOTIFY_REPLY] = { NULL, "NOTIFY_REPLY" },
2265 [FUSE_BATCH_FORGET] = { do_batch_forget, "BATCH_FORGET" },
2266 [FUSE_READDIRPLUS] = { do_readdirplus, "READDIRPLUS" },
2267 [FUSE_RENAME2] = { do_rename2, "RENAME2" },
2268 [FUSE_COPY_FILE_RANGE] = { do_copy_file_range, "COPY_FILE_RANGE" },
2269 [FUSE_LSEEK] = { do_lseek, "LSEEK" },
2272 #define FUSE_MAXOP (sizeof(fuse_ll_ops) / sizeof(fuse_ll_ops[0]))
2274 static const char *opname(enum fuse_opcode opcode)
2276 if (opcode >= FUSE_MAXOP || !fuse_ll_ops[opcode].name) {
2277 return "???";
2278 } else {
2279 return fuse_ll_ops[opcode].name;
2283 void fuse_session_process_buf(struct fuse_session *se,
2284 const struct fuse_buf *buf)
2286 struct fuse_bufvec bufv = { .buf[0] = *buf, .count = 1 };
2287 fuse_session_process_buf_int(se, &bufv, NULL);
2291 * Restriction:
2292 * bufv is normally a single entry buffer, except for a write
2293 * where (if it's in memory) then the bufv may be multiple entries,
2294 * where the first entry contains all headers and subsequent entries
2295 * contain data
2296 * bufv shall not use any offsets etc to make the data anything
2297 * other than contiguous starting from 0.
2299 void fuse_session_process_buf_int(struct fuse_session *se,
2300 struct fuse_bufvec *bufv,
2301 struct fuse_chan *ch)
2303 const struct fuse_buf *buf = bufv->buf;
2304 struct fuse_mbuf_iter iter = FUSE_MBUF_ITER_INIT(buf);
2305 struct fuse_in_header *in;
2306 struct fuse_req *req;
2307 int err;
2309 /* The first buffer must be a memory buffer */
2310 assert(!(buf->flags & FUSE_BUF_IS_FD));
2312 in = fuse_mbuf_iter_advance(&iter, sizeof(*in));
2313 assert(in); /* caller guarantees the input buffer is large enough */
2315 fuse_log(
2316 FUSE_LOG_DEBUG,
2317 "unique: %llu, opcode: %s (%i), nodeid: %llu, insize: %zu, pid: %u\n",
2318 (unsigned long long)in->unique, opname((enum fuse_opcode)in->opcode),
2319 in->opcode, (unsigned long long)in->nodeid, buf->size, in->pid);
2321 req = fuse_ll_alloc_req(se);
2322 if (req == NULL) {
2323 struct fuse_out_header out = {
2324 .unique = in->unique,
2325 .error = -ENOMEM,
2327 struct iovec iov = {
2328 .iov_base = &out,
2329 .iov_len = sizeof(struct fuse_out_header),
2332 fuse_send_msg(se, ch, &iov, 1);
2333 return;
2336 req->unique = in->unique;
2337 req->ctx.uid = in->uid;
2338 req->ctx.gid = in->gid;
2339 req->ctx.pid = in->pid;
2340 req->ch = ch;
2343 * INIT and DESTROY requests are serialized, all other request types
2344 * run in parallel. This prevents races between FUSE_INIT and ordinary
2345 * requests, FUSE_INIT and FUSE_INIT, FUSE_INIT and FUSE_DESTROY, and
2346 * FUSE_DESTROY and FUSE_DESTROY.
2348 if (in->opcode == FUSE_INIT || in->opcode == CUSE_INIT ||
2349 in->opcode == FUSE_DESTROY) {
2350 pthread_rwlock_wrlock(&se->init_rwlock);
2351 } else {
2352 pthread_rwlock_rdlock(&se->init_rwlock);
2355 err = EIO;
2356 if (!se->got_init) {
2357 enum fuse_opcode expected;
2359 expected = se->cuse_data ? CUSE_INIT : FUSE_INIT;
2360 if (in->opcode != expected) {
2361 goto reply_err;
2363 } else if (in->opcode == FUSE_INIT || in->opcode == CUSE_INIT) {
2364 if (fuse_lowlevel_is_virtio(se)) {
2366 * TODO: This is after a hard reboot typically, we need to do
2367 * a destroy, but we can't reply to this request yet so
2368 * we can't use do_destroy
2370 fuse_log(FUSE_LOG_DEBUG, "%s: reinit\n", __func__);
2371 se->got_destroy = 1;
2372 se->got_init = 0;
2373 if (se->op.destroy) {
2374 se->op.destroy(se->userdata);
2376 } else {
2377 goto reply_err;
2381 err = EACCES;
2382 /* Implement -o allow_root */
2383 if (se->deny_others && in->uid != se->owner && in->uid != 0 &&
2384 in->opcode != FUSE_INIT && in->opcode != FUSE_READ &&
2385 in->opcode != FUSE_WRITE && in->opcode != FUSE_FSYNC &&
2386 in->opcode != FUSE_RELEASE && in->opcode != FUSE_READDIR &&
2387 in->opcode != FUSE_FSYNCDIR && in->opcode != FUSE_RELEASEDIR &&
2388 in->opcode != FUSE_NOTIFY_REPLY && in->opcode != FUSE_READDIRPLUS) {
2389 goto reply_err;
2392 err = ENOSYS;
2393 if (in->opcode >= FUSE_MAXOP || !fuse_ll_ops[in->opcode].func) {
2394 goto reply_err;
2396 if (in->opcode != FUSE_INTERRUPT) {
2397 struct fuse_req *intr;
2398 pthread_mutex_lock(&se->lock);
2399 intr = check_interrupt(se, req);
2400 list_add_req(req, &se->list);
2401 pthread_mutex_unlock(&se->lock);
2402 if (intr) {
2403 fuse_reply_err(intr, EAGAIN);
2407 if (in->opcode == FUSE_WRITE && se->op.write_buf) {
2408 do_write_buf(req, in->nodeid, &iter, bufv);
2409 } else {
2410 fuse_ll_ops[in->opcode].func(req, in->nodeid, &iter);
2413 pthread_rwlock_unlock(&se->init_rwlock);
2414 return;
2416 reply_err:
2417 fuse_reply_err(req, err);
2418 pthread_rwlock_unlock(&se->init_rwlock);
2421 #define LL_OPTION(n, o, v) \
2423 n, offsetof(struct fuse_session, o), v \
2426 static const struct fuse_opt fuse_ll_opts[] = {
2427 LL_OPTION("debug", debug, 1),
2428 LL_OPTION("-d", debug, 1),
2429 LL_OPTION("--debug", debug, 1),
2430 LL_OPTION("allow_root", deny_others, 1),
2431 LL_OPTION("--socket-path=%s", vu_socket_path, 0),
2432 LL_OPTION("--socket-group=%s", vu_socket_group, 0),
2433 LL_OPTION("--fd=%d", vu_listen_fd, 0),
2434 LL_OPTION("--thread-pool-size=%d", thread_pool_size, 0),
2435 FUSE_OPT_END
2438 void fuse_lowlevel_version(void)
2440 printf("using FUSE kernel interface version %i.%i\n", FUSE_KERNEL_VERSION,
2441 FUSE_KERNEL_MINOR_VERSION);
2444 void fuse_lowlevel_help(void)
2447 * These are not all options, but the ones that are
2448 * potentially of interest to an end-user
2450 printf(
2451 " -o allow_root allow access by root\n"
2452 " --socket-path=PATH path for the vhost-user socket\n"
2453 " --fd=FDNUM fd number of vhost-user socket\n"
2454 " --thread-pool-size=NUM thread pool size limit (default %d)\n",
2455 THREAD_POOL_SIZE);
2458 void fuse_session_destroy(struct fuse_session *se)
2460 if (se->got_init && !se->got_destroy) {
2461 if (se->op.destroy) {
2462 se->op.destroy(se->userdata);
2465 pthread_rwlock_destroy(&se->init_rwlock);
2466 pthread_mutex_destroy(&se->lock);
2467 free(se->cuse_data);
2468 if (se->fd != -1) {
2469 close(se->fd);
2472 if (fuse_lowlevel_is_virtio(se)) {
2473 virtio_session_close(se);
2476 free(se->vu_socket_path);
2477 se->vu_socket_path = NULL;
2479 free(se);
2483 struct fuse_session *fuse_session_new(struct fuse_args *args,
2484 const struct fuse_lowlevel_ops *op,
2485 size_t op_size, void *userdata)
2487 struct fuse_session *se;
2489 if (sizeof(struct fuse_lowlevel_ops) < op_size) {
2490 fuse_log(
2491 FUSE_LOG_ERR,
2492 "fuse: warning: library too old, some operations may not work\n");
2493 op_size = sizeof(struct fuse_lowlevel_ops);
2496 if (args->argc == 0) {
2497 fuse_log(FUSE_LOG_ERR,
2498 "fuse: empty argv passed to fuse_session_new().\n");
2499 return NULL;
2502 se = (struct fuse_session *)calloc(1, sizeof(struct fuse_session));
2503 if (se == NULL) {
2504 fuse_log(FUSE_LOG_ERR, "fuse: failed to allocate fuse object\n");
2505 goto out1;
2507 se->fd = -1;
2508 se->vu_listen_fd = -1;
2509 se->thread_pool_size = THREAD_POOL_SIZE;
2510 se->conn.max_write = UINT_MAX;
2511 se->conn.max_readahead = UINT_MAX;
2513 /* Parse options */
2514 if (fuse_opt_parse(args, se, fuse_ll_opts, NULL) == -1) {
2515 goto out2;
2517 if (args->argc == 1 && args->argv[0][0] == '-') {
2518 fuse_log(FUSE_LOG_ERR,
2519 "fuse: warning: argv[0] looks like an option, but "
2520 "will be ignored\n");
2521 } else if (args->argc != 1) {
2522 int i;
2523 fuse_log(FUSE_LOG_ERR, "fuse: unknown option(s): `");
2524 for (i = 1; i < args->argc - 1; i++) {
2525 fuse_log(FUSE_LOG_ERR, "%s ", args->argv[i]);
2527 fuse_log(FUSE_LOG_ERR, "%s'\n", args->argv[i]);
2528 goto out4;
2531 if (!se->vu_socket_path && se->vu_listen_fd < 0) {
2532 fuse_log(FUSE_LOG_ERR, "fuse: missing --socket-path or --fd option\n");
2533 goto out4;
2535 if (se->vu_socket_path && se->vu_listen_fd >= 0) {
2536 fuse_log(FUSE_LOG_ERR,
2537 "fuse: --socket-path and --fd cannot be given together\n");
2538 goto out4;
2540 if (se->vu_socket_group && !se->vu_socket_path) {
2541 fuse_log(FUSE_LOG_ERR,
2542 "fuse: --socket-group can only be used with --socket-path\n");
2543 goto out4;
2546 se->bufsize = FUSE_MAX_MAX_PAGES * getpagesize() + FUSE_BUFFER_HEADER_SIZE;
2548 list_init_req(&se->list);
2549 list_init_req(&se->interrupts);
2550 fuse_mutex_init(&se->lock);
2551 pthread_rwlock_init(&se->init_rwlock, NULL);
2553 memcpy(&se->op, op, op_size);
2554 se->owner = getuid();
2555 se->userdata = userdata;
2557 return se;
2559 out4:
2560 fuse_opt_free_args(args);
2561 out2:
2562 free(se);
2563 out1:
2564 return NULL;
2567 int fuse_session_mount(struct fuse_session *se)
2569 return virtio_session_mount(se);
2572 int fuse_session_fd(struct fuse_session *se)
2574 return se->fd;
2577 void fuse_session_unmount(struct fuse_session *se)
2581 int fuse_lowlevel_is_virtio(struct fuse_session *se)
2583 return !!se->virtio_dev;
2586 void fuse_session_exit(struct fuse_session *se)
2588 se->exited = 1;
2591 void fuse_session_reset(struct fuse_session *se)
2593 se->exited = 0;
2594 se->error = 0;
2597 int fuse_session_exited(struct fuse_session *se)
2599 return se->exited;