1
2
3
4
5
6
7
8
9
10
11
12#include "qemu/osdep.h"
13#include "fuse_i.h"
14#include "standard-headers/linux/fuse.h"
15#include "fuse_misc.h"
16#include "fuse_opt.h"
17#include "fuse_virtio.h"
18
19#include <sys/file.h>
20
21#define THREAD_POOL_SIZE 0
22
23#define OFFSET_MAX 0x7fffffffffffffffLL
24
25struct fuse_pollhandle {
26 uint64_t kh;
27 struct fuse_session *se;
28};
29
30static size_t pagesize;
31
32static __attribute__((constructor)) void fuse_ll_init_pagesize(void)
33{
34 pagesize = getpagesize();
35}
36
37static void convert_stat(const struct stat *stbuf, struct fuse_attr *attr)
38{
39 *attr = (struct fuse_attr){
40 .ino = stbuf->st_ino,
41 .mode = stbuf->st_mode,
42 .nlink = stbuf->st_nlink,
43 .uid = stbuf->st_uid,
44 .gid = stbuf->st_gid,
45 .rdev = stbuf->st_rdev,
46 .size = stbuf->st_size,
47 .blksize = stbuf->st_blksize,
48 .blocks = stbuf->st_blocks,
49 .atime = stbuf->st_atime,
50 .mtime = stbuf->st_mtime,
51 .ctime = stbuf->st_ctime,
52 .atimensec = ST_ATIM_NSEC(stbuf),
53 .mtimensec = ST_MTIM_NSEC(stbuf),
54 .ctimensec = ST_CTIM_NSEC(stbuf),
55 };
56}
57
58static void convert_attr(const struct fuse_setattr_in *attr, struct stat *stbuf)
59{
60 stbuf->st_mode = attr->mode;
61 stbuf->st_uid = attr->uid;
62 stbuf->st_gid = attr->gid;
63 stbuf->st_size = attr->size;
64 stbuf->st_atime = attr->atime;
65 stbuf->st_mtime = attr->mtime;
66 stbuf->st_ctime = attr->ctime;
67 ST_ATIM_NSEC_SET(stbuf, attr->atimensec);
68 ST_MTIM_NSEC_SET(stbuf, attr->mtimensec);
69 ST_CTIM_NSEC_SET(stbuf, attr->ctimensec);
70}
71
72static size_t iov_length(const struct iovec *iov, size_t count)
73{
74 size_t seg;
75 size_t ret = 0;
76
77 for (seg = 0; seg < count; seg++) {
78 ret += iov[seg].iov_len;
79 }
80 return ret;
81}
82
83static void list_init_req(struct fuse_req *req)
84{
85 req->next = req;
86 req->prev = req;
87}
88
89static void list_del_req(struct fuse_req *req)
90{
91 struct fuse_req *prev = req->prev;
92 struct fuse_req *next = req->next;
93 prev->next = next;
94 next->prev = prev;
95}
96
97static void list_add_req(struct fuse_req *req, struct fuse_req *next)
98{
99 struct fuse_req *prev = next->prev;
100 req->next = next;
101 req->prev = prev;
102 prev->next = req;
103 next->prev = req;
104}
105
106static void destroy_req(fuse_req_t req)
107{
108 pthread_mutex_destroy(&req->lock);
109 free(req);
110}
111
112void fuse_free_req(fuse_req_t req)
113{
114 int ctr;
115 struct fuse_session *se = req->se;
116
117 pthread_mutex_lock(&se->lock);
118 req->u.ni.func = NULL;
119 req->u.ni.data = NULL;
120 list_del_req(req);
121 ctr = --req->ctr;
122 req->ch = NULL;
123 pthread_mutex_unlock(&se->lock);
124 if (!ctr) {
125 destroy_req(req);
126 }
127}
128
129static struct fuse_req *fuse_ll_alloc_req(struct fuse_session *se)
130{
131 struct fuse_req *req;
132
133 req = (struct fuse_req *)calloc(1, sizeof(struct fuse_req));
134 if (req == NULL) {
135 fuse_log(FUSE_LOG_ERR, "fuse: failed to allocate request\n");
136 } else {
137 req->se = se;
138 req->ctr = 1;
139 list_init_req(req);
140 fuse_mutex_init(&req->lock);
141 }
142
143 return req;
144}
145
146
147static int fuse_send_msg(struct fuse_session *se, struct fuse_chan *ch,
148 struct iovec *iov, int count)
149{
150 struct fuse_out_header *out = iov[0].iov_base;
151
152 out->len = iov_length(iov, count);
153 if (out->unique == 0) {
154 fuse_log(FUSE_LOG_DEBUG, "NOTIFY: code=%d length=%u\n", out->error,
155 out->len);
156 } else if (out->error) {
157 fuse_log(FUSE_LOG_DEBUG,
158 " unique: %llu, error: %i (%s), outsize: %i\n",
159 (unsigned long long)out->unique, out->error,
160 strerror(-out->error), out->len);
161 } else {
162 fuse_log(FUSE_LOG_DEBUG, " unique: %llu, success, outsize: %i\n",
163 (unsigned long long)out->unique, out->len);
164 }
165
166 if (fuse_lowlevel_is_virtio(se)) {
167 return virtio_send_msg(se, ch, iov, count);
168 }
169
170 abort();
171 return 0;
172}
173
174
175int fuse_send_reply_iov_nofree(fuse_req_t req, int error, struct iovec *iov,
176 int count)
177{
178 struct fuse_out_header out = {
179 .unique = req->unique,
180 .error = error,
181 };
182
183 if (error <= -1000 || error > 0) {
184 fuse_log(FUSE_LOG_ERR, "fuse: bad error value: %i\n", error);
185 out.error = -ERANGE;
186 }
187
188 iov[0].iov_base = &out;
189 iov[0].iov_len = sizeof(struct fuse_out_header);
190
191 return fuse_send_msg(req->se, req->ch, iov, count);
192}
193
194static int send_reply_iov(fuse_req_t req, int error, struct iovec *iov,
195 int count)
196{
197 int res;
198
199 res = fuse_send_reply_iov_nofree(req, error, iov, count);
200 fuse_free_req(req);
201 return res;
202}
203
204static int send_reply(fuse_req_t req, int error, const void *arg,
205 size_t argsize)
206{
207 struct iovec iov[2];
208 int count = 1;
209 if (argsize) {
210 iov[1].iov_base = (void *)arg;
211 iov[1].iov_len = argsize;
212 count++;
213 }
214 return send_reply_iov(req, error, iov, count);
215}
216
217int fuse_reply_iov(fuse_req_t req, const struct iovec *iov, int count)
218{
219 int res;
220 struct iovec *padded_iov;
221
222 padded_iov = malloc((count + 1) * sizeof(struct iovec));
223 if (padded_iov == NULL) {
224 return fuse_reply_err(req, ENOMEM);
225 }
226
227 memcpy(padded_iov + 1, iov, count * sizeof(struct iovec));
228 count++;
229
230 res = send_reply_iov(req, 0, padded_iov, count);
231 free(padded_iov);
232
233 return res;
234}
235
236
237
238
239
240
241size_t fuse_add_direntry(fuse_req_t req, char *buf, size_t bufsize,
242 const char *name, const struct stat *stbuf, off_t off)
243{
244 (void)req;
245 size_t namelen;
246 size_t entlen;
247 size_t entlen_padded;
248 struct fuse_dirent *dirent;
249
250 namelen = strlen(name);
251 entlen = FUSE_NAME_OFFSET + namelen;
252 entlen_padded = FUSE_DIRENT_ALIGN(entlen);
253
254 if ((buf == NULL) || (entlen_padded > bufsize)) {
255 return entlen_padded;
256 }
257
258 dirent = (struct fuse_dirent *)buf;
259 dirent->ino = stbuf->st_ino;
260 dirent->off = off;
261 dirent->namelen = namelen;
262 dirent->type = (stbuf->st_mode & S_IFMT) >> 12;
263 memcpy(dirent->name, name, namelen);
264 memset(dirent->name + namelen, 0, entlen_padded - entlen);
265
266 return entlen_padded;
267}
268
269static void convert_statfs(const struct statvfs *stbuf,
270 struct fuse_kstatfs *kstatfs)
271{
272 *kstatfs = (struct fuse_kstatfs){
273 .bsize = stbuf->f_bsize,
274 .frsize = stbuf->f_frsize,
275 .blocks = stbuf->f_blocks,
276 .bfree = stbuf->f_bfree,
277 .bavail = stbuf->f_bavail,
278 .files = stbuf->f_files,
279 .ffree = stbuf->f_ffree,
280 .namelen = stbuf->f_namemax,
281 };
282}
283
284static int send_reply_ok(fuse_req_t req, const void *arg, size_t argsize)
285{
286 return send_reply(req, 0, arg, argsize);
287}
288
289int fuse_reply_err(fuse_req_t req, int err)
290{
291 return send_reply(req, -err, NULL, 0);
292}
293
294void fuse_reply_none(fuse_req_t req)
295{
296 fuse_free_req(req);
297}
298
299static unsigned long calc_timeout_sec(double t)
300{
301 if (t > (double)ULONG_MAX) {
302 return ULONG_MAX;
303 } else if (t < 0.0) {
304 return 0;
305 } else {
306 return (unsigned long)t;
307 }
308}
309
310static unsigned int calc_timeout_nsec(double t)
311{
312 double f = t - (double)calc_timeout_sec(t);
313 if (f < 0.0) {
314 return 0;
315 } else if (f >= 0.999999999) {
316 return 999999999;
317 } else {
318 return (unsigned int)(f * 1.0e9);
319 }
320}
321
322static void fill_entry(struct fuse_entry_out *arg,
323 const struct fuse_entry_param *e)
324{
325 *arg = (struct fuse_entry_out){
326 .nodeid = e->ino,
327 .generation = e->generation,
328 .entry_valid = calc_timeout_sec(e->entry_timeout),
329 .entry_valid_nsec = calc_timeout_nsec(e->entry_timeout),
330 .attr_valid = calc_timeout_sec(e->attr_timeout),
331 .attr_valid_nsec = calc_timeout_nsec(e->attr_timeout),
332 };
333 convert_stat(&e->attr, &arg->attr);
334
335 arg->attr.flags = e->attr_flags;
336}
337
338
339
340
341
342size_t fuse_add_direntry_plus(fuse_req_t req, char *buf, size_t bufsize,
343 const char *name,
344 const struct fuse_entry_param *e, off_t off)
345{
346 (void)req;
347 size_t namelen;
348 size_t entlen;
349 size_t entlen_padded;
350
351 namelen = strlen(name);
352 entlen = FUSE_NAME_OFFSET_DIRENTPLUS + namelen;
353 entlen_padded = FUSE_DIRENT_ALIGN(entlen);
354 if ((buf == NULL) || (entlen_padded > bufsize)) {
355 return entlen_padded;
356 }
357
358 struct fuse_direntplus *dp = (struct fuse_direntplus *)buf;
359 memset(&dp->entry_out, 0, sizeof(dp->entry_out));
360 fill_entry(&dp->entry_out, e);
361
362 struct fuse_dirent *dirent = &dp->dirent;
363 *dirent = (struct fuse_dirent){
364 .ino = e->attr.st_ino,
365 .off = off,
366 .namelen = namelen,
367 .type = (e->attr.st_mode & S_IFMT) >> 12,
368 };
369 memcpy(dirent->name, name, namelen);
370 memset(dirent->name + namelen, 0, entlen_padded - entlen);
371
372 return entlen_padded;
373}
374
375static void fill_open(struct fuse_open_out *arg, const struct fuse_file_info *f)
376{
377 arg->fh = f->fh;
378 if (f->direct_io) {
379 arg->open_flags |= FOPEN_DIRECT_IO;
380 }
381 if (f->keep_cache) {
382 arg->open_flags |= FOPEN_KEEP_CACHE;
383 }
384 if (f->cache_readdir) {
385 arg->open_flags |= FOPEN_CACHE_DIR;
386 }
387 if (f->nonseekable) {
388 arg->open_flags |= FOPEN_NONSEEKABLE;
389 }
390}
391
392int fuse_reply_entry(fuse_req_t req, const struct fuse_entry_param *e)
393{
394 struct fuse_entry_out arg;
395 size_t size = sizeof(arg);
396
397 memset(&arg, 0, sizeof(arg));
398 fill_entry(&arg, e);
399 return send_reply_ok(req, &arg, size);
400}
401
402int fuse_reply_create(fuse_req_t req, const struct fuse_entry_param *e,
403 const struct fuse_file_info *f)
404{
405 char buf[sizeof(struct fuse_entry_out) + sizeof(struct fuse_open_out)];
406 size_t entrysize = sizeof(struct fuse_entry_out);
407 struct fuse_entry_out *earg = (struct fuse_entry_out *)buf;
408 struct fuse_open_out *oarg = (struct fuse_open_out *)(buf + entrysize);
409
410 memset(buf, 0, sizeof(buf));
411 fill_entry(earg, e);
412 fill_open(oarg, f);
413 return send_reply_ok(req, buf, entrysize + sizeof(struct fuse_open_out));
414}
415
416int fuse_reply_attr(fuse_req_t req, const struct stat *attr,
417 double attr_timeout)
418{
419 struct fuse_attr_out arg;
420 size_t size = sizeof(arg);
421
422 memset(&arg, 0, sizeof(arg));
423 arg.attr_valid = calc_timeout_sec(attr_timeout);
424 arg.attr_valid_nsec = calc_timeout_nsec(attr_timeout);
425 convert_stat(attr, &arg.attr);
426
427 return send_reply_ok(req, &arg, size);
428}
429
430int fuse_reply_readlink(fuse_req_t req, const char *linkname)
431{
432 return send_reply_ok(req, linkname, strlen(linkname));
433}
434
435int fuse_reply_open(fuse_req_t req, const struct fuse_file_info *f)
436{
437 struct fuse_open_out arg;
438
439 memset(&arg, 0, sizeof(arg));
440 fill_open(&arg, f);
441 return send_reply_ok(req, &arg, sizeof(arg));
442}
443
444int fuse_reply_write(fuse_req_t req, size_t count)
445{
446 struct fuse_write_out arg;
447
448 memset(&arg, 0, sizeof(arg));
449 arg.size = count;
450
451 return send_reply_ok(req, &arg, sizeof(arg));
452}
453
454int fuse_reply_buf(fuse_req_t req, const char *buf, size_t size)
455{
456 return send_reply_ok(req, buf, size);
457}
458
459static int fuse_send_data_iov_fallback(struct fuse_session *se,
460 struct fuse_chan *ch, struct iovec *iov,
461 int iov_count, struct fuse_bufvec *buf,
462 size_t len)
463{
464
465 if (buf->count == 1 && buf->idx == 0 && buf->off == 0 &&
466 !(buf->buf[0].flags & FUSE_BUF_IS_FD)) {
467
468
469
470
471
472 iov[iov_count].iov_base = buf->buf[0].mem;
473 iov[iov_count].iov_len = len;
474 iov_count++;
475 return fuse_send_msg(se, ch, iov, iov_count);
476 }
477
478 if (fuse_lowlevel_is_virtio(se) && buf->count == 1 &&
479 buf->buf[0].flags == (FUSE_BUF_IS_FD | FUSE_BUF_FD_SEEK)) {
480 return virtio_send_data_iov(se, ch, iov, iov_count, buf, len);
481 }
482
483 abort();
484 return 0;
485}
486
487static int fuse_send_data_iov(struct fuse_session *se, struct fuse_chan *ch,
488 struct iovec *iov, int iov_count,
489 struct fuse_bufvec *buf)
490{
491 size_t len = fuse_buf_size(buf);
492
493 return fuse_send_data_iov_fallback(se, ch, iov, iov_count, buf, len);
494}
495
496int fuse_reply_data(fuse_req_t req, struct fuse_bufvec *bufv)
497{
498 struct iovec iov[2];
499 struct fuse_out_header out = {
500 .unique = req->unique,
501 };
502 int res;
503
504 iov[0].iov_base = &out;
505 iov[0].iov_len = sizeof(struct fuse_out_header);
506
507 res = fuse_send_data_iov(req->se, req->ch, iov, 1, bufv);
508 if (res <= 0) {
509 fuse_free_req(req);
510 return res;
511 } else {
512 return fuse_reply_err(req, res);
513 }
514}
515
516int fuse_reply_statfs(fuse_req_t req, const struct statvfs *stbuf)
517{
518 struct fuse_statfs_out arg;
519 size_t size = sizeof(arg);
520
521 memset(&arg, 0, sizeof(arg));
522 convert_statfs(stbuf, &arg.st);
523
524 return send_reply_ok(req, &arg, size);
525}
526
527int fuse_reply_xattr(fuse_req_t req, size_t count)
528{
529 struct fuse_getxattr_out arg;
530
531 memset(&arg, 0, sizeof(arg));
532 arg.size = count;
533
534 return send_reply_ok(req, &arg, sizeof(arg));
535}
536
537int fuse_reply_lock(fuse_req_t req, const struct flock *lock)
538{
539 struct fuse_lk_out arg;
540
541 memset(&arg, 0, sizeof(arg));
542 arg.lk.type = lock->l_type;
543 if (lock->l_type != F_UNLCK) {
544 arg.lk.start = lock->l_start;
545 if (lock->l_len == 0) {
546 arg.lk.end = OFFSET_MAX;
547 } else {
548 arg.lk.end = lock->l_start + lock->l_len - 1;
549 }
550 }
551 arg.lk.pid = lock->l_pid;
552 return send_reply_ok(req, &arg, sizeof(arg));
553}
554
555int fuse_reply_bmap(fuse_req_t req, uint64_t idx)
556{
557 struct fuse_bmap_out arg;
558
559 memset(&arg, 0, sizeof(arg));
560 arg.block = idx;
561
562 return send_reply_ok(req, &arg, sizeof(arg));
563}
564
565static struct fuse_ioctl_iovec *fuse_ioctl_iovec_copy(const struct iovec *iov,
566 size_t count)
567{
568 struct fuse_ioctl_iovec *fiov;
569 size_t i;
570
571 fiov = malloc(sizeof(fiov[0]) * count);
572 if (!fiov) {
573 return NULL;
574 }
575
576 for (i = 0; i < count; i++) {
577 fiov[i].base = (uintptr_t)iov[i].iov_base;
578 fiov[i].len = iov[i].iov_len;
579 }
580
581 return fiov;
582}
583
584int fuse_reply_ioctl_retry(fuse_req_t req, const struct iovec *in_iov,
585 size_t in_count, const struct iovec *out_iov,
586 size_t out_count)
587{
588 struct fuse_ioctl_out arg;
589 struct fuse_ioctl_iovec *in_fiov = NULL;
590 struct fuse_ioctl_iovec *out_fiov = NULL;
591 struct iovec iov[4];
592 size_t count = 1;
593 int res;
594
595 memset(&arg, 0, sizeof(arg));
596 arg.flags |= FUSE_IOCTL_RETRY;
597 arg.in_iovs = in_count;
598 arg.out_iovs = out_count;
599 iov[count].iov_base = &arg;
600 iov[count].iov_len = sizeof(arg);
601 count++;
602
603
604 if (sizeof(void *) == 4 && req->ioctl_64bit) {
605 res = fuse_reply_err(req, EINVAL);
606 goto out;
607 }
608
609 if (in_count) {
610 in_fiov = fuse_ioctl_iovec_copy(in_iov, in_count);
611 if (!in_fiov) {
612 goto enomem;
613 }
614
615 iov[count].iov_base = (void *)in_fiov;
616 iov[count].iov_len = sizeof(in_fiov[0]) * in_count;
617 count++;
618 }
619 if (out_count) {
620 out_fiov = fuse_ioctl_iovec_copy(out_iov, out_count);
621 if (!out_fiov) {
622 goto enomem;
623 }
624
625 iov[count].iov_base = (void *)out_fiov;
626 iov[count].iov_len = sizeof(out_fiov[0]) * out_count;
627 count++;
628 }
629
630 res = send_reply_iov(req, 0, iov, count);
631out:
632 free(in_fiov);
633 free(out_fiov);
634
635 return res;
636
637enomem:
638 res = fuse_reply_err(req, ENOMEM);
639 goto out;
640}
641
642int fuse_reply_ioctl(fuse_req_t req, int result, const void *buf, size_t size)
643{
644 struct fuse_ioctl_out arg;
645 struct iovec iov[3];
646 size_t count = 1;
647
648 memset(&arg, 0, sizeof(arg));
649 arg.result = result;
650 iov[count].iov_base = &arg;
651 iov[count].iov_len = sizeof(arg);
652 count++;
653
654 if (size) {
655 iov[count].iov_base = (char *)buf;
656 iov[count].iov_len = size;
657 count++;
658 }
659
660 return send_reply_iov(req, 0, iov, count);
661}
662
663int fuse_reply_ioctl_iov(fuse_req_t req, int result, const struct iovec *iov,
664 int count)
665{
666 struct iovec *padded_iov;
667 struct fuse_ioctl_out arg;
668 int res;
669
670 padded_iov = malloc((count + 2) * sizeof(struct iovec));
671 if (padded_iov == NULL) {
672 return fuse_reply_err(req, ENOMEM);
673 }
674
675 memset(&arg, 0, sizeof(arg));
676 arg.result = result;
677 padded_iov[1].iov_base = &arg;
678 padded_iov[1].iov_len = sizeof(arg);
679
680 memcpy(&padded_iov[2], iov, count * sizeof(struct iovec));
681
682 res = send_reply_iov(req, 0, padded_iov, count + 2);
683 free(padded_iov);
684
685 return res;
686}
687
688int fuse_reply_poll(fuse_req_t req, unsigned revents)
689{
690 struct fuse_poll_out arg;
691
692 memset(&arg, 0, sizeof(arg));
693 arg.revents = revents;
694
695 return send_reply_ok(req, &arg, sizeof(arg));
696}
697
698int fuse_reply_lseek(fuse_req_t req, off_t off)
699{
700 struct fuse_lseek_out arg;
701
702 memset(&arg, 0, sizeof(arg));
703 arg.offset = off;
704
705 return send_reply_ok(req, &arg, sizeof(arg));
706}
707
708static void do_lookup(fuse_req_t req, fuse_ino_t nodeid,
709 struct fuse_mbuf_iter *iter)
710{
711 const char *name = fuse_mbuf_iter_advance_str(iter);
712 if (!name) {
713 fuse_reply_err(req, EINVAL);
714 return;
715 }
716
717 if (req->se->op.lookup) {
718 req->se->op.lookup(req, nodeid, name);
719 } else {
720 fuse_reply_err(req, ENOSYS);
721 }
722}
723
724static void do_forget(fuse_req_t req, fuse_ino_t nodeid,
725 struct fuse_mbuf_iter *iter)
726{
727 struct fuse_forget_in *arg;
728
729 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
730 if (!arg) {
731 fuse_reply_err(req, EINVAL);
732 return;
733 }
734
735 if (req->se->op.forget) {
736 req->se->op.forget(req, nodeid, arg->nlookup);
737 } else {
738 fuse_reply_none(req);
739 }
740}
741
742static void do_batch_forget(fuse_req_t req, fuse_ino_t nodeid,
743 struct fuse_mbuf_iter *iter)
744{
745 struct fuse_batch_forget_in *arg;
746 struct fuse_forget_data *forgets;
747 size_t scount;
748
749 (void)nodeid;
750
751 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
752 if (!arg) {
753 fuse_reply_none(req);
754 return;
755 }
756
757
758
759
760
761
762
763
764
765
766
767 scount = arg->count;
768 if (scount > SIZE_MAX / sizeof(forgets[0])) {
769 fuse_reply_none(req);
770 return;
771 }
772
773 forgets = fuse_mbuf_iter_advance(iter, arg->count * sizeof(forgets[0]));
774 if (!forgets) {
775 fuse_reply_none(req);
776 return;
777 }
778
779 if (req->se->op.forget_multi) {
780 req->se->op.forget_multi(req, arg->count, forgets);
781 } else if (req->se->op.forget) {
782 unsigned int i;
783
784 for (i = 0; i < arg->count; i++) {
785 struct fuse_req *dummy_req;
786
787 dummy_req = fuse_ll_alloc_req(req->se);
788 if (dummy_req == NULL) {
789 break;
790 }
791
792 dummy_req->unique = req->unique;
793 dummy_req->ctx = req->ctx;
794 dummy_req->ch = NULL;
795
796 req->se->op.forget(dummy_req, forgets[i].ino, forgets[i].nlookup);
797 }
798 fuse_reply_none(req);
799 } else {
800 fuse_reply_none(req);
801 }
802}
803
804static void do_getattr(fuse_req_t req, fuse_ino_t nodeid,
805 struct fuse_mbuf_iter *iter)
806{
807 struct fuse_file_info *fip = NULL;
808 struct fuse_file_info fi;
809
810 struct fuse_getattr_in *arg;
811
812 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
813 if (!arg) {
814 fuse_reply_err(req, EINVAL);
815 return;
816 }
817
818 if (arg->getattr_flags & FUSE_GETATTR_FH) {
819 memset(&fi, 0, sizeof(fi));
820 fi.fh = arg->fh;
821 fip = &fi;
822 }
823
824 if (req->se->op.getattr) {
825 req->se->op.getattr(req, nodeid, fip);
826 } else {
827 fuse_reply_err(req, ENOSYS);
828 }
829}
830
831static void do_setattr(fuse_req_t req, fuse_ino_t nodeid,
832 struct fuse_mbuf_iter *iter)
833{
834 if (req->se->op.setattr) {
835 struct fuse_setattr_in *arg;
836 struct fuse_file_info *fi = NULL;
837 struct fuse_file_info fi_store;
838 struct stat stbuf;
839
840 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
841 if (!arg) {
842 fuse_reply_err(req, EINVAL);
843 return;
844 }
845
846 memset(&stbuf, 0, sizeof(stbuf));
847 convert_attr(arg, &stbuf);
848 if (arg->valid & FATTR_FH) {
849 arg->valid &= ~FATTR_FH;
850 memset(&fi_store, 0, sizeof(fi_store));
851 fi = &fi_store;
852 fi->fh = arg->fh;
853 }
854 arg->valid &= FUSE_SET_ATTR_MODE | FUSE_SET_ATTR_UID |
855 FUSE_SET_ATTR_GID | FUSE_SET_ATTR_SIZE |
856 FUSE_SET_ATTR_ATIME | FUSE_SET_ATTR_MTIME |
857 FUSE_SET_ATTR_ATIME_NOW | FUSE_SET_ATTR_MTIME_NOW |
858 FUSE_SET_ATTR_CTIME | FUSE_SET_ATTR_KILL_SUIDGID;
859
860 req->se->op.setattr(req, nodeid, &stbuf, arg->valid, fi);
861 } else {
862 fuse_reply_err(req, ENOSYS);
863 }
864}
865
866static void do_access(fuse_req_t req, fuse_ino_t nodeid,
867 struct fuse_mbuf_iter *iter)
868{
869 struct fuse_access_in *arg;
870
871 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
872 if (!arg) {
873 fuse_reply_err(req, EINVAL);
874 return;
875 }
876
877 if (req->se->op.access) {
878 req->se->op.access(req, nodeid, arg->mask);
879 } else {
880 fuse_reply_err(req, ENOSYS);
881 }
882}
883
884static void do_readlink(fuse_req_t req, fuse_ino_t nodeid,
885 struct fuse_mbuf_iter *iter)
886{
887 (void)iter;
888
889 if (req->se->op.readlink) {
890 req->se->op.readlink(req, nodeid);
891 } else {
892 fuse_reply_err(req, ENOSYS);
893 }
894}
895
896static void do_mknod(fuse_req_t req, fuse_ino_t nodeid,
897 struct fuse_mbuf_iter *iter)
898{
899 struct fuse_mknod_in *arg;
900 const char *name;
901
902 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
903 name = fuse_mbuf_iter_advance_str(iter);
904 if (!arg || !name) {
905 fuse_reply_err(req, EINVAL);
906 return;
907 }
908
909 req->ctx.umask = arg->umask;
910
911 if (req->se->op.mknod) {
912 req->se->op.mknod(req, nodeid, name, arg->mode, arg->rdev);
913 } else {
914 fuse_reply_err(req, ENOSYS);
915 }
916}
917
918static void do_mkdir(fuse_req_t req, fuse_ino_t nodeid,
919 struct fuse_mbuf_iter *iter)
920{
921 struct fuse_mkdir_in *arg;
922 const char *name;
923
924 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
925 name = fuse_mbuf_iter_advance_str(iter);
926 if (!arg || !name) {
927 fuse_reply_err(req, EINVAL);
928 return;
929 }
930
931 req->ctx.umask = arg->umask;
932
933 if (req->se->op.mkdir) {
934 req->se->op.mkdir(req, nodeid, name, arg->mode);
935 } else {
936 fuse_reply_err(req, ENOSYS);
937 }
938}
939
940static void do_unlink(fuse_req_t req, fuse_ino_t nodeid,
941 struct fuse_mbuf_iter *iter)
942{
943 const char *name = fuse_mbuf_iter_advance_str(iter);
944
945 if (!name) {
946 fuse_reply_err(req, EINVAL);
947 return;
948 }
949
950 if (req->se->op.unlink) {
951 req->se->op.unlink(req, nodeid, name);
952 } else {
953 fuse_reply_err(req, ENOSYS);
954 }
955}
956
957static void do_rmdir(fuse_req_t req, fuse_ino_t nodeid,
958 struct fuse_mbuf_iter *iter)
959{
960 const char *name = fuse_mbuf_iter_advance_str(iter);
961
962 if (!name) {
963 fuse_reply_err(req, EINVAL);
964 return;
965 }
966
967 if (req->se->op.rmdir) {
968 req->se->op.rmdir(req, nodeid, name);
969 } else {
970 fuse_reply_err(req, ENOSYS);
971 }
972}
973
974static void do_symlink(fuse_req_t req, fuse_ino_t nodeid,
975 struct fuse_mbuf_iter *iter)
976{
977 const char *name = fuse_mbuf_iter_advance_str(iter);
978 const char *linkname = fuse_mbuf_iter_advance_str(iter);
979
980 if (!name || !linkname) {
981 fuse_reply_err(req, EINVAL);
982 return;
983 }
984
985 if (req->se->op.symlink) {
986 req->se->op.symlink(req, linkname, nodeid, name);
987 } else {
988 fuse_reply_err(req, ENOSYS);
989 }
990}
991
992static void do_rename(fuse_req_t req, fuse_ino_t nodeid,
993 struct fuse_mbuf_iter *iter)
994{
995 struct fuse_rename_in *arg;
996 const char *oldname;
997 const char *newname;
998
999 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1000 oldname = fuse_mbuf_iter_advance_str(iter);
1001 newname = fuse_mbuf_iter_advance_str(iter);
1002 if (!arg || !oldname || !newname) {
1003 fuse_reply_err(req, EINVAL);
1004 return;
1005 }
1006
1007 if (req->se->op.rename) {
1008 req->se->op.rename(req, nodeid, oldname, arg->newdir, newname, 0);
1009 } else {
1010 fuse_reply_err(req, ENOSYS);
1011 }
1012}
1013
1014static void do_rename2(fuse_req_t req, fuse_ino_t nodeid,
1015 struct fuse_mbuf_iter *iter)
1016{
1017 struct fuse_rename2_in *arg;
1018 const char *oldname;
1019 const char *newname;
1020
1021 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1022 oldname = fuse_mbuf_iter_advance_str(iter);
1023 newname = fuse_mbuf_iter_advance_str(iter);
1024 if (!arg || !oldname || !newname) {
1025 fuse_reply_err(req, EINVAL);
1026 return;
1027 }
1028
1029 if (req->se->op.rename) {
1030 req->se->op.rename(req, nodeid, oldname, arg->newdir, newname,
1031 arg->flags);
1032 } else {
1033 fuse_reply_err(req, ENOSYS);
1034 }
1035}
1036
1037static void do_link(fuse_req_t req, fuse_ino_t nodeid,
1038 struct fuse_mbuf_iter *iter)
1039{
1040 struct fuse_link_in *arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1041 const char *name = fuse_mbuf_iter_advance_str(iter);
1042
1043 if (!arg || !name) {
1044 fuse_reply_err(req, EINVAL);
1045 return;
1046 }
1047
1048 if (req->se->op.link) {
1049 req->se->op.link(req, arg->oldnodeid, nodeid, name);
1050 } else {
1051 fuse_reply_err(req, ENOSYS);
1052 }
1053}
1054
1055static void do_create(fuse_req_t req, fuse_ino_t nodeid,
1056 struct fuse_mbuf_iter *iter)
1057{
1058 if (req->se->op.create) {
1059 struct fuse_create_in *arg;
1060 struct fuse_file_info fi;
1061 const char *name;
1062
1063 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1064 name = fuse_mbuf_iter_advance_str(iter);
1065 if (!arg || !name) {
1066 fuse_reply_err(req, EINVAL);
1067 return;
1068 }
1069
1070 memset(&fi, 0, sizeof(fi));
1071 fi.flags = arg->flags;
1072 fi.kill_priv = arg->open_flags & FUSE_OPEN_KILL_SUIDGID;
1073
1074 req->ctx.umask = arg->umask;
1075
1076 req->se->op.create(req, nodeid, name, arg->mode, &fi);
1077 } else {
1078 fuse_reply_err(req, ENOSYS);
1079 }
1080}
1081
1082static void do_open(fuse_req_t req, fuse_ino_t nodeid,
1083 struct fuse_mbuf_iter *iter)
1084{
1085 struct fuse_open_in *arg;
1086 struct fuse_file_info fi;
1087
1088 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1089 if (!arg) {
1090 fuse_reply_err(req, EINVAL);
1091 return;
1092 }
1093
1094 memset(&fi, 0, sizeof(fi));
1095 fi.flags = arg->flags;
1096 fi.kill_priv = arg->open_flags & FUSE_OPEN_KILL_SUIDGID;
1097
1098 if (req->se->op.open) {
1099 req->se->op.open(req, nodeid, &fi);
1100 } else {
1101 fuse_reply_open(req, &fi);
1102 }
1103}
1104
1105static void do_read(fuse_req_t req, fuse_ino_t nodeid,
1106 struct fuse_mbuf_iter *iter)
1107{
1108 if (req->se->op.read) {
1109 struct fuse_read_in *arg;
1110 struct fuse_file_info fi;
1111
1112 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1113 if (!arg) {
1114 fuse_reply_err(req, EINVAL);
1115 return;
1116 }
1117
1118 memset(&fi, 0, sizeof(fi));
1119 fi.fh = arg->fh;
1120 fi.lock_owner = arg->lock_owner;
1121 fi.flags = arg->flags;
1122 req->se->op.read(req, nodeid, arg->size, arg->offset, &fi);
1123 } else {
1124 fuse_reply_err(req, ENOSYS);
1125 }
1126}
1127
1128static void do_write(fuse_req_t req, fuse_ino_t nodeid,
1129 struct fuse_mbuf_iter *iter)
1130{
1131 struct fuse_write_in *arg;
1132 struct fuse_file_info fi;
1133 const char *param;
1134
1135 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1136 if (!arg) {
1137 fuse_reply_err(req, EINVAL);
1138 return;
1139 }
1140
1141 param = fuse_mbuf_iter_advance(iter, arg->size);
1142 if (!param) {
1143 fuse_reply_err(req, EINVAL);
1144 return;
1145 }
1146
1147 memset(&fi, 0, sizeof(fi));
1148 fi.fh = arg->fh;
1149 fi.writepage = (arg->write_flags & FUSE_WRITE_CACHE) != 0;
1150 fi.kill_priv = !!(arg->write_flags & FUSE_WRITE_KILL_PRIV);
1151
1152 fi.lock_owner = arg->lock_owner;
1153 fi.flags = arg->flags;
1154
1155 if (req->se->op.write) {
1156 req->se->op.write(req, nodeid, param, arg->size, arg->offset, &fi);
1157 } else {
1158 fuse_reply_err(req, ENOSYS);
1159 }
1160}
1161
1162static void do_write_buf(fuse_req_t req, fuse_ino_t nodeid,
1163 struct fuse_mbuf_iter *iter, struct fuse_bufvec *ibufv)
1164{
1165 struct fuse_session *se = req->se;
1166 struct fuse_bufvec *pbufv = ibufv;
1167 struct fuse_bufvec tmpbufv = {
1168 .buf[0] = ibufv->buf[0],
1169 .count = 1,
1170 };
1171 struct fuse_write_in *arg;
1172 size_t arg_size = sizeof(*arg);
1173 struct fuse_file_info fi;
1174
1175 memset(&fi, 0, sizeof(fi));
1176
1177 arg = fuse_mbuf_iter_advance(iter, arg_size);
1178 if (!arg) {
1179 fuse_reply_err(req, EINVAL);
1180 return;
1181 }
1182
1183 fi.lock_owner = arg->lock_owner;
1184 fi.flags = arg->flags;
1185 fi.fh = arg->fh;
1186 fi.writepage = !!(arg->write_flags & FUSE_WRITE_CACHE);
1187 fi.kill_priv = !!(arg->write_flags & FUSE_WRITE_KILL_PRIV);
1188
1189 if (ibufv->count == 1) {
1190 assert(!(tmpbufv.buf[0].flags & FUSE_BUF_IS_FD));
1191 tmpbufv.buf[0].mem = ((char *)arg) + arg_size;
1192 tmpbufv.buf[0].size -= sizeof(struct fuse_in_header) + arg_size;
1193 pbufv = &tmpbufv;
1194 } else {
1195
1196
1197
1198
1199 ibufv->buf[0].size = 0;
1200 }
1201
1202 if (fuse_buf_size(pbufv) != arg->size) {
1203 fuse_log(FUSE_LOG_ERR,
1204 "fuse: do_write_buf: buffer size doesn't match arg->size\n");
1205 fuse_reply_err(req, EIO);
1206 return;
1207 }
1208
1209 se->op.write_buf(req, nodeid, pbufv, arg->offset, &fi);
1210}
1211
1212static void do_flush(fuse_req_t req, fuse_ino_t nodeid,
1213 struct fuse_mbuf_iter *iter)
1214{
1215 struct fuse_flush_in *arg;
1216 struct fuse_file_info fi;
1217
1218 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1219 if (!arg) {
1220 fuse_reply_err(req, EINVAL);
1221 return;
1222 }
1223
1224 memset(&fi, 0, sizeof(fi));
1225 fi.fh = arg->fh;
1226 fi.flush = 1;
1227 fi.lock_owner = arg->lock_owner;
1228
1229 if (req->se->op.flush) {
1230 req->se->op.flush(req, nodeid, &fi);
1231 } else {
1232 fuse_reply_err(req, ENOSYS);
1233 }
1234}
1235
1236static void do_release(fuse_req_t req, fuse_ino_t nodeid,
1237 struct fuse_mbuf_iter *iter)
1238{
1239 struct fuse_release_in *arg;
1240 struct fuse_file_info fi;
1241
1242 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1243 if (!arg) {
1244 fuse_reply_err(req, EINVAL);
1245 return;
1246 }
1247
1248 memset(&fi, 0, sizeof(fi));
1249 fi.flags = arg->flags;
1250 fi.fh = arg->fh;
1251 fi.flush = (arg->release_flags & FUSE_RELEASE_FLUSH) ? 1 : 0;
1252 fi.lock_owner = arg->lock_owner;
1253
1254 if (arg->release_flags & FUSE_RELEASE_FLOCK_UNLOCK) {
1255 fi.flock_release = 1;
1256 }
1257
1258 if (req->se->op.release) {
1259 req->se->op.release(req, nodeid, &fi);
1260 } else {
1261 fuse_reply_err(req, 0);
1262 }
1263}
1264
1265static void do_fsync(fuse_req_t req, fuse_ino_t nodeid,
1266 struct fuse_mbuf_iter *iter)
1267{
1268 struct fuse_fsync_in *arg;
1269 struct fuse_file_info fi;
1270 int datasync;
1271
1272 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1273 if (!arg) {
1274 fuse_reply_err(req, EINVAL);
1275 return;
1276 }
1277 datasync = arg->fsync_flags & 1;
1278
1279 memset(&fi, 0, sizeof(fi));
1280 fi.fh = arg->fh;
1281
1282 if (req->se->op.fsync) {
1283 if (fi.fh == (uint64_t)-1) {
1284 req->se->op.fsync(req, nodeid, datasync, NULL);
1285 } else {
1286 req->se->op.fsync(req, nodeid, datasync, &fi);
1287 }
1288 } else {
1289 fuse_reply_err(req, ENOSYS);
1290 }
1291}
1292
1293static void do_opendir(fuse_req_t req, fuse_ino_t nodeid,
1294 struct fuse_mbuf_iter *iter)
1295{
1296 struct fuse_open_in *arg;
1297 struct fuse_file_info fi;
1298
1299 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1300 if (!arg) {
1301 fuse_reply_err(req, EINVAL);
1302 return;
1303 }
1304
1305 memset(&fi, 0, sizeof(fi));
1306 fi.flags = arg->flags;
1307
1308 if (req->se->op.opendir) {
1309 req->se->op.opendir(req, nodeid, &fi);
1310 } else {
1311 fuse_reply_open(req, &fi);
1312 }
1313}
1314
1315static void do_readdir(fuse_req_t req, fuse_ino_t nodeid,
1316 struct fuse_mbuf_iter *iter)
1317{
1318 struct fuse_read_in *arg;
1319 struct fuse_file_info fi;
1320
1321 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1322 if (!arg) {
1323 fuse_reply_err(req, EINVAL);
1324 return;
1325 }
1326
1327 memset(&fi, 0, sizeof(fi));
1328 fi.fh = arg->fh;
1329
1330 if (req->se->op.readdir) {
1331 req->se->op.readdir(req, nodeid, arg->size, arg->offset, &fi);
1332 } else {
1333 fuse_reply_err(req, ENOSYS);
1334 }
1335}
1336
1337static void do_readdirplus(fuse_req_t req, fuse_ino_t nodeid,
1338 struct fuse_mbuf_iter *iter)
1339{
1340 struct fuse_read_in *arg;
1341 struct fuse_file_info fi;
1342
1343 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1344 if (!arg) {
1345 fuse_reply_err(req, EINVAL);
1346 return;
1347 }
1348
1349 memset(&fi, 0, sizeof(fi));
1350 fi.fh = arg->fh;
1351
1352 if (req->se->op.readdirplus) {
1353 req->se->op.readdirplus(req, nodeid, arg->size, arg->offset, &fi);
1354 } else {
1355 fuse_reply_err(req, ENOSYS);
1356 }
1357}
1358
1359static void do_releasedir(fuse_req_t req, fuse_ino_t nodeid,
1360 struct fuse_mbuf_iter *iter)
1361{
1362 struct fuse_release_in *arg;
1363 struct fuse_file_info fi;
1364
1365 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1366 if (!arg) {
1367 fuse_reply_err(req, EINVAL);
1368 return;
1369 }
1370
1371 memset(&fi, 0, sizeof(fi));
1372 fi.flags = arg->flags;
1373 fi.fh = arg->fh;
1374
1375 if (req->se->op.releasedir) {
1376 req->se->op.releasedir(req, nodeid, &fi);
1377 } else {
1378 fuse_reply_err(req, 0);
1379 }
1380}
1381
1382static void do_fsyncdir(fuse_req_t req, fuse_ino_t nodeid,
1383 struct fuse_mbuf_iter *iter)
1384{
1385 struct fuse_fsync_in *arg;
1386 struct fuse_file_info fi;
1387 int datasync;
1388
1389 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1390 if (!arg) {
1391 fuse_reply_err(req, EINVAL);
1392 return;
1393 }
1394 datasync = arg->fsync_flags & 1;
1395
1396 memset(&fi, 0, sizeof(fi));
1397 fi.fh = arg->fh;
1398
1399 if (req->se->op.fsyncdir) {
1400 req->se->op.fsyncdir(req, nodeid, datasync, &fi);
1401 } else {
1402 fuse_reply_err(req, ENOSYS);
1403 }
1404}
1405
1406static void do_statfs(fuse_req_t req, fuse_ino_t nodeid,
1407 struct fuse_mbuf_iter *iter)
1408{
1409 (void)nodeid;
1410 (void)iter;
1411
1412 if (req->se->op.statfs) {
1413 req->se->op.statfs(req, nodeid);
1414 } else {
1415 struct statvfs buf = {
1416 .f_namemax = 255,
1417 .f_bsize = 512,
1418 };
1419 fuse_reply_statfs(req, &buf);
1420 }
1421}
1422
1423static void do_setxattr(fuse_req_t req, fuse_ino_t nodeid,
1424 struct fuse_mbuf_iter *iter)
1425{
1426 struct fuse_setxattr_in *arg;
1427 const char *name;
1428 const char *value;
1429
1430 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1431 name = fuse_mbuf_iter_advance_str(iter);
1432 if (!arg || !name) {
1433 fuse_reply_err(req, EINVAL);
1434 return;
1435 }
1436
1437 value = fuse_mbuf_iter_advance(iter, arg->size);
1438 if (!value) {
1439 fuse_reply_err(req, EINVAL);
1440 return;
1441 }
1442
1443 if (req->se->op.setxattr) {
1444 req->se->op.setxattr(req, nodeid, name, value, arg->size, arg->flags);
1445 } else {
1446 fuse_reply_err(req, ENOSYS);
1447 }
1448}
1449
1450static void do_getxattr(fuse_req_t req, fuse_ino_t nodeid,
1451 struct fuse_mbuf_iter *iter)
1452{
1453 struct fuse_getxattr_in *arg;
1454 const char *name;
1455
1456 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1457 name = fuse_mbuf_iter_advance_str(iter);
1458 if (!arg || !name) {
1459 fuse_reply_err(req, EINVAL);
1460 return;
1461 }
1462
1463 if (req->se->op.getxattr) {
1464 req->se->op.getxattr(req, nodeid, name, arg->size);
1465 } else {
1466 fuse_reply_err(req, ENOSYS);
1467 }
1468}
1469
1470static void do_listxattr(fuse_req_t req, fuse_ino_t nodeid,
1471 struct fuse_mbuf_iter *iter)
1472{
1473 struct fuse_getxattr_in *arg;
1474
1475 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1476 if (!arg) {
1477 fuse_reply_err(req, EINVAL);
1478 return;
1479 }
1480
1481 if (req->se->op.listxattr) {
1482 req->se->op.listxattr(req, nodeid, arg->size);
1483 } else {
1484 fuse_reply_err(req, ENOSYS);
1485 }
1486}
1487
1488static void do_removexattr(fuse_req_t req, fuse_ino_t nodeid,
1489 struct fuse_mbuf_iter *iter)
1490{
1491 const char *name = fuse_mbuf_iter_advance_str(iter);
1492
1493 if (!name) {
1494 fuse_reply_err(req, EINVAL);
1495 return;
1496 }
1497
1498 if (req->se->op.removexattr) {
1499 req->se->op.removexattr(req, nodeid, name);
1500 } else {
1501 fuse_reply_err(req, ENOSYS);
1502 }
1503}
1504
1505static void convert_fuse_file_lock(struct fuse_file_lock *fl,
1506 struct flock *flock)
1507{
1508 memset(flock, 0, sizeof(struct flock));
1509 flock->l_type = fl->type;
1510 flock->l_whence = SEEK_SET;
1511 flock->l_start = fl->start;
1512 if (fl->end == OFFSET_MAX) {
1513 flock->l_len = 0;
1514 } else {
1515 flock->l_len = fl->end - fl->start + 1;
1516 }
1517 flock->l_pid = fl->pid;
1518}
1519
1520static void do_getlk(fuse_req_t req, fuse_ino_t nodeid,
1521 struct fuse_mbuf_iter *iter)
1522{
1523 struct fuse_lk_in *arg;
1524 struct fuse_file_info fi;
1525 struct flock flock;
1526
1527 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1528 if (!arg) {
1529 fuse_reply_err(req, EINVAL);
1530 return;
1531 }
1532
1533 memset(&fi, 0, sizeof(fi));
1534 fi.fh = arg->fh;
1535 fi.lock_owner = arg->owner;
1536
1537 convert_fuse_file_lock(&arg->lk, &flock);
1538 if (req->se->op.getlk) {
1539 req->se->op.getlk(req, nodeid, &fi, &flock);
1540 } else {
1541 fuse_reply_err(req, ENOSYS);
1542 }
1543}
1544
1545static void do_setlk_common(fuse_req_t req, fuse_ino_t nodeid,
1546 struct fuse_mbuf_iter *iter, int sleep)
1547{
1548 struct fuse_lk_in *arg;
1549 struct fuse_file_info fi;
1550 struct flock flock;
1551
1552 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1553 if (!arg) {
1554 fuse_reply_err(req, EINVAL);
1555 return;
1556 }
1557
1558 memset(&fi, 0, sizeof(fi));
1559 fi.fh = arg->fh;
1560 fi.lock_owner = arg->owner;
1561
1562 if (arg->lk_flags & FUSE_LK_FLOCK) {
1563 int op = 0;
1564
1565 switch (arg->lk.type) {
1566 case F_RDLCK:
1567 op = LOCK_SH;
1568 break;
1569 case F_WRLCK:
1570 op = LOCK_EX;
1571 break;
1572 case F_UNLCK:
1573 op = LOCK_UN;
1574 break;
1575 }
1576 if (!sleep) {
1577 op |= LOCK_NB;
1578 }
1579
1580 if (req->se->op.flock) {
1581 req->se->op.flock(req, nodeid, &fi, op);
1582 } else {
1583 fuse_reply_err(req, ENOSYS);
1584 }
1585 } else {
1586 convert_fuse_file_lock(&arg->lk, &flock);
1587 if (req->se->op.setlk) {
1588 req->se->op.setlk(req, nodeid, &fi, &flock, sleep);
1589 } else {
1590 fuse_reply_err(req, ENOSYS);
1591 }
1592 }
1593}
1594
1595static void do_setlk(fuse_req_t req, fuse_ino_t nodeid,
1596 struct fuse_mbuf_iter *iter)
1597{
1598 do_setlk_common(req, nodeid, iter, 0);
1599}
1600
1601static void do_setlkw(fuse_req_t req, fuse_ino_t nodeid,
1602 struct fuse_mbuf_iter *iter)
1603{
1604 do_setlk_common(req, nodeid, iter, 1);
1605}
1606
1607static int find_interrupted(struct fuse_session *se, struct fuse_req *req)
1608{
1609 struct fuse_req *curr;
1610
1611 for (curr = se->list.next; curr != &se->list; curr = curr->next) {
1612 if (curr->unique == req->u.i.unique) {
1613 fuse_interrupt_func_t func;
1614 void *data;
1615
1616 curr->ctr++;
1617 pthread_mutex_unlock(&se->lock);
1618
1619
1620 pthread_mutex_lock(&curr->lock);
1621 pthread_mutex_lock(&se->lock);
1622 curr->interrupted = 1;
1623 func = curr->u.ni.func;
1624 data = curr->u.ni.data;
1625 pthread_mutex_unlock(&se->lock);
1626 if (func) {
1627 func(curr, data);
1628 }
1629 pthread_mutex_unlock(&curr->lock);
1630
1631 pthread_mutex_lock(&se->lock);
1632 curr->ctr--;
1633 if (!curr->ctr) {
1634 destroy_req(curr);
1635 }
1636
1637 return 1;
1638 }
1639 }
1640 for (curr = se->interrupts.next; curr != &se->interrupts;
1641 curr = curr->next) {
1642 if (curr->u.i.unique == req->u.i.unique) {
1643 return 1;
1644 }
1645 }
1646 return 0;
1647}
1648
1649static void do_interrupt(fuse_req_t req, fuse_ino_t nodeid,
1650 struct fuse_mbuf_iter *iter)
1651{
1652 struct fuse_interrupt_in *arg;
1653 struct fuse_session *se = req->se;
1654
1655 (void)nodeid;
1656
1657 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1658 if (!arg) {
1659 fuse_reply_err(req, EINVAL);
1660 return;
1661 }
1662
1663 fuse_log(FUSE_LOG_DEBUG, "INTERRUPT: %llu\n",
1664 (unsigned long long)arg->unique);
1665
1666 req->u.i.unique = arg->unique;
1667
1668 pthread_mutex_lock(&se->lock);
1669 if (find_interrupted(se, req)) {
1670 destroy_req(req);
1671 } else {
1672 list_add_req(req, &se->interrupts);
1673 }
1674 pthread_mutex_unlock(&se->lock);
1675}
1676
1677static struct fuse_req *check_interrupt(struct fuse_session *se,
1678 struct fuse_req *req)
1679{
1680 struct fuse_req *curr;
1681
1682 for (curr = se->interrupts.next; curr != &se->interrupts;
1683 curr = curr->next) {
1684 if (curr->u.i.unique == req->unique) {
1685 req->interrupted = 1;
1686 list_del_req(curr);
1687 free(curr);
1688 return NULL;
1689 }
1690 }
1691 curr = se->interrupts.next;
1692 if (curr != &se->interrupts) {
1693 list_del_req(curr);
1694 list_init_req(curr);
1695 return curr;
1696 } else {
1697 return NULL;
1698 }
1699}
1700
1701static void do_bmap(fuse_req_t req, fuse_ino_t nodeid,
1702 struct fuse_mbuf_iter *iter)
1703{
1704 struct fuse_bmap_in *arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1705
1706 if (!arg) {
1707 fuse_reply_err(req, EINVAL);
1708 return;
1709 }
1710
1711 if (req->se->op.bmap) {
1712 req->se->op.bmap(req, nodeid, arg->blocksize, arg->block);
1713 } else {
1714 fuse_reply_err(req, ENOSYS);
1715 }
1716}
1717
1718static void do_ioctl(fuse_req_t req, fuse_ino_t nodeid,
1719 struct fuse_mbuf_iter *iter)
1720{
1721 struct fuse_ioctl_in *arg;
1722 unsigned int flags;
1723 void *in_buf = NULL;
1724 struct fuse_file_info fi;
1725
1726 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1727 if (!arg) {
1728 fuse_reply_err(req, EINVAL);
1729 return;
1730 }
1731
1732 flags = arg->flags;
1733 if (flags & FUSE_IOCTL_DIR && !(req->se->conn.want & FUSE_CAP_IOCTL_DIR)) {
1734 fuse_reply_err(req, ENOTTY);
1735 return;
1736 }
1737
1738 if (arg->in_size) {
1739 in_buf = fuse_mbuf_iter_advance(iter, arg->in_size);
1740 if (!in_buf) {
1741 fuse_reply_err(req, EINVAL);
1742 return;
1743 }
1744 }
1745
1746 memset(&fi, 0, sizeof(fi));
1747 fi.fh = arg->fh;
1748
1749 if (sizeof(void *) == 4 && !(flags & FUSE_IOCTL_32BIT)) {
1750 req->ioctl_64bit = 1;
1751 }
1752
1753 if (req->se->op.ioctl) {
1754 req->se->op.ioctl(req, nodeid, arg->cmd, (void *)(uintptr_t)arg->arg,
1755 &fi, flags, in_buf, arg->in_size, arg->out_size);
1756 } else {
1757 fuse_reply_err(req, ENOSYS);
1758 }
1759}
1760
1761void fuse_pollhandle_destroy(struct fuse_pollhandle *ph)
1762{
1763 free(ph);
1764}
1765
1766static void do_poll(fuse_req_t req, fuse_ino_t nodeid,
1767 struct fuse_mbuf_iter *iter)
1768{
1769 struct fuse_poll_in *arg;
1770 struct fuse_file_info fi;
1771
1772 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1773 if (!arg) {
1774 fuse_reply_err(req, EINVAL);
1775 return;
1776 }
1777
1778 memset(&fi, 0, sizeof(fi));
1779 fi.fh = arg->fh;
1780 fi.poll_events = arg->events;
1781
1782 if (req->se->op.poll) {
1783 struct fuse_pollhandle *ph = NULL;
1784
1785 if (arg->flags & FUSE_POLL_SCHEDULE_NOTIFY) {
1786 ph = malloc(sizeof(struct fuse_pollhandle));
1787 if (ph == NULL) {
1788 fuse_reply_err(req, ENOMEM);
1789 return;
1790 }
1791 ph->kh = arg->kh;
1792 ph->se = req->se;
1793 }
1794
1795 req->se->op.poll(req, nodeid, &fi, ph);
1796 } else {
1797 fuse_reply_err(req, ENOSYS);
1798 }
1799}
1800
1801static void do_fallocate(fuse_req_t req, fuse_ino_t nodeid,
1802 struct fuse_mbuf_iter *iter)
1803{
1804 struct fuse_fallocate_in *arg;
1805 struct fuse_file_info fi;
1806
1807 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1808 if (!arg) {
1809 fuse_reply_err(req, EINVAL);
1810 return;
1811 }
1812
1813 memset(&fi, 0, sizeof(fi));
1814 fi.fh = arg->fh;
1815
1816 if (req->se->op.fallocate) {
1817 req->se->op.fallocate(req, nodeid, arg->mode, arg->offset, arg->length,
1818 &fi);
1819 } else {
1820 fuse_reply_err(req, ENOSYS);
1821 }
1822}
1823
1824static void do_copy_file_range(fuse_req_t req, fuse_ino_t nodeid_in,
1825 struct fuse_mbuf_iter *iter)
1826{
1827 struct fuse_copy_file_range_in *arg;
1828 struct fuse_file_info fi_in, fi_out;
1829
1830 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1831 if (!arg) {
1832 fuse_reply_err(req, EINVAL);
1833 return;
1834 }
1835
1836 memset(&fi_in, 0, sizeof(fi_in));
1837 fi_in.fh = arg->fh_in;
1838
1839 memset(&fi_out, 0, sizeof(fi_out));
1840 fi_out.fh = arg->fh_out;
1841
1842
1843 if (req->se->op.copy_file_range) {
1844 req->se->op.copy_file_range(req, nodeid_in, arg->off_in, &fi_in,
1845 arg->nodeid_out, arg->off_out, &fi_out,
1846 arg->len, arg->flags);
1847 } else {
1848 fuse_reply_err(req, ENOSYS);
1849 }
1850}
1851
1852static void do_lseek(fuse_req_t req, fuse_ino_t nodeid,
1853 struct fuse_mbuf_iter *iter)
1854{
1855 struct fuse_lseek_in *arg;
1856 struct fuse_file_info fi;
1857
1858 arg = fuse_mbuf_iter_advance(iter, sizeof(*arg));
1859 if (!arg) {
1860 fuse_reply_err(req, EINVAL);
1861 return;
1862 }
1863 memset(&fi, 0, sizeof(fi));
1864 fi.fh = arg->fh;
1865
1866 if (req->se->op.lseek) {
1867 req->se->op.lseek(req, nodeid, arg->offset, arg->whence, &fi);
1868 } else {
1869 fuse_reply_err(req, ENOSYS);
1870 }
1871}
1872
1873static void do_init(fuse_req_t req, fuse_ino_t nodeid,
1874 struct fuse_mbuf_iter *iter)
1875{
1876 size_t compat_size = offsetof(struct fuse_init_in, max_readahead);
1877 struct fuse_init_in *arg;
1878 struct fuse_init_out outarg;
1879 struct fuse_session *se = req->se;
1880 size_t bufsize = se->bufsize;
1881 size_t outargsize = sizeof(outarg);
1882
1883 (void)nodeid;
1884
1885
1886 arg = fuse_mbuf_iter_advance(iter, compat_size);
1887 if (!arg) {
1888 fuse_reply_err(req, EINVAL);
1889 return;
1890 }
1891
1892
1893 if (arg->major == 7 && arg->minor >= 6) {
1894 if (!fuse_mbuf_iter_advance(iter, sizeof(*arg) - compat_size)) {
1895 fuse_reply_err(req, EINVAL);
1896 return;
1897 }
1898 }
1899
1900 fuse_log(FUSE_LOG_DEBUG, "INIT: %u.%u\n", arg->major, arg->minor);
1901 if (arg->major == 7 && arg->minor >= 6) {
1902 fuse_log(FUSE_LOG_DEBUG, "flags=0x%08x\n", arg->flags);
1903 fuse_log(FUSE_LOG_DEBUG, "max_readahead=0x%08x\n", arg->max_readahead);
1904 }
1905 se->conn.proto_major = arg->major;
1906 se->conn.proto_minor = arg->minor;
1907 se->conn.capable = 0;
1908 se->conn.want = 0;
1909
1910 memset(&outarg, 0, sizeof(outarg));
1911 outarg.major = FUSE_KERNEL_VERSION;
1912 outarg.minor = FUSE_KERNEL_MINOR_VERSION;
1913
1914 if (arg->major < 7 || (arg->major == 7 && arg->minor < 31)) {
1915 fuse_log(FUSE_LOG_ERR, "fuse: unsupported protocol version: %u.%u\n",
1916 arg->major, arg->minor);
1917 fuse_reply_err(req, EPROTO);
1918 return;
1919 }
1920
1921 if (arg->major > 7) {
1922
1923 send_reply_ok(req, &outarg, sizeof(outarg));
1924 return;
1925 }
1926
1927 if (arg->max_readahead < se->conn.max_readahead) {
1928 se->conn.max_readahead = arg->max_readahead;
1929 }
1930 if (arg->flags & FUSE_ASYNC_READ) {
1931 se->conn.capable |= FUSE_CAP_ASYNC_READ;
1932 }
1933 if (arg->flags & FUSE_POSIX_LOCKS) {
1934 se->conn.capable |= FUSE_CAP_POSIX_LOCKS;
1935 }
1936 if (arg->flags & FUSE_ATOMIC_O_TRUNC) {
1937 se->conn.capable |= FUSE_CAP_ATOMIC_O_TRUNC;
1938 }
1939 if (arg->flags & FUSE_EXPORT_SUPPORT) {
1940 se->conn.capable |= FUSE_CAP_EXPORT_SUPPORT;
1941 }
1942 if (arg->flags & FUSE_DONT_MASK) {
1943 se->conn.capable |= FUSE_CAP_DONT_MASK;
1944 }
1945 if (arg->flags & FUSE_FLOCK_LOCKS) {
1946 se->conn.capable |= FUSE_CAP_FLOCK_LOCKS;
1947 }
1948 if (arg->flags & FUSE_AUTO_INVAL_DATA) {
1949 se->conn.capable |= FUSE_CAP_AUTO_INVAL_DATA;
1950 }
1951 if (arg->flags & FUSE_DO_READDIRPLUS) {
1952 se->conn.capable |= FUSE_CAP_READDIRPLUS;
1953 }
1954 if (arg->flags & FUSE_READDIRPLUS_AUTO) {
1955 se->conn.capable |= FUSE_CAP_READDIRPLUS_AUTO;
1956 }
1957 if (arg->flags & FUSE_ASYNC_DIO) {
1958 se->conn.capable |= FUSE_CAP_ASYNC_DIO;
1959 }
1960 if (arg->flags & FUSE_WRITEBACK_CACHE) {
1961 se->conn.capable |= FUSE_CAP_WRITEBACK_CACHE;
1962 }
1963 if (arg->flags & FUSE_NO_OPEN_SUPPORT) {
1964 se->conn.capable |= FUSE_CAP_NO_OPEN_SUPPORT;
1965 }
1966 if (arg->flags & FUSE_PARALLEL_DIROPS) {
1967 se->conn.capable |= FUSE_CAP_PARALLEL_DIROPS;
1968 }
1969 if (arg->flags & FUSE_POSIX_ACL) {
1970 se->conn.capable |= FUSE_CAP_POSIX_ACL;
1971 }
1972 if (arg->flags & FUSE_HANDLE_KILLPRIV) {
1973 se->conn.capable |= FUSE_CAP_HANDLE_KILLPRIV;
1974 }
1975 if (arg->flags & FUSE_NO_OPENDIR_SUPPORT) {
1976 se->conn.capable |= FUSE_CAP_NO_OPENDIR_SUPPORT;
1977 }
1978 if (!(arg->flags & FUSE_MAX_PAGES)) {
1979 size_t max_bufsize = FUSE_DEFAULT_MAX_PAGES_PER_REQ * getpagesize() +
1980 FUSE_BUFFER_HEADER_SIZE;
1981 if (bufsize > max_bufsize) {
1982 bufsize = max_bufsize;
1983 }
1984 }
1985 if (arg->flags & FUSE_SUBMOUNTS) {
1986 se->conn.capable |= FUSE_CAP_SUBMOUNTS;
1987 }
1988 if (arg->flags & FUSE_HANDLE_KILLPRIV_V2) {
1989 se->conn.capable |= FUSE_CAP_HANDLE_KILLPRIV_V2;
1990 }
1991#ifdef HAVE_SPLICE
1992#ifdef HAVE_VMSPLICE
1993 se->conn.capable |= FUSE_CAP_SPLICE_WRITE | FUSE_CAP_SPLICE_MOVE;
1994#endif
1995 se->conn.capable |= FUSE_CAP_SPLICE_READ;
1996#endif
1997 se->conn.capable |= FUSE_CAP_IOCTL_DIR;
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007#define LL_SET_DEFAULT(cond, cap) \
2008 if ((cond) && (se->conn.capable & (cap))) \
2009 se->conn.want |= (cap)
2010 LL_SET_DEFAULT(1, FUSE_CAP_ASYNC_READ);
2011 LL_SET_DEFAULT(1, FUSE_CAP_PARALLEL_DIROPS);
2012 LL_SET_DEFAULT(1, FUSE_CAP_AUTO_INVAL_DATA);
2013 LL_SET_DEFAULT(1, FUSE_CAP_HANDLE_KILLPRIV);
2014 LL_SET_DEFAULT(1, FUSE_CAP_ASYNC_DIO);
2015 LL_SET_DEFAULT(1, FUSE_CAP_IOCTL_DIR);
2016 LL_SET_DEFAULT(1, FUSE_CAP_ATOMIC_O_TRUNC);
2017 LL_SET_DEFAULT(se->op.write_buf, FUSE_CAP_SPLICE_READ);
2018 LL_SET_DEFAULT(se->op.getlk && se->op.setlk, FUSE_CAP_POSIX_LOCKS);
2019 LL_SET_DEFAULT(se->op.flock, FUSE_CAP_FLOCK_LOCKS);
2020 LL_SET_DEFAULT(se->op.readdirplus, FUSE_CAP_READDIRPLUS);
2021 LL_SET_DEFAULT(se->op.readdirplus && se->op.readdir,
2022 FUSE_CAP_READDIRPLUS_AUTO);
2023 se->conn.time_gran = 1;
2024
2025 if (bufsize < FUSE_MIN_READ_BUFFER) {
2026 fuse_log(FUSE_LOG_ERR, "fuse: warning: buffer size too small: %zu\n",
2027 bufsize);
2028 bufsize = FUSE_MIN_READ_BUFFER;
2029 }
2030 se->bufsize = bufsize;
2031
2032 if (se->conn.max_write > bufsize - FUSE_BUFFER_HEADER_SIZE) {
2033 se->conn.max_write = bufsize - FUSE_BUFFER_HEADER_SIZE;
2034 }
2035
2036 se->got_init = 1;
2037 se->got_destroy = 0;
2038 if (se->op.init) {
2039 se->op.init(se->userdata, &se->conn);
2040 }
2041
2042 if (se->conn.want & (~se->conn.capable)) {
2043 fuse_log(FUSE_LOG_ERR,
2044 "fuse: error: filesystem requested capabilities "
2045 "0x%x that are not supported by kernel, aborting.\n",
2046 se->conn.want & (~se->conn.capable));
2047 fuse_reply_err(req, EPROTO);
2048 se->error = -EPROTO;
2049 fuse_session_exit(se);
2050 return;
2051 }
2052
2053 if (se->conn.max_write < bufsize - FUSE_BUFFER_HEADER_SIZE) {
2054 se->bufsize = se->conn.max_write + FUSE_BUFFER_HEADER_SIZE;
2055 }
2056 if (arg->flags & FUSE_MAX_PAGES) {
2057 outarg.flags |= FUSE_MAX_PAGES;
2058 outarg.max_pages = (se->conn.max_write - 1) / getpagesize() + 1;
2059 }
2060
2061
2062
2063
2064
2065 outarg.flags |= FUSE_BIG_WRITES;
2066
2067 if (se->conn.want & FUSE_CAP_ASYNC_READ) {
2068 outarg.flags |= FUSE_ASYNC_READ;
2069 }
2070 if (se->conn.want & FUSE_CAP_PARALLEL_DIROPS) {
2071 outarg.flags |= FUSE_PARALLEL_DIROPS;
2072 }
2073 if (se->conn.want & FUSE_CAP_POSIX_LOCKS) {
2074 outarg.flags |= FUSE_POSIX_LOCKS;
2075 }
2076 if (se->conn.want & FUSE_CAP_ATOMIC_O_TRUNC) {
2077 outarg.flags |= FUSE_ATOMIC_O_TRUNC;
2078 }
2079 if (se->conn.want & FUSE_CAP_EXPORT_SUPPORT) {
2080 outarg.flags |= FUSE_EXPORT_SUPPORT;
2081 }
2082 if (se->conn.want & FUSE_CAP_DONT_MASK) {
2083 outarg.flags |= FUSE_DONT_MASK;
2084 }
2085 if (se->conn.want & FUSE_CAP_FLOCK_LOCKS) {
2086 outarg.flags |= FUSE_FLOCK_LOCKS;
2087 }
2088 if (se->conn.want & FUSE_CAP_AUTO_INVAL_DATA) {
2089 outarg.flags |= FUSE_AUTO_INVAL_DATA;
2090 }
2091 if (se->conn.want & FUSE_CAP_READDIRPLUS) {
2092 outarg.flags |= FUSE_DO_READDIRPLUS;
2093 }
2094 if (se->conn.want & FUSE_CAP_READDIRPLUS_AUTO) {
2095 outarg.flags |= FUSE_READDIRPLUS_AUTO;
2096 }
2097 if (se->conn.want & FUSE_CAP_ASYNC_DIO) {
2098 outarg.flags |= FUSE_ASYNC_DIO;
2099 }
2100 if (se->conn.want & FUSE_CAP_WRITEBACK_CACHE) {
2101 outarg.flags |= FUSE_WRITEBACK_CACHE;
2102 }
2103 if (se->conn.want & FUSE_CAP_POSIX_ACL) {
2104 outarg.flags |= FUSE_POSIX_ACL;
2105 }
2106 outarg.max_readahead = se->conn.max_readahead;
2107 outarg.max_write = se->conn.max_write;
2108 if (se->conn.max_background >= (1 << 16)) {
2109 se->conn.max_background = (1 << 16) - 1;
2110 }
2111 if (se->conn.congestion_threshold > se->conn.max_background) {
2112 se->conn.congestion_threshold = se->conn.max_background;
2113 }
2114 if (!se->conn.congestion_threshold) {
2115 se->conn.congestion_threshold = se->conn.max_background * 3 / 4;
2116 }
2117
2118 outarg.max_background = se->conn.max_background;
2119 outarg.congestion_threshold = se->conn.congestion_threshold;
2120 outarg.time_gran = se->conn.time_gran;
2121
2122 if (se->conn.want & FUSE_CAP_HANDLE_KILLPRIV_V2) {
2123 outarg.flags |= FUSE_HANDLE_KILLPRIV_V2;
2124 }
2125
2126 fuse_log(FUSE_LOG_DEBUG, " INIT: %u.%u\n", outarg.major, outarg.minor);
2127 fuse_log(FUSE_LOG_DEBUG, " flags=0x%08x\n", outarg.flags);
2128 fuse_log(FUSE_LOG_DEBUG, " max_readahead=0x%08x\n", outarg.max_readahead);
2129 fuse_log(FUSE_LOG_DEBUG, " max_write=0x%08x\n", outarg.max_write);
2130 fuse_log(FUSE_LOG_DEBUG, " max_background=%i\n", outarg.max_background);
2131 fuse_log(FUSE_LOG_DEBUG, " congestion_threshold=%i\n",
2132 outarg.congestion_threshold);
2133 fuse_log(FUSE_LOG_DEBUG, " time_gran=%u\n", outarg.time_gran);
2134
2135 send_reply_ok(req, &outarg, outargsize);
2136}
2137
2138static void do_destroy(fuse_req_t req, fuse_ino_t nodeid,
2139 struct fuse_mbuf_iter *iter)
2140{
2141 struct fuse_session *se = req->se;
2142
2143 (void)nodeid;
2144 (void)iter;
2145
2146 se->got_destroy = 1;
2147 se->got_init = 0;
2148 if (se->op.destroy) {
2149 se->op.destroy(se->userdata);
2150 }
2151
2152 send_reply_ok(req, NULL, 0);
2153}
2154
2155int fuse_lowlevel_notify_store(struct fuse_session *se, fuse_ino_t ino,
2156 off_t offset, struct fuse_bufvec *bufv)
2157{
2158 struct fuse_out_header out = {
2159 .error = FUSE_NOTIFY_STORE,
2160 };
2161 struct fuse_notify_store_out outarg = {
2162 .nodeid = ino,
2163 .offset = offset,
2164 .size = fuse_buf_size(bufv),
2165 };
2166 struct iovec iov[3];
2167 int res;
2168
2169 if (!se) {
2170 return -EINVAL;
2171 }
2172
2173 iov[0].iov_base = &out;
2174 iov[0].iov_len = sizeof(out);
2175 iov[1].iov_base = &outarg;
2176 iov[1].iov_len = sizeof(outarg);
2177
2178 res = fuse_send_data_iov(se, NULL, iov, 2, bufv);
2179 if (res > 0) {
2180 res = -res;
2181 }
2182
2183 return res;
2184}
2185
2186void *fuse_req_userdata(fuse_req_t req)
2187{
2188 return req->se->userdata;
2189}
2190
2191const struct fuse_ctx *fuse_req_ctx(fuse_req_t req)
2192{
2193 return &req->ctx;
2194}
2195
2196void fuse_req_interrupt_func(fuse_req_t req, fuse_interrupt_func_t func,
2197 void *data)
2198{
2199 pthread_mutex_lock(&req->lock);
2200 pthread_mutex_lock(&req->se->lock);
2201 req->u.ni.func = func;
2202 req->u.ni.data = data;
2203 pthread_mutex_unlock(&req->se->lock);
2204 if (req->interrupted && func) {
2205 func(req, data);
2206 }
2207 pthread_mutex_unlock(&req->lock);
2208}
2209
2210int fuse_req_interrupted(fuse_req_t req)
2211{
2212 int interrupted;
2213
2214 pthread_mutex_lock(&req->se->lock);
2215 interrupted = req->interrupted;
2216 pthread_mutex_unlock(&req->se->lock);
2217
2218 return interrupted;
2219}
2220
2221static struct {
2222 void (*func)(fuse_req_t, fuse_ino_t, struct fuse_mbuf_iter *);
2223 const char *name;
2224} fuse_ll_ops[] = {
2225 [FUSE_LOOKUP] = { do_lookup, "LOOKUP" },
2226 [FUSE_FORGET] = { do_forget, "FORGET" },
2227 [FUSE_GETATTR] = { do_getattr, "GETATTR" },
2228 [FUSE_SETATTR] = { do_setattr, "SETATTR" },
2229 [FUSE_READLINK] = { do_readlink, "READLINK" },
2230 [FUSE_SYMLINK] = { do_symlink, "SYMLINK" },
2231 [FUSE_MKNOD] = { do_mknod, "MKNOD" },
2232 [FUSE_MKDIR] = { do_mkdir, "MKDIR" },
2233 [FUSE_UNLINK] = { do_unlink, "UNLINK" },
2234 [FUSE_RMDIR] = { do_rmdir, "RMDIR" },
2235 [FUSE_RENAME] = { do_rename, "RENAME" },
2236 [FUSE_LINK] = { do_link, "LINK" },
2237 [FUSE_OPEN] = { do_open, "OPEN" },
2238 [FUSE_READ] = { do_read, "READ" },
2239 [FUSE_WRITE] = { do_write, "WRITE" },
2240 [FUSE_STATFS] = { do_statfs, "STATFS" },
2241 [FUSE_RELEASE] = { do_release, "RELEASE" },
2242 [FUSE_FSYNC] = { do_fsync, "FSYNC" },
2243 [FUSE_SETXATTR] = { do_setxattr, "SETXATTR" },
2244 [FUSE_GETXATTR] = { do_getxattr, "GETXATTR" },
2245 [FUSE_LISTXATTR] = { do_listxattr, "LISTXATTR" },
2246 [FUSE_REMOVEXATTR] = { do_removexattr, "REMOVEXATTR" },
2247 [FUSE_FLUSH] = { do_flush, "FLUSH" },
2248 [FUSE_INIT] = { do_init, "INIT" },
2249 [FUSE_OPENDIR] = { do_opendir, "OPENDIR" },
2250 [FUSE_READDIR] = { do_readdir, "READDIR" },
2251 [FUSE_RELEASEDIR] = { do_releasedir, "RELEASEDIR" },
2252 [FUSE_FSYNCDIR] = { do_fsyncdir, "FSYNCDIR" },
2253 [FUSE_GETLK] = { do_getlk, "GETLK" },
2254 [FUSE_SETLK] = { do_setlk, "SETLK" },
2255 [FUSE_SETLKW] = { do_setlkw, "SETLKW" },
2256 [FUSE_ACCESS] = { do_access, "ACCESS" },
2257 [FUSE_CREATE] = { do_create, "CREATE" },
2258 [FUSE_INTERRUPT] = { do_interrupt, "INTERRUPT" },
2259 [FUSE_BMAP] = { do_bmap, "BMAP" },
2260 [FUSE_IOCTL] = { do_ioctl, "IOCTL" },
2261 [FUSE_POLL] = { do_poll, "POLL" },
2262 [FUSE_FALLOCATE] = { do_fallocate, "FALLOCATE" },
2263 [FUSE_DESTROY] = { do_destroy, "DESTROY" },
2264 [FUSE_NOTIFY_REPLY] = { NULL, "NOTIFY_REPLY" },
2265 [FUSE_BATCH_FORGET] = { do_batch_forget, "BATCH_FORGET" },
2266 [FUSE_READDIRPLUS] = { do_readdirplus, "READDIRPLUS" },
2267 [FUSE_RENAME2] = { do_rename2, "RENAME2" },
2268 [FUSE_COPY_FILE_RANGE] = { do_copy_file_range, "COPY_FILE_RANGE" },
2269 [FUSE_LSEEK] = { do_lseek, "LSEEK" },
2270};
2271
2272#define FUSE_MAXOP (sizeof(fuse_ll_ops) / sizeof(fuse_ll_ops[0]))
2273
2274static const char *opname(enum fuse_opcode opcode)
2275{
2276 if (opcode >= FUSE_MAXOP || !fuse_ll_ops[opcode].name) {
2277 return "???";
2278 } else {
2279 return fuse_ll_ops[opcode].name;
2280 }
2281}
2282
2283void fuse_session_process_buf(struct fuse_session *se,
2284 const struct fuse_buf *buf)
2285{
2286 struct fuse_bufvec bufv = { .buf[0] = *buf, .count = 1 };
2287 fuse_session_process_buf_int(se, &bufv, NULL);
2288}
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299void fuse_session_process_buf_int(struct fuse_session *se,
2300 struct fuse_bufvec *bufv,
2301 struct fuse_chan *ch)
2302{
2303 const struct fuse_buf *buf = bufv->buf;
2304 struct fuse_mbuf_iter iter = FUSE_MBUF_ITER_INIT(buf);
2305 struct fuse_in_header *in;
2306 struct fuse_req *req;
2307 int err;
2308
2309
2310 assert(!(buf->flags & FUSE_BUF_IS_FD));
2311
2312 in = fuse_mbuf_iter_advance(&iter, sizeof(*in));
2313 assert(in);
2314
2315 fuse_log(
2316 FUSE_LOG_DEBUG,
2317 "unique: %llu, opcode: %s (%i), nodeid: %llu, insize: %zu, pid: %u\n",
2318 (unsigned long long)in->unique, opname((enum fuse_opcode)in->opcode),
2319 in->opcode, (unsigned long long)in->nodeid, buf->size, in->pid);
2320
2321 req = fuse_ll_alloc_req(se);
2322 if (req == NULL) {
2323 struct fuse_out_header out = {
2324 .unique = in->unique,
2325 .error = -ENOMEM,
2326 };
2327 struct iovec iov = {
2328 .iov_base = &out,
2329 .iov_len = sizeof(struct fuse_out_header),
2330 };
2331
2332 fuse_send_msg(se, ch, &iov, 1);
2333 return;
2334 }
2335
2336 req->unique = in->unique;
2337 req->ctx.uid = in->uid;
2338 req->ctx.gid = in->gid;
2339 req->ctx.pid = in->pid;
2340 req->ch = ch;
2341
2342
2343
2344
2345
2346
2347
2348 if (in->opcode == FUSE_INIT || in->opcode == CUSE_INIT ||
2349 in->opcode == FUSE_DESTROY) {
2350 pthread_rwlock_wrlock(&se->init_rwlock);
2351 } else {
2352 pthread_rwlock_rdlock(&se->init_rwlock);
2353 }
2354
2355 err = EIO;
2356 if (!se->got_init) {
2357 enum fuse_opcode expected;
2358
2359 expected = se->cuse_data ? CUSE_INIT : FUSE_INIT;
2360 if (in->opcode != expected) {
2361 goto reply_err;
2362 }
2363 } else if (in->opcode == FUSE_INIT || in->opcode == CUSE_INIT) {
2364 if (fuse_lowlevel_is_virtio(se)) {
2365
2366
2367
2368
2369
2370 fuse_log(FUSE_LOG_DEBUG, "%s: reinit\n", __func__);
2371 se->got_destroy = 1;
2372 se->got_init = 0;
2373 if (se->op.destroy) {
2374 se->op.destroy(se->userdata);
2375 }
2376 } else {
2377 goto reply_err;
2378 }
2379 }
2380
2381 err = EACCES;
2382
2383 if (se->deny_others && in->uid != se->owner && in->uid != 0 &&
2384 in->opcode != FUSE_INIT && in->opcode != FUSE_READ &&
2385 in->opcode != FUSE_WRITE && in->opcode != FUSE_FSYNC &&
2386 in->opcode != FUSE_RELEASE && in->opcode != FUSE_READDIR &&
2387 in->opcode != FUSE_FSYNCDIR && in->opcode != FUSE_RELEASEDIR &&
2388 in->opcode != FUSE_NOTIFY_REPLY && in->opcode != FUSE_READDIRPLUS) {
2389 goto reply_err;
2390 }
2391
2392 err = ENOSYS;
2393 if (in->opcode >= FUSE_MAXOP || !fuse_ll_ops[in->opcode].func) {
2394 goto reply_err;
2395 }
2396 if (in->opcode != FUSE_INTERRUPT) {
2397 struct fuse_req *intr;
2398 pthread_mutex_lock(&se->lock);
2399 intr = check_interrupt(se, req);
2400 list_add_req(req, &se->list);
2401 pthread_mutex_unlock(&se->lock);
2402 if (intr) {
2403 fuse_reply_err(intr, EAGAIN);
2404 }
2405 }
2406
2407 if (in->opcode == FUSE_WRITE && se->op.write_buf) {
2408 do_write_buf(req, in->nodeid, &iter, bufv);
2409 } else {
2410 fuse_ll_ops[in->opcode].func(req, in->nodeid, &iter);
2411 }
2412
2413 pthread_rwlock_unlock(&se->init_rwlock);
2414 return;
2415
2416reply_err:
2417 fuse_reply_err(req, err);
2418 pthread_rwlock_unlock(&se->init_rwlock);
2419}
2420
2421#define LL_OPTION(n, o, v) \
2422 { \
2423 n, offsetof(struct fuse_session, o), v \
2424 }
2425
2426static const struct fuse_opt fuse_ll_opts[] = {
2427 LL_OPTION("debug", debug, 1),
2428 LL_OPTION("-d", debug, 1),
2429 LL_OPTION("--debug", debug, 1),
2430 LL_OPTION("allow_root", deny_others, 1),
2431 LL_OPTION("--socket-path=%s", vu_socket_path, 0),
2432 LL_OPTION("--socket-group=%s", vu_socket_group, 0),
2433 LL_OPTION("--fd=%d", vu_listen_fd, 0),
2434 LL_OPTION("--thread-pool-size=%d", thread_pool_size, 0),
2435 FUSE_OPT_END
2436};
2437
2438void fuse_lowlevel_version(void)
2439{
2440 printf("using FUSE kernel interface version %i.%i\n", FUSE_KERNEL_VERSION,
2441 FUSE_KERNEL_MINOR_VERSION);
2442}
2443
2444void fuse_lowlevel_help(void)
2445{
2446
2447
2448
2449
2450 printf(
2451 " -o allow_root allow access by root\n"
2452 " --socket-path=PATH path for the vhost-user socket\n"
2453 " --socket-group=GRNAME name of group for the vhost-user socket\n"
2454 " --fd=FDNUM fd number of vhost-user socket\n"
2455 " --thread-pool-size=NUM thread pool size limit (default %d)\n",
2456 THREAD_POOL_SIZE);
2457}
2458
2459void fuse_session_destroy(struct fuse_session *se)
2460{
2461 if (se->got_init && !se->got_destroy) {
2462 if (se->op.destroy) {
2463 se->op.destroy(se->userdata);
2464 }
2465 }
2466 pthread_rwlock_destroy(&se->init_rwlock);
2467 pthread_mutex_destroy(&se->lock);
2468 free(se->cuse_data);
2469 if (se->fd != -1) {
2470 close(se->fd);
2471 }
2472
2473 if (fuse_lowlevel_is_virtio(se)) {
2474 virtio_session_close(se);
2475 }
2476
2477 free(se->vu_socket_path);
2478 se->vu_socket_path = NULL;
2479
2480 free(se);
2481}
2482
2483
2484struct fuse_session *fuse_session_new(struct fuse_args *args,
2485 const struct fuse_lowlevel_ops *op,
2486 size_t op_size, void *userdata)
2487{
2488 struct fuse_session *se;
2489
2490 if (sizeof(struct fuse_lowlevel_ops) < op_size) {
2491 fuse_log(
2492 FUSE_LOG_ERR,
2493 "fuse: warning: library too old, some operations may not work\n");
2494 op_size = sizeof(struct fuse_lowlevel_ops);
2495 }
2496
2497 if (args->argc == 0) {
2498 fuse_log(FUSE_LOG_ERR,
2499 "fuse: empty argv passed to fuse_session_new().\n");
2500 return NULL;
2501 }
2502
2503 se = (struct fuse_session *)calloc(1, sizeof(struct fuse_session));
2504 if (se == NULL) {
2505 fuse_log(FUSE_LOG_ERR, "fuse: failed to allocate fuse object\n");
2506 goto out1;
2507 }
2508 se->fd = -1;
2509 se->vu_listen_fd = -1;
2510 se->thread_pool_size = THREAD_POOL_SIZE;
2511 se->conn.max_write = UINT_MAX;
2512 se->conn.max_readahead = UINT_MAX;
2513
2514
2515 if (fuse_opt_parse(args, se, fuse_ll_opts, NULL) == -1) {
2516 goto out2;
2517 }
2518 if (args->argc == 1 && args->argv[0][0] == '-') {
2519 fuse_log(FUSE_LOG_ERR,
2520 "fuse: warning: argv[0] looks like an option, but "
2521 "will be ignored\n");
2522 } else if (args->argc != 1) {
2523 int i;
2524 fuse_log(FUSE_LOG_ERR, "fuse: unknown option(s): `");
2525 for (i = 1; i < args->argc - 1; i++) {
2526 fuse_log(FUSE_LOG_ERR, "%s ", args->argv[i]);
2527 }
2528 fuse_log(FUSE_LOG_ERR, "%s'\n", args->argv[i]);
2529 goto out4;
2530 }
2531
2532 if (!se->vu_socket_path && se->vu_listen_fd < 0) {
2533 fuse_log(FUSE_LOG_ERR, "fuse: missing --socket-path or --fd option\n");
2534 goto out4;
2535 }
2536 if (se->vu_socket_path && se->vu_listen_fd >= 0) {
2537 fuse_log(FUSE_LOG_ERR,
2538 "fuse: --socket-path and --fd cannot be given together\n");
2539 goto out4;
2540 }
2541 if (se->vu_socket_group && !se->vu_socket_path) {
2542 fuse_log(FUSE_LOG_ERR,
2543 "fuse: --socket-group can only be used with --socket-path\n");
2544 goto out4;
2545 }
2546
2547 se->bufsize = FUSE_MAX_MAX_PAGES * getpagesize() + FUSE_BUFFER_HEADER_SIZE;
2548
2549 list_init_req(&se->list);
2550 list_init_req(&se->interrupts);
2551 fuse_mutex_init(&se->lock);
2552 pthread_rwlock_init(&se->init_rwlock, NULL);
2553
2554 memcpy(&se->op, op, op_size);
2555 se->owner = getuid();
2556 se->userdata = userdata;
2557
2558 return se;
2559
2560out4:
2561 fuse_opt_free_args(args);
2562out2:
2563 free(se);
2564out1:
2565 return NULL;
2566}
2567
2568int fuse_session_mount(struct fuse_session *se)
2569{
2570 return virtio_session_mount(se);
2571}
2572
2573int fuse_session_fd(struct fuse_session *se)
2574{
2575 return se->fd;
2576}
2577
2578void fuse_session_unmount(struct fuse_session *se)
2579{
2580}
2581
2582int fuse_lowlevel_is_virtio(struct fuse_session *se)
2583{
2584 return !!se->virtio_dev;
2585}
2586
2587void fuse_session_exit(struct fuse_session *se)
2588{
2589 se->exited = 1;
2590}
2591
2592void fuse_session_reset(struct fuse_session *se)
2593{
2594 se->exited = 0;
2595 se->error = 0;
2596}
2597
2598int fuse_session_exited(struct fuse_session *se)
2599{
2600 return se->exited;
2601}
2602