2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2007-2009 Google Inc. and Amit Singh
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following disclaimer
15 * in the documentation and/or other materials provided with the
17 * * Neither the name of Google Inc. nor the names of its
18 * contributors may be used to endorse or promote products derived from
19 * this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * Copyright (C) 2005 Csaba Henk.
34 * All rights reserved.
36 * Copyright (c) 2019 The FreeBSD Foundation
38 * Portions of this software were developed by BFF Storage Systems, LLC under
39 * sponsorship from the FreeBSD Foundation.
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
44 * 1. Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in the
48 * documentation and/or other materials provided with the distribution.
50 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 #include <sys/cdefs.h>
64 __FBSDID("$FreeBSD$");
66 #include <sys/param.h>
67 #include <sys/module.h>
68 #include <sys/systm.h>
69 #include <sys/counter.h>
70 #include <sys/errno.h>
71 #include <sys/kernel.h>
74 #include <sys/malloc.h>
75 #include <sys/queue.h>
78 #include <sys/mutex.h>
80 #include <sys/mount.h>
82 #include <sys/vnode.h>
83 #include <sys/signalvar.h>
84 #include <sys/syscallsubr.h>
85 #include <sys/sysctl.h>
89 #include "fuse_node.h"
91 #include "fuse_internal.h"
93 SDT_PROVIDER_DECLARE(fusefs);
96 * arg0: verbosity. Higher numbers give more verbose messages
97 * arg1: Textual message
99 SDT_PROBE_DEFINE2(fusefs, , ipc, trace, "int", "char*");
101 static void fdisp_make_pid(struct fuse_dispatcher *fdip, enum fuse_opcode op,
102 struct fuse_data *data, uint64_t nid, pid_t pid, struct ucred *cred);
103 static void fuse_interrupt_send(struct fuse_ticket *otick, int err);
104 static struct fuse_ticket *fticket_alloc(struct fuse_data *data);
105 static void fticket_refresh(struct fuse_ticket *ftick);
106 static inline void fticket_reset(struct fuse_ticket *ftick);
107 static void fticket_destroy(struct fuse_ticket *ftick);
108 static int fticket_wait_answer(struct fuse_ticket *ftick);
110 fticket_aw_pull_uio(struct fuse_ticket *ftick,
113 static int fuse_body_audit(struct fuse_ticket *ftick, size_t blen);
115 static fuse_handler_t fuse_standard_handler;
117 static counter_u64_t fuse_ticket_count;
118 SYSCTL_COUNTER_U64(_vfs_fusefs_stats, OID_AUTO, ticket_count, CTLFLAG_RD,
119 &fuse_ticket_count, "Number of allocated tickets");
121 static long fuse_iov_permanent_bufsize = 1 << 19;
123 SYSCTL_LONG(_vfs_fusefs, OID_AUTO, iov_permanent_bufsize, CTLFLAG_RW,
124 &fuse_iov_permanent_bufsize, 0,
125 "limit for permanently stored buffer size for fuse_iovs");
126 static int fuse_iov_credit = 16;
128 SYSCTL_INT(_vfs_fusefs, OID_AUTO, iov_credit, CTLFLAG_RW,
130 "how many times is an oversized fuse_iov tolerated");
132 MALLOC_DEFINE(M_FUSEMSG, "fuse_msgbuf", "fuse message buffer");
133 static uma_zone_t ticket_zone;
136 * TODO: figure out how to timeout INTERRUPT requests, because the daemon may
137 * leagally never respond
140 fuse_interrupt_callback(struct fuse_ticket *tick, struct uio *uio)
142 struct fuse_ticket *otick, *x_tick;
143 struct fuse_interrupt_in *fii;
144 struct fuse_data *data = tick->tk_data;
147 fii = (struct fuse_interrupt_in*)((char*)tick->tk_ms_fiov.base +
148 sizeof(struct fuse_in_header));
150 fuse_lck_mtx_lock(data->aw_mtx);
151 TAILQ_FOREACH_SAFE(otick, &data->aw_head, tk_aw_link, x_tick) {
152 if (otick->tk_unique == fii->unique) {
157 fuse_lck_mtx_unlock(data->aw_mtx);
160 /* Original is already complete. Just return */
164 /* Clear the original ticket's interrupt association */
165 otick->irq_unique = 0;
167 if (tick->tk_aw_ohead.error == ENOSYS) {
168 fsess_set_notimpl(data->mp, FUSE_INTERRUPT);
170 } else if (tick->tk_aw_ohead.error == EAGAIN) {
172 * There are two reasons we might get this:
173 * 1) the daemon received the INTERRUPT request before the
175 * 2) the daemon received the INTERRUPT request after it
176 * completed the original request.
177 * In the first case we should re-send the INTERRUPT. In the
178 * second, we should ignore it.
181 fuse_interrupt_send(otick, EINTR);
184 /* Illegal FUSE_INTERRUPT response */
189 /* Interrupt the operation otick. Return err as its error code */
191 fuse_interrupt_send(struct fuse_ticket *otick, int err)
193 struct fuse_dispatcher fdi;
194 struct fuse_interrupt_in *fii;
195 struct fuse_in_header *ftick_hdr;
196 struct fuse_data *data = otick->tk_data;
197 struct fuse_ticket *tick, *xtick;
198 struct ucred reused_creds;
199 gid_t reused_groups[1];
201 if (otick->irq_unique == 0) {
203 * If the daemon hasn't yet received otick, then we can answer
204 * it ourselves and return.
206 fuse_lck_mtx_lock(data->ms_mtx);
207 STAILQ_FOREACH_SAFE(tick, &otick->tk_data->ms_head, tk_ms_link,
210 STAILQ_REMOVE(&otick->tk_data->ms_head, tick,
211 fuse_ticket, tk_ms_link);
212 otick->tk_data->ms_count--;
213 otick->tk_ms_link.stqe_next = NULL;
214 fuse_lck_mtx_unlock(data->ms_mtx);
216 fuse_lck_mtx_lock(otick->tk_aw_mtx);
217 if (!fticket_answered(otick)) {
218 fticket_set_answered(otick);
219 otick->tk_aw_errno = err;
222 fuse_lck_mtx_unlock(otick->tk_aw_mtx);
224 fuse_ticket_drop(tick);
228 fuse_lck_mtx_unlock(data->ms_mtx);
231 * If the fuse daemon doesn't support interrupts, then there's
232 * nothing more that we can do
234 if (fsess_not_impl(data->mp, FUSE_INTERRUPT))
238 * If the fuse daemon has already received otick, then we must
239 * send FUSE_INTERRUPT.
241 ftick_hdr = fticket_in_header(otick);
242 reused_creds.cr_uid = ftick_hdr->uid;
243 reused_groups[0] = ftick_hdr->gid;
244 reused_creds.cr_groups = reused_groups;
245 fdisp_init(&fdi, sizeof(*fii));
246 fdisp_make_pid(&fdi, FUSE_INTERRUPT, data, ftick_hdr->nodeid,
247 ftick_hdr->pid, &reused_creds);
250 fii->unique = otick->tk_unique;
251 fuse_insert_callback(fdi.tick, fuse_interrupt_callback);
253 otick->irq_unique = fdi.tick->tk_unique;
254 /* Interrupt ops should be delivered ASAP */
255 fuse_insert_message(fdi.tick, true);
258 /* This ticket has already been interrupted */
263 fiov_init(struct fuse_iov *fiov, size_t size)
265 uint32_t msize = FU_AT_LEAST(size);
269 fiov->base = malloc(msize, M_FUSEMSG, M_WAITOK | M_ZERO);
271 fiov->allocated_size = msize;
272 fiov->credit = fuse_iov_credit;
276 fiov_teardown(struct fuse_iov *fiov)
278 MPASS(fiov->base != NULL);
279 free(fiov->base, M_FUSEMSG);
283 fiov_adjust(struct fuse_iov *fiov, size_t size)
285 if (fiov->allocated_size < size ||
286 (fuse_iov_permanent_bufsize >= 0 &&
287 fiov->allocated_size - size > fuse_iov_permanent_bufsize &&
288 --fiov->credit < 0)) {
289 fiov->base = realloc(fiov->base, FU_AT_LEAST(size), M_FUSEMSG,
292 panic("FUSE: realloc failed");
294 fiov->allocated_size = FU_AT_LEAST(size);
295 fiov->credit = fuse_iov_credit;
296 /* Clear data buffer after reallocation */
297 bzero(fiov->base, size);
298 } else if (size > fiov->len) {
299 /* Clear newly extended portion of data buffer */
300 bzero((char*)fiov->base + fiov->len, size - fiov->len);
305 /* Resize the fiov if needed, and clear it's buffer */
307 fiov_refresh(struct fuse_iov *fiov)
309 fiov_adjust(fiov, 0);
313 fticket_ctor(void *mem, int size, void *arg, int flags)
315 struct fuse_ticket *ftick = mem;
316 struct fuse_data *data = arg;
318 FUSE_ASSERT_MS_DONE(ftick);
319 FUSE_ASSERT_AW_DONE(ftick);
321 ftick->tk_data = data;
322 ftick->irq_unique = 0;
323 refcount_init(&ftick->tk_refcount, 1);
324 counter_u64_add(fuse_ticket_count, 1);
326 fticket_refresh(ftick);
332 fticket_dtor(void *mem, int size, void *arg)
335 struct fuse_ticket *ftick = mem;
338 FUSE_ASSERT_MS_DONE(ftick);
339 FUSE_ASSERT_AW_DONE(ftick);
341 counter_u64_add(fuse_ticket_count, -1);
345 fticket_init(void *mem, int size, int flags)
347 struct fuse_ticket *ftick = mem;
349 bzero(ftick, sizeof(struct fuse_ticket));
351 fiov_init(&ftick->tk_ms_fiov, sizeof(struct fuse_in_header));
353 mtx_init(&ftick->tk_aw_mtx, "fuse answer delivery mutex", NULL, MTX_DEF);
354 fiov_init(&ftick->tk_aw_fiov, 0);
360 fticket_fini(void *mem, int size)
362 struct fuse_ticket *ftick = mem;
364 fiov_teardown(&ftick->tk_ms_fiov);
365 fiov_teardown(&ftick->tk_aw_fiov);
366 mtx_destroy(&ftick->tk_aw_mtx);
369 static inline struct fuse_ticket *
370 fticket_alloc(struct fuse_data *data)
372 return uma_zalloc_arg(ticket_zone, data, M_WAITOK);
376 fticket_destroy(struct fuse_ticket *ftick)
378 return uma_zfree(ticket_zone, ftick);
381 /* Prepare the ticket to be reused and clear its data buffers */
383 fticket_refresh(struct fuse_ticket *ftick)
385 fticket_reset(ftick);
387 fiov_refresh(&ftick->tk_ms_fiov);
388 fiov_refresh(&ftick->tk_aw_fiov);
391 /* Prepare the ticket to be reused, but don't clear its data buffers */
393 fticket_reset(struct fuse_ticket *ftick)
395 struct fuse_data *data = ftick->tk_data;
397 FUSE_ASSERT_MS_DONE(ftick);
398 FUSE_ASSERT_AW_DONE(ftick);
400 bzero(&ftick->tk_aw_ohead, sizeof(struct fuse_out_header));
402 ftick->tk_aw_errno = 0;
405 /* May be truncated to 32 bits on LP32 arches */
406 ftick->tk_unique = atomic_fetchadd_long(&data->ticketer, 1);
407 if (ftick->tk_unique == 0)
408 ftick->tk_unique = atomic_fetchadd_long(&data->ticketer, 1);
412 fticket_wait_answer(struct fuse_ticket *ftick)
414 struct thread *td = curthread;
415 sigset_t blockedset, oldset;
416 int err = 0, stops_deferred;
417 struct fuse_data *data = ftick->tk_data;
418 bool interrupted = false;
420 if (fsess_maybe_impl(ftick->tk_data->mp, FUSE_INTERRUPT) &&
421 data->dataflags & FSESS_INTR) {
422 SIGEMPTYSET(blockedset);
424 /* Block all signals except (implicitly) SIGKILL */
425 SIGFILLSET(blockedset);
427 stops_deferred = sigdeferstop(SIGDEFERSTOP_SILENT);
428 kern_sigprocmask(td, SIG_BLOCK, NULL, &oldset, 0);
430 fuse_lck_mtx_lock(ftick->tk_aw_mtx);
433 if (fticket_answered(ftick)) {
437 if (fdata_get_dead(data)) {
439 fticket_set_answered(ftick);
442 kern_sigprocmask(td, SIG_BLOCK, &blockedset, NULL, 0);
443 err = msleep(ftick, &ftick->tk_aw_mtx, PCATCH, "fu_ans",
444 data->daemon_timeout * hz);
445 kern_sigprocmask(td, SIG_SETMASK, &oldset, NULL, 0);
446 if (err == EWOULDBLOCK) {
447 SDT_PROBE2(fusefs, , ipc, trace, 3,
448 "fticket_wait_answer: EWOULDBLOCK");
449 #ifdef XXXIP /* die conditionally */
450 if (!fdata_get_dead(data)) {
451 fdata_set_dead(data);
455 fticket_set_answered(ftick);
456 } else if ((err == EINTR || err == ERESTART)) {
458 * Whether we get EINTR or ERESTART depends on whether
459 * SA_RESTART was set by sigaction(2).
461 * Try to interrupt the operation and wait for an EINTR response
462 * to the original operation. If the file system does not
463 * support FUSE_INTERRUPT, then we'll just wait for it to
464 * complete like normal. If it does support FUSE_INTERRUPT,
465 * then it will either respond EINTR to the original operation,
466 * or EAGAIN to the interrupt.
470 SDT_PROBE2(fusefs, , ipc, trace, 4,
471 "fticket_wait_answer: interrupt");
472 fuse_lck_mtx_unlock(ftick->tk_aw_mtx);
473 fuse_interrupt_send(ftick, err);
475 PROC_LOCK(td->td_proc);
476 mtx_lock(&td->td_proc->p_sigacts->ps_mtx);
477 tmpset = td->td_proc->p_siglist;
478 SIGSETOR(tmpset, td->td_siglist);
479 mtx_unlock(&td->td_proc->p_sigacts->ps_mtx);
480 PROC_UNLOCK(td->td_proc);
482 fuse_lck_mtx_lock(ftick->tk_aw_mtx);
483 if (!interrupted && !SIGISMEMBER(tmpset, SIGKILL)) {
485 * Block all signals while we wait for an interrupt
486 * response. The protocol doesn't discriminate between
489 SIGFILLSET(blockedset);
494 * Return immediately for fatal signals, or if this is
495 * the second interruption. We should only be
496 * interrupted twice if the thread is stopped, for
497 * example during sigexit.
501 SDT_PROBE2(fusefs, , ipc, trace, 6,
502 "fticket_wait_answer: other error");
504 SDT_PROBE2(fusefs, , ipc, trace, 7, "fticket_wait_answer: OK");
507 if (!(err || fticket_answered(ftick))) {
508 SDT_PROBE2(fusefs, , ipc, trace, 1,
509 "FUSE: requester was woken up but still no answer");
512 fuse_lck_mtx_unlock(ftick->tk_aw_mtx);
513 sigallowstop(stops_deferred);
520 fticket_aw_pull_uio(struct fuse_ticket *ftick, struct uio *uio)
523 size_t len = uio_resid(uio);
526 fiov_adjust(fticket_resp(ftick), len);
527 err = uiomove(fticket_resp(ftick)->base, len, uio);
533 fticket_pull(struct fuse_ticket *ftick, struct uio *uio)
537 if (ftick->tk_aw_ohead.error) {
540 err = fuse_body_audit(ftick, uio_resid(uio));
542 err = fticket_aw_pull_uio(ftick, uio);
548 fdata_alloc(struct cdev *fdev, struct ucred *cred)
550 struct fuse_data *data;
552 data = malloc(sizeof(struct fuse_data), M_FUSEMSG, M_WAITOK | M_ZERO);
555 mtx_init(&data->ms_mtx, "fuse message list mutex", NULL, MTX_DEF);
556 STAILQ_INIT(&data->ms_head);
558 knlist_init_mtx(&data->ks_rsel.si_note, &data->ms_mtx);
559 mtx_init(&data->aw_mtx, "fuse answer list mutex", NULL, MTX_DEF);
560 TAILQ_INIT(&data->aw_head);
561 data->daemoncred = crhold(cred);
562 data->daemon_timeout = FUSE_DEFAULT_DAEMON_TIMEOUT;
563 sx_init(&data->rename_lock, "fuse rename lock");
570 fdata_trydestroy(struct fuse_data *data)
573 MPASS(data->ref >= 0);
577 /* Driving off stage all that stuff thrown at device... */
578 sx_destroy(&data->rename_lock);
579 crfree(data->daemoncred);
580 mtx_destroy(&data->aw_mtx);
581 knlist_delete(&data->ks_rsel.si_note, curthread, 0);
582 knlist_destroy(&data->ks_rsel.si_note);
583 mtx_destroy(&data->ms_mtx);
585 free(data, M_FUSEMSG);
589 fdata_set_dead(struct fuse_data *data)
592 if (fdata_get_dead(data)) {
596 fuse_lck_mtx_lock(data->ms_mtx);
597 data->dataflags |= FSESS_DEAD;
599 selwakeuppri(&data->ks_rsel, PZERO + 1);
600 wakeup(&data->ticketer);
601 fuse_lck_mtx_unlock(data->ms_mtx);
606 fuse_ticket_fetch(struct fuse_data *data)
609 struct fuse_ticket *ftick;
611 ftick = fticket_alloc(data);
613 if (!(data->dataflags & FSESS_INITED)) {
614 /* Sleep until get answer for INIT messsage */
616 if (!(data->dataflags & FSESS_INITED) && data->ticketer > 2) {
617 err = msleep(&data->ticketer, &fuse_mtx, PCATCH | PDROP,
620 fdata_set_dead(data);
628 fuse_ticket_drop(struct fuse_ticket *ftick)
632 die = refcount_release(&ftick->tk_refcount);
634 fticket_destroy(ftick);
640 fuse_insert_callback(struct fuse_ticket *ftick, fuse_handler_t * handler)
642 if (fdata_get_dead(ftick->tk_data)) {
645 ftick->tk_aw_handler = handler;
647 fuse_lck_mtx_lock(ftick->tk_data->aw_mtx);
649 fuse_lck_mtx_unlock(ftick->tk_data->aw_mtx);
653 * Insert a new upgoing ticket into the message queue
655 * If urgent is true, insert at the front of the queue. Otherwise, insert in
659 fuse_insert_message(struct fuse_ticket *ftick, bool urgent)
661 if (ftick->tk_flag & FT_DIRTY) {
662 panic("FUSE: ticket reused without being refreshed");
664 ftick->tk_flag |= FT_DIRTY;
666 if (fdata_get_dead(ftick->tk_data)) {
669 fuse_lck_mtx_lock(ftick->tk_data->ms_mtx);
671 fuse_ms_push_head(ftick);
674 wakeup_one(ftick->tk_data);
675 selwakeuppri(&ftick->tk_data->ks_rsel, PZERO + 1);
676 KNOTE_LOCKED(&ftick->tk_data->ks_rsel.si_note, 0);
677 fuse_lck_mtx_unlock(ftick->tk_data->ms_mtx);
681 fuse_body_audit(struct fuse_ticket *ftick, size_t blen)
684 enum fuse_opcode opcode;
686 opcode = fticket_opcode(ftick);
690 err = (blen == sizeof(struct fuse_bmap_out)) ? 0 : EINVAL;
698 if (fuse_libabi_geq(ftick->tk_data, 7, 9)) {
699 err = (blen == sizeof(struct fuse_entry_out)) ?
702 err = (blen == FUSE_COMPAT_ENTRY_OUT_SIZE) ? 0 : EINVAL;
707 panic("FUSE: a handler has been intalled for FUSE_FORGET");
712 if (fuse_libabi_geq(ftick->tk_data, 7, 9)) {
713 err = (blen == sizeof(struct fuse_attr_out)) ?
716 err = (blen == FUSE_COMPAT_ATTR_OUT_SIZE) ? 0 : EINVAL;
721 err = (PAGE_SIZE >= blen) ? 0 : EINVAL;
725 err = (blen == 0) ? 0 : EINVAL;
729 err = (blen == 0) ? 0 : EINVAL;
733 err = (blen == 0) ? 0 : EINVAL;
737 err = (blen == sizeof(struct fuse_open_out)) ? 0 : EINVAL;
741 err = (((struct fuse_read_in *)(
742 (char *)ftick->tk_ms_fiov.base +
743 sizeof(struct fuse_in_header)
744 ))->size >= blen) ? 0 : EINVAL;
748 err = (blen == sizeof(struct fuse_write_out)) ? 0 : EINVAL;
752 if (fuse_libabi_geq(ftick->tk_data, 7, 4)) {
753 err = (blen == sizeof(struct fuse_statfs_out)) ?
756 err = (blen == FUSE_COMPAT_STATFS_SIZE) ? 0 : EINVAL;
761 err = (blen == 0) ? 0 : EINVAL;
765 err = (blen == 0) ? 0 : EINVAL;
769 err = (blen == 0) ? 0 : EINVAL;
775 * These can have varying response lengths, and 0 length
776 * isn't necessarily invalid.
781 case FUSE_REMOVEXATTR:
782 err = (blen == 0) ? 0 : EINVAL;
786 err = (blen == 0) ? 0 : EINVAL;
790 if (blen == sizeof(struct fuse_init_out) ||
791 blen == FUSE_COMPAT_INIT_OUT_SIZE ||
792 blen == FUSE_COMPAT_22_INIT_OUT_SIZE) {
800 err = (blen == sizeof(struct fuse_open_out)) ? 0 : EINVAL;
804 err = (((struct fuse_read_in *)(
805 (char *)ftick->tk_ms_fiov.base +
806 sizeof(struct fuse_in_header)
807 ))->size >= blen) ? 0 : EINVAL;
810 case FUSE_RELEASEDIR:
811 err = (blen == 0) ? 0 : EINVAL;
815 err = (blen == 0) ? 0 : EINVAL;
819 err = (blen == sizeof(struct fuse_lk_out)) ? 0 : EINVAL;
823 err = (blen == 0) ? 0 : EINVAL;
827 err = (blen == 0) ? 0 : EINVAL;
831 err = (blen == 0) ? 0 : EINVAL;
835 if (fuse_libabi_geq(ftick->tk_data, 7, 9)) {
836 err = (blen == sizeof(struct fuse_entry_out) +
837 sizeof(struct fuse_open_out)) ? 0 : EINVAL;
839 err = (blen == FUSE_COMPAT_ENTRY_OUT_SIZE +
840 sizeof(struct fuse_open_out)) ? 0 : EINVAL;
845 err = (blen == 0) ? 0 : EINVAL;
849 err = (blen == 0) ? 0 : EINVAL;
853 err = (blen == sizeof(struct fuse_lseek_out)) ? 0 : EINVAL;
856 case FUSE_COPY_FILE_RANGE:
857 err = (blen == sizeof(struct fuse_write_out)) ? 0 : EINVAL;
861 panic("FUSE: opcodes out of sync (%d)\n", opcode);
868 fuse_setup_ihead(struct fuse_in_header *ihead, struct fuse_ticket *ftick,
869 uint64_t nid, enum fuse_opcode op, size_t blen, pid_t pid,
872 ihead->len = sizeof(*ihead) + blen;
873 ihead->unique = ftick->tk_unique;
878 ihead->uid = cred->cr_uid;
879 ihead->gid = cred->cr_groups[0];
883 * fuse_standard_handler just pulls indata and wakes up pretender.
884 * Doesn't try to interpret data, that's left for the pretender.
885 * Though might do a basic size verification before the pull-in takes place
889 fuse_standard_handler(struct fuse_ticket *ftick, struct uio *uio)
893 err = fticket_pull(ftick, uio);
895 fuse_lck_mtx_lock(ftick->tk_aw_mtx);
897 if (!fticket_answered(ftick)) {
898 fticket_set_answered(ftick);
899 ftick->tk_aw_errno = err;
902 fuse_lck_mtx_unlock(ftick->tk_aw_mtx);
908 * Reinitialize a dispatcher from a pid and node id, without resizing or
909 * clearing its data buffers
912 fdisp_refresh_pid(struct fuse_dispatcher *fdip, enum fuse_opcode op,
913 struct mount *mp, uint64_t nid, pid_t pid, struct ucred *cred)
916 MPASS2(sizeof(fdip->finh) + fdip->iosize <= fdip->tick->tk_ms_fiov.len,
917 "Must use fdisp_make_pid to increase the size of the fiov");
918 fticket_reset(fdip->tick);
920 FUSE_DIMALLOC(&fdip->tick->tk_ms_fiov, fdip->finh,
921 fdip->indata, fdip->iosize);
923 fuse_setup_ihead(fdip->finh, fdip->tick, nid, op, fdip->iosize, pid,
927 /* Initialize a dispatcher from a pid and node id */
929 fdisp_make_pid(struct fuse_dispatcher *fdip, enum fuse_opcode op,
930 struct fuse_data *data, uint64_t nid, pid_t pid, struct ucred *cred)
933 fticket_refresh(fdip->tick);
935 fdip->tick = fuse_ticket_fetch(data);
938 /* FUSE_DIMALLOC will bzero the fiovs when it enlarges them */
939 FUSE_DIMALLOC(&fdip->tick->tk_ms_fiov, fdip->finh,
940 fdip->indata, fdip->iosize);
942 fuse_setup_ihead(fdip->finh, fdip->tick, nid, op, fdip->iosize, pid, cred);
946 fdisp_make(struct fuse_dispatcher *fdip, enum fuse_opcode op, struct mount *mp,
947 uint64_t nid, struct thread *td, struct ucred *cred)
949 struct fuse_data *data = fuse_get_mpdata(mp);
950 RECTIFY_TDCR(td, cred);
952 return fdisp_make_pid(fdip, op, data, nid, td->td_proc->p_pid, cred);
956 fdisp_make_vp(struct fuse_dispatcher *fdip, enum fuse_opcode op,
957 struct vnode *vp, struct thread *td, struct ucred *cred)
959 struct mount *mp = vnode_mount(vp);
960 struct fuse_data *data = fuse_get_mpdata(mp);
962 RECTIFY_TDCR(td, cred);
963 return fdisp_make_pid(fdip, op, data, VTOI(vp),
964 td->td_proc->p_pid, cred);
967 /* Refresh a fuse_dispatcher so it can be reused, but don't zero its data */
969 fdisp_refresh_vp(struct fuse_dispatcher *fdip, enum fuse_opcode op,
970 struct vnode *vp, struct thread *td, struct ucred *cred)
972 RECTIFY_TDCR(td, cred);
973 return fdisp_refresh_pid(fdip, op, vnode_mount(vp), VTOI(vp),
974 td->td_proc->p_pid, cred);
977 SDT_PROBE_DEFINE2(fusefs, , ipc, fdisp_wait_answ_error, "char*", "int");
980 fdisp_wait_answ(struct fuse_dispatcher *fdip)
985 fuse_insert_callback(fdip->tick, fuse_standard_handler);
986 fuse_insert_message(fdip->tick, false);
988 if ((err = fticket_wait_answer(fdip->tick))) {
989 fuse_lck_mtx_lock(fdip->tick->tk_aw_mtx);
991 if (fticket_answered(fdip->tick)) {
993 * Just between noticing the interrupt and getting here,
994 * the standard handler has completed his job.
995 * So we drop the ticket and exit as usual.
997 SDT_PROBE2(fusefs, , ipc, fdisp_wait_answ_error,
998 "IPC: interrupted, already answered", err);
999 fuse_lck_mtx_unlock(fdip->tick->tk_aw_mtx);
1003 * So we were faster than the standard handler.
1004 * Then by setting the answered flag we get *him*
1005 * to drop the ticket.
1007 SDT_PROBE2(fusefs, , ipc, fdisp_wait_answ_error,
1008 "IPC: interrupted, setting to answered", err);
1009 fticket_set_answered(fdip->tick);
1010 fuse_lck_mtx_unlock(fdip->tick->tk_aw_mtx);
1015 if (fdip->tick->tk_aw_errno == ENOTCONN) {
1016 /* The daemon died while we were waiting for a response */
1019 } else if (fdip->tick->tk_aw_errno) {
1021 * There was some sort of communication error with the daemon
1022 * that the client wouldn't understand.
1024 SDT_PROBE2(fusefs, , ipc, fdisp_wait_answ_error,
1025 "IPC: explicit EIO-ing", fdip->tick->tk_aw_errno);
1029 if ((err = fdip->tick->tk_aw_ohead.error)) {
1030 SDT_PROBE2(fusefs, , ipc, fdisp_wait_answ_error,
1031 "IPC: setting status", fdip->tick->tk_aw_ohead.error);
1033 * This means a "proper" fuse syscall error.
1034 * We record this value so the caller will
1035 * be able to know it's not a boring messaging
1036 * failure, if she wishes so (and if not, she can
1037 * just simply propagate the return value of this routine).
1038 * [XXX Maybe a bitflag would do the job too,
1039 * if other flags needed, this will be converted thusly.]
1041 fdip->answ_stat = err;
1044 fdip->answ = fticket_resp(fdip->tick)->base;
1045 fdip->iosize = fticket_resp(fdip->tick)->len;
1056 ticket_zone = uma_zcreate("fuse_ticket", sizeof(struct fuse_ticket),
1057 fticket_ctor, fticket_dtor, fticket_init, fticket_fini,
1059 fuse_ticket_count = counter_u64_alloc(M_WAITOK);
1063 fuse_ipc_destroy(void)
1065 counter_u64_free(fuse_ticket_count);
1066 uma_zdestroy(ticket_zone);
1069 SDT_PROBE_DEFINE3(fusefs,, ipc, warn, "struct fuse_data*", "unsigned", "char*");
1071 fuse_warn(struct fuse_data *data, unsigned flag, const char *msg)
1073 SDT_PROBE3(fusefs, , ipc, warn, data, flag, msg);
1074 if (!(data->dataflags & flag)) {
1075 printf("WARNING: FUSE protocol violation for server mounted at "
1077 "This warning will not be repeated.\n",
1078 data->mp->mnt_stat.f_mntonname, msg);
1079 data->dataflags |= flag;