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