libfuse: add "time_gran" option
[fuse.git] / lib / fuse_lowlevel.c
blobdc27cb534890745f691040e01210a4dec7889a02
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 ST_ATIM_NSEC_SET(stbuf, attr->atimensec);
83 ST_MTIM_NSEC_SET(stbuf, attr->mtimensec);
86 static size_t iov_length(const struct iovec *iov, size_t count)
88 size_t seg;
89 size_t ret = 0;
91 for (seg = 0; seg < count; seg++)
92 ret += iov[seg].iov_len;
93 return ret;
96 static void list_init_req(struct fuse_req *req)
98 req->next = req;
99 req->prev = req;
102 static void list_del_req(struct fuse_req *req)
104 struct fuse_req *prev = req->prev;
105 struct fuse_req *next = req->next;
106 prev->next = next;
107 next->prev = prev;
110 static void list_add_req(struct fuse_req *req, struct fuse_req *next)
112 struct fuse_req *prev = next->prev;
113 req->next = next;
114 req->prev = prev;
115 prev->next = req;
116 next->prev = req;
119 static void destroy_req(fuse_req_t req)
121 pthread_mutex_destroy(&req->lock);
122 free(req);
125 void fuse_free_req(fuse_req_t req)
127 int ctr;
128 struct fuse_ll *f = req->f;
130 pthread_mutex_lock(&f->lock);
131 req->u.ni.func = NULL;
132 req->u.ni.data = NULL;
133 list_del_req(req);
134 ctr = --req->ctr;
135 pthread_mutex_unlock(&f->lock);
136 if (!ctr)
137 destroy_req(req);
140 static struct fuse_req *fuse_ll_alloc_req(struct fuse_ll *f)
142 struct fuse_req *req;
144 req = (struct fuse_req *) calloc(1, sizeof(struct fuse_req));
145 if (req == NULL) {
146 fprintf(stderr, "fuse: failed to allocate request\n");
147 } else {
148 req->f = f;
149 req->ctr = 1;
150 list_init_req(req);
151 fuse_mutex_init(&req->lock);
154 return req;
157 static int fuse_chan_recv(struct fuse_session *se, struct fuse_buf *buf,
158 struct fuse_chan *ch)
160 struct fuse_ll *f = se->f;
161 int err;
162 ssize_t res;
164 if (!buf->mem) {
165 buf->mem = malloc(f->bufsize);
166 if (!buf->mem) {
167 fprintf(stderr,
168 "fuse: failed to allocate read buffer\n");
169 return -ENOMEM;
173 restart:
174 res = read(fuse_chan_fd(ch), buf->mem, f->bufsize);
175 err = errno;
177 if (fuse_session_exited(se))
178 return 0;
179 if (res == -1) {
180 /* ENOENT means the operation was interrupted, it's safe
181 to restart */
182 if (err == ENOENT)
183 goto restart;
185 if (err == ENODEV) {
186 fuse_session_exit(se);
187 return 0;
189 /* Errors occurring during normal operation: EINTR (read
190 interrupted), EAGAIN (nonblocking I/O), ENODEV (filesystem
191 umounted) */
192 if (err != EINTR && err != EAGAIN)
193 perror("fuse: reading device");
194 return -err;
196 if ((size_t) res < sizeof(struct fuse_in_header)) {
197 fprintf(stderr, "short read on fuse device\n");
198 return -EIO;
201 buf->size = res;
203 return res;
206 static int fuse_chan_send(struct fuse_chan *ch, const struct iovec iov[],
207 size_t count)
209 ssize_t res = writev(fuse_chan_fd(ch), iov, count);
210 int err = errno;
212 if (res == -1) {
213 struct fuse_session *se = fuse_chan_session(ch);
215 assert(se != NULL);
217 /* ENOENT means the operation was interrupted */
218 if (!fuse_session_exited(se) && err != ENOENT)
219 perror("fuse: writing device");
220 return -err;
223 return 0;
226 void fuse_chan_close(struct fuse_chan *ch)
228 int fd = fuse_chan_fd(ch);
229 if (fd != -1)
230 close(fd);
234 static int fuse_send_msg(struct fuse_ll *f, struct fuse_chan *ch,
235 struct iovec *iov, int count)
237 struct fuse_out_header *out = iov[0].iov_base;
239 out->len = iov_length(iov, count);
240 if (f->debug) {
241 if (out->unique == 0) {
242 fprintf(stderr, "NOTIFY: code=%d length=%u\n",
243 out->error, out->len);
244 } else if (out->error) {
245 fprintf(stderr,
246 " unique: %llu, error: %i (%s), outsize: %i\n",
247 (unsigned long long) out->unique, out->error,
248 strerror(-out->error), out->len);
249 } else {
250 fprintf(stderr,
251 " unique: %llu, success, outsize: %i\n",
252 (unsigned long long) out->unique, out->len);
256 return fuse_chan_send(ch, iov, count);
259 int fuse_send_reply_iov_nofree(fuse_req_t req, int error, struct iovec *iov,
260 int count)
262 struct fuse_out_header out;
264 if (error <= -1000 || error > 0) {
265 fprintf(stderr, "fuse: bad error value: %i\n", error);
266 error = -ERANGE;
269 out.unique = req->unique;
270 out.error = error;
272 iov[0].iov_base = &out;
273 iov[0].iov_len = sizeof(struct fuse_out_header);
275 return fuse_send_msg(req->f, req->ch, iov, count);
278 static int send_reply_iov(fuse_req_t req, int error, struct iovec *iov,
279 int count)
281 int res;
283 res = fuse_send_reply_iov_nofree(req, error, iov, count);
284 fuse_free_req(req);
285 return res;
288 static int send_reply(fuse_req_t req, int error, const void *arg,
289 size_t argsize)
291 struct iovec iov[2];
292 int count = 1;
293 if (argsize) {
294 iov[1].iov_base = (void *) arg;
295 iov[1].iov_len = argsize;
296 count++;
298 return send_reply_iov(req, error, iov, count);
301 int fuse_reply_iov(fuse_req_t req, const struct iovec *iov, int count)
303 int res;
304 struct iovec *padded_iov;
306 padded_iov = malloc((count + 1) * sizeof(struct iovec));
307 if (padded_iov == NULL)
308 return fuse_reply_err(req, ENOMEM);
310 memcpy(padded_iov + 1, iov, count * sizeof(struct iovec));
311 count++;
313 res = send_reply_iov(req, 0, padded_iov, count);
314 free(padded_iov);
316 return res;
319 static size_t fuse_dirent_size(size_t namelen)
321 return FUSE_DIRENT_ALIGN(FUSE_NAME_OFFSET + namelen);
324 static void fuse_add_dirent(struct fuse_dirent *dirent, const char *name,
325 const struct stat *stbuf, off_t off)
327 unsigned namelen = strlen(name);
328 unsigned entlen = FUSE_NAME_OFFSET + namelen;
329 unsigned entsize = fuse_dirent_size(namelen);
330 unsigned padlen = entsize - entlen;
332 dirent->ino = stbuf->st_ino;
333 dirent->off = off;
334 dirent->namelen = namelen;
335 dirent->type = (stbuf->st_mode & 0170000) >> 12;
336 strncpy(dirent->name, name, namelen);
337 if (padlen)
338 memset(dirent->name + namelen, 0, padlen);
341 size_t fuse_add_direntry(fuse_req_t req, char *buf, size_t bufsize,
342 const char *name, const struct stat *stbuf, off_t off)
344 size_t entsize;
346 (void) req;
347 entsize = fuse_dirent_size(strlen(name));
348 if (entsize <= bufsize && buf)
349 fuse_add_dirent((struct fuse_dirent *) buf, name, stbuf, off);
350 return entsize;
353 static void convert_statfs(const struct statvfs *stbuf,
354 struct fuse_kstatfs *kstatfs)
356 kstatfs->bsize = stbuf->f_bsize;
357 kstatfs->frsize = stbuf->f_frsize;
358 kstatfs->blocks = stbuf->f_blocks;
359 kstatfs->bfree = stbuf->f_bfree;
360 kstatfs->bavail = stbuf->f_bavail;
361 kstatfs->files = stbuf->f_files;
362 kstatfs->ffree = stbuf->f_ffree;
363 kstatfs->namelen = stbuf->f_namemax;
366 static int send_reply_ok(fuse_req_t req, const void *arg, size_t argsize)
368 return send_reply(req, 0, arg, argsize);
371 int fuse_reply_err(fuse_req_t req, int err)
373 return send_reply(req, -err, NULL, 0);
376 void fuse_reply_none(fuse_req_t req)
378 fuse_free_req(req);
381 static unsigned long calc_timeout_sec(double t)
383 if (t > (double) ULONG_MAX)
384 return ULONG_MAX;
385 else if (t < 0.0)
386 return 0;
387 else
388 return (unsigned long) t;
391 static unsigned int calc_timeout_nsec(double t)
393 double f = t - (double) calc_timeout_sec(t);
394 if (f < 0.0)
395 return 0;
396 else if (f >= 0.999999999)
397 return 999999999;
398 else
399 return (unsigned int) (f * 1.0e9);
402 static void fill_entry(struct fuse_entry_out *arg,
403 const struct fuse_entry_param *e)
405 arg->nodeid = e->ino;
406 arg->generation = e->generation;
407 arg->entry_valid = calc_timeout_sec(e->entry_timeout);
408 arg->entry_valid_nsec = calc_timeout_nsec(e->entry_timeout);
409 arg->attr_valid = calc_timeout_sec(e->attr_timeout);
410 arg->attr_valid_nsec = calc_timeout_nsec(e->attr_timeout);
411 convert_stat(&e->attr, &arg->attr);
414 size_t fuse_add_direntry_plus(fuse_req_t req, char *buf, size_t bufsize,
415 const char *name,
416 const struct fuse_entry_param *e, off_t off)
418 size_t entsize;
420 (void) req;
421 entsize = FUSE_DIRENT_ALIGN(FUSE_NAME_OFFSET_DIRENTPLUS + strlen(name));
422 if (entsize <= bufsize && buf) {
423 struct fuse_direntplus *dp = (struct fuse_direntplus *) buf;
424 memset(&dp->entry_out, 0, sizeof(dp->entry_out));
425 fill_entry(&dp->entry_out, e);
426 fuse_add_dirent(&dp->dirent, name, &e->attr, off);
428 return entsize;
431 static void fill_open(struct fuse_open_out *arg,
432 const struct fuse_file_info *f)
434 arg->fh = f->fh;
435 if (f->direct_io)
436 arg->open_flags |= FOPEN_DIRECT_IO;
437 if (f->keep_cache)
438 arg->open_flags |= FOPEN_KEEP_CACHE;
439 if (f->nonseekable)
440 arg->open_flags |= FOPEN_NONSEEKABLE;
443 int fuse_reply_entry(fuse_req_t req, const struct fuse_entry_param *e)
445 struct fuse_entry_out arg;
446 size_t size = req->f->conn.proto_minor < 9 ?
447 FUSE_COMPAT_ENTRY_OUT_SIZE : sizeof(arg);
449 /* before ABI 7.4 e->ino == 0 was invalid, only ENOENT meant
450 negative entry */
451 if (!e->ino && req->f->conn.proto_minor < 4)
452 return fuse_reply_err(req, ENOENT);
454 memset(&arg, 0, sizeof(arg));
455 fill_entry(&arg, e);
456 return send_reply_ok(req, &arg, size);
459 int fuse_reply_create(fuse_req_t req, const struct fuse_entry_param *e,
460 const struct fuse_file_info *f)
462 char buf[sizeof(struct fuse_entry_out) + sizeof(struct fuse_open_out)];
463 size_t entrysize = req->f->conn.proto_minor < 9 ?
464 FUSE_COMPAT_ENTRY_OUT_SIZE : sizeof(struct fuse_entry_out);
465 struct fuse_entry_out *earg = (struct fuse_entry_out *) buf;
466 struct fuse_open_out *oarg = (struct fuse_open_out *) (buf + entrysize);
468 memset(buf, 0, sizeof(buf));
469 fill_entry(earg, e);
470 fill_open(oarg, f);
471 return send_reply_ok(req, buf,
472 entrysize + sizeof(struct fuse_open_out));
475 int fuse_reply_attr(fuse_req_t req, const struct stat *attr,
476 double attr_timeout)
478 struct fuse_attr_out arg;
479 size_t size = req->f->conn.proto_minor < 9 ?
480 FUSE_COMPAT_ATTR_OUT_SIZE : sizeof(arg);
482 memset(&arg, 0, sizeof(arg));
483 arg.attr_valid = calc_timeout_sec(attr_timeout);
484 arg.attr_valid_nsec = calc_timeout_nsec(attr_timeout);
485 convert_stat(attr, &arg.attr);
487 return send_reply_ok(req, &arg, size);
490 int fuse_reply_readlink(fuse_req_t req, const char *linkname)
492 return send_reply_ok(req, linkname, strlen(linkname));
495 int fuse_reply_open(fuse_req_t req, const struct fuse_file_info *f)
497 struct fuse_open_out arg;
499 memset(&arg, 0, sizeof(arg));
500 fill_open(&arg, f);
501 return send_reply_ok(req, &arg, sizeof(arg));
504 int fuse_reply_write(fuse_req_t req, size_t count)
506 struct fuse_write_out arg;
508 memset(&arg, 0, sizeof(arg));
509 arg.size = count;
511 return send_reply_ok(req, &arg, sizeof(arg));
514 int fuse_reply_buf(fuse_req_t req, const char *buf, size_t size)
516 return send_reply_ok(req, buf, size);
519 static int fuse_send_data_iov_fallback(struct fuse_ll *f, struct fuse_chan *ch,
520 struct iovec *iov, int iov_count,
521 struct fuse_bufvec *buf,
522 size_t len)
524 struct fuse_bufvec mem_buf = FUSE_BUFVEC_INIT(len);
525 void *mbuf;
526 int res;
528 /* Optimize common case */
529 if (buf->count == 1 && buf->idx == 0 && buf->off == 0 &&
530 !(buf->buf[0].flags & FUSE_BUF_IS_FD)) {
531 /* FIXME: also avoid memory copy if there are multiple buffers
532 but none of them contain an fd */
534 iov[iov_count].iov_base = buf->buf[0].mem;
535 iov[iov_count].iov_len = len;
536 iov_count++;
537 return fuse_send_msg(f, ch, iov, iov_count);
540 res = posix_memalign(&mbuf, pagesize, len);
541 if (res != 0)
542 return res;
544 mem_buf.buf[0].mem = mbuf;
545 res = fuse_buf_copy(&mem_buf, buf, 0);
546 if (res < 0) {
547 free(mbuf);
548 return -res;
550 len = res;
552 iov[iov_count].iov_base = mbuf;
553 iov[iov_count].iov_len = len;
554 iov_count++;
555 res = fuse_send_msg(f, ch, iov, iov_count);
556 free(mbuf);
558 return res;
561 struct fuse_ll_pipe {
562 size_t size;
563 int can_grow;
564 int pipe[2];
567 static void fuse_ll_pipe_free(struct fuse_ll_pipe *llp)
569 close(llp->pipe[0]);
570 close(llp->pipe[1]);
571 free(llp);
574 #ifdef HAVE_SPLICE
575 #if !defined(HAVE_PIPE2) || !defined(O_CLOEXEC)
576 static int fuse_pipe(int fds[2])
578 int rv = pipe(fds);
580 if (rv == -1)
581 return rv;
583 if (fcntl(fds[0], F_SETFL, O_NONBLOCK) == -1 ||
584 fcntl(fds[1], F_SETFL, O_NONBLOCK) == -1 ||
585 fcntl(fds[0], F_SETFD, FD_CLOEXEC) == -1 ||
586 fcntl(fds[1], F_SETFD, FD_CLOEXEC) == -1) {
587 close(fds[0]);
588 close(fds[1]);
589 rv = -1;
591 return rv;
593 #else
594 static int fuse_pipe(int fds[2])
596 return pipe2(fds, O_CLOEXEC | O_NONBLOCK);
598 #endif
600 static struct fuse_ll_pipe *fuse_ll_get_pipe(struct fuse_ll *f)
602 struct fuse_ll_pipe *llp = pthread_getspecific(f->pipe_key);
603 if (llp == NULL) {
604 int res;
606 llp = malloc(sizeof(struct fuse_ll_pipe));
607 if (llp == NULL)
608 return NULL;
610 res = fuse_pipe(llp->pipe);
611 if (res == -1) {
612 free(llp);
613 return NULL;
617 *the default size is 16 pages on linux
619 llp->size = pagesize * 16;
620 llp->can_grow = 1;
622 pthread_setspecific(f->pipe_key, llp);
625 return llp;
627 #endif
629 static void fuse_ll_clear_pipe(struct fuse_ll *f)
631 struct fuse_ll_pipe *llp = pthread_getspecific(f->pipe_key);
632 if (llp) {
633 pthread_setspecific(f->pipe_key, NULL);
634 fuse_ll_pipe_free(llp);
638 #if defined(HAVE_SPLICE) && defined(HAVE_VMSPLICE)
639 static int read_back(int fd, char *buf, size_t len)
641 int res;
643 res = read(fd, buf, len);
644 if (res == -1) {
645 fprintf(stderr, "fuse: internal error: failed to read back from pipe: %s\n", strerror(errno));
646 return -EIO;
648 if (res != len) {
649 fprintf(stderr, "fuse: internal error: short read back from pipe: %i from %zi\n", res, len);
650 return -EIO;
652 return 0;
655 static int fuse_send_data_iov(struct fuse_ll *f, struct fuse_chan *ch,
656 struct iovec *iov, int iov_count,
657 struct fuse_bufvec *buf, unsigned int flags)
659 int res;
660 size_t len = fuse_buf_size(buf);
661 struct fuse_out_header *out = iov[0].iov_base;
662 struct fuse_ll_pipe *llp;
663 int splice_flags;
664 size_t pipesize;
665 size_t total_fd_size;
666 size_t idx;
667 size_t headerlen;
668 struct fuse_bufvec pipe_buf = FUSE_BUFVEC_INIT(len);
670 if (f->broken_splice_nonblock)
671 goto fallback;
673 if (flags & FUSE_BUF_NO_SPLICE)
674 goto fallback;
676 total_fd_size = 0;
677 for (idx = buf->idx; idx < buf->count; idx++) {
678 if (buf->buf[idx].flags & FUSE_BUF_IS_FD) {
679 total_fd_size = buf->buf[idx].size;
680 if (idx == buf->idx)
681 total_fd_size -= buf->off;
684 if (total_fd_size < 2 * pagesize)
685 goto fallback;
687 if (f->conn.proto_minor < 14 ||
688 !(f->conn.want & FUSE_CAP_SPLICE_WRITE))
689 goto fallback;
691 llp = fuse_ll_get_pipe(f);
692 if (llp == NULL)
693 goto fallback;
696 headerlen = iov_length(iov, iov_count);
698 out->len = headerlen + len;
701 * Heuristic for the required pipe size, does not work if the
702 * source contains less than page size fragments
704 pipesize = pagesize * (iov_count + buf->count + 1) + out->len;
706 if (llp->size < pipesize) {
707 if (llp->can_grow) {
708 res = fcntl(llp->pipe[0], F_SETPIPE_SZ, pipesize);
709 if (res == -1) {
710 llp->can_grow = 0;
711 goto fallback;
713 llp->size = res;
715 if (llp->size < pipesize)
716 goto fallback;
720 res = vmsplice(llp->pipe[1], iov, iov_count, SPLICE_F_NONBLOCK);
721 if (res == -1)
722 goto fallback;
724 if (res != headerlen) {
725 res = -EIO;
726 fprintf(stderr, "fuse: short vmsplice to pipe: %u/%zu\n", res,
727 headerlen);
728 goto clear_pipe;
731 pipe_buf.buf[0].flags = FUSE_BUF_IS_FD;
732 pipe_buf.buf[0].fd = llp->pipe[1];
734 res = fuse_buf_copy(&pipe_buf, buf,
735 FUSE_BUF_FORCE_SPLICE | FUSE_BUF_SPLICE_NONBLOCK);
736 if (res < 0) {
737 if (res == -EAGAIN || res == -EINVAL) {
739 * Should only get EAGAIN on kernels with
740 * broken SPLICE_F_NONBLOCK support (<=
741 * 2.6.35) where this error or a short read is
742 * returned even if the pipe itself is not
743 * full
745 * EINVAL might mean that splice can't handle
746 * this combination of input and output.
748 if (res == -EAGAIN)
749 f->broken_splice_nonblock = 1;
751 pthread_setspecific(f->pipe_key, NULL);
752 fuse_ll_pipe_free(llp);
753 goto fallback;
755 res = -res;
756 goto clear_pipe;
759 if (res != 0 && res < len) {
760 struct fuse_bufvec mem_buf = FUSE_BUFVEC_INIT(len);
761 void *mbuf;
762 size_t now_len = res;
764 * For regular files a short count is either
765 * 1) due to EOF, or
766 * 2) because of broken SPLICE_F_NONBLOCK (see above)
768 * For other inputs it's possible that we overflowed
769 * the pipe because of small buffer fragments.
772 res = posix_memalign(&mbuf, pagesize, len);
773 if (res != 0)
774 goto clear_pipe;
776 mem_buf.buf[0].mem = mbuf;
777 mem_buf.off = now_len;
778 res = fuse_buf_copy(&mem_buf, buf, 0);
779 if (res > 0) {
780 char *tmpbuf;
781 size_t extra_len = res;
783 * Trickiest case: got more data. Need to get
784 * back the data from the pipe and then fall
785 * back to regular write.
787 tmpbuf = malloc(headerlen);
788 if (tmpbuf == NULL) {
789 free(mbuf);
790 res = ENOMEM;
791 goto clear_pipe;
793 res = read_back(llp->pipe[0], tmpbuf, headerlen);
794 if (res != 0) {
795 free(mbuf);
796 goto clear_pipe;
798 free(tmpbuf);
799 res = read_back(llp->pipe[0], mbuf, now_len);
800 if (res != 0) {
801 free(mbuf);
802 goto clear_pipe;
804 len = now_len + extra_len;
805 iov[iov_count].iov_base = mbuf;
806 iov[iov_count].iov_len = len;
807 iov_count++;
808 res = fuse_send_msg(f, ch, iov, iov_count);
809 free(mbuf);
810 return res;
812 free(mbuf);
813 res = now_len;
815 len = res;
816 out->len = headerlen + len;
818 if (f->debug) {
819 fprintf(stderr,
820 " unique: %llu, success, outsize: %i (splice)\n",
821 (unsigned long long) out->unique, out->len);
824 splice_flags = 0;
825 if ((flags & FUSE_BUF_SPLICE_MOVE) &&
826 (f->conn.want & FUSE_CAP_SPLICE_MOVE))
827 splice_flags |= SPLICE_F_MOVE;
829 res = splice(llp->pipe[0], NULL,
830 fuse_chan_fd(ch), NULL, out->len, splice_flags);
831 if (res == -1) {
832 res = -errno;
833 perror("fuse: splice from pipe");
834 goto clear_pipe;
836 if (res != out->len) {
837 res = -EIO;
838 fprintf(stderr, "fuse: short splice from pipe: %u/%u\n",
839 res, out->len);
840 goto clear_pipe;
842 return 0;
844 clear_pipe:
845 fuse_ll_clear_pipe(f);
846 return res;
848 fallback:
849 return fuse_send_data_iov_fallback(f, ch, iov, iov_count, buf, len);
851 #else
852 static int fuse_send_data_iov(struct fuse_ll *f, struct fuse_chan *ch,
853 struct iovec *iov, int iov_count,
854 struct fuse_bufvec *buf, unsigned int flags)
856 size_t len = fuse_buf_size(buf);
857 (void) flags;
859 return fuse_send_data_iov_fallback(f, ch, iov, iov_count, buf, len);
861 #endif
863 int fuse_reply_data(fuse_req_t req, struct fuse_bufvec *bufv,
864 enum fuse_buf_copy_flags flags)
866 struct iovec iov[2];
867 struct fuse_out_header out;
868 int res;
870 iov[0].iov_base = &out;
871 iov[0].iov_len = sizeof(struct fuse_out_header);
873 out.unique = req->unique;
874 out.error = 0;
876 res = fuse_send_data_iov(req->f, req->ch, iov, 1, bufv, flags);
877 if (res <= 0) {
878 fuse_free_req(req);
879 return res;
880 } else {
881 return fuse_reply_err(req, res);
885 int fuse_reply_statfs(fuse_req_t req, const struct statvfs *stbuf)
887 struct fuse_statfs_out arg;
888 size_t size = req->f->conn.proto_minor < 4 ?
889 FUSE_COMPAT_STATFS_SIZE : sizeof(arg);
891 memset(&arg, 0, sizeof(arg));
892 convert_statfs(stbuf, &arg.st);
894 return send_reply_ok(req, &arg, size);
897 int fuse_reply_xattr(fuse_req_t req, size_t count)
899 struct fuse_getxattr_out arg;
901 memset(&arg, 0, sizeof(arg));
902 arg.size = count;
904 return send_reply_ok(req, &arg, sizeof(arg));
907 int fuse_reply_lock(fuse_req_t req, const struct flock *lock)
909 struct fuse_lk_out arg;
911 memset(&arg, 0, sizeof(arg));
912 arg.lk.type = lock->l_type;
913 if (lock->l_type != F_UNLCK) {
914 arg.lk.start = lock->l_start;
915 if (lock->l_len == 0)
916 arg.lk.end = OFFSET_MAX;
917 else
918 arg.lk.end = lock->l_start + lock->l_len - 1;
920 arg.lk.pid = lock->l_pid;
921 return send_reply_ok(req, &arg, sizeof(arg));
924 int fuse_reply_bmap(fuse_req_t req, uint64_t idx)
926 struct fuse_bmap_out arg;
928 memset(&arg, 0, sizeof(arg));
929 arg.block = idx;
931 return send_reply_ok(req, &arg, sizeof(arg));
934 static struct fuse_ioctl_iovec *fuse_ioctl_iovec_copy(const struct iovec *iov,
935 size_t count)
937 struct fuse_ioctl_iovec *fiov;
938 size_t i;
940 fiov = malloc(sizeof(fiov[0]) * count);
941 if (!fiov)
942 return NULL;
944 for (i = 0; i < count; i++) {
945 fiov[i].base = (uintptr_t) iov[i].iov_base;
946 fiov[i].len = iov[i].iov_len;
949 return fiov;
952 int fuse_reply_ioctl_retry(fuse_req_t req,
953 const struct iovec *in_iov, size_t in_count,
954 const struct iovec *out_iov, size_t out_count)
956 struct fuse_ioctl_out arg;
957 struct fuse_ioctl_iovec *in_fiov = NULL;
958 struct fuse_ioctl_iovec *out_fiov = NULL;
959 struct iovec iov[4];
960 size_t count = 1;
961 int res;
963 memset(&arg, 0, sizeof(arg));
964 arg.flags |= FUSE_IOCTL_RETRY;
965 arg.in_iovs = in_count;
966 arg.out_iovs = out_count;
967 iov[count].iov_base = &arg;
968 iov[count].iov_len = sizeof(arg);
969 count++;
971 if (req->f->conn.proto_minor < 16) {
972 if (in_count) {
973 iov[count].iov_base = (void *)in_iov;
974 iov[count].iov_len = sizeof(in_iov[0]) * in_count;
975 count++;
978 if (out_count) {
979 iov[count].iov_base = (void *)out_iov;
980 iov[count].iov_len = sizeof(out_iov[0]) * out_count;
981 count++;
983 } else {
984 /* Can't handle non-compat 64bit ioctls on 32bit */
985 if (sizeof(void *) == 4 && req->ioctl_64bit) {
986 res = fuse_reply_err(req, EINVAL);
987 goto out;
990 if (in_count) {
991 in_fiov = fuse_ioctl_iovec_copy(in_iov, in_count);
992 if (!in_fiov)
993 goto enomem;
995 iov[count].iov_base = (void *)in_fiov;
996 iov[count].iov_len = sizeof(in_fiov[0]) * in_count;
997 count++;
999 if (out_count) {
1000 out_fiov = fuse_ioctl_iovec_copy(out_iov, out_count);
1001 if (!out_fiov)
1002 goto enomem;
1004 iov[count].iov_base = (void *)out_fiov;
1005 iov[count].iov_len = sizeof(out_fiov[0]) * out_count;
1006 count++;
1010 res = send_reply_iov(req, 0, iov, count);
1011 out:
1012 free(in_fiov);
1013 free(out_fiov);
1015 return res;
1017 enomem:
1018 res = fuse_reply_err(req, ENOMEM);
1019 goto out;
1022 int fuse_reply_ioctl(fuse_req_t req, int result, const void *buf, size_t size)
1024 struct fuse_ioctl_out arg;
1025 struct iovec iov[3];
1026 size_t count = 1;
1028 memset(&arg, 0, sizeof(arg));
1029 arg.result = result;
1030 iov[count].iov_base = &arg;
1031 iov[count].iov_len = sizeof(arg);
1032 count++;
1034 if (size) {
1035 iov[count].iov_base = (char *) buf;
1036 iov[count].iov_len = size;
1037 count++;
1040 return send_reply_iov(req, 0, iov, count);
1043 int fuse_reply_ioctl_iov(fuse_req_t req, int result, const struct iovec *iov,
1044 int count)
1046 struct iovec *padded_iov;
1047 struct fuse_ioctl_out arg;
1048 int res;
1050 padded_iov = malloc((count + 2) * sizeof(struct iovec));
1051 if (padded_iov == NULL)
1052 return fuse_reply_err(req, ENOMEM);
1054 memset(&arg, 0, sizeof(arg));
1055 arg.result = result;
1056 padded_iov[1].iov_base = &arg;
1057 padded_iov[1].iov_len = sizeof(arg);
1059 memcpy(&padded_iov[2], iov, count * sizeof(struct iovec));
1061 res = send_reply_iov(req, 0, padded_iov, count + 2);
1062 free(padded_iov);
1064 return res;
1067 int fuse_reply_poll(fuse_req_t req, unsigned revents)
1069 struct fuse_poll_out arg;
1071 memset(&arg, 0, sizeof(arg));
1072 arg.revents = revents;
1074 return send_reply_ok(req, &arg, sizeof(arg));
1077 static void do_lookup(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1079 char *name = (char *) inarg;
1081 if (req->f->op.lookup)
1082 req->f->op.lookup(req, nodeid, name);
1083 else
1084 fuse_reply_err(req, ENOSYS);
1087 static void do_forget(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1089 struct fuse_forget_in *arg = (struct fuse_forget_in *) inarg;
1091 if (req->f->op.forget)
1092 req->f->op.forget(req, nodeid, arg->nlookup);
1093 else
1094 fuse_reply_none(req);
1097 static void do_batch_forget(fuse_req_t req, fuse_ino_t nodeid,
1098 const void *inarg)
1100 struct fuse_batch_forget_in *arg = (void *) inarg;
1101 struct fuse_forget_one *param = (void *) PARAM(arg);
1102 unsigned int i;
1104 (void) nodeid;
1106 if (req->f->op.forget_multi) {
1107 req->f->op.forget_multi(req, arg->count,
1108 (struct fuse_forget_data *) param);
1109 } else if (req->f->op.forget) {
1110 for (i = 0; i < arg->count; i++) {
1111 struct fuse_forget_one *forget = &param[i];
1112 struct fuse_req *dummy_req;
1114 dummy_req = fuse_ll_alloc_req(req->f);
1115 if (dummy_req == NULL)
1116 break;
1118 dummy_req->unique = req->unique;
1119 dummy_req->ctx = req->ctx;
1120 dummy_req->ch = NULL;
1122 req->f->op.forget(dummy_req, forget->nodeid,
1123 forget->nlookup);
1125 fuse_reply_none(req);
1126 } else {
1127 fuse_reply_none(req);
1131 static void do_getattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1133 struct fuse_file_info *fip = NULL;
1134 struct fuse_file_info fi;
1136 if (req->f->conn.proto_minor >= 9) {
1137 struct fuse_getattr_in *arg = (struct fuse_getattr_in *) inarg;
1139 if (arg->getattr_flags & FUSE_GETATTR_FH) {
1140 memset(&fi, 0, sizeof(fi));
1141 fi.fh = arg->fh;
1142 fip = &fi;
1146 if (req->f->op.getattr)
1147 req->f->op.getattr(req, nodeid, fip);
1148 else
1149 fuse_reply_err(req, ENOSYS);
1152 static void do_setattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1154 struct fuse_setattr_in *arg = (struct fuse_setattr_in *) inarg;
1156 if (req->f->op.setattr) {
1157 struct fuse_file_info *fi = NULL;
1158 struct fuse_file_info fi_store;
1159 struct stat stbuf;
1160 memset(&stbuf, 0, sizeof(stbuf));
1161 convert_attr(arg, &stbuf);
1162 if (arg->valid & FATTR_FH) {
1163 arg->valid &= ~FATTR_FH;
1164 memset(&fi_store, 0, sizeof(fi_store));
1165 fi = &fi_store;
1166 fi->fh = arg->fh;
1168 arg->valid &=
1169 FUSE_SET_ATTR_MODE |
1170 FUSE_SET_ATTR_UID |
1171 FUSE_SET_ATTR_GID |
1172 FUSE_SET_ATTR_SIZE |
1173 FUSE_SET_ATTR_ATIME |
1174 FUSE_SET_ATTR_MTIME |
1175 FUSE_SET_ATTR_ATIME_NOW |
1176 FUSE_SET_ATTR_MTIME_NOW;
1178 req->f->op.setattr(req, nodeid, &stbuf, arg->valid, fi);
1179 } else
1180 fuse_reply_err(req, ENOSYS);
1183 static void do_access(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1185 struct fuse_access_in *arg = (struct fuse_access_in *) inarg;
1187 if (req->f->op.access)
1188 req->f->op.access(req, nodeid, arg->mask);
1189 else
1190 fuse_reply_err(req, ENOSYS);
1193 static void do_readlink(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1195 (void) inarg;
1197 if (req->f->op.readlink)
1198 req->f->op.readlink(req, nodeid);
1199 else
1200 fuse_reply_err(req, ENOSYS);
1203 static void do_mknod(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1205 struct fuse_mknod_in *arg = (struct fuse_mknod_in *) inarg;
1206 char *name = PARAM(arg);
1208 if (req->f->conn.proto_minor >= 12)
1209 req->ctx.umask = arg->umask;
1210 else
1211 name = (char *) inarg + FUSE_COMPAT_MKNOD_IN_SIZE;
1213 if (req->f->op.mknod)
1214 req->f->op.mknod(req, nodeid, name, arg->mode, arg->rdev);
1215 else
1216 fuse_reply_err(req, ENOSYS);
1219 static void do_mkdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1221 struct fuse_mkdir_in *arg = (struct fuse_mkdir_in *) inarg;
1223 if (req->f->conn.proto_minor >= 12)
1224 req->ctx.umask = arg->umask;
1226 if (req->f->op.mkdir)
1227 req->f->op.mkdir(req, nodeid, PARAM(arg), arg->mode);
1228 else
1229 fuse_reply_err(req, ENOSYS);
1232 static void do_unlink(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1234 char *name = (char *) inarg;
1236 if (req->f->op.unlink)
1237 req->f->op.unlink(req, nodeid, name);
1238 else
1239 fuse_reply_err(req, ENOSYS);
1242 static void do_rmdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1244 char *name = (char *) inarg;
1246 if (req->f->op.rmdir)
1247 req->f->op.rmdir(req, nodeid, name);
1248 else
1249 fuse_reply_err(req, ENOSYS);
1252 static void do_symlink(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1254 char *name = (char *) inarg;
1255 char *linkname = ((char *) inarg) + strlen((char *) inarg) + 1;
1257 if (req->f->op.symlink)
1258 req->f->op.symlink(req, linkname, nodeid, name);
1259 else
1260 fuse_reply_err(req, ENOSYS);
1263 static void do_rename(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1265 struct fuse_rename_in *arg = (struct fuse_rename_in *) inarg;
1266 char *oldname = PARAM(arg);
1267 char *newname = oldname + strlen(oldname) + 1;
1269 if (req->f->op.rename)
1270 req->f->op.rename(req, nodeid, oldname, arg->newdir, newname);
1271 else
1272 fuse_reply_err(req, ENOSYS);
1275 static void do_link(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1277 struct fuse_link_in *arg = (struct fuse_link_in *) inarg;
1279 if (req->f->op.link)
1280 req->f->op.link(req, arg->oldnodeid, nodeid, PARAM(arg));
1281 else
1282 fuse_reply_err(req, ENOSYS);
1285 static void do_create(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1287 struct fuse_create_in *arg = (struct fuse_create_in *) inarg;
1289 if (req->f->op.create) {
1290 struct fuse_file_info fi;
1291 char *name = PARAM(arg);
1293 memset(&fi, 0, sizeof(fi));
1294 fi.flags = arg->flags;
1296 if (req->f->conn.proto_minor >= 12)
1297 req->ctx.umask = arg->umask;
1298 else
1299 name = (char *) inarg + sizeof(struct fuse_open_in);
1301 req->f->op.create(req, nodeid, name, arg->mode, &fi);
1302 } else
1303 fuse_reply_err(req, ENOSYS);
1306 static void do_open(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1308 struct fuse_open_in *arg = (struct fuse_open_in *) inarg;
1309 struct fuse_file_info fi;
1311 memset(&fi, 0, sizeof(fi));
1312 fi.flags = arg->flags;
1314 if (req->f->op.open)
1315 req->f->op.open(req, nodeid, &fi);
1316 else
1317 fuse_reply_open(req, &fi);
1320 static void do_read(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1322 struct fuse_read_in *arg = (struct fuse_read_in *) inarg;
1324 if (req->f->op.read) {
1325 struct fuse_file_info fi;
1327 memset(&fi, 0, sizeof(fi));
1328 fi.fh = arg->fh;
1329 if (req->f->conn.proto_minor >= 9) {
1330 fi.lock_owner = arg->lock_owner;
1331 fi.flags = arg->flags;
1333 req->f->op.read(req, nodeid, arg->size, arg->offset, &fi);
1334 } else
1335 fuse_reply_err(req, ENOSYS);
1338 static void do_write(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1340 struct fuse_write_in *arg = (struct fuse_write_in *) inarg;
1341 struct fuse_file_info fi;
1342 char *param;
1344 memset(&fi, 0, sizeof(fi));
1345 fi.fh = arg->fh;
1346 fi.writepage = (arg->write_flags & 1) != 0;
1348 if (req->f->conn.proto_minor < 9) {
1349 param = ((char *) arg) + FUSE_COMPAT_WRITE_IN_SIZE;
1350 } else {
1351 fi.lock_owner = arg->lock_owner;
1352 fi.flags = arg->flags;
1353 param = PARAM(arg);
1356 if (req->f->op.write)
1357 req->f->op.write(req, nodeid, param, arg->size,
1358 arg->offset, &fi);
1359 else
1360 fuse_reply_err(req, ENOSYS);
1363 static void do_write_buf(fuse_req_t req, fuse_ino_t nodeid, const void *inarg,
1364 const struct fuse_buf *ibuf)
1366 struct fuse_ll *f = req->f;
1367 struct fuse_bufvec bufv = {
1368 .buf[0] = *ibuf,
1369 .count = 1,
1371 struct fuse_write_in *arg = (struct fuse_write_in *) inarg;
1372 struct fuse_file_info fi;
1374 memset(&fi, 0, sizeof(fi));
1375 fi.fh = arg->fh;
1376 fi.writepage = arg->write_flags & 1;
1378 if (req->f->conn.proto_minor < 9) {
1379 bufv.buf[0].mem = ((char *) arg) + FUSE_COMPAT_WRITE_IN_SIZE;
1380 bufv.buf[0].size -= sizeof(struct fuse_in_header) +
1381 FUSE_COMPAT_WRITE_IN_SIZE;
1382 assert(!(bufv.buf[0].flags & FUSE_BUF_IS_FD));
1383 } else {
1384 fi.lock_owner = arg->lock_owner;
1385 fi.flags = arg->flags;
1386 if (!(bufv.buf[0].flags & FUSE_BUF_IS_FD))
1387 bufv.buf[0].mem = PARAM(arg);
1389 bufv.buf[0].size -= sizeof(struct fuse_in_header) +
1390 sizeof(struct fuse_write_in);
1392 if (bufv.buf[0].size < arg->size) {
1393 fprintf(stderr, "fuse: do_write_buf: buffer size too small\n");
1394 fuse_reply_err(req, EIO);
1395 goto out;
1397 bufv.buf[0].size = arg->size;
1399 req->f->op.write_buf(req, nodeid, &bufv, arg->offset, &fi);
1401 out:
1402 /* Need to reset the pipe if ->write_buf() didn't consume all data */
1403 if ((ibuf->flags & FUSE_BUF_IS_FD) && bufv.idx < bufv.count)
1404 fuse_ll_clear_pipe(f);
1407 static void do_flush(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1409 struct fuse_flush_in *arg = (struct fuse_flush_in *) inarg;
1410 struct fuse_file_info fi;
1412 memset(&fi, 0, sizeof(fi));
1413 fi.fh = arg->fh;
1414 fi.flush = 1;
1415 if (req->f->conn.proto_minor >= 7)
1416 fi.lock_owner = arg->lock_owner;
1418 if (req->f->op.flush)
1419 req->f->op.flush(req, nodeid, &fi);
1420 else
1421 fuse_reply_err(req, ENOSYS);
1424 static void do_release(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1426 struct fuse_release_in *arg = (struct fuse_release_in *) inarg;
1427 struct fuse_file_info fi;
1429 memset(&fi, 0, sizeof(fi));
1430 fi.flags = arg->flags;
1431 fi.fh = arg->fh;
1432 if (req->f->conn.proto_minor >= 8) {
1433 fi.flush = (arg->release_flags & FUSE_RELEASE_FLUSH) ? 1 : 0;
1434 fi.lock_owner = arg->lock_owner;
1436 if (arg->release_flags & FUSE_RELEASE_FLOCK_UNLOCK) {
1437 fi.flock_release = 1;
1438 fi.lock_owner = arg->lock_owner;
1441 if (req->f->op.release)
1442 req->f->op.release(req, nodeid, &fi);
1443 else
1444 fuse_reply_err(req, 0);
1447 static void do_fsync(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1449 struct fuse_fsync_in *arg = (struct fuse_fsync_in *) inarg;
1450 struct fuse_file_info fi;
1452 memset(&fi, 0, sizeof(fi));
1453 fi.fh = arg->fh;
1455 if (req->f->op.fsync)
1456 req->f->op.fsync(req, nodeid, arg->fsync_flags & 1, &fi);
1457 else
1458 fuse_reply_err(req, ENOSYS);
1461 static void do_opendir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1463 struct fuse_open_in *arg = (struct fuse_open_in *) inarg;
1464 struct fuse_file_info fi;
1466 memset(&fi, 0, sizeof(fi));
1467 fi.flags = arg->flags;
1469 if (req->f->op.opendir)
1470 req->f->op.opendir(req, nodeid, &fi);
1471 else
1472 fuse_reply_open(req, &fi);
1475 static void do_readdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1477 struct fuse_read_in *arg = (struct fuse_read_in *) inarg;
1478 struct fuse_file_info fi;
1480 memset(&fi, 0, sizeof(fi));
1481 fi.fh = arg->fh;
1483 if (req->f->op.readdir)
1484 req->f->op.readdir(req, nodeid, arg->size, arg->offset, &fi);
1485 else
1486 fuse_reply_err(req, ENOSYS);
1489 static void do_readdirplus(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1491 struct fuse_read_in *arg = (struct fuse_read_in *) inarg;
1492 struct fuse_file_info fi;
1494 memset(&fi, 0, sizeof(fi));
1495 fi.fh = arg->fh;
1497 if (req->f->op.readdirplus)
1498 req->f->op.readdirplus(req, nodeid, arg->size, arg->offset, &fi);
1499 else
1500 fuse_reply_err(req, ENOSYS);
1503 static void do_releasedir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1505 struct fuse_release_in *arg = (struct fuse_release_in *) inarg;
1506 struct fuse_file_info fi;
1508 memset(&fi, 0, sizeof(fi));
1509 fi.flags = arg->flags;
1510 fi.fh = arg->fh;
1512 if (req->f->op.releasedir)
1513 req->f->op.releasedir(req, nodeid, &fi);
1514 else
1515 fuse_reply_err(req, 0);
1518 static void do_fsyncdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1520 struct fuse_fsync_in *arg = (struct fuse_fsync_in *) inarg;
1521 struct fuse_file_info fi;
1523 memset(&fi, 0, sizeof(fi));
1524 fi.fh = arg->fh;
1526 if (req->f->op.fsyncdir)
1527 req->f->op.fsyncdir(req, nodeid, arg->fsync_flags & 1, &fi);
1528 else
1529 fuse_reply_err(req, ENOSYS);
1532 static void do_statfs(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1534 (void) nodeid;
1535 (void) inarg;
1537 if (req->f->op.statfs)
1538 req->f->op.statfs(req, nodeid);
1539 else {
1540 struct statvfs buf = {
1541 .f_namemax = 255,
1542 .f_bsize = 512,
1544 fuse_reply_statfs(req, &buf);
1548 static void do_setxattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1550 struct fuse_setxattr_in *arg = (struct fuse_setxattr_in *) inarg;
1551 char *name = PARAM(arg);
1552 char *value = name + strlen(name) + 1;
1554 if (req->f->op.setxattr)
1555 req->f->op.setxattr(req, nodeid, name, value, arg->size,
1556 arg->flags);
1557 else
1558 fuse_reply_err(req, ENOSYS);
1561 static void do_getxattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1563 struct fuse_getxattr_in *arg = (struct fuse_getxattr_in *) inarg;
1565 if (req->f->op.getxattr)
1566 req->f->op.getxattr(req, nodeid, PARAM(arg), arg->size);
1567 else
1568 fuse_reply_err(req, ENOSYS);
1571 static void do_listxattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1573 struct fuse_getxattr_in *arg = (struct fuse_getxattr_in *) inarg;
1575 if (req->f->op.listxattr)
1576 req->f->op.listxattr(req, nodeid, arg->size);
1577 else
1578 fuse_reply_err(req, ENOSYS);
1581 static void do_removexattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1583 char *name = (char *) inarg;
1585 if (req->f->op.removexattr)
1586 req->f->op.removexattr(req, nodeid, name);
1587 else
1588 fuse_reply_err(req, ENOSYS);
1591 static void convert_fuse_file_lock(struct fuse_file_lock *fl,
1592 struct flock *flock)
1594 memset(flock, 0, sizeof(struct flock));
1595 flock->l_type = fl->type;
1596 flock->l_whence = SEEK_SET;
1597 flock->l_start = fl->start;
1598 if (fl->end == OFFSET_MAX)
1599 flock->l_len = 0;
1600 else
1601 flock->l_len = fl->end - fl->start + 1;
1602 flock->l_pid = fl->pid;
1605 static void do_getlk(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1607 struct fuse_lk_in *arg = (struct fuse_lk_in *) inarg;
1608 struct fuse_file_info fi;
1609 struct flock flock;
1611 memset(&fi, 0, sizeof(fi));
1612 fi.fh = arg->fh;
1613 fi.lock_owner = arg->owner;
1615 convert_fuse_file_lock(&arg->lk, &flock);
1616 if (req->f->op.getlk)
1617 req->f->op.getlk(req, nodeid, &fi, &flock);
1618 else
1619 fuse_reply_err(req, ENOSYS);
1622 static void do_setlk_common(fuse_req_t req, fuse_ino_t nodeid,
1623 const void *inarg, int sleep)
1625 struct fuse_lk_in *arg = (struct fuse_lk_in *) inarg;
1626 struct fuse_file_info fi;
1627 struct flock flock;
1629 memset(&fi, 0, sizeof(fi));
1630 fi.fh = arg->fh;
1631 fi.lock_owner = arg->owner;
1633 if (arg->lk_flags & FUSE_LK_FLOCK) {
1634 int op = 0;
1636 switch (arg->lk.type) {
1637 case F_RDLCK:
1638 op = LOCK_SH;
1639 break;
1640 case F_WRLCK:
1641 op = LOCK_EX;
1642 break;
1643 case F_UNLCK:
1644 op = LOCK_UN;
1645 break;
1647 if (!sleep)
1648 op |= LOCK_NB;
1650 if (req->f->op.flock)
1651 req->f->op.flock(req, nodeid, &fi, op);
1652 else
1653 fuse_reply_err(req, ENOSYS);
1654 } else {
1655 convert_fuse_file_lock(&arg->lk, &flock);
1656 if (req->f->op.setlk)
1657 req->f->op.setlk(req, nodeid, &fi, &flock, sleep);
1658 else
1659 fuse_reply_err(req, ENOSYS);
1663 static void do_setlk(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1665 do_setlk_common(req, nodeid, inarg, 0);
1668 static void do_setlkw(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1670 do_setlk_common(req, nodeid, inarg, 1);
1673 static int find_interrupted(struct fuse_ll *f, struct fuse_req *req)
1675 struct fuse_req *curr;
1677 for (curr = f->list.next; curr != &f->list; curr = curr->next) {
1678 if (curr->unique == req->u.i.unique) {
1679 fuse_interrupt_func_t func;
1680 void *data;
1682 curr->ctr++;
1683 pthread_mutex_unlock(&f->lock);
1685 /* Ugh, ugly locking */
1686 pthread_mutex_lock(&curr->lock);
1687 pthread_mutex_lock(&f->lock);
1688 curr->interrupted = 1;
1689 func = curr->u.ni.func;
1690 data = curr->u.ni.data;
1691 pthread_mutex_unlock(&f->lock);
1692 if (func)
1693 func(curr, data);
1694 pthread_mutex_unlock(&curr->lock);
1696 pthread_mutex_lock(&f->lock);
1697 curr->ctr--;
1698 if (!curr->ctr)
1699 destroy_req(curr);
1701 return 1;
1704 for (curr = f->interrupts.next; curr != &f->interrupts;
1705 curr = curr->next) {
1706 if (curr->u.i.unique == req->u.i.unique)
1707 return 1;
1709 return 0;
1712 static void do_interrupt(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1714 struct fuse_interrupt_in *arg = (struct fuse_interrupt_in *) inarg;
1715 struct fuse_ll *f = req->f;
1717 (void) nodeid;
1718 if (f->debug)
1719 fprintf(stderr, "INTERRUPT: %llu\n",
1720 (unsigned long long) arg->unique);
1722 req->u.i.unique = arg->unique;
1724 pthread_mutex_lock(&f->lock);
1725 if (find_interrupted(f, req))
1726 destroy_req(req);
1727 else
1728 list_add_req(req, &f->interrupts);
1729 pthread_mutex_unlock(&f->lock);
1732 static struct fuse_req *check_interrupt(struct fuse_ll *f, struct fuse_req *req)
1734 struct fuse_req *curr;
1736 for (curr = f->interrupts.next; curr != &f->interrupts;
1737 curr = curr->next) {
1738 if (curr->u.i.unique == req->unique) {
1739 req->interrupted = 1;
1740 list_del_req(curr);
1741 free(curr);
1742 return NULL;
1745 curr = f->interrupts.next;
1746 if (curr != &f->interrupts) {
1747 list_del_req(curr);
1748 list_init_req(curr);
1749 return curr;
1750 } else
1751 return NULL;
1754 static void do_bmap(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1756 struct fuse_bmap_in *arg = (struct fuse_bmap_in *) inarg;
1758 if (req->f->op.bmap)
1759 req->f->op.bmap(req, nodeid, arg->blocksize, arg->block);
1760 else
1761 fuse_reply_err(req, ENOSYS);
1764 static void do_ioctl(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1766 struct fuse_ioctl_in *arg = (struct fuse_ioctl_in *) inarg;
1767 unsigned int flags = arg->flags;
1768 void *in_buf = arg->in_size ? PARAM(arg) : NULL;
1769 struct fuse_file_info fi;
1771 if (flags & FUSE_IOCTL_DIR &&
1772 !(req->f->conn.want & FUSE_CAP_IOCTL_DIR)) {
1773 fuse_reply_err(req, ENOTTY);
1774 return;
1777 memset(&fi, 0, sizeof(fi));
1778 fi.fh = arg->fh;
1780 if (sizeof(void *) == 4 && req->f->conn.proto_minor >= 16 &&
1781 !(flags & FUSE_IOCTL_32BIT)) {
1782 req->ioctl_64bit = 1;
1785 if (req->f->op.ioctl)
1786 req->f->op.ioctl(req, nodeid, arg->cmd,
1787 (void *)(uintptr_t)arg->arg, &fi, flags,
1788 in_buf, arg->in_size, arg->out_size);
1789 else
1790 fuse_reply_err(req, ENOSYS);
1793 void fuse_pollhandle_destroy(struct fuse_pollhandle *ph)
1795 free(ph);
1798 static void do_poll(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1800 struct fuse_poll_in *arg = (struct fuse_poll_in *) inarg;
1801 struct fuse_file_info fi;
1803 memset(&fi, 0, sizeof(fi));
1804 fi.fh = arg->fh;
1805 fi.poll_events = arg->events;
1807 if (req->f->op.poll) {
1808 struct fuse_pollhandle *ph = NULL;
1810 if (arg->flags & FUSE_POLL_SCHEDULE_NOTIFY) {
1811 ph = malloc(sizeof(struct fuse_pollhandle));
1812 if (ph == NULL) {
1813 fuse_reply_err(req, ENOMEM);
1814 return;
1816 ph->kh = arg->kh;
1817 ph->ch = req->ch;
1818 ph->f = req->f;
1821 req->f->op.poll(req, nodeid, &fi, ph);
1822 } else {
1823 fuse_reply_err(req, ENOSYS);
1827 static void do_fallocate(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1829 struct fuse_fallocate_in *arg = (struct fuse_fallocate_in *) inarg;
1830 struct fuse_file_info fi;
1832 memset(&fi, 0, sizeof(fi));
1833 fi.fh = arg->fh;
1835 if (req->f->op.fallocate)
1836 req->f->op.fallocate(req, nodeid, arg->mode, arg->offset, arg->length, &fi);
1837 else
1838 fuse_reply_err(req, ENOSYS);
1841 static void do_init(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1843 struct fuse_init_in *arg = (struct fuse_init_in *) inarg;
1844 struct fuse_init_out outarg;
1845 struct fuse_ll *f = req->f;
1846 size_t bufsize = f->bufsize;
1847 size_t outargsize = sizeof(outarg);
1849 (void) nodeid;
1850 if (f->debug) {
1851 fprintf(stderr, "INIT: %u.%u\n", arg->major, arg->minor);
1852 if (arg->major == 7 && arg->minor >= 6) {
1853 fprintf(stderr, "flags=0x%08x\n", arg->flags);
1854 fprintf(stderr, "max_readahead=0x%08x\n",
1855 arg->max_readahead);
1858 f->conn.proto_major = arg->major;
1859 f->conn.proto_minor = arg->minor;
1860 f->conn.capable = 0;
1861 f->conn.want = 0;
1863 memset(&outarg, 0, sizeof(outarg));
1864 outarg.major = FUSE_KERNEL_VERSION;
1865 outarg.minor = FUSE_KERNEL_MINOR_VERSION;
1867 if (arg->major < 7) {
1868 fprintf(stderr, "fuse: unsupported protocol version: %u.%u\n",
1869 arg->major, arg->minor);
1870 fuse_reply_err(req, EPROTO);
1871 return;
1874 if (arg->major > 7) {
1875 /* Wait for a second INIT request with a 7.X version */
1876 send_reply_ok(req, &outarg, sizeof(outarg));
1877 return;
1880 if (arg->minor >= 6) {
1881 if (f->conn.async_read)
1882 f->conn.async_read = arg->flags & FUSE_ASYNC_READ;
1883 if (arg->max_readahead < f->conn.max_readahead)
1884 f->conn.max_readahead = arg->max_readahead;
1885 if (arg->flags & FUSE_ASYNC_READ)
1886 f->conn.capable |= FUSE_CAP_ASYNC_READ;
1887 if (arg->flags & FUSE_POSIX_LOCKS)
1888 f->conn.capable |= FUSE_CAP_POSIX_LOCKS;
1889 if (arg->flags & FUSE_ATOMIC_O_TRUNC)
1890 f->conn.capable |= FUSE_CAP_ATOMIC_O_TRUNC;
1891 if (arg->flags & FUSE_EXPORT_SUPPORT)
1892 f->conn.capable |= FUSE_CAP_EXPORT_SUPPORT;
1893 if (arg->flags & FUSE_BIG_WRITES)
1894 f->conn.capable |= FUSE_CAP_BIG_WRITES;
1895 if (arg->flags & FUSE_DONT_MASK)
1896 f->conn.capable |= FUSE_CAP_DONT_MASK;
1897 if (arg->flags & FUSE_FLOCK_LOCKS)
1898 f->conn.capable |= FUSE_CAP_FLOCK_LOCKS;
1899 if (arg->flags & FUSE_AUTO_INVAL_DATA)
1900 f->conn.capable |= FUSE_CAP_AUTO_INVAL_DATA;
1901 if (arg->flags & FUSE_DO_READDIRPLUS)
1902 f->conn.capable |= FUSE_CAP_READDIRPLUS;
1903 if (arg->flags & FUSE_READDIRPLUS_AUTO)
1904 f->conn.capable |= FUSE_CAP_READDIRPLUS_AUTO;
1905 if (arg->flags & FUSE_ASYNC_DIO)
1906 f->conn.capable |= FUSE_CAP_ASYNC_DIO;
1907 if (arg->flags & FUSE_WRITEBACK_CACHE)
1908 f->conn.capable |= FUSE_CAP_WRITEBACK_CACHE;
1909 } else {
1910 f->conn.async_read = 0;
1911 f->conn.max_readahead = 0;
1914 if (req->f->conn.proto_minor >= 14) {
1915 #ifdef HAVE_SPLICE
1916 #ifdef HAVE_VMSPLICE
1917 f->conn.capable |= FUSE_CAP_SPLICE_WRITE | FUSE_CAP_SPLICE_MOVE;
1918 if (f->splice_write)
1919 f->conn.want |= FUSE_CAP_SPLICE_WRITE;
1920 if (f->splice_move)
1921 f->conn.want |= FUSE_CAP_SPLICE_MOVE;
1922 #endif
1923 f->conn.capable |= FUSE_CAP_SPLICE_READ;
1924 if (f->splice_read)
1925 f->conn.want |= FUSE_CAP_SPLICE_READ;
1926 #endif
1928 if (req->f->conn.proto_minor >= 18)
1929 f->conn.capable |= FUSE_CAP_IOCTL_DIR;
1931 if (f->atomic_o_trunc)
1932 f->conn.want |= FUSE_CAP_ATOMIC_O_TRUNC;
1933 if (f->op.getlk && f->op.setlk && !f->no_remote_posix_lock)
1934 f->conn.want |= FUSE_CAP_POSIX_LOCKS;
1935 if (f->op.flock && !f->no_remote_flock)
1936 f->conn.want |= FUSE_CAP_FLOCK_LOCKS;
1937 if (f->big_writes)
1938 f->conn.want |= FUSE_CAP_BIG_WRITES;
1939 if (f->auto_inval_data)
1940 f->conn.want |= FUSE_CAP_AUTO_INVAL_DATA;
1941 if (f->op.readdirplus && !f->no_readdirplus) {
1942 f->conn.want |= FUSE_CAP_READDIRPLUS;
1943 if (!f->no_readdirplus_auto)
1944 f->conn.want |= FUSE_CAP_READDIRPLUS_AUTO;
1946 if (f->async_dio)
1947 f->conn.want |= FUSE_CAP_ASYNC_DIO;
1948 if (f->writeback_cache)
1949 f->conn.want |= FUSE_CAP_WRITEBACK_CACHE;
1951 if (bufsize < FUSE_MIN_READ_BUFFER) {
1952 fprintf(stderr, "fuse: warning: buffer size too small: %zu\n",
1953 bufsize);
1954 bufsize = FUSE_MIN_READ_BUFFER;
1957 bufsize -= 4096;
1958 if (bufsize < f->conn.max_write)
1959 f->conn.max_write = bufsize;
1961 f->got_init = 1;
1962 if (f->op.init)
1963 f->op.init(f->userdata, &f->conn);
1965 if (f->no_splice_read)
1966 f->conn.want &= ~FUSE_CAP_SPLICE_READ;
1967 if (f->no_splice_write)
1968 f->conn.want &= ~FUSE_CAP_SPLICE_WRITE;
1969 if (f->no_splice_move)
1970 f->conn.want &= ~FUSE_CAP_SPLICE_MOVE;
1971 if (f->no_auto_inval_data)
1972 f->conn.want &= ~FUSE_CAP_AUTO_INVAL_DATA;
1973 if (f->no_readdirplus)
1974 f->conn.want &= ~FUSE_CAP_READDIRPLUS;
1975 if (f->no_readdirplus_auto)
1976 f->conn.want &= ~FUSE_CAP_READDIRPLUS_AUTO;
1977 if (f->no_async_dio)
1978 f->conn.want &= ~FUSE_CAP_ASYNC_DIO;
1979 if (f->no_writeback_cache)
1980 f->conn.want &= ~FUSE_CAP_WRITEBACK_CACHE;
1982 if (f->conn.async_read || (f->conn.want & FUSE_CAP_ASYNC_READ))
1983 outarg.flags |= FUSE_ASYNC_READ;
1984 if (f->conn.want & FUSE_CAP_POSIX_LOCKS)
1985 outarg.flags |= FUSE_POSIX_LOCKS;
1986 if (f->conn.want & FUSE_CAP_ATOMIC_O_TRUNC)
1987 outarg.flags |= FUSE_ATOMIC_O_TRUNC;
1988 if (f->conn.want & FUSE_CAP_EXPORT_SUPPORT)
1989 outarg.flags |= FUSE_EXPORT_SUPPORT;
1990 if (f->conn.want & FUSE_CAP_BIG_WRITES)
1991 outarg.flags |= FUSE_BIG_WRITES;
1992 if (f->conn.want & FUSE_CAP_DONT_MASK)
1993 outarg.flags |= FUSE_DONT_MASK;
1994 if (f->conn.want & FUSE_CAP_FLOCK_LOCKS)
1995 outarg.flags |= FUSE_FLOCK_LOCKS;
1996 if (f->conn.want & FUSE_CAP_AUTO_INVAL_DATA)
1997 outarg.flags |= FUSE_AUTO_INVAL_DATA;
1998 if (f->conn.want & FUSE_CAP_READDIRPLUS)
1999 outarg.flags |= FUSE_DO_READDIRPLUS;
2000 if (f->conn.want & FUSE_CAP_READDIRPLUS_AUTO)
2001 outarg.flags |= FUSE_READDIRPLUS_AUTO;
2002 if (f->conn.want & FUSE_CAP_ASYNC_DIO)
2003 outarg.flags |= FUSE_ASYNC_DIO;
2004 if (f->conn.want & FUSE_CAP_WRITEBACK_CACHE)
2005 outarg.flags |= FUSE_WRITEBACK_CACHE;
2006 outarg.max_readahead = f->conn.max_readahead;
2007 outarg.max_write = f->conn.max_write;
2008 if (f->conn.proto_minor >= 13) {
2009 if (f->conn.max_background >= (1 << 16))
2010 f->conn.max_background = (1 << 16) - 1;
2011 if (f->conn.congestion_threshold > f->conn.max_background)
2012 f->conn.congestion_threshold = f->conn.max_background;
2013 if (!f->conn.congestion_threshold) {
2014 f->conn.congestion_threshold =
2015 f->conn.max_background * 3 / 4;
2018 outarg.max_background = f->conn.max_background;
2019 outarg.congestion_threshold = f->conn.congestion_threshold;
2021 if (f->conn.proto_minor >= 23)
2022 outarg.time_gran = f->conn.time_gran;
2024 if (f->debug) {
2025 fprintf(stderr, " INIT: %u.%u\n", outarg.major, outarg.minor);
2026 fprintf(stderr, " flags=0x%08x\n", outarg.flags);
2027 fprintf(stderr, " max_readahead=0x%08x\n",
2028 outarg.max_readahead);
2029 fprintf(stderr, " max_write=0x%08x\n", outarg.max_write);
2030 fprintf(stderr, " max_background=%i\n",
2031 outarg.max_background);
2032 fprintf(stderr, " congestion_threshold=%i\n",
2033 outarg.congestion_threshold);
2034 fprintf(stderr, " time_gran=%u\n",
2035 outarg.time_gran);
2037 if (arg->minor < 5)
2038 outargsize = FUSE_COMPAT_INIT_OUT_SIZE;
2039 else if (arg->minor < 23)
2040 outargsize = FUSE_COMPAT_22_INIT_OUT_SIZE;
2042 send_reply_ok(req, &outarg, outargsize);
2045 static void do_destroy(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
2047 struct fuse_ll *f = req->f;
2049 (void) nodeid;
2050 (void) inarg;
2052 f->got_destroy = 1;
2053 if (f->op.destroy)
2054 f->op.destroy(f->userdata);
2056 send_reply_ok(req, NULL, 0);
2059 static void list_del_nreq(struct fuse_notify_req *nreq)
2061 struct fuse_notify_req *prev = nreq->prev;
2062 struct fuse_notify_req *next = nreq->next;
2063 prev->next = next;
2064 next->prev = prev;
2067 static void list_add_nreq(struct fuse_notify_req *nreq,
2068 struct fuse_notify_req *next)
2070 struct fuse_notify_req *prev = next->prev;
2071 nreq->next = next;
2072 nreq->prev = prev;
2073 prev->next = nreq;
2074 next->prev = nreq;
2077 static void list_init_nreq(struct fuse_notify_req *nreq)
2079 nreq->next = nreq;
2080 nreq->prev = nreq;
2083 static void do_notify_reply(fuse_req_t req, fuse_ino_t nodeid,
2084 const void *inarg, const struct fuse_buf *buf)
2086 struct fuse_ll *f = req->f;
2087 struct fuse_notify_req *nreq;
2088 struct fuse_notify_req *head;
2090 pthread_mutex_lock(&f->lock);
2091 head = &f->notify_list;
2092 for (nreq = head->next; nreq != head; nreq = nreq->next) {
2093 if (nreq->unique == req->unique) {
2094 list_del_nreq(nreq);
2095 break;
2098 pthread_mutex_unlock(&f->lock);
2100 if (nreq != head)
2101 nreq->reply(nreq, req, nodeid, inarg, buf);
2104 static int send_notify_iov(struct fuse_ll *f, struct fuse_chan *ch,
2105 int notify_code, struct iovec *iov, int count)
2107 struct fuse_out_header out;
2109 if (!f->got_init)
2110 return -ENOTCONN;
2112 out.unique = 0;
2113 out.error = notify_code;
2114 iov[0].iov_base = &out;
2115 iov[0].iov_len = sizeof(struct fuse_out_header);
2117 return fuse_send_msg(f, ch, iov, count);
2120 int fuse_lowlevel_notify_poll(struct fuse_pollhandle *ph)
2122 if (ph != NULL) {
2123 struct fuse_notify_poll_wakeup_out outarg;
2124 struct iovec iov[2];
2126 outarg.kh = ph->kh;
2128 iov[1].iov_base = &outarg;
2129 iov[1].iov_len = sizeof(outarg);
2131 return send_notify_iov(ph->f, ph->ch, FUSE_NOTIFY_POLL, iov, 2);
2132 } else {
2133 return 0;
2137 int fuse_lowlevel_notify_inval_inode(struct fuse_chan *ch, fuse_ino_t ino,
2138 off_t off, off_t len)
2140 struct fuse_notify_inval_inode_out outarg;
2141 struct fuse_ll *f;
2142 struct iovec iov[2];
2144 if (!ch)
2145 return -EINVAL;
2147 f = fuse_chan_session(ch)->f;
2148 if (!f)
2149 return -ENODEV;
2151 outarg.ino = ino;
2152 outarg.off = off;
2153 outarg.len = len;
2155 iov[1].iov_base = &outarg;
2156 iov[1].iov_len = sizeof(outarg);
2158 return send_notify_iov(f, ch, FUSE_NOTIFY_INVAL_INODE, iov, 2);
2161 int fuse_lowlevel_notify_inval_entry(struct fuse_chan *ch, fuse_ino_t parent,
2162 const char *name, size_t namelen)
2164 struct fuse_notify_inval_entry_out outarg;
2165 struct fuse_ll *f;
2166 struct iovec iov[3];
2168 if (!ch)
2169 return -EINVAL;
2171 f = fuse_chan_session(ch)->f;
2172 if (!f)
2173 return -ENODEV;
2175 outarg.parent = parent;
2176 outarg.namelen = namelen;
2177 outarg.padding = 0;
2179 iov[1].iov_base = &outarg;
2180 iov[1].iov_len = sizeof(outarg);
2181 iov[2].iov_base = (void *)name;
2182 iov[2].iov_len = namelen + 1;
2184 return send_notify_iov(f, ch, FUSE_NOTIFY_INVAL_ENTRY, iov, 3);
2187 int fuse_lowlevel_notify_delete(struct fuse_chan *ch,
2188 fuse_ino_t parent, fuse_ino_t child,
2189 const char *name, size_t namelen)
2191 struct fuse_notify_delete_out outarg;
2192 struct fuse_ll *f;
2193 struct iovec iov[3];
2195 if (!ch)
2196 return -EINVAL;
2198 f = fuse_chan_session(ch)->f;
2199 if (!f)
2200 return -ENODEV;
2202 if (f->conn.proto_minor < 18)
2203 return -ENOSYS;
2205 outarg.parent = parent;
2206 outarg.child = child;
2207 outarg.namelen = namelen;
2208 outarg.padding = 0;
2210 iov[1].iov_base = &outarg;
2211 iov[1].iov_len = sizeof(outarg);
2212 iov[2].iov_base = (void *)name;
2213 iov[2].iov_len = namelen + 1;
2215 return send_notify_iov(f, ch, FUSE_NOTIFY_DELETE, iov, 3);
2218 int fuse_lowlevel_notify_store(struct fuse_chan *ch, fuse_ino_t ino,
2219 off_t offset, struct fuse_bufvec *bufv,
2220 enum fuse_buf_copy_flags flags)
2222 struct fuse_out_header out;
2223 struct fuse_notify_store_out outarg;
2224 struct fuse_ll *f;
2225 struct iovec iov[3];
2226 size_t size = fuse_buf_size(bufv);
2227 int res;
2229 if (!ch)
2230 return -EINVAL;
2232 f = fuse_chan_session(ch)->f;
2233 if (!f)
2234 return -ENODEV;
2236 if (f->conn.proto_minor < 15)
2237 return -ENOSYS;
2239 out.unique = 0;
2240 out.error = FUSE_NOTIFY_STORE;
2242 outarg.nodeid = ino;
2243 outarg.offset = offset;
2244 outarg.size = size;
2246 iov[0].iov_base = &out;
2247 iov[0].iov_len = sizeof(out);
2248 iov[1].iov_base = &outarg;
2249 iov[1].iov_len = sizeof(outarg);
2251 res = fuse_send_data_iov(f, ch, iov, 2, bufv, flags);
2252 if (res > 0)
2253 res = -res;
2255 return res;
2258 struct fuse_retrieve_req {
2259 struct fuse_notify_req nreq;
2260 void *cookie;
2263 static void fuse_ll_retrieve_reply(struct fuse_notify_req *nreq,
2264 fuse_req_t req, fuse_ino_t ino,
2265 const void *inarg,
2266 const struct fuse_buf *ibuf)
2268 struct fuse_ll *f = req->f;
2269 struct fuse_retrieve_req *rreq =
2270 container_of(nreq, struct fuse_retrieve_req, nreq);
2271 const struct fuse_notify_retrieve_in *arg = inarg;
2272 struct fuse_bufvec bufv = {
2273 .buf[0] = *ibuf,
2274 .count = 1,
2277 if (!(bufv.buf[0].flags & FUSE_BUF_IS_FD))
2278 bufv.buf[0].mem = PARAM(arg);
2280 bufv.buf[0].size -= sizeof(struct fuse_in_header) +
2281 sizeof(struct fuse_notify_retrieve_in);
2283 if (bufv.buf[0].size < arg->size) {
2284 fprintf(stderr, "fuse: retrieve reply: buffer size too small\n");
2285 fuse_reply_none(req);
2286 goto out;
2288 bufv.buf[0].size = arg->size;
2290 if (req->f->op.retrieve_reply) {
2291 req->f->op.retrieve_reply(req, rreq->cookie, ino,
2292 arg->offset, &bufv);
2293 } else {
2294 fuse_reply_none(req);
2296 out:
2297 free(rreq);
2298 if ((ibuf->flags & FUSE_BUF_IS_FD) && bufv.idx < bufv.count)
2299 fuse_ll_clear_pipe(f);
2302 int fuse_lowlevel_notify_retrieve(struct fuse_chan *ch, fuse_ino_t ino,
2303 size_t size, off_t offset, void *cookie)
2305 struct fuse_notify_retrieve_out outarg;
2306 struct fuse_ll *f;
2307 struct iovec iov[2];
2308 struct fuse_retrieve_req *rreq;
2309 int err;
2311 if (!ch)
2312 return -EINVAL;
2314 f = fuse_chan_session(ch)->f;
2315 if (!f)
2316 return -ENODEV;
2318 if (f->conn.proto_minor < 15)
2319 return -ENOSYS;
2321 rreq = malloc(sizeof(*rreq));
2322 if (rreq == NULL)
2323 return -ENOMEM;
2325 pthread_mutex_lock(&f->lock);
2326 rreq->cookie = cookie;
2327 rreq->nreq.unique = f->notify_ctr++;
2328 rreq->nreq.reply = fuse_ll_retrieve_reply;
2329 list_add_nreq(&rreq->nreq, &f->notify_list);
2330 pthread_mutex_unlock(&f->lock);
2332 outarg.notify_unique = rreq->nreq.unique;
2333 outarg.nodeid = ino;
2334 outarg.offset = offset;
2335 outarg.size = size;
2337 iov[1].iov_base = &outarg;
2338 iov[1].iov_len = sizeof(outarg);
2340 err = send_notify_iov(f, ch, FUSE_NOTIFY_RETRIEVE, iov, 2);
2341 if (err) {
2342 pthread_mutex_lock(&f->lock);
2343 list_del_nreq(&rreq->nreq);
2344 pthread_mutex_unlock(&f->lock);
2345 free(rreq);
2348 return err;
2351 void *fuse_req_userdata(fuse_req_t req)
2353 return req->f->userdata;
2356 const struct fuse_ctx *fuse_req_ctx(fuse_req_t req)
2358 return &req->ctx;
2361 void fuse_req_interrupt_func(fuse_req_t req, fuse_interrupt_func_t func,
2362 void *data)
2364 pthread_mutex_lock(&req->lock);
2365 pthread_mutex_lock(&req->f->lock);
2366 req->u.ni.func = func;
2367 req->u.ni.data = data;
2368 pthread_mutex_unlock(&req->f->lock);
2369 if (req->interrupted && func)
2370 func(req, data);
2371 pthread_mutex_unlock(&req->lock);
2374 int fuse_req_interrupted(fuse_req_t req)
2376 int interrupted;
2378 pthread_mutex_lock(&req->f->lock);
2379 interrupted = req->interrupted;
2380 pthread_mutex_unlock(&req->f->lock);
2382 return interrupted;
2385 static struct {
2386 void (*func)(fuse_req_t, fuse_ino_t, const void *);
2387 const char *name;
2388 } fuse_ll_ops[] = {
2389 [FUSE_LOOKUP] = { do_lookup, "LOOKUP" },
2390 [FUSE_FORGET] = { do_forget, "FORGET" },
2391 [FUSE_GETATTR] = { do_getattr, "GETATTR" },
2392 [FUSE_SETATTR] = { do_setattr, "SETATTR" },
2393 [FUSE_READLINK] = { do_readlink, "READLINK" },
2394 [FUSE_SYMLINK] = { do_symlink, "SYMLINK" },
2395 [FUSE_MKNOD] = { do_mknod, "MKNOD" },
2396 [FUSE_MKDIR] = { do_mkdir, "MKDIR" },
2397 [FUSE_UNLINK] = { do_unlink, "UNLINK" },
2398 [FUSE_RMDIR] = { do_rmdir, "RMDIR" },
2399 [FUSE_RENAME] = { do_rename, "RENAME" },
2400 [FUSE_LINK] = { do_link, "LINK" },
2401 [FUSE_OPEN] = { do_open, "OPEN" },
2402 [FUSE_READ] = { do_read, "READ" },
2403 [FUSE_WRITE] = { do_write, "WRITE" },
2404 [FUSE_STATFS] = { do_statfs, "STATFS" },
2405 [FUSE_RELEASE] = { do_release, "RELEASE" },
2406 [FUSE_FSYNC] = { do_fsync, "FSYNC" },
2407 [FUSE_SETXATTR] = { do_setxattr, "SETXATTR" },
2408 [FUSE_GETXATTR] = { do_getxattr, "GETXATTR" },
2409 [FUSE_LISTXATTR] = { do_listxattr, "LISTXATTR" },
2410 [FUSE_REMOVEXATTR] = { do_removexattr, "REMOVEXATTR" },
2411 [FUSE_FLUSH] = { do_flush, "FLUSH" },
2412 [FUSE_INIT] = { do_init, "INIT" },
2413 [FUSE_OPENDIR] = { do_opendir, "OPENDIR" },
2414 [FUSE_READDIR] = { do_readdir, "READDIR" },
2415 [FUSE_RELEASEDIR] = { do_releasedir, "RELEASEDIR" },
2416 [FUSE_FSYNCDIR] = { do_fsyncdir, "FSYNCDIR" },
2417 [FUSE_GETLK] = { do_getlk, "GETLK" },
2418 [FUSE_SETLK] = { do_setlk, "SETLK" },
2419 [FUSE_SETLKW] = { do_setlkw, "SETLKW" },
2420 [FUSE_ACCESS] = { do_access, "ACCESS" },
2421 [FUSE_CREATE] = { do_create, "CREATE" },
2422 [FUSE_INTERRUPT] = { do_interrupt, "INTERRUPT" },
2423 [FUSE_BMAP] = { do_bmap, "BMAP" },
2424 [FUSE_IOCTL] = { do_ioctl, "IOCTL" },
2425 [FUSE_POLL] = { do_poll, "POLL" },
2426 [FUSE_FALLOCATE] = { do_fallocate, "FALLOCATE" },
2427 [FUSE_DESTROY] = { do_destroy, "DESTROY" },
2428 [FUSE_NOTIFY_REPLY] = { (void *) 1, "NOTIFY_REPLY" },
2429 [FUSE_BATCH_FORGET] = { do_batch_forget, "BATCH_FORGET" },
2430 [FUSE_READDIRPLUS] = { do_readdirplus, "READDIRPLUS"},
2431 [CUSE_INIT] = { cuse_lowlevel_init, "CUSE_INIT" },
2434 #define FUSE_MAXOP (sizeof(fuse_ll_ops) / sizeof(fuse_ll_ops[0]))
2436 static const char *opname(enum fuse_opcode opcode)
2438 if (opcode >= FUSE_MAXOP || !fuse_ll_ops[opcode].name)
2439 return "???";
2440 else
2441 return fuse_ll_ops[opcode].name;
2444 static int fuse_ll_copy_from_pipe(struct fuse_bufvec *dst,
2445 struct fuse_bufvec *src)
2447 int res = fuse_buf_copy(dst, src, 0);
2448 if (res < 0) {
2449 fprintf(stderr, "fuse: copy from pipe: %s\n", strerror(-res));
2450 return res;
2452 if (res < fuse_buf_size(dst)) {
2453 fprintf(stderr, "fuse: copy from pipe: short read\n");
2454 return -1;
2456 return 0;
2459 void fuse_session_process_buf(struct fuse_session *se,
2460 const struct fuse_buf *buf, struct fuse_chan *ch)
2462 struct fuse_ll *f = se->f;
2463 const size_t write_header_size = sizeof(struct fuse_in_header) +
2464 sizeof(struct fuse_write_in);
2465 struct fuse_bufvec bufv = { .buf[0] = *buf, .count = 1 };
2466 struct fuse_bufvec tmpbuf = FUSE_BUFVEC_INIT(write_header_size);
2467 struct fuse_in_header *in;
2468 const void *inarg;
2469 struct fuse_req *req;
2470 void *mbuf = NULL;
2471 int err;
2472 int res;
2474 if (buf->flags & FUSE_BUF_IS_FD) {
2475 if (buf->size < tmpbuf.buf[0].size)
2476 tmpbuf.buf[0].size = buf->size;
2478 mbuf = malloc(tmpbuf.buf[0].size);
2479 if (mbuf == NULL) {
2480 fprintf(stderr, "fuse: failed to allocate header\n");
2481 goto clear_pipe;
2483 tmpbuf.buf[0].mem = mbuf;
2485 res = fuse_ll_copy_from_pipe(&tmpbuf, &bufv);
2486 if (res < 0)
2487 goto clear_pipe;
2489 in = mbuf;
2490 } else {
2491 in = buf->mem;
2494 if (f->debug) {
2495 fprintf(stderr,
2496 "unique: %llu, opcode: %s (%i), nodeid: %llu, insize: %zu, pid: %u\n",
2497 (unsigned long long) in->unique,
2498 opname((enum fuse_opcode) in->opcode), in->opcode,
2499 (unsigned long long) in->nodeid, buf->size, in->pid);
2502 req = fuse_ll_alloc_req(f);
2503 if (req == NULL) {
2504 struct fuse_out_header out = {
2505 .unique = in->unique,
2506 .error = -ENOMEM,
2508 struct iovec iov = {
2509 .iov_base = &out,
2510 .iov_len = sizeof(struct fuse_out_header),
2513 fuse_send_msg(f, ch, &iov, 1);
2514 goto clear_pipe;
2517 req->unique = in->unique;
2518 req->ctx.uid = in->uid;
2519 req->ctx.gid = in->gid;
2520 req->ctx.pid = in->pid;
2521 req->ch = ch;
2523 err = EIO;
2524 if (!f->got_init) {
2525 enum fuse_opcode expected;
2527 expected = f->cuse_data ? CUSE_INIT : FUSE_INIT;
2528 if (in->opcode != expected)
2529 goto reply_err;
2530 } else if (in->opcode == FUSE_INIT || in->opcode == CUSE_INIT)
2531 goto reply_err;
2533 err = EACCES;
2534 if (f->allow_root && in->uid != f->owner && in->uid != 0 &&
2535 in->opcode != FUSE_INIT && in->opcode != FUSE_READ &&
2536 in->opcode != FUSE_WRITE && in->opcode != FUSE_FSYNC &&
2537 in->opcode != FUSE_RELEASE && in->opcode != FUSE_READDIR &&
2538 in->opcode != FUSE_FSYNCDIR && in->opcode != FUSE_RELEASEDIR &&
2539 in->opcode != FUSE_NOTIFY_REPLY &&
2540 in->opcode != FUSE_READDIRPLUS)
2541 goto reply_err;
2543 err = ENOSYS;
2544 if (in->opcode >= FUSE_MAXOP || !fuse_ll_ops[in->opcode].func)
2545 goto reply_err;
2546 if (in->opcode != FUSE_INTERRUPT) {
2547 struct fuse_req *intr;
2548 pthread_mutex_lock(&f->lock);
2549 intr = check_interrupt(f, req);
2550 list_add_req(req, &f->list);
2551 pthread_mutex_unlock(&f->lock);
2552 if (intr)
2553 fuse_reply_err(intr, EAGAIN);
2556 if ((buf->flags & FUSE_BUF_IS_FD) && write_header_size < buf->size &&
2557 (in->opcode != FUSE_WRITE || !f->op.write_buf) &&
2558 in->opcode != FUSE_NOTIFY_REPLY) {
2559 void *newmbuf;
2561 err = ENOMEM;
2562 newmbuf = realloc(mbuf, buf->size);
2563 if (newmbuf == NULL)
2564 goto reply_err;
2565 mbuf = newmbuf;
2567 tmpbuf = FUSE_BUFVEC_INIT(buf->size - write_header_size);
2568 tmpbuf.buf[0].mem = mbuf + write_header_size;
2570 res = fuse_ll_copy_from_pipe(&tmpbuf, &bufv);
2571 err = -res;
2572 if (res < 0)
2573 goto reply_err;
2575 in = mbuf;
2578 inarg = (void *) &in[1];
2579 if (in->opcode == FUSE_WRITE && f->op.write_buf)
2580 do_write_buf(req, in->nodeid, inarg, buf);
2581 else if (in->opcode == FUSE_NOTIFY_REPLY)
2582 do_notify_reply(req, in->nodeid, inarg, buf);
2583 else
2584 fuse_ll_ops[in->opcode].func(req, in->nodeid, inarg);
2586 out_free:
2587 free(mbuf);
2588 return;
2590 reply_err:
2591 fuse_reply_err(req, err);
2592 clear_pipe:
2593 if (buf->flags & FUSE_BUF_IS_FD)
2594 fuse_ll_clear_pipe(f);
2595 goto out_free;
2598 enum {
2599 KEY_HELP,
2600 KEY_VERSION,
2603 static const struct fuse_opt fuse_ll_opts[] = {
2604 { "debug", offsetof(struct fuse_ll, debug), 1 },
2605 { "-d", offsetof(struct fuse_ll, debug), 1 },
2606 { "allow_root", offsetof(struct fuse_ll, allow_root), 1 },
2607 { "max_write=%u", offsetof(struct fuse_ll, conn.max_write), 0 },
2608 { "max_readahead=%u", offsetof(struct fuse_ll, conn.max_readahead), 0 },
2609 { "max_background=%u", offsetof(struct fuse_ll, conn.max_background), 0 },
2610 { "congestion_threshold=%u",
2611 offsetof(struct fuse_ll, conn.congestion_threshold), 0 },
2612 { "async_read", offsetof(struct fuse_ll, conn.async_read), 1 },
2613 { "sync_read", offsetof(struct fuse_ll, conn.async_read), 0 },
2614 { "atomic_o_trunc", offsetof(struct fuse_ll, atomic_o_trunc), 1},
2615 { "no_remote_lock", offsetof(struct fuse_ll, no_remote_posix_lock), 1},
2616 { "no_remote_lock", offsetof(struct fuse_ll, no_remote_flock), 1},
2617 { "no_remote_flock", offsetof(struct fuse_ll, no_remote_flock), 1},
2618 { "no_remote_posix_lock", offsetof(struct fuse_ll, no_remote_posix_lock), 1},
2619 { "big_writes", offsetof(struct fuse_ll, big_writes), 1},
2620 { "splice_write", offsetof(struct fuse_ll, splice_write), 1},
2621 { "no_splice_write", offsetof(struct fuse_ll, no_splice_write), 1},
2622 { "splice_move", offsetof(struct fuse_ll, splice_move), 1},
2623 { "no_splice_move", offsetof(struct fuse_ll, no_splice_move), 1},
2624 { "splice_read", offsetof(struct fuse_ll, splice_read), 1},
2625 { "no_splice_read", offsetof(struct fuse_ll, no_splice_read), 1},
2626 { "auto_inval_data", offsetof(struct fuse_ll, auto_inval_data), 1},
2627 { "no_auto_inval_data", offsetof(struct fuse_ll, no_auto_inval_data), 1},
2628 { "readdirplus=no", offsetof(struct fuse_ll, no_readdirplus), 1},
2629 { "readdirplus=yes", offsetof(struct fuse_ll, no_readdirplus), 0},
2630 { "readdirplus=yes", offsetof(struct fuse_ll, no_readdirplus_auto), 1},
2631 { "readdirplus=auto", offsetof(struct fuse_ll, no_readdirplus), 0},
2632 { "readdirplus=auto", offsetof(struct fuse_ll, no_readdirplus_auto), 0},
2633 { "async_dio", offsetof(struct fuse_ll, async_dio), 1},
2634 { "no_async_dio", offsetof(struct fuse_ll, no_async_dio), 1},
2635 { "writeback_cache", offsetof(struct fuse_ll, writeback_cache), 1},
2636 { "no_writeback_cache", offsetof(struct fuse_ll, no_writeback_cache), 1},
2637 { "time_gran=%u", offsetof(struct fuse_ll, conn.time_gran), 0 },
2638 FUSE_OPT_KEY("max_read=", FUSE_OPT_KEY_DISCARD),
2639 FUSE_OPT_KEY("-h", KEY_HELP),
2640 FUSE_OPT_KEY("--help", KEY_HELP),
2641 FUSE_OPT_KEY("-V", KEY_VERSION),
2642 FUSE_OPT_KEY("--version", KEY_VERSION),
2643 FUSE_OPT_END
2646 static void fuse_ll_version(void)
2648 printf("using FUSE kernel interface version %i.%i\n",
2649 FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
2652 static void fuse_ll_help(void)
2654 printf(
2655 " -o max_write=N set maximum size of write requests\n"
2656 " -o max_readahead=N set maximum readahead\n"
2657 " -o max_background=N set number of maximum background requests\n"
2658 " -o congestion_threshold=N set kernel's congestion threshold\n"
2659 " -o async_read perform reads asynchronously (default)\n"
2660 " -o sync_read perform reads synchronously\n"
2661 " -o atomic_o_trunc enable atomic open+truncate support\n"
2662 " -o big_writes enable larger than 4kB writes\n"
2663 " -o no_remote_lock disable remote file locking\n"
2664 " -o no_remote_flock disable remote file locking (BSD)\n"
2665 " -o no_remote_posix_lock disable remove file locking (POSIX)\n"
2666 " -o [no_]splice_write use splice to write to the fuse device\n"
2667 " -o [no_]splice_move move data while splicing to the fuse device\n"
2668 " -o [no_]splice_read use splice to read from the fuse device\n"
2669 " -o [no_]auto_inval_data use automatic kernel cache invalidation logic\n"
2670 " -o readdirplus=S control readdirplus use (yes|no|auto)\n"
2671 " -o [no_]async_dio asynchronous direct I/O\n"
2672 " -o [no_]writeback_cache asynchronous, buffered writes\n"
2673 " -o time_gran=N time granularity in nsec\n"
2677 static int fuse_ll_opt_proc(void *data, const char *arg, int key,
2678 struct fuse_args *outargs)
2680 (void) data; (void) outargs;
2682 switch (key) {
2683 case KEY_HELP:
2684 fuse_ll_help();
2685 break;
2687 case KEY_VERSION:
2688 fuse_ll_version();
2689 break;
2691 default:
2692 fprintf(stderr, "fuse: unknown option `%s'\n", arg);
2695 return -1;
2698 static void fuse_ll_destroy(struct fuse_ll *f)
2700 struct fuse_ll_pipe *llp;
2702 if (f->got_init && !f->got_destroy) {
2703 if (f->op.destroy)
2704 f->op.destroy(f->userdata);
2706 llp = pthread_getspecific(f->pipe_key);
2707 if (llp != NULL)
2708 fuse_ll_pipe_free(llp);
2709 pthread_key_delete(f->pipe_key);
2710 pthread_mutex_destroy(&f->lock);
2711 free(f->cuse_data);
2712 free(f);
2715 void fuse_session_destroy(struct fuse_session *se)
2717 fuse_ll_destroy(se->f);
2718 if (se->ch != NULL)
2719 fuse_chan_destroy(se->ch);
2720 free(se);
2724 static void fuse_ll_pipe_destructor(void *data)
2726 struct fuse_ll_pipe *llp = data;
2727 fuse_ll_pipe_free(llp);
2730 #ifdef HAVE_SPLICE
2731 int fuse_session_receive_buf(struct fuse_session *se, struct fuse_buf *buf,
2732 struct fuse_chan *ch)
2734 struct fuse_ll *f = se->f;
2735 size_t bufsize = f->bufsize;
2736 struct fuse_ll_pipe *llp;
2737 struct fuse_buf tmpbuf;
2738 int err;
2739 int res;
2741 if (f->conn.proto_minor < 14 || !(f->conn.want & FUSE_CAP_SPLICE_READ))
2742 goto fallback;
2744 llp = fuse_ll_get_pipe(f);
2745 if (llp == NULL)
2746 goto fallback;
2748 if (llp->size < bufsize) {
2749 if (llp->can_grow) {
2750 res = fcntl(llp->pipe[0], F_SETPIPE_SZ, bufsize);
2751 if (res == -1) {
2752 llp->can_grow = 0;
2753 goto fallback;
2755 llp->size = res;
2757 if (llp->size < bufsize)
2758 goto fallback;
2761 res = splice(fuse_chan_fd(ch), NULL, llp->pipe[1], NULL, bufsize, 0);
2762 err = errno;
2764 if (fuse_session_exited(se))
2765 return 0;
2767 if (res == -1) {
2768 if (err == ENODEV) {
2769 fuse_session_exit(se);
2770 return 0;
2772 if (err != EINTR && err != EAGAIN)
2773 perror("fuse: splice from device");
2774 return -err;
2777 if (res < sizeof(struct fuse_in_header)) {
2778 fprintf(stderr, "short splice from fuse device\n");
2779 return -EIO;
2782 tmpbuf = (struct fuse_buf) {
2783 .size = res,
2784 .flags = FUSE_BUF_IS_FD,
2785 .fd = llp->pipe[0],
2789 * Don't bother with zero copy for small requests.
2790 * fuse_loop_mt() needs to check for FORGET so this more than
2791 * just an optimization.
2793 if (res < sizeof(struct fuse_in_header) +
2794 sizeof(struct fuse_write_in) + pagesize) {
2795 struct fuse_bufvec src = { .buf[0] = tmpbuf, .count = 1 };
2796 struct fuse_bufvec dst = { .count = 1 };
2798 if (!buf->mem) {
2799 buf->mem = malloc(f->bufsize);
2800 if (!buf->mem) {
2801 fprintf(stderr,
2802 "fuse: failed to allocate read buffer\n");
2803 return -ENOMEM;
2806 buf->size = f->bufsize;
2807 buf->flags = 0;
2808 dst.buf[0] = *buf;
2810 res = fuse_buf_copy(&dst, &src, 0);
2811 if (res < 0) {
2812 fprintf(stderr, "fuse: copy from pipe: %s\n",
2813 strerror(-res));
2814 fuse_ll_clear_pipe(f);
2815 return res;
2817 if (res < tmpbuf.size) {
2818 fprintf(stderr, "fuse: copy from pipe: short read\n");
2819 fuse_ll_clear_pipe(f);
2820 return -EIO;
2822 assert(res == tmpbuf.size);
2824 } else {
2825 /* Don't overwrite buf->mem, as that would cause a leak */
2826 buf->fd = tmpbuf.fd;
2827 buf->flags = tmpbuf.flags;
2829 buf->size = tmpbuf.size;
2831 return res;
2833 fallback:
2834 return fuse_chan_recv(se, buf, ch);
2836 #else
2837 int fuse_session_receive_buf(struct fuse_session *se, struct fuse_buf *buf,
2838 struct fuse_chan *ch)
2840 return fuse_chan_recv(se, buf, ch);
2842 #endif
2844 #define MIN_BUFSIZE 0x21000
2846 struct fuse_session *fuse_lowlevel_new(struct fuse_args *args,
2847 const struct fuse_lowlevel_ops *op,
2848 size_t op_size, void *userdata)
2850 int err;
2851 struct fuse_ll *f;
2852 struct fuse_session *se;
2854 if (sizeof(struct fuse_lowlevel_ops) < op_size) {
2855 fprintf(stderr, "fuse: warning: library too old, some operations may not work\n");
2856 op_size = sizeof(struct fuse_lowlevel_ops);
2859 f = (struct fuse_ll *) calloc(1, sizeof(struct fuse_ll));
2860 if (f == NULL) {
2861 fprintf(stderr, "fuse: failed to allocate fuse object\n");
2862 goto out;
2865 f->conn.async_read = 1;
2866 f->conn.max_write = UINT_MAX;
2867 f->conn.max_readahead = UINT_MAX;
2868 f->atomic_o_trunc = 0;
2869 f->bufsize = getpagesize() + 0x1000;
2870 f->bufsize = f->bufsize < MIN_BUFSIZE ? MIN_BUFSIZE : f->bufsize;
2872 list_init_req(&f->list);
2873 list_init_req(&f->interrupts);
2874 list_init_nreq(&f->notify_list);
2875 f->notify_ctr = 1;
2876 fuse_mutex_init(&f->lock);
2878 err = pthread_key_create(&f->pipe_key, fuse_ll_pipe_destructor);
2879 if (err) {
2880 fprintf(stderr, "fuse: failed to create thread specific key: %s\n",
2881 strerror(err));
2882 goto out_free;
2885 if (fuse_opt_parse(args, f, fuse_ll_opts, fuse_ll_opt_proc) == -1)
2886 goto out_key_destroy;
2888 if (f->debug)
2889 fprintf(stderr, "FUSE library version: %s\n", PACKAGE_VERSION);
2891 memcpy(&f->op, op, op_size);
2892 f->owner = getuid();
2893 f->userdata = userdata;
2895 se = fuse_session_new();
2896 if (!se)
2897 goto out_key_destroy;
2899 se->f = f;
2901 return se;
2903 out_key_destroy:
2904 pthread_key_delete(f->pipe_key);
2905 out_free:
2906 pthread_mutex_destroy(&f->lock);
2907 free(f);
2908 out:
2909 return NULL;
2912 #ifdef linux
2913 int fuse_req_getgroups(fuse_req_t req, int size, gid_t list[])
2915 char *buf;
2916 size_t bufsize = 1024;
2917 char path[128];
2918 int ret;
2919 int fd;
2920 unsigned long pid = req->ctx.pid;
2921 char *s;
2923 sprintf(path, "/proc/%lu/task/%lu/status", pid, pid);
2925 retry:
2926 buf = malloc(bufsize);
2927 if (buf == NULL)
2928 return -ENOMEM;
2930 ret = -EIO;
2931 fd = open(path, O_RDONLY);
2932 if (fd == -1)
2933 goto out_free;
2935 ret = read(fd, buf, bufsize);
2936 close(fd);
2937 if (ret == -1) {
2938 ret = -EIO;
2939 goto out_free;
2942 if (ret == bufsize) {
2943 free(buf);
2944 bufsize *= 4;
2945 goto retry;
2948 ret = -EIO;
2949 s = strstr(buf, "\nGroups:");
2950 if (s == NULL)
2951 goto out_free;
2953 s += 8;
2954 ret = 0;
2955 while (1) {
2956 char *end;
2957 unsigned long val = strtoul(s, &end, 0);
2958 if (end == s)
2959 break;
2961 s = end;
2962 if (ret < size)
2963 list[ret] = val;
2964 ret++;
2967 out_free:
2968 free(buf);
2969 return ret;
2971 #else /* linux */
2973 * This is currently not implemented on other than Linux...
2975 int fuse_req_getgroups(fuse_req_t req, int size, gid_t list[])
2977 return -ENOSYS;
2979 #endif