virtiofsd: Add options for virtio
[qemu/kevin.git] / tools / virtiofsd / fuse_lowlevel.c
blob17e8718283f0c504abc865e7fa43f3ea72e84731
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"
18 #include <assert.h>
19 #include <errno.h>
20 #include <limits.h>
21 #include <stddef.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sys/file.h>
26 #include <unistd.h>
29 #define PARAM(inarg) (((char *)(inarg)) + sizeof(*(inarg)))
30 #define OFFSET_MAX 0x7fffffffffffffffLL
32 struct fuse_pollhandle {
33 uint64_t kh;
34 struct fuse_session *se;
37 static size_t pagesize;
39 static __attribute__((constructor)) void fuse_ll_init_pagesize(void)
41 pagesize = getpagesize();
44 static void convert_stat(const struct stat *stbuf, struct fuse_attr *attr)
46 attr->ino = stbuf->st_ino;
47 attr->mode = stbuf->st_mode;
48 attr->nlink = stbuf->st_nlink;
49 attr->uid = stbuf->st_uid;
50 attr->gid = stbuf->st_gid;
51 attr->rdev = stbuf->st_rdev;
52 attr->size = stbuf->st_size;
53 attr->blksize = stbuf->st_blksize;
54 attr->blocks = stbuf->st_blocks;
55 attr->atime = stbuf->st_atime;
56 attr->mtime = stbuf->st_mtime;
57 attr->ctime = stbuf->st_ctime;
58 attr->atimensec = ST_ATIM_NSEC(stbuf);
59 attr->mtimensec = ST_MTIM_NSEC(stbuf);
60 attr->ctimensec = ST_CTIM_NSEC(stbuf);
63 static void convert_attr(const struct fuse_setattr_in *attr, struct stat *stbuf)
65 stbuf->st_mode = attr->mode;
66 stbuf->st_uid = attr->uid;
67 stbuf->st_gid = attr->gid;
68 stbuf->st_size = attr->size;
69 stbuf->st_atime = attr->atime;
70 stbuf->st_mtime = attr->mtime;
71 stbuf->st_ctime = attr->ctime;
72 ST_ATIM_NSEC_SET(stbuf, attr->atimensec);
73 ST_MTIM_NSEC_SET(stbuf, attr->mtimensec);
74 ST_CTIM_NSEC_SET(stbuf, attr->ctimensec);
77 static size_t iov_length(const struct iovec *iov, size_t count)
79 size_t seg;
80 size_t ret = 0;
82 for (seg = 0; seg < count; seg++) {
83 ret += iov[seg].iov_len;
85 return ret;
88 static void list_init_req(struct fuse_req *req)
90 req->next = req;
91 req->prev = req;
94 static void list_del_req(struct fuse_req *req)
96 struct fuse_req *prev = req->prev;
97 struct fuse_req *next = req->next;
98 prev->next = next;
99 next->prev = prev;
102 static void list_add_req(struct fuse_req *req, struct fuse_req *next)
104 struct fuse_req *prev = next->prev;
105 req->next = next;
106 req->prev = prev;
107 prev->next = req;
108 next->prev = req;
111 static void destroy_req(fuse_req_t req)
113 pthread_mutex_destroy(&req->lock);
114 free(req);
117 void fuse_free_req(fuse_req_t req)
119 int ctr;
120 struct fuse_session *se = req->se;
122 pthread_mutex_lock(&se->lock);
123 req->u.ni.func = NULL;
124 req->u.ni.data = NULL;
125 list_del_req(req);
126 ctr = --req->ctr;
127 req->ch = NULL;
128 pthread_mutex_unlock(&se->lock);
129 if (!ctr) {
130 destroy_req(req);
134 static struct fuse_req *fuse_ll_alloc_req(struct fuse_session *se)
136 struct fuse_req *req;
138 req = (struct fuse_req *)calloc(1, sizeof(struct fuse_req));
139 if (req == NULL) {
140 fuse_log(FUSE_LOG_ERR, "fuse: failed to allocate request\n");
141 } else {
142 req->se = se;
143 req->ctr = 1;
144 list_init_req(req);
145 fuse_mutex_init(&req->lock);
148 return req;
151 /* Send data. If *ch* is NULL, send via session master fd */
152 static int fuse_send_msg(struct fuse_session *se, struct fuse_chan *ch,
153 struct iovec *iov, int count)
155 struct fuse_out_header *out = iov[0].iov_base;
157 out->len = iov_length(iov, count);
158 if (se->debug) {
159 if (out->unique == 0) {
160 fuse_log(FUSE_LOG_DEBUG, "NOTIFY: code=%d length=%u\n", out->error,
161 out->len);
162 } else if (out->error) {
163 fuse_log(FUSE_LOG_DEBUG,
164 " unique: %llu, error: %i (%s), outsize: %i\n",
165 (unsigned long long)out->unique, out->error,
166 strerror(-out->error), out->len);
167 } else {
168 fuse_log(FUSE_LOG_DEBUG, " unique: %llu, success, outsize: %i\n",
169 (unsigned long long)out->unique, out->len);
173 abort(); /* virtio should have taken it before here */
174 return 0;
178 int fuse_send_reply_iov_nofree(fuse_req_t req, int error, struct iovec *iov,
179 int count)
181 struct fuse_out_header out;
183 if (error <= -1000 || error > 0) {
184 fuse_log(FUSE_LOG_ERR, "fuse: bad error value: %i\n", error);
185 error = -ERANGE;
188 out.unique = req->unique;
189 out.error = error;
191 iov[0].iov_base = &out;
192 iov[0].iov_len = sizeof(struct fuse_out_header);
194 return fuse_send_msg(req->se, req->ch, iov, count);
197 static int send_reply_iov(fuse_req_t req, int error, struct iovec *iov,
198 int count)
200 int res;
202 res = fuse_send_reply_iov_nofree(req, error, iov, count);
203 fuse_free_req(req);
204 return res;
207 static int send_reply(fuse_req_t req, int error, const void *arg,
208 size_t argsize)
210 struct iovec iov[2];
211 int count = 1;
212 if (argsize) {
213 iov[1].iov_base = (void *)arg;
214 iov[1].iov_len = argsize;
215 count++;
217 return send_reply_iov(req, error, iov, count);
220 int fuse_reply_iov(fuse_req_t req, const struct iovec *iov, int count)
222 int res;
223 struct iovec *padded_iov;
225 padded_iov = malloc((count + 1) * sizeof(struct iovec));
226 if (padded_iov == NULL) {
227 return fuse_reply_err(req, ENOMEM);
230 memcpy(padded_iov + 1, iov, count * sizeof(struct iovec));
231 count++;
233 res = send_reply_iov(req, 0, padded_iov, count);
234 free(padded_iov);
236 return res;
241 * 'buf` is allowed to be empty so that the proper size may be
242 * allocated by the caller
244 size_t fuse_add_direntry(fuse_req_t req, char *buf, size_t bufsize,
245 const char *name, const struct stat *stbuf, off_t off)
247 (void)req;
248 size_t namelen;
249 size_t entlen;
250 size_t entlen_padded;
251 struct fuse_dirent *dirent;
253 namelen = strlen(name);
254 entlen = FUSE_NAME_OFFSET + namelen;
255 entlen_padded = FUSE_DIRENT_ALIGN(entlen);
257 if ((buf == NULL) || (entlen_padded > bufsize)) {
258 return entlen_padded;
261 dirent = (struct fuse_dirent *)buf;
262 dirent->ino = stbuf->st_ino;
263 dirent->off = off;
264 dirent->namelen = namelen;
265 dirent->type = (stbuf->st_mode & S_IFMT) >> 12;
266 memcpy(dirent->name, name, namelen);
267 memset(dirent->name + namelen, 0, entlen_padded - entlen);
269 return entlen_padded;
272 static void convert_statfs(const struct statvfs *stbuf,
273 struct fuse_kstatfs *kstatfs)
275 kstatfs->bsize = stbuf->f_bsize;
276 kstatfs->frsize = stbuf->f_frsize;
277 kstatfs->blocks = stbuf->f_blocks;
278 kstatfs->bfree = stbuf->f_bfree;
279 kstatfs->bavail = stbuf->f_bavail;
280 kstatfs->files = stbuf->f_files;
281 kstatfs->ffree = stbuf->f_ffree;
282 kstatfs->namelen = stbuf->f_namemax;
285 static int send_reply_ok(fuse_req_t req, const void *arg, size_t argsize)
287 return send_reply(req, 0, arg, argsize);
290 int fuse_reply_err(fuse_req_t req, int err)
292 return send_reply(req, -err, NULL, 0);
295 void fuse_reply_none(fuse_req_t req)
297 fuse_free_req(req);
300 static unsigned long calc_timeout_sec(double t)
302 if (t > (double)ULONG_MAX) {
303 return ULONG_MAX;
304 } else if (t < 0.0) {
305 return 0;
306 } else {
307 return (unsigned long)t;
311 static unsigned int calc_timeout_nsec(double t)
313 double f = t - (double)calc_timeout_sec(t);
314 if (f < 0.0) {
315 return 0;
316 } else if (f >= 0.999999999) {
317 return 999999999;
318 } else {
319 return (unsigned int)(f * 1.0e9);
323 static void fill_entry(struct fuse_entry_out *arg,
324 const struct fuse_entry_param *e)
326 arg->nodeid = e->ino;
327 arg->generation = e->generation;
328 arg->entry_valid = calc_timeout_sec(e->entry_timeout);
329 arg->entry_valid_nsec = calc_timeout_nsec(e->entry_timeout);
330 arg->attr_valid = calc_timeout_sec(e->attr_timeout);
331 arg->attr_valid_nsec = calc_timeout_nsec(e->attr_timeout);
332 convert_stat(&e->attr, &arg->attr);
336 * `buf` is allowed to be empty so that the proper size may be
337 * allocated by the caller
339 size_t fuse_add_direntry_plus(fuse_req_t req, char *buf, size_t bufsize,
340 const char *name,
341 const struct fuse_entry_param *e, off_t off)
343 (void)req;
344 size_t namelen;
345 size_t entlen;
346 size_t entlen_padded;
348 namelen = strlen(name);
349 entlen = FUSE_NAME_OFFSET_DIRENTPLUS + namelen;
350 entlen_padded = FUSE_DIRENT_ALIGN(entlen);
351 if ((buf == NULL) || (entlen_padded > bufsize)) {
352 return entlen_padded;
355 struct fuse_direntplus *dp = (struct fuse_direntplus *)buf;
356 memset(&dp->entry_out, 0, sizeof(dp->entry_out));
357 fill_entry(&dp->entry_out, e);
359 struct fuse_dirent *dirent = &dp->dirent;
360 dirent->ino = e->attr.st_ino;
361 dirent->off = off;
362 dirent->namelen = namelen;
363 dirent->type = (e->attr.st_mode & S_IFMT) >> 12;
364 memcpy(dirent->name, name, namelen);
365 memset(dirent->name + namelen, 0, entlen_padded - entlen);
367 return entlen_padded;
370 static void fill_open(struct fuse_open_out *arg, const struct fuse_file_info *f)
372 arg->fh = f->fh;
373 if (f->direct_io) {
374 arg->open_flags |= FOPEN_DIRECT_IO;
376 if (f->keep_cache) {
377 arg->open_flags |= FOPEN_KEEP_CACHE;
379 if (f->cache_readdir) {
380 arg->open_flags |= FOPEN_CACHE_DIR;
382 if (f->nonseekable) {
383 arg->open_flags |= FOPEN_NONSEEKABLE;
387 int fuse_reply_entry(fuse_req_t req, const struct fuse_entry_param *e)
389 struct fuse_entry_out arg;
390 size_t size = sizeof(arg);
392 memset(&arg, 0, sizeof(arg));
393 fill_entry(&arg, e);
394 return send_reply_ok(req, &arg, size);
397 int fuse_reply_create(fuse_req_t req, const struct fuse_entry_param *e,
398 const struct fuse_file_info *f)
400 char buf[sizeof(struct fuse_entry_out) + sizeof(struct fuse_open_out)];
401 size_t entrysize = sizeof(struct fuse_entry_out);
402 struct fuse_entry_out *earg = (struct fuse_entry_out *)buf;
403 struct fuse_open_out *oarg = (struct fuse_open_out *)(buf + entrysize);
405 memset(buf, 0, sizeof(buf));
406 fill_entry(earg, e);
407 fill_open(oarg, f);
408 return send_reply_ok(req, buf, entrysize + sizeof(struct fuse_open_out));
411 int fuse_reply_attr(fuse_req_t req, const struct stat *attr,
412 double attr_timeout)
414 struct fuse_attr_out arg;
415 size_t size = sizeof(arg);
417 memset(&arg, 0, sizeof(arg));
418 arg.attr_valid = calc_timeout_sec(attr_timeout);
419 arg.attr_valid_nsec = calc_timeout_nsec(attr_timeout);
420 convert_stat(attr, &arg.attr);
422 return send_reply_ok(req, &arg, size);
425 int fuse_reply_readlink(fuse_req_t req, const char *linkname)
427 return send_reply_ok(req, linkname, strlen(linkname));
430 int fuse_reply_open(fuse_req_t req, const struct fuse_file_info *f)
432 struct fuse_open_out arg;
434 memset(&arg, 0, sizeof(arg));
435 fill_open(&arg, f);
436 return send_reply_ok(req, &arg, sizeof(arg));
439 int fuse_reply_write(fuse_req_t req, size_t count)
441 struct fuse_write_out arg;
443 memset(&arg, 0, sizeof(arg));
444 arg.size = count;
446 return send_reply_ok(req, &arg, sizeof(arg));
449 int fuse_reply_buf(fuse_req_t req, const char *buf, size_t size)
451 return send_reply_ok(req, buf, size);
454 static int fuse_send_data_iov_fallback(struct fuse_session *se,
455 struct fuse_chan *ch, struct iovec *iov,
456 int iov_count, struct fuse_bufvec *buf,
457 size_t len)
459 /* Optimize common case */
460 if (buf->count == 1 && buf->idx == 0 && buf->off == 0 &&
461 !(buf->buf[0].flags & FUSE_BUF_IS_FD)) {
463 * FIXME: also avoid memory copy if there are multiple buffers
464 * but none of them contain an fd
467 iov[iov_count].iov_base = buf->buf[0].mem;
468 iov[iov_count].iov_len = len;
469 iov_count++;
470 return fuse_send_msg(se, ch, iov, iov_count);
473 abort(); /* Will have taken vhost path */
474 return 0;
477 static int fuse_send_data_iov(struct fuse_session *se, struct fuse_chan *ch,
478 struct iovec *iov, int iov_count,
479 struct fuse_bufvec *buf)
481 size_t len = fuse_buf_size(buf);
483 return fuse_send_data_iov_fallback(se, ch, iov, iov_count, buf, len);
486 int fuse_reply_data(fuse_req_t req, struct fuse_bufvec *bufv)
488 struct iovec iov[2];
489 struct fuse_out_header out;
490 int res;
492 iov[0].iov_base = &out;
493 iov[0].iov_len = sizeof(struct fuse_out_header);
495 out.unique = req->unique;
496 out.error = 0;
498 res = fuse_send_data_iov(req->se, req->ch, iov, 1, bufv);
499 if (res <= 0) {
500 fuse_free_req(req);
501 return res;
502 } else {
503 return fuse_reply_err(req, res);
507 int fuse_reply_statfs(fuse_req_t req, const struct statvfs *stbuf)
509 struct fuse_statfs_out arg;
510 size_t size = sizeof(arg);
512 memset(&arg, 0, sizeof(arg));
513 convert_statfs(stbuf, &arg.st);
515 return send_reply_ok(req, &arg, size);
518 int fuse_reply_xattr(fuse_req_t req, size_t count)
520 struct fuse_getxattr_out arg;
522 memset(&arg, 0, sizeof(arg));
523 arg.size = count;
525 return send_reply_ok(req, &arg, sizeof(arg));
528 int fuse_reply_lock(fuse_req_t req, const struct flock *lock)
530 struct fuse_lk_out arg;
532 memset(&arg, 0, sizeof(arg));
533 arg.lk.type = lock->l_type;
534 if (lock->l_type != F_UNLCK) {
535 arg.lk.start = lock->l_start;
536 if (lock->l_len == 0) {
537 arg.lk.end = OFFSET_MAX;
538 } else {
539 arg.lk.end = lock->l_start + lock->l_len - 1;
542 arg.lk.pid = lock->l_pid;
543 return send_reply_ok(req, &arg, sizeof(arg));
546 int fuse_reply_bmap(fuse_req_t req, uint64_t idx)
548 struct fuse_bmap_out arg;
550 memset(&arg, 0, sizeof(arg));
551 arg.block = idx;
553 return send_reply_ok(req, &arg, sizeof(arg));
556 static struct fuse_ioctl_iovec *fuse_ioctl_iovec_copy(const struct iovec *iov,
557 size_t count)
559 struct fuse_ioctl_iovec *fiov;
560 size_t i;
562 fiov = malloc(sizeof(fiov[0]) * count);
563 if (!fiov) {
564 return NULL;
567 for (i = 0; i < count; i++) {
568 fiov[i].base = (uintptr_t)iov[i].iov_base;
569 fiov[i].len = iov[i].iov_len;
572 return fiov;
575 int fuse_reply_ioctl_retry(fuse_req_t req, const struct iovec *in_iov,
576 size_t in_count, const struct iovec *out_iov,
577 size_t out_count)
579 struct fuse_ioctl_out arg;
580 struct fuse_ioctl_iovec *in_fiov = NULL;
581 struct fuse_ioctl_iovec *out_fiov = NULL;
582 struct iovec iov[4];
583 size_t count = 1;
584 int res;
586 memset(&arg, 0, sizeof(arg));
587 arg.flags |= FUSE_IOCTL_RETRY;
588 arg.in_iovs = in_count;
589 arg.out_iovs = out_count;
590 iov[count].iov_base = &arg;
591 iov[count].iov_len = sizeof(arg);
592 count++;
594 /* Can't handle non-compat 64bit ioctls on 32bit */
595 if (sizeof(void *) == 4 && req->ioctl_64bit) {
596 res = fuse_reply_err(req, EINVAL);
597 goto out;
600 if (in_count) {
601 in_fiov = fuse_ioctl_iovec_copy(in_iov, in_count);
602 if (!in_fiov) {
603 goto enomem;
606 iov[count].iov_base = (void *)in_fiov;
607 iov[count].iov_len = sizeof(in_fiov[0]) * in_count;
608 count++;
610 if (out_count) {
611 out_fiov = fuse_ioctl_iovec_copy(out_iov, out_count);
612 if (!out_fiov) {
613 goto enomem;
616 iov[count].iov_base = (void *)out_fiov;
617 iov[count].iov_len = sizeof(out_fiov[0]) * out_count;
618 count++;
621 res = send_reply_iov(req, 0, iov, count);
622 out:
623 free(in_fiov);
624 free(out_fiov);
626 return res;
628 enomem:
629 res = fuse_reply_err(req, ENOMEM);
630 goto out;
633 int fuse_reply_ioctl(fuse_req_t req, int result, const void *buf, size_t size)
635 struct fuse_ioctl_out arg;
636 struct iovec iov[3];
637 size_t count = 1;
639 memset(&arg, 0, sizeof(arg));
640 arg.result = result;
641 iov[count].iov_base = &arg;
642 iov[count].iov_len = sizeof(arg);
643 count++;
645 if (size) {
646 iov[count].iov_base = (char *)buf;
647 iov[count].iov_len = size;
648 count++;
651 return send_reply_iov(req, 0, iov, count);
654 int fuse_reply_ioctl_iov(fuse_req_t req, int result, const struct iovec *iov,
655 int count)
657 struct iovec *padded_iov;
658 struct fuse_ioctl_out arg;
659 int res;
661 padded_iov = malloc((count + 2) * sizeof(struct iovec));
662 if (padded_iov == NULL) {
663 return fuse_reply_err(req, ENOMEM);
666 memset(&arg, 0, sizeof(arg));
667 arg.result = result;
668 padded_iov[1].iov_base = &arg;
669 padded_iov[1].iov_len = sizeof(arg);
671 memcpy(&padded_iov[2], iov, count * sizeof(struct iovec));
673 res = send_reply_iov(req, 0, padded_iov, count + 2);
674 free(padded_iov);
676 return res;
679 int fuse_reply_poll(fuse_req_t req, unsigned revents)
681 struct fuse_poll_out arg;
683 memset(&arg, 0, sizeof(arg));
684 arg.revents = revents;
686 return send_reply_ok(req, &arg, sizeof(arg));
689 int fuse_reply_lseek(fuse_req_t req, off_t off)
691 struct fuse_lseek_out arg;
693 memset(&arg, 0, sizeof(arg));
694 arg.offset = off;
696 return send_reply_ok(req, &arg, sizeof(arg));
699 static void do_lookup(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
701 char *name = (char *)inarg;
703 if (req->se->op.lookup) {
704 req->se->op.lookup(req, nodeid, name);
705 } else {
706 fuse_reply_err(req, ENOSYS);
710 static void do_forget(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
712 struct fuse_forget_in *arg = (struct fuse_forget_in *)inarg;
714 if (req->se->op.forget) {
715 req->se->op.forget(req, nodeid, arg->nlookup);
716 } else {
717 fuse_reply_none(req);
721 static void do_batch_forget(fuse_req_t req, fuse_ino_t nodeid,
722 const void *inarg)
724 struct fuse_batch_forget_in *arg = (void *)inarg;
725 struct fuse_forget_one *param = (void *)PARAM(arg);
726 unsigned int i;
728 (void)nodeid;
730 if (req->se->op.forget_multi) {
731 req->se->op.forget_multi(req, arg->count,
732 (struct fuse_forget_data *)param);
733 } else if (req->se->op.forget) {
734 for (i = 0; i < arg->count; i++) {
735 struct fuse_forget_one *forget = &param[i];
736 struct fuse_req *dummy_req;
738 dummy_req = fuse_ll_alloc_req(req->se);
739 if (dummy_req == NULL) {
740 break;
743 dummy_req->unique = req->unique;
744 dummy_req->ctx = req->ctx;
745 dummy_req->ch = NULL;
747 req->se->op.forget(dummy_req, forget->nodeid, forget->nlookup);
749 fuse_reply_none(req);
750 } else {
751 fuse_reply_none(req);
755 static void do_getattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
757 struct fuse_file_info *fip = NULL;
758 struct fuse_file_info fi;
760 struct fuse_getattr_in *arg = (struct fuse_getattr_in *)inarg;
762 if (arg->getattr_flags & FUSE_GETATTR_FH) {
763 memset(&fi, 0, sizeof(fi));
764 fi.fh = arg->fh;
765 fip = &fi;
768 if (req->se->op.getattr) {
769 req->se->op.getattr(req, nodeid, fip);
770 } else {
771 fuse_reply_err(req, ENOSYS);
775 static void do_setattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
777 struct fuse_setattr_in *arg = (struct fuse_setattr_in *)inarg;
779 if (req->se->op.setattr) {
780 struct fuse_file_info *fi = NULL;
781 struct fuse_file_info fi_store;
782 struct stat stbuf;
783 memset(&stbuf, 0, sizeof(stbuf));
784 convert_attr(arg, &stbuf);
785 if (arg->valid & FATTR_FH) {
786 arg->valid &= ~FATTR_FH;
787 memset(&fi_store, 0, sizeof(fi_store));
788 fi = &fi_store;
789 fi->fh = arg->fh;
791 arg->valid &= FUSE_SET_ATTR_MODE | FUSE_SET_ATTR_UID |
792 FUSE_SET_ATTR_GID | FUSE_SET_ATTR_SIZE |
793 FUSE_SET_ATTR_ATIME | FUSE_SET_ATTR_MTIME |
794 FUSE_SET_ATTR_ATIME_NOW | FUSE_SET_ATTR_MTIME_NOW |
795 FUSE_SET_ATTR_CTIME;
797 req->se->op.setattr(req, nodeid, &stbuf, arg->valid, fi);
798 } else {
799 fuse_reply_err(req, ENOSYS);
803 static void do_access(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
805 struct fuse_access_in *arg = (struct fuse_access_in *)inarg;
807 if (req->se->op.access) {
808 req->se->op.access(req, nodeid, arg->mask);
809 } else {
810 fuse_reply_err(req, ENOSYS);
814 static void do_readlink(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
816 (void)inarg;
818 if (req->se->op.readlink) {
819 req->se->op.readlink(req, nodeid);
820 } else {
821 fuse_reply_err(req, ENOSYS);
825 static void do_mknod(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
827 struct fuse_mknod_in *arg = (struct fuse_mknod_in *)inarg;
828 char *name = PARAM(arg);
830 req->ctx.umask = arg->umask;
832 if (req->se->op.mknod) {
833 req->se->op.mknod(req, nodeid, name, arg->mode, arg->rdev);
834 } else {
835 fuse_reply_err(req, ENOSYS);
839 static void do_mkdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
841 struct fuse_mkdir_in *arg = (struct fuse_mkdir_in *)inarg;
843 req->ctx.umask = arg->umask;
845 if (req->se->op.mkdir) {
846 req->se->op.mkdir(req, nodeid, PARAM(arg), arg->mode);
847 } else {
848 fuse_reply_err(req, ENOSYS);
852 static void do_unlink(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
854 char *name = (char *)inarg;
856 if (req->se->op.unlink) {
857 req->se->op.unlink(req, nodeid, name);
858 } else {
859 fuse_reply_err(req, ENOSYS);
863 static void do_rmdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
865 char *name = (char *)inarg;
867 if (req->se->op.rmdir) {
868 req->se->op.rmdir(req, nodeid, name);
869 } else {
870 fuse_reply_err(req, ENOSYS);
874 static void do_symlink(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
876 char *name = (char *)inarg;
877 char *linkname = ((char *)inarg) + strlen((char *)inarg) + 1;
879 if (req->se->op.symlink) {
880 req->se->op.symlink(req, linkname, nodeid, name);
881 } else {
882 fuse_reply_err(req, ENOSYS);
886 static void do_rename(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
888 struct fuse_rename_in *arg = (struct fuse_rename_in *)inarg;
889 char *oldname = PARAM(arg);
890 char *newname = oldname + strlen(oldname) + 1;
892 if (req->se->op.rename) {
893 req->se->op.rename(req, nodeid, oldname, arg->newdir, newname, 0);
894 } else {
895 fuse_reply_err(req, ENOSYS);
899 static void do_rename2(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
901 struct fuse_rename2_in *arg = (struct fuse_rename2_in *)inarg;
902 char *oldname = PARAM(arg);
903 char *newname = oldname + strlen(oldname) + 1;
905 if (req->se->op.rename) {
906 req->se->op.rename(req, nodeid, oldname, arg->newdir, newname,
907 arg->flags);
908 } else {
909 fuse_reply_err(req, ENOSYS);
913 static void do_link(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
915 struct fuse_link_in *arg = (struct fuse_link_in *)inarg;
917 if (req->se->op.link) {
918 req->se->op.link(req, arg->oldnodeid, nodeid, PARAM(arg));
919 } else {
920 fuse_reply_err(req, ENOSYS);
924 static void do_create(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
926 struct fuse_create_in *arg = (struct fuse_create_in *)inarg;
928 if (req->se->op.create) {
929 struct fuse_file_info fi;
930 char *name = PARAM(arg);
932 memset(&fi, 0, sizeof(fi));
933 fi.flags = arg->flags;
935 req->ctx.umask = arg->umask;
937 req->se->op.create(req, nodeid, name, arg->mode, &fi);
938 } else {
939 fuse_reply_err(req, ENOSYS);
943 static void do_open(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
945 struct fuse_open_in *arg = (struct fuse_open_in *)inarg;
946 struct fuse_file_info fi;
948 memset(&fi, 0, sizeof(fi));
949 fi.flags = arg->flags;
951 if (req->se->op.open) {
952 req->se->op.open(req, nodeid, &fi);
953 } else {
954 fuse_reply_open(req, &fi);
958 static void do_read(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
960 struct fuse_read_in *arg = (struct fuse_read_in *)inarg;
962 if (req->se->op.read) {
963 struct fuse_file_info fi;
965 memset(&fi, 0, sizeof(fi));
966 fi.fh = arg->fh;
967 fi.lock_owner = arg->lock_owner;
968 fi.flags = arg->flags;
969 req->se->op.read(req, nodeid, arg->size, arg->offset, &fi);
970 } else {
971 fuse_reply_err(req, ENOSYS);
975 static void do_write(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
977 struct fuse_write_in *arg = (struct fuse_write_in *)inarg;
978 struct fuse_file_info fi;
979 char *param;
981 memset(&fi, 0, sizeof(fi));
982 fi.fh = arg->fh;
983 fi.writepage = (arg->write_flags & FUSE_WRITE_CACHE) != 0;
985 fi.lock_owner = arg->lock_owner;
986 fi.flags = arg->flags;
987 param = PARAM(arg);
989 if (req->se->op.write) {
990 req->se->op.write(req, nodeid, param, arg->size, arg->offset, &fi);
991 } else {
992 fuse_reply_err(req, ENOSYS);
996 static void do_write_buf(fuse_req_t req, fuse_ino_t nodeid, const void *inarg,
997 const struct fuse_buf *ibuf)
999 struct fuse_session *se = req->se;
1000 struct fuse_bufvec bufv = {
1001 .buf[0] = *ibuf,
1002 .count = 1,
1004 struct fuse_write_in *arg = (struct fuse_write_in *)inarg;
1005 struct fuse_file_info fi;
1007 memset(&fi, 0, sizeof(fi));
1008 fi.fh = arg->fh;
1009 fi.writepage = arg->write_flags & FUSE_WRITE_CACHE;
1011 fi.lock_owner = arg->lock_owner;
1012 fi.flags = arg->flags;
1013 if (!(bufv.buf[0].flags & FUSE_BUF_IS_FD)) {
1014 bufv.buf[0].mem = PARAM(arg);
1017 bufv.buf[0].size -=
1018 sizeof(struct fuse_in_header) + sizeof(struct fuse_write_in);
1019 if (bufv.buf[0].size < arg->size) {
1020 fuse_log(FUSE_LOG_ERR, "fuse: do_write_buf: buffer size too small\n");
1021 fuse_reply_err(req, EIO);
1022 return;
1024 bufv.buf[0].size = arg->size;
1026 se->op.write_buf(req, nodeid, &bufv, arg->offset, &fi);
1029 static void do_flush(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1031 struct fuse_flush_in *arg = (struct fuse_flush_in *)inarg;
1032 struct fuse_file_info fi;
1034 memset(&fi, 0, sizeof(fi));
1035 fi.fh = arg->fh;
1036 fi.flush = 1;
1037 fi.lock_owner = arg->lock_owner;
1039 if (req->se->op.flush) {
1040 req->se->op.flush(req, nodeid, &fi);
1041 } else {
1042 fuse_reply_err(req, ENOSYS);
1046 static void do_release(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1048 struct fuse_release_in *arg = (struct fuse_release_in *)inarg;
1049 struct fuse_file_info fi;
1051 memset(&fi, 0, sizeof(fi));
1052 fi.flags = arg->flags;
1053 fi.fh = arg->fh;
1054 fi.flush = (arg->release_flags & FUSE_RELEASE_FLUSH) ? 1 : 0;
1055 fi.lock_owner = arg->lock_owner;
1056 if (arg->release_flags & FUSE_RELEASE_FLOCK_UNLOCK) {
1057 fi.flock_release = 1;
1058 fi.lock_owner = arg->lock_owner;
1061 if (req->se->op.release) {
1062 req->se->op.release(req, nodeid, &fi);
1063 } else {
1064 fuse_reply_err(req, 0);
1068 static void do_fsync(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1070 struct fuse_fsync_in *arg = (struct fuse_fsync_in *)inarg;
1071 struct fuse_file_info fi;
1072 int datasync = arg->fsync_flags & 1;
1074 memset(&fi, 0, sizeof(fi));
1075 fi.fh = arg->fh;
1077 if (req->se->op.fsync) {
1078 if (fi.fh == (uint64_t)-1) {
1079 req->se->op.fsync(req, nodeid, datasync, NULL);
1080 } else {
1081 req->se->op.fsync(req, nodeid, datasync, &fi);
1083 } else {
1084 fuse_reply_err(req, ENOSYS);
1088 static void do_opendir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1090 struct fuse_open_in *arg = (struct fuse_open_in *)inarg;
1091 struct fuse_file_info fi;
1093 memset(&fi, 0, sizeof(fi));
1094 fi.flags = arg->flags;
1096 if (req->se->op.opendir) {
1097 req->se->op.opendir(req, nodeid, &fi);
1098 } else {
1099 fuse_reply_open(req, &fi);
1103 static void do_readdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1105 struct fuse_read_in *arg = (struct fuse_read_in *)inarg;
1106 struct fuse_file_info fi;
1108 memset(&fi, 0, sizeof(fi));
1109 fi.fh = arg->fh;
1111 if (req->se->op.readdir) {
1112 req->se->op.readdir(req, nodeid, arg->size, arg->offset, &fi);
1113 } else {
1114 fuse_reply_err(req, ENOSYS);
1118 static void do_readdirplus(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1120 struct fuse_read_in *arg = (struct fuse_read_in *)inarg;
1121 struct fuse_file_info fi;
1123 memset(&fi, 0, sizeof(fi));
1124 fi.fh = arg->fh;
1126 if (req->se->op.readdirplus) {
1127 req->se->op.readdirplus(req, nodeid, arg->size, arg->offset, &fi);
1128 } else {
1129 fuse_reply_err(req, ENOSYS);
1133 static void do_releasedir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1135 struct fuse_release_in *arg = (struct fuse_release_in *)inarg;
1136 struct fuse_file_info fi;
1138 memset(&fi, 0, sizeof(fi));
1139 fi.flags = arg->flags;
1140 fi.fh = arg->fh;
1142 if (req->se->op.releasedir) {
1143 req->se->op.releasedir(req, nodeid, &fi);
1144 } else {
1145 fuse_reply_err(req, 0);
1149 static void do_fsyncdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1151 struct fuse_fsync_in *arg = (struct fuse_fsync_in *)inarg;
1152 struct fuse_file_info fi;
1153 int datasync = arg->fsync_flags & 1;
1155 memset(&fi, 0, sizeof(fi));
1156 fi.fh = arg->fh;
1158 if (req->se->op.fsyncdir) {
1159 req->se->op.fsyncdir(req, nodeid, datasync, &fi);
1160 } else {
1161 fuse_reply_err(req, ENOSYS);
1165 static void do_statfs(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1167 (void)nodeid;
1168 (void)inarg;
1170 if (req->se->op.statfs) {
1171 req->se->op.statfs(req, nodeid);
1172 } else {
1173 struct statvfs buf = {
1174 .f_namemax = 255,
1175 .f_bsize = 512,
1177 fuse_reply_statfs(req, &buf);
1181 static void do_setxattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1183 struct fuse_setxattr_in *arg = (struct fuse_setxattr_in *)inarg;
1184 char *name = PARAM(arg);
1185 char *value = name + strlen(name) + 1;
1187 if (req->se->op.setxattr) {
1188 req->se->op.setxattr(req, nodeid, name, value, arg->size, arg->flags);
1189 } else {
1190 fuse_reply_err(req, ENOSYS);
1194 static void do_getxattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1196 struct fuse_getxattr_in *arg = (struct fuse_getxattr_in *)inarg;
1198 if (req->se->op.getxattr) {
1199 req->se->op.getxattr(req, nodeid, PARAM(arg), arg->size);
1200 } else {
1201 fuse_reply_err(req, ENOSYS);
1205 static void do_listxattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1207 struct fuse_getxattr_in *arg = (struct fuse_getxattr_in *)inarg;
1209 if (req->se->op.listxattr) {
1210 req->se->op.listxattr(req, nodeid, arg->size);
1211 } else {
1212 fuse_reply_err(req, ENOSYS);
1216 static void do_removexattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1218 char *name = (char *)inarg;
1220 if (req->se->op.removexattr) {
1221 req->se->op.removexattr(req, nodeid, name);
1222 } else {
1223 fuse_reply_err(req, ENOSYS);
1227 static void convert_fuse_file_lock(struct fuse_file_lock *fl,
1228 struct flock *flock)
1230 memset(flock, 0, sizeof(struct flock));
1231 flock->l_type = fl->type;
1232 flock->l_whence = SEEK_SET;
1233 flock->l_start = fl->start;
1234 if (fl->end == OFFSET_MAX) {
1235 flock->l_len = 0;
1236 } else {
1237 flock->l_len = fl->end - fl->start + 1;
1239 flock->l_pid = fl->pid;
1242 static void do_getlk(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1244 struct fuse_lk_in *arg = (struct fuse_lk_in *)inarg;
1245 struct fuse_file_info fi;
1246 struct flock flock;
1248 memset(&fi, 0, sizeof(fi));
1249 fi.fh = arg->fh;
1250 fi.lock_owner = arg->owner;
1252 convert_fuse_file_lock(&arg->lk, &flock);
1253 if (req->se->op.getlk) {
1254 req->se->op.getlk(req, nodeid, &fi, &flock);
1255 } else {
1256 fuse_reply_err(req, ENOSYS);
1260 static void do_setlk_common(fuse_req_t req, fuse_ino_t nodeid,
1261 const void *inarg, int sleep)
1263 struct fuse_lk_in *arg = (struct fuse_lk_in *)inarg;
1264 struct fuse_file_info fi;
1265 struct flock flock;
1267 memset(&fi, 0, sizeof(fi));
1268 fi.fh = arg->fh;
1269 fi.lock_owner = arg->owner;
1271 if (arg->lk_flags & FUSE_LK_FLOCK) {
1272 int op = 0;
1274 switch (arg->lk.type) {
1275 case F_RDLCK:
1276 op = LOCK_SH;
1277 break;
1278 case F_WRLCK:
1279 op = LOCK_EX;
1280 break;
1281 case F_UNLCK:
1282 op = LOCK_UN;
1283 break;
1285 if (!sleep) {
1286 op |= LOCK_NB;
1289 if (req->se->op.flock) {
1290 req->se->op.flock(req, nodeid, &fi, op);
1291 } else {
1292 fuse_reply_err(req, ENOSYS);
1294 } else {
1295 convert_fuse_file_lock(&arg->lk, &flock);
1296 if (req->se->op.setlk) {
1297 req->se->op.setlk(req, nodeid, &fi, &flock, sleep);
1298 } else {
1299 fuse_reply_err(req, ENOSYS);
1304 static void do_setlk(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1306 do_setlk_common(req, nodeid, inarg, 0);
1309 static void do_setlkw(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1311 do_setlk_common(req, nodeid, inarg, 1);
1314 static int find_interrupted(struct fuse_session *se, struct fuse_req *req)
1316 struct fuse_req *curr;
1318 for (curr = se->list.next; curr != &se->list; curr = curr->next) {
1319 if (curr->unique == req->u.i.unique) {
1320 fuse_interrupt_func_t func;
1321 void *data;
1323 curr->ctr++;
1324 pthread_mutex_unlock(&se->lock);
1326 /* Ugh, ugly locking */
1327 pthread_mutex_lock(&curr->lock);
1328 pthread_mutex_lock(&se->lock);
1329 curr->interrupted = 1;
1330 func = curr->u.ni.func;
1331 data = curr->u.ni.data;
1332 pthread_mutex_unlock(&se->lock);
1333 if (func) {
1334 func(curr, data);
1336 pthread_mutex_unlock(&curr->lock);
1338 pthread_mutex_lock(&se->lock);
1339 curr->ctr--;
1340 if (!curr->ctr) {
1341 destroy_req(curr);
1344 return 1;
1347 for (curr = se->interrupts.next; curr != &se->interrupts;
1348 curr = curr->next) {
1349 if (curr->u.i.unique == req->u.i.unique) {
1350 return 1;
1353 return 0;
1356 static void do_interrupt(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1358 struct fuse_interrupt_in *arg = (struct fuse_interrupt_in *)inarg;
1359 struct fuse_session *se = req->se;
1361 (void)nodeid;
1362 if (se->debug) {
1363 fuse_log(FUSE_LOG_DEBUG, "INTERRUPT: %llu\n",
1364 (unsigned long long)arg->unique);
1367 req->u.i.unique = arg->unique;
1369 pthread_mutex_lock(&se->lock);
1370 if (find_interrupted(se, req)) {
1371 destroy_req(req);
1372 } else {
1373 list_add_req(req, &se->interrupts);
1375 pthread_mutex_unlock(&se->lock);
1378 static struct fuse_req *check_interrupt(struct fuse_session *se,
1379 struct fuse_req *req)
1381 struct fuse_req *curr;
1383 for (curr = se->interrupts.next; curr != &se->interrupts;
1384 curr = curr->next) {
1385 if (curr->u.i.unique == req->unique) {
1386 req->interrupted = 1;
1387 list_del_req(curr);
1388 free(curr);
1389 return NULL;
1392 curr = se->interrupts.next;
1393 if (curr != &se->interrupts) {
1394 list_del_req(curr);
1395 list_init_req(curr);
1396 return curr;
1397 } else {
1398 return NULL;
1402 static void do_bmap(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1404 struct fuse_bmap_in *arg = (struct fuse_bmap_in *)inarg;
1406 if (req->se->op.bmap) {
1407 req->se->op.bmap(req, nodeid, arg->blocksize, arg->block);
1408 } else {
1409 fuse_reply_err(req, ENOSYS);
1413 static void do_ioctl(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1415 struct fuse_ioctl_in *arg = (struct fuse_ioctl_in *)inarg;
1416 unsigned int flags = arg->flags;
1417 void *in_buf = arg->in_size ? PARAM(arg) : NULL;
1418 struct fuse_file_info fi;
1420 if (flags & FUSE_IOCTL_DIR && !(req->se->conn.want & FUSE_CAP_IOCTL_DIR)) {
1421 fuse_reply_err(req, ENOTTY);
1422 return;
1425 memset(&fi, 0, sizeof(fi));
1426 fi.fh = arg->fh;
1428 if (sizeof(void *) == 4 && !(flags & FUSE_IOCTL_32BIT)) {
1429 req->ioctl_64bit = 1;
1432 if (req->se->op.ioctl) {
1433 req->se->op.ioctl(req, nodeid, arg->cmd, (void *)(uintptr_t)arg->arg,
1434 &fi, flags, in_buf, arg->in_size, arg->out_size);
1435 } else {
1436 fuse_reply_err(req, ENOSYS);
1440 void fuse_pollhandle_destroy(struct fuse_pollhandle *ph)
1442 free(ph);
1445 static void do_poll(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1447 struct fuse_poll_in *arg = (struct fuse_poll_in *)inarg;
1448 struct fuse_file_info fi;
1450 memset(&fi, 0, sizeof(fi));
1451 fi.fh = arg->fh;
1452 fi.poll_events = arg->events;
1454 if (req->se->op.poll) {
1455 struct fuse_pollhandle *ph = NULL;
1457 if (arg->flags & FUSE_POLL_SCHEDULE_NOTIFY) {
1458 ph = malloc(sizeof(struct fuse_pollhandle));
1459 if (ph == NULL) {
1460 fuse_reply_err(req, ENOMEM);
1461 return;
1463 ph->kh = arg->kh;
1464 ph->se = req->se;
1467 req->se->op.poll(req, nodeid, &fi, ph);
1468 } else {
1469 fuse_reply_err(req, ENOSYS);
1473 static void do_fallocate(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1475 struct fuse_fallocate_in *arg = (struct fuse_fallocate_in *)inarg;
1476 struct fuse_file_info fi;
1478 memset(&fi, 0, sizeof(fi));
1479 fi.fh = arg->fh;
1481 if (req->se->op.fallocate) {
1482 req->se->op.fallocate(req, nodeid, arg->mode, arg->offset, arg->length,
1483 &fi);
1484 } else {
1485 fuse_reply_err(req, ENOSYS);
1489 static void do_copy_file_range(fuse_req_t req, fuse_ino_t nodeid_in,
1490 const void *inarg)
1492 struct fuse_copy_file_range_in *arg =
1493 (struct fuse_copy_file_range_in *)inarg;
1494 struct fuse_file_info fi_in, fi_out;
1496 memset(&fi_in, 0, sizeof(fi_in));
1497 fi_in.fh = arg->fh_in;
1499 memset(&fi_out, 0, sizeof(fi_out));
1500 fi_out.fh = arg->fh_out;
1503 if (req->se->op.copy_file_range) {
1504 req->se->op.copy_file_range(req, nodeid_in, arg->off_in, &fi_in,
1505 arg->nodeid_out, arg->off_out, &fi_out,
1506 arg->len, arg->flags);
1507 } else {
1508 fuse_reply_err(req, ENOSYS);
1512 static void do_lseek(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1514 struct fuse_lseek_in *arg = (struct fuse_lseek_in *)inarg;
1515 struct fuse_file_info fi;
1517 memset(&fi, 0, sizeof(fi));
1518 fi.fh = arg->fh;
1520 if (req->se->op.lseek) {
1521 req->se->op.lseek(req, nodeid, arg->offset, arg->whence, &fi);
1522 } else {
1523 fuse_reply_err(req, ENOSYS);
1527 static void do_init(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1529 struct fuse_init_in *arg = (struct fuse_init_in *)inarg;
1530 struct fuse_init_out outarg;
1531 struct fuse_session *se = req->se;
1532 size_t bufsize = se->bufsize;
1533 size_t outargsize = sizeof(outarg);
1535 (void)nodeid;
1536 if (se->debug) {
1537 fuse_log(FUSE_LOG_DEBUG, "INIT: %u.%u\n", arg->major, arg->minor);
1538 if (arg->major == 7 && arg->minor >= 6) {
1539 fuse_log(FUSE_LOG_DEBUG, "flags=0x%08x\n", arg->flags);
1540 fuse_log(FUSE_LOG_DEBUG, "max_readahead=0x%08x\n",
1541 arg->max_readahead);
1544 se->conn.proto_major = arg->major;
1545 se->conn.proto_minor = arg->minor;
1546 se->conn.capable = 0;
1547 se->conn.want = 0;
1549 memset(&outarg, 0, sizeof(outarg));
1550 outarg.major = FUSE_KERNEL_VERSION;
1551 outarg.minor = FUSE_KERNEL_MINOR_VERSION;
1553 if (arg->major < 7 || (arg->major == 7 && arg->minor < 31)) {
1554 fuse_log(FUSE_LOG_ERR, "fuse: unsupported protocol version: %u.%u\n",
1555 arg->major, arg->minor);
1556 fuse_reply_err(req, EPROTO);
1557 return;
1560 if (arg->major > 7) {
1561 /* Wait for a second INIT request with a 7.X version */
1562 send_reply_ok(req, &outarg, sizeof(outarg));
1563 return;
1566 if (arg->max_readahead < se->conn.max_readahead) {
1567 se->conn.max_readahead = arg->max_readahead;
1569 if (arg->flags & FUSE_ASYNC_READ) {
1570 se->conn.capable |= FUSE_CAP_ASYNC_READ;
1572 if (arg->flags & FUSE_POSIX_LOCKS) {
1573 se->conn.capable |= FUSE_CAP_POSIX_LOCKS;
1575 if (arg->flags & FUSE_ATOMIC_O_TRUNC) {
1576 se->conn.capable |= FUSE_CAP_ATOMIC_O_TRUNC;
1578 if (arg->flags & FUSE_EXPORT_SUPPORT) {
1579 se->conn.capable |= FUSE_CAP_EXPORT_SUPPORT;
1581 if (arg->flags & FUSE_DONT_MASK) {
1582 se->conn.capable |= FUSE_CAP_DONT_MASK;
1584 if (arg->flags & FUSE_FLOCK_LOCKS) {
1585 se->conn.capable |= FUSE_CAP_FLOCK_LOCKS;
1587 if (arg->flags & FUSE_AUTO_INVAL_DATA) {
1588 se->conn.capable |= FUSE_CAP_AUTO_INVAL_DATA;
1590 if (arg->flags & FUSE_DO_READDIRPLUS) {
1591 se->conn.capable |= FUSE_CAP_READDIRPLUS;
1593 if (arg->flags & FUSE_READDIRPLUS_AUTO) {
1594 se->conn.capable |= FUSE_CAP_READDIRPLUS_AUTO;
1596 if (arg->flags & FUSE_ASYNC_DIO) {
1597 se->conn.capable |= FUSE_CAP_ASYNC_DIO;
1599 if (arg->flags & FUSE_WRITEBACK_CACHE) {
1600 se->conn.capable |= FUSE_CAP_WRITEBACK_CACHE;
1602 if (arg->flags & FUSE_NO_OPEN_SUPPORT) {
1603 se->conn.capable |= FUSE_CAP_NO_OPEN_SUPPORT;
1605 if (arg->flags & FUSE_PARALLEL_DIROPS) {
1606 se->conn.capable |= FUSE_CAP_PARALLEL_DIROPS;
1608 if (arg->flags & FUSE_POSIX_ACL) {
1609 se->conn.capable |= FUSE_CAP_POSIX_ACL;
1611 if (arg->flags & FUSE_HANDLE_KILLPRIV) {
1612 se->conn.capable |= FUSE_CAP_HANDLE_KILLPRIV;
1614 if (arg->flags & FUSE_NO_OPENDIR_SUPPORT) {
1615 se->conn.capable |= FUSE_CAP_NO_OPENDIR_SUPPORT;
1617 if (!(arg->flags & FUSE_MAX_PAGES)) {
1618 size_t max_bufsize = FUSE_DEFAULT_MAX_PAGES_PER_REQ * getpagesize() +
1619 FUSE_BUFFER_HEADER_SIZE;
1620 if (bufsize > max_bufsize) {
1621 bufsize = max_bufsize;
1624 #ifdef HAVE_SPLICE
1625 #ifdef HAVE_VMSPLICE
1626 se->conn.capable |= FUSE_CAP_SPLICE_WRITE | FUSE_CAP_SPLICE_MOVE;
1627 #endif
1628 se->conn.capable |= FUSE_CAP_SPLICE_READ;
1629 #endif
1630 se->conn.capable |= FUSE_CAP_IOCTL_DIR;
1633 * Default settings for modern filesystems.
1635 * Most of these capabilities were disabled by default in
1636 * libfuse2 for backwards compatibility reasons. In libfuse3,
1637 * we can finally enable them by default (as long as they're
1638 * supported by the kernel).
1640 #define LL_SET_DEFAULT(cond, cap) \
1641 if ((cond) && (se->conn.capable & (cap))) \
1642 se->conn.want |= (cap)
1643 LL_SET_DEFAULT(1, FUSE_CAP_ASYNC_READ);
1644 LL_SET_DEFAULT(1, FUSE_CAP_PARALLEL_DIROPS);
1645 LL_SET_DEFAULT(1, FUSE_CAP_AUTO_INVAL_DATA);
1646 LL_SET_DEFAULT(1, FUSE_CAP_HANDLE_KILLPRIV);
1647 LL_SET_DEFAULT(1, FUSE_CAP_ASYNC_DIO);
1648 LL_SET_DEFAULT(1, FUSE_CAP_IOCTL_DIR);
1649 LL_SET_DEFAULT(1, FUSE_CAP_ATOMIC_O_TRUNC);
1650 LL_SET_DEFAULT(se->op.write_buf, FUSE_CAP_SPLICE_READ);
1651 LL_SET_DEFAULT(se->op.getlk && se->op.setlk, FUSE_CAP_POSIX_LOCKS);
1652 LL_SET_DEFAULT(se->op.flock, FUSE_CAP_FLOCK_LOCKS);
1653 LL_SET_DEFAULT(se->op.readdirplus, FUSE_CAP_READDIRPLUS);
1654 LL_SET_DEFAULT(se->op.readdirplus && se->op.readdir,
1655 FUSE_CAP_READDIRPLUS_AUTO);
1656 se->conn.time_gran = 1;
1658 if (bufsize < FUSE_MIN_READ_BUFFER) {
1659 fuse_log(FUSE_LOG_ERR, "fuse: warning: buffer size too small: %zu\n",
1660 bufsize);
1661 bufsize = FUSE_MIN_READ_BUFFER;
1663 se->bufsize = bufsize;
1665 if (se->conn.max_write > bufsize - FUSE_BUFFER_HEADER_SIZE) {
1666 se->conn.max_write = bufsize - FUSE_BUFFER_HEADER_SIZE;
1669 se->got_init = 1;
1670 if (se->op.init) {
1671 se->op.init(se->userdata, &se->conn);
1674 if (se->conn.want & (~se->conn.capable)) {
1675 fuse_log(FUSE_LOG_ERR,
1676 "fuse: error: filesystem requested capabilities "
1677 "0x%x that are not supported by kernel, aborting.\n",
1678 se->conn.want & (~se->conn.capable));
1679 fuse_reply_err(req, EPROTO);
1680 se->error = -EPROTO;
1681 fuse_session_exit(se);
1682 return;
1685 if (se->conn.max_write < bufsize - FUSE_BUFFER_HEADER_SIZE) {
1686 se->bufsize = se->conn.max_write + FUSE_BUFFER_HEADER_SIZE;
1688 if (arg->flags & FUSE_MAX_PAGES) {
1689 outarg.flags |= FUSE_MAX_PAGES;
1690 outarg.max_pages = (se->conn.max_write - 1) / getpagesize() + 1;
1694 * Always enable big writes, this is superseded
1695 * by the max_write option
1697 outarg.flags |= FUSE_BIG_WRITES;
1699 if (se->conn.want & FUSE_CAP_ASYNC_READ) {
1700 outarg.flags |= FUSE_ASYNC_READ;
1702 if (se->conn.want & FUSE_CAP_POSIX_LOCKS) {
1703 outarg.flags |= FUSE_POSIX_LOCKS;
1705 if (se->conn.want & FUSE_CAP_ATOMIC_O_TRUNC) {
1706 outarg.flags |= FUSE_ATOMIC_O_TRUNC;
1708 if (se->conn.want & FUSE_CAP_EXPORT_SUPPORT) {
1709 outarg.flags |= FUSE_EXPORT_SUPPORT;
1711 if (se->conn.want & FUSE_CAP_DONT_MASK) {
1712 outarg.flags |= FUSE_DONT_MASK;
1714 if (se->conn.want & FUSE_CAP_FLOCK_LOCKS) {
1715 outarg.flags |= FUSE_FLOCK_LOCKS;
1717 if (se->conn.want & FUSE_CAP_AUTO_INVAL_DATA) {
1718 outarg.flags |= FUSE_AUTO_INVAL_DATA;
1720 if (se->conn.want & FUSE_CAP_READDIRPLUS) {
1721 outarg.flags |= FUSE_DO_READDIRPLUS;
1723 if (se->conn.want & FUSE_CAP_READDIRPLUS_AUTO) {
1724 outarg.flags |= FUSE_READDIRPLUS_AUTO;
1726 if (se->conn.want & FUSE_CAP_ASYNC_DIO) {
1727 outarg.flags |= FUSE_ASYNC_DIO;
1729 if (se->conn.want & FUSE_CAP_WRITEBACK_CACHE) {
1730 outarg.flags |= FUSE_WRITEBACK_CACHE;
1732 if (se->conn.want & FUSE_CAP_POSIX_ACL) {
1733 outarg.flags |= FUSE_POSIX_ACL;
1735 outarg.max_readahead = se->conn.max_readahead;
1736 outarg.max_write = se->conn.max_write;
1737 if (se->conn.max_background >= (1 << 16)) {
1738 se->conn.max_background = (1 << 16) - 1;
1740 if (se->conn.congestion_threshold > se->conn.max_background) {
1741 se->conn.congestion_threshold = se->conn.max_background;
1743 if (!se->conn.congestion_threshold) {
1744 se->conn.congestion_threshold = se->conn.max_background * 3 / 4;
1747 outarg.max_background = se->conn.max_background;
1748 outarg.congestion_threshold = se->conn.congestion_threshold;
1749 outarg.time_gran = se->conn.time_gran;
1751 if (se->debug) {
1752 fuse_log(FUSE_LOG_DEBUG, " INIT: %u.%u\n", outarg.major,
1753 outarg.minor);
1754 fuse_log(FUSE_LOG_DEBUG, " flags=0x%08x\n", outarg.flags);
1755 fuse_log(FUSE_LOG_DEBUG, " max_readahead=0x%08x\n",
1756 outarg.max_readahead);
1757 fuse_log(FUSE_LOG_DEBUG, " max_write=0x%08x\n", outarg.max_write);
1758 fuse_log(FUSE_LOG_DEBUG, " max_background=%i\n",
1759 outarg.max_background);
1760 fuse_log(FUSE_LOG_DEBUG, " congestion_threshold=%i\n",
1761 outarg.congestion_threshold);
1762 fuse_log(FUSE_LOG_DEBUG, " time_gran=%u\n", outarg.time_gran);
1765 send_reply_ok(req, &outarg, outargsize);
1768 static void do_destroy(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1770 struct fuse_session *se = req->se;
1772 (void)nodeid;
1773 (void)inarg;
1775 se->got_destroy = 1;
1776 if (se->op.destroy) {
1777 se->op.destroy(se->userdata);
1780 send_reply_ok(req, NULL, 0);
1783 static int send_notify_iov(struct fuse_session *se, int notify_code,
1784 struct iovec *iov, int count)
1786 struct fuse_out_header out;
1788 if (!se->got_init) {
1789 return -ENOTCONN;
1792 out.unique = 0;
1793 out.error = notify_code;
1794 iov[0].iov_base = &out;
1795 iov[0].iov_len = sizeof(struct fuse_out_header);
1797 return fuse_send_msg(se, NULL, iov, count);
1800 int fuse_lowlevel_notify_poll(struct fuse_pollhandle *ph)
1802 if (ph != NULL) {
1803 struct fuse_notify_poll_wakeup_out outarg;
1804 struct iovec iov[2];
1806 outarg.kh = ph->kh;
1808 iov[1].iov_base = &outarg;
1809 iov[1].iov_len = sizeof(outarg);
1811 return send_notify_iov(ph->se, FUSE_NOTIFY_POLL, iov, 2);
1812 } else {
1813 return 0;
1817 int fuse_lowlevel_notify_inval_inode(struct fuse_session *se, fuse_ino_t ino,
1818 off_t off, off_t len)
1820 struct fuse_notify_inval_inode_out outarg;
1821 struct iovec iov[2];
1823 if (!se) {
1824 return -EINVAL;
1827 outarg.ino = ino;
1828 outarg.off = off;
1829 outarg.len = len;
1831 iov[1].iov_base = &outarg;
1832 iov[1].iov_len = sizeof(outarg);
1834 return send_notify_iov(se, FUSE_NOTIFY_INVAL_INODE, iov, 2);
1837 int fuse_lowlevel_notify_inval_entry(struct fuse_session *se, fuse_ino_t parent,
1838 const char *name, size_t namelen)
1840 struct fuse_notify_inval_entry_out outarg;
1841 struct iovec iov[3];
1843 if (!se) {
1844 return -EINVAL;
1847 outarg.parent = parent;
1848 outarg.namelen = namelen;
1849 outarg.padding = 0;
1851 iov[1].iov_base = &outarg;
1852 iov[1].iov_len = sizeof(outarg);
1853 iov[2].iov_base = (void *)name;
1854 iov[2].iov_len = namelen + 1;
1856 return send_notify_iov(se, FUSE_NOTIFY_INVAL_ENTRY, iov, 3);
1859 int fuse_lowlevel_notify_delete(struct fuse_session *se, fuse_ino_t parent,
1860 fuse_ino_t child, const char *name,
1861 size_t namelen)
1863 struct fuse_notify_delete_out outarg;
1864 struct iovec iov[3];
1866 if (!se) {
1867 return -EINVAL;
1870 outarg.parent = parent;
1871 outarg.child = child;
1872 outarg.namelen = namelen;
1873 outarg.padding = 0;
1875 iov[1].iov_base = &outarg;
1876 iov[1].iov_len = sizeof(outarg);
1877 iov[2].iov_base = (void *)name;
1878 iov[2].iov_len = namelen + 1;
1880 return send_notify_iov(se, FUSE_NOTIFY_DELETE, iov, 3);
1883 int fuse_lowlevel_notify_store(struct fuse_session *se, fuse_ino_t ino,
1884 off_t offset, struct fuse_bufvec *bufv)
1886 struct fuse_out_header out;
1887 struct fuse_notify_store_out outarg;
1888 struct iovec iov[3];
1889 size_t size = fuse_buf_size(bufv);
1890 int res;
1892 if (!se) {
1893 return -EINVAL;
1896 out.unique = 0;
1897 out.error = FUSE_NOTIFY_STORE;
1899 outarg.nodeid = ino;
1900 outarg.offset = offset;
1901 outarg.size = size;
1902 outarg.padding = 0;
1904 iov[0].iov_base = &out;
1905 iov[0].iov_len = sizeof(out);
1906 iov[1].iov_base = &outarg;
1907 iov[1].iov_len = sizeof(outarg);
1909 res = fuse_send_data_iov(se, NULL, iov, 2, bufv);
1910 if (res > 0) {
1911 res = -res;
1914 return res;
1917 void *fuse_req_userdata(fuse_req_t req)
1919 return req->se->userdata;
1922 const struct fuse_ctx *fuse_req_ctx(fuse_req_t req)
1924 return &req->ctx;
1927 void fuse_req_interrupt_func(fuse_req_t req, fuse_interrupt_func_t func,
1928 void *data)
1930 pthread_mutex_lock(&req->lock);
1931 pthread_mutex_lock(&req->se->lock);
1932 req->u.ni.func = func;
1933 req->u.ni.data = data;
1934 pthread_mutex_unlock(&req->se->lock);
1935 if (req->interrupted && func) {
1936 func(req, data);
1938 pthread_mutex_unlock(&req->lock);
1941 int fuse_req_interrupted(fuse_req_t req)
1943 int interrupted;
1945 pthread_mutex_lock(&req->se->lock);
1946 interrupted = req->interrupted;
1947 pthread_mutex_unlock(&req->se->lock);
1949 return interrupted;
1952 static struct {
1953 void (*func)(fuse_req_t, fuse_ino_t, const void *);
1954 const char *name;
1955 } fuse_ll_ops[] = {
1956 [FUSE_LOOKUP] = { do_lookup, "LOOKUP" },
1957 [FUSE_FORGET] = { do_forget, "FORGET" },
1958 [FUSE_GETATTR] = { do_getattr, "GETATTR" },
1959 [FUSE_SETATTR] = { do_setattr, "SETATTR" },
1960 [FUSE_READLINK] = { do_readlink, "READLINK" },
1961 [FUSE_SYMLINK] = { do_symlink, "SYMLINK" },
1962 [FUSE_MKNOD] = { do_mknod, "MKNOD" },
1963 [FUSE_MKDIR] = { do_mkdir, "MKDIR" },
1964 [FUSE_UNLINK] = { do_unlink, "UNLINK" },
1965 [FUSE_RMDIR] = { do_rmdir, "RMDIR" },
1966 [FUSE_RENAME] = { do_rename, "RENAME" },
1967 [FUSE_LINK] = { do_link, "LINK" },
1968 [FUSE_OPEN] = { do_open, "OPEN" },
1969 [FUSE_READ] = { do_read, "READ" },
1970 [FUSE_WRITE] = { do_write, "WRITE" },
1971 [FUSE_STATFS] = { do_statfs, "STATFS" },
1972 [FUSE_RELEASE] = { do_release, "RELEASE" },
1973 [FUSE_FSYNC] = { do_fsync, "FSYNC" },
1974 [FUSE_SETXATTR] = { do_setxattr, "SETXATTR" },
1975 [FUSE_GETXATTR] = { do_getxattr, "GETXATTR" },
1976 [FUSE_LISTXATTR] = { do_listxattr, "LISTXATTR" },
1977 [FUSE_REMOVEXATTR] = { do_removexattr, "REMOVEXATTR" },
1978 [FUSE_FLUSH] = { do_flush, "FLUSH" },
1979 [FUSE_INIT] = { do_init, "INIT" },
1980 [FUSE_OPENDIR] = { do_opendir, "OPENDIR" },
1981 [FUSE_READDIR] = { do_readdir, "READDIR" },
1982 [FUSE_RELEASEDIR] = { do_releasedir, "RELEASEDIR" },
1983 [FUSE_FSYNCDIR] = { do_fsyncdir, "FSYNCDIR" },
1984 [FUSE_GETLK] = { do_getlk, "GETLK" },
1985 [FUSE_SETLK] = { do_setlk, "SETLK" },
1986 [FUSE_SETLKW] = { do_setlkw, "SETLKW" },
1987 [FUSE_ACCESS] = { do_access, "ACCESS" },
1988 [FUSE_CREATE] = { do_create, "CREATE" },
1989 [FUSE_INTERRUPT] = { do_interrupt, "INTERRUPT" },
1990 [FUSE_BMAP] = { do_bmap, "BMAP" },
1991 [FUSE_IOCTL] = { do_ioctl, "IOCTL" },
1992 [FUSE_POLL] = { do_poll, "POLL" },
1993 [FUSE_FALLOCATE] = { do_fallocate, "FALLOCATE" },
1994 [FUSE_DESTROY] = { do_destroy, "DESTROY" },
1995 [FUSE_NOTIFY_REPLY] = { NULL, "NOTIFY_REPLY" },
1996 [FUSE_BATCH_FORGET] = { do_batch_forget, "BATCH_FORGET" },
1997 [FUSE_READDIRPLUS] = { do_readdirplus, "READDIRPLUS" },
1998 [FUSE_RENAME2] = { do_rename2, "RENAME2" },
1999 [FUSE_COPY_FILE_RANGE] = { do_copy_file_range, "COPY_FILE_RANGE" },
2000 [FUSE_LSEEK] = { do_lseek, "LSEEK" },
2003 #define FUSE_MAXOP (sizeof(fuse_ll_ops) / sizeof(fuse_ll_ops[0]))
2005 static const char *opname(enum fuse_opcode opcode)
2007 if (opcode >= FUSE_MAXOP || !fuse_ll_ops[opcode].name) {
2008 return "???";
2009 } else {
2010 return fuse_ll_ops[opcode].name;
2014 void fuse_session_process_buf(struct fuse_session *se,
2015 const struct fuse_buf *buf)
2017 fuse_session_process_buf_int(se, buf, NULL);
2020 void fuse_session_process_buf_int(struct fuse_session *se,
2021 const struct fuse_buf *buf,
2022 struct fuse_chan *ch)
2024 struct fuse_in_header *in;
2025 const void *inarg;
2026 struct fuse_req *req;
2027 int err;
2029 in = buf->mem;
2031 if (se->debug) {
2032 fuse_log(FUSE_LOG_DEBUG,
2033 "unique: %llu, opcode: %s (%i), nodeid: %llu, insize: %zu, "
2034 "pid: %u\n",
2035 (unsigned long long)in->unique,
2036 opname((enum fuse_opcode)in->opcode), in->opcode,
2037 (unsigned long long)in->nodeid, buf->size, in->pid);
2040 req = fuse_ll_alloc_req(se);
2041 if (req == NULL) {
2042 struct fuse_out_header out = {
2043 .unique = in->unique,
2044 .error = -ENOMEM,
2046 struct iovec iov = {
2047 .iov_base = &out,
2048 .iov_len = sizeof(struct fuse_out_header),
2051 fuse_send_msg(se, ch, &iov, 1);
2052 return;
2055 req->unique = in->unique;
2056 req->ctx.uid = in->uid;
2057 req->ctx.gid = in->gid;
2058 req->ctx.pid = in->pid;
2059 req->ch = ch;
2061 err = EIO;
2062 if (!se->got_init) {
2063 enum fuse_opcode expected;
2065 expected = se->cuse_data ? CUSE_INIT : FUSE_INIT;
2066 if (in->opcode != expected) {
2067 goto reply_err;
2069 } else if (in->opcode == FUSE_INIT || in->opcode == CUSE_INIT) {
2070 goto reply_err;
2073 err = EACCES;
2074 /* Implement -o allow_root */
2075 if (se->deny_others && in->uid != se->owner && in->uid != 0 &&
2076 in->opcode != FUSE_INIT && in->opcode != FUSE_READ &&
2077 in->opcode != FUSE_WRITE && in->opcode != FUSE_FSYNC &&
2078 in->opcode != FUSE_RELEASE && in->opcode != FUSE_READDIR &&
2079 in->opcode != FUSE_FSYNCDIR && in->opcode != FUSE_RELEASEDIR &&
2080 in->opcode != FUSE_NOTIFY_REPLY && in->opcode != FUSE_READDIRPLUS) {
2081 goto reply_err;
2084 err = ENOSYS;
2085 if (in->opcode >= FUSE_MAXOP || !fuse_ll_ops[in->opcode].func) {
2086 goto reply_err;
2088 if (in->opcode != FUSE_INTERRUPT) {
2089 struct fuse_req *intr;
2090 pthread_mutex_lock(&se->lock);
2091 intr = check_interrupt(se, req);
2092 list_add_req(req, &se->list);
2093 pthread_mutex_unlock(&se->lock);
2094 if (intr) {
2095 fuse_reply_err(intr, EAGAIN);
2099 inarg = (void *)&in[1];
2100 if (in->opcode == FUSE_WRITE && se->op.write_buf) {
2101 do_write_buf(req, in->nodeid, inarg, buf);
2102 } else {
2103 fuse_ll_ops[in->opcode].func(req, in->nodeid, inarg);
2106 return;
2108 reply_err:
2109 fuse_reply_err(req, err);
2112 #define LL_OPTION(n, o, v) \
2114 n, offsetof(struct fuse_session, o), v \
2117 static const struct fuse_opt fuse_ll_opts[] = {
2118 LL_OPTION("debug", debug, 1),
2119 LL_OPTION("-d", debug, 1),
2120 LL_OPTION("--debug", debug, 1),
2121 LL_OPTION("allow_root", deny_others, 1),
2122 LL_OPTION("--socket-path=%s", vu_socket_path, 0),
2123 FUSE_OPT_END
2126 void fuse_lowlevel_version(void)
2128 printf("using FUSE kernel interface version %i.%i\n", FUSE_KERNEL_VERSION,
2129 FUSE_KERNEL_MINOR_VERSION);
2132 void fuse_lowlevel_help(void)
2135 * These are not all options, but the ones that are
2136 * potentially of interest to an end-user
2138 printf(
2139 " -o allow_root allow access by root\n"
2140 " --socket-path=PATH path for the vhost-user socket\n");
2143 void fuse_session_destroy(struct fuse_session *se)
2145 if (se->got_init && !se->got_destroy) {
2146 if (se->op.destroy) {
2147 se->op.destroy(se->userdata);
2150 pthread_mutex_destroy(&se->lock);
2151 free(se->cuse_data);
2152 if (se->fd != -1) {
2153 close(se->fd);
2155 free(se);
2159 struct fuse_session *fuse_session_new(struct fuse_args *args,
2160 const struct fuse_lowlevel_ops *op,
2161 size_t op_size, void *userdata)
2163 struct fuse_session *se;
2165 if (sizeof(struct fuse_lowlevel_ops) < op_size) {
2166 fuse_log(
2167 FUSE_LOG_ERR,
2168 "fuse: warning: library too old, some operations may not work\n");
2169 op_size = sizeof(struct fuse_lowlevel_ops);
2172 if (args->argc == 0) {
2173 fuse_log(FUSE_LOG_ERR,
2174 "fuse: empty argv passed to fuse_session_new().\n");
2175 return NULL;
2178 se = (struct fuse_session *)calloc(1, sizeof(struct fuse_session));
2179 if (se == NULL) {
2180 fuse_log(FUSE_LOG_ERR, "fuse: failed to allocate fuse object\n");
2181 goto out1;
2183 se->fd = -1;
2184 se->conn.max_write = UINT_MAX;
2185 se->conn.max_readahead = UINT_MAX;
2187 /* Parse options */
2188 if (fuse_opt_parse(args, se, fuse_ll_opts, NULL) == -1) {
2189 goto out2;
2191 if (args->argc == 1 && args->argv[0][0] == '-') {
2192 fuse_log(FUSE_LOG_ERR,
2193 "fuse: warning: argv[0] looks like an option, but "
2194 "will be ignored\n");
2195 } else if (args->argc != 1) {
2196 int i;
2197 fuse_log(FUSE_LOG_ERR, "fuse: unknown option(s): `");
2198 for (i = 1; i < args->argc - 1; i++) {
2199 fuse_log(FUSE_LOG_ERR, "%s ", args->argv[i]);
2201 fuse_log(FUSE_LOG_ERR, "%s'\n", args->argv[i]);
2202 goto out4;
2205 se->bufsize = FUSE_MAX_MAX_PAGES * getpagesize() + FUSE_BUFFER_HEADER_SIZE;
2207 list_init_req(&se->list);
2208 list_init_req(&se->interrupts);
2209 fuse_mutex_init(&se->lock);
2211 memcpy(&se->op, op, op_size);
2212 se->owner = getuid();
2213 se->userdata = userdata;
2215 return se;
2217 out4:
2218 fuse_opt_free_args(args);
2219 out2:
2220 free(se);
2221 out1:
2222 return NULL;
2225 int fuse_session_mount(struct fuse_session *se)
2227 int fd;
2230 * Make sure file descriptors 0, 1 and 2 are open, otherwise chaos
2231 * would ensue.
2233 do {
2234 fd = open("/dev/null", O_RDWR);
2235 if (fd > 2) {
2236 close(fd);
2238 } while (fd >= 0 && fd <= 2);
2241 * To allow FUSE daemons to run without privileges, the caller may open
2242 * /dev/fuse before launching the file system and pass on the file
2243 * descriptor by specifying /dev/fd/N as the mount point. Note that the
2244 * parent process takes care of performing the mount in this case.
2246 fd = fuse_mnt_parse_fuse_fd(mountpoint);
2247 if (fd != -1) {
2248 if (fcntl(fd, F_GETFD) == -1) {
2249 fuse_log(FUSE_LOG_ERR, "fuse: Invalid file descriptor /dev/fd/%u\n",
2250 fd);
2251 return -1;
2253 se->fd = fd;
2254 return 0;
2257 /* Open channel */
2258 fd = fuse_kern_mount(mountpoint, se->mo);
2259 if (fd == -1) {
2260 return -1;
2262 se->fd = fd;
2264 /* Save mountpoint */
2265 se->mountpoint = strdup(mountpoint);
2266 if (se->mountpoint == NULL) {
2267 goto error_out;
2270 return 0;
2272 error_out:
2273 fuse_kern_unmount(mountpoint, fd);
2274 return -1;
2277 int fuse_session_fd(struct fuse_session *se)
2279 return se->fd;
2282 void fuse_session_unmount(struct fuse_session *se)
2286 #ifdef linux
2287 int fuse_req_getgroups(fuse_req_t req, int size, gid_t list[])
2289 char *buf;
2290 size_t bufsize = 1024;
2291 char path[128];
2292 int ret;
2293 int fd;
2294 unsigned long pid = req->ctx.pid;
2295 char *s;
2297 sprintf(path, "/proc/%lu/task/%lu/status", pid, pid);
2299 retry:
2300 buf = malloc(bufsize);
2301 if (buf == NULL) {
2302 return -ENOMEM;
2305 ret = -EIO;
2306 fd = open(path, O_RDONLY);
2307 if (fd == -1) {
2308 goto out_free;
2311 ret = read(fd, buf, bufsize);
2312 close(fd);
2313 if (ret < 0) {
2314 ret = -EIO;
2315 goto out_free;
2318 if ((size_t)ret == bufsize) {
2319 free(buf);
2320 bufsize *= 4;
2321 goto retry;
2324 ret = -EIO;
2325 s = strstr(buf, "\nGroups:");
2326 if (s == NULL) {
2327 goto out_free;
2330 s += 8;
2331 ret = 0;
2332 while (1) {
2333 char *end;
2334 unsigned long val = strtoul(s, &end, 0);
2335 if (end == s) {
2336 break;
2339 s = end;
2340 if (ret < size) {
2341 list[ret] = val;
2343 ret++;
2346 out_free:
2347 free(buf);
2348 return ret;
2350 #else /* linux */
2352 * This is currently not implemented on other than Linux...
2354 int fuse_req_getgroups(fuse_req_t req, int size, gid_t list[])
2356 (void)req;
2357 (void)size;
2358 (void)list;
2359 return -ENOSYS;
2361 #endif
2363 void fuse_session_exit(struct fuse_session *se)
2365 se->exited = 1;
2368 void fuse_session_reset(struct fuse_session *se)
2370 se->exited = 0;
2371 se->error = 0;
2374 int fuse_session_exited(struct fuse_session *se)
2376 return se->exited;