libfuse: Notifying the kernel of deletion.
[fuse.git] / lib / fuse_lowlevel.c
blob3bfc9932d652ce20f4be451b6afaa0a75a383f0b
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 "fuse_i.h"
12 #include "fuse_kernel.h"
13 #include "fuse_opt.h"
14 #include "fuse_misc.h"
15 #include "fuse_common_compat.h"
16 #include "fuse_lowlevel_compat.h"
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <stddef.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include <limits.h>
24 #include <errno.h>
25 #include <assert.h>
27 #ifndef F_LINUX_SPECIFIC_BASE
28 #define F_LINUX_SPECIFIC_BASE 1024
29 #endif
30 #ifndef F_SETPIPE_SZ
31 #define F_SETPIPE_SZ (F_LINUX_SPECIFIC_BASE + 7)
32 #endif
35 #define PARAM(inarg) (((char *)(inarg)) + sizeof(*(inarg)))
36 #define OFFSET_MAX 0x7fffffffffffffffLL
38 #define container_of(ptr, type, member) ({ \
39 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
40 (type *)( (char *)__mptr - offsetof(type,member) );})
42 struct fuse_pollhandle {
43 uint64_t kh;
44 struct fuse_chan *ch;
45 struct fuse_ll *f;
48 static size_t pagesize;
50 static __attribute__((constructor)) void fuse_ll_init_pagesize(void)
52 pagesize = getpagesize();
55 static void convert_stat(const struct stat *stbuf, struct fuse_attr *attr)
57 attr->ino = stbuf->st_ino;
58 attr->mode = stbuf->st_mode;
59 attr->nlink = stbuf->st_nlink;
60 attr->uid = stbuf->st_uid;
61 attr->gid = stbuf->st_gid;
62 attr->rdev = stbuf->st_rdev;
63 attr->size = stbuf->st_size;
64 attr->blksize = stbuf->st_blksize;
65 attr->blocks = stbuf->st_blocks;
66 attr->atime = stbuf->st_atime;
67 attr->mtime = stbuf->st_mtime;
68 attr->ctime = stbuf->st_ctime;
69 attr->atimensec = ST_ATIM_NSEC(stbuf);
70 attr->mtimensec = ST_MTIM_NSEC(stbuf);
71 attr->ctimensec = ST_CTIM_NSEC(stbuf);
74 static void convert_attr(const struct fuse_setattr_in *attr, struct stat *stbuf)
76 stbuf->st_mode = attr->mode;
77 stbuf->st_uid = attr->uid;
78 stbuf->st_gid = attr->gid;
79 stbuf->st_size = attr->size;
80 stbuf->st_atime = attr->atime;
81 stbuf->st_mtime = attr->mtime;
82 ST_ATIM_NSEC_SET(stbuf, attr->atimensec);
83 ST_MTIM_NSEC_SET(stbuf, attr->mtimensec);
86 static size_t iov_length(const struct iovec *iov, size_t count)
88 size_t seg;
89 size_t ret = 0;
91 for (seg = 0; seg < count; seg++)
92 ret += iov[seg].iov_len;
93 return ret;
96 static void list_init_req(struct fuse_req *req)
98 req->next = req;
99 req->prev = req;
102 static void list_del_req(struct fuse_req *req)
104 struct fuse_req *prev = req->prev;
105 struct fuse_req *next = req->next;
106 prev->next = next;
107 next->prev = prev;
110 static void list_add_req(struct fuse_req *req, struct fuse_req *next)
112 struct fuse_req *prev = next->prev;
113 req->next = next;
114 req->prev = prev;
115 prev->next = req;
116 next->prev = req;
119 static void destroy_req(fuse_req_t req)
121 pthread_mutex_destroy(&req->lock);
122 free(req);
125 void fuse_free_req(fuse_req_t req)
127 int ctr;
128 struct fuse_ll *f = req->f;
130 pthread_mutex_lock(&f->lock);
131 req->u.ni.func = NULL;
132 req->u.ni.data = NULL;
133 list_del_req(req);
134 ctr = --req->ctr;
135 pthread_mutex_unlock(&f->lock);
136 if (!ctr)
137 destroy_req(req);
140 static struct fuse_req *fuse_ll_alloc_req(struct fuse_ll *f)
142 struct fuse_req *req;
144 req = (struct fuse_req *) calloc(1, sizeof(struct fuse_req));
145 if (req == NULL) {
146 fprintf(stderr, "fuse: failed to allocate request\n");
147 } else {
148 req->f = f;
149 req->ctr = 1;
150 list_init_req(req);
151 fuse_mutex_init(&req->lock);
154 return req;
158 static int fuse_send_msg(struct fuse_ll *f, struct fuse_chan *ch,
159 struct iovec *iov, int count)
161 struct fuse_out_header *out = iov[0].iov_base;
163 out->len = iov_length(iov, count);
164 if (f->debug) {
165 if (out->unique == 0) {
166 fprintf(stderr, "NOTIFY: code=%d length=%u\n",
167 out->error, out->len);
168 } else if (out->error) {
169 fprintf(stderr,
170 " unique: %llu, error: %i (%s), outsize: %i\n",
171 (unsigned long long) out->unique, out->error,
172 strerror(-out->error), out->len);
173 } else {
174 fprintf(stderr,
175 " unique: %llu, success, outsize: %i\n",
176 (unsigned long long) out->unique, out->len);
180 return fuse_chan_send(ch, iov, count);
183 int fuse_send_reply_iov_nofree(fuse_req_t req, int error, struct iovec *iov,
184 int count)
186 struct fuse_out_header out;
188 if (error <= -1000 || error > 0) {
189 fprintf(stderr, "fuse: bad error value: %i\n", error);
190 error = -ERANGE;
193 out.unique = req->unique;
194 out.error = error;
196 iov[0].iov_base = &out;
197 iov[0].iov_len = sizeof(struct fuse_out_header);
199 return fuse_send_msg(req->f, req->ch, iov, count);
202 static int send_reply_iov(fuse_req_t req, int error, struct iovec *iov,
203 int count)
205 int res;
207 res = fuse_send_reply_iov_nofree(req, error, iov, count);
208 fuse_free_req(req);
209 return res;
212 static int send_reply(fuse_req_t req, int error, const void *arg,
213 size_t argsize)
215 struct iovec iov[2];
216 int count = 1;
217 if (argsize) {
218 iov[1].iov_base = (void *) arg;
219 iov[1].iov_len = argsize;
220 count++;
222 return send_reply_iov(req, error, iov, count);
225 int fuse_reply_iov(fuse_req_t req, const struct iovec *iov, int count)
227 int res;
228 struct iovec *padded_iov;
230 padded_iov = malloc((count + 1) * sizeof(struct iovec));
231 if (padded_iov == NULL)
232 return fuse_reply_err(req, ENOMEM);
234 memcpy(padded_iov + 1, iov, count * sizeof(struct iovec));
235 count++;
237 res = send_reply_iov(req, 0, padded_iov, count);
238 free(padded_iov);
240 return res;
243 size_t fuse_dirent_size(size_t namelen)
245 return FUSE_DIRENT_ALIGN(FUSE_NAME_OFFSET + namelen);
248 char *fuse_add_dirent(char *buf, const char *name, const struct stat *stbuf,
249 off_t off)
251 unsigned namelen = strlen(name);
252 unsigned entlen = FUSE_NAME_OFFSET + namelen;
253 unsigned entsize = fuse_dirent_size(namelen);
254 unsigned padlen = entsize - entlen;
255 struct fuse_dirent *dirent = (struct fuse_dirent *) buf;
257 dirent->ino = stbuf->st_ino;
258 dirent->off = off;
259 dirent->namelen = namelen;
260 dirent->type = (stbuf->st_mode & 0170000) >> 12;
261 strncpy(dirent->name, name, namelen);
262 if (padlen)
263 memset(buf + entlen, 0, padlen);
265 return buf + entsize;
268 size_t fuse_add_direntry(fuse_req_t req, char *buf, size_t bufsize,
269 const char *name, const struct stat *stbuf, off_t off)
271 size_t entsize;
273 (void) req;
274 entsize = fuse_dirent_size(strlen(name));
275 if (entsize <= bufsize && buf)
276 fuse_add_dirent(buf, name, stbuf, off);
277 return entsize;
280 static void convert_statfs(const struct statvfs *stbuf,
281 struct fuse_kstatfs *kstatfs)
283 kstatfs->bsize = stbuf->f_bsize;
284 kstatfs->frsize = stbuf->f_frsize;
285 kstatfs->blocks = stbuf->f_blocks;
286 kstatfs->bfree = stbuf->f_bfree;
287 kstatfs->bavail = stbuf->f_bavail;
288 kstatfs->files = stbuf->f_files;
289 kstatfs->ffree = stbuf->f_ffree;
290 kstatfs->namelen = stbuf->f_namemax;
293 static int send_reply_ok(fuse_req_t req, const void *arg, size_t argsize)
295 return send_reply(req, 0, arg, argsize);
298 int fuse_reply_err(fuse_req_t req, int err)
300 return send_reply(req, -err, NULL, 0);
303 void fuse_reply_none(fuse_req_t req)
305 if (req->ch)
306 fuse_chan_send(req->ch, NULL, 0);
307 fuse_free_req(req);
310 static unsigned long calc_timeout_sec(double t)
312 if (t > (double) ULONG_MAX)
313 return ULONG_MAX;
314 else if (t < 0.0)
315 return 0;
316 else
317 return (unsigned long) t;
320 static unsigned int calc_timeout_nsec(double t)
322 double f = t - (double) calc_timeout_sec(t);
323 if (f < 0.0)
324 return 0;
325 else if (f >= 0.999999999)
326 return 999999999;
327 else
328 return (unsigned int) (f * 1.0e9);
331 static void fill_entry(struct fuse_entry_out *arg,
332 const struct fuse_entry_param *e)
334 arg->nodeid = e->ino;
335 arg->generation = e->generation;
336 arg->entry_valid = calc_timeout_sec(e->entry_timeout);
337 arg->entry_valid_nsec = calc_timeout_nsec(e->entry_timeout);
338 arg->attr_valid = calc_timeout_sec(e->attr_timeout);
339 arg->attr_valid_nsec = calc_timeout_nsec(e->attr_timeout);
340 convert_stat(&e->attr, &arg->attr);
343 static void fill_open(struct fuse_open_out *arg,
344 const struct fuse_file_info *f)
346 arg->fh = f->fh;
347 if (f->direct_io)
348 arg->open_flags |= FOPEN_DIRECT_IO;
349 if (f->keep_cache)
350 arg->open_flags |= FOPEN_KEEP_CACHE;
351 if (f->nonseekable)
352 arg->open_flags |= FOPEN_NONSEEKABLE;
355 int fuse_reply_entry(fuse_req_t req, const struct fuse_entry_param *e)
357 struct fuse_entry_out arg;
358 size_t size = req->f->conn.proto_minor < 9 ?
359 FUSE_COMPAT_ENTRY_OUT_SIZE : sizeof(arg);
361 /* before ABI 7.4 e->ino == 0 was invalid, only ENOENT meant
362 negative entry */
363 if (!e->ino && req->f->conn.proto_minor < 4)
364 return fuse_reply_err(req, ENOENT);
366 memset(&arg, 0, sizeof(arg));
367 fill_entry(&arg, e);
368 return send_reply_ok(req, &arg, size);
371 int fuse_reply_create(fuse_req_t req, const struct fuse_entry_param *e,
372 const struct fuse_file_info *f)
374 char buf[sizeof(struct fuse_entry_out) + sizeof(struct fuse_open_out)];
375 size_t entrysize = req->f->conn.proto_minor < 9 ?
376 FUSE_COMPAT_ENTRY_OUT_SIZE : sizeof(struct fuse_entry_out);
377 struct fuse_entry_out *earg = (struct fuse_entry_out *) buf;
378 struct fuse_open_out *oarg = (struct fuse_open_out *) (buf + entrysize);
380 memset(buf, 0, sizeof(buf));
381 fill_entry(earg, e);
382 fill_open(oarg, f);
383 return send_reply_ok(req, buf,
384 entrysize + sizeof(struct fuse_open_out));
387 int fuse_reply_attr(fuse_req_t req, const struct stat *attr,
388 double attr_timeout)
390 struct fuse_attr_out arg;
391 size_t size = req->f->conn.proto_minor < 9 ?
392 FUSE_COMPAT_ATTR_OUT_SIZE : sizeof(arg);
394 memset(&arg, 0, sizeof(arg));
395 arg.attr_valid = calc_timeout_sec(attr_timeout);
396 arg.attr_valid_nsec = calc_timeout_nsec(attr_timeout);
397 convert_stat(attr, &arg.attr);
399 return send_reply_ok(req, &arg, size);
402 int fuse_reply_readlink(fuse_req_t req, const char *linkname)
404 return send_reply_ok(req, linkname, strlen(linkname));
407 int fuse_reply_open(fuse_req_t req, const struct fuse_file_info *f)
409 struct fuse_open_out arg;
411 memset(&arg, 0, sizeof(arg));
412 fill_open(&arg, f);
413 return send_reply_ok(req, &arg, sizeof(arg));
416 int fuse_reply_write(fuse_req_t req, size_t count)
418 struct fuse_write_out arg;
420 memset(&arg, 0, sizeof(arg));
421 arg.size = count;
423 return send_reply_ok(req, &arg, sizeof(arg));
426 int fuse_reply_buf(fuse_req_t req, const char *buf, size_t size)
428 return send_reply_ok(req, buf, size);
431 static int fuse_send_data_iov_fallback(struct fuse_ll *f, struct fuse_chan *ch,
432 struct iovec *iov, int iov_count,
433 struct fuse_bufvec *buf,
434 size_t len)
436 struct fuse_bufvec mem_buf = FUSE_BUFVEC_INIT(len);
437 void *mbuf;
438 int res;
440 /* Optimize common case */
441 if (buf->count == 1 && buf->idx == 0 && buf->off == 0 &&
442 !(buf->buf[0].flags & FUSE_BUF_IS_FD)) {
443 /* FIXME: also avoid memory copy if there are multiple buffers
444 but none of them contain an fd */
446 iov[iov_count].iov_base = buf->buf[0].mem;
447 iov[iov_count].iov_len = len;
448 iov_count++;
449 return fuse_send_msg(f, ch, iov, iov_count);
452 res = posix_memalign(&mbuf, pagesize, len);
453 if (res != 0)
454 return res;
456 mem_buf.buf[0].mem = mbuf;
457 res = fuse_buf_copy(&mem_buf, buf, 0);
458 if (res < 0) {
459 free(mbuf);
460 return -res;
462 len = res;
464 iov[iov_count].iov_base = mbuf;
465 iov[iov_count].iov_len = len;
466 iov_count++;
467 res = fuse_send_msg(f, ch, iov, iov_count);
468 free(mbuf);
470 return res;
473 struct fuse_ll_pipe {
474 size_t size;
475 int can_grow;
476 int pipe[2];
479 static void fuse_ll_pipe_free(struct fuse_ll_pipe *llp)
481 close(llp->pipe[0]);
482 close(llp->pipe[1]);
483 free(llp);
486 #ifdef HAVE_SPLICE
487 static struct fuse_ll_pipe *fuse_ll_get_pipe(struct fuse_ll *f)
489 struct fuse_ll_pipe *llp = pthread_getspecific(f->pipe_key);
490 if (llp == NULL) {
491 int res;
493 llp = malloc(sizeof(struct fuse_ll_pipe));
494 if (llp == NULL)
495 return NULL;
497 res = pipe(llp->pipe);
498 if (res == -1) {
499 free(llp);
500 return NULL;
503 if (fcntl(llp->pipe[0], F_SETFL, O_NONBLOCK) == -1 ||
504 fcntl(llp->pipe[1], F_SETFL, O_NONBLOCK) == -1) {
505 close(llp->pipe[0]);
506 close(llp->pipe[1]);
507 free(llp);
508 return NULL;
512 *the default size is 16 pages on linux
514 llp->size = pagesize * 16;
515 llp->can_grow = 1;
517 pthread_setspecific(f->pipe_key, llp);
520 return llp;
522 #endif
524 static void fuse_ll_clear_pipe(struct fuse_ll *f)
526 struct fuse_ll_pipe *llp = pthread_getspecific(f->pipe_key);
527 if (llp) {
528 pthread_setspecific(f->pipe_key, NULL);
529 fuse_ll_pipe_free(llp);
533 #if defined(HAVE_SPLICE) && defined(HAVE_VMSPLICE)
534 static int read_back(int fd, char *buf, size_t len)
536 int res;
538 res = read(fd, buf, len);
539 if (res == -1) {
540 fprintf(stderr, "fuse: internal error: failed to read back from pipe: %s\n", strerror(errno));
541 return -EIO;
543 if (res != len) {
544 fprintf(stderr, "fuse: internal error: short read back from pipe: %i from %zi\n", res, len);
545 return -EIO;
547 return 0;
550 static int fuse_send_data_iov(struct fuse_ll *f, struct fuse_chan *ch,
551 struct iovec *iov, int iov_count,
552 struct fuse_bufvec *buf, unsigned int flags)
554 int res;
555 size_t len = fuse_buf_size(buf);
556 struct fuse_out_header *out = iov[0].iov_base;
557 struct fuse_ll_pipe *llp;
558 int splice_flags;
559 size_t pipesize;
560 size_t total_fd_size;
561 size_t idx;
562 size_t headerlen;
563 struct fuse_bufvec pipe_buf = FUSE_BUFVEC_INIT(len);
565 if (f->broken_splice_nonblock)
566 goto fallback;
568 if (flags & FUSE_BUF_NO_SPLICE)
569 goto fallback;
571 total_fd_size = 0;
572 for (idx = buf->idx; idx < buf->count; idx++) {
573 if (buf->buf[idx].flags & FUSE_BUF_IS_FD) {
574 total_fd_size = buf->buf[idx].size;
575 if (idx == buf->idx)
576 total_fd_size -= buf->off;
579 if (total_fd_size < 2 * pagesize)
580 goto fallback;
582 if (f->conn.proto_minor < 14 ||
583 !(f->conn.want & FUSE_CAP_SPLICE_WRITE))
584 goto fallback;
586 llp = fuse_ll_get_pipe(f);
587 if (llp == NULL)
588 goto fallback;
591 headerlen = iov_length(iov, iov_count);
593 out->len = headerlen + len;
596 * Heuristic for the required pipe size, does not work if the
597 * source contains less than page size fragments
599 pipesize = pagesize * (iov_count + buf->count + 1) + out->len;
601 if (llp->size < pipesize) {
602 if (llp->can_grow) {
603 res = fcntl(llp->pipe[0], F_SETPIPE_SZ, pipesize);
604 if (res == -1) {
605 llp->can_grow = 0;
606 goto fallback;
608 llp->size = res;
610 if (llp->size < pipesize)
611 goto fallback;
615 res = vmsplice(llp->pipe[1], iov, iov_count, SPLICE_F_NONBLOCK);
616 if (res == -1)
617 goto fallback;
619 if (res != headerlen) {
620 res = -EIO;
621 fprintf(stderr, "fuse: short vmsplice to pipe: %u/%zu\n", res,
622 headerlen);
623 goto clear_pipe;
626 pipe_buf.buf[0].flags = FUSE_BUF_IS_FD;
627 pipe_buf.buf[0].fd = llp->pipe[1];
629 res = fuse_buf_copy(&pipe_buf, buf,
630 FUSE_BUF_FORCE_SPLICE | FUSE_BUF_SPLICE_NONBLOCK);
631 if (res < 0) {
632 if (res == -EAGAIN || res == -EINVAL) {
634 * Should only get EAGAIN on kernels with
635 * broken SPLICE_F_NONBLOCK support (<=
636 * 2.6.35) where this error or a short read is
637 * returned even if the pipe itself is not
638 * full
640 * EINVAL might mean that splice can't handle
641 * this combination of input and output.
643 if (res == -EAGAIN)
644 f->broken_splice_nonblock = 1;
646 pthread_setspecific(f->pipe_key, NULL);
647 fuse_ll_pipe_free(llp);
648 goto fallback;
650 res = -res;
651 goto clear_pipe;
654 if (res != 0 && res < len) {
655 struct fuse_bufvec mem_buf = FUSE_BUFVEC_INIT(len);
656 void *mbuf;
657 size_t now_len = res;
659 * For regular files a short count is either
660 * 1) due to EOF, or
661 * 2) because of broken SPLICE_F_NONBLOCK (see above)
663 * For other inputs it's possible that we overflowed
664 * the pipe because of small buffer fragments.
667 res = posix_memalign(&mbuf, pagesize, len);
668 if (res != 0)
669 goto clear_pipe;
671 mem_buf.buf[0].mem = mbuf;
672 mem_buf.off = now_len;
673 res = fuse_buf_copy(&mem_buf, buf, 0);
674 if (res > 0) {
675 char *tmpbuf;
676 size_t extra_len = res;
678 * Trickiest case: got more data. Need to get
679 * back the data from the pipe and then fall
680 * back to regular write.
682 tmpbuf = malloc(headerlen);
683 if (tmpbuf == NULL) {
684 free(mbuf);
685 res = ENOMEM;
686 goto clear_pipe;
688 res = read_back(llp->pipe[0], tmpbuf, headerlen);
689 if (res != 0) {
690 free(mbuf);
691 goto clear_pipe;
693 free(tmpbuf);
694 res = read_back(llp->pipe[0], mbuf, now_len);
695 if (res != 0) {
696 free(mbuf);
697 goto clear_pipe;
699 len = now_len + extra_len;
700 iov[iov_count].iov_base = mbuf;
701 iov[iov_count].iov_len = len;
702 iov_count++;
703 res = fuse_send_msg(f, ch, iov, iov_count);
704 free(mbuf);
705 return res;
707 free(mbuf);
708 res = now_len;
710 len = res;
711 out->len = headerlen + len;
713 if (f->debug) {
714 fprintf(stderr,
715 " unique: %llu, success, outsize: %i (splice)\n",
716 (unsigned long long) out->unique, out->len);
719 splice_flags = 0;
720 if ((flags & FUSE_BUF_SPLICE_MOVE) &&
721 (f->conn.want & FUSE_CAP_SPLICE_MOVE))
722 splice_flags |= SPLICE_F_MOVE;
724 res = splice(llp->pipe[0], NULL,
725 fuse_chan_fd(ch), NULL, out->len, splice_flags);
726 if (res == -1) {
727 res = -errno;
728 perror("fuse: splice from pipe");
729 goto clear_pipe;
731 if (res != out->len) {
732 res = -EIO;
733 fprintf(stderr, "fuse: short splice from pipe: %u/%u\n",
734 res, out->len);
735 goto clear_pipe;
737 return 0;
739 clear_pipe:
740 fuse_ll_clear_pipe(f);
741 return res;
743 fallback:
744 return fuse_send_data_iov_fallback(f, ch, iov, iov_count, buf, len);
746 #else
747 static int fuse_send_data_iov(struct fuse_ll *f, struct fuse_chan *ch,
748 struct iovec *iov, int iov_count,
749 struct fuse_bufvec *buf, unsigned int flags)
751 size_t len = fuse_buf_size(buf);
752 (void) flags;
754 return fuse_send_data_iov_fallback(f, ch, iov, iov_count, buf, len);
756 #endif
758 int fuse_reply_data(fuse_req_t req, struct fuse_bufvec *bufv,
759 enum fuse_buf_copy_flags flags)
761 struct iovec iov[2];
762 struct fuse_out_header out;
763 int res;
765 iov[0].iov_base = &out;
766 iov[0].iov_len = sizeof(struct fuse_out_header);
768 out.unique = req->unique;
769 out.error = 0;
771 res = fuse_send_data_iov(req->f, req->ch, iov, 1, bufv, flags);
772 if (res <= 0)
773 return res;
774 else
775 return fuse_reply_err(req, res);
778 int fuse_reply_statfs(fuse_req_t req, const struct statvfs *stbuf)
780 struct fuse_statfs_out arg;
781 size_t size = req->f->conn.proto_minor < 4 ?
782 FUSE_COMPAT_STATFS_SIZE : sizeof(arg);
784 memset(&arg, 0, sizeof(arg));
785 convert_statfs(stbuf, &arg.st);
787 return send_reply_ok(req, &arg, size);
790 int fuse_reply_xattr(fuse_req_t req, size_t count)
792 struct fuse_getxattr_out arg;
794 memset(&arg, 0, sizeof(arg));
795 arg.size = count;
797 return send_reply_ok(req, &arg, sizeof(arg));
800 int fuse_reply_lock(fuse_req_t req, const struct flock *lock)
802 struct fuse_lk_out arg;
804 memset(&arg, 0, sizeof(arg));
805 arg.lk.type = lock->l_type;
806 if (lock->l_type != F_UNLCK) {
807 arg.lk.start = lock->l_start;
808 if (lock->l_len == 0)
809 arg.lk.end = OFFSET_MAX;
810 else
811 arg.lk.end = lock->l_start + lock->l_len - 1;
813 arg.lk.pid = lock->l_pid;
814 return send_reply_ok(req, &arg, sizeof(arg));
817 int fuse_reply_bmap(fuse_req_t req, uint64_t idx)
819 struct fuse_bmap_out arg;
821 memset(&arg, 0, sizeof(arg));
822 arg.block = idx;
824 return send_reply_ok(req, &arg, sizeof(arg));
827 static struct fuse_ioctl_iovec *fuse_ioctl_iovec_copy(const struct iovec *iov,
828 size_t count)
830 struct fuse_ioctl_iovec *fiov;
831 size_t i;
833 fiov = malloc(sizeof(fiov[0]) * count);
834 if (!fiov)
835 return NULL;
837 for (i = 0; i < count; i++) {
838 fiov[i].base = (uintptr_t) iov[i].iov_base;
839 fiov[i].len = iov[i].iov_len;
842 return fiov;
845 int fuse_reply_ioctl_retry(fuse_req_t req,
846 const struct iovec *in_iov, size_t in_count,
847 const struct iovec *out_iov, size_t out_count)
849 struct fuse_ioctl_out arg;
850 struct fuse_ioctl_iovec *in_fiov = NULL;
851 struct fuse_ioctl_iovec *out_fiov = NULL;
852 struct iovec iov[4];
853 size_t count = 1;
854 int res;
856 memset(&arg, 0, sizeof(arg));
857 arg.flags |= FUSE_IOCTL_RETRY;
858 arg.in_iovs = in_count;
859 arg.out_iovs = out_count;
860 iov[count].iov_base = &arg;
861 iov[count].iov_len = sizeof(arg);
862 count++;
864 if (req->f->conn.proto_minor < 16) {
865 if (in_count) {
866 iov[count].iov_base = (void *)in_iov;
867 iov[count].iov_len = sizeof(in_iov[0]) * in_count;
868 count++;
871 if (out_count) {
872 iov[count].iov_base = (void *)out_iov;
873 iov[count].iov_len = sizeof(out_iov[0]) * out_count;
874 count++;
876 } else {
877 /* Can't handle non-compat 64bit ioctls on 32bit */
878 if (sizeof(void *) == 4 && req->ioctl_64bit) {
879 res = fuse_reply_err(req, EINVAL);
880 goto out;
883 if (in_count) {
884 in_fiov = fuse_ioctl_iovec_copy(in_iov, in_count);
885 if (!in_fiov)
886 goto enomem;
888 iov[count].iov_base = (void *)in_fiov;
889 iov[count].iov_len = sizeof(in_fiov[0]) * in_count;
890 count++;
892 if (out_count) {
893 out_fiov = fuse_ioctl_iovec_copy(out_iov, out_count);
894 if (!out_fiov)
895 goto enomem;
897 iov[count].iov_base = (void *)out_fiov;
898 iov[count].iov_len = sizeof(out_fiov[0]) * out_count;
899 count++;
903 res = send_reply_iov(req, 0, iov, count);
904 out:
905 free(in_fiov);
906 free(out_fiov);
908 return res;
910 enomem:
911 res = fuse_reply_err(req, ENOMEM);
912 goto out;
915 int fuse_reply_ioctl(fuse_req_t req, int result, const void *buf, size_t size)
917 struct fuse_ioctl_out arg;
918 struct iovec iov[3];
919 size_t count = 1;
921 memset(&arg, 0, sizeof(arg));
922 arg.result = result;
923 iov[count].iov_base = &arg;
924 iov[count].iov_len = sizeof(arg);
925 count++;
927 if (size) {
928 iov[count].iov_base = (char *) buf;
929 iov[count].iov_len = size;
930 count++;
933 return send_reply_iov(req, 0, iov, count);
936 int fuse_reply_ioctl_iov(fuse_req_t req, int result, const struct iovec *iov,
937 int count)
939 struct iovec *padded_iov;
940 struct fuse_ioctl_out arg;
941 int res;
943 padded_iov = malloc((count + 2) * sizeof(struct iovec));
944 if (padded_iov == NULL)
945 return fuse_reply_err(req, ENOMEM);
947 memset(&arg, 0, sizeof(arg));
948 arg.result = result;
949 padded_iov[1].iov_base = &arg;
950 padded_iov[1].iov_len = sizeof(arg);
952 memcpy(&padded_iov[2], iov, count * sizeof(struct iovec));
954 res = send_reply_iov(req, 0, padded_iov, count + 2);
955 free(padded_iov);
957 return res;
960 int fuse_reply_poll(fuse_req_t req, unsigned revents)
962 struct fuse_poll_out arg;
964 memset(&arg, 0, sizeof(arg));
965 arg.revents = revents;
967 return send_reply_ok(req, &arg, sizeof(arg));
970 int fuse_reply_mmap(fuse_req_t req, uint64_t map_id, size_t length)
972 struct fuse_mmap_out arg;
974 memset(&arg, 0, sizeof(arg));
975 arg.mapid = map_id;
976 arg.size = length;
978 return send_reply_ok(req, &arg, sizeof(arg));
981 static void do_lookup(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
983 char *name = (char *) inarg;
985 if (req->f->op.lookup)
986 req->f->op.lookup(req, nodeid, name);
987 else
988 fuse_reply_err(req, ENOSYS);
991 static void do_forget(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
993 struct fuse_forget_in *arg = (struct fuse_forget_in *) inarg;
995 if (req->f->op.forget)
996 req->f->op.forget(req, nodeid, arg->nlookup);
997 else
998 fuse_reply_none(req);
1001 static void do_batch_forget(fuse_req_t req, fuse_ino_t nodeid,
1002 const void *inarg)
1004 struct fuse_batch_forget_in *arg = (void *) inarg;
1005 struct fuse_forget_one *param = (void *) PARAM(arg);
1006 unsigned int i;
1008 (void) nodeid;
1010 if (req->f->op.forget_multi) {
1011 req->f->op.forget_multi(req, arg->count,
1012 (struct fuse_forget_data *) param);
1013 } else if (req->f->op.forget) {
1014 for (i = 0; i < arg->count; i++) {
1015 struct fuse_forget_one *forget = &param[i];
1016 struct fuse_req *dummy_req;
1018 dummy_req = fuse_ll_alloc_req(req->f);
1019 if (dummy_req == NULL)
1020 break;
1022 dummy_req->unique = req->unique;
1023 dummy_req->ctx = req->ctx;
1024 dummy_req->ch = NULL;
1026 req->f->op.forget(dummy_req, forget->nodeid,
1027 forget->nlookup);
1029 fuse_reply_none(req);
1030 } else {
1031 fuse_reply_none(req);
1035 static void do_getattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1037 struct fuse_file_info *fip = NULL;
1038 struct fuse_file_info fi;
1040 if (req->f->conn.proto_minor >= 9) {
1041 struct fuse_getattr_in *arg = (struct fuse_getattr_in *) inarg;
1043 if (arg->getattr_flags & FUSE_GETATTR_FH) {
1044 memset(&fi, 0, sizeof(fi));
1045 fi.fh = arg->fh;
1046 fi.fh_old = fi.fh;
1047 fip = &fi;
1051 if (req->f->op.getattr)
1052 req->f->op.getattr(req, nodeid, fip);
1053 else
1054 fuse_reply_err(req, ENOSYS);
1057 static void do_setattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1059 struct fuse_setattr_in *arg = (struct fuse_setattr_in *) inarg;
1061 if (req->f->op.setattr) {
1062 struct fuse_file_info *fi = NULL;
1063 struct fuse_file_info fi_store;
1064 struct stat stbuf;
1065 memset(&stbuf, 0, sizeof(stbuf));
1066 convert_attr(arg, &stbuf);
1067 if (arg->valid & FATTR_FH) {
1068 arg->valid &= ~FATTR_FH;
1069 memset(&fi_store, 0, sizeof(fi_store));
1070 fi = &fi_store;
1071 fi->fh = arg->fh;
1072 fi->fh_old = fi->fh;
1074 arg->valid &=
1075 FUSE_SET_ATTR_MODE |
1076 FUSE_SET_ATTR_UID |
1077 FUSE_SET_ATTR_GID |
1078 FUSE_SET_ATTR_SIZE |
1079 FUSE_SET_ATTR_ATIME |
1080 FUSE_SET_ATTR_MTIME |
1081 FUSE_SET_ATTR_ATIME_NOW |
1082 FUSE_SET_ATTR_MTIME_NOW;
1084 req->f->op.setattr(req, nodeid, &stbuf, arg->valid, fi);
1085 } else
1086 fuse_reply_err(req, ENOSYS);
1089 static void do_access(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1091 struct fuse_access_in *arg = (struct fuse_access_in *) inarg;
1093 if (req->f->op.access)
1094 req->f->op.access(req, nodeid, arg->mask);
1095 else
1096 fuse_reply_err(req, ENOSYS);
1099 static void do_readlink(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1101 (void) inarg;
1103 if (req->f->op.readlink)
1104 req->f->op.readlink(req, nodeid);
1105 else
1106 fuse_reply_err(req, ENOSYS);
1109 static void do_mknod(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1111 struct fuse_mknod_in *arg = (struct fuse_mknod_in *) inarg;
1112 char *name = PARAM(arg);
1114 if (req->f->conn.proto_minor >= 12)
1115 req->ctx.umask = arg->umask;
1116 else
1117 name = (char *) inarg + FUSE_COMPAT_MKNOD_IN_SIZE;
1119 if (req->f->op.mknod)
1120 req->f->op.mknod(req, nodeid, name, arg->mode, arg->rdev);
1121 else
1122 fuse_reply_err(req, ENOSYS);
1125 static void do_mkdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1127 struct fuse_mkdir_in *arg = (struct fuse_mkdir_in *) inarg;
1129 if (req->f->conn.proto_minor >= 12)
1130 req->ctx.umask = arg->umask;
1132 if (req->f->op.mkdir)
1133 req->f->op.mkdir(req, nodeid, PARAM(arg), arg->mode);
1134 else
1135 fuse_reply_err(req, ENOSYS);
1138 static void do_unlink(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1140 char *name = (char *) inarg;
1142 if (req->f->op.unlink)
1143 req->f->op.unlink(req, nodeid, name);
1144 else
1145 fuse_reply_err(req, ENOSYS);
1148 static void do_rmdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1150 char *name = (char *) inarg;
1152 if (req->f->op.rmdir)
1153 req->f->op.rmdir(req, nodeid, name);
1154 else
1155 fuse_reply_err(req, ENOSYS);
1158 static void do_symlink(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1160 char *name = (char *) inarg;
1161 char *linkname = ((char *) inarg) + strlen((char *) inarg) + 1;
1163 if (req->f->op.symlink)
1164 req->f->op.symlink(req, linkname, nodeid, name);
1165 else
1166 fuse_reply_err(req, ENOSYS);
1169 static void do_rename(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1171 struct fuse_rename_in *arg = (struct fuse_rename_in *) inarg;
1172 char *oldname = PARAM(arg);
1173 char *newname = oldname + strlen(oldname) + 1;
1175 if (req->f->op.rename)
1176 req->f->op.rename(req, nodeid, oldname, arg->newdir, newname);
1177 else
1178 fuse_reply_err(req, ENOSYS);
1181 static void do_link(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1183 struct fuse_link_in *arg = (struct fuse_link_in *) inarg;
1185 if (req->f->op.link)
1186 req->f->op.link(req, arg->oldnodeid, nodeid, PARAM(arg));
1187 else
1188 fuse_reply_err(req, ENOSYS);
1191 static void do_create(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1193 struct fuse_create_in *arg = (struct fuse_create_in *) inarg;
1195 if (req->f->op.create) {
1196 struct fuse_file_info fi;
1197 char *name = PARAM(arg);
1199 memset(&fi, 0, sizeof(fi));
1200 fi.flags = arg->flags;
1202 if (req->f->conn.proto_minor >= 12)
1203 req->ctx.umask = arg->umask;
1204 else
1205 name = (char *) inarg + sizeof(struct fuse_open_in);
1207 req->f->op.create(req, nodeid, name, arg->mode, &fi);
1208 } else
1209 fuse_reply_err(req, ENOSYS);
1212 static void do_open(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1214 struct fuse_open_in *arg = (struct fuse_open_in *) inarg;
1215 struct fuse_file_info fi;
1217 memset(&fi, 0, sizeof(fi));
1218 fi.flags = arg->flags;
1220 if (req->f->op.open)
1221 req->f->op.open(req, nodeid, &fi);
1222 else
1223 fuse_reply_open(req, &fi);
1226 static void do_read(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1228 struct fuse_read_in *arg = (struct fuse_read_in *) inarg;
1230 if (req->f->op.read) {
1231 struct fuse_file_info fi;
1233 memset(&fi, 0, sizeof(fi));
1234 fi.fh = arg->fh;
1235 fi.fh_old = fi.fh;
1236 if (req->f->conn.proto_minor >= 9) {
1237 fi.lock_owner = arg->lock_owner;
1238 fi.flags = arg->flags;
1240 req->f->op.read(req, nodeid, arg->size, arg->offset, &fi);
1241 } else
1242 fuse_reply_err(req, ENOSYS);
1245 static void do_write(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1247 struct fuse_write_in *arg = (struct fuse_write_in *) inarg;
1248 struct fuse_file_info fi;
1249 char *param;
1251 memset(&fi, 0, sizeof(fi));
1252 fi.fh = arg->fh;
1253 fi.fh_old = fi.fh;
1254 fi.writepage = arg->write_flags & 1;
1256 if (req->f->conn.proto_minor < 9) {
1257 param = ((char *) arg) + FUSE_COMPAT_WRITE_IN_SIZE;
1258 } else {
1259 fi.lock_owner = arg->lock_owner;
1260 fi.flags = arg->flags;
1261 param = PARAM(arg);
1264 if (req->f->op.write)
1265 req->f->op.write(req, nodeid, param, arg->size,
1266 arg->offset, &fi);
1267 else
1268 fuse_reply_err(req, ENOSYS);
1271 static void do_write_buf(fuse_req_t req, fuse_ino_t nodeid, const void *inarg,
1272 const struct fuse_buf *ibuf)
1274 struct fuse_ll *f = req->f;
1275 struct fuse_bufvec bufv = {
1276 .buf[0] = *ibuf,
1277 .count = 1,
1279 struct fuse_write_in *arg = (struct fuse_write_in *) inarg;
1280 struct fuse_file_info fi;
1282 memset(&fi, 0, sizeof(fi));
1283 fi.fh = arg->fh;
1284 fi.fh_old = fi.fh;
1285 fi.writepage = arg->write_flags & 1;
1287 if (req->f->conn.proto_minor < 9) {
1288 bufv.buf[0].mem = ((char *) arg) + FUSE_COMPAT_WRITE_IN_SIZE;
1289 bufv.buf[0].size -= sizeof(struct fuse_in_header) +
1290 FUSE_COMPAT_WRITE_IN_SIZE;
1291 assert(!(bufv.buf[0].flags & FUSE_BUF_IS_FD));
1292 } else {
1293 fi.lock_owner = arg->lock_owner;
1294 fi.flags = arg->flags;
1295 if (!(bufv.buf[0].flags & FUSE_BUF_IS_FD))
1296 bufv.buf[0].mem = PARAM(arg);
1298 bufv.buf[0].size -= sizeof(struct fuse_in_header) +
1299 sizeof(struct fuse_write_in);
1301 if (bufv.buf[0].size < arg->size) {
1302 fprintf(stderr, "fuse: do_write_buf: buffer size too small\n");
1303 fuse_reply_err(req, EIO);
1304 goto out;
1306 bufv.buf[0].size = arg->size;
1308 req->f->op.write_buf(req, nodeid, &bufv, arg->offset, &fi);
1310 out:
1311 /* Need to reset the pipe if ->write_buf() didn't consume all data */
1312 if ((ibuf->flags & FUSE_BUF_IS_FD) && bufv.idx < bufv.count)
1313 fuse_ll_clear_pipe(f);
1316 static void do_flush(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1318 struct fuse_flush_in *arg = (struct fuse_flush_in *) inarg;
1319 struct fuse_file_info fi;
1321 memset(&fi, 0, sizeof(fi));
1322 fi.fh = arg->fh;
1323 fi.fh_old = fi.fh;
1324 fi.flush = 1;
1325 if (req->f->conn.proto_minor >= 7)
1326 fi.lock_owner = arg->lock_owner;
1328 if (req->f->op.flush)
1329 req->f->op.flush(req, nodeid, &fi);
1330 else
1331 fuse_reply_err(req, ENOSYS);
1334 static void do_release(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1336 struct fuse_release_in *arg = (struct fuse_release_in *) inarg;
1337 struct fuse_file_info fi;
1339 memset(&fi, 0, sizeof(fi));
1340 fi.flags = arg->flags;
1341 fi.fh = arg->fh;
1342 fi.fh_old = fi.fh;
1343 if (req->f->conn.proto_minor >= 8) {
1344 fi.flush = (arg->release_flags & FUSE_RELEASE_FLUSH) ? 1 : 0;
1345 fi.lock_owner = arg->lock_owner;
1347 if (arg->release_flags & FUSE_RELEASE_FLOCK_UNLOCK) {
1348 fi.flock_release = 1;
1349 fi.lock_owner = arg->lock_owner;
1352 if (req->f->op.release)
1353 req->f->op.release(req, nodeid, &fi);
1354 else
1355 fuse_reply_err(req, 0);
1358 static void do_fsync(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1360 struct fuse_fsync_in *arg = (struct fuse_fsync_in *) inarg;
1361 struct fuse_file_info fi;
1363 memset(&fi, 0, sizeof(fi));
1364 fi.fh = arg->fh;
1365 fi.fh_old = fi.fh;
1367 if (req->f->op.fsync)
1368 req->f->op.fsync(req, nodeid, arg->fsync_flags & 1, &fi);
1369 else
1370 fuse_reply_err(req, ENOSYS);
1373 static void do_opendir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1375 struct fuse_open_in *arg = (struct fuse_open_in *) inarg;
1376 struct fuse_file_info fi;
1378 memset(&fi, 0, sizeof(fi));
1379 fi.flags = arg->flags;
1381 if (req->f->op.opendir)
1382 req->f->op.opendir(req, nodeid, &fi);
1383 else
1384 fuse_reply_open(req, &fi);
1387 static void do_readdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1389 struct fuse_read_in *arg = (struct fuse_read_in *) inarg;
1390 struct fuse_file_info fi;
1392 memset(&fi, 0, sizeof(fi));
1393 fi.fh = arg->fh;
1394 fi.fh_old = fi.fh;
1396 if (req->f->op.readdir)
1397 req->f->op.readdir(req, nodeid, arg->size, arg->offset, &fi);
1398 else
1399 fuse_reply_err(req, ENOSYS);
1402 static void do_releasedir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1404 struct fuse_release_in *arg = (struct fuse_release_in *) inarg;
1405 struct fuse_file_info fi;
1407 memset(&fi, 0, sizeof(fi));
1408 fi.flags = arg->flags;
1409 fi.fh = arg->fh;
1410 fi.fh_old = fi.fh;
1412 if (req->f->op.releasedir)
1413 req->f->op.releasedir(req, nodeid, &fi);
1414 else
1415 fuse_reply_err(req, 0);
1418 static void do_fsyncdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1420 struct fuse_fsync_in *arg = (struct fuse_fsync_in *) inarg;
1421 struct fuse_file_info fi;
1423 memset(&fi, 0, sizeof(fi));
1424 fi.fh = arg->fh;
1425 fi.fh_old = fi.fh;
1427 if (req->f->op.fsyncdir)
1428 req->f->op.fsyncdir(req, nodeid, arg->fsync_flags & 1, &fi);
1429 else
1430 fuse_reply_err(req, ENOSYS);
1433 static void do_statfs(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1435 (void) nodeid;
1436 (void) inarg;
1438 if (req->f->op.statfs)
1439 req->f->op.statfs(req, nodeid);
1440 else {
1441 struct statvfs buf = {
1442 .f_namemax = 255,
1443 .f_bsize = 512,
1445 fuse_reply_statfs(req, &buf);
1449 static void do_setxattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1451 struct fuse_setxattr_in *arg = (struct fuse_setxattr_in *) inarg;
1452 char *name = PARAM(arg);
1453 char *value = name + strlen(name) + 1;
1455 if (req->f->op.setxattr)
1456 req->f->op.setxattr(req, nodeid, name, value, arg->size,
1457 arg->flags);
1458 else
1459 fuse_reply_err(req, ENOSYS);
1462 static void do_getxattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1464 struct fuse_getxattr_in *arg = (struct fuse_getxattr_in *) inarg;
1466 if (req->f->op.getxattr)
1467 req->f->op.getxattr(req, nodeid, PARAM(arg), arg->size);
1468 else
1469 fuse_reply_err(req, ENOSYS);
1472 static void do_listxattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1474 struct fuse_getxattr_in *arg = (struct fuse_getxattr_in *) inarg;
1476 if (req->f->op.listxattr)
1477 req->f->op.listxattr(req, nodeid, arg->size);
1478 else
1479 fuse_reply_err(req, ENOSYS);
1482 static void do_removexattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1484 char *name = (char *) inarg;
1486 if (req->f->op.removexattr)
1487 req->f->op.removexattr(req, nodeid, name);
1488 else
1489 fuse_reply_err(req, ENOSYS);
1492 static void convert_fuse_file_lock(struct fuse_file_lock *fl,
1493 struct flock *flock)
1495 memset(flock, 0, sizeof(struct flock));
1496 flock->l_type = fl->type;
1497 flock->l_whence = SEEK_SET;
1498 flock->l_start = fl->start;
1499 if (fl->end == OFFSET_MAX)
1500 flock->l_len = 0;
1501 else
1502 flock->l_len = fl->end - fl->start + 1;
1503 flock->l_pid = fl->pid;
1506 static void do_getlk(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1508 struct fuse_lk_in *arg = (struct fuse_lk_in *) inarg;
1509 struct fuse_file_info fi;
1510 struct flock flock;
1512 memset(&fi, 0, sizeof(fi));
1513 fi.fh = arg->fh;
1514 fi.lock_owner = arg->owner;
1516 convert_fuse_file_lock(&arg->lk, &flock);
1517 if (req->f->op.getlk)
1518 req->f->op.getlk(req, nodeid, &fi, &flock);
1519 else
1520 fuse_reply_err(req, ENOSYS);
1523 static void do_setlk_common(fuse_req_t req, fuse_ino_t nodeid,
1524 const void *inarg, int sleep)
1526 struct fuse_lk_in *arg = (struct fuse_lk_in *) inarg;
1527 struct fuse_file_info fi;
1528 struct flock flock;
1530 memset(&fi, 0, sizeof(fi));
1531 fi.fh = arg->fh;
1532 fi.lock_owner = arg->owner;
1534 if (arg->lk_flags & FUSE_LK_FLOCK) {
1535 int op = 0;
1537 switch (arg->lk.type) {
1538 case F_RDLCK:
1539 op = LOCK_SH;
1540 break;
1541 case F_WRLCK:
1542 op = LOCK_EX;
1543 break;
1544 case F_UNLCK:
1545 op = LOCK_UN;
1546 break;
1548 if (!sleep)
1549 op |= LOCK_NB;
1551 if (req->f->op.flock)
1552 req->f->op.flock(req, nodeid, &fi, op);
1553 else
1554 fuse_reply_err(req, ENOSYS);
1555 } else {
1556 convert_fuse_file_lock(&arg->lk, &flock);
1557 if (req->f->op.setlk)
1558 req->f->op.setlk(req, nodeid, &fi, &flock, sleep);
1559 else
1560 fuse_reply_err(req, ENOSYS);
1564 static void do_setlk(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1566 do_setlk_common(req, nodeid, inarg, 0);
1569 static void do_setlkw(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1571 do_setlk_common(req, nodeid, inarg, 1);
1574 static int find_interrupted(struct fuse_ll *f, struct fuse_req *req)
1576 struct fuse_req *curr;
1578 for (curr = f->list.next; curr != &f->list; curr = curr->next) {
1579 if (curr->unique == req->u.i.unique) {
1580 fuse_interrupt_func_t func;
1581 void *data;
1583 curr->ctr++;
1584 pthread_mutex_unlock(&f->lock);
1586 /* Ugh, ugly locking */
1587 pthread_mutex_lock(&curr->lock);
1588 pthread_mutex_lock(&f->lock);
1589 curr->interrupted = 1;
1590 func = curr->u.ni.func;
1591 data = curr->u.ni.data;
1592 pthread_mutex_unlock(&f->lock);
1593 if (func)
1594 func(curr, data);
1595 pthread_mutex_unlock(&curr->lock);
1597 pthread_mutex_lock(&f->lock);
1598 curr->ctr--;
1599 if (!curr->ctr)
1600 destroy_req(curr);
1602 return 1;
1605 for (curr = f->interrupts.next; curr != &f->interrupts;
1606 curr = curr->next) {
1607 if (curr->u.i.unique == req->u.i.unique)
1608 return 1;
1610 return 0;
1613 static void do_interrupt(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1615 struct fuse_interrupt_in *arg = (struct fuse_interrupt_in *) inarg;
1616 struct fuse_ll *f = req->f;
1618 (void) nodeid;
1619 if (f->debug)
1620 fprintf(stderr, "INTERRUPT: %llu\n",
1621 (unsigned long long) arg->unique);
1623 req->u.i.unique = arg->unique;
1625 pthread_mutex_lock(&f->lock);
1626 if (find_interrupted(f, req))
1627 destroy_req(req);
1628 else
1629 list_add_req(req, &f->interrupts);
1630 pthread_mutex_unlock(&f->lock);
1633 static struct fuse_req *check_interrupt(struct fuse_ll *f, struct fuse_req *req)
1635 struct fuse_req *curr;
1637 for (curr = f->interrupts.next; curr != &f->interrupts;
1638 curr = curr->next) {
1639 if (curr->u.i.unique == req->unique) {
1640 req->interrupted = 1;
1641 list_del_req(curr);
1642 free(curr);
1643 return NULL;
1646 curr = f->interrupts.next;
1647 if (curr != &f->interrupts) {
1648 list_del_req(curr);
1649 list_init_req(curr);
1650 return curr;
1651 } else
1652 return NULL;
1655 static void do_bmap(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1657 struct fuse_bmap_in *arg = (struct fuse_bmap_in *) inarg;
1659 if (req->f->op.bmap)
1660 req->f->op.bmap(req, nodeid, arg->blocksize, arg->block);
1661 else
1662 fuse_reply_err(req, ENOSYS);
1665 static void do_ioctl(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1667 struct fuse_ioctl_in *arg = (struct fuse_ioctl_in *) inarg;
1668 unsigned int flags = arg->flags;
1669 void *in_buf = arg->in_size ? PARAM(arg) : NULL;
1670 struct fuse_file_info fi;
1672 if (flags & FUSE_IOCTL_DIR &&
1673 !(req->f->conn.want & FUSE_CAP_IOCTL_DIR)) {
1674 fuse_reply_err(req, ENOTTY);
1675 return;
1678 memset(&fi, 0, sizeof(fi));
1679 fi.fh = arg->fh;
1680 fi.fh_old = fi.fh;
1682 if (sizeof(void *) == 4 && req->f->conn.proto_minor >= 16 &&
1683 !(flags & FUSE_IOCTL_32BIT)) {
1684 req->ioctl_64bit = 1;
1687 if (req->f->op.ioctl)
1688 req->f->op.ioctl(req, nodeid, arg->cmd,
1689 (void *)(uintptr_t)arg->arg, &fi, flags,
1690 in_buf, arg->in_size, arg->out_size);
1691 else
1692 fuse_reply_err(req, ENOSYS);
1695 void fuse_pollhandle_destroy(struct fuse_pollhandle *ph)
1697 free(ph);
1700 static void do_poll(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1702 struct fuse_poll_in *arg = (struct fuse_poll_in *) inarg;
1703 struct fuse_file_info fi;
1705 memset(&fi, 0, sizeof(fi));
1706 fi.fh = arg->fh;
1707 fi.fh_old = fi.fh;
1709 if (req->f->op.poll) {
1710 struct fuse_pollhandle *ph = NULL;
1712 if (arg->flags & FUSE_POLL_SCHEDULE_NOTIFY) {
1713 ph = malloc(sizeof(struct fuse_pollhandle));
1714 if (ph == NULL) {
1715 fuse_reply_err(req, ENOMEM);
1716 return;
1718 ph->kh = arg->kh;
1719 ph->ch = req->ch;
1720 ph->f = req->f;
1723 req->f->op.poll(req, nodeid, &fi, ph);
1724 } else {
1725 fuse_reply_err(req, ENOSYS);
1729 static void do_mmap(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1731 struct fuse_mmap_in *arg = (struct fuse_mmap_in *) inarg;
1732 struct fuse_file_info fi;
1734 memset(&fi, 0, sizeof(fi));
1735 fi.fh = arg->fh;
1736 fi.fh_old = fi.fh;
1738 if (req->f->op.mmap)
1739 req->f->op.mmap(req, nodeid, arg->addr, arg->len, arg->prot,
1740 arg->flags, arg->offset, &fi);
1741 else
1742 fuse_reply_err(req, ENOSYS);
1746 static void do_munmap(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1748 struct fuse_munmap_in *arg = (struct fuse_munmap_in *) inarg;
1749 struct fuse_file_info fi;
1751 memset(&fi, 0, sizeof(fi));
1752 fi.fh = arg->fh;
1753 fi.fh_old = fi.fh;
1755 if (req->f->op.munmap)
1756 req->f->op.munmap(req, nodeid, arg->mapid, arg->size, &fi);
1757 else
1758 fuse_reply_err(req, ENOSYS);
1761 static void do_init(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1763 struct fuse_init_in *arg = (struct fuse_init_in *) inarg;
1764 struct fuse_init_out outarg;
1765 struct fuse_ll *f = req->f;
1766 size_t bufsize = fuse_chan_bufsize(req->ch);
1768 (void) nodeid;
1769 if (f->debug) {
1770 fprintf(stderr, "INIT: %u.%u\n", arg->major, arg->minor);
1771 if (arg->major == 7 && arg->minor >= 6) {
1772 fprintf(stderr, "flags=0x%08x\n", arg->flags);
1773 fprintf(stderr, "max_readahead=0x%08x\n",
1774 arg->max_readahead);
1777 f->conn.proto_major = arg->major;
1778 f->conn.proto_minor = arg->minor;
1779 f->conn.capable = 0;
1780 f->conn.want = 0;
1782 memset(&outarg, 0, sizeof(outarg));
1783 outarg.major = FUSE_KERNEL_VERSION;
1784 outarg.minor = FUSE_KERNEL_MINOR_VERSION;
1786 if (arg->major < 7) {
1787 fprintf(stderr, "fuse: unsupported protocol version: %u.%u\n",
1788 arg->major, arg->minor);
1789 fuse_reply_err(req, EPROTO);
1790 return;
1793 if (arg->major > 7) {
1794 /* Wait for a second INIT request with a 7.X version */
1795 send_reply_ok(req, &outarg, sizeof(outarg));
1796 return;
1799 if (arg->minor >= 6) {
1800 if (f->conn.async_read)
1801 f->conn.async_read = arg->flags & FUSE_ASYNC_READ;
1802 if (arg->max_readahead < f->conn.max_readahead)
1803 f->conn.max_readahead = arg->max_readahead;
1804 if (arg->flags & FUSE_ASYNC_READ)
1805 f->conn.capable |= FUSE_CAP_ASYNC_READ;
1806 if (arg->flags & FUSE_POSIX_LOCKS)
1807 f->conn.capable |= FUSE_CAP_POSIX_LOCKS;
1808 if (arg->flags & FUSE_ATOMIC_O_TRUNC)
1809 f->conn.capable |= FUSE_CAP_ATOMIC_O_TRUNC;
1810 if (arg->flags & FUSE_EXPORT_SUPPORT)
1811 f->conn.capable |= FUSE_CAP_EXPORT_SUPPORT;
1812 if (arg->flags & FUSE_BIG_WRITES)
1813 f->conn.capable |= FUSE_CAP_BIG_WRITES;
1814 if (arg->flags & FUSE_DONT_MASK)
1815 f->conn.capable |= FUSE_CAP_DONT_MASK;
1816 if (arg->flags & FUSE_FLOCK_LOCKS)
1817 f->conn.capable |= FUSE_CAP_FLOCK_LOCKS;
1818 } else {
1819 f->conn.async_read = 0;
1820 f->conn.max_readahead = 0;
1823 if (req->f->conn.proto_minor >= 14) {
1824 #ifdef HAVE_SPLICE
1825 #ifdef HAVE_VMSPLICE
1826 f->conn.capable |= FUSE_CAP_SPLICE_WRITE | FUSE_CAP_SPLICE_MOVE;
1827 if (f->splice_write)
1828 f->conn.want |= FUSE_CAP_SPLICE_WRITE;
1829 if (f->splice_move)
1830 f->conn.want |= FUSE_CAP_SPLICE_MOVE;
1831 #endif
1832 f->conn.capable |= FUSE_CAP_SPLICE_READ;
1833 if (f->splice_read)
1834 f->conn.want |= FUSE_CAP_SPLICE_READ;
1835 #endif
1837 if (req->f->conn.proto_minor >= 18)
1838 f->conn.capable |= FUSE_CAP_IOCTL_DIR;
1840 if (f->atomic_o_trunc)
1841 f->conn.want |= FUSE_CAP_ATOMIC_O_TRUNC;
1842 if (f->op.getlk && f->op.setlk && !f->no_remote_posix_lock)
1843 f->conn.want |= FUSE_CAP_POSIX_LOCKS;
1844 if (f->op.flock && !f->no_remote_flock)
1845 f->conn.want |= FUSE_CAP_FLOCK_LOCKS;
1846 if (f->big_writes)
1847 f->conn.want |= FUSE_CAP_BIG_WRITES;
1849 if (bufsize < FUSE_MIN_READ_BUFFER) {
1850 fprintf(stderr, "fuse: warning: buffer size too small: %zu\n",
1851 bufsize);
1852 bufsize = FUSE_MIN_READ_BUFFER;
1855 bufsize -= 4096;
1856 if (bufsize < f->conn.max_write)
1857 f->conn.max_write = bufsize;
1859 f->got_init = 1;
1860 if (f->op.init)
1861 f->op.init(f->userdata, &f->conn);
1863 if (f->no_splice_read)
1864 f->conn.want &= ~FUSE_CAP_SPLICE_READ;
1865 if (f->no_splice_write)
1866 f->conn.want &= ~FUSE_CAP_SPLICE_WRITE;
1867 if (f->no_splice_move)
1868 f->conn.want &= ~FUSE_CAP_SPLICE_MOVE;
1870 if (f->conn.async_read || (f->conn.want & FUSE_CAP_ASYNC_READ))
1871 outarg.flags |= FUSE_ASYNC_READ;
1872 if (f->conn.want & FUSE_CAP_POSIX_LOCKS)
1873 outarg.flags |= FUSE_POSIX_LOCKS;
1874 if (f->conn.want & FUSE_CAP_ATOMIC_O_TRUNC)
1875 outarg.flags |= FUSE_ATOMIC_O_TRUNC;
1876 if (f->conn.want & FUSE_CAP_EXPORT_SUPPORT)
1877 outarg.flags |= FUSE_EXPORT_SUPPORT;
1878 if (f->conn.want & FUSE_CAP_BIG_WRITES)
1879 outarg.flags |= FUSE_BIG_WRITES;
1880 if (f->conn.want & FUSE_CAP_DONT_MASK)
1881 outarg.flags |= FUSE_DONT_MASK;
1882 if (f->conn.want & FUSE_CAP_FLOCK_LOCKS)
1883 outarg.flags |= FUSE_FLOCK_LOCKS;
1884 outarg.max_readahead = f->conn.max_readahead;
1885 outarg.max_write = f->conn.max_write;
1886 if (f->conn.proto_minor >= 13) {
1887 if (f->conn.max_background >= (1 << 16))
1888 f->conn.max_background = (1 << 16) - 1;
1889 if (f->conn.congestion_threshold > f->conn.max_background)
1890 f->conn.congestion_threshold = f->conn.max_background;
1891 if (!f->conn.congestion_threshold) {
1892 f->conn.congestion_threshold =
1893 f->conn.max_background * 3 / 4;
1896 outarg.max_background = f->conn.max_background;
1897 outarg.congestion_threshold = f->conn.congestion_threshold;
1900 if (f->debug) {
1901 fprintf(stderr, " INIT: %u.%u\n", outarg.major, outarg.minor);
1902 fprintf(stderr, " flags=0x%08x\n", outarg.flags);
1903 fprintf(stderr, " max_readahead=0x%08x\n",
1904 outarg.max_readahead);
1905 fprintf(stderr, " max_write=0x%08x\n", outarg.max_write);
1906 fprintf(stderr, " max_background=%i\n",
1907 outarg.max_background);
1908 fprintf(stderr, " congestion_threshold=%i\n",
1909 outarg.congestion_threshold);
1912 send_reply_ok(req, &outarg, arg->minor < 5 ? 8 : sizeof(outarg));
1915 static void do_destroy(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1917 struct fuse_ll *f = req->f;
1919 (void) nodeid;
1920 (void) inarg;
1922 f->got_destroy = 1;
1923 if (f->op.destroy)
1924 f->op.destroy(f->userdata);
1926 send_reply_ok(req, NULL, 0);
1929 static void list_del_nreq(struct fuse_notify_req *nreq)
1931 struct fuse_notify_req *prev = nreq->prev;
1932 struct fuse_notify_req *next = nreq->next;
1933 prev->next = next;
1934 next->prev = prev;
1937 static void list_add_nreq(struct fuse_notify_req *nreq,
1938 struct fuse_notify_req *next)
1940 struct fuse_notify_req *prev = next->prev;
1941 nreq->next = next;
1942 nreq->prev = prev;
1943 prev->next = nreq;
1944 next->prev = nreq;
1947 static void list_init_nreq(struct fuse_notify_req *nreq)
1949 nreq->next = nreq;
1950 nreq->prev = nreq;
1953 static void do_notify_reply(fuse_req_t req, fuse_ino_t nodeid,
1954 const void *inarg, const struct fuse_buf *buf)
1956 struct fuse_ll *f = req->f;
1957 struct fuse_notify_req *nreq;
1958 struct fuse_notify_req *head;
1960 pthread_mutex_lock(&f->lock);
1961 head = &f->notify_list;
1962 for (nreq = head->next; nreq != head; nreq = nreq->next) {
1963 if (nreq->unique == req->unique) {
1964 list_del_nreq(nreq);
1965 break;
1968 pthread_mutex_unlock(&f->lock);
1970 if (nreq != head)
1971 nreq->reply(nreq, req, nodeid, inarg, buf);
1974 static int send_notify_iov(struct fuse_ll *f, struct fuse_chan *ch,
1975 int notify_code, struct iovec *iov, int count)
1977 struct fuse_out_header out;
1979 if (!f->got_init)
1980 return -ENOTCONN;
1982 out.unique = 0;
1983 out.error = notify_code;
1984 iov[0].iov_base = &out;
1985 iov[0].iov_len = sizeof(struct fuse_out_header);
1987 return fuse_send_msg(f, ch, iov, count);
1990 int fuse_lowlevel_notify_poll(struct fuse_pollhandle *ph)
1992 if (ph != NULL) {
1993 struct fuse_notify_poll_wakeup_out outarg;
1994 struct iovec iov[2];
1996 outarg.kh = ph->kh;
1998 iov[1].iov_base = &outarg;
1999 iov[1].iov_len = sizeof(outarg);
2001 return send_notify_iov(ph->f, ph->ch, FUSE_NOTIFY_POLL, iov, 2);
2002 } else {
2003 return 0;
2007 int fuse_lowlevel_notify_inval_inode(struct fuse_chan *ch, fuse_ino_t ino,
2008 off_t off, off_t len)
2010 struct fuse_notify_inval_inode_out outarg;
2011 struct fuse_ll *f;
2012 struct iovec iov[2];
2014 if (!ch)
2015 return -EINVAL;
2017 f = (struct fuse_ll *)fuse_session_data(fuse_chan_session(ch));
2018 if (!f)
2019 return -ENODEV;
2021 outarg.ino = ino;
2022 outarg.off = off;
2023 outarg.len = len;
2025 iov[1].iov_base = &outarg;
2026 iov[1].iov_len = sizeof(outarg);
2028 return send_notify_iov(f, ch, FUSE_NOTIFY_INVAL_INODE, iov, 2);
2031 int fuse_lowlevel_notify_inval_entry(struct fuse_chan *ch, fuse_ino_t parent,
2032 const char *name, size_t namelen)
2034 struct fuse_notify_inval_entry_out outarg;
2035 struct fuse_ll *f;
2036 struct iovec iov[3];
2038 if (!ch)
2039 return -EINVAL;
2041 f = (struct fuse_ll *)fuse_session_data(fuse_chan_session(ch));
2042 if (!f)
2043 return -ENODEV;
2045 outarg.parent = parent;
2046 outarg.namelen = namelen;
2047 outarg.padding = 0;
2049 iov[1].iov_base = &outarg;
2050 iov[1].iov_len = sizeof(outarg);
2051 iov[2].iov_base = (void *)name;
2052 iov[2].iov_len = namelen + 1;
2054 return send_notify_iov(f, ch, FUSE_NOTIFY_INVAL_ENTRY, iov, 3);
2057 int fuse_lowlevel_notify_delete(struct fuse_chan *ch,
2058 fuse_ino_t parent, fuse_ino_t child,
2059 const char *name, size_t namelen)
2061 struct fuse_notify_delete_out outarg;
2062 struct fuse_ll *f;
2063 struct iovec iov[3];
2065 if (!ch)
2066 return -EINVAL;
2068 f = (struct fuse_ll *)fuse_session_data(fuse_chan_session(ch));
2069 if (!f)
2070 return -ENODEV;
2072 outarg.parent = parent;
2073 outarg.child = child;
2074 outarg.namelen = namelen;
2075 outarg.padding = 0;
2077 iov[1].iov_base = &outarg;
2078 iov[1].iov_len = sizeof(outarg);
2079 iov[2].iov_base = (void *)name;
2080 iov[2].iov_len = namelen + 1;
2082 return send_notify_iov(f, ch, FUSE_NOTIFY_DELETE, iov, 3);
2085 int fuse_lowlevel_notify_store(struct fuse_chan *ch, fuse_ino_t ino,
2086 off_t offset, struct fuse_bufvec *bufv,
2087 enum fuse_buf_copy_flags flags)
2089 struct fuse_out_header out;
2090 struct fuse_notify_store_out outarg;
2091 struct fuse_ll *f;
2092 struct iovec iov[3];
2093 size_t size = fuse_buf_size(bufv);
2094 int res;
2096 if (!ch)
2097 return -EINVAL;
2099 f = (struct fuse_ll *)fuse_session_data(fuse_chan_session(ch));
2100 if (!f)
2101 return -ENODEV;
2103 out.unique = 0;
2104 out.error = FUSE_NOTIFY_STORE;
2106 outarg.nodeid = ino;
2107 outarg.offset = offset;
2108 outarg.size = size;
2110 iov[0].iov_base = &out;
2111 iov[0].iov_len = sizeof(out);
2112 iov[1].iov_base = &outarg;
2113 iov[1].iov_len = sizeof(outarg);
2115 res = fuse_send_data_iov(f, ch, iov, 2, bufv, flags);
2116 if (res > 0)
2117 res = -res;
2119 return res;
2122 struct fuse_retrieve_req {
2123 struct fuse_notify_req nreq;
2124 void *cookie;
2127 static void fuse_ll_retrieve_reply(struct fuse_notify_req *nreq,
2128 fuse_req_t req, fuse_ino_t ino,
2129 const void *inarg,
2130 const struct fuse_buf *ibuf)
2132 struct fuse_ll *f = req->f;
2133 struct fuse_retrieve_req *rreq =
2134 container_of(nreq, struct fuse_retrieve_req, nreq);
2135 const struct fuse_notify_retrieve_in *arg = inarg;
2136 struct fuse_bufvec bufv = {
2137 .buf[0] = *ibuf,
2138 .count = 1,
2141 if (!(bufv.buf[0].flags & FUSE_BUF_IS_FD))
2142 bufv.buf[0].mem = PARAM(arg);
2144 bufv.buf[0].size -= sizeof(struct fuse_in_header) +
2145 sizeof(struct fuse_notify_retrieve_in);
2147 if (bufv.buf[0].size < arg->size) {
2148 fprintf(stderr, "fuse: retrieve reply: buffer size too small\n");
2149 fuse_reply_none(req);
2150 goto out;
2152 bufv.buf[0].size = arg->size;
2154 if (req->f->op.retrieve_reply) {
2155 req->f->op.retrieve_reply(req, rreq->cookie, ino,
2156 arg->offset, &bufv);
2157 } else {
2158 fuse_reply_none(req);
2160 out:
2161 free(rreq);
2162 if ((ibuf->flags & FUSE_BUF_IS_FD) && bufv.idx < bufv.count)
2163 fuse_ll_clear_pipe(f);
2166 int fuse_lowlevel_notify_retrieve(struct fuse_chan *ch, fuse_ino_t ino,
2167 size_t size, off_t offset, void *cookie)
2169 struct fuse_notify_retrieve_out outarg;
2170 struct fuse_ll *f;
2171 struct iovec iov[2];
2172 struct fuse_retrieve_req *rreq;
2173 int err;
2175 if (!ch)
2176 return -EINVAL;
2178 f = (struct fuse_ll *)fuse_session_data(fuse_chan_session(ch));
2179 if (!f)
2180 return -ENODEV;
2182 rreq = malloc(sizeof(*rreq));
2183 if (rreq == NULL)
2184 return -ENOMEM;
2186 pthread_mutex_lock(&f->lock);
2187 rreq->cookie = cookie;
2188 rreq->nreq.unique = f->notify_ctr++;
2189 rreq->nreq.reply = fuse_ll_retrieve_reply;
2190 list_add_nreq(&rreq->nreq, &f->notify_list);
2191 pthread_mutex_unlock(&f->lock);
2193 outarg.notify_unique = rreq->nreq.unique;
2194 outarg.nodeid = ino;
2195 outarg.offset = offset;
2196 outarg.size = size;
2198 iov[1].iov_base = &outarg;
2199 iov[1].iov_len = sizeof(outarg);
2201 err = send_notify_iov(f, ch, FUSE_NOTIFY_RETRIEVE, iov, 2);
2202 if (err) {
2203 pthread_mutex_lock(&f->lock);
2204 list_del_nreq(&rreq->nreq);
2205 pthread_mutex_unlock(&f->lock);
2206 free(rreq);
2209 return err;
2212 void *fuse_req_userdata(fuse_req_t req)
2214 return req->f->userdata;
2217 const struct fuse_ctx *fuse_req_ctx(fuse_req_t req)
2219 return &req->ctx;
2223 * The size of fuse_ctx got extended, so need to be careful about
2224 * incompatibility (i.e. a new binary cannot work with an old
2225 * library).
2227 const struct fuse_ctx *fuse_req_ctx_compat24(fuse_req_t req);
2228 const struct fuse_ctx *fuse_req_ctx_compat24(fuse_req_t req)
2230 return fuse_req_ctx(req);
2232 #ifndef __NetBSD__
2233 FUSE_SYMVER(".symver fuse_req_ctx_compat24,fuse_req_ctx@FUSE_2.4");
2234 #endif
2237 void fuse_req_interrupt_func(fuse_req_t req, fuse_interrupt_func_t func,
2238 void *data)
2240 pthread_mutex_lock(&req->lock);
2241 pthread_mutex_lock(&req->f->lock);
2242 req->u.ni.func = func;
2243 req->u.ni.data = data;
2244 pthread_mutex_unlock(&req->f->lock);
2245 if (req->interrupted && func)
2246 func(req, data);
2247 pthread_mutex_unlock(&req->lock);
2250 int fuse_req_interrupted(fuse_req_t req)
2252 int interrupted;
2254 pthread_mutex_lock(&req->f->lock);
2255 interrupted = req->interrupted;
2256 pthread_mutex_unlock(&req->f->lock);
2258 return interrupted;
2261 static struct {
2262 void (*func)(fuse_req_t, fuse_ino_t, const void *);
2263 const char *name;
2264 } fuse_ll_ops[] = {
2265 [FUSE_LOOKUP] = { do_lookup, "LOOKUP" },
2266 [FUSE_FORGET] = { do_forget, "FORGET" },
2267 [FUSE_GETATTR] = { do_getattr, "GETATTR" },
2268 [FUSE_SETATTR] = { do_setattr, "SETATTR" },
2269 [FUSE_READLINK] = { do_readlink, "READLINK" },
2270 [FUSE_SYMLINK] = { do_symlink, "SYMLINK" },
2271 [FUSE_MKNOD] = { do_mknod, "MKNOD" },
2272 [FUSE_MKDIR] = { do_mkdir, "MKDIR" },
2273 [FUSE_UNLINK] = { do_unlink, "UNLINK" },
2274 [FUSE_RMDIR] = { do_rmdir, "RMDIR" },
2275 [FUSE_RENAME] = { do_rename, "RENAME" },
2276 [FUSE_LINK] = { do_link, "LINK" },
2277 [FUSE_OPEN] = { do_open, "OPEN" },
2278 [FUSE_READ] = { do_read, "READ" },
2279 [FUSE_WRITE] = { do_write, "WRITE" },
2280 [FUSE_STATFS] = { do_statfs, "STATFS" },
2281 [FUSE_RELEASE] = { do_release, "RELEASE" },
2282 [FUSE_FSYNC] = { do_fsync, "FSYNC" },
2283 [FUSE_SETXATTR] = { do_setxattr, "SETXATTR" },
2284 [FUSE_GETXATTR] = { do_getxattr, "GETXATTR" },
2285 [FUSE_LISTXATTR] = { do_listxattr, "LISTXATTR" },
2286 [FUSE_REMOVEXATTR] = { do_removexattr, "REMOVEXATTR" },
2287 [FUSE_FLUSH] = { do_flush, "FLUSH" },
2288 [FUSE_INIT] = { do_init, "INIT" },
2289 [FUSE_OPENDIR] = { do_opendir, "OPENDIR" },
2290 [FUSE_READDIR] = { do_readdir, "READDIR" },
2291 [FUSE_RELEASEDIR] = { do_releasedir, "RELEASEDIR" },
2292 [FUSE_FSYNCDIR] = { do_fsyncdir, "FSYNCDIR" },
2293 [FUSE_GETLK] = { do_getlk, "GETLK" },
2294 [FUSE_SETLK] = { do_setlk, "SETLK" },
2295 [FUSE_SETLKW] = { do_setlkw, "SETLKW" },
2296 [FUSE_ACCESS] = { do_access, "ACCESS" },
2297 [FUSE_CREATE] = { do_create, "CREATE" },
2298 [FUSE_INTERRUPT] = { do_interrupt, "INTERRUPT" },
2299 [FUSE_BMAP] = { do_bmap, "BMAP" },
2300 [FUSE_IOCTL] = { do_ioctl, "IOCTL" },
2301 [FUSE_POLL] = { do_poll, "POLL" },
2302 [FUSE_DESTROY] = { do_destroy, "DESTROY" },
2303 [FUSE_NOTIFY_REPLY] = { (void *) 1, "NOTIFY_REPLY" },
2304 [FUSE_BATCH_FORGET] = { do_batch_forget, "BATCH_FORGET" },
2305 [FUSE_MMAP] = { do_mmap, "MMAP" },
2306 [FUSE_MUNMAP] = { do_munmap, "MUNMAP" },
2307 [CUSE_INIT] = { cuse_lowlevel_init, "CUSE_INIT" },
2310 #define FUSE_MAXOP (sizeof(fuse_ll_ops) / sizeof(fuse_ll_ops[0]))
2312 static const char *opname(enum fuse_opcode opcode)
2314 if (opcode >= FUSE_MAXOP || !fuse_ll_ops[opcode].name)
2315 return "???";
2316 else
2317 return fuse_ll_ops[opcode].name;
2320 static int fuse_ll_copy_from_pipe(struct fuse_bufvec *dst,
2321 struct fuse_bufvec *src)
2323 int res = fuse_buf_copy(dst, src, 0);
2324 if (res < 0) {
2325 fprintf(stderr, "fuse: copy from pipe: %s\n", strerror(-res));
2326 return res;
2328 if (res < fuse_buf_size(dst)) {
2329 fprintf(stderr, "fuse: copy from pipe: short read\n");
2330 return -1;
2332 return 0;
2335 static void fuse_ll_process_buf(void *data, const struct fuse_buf *buf,
2336 struct fuse_chan *ch)
2338 struct fuse_ll *f = (struct fuse_ll *) data;
2339 const size_t write_header_size = sizeof(struct fuse_in_header) +
2340 sizeof(struct fuse_write_in);
2341 struct fuse_bufvec bufv = { .buf[0] = *buf, .count = 1 };
2342 struct fuse_bufvec tmpbuf = FUSE_BUFVEC_INIT(write_header_size);
2343 struct fuse_in_header *in;
2344 const void *inarg;
2345 struct fuse_req *req;
2346 void *mbuf = NULL;
2347 int err;
2348 int res;
2350 if (buf->flags & FUSE_BUF_IS_FD) {
2351 if (buf->size < tmpbuf.buf[0].size)
2352 tmpbuf.buf[0].size = buf->size;
2354 mbuf = malloc(tmpbuf.buf[0].size);
2355 if (mbuf == NULL) {
2356 fprintf(stderr, "fuse: failed to allocate header\n");
2357 goto clear_pipe;
2359 tmpbuf.buf[0].mem = mbuf;
2361 res = fuse_ll_copy_from_pipe(&tmpbuf, &bufv);
2362 if (res < 0)
2363 goto clear_pipe;
2365 in = mbuf;
2366 } else {
2367 in = buf->mem;
2370 if (f->debug) {
2371 fprintf(stderr,
2372 "unique: %llu, opcode: %s (%i), nodeid: %lu, insize: %zu, pid: %u\n",
2373 (unsigned long long) in->unique,
2374 opname((enum fuse_opcode) in->opcode), in->opcode,
2375 (unsigned long) in->nodeid, buf->size, in->pid);
2378 req = fuse_ll_alloc_req(f);
2379 if (req == NULL) {
2380 struct fuse_out_header out = {
2381 .unique = in->unique,
2382 .error = -ENOMEM,
2384 struct iovec iov = {
2385 .iov_base = &out,
2386 .iov_len = sizeof(struct fuse_out_header),
2389 fuse_send_msg(f, ch, &iov, 1);
2390 goto clear_pipe;
2393 req->unique = in->unique;
2394 req->ctx.uid = in->uid;
2395 req->ctx.gid = in->gid;
2396 req->ctx.pid = in->pid;
2397 req->ch = ch;
2399 err = EIO;
2400 if (!f->got_init) {
2401 enum fuse_opcode expected;
2403 expected = f->cuse_data ? CUSE_INIT : FUSE_INIT;
2404 if (in->opcode != expected)
2405 goto reply_err;
2406 } else if (in->opcode == FUSE_INIT || in->opcode == CUSE_INIT)
2407 goto reply_err;
2409 err = EACCES;
2410 if (f->allow_root && in->uid != f->owner && in->uid != 0 &&
2411 in->opcode != FUSE_INIT && in->opcode != FUSE_READ &&
2412 in->opcode != FUSE_WRITE && in->opcode != FUSE_FSYNC &&
2413 in->opcode != FUSE_RELEASE && in->opcode != FUSE_READDIR &&
2414 in->opcode != FUSE_FSYNCDIR && in->opcode != FUSE_RELEASEDIR &&
2415 in->opcode != FUSE_NOTIFY_REPLY)
2416 goto reply_err;
2418 err = ENOSYS;
2419 if (in->opcode >= FUSE_MAXOP || !fuse_ll_ops[in->opcode].func)
2420 goto reply_err;
2421 if (in->opcode != FUSE_INTERRUPT) {
2422 struct fuse_req *intr;
2423 pthread_mutex_lock(&f->lock);
2424 intr = check_interrupt(f, req);
2425 list_add_req(req, &f->list);
2426 pthread_mutex_unlock(&f->lock);
2427 if (intr)
2428 fuse_reply_err(intr, EAGAIN);
2431 if ((buf->flags & FUSE_BUF_IS_FD) && write_header_size < buf->size &&
2432 (in->opcode != FUSE_WRITE || !f->op.write_buf) &&
2433 in->opcode != FUSE_NOTIFY_REPLY) {
2434 void *newmbuf;
2436 err = ENOMEM;
2437 newmbuf = realloc(mbuf, buf->size);
2438 if (newmbuf == NULL)
2439 goto reply_err;
2440 mbuf = newmbuf;
2442 tmpbuf = FUSE_BUFVEC_INIT(buf->size - write_header_size);
2443 tmpbuf.buf[0].mem = mbuf + write_header_size;
2445 res = fuse_ll_copy_from_pipe(&tmpbuf, &bufv);
2446 err = -res;
2447 if (res < 0)
2448 goto reply_err;
2450 in = mbuf;
2453 inarg = (void *) &in[1];
2454 if (in->opcode == FUSE_WRITE && f->op.write_buf)
2455 do_write_buf(req, in->nodeid, inarg, buf);
2456 else if (in->opcode == FUSE_NOTIFY_REPLY)
2457 do_notify_reply(req, in->nodeid, inarg, buf);
2458 else
2459 fuse_ll_ops[in->opcode].func(req, in->nodeid, inarg);
2461 out_free:
2462 free(mbuf);
2463 return;
2465 reply_err:
2466 fuse_reply_err(req, err);
2467 clear_pipe:
2468 if (buf->flags & FUSE_BUF_IS_FD)
2469 fuse_ll_clear_pipe(f);
2470 goto out_free;
2473 static void fuse_ll_process(void *data, const char *buf, size_t len,
2474 struct fuse_chan *ch)
2476 struct fuse_buf fbuf = {
2477 .mem = (void *) buf,
2478 .size = len,
2481 fuse_ll_process_buf(data, &fbuf, ch);
2484 enum {
2485 KEY_HELP,
2486 KEY_VERSION,
2489 static struct fuse_opt fuse_ll_opts[] = {
2490 { "debug", offsetof(struct fuse_ll, debug), 1 },
2491 { "-d", offsetof(struct fuse_ll, debug), 1 },
2492 { "allow_root", offsetof(struct fuse_ll, allow_root), 1 },
2493 { "max_write=%u", offsetof(struct fuse_ll, conn.max_write), 0 },
2494 { "max_readahead=%u", offsetof(struct fuse_ll, conn.max_readahead), 0 },
2495 { "max_background=%u", offsetof(struct fuse_ll, conn.max_background), 0 },
2496 { "congestion_threshold=%u",
2497 offsetof(struct fuse_ll, conn.congestion_threshold), 0 },
2498 { "async_read", offsetof(struct fuse_ll, conn.async_read), 1 },
2499 { "sync_read", offsetof(struct fuse_ll, conn.async_read), 0 },
2500 { "atomic_o_trunc", offsetof(struct fuse_ll, atomic_o_trunc), 1},
2501 { "no_remote_lock", offsetof(struct fuse_ll, no_remote_posix_lock), 1},
2502 { "no_remote_lock", offsetof(struct fuse_ll, no_remote_flock), 1},
2503 { "no_remote_flock", offsetof(struct fuse_ll, no_remote_flock), 1},
2504 { "no_remote_posix_lock", offsetof(struct fuse_ll, no_remote_posix_lock), 1},
2505 { "big_writes", offsetof(struct fuse_ll, big_writes), 1},
2506 { "splice_write", offsetof(struct fuse_ll, splice_write), 1},
2507 { "no_splice_write", offsetof(struct fuse_ll, no_splice_write), 1},
2508 { "splice_move", offsetof(struct fuse_ll, splice_move), 1},
2509 { "no_splice_move", offsetof(struct fuse_ll, no_splice_move), 1},
2510 { "splice_read", offsetof(struct fuse_ll, splice_read), 1},
2511 { "no_splice_read", offsetof(struct fuse_ll, no_splice_read), 1},
2512 FUSE_OPT_KEY("max_read=", FUSE_OPT_KEY_DISCARD),
2513 FUSE_OPT_KEY("-h", KEY_HELP),
2514 FUSE_OPT_KEY("--help", KEY_HELP),
2515 FUSE_OPT_KEY("-V", KEY_VERSION),
2516 FUSE_OPT_KEY("--version", KEY_VERSION),
2517 FUSE_OPT_END
2520 static void fuse_ll_version(void)
2522 fprintf(stderr, "using FUSE kernel interface version %i.%i\n",
2523 FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
2526 static void fuse_ll_help(void)
2528 fprintf(stderr,
2529 " -o max_write=N set maximum size of write requests\n"
2530 " -o max_readahead=N set maximum readahead\n"
2531 " -o max_background=N set number of maximum background requests\n"
2532 " -o congestion_threshold=N set kernel's congestion threshold\n"
2533 " -o async_read perform reads asynchronously (default)\n"
2534 " -o sync_read perform reads synchronously\n"
2535 " -o atomic_o_trunc enable atomic open+truncate support\n"
2536 " -o big_writes enable larger than 4kB writes\n"
2537 " -o no_remote_lock disable remote file locking\n"
2538 " -o no_remote_flock disable remote file locking (BSD)\n"
2539 " -o no_remote_posix_lock disable remove file locking (POSIX)\n"
2540 " -o [no_]splice_write use splice to write to the fuse device\n"
2541 " -o [no_]splice_move move data while splicing to the fuse device\n"
2542 " -o [no_]splice_read use splice to read from the fuse device\n"
2546 static int fuse_ll_opt_proc(void *data, const char *arg, int key,
2547 struct fuse_args *outargs)
2549 (void) data; (void) outargs;
2551 switch (key) {
2552 case KEY_HELP:
2553 fuse_ll_help();
2554 break;
2556 case KEY_VERSION:
2557 fuse_ll_version();
2558 break;
2560 default:
2561 fprintf(stderr, "fuse: unknown option `%s'\n", arg);
2564 return -1;
2567 int fuse_lowlevel_is_lib_option(const char *opt)
2569 return fuse_opt_match(fuse_ll_opts, opt);
2572 static void fuse_ll_destroy(void *data)
2574 struct fuse_ll *f = (struct fuse_ll *) data;
2575 struct fuse_ll_pipe *llp;
2577 if (f->got_init && !f->got_destroy) {
2578 if (f->op.destroy)
2579 f->op.destroy(f->userdata);
2581 llp = pthread_getspecific(f->pipe_key);
2582 if (llp != NULL)
2583 fuse_ll_pipe_free(llp);
2584 pthread_key_delete(f->pipe_key);
2585 pthread_mutex_destroy(&f->lock);
2586 free(f->cuse_data);
2587 free(f);
2590 static void fuse_ll_pipe_destructor(void *data)
2592 struct fuse_ll_pipe *llp = data;
2593 fuse_ll_pipe_free(llp);
2596 #ifdef HAVE_SPLICE
2597 static int fuse_ll_receive_buf(struct fuse_session *se, struct fuse_buf *buf,
2598 struct fuse_chan **chp)
2600 struct fuse_chan *ch = *chp;
2601 struct fuse_ll *f = fuse_session_data(se);
2602 size_t bufsize = buf->size;
2603 struct fuse_ll_pipe *llp;
2604 struct fuse_buf tmpbuf;
2605 int err;
2606 int res;
2608 if (f->conn.proto_minor < 14 || !(f->conn.want & FUSE_CAP_SPLICE_READ))
2609 goto fallback;
2611 llp = fuse_ll_get_pipe(f);
2612 if (llp == NULL)
2613 goto fallback;
2615 if (llp->size < bufsize) {
2616 if (llp->can_grow) {
2617 res = fcntl(llp->pipe[0], F_SETPIPE_SZ, bufsize);
2618 if (res == -1) {
2619 llp->can_grow = 0;
2620 goto fallback;
2622 llp->size = res;
2624 if (llp->size < bufsize)
2625 goto fallback;
2628 res = splice(fuse_chan_fd(ch), NULL, llp->pipe[1], NULL, bufsize, 0);
2629 err = errno;
2631 if (fuse_session_exited(se))
2632 return 0;
2634 if (res == -1) {
2635 if (err == ENODEV) {
2636 fuse_session_exit(se);
2637 return 0;
2639 if (err != EINTR && err != EAGAIN)
2640 perror("fuse: splice from device");
2641 return -err;
2644 if (res < sizeof(struct fuse_in_header)) {
2645 fprintf(stderr, "short splice from fuse device\n");
2646 return -EIO;
2649 tmpbuf = (struct fuse_buf) {
2650 .size = res,
2651 .flags = FUSE_BUF_IS_FD,
2652 .fd = llp->pipe[0],
2656 * Don't bother with zero copy for small requests.
2657 * fuse_loop_mt() needs to check for FORGET so this more than
2658 * just an optimization.
2660 if (res < sizeof(struct fuse_in_header) +
2661 sizeof(struct fuse_write_in) + pagesize) {
2662 struct fuse_bufvec src = { .buf[0] = tmpbuf, .count = 1 };
2663 struct fuse_bufvec dst = { .buf[0] = *buf, .count = 1 };
2665 res = fuse_buf_copy(&dst, &src, 0);
2666 if (res < 0) {
2667 fprintf(stderr, "fuse: copy from pipe: %s\n",
2668 strerror(-res));
2669 fuse_ll_clear_pipe(f);
2670 return res;
2672 if (res < tmpbuf.size) {
2673 fprintf(stderr, "fuse: copy from pipe: short read\n");
2674 fuse_ll_clear_pipe(f);
2675 return -EIO;
2677 buf->size = tmpbuf.size;
2678 return buf->size;
2681 *buf = tmpbuf;
2683 return res;
2685 fallback:
2686 res = fuse_chan_recv(chp, buf->mem, bufsize);
2687 if (res <= 0)
2688 return res;
2690 buf->size = res;
2692 return res;
2694 #else
2695 static int fuse_ll_receive_buf(struct fuse_session *se, struct fuse_buf *buf,
2696 struct fuse_chan **chp)
2698 (void) se;
2700 int res = fuse_chan_recv(chp, buf->mem, buf->size);
2701 if (res <= 0)
2702 return res;
2704 buf->size = res;
2706 return res;
2708 #endif
2712 * always call fuse_lowlevel_new_common() internally, to work around a
2713 * misfeature in the FreeBSD runtime linker, which links the old
2714 * version of a symbol to internal references.
2716 struct fuse_session *fuse_lowlevel_new_common(struct fuse_args *args,
2717 const struct fuse_lowlevel_ops *op,
2718 size_t op_size, void *userdata)
2720 int err;
2721 struct fuse_ll *f;
2722 struct fuse_session *se;
2723 struct fuse_session_ops sop = {
2724 .process = fuse_ll_process,
2725 .destroy = fuse_ll_destroy,
2728 if (sizeof(struct fuse_lowlevel_ops) < op_size) {
2729 fprintf(stderr, "fuse: warning: library too old, some operations may not work\n");
2730 op_size = sizeof(struct fuse_lowlevel_ops);
2733 f = (struct fuse_ll *) calloc(1, sizeof(struct fuse_ll));
2734 if (f == NULL) {
2735 fprintf(stderr, "fuse: failed to allocate fuse object\n");
2736 goto out;
2739 f->conn.async_read = 1;
2740 f->conn.max_write = UINT_MAX;
2741 f->conn.max_readahead = UINT_MAX;
2742 f->atomic_o_trunc = 0;
2743 list_init_req(&f->list);
2744 list_init_req(&f->interrupts);
2745 list_init_nreq(&f->notify_list);
2746 f->notify_ctr = 1;
2747 fuse_mutex_init(&f->lock);
2749 err = pthread_key_create(&f->pipe_key, fuse_ll_pipe_destructor);
2750 if (err) {
2751 fprintf(stderr, "fuse: failed to create thread specific key: %s\n",
2752 strerror(err));
2753 goto out_free;
2756 if (fuse_opt_parse(args, f, fuse_ll_opts, fuse_ll_opt_proc) == -1)
2757 goto out_key_destroy;
2759 if (f->debug)
2760 fprintf(stderr, "FUSE library version: %s\n", PACKAGE_VERSION);
2762 memcpy(&f->op, op, op_size);
2763 f->owner = getuid();
2764 f->userdata = userdata;
2766 se = fuse_session_new(&sop, f);
2767 if (!se)
2768 goto out_key_destroy;
2770 se->receive_buf = fuse_ll_receive_buf;
2771 se->process_buf = fuse_ll_process_buf;
2773 return se;
2775 out_key_destroy:
2776 pthread_key_delete(f->pipe_key);
2777 out_free:
2778 pthread_mutex_destroy(&f->lock);
2779 free(f);
2780 out:
2781 return NULL;
2785 struct fuse_session *fuse_lowlevel_new(struct fuse_args *args,
2786 const struct fuse_lowlevel_ops *op,
2787 size_t op_size, void *userdata)
2789 return fuse_lowlevel_new_common(args, op, op_size, userdata);
2792 #ifdef linux
2793 int fuse_req_getgroups(fuse_req_t req, int size, gid_t list[])
2795 char *buf;
2796 size_t bufsize = 1024;
2797 char path[128];
2798 int ret;
2799 int fd;
2800 unsigned long pid = req->ctx.pid;
2801 char *s;
2803 sprintf(path, "/proc/%lu/task/%lu/status", pid, pid);
2805 retry:
2806 buf = malloc(bufsize);
2807 if (buf == NULL)
2808 return -ENOMEM;
2810 ret = -EIO;
2811 fd = open(path, O_RDONLY);
2812 if (fd == -1)
2813 goto out_free;
2815 ret = read(fd, buf, bufsize);
2816 close(fd);
2817 if (ret == -1) {
2818 ret = -EIO;
2819 goto out_free;
2822 if (ret == bufsize) {
2823 free(buf);
2824 bufsize *= 4;
2825 goto retry;
2828 ret = -EIO;
2829 s = strstr(buf, "\nGroups:");
2830 if (s == NULL)
2831 goto out_free;
2833 s += 8;
2834 ret = 0;
2835 while (1) {
2836 char *end;
2837 unsigned long val = strtoul(s, &end, 0);
2838 if (end == s)
2839 break;
2841 s = end;
2842 if (ret < size)
2843 list[ret] = val;
2844 ret++;
2847 out_free:
2848 free(buf);
2849 return ret;
2851 #else /* linux */
2853 * This is currently not implemented on other than Linux...
2855 int fuse_req_getgroups(fuse_req_t req, int size, gid_t list[])
2857 return -ENOSYS;
2859 #endif
2861 #if !defined(__FreeBSD__) && !defined(__NetBSD__)
2863 static void fill_open_compat(struct fuse_open_out *arg,
2864 const struct fuse_file_info_compat *f)
2866 arg->fh = f->fh;
2867 if (f->direct_io)
2868 arg->open_flags |= FOPEN_DIRECT_IO;
2869 if (f->keep_cache)
2870 arg->open_flags |= FOPEN_KEEP_CACHE;
2873 static void convert_statfs_compat(const struct statfs *compatbuf,
2874 struct statvfs *buf)
2876 buf->f_bsize = compatbuf->f_bsize;
2877 buf->f_blocks = compatbuf->f_blocks;
2878 buf->f_bfree = compatbuf->f_bfree;
2879 buf->f_bavail = compatbuf->f_bavail;
2880 buf->f_files = compatbuf->f_files;
2881 buf->f_ffree = compatbuf->f_ffree;
2882 buf->f_namemax = compatbuf->f_namelen;
2885 int fuse_reply_open_compat(fuse_req_t req,
2886 const struct fuse_file_info_compat *f)
2888 struct fuse_open_out arg;
2890 memset(&arg, 0, sizeof(arg));
2891 fill_open_compat(&arg, f);
2892 return send_reply_ok(req, &arg, sizeof(arg));
2895 int fuse_reply_statfs_compat(fuse_req_t req, const struct statfs *stbuf)
2897 struct statvfs newbuf;
2899 memset(&newbuf, 0, sizeof(newbuf));
2900 convert_statfs_compat(stbuf, &newbuf);
2902 return fuse_reply_statfs(req, &newbuf);
2905 struct fuse_session *fuse_lowlevel_new_compat(const char *opts,
2906 const struct fuse_lowlevel_ops_compat *op,
2907 size_t op_size, void *userdata)
2909 struct fuse_session *se;
2910 struct fuse_args args = FUSE_ARGS_INIT(0, NULL);
2912 if (opts &&
2913 (fuse_opt_add_arg(&args, "") == -1 ||
2914 fuse_opt_add_arg(&args, "-o") == -1 ||
2915 fuse_opt_add_arg(&args, opts) == -1)) {
2916 fuse_opt_free_args(&args);
2917 return NULL;
2919 se = fuse_lowlevel_new(&args, (const struct fuse_lowlevel_ops *) op,
2920 op_size, userdata);
2921 fuse_opt_free_args(&args);
2923 return se;
2926 struct fuse_ll_compat_conf {
2927 unsigned max_read;
2928 int set_max_read;
2931 static const struct fuse_opt fuse_ll_opts_compat[] = {
2932 { "max_read=", offsetof(struct fuse_ll_compat_conf, set_max_read), 1 },
2933 { "max_read=%u", offsetof(struct fuse_ll_compat_conf, max_read), 0 },
2934 FUSE_OPT_KEY("max_read=", FUSE_OPT_KEY_KEEP),
2935 FUSE_OPT_END
2938 int fuse_sync_compat_args(struct fuse_args *args)
2940 struct fuse_ll_compat_conf conf;
2942 memset(&conf, 0, sizeof(conf));
2943 if (fuse_opt_parse(args, &conf, fuse_ll_opts_compat, NULL) == -1)
2944 return -1;
2946 if (fuse_opt_insert_arg(args, 1, "-osync_read"))
2947 return -1;
2949 if (conf.set_max_read) {
2950 char tmpbuf[64];
2952 sprintf(tmpbuf, "-omax_readahead=%u", conf.max_read);
2953 if (fuse_opt_insert_arg(args, 1, tmpbuf) == -1)
2954 return -1;
2956 return 0;
2959 FUSE_SYMVER(".symver fuse_reply_statfs_compat,fuse_reply_statfs@FUSE_2.4");
2960 FUSE_SYMVER(".symver fuse_reply_open_compat,fuse_reply_open@FUSE_2.4");
2961 FUSE_SYMVER(".symver fuse_lowlevel_new_compat,fuse_lowlevel_new@FUSE_2.4");
2963 #else /* __FreeBSD__ || __NetBSD__ */
2965 int fuse_sync_compat_args(struct fuse_args *args)
2967 (void) args;
2968 return 0;
2971 #endif /* __FreeBSD__ || __NetBSD__ */
2973 struct fuse_session *fuse_lowlevel_new_compat25(struct fuse_args *args,
2974 const struct fuse_lowlevel_ops_compat25 *op,
2975 size_t op_size, void *userdata)
2977 if (fuse_sync_compat_args(args) == -1)
2978 return NULL;
2980 return fuse_lowlevel_new_common(args,
2981 (const struct fuse_lowlevel_ops *) op,
2982 op_size, userdata);
2985 FUSE_SYMVER(".symver fuse_lowlevel_new_compat25,fuse_lowlevel_new@FUSE_2.5");