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