blob: 5f223c98fc39fe0b4acad7b9b07e1515cf9d738d [file] [log] [blame]
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001/*
2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2007 Miklos Szeredi <miklos@szeredi.hu>
4
5 This program can be distributed under the terms of the GNU LGPLv2.
6 See the file COPYING.LIB
7*/
8
Dees_Troye34c1332013-02-06 19:13:00 +00009#define _GNU_SOURCE
10
11#include "config.h"
bigbiff bigbiff9c754052013-01-09 09:09:08 -050012#include "fuse_i.h"
13#include "fuse_kernel.h"
14#include "fuse_opt.h"
15#include "fuse_misc.h"
16#include "fuse_common_compat.h"
17#include "fuse_lowlevel_compat.h"
18
bigbiff bigbiff9c754052013-01-09 09:09:08 -050019#include <stdio.h>
20#include <stdlib.h>
21#include <stddef.h>
22#include <string.h>
23#include <unistd.h>
24#include <limits.h>
25#include <errno.h>
Dees_Troye34c1332013-02-06 19:13:00 +000026#include <assert.h>
Matt Mower523a0592015-12-13 11:31:00 -060027#include <sys/file.h>
Dees_Troye34c1332013-02-06 19:13:00 +000028
29#ifndef F_LINUX_SPECIFIC_BASE
30#define F_LINUX_SPECIFIC_BASE 1024
31#endif
32#ifndef F_SETPIPE_SZ
33#define F_SETPIPE_SZ (F_LINUX_SPECIFIC_BASE + 7)
34#endif
35
bigbiff bigbiff9c754052013-01-09 09:09:08 -050036
37#define PARAM(inarg) (((char *)(inarg)) + sizeof(*(inarg)))
38#define OFFSET_MAX 0x7fffffffffffffffLL
39
Dees_Troye34c1332013-02-06 19:13:00 +000040#define container_of(ptr, type, member) ({ \
41 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
42 (type *)( (char *)__mptr - offsetof(type,member) );})
43
bigbiff bigbiff9c754052013-01-09 09:09:08 -050044struct fuse_pollhandle {
45 uint64_t kh;
46 struct fuse_chan *ch;
47 struct fuse_ll *f;
48};
49
Dees_Troye34c1332013-02-06 19:13:00 +000050static size_t pagesize;
51
52static __attribute__((constructor)) void fuse_ll_init_pagesize(void)
53{
54 pagesize = getpagesize();
55}
56
bigbiff bigbiff9c754052013-01-09 09:09:08 -050057static void convert_stat(const struct stat *stbuf, struct fuse_attr *attr)
58{
59 attr->ino = stbuf->st_ino;
60 attr->mode = stbuf->st_mode;
61 attr->nlink = stbuf->st_nlink;
62 attr->uid = stbuf->st_uid;
63 attr->gid = stbuf->st_gid;
64 attr->rdev = stbuf->st_rdev;
65 attr->size = stbuf->st_size;
66 attr->blksize = stbuf->st_blksize;
67 attr->blocks = stbuf->st_blocks;
68 attr->atime = stbuf->st_atime;
69 attr->mtime = stbuf->st_mtime;
70 attr->ctime = stbuf->st_ctime;
71 attr->atimensec = ST_ATIM_NSEC(stbuf);
72 attr->mtimensec = ST_MTIM_NSEC(stbuf);
73 attr->ctimensec = ST_CTIM_NSEC(stbuf);
74}
75
76static void convert_attr(const struct fuse_setattr_in *attr, struct stat *stbuf)
77{
78 stbuf->st_mode = attr->mode;
79 stbuf->st_uid = attr->uid;
80 stbuf->st_gid = attr->gid;
81 stbuf->st_size = attr->size;
82 stbuf->st_atime = attr->atime;
83 stbuf->st_mtime = attr->mtime;
84 ST_ATIM_NSEC_SET(stbuf, attr->atimensec);
85 ST_MTIM_NSEC_SET(stbuf, attr->mtimensec);
86}
87
88static size_t iov_length(const struct iovec *iov, size_t count)
89{
90 size_t seg;
91 size_t ret = 0;
92
93 for (seg = 0; seg < count; seg++)
94 ret += iov[seg].iov_len;
95 return ret;
96}
97
98static void list_init_req(struct fuse_req *req)
99{
100 req->next = req;
101 req->prev = req;
102}
103
104static void list_del_req(struct fuse_req *req)
105{
106 struct fuse_req *prev = req->prev;
107 struct fuse_req *next = req->next;
108 prev->next = next;
109 next->prev = prev;
110}
111
112static void list_add_req(struct fuse_req *req, struct fuse_req *next)
113{
114 struct fuse_req *prev = next->prev;
115 req->next = next;
116 req->prev = prev;
117 prev->next = req;
118 next->prev = req;
119}
120
121static void destroy_req(fuse_req_t req)
122{
123 pthread_mutex_destroy(&req->lock);
124 free(req);
125}
126
127void fuse_free_req(fuse_req_t req)
128{
129 int ctr;
130 struct fuse_ll *f = req->f;
131
132 pthread_mutex_lock(&f->lock);
133 req->u.ni.func = NULL;
134 req->u.ni.data = NULL;
135 list_del_req(req);
136 ctr = --req->ctr;
137 pthread_mutex_unlock(&f->lock);
138 if (!ctr)
139 destroy_req(req);
140}
141
Dees_Troye34c1332013-02-06 19:13:00 +0000142static struct fuse_req *fuse_ll_alloc_req(struct fuse_ll *f)
143{
144 struct fuse_req *req;
145
146 req = (struct fuse_req *) calloc(1, sizeof(struct fuse_req));
147 if (req == NULL) {
148 fprintf(stderr, "fuse: failed to allocate request\n");
149 } else {
150 req->f = f;
151 req->ctr = 1;
152 list_init_req(req);
153 fuse_mutex_init(&req->lock);
154 }
155
156 return req;
157}
158
159
160static int fuse_send_msg(struct fuse_ll *f, struct fuse_chan *ch,
161 struct iovec *iov, int count)
162{
163 struct fuse_out_header *out = iov[0].iov_base;
164
165 out->len = iov_length(iov, count);
166 if (f->debug) {
167 if (out->unique == 0) {
168 fprintf(stderr, "NOTIFY: code=%d length=%u\n",
169 out->error, out->len);
170 } else if (out->error) {
171 fprintf(stderr,
172 " unique: %llu, error: %i (%s), outsize: %i\n",
173 (unsigned long long) out->unique, out->error,
174 strerror(-out->error), out->len);
175 } else {
176 fprintf(stderr,
177 " unique: %llu, success, outsize: %i\n",
178 (unsigned long long) out->unique, out->len);
179 }
180 }
181
182 return fuse_chan_send(ch, iov, count);
183}
184
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500185int fuse_send_reply_iov_nofree(fuse_req_t req, int error, struct iovec *iov,
186 int count)
187{
188 struct fuse_out_header out;
189
190 if (error <= -1000 || error > 0) {
191 fprintf(stderr, "fuse: bad error value: %i\n", error);
192 error = -ERANGE;
193 }
194
195 out.unique = req->unique;
196 out.error = error;
Dees_Troye34c1332013-02-06 19:13:00 +0000197
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500198 iov[0].iov_base = &out;
199 iov[0].iov_len = sizeof(struct fuse_out_header);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500200
Dees_Troye34c1332013-02-06 19:13:00 +0000201 return fuse_send_msg(req->f, req->ch, iov, count);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500202}
203
204static int send_reply_iov(fuse_req_t req, int error, struct iovec *iov,
205 int count)
206{
207 int res;
208
209 res = fuse_send_reply_iov_nofree(req, error, iov, count);
210 fuse_free_req(req);
211 return res;
212}
213
214static int send_reply(fuse_req_t req, int error, const void *arg,
215 size_t argsize)
216{
217 struct iovec iov[2];
218 int count = 1;
219 if (argsize) {
220 iov[1].iov_base = (void *) arg;
221 iov[1].iov_len = argsize;
222 count++;
223 }
224 return send_reply_iov(req, error, iov, count);
225}
226
227int fuse_reply_iov(fuse_req_t req, const struct iovec *iov, int count)
228{
229 int res;
230 struct iovec *padded_iov;
231
232 padded_iov = malloc((count + 1) * sizeof(struct iovec));
Dees_Troye34c1332013-02-06 19:13:00 +0000233 if (padded_iov == NULL)
234 return fuse_reply_err(req, ENOMEM);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500235
236 memcpy(padded_iov + 1, iov, count * sizeof(struct iovec));
237 count++;
238
239 res = send_reply_iov(req, 0, padded_iov, count);
240 free(padded_iov);
241
242 return res;
243}
244
245size_t fuse_dirent_size(size_t namelen)
246{
247 return FUSE_DIRENT_ALIGN(FUSE_NAME_OFFSET + namelen);
248}
249
250char *fuse_add_dirent(char *buf, const char *name, const struct stat *stbuf,
Matt Mower523a0592015-12-13 11:31:00 -0600251 loff_t off)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500252{
253 unsigned namelen = strlen(name);
254 unsigned entlen = FUSE_NAME_OFFSET + namelen;
255 unsigned entsize = fuse_dirent_size(namelen);
256 unsigned padlen = entsize - entlen;
257 struct fuse_dirent *dirent = (struct fuse_dirent *) buf;
258
259 dirent->ino = stbuf->st_ino;
260 dirent->off = off;
261 dirent->namelen = namelen;
262 dirent->type = (stbuf->st_mode & 0170000) >> 12;
263 strncpy(dirent->name, name, namelen);
264 if (padlen)
265 memset(buf + entlen, 0, padlen);
266
267 return buf + entsize;
268}
269
270size_t fuse_add_direntry(fuse_req_t req, char *buf, size_t bufsize,
Matt Mower523a0592015-12-13 11:31:00 -0600271 const char *name, const struct stat *stbuf, loff_t off)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500272{
273 size_t entsize;
274
275 (void) req;
276 entsize = fuse_dirent_size(strlen(name));
277 if (entsize <= bufsize && buf)
278 fuse_add_dirent(buf, name, stbuf, off);
279 return entsize;
280}
281
282static void convert_statfs(const struct statvfs *stbuf,
283 struct fuse_kstatfs *kstatfs)
284{
285 kstatfs->bsize = stbuf->f_bsize;
286 kstatfs->frsize = stbuf->f_frsize;
287 kstatfs->blocks = stbuf->f_blocks;
288 kstatfs->bfree = stbuf->f_bfree;
289 kstatfs->bavail = stbuf->f_bavail;
290 kstatfs->files = stbuf->f_files;
291 kstatfs->ffree = stbuf->f_ffree;
292 kstatfs->namelen = stbuf->f_namemax;
293}
294
295static int send_reply_ok(fuse_req_t req, const void *arg, size_t argsize)
296{
297 return send_reply(req, 0, arg, argsize);
298}
299
300int fuse_reply_err(fuse_req_t req, int err)
301{
302 return send_reply(req, -err, NULL, 0);
303}
304
305void fuse_reply_none(fuse_req_t req)
306{
Dees_Troye34c1332013-02-06 19:13:00 +0000307 if (req->ch)
308 fuse_chan_send(req->ch, NULL, 0);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500309 fuse_free_req(req);
310}
311
312static unsigned long calc_timeout_sec(double t)
313{
314 if (t > (double) ULONG_MAX)
315 return ULONG_MAX;
316 else if (t < 0.0)
317 return 0;
318 else
319 return (unsigned long) t;
320}
321
322static unsigned int calc_timeout_nsec(double t)
323{
324 double f = t - (double) calc_timeout_sec(t);
325 if (f < 0.0)
326 return 0;
327 else if (f >= 0.999999999)
328 return 999999999;
329 else
330 return (unsigned int) (f * 1.0e9);
331}
332
333static void fill_entry(struct fuse_entry_out *arg,
334 const struct fuse_entry_param *e)
335{
336 arg->nodeid = e->ino;
337 arg->generation = e->generation;
338 arg->entry_valid = calc_timeout_sec(e->entry_timeout);
339 arg->entry_valid_nsec = calc_timeout_nsec(e->entry_timeout);
340 arg->attr_valid = calc_timeout_sec(e->attr_timeout);
341 arg->attr_valid_nsec = calc_timeout_nsec(e->attr_timeout);
342 convert_stat(&e->attr, &arg->attr);
343}
344
345static void fill_open(struct fuse_open_out *arg,
346 const struct fuse_file_info *f)
347{
348 arg->fh = f->fh;
349 if (f->direct_io)
350 arg->open_flags |= FOPEN_DIRECT_IO;
351 if (f->keep_cache)
352 arg->open_flags |= FOPEN_KEEP_CACHE;
353 if (f->nonseekable)
354 arg->open_flags |= FOPEN_NONSEEKABLE;
355}
356
357int fuse_reply_entry(fuse_req_t req, const struct fuse_entry_param *e)
358{
359 struct fuse_entry_out arg;
360 size_t size = req->f->conn.proto_minor < 9 ?
361 FUSE_COMPAT_ENTRY_OUT_SIZE : sizeof(arg);
362
363 /* before ABI 7.4 e->ino == 0 was invalid, only ENOENT meant
364 negative entry */
Dees_Troye34c1332013-02-06 19:13:00 +0000365 if (!e->ino && req->f->conn.proto_minor < 4)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500366 return fuse_reply_err(req, ENOENT);
367
368 memset(&arg, 0, sizeof(arg));
369 fill_entry(&arg, e);
370 return send_reply_ok(req, &arg, size);
371}
372
373int fuse_reply_create(fuse_req_t req, const struct fuse_entry_param *e,
374 const struct fuse_file_info *f)
375{
376 char buf[sizeof(struct fuse_entry_out) + sizeof(struct fuse_open_out)];
377 size_t entrysize = req->f->conn.proto_minor < 9 ?
378 FUSE_COMPAT_ENTRY_OUT_SIZE : sizeof(struct fuse_entry_out);
379 struct fuse_entry_out *earg = (struct fuse_entry_out *) buf;
380 struct fuse_open_out *oarg = (struct fuse_open_out *) (buf + entrysize);
381
382 memset(buf, 0, sizeof(buf));
383 fill_entry(earg, e);
384 fill_open(oarg, f);
385 return send_reply_ok(req, buf,
386 entrysize + sizeof(struct fuse_open_out));
387}
388
389int fuse_reply_attr(fuse_req_t req, const struct stat *attr,
390 double attr_timeout)
391{
392 struct fuse_attr_out arg;
393 size_t size = req->f->conn.proto_minor < 9 ?
394 FUSE_COMPAT_ATTR_OUT_SIZE : sizeof(arg);
395
396 memset(&arg, 0, sizeof(arg));
397 arg.attr_valid = calc_timeout_sec(attr_timeout);
398 arg.attr_valid_nsec = calc_timeout_nsec(attr_timeout);
399 convert_stat(attr, &arg.attr);
400
401 return send_reply_ok(req, &arg, size);
402}
403
404int fuse_reply_readlink(fuse_req_t req, const char *linkname)
405{
406 return send_reply_ok(req, linkname, strlen(linkname));
407}
408
409int fuse_reply_open(fuse_req_t req, const struct fuse_file_info *f)
410{
411 struct fuse_open_out arg;
412
413 memset(&arg, 0, sizeof(arg));
414 fill_open(&arg, f);
415 return send_reply_ok(req, &arg, sizeof(arg));
416}
417
418int fuse_reply_write(fuse_req_t req, size_t count)
419{
420 struct fuse_write_out arg;
421
422 memset(&arg, 0, sizeof(arg));
423 arg.size = count;
424
425 return send_reply_ok(req, &arg, sizeof(arg));
426}
427
428int fuse_reply_buf(fuse_req_t req, const char *buf, size_t size)
429{
430 return send_reply_ok(req, buf, size);
431}
432
Dees_Troye34c1332013-02-06 19:13:00 +0000433static int fuse_send_data_iov_fallback(struct fuse_ll *f, struct fuse_chan *ch,
434 struct iovec *iov, int iov_count,
435 struct fuse_bufvec *buf,
436 size_t len)
437{
438 struct fuse_bufvec mem_buf = FUSE_BUFVEC_INIT(len);
439 void *mbuf;
440 int res;
441
442 /* Optimize common case */
443 if (buf->count == 1 && buf->idx == 0 && buf->off == 0 &&
444 !(buf->buf[0].flags & FUSE_BUF_IS_FD)) {
445 /* FIXME: also avoid memory copy if there are multiple buffers
446 but none of them contain an fd */
447
448 iov[iov_count].iov_base = buf->buf[0].mem;
449 iov[iov_count].iov_len = len;
450 iov_count++;
451 return fuse_send_msg(f, ch, iov, iov_count);
452 }
453
454 res = posix_memalign(&mbuf, pagesize, len);
455 if (res != 0)
456 return res;
457
458 mem_buf.buf[0].mem = mbuf;
459 res = fuse_buf_copy(&mem_buf, buf, 0);
460 if (res < 0) {
461 free(mbuf);
462 return -res;
463 }
464 len = res;
465
466 iov[iov_count].iov_base = mbuf;
467 iov[iov_count].iov_len = len;
468 iov_count++;
469 res = fuse_send_msg(f, ch, iov, iov_count);
470 free(mbuf);
471
472 return res;
473}
474
475struct fuse_ll_pipe {
476 size_t size;
477 int can_grow;
478 int pipe[2];
479};
480
481static void fuse_ll_pipe_free(struct fuse_ll_pipe *llp)
482{
483 close(llp->pipe[0]);
484 close(llp->pipe[1]);
485 free(llp);
486}
487
488#ifdef HAVE_SPLICE
489static struct fuse_ll_pipe *fuse_ll_get_pipe(struct fuse_ll *f)
490{
491 struct fuse_ll_pipe *llp = pthread_getspecific(f->pipe_key);
492 if (llp == NULL) {
493 int res;
494
495 llp = malloc(sizeof(struct fuse_ll_pipe));
496 if (llp == NULL)
497 return NULL;
498
499 res = pipe(llp->pipe);
500 if (res == -1) {
501 free(llp);
502 return NULL;
503 }
504
505 if (fcntl(llp->pipe[0], F_SETFL, O_NONBLOCK) == -1 ||
506 fcntl(llp->pipe[1], F_SETFL, O_NONBLOCK) == -1) {
507 close(llp->pipe[0]);
508 close(llp->pipe[1]);
509 free(llp);
510 return NULL;
511 }
512
513 /*
514 *the default size is 16 pages on linux
515 */
516 llp->size = pagesize * 16;
517 llp->can_grow = 1;
518
519 pthread_setspecific(f->pipe_key, llp);
520 }
521
522 return llp;
523}
524#endif
525
526static void fuse_ll_clear_pipe(struct fuse_ll *f)
527{
528 struct fuse_ll_pipe *llp = pthread_getspecific(f->pipe_key);
529 if (llp) {
530 pthread_setspecific(f->pipe_key, NULL);
531 fuse_ll_pipe_free(llp);
532 }
533}
534
535#if defined(HAVE_SPLICE) && defined(HAVE_VMSPLICE)
536static int read_back(int fd, char *buf, size_t len)
537{
538 int res;
539
540 res = read(fd, buf, len);
541 if (res == -1) {
542 fprintf(stderr, "fuse: internal error: failed to read back from pipe: %s\n", strerror(errno));
543 return -EIO;
544 }
545 if (res != len) {
546 fprintf(stderr, "fuse: internal error: short read back from pipe: %i from %zi\n", res, len);
547 return -EIO;
548 }
549 return 0;
550}
551
552static int fuse_send_data_iov(struct fuse_ll *f, struct fuse_chan *ch,
553 struct iovec *iov, int iov_count,
554 struct fuse_bufvec *buf, unsigned int flags)
555{
556 int res;
557 size_t len = fuse_buf_size(buf);
558 struct fuse_out_header *out = iov[0].iov_base;
559 struct fuse_ll_pipe *llp;
560 int splice_flags;
561 size_t pipesize;
562 size_t total_fd_size;
563 size_t idx;
564 size_t headerlen;
565 struct fuse_bufvec pipe_buf = FUSE_BUFVEC_INIT(len);
566
567 if (f->broken_splice_nonblock)
568 goto fallback;
569
570 if (flags & FUSE_BUF_NO_SPLICE)
571 goto fallback;
572
573 total_fd_size = 0;
574 for (idx = buf->idx; idx < buf->count; idx++) {
575 if (buf->buf[idx].flags & FUSE_BUF_IS_FD) {
576 total_fd_size = buf->buf[idx].size;
577 if (idx == buf->idx)
578 total_fd_size -= buf->off;
579 }
580 }
581 if (total_fd_size < 2 * pagesize)
582 goto fallback;
583
584 if (f->conn.proto_minor < 14 ||
585 !(f->conn.want & FUSE_CAP_SPLICE_WRITE))
586 goto fallback;
587
588 llp = fuse_ll_get_pipe(f);
589 if (llp == NULL)
590 goto fallback;
591
592
593 headerlen = iov_length(iov, iov_count);
594
595 out->len = headerlen + len;
596
597 /*
598 * Heuristic for the required pipe size, does not work if the
599 * source contains less than page size fragments
600 */
601 pipesize = pagesize * (iov_count + buf->count + 1) + out->len;
602
603 if (llp->size < pipesize) {
604 if (llp->can_grow) {
605 res = fcntl(llp->pipe[0], F_SETPIPE_SZ, pipesize);
606 if (res == -1) {
607 llp->can_grow = 0;
608 goto fallback;
609 }
610 llp->size = res;
611 }
612 if (llp->size < pipesize)
613 goto fallback;
614 }
615
616
617 res = vmsplice(llp->pipe[1], iov, iov_count, SPLICE_F_NONBLOCK);
618 if (res == -1)
619 goto fallback;
620
621 if (res != headerlen) {
622 res = -EIO;
623 fprintf(stderr, "fuse: short vmsplice to pipe: %u/%zu\n", res,
624 headerlen);
625 goto clear_pipe;
626 }
627
628 pipe_buf.buf[0].flags = FUSE_BUF_IS_FD;
629 pipe_buf.buf[0].fd = llp->pipe[1];
630
631 res = fuse_buf_copy(&pipe_buf, buf,
632 FUSE_BUF_FORCE_SPLICE | FUSE_BUF_SPLICE_NONBLOCK);
633 if (res < 0) {
634 if (res == -EAGAIN || res == -EINVAL) {
635 /*
636 * Should only get EAGAIN on kernels with
637 * broken SPLICE_F_NONBLOCK support (<=
638 * 2.6.35) where this error or a short read is
639 * returned even if the pipe itself is not
640 * full
641 *
642 * EINVAL might mean that splice can't handle
643 * this combination of input and output.
644 */
645 if (res == -EAGAIN)
646 f->broken_splice_nonblock = 1;
647
648 pthread_setspecific(f->pipe_key, NULL);
649 fuse_ll_pipe_free(llp);
650 goto fallback;
651 }
652 res = -res;
653 goto clear_pipe;
654 }
655
656 if (res != 0 && res < len) {
657 struct fuse_bufvec mem_buf = FUSE_BUFVEC_INIT(len);
658 void *mbuf;
659 size_t now_len = res;
660 /*
661 * For regular files a short count is either
662 * 1) due to EOF, or
663 * 2) because of broken SPLICE_F_NONBLOCK (see above)
664 *
665 * For other inputs it's possible that we overflowed
666 * the pipe because of small buffer fragments.
667 */
668
669 res = posix_memalign(&mbuf, pagesize, len);
670 if (res != 0)
671 goto clear_pipe;
672
673 mem_buf.buf[0].mem = mbuf;
674 mem_buf.off = now_len;
675 res = fuse_buf_copy(&mem_buf, buf, 0);
676 if (res > 0) {
677 char *tmpbuf;
678 size_t extra_len = res;
679 /*
680 * Trickiest case: got more data. Need to get
681 * back the data from the pipe and then fall
682 * back to regular write.
683 */
684 tmpbuf = malloc(headerlen);
685 if (tmpbuf == NULL) {
686 free(mbuf);
687 res = ENOMEM;
688 goto clear_pipe;
689 }
690 res = read_back(llp->pipe[0], tmpbuf, headerlen);
691 if (res != 0) {
692 free(mbuf);
693 goto clear_pipe;
694 }
695 free(tmpbuf);
696 res = read_back(llp->pipe[0], mbuf, now_len);
697 if (res != 0) {
698 free(mbuf);
699 goto clear_pipe;
700 }
701 len = now_len + extra_len;
702 iov[iov_count].iov_base = mbuf;
703 iov[iov_count].iov_len = len;
704 iov_count++;
705 res = fuse_send_msg(f, ch, iov, iov_count);
706 free(mbuf);
707 return res;
708 }
709 free(mbuf);
710 res = now_len;
711 }
712 len = res;
713 out->len = headerlen + len;
714
715 if (f->debug) {
716 fprintf(stderr,
717 " unique: %llu, success, outsize: %i (splice)\n",
718 (unsigned long long) out->unique, out->len);
719 }
720
721 splice_flags = 0;
722 if ((flags & FUSE_BUF_SPLICE_MOVE) &&
723 (f->conn.want & FUSE_CAP_SPLICE_MOVE))
724 splice_flags |= SPLICE_F_MOVE;
725
726 res = splice(llp->pipe[0], NULL,
727 fuse_chan_fd(ch), NULL, out->len, splice_flags);
728 if (res == -1) {
729 res = -errno;
730 perror("fuse: splice from pipe");
731 goto clear_pipe;
732 }
733 if (res != out->len) {
734 res = -EIO;
735 fprintf(stderr, "fuse: short splice from pipe: %u/%u\n",
736 res, out->len);
737 goto clear_pipe;
738 }
739 return 0;
740
741clear_pipe:
742 fuse_ll_clear_pipe(f);
743 return res;
744
745fallback:
746 return fuse_send_data_iov_fallback(f, ch, iov, iov_count, buf, len);
747}
748#else
749static int fuse_send_data_iov(struct fuse_ll *f, struct fuse_chan *ch,
750 struct iovec *iov, int iov_count,
751 struct fuse_bufvec *buf, unsigned int flags)
752{
753 size_t len = fuse_buf_size(buf);
754 (void) flags;
755
756 return fuse_send_data_iov_fallback(f, ch, iov, iov_count, buf, len);
757}
758#endif
759
760int fuse_reply_data(fuse_req_t req, struct fuse_bufvec *bufv,
761 enum fuse_buf_copy_flags flags)
762{
763 struct iovec iov[2];
764 struct fuse_out_header out;
765 int res;
766
767 iov[0].iov_base = &out;
768 iov[0].iov_len = sizeof(struct fuse_out_header);
769
770 out.unique = req->unique;
771 out.error = 0;
772
773 res = fuse_send_data_iov(req->f, req->ch, iov, 1, bufv, flags);
774 if (res <= 0) {
775 fuse_free_req(req);
776 return res;
777 } else {
778 return fuse_reply_err(req, res);
779 }
780}
781
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500782int fuse_reply_statfs(fuse_req_t req, const struct statvfs *stbuf)
783{
784 struct fuse_statfs_out arg;
785 size_t size = req->f->conn.proto_minor < 4 ?
786 FUSE_COMPAT_STATFS_SIZE : sizeof(arg);
787
788 memset(&arg, 0, sizeof(arg));
789 convert_statfs(stbuf, &arg.st);
790
791 return send_reply_ok(req, &arg, size);
792}
793
794int fuse_reply_xattr(fuse_req_t req, size_t count)
795{
796 struct fuse_getxattr_out arg;
797
798 memset(&arg, 0, sizeof(arg));
799 arg.size = count;
800
801 return send_reply_ok(req, &arg, sizeof(arg));
802}
803
Dees_Troye34c1332013-02-06 19:13:00 +0000804int fuse_reply_lock(fuse_req_t req, const struct flock *lock)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500805{
806 struct fuse_lk_out arg;
807
808 memset(&arg, 0, sizeof(arg));
809 arg.lk.type = lock->l_type;
810 if (lock->l_type != F_UNLCK) {
811 arg.lk.start = lock->l_start;
812 if (lock->l_len == 0)
813 arg.lk.end = OFFSET_MAX;
814 else
815 arg.lk.end = lock->l_start + lock->l_len - 1;
816 }
817 arg.lk.pid = lock->l_pid;
818 return send_reply_ok(req, &arg, sizeof(arg));
819}
820
821int fuse_reply_bmap(fuse_req_t req, uint64_t idx)
822{
823 struct fuse_bmap_out arg;
824
825 memset(&arg, 0, sizeof(arg));
826 arg.block = idx;
827
828 return send_reply_ok(req, &arg, sizeof(arg));
829}
830
Dees_Troye34c1332013-02-06 19:13:00 +0000831static struct fuse_ioctl_iovec *fuse_ioctl_iovec_copy(const struct iovec *iov,
832 size_t count)
833{
834 struct fuse_ioctl_iovec *fiov;
835 size_t i;
836
837 fiov = malloc(sizeof(fiov[0]) * count);
838 if (!fiov)
839 return NULL;
840
841 for (i = 0; i < count; i++) {
842 fiov[i].base = (uintptr_t) iov[i].iov_base;
843 fiov[i].len = iov[i].iov_len;
844 }
845
846 return fiov;
847}
848
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500849int fuse_reply_ioctl_retry(fuse_req_t req,
850 const struct iovec *in_iov, size_t in_count,
851 const struct iovec *out_iov, size_t out_count)
852{
853 struct fuse_ioctl_out arg;
Dees_Troye34c1332013-02-06 19:13:00 +0000854 struct fuse_ioctl_iovec *in_fiov = NULL;
855 struct fuse_ioctl_iovec *out_fiov = NULL;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500856 struct iovec iov[4];
857 size_t count = 1;
Dees_Troye34c1332013-02-06 19:13:00 +0000858 int res;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500859
860 memset(&arg, 0, sizeof(arg));
861 arg.flags |= FUSE_IOCTL_RETRY;
862 arg.in_iovs = in_count;
863 arg.out_iovs = out_count;
864 iov[count].iov_base = &arg;
865 iov[count].iov_len = sizeof(arg);
866 count++;
867
Dees_Troye34c1332013-02-06 19:13:00 +0000868 if (req->f->conn.proto_minor < 16) {
869 if (in_count) {
870 iov[count].iov_base = (void *)in_iov;
871 iov[count].iov_len = sizeof(in_iov[0]) * in_count;
872 count++;
873 }
874
875 if (out_count) {
876 iov[count].iov_base = (void *)out_iov;
877 iov[count].iov_len = sizeof(out_iov[0]) * out_count;
878 count++;
879 }
880 } else {
881 /* Can't handle non-compat 64bit ioctls on 32bit */
882 if (sizeof(void *) == 4 && req->ioctl_64bit) {
883 res = fuse_reply_err(req, EINVAL);
884 goto out;
885 }
886
887 if (in_count) {
888 in_fiov = fuse_ioctl_iovec_copy(in_iov, in_count);
889 if (!in_fiov)
890 goto enomem;
891
892 iov[count].iov_base = (void *)in_fiov;
893 iov[count].iov_len = sizeof(in_fiov[0]) * in_count;
894 count++;
895 }
896 if (out_count) {
897 out_fiov = fuse_ioctl_iovec_copy(out_iov, out_count);
898 if (!out_fiov)
899 goto enomem;
900
901 iov[count].iov_base = (void *)out_fiov;
902 iov[count].iov_len = sizeof(out_fiov[0]) * out_count;
903 count++;
904 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500905 }
906
Dees_Troye34c1332013-02-06 19:13:00 +0000907 res = send_reply_iov(req, 0, iov, count);
908out:
909 free(in_fiov);
910 free(out_fiov);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500911
Dees_Troye34c1332013-02-06 19:13:00 +0000912 return res;
913
914enomem:
915 res = fuse_reply_err(req, ENOMEM);
916 goto out;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500917}
918
919int fuse_reply_ioctl(fuse_req_t req, int result, const void *buf, size_t size)
920{
921 struct fuse_ioctl_out arg;
922 struct iovec iov[3];
923 size_t count = 1;
924
925 memset(&arg, 0, sizeof(arg));
926 arg.result = result;
927 iov[count].iov_base = &arg;
928 iov[count].iov_len = sizeof(arg);
929 count++;
930
931 if (size) {
932 iov[count].iov_base = (char *) buf;
933 iov[count].iov_len = size;
934 count++;
935 }
936
937 return send_reply_iov(req, 0, iov, count);
938}
939
940int fuse_reply_ioctl_iov(fuse_req_t req, int result, const struct iovec *iov,
941 int count)
942{
943 struct iovec *padded_iov;
944 struct fuse_ioctl_out arg;
945 int res;
946
947 padded_iov = malloc((count + 2) * sizeof(struct iovec));
Dees_Troye34c1332013-02-06 19:13:00 +0000948 if (padded_iov == NULL)
949 return fuse_reply_err(req, ENOMEM);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500950
951 memset(&arg, 0, sizeof(arg));
952 arg.result = result;
953 padded_iov[1].iov_base = &arg;
954 padded_iov[1].iov_len = sizeof(arg);
955
956 memcpy(&padded_iov[2], iov, count * sizeof(struct iovec));
957
958 res = send_reply_iov(req, 0, padded_iov, count + 2);
959 free(padded_iov);
960
961 return res;
962}
963
964int fuse_reply_poll(fuse_req_t req, unsigned revents)
965{
966 struct fuse_poll_out arg;
967
968 memset(&arg, 0, sizeof(arg));
969 arg.revents = revents;
970
971 return send_reply_ok(req, &arg, sizeof(arg));
972}
973
974static void do_lookup(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
975{
976 char *name = (char *) inarg;
977
978 if (req->f->op.lookup)
979 req->f->op.lookup(req, nodeid, name);
Dees_Troye34c1332013-02-06 19:13:00 +0000980 else
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500981 fuse_reply_err(req, ENOSYS);
982}
983
984static void do_forget(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
985{
986 struct fuse_forget_in *arg = (struct fuse_forget_in *) inarg;
987
988 if (req->f->op.forget)
989 req->f->op.forget(req, nodeid, arg->nlookup);
990 else
991 fuse_reply_none(req);
992}
993
Dees_Troye34c1332013-02-06 19:13:00 +0000994static void do_batch_forget(fuse_req_t req, fuse_ino_t nodeid,
995 const void *inarg)
996{
997 struct fuse_batch_forget_in *arg = (void *) inarg;
998 struct fuse_forget_one *param = (void *) PARAM(arg);
999 unsigned int i;
1000
1001 (void) nodeid;
1002
1003 if (req->f->op.forget_multi) {
1004 req->f->op.forget_multi(req, arg->count,
1005 (struct fuse_forget_data *) param);
1006 } else if (req->f->op.forget) {
1007 for (i = 0; i < arg->count; i++) {
1008 struct fuse_forget_one *forget = &param[i];
1009 struct fuse_req *dummy_req;
1010
1011 dummy_req = fuse_ll_alloc_req(req->f);
1012 if (dummy_req == NULL)
1013 break;
1014
1015 dummy_req->unique = req->unique;
1016 dummy_req->ctx = req->ctx;
1017 dummy_req->ch = NULL;
1018
1019 req->f->op.forget(dummy_req, forget->nodeid,
1020 forget->nlookup);
1021 }
1022 fuse_reply_none(req);
1023 } else {
1024 fuse_reply_none(req);
1025 }
1026}
1027
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001028static void do_getattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1029{
1030 struct fuse_file_info *fip = NULL;
1031 struct fuse_file_info fi;
1032
1033 if (req->f->conn.proto_minor >= 9) {
1034 struct fuse_getattr_in *arg = (struct fuse_getattr_in *) inarg;
1035
1036 if (arg->getattr_flags & FUSE_GETATTR_FH) {
1037 memset(&fi, 0, sizeof(fi));
1038 fi.fh = arg->fh;
1039 fi.fh_old = fi.fh;
1040 fip = &fi;
1041 }
1042 }
1043
1044 if (req->f->op.getattr)
1045 req->f->op.getattr(req, nodeid, fip);
Dees_Troye34c1332013-02-06 19:13:00 +00001046 else
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001047 fuse_reply_err(req, ENOSYS);
1048}
1049
1050static void do_setattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1051{
1052 struct fuse_setattr_in *arg = (struct fuse_setattr_in *) inarg;
1053
1054 if (req->f->op.setattr) {
1055 struct fuse_file_info *fi = NULL;
1056 struct fuse_file_info fi_store;
1057 struct stat stbuf;
1058 memset(&stbuf, 0, sizeof(stbuf));
1059 convert_attr(arg, &stbuf);
1060 if (arg->valid & FATTR_FH) {
1061 arg->valid &= ~FATTR_FH;
1062 memset(&fi_store, 0, sizeof(fi_store));
1063 fi = &fi_store;
1064 fi->fh = arg->fh;
1065 fi->fh_old = fi->fh;
1066 }
1067 arg->valid &=
1068 FUSE_SET_ATTR_MODE |
1069 FUSE_SET_ATTR_UID |
1070 FUSE_SET_ATTR_GID |
1071 FUSE_SET_ATTR_SIZE |
1072 FUSE_SET_ATTR_ATIME |
1073 FUSE_SET_ATTR_MTIME |
1074 FUSE_SET_ATTR_ATIME_NOW |
1075 FUSE_SET_ATTR_MTIME_NOW;
1076
1077 req->f->op.setattr(req, nodeid, &stbuf, arg->valid, fi);
Dees_Troye34c1332013-02-06 19:13:00 +00001078 } else
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001079 fuse_reply_err(req, ENOSYS);
1080}
1081
1082static void do_access(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1083{
1084 struct fuse_access_in *arg = (struct fuse_access_in *) inarg;
1085
1086 if (req->f->op.access)
1087 req->f->op.access(req, nodeid, arg->mask);
Dees_Troye34c1332013-02-06 19:13:00 +00001088 else
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001089 fuse_reply_err(req, ENOSYS);
1090}
1091
1092static void do_readlink(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1093{
1094 (void) inarg;
1095
1096 if (req->f->op.readlink)
1097 req->f->op.readlink(req, nodeid);
Dees_Troye34c1332013-02-06 19:13:00 +00001098 else
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001099 fuse_reply_err(req, ENOSYS);
1100}
1101
1102static void do_mknod(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1103{
1104 struct fuse_mknod_in *arg = (struct fuse_mknod_in *) inarg;
1105 char *name = PARAM(arg);
1106
1107 if (req->f->conn.proto_minor >= 12)
1108 req->ctx.umask = arg->umask;
1109 else
1110 name = (char *) inarg + FUSE_COMPAT_MKNOD_IN_SIZE;
1111
1112 if (req->f->op.mknod)
1113 req->f->op.mknod(req, nodeid, name, arg->mode, arg->rdev);
Dees_Troye34c1332013-02-06 19:13:00 +00001114 else
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001115 fuse_reply_err(req, ENOSYS);
1116}
1117
1118static void do_mkdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1119{
1120 struct fuse_mkdir_in *arg = (struct fuse_mkdir_in *) inarg;
1121
1122 if (req->f->conn.proto_minor >= 12)
1123 req->ctx.umask = arg->umask;
1124
1125 if (req->f->op.mkdir)
1126 req->f->op.mkdir(req, nodeid, PARAM(arg), arg->mode);
Dees_Troye34c1332013-02-06 19:13:00 +00001127 else
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001128 fuse_reply_err(req, ENOSYS);
1129}
1130
1131static void do_unlink(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1132{
1133 char *name = (char *) inarg;
1134
1135 if (req->f->op.unlink)
1136 req->f->op.unlink(req, nodeid, name);
Dees_Troye34c1332013-02-06 19:13:00 +00001137 else
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001138 fuse_reply_err(req, ENOSYS);
1139}
1140
1141static void do_rmdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1142{
1143 char *name = (char *) inarg;
1144
1145 if (req->f->op.rmdir)
1146 req->f->op.rmdir(req, nodeid, name);
Dees_Troye34c1332013-02-06 19:13:00 +00001147 else
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001148 fuse_reply_err(req, ENOSYS);
1149}
1150
1151static void do_symlink(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1152{
1153 char *name = (char *) inarg;
1154 char *linkname = ((char *) inarg) + strlen((char *) inarg) + 1;
1155
1156 if (req->f->op.symlink)
1157 req->f->op.symlink(req, linkname, nodeid, name);
Dees_Troye34c1332013-02-06 19:13:00 +00001158 else
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001159 fuse_reply_err(req, ENOSYS);
1160}
1161
1162static void do_rename(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1163{
1164 struct fuse_rename_in *arg = (struct fuse_rename_in *) inarg;
1165 char *oldname = PARAM(arg);
1166 char *newname = oldname + strlen(oldname) + 1;
1167
1168 if (req->f->op.rename)
1169 req->f->op.rename(req, nodeid, oldname, arg->newdir, newname);
Dees_Troye34c1332013-02-06 19:13:00 +00001170 else
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001171 fuse_reply_err(req, ENOSYS);
1172}
1173
1174static void do_link(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1175{
1176 struct fuse_link_in *arg = (struct fuse_link_in *) inarg;
1177
1178 if (req->f->op.link)
1179 req->f->op.link(req, arg->oldnodeid, nodeid, PARAM(arg));
Dees_Troye34c1332013-02-06 19:13:00 +00001180 else
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001181 fuse_reply_err(req, ENOSYS);
1182}
1183
1184static void do_create(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1185{
1186 struct fuse_create_in *arg = (struct fuse_create_in *) inarg;
1187
1188 if (req->f->op.create) {
1189 struct fuse_file_info fi;
1190 char *name = PARAM(arg);
1191
1192 memset(&fi, 0, sizeof(fi));
1193 fi.flags = arg->flags;
1194
1195 if (req->f->conn.proto_minor >= 12)
1196 req->ctx.umask = arg->umask;
1197 else
1198 name = (char *) inarg + sizeof(struct fuse_open_in);
1199
1200 req->f->op.create(req, nodeid, name, arg->mode, &fi);
Dees_Troye34c1332013-02-06 19:13:00 +00001201 } else
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001202 fuse_reply_err(req, ENOSYS);
1203}
1204
1205static void do_open(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1206{
1207 struct fuse_open_in *arg = (struct fuse_open_in *) inarg;
1208 struct fuse_file_info fi;
1209
1210 memset(&fi, 0, sizeof(fi));
1211 fi.flags = arg->flags;
1212
1213 if (req->f->op.open)
1214 req->f->op.open(req, nodeid, &fi);
1215 else
1216 fuse_reply_open(req, &fi);
1217}
1218
1219static void do_read(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1220{
1221 struct fuse_read_in *arg = (struct fuse_read_in *) inarg;
1222
1223 if (req->f->op.read) {
1224 struct fuse_file_info fi;
1225
1226 memset(&fi, 0, sizeof(fi));
1227 fi.fh = arg->fh;
1228 fi.fh_old = fi.fh;
1229 if (req->f->conn.proto_minor >= 9) {
1230 fi.lock_owner = arg->lock_owner;
1231 fi.flags = arg->flags;
1232 }
1233 req->f->op.read(req, nodeid, arg->size, arg->offset, &fi);
Dees_Troye34c1332013-02-06 19:13:00 +00001234 } else
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001235 fuse_reply_err(req, ENOSYS);
1236}
1237
1238static void do_write(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1239{
1240 struct fuse_write_in *arg = (struct fuse_write_in *) inarg;
1241 struct fuse_file_info fi;
1242 char *param;
1243
1244 memset(&fi, 0, sizeof(fi));
1245 fi.fh = arg->fh;
1246 fi.fh_old = fi.fh;
1247 fi.writepage = arg->write_flags & 1;
1248
1249 if (req->f->conn.proto_minor < 9) {
1250 param = ((char *) arg) + FUSE_COMPAT_WRITE_IN_SIZE;
1251 } else {
1252 fi.lock_owner = arg->lock_owner;
1253 fi.flags = arg->flags;
1254 param = PARAM(arg);
1255 }
1256
1257 if (req->f->op.write)
1258 req->f->op.write(req, nodeid, param, arg->size,
1259 arg->offset, &fi);
Dees_Troye34c1332013-02-06 19:13:00 +00001260 else
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001261 fuse_reply_err(req, ENOSYS);
Dees_Troye34c1332013-02-06 19:13:00 +00001262}
1263
1264static void do_write_buf(fuse_req_t req, fuse_ino_t nodeid, const void *inarg,
1265 const struct fuse_buf *ibuf)
1266{
1267 struct fuse_ll *f = req->f;
1268 struct fuse_bufvec bufv = {
1269 .buf[0] = *ibuf,
1270 .count = 1,
1271 };
1272 struct fuse_write_in *arg = (struct fuse_write_in *) inarg;
1273 struct fuse_file_info fi;
1274
1275 memset(&fi, 0, sizeof(fi));
1276 fi.fh = arg->fh;
1277 fi.fh_old = fi.fh;
1278 fi.writepage = arg->write_flags & 1;
1279
1280 if (req->f->conn.proto_minor < 9) {
1281 bufv.buf[0].mem = ((char *) arg) + FUSE_COMPAT_WRITE_IN_SIZE;
1282 bufv.buf[0].size -= sizeof(struct fuse_in_header) +
1283 FUSE_COMPAT_WRITE_IN_SIZE;
1284 assert(!(bufv.buf[0].flags & FUSE_BUF_IS_FD));
1285 } else {
1286 fi.lock_owner = arg->lock_owner;
1287 fi.flags = arg->flags;
1288 if (!(bufv.buf[0].flags & FUSE_BUF_IS_FD))
1289 bufv.buf[0].mem = PARAM(arg);
1290
1291 bufv.buf[0].size -= sizeof(struct fuse_in_header) +
1292 sizeof(struct fuse_write_in);
bigbiff bigbiff31f0e5f2013-01-19 10:23:42 -05001293 }
Dees_Troye34c1332013-02-06 19:13:00 +00001294 if (bufv.buf[0].size < arg->size) {
1295 fprintf(stderr, "fuse: do_write_buf: buffer size too small\n");
1296 fuse_reply_err(req, EIO);
1297 goto out;
1298 }
1299 bufv.buf[0].size = arg->size;
1300
1301 req->f->op.write_buf(req, nodeid, &bufv, arg->offset, &fi);
1302
1303out:
1304 /* Need to reset the pipe if ->write_buf() didn't consume all data */
1305 if ((ibuf->flags & FUSE_BUF_IS_FD) && bufv.idx < bufv.count)
1306 fuse_ll_clear_pipe(f);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001307}
1308
1309static void do_flush(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1310{
1311 struct fuse_flush_in *arg = (struct fuse_flush_in *) inarg;
1312 struct fuse_file_info fi;
1313
1314 memset(&fi, 0, sizeof(fi));
1315 fi.fh = arg->fh;
1316 fi.fh_old = fi.fh;
1317 fi.flush = 1;
1318 if (req->f->conn.proto_minor >= 7)
1319 fi.lock_owner = arg->lock_owner;
1320
1321 if (req->f->op.flush)
1322 req->f->op.flush(req, nodeid, &fi);
Dees_Troye34c1332013-02-06 19:13:00 +00001323 else
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001324 fuse_reply_err(req, ENOSYS);
1325}
1326
1327static void do_release(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1328{
1329 struct fuse_release_in *arg = (struct fuse_release_in *) inarg;
1330 struct fuse_file_info fi;
1331
1332 memset(&fi, 0, sizeof(fi));
1333 fi.flags = arg->flags;
1334 fi.fh = arg->fh;
1335 fi.fh_old = fi.fh;
1336 if (req->f->conn.proto_minor >= 8) {
1337 fi.flush = (arg->release_flags & FUSE_RELEASE_FLUSH) ? 1 : 0;
1338 fi.lock_owner = arg->lock_owner;
1339 }
Dees_Troye34c1332013-02-06 19:13:00 +00001340 if (arg->release_flags & FUSE_RELEASE_FLOCK_UNLOCK) {
1341 fi.flock_release = 1;
1342 fi.lock_owner = arg->lock_owner;
1343 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001344
1345 if (req->f->op.release)
1346 req->f->op.release(req, nodeid, &fi);
1347 else
1348 fuse_reply_err(req, 0);
1349}
1350
1351static void do_fsync(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1352{
1353 struct fuse_fsync_in *arg = (struct fuse_fsync_in *) inarg;
1354 struct fuse_file_info fi;
1355
1356 memset(&fi, 0, sizeof(fi));
1357 fi.fh = arg->fh;
1358 fi.fh_old = fi.fh;
1359
1360 if (req->f->op.fsync)
1361 req->f->op.fsync(req, nodeid, arg->fsync_flags & 1, &fi);
Dees_Troye34c1332013-02-06 19:13:00 +00001362 else
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001363 fuse_reply_err(req, ENOSYS);
1364}
1365
1366static void do_opendir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1367{
1368 struct fuse_open_in *arg = (struct fuse_open_in *) inarg;
1369 struct fuse_file_info fi;
1370
1371 memset(&fi, 0, sizeof(fi));
1372 fi.flags = arg->flags;
1373
1374 if (req->f->op.opendir)
1375 req->f->op.opendir(req, nodeid, &fi);
1376 else
1377 fuse_reply_open(req, &fi);
1378}
1379
1380static void do_readdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1381{
1382 struct fuse_read_in *arg = (struct fuse_read_in *) inarg;
1383 struct fuse_file_info fi;
1384
1385 memset(&fi, 0, sizeof(fi));
1386 fi.fh = arg->fh;
1387 fi.fh_old = fi.fh;
1388
1389 if (req->f->op.readdir)
1390 req->f->op.readdir(req, nodeid, arg->size, arg->offset, &fi);
Dees_Troye34c1332013-02-06 19:13:00 +00001391 else
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001392 fuse_reply_err(req, ENOSYS);
1393}
1394
1395static void do_releasedir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1396{
1397 struct fuse_release_in *arg = (struct fuse_release_in *) inarg;
1398 struct fuse_file_info fi;
1399
1400 memset(&fi, 0, sizeof(fi));
1401 fi.flags = arg->flags;
1402 fi.fh = arg->fh;
1403 fi.fh_old = fi.fh;
1404
1405 if (req->f->op.releasedir)
1406 req->f->op.releasedir(req, nodeid, &fi);
1407 else
1408 fuse_reply_err(req, 0);
1409}
1410
1411static void do_fsyncdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1412{
1413 struct fuse_fsync_in *arg = (struct fuse_fsync_in *) inarg;
1414 struct fuse_file_info fi;
1415
1416 memset(&fi, 0, sizeof(fi));
1417 fi.fh = arg->fh;
1418 fi.fh_old = fi.fh;
1419
1420 if (req->f->op.fsyncdir)
1421 req->f->op.fsyncdir(req, nodeid, arg->fsync_flags & 1, &fi);
Dees_Troye34c1332013-02-06 19:13:00 +00001422 else
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001423 fuse_reply_err(req, ENOSYS);
1424}
1425
1426static void do_statfs(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1427{
1428 (void) nodeid;
1429 (void) inarg;
1430
1431 if (req->f->op.statfs)
1432 req->f->op.statfs(req, nodeid);
1433 else {
1434 struct statvfs buf = {
1435 .f_namemax = 255,
1436 .f_bsize = 512,
1437 };
1438 fuse_reply_statfs(req, &buf);
1439 }
1440}
1441
1442static void do_setxattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1443{
1444 struct fuse_setxattr_in *arg = (struct fuse_setxattr_in *) inarg;
1445 char *name = PARAM(arg);
1446 char *value = name + strlen(name) + 1;
1447
1448 if (req->f->op.setxattr)
1449 req->f->op.setxattr(req, nodeid, name, value, arg->size,
1450 arg->flags);
Dees_Troye34c1332013-02-06 19:13:00 +00001451 else
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001452 fuse_reply_err(req, ENOSYS);
1453}
1454
1455static void do_getxattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1456{
1457 struct fuse_getxattr_in *arg = (struct fuse_getxattr_in *) inarg;
1458
1459 if (req->f->op.getxattr)
1460 req->f->op.getxattr(req, nodeid, PARAM(arg), arg->size);
Dees_Troye34c1332013-02-06 19:13:00 +00001461 else
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001462 fuse_reply_err(req, ENOSYS);
1463}
1464
1465static void do_listxattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1466{
1467 struct fuse_getxattr_in *arg = (struct fuse_getxattr_in *) inarg;
1468
1469 if (req->f->op.listxattr)
1470 req->f->op.listxattr(req, nodeid, arg->size);
Dees_Troye34c1332013-02-06 19:13:00 +00001471 else
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001472 fuse_reply_err(req, ENOSYS);
1473}
1474
1475static void do_removexattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1476{
1477 char *name = (char *) inarg;
1478
1479 if (req->f->op.removexattr)
1480 req->f->op.removexattr(req, nodeid, name);
Dees_Troye34c1332013-02-06 19:13:00 +00001481 else
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001482 fuse_reply_err(req, ENOSYS);
1483}
1484
1485static void convert_fuse_file_lock(struct fuse_file_lock *fl,
1486 struct flock *flock)
1487{
1488 memset(flock, 0, sizeof(struct flock));
1489 flock->l_type = fl->type;
1490 flock->l_whence = SEEK_SET;
1491 flock->l_start = fl->start;
1492 if (fl->end == OFFSET_MAX)
1493 flock->l_len = 0;
1494 else
1495 flock->l_len = fl->end - fl->start + 1;
1496 flock->l_pid = fl->pid;
1497}
1498
1499static void do_getlk(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1500{
1501 struct fuse_lk_in *arg = (struct fuse_lk_in *) inarg;
1502 struct fuse_file_info fi;
1503 struct flock flock;
1504
1505 memset(&fi, 0, sizeof(fi));
1506 fi.fh = arg->fh;
1507 fi.lock_owner = arg->owner;
1508
1509 convert_fuse_file_lock(&arg->lk, &flock);
1510 if (req->f->op.getlk)
1511 req->f->op.getlk(req, nodeid, &fi, &flock);
Dees_Troye34c1332013-02-06 19:13:00 +00001512 else
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001513 fuse_reply_err(req, ENOSYS);
1514}
1515
1516static void do_setlk_common(fuse_req_t req, fuse_ino_t nodeid,
1517 const void *inarg, int sleep)
1518{
1519 struct fuse_lk_in *arg = (struct fuse_lk_in *) inarg;
1520 struct fuse_file_info fi;
1521 struct flock flock;
1522
1523 memset(&fi, 0, sizeof(fi));
1524 fi.fh = arg->fh;
1525 fi.lock_owner = arg->owner;
1526
Dees_Troye34c1332013-02-06 19:13:00 +00001527 if (arg->lk_flags & FUSE_LK_FLOCK) {
1528 int op = 0;
1529
1530 switch (arg->lk.type) {
1531 case F_RDLCK:
1532 op = LOCK_SH;
1533 break;
1534 case F_WRLCK:
1535 op = LOCK_EX;
1536 break;
1537 case F_UNLCK:
1538 op = LOCK_UN;
1539 break;
1540 }
1541 if (!sleep)
1542 op |= LOCK_NB;
1543
1544 if (req->f->op.flock)
1545 req->f->op.flock(req, nodeid, &fi, op);
1546 else
1547 fuse_reply_err(req, ENOSYS);
1548 } else {
1549 convert_fuse_file_lock(&arg->lk, &flock);
1550 if (req->f->op.setlk)
1551 req->f->op.setlk(req, nodeid, &fi, &flock, sleep);
1552 else
1553 fuse_reply_err(req, ENOSYS);
bigbiff bigbiff31f0e5f2013-01-19 10:23:42 -05001554 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001555}
1556
1557static void do_setlk(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1558{
1559 do_setlk_common(req, nodeid, inarg, 0);
1560}
1561
1562static void do_setlkw(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1563{
1564 do_setlk_common(req, nodeid, inarg, 1);
1565}
1566
1567static int find_interrupted(struct fuse_ll *f, struct fuse_req *req)
1568{
1569 struct fuse_req *curr;
1570
1571 for (curr = f->list.next; curr != &f->list; curr = curr->next) {
1572 if (curr->unique == req->u.i.unique) {
1573 fuse_interrupt_func_t func;
1574 void *data;
1575
1576 curr->ctr++;
1577 pthread_mutex_unlock(&f->lock);
1578
1579 /* Ugh, ugly locking */
1580 pthread_mutex_lock(&curr->lock);
1581 pthread_mutex_lock(&f->lock);
1582 curr->interrupted = 1;
1583 func = curr->u.ni.func;
1584 data = curr->u.ni.data;
1585 pthread_mutex_unlock(&f->lock);
1586 if (func)
1587 func(curr, data);
1588 pthread_mutex_unlock(&curr->lock);
1589
1590 pthread_mutex_lock(&f->lock);
1591 curr->ctr--;
1592 if (!curr->ctr)
1593 destroy_req(curr);
1594
1595 return 1;
1596 }
1597 }
1598 for (curr = f->interrupts.next; curr != &f->interrupts;
1599 curr = curr->next) {
1600 if (curr->u.i.unique == req->u.i.unique)
1601 return 1;
1602 }
1603 return 0;
1604}
1605
1606static void do_interrupt(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1607{
1608 struct fuse_interrupt_in *arg = (struct fuse_interrupt_in *) inarg;
1609 struct fuse_ll *f = req->f;
1610
1611 (void) nodeid;
1612 if (f->debug)
1613 fprintf(stderr, "INTERRUPT: %llu\n",
1614 (unsigned long long) arg->unique);
1615
1616 req->u.i.unique = arg->unique;
1617
1618 pthread_mutex_lock(&f->lock);
1619 if (find_interrupted(f, req))
1620 destroy_req(req);
1621 else
1622 list_add_req(req, &f->interrupts);
1623 pthread_mutex_unlock(&f->lock);
1624}
1625
1626static struct fuse_req *check_interrupt(struct fuse_ll *f, struct fuse_req *req)
1627{
1628 struct fuse_req *curr;
1629
1630 for (curr = f->interrupts.next; curr != &f->interrupts;
1631 curr = curr->next) {
1632 if (curr->u.i.unique == req->unique) {
1633 req->interrupted = 1;
1634 list_del_req(curr);
1635 free(curr);
1636 return NULL;
1637 }
1638 }
1639 curr = f->interrupts.next;
1640 if (curr != &f->interrupts) {
1641 list_del_req(curr);
1642 list_init_req(curr);
1643 return curr;
1644 } else
1645 return NULL;
1646}
1647
1648static void do_bmap(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1649{
1650 struct fuse_bmap_in *arg = (struct fuse_bmap_in *) inarg;
1651
1652 if (req->f->op.bmap)
1653 req->f->op.bmap(req, nodeid, arg->blocksize, arg->block);
Dees_Troye34c1332013-02-06 19:13:00 +00001654 else
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001655 fuse_reply_err(req, ENOSYS);
1656}
1657
1658static void do_ioctl(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1659{
1660 struct fuse_ioctl_in *arg = (struct fuse_ioctl_in *) inarg;
1661 unsigned int flags = arg->flags;
1662 void *in_buf = arg->in_size ? PARAM(arg) : NULL;
1663 struct fuse_file_info fi;
1664
Dees_Troye34c1332013-02-06 19:13:00 +00001665 if (flags & FUSE_IOCTL_DIR &&
1666 !(req->f->conn.want & FUSE_CAP_IOCTL_DIR)) {
1667 fuse_reply_err(req, ENOTTY);
1668 return;
1669 }
1670
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001671 memset(&fi, 0, sizeof(fi));
1672 fi.fh = arg->fh;
1673 fi.fh_old = fi.fh;
1674
Dees_Troye34c1332013-02-06 19:13:00 +00001675 if (sizeof(void *) == 4 && req->f->conn.proto_minor >= 16 &&
1676 !(flags & FUSE_IOCTL_32BIT)) {
1677 req->ioctl_64bit = 1;
1678 }
1679
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001680 if (req->f->op.ioctl)
1681 req->f->op.ioctl(req, nodeid, arg->cmd,
1682 (void *)(uintptr_t)arg->arg, &fi, flags,
1683 in_buf, arg->in_size, arg->out_size);
Dees_Troye34c1332013-02-06 19:13:00 +00001684 else
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001685 fuse_reply_err(req, ENOSYS);
1686}
1687
1688void fuse_pollhandle_destroy(struct fuse_pollhandle *ph)
1689{
1690 free(ph);
1691}
1692
1693static void do_poll(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1694{
1695 struct fuse_poll_in *arg = (struct fuse_poll_in *) inarg;
1696 struct fuse_file_info fi;
1697
1698 memset(&fi, 0, sizeof(fi));
1699 fi.fh = arg->fh;
1700 fi.fh_old = fi.fh;
1701
1702 if (req->f->op.poll) {
1703 struct fuse_pollhandle *ph = NULL;
1704
1705 if (arg->flags & FUSE_POLL_SCHEDULE_NOTIFY) {
1706 ph = malloc(sizeof(struct fuse_pollhandle));
1707 if (ph == NULL) {
1708 fuse_reply_err(req, ENOMEM);
1709 return;
1710 }
1711 ph->kh = arg->kh;
1712 ph->ch = req->ch;
1713 ph->f = req->f;
1714 }
1715
1716 req->f->op.poll(req, nodeid, &fi, ph);
1717 } else {
1718 fuse_reply_err(req, ENOSYS);
1719 }
1720}
1721
Dees_Troye34c1332013-02-06 19:13:00 +00001722static void do_fallocate(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1723{
1724 struct fuse_fallocate_in *arg = (struct fuse_fallocate_in *) inarg;
1725 struct fuse_file_info fi;
1726
1727 memset(&fi, 0, sizeof(fi));
1728 fi.fh = arg->fh;
1729
1730 if (req->f->op.fallocate)
1731 req->f->op.fallocate(req, nodeid, arg->mode, arg->offset, arg->length, &fi);
1732 else
1733 fuse_reply_err(req, ENOSYS);
1734}
1735
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001736static void do_init(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1737{
1738 struct fuse_init_in *arg = (struct fuse_init_in *) inarg;
1739 struct fuse_init_out outarg;
1740 struct fuse_ll *f = req->f;
1741 size_t bufsize = fuse_chan_bufsize(req->ch);
1742
1743 (void) nodeid;
1744 if (f->debug) {
1745 fprintf(stderr, "INIT: %u.%u\n", arg->major, arg->minor);
1746 if (arg->major == 7 && arg->minor >= 6) {
1747 fprintf(stderr, "flags=0x%08x\n", arg->flags);
1748 fprintf(stderr, "max_readahead=0x%08x\n",
1749 arg->max_readahead);
1750 }
1751 }
1752 f->conn.proto_major = arg->major;
1753 f->conn.proto_minor = arg->minor;
1754 f->conn.capable = 0;
1755 f->conn.want = 0;
1756
1757 memset(&outarg, 0, sizeof(outarg));
1758 outarg.major = FUSE_KERNEL_VERSION;
1759 outarg.minor = FUSE_KERNEL_MINOR_VERSION;
1760
1761 if (arg->major < 7) {
1762 fprintf(stderr, "fuse: unsupported protocol version: %u.%u\n",
1763 arg->major, arg->minor);
1764 fuse_reply_err(req, EPROTO);
1765 return;
1766 }
1767
1768 if (arg->major > 7) {
1769 /* Wait for a second INIT request with a 7.X version */
1770 send_reply_ok(req, &outarg, sizeof(outarg));
1771 return;
1772 }
1773
1774 if (arg->minor >= 6) {
1775 if (f->conn.async_read)
1776 f->conn.async_read = arg->flags & FUSE_ASYNC_READ;
1777 if (arg->max_readahead < f->conn.max_readahead)
1778 f->conn.max_readahead = arg->max_readahead;
1779 if (arg->flags & FUSE_ASYNC_READ)
1780 f->conn.capable |= FUSE_CAP_ASYNC_READ;
1781 if (arg->flags & FUSE_POSIX_LOCKS)
1782 f->conn.capable |= FUSE_CAP_POSIX_LOCKS;
1783 if (arg->flags & FUSE_ATOMIC_O_TRUNC)
1784 f->conn.capable |= FUSE_CAP_ATOMIC_O_TRUNC;
1785 if (arg->flags & FUSE_EXPORT_SUPPORT)
1786 f->conn.capable |= FUSE_CAP_EXPORT_SUPPORT;
1787 if (arg->flags & FUSE_BIG_WRITES)
1788 f->conn.capable |= FUSE_CAP_BIG_WRITES;
1789 if (arg->flags & FUSE_DONT_MASK)
1790 f->conn.capable |= FUSE_CAP_DONT_MASK;
Dees_Troye34c1332013-02-06 19:13:00 +00001791 if (arg->flags & FUSE_FLOCK_LOCKS)
1792 f->conn.capable |= FUSE_CAP_FLOCK_LOCKS;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001793 } else {
1794 f->conn.async_read = 0;
1795 f->conn.max_readahead = 0;
1796 }
1797
Dees_Troye34c1332013-02-06 19:13:00 +00001798 if (req->f->conn.proto_minor >= 14) {
1799#ifdef HAVE_SPLICE
1800#ifdef HAVE_VMSPLICE
1801 f->conn.capable |= FUSE_CAP_SPLICE_WRITE | FUSE_CAP_SPLICE_MOVE;
1802 if (f->splice_write)
1803 f->conn.want |= FUSE_CAP_SPLICE_WRITE;
1804 if (f->splice_move)
1805 f->conn.want |= FUSE_CAP_SPLICE_MOVE;
1806#endif
1807 f->conn.capable |= FUSE_CAP_SPLICE_READ;
1808 if (f->splice_read)
1809 f->conn.want |= FUSE_CAP_SPLICE_READ;
1810#endif
1811 }
1812 if (req->f->conn.proto_minor >= 18)
1813 f->conn.capable |= FUSE_CAP_IOCTL_DIR;
1814
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001815 if (f->atomic_o_trunc)
1816 f->conn.want |= FUSE_CAP_ATOMIC_O_TRUNC;
Dees_Troye34c1332013-02-06 19:13:00 +00001817 if (f->op.getlk && f->op.setlk && !f->no_remote_posix_lock)
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001818 f->conn.want |= FUSE_CAP_POSIX_LOCKS;
Dees_Troye34c1332013-02-06 19:13:00 +00001819 if (f->op.flock && !f->no_remote_flock)
1820 f->conn.want |= FUSE_CAP_FLOCK_LOCKS;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001821 if (f->big_writes)
1822 f->conn.want |= FUSE_CAP_BIG_WRITES;
1823
1824 if (bufsize < FUSE_MIN_READ_BUFFER) {
1825 fprintf(stderr, "fuse: warning: buffer size too small: %zu\n",
1826 bufsize);
1827 bufsize = FUSE_MIN_READ_BUFFER;
1828 }
1829
1830 bufsize -= 4096;
1831 if (bufsize < f->conn.max_write)
1832 f->conn.max_write = bufsize;
1833
1834 f->got_init = 1;
1835 if (f->op.init)
1836 f->op.init(f->userdata, &f->conn);
1837
Dees_Troye34c1332013-02-06 19:13:00 +00001838 if (f->no_splice_read)
1839 f->conn.want &= ~FUSE_CAP_SPLICE_READ;
1840 if (f->no_splice_write)
1841 f->conn.want &= ~FUSE_CAP_SPLICE_WRITE;
1842 if (f->no_splice_move)
1843 f->conn.want &= ~FUSE_CAP_SPLICE_MOVE;
1844
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001845 if (f->conn.async_read || (f->conn.want & FUSE_CAP_ASYNC_READ))
1846 outarg.flags |= FUSE_ASYNC_READ;
1847 if (f->conn.want & FUSE_CAP_POSIX_LOCKS)
1848 outarg.flags |= FUSE_POSIX_LOCKS;
1849 if (f->conn.want & FUSE_CAP_ATOMIC_O_TRUNC)
1850 outarg.flags |= FUSE_ATOMIC_O_TRUNC;
1851 if (f->conn.want & FUSE_CAP_EXPORT_SUPPORT)
1852 outarg.flags |= FUSE_EXPORT_SUPPORT;
1853 if (f->conn.want & FUSE_CAP_BIG_WRITES)
1854 outarg.flags |= FUSE_BIG_WRITES;
1855 if (f->conn.want & FUSE_CAP_DONT_MASK)
1856 outarg.flags |= FUSE_DONT_MASK;
Dees_Troye34c1332013-02-06 19:13:00 +00001857 if (f->conn.want & FUSE_CAP_FLOCK_LOCKS)
1858 outarg.flags |= FUSE_FLOCK_LOCKS;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001859 outarg.max_readahead = f->conn.max_readahead;
1860 outarg.max_write = f->conn.max_write;
Dees_Troye34c1332013-02-06 19:13:00 +00001861 if (f->conn.proto_minor >= 13) {
1862 if (f->conn.max_background >= (1 << 16))
1863 f->conn.max_background = (1 << 16) - 1;
1864 if (f->conn.congestion_threshold > f->conn.max_background)
1865 f->conn.congestion_threshold = f->conn.max_background;
1866 if (!f->conn.congestion_threshold) {
1867 f->conn.congestion_threshold =
1868 f->conn.max_background * 3 / 4;
1869 }
1870
1871 outarg.max_background = f->conn.max_background;
1872 outarg.congestion_threshold = f->conn.congestion_threshold;
1873 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001874
1875 if (f->debug) {
1876 fprintf(stderr, " INIT: %u.%u\n", outarg.major, outarg.minor);
1877 fprintf(stderr, " flags=0x%08x\n", outarg.flags);
1878 fprintf(stderr, " max_readahead=0x%08x\n",
1879 outarg.max_readahead);
1880 fprintf(stderr, " max_write=0x%08x\n", outarg.max_write);
Dees_Troye34c1332013-02-06 19:13:00 +00001881 fprintf(stderr, " max_background=%i\n",
1882 outarg.max_background);
1883 fprintf(stderr, " congestion_threshold=%i\n",
1884 outarg.congestion_threshold);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001885 }
1886
1887 send_reply_ok(req, &outarg, arg->minor < 5 ? 8 : sizeof(outarg));
1888}
1889
1890static void do_destroy(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1891{
1892 struct fuse_ll *f = req->f;
1893
1894 (void) nodeid;
1895 (void) inarg;
1896
1897 f->got_destroy = 1;
1898 if (f->op.destroy)
1899 f->op.destroy(f->userdata);
1900
1901 send_reply_ok(req, NULL, 0);
1902}
1903
Dees_Troye34c1332013-02-06 19:13:00 +00001904static void list_del_nreq(struct fuse_notify_req *nreq)
1905{
1906 struct fuse_notify_req *prev = nreq->prev;
1907 struct fuse_notify_req *next = nreq->next;
1908 prev->next = next;
1909 next->prev = prev;
1910}
1911
1912static void list_add_nreq(struct fuse_notify_req *nreq,
1913 struct fuse_notify_req *next)
1914{
1915 struct fuse_notify_req *prev = next->prev;
1916 nreq->next = next;
1917 nreq->prev = prev;
1918 prev->next = nreq;
1919 next->prev = nreq;
1920}
1921
1922static void list_init_nreq(struct fuse_notify_req *nreq)
1923{
1924 nreq->next = nreq;
1925 nreq->prev = nreq;
1926}
1927
1928static void do_notify_reply(fuse_req_t req, fuse_ino_t nodeid,
1929 const void *inarg, const struct fuse_buf *buf)
1930{
1931 struct fuse_ll *f = req->f;
1932 struct fuse_notify_req *nreq;
1933 struct fuse_notify_req *head;
1934
1935 pthread_mutex_lock(&f->lock);
1936 head = &f->notify_list;
1937 for (nreq = head->next; nreq != head; nreq = nreq->next) {
1938 if (nreq->unique == req->unique) {
1939 list_del_nreq(nreq);
1940 break;
1941 }
1942 }
1943 pthread_mutex_unlock(&f->lock);
1944
1945 if (nreq != head)
1946 nreq->reply(nreq, req, nodeid, inarg, buf);
1947}
1948
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001949static int send_notify_iov(struct fuse_ll *f, struct fuse_chan *ch,
1950 int notify_code, struct iovec *iov, int count)
1951{
1952 struct fuse_out_header out;
1953
Dees_Troye34c1332013-02-06 19:13:00 +00001954 if (!f->got_init)
1955 return -ENOTCONN;
1956
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001957 out.unique = 0;
1958 out.error = notify_code;
1959 iov[0].iov_base = &out;
1960 iov[0].iov_len = sizeof(struct fuse_out_header);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001961
Dees_Troye34c1332013-02-06 19:13:00 +00001962 return fuse_send_msg(f, ch, iov, count);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001963}
1964
1965int fuse_lowlevel_notify_poll(struct fuse_pollhandle *ph)
1966{
1967 if (ph != NULL) {
1968 struct fuse_notify_poll_wakeup_out outarg;
1969 struct iovec iov[2];
1970
1971 outarg.kh = ph->kh;
1972
1973 iov[1].iov_base = &outarg;
1974 iov[1].iov_len = sizeof(outarg);
1975
1976 return send_notify_iov(ph->f, ph->ch, FUSE_NOTIFY_POLL, iov, 2);
1977 } else {
1978 return 0;
1979 }
1980}
1981
1982int fuse_lowlevel_notify_inval_inode(struct fuse_chan *ch, fuse_ino_t ino,
Matt Mower523a0592015-12-13 11:31:00 -06001983 loff_t off, loff_t len)
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001984{
1985 struct fuse_notify_inval_inode_out outarg;
1986 struct fuse_ll *f;
1987 struct iovec iov[2];
1988
1989 if (!ch)
1990 return -EINVAL;
1991
1992 f = (struct fuse_ll *)fuse_session_data(fuse_chan_session(ch));
1993 if (!f)
1994 return -ENODEV;
1995
1996 outarg.ino = ino;
1997 outarg.off = off;
1998 outarg.len = len;
1999
2000 iov[1].iov_base = &outarg;
2001 iov[1].iov_len = sizeof(outarg);
2002
2003 return send_notify_iov(f, ch, FUSE_NOTIFY_INVAL_INODE, iov, 2);
2004}
2005
2006int fuse_lowlevel_notify_inval_entry(struct fuse_chan *ch, fuse_ino_t parent,
2007 const char *name, size_t namelen)
2008{
2009 struct fuse_notify_inval_entry_out outarg;
2010 struct fuse_ll *f;
2011 struct iovec iov[3];
2012
2013 if (!ch)
2014 return -EINVAL;
2015
2016 f = (struct fuse_ll *)fuse_session_data(fuse_chan_session(ch));
2017 if (!f)
2018 return -ENODEV;
2019
2020 outarg.parent = parent;
2021 outarg.namelen = namelen;
2022 outarg.padding = 0;
2023
2024 iov[1].iov_base = &outarg;
2025 iov[1].iov_len = sizeof(outarg);
2026 iov[2].iov_base = (void *)name;
2027 iov[2].iov_len = namelen + 1;
2028
2029 return send_notify_iov(f, ch, FUSE_NOTIFY_INVAL_ENTRY, iov, 3);
2030}
2031
Dees_Troye34c1332013-02-06 19:13:00 +00002032int fuse_lowlevel_notify_delete(struct fuse_chan *ch,
2033 fuse_ino_t parent, fuse_ino_t child,
2034 const char *name, size_t namelen)
2035{
2036 struct fuse_notify_delete_out outarg;
2037 struct fuse_ll *f;
2038 struct iovec iov[3];
2039
2040 if (!ch)
2041 return -EINVAL;
2042
2043 f = (struct fuse_ll *)fuse_session_data(fuse_chan_session(ch));
2044 if (!f)
2045 return -ENODEV;
2046
2047 if (f->conn.proto_minor < 18)
2048 return -ENOSYS;
2049
2050 outarg.parent = parent;
2051 outarg.child = child;
2052 outarg.namelen = namelen;
2053 outarg.padding = 0;
2054
2055 iov[1].iov_base = &outarg;
2056 iov[1].iov_len = sizeof(outarg);
2057 iov[2].iov_base = (void *)name;
2058 iov[2].iov_len = namelen + 1;
2059
2060 return send_notify_iov(f, ch, FUSE_NOTIFY_DELETE, iov, 3);
2061}
2062
2063int fuse_lowlevel_notify_store(struct fuse_chan *ch, fuse_ino_t ino,
Matt Mower523a0592015-12-13 11:31:00 -06002064 loff_t offset, struct fuse_bufvec *bufv,
Dees_Troye34c1332013-02-06 19:13:00 +00002065 enum fuse_buf_copy_flags flags)
2066{
2067 struct fuse_out_header out;
2068 struct fuse_notify_store_out outarg;
2069 struct fuse_ll *f;
2070 struct iovec iov[3];
2071 size_t size = fuse_buf_size(bufv);
2072 int res;
2073
2074 if (!ch)
2075 return -EINVAL;
2076
2077 f = (struct fuse_ll *)fuse_session_data(fuse_chan_session(ch));
2078 if (!f)
2079 return -ENODEV;
2080
2081 if (f->conn.proto_minor < 15)
2082 return -ENOSYS;
2083
2084 out.unique = 0;
2085 out.error = FUSE_NOTIFY_STORE;
2086
2087 outarg.nodeid = ino;
2088 outarg.offset = offset;
2089 outarg.size = size;
2090
2091 iov[0].iov_base = &out;
2092 iov[0].iov_len = sizeof(out);
2093 iov[1].iov_base = &outarg;
2094 iov[1].iov_len = sizeof(outarg);
2095
2096 res = fuse_send_data_iov(f, ch, iov, 2, bufv, flags);
2097 if (res > 0)
2098 res = -res;
2099
2100 return res;
2101}
2102
2103struct fuse_retrieve_req {
2104 struct fuse_notify_req nreq;
2105 void *cookie;
2106};
2107
2108static void fuse_ll_retrieve_reply(struct fuse_notify_req *nreq,
2109 fuse_req_t req, fuse_ino_t ino,
2110 const void *inarg,
2111 const struct fuse_buf *ibuf)
2112{
2113 struct fuse_ll *f = req->f;
2114 struct fuse_retrieve_req *rreq =
2115 container_of(nreq, struct fuse_retrieve_req, nreq);
2116 const struct fuse_notify_retrieve_in *arg = inarg;
2117 struct fuse_bufvec bufv = {
2118 .buf[0] = *ibuf,
2119 .count = 1,
2120 };
2121
2122 if (!(bufv.buf[0].flags & FUSE_BUF_IS_FD))
2123 bufv.buf[0].mem = PARAM(arg);
2124
2125 bufv.buf[0].size -= sizeof(struct fuse_in_header) +
2126 sizeof(struct fuse_notify_retrieve_in);
2127
2128 if (bufv.buf[0].size < arg->size) {
2129 fprintf(stderr, "fuse: retrieve reply: buffer size too small\n");
2130 fuse_reply_none(req);
2131 goto out;
2132 }
2133 bufv.buf[0].size = arg->size;
2134
2135 if (req->f->op.retrieve_reply) {
2136 req->f->op.retrieve_reply(req, rreq->cookie, ino,
2137 arg->offset, &bufv);
2138 } else {
2139 fuse_reply_none(req);
2140 }
2141out:
2142 free(rreq);
2143 if ((ibuf->flags & FUSE_BUF_IS_FD) && bufv.idx < bufv.count)
2144 fuse_ll_clear_pipe(f);
2145}
2146
2147int fuse_lowlevel_notify_retrieve(struct fuse_chan *ch, fuse_ino_t ino,
Matt Mower523a0592015-12-13 11:31:00 -06002148 size_t size, loff_t offset, void *cookie)
Dees_Troye34c1332013-02-06 19:13:00 +00002149{
2150 struct fuse_notify_retrieve_out outarg;
2151 struct fuse_ll *f;
2152 struct iovec iov[2];
2153 struct fuse_retrieve_req *rreq;
2154 int err;
2155
2156 if (!ch)
2157 return -EINVAL;
2158
2159 f = (struct fuse_ll *)fuse_session_data(fuse_chan_session(ch));
2160 if (!f)
2161 return -ENODEV;
2162
2163 if (f->conn.proto_minor < 15)
2164 return -ENOSYS;
2165
2166 rreq = malloc(sizeof(*rreq));
2167 if (rreq == NULL)
2168 return -ENOMEM;
2169
2170 pthread_mutex_lock(&f->lock);
2171 rreq->cookie = cookie;
2172 rreq->nreq.unique = f->notify_ctr++;
2173 rreq->nreq.reply = fuse_ll_retrieve_reply;
2174 list_add_nreq(&rreq->nreq, &f->notify_list);
2175 pthread_mutex_unlock(&f->lock);
2176
2177 outarg.notify_unique = rreq->nreq.unique;
2178 outarg.nodeid = ino;
2179 outarg.offset = offset;
2180 outarg.size = size;
2181
2182 iov[1].iov_base = &outarg;
2183 iov[1].iov_len = sizeof(outarg);
2184
2185 err = send_notify_iov(f, ch, FUSE_NOTIFY_RETRIEVE, iov, 2);
2186 if (err) {
2187 pthread_mutex_lock(&f->lock);
2188 list_del_nreq(&rreq->nreq);
2189 pthread_mutex_unlock(&f->lock);
2190 free(rreq);
2191 }
2192
2193 return err;
2194}
2195
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002196void *fuse_req_userdata(fuse_req_t req)
2197{
2198 return req->f->userdata;
2199}
2200
2201const struct fuse_ctx *fuse_req_ctx(fuse_req_t req)
2202{
2203 return &req->ctx;
2204}
2205
2206/*
2207 * The size of fuse_ctx got extended, so need to be careful about
2208 * incompatibility (i.e. a new binary cannot work with an old
2209 * library).
2210 */
2211const struct fuse_ctx *fuse_req_ctx_compat24(fuse_req_t req);
2212const struct fuse_ctx *fuse_req_ctx_compat24(fuse_req_t req)
2213{
2214 return fuse_req_ctx(req);
2215}
Dees_Troye34c1332013-02-06 19:13:00 +00002216#ifndef __NetBSD__
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002217FUSE_SYMVER(".symver fuse_req_ctx_compat24,fuse_req_ctx@FUSE_2.4");
Dees_Troye34c1332013-02-06 19:13:00 +00002218#endif
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002219
2220
2221void fuse_req_interrupt_func(fuse_req_t req, fuse_interrupt_func_t func,
2222 void *data)
2223{
2224 pthread_mutex_lock(&req->lock);
2225 pthread_mutex_lock(&req->f->lock);
2226 req->u.ni.func = func;
2227 req->u.ni.data = data;
2228 pthread_mutex_unlock(&req->f->lock);
2229 if (req->interrupted && func)
2230 func(req, data);
2231 pthread_mutex_unlock(&req->lock);
2232}
2233
2234int fuse_req_interrupted(fuse_req_t req)
2235{
2236 int interrupted;
2237
2238 pthread_mutex_lock(&req->f->lock);
2239 interrupted = req->interrupted;
2240 pthread_mutex_unlock(&req->f->lock);
2241
2242 return interrupted;
2243}
2244
2245static struct {
2246 void (*func)(fuse_req_t, fuse_ino_t, const void *);
2247 const char *name;
2248} fuse_ll_ops[] = {
2249 [FUSE_LOOKUP] = { do_lookup, "LOOKUP" },
2250 [FUSE_FORGET] = { do_forget, "FORGET" },
2251 [FUSE_GETATTR] = { do_getattr, "GETATTR" },
2252 [FUSE_SETATTR] = { do_setattr, "SETATTR" },
2253 [FUSE_READLINK] = { do_readlink, "READLINK" },
2254 [FUSE_SYMLINK] = { do_symlink, "SYMLINK" },
2255 [FUSE_MKNOD] = { do_mknod, "MKNOD" },
2256 [FUSE_MKDIR] = { do_mkdir, "MKDIR" },
2257 [FUSE_UNLINK] = { do_unlink, "UNLINK" },
2258 [FUSE_RMDIR] = { do_rmdir, "RMDIR" },
2259 [FUSE_RENAME] = { do_rename, "RENAME" },
2260 [FUSE_LINK] = { do_link, "LINK" },
2261 [FUSE_OPEN] = { do_open, "OPEN" },
2262 [FUSE_READ] = { do_read, "READ" },
2263 [FUSE_WRITE] = { do_write, "WRITE" },
2264 [FUSE_STATFS] = { do_statfs, "STATFS" },
2265 [FUSE_RELEASE] = { do_release, "RELEASE" },
2266 [FUSE_FSYNC] = { do_fsync, "FSYNC" },
2267 [FUSE_SETXATTR] = { do_setxattr, "SETXATTR" },
2268 [FUSE_GETXATTR] = { do_getxattr, "GETXATTR" },
2269 [FUSE_LISTXATTR] = { do_listxattr, "LISTXATTR" },
2270 [FUSE_REMOVEXATTR] = { do_removexattr, "REMOVEXATTR" },
2271 [FUSE_FLUSH] = { do_flush, "FLUSH" },
2272 [FUSE_INIT] = { do_init, "INIT" },
2273 [FUSE_OPENDIR] = { do_opendir, "OPENDIR" },
2274 [FUSE_READDIR] = { do_readdir, "READDIR" },
2275 [FUSE_RELEASEDIR] = { do_releasedir, "RELEASEDIR" },
2276 [FUSE_FSYNCDIR] = { do_fsyncdir, "FSYNCDIR" },
2277 [FUSE_GETLK] = { do_getlk, "GETLK" },
2278 [FUSE_SETLK] = { do_setlk, "SETLK" },
2279 [FUSE_SETLKW] = { do_setlkw, "SETLKW" },
2280 [FUSE_ACCESS] = { do_access, "ACCESS" },
2281 [FUSE_CREATE] = { do_create, "CREATE" },
2282 [FUSE_INTERRUPT] = { do_interrupt, "INTERRUPT" },
2283 [FUSE_BMAP] = { do_bmap, "BMAP" },
2284 [FUSE_IOCTL] = { do_ioctl, "IOCTL" },
2285 [FUSE_POLL] = { do_poll, "POLL" },
Dees_Troye34c1332013-02-06 19:13:00 +00002286 [FUSE_FALLOCATE] = { do_fallocate, "FALLOCATE" },
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002287 [FUSE_DESTROY] = { do_destroy, "DESTROY" },
Dees_Troye34c1332013-02-06 19:13:00 +00002288 [FUSE_NOTIFY_REPLY] = { (void *) 1, "NOTIFY_REPLY" },
2289 [FUSE_BATCH_FORGET] = { do_batch_forget, "BATCH_FORGET" },
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002290 [CUSE_INIT] = { cuse_lowlevel_init, "CUSE_INIT" },
2291};
2292
2293#define FUSE_MAXOP (sizeof(fuse_ll_ops) / sizeof(fuse_ll_ops[0]))
2294
2295static const char *opname(enum fuse_opcode opcode)
2296{
2297 if (opcode >= FUSE_MAXOP || !fuse_ll_ops[opcode].name)
2298 return "???";
2299 else
2300 return fuse_ll_ops[opcode].name;
2301}
2302
Dees_Troye34c1332013-02-06 19:13:00 +00002303static int fuse_ll_copy_from_pipe(struct fuse_bufvec *dst,
2304 struct fuse_bufvec *src)
2305{
2306 int res = fuse_buf_copy(dst, src, 0);
2307 if (res < 0) {
2308 fprintf(stderr, "fuse: copy from pipe: %s\n", strerror(-res));
2309 return res;
2310 }
2311 if (res < fuse_buf_size(dst)) {
2312 fprintf(stderr, "fuse: copy from pipe: short read\n");
2313 return -1;
2314 }
2315 return 0;
2316}
2317
2318static void fuse_ll_process_buf(void *data, const struct fuse_buf *buf,
2319 struct fuse_chan *ch)
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002320{
2321 struct fuse_ll *f = (struct fuse_ll *) data;
Dees_Troye34c1332013-02-06 19:13:00 +00002322 const size_t write_header_size = sizeof(struct fuse_in_header) +
2323 sizeof(struct fuse_write_in);
2324 struct fuse_bufvec bufv = { .buf[0] = *buf, .count = 1 };
2325 struct fuse_bufvec tmpbuf = FUSE_BUFVEC_INIT(write_header_size);
2326 struct fuse_in_header *in;
2327 const void *inarg;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002328 struct fuse_req *req;
Dees_Troye34c1332013-02-06 19:13:00 +00002329 void *mbuf = NULL;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002330 int err;
Dees_Troye34c1332013-02-06 19:13:00 +00002331 int res;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002332
Dees_Troye34c1332013-02-06 19:13:00 +00002333 if (buf->flags & FUSE_BUF_IS_FD) {
2334 if (buf->size < tmpbuf.buf[0].size)
2335 tmpbuf.buf[0].size = buf->size;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002336
Dees_Troye34c1332013-02-06 19:13:00 +00002337 mbuf = malloc(tmpbuf.buf[0].size);
2338 if (mbuf == NULL) {
2339 fprintf(stderr, "fuse: failed to allocate header\n");
2340 goto clear_pipe;
2341 }
2342 tmpbuf.buf[0].mem = mbuf;
2343
2344 res = fuse_ll_copy_from_pipe(&tmpbuf, &bufv);
2345 if (res < 0)
2346 goto clear_pipe;
2347
2348 in = mbuf;
2349 } else {
2350 in = buf->mem;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002351 }
2352
Dees_Troye34c1332013-02-06 19:13:00 +00002353 if (f->debug) {
2354 fprintf(stderr,
2355 "unique: %llu, opcode: %s (%i), nodeid: %lu, insize: %zu, pid: %u\n",
2356 (unsigned long long) in->unique,
2357 opname((enum fuse_opcode) in->opcode), in->opcode,
2358 (unsigned long) in->nodeid, buf->size, in->pid);
2359 }
2360
2361 req = fuse_ll_alloc_req(f);
2362 if