libfuse: fuse-3.9.4/lib/fuse_lowlevel.c Source File

libfuse

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