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