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