libfuse

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