xref: /freebsd/sys/fs/fuse/fuse_ipc.c (revision 8d013bec7ab92f8f7a2865e9adf59e1daa7db36a)
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  *
365fe58019SAttilio Rao  * Redistribution and use in source and binary forms, with or without
375fe58019SAttilio Rao  * modification, are permitted provided that the following conditions
385fe58019SAttilio Rao  * are met:
395fe58019SAttilio Rao  * 1. Redistributions of source code must retain the above copyright
405fe58019SAttilio Rao  *    notice, this list of conditions and the following disclaimer.
415fe58019SAttilio Rao  * 2. Redistributions in binary form must reproduce the above copyright
425fe58019SAttilio Rao  *    notice, this list of conditions and the following disclaimer in the
435fe58019SAttilio Rao  *    documentation and/or other materials provided with the distribution.
445fe58019SAttilio Rao  *
455fe58019SAttilio Rao  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
465fe58019SAttilio Rao  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
475fe58019SAttilio Rao  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
485fe58019SAttilio Rao  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
495fe58019SAttilio Rao  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
505fe58019SAttilio Rao  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
515fe58019SAttilio Rao  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
525fe58019SAttilio Rao  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
535fe58019SAttilio Rao  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
545fe58019SAttilio Rao  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
555fe58019SAttilio Rao  * SUCH DAMAGE.
565fe58019SAttilio Rao  */
575fe58019SAttilio Rao 
585fe58019SAttilio Rao #include <sys/cdefs.h>
595fe58019SAttilio Rao __FBSDID("$FreeBSD$");
605fe58019SAttilio Rao 
61cf169498SAlan Somers #include <sys/param.h>
625fe58019SAttilio Rao #include <sys/module.h>
635fe58019SAttilio Rao #include <sys/systm.h>
645fe58019SAttilio Rao #include <sys/errno.h>
655fe58019SAttilio Rao #include <sys/kernel.h>
665fe58019SAttilio Rao #include <sys/conf.h>
675fe58019SAttilio Rao #include <sys/uio.h>
685fe58019SAttilio Rao #include <sys/malloc.h>
695fe58019SAttilio Rao #include <sys/queue.h>
705fe58019SAttilio Rao #include <sys/lock.h>
715fe58019SAttilio Rao #include <sys/sx.h>
725fe58019SAttilio Rao #include <sys/mutex.h>
735fe58019SAttilio Rao #include <sys/proc.h>
745fe58019SAttilio Rao #include <sys/mount.h>
75cf169498SAlan Somers #include <sys/sdt.h>
765fe58019SAttilio Rao #include <sys/vnode.h>
775fe58019SAttilio Rao #include <sys/signalvar.h>
785fe58019SAttilio Rao #include <sys/syscallsubr.h>
795fe58019SAttilio Rao #include <sys/sysctl.h>
805fe58019SAttilio Rao #include <vm/uma.h>
815fe58019SAttilio Rao 
825fe58019SAttilio Rao #include "fuse.h"
835fe58019SAttilio Rao #include "fuse_node.h"
845fe58019SAttilio Rao #include "fuse_ipc.h"
855fe58019SAttilio Rao #include "fuse_internal.h"
865fe58019SAttilio Rao 
87cf169498SAlan Somers SDT_PROVIDER_DECLARE(fuse);
88cf169498SAlan Somers /*
89cf169498SAlan Somers  * Fuse trace probe:
90cf169498SAlan Somers  * arg0: verbosity.  Higher numbers give more verbose messages
91cf169498SAlan Somers  * arg1: Textual message
92cf169498SAlan Somers  */
93cf169498SAlan Somers SDT_PROBE_DEFINE2(fuse, , ipc, trace, "int", "char*");
945fe58019SAttilio Rao 
9512292a99SAlan Somers static void fiov_clear(struct fuse_iov *fiov);
965fe58019SAttilio Rao static struct fuse_ticket *fticket_alloc(struct fuse_data *data);
975fe58019SAttilio Rao static void fticket_refresh(struct fuse_ticket *ftick);
985fe58019SAttilio Rao static void fticket_destroy(struct fuse_ticket *ftick);
995fe58019SAttilio Rao static int fticket_wait_answer(struct fuse_ticket *ftick);
10002295cafSConrad Meyer static inline int
1015fe58019SAttilio Rao fticket_aw_pull_uio(struct fuse_ticket *ftick,
1025fe58019SAttilio Rao     struct uio *uio);
1035fe58019SAttilio Rao 
1045fe58019SAttilio Rao static int fuse_body_audit(struct fuse_ticket *ftick, size_t blen);
1055fe58019SAttilio Rao 
1065fe58019SAttilio Rao static fuse_handler_t fuse_standard_handler;
1075fe58019SAttilio Rao 
108123af6ecSAlan Somers SYSCTL_NODE(_vfs, OID_AUTO, fusefs, CTLFLAG_RW, 0, "FUSE tunables");
109123af6ecSAlan Somers SYSCTL_STRING(_vfs_fusefs, OID_AUTO, version, CTLFLAG_RD,
1105fe58019SAttilio Rao     FUSE_FREEBSD_VERSION, 0, "fuse-freebsd version");
1115fe58019SAttilio Rao static int fuse_ticket_count = 0;
1125fe58019SAttilio Rao 
113123af6ecSAlan Somers SYSCTL_INT(_vfs_fusefs, OID_AUTO, ticket_count, CTLFLAG_RW,
1145fe58019SAttilio Rao     &fuse_ticket_count, 0, "number of allocated tickets");
1155fe58019SAttilio Rao static long fuse_iov_permanent_bufsize = 1 << 19;
1165fe58019SAttilio Rao 
117123af6ecSAlan Somers SYSCTL_LONG(_vfs_fusefs, OID_AUTO, iov_permanent_bufsize, CTLFLAG_RW,
1185fe58019SAttilio Rao     &fuse_iov_permanent_bufsize, 0,
1195fe58019SAttilio Rao     "limit for permanently stored buffer size for fuse_iovs");
1205fe58019SAttilio Rao static int fuse_iov_credit = 16;
1215fe58019SAttilio Rao 
122123af6ecSAlan Somers SYSCTL_INT(_vfs_fusefs, OID_AUTO, iov_credit, CTLFLAG_RW,
1235fe58019SAttilio Rao     &fuse_iov_credit, 0,
1245fe58019SAttilio Rao     "how many times is an oversized fuse_iov tolerated");
1255fe58019SAttilio Rao 
1265fe58019SAttilio Rao MALLOC_DEFINE(M_FUSEMSG, "fuse_msgbuf", "fuse message buffer");
1275fe58019SAttilio Rao static uma_zone_t ticket_zone;
1285fe58019SAttilio Rao 
1295fe58019SAttilio Rao static void
1305fe58019SAttilio Rao fuse_block_sigs(sigset_t *oldset)
1315fe58019SAttilio Rao {
1325fe58019SAttilio Rao 	sigset_t newset;
1335fe58019SAttilio Rao 
1345fe58019SAttilio Rao 	SIGFILLSET(newset);
1355fe58019SAttilio Rao 	SIGDELSET(newset, SIGKILL);
1365fe58019SAttilio Rao 	if (kern_sigprocmask(curthread, SIG_BLOCK, &newset, oldset, 0))
1375fe58019SAttilio Rao 		panic("%s: Invalid operation for kern_sigprocmask()",
1385fe58019SAttilio Rao 		    __func__);
1395fe58019SAttilio Rao }
1405fe58019SAttilio Rao 
1415fe58019SAttilio Rao static void
1425fe58019SAttilio Rao fuse_restore_sigs(sigset_t *oldset)
1435fe58019SAttilio Rao {
1445fe58019SAttilio Rao 
1455fe58019SAttilio Rao 	if (kern_sigprocmask(curthread, SIG_SETMASK, oldset, NULL, 0))
1465fe58019SAttilio Rao 		panic("%s: Invalid operation for kern_sigprocmask()",
1475fe58019SAttilio Rao 		    __func__);
1485fe58019SAttilio Rao }
1495fe58019SAttilio Rao 
1505fe58019SAttilio Rao void
1515fe58019SAttilio Rao fiov_init(struct fuse_iov *fiov, size_t size)
1525fe58019SAttilio Rao {
1535fe58019SAttilio Rao 	uint32_t msize = FU_AT_LEAST(size);
1545fe58019SAttilio Rao 
1555fe58019SAttilio Rao 	fiov->len = 0;
1565fe58019SAttilio Rao 
1575fe58019SAttilio Rao 	fiov->base = malloc(msize, M_FUSEMSG, M_WAITOK | M_ZERO);
1585fe58019SAttilio Rao 
1595fe58019SAttilio Rao 	fiov->allocated_size = msize;
1605fe58019SAttilio Rao 	fiov->credit = fuse_iov_credit;
1615fe58019SAttilio Rao }
1625fe58019SAttilio Rao 
1635fe58019SAttilio Rao void
1645fe58019SAttilio Rao fiov_teardown(struct fuse_iov *fiov)
1655fe58019SAttilio Rao {
1665fe58019SAttilio Rao 	MPASS(fiov->base != NULL);
1675fe58019SAttilio Rao 	free(fiov->base, M_FUSEMSG);
1685fe58019SAttilio Rao }
1695fe58019SAttilio Rao 
1705fe58019SAttilio Rao void
1715fe58019SAttilio Rao fiov_adjust(struct fuse_iov *fiov, size_t size)
1725fe58019SAttilio Rao {
1735fe58019SAttilio Rao 	if (fiov->allocated_size < size ||
1745fe58019SAttilio Rao 	    (fuse_iov_permanent_bufsize >= 0 &&
1755fe58019SAttilio Rao 	    fiov->allocated_size - size > fuse_iov_permanent_bufsize &&
1765fe58019SAttilio Rao 	    --fiov->credit < 0)) {
1775fe58019SAttilio Rao 
1785fe58019SAttilio Rao 		fiov->base = realloc(fiov->base, FU_AT_LEAST(size), M_FUSEMSG,
1795fe58019SAttilio Rao 		    M_WAITOK | M_ZERO);
1805fe58019SAttilio Rao 		if (!fiov->base) {
1815fe58019SAttilio Rao 			panic("FUSE: realloc failed");
1825fe58019SAttilio Rao 		}
1835fe58019SAttilio Rao 		fiov->allocated_size = FU_AT_LEAST(size);
1845fe58019SAttilio Rao 		fiov->credit = fuse_iov_credit;
185*8d013becSAlan Somers 		/* Clear data buffer after reallocation */
186*8d013becSAlan Somers 		bzero(fiov->base, size);
187*8d013becSAlan Somers 	} else if (size > fiov->len) {
188*8d013becSAlan Somers 		/* Clear newly extended portion of data buffer */
189*8d013becSAlan Somers 		bzero((char*)fiov->base + fiov->len, size - fiov->len);
1905fe58019SAttilio Rao 	}
1915fe58019SAttilio Rao 	fiov->len = size;
1925fe58019SAttilio Rao }
1935fe58019SAttilio Rao 
19412292a99SAlan Somers /* Clear the fiov's data buffer */
19512292a99SAlan Somers static void
19612292a99SAlan Somers fiov_clear(struct fuse_iov *fiov)
19712292a99SAlan Somers {
19812292a99SAlan Somers 	bzero(fiov->base, fiov->len);
19912292a99SAlan Somers }
20012292a99SAlan Somers 
20112292a99SAlan Somers /* Resize the fiov if needed, and clear it's buffer */
2025fe58019SAttilio Rao void
2035fe58019SAttilio Rao fiov_refresh(struct fuse_iov *fiov)
2045fe58019SAttilio Rao {
2055fe58019SAttilio Rao 	fiov_adjust(fiov, 0);
2065fe58019SAttilio Rao }
2075fe58019SAttilio Rao 
2085fe58019SAttilio Rao static int
2095fe58019SAttilio Rao fticket_ctor(void *mem, int size, void *arg, int flags)
2105fe58019SAttilio Rao {
2115fe58019SAttilio Rao 	struct fuse_ticket *ftick = mem;
2125fe58019SAttilio Rao 	struct fuse_data *data = arg;
2135fe58019SAttilio Rao 
2145fe58019SAttilio Rao 	FUSE_ASSERT_MS_DONE(ftick);
2155fe58019SAttilio Rao 	FUSE_ASSERT_AW_DONE(ftick);
2165fe58019SAttilio Rao 
2175fe58019SAttilio Rao 	ftick->tk_data = data;
2185fe58019SAttilio Rao 
2195fe58019SAttilio Rao 	if (ftick->tk_unique != 0)
2205fe58019SAttilio Rao 		fticket_refresh(ftick);
2215fe58019SAttilio Rao 
2225fe58019SAttilio Rao 	/* May be truncated to 32 bits */
2235fe58019SAttilio Rao 	ftick->tk_unique = atomic_fetchadd_long(&data->ticketer, 1);
2245fe58019SAttilio Rao 	if (ftick->tk_unique == 0)
2255fe58019SAttilio Rao 		ftick->tk_unique = atomic_fetchadd_long(&data->ticketer, 1);
2265fe58019SAttilio Rao 
2275fe58019SAttilio Rao 	refcount_init(&ftick->tk_refcount, 1);
2285fe58019SAttilio Rao 	atomic_add_acq_int(&fuse_ticket_count, 1);
2295fe58019SAttilio Rao 
2305fe58019SAttilio Rao 	return 0;
2315fe58019SAttilio Rao }
2325fe58019SAttilio Rao 
2335fe58019SAttilio Rao static void
2345fe58019SAttilio Rao fticket_dtor(void *mem, int size, void *arg)
2355fe58019SAttilio Rao {
236f220ef0bSAlan Somers #ifdef INVARIANTS
2375fe58019SAttilio Rao 	struct fuse_ticket *ftick = mem;
238f220ef0bSAlan Somers #endif
2395fe58019SAttilio Rao 
2405fe58019SAttilio Rao 	FUSE_ASSERT_MS_DONE(ftick);
2415fe58019SAttilio Rao 	FUSE_ASSERT_AW_DONE(ftick);
2425fe58019SAttilio Rao 
2435fe58019SAttilio Rao 	atomic_subtract_acq_int(&fuse_ticket_count, 1);
2445fe58019SAttilio Rao }
2455fe58019SAttilio Rao 
2465fe58019SAttilio Rao static int
2475fe58019SAttilio Rao fticket_init(void *mem, int size, int flags)
2485fe58019SAttilio Rao {
2495fe58019SAttilio Rao 	struct fuse_ticket *ftick = mem;
2505fe58019SAttilio Rao 
2515fe58019SAttilio Rao 	bzero(ftick, sizeof(struct fuse_ticket));
2525fe58019SAttilio Rao 
2535fe58019SAttilio Rao 	fiov_init(&ftick->tk_ms_fiov, sizeof(struct fuse_in_header));
2545fe58019SAttilio Rao 	ftick->tk_ms_type = FT_M_FIOV;
2555fe58019SAttilio Rao 
2565fe58019SAttilio Rao 	mtx_init(&ftick->tk_aw_mtx, "fuse answer delivery mutex", NULL, MTX_DEF);
2575fe58019SAttilio Rao 	fiov_init(&ftick->tk_aw_fiov, 0);
2585fe58019SAttilio Rao 	ftick->tk_aw_type = FT_A_FIOV;
2595fe58019SAttilio Rao 
2605fe58019SAttilio Rao 	return 0;
2615fe58019SAttilio Rao }
2625fe58019SAttilio Rao 
2635fe58019SAttilio Rao static void
2645fe58019SAttilio Rao fticket_fini(void *mem, int size)
2655fe58019SAttilio Rao {
2665fe58019SAttilio Rao 	struct fuse_ticket *ftick = mem;
2675fe58019SAttilio Rao 
2685fe58019SAttilio Rao 	fiov_teardown(&ftick->tk_ms_fiov);
2695fe58019SAttilio Rao 	fiov_teardown(&ftick->tk_aw_fiov);
2705fe58019SAttilio Rao 	mtx_destroy(&ftick->tk_aw_mtx);
2715fe58019SAttilio Rao }
2725fe58019SAttilio Rao 
27302295cafSConrad Meyer static inline struct fuse_ticket *
2745fe58019SAttilio Rao fticket_alloc(struct fuse_data *data)
2755fe58019SAttilio Rao {
2765fe58019SAttilio Rao 	return uma_zalloc_arg(ticket_zone, data, M_WAITOK);
2775fe58019SAttilio Rao }
2785fe58019SAttilio Rao 
27902295cafSConrad Meyer static inline void
2805fe58019SAttilio Rao fticket_destroy(struct fuse_ticket *ftick)
2815fe58019SAttilio Rao {
2825fe58019SAttilio Rao 	return uma_zfree(ticket_zone, ftick);
2835fe58019SAttilio Rao }
2845fe58019SAttilio Rao 
28502295cafSConrad Meyer static inline
2865fe58019SAttilio Rao void
2875fe58019SAttilio Rao fticket_refresh(struct fuse_ticket *ftick)
2885fe58019SAttilio Rao {
2895fe58019SAttilio Rao 	FUSE_ASSERT_MS_DONE(ftick);
2905fe58019SAttilio Rao 	FUSE_ASSERT_AW_DONE(ftick);
2915fe58019SAttilio Rao 
2925fe58019SAttilio Rao 	fiov_refresh(&ftick->tk_ms_fiov);
2935fe58019SAttilio Rao 	ftick->tk_ms_bufdata = NULL;
2945fe58019SAttilio Rao 	ftick->tk_ms_bufsize = 0;
2955fe58019SAttilio Rao 	ftick->tk_ms_type = FT_M_FIOV;
2965fe58019SAttilio Rao 
2975fe58019SAttilio Rao 	bzero(&ftick->tk_aw_ohead, sizeof(struct fuse_out_header));
2985fe58019SAttilio Rao 
2995fe58019SAttilio Rao 	fiov_refresh(&ftick->tk_aw_fiov);
3005fe58019SAttilio Rao 	ftick->tk_aw_errno = 0;
3015fe58019SAttilio Rao 	ftick->tk_aw_bufdata = NULL;
3025fe58019SAttilio Rao 	ftick->tk_aw_bufsize = 0;
3035fe58019SAttilio Rao 	ftick->tk_aw_type = FT_A_FIOV;
3045fe58019SAttilio Rao 
3055fe58019SAttilio Rao 	ftick->tk_flag = 0;
3065fe58019SAttilio Rao }
3075fe58019SAttilio Rao 
30812292a99SAlan Somers /* Prepar the ticket to be reused, but don't clear its data buffers */
30912292a99SAlan Somers static inline void
31012292a99SAlan Somers fticket_reset(struct fuse_ticket *ftick)
31112292a99SAlan Somers {
31212292a99SAlan Somers 	FUSE_ASSERT_MS_DONE(ftick);
31312292a99SAlan Somers 	FUSE_ASSERT_AW_DONE(ftick);
31412292a99SAlan Somers 
31512292a99SAlan Somers 	ftick->tk_ms_bufdata = NULL;
31612292a99SAlan Somers 	ftick->tk_ms_bufsize = 0;
31712292a99SAlan Somers 	ftick->tk_ms_type = FT_M_FIOV;
31812292a99SAlan Somers 
31912292a99SAlan Somers 	bzero(&ftick->tk_aw_ohead, sizeof(struct fuse_out_header));
32012292a99SAlan Somers 
32112292a99SAlan Somers 	ftick->tk_aw_errno = 0;
32212292a99SAlan Somers 	ftick->tk_aw_bufdata = NULL;
32312292a99SAlan Somers 	ftick->tk_aw_bufsize = 0;
32412292a99SAlan Somers 	ftick->tk_aw_type = FT_A_FIOV;
32512292a99SAlan Somers 
32612292a99SAlan Somers 	ftick->tk_flag = 0;
32712292a99SAlan Somers }
32812292a99SAlan Somers 
3295fe58019SAttilio Rao static int
3305fe58019SAttilio Rao fticket_wait_answer(struct fuse_ticket *ftick)
3315fe58019SAttilio Rao {
3325fe58019SAttilio Rao 	sigset_t tset;
3335fe58019SAttilio Rao 	int err = 0;
3345fe58019SAttilio Rao 	struct fuse_data *data;
3355fe58019SAttilio Rao 
336c02ccc7eSAlan Somers 	fuse_lck_mtx_lock(ftick->tk_aw_mtx);
337c02ccc7eSAlan Somers 
3385fe58019SAttilio Rao 	if (fticket_answered(ftick)) {
3395fe58019SAttilio Rao 		goto out;
3405fe58019SAttilio Rao 	}
3415fe58019SAttilio Rao 	data = ftick->tk_data;
3425fe58019SAttilio Rao 
3435fe58019SAttilio Rao 	if (fdata_get_dead(data)) {
3445fe58019SAttilio Rao 		err = ENOTCONN;
3455fe58019SAttilio Rao 		fticket_set_answered(ftick);
3465fe58019SAttilio Rao 		goto out;
3475fe58019SAttilio Rao 	}
3485fe58019SAttilio Rao 	fuse_block_sigs(&tset);
3495fe58019SAttilio Rao 	err = msleep(ftick, &ftick->tk_aw_mtx, PCATCH, "fu_ans",
3505fe58019SAttilio Rao 	    data->daemon_timeout * hz);
3515fe58019SAttilio Rao 	fuse_restore_sigs(&tset);
3525fe58019SAttilio Rao 	if (err == EAGAIN) {		/* same as EWOULDBLOCK */
3535fe58019SAttilio Rao #ifdef XXXIP				/* die conditionally */
3545fe58019SAttilio Rao 		if (!fdata_get_dead(data)) {
3555fe58019SAttilio Rao 			fdata_set_dead(data);
3565fe58019SAttilio Rao 		}
3575fe58019SAttilio Rao #endif
3585fe58019SAttilio Rao 		err = ETIMEDOUT;
3595fe58019SAttilio Rao 		fticket_set_answered(ftick);
3605fe58019SAttilio Rao 	}
3615fe58019SAttilio Rao out:
3625fe58019SAttilio Rao 	if (!(err || fticket_answered(ftick))) {
363cf169498SAlan Somers 		SDT_PROBE2(fuse, , ipc, trace, 1,
364cf169498SAlan Somers 			"FUSE: requester was woken up but still no answer");
3655fe58019SAttilio Rao 		err = ENXIO;
3665fe58019SAttilio Rao 	}
3675fe58019SAttilio Rao 	fuse_lck_mtx_unlock(ftick->tk_aw_mtx);
3685fe58019SAttilio Rao 
3695fe58019SAttilio Rao 	return err;
3705fe58019SAttilio Rao }
3715fe58019SAttilio Rao 
37202295cafSConrad Meyer static	inline
3735fe58019SAttilio Rao int
3745fe58019SAttilio Rao fticket_aw_pull_uio(struct fuse_ticket *ftick, struct uio *uio)
3755fe58019SAttilio Rao {
3765fe58019SAttilio Rao 	int err = 0;
3775fe58019SAttilio Rao 	size_t len = uio_resid(uio);
3785fe58019SAttilio Rao 
3795fe58019SAttilio Rao 	if (len) {
3805fe58019SAttilio Rao 		switch (ftick->tk_aw_type) {
3815fe58019SAttilio Rao 		case FT_A_FIOV:
3825fe58019SAttilio Rao 			fiov_adjust(fticket_resp(ftick), len);
3835fe58019SAttilio Rao 			err = uiomove(fticket_resp(ftick)->base, len, uio);
3845fe58019SAttilio Rao 			break;
3855fe58019SAttilio Rao 
3865fe58019SAttilio Rao 		case FT_A_BUF:
3875fe58019SAttilio Rao 			ftick->tk_aw_bufsize = len;
3885fe58019SAttilio Rao 			err = uiomove(ftick->tk_aw_bufdata, len, uio);
3895fe58019SAttilio Rao 			break;
3905fe58019SAttilio Rao 
3915fe58019SAttilio Rao 		default:
3925fe58019SAttilio Rao 			panic("FUSE: unknown answer type for ticket %p", ftick);
3935fe58019SAttilio Rao 		}
3945fe58019SAttilio Rao 	}
3955fe58019SAttilio Rao 	return err;
3965fe58019SAttilio Rao }
3975fe58019SAttilio Rao 
3985fe58019SAttilio Rao int
3995fe58019SAttilio Rao fticket_pull(struct fuse_ticket *ftick, struct uio *uio)
4005fe58019SAttilio Rao {
4015fe58019SAttilio Rao 	int err = 0;
4025fe58019SAttilio Rao 
4035fe58019SAttilio Rao 	if (ftick->tk_aw_ohead.error) {
4045fe58019SAttilio Rao 		return 0;
4055fe58019SAttilio Rao 	}
4065fe58019SAttilio Rao 	err = fuse_body_audit(ftick, uio_resid(uio));
4075fe58019SAttilio Rao 	if (!err) {
4085fe58019SAttilio Rao 		err = fticket_aw_pull_uio(ftick, uio);
4095fe58019SAttilio Rao 	}
4105fe58019SAttilio Rao 	return err;
4115fe58019SAttilio Rao }
4125fe58019SAttilio Rao 
4135fe58019SAttilio Rao struct fuse_data *
4145fe58019SAttilio Rao fdata_alloc(struct cdev *fdev, struct ucred *cred)
4155fe58019SAttilio Rao {
4165fe58019SAttilio Rao 	struct fuse_data *data;
4175fe58019SAttilio Rao 
4185fe58019SAttilio Rao 	data = malloc(sizeof(struct fuse_data), M_FUSEMSG, M_WAITOK | M_ZERO);
4195fe58019SAttilio Rao 
4205fe58019SAttilio Rao 	data->fdev = fdev;
4215fe58019SAttilio Rao 	mtx_init(&data->ms_mtx, "fuse message list mutex", NULL, MTX_DEF);
4225fe58019SAttilio Rao 	STAILQ_INIT(&data->ms_head);
4235fe58019SAttilio Rao 	mtx_init(&data->aw_mtx, "fuse answer list mutex", NULL, MTX_DEF);
4245fe58019SAttilio Rao 	TAILQ_INIT(&data->aw_head);
4255fe58019SAttilio Rao 	data->daemoncred = crhold(cred);
4265fe58019SAttilio Rao 	data->daemon_timeout = FUSE_DEFAULT_DAEMON_TIMEOUT;
4275fe58019SAttilio Rao 	sx_init(&data->rename_lock, "fuse rename lock");
4285fe58019SAttilio Rao 	data->ref = 1;
4295fe58019SAttilio Rao 
4305fe58019SAttilio Rao 	return data;
4315fe58019SAttilio Rao }
4325fe58019SAttilio Rao 
4335fe58019SAttilio Rao void
4345fe58019SAttilio Rao fdata_trydestroy(struct fuse_data *data)
4355fe58019SAttilio Rao {
4365fe58019SAttilio Rao 	data->ref--;
4375fe58019SAttilio Rao 	MPASS(data->ref >= 0);
4385fe58019SAttilio Rao 	if (data->ref != 0)
4395fe58019SAttilio Rao 		return;
4405fe58019SAttilio Rao 
4415fe58019SAttilio Rao 	/* Driving off stage all that stuff thrown at device... */
4425fe58019SAttilio Rao 	mtx_destroy(&data->ms_mtx);
4435fe58019SAttilio Rao 	mtx_destroy(&data->aw_mtx);
4445fe58019SAttilio Rao 	sx_destroy(&data->rename_lock);
4455fe58019SAttilio Rao 
4465fe58019SAttilio Rao 	crfree(data->daemoncred);
4475fe58019SAttilio Rao 
4485fe58019SAttilio Rao 	free(data, M_FUSEMSG);
4495fe58019SAttilio Rao }
4505fe58019SAttilio Rao 
4515fe58019SAttilio Rao void
4525fe58019SAttilio Rao fdata_set_dead(struct fuse_data *data)
4535fe58019SAttilio Rao {
4545fe58019SAttilio Rao 	FUSE_LOCK();
4555fe58019SAttilio Rao 	if (fdata_get_dead(data)) {
4565fe58019SAttilio Rao 		FUSE_UNLOCK();
4575fe58019SAttilio Rao 		return;
4585fe58019SAttilio Rao 	}
4595fe58019SAttilio Rao 	fuse_lck_mtx_lock(data->ms_mtx);
4605fe58019SAttilio Rao 	data->dataflags |= FSESS_DEAD;
4615fe58019SAttilio Rao 	wakeup_one(data);
4625fe58019SAttilio Rao 	selwakeuppri(&data->ks_rsel, PZERO + 1);
4635fe58019SAttilio Rao 	wakeup(&data->ticketer);
4645fe58019SAttilio Rao 	fuse_lck_mtx_unlock(data->ms_mtx);
4655fe58019SAttilio Rao 	FUSE_UNLOCK();
4665fe58019SAttilio Rao }
4675fe58019SAttilio Rao 
4685fe58019SAttilio Rao struct fuse_ticket *
4695fe58019SAttilio Rao fuse_ticket_fetch(struct fuse_data *data)
4705fe58019SAttilio Rao {
4715fe58019SAttilio Rao 	int err = 0;
4725fe58019SAttilio Rao 	struct fuse_ticket *ftick;
4735fe58019SAttilio Rao 
4745fe58019SAttilio Rao 	ftick = fticket_alloc(data);
4755fe58019SAttilio Rao 
4765fe58019SAttilio Rao 	if (!(data->dataflags & FSESS_INITED)) {
4775fe58019SAttilio Rao 		/* Sleep until get answer for INIT messsage */
4785fe58019SAttilio Rao 		FUSE_LOCK();
4795fe58019SAttilio Rao 		if (!(data->dataflags & FSESS_INITED) && data->ticketer > 2) {
4805fe58019SAttilio Rao 			err = msleep(&data->ticketer, &fuse_mtx, PCATCH | PDROP,
4815fe58019SAttilio Rao 			    "fu_ini", 0);
4825fe58019SAttilio Rao 			if (err)
4835fe58019SAttilio Rao 				fdata_set_dead(data);
4845fe58019SAttilio Rao 		} else
4855fe58019SAttilio Rao 			FUSE_UNLOCK();
4865fe58019SAttilio Rao 	}
4875fe58019SAttilio Rao 	return ftick;
4885fe58019SAttilio Rao }
4895fe58019SAttilio Rao 
4905fe58019SAttilio Rao int
4915fe58019SAttilio Rao fuse_ticket_drop(struct fuse_ticket *ftick)
4925fe58019SAttilio Rao {
4935fe58019SAttilio Rao 	int die;
4945fe58019SAttilio Rao 
4955fe58019SAttilio Rao 	die = refcount_release(&ftick->tk_refcount);
4965fe58019SAttilio Rao 	if (die)
4975fe58019SAttilio Rao 		fticket_destroy(ftick);
4985fe58019SAttilio Rao 
4995fe58019SAttilio Rao 	return die;
5005fe58019SAttilio Rao }
5015fe58019SAttilio Rao 
5025fe58019SAttilio Rao void
5035fe58019SAttilio Rao fuse_insert_callback(struct fuse_ticket *ftick, fuse_handler_t * handler)
5045fe58019SAttilio Rao {
5055fe58019SAttilio Rao 	if (fdata_get_dead(ftick->tk_data)) {
5065fe58019SAttilio Rao 		return;
5075fe58019SAttilio Rao 	}
5085fe58019SAttilio Rao 	ftick->tk_aw_handler = handler;
5095fe58019SAttilio Rao 
5105fe58019SAttilio Rao 	fuse_lck_mtx_lock(ftick->tk_data->aw_mtx);
5115fe58019SAttilio Rao 	fuse_aw_push(ftick);
5125fe58019SAttilio Rao 	fuse_lck_mtx_unlock(ftick->tk_data->aw_mtx);
5135fe58019SAttilio Rao }
5145fe58019SAttilio Rao 
5155fe58019SAttilio Rao void
5165fe58019SAttilio Rao fuse_insert_message(struct fuse_ticket *ftick)
5175fe58019SAttilio Rao {
5185fe58019SAttilio Rao 	if (ftick->tk_flag & FT_DIRTY) {
5195fe58019SAttilio Rao 		panic("FUSE: ticket reused without being refreshed");
5205fe58019SAttilio Rao 	}
5215fe58019SAttilio Rao 	ftick->tk_flag |= FT_DIRTY;
5225fe58019SAttilio Rao 
5235fe58019SAttilio Rao 	if (fdata_get_dead(ftick->tk_data)) {
5245fe58019SAttilio Rao 		return;
5255fe58019SAttilio Rao 	}
5265fe58019SAttilio Rao 	fuse_lck_mtx_lock(ftick->tk_data->ms_mtx);
5275fe58019SAttilio Rao 	fuse_ms_push(ftick);
5285fe58019SAttilio Rao 	wakeup_one(ftick->tk_data);
5295fe58019SAttilio Rao 	selwakeuppri(&ftick->tk_data->ks_rsel, PZERO + 1);
5305fe58019SAttilio Rao 	fuse_lck_mtx_unlock(ftick->tk_data->ms_mtx);
5315fe58019SAttilio Rao }
5325fe58019SAttilio Rao 
5335fe58019SAttilio Rao static int
5345fe58019SAttilio Rao fuse_body_audit(struct fuse_ticket *ftick, size_t blen)
5355fe58019SAttilio Rao {
5365fe58019SAttilio Rao 	int err = 0;
5375fe58019SAttilio Rao 	enum fuse_opcode opcode;
5385fe58019SAttilio Rao 
5395fe58019SAttilio Rao 	opcode = fticket_opcode(ftick);
5405fe58019SAttilio Rao 
5415fe58019SAttilio Rao 	switch (opcode) {
5425fe58019SAttilio Rao 	case FUSE_LOOKUP:
5435fe58019SAttilio Rao 		err = (blen == sizeof(struct fuse_entry_out)) ? 0 : EINVAL;
5445fe58019SAttilio Rao 		break;
5455fe58019SAttilio Rao 
5465fe58019SAttilio Rao 	case FUSE_FORGET:
5475fe58019SAttilio Rao 		panic("FUSE: a handler has been intalled for FUSE_FORGET");
5485fe58019SAttilio Rao 		break;
5495fe58019SAttilio Rao 
5505fe58019SAttilio Rao 	case FUSE_GETATTR:
5515fe58019SAttilio Rao 		err = (blen == sizeof(struct fuse_attr_out)) ? 0 : EINVAL;
5525fe58019SAttilio Rao 		break;
5535fe58019SAttilio Rao 
5545fe58019SAttilio Rao 	case FUSE_SETATTR:
5555fe58019SAttilio Rao 		err = (blen == sizeof(struct fuse_attr_out)) ? 0 : EINVAL;
5565fe58019SAttilio Rao 		break;
5575fe58019SAttilio Rao 
5585fe58019SAttilio Rao 	case FUSE_READLINK:
5595fe58019SAttilio Rao 		err = (PAGE_SIZE >= blen) ? 0 : EINVAL;
5605fe58019SAttilio Rao 		break;
5615fe58019SAttilio Rao 
5625fe58019SAttilio Rao 	case FUSE_SYMLINK:
5635fe58019SAttilio Rao 		err = (blen == sizeof(struct fuse_entry_out)) ? 0 : EINVAL;
5645fe58019SAttilio Rao 		break;
5655fe58019SAttilio Rao 
5665fe58019SAttilio Rao 	case FUSE_MKNOD:
5675fe58019SAttilio Rao 		err = (blen == sizeof(struct fuse_entry_out)) ? 0 : EINVAL;
5685fe58019SAttilio Rao 		break;
5695fe58019SAttilio Rao 
5705fe58019SAttilio Rao 	case FUSE_MKDIR:
5715fe58019SAttilio Rao 		err = (blen == sizeof(struct fuse_entry_out)) ? 0 : EINVAL;
5725fe58019SAttilio Rao 		break;
5735fe58019SAttilio Rao 
5745fe58019SAttilio Rao 	case FUSE_UNLINK:
5755fe58019SAttilio Rao 		err = (blen == 0) ? 0 : EINVAL;
5765fe58019SAttilio Rao 		break;
5775fe58019SAttilio Rao 
5785fe58019SAttilio Rao 	case FUSE_RMDIR:
5795fe58019SAttilio Rao 		err = (blen == 0) ? 0 : EINVAL;
5805fe58019SAttilio Rao 		break;
5815fe58019SAttilio Rao 
5825fe58019SAttilio Rao 	case FUSE_RENAME:
5835fe58019SAttilio Rao 		err = (blen == 0) ? 0 : EINVAL;
5845fe58019SAttilio Rao 		break;
5855fe58019SAttilio Rao 
5865fe58019SAttilio Rao 	case FUSE_LINK:
5875fe58019SAttilio Rao 		err = (blen == sizeof(struct fuse_entry_out)) ? 0 : EINVAL;
5885fe58019SAttilio Rao 		break;
5895fe58019SAttilio Rao 
5905fe58019SAttilio Rao 	case FUSE_OPEN:
5915fe58019SAttilio Rao 		err = (blen == sizeof(struct fuse_open_out)) ? 0 : EINVAL;
5925fe58019SAttilio Rao 		break;
5935fe58019SAttilio Rao 
5945fe58019SAttilio Rao 	case FUSE_READ:
5955fe58019SAttilio Rao 		err = (((struct fuse_read_in *)(
5965fe58019SAttilio Rao 		    (char *)ftick->tk_ms_fiov.base +
5975fe58019SAttilio Rao 		    sizeof(struct fuse_in_header)
5985fe58019SAttilio Rao 		    ))->size >= blen) ? 0 : EINVAL;
5995fe58019SAttilio Rao 		break;
6005fe58019SAttilio Rao 
6015fe58019SAttilio Rao 	case FUSE_WRITE:
6025fe58019SAttilio Rao 		err = (blen == sizeof(struct fuse_write_out)) ? 0 : EINVAL;
6035fe58019SAttilio Rao 		break;
6045fe58019SAttilio Rao 
6055fe58019SAttilio Rao 	case FUSE_STATFS:
6065fe58019SAttilio Rao 		if (fuse_libabi_geq(ftick->tk_data, 7, 4)) {
6075fe58019SAttilio Rao 			err = (blen == sizeof(struct fuse_statfs_out)) ?
6085fe58019SAttilio Rao 			  0 : EINVAL;
6095fe58019SAttilio Rao 		} else {
6105fe58019SAttilio Rao 			err = (blen == FUSE_COMPAT_STATFS_SIZE) ? 0 : EINVAL;
6115fe58019SAttilio Rao 		}
6125fe58019SAttilio Rao 		break;
6135fe58019SAttilio Rao 
6145fe58019SAttilio Rao 	case FUSE_RELEASE:
6155fe58019SAttilio Rao 		err = (blen == 0) ? 0 : EINVAL;
6165fe58019SAttilio Rao 		break;
6175fe58019SAttilio Rao 
6185fe58019SAttilio Rao 	case FUSE_FSYNC:
6195fe58019SAttilio Rao 		err = (blen == 0) ? 0 : EINVAL;
6205fe58019SAttilio Rao 		break;
6215fe58019SAttilio Rao 
6225fe58019SAttilio Rao 	case FUSE_SETXATTR:
62304660064SFedor Uporov 		err = (blen == 0) ? 0 : EINVAL;
6245fe58019SAttilio Rao 		break;
6255fe58019SAttilio Rao 
6265fe58019SAttilio Rao 	case FUSE_GETXATTR:
6275fe58019SAttilio Rao 	case FUSE_LISTXATTR:
62804660064SFedor Uporov 		/*
62904660064SFedor Uporov 		 * These can have varying response lengths, and 0 length
63004660064SFedor Uporov 		 * isn't necessarily invalid.
63104660064SFedor Uporov 		 */
63204660064SFedor Uporov 		err = 0;
6335fe58019SAttilio Rao 		break;
6345fe58019SAttilio Rao 
6355fe58019SAttilio Rao 	case FUSE_REMOVEXATTR:
63604660064SFedor Uporov 		err = (blen == 0) ? 0 : EINVAL;
6375fe58019SAttilio Rao 		break;
6385fe58019SAttilio Rao 
6395fe58019SAttilio Rao 	case FUSE_FLUSH:
6405fe58019SAttilio Rao 		err = (blen == 0) ? 0 : EINVAL;
6415fe58019SAttilio Rao 		break;
6425fe58019SAttilio Rao 
6435fe58019SAttilio Rao 	case FUSE_INIT:
6445fe58019SAttilio Rao 		if (blen == sizeof(struct fuse_init_out) || blen == 8) {
6455fe58019SAttilio Rao 			err = 0;
6465fe58019SAttilio Rao 		} else {
6475fe58019SAttilio Rao 			err = EINVAL;
6485fe58019SAttilio Rao 		}
6495fe58019SAttilio Rao 		break;
6505fe58019SAttilio Rao 
6515fe58019SAttilio Rao 	case FUSE_OPENDIR:
6525fe58019SAttilio Rao 		err = (blen == sizeof(struct fuse_open_out)) ? 0 : EINVAL;
6535fe58019SAttilio Rao 		break;
6545fe58019SAttilio Rao 
6555fe58019SAttilio Rao 	case FUSE_READDIR:
6565fe58019SAttilio Rao 		err = (((struct fuse_read_in *)(
6575fe58019SAttilio Rao 		    (char *)ftick->tk_ms_fiov.base +
6585fe58019SAttilio Rao 		    sizeof(struct fuse_in_header)
6595fe58019SAttilio Rao 		    ))->size >= blen) ? 0 : EINVAL;
6605fe58019SAttilio Rao 		break;
6615fe58019SAttilio Rao 
6625fe58019SAttilio Rao 	case FUSE_RELEASEDIR:
6635fe58019SAttilio Rao 		err = (blen == 0) ? 0 : EINVAL;
6645fe58019SAttilio Rao 		break;
6655fe58019SAttilio Rao 
6665fe58019SAttilio Rao 	case FUSE_FSYNCDIR:
6675fe58019SAttilio Rao 		err = (blen == 0) ? 0 : EINVAL;
6685fe58019SAttilio Rao 		break;
6695fe58019SAttilio Rao 
6705fe58019SAttilio Rao 	case FUSE_GETLK:
6715fe58019SAttilio Rao 		panic("FUSE: no response body format check for FUSE_GETLK");
6725fe58019SAttilio Rao 		break;
6735fe58019SAttilio Rao 
6745fe58019SAttilio Rao 	case FUSE_SETLK:
6755fe58019SAttilio Rao 		panic("FUSE: no response body format check for FUSE_SETLK");
6765fe58019SAttilio Rao 		break;
6775fe58019SAttilio Rao 
6785fe58019SAttilio Rao 	case FUSE_SETLKW:
6795fe58019SAttilio Rao 		panic("FUSE: no response body format check for FUSE_SETLKW");
6805fe58019SAttilio Rao 		break;
6815fe58019SAttilio Rao 
6825fe58019SAttilio Rao 	case FUSE_ACCESS:
6835fe58019SAttilio Rao 		err = (blen == 0) ? 0 : EINVAL;
6845fe58019SAttilio Rao 		break;
6855fe58019SAttilio Rao 
6865fe58019SAttilio Rao 	case FUSE_CREATE:
6875fe58019SAttilio Rao 		err = (blen == sizeof(struct fuse_entry_out) +
6885fe58019SAttilio Rao 		    sizeof(struct fuse_open_out)) ? 0 : EINVAL;
6895fe58019SAttilio Rao 		break;
6905fe58019SAttilio Rao 
6915fe58019SAttilio Rao 	case FUSE_DESTROY:
6925fe58019SAttilio Rao 		err = (blen == 0) ? 0 : EINVAL;
6935fe58019SAttilio Rao 		break;
6945fe58019SAttilio Rao 
6955fe58019SAttilio Rao 	default:
6965fe58019SAttilio Rao 		panic("FUSE: opcodes out of sync (%d)\n", opcode);
6975fe58019SAttilio Rao 	}
6985fe58019SAttilio Rao 
6995fe58019SAttilio Rao 	return err;
7005fe58019SAttilio Rao }
7015fe58019SAttilio Rao 
70202295cafSConrad Meyer static inline void
70302295cafSConrad Meyer fuse_setup_ihead(struct fuse_in_header *ihead, struct fuse_ticket *ftick,
70402295cafSConrad Meyer     uint64_t nid, enum fuse_opcode op, size_t blen, pid_t pid,
7055fe58019SAttilio Rao     struct ucred *cred)
7065fe58019SAttilio Rao {
7075fe58019SAttilio Rao 	ihead->len = sizeof(*ihead) + blen;
7085fe58019SAttilio Rao 	ihead->unique = ftick->tk_unique;
7095fe58019SAttilio Rao 	ihead->nodeid = nid;
7105fe58019SAttilio Rao 	ihead->opcode = op;
7115fe58019SAttilio Rao 
7125fe58019SAttilio Rao 	ihead->pid = pid;
7135fe58019SAttilio Rao 	ihead->uid = cred->cr_uid;
7145fe58019SAttilio Rao 	ihead->gid = cred->cr_rgid;
7155fe58019SAttilio Rao }
7165fe58019SAttilio Rao 
7175fe58019SAttilio Rao /*
7185fe58019SAttilio Rao  * fuse_standard_handler just pulls indata and wakes up pretender.
7195fe58019SAttilio Rao  * Doesn't try to interpret data, that's left for the pretender.
7205fe58019SAttilio Rao  * Though might do a basic size verification before the pull-in takes place
7215fe58019SAttilio Rao  */
7225fe58019SAttilio Rao 
7235fe58019SAttilio Rao static int
7245fe58019SAttilio Rao fuse_standard_handler(struct fuse_ticket *ftick, struct uio *uio)
7255fe58019SAttilio Rao {
7265fe58019SAttilio Rao 	int err = 0;
7275fe58019SAttilio Rao 
7285fe58019SAttilio Rao 	err = fticket_pull(ftick, uio);
7295fe58019SAttilio Rao 
7305fe58019SAttilio Rao 	fuse_lck_mtx_lock(ftick->tk_aw_mtx);
7315fe58019SAttilio Rao 
7325fe58019SAttilio Rao 	if (!fticket_answered(ftick)) {
7335fe58019SAttilio Rao 		fticket_set_answered(ftick);
7345fe58019SAttilio Rao 		ftick->tk_aw_errno = err;
7355fe58019SAttilio Rao 		wakeup(ftick);
7365fe58019SAttilio Rao 	}
7375fe58019SAttilio Rao 	fuse_lck_mtx_unlock(ftick->tk_aw_mtx);
7385fe58019SAttilio Rao 
7395fe58019SAttilio Rao 	return err;
7405fe58019SAttilio Rao }
7415fe58019SAttilio Rao 
74212292a99SAlan Somers /*
74312292a99SAlan Somers  * Reinitialize a dispatcher from a pid and node id, without resizing or
74412292a99SAlan Somers  * clearing its data buffers
74512292a99SAlan Somers  */
74612292a99SAlan Somers static void
74712292a99SAlan Somers fdisp_refresh_pid(struct fuse_dispatcher *fdip, enum fuse_opcode op,
74812292a99SAlan Somers     struct mount *mp, uint64_t nid, pid_t pid, struct ucred *cred)
74912292a99SAlan Somers {
75012292a99SAlan Somers 	MPASS(fdip->tick);
751*8d013becSAlan Somers 	MPASS2(sizeof(fdip->finh) + fdip->iosize <= fdip->tick->tk_ms_fiov.len,
752*8d013becSAlan Somers 		"Must use fdisp_make_pid to increase the size of the fiov");
75312292a99SAlan Somers 	fticket_reset(fdip->tick);
75412292a99SAlan Somers 
75512292a99SAlan Somers 	FUSE_DIMALLOC(&fdip->tick->tk_ms_fiov, fdip->finh,
75612292a99SAlan Somers 	    fdip->indata, fdip->iosize);
75712292a99SAlan Somers 
75812292a99SAlan Somers 	fuse_setup_ihead(fdip->finh, fdip->tick, nid, op, fdip->iosize, pid,
75912292a99SAlan Somers 		cred);
76012292a99SAlan Somers }
76112292a99SAlan Somers 
76212292a99SAlan Somers /* Initialize a dispatcher from a pid and node id */
76312292a99SAlan Somers static void
76402295cafSConrad Meyer fdisp_make_pid(struct fuse_dispatcher *fdip, enum fuse_opcode op,
76502295cafSConrad Meyer     struct mount *mp, uint64_t nid, pid_t pid, struct ucred *cred)
7665fe58019SAttilio Rao {
7675fe58019SAttilio Rao 	struct fuse_data *data = fuse_get_mpdata(mp);
7685fe58019SAttilio Rao 
7695fe58019SAttilio Rao 	if (fdip->tick) {
7705fe58019SAttilio Rao 		fticket_refresh(fdip->tick);
7715fe58019SAttilio Rao 	} else {
7725fe58019SAttilio Rao 		fdip->tick = fuse_ticket_fetch(data);
7735fe58019SAttilio Rao 	}
7745fe58019SAttilio Rao 
775*8d013becSAlan Somers 	/* FUSE_DIMALLOC will bzero the fiovs when it enlarges them */
7765fe58019SAttilio Rao 	FUSE_DIMALLOC(&fdip->tick->tk_ms_fiov, fdip->finh,
7775fe58019SAttilio Rao 	    fdip->indata, fdip->iosize);
7785fe58019SAttilio Rao 
7795fe58019SAttilio Rao 	fuse_setup_ihead(fdip->finh, fdip->tick, nid, op, fdip->iosize, pid, cred);
7805fe58019SAttilio Rao }
7815fe58019SAttilio Rao 
7825fe58019SAttilio Rao void
78302295cafSConrad Meyer fdisp_make(struct fuse_dispatcher *fdip, enum fuse_opcode op, struct mount *mp,
78402295cafSConrad Meyer     uint64_t nid, struct thread *td, struct ucred *cred)
7855fe58019SAttilio Rao {
7865fe58019SAttilio Rao 	RECTIFY_TDCR(td, cred);
7875fe58019SAttilio Rao 
7885fe58019SAttilio Rao 	return fdisp_make_pid(fdip, op, mp, nid, td->td_proc->p_pid, cred);
7895fe58019SAttilio Rao }
7905fe58019SAttilio Rao 
7915fe58019SAttilio Rao void
79202295cafSConrad Meyer fdisp_make_vp(struct fuse_dispatcher *fdip, enum fuse_opcode op,
79302295cafSConrad Meyer     struct vnode *vp, struct thread *td, struct ucred *cred)
7945fe58019SAttilio Rao {
7955fe58019SAttilio Rao 	RECTIFY_TDCR(td, cred);
7965fe58019SAttilio Rao 	return fdisp_make_pid(fdip, op, vnode_mount(vp), VTOI(vp),
7975fe58019SAttilio Rao 	    td->td_proc->p_pid, cred);
7985fe58019SAttilio Rao }
7995fe58019SAttilio Rao 
80012292a99SAlan Somers /* Refresh a fuse_dispatcher so it can be reused, but don't zero its data */
80112292a99SAlan Somers void
80212292a99SAlan Somers fdisp_refresh_vp(struct fuse_dispatcher *fdip, enum fuse_opcode op,
80312292a99SAlan Somers     struct vnode *vp, struct thread *td, struct ucred *cred)
80412292a99SAlan Somers {
80512292a99SAlan Somers 	RECTIFY_TDCR(td, cred);
80612292a99SAlan Somers 	return fdisp_refresh_pid(fdip, op, vnode_mount(vp), VTOI(vp),
80712292a99SAlan Somers 	    td->td_proc->p_pid, cred);
80812292a99SAlan Somers }
80912292a99SAlan Somers 
81012292a99SAlan Somers void
81112292a99SAlan Somers fdisp_refresh(struct fuse_dispatcher *fdip)
81212292a99SAlan Somers {
81312292a99SAlan Somers 	fticket_refresh(fdip->tick);
81412292a99SAlan Somers }
81512292a99SAlan Somers 
816cf169498SAlan Somers SDT_PROBE_DEFINE2(fuse, , ipc, fdisp_wait_answ_error, "char*", "int");
817cf169498SAlan Somers 
8185fe58019SAttilio Rao int
8195fe58019SAttilio Rao fdisp_wait_answ(struct fuse_dispatcher *fdip)
8205fe58019SAttilio Rao {
8215fe58019SAttilio Rao 	int err = 0;
8225fe58019SAttilio Rao 
8235fe58019SAttilio Rao 	fdip->answ_stat = 0;
8245fe58019SAttilio Rao 	fuse_insert_callback(fdip->tick, fuse_standard_handler);
8255fe58019SAttilio Rao 	fuse_insert_message(fdip->tick);
8265fe58019SAttilio Rao 
8275fe58019SAttilio Rao 	if ((err = fticket_wait_answer(fdip->tick))) {
8285fe58019SAttilio Rao 		fuse_lck_mtx_lock(fdip->tick->tk_aw_mtx);
8295fe58019SAttilio Rao 
8305fe58019SAttilio Rao 		if (fticket_answered(fdip->tick)) {
8315fe58019SAttilio Rao 			/*
8325fe58019SAttilio Rao 	                 * Just between noticing the interrupt and getting here,
8335fe58019SAttilio Rao 	                 * the standard handler has completed his job.
8345fe58019SAttilio Rao 	                 * So we drop the ticket and exit as usual.
8355fe58019SAttilio Rao 	                 */
836cf169498SAlan Somers 			SDT_PROBE2(fuse, , ipc, fdisp_wait_answ_error,
837cf169498SAlan Somers 				"IPC: interrupted, already answered", err);
8385fe58019SAttilio Rao 			fuse_lck_mtx_unlock(fdip->tick->tk_aw_mtx);
8395fe58019SAttilio Rao 			goto out;
8405fe58019SAttilio Rao 		} else {
8415fe58019SAttilio Rao 			/*
8425fe58019SAttilio Rao 	                 * So we were faster than the standard handler.
8435fe58019SAttilio Rao 	                 * Then by setting the answered flag we get *him*
8445fe58019SAttilio Rao 	                 * to drop the ticket.
8455fe58019SAttilio Rao 	                 */
846cf169498SAlan Somers 			SDT_PROBE2(fuse, , ipc, fdisp_wait_answ_error,
847cf169498SAlan Somers 				"IPC: interrupted, setting to answered", err);
8485fe58019SAttilio Rao 			fticket_set_answered(fdip->tick);
8495fe58019SAttilio Rao 			fuse_lck_mtx_unlock(fdip->tick->tk_aw_mtx);
8505fe58019SAttilio Rao 			return err;
8515fe58019SAttilio Rao 		}
8525fe58019SAttilio Rao 	}
8535fe58019SAttilio Rao 
8545fe58019SAttilio Rao 	if (fdip->tick->tk_aw_errno) {
855cf169498SAlan Somers 		SDT_PROBE2(fuse, , ipc, fdisp_wait_answ_error,
856cf169498SAlan Somers 			"IPC: explicit EIO-ing", fdip->tick->tk_aw_errno);
8575fe58019SAttilio Rao 		err = EIO;
8585fe58019SAttilio Rao 		goto out;
8595fe58019SAttilio Rao 	}
8605fe58019SAttilio Rao 	if ((err = fdip->tick->tk_aw_ohead.error)) {
861cf169498SAlan Somers 		SDT_PROBE2(fuse, , ipc, fdisp_wait_answ_error,
862cf169498SAlan Somers 			"IPC: setting status", fdip->tick->tk_aw_ohead.error);
8635fe58019SAttilio Rao 		/*
8645fe58019SAttilio Rao 	         * This means a "proper" fuse syscall error.
8655fe58019SAttilio Rao 	         * We record this value so the caller will
8665fe58019SAttilio Rao 	         * be able to know it's not a boring messaging
8675fe58019SAttilio Rao 	         * failure, if she wishes so (and if not, she can
8685fe58019SAttilio Rao 	         * just simply propagate the return value of this routine).
8695fe58019SAttilio Rao 	         * [XXX Maybe a bitflag would do the job too,
8705fe58019SAttilio Rao 	         * if other flags needed, this will be converted thusly.]
8715fe58019SAttilio Rao 	         */
8725fe58019SAttilio Rao 		fdip->answ_stat = err;
8735fe58019SAttilio Rao 		goto out;
8745fe58019SAttilio Rao 	}
8755fe58019SAttilio Rao 	fdip->answ = fticket_resp(fdip->tick)->base;
8765fe58019SAttilio Rao 	fdip->iosize = fticket_resp(fdip->tick)->len;
8775fe58019SAttilio Rao 
8785fe58019SAttilio Rao 	return 0;
8795fe58019SAttilio Rao 
8805fe58019SAttilio Rao out:
8815fe58019SAttilio Rao 	return err;
8825fe58019SAttilio Rao }
8835fe58019SAttilio Rao 
8845fe58019SAttilio Rao void
8855fe58019SAttilio Rao fuse_ipc_init(void)
8865fe58019SAttilio Rao {
8875fe58019SAttilio Rao 	ticket_zone = uma_zcreate("fuse_ticket", sizeof(struct fuse_ticket),
8885fe58019SAttilio Rao 	    fticket_ctor, fticket_dtor, fticket_init, fticket_fini,
8895fe58019SAttilio Rao 	    UMA_ALIGN_PTR, 0);
8905fe58019SAttilio Rao }
8915fe58019SAttilio Rao 
8925fe58019SAttilio Rao void
8935fe58019SAttilio Rao fuse_ipc_destroy(void)
8945fe58019SAttilio Rao {
8955fe58019SAttilio Rao 	uma_zdestroy(ticket_zone);
8965fe58019SAttilio Rao }
897