virtiofsd: Add fuse_lowlevel.c
[qemu/kevin.git] / tools / virtiofsd / fuse_lowlevel.c
blobf2d7038e34dd15b0ed349e6b14f73518044562a3
1 /*
2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2007 Miklos Szeredi <miklos@szeredi.hu>
5 Implementation of (most of) the low-level FUSE API. The session loop
6 functions are implemented in separate files.
8 This program can be distributed under the terms of the GNU LGPLv2.
9 See the file COPYING.LIB
12 #define _GNU_SOURCE
14 #include "config.h"
15 #include "fuse_i.h"
16 #include "fuse_kernel.h"
17 #include "fuse_opt.h"
18 #include "fuse_misc.h"
19 #include "mount_util.h"
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <stddef.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <limits.h>
27 #include <errno.h>
28 #include <assert.h>
29 #include <sys/file.h>
31 #ifndef F_LINUX_SPECIFIC_BASE
32 #define F_LINUX_SPECIFIC_BASE 1024
33 #endif
34 #ifndef F_SETPIPE_SZ
35 #define F_SETPIPE_SZ (F_LINUX_SPECIFIC_BASE + 7)
36 #endif
39 #define PARAM(inarg) (((char *)(inarg)) + sizeof(*(inarg)))
40 #define OFFSET_MAX 0x7fffffffffffffffLL
42 #define container_of(ptr, type, member) ({ \
43 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
44 (type *)( (char *)__mptr - offsetof(type,member) );})
46 struct fuse_pollhandle {
47 uint64_t kh;
48 struct fuse_session *se;
51 static size_t pagesize;
53 static __attribute__((constructor)) void fuse_ll_init_pagesize(void)
55 pagesize = getpagesize();
58 static void convert_stat(const struct stat *stbuf, struct fuse_attr *attr)
60 attr->ino = stbuf->st_ino;
61 attr->mode = stbuf->st_mode;
62 attr->nlink = stbuf->st_nlink;
63 attr->uid = stbuf->st_uid;
64 attr->gid = stbuf->st_gid;
65 attr->rdev = stbuf->st_rdev;
66 attr->size = stbuf->st_size;
67 attr->blksize = stbuf->st_blksize;
68 attr->blocks = stbuf->st_blocks;
69 attr->atime = stbuf->st_atime;
70 attr->mtime = stbuf->st_mtime;
71 attr->ctime = stbuf->st_ctime;
72 attr->atimensec = ST_ATIM_NSEC(stbuf);
73 attr->mtimensec = ST_MTIM_NSEC(stbuf);
74 attr->ctimensec = ST_CTIM_NSEC(stbuf);
77 static void convert_attr(const struct fuse_setattr_in *attr, struct stat *stbuf)
79 stbuf->st_mode = attr->mode;
80 stbuf->st_uid = attr->uid;
81 stbuf->st_gid = attr->gid;
82 stbuf->st_size = attr->size;
83 stbuf->st_atime = attr->atime;
84 stbuf->st_mtime = attr->mtime;
85 stbuf->st_ctime = attr->ctime;
86 ST_ATIM_NSEC_SET(stbuf, attr->atimensec);
87 ST_MTIM_NSEC_SET(stbuf, attr->mtimensec);
88 ST_CTIM_NSEC_SET(stbuf, attr->ctimensec);
91 static size_t iov_length(const struct iovec *iov, size_t count)
93 size_t seg;
94 size_t ret = 0;
96 for (seg = 0; seg < count; seg++)
97 ret += iov[seg].iov_len;
98 return ret;
101 static void list_init_req(struct fuse_req *req)
103 req->next = req;
104 req->prev = req;
107 static void list_del_req(struct fuse_req *req)
109 struct fuse_req *prev = req->prev;
110 struct fuse_req *next = req->next;
111 prev->next = next;
112 next->prev = prev;
115 static void list_add_req(struct fuse_req *req, struct fuse_req *next)
117 struct fuse_req *prev = next->prev;
118 req->next = next;
119 req->prev = prev;
120 prev->next = req;
121 next->prev = req;
124 static void destroy_req(fuse_req_t req)
126 pthread_mutex_destroy(&req->lock);
127 free(req);
130 void fuse_free_req(fuse_req_t req)
132 int ctr;
133 struct fuse_session *se = req->se;
135 pthread_mutex_lock(&se->lock);
136 req->u.ni.func = NULL;
137 req->u.ni.data = NULL;
138 list_del_req(req);
139 ctr = --req->ctr;
140 fuse_chan_put(req->ch);
141 req->ch = NULL;
142 pthread_mutex_unlock(&se->lock);
143 if (!ctr)
144 destroy_req(req);
147 static struct fuse_req *fuse_ll_alloc_req(struct fuse_session *se)
149 struct fuse_req *req;
151 req = (struct fuse_req *) calloc(1, sizeof(struct fuse_req));
152 if (req == NULL) {
153 fuse_log(FUSE_LOG_ERR, "fuse: failed to allocate request\n");
154 } else {
155 req->se = se;
156 req->ctr = 1;
157 list_init_req(req);
158 fuse_mutex_init(&req->lock);
161 return req;
164 /* Send data. If *ch* is NULL, send via session master fd */
165 static int fuse_send_msg(struct fuse_session *se, struct fuse_chan *ch,
166 struct iovec *iov, int count)
168 struct fuse_out_header *out = iov[0].iov_base;
170 out->len = iov_length(iov, count);
171 if (se->debug) {
172 if (out->unique == 0) {
173 fuse_log(FUSE_LOG_DEBUG, "NOTIFY: code=%d length=%u\n",
174 out->error, out->len);
175 } else if (out->error) {
176 fuse_log(FUSE_LOG_DEBUG,
177 " unique: %llu, error: %i (%s), outsize: %i\n",
178 (unsigned long long) out->unique, out->error,
179 strerror(-out->error), out->len);
180 } else {
181 fuse_log(FUSE_LOG_DEBUG,
182 " unique: %llu, success, outsize: %i\n",
183 (unsigned long long) out->unique, out->len);
187 ssize_t res = writev(ch ? ch->fd : se->fd,
188 iov, count);
189 int err = errno;
191 if (res == -1) {
192 assert(se != NULL);
194 /* ENOENT means the operation was interrupted */
195 if (!fuse_session_exited(se) && err != ENOENT)
196 perror("fuse: writing device");
197 return -err;
200 return 0;
204 int fuse_send_reply_iov_nofree(fuse_req_t req, int error, struct iovec *iov,
205 int count)
207 struct fuse_out_header out;
209 if (error <= -1000 || error > 0) {
210 fuse_log(FUSE_LOG_ERR, "fuse: bad error value: %i\n", error);
211 error = -ERANGE;
214 out.unique = req->unique;
215 out.error = error;
217 iov[0].iov_base = &out;
218 iov[0].iov_len = sizeof(struct fuse_out_header);
220 return fuse_send_msg(req->se, req->ch, iov, count);
223 static int send_reply_iov(fuse_req_t req, int error, struct iovec *iov,
224 int count)
226 int res;
228 res = fuse_send_reply_iov_nofree(req, error, iov, count);
229 fuse_free_req(req);
230 return res;
233 static int send_reply(fuse_req_t req, int error, const void *arg,
234 size_t argsize)
236 struct iovec iov[2];
237 int count = 1;
238 if (argsize) {
239 iov[1].iov_base = (void *) arg;
240 iov[1].iov_len = argsize;
241 count++;
243 return send_reply_iov(req, error, iov, count);
246 int fuse_reply_iov(fuse_req_t req, const struct iovec *iov, int count)
248 int res;
249 struct iovec *padded_iov;
251 padded_iov = malloc((count + 1) * sizeof(struct iovec));
252 if (padded_iov == NULL)
253 return fuse_reply_err(req, ENOMEM);
255 memcpy(padded_iov + 1, iov, count * sizeof(struct iovec));
256 count++;
258 res = send_reply_iov(req, 0, padded_iov, count);
259 free(padded_iov);
261 return res;
265 /* `buf` is allowed to be empty so that the proper size may be
266 allocated by the caller */
267 size_t fuse_add_direntry(fuse_req_t req, char *buf, size_t bufsize,
268 const char *name, const struct stat *stbuf, off_t off)
270 (void)req;
271 size_t namelen;
272 size_t entlen;
273 size_t entlen_padded;
274 struct fuse_dirent *dirent;
276 namelen = strlen(name);
277 entlen = FUSE_NAME_OFFSET + namelen;
278 entlen_padded = FUSE_DIRENT_ALIGN(entlen);
280 if ((buf == NULL) || (entlen_padded > bufsize))
281 return entlen_padded;
283 dirent = (struct fuse_dirent*) buf;
284 dirent->ino = stbuf->st_ino;
285 dirent->off = off;
286 dirent->namelen = namelen;
287 dirent->type = (stbuf->st_mode & S_IFMT) >> 12;
288 memcpy(dirent->name, name, namelen);
289 memset(dirent->name + namelen, 0, entlen_padded - entlen);
291 return entlen_padded;
294 static void convert_statfs(const struct statvfs *stbuf,
295 struct fuse_kstatfs *kstatfs)
297 kstatfs->bsize = stbuf->f_bsize;
298 kstatfs->frsize = stbuf->f_frsize;
299 kstatfs->blocks = stbuf->f_blocks;
300 kstatfs->bfree = stbuf->f_bfree;
301 kstatfs->bavail = stbuf->f_bavail;
302 kstatfs->files = stbuf->f_files;
303 kstatfs->ffree = stbuf->f_ffree;
304 kstatfs->namelen = stbuf->f_namemax;
307 static int send_reply_ok(fuse_req_t req, const void *arg, size_t argsize)
309 return send_reply(req, 0, arg, argsize);
312 int fuse_reply_err(fuse_req_t req, int err)
314 return send_reply(req, -err, NULL, 0);
317 void fuse_reply_none(fuse_req_t req)
319 fuse_free_req(req);
322 static unsigned long calc_timeout_sec(double t)
324 if (t > (double) ULONG_MAX)
325 return ULONG_MAX;
326 else if (t < 0.0)
327 return 0;
328 else
329 return (unsigned long) t;
332 static unsigned int calc_timeout_nsec(double t)
334 double f = t - (double) calc_timeout_sec(t);
335 if (f < 0.0)
336 return 0;
337 else if (f >= 0.999999999)
338 return 999999999;
339 else
340 return (unsigned int) (f * 1.0e9);
343 static void fill_entry(struct fuse_entry_out *arg,
344 const struct fuse_entry_param *e)
346 arg->nodeid = e->ino;
347 arg->generation = e->generation;
348 arg->entry_valid = calc_timeout_sec(e->entry_timeout);
349 arg->entry_valid_nsec = calc_timeout_nsec(e->entry_timeout);
350 arg->attr_valid = calc_timeout_sec(e->attr_timeout);
351 arg->attr_valid_nsec = calc_timeout_nsec(e->attr_timeout);
352 convert_stat(&e->attr, &arg->attr);
355 /* `buf` is allowed to be empty so that the proper size may be
356 allocated by the caller */
357 size_t fuse_add_direntry_plus(fuse_req_t req, char *buf, size_t bufsize,
358 const char *name,
359 const struct fuse_entry_param *e, off_t off)
361 (void)req;
362 size_t namelen;
363 size_t entlen;
364 size_t entlen_padded;
366 namelen = strlen(name);
367 entlen = FUSE_NAME_OFFSET_DIRENTPLUS + namelen;
368 entlen_padded = FUSE_DIRENT_ALIGN(entlen);
369 if ((buf == NULL) || (entlen_padded > bufsize))
370 return entlen_padded;
372 struct fuse_direntplus *dp = (struct fuse_direntplus *) buf;
373 memset(&dp->entry_out, 0, sizeof(dp->entry_out));
374 fill_entry(&dp->entry_out, e);
376 struct fuse_dirent *dirent = &dp->dirent;
377 dirent->ino = e->attr.st_ino;
378 dirent->off = off;
379 dirent->namelen = namelen;
380 dirent->type = (e->attr.st_mode & S_IFMT) >> 12;
381 memcpy(dirent->name, name, namelen);
382 memset(dirent->name + namelen, 0, entlen_padded - entlen);
384 return entlen_padded;
387 static void fill_open(struct fuse_open_out *arg,
388 const struct fuse_file_info *f)
390 arg->fh = f->fh;
391 if (f->direct_io)
392 arg->open_flags |= FOPEN_DIRECT_IO;
393 if (f->keep_cache)
394 arg->open_flags |= FOPEN_KEEP_CACHE;
395 if (f->cache_readdir)
396 arg->open_flags |= FOPEN_CACHE_DIR;
397 if (f->nonseekable)
398 arg->open_flags |= FOPEN_NONSEEKABLE;
401 int fuse_reply_entry(fuse_req_t req, const struct fuse_entry_param *e)
403 struct fuse_entry_out arg;
404 size_t size = req->se->conn.proto_minor < 9 ?
405 FUSE_COMPAT_ENTRY_OUT_SIZE : sizeof(arg);
407 /* before ABI 7.4 e->ino == 0 was invalid, only ENOENT meant
408 negative entry */
409 if (!e->ino && req->se->conn.proto_minor < 4)
410 return fuse_reply_err(req, ENOENT);
412 memset(&arg, 0, sizeof(arg));
413 fill_entry(&arg, e);
414 return send_reply_ok(req, &arg, size);
417 int fuse_reply_create(fuse_req_t req, const struct fuse_entry_param *e,
418 const struct fuse_file_info *f)
420 char buf[sizeof(struct fuse_entry_out) + sizeof(struct fuse_open_out)];
421 size_t entrysize = req->se->conn.proto_minor < 9 ?
422 FUSE_COMPAT_ENTRY_OUT_SIZE : sizeof(struct fuse_entry_out);
423 struct fuse_entry_out *earg = (struct fuse_entry_out *) buf;
424 struct fuse_open_out *oarg = (struct fuse_open_out *) (buf + entrysize);
426 memset(buf, 0, sizeof(buf));
427 fill_entry(earg, e);
428 fill_open(oarg, f);
429 return send_reply_ok(req, buf,
430 entrysize + sizeof(struct fuse_open_out));
433 int fuse_reply_attr(fuse_req_t req, const struct stat *attr,
434 double attr_timeout)
436 struct fuse_attr_out arg;
437 size_t size = req->se->conn.proto_minor < 9 ?
438 FUSE_COMPAT_ATTR_OUT_SIZE : sizeof(arg);
440 memset(&arg, 0, sizeof(arg));
441 arg.attr_valid = calc_timeout_sec(attr_timeout);
442 arg.attr_valid_nsec = calc_timeout_nsec(attr_timeout);
443 convert_stat(attr, &arg.attr);
445 return send_reply_ok(req, &arg, size);
448 int fuse_reply_readlink(fuse_req_t req, const char *linkname)
450 return send_reply_ok(req, linkname, strlen(linkname));
453 int fuse_reply_open(fuse_req_t req, const struct fuse_file_info *f)
455 struct fuse_open_out arg;
457 memset(&arg, 0, sizeof(arg));
458 fill_open(&arg, f);
459 return send_reply_ok(req, &arg, sizeof(arg));
462 int fuse_reply_write(fuse_req_t req, size_t count)
464 struct fuse_write_out arg;
466 memset(&arg, 0, sizeof(arg));
467 arg.size = count;
469 return send_reply_ok(req, &arg, sizeof(arg));
472 int fuse_reply_buf(fuse_req_t req, const char *buf, size_t size)
474 return send_reply_ok(req, buf, size);
477 static int fuse_send_data_iov_fallback(struct fuse_session *se,
478 struct fuse_chan *ch,
479 struct iovec *iov, int iov_count,
480 struct fuse_bufvec *buf,
481 size_t len)
483 struct fuse_bufvec mem_buf = FUSE_BUFVEC_INIT(len);
484 void *mbuf;
485 int res;
487 /* Optimize common case */
488 if (buf->count == 1 && buf->idx == 0 && buf->off == 0 &&
489 !(buf->buf[0].flags & FUSE_BUF_IS_FD)) {
490 /* FIXME: also avoid memory copy if there are multiple buffers
491 but none of them contain an fd */
493 iov[iov_count].iov_base = buf->buf[0].mem;
494 iov[iov_count].iov_len = len;
495 iov_count++;
496 return fuse_send_msg(se, ch, iov, iov_count);
499 res = posix_memalign(&mbuf, pagesize, len);
500 if (res != 0)
501 return res;
503 mem_buf.buf[0].mem = mbuf;
504 res = fuse_buf_copy(&mem_buf, buf, 0);
505 if (res < 0) {
506 free(mbuf);
507 return -res;
509 len = res;
511 iov[iov_count].iov_base = mbuf;
512 iov[iov_count].iov_len = len;
513 iov_count++;
514 res = fuse_send_msg(se, ch, iov, iov_count);
515 free(mbuf);
517 return res;
520 struct fuse_ll_pipe {
521 size_t size;
522 int can_grow;
523 int pipe[2];
526 static void fuse_ll_pipe_free(struct fuse_ll_pipe *llp)
528 close(llp->pipe[0]);
529 close(llp->pipe[1]);
530 free(llp);
533 #ifdef HAVE_SPLICE
534 #if !defined(HAVE_PIPE2) || !defined(O_CLOEXEC)
535 static int fuse_pipe(int fds[2])
537 int rv = pipe(fds);
539 if (rv == -1)
540 return rv;
542 if (fcntl(fds[0], F_SETFL, O_NONBLOCK) == -1 ||
543 fcntl(fds[1], F_SETFL, O_NONBLOCK) == -1 ||
544 fcntl(fds[0], F_SETFD, FD_CLOEXEC) == -1 ||
545 fcntl(fds[1], F_SETFD, FD_CLOEXEC) == -1) {
546 close(fds[0]);
547 close(fds[1]);
548 rv = -1;
550 return rv;
552 #else
553 static int fuse_pipe(int fds[2])
555 return pipe2(fds, O_CLOEXEC | O_NONBLOCK);
557 #endif
559 static struct fuse_ll_pipe *fuse_ll_get_pipe(struct fuse_session *se)
561 struct fuse_ll_pipe *llp = pthread_getspecific(se->pipe_key);
562 if (llp == NULL) {
563 int res;
565 llp = malloc(sizeof(struct fuse_ll_pipe));
566 if (llp == NULL)
567 return NULL;
569 res = fuse_pipe(llp->pipe);
570 if (res == -1) {
571 free(llp);
572 return NULL;
576 *the default size is 16 pages on linux
578 llp->size = pagesize * 16;
579 llp->can_grow = 1;
581 pthread_setspecific(se->pipe_key, llp);
584 return llp;
586 #endif
588 static void fuse_ll_clear_pipe(struct fuse_session *se)
590 struct fuse_ll_pipe *llp = pthread_getspecific(se->pipe_key);
591 if (llp) {
592 pthread_setspecific(se->pipe_key, NULL);
593 fuse_ll_pipe_free(llp);
597 #if defined(HAVE_SPLICE) && defined(HAVE_VMSPLICE)
598 static int read_back(int fd, char *buf, size_t len)
600 int res;
602 res = read(fd, buf, len);
603 if (res == -1) {
604 fuse_log(FUSE_LOG_ERR, "fuse: internal error: failed to read back from pipe: %s\n", strerror(errno));
605 return -EIO;
607 if (res != len) {
608 fuse_log(FUSE_LOG_ERR, "fuse: internal error: short read back from pipe: %i from %zi\n", res, len);
609 return -EIO;
611 return 0;
614 static int grow_pipe_to_max(int pipefd)
616 int max;
617 int res;
618 int maxfd;
619 char buf[32];
621 maxfd = open("/proc/sys/fs/pipe-max-size", O_RDONLY);
622 if (maxfd < 0)
623 return -errno;
625 res = read(maxfd, buf, sizeof(buf) - 1);
626 if (res < 0) {
627 int saved_errno;
629 saved_errno = errno;
630 close(maxfd);
631 return -saved_errno;
633 close(maxfd);
634 buf[res] = '\0';
636 max = atoi(buf);
637 res = fcntl(pipefd, F_SETPIPE_SZ, max);
638 if (res < 0)
639 return -errno;
640 return max;
643 static int fuse_send_data_iov(struct fuse_session *se, struct fuse_chan *ch,
644 struct iovec *iov, int iov_count,
645 struct fuse_bufvec *buf, unsigned int flags)
647 int res;
648 size_t len = fuse_buf_size(buf);
649 struct fuse_out_header *out = iov[0].iov_base;
650 struct fuse_ll_pipe *llp;
651 int splice_flags;
652 size_t pipesize;
653 size_t total_fd_size;
654 size_t idx;
655 size_t headerlen;
656 struct fuse_bufvec pipe_buf = FUSE_BUFVEC_INIT(len);
658 if (se->broken_splice_nonblock)
659 goto fallback;
661 if (flags & FUSE_BUF_NO_SPLICE)
662 goto fallback;
664 total_fd_size = 0;
665 for (idx = buf->idx; idx < buf->count; idx++) {
666 if (buf->buf[idx].flags & FUSE_BUF_IS_FD) {
667 total_fd_size = buf->buf[idx].size;
668 if (idx == buf->idx)
669 total_fd_size -= buf->off;
672 if (total_fd_size < 2 * pagesize)
673 goto fallback;
675 if (se->conn.proto_minor < 14 ||
676 !(se->conn.want & FUSE_CAP_SPLICE_WRITE))
677 goto fallback;
679 llp = fuse_ll_get_pipe(se);
680 if (llp == NULL)
681 goto fallback;
684 headerlen = iov_length(iov, iov_count);
686 out->len = headerlen + len;
689 * Heuristic for the required pipe size, does not work if the
690 * source contains less than page size fragments
692 pipesize = pagesize * (iov_count + buf->count + 1) + out->len;
694 if (llp->size < pipesize) {
695 if (llp->can_grow) {
696 res = fcntl(llp->pipe[0], F_SETPIPE_SZ, pipesize);
697 if (res == -1) {
698 res = grow_pipe_to_max(llp->pipe[0]);
699 if (res > 0)
700 llp->size = res;
701 llp->can_grow = 0;
702 goto fallback;
704 llp->size = res;
706 if (llp->size < pipesize)
707 goto fallback;
711 res = vmsplice(llp->pipe[1], iov, iov_count, SPLICE_F_NONBLOCK);
712 if (res == -1)
713 goto fallback;
715 if (res != headerlen) {
716 res = -EIO;
717 fuse_log(FUSE_LOG_ERR, "fuse: short vmsplice to pipe: %u/%zu\n", res,
718 headerlen);
719 goto clear_pipe;
722 pipe_buf.buf[0].flags = FUSE_BUF_IS_FD;
723 pipe_buf.buf[0].fd = llp->pipe[1];
725 res = fuse_buf_copy(&pipe_buf, buf,
726 FUSE_BUF_FORCE_SPLICE | FUSE_BUF_SPLICE_NONBLOCK);
727 if (res < 0) {
728 if (res == -EAGAIN || res == -EINVAL) {
730 * Should only get EAGAIN on kernels with
731 * broken SPLICE_F_NONBLOCK support (<=
732 * 2.6.35) where this error or a short read is
733 * returned even if the pipe itself is not
734 * full
736 * EINVAL might mean that splice can't handle
737 * this combination of input and output.
739 if (res == -EAGAIN)
740 se->broken_splice_nonblock = 1;
742 pthread_setspecific(se->pipe_key, NULL);
743 fuse_ll_pipe_free(llp);
744 goto fallback;
746 res = -res;
747 goto clear_pipe;
750 if (res != 0 && res < len) {
751 struct fuse_bufvec mem_buf = FUSE_BUFVEC_INIT(len);
752 void *mbuf;
753 size_t now_len = res;
755 * For regular files a short count is either
756 * 1) due to EOF, or
757 * 2) because of broken SPLICE_F_NONBLOCK (see above)
759 * For other inputs it's possible that we overflowed
760 * the pipe because of small buffer fragments.
763 res = posix_memalign(&mbuf, pagesize, len);
764 if (res != 0)
765 goto clear_pipe;
767 mem_buf.buf[0].mem = mbuf;
768 mem_buf.off = now_len;
769 res = fuse_buf_copy(&mem_buf, buf, 0);
770 if (res > 0) {
771 char *tmpbuf;
772 size_t extra_len = res;
774 * Trickiest case: got more data. Need to get
775 * back the data from the pipe and then fall
776 * back to regular write.
778 tmpbuf = malloc(headerlen);
779 if (tmpbuf == NULL) {
780 free(mbuf);
781 res = ENOMEM;
782 goto clear_pipe;
784 res = read_back(llp->pipe[0], tmpbuf, headerlen);
785 free(tmpbuf);
786 if (res != 0) {
787 free(mbuf);
788 goto clear_pipe;
790 res = read_back(llp->pipe[0], mbuf, now_len);
791 if (res != 0) {
792 free(mbuf);
793 goto clear_pipe;
795 len = now_len + extra_len;
796 iov[iov_count].iov_base = mbuf;
797 iov[iov_count].iov_len = len;
798 iov_count++;
799 res = fuse_send_msg(se, ch, iov, iov_count);
800 free(mbuf);
801 return res;
803 free(mbuf);
804 res = now_len;
806 len = res;
807 out->len = headerlen + len;
809 if (se->debug) {
810 fuse_log(FUSE_LOG_DEBUG,
811 " unique: %llu, success, outsize: %i (splice)\n",
812 (unsigned long long) out->unique, out->len);
815 splice_flags = 0;
816 if ((flags & FUSE_BUF_SPLICE_MOVE) &&
817 (se->conn.want & FUSE_CAP_SPLICE_MOVE))
818 splice_flags |= SPLICE_F_MOVE;
820 res = splice(llp->pipe[0], NULL, ch ? ch->fd : se->fd,
821 NULL, out->len, splice_flags);
822 if (res == -1) {
823 res = -errno;
824 perror("fuse: splice from pipe");
825 goto clear_pipe;
827 if (res != out->len) {
828 res = -EIO;
829 fuse_log(FUSE_LOG_ERR, "fuse: short splice from pipe: %u/%u\n",
830 res, out->len);
831 goto clear_pipe;
833 return 0;
835 clear_pipe:
836 fuse_ll_clear_pipe(se);
837 return res;
839 fallback:
840 return fuse_send_data_iov_fallback(se, ch, iov, iov_count, buf, len);
842 #else
843 static int fuse_send_data_iov(struct fuse_session *se, struct fuse_chan *ch,
844 struct iovec *iov, int iov_count,
845 struct fuse_bufvec *buf, unsigned int flags)
847 size_t len = fuse_buf_size(buf);
848 (void) flags;
850 return fuse_send_data_iov_fallback(se, ch, iov, iov_count, buf, len);
852 #endif
854 int fuse_reply_data(fuse_req_t req, struct fuse_bufvec *bufv,
855 enum fuse_buf_copy_flags flags)
857 struct iovec iov[2];
858 struct fuse_out_header out;
859 int res;
861 iov[0].iov_base = &out;
862 iov[0].iov_len = sizeof(struct fuse_out_header);
864 out.unique = req->unique;
865 out.error = 0;
867 res = fuse_send_data_iov(req->se, req->ch, iov, 1, bufv, flags);
868 if (res <= 0) {
869 fuse_free_req(req);
870 return res;
871 } else {
872 return fuse_reply_err(req, res);
876 int fuse_reply_statfs(fuse_req_t req, const struct statvfs *stbuf)
878 struct fuse_statfs_out arg;
879 size_t size = req->se->conn.proto_minor < 4 ?
880 FUSE_COMPAT_STATFS_SIZE : sizeof(arg);
882 memset(&arg, 0, sizeof(arg));
883 convert_statfs(stbuf, &arg.st);
885 return send_reply_ok(req, &arg, size);
888 int fuse_reply_xattr(fuse_req_t req, size_t count)
890 struct fuse_getxattr_out arg;
892 memset(&arg, 0, sizeof(arg));
893 arg.size = count;
895 return send_reply_ok(req, &arg, sizeof(arg));
898 int fuse_reply_lock(fuse_req_t req, const struct flock *lock)
900 struct fuse_lk_out arg;
902 memset(&arg, 0, sizeof(arg));
903 arg.lk.type = lock->l_type;
904 if (lock->l_type != F_UNLCK) {
905 arg.lk.start = lock->l_start;
906 if (lock->l_len == 0)
907 arg.lk.end = OFFSET_MAX;
908 else
909 arg.lk.end = lock->l_start + lock->l_len - 1;
911 arg.lk.pid = lock->l_pid;
912 return send_reply_ok(req, &arg, sizeof(arg));
915 int fuse_reply_bmap(fuse_req_t req, uint64_t idx)
917 struct fuse_bmap_out arg;
919 memset(&arg, 0, sizeof(arg));
920 arg.block = idx;
922 return send_reply_ok(req, &arg, sizeof(arg));
925 static struct fuse_ioctl_iovec *fuse_ioctl_iovec_copy(const struct iovec *iov,
926 size_t count)
928 struct fuse_ioctl_iovec *fiov;
929 size_t i;
931 fiov = malloc(sizeof(fiov[0]) * count);
932 if (!fiov)
933 return NULL;
935 for (i = 0; i < count; i++) {
936 fiov[i].base = (uintptr_t) iov[i].iov_base;
937 fiov[i].len = iov[i].iov_len;
940 return fiov;
943 int fuse_reply_ioctl_retry(fuse_req_t req,
944 const struct iovec *in_iov, size_t in_count,
945 const struct iovec *out_iov, size_t out_count)
947 struct fuse_ioctl_out arg;
948 struct fuse_ioctl_iovec *in_fiov = NULL;
949 struct fuse_ioctl_iovec *out_fiov = NULL;
950 struct iovec iov[4];
951 size_t count = 1;
952 int res;
954 memset(&arg, 0, sizeof(arg));
955 arg.flags |= FUSE_IOCTL_RETRY;
956 arg.in_iovs = in_count;
957 arg.out_iovs = out_count;
958 iov[count].iov_base = &arg;
959 iov[count].iov_len = sizeof(arg);
960 count++;
962 if (req->se->conn.proto_minor < 16) {
963 if (in_count) {
964 iov[count].iov_base = (void *)in_iov;
965 iov[count].iov_len = sizeof(in_iov[0]) * in_count;
966 count++;
969 if (out_count) {
970 iov[count].iov_base = (void *)out_iov;
971 iov[count].iov_len = sizeof(out_iov[0]) * out_count;
972 count++;
974 } else {
975 /* Can't handle non-compat 64bit ioctls on 32bit */
976 if (sizeof(void *) == 4 && req->ioctl_64bit) {
977 res = fuse_reply_err(req, EINVAL);
978 goto out;
981 if (in_count) {
982 in_fiov = fuse_ioctl_iovec_copy(in_iov, in_count);
983 if (!in_fiov)
984 goto enomem;
986 iov[count].iov_base = (void *)in_fiov;
987 iov[count].iov_len = sizeof(in_fiov[0]) * in_count;
988 count++;
990 if (out_count) {
991 out_fiov = fuse_ioctl_iovec_copy(out_iov, out_count);
992 if (!out_fiov)
993 goto enomem;
995 iov[count].iov_base = (void *)out_fiov;
996 iov[count].iov_len = sizeof(out_fiov[0]) * out_count;
997 count++;
1001 res = send_reply_iov(req, 0, iov, count);
1002 out:
1003 free(in_fiov);
1004 free(out_fiov);
1006 return res;
1008 enomem:
1009 res = fuse_reply_err(req, ENOMEM);
1010 goto out;
1013 int fuse_reply_ioctl(fuse_req_t req, int result, const void *buf, size_t size)
1015 struct fuse_ioctl_out arg;
1016 struct iovec iov[3];
1017 size_t count = 1;
1019 memset(&arg, 0, sizeof(arg));
1020 arg.result = result;
1021 iov[count].iov_base = &arg;
1022 iov[count].iov_len = sizeof(arg);
1023 count++;
1025 if (size) {
1026 iov[count].iov_base = (char *) buf;
1027 iov[count].iov_len = size;
1028 count++;
1031 return send_reply_iov(req, 0, iov, count);
1034 int fuse_reply_ioctl_iov(fuse_req_t req, int result, const struct iovec *iov,
1035 int count)
1037 struct iovec *padded_iov;
1038 struct fuse_ioctl_out arg;
1039 int res;
1041 padded_iov = malloc((count + 2) * sizeof(struct iovec));
1042 if (padded_iov == NULL)
1043 return fuse_reply_err(req, ENOMEM);
1045 memset(&arg, 0, sizeof(arg));
1046 arg.result = result;
1047 padded_iov[1].iov_base = &arg;
1048 padded_iov[1].iov_len = sizeof(arg);
1050 memcpy(&padded_iov[2], iov, count * sizeof(struct iovec));
1052 res = send_reply_iov(req, 0, padded_iov, count + 2);
1053 free(padded_iov);
1055 return res;
1058 int fuse_reply_poll(fuse_req_t req, unsigned revents)
1060 struct fuse_poll_out arg;
1062 memset(&arg, 0, sizeof(arg));
1063 arg.revents = revents;
1065 return send_reply_ok(req, &arg, sizeof(arg));
1068 int fuse_reply_lseek(fuse_req_t req, off_t off)
1070 struct fuse_lseek_out arg;
1072 memset(&arg, 0, sizeof(arg));
1073 arg.offset = off;
1075 return send_reply_ok(req, &arg, sizeof(arg));
1078 static void do_lookup(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1080 char *name = (char *) inarg;
1082 if (req->se->op.lookup)
1083 req->se->op.lookup(req, nodeid, name);
1084 else
1085 fuse_reply_err(req, ENOSYS);
1088 static void do_forget(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1090 struct fuse_forget_in *arg = (struct fuse_forget_in *) inarg;
1092 if (req->se->op.forget)
1093 req->se->op.forget(req, nodeid, arg->nlookup);
1094 else
1095 fuse_reply_none(req);
1098 static void do_batch_forget(fuse_req_t req, fuse_ino_t nodeid,
1099 const void *inarg)
1101 struct fuse_batch_forget_in *arg = (void *) inarg;
1102 struct fuse_forget_one *param = (void *) PARAM(arg);
1103 unsigned int i;
1105 (void) nodeid;
1107 if (req->se->op.forget_multi) {
1108 req->se->op.forget_multi(req, arg->count,
1109 (struct fuse_forget_data *) param);
1110 } else if (req->se->op.forget) {
1111 for (i = 0; i < arg->count; i++) {
1112 struct fuse_forget_one *forget = &param[i];
1113 struct fuse_req *dummy_req;
1115 dummy_req = fuse_ll_alloc_req(req->se);
1116 if (dummy_req == NULL)
1117 break;
1119 dummy_req->unique = req->unique;
1120 dummy_req->ctx = req->ctx;
1121 dummy_req->ch = NULL;
1123 req->se->op.forget(dummy_req, forget->nodeid,
1124 forget->nlookup);
1126 fuse_reply_none(req);
1127 } else {
1128 fuse_reply_none(req);
1132 static void do_getattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1134 struct fuse_file_info *fip = NULL;
1135 struct fuse_file_info fi;
1137 if (req->se->conn.proto_minor >= 9) {
1138 struct fuse_getattr_in *arg = (struct fuse_getattr_in *) inarg;
1140 if (arg->getattr_flags & FUSE_GETATTR_FH) {
1141 memset(&fi, 0, sizeof(fi));
1142 fi.fh = arg->fh;
1143 fip = &fi;
1147 if (req->se->op.getattr)
1148 req->se->op.getattr(req, nodeid, fip);
1149 else
1150 fuse_reply_err(req, ENOSYS);
1153 static void do_setattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1155 struct fuse_setattr_in *arg = (struct fuse_setattr_in *) inarg;
1157 if (req->se->op.setattr) {
1158 struct fuse_file_info *fi = NULL;
1159 struct fuse_file_info fi_store;
1160 struct stat stbuf;
1161 memset(&stbuf, 0, sizeof(stbuf));
1162 convert_attr(arg, &stbuf);
1163 if (arg->valid & FATTR_FH) {
1164 arg->valid &= ~FATTR_FH;
1165 memset(&fi_store, 0, sizeof(fi_store));
1166 fi = &fi_store;
1167 fi->fh = arg->fh;
1169 arg->valid &=
1170 FUSE_SET_ATTR_MODE |
1171 FUSE_SET_ATTR_UID |
1172 FUSE_SET_ATTR_GID |
1173 FUSE_SET_ATTR_SIZE |
1174 FUSE_SET_ATTR_ATIME |
1175 FUSE_SET_ATTR_MTIME |
1176 FUSE_SET_ATTR_ATIME_NOW |
1177 FUSE_SET_ATTR_MTIME_NOW |
1178 FUSE_SET_ATTR_CTIME;
1180 req->se->op.setattr(req, nodeid, &stbuf, arg->valid, fi);
1181 } else
1182 fuse_reply_err(req, ENOSYS);
1185 static void do_access(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1187 struct fuse_access_in *arg = (struct fuse_access_in *) inarg;
1189 if (req->se->op.access)
1190 req->se->op.access(req, nodeid, arg->mask);
1191 else
1192 fuse_reply_err(req, ENOSYS);
1195 static void do_readlink(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1197 (void) inarg;
1199 if (req->se->op.readlink)
1200 req->se->op.readlink(req, nodeid);
1201 else
1202 fuse_reply_err(req, ENOSYS);
1205 static void do_mknod(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1207 struct fuse_mknod_in *arg = (struct fuse_mknod_in *) inarg;
1208 char *name = PARAM(arg);
1210 if (req->se->conn.proto_minor >= 12)
1211 req->ctx.umask = arg->umask;
1212 else
1213 name = (char *) inarg + FUSE_COMPAT_MKNOD_IN_SIZE;
1215 if (req->se->op.mknod)
1216 req->se->op.mknod(req, nodeid, name, arg->mode, arg->rdev);
1217 else
1218 fuse_reply_err(req, ENOSYS);
1221 static void do_mkdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1223 struct fuse_mkdir_in *arg = (struct fuse_mkdir_in *) inarg;
1225 if (req->se->conn.proto_minor >= 12)
1226 req->ctx.umask = arg->umask;
1228 if (req->se->op.mkdir)
1229 req->se->op.mkdir(req, nodeid, PARAM(arg), arg->mode);
1230 else
1231 fuse_reply_err(req, ENOSYS);
1234 static void do_unlink(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1236 char *name = (char *) inarg;
1238 if (req->se->op.unlink)
1239 req->se->op.unlink(req, nodeid, name);
1240 else
1241 fuse_reply_err(req, ENOSYS);
1244 static void do_rmdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1246 char *name = (char *) inarg;
1248 if (req->se->op.rmdir)
1249 req->se->op.rmdir(req, nodeid, name);
1250 else
1251 fuse_reply_err(req, ENOSYS);
1254 static void do_symlink(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1256 char *name = (char *) inarg;
1257 char *linkname = ((char *) inarg) + strlen((char *) inarg) + 1;
1259 if (req->se->op.symlink)
1260 req->se->op.symlink(req, linkname, nodeid, name);
1261 else
1262 fuse_reply_err(req, ENOSYS);
1265 static void do_rename(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1267 struct fuse_rename_in *arg = (struct fuse_rename_in *) inarg;
1268 char *oldname = PARAM(arg);
1269 char *newname = oldname + strlen(oldname) + 1;
1271 if (req->se->op.rename)
1272 req->se->op.rename(req, nodeid, oldname, arg->newdir, newname,
1274 else
1275 fuse_reply_err(req, ENOSYS);
1278 static void do_rename2(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1280 struct fuse_rename2_in *arg = (struct fuse_rename2_in *) inarg;
1281 char *oldname = PARAM(arg);
1282 char *newname = oldname + strlen(oldname) + 1;
1284 if (req->se->op.rename)
1285 req->se->op.rename(req, nodeid, oldname, arg->newdir, newname,
1286 arg->flags);
1287 else
1288 fuse_reply_err(req, ENOSYS);
1291 static void do_link(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1293 struct fuse_link_in *arg = (struct fuse_link_in *) inarg;
1295 if (req->se->op.link)
1296 req->se->op.link(req, arg->oldnodeid, nodeid, PARAM(arg));
1297 else
1298 fuse_reply_err(req, ENOSYS);
1301 static void do_create(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1303 struct fuse_create_in *arg = (struct fuse_create_in *) inarg;
1305 if (req->se->op.create) {
1306 struct fuse_file_info fi;
1307 char *name = PARAM(arg);
1309 memset(&fi, 0, sizeof(fi));
1310 fi.flags = arg->flags;
1312 if (req->se->conn.proto_minor >= 12)
1313 req->ctx.umask = arg->umask;
1314 else
1315 name = (char *) inarg + sizeof(struct fuse_open_in);
1317 req->se->op.create(req, nodeid, name, arg->mode, &fi);
1318 } else
1319 fuse_reply_err(req, ENOSYS);
1322 static void do_open(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1324 struct fuse_open_in *arg = (struct fuse_open_in *) inarg;
1325 struct fuse_file_info fi;
1327 memset(&fi, 0, sizeof(fi));
1328 fi.flags = arg->flags;
1330 if (req->se->op.open)
1331 req->se->op.open(req, nodeid, &fi);
1332 else
1333 fuse_reply_open(req, &fi);
1336 static void do_read(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1338 struct fuse_read_in *arg = (struct fuse_read_in *) inarg;
1340 if (req->se->op.read) {
1341 struct fuse_file_info fi;
1343 memset(&fi, 0, sizeof(fi));
1344 fi.fh = arg->fh;
1345 if (req->se->conn.proto_minor >= 9) {
1346 fi.lock_owner = arg->lock_owner;
1347 fi.flags = arg->flags;
1349 req->se->op.read(req, nodeid, arg->size, arg->offset, &fi);
1350 } else
1351 fuse_reply_err(req, ENOSYS);
1354 static void do_write(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1356 struct fuse_write_in *arg = (struct fuse_write_in *) inarg;
1357 struct fuse_file_info fi;
1358 char *param;
1360 memset(&fi, 0, sizeof(fi));
1361 fi.fh = arg->fh;
1362 fi.writepage = (arg->write_flags & FUSE_WRITE_CACHE) != 0;
1364 if (req->se->conn.proto_minor < 9) {
1365 param = ((char *) arg) + FUSE_COMPAT_WRITE_IN_SIZE;
1366 } else {
1367 fi.lock_owner = arg->lock_owner;
1368 fi.flags = arg->flags;
1369 param = PARAM(arg);
1372 if (req->se->op.write)
1373 req->se->op.write(req, nodeid, param, arg->size,
1374 arg->offset, &fi);
1375 else
1376 fuse_reply_err(req, ENOSYS);
1379 static void do_write_buf(fuse_req_t req, fuse_ino_t nodeid, const void *inarg,
1380 const struct fuse_buf *ibuf)
1382 struct fuse_session *se = req->se;
1383 struct fuse_bufvec bufv = {
1384 .buf[0] = *ibuf,
1385 .count = 1,
1387 struct fuse_write_in *arg = (struct fuse_write_in *) inarg;
1388 struct fuse_file_info fi;
1390 memset(&fi, 0, sizeof(fi));
1391 fi.fh = arg->fh;
1392 fi.writepage = arg->write_flags & FUSE_WRITE_CACHE;
1394 if (se->conn.proto_minor < 9) {
1395 bufv.buf[0].mem = ((char *) arg) + FUSE_COMPAT_WRITE_IN_SIZE;
1396 bufv.buf[0].size -= sizeof(struct fuse_in_header) +
1397 FUSE_COMPAT_WRITE_IN_SIZE;
1398 assert(!(bufv.buf[0].flags & FUSE_BUF_IS_FD));
1399 } else {
1400 fi.lock_owner = arg->lock_owner;
1401 fi.flags = arg->flags;
1402 if (!(bufv.buf[0].flags & FUSE_BUF_IS_FD))
1403 bufv.buf[0].mem = PARAM(arg);
1405 bufv.buf[0].size -= sizeof(struct fuse_in_header) +
1406 sizeof(struct fuse_write_in);
1408 if (bufv.buf[0].size < arg->size) {
1409 fuse_log(FUSE_LOG_ERR, "fuse: do_write_buf: buffer size too small\n");
1410 fuse_reply_err(req, EIO);
1411 goto out;
1413 bufv.buf[0].size = arg->size;
1415 se->op.write_buf(req, nodeid, &bufv, arg->offset, &fi);
1417 out:
1418 /* Need to reset the pipe if ->write_buf() didn't consume all data */
1419 if ((ibuf->flags & FUSE_BUF_IS_FD) && bufv.idx < bufv.count)
1420 fuse_ll_clear_pipe(se);
1423 static void do_flush(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1425 struct fuse_flush_in *arg = (struct fuse_flush_in *) inarg;
1426 struct fuse_file_info fi;
1428 memset(&fi, 0, sizeof(fi));
1429 fi.fh = arg->fh;
1430 fi.flush = 1;
1431 if (req->se->conn.proto_minor >= 7)
1432 fi.lock_owner = arg->lock_owner;
1434 if (req->se->op.flush)
1435 req->se->op.flush(req, nodeid, &fi);
1436 else
1437 fuse_reply_err(req, ENOSYS);
1440 static void do_release(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1442 struct fuse_release_in *arg = (struct fuse_release_in *) inarg;
1443 struct fuse_file_info fi;
1445 memset(&fi, 0, sizeof(fi));
1446 fi.flags = arg->flags;
1447 fi.fh = arg->fh;
1448 if (req->se->conn.proto_minor >= 8) {
1449 fi.flush = (arg->release_flags & FUSE_RELEASE_FLUSH) ? 1 : 0;
1450 fi.lock_owner = arg->lock_owner;
1452 if (arg->release_flags & FUSE_RELEASE_FLOCK_UNLOCK) {
1453 fi.flock_release = 1;
1454 fi.lock_owner = arg->lock_owner;
1457 if (req->se->op.release)
1458 req->se->op.release(req, nodeid, &fi);
1459 else
1460 fuse_reply_err(req, 0);
1463 static void do_fsync(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1465 struct fuse_fsync_in *arg = (struct fuse_fsync_in *) inarg;
1466 struct fuse_file_info fi;
1467 int datasync = arg->fsync_flags & 1;
1469 memset(&fi, 0, sizeof(fi));
1470 fi.fh = arg->fh;
1472 if (req->se->op.fsync)
1473 req->se->op.fsync(req, nodeid, datasync, &fi);
1474 else
1475 fuse_reply_err(req, ENOSYS);
1478 static void do_opendir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1480 struct fuse_open_in *arg = (struct fuse_open_in *) inarg;
1481 struct fuse_file_info fi;
1483 memset(&fi, 0, sizeof(fi));
1484 fi.flags = arg->flags;
1486 if (req->se->op.opendir)
1487 req->se->op.opendir(req, nodeid, &fi);
1488 else
1489 fuse_reply_open(req, &fi);
1492 static void do_readdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1494 struct fuse_read_in *arg = (struct fuse_read_in *) inarg;
1495 struct fuse_file_info fi;
1497 memset(&fi, 0, sizeof(fi));
1498 fi.fh = arg->fh;
1500 if (req->se->op.readdir)
1501 req->se->op.readdir(req, nodeid, arg->size, arg->offset, &fi);
1502 else
1503 fuse_reply_err(req, ENOSYS);
1506 static void do_readdirplus(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1508 struct fuse_read_in *arg = (struct fuse_read_in *) inarg;
1509 struct fuse_file_info fi;
1511 memset(&fi, 0, sizeof(fi));
1512 fi.fh = arg->fh;
1514 if (req->se->op.readdirplus)
1515 req->se->op.readdirplus(req, nodeid, arg->size, arg->offset, &fi);
1516 else
1517 fuse_reply_err(req, ENOSYS);
1520 static void do_releasedir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1522 struct fuse_release_in *arg = (struct fuse_release_in *) inarg;
1523 struct fuse_file_info fi;
1525 memset(&fi, 0, sizeof(fi));
1526 fi.flags = arg->flags;
1527 fi.fh = arg->fh;
1529 if (req->se->op.releasedir)
1530 req->se->op.releasedir(req, nodeid, &fi);
1531 else
1532 fuse_reply_err(req, 0);
1535 static void do_fsyncdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1537 struct fuse_fsync_in *arg = (struct fuse_fsync_in *) inarg;
1538 struct fuse_file_info fi;
1539 int datasync = arg->fsync_flags & 1;
1541 memset(&fi, 0, sizeof(fi));
1542 fi.fh = arg->fh;
1544 if (req->se->op.fsyncdir)
1545 req->se->op.fsyncdir(req, nodeid, datasync, &fi);
1546 else
1547 fuse_reply_err(req, ENOSYS);
1550 static void do_statfs(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1552 (void) nodeid;
1553 (void) inarg;
1555 if (req->se->op.statfs)
1556 req->se->op.statfs(req, nodeid);
1557 else {
1558 struct statvfs buf = {
1559 .f_namemax = 255,
1560 .f_bsize = 512,
1562 fuse_reply_statfs(req, &buf);
1566 static void do_setxattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1568 struct fuse_setxattr_in *arg = (struct fuse_setxattr_in *) inarg;
1569 char *name = PARAM(arg);
1570 char *value = name + strlen(name) + 1;
1572 if (req->se->op.setxattr)
1573 req->se->op.setxattr(req, nodeid, name, value, arg->size,
1574 arg->flags);
1575 else
1576 fuse_reply_err(req, ENOSYS);
1579 static void do_getxattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1581 struct fuse_getxattr_in *arg = (struct fuse_getxattr_in *) inarg;
1583 if (req->se->op.getxattr)
1584 req->se->op.getxattr(req, nodeid, PARAM(arg), arg->size);
1585 else
1586 fuse_reply_err(req, ENOSYS);
1589 static void do_listxattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1591 struct fuse_getxattr_in *arg = (struct fuse_getxattr_in *) inarg;
1593 if (req->se->op.listxattr)
1594 req->se->op.listxattr(req, nodeid, arg->size);
1595 else
1596 fuse_reply_err(req, ENOSYS);
1599 static void do_removexattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1601 char *name = (char *) inarg;
1603 if (req->se->op.removexattr)
1604 req->se->op.removexattr(req, nodeid, name);
1605 else
1606 fuse_reply_err(req, ENOSYS);
1609 static void convert_fuse_file_lock(struct fuse_file_lock *fl,
1610 struct flock *flock)
1612 memset(flock, 0, sizeof(struct flock));
1613 flock->l_type = fl->type;
1614 flock->l_whence = SEEK_SET;
1615 flock->l_start = fl->start;
1616 if (fl->end == OFFSET_MAX)
1617 flock->l_len = 0;
1618 else
1619 flock->l_len = fl->end - fl->start + 1;
1620 flock->l_pid = fl->pid;
1623 static void do_getlk(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1625 struct fuse_lk_in *arg = (struct fuse_lk_in *) inarg;
1626 struct fuse_file_info fi;
1627 struct flock flock;
1629 memset(&fi, 0, sizeof(fi));
1630 fi.fh = arg->fh;
1631 fi.lock_owner = arg->owner;
1633 convert_fuse_file_lock(&arg->lk, &flock);
1634 if (req->se->op.getlk)
1635 req->se->op.getlk(req, nodeid, &fi, &flock);
1636 else
1637 fuse_reply_err(req, ENOSYS);
1640 static void do_setlk_common(fuse_req_t req, fuse_ino_t nodeid,
1641 const void *inarg, int sleep)
1643 struct fuse_lk_in *arg = (struct fuse_lk_in *) inarg;
1644 struct fuse_file_info fi;
1645 struct flock flock;
1647 memset(&fi, 0, sizeof(fi));
1648 fi.fh = arg->fh;
1649 fi.lock_owner = arg->owner;
1651 if (arg->lk_flags & FUSE_LK_FLOCK) {
1652 int op = 0;
1654 switch (arg->lk.type) {
1655 case F_RDLCK:
1656 op = LOCK_SH;
1657 break;
1658 case F_WRLCK:
1659 op = LOCK_EX;
1660 break;
1661 case F_UNLCK:
1662 op = LOCK_UN;
1663 break;
1665 if (!sleep)
1666 op |= LOCK_NB;
1668 if (req->se->op.flock)
1669 req->se->op.flock(req, nodeid, &fi, op);
1670 else
1671 fuse_reply_err(req, ENOSYS);
1672 } else {
1673 convert_fuse_file_lock(&arg->lk, &flock);
1674 if (req->se->op.setlk)
1675 req->se->op.setlk(req, nodeid, &fi, &flock, sleep);
1676 else
1677 fuse_reply_err(req, ENOSYS);
1681 static void do_setlk(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1683 do_setlk_common(req, nodeid, inarg, 0);
1686 static void do_setlkw(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1688 do_setlk_common(req, nodeid, inarg, 1);
1691 static int find_interrupted(struct fuse_session *se, struct fuse_req *req)
1693 struct fuse_req *curr;
1695 for (curr = se->list.next; curr != &se->list; curr = curr->next) {
1696 if (curr->unique == req->u.i.unique) {
1697 fuse_interrupt_func_t func;
1698 void *data;
1700 curr->ctr++;
1701 pthread_mutex_unlock(&se->lock);
1703 /* Ugh, ugly locking */
1704 pthread_mutex_lock(&curr->lock);
1705 pthread_mutex_lock(&se->lock);
1706 curr->interrupted = 1;
1707 func = curr->u.ni.func;
1708 data = curr->u.ni.data;
1709 pthread_mutex_unlock(&se->lock);
1710 if (func)
1711 func(curr, data);
1712 pthread_mutex_unlock(&curr->lock);
1714 pthread_mutex_lock(&se->lock);
1715 curr->ctr--;
1716 if (!curr->ctr)
1717 destroy_req(curr);
1719 return 1;
1722 for (curr = se->interrupts.next; curr != &se->interrupts;
1723 curr = curr->next) {
1724 if (curr->u.i.unique == req->u.i.unique)
1725 return 1;
1727 return 0;
1730 static void do_interrupt(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1732 struct fuse_interrupt_in *arg = (struct fuse_interrupt_in *) inarg;
1733 struct fuse_session *se = req->se;
1735 (void) nodeid;
1736 if (se->debug)
1737 fuse_log(FUSE_LOG_DEBUG, "INTERRUPT: %llu\n",
1738 (unsigned long long) arg->unique);
1740 req->u.i.unique = arg->unique;
1742 pthread_mutex_lock(&se->lock);
1743 if (find_interrupted(se, req))
1744 destroy_req(req);
1745 else
1746 list_add_req(req, &se->interrupts);
1747 pthread_mutex_unlock(&se->lock);
1750 static struct fuse_req *check_interrupt(struct fuse_session *se,
1751 struct fuse_req *req)
1753 struct fuse_req *curr;
1755 for (curr = se->interrupts.next; curr != &se->interrupts;
1756 curr = curr->next) {
1757 if (curr->u.i.unique == req->unique) {
1758 req->interrupted = 1;
1759 list_del_req(curr);
1760 free(curr);
1761 return NULL;
1764 curr = se->interrupts.next;
1765 if (curr != &se->interrupts) {
1766 list_del_req(curr);
1767 list_init_req(curr);
1768 return curr;
1769 } else
1770 return NULL;
1773 static void do_bmap(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1775 struct fuse_bmap_in *arg = (struct fuse_bmap_in *) inarg;
1777 if (req->se->op.bmap)
1778 req->se->op.bmap(req, nodeid, arg->blocksize, arg->block);
1779 else
1780 fuse_reply_err(req, ENOSYS);
1783 static void do_ioctl(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1785 struct fuse_ioctl_in *arg = (struct fuse_ioctl_in *) inarg;
1786 unsigned int flags = arg->flags;
1787 void *in_buf = arg->in_size ? PARAM(arg) : NULL;
1788 struct fuse_file_info fi;
1790 if (flags & FUSE_IOCTL_DIR &&
1791 !(req->se->conn.want & FUSE_CAP_IOCTL_DIR)) {
1792 fuse_reply_err(req, ENOTTY);
1793 return;
1796 memset(&fi, 0, sizeof(fi));
1797 fi.fh = arg->fh;
1799 if (sizeof(void *) == 4 && req->se->conn.proto_minor >= 16 &&
1800 !(flags & FUSE_IOCTL_32BIT)) {
1801 req->ioctl_64bit = 1;
1804 if (req->se->op.ioctl)
1805 req->se->op.ioctl(req, nodeid, arg->cmd,
1806 (void *)(uintptr_t)arg->arg, &fi, flags,
1807 in_buf, arg->in_size, arg->out_size);
1808 else
1809 fuse_reply_err(req, ENOSYS);
1812 void fuse_pollhandle_destroy(struct fuse_pollhandle *ph)
1814 free(ph);
1817 static void do_poll(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1819 struct fuse_poll_in *arg = (struct fuse_poll_in *) inarg;
1820 struct fuse_file_info fi;
1822 memset(&fi, 0, sizeof(fi));
1823 fi.fh = arg->fh;
1824 fi.poll_events = arg->events;
1826 if (req->se->op.poll) {
1827 struct fuse_pollhandle *ph = NULL;
1829 if (arg->flags & FUSE_POLL_SCHEDULE_NOTIFY) {
1830 ph = malloc(sizeof(struct fuse_pollhandle));
1831 if (ph == NULL) {
1832 fuse_reply_err(req, ENOMEM);
1833 return;
1835 ph->kh = arg->kh;
1836 ph->se = req->se;
1839 req->se->op.poll(req, nodeid, &fi, ph);
1840 } else {
1841 fuse_reply_err(req, ENOSYS);
1845 static void do_fallocate(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1847 struct fuse_fallocate_in *arg = (struct fuse_fallocate_in *) inarg;
1848 struct fuse_file_info fi;
1850 memset(&fi, 0, sizeof(fi));
1851 fi.fh = arg->fh;
1853 if (req->se->op.fallocate)
1854 req->se->op.fallocate(req, nodeid, arg->mode, arg->offset, arg->length, &fi);
1855 else
1856 fuse_reply_err(req, ENOSYS);
1859 static void do_copy_file_range(fuse_req_t req, fuse_ino_t nodeid_in, const void *inarg)
1861 struct fuse_copy_file_range_in *arg = (struct fuse_copy_file_range_in *) inarg;
1862 struct fuse_file_info fi_in, fi_out;
1864 memset(&fi_in, 0, sizeof(fi_in));
1865 fi_in.fh = arg->fh_in;
1867 memset(&fi_out, 0, sizeof(fi_out));
1868 fi_out.fh = arg->fh_out;
1871 if (req->se->op.copy_file_range)
1872 req->se->op.copy_file_range(req, nodeid_in, arg->off_in,
1873 &fi_in, arg->nodeid_out,
1874 arg->off_out, &fi_out, arg->len,
1875 arg->flags);
1876 else
1877 fuse_reply_err(req, ENOSYS);
1880 static void do_lseek(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1882 struct fuse_lseek_in *arg = (struct fuse_lseek_in *) inarg;
1883 struct fuse_file_info fi;
1885 memset(&fi, 0, sizeof(fi));
1886 fi.fh = arg->fh;
1888 if (req->se->op.lseek)
1889 req->se->op.lseek(req, nodeid, arg->offset, arg->whence, &fi);
1890 else
1891 fuse_reply_err(req, ENOSYS);
1894 static void do_init(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1896 struct fuse_init_in *arg = (struct fuse_init_in *) inarg;
1897 struct fuse_init_out outarg;
1898 struct fuse_session *se = req->se;
1899 size_t bufsize = se->bufsize;
1900 size_t outargsize = sizeof(outarg);
1902 (void) nodeid;
1903 if (se->debug) {
1904 fuse_log(FUSE_LOG_DEBUG, "INIT: %u.%u\n", arg->major, arg->minor);
1905 if (arg->major == 7 && arg->minor >= 6) {
1906 fuse_log(FUSE_LOG_DEBUG, "flags=0x%08x\n", arg->flags);
1907 fuse_log(FUSE_LOG_DEBUG, "max_readahead=0x%08x\n",
1908 arg->max_readahead);
1911 se->conn.proto_major = arg->major;
1912 se->conn.proto_minor = arg->minor;
1913 se->conn.capable = 0;
1914 se->conn.want = 0;
1916 memset(&outarg, 0, sizeof(outarg));
1917 outarg.major = FUSE_KERNEL_VERSION;
1918 outarg.minor = FUSE_KERNEL_MINOR_VERSION;
1920 if (arg->major < 7) {
1921 fuse_log(FUSE_LOG_ERR, "fuse: unsupported protocol version: %u.%u\n",
1922 arg->major, arg->minor);
1923 fuse_reply_err(req, EPROTO);
1924 return;
1927 if (arg->major > 7) {
1928 /* Wait for a second INIT request with a 7.X version */
1929 send_reply_ok(req, &outarg, sizeof(outarg));
1930 return;
1933 if (arg->minor >= 6) {
1934 if (arg->max_readahead < se->conn.max_readahead)
1935 se->conn.max_readahead = arg->max_readahead;
1936 if (arg->flags & FUSE_ASYNC_READ)
1937 se->conn.capable |= FUSE_CAP_ASYNC_READ;
1938 if (arg->flags & FUSE_POSIX_LOCKS)
1939 se->conn.capable |= FUSE_CAP_POSIX_LOCKS;
1940 if (arg->flags & FUSE_ATOMIC_O_TRUNC)
1941 se->conn.capable |= FUSE_CAP_ATOMIC_O_TRUNC;
1942 if (arg->flags & FUSE_EXPORT_SUPPORT)
1943 se->conn.capable |= FUSE_CAP_EXPORT_SUPPORT;
1944 if (arg->flags & FUSE_DONT_MASK)
1945 se->conn.capable |= FUSE_CAP_DONT_MASK;
1946 if (arg->flags & FUSE_FLOCK_LOCKS)
1947 se->conn.capable |= FUSE_CAP_FLOCK_LOCKS;
1948 if (arg->flags & FUSE_AUTO_INVAL_DATA)
1949 se->conn.capable |= FUSE_CAP_AUTO_INVAL_DATA;
1950 if (arg->flags & FUSE_DO_READDIRPLUS)
1951 se->conn.capable |= FUSE_CAP_READDIRPLUS;
1952 if (arg->flags & FUSE_READDIRPLUS_AUTO)
1953 se->conn.capable |= FUSE_CAP_READDIRPLUS_AUTO;
1954 if (arg->flags & FUSE_ASYNC_DIO)
1955 se->conn.capable |= FUSE_CAP_ASYNC_DIO;
1956 if (arg->flags & FUSE_WRITEBACK_CACHE)
1957 se->conn.capable |= FUSE_CAP_WRITEBACK_CACHE;
1958 if (arg->flags & FUSE_NO_OPEN_SUPPORT)
1959 se->conn.capable |= FUSE_CAP_NO_OPEN_SUPPORT;
1960 if (arg->flags & FUSE_PARALLEL_DIROPS)
1961 se->conn.capable |= FUSE_CAP_PARALLEL_DIROPS;
1962 if (arg->flags & FUSE_POSIX_ACL)
1963 se->conn.capable |= FUSE_CAP_POSIX_ACL;
1964 if (arg->flags & FUSE_HANDLE_KILLPRIV)
1965 se->conn.capable |= FUSE_CAP_HANDLE_KILLPRIV;
1966 if (arg->flags & FUSE_NO_OPENDIR_SUPPORT)
1967 se->conn.capable |= FUSE_CAP_NO_OPENDIR_SUPPORT;
1968 if (!(arg->flags & FUSE_MAX_PAGES)) {
1969 size_t max_bufsize =
1970 FUSE_DEFAULT_MAX_PAGES_PER_REQ * getpagesize()
1971 + FUSE_BUFFER_HEADER_SIZE;
1972 if (bufsize > max_bufsize) {
1973 bufsize = max_bufsize;
1976 } else {
1977 se->conn.max_readahead = 0;
1980 if (se->conn.proto_minor >= 14) {
1981 #ifdef HAVE_SPLICE
1982 #ifdef HAVE_VMSPLICE
1983 se->conn.capable |= FUSE_CAP_SPLICE_WRITE | FUSE_CAP_SPLICE_MOVE;
1984 #endif
1985 se->conn.capable |= FUSE_CAP_SPLICE_READ;
1986 #endif
1988 if (se->conn.proto_minor >= 18)
1989 se->conn.capable |= FUSE_CAP_IOCTL_DIR;
1991 /* Default settings for modern filesystems.
1993 * Most of these capabilities were disabled by default in
1994 * libfuse2 for backwards compatibility reasons. In libfuse3,
1995 * we can finally enable them by default (as long as they're
1996 * supported by the kernel).
1998 #define LL_SET_DEFAULT(cond, cap) \
1999 if ((cond) && (se->conn.capable & (cap))) \
2000 se->conn.want |= (cap)
2001 LL_SET_DEFAULT(1, FUSE_CAP_ASYNC_READ);
2002 LL_SET_DEFAULT(1, FUSE_CAP_PARALLEL_DIROPS);
2003 LL_SET_DEFAULT(1, FUSE_CAP_AUTO_INVAL_DATA);
2004 LL_SET_DEFAULT(1, FUSE_CAP_HANDLE_KILLPRIV);
2005 LL_SET_DEFAULT(1, FUSE_CAP_ASYNC_DIO);
2006 LL_SET_DEFAULT(1, FUSE_CAP_IOCTL_DIR);
2007 LL_SET_DEFAULT(1, FUSE_CAP_ATOMIC_O_TRUNC);
2008 LL_SET_DEFAULT(se->op.write_buf, FUSE_CAP_SPLICE_READ);
2009 LL_SET_DEFAULT(se->op.getlk && se->op.setlk,
2010 FUSE_CAP_POSIX_LOCKS);
2011 LL_SET_DEFAULT(se->op.flock, FUSE_CAP_FLOCK_LOCKS);
2012 LL_SET_DEFAULT(se->op.readdirplus, FUSE_CAP_READDIRPLUS);
2013 LL_SET_DEFAULT(se->op.readdirplus && se->op.readdir,
2014 FUSE_CAP_READDIRPLUS_AUTO);
2015 se->conn.time_gran = 1;
2017 if (bufsize < FUSE_MIN_READ_BUFFER) {
2018 fuse_log(FUSE_LOG_ERR, "fuse: warning: buffer size too small: %zu\n",
2019 bufsize);
2020 bufsize = FUSE_MIN_READ_BUFFER;
2022 se->bufsize = bufsize;
2024 if (se->conn.max_write > bufsize - FUSE_BUFFER_HEADER_SIZE)
2025 se->conn.max_write = bufsize - FUSE_BUFFER_HEADER_SIZE;
2027 se->got_init = 1;
2028 if (se->op.init)
2029 se->op.init(se->userdata, &se->conn);
2031 if (se->conn.want & (~se->conn.capable)) {
2032 fuse_log(FUSE_LOG_ERR, "fuse: error: filesystem requested capabilities "
2033 "0x%x that are not supported by kernel, aborting.\n",
2034 se->conn.want & (~se->conn.capable));
2035 fuse_reply_err(req, EPROTO);
2036 se->error = -EPROTO;
2037 fuse_session_exit(se);
2038 return;
2041 unsigned max_read_mo = get_max_read(se->mo);
2042 if (se->conn.max_read != max_read_mo) {
2043 fuse_log(FUSE_LOG_ERR, "fuse: error: init() and fuse_session_new() "
2044 "requested different maximum read size (%u vs %u)\n",
2045 se->conn.max_read, max_read_mo);
2046 fuse_reply_err(req, EPROTO);
2047 se->error = -EPROTO;
2048 fuse_session_exit(se);
2049 return;
2052 if (se->conn.max_write < bufsize - FUSE_BUFFER_HEADER_SIZE) {
2053 se->bufsize = se->conn.max_write + FUSE_BUFFER_HEADER_SIZE;
2055 if (arg->flags & FUSE_MAX_PAGES) {
2056 outarg.flags |= FUSE_MAX_PAGES;
2057 outarg.max_pages = (se->conn.max_write - 1) / getpagesize() + 1;
2060 /* Always enable big writes, this is superseded
2061 by the max_write option */
2062 outarg.flags |= FUSE_BIG_WRITES;
2064 if (se->conn.want & FUSE_CAP_ASYNC_READ)
2065 outarg.flags |= FUSE_ASYNC_READ;
2066 if (se->conn.want & FUSE_CAP_POSIX_LOCKS)
2067 outarg.flags |= FUSE_POSIX_LOCKS;
2068 if (se->conn.want & FUSE_CAP_ATOMIC_O_TRUNC)
2069 outarg.flags |= FUSE_ATOMIC_O_TRUNC;
2070 if (se->conn.want & FUSE_CAP_EXPORT_SUPPORT)
2071 outarg.flags |= FUSE_EXPORT_SUPPORT;
2072 if (se->conn.want & FUSE_CAP_DONT_MASK)
2073 outarg.flags |= FUSE_DONT_MASK;
2074 if (se->conn.want & FUSE_CAP_FLOCK_LOCKS)
2075 outarg.flags |= FUSE_FLOCK_LOCKS;
2076 if (se->conn.want & FUSE_CAP_AUTO_INVAL_DATA)
2077 outarg.flags |= FUSE_AUTO_INVAL_DATA;
2078 if (se->conn.want & FUSE_CAP_READDIRPLUS)
2079 outarg.flags |= FUSE_DO_READDIRPLUS;
2080 if (se->conn.want & FUSE_CAP_READDIRPLUS_AUTO)
2081 outarg.flags |= FUSE_READDIRPLUS_AUTO;
2082 if (se->conn.want & FUSE_CAP_ASYNC_DIO)
2083 outarg.flags |= FUSE_ASYNC_DIO;
2084 if (se->conn.want & FUSE_CAP_WRITEBACK_CACHE)
2085 outarg.flags |= FUSE_WRITEBACK_CACHE;
2086 if (se->conn.want & FUSE_CAP_POSIX_ACL)
2087 outarg.flags |= FUSE_POSIX_ACL;
2088 outarg.max_readahead = se->conn.max_readahead;
2089 outarg.max_write = se->conn.max_write;
2090 if (se->conn.proto_minor >= 13) {
2091 if (se->conn.max_background >= (1 << 16))
2092 se->conn.max_background = (1 << 16) - 1;
2093 if (se->conn.congestion_threshold > se->conn.max_background)
2094 se->conn.congestion_threshold = se->conn.max_background;
2095 if (!se->conn.congestion_threshold) {
2096 se->conn.congestion_threshold =
2097 se->conn.max_background * 3 / 4;
2100 outarg.max_background = se->conn.max_background;
2101 outarg.congestion_threshold = se->conn.congestion_threshold;
2103 if (se->conn.proto_minor >= 23)
2104 outarg.time_gran = se->conn.time_gran;
2106 if (se->debug) {
2107 fuse_log(FUSE_LOG_DEBUG, " INIT: %u.%u\n", outarg.major, outarg.minor);
2108 fuse_log(FUSE_LOG_DEBUG, " flags=0x%08x\n", outarg.flags);
2109 fuse_log(FUSE_LOG_DEBUG, " max_readahead=0x%08x\n",
2110 outarg.max_readahead);
2111 fuse_log(FUSE_LOG_DEBUG, " max_write=0x%08x\n", outarg.max_write);
2112 fuse_log(FUSE_LOG_DEBUG, " max_background=%i\n",
2113 outarg.max_background);
2114 fuse_log(FUSE_LOG_DEBUG, " congestion_threshold=%i\n",
2115 outarg.congestion_threshold);
2116 fuse_log(FUSE_LOG_DEBUG, " time_gran=%u\n",
2117 outarg.time_gran);
2119 if (arg->minor < 5)
2120 outargsize = FUSE_COMPAT_INIT_OUT_SIZE;
2121 else if (arg->minor < 23)
2122 outargsize = FUSE_COMPAT_22_INIT_OUT_SIZE;
2124 send_reply_ok(req, &outarg, outargsize);
2127 static void do_destroy(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
2129 struct fuse_session *se = req->se;
2131 (void) nodeid;
2132 (void) inarg;
2134 se->got_destroy = 1;
2135 if (se->op.destroy)
2136 se->op.destroy(se->userdata);
2138 send_reply_ok(req, NULL, 0);
2141 static void list_del_nreq(struct fuse_notify_req *nreq)
2143 struct fuse_notify_req *prev = nreq->prev;
2144 struct fuse_notify_req *next = nreq->next;
2145 prev->next = next;
2146 next->prev = prev;
2149 static void list_add_nreq(struct fuse_notify_req *nreq,
2150 struct fuse_notify_req *next)
2152 struct fuse_notify_req *prev = next->prev;
2153 nreq->next = next;
2154 nreq->prev = prev;
2155 prev->next = nreq;
2156 next->prev = nreq;
2159 static void list_init_nreq(struct fuse_notify_req *nreq)
2161 nreq->next = nreq;
2162 nreq->prev = nreq;
2165 static void do_notify_reply(fuse_req_t req, fuse_ino_t nodeid,
2166 const void *inarg, const struct fuse_buf *buf)
2168 struct fuse_session *se = req->se;
2169 struct fuse_notify_req *nreq;
2170 struct fuse_notify_req *head;
2172 pthread_mutex_lock(&se->lock);
2173 head = &se->notify_list;
2174 for (nreq = head->next; nreq != head; nreq = nreq->next) {
2175 if (nreq->unique == req->unique) {
2176 list_del_nreq(nreq);
2177 break;
2180 pthread_mutex_unlock(&se->lock);
2182 if (nreq != head)
2183 nreq->reply(nreq, req, nodeid, inarg, buf);
2186 static int send_notify_iov(struct fuse_session *se, int notify_code,
2187 struct iovec *iov, int count)
2189 struct fuse_out_header out;
2191 if (!se->got_init)
2192 return -ENOTCONN;
2194 out.unique = 0;
2195 out.error = notify_code;
2196 iov[0].iov_base = &out;
2197 iov[0].iov_len = sizeof(struct fuse_out_header);
2199 return fuse_send_msg(se, NULL, iov, count);
2202 int fuse_lowlevel_notify_poll(struct fuse_pollhandle *ph)
2204 if (ph != NULL) {
2205 struct fuse_notify_poll_wakeup_out outarg;
2206 struct iovec iov[2];
2208 outarg.kh = ph->kh;
2210 iov[1].iov_base = &outarg;
2211 iov[1].iov_len = sizeof(outarg);
2213 return send_notify_iov(ph->se, FUSE_NOTIFY_POLL, iov, 2);
2214 } else {
2215 return 0;
2219 int fuse_lowlevel_notify_inval_inode(struct fuse_session *se, fuse_ino_t ino,
2220 off_t off, off_t len)
2222 struct fuse_notify_inval_inode_out outarg;
2223 struct iovec iov[2];
2225 if (!se)
2226 return -EINVAL;
2228 if (se->conn.proto_major < 6 || se->conn.proto_minor < 12)
2229 return -ENOSYS;
2231 outarg.ino = ino;
2232 outarg.off = off;
2233 outarg.len = len;
2235 iov[1].iov_base = &outarg;
2236 iov[1].iov_len = sizeof(outarg);
2238 return send_notify_iov(se, FUSE_NOTIFY_INVAL_INODE, iov, 2);
2241 int fuse_lowlevel_notify_inval_entry(struct fuse_session *se, fuse_ino_t parent,
2242 const char *name, size_t namelen)
2244 struct fuse_notify_inval_entry_out outarg;
2245 struct iovec iov[3];
2247 if (!se)
2248 return -EINVAL;
2250 if (se->conn.proto_major < 6 || se->conn.proto_minor < 12)
2251 return -ENOSYS;
2253 outarg.parent = parent;
2254 outarg.namelen = namelen;
2255 outarg.padding = 0;
2257 iov[1].iov_base = &outarg;
2258 iov[1].iov_len = sizeof(outarg);
2259 iov[2].iov_base = (void *)name;
2260 iov[2].iov_len = namelen + 1;
2262 return send_notify_iov(se, FUSE_NOTIFY_INVAL_ENTRY, iov, 3);
2265 int fuse_lowlevel_notify_delete(struct fuse_session *se,
2266 fuse_ino_t parent, fuse_ino_t child,
2267 const char *name, size_t namelen)
2269 struct fuse_notify_delete_out outarg;
2270 struct iovec iov[3];
2272 if (!se)
2273 return -EINVAL;
2275 if (se->conn.proto_major < 6 || se->conn.proto_minor < 18)
2276 return -ENOSYS;
2278 outarg.parent = parent;
2279 outarg.child = child;
2280 outarg.namelen = namelen;
2281 outarg.padding = 0;
2283 iov[1].iov_base = &outarg;
2284 iov[1].iov_len = sizeof(outarg);
2285 iov[2].iov_base = (void *)name;
2286 iov[2].iov_len = namelen + 1;
2288 return send_notify_iov(se, FUSE_NOTIFY_DELETE, iov, 3);
2291 int fuse_lowlevel_notify_store(struct fuse_session *se, fuse_ino_t ino,
2292 off_t offset, struct fuse_bufvec *bufv,
2293 enum fuse_buf_copy_flags flags)
2295 struct fuse_out_header out;
2296 struct fuse_notify_store_out outarg;
2297 struct iovec iov[3];
2298 size_t size = fuse_buf_size(bufv);
2299 int res;
2301 if (!se)
2302 return -EINVAL;
2304 if (se->conn.proto_major < 6 || se->conn.proto_minor < 15)
2305 return -ENOSYS;
2307 out.unique = 0;
2308 out.error = FUSE_NOTIFY_STORE;
2310 outarg.nodeid = ino;
2311 outarg.offset = offset;
2312 outarg.size = size;
2313 outarg.padding = 0;
2315 iov[0].iov_base = &out;
2316 iov[0].iov_len = sizeof(out);
2317 iov[1].iov_base = &outarg;
2318 iov[1].iov_len = sizeof(outarg);
2320 res = fuse_send_data_iov(se, NULL, iov, 2, bufv, flags);
2321 if (res > 0)
2322 res = -res;
2324 return res;
2327 struct fuse_retrieve_req {
2328 struct fuse_notify_req nreq;
2329 void *cookie;
2332 static void fuse_ll_retrieve_reply(struct fuse_notify_req *nreq,
2333 fuse_req_t req, fuse_ino_t ino,
2334 const void *inarg,
2335 const struct fuse_buf *ibuf)
2337 struct fuse_session *se = req->se;
2338 struct fuse_retrieve_req *rreq =
2339 container_of(nreq, struct fuse_retrieve_req, nreq);
2340 const struct fuse_notify_retrieve_in *arg = inarg;
2341 struct fuse_bufvec bufv = {
2342 .buf[0] = *ibuf,
2343 .count = 1,
2346 if (!(bufv.buf[0].flags & FUSE_BUF_IS_FD))
2347 bufv.buf[0].mem = PARAM(arg);
2349 bufv.buf[0].size -= sizeof(struct fuse_in_header) +
2350 sizeof(struct fuse_notify_retrieve_in);
2352 if (bufv.buf[0].size < arg->size) {
2353 fuse_log(FUSE_LOG_ERR, "fuse: retrieve reply: buffer size too small\n");
2354 fuse_reply_none(req);
2355 goto out;
2357 bufv.buf[0].size = arg->size;
2359 if (se->op.retrieve_reply) {
2360 se->op.retrieve_reply(req, rreq->cookie, ino,
2361 arg->offset, &bufv);
2362 } else {
2363 fuse_reply_none(req);
2365 out:
2366 free(rreq);
2367 if ((ibuf->flags & FUSE_BUF_IS_FD) && bufv.idx < bufv.count)
2368 fuse_ll_clear_pipe(se);
2371 int fuse_lowlevel_notify_retrieve(struct fuse_session *se, fuse_ino_t ino,
2372 size_t size, off_t offset, void *cookie)
2374 struct fuse_notify_retrieve_out outarg;
2375 struct iovec iov[2];
2376 struct fuse_retrieve_req *rreq;
2377 int err;
2379 if (!se)
2380 return -EINVAL;
2382 if (se->conn.proto_major < 6 || se->conn.proto_minor < 15)
2383 return -ENOSYS;
2385 rreq = malloc(sizeof(*rreq));
2386 if (rreq == NULL)
2387 return -ENOMEM;
2389 pthread_mutex_lock(&se->lock);
2390 rreq->cookie = cookie;
2391 rreq->nreq.unique = se->notify_ctr++;
2392 rreq->nreq.reply = fuse_ll_retrieve_reply;
2393 list_add_nreq(&rreq->nreq, &se->notify_list);
2394 pthread_mutex_unlock(&se->lock);
2396 outarg.notify_unique = rreq->nreq.unique;
2397 outarg.nodeid = ino;
2398 outarg.offset = offset;
2399 outarg.size = size;
2400 outarg.padding = 0;
2402 iov[1].iov_base = &outarg;
2403 iov[1].iov_len = sizeof(outarg);
2405 err = send_notify_iov(se, FUSE_NOTIFY_RETRIEVE, iov, 2);
2406 if (err) {
2407 pthread_mutex_lock(&se->lock);
2408 list_del_nreq(&rreq->nreq);
2409 pthread_mutex_unlock(&se->lock);
2410 free(rreq);
2413 return err;
2416 void *fuse_req_userdata(fuse_req_t req)
2418 return req->se->userdata;
2421 const struct fuse_ctx *fuse_req_ctx(fuse_req_t req)
2423 return &req->ctx;
2426 void fuse_req_interrupt_func(fuse_req_t req, fuse_interrupt_func_t func,
2427 void *data)
2429 pthread_mutex_lock(&req->lock);
2430 pthread_mutex_lock(&req->se->lock);
2431 req->u.ni.func = func;
2432 req->u.ni.data = data;
2433 pthread_mutex_unlock(&req->se->lock);
2434 if (req->interrupted && func)
2435 func(req, data);
2436 pthread_mutex_unlock(&req->lock);
2439 int fuse_req_interrupted(fuse_req_t req)
2441 int interrupted;
2443 pthread_mutex_lock(&req->se->lock);
2444 interrupted = req->interrupted;
2445 pthread_mutex_unlock(&req->se->lock);
2447 return interrupted;
2450 static struct {
2451 void (*func)(fuse_req_t, fuse_ino_t, const void *);
2452 const char *name;
2453 } fuse_ll_ops[] = {
2454 [FUSE_LOOKUP] = { do_lookup, "LOOKUP" },
2455 [FUSE_FORGET] = { do_forget, "FORGET" },
2456 [FUSE_GETATTR] = { do_getattr, "GETATTR" },
2457 [FUSE_SETATTR] = { do_setattr, "SETATTR" },
2458 [FUSE_READLINK] = { do_readlink, "READLINK" },
2459 [FUSE_SYMLINK] = { do_symlink, "SYMLINK" },
2460 [FUSE_MKNOD] = { do_mknod, "MKNOD" },
2461 [FUSE_MKDIR] = { do_mkdir, "MKDIR" },
2462 [FUSE_UNLINK] = { do_unlink, "UNLINK" },
2463 [FUSE_RMDIR] = { do_rmdir, "RMDIR" },
2464 [FUSE_RENAME] = { do_rename, "RENAME" },
2465 [FUSE_LINK] = { do_link, "LINK" },
2466 [FUSE_OPEN] = { do_open, "OPEN" },
2467 [FUSE_READ] = { do_read, "READ" },
2468 [FUSE_WRITE] = { do_write, "WRITE" },
2469 [FUSE_STATFS] = { do_statfs, "STATFS" },
2470 [FUSE_RELEASE] = { do_release, "RELEASE" },
2471 [FUSE_FSYNC] = { do_fsync, "FSYNC" },
2472 [FUSE_SETXATTR] = { do_setxattr, "SETXATTR" },
2473 [FUSE_GETXATTR] = { do_getxattr, "GETXATTR" },
2474 [FUSE_LISTXATTR] = { do_listxattr, "LISTXATTR" },
2475 [FUSE_REMOVEXATTR] = { do_removexattr, "REMOVEXATTR" },
2476 [FUSE_FLUSH] = { do_flush, "FLUSH" },
2477 [FUSE_INIT] = { do_init, "INIT" },
2478 [FUSE_OPENDIR] = { do_opendir, "OPENDIR" },
2479 [FUSE_READDIR] = { do_readdir, "READDIR" },
2480 [FUSE_RELEASEDIR] = { do_releasedir, "RELEASEDIR" },
2481 [FUSE_FSYNCDIR] = { do_fsyncdir, "FSYNCDIR" },
2482 [FUSE_GETLK] = { do_getlk, "GETLK" },
2483 [FUSE_SETLK] = { do_setlk, "SETLK" },
2484 [FUSE_SETLKW] = { do_setlkw, "SETLKW" },
2485 [FUSE_ACCESS] = { do_access, "ACCESS" },
2486 [FUSE_CREATE] = { do_create, "CREATE" },
2487 [FUSE_INTERRUPT] = { do_interrupt, "INTERRUPT" },
2488 [FUSE_BMAP] = { do_bmap, "BMAP" },
2489 [FUSE_IOCTL] = { do_ioctl, "IOCTL" },
2490 [FUSE_POLL] = { do_poll, "POLL" },
2491 [FUSE_FALLOCATE] = { do_fallocate, "FALLOCATE" },
2492 [FUSE_DESTROY] = { do_destroy, "DESTROY" },
2493 [FUSE_NOTIFY_REPLY] = { (void *) 1, "NOTIFY_REPLY" },
2494 [FUSE_BATCH_FORGET] = { do_batch_forget, "BATCH_FORGET" },
2495 [FUSE_READDIRPLUS] = { do_readdirplus, "READDIRPLUS"},
2496 [FUSE_RENAME2] = { do_rename2, "RENAME2" },
2497 [FUSE_COPY_FILE_RANGE] = { do_copy_file_range, "COPY_FILE_RANGE" },
2498 [FUSE_LSEEK] = { do_lseek, "LSEEK" },
2499 [CUSE_INIT] = { cuse_lowlevel_init, "CUSE_INIT" },
2502 #define FUSE_MAXOP (sizeof(fuse_ll_ops) / sizeof(fuse_ll_ops[0]))
2504 static const char *opname(enum fuse_opcode opcode)
2506 if (opcode >= FUSE_MAXOP || !fuse_ll_ops[opcode].name)
2507 return "???";
2508 else
2509 return fuse_ll_ops[opcode].name;
2512 static int fuse_ll_copy_from_pipe(struct fuse_bufvec *dst,
2513 struct fuse_bufvec *src)
2515 ssize_t res = fuse_buf_copy(dst, src, 0);
2516 if (res < 0) {
2517 fuse_log(FUSE_LOG_ERR, "fuse: copy from pipe: %s\n", strerror(-res));
2518 return res;
2520 if ((size_t)res < fuse_buf_size(dst)) {
2521 fuse_log(FUSE_LOG_ERR, "fuse: copy from pipe: short read\n");
2522 return -1;
2524 return 0;
2527 void fuse_session_process_buf(struct fuse_session *se,
2528 const struct fuse_buf *buf)
2530 fuse_session_process_buf_int(se, buf, NULL);
2533 void fuse_session_process_buf_int(struct fuse_session *se,
2534 const struct fuse_buf *buf, struct fuse_chan *ch)
2536 const size_t write_header_size = sizeof(struct fuse_in_header) +
2537 sizeof(struct fuse_write_in);
2538 struct fuse_bufvec bufv = { .buf[0] = *buf, .count = 1 };
2539 struct fuse_bufvec tmpbuf = FUSE_BUFVEC_INIT(write_header_size);
2540 struct fuse_in_header *in;
2541 const void *inarg;
2542 struct fuse_req *req;
2543 void *mbuf = NULL;
2544 int err;
2545 int res;
2547 if (buf->flags & FUSE_BUF_IS_FD) {
2548 if (buf->size < tmpbuf.buf[0].size)
2549 tmpbuf.buf[0].size = buf->size;
2551 mbuf = malloc(tmpbuf.buf[0].size);
2552 if (mbuf == NULL) {
2553 fuse_log(FUSE_LOG_ERR, "fuse: failed to allocate header\n");
2554 goto clear_pipe;
2556 tmpbuf.buf[0].mem = mbuf;
2558 res = fuse_ll_copy_from_pipe(&tmpbuf, &bufv);
2559 if (res < 0)
2560 goto clear_pipe;
2562 in = mbuf;
2563 } else {
2564 in = buf->mem;
2567 if (se->debug) {
2568 fuse_log(FUSE_LOG_DEBUG,
2569 "unique: %llu, opcode: %s (%i), nodeid: %llu, insize: %zu, pid: %u\n",
2570 (unsigned long long) in->unique,
2571 opname((enum fuse_opcode) in->opcode), in->opcode,
2572 (unsigned long long) in->nodeid, buf->size, in->pid);
2575 req = fuse_ll_alloc_req(se);
2576 if (req == NULL) {
2577 struct fuse_out_header out = {
2578 .unique = in->unique,
2579 .error = -ENOMEM,
2581 struct iovec iov = {
2582 .iov_base = &out,
2583 .iov_len = sizeof(struct fuse_out_header),
2586 fuse_send_msg(se, ch, &iov, 1);
2587 goto clear_pipe;
2590 req->unique = in->unique;
2591 req->ctx.uid = in->uid;
2592 req->ctx.gid = in->gid;
2593 req->ctx.pid = in->pid;
2594 req->ch = ch ? fuse_chan_get(ch) : NULL;
2596 err = EIO;
2597 if (!se->got_init) {
2598 enum fuse_opcode expected;
2600 expected = se->cuse_data ? CUSE_INIT : FUSE_INIT;
2601 if (in->opcode != expected)
2602 goto reply_err;
2603 } else if (in->opcode == FUSE_INIT || in->opcode == CUSE_INIT)
2604 goto reply_err;
2606 err = EACCES;
2607 /* Implement -o allow_root */
2608 if (se->deny_others && in->uid != se->owner && in->uid != 0 &&
2609 in->opcode != FUSE_INIT && in->opcode != FUSE_READ &&
2610 in->opcode != FUSE_WRITE && in->opcode != FUSE_FSYNC &&
2611 in->opcode != FUSE_RELEASE && in->opcode != FUSE_READDIR &&
2612 in->opcode != FUSE_FSYNCDIR && in->opcode != FUSE_RELEASEDIR &&
2613 in->opcode != FUSE_NOTIFY_REPLY &&
2614 in->opcode != FUSE_READDIRPLUS)
2615 goto reply_err;
2617 err = ENOSYS;
2618 if (in->opcode >= FUSE_MAXOP || !fuse_ll_ops[in->opcode].func)
2619 goto reply_err;
2620 if (in->opcode != FUSE_INTERRUPT) {
2621 struct fuse_req *intr;
2622 pthread_mutex_lock(&se->lock);
2623 intr = check_interrupt(se, req);
2624 list_add_req(req, &se->list);
2625 pthread_mutex_unlock(&se->lock);
2626 if (intr)
2627 fuse_reply_err(intr, EAGAIN);
2630 if ((buf->flags & FUSE_BUF_IS_FD) && write_header_size < buf->size &&
2631 (in->opcode != FUSE_WRITE || !se->op.write_buf) &&
2632 in->opcode != FUSE_NOTIFY_REPLY) {
2633 void *newmbuf;
2635 err = ENOMEM;
2636 newmbuf = realloc(mbuf, buf->size);
2637 if (newmbuf == NULL)
2638 goto reply_err;
2639 mbuf = newmbuf;
2641 tmpbuf = FUSE_BUFVEC_INIT(buf->size - write_header_size);
2642 tmpbuf.buf[0].mem = (char *)mbuf + write_header_size;
2644 res = fuse_ll_copy_from_pipe(&tmpbuf, &bufv);
2645 err = -res;
2646 if (res < 0)
2647 goto reply_err;
2649 in = mbuf;
2652 inarg = (void *) &in[1];
2653 if (in->opcode == FUSE_WRITE && se->op.write_buf)
2654 do_write_buf(req, in->nodeid, inarg, buf);
2655 else if (in->opcode == FUSE_NOTIFY_REPLY)
2656 do_notify_reply(req, in->nodeid, inarg, buf);
2657 else
2658 fuse_ll_ops[in->opcode].func(req, in->nodeid, inarg);
2660 out_free:
2661 free(mbuf);
2662 return;
2664 reply_err:
2665 fuse_reply_err(req, err);
2666 clear_pipe:
2667 if (buf->flags & FUSE_BUF_IS_FD)
2668 fuse_ll_clear_pipe(se);
2669 goto out_free;
2672 #define LL_OPTION(n,o,v) \
2673 { n, offsetof(struct fuse_session, o), v }
2675 static const struct fuse_opt fuse_ll_opts[] = {
2676 LL_OPTION("debug", debug, 1),
2677 LL_OPTION("-d", debug, 1),
2678 LL_OPTION("--debug", debug, 1),
2679 LL_OPTION("allow_root", deny_others, 1),
2680 FUSE_OPT_END
2683 void fuse_lowlevel_version(void)
2685 printf("using FUSE kernel interface version %i.%i\n",
2686 FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
2687 fuse_mount_version();
2690 void fuse_lowlevel_help(void)
2692 /* These are not all options, but the ones that are
2693 potentially of interest to an end-user */
2694 printf(
2695 " -o allow_other allow access by all users\n"
2696 " -o allow_root allow access by root\n"
2697 " -o auto_unmount auto unmount on process termination\n");
2700 void fuse_session_destroy(struct fuse_session *se)
2702 struct fuse_ll_pipe *llp;
2704 if (se->got_init && !se->got_destroy) {
2705 if (se->op.destroy)
2706 se->op.destroy(se->userdata);
2708 llp = pthread_getspecific(se->pipe_key);
2709 if (llp != NULL)
2710 fuse_ll_pipe_free(llp);
2711 pthread_key_delete(se->pipe_key);
2712 pthread_mutex_destroy(&se->lock);
2713 free(se->cuse_data);
2714 if (se->fd != -1)
2715 close(se->fd);
2716 destroy_mount_opts(se->mo);
2717 free(se);
2721 static void fuse_ll_pipe_destructor(void *data)
2723 struct fuse_ll_pipe *llp = data;
2724 fuse_ll_pipe_free(llp);
2727 int fuse_session_receive_buf(struct fuse_session *se, struct fuse_buf *buf)
2729 return fuse_session_receive_buf_int(se, buf, NULL);
2732 int fuse_session_receive_buf_int(struct fuse_session *se, struct fuse_buf *buf,
2733 struct fuse_chan *ch)
2735 int err;
2736 ssize_t res;
2737 #ifdef HAVE_SPLICE
2738 size_t bufsize = se->bufsize;
2739 struct fuse_ll_pipe *llp;
2740 struct fuse_buf tmpbuf;
2742 if (se->conn.proto_minor < 14 || !(se->conn.want & FUSE_CAP_SPLICE_READ))
2743 goto fallback;
2745 llp = fuse_ll_get_pipe(se);
2746 if (llp == NULL)
2747 goto fallback;
2749 if (llp->size < bufsize) {
2750 if (llp->can_grow) {
2751 res = fcntl(llp->pipe[0], F_SETPIPE_SZ, bufsize);
2752 if (res == -1) {
2753 llp->can_grow = 0;
2754 res = grow_pipe_to_max(llp->pipe[0]);
2755 if (res > 0)
2756 llp->size = res;
2757 goto fallback;
2759 llp->size = res;
2761 if (llp->size < bufsize)
2762 goto fallback;
2765 res = splice(ch ? ch->fd : se->fd,
2766 NULL, llp->pipe[1], NULL, bufsize, 0);
2767 err = errno;
2769 if (fuse_session_exited(se))
2770 return 0;
2772 if (res == -1) {
2773 if (err == ENODEV) {
2774 /* Filesystem was unmounted, or connection was aborted
2775 via /sys/fs/fuse/connections */
2776 fuse_session_exit(se);
2777 return 0;
2779 if (err != EINTR && err != EAGAIN)
2780 perror("fuse: splice from device");
2781 return -err;
2784 if (res < sizeof(struct fuse_in_header)) {
2785 fuse_log(FUSE_LOG_ERR, "short splice from fuse device\n");
2786 return -EIO;
2789 tmpbuf = (struct fuse_buf) {
2790 .size = res,
2791 .flags = FUSE_BUF_IS_FD,
2792 .fd = llp->pipe[0],
2796 * Don't bother with zero copy for small requests.
2797 * fuse_loop_mt() needs to check for FORGET so this more than
2798 * just an optimization.
2800 if (res < sizeof(struct fuse_in_header) +
2801 sizeof(struct fuse_write_in) + pagesize) {
2802 struct fuse_bufvec src = { .buf[0] = tmpbuf, .count = 1 };
2803 struct fuse_bufvec dst = { .count = 1 };
2805 if (!buf->mem) {
2806 buf->mem = malloc(se->bufsize);
2807 if (!buf->mem) {
2808 fuse_log(FUSE_LOG_ERR,
2809 "fuse: failed to allocate read buffer\n");
2810 return -ENOMEM;
2813 buf->size = se->bufsize;
2814 buf->flags = 0;
2815 dst.buf[0] = *buf;
2817 res = fuse_buf_copy(&dst, &src, 0);
2818 if (res < 0) {
2819 fuse_log(FUSE_LOG_ERR, "fuse: copy from pipe: %s\n",
2820 strerror(-res));
2821 fuse_ll_clear_pipe(se);
2822 return res;
2824 if (res < tmpbuf.size) {
2825 fuse_log(FUSE_LOG_ERR, "fuse: copy from pipe: short read\n");
2826 fuse_ll_clear_pipe(se);
2827 return -EIO;
2829 assert(res == tmpbuf.size);
2831 } else {
2832 /* Don't overwrite buf->mem, as that would cause a leak */
2833 buf->fd = tmpbuf.fd;
2834 buf->flags = tmpbuf.flags;
2836 buf->size = tmpbuf.size;
2838 return res;
2840 fallback:
2841 #endif
2842 if (!buf->mem) {
2843 buf->mem = malloc(se->bufsize);
2844 if (!buf->mem) {
2845 fuse_log(FUSE_LOG_ERR,
2846 "fuse: failed to allocate read buffer\n");
2847 return -ENOMEM;
2851 restart:
2852 res = read(ch ? ch->fd : se->fd, buf->mem, se->bufsize);
2853 err = errno;
2855 if (fuse_session_exited(se))
2856 return 0;
2857 if (res == -1) {
2858 /* ENOENT means the operation was interrupted, it's safe
2859 to restart */
2860 if (err == ENOENT)
2861 goto restart;
2863 if (err == ENODEV) {
2864 /* Filesystem was unmounted, or connection was aborted
2865 via /sys/fs/fuse/connections */
2866 fuse_session_exit(se);
2867 return 0;
2869 /* Errors occurring during normal operation: EINTR (read
2870 interrupted), EAGAIN (nonblocking I/O), ENODEV (filesystem
2871 umounted) */
2872 if (err != EINTR && err != EAGAIN)
2873 perror("fuse: reading device");
2874 return -err;
2876 if ((size_t) res < sizeof(struct fuse_in_header)) {
2877 fuse_log(FUSE_LOG_ERR, "short read on fuse device\n");
2878 return -EIO;
2881 buf->size = res;
2883 return res;
2886 struct fuse_session *fuse_session_new(struct fuse_args *args,
2887 const struct fuse_lowlevel_ops *op,
2888 size_t op_size, void *userdata)
2890 int err;
2891 struct fuse_session *se;
2892 struct mount_opts *mo;
2894 if (sizeof(struct fuse_lowlevel_ops) < op_size) {
2895 fuse_log(FUSE_LOG_ERR, "fuse: warning: library too old, some operations may not work\n");
2896 op_size = sizeof(struct fuse_lowlevel_ops);
2899 if (args->argc == 0) {
2900 fuse_log(FUSE_LOG_ERR, "fuse: empty argv passed to fuse_session_new().\n");
2901 return NULL;
2904 se = (struct fuse_session *) calloc(1, sizeof(struct fuse_session));
2905 if (se == NULL) {
2906 fuse_log(FUSE_LOG_ERR, "fuse: failed to allocate fuse object\n");
2907 goto out1;
2909 se->fd = -1;
2910 se->conn.max_write = UINT_MAX;
2911 se->conn.max_readahead = UINT_MAX;
2913 /* Parse options */
2914 if(fuse_opt_parse(args, se, fuse_ll_opts, NULL) == -1)
2915 goto out2;
2916 if(se->deny_others) {
2917 /* Allowing access only by root is done by instructing
2918 * kernel to allow access by everyone, and then restricting
2919 * access to root and mountpoint owner in libfuse.
2921 // We may be adding the option a second time, but
2922 // that doesn't hurt.
2923 if(fuse_opt_add_arg(args, "-oallow_other") == -1)
2924 goto out2;
2926 mo = parse_mount_opts(args);
2927 if (mo == NULL)
2928 goto out3;
2930 if(args->argc == 1 &&
2931 args->argv[0][0] == '-') {
2932 fuse_log(FUSE_LOG_ERR, "fuse: warning: argv[0] looks like an option, but "
2933 "will be ignored\n");
2934 } else if (args->argc != 1) {
2935 int i;
2936 fuse_log(FUSE_LOG_ERR, "fuse: unknown option(s): `");
2937 for(i = 1; i < args->argc-1; i++)
2938 fuse_log(FUSE_LOG_ERR, "%s ", args->argv[i]);
2939 fuse_log(FUSE_LOG_ERR, "%s'\n", args->argv[i]);
2940 goto out4;
2943 if (se->debug)
2944 fuse_log(FUSE_LOG_DEBUG, "FUSE library version: %s\n", PACKAGE_VERSION);
2946 se->bufsize = FUSE_MAX_MAX_PAGES * getpagesize() +
2947 FUSE_BUFFER_HEADER_SIZE;
2949 list_init_req(&se->list);
2950 list_init_req(&se->interrupts);
2951 list_init_nreq(&se->notify_list);
2952 se->notify_ctr = 1;
2953 fuse_mutex_init(&se->lock);
2955 err = pthread_key_create(&se->pipe_key, fuse_ll_pipe_destructor);
2956 if (err) {
2957 fuse_log(FUSE_LOG_ERR, "fuse: failed to create thread specific key: %s\n",
2958 strerror(err));
2959 goto out5;
2962 memcpy(&se->op, op, op_size);
2963 se->owner = getuid();
2964 se->userdata = userdata;
2966 se->mo = mo;
2967 return se;
2969 out5:
2970 pthread_mutex_destroy(&se->lock);
2971 out4:
2972 fuse_opt_free_args(args);
2973 out3:
2974 free(mo);
2975 out2:
2976 free(se);
2977 out1:
2978 return NULL;
2981 int fuse_session_mount(struct fuse_session *se, const char *mountpoint)
2983 int fd;
2986 * Make sure file descriptors 0, 1 and 2 are open, otherwise chaos
2987 * would ensue.
2989 do {
2990 fd = open("/dev/null", O_RDWR);
2991 if (fd > 2)
2992 close(fd);
2993 } while (fd >= 0 && fd <= 2);
2996 * To allow FUSE daemons to run without privileges, the caller may open
2997 * /dev/fuse before launching the file system and pass on the file
2998 * descriptor by specifying /dev/fd/N as the mount point. Note that the
2999 * parent process takes care of performing the mount in this case.
3001 fd = fuse_mnt_parse_fuse_fd(mountpoint);
3002 if (fd != -1) {
3003 if (fcntl(fd, F_GETFD) == -1) {
3004 fuse_log(FUSE_LOG_ERR,
3005 "fuse: Invalid file descriptor /dev/fd/%u\n",
3006 fd);
3007 return -1;
3009 se->fd = fd;
3010 return 0;
3013 /* Open channel */
3014 fd = fuse_kern_mount(mountpoint, se->mo);
3015 if (fd == -1)
3016 return -1;
3017 se->fd = fd;
3019 /* Save mountpoint */
3020 se->mountpoint = strdup(mountpoint);
3021 if (se->mountpoint == NULL)
3022 goto error_out;
3024 return 0;
3026 error_out:
3027 fuse_kern_unmount(mountpoint, fd);
3028 return -1;
3031 int fuse_session_fd(struct fuse_session *se)
3033 return se->fd;
3036 void fuse_session_unmount(struct fuse_session *se)
3038 if (se->mountpoint != NULL) {
3039 fuse_kern_unmount(se->mountpoint, se->fd);
3040 free(se->mountpoint);
3041 se->mountpoint = NULL;
3045 #ifdef linux
3046 int fuse_req_getgroups(fuse_req_t req, int size, gid_t list[])
3048 char *buf;
3049 size_t bufsize = 1024;
3050 char path[128];
3051 int ret;
3052 int fd;
3053 unsigned long pid = req->ctx.pid;
3054 char *s;
3056 sprintf(path, "/proc/%lu/task/%lu/status", pid, pid);
3058 retry:
3059 buf = malloc(bufsize);
3060 if (buf == NULL)
3061 return -ENOMEM;
3063 ret = -EIO;
3064 fd = open(path, O_RDONLY);
3065 if (fd == -1)
3066 goto out_free;
3068 ret = read(fd, buf, bufsize);
3069 close(fd);
3070 if (ret < 0) {
3071 ret = -EIO;
3072 goto out_free;
3075 if ((size_t)ret == bufsize) {
3076 free(buf);
3077 bufsize *= 4;
3078 goto retry;
3081 ret = -EIO;
3082 s = strstr(buf, "\nGroups:");
3083 if (s == NULL)
3084 goto out_free;
3086 s += 8;
3087 ret = 0;
3088 while (1) {
3089 char *end;
3090 unsigned long val = strtoul(s, &end, 0);
3091 if (end == s)
3092 break;
3094 s = end;
3095 if (ret < size)
3096 list[ret] = val;
3097 ret++;
3100 out_free:
3101 free(buf);
3102 return ret;
3104 #else /* linux */
3106 * This is currently not implemented on other than Linux...
3108 int fuse_req_getgroups(fuse_req_t req, int size, gid_t list[])
3110 (void) req; (void) size; (void) list;
3111 return -ENOSYS;
3113 #endif
3115 void fuse_session_exit(struct fuse_session *se)
3117 se->exited = 1;
3120 void fuse_session_reset(struct fuse_session *se)
3122 se->exited = 0;
3123 se->error = 0;
3126 int fuse_session_exited(struct fuse_session *se)
3128 return se->exited;