libfuse: refcount fuse_chan objects
[fuse.git] / lib / fuse_lowlevel.c
blobe70733ac799b41126442945adf65e03140933010
1 /*
2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2007 Miklos Szeredi <miklos@szeredi.hu>
5 This program can be distributed under the terms of the GNU LGPLv2.
6 See the file COPYING.LIB
7 */
9 #define _GNU_SOURCE
11 #include "config.h"
12 #include "fuse_i.h"
13 #include "fuse_kernel.h"
14 #include "fuse_opt.h"
15 #include "fuse_misc.h"
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <stddef.h>
20 #include <string.h>
21 #include <unistd.h>
22 #include <limits.h>
23 #include <errno.h>
24 #include <assert.h>
25 #include <sys/file.h>
27 #ifndef F_LINUX_SPECIFIC_BASE
28 #define F_LINUX_SPECIFIC_BASE 1024
29 #endif
30 #ifndef F_SETPIPE_SZ
31 #define F_SETPIPE_SZ (F_LINUX_SPECIFIC_BASE + 7)
32 #endif
35 #define PARAM(inarg) (((char *)(inarg)) + sizeof(*(inarg)))
36 #define OFFSET_MAX 0x7fffffffffffffffLL
38 #define container_of(ptr, type, member) ({ \
39 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
40 (type *)( (char *)__mptr - offsetof(type,member) );})
42 struct fuse_pollhandle {
43 uint64_t kh;
44 struct fuse_chan *ch;
45 struct fuse_ll *f;
48 static size_t pagesize;
50 static __attribute__((constructor)) void fuse_ll_init_pagesize(void)
52 pagesize = getpagesize();
55 static void convert_stat(const struct stat *stbuf, struct fuse_attr *attr)
57 attr->ino = stbuf->st_ino;
58 attr->mode = stbuf->st_mode;
59 attr->nlink = stbuf->st_nlink;
60 attr->uid = stbuf->st_uid;
61 attr->gid = stbuf->st_gid;
62 attr->rdev = stbuf->st_rdev;
63 attr->size = stbuf->st_size;
64 attr->blksize = stbuf->st_blksize;
65 attr->blocks = stbuf->st_blocks;
66 attr->atime = stbuf->st_atime;
67 attr->mtime = stbuf->st_mtime;
68 attr->ctime = stbuf->st_ctime;
69 attr->atimensec = ST_ATIM_NSEC(stbuf);
70 attr->mtimensec = ST_MTIM_NSEC(stbuf);
71 attr->ctimensec = ST_CTIM_NSEC(stbuf);
74 static void convert_attr(const struct fuse_setattr_in *attr, struct stat *stbuf)
76 stbuf->st_mode = attr->mode;
77 stbuf->st_uid = attr->uid;
78 stbuf->st_gid = attr->gid;
79 stbuf->st_size = attr->size;
80 stbuf->st_atime = attr->atime;
81 stbuf->st_mtime = attr->mtime;
82 stbuf->st_ctime = attr->ctime;
83 ST_ATIM_NSEC_SET(stbuf, attr->atimensec);
84 ST_MTIM_NSEC_SET(stbuf, attr->mtimensec);
85 ST_CTIM_NSEC_SET(stbuf, attr->ctimensec);
88 static size_t iov_length(const struct iovec *iov, size_t count)
90 size_t seg;
91 size_t ret = 0;
93 for (seg = 0; seg < count; seg++)
94 ret += iov[seg].iov_len;
95 return ret;
98 static void list_init_req(struct fuse_req *req)
100 req->next = req;
101 req->prev = req;
104 static void list_del_req(struct fuse_req *req)
106 struct fuse_req *prev = req->prev;
107 struct fuse_req *next = req->next;
108 prev->next = next;
109 next->prev = prev;
112 static void list_add_req(struct fuse_req *req, struct fuse_req *next)
114 struct fuse_req *prev = next->prev;
115 req->next = next;
116 req->prev = prev;
117 prev->next = req;
118 next->prev = req;
121 static void destroy_req(fuse_req_t req)
123 pthread_mutex_destroy(&req->lock);
124 free(req);
127 void fuse_free_req(fuse_req_t req)
129 int ctr;
130 struct fuse_ll *f = req->f;
132 pthread_mutex_lock(&f->lock);
133 req->u.ni.func = NULL;
134 req->u.ni.data = NULL;
135 list_del_req(req);
136 ctr = --req->ctr;
137 pthread_mutex_unlock(&f->lock);
138 if (!ctr)
139 destroy_req(req);
142 static struct fuse_req *fuse_ll_alloc_req(struct fuse_ll *f)
144 struct fuse_req *req;
146 req = (struct fuse_req *) calloc(1, sizeof(struct fuse_req));
147 if (req == NULL) {
148 fprintf(stderr, "fuse: failed to allocate request\n");
149 } else {
150 req->f = f;
151 req->ctr = 1;
152 list_init_req(req);
153 fuse_mutex_init(&req->lock);
156 return req;
159 static int fuse_chan_recv(struct fuse_session *se, struct fuse_buf *buf,
160 struct fuse_chan *ch)
162 struct fuse_ll *f = se->f;
163 int err;
164 ssize_t res;
166 if (!buf->mem) {
167 buf->mem = malloc(f->bufsize);
168 if (!buf->mem) {
169 fprintf(stderr,
170 "fuse: failed to allocate read buffer\n");
171 return -ENOMEM;
175 restart:
176 res = read(fuse_chan_fd(ch), buf->mem, f->bufsize);
177 err = errno;
179 if (fuse_session_exited(se))
180 return 0;
181 if (res == -1) {
182 /* ENOENT means the operation was interrupted, it's safe
183 to restart */
184 if (err == ENOENT)
185 goto restart;
187 if (err == ENODEV) {
188 fuse_session_exit(se);
189 return 0;
191 /* Errors occurring during normal operation: EINTR (read
192 interrupted), EAGAIN (nonblocking I/O), ENODEV (filesystem
193 umounted) */
194 if (err != EINTR && err != EAGAIN)
195 perror("fuse: reading device");
196 return -err;
198 if ((size_t) res < sizeof(struct fuse_in_header)) {
199 fprintf(stderr, "short read on fuse device\n");
200 return -EIO;
203 buf->size = res;
205 return res;
208 static int fuse_chan_send(struct fuse_chan *ch, const struct iovec iov[],
209 size_t count)
211 ssize_t res = writev(fuse_chan_fd(ch), iov, count);
212 int err = errno;
214 if (res == -1) {
215 struct fuse_session *se = fuse_chan_session(ch);
217 assert(se != NULL);
219 /* ENOENT means the operation was interrupted */
220 if (!fuse_session_exited(se) && err != ENOENT)
221 perror("fuse: writing device");
222 return -err;
225 return 0;
228 void fuse_chan_close(struct fuse_chan *ch)
230 int fd = fuse_chan_fd(ch);
231 if (fd != -1)
232 close(fd);
236 static int fuse_send_msg(struct fuse_ll *f, struct fuse_chan *ch,
237 struct iovec *iov, int count)
239 struct fuse_out_header *out = iov[0].iov_base;
241 out->len = iov_length(iov, count);
242 if (f->debug) {
243 if (out->unique == 0) {
244 fprintf(stderr, "NOTIFY: code=%d length=%u\n",
245 out->error, out->len);
246 } else if (out->error) {
247 fprintf(stderr,
248 " unique: %llu, error: %i (%s), outsize: %i\n",
249 (unsigned long long) out->unique, out->error,
250 strerror(-out->error), out->len);
251 } else {
252 fprintf(stderr,
253 " unique: %llu, success, outsize: %i\n",
254 (unsigned long long) out->unique, out->len);
258 return fuse_chan_send(ch, iov, count);
261 int fuse_send_reply_iov_nofree(fuse_req_t req, int error, struct iovec *iov,
262 int count)
264 struct fuse_out_header out;
266 if (error <= -1000 || error > 0) {
267 fprintf(stderr, "fuse: bad error value: %i\n", error);
268 error = -ERANGE;
271 out.unique = req->unique;
272 out.error = error;
274 iov[0].iov_base = &out;
275 iov[0].iov_len = sizeof(struct fuse_out_header);
277 return fuse_send_msg(req->f, req->ch, iov, count);
280 static int send_reply_iov(fuse_req_t req, int error, struct iovec *iov,
281 int count)
283 int res;
285 res = fuse_send_reply_iov_nofree(req, error, iov, count);
286 fuse_free_req(req);
287 return res;
290 static int send_reply(fuse_req_t req, int error, const void *arg,
291 size_t argsize)
293 struct iovec iov[2];
294 int count = 1;
295 if (argsize) {
296 iov[1].iov_base = (void *) arg;
297 iov[1].iov_len = argsize;
298 count++;
300 return send_reply_iov(req, error, iov, count);
303 int fuse_reply_iov(fuse_req_t req, const struct iovec *iov, int count)
305 int res;
306 struct iovec *padded_iov;
308 padded_iov = malloc((count + 1) * sizeof(struct iovec));
309 if (padded_iov == NULL)
310 return fuse_reply_err(req, ENOMEM);
312 memcpy(padded_iov + 1, iov, count * sizeof(struct iovec));
313 count++;
315 res = send_reply_iov(req, 0, padded_iov, count);
316 free(padded_iov);
318 return res;
321 static size_t fuse_dirent_size(size_t namelen)
323 return FUSE_DIRENT_ALIGN(FUSE_NAME_OFFSET + namelen);
326 static void fuse_add_dirent(struct fuse_dirent *dirent, const char *name,
327 const struct stat *stbuf, off_t off)
329 unsigned namelen = strlen(name);
330 unsigned entlen = FUSE_NAME_OFFSET + namelen;
331 unsigned entsize = fuse_dirent_size(namelen);
332 unsigned padlen = entsize - entlen;
334 dirent->ino = stbuf->st_ino;
335 dirent->off = off;
336 dirent->namelen = namelen;
337 dirent->type = (stbuf->st_mode & 0170000) >> 12;
338 strncpy(dirent->name, name, namelen);
339 if (padlen)
340 memset(dirent->name + namelen, 0, padlen);
343 size_t fuse_add_direntry(fuse_req_t req, char *buf, size_t bufsize,
344 const char *name, const struct stat *stbuf, off_t off)
346 size_t entsize;
348 (void) req;
349 entsize = fuse_dirent_size(strlen(name));
350 if (entsize <= bufsize && buf)
351 fuse_add_dirent((struct fuse_dirent *) buf, name, stbuf, off);
352 return entsize;
355 static void convert_statfs(const struct statvfs *stbuf,
356 struct fuse_kstatfs *kstatfs)
358 kstatfs->bsize = stbuf->f_bsize;
359 kstatfs->frsize = stbuf->f_frsize;
360 kstatfs->blocks = stbuf->f_blocks;
361 kstatfs->bfree = stbuf->f_bfree;
362 kstatfs->bavail = stbuf->f_bavail;
363 kstatfs->files = stbuf->f_files;
364 kstatfs->ffree = stbuf->f_ffree;
365 kstatfs->namelen = stbuf->f_namemax;
368 static int send_reply_ok(fuse_req_t req, const void *arg, size_t argsize)
370 return send_reply(req, 0, arg, argsize);
373 int fuse_reply_err(fuse_req_t req, int err)
375 return send_reply(req, -err, NULL, 0);
378 void fuse_reply_none(fuse_req_t req)
380 fuse_free_req(req);
383 static unsigned long calc_timeout_sec(double t)
385 if (t > (double) ULONG_MAX)
386 return ULONG_MAX;
387 else if (t < 0.0)
388 return 0;
389 else
390 return (unsigned long) t;
393 static unsigned int calc_timeout_nsec(double t)
395 double f = t - (double) calc_timeout_sec(t);
396 if (f < 0.0)
397 return 0;
398 else if (f >= 0.999999999)
399 return 999999999;
400 else
401 return (unsigned int) (f * 1.0e9);
404 static void fill_entry(struct fuse_entry_out *arg,
405 const struct fuse_entry_param *e)
407 arg->nodeid = e->ino;
408 arg->generation = e->generation;
409 arg->entry_valid = calc_timeout_sec(e->entry_timeout);
410 arg->entry_valid_nsec = calc_timeout_nsec(e->entry_timeout);
411 arg->attr_valid = calc_timeout_sec(e->attr_timeout);
412 arg->attr_valid_nsec = calc_timeout_nsec(e->attr_timeout);
413 convert_stat(&e->attr, &arg->attr);
416 size_t fuse_add_direntry_plus(fuse_req_t req, char *buf, size_t bufsize,
417 const char *name,
418 const struct fuse_entry_param *e, off_t off)
420 size_t entsize;
422 (void) req;
423 entsize = FUSE_DIRENT_ALIGN(FUSE_NAME_OFFSET_DIRENTPLUS + strlen(name));
424 if (entsize <= bufsize && buf) {
425 struct fuse_direntplus *dp = (struct fuse_direntplus *) buf;
426 memset(&dp->entry_out, 0, sizeof(dp->entry_out));
427 fill_entry(&dp->entry_out, e);
428 fuse_add_dirent(&dp->dirent, name, &e->attr, off);
430 return entsize;
433 static void fill_open(struct fuse_open_out *arg,
434 const struct fuse_file_info *f)
436 arg->fh = f->fh;
437 if (f->direct_io)
438 arg->open_flags |= FOPEN_DIRECT_IO;
439 if (f->keep_cache)
440 arg->open_flags |= FOPEN_KEEP_CACHE;
441 if (f->nonseekable)
442 arg->open_flags |= FOPEN_NONSEEKABLE;
445 int fuse_reply_entry(fuse_req_t req, const struct fuse_entry_param *e)
447 struct fuse_entry_out arg;
448 size_t size = req->f->conn.proto_minor < 9 ?
449 FUSE_COMPAT_ENTRY_OUT_SIZE : sizeof(arg);
451 /* before ABI 7.4 e->ino == 0 was invalid, only ENOENT meant
452 negative entry */
453 if (!e->ino && req->f->conn.proto_minor < 4)
454 return fuse_reply_err(req, ENOENT);
456 memset(&arg, 0, sizeof(arg));
457 fill_entry(&arg, e);
458 return send_reply_ok(req, &arg, size);
461 int fuse_reply_create(fuse_req_t req, const struct fuse_entry_param *e,
462 const struct fuse_file_info *f)
464 char buf[sizeof(struct fuse_entry_out) + sizeof(struct fuse_open_out)];
465 size_t entrysize = req->f->conn.proto_minor < 9 ?
466 FUSE_COMPAT_ENTRY_OUT_SIZE : sizeof(struct fuse_entry_out);
467 struct fuse_entry_out *earg = (struct fuse_entry_out *) buf;
468 struct fuse_open_out *oarg = (struct fuse_open_out *) (buf + entrysize);
470 memset(buf, 0, sizeof(buf));
471 fill_entry(earg, e);
472 fill_open(oarg, f);
473 return send_reply_ok(req, buf,
474 entrysize + sizeof(struct fuse_open_out));
477 int fuse_reply_attr(fuse_req_t req, const struct stat *attr,
478 double attr_timeout)
480 struct fuse_attr_out arg;
481 size_t size = req->f->conn.proto_minor < 9 ?
482 FUSE_COMPAT_ATTR_OUT_SIZE : sizeof(arg);
484 memset(&arg, 0, sizeof(arg));
485 arg.attr_valid = calc_timeout_sec(attr_timeout);
486 arg.attr_valid_nsec = calc_timeout_nsec(attr_timeout);
487 convert_stat(attr, &arg.attr);
489 return send_reply_ok(req, &arg, size);
492 int fuse_reply_readlink(fuse_req_t req, const char *linkname)
494 return send_reply_ok(req, linkname, strlen(linkname));
497 int fuse_reply_open(fuse_req_t req, const struct fuse_file_info *f)
499 struct fuse_open_out arg;
501 memset(&arg, 0, sizeof(arg));
502 fill_open(&arg, f);
503 return send_reply_ok(req, &arg, sizeof(arg));
506 int fuse_reply_write(fuse_req_t req, size_t count)
508 struct fuse_write_out arg;
510 memset(&arg, 0, sizeof(arg));
511 arg.size = count;
513 return send_reply_ok(req, &arg, sizeof(arg));
516 int fuse_reply_buf(fuse_req_t req, const char *buf, size_t size)
518 return send_reply_ok(req, buf, size);
521 static int fuse_send_data_iov_fallback(struct fuse_ll *f, struct fuse_chan *ch,
522 struct iovec *iov, int iov_count,
523 struct fuse_bufvec *buf,
524 size_t len)
526 struct fuse_bufvec mem_buf = FUSE_BUFVEC_INIT(len);
527 void *mbuf;
528 int res;
530 /* Optimize common case */
531 if (buf->count == 1 && buf->idx == 0 && buf->off == 0 &&
532 !(buf->buf[0].flags & FUSE_BUF_IS_FD)) {
533 /* FIXME: also avoid memory copy if there are multiple buffers
534 but none of them contain an fd */
536 iov[iov_count].iov_base = buf->buf[0].mem;
537 iov[iov_count].iov_len = len;
538 iov_count++;
539 return fuse_send_msg(f, ch, iov, iov_count);
542 res = posix_memalign(&mbuf, pagesize, len);
543 if (res != 0)
544 return res;
546 mem_buf.buf[0].mem = mbuf;
547 res = fuse_buf_copy(&mem_buf, buf, 0);
548 if (res < 0) {
549 free(mbuf);
550 return -res;
552 len = res;
554 iov[iov_count].iov_base = mbuf;
555 iov[iov_count].iov_len = len;
556 iov_count++;
557 res = fuse_send_msg(f, ch, iov, iov_count);
558 free(mbuf);
560 return res;
563 struct fuse_ll_pipe {
564 size_t size;
565 int can_grow;
566 int pipe[2];
569 static void fuse_ll_pipe_free(struct fuse_ll_pipe *llp)
571 close(llp->pipe[0]);
572 close(llp->pipe[1]);
573 free(llp);
576 #ifdef HAVE_SPLICE
577 #if !defined(HAVE_PIPE2) || !defined(O_CLOEXEC)
578 static int fuse_pipe(int fds[2])
580 int rv = pipe(fds);
582 if (rv == -1)
583 return rv;
585 if (fcntl(fds[0], F_SETFL, O_NONBLOCK) == -1 ||
586 fcntl(fds[1], F_SETFL, O_NONBLOCK) == -1 ||
587 fcntl(fds[0], F_SETFD, FD_CLOEXEC) == -1 ||
588 fcntl(fds[1], F_SETFD, FD_CLOEXEC) == -1) {
589 close(fds[0]);
590 close(fds[1]);
591 rv = -1;
593 return rv;
595 #else
596 static int fuse_pipe(int fds[2])
598 return pipe2(fds, O_CLOEXEC | O_NONBLOCK);
600 #endif
602 static struct fuse_ll_pipe *fuse_ll_get_pipe(struct fuse_ll *f)
604 struct fuse_ll_pipe *llp = pthread_getspecific(f->pipe_key);
605 if (llp == NULL) {
606 int res;
608 llp = malloc(sizeof(struct fuse_ll_pipe));
609 if (llp == NULL)
610 return NULL;
612 res = fuse_pipe(llp->pipe);
613 if (res == -1) {
614 free(llp);
615 return NULL;
619 *the default size is 16 pages on linux
621 llp->size = pagesize * 16;
622 llp->can_grow = 1;
624 pthread_setspecific(f->pipe_key, llp);
627 return llp;
629 #endif
631 static void fuse_ll_clear_pipe(struct fuse_ll *f)
633 struct fuse_ll_pipe *llp = pthread_getspecific(f->pipe_key);
634 if (llp) {
635 pthread_setspecific(f->pipe_key, NULL);
636 fuse_ll_pipe_free(llp);
640 #if defined(HAVE_SPLICE) && defined(HAVE_VMSPLICE)
641 static int read_back(int fd, char *buf, size_t len)
643 int res;
645 res = read(fd, buf, len);
646 if (res == -1) {
647 fprintf(stderr, "fuse: internal error: failed to read back from pipe: %s\n", strerror(errno));
648 return -EIO;
650 if (res != len) {
651 fprintf(stderr, "fuse: internal error: short read back from pipe: %i from %zi\n", res, len);
652 return -EIO;
654 return 0;
657 static int fuse_send_data_iov(struct fuse_ll *f, struct fuse_chan *ch,
658 struct iovec *iov, int iov_count,
659 struct fuse_bufvec *buf, unsigned int flags)
661 int res;
662 size_t len = fuse_buf_size(buf);
663 struct fuse_out_header *out = iov[0].iov_base;
664 struct fuse_ll_pipe *llp;
665 int splice_flags;
666 size_t pipesize;
667 size_t total_fd_size;
668 size_t idx;
669 size_t headerlen;
670 struct fuse_bufvec pipe_buf = FUSE_BUFVEC_INIT(len);
672 if (f->broken_splice_nonblock)
673 goto fallback;
675 if (flags & FUSE_BUF_NO_SPLICE)
676 goto fallback;
678 total_fd_size = 0;
679 for (idx = buf->idx; idx < buf->count; idx++) {
680 if (buf->buf[idx].flags & FUSE_BUF_IS_FD) {
681 total_fd_size = buf->buf[idx].size;
682 if (idx == buf->idx)
683 total_fd_size -= buf->off;
686 if (total_fd_size < 2 * pagesize)
687 goto fallback;
689 if (f->conn.proto_minor < 14 ||
690 !(f->conn.want & FUSE_CAP_SPLICE_WRITE))
691 goto fallback;
693 llp = fuse_ll_get_pipe(f);
694 if (llp == NULL)
695 goto fallback;
698 headerlen = iov_length(iov, iov_count);
700 out->len = headerlen + len;
703 * Heuristic for the required pipe size, does not work if the
704 * source contains less than page size fragments
706 pipesize = pagesize * (iov_count + buf->count + 1) + out->len;
708 if (llp->size < pipesize) {
709 if (llp->can_grow) {
710 res = fcntl(llp->pipe[0], F_SETPIPE_SZ, pipesize);
711 if (res == -1) {
712 llp->can_grow = 0;
713 goto fallback;
715 llp->size = res;
717 if (llp->size < pipesize)
718 goto fallback;
722 res = vmsplice(llp->pipe[1], iov, iov_count, SPLICE_F_NONBLOCK);
723 if (res == -1)
724 goto fallback;
726 if (res != headerlen) {
727 res = -EIO;
728 fprintf(stderr, "fuse: short vmsplice to pipe: %u/%zu\n", res,
729 headerlen);
730 goto clear_pipe;
733 pipe_buf.buf[0].flags = FUSE_BUF_IS_FD;
734 pipe_buf.buf[0].fd = llp->pipe[1];
736 res = fuse_buf_copy(&pipe_buf, buf,
737 FUSE_BUF_FORCE_SPLICE | FUSE_BUF_SPLICE_NONBLOCK);
738 if (res < 0) {
739 if (res == -EAGAIN || res == -EINVAL) {
741 * Should only get EAGAIN on kernels with
742 * broken SPLICE_F_NONBLOCK support (<=
743 * 2.6.35) where this error or a short read is
744 * returned even if the pipe itself is not
745 * full
747 * EINVAL might mean that splice can't handle
748 * this combination of input and output.
750 if (res == -EAGAIN)
751 f->broken_splice_nonblock = 1;
753 pthread_setspecific(f->pipe_key, NULL);
754 fuse_ll_pipe_free(llp);
755 goto fallback;
757 res = -res;
758 goto clear_pipe;
761 if (res != 0 && res < len) {
762 struct fuse_bufvec mem_buf = FUSE_BUFVEC_INIT(len);
763 void *mbuf;
764 size_t now_len = res;
766 * For regular files a short count is either
767 * 1) due to EOF, or
768 * 2) because of broken SPLICE_F_NONBLOCK (see above)
770 * For other inputs it's possible that we overflowed
771 * the pipe because of small buffer fragments.
774 res = posix_memalign(&mbuf, pagesize, len);
775 if (res != 0)
776 goto clear_pipe;
778 mem_buf.buf[0].mem = mbuf;
779 mem_buf.off = now_len;
780 res = fuse_buf_copy(&mem_buf, buf, 0);
781 if (res > 0) {
782 char *tmpbuf;
783 size_t extra_len = res;
785 * Trickiest case: got more data. Need to get
786 * back the data from the pipe and then fall
787 * back to regular write.
789 tmpbuf = malloc(headerlen);
790 if (tmpbuf == NULL) {
791 free(mbuf);
792 res = ENOMEM;
793 goto clear_pipe;
795 res = read_back(llp->pipe[0], tmpbuf, headerlen);
796 if (res != 0) {
797 free(mbuf);
798 goto clear_pipe;
800 free(tmpbuf);
801 res = read_back(llp->pipe[0], mbuf, now_len);
802 if (res != 0) {
803 free(mbuf);
804 goto clear_pipe;
806 len = now_len + extra_len;
807 iov[iov_count].iov_base = mbuf;
808 iov[iov_count].iov_len = len;
809 iov_count++;
810 res = fuse_send_msg(f, ch, iov, iov_count);
811 free(mbuf);
812 return res;
814 free(mbuf);
815 res = now_len;
817 len = res;
818 out->len = headerlen + len;
820 if (f->debug) {
821 fprintf(stderr,
822 " unique: %llu, success, outsize: %i (splice)\n",
823 (unsigned long long) out->unique, out->len);
826 splice_flags = 0;
827 if ((flags & FUSE_BUF_SPLICE_MOVE) &&
828 (f->conn.want & FUSE_CAP_SPLICE_MOVE))
829 splice_flags |= SPLICE_F_MOVE;
831 res = splice(llp->pipe[0], NULL,
832 fuse_chan_fd(ch), NULL, out->len, splice_flags);
833 if (res == -1) {
834 res = -errno;
835 perror("fuse: splice from pipe");
836 goto clear_pipe;
838 if (res != out->len) {
839 res = -EIO;
840 fprintf(stderr, "fuse: short splice from pipe: %u/%u\n",
841 res, out->len);
842 goto clear_pipe;
844 return 0;
846 clear_pipe:
847 fuse_ll_clear_pipe(f);
848 return res;
850 fallback:
851 return fuse_send_data_iov_fallback(f, ch, iov, iov_count, buf, len);
853 #else
854 static int fuse_send_data_iov(struct fuse_ll *f, struct fuse_chan *ch,
855 struct iovec *iov, int iov_count,
856 struct fuse_bufvec *buf, unsigned int flags)
858 size_t len = fuse_buf_size(buf);
859 (void) flags;
861 return fuse_send_data_iov_fallback(f, ch, iov, iov_count, buf, len);
863 #endif
865 int fuse_reply_data(fuse_req_t req, struct fuse_bufvec *bufv,
866 enum fuse_buf_copy_flags flags)
868 struct iovec iov[2];
869 struct fuse_out_header out;
870 int res;
872 iov[0].iov_base = &out;
873 iov[0].iov_len = sizeof(struct fuse_out_header);
875 out.unique = req->unique;
876 out.error = 0;
878 res = fuse_send_data_iov(req->f, req->ch, iov, 1, bufv, flags);
879 if (res <= 0) {
880 fuse_free_req(req);
881 return res;
882 } else {
883 return fuse_reply_err(req, res);
887 int fuse_reply_statfs(fuse_req_t req, const struct statvfs *stbuf)
889 struct fuse_statfs_out arg;
890 size_t size = req->f->conn.proto_minor < 4 ?
891 FUSE_COMPAT_STATFS_SIZE : sizeof(arg);
893 memset(&arg, 0, sizeof(arg));
894 convert_statfs(stbuf, &arg.st);
896 return send_reply_ok(req, &arg, size);
899 int fuse_reply_xattr(fuse_req_t req, size_t count)
901 struct fuse_getxattr_out arg;
903 memset(&arg, 0, sizeof(arg));
904 arg.size = count;
906 return send_reply_ok(req, &arg, sizeof(arg));
909 int fuse_reply_lock(fuse_req_t req, const struct flock *lock)
911 struct fuse_lk_out arg;
913 memset(&arg, 0, sizeof(arg));
914 arg.lk.type = lock->l_type;
915 if (lock->l_type != F_UNLCK) {
916 arg.lk.start = lock->l_start;
917 if (lock->l_len == 0)
918 arg.lk.end = OFFSET_MAX;
919 else
920 arg.lk.end = lock->l_start + lock->l_len - 1;
922 arg.lk.pid = lock->l_pid;
923 return send_reply_ok(req, &arg, sizeof(arg));
926 int fuse_reply_bmap(fuse_req_t req, uint64_t idx)
928 struct fuse_bmap_out arg;
930 memset(&arg, 0, sizeof(arg));
931 arg.block = idx;
933 return send_reply_ok(req, &arg, sizeof(arg));
936 static struct fuse_ioctl_iovec *fuse_ioctl_iovec_copy(const struct iovec *iov,
937 size_t count)
939 struct fuse_ioctl_iovec *fiov;
940 size_t i;
942 fiov = malloc(sizeof(fiov[0]) * count);
943 if (!fiov)
944 return NULL;
946 for (i = 0; i < count; i++) {
947 fiov[i].base = (uintptr_t) iov[i].iov_base;
948 fiov[i].len = iov[i].iov_len;
951 return fiov;
954 int fuse_reply_ioctl_retry(fuse_req_t req,
955 const struct iovec *in_iov, size_t in_count,
956 const struct iovec *out_iov, size_t out_count)
958 struct fuse_ioctl_out arg;
959 struct fuse_ioctl_iovec *in_fiov = NULL;
960 struct fuse_ioctl_iovec *out_fiov = NULL;
961 struct iovec iov[4];
962 size_t count = 1;
963 int res;
965 memset(&arg, 0, sizeof(arg));
966 arg.flags |= FUSE_IOCTL_RETRY;
967 arg.in_iovs = in_count;
968 arg.out_iovs = out_count;
969 iov[count].iov_base = &arg;
970 iov[count].iov_len = sizeof(arg);
971 count++;
973 if (req->f->conn.proto_minor < 16) {
974 if (in_count) {
975 iov[count].iov_base = (void *)in_iov;
976 iov[count].iov_len = sizeof(in_iov[0]) * in_count;
977 count++;
980 if (out_count) {
981 iov[count].iov_base = (void *)out_iov;
982 iov[count].iov_len = sizeof(out_iov[0]) * out_count;
983 count++;
985 } else {
986 /* Can't handle non-compat 64bit ioctls on 32bit */
987 if (sizeof(void *) == 4 && req->ioctl_64bit) {
988 res = fuse_reply_err(req, EINVAL);
989 goto out;
992 if (in_count) {
993 in_fiov = fuse_ioctl_iovec_copy(in_iov, in_count);
994 if (!in_fiov)
995 goto enomem;
997 iov[count].iov_base = (void *)in_fiov;
998 iov[count].iov_len = sizeof(in_fiov[0]) * in_count;
999 count++;
1001 if (out_count) {
1002 out_fiov = fuse_ioctl_iovec_copy(out_iov, out_count);
1003 if (!out_fiov)
1004 goto enomem;
1006 iov[count].iov_base = (void *)out_fiov;
1007 iov[count].iov_len = sizeof(out_fiov[0]) * out_count;
1008 count++;
1012 res = send_reply_iov(req, 0, iov, count);
1013 out:
1014 free(in_fiov);
1015 free(out_fiov);
1017 return res;
1019 enomem:
1020 res = fuse_reply_err(req, ENOMEM);
1021 goto out;
1024 int fuse_reply_ioctl(fuse_req_t req, int result, const void *buf, size_t size)
1026 struct fuse_ioctl_out arg;
1027 struct iovec iov[3];
1028 size_t count = 1;
1030 memset(&arg, 0, sizeof(arg));
1031 arg.result = result;
1032 iov[count].iov_base = &arg;
1033 iov[count].iov_len = sizeof(arg);
1034 count++;
1036 if (size) {
1037 iov[count].iov_base = (char *) buf;
1038 iov[count].iov_len = size;
1039 count++;
1042 return send_reply_iov(req, 0, iov, count);
1045 int fuse_reply_ioctl_iov(fuse_req_t req, int result, const struct iovec *iov,
1046 int count)
1048 struct iovec *padded_iov;
1049 struct fuse_ioctl_out arg;
1050 int res;
1052 padded_iov = malloc((count + 2) * sizeof(struct iovec));
1053 if (padded_iov == NULL)
1054 return fuse_reply_err(req, ENOMEM);
1056 memset(&arg, 0, sizeof(arg));
1057 arg.result = result;
1058 padded_iov[1].iov_base = &arg;
1059 padded_iov[1].iov_len = sizeof(arg);
1061 memcpy(&padded_iov[2], iov, count * sizeof(struct iovec));
1063 res = send_reply_iov(req, 0, padded_iov, count + 2);
1064 free(padded_iov);
1066 return res;
1069 int fuse_reply_poll(fuse_req_t req, unsigned revents)
1071 struct fuse_poll_out arg;
1073 memset(&arg, 0, sizeof(arg));
1074 arg.revents = revents;
1076 return send_reply_ok(req, &arg, sizeof(arg));
1079 static void do_lookup(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1081 char *name = (char *) inarg;
1083 if (req->f->op.lookup)
1084 req->f->op.lookup(req, nodeid, name);
1085 else
1086 fuse_reply_err(req, ENOSYS);
1089 static void do_forget(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1091 struct fuse_forget_in *arg = (struct fuse_forget_in *) inarg;
1093 if (req->f->op.forget)
1094 req->f->op.forget(req, nodeid, arg->nlookup);
1095 else
1096 fuse_reply_none(req);
1099 static void do_batch_forget(fuse_req_t req, fuse_ino_t nodeid,
1100 const void *inarg)
1102 struct fuse_batch_forget_in *arg = (void *) inarg;
1103 struct fuse_forget_one *param = (void *) PARAM(arg);
1104 unsigned int i;
1106 (void) nodeid;
1108 if (req->f->op.forget_multi) {
1109 req->f->op.forget_multi(req, arg->count,
1110 (struct fuse_forget_data *) param);
1111 } else if (req->f->op.forget) {
1112 for (i = 0; i < arg->count; i++) {
1113 struct fuse_forget_one *forget = &param[i];
1114 struct fuse_req *dummy_req;
1116 dummy_req = fuse_ll_alloc_req(req->f);
1117 if (dummy_req == NULL)
1118 break;
1120 dummy_req->unique = req->unique;
1121 dummy_req->ctx = req->ctx;
1122 dummy_req->ch = NULL;
1124 req->f->op.forget(dummy_req, forget->nodeid,
1125 forget->nlookup);
1127 fuse_reply_none(req);
1128 } else {
1129 fuse_reply_none(req);
1133 static void do_getattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1135 struct fuse_file_info *fip = NULL;
1136 struct fuse_file_info fi;
1138 if (req->f->conn.proto_minor >= 9) {
1139 struct fuse_getattr_in *arg = (struct fuse_getattr_in *) inarg;
1141 if (arg->getattr_flags & FUSE_GETATTR_FH) {
1142 memset(&fi, 0, sizeof(fi));
1143 fi.fh = arg->fh;
1144 fip = &fi;
1148 if (req->f->op.getattr)
1149 req->f->op.getattr(req, nodeid, fip);
1150 else
1151 fuse_reply_err(req, ENOSYS);
1154 static void do_setattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1156 struct fuse_setattr_in *arg = (struct fuse_setattr_in *) inarg;
1158 if (req->f->op.setattr) {
1159 struct fuse_file_info *fi = NULL;
1160 struct fuse_file_info fi_store;
1161 struct stat stbuf;
1162 memset(&stbuf, 0, sizeof(stbuf));
1163 convert_attr(arg, &stbuf);
1164 if (arg->valid & FATTR_FH) {
1165 arg->valid &= ~FATTR_FH;
1166 memset(&fi_store, 0, sizeof(fi_store));
1167 fi = &fi_store;
1168 fi->fh = arg->fh;
1170 arg->valid &=
1171 FUSE_SET_ATTR_MODE |
1172 FUSE_SET_ATTR_UID |
1173 FUSE_SET_ATTR_GID |
1174 FUSE_SET_ATTR_SIZE |
1175 FUSE_SET_ATTR_ATIME |
1176 FUSE_SET_ATTR_MTIME |
1177 FUSE_SET_ATTR_ATIME_NOW |
1178 FUSE_SET_ATTR_MTIME_NOW |
1179 FUSE_SET_ATTR_CTIME;
1181 req->f->op.setattr(req, nodeid, &stbuf, arg->valid, fi);
1182 } else
1183 fuse_reply_err(req, ENOSYS);
1186 static void do_access(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1188 struct fuse_access_in *arg = (struct fuse_access_in *) inarg;
1190 if (req->f->op.access)
1191 req->f->op.access(req, nodeid, arg->mask);
1192 else
1193 fuse_reply_err(req, ENOSYS);
1196 static void do_readlink(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1198 (void) inarg;
1200 if (req->f->op.readlink)
1201 req->f->op.readlink(req, nodeid);
1202 else
1203 fuse_reply_err(req, ENOSYS);
1206 static void do_mknod(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1208 struct fuse_mknod_in *arg = (struct fuse_mknod_in *) inarg;
1209 char *name = PARAM(arg);
1211 if (req->f->conn.proto_minor >= 12)
1212 req->ctx.umask = arg->umask;
1213 else
1214 name = (char *) inarg + FUSE_COMPAT_MKNOD_IN_SIZE;
1216 if (req->f->op.mknod)
1217 req->f->op.mknod(req, nodeid, name, arg->mode, arg->rdev);
1218 else
1219 fuse_reply_err(req, ENOSYS);
1222 static void do_mkdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1224 struct fuse_mkdir_in *arg = (struct fuse_mkdir_in *) inarg;
1226 if (req->f->conn.proto_minor >= 12)
1227 req->ctx.umask = arg->umask;
1229 if (req->f->op.mkdir)
1230 req->f->op.mkdir(req, nodeid, PARAM(arg), arg->mode);
1231 else
1232 fuse_reply_err(req, ENOSYS);
1235 static void do_unlink(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1237 char *name = (char *) inarg;
1239 if (req->f->op.unlink)
1240 req->f->op.unlink(req, nodeid, name);
1241 else
1242 fuse_reply_err(req, ENOSYS);
1245 static void do_rmdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1247 char *name = (char *) inarg;
1249 if (req->f->op.rmdir)
1250 req->f->op.rmdir(req, nodeid, name);
1251 else
1252 fuse_reply_err(req, ENOSYS);
1255 static void do_symlink(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1257 char *name = (char *) inarg;
1258 char *linkname = ((char *) inarg) + strlen((char *) inarg) + 1;
1260 if (req->f->op.symlink)
1261 req->f->op.symlink(req, linkname, nodeid, name);
1262 else
1263 fuse_reply_err(req, ENOSYS);
1266 static void do_rename(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1268 struct fuse_rename_in *arg = (struct fuse_rename_in *) inarg;
1269 char *oldname = PARAM(arg);
1270 char *newname = oldname + strlen(oldname) + 1;
1272 if (req->f->op.rename)
1273 req->f->op.rename(req, nodeid, oldname, arg->newdir, newname,
1275 else
1276 fuse_reply_err(req, ENOSYS);
1279 static void do_rename2(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1281 struct fuse_rename2_in *arg = (struct fuse_rename2_in *) inarg;
1282 char *oldname = PARAM(arg);
1283 char *newname = oldname + strlen(oldname) + 1;
1285 if (req->f->op.rename)
1286 req->f->op.rename(req, nodeid, oldname, arg->newdir, newname,
1287 arg->flags);
1288 else
1289 fuse_reply_err(req, ENOSYS);
1292 static void do_link(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1294 struct fuse_link_in *arg = (struct fuse_link_in *) inarg;
1296 if (req->f->op.link)
1297 req->f->op.link(req, arg->oldnodeid, nodeid, PARAM(arg));
1298 else
1299 fuse_reply_err(req, ENOSYS);
1302 static void do_create(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1304 struct fuse_create_in *arg = (struct fuse_create_in *) inarg;
1306 if (req->f->op.create) {
1307 struct fuse_file_info fi;
1308 char *name = PARAM(arg);
1310 memset(&fi, 0, sizeof(fi));
1311 fi.flags = arg->flags;
1313 if (req->f->conn.proto_minor >= 12)
1314 req->ctx.umask = arg->umask;
1315 else
1316 name = (char *) inarg + sizeof(struct fuse_open_in);
1318 req->f->op.create(req, nodeid, name, arg->mode, &fi);
1319 } else
1320 fuse_reply_err(req, ENOSYS);
1323 static void do_open(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1325 struct fuse_open_in *arg = (struct fuse_open_in *) inarg;
1326 struct fuse_file_info fi;
1328 memset(&fi, 0, sizeof(fi));
1329 fi.flags = arg->flags;
1331 if (req->f->op.open)
1332 req->f->op.open(req, nodeid, &fi);
1333 else
1334 fuse_reply_open(req, &fi);
1337 static void do_read(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1339 struct fuse_read_in *arg = (struct fuse_read_in *) inarg;
1341 if (req->f->op.read) {
1342 struct fuse_file_info fi;
1344 memset(&fi, 0, sizeof(fi));
1345 fi.fh = arg->fh;
1346 if (req->f->conn.proto_minor >= 9) {
1347 fi.lock_owner = arg->lock_owner;
1348 fi.flags = arg->flags;
1350 req->f->op.read(req, nodeid, arg->size, arg->offset, &fi);
1351 } else
1352 fuse_reply_err(req, ENOSYS);
1355 static void do_write(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1357 struct fuse_write_in *arg = (struct fuse_write_in *) inarg;
1358 struct fuse_file_info fi;
1359 char *param;
1361 memset(&fi, 0, sizeof(fi));
1362 fi.fh = arg->fh;
1363 fi.writepage = (arg->write_flags & 1) != 0;
1365 if (req->f->conn.proto_minor < 9) {
1366 param = ((char *) arg) + FUSE_COMPAT_WRITE_IN_SIZE;
1367 } else {
1368 fi.lock_owner = arg->lock_owner;
1369 fi.flags = arg->flags;
1370 param = PARAM(arg);
1373 if (req->f->op.write)
1374 req->f->op.write(req, nodeid, param, arg->size,
1375 arg->offset, &fi);
1376 else
1377 fuse_reply_err(req, ENOSYS);
1380 static void do_write_buf(fuse_req_t req, fuse_ino_t nodeid, const void *inarg,
1381 const struct fuse_buf *ibuf)
1383 struct fuse_ll *f = req->f;
1384 struct fuse_bufvec bufv = {
1385 .buf[0] = *ibuf,
1386 .count = 1,
1388 struct fuse_write_in *arg = (struct fuse_write_in *) inarg;
1389 struct fuse_file_info fi;
1391 memset(&fi, 0, sizeof(fi));
1392 fi.fh = arg->fh;
1393 fi.writepage = arg->write_flags & 1;
1395 if (req->f->conn.proto_minor < 9) {
1396 bufv.buf[0].mem = ((char *) arg) + FUSE_COMPAT_WRITE_IN_SIZE;
1397 bufv.buf[0].size -= sizeof(struct fuse_in_header) +
1398 FUSE_COMPAT_WRITE_IN_SIZE;
1399 assert(!(bufv.buf[0].flags & FUSE_BUF_IS_FD));
1400 } else {
1401 fi.lock_owner = arg->lock_owner;
1402 fi.flags = arg->flags;
1403 if (!(bufv.buf[0].flags & FUSE_BUF_IS_FD))
1404 bufv.buf[0].mem = PARAM(arg);
1406 bufv.buf[0].size -= sizeof(struct fuse_in_header) +
1407 sizeof(struct fuse_write_in);
1409 if (bufv.buf[0].size < arg->size) {
1410 fprintf(stderr, "fuse: do_write_buf: buffer size too small\n");
1411 fuse_reply_err(req, EIO);
1412 goto out;
1414 bufv.buf[0].size = arg->size;
1416 req->f->op.write_buf(req, nodeid, &bufv, arg->offset, &fi);
1418 out:
1419 /* Need to reset the pipe if ->write_buf() didn't consume all data */
1420 if ((ibuf->flags & FUSE_BUF_IS_FD) && bufv.idx < bufv.count)
1421 fuse_ll_clear_pipe(f);
1424 static void do_flush(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1426 struct fuse_flush_in *arg = (struct fuse_flush_in *) inarg;
1427 struct fuse_file_info fi;
1429 memset(&fi, 0, sizeof(fi));
1430 fi.fh = arg->fh;
1431 fi.flush = 1;
1432 if (req->f->conn.proto_minor >= 7)
1433 fi.lock_owner = arg->lock_owner;
1435 if (req->f->op.flush)
1436 req->f->op.flush(req, nodeid, &fi);
1437 else
1438 fuse_reply_err(req, ENOSYS);
1441 static void do_release(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1443 struct fuse_release_in *arg = (struct fuse_release_in *) inarg;
1444 struct fuse_file_info fi;
1446 memset(&fi, 0, sizeof(fi));
1447 fi.flags = arg->flags;
1448 fi.fh = arg->fh;
1449 if (req->f->conn.proto_minor >= 8) {
1450 fi.flush = (arg->release_flags & FUSE_RELEASE_FLUSH) ? 1 : 0;
1451 fi.lock_owner = arg->lock_owner;
1453 if (arg->release_flags & FUSE_RELEASE_FLOCK_UNLOCK) {
1454 fi.flock_release = 1;
1455 fi.lock_owner = arg->lock_owner;
1458 if (req->f->op.release)
1459 req->f->op.release(req, nodeid, &fi);
1460 else
1461 fuse_reply_err(req, 0);
1464 static void do_fsync(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1466 struct fuse_fsync_in *arg = (struct fuse_fsync_in *) inarg;
1467 struct fuse_file_info fi;
1469 memset(&fi, 0, sizeof(fi));
1470 fi.fh = arg->fh;
1472 if (req->f->op.fsync)
1473 req->f->op.fsync(req, nodeid, arg->fsync_flags & 1, &fi);
1474 else
1475 fuse_reply_err(req, ENOSYS);
1478 static void do_opendir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1480 struct fuse_open_in *arg = (struct fuse_open_in *) inarg;
1481 struct fuse_file_info fi;
1483 memset(&fi, 0, sizeof(fi));
1484 fi.flags = arg->flags;
1486 if (req->f->op.opendir)
1487 req->f->op.opendir(req, nodeid, &fi);
1488 else
1489 fuse_reply_open(req, &fi);
1492 static void do_readdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1494 struct fuse_read_in *arg = (struct fuse_read_in *) inarg;
1495 struct fuse_file_info fi;
1497 memset(&fi, 0, sizeof(fi));
1498 fi.fh = arg->fh;
1500 if (req->f->op.readdir)
1501 req->f->op.readdir(req, nodeid, arg->size, arg->offset, &fi);
1502 else
1503 fuse_reply_err(req, ENOSYS);
1506 static void do_readdirplus(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1508 struct fuse_read_in *arg = (struct fuse_read_in *) inarg;
1509 struct fuse_file_info fi;
1511 memset(&fi, 0, sizeof(fi));
1512 fi.fh = arg->fh;
1514 if (req->f->op.readdirplus)
1515 req->f->op.readdirplus(req, nodeid, arg->size, arg->offset, &fi);
1516 else
1517 fuse_reply_err(req, ENOSYS);
1520 static void do_releasedir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1522 struct fuse_release_in *arg = (struct fuse_release_in *) inarg;
1523 struct fuse_file_info fi;
1525 memset(&fi, 0, sizeof(fi));
1526 fi.flags = arg->flags;
1527 fi.fh = arg->fh;
1529 if (req->f->op.releasedir)
1530 req->f->op.releasedir(req, nodeid, &fi);
1531 else
1532 fuse_reply_err(req, 0);
1535 static void do_fsyncdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1537 struct fuse_fsync_in *arg = (struct fuse_fsync_in *) inarg;
1538 struct fuse_file_info fi;
1540 memset(&fi, 0, sizeof(fi));
1541 fi.fh = arg->fh;
1543 if (req->f->op.fsyncdir)
1544 req->f->op.fsyncdir(req, nodeid, arg->fsync_flags & 1, &fi);
1545 else
1546 fuse_reply_err(req, ENOSYS);
1549 static void do_statfs(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1551 (void) nodeid;
1552 (void) inarg;
1554 if (req->f->op.statfs)
1555 req->f->op.statfs(req, nodeid);
1556 else {
1557 struct statvfs buf = {
1558 .f_namemax = 255,
1559 .f_bsize = 512,
1561 fuse_reply_statfs(req, &buf);
1565 static void do_setxattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1567 struct fuse_setxattr_in *arg = (struct fuse_setxattr_in *) inarg;
1568 char *name = PARAM(arg);
1569 char *value = name + strlen(name) + 1;
1571 if (req->f->op.setxattr)
1572 req->f->op.setxattr(req, nodeid, name, value, arg->size,
1573 arg->flags);
1574 else
1575 fuse_reply_err(req, ENOSYS);
1578 static void do_getxattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1580 struct fuse_getxattr_in *arg = (struct fuse_getxattr_in *) inarg;
1582 if (req->f->op.getxattr)
1583 req->f->op.getxattr(req, nodeid, PARAM(arg), arg->size);
1584 else
1585 fuse_reply_err(req, ENOSYS);
1588 static void do_listxattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1590 struct fuse_getxattr_in *arg = (struct fuse_getxattr_in *) inarg;
1592 if (req->f->op.listxattr)
1593 req->f->op.listxattr(req, nodeid, arg->size);
1594 else
1595 fuse_reply_err(req, ENOSYS);
1598 static void do_removexattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1600 char *name = (char *) inarg;
1602 if (req->f->op.removexattr)
1603 req->f->op.removexattr(req, nodeid, name);
1604 else
1605 fuse_reply_err(req, ENOSYS);
1608 static void convert_fuse_file_lock(struct fuse_file_lock *fl,
1609 struct flock *flock)
1611 memset(flock, 0, sizeof(struct flock));
1612 flock->l_type = fl->type;
1613 flock->l_whence = SEEK_SET;
1614 flock->l_start = fl->start;
1615 if (fl->end == OFFSET_MAX)
1616 flock->l_len = 0;
1617 else
1618 flock->l_len = fl->end - fl->start + 1;
1619 flock->l_pid = fl->pid;
1622 static void do_getlk(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1624 struct fuse_lk_in *arg = (struct fuse_lk_in *) inarg;
1625 struct fuse_file_info fi;
1626 struct flock flock;
1628 memset(&fi, 0, sizeof(fi));
1629 fi.fh = arg->fh;
1630 fi.lock_owner = arg->owner;
1632 convert_fuse_file_lock(&arg->lk, &flock);
1633 if (req->f->op.getlk)
1634 req->f->op.getlk(req, nodeid, &fi, &flock);
1635 else
1636 fuse_reply_err(req, ENOSYS);
1639 static void do_setlk_common(fuse_req_t req, fuse_ino_t nodeid,
1640 const void *inarg, int sleep)
1642 struct fuse_lk_in *arg = (struct fuse_lk_in *) inarg;
1643 struct fuse_file_info fi;
1644 struct flock flock;
1646 memset(&fi, 0, sizeof(fi));
1647 fi.fh = arg->fh;
1648 fi.lock_owner = arg->owner;
1650 if (arg->lk_flags & FUSE_LK_FLOCK) {
1651 int op = 0;
1653 switch (arg->lk.type) {
1654 case F_RDLCK:
1655 op = LOCK_SH;
1656 break;
1657 case F_WRLCK:
1658 op = LOCK_EX;
1659 break;
1660 case F_UNLCK:
1661 op = LOCK_UN;
1662 break;
1664 if (!sleep)
1665 op |= LOCK_NB;
1667 if (req->f->op.flock)
1668 req->f->op.flock(req, nodeid, &fi, op);
1669 else
1670 fuse_reply_err(req, ENOSYS);
1671 } else {
1672 convert_fuse_file_lock(&arg->lk, &flock);
1673 if (req->f->op.setlk)
1674 req->f->op.setlk(req, nodeid, &fi, &flock, sleep);
1675 else
1676 fuse_reply_err(req, ENOSYS);
1680 static void do_setlk(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1682 do_setlk_common(req, nodeid, inarg, 0);
1685 static void do_setlkw(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1687 do_setlk_common(req, nodeid, inarg, 1);
1690 static int find_interrupted(struct fuse_ll *f, struct fuse_req *req)
1692 struct fuse_req *curr;
1694 for (curr = f->list.next; curr != &f->list; curr = curr->next) {
1695 if (curr->unique == req->u.i.unique) {
1696 fuse_interrupt_func_t func;
1697 void *data;
1699 curr->ctr++;
1700 pthread_mutex_unlock(&f->lock);
1702 /* Ugh, ugly locking */
1703 pthread_mutex_lock(&curr->lock);
1704 pthread_mutex_lock(&f->lock);
1705 curr->interrupted = 1;
1706 func = curr->u.ni.func;
1707 data = curr->u.ni.data;
1708 pthread_mutex_unlock(&f->lock);
1709 if (func)
1710 func(curr, data);
1711 pthread_mutex_unlock(&curr->lock);
1713 pthread_mutex_lock(&f->lock);
1714 curr->ctr--;
1715 if (!curr->ctr)
1716 destroy_req(curr);
1718 return 1;
1721 for (curr = f->interrupts.next; curr != &f->interrupts;
1722 curr = curr->next) {
1723 if (curr->u.i.unique == req->u.i.unique)
1724 return 1;
1726 return 0;
1729 static void do_interrupt(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1731 struct fuse_interrupt_in *arg = (struct fuse_interrupt_in *) inarg;
1732 struct fuse_ll *f = req->f;
1734 (void) nodeid;
1735 if (f->debug)
1736 fprintf(stderr, "INTERRUPT: %llu\n",
1737 (unsigned long long) arg->unique);
1739 req->u.i.unique = arg->unique;
1741 pthread_mutex_lock(&f->lock);
1742 if (find_interrupted(f, req))
1743 destroy_req(req);
1744 else
1745 list_add_req(req, &f->interrupts);
1746 pthread_mutex_unlock(&f->lock);
1749 static struct fuse_req *check_interrupt(struct fuse_ll *f, struct fuse_req *req)
1751 struct fuse_req *curr;
1753 for (curr = f->interrupts.next; curr != &f->interrupts;
1754 curr = curr->next) {
1755 if (curr->u.i.unique == req->unique) {
1756 req->interrupted = 1;
1757 list_del_req(curr);
1758 free(curr);
1759 return NULL;
1762 curr = f->interrupts.next;
1763 if (curr != &f->interrupts) {
1764 list_del_req(curr);
1765 list_init_req(curr);
1766 return curr;
1767 } else
1768 return NULL;
1771 static void do_bmap(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1773 struct fuse_bmap_in *arg = (struct fuse_bmap_in *) inarg;
1775 if (req->f->op.bmap)
1776 req->f->op.bmap(req, nodeid, arg->blocksize, arg->block);
1777 else
1778 fuse_reply_err(req, ENOSYS);
1781 static void do_ioctl(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1783 struct fuse_ioctl_in *arg = (struct fuse_ioctl_in *) inarg;
1784 unsigned int flags = arg->flags;
1785 void *in_buf = arg->in_size ? PARAM(arg) : NULL;
1786 struct fuse_file_info fi;
1788 if (flags & FUSE_IOCTL_DIR &&
1789 !(req->f->conn.want & FUSE_CAP_IOCTL_DIR)) {
1790 fuse_reply_err(req, ENOTTY);
1791 return;
1794 memset(&fi, 0, sizeof(fi));
1795 fi.fh = arg->fh;
1797 if (sizeof(void *) == 4 && req->f->conn.proto_minor >= 16 &&
1798 !(flags & FUSE_IOCTL_32BIT)) {
1799 req->ioctl_64bit = 1;
1802 if (req->f->op.ioctl)
1803 req->f->op.ioctl(req, nodeid, arg->cmd,
1804 (void *)(uintptr_t)arg->arg, &fi, flags,
1805 in_buf, arg->in_size, arg->out_size);
1806 else
1807 fuse_reply_err(req, ENOSYS);
1810 void fuse_pollhandle_destroy(struct fuse_pollhandle *ph)
1812 free(ph);
1815 static void do_poll(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1817 struct fuse_poll_in *arg = (struct fuse_poll_in *) inarg;
1818 struct fuse_file_info fi;
1820 memset(&fi, 0, sizeof(fi));
1821 fi.fh = arg->fh;
1822 fi.poll_events = arg->events;
1824 if (req->f->op.poll) {
1825 struct fuse_pollhandle *ph = NULL;
1827 if (arg->flags & FUSE_POLL_SCHEDULE_NOTIFY) {
1828 ph = malloc(sizeof(struct fuse_pollhandle));
1829 if (ph == NULL) {
1830 fuse_reply_err(req, ENOMEM);
1831 return;
1833 ph->kh = arg->kh;
1834 ph->ch = req->ch;
1835 ph->f = req->f;
1838 req->f->op.poll(req, nodeid, &fi, ph);
1839 } else {
1840 fuse_reply_err(req, ENOSYS);
1844 static void do_fallocate(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1846 struct fuse_fallocate_in *arg = (struct fuse_fallocate_in *) inarg;
1847 struct fuse_file_info fi;
1849 memset(&fi, 0, sizeof(fi));
1850 fi.fh = arg->fh;
1852 if (req->f->op.fallocate)
1853 req->f->op.fallocate(req, nodeid, arg->mode, arg->offset, arg->length, &fi);
1854 else
1855 fuse_reply_err(req, ENOSYS);
1858 static void do_init(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1860 struct fuse_init_in *arg = (struct fuse_init_in *) inarg;
1861 struct fuse_init_out outarg;
1862 struct fuse_ll *f = req->f;
1863 size_t bufsize = f->bufsize;
1864 size_t outargsize = sizeof(outarg);
1866 (void) nodeid;
1867 if (f->debug) {
1868 fprintf(stderr, "INIT: %u.%u\n", arg->major, arg->minor);
1869 if (arg->major == 7 && arg->minor >= 6) {
1870 fprintf(stderr, "flags=0x%08x\n", arg->flags);
1871 fprintf(stderr, "max_readahead=0x%08x\n",
1872 arg->max_readahead);
1875 f->conn.proto_major = arg->major;
1876 f->conn.proto_minor = arg->minor;
1877 f->conn.capable = 0;
1878 f->conn.want = 0;
1880 memset(&outarg, 0, sizeof(outarg));
1881 outarg.major = FUSE_KERNEL_VERSION;
1882 outarg.minor = FUSE_KERNEL_MINOR_VERSION;
1884 if (arg->major < 7) {
1885 fprintf(stderr, "fuse: unsupported protocol version: %u.%u\n",
1886 arg->major, arg->minor);
1887 fuse_reply_err(req, EPROTO);
1888 return;
1891 if (arg->major > 7) {
1892 /* Wait for a second INIT request with a 7.X version */
1893 send_reply_ok(req, &outarg, sizeof(outarg));
1894 return;
1897 if (arg->minor >= 6) {
1898 if (f->conn.async_read)
1899 f->conn.async_read = arg->flags & FUSE_ASYNC_READ;
1900 if (arg->max_readahead < f->conn.max_readahead)
1901 f->conn.max_readahead = arg->max_readahead;
1902 if (arg->flags & FUSE_ASYNC_READ)
1903 f->conn.capable |= FUSE_CAP_ASYNC_READ;
1904 if (arg->flags & FUSE_POSIX_LOCKS)
1905 f->conn.capable |= FUSE_CAP_POSIX_LOCKS;
1906 if (arg->flags & FUSE_ATOMIC_O_TRUNC)
1907 f->conn.capable |= FUSE_CAP_ATOMIC_O_TRUNC;
1908 if (arg->flags & FUSE_EXPORT_SUPPORT)
1909 f->conn.capable |= FUSE_CAP_EXPORT_SUPPORT;
1910 if (arg->flags & FUSE_BIG_WRITES)
1911 f->conn.capable |= FUSE_CAP_BIG_WRITES;
1912 if (arg->flags & FUSE_DONT_MASK)
1913 f->conn.capable |= FUSE_CAP_DONT_MASK;
1914 if (arg->flags & FUSE_FLOCK_LOCKS)
1915 f->conn.capable |= FUSE_CAP_FLOCK_LOCKS;
1916 if (arg->flags & FUSE_AUTO_INVAL_DATA)
1917 f->conn.capable |= FUSE_CAP_AUTO_INVAL_DATA;
1918 if (arg->flags & FUSE_DO_READDIRPLUS)
1919 f->conn.capable |= FUSE_CAP_READDIRPLUS;
1920 if (arg->flags & FUSE_READDIRPLUS_AUTO)
1921 f->conn.capable |= FUSE_CAP_READDIRPLUS_AUTO;
1922 if (arg->flags & FUSE_ASYNC_DIO)
1923 f->conn.capable |= FUSE_CAP_ASYNC_DIO;
1924 if (arg->flags & FUSE_WRITEBACK_CACHE)
1925 f->conn.capable |= FUSE_CAP_WRITEBACK_CACHE;
1926 if (arg->flags & FUSE_NO_OPEN_SUPPORT)
1927 f->conn.capable |= FUSE_CAP_NO_OPEN_SUPPORT;
1928 } else {
1929 f->conn.async_read = 0;
1930 f->conn.max_readahead = 0;
1933 if (req->f->conn.proto_minor >= 14) {
1934 #ifdef HAVE_SPLICE
1935 #ifdef HAVE_VMSPLICE
1936 f->conn.capable |= FUSE_CAP_SPLICE_WRITE | FUSE_CAP_SPLICE_MOVE;
1937 if (f->splice_write)
1938 f->conn.want |= FUSE_CAP_SPLICE_WRITE;
1939 if (f->splice_move)
1940 f->conn.want |= FUSE_CAP_SPLICE_MOVE;
1941 #endif
1942 f->conn.capable |= FUSE_CAP_SPLICE_READ;
1943 if (f->splice_read)
1944 f->conn.want |= FUSE_CAP_SPLICE_READ;
1945 #endif
1947 if (req->f->conn.proto_minor >= 18)
1948 f->conn.capable |= FUSE_CAP_IOCTL_DIR;
1950 if (f->atomic_o_trunc)
1951 f->conn.want |= FUSE_CAP_ATOMIC_O_TRUNC;
1952 if (f->op.getlk && f->op.setlk && !f->no_remote_posix_lock)
1953 f->conn.want |= FUSE_CAP_POSIX_LOCKS;
1954 if (f->op.flock && !f->no_remote_flock)
1955 f->conn.want |= FUSE_CAP_FLOCK_LOCKS;
1956 if (f->big_writes)
1957 f->conn.want |= FUSE_CAP_BIG_WRITES;
1958 if (f->auto_inval_data)
1959 f->conn.want |= FUSE_CAP_AUTO_INVAL_DATA;
1960 if (f->op.readdirplus && !f->no_readdirplus) {
1961 f->conn.want |= FUSE_CAP_READDIRPLUS;
1962 if (!f->no_readdirplus_auto)
1963 f->conn.want |= FUSE_CAP_READDIRPLUS_AUTO;
1965 if (f->async_dio)
1966 f->conn.want |= FUSE_CAP_ASYNC_DIO;
1967 if (f->writeback_cache)
1968 f->conn.want |= FUSE_CAP_WRITEBACK_CACHE;
1970 if (bufsize < FUSE_MIN_READ_BUFFER) {
1971 fprintf(stderr, "fuse: warning: buffer size too small: %zu\n",
1972 bufsize);
1973 bufsize = FUSE_MIN_READ_BUFFER;
1976 bufsize -= 4096;
1977 if (bufsize < f->conn.max_write)
1978 f->conn.max_write = bufsize;
1980 f->got_init = 1;
1981 if (f->op.init)
1982 f->op.init(f->userdata, &f->conn);
1984 if (f->no_splice_read)
1985 f->conn.want &= ~FUSE_CAP_SPLICE_READ;
1986 if (f->no_splice_write)
1987 f->conn.want &= ~FUSE_CAP_SPLICE_WRITE;
1988 if (f->no_splice_move)
1989 f->conn.want &= ~FUSE_CAP_SPLICE_MOVE;
1990 if (f->no_auto_inval_data)
1991 f->conn.want &= ~FUSE_CAP_AUTO_INVAL_DATA;
1992 if (f->no_readdirplus)
1993 f->conn.want &= ~FUSE_CAP_READDIRPLUS;
1994 if (f->no_readdirplus_auto)
1995 f->conn.want &= ~FUSE_CAP_READDIRPLUS_AUTO;
1996 if (f->no_async_dio)
1997 f->conn.want &= ~FUSE_CAP_ASYNC_DIO;
1998 if (f->no_writeback_cache)
1999 f->conn.want &= ~FUSE_CAP_WRITEBACK_CACHE;
2001 if (f->conn.async_read || (f->conn.want & FUSE_CAP_ASYNC_READ))
2002 outarg.flags |= FUSE_ASYNC_READ;
2003 if (f->conn.want & FUSE_CAP_POSIX_LOCKS)
2004 outarg.flags |= FUSE_POSIX_LOCKS;
2005 if (f->conn.want & FUSE_CAP_ATOMIC_O_TRUNC)
2006 outarg.flags |= FUSE_ATOMIC_O_TRUNC;
2007 if (f->conn.want & FUSE_CAP_EXPORT_SUPPORT)
2008 outarg.flags |= FUSE_EXPORT_SUPPORT;
2009 if (f->conn.want & FUSE_CAP_BIG_WRITES)
2010 outarg.flags |= FUSE_BIG_WRITES;
2011 if (f->conn.want & FUSE_CAP_DONT_MASK)
2012 outarg.flags |= FUSE_DONT_MASK;
2013 if (f->conn.want & FUSE_CAP_FLOCK_LOCKS)
2014 outarg.flags |= FUSE_FLOCK_LOCKS;
2015 if (f->conn.want & FUSE_CAP_AUTO_INVAL_DATA)
2016 outarg.flags |= FUSE_AUTO_INVAL_DATA;
2017 if (f->conn.want & FUSE_CAP_READDIRPLUS)
2018 outarg.flags |= FUSE_DO_READDIRPLUS;
2019 if (f->conn.want & FUSE_CAP_READDIRPLUS_AUTO)
2020 outarg.flags |= FUSE_READDIRPLUS_AUTO;
2021 if (f->conn.want & FUSE_CAP_ASYNC_DIO)
2022 outarg.flags |= FUSE_ASYNC_DIO;
2023 if (f->conn.want & FUSE_CAP_WRITEBACK_CACHE)
2024 outarg.flags |= FUSE_WRITEBACK_CACHE;
2025 outarg.max_readahead = f->conn.max_readahead;
2026 outarg.max_write = f->conn.max_write;
2027 if (f->conn.proto_minor >= 13) {
2028 if (f->conn.max_background >= (1 << 16))
2029 f->conn.max_background = (1 << 16) - 1;
2030 if (f->conn.congestion_threshold > f->conn.max_background)
2031 f->conn.congestion_threshold = f->conn.max_background;
2032 if (!f->conn.congestion_threshold) {
2033 f->conn.congestion_threshold =
2034 f->conn.max_background * 3 / 4;
2037 outarg.max_background = f->conn.max_background;
2038 outarg.congestion_threshold = f->conn.congestion_threshold;
2040 if (f->conn.proto_minor >= 23)
2041 outarg.time_gran = f->conn.time_gran;
2043 if (f->debug) {
2044 fprintf(stderr, " INIT: %u.%u\n", outarg.major, outarg.minor);
2045 fprintf(stderr, " flags=0x%08x\n", outarg.flags);
2046 fprintf(stderr, " max_readahead=0x%08x\n",
2047 outarg.max_readahead);
2048 fprintf(stderr, " max_write=0x%08x\n", outarg.max_write);
2049 fprintf(stderr, " max_background=%i\n",
2050 outarg.max_background);
2051 fprintf(stderr, " congestion_threshold=%i\n",
2052 outarg.congestion_threshold);
2053 fprintf(stderr, " time_gran=%u\n",
2054 outarg.time_gran);
2056 if (arg->minor < 5)
2057 outargsize = FUSE_COMPAT_INIT_OUT_SIZE;
2058 else if (arg->minor < 23)
2059 outargsize = FUSE_COMPAT_22_INIT_OUT_SIZE;
2061 send_reply_ok(req, &outarg, outargsize);
2064 static void do_destroy(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
2066 struct fuse_ll *f = req->f;
2068 (void) nodeid;
2069 (void) inarg;
2071 f->got_destroy = 1;
2072 if (f->op.destroy)
2073 f->op.destroy(f->userdata);
2075 send_reply_ok(req, NULL, 0);
2078 static void list_del_nreq(struct fuse_notify_req *nreq)
2080 struct fuse_notify_req *prev = nreq->prev;
2081 struct fuse_notify_req *next = nreq->next;
2082 prev->next = next;
2083 next->prev = prev;
2086 static void list_add_nreq(struct fuse_notify_req *nreq,
2087 struct fuse_notify_req *next)
2089 struct fuse_notify_req *prev = next->prev;
2090 nreq->next = next;
2091 nreq->prev = prev;
2092 prev->next = nreq;
2093 next->prev = nreq;
2096 static void list_init_nreq(struct fuse_notify_req *nreq)
2098 nreq->next = nreq;
2099 nreq->prev = nreq;
2102 static void do_notify_reply(fuse_req_t req, fuse_ino_t nodeid,
2103 const void *inarg, const struct fuse_buf *buf)
2105 struct fuse_ll *f = req->f;
2106 struct fuse_notify_req *nreq;
2107 struct fuse_notify_req *head;
2109 pthread_mutex_lock(&f->lock);
2110 head = &f->notify_list;
2111 for (nreq = head->next; nreq != head; nreq = nreq->next) {
2112 if (nreq->unique == req->unique) {
2113 list_del_nreq(nreq);
2114 break;
2117 pthread_mutex_unlock(&f->lock);
2119 if (nreq != head)
2120 nreq->reply(nreq, req, nodeid, inarg, buf);
2123 static int send_notify_iov(struct fuse_ll *f, struct fuse_chan *ch,
2124 int notify_code, struct iovec *iov, int count)
2126 struct fuse_out_header out;
2128 if (!f->got_init)
2129 return -ENOTCONN;
2131 out.unique = 0;
2132 out.error = notify_code;
2133 iov[0].iov_base = &out;
2134 iov[0].iov_len = sizeof(struct fuse_out_header);
2136 return fuse_send_msg(f, ch, iov, count);
2139 int fuse_lowlevel_notify_poll(struct fuse_pollhandle *ph)
2141 if (ph != NULL) {
2142 struct fuse_notify_poll_wakeup_out outarg;
2143 struct iovec iov[2];
2145 outarg.kh = ph->kh;
2147 iov[1].iov_base = &outarg;
2148 iov[1].iov_len = sizeof(outarg);
2150 return send_notify_iov(ph->f, ph->ch, FUSE_NOTIFY_POLL, iov, 2);
2151 } else {
2152 return 0;
2156 int fuse_lowlevel_notify_inval_inode(struct fuse_chan *ch, fuse_ino_t ino,
2157 off_t off, off_t len)
2159 struct fuse_notify_inval_inode_out outarg;
2160 struct fuse_ll *f;
2161 struct iovec iov[2];
2163 if (!ch)
2164 return -EINVAL;
2166 f = fuse_chan_session(ch)->f;
2167 if (!f)
2168 return -ENODEV;
2170 outarg.ino = ino;
2171 outarg.off = off;
2172 outarg.len = len;
2174 iov[1].iov_base = &outarg;
2175 iov[1].iov_len = sizeof(outarg);
2177 return send_notify_iov(f, ch, FUSE_NOTIFY_INVAL_INODE, iov, 2);
2180 int fuse_lowlevel_notify_inval_entry(struct fuse_chan *ch, fuse_ino_t parent,
2181 const char *name, size_t namelen)
2183 struct fuse_notify_inval_entry_out outarg;
2184 struct fuse_ll *f;
2185 struct iovec iov[3];
2187 if (!ch)
2188 return -EINVAL;
2190 f = fuse_chan_session(ch)->f;
2191 if (!f)
2192 return -ENODEV;
2194 outarg.parent = parent;
2195 outarg.namelen = namelen;
2196 outarg.padding = 0;
2198 iov[1].iov_base = &outarg;
2199 iov[1].iov_len = sizeof(outarg);
2200 iov[2].iov_base = (void *)name;
2201 iov[2].iov_len = namelen + 1;
2203 return send_notify_iov(f, ch, FUSE_NOTIFY_INVAL_ENTRY, iov, 3);
2206 int fuse_lowlevel_notify_delete(struct fuse_chan *ch,
2207 fuse_ino_t parent, fuse_ino_t child,
2208 const char *name, size_t namelen)
2210 struct fuse_notify_delete_out outarg;
2211 struct fuse_ll *f;
2212 struct iovec iov[3];
2214 if (!ch)
2215 return -EINVAL;
2217 f = fuse_chan_session(ch)->f;
2218 if (!f)
2219 return -ENODEV;
2221 if (f->conn.proto_minor < 18)
2222 return -ENOSYS;
2224 outarg.parent = parent;
2225 outarg.child = child;
2226 outarg.namelen = namelen;
2227 outarg.padding = 0;
2229 iov[1].iov_base = &outarg;
2230 iov[1].iov_len = sizeof(outarg);
2231 iov[2].iov_base = (void *)name;
2232 iov[2].iov_len = namelen + 1;
2234 return send_notify_iov(f, ch, FUSE_NOTIFY_DELETE, iov, 3);
2237 int fuse_lowlevel_notify_store(struct fuse_chan *ch, fuse_ino_t ino,
2238 off_t offset, struct fuse_bufvec *bufv,
2239 enum fuse_buf_copy_flags flags)
2241 struct fuse_out_header out;
2242 struct fuse_notify_store_out outarg;
2243 struct fuse_ll *f;
2244 struct iovec iov[3];
2245 size_t size = fuse_buf_size(bufv);
2246 int res;
2248 if (!ch)
2249 return -EINVAL;
2251 f = fuse_chan_session(ch)->f;
2252 if (!f)
2253 return -ENODEV;
2255 if (f->conn.proto_minor < 15)
2256 return -ENOSYS;
2258 out.unique = 0;
2259 out.error = FUSE_NOTIFY_STORE;
2261 outarg.nodeid = ino;
2262 outarg.offset = offset;
2263 outarg.size = size;
2265 iov[0].iov_base = &out;
2266 iov[0].iov_len = sizeof(out);
2267 iov[1].iov_base = &outarg;
2268 iov[1].iov_len = sizeof(outarg);
2270 res = fuse_send_data_iov(f, ch, iov, 2, bufv, flags);
2271 if (res > 0)
2272 res = -res;
2274 return res;
2277 struct fuse_retrieve_req {
2278 struct fuse_notify_req nreq;
2279 void *cookie;
2282 static void fuse_ll_retrieve_reply(struct fuse_notify_req *nreq,
2283 fuse_req_t req, fuse_ino_t ino,
2284 const void *inarg,
2285 const struct fuse_buf *ibuf)
2287 struct fuse_ll *f = req->f;
2288 struct fuse_retrieve_req *rreq =
2289 container_of(nreq, struct fuse_retrieve_req, nreq);
2290 const struct fuse_notify_retrieve_in *arg = inarg;
2291 struct fuse_bufvec bufv = {
2292 .buf[0] = *ibuf,
2293 .count = 1,
2296 if (!(bufv.buf[0].flags & FUSE_BUF_IS_FD))
2297 bufv.buf[0].mem = PARAM(arg);
2299 bufv.buf[0].size -= sizeof(struct fuse_in_header) +
2300 sizeof(struct fuse_notify_retrieve_in);
2302 if (bufv.buf[0].size < arg->size) {
2303 fprintf(stderr, "fuse: retrieve reply: buffer size too small\n");
2304 fuse_reply_none(req);
2305 goto out;
2307 bufv.buf[0].size = arg->size;
2309 if (req->f->op.retrieve_reply) {
2310 req->f->op.retrieve_reply(req, rreq->cookie, ino,
2311 arg->offset, &bufv);
2312 } else {
2313 fuse_reply_none(req);
2315 out:
2316 free(rreq);
2317 if ((ibuf->flags & FUSE_BUF_IS_FD) && bufv.idx < bufv.count)
2318 fuse_ll_clear_pipe(f);
2321 int fuse_lowlevel_notify_retrieve(struct fuse_chan *ch, fuse_ino_t ino,
2322 size_t size, off_t offset, void *cookie)
2324 struct fuse_notify_retrieve_out outarg;
2325 struct fuse_ll *f;
2326 struct iovec iov[2];
2327 struct fuse_retrieve_req *rreq;
2328 int err;
2330 if (!ch)
2331 return -EINVAL;
2333 f = fuse_chan_session(ch)->f;
2334 if (!f)
2335 return -ENODEV;
2337 if (f->conn.proto_minor < 15)
2338 return -ENOSYS;
2340 rreq = malloc(sizeof(*rreq));
2341 if (rreq == NULL)
2342 return -ENOMEM;
2344 pthread_mutex_lock(&f->lock);
2345 rreq->cookie = cookie;
2346 rreq->nreq.unique = f->notify_ctr++;
2347 rreq->nreq.reply = fuse_ll_retrieve_reply;
2348 list_add_nreq(&rreq->nreq, &f->notify_list);
2349 pthread_mutex_unlock(&f->lock);
2351 outarg.notify_unique = rreq->nreq.unique;
2352 outarg.nodeid = ino;
2353 outarg.offset = offset;
2354 outarg.size = size;
2356 iov[1].iov_base = &outarg;
2357 iov[1].iov_len = sizeof(outarg);
2359 err = send_notify_iov(f, ch, FUSE_NOTIFY_RETRIEVE, iov, 2);
2360 if (err) {
2361 pthread_mutex_lock(&f->lock);
2362 list_del_nreq(&rreq->nreq);
2363 pthread_mutex_unlock(&f->lock);
2364 free(rreq);
2367 return err;
2370 void *fuse_req_userdata(fuse_req_t req)
2372 return req->f->userdata;
2375 const struct fuse_ctx *fuse_req_ctx(fuse_req_t req)
2377 return &req->ctx;
2380 void fuse_req_interrupt_func(fuse_req_t req, fuse_interrupt_func_t func,
2381 void *data)
2383 pthread_mutex_lock(&req->lock);
2384 pthread_mutex_lock(&req->f->lock);
2385 req->u.ni.func = func;
2386 req->u.ni.data = data;
2387 pthread_mutex_unlock(&req->f->lock);
2388 if (req->interrupted && func)
2389 func(req, data);
2390 pthread_mutex_unlock(&req->lock);
2393 int fuse_req_interrupted(fuse_req_t req)
2395 int interrupted;
2397 pthread_mutex_lock(&req->f->lock);
2398 interrupted = req->interrupted;
2399 pthread_mutex_unlock(&req->f->lock);
2401 return interrupted;
2404 static struct {
2405 void (*func)(fuse_req_t, fuse_ino_t, const void *);
2406 const char *name;
2407 } fuse_ll_ops[] = {
2408 [FUSE_LOOKUP] = { do_lookup, "LOOKUP" },
2409 [FUSE_FORGET] = { do_forget, "FORGET" },
2410 [FUSE_GETATTR] = { do_getattr, "GETATTR" },
2411 [FUSE_SETATTR] = { do_setattr, "SETATTR" },
2412 [FUSE_READLINK] = { do_readlink, "READLINK" },
2413 [FUSE_SYMLINK] = { do_symlink, "SYMLINK" },
2414 [FUSE_MKNOD] = { do_mknod, "MKNOD" },
2415 [FUSE_MKDIR] = { do_mkdir, "MKDIR" },
2416 [FUSE_UNLINK] = { do_unlink, "UNLINK" },
2417 [FUSE_RMDIR] = { do_rmdir, "RMDIR" },
2418 [FUSE_RENAME] = { do_rename, "RENAME" },
2419 [FUSE_LINK] = { do_link, "LINK" },
2420 [FUSE_OPEN] = { do_open, "OPEN" },
2421 [FUSE_READ] = { do_read, "READ" },
2422 [FUSE_WRITE] = { do_write, "WRITE" },
2423 [FUSE_STATFS] = { do_statfs, "STATFS" },
2424 [FUSE_RELEASE] = { do_release, "RELEASE" },
2425 [FUSE_FSYNC] = { do_fsync, "FSYNC" },
2426 [FUSE_SETXATTR] = { do_setxattr, "SETXATTR" },
2427 [FUSE_GETXATTR] = { do_getxattr, "GETXATTR" },
2428 [FUSE_LISTXATTR] = { do_listxattr, "LISTXATTR" },
2429 [FUSE_REMOVEXATTR] = { do_removexattr, "REMOVEXATTR" },
2430 [FUSE_FLUSH] = { do_flush, "FLUSH" },
2431 [FUSE_INIT] = { do_init, "INIT" },
2432 [FUSE_OPENDIR] = { do_opendir, "OPENDIR" },
2433 [FUSE_READDIR] = { do_readdir, "READDIR" },
2434 [FUSE_RELEASEDIR] = { do_releasedir, "RELEASEDIR" },
2435 [FUSE_FSYNCDIR] = { do_fsyncdir, "FSYNCDIR" },
2436 [FUSE_GETLK] = { do_getlk, "GETLK" },
2437 [FUSE_SETLK] = { do_setlk, "SETLK" },
2438 [FUSE_SETLKW] = { do_setlkw, "SETLKW" },
2439 [FUSE_ACCESS] = { do_access, "ACCESS" },
2440 [FUSE_CREATE] = { do_create, "CREATE" },
2441 [FUSE_INTERRUPT] = { do_interrupt, "INTERRUPT" },
2442 [FUSE_BMAP] = { do_bmap, "BMAP" },
2443 [FUSE_IOCTL] = { do_ioctl, "IOCTL" },
2444 [FUSE_POLL] = { do_poll, "POLL" },
2445 [FUSE_FALLOCATE] = { do_fallocate, "FALLOCATE" },
2446 [FUSE_DESTROY] = { do_destroy, "DESTROY" },
2447 [FUSE_NOTIFY_REPLY] = { (void *) 1, "NOTIFY_REPLY" },
2448 [FUSE_BATCH_FORGET] = { do_batch_forget, "BATCH_FORGET" },
2449 [FUSE_READDIRPLUS] = { do_readdirplus, "READDIRPLUS"},
2450 [FUSE_RENAME2] = { do_rename2, "RENAME2" },
2451 [CUSE_INIT] = { cuse_lowlevel_init, "CUSE_INIT" },
2454 #define FUSE_MAXOP (sizeof(fuse_ll_ops) / sizeof(fuse_ll_ops[0]))
2456 static const char *opname(enum fuse_opcode opcode)
2458 if (opcode >= FUSE_MAXOP || !fuse_ll_ops[opcode].name)
2459 return "???";
2460 else
2461 return fuse_ll_ops[opcode].name;
2464 static int fuse_ll_copy_from_pipe(struct fuse_bufvec *dst,
2465 struct fuse_bufvec *src)
2467 int res = fuse_buf_copy(dst, src, 0);
2468 if (res < 0) {
2469 fprintf(stderr, "fuse: copy from pipe: %s\n", strerror(-res));
2470 return res;
2472 if (res < fuse_buf_size(dst)) {
2473 fprintf(stderr, "fuse: copy from pipe: short read\n");
2474 return -1;
2476 return 0;
2479 void fuse_session_process_buf(struct fuse_session *se,
2480 const struct fuse_buf *buf, struct fuse_chan *ch)
2482 struct fuse_ll *f = se->f;
2483 const size_t write_header_size = sizeof(struct fuse_in_header) +
2484 sizeof(struct fuse_write_in);
2485 struct fuse_bufvec bufv = { .buf[0] = *buf, .count = 1 };
2486 struct fuse_bufvec tmpbuf = FUSE_BUFVEC_INIT(write_header_size);
2487 struct fuse_in_header *in;
2488 const void *inarg;
2489 struct fuse_req *req;
2490 void *mbuf = NULL;
2491 int err;
2492 int res;
2494 if (buf->flags & FUSE_BUF_IS_FD) {
2495 if (buf->size < tmpbuf.buf[0].size)
2496 tmpbuf.buf[0].size = buf->size;
2498 mbuf = malloc(tmpbuf.buf[0].size);
2499 if (mbuf == NULL) {
2500 fprintf(stderr, "fuse: failed to allocate header\n");
2501 goto clear_pipe;
2503 tmpbuf.buf[0].mem = mbuf;
2505 res = fuse_ll_copy_from_pipe(&tmpbuf, &bufv);
2506 if (res < 0)
2507 goto clear_pipe;
2509 in = mbuf;
2510 } else {
2511 in = buf->mem;
2514 if (f->debug) {
2515 fprintf(stderr,
2516 "unique: %llu, opcode: %s (%i), nodeid: %llu, insize: %zu, pid: %u\n",
2517 (unsigned long long) in->unique,
2518 opname((enum fuse_opcode) in->opcode), in->opcode,
2519 (unsigned long long) in->nodeid, buf->size, in->pid);
2522 req = fuse_ll_alloc_req(f);
2523 if (req == NULL) {
2524 struct fuse_out_header out = {
2525 .unique = in->unique,
2526 .error = -ENOMEM,
2528 struct iovec iov = {
2529 .iov_base = &out,
2530 .iov_len = sizeof(struct fuse_out_header),
2533 fuse_send_msg(f, ch, &iov, 1);
2534 goto clear_pipe;
2537 req->unique = in->unique;
2538 req->ctx.uid = in->uid;
2539 req->ctx.gid = in->gid;
2540 req->ctx.pid = in->pid;
2541 req->ch = ch;
2543 err = EIO;
2544 if (!f->got_init) {
2545 enum fuse_opcode expected;
2547 expected = f->cuse_data ? CUSE_INIT : FUSE_INIT;
2548 if (in->opcode != expected)
2549 goto reply_err;
2550 } else if (in->opcode == FUSE_INIT || in->opcode == CUSE_INIT)
2551 goto reply_err;
2553 err = EACCES;
2554 if (f->allow_root && in->uid != f->owner && in->uid != 0 &&
2555 in->opcode != FUSE_INIT && in->opcode != FUSE_READ &&
2556 in->opcode != FUSE_WRITE && in->opcode != FUSE_FSYNC &&
2557 in->opcode != FUSE_RELEASE && in->opcode != FUSE_READDIR &&
2558 in->opcode != FUSE_FSYNCDIR && in->opcode != FUSE_RELEASEDIR &&
2559 in->opcode != FUSE_NOTIFY_REPLY &&
2560 in->opcode != FUSE_READDIRPLUS)
2561 goto reply_err;
2563 err = ENOSYS;
2564 if (in->opcode >= FUSE_MAXOP || !fuse_ll_ops[in->opcode].func)
2565 goto reply_err;
2566 if (in->opcode != FUSE_INTERRUPT) {
2567 struct fuse_req *intr;
2568 pthread_mutex_lock(&f->lock);
2569 intr = check_interrupt(f, req);
2570 list_add_req(req, &f->list);
2571 pthread_mutex_unlock(&f->lock);
2572 if (intr)
2573 fuse_reply_err(intr, EAGAIN);
2576 if ((buf->flags & FUSE_BUF_IS_FD) && write_header_size < buf->size &&
2577 (in->opcode != FUSE_WRITE || !f->op.write_buf) &&
2578 in->opcode != FUSE_NOTIFY_REPLY) {
2579 void *newmbuf;
2581 err = ENOMEM;
2582 newmbuf = realloc(mbuf, buf->size);
2583 if (newmbuf == NULL)
2584 goto reply_err;
2585 mbuf = newmbuf;
2587 tmpbuf = FUSE_BUFVEC_INIT(buf->size - write_header_size);
2588 tmpbuf.buf[0].mem = mbuf + write_header_size;
2590 res = fuse_ll_copy_from_pipe(&tmpbuf, &bufv);
2591 err = -res;
2592 if (res < 0)
2593 goto reply_err;
2595 in = mbuf;
2598 inarg = (void *) &in[1];
2599 if (in->opcode == FUSE_WRITE && f->op.write_buf)
2600 do_write_buf(req, in->nodeid, inarg, buf);
2601 else if (in->opcode == FUSE_NOTIFY_REPLY)
2602 do_notify_reply(req, in->nodeid, inarg, buf);
2603 else
2604 fuse_ll_ops[in->opcode].func(req, in->nodeid, inarg);
2606 out_free:
2607 free(mbuf);
2608 return;
2610 reply_err:
2611 fuse_reply_err(req, err);
2612 clear_pipe:
2613 if (buf->flags & FUSE_BUF_IS_FD)
2614 fuse_ll_clear_pipe(f);
2615 goto out_free;
2618 enum {
2619 KEY_HELP,
2620 KEY_VERSION,
2623 static const struct fuse_opt fuse_ll_opts[] = {
2624 { "debug", offsetof(struct fuse_ll, debug), 1 },
2625 { "-d", offsetof(struct fuse_ll, debug), 1 },
2626 { "allow_root", offsetof(struct fuse_ll, allow_root), 1 },
2627 { "max_write=%u", offsetof(struct fuse_ll, conn.max_write), 0 },
2628 { "max_readahead=%u", offsetof(struct fuse_ll, conn.max_readahead), 0 },
2629 { "max_background=%u", offsetof(struct fuse_ll, conn.max_background), 0 },
2630 { "congestion_threshold=%u",
2631 offsetof(struct fuse_ll, conn.congestion_threshold), 0 },
2632 { "async_read", offsetof(struct fuse_ll, conn.async_read), 1 },
2633 { "sync_read", offsetof(struct fuse_ll, conn.async_read), 0 },
2634 { "atomic_o_trunc", offsetof(struct fuse_ll, atomic_o_trunc), 1},
2635 { "no_remote_lock", offsetof(struct fuse_ll, no_remote_posix_lock), 1},
2636 { "no_remote_lock", offsetof(struct fuse_ll, no_remote_flock), 1},
2637 { "no_remote_flock", offsetof(struct fuse_ll, no_remote_flock), 1},
2638 { "no_remote_posix_lock", offsetof(struct fuse_ll, no_remote_posix_lock), 1},
2639 { "big_writes", offsetof(struct fuse_ll, big_writes), 1},
2640 { "splice_write", offsetof(struct fuse_ll, splice_write), 1},
2641 { "no_splice_write", offsetof(struct fuse_ll, no_splice_write), 1},
2642 { "splice_move", offsetof(struct fuse_ll, splice_move), 1},
2643 { "no_splice_move", offsetof(struct fuse_ll, no_splice_move), 1},
2644 { "splice_read", offsetof(struct fuse_ll, splice_read), 1},
2645 { "no_splice_read", offsetof(struct fuse_ll, no_splice_read), 1},
2646 { "auto_inval_data", offsetof(struct fuse_ll, auto_inval_data), 1},
2647 { "no_auto_inval_data", offsetof(struct fuse_ll, no_auto_inval_data), 1},
2648 { "readdirplus=no", offsetof(struct fuse_ll, no_readdirplus), 1},
2649 { "readdirplus=yes", offsetof(struct fuse_ll, no_readdirplus), 0},
2650 { "readdirplus=yes", offsetof(struct fuse_ll, no_readdirplus_auto), 1},
2651 { "readdirplus=auto", offsetof(struct fuse_ll, no_readdirplus), 0},
2652 { "readdirplus=auto", offsetof(struct fuse_ll, no_readdirplus_auto), 0},
2653 { "async_dio", offsetof(struct fuse_ll, async_dio), 1},
2654 { "no_async_dio", offsetof(struct fuse_ll, no_async_dio), 1},
2655 { "writeback_cache", offsetof(struct fuse_ll, writeback_cache), 1},
2656 { "no_writeback_cache", offsetof(struct fuse_ll, no_writeback_cache), 1},
2657 { "time_gran=%u", offsetof(struct fuse_ll, conn.time_gran), 0 },
2658 FUSE_OPT_KEY("max_read=", FUSE_OPT_KEY_DISCARD),
2659 FUSE_OPT_KEY("-h", KEY_HELP),
2660 FUSE_OPT_KEY("--help", KEY_HELP),
2661 FUSE_OPT_KEY("-V", KEY_VERSION),
2662 FUSE_OPT_KEY("--version", KEY_VERSION),
2663 FUSE_OPT_END
2666 static void fuse_ll_version(void)
2668 printf("using FUSE kernel interface version %i.%i\n",
2669 FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
2672 static void fuse_ll_help(void)
2674 printf(
2675 " -o max_write=N set maximum size of write requests\n"
2676 " -o max_readahead=N set maximum readahead\n"
2677 " -o max_background=N set number of maximum background requests\n"
2678 " -o congestion_threshold=N set kernel's congestion threshold\n"
2679 " -o async_read perform reads asynchronously (default)\n"
2680 " -o sync_read perform reads synchronously\n"
2681 " -o atomic_o_trunc enable atomic open+truncate support\n"
2682 " -o big_writes enable larger than 4kB writes\n"
2683 " -o no_remote_lock disable remote file locking\n"
2684 " -o no_remote_flock disable remote file locking (BSD)\n"
2685 " -o no_remote_posix_lock disable remove file locking (POSIX)\n"
2686 " -o [no_]splice_write use splice to write to the fuse device\n"
2687 " -o [no_]splice_move move data while splicing to the fuse device\n"
2688 " -o [no_]splice_read use splice to read from the fuse device\n"
2689 " -o [no_]auto_inval_data use automatic kernel cache invalidation logic\n"
2690 " -o readdirplus=S control readdirplus use (yes|no|auto)\n"
2691 " -o [no_]async_dio asynchronous direct I/O\n"
2692 " -o [no_]writeback_cache asynchronous, buffered writes\n"
2693 " -o time_gran=N time granularity in nsec\n"
2697 static int fuse_ll_opt_proc(void *data, const char *arg, int key,
2698 struct fuse_args *outargs)
2700 (void) data; (void) outargs;
2702 switch (key) {
2703 case KEY_HELP:
2704 fuse_ll_help();
2705 break;
2707 case KEY_VERSION:
2708 fuse_ll_version();
2709 break;
2711 default:
2712 fprintf(stderr, "fuse: unknown option `%s'\n", arg);
2715 return -1;
2718 static void fuse_ll_destroy(struct fuse_ll *f)
2720 struct fuse_ll_pipe *llp;
2722 if (f->got_init && !f->got_destroy) {
2723 if (f->op.destroy)
2724 f->op.destroy(f->userdata);
2726 llp = pthread_getspecific(f->pipe_key);
2727 if (llp != NULL)
2728 fuse_ll_pipe_free(llp);
2729 pthread_key_delete(f->pipe_key);
2730 pthread_mutex_destroy(&f->lock);
2731 free(f->cuse_data);
2732 free(f);
2735 void fuse_session_destroy(struct fuse_session *se)
2737 fuse_ll_destroy(se->f);
2738 fuse_chan_put(se->ch);
2739 free(se);
2743 static void fuse_ll_pipe_destructor(void *data)
2745 struct fuse_ll_pipe *llp = data;
2746 fuse_ll_pipe_free(llp);
2749 #ifdef HAVE_SPLICE
2750 int fuse_session_receive_buf(struct fuse_session *se, struct fuse_buf *buf,
2751 struct fuse_chan *ch)
2753 struct fuse_ll *f = se->f;
2754 size_t bufsize = f->bufsize;
2755 struct fuse_ll_pipe *llp;
2756 struct fuse_buf tmpbuf;
2757 int err;
2758 int res;
2760 if (f->conn.proto_minor < 14 || !(f->conn.want & FUSE_CAP_SPLICE_READ))
2761 goto fallback;
2763 llp = fuse_ll_get_pipe(f);
2764 if (llp == NULL)
2765 goto fallback;
2767 if (llp->size < bufsize) {
2768 if (llp->can_grow) {
2769 res = fcntl(llp->pipe[0], F_SETPIPE_SZ, bufsize);
2770 if (res == -1) {
2771 llp->can_grow = 0;
2772 goto fallback;
2774 llp->size = res;
2776 if (llp->size < bufsize)
2777 goto fallback;
2780 res = splice(fuse_chan_fd(ch), NULL, llp->pipe[1], NULL, bufsize, 0);
2781 err = errno;
2783 if (fuse_session_exited(se))
2784 return 0;
2786 if (res == -1) {
2787 if (err == ENODEV) {
2788 fuse_session_exit(se);
2789 return 0;
2791 if (err != EINTR && err != EAGAIN)
2792 perror("fuse: splice from device");
2793 return -err;
2796 if (res < sizeof(struct fuse_in_header)) {
2797 fprintf(stderr, "short splice from fuse device\n");
2798 return -EIO;
2801 tmpbuf = (struct fuse_buf) {
2802 .size = res,
2803 .flags = FUSE_BUF_IS_FD,
2804 .fd = llp->pipe[0],
2808 * Don't bother with zero copy for small requests.
2809 * fuse_loop_mt() needs to check for FORGET so this more than
2810 * just an optimization.
2812 if (res < sizeof(struct fuse_in_header) +
2813 sizeof(struct fuse_write_in) + pagesize) {
2814 struct fuse_bufvec src = { .buf[0] = tmpbuf, .count = 1 };
2815 struct fuse_bufvec dst = { .count = 1 };
2817 if (!buf->mem) {
2818 buf->mem = malloc(f->bufsize);
2819 if (!buf->mem) {
2820 fprintf(stderr,
2821 "fuse: failed to allocate read buffer\n");
2822 return -ENOMEM;
2825 buf->size = f->bufsize;
2826 buf->flags = 0;
2827 dst.buf[0] = *buf;
2829 res = fuse_buf_copy(&dst, &src, 0);
2830 if (res < 0) {
2831 fprintf(stderr, "fuse: copy from pipe: %s\n",
2832 strerror(-res));
2833 fuse_ll_clear_pipe(f);
2834 return res;
2836 if (res < tmpbuf.size) {
2837 fprintf(stderr, "fuse: copy from pipe: short read\n");
2838 fuse_ll_clear_pipe(f);
2839 return -EIO;
2841 assert(res == tmpbuf.size);
2843 } else {
2844 /* Don't overwrite buf->mem, as that would cause a leak */
2845 buf->fd = tmpbuf.fd;
2846 buf->flags = tmpbuf.flags;
2848 buf->size = tmpbuf.size;
2850 return res;
2852 fallback:
2853 return fuse_chan_recv(se, buf, ch);
2855 #else
2856 int fuse_session_receive_buf(struct fuse_session *se, struct fuse_buf *buf,
2857 struct fuse_chan *ch)
2859 return fuse_chan_recv(se, buf, ch);
2861 #endif
2863 #define MIN_BUFSIZE 0x21000
2865 struct fuse_session *fuse_lowlevel_new(struct fuse_args *args,
2866 const struct fuse_lowlevel_ops *op,
2867 size_t op_size, void *userdata)
2869 int err;
2870 struct fuse_ll *f;
2871 struct fuse_session *se;
2873 if (sizeof(struct fuse_lowlevel_ops) < op_size) {
2874 fprintf(stderr, "fuse: warning: library too old, some operations may not work\n");
2875 op_size = sizeof(struct fuse_lowlevel_ops);
2878 f = (struct fuse_ll *) calloc(1, sizeof(struct fuse_ll));
2879 if (f == NULL) {
2880 fprintf(stderr, "fuse: failed to allocate fuse object\n");
2881 goto out;
2884 f->conn.async_read = 1;
2885 f->conn.max_write = UINT_MAX;
2886 f->conn.max_readahead = UINT_MAX;
2887 f->atomic_o_trunc = 0;
2888 f->bufsize = getpagesize() + 0x1000;
2889 f->bufsize = f->bufsize < MIN_BUFSIZE ? MIN_BUFSIZE : f->bufsize;
2891 list_init_req(&f->list);
2892 list_init_req(&f->interrupts);
2893 list_init_nreq(&f->notify_list);
2894 f->notify_ctr = 1;
2895 fuse_mutex_init(&f->lock);
2897 err = pthread_key_create(&f->pipe_key, fuse_ll_pipe_destructor);
2898 if (err) {
2899 fprintf(stderr, "fuse: failed to create thread specific key: %s\n",
2900 strerror(err));
2901 goto out_free;
2904 if (fuse_opt_parse(args, f, fuse_ll_opts, fuse_ll_opt_proc) == -1)
2905 goto out_key_destroy;
2907 if (f->debug)
2908 fprintf(stderr, "FUSE library version: %s\n", PACKAGE_VERSION);
2910 memcpy(&f->op, op, op_size);
2911 f->owner = getuid();
2912 f->userdata = userdata;
2914 se = fuse_session_new();
2915 if (!se)
2916 goto out_key_destroy;
2918 se->f = f;
2920 return se;
2922 out_key_destroy:
2923 pthread_key_delete(f->pipe_key);
2924 out_free:
2925 pthread_mutex_destroy(&f->lock);
2926 free(f);
2927 out:
2928 return NULL;
2931 #ifdef linux
2932 int fuse_req_getgroups(fuse_req_t req, int size, gid_t list[])
2934 char *buf;
2935 size_t bufsize = 1024;
2936 char path[128];
2937 int ret;
2938 int fd;
2939 unsigned long pid = req->ctx.pid;
2940 char *s;
2942 sprintf(path, "/proc/%lu/task/%lu/status", pid, pid);
2944 retry:
2945 buf = malloc(bufsize);
2946 if (buf == NULL)
2947 return -ENOMEM;
2949 ret = -EIO;
2950 fd = open(path, O_RDONLY);
2951 if (fd == -1)
2952 goto out_free;
2954 ret = read(fd, buf, bufsize);
2955 close(fd);
2956 if (ret == -1) {
2957 ret = -EIO;
2958 goto out_free;
2961 if (ret == bufsize) {
2962 free(buf);
2963 bufsize *= 4;
2964 goto retry;
2967 ret = -EIO;
2968 s = strstr(buf, "\nGroups:");
2969 if (s == NULL)
2970 goto out_free;
2972 s += 8;
2973 ret = 0;
2974 while (1) {
2975 char *end;
2976 unsigned long val = strtoul(s, &end, 0);
2977 if (end == s)
2978 break;
2980 s = end;
2981 if (ret < size)
2982 list[ret] = val;
2983 ret++;
2986 out_free:
2987 free(buf);
2988 return ret;
2990 #else /* linux */
2992 * This is currently not implemented on other than Linux...
2994 int fuse_req_getgroups(fuse_req_t req, int size, gid_t list[])
2996 return -ENOSYS;
2998 #endif