xref: /freebsd/sys/fs/fuse/fuse_ipc.c (revision ed74f781c9f704092556f860a00b0bb53fdedff7)
151369649SPedro F. Giffuni /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
45fe58019SAttilio Rao  * Copyright (c) 2007-2009 Google Inc. and Amit Singh
55fe58019SAttilio Rao  * All rights reserved.
65fe58019SAttilio Rao  *
75fe58019SAttilio Rao  * Redistribution and use in source and binary forms, with or without
85fe58019SAttilio Rao  * modification, are permitted provided that the following conditions are
95fe58019SAttilio Rao  * met:
105fe58019SAttilio Rao  *
115fe58019SAttilio Rao  * * Redistributions of source code must retain the above copyright
125fe58019SAttilio Rao  *   notice, this list of conditions and the following disclaimer.
135fe58019SAttilio Rao  * * Redistributions in binary form must reproduce the above
145fe58019SAttilio Rao  *   copyright notice, this list of conditions and the following disclaimer
155fe58019SAttilio Rao  *   in the documentation and/or other materials provided with the
165fe58019SAttilio Rao  *   distribution.
175fe58019SAttilio Rao  * * Neither the name of Google Inc. nor the names of its
185fe58019SAttilio Rao  *   contributors may be used to endorse or promote products derived from
195fe58019SAttilio Rao  *   this software without specific prior written permission.
205fe58019SAttilio Rao  *
215fe58019SAttilio Rao  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
225fe58019SAttilio Rao  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
235fe58019SAttilio Rao  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
245fe58019SAttilio Rao  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
255fe58019SAttilio Rao  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
265fe58019SAttilio Rao  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
275fe58019SAttilio Rao  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
285fe58019SAttilio Rao  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
295fe58019SAttilio Rao  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
305fe58019SAttilio Rao  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
315fe58019SAttilio Rao  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
325fe58019SAttilio Rao  *
335fe58019SAttilio Rao  * Copyright (C) 2005 Csaba Henk.
345fe58019SAttilio Rao  * All rights reserved.
355fe58019SAttilio Rao  *
368aafc8c3SAlan Somers  * Copyright (c) 2019 The FreeBSD Foundation
378aafc8c3SAlan Somers  *
388aafc8c3SAlan Somers  * Portions of this software were developed by BFF Storage Systems, LLC under
398aafc8c3SAlan Somers  * sponsorship from the FreeBSD Foundation.
408aafc8c3SAlan Somers  *
415fe58019SAttilio Rao  * Redistribution and use in source and binary forms, with or without
425fe58019SAttilio Rao  * modification, are permitted provided that the following conditions
435fe58019SAttilio Rao  * are met:
445fe58019SAttilio Rao  * 1. Redistributions of source code must retain the above copyright
455fe58019SAttilio Rao  *    notice, this list of conditions and the following disclaimer.
465fe58019SAttilio Rao  * 2. Redistributions in binary form must reproduce the above copyright
475fe58019SAttilio Rao  *    notice, this list of conditions and the following disclaimer in the
485fe58019SAttilio Rao  *    documentation and/or other materials provided with the distribution.
495fe58019SAttilio Rao  *
505fe58019SAttilio Rao  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
515fe58019SAttilio Rao  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
525fe58019SAttilio Rao  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
535fe58019SAttilio Rao  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
545fe58019SAttilio Rao  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
555fe58019SAttilio Rao  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
565fe58019SAttilio Rao  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
575fe58019SAttilio Rao  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
585fe58019SAttilio Rao  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
595fe58019SAttilio Rao  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
605fe58019SAttilio Rao  * SUCH DAMAGE.
615fe58019SAttilio Rao  */
625fe58019SAttilio Rao 
635fe58019SAttilio Rao #include <sys/cdefs.h>
645fe58019SAttilio Rao __FBSDID("$FreeBSD$");
655fe58019SAttilio Rao 
66cf169498SAlan Somers #include <sys/param.h>
675fe58019SAttilio Rao #include <sys/module.h>
685fe58019SAttilio Rao #include <sys/systm.h>
69560a55d0SAlan Somers #include <sys/counter.h>
705fe58019SAttilio Rao #include <sys/errno.h>
715fe58019SAttilio Rao #include <sys/kernel.h>
725fe58019SAttilio Rao #include <sys/conf.h>
735fe58019SAttilio Rao #include <sys/uio.h>
745fe58019SAttilio Rao #include <sys/malloc.h>
755fe58019SAttilio Rao #include <sys/queue.h>
765fe58019SAttilio Rao #include <sys/lock.h>
775fe58019SAttilio Rao #include <sys/sx.h>
785fe58019SAttilio Rao #include <sys/mutex.h>
795fe58019SAttilio Rao #include <sys/proc.h>
805fe58019SAttilio Rao #include <sys/mount.h>
81cf169498SAlan Somers #include <sys/sdt.h>
825fe58019SAttilio Rao #include <sys/vnode.h>
835fe58019SAttilio Rao #include <sys/signalvar.h>
845fe58019SAttilio Rao #include <sys/syscallsubr.h>
855fe58019SAttilio Rao #include <sys/sysctl.h>
865fe58019SAttilio Rao #include <vm/uma.h>
875fe58019SAttilio Rao 
885fe58019SAttilio Rao #include "fuse.h"
895fe58019SAttilio Rao #include "fuse_node.h"
905fe58019SAttilio Rao #include "fuse_ipc.h"
915fe58019SAttilio Rao #include "fuse_internal.h"
925fe58019SAttilio Rao 
93419e7ff6SAlan Somers SDT_PROVIDER_DECLARE(fusefs);
94cf169498SAlan Somers /*
95cf169498SAlan Somers  * Fuse trace probe:
96cf169498SAlan Somers  * arg0: verbosity.  Higher numbers give more verbose messages
97cf169498SAlan Somers  * arg1: Textual message
98cf169498SAlan Somers  */
99419e7ff6SAlan Somers SDT_PROBE_DEFINE2(fusefs, , ipc, trace, "int", "char*");
1005fe58019SAttilio Rao 
101723c7768SAlan Somers static void fdisp_make_pid(struct fuse_dispatcher *fdip, enum fuse_opcode op,
102723c7768SAlan Somers     struct fuse_data *data, uint64_t nid, pid_t pid, struct ucred *cred);
1033d070fdcSAlan Somers static void fuse_interrupt_send(struct fuse_ticket *otick, int err);
1045fe58019SAttilio Rao static struct fuse_ticket *fticket_alloc(struct fuse_data *data);
1055fe58019SAttilio Rao static void fticket_refresh(struct fuse_ticket *ftick);
1065fe58019SAttilio Rao static void fticket_destroy(struct fuse_ticket *ftick);
1075fe58019SAttilio Rao static int fticket_wait_answer(struct fuse_ticket *ftick);
10802295cafSConrad Meyer static inline int
1095fe58019SAttilio Rao fticket_aw_pull_uio(struct fuse_ticket *ftick,
1105fe58019SAttilio Rao     struct uio *uio);
1115fe58019SAttilio Rao 
1125fe58019SAttilio Rao static int fuse_body_audit(struct fuse_ticket *ftick, size_t blen);
1135fe58019SAttilio Rao 
1145fe58019SAttilio Rao static fuse_handler_t fuse_standard_handler;
1155fe58019SAttilio Rao 
116560a55d0SAlan Somers static counter_u64_t fuse_ticket_count;
117560a55d0SAlan Somers SYSCTL_COUNTER_U64(_vfs_fusefs_stats, OID_AUTO, ticket_count, CTLFLAG_RD,
118560a55d0SAlan Somers     &fuse_ticket_count, "Number of allocated tickets");
1195fe58019SAttilio Rao 
1205fe58019SAttilio Rao static long fuse_iov_permanent_bufsize = 1 << 19;
1215fe58019SAttilio Rao 
122123af6ecSAlan Somers SYSCTL_LONG(_vfs_fusefs, OID_AUTO, iov_permanent_bufsize, CTLFLAG_RW,
1235fe58019SAttilio Rao     &fuse_iov_permanent_bufsize, 0,
1245fe58019SAttilio Rao     "limit for permanently stored buffer size for fuse_iovs");
1255fe58019SAttilio Rao static int fuse_iov_credit = 16;
1265fe58019SAttilio Rao 
127123af6ecSAlan Somers SYSCTL_INT(_vfs_fusefs, OID_AUTO, iov_credit, CTLFLAG_RW,
1285fe58019SAttilio Rao     &fuse_iov_credit, 0,
1295fe58019SAttilio Rao     "how many times is an oversized fuse_iov tolerated");
1305fe58019SAttilio Rao 
1315fe58019SAttilio Rao MALLOC_DEFINE(M_FUSEMSG, "fuse_msgbuf", "fuse message buffer");
1325fe58019SAttilio Rao static uma_zone_t ticket_zone;
1335fe58019SAttilio Rao 
134723c7768SAlan Somers /*
135723c7768SAlan Somers  * TODO: figure out how to timeout INTERRUPT requests, because the daemon may
136723c7768SAlan Somers  * leagally never respond
137723c7768SAlan Somers  */
138723c7768SAlan Somers static int
139723c7768SAlan Somers fuse_interrupt_callback(struct fuse_ticket *tick, struct uio *uio)
1405fe58019SAttilio Rao {
141a1542146SAlan Somers 	struct fuse_ticket *otick, *x_tick;
142a1542146SAlan Somers 	struct fuse_interrupt_in *fii;
143102c7ac0SAlan Somers 	struct fuse_data *data = tick->tk_data;
144a1542146SAlan Somers 	bool found = false;
145a1542146SAlan Somers 
146a1542146SAlan Somers 	fii = (struct fuse_interrupt_in*)((char*)tick->tk_ms_fiov.base +
147a1542146SAlan Somers 		sizeof(struct fuse_in_header));
148a1542146SAlan Somers 
149a1542146SAlan Somers 	fuse_lck_mtx_lock(data->aw_mtx);
150a1542146SAlan Somers 	TAILQ_FOREACH_SAFE(otick, &data->aw_head, tk_aw_link, x_tick) {
151a1542146SAlan Somers 		if (otick->tk_unique == fii->unique) {
152a1542146SAlan Somers 			found = true;
153a1542146SAlan Somers 			break;
154a1542146SAlan Somers 		}
155a1542146SAlan Somers 	}
156a1542146SAlan Somers 	fuse_lck_mtx_unlock(data->aw_mtx);
157a1542146SAlan Somers 
158a1542146SAlan Somers 	if (!found) {
159a1542146SAlan Somers 		/* Original is already complete.  Just return */
160a1542146SAlan Somers 		return 0;
161a1542146SAlan Somers 	}
162a1542146SAlan Somers 
163a1542146SAlan Somers 	/* Clear the original ticket's interrupt association */
164a1542146SAlan Somers 	otick->irq_unique = 0;
165a1542146SAlan Somers 
166102c7ac0SAlan Somers 	if (tick->tk_aw_ohead.error == ENOSYS) {
167102c7ac0SAlan Somers 		fsess_set_notimpl(data->mp, FUSE_INTERRUPT);
168102c7ac0SAlan Somers 		return 0;
169102c7ac0SAlan Somers 	} else if (tick->tk_aw_ohead.error == EAGAIN) {
170723c7768SAlan Somers 		/*
171723c7768SAlan Somers 		 * There are two reasons we might get this:
172723c7768SAlan Somers 		 * 1) the daemon received the INTERRUPT request before the
173723c7768SAlan Somers 		 *    original, or
174723c7768SAlan Somers 		 * 2) the daemon received the INTERRUPT request after it
175723c7768SAlan Somers 		 *    completed the original request.
176723c7768SAlan Somers 		 * In the first case we should re-send the INTERRUPT.  In the
177723c7768SAlan Somers 		 * second, we should ignore it.
178723c7768SAlan Somers 		 */
179723c7768SAlan Somers 		/* Resend */
1803d070fdcSAlan Somers 		fuse_interrupt_send(otick, EINTR);
181723c7768SAlan Somers 		return 0;
182723c7768SAlan Somers 	} else {
183723c7768SAlan Somers 		/* Illegal FUSE_INTERRUPT response */
184723c7768SAlan Somers 		return EINVAL;
185723c7768SAlan Somers 	}
1865fe58019SAttilio Rao }
1875fe58019SAttilio Rao 
1883d070fdcSAlan Somers /* Interrupt the operation otick.  Return err as its error code */
189723c7768SAlan Somers void
1903d070fdcSAlan Somers fuse_interrupt_send(struct fuse_ticket *otick, int err)
1915fe58019SAttilio Rao {
192723c7768SAlan Somers 	struct fuse_dispatcher fdi;
193723c7768SAlan Somers 	struct fuse_interrupt_in *fii;
194723c7768SAlan Somers 	struct fuse_in_header *ftick_hdr;
195723c7768SAlan Somers 	struct fuse_data *data = otick->tk_data;
1963d070fdcSAlan Somers 	struct fuse_ticket *tick, *xtick;
197723c7768SAlan Somers 	struct ucred reused_creds;
19861b0a927SAlan Somers 	gid_t reused_groups[1];
1995fe58019SAttilio Rao 
200a1542146SAlan Somers 	if (otick->irq_unique == 0) {
2013d070fdcSAlan Somers 		/*
2023d070fdcSAlan Somers 		 * If the daemon hasn't yet received otick, then we can answer
2033d070fdcSAlan Somers 		 * it ourselves and return.
2043d070fdcSAlan Somers 		 */
2053d070fdcSAlan Somers 		fuse_lck_mtx_lock(data->ms_mtx);
2063d070fdcSAlan Somers 		STAILQ_FOREACH_SAFE(tick, &otick->tk_data->ms_head, tk_ms_link,
2073d070fdcSAlan Somers 			xtick) {
2083d070fdcSAlan Somers 			if (tick == otick) {
2093d070fdcSAlan Somers 				STAILQ_REMOVE(&otick->tk_data->ms_head, tick,
2103d070fdcSAlan Somers 					fuse_ticket, tk_ms_link);
2110a7c63e0SAlan Somers 				otick->tk_data->ms_count--;
2123d070fdcSAlan Somers 				otick->tk_ms_link.stqe_next = NULL;
2133d070fdcSAlan Somers 				fuse_lck_mtx_unlock(data->ms_mtx);
2143d070fdcSAlan Somers 
2153d070fdcSAlan Somers 				fuse_lck_mtx_lock(otick->tk_aw_mtx);
2163d070fdcSAlan Somers 				if (!fticket_answered(otick)) {
2173d070fdcSAlan Somers 					fticket_set_answered(otick);
2183d070fdcSAlan Somers 					otick->tk_aw_errno = err;
2193d070fdcSAlan Somers 					wakeup(otick);
2203d070fdcSAlan Somers 				}
2213d070fdcSAlan Somers 				fuse_lck_mtx_unlock(otick->tk_aw_mtx);
2223d070fdcSAlan Somers 
2233d070fdcSAlan Somers 				fuse_ticket_drop(tick);
2243d070fdcSAlan Somers 				return;
2253d070fdcSAlan Somers 			}
2263d070fdcSAlan Somers 		}
2273d070fdcSAlan Somers 		fuse_lck_mtx_unlock(data->ms_mtx);
2283d070fdcSAlan Somers 
2293d070fdcSAlan Somers 		/*
230102c7ac0SAlan Somers 		 * If the fuse daemon doesn't support interrupts, then there's
231102c7ac0SAlan Somers 		 * nothing more that we can do
232102c7ac0SAlan Somers 		 */
233102c7ac0SAlan Somers 		if (!fsess_isimpl(data->mp, FUSE_INTERRUPT))
234102c7ac0SAlan Somers 			return;
235102c7ac0SAlan Somers 
236102c7ac0SAlan Somers 		/*
2373d070fdcSAlan Somers 		 * If the fuse daemon has already received otick, then we must
2383d070fdcSAlan Somers 		 * send FUSE_INTERRUPT.
2393d070fdcSAlan Somers 		 */
240723c7768SAlan Somers 		ftick_hdr = fticket_in_header(otick);
241723c7768SAlan Somers 		reused_creds.cr_uid = ftick_hdr->uid;
24261b0a927SAlan Somers 		reused_groups[0] = ftick_hdr->gid;
24361b0a927SAlan Somers 		reused_creds.cr_groups = reused_groups;
244723c7768SAlan Somers 		fdisp_init(&fdi, sizeof(*fii));
245723c7768SAlan Somers 		fdisp_make_pid(&fdi, FUSE_INTERRUPT, data, ftick_hdr->nodeid,
246723c7768SAlan Somers 			ftick_hdr->pid, &reused_creds);
247723c7768SAlan Somers 
248723c7768SAlan Somers 		fii = fdi.indata;
249723c7768SAlan Somers 		fii->unique = otick->tk_unique;
250723c7768SAlan Somers 		fuse_insert_callback(fdi.tick, fuse_interrupt_callback);
251723c7768SAlan Somers 
252a1542146SAlan Somers 		otick->irq_unique = fdi.tick->tk_unique;
253268c28edSAlan Somers 		/* Interrupt ops should be delivered ASAP */
254268c28edSAlan Somers 		fuse_insert_message(fdi.tick, true);
255723c7768SAlan Somers 		fdisp_destroy(&fdi);
256a1542146SAlan Somers 	} else {
257a1542146SAlan Somers 		/* This ticket has already been interrupted */
258a1542146SAlan Somers 	}
2595fe58019SAttilio Rao }
2605fe58019SAttilio Rao 
2615fe58019SAttilio Rao void
2625fe58019SAttilio Rao fiov_init(struct fuse_iov *fiov, size_t size)
2635fe58019SAttilio Rao {
2645fe58019SAttilio Rao 	uint32_t msize = FU_AT_LEAST(size);
2655fe58019SAttilio Rao 
2665fe58019SAttilio Rao 	fiov->len = 0;
2675fe58019SAttilio Rao 
2685fe58019SAttilio Rao 	fiov->base = malloc(msize, M_FUSEMSG, M_WAITOK | M_ZERO);
2695fe58019SAttilio Rao 
2705fe58019SAttilio Rao 	fiov->allocated_size = msize;
2715fe58019SAttilio Rao 	fiov->credit = fuse_iov_credit;
2725fe58019SAttilio Rao }
2735fe58019SAttilio Rao 
2745fe58019SAttilio Rao void
2755fe58019SAttilio Rao fiov_teardown(struct fuse_iov *fiov)
2765fe58019SAttilio Rao {
2775fe58019SAttilio Rao 	MPASS(fiov->base != NULL);
2785fe58019SAttilio Rao 	free(fiov->base, M_FUSEMSG);
2795fe58019SAttilio Rao }
2805fe58019SAttilio Rao 
2815fe58019SAttilio Rao void
2825fe58019SAttilio Rao fiov_adjust(struct fuse_iov *fiov, size_t size)
2835fe58019SAttilio Rao {
2845fe58019SAttilio Rao 	if (fiov->allocated_size < size ||
2855fe58019SAttilio Rao 	    (fuse_iov_permanent_bufsize >= 0 &&
2865fe58019SAttilio Rao 	    fiov->allocated_size - size > fuse_iov_permanent_bufsize &&
2875fe58019SAttilio Rao 	    --fiov->credit < 0)) {
2885fe58019SAttilio Rao 
2895fe58019SAttilio Rao 		fiov->base = realloc(fiov->base, FU_AT_LEAST(size), M_FUSEMSG,
2905fe58019SAttilio Rao 		    M_WAITOK | M_ZERO);
2915fe58019SAttilio Rao 		if (!fiov->base) {
2925fe58019SAttilio Rao 			panic("FUSE: realloc failed");
2935fe58019SAttilio Rao 		}
2945fe58019SAttilio Rao 		fiov->allocated_size = FU_AT_LEAST(size);
2955fe58019SAttilio Rao 		fiov->credit = fuse_iov_credit;
2968d013becSAlan Somers 		/* Clear data buffer after reallocation */
2978d013becSAlan Somers 		bzero(fiov->base, size);
2988d013becSAlan Somers 	} else if (size > fiov->len) {
2998d013becSAlan Somers 		/* Clear newly extended portion of data buffer */
3008d013becSAlan Somers 		bzero((char*)fiov->base + fiov->len, size - fiov->len);
3015fe58019SAttilio Rao 	}
3025fe58019SAttilio Rao 	fiov->len = size;
3035fe58019SAttilio Rao }
3045fe58019SAttilio Rao 
30512292a99SAlan Somers /* Resize the fiov if needed, and clear it's buffer */
3065fe58019SAttilio Rao void
3075fe58019SAttilio Rao fiov_refresh(struct fuse_iov *fiov)
3085fe58019SAttilio Rao {
3095fe58019SAttilio Rao 	fiov_adjust(fiov, 0);
3105fe58019SAttilio Rao }
3115fe58019SAttilio Rao 
3125fe58019SAttilio Rao static int
3135fe58019SAttilio Rao fticket_ctor(void *mem, int size, void *arg, int flags)
3145fe58019SAttilio Rao {
3155fe58019SAttilio Rao 	struct fuse_ticket *ftick = mem;
3165fe58019SAttilio Rao 	struct fuse_data *data = arg;
3175fe58019SAttilio Rao 
3185fe58019SAttilio Rao 	FUSE_ASSERT_MS_DONE(ftick);
3195fe58019SAttilio Rao 	FUSE_ASSERT_AW_DONE(ftick);
3205fe58019SAttilio Rao 
3215fe58019SAttilio Rao 	ftick->tk_data = data;
3225fe58019SAttilio Rao 
3235fe58019SAttilio Rao 	if (ftick->tk_unique != 0)
3245fe58019SAttilio Rao 		fticket_refresh(ftick);
3255fe58019SAttilio Rao 
3265fe58019SAttilio Rao 	/* May be truncated to 32 bits */
3275fe58019SAttilio Rao 	ftick->tk_unique = atomic_fetchadd_long(&data->ticketer, 1);
3285fe58019SAttilio Rao 	if (ftick->tk_unique == 0)
3295fe58019SAttilio Rao 		ftick->tk_unique = atomic_fetchadd_long(&data->ticketer, 1);
3305fe58019SAttilio Rao 
331a1542146SAlan Somers 	ftick->irq_unique = 0;
332a1542146SAlan Somers 
3335fe58019SAttilio Rao 	refcount_init(&ftick->tk_refcount, 1);
334560a55d0SAlan Somers 	counter_u64_add(fuse_ticket_count, 1);
3355fe58019SAttilio Rao 
3365fe58019SAttilio Rao 	return 0;
3375fe58019SAttilio Rao }
3385fe58019SAttilio Rao 
3395fe58019SAttilio Rao static void
3405fe58019SAttilio Rao fticket_dtor(void *mem, int size, void *arg)
3415fe58019SAttilio Rao {
342f220ef0bSAlan Somers #ifdef INVARIANTS
3435fe58019SAttilio Rao 	struct fuse_ticket *ftick = mem;
344f220ef0bSAlan Somers #endif
3455fe58019SAttilio Rao 
3465fe58019SAttilio Rao 	FUSE_ASSERT_MS_DONE(ftick);
3475fe58019SAttilio Rao 	FUSE_ASSERT_AW_DONE(ftick);
3485fe58019SAttilio Rao 
349560a55d0SAlan Somers 	counter_u64_add(fuse_ticket_count, -1);
3505fe58019SAttilio Rao }
3515fe58019SAttilio Rao 
3525fe58019SAttilio Rao static int
3535fe58019SAttilio Rao fticket_init(void *mem, int size, int flags)
3545fe58019SAttilio Rao {
3555fe58019SAttilio Rao 	struct fuse_ticket *ftick = mem;
3565fe58019SAttilio Rao 
3575fe58019SAttilio Rao 	bzero(ftick, sizeof(struct fuse_ticket));
3585fe58019SAttilio Rao 
3595fe58019SAttilio Rao 	fiov_init(&ftick->tk_ms_fiov, sizeof(struct fuse_in_header));
3605fe58019SAttilio Rao 	ftick->tk_ms_type = FT_M_FIOV;
3615fe58019SAttilio Rao 
3625fe58019SAttilio Rao 	mtx_init(&ftick->tk_aw_mtx, "fuse answer delivery mutex", NULL, MTX_DEF);
3635fe58019SAttilio Rao 	fiov_init(&ftick->tk_aw_fiov, 0);
3645fe58019SAttilio Rao 	ftick->tk_aw_type = FT_A_FIOV;
3655fe58019SAttilio Rao 
3665fe58019SAttilio Rao 	return 0;
3675fe58019SAttilio Rao }
3685fe58019SAttilio Rao 
3695fe58019SAttilio Rao static void
3705fe58019SAttilio Rao fticket_fini(void *mem, int size)
3715fe58019SAttilio Rao {
3725fe58019SAttilio Rao 	struct fuse_ticket *ftick = mem;
3735fe58019SAttilio Rao 
3745fe58019SAttilio Rao 	fiov_teardown(&ftick->tk_ms_fiov);
3755fe58019SAttilio Rao 	fiov_teardown(&ftick->tk_aw_fiov);
3765fe58019SAttilio Rao 	mtx_destroy(&ftick->tk_aw_mtx);
3775fe58019SAttilio Rao }
3785fe58019SAttilio Rao 
37902295cafSConrad Meyer static inline struct fuse_ticket *
3805fe58019SAttilio Rao fticket_alloc(struct fuse_data *data)
3815fe58019SAttilio Rao {
3825fe58019SAttilio Rao 	return uma_zalloc_arg(ticket_zone, data, M_WAITOK);
3835fe58019SAttilio Rao }
3845fe58019SAttilio Rao 
38502295cafSConrad Meyer static inline void
3865fe58019SAttilio Rao fticket_destroy(struct fuse_ticket *ftick)
3875fe58019SAttilio Rao {
3885fe58019SAttilio Rao 	return uma_zfree(ticket_zone, ftick);
3895fe58019SAttilio Rao }
3905fe58019SAttilio Rao 
39102295cafSConrad Meyer static inline
3925fe58019SAttilio Rao void
3935fe58019SAttilio Rao fticket_refresh(struct fuse_ticket *ftick)
3945fe58019SAttilio Rao {
3955fe58019SAttilio Rao 	FUSE_ASSERT_MS_DONE(ftick);
3965fe58019SAttilio Rao 	FUSE_ASSERT_AW_DONE(ftick);
3975fe58019SAttilio Rao 
3985fe58019SAttilio Rao 	fiov_refresh(&ftick->tk_ms_fiov);
3995fe58019SAttilio Rao 	ftick->tk_ms_bufdata = NULL;
4005fe58019SAttilio Rao 	ftick->tk_ms_bufsize = 0;
4015fe58019SAttilio Rao 	ftick->tk_ms_type = FT_M_FIOV;
4025fe58019SAttilio Rao 
4035fe58019SAttilio Rao 	bzero(&ftick->tk_aw_ohead, sizeof(struct fuse_out_header));
4045fe58019SAttilio Rao 
4055fe58019SAttilio Rao 	fiov_refresh(&ftick->tk_aw_fiov);
4065fe58019SAttilio Rao 	ftick->tk_aw_errno = 0;
4075fe58019SAttilio Rao 	ftick->tk_aw_bufdata = NULL;
4085fe58019SAttilio Rao 	ftick->tk_aw_bufsize = 0;
4095fe58019SAttilio Rao 	ftick->tk_aw_type = FT_A_FIOV;
4105fe58019SAttilio Rao 
4115fe58019SAttilio Rao 	ftick->tk_flag = 0;
4125fe58019SAttilio Rao }
4135fe58019SAttilio Rao 
41412292a99SAlan Somers /* Prepar the ticket to be reused, but don't clear its data buffers */
41512292a99SAlan Somers static inline void
41612292a99SAlan Somers fticket_reset(struct fuse_ticket *ftick)
41712292a99SAlan Somers {
41812292a99SAlan Somers 	FUSE_ASSERT_MS_DONE(ftick);
41912292a99SAlan Somers 	FUSE_ASSERT_AW_DONE(ftick);
42012292a99SAlan Somers 
42112292a99SAlan Somers 	ftick->tk_ms_bufdata = NULL;
42212292a99SAlan Somers 	ftick->tk_ms_bufsize = 0;
42312292a99SAlan Somers 	ftick->tk_ms_type = FT_M_FIOV;
42412292a99SAlan Somers 
42512292a99SAlan Somers 	bzero(&ftick->tk_aw_ohead, sizeof(struct fuse_out_header));
42612292a99SAlan Somers 
42712292a99SAlan Somers 	ftick->tk_aw_errno = 0;
42812292a99SAlan Somers 	ftick->tk_aw_bufdata = NULL;
42912292a99SAlan Somers 	ftick->tk_aw_bufsize = 0;
43012292a99SAlan Somers 	ftick->tk_aw_type = FT_A_FIOV;
43112292a99SAlan Somers 
43212292a99SAlan Somers 	ftick->tk_flag = 0;
43312292a99SAlan Somers }
43412292a99SAlan Somers 
4355fe58019SAttilio Rao static int
4365fe58019SAttilio Rao fticket_wait_answer(struct fuse_ticket *ftick)
4375fe58019SAttilio Rao {
438723c7768SAlan Somers 	struct thread *td = curthread;
439723c7768SAlan Somers 	sigset_t blockedset, oldset;
440ebbfe00eSAlan Somers 	int err = 0, stops_deferred;
441*ed74f781SAlan Somers 	struct fuse_data *data = ftick->tk_data;
442f0596245SAlan Somers 	bool interrupted = false;
4433d070fdcSAlan Somers 
444*ed74f781SAlan Somers 	if (fsess_isimpl(ftick->tk_data->mp, FUSE_INTERRUPT) &&
445*ed74f781SAlan Somers 	    data->dataflags & FSESS_INTR) {
446ebbfe00eSAlan Somers 		SIGEMPTYSET(blockedset);
447d5024ba2SAlan Somers 	} else {
448f0596245SAlan Somers 		/* Block all signals except (implicitly) SIGKILL */
449d5024ba2SAlan Somers 		SIGFILLSET(blockedset);
450d5024ba2SAlan Somers 	}
451ebbfe00eSAlan Somers 	stops_deferred = sigdeferstop(SIGDEFERSTOP_SILENT);
4523d070fdcSAlan Somers 	kern_sigprocmask(td, SIG_BLOCK, NULL, &oldset, 0);
4535fe58019SAttilio Rao 
454c02ccc7eSAlan Somers 	fuse_lck_mtx_lock(ftick->tk_aw_mtx);
455c02ccc7eSAlan Somers 
456723c7768SAlan Somers retry:
4575fe58019SAttilio Rao 	if (fticket_answered(ftick)) {
4585fe58019SAttilio Rao 		goto out;
4595fe58019SAttilio Rao 	}
4605fe58019SAttilio Rao 
4615fe58019SAttilio Rao 	if (fdata_get_dead(data)) {
4625fe58019SAttilio Rao 		err = ENOTCONN;
4635fe58019SAttilio Rao 		fticket_set_answered(ftick);
4645fe58019SAttilio Rao 		goto out;
4655fe58019SAttilio Rao 	}
4663d070fdcSAlan Somers 	kern_sigprocmask(td, SIG_BLOCK, &blockedset, NULL, 0);
4675fe58019SAttilio Rao 	err = msleep(ftick, &ftick->tk_aw_mtx, PCATCH, "fu_ans",
4685fe58019SAttilio Rao 	    data->daemon_timeout * hz);
469723c7768SAlan Somers 	kern_sigprocmask(td, SIG_SETMASK, &oldset, NULL, 0);
470723c7768SAlan Somers 	if (err == EWOULDBLOCK) {
471419e7ff6SAlan Somers 		SDT_PROBE2(fusefs, , ipc, trace, 3,
472723c7768SAlan Somers 			"fticket_wait_answer: EWOULDBLOCK");
4735fe58019SAttilio Rao #ifdef XXXIP				/* die conditionally */
4745fe58019SAttilio Rao 		if (!fdata_get_dead(data)) {
4755fe58019SAttilio Rao 			fdata_set_dead(data);
4765fe58019SAttilio Rao 		}
4775fe58019SAttilio Rao #endif
4785fe58019SAttilio Rao 		err = ETIMEDOUT;
4795fe58019SAttilio Rao 		fticket_set_answered(ftick);
480723c7768SAlan Somers 	} else if ((err == EINTR || err == ERESTART)) {
481723c7768SAlan Somers 		/*
482723c7768SAlan Somers 		 * Whether we get EINTR or ERESTART depends on whether
483723c7768SAlan Somers 		 * SA_RESTART was set by sigaction(2).
484723c7768SAlan Somers 		 *
485723c7768SAlan Somers 		 * Try to interrupt the operation and wait for an EINTR response
486723c7768SAlan Somers 		 * to the original operation.  If the file system does not
487723c7768SAlan Somers 		 * support FUSE_INTERRUPT, then we'll just wait for it to
488723c7768SAlan Somers 		 * complete like normal.  If it does support FUSE_INTERRUPT,
489723c7768SAlan Somers 		 * then it will either respond EINTR to the original operation,
490723c7768SAlan Somers 		 * or EAGAIN to the interrupt.
491723c7768SAlan Somers 		 */
492d26d63a4SAlan Somers 		sigset_t tmpset;
493723c7768SAlan Somers 
494419e7ff6SAlan Somers 		SDT_PROBE2(fusefs, , ipc, trace, 4,
495723c7768SAlan Somers 			"fticket_wait_answer: interrupt");
496723c7768SAlan Somers 		fuse_lck_mtx_unlock(ftick->tk_aw_mtx);
4973d070fdcSAlan Somers 		fuse_interrupt_send(ftick, err);
498723c7768SAlan Somers 
499723c7768SAlan Somers 		PROC_LOCK(td->td_proc);
500723c7768SAlan Somers 		mtx_lock(&td->td_proc->p_sigacts->ps_mtx);
501d26d63a4SAlan Somers 		tmpset = td->td_proc->p_siglist;
502d26d63a4SAlan Somers 		SIGSETOR(tmpset, td->td_siglist);
503723c7768SAlan Somers 		mtx_unlock(&td->td_proc->p_sigacts->ps_mtx);
504723c7768SAlan Somers 		PROC_UNLOCK(td->td_proc);
5053d070fdcSAlan Somers 
5063d070fdcSAlan Somers 		fuse_lck_mtx_lock(ftick->tk_aw_mtx);
507f0596245SAlan Somers 		if (!interrupted && !SIGISMEMBER(tmpset, SIGKILL)) {
508a1542146SAlan Somers 			/*
509f0596245SAlan Somers 			 * Block all signals while we wait for an interrupt
510f0596245SAlan Somers 			 * response.  The protocol doesn't discriminate between
511f0596245SAlan Somers 			 * different signals.
512a1542146SAlan Somers 			 */
513f0596245SAlan Somers 			SIGFILLSET(blockedset);
514f0596245SAlan Somers 			interrupted = true;
515723c7768SAlan Somers 			goto retry;
516a1542146SAlan Somers 		} else {
517f0596245SAlan Somers 			/*
518f0596245SAlan Somers 			 * Return immediately for fatal signals, or if this is
519f0596245SAlan Somers 			 * the second interruption.  We should only be
520f0596245SAlan Somers 			 * interrupted twice if the thread is stopped, for
521f0596245SAlan Somers 			 * example during sigexit.
522f0596245SAlan Somers 			 */
523a1542146SAlan Somers 		}
524723c7768SAlan Somers 	} else if (err) {
525419e7ff6SAlan Somers 		SDT_PROBE2(fusefs, , ipc, trace, 6,
526723c7768SAlan Somers 			"fticket_wait_answer: other error");
527723c7768SAlan Somers 	} else {
528419e7ff6SAlan Somers 		SDT_PROBE2(fusefs, , ipc, trace, 7, "fticket_wait_answer: OK");
5295fe58019SAttilio Rao 	}
5305fe58019SAttilio Rao out:
5315fe58019SAttilio Rao 	if (!(err || fticket_answered(ftick))) {
532419e7ff6SAlan Somers 		SDT_PROBE2(fusefs, , ipc, trace, 1,
533cf169498SAlan Somers 			"FUSE: requester was woken up but still no answer");
5345fe58019SAttilio Rao 		err = ENXIO;
5355fe58019SAttilio Rao 	}
5365fe58019SAttilio Rao 	fuse_lck_mtx_unlock(ftick->tk_aw_mtx);
537ebbfe00eSAlan Somers 	sigallowstop(stops_deferred);
5385fe58019SAttilio Rao 
5395fe58019SAttilio Rao 	return err;
5405fe58019SAttilio Rao }
5415fe58019SAttilio Rao 
54202295cafSConrad Meyer static	inline
5435fe58019SAttilio Rao int
5445fe58019SAttilio Rao fticket_aw_pull_uio(struct fuse_ticket *ftick, struct uio *uio)
5455fe58019SAttilio Rao {
5465fe58019SAttilio Rao 	int err = 0;
5475fe58019SAttilio Rao 	size_t len = uio_resid(uio);
5485fe58019SAttilio Rao 
5495fe58019SAttilio Rao 	if (len) {
5505fe58019SAttilio Rao 		switch (ftick->tk_aw_type) {
5515fe58019SAttilio Rao 		case FT_A_FIOV:
5525fe58019SAttilio Rao 			fiov_adjust(fticket_resp(ftick), len);
5535fe58019SAttilio Rao 			err = uiomove(fticket_resp(ftick)->base, len, uio);
5545fe58019SAttilio Rao 			break;
5555fe58019SAttilio Rao 
5565fe58019SAttilio Rao 		case FT_A_BUF:
5575fe58019SAttilio Rao 			ftick->tk_aw_bufsize = len;
5585fe58019SAttilio Rao 			err = uiomove(ftick->tk_aw_bufdata, len, uio);
5595fe58019SAttilio Rao 			break;
5605fe58019SAttilio Rao 
5615fe58019SAttilio Rao 		default:
5625fe58019SAttilio Rao 			panic("FUSE: unknown answer type for ticket %p", ftick);
5635fe58019SAttilio Rao 		}
5645fe58019SAttilio Rao 	}
5655fe58019SAttilio Rao 	return err;
5665fe58019SAttilio Rao }
5675fe58019SAttilio Rao 
5685fe58019SAttilio Rao int
5695fe58019SAttilio Rao fticket_pull(struct fuse_ticket *ftick, struct uio *uio)
5705fe58019SAttilio Rao {
5715fe58019SAttilio Rao 	int err = 0;
5725fe58019SAttilio Rao 
5735fe58019SAttilio Rao 	if (ftick->tk_aw_ohead.error) {
5745fe58019SAttilio Rao 		return 0;
5755fe58019SAttilio Rao 	}
5765fe58019SAttilio Rao 	err = fuse_body_audit(ftick, uio_resid(uio));
5775fe58019SAttilio Rao 	if (!err) {
5785fe58019SAttilio Rao 		err = fticket_aw_pull_uio(ftick, uio);
5795fe58019SAttilio Rao 	}
5805fe58019SAttilio Rao 	return err;
5815fe58019SAttilio Rao }
5825fe58019SAttilio Rao 
5835fe58019SAttilio Rao struct fuse_data *
5845fe58019SAttilio Rao fdata_alloc(struct cdev *fdev, struct ucred *cred)
5855fe58019SAttilio Rao {
5865fe58019SAttilio Rao 	struct fuse_data *data;
5875fe58019SAttilio Rao 
5885fe58019SAttilio Rao 	data = malloc(sizeof(struct fuse_data), M_FUSEMSG, M_WAITOK | M_ZERO);
5895fe58019SAttilio Rao 
5905fe58019SAttilio Rao 	data->fdev = fdev;
5915fe58019SAttilio Rao 	mtx_init(&data->ms_mtx, "fuse message list mutex", NULL, MTX_DEF);
5925fe58019SAttilio Rao 	STAILQ_INIT(&data->ms_head);
5930a7c63e0SAlan Somers 	data->ms_count = 0;
5943429092cSAlan Somers 	knlist_init_mtx(&data->ks_rsel.si_note, &data->ms_mtx);
5955fe58019SAttilio Rao 	mtx_init(&data->aw_mtx, "fuse answer list mutex", NULL, MTX_DEF);
5965fe58019SAttilio Rao 	TAILQ_INIT(&data->aw_head);
5975fe58019SAttilio Rao 	data->daemoncred = crhold(cred);
5985fe58019SAttilio Rao 	data->daemon_timeout = FUSE_DEFAULT_DAEMON_TIMEOUT;
5995fe58019SAttilio Rao 	sx_init(&data->rename_lock, "fuse rename lock");
6005fe58019SAttilio Rao 	data->ref = 1;
6015fe58019SAttilio Rao 
6025fe58019SAttilio Rao 	return data;
6035fe58019SAttilio Rao }
6045fe58019SAttilio Rao 
6055fe58019SAttilio Rao void
6065fe58019SAttilio Rao fdata_trydestroy(struct fuse_data *data)
6075fe58019SAttilio Rao {
6085fe58019SAttilio Rao 	data->ref--;
6095fe58019SAttilio Rao 	MPASS(data->ref >= 0);
6105fe58019SAttilio Rao 	if (data->ref != 0)
6115fe58019SAttilio Rao 		return;
6125fe58019SAttilio Rao 
6135fe58019SAttilio Rao 	/* Driving off stage all that stuff thrown at device... */
6145fe58019SAttilio Rao 	sx_destroy(&data->rename_lock);
6155fe58019SAttilio Rao 	crfree(data->daemoncred);
6163429092cSAlan Somers 	mtx_destroy(&data->aw_mtx);
6173429092cSAlan Somers 	knlist_delete(&data->ks_rsel.si_note, curthread, 0);
6183429092cSAlan Somers 	knlist_destroy(&data->ks_rsel.si_note);
6193429092cSAlan Somers 	mtx_destroy(&data->ms_mtx);
6205fe58019SAttilio Rao 
6215fe58019SAttilio Rao 	free(data, M_FUSEMSG);
6225fe58019SAttilio Rao }
6235fe58019SAttilio Rao 
6245fe58019SAttilio Rao void
6255fe58019SAttilio Rao fdata_set_dead(struct fuse_data *data)
6265fe58019SAttilio Rao {
6275fe58019SAttilio Rao 	FUSE_LOCK();
6285fe58019SAttilio Rao 	if (fdata_get_dead(data)) {
6295fe58019SAttilio Rao 		FUSE_UNLOCK();
6305fe58019SAttilio Rao 		return;
6315fe58019SAttilio Rao 	}
6325fe58019SAttilio Rao 	fuse_lck_mtx_lock(data->ms_mtx);
6335fe58019SAttilio Rao 	data->dataflags |= FSESS_DEAD;
6345fe58019SAttilio Rao 	wakeup_one(data);
6355fe58019SAttilio Rao 	selwakeuppri(&data->ks_rsel, PZERO + 1);
6365fe58019SAttilio Rao 	wakeup(&data->ticketer);
6375fe58019SAttilio Rao 	fuse_lck_mtx_unlock(data->ms_mtx);
6385fe58019SAttilio Rao 	FUSE_UNLOCK();
6395fe58019SAttilio Rao }
6405fe58019SAttilio Rao 
6415fe58019SAttilio Rao struct fuse_ticket *
6425fe58019SAttilio Rao fuse_ticket_fetch(struct fuse_data *data)
6435fe58019SAttilio Rao {
6445fe58019SAttilio Rao 	int err = 0;
6455fe58019SAttilio Rao 	struct fuse_ticket *ftick;
6465fe58019SAttilio Rao 
6475fe58019SAttilio Rao 	ftick = fticket_alloc(data);
6485fe58019SAttilio Rao 
6495fe58019SAttilio Rao 	if (!(data->dataflags & FSESS_INITED)) {
6505fe58019SAttilio Rao 		/* Sleep until get answer for INIT messsage */
6515fe58019SAttilio Rao 		FUSE_LOCK();
6525fe58019SAttilio Rao 		if (!(data->dataflags & FSESS_INITED) && data->ticketer > 2) {
6535fe58019SAttilio Rao 			err = msleep(&data->ticketer, &fuse_mtx, PCATCH | PDROP,
6545fe58019SAttilio Rao 			    "fu_ini", 0);
6555fe58019SAttilio Rao 			if (err)
6565fe58019SAttilio Rao 				fdata_set_dead(data);
6575fe58019SAttilio Rao 		} else
6585fe58019SAttilio Rao 			FUSE_UNLOCK();
6595fe58019SAttilio Rao 	}
6605fe58019SAttilio Rao 	return ftick;
6615fe58019SAttilio Rao }
6625fe58019SAttilio Rao 
6635fe58019SAttilio Rao int
6645fe58019SAttilio Rao fuse_ticket_drop(struct fuse_ticket *ftick)
6655fe58019SAttilio Rao {
6665fe58019SAttilio Rao 	int die;
6675fe58019SAttilio Rao 
6685fe58019SAttilio Rao 	die = refcount_release(&ftick->tk_refcount);
6695fe58019SAttilio Rao 	if (die)
6705fe58019SAttilio Rao 		fticket_destroy(ftick);
6715fe58019SAttilio Rao 
6725fe58019SAttilio Rao 	return die;
6735fe58019SAttilio Rao }
6745fe58019SAttilio Rao 
6755fe58019SAttilio Rao void
6765fe58019SAttilio Rao fuse_insert_callback(struct fuse_ticket *ftick, fuse_handler_t * handler)
6775fe58019SAttilio Rao {
6785fe58019SAttilio Rao 	if (fdata_get_dead(ftick->tk_data)) {
6795fe58019SAttilio Rao 		return;
6805fe58019SAttilio Rao 	}
6815fe58019SAttilio Rao 	ftick->tk_aw_handler = handler;
6825fe58019SAttilio Rao 
6835fe58019SAttilio Rao 	fuse_lck_mtx_lock(ftick->tk_data->aw_mtx);
6845fe58019SAttilio Rao 	fuse_aw_push(ftick);
6855fe58019SAttilio Rao 	fuse_lck_mtx_unlock(ftick->tk_data->aw_mtx);
6865fe58019SAttilio Rao }
6875fe58019SAttilio Rao 
688268c28edSAlan Somers /*
689268c28edSAlan Somers  * Insert a new upgoing ticket into the message queue
690268c28edSAlan Somers  *
691268c28edSAlan Somers  * If urgent is true, insert at the front of the queue.  Otherwise, insert in
692268c28edSAlan Somers  * FIFO order.
693268c28edSAlan Somers  */
6945fe58019SAttilio Rao void
695268c28edSAlan Somers fuse_insert_message(struct fuse_ticket *ftick, bool urgent)
6965fe58019SAttilio Rao {
6975fe58019SAttilio Rao 	if (ftick->tk_flag & FT_DIRTY) {
6985fe58019SAttilio Rao 		panic("FUSE: ticket reused without being refreshed");
6995fe58019SAttilio Rao 	}
7005fe58019SAttilio Rao 	ftick->tk_flag |= FT_DIRTY;
7015fe58019SAttilio Rao 
7025fe58019SAttilio Rao 	if (fdata_get_dead(ftick->tk_data)) {
7035fe58019SAttilio Rao 		return;
7045fe58019SAttilio Rao 	}
7055fe58019SAttilio Rao 	fuse_lck_mtx_lock(ftick->tk_data->ms_mtx);
706268c28edSAlan Somers 	if (urgent)
707268c28edSAlan Somers 		fuse_ms_push_head(ftick);
708268c28edSAlan Somers 	else
7095fe58019SAttilio Rao 		fuse_ms_push(ftick);
7105fe58019SAttilio Rao 	wakeup_one(ftick->tk_data);
7115fe58019SAttilio Rao 	selwakeuppri(&ftick->tk_data->ks_rsel, PZERO + 1);
7123429092cSAlan Somers 	KNOTE_LOCKED(&ftick->tk_data->ks_rsel.si_note, 0);
7135fe58019SAttilio Rao 	fuse_lck_mtx_unlock(ftick->tk_data->ms_mtx);
7145fe58019SAttilio Rao }
7155fe58019SAttilio Rao 
7165fe58019SAttilio Rao static int
7175fe58019SAttilio Rao fuse_body_audit(struct fuse_ticket *ftick, size_t blen)
7185fe58019SAttilio Rao {
7195fe58019SAttilio Rao 	int err = 0;
7205fe58019SAttilio Rao 	enum fuse_opcode opcode;
7215fe58019SAttilio Rao 
7225fe58019SAttilio Rao 	opcode = fticket_opcode(ftick);
7235fe58019SAttilio Rao 
7245fe58019SAttilio Rao 	switch (opcode) {
725a1c9f4adSAlan Somers 	case FUSE_BMAP:
726a1c9f4adSAlan Somers 		err = (blen == sizeof(struct fuse_bmap_out)) ? 0 : EINVAL;
727a1c9f4adSAlan Somers 		break;
728a1c9f4adSAlan Somers 
72916bd2d47SAlan Somers 	case FUSE_LINK:
7305fe58019SAttilio Rao 	case FUSE_LOOKUP:
73116bd2d47SAlan Somers 	case FUSE_MKDIR:
73216bd2d47SAlan Somers 	case FUSE_MKNOD:
73316bd2d47SAlan Somers 	case FUSE_SYMLINK:
73416bd2d47SAlan Somers 		if (fuse_libabi_geq(ftick->tk_data, 7, 9)) {
73516bd2d47SAlan Somers 			err = (blen == sizeof(struct fuse_entry_out)) ?
73616bd2d47SAlan Somers 				0 : EINVAL;
73716bd2d47SAlan Somers 		} else {
73816bd2d47SAlan Somers 			err = (blen == FUSE_COMPAT_ENTRY_OUT_SIZE) ? 0 : EINVAL;
73916bd2d47SAlan Somers 		}
7405fe58019SAttilio Rao 		break;
7415fe58019SAttilio Rao 
7425fe58019SAttilio Rao 	case FUSE_FORGET:
7435fe58019SAttilio Rao 		panic("FUSE: a handler has been intalled for FUSE_FORGET");
7445fe58019SAttilio Rao 		break;
7455fe58019SAttilio Rao 
7465fe58019SAttilio Rao 	case FUSE_GETATTR:
7475fe58019SAttilio Rao 	case FUSE_SETATTR:
74816bd2d47SAlan Somers 		if (fuse_libabi_geq(ftick->tk_data, 7, 9)) {
74916bd2d47SAlan Somers 			err = (blen == sizeof(struct fuse_attr_out)) ?
75016bd2d47SAlan Somers 			  0 : EINVAL;
75116bd2d47SAlan Somers 		} else {
75216bd2d47SAlan Somers 			err = (blen == FUSE_COMPAT_ATTR_OUT_SIZE) ? 0 : EINVAL;
75316bd2d47SAlan Somers 		}
7545fe58019SAttilio Rao 		break;
7555fe58019SAttilio Rao 
7565fe58019SAttilio Rao 	case FUSE_READLINK:
7575fe58019SAttilio Rao 		err = (PAGE_SIZE >= blen) ? 0 : EINVAL;
7585fe58019SAttilio Rao 		break;
7595fe58019SAttilio Rao 
7605fe58019SAttilio Rao 	case FUSE_UNLINK:
7615fe58019SAttilio Rao 		err = (blen == 0) ? 0 : EINVAL;
7625fe58019SAttilio Rao 		break;
7635fe58019SAttilio Rao 
7645fe58019SAttilio Rao 	case FUSE_RMDIR:
7655fe58019SAttilio Rao 		err = (blen == 0) ? 0 : EINVAL;
7665fe58019SAttilio Rao 		break;
7675fe58019SAttilio Rao 
7685fe58019SAttilio Rao 	case FUSE_RENAME:
7695fe58019SAttilio Rao 		err = (blen == 0) ? 0 : EINVAL;
7705fe58019SAttilio Rao 		break;
7715fe58019SAttilio Rao 
7725fe58019SAttilio Rao 	case FUSE_OPEN:
7735fe58019SAttilio Rao 		err = (blen == sizeof(struct fuse_open_out)) ? 0 : EINVAL;
7745fe58019SAttilio Rao 		break;
7755fe58019SAttilio Rao 
7765fe58019SAttilio Rao 	case FUSE_READ:
7775fe58019SAttilio Rao 		err = (((struct fuse_read_in *)(
7785fe58019SAttilio Rao 		    (char *)ftick->tk_ms_fiov.base +
7795fe58019SAttilio Rao 		    sizeof(struct fuse_in_header)
7805fe58019SAttilio Rao 		    ))->size >= blen) ? 0 : EINVAL;
7815fe58019SAttilio Rao 		break;
7825fe58019SAttilio Rao 
7835fe58019SAttilio Rao 	case FUSE_WRITE:
7845fe58019SAttilio Rao 		err = (blen == sizeof(struct fuse_write_out)) ? 0 : EINVAL;
7855fe58019SAttilio Rao 		break;
7865fe58019SAttilio Rao 
7875fe58019SAttilio Rao 	case FUSE_STATFS:
7885fe58019SAttilio Rao 		if (fuse_libabi_geq(ftick->tk_data, 7, 4)) {
7895fe58019SAttilio Rao 			err = (blen == sizeof(struct fuse_statfs_out)) ?
7905fe58019SAttilio Rao 			  0 : EINVAL;
7915fe58019SAttilio Rao 		} else {
7925fe58019SAttilio Rao 			err = (blen == FUSE_COMPAT_STATFS_SIZE) ? 0 : EINVAL;
7935fe58019SAttilio Rao 		}
7945fe58019SAttilio Rao 		break;
7955fe58019SAttilio Rao 
7965fe58019SAttilio Rao 	case FUSE_RELEASE:
7975fe58019SAttilio Rao 		err = (blen == 0) ? 0 : EINVAL;
7985fe58019SAttilio Rao 		break;
7995fe58019SAttilio Rao 
8005fe58019SAttilio Rao 	case FUSE_FSYNC:
8015fe58019SAttilio Rao 		err = (blen == 0) ? 0 : EINVAL;
8025fe58019SAttilio Rao 		break;
8035fe58019SAttilio Rao 
8045fe58019SAttilio Rao 	case FUSE_SETXATTR:
80504660064SFedor Uporov 		err = (blen == 0) ? 0 : EINVAL;
8065fe58019SAttilio Rao 		break;
8075fe58019SAttilio Rao 
8085fe58019SAttilio Rao 	case FUSE_GETXATTR:
8095fe58019SAttilio Rao 	case FUSE_LISTXATTR:
81004660064SFedor Uporov 		/*
81104660064SFedor Uporov 		 * These can have varying response lengths, and 0 length
81204660064SFedor Uporov 		 * isn't necessarily invalid.
81304660064SFedor Uporov 		 */
81404660064SFedor Uporov 		err = 0;
8155fe58019SAttilio Rao 		break;
8165fe58019SAttilio Rao 
8175fe58019SAttilio Rao 	case FUSE_REMOVEXATTR:
81804660064SFedor Uporov 		err = (blen == 0) ? 0 : EINVAL;
8195fe58019SAttilio Rao 		break;
8205fe58019SAttilio Rao 
8215fe58019SAttilio Rao 	case FUSE_FLUSH:
8225fe58019SAttilio Rao 		err = (blen == 0) ? 0 : EINVAL;
8235fe58019SAttilio Rao 		break;
8245fe58019SAttilio Rao 
8255fe58019SAttilio Rao 	case FUSE_INIT:
82687ff949aSAlan Somers 		if (blen == sizeof(struct fuse_init_out) ||
82787ff949aSAlan Somers 		    blen == FUSE_COMPAT_INIT_OUT_SIZE ||
82887ff949aSAlan Somers 		    blen == FUSE_COMPAT_22_INIT_OUT_SIZE) {
8295fe58019SAttilio Rao 			err = 0;
8305fe58019SAttilio Rao 		} else {
8315fe58019SAttilio Rao 			err = EINVAL;
8325fe58019SAttilio Rao 		}
8335fe58019SAttilio Rao 		break;
8345fe58019SAttilio Rao 
8355fe58019SAttilio Rao 	case FUSE_OPENDIR:
8365fe58019SAttilio Rao 		err = (blen == sizeof(struct fuse_open_out)) ? 0 : EINVAL;
8375fe58019SAttilio Rao 		break;
8385fe58019SAttilio Rao 
8395fe58019SAttilio Rao 	case FUSE_READDIR:
8405fe58019SAttilio Rao 		err = (((struct fuse_read_in *)(
8415fe58019SAttilio Rao 		    (char *)ftick->tk_ms_fiov.base +
8425fe58019SAttilio Rao 		    sizeof(struct fuse_in_header)
8435fe58019SAttilio Rao 		    ))->size >= blen) ? 0 : EINVAL;
8445fe58019SAttilio Rao 		break;
8455fe58019SAttilio Rao 
8465fe58019SAttilio Rao 	case FUSE_RELEASEDIR:
8475fe58019SAttilio Rao 		err = (blen == 0) ? 0 : EINVAL;
8485fe58019SAttilio Rao 		break;
8495fe58019SAttilio Rao 
8505fe58019SAttilio Rao 	case FUSE_FSYNCDIR:
8515fe58019SAttilio Rao 		err = (blen == 0) ? 0 : EINVAL;
8525fe58019SAttilio Rao 		break;
8535fe58019SAttilio Rao 
8545fe58019SAttilio Rao 	case FUSE_GETLK:
855f067b609SAlan Somers 		err = (blen == sizeof(struct fuse_lk_out)) ? 0 : EINVAL;
8565fe58019SAttilio Rao 		break;
8575fe58019SAttilio Rao 
8585fe58019SAttilio Rao 	case FUSE_SETLK:
859f067b609SAlan Somers 		err = (blen == 0) ? 0 : EINVAL;
8605fe58019SAttilio Rao 		break;
8615fe58019SAttilio Rao 
8625fe58019SAttilio Rao 	case FUSE_SETLKW:
863f067b609SAlan Somers 		err = (blen == 0) ? 0 : EINVAL;
8645fe58019SAttilio Rao 		break;
8655fe58019SAttilio Rao 
8665fe58019SAttilio Rao 	case FUSE_ACCESS:
8675fe58019SAttilio Rao 		err = (blen == 0) ? 0 : EINVAL;
8685fe58019SAttilio Rao 		break;
8695fe58019SAttilio Rao 
8705fe58019SAttilio Rao 	case FUSE_CREATE:
87116bd2d47SAlan Somers 		if (fuse_libabi_geq(ftick->tk_data, 7, 9)) {
8725fe58019SAttilio Rao 			err = (blen == sizeof(struct fuse_entry_out) +
8735fe58019SAttilio Rao 			    sizeof(struct fuse_open_out)) ? 0 : EINVAL;
87416bd2d47SAlan Somers 		} else {
87516bd2d47SAlan Somers 			err = (blen == FUSE_COMPAT_ENTRY_OUT_SIZE +
87616bd2d47SAlan Somers 			    sizeof(struct fuse_open_out)) ? 0 : EINVAL;
87716bd2d47SAlan Somers 		}
8785fe58019SAttilio Rao 		break;
8795fe58019SAttilio Rao 
8805fe58019SAttilio Rao 	case FUSE_DESTROY:
8815fe58019SAttilio Rao 		err = (blen == 0) ? 0 : EINVAL;
8825fe58019SAttilio Rao 		break;
8835fe58019SAttilio Rao 
8845fe58019SAttilio Rao 	default:
8855fe58019SAttilio Rao 		panic("FUSE: opcodes out of sync (%d)\n", opcode);
8865fe58019SAttilio Rao 	}
8875fe58019SAttilio Rao 
8885fe58019SAttilio Rao 	return err;
8895fe58019SAttilio Rao }
8905fe58019SAttilio Rao 
89102295cafSConrad Meyer static inline void
89202295cafSConrad Meyer fuse_setup_ihead(struct fuse_in_header *ihead, struct fuse_ticket *ftick,
89302295cafSConrad Meyer     uint64_t nid, enum fuse_opcode op, size_t blen, pid_t pid,
8945fe58019SAttilio Rao     struct ucred *cred)
8955fe58019SAttilio Rao {
8965fe58019SAttilio Rao 	ihead->len = sizeof(*ihead) + blen;
8975fe58019SAttilio Rao 	ihead->unique = ftick->tk_unique;
8985fe58019SAttilio Rao 	ihead->nodeid = nid;
8995fe58019SAttilio Rao 	ihead->opcode = op;
9005fe58019SAttilio Rao 
9015fe58019SAttilio Rao 	ihead->pid = pid;
9025fe58019SAttilio Rao 	ihead->uid = cred->cr_uid;
90361b0a927SAlan Somers 	ihead->gid = cred->cr_groups[0];
9045fe58019SAttilio Rao }
9055fe58019SAttilio Rao 
9065fe58019SAttilio Rao /*
9075fe58019SAttilio Rao  * fuse_standard_handler just pulls indata and wakes up pretender.
9085fe58019SAttilio Rao  * Doesn't try to interpret data, that's left for the pretender.
9095fe58019SAttilio Rao  * Though might do a basic size verification before the pull-in takes place
9105fe58019SAttilio Rao  */
9115fe58019SAttilio Rao 
9125fe58019SAttilio Rao static int
9135fe58019SAttilio Rao fuse_standard_handler(struct fuse_ticket *ftick, struct uio *uio)
9145fe58019SAttilio Rao {
9155fe58019SAttilio Rao 	int err = 0;
9165fe58019SAttilio Rao 
9175fe58019SAttilio Rao 	err = fticket_pull(ftick, uio);
9185fe58019SAttilio Rao 
9195fe58019SAttilio Rao 	fuse_lck_mtx_lock(ftick->tk_aw_mtx);
9205fe58019SAttilio Rao 
9215fe58019SAttilio Rao 	if (!fticket_answered(ftick)) {
9225fe58019SAttilio Rao 		fticket_set_answered(ftick);
9235fe58019SAttilio Rao 		ftick->tk_aw_errno = err;
9245fe58019SAttilio Rao 		wakeup(ftick);
9255fe58019SAttilio Rao 	}
9265fe58019SAttilio Rao 	fuse_lck_mtx_unlock(ftick->tk_aw_mtx);
9275fe58019SAttilio Rao 
9285fe58019SAttilio Rao 	return err;
9295fe58019SAttilio Rao }
9305fe58019SAttilio Rao 
93112292a99SAlan Somers /*
93212292a99SAlan Somers  * Reinitialize a dispatcher from a pid and node id, without resizing or
93312292a99SAlan Somers  * clearing its data buffers
93412292a99SAlan Somers  */
93512292a99SAlan Somers static void
93612292a99SAlan Somers fdisp_refresh_pid(struct fuse_dispatcher *fdip, enum fuse_opcode op,
93712292a99SAlan Somers     struct mount *mp, uint64_t nid, pid_t pid, struct ucred *cred)
93812292a99SAlan Somers {
93912292a99SAlan Somers 	MPASS(fdip->tick);
9408d013becSAlan Somers 	MPASS2(sizeof(fdip->finh) + fdip->iosize <= fdip->tick->tk_ms_fiov.len,
9418d013becSAlan Somers 		"Must use fdisp_make_pid to increase the size of the fiov");
94212292a99SAlan Somers 	fticket_reset(fdip->tick);
94312292a99SAlan Somers 
94412292a99SAlan Somers 	FUSE_DIMALLOC(&fdip->tick->tk_ms_fiov, fdip->finh,
94512292a99SAlan Somers 	    fdip->indata, fdip->iosize);
94612292a99SAlan Somers 
94712292a99SAlan Somers 	fuse_setup_ihead(fdip->finh, fdip->tick, nid, op, fdip->iosize, pid,
94812292a99SAlan Somers 		cred);
94912292a99SAlan Somers }
95012292a99SAlan Somers 
95112292a99SAlan Somers /* Initialize a dispatcher from a pid and node id */
95212292a99SAlan Somers static void
95302295cafSConrad Meyer fdisp_make_pid(struct fuse_dispatcher *fdip, enum fuse_opcode op,
954723c7768SAlan Somers     struct fuse_data *data, uint64_t nid, pid_t pid, struct ucred *cred)
9555fe58019SAttilio Rao {
9565fe58019SAttilio Rao 	if (fdip->tick) {
9575fe58019SAttilio Rao 		fticket_refresh(fdip->tick);
9585fe58019SAttilio Rao 	} else {
9595fe58019SAttilio Rao 		fdip->tick = fuse_ticket_fetch(data);
9605fe58019SAttilio Rao 	}
9615fe58019SAttilio Rao 
9628d013becSAlan Somers 	/* FUSE_DIMALLOC will bzero the fiovs when it enlarges them */
9635fe58019SAttilio Rao 	FUSE_DIMALLOC(&fdip->tick->tk_ms_fiov, fdip->finh,
9645fe58019SAttilio Rao 	    fdip->indata, fdip->iosize);
9655fe58019SAttilio Rao 
9665fe58019SAttilio Rao 	fuse_setup_ihead(fdip->finh, fdip->tick, nid, op, fdip->iosize, pid, cred);
9675fe58019SAttilio Rao }
9685fe58019SAttilio Rao 
9695fe58019SAttilio Rao void
97002295cafSConrad Meyer fdisp_make(struct fuse_dispatcher *fdip, enum fuse_opcode op, struct mount *mp,
97102295cafSConrad Meyer     uint64_t nid, struct thread *td, struct ucred *cred)
9725fe58019SAttilio Rao {
973723c7768SAlan Somers 	struct fuse_data *data = fuse_get_mpdata(mp);
9745fe58019SAttilio Rao 	RECTIFY_TDCR(td, cred);
9755fe58019SAttilio Rao 
976723c7768SAlan Somers 	return fdisp_make_pid(fdip, op, data, nid, td->td_proc->p_pid, cred);
9775fe58019SAttilio Rao }
9785fe58019SAttilio Rao 
9795fe58019SAttilio Rao void
98002295cafSConrad Meyer fdisp_make_vp(struct fuse_dispatcher *fdip, enum fuse_opcode op,
98102295cafSConrad Meyer     struct vnode *vp, struct thread *td, struct ucred *cred)
9825fe58019SAttilio Rao {
983723c7768SAlan Somers 	struct mount *mp = vnode_mount(vp);
984723c7768SAlan Somers 	struct fuse_data *data = fuse_get_mpdata(mp);
985723c7768SAlan Somers 
9865fe58019SAttilio Rao 	RECTIFY_TDCR(td, cred);
987723c7768SAlan Somers 	return fdisp_make_pid(fdip, op, data, VTOI(vp),
9885fe58019SAttilio Rao 	    td->td_proc->p_pid, cred);
9895fe58019SAttilio Rao }
9905fe58019SAttilio Rao 
99112292a99SAlan Somers /* Refresh a fuse_dispatcher so it can be reused, but don't zero its data */
99212292a99SAlan Somers void
99312292a99SAlan Somers fdisp_refresh_vp(struct fuse_dispatcher *fdip, enum fuse_opcode op,
99412292a99SAlan Somers     struct vnode *vp, struct thread *td, struct ucred *cred)
99512292a99SAlan Somers {
99612292a99SAlan Somers 	RECTIFY_TDCR(td, cred);
99712292a99SAlan Somers 	return fdisp_refresh_pid(fdip, op, vnode_mount(vp), VTOI(vp),
99812292a99SAlan Somers 	    td->td_proc->p_pid, cred);
99912292a99SAlan Somers }
100012292a99SAlan Somers 
100112292a99SAlan Somers void
100212292a99SAlan Somers fdisp_refresh(struct fuse_dispatcher *fdip)
100312292a99SAlan Somers {
100412292a99SAlan Somers 	fticket_refresh(fdip->tick);
100512292a99SAlan Somers }
100612292a99SAlan Somers 
1007419e7ff6SAlan Somers SDT_PROBE_DEFINE2(fusefs, , ipc, fdisp_wait_answ_error, "char*", "int");
1008cf169498SAlan Somers 
10095fe58019SAttilio Rao int
10105fe58019SAttilio Rao fdisp_wait_answ(struct fuse_dispatcher *fdip)
10115fe58019SAttilio Rao {
10125fe58019SAttilio Rao 	int err = 0;
10135fe58019SAttilio Rao 
10145fe58019SAttilio Rao 	fdip->answ_stat = 0;
10155fe58019SAttilio Rao 	fuse_insert_callback(fdip->tick, fuse_standard_handler);
1016268c28edSAlan Somers 	fuse_insert_message(fdip->tick, false);
10175fe58019SAttilio Rao 
10185fe58019SAttilio Rao 	if ((err = fticket_wait_answer(fdip->tick))) {
10195fe58019SAttilio Rao 		fuse_lck_mtx_lock(fdip->tick->tk_aw_mtx);
10205fe58019SAttilio Rao 
10215fe58019SAttilio Rao 		if (fticket_answered(fdip->tick)) {
10225fe58019SAttilio Rao 			/*
10235fe58019SAttilio Rao 	                 * Just between noticing the interrupt and getting here,
10245fe58019SAttilio Rao 	                 * the standard handler has completed his job.
10255fe58019SAttilio Rao 	                 * So we drop the ticket and exit as usual.
10265fe58019SAttilio Rao 	                 */
1027419e7ff6SAlan Somers 			SDT_PROBE2(fusefs, , ipc, fdisp_wait_answ_error,
1028cf169498SAlan Somers 				"IPC: interrupted, already answered", err);
10295fe58019SAttilio Rao 			fuse_lck_mtx_unlock(fdip->tick->tk_aw_mtx);
10305fe58019SAttilio Rao 			goto out;
10315fe58019SAttilio Rao 		} else {
10325fe58019SAttilio Rao 			/*
10335fe58019SAttilio Rao 	                 * So we were faster than the standard handler.
10345fe58019SAttilio Rao 	                 * Then by setting the answered flag we get *him*
10355fe58019SAttilio Rao 	                 * to drop the ticket.
10365fe58019SAttilio Rao 	                 */
1037419e7ff6SAlan Somers 			SDT_PROBE2(fusefs, , ipc, fdisp_wait_answ_error,
1038cf169498SAlan Somers 				"IPC: interrupted, setting to answered", err);
10395fe58019SAttilio Rao 			fticket_set_answered(fdip->tick);
10405fe58019SAttilio Rao 			fuse_lck_mtx_unlock(fdip->tick->tk_aw_mtx);
10415fe58019SAttilio Rao 			return err;
10425fe58019SAttilio Rao 		}
10435fe58019SAttilio Rao 	}
10445fe58019SAttilio Rao 
10457e0aac24SAlan Somers 	if (fdip->tick->tk_aw_errno == ENOTCONN) {
10467e0aac24SAlan Somers 		/* The daemon died while we were waiting for a response */
10477e0aac24SAlan Somers 		err = ENOTCONN;
10487e0aac24SAlan Somers 		goto out;
10497e0aac24SAlan Somers 	} else if (fdip->tick->tk_aw_errno) {
10507e0aac24SAlan Somers 		/*
10517e0aac24SAlan Somers 		 * There was some sort of communication error with the daemon
10527e0aac24SAlan Somers 		 * that the client wouldn't understand.
10537e0aac24SAlan Somers 		 */
1054419e7ff6SAlan Somers 		SDT_PROBE2(fusefs, , ipc, fdisp_wait_answ_error,
1055cf169498SAlan Somers 			"IPC: explicit EIO-ing", fdip->tick->tk_aw_errno);
10565fe58019SAttilio Rao 		err = EIO;
10575fe58019SAttilio Rao 		goto out;
10585fe58019SAttilio Rao 	}
10595fe58019SAttilio Rao 	if ((err = fdip->tick->tk_aw_ohead.error)) {
1060419e7ff6SAlan Somers 		SDT_PROBE2(fusefs, , ipc, fdisp_wait_answ_error,
1061cf169498SAlan Somers 			"IPC: setting status", fdip->tick->tk_aw_ohead.error);
10625fe58019SAttilio Rao 		/*
10635fe58019SAttilio Rao 	         * This means a "proper" fuse syscall error.
10645fe58019SAttilio Rao 	         * We record this value so the caller will
10655fe58019SAttilio Rao 	         * be able to know it's not a boring messaging
10665fe58019SAttilio Rao 	         * failure, if she wishes so (and if not, she can
10675fe58019SAttilio Rao 	         * just simply propagate the return value of this routine).
10685fe58019SAttilio Rao 	         * [XXX Maybe a bitflag would do the job too,
10695fe58019SAttilio Rao 	         * if other flags needed, this will be converted thusly.]
10705fe58019SAttilio Rao 	         */
10715fe58019SAttilio Rao 		fdip->answ_stat = err;
10725fe58019SAttilio Rao 		goto out;
10735fe58019SAttilio Rao 	}
10745fe58019SAttilio Rao 	fdip->answ = fticket_resp(fdip->tick)->base;
10755fe58019SAttilio Rao 	fdip->iosize = fticket_resp(fdip->tick)->len;
10765fe58019SAttilio Rao 
10775fe58019SAttilio Rao 	return 0;
10785fe58019SAttilio Rao 
10795fe58019SAttilio Rao out:
10805fe58019SAttilio Rao 	return err;
10815fe58019SAttilio Rao }
10825fe58019SAttilio Rao 
10835fe58019SAttilio Rao void
10845fe58019SAttilio Rao fuse_ipc_init(void)
10855fe58019SAttilio Rao {
10865fe58019SAttilio Rao 	ticket_zone = uma_zcreate("fuse_ticket", sizeof(struct fuse_ticket),
10875fe58019SAttilio Rao 	    fticket_ctor, fticket_dtor, fticket_init, fticket_fini,
10885fe58019SAttilio Rao 	    UMA_ALIGN_PTR, 0);
1089560a55d0SAlan Somers 	fuse_ticket_count = counter_u64_alloc(M_WAITOK);
1090560a55d0SAlan Somers 	counter_u64_zero(fuse_ticket_count);
10915fe58019SAttilio Rao }
10925fe58019SAttilio Rao 
10935fe58019SAttilio Rao void
10945fe58019SAttilio Rao fuse_ipc_destroy(void)
10955fe58019SAttilio Rao {
1096560a55d0SAlan Somers 	counter_u64_free(fuse_ticket_count);
10975fe58019SAttilio Rao 	uma_zdestroy(ticket_zone);
10985fe58019SAttilio Rao }
1099