libfuse: remove session and chan abstractions
[fuse.git] / lib / fuse_lowlevel.c
blob76cb7b7947269d53199d95b4a44203c6a8009b39
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>
26 #ifndef F_LINUX_SPECIFIC_BASE
27 #define F_LINUX_SPECIFIC_BASE 1024
28 #endif
29 #ifndef F_SETPIPE_SZ
30 #define F_SETPIPE_SZ (F_LINUX_SPECIFIC_BASE + 7)
31 #endif
34 #define PARAM(inarg) (((char *)(inarg)) + sizeof(*(inarg)))
35 #define OFFSET_MAX 0x7fffffffffffffffLL
37 #define container_of(ptr, type, member) ({ \
38 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
39 (type *)( (char *)__mptr - offsetof(type,member) );})
41 struct fuse_pollhandle {
42 uint64_t kh;
43 struct fuse_chan *ch;
44 struct fuse_ll *f;
47 static size_t pagesize;
49 static __attribute__((constructor)) void fuse_ll_init_pagesize(void)
51 pagesize = getpagesize();
54 static void convert_stat(const struct stat *stbuf, struct fuse_attr *attr)
56 attr->ino = stbuf->st_ino;
57 attr->mode = stbuf->st_mode;
58 attr->nlink = stbuf->st_nlink;
59 attr->uid = stbuf->st_uid;
60 attr->gid = stbuf->st_gid;
61 attr->rdev = stbuf->st_rdev;
62 attr->size = stbuf->st_size;
63 attr->blksize = stbuf->st_blksize;
64 attr->blocks = stbuf->st_blocks;
65 attr->atime = stbuf->st_atime;
66 attr->mtime = stbuf->st_mtime;
67 attr->ctime = stbuf->st_ctime;
68 attr->atimensec = ST_ATIM_NSEC(stbuf);
69 attr->mtimensec = ST_MTIM_NSEC(stbuf);
70 attr->ctimensec = ST_CTIM_NSEC(stbuf);
73 static void convert_attr(const struct fuse_setattr_in *attr, struct stat *stbuf)
75 stbuf->st_mode = attr->mode;
76 stbuf->st_uid = attr->uid;
77 stbuf->st_gid = attr->gid;
78 stbuf->st_size = attr->size;
79 stbuf->st_atime = attr->atime;
80 stbuf->st_mtime = attr->mtime;
81 ST_ATIM_NSEC_SET(stbuf, attr->atimensec);
82 ST_MTIM_NSEC_SET(stbuf, attr->mtimensec);
85 static size_t iov_length(const struct iovec *iov, size_t count)
87 size_t seg;
88 size_t ret = 0;
90 for (seg = 0; seg < count; seg++)
91 ret += iov[seg].iov_len;
92 return ret;
95 static void list_init_req(struct fuse_req *req)
97 req->next = req;
98 req->prev = req;
101 static void list_del_req(struct fuse_req *req)
103 struct fuse_req *prev = req->prev;
104 struct fuse_req *next = req->next;
105 prev->next = next;
106 next->prev = prev;
109 static void list_add_req(struct fuse_req *req, struct fuse_req *next)
111 struct fuse_req *prev = next->prev;
112 req->next = next;
113 req->prev = prev;
114 prev->next = req;
115 next->prev = req;
118 static void destroy_req(fuse_req_t req)
120 pthread_mutex_destroy(&req->lock);
121 free(req);
124 void fuse_free_req(fuse_req_t req)
126 int ctr;
127 struct fuse_ll *f = req->f;
129 pthread_mutex_lock(&f->lock);
130 req->u.ni.func = NULL;
131 req->u.ni.data = NULL;
132 list_del_req(req);
133 ctr = --req->ctr;
134 pthread_mutex_unlock(&f->lock);
135 if (!ctr)
136 destroy_req(req);
139 static struct fuse_req *fuse_ll_alloc_req(struct fuse_ll *f)
141 struct fuse_req *req;
143 req = (struct fuse_req *) calloc(1, sizeof(struct fuse_req));
144 if (req == NULL) {
145 fprintf(stderr, "fuse: failed to allocate request\n");
146 } else {
147 req->f = f;
148 req->ctr = 1;
149 list_init_req(req);
150 fuse_mutex_init(&req->lock);
153 return req;
156 static int fuse_chan_recv(struct fuse_session *se, struct fuse_buf *buf,
157 struct fuse_chan *ch)
159 struct fuse_ll *f = se->f;
160 int err;
161 ssize_t res;
163 if (!buf->mem) {
164 buf->mem = malloc(f->bufsize);
165 if (!buf->mem) {
166 fprintf(stderr,
167 "fuse: failed to allocate read buffer\n");
168 return -ENOMEM;
172 restart:
173 res = read(fuse_chan_fd(ch), buf->mem, f->bufsize);
174 err = errno;
176 if (fuse_session_exited(se))
177 return 0;
178 if (res == -1) {
179 /* ENOENT means the operation was interrupted, it's safe
180 to restart */
181 if (err == ENOENT)
182 goto restart;
184 if (err == ENODEV) {
185 fuse_session_exit(se);
186 return 0;
188 /* Errors occurring during normal operation: EINTR (read
189 interrupted), EAGAIN (nonblocking I/O), ENODEV (filesystem
190 umounted) */
191 if (err != EINTR && err != EAGAIN)
192 perror("fuse: reading device");
193 return -err;
195 if ((size_t) res < sizeof(struct fuse_in_header)) {
196 fprintf(stderr, "short read on fuse device\n");
197 return -EIO;
200 buf->size = res;
202 return res;
205 static int fuse_chan_send(struct fuse_chan *ch, const struct iovec iov[],
206 size_t count)
208 ssize_t res = writev(fuse_chan_fd(ch), iov, count);
209 int err = errno;
211 if (res == -1) {
212 struct fuse_session *se = fuse_chan_session(ch);
214 assert(se != NULL);
216 /* ENOENT means the operation was interrupted */
217 if (!fuse_session_exited(se) && err != ENOENT)
218 perror("fuse: writing device");
219 return -err;
222 return 0;
225 void fuse_chan_close(struct fuse_chan *ch)
227 close(fuse_chan_fd(ch));
231 static int fuse_send_msg(struct fuse_ll *f, struct fuse_chan *ch,
232 struct iovec *iov, int count)
234 struct fuse_out_header *out = iov[0].iov_base;
236 out->len = iov_length(iov, count);
237 if (f->debug) {
238 if (out->unique == 0) {
239 fprintf(stderr, "NOTIFY: code=%d length=%u\n",
240 out->error, out->len);
241 } else if (out->error) {
242 fprintf(stderr,
243 " unique: %llu, error: %i (%s), outsize: %i\n",
244 (unsigned long long) out->unique, out->error,
245 strerror(-out->error), out->len);
246 } else {
247 fprintf(stderr,
248 " unique: %llu, success, outsize: %i\n",
249 (unsigned long long) out->unique, out->len);
253 return fuse_chan_send(ch, iov, count);
256 int fuse_send_reply_iov_nofree(fuse_req_t req, int error, struct iovec *iov,
257 int count)
259 struct fuse_out_header out;
261 if (error <= -1000 || error > 0) {
262 fprintf(stderr, "fuse: bad error value: %i\n", error);
263 error = -ERANGE;
266 out.unique = req->unique;
267 out.error = error;
269 iov[0].iov_base = &out;
270 iov[0].iov_len = sizeof(struct fuse_out_header);
272 return fuse_send_msg(req->f, req->ch, iov, count);
275 static int send_reply_iov(fuse_req_t req, int error, struct iovec *iov,
276 int count)
278 int res;
280 res = fuse_send_reply_iov_nofree(req, error, iov, count);
281 fuse_free_req(req);
282 return res;
285 static int send_reply(fuse_req_t req, int error, const void *arg,
286 size_t argsize)
288 struct iovec iov[2];
289 int count = 1;
290 if (argsize) {
291 iov[1].iov_base = (void *) arg;
292 iov[1].iov_len = argsize;
293 count++;
295 return send_reply_iov(req, error, iov, count);
298 int fuse_reply_iov(fuse_req_t req, const struct iovec *iov, int count)
300 int res;
301 struct iovec *padded_iov;
303 padded_iov = malloc((count + 1) * sizeof(struct iovec));
304 if (padded_iov == NULL)
305 return fuse_reply_err(req, ENOMEM);
307 memcpy(padded_iov + 1, iov, count * sizeof(struct iovec));
308 count++;
310 res = send_reply_iov(req, 0, padded_iov, count);
311 free(padded_iov);
313 return res;
316 static size_t fuse_dirent_size(size_t namelen)
318 return FUSE_DIRENT_ALIGN(FUSE_NAME_OFFSET + namelen);
321 static char *fuse_add_dirent(char *buf, const char *name,
322 const struct stat *stbuf, off_t off)
324 unsigned namelen = strlen(name);
325 unsigned entlen = FUSE_NAME_OFFSET + namelen;
326 unsigned entsize = fuse_dirent_size(namelen);
327 unsigned padlen = entsize - entlen;
328 struct fuse_dirent *dirent = (struct fuse_dirent *) buf;
330 dirent->ino = stbuf->st_ino;
331 dirent->off = off;
332 dirent->namelen = namelen;
333 dirent->type = (stbuf->st_mode & 0170000) >> 12;
334 strncpy(dirent->name, name, namelen);
335 if (padlen)
336 memset(buf + entlen, 0, padlen);
338 return buf + entsize;
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(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 struct fuse_entry_out *argp;
419 size_t entsize;
421 (void) req;
422 entsize = FUSE_DIRENT_ALIGN(FUSE_NAME_OFFSET_DIRENTPLUS +
423 fuse_dirent_size(strlen(name)));
424 if (entsize <= bufsize && buf){
425 argp = (struct fuse_entry_out *)buf;
426 memset(argp, 0, sizeof(*argp));
427 fill_entry(argp, e);
428 fuse_add_dirent(buf + sizeof(*argp), 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 fi.fh_old = fi.fh;
1145 fip = &fi;
1149 if (req->f->op.getattr)
1150 req->f->op.getattr(req, nodeid, fip);
1151 else
1152 fuse_reply_err(req, ENOSYS);
1155 static void do_setattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1157 struct fuse_setattr_in *arg = (struct fuse_setattr_in *) inarg;
1159 if (req->f->op.setattr) {
1160 struct fuse_file_info *fi = NULL;
1161 struct fuse_file_info fi_store;
1162 struct stat stbuf;
1163 memset(&stbuf, 0, sizeof(stbuf));
1164 convert_attr(arg, &stbuf);
1165 if (arg->valid & FATTR_FH) {
1166 arg->valid &= ~FATTR_FH;
1167 memset(&fi_store, 0, sizeof(fi_store));
1168 fi = &fi_store;
1169 fi->fh = arg->fh;
1170 fi->fh_old = fi->fh;
1172 arg->valid &=
1173 FUSE_SET_ATTR_MODE |
1174 FUSE_SET_ATTR_UID |
1175 FUSE_SET_ATTR_GID |
1176 FUSE_SET_ATTR_SIZE |
1177 FUSE_SET_ATTR_ATIME |
1178 FUSE_SET_ATTR_MTIME |
1179 FUSE_SET_ATTR_ATIME_NOW |
1180 FUSE_SET_ATTR_MTIME_NOW;
1182 req->f->op.setattr(req, nodeid, &stbuf, arg->valid, fi);
1183 } else
1184 fuse_reply_err(req, ENOSYS);
1187 static void do_access(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1189 struct fuse_access_in *arg = (struct fuse_access_in *) inarg;
1191 if (req->f->op.access)
1192 req->f->op.access(req, nodeid, arg->mask);
1193 else
1194 fuse_reply_err(req, ENOSYS);
1197 static void do_readlink(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1199 (void) inarg;
1201 if (req->f->op.readlink)
1202 req->f->op.readlink(req, nodeid);
1203 else
1204 fuse_reply_err(req, ENOSYS);
1207 static void do_mknod(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1209 struct fuse_mknod_in *arg = (struct fuse_mknod_in *) inarg;
1210 char *name = PARAM(arg);
1212 if (req->f->conn.proto_minor >= 12)
1213 req->ctx.umask = arg->umask;
1214 else
1215 name = (char *) inarg + FUSE_COMPAT_MKNOD_IN_SIZE;
1217 if (req->f->op.mknod)
1218 req->f->op.mknod(req, nodeid, name, arg->mode, arg->rdev);
1219 else
1220 fuse_reply_err(req, ENOSYS);
1223 static void do_mkdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1225 struct fuse_mkdir_in *arg = (struct fuse_mkdir_in *) inarg;
1227 if (req->f->conn.proto_minor >= 12)
1228 req->ctx.umask = arg->umask;
1230 if (req->f->op.mkdir)
1231 req->f->op.mkdir(req, nodeid, PARAM(arg), arg->mode);
1232 else
1233 fuse_reply_err(req, ENOSYS);
1236 static void do_unlink(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1238 char *name = (char *) inarg;
1240 if (req->f->op.unlink)
1241 req->f->op.unlink(req, nodeid, name);
1242 else
1243 fuse_reply_err(req, ENOSYS);
1246 static void do_rmdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1248 char *name = (char *) inarg;
1250 if (req->f->op.rmdir)
1251 req->f->op.rmdir(req, nodeid, name);
1252 else
1253 fuse_reply_err(req, ENOSYS);
1256 static void do_symlink(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1258 char *name = (char *) inarg;
1259 char *linkname = ((char *) inarg) + strlen((char *) inarg) + 1;
1261 if (req->f->op.symlink)
1262 req->f->op.symlink(req, linkname, nodeid, name);
1263 else
1264 fuse_reply_err(req, ENOSYS);
1267 static void do_rename(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1269 struct fuse_rename_in *arg = (struct fuse_rename_in *) inarg;
1270 char *oldname = PARAM(arg);
1271 char *newname = oldname + strlen(oldname) + 1;
1273 if (req->f->op.rename)
1274 req->f->op.rename(req, nodeid, oldname, arg->newdir, newname);
1275 else
1276 fuse_reply_err(req, ENOSYS);
1279 static void do_link(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1281 struct fuse_link_in *arg = (struct fuse_link_in *) inarg;
1283 if (req->f->op.link)
1284 req->f->op.link(req, arg->oldnodeid, nodeid, PARAM(arg));
1285 else
1286 fuse_reply_err(req, ENOSYS);
1289 static void do_create(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1291 struct fuse_create_in *arg = (struct fuse_create_in *) inarg;
1293 if (req->f->op.create) {
1294 struct fuse_file_info fi;
1295 char *name = PARAM(arg);
1297 memset(&fi, 0, sizeof(fi));
1298 fi.flags = arg->flags;
1300 if (req->f->conn.proto_minor >= 12)
1301 req->ctx.umask = arg->umask;
1302 else
1303 name = (char *) inarg + sizeof(struct fuse_open_in);
1305 req->f->op.create(req, nodeid, name, arg->mode, &fi);
1306 } else
1307 fuse_reply_err(req, ENOSYS);
1310 static void do_open(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1312 struct fuse_open_in *arg = (struct fuse_open_in *) inarg;
1313 struct fuse_file_info fi;
1315 memset(&fi, 0, sizeof(fi));
1316 fi.flags = arg->flags;
1318 if (req->f->op.open)
1319 req->f->op.open(req, nodeid, &fi);
1320 else
1321 fuse_reply_open(req, &fi);
1324 static void do_read(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1326 struct fuse_read_in *arg = (struct fuse_read_in *) inarg;
1328 if (req->f->op.read) {
1329 struct fuse_file_info fi;
1331 memset(&fi, 0, sizeof(fi));
1332 fi.fh = arg->fh;
1333 fi.fh_old = fi.fh;
1334 if (req->f->conn.proto_minor >= 9) {
1335 fi.lock_owner = arg->lock_owner;
1336 fi.flags = arg->flags;
1338 req->f->op.read(req, nodeid, arg->size, arg->offset, &fi);
1339 } else
1340 fuse_reply_err(req, ENOSYS);
1343 static void do_write(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1345 struct fuse_write_in *arg = (struct fuse_write_in *) inarg;
1346 struct fuse_file_info fi;
1347 char *param;
1349 memset(&fi, 0, sizeof(fi));
1350 fi.fh = arg->fh;
1351 fi.fh_old = fi.fh;
1352 fi.writepage = arg->write_flags & 1;
1354 if (req->f->conn.proto_minor < 9) {
1355 param = ((char *) arg) + FUSE_COMPAT_WRITE_IN_SIZE;
1356 } else {
1357 fi.lock_owner = arg->lock_owner;
1358 fi.flags = arg->flags;
1359 param = PARAM(arg);
1362 if (req->f->op.write)
1363 req->f->op.write(req, nodeid, param, arg->size,
1364 arg->offset, &fi);
1365 else
1366 fuse_reply_err(req, ENOSYS);
1369 static void do_write_buf(fuse_req_t req, fuse_ino_t nodeid, const void *inarg,
1370 const struct fuse_buf *ibuf)
1372 struct fuse_ll *f = req->f;
1373 struct fuse_bufvec bufv = {
1374 .buf[0] = *ibuf,
1375 .count = 1,
1377 struct fuse_write_in *arg = (struct fuse_write_in *) inarg;
1378 struct fuse_file_info fi;
1380 memset(&fi, 0, sizeof(fi));
1381 fi.fh = arg->fh;
1382 fi.fh_old = fi.fh;
1383 fi.writepage = arg->write_flags & 1;
1385 if (req->f->conn.proto_minor < 9) {
1386 bufv.buf[0].mem = ((char *) arg) + FUSE_COMPAT_WRITE_IN_SIZE;
1387 bufv.buf[0].size -= sizeof(struct fuse_in_header) +
1388 FUSE_COMPAT_WRITE_IN_SIZE;
1389 assert(!(bufv.buf[0].flags & FUSE_BUF_IS_FD));
1390 } else {
1391 fi.lock_owner = arg->lock_owner;
1392 fi.flags = arg->flags;
1393 if (!(bufv.buf[0].flags & FUSE_BUF_IS_FD))
1394 bufv.buf[0].mem = PARAM(arg);
1396 bufv.buf[0].size -= sizeof(struct fuse_in_header) +
1397 sizeof(struct fuse_write_in);
1399 if (bufv.buf[0].size < arg->size) {
1400 fprintf(stderr, "fuse: do_write_buf: buffer size too small\n");
1401 fuse_reply_err(req, EIO);
1402 goto out;
1404 bufv.buf[0].size = arg->size;
1406 req->f->op.write_buf(req, nodeid, &bufv, arg->offset, &fi);
1408 out:
1409 /* Need to reset the pipe if ->write_buf() didn't consume all data */
1410 if ((ibuf->flags & FUSE_BUF_IS_FD) && bufv.idx < bufv.count)
1411 fuse_ll_clear_pipe(f);
1414 static void do_flush(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1416 struct fuse_flush_in *arg = (struct fuse_flush_in *) inarg;
1417 struct fuse_file_info fi;
1419 memset(&fi, 0, sizeof(fi));
1420 fi.fh = arg->fh;
1421 fi.fh_old = fi.fh;
1422 fi.flush = 1;
1423 if (req->f->conn.proto_minor >= 7)
1424 fi.lock_owner = arg->lock_owner;
1426 if (req->f->op.flush)
1427 req->f->op.flush(req, nodeid, &fi);
1428 else
1429 fuse_reply_err(req, ENOSYS);
1432 static void do_release(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1434 struct fuse_release_in *arg = (struct fuse_release_in *) inarg;
1435 struct fuse_file_info fi;
1437 memset(&fi, 0, sizeof(fi));
1438 fi.flags = arg->flags;
1439 fi.fh = arg->fh;
1440 fi.fh_old = fi.fh;
1441 if (req->f->conn.proto_minor >= 8) {
1442 fi.flush = (arg->release_flags & FUSE_RELEASE_FLUSH) ? 1 : 0;
1443 fi.lock_owner = arg->lock_owner;
1445 if (arg->release_flags & FUSE_RELEASE_FLOCK_UNLOCK) {
1446 fi.flock_release = 1;
1447 fi.lock_owner = arg->lock_owner;
1450 if (req->f->op.release)
1451 req->f->op.release(req, nodeid, &fi);
1452 else
1453 fuse_reply_err(req, 0);
1456 static void do_fsync(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1458 struct fuse_fsync_in *arg = (struct fuse_fsync_in *) inarg;
1459 struct fuse_file_info fi;
1461 memset(&fi, 0, sizeof(fi));
1462 fi.fh = arg->fh;
1463 fi.fh_old = fi.fh;
1465 if (req->f->op.fsync)
1466 req->f->op.fsync(req, nodeid, arg->fsync_flags & 1, &fi);
1467 else
1468 fuse_reply_err(req, ENOSYS);
1471 static void do_opendir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1473 struct fuse_open_in *arg = (struct fuse_open_in *) inarg;
1474 struct fuse_file_info fi;
1476 memset(&fi, 0, sizeof(fi));
1477 fi.flags = arg->flags;
1479 if (req->f->op.opendir)
1480 req->f->op.opendir(req, nodeid, &fi);
1481 else
1482 fuse_reply_open(req, &fi);
1485 static void do_readdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1487 struct fuse_read_in *arg = (struct fuse_read_in *) inarg;
1488 struct fuse_file_info fi;
1490 memset(&fi, 0, sizeof(fi));
1491 fi.fh = arg->fh;
1492 fi.fh_old = fi.fh;
1494 if (req->f->op.readdir)
1495 req->f->op.readdir(req, nodeid, arg->size, arg->offset, &fi);
1496 else
1497 fuse_reply_err(req, ENOSYS);
1500 static void do_readdirplus(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1502 struct fuse_read_in *arg = (struct fuse_read_in *) inarg;
1503 struct fuse_file_info fi;
1505 memset(&fi, 0, sizeof(fi));
1506 fi.fh = arg->fh;
1507 fi.fh_old = fi.fh;
1509 if (req->f->op.readdirplus)
1510 req->f->op.readdirplus(req, nodeid, arg->size, arg->offset, &fi);
1511 else
1512 fuse_reply_err(req, ENOSYS);
1515 static void do_releasedir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1517 struct fuse_release_in *arg = (struct fuse_release_in *) inarg;
1518 struct fuse_file_info fi;
1520 memset(&fi, 0, sizeof(fi));
1521 fi.flags = arg->flags;
1522 fi.fh = arg->fh;
1523 fi.fh_old = fi.fh;
1525 if (req->f->op.releasedir)
1526 req->f->op.releasedir(req, nodeid, &fi);
1527 else
1528 fuse_reply_err(req, 0);
1531 static void do_fsyncdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1533 struct fuse_fsync_in *arg = (struct fuse_fsync_in *) inarg;
1534 struct fuse_file_info fi;
1536 memset(&fi, 0, sizeof(fi));
1537 fi.fh = arg->fh;
1538 fi.fh_old = fi.fh;
1540 if (req->f->op.fsyncdir)
1541 req->f->op.fsyncdir(req, nodeid, arg->fsync_flags & 1, &fi);
1542 else
1543 fuse_reply_err(req, ENOSYS);
1546 static void do_statfs(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1548 (void) nodeid;
1549 (void) inarg;
1551 if (req->f->op.statfs)
1552 req->f->op.statfs(req, nodeid);
1553 else {
1554 struct statvfs buf = {
1555 .f_namemax = 255,
1556 .f_bsize = 512,
1558 fuse_reply_statfs(req, &buf);
1562 static void do_setxattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1564 struct fuse_setxattr_in *arg = (struct fuse_setxattr_in *) inarg;
1565 char *name = PARAM(arg);
1566 char *value = name + strlen(name) + 1;
1568 if (req->f->op.setxattr)
1569 req->f->op.setxattr(req, nodeid, name, value, arg->size,
1570 arg->flags);
1571 else
1572 fuse_reply_err(req, ENOSYS);
1575 static void do_getxattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1577 struct fuse_getxattr_in *arg = (struct fuse_getxattr_in *) inarg;
1579 if (req->f->op.getxattr)
1580 req->f->op.getxattr(req, nodeid, PARAM(arg), arg->size);
1581 else
1582 fuse_reply_err(req, ENOSYS);
1585 static void do_listxattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1587 struct fuse_getxattr_in *arg = (struct fuse_getxattr_in *) inarg;
1589 if (req->f->op.listxattr)
1590 req->f->op.listxattr(req, nodeid, arg->size);
1591 else
1592 fuse_reply_err(req, ENOSYS);
1595 static void do_removexattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1597 char *name = (char *) inarg;
1599 if (req->f->op.removexattr)
1600 req->f->op.removexattr(req, nodeid, name);
1601 else
1602 fuse_reply_err(req, ENOSYS);
1605 static void convert_fuse_file_lock(struct fuse_file_lock *fl,
1606 struct flock *flock)
1608 memset(flock, 0, sizeof(struct flock));
1609 flock->l_type = fl->type;
1610 flock->l_whence = SEEK_SET;
1611 flock->l_start = fl->start;
1612 if (fl->end == OFFSET_MAX)
1613 flock->l_len = 0;
1614 else
1615 flock->l_len = fl->end - fl->start + 1;
1616 flock->l_pid = fl->pid;
1619 static void do_getlk(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1621 struct fuse_lk_in *arg = (struct fuse_lk_in *) inarg;
1622 struct fuse_file_info fi;
1623 struct flock flock;
1625 memset(&fi, 0, sizeof(fi));
1626 fi.fh = arg->fh;
1627 fi.lock_owner = arg->owner;
1629 convert_fuse_file_lock(&arg->lk, &flock);
1630 if (req->f->op.getlk)
1631 req->f->op.getlk(req, nodeid, &fi, &flock);
1632 else
1633 fuse_reply_err(req, ENOSYS);
1636 static void do_setlk_common(fuse_req_t req, fuse_ino_t nodeid,
1637 const void *inarg, int sleep)
1639 struct fuse_lk_in *arg = (struct fuse_lk_in *) inarg;
1640 struct fuse_file_info fi;
1641 struct flock flock;
1643 memset(&fi, 0, sizeof(fi));
1644 fi.fh = arg->fh;
1645 fi.lock_owner = arg->owner;
1647 if (arg->lk_flags & FUSE_LK_FLOCK) {
1648 int op = 0;
1650 switch (arg->lk.type) {
1651 case F_RDLCK:
1652 op = LOCK_SH;
1653 break;
1654 case F_WRLCK:
1655 op = LOCK_EX;
1656 break;
1657 case F_UNLCK:
1658 op = LOCK_UN;
1659 break;
1661 if (!sleep)
1662 op |= LOCK_NB;
1664 if (req->f->op.flock)
1665 req->f->op.flock(req, nodeid, &fi, op);
1666 else
1667 fuse_reply_err(req, ENOSYS);
1668 } else {
1669 convert_fuse_file_lock(&arg->lk, &flock);
1670 if (req->f->op.setlk)
1671 req->f->op.setlk(req, nodeid, &fi, &flock, sleep);
1672 else
1673 fuse_reply_err(req, ENOSYS);
1677 static void do_setlk(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1679 do_setlk_common(req, nodeid, inarg, 0);
1682 static void do_setlkw(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1684 do_setlk_common(req, nodeid, inarg, 1);
1687 static int find_interrupted(struct fuse_ll *f, struct fuse_req *req)
1689 struct fuse_req *curr;
1691 for (curr = f->list.next; curr != &f->list; curr = curr->next) {
1692 if (curr->unique == req->u.i.unique) {
1693 fuse_interrupt_func_t func;
1694 void *data;
1696 curr->ctr++;
1697 pthread_mutex_unlock(&f->lock);
1699 /* Ugh, ugly locking */
1700 pthread_mutex_lock(&curr->lock);
1701 pthread_mutex_lock(&f->lock);
1702 curr->interrupted = 1;
1703 func = curr->u.ni.func;
1704 data = curr->u.ni.data;
1705 pthread_mutex_unlock(&f->lock);
1706 if (func)
1707 func(curr, data);
1708 pthread_mutex_unlock(&curr->lock);
1710 pthread_mutex_lock(&f->lock);
1711 curr->ctr--;
1712 if (!curr->ctr)
1713 destroy_req(curr);
1715 return 1;
1718 for (curr = f->interrupts.next; curr != &f->interrupts;
1719 curr = curr->next) {
1720 if (curr->u.i.unique == req->u.i.unique)
1721 return 1;
1723 return 0;
1726 static void do_interrupt(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1728 struct fuse_interrupt_in *arg = (struct fuse_interrupt_in *) inarg;
1729 struct fuse_ll *f = req->f;
1731 (void) nodeid;
1732 if (f->debug)
1733 fprintf(stderr, "INTERRUPT: %llu\n",
1734 (unsigned long long) arg->unique);
1736 req->u.i.unique = arg->unique;
1738 pthread_mutex_lock(&f->lock);
1739 if (find_interrupted(f, req))
1740 destroy_req(req);
1741 else
1742 list_add_req(req, &f->interrupts);
1743 pthread_mutex_unlock(&f->lock);
1746 static struct fuse_req *check_interrupt(struct fuse_ll *f, struct fuse_req *req)
1748 struct fuse_req *curr;
1750 for (curr = f->interrupts.next; curr != &f->interrupts;
1751 curr = curr->next) {
1752 if (curr->u.i.unique == req->unique) {
1753 req->interrupted = 1;
1754 list_del_req(curr);
1755 free(curr);
1756 return NULL;
1759 curr = f->interrupts.next;
1760 if (curr != &f->interrupts) {
1761 list_del_req(curr);
1762 list_init_req(curr);
1763 return curr;
1764 } else
1765 return NULL;
1768 static void do_bmap(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1770 struct fuse_bmap_in *arg = (struct fuse_bmap_in *) inarg;
1772 if (req->f->op.bmap)
1773 req->f->op.bmap(req, nodeid, arg->blocksize, arg->block);
1774 else
1775 fuse_reply_err(req, ENOSYS);
1778 static void do_ioctl(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1780 struct fuse_ioctl_in *arg = (struct fuse_ioctl_in *) inarg;
1781 unsigned int flags = arg->flags;
1782 void *in_buf = arg->in_size ? PARAM(arg) : NULL;
1783 struct fuse_file_info fi;
1785 if (flags & FUSE_IOCTL_DIR &&
1786 !(req->f->conn.want & FUSE_CAP_IOCTL_DIR)) {
1787 fuse_reply_err(req, ENOTTY);
1788 return;
1791 memset(&fi, 0, sizeof(fi));
1792 fi.fh = arg->fh;
1793 fi.fh_old = fi.fh;
1795 if (sizeof(void *) == 4 && req->f->conn.proto_minor >= 16 &&
1796 !(flags & FUSE_IOCTL_32BIT)) {
1797 req->ioctl_64bit = 1;
1800 if (req->f->op.ioctl)
1801 req->f->op.ioctl(req, nodeid, arg->cmd,
1802 (void *)(uintptr_t)arg->arg, &fi, flags,
1803 in_buf, arg->in_size, arg->out_size);
1804 else
1805 fuse_reply_err(req, ENOSYS);
1808 void fuse_pollhandle_destroy(struct fuse_pollhandle *ph)
1810 free(ph);
1813 static void do_poll(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1815 struct fuse_poll_in *arg = (struct fuse_poll_in *) inarg;
1816 struct fuse_file_info fi;
1818 memset(&fi, 0, sizeof(fi));
1819 fi.fh = arg->fh;
1820 fi.fh_old = fi.fh;
1821 fi.poll_events = arg->events;
1823 if (req->f->op.poll) {
1824 struct fuse_pollhandle *ph = NULL;
1826 if (arg->flags & FUSE_POLL_SCHEDULE_NOTIFY) {
1827 ph = malloc(sizeof(struct fuse_pollhandle));
1828 if (ph == NULL) {
1829 fuse_reply_err(req, ENOMEM);
1830 return;
1832 ph->kh = arg->kh;
1833 ph->ch = req->ch;
1834 ph->f = req->f;
1837 req->f->op.poll(req, nodeid, &fi, ph);
1838 } else {
1839 fuse_reply_err(req, ENOSYS);
1843 static void do_fallocate(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1845 struct fuse_fallocate_in *arg = (struct fuse_fallocate_in *) inarg;
1846 struct fuse_file_info fi;
1848 memset(&fi, 0, sizeof(fi));
1849 fi.fh = arg->fh;
1851 if (req->f->op.fallocate)
1852 req->f->op.fallocate(req, nodeid, arg->mode, arg->offset, arg->length, &fi);
1853 else
1854 fuse_reply_err(req, ENOSYS);
1857 static void do_init(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1859 struct fuse_init_in *arg = (struct fuse_init_in *) inarg;
1860 struct fuse_init_out outarg;
1861 struct fuse_ll *f = req->f;
1862 size_t bufsize = f->bufsize;
1864 (void) nodeid;
1865 if (f->debug) {
1866 fprintf(stderr, "INIT: %u.%u\n", arg->major, arg->minor);
1867 if (arg->major == 7 && arg->minor >= 6) {
1868 fprintf(stderr, "flags=0x%08x\n", arg->flags);
1869 fprintf(stderr, "max_readahead=0x%08x\n",
1870 arg->max_readahead);
1873 f->conn.proto_major = arg->major;
1874 f->conn.proto_minor = arg->minor;
1875 f->conn.capable = 0;
1876 f->conn.want = 0;
1878 memset(&outarg, 0, sizeof(outarg));
1879 outarg.major = FUSE_KERNEL_VERSION;
1880 outarg.minor = FUSE_KERNEL_MINOR_VERSION;
1882 if (arg->major < 7) {
1883 fprintf(stderr, "fuse: unsupported protocol version: %u.%u\n",
1884 arg->major, arg->minor);
1885 fuse_reply_err(req, EPROTO);
1886 return;
1889 if (arg->major > 7) {
1890 /* Wait for a second INIT request with a 7.X version */
1891 send_reply_ok(req, &outarg, sizeof(outarg));
1892 return;
1895 if (arg->minor >= 6) {
1896 if (f->conn.async_read)
1897 f->conn.async_read = arg->flags & FUSE_ASYNC_READ;
1898 if (arg->max_readahead < f->conn.max_readahead)
1899 f->conn.max_readahead = arg->max_readahead;
1900 if (arg->flags & FUSE_ASYNC_READ)
1901 f->conn.capable |= FUSE_CAP_ASYNC_READ;
1902 if (arg->flags & FUSE_POSIX_LOCKS)
1903 f->conn.capable |= FUSE_CAP_POSIX_LOCKS;
1904 if (arg->flags & FUSE_ATOMIC_O_TRUNC)
1905 f->conn.capable |= FUSE_CAP_ATOMIC_O_TRUNC;
1906 if (arg->flags & FUSE_EXPORT_SUPPORT)
1907 f->conn.capable |= FUSE_CAP_EXPORT_SUPPORT;
1908 if (arg->flags & FUSE_BIG_WRITES)
1909 f->conn.capable |= FUSE_CAP_BIG_WRITES;
1910 if (arg->flags & FUSE_DONT_MASK)
1911 f->conn.capable |= FUSE_CAP_DONT_MASK;
1912 if (arg->flags & FUSE_FLOCK_LOCKS)
1913 f->conn.capable |= FUSE_CAP_FLOCK_LOCKS;
1914 if (arg->flags & FUSE_AUTO_INVAL_DATA)
1915 f->conn.capable |= FUSE_CAP_AUTO_INVAL_DATA;
1916 if (arg->flags & FUSE_DO_READDIRPLUS)
1917 f->conn.capable |= FUSE_CAP_READDIRPLUS;
1918 if (arg->flags & FUSE_READDIRPLUS_AUTO)
1919 f->conn.capable |= FUSE_CAP_READDIRPLUS_AUTO;
1920 } else {
1921 f->conn.async_read = 0;
1922 f->conn.max_readahead = 0;
1925 if (req->f->conn.proto_minor >= 14) {
1926 #ifdef HAVE_SPLICE
1927 #ifdef HAVE_VMSPLICE
1928 f->conn.capable |= FUSE_CAP_SPLICE_WRITE | FUSE_CAP_SPLICE_MOVE;
1929 if (f->splice_write)
1930 f->conn.want |= FUSE_CAP_SPLICE_WRITE;
1931 if (f->splice_move)
1932 f->conn.want |= FUSE_CAP_SPLICE_MOVE;
1933 #endif
1934 f->conn.capable |= FUSE_CAP_SPLICE_READ;
1935 if (f->splice_read)
1936 f->conn.want |= FUSE_CAP_SPLICE_READ;
1937 #endif
1939 if (req->f->conn.proto_minor >= 18)
1940 f->conn.capable |= FUSE_CAP_IOCTL_DIR;
1942 if (f->atomic_o_trunc)
1943 f->conn.want |= FUSE_CAP_ATOMIC_O_TRUNC;
1944 if (f->op.getlk && f->op.setlk && !f->no_remote_posix_lock)
1945 f->conn.want |= FUSE_CAP_POSIX_LOCKS;
1946 if (f->op.flock && !f->no_remote_flock)
1947 f->conn.want |= FUSE_CAP_FLOCK_LOCKS;
1948 if (f->big_writes)
1949 f->conn.want |= FUSE_CAP_BIG_WRITES;
1950 if (f->auto_inval_data)
1951 f->conn.want |= FUSE_CAP_AUTO_INVAL_DATA;
1952 if (f->op.readdirplus && !f->no_readdirplus) {
1953 f->conn.want |= FUSE_CAP_READDIRPLUS;
1954 if (!f->no_readdirplus_auto)
1955 f->conn.want |= FUSE_CAP_READDIRPLUS_AUTO;
1958 if (bufsize < FUSE_MIN_READ_BUFFER) {
1959 fprintf(stderr, "fuse: warning: buffer size too small: %zu\n",
1960 bufsize);
1961 bufsize = FUSE_MIN_READ_BUFFER;
1964 bufsize -= 4096;
1965 if (bufsize < f->conn.max_write)
1966 f->conn.max_write = bufsize;
1968 f->got_init = 1;
1969 if (f->op.init)
1970 f->op.init(f->userdata, &f->conn);
1972 if (f->no_splice_read)
1973 f->conn.want &= ~FUSE_CAP_SPLICE_READ;
1974 if (f->no_splice_write)
1975 f->conn.want &= ~FUSE_CAP_SPLICE_WRITE;
1976 if (f->no_splice_move)
1977 f->conn.want &= ~FUSE_CAP_SPLICE_MOVE;
1978 if (f->no_auto_inval_data)
1979 f->conn.want &= ~FUSE_CAP_AUTO_INVAL_DATA;
1980 if (f->no_readdirplus)
1981 f->conn.want &= ~FUSE_CAP_READDIRPLUS;
1982 if (f->no_readdirplus_auto)
1983 f->conn.want &= ~FUSE_CAP_READDIRPLUS_AUTO;
1984 if (f->conn.async_read || (f->conn.want & FUSE_CAP_ASYNC_READ))
1985 outarg.flags |= FUSE_ASYNC_READ;
1986 if (f->conn.want & FUSE_CAP_POSIX_LOCKS)
1987 outarg.flags |= FUSE_POSIX_LOCKS;
1988 if (f->conn.want & FUSE_CAP_ATOMIC_O_TRUNC)
1989 outarg.flags |= FUSE_ATOMIC_O_TRUNC;
1990 if (f->conn.want & FUSE_CAP_EXPORT_SUPPORT)
1991 outarg.flags |= FUSE_EXPORT_SUPPORT;
1992 if (f->conn.want & FUSE_CAP_BIG_WRITES)
1993 outarg.flags |= FUSE_BIG_WRITES;
1994 if (f->conn.want & FUSE_CAP_DONT_MASK)
1995 outarg.flags |= FUSE_DONT_MASK;
1996 if (f->conn.want & FUSE_CAP_FLOCK_LOCKS)
1997 outarg.flags |= FUSE_FLOCK_LOCKS;
1998 if (f->conn.want & FUSE_CAP_AUTO_INVAL_DATA)
1999 outarg.flags |= FUSE_AUTO_INVAL_DATA;
2000 if (f->conn.want & FUSE_CAP_READDIRPLUS)
2001 outarg.flags |= FUSE_DO_READDIRPLUS;
2002 if (f->conn.want & FUSE_CAP_READDIRPLUS_AUTO)
2003 outarg.flags |= FUSE_READDIRPLUS_AUTO;
2004 outarg.max_readahead = f->conn.max_readahead;
2005 outarg.max_write = f->conn.max_write;
2006 if (f->conn.proto_minor >= 13) {
2007 if (f->conn.max_background >= (1 << 16))
2008 f->conn.max_background = (1 << 16) - 1;
2009 if (f->conn.congestion_threshold > f->conn.max_background)
2010 f->conn.congestion_threshold = f->conn.max_background;
2011 if (!f->conn.congestion_threshold) {
2012 f->conn.congestion_threshold =
2013 f->conn.max_background * 3 / 4;
2016 outarg.max_background = f->conn.max_background;
2017 outarg.congestion_threshold = f->conn.congestion_threshold;
2020 if (f->debug) {
2021 fprintf(stderr, " INIT: %u.%u\n", outarg.major, outarg.minor);
2022 fprintf(stderr, " flags=0x%08x\n", outarg.flags);
2023 fprintf(stderr, " max_readahead=0x%08x\n",
2024 outarg.max_readahead);
2025 fprintf(stderr, " max_write=0x%08x\n", outarg.max_write);
2026 fprintf(stderr, " max_background=%i\n",
2027 outarg.max_background);
2028 fprintf(stderr, " congestion_threshold=%i\n",
2029 outarg.congestion_threshold);
2032 send_reply_ok(req, &outarg, arg->minor < 5 ? 8 : sizeof(outarg));
2035 static void do_destroy(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
2037 struct fuse_ll *f = req->f;
2039 (void) nodeid;
2040 (void) inarg;
2042 f->got_destroy = 1;
2043 if (f->op.destroy)
2044 f->op.destroy(f->userdata);
2046 send_reply_ok(req, NULL, 0);
2049 static void list_del_nreq(struct fuse_notify_req *nreq)
2051 struct fuse_notify_req *prev = nreq->prev;
2052 struct fuse_notify_req *next = nreq->next;
2053 prev->next = next;
2054 next->prev = prev;
2057 static void list_add_nreq(struct fuse_notify_req *nreq,
2058 struct fuse_notify_req *next)
2060 struct fuse_notify_req *prev = next->prev;
2061 nreq->next = next;
2062 nreq->prev = prev;
2063 prev->next = nreq;
2064 next->prev = nreq;
2067 static void list_init_nreq(struct fuse_notify_req *nreq)
2069 nreq->next = nreq;
2070 nreq->prev = nreq;
2073 static void do_notify_reply(fuse_req_t req, fuse_ino_t nodeid,
2074 const void *inarg, const struct fuse_buf *buf)
2076 struct fuse_ll *f = req->f;
2077 struct fuse_notify_req *nreq;
2078 struct fuse_notify_req *head;
2080 pthread_mutex_lock(&f->lock);
2081 head = &f->notify_list;
2082 for (nreq = head->next; nreq != head; nreq = nreq->next) {
2083 if (nreq->unique == req->unique) {
2084 list_del_nreq(nreq);
2085 break;
2088 pthread_mutex_unlock(&f->lock);
2090 if (nreq != head)
2091 nreq->reply(nreq, req, nodeid, inarg, buf);
2094 static int send_notify_iov(struct fuse_ll *f, struct fuse_chan *ch,
2095 int notify_code, struct iovec *iov, int count)
2097 struct fuse_out_header out;
2099 if (!f->got_init)
2100 return -ENOTCONN;
2102 out.unique = 0;
2103 out.error = notify_code;
2104 iov[0].iov_base = &out;
2105 iov[0].iov_len = sizeof(struct fuse_out_header);
2107 return fuse_send_msg(f, ch, iov, count);
2110 int fuse_lowlevel_notify_poll(struct fuse_pollhandle *ph)
2112 if (ph != NULL) {
2113 struct fuse_notify_poll_wakeup_out outarg;
2114 struct iovec iov[2];
2116 outarg.kh = ph->kh;
2118 iov[1].iov_base = &outarg;
2119 iov[1].iov_len = sizeof(outarg);
2121 return send_notify_iov(ph->f, ph->ch, FUSE_NOTIFY_POLL, iov, 2);
2122 } else {
2123 return 0;
2127 int fuse_lowlevel_notify_inval_inode(struct fuse_chan *ch, fuse_ino_t ino,
2128 off_t off, off_t len)
2130 struct fuse_notify_inval_inode_out outarg;
2131 struct fuse_ll *f;
2132 struct iovec iov[2];
2134 if (!ch)
2135 return -EINVAL;
2137 f = fuse_chan_session(ch)->f;
2138 if (!f)
2139 return -ENODEV;
2141 outarg.ino = ino;
2142 outarg.off = off;
2143 outarg.len = len;
2145 iov[1].iov_base = &outarg;
2146 iov[1].iov_len = sizeof(outarg);
2148 return send_notify_iov(f, ch, FUSE_NOTIFY_INVAL_INODE, iov, 2);
2151 int fuse_lowlevel_notify_inval_entry(struct fuse_chan *ch, fuse_ino_t parent,
2152 const char *name, size_t namelen)
2154 struct fuse_notify_inval_entry_out outarg;
2155 struct fuse_ll *f;
2156 struct iovec iov[3];
2158 if (!ch)
2159 return -EINVAL;
2161 f = fuse_chan_session(ch)->f;
2162 if (!f)
2163 return -ENODEV;
2165 outarg.parent = parent;
2166 outarg.namelen = namelen;
2167 outarg.padding = 0;
2169 iov[1].iov_base = &outarg;
2170 iov[1].iov_len = sizeof(outarg);
2171 iov[2].iov_base = (void *)name;
2172 iov[2].iov_len = namelen + 1;
2174 return send_notify_iov(f, ch, FUSE_NOTIFY_INVAL_ENTRY, iov, 3);
2177 int fuse_lowlevel_notify_delete(struct fuse_chan *ch,
2178 fuse_ino_t parent, fuse_ino_t child,
2179 const char *name, size_t namelen)
2181 struct fuse_notify_delete_out outarg;
2182 struct fuse_ll *f;
2183 struct iovec iov[3];
2185 if (!ch)
2186 return -EINVAL;
2188 f = fuse_chan_session(ch)->f;
2189 if (!f)
2190 return -ENODEV;
2192 if (f->conn.proto_minor < 18)
2193 return -ENOSYS;
2195 outarg.parent = parent;
2196 outarg.child = child;
2197 outarg.namelen = namelen;
2198 outarg.padding = 0;
2200 iov[1].iov_base = &outarg;
2201 iov[1].iov_len = sizeof(outarg);
2202 iov[2].iov_base = (void *)name;
2203 iov[2].iov_len = namelen + 1;
2205 return send_notify_iov(f, ch, FUSE_NOTIFY_DELETE, iov, 3);
2208 int fuse_lowlevel_notify_store(struct fuse_chan *ch, fuse_ino_t ino,
2209 off_t offset, struct fuse_bufvec *bufv,
2210 enum fuse_buf_copy_flags flags)
2212 struct fuse_out_header out;
2213 struct fuse_notify_store_out outarg;
2214 struct fuse_ll *f;
2215 struct iovec iov[3];
2216 size_t size = fuse_buf_size(bufv);
2217 int res;
2219 if (!ch)
2220 return -EINVAL;
2222 f = fuse_chan_session(ch)->f;
2223 if (!f)
2224 return -ENODEV;
2226 if (f->conn.proto_minor < 15)
2227 return -ENOSYS;
2229 out.unique = 0;
2230 out.error = FUSE_NOTIFY_STORE;
2232 outarg.nodeid = ino;
2233 outarg.offset = offset;
2234 outarg.size = size;
2236 iov[0].iov_base = &out;
2237 iov[0].iov_len = sizeof(out);
2238 iov[1].iov_base = &outarg;
2239 iov[1].iov_len = sizeof(outarg);
2241 res = fuse_send_data_iov(f, ch, iov, 2, bufv, flags);
2242 if (res > 0)
2243 res = -res;
2245 return res;
2248 struct fuse_retrieve_req {
2249 struct fuse_notify_req nreq;
2250 void *cookie;
2253 static void fuse_ll_retrieve_reply(struct fuse_notify_req *nreq,
2254 fuse_req_t req, fuse_ino_t ino,
2255 const void *inarg,
2256 const struct fuse_buf *ibuf)
2258 struct fuse_ll *f = req->f;
2259 struct fuse_retrieve_req *rreq =
2260 container_of(nreq, struct fuse_retrieve_req, nreq);
2261 const struct fuse_notify_retrieve_in *arg = inarg;
2262 struct fuse_bufvec bufv = {
2263 .buf[0] = *ibuf,
2264 .count = 1,
2267 if (!(bufv.buf[0].flags & FUSE_BUF_IS_FD))
2268 bufv.buf[0].mem = PARAM(arg);
2270 bufv.buf[0].size -= sizeof(struct fuse_in_header) +
2271 sizeof(struct fuse_notify_retrieve_in);
2273 if (bufv.buf[0].size < arg->size) {
2274 fprintf(stderr, "fuse: retrieve reply: buffer size too small\n");
2275 fuse_reply_none(req);
2276 goto out;
2278 bufv.buf[0].size = arg->size;
2280 if (req->f->op.retrieve_reply) {
2281 req->f->op.retrieve_reply(req, rreq->cookie, ino,
2282 arg->offset, &bufv);
2283 } else {
2284 fuse_reply_none(req);
2286 out:
2287 free(rreq);
2288 if ((ibuf->flags & FUSE_BUF_IS_FD) && bufv.idx < bufv.count)
2289 fuse_ll_clear_pipe(f);
2292 int fuse_lowlevel_notify_retrieve(struct fuse_chan *ch, fuse_ino_t ino,
2293 size_t size, off_t offset, void *cookie)
2295 struct fuse_notify_retrieve_out outarg;
2296 struct fuse_ll *f;
2297 struct iovec iov[2];
2298 struct fuse_retrieve_req *rreq;
2299 int err;
2301 if (!ch)
2302 return -EINVAL;
2304 f = fuse_chan_session(ch)->f;
2305 if (!f)
2306 return -ENODEV;
2308 if (f->conn.proto_minor < 15)
2309 return -ENOSYS;
2311 rreq = malloc(sizeof(*rreq));
2312 if (rreq == NULL)
2313 return -ENOMEM;
2315 pthread_mutex_lock(&f->lock);
2316 rreq->cookie = cookie;
2317 rreq->nreq.unique = f->notify_ctr++;
2318 rreq->nreq.reply = fuse_ll_retrieve_reply;
2319 list_add_nreq(&rreq->nreq, &f->notify_list);
2320 pthread_mutex_unlock(&f->lock);
2322 outarg.notify_unique = rreq->nreq.unique;
2323 outarg.nodeid = ino;
2324 outarg.offset = offset;
2325 outarg.size = size;
2327 iov[1].iov_base = &outarg;
2328 iov[1].iov_len = sizeof(outarg);
2330 err = send_notify_iov(f, ch, FUSE_NOTIFY_RETRIEVE, iov, 2);
2331 if (err) {
2332 pthread_mutex_lock(&f->lock);
2333 list_del_nreq(&rreq->nreq);
2334 pthread_mutex_unlock(&f->lock);
2335 free(rreq);
2338 return err;
2341 void *fuse_req_userdata(fuse_req_t req)
2343 return req->f->userdata;
2346 const struct fuse_ctx *fuse_req_ctx(fuse_req_t req)
2348 return &req->ctx;
2351 void fuse_req_interrupt_func(fuse_req_t req, fuse_interrupt_func_t func,
2352 void *data)
2354 pthread_mutex_lock(&req->lock);
2355 pthread_mutex_lock(&req->f->lock);
2356 req->u.ni.func = func;
2357 req->u.ni.data = data;
2358 pthread_mutex_unlock(&req->f->lock);
2359 if (req->interrupted && func)
2360 func(req, data);
2361 pthread_mutex_unlock(&req->lock);
2364 int fuse_req_interrupted(fuse_req_t req)
2366 int interrupted;
2368 pthread_mutex_lock(&req->f->lock);
2369 interrupted = req->interrupted;
2370 pthread_mutex_unlock(&req->f->lock);
2372 return interrupted;
2375 static struct {
2376 void (*func)(fuse_req_t, fuse_ino_t, const void *);
2377 const char *name;
2378 } fuse_ll_ops[] = {
2379 [FUSE_LOOKUP] = { do_lookup, "LOOKUP" },
2380 [FUSE_FORGET] = { do_forget, "FORGET" },
2381 [FUSE_GETATTR] = { do_getattr, "GETATTR" },
2382 [FUSE_SETATTR] = { do_setattr, "SETATTR" },
2383 [FUSE_READLINK] = { do_readlink, "READLINK" },
2384 [FUSE_SYMLINK] = { do_symlink, "SYMLINK" },
2385 [FUSE_MKNOD] = { do_mknod, "MKNOD" },
2386 [FUSE_MKDIR] = { do_mkdir, "MKDIR" },
2387 [FUSE_UNLINK] = { do_unlink, "UNLINK" },
2388 [FUSE_RMDIR] = { do_rmdir, "RMDIR" },
2389 [FUSE_RENAME] = { do_rename, "RENAME" },
2390 [FUSE_LINK] = { do_link, "LINK" },
2391 [FUSE_OPEN] = { do_open, "OPEN" },
2392 [FUSE_READ] = { do_read, "READ" },
2393 [FUSE_WRITE] = { do_write, "WRITE" },
2394 [FUSE_STATFS] = { do_statfs, "STATFS" },
2395 [FUSE_RELEASE] = { do_release, "RELEASE" },
2396 [FUSE_FSYNC] = { do_fsync, "FSYNC" },
2397 [FUSE_SETXATTR] = { do_setxattr, "SETXATTR" },
2398 [FUSE_GETXATTR] = { do_getxattr, "GETXATTR" },
2399 [FUSE_LISTXATTR] = { do_listxattr, "LISTXATTR" },
2400 [FUSE_REMOVEXATTR] = { do_removexattr, "REMOVEXATTR" },
2401 [FUSE_FLUSH] = { do_flush, "FLUSH" },
2402 [FUSE_INIT] = { do_init, "INIT" },
2403 [FUSE_OPENDIR] = { do_opendir, "OPENDIR" },
2404 [FUSE_READDIR] = { do_readdir, "READDIR" },
2405 [FUSE_RELEASEDIR] = { do_releasedir, "RELEASEDIR" },
2406 [FUSE_FSYNCDIR] = { do_fsyncdir, "FSYNCDIR" },
2407 [FUSE_GETLK] = { do_getlk, "GETLK" },
2408 [FUSE_SETLK] = { do_setlk, "SETLK" },
2409 [FUSE_SETLKW] = { do_setlkw, "SETLKW" },
2410 [FUSE_ACCESS] = { do_access, "ACCESS" },
2411 [FUSE_CREATE] = { do_create, "CREATE" },
2412 [FUSE_INTERRUPT] = { do_interrupt, "INTERRUPT" },
2413 [FUSE_BMAP] = { do_bmap, "BMAP" },
2414 [FUSE_IOCTL] = { do_ioctl, "IOCTL" },
2415 [FUSE_POLL] = { do_poll, "POLL" },
2416 [FUSE_FALLOCATE] = { do_fallocate, "FALLOCATE" },
2417 [FUSE_DESTROY] = { do_destroy, "DESTROY" },
2418 [FUSE_NOTIFY_REPLY] = { (void *) 1, "NOTIFY_REPLY" },
2419 [FUSE_BATCH_FORGET] = { do_batch_forget, "BATCH_FORGET" },
2420 [FUSE_READDIRPLUS] = { do_readdirplus, "READDIRPLUS"},
2421 [CUSE_INIT] = { cuse_lowlevel_init, "CUSE_INIT" },
2424 #define FUSE_MAXOP (sizeof(fuse_ll_ops) / sizeof(fuse_ll_ops[0]))
2426 static const char *opname(enum fuse_opcode opcode)
2428 if (opcode >= FUSE_MAXOP || !fuse_ll_ops[opcode].name)
2429 return "???";
2430 else
2431 return fuse_ll_ops[opcode].name;
2434 static int fuse_ll_copy_from_pipe(struct fuse_bufvec *dst,
2435 struct fuse_bufvec *src)
2437 int res = fuse_buf_copy(dst, src, 0);
2438 if (res < 0) {
2439 fprintf(stderr, "fuse: copy from pipe: %s\n", strerror(-res));
2440 return res;
2442 if (res < fuse_buf_size(dst)) {
2443 fprintf(stderr, "fuse: copy from pipe: short read\n");
2444 return -1;
2446 return 0;
2449 void fuse_session_process_buf(struct fuse_session *se,
2450 const struct fuse_buf *buf, struct fuse_chan *ch)
2452 struct fuse_ll *f = se->f;
2453 const size_t write_header_size = sizeof(struct fuse_in_header) +
2454 sizeof(struct fuse_write_in);
2455 struct fuse_bufvec bufv = { .buf[0] = *buf, .count = 1 };
2456 struct fuse_bufvec tmpbuf = FUSE_BUFVEC_INIT(write_header_size);
2457 struct fuse_in_header *in;
2458 const void *inarg;
2459 struct fuse_req *req;
2460 void *mbuf = NULL;
2461 int err;
2462 int res;
2464 if (buf->flags & FUSE_BUF_IS_FD) {
2465 if (buf->size < tmpbuf.buf[0].size)
2466 tmpbuf.buf[0].size = buf->size;
2468 mbuf = malloc(tmpbuf.buf[0].size);
2469 if (mbuf == NULL) {
2470 fprintf(stderr, "fuse: failed to allocate header\n");
2471 goto clear_pipe;
2473 tmpbuf.buf[0].mem = mbuf;
2475 res = fuse_ll_copy_from_pipe(&tmpbuf, &bufv);
2476 if (res < 0)
2477 goto clear_pipe;
2479 in = mbuf;
2480 } else {
2481 in = buf->mem;
2484 if (f->debug) {
2485 fprintf(stderr,
2486 "unique: %llu, opcode: %s (%i), nodeid: %llu, insize: %zu, pid: %u\n",
2487 (unsigned long long) in->unique,
2488 opname((enum fuse_opcode) in->opcode), in->opcode,
2489 (unsigned long long) in->nodeid, buf->size, in->pid);
2492 req = fuse_ll_alloc_req(f);
2493 if (req == NULL) {
2494 struct fuse_out_header out = {
2495 .unique = in->unique,
2496 .error = -ENOMEM,
2498 struct iovec iov = {
2499 .iov_base = &out,
2500 .iov_len = sizeof(struct fuse_out_header),
2503 fuse_send_msg(f, ch, &iov, 1);
2504 goto clear_pipe;
2507 req->unique = in->unique;
2508 req->ctx.uid = in->uid;
2509 req->ctx.gid = in->gid;
2510 req->ctx.pid = in->pid;
2511 req->ch = ch;
2513 err = EIO;
2514 if (!f->got_init) {
2515 enum fuse_opcode expected;
2517 expected = f->cuse_data ? CUSE_INIT : FUSE_INIT;
2518 if (in->opcode != expected)
2519 goto reply_err;
2520 } else if (in->opcode == FUSE_INIT || in->opcode == CUSE_INIT)
2521 goto reply_err;
2523 err = EACCES;
2524 if (f->allow_root && in->uid != f->owner && in->uid != 0 &&
2525 in->opcode != FUSE_INIT && in->opcode != FUSE_READ &&
2526 in->opcode != FUSE_WRITE && in->opcode != FUSE_FSYNC &&
2527 in->opcode != FUSE_RELEASE && in->opcode != FUSE_READDIR &&
2528 in->opcode != FUSE_FSYNCDIR && in->opcode != FUSE_RELEASEDIR &&
2529 in->opcode != FUSE_NOTIFY_REPLY &&
2530 in->opcode != FUSE_READDIRPLUS)
2531 goto reply_err;
2533 err = ENOSYS;
2534 if (in->opcode >= FUSE_MAXOP || !fuse_ll_ops[in->opcode].func)
2535 goto reply_err;
2536 if (in->opcode != FUSE_INTERRUPT) {
2537 struct fuse_req *intr;
2538 pthread_mutex_lock(&f->lock);
2539 intr = check_interrupt(f, req);
2540 list_add_req(req, &f->list);
2541 pthread_mutex_unlock(&f->lock);
2542 if (intr)
2543 fuse_reply_err(intr, EAGAIN);
2546 if ((buf->flags & FUSE_BUF_IS_FD) && write_header_size < buf->size &&
2547 (in->opcode != FUSE_WRITE || !f->op.write_buf) &&
2548 in->opcode != FUSE_NOTIFY_REPLY) {
2549 void *newmbuf;
2551 err = ENOMEM;
2552 newmbuf = realloc(mbuf, buf->size);
2553 if (newmbuf == NULL)
2554 goto reply_err;
2555 mbuf = newmbuf;
2557 tmpbuf = FUSE_BUFVEC_INIT(buf->size - write_header_size);
2558 tmpbuf.buf[0].mem = mbuf + write_header_size;
2560 res = fuse_ll_copy_from_pipe(&tmpbuf, &bufv);
2561 err = -res;
2562 if (res < 0)
2563 goto reply_err;
2565 in = mbuf;
2568 inarg = (void *) &in[1];
2569 if (in->opcode == FUSE_WRITE && f->op.write_buf)
2570 do_write_buf(req, in->nodeid, inarg, buf);
2571 else if (in->opcode == FUSE_NOTIFY_REPLY)
2572 do_notify_reply(req, in->nodeid, inarg, buf);
2573 else
2574 fuse_ll_ops[in->opcode].func(req, in->nodeid, inarg);
2576 out_free:
2577 free(mbuf);
2578 return;
2580 reply_err:
2581 fuse_reply_err(req, err);
2582 clear_pipe:
2583 if (buf->flags & FUSE_BUF_IS_FD)
2584 fuse_ll_clear_pipe(f);
2585 goto out_free;
2588 enum {
2589 KEY_HELP,
2590 KEY_VERSION,
2593 static const struct fuse_opt fuse_ll_opts[] = {
2594 { "debug", offsetof(struct fuse_ll, debug), 1 },
2595 { "-d", offsetof(struct fuse_ll, debug), 1 },
2596 { "allow_root", offsetof(struct fuse_ll, allow_root), 1 },
2597 { "max_write=%u", offsetof(struct fuse_ll, conn.max_write), 0 },
2598 { "max_readahead=%u", offsetof(struct fuse_ll, conn.max_readahead), 0 },
2599 { "max_background=%u", offsetof(struct fuse_ll, conn.max_background), 0 },
2600 { "congestion_threshold=%u",
2601 offsetof(struct fuse_ll, conn.congestion_threshold), 0 },
2602 { "async_read", offsetof(struct fuse_ll, conn.async_read), 1 },
2603 { "sync_read", offsetof(struct fuse_ll, conn.async_read), 0 },
2604 { "atomic_o_trunc", offsetof(struct fuse_ll, atomic_o_trunc), 1},
2605 { "no_remote_lock", offsetof(struct fuse_ll, no_remote_posix_lock), 1},
2606 { "no_remote_lock", offsetof(struct fuse_ll, no_remote_flock), 1},
2607 { "no_remote_flock", offsetof(struct fuse_ll, no_remote_flock), 1},
2608 { "no_remote_posix_lock", offsetof(struct fuse_ll, no_remote_posix_lock), 1},
2609 { "big_writes", offsetof(struct fuse_ll, big_writes), 1},
2610 { "splice_write", offsetof(struct fuse_ll, splice_write), 1},
2611 { "no_splice_write", offsetof(struct fuse_ll, no_splice_write), 1},
2612 { "splice_move", offsetof(struct fuse_ll, splice_move), 1},
2613 { "no_splice_move", offsetof(struct fuse_ll, no_splice_move), 1},
2614 { "splice_read", offsetof(struct fuse_ll, splice_read), 1},
2615 { "no_splice_read", offsetof(struct fuse_ll, no_splice_read), 1},
2616 { "auto_inval_data", offsetof(struct fuse_ll, auto_inval_data), 1},
2617 { "no_auto_inval_data", offsetof(struct fuse_ll, no_auto_inval_data), 1},
2618 { "readdirplus=no", offsetof(struct fuse_ll, no_readdirplus), 1},
2619 { "readdirplus=yes", offsetof(struct fuse_ll, no_readdirplus), 0},
2620 { "readdirplus=yes", offsetof(struct fuse_ll, no_readdirplus_auto), 1},
2621 { "readdirplus=auto", offsetof(struct fuse_ll, no_readdirplus), 0},
2622 { "readdirplus=auto", offsetof(struct fuse_ll, no_readdirplus_auto), 0},
2623 FUSE_OPT_KEY("max_read=", FUSE_OPT_KEY_DISCARD),
2624 FUSE_OPT_KEY("-h", KEY_HELP),
2625 FUSE_OPT_KEY("--help", KEY_HELP),
2626 FUSE_OPT_KEY("-V", KEY_VERSION),
2627 FUSE_OPT_KEY("--version", KEY_VERSION),
2628 FUSE_OPT_END
2631 static void fuse_ll_version(void)
2633 fprintf(stderr, "using FUSE kernel interface version %i.%i\n",
2634 FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
2637 static void fuse_ll_help(void)
2639 fprintf(stderr,
2640 " -o max_write=N set maximum size of write requests\n"
2641 " -o max_readahead=N set maximum readahead\n"
2642 " -o max_background=N set number of maximum background requests\n"
2643 " -o congestion_threshold=N set kernel's congestion threshold\n"
2644 " -o async_read perform reads asynchronously (default)\n"
2645 " -o sync_read perform reads synchronously\n"
2646 " -o atomic_o_trunc enable atomic open+truncate support\n"
2647 " -o big_writes enable larger than 4kB writes\n"
2648 " -o no_remote_lock disable remote file locking\n"
2649 " -o no_remote_flock disable remote file locking (BSD)\n"
2650 " -o no_remote_posix_lock disable remove file locking (POSIX)\n"
2651 " -o [no_]splice_write use splice to write to the fuse device\n"
2652 " -o [no_]splice_move move data while splicing to the fuse device\n"
2653 " -o [no_]splice_read use splice to read from the fuse device\n"
2654 " -o [no_]auto_inval_data use automatic kernel cache invalidation logic\n"
2655 " -o readdirplus=S control readdirplus use (yes|no|auto)\n"
2659 static int fuse_ll_opt_proc(void *data, const char *arg, int key,
2660 struct fuse_args *outargs)
2662 (void) data; (void) outargs;
2664 switch (key) {
2665 case KEY_HELP:
2666 fuse_ll_help();
2667 break;
2669 case KEY_VERSION:
2670 fuse_ll_version();
2671 break;
2673 default:
2674 fprintf(stderr, "fuse: unknown option `%s'\n", arg);
2677 return -1;
2680 static void fuse_ll_destroy(struct fuse_ll *f)
2682 struct fuse_ll_pipe *llp;
2684 if (f->got_init && !f->got_destroy) {
2685 if (f->op.destroy)
2686 f->op.destroy(f->userdata);
2688 llp = pthread_getspecific(f->pipe_key);
2689 if (llp != NULL)
2690 fuse_ll_pipe_free(llp);
2691 pthread_key_delete(f->pipe_key);
2692 pthread_mutex_destroy(&f->lock);
2693 free(f->cuse_data);
2694 free(f);
2697 void fuse_session_destroy(struct fuse_session *se)
2699 fuse_ll_destroy(se->f);
2700 if (se->ch != NULL)
2701 fuse_chan_destroy(se->ch);
2702 free(se);
2706 static void fuse_ll_pipe_destructor(void *data)
2708 struct fuse_ll_pipe *llp = data;
2709 fuse_ll_pipe_free(llp);
2712 #ifdef HAVE_SPLICE
2713 int fuse_session_receive_buf(struct fuse_session *se, struct fuse_buf *buf,
2714 struct fuse_chan *ch)
2716 struct fuse_ll *f = se->f;
2717 size_t bufsize = buf->size = f->bufsize;
2718 struct fuse_ll_pipe *llp;
2719 struct fuse_buf tmpbuf;
2720 int err;
2721 int res;
2723 if (f->conn.proto_minor < 14 || !(f->conn.want & FUSE_CAP_SPLICE_READ))
2724 goto fallback;
2726 llp = fuse_ll_get_pipe(f);
2727 if (llp == NULL)
2728 goto fallback;
2730 if (llp->size < bufsize) {
2731 if (llp->can_grow) {
2732 res = fcntl(llp->pipe[0], F_SETPIPE_SZ, bufsize);
2733 if (res == -1) {
2734 llp->can_grow = 0;
2735 goto fallback;
2737 llp->size = res;
2739 if (llp->size < bufsize)
2740 goto fallback;
2743 res = splice(fuse_chan_fd(ch), NULL, llp->pipe[1], NULL, bufsize, 0);
2744 err = errno;
2746 if (fuse_session_exited(se))
2747 return 0;
2749 if (res == -1) {
2750 if (err == ENODEV) {
2751 fuse_session_exit(se);
2752 return 0;
2754 if (err != EINTR && err != EAGAIN)
2755 perror("fuse: splice from device");
2756 return -err;
2759 if (res < sizeof(struct fuse_in_header)) {
2760 fprintf(stderr, "short splice from fuse device\n");
2761 return -EIO;
2764 tmpbuf = (struct fuse_buf) {
2765 .size = res,
2766 .flags = FUSE_BUF_IS_FD,
2767 .fd = llp->pipe[0],
2771 * Don't bother with zero copy for small requests.
2772 * fuse_loop_mt() needs to check for FORGET so this more than
2773 * just an optimization.
2775 if (res < sizeof(struct fuse_in_header) +
2776 sizeof(struct fuse_write_in) + pagesize) {
2777 struct fuse_bufvec src = { .buf[0] = tmpbuf, .count = 1 };
2778 struct fuse_bufvec dst = { .buf[0] = *buf, .count = 1 };
2780 res = fuse_buf_copy(&dst, &src, 0);
2781 if (res < 0) {
2782 fprintf(stderr, "fuse: copy from pipe: %s\n",
2783 strerror(-res));
2784 fuse_ll_clear_pipe(f);
2785 return res;
2787 if (res < tmpbuf.size) {
2788 fprintf(stderr, "fuse: copy from pipe: short read\n");
2789 fuse_ll_clear_pipe(f);
2790 return -EIO;
2792 buf->size = tmpbuf.size;
2793 return buf->size;
2796 *buf = tmpbuf;
2798 return res;
2800 fallback:
2801 return fuse_chan_recv(se, buf, ch);
2803 #else
2804 int fuse_session_receive_buf(struct fuse_session *se, struct fuse_buf *buf,
2805 struct fuse_chan *ch)
2807 return fuse_chan_recv(se, buf, ch);
2809 #endif
2811 #define MIN_BUFSIZE 0x21000
2813 struct fuse_session *fuse_lowlevel_new(struct fuse_args *args,
2814 const struct fuse_lowlevel_ops *op,
2815 size_t op_size, void *userdata)
2817 int err;
2818 struct fuse_ll *f;
2819 struct fuse_session *se;
2821 if (sizeof(struct fuse_lowlevel_ops) < op_size) {
2822 fprintf(stderr, "fuse: warning: library too old, some operations may not work\n");
2823 op_size = sizeof(struct fuse_lowlevel_ops);
2826 f = (struct fuse_ll *) calloc(1, sizeof(struct fuse_ll));
2827 if (f == NULL) {
2828 fprintf(stderr, "fuse: failed to allocate fuse object\n");
2829 goto out;
2832 f->conn.async_read = 1;
2833 f->conn.max_write = UINT_MAX;
2834 f->conn.max_readahead = UINT_MAX;
2835 f->atomic_o_trunc = 0;
2836 f->bufsize = getpagesize() + 0x1000;
2837 f->bufsize = f->bufsize < MIN_BUFSIZE ? MIN_BUFSIZE : f->bufsize;
2839 list_init_req(&f->list);
2840 list_init_req(&f->interrupts);
2841 list_init_nreq(&f->notify_list);
2842 f->notify_ctr = 1;
2843 fuse_mutex_init(&f->lock);
2845 err = pthread_key_create(&f->pipe_key, fuse_ll_pipe_destructor);
2846 if (err) {
2847 fprintf(stderr, "fuse: failed to create thread specific key: %s\n",
2848 strerror(err));
2849 goto out_free;
2852 if (fuse_opt_parse(args, f, fuse_ll_opts, fuse_ll_opt_proc) == -1)
2853 goto out_key_destroy;
2855 if (f->debug)
2856 fprintf(stderr, "FUSE library version: %s\n", PACKAGE_VERSION);
2858 memcpy(&f->op, op, op_size);
2859 f->owner = getuid();
2860 f->userdata = userdata;
2862 se = fuse_session_new();
2863 if (!se)
2864 goto out_key_destroy;
2866 se->f = f;
2868 return se;
2870 out_key_destroy:
2871 pthread_key_delete(f->pipe_key);
2872 out_free:
2873 pthread_mutex_destroy(&f->lock);
2874 free(f);
2875 out:
2876 return NULL;
2879 #ifdef linux
2880 int fuse_req_getgroups(fuse_req_t req, int size, gid_t list[])
2882 char *buf;
2883 size_t bufsize = 1024;
2884 char path[128];
2885 int ret;
2886 int fd;
2887 unsigned long pid = req->ctx.pid;
2888 char *s;
2890 sprintf(path, "/proc/%lu/task/%lu/status", pid, pid);
2892 retry:
2893 buf = malloc(bufsize);
2894 if (buf == NULL)
2895 return -ENOMEM;
2897 ret = -EIO;
2898 fd = open(path, O_RDONLY);
2899 if (fd == -1)
2900 goto out_free;
2902 ret = read(fd, buf, bufsize);
2903 close(fd);
2904 if (ret == -1) {
2905 ret = -EIO;
2906 goto out_free;
2909 if (ret == bufsize) {
2910 free(buf);
2911 bufsize *= 4;
2912 goto retry;
2915 ret = -EIO;
2916 s = strstr(buf, "\nGroups:");
2917 if (s == NULL)
2918 goto out_free;
2920 s += 8;
2921 ret = 0;
2922 while (1) {
2923 char *end;
2924 unsigned long val = strtoul(s, &end, 0);
2925 if (end == s)
2926 break;
2928 s = end;
2929 if (ret < size)
2930 list[ret] = val;
2931 ret++;
2934 out_free:
2935 free(buf);
2936 return ret;
2938 #else /* linux */
2940 * This is currently not implemented on other than Linux...
2942 int fuse_req_getgroups(fuse_req_t req, int size, gid_t list[])
2944 return -ENOSYS;
2946 #endif