Merge branch 'clone_fd'
[fuse.git] / lib / fuse_lowlevel.c
blobc5108f73858bbb7b626ca4584940c9ef7e43645f
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 fuse_chan_put(req->ch);
138 req->ch = NULL;
139 pthread_mutex_unlock(&f->lock);
140 if (!ctr)
141 destroy_req(req);
144 static struct fuse_req *fuse_ll_alloc_req(struct fuse_ll *f)
146 struct fuse_req *req;
148 req = (struct fuse_req *) calloc(1, sizeof(struct fuse_req));
149 if (req == NULL) {
150 fprintf(stderr, "fuse: failed to allocate request\n");
151 } else {
152 req->f = f;
153 req->ctr = 1;
154 list_init_req(req);
155 fuse_mutex_init(&req->lock);
158 return req;
161 static int fuse_chan_recv(struct fuse_session *se, struct fuse_buf *buf,
162 struct fuse_chan *ch)
164 struct fuse_ll *f = se->f;
165 int err;
166 ssize_t res;
168 if (!buf->mem) {
169 buf->mem = malloc(f->bufsize);
170 if (!buf->mem) {
171 fprintf(stderr,
172 "fuse: failed to allocate read buffer\n");
173 return -ENOMEM;
177 restart:
178 res = read(fuse_chan_fd(ch), buf->mem, f->bufsize);
179 err = errno;
181 if (fuse_session_exited(se))
182 return 0;
183 if (res == -1) {
184 /* ENOENT means the operation was interrupted, it's safe
185 to restart */
186 if (err == ENOENT)
187 goto restart;
189 if (err == ENODEV) {
190 fuse_session_exit(se);
191 return 0;
193 /* Errors occurring during normal operation: EINTR (read
194 interrupted), EAGAIN (nonblocking I/O), ENODEV (filesystem
195 umounted) */
196 if (err != EINTR && err != EAGAIN)
197 perror("fuse: reading device");
198 return -err;
200 if ((size_t) res < sizeof(struct fuse_in_header)) {
201 fprintf(stderr, "short read on fuse device\n");
202 return -EIO;
205 buf->size = res;
207 return res;
210 static int fuse_chan_send(struct fuse_chan *ch, const struct iovec iov[],
211 size_t count)
213 ssize_t res = writev(fuse_chan_fd(ch), iov, count);
214 int err = errno;
216 if (res == -1) {
217 struct fuse_session *se = fuse_chan_session(ch);
219 assert(se != NULL);
221 /* ENOENT means the operation was interrupted */
222 if (!fuse_session_exited(se) && err != ENOENT)
223 perror("fuse: writing device");
224 return -err;
227 return 0;
230 void fuse_chan_close(struct fuse_chan *ch)
232 int fd = fuse_chan_fd(ch);
233 if (fd != -1)
234 close(fd);
238 static int fuse_send_msg(struct fuse_ll *f, struct fuse_chan *ch,
239 struct iovec *iov, int count)
241 struct fuse_out_header *out = iov[0].iov_base;
243 out->len = iov_length(iov, count);
244 if (f->debug) {
245 if (out->unique == 0) {
246 fprintf(stderr, "NOTIFY: code=%d length=%u\n",
247 out->error, out->len);
248 } else if (out->error) {
249 fprintf(stderr,
250 " unique: %llu, error: %i (%s), outsize: %i\n",
251 (unsigned long long) out->unique, out->error,
252 strerror(-out->error), out->len);
253 } else {
254 fprintf(stderr,
255 " unique: %llu, success, outsize: %i\n",
256 (unsigned long long) out->unique, out->len);
260 return fuse_chan_send(ch, iov, count);
263 int fuse_send_reply_iov_nofree(fuse_req_t req, int error, struct iovec *iov,
264 int count)
266 struct fuse_out_header out;
268 if (error <= -1000 || error > 0) {
269 fprintf(stderr, "fuse: bad error value: %i\n", error);
270 error = -ERANGE;
273 out.unique = req->unique;
274 out.error = error;
276 iov[0].iov_base = &out;
277 iov[0].iov_len = sizeof(struct fuse_out_header);
279 return fuse_send_msg(req->f, req->ch, iov, count);
282 static int send_reply_iov(fuse_req_t req, int error, struct iovec *iov,
283 int count)
285 int res;
287 res = fuse_send_reply_iov_nofree(req, error, iov, count);
288 fuse_free_req(req);
289 return res;
292 static int send_reply(fuse_req_t req, int error, const void *arg,
293 size_t argsize)
295 struct iovec iov[2];
296 int count = 1;
297 if (argsize) {
298 iov[1].iov_base = (void *) arg;
299 iov[1].iov_len = argsize;
300 count++;
302 return send_reply_iov(req, error, iov, count);
305 int fuse_reply_iov(fuse_req_t req, const struct iovec *iov, int count)
307 int res;
308 struct iovec *padded_iov;
310 padded_iov = malloc((count + 1) * sizeof(struct iovec));
311 if (padded_iov == NULL)
312 return fuse_reply_err(req, ENOMEM);
314 memcpy(padded_iov + 1, iov, count * sizeof(struct iovec));
315 count++;
317 res = send_reply_iov(req, 0, padded_iov, count);
318 free(padded_iov);
320 return res;
323 static size_t fuse_dirent_size(size_t namelen)
325 return FUSE_DIRENT_ALIGN(FUSE_NAME_OFFSET + namelen);
328 static void fuse_add_dirent(struct fuse_dirent *dirent, const char *name,
329 const struct stat *stbuf, off_t off)
331 unsigned namelen = strlen(name);
332 unsigned entlen = FUSE_NAME_OFFSET + namelen;
333 unsigned entsize = fuse_dirent_size(namelen);
334 unsigned padlen = entsize - entlen;
336 dirent->ino = stbuf->st_ino;
337 dirent->off = off;
338 dirent->namelen = namelen;
339 dirent->type = (stbuf->st_mode & 0170000) >> 12;
340 strncpy(dirent->name, name, namelen);
341 if (padlen)
342 memset(dirent->name + namelen, 0, padlen);
345 size_t fuse_add_direntry(fuse_req_t req, char *buf, size_t bufsize,
346 const char *name, const struct stat *stbuf, off_t off)
348 size_t entsize;
350 (void) req;
351 entsize = fuse_dirent_size(strlen(name));
352 if (entsize <= bufsize && buf)
353 fuse_add_dirent((struct fuse_dirent *) buf, name, stbuf, off);
354 return entsize;
357 static void convert_statfs(const struct statvfs *stbuf,
358 struct fuse_kstatfs *kstatfs)
360 kstatfs->bsize = stbuf->f_bsize;
361 kstatfs->frsize = stbuf->f_frsize;
362 kstatfs->blocks = stbuf->f_blocks;
363 kstatfs->bfree = stbuf->f_bfree;
364 kstatfs->bavail = stbuf->f_bavail;
365 kstatfs->files = stbuf->f_files;
366 kstatfs->ffree = stbuf->f_ffree;
367 kstatfs->namelen = stbuf->f_namemax;
370 static int send_reply_ok(fuse_req_t req, const void *arg, size_t argsize)
372 return send_reply(req, 0, arg, argsize);
375 int fuse_reply_err(fuse_req_t req, int err)
377 return send_reply(req, -err, NULL, 0);
380 void fuse_reply_none(fuse_req_t req)
382 fuse_free_req(req);
385 static unsigned long calc_timeout_sec(double t)
387 if (t > (double) ULONG_MAX)
388 return ULONG_MAX;
389 else if (t < 0.0)
390 return 0;
391 else
392 return (unsigned long) t;
395 static unsigned int calc_timeout_nsec(double t)
397 double f = t - (double) calc_timeout_sec(t);
398 if (f < 0.0)
399 return 0;
400 else if (f >= 0.999999999)
401 return 999999999;
402 else
403 return (unsigned int) (f * 1.0e9);
406 static void fill_entry(struct fuse_entry_out *arg,
407 const struct fuse_entry_param *e)
409 arg->nodeid = e->ino;
410 arg->generation = e->generation;
411 arg->entry_valid = calc_timeout_sec(e->entry_timeout);
412 arg->entry_valid_nsec = calc_timeout_nsec(e->entry_timeout);
413 arg->attr_valid = calc_timeout_sec(e->attr_timeout);
414 arg->attr_valid_nsec = calc_timeout_nsec(e->attr_timeout);
415 convert_stat(&e->attr, &arg->attr);
418 size_t fuse_add_direntry_plus(fuse_req_t req, char *buf, size_t bufsize,
419 const char *name,
420 const struct fuse_entry_param *e, off_t off)
422 size_t entsize;
424 (void) req;
425 entsize = FUSE_DIRENT_ALIGN(FUSE_NAME_OFFSET_DIRENTPLUS + strlen(name));
426 if (entsize <= bufsize && buf) {
427 struct fuse_direntplus *dp = (struct fuse_direntplus *) buf;
428 memset(&dp->entry_out, 0, sizeof(dp->entry_out));
429 fill_entry(&dp->entry_out, e);
430 fuse_add_dirent(&dp->dirent, name, &e->attr, off);
432 return entsize;
435 static void fill_open(struct fuse_open_out *arg,
436 const struct fuse_file_info *f)
438 arg->fh = f->fh;
439 if (f->direct_io)
440 arg->open_flags |= FOPEN_DIRECT_IO;
441 if (f->keep_cache)
442 arg->open_flags |= FOPEN_KEEP_CACHE;
443 if (f->nonseekable)
444 arg->open_flags |= FOPEN_NONSEEKABLE;
447 int fuse_reply_entry(fuse_req_t req, const struct fuse_entry_param *e)
449 struct fuse_entry_out arg;
450 size_t size = req->f->conn.proto_minor < 9 ?
451 FUSE_COMPAT_ENTRY_OUT_SIZE : sizeof(arg);
453 /* before ABI 7.4 e->ino == 0 was invalid, only ENOENT meant
454 negative entry */
455 if (!e->ino && req->f->conn.proto_minor < 4)
456 return fuse_reply_err(req, ENOENT);
458 memset(&arg, 0, sizeof(arg));
459 fill_entry(&arg, e);
460 return send_reply_ok(req, &arg, size);
463 int fuse_reply_create(fuse_req_t req, const struct fuse_entry_param *e,
464 const struct fuse_file_info *f)
466 char buf[sizeof(struct fuse_entry_out) + sizeof(struct fuse_open_out)];
467 size_t entrysize = req->f->conn.proto_minor < 9 ?
468 FUSE_COMPAT_ENTRY_OUT_SIZE : sizeof(struct fuse_entry_out);
469 struct fuse_entry_out *earg = (struct fuse_entry_out *) buf;
470 struct fuse_open_out *oarg = (struct fuse_open_out *) (buf + entrysize);
472 memset(buf, 0, sizeof(buf));
473 fill_entry(earg, e);
474 fill_open(oarg, f);
475 return send_reply_ok(req, buf,
476 entrysize + sizeof(struct fuse_open_out));
479 int fuse_reply_attr(fuse_req_t req, const struct stat *attr,
480 double attr_timeout)
482 struct fuse_attr_out arg;
483 size_t size = req->f->conn.proto_minor < 9 ?
484 FUSE_COMPAT_ATTR_OUT_SIZE : sizeof(arg);
486 memset(&arg, 0, sizeof(arg));
487 arg.attr_valid = calc_timeout_sec(attr_timeout);
488 arg.attr_valid_nsec = calc_timeout_nsec(attr_timeout);
489 convert_stat(attr, &arg.attr);
491 return send_reply_ok(req, &arg, size);
494 int fuse_reply_readlink(fuse_req_t req, const char *linkname)
496 return send_reply_ok(req, linkname, strlen(linkname));
499 int fuse_reply_open(fuse_req_t req, const struct fuse_file_info *f)
501 struct fuse_open_out arg;
503 memset(&arg, 0, sizeof(arg));
504 fill_open(&arg, f);
505 return send_reply_ok(req, &arg, sizeof(arg));
508 int fuse_reply_write(fuse_req_t req, size_t count)
510 struct fuse_write_out arg;
512 memset(&arg, 0, sizeof(arg));
513 arg.size = count;
515 return send_reply_ok(req, &arg, sizeof(arg));
518 int fuse_reply_buf(fuse_req_t req, const char *buf, size_t size)
520 return send_reply_ok(req, buf, size);
523 static int fuse_send_data_iov_fallback(struct fuse_ll *f, struct fuse_chan *ch,
524 struct iovec *iov, int iov_count,
525 struct fuse_bufvec *buf,
526 size_t len)
528 struct fuse_bufvec mem_buf = FUSE_BUFVEC_INIT(len);
529 void *mbuf;
530 int res;
532 /* Optimize common case */
533 if (buf->count == 1 && buf->idx == 0 && buf->off == 0 &&
534 !(buf->buf[0].flags & FUSE_BUF_IS_FD)) {
535 /* FIXME: also avoid memory copy if there are multiple buffers
536 but none of them contain an fd */
538 iov[iov_count].iov_base = buf->buf[0].mem;
539 iov[iov_count].iov_len = len;
540 iov_count++;
541 return fuse_send_msg(f, ch, iov, iov_count);
544 res = posix_memalign(&mbuf, pagesize, len);
545 if (res != 0)
546 return res;
548 mem_buf.buf[0].mem = mbuf;
549 res = fuse_buf_copy(&mem_buf, buf, 0);
550 if (res < 0) {
551 free(mbuf);
552 return -res;
554 len = res;
556 iov[iov_count].iov_base = mbuf;
557 iov[iov_count].iov_len = len;
558 iov_count++;
559 res = fuse_send_msg(f, ch, iov, iov_count);
560 free(mbuf);
562 return res;
565 struct fuse_ll_pipe {
566 size_t size;
567 int can_grow;
568 int pipe[2];
571 static void fuse_ll_pipe_free(struct fuse_ll_pipe *llp)
573 close(llp->pipe[0]);
574 close(llp->pipe[1]);
575 free(llp);
578 #ifdef HAVE_SPLICE
579 #if !defined(HAVE_PIPE2) || !defined(O_CLOEXEC)
580 static int fuse_pipe(int fds[2])
582 int rv = pipe(fds);
584 if (rv == -1)
585 return rv;
587 if (fcntl(fds[0], F_SETFL, O_NONBLOCK) == -1 ||
588 fcntl(fds[1], F_SETFL, O_NONBLOCK) == -1 ||
589 fcntl(fds[0], F_SETFD, FD_CLOEXEC) == -1 ||
590 fcntl(fds[1], F_SETFD, FD_CLOEXEC) == -1) {
591 close(fds[0]);
592 close(fds[1]);
593 rv = -1;
595 return rv;
597 #else
598 static int fuse_pipe(int fds[2])
600 return pipe2(fds, O_CLOEXEC | O_NONBLOCK);
602 #endif
604 static struct fuse_ll_pipe *fuse_ll_get_pipe(struct fuse_ll *f)
606 struct fuse_ll_pipe *llp = pthread_getspecific(f->pipe_key);
607 if (llp == NULL) {
608 int res;
610 llp = malloc(sizeof(struct fuse_ll_pipe));
611 if (llp == NULL)
612 return NULL;
614 res = fuse_pipe(llp->pipe);
615 if (res == -1) {
616 free(llp);
617 return NULL;
621 *the default size is 16 pages on linux
623 llp->size = pagesize * 16;
624 llp->can_grow = 1;
626 pthread_setspecific(f->pipe_key, llp);
629 return llp;
631 #endif
633 static void fuse_ll_clear_pipe(struct fuse_ll *f)
635 struct fuse_ll_pipe *llp = pthread_getspecific(f->pipe_key);
636 if (llp) {
637 pthread_setspecific(f->pipe_key, NULL);
638 fuse_ll_pipe_free(llp);
642 #if defined(HAVE_SPLICE) && defined(HAVE_VMSPLICE)
643 static int read_back(int fd, char *buf, size_t len)
645 int res;
647 res = read(fd, buf, len);
648 if (res == -1) {
649 fprintf(stderr, "fuse: internal error: failed to read back from pipe: %s\n", strerror(errno));
650 return -EIO;
652 if (res != len) {
653 fprintf(stderr, "fuse: internal error: short read back from pipe: %i from %zi\n", res, len);
654 return -EIO;
656 return 0;
659 static int fuse_send_data_iov(struct fuse_ll *f, struct fuse_chan *ch,
660 struct iovec *iov, int iov_count,
661 struct fuse_bufvec *buf, unsigned int flags)
663 int res;
664 size_t len = fuse_buf_size(buf);
665 struct fuse_out_header *out = iov[0].iov_base;
666 struct fuse_ll_pipe *llp;
667 int splice_flags;
668 size_t pipesize;
669 size_t total_fd_size;
670 size_t idx;
671 size_t headerlen;
672 struct fuse_bufvec pipe_buf = FUSE_BUFVEC_INIT(len);
674 if (f->broken_splice_nonblock)
675 goto fallback;
677 if (flags & FUSE_BUF_NO_SPLICE)
678 goto fallback;
680 total_fd_size = 0;
681 for (idx = buf->idx; idx < buf->count; idx++) {
682 if (buf->buf[idx].flags & FUSE_BUF_IS_FD) {
683 total_fd_size = buf->buf[idx].size;
684 if (idx == buf->idx)
685 total_fd_size -= buf->off;
688 if (total_fd_size < 2 * pagesize)
689 goto fallback;
691 if (f->conn.proto_minor < 14 ||
692 !(f->conn.want & FUSE_CAP_SPLICE_WRITE))
693 goto fallback;
695 llp = fuse_ll_get_pipe(f);
696 if (llp == NULL)
697 goto fallback;
700 headerlen = iov_length(iov, iov_count);
702 out->len = headerlen + len;
705 * Heuristic for the required pipe size, does not work if the
706 * source contains less than page size fragments
708 pipesize = pagesize * (iov_count + buf->count + 1) + out->len;
710 if (llp->size < pipesize) {
711 if (llp->can_grow) {
712 res = fcntl(llp->pipe[0], F_SETPIPE_SZ, pipesize);
713 if (res == -1) {
714 llp->can_grow = 0;
715 goto fallback;
717 llp->size = res;
719 if (llp->size < pipesize)
720 goto fallback;
724 res = vmsplice(llp->pipe[1], iov, iov_count, SPLICE_F_NONBLOCK);
725 if (res == -1)
726 goto fallback;
728 if (res != headerlen) {
729 res = -EIO;
730 fprintf(stderr, "fuse: short vmsplice to pipe: %u/%zu\n", res,
731 headerlen);
732 goto clear_pipe;
735 pipe_buf.buf[0].flags = FUSE_BUF_IS_FD;
736 pipe_buf.buf[0].fd = llp->pipe[1];
738 res = fuse_buf_copy(&pipe_buf, buf,
739 FUSE_BUF_FORCE_SPLICE | FUSE_BUF_SPLICE_NONBLOCK);
740 if (res < 0) {
741 if (res == -EAGAIN || res == -EINVAL) {
743 * Should only get EAGAIN on kernels with
744 * broken SPLICE_F_NONBLOCK support (<=
745 * 2.6.35) where this error or a short read is
746 * returned even if the pipe itself is not
747 * full
749 * EINVAL might mean that splice can't handle
750 * this combination of input and output.
752 if (res == -EAGAIN)
753 f->broken_splice_nonblock = 1;
755 pthread_setspecific(f->pipe_key, NULL);
756 fuse_ll_pipe_free(llp);
757 goto fallback;
759 res = -res;
760 goto clear_pipe;
763 if (res != 0 && res < len) {
764 struct fuse_bufvec mem_buf = FUSE_BUFVEC_INIT(len);
765 void *mbuf;
766 size_t now_len = res;
768 * For regular files a short count is either
769 * 1) due to EOF, or
770 * 2) because of broken SPLICE_F_NONBLOCK (see above)
772 * For other inputs it's possible that we overflowed
773 * the pipe because of small buffer fragments.
776 res = posix_memalign(&mbuf, pagesize, len);
777 if (res != 0)
778 goto clear_pipe;
780 mem_buf.buf[0].mem = mbuf;
781 mem_buf.off = now_len;
782 res = fuse_buf_copy(&mem_buf, buf, 0);
783 if (res > 0) {
784 char *tmpbuf;
785 size_t extra_len = res;
787 * Trickiest case: got more data. Need to get
788 * back the data from the pipe and then fall
789 * back to regular write.
791 tmpbuf = malloc(headerlen);
792 if (tmpbuf == NULL) {
793 free(mbuf);
794 res = ENOMEM;
795 goto clear_pipe;
797 res = read_back(llp->pipe[0], tmpbuf, headerlen);
798 free(tmpbuf);
799 if (res != 0) {
800 free(mbuf);
801 goto clear_pipe;
803 res = read_back(llp->pipe[0], mbuf, now_len);
804 if (res != 0) {
805 free(mbuf);
806 goto clear_pipe;
808 len = now_len + extra_len;
809 iov[iov_count].iov_base = mbuf;
810 iov[iov_count].iov_len = len;
811 iov_count++;
812 res = fuse_send_msg(f, ch, iov, iov_count);
813 free(mbuf);
814 return res;
816 free(mbuf);
817 res = now_len;
819 len = res;
820 out->len = headerlen + len;
822 if (f->debug) {
823 fprintf(stderr,
824 " unique: %llu, success, outsize: %i (splice)\n",
825 (unsigned long long) out->unique, out->len);
828 splice_flags = 0;
829 if ((flags & FUSE_BUF_SPLICE_MOVE) &&
830 (f->conn.want & FUSE_CAP_SPLICE_MOVE))
831 splice_flags |= SPLICE_F_MOVE;
833 res = splice(llp->pipe[0], NULL,
834 fuse_chan_fd(ch), NULL, out->len, splice_flags);
835 if (res == -1) {
836 res = -errno;
837 perror("fuse: splice from pipe");
838 goto clear_pipe;
840 if (res != out->len) {
841 res = -EIO;
842 fprintf(stderr, "fuse: short splice from pipe: %u/%u\n",
843 res, out->len);
844 goto clear_pipe;
846 return 0;
848 clear_pipe:
849 fuse_ll_clear_pipe(f);
850 return res;
852 fallback:
853 return fuse_send_data_iov_fallback(f, ch, iov, iov_count, buf, len);
855 #else
856 static int fuse_send_data_iov(struct fuse_ll *f, struct fuse_chan *ch,
857 struct iovec *iov, int iov_count,
858 struct fuse_bufvec *buf, unsigned int flags)
860 size_t len = fuse_buf_size(buf);
861 (void) flags;
863 return fuse_send_data_iov_fallback(f, ch, iov, iov_count, buf, len);
865 #endif
867 int fuse_reply_data(fuse_req_t req, struct fuse_bufvec *bufv,
868 enum fuse_buf_copy_flags flags)
870 struct iovec iov[2];
871 struct fuse_out_header out;
872 int res;
874 iov[0].iov_base = &out;
875 iov[0].iov_len = sizeof(struct fuse_out_header);
877 out.unique = req->unique;
878 out.error = 0;
880 res = fuse_send_data_iov(req->f, req->ch, iov, 1, bufv, flags);
881 if (res <= 0) {
882 fuse_free_req(req);
883 return res;
884 } else {
885 return fuse_reply_err(req, res);
889 int fuse_reply_statfs(fuse_req_t req, const struct statvfs *stbuf)
891 struct fuse_statfs_out arg;
892 size_t size = req->f->conn.proto_minor < 4 ?
893 FUSE_COMPAT_STATFS_SIZE : sizeof(arg);
895 memset(&arg, 0, sizeof(arg));
896 convert_statfs(stbuf, &arg.st);
898 return send_reply_ok(req, &arg, size);
901 int fuse_reply_xattr(fuse_req_t req, size_t count)
903 struct fuse_getxattr_out arg;
905 memset(&arg, 0, sizeof(arg));
906 arg.size = count;
908 return send_reply_ok(req, &arg, sizeof(arg));
911 int fuse_reply_lock(fuse_req_t req, const struct flock *lock)
913 struct fuse_lk_out arg;
915 memset(&arg, 0, sizeof(arg));
916 arg.lk.type = lock->l_type;
917 if (lock->l_type != F_UNLCK) {
918 arg.lk.start = lock->l_start;
919 if (lock->l_len == 0)
920 arg.lk.end = OFFSET_MAX;
921 else
922 arg.lk.end = lock->l_start + lock->l_len - 1;
924 arg.lk.pid = lock->l_pid;
925 return send_reply_ok(req, &arg, sizeof(arg));
928 int fuse_reply_bmap(fuse_req_t req, uint64_t idx)
930 struct fuse_bmap_out arg;
932 memset(&arg, 0, sizeof(arg));
933 arg.block = idx;
935 return send_reply_ok(req, &arg, sizeof(arg));
938 static struct fuse_ioctl_iovec *fuse_ioctl_iovec_copy(const struct iovec *iov,
939 size_t count)
941 struct fuse_ioctl_iovec *fiov;
942 size_t i;
944 fiov = malloc(sizeof(fiov[0]) * count);
945 if (!fiov)
946 return NULL;
948 for (i = 0; i < count; i++) {
949 fiov[i].base = (uintptr_t) iov[i].iov_base;
950 fiov[i].len = iov[i].iov_len;
953 return fiov;
956 int fuse_reply_ioctl_retry(fuse_req_t req,
957 const struct iovec *in_iov, size_t in_count,
958 const struct iovec *out_iov, size_t out_count)
960 struct fuse_ioctl_out arg;
961 struct fuse_ioctl_iovec *in_fiov = NULL;
962 struct fuse_ioctl_iovec *out_fiov = NULL;
963 struct iovec iov[4];
964 size_t count = 1;
965 int res;
967 memset(&arg, 0, sizeof(arg));
968 arg.flags |= FUSE_IOCTL_RETRY;
969 arg.in_iovs = in_count;
970 arg.out_iovs = out_count;
971 iov[count].iov_base = &arg;
972 iov[count].iov_len = sizeof(arg);
973 count++;
975 if (req->f->conn.proto_minor < 16) {
976 if (in_count) {
977 iov[count].iov_base = (void *)in_iov;
978 iov[count].iov_len = sizeof(in_iov[0]) * in_count;
979 count++;
982 if (out_count) {
983 iov[count].iov_base = (void *)out_iov;
984 iov[count].iov_len = sizeof(out_iov[0]) * out_count;
985 count++;
987 } else {
988 /* Can't handle non-compat 64bit ioctls on 32bit */
989 if (sizeof(void *) == 4 && req->ioctl_64bit) {
990 res = fuse_reply_err(req, EINVAL);
991 goto out;
994 if (in_count) {
995 in_fiov = fuse_ioctl_iovec_copy(in_iov, in_count);
996 if (!in_fiov)
997 goto enomem;
999 iov[count].iov_base = (void *)in_fiov;
1000 iov[count].iov_len = sizeof(in_fiov[0]) * in_count;
1001 count++;
1003 if (out_count) {
1004 out_fiov = fuse_ioctl_iovec_copy(out_iov, out_count);
1005 if (!out_fiov)
1006 goto enomem;
1008 iov[count].iov_base = (void *)out_fiov;
1009 iov[count].iov_len = sizeof(out_fiov[0]) * out_count;
1010 count++;
1014 res = send_reply_iov(req, 0, iov, count);
1015 out:
1016 free(in_fiov);
1017 free(out_fiov);
1019 return res;
1021 enomem:
1022 res = fuse_reply_err(req, ENOMEM);
1023 goto out;
1026 int fuse_reply_ioctl(fuse_req_t req, int result, const void *buf, size_t size)
1028 struct fuse_ioctl_out arg;
1029 struct iovec iov[3];
1030 size_t count = 1;
1032 memset(&arg, 0, sizeof(arg));
1033 arg.result = result;
1034 iov[count].iov_base = &arg;
1035 iov[count].iov_len = sizeof(arg);
1036 count++;
1038 if (size) {
1039 iov[count].iov_base = (char *) buf;
1040 iov[count].iov_len = size;
1041 count++;
1044 return send_reply_iov(req, 0, iov, count);
1047 int fuse_reply_ioctl_iov(fuse_req_t req, int result, const struct iovec *iov,
1048 int count)
1050 struct iovec *padded_iov;
1051 struct fuse_ioctl_out arg;
1052 int res;
1054 padded_iov = malloc((count + 2) * sizeof(struct iovec));
1055 if (padded_iov == NULL)
1056 return fuse_reply_err(req, ENOMEM);
1058 memset(&arg, 0, sizeof(arg));
1059 arg.result = result;
1060 padded_iov[1].iov_base = &arg;
1061 padded_iov[1].iov_len = sizeof(arg);
1063 memcpy(&padded_iov[2], iov, count * sizeof(struct iovec));
1065 res = send_reply_iov(req, 0, padded_iov, count + 2);
1066 free(padded_iov);
1068 return res;
1071 int fuse_reply_poll(fuse_req_t req, unsigned revents)
1073 struct fuse_poll_out arg;
1075 memset(&arg, 0, sizeof(arg));
1076 arg.revents = revents;
1078 return send_reply_ok(req, &arg, sizeof(arg));
1081 static void do_lookup(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1083 char *name = (char *) inarg;
1085 if (req->f->op.lookup)
1086 req->f->op.lookup(req, nodeid, name);
1087 else
1088 fuse_reply_err(req, ENOSYS);
1091 static void do_forget(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1093 struct fuse_forget_in *arg = (struct fuse_forget_in *) inarg;
1095 if (req->f->op.forget)
1096 req->f->op.forget(req, nodeid, arg->nlookup);
1097 else
1098 fuse_reply_none(req);
1101 static void do_batch_forget(fuse_req_t req, fuse_ino_t nodeid,
1102 const void *inarg)
1104 struct fuse_batch_forget_in *arg = (void *) inarg;
1105 struct fuse_forget_one *param = (void *) PARAM(arg);
1106 unsigned int i;
1108 (void) nodeid;
1110 if (req->f->op.forget_multi) {
1111 req->f->op.forget_multi(req, arg->count,
1112 (struct fuse_forget_data *) param);
1113 } else if (req->f->op.forget) {
1114 for (i = 0; i < arg->count; i++) {
1115 struct fuse_forget_one *forget = &param[i];
1116 struct fuse_req *dummy_req;
1118 dummy_req = fuse_ll_alloc_req(req->f);
1119 if (dummy_req == NULL)
1120 break;
1122 dummy_req->unique = req->unique;
1123 dummy_req->ctx = req->ctx;
1124 dummy_req->ch = NULL;
1126 req->f->op.forget(dummy_req, forget->nodeid,
1127 forget->nlookup);
1129 fuse_reply_none(req);
1130 } else {
1131 fuse_reply_none(req);
1135 static void do_getattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1137 struct fuse_file_info *fip = NULL;
1138 struct fuse_file_info fi;
1140 if (req->f->conn.proto_minor >= 9) {
1141 struct fuse_getattr_in *arg = (struct fuse_getattr_in *) inarg;
1143 if (arg->getattr_flags & FUSE_GETATTR_FH) {
1144 memset(&fi, 0, sizeof(fi));
1145 fi.fh = arg->fh;
1146 fip = &fi;
1150 if (req->f->op.getattr)
1151 req->f->op.getattr(req, nodeid, fip);
1152 else
1153 fuse_reply_err(req, ENOSYS);
1156 static void do_setattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1158 struct fuse_setattr_in *arg = (struct fuse_setattr_in *) inarg;
1160 if (req->f->op.setattr) {
1161 struct fuse_file_info *fi = NULL;
1162 struct fuse_file_info fi_store;
1163 struct stat stbuf;
1164 memset(&stbuf, 0, sizeof(stbuf));
1165 convert_attr(arg, &stbuf);
1166 if (arg->valid & FATTR_FH) {
1167 arg->valid &= ~FATTR_FH;
1168 memset(&fi_store, 0, sizeof(fi_store));
1169 fi = &fi_store;
1170 fi->fh = arg->fh;
1172 arg->valid &=
1173 FUSE_SET_ATTR_MODE |
1174 FUSE_SET_ATTR_UID |
1175 FUSE_SET_ATTR_GID |
1176 FUSE_SET_ATTR_SIZE |
1177 FUSE_SET_ATTR_ATIME |
1178 FUSE_SET_ATTR_MTIME |
1179 FUSE_SET_ATTR_ATIME_NOW |
1180 FUSE_SET_ATTR_MTIME_NOW |
1181 FUSE_SET_ATTR_CTIME;
1183 req->f->op.setattr(req, nodeid, &stbuf, arg->valid, fi);
1184 } else
1185 fuse_reply_err(req, ENOSYS);
1188 static void do_access(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1190 struct fuse_access_in *arg = (struct fuse_access_in *) inarg;
1192 if (req->f->op.access)
1193 req->f->op.access(req, nodeid, arg->mask);
1194 else
1195 fuse_reply_err(req, ENOSYS);
1198 static void do_readlink(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1200 (void) inarg;
1202 if (req->f->op.readlink)
1203 req->f->op.readlink(req, nodeid);
1204 else
1205 fuse_reply_err(req, ENOSYS);
1208 static void do_mknod(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1210 struct fuse_mknod_in *arg = (struct fuse_mknod_in *) inarg;
1211 char *name = PARAM(arg);
1213 if (req->f->conn.proto_minor >= 12)
1214 req->ctx.umask = arg->umask;
1215 else
1216 name = (char *) inarg + FUSE_COMPAT_MKNOD_IN_SIZE;
1218 if (req->f->op.mknod)
1219 req->f->op.mknod(req, nodeid, name, arg->mode, arg->rdev);
1220 else
1221 fuse_reply_err(req, ENOSYS);
1224 static void do_mkdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1226 struct fuse_mkdir_in *arg = (struct fuse_mkdir_in *) inarg;
1228 if (req->f->conn.proto_minor >= 12)
1229 req->ctx.umask = arg->umask;
1231 if (req->f->op.mkdir)
1232 req->f->op.mkdir(req, nodeid, PARAM(arg), arg->mode);
1233 else
1234 fuse_reply_err(req, ENOSYS);
1237 static void do_unlink(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1239 char *name = (char *) inarg;
1241 if (req->f->op.unlink)
1242 req->f->op.unlink(req, nodeid, name);
1243 else
1244 fuse_reply_err(req, ENOSYS);
1247 static void do_rmdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1249 char *name = (char *) inarg;
1251 if (req->f->op.rmdir)
1252 req->f->op.rmdir(req, nodeid, name);
1253 else
1254 fuse_reply_err(req, ENOSYS);
1257 static void do_symlink(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1259 char *name = (char *) inarg;
1260 char *linkname = ((char *) inarg) + strlen((char *) inarg) + 1;
1262 if (req->f->op.symlink)
1263 req->f->op.symlink(req, linkname, nodeid, name);
1264 else
1265 fuse_reply_err(req, ENOSYS);
1268 static void do_rename(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1270 struct fuse_rename_in *arg = (struct fuse_rename_in *) inarg;
1271 char *oldname = PARAM(arg);
1272 char *newname = oldname + strlen(oldname) + 1;
1274 if (req->f->op.rename)
1275 req->f->op.rename(req, nodeid, oldname, arg->newdir, newname,
1277 else
1278 fuse_reply_err(req, ENOSYS);
1281 static void do_rename2(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1283 struct fuse_rename2_in *arg = (struct fuse_rename2_in *) inarg;
1284 char *oldname = PARAM(arg);
1285 char *newname = oldname + strlen(oldname) + 1;
1287 if (req->f->op.rename)
1288 req->f->op.rename(req, nodeid, oldname, arg->newdir, newname,
1289 arg->flags);
1290 else
1291 fuse_reply_err(req, ENOSYS);
1294 static void do_link(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1296 struct fuse_link_in *arg = (struct fuse_link_in *) inarg;
1298 if (req->f->op.link)
1299 req->f->op.link(req, arg->oldnodeid, nodeid, PARAM(arg));
1300 else
1301 fuse_reply_err(req, ENOSYS);
1304 static void do_create(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1306 struct fuse_create_in *arg = (struct fuse_create_in *) inarg;
1308 if (req->f->op.create) {
1309 struct fuse_file_info fi;
1310 char *name = PARAM(arg);
1312 memset(&fi, 0, sizeof(fi));
1313 fi.flags = arg->flags;
1315 if (req->f->conn.proto_minor >= 12)
1316 req->ctx.umask = arg->umask;
1317 else
1318 name = (char *) inarg + sizeof(struct fuse_open_in);
1320 req->f->op.create(req, nodeid, name, arg->mode, &fi);
1321 } else
1322 fuse_reply_err(req, ENOSYS);
1325 static void do_open(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1327 struct fuse_open_in *arg = (struct fuse_open_in *) inarg;
1328 struct fuse_file_info fi;
1330 memset(&fi, 0, sizeof(fi));
1331 fi.flags = arg->flags;
1333 if (req->f->op.open)
1334 req->f->op.open(req, nodeid, &fi);
1335 else
1336 fuse_reply_open(req, &fi);
1339 static void do_read(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1341 struct fuse_read_in *arg = (struct fuse_read_in *) inarg;
1343 if (req->f->op.read) {
1344 struct fuse_file_info fi;
1346 memset(&fi, 0, sizeof(fi));
1347 fi.fh = arg->fh;
1348 if (req->f->conn.proto_minor >= 9) {
1349 fi.lock_owner = arg->lock_owner;
1350 fi.flags = arg->flags;
1352 req->f->op.read(req, nodeid, arg->size, arg->offset, &fi);
1353 } else
1354 fuse_reply_err(req, ENOSYS);
1357 static void do_write(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1359 struct fuse_write_in *arg = (struct fuse_write_in *) inarg;
1360 struct fuse_file_info fi;
1361 char *param;
1363 memset(&fi, 0, sizeof(fi));
1364 fi.fh = arg->fh;
1365 fi.writepage = (arg->write_flags & 1) != 0;
1367 if (req->f->conn.proto_minor < 9) {
1368 param = ((char *) arg) + FUSE_COMPAT_WRITE_IN_SIZE;
1369 } else {
1370 fi.lock_owner = arg->lock_owner;
1371 fi.flags = arg->flags;
1372 param = PARAM(arg);
1375 if (req->f->op.write)
1376 req->f->op.write(req, nodeid, param, arg->size,
1377 arg->offset, &fi);
1378 else
1379 fuse_reply_err(req, ENOSYS);
1382 static void do_write_buf(fuse_req_t req, fuse_ino_t nodeid, const void *inarg,
1383 const struct fuse_buf *ibuf)
1385 struct fuse_ll *f = req->f;
1386 struct fuse_bufvec bufv = {
1387 .buf[0] = *ibuf,
1388 .count = 1,
1390 struct fuse_write_in *arg = (struct fuse_write_in *) inarg;
1391 struct fuse_file_info fi;
1393 memset(&fi, 0, sizeof(fi));
1394 fi.fh = arg->fh;
1395 fi.writepage = arg->write_flags & 1;
1397 if (req->f->conn.proto_minor < 9) {
1398 bufv.buf[0].mem = ((char *) arg) + FUSE_COMPAT_WRITE_IN_SIZE;
1399 bufv.buf[0].size -= sizeof(struct fuse_in_header) +
1400 FUSE_COMPAT_WRITE_IN_SIZE;
1401 assert(!(bufv.buf[0].flags & FUSE_BUF_IS_FD));
1402 } else {
1403 fi.lock_owner = arg->lock_owner;
1404 fi.flags = arg->flags;
1405 if (!(bufv.buf[0].flags & FUSE_BUF_IS_FD))
1406 bufv.buf[0].mem = PARAM(arg);
1408 bufv.buf[0].size -= sizeof(struct fuse_in_header) +
1409 sizeof(struct fuse_write_in);
1411 if (bufv.buf[0].size < arg->size) {
1412 fprintf(stderr, "fuse: do_write_buf: buffer size too small\n");
1413 fuse_reply_err(req, EIO);
1414 goto out;
1416 bufv.buf[0].size = arg->size;
1418 req->f->op.write_buf(req, nodeid, &bufv, arg->offset, &fi);
1420 out:
1421 /* Need to reset the pipe if ->write_buf() didn't consume all data */
1422 if ((ibuf->flags & FUSE_BUF_IS_FD) && bufv.idx < bufv.count)
1423 fuse_ll_clear_pipe(f);
1426 static void do_flush(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1428 struct fuse_flush_in *arg = (struct fuse_flush_in *) inarg;
1429 struct fuse_file_info fi;
1431 memset(&fi, 0, sizeof(fi));
1432 fi.fh = arg->fh;
1433 fi.flush = 1;
1434 if (req->f->conn.proto_minor >= 7)
1435 fi.lock_owner = arg->lock_owner;
1437 if (req->f->op.flush)
1438 req->f->op.flush(req, nodeid, &fi);
1439 else
1440 fuse_reply_err(req, ENOSYS);
1443 static void do_release(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1445 struct fuse_release_in *arg = (struct fuse_release_in *) inarg;
1446 struct fuse_file_info fi;
1448 memset(&fi, 0, sizeof(fi));
1449 fi.flags = arg->flags;
1450 fi.fh = arg->fh;
1451 if (req->f->conn.proto_minor >= 8) {
1452 fi.flush = (arg->release_flags & FUSE_RELEASE_FLUSH) ? 1 : 0;
1453 fi.lock_owner = arg->lock_owner;
1455 if (arg->release_flags & FUSE_RELEASE_FLOCK_UNLOCK) {
1456 fi.flock_release = 1;
1457 fi.lock_owner = arg->lock_owner;
1460 if (req->f->op.release)
1461 req->f->op.release(req, nodeid, &fi);
1462 else
1463 fuse_reply_err(req, 0);
1466 static void do_fsync(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1468 struct fuse_fsync_in *arg = (struct fuse_fsync_in *) inarg;
1469 struct fuse_file_info fi;
1471 memset(&fi, 0, sizeof(fi));
1472 fi.fh = arg->fh;
1474 if (req->f->op.fsync)
1475 req->f->op.fsync(req, nodeid, arg->fsync_flags & 1, &fi);
1476 else
1477 fuse_reply_err(req, ENOSYS);
1480 static void do_opendir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1482 struct fuse_open_in *arg = (struct fuse_open_in *) inarg;
1483 struct fuse_file_info fi;
1485 memset(&fi, 0, sizeof(fi));
1486 fi.flags = arg->flags;
1488 if (req->f->op.opendir)
1489 req->f->op.opendir(req, nodeid, &fi);
1490 else
1491 fuse_reply_open(req, &fi);
1494 static void do_readdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1496 struct fuse_read_in *arg = (struct fuse_read_in *) inarg;
1497 struct fuse_file_info fi;
1499 memset(&fi, 0, sizeof(fi));
1500 fi.fh = arg->fh;
1502 if (req->f->op.readdir)
1503 req->f->op.readdir(req, nodeid, arg->size, arg->offset, &fi);
1504 else
1505 fuse_reply_err(req, ENOSYS);
1508 static void do_readdirplus(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1510 struct fuse_read_in *arg = (struct fuse_read_in *) inarg;
1511 struct fuse_file_info fi;
1513 memset(&fi, 0, sizeof(fi));
1514 fi.fh = arg->fh;
1516 if (req->f->op.readdirplus)
1517 req->f->op.readdirplus(req, nodeid, arg->size, arg->offset, &fi);
1518 else
1519 fuse_reply_err(req, ENOSYS);
1522 static void do_releasedir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1524 struct fuse_release_in *arg = (struct fuse_release_in *) inarg;
1525 struct fuse_file_info fi;
1527 memset(&fi, 0, sizeof(fi));
1528 fi.flags = arg->flags;
1529 fi.fh = arg->fh;
1531 if (req->f->op.releasedir)
1532 req->f->op.releasedir(req, nodeid, &fi);
1533 else
1534 fuse_reply_err(req, 0);
1537 static void do_fsyncdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1539 struct fuse_fsync_in *arg = (struct fuse_fsync_in *) inarg;
1540 struct fuse_file_info fi;
1542 memset(&fi, 0, sizeof(fi));
1543 fi.fh = arg->fh;
1545 if (req->f->op.fsyncdir)
1546 req->f->op.fsyncdir(req, nodeid, arg->fsync_flags & 1, &fi);
1547 else
1548 fuse_reply_err(req, ENOSYS);
1551 static void do_statfs(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1553 (void) nodeid;
1554 (void) inarg;
1556 if (req->f->op.statfs)
1557 req->f->op.statfs(req, nodeid);
1558 else {
1559 struct statvfs buf = {
1560 .f_namemax = 255,
1561 .f_bsize = 512,
1563 fuse_reply_statfs(req, &buf);
1567 static void do_setxattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1569 struct fuse_setxattr_in *arg = (struct fuse_setxattr_in *) inarg;
1570 char *name = PARAM(arg);
1571 char *value = name + strlen(name) + 1;
1573 if (req->f->op.setxattr)
1574 req->f->op.setxattr(req, nodeid, name, value, arg->size,
1575 arg->flags);
1576 else
1577 fuse_reply_err(req, ENOSYS);
1580 static void do_getxattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1582 struct fuse_getxattr_in *arg = (struct fuse_getxattr_in *) inarg;
1584 if (req->f->op.getxattr)
1585 req->f->op.getxattr(req, nodeid, PARAM(arg), arg->size);
1586 else
1587 fuse_reply_err(req, ENOSYS);
1590 static void do_listxattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1592 struct fuse_getxattr_in *arg = (struct fuse_getxattr_in *) inarg;
1594 if (req->f->op.listxattr)
1595 req->f->op.listxattr(req, nodeid, arg->size);
1596 else
1597 fuse_reply_err(req, ENOSYS);
1600 static void do_removexattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1602 char *name = (char *) inarg;
1604 if (req->f->op.removexattr)
1605 req->f->op.removexattr(req, nodeid, name);
1606 else
1607 fuse_reply_err(req, ENOSYS);
1610 static void convert_fuse_file_lock(struct fuse_file_lock *fl,
1611 struct flock *flock)
1613 memset(flock, 0, sizeof(struct flock));
1614 flock->l_type = fl->type;
1615 flock->l_whence = SEEK_SET;
1616 flock->l_start = fl->start;
1617 if (fl->end == OFFSET_MAX)
1618 flock->l_len = 0;
1619 else
1620 flock->l_len = fl->end - fl->start + 1;
1621 flock->l_pid = fl->pid;
1624 static void do_getlk(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1626 struct fuse_lk_in *arg = (struct fuse_lk_in *) inarg;
1627 struct fuse_file_info fi;
1628 struct flock flock;
1630 memset(&fi, 0, sizeof(fi));
1631 fi.fh = arg->fh;
1632 fi.lock_owner = arg->owner;
1634 convert_fuse_file_lock(&arg->lk, &flock);
1635 if (req->f->op.getlk)
1636 req->f->op.getlk(req, nodeid, &fi, &flock);
1637 else
1638 fuse_reply_err(req, ENOSYS);
1641 static void do_setlk_common(fuse_req_t req, fuse_ino_t nodeid,
1642 const void *inarg, int sleep)
1644 struct fuse_lk_in *arg = (struct fuse_lk_in *) inarg;
1645 struct fuse_file_info fi;
1646 struct flock flock;
1648 memset(&fi, 0, sizeof(fi));
1649 fi.fh = arg->fh;
1650 fi.lock_owner = arg->owner;
1652 if (arg->lk_flags & FUSE_LK_FLOCK) {
1653 int op = 0;
1655 switch (arg->lk.type) {
1656 case F_RDLCK:
1657 op = LOCK_SH;
1658 break;
1659 case F_WRLCK:
1660 op = LOCK_EX;
1661 break;
1662 case F_UNLCK:
1663 op = LOCK_UN;
1664 break;
1666 if (!sleep)
1667 op |= LOCK_NB;
1669 if (req->f->op.flock)
1670 req->f->op.flock(req, nodeid, &fi, op);
1671 else
1672 fuse_reply_err(req, ENOSYS);
1673 } else {
1674 convert_fuse_file_lock(&arg->lk, &flock);
1675 if (req->f->op.setlk)
1676 req->f->op.setlk(req, nodeid, &fi, &flock, sleep);
1677 else
1678 fuse_reply_err(req, ENOSYS);
1682 static void do_setlk(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1684 do_setlk_common(req, nodeid, inarg, 0);
1687 static void do_setlkw(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1689 do_setlk_common(req, nodeid, inarg, 1);
1692 static int find_interrupted(struct fuse_ll *f, struct fuse_req *req)
1694 struct fuse_req *curr;
1696 for (curr = f->list.next; curr != &f->list; curr = curr->next) {
1697 if (curr->unique == req->u.i.unique) {
1698 fuse_interrupt_func_t func;
1699 void *data;
1701 curr->ctr++;
1702 pthread_mutex_unlock(&f->lock);
1704 /* Ugh, ugly locking */
1705 pthread_mutex_lock(&curr->lock);
1706 pthread_mutex_lock(&f->lock);
1707 curr->interrupted = 1;
1708 func = curr->u.ni.func;
1709 data = curr->u.ni.data;
1710 pthread_mutex_unlock(&f->lock);
1711 if (func)
1712 func(curr, data);
1713 pthread_mutex_unlock(&curr->lock);
1715 pthread_mutex_lock(&f->lock);
1716 curr->ctr--;
1717 if (!curr->ctr)
1718 destroy_req(curr);
1720 return 1;
1723 for (curr = f->interrupts.next; curr != &f->interrupts;
1724 curr = curr->next) {
1725 if (curr->u.i.unique == req->u.i.unique)
1726 return 1;
1728 return 0;
1731 static void do_interrupt(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1733 struct fuse_interrupt_in *arg = (struct fuse_interrupt_in *) inarg;
1734 struct fuse_ll *f = req->f;
1736 (void) nodeid;
1737 if (f->debug)
1738 fprintf(stderr, "INTERRUPT: %llu\n",
1739 (unsigned long long) arg->unique);
1741 req->u.i.unique = arg->unique;
1743 pthread_mutex_lock(&f->lock);
1744 if (find_interrupted(f, req))
1745 destroy_req(req);
1746 else
1747 list_add_req(req, &f->interrupts);
1748 pthread_mutex_unlock(&f->lock);
1751 static struct fuse_req *check_interrupt(struct fuse_ll *f, struct fuse_req *req)
1753 struct fuse_req *curr;
1755 for (curr = f->interrupts.next; curr != &f->interrupts;
1756 curr = curr->next) {
1757 if (curr->u.i.unique == req->unique) {
1758 req->interrupted = 1;
1759 list_del_req(curr);
1760 free(curr);
1761 return NULL;
1764 curr = f->interrupts.next;
1765 if (curr != &f->interrupts) {
1766 list_del_req(curr);
1767 list_init_req(curr);
1768 return curr;
1769 } else
1770 return NULL;
1773 static void do_bmap(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1775 struct fuse_bmap_in *arg = (struct fuse_bmap_in *) inarg;
1777 if (req->f->op.bmap)
1778 req->f->op.bmap(req, nodeid, arg->blocksize, arg->block);
1779 else
1780 fuse_reply_err(req, ENOSYS);
1783 static void do_ioctl(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1785 struct fuse_ioctl_in *arg = (struct fuse_ioctl_in *) inarg;
1786 unsigned int flags = arg->flags;
1787 void *in_buf = arg->in_size ? PARAM(arg) : NULL;
1788 struct fuse_file_info fi;
1790 if (flags & FUSE_IOCTL_DIR &&
1791 !(req->f->conn.want & FUSE_CAP_IOCTL_DIR)) {
1792 fuse_reply_err(req, ENOTTY);
1793 return;
1796 memset(&fi, 0, sizeof(fi));
1797 fi.fh = arg->fh;
1799 if (sizeof(void *) == 4 && req->f->conn.proto_minor >= 16 &&
1800 !(flags & FUSE_IOCTL_32BIT)) {
1801 req->ioctl_64bit = 1;
1804 if (req->f->op.ioctl)
1805 req->f->op.ioctl(req, nodeid, arg->cmd,
1806 (void *)(uintptr_t)arg->arg, &fi, flags,
1807 in_buf, arg->in_size, arg->out_size);
1808 else
1809 fuse_reply_err(req, ENOSYS);
1812 void fuse_pollhandle_destroy(struct fuse_pollhandle *ph)
1814 free(ph);
1817 static void do_poll(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1819 struct fuse_poll_in *arg = (struct fuse_poll_in *) inarg;
1820 struct fuse_file_info fi;
1822 memset(&fi, 0, sizeof(fi));
1823 fi.fh = arg->fh;
1824 fi.poll_events = arg->events;
1826 if (req->f->op.poll) {
1827 struct fuse_pollhandle *ph = NULL;
1829 if (arg->flags & FUSE_POLL_SCHEDULE_NOTIFY) {
1830 ph = malloc(sizeof(struct fuse_pollhandle));
1831 if (ph == NULL) {
1832 fuse_reply_err(req, ENOMEM);
1833 return;
1835 ph->kh = arg->kh;
1836 ph->ch = req->ch;
1837 ph->f = req->f;
1840 req->f->op.poll(req, nodeid, &fi, ph);
1841 } else {
1842 fuse_reply_err(req, ENOSYS);
1846 static void do_fallocate(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1848 struct fuse_fallocate_in *arg = (struct fuse_fallocate_in *) inarg;
1849 struct fuse_file_info fi;
1851 memset(&fi, 0, sizeof(fi));
1852 fi.fh = arg->fh;
1854 if (req->f->op.fallocate)
1855 req->f->op.fallocate(req, nodeid, arg->mode, arg->offset, arg->length, &fi);
1856 else
1857 fuse_reply_err(req, ENOSYS);
1860 static void do_init(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1862 struct fuse_init_in *arg = (struct fuse_init_in *) inarg;
1863 struct fuse_init_out outarg;
1864 struct fuse_ll *f = req->f;
1865 size_t bufsize = f->bufsize;
1866 size_t outargsize = sizeof(outarg);
1868 (void) nodeid;
1869 if (f->debug) {
1870 fprintf(stderr, "INIT: %u.%u\n", arg->major, arg->minor);
1871 if (arg->major == 7 && arg->minor >= 6) {
1872 fprintf(stderr, "flags=0x%08x\n", arg->flags);
1873 fprintf(stderr, "max_readahead=0x%08x\n",
1874 arg->max_readahead);
1877 f->conn.proto_major = arg->major;
1878 f->conn.proto_minor = arg->minor;
1879 f->conn.capable = 0;
1880 f->conn.want = 0;
1882 memset(&outarg, 0, sizeof(outarg));
1883 outarg.major = FUSE_KERNEL_VERSION;
1884 outarg.minor = FUSE_KERNEL_MINOR_VERSION;
1886 if (arg->major < 7) {
1887 fprintf(stderr, "fuse: unsupported protocol version: %u.%u\n",
1888 arg->major, arg->minor);
1889 fuse_reply_err(req, EPROTO);
1890 return;
1893 if (arg->major > 7) {
1894 /* Wait for a second INIT request with a 7.X version */
1895 send_reply_ok(req, &outarg, sizeof(outarg));
1896 return;
1899 if (arg->minor >= 6) {
1900 if (f->conn.async_read)
1901 f->conn.async_read = arg->flags & FUSE_ASYNC_READ;
1902 if (arg->max_readahead < f->conn.max_readahead)
1903 f->conn.max_readahead = arg->max_readahead;
1904 if (arg->flags & FUSE_ASYNC_READ)
1905 f->conn.capable |= FUSE_CAP_ASYNC_READ;
1906 if (arg->flags & FUSE_POSIX_LOCKS)
1907 f->conn.capable |= FUSE_CAP_POSIX_LOCKS;
1908 if (arg->flags & FUSE_ATOMIC_O_TRUNC)
1909 f->conn.capable |= FUSE_CAP_ATOMIC_O_TRUNC;
1910 if (arg->flags & FUSE_EXPORT_SUPPORT)
1911 f->conn.capable |= FUSE_CAP_EXPORT_SUPPORT;
1912 if (arg->flags & FUSE_BIG_WRITES)
1913 f->conn.capable |= FUSE_CAP_BIG_WRITES;
1914 if (arg->flags & FUSE_DONT_MASK)
1915 f->conn.capable |= FUSE_CAP_DONT_MASK;
1916 if (arg->flags & FUSE_FLOCK_LOCKS)
1917 f->conn.capable |= FUSE_CAP_FLOCK_LOCKS;
1918 if (arg->flags & FUSE_AUTO_INVAL_DATA)
1919 f->conn.capable |= FUSE_CAP_AUTO_INVAL_DATA;
1920 if (arg->flags & FUSE_DO_READDIRPLUS)
1921 f->conn.capable |= FUSE_CAP_READDIRPLUS;
1922 if (arg->flags & FUSE_READDIRPLUS_AUTO)
1923 f->conn.capable |= FUSE_CAP_READDIRPLUS_AUTO;
1924 if (arg->flags & FUSE_ASYNC_DIO)
1925 f->conn.capable |= FUSE_CAP_ASYNC_DIO;
1926 if (arg->flags & FUSE_WRITEBACK_CACHE)
1927 f->conn.capable |= FUSE_CAP_WRITEBACK_CACHE;
1928 if (arg->flags & FUSE_NO_OPEN_SUPPORT)
1929 f->conn.capable |= FUSE_CAP_NO_OPEN_SUPPORT;
1930 } else {
1931 f->conn.async_read = 0;
1932 f->conn.max_readahead = 0;
1935 if (req->f->conn.proto_minor >= 14) {
1936 #ifdef HAVE_SPLICE
1937 #ifdef HAVE_VMSPLICE
1938 f->conn.capable |= FUSE_CAP_SPLICE_WRITE | FUSE_CAP_SPLICE_MOVE;
1939 if (f->splice_write)
1940 f->conn.want |= FUSE_CAP_SPLICE_WRITE;
1941 if (f->splice_move)
1942 f->conn.want |= FUSE_CAP_SPLICE_MOVE;
1943 #endif
1944 f->conn.capable |= FUSE_CAP_SPLICE_READ;
1945 if (f->splice_read)
1946 f->conn.want |= FUSE_CAP_SPLICE_READ;
1947 #endif
1949 if (req->f->conn.proto_minor >= 18)
1950 f->conn.capable |= FUSE_CAP_IOCTL_DIR;
1952 if (f->atomic_o_trunc)
1953 f->conn.want |= FUSE_CAP_ATOMIC_O_TRUNC;
1954 if (f->op.getlk && f->op.setlk && !f->no_remote_posix_lock)
1955 f->conn.want |= FUSE_CAP_POSIX_LOCKS;
1956 if (f->op.flock && !f->no_remote_flock)
1957 f->conn.want |= FUSE_CAP_FLOCK_LOCKS;
1958 if (f->big_writes)
1959 f->conn.want |= FUSE_CAP_BIG_WRITES;
1960 if (f->auto_inval_data)
1961 f->conn.want |= FUSE_CAP_AUTO_INVAL_DATA;
1962 if (f->op.readdirplus && !f->no_readdirplus) {
1963 f->conn.want |= FUSE_CAP_READDIRPLUS;
1964 if (!f->no_readdirplus_auto)
1965 f->conn.want |= FUSE_CAP_READDIRPLUS_AUTO;
1967 if (f->async_dio)
1968 f->conn.want |= FUSE_CAP_ASYNC_DIO;
1969 if (f->writeback_cache)
1970 f->conn.want |= FUSE_CAP_WRITEBACK_CACHE;
1972 if (bufsize < FUSE_MIN_READ_BUFFER) {
1973 fprintf(stderr, "fuse: warning: buffer size too small: %zu\n",
1974 bufsize);
1975 bufsize = FUSE_MIN_READ_BUFFER;
1978 bufsize -= 4096;
1979 if (bufsize < f->conn.max_write)
1980 f->conn.max_write = bufsize;
1982 f->got_init = 1;
1983 if (f->op.init)
1984 f->op.init(f->userdata, &f->conn);
1986 if (f->no_splice_read)
1987 f->conn.want &= ~FUSE_CAP_SPLICE_READ;
1988 if (f->no_splice_write)
1989 f->conn.want &= ~FUSE_CAP_SPLICE_WRITE;
1990 if (f->no_splice_move)
1991 f->conn.want &= ~FUSE_CAP_SPLICE_MOVE;
1992 if (f->no_auto_inval_data)
1993 f->conn.want &= ~FUSE_CAP_AUTO_INVAL_DATA;
1994 if (f->no_readdirplus)
1995 f->conn.want &= ~FUSE_CAP_READDIRPLUS;
1996 if (f->no_readdirplus_auto)
1997 f->conn.want &= ~FUSE_CAP_READDIRPLUS_AUTO;
1998 if (f->no_async_dio)
1999 f->conn.want &= ~FUSE_CAP_ASYNC_DIO;
2000 if (f->no_writeback_cache)
2001 f->conn.want &= ~FUSE_CAP_WRITEBACK_CACHE;
2003 if (f->conn.async_read || (f->conn.want & FUSE_CAP_ASYNC_READ))
2004 outarg.flags |= FUSE_ASYNC_READ;
2005 if (f->conn.want & FUSE_CAP_POSIX_LOCKS)
2006 outarg.flags |= FUSE_POSIX_LOCKS;
2007 if (f->conn.want & FUSE_CAP_ATOMIC_O_TRUNC)
2008 outarg.flags |= FUSE_ATOMIC_O_TRUNC;
2009 if (f->conn.want & FUSE_CAP_EXPORT_SUPPORT)
2010 outarg.flags |= FUSE_EXPORT_SUPPORT;
2011 if (f->conn.want & FUSE_CAP_BIG_WRITES)
2012 outarg.flags |= FUSE_BIG_WRITES;
2013 if (f->conn.want & FUSE_CAP_DONT_MASK)
2014 outarg.flags |= FUSE_DONT_MASK;
2015 if (f->conn.want & FUSE_CAP_FLOCK_LOCKS)
2016 outarg.flags |= FUSE_FLOCK_LOCKS;
2017 if (f->conn.want & FUSE_CAP_AUTO_INVAL_DATA)
2018 outarg.flags |= FUSE_AUTO_INVAL_DATA;
2019 if (f->conn.want & FUSE_CAP_READDIRPLUS)
2020 outarg.flags |= FUSE_DO_READDIRPLUS;
2021 if (f->conn.want & FUSE_CAP_READDIRPLUS_AUTO)
2022 outarg.flags |= FUSE_READDIRPLUS_AUTO;
2023 if (f->conn.want & FUSE_CAP_ASYNC_DIO)
2024 outarg.flags |= FUSE_ASYNC_DIO;
2025 if (f->conn.want & FUSE_CAP_WRITEBACK_CACHE)
2026 outarg.flags |= FUSE_WRITEBACK_CACHE;
2027 outarg.max_readahead = f->conn.max_readahead;
2028 outarg.max_write = f->conn.max_write;
2029 if (f->conn.proto_minor >= 13) {
2030 if (f->conn.max_background >= (1 << 16))
2031 f->conn.max_background = (1 << 16) - 1;
2032 if (f->conn.congestion_threshold > f->conn.max_background)
2033 f->conn.congestion_threshold = f->conn.max_background;
2034 if (!f->conn.congestion_threshold) {
2035 f->conn.congestion_threshold =
2036 f->conn.max_background * 3 / 4;
2039 outarg.max_background = f->conn.max_background;
2040 outarg.congestion_threshold = f->conn.congestion_threshold;
2042 if (f->conn.proto_minor >= 23)
2043 outarg.time_gran = f->conn.time_gran;
2045 if (f->debug) {
2046 fprintf(stderr, " INIT: %u.%u\n", outarg.major, outarg.minor);
2047 fprintf(stderr, " flags=0x%08x\n", outarg.flags);
2048 fprintf(stderr, " max_readahead=0x%08x\n",
2049 outarg.max_readahead);
2050 fprintf(stderr, " max_write=0x%08x\n", outarg.max_write);
2051 fprintf(stderr, " max_background=%i\n",
2052 outarg.max_background);
2053 fprintf(stderr, " congestion_threshold=%i\n",
2054 outarg.congestion_threshold);
2055 fprintf(stderr, " time_gran=%u\n",
2056 outarg.time_gran);
2058 if (arg->minor < 5)
2059 outargsize = FUSE_COMPAT_INIT_OUT_SIZE;
2060 else if (arg->minor < 23)
2061 outargsize = FUSE_COMPAT_22_INIT_OUT_SIZE;
2063 send_reply_ok(req, &outarg, outargsize);
2066 static void do_destroy(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
2068 struct fuse_ll *f = req->f;
2070 (void) nodeid;
2071 (void) inarg;
2073 f->got_destroy = 1;
2074 if (f->op.destroy)
2075 f->op.destroy(f->userdata);
2077 send_reply_ok(req, NULL, 0);
2080 static void list_del_nreq(struct fuse_notify_req *nreq)
2082 struct fuse_notify_req *prev = nreq->prev;
2083 struct fuse_notify_req *next = nreq->next;
2084 prev->next = next;
2085 next->prev = prev;
2088 static void list_add_nreq(struct fuse_notify_req *nreq,
2089 struct fuse_notify_req *next)
2091 struct fuse_notify_req *prev = next->prev;
2092 nreq->next = next;
2093 nreq->prev = prev;
2094 prev->next = nreq;
2095 next->prev = nreq;
2098 static void list_init_nreq(struct fuse_notify_req *nreq)
2100 nreq->next = nreq;
2101 nreq->prev = nreq;
2104 static void do_notify_reply(fuse_req_t req, fuse_ino_t nodeid,
2105 const void *inarg, const struct fuse_buf *buf)
2107 struct fuse_ll *f = req->f;
2108 struct fuse_notify_req *nreq;
2109 struct fuse_notify_req *head;
2111 pthread_mutex_lock(&f->lock);
2112 head = &f->notify_list;
2113 for (nreq = head->next; nreq != head; nreq = nreq->next) {
2114 if (nreq->unique == req->unique) {
2115 list_del_nreq(nreq);
2116 break;
2119 pthread_mutex_unlock(&f->lock);
2121 if (nreq != head)
2122 nreq->reply(nreq, req, nodeid, inarg, buf);
2125 static int send_notify_iov(struct fuse_ll *f, struct fuse_chan *ch,
2126 int notify_code, struct iovec *iov, int count)
2128 struct fuse_out_header out;
2130 if (!f->got_init)
2131 return -ENOTCONN;
2133 out.unique = 0;
2134 out.error = notify_code;
2135 iov[0].iov_base = &out;
2136 iov[0].iov_len = sizeof(struct fuse_out_header);
2138 return fuse_send_msg(f, ch, iov, count);
2141 int fuse_lowlevel_notify_poll(struct fuse_pollhandle *ph)
2143 if (ph != NULL) {
2144 struct fuse_notify_poll_wakeup_out outarg;
2145 struct iovec iov[2];
2147 outarg.kh = ph->kh;
2149 iov[1].iov_base = &outarg;
2150 iov[1].iov_len = sizeof(outarg);
2152 return send_notify_iov(ph->f, ph->ch, FUSE_NOTIFY_POLL, iov, 2);
2153 } else {
2154 return 0;
2158 int fuse_lowlevel_notify_inval_inode(struct fuse_chan *ch, fuse_ino_t ino,
2159 off_t off, off_t len)
2161 struct fuse_notify_inval_inode_out outarg;
2162 struct fuse_ll *f;
2163 struct iovec iov[2];
2165 if (!ch)
2166 return -EINVAL;
2168 f = fuse_chan_session(ch)->f;
2169 if (!f)
2170 return -ENODEV;
2172 outarg.ino = ino;
2173 outarg.off = off;
2174 outarg.len = len;
2176 iov[1].iov_base = &outarg;
2177 iov[1].iov_len = sizeof(outarg);
2179 return send_notify_iov(f, ch, FUSE_NOTIFY_INVAL_INODE, iov, 2);
2182 int fuse_lowlevel_notify_inval_entry(struct fuse_chan *ch, fuse_ino_t parent,
2183 const char *name, size_t namelen)
2185 struct fuse_notify_inval_entry_out outarg;
2186 struct fuse_ll *f;
2187 struct iovec iov[3];
2189 if (!ch)
2190 return -EINVAL;
2192 f = fuse_chan_session(ch)->f;
2193 if (!f)
2194 return -ENODEV;
2196 outarg.parent = parent;
2197 outarg.namelen = namelen;
2198 outarg.padding = 0;
2200 iov[1].iov_base = &outarg;
2201 iov[1].iov_len = sizeof(outarg);
2202 iov[2].iov_base = (void *)name;
2203 iov[2].iov_len = namelen + 1;
2205 return send_notify_iov(f, ch, FUSE_NOTIFY_INVAL_ENTRY, iov, 3);
2208 int fuse_lowlevel_notify_delete(struct fuse_chan *ch,
2209 fuse_ino_t parent, fuse_ino_t child,
2210 const char *name, size_t namelen)
2212 struct fuse_notify_delete_out outarg;
2213 struct fuse_ll *f;
2214 struct iovec iov[3];
2216 if (!ch)
2217 return -EINVAL;
2219 f = fuse_chan_session(ch)->f;
2220 if (!f)
2221 return -ENODEV;
2223 if (f->conn.proto_minor < 18)
2224 return -ENOSYS;
2226 outarg.parent = parent;
2227 outarg.child = child;
2228 outarg.namelen = namelen;
2229 outarg.padding = 0;
2231 iov[1].iov_base = &outarg;
2232 iov[1].iov_len = sizeof(outarg);
2233 iov[2].iov_base = (void *)name;
2234 iov[2].iov_len = namelen + 1;
2236 return send_notify_iov(f, ch, FUSE_NOTIFY_DELETE, iov, 3);
2239 int fuse_lowlevel_notify_store(struct fuse_chan *ch, fuse_ino_t ino,
2240 off_t offset, struct fuse_bufvec *bufv,
2241 enum fuse_buf_copy_flags flags)
2243 struct fuse_out_header out;
2244 struct fuse_notify_store_out outarg;
2245 struct fuse_ll *f;
2246 struct iovec iov[3];
2247 size_t size = fuse_buf_size(bufv);
2248 int res;
2250 if (!ch)
2251 return -EINVAL;
2253 f = fuse_chan_session(ch)->f;
2254 if (!f)
2255 return -ENODEV;
2257 if (f->conn.proto_minor < 15)
2258 return -ENOSYS;
2260 out.unique = 0;
2261 out.error = FUSE_NOTIFY_STORE;
2263 outarg.nodeid = ino;
2264 outarg.offset = offset;
2265 outarg.size = size;
2267 iov[0].iov_base = &out;
2268 iov[0].iov_len = sizeof(out);
2269 iov[1].iov_base = &outarg;
2270 iov[1].iov_len = sizeof(outarg);
2272 res = fuse_send_data_iov(f, ch, iov, 2, bufv, flags);
2273 if (res > 0)
2274 res = -res;
2276 return res;
2279 struct fuse_retrieve_req {
2280 struct fuse_notify_req nreq;
2281 void *cookie;
2284 static void fuse_ll_retrieve_reply(struct fuse_notify_req *nreq,
2285 fuse_req_t req, fuse_ino_t ino,
2286 const void *inarg,
2287 const struct fuse_buf *ibuf)
2289 struct fuse_ll *f = req->f;
2290 struct fuse_retrieve_req *rreq =
2291 container_of(nreq, struct fuse_retrieve_req, nreq);
2292 const struct fuse_notify_retrieve_in *arg = inarg;
2293 struct fuse_bufvec bufv = {
2294 .buf[0] = *ibuf,
2295 .count = 1,
2298 if (!(bufv.buf[0].flags & FUSE_BUF_IS_FD))
2299 bufv.buf[0].mem = PARAM(arg);
2301 bufv.buf[0].size -= sizeof(struct fuse_in_header) +
2302 sizeof(struct fuse_notify_retrieve_in);
2304 if (bufv.buf[0].size < arg->size) {
2305 fprintf(stderr, "fuse: retrieve reply: buffer size too small\n");
2306 fuse_reply_none(req);
2307 goto out;
2309 bufv.buf[0].size = arg->size;
2311 if (req->f->op.retrieve_reply) {
2312 req->f->op.retrieve_reply(req, rreq->cookie, ino,
2313 arg->offset, &bufv);
2314 } else {
2315 fuse_reply_none(req);
2317 out:
2318 free(rreq);
2319 if ((ibuf->flags & FUSE_BUF_IS_FD) && bufv.idx < bufv.count)
2320 fuse_ll_clear_pipe(f);
2323 int fuse_lowlevel_notify_retrieve(struct fuse_chan *ch, fuse_ino_t ino,
2324 size_t size, off_t offset, void *cookie)
2326 struct fuse_notify_retrieve_out outarg;
2327 struct fuse_ll *f;
2328 struct iovec iov[2];
2329 struct fuse_retrieve_req *rreq;
2330 int err;
2332 if (!ch)
2333 return -EINVAL;
2335 f = fuse_chan_session(ch)->f;
2336 if (!f)
2337 return -ENODEV;
2339 if (f->conn.proto_minor < 15)
2340 return -ENOSYS;
2342 rreq = malloc(sizeof(*rreq));
2343 if (rreq == NULL)
2344 return -ENOMEM;
2346 pthread_mutex_lock(&f->lock);
2347 rreq->cookie = cookie;
2348 rreq->nreq.unique = f->notify_ctr++;
2349 rreq->nreq.reply = fuse_ll_retrieve_reply;
2350 list_add_nreq(&rreq->nreq, &f->notify_list);
2351 pthread_mutex_unlock(&f->lock);
2353 outarg.notify_unique = rreq->nreq.unique;
2354 outarg.nodeid = ino;
2355 outarg.offset = offset;
2356 outarg.size = size;
2358 iov[1].iov_base = &outarg;
2359 iov[1].iov_len = sizeof(outarg);
2361 err = send_notify_iov(f, ch, FUSE_NOTIFY_RETRIEVE, iov, 2);
2362 if (err) {
2363 pthread_mutex_lock(&f->lock);
2364 list_del_nreq(&rreq->nreq);
2365 pthread_mutex_unlock(&f->lock);
2366 free(rreq);
2369 return err;
2372 void *fuse_req_userdata(fuse_req_t req)
2374 return req->f->userdata;
2377 const struct fuse_ctx *fuse_req_ctx(fuse_req_t req)
2379 return &req->ctx;
2382 void fuse_req_interrupt_func(fuse_req_t req, fuse_interrupt_func_t func,
2383 void *data)
2385 pthread_mutex_lock(&req->lock);
2386 pthread_mutex_lock(&req->f->lock);
2387 req->u.ni.func = func;
2388 req->u.ni.data = data;
2389 pthread_mutex_unlock(&req->f->lock);
2390 if (req->interrupted && func)
2391 func(req, data);
2392 pthread_mutex_unlock(&req->lock);
2395 int fuse_req_interrupted(fuse_req_t req)
2397 int interrupted;
2399 pthread_mutex_lock(&req->f->lock);
2400 interrupted = req->interrupted;
2401 pthread_mutex_unlock(&req->f->lock);
2403 return interrupted;
2406 static struct {
2407 void (*func)(fuse_req_t, fuse_ino_t, const void *);
2408 const char *name;
2409 } fuse_ll_ops[] = {
2410 [FUSE_LOOKUP] = { do_lookup, "LOOKUP" },
2411 [FUSE_FORGET] = { do_forget, "FORGET" },
2412 [FUSE_GETATTR] = { do_getattr, "GETATTR" },
2413 [FUSE_SETATTR] = { do_setattr, "SETATTR" },
2414 [FUSE_READLINK] = { do_readlink, "READLINK" },
2415 [FUSE_SYMLINK] = { do_symlink, "SYMLINK" },
2416 [FUSE_MKNOD] = { do_mknod, "MKNOD" },
2417 [FUSE_MKDIR] = { do_mkdir, "MKDIR" },
2418 [FUSE_UNLINK] = { do_unlink, "UNLINK" },
2419 [FUSE_RMDIR] = { do_rmdir, "RMDIR" },
2420 [FUSE_RENAME] = { do_rename, "RENAME" },
2421 [FUSE_LINK] = { do_link, "LINK" },
2422 [FUSE_OPEN] = { do_open, "OPEN" },
2423 [FUSE_READ] = { do_read, "READ" },
2424 [FUSE_WRITE] = { do_write, "WRITE" },
2425 [FUSE_STATFS] = { do_statfs, "STATFS" },
2426 [FUSE_RELEASE] = { do_release, "RELEASE" },
2427 [FUSE_FSYNC] = { do_fsync, "FSYNC" },
2428 [FUSE_SETXATTR] = { do_setxattr, "SETXATTR" },
2429 [FUSE_GETXATTR] = { do_getxattr, "GETXATTR" },
2430 [FUSE_LISTXATTR] = { do_listxattr, "LISTXATTR" },
2431 [FUSE_REMOVEXATTR] = { do_removexattr, "REMOVEXATTR" },
2432 [FUSE_FLUSH] = { do_flush, "FLUSH" },
2433 [FUSE_INIT] = { do_init, "INIT" },
2434 [FUSE_OPENDIR] = { do_opendir, "OPENDIR" },
2435 [FUSE_READDIR] = { do_readdir, "READDIR" },
2436 [FUSE_RELEASEDIR] = { do_releasedir, "RELEASEDIR" },
2437 [FUSE_FSYNCDIR] = { do_fsyncdir, "FSYNCDIR" },
2438 [FUSE_GETLK] = { do_getlk, "GETLK" },
2439 [FUSE_SETLK] = { do_setlk, "SETLK" },
2440 [FUSE_SETLKW] = { do_setlkw, "SETLKW" },
2441 [FUSE_ACCESS] = { do_access, "ACCESS" },
2442 [FUSE_CREATE] = { do_create, "CREATE" },
2443 [FUSE_INTERRUPT] = { do_interrupt, "INTERRUPT" },
2444 [FUSE_BMAP] = { do_bmap, "BMAP" },
2445 [FUSE_IOCTL] = { do_ioctl, "IOCTL" },
2446 [FUSE_POLL] = { do_poll, "POLL" },
2447 [FUSE_FALLOCATE] = { do_fallocate, "FALLOCATE" },
2448 [FUSE_DESTROY] = { do_destroy, "DESTROY" },
2449 [FUSE_NOTIFY_REPLY] = { (void *) 1, "NOTIFY_REPLY" },
2450 [FUSE_BATCH_FORGET] = { do_batch_forget, "BATCH_FORGET" },
2451 [FUSE_READDIRPLUS] = { do_readdirplus, "READDIRPLUS"},
2452 [FUSE_RENAME2] = { do_rename2, "RENAME2" },
2453 [CUSE_INIT] = { cuse_lowlevel_init, "CUSE_INIT" },
2456 #define FUSE_MAXOP (sizeof(fuse_ll_ops) / sizeof(fuse_ll_ops[0]))
2458 static const char *opname(enum fuse_opcode opcode)
2460 if (opcode >= FUSE_MAXOP || !fuse_ll_ops[opcode].name)
2461 return "???";
2462 else
2463 return fuse_ll_ops[opcode].name;
2466 static int fuse_ll_copy_from_pipe(struct fuse_bufvec *dst,
2467 struct fuse_bufvec *src)
2469 int res = fuse_buf_copy(dst, src, 0);
2470 if (res < 0) {
2471 fprintf(stderr, "fuse: copy from pipe: %s\n", strerror(-res));
2472 return res;
2474 if (res < fuse_buf_size(dst)) {
2475 fprintf(stderr, "fuse: copy from pipe: short read\n");
2476 return -1;
2478 return 0;
2481 void fuse_session_process_buf(struct fuse_session *se,
2482 const struct fuse_buf *buf, struct fuse_chan *ch)
2484 struct fuse_ll *f = se->f;
2485 const size_t write_header_size = sizeof(struct fuse_in_header) +
2486 sizeof(struct fuse_write_in);
2487 struct fuse_bufvec bufv = { .buf[0] = *buf, .count = 1 };
2488 struct fuse_bufvec tmpbuf = FUSE_BUFVEC_INIT(write_header_size);
2489 struct fuse_in_header *in;
2490 const void *inarg;
2491 struct fuse_req *req;
2492 void *mbuf = NULL;
2493 int err;
2494 int res;
2496 if (buf->flags & FUSE_BUF_IS_FD) {
2497 if (buf->size < tmpbuf.buf[0].size)
2498 tmpbuf.buf[0].size = buf->size;
2500 mbuf = malloc(tmpbuf.buf[0].size);
2501 if (mbuf == NULL) {
2502 fprintf(stderr, "fuse: failed to allocate header\n");
2503 goto clear_pipe;
2505 tmpbuf.buf[0].mem = mbuf;
2507 res = fuse_ll_copy_from_pipe(&tmpbuf, &bufv);
2508 if (res < 0)
2509 goto clear_pipe;
2511 in = mbuf;
2512 } else {
2513 in = buf->mem;
2516 if (f->debug) {
2517 fprintf(stderr,
2518 "unique: %llu, opcode: %s (%i), nodeid: %llu, insize: %zu, pid: %u\n",
2519 (unsigned long long) in->unique,
2520 opname((enum fuse_opcode) in->opcode), in->opcode,
2521 (unsigned long long) in->nodeid, buf->size, in->pid);
2524 req = fuse_ll_alloc_req(f);
2525 if (req == NULL) {
2526 struct fuse_out_header out = {
2527 .unique = in->unique,
2528 .error = -ENOMEM,
2530 struct iovec iov = {
2531 .iov_base = &out,
2532 .iov_len = sizeof(struct fuse_out_header),
2535 fuse_send_msg(f, ch, &iov, 1);
2536 goto clear_pipe;
2539 req->unique = in->unique;
2540 req->ctx.uid = in->uid;
2541 req->ctx.gid = in->gid;
2542 req->ctx.pid = in->pid;
2543 req->ch = fuse_chan_get(ch);
2545 err = EIO;
2546 if (!f->got_init) {
2547 enum fuse_opcode expected;
2549 expected = f->cuse_data ? CUSE_INIT : FUSE_INIT;
2550 if (in->opcode != expected)
2551 goto reply_err;
2552 } else if (in->opcode == FUSE_INIT || in->opcode == CUSE_INIT)
2553 goto reply_err;
2555 err = EACCES;
2556 if (f->allow_root && in->uid != f->owner && in->uid != 0 &&
2557 in->opcode != FUSE_INIT && in->opcode != FUSE_READ &&
2558 in->opcode != FUSE_WRITE && in->opcode != FUSE_FSYNC &&
2559 in->opcode != FUSE_RELEASE && in->opcode != FUSE_READDIR &&
2560 in->opcode != FUSE_FSYNCDIR && in->opcode != FUSE_RELEASEDIR &&
2561 in->opcode != FUSE_NOTIFY_REPLY &&
2562 in->opcode != FUSE_READDIRPLUS)
2563 goto reply_err;
2565 err = ENOSYS;
2566 if (in->opcode >= FUSE_MAXOP || !fuse_ll_ops[in->opcode].func)
2567 goto reply_err;
2568 if (in->opcode != FUSE_INTERRUPT) {
2569 struct fuse_req *intr;
2570 pthread_mutex_lock(&f->lock);
2571 intr = check_interrupt(f, req);
2572 list_add_req(req, &f->list);
2573 pthread_mutex_unlock(&f->lock);
2574 if (intr)
2575 fuse_reply_err(intr, EAGAIN);
2578 if ((buf->flags & FUSE_BUF_IS_FD) && write_header_size < buf->size &&
2579 (in->opcode != FUSE_WRITE || !f->op.write_buf) &&
2580 in->opcode != FUSE_NOTIFY_REPLY) {
2581 void *newmbuf;
2583 err = ENOMEM;
2584 newmbuf = realloc(mbuf, buf->size);
2585 if (newmbuf == NULL)
2586 goto reply_err;
2587 mbuf = newmbuf;
2589 tmpbuf = FUSE_BUFVEC_INIT(buf->size - write_header_size);
2590 tmpbuf.buf[0].mem = mbuf + write_header_size;
2592 res = fuse_ll_copy_from_pipe(&tmpbuf, &bufv);
2593 err = -res;
2594 if (res < 0)
2595 goto reply_err;
2597 in = mbuf;
2600 inarg = (void *) &in[1];
2601 if (in->opcode == FUSE_WRITE && f->op.write_buf)
2602 do_write_buf(req, in->nodeid, inarg, buf);
2603 else if (in->opcode == FUSE_NOTIFY_REPLY)
2604 do_notify_reply(req, in->nodeid, inarg, buf);
2605 else
2606 fuse_ll_ops[in->opcode].func(req, in->nodeid, inarg);
2608 out_free:
2609 free(mbuf);
2610 return;
2612 reply_err:
2613 fuse_reply_err(req, err);
2614 clear_pipe:
2615 if (buf->flags & FUSE_BUF_IS_FD)
2616 fuse_ll_clear_pipe(f);
2617 goto out_free;
2620 enum {
2621 KEY_HELP,
2622 KEY_VERSION,
2625 static const struct fuse_opt fuse_ll_opts[] = {
2626 { "debug", offsetof(struct fuse_ll, debug), 1 },
2627 { "-d", offsetof(struct fuse_ll, debug), 1 },
2628 { "allow_root", offsetof(struct fuse_ll, allow_root), 1 },
2629 { "max_write=%u", offsetof(struct fuse_ll, conn.max_write), 0 },
2630 { "max_readahead=%u", offsetof(struct fuse_ll, conn.max_readahead), 0 },
2631 { "max_background=%u", offsetof(struct fuse_ll, conn.max_background), 0 },
2632 { "congestion_threshold=%u",
2633 offsetof(struct fuse_ll, conn.congestion_threshold), 0 },
2634 { "async_read", offsetof(struct fuse_ll, conn.async_read), 1 },
2635 { "sync_read", offsetof(struct fuse_ll, conn.async_read), 0 },
2636 { "atomic_o_trunc", offsetof(struct fuse_ll, atomic_o_trunc), 1},
2637 { "no_remote_lock", offsetof(struct fuse_ll, no_remote_posix_lock), 1},
2638 { "no_remote_lock", offsetof(struct fuse_ll, no_remote_flock), 1},
2639 { "no_remote_flock", offsetof(struct fuse_ll, no_remote_flock), 1},
2640 { "no_remote_posix_lock", offsetof(struct fuse_ll, no_remote_posix_lock), 1},
2641 { "big_writes", offsetof(struct fuse_ll, big_writes), 1},
2642 { "splice_write", offsetof(struct fuse_ll, splice_write), 1},
2643 { "no_splice_write", offsetof(struct fuse_ll, no_splice_write), 1},
2644 { "splice_move", offsetof(struct fuse_ll, splice_move), 1},
2645 { "no_splice_move", offsetof(struct fuse_ll, no_splice_move), 1},
2646 { "splice_read", offsetof(struct fuse_ll, splice_read), 1},
2647 { "no_splice_read", offsetof(struct fuse_ll, no_splice_read), 1},
2648 { "auto_inval_data", offsetof(struct fuse_ll, auto_inval_data), 1},
2649 { "no_auto_inval_data", offsetof(struct fuse_ll, no_auto_inval_data), 1},
2650 { "readdirplus=no", offsetof(struct fuse_ll, no_readdirplus), 1},
2651 { "readdirplus=yes", offsetof(struct fuse_ll, no_readdirplus), 0},
2652 { "readdirplus=yes", offsetof(struct fuse_ll, no_readdirplus_auto), 1},
2653 { "readdirplus=auto", offsetof(struct fuse_ll, no_readdirplus), 0},
2654 { "readdirplus=auto", offsetof(struct fuse_ll, no_readdirplus_auto), 0},
2655 { "async_dio", offsetof(struct fuse_ll, async_dio), 1},
2656 { "no_async_dio", offsetof(struct fuse_ll, no_async_dio), 1},
2657 { "writeback_cache", offsetof(struct fuse_ll, writeback_cache), 1},
2658 { "no_writeback_cache", offsetof(struct fuse_ll, no_writeback_cache), 1},
2659 { "time_gran=%u", offsetof(struct fuse_ll, conn.time_gran), 0 },
2660 { "clone_fd", offsetof(struct fuse_ll, clone_fd), 1 },
2661 FUSE_OPT_KEY("max_read=", FUSE_OPT_KEY_DISCARD),
2662 FUSE_OPT_KEY("-h", KEY_HELP),
2663 FUSE_OPT_KEY("--help", KEY_HELP),
2664 FUSE_OPT_KEY("-V", KEY_VERSION),
2665 FUSE_OPT_KEY("--version", KEY_VERSION),
2666 FUSE_OPT_END
2669 static void fuse_ll_version(void)
2671 printf("using FUSE kernel interface version %i.%i\n",
2672 FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
2675 static void fuse_ll_help(void)
2677 printf(
2678 " -o max_write=N set maximum size of write requests\n"
2679 " -o max_readahead=N set maximum readahead\n"
2680 " -o max_background=N set number of maximum background requests\n"
2681 " -o congestion_threshold=N set kernel's congestion threshold\n"
2682 " -o async_read perform reads asynchronously (default)\n"
2683 " -o sync_read perform reads synchronously\n"
2684 " -o atomic_o_trunc enable atomic open+truncate support\n"
2685 " -o big_writes enable larger than 4kB writes\n"
2686 " -o no_remote_lock disable remote file locking\n"
2687 " -o no_remote_flock disable remote file locking (BSD)\n"
2688 " -o no_remote_posix_lock disable remove file locking (POSIX)\n"
2689 " -o [no_]splice_write use splice to write to the fuse device\n"
2690 " -o [no_]splice_move move data while splicing to the fuse device\n"
2691 " -o [no_]splice_read use splice to read from the fuse device\n"
2692 " -o [no_]auto_inval_data use automatic kernel cache invalidation logic\n"
2693 " -o readdirplus=S control readdirplus use (yes|no|auto)\n"
2694 " -o [no_]async_dio asynchronous direct I/O\n"
2695 " -o [no_]writeback_cache asynchronous, buffered writes\n"
2696 " -o time_gran=N time granularity in nsec\n"
2697 " -o clone_fd clone fuse device file descriptors\n"
2701 static int fuse_ll_opt_proc(void *data, const char *arg, int key,
2702 struct fuse_args *outargs)
2704 (void) data; (void) outargs;
2706 switch (key) {
2707 case KEY_HELP:
2708 fuse_ll_help();
2709 break;
2711 case KEY_VERSION:
2712 fuse_ll_version();
2713 break;
2715 default:
2716 fprintf(stderr, "fuse: unknown option `%s'\n", arg);
2719 return -1;
2722 static void fuse_ll_destroy(struct fuse_ll *f)
2724 struct fuse_ll_pipe *llp;
2726 if (f->got_init && !f->got_destroy) {
2727 if (f->op.destroy)
2728 f->op.destroy(f->userdata);
2730 llp = pthread_getspecific(f->pipe_key);
2731 if (llp != NULL)
2732 fuse_ll_pipe_free(llp);
2733 pthread_key_delete(f->pipe_key);
2734 pthread_mutex_destroy(&f->lock);
2735 free(f->cuse_data);
2736 free(f);
2739 void fuse_session_destroy(struct fuse_session *se)
2741 fuse_ll_destroy(se->f);
2742 fuse_chan_put(se->ch);
2743 free(se);
2747 static void fuse_ll_pipe_destructor(void *data)
2749 struct fuse_ll_pipe *llp = data;
2750 fuse_ll_pipe_free(llp);
2753 #ifdef HAVE_SPLICE
2754 int fuse_session_receive_buf(struct fuse_session *se, struct fuse_buf *buf,
2755 struct fuse_chan *ch)
2757 struct fuse_ll *f = se->f;
2758 size_t bufsize = f->bufsize;
2759 struct fuse_ll_pipe *llp;
2760 struct fuse_buf tmpbuf;
2761 int err;
2762 int res;
2764 if (f->conn.proto_minor < 14 || !(f->conn.want & FUSE_CAP_SPLICE_READ))
2765 goto fallback;
2767 llp = fuse_ll_get_pipe(f);
2768 if (llp == NULL)
2769 goto fallback;
2771 if (llp->size < bufsize) {
2772 if (llp->can_grow) {
2773 res = fcntl(llp->pipe[0], F_SETPIPE_SZ, bufsize);
2774 if (res == -1) {
2775 llp->can_grow = 0;
2776 goto fallback;
2778 llp->size = res;
2780 if (llp->size < bufsize)
2781 goto fallback;
2784 res = splice(fuse_chan_fd(ch), NULL, llp->pipe[1], NULL, bufsize, 0);
2785 err = errno;
2787 if (fuse_session_exited(se))
2788 return 0;
2790 if (res == -1) {
2791 if (err == ENODEV) {
2792 fuse_session_exit(se);
2793 return 0;
2795 if (err != EINTR && err != EAGAIN)
2796 perror("fuse: splice from device");
2797 return -err;
2800 if (res < sizeof(struct fuse_in_header)) {
2801 fprintf(stderr, "short splice from fuse device\n");
2802 return -EIO;
2805 tmpbuf = (struct fuse_buf) {
2806 .size = res,
2807 .flags = FUSE_BUF_IS_FD,
2808 .fd = llp->pipe[0],
2812 * Don't bother with zero copy for small requests.
2813 * fuse_loop_mt() needs to check for FORGET so this more than
2814 * just an optimization.
2816 if (res < sizeof(struct fuse_in_header) +
2817 sizeof(struct fuse_write_in) + pagesize) {
2818 struct fuse_bufvec src = { .buf[0] = tmpbuf, .count = 1 };
2819 struct fuse_bufvec dst = { .count = 1 };
2821 if (!buf->mem) {
2822 buf->mem = malloc(f->bufsize);
2823 if (!buf->mem) {
2824 fprintf(stderr,
2825 "fuse: failed to allocate read buffer\n");
2826 return -ENOMEM;
2829 buf->size = f->bufsize;
2830 buf->flags = 0;
2831 dst.buf[0] = *buf;
2833 res = fuse_buf_copy(&dst, &src, 0);
2834 if (res < 0) {
2835 fprintf(stderr, "fuse: copy from pipe: %s\n",
2836 strerror(-res));
2837 fuse_ll_clear_pipe(f);
2838 return res;
2840 if (res < tmpbuf.size) {
2841 fprintf(stderr, "fuse: copy from pipe: short read\n");
2842 fuse_ll_clear_pipe(f);
2843 return -EIO;
2845 assert(res == tmpbuf.size);
2847 } else {
2848 /* Don't overwrite buf->mem, as that would cause a leak */
2849 buf->fd = tmpbuf.fd;
2850 buf->flags = tmpbuf.flags;
2852 buf->size = tmpbuf.size;
2854 return res;
2856 fallback:
2857 return fuse_chan_recv(se, buf, ch);
2859 #else
2860 int fuse_session_receive_buf(struct fuse_session *se, struct fuse_buf *buf,
2861 struct fuse_chan *ch)
2863 return fuse_chan_recv(se, buf, ch);
2865 #endif
2867 #define MIN_BUFSIZE 0x21000
2869 struct fuse_session *fuse_lowlevel_new(struct fuse_args *args,
2870 const struct fuse_lowlevel_ops *op,
2871 size_t op_size, void *userdata)
2873 int err;
2874 struct fuse_ll *f;
2875 struct fuse_session *se;
2877 if (sizeof(struct fuse_lowlevel_ops) < op_size) {
2878 fprintf(stderr, "fuse: warning: library too old, some operations may not work\n");
2879 op_size = sizeof(struct fuse_lowlevel_ops);
2882 f = (struct fuse_ll *) calloc(1, sizeof(struct fuse_ll));
2883 if (f == NULL) {
2884 fprintf(stderr, "fuse: failed to allocate fuse object\n");
2885 goto out;
2888 f->conn.async_read = 1;
2889 f->conn.max_write = UINT_MAX;
2890 f->conn.max_readahead = UINT_MAX;
2891 f->atomic_o_trunc = 0;
2892 f->bufsize = getpagesize() + 0x1000;
2893 f->bufsize = f->bufsize < MIN_BUFSIZE ? MIN_BUFSIZE : f->bufsize;
2895 list_init_req(&f->list);
2896 list_init_req(&f->interrupts);
2897 list_init_nreq(&f->notify_list);
2898 f->notify_ctr = 1;
2899 fuse_mutex_init(&f->lock);
2901 err = pthread_key_create(&f->pipe_key, fuse_ll_pipe_destructor);
2902 if (err) {
2903 fprintf(stderr, "fuse: failed to create thread specific key: %s\n",
2904 strerror(err));
2905 goto out_free;
2908 if (fuse_opt_parse(args, f, fuse_ll_opts, fuse_ll_opt_proc) == -1)
2909 goto out_key_destroy;
2911 if (f->debug)
2912 fprintf(stderr, "FUSE library version: %s\n", PACKAGE_VERSION);
2914 memcpy(&f->op, op, op_size);
2915 f->owner = getuid();
2916 f->userdata = userdata;
2918 se = fuse_session_new();
2919 if (!se)
2920 goto out_key_destroy;
2922 se->f = f;
2924 return se;
2926 out_key_destroy:
2927 pthread_key_delete(f->pipe_key);
2928 out_free:
2929 pthread_mutex_destroy(&f->lock);
2930 free(f);
2931 out:
2932 return NULL;
2935 #ifdef linux
2936 int fuse_req_getgroups(fuse_req_t req, int size, gid_t list[])
2938 char *buf;
2939 size_t bufsize = 1024;
2940 char path[128];
2941 int ret;
2942 int fd;
2943 unsigned long pid = req->ctx.pid;
2944 char *s;
2946 sprintf(path, "/proc/%lu/task/%lu/status", pid, pid);
2948 retry:
2949 buf = malloc(bufsize);
2950 if (buf == NULL)
2951 return -ENOMEM;
2953 ret = -EIO;
2954 fd = open(path, O_RDONLY);
2955 if (fd == -1)
2956 goto out_free;
2958 ret = read(fd, buf, bufsize);
2959 close(fd);
2960 if (ret == -1) {
2961 ret = -EIO;
2962 goto out_free;
2965 if (ret == bufsize) {
2966 free(buf);
2967 bufsize *= 4;
2968 goto retry;
2971 ret = -EIO;
2972 s = strstr(buf, "\nGroups:");
2973 if (s == NULL)
2974 goto out_free;
2976 s += 8;
2977 ret = 0;
2978 while (1) {
2979 char *end;
2980 unsigned long val = strtoul(s, &end, 0);
2981 if (end == s)
2982 break;
2984 s = end;
2985 if (ret < size)
2986 list[ret] = val;
2987 ret++;
2990 out_free:
2991 free(buf);
2992 return ret;
2994 #else /* linux */
2996 * This is currently not implemented on other than Linux...
2998 int fuse_req_getgroups(fuse_req_t req, int size, gid_t list[])
3000 return -ENOSYS;
3002 #endif