blob: 103f8315480c9a91aa984000c22f6aa37906896c [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>
27
28#ifndef F_LINUX_SPECIFIC_BASE
29#define F_LINUX_SPECIFIC_BASE 1024
30#endif
31#ifndef F_SETPIPE_SZ
32#define F_SETPIPE_SZ (F_LINUX_SPECIFIC_BASE + 7)
33#endif
34
bigbiff bigbiff9c754052013-01-09 09:09:08 -050035
36#define PARAM(inarg) (((char *)(inarg)) + sizeof(*(inarg)))
37#define OFFSET_MAX 0x7fffffffffffffffLL
38
Dees_Troye34c1332013-02-06 19:13:00 +000039#define container_of(ptr, type, member) ({ \
40 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
41 (type *)( (char *)__mptr - offsetof(type,member) );})
42
bigbiff bigbiff9c754052013-01-09 09:09:08 -050043struct fuse_pollhandle {
44 uint64_t kh;
45 struct fuse_chan *ch;
46 struct fuse_ll *f;
47};
48
Dees_Troye34c1332013-02-06 19:13:00 +000049static size_t pagesize;
50
51static __attribute__((constructor)) void fuse_ll_init_pagesize(void)
52{
53 pagesize = getpagesize();
54}
55
bigbiff bigbiff9c754052013-01-09 09:09:08 -050056static void convert_stat(const struct stat *stbuf, struct fuse_attr *attr)
57{
58 attr->ino = stbuf->st_ino;
59 attr->mode = stbuf->st_mode;
60 attr->nlink = stbuf->st_nlink;
61 attr->uid = stbuf->st_uid;
62 attr->gid = stbuf->st_gid;
63 attr->rdev = stbuf->st_rdev;
64 attr->size = stbuf->st_size;
65 attr->blksize = stbuf->st_blksize;
66 attr->blocks = stbuf->st_blocks;
67 attr->atime = stbuf->st_atime;
68 attr->mtime = stbuf->st_mtime;
69 attr->ctime = stbuf->st_ctime;
70 attr->atimensec = ST_ATIM_NSEC(stbuf);
71 attr->mtimensec = ST_MTIM_NSEC(stbuf);
72 attr->ctimensec = ST_CTIM_NSEC(stbuf);
73}
74
75static void convert_attr(const struct fuse_setattr_in *attr, struct stat *stbuf)
76{
77 stbuf->st_mode = attr->mode;
78 stbuf->st_uid = attr->uid;
79 stbuf->st_gid = attr->gid;
80 stbuf->st_size = attr->size;
81 stbuf->st_atime = attr->atime;
82 stbuf->st_mtime = attr->mtime;
83 ST_ATIM_NSEC_SET(stbuf, attr->atimensec);
84 ST_MTIM_NSEC_SET(stbuf, attr->mtimensec);
85}
86
87static size_t iov_length(const struct iovec *iov, size_t count)
88{
89 size_t seg;
90 size_t ret = 0;
91
92 for (seg = 0; seg < count; seg++)
93 ret += iov[seg].iov_len;
94 return ret;
95}
96
97static void list_init_req(struct fuse_req *req)
98{
99 req->next = req;
100 req->prev = req;
101}
102
103static void list_del_req(struct fuse_req *req)
104{
105 struct fuse_req *prev = req->prev;
106 struct fuse_req *next = req->next;
107 prev->next = next;
108 next->prev = prev;
109}
110
111static void list_add_req(struct fuse_req *req, struct fuse_req *next)
112{
113 struct fuse_req *prev = next->prev;
114 req->next = next;
115 req->prev = prev;
116 prev->next = req;
117 next->prev = req;
118}
119
120static void destroy_req(fuse_req_t req)
121{
122 pthread_mutex_destroy(&req->lock);
123 free(req);
124}
125
126void fuse_free_req(fuse_req_t req)
127{
128 int ctr;
129 struct fuse_ll *f = req->f;
130
131 pthread_mutex_lock(&f->lock);
132 req->u.ni.func = NULL;
133 req->u.ni.data = NULL;
134 list_del_req(req);
135 ctr = --req->ctr;
136 pthread_mutex_unlock(&f->lock);
137 if (!ctr)
138 destroy_req(req);
139}
140
Dees_Troye34c1332013-02-06 19:13:00 +0000141static struct fuse_req *fuse_ll_alloc_req(struct fuse_ll *f)
142{
143 struct fuse_req *req;
144
145 req = (struct fuse_req *) calloc(1, sizeof(struct fuse_req));
146 if (req == NULL) {
147 fprintf(stderr, "fuse: failed to allocate request\n");
148 } else {
149 req->f = f;
150 req->ctr = 1;
151 list_init_req(req);
152 fuse_mutex_init(&req->lock);
153 }
154
155 return req;
156}
157
158
159static int fuse_send_msg(struct fuse_ll *f, struct fuse_chan *ch,
160 struct iovec *iov, int count)
161{
162 struct fuse_out_header *out = iov[0].iov_base;
163
164 out->len = iov_length(iov, count);
165 if (f->debug) {
166 if (out->unique == 0) {
167 fprintf(stderr, "NOTIFY: code=%d length=%u\n",
168 out->error, out->len);
169 } else if (out->error) {
170 fprintf(stderr,
171 " unique: %llu, error: %i (%s), outsize: %i\n",
172 (unsigned long long) out->unique, out->error,
173 strerror(-out->error), out->len);
174 } else {
175 fprintf(stderr,
176 " unique: %llu, success, outsize: %i\n",
177 (unsigned long long) out->unique, out->len);
178 }
179 }
180
181 return fuse_chan_send(ch, iov, count);
182}
183
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500184int fuse_send_reply_iov_nofree(fuse_req_t req, int error, struct iovec *iov,
185 int count)
186{
187 struct fuse_out_header out;
188
189 if (error <= -1000 || error > 0) {
190 fprintf(stderr, "fuse: bad error value: %i\n", error);
191 error = -ERANGE;
192 }
193
194 out.unique = req->unique;
195 out.error = error;
Dees_Troye34c1332013-02-06 19:13:00 +0000196
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500197 iov[0].iov_base = &out;
198 iov[0].iov_len = sizeof(struct fuse_out_header);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500199
Dees_Troye34c1332013-02-06 19:13:00 +0000200 return fuse_send_msg(req->f, req->ch, iov, count);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500201}
202
203static int send_reply_iov(fuse_req_t req, int error, struct iovec *iov,
204 int count)
205{
206 int res;
207
208 res = fuse_send_reply_iov_nofree(req, error, iov, count);
209 fuse_free_req(req);
210 return res;
211}
212
213static int send_reply(fuse_req_t req, int error, const void *arg,
214 size_t argsize)
215{
216 struct iovec iov[2];
217 int count = 1;
218 if (argsize) {
219 iov[1].iov_base = (void *) arg;
220 iov[1].iov_len = argsize;
221 count++;
222 }
223 return send_reply_iov(req, error, iov, count);
224}
225
226int fuse_reply_iov(fuse_req_t req, const struct iovec *iov, int count)
227{
228 int res;
229 struct iovec *padded_iov;
230
231 padded_iov = malloc((count + 1) * sizeof(struct iovec));
Dees_Troye34c1332013-02-06 19:13:00 +0000232 if (padded_iov == NULL)
233 return fuse_reply_err(req, ENOMEM);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500234
235 memcpy(padded_iov + 1, iov, count * sizeof(struct iovec));
236 count++;
237
238 res = send_reply_iov(req, 0, padded_iov, count);
239 free(padded_iov);
240
241 return res;
242}
243
244size_t fuse_dirent_size(size_t namelen)
245{
246 return FUSE_DIRENT_ALIGN(FUSE_NAME_OFFSET + namelen);
247}
248
249char *fuse_add_dirent(char *buf, const char *name, const struct stat *stbuf,
250 off64_t off)
251{
252 unsigned namelen = strlen(name);
253 unsigned entlen = FUSE_NAME_OFFSET + namelen;
254 unsigned entsize = fuse_dirent_size(namelen);
255 unsigned padlen = entsize - entlen;
256 struct fuse_dirent *dirent = (struct fuse_dirent *) buf;
257
258 dirent->ino = stbuf->st_ino;
259 dirent->off = off;
260 dirent->namelen = namelen;
261 dirent->type = (stbuf->st_mode & 0170000) >> 12;
262 strncpy(dirent->name, name, namelen);
263 if (padlen)
264 memset(buf + entlen, 0, padlen);
265
266 return buf + entsize;
267}
268
269size_t fuse_add_direntry(fuse_req_t req, char *buf, size_t bufsize,
270 const char *name, const struct stat *stbuf, off64_t off)
271{
272 size_t entsize;
273
274 (void) req;
275 entsize = fuse_dirent_size(strlen(name));
276 if (entsize <= bufsize && buf)
277 fuse_add_dirent(buf, name, stbuf, off);
278 return entsize;
279}
280
281static void convert_statfs(const struct statvfs *stbuf,
282 struct fuse_kstatfs *kstatfs)
283{
284 kstatfs->bsize = stbuf->f_bsize;
285 kstatfs->frsize = stbuf->f_frsize;
286 kstatfs->blocks = stbuf->f_blocks;
287 kstatfs->bfree = stbuf->f_bfree;
288 kstatfs->bavail = stbuf->f_bavail;
289 kstatfs->files = stbuf->f_files;
290 kstatfs->ffree = stbuf->f_ffree;
291 kstatfs->namelen = stbuf->f_namemax;
292}
293
294static int send_reply_ok(fuse_req_t req, const void *arg, size_t argsize)
295{
296 return send_reply(req, 0, arg, argsize);
297}
298
299int fuse_reply_err(fuse_req_t req, int err)
300{
301 return send_reply(req, -err, NULL, 0);
302}
303
304void fuse_reply_none(fuse_req_t req)
305{
Dees_Troye34c1332013-02-06 19:13:00 +0000306 if (req->ch)
307 fuse_chan_send(req->ch, NULL, 0);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500308 fuse_free_req(req);
309}
310
311static unsigned long calc_timeout_sec(double t)
312{
313 if (t > (double) ULONG_MAX)
314 return ULONG_MAX;
315 else if (t < 0.0)
316 return 0;
317 else
318 return (unsigned long) t;
319}
320
321static unsigned int calc_timeout_nsec(double t)
322{
323 double f = t - (double) calc_timeout_sec(t);
324 if (f < 0.0)
325 return 0;
326 else if (f >= 0.999999999)
327 return 999999999;
328 else
329 return (unsigned int) (f * 1.0e9);
330}
331
332static void fill_entry(struct fuse_entry_out *arg,
333 const struct fuse_entry_param *e)
334{
335 arg->nodeid = e->ino;
336 arg->generation = e->generation;
337 arg->entry_valid = calc_timeout_sec(e->entry_timeout);
338 arg->entry_valid_nsec = calc_timeout_nsec(e->entry_timeout);
339 arg->attr_valid = calc_timeout_sec(e->attr_timeout);
340 arg->attr_valid_nsec = calc_timeout_nsec(e->attr_timeout);
341 convert_stat(&e->attr, &arg->attr);
342}
343
344static void fill_open(struct fuse_open_out *arg,
345 const struct fuse_file_info *f)
346{
347 arg->fh = f->fh;
348 if (f->direct_io)
349 arg->open_flags |= FOPEN_DIRECT_IO;
350 if (f->keep_cache)
351 arg->open_flags |= FOPEN_KEEP_CACHE;
352 if (f->nonseekable)
353 arg->open_flags |= FOPEN_NONSEEKABLE;
354}
355
356int fuse_reply_entry(fuse_req_t req, const struct fuse_entry_param *e)
357{
358 struct fuse_entry_out arg;
359 size_t size = req->f->conn.proto_minor < 9 ?
360 FUSE_COMPAT_ENTRY_OUT_SIZE : sizeof(arg);
361
362 /* before ABI 7.4 e->ino == 0 was invalid, only ENOENT meant
363 negative entry */
Dees_Troye34c1332013-02-06 19:13:00 +0000364 if (!e->ino && req->f->conn.proto_minor < 4)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500365 return fuse_reply_err(req, ENOENT);
366
367 memset(&arg, 0, sizeof(arg));
368 fill_entry(&arg, e);
369 return send_reply_ok(req, &arg, size);
370}
371
372int fuse_reply_create(fuse_req_t req, const struct fuse_entry_param *e,
373 const struct fuse_file_info *f)
374{
375 char buf[sizeof(struct fuse_entry_out) + sizeof(struct fuse_open_out)];
376 size_t entrysize = req->f->conn.proto_minor < 9 ?
377 FUSE_COMPAT_ENTRY_OUT_SIZE : sizeof(struct fuse_entry_out);
378 struct fuse_entry_out *earg = (struct fuse_entry_out *) buf;
379 struct fuse_open_out *oarg = (struct fuse_open_out *) (buf + entrysize);
380
381 memset(buf, 0, sizeof(buf));
382 fill_entry(earg, e);
383 fill_open(oarg, f);
384 return send_reply_ok(req, buf,
385 entrysize + sizeof(struct fuse_open_out));
386}
387
388int fuse_reply_attr(fuse_req_t req, const struct stat *attr,
389 double attr_timeout)
390{
391 struct fuse_attr_out arg;
392 size_t size = req->f->conn.proto_minor < 9 ?
393 FUSE_COMPAT_ATTR_OUT_SIZE : sizeof(arg);
394
395 memset(&arg, 0, sizeof(arg));
396 arg.attr_valid = calc_timeout_sec(attr_timeout);
397 arg.attr_valid_nsec = calc_timeout_nsec(attr_timeout);
398 convert_stat(attr, &arg.attr);
399
400 return send_reply_ok(req, &arg, size);
401}
402
403int fuse_reply_readlink(fuse_req_t req, const char *linkname)
404{
405 return send_reply_ok(req, linkname, strlen(linkname));
406}
407
408int fuse_reply_open(fuse_req_t req, const struct fuse_file_info *f)
409{
410 struct fuse_open_out arg;
411
412 memset(&arg, 0, sizeof(arg));
413 fill_open(&arg, f);
414 return send_reply_ok(req, &arg, sizeof(arg));
415}
416
417int fuse_reply_write(fuse_req_t req, size_t count)
418{
419 struct fuse_write_out arg;
420
421 memset(&arg, 0, sizeof(arg));
422 arg.size = count;
423
424 return send_reply_ok(req, &arg, sizeof(arg));
425}
426
427int fuse_reply_buf(fuse_req_t req, const char *buf, size_t size)
428{
429 return send_reply_ok(req, buf, size);
430}
431
Dees_Troye34c1332013-02-06 19:13:00 +0000432static int fuse_send_data_iov_fallback(struct fuse_ll *f, struct fuse_chan *ch,
433 struct iovec *iov, int iov_count,
434 struct fuse_bufvec *buf,
435 size_t len)
436{
437 struct fuse_bufvec mem_buf = FUSE_BUFVEC_INIT(len);
438 void *mbuf;
439 int res;
440
441 /* Optimize common case */
442 if (buf->count == 1 && buf->idx == 0 && buf->off == 0 &&
443 !(buf->buf[0].flags & FUSE_BUF_IS_FD)) {
444 /* FIXME: also avoid memory copy if there are multiple buffers
445 but none of them contain an fd */
446
447 iov[iov_count].iov_base = buf->buf[0].mem;
448 iov[iov_count].iov_len = len;
449 iov_count++;
450 return fuse_send_msg(f, ch, iov, iov_count);
451 }
452
453 res = posix_memalign(&mbuf, pagesize, len);
454 if (res != 0)
455 return res;
456
457 mem_buf.buf[0].mem = mbuf;
458 res = fuse_buf_copy(&mem_buf, buf, 0);
459 if (res < 0) {
460 free(mbuf);
461 return -res;
462 }
463 len = res;
464
465 iov[iov_count].iov_base = mbuf;
466 iov[iov_count].iov_len = len;
467 iov_count++;
468 res = fuse_send_msg(f, ch, iov, iov_count);
469 free(mbuf);
470
471 return res;
472}
473
474struct fuse_ll_pipe {
475 size_t size;
476 int can_grow;
477 int pipe[2];
478};
479
480static void fuse_ll_pipe_free(struct fuse_ll_pipe *llp)
481{
482 close(llp->pipe[0]);
483 close(llp->pipe[1]);
484 free(llp);
485}
486
487#ifdef HAVE_SPLICE
488static struct fuse_ll_pipe *fuse_ll_get_pipe(struct fuse_ll *f)
489{
490 struct fuse_ll_pipe *llp = pthread_getspecific(f->pipe_key);
491 if (llp == NULL) {
492 int res;
493
494 llp = malloc(sizeof(struct fuse_ll_pipe));
495 if (llp == NULL)
496 return NULL;
497
498 res = pipe(llp->pipe);
499 if (res == -1) {
500 free(llp);
501 return NULL;
502 }
503
504 if (fcntl(llp->pipe[0], F_SETFL, O_NONBLOCK) == -1 ||
505 fcntl(llp->pipe[1], F_SETFL, O_NONBLOCK) == -1) {
506 close(llp->pipe[0]);
507 close(llp->pipe[1]);
508 free(llp);
509 return NULL;
510 }
511
512 /*
513 *the default size is 16 pages on linux
514 */
515 llp->size = pagesize * 16;
516 llp->can_grow = 1;
517
518 pthread_setspecific(f->pipe_key, llp);
519 }
520
521 return llp;
522}
523#endif
524
525static void fuse_ll_clear_pipe(struct fuse_ll *f)
526{
527 struct fuse_ll_pipe *llp = pthread_getspecific(f->pipe_key);
528 if (llp) {
529 pthread_setspecific(f->pipe_key, NULL);
530 fuse_ll_pipe_free(llp);
531 }
532}
533
534#if defined(HAVE_SPLICE) && defined(HAVE_VMSPLICE)
535static int read_back(int fd, char *buf, size_t len)
536{
537 int res;
538
539 res = read(fd, buf, len);
540 if (res == -1) {
541 fprintf(stderr, "fuse: internal error: failed to read back from pipe: %s\n", strerror(errno));
542 return -EIO;
543 }
544 if (res != len) {
545 fprintf(stderr, "fuse: internal error: short read back from pipe: %i from %zi\n", res, len);
546 return -EIO;
547 }
548 return 0;
549}
550
551static int fuse_send_data_iov(struct fuse_ll *f, struct fuse_chan *ch,
552 struct iovec *iov, int iov_count,
553 struct fuse_bufvec *buf, unsigned int flags)
554{
555 int res;
556 size_t len = fuse_buf_size(buf);
557 struct fuse_out_header *out = iov[0].iov_base;
558 struct fuse_ll_pipe *llp;
559 int splice_flags;
560 size_t pipesize;
561 size_t total_fd_size;
562 size_t idx;
563 size_t headerlen;
564 struct fuse_bufvec pipe_buf = FUSE_BUFVEC_INIT(len);
565
566 if (f->broken_splice_nonblock)
567 goto fallback;
568
569 if (flags & FUSE_BUF_NO_SPLICE)
570 goto fallback;
571
572 total_fd_size = 0;
573 for (idx = buf->idx; idx < buf->count; idx++) {
574 if (buf->buf[idx].flags & FUSE_BUF_IS_FD) {
575 total_fd_size = buf->buf[idx].size;
576 if (idx == buf->idx)
577 total_fd_size -= buf->off;
578 }
579 }
580 if (total_fd_size < 2 * pagesize)
581 goto fallback;
582
583 if (f->conn.proto_minor < 14 ||
584 !(f->conn.want & FUSE_CAP_SPLICE_WRITE))
585 goto fallback;
586
587 llp = fuse_ll_get_pipe(f);
588 if (llp == NULL)
589 goto fallback;
590
591
592 headerlen = iov_length(iov, iov_count);
593
594 out->len = headerlen + len;
595
596 /*
597 * Heuristic for the required pipe size, does not work if the
598 * source contains less than page size fragments
599 */
600 pipesize = pagesize * (iov_count + buf->count + 1) + out->len;
601
602 if (llp->size < pipesize) {
603 if (llp->can_grow) {
604 res = fcntl(llp->pipe[0], F_SETPIPE_SZ, pipesize);
605 if (res == -1) {
606 llp->can_grow = 0;
607 goto fallback;
608 }
609 llp->size = res;
610 }
611 if (llp->size < pipesize)
612 goto fallback;
613 }
614
615
616 res = vmsplice(llp->pipe[1], iov, iov_count, SPLICE_F_NONBLOCK);
617 if (res == -1)
618 goto fallback;
619
620 if (res != headerlen) {
621 res = -EIO;
622 fprintf(stderr, "fuse: short vmsplice to pipe: %u/%zu\n", res,
623 headerlen);
624 goto clear_pipe;
625 }
626
627 pipe_buf.buf[0].flags = FUSE_BUF_IS_FD;
628 pipe_buf.buf[0].fd = llp->pipe[1];
629
630 res = fuse_buf_copy(&pipe_buf, buf,
631 FUSE_BUF_FORCE_SPLICE | FUSE_BUF_SPLICE_NONBLOCK);
632 if (res < 0) {
633 if (res == -EAGAIN || res == -EINVAL) {
634 /*
635 * Should only get EAGAIN on kernels with
636 * broken SPLICE_F_NONBLOCK support (<=
637 * 2.6.35) where this error or a short read is
638 * returned even if the pipe itself is not
639 * full
640 *
641 * EINVAL might mean that splice can't handle
642 * this combination of input and output.
643 */
644 if (res == -EAGAIN)
645 f->broken_splice_nonblock = 1;
646
647 pthread_setspecific(f->pipe_key, NULL);
648 fuse_ll_pipe_free(llp);
649 goto fallback;
650 }
651 res = -res;
652 goto clear_pipe;
653 }
654
655 if (res != 0 && res < len) {
656 struct fuse_bufvec mem_buf = FUSE_BUFVEC_INIT(len);
657 void *mbuf;
658 size_t now_len = res;
659 /*
660 * For regular files a short count is either
661 * 1) due to EOF, or
662 * 2) because of broken SPLICE_F_NONBLOCK (see above)
663 *
664 * For other inputs it's possible that we overflowed
665 * the pipe because of small buffer fragments.
666 */
667
668 res = posix_memalign(&mbuf, pagesize, len);
669 if (res != 0)
670 goto clear_pipe;
671
672 mem_buf.buf[0].mem = mbuf;
673 mem_buf.off = now_len;
674 res = fuse_buf_copy(&mem_buf, buf, 0);
675 if (res > 0) {
676 char *tmpbuf;
677 size_t extra_len = res;
678 /*
679 * Trickiest case: got more data. Need to get
680 * back the data from the pipe and then fall
681 * back to regular write.
682 */
683 tmpbuf = malloc(headerlen);
684 if (tmpbuf == NULL) {
685 free(mbuf);
686 res = ENOMEM;
687 goto clear_pipe;
688 }
689 res = read_back(llp->pipe[0], tmpbuf, headerlen);
690 if (res != 0) {
691 free(mbuf);
692 goto clear_pipe;
693 }
694 free(tmpbuf);
695 res = read_back(llp->pipe[0], mbuf, now_len);
696 if (res != 0) {
697 free(mbuf);
698 goto clear_pipe;
699 }
700 len = now_len + extra_len;
701 iov[iov_count].iov_base = mbuf;
702 iov[iov_count].iov_len = len;
703 iov_count++;
704 res = fuse_send_msg(f, ch, iov, iov_count);
705 free(mbuf);
706 return res;
707 }
708 free(mbuf);
709 res = now_len;
710 }
711 len = res;
712 out->len = headerlen + len;
713
714 if (f->debug) {
715 fprintf(stderr,
716 " unique: %llu, success, outsize: %i (splice)\n",
717 (unsigned long long) out->unique, out->len);
718 }
719
720 splice_flags = 0;
721 if ((flags & FUSE_BUF_SPLICE_MOVE) &&
722 (f->conn.want & FUSE_CAP_SPLICE_MOVE))
723 splice_flags |= SPLICE_F_MOVE;
724
725 res = splice(llp->pipe[0], NULL,
726 fuse_chan_fd(ch), NULL, out->len, splice_flags);
727 if (res == -1) {
728 res = -errno;
729 perror("fuse: splice from pipe");
730 goto clear_pipe;
731 }
732 if (res != out->len) {
733 res = -EIO;
734 fprintf(stderr, "fuse: short splice from pipe: %u/%u\n",
735 res, out->len);
736 goto clear_pipe;
737 }
738 return 0;
739
740clear_pipe:
741 fuse_ll_clear_pipe(f);
742 return res;
743
744fallback:
745 return fuse_send_data_iov_fallback(f, ch, iov, iov_count, buf, len);
746}
747#else
748static int fuse_send_data_iov(struct fuse_ll *f, struct fuse_chan *ch,
749 struct iovec *iov, int iov_count,
750 struct fuse_bufvec *buf, unsigned int flags)
751{
752 size_t len = fuse_buf_size(buf);
753 (void) flags;
754
755 return fuse_send_data_iov_fallback(f, ch, iov, iov_count, buf, len);
756}
757#endif
758
759int fuse_reply_data(fuse_req_t req, struct fuse_bufvec *bufv,
760 enum fuse_buf_copy_flags flags)
761{
762 struct iovec iov[2];
763 struct fuse_out_header out;
764 int res;
765
766 iov[0].iov_base = &out;
767 iov[0].iov_len = sizeof(struct fuse_out_header);
768
769 out.unique = req->unique;
770 out.error = 0;
771
772 res = fuse_send_data_iov(req->f, req->ch, iov, 1, bufv, flags);
773 if (res <= 0) {
774 fuse_free_req(req);
775 return res;
776 } else {
777 return fuse_reply_err(req, res);
778 }
779}
780
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500781int fuse_reply_statfs(fuse_req_t req, const struct statvfs *stbuf)
782{
783 struct fuse_statfs_out arg;
784 size_t size = req->f->conn.proto_minor < 4 ?
785 FUSE_COMPAT_STATFS_SIZE : sizeof(arg);
786
787 memset(&arg, 0, sizeof(arg));
788 convert_statfs(stbuf, &arg.st);
789
790 return send_reply_ok(req, &arg, size);
791}
792
793int fuse_reply_xattr(fuse_req_t req, size_t count)
794{
795 struct fuse_getxattr_out arg;
796
797 memset(&arg, 0, sizeof(arg));
798 arg.size = count;
799
800 return send_reply_ok(req, &arg, sizeof(arg));
801}
802
Dees_Troye34c1332013-02-06 19:13:00 +0000803int fuse_reply_lock(fuse_req_t req, const struct flock *lock)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500804{
805 struct fuse_lk_out arg;
806
807 memset(&arg, 0, sizeof(arg));
808 arg.lk.type = lock->l_type;
809 if (lock->l_type != F_UNLCK) {
810 arg.lk.start = lock->l_start;
811 if (lock->l_len == 0)
812 arg.lk.end = OFFSET_MAX;
813 else
814 arg.lk.end = lock->l_start + lock->l_len - 1;
815 }
816 arg.lk.pid = lock->l_pid;
817 return send_reply_ok(req, &arg, sizeof(arg));
818}
819
820int fuse_reply_bmap(fuse_req_t req, uint64_t idx)
821{
822 struct fuse_bmap_out arg;
823
824 memset(&arg, 0, sizeof(arg));
825 arg.block = idx;
826
827 return send_reply_ok(req, &arg, sizeof(arg));
828}
829
Dees_Troye34c1332013-02-06 19:13:00 +0000830static struct fuse_ioctl_iovec *fuse_ioctl_iovec_copy(const struct iovec *iov,
831 size_t count)
832{
833 struct fuse_ioctl_iovec *fiov;
834 size_t i;
835
836 fiov = malloc(sizeof(fiov[0]) * count);
837 if (!fiov)
838 return NULL;
839
840 for (i = 0; i < count; i++) {
841 fiov[i].base = (uintptr_t) iov[i].iov_base;
842 fiov[i].len = iov[i].iov_len;
843 }
844
845 return fiov;
846}
847
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500848int fuse_reply_ioctl_retry(fuse_req_t req,
849 const struct iovec *in_iov, size_t in_count,
850 const struct iovec *out_iov, size_t out_count)
851{
852 struct fuse_ioctl_out arg;
Dees_Troye34c1332013-02-06 19:13:00 +0000853 struct fuse_ioctl_iovec *in_fiov = NULL;
854 struct fuse_ioctl_iovec *out_fiov = NULL;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500855 struct iovec iov[4];
856 size_t count = 1;
Dees_Troye34c1332013-02-06 19:13:00 +0000857 int res;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500858
859 memset(&arg, 0, sizeof(arg));
860 arg.flags |= FUSE_IOCTL_RETRY;
861 arg.in_iovs = in_count;
862 arg.out_iovs = out_count;
863 iov[count].iov_base = &arg;
864 iov[count].iov_len = sizeof(arg);
865 count++;
866
Dees_Troye34c1332013-02-06 19:13:00 +0000867 if (req->f->conn.proto_minor < 16) {
868 if (in_count) {
869 iov[count].iov_base = (void *)in_iov;
870 iov[count].iov_len = sizeof(in_iov[0]) * in_count;
871 count++;
872 }
873
874 if (out_count) {
875 iov[count].iov_base = (void *)out_iov;
876 iov[count].iov_len = sizeof(out_iov[0]) * out_count;
877 count++;
878 }
879 } else {
880 /* Can't handle non-compat 64bit ioctls on 32bit */
881 if (sizeof(void *) == 4 && req->ioctl_64bit) {
882 res = fuse_reply_err(req, EINVAL);
883 goto out;
884 }
885
886 if (in_count) {
887 in_fiov = fuse_ioctl_iovec_copy(in_iov, in_count);
888 if (!in_fiov)
889 goto enomem;
890
891 iov[count].iov_base = (void *)in_fiov;
892 iov[count].iov_len = sizeof(in_fiov[0]) * in_count;
893 count++;
894 }
895 if (out_count) {
896 out_fiov = fuse_ioctl_iovec_copy(out_iov, out_count);
897 if (!out_fiov)
898 goto enomem;
899
900 iov[count].iov_base = (void *)out_fiov;
901 iov[count].iov_len = sizeof(out_fiov[0]) * out_count;
902 count++;
903 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500904 }
905
Dees_Troye34c1332013-02-06 19:13:00 +0000906 res = send_reply_iov(req, 0, iov, count);
907out:
908 free(in_fiov);
909 free(out_fiov);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500910
Dees_Troye34c1332013-02-06 19:13:00 +0000911 return res;
912
913enomem:
914 res = fuse_reply_err(req, ENOMEM);
915 goto out;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500916}
917
918int fuse_reply_ioctl(fuse_req_t req, int result, const void *buf, size_t size)
919{
920 struct fuse_ioctl_out arg;
921 struct iovec iov[3];
922 size_t count = 1;
923
924 memset(&arg, 0, sizeof(arg));
925 arg.result = result;
926 iov[count].iov_base = &arg;
927 iov[count].iov_len = sizeof(arg);
928 count++;
929
930 if (size) {
931 iov[count].iov_base = (char *) buf;
932 iov[count].iov_len = size;
933 count++;
934 }
935
936 return send_reply_iov(req, 0, iov, count);
937}
938
939int fuse_reply_ioctl_iov(fuse_req_t req, int result, const struct iovec *iov,
940 int count)
941{
942 struct iovec *padded_iov;
943 struct fuse_ioctl_out arg;
944 int res;
945
946 padded_iov = malloc((count + 2) * sizeof(struct iovec));
Dees_Troye34c1332013-02-06 19:13:00 +0000947 if (padded_iov == NULL)
948 return fuse_reply_err(req, ENOMEM);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500949
950 memset(&arg, 0, sizeof(arg));
951 arg.result = result;
952 padded_iov[1].iov_base = &arg;
953 padded_iov[1].iov_len = sizeof(arg);
954
955 memcpy(&padded_iov[2], iov, count * sizeof(struct iovec));
956
957 res = send_reply_iov(req, 0, padded_iov, count + 2);
958 free(padded_iov);
959
960 return res;
961}
962
963int fuse_reply_poll(fuse_req_t req, unsigned revents)
964{
965 struct fuse_poll_out arg;
966
967 memset(&arg, 0, sizeof(arg));
968 arg.revents = revents;
969
970 return send_reply_ok(req, &arg, sizeof(arg));
971}
972
973static void do_lookup(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
974{
975 char *name = (char *) inarg;
976
977 if (req->f->op.lookup)
978 req->f->op.lookup(req, nodeid, name);
Dees_Troye34c1332013-02-06 19:13:00 +0000979 else
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500980 fuse_reply_err(req, ENOSYS);
981}
982
983static void do_forget(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
984{
985 struct fuse_forget_in *arg = (struct fuse_forget_in *) inarg;
986
987 if (req->f->op.forget)
988 req->f->op.forget(req, nodeid, arg->nlookup);
989 else
990 fuse_reply_none(req);
991}
992
Dees_Troye34c1332013-02-06 19:13:00 +0000993static void do_batch_forget(fuse_req_t req, fuse_ino_t nodeid,
994 const void *inarg)
995{
996 struct fuse_batch_forget_in *arg = (void *) inarg;
997 struct fuse_forget_one *param = (void *) PARAM(arg);
998 unsigned int i;
999
1000 (void) nodeid;
1001
1002 if (req->f->op.forget_multi) {
1003 req->f->op.forget_multi(req, arg->count,
1004 (struct fuse_forget_data *) param);
1005 } else if (req->f->op.forget) {
1006 for (i = 0; i < arg->count; i++) {
1007 struct fuse_forget_one *forget = &param[i];
1008 struct fuse_req *dummy_req;
1009
1010 dummy_req = fuse_ll_alloc_req(req->f);
1011 if (dummy_req == NULL)
1012 break;
1013
1014 dummy_req->unique = req->unique;
1015 dummy_req->ctx = req->ctx;
1016 dummy_req->ch = NULL;
1017
1018 req->f->op.forget(dummy_req, forget->nodeid,
1019 forget->nlookup);
1020 }
1021 fuse_reply_none(req);
1022 } else {
1023 fuse_reply_none(req);
1024 }
1025}
1026
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001027static void do_getattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1028{
1029 struct fuse_file_info *fip = NULL;
1030 struct fuse_file_info fi;
1031
1032 if (req->f->conn.proto_minor >= 9) {
1033 struct fuse_getattr_in *arg = (struct fuse_getattr_in *) inarg;
1034
1035 if (arg->getattr_flags & FUSE_GETATTR_FH) {
1036 memset(&fi, 0, sizeof(fi));
1037 fi.fh = arg->fh;
1038 fi.fh_old = fi.fh;
1039 fip = &fi;
1040 }
1041 }
1042
1043 if (req->f->op.getattr)
1044 req->f->op.getattr(req, nodeid, fip);
Dees_Troye34c1332013-02-06 19:13:00 +00001045 else
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001046 fuse_reply_err(req, ENOSYS);
1047}
1048
1049static void do_setattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1050{
1051 struct fuse_setattr_in *arg = (struct fuse_setattr_in *) inarg;
1052
1053 if (req->f->op.setattr) {
1054 struct fuse_file_info *fi = NULL;
1055 struct fuse_file_info fi_store;
1056 struct stat stbuf;
1057 memset(&stbuf, 0, sizeof(stbuf));
1058 convert_attr(arg, &stbuf);
1059 if (arg->valid & FATTR_FH) {
1060 arg->valid &= ~FATTR_FH;
1061 memset(&fi_store, 0, sizeof(fi_store));
1062 fi = &fi_store;
1063 fi->fh = arg->fh;
1064 fi->fh_old = fi->fh;
1065 }
1066 arg->valid &=
1067 FUSE_SET_ATTR_MODE |
1068 FUSE_SET_ATTR_UID |
1069 FUSE_SET_ATTR_GID |
1070 FUSE_SET_ATTR_SIZE |
1071 FUSE_SET_ATTR_ATIME |
1072 FUSE_SET_ATTR_MTIME |
1073 FUSE_SET_ATTR_ATIME_NOW |
1074 FUSE_SET_ATTR_MTIME_NOW;
1075
1076 req->f->op.setattr(req, nodeid, &stbuf, arg->valid, fi);
Dees_Troye34c1332013-02-06 19:13:00 +00001077 } else
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001078 fuse_reply_err(req, ENOSYS);
1079}
1080
1081static void do_access(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1082{
1083 struct fuse_access_in *arg = (struct fuse_access_in *) inarg;
1084
1085 if (req->f->op.access)
1086 req->f->op.access(req, nodeid, arg->mask);
Dees_Troye34c1332013-02-06 19:13:00 +00001087 else
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001088 fuse_reply_err(req, ENOSYS);
1089}
1090
1091static void do_readlink(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1092{
1093 (void) inarg;
1094
1095 if (req->f->op.readlink)
1096 req->f->op.readlink(req, nodeid);
Dees_Troye34c1332013-02-06 19:13:00 +00001097 else
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001098 fuse_reply_err(req, ENOSYS);
1099}
1100
1101static void do_mknod(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1102{
1103 struct fuse_mknod_in *arg = (struct fuse_mknod_in *) inarg;
1104 char *name = PARAM(arg);
1105
1106 if (req->f->conn.proto_minor >= 12)
1107 req->ctx.umask = arg->umask;
1108 else
1109 name = (char *) inarg + FUSE_COMPAT_MKNOD_IN_SIZE;
1110
1111 if (req->f->op.mknod)
1112 req->f->op.mknod(req, nodeid, name, arg->mode, arg->rdev);
Dees_Troye34c1332013-02-06 19:13:00 +00001113 else
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001114 fuse_reply_err(req, ENOSYS);
1115}
1116
1117static void do_mkdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1118{
1119 struct fuse_mkdir_in *arg = (struct fuse_mkdir_in *) inarg;
1120
1121 if (req->f->conn.proto_minor >= 12)
1122 req->ctx.umask = arg->umask;
1123
1124 if (req->f->op.mkdir)
1125 req->f->op.mkdir(req, nodeid, PARAM(arg), arg->mode);
Dees_Troye34c1332013-02-06 19:13:00 +00001126 else
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001127 fuse_reply_err(req, ENOSYS);
1128}
1129
1130static void do_unlink(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1131{
1132 char *name = (char *) inarg;
1133
1134 if (req->f->op.unlink)
1135 req->f->op.unlink(req, nodeid, name);
Dees_Troye34c1332013-02-06 19:13:00 +00001136 else
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001137 fuse_reply_err(req, ENOSYS);
1138}
1139
1140static void do_rmdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1141{
1142 char *name = (char *) inarg;
1143
1144 if (req->f->op.rmdir)
1145 req->f->op.rmdir(req, nodeid, name);
Dees_Troye34c1332013-02-06 19:13:00 +00001146 else
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001147 fuse_reply_err(req, ENOSYS);
1148}
1149
1150static void do_symlink(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1151{
1152 char *name = (char *) inarg;
1153 char *linkname = ((char *) inarg) + strlen((char *) inarg) + 1;
1154
1155 if (req->f->op.symlink)
1156 req->f->op.symlink(req, linkname, nodeid, name);
Dees_Troye34c1332013-02-06 19:13:00 +00001157 else
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001158 fuse_reply_err(req, ENOSYS);
1159}
1160
1161static void do_rename(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1162{
1163 struct fuse_rename_in *arg = (struct fuse_rename_in *) inarg;
1164 char *oldname = PARAM(arg);
1165 char *newname = oldname + strlen(oldname) + 1;
1166
1167 if (req->f->op.rename)
1168 req->f->op.rename(req, nodeid, oldname, arg->newdir, newname);
Dees_Troye34c1332013-02-06 19:13:00 +00001169 else
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001170 fuse_reply_err(req, ENOSYS);
1171}
1172
1173static void do_link(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1174{
1175 struct fuse_link_in *arg = (struct fuse_link_in *) inarg;
1176
1177 if (req->f->op.link)
1178 req->f->op.link(req, arg->oldnodeid, nodeid, PARAM(arg));
Dees_Troye34c1332013-02-06 19:13:00 +00001179 else
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001180 fuse_reply_err(req, ENOSYS);
1181}
1182
1183static void do_create(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1184{
1185 struct fuse_create_in *arg = (struct fuse_create_in *) inarg;
1186
1187 if (req->f->op.create) {
1188 struct fuse_file_info fi;
1189 char *name = PARAM(arg);
1190
1191 memset(&fi, 0, sizeof(fi));
1192 fi.flags = arg->flags;
1193
1194 if (req->f->conn.proto_minor >= 12)
1195 req->ctx.umask = arg->umask;
1196 else
1197 name = (char *) inarg + sizeof(struct fuse_open_in);
1198
1199 req->f->op.create(req, nodeid, name, arg->mode, &fi);
Dees_Troye34c1332013-02-06 19:13:00 +00001200 } else
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001201 fuse_reply_err(req, ENOSYS);
1202}
1203
1204static void do_open(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1205{
1206 struct fuse_open_in *arg = (struct fuse_open_in *) inarg;
1207 struct fuse_file_info fi;
1208
1209 memset(&fi, 0, sizeof(fi));
1210 fi.flags = arg->flags;
1211
1212 if (req->f->op.open)
1213 req->f->op.open(req, nodeid, &fi);
1214 else
1215 fuse_reply_open(req, &fi);
1216}
1217
1218static void do_read(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1219{
1220 struct fuse_read_in *arg = (struct fuse_read_in *) inarg;
1221
1222 if (req->f->op.read) {
1223 struct fuse_file_info fi;
1224
1225 memset(&fi, 0, sizeof(fi));
1226 fi.fh = arg->fh;
1227 fi.fh_old = fi.fh;
1228 if (req->f->conn.proto_minor >= 9) {
1229 fi.lock_owner = arg->lock_owner;
1230 fi.flags = arg->flags;
1231 }
1232 req->f->op.read(req, nodeid, arg->size, arg->offset, &fi);
Dees_Troye34c1332013-02-06 19:13:00 +00001233 } else
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001234 fuse_reply_err(req, ENOSYS);
1235}
1236
1237static void do_write(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1238{
1239 struct fuse_write_in *arg = (struct fuse_write_in *) inarg;
1240 struct fuse_file_info fi;
1241 char *param;
1242
1243 memset(&fi, 0, sizeof(fi));
1244 fi.fh = arg->fh;
1245 fi.fh_old = fi.fh;
1246 fi.writepage = arg->write_flags & 1;
1247
1248 if (req->f->conn.proto_minor < 9) {
1249 param = ((char *) arg) + FUSE_COMPAT_WRITE_IN_SIZE;
1250 } else {
1251 fi.lock_owner = arg->lock_owner;
1252 fi.flags = arg->flags;
1253 param = PARAM(arg);
1254 }
1255
1256 if (req->f->op.write)
1257 req->f->op.write(req, nodeid, param, arg->size,
1258 arg->offset, &fi);
Dees_Troye34c1332013-02-06 19:13:00 +00001259 else
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001260 fuse_reply_err(req, ENOSYS);
Dees_Troye34c1332013-02-06 19:13:00 +00001261}
1262
1263static void do_write_buf(fuse_req_t req, fuse_ino_t nodeid, const void *inarg,
1264 const struct fuse_buf *ibuf)
1265{
1266 struct fuse_ll *f = req->f;
1267 struct fuse_bufvec bufv = {
1268 .buf[0] = *ibuf,
1269 .count = 1,
1270 };
1271 struct fuse_write_in *arg = (struct fuse_write_in *) inarg;
1272 struct fuse_file_info fi;
1273
1274 memset(&fi, 0, sizeof(fi));
1275 fi.fh = arg->fh;
1276 fi.fh_old = fi.fh;
1277 fi.writepage = arg->write_flags & 1;
1278
1279 if (req->f->conn.proto_minor < 9) {
1280 bufv.buf[0].mem = ((char *) arg) + FUSE_COMPAT_WRITE_IN_SIZE;
1281 bufv.buf[0].size -= sizeof(struct fuse_in_header) +
1282 FUSE_COMPAT_WRITE_IN_SIZE;
1283 assert(!(bufv.buf[0].flags & FUSE_BUF_IS_FD));
1284 } else {
1285 fi.lock_owner = arg->lock_owner;
1286 fi.flags = arg->flags;
1287 if (!(bufv.buf[0].flags & FUSE_BUF_IS_FD))
1288 bufv.buf[0].mem = PARAM(arg);
1289
1290 bufv.buf[0].size -= sizeof(struct fuse_in_header) +
1291 sizeof(struct fuse_write_in);
bigbiff bigbiff31f0e5f2013-01-19 10:23:42 -05001292 }
Dees_Troye34c1332013-02-06 19:13:00 +00001293 if (bufv.buf[0].size < arg->size) {
1294 fprintf(stderr, "fuse: do_write_buf: buffer size too small\n");
1295 fuse_reply_err(req, EIO);
1296 goto out;
1297 }
1298 bufv.buf[0].size = arg->size;
1299
1300 req->f->op.write_buf(req, nodeid, &bufv, arg->offset, &fi);
1301
1302out:
1303 /* Need to reset the pipe if ->write_buf() didn't consume all data */
1304 if ((ibuf->flags & FUSE_BUF_IS_FD) && bufv.idx < bufv.count)
1305 fuse_ll_clear_pipe(f);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001306}
1307
1308static void do_flush(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1309{
1310 struct fuse_flush_in *arg = (struct fuse_flush_in *) inarg;
1311 struct fuse_file_info fi;
1312
1313 memset(&fi, 0, sizeof(fi));
1314 fi.fh = arg->fh;
1315 fi.fh_old = fi.fh;
1316 fi.flush = 1;
1317 if (req->f->conn.proto_minor >= 7)
1318 fi.lock_owner = arg->lock_owner;
1319
1320 if (req->f->op.flush)
1321 req->f->op.flush(req, nodeid, &fi);
Dees_Troye34c1332013-02-06 19:13:00 +00001322 else
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001323 fuse_reply_err(req, ENOSYS);
1324}
1325
1326static void do_release(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1327{
1328 struct fuse_release_in *arg = (struct fuse_release_in *) inarg;
1329 struct fuse_file_info fi;
1330
1331 memset(&fi, 0, sizeof(fi));
1332 fi.flags = arg->flags;
1333 fi.fh = arg->fh;
1334 fi.fh_old = fi.fh;
1335 if (req->f->conn.proto_minor >= 8) {
1336 fi.flush = (arg->release_flags & FUSE_RELEASE_FLUSH) ? 1 : 0;
1337 fi.lock_owner = arg->lock_owner;
1338 }
Dees_Troye34c1332013-02-06 19:13:00 +00001339 if (arg->release_flags & FUSE_RELEASE_FLOCK_UNLOCK) {
1340 fi.flock_release = 1;
1341 fi.lock_owner = arg->lock_owner;
1342 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001343
1344 if (req->f->op.release)
1345 req->f->op.release(req, nodeid, &fi);
1346 else
1347 fuse_reply_err(req, 0);
1348}
1349
1350static void do_fsync(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1351{
1352 struct fuse_fsync_in *arg = (struct fuse_fsync_in *) inarg;
1353 struct fuse_file_info fi;
1354
1355 memset(&fi, 0, sizeof(fi));
1356 fi.fh = arg->fh;
1357 fi.fh_old = fi.fh;
1358
1359 if (req->f->op.fsync)
1360 req->f->op.fsync(req, nodeid, arg->fsync_flags & 1, &fi);
Dees_Troye34c1332013-02-06 19:13:00 +00001361 else
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001362 fuse_reply_err(req, ENOSYS);
1363}
1364
1365static void do_opendir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1366{
1367 struct fuse_open_in *arg = (struct fuse_open_in *) inarg;
1368 struct fuse_file_info fi;
1369
1370 memset(&fi, 0, sizeof(fi));
1371 fi.flags = arg->flags;
1372
1373 if (req->f->op.opendir)
1374 req->f->op.opendir(req, nodeid, &fi);
1375 else
1376 fuse_reply_open(req, &fi);
1377}
1378
1379static void do_readdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1380{
1381 struct fuse_read_in *arg = (struct fuse_read_in *) inarg;
1382 struct fuse_file_info fi;
1383
1384 memset(&fi, 0, sizeof(fi));
1385 fi.fh = arg->fh;
1386 fi.fh_old = fi.fh;
1387
1388 if (req->f->op.readdir)
1389 req->f->op.readdir(req, nodeid, arg->size, arg->offset, &fi);
Dees_Troye34c1332013-02-06 19:13:00 +00001390 else
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001391 fuse_reply_err(req, ENOSYS);
1392}
1393
1394static void do_releasedir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1395{
1396 struct fuse_release_in *arg = (struct fuse_release_in *) inarg;
1397 struct fuse_file_info fi;
1398
1399 memset(&fi, 0, sizeof(fi));
1400 fi.flags = arg->flags;
1401 fi.fh = arg->fh;
1402 fi.fh_old = fi.fh;
1403
1404 if (req->f->op.releasedir)
1405 req->f->op.releasedir(req, nodeid, &fi);
1406 else
1407 fuse_reply_err(req, 0);
1408}
1409
1410static void do_fsyncdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1411{
1412 struct fuse_fsync_in *arg = (struct fuse_fsync_in *) inarg;
1413 struct fuse_file_info fi;
1414
1415 memset(&fi, 0, sizeof(fi));
1416 fi.fh = arg->fh;
1417 fi.fh_old = fi.fh;
1418
1419 if (req->f->op.fsyncdir)
1420 req->f->op.fsyncdir(req, nodeid, arg->fsync_flags & 1, &fi);
Dees_Troye34c1332013-02-06 19:13:00 +00001421 else
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001422 fuse_reply_err(req, ENOSYS);
1423}
1424
1425static void do_statfs(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1426{
1427 (void) nodeid;
1428 (void) inarg;
1429
1430 if (req->f->op.statfs)
1431 req->f->op.statfs(req, nodeid);
1432 else {
1433 struct statvfs buf = {
1434 .f_namemax = 255,
1435 .f_bsize = 512,
1436 };
1437 fuse_reply_statfs(req, &buf);
1438 }
1439}
1440
1441static void do_setxattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1442{
1443 struct fuse_setxattr_in *arg = (struct fuse_setxattr_in *) inarg;
1444 char *name = PARAM(arg);
1445 char *value = name + strlen(name) + 1;
1446
1447 if (req->f->op.setxattr)
1448 req->f->op.setxattr(req, nodeid, name, value, arg->size,
1449 arg->flags);
Dees_Troye34c1332013-02-06 19:13:00 +00001450 else
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001451 fuse_reply_err(req, ENOSYS);
1452}
1453
1454static void do_getxattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1455{
1456 struct fuse_getxattr_in *arg = (struct fuse_getxattr_in *) inarg;
1457
1458 if (req->f->op.getxattr)
1459 req->f->op.getxattr(req, nodeid, PARAM(arg), arg->size);
Dees_Troye34c1332013-02-06 19:13:00 +00001460 else
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001461 fuse_reply_err(req, ENOSYS);
1462}
1463
1464static void do_listxattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1465{
1466 struct fuse_getxattr_in *arg = (struct fuse_getxattr_in *) inarg;
1467
1468 if (req->f->op.listxattr)
1469 req->f->op.listxattr(req, nodeid, arg->size);
Dees_Troye34c1332013-02-06 19:13:00 +00001470 else
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001471 fuse_reply_err(req, ENOSYS);
1472}
1473
1474static void do_removexattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1475{
1476 char *name = (char *) inarg;
1477
1478 if (req->f->op.removexattr)
1479 req->f->op.removexattr(req, nodeid, name);
Dees_Troye34c1332013-02-06 19:13:00 +00001480 else
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001481 fuse_reply_err(req, ENOSYS);
1482}
1483
1484static void convert_fuse_file_lock(struct fuse_file_lock *fl,
1485 struct flock *flock)
1486{
1487 memset(flock, 0, sizeof(struct flock));
1488 flock->l_type = fl->type;
1489 flock->l_whence = SEEK_SET;
1490 flock->l_start = fl->start;
1491 if (fl->end == OFFSET_MAX)
1492 flock->l_len = 0;
1493 else
1494 flock->l_len = fl->end - fl->start + 1;
1495 flock->l_pid = fl->pid;
1496}
1497
1498static void do_getlk(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1499{
1500 struct fuse_lk_in *arg = (struct fuse_lk_in *) inarg;
1501 struct fuse_file_info fi;
1502 struct flock flock;
1503
1504 memset(&fi, 0, sizeof(fi));
1505 fi.fh = arg->fh;
1506 fi.lock_owner = arg->owner;
1507
1508 convert_fuse_file_lock(&arg->lk, &flock);
1509 if (req->f->op.getlk)
1510 req->f->op.getlk(req, nodeid, &fi, &flock);
Dees_Troye34c1332013-02-06 19:13:00 +00001511 else
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001512 fuse_reply_err(req, ENOSYS);
1513}
1514
1515static void do_setlk_common(fuse_req_t req, fuse_ino_t nodeid,
1516 const void *inarg, int sleep)
1517{
1518 struct fuse_lk_in *arg = (struct fuse_lk_in *) inarg;
1519 struct fuse_file_info fi;
1520 struct flock flock;
1521
1522 memset(&fi, 0, sizeof(fi));
1523 fi.fh = arg->fh;
1524 fi.lock_owner = arg->owner;
1525
Dees_Troye34c1332013-02-06 19:13:00 +00001526 if (arg->lk_flags & FUSE_LK_FLOCK) {
1527 int op = 0;
1528
1529 switch (arg->lk.type) {
1530 case F_RDLCK:
1531 op = LOCK_SH;
1532 break;
1533 case F_WRLCK:
1534 op = LOCK_EX;
1535 break;
1536 case F_UNLCK:
1537 op = LOCK_UN;
1538 break;
1539 }
1540 if (!sleep)
1541 op |= LOCK_NB;
1542
1543 if (req->f->op.flock)
1544 req->f->op.flock(req, nodeid, &fi, op);
1545 else
1546 fuse_reply_err(req, ENOSYS);
1547 } else {
1548 convert_fuse_file_lock(&arg->lk, &flock);
1549 if (req->f->op.setlk)
1550 req->f->op.setlk(req, nodeid, &fi, &flock, sleep);
1551 else
1552 fuse_reply_err(req, ENOSYS);
bigbiff bigbiff31f0e5f2013-01-19 10:23:42 -05001553 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001554}
1555
1556static void do_setlk(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1557{
1558 do_setlk_common(req, nodeid, inarg, 0);
1559}
1560
1561static void do_setlkw(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1562{
1563 do_setlk_common(req, nodeid, inarg, 1);
1564}
1565
1566static int find_interrupted(struct fuse_ll *f, struct fuse_req *req)
1567{
1568 struct fuse_req *curr;
1569
1570 for (curr = f->list.next; curr != &f->list; curr = curr->next) {
1571 if (curr->unique == req->u.i.unique) {
1572 fuse_interrupt_func_t func;
1573 void *data;
1574
1575 curr->ctr++;
1576 pthread_mutex_unlock(&f->lock);
1577
1578 /* Ugh, ugly locking */
1579 pthread_mutex_lock(&curr->lock);
1580 pthread_mutex_lock(&f->lock);
1581 curr->interrupted = 1;
1582 func = curr->u.ni.func;
1583 data = curr->u.ni.data;
1584 pthread_mutex_unlock(&f->lock);
1585 if (func)
1586 func(curr, data);
1587 pthread_mutex_unlock(&curr->lock);
1588
1589 pthread_mutex_lock(&f->lock);
1590 curr->ctr--;
1591 if (!curr->ctr)
1592 destroy_req(curr);
1593
1594 return 1;
1595 }
1596 }
1597 for (curr = f->interrupts.next; curr != &f->interrupts;
1598 curr = curr->next) {
1599 if (curr->u.i.unique == req->u.i.unique)
1600 return 1;
1601 }
1602 return 0;
1603}
1604
1605static void do_interrupt(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1606{
1607 struct fuse_interrupt_in *arg = (struct fuse_interrupt_in *) inarg;
1608 struct fuse_ll *f = req->f;
1609
1610 (void) nodeid;
1611 if (f->debug)
1612 fprintf(stderr, "INTERRUPT: %llu\n",
1613 (unsigned long long) arg->unique);
1614
1615 req->u.i.unique = arg->unique;
1616
1617 pthread_mutex_lock(&f->lock);
1618 if (find_interrupted(f, req))
1619 destroy_req(req);
1620 else
1621 list_add_req(req, &f->interrupts);
1622 pthread_mutex_unlock(&f->lock);
1623}
1624
1625static struct fuse_req *check_interrupt(struct fuse_ll *f, struct fuse_req *req)
1626{
1627 struct fuse_req *curr;
1628
1629 for (curr = f->interrupts.next; curr != &f->interrupts;
1630 curr = curr->next) {
1631 if (curr->u.i.unique == req->unique) {
1632 req->interrupted = 1;
1633 list_del_req(curr);
1634 free(curr);
1635 return NULL;
1636 }
1637 }
1638 curr = f->interrupts.next;
1639 if (curr != &f->interrupts) {
1640 list_del_req(curr);
1641 list_init_req(curr);
1642 return curr;
1643 } else
1644 return NULL;
1645}
1646
1647static void do_bmap(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1648{
1649 struct fuse_bmap_in *arg = (struct fuse_bmap_in *) inarg;
1650
1651 if (req->f->op.bmap)
1652 req->f->op.bmap(req, nodeid, arg->blocksize, arg->block);
Dees_Troye34c1332013-02-06 19:13:00 +00001653 else
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001654 fuse_reply_err(req, ENOSYS);
1655}
1656
1657static void do_ioctl(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1658{
1659 struct fuse_ioctl_in *arg = (struct fuse_ioctl_in *) inarg;
1660 unsigned int flags = arg->flags;
1661 void *in_buf = arg->in_size ? PARAM(arg) : NULL;
1662 struct fuse_file_info fi;
1663
Dees_Troye34c1332013-02-06 19:13:00 +00001664 if (flags & FUSE_IOCTL_DIR &&
1665 !(req->f->conn.want & FUSE_CAP_IOCTL_DIR)) {
1666 fuse_reply_err(req, ENOTTY);
1667 return;
1668 }
1669
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001670 memset(&fi, 0, sizeof(fi));
1671 fi.fh = arg->fh;
1672 fi.fh_old = fi.fh;
1673
Dees_Troye34c1332013-02-06 19:13:00 +00001674 if (sizeof(void *) == 4 && req->f->conn.proto_minor >= 16 &&
1675 !(flags & FUSE_IOCTL_32BIT)) {
1676 req->ioctl_64bit = 1;
1677 }
1678
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001679 if (req->f->op.ioctl)
1680 req->f->op.ioctl(req, nodeid, arg->cmd,
1681 (void *)(uintptr_t)arg->arg, &fi, flags,
1682 in_buf, arg->in_size, arg->out_size);
Dees_Troye34c1332013-02-06 19:13:00 +00001683 else
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001684 fuse_reply_err(req, ENOSYS);
1685}
1686
1687void fuse_pollhandle_destroy(struct fuse_pollhandle *ph)
1688{
1689 free(ph);
1690}
1691
1692static void do_poll(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1693{
1694 struct fuse_poll_in *arg = (struct fuse_poll_in *) inarg;
1695 struct fuse_file_info fi;
1696
1697 memset(&fi, 0, sizeof(fi));
1698 fi.fh = arg->fh;
1699 fi.fh_old = fi.fh;
1700
1701 if (req->f->op.poll) {
1702 struct fuse_pollhandle *ph = NULL;
1703
1704 if (arg->flags & FUSE_POLL_SCHEDULE_NOTIFY) {
1705 ph = malloc(sizeof(struct fuse_pollhandle));
1706 if (ph == NULL) {
1707 fuse_reply_err(req, ENOMEM);
1708 return;
1709 }
1710 ph->kh = arg->kh;
1711 ph->ch = req->ch;
1712 ph->f = req->f;
1713 }
1714
1715 req->f->op.poll(req, nodeid, &fi, ph);
1716 } else {
1717 fuse_reply_err(req, ENOSYS);
1718 }
1719}
1720
Dees_Troye34c1332013-02-06 19:13:00 +00001721static void do_fallocate(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1722{
1723 struct fuse_fallocate_in *arg = (struct fuse_fallocate_in *) inarg;
1724 struct fuse_file_info fi;
1725
1726 memset(&fi, 0, sizeof(fi));
1727 fi.fh = arg->fh;
1728
1729 if (req->f->op.fallocate)
1730 req->f->op.fallocate(req, nodeid, arg->mode, arg->offset, arg->length, &fi);
1731 else
1732 fuse_reply_err(req, ENOSYS);
1733}
1734
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001735static void do_init(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1736{
1737 struct fuse_init_in *arg = (struct fuse_init_in *) inarg;
1738 struct fuse_init_out outarg;
1739 struct fuse_ll *f = req->f;
1740 size_t bufsize = fuse_chan_bufsize(req->ch);
1741
1742 (void) nodeid;
1743 if (f->debug) {
1744 fprintf(stderr, "INIT: %u.%u\n", arg->major, arg->minor);
1745 if (arg->major == 7 && arg->minor >= 6) {
1746 fprintf(stderr, "flags=0x%08x\n", arg->flags);
1747 fprintf(stderr, "max_readahead=0x%08x\n",
1748 arg->max_readahead);
1749 }
1750 }
1751 f->conn.proto_major = arg->major;
1752 f->conn.proto_minor = arg->minor;
1753 f->conn.capable = 0;
1754 f->conn.want = 0;
1755
1756 memset(&outarg, 0, sizeof(outarg));
1757 outarg.major = FUSE_KERNEL_VERSION;
1758 outarg.minor = FUSE_KERNEL_MINOR_VERSION;
1759
1760 if (arg->major < 7) {
1761 fprintf(stderr, "fuse: unsupported protocol version: %u.%u\n",
1762 arg->major, arg->minor);
1763 fuse_reply_err(req, EPROTO);
1764 return;
1765 }
1766
1767 if (arg->major > 7) {
1768 /* Wait for a second INIT request with a 7.X version */
1769 send_reply_ok(req, &outarg, sizeof(outarg));
1770 return;
1771 }
1772
1773 if (arg->minor >= 6) {
1774 if (f->conn.async_read)
1775 f->conn.async_read = arg->flags & FUSE_ASYNC_READ;
1776 if (arg->max_readahead < f->conn.max_readahead)
1777 f->conn.max_readahead = arg->max_readahead;
1778 if (arg->flags & FUSE_ASYNC_READ)
1779 f->conn.capable |= FUSE_CAP_ASYNC_READ;
1780 if (arg->flags & FUSE_POSIX_LOCKS)
1781 f->conn.capable |= FUSE_CAP_POSIX_LOCKS;
1782 if (arg->flags & FUSE_ATOMIC_O_TRUNC)
1783 f->conn.capable |= FUSE_CAP_ATOMIC_O_TRUNC;
1784 if (arg->flags & FUSE_EXPORT_SUPPORT)
1785 f->conn.capable |= FUSE_CAP_EXPORT_SUPPORT;
1786 if (arg->flags & FUSE_BIG_WRITES)
1787 f->conn.capable |= FUSE_CAP_BIG_WRITES;
1788 if (arg->flags & FUSE_DONT_MASK)
1789 f->conn.capable |= FUSE_CAP_DONT_MASK;
Dees_Troye34c1332013-02-06 19:13:00 +00001790 if (arg->flags & FUSE_FLOCK_LOCKS)
1791 f->conn.capable |= FUSE_CAP_FLOCK_LOCKS;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001792 } else {
1793 f->conn.async_read = 0;
1794 f->conn.max_readahead = 0;
1795 }
1796
Dees_Troye34c1332013-02-06 19:13:00 +00001797 if (req->f->conn.proto_minor >= 14) {
1798#ifdef HAVE_SPLICE
1799#ifdef HAVE_VMSPLICE
1800 f->conn.capable |= FUSE_CAP_SPLICE_WRITE | FUSE_CAP_SPLICE_MOVE;
1801 if (f->splice_write)
1802 f->conn.want |= FUSE_CAP_SPLICE_WRITE;
1803 if (f->splice_move)
1804 f->conn.want |= FUSE_CAP_SPLICE_MOVE;
1805#endif
1806 f->conn.capable |= FUSE_CAP_SPLICE_READ;
1807 if (f->splice_read)
1808 f->conn.want |= FUSE_CAP_SPLICE_READ;
1809#endif
1810 }
1811 if (req->f->conn.proto_minor >= 18)
1812 f->conn.capable |= FUSE_CAP_IOCTL_DIR;
1813
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001814 if (f->atomic_o_trunc)
1815 f->conn.want |= FUSE_CAP_ATOMIC_O_TRUNC;
Dees_Troye34c1332013-02-06 19:13:00 +00001816 if (f->op.getlk && f->op.setlk && !f->no_remote_posix_lock)
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001817 f->conn.want |= FUSE_CAP_POSIX_LOCKS;
Dees_Troye34c1332013-02-06 19:13:00 +00001818 if (f->op.flock && !f->no_remote_flock)
1819 f->conn.want |= FUSE_CAP_FLOCK_LOCKS;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001820 if (f->big_writes)
1821 f->conn.want |= FUSE_CAP_BIG_WRITES;
1822
1823 if (bufsize < FUSE_MIN_READ_BUFFER) {
1824 fprintf(stderr, "fuse: warning: buffer size too small: %zu\n",
1825 bufsize);
1826 bufsize = FUSE_MIN_READ_BUFFER;
1827 }
1828
1829 bufsize -= 4096;
1830 if (bufsize < f->conn.max_write)
1831 f->conn.max_write = bufsize;
1832
1833 f->got_init = 1;
1834 if (f->op.init)
1835 f->op.init(f->userdata, &f->conn);
1836
Dees_Troye34c1332013-02-06 19:13:00 +00001837 if (f->no_splice_read)
1838 f->conn.want &= ~FUSE_CAP_SPLICE_READ;
1839 if (f->no_splice_write)
1840 f->conn.want &= ~FUSE_CAP_SPLICE_WRITE;
1841 if (f->no_splice_move)
1842 f->conn.want &= ~FUSE_CAP_SPLICE_MOVE;
1843
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001844 if (f->conn.async_read || (f->conn.want & FUSE_CAP_ASYNC_READ))
1845 outarg.flags |= FUSE_ASYNC_READ;
1846 if (f->conn.want & FUSE_CAP_POSIX_LOCKS)
1847 outarg.flags |= FUSE_POSIX_LOCKS;
1848 if (f->conn.want & FUSE_CAP_ATOMIC_O_TRUNC)
1849 outarg.flags |= FUSE_ATOMIC_O_TRUNC;
1850 if (f->conn.want & FUSE_CAP_EXPORT_SUPPORT)
1851 outarg.flags |= FUSE_EXPORT_SUPPORT;
1852 if (f->conn.want & FUSE_CAP_BIG_WRITES)
1853 outarg.flags |= FUSE_BIG_WRITES;
1854 if (f->conn.want & FUSE_CAP_DONT_MASK)
1855 outarg.flags |= FUSE_DONT_MASK;
Dees_Troye34c1332013-02-06 19:13:00 +00001856 if (f->conn.want & FUSE_CAP_FLOCK_LOCKS)
1857 outarg.flags |= FUSE_FLOCK_LOCKS;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001858 outarg.max_readahead = f->conn.max_readahead;
1859 outarg.max_write = f->conn.max_write;
Dees_Troye34c1332013-02-06 19:13:00 +00001860 if (f->conn.proto_minor >= 13) {
1861 if (f->conn.max_background >= (1 << 16))
1862 f->conn.max_background = (1 << 16) - 1;
1863 if (f->conn.congestion_threshold > f->conn.max_background)
1864 f->conn.congestion_threshold = f->conn.max_background;
1865 if (!f->conn.congestion_threshold) {
1866 f->conn.congestion_threshold =
1867 f->conn.max_background * 3 / 4;
1868 }
1869
1870 outarg.max_background = f->conn.max_background;
1871 outarg.congestion_threshold = f->conn.congestion_threshold;
1872 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001873
1874 if (f->debug) {
1875 fprintf(stderr, " INIT: %u.%u\n", outarg.major, outarg.minor);
1876 fprintf(stderr, " flags=0x%08x\n", outarg.flags);
1877 fprintf(stderr, " max_readahead=0x%08x\n",
1878 outarg.max_readahead);
1879 fprintf(stderr, " max_write=0x%08x\n", outarg.max_write);
Dees_Troye34c1332013-02-06 19:13:00 +00001880 fprintf(stderr, " max_background=%i\n",
1881 outarg.max_background);
1882 fprintf(stderr, " congestion_threshold=%i\n",
1883 outarg.congestion_threshold);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001884 }
1885
1886 send_reply_ok(req, &outarg, arg->minor < 5 ? 8 : sizeof(outarg));
1887}
1888
1889static void do_destroy(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1890{
1891 struct fuse_ll *f = req->f;
1892
1893 (void) nodeid;
1894 (void) inarg;
1895
1896 f->got_destroy = 1;
1897 if (f->op.destroy)
1898 f->op.destroy(f->userdata);
1899
1900 send_reply_ok(req, NULL, 0);
1901}
1902
Dees_Troye34c1332013-02-06 19:13:00 +00001903static void list_del_nreq(struct fuse_notify_req *nreq)
1904{
1905 struct fuse_notify_req *prev = nreq->prev;
1906 struct fuse_notify_req *next = nreq->next;
1907 prev->next = next;
1908 next->prev = prev;
1909}
1910
1911static void list_add_nreq(struct fuse_notify_req *nreq,
1912 struct fuse_notify_req *next)
1913{
1914 struct fuse_notify_req *prev = next->prev;
1915 nreq->next = next;
1916 nreq->prev = prev;
1917 prev->next = nreq;
1918 next->prev = nreq;
1919}
1920
1921static void list_init_nreq(struct fuse_notify_req *nreq)
1922{
1923 nreq->next = nreq;
1924 nreq->prev = nreq;
1925}
1926
1927static void do_notify_reply(fuse_req_t req, fuse_ino_t nodeid,
1928 const void *inarg, const struct fuse_buf *buf)
1929{
1930 struct fuse_ll *f = req->f;
1931 struct fuse_notify_req *nreq;
1932 struct fuse_notify_req *head;
1933
1934 pthread_mutex_lock(&f->lock);
1935 head = &f->notify_list;
1936 for (nreq = head->next; nreq != head; nreq = nreq->next) {
1937 if (nreq->unique == req->unique) {
1938 list_del_nreq(nreq);
1939 break;
1940 }
1941 }
1942 pthread_mutex_unlock(&f->lock);
1943
1944 if (nreq != head)
1945 nreq->reply(nreq, req, nodeid, inarg, buf);
1946}
1947
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001948static int send_notify_iov(struct fuse_ll *f, struct fuse_chan *ch,
1949 int notify_code, struct iovec *iov, int count)
1950{
1951 struct fuse_out_header out;
1952
Dees_Troye34c1332013-02-06 19:13:00 +00001953 if (!f->got_init)
1954 return -ENOTCONN;
1955
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001956 out.unique = 0;
1957 out.error = notify_code;
1958 iov[0].iov_base = &out;
1959 iov[0].iov_len = sizeof(struct fuse_out_header);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001960
Dees_Troye34c1332013-02-06 19:13:00 +00001961 return fuse_send_msg(f, ch, iov, count);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001962}
1963
1964int fuse_lowlevel_notify_poll(struct fuse_pollhandle *ph)
1965{
1966 if (ph != NULL) {
1967 struct fuse_notify_poll_wakeup_out outarg;
1968 struct iovec iov[2];
1969
1970 outarg.kh = ph->kh;
1971
1972 iov[1].iov_base = &outarg;
1973 iov[1].iov_len = sizeof(outarg);
1974
1975 return send_notify_iov(ph->f, ph->ch, FUSE_NOTIFY_POLL, iov, 2);
1976 } else {
1977 return 0;
1978 }
1979}
1980
1981int fuse_lowlevel_notify_inval_inode(struct fuse_chan *ch, fuse_ino_t ino,
1982 off64_t off, off64_t len)
1983{
1984 struct fuse_notify_inval_inode_out outarg;
1985 struct fuse_ll *f;
1986 struct iovec iov[2];
1987
1988 if (!ch)
1989 return -EINVAL;
1990
1991 f = (struct fuse_ll *)fuse_session_data(fuse_chan_session(ch));
1992 if (!f)
1993 return -ENODEV;
1994
1995 outarg.ino = ino;
1996 outarg.off = off;
1997 outarg.len = len;
1998
1999 iov[1].iov_base = &outarg;
2000 iov[1].iov_len = sizeof(outarg);
2001
2002 return send_notify_iov(f, ch, FUSE_NOTIFY_INVAL_INODE, iov, 2);
2003}
2004
2005int fuse_lowlevel_notify_inval_entry(struct fuse_chan *ch, fuse_ino_t parent,
2006 const char *name, size_t namelen)
2007{
2008 struct fuse_notify_inval_entry_out outarg;
2009 struct fuse_ll *f;
2010 struct iovec iov[3];
2011
2012 if (!ch)
2013 return -EINVAL;
2014
2015 f = (struct fuse_ll *)fuse_session_data(fuse_chan_session(ch));
2016 if (!f)
2017 return -ENODEV;
2018
2019 outarg.parent = parent;
2020 outarg.namelen = namelen;
2021 outarg.padding = 0;
2022
2023 iov[1].iov_base = &outarg;
2024 iov[1].iov_len = sizeof(outarg);
2025 iov[2].iov_base = (void *)name;
2026 iov[2].iov_len = namelen + 1;
2027
2028 return send_notify_iov(f, ch, FUSE_NOTIFY_INVAL_ENTRY, iov, 3);
2029}
2030
Dees_Troye34c1332013-02-06 19:13:00 +00002031int fuse_lowlevel_notify_delete(struct fuse_chan *ch,
2032 fuse_ino_t parent, fuse_ino_t child,
2033 const char *name, size_t namelen)
2034{
2035 struct fuse_notify_delete_out outarg;
2036 struct fuse_ll *f;
2037 struct iovec iov[3];
2038
2039 if (!ch)
2040 return -EINVAL;
2041
2042 f = (struct fuse_ll *)fuse_session_data(fuse_chan_session(ch));
2043 if (!f)
2044 return -ENODEV;
2045
2046 if (f->conn.proto_minor < 18)
2047 return -ENOSYS;
2048
2049 outarg.parent = parent;
2050 outarg.child = child;
2051 outarg.namelen = namelen;
2052 outarg.padding = 0;
2053
2054 iov[1].iov_base = &outarg;
2055 iov[1].iov_len = sizeof(outarg);
2056 iov[2].iov_base = (void *)name;
2057 iov[2].iov_len = namelen + 1;
2058
2059 return send_notify_iov(f, ch, FUSE_NOTIFY_DELETE, iov, 3);
2060}
2061
2062int fuse_lowlevel_notify_store(struct fuse_chan *ch, fuse_ino_t ino,
2063 off64_t offset, struct fuse_bufvec *bufv,
2064 enum fuse_buf_copy_flags flags)
2065{
2066 struct fuse_out_header out;
2067 struct fuse_notify_store_out outarg;
2068 struct fuse_ll *f;
2069 struct iovec iov[3];
2070 size_t size = fuse_buf_size(bufv);
2071 int res;
2072
2073 if (!ch)
2074 return -EINVAL;
2075
2076 f = (struct fuse_ll *)fuse_session_data(fuse_chan_session(ch));
2077 if (!f)
2078 return -ENODEV;
2079
2080 if (f->conn.proto_minor < 15)
2081 return -ENOSYS;
2082
2083 out.unique = 0;
2084 out.error = FUSE_NOTIFY_STORE;
2085
2086 outarg.nodeid = ino;
2087 outarg.offset = offset;
2088 outarg.size = size;
2089
2090 iov[0].iov_base = &out;
2091 iov[0].iov_len = sizeof(out);
2092 iov[1].iov_base = &outarg;
2093 iov[1].iov_len = sizeof(outarg);
2094
2095 res = fuse_send_data_iov(f, ch, iov, 2, bufv, flags);
2096 if (res > 0)
2097 res = -res;
2098
2099 return res;
2100}
2101
2102struct fuse_retrieve_req {
2103 struct fuse_notify_req nreq;
2104 void *cookie;
2105};
2106
2107static void fuse_ll_retrieve_reply(struct fuse_notify_req *nreq,
2108 fuse_req_t req, fuse_ino_t ino,
2109 const void *inarg,
2110 const struct fuse_buf *ibuf)
2111{
2112 struct fuse_ll *f = req->f;
2113 struct fuse_retrieve_req *rreq =
2114 container_of(nreq, struct fuse_retrieve_req, nreq);
2115 const struct fuse_notify_retrieve_in *arg = inarg;
2116 struct fuse_bufvec bufv = {
2117 .buf[0] = *ibuf,
2118 .count = 1,
2119 };
2120
2121 if (!(bufv.buf[0].flags & FUSE_BUF_IS_FD))
2122 bufv.buf[0].mem = PARAM(arg);
2123
2124 bufv.buf[0].size -= sizeof(struct fuse_in_header) +
2125 sizeof(struct fuse_notify_retrieve_in);
2126
2127 if (bufv.buf[0].size < arg->size) {
2128 fprintf(stderr, "fuse: retrieve reply: buffer size too small\n");
2129 fuse_reply_none(req);
2130 goto out;
2131 }
2132 bufv.buf[0].size = arg->size;
2133
2134 if (req->f->op.retrieve_reply) {
2135 req->f->op.retrieve_reply(req, rreq->cookie, ino,
2136 arg->offset, &bufv);
2137 } else {
2138 fuse_reply_none(req);
2139 }
2140out:
2141 free(rreq);
2142 if ((ibuf->flags & FUSE_BUF_IS_FD) && bufv.idx < bufv.count)
2143 fuse_ll_clear_pipe(f);
2144}
2145
2146int fuse_lowlevel_notify_retrieve(struct fuse_chan *ch, fuse_ino_t ino,
2147 size_t size, off64_t offset, void *cookie)
2148{
2149 struct fuse_notify_retrieve_out outarg;
2150 struct fuse_ll *f;
2151 struct iovec iov[2];
2152 struct fuse_retrieve_req *rreq;
2153 int err;
2154
2155 if (!ch)
2156 return -EINVAL;
2157
2158 f = (struct fuse_ll *)fuse_session_data(fuse_chan_session(ch));
2159 if (!f)
2160 return -ENODEV;
2161
2162 if (f->conn.proto_minor < 15)
2163 return -ENOSYS;
2164
2165 rreq = malloc(sizeof(*rreq));
2166 if (rreq == NULL)
2167 return -ENOMEM;
2168
2169 pthread_mutex_lock(&f->lock);
2170 rreq->cookie = cookie;
2171 rreq->nreq.unique = f->notify_ctr++;
2172 rreq->nreq.reply = fuse_ll_retrieve_reply;
2173 list_add_nreq(&rreq->nreq, &f->notify_list);
2174 pthread_mutex_unlock(&f->lock);
2175
2176 outarg.notify_unique = rreq->nreq.unique;
2177 outarg.nodeid = ino;
2178 outarg.offset = offset;
2179 outarg.size = size;
2180
2181 iov[1].iov_base = &outarg;
2182 iov[1].iov_len = sizeof(outarg);
2183
2184 err = send_notify_iov(f, ch, FUSE_NOTIFY_RETRIEVE, iov, 2);
2185 if (err) {
2186 pthread_mutex_lock(&f->lock);
2187 list_del_nreq(&rreq->nreq);
2188 pthread_mutex_unlock(&f->lock);
2189 free(rreq);
2190 }
2191
2192 return err;
2193}
2194
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002195void *fuse_req_userdata(fuse_req_t req)
2196{
2197 return req->f->userdata;
2198}
2199
2200const struct fuse_ctx *fuse_req_ctx(fuse_req_t req)
2201{
2202 return &req->ctx;
2203}
2204
2205/*
2206 * The size of fuse_ctx got extended, so need to be careful about
2207 * incompatibility (i.e. a new binary cannot work with an old
2208 * library).
2209 */
2210const struct fuse_ctx *fuse_req_ctx_compat24(fuse_req_t req);
2211const struct fuse_ctx *fuse_req_ctx_compat24(fuse_req_t req)
2212{
2213 return fuse_req_ctx(req);
2214}
Dees_Troye34c1332013-02-06 19:13:00 +00002215#ifndef __NetBSD__
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002216FUSE_SYMVER(".symver fuse_req_ctx_compat24,fuse_req_ctx@FUSE_2.4");
Dees_Troye34c1332013-02-06 19:13:00 +00002217#endif
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002218
2219
2220void fuse_req_interrupt_func(fuse_req_t req, fuse_interrupt_func_t func,
2221 void *data)
2222{
2223 pthread_mutex_lock(&req->lock);
2224 pthread_mutex_lock(&req->f->lock);
2225 req->u.ni.func = func;
2226 req->u.ni.data = data;
2227 pthread_mutex_unlock(&req->f->lock);
2228 if (req->interrupted && func)
2229 func(req, data);
2230 pthread_mutex_unlock(&req->lock);
2231}
2232
2233int fuse_req_interrupted(fuse_req_t req)
2234{
2235 int interrupted;
2236
2237 pthread_mutex_lock(&req->f->lock);
2238 interrupted = req->interrupted;
2239 pthread_mutex_unlock(&req->f->lock);
2240
2241 return interrupted;
2242}
2243
2244static struct {
2245 void (*func)(fuse_req_t, fuse_ino_t, const void *);
2246 const char *name;
2247} fuse_ll_ops[] = {
2248 [FUSE_LOOKUP] = { do_lookup, "LOOKUP" },
2249 [FUSE_FORGET] = { do_forget, "FORGET" },
2250 [FUSE_GETATTR] = { do_getattr, "GETATTR" },
2251 [FUSE_SETATTR] = { do_setattr, "SETATTR" },
2252 [FUSE_READLINK] = { do_readlink, "READLINK" },
2253 [FUSE_SYMLINK] = { do_symlink, "SYMLINK" },
2254 [FUSE_MKNOD] = { do_mknod, "MKNOD" },
2255 [FUSE_MKDIR] = { do_mkdir, "MKDIR" },
2256 [FUSE_UNLINK] = { do_unlink, "UNLINK" },
2257 [FUSE_RMDIR] = { do_rmdir, "RMDIR" },
2258 [FUSE_RENAME] = { do_rename, "RENAME" },
2259 [FUSE_LINK] = { do_link, "LINK" },
2260 [FUSE_OPEN] = { do_open, "OPEN" },
2261 [FUSE_READ] = { do_read, "READ" },
2262 [FUSE_WRITE] = { do_write, "WRITE" },
2263 [FUSE_STATFS] = { do_statfs, "STATFS" },
2264 [FUSE_RELEASE] = { do_release, "RELEASE" },
2265 [FUSE_FSYNC] = { do_fsync, "FSYNC" },
2266 [FUSE_SETXATTR] = { do_setxattr, "SETXATTR" },
2267 [FUSE_GETXATTR] = { do_getxattr, "GETXATTR" },
2268 [FUSE_LISTXATTR] = { do_listxattr, "LISTXATTR" },
2269 [FUSE_REMOVEXATTR] = { do_removexattr, "REMOVEXATTR" },
2270 [FUSE_FLUSH] = { do_flush, "FLUSH" },
2271 [FUSE_INIT] = { do_init, "INIT" },
2272 [FUSE_OPENDIR] = { do_opendir, "OPENDIR" },
2273 [FUSE_READDIR] = { do_readdir, "READDIR" },
2274 [FUSE_RELEASEDIR] = { do_releasedir, "RELEASEDIR" },
2275 [FUSE_FSYNCDIR] = { do_fsyncdir, "FSYNCDIR" },
2276 [FUSE_GETLK] = { do_getlk, "GETLK" },
2277 [FUSE_SETLK] = { do_setlk, "SETLK" },
2278 [FUSE_SETLKW] = { do_setlkw, "SETLKW" },
2279 [FUSE_ACCESS] = { do_access, "ACCESS" },
2280 [FUSE_CREATE] = { do_create, "CREATE" },
2281 [FUSE_INTERRUPT] = { do_interrupt, "INTERRUPT" },
2282 [FUSE_BMAP] = { do_bmap, "BMAP" },
2283 [FUSE_IOCTL] = { do_ioctl, "IOCTL" },
2284 [FUSE_POLL] = { do_poll, "POLL" },
Dees_Troye34c1332013-02-06 19:13:00 +00002285 [FUSE_FALLOCATE] = { do_fallocate, "FALLOCATE" },
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002286 [FUSE_DESTROY] = { do_destroy, "DESTROY" },
Dees_Troye34c1332013-02-06 19:13:00 +00002287 [FUSE_NOTIFY_REPLY] = { (void *) 1, "NOTIFY_REPLY" },
2288 [FUSE_BATCH_FORGET] = { do_batch_forget, "BATCH_FORGET" },
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002289 [CUSE_INIT] = { cuse_lowlevel_init, "CUSE_INIT" },
2290};
2291
2292#define FUSE_MAXOP (sizeof(fuse_ll_ops) / sizeof(fuse_ll_ops[0]))
2293
2294static const char *opname(enum fuse_opcode opcode)
2295{
2296 if (opcode >= FUSE_MAXOP || !fuse_ll_ops[opcode].name)
2297 return "???";
2298 else
2299 return fuse_ll_ops[opcode].name;
2300}
2301
Dees_Troye34c1332013-02-06 19:13:00 +00002302static int fuse_ll_copy_from_pipe(struct fuse_bufvec *dst,
2303 struct fuse_bufvec *src)
2304{
2305 int res = fuse_buf_copy(dst, src, 0);
2306 if (res < 0) {
2307 fprintf(stderr, "fuse: copy from pipe: %s\n", strerror(-res));
2308 return res;
2309 }
2310 if (res < fuse_buf_size(dst)) {
2311 fprintf(stderr, "fuse: copy from pipe: short read\n");
2312 return -1;
2313 }
2314 return 0;
2315}
2316
2317static void fuse_ll_process_buf(void *data, const struct fuse_buf *buf,
2318 struct fuse_chan *ch)
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002319{
2320 struct fuse_ll *f = (struct fuse_ll *) data;
Dees_Troye34c1332013-02-06 19:13:00 +00002321 const size_t write_header_size = sizeof(struct fuse_in_header) +
2322 sizeof(struct fuse_write_in);
2323 struct fuse_bufvec bufv = { .buf[0] = *buf, .count = 1 };
2324 struct fuse_bufvec tmpbuf = FUSE_BUFVEC_INIT(write_header_size);
2325 struct fuse_in_header *in;
2326 const void *inarg;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002327 struct fuse_req *req;
Dees_Troye34c1332013-02-06 19:13:00 +00002328 void *mbuf = NULL;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002329 int err;
Dees_Troye34c1332013-02-06 19:13:00 +00002330 int res;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002331
Dees_Troye34c1332013-02-06 19:13:00 +00002332 if (buf->flags & FUSE_BUF_IS_FD) {
2333 if (buf->size < tmpbuf.buf[0].size)
2334 tmpbuf.buf[0].size = buf->size;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002335
Dees_Troye34c1332013-02-06 19:13:00 +00002336 mbuf = malloc(tmpbuf.buf[0].size);
2337 if (mbuf == NULL) {
2338 fprintf(stderr, "fuse: failed to allocate header\n");
2339 goto clear_pipe;
2340 }
2341 tmpbuf.buf[0].mem = mbuf;
2342
2343 res = fuse_ll_copy_from_pipe(&tmpbuf, &bufv);
2344 if (res < 0)
2345 goto clear_pipe;
2346
2347 in = mbuf;
2348 } else {
2349 in = buf->mem;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002350 }
2351
Dees_Troye34c1332013-02-06 19:13:00 +00002352 if (f->debug) {
2353 fprintf(stderr,
2354 "unique: %llu, opcode: %s (%i), nodeid: %lu, insize: %zu, pid: %u\n",
2355 (unsigned long long) in->unique,
2356 opname((enum fuse_opcode) in->opcode), in->opcode,
2357 (unsigned long) in->nodeid, buf->size, in->pid);
2358 }
2359
2360 req = fuse_ll_alloc_req(f);
2361 if (req == NULL) {
2362 struct fuse_out_header out = {
2363 .unique = in->unique,
2364 .error = -ENOMEM,
2365 };
2366 struct iovec iov = {
2367 .iov_base = &out,
2368 .iov_len = sizeof(struct fuse_out_header),
2369 };
2370
2371 fuse_send_msg(f, ch, &iov, 1);
2372 goto clear_pipe;
2373 }
2374
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002375 req->unique = in->unique;
2376 req->ctx.uid = in->uid;
2377 req->ctx.gid = in->gid;
2378 req->ctx.pid = in->pid;
2379 req->ch = ch;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002380
2381 err = EIO;
2382 if (!f->got_init) {
2383 enum fuse_opcode expected;
2384
2385 expected = f->cuse_data ? CUSE_INIT : FUSE_INIT;
2386 if (in->opcode != expected)
2387 goto reply_err;
2388 } else if (in->opcode == FUSE_INIT || in->opcode == CUSE_INIT)
2389 goto reply_err;
2390
2391 err = EACCES;
2392 if (f->allow_root && in->uid != f->owner && in->uid != 0 &&
2393 in->opcode != FUSE_INIT && in->opcode != FUSE_READ &&
2394 in->opcode != FUSE_WRITE && in->opcode != FUSE_FSYNC &&
2395 in->opcode != FUSE_RELEASE && in->opcode != FUSE_READDIR &&
Dees_Troye34c1332013-02-06 19:13:00 +00002396 in->opcode != FUSE_FSYNCDIR && in->opcode != FUSE_RELEASEDIR &&
2397 in->opcode != FUSE_NOTIFY_REPLY)
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002398 goto reply_err;
2399
2400 err = ENOSYS;
2401 if (in->opcode >= FUSE_MAXOP || !fuse_ll_ops[in->opcode].func)
2402 goto reply_err;
2403 if (in->opcode != FUSE_INTERRUPT) {
2404 struct fuse_req *intr;
2405 pthread_mutex_lock(&f->lock);
2406 intr = check_interrupt(f, req);
2407 list_add_req(req, &f->list);
2408 pthread_mutex_unlock(&f->lock);
Dees_Troye34c1332013-02-06 19:13:00 +00002409 if (intr)
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002410 fuse_reply_err(intr, EAGAIN);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002411 }
Dees_Troye34c1332013-02-06 19:13:00 +00002412
2413 if ((buf->flags & FUSE_BUF_IS_FD) && write_header_size < buf->size &&
2414 (in->opcode != FUSE_WRITE || !f->op.write_buf) &&
2415 in->opcode != FUSE_NOTIFY_REPLY) {
2416 void *newmbuf;
2417
2418 err = ENOMEM;
2419 newmbuf = realloc(mbuf, buf->size);
2420 if (newmbuf == NULL)
2421 goto reply_err;
2422 mbuf = newmbuf;
2423
2424 tmpbuf = FUSE_BUFVEC_INIT(buf->size - write_header_size);
2425 tmpbuf.buf[0].mem = mbuf + write_header_size;
2426
2427 res = fuse_ll_copy_from_pipe(&tmpbuf, &bufv);
2428 err = -res;
2429 if (res < 0)
2430 goto reply_err;
2431
2432 in = mbuf;
2433 }
2434
2435 inarg = (void *) &in[1];
2436 if (in->opcode == FUSE_WRITE && f->op.write_buf)
2437 do_write_buf(req, in->nodeid, inarg, buf);
2438 else if (in->opcode == FUSE_NOTIFY_REPLY)
2439 do_notify_reply(req, in->nodeid, inarg, buf);
2440 else
2441 fuse_ll_ops[in->opcode].func(req, in->nodeid, inarg);
2442
2443out_free:
2444 free(mbuf);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002445 return;
2446
Dees_Troye34c1332013-02-06 19:13:00 +00002447reply_err:
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002448 fuse_reply_err(req, err);
Dees_Troye34c1332013-02-06 19:13:00 +00002449clear_pipe:
2450 if (buf->flags & FUSE_BUF_IS_FD)
2451 fuse_ll_clear_pipe(f);
2452 goto out_free;
2453}
2454
2455static void fuse_ll_process(void *data, const char *buf, size_t len,
2456 struct fuse_chan *ch)
2457{
2458 struct fuse_buf fbuf = {
2459 .mem = (void *) buf,
2460 .size = len,
2461 };
2462
2463 fuse_ll_process_buf(data, &fbuf, ch);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002464}
2465
2466enum {
2467 KEY_HELP,
2468 KEY_VERSION,
2469};
2470
Dees_Troye34c1332013-02-06 19:13:00 +00002471static const struct fuse_opt fuse_ll_opts[] = {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002472 { "debug", offsetof(struct fuse_ll, debug), 1 },
2473 { "-d", offsetof(struct fuse_ll, debug), 1 },
2474 { "allow_root", offsetof(struct fuse_ll, allow_root), 1 },
2475 { "max_write=%u", offsetof(struct fuse_ll, conn.max_write), 0 },
2476 { "max_readahead=%u", offsetof(struct fuse_ll, conn.max_readahead), 0 },
Dees_Troye34c1332013-02-06 19:13:00 +00002477 { "max_background=%u", offsetof(struct fuse_ll, conn.max_background), 0 },
2478 { "congestion_threshold=%u",
2479 offsetof(struct fuse_ll, conn.congestion_threshold), 0 },
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002480 { "async_read", offsetof(struct fuse_ll, conn.async_read), 1 },
2481 { "sync_read", offsetof(struct fuse_ll, conn.async_read), 0 },
2482 { "atomic_o_trunc", offsetof(struct fuse_ll, atomic_o_trunc), 1},
Dees_Troye34c1332013-02-06 19:13:00 +00002483 { "no_remote_lock", offsetof(struct fuse_ll, no_remote_posix_lock), 1},
2484 { "no_remote_lock", offsetof(struct fuse_ll, no_remote_flock), 1},
2485 { "no_remote_flock", offsetof(struct fuse_ll, no_remote_flock), 1},
2486 { "no_remote_posix_lock", offsetof(struct fuse_ll, no_remote_posix_lock), 1},
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002487 { "big_writes", offsetof(struct fuse_ll, big_writes), 1},
Dees_Troye34c1332013-02-06 19:13:00 +00002488 { "splice_write", offsetof(struct fuse_ll, splice_write), 1},
2489 { "no_splice_write", offsetof(struct fuse_ll, no_splice_write), 1},
2490 { "splice_move", offsetof(struct fuse_ll, splice_move), 1},
2491 { "no_splice_move", offsetof(struct fuse_ll, no_splice_move), 1},
2492 { "splice_read", offsetof(struct fuse_ll, splice_read), 1},
2493 { "no_splice_read", offsetof(struct fuse_ll, no_splice_read), 1},
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002494 FUSE_OPT_KEY("max_read=", FUSE_OPT_KEY_DISCARD),
2495 FUSE_OPT_KEY("-h", KEY_HELP),
2496 FUSE_OPT_KEY("--help", KEY_HELP),
2497 FUSE_OPT_KEY("-V", KEY_VERSION),
2498 FUSE_OPT_KEY("--version", KEY_VERSION),
2499 FUSE_OPT_END
2500};
2501
2502static void fuse_ll_version(void)
2503{
2504 fprintf(stderr, "using FUSE kernel interface version %i.%i\n",
2505 FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
2506}
2507
2508static void fuse_ll_help(void)
2509{
2510 fprintf(stderr,
2511" -o max_write=N set maximum size of write requests\n"
2512" -o max_readahead=N set maximum readahead\n"
Dees_Troye34c1332013-02-06 19:13:00 +00002513" -o max_background=N set number of maximum background requests\n"
2514" -o congestion_threshold=N set kernel's congestion threshold\n"
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002515" -o async_read perform reads asynchronously (default)\n"
2516" -o sync_read perform reads synchronously\n"
2517" -o atomic_o_trunc enable atomic open+truncate support\n"
2518" -o big_writes enable larger than 4kB writes\n"
Dees_Troye34c1332013-02-06 19:13:00 +00002519" -o no_remote_lock disable remote file locking\n"
2520" -o no_remote_flock disable remote file locking (BSD)\n"
2521" -o no_remote_posix_lock disable remove file locking (POSIX)\n"
2522" -o [no_]splice_write use splice to write to the fuse device\n"
2523" -o [no_]splice_move move data while splicing to the fuse device\n"
2524" -o [no_]splice_read use splice to read from the fuse device\n"
2525);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002526}
2527
2528static int fuse_ll_opt_proc(void *data, const char *arg, int key,
2529 struct fuse_args *outargs)
2530{
2531 (void) data; (void) outargs;
2532
2533 switch (key) {
2534 case KEY_HELP:
2535 fuse_ll_help();
2536 break;
2537
2538 case KEY_VERSION:
2539 fuse_ll_version();
2540 break;
2541
2542 default:
2543 fprintf(stderr, "fuse: unknown option `%s'\n", arg);
2544 }
2545
2546 return -1;
2547}
2548
2549int fuse_lowlevel_is_lib_option(const char *opt)
2550{
2551 return fuse_opt_match(fuse_ll_opts, opt);
2552}
2553
2554static void fuse_ll_destroy(void *data)
2555{
2556 struct fuse_ll *f = (struct fuse_ll *) data;
Dees_Troye34c1332013-02-06 19:13:00 +00002557 struct fuse_ll_pipe *llp;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002558
2559 if (f->got_init && !f->got_destroy) {
2560 if (f->op.destroy)
2561 f->op.destroy(f->userdata);
2562 }
Dees_Troye34c1332013-02-06 19:13:00 +00002563 llp = pthread_getspecific(f->pipe_key);
2564 if (llp != NULL)
2565 fuse_ll_pipe_free(llp);
2566 pthread_key_delete(f->pipe_key);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002567 pthread_mutex_destroy(&f->lock);
2568 free(f->cuse_data);
2569 free(f);
2570}
2571
Dees_Troye34c1332013-02-06 19:13:00 +00002572static void fuse_ll_pipe_destructor(void *data)
2573{
2574 struct fuse_ll_pipe *llp = data;
2575 fuse_ll_pipe_free(llp);
2576}
2577
2578#ifdef HAVE_SPLICE
2579static int fuse_ll_receive_buf(struct fuse_session *se, struct fuse_buf *buf,
2580 struct fuse_chan **chp)
2581{
2582 struct fuse_chan *ch = *chp;
2583 struct fuse_ll *f = fuse_session_data(se);
2584 size_t bufsize = buf->size;
2585 struct fuse_ll_pipe *llp;
2586 struct fuse_buf tmpbuf;
2587 int err;
2588 int res;
2589
2590 if (f->conn.proto_minor < 14 || !(f->conn.want & FUSE_CAP_SPLICE_READ))
2591 goto fallback;
2592
2593 llp = fuse_ll_get_pipe(f);
2594 if (llp == NULL)
2595 goto fallback;
2596
2597 if (llp->size < bufsize) {
2598 if (llp->can_grow) {
2599 res = fcntl(llp->pipe[0], F_SETPIPE_SZ, bufsize);
2600 if (res == -1) {
2601 llp->can_grow = 0;
2602 goto fallback;
2603 }
2604 llp->size = res;
2605 }
2606 if (llp->size < bufsize)
2607 goto fallback;
2608 }
2609
2610 res = splice(fuse_chan_fd(ch), NULL, llp->pipe[1], NULL, bufsize, 0);
2611 err = errno;
2612
2613 if (fuse_session_exited(se))
2614 return 0;
2615
2616 if (res == -1) {
2617 if (err == ENODEV) {
2618 fuse_session_exit(se);
2619 return 0;
2620 }
2621 if (err != EINTR && err != EAGAIN)
2622 perror("fuse: splice from device");
2623 return -err;
2624 }
2625
2626 if (res < sizeof(struct fuse_in_header)) {
2627 fprintf(stderr, "short splice from fuse device\n");
2628 return -EIO;
2629 }
2630
2631 tmpbuf = (struct fuse_buf) {
2632 .size = res,
2633 .flags = FUSE_BUF_IS_FD,
2634 .fd = llp->pipe[0],
2635 };
2636
2637 /*
2638 * Don't bother with zero copy for small requests.
2639 * fuse_loop_mt() needs to check for FORGET so this more than
2640 * just an optimization.
2641 */
2642 if (res < sizeof(struct fuse_in_header) +
2643 sizeof(struct fuse_write_in) + pagesize) {
2644 struct fuse_bufvec src = { .buf[0] = tmpbuf, .count = 1 };
2645 struct fuse_bufvec dst = { .buf[0] = *buf, .count = 1 };
2646
2647 res = fuse_buf_copy(&dst, &src, 0);
2648 if (res < 0) {
2649 fprintf(stderr, "fuse: copy from pipe: %s\n",
2650 strerror(-res));
2651 fuse_ll_clear_pipe(f);
2652 return res;
2653 }
2654 if (res < tmpbuf.size) {
2655 fprintf(stderr, "fuse: copy from pipe: short read\n");
2656 fuse_ll_clear_pipe(f);
2657 return -EIO;
2658 }
2659 buf->size = tmpbuf.size;
2660 return buf->size;
2661 }
2662
2663 *buf = tmpbuf;
2664
2665 return res;
2666
2667fallback:
2668 res = fuse_chan_recv(chp, buf->mem, bufsize);
2669 if (res <= 0)
2670 return res;
2671
2672 buf->size = res;
2673
2674 return res;
2675}
2676#else
2677static int fuse_ll_receive_buf(struct fuse_session *se, struct fuse_buf *buf,
2678 struct fuse_chan **chp)
2679{
2680 (void) se;
2681
2682 int res = fuse_chan_recv(chp, buf->mem, buf->size);
2683 if (res <= 0)
2684 return res;
2685
2686 buf->size = res;
2687
2688 return res;
2689}
2690#endif
2691
2692
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002693/*
2694 * always call fuse_lowlevel_new_common() internally, to work around a
2695 * misfeature in the FreeBSD runtime linker, which links the old
2696 * version of a symbol to internal references.
2697 */
2698struct fuse_session *fuse_lowlevel_new_common(struct fuse_args *args,
2699 const struct fuse_lowlevel_ops *op,
2700 size_t op_size, void *userdata)
2701{
Dees_Troye34c1332013-02-06 19:13:00 +00002702 int err;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002703 struct fuse_ll *f;
2704 struct fuse_session *se;
2705 struct fuse_session_ops sop = {
2706 .process = fuse_ll_process,
2707 .destroy = fuse_ll_destroy,
2708 };
2709
2710 if (sizeof(struct fuse_lowlevel_ops) < op_size) {
2711 fprintf(stderr, "fuse: warning: library too old, some operations may not work\n");
2712 op_size = sizeof(struct fuse_lowlevel_ops);
2713 }
2714
2715 f = (struct fuse_ll *) calloc(1, sizeof(struct fuse_ll));
2716 if (f == NULL) {
2717 fprintf(stderr, "fuse: failed to allocate fuse object\n");
2718 goto out;
2719 }
2720
2721 f->conn.async_read = 1;
2722 f->conn.max_write = UINT_MAX;
2723 f->conn.max_readahead = UINT_MAX;
2724 f->atomic_o_trunc = 0;
2725 list_init_req(&f->list);
2726 list_init_req(&f->interrupts);
Dees_Troye34c1332013-02-06 19:13:00 +00002727 list_init_nreq(&f->notify_list);
2728 f->notify_ctr = 1;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002729 fuse_mutex_init(&f->lock);
2730
Dees_Troye34c1332013-02-06 19:13:00 +00002731 err = pthread_key_create(&f->pipe_key, fuse_ll_pipe_destructor);
2732 if (err) {
2733 fprintf(stderr, "fuse: failed to create thread specific key: %s\n",
2734 strerror(err));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002735 goto out_free;
Dees_Troye34c1332013-02-06 19:13:00 +00002736 }
2737
2738 if (fuse_opt_parse(args, f, fuse_ll_opts, fuse_ll_opt_proc) == -1)
2739 goto out_key_destroy;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002740
2741 if (f->debug)
2742 fprintf(stderr, "FUSE library version: %s\n", PACKAGE_VERSION);
2743
2744 memcpy(&f->op, op, op_size);
2745 f->owner = getuid();
2746 f->userdata = userdata;
2747
2748 se = fuse_session_new(&sop, f);
2749 if (!se)
Dees_Troye34c1332013-02-06 19:13:00 +00002750 goto out_key_destroy;
2751
2752 se->receive_buf = fuse_ll_receive_buf;
2753 se->process_buf = fuse_ll_process_buf;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002754
2755 return se;
2756
Dees_Troye34c1332013-02-06 19:13:00 +00002757out_key_destroy:
2758 pthread_key_delete(f->pipe_key);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002759out_free:
Dees_Troye34c1332013-02-06 19:13:00 +00002760 pthread_mutex_destroy(&f->lock);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002761 free(f);
2762out:
2763 return NULL;
2764}
2765
2766
2767struct fuse_session *fuse_lowlevel_new(struct fuse_args *args,
2768 const struct fuse_lowlevel_ops *op,
2769 size_t op_size, void *userdata)
2770{
2771 return fuse_lowlevel_new_common(args, op, op_size, userdata);
2772}
2773
2774#ifdef linux
2775int fuse_req_getgroups(fuse_req_t req, int size, gid_t list[])
2776{
2777 char *buf;
2778 size_t bufsize = 1024;
2779 char path[128];
2780 int ret;
2781 int fd;
2782 unsigned long pid = req->ctx.pid;
2783 char *s;
2784
2785 sprintf(path, "/proc/%lu/task/%lu/status", pid, pid);
2786
2787retry:
2788 buf = malloc(bufsize);
2789 if (buf == NULL)
2790 return -ENOMEM;
2791
2792 ret = -EIO;
2793 fd = open(path, O_RDONLY);
2794 if (fd == -1)
2795 goto out_free;
2796
2797 ret = read(fd, buf, bufsize);
2798 close(fd);
2799 if (ret == -1) {
2800 ret = -EIO;
2801 goto out_free;
2802 }
2803
2804 if (ret == bufsize) {
2805 free(buf);
2806 bufsize *= 4;
2807 goto retry;
2808 }
2809
2810 ret = -EIO;
2811 s = strstr(buf, "\nGroups:");
2812 if (s == NULL)
2813 goto out_free;
2814
2815 s += 8;
2816 ret = 0;
2817 while (1) {
2818 char *end;
2819 unsigned long val = strtoul(s, &end, 0);
2820 if (end == s)
2821 break;
2822
2823 s = end;
2824 if (ret < size)
2825 list[ret] = val;
2826 ret++;
2827 }
2828
2829out_free:
2830 free(buf);
2831 return ret;
2832}
2833#else /* linux */
2834/*
2835 * This is currently not implemented on other than Linux...
2836 */
2837int fuse_req_getgroups(fuse_req_t req, int size, gid_t list[])
2838{
2839 return -ENOSYS;
2840}
2841#endif
2842
Dees_Troye34c1332013-02-06 19:13:00 +00002843#if !defined(__FreeBSD__) && !defined(__NetBSD__)
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002844
2845static void fill_open_compat(struct fuse_open_out *arg,
2846 const struct fuse_file_info_compat *f)
2847{
2848 arg->fh = f->fh;
2849 if (f->direct_io)
2850 arg->open_flags |= FOPEN_DIRECT_IO;
2851 if (f->keep_cache)
2852 arg->open_flags |= FOPEN_KEEP_CACHE;
2853}
2854
2855static void convert_statfs_compat(const struct statfs *compatbuf,
2856 struct statvfs *buf)
2857{
2858 buf->f_bsize = compatbuf->f_bsize;
2859 buf->f_blocks = compatbuf->f_blocks;
2860 buf->f_bfree = compatbuf->f_bfree;
2861 buf->f_bavail = compatbuf->f_bavail;
2862 buf->f_files = compatbuf->f_files;
2863 buf->f_ffree = compatbuf->f_ffree;
2864 buf->f_namemax = compatbuf->f_namelen;
2865}
2866
2867int fuse_reply_open_compat(fuse_req_t req,
2868 const struct fuse_file_info_compat *f)
2869{
2870 struct fuse_open_out arg;
2871
2872 memset(&arg, 0, sizeof(arg));
2873 fill_open_compat(&arg, f);
2874 return send_reply_ok(req, &arg, sizeof(arg));
2875}
2876
2877int fuse_reply_statfs_compat(fuse_req_t req, const struct statfs *stbuf)
2878{
2879 struct statvfs newbuf;
2880
2881 memset(&newbuf, 0, sizeof(newbuf));
2882 convert_statfs_compat(stbuf, &newbuf);
2883
2884 return fuse_reply_statfs(req, &newbuf);
2885}
2886
2887struct fuse_session *fuse_lowlevel_new_compat(const char *opts,
2888 const struct fuse_lowlevel_ops_compat *op,
2889 size_t op_size, void *userdata)
2890{
2891 struct fuse_session *se;
2892 struct fuse_args args = FUSE_ARGS_INIT(0, NULL);
2893
2894 if (opts &&
2895 (fuse_opt_add_arg(&args, "") == -1 ||
2896 fuse_opt_add_arg(&args, "-o") == -1 ||
2897 fuse_opt_add_arg(&args, opts) == -1)) {
2898 fuse_opt_free_args(&args);
2899 return NULL;
2900 }
2901 se = fuse_lowlevel_new(&args, (const struct fuse_lowlevel_ops *) op,
2902 op_size, userdata);
2903 fuse_opt_free_args(&args);
2904
2905 return se;
2906}
2907
2908struct fuse_ll_compat_conf {
2909 unsigned max_read;
2910 int set_max_read;
2911};
2912
2913static const struct fuse_opt fuse_ll_opts_compat[] = {
2914 { "max_read=", offsetof(struct fuse_ll_compat_conf, set_max_read), 1 },
2915 { "max_read=%u", offsetof(struct fuse_ll_compat_conf, max_read), 0 },
2916 FUSE_OPT_KEY("max_read=", FUSE_OPT_KEY_KEEP),
2917 FUSE_OPT_END
2918};
2919
2920int fuse_sync_compat_args(struct fuse_args *args)
2921{
2922 struct fuse_ll_compat_conf conf;
2923
2924 memset(&conf, 0, sizeof(conf));
2925 if (fuse_opt_parse(args, &conf, fuse_ll_opts_compat, NULL) == -1)
2926 return -1;
2927
2928 if (fuse_opt_insert_arg(args, 1, "-osync_read"))
2929 return -1;
2930
2931 if (conf.set_max_read) {
2932 char tmpbuf[64];
2933
2934 sprintf(tmpbuf, "-omax_readahead=%u", conf.max_read);
2935 if (fuse_opt_insert_arg(args, 1, tmpbuf) == -1)
2936 return -1;
2937 }
2938 return 0;
2939}
2940
2941FUSE_SYMVER(".symver fuse_reply_statfs_compat,fuse_reply_statfs@FUSE_2.4");
2942FUSE_SYMVER(".symver fuse_reply_open_compat,fuse_reply_open@FUSE_2.4");
2943FUSE_SYMVER(".symver fuse_lowlevel_new_compat,fuse_lowlevel_new@FUSE_2.4");
2944
Dees_Troye34c1332013-02-06 19:13:00 +00002945#else /* __FreeBSD__ || __NetBSD__ */
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002946
2947int fuse_sync_compat_args(struct fuse_args *args)
2948{
2949 (void) args;
2950 return 0;
2951}
2952
Dees_Troye34c1332013-02-06 19:13:00 +00002953#endif /* __FreeBSD__ || __NetBSD__ */
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002954
2955struct fuse_session *fuse_lowlevel_new_compat25(struct fuse_args *args,
2956 const struct fuse_lowlevel_ops_compat25 *op,
2957 size_t op_size, void *userdata)
2958{
2959 if (fuse_sync_compat_args(args) == -1)
2960 return NULL;
2961
2962 return fuse_lowlevel_new_common(args,
2963 (const struct fuse_lowlevel_ops *) op,
2964 op_size, userdata);
2965}
2966
2967FUSE_SYMVER(".symver fuse_lowlevel_new_compat25,fuse_lowlevel_new@FUSE_2.5");