iotests/287: Clean up subshell test image
[qemu.git] / tools / virtiofsd / fuse_lowlevel.c
blobd4119e92ab4111e499deebb2ef31d9591d612033
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 64
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;
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;
1073 req->ctx.umask = arg->umask;
1075 req->se->op.create(req, nodeid, name, arg->mode, &fi);
1076 } else {
1077 fuse_reply_err(req, ENOSYS);
1081 static void do_open(fuse_req_t req, fuse_ino_t nodeid,
1082 struct fuse_mbuf_iter *iter)
1084 struct fuse_open_in *arg;
1085 struct fuse_file_info fi;
1087 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1088 if (!arg) {
1089 fuse_reply_err(req, EINVAL);
1090 return;
1093 memset(&fi, 0, sizeof(fi));
1094 fi.flags = arg->flags;
1096 if (req->se->op.open) {
1097 req->se->op.open(req, nodeid, &fi);
1098 } else {
1099 fuse_reply_open(req, &fi);
1103 static void do_read(fuse_req_t req, fuse_ino_t nodeid,
1104 struct fuse_mbuf_iter *iter)
1106 if (req->se->op.read) {
1107 struct fuse_read_in *arg;
1108 struct fuse_file_info fi;
1110 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1111 if (!arg) {
1112 fuse_reply_err(req, EINVAL);
1113 return;
1116 memset(&fi, 0, sizeof(fi));
1117 fi.fh = arg->fh;
1118 fi.lock_owner = arg->lock_owner;
1119 fi.flags = arg->flags;
1120 req->se->op.read(req, nodeid, arg->size, arg->offset, &fi);
1121 } else {
1122 fuse_reply_err(req, ENOSYS);
1126 static void do_write(fuse_req_t req, fuse_ino_t nodeid,
1127 struct fuse_mbuf_iter *iter)
1129 struct fuse_write_in *arg;
1130 struct fuse_file_info fi;
1131 const char *param;
1133 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1134 if (!arg) {
1135 fuse_reply_err(req, EINVAL);
1136 return;
1139 param = fuse_mbuf_iter_advance(iter, arg->size);
1140 if (!param) {
1141 fuse_reply_err(req, EINVAL);
1142 return;
1145 memset(&fi, 0, sizeof(fi));
1146 fi.fh = arg->fh;
1147 fi.writepage = (arg->write_flags & FUSE_WRITE_CACHE) != 0;
1148 fi.kill_priv = !!(arg->write_flags & FUSE_WRITE_KILL_PRIV);
1150 fi.lock_owner = arg->lock_owner;
1151 fi.flags = arg->flags;
1153 if (req->se->op.write) {
1154 req->se->op.write(req, nodeid, param, arg->size, arg->offset, &fi);
1155 } else {
1156 fuse_reply_err(req, ENOSYS);
1160 static void do_write_buf(fuse_req_t req, fuse_ino_t nodeid,
1161 struct fuse_mbuf_iter *iter, struct fuse_bufvec *ibufv)
1163 struct fuse_session *se = req->se;
1164 struct fuse_bufvec *pbufv = ibufv;
1165 struct fuse_bufvec tmpbufv = {
1166 .buf[0] = ibufv->buf[0],
1167 .count = 1,
1169 struct fuse_write_in *arg;
1170 size_t arg_size = sizeof(*arg);
1171 struct fuse_file_info fi;
1173 memset(&fi, 0, sizeof(fi));
1175 arg = fuse_mbuf_iter_advance(iter, arg_size);
1176 if (!arg) {
1177 fuse_reply_err(req, EINVAL);
1178 return;
1181 fi.lock_owner = arg->lock_owner;
1182 fi.flags = arg->flags;
1183 fi.fh = arg->fh;
1184 fi.writepage = !!(arg->write_flags & FUSE_WRITE_CACHE);
1185 fi.kill_priv = !!(arg->write_flags & FUSE_WRITE_KILL_PRIV);
1187 if (ibufv->count == 1) {
1188 assert(!(tmpbufv.buf[0].flags & FUSE_BUF_IS_FD));
1189 tmpbufv.buf[0].mem = ((char *)arg) + arg_size;
1190 tmpbufv.buf[0].size -= sizeof(struct fuse_in_header) + arg_size;
1191 pbufv = &tmpbufv;
1192 } else {
1194 * Input bufv contains the headers in the first element
1195 * and the data in the rest, we need to skip that first element
1197 ibufv->buf[0].size = 0;
1200 if (fuse_buf_size(pbufv) != arg->size) {
1201 fuse_log(FUSE_LOG_ERR,
1202 "fuse: do_write_buf: buffer size doesn't match arg->size\n");
1203 fuse_reply_err(req, EIO);
1204 return;
1207 se->op.write_buf(req, nodeid, pbufv, arg->offset, &fi);
1210 static void do_flush(fuse_req_t req, fuse_ino_t nodeid,
1211 struct fuse_mbuf_iter *iter)
1213 struct fuse_flush_in *arg;
1214 struct fuse_file_info fi;
1216 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1217 if (!arg) {
1218 fuse_reply_err(req, EINVAL);
1219 return;
1222 memset(&fi, 0, sizeof(fi));
1223 fi.fh = arg->fh;
1224 fi.flush = 1;
1225 fi.lock_owner = arg->lock_owner;
1227 if (req->se->op.flush) {
1228 req->se->op.flush(req, nodeid, &fi);
1229 } else {
1230 fuse_reply_err(req, ENOSYS);
1234 static void do_release(fuse_req_t req, fuse_ino_t nodeid,
1235 struct fuse_mbuf_iter *iter)
1237 struct fuse_release_in *arg;
1238 struct fuse_file_info fi;
1240 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1241 if (!arg) {
1242 fuse_reply_err(req, EINVAL);
1243 return;
1246 memset(&fi, 0, sizeof(fi));
1247 fi.flags = arg->flags;
1248 fi.fh = arg->fh;
1249 fi.flush = (arg->release_flags & FUSE_RELEASE_FLUSH) ? 1 : 0;
1250 fi.lock_owner = arg->lock_owner;
1252 if (arg->release_flags & FUSE_RELEASE_FLOCK_UNLOCK) {
1253 fi.flock_release = 1;
1256 if (req->se->op.release) {
1257 req->se->op.release(req, nodeid, &fi);
1258 } else {
1259 fuse_reply_err(req, 0);
1263 static void do_fsync(fuse_req_t req, fuse_ino_t nodeid,
1264 struct fuse_mbuf_iter *iter)
1266 struct fuse_fsync_in *arg;
1267 struct fuse_file_info fi;
1268 int datasync;
1270 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1271 if (!arg) {
1272 fuse_reply_err(req, EINVAL);
1273 return;
1275 datasync = arg->fsync_flags & 1;
1277 memset(&fi, 0, sizeof(fi));
1278 fi.fh = arg->fh;
1280 if (req->se->op.fsync) {
1281 if (fi.fh == (uint64_t)-1) {
1282 req->se->op.fsync(req, nodeid, datasync, NULL);
1283 } else {
1284 req->se->op.fsync(req, nodeid, datasync, &fi);
1286 } else {
1287 fuse_reply_err(req, ENOSYS);
1291 static void do_opendir(fuse_req_t req, fuse_ino_t nodeid,
1292 struct fuse_mbuf_iter *iter)
1294 struct fuse_open_in *arg;
1295 struct fuse_file_info fi;
1297 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1298 if (!arg) {
1299 fuse_reply_err(req, EINVAL);
1300 return;
1303 memset(&fi, 0, sizeof(fi));
1304 fi.flags = arg->flags;
1306 if (req->se->op.opendir) {
1307 req->se->op.opendir(req, nodeid, &fi);
1308 } else {
1309 fuse_reply_open(req, &fi);
1313 static void do_readdir(fuse_req_t req, fuse_ino_t nodeid,
1314 struct fuse_mbuf_iter *iter)
1316 struct fuse_read_in *arg;
1317 struct fuse_file_info fi;
1319 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1320 if (!arg) {
1321 fuse_reply_err(req, EINVAL);
1322 return;
1325 memset(&fi, 0, sizeof(fi));
1326 fi.fh = arg->fh;
1328 if (req->se->op.readdir) {
1329 req->se->op.readdir(req, nodeid, arg->size, arg->offset, &fi);
1330 } else {
1331 fuse_reply_err(req, ENOSYS);
1335 static void do_readdirplus(fuse_req_t req, fuse_ino_t nodeid,
1336 struct fuse_mbuf_iter *iter)
1338 struct fuse_read_in *arg;
1339 struct fuse_file_info fi;
1341 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1342 if (!arg) {
1343 fuse_reply_err(req, EINVAL);
1344 return;
1347 memset(&fi, 0, sizeof(fi));
1348 fi.fh = arg->fh;
1350 if (req->se->op.readdirplus) {
1351 req->se->op.readdirplus(req, nodeid, arg->size, arg->offset, &fi);
1352 } else {
1353 fuse_reply_err(req, ENOSYS);
1357 static void do_releasedir(fuse_req_t req, fuse_ino_t nodeid,
1358 struct fuse_mbuf_iter *iter)
1360 struct fuse_release_in *arg;
1361 struct fuse_file_info fi;
1363 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1364 if (!arg) {
1365 fuse_reply_err(req, EINVAL);
1366 return;
1369 memset(&fi, 0, sizeof(fi));
1370 fi.flags = arg->flags;
1371 fi.fh = arg->fh;
1373 if (req->se->op.releasedir) {
1374 req->se->op.releasedir(req, nodeid, &fi);
1375 } else {
1376 fuse_reply_err(req, 0);
1380 static void do_fsyncdir(fuse_req_t req, fuse_ino_t nodeid,
1381 struct fuse_mbuf_iter *iter)
1383 struct fuse_fsync_in *arg;
1384 struct fuse_file_info fi;
1385 int datasync;
1387 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1388 if (!arg) {
1389 fuse_reply_err(req, EINVAL);
1390 return;
1392 datasync = arg->fsync_flags & 1;
1394 memset(&fi, 0, sizeof(fi));
1395 fi.fh = arg->fh;
1397 if (req->se->op.fsyncdir) {
1398 req->se->op.fsyncdir(req, nodeid, datasync, &fi);
1399 } else {
1400 fuse_reply_err(req, ENOSYS);
1404 static void do_statfs(fuse_req_t req, fuse_ino_t nodeid,
1405 struct fuse_mbuf_iter *iter)
1407 (void)nodeid;
1408 (void)iter;
1410 if (req->se->op.statfs) {
1411 req->se->op.statfs(req, nodeid);
1412 } else {
1413 struct statvfs buf = {
1414 .f_namemax = 255,
1415 .f_bsize = 512,
1417 fuse_reply_statfs(req, &buf);
1421 static void do_setxattr(fuse_req_t req, fuse_ino_t nodeid,
1422 struct fuse_mbuf_iter *iter)
1424 struct fuse_setxattr_in *arg;
1425 const char *name;
1426 const char *value;
1428 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1429 name = fuse_mbuf_iter_advance_str(iter);
1430 if (!arg || !name) {
1431 fuse_reply_err(req, EINVAL);
1432 return;
1435 value = fuse_mbuf_iter_advance(iter, arg->size);
1436 if (!value) {
1437 fuse_reply_err(req, EINVAL);
1438 return;
1441 if (req->se->op.setxattr) {
1442 req->se->op.setxattr(req, nodeid, name, value, arg->size, arg->flags);
1443 } else {
1444 fuse_reply_err(req, ENOSYS);
1448 static void do_getxattr(fuse_req_t req, fuse_ino_t nodeid,
1449 struct fuse_mbuf_iter *iter)
1451 struct fuse_getxattr_in *arg;
1452 const char *name;
1454 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1455 name = fuse_mbuf_iter_advance_str(iter);
1456 if (!arg || !name) {
1457 fuse_reply_err(req, EINVAL);
1458 return;
1461 if (req->se->op.getxattr) {
1462 req->se->op.getxattr(req, nodeid, name, arg->size);
1463 } else {
1464 fuse_reply_err(req, ENOSYS);
1468 static void do_listxattr(fuse_req_t req, fuse_ino_t nodeid,
1469 struct fuse_mbuf_iter *iter)
1471 struct fuse_getxattr_in *arg;
1473 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1474 if (!arg) {
1475 fuse_reply_err(req, EINVAL);
1476 return;
1479 if (req->se->op.listxattr) {
1480 req->se->op.listxattr(req, nodeid, arg->size);
1481 } else {
1482 fuse_reply_err(req, ENOSYS);
1486 static void do_removexattr(fuse_req_t req, fuse_ino_t nodeid,
1487 struct fuse_mbuf_iter *iter)
1489 const char *name = fuse_mbuf_iter_advance_str(iter);
1491 if (!name) {
1492 fuse_reply_err(req, EINVAL);
1493 return;
1496 if (req->se->op.removexattr) {
1497 req->se->op.removexattr(req, nodeid, name);
1498 } else {
1499 fuse_reply_err(req, ENOSYS);
1503 static void convert_fuse_file_lock(struct fuse_file_lock *fl,
1504 struct flock *flock)
1506 memset(flock, 0, sizeof(struct flock));
1507 flock->l_type = fl->type;
1508 flock->l_whence = SEEK_SET;
1509 flock->l_start = fl->start;
1510 if (fl->end == OFFSET_MAX) {
1511 flock->l_len = 0;
1512 } else {
1513 flock->l_len = fl->end - fl->start + 1;
1515 flock->l_pid = fl->pid;
1518 static void do_getlk(fuse_req_t req, fuse_ino_t nodeid,
1519 struct fuse_mbuf_iter *iter)
1521 struct fuse_lk_in *arg;
1522 struct fuse_file_info fi;
1523 struct flock flock;
1525 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1526 if (!arg) {
1527 fuse_reply_err(req, EINVAL);
1528 return;
1531 memset(&fi, 0, sizeof(fi));
1532 fi.fh = arg->fh;
1533 fi.lock_owner = arg->owner;
1535 convert_fuse_file_lock(&arg->lk, &flock);
1536 if (req->se->op.getlk) {
1537 req->se->op.getlk(req, nodeid, &fi, &flock);
1538 } else {
1539 fuse_reply_err(req, ENOSYS);
1543 static void do_setlk_common(fuse_req_t req, fuse_ino_t nodeid,
1544 struct fuse_mbuf_iter *iter, int sleep)
1546 struct fuse_lk_in *arg;
1547 struct fuse_file_info fi;
1548 struct flock flock;
1550 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1551 if (!arg) {
1552 fuse_reply_err(req, EINVAL);
1553 return;
1556 memset(&fi, 0, sizeof(fi));
1557 fi.fh = arg->fh;
1558 fi.lock_owner = arg->owner;
1560 if (arg->lk_flags & FUSE_LK_FLOCK) {
1561 int op = 0;
1563 switch (arg->lk.type) {
1564 case F_RDLCK:
1565 op = LOCK_SH;
1566 break;
1567 case F_WRLCK:
1568 op = LOCK_EX;
1569 break;
1570 case F_UNLCK:
1571 op = LOCK_UN;
1572 break;
1574 if (!sleep) {
1575 op |= LOCK_NB;
1578 if (req->se->op.flock) {
1579 req->se->op.flock(req, nodeid, &fi, op);
1580 } else {
1581 fuse_reply_err(req, ENOSYS);
1583 } else {
1584 convert_fuse_file_lock(&arg->lk, &flock);
1585 if (req->se->op.setlk) {
1586 req->se->op.setlk(req, nodeid, &fi, &flock, sleep);
1587 } else {
1588 fuse_reply_err(req, ENOSYS);
1593 static void do_setlk(fuse_req_t req, fuse_ino_t nodeid,
1594 struct fuse_mbuf_iter *iter)
1596 do_setlk_common(req, nodeid, iter, 0);
1599 static void do_setlkw(fuse_req_t req, fuse_ino_t nodeid,
1600 struct fuse_mbuf_iter *iter)
1602 do_setlk_common(req, nodeid, iter, 1);
1605 static int find_interrupted(struct fuse_session *se, struct fuse_req *req)
1607 struct fuse_req *curr;
1609 for (curr = se->list.next; curr != &se->list; curr = curr->next) {
1610 if (curr->unique == req->u.i.unique) {
1611 fuse_interrupt_func_t func;
1612 void *data;
1614 curr->ctr++;
1615 pthread_mutex_unlock(&se->lock);
1617 /* Ugh, ugly locking */
1618 pthread_mutex_lock(&curr->lock);
1619 pthread_mutex_lock(&se->lock);
1620 curr->interrupted = 1;
1621 func = curr->u.ni.func;
1622 data = curr->u.ni.data;
1623 pthread_mutex_unlock(&se->lock);
1624 if (func) {
1625 func(curr, data);
1627 pthread_mutex_unlock(&curr->lock);
1629 pthread_mutex_lock(&se->lock);
1630 curr->ctr--;
1631 if (!curr->ctr) {
1632 destroy_req(curr);
1635 return 1;
1638 for (curr = se->interrupts.next; curr != &se->interrupts;
1639 curr = curr->next) {
1640 if (curr->u.i.unique == req->u.i.unique) {
1641 return 1;
1644 return 0;
1647 static void do_interrupt(fuse_req_t req, fuse_ino_t nodeid,
1648 struct fuse_mbuf_iter *iter)
1650 struct fuse_interrupt_in *arg;
1651 struct fuse_session *se = req->se;
1653 (void)nodeid;
1655 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1656 if (!arg) {
1657 fuse_reply_err(req, EINVAL);
1658 return;
1661 fuse_log(FUSE_LOG_DEBUG, "INTERRUPT: %llu\n",
1662 (unsigned long long)arg->unique);
1664 req->u.i.unique = arg->unique;
1666 pthread_mutex_lock(&se->lock);
1667 if (find_interrupted(se, req)) {
1668 destroy_req(req);
1669 } else {
1670 list_add_req(req, &se->interrupts);
1672 pthread_mutex_unlock(&se->lock);
1675 static struct fuse_req *check_interrupt(struct fuse_session *se,
1676 struct fuse_req *req)
1678 struct fuse_req *curr;
1680 for (curr = se->interrupts.next; curr != &se->interrupts;
1681 curr = curr->next) {
1682 if (curr->u.i.unique == req->unique) {
1683 req->interrupted = 1;
1684 list_del_req(curr);
1685 free(curr);
1686 return NULL;
1689 curr = se->interrupts.next;
1690 if (curr != &se->interrupts) {
1691 list_del_req(curr);
1692 list_init_req(curr);
1693 return curr;
1694 } else {
1695 return NULL;
1699 static void do_bmap(fuse_req_t req, fuse_ino_t nodeid,
1700 struct fuse_mbuf_iter *iter)
1702 struct fuse_bmap_in *arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1704 if (!arg) {
1705 fuse_reply_err(req, EINVAL);
1706 return;
1709 if (req->se->op.bmap) {
1710 req->se->op.bmap(req, nodeid, arg->blocksize, arg->block);
1711 } else {
1712 fuse_reply_err(req, ENOSYS);
1716 static void do_ioctl(fuse_req_t req, fuse_ino_t nodeid,
1717 struct fuse_mbuf_iter *iter)
1719 struct fuse_ioctl_in *arg;
1720 unsigned int flags;
1721 void *in_buf = NULL;
1722 struct fuse_file_info fi;
1724 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1725 if (!arg) {
1726 fuse_reply_err(req, EINVAL);
1727 return;
1730 flags = arg->flags;
1731 if (flags & FUSE_IOCTL_DIR && !(req->se->conn.want & FUSE_CAP_IOCTL_DIR)) {
1732 fuse_reply_err(req, ENOTTY);
1733 return;
1736 if (arg->in_size) {
1737 in_buf = fuse_mbuf_iter_advance(iter, arg->in_size);
1738 if (!in_buf) {
1739 fuse_reply_err(req, EINVAL);
1740 return;
1744 memset(&fi, 0, sizeof(fi));
1745 fi.fh = arg->fh;
1747 if (sizeof(void *) == 4 && !(flags & FUSE_IOCTL_32BIT)) {
1748 req->ioctl_64bit = 1;
1751 if (req->se->op.ioctl) {
1752 req->se->op.ioctl(req, nodeid, arg->cmd, (void *)(uintptr_t)arg->arg,
1753 &fi, flags, in_buf, arg->in_size, arg->out_size);
1754 } else {
1755 fuse_reply_err(req, ENOSYS);
1759 void fuse_pollhandle_destroy(struct fuse_pollhandle *ph)
1761 free(ph);
1764 static void do_poll(fuse_req_t req, fuse_ino_t nodeid,
1765 struct fuse_mbuf_iter *iter)
1767 struct fuse_poll_in *arg;
1768 struct fuse_file_info fi;
1770 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1771 if (!arg) {
1772 fuse_reply_err(req, EINVAL);
1773 return;
1776 memset(&fi, 0, sizeof(fi));
1777 fi.fh = arg->fh;
1778 fi.poll_events = arg->events;
1780 if (req->se->op.poll) {
1781 struct fuse_pollhandle *ph = NULL;
1783 if (arg->flags & FUSE_POLL_SCHEDULE_NOTIFY) {
1784 ph = malloc(sizeof(struct fuse_pollhandle));
1785 if (ph == NULL) {
1786 fuse_reply_err(req, ENOMEM);
1787 return;
1789 ph->kh = arg->kh;
1790 ph->se = req->se;
1793 req->se->op.poll(req, nodeid, &fi, ph);
1794 } else {
1795 fuse_reply_err(req, ENOSYS);
1799 static void do_fallocate(fuse_req_t req, fuse_ino_t nodeid,
1800 struct fuse_mbuf_iter *iter)
1802 struct fuse_fallocate_in *arg;
1803 struct fuse_file_info fi;
1805 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1806 if (!arg) {
1807 fuse_reply_err(req, EINVAL);
1808 return;
1811 memset(&fi, 0, sizeof(fi));
1812 fi.fh = arg->fh;
1814 if (req->se->op.fallocate) {
1815 req->se->op.fallocate(req, nodeid, arg->mode, arg->offset, arg->length,
1816 &fi);
1817 } else {
1818 fuse_reply_err(req, ENOSYS);
1822 static void do_copy_file_range(fuse_req_t req, fuse_ino_t nodeid_in,
1823 struct fuse_mbuf_iter *iter)
1825 struct fuse_copy_file_range_in *arg;
1826 struct fuse_file_info fi_in, fi_out;
1828 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1829 if (!arg) {
1830 fuse_reply_err(req, EINVAL);
1831 return;
1834 memset(&fi_in, 0, sizeof(fi_in));
1835 fi_in.fh = arg->fh_in;
1837 memset(&fi_out, 0, sizeof(fi_out));
1838 fi_out.fh = arg->fh_out;
1841 if (req->se->op.copy_file_range) {
1842 req->se->op.copy_file_range(req, nodeid_in, arg->off_in, &fi_in,
1843 arg->nodeid_out, arg->off_out, &fi_out,
1844 arg->len, arg->flags);
1845 } else {
1846 fuse_reply_err(req, ENOSYS);
1850 static void do_lseek(fuse_req_t req, fuse_ino_t nodeid,
1851 struct fuse_mbuf_iter *iter)
1853 struct fuse_lseek_in *arg;
1854 struct fuse_file_info fi;
1856 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1857 if (!arg) {
1858 fuse_reply_err(req, EINVAL);
1859 return;
1861 memset(&fi, 0, sizeof(fi));
1862 fi.fh = arg->fh;
1864 if (req->se->op.lseek) {
1865 req->se->op.lseek(req, nodeid, arg->offset, arg->whence, &fi);
1866 } else {
1867 fuse_reply_err(req, ENOSYS);
1871 static void do_init(fuse_req_t req, fuse_ino_t nodeid,
1872 struct fuse_mbuf_iter *iter)
1874 size_t compat_size = offsetof(struct fuse_init_in, max_readahead);
1875 struct fuse_init_in *arg;
1876 struct fuse_init_out outarg;
1877 struct fuse_session *se = req->se;
1878 size_t bufsize = se->bufsize;
1879 size_t outargsize = sizeof(outarg);
1881 (void)nodeid;
1883 /* First consume the old fields... */
1884 arg = fuse_mbuf_iter_advance(iter, compat_size);
1885 if (!arg) {
1886 fuse_reply_err(req, EINVAL);
1887 return;
1890 /* ...and now consume the new fields. */
1891 if (arg->major == 7 && arg->minor >= 6) {
1892 if (!fuse_mbuf_iter_advance(iter, sizeof(*arg) - compat_size)) {
1893 fuse_reply_err(req, EINVAL);
1894 return;
1898 fuse_log(FUSE_LOG_DEBUG, "INIT: %u.%u\n", arg->major, arg->minor);
1899 if (arg->major == 7 && arg->minor >= 6) {
1900 fuse_log(FUSE_LOG_DEBUG, "flags=0x%08x\n", arg->flags);
1901 fuse_log(FUSE_LOG_DEBUG, "max_readahead=0x%08x\n", arg->max_readahead);
1903 se->conn.proto_major = arg->major;
1904 se->conn.proto_minor = arg->minor;
1905 se->conn.capable = 0;
1906 se->conn.want = 0;
1908 memset(&outarg, 0, sizeof(outarg));
1909 outarg.major = FUSE_KERNEL_VERSION;
1910 outarg.minor = FUSE_KERNEL_MINOR_VERSION;
1912 if (arg->major < 7 || (arg->major == 7 && arg->minor < 31)) {
1913 fuse_log(FUSE_LOG_ERR, "fuse: unsupported protocol version: %u.%u\n",
1914 arg->major, arg->minor);
1915 fuse_reply_err(req, EPROTO);
1916 return;
1919 if (arg->major > 7) {
1920 /* Wait for a second INIT request with a 7.X version */
1921 send_reply_ok(req, &outarg, sizeof(outarg));
1922 return;
1925 if (arg->max_readahead < se->conn.max_readahead) {
1926 se->conn.max_readahead = arg->max_readahead;
1928 if (arg->flags & FUSE_ASYNC_READ) {
1929 se->conn.capable |= FUSE_CAP_ASYNC_READ;
1931 if (arg->flags & FUSE_POSIX_LOCKS) {
1932 se->conn.capable |= FUSE_CAP_POSIX_LOCKS;
1934 if (arg->flags & FUSE_ATOMIC_O_TRUNC) {
1935 se->conn.capable |= FUSE_CAP_ATOMIC_O_TRUNC;
1937 if (arg->flags & FUSE_EXPORT_SUPPORT) {
1938 se->conn.capable |= FUSE_CAP_EXPORT_SUPPORT;
1940 if (arg->flags & FUSE_DONT_MASK) {
1941 se->conn.capable |= FUSE_CAP_DONT_MASK;
1943 if (arg->flags & FUSE_FLOCK_LOCKS) {
1944 se->conn.capable |= FUSE_CAP_FLOCK_LOCKS;
1946 if (arg->flags & FUSE_AUTO_INVAL_DATA) {
1947 se->conn.capable |= FUSE_CAP_AUTO_INVAL_DATA;
1949 if (arg->flags & FUSE_DO_READDIRPLUS) {
1950 se->conn.capable |= FUSE_CAP_READDIRPLUS;
1952 if (arg->flags & FUSE_READDIRPLUS_AUTO) {
1953 se->conn.capable |= FUSE_CAP_READDIRPLUS_AUTO;
1955 if (arg->flags & FUSE_ASYNC_DIO) {
1956 se->conn.capable |= FUSE_CAP_ASYNC_DIO;
1958 if (arg->flags & FUSE_WRITEBACK_CACHE) {
1959 se->conn.capable |= FUSE_CAP_WRITEBACK_CACHE;
1961 if (arg->flags & FUSE_NO_OPEN_SUPPORT) {
1962 se->conn.capable |= FUSE_CAP_NO_OPEN_SUPPORT;
1964 if (arg->flags & FUSE_PARALLEL_DIROPS) {
1965 se->conn.capable |= FUSE_CAP_PARALLEL_DIROPS;
1967 if (arg->flags & FUSE_POSIX_ACL) {
1968 se->conn.capable |= FUSE_CAP_POSIX_ACL;
1970 if (arg->flags & FUSE_HANDLE_KILLPRIV) {
1971 se->conn.capable |= FUSE_CAP_HANDLE_KILLPRIV;
1973 if (arg->flags & FUSE_NO_OPENDIR_SUPPORT) {
1974 se->conn.capable |= FUSE_CAP_NO_OPENDIR_SUPPORT;
1976 if (!(arg->flags & FUSE_MAX_PAGES)) {
1977 size_t max_bufsize = FUSE_DEFAULT_MAX_PAGES_PER_REQ * getpagesize() +
1978 FUSE_BUFFER_HEADER_SIZE;
1979 if (bufsize > max_bufsize) {
1980 bufsize = max_bufsize;
1983 if (arg->flags & FUSE_SUBMOUNTS) {
1984 se->conn.capable |= FUSE_CAP_SUBMOUNTS;
1986 #ifdef HAVE_SPLICE
1987 #ifdef HAVE_VMSPLICE
1988 se->conn.capable |= FUSE_CAP_SPLICE_WRITE | FUSE_CAP_SPLICE_MOVE;
1989 #endif
1990 se->conn.capable |= FUSE_CAP_SPLICE_READ;
1991 #endif
1992 se->conn.capable |= FUSE_CAP_IOCTL_DIR;
1995 * Default settings for modern filesystems.
1997 * Most of these capabilities were disabled by default in
1998 * libfuse2 for backwards compatibility reasons. In libfuse3,
1999 * we can finally enable them by default (as long as they're
2000 * supported by the kernel).
2002 #define LL_SET_DEFAULT(cond, cap) \
2003 if ((cond) && (se->conn.capable & (cap))) \
2004 se->conn.want |= (cap)
2005 LL_SET_DEFAULT(1, FUSE_CAP_ASYNC_READ);
2006 LL_SET_DEFAULT(1, FUSE_CAP_PARALLEL_DIROPS);
2007 LL_SET_DEFAULT(1, FUSE_CAP_AUTO_INVAL_DATA);
2008 LL_SET_DEFAULT(1, FUSE_CAP_HANDLE_KILLPRIV);
2009 LL_SET_DEFAULT(1, FUSE_CAP_ASYNC_DIO);
2010 LL_SET_DEFAULT(1, FUSE_CAP_IOCTL_DIR);
2011 LL_SET_DEFAULT(1, FUSE_CAP_ATOMIC_O_TRUNC);
2012 LL_SET_DEFAULT(se->op.write_buf, FUSE_CAP_SPLICE_READ);
2013 LL_SET_DEFAULT(se->op.getlk && se->op.setlk, FUSE_CAP_POSIX_LOCKS);
2014 LL_SET_DEFAULT(se->op.flock, FUSE_CAP_FLOCK_LOCKS);
2015 LL_SET_DEFAULT(se->op.readdirplus, FUSE_CAP_READDIRPLUS);
2016 LL_SET_DEFAULT(se->op.readdirplus && se->op.readdir,
2017 FUSE_CAP_READDIRPLUS_AUTO);
2018 se->conn.time_gran = 1;
2020 if (bufsize < FUSE_MIN_READ_BUFFER) {
2021 fuse_log(FUSE_LOG_ERR, "fuse: warning: buffer size too small: %zu\n",
2022 bufsize);
2023 bufsize = FUSE_MIN_READ_BUFFER;
2025 se->bufsize = bufsize;
2027 if (se->conn.max_write > bufsize - FUSE_BUFFER_HEADER_SIZE) {
2028 se->conn.max_write = bufsize - FUSE_BUFFER_HEADER_SIZE;
2031 se->got_init = 1;
2032 se->got_destroy = 0;
2033 if (se->op.init) {
2034 se->op.init(se->userdata, &se->conn);
2037 if (se->conn.want & (~se->conn.capable)) {
2038 fuse_log(FUSE_LOG_ERR,
2039 "fuse: error: filesystem requested capabilities "
2040 "0x%x that are not supported by kernel, aborting.\n",
2041 se->conn.want & (~se->conn.capable));
2042 fuse_reply_err(req, EPROTO);
2043 se->error = -EPROTO;
2044 fuse_session_exit(se);
2045 return;
2048 if (se->conn.max_write < bufsize - FUSE_BUFFER_HEADER_SIZE) {
2049 se->bufsize = se->conn.max_write + FUSE_BUFFER_HEADER_SIZE;
2051 if (arg->flags & FUSE_MAX_PAGES) {
2052 outarg.flags |= FUSE_MAX_PAGES;
2053 outarg.max_pages = (se->conn.max_write - 1) / getpagesize() + 1;
2057 * Always enable big writes, this is superseded
2058 * by the max_write option
2060 outarg.flags |= FUSE_BIG_WRITES;
2062 if (se->conn.want & FUSE_CAP_ASYNC_READ) {
2063 outarg.flags |= FUSE_ASYNC_READ;
2065 if (se->conn.want & FUSE_CAP_PARALLEL_DIROPS) {
2066 outarg.flags |= FUSE_PARALLEL_DIROPS;
2068 if (se->conn.want & FUSE_CAP_POSIX_LOCKS) {
2069 outarg.flags |= FUSE_POSIX_LOCKS;
2071 if (se->conn.want & FUSE_CAP_ATOMIC_O_TRUNC) {
2072 outarg.flags |= FUSE_ATOMIC_O_TRUNC;
2074 if (se->conn.want & FUSE_CAP_EXPORT_SUPPORT) {
2075 outarg.flags |= FUSE_EXPORT_SUPPORT;
2077 if (se->conn.want & FUSE_CAP_DONT_MASK) {
2078 outarg.flags |= FUSE_DONT_MASK;
2080 if (se->conn.want & FUSE_CAP_FLOCK_LOCKS) {
2081 outarg.flags |= FUSE_FLOCK_LOCKS;
2083 if (se->conn.want & FUSE_CAP_AUTO_INVAL_DATA) {
2084 outarg.flags |= FUSE_AUTO_INVAL_DATA;
2086 if (se->conn.want & FUSE_CAP_READDIRPLUS) {
2087 outarg.flags |= FUSE_DO_READDIRPLUS;
2089 if (se->conn.want & FUSE_CAP_READDIRPLUS_AUTO) {
2090 outarg.flags |= FUSE_READDIRPLUS_AUTO;
2092 if (se->conn.want & FUSE_CAP_ASYNC_DIO) {
2093 outarg.flags |= FUSE_ASYNC_DIO;
2095 if (se->conn.want & FUSE_CAP_WRITEBACK_CACHE) {
2096 outarg.flags |= FUSE_WRITEBACK_CACHE;
2098 if (se->conn.want & FUSE_CAP_POSIX_ACL) {
2099 outarg.flags |= FUSE_POSIX_ACL;
2101 outarg.max_readahead = se->conn.max_readahead;
2102 outarg.max_write = se->conn.max_write;
2103 if (se->conn.max_background >= (1 << 16)) {
2104 se->conn.max_background = (1 << 16) - 1;
2106 if (se->conn.congestion_threshold > se->conn.max_background) {
2107 se->conn.congestion_threshold = se->conn.max_background;
2109 if (!se->conn.congestion_threshold) {
2110 se->conn.congestion_threshold = se->conn.max_background * 3 / 4;
2113 outarg.max_background = se->conn.max_background;
2114 outarg.congestion_threshold = se->conn.congestion_threshold;
2115 outarg.time_gran = se->conn.time_gran;
2117 fuse_log(FUSE_LOG_DEBUG, " INIT: %u.%u\n", outarg.major, outarg.minor);
2118 fuse_log(FUSE_LOG_DEBUG, " flags=0x%08x\n", outarg.flags);
2119 fuse_log(FUSE_LOG_DEBUG, " max_readahead=0x%08x\n", outarg.max_readahead);
2120 fuse_log(FUSE_LOG_DEBUG, " max_write=0x%08x\n", outarg.max_write);
2121 fuse_log(FUSE_LOG_DEBUG, " max_background=%i\n", outarg.max_background);
2122 fuse_log(FUSE_LOG_DEBUG, " congestion_threshold=%i\n",
2123 outarg.congestion_threshold);
2124 fuse_log(FUSE_LOG_DEBUG, " time_gran=%u\n", outarg.time_gran);
2126 send_reply_ok(req, &outarg, outargsize);
2129 static void do_destroy(fuse_req_t req, fuse_ino_t nodeid,
2130 struct fuse_mbuf_iter *iter)
2132 struct fuse_session *se = req->se;
2134 (void)nodeid;
2135 (void)iter;
2137 se->got_destroy = 1;
2138 se->got_init = 0;
2139 if (se->op.destroy) {
2140 se->op.destroy(se->userdata);
2143 send_reply_ok(req, NULL, 0);
2146 static int send_notify_iov(struct fuse_session *se, int notify_code,
2147 struct iovec *iov, int count)
2149 struct fuse_out_header out = {
2150 .error = notify_code,
2153 if (!se->got_init) {
2154 return -ENOTCONN;
2157 iov[0].iov_base = &out;
2158 iov[0].iov_len = sizeof(struct fuse_out_header);
2160 return fuse_send_msg(se, NULL, iov, count);
2163 int fuse_lowlevel_notify_poll(struct fuse_pollhandle *ph)
2165 if (ph != NULL) {
2166 struct fuse_notify_poll_wakeup_out outarg = {
2167 .kh = ph->kh,
2169 struct iovec iov[2];
2171 iov[1].iov_base = &outarg;
2172 iov[1].iov_len = sizeof(outarg);
2174 return send_notify_iov(ph->se, FUSE_NOTIFY_POLL, iov, 2);
2175 } else {
2176 return 0;
2180 int fuse_lowlevel_notify_inval_inode(struct fuse_session *se, fuse_ino_t ino,
2181 off_t off, off_t len)
2183 struct fuse_notify_inval_inode_out outarg = {
2184 .ino = ino,
2185 .off = off,
2186 .len = len,
2188 struct iovec iov[2];
2190 if (!se) {
2191 return -EINVAL;
2194 iov[1].iov_base = &outarg;
2195 iov[1].iov_len = sizeof(outarg);
2197 return send_notify_iov(se, FUSE_NOTIFY_INVAL_INODE, iov, 2);
2200 int fuse_lowlevel_notify_inval_entry(struct fuse_session *se, fuse_ino_t parent,
2201 const char *name, size_t namelen)
2203 struct fuse_notify_inval_entry_out outarg = {
2204 .parent = parent,
2205 .namelen = namelen,
2207 struct iovec iov[3];
2209 if (!se) {
2210 return -EINVAL;
2213 iov[1].iov_base = &outarg;
2214 iov[1].iov_len = sizeof(outarg);
2215 iov[2].iov_base = (void *)name;
2216 iov[2].iov_len = namelen + 1;
2218 return send_notify_iov(se, FUSE_NOTIFY_INVAL_ENTRY, iov, 3);
2221 int fuse_lowlevel_notify_delete(struct fuse_session *se, fuse_ino_t parent,
2222 fuse_ino_t child, const char *name,
2223 size_t namelen)
2225 struct fuse_notify_delete_out outarg = {
2226 .parent = parent,
2227 .child = child,
2228 .namelen = namelen,
2230 struct iovec iov[3];
2232 if (!se) {
2233 return -EINVAL;
2236 iov[1].iov_base = &outarg;
2237 iov[1].iov_len = sizeof(outarg);
2238 iov[2].iov_base = (void *)name;
2239 iov[2].iov_len = namelen + 1;
2241 return send_notify_iov(se, FUSE_NOTIFY_DELETE, iov, 3);
2244 int fuse_lowlevel_notify_store(struct fuse_session *se, fuse_ino_t ino,
2245 off_t offset, struct fuse_bufvec *bufv)
2247 struct fuse_out_header out = {
2248 .error = FUSE_NOTIFY_STORE,
2250 struct fuse_notify_store_out outarg = {
2251 .nodeid = ino,
2252 .offset = offset,
2253 .size = fuse_buf_size(bufv),
2255 struct iovec iov[3];
2256 int res;
2258 if (!se) {
2259 return -EINVAL;
2262 iov[0].iov_base = &out;
2263 iov[0].iov_len = sizeof(out);
2264 iov[1].iov_base = &outarg;
2265 iov[1].iov_len = sizeof(outarg);
2267 res = fuse_send_data_iov(se, NULL, iov, 2, bufv);
2268 if (res > 0) {
2269 res = -res;
2272 return res;
2275 void *fuse_req_userdata(fuse_req_t req)
2277 return req->se->userdata;
2280 const struct fuse_ctx *fuse_req_ctx(fuse_req_t req)
2282 return &req->ctx;
2285 void fuse_req_interrupt_func(fuse_req_t req, fuse_interrupt_func_t func,
2286 void *data)
2288 pthread_mutex_lock(&req->lock);
2289 pthread_mutex_lock(&req->se->lock);
2290 req->u.ni.func = func;
2291 req->u.ni.data = data;
2292 pthread_mutex_unlock(&req->se->lock);
2293 if (req->interrupted && func) {
2294 func(req, data);
2296 pthread_mutex_unlock(&req->lock);
2299 int fuse_req_interrupted(fuse_req_t req)
2301 int interrupted;
2303 pthread_mutex_lock(&req->se->lock);
2304 interrupted = req->interrupted;
2305 pthread_mutex_unlock(&req->se->lock);
2307 return interrupted;
2310 static struct {
2311 void (*func)(fuse_req_t, fuse_ino_t, struct fuse_mbuf_iter *);
2312 const char *name;
2313 } fuse_ll_ops[] = {
2314 [FUSE_LOOKUP] = { do_lookup, "LOOKUP" },
2315 [FUSE_FORGET] = { do_forget, "FORGET" },
2316 [FUSE_GETATTR] = { do_getattr, "GETATTR" },
2317 [FUSE_SETATTR] = { do_setattr, "SETATTR" },
2318 [FUSE_READLINK] = { do_readlink, "READLINK" },
2319 [FUSE_SYMLINK] = { do_symlink, "SYMLINK" },
2320 [FUSE_MKNOD] = { do_mknod, "MKNOD" },
2321 [FUSE_MKDIR] = { do_mkdir, "MKDIR" },
2322 [FUSE_UNLINK] = { do_unlink, "UNLINK" },
2323 [FUSE_RMDIR] = { do_rmdir, "RMDIR" },
2324 [FUSE_RENAME] = { do_rename, "RENAME" },
2325 [FUSE_LINK] = { do_link, "LINK" },
2326 [FUSE_OPEN] = { do_open, "OPEN" },
2327 [FUSE_READ] = { do_read, "READ" },
2328 [FUSE_WRITE] = { do_write, "WRITE" },
2329 [FUSE_STATFS] = { do_statfs, "STATFS" },
2330 [FUSE_RELEASE] = { do_release, "RELEASE" },
2331 [FUSE_FSYNC] = { do_fsync, "FSYNC" },
2332 [FUSE_SETXATTR] = { do_setxattr, "SETXATTR" },
2333 [FUSE_GETXATTR] = { do_getxattr, "GETXATTR" },
2334 [FUSE_LISTXATTR] = { do_listxattr, "LISTXATTR" },
2335 [FUSE_REMOVEXATTR] = { do_removexattr, "REMOVEXATTR" },
2336 [FUSE_FLUSH] = { do_flush, "FLUSH" },
2337 [FUSE_INIT] = { do_init, "INIT" },
2338 [FUSE_OPENDIR] = { do_opendir, "OPENDIR" },
2339 [FUSE_READDIR] = { do_readdir, "READDIR" },
2340 [FUSE_RELEASEDIR] = { do_releasedir, "RELEASEDIR" },
2341 [FUSE_FSYNCDIR] = { do_fsyncdir, "FSYNCDIR" },
2342 [FUSE_GETLK] = { do_getlk, "GETLK" },
2343 [FUSE_SETLK] = { do_setlk, "SETLK" },
2344 [FUSE_SETLKW] = { do_setlkw, "SETLKW" },
2345 [FUSE_ACCESS] = { do_access, "ACCESS" },
2346 [FUSE_CREATE] = { do_create, "CREATE" },
2347 [FUSE_INTERRUPT] = { do_interrupt, "INTERRUPT" },
2348 [FUSE_BMAP] = { do_bmap, "BMAP" },
2349 [FUSE_IOCTL] = { do_ioctl, "IOCTL" },
2350 [FUSE_POLL] = { do_poll, "POLL" },
2351 [FUSE_FALLOCATE] = { do_fallocate, "FALLOCATE" },
2352 [FUSE_DESTROY] = { do_destroy, "DESTROY" },
2353 [FUSE_NOTIFY_REPLY] = { NULL, "NOTIFY_REPLY" },
2354 [FUSE_BATCH_FORGET] = { do_batch_forget, "BATCH_FORGET" },
2355 [FUSE_READDIRPLUS] = { do_readdirplus, "READDIRPLUS" },
2356 [FUSE_RENAME2] = { do_rename2, "RENAME2" },
2357 [FUSE_COPY_FILE_RANGE] = { do_copy_file_range, "COPY_FILE_RANGE" },
2358 [FUSE_LSEEK] = { do_lseek, "LSEEK" },
2361 #define FUSE_MAXOP (sizeof(fuse_ll_ops) / sizeof(fuse_ll_ops[0]))
2363 static const char *opname(enum fuse_opcode opcode)
2365 if (opcode >= FUSE_MAXOP || !fuse_ll_ops[opcode].name) {
2366 return "???";
2367 } else {
2368 return fuse_ll_ops[opcode].name;
2372 void fuse_session_process_buf(struct fuse_session *se,
2373 const struct fuse_buf *buf)
2375 struct fuse_bufvec bufv = { .buf[0] = *buf, .count = 1 };
2376 fuse_session_process_buf_int(se, &bufv, NULL);
2380 * Restriction:
2381 * bufv is normally a single entry buffer, except for a write
2382 * where (if it's in memory) then the bufv may be multiple entries,
2383 * where the first entry contains all headers and subsequent entries
2384 * contain data
2385 * bufv shall not use any offsets etc to make the data anything
2386 * other than contiguous starting from 0.
2388 void fuse_session_process_buf_int(struct fuse_session *se,
2389 struct fuse_bufvec *bufv,
2390 struct fuse_chan *ch)
2392 const struct fuse_buf *buf = bufv->buf;
2393 struct fuse_mbuf_iter iter = FUSE_MBUF_ITER_INIT(buf);
2394 struct fuse_in_header *in;
2395 struct fuse_req *req;
2396 int err;
2398 /* The first buffer must be a memory buffer */
2399 assert(!(buf->flags & FUSE_BUF_IS_FD));
2401 in = fuse_mbuf_iter_advance(&iter, sizeof(*in));
2402 assert(in); /* caller guarantees the input buffer is large enough */
2404 fuse_log(
2405 FUSE_LOG_DEBUG,
2406 "unique: %llu, opcode: %s (%i), nodeid: %llu, insize: %zu, pid: %u\n",
2407 (unsigned long long)in->unique, opname((enum fuse_opcode)in->opcode),
2408 in->opcode, (unsigned long long)in->nodeid, buf->size, in->pid);
2410 req = fuse_ll_alloc_req(se);
2411 if (req == NULL) {
2412 struct fuse_out_header out = {
2413 .unique = in->unique,
2414 .error = -ENOMEM,
2416 struct iovec iov = {
2417 .iov_base = &out,
2418 .iov_len = sizeof(struct fuse_out_header),
2421 fuse_send_msg(se, ch, &iov, 1);
2422 return;
2425 req->unique = in->unique;
2426 req->ctx.uid = in->uid;
2427 req->ctx.gid = in->gid;
2428 req->ctx.pid = in->pid;
2429 req->ch = ch;
2432 * INIT and DESTROY requests are serialized, all other request types
2433 * run in parallel. This prevents races between FUSE_INIT and ordinary
2434 * requests, FUSE_INIT and FUSE_INIT, FUSE_INIT and FUSE_DESTROY, and
2435 * FUSE_DESTROY and FUSE_DESTROY.
2437 if (in->opcode == FUSE_INIT || in->opcode == CUSE_INIT ||
2438 in->opcode == FUSE_DESTROY) {
2439 pthread_rwlock_wrlock(&se->init_rwlock);
2440 } else {
2441 pthread_rwlock_rdlock(&se->init_rwlock);
2444 err = EIO;
2445 if (!se->got_init) {
2446 enum fuse_opcode expected;
2448 expected = se->cuse_data ? CUSE_INIT : FUSE_INIT;
2449 if (in->opcode != expected) {
2450 goto reply_err;
2452 } else if (in->opcode == FUSE_INIT || in->opcode == CUSE_INIT) {
2453 if (fuse_lowlevel_is_virtio(se)) {
2455 * TODO: This is after a hard reboot typically, we need to do
2456 * a destroy, but we can't reply to this request yet so
2457 * we can't use do_destroy
2459 fuse_log(FUSE_LOG_DEBUG, "%s: reinit\n", __func__);
2460 se->got_destroy = 1;
2461 se->got_init = 0;
2462 if (se->op.destroy) {
2463 se->op.destroy(se->userdata);
2465 } else {
2466 goto reply_err;
2470 err = EACCES;
2471 /* Implement -o allow_root */
2472 if (se->deny_others && in->uid != se->owner && in->uid != 0 &&
2473 in->opcode != FUSE_INIT && in->opcode != FUSE_READ &&
2474 in->opcode != FUSE_WRITE && in->opcode != FUSE_FSYNC &&
2475 in->opcode != FUSE_RELEASE && in->opcode != FUSE_READDIR &&
2476 in->opcode != FUSE_FSYNCDIR && in->opcode != FUSE_RELEASEDIR &&
2477 in->opcode != FUSE_NOTIFY_REPLY && in->opcode != FUSE_READDIRPLUS) {
2478 goto reply_err;
2481 err = ENOSYS;
2482 if (in->opcode >= FUSE_MAXOP || !fuse_ll_ops[in->opcode].func) {
2483 goto reply_err;
2485 if (in->opcode != FUSE_INTERRUPT) {
2486 struct fuse_req *intr;
2487 pthread_mutex_lock(&se->lock);
2488 intr = check_interrupt(se, req);
2489 list_add_req(req, &se->list);
2490 pthread_mutex_unlock(&se->lock);
2491 if (intr) {
2492 fuse_reply_err(intr, EAGAIN);
2496 if (in->opcode == FUSE_WRITE && se->op.write_buf) {
2497 do_write_buf(req, in->nodeid, &iter, bufv);
2498 } else {
2499 fuse_ll_ops[in->opcode].func(req, in->nodeid, &iter);
2502 pthread_rwlock_unlock(&se->init_rwlock);
2503 return;
2505 reply_err:
2506 fuse_reply_err(req, err);
2507 pthread_rwlock_unlock(&se->init_rwlock);
2510 #define LL_OPTION(n, o, v) \
2512 n, offsetof(struct fuse_session, o), v \
2515 static const struct fuse_opt fuse_ll_opts[] = {
2516 LL_OPTION("debug", debug, 1),
2517 LL_OPTION("-d", debug, 1),
2518 LL_OPTION("--debug", debug, 1),
2519 LL_OPTION("allow_root", deny_others, 1),
2520 LL_OPTION("--socket-path=%s", vu_socket_path, 0),
2521 LL_OPTION("--socket-group=%s", vu_socket_group, 0),
2522 LL_OPTION("--fd=%d", vu_listen_fd, 0),
2523 LL_OPTION("--thread-pool-size=%d", thread_pool_size, 0),
2524 FUSE_OPT_END
2527 void fuse_lowlevel_version(void)
2529 printf("using FUSE kernel interface version %i.%i\n", FUSE_KERNEL_VERSION,
2530 FUSE_KERNEL_MINOR_VERSION);
2533 void fuse_lowlevel_help(void)
2536 * These are not all options, but the ones that are
2537 * potentially of interest to an end-user
2539 printf(
2540 " -o allow_root allow access by root\n"
2541 " --socket-path=PATH path for the vhost-user socket\n"
2542 " --fd=FDNUM fd number of vhost-user socket\n"
2543 " --thread-pool-size=NUM thread pool size limit (default %d)\n",
2544 THREAD_POOL_SIZE);
2547 void fuse_session_destroy(struct fuse_session *se)
2549 if (se->got_init && !se->got_destroy) {
2550 if (se->op.destroy) {
2551 se->op.destroy(se->userdata);
2554 pthread_rwlock_destroy(&se->init_rwlock);
2555 pthread_mutex_destroy(&se->lock);
2556 free(se->cuse_data);
2557 if (se->fd != -1) {
2558 close(se->fd);
2561 if (fuse_lowlevel_is_virtio(se)) {
2562 virtio_session_close(se);
2565 free(se->vu_socket_path);
2566 se->vu_socket_path = NULL;
2568 free(se);
2572 struct fuse_session *fuse_session_new(struct fuse_args *args,
2573 const struct fuse_lowlevel_ops *op,
2574 size_t op_size, void *userdata)
2576 struct fuse_session *se;
2578 if (sizeof(struct fuse_lowlevel_ops) < op_size) {
2579 fuse_log(
2580 FUSE_LOG_ERR,
2581 "fuse: warning: library too old, some operations may not work\n");
2582 op_size = sizeof(struct fuse_lowlevel_ops);
2585 if (args->argc == 0) {
2586 fuse_log(FUSE_LOG_ERR,
2587 "fuse: empty argv passed to fuse_session_new().\n");
2588 return NULL;
2591 se = (struct fuse_session *)calloc(1, sizeof(struct fuse_session));
2592 if (se == NULL) {
2593 fuse_log(FUSE_LOG_ERR, "fuse: failed to allocate fuse object\n");
2594 goto out1;
2596 se->fd = -1;
2597 se->vu_listen_fd = -1;
2598 se->thread_pool_size = THREAD_POOL_SIZE;
2599 se->conn.max_write = UINT_MAX;
2600 se->conn.max_readahead = UINT_MAX;
2602 /* Parse options */
2603 if (fuse_opt_parse(args, se, fuse_ll_opts, NULL) == -1) {
2604 goto out2;
2606 if (args->argc == 1 && args->argv[0][0] == '-') {
2607 fuse_log(FUSE_LOG_ERR,
2608 "fuse: warning: argv[0] looks like an option, but "
2609 "will be ignored\n");
2610 } else if (args->argc != 1) {
2611 int i;
2612 fuse_log(FUSE_LOG_ERR, "fuse: unknown option(s): `");
2613 for (i = 1; i < args->argc - 1; i++) {
2614 fuse_log(FUSE_LOG_ERR, "%s ", args->argv[i]);
2616 fuse_log(FUSE_LOG_ERR, "%s'\n", args->argv[i]);
2617 goto out4;
2620 if (!se->vu_socket_path && se->vu_listen_fd < 0) {
2621 fuse_log(FUSE_LOG_ERR, "fuse: missing --socket-path or --fd option\n");
2622 goto out4;
2624 if (se->vu_socket_path && se->vu_listen_fd >= 0) {
2625 fuse_log(FUSE_LOG_ERR,
2626 "fuse: --socket-path and --fd cannot be given together\n");
2627 goto out4;
2629 if (se->vu_socket_group && !se->vu_socket_path) {
2630 fuse_log(FUSE_LOG_ERR,
2631 "fuse: --socket-group can only be used with --socket-path\n");
2632 goto out4;
2635 se->bufsize = FUSE_MAX_MAX_PAGES * getpagesize() + FUSE_BUFFER_HEADER_SIZE;
2637 list_init_req(&se->list);
2638 list_init_req(&se->interrupts);
2639 fuse_mutex_init(&se->lock);
2640 pthread_rwlock_init(&se->init_rwlock, NULL);
2642 memcpy(&se->op, op, op_size);
2643 se->owner = getuid();
2644 se->userdata = userdata;
2646 return se;
2648 out4:
2649 fuse_opt_free_args(args);
2650 out2:
2651 free(se);
2652 out1:
2653 return NULL;
2656 int fuse_session_mount(struct fuse_session *se)
2658 return virtio_session_mount(se);
2661 int fuse_session_fd(struct fuse_session *se)
2663 return se->fd;
2666 void fuse_session_unmount(struct fuse_session *se)
2670 int fuse_lowlevel_is_virtio(struct fuse_session *se)
2672 return !!se->virtio_dev;
2675 void fuse_session_exit(struct fuse_session *se)
2677 se->exited = 1;
2680 void fuse_session_reset(struct fuse_session *se)
2682 se->exited = 0;
2683 se->error = 0;
2686 int fuse_session_exited(struct fuse_session *se)
2688 return se->exited;