virtiofsd: remove mountpoint dummy argument
[qemu/kevin.git] / tools / virtiofsd / fuse_lowlevel.c
blob2f32c68161dc29abb91ea0bef4c29d625f7cffd0
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 #define _GNU_SOURCE
14 #include "config.h"
15 #include "fuse_i.h"
16 #include "fuse_kernel.h"
17 #include "fuse_misc.h"
18 #include "fuse_opt.h"
20 #include <assert.h>
21 #include <errno.h>
22 #include <limits.h>
23 #include <stddef.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sys/file.h>
28 #include <unistd.h>
31 #define PARAM(inarg) (((char *)(inarg)) + sizeof(*(inarg)))
32 #define OFFSET_MAX 0x7fffffffffffffffLL
34 #define container_of(ptr, type, member) \
35 ({ \
36 const typeof(((type *)0)->member) *__mptr = (ptr); \
37 (type *)((char *)__mptr - offsetof(type, member)); \
40 struct fuse_pollhandle {
41 uint64_t kh;
42 struct fuse_session *se;
45 static size_t pagesize;
47 static __attribute__((constructor)) void fuse_ll_init_pagesize(void)
49 pagesize = getpagesize();
52 static void convert_stat(const struct stat *stbuf, struct fuse_attr *attr)
54 attr->ino = stbuf->st_ino;
55 attr->mode = stbuf->st_mode;
56 attr->nlink = stbuf->st_nlink;
57 attr->uid = stbuf->st_uid;
58 attr->gid = stbuf->st_gid;
59 attr->rdev = stbuf->st_rdev;
60 attr->size = stbuf->st_size;
61 attr->blksize = stbuf->st_blksize;
62 attr->blocks = stbuf->st_blocks;
63 attr->atime = stbuf->st_atime;
64 attr->mtime = stbuf->st_mtime;
65 attr->ctime = stbuf->st_ctime;
66 attr->atimensec = ST_ATIM_NSEC(stbuf);
67 attr->mtimensec = ST_MTIM_NSEC(stbuf);
68 attr->ctimensec = ST_CTIM_NSEC(stbuf);
71 static void convert_attr(const struct fuse_setattr_in *attr, struct stat *stbuf)
73 stbuf->st_mode = attr->mode;
74 stbuf->st_uid = attr->uid;
75 stbuf->st_gid = attr->gid;
76 stbuf->st_size = attr->size;
77 stbuf->st_atime = attr->atime;
78 stbuf->st_mtime = attr->mtime;
79 stbuf->st_ctime = attr->ctime;
80 ST_ATIM_NSEC_SET(stbuf, attr->atimensec);
81 ST_MTIM_NSEC_SET(stbuf, attr->mtimensec);
82 ST_CTIM_NSEC_SET(stbuf, attr->ctimensec);
85 static size_t iov_length(const struct iovec *iov, size_t count)
87 size_t seg;
88 size_t ret = 0;
90 for (seg = 0; seg < count; seg++) {
91 ret += iov[seg].iov_len;
93 return ret;
96 static void list_init_req(struct fuse_req *req)
98 req->next = req;
99 req->prev = req;
102 static void list_del_req(struct fuse_req *req)
104 struct fuse_req *prev = req->prev;
105 struct fuse_req *next = req->next;
106 prev->next = next;
107 next->prev = prev;
110 static void list_add_req(struct fuse_req *req, struct fuse_req *next)
112 struct fuse_req *prev = next->prev;
113 req->next = next;
114 req->prev = prev;
115 prev->next = req;
116 next->prev = req;
119 static void destroy_req(fuse_req_t req)
121 pthread_mutex_destroy(&req->lock);
122 free(req);
125 void fuse_free_req(fuse_req_t req)
127 int ctr;
128 struct fuse_session *se = req->se;
130 pthread_mutex_lock(&se->lock);
131 req->u.ni.func = NULL;
132 req->u.ni.data = NULL;
133 list_del_req(req);
134 ctr = --req->ctr;
135 req->ch = NULL;
136 pthread_mutex_unlock(&se->lock);
137 if (!ctr) {
138 destroy_req(req);
142 static struct fuse_req *fuse_ll_alloc_req(struct fuse_session *se)
144 struct fuse_req *req;
146 req = (struct fuse_req *)calloc(1, sizeof(struct fuse_req));
147 if (req == NULL) {
148 fuse_log(FUSE_LOG_ERR, "fuse: failed to allocate request\n");
149 } else {
150 req->se = se;
151 req->ctr = 1;
152 list_init_req(req);
153 fuse_mutex_init(&req->lock);
156 return req;
159 /* Send data. If *ch* is NULL, send via session master fd */
160 static int fuse_send_msg(struct fuse_session *se, struct fuse_chan *ch,
161 struct iovec *iov, int count)
163 struct fuse_out_header *out = iov[0].iov_base;
165 out->len = iov_length(iov, count);
166 if (se->debug) {
167 if (out->unique == 0) {
168 fuse_log(FUSE_LOG_DEBUG, "NOTIFY: code=%d length=%u\n", out->error,
169 out->len);
170 } else if (out->error) {
171 fuse_log(FUSE_LOG_DEBUG,
172 " unique: %llu, error: %i (%s), outsize: %i\n",
173 (unsigned long long)out->unique, out->error,
174 strerror(-out->error), out->len);
175 } else {
176 fuse_log(FUSE_LOG_DEBUG, " unique: %llu, success, outsize: %i\n",
177 (unsigned long long)out->unique, out->len);
181 abort(); /* virtio should have taken it before here */
182 return 0;
186 int fuse_send_reply_iov_nofree(fuse_req_t req, int error, struct iovec *iov,
187 int count)
189 struct fuse_out_header out;
191 if (error <= -1000 || error > 0) {
192 fuse_log(FUSE_LOG_ERR, "fuse: bad error value: %i\n", error);
193 error = -ERANGE;
196 out.unique = req->unique;
197 out.error = error;
199 iov[0].iov_base = &out;
200 iov[0].iov_len = sizeof(struct fuse_out_header);
202 return fuse_send_msg(req->se, req->ch, iov, count);
205 static int send_reply_iov(fuse_req_t req, int error, struct iovec *iov,
206 int count)
208 int res;
210 res = fuse_send_reply_iov_nofree(req, error, iov, count);
211 fuse_free_req(req);
212 return res;
215 static int send_reply(fuse_req_t req, int error, const void *arg,
216 size_t argsize)
218 struct iovec iov[2];
219 int count = 1;
220 if (argsize) {
221 iov[1].iov_base = (void *)arg;
222 iov[1].iov_len = argsize;
223 count++;
225 return send_reply_iov(req, error, iov, count);
228 int fuse_reply_iov(fuse_req_t req, const struct iovec *iov, int count)
230 int res;
231 struct iovec *padded_iov;
233 padded_iov = malloc((count + 1) * sizeof(struct iovec));
234 if (padded_iov == NULL) {
235 return fuse_reply_err(req, ENOMEM);
238 memcpy(padded_iov + 1, iov, count * sizeof(struct iovec));
239 count++;
241 res = send_reply_iov(req, 0, padded_iov, count);
242 free(padded_iov);
244 return res;
249 * 'buf` is allowed to be empty so that the proper size may be
250 * allocated by the caller
252 size_t fuse_add_direntry(fuse_req_t req, char *buf, size_t bufsize,
253 const char *name, const struct stat *stbuf, off_t off)
255 (void)req;
256 size_t namelen;
257 size_t entlen;
258 size_t entlen_padded;
259 struct fuse_dirent *dirent;
261 namelen = strlen(name);
262 entlen = FUSE_NAME_OFFSET + namelen;
263 entlen_padded = FUSE_DIRENT_ALIGN(entlen);
265 if ((buf == NULL) || (entlen_padded > bufsize)) {
266 return entlen_padded;
269 dirent = (struct fuse_dirent *)buf;
270 dirent->ino = stbuf->st_ino;
271 dirent->off = off;
272 dirent->namelen = namelen;
273 dirent->type = (stbuf->st_mode & S_IFMT) >> 12;
274 memcpy(dirent->name, name, namelen);
275 memset(dirent->name + namelen, 0, entlen_padded - entlen);
277 return entlen_padded;
280 static void convert_statfs(const struct statvfs *stbuf,
281 struct fuse_kstatfs *kstatfs)
283 kstatfs->bsize = stbuf->f_bsize;
284 kstatfs->frsize = stbuf->f_frsize;
285 kstatfs->blocks = stbuf->f_blocks;
286 kstatfs->bfree = stbuf->f_bfree;
287 kstatfs->bavail = stbuf->f_bavail;
288 kstatfs->files = stbuf->f_files;
289 kstatfs->ffree = stbuf->f_ffree;
290 kstatfs->namelen = stbuf->f_namemax;
293 static int send_reply_ok(fuse_req_t req, const void *arg, size_t argsize)
295 return send_reply(req, 0, arg, argsize);
298 int fuse_reply_err(fuse_req_t req, int err)
300 return send_reply(req, -err, NULL, 0);
303 void fuse_reply_none(fuse_req_t req)
305 fuse_free_req(req);
308 static unsigned long calc_timeout_sec(double t)
310 if (t > (double)ULONG_MAX) {
311 return ULONG_MAX;
312 } else if (t < 0.0) {
313 return 0;
314 } else {
315 return (unsigned long)t;
319 static unsigned int calc_timeout_nsec(double t)
321 double f = t - (double)calc_timeout_sec(t);
322 if (f < 0.0) {
323 return 0;
324 } else if (f >= 0.999999999) {
325 return 999999999;
326 } else {
327 return (unsigned int)(f * 1.0e9);
331 static void fill_entry(struct fuse_entry_out *arg,
332 const struct fuse_entry_param *e)
334 arg->nodeid = e->ino;
335 arg->generation = e->generation;
336 arg->entry_valid = calc_timeout_sec(e->entry_timeout);
337 arg->entry_valid_nsec = calc_timeout_nsec(e->entry_timeout);
338 arg->attr_valid = calc_timeout_sec(e->attr_timeout);
339 arg->attr_valid_nsec = calc_timeout_nsec(e->attr_timeout);
340 convert_stat(&e->attr, &arg->attr);
344 * `buf` is allowed to be empty so that the proper size may be
345 * allocated by the caller
347 size_t fuse_add_direntry_plus(fuse_req_t req, char *buf, size_t bufsize,
348 const char *name,
349 const struct fuse_entry_param *e, off_t off)
351 (void)req;
352 size_t namelen;
353 size_t entlen;
354 size_t entlen_padded;
356 namelen = strlen(name);
357 entlen = FUSE_NAME_OFFSET_DIRENTPLUS + namelen;
358 entlen_padded = FUSE_DIRENT_ALIGN(entlen);
359 if ((buf == NULL) || (entlen_padded > bufsize)) {
360 return entlen_padded;
363 struct fuse_direntplus *dp = (struct fuse_direntplus *)buf;
364 memset(&dp->entry_out, 0, sizeof(dp->entry_out));
365 fill_entry(&dp->entry_out, e);
367 struct fuse_dirent *dirent = &dp->dirent;
368 dirent->ino = e->attr.st_ino;
369 dirent->off = off;
370 dirent->namelen = namelen;
371 dirent->type = (e->attr.st_mode & S_IFMT) >> 12;
372 memcpy(dirent->name, name, namelen);
373 memset(dirent->name + namelen, 0, entlen_padded - entlen);
375 return entlen_padded;
378 static void fill_open(struct fuse_open_out *arg, const struct fuse_file_info *f)
380 arg->fh = f->fh;
381 if (f->direct_io) {
382 arg->open_flags |= FOPEN_DIRECT_IO;
384 if (f->keep_cache) {
385 arg->open_flags |= FOPEN_KEEP_CACHE;
387 if (f->cache_readdir) {
388 arg->open_flags |= FOPEN_CACHE_DIR;
390 if (f->nonseekable) {
391 arg->open_flags |= FOPEN_NONSEEKABLE;
395 int fuse_reply_entry(fuse_req_t req, const struct fuse_entry_param *e)
397 struct fuse_entry_out arg;
398 size_t size = req->se->conn.proto_minor < 9 ? FUSE_COMPAT_ENTRY_OUT_SIZE :
399 sizeof(arg);
402 * before ABI 7.4 e->ino == 0 was invalid, only ENOENT meant
403 * negative entry
405 if (!e->ino && req->se->conn.proto_minor < 4) {
406 return fuse_reply_err(req, ENOENT);
409 memset(&arg, 0, sizeof(arg));
410 fill_entry(&arg, e);
411 return send_reply_ok(req, &arg, size);
414 int fuse_reply_create(fuse_req_t req, const struct fuse_entry_param *e,
415 const struct fuse_file_info *f)
417 char buf[sizeof(struct fuse_entry_out) + sizeof(struct fuse_open_out)];
418 size_t entrysize = req->se->conn.proto_minor < 9 ?
419 FUSE_COMPAT_ENTRY_OUT_SIZE :
420 sizeof(struct fuse_entry_out);
421 struct fuse_entry_out *earg = (struct fuse_entry_out *)buf;
422 struct fuse_open_out *oarg = (struct fuse_open_out *)(buf + entrysize);
424 memset(buf, 0, sizeof(buf));
425 fill_entry(earg, e);
426 fill_open(oarg, f);
427 return send_reply_ok(req, buf, entrysize + sizeof(struct fuse_open_out));
430 int fuse_reply_attr(fuse_req_t req, const struct stat *attr,
431 double attr_timeout)
433 struct fuse_attr_out arg;
434 size_t size =
435 req->se->conn.proto_minor < 9 ? FUSE_COMPAT_ATTR_OUT_SIZE : sizeof(arg);
437 memset(&arg, 0, sizeof(arg));
438 arg.attr_valid = calc_timeout_sec(attr_timeout);
439 arg.attr_valid_nsec = calc_timeout_nsec(attr_timeout);
440 convert_stat(attr, &arg.attr);
442 return send_reply_ok(req, &arg, size);
445 int fuse_reply_readlink(fuse_req_t req, const char *linkname)
447 return send_reply_ok(req, linkname, strlen(linkname));
450 int fuse_reply_open(fuse_req_t req, const struct fuse_file_info *f)
452 struct fuse_open_out arg;
454 memset(&arg, 0, sizeof(arg));
455 fill_open(&arg, f);
456 return send_reply_ok(req, &arg, sizeof(arg));
459 int fuse_reply_write(fuse_req_t req, size_t count)
461 struct fuse_write_out arg;
463 memset(&arg, 0, sizeof(arg));
464 arg.size = count;
466 return send_reply_ok(req, &arg, sizeof(arg));
469 int fuse_reply_buf(fuse_req_t req, const char *buf, size_t size)
471 return send_reply_ok(req, buf, size);
474 static int fuse_send_data_iov_fallback(struct fuse_session *se,
475 struct fuse_chan *ch, struct iovec *iov,
476 int iov_count, struct fuse_bufvec *buf,
477 size_t len)
479 /* Optimize common case */
480 if (buf->count == 1 && buf->idx == 0 && buf->off == 0 &&
481 !(buf->buf[0].flags & FUSE_BUF_IS_FD)) {
483 * FIXME: also avoid memory copy if there are multiple buffers
484 * but none of them contain an fd
487 iov[iov_count].iov_base = buf->buf[0].mem;
488 iov[iov_count].iov_len = len;
489 iov_count++;
490 return fuse_send_msg(se, ch, iov, iov_count);
493 abort(); /* Will have taken vhost path */
494 return 0;
497 static int fuse_send_data_iov(struct fuse_session *se, struct fuse_chan *ch,
498 struct iovec *iov, int iov_count,
499 struct fuse_bufvec *buf, unsigned int flags)
501 size_t len = fuse_buf_size(buf);
502 (void)flags;
504 return fuse_send_data_iov_fallback(se, ch, iov, iov_count, buf, len);
507 int fuse_reply_data(fuse_req_t req, struct fuse_bufvec *bufv,
508 enum fuse_buf_copy_flags flags)
510 struct iovec iov[2];
511 struct fuse_out_header out;
512 int res;
514 iov[0].iov_base = &out;
515 iov[0].iov_len = sizeof(struct fuse_out_header);
517 out.unique = req->unique;
518 out.error = 0;
520 res = fuse_send_data_iov(req->se, req->ch, iov, 1, bufv, flags);
521 if (res <= 0) {
522 fuse_free_req(req);
523 return res;
524 } else {
525 return fuse_reply_err(req, res);
529 int fuse_reply_statfs(fuse_req_t req, const struct statvfs *stbuf)
531 struct fuse_statfs_out arg;
532 size_t size =
533 req->se->conn.proto_minor < 4 ? FUSE_COMPAT_STATFS_SIZE : sizeof(arg);
535 memset(&arg, 0, sizeof(arg));
536 convert_statfs(stbuf, &arg.st);
538 return send_reply_ok(req, &arg, size);
541 int fuse_reply_xattr(fuse_req_t req, size_t count)
543 struct fuse_getxattr_out arg;
545 memset(&arg, 0, sizeof(arg));
546 arg.size = count;
548 return send_reply_ok(req, &arg, sizeof(arg));
551 int fuse_reply_lock(fuse_req_t req, const struct flock *lock)
553 struct fuse_lk_out arg;
555 memset(&arg, 0, sizeof(arg));
556 arg.lk.type = lock->l_type;
557 if (lock->l_type != F_UNLCK) {
558 arg.lk.start = lock->l_start;
559 if (lock->l_len == 0) {
560 arg.lk.end = OFFSET_MAX;
561 } else {
562 arg.lk.end = lock->l_start + lock->l_len - 1;
565 arg.lk.pid = lock->l_pid;
566 return send_reply_ok(req, &arg, sizeof(arg));
569 int fuse_reply_bmap(fuse_req_t req, uint64_t idx)
571 struct fuse_bmap_out arg;
573 memset(&arg, 0, sizeof(arg));
574 arg.block = idx;
576 return send_reply_ok(req, &arg, sizeof(arg));
579 static struct fuse_ioctl_iovec *fuse_ioctl_iovec_copy(const struct iovec *iov,
580 size_t count)
582 struct fuse_ioctl_iovec *fiov;
583 size_t i;
585 fiov = malloc(sizeof(fiov[0]) * count);
586 if (!fiov) {
587 return NULL;
590 for (i = 0; i < count; i++) {
591 fiov[i].base = (uintptr_t)iov[i].iov_base;
592 fiov[i].len = iov[i].iov_len;
595 return fiov;
598 int fuse_reply_ioctl_retry(fuse_req_t req, const struct iovec *in_iov,
599 size_t in_count, const struct iovec *out_iov,
600 size_t out_count)
602 struct fuse_ioctl_out arg;
603 struct fuse_ioctl_iovec *in_fiov = NULL;
604 struct fuse_ioctl_iovec *out_fiov = NULL;
605 struct iovec iov[4];
606 size_t count = 1;
607 int res;
609 memset(&arg, 0, sizeof(arg));
610 arg.flags |= FUSE_IOCTL_RETRY;
611 arg.in_iovs = in_count;
612 arg.out_iovs = out_count;
613 iov[count].iov_base = &arg;
614 iov[count].iov_len = sizeof(arg);
615 count++;
617 if (req->se->conn.proto_minor < 16) {
618 if (in_count) {
619 iov[count].iov_base = (void *)in_iov;
620 iov[count].iov_len = sizeof(in_iov[0]) * in_count;
621 count++;
624 if (out_count) {
625 iov[count].iov_base = (void *)out_iov;
626 iov[count].iov_len = sizeof(out_iov[0]) * out_count;
627 count++;
629 } else {
630 /* Can't handle non-compat 64bit ioctls on 32bit */
631 if (sizeof(void *) == 4 && req->ioctl_64bit) {
632 res = fuse_reply_err(req, EINVAL);
633 goto out;
636 if (in_count) {
637 in_fiov = fuse_ioctl_iovec_copy(in_iov, in_count);
638 if (!in_fiov) {
639 goto enomem;
642 iov[count].iov_base = (void *)in_fiov;
643 iov[count].iov_len = sizeof(in_fiov[0]) * in_count;
644 count++;
646 if (out_count) {
647 out_fiov = fuse_ioctl_iovec_copy(out_iov, out_count);
648 if (!out_fiov) {
649 goto enomem;
652 iov[count].iov_base = (void *)out_fiov;
653 iov[count].iov_len = sizeof(out_fiov[0]) * out_count;
654 count++;
658 res = send_reply_iov(req, 0, iov, count);
659 out:
660 free(in_fiov);
661 free(out_fiov);
663 return res;
665 enomem:
666 res = fuse_reply_err(req, ENOMEM);
667 goto out;
670 int fuse_reply_ioctl(fuse_req_t req, int result, const void *buf, size_t size)
672 struct fuse_ioctl_out arg;
673 struct iovec iov[3];
674 size_t count = 1;
676 memset(&arg, 0, sizeof(arg));
677 arg.result = result;
678 iov[count].iov_base = &arg;
679 iov[count].iov_len = sizeof(arg);
680 count++;
682 if (size) {
683 iov[count].iov_base = (char *)buf;
684 iov[count].iov_len = size;
685 count++;
688 return send_reply_iov(req, 0, iov, count);
691 int fuse_reply_ioctl_iov(fuse_req_t req, int result, const struct iovec *iov,
692 int count)
694 struct iovec *padded_iov;
695 struct fuse_ioctl_out arg;
696 int res;
698 padded_iov = malloc((count + 2) * sizeof(struct iovec));
699 if (padded_iov == NULL) {
700 return fuse_reply_err(req, ENOMEM);
703 memset(&arg, 0, sizeof(arg));
704 arg.result = result;
705 padded_iov[1].iov_base = &arg;
706 padded_iov[1].iov_len = sizeof(arg);
708 memcpy(&padded_iov[2], iov, count * sizeof(struct iovec));
710 res = send_reply_iov(req, 0, padded_iov, count + 2);
711 free(padded_iov);
713 return res;
716 int fuse_reply_poll(fuse_req_t req, unsigned revents)
718 struct fuse_poll_out arg;
720 memset(&arg, 0, sizeof(arg));
721 arg.revents = revents;
723 return send_reply_ok(req, &arg, sizeof(arg));
726 int fuse_reply_lseek(fuse_req_t req, off_t off)
728 struct fuse_lseek_out arg;
730 memset(&arg, 0, sizeof(arg));
731 arg.offset = off;
733 return send_reply_ok(req, &arg, sizeof(arg));
736 static void do_lookup(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
738 char *name = (char *)inarg;
740 if (req->se->op.lookup) {
741 req->se->op.lookup(req, nodeid, name);
742 } else {
743 fuse_reply_err(req, ENOSYS);
747 static void do_forget(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
749 struct fuse_forget_in *arg = (struct fuse_forget_in *)inarg;
751 if (req->se->op.forget) {
752 req->se->op.forget(req, nodeid, arg->nlookup);
753 } else {
754 fuse_reply_none(req);
758 static void do_batch_forget(fuse_req_t req, fuse_ino_t nodeid,
759 const void *inarg)
761 struct fuse_batch_forget_in *arg = (void *)inarg;
762 struct fuse_forget_one *param = (void *)PARAM(arg);
763 unsigned int i;
765 (void)nodeid;
767 if (req->se->op.forget_multi) {
768 req->se->op.forget_multi(req, arg->count,
769 (struct fuse_forget_data *)param);
770 } else if (req->se->op.forget) {
771 for (i = 0; i < arg->count; i++) {
772 struct fuse_forget_one *forget = &param[i];
773 struct fuse_req *dummy_req;
775 dummy_req = fuse_ll_alloc_req(req->se);
776 if (dummy_req == NULL) {
777 break;
780 dummy_req->unique = req->unique;
781 dummy_req->ctx = req->ctx;
782 dummy_req->ch = NULL;
784 req->se->op.forget(dummy_req, forget->nodeid, forget->nlookup);
786 fuse_reply_none(req);
787 } else {
788 fuse_reply_none(req);
792 static void do_getattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
794 struct fuse_file_info *fip = NULL;
795 struct fuse_file_info fi;
797 if (req->se->conn.proto_minor >= 9) {
798 struct fuse_getattr_in *arg = (struct fuse_getattr_in *)inarg;
800 if (arg->getattr_flags & FUSE_GETATTR_FH) {
801 memset(&fi, 0, sizeof(fi));
802 fi.fh = arg->fh;
803 fip = &fi;
807 if (req->se->op.getattr) {
808 req->se->op.getattr(req, nodeid, fip);
809 } else {
810 fuse_reply_err(req, ENOSYS);
814 static void do_setattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
816 struct fuse_setattr_in *arg = (struct fuse_setattr_in *)inarg;
818 if (req->se->op.setattr) {
819 struct fuse_file_info *fi = NULL;
820 struct fuse_file_info fi_store;
821 struct stat stbuf;
822 memset(&stbuf, 0, sizeof(stbuf));
823 convert_attr(arg, &stbuf);
824 if (arg->valid & FATTR_FH) {
825 arg->valid &= ~FATTR_FH;
826 memset(&fi_store, 0, sizeof(fi_store));
827 fi = &fi_store;
828 fi->fh = arg->fh;
830 arg->valid &= FUSE_SET_ATTR_MODE | FUSE_SET_ATTR_UID |
831 FUSE_SET_ATTR_GID | FUSE_SET_ATTR_SIZE |
832 FUSE_SET_ATTR_ATIME | FUSE_SET_ATTR_MTIME |
833 FUSE_SET_ATTR_ATIME_NOW | FUSE_SET_ATTR_MTIME_NOW |
834 FUSE_SET_ATTR_CTIME;
836 req->se->op.setattr(req, nodeid, &stbuf, arg->valid, fi);
837 } else {
838 fuse_reply_err(req, ENOSYS);
842 static void do_access(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
844 struct fuse_access_in *arg = (struct fuse_access_in *)inarg;
846 if (req->se->op.access) {
847 req->se->op.access(req, nodeid, arg->mask);
848 } else {
849 fuse_reply_err(req, ENOSYS);
853 static void do_readlink(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
855 (void)inarg;
857 if (req->se->op.readlink) {
858 req->se->op.readlink(req, nodeid);
859 } else {
860 fuse_reply_err(req, ENOSYS);
864 static void do_mknod(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
866 struct fuse_mknod_in *arg = (struct fuse_mknod_in *)inarg;
867 char *name = PARAM(arg);
869 if (req->se->conn.proto_minor >= 12) {
870 req->ctx.umask = arg->umask;
871 } else {
872 name = (char *)inarg + FUSE_COMPAT_MKNOD_IN_SIZE;
875 if (req->se->op.mknod) {
876 req->se->op.mknod(req, nodeid, name, arg->mode, arg->rdev);
877 } else {
878 fuse_reply_err(req, ENOSYS);
882 static void do_mkdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
884 struct fuse_mkdir_in *arg = (struct fuse_mkdir_in *)inarg;
886 if (req->se->conn.proto_minor >= 12) {
887 req->ctx.umask = arg->umask;
890 if (req->se->op.mkdir) {
891 req->se->op.mkdir(req, nodeid, PARAM(arg), arg->mode);
892 } else {
893 fuse_reply_err(req, ENOSYS);
897 static void do_unlink(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
899 char *name = (char *)inarg;
901 if (req->se->op.unlink) {
902 req->se->op.unlink(req, nodeid, name);
903 } else {
904 fuse_reply_err(req, ENOSYS);
908 static void do_rmdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
910 char *name = (char *)inarg;
912 if (req->se->op.rmdir) {
913 req->se->op.rmdir(req, nodeid, name);
914 } else {
915 fuse_reply_err(req, ENOSYS);
919 static void do_symlink(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
921 char *name = (char *)inarg;
922 char *linkname = ((char *)inarg) + strlen((char *)inarg) + 1;
924 if (req->se->op.symlink) {
925 req->se->op.symlink(req, linkname, nodeid, name);
926 } else {
927 fuse_reply_err(req, ENOSYS);
931 static void do_rename(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
933 struct fuse_rename_in *arg = (struct fuse_rename_in *)inarg;
934 char *oldname = PARAM(arg);
935 char *newname = oldname + strlen(oldname) + 1;
937 if (req->se->op.rename) {
938 req->se->op.rename(req, nodeid, oldname, arg->newdir, newname, 0);
939 } else {
940 fuse_reply_err(req, ENOSYS);
944 static void do_rename2(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
946 struct fuse_rename2_in *arg = (struct fuse_rename2_in *)inarg;
947 char *oldname = PARAM(arg);
948 char *newname = oldname + strlen(oldname) + 1;
950 if (req->se->op.rename) {
951 req->se->op.rename(req, nodeid, oldname, arg->newdir, newname,
952 arg->flags);
953 } else {
954 fuse_reply_err(req, ENOSYS);
958 static void do_link(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
960 struct fuse_link_in *arg = (struct fuse_link_in *)inarg;
962 if (req->se->op.link) {
963 req->se->op.link(req, arg->oldnodeid, nodeid, PARAM(arg));
964 } else {
965 fuse_reply_err(req, ENOSYS);
969 static void do_create(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
971 struct fuse_create_in *arg = (struct fuse_create_in *)inarg;
973 if (req->se->op.create) {
974 struct fuse_file_info fi;
975 char *name = PARAM(arg);
977 memset(&fi, 0, sizeof(fi));
978 fi.flags = arg->flags;
980 if (req->se->conn.proto_minor >= 12) {
981 req->ctx.umask = arg->umask;
982 } else {
983 name = (char *)inarg + sizeof(struct fuse_open_in);
986 req->se->op.create(req, nodeid, name, arg->mode, &fi);
987 } else {
988 fuse_reply_err(req, ENOSYS);
992 static void do_open(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
994 struct fuse_open_in *arg = (struct fuse_open_in *)inarg;
995 struct fuse_file_info fi;
997 memset(&fi, 0, sizeof(fi));
998 fi.flags = arg->flags;
1000 if (req->se->op.open) {
1001 req->se->op.open(req, nodeid, &fi);
1002 } else {
1003 fuse_reply_open(req, &fi);
1007 static void do_read(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1009 struct fuse_read_in *arg = (struct fuse_read_in *)inarg;
1011 if (req->se->op.read) {
1012 struct fuse_file_info fi;
1014 memset(&fi, 0, sizeof(fi));
1015 fi.fh = arg->fh;
1016 if (req->se->conn.proto_minor >= 9) {
1017 fi.lock_owner = arg->lock_owner;
1018 fi.flags = arg->flags;
1020 req->se->op.read(req, nodeid, arg->size, arg->offset, &fi);
1021 } else {
1022 fuse_reply_err(req, ENOSYS);
1026 static void do_write(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1028 struct fuse_write_in *arg = (struct fuse_write_in *)inarg;
1029 struct fuse_file_info fi;
1030 char *param;
1032 memset(&fi, 0, sizeof(fi));
1033 fi.fh = arg->fh;
1034 fi.writepage = (arg->write_flags & FUSE_WRITE_CACHE) != 0;
1036 if (req->se->conn.proto_minor < 9) {
1037 param = ((char *)arg) + FUSE_COMPAT_WRITE_IN_SIZE;
1038 } else {
1039 fi.lock_owner = arg->lock_owner;
1040 fi.flags = arg->flags;
1041 param = PARAM(arg);
1044 if (req->se->op.write) {
1045 req->se->op.write(req, nodeid, param, arg->size, arg->offset, &fi);
1046 } else {
1047 fuse_reply_err(req, ENOSYS);
1051 static void do_write_buf(fuse_req_t req, fuse_ino_t nodeid, const void *inarg,
1052 const struct fuse_buf *ibuf)
1054 struct fuse_session *se = req->se;
1055 struct fuse_bufvec bufv = {
1056 .buf[0] = *ibuf,
1057 .count = 1,
1059 struct fuse_write_in *arg = (struct fuse_write_in *)inarg;
1060 struct fuse_file_info fi;
1062 memset(&fi, 0, sizeof(fi));
1063 fi.fh = arg->fh;
1064 fi.writepage = arg->write_flags & FUSE_WRITE_CACHE;
1066 if (se->conn.proto_minor < 9) {
1067 bufv.buf[0].mem = ((char *)arg) + FUSE_COMPAT_WRITE_IN_SIZE;
1068 bufv.buf[0].size -=
1069 sizeof(struct fuse_in_header) + FUSE_COMPAT_WRITE_IN_SIZE;
1070 assert(!(bufv.buf[0].flags & FUSE_BUF_IS_FD));
1071 } else {
1072 fi.lock_owner = arg->lock_owner;
1073 fi.flags = arg->flags;
1074 if (!(bufv.buf[0].flags & FUSE_BUF_IS_FD)) {
1075 bufv.buf[0].mem = PARAM(arg);
1078 bufv.buf[0].size -=
1079 sizeof(struct fuse_in_header) + sizeof(struct fuse_write_in);
1081 if (bufv.buf[0].size < arg->size) {
1082 fuse_log(FUSE_LOG_ERR, "fuse: do_write_buf: buffer size too small\n");
1083 fuse_reply_err(req, EIO);
1084 return;
1086 bufv.buf[0].size = arg->size;
1088 se->op.write_buf(req, nodeid, &bufv, arg->offset, &fi);
1091 static void do_flush(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1093 struct fuse_flush_in *arg = (struct fuse_flush_in *)inarg;
1094 struct fuse_file_info fi;
1096 memset(&fi, 0, sizeof(fi));
1097 fi.fh = arg->fh;
1098 fi.flush = 1;
1099 if (req->se->conn.proto_minor >= 7) {
1100 fi.lock_owner = arg->lock_owner;
1103 if (req->se->op.flush) {
1104 req->se->op.flush(req, nodeid, &fi);
1105 } else {
1106 fuse_reply_err(req, ENOSYS);
1110 static void do_release(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1112 struct fuse_release_in *arg = (struct fuse_release_in *)inarg;
1113 struct fuse_file_info fi;
1115 memset(&fi, 0, sizeof(fi));
1116 fi.flags = arg->flags;
1117 fi.fh = arg->fh;
1118 if (req->se->conn.proto_minor >= 8) {
1119 fi.flush = (arg->release_flags & FUSE_RELEASE_FLUSH) ? 1 : 0;
1120 fi.lock_owner = arg->lock_owner;
1122 if (arg->release_flags & FUSE_RELEASE_FLOCK_UNLOCK) {
1123 fi.flock_release = 1;
1124 fi.lock_owner = arg->lock_owner;
1127 if (req->se->op.release) {
1128 req->se->op.release(req, nodeid, &fi);
1129 } else {
1130 fuse_reply_err(req, 0);
1134 static void do_fsync(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1136 struct fuse_fsync_in *arg = (struct fuse_fsync_in *)inarg;
1137 struct fuse_file_info fi;
1138 int datasync = arg->fsync_flags & 1;
1140 memset(&fi, 0, sizeof(fi));
1141 fi.fh = arg->fh;
1143 if (req->se->op.fsync) {
1144 req->se->op.fsync(req, nodeid, datasync, &fi);
1145 } else {
1146 fuse_reply_err(req, ENOSYS);
1150 static void do_opendir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1152 struct fuse_open_in *arg = (struct fuse_open_in *)inarg;
1153 struct fuse_file_info fi;
1155 memset(&fi, 0, sizeof(fi));
1156 fi.flags = arg->flags;
1158 if (req->se->op.opendir) {
1159 req->se->op.opendir(req, nodeid, &fi);
1160 } else {
1161 fuse_reply_open(req, &fi);
1165 static void do_readdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1167 struct fuse_read_in *arg = (struct fuse_read_in *)inarg;
1168 struct fuse_file_info fi;
1170 memset(&fi, 0, sizeof(fi));
1171 fi.fh = arg->fh;
1173 if (req->se->op.readdir) {
1174 req->se->op.readdir(req, nodeid, arg->size, arg->offset, &fi);
1175 } else {
1176 fuse_reply_err(req, ENOSYS);
1180 static void do_readdirplus(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1182 struct fuse_read_in *arg = (struct fuse_read_in *)inarg;
1183 struct fuse_file_info fi;
1185 memset(&fi, 0, sizeof(fi));
1186 fi.fh = arg->fh;
1188 if (req->se->op.readdirplus) {
1189 req->se->op.readdirplus(req, nodeid, arg->size, arg->offset, &fi);
1190 } else {
1191 fuse_reply_err(req, ENOSYS);
1195 static void do_releasedir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1197 struct fuse_release_in *arg = (struct fuse_release_in *)inarg;
1198 struct fuse_file_info fi;
1200 memset(&fi, 0, sizeof(fi));
1201 fi.flags = arg->flags;
1202 fi.fh = arg->fh;
1204 if (req->se->op.releasedir) {
1205 req->se->op.releasedir(req, nodeid, &fi);
1206 } else {
1207 fuse_reply_err(req, 0);
1211 static void do_fsyncdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1213 struct fuse_fsync_in *arg = (struct fuse_fsync_in *)inarg;
1214 struct fuse_file_info fi;
1215 int datasync = arg->fsync_flags & 1;
1217 memset(&fi, 0, sizeof(fi));
1218 fi.fh = arg->fh;
1220 if (req->se->op.fsyncdir) {
1221 req->se->op.fsyncdir(req, nodeid, datasync, &fi);
1222 } else {
1223 fuse_reply_err(req, ENOSYS);
1227 static void do_statfs(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1229 (void)nodeid;
1230 (void)inarg;
1232 if (req->se->op.statfs) {
1233 req->se->op.statfs(req, nodeid);
1234 } else {
1235 struct statvfs buf = {
1236 .f_namemax = 255,
1237 .f_bsize = 512,
1239 fuse_reply_statfs(req, &buf);
1243 static void do_setxattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1245 struct fuse_setxattr_in *arg = (struct fuse_setxattr_in *)inarg;
1246 char *name = PARAM(arg);
1247 char *value = name + strlen(name) + 1;
1249 if (req->se->op.setxattr) {
1250 req->se->op.setxattr(req, nodeid, name, value, arg->size, arg->flags);
1251 } else {
1252 fuse_reply_err(req, ENOSYS);
1256 static void do_getxattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1258 struct fuse_getxattr_in *arg = (struct fuse_getxattr_in *)inarg;
1260 if (req->se->op.getxattr) {
1261 req->se->op.getxattr(req, nodeid, PARAM(arg), arg->size);
1262 } else {
1263 fuse_reply_err(req, ENOSYS);
1267 static void do_listxattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1269 struct fuse_getxattr_in *arg = (struct fuse_getxattr_in *)inarg;
1271 if (req->se->op.listxattr) {
1272 req->se->op.listxattr(req, nodeid, arg->size);
1273 } else {
1274 fuse_reply_err(req, ENOSYS);
1278 static void do_removexattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1280 char *name = (char *)inarg;
1282 if (req->se->op.removexattr) {
1283 req->se->op.removexattr(req, nodeid, name);
1284 } else {
1285 fuse_reply_err(req, ENOSYS);
1289 static void convert_fuse_file_lock(struct fuse_file_lock *fl,
1290 struct flock *flock)
1292 memset(flock, 0, sizeof(struct flock));
1293 flock->l_type = fl->type;
1294 flock->l_whence = SEEK_SET;
1295 flock->l_start = fl->start;
1296 if (fl->end == OFFSET_MAX) {
1297 flock->l_len = 0;
1298 } else {
1299 flock->l_len = fl->end - fl->start + 1;
1301 flock->l_pid = fl->pid;
1304 static void do_getlk(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1306 struct fuse_lk_in *arg = (struct fuse_lk_in *)inarg;
1307 struct fuse_file_info fi;
1308 struct flock flock;
1310 memset(&fi, 0, sizeof(fi));
1311 fi.fh = arg->fh;
1312 fi.lock_owner = arg->owner;
1314 convert_fuse_file_lock(&arg->lk, &flock);
1315 if (req->se->op.getlk) {
1316 req->se->op.getlk(req, nodeid, &fi, &flock);
1317 } else {
1318 fuse_reply_err(req, ENOSYS);
1322 static void do_setlk_common(fuse_req_t req, fuse_ino_t nodeid,
1323 const void *inarg, int sleep)
1325 struct fuse_lk_in *arg = (struct fuse_lk_in *)inarg;
1326 struct fuse_file_info fi;
1327 struct flock flock;
1329 memset(&fi, 0, sizeof(fi));
1330 fi.fh = arg->fh;
1331 fi.lock_owner = arg->owner;
1333 if (arg->lk_flags & FUSE_LK_FLOCK) {
1334 int op = 0;
1336 switch (arg->lk.type) {
1337 case F_RDLCK:
1338 op = LOCK_SH;
1339 break;
1340 case F_WRLCK:
1341 op = LOCK_EX;
1342 break;
1343 case F_UNLCK:
1344 op = LOCK_UN;
1345 break;
1347 if (!sleep) {
1348 op |= LOCK_NB;
1351 if (req->se->op.flock) {
1352 req->se->op.flock(req, nodeid, &fi, op);
1353 } else {
1354 fuse_reply_err(req, ENOSYS);
1356 } else {
1357 convert_fuse_file_lock(&arg->lk, &flock);
1358 if (req->se->op.setlk) {
1359 req->se->op.setlk(req, nodeid, &fi, &flock, sleep);
1360 } else {
1361 fuse_reply_err(req, ENOSYS);
1366 static void do_setlk(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1368 do_setlk_common(req, nodeid, inarg, 0);
1371 static void do_setlkw(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1373 do_setlk_common(req, nodeid, inarg, 1);
1376 static int find_interrupted(struct fuse_session *se, struct fuse_req *req)
1378 struct fuse_req *curr;
1380 for (curr = se->list.next; curr != &se->list; curr = curr->next) {
1381 if (curr->unique == req->u.i.unique) {
1382 fuse_interrupt_func_t func;
1383 void *data;
1385 curr->ctr++;
1386 pthread_mutex_unlock(&se->lock);
1388 /* Ugh, ugly locking */
1389 pthread_mutex_lock(&curr->lock);
1390 pthread_mutex_lock(&se->lock);
1391 curr->interrupted = 1;
1392 func = curr->u.ni.func;
1393 data = curr->u.ni.data;
1394 pthread_mutex_unlock(&se->lock);
1395 if (func) {
1396 func(curr, data);
1398 pthread_mutex_unlock(&curr->lock);
1400 pthread_mutex_lock(&se->lock);
1401 curr->ctr--;
1402 if (!curr->ctr) {
1403 destroy_req(curr);
1406 return 1;
1409 for (curr = se->interrupts.next; curr != &se->interrupts;
1410 curr = curr->next) {
1411 if (curr->u.i.unique == req->u.i.unique) {
1412 return 1;
1415 return 0;
1418 static void do_interrupt(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1420 struct fuse_interrupt_in *arg = (struct fuse_interrupt_in *)inarg;
1421 struct fuse_session *se = req->se;
1423 (void)nodeid;
1424 if (se->debug) {
1425 fuse_log(FUSE_LOG_DEBUG, "INTERRUPT: %llu\n",
1426 (unsigned long long)arg->unique);
1429 req->u.i.unique = arg->unique;
1431 pthread_mutex_lock(&se->lock);
1432 if (find_interrupted(se, req)) {
1433 destroy_req(req);
1434 } else {
1435 list_add_req(req, &se->interrupts);
1437 pthread_mutex_unlock(&se->lock);
1440 static struct fuse_req *check_interrupt(struct fuse_session *se,
1441 struct fuse_req *req)
1443 struct fuse_req *curr;
1445 for (curr = se->interrupts.next; curr != &se->interrupts;
1446 curr = curr->next) {
1447 if (curr->u.i.unique == req->unique) {
1448 req->interrupted = 1;
1449 list_del_req(curr);
1450 free(curr);
1451 return NULL;
1454 curr = se->interrupts.next;
1455 if (curr != &se->interrupts) {
1456 list_del_req(curr);
1457 list_init_req(curr);
1458 return curr;
1459 } else {
1460 return NULL;
1464 static void do_bmap(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1466 struct fuse_bmap_in *arg = (struct fuse_bmap_in *)inarg;
1468 if (req->se->op.bmap) {
1469 req->se->op.bmap(req, nodeid, arg->blocksize, arg->block);
1470 } else {
1471 fuse_reply_err(req, ENOSYS);
1475 static void do_ioctl(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1477 struct fuse_ioctl_in *arg = (struct fuse_ioctl_in *)inarg;
1478 unsigned int flags = arg->flags;
1479 void *in_buf = arg->in_size ? PARAM(arg) : NULL;
1480 struct fuse_file_info fi;
1482 if (flags & FUSE_IOCTL_DIR && !(req->se->conn.want & FUSE_CAP_IOCTL_DIR)) {
1483 fuse_reply_err(req, ENOTTY);
1484 return;
1487 memset(&fi, 0, sizeof(fi));
1488 fi.fh = arg->fh;
1490 if (sizeof(void *) == 4 && req->se->conn.proto_minor >= 16 &&
1491 !(flags & FUSE_IOCTL_32BIT)) {
1492 req->ioctl_64bit = 1;
1495 if (req->se->op.ioctl) {
1496 req->se->op.ioctl(req, nodeid, arg->cmd, (void *)(uintptr_t)arg->arg,
1497 &fi, flags, in_buf, arg->in_size, arg->out_size);
1498 } else {
1499 fuse_reply_err(req, ENOSYS);
1503 void fuse_pollhandle_destroy(struct fuse_pollhandle *ph)
1505 free(ph);
1508 static void do_poll(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1510 struct fuse_poll_in *arg = (struct fuse_poll_in *)inarg;
1511 struct fuse_file_info fi;
1513 memset(&fi, 0, sizeof(fi));
1514 fi.fh = arg->fh;
1515 fi.poll_events = arg->events;
1517 if (req->se->op.poll) {
1518 struct fuse_pollhandle *ph = NULL;
1520 if (arg->flags & FUSE_POLL_SCHEDULE_NOTIFY) {
1521 ph = malloc(sizeof(struct fuse_pollhandle));
1522 if (ph == NULL) {
1523 fuse_reply_err(req, ENOMEM);
1524 return;
1526 ph->kh = arg->kh;
1527 ph->se = req->se;
1530 req->se->op.poll(req, nodeid, &fi, ph);
1531 } else {
1532 fuse_reply_err(req, ENOSYS);
1536 static void do_fallocate(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1538 struct fuse_fallocate_in *arg = (struct fuse_fallocate_in *)inarg;
1539 struct fuse_file_info fi;
1541 memset(&fi, 0, sizeof(fi));
1542 fi.fh = arg->fh;
1544 if (req->se->op.fallocate) {
1545 req->se->op.fallocate(req, nodeid, arg->mode, arg->offset, arg->length,
1546 &fi);
1547 } else {
1548 fuse_reply_err(req, ENOSYS);
1552 static void do_copy_file_range(fuse_req_t req, fuse_ino_t nodeid_in,
1553 const void *inarg)
1555 struct fuse_copy_file_range_in *arg =
1556 (struct fuse_copy_file_range_in *)inarg;
1557 struct fuse_file_info fi_in, fi_out;
1559 memset(&fi_in, 0, sizeof(fi_in));
1560 fi_in.fh = arg->fh_in;
1562 memset(&fi_out, 0, sizeof(fi_out));
1563 fi_out.fh = arg->fh_out;
1566 if (req->se->op.copy_file_range) {
1567 req->se->op.copy_file_range(req, nodeid_in, arg->off_in, &fi_in,
1568 arg->nodeid_out, arg->off_out, &fi_out,
1569 arg->len, arg->flags);
1570 } else {
1571 fuse_reply_err(req, ENOSYS);
1575 static void do_lseek(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1577 struct fuse_lseek_in *arg = (struct fuse_lseek_in *)inarg;
1578 struct fuse_file_info fi;
1580 memset(&fi, 0, sizeof(fi));
1581 fi.fh = arg->fh;
1583 if (req->se->op.lseek) {
1584 req->se->op.lseek(req, nodeid, arg->offset, arg->whence, &fi);
1585 } else {
1586 fuse_reply_err(req, ENOSYS);
1590 static void do_init(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1592 struct fuse_init_in *arg = (struct fuse_init_in *)inarg;
1593 struct fuse_init_out outarg;
1594 struct fuse_session *se = req->se;
1595 size_t bufsize = se->bufsize;
1596 size_t outargsize = sizeof(outarg);
1598 (void)nodeid;
1599 if (se->debug) {
1600 fuse_log(FUSE_LOG_DEBUG, "INIT: %u.%u\n", arg->major, arg->minor);
1601 if (arg->major == 7 && arg->minor >= 6) {
1602 fuse_log(FUSE_LOG_DEBUG, "flags=0x%08x\n", arg->flags);
1603 fuse_log(FUSE_LOG_DEBUG, "max_readahead=0x%08x\n",
1604 arg->max_readahead);
1607 se->conn.proto_major = arg->major;
1608 se->conn.proto_minor = arg->minor;
1609 se->conn.capable = 0;
1610 se->conn.want = 0;
1612 memset(&outarg, 0, sizeof(outarg));
1613 outarg.major = FUSE_KERNEL_VERSION;
1614 outarg.minor = FUSE_KERNEL_MINOR_VERSION;
1616 if (arg->major < 7) {
1617 fuse_log(FUSE_LOG_ERR, "fuse: unsupported protocol version: %u.%u\n",
1618 arg->major, arg->minor);
1619 fuse_reply_err(req, EPROTO);
1620 return;
1623 if (arg->major > 7) {
1624 /* Wait for a second INIT request with a 7.X version */
1625 send_reply_ok(req, &outarg, sizeof(outarg));
1626 return;
1629 if (arg->minor >= 6) {
1630 if (arg->max_readahead < se->conn.max_readahead) {
1631 se->conn.max_readahead = arg->max_readahead;
1633 if (arg->flags & FUSE_ASYNC_READ) {
1634 se->conn.capable |= FUSE_CAP_ASYNC_READ;
1636 if (arg->flags & FUSE_POSIX_LOCKS) {
1637 se->conn.capable |= FUSE_CAP_POSIX_LOCKS;
1639 if (arg->flags & FUSE_ATOMIC_O_TRUNC) {
1640 se->conn.capable |= FUSE_CAP_ATOMIC_O_TRUNC;
1642 if (arg->flags & FUSE_EXPORT_SUPPORT) {
1643 se->conn.capable |= FUSE_CAP_EXPORT_SUPPORT;
1645 if (arg->flags & FUSE_DONT_MASK) {
1646 se->conn.capable |= FUSE_CAP_DONT_MASK;
1648 if (arg->flags & FUSE_FLOCK_LOCKS) {
1649 se->conn.capable |= FUSE_CAP_FLOCK_LOCKS;
1651 if (arg->flags & FUSE_AUTO_INVAL_DATA) {
1652 se->conn.capable |= FUSE_CAP_AUTO_INVAL_DATA;
1654 if (arg->flags & FUSE_DO_READDIRPLUS) {
1655 se->conn.capable |= FUSE_CAP_READDIRPLUS;
1657 if (arg->flags & FUSE_READDIRPLUS_AUTO) {
1658 se->conn.capable |= FUSE_CAP_READDIRPLUS_AUTO;
1660 if (arg->flags & FUSE_ASYNC_DIO) {
1661 se->conn.capable |= FUSE_CAP_ASYNC_DIO;
1663 if (arg->flags & FUSE_WRITEBACK_CACHE) {
1664 se->conn.capable |= FUSE_CAP_WRITEBACK_CACHE;
1666 if (arg->flags & FUSE_NO_OPEN_SUPPORT) {
1667 se->conn.capable |= FUSE_CAP_NO_OPEN_SUPPORT;
1669 if (arg->flags & FUSE_PARALLEL_DIROPS) {
1670 se->conn.capable |= FUSE_CAP_PARALLEL_DIROPS;
1672 if (arg->flags & FUSE_POSIX_ACL) {
1673 se->conn.capable |= FUSE_CAP_POSIX_ACL;
1675 if (arg->flags & FUSE_HANDLE_KILLPRIV) {
1676 se->conn.capable |= FUSE_CAP_HANDLE_KILLPRIV;
1678 if (arg->flags & FUSE_NO_OPENDIR_SUPPORT) {
1679 se->conn.capable |= FUSE_CAP_NO_OPENDIR_SUPPORT;
1681 if (!(arg->flags & FUSE_MAX_PAGES)) {
1682 size_t max_bufsize =
1683 FUSE_DEFAULT_MAX_PAGES_PER_REQ * getpagesize() +
1684 FUSE_BUFFER_HEADER_SIZE;
1685 if (bufsize > max_bufsize) {
1686 bufsize = max_bufsize;
1689 } else {
1690 se->conn.max_readahead = 0;
1693 if (se->conn.proto_minor >= 14) {
1694 #ifdef HAVE_SPLICE
1695 #ifdef HAVE_VMSPLICE
1696 se->conn.capable |= FUSE_CAP_SPLICE_WRITE | FUSE_CAP_SPLICE_MOVE;
1697 #endif
1698 se->conn.capable |= FUSE_CAP_SPLICE_READ;
1699 #endif
1701 if (se->conn.proto_minor >= 18) {
1702 se->conn.capable |= FUSE_CAP_IOCTL_DIR;
1706 * Default settings for modern filesystems.
1708 * Most of these capabilities were disabled by default in
1709 * libfuse2 for backwards compatibility reasons. In libfuse3,
1710 * we can finally enable them by default (as long as they're
1711 * supported by the kernel).
1713 #define LL_SET_DEFAULT(cond, cap) \
1714 if ((cond) && (se->conn.capable & (cap))) \
1715 se->conn.want |= (cap)
1716 LL_SET_DEFAULT(1, FUSE_CAP_ASYNC_READ);
1717 LL_SET_DEFAULT(1, FUSE_CAP_PARALLEL_DIROPS);
1718 LL_SET_DEFAULT(1, FUSE_CAP_AUTO_INVAL_DATA);
1719 LL_SET_DEFAULT(1, FUSE_CAP_HANDLE_KILLPRIV);
1720 LL_SET_DEFAULT(1, FUSE_CAP_ASYNC_DIO);
1721 LL_SET_DEFAULT(1, FUSE_CAP_IOCTL_DIR);
1722 LL_SET_DEFAULT(1, FUSE_CAP_ATOMIC_O_TRUNC);
1723 LL_SET_DEFAULT(se->op.write_buf, FUSE_CAP_SPLICE_READ);
1724 LL_SET_DEFAULT(se->op.getlk && se->op.setlk, FUSE_CAP_POSIX_LOCKS);
1725 LL_SET_DEFAULT(se->op.flock, FUSE_CAP_FLOCK_LOCKS);
1726 LL_SET_DEFAULT(se->op.readdirplus, FUSE_CAP_READDIRPLUS);
1727 LL_SET_DEFAULT(se->op.readdirplus && se->op.readdir,
1728 FUSE_CAP_READDIRPLUS_AUTO);
1729 se->conn.time_gran = 1;
1731 if (bufsize < FUSE_MIN_READ_BUFFER) {
1732 fuse_log(FUSE_LOG_ERR, "fuse: warning: buffer size too small: %zu\n",
1733 bufsize);
1734 bufsize = FUSE_MIN_READ_BUFFER;
1736 se->bufsize = bufsize;
1738 if (se->conn.max_write > bufsize - FUSE_BUFFER_HEADER_SIZE) {
1739 se->conn.max_write = bufsize - FUSE_BUFFER_HEADER_SIZE;
1742 se->got_init = 1;
1743 if (se->op.init) {
1744 se->op.init(se->userdata, &se->conn);
1747 if (se->conn.want & (~se->conn.capable)) {
1748 fuse_log(FUSE_LOG_ERR,
1749 "fuse: error: filesystem requested capabilities "
1750 "0x%x that are not supported by kernel, aborting.\n",
1751 se->conn.want & (~se->conn.capable));
1752 fuse_reply_err(req, EPROTO);
1753 se->error = -EPROTO;
1754 fuse_session_exit(se);
1755 return;
1758 if (se->conn.max_write < bufsize - FUSE_BUFFER_HEADER_SIZE) {
1759 se->bufsize = se->conn.max_write + FUSE_BUFFER_HEADER_SIZE;
1761 if (arg->flags & FUSE_MAX_PAGES) {
1762 outarg.flags |= FUSE_MAX_PAGES;
1763 outarg.max_pages = (se->conn.max_write - 1) / getpagesize() + 1;
1767 * Always enable big writes, this is superseded
1768 * by the max_write option
1770 outarg.flags |= FUSE_BIG_WRITES;
1772 if (se->conn.want & FUSE_CAP_ASYNC_READ) {
1773 outarg.flags |= FUSE_ASYNC_READ;
1775 if (se->conn.want & FUSE_CAP_POSIX_LOCKS) {
1776 outarg.flags |= FUSE_POSIX_LOCKS;
1778 if (se->conn.want & FUSE_CAP_ATOMIC_O_TRUNC) {
1779 outarg.flags |= FUSE_ATOMIC_O_TRUNC;
1781 if (se->conn.want & FUSE_CAP_EXPORT_SUPPORT) {
1782 outarg.flags |= FUSE_EXPORT_SUPPORT;
1784 if (se->conn.want & FUSE_CAP_DONT_MASK) {
1785 outarg.flags |= FUSE_DONT_MASK;
1787 if (se->conn.want & FUSE_CAP_FLOCK_LOCKS) {
1788 outarg.flags |= FUSE_FLOCK_LOCKS;
1790 if (se->conn.want & FUSE_CAP_AUTO_INVAL_DATA) {
1791 outarg.flags |= FUSE_AUTO_INVAL_DATA;
1793 if (se->conn.want & FUSE_CAP_READDIRPLUS) {
1794 outarg.flags |= FUSE_DO_READDIRPLUS;
1796 if (se->conn.want & FUSE_CAP_READDIRPLUS_AUTO) {
1797 outarg.flags |= FUSE_READDIRPLUS_AUTO;
1799 if (se->conn.want & FUSE_CAP_ASYNC_DIO) {
1800 outarg.flags |= FUSE_ASYNC_DIO;
1802 if (se->conn.want & FUSE_CAP_WRITEBACK_CACHE) {
1803 outarg.flags |= FUSE_WRITEBACK_CACHE;
1805 if (se->conn.want & FUSE_CAP_POSIX_ACL) {
1806 outarg.flags |= FUSE_POSIX_ACL;
1808 outarg.max_readahead = se->conn.max_readahead;
1809 outarg.max_write = se->conn.max_write;
1810 if (se->conn.proto_minor >= 13) {
1811 if (se->conn.max_background >= (1 << 16)) {
1812 se->conn.max_background = (1 << 16) - 1;
1814 if (se->conn.congestion_threshold > se->conn.max_background) {
1815 se->conn.congestion_threshold = se->conn.max_background;
1817 if (!se->conn.congestion_threshold) {
1818 se->conn.congestion_threshold = se->conn.max_background * 3 / 4;
1821 outarg.max_background = se->conn.max_background;
1822 outarg.congestion_threshold = se->conn.congestion_threshold;
1824 if (se->conn.proto_minor >= 23) {
1825 outarg.time_gran = se->conn.time_gran;
1828 if (se->debug) {
1829 fuse_log(FUSE_LOG_DEBUG, " INIT: %u.%u\n", outarg.major,
1830 outarg.minor);
1831 fuse_log(FUSE_LOG_DEBUG, " flags=0x%08x\n", outarg.flags);
1832 fuse_log(FUSE_LOG_DEBUG, " max_readahead=0x%08x\n",
1833 outarg.max_readahead);
1834 fuse_log(FUSE_LOG_DEBUG, " max_write=0x%08x\n", outarg.max_write);
1835 fuse_log(FUSE_LOG_DEBUG, " max_background=%i\n",
1836 outarg.max_background);
1837 fuse_log(FUSE_LOG_DEBUG, " congestion_threshold=%i\n",
1838 outarg.congestion_threshold);
1839 fuse_log(FUSE_LOG_DEBUG, " time_gran=%u\n", outarg.time_gran);
1841 if (arg->minor < 5) {
1842 outargsize = FUSE_COMPAT_INIT_OUT_SIZE;
1843 } else if (arg->minor < 23) {
1844 outargsize = FUSE_COMPAT_22_INIT_OUT_SIZE;
1847 send_reply_ok(req, &outarg, outargsize);
1850 static void do_destroy(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1852 struct fuse_session *se = req->se;
1854 (void)nodeid;
1855 (void)inarg;
1857 se->got_destroy = 1;
1858 if (se->op.destroy) {
1859 se->op.destroy(se->userdata);
1862 send_reply_ok(req, NULL, 0);
1865 static void list_del_nreq(struct fuse_notify_req *nreq)
1867 struct fuse_notify_req *prev = nreq->prev;
1868 struct fuse_notify_req *next = nreq->next;
1869 prev->next = next;
1870 next->prev = prev;
1873 static void list_add_nreq(struct fuse_notify_req *nreq,
1874 struct fuse_notify_req *next)
1876 struct fuse_notify_req *prev = next->prev;
1877 nreq->next = next;
1878 nreq->prev = prev;
1879 prev->next = nreq;
1880 next->prev = nreq;
1883 static void list_init_nreq(struct fuse_notify_req *nreq)
1885 nreq->next = nreq;
1886 nreq->prev = nreq;
1889 static void do_notify_reply(fuse_req_t req, fuse_ino_t nodeid,
1890 const void *inarg, const struct fuse_buf *buf)
1892 struct fuse_session *se = req->se;
1893 struct fuse_notify_req *nreq;
1894 struct fuse_notify_req *head;
1896 pthread_mutex_lock(&se->lock);
1897 head = &se->notify_list;
1898 for (nreq = head->next; nreq != head; nreq = nreq->next) {
1899 if (nreq->unique == req->unique) {
1900 list_del_nreq(nreq);
1901 break;
1904 pthread_mutex_unlock(&se->lock);
1906 if (nreq != head) {
1907 nreq->reply(nreq, req, nodeid, inarg, buf);
1911 static int send_notify_iov(struct fuse_session *se, int notify_code,
1912 struct iovec *iov, int count)
1914 struct fuse_out_header out;
1916 if (!se->got_init) {
1917 return -ENOTCONN;
1920 out.unique = 0;
1921 out.error = notify_code;
1922 iov[0].iov_base = &out;
1923 iov[0].iov_len = sizeof(struct fuse_out_header);
1925 return fuse_send_msg(se, NULL, iov, count);
1928 int fuse_lowlevel_notify_poll(struct fuse_pollhandle *ph)
1930 if (ph != NULL) {
1931 struct fuse_notify_poll_wakeup_out outarg;
1932 struct iovec iov[2];
1934 outarg.kh = ph->kh;
1936 iov[1].iov_base = &outarg;
1937 iov[1].iov_len = sizeof(outarg);
1939 return send_notify_iov(ph->se, FUSE_NOTIFY_POLL, iov, 2);
1940 } else {
1941 return 0;
1945 int fuse_lowlevel_notify_inval_inode(struct fuse_session *se, fuse_ino_t ino,
1946 off_t off, off_t len)
1948 struct fuse_notify_inval_inode_out outarg;
1949 struct iovec iov[2];
1951 if (!se) {
1952 return -EINVAL;
1955 if (se->conn.proto_major < 6 || se->conn.proto_minor < 12) {
1956 return -ENOSYS;
1959 outarg.ino = ino;
1960 outarg.off = off;
1961 outarg.len = len;
1963 iov[1].iov_base = &outarg;
1964 iov[1].iov_len = sizeof(outarg);
1966 return send_notify_iov(se, FUSE_NOTIFY_INVAL_INODE, iov, 2);
1969 int fuse_lowlevel_notify_inval_entry(struct fuse_session *se, fuse_ino_t parent,
1970 const char *name, size_t namelen)
1972 struct fuse_notify_inval_entry_out outarg;
1973 struct iovec iov[3];
1975 if (!se) {
1976 return -EINVAL;
1979 if (se->conn.proto_major < 6 || se->conn.proto_minor < 12) {
1980 return -ENOSYS;
1983 outarg.parent = parent;
1984 outarg.namelen = namelen;
1985 outarg.padding = 0;
1987 iov[1].iov_base = &outarg;
1988 iov[1].iov_len = sizeof(outarg);
1989 iov[2].iov_base = (void *)name;
1990 iov[2].iov_len = namelen + 1;
1992 return send_notify_iov(se, FUSE_NOTIFY_INVAL_ENTRY, iov, 3);
1995 int fuse_lowlevel_notify_delete(struct fuse_session *se, fuse_ino_t parent,
1996 fuse_ino_t child, const char *name,
1997 size_t namelen)
1999 struct fuse_notify_delete_out outarg;
2000 struct iovec iov[3];
2002 if (!se) {
2003 return -EINVAL;
2006 if (se->conn.proto_major < 6 || se->conn.proto_minor < 18) {
2007 return -ENOSYS;
2010 outarg.parent = parent;
2011 outarg.child = child;
2012 outarg.namelen = namelen;
2013 outarg.padding = 0;
2015 iov[1].iov_base = &outarg;
2016 iov[1].iov_len = sizeof(outarg);
2017 iov[2].iov_base = (void *)name;
2018 iov[2].iov_len = namelen + 1;
2020 return send_notify_iov(se, FUSE_NOTIFY_DELETE, iov, 3);
2023 int fuse_lowlevel_notify_store(struct fuse_session *se, fuse_ino_t ino,
2024 off_t offset, struct fuse_bufvec *bufv,
2025 enum fuse_buf_copy_flags flags)
2027 struct fuse_out_header out;
2028 struct fuse_notify_store_out outarg;
2029 struct iovec iov[3];
2030 size_t size = fuse_buf_size(bufv);
2031 int res;
2033 if (!se) {
2034 return -EINVAL;
2037 if (se->conn.proto_major < 6 || se->conn.proto_minor < 15) {
2038 return -ENOSYS;
2041 out.unique = 0;
2042 out.error = FUSE_NOTIFY_STORE;
2044 outarg.nodeid = ino;
2045 outarg.offset = offset;
2046 outarg.size = size;
2047 outarg.padding = 0;
2049 iov[0].iov_base = &out;
2050 iov[0].iov_len = sizeof(out);
2051 iov[1].iov_base = &outarg;
2052 iov[1].iov_len = sizeof(outarg);
2054 res = fuse_send_data_iov(se, NULL, iov, 2, bufv, flags);
2055 if (res > 0) {
2056 res = -res;
2059 return res;
2062 struct fuse_retrieve_req {
2063 struct fuse_notify_req nreq;
2064 void *cookie;
2067 static void fuse_ll_retrieve_reply(struct fuse_notify_req *nreq, fuse_req_t req,
2068 fuse_ino_t ino, const void *inarg,
2069 const struct fuse_buf *ibuf)
2071 struct fuse_session *se = req->se;
2072 struct fuse_retrieve_req *rreq =
2073 container_of(nreq, struct fuse_retrieve_req, nreq);
2074 const struct fuse_notify_retrieve_in *arg = inarg;
2075 struct fuse_bufvec bufv = {
2076 .buf[0] = *ibuf,
2077 .count = 1,
2080 if (!(bufv.buf[0].flags & FUSE_BUF_IS_FD)) {
2081 bufv.buf[0].mem = PARAM(arg);
2084 bufv.buf[0].size -=
2085 sizeof(struct fuse_in_header) + sizeof(struct fuse_notify_retrieve_in);
2087 if (bufv.buf[0].size < arg->size) {
2088 fuse_log(FUSE_LOG_ERR, "fuse: retrieve reply: buffer size too small\n");
2089 fuse_reply_none(req);
2090 goto out;
2092 bufv.buf[0].size = arg->size;
2094 if (se->op.retrieve_reply) {
2095 se->op.retrieve_reply(req, rreq->cookie, ino, arg->offset, &bufv);
2096 } else {
2097 fuse_reply_none(req);
2099 out:
2100 free(rreq);
2103 int fuse_lowlevel_notify_retrieve(struct fuse_session *se, fuse_ino_t ino,
2104 size_t size, off_t offset, void *cookie)
2106 struct fuse_notify_retrieve_out outarg;
2107 struct iovec iov[2];
2108 struct fuse_retrieve_req *rreq;
2109 int err;
2111 if (!se) {
2112 return -EINVAL;
2115 if (se->conn.proto_major < 6 || se->conn.proto_minor < 15) {
2116 return -ENOSYS;
2119 rreq = malloc(sizeof(*rreq));
2120 if (rreq == NULL) {
2121 return -ENOMEM;
2124 pthread_mutex_lock(&se->lock);
2125 rreq->cookie = cookie;
2126 rreq->nreq.unique = se->notify_ctr++;
2127 rreq->nreq.reply = fuse_ll_retrieve_reply;
2128 list_add_nreq(&rreq->nreq, &se->notify_list);
2129 pthread_mutex_unlock(&se->lock);
2131 outarg.notify_unique = rreq->nreq.unique;
2132 outarg.nodeid = ino;
2133 outarg.offset = offset;
2134 outarg.size = size;
2135 outarg.padding = 0;
2137 iov[1].iov_base = &outarg;
2138 iov[1].iov_len = sizeof(outarg);
2140 err = send_notify_iov(se, FUSE_NOTIFY_RETRIEVE, iov, 2);
2141 if (err) {
2142 pthread_mutex_lock(&se->lock);
2143 list_del_nreq(&rreq->nreq);
2144 pthread_mutex_unlock(&se->lock);
2145 free(rreq);
2148 return err;
2151 void *fuse_req_userdata(fuse_req_t req)
2153 return req->se->userdata;
2156 const struct fuse_ctx *fuse_req_ctx(fuse_req_t req)
2158 return &req->ctx;
2161 void fuse_req_interrupt_func(fuse_req_t req, fuse_interrupt_func_t func,
2162 void *data)
2164 pthread_mutex_lock(&req->lock);
2165 pthread_mutex_lock(&req->se->lock);
2166 req->u.ni.func = func;
2167 req->u.ni.data = data;
2168 pthread_mutex_unlock(&req->se->lock);
2169 if (req->interrupted && func) {
2170 func(req, data);
2172 pthread_mutex_unlock(&req->lock);
2175 int fuse_req_interrupted(fuse_req_t req)
2177 int interrupted;
2179 pthread_mutex_lock(&req->se->lock);
2180 interrupted = req->interrupted;
2181 pthread_mutex_unlock(&req->se->lock);
2183 return interrupted;
2186 static struct {
2187 void (*func)(fuse_req_t, fuse_ino_t, const void *);
2188 const char *name;
2189 } fuse_ll_ops[] = {
2190 [FUSE_LOOKUP] = { do_lookup, "LOOKUP" },
2191 [FUSE_FORGET] = { do_forget, "FORGET" },
2192 [FUSE_GETATTR] = { do_getattr, "GETATTR" },
2193 [FUSE_SETATTR] = { do_setattr, "SETATTR" },
2194 [FUSE_READLINK] = { do_readlink, "READLINK" },
2195 [FUSE_SYMLINK] = { do_symlink, "SYMLINK" },
2196 [FUSE_MKNOD] = { do_mknod, "MKNOD" },
2197 [FUSE_MKDIR] = { do_mkdir, "MKDIR" },
2198 [FUSE_UNLINK] = { do_unlink, "UNLINK" },
2199 [FUSE_RMDIR] = { do_rmdir, "RMDIR" },
2200 [FUSE_RENAME] = { do_rename, "RENAME" },
2201 [FUSE_LINK] = { do_link, "LINK" },
2202 [FUSE_OPEN] = { do_open, "OPEN" },
2203 [FUSE_READ] = { do_read, "READ" },
2204 [FUSE_WRITE] = { do_write, "WRITE" },
2205 [FUSE_STATFS] = { do_statfs, "STATFS" },
2206 [FUSE_RELEASE] = { do_release, "RELEASE" },
2207 [FUSE_FSYNC] = { do_fsync, "FSYNC" },
2208 [FUSE_SETXATTR] = { do_setxattr, "SETXATTR" },
2209 [FUSE_GETXATTR] = { do_getxattr, "GETXATTR" },
2210 [FUSE_LISTXATTR] = { do_listxattr, "LISTXATTR" },
2211 [FUSE_REMOVEXATTR] = { do_removexattr, "REMOVEXATTR" },
2212 [FUSE_FLUSH] = { do_flush, "FLUSH" },
2213 [FUSE_INIT] = { do_init, "INIT" },
2214 [FUSE_OPENDIR] = { do_opendir, "OPENDIR" },
2215 [FUSE_READDIR] = { do_readdir, "READDIR" },
2216 [FUSE_RELEASEDIR] = { do_releasedir, "RELEASEDIR" },
2217 [FUSE_FSYNCDIR] = { do_fsyncdir, "FSYNCDIR" },
2218 [FUSE_GETLK] = { do_getlk, "GETLK" },
2219 [FUSE_SETLK] = { do_setlk, "SETLK" },
2220 [FUSE_SETLKW] = { do_setlkw, "SETLKW" },
2221 [FUSE_ACCESS] = { do_access, "ACCESS" },
2222 [FUSE_CREATE] = { do_create, "CREATE" },
2223 [FUSE_INTERRUPT] = { do_interrupt, "INTERRUPT" },
2224 [FUSE_BMAP] = { do_bmap, "BMAP" },
2225 [FUSE_IOCTL] = { do_ioctl, "IOCTL" },
2226 [FUSE_POLL] = { do_poll, "POLL" },
2227 [FUSE_FALLOCATE] = { do_fallocate, "FALLOCATE" },
2228 [FUSE_DESTROY] = { do_destroy, "DESTROY" },
2229 [FUSE_NOTIFY_REPLY] = { (void *)1, "NOTIFY_REPLY" },
2230 [FUSE_BATCH_FORGET] = { do_batch_forget, "BATCH_FORGET" },
2231 [FUSE_READDIRPLUS] = { do_readdirplus, "READDIRPLUS" },
2232 [FUSE_RENAME2] = { do_rename2, "RENAME2" },
2233 [FUSE_COPY_FILE_RANGE] = { do_copy_file_range, "COPY_FILE_RANGE" },
2234 [FUSE_LSEEK] = { do_lseek, "LSEEK" },
2237 #define FUSE_MAXOP (sizeof(fuse_ll_ops) / sizeof(fuse_ll_ops[0]))
2239 static const char *opname(enum fuse_opcode opcode)
2241 if (opcode >= FUSE_MAXOP || !fuse_ll_ops[opcode].name) {
2242 return "???";
2243 } else {
2244 return fuse_ll_ops[opcode].name;
2248 void fuse_session_process_buf(struct fuse_session *se,
2249 const struct fuse_buf *buf)
2251 fuse_session_process_buf_int(se, buf, NULL);
2254 void fuse_session_process_buf_int(struct fuse_session *se,
2255 const struct fuse_buf *buf,
2256 struct fuse_chan *ch)
2258 struct fuse_in_header *in;
2259 const void *inarg;
2260 struct fuse_req *req;
2261 int err;
2263 in = buf->mem;
2265 if (se->debug) {
2266 fuse_log(FUSE_LOG_DEBUG,
2267 "unique: %llu, opcode: %s (%i), nodeid: %llu, insize: %zu, "
2268 "pid: %u\n",
2269 (unsigned long long)in->unique,
2270 opname((enum fuse_opcode)in->opcode), in->opcode,
2271 (unsigned long long)in->nodeid, buf->size, in->pid);
2274 req = fuse_ll_alloc_req(se);
2275 if (req == NULL) {
2276 struct fuse_out_header out = {
2277 .unique = in->unique,
2278 .error = -ENOMEM,
2280 struct iovec iov = {
2281 .iov_base = &out,
2282 .iov_len = sizeof(struct fuse_out_header),
2285 fuse_send_msg(se, ch, &iov, 1);
2286 return;
2289 req->unique = in->unique;
2290 req->ctx.uid = in->uid;
2291 req->ctx.gid = in->gid;
2292 req->ctx.pid = in->pid;
2293 req->ch = ch;
2295 err = EIO;
2296 if (!se->got_init) {
2297 enum fuse_opcode expected;
2299 expected = se->cuse_data ? CUSE_INIT : FUSE_INIT;
2300 if (in->opcode != expected) {
2301 goto reply_err;
2303 } else if (in->opcode == FUSE_INIT || in->opcode == CUSE_INIT) {
2304 goto reply_err;
2307 err = EACCES;
2308 /* Implement -o allow_root */
2309 if (se->deny_others && in->uid != se->owner && in->uid != 0 &&
2310 in->opcode != FUSE_INIT && in->opcode != FUSE_READ &&
2311 in->opcode != FUSE_WRITE && in->opcode != FUSE_FSYNC &&
2312 in->opcode != FUSE_RELEASE && in->opcode != FUSE_READDIR &&
2313 in->opcode != FUSE_FSYNCDIR && in->opcode != FUSE_RELEASEDIR &&
2314 in->opcode != FUSE_NOTIFY_REPLY && in->opcode != FUSE_READDIRPLUS) {
2315 goto reply_err;
2318 err = ENOSYS;
2319 if (in->opcode >= FUSE_MAXOP || !fuse_ll_ops[in->opcode].func) {
2320 goto reply_err;
2322 if (in->opcode != FUSE_INTERRUPT) {
2323 struct fuse_req *intr;
2324 pthread_mutex_lock(&se->lock);
2325 intr = check_interrupt(se, req);
2326 list_add_req(req, &se->list);
2327 pthread_mutex_unlock(&se->lock);
2328 if (intr) {
2329 fuse_reply_err(intr, EAGAIN);
2333 inarg = (void *)&in[1];
2334 if (in->opcode == FUSE_WRITE && se->op.write_buf) {
2335 do_write_buf(req, in->nodeid, inarg, buf);
2336 } else if (in->opcode == FUSE_NOTIFY_REPLY) {
2337 do_notify_reply(req, in->nodeid, inarg, buf);
2338 } else {
2339 fuse_ll_ops[in->opcode].func(req, in->nodeid, inarg);
2342 return;
2344 reply_err:
2345 fuse_reply_err(req, err);
2348 #define LL_OPTION(n, o, v) \
2350 n, offsetof(struct fuse_session, o), v \
2353 static const struct fuse_opt fuse_ll_opts[] = {
2354 LL_OPTION("debug", debug, 1), LL_OPTION("-d", debug, 1),
2355 LL_OPTION("--debug", debug, 1), LL_OPTION("allow_root", deny_others, 1),
2356 FUSE_OPT_END
2359 void fuse_lowlevel_version(void)
2361 printf("using FUSE kernel interface version %i.%i\n", FUSE_KERNEL_VERSION,
2362 FUSE_KERNEL_MINOR_VERSION);
2365 void fuse_lowlevel_help(void)
2368 * These are not all options, but the ones that are
2369 * potentially of interest to an end-user
2371 printf(" -o allow_root allow access by root\n");
2374 void fuse_session_destroy(struct fuse_session *se)
2376 if (se->got_init && !se->got_destroy) {
2377 if (se->op.destroy) {
2378 se->op.destroy(se->userdata);
2381 pthread_mutex_destroy(&se->lock);
2382 free(se->cuse_data);
2383 if (se->fd != -1) {
2384 close(se->fd);
2386 free(se);
2390 struct fuse_session *fuse_session_new(struct fuse_args *args,
2391 const struct fuse_lowlevel_ops *op,
2392 size_t op_size, void *userdata)
2394 struct fuse_session *se;
2396 if (sizeof(struct fuse_lowlevel_ops) < op_size) {
2397 fuse_log(
2398 FUSE_LOG_ERR,
2399 "fuse: warning: library too old, some operations may not work\n");
2400 op_size = sizeof(struct fuse_lowlevel_ops);
2403 if (args->argc == 0) {
2404 fuse_log(FUSE_LOG_ERR,
2405 "fuse: empty argv passed to fuse_session_new().\n");
2406 return NULL;
2409 se = (struct fuse_session *)calloc(1, sizeof(struct fuse_session));
2410 if (se == NULL) {
2411 fuse_log(FUSE_LOG_ERR, "fuse: failed to allocate fuse object\n");
2412 goto out1;
2414 se->fd = -1;
2415 se->conn.max_write = UINT_MAX;
2416 se->conn.max_readahead = UINT_MAX;
2418 /* Parse options */
2419 if (fuse_opt_parse(args, se, fuse_ll_opts, NULL) == -1) {
2420 goto out2;
2422 if (args->argc == 1 && args->argv[0][0] == '-') {
2423 fuse_log(FUSE_LOG_ERR,
2424 "fuse: warning: argv[0] looks like an option, but "
2425 "will be ignored\n");
2426 } else if (args->argc != 1) {
2427 int i;
2428 fuse_log(FUSE_LOG_ERR, "fuse: unknown option(s): `");
2429 for (i = 1; i < args->argc - 1; i++) {
2430 fuse_log(FUSE_LOG_ERR, "%s ", args->argv[i]);
2432 fuse_log(FUSE_LOG_ERR, "%s'\n", args->argv[i]);
2433 goto out4;
2436 se->bufsize = FUSE_MAX_MAX_PAGES * getpagesize() + FUSE_BUFFER_HEADER_SIZE;
2438 list_init_req(&se->list);
2439 list_init_req(&se->interrupts);
2440 list_init_nreq(&se->notify_list);
2441 se->notify_ctr = 1;
2442 fuse_mutex_init(&se->lock);
2444 memcpy(&se->op, op, op_size);
2445 se->owner = getuid();
2446 se->userdata = userdata;
2448 return se;
2450 out4:
2451 fuse_opt_free_args(args);
2452 out2:
2453 free(se);
2454 out1:
2455 return NULL;
2458 int fuse_session_mount(struct fuse_session *se)
2460 int fd;
2463 * Make sure file descriptors 0, 1 and 2 are open, otherwise chaos
2464 * would ensue.
2466 do {
2467 fd = open("/dev/null", O_RDWR);
2468 if (fd > 2) {
2469 close(fd);
2471 } while (fd >= 0 && fd <= 2);
2474 * To allow FUSE daemons to run without privileges, the caller may open
2475 * /dev/fuse before launching the file system and pass on the file
2476 * descriptor by specifying /dev/fd/N as the mount point. Note that the
2477 * parent process takes care of performing the mount in this case.
2479 fd = fuse_mnt_parse_fuse_fd(mountpoint);
2480 if (fd != -1) {
2481 if (fcntl(fd, F_GETFD) == -1) {
2482 fuse_log(FUSE_LOG_ERR, "fuse: Invalid file descriptor /dev/fd/%u\n",
2483 fd);
2484 return -1;
2486 se->fd = fd;
2487 return 0;
2490 /* Open channel */
2491 fd = fuse_kern_mount(mountpoint, se->mo);
2492 if (fd == -1) {
2493 return -1;
2495 se->fd = fd;
2497 /* Save mountpoint */
2498 se->mountpoint = strdup(mountpoint);
2499 if (se->mountpoint == NULL) {
2500 goto error_out;
2503 return 0;
2505 error_out:
2506 fuse_kern_unmount(mountpoint, fd);
2507 return -1;
2510 int fuse_session_fd(struct fuse_session *se)
2512 return se->fd;
2515 void fuse_session_unmount(struct fuse_session *se)
2519 #ifdef linux
2520 int fuse_req_getgroups(fuse_req_t req, int size, gid_t list[])
2522 char *buf;
2523 size_t bufsize = 1024;
2524 char path[128];
2525 int ret;
2526 int fd;
2527 unsigned long pid = req->ctx.pid;
2528 char *s;
2530 sprintf(path, "/proc/%lu/task/%lu/status", pid, pid);
2532 retry:
2533 buf = malloc(bufsize);
2534 if (buf == NULL) {
2535 return -ENOMEM;
2538 ret = -EIO;
2539 fd = open(path, O_RDONLY);
2540 if (fd == -1) {
2541 goto out_free;
2544 ret = read(fd, buf, bufsize);
2545 close(fd);
2546 if (ret < 0) {
2547 ret = -EIO;
2548 goto out_free;
2551 if ((size_t)ret == bufsize) {
2552 free(buf);
2553 bufsize *= 4;
2554 goto retry;
2557 ret = -EIO;
2558 s = strstr(buf, "\nGroups:");
2559 if (s == NULL) {
2560 goto out_free;
2563 s += 8;
2564 ret = 0;
2565 while (1) {
2566 char *end;
2567 unsigned long val = strtoul(s, &end, 0);
2568 if (end == s) {
2569 break;
2572 s = end;
2573 if (ret < size) {
2574 list[ret] = val;
2576 ret++;
2579 out_free:
2580 free(buf);
2581 return ret;
2583 #else /* linux */
2585 * This is currently not implemented on other than Linux...
2587 int fuse_req_getgroups(fuse_req_t req, int size, gid_t list[])
2589 (void)req;
2590 (void)size;
2591 (void)list;
2592 return -ENOSYS;
2594 #endif
2596 void fuse_session_exit(struct fuse_session *se)
2598 se->exited = 1;
2601 void fuse_session_reset(struct fuse_session *se)
2603 se->exited = 0;
2604 se->error = 0;
2607 int fuse_session_exited(struct fuse_session *se)
2609 return se->exited;