xref: /freebsd/sys/fs/fuse/fuse_device.c (revision cf16949867b7af0ac2b53b1489fb428d9608c6bb)
151369649SPedro F. Giffuni /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
45fe58019SAttilio Rao  * Copyright (c) 2007-2009 Google Inc.
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 
615fe58019SAttilio Rao #include <sys/types.h>
625fe58019SAttilio Rao #include <sys/module.h>
635fe58019SAttilio Rao #include <sys/systm.h>
645fe58019SAttilio Rao #include <sys/errno.h>
655fe58019SAttilio Rao #include <sys/param.h>
665fe58019SAttilio Rao #include <sys/kernel.h>
675fe58019SAttilio Rao #include <sys/conf.h>
685fe58019SAttilio Rao #include <sys/uio.h>
695fe58019SAttilio Rao #include <sys/malloc.h>
705fe58019SAttilio Rao #include <sys/queue.h>
715fe58019SAttilio Rao #include <sys/lock.h>
725fe58019SAttilio Rao #include <sys/sx.h>
735fe58019SAttilio Rao #include <sys/mutex.h>
745fe58019SAttilio Rao #include <sys/proc.h>
755fe58019SAttilio Rao #include <sys/mount.h>
76*cf169498SAlan Somers #include <sys/sdt.h>
775fe58019SAttilio Rao #include <sys/stat.h>
785fe58019SAttilio Rao #include <sys/fcntl.h>
795fe58019SAttilio Rao #include <sys/sysctl.h>
805fe58019SAttilio Rao #include <sys/poll.h>
815fe58019SAttilio Rao #include <sys/selinfo.h>
825fe58019SAttilio Rao 
835fe58019SAttilio Rao #include "fuse.h"
845fe58019SAttilio Rao #include "fuse_ipc.h"
855fe58019SAttilio Rao 
86*cf169498SAlan Somers SDT_PROVIDER_DECLARE(fuse);
87*cf169498SAlan Somers /*
88*cf169498SAlan Somers  * Fuse trace probe:
89*cf169498SAlan Somers  * arg0: verbosity.  Higher numbers give more verbose messages
90*cf169498SAlan Somers  * arg1: Textual message
91*cf169498SAlan Somers  */
92*cf169498SAlan Somers SDT_PROBE_DEFINE2(fuse, , device, trace, "int", "char*");
935fe58019SAttilio Rao 
945fe58019SAttilio Rao static struct cdev *fuse_dev;
955fe58019SAttilio Rao 
965fe58019SAttilio Rao static d_open_t fuse_device_open;
975fe58019SAttilio Rao static d_close_t fuse_device_close;
985fe58019SAttilio Rao static d_poll_t fuse_device_poll;
995fe58019SAttilio Rao static d_read_t fuse_device_read;
1005fe58019SAttilio Rao static d_write_t fuse_device_write;
1015fe58019SAttilio Rao 
1025fe58019SAttilio Rao static struct cdevsw fuse_device_cdevsw = {
1035fe58019SAttilio Rao 	.d_open = fuse_device_open,
1045fe58019SAttilio Rao 	.d_close = fuse_device_close,
1055fe58019SAttilio Rao 	.d_name = "fuse",
1065fe58019SAttilio Rao 	.d_poll = fuse_device_poll,
1075fe58019SAttilio Rao 	.d_read = fuse_device_read,
1085fe58019SAttilio Rao 	.d_write = fuse_device_write,
1095fe58019SAttilio Rao 	.d_version = D_VERSION,
1105fe58019SAttilio Rao };
1115fe58019SAttilio Rao 
1125fe58019SAttilio Rao /****************************
1135fe58019SAttilio Rao  *
1145fe58019SAttilio Rao  * >>> Fuse device op defs
1155fe58019SAttilio Rao  *
1165fe58019SAttilio Rao  ****************************/
1175fe58019SAttilio Rao 
1185fe58019SAttilio Rao static void
1195fe58019SAttilio Rao fdata_dtor(void *arg)
1205fe58019SAttilio Rao {
1215fe58019SAttilio Rao 	struct fuse_data *fdata;
1225fe58019SAttilio Rao 
1235fe58019SAttilio Rao 	fdata = arg;
1245fe58019SAttilio Rao 	fdata_trydestroy(fdata);
1255fe58019SAttilio Rao }
1265fe58019SAttilio Rao 
1275fe58019SAttilio Rao /*
1285fe58019SAttilio Rao  * Resources are set up on a per-open basis
1295fe58019SAttilio Rao  */
1305fe58019SAttilio Rao static int
1315fe58019SAttilio Rao fuse_device_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
1325fe58019SAttilio Rao {
1335fe58019SAttilio Rao 	struct fuse_data *fdata;
1345fe58019SAttilio Rao 	int error;
1355fe58019SAttilio Rao 
136*cf169498SAlan Somers 	SDT_PROBE2(fuse, , device, trace, 1, "device open");
1375fe58019SAttilio Rao 
1385fe58019SAttilio Rao 	fdata = fdata_alloc(dev, td->td_ucred);
1395fe58019SAttilio Rao 	error = devfs_set_cdevpriv(fdata, fdata_dtor);
1405fe58019SAttilio Rao 	if (error != 0)
1415fe58019SAttilio Rao 		fdata_trydestroy(fdata);
1425fe58019SAttilio Rao 	else
143*cf169498SAlan Somers 		SDT_PROBE2(fuse, , device, trace, 1, "device open success");
1445fe58019SAttilio Rao 	return (error);
1455fe58019SAttilio Rao }
1465fe58019SAttilio Rao 
1475fe58019SAttilio Rao static int
1485fe58019SAttilio Rao fuse_device_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
1495fe58019SAttilio Rao {
1505fe58019SAttilio Rao 	struct fuse_data *data;
1515fe58019SAttilio Rao 	struct fuse_ticket *tick;
1525fe58019SAttilio Rao 	int error;
1535fe58019SAttilio Rao 
1545fe58019SAttilio Rao 	error = devfs_get_cdevpriv((void **)&data);
1555fe58019SAttilio Rao 	if (error != 0)
1565fe58019SAttilio Rao 		return (error);
1575fe58019SAttilio Rao 	if (!data)
1585fe58019SAttilio Rao 		panic("no fuse data upon fuse device close");
1595fe58019SAttilio Rao 	fdata_set_dead(data);
1605fe58019SAttilio Rao 
1615fe58019SAttilio Rao 	FUSE_LOCK();
1625fe58019SAttilio Rao 	fuse_lck_mtx_lock(data->aw_mtx);
1635fe58019SAttilio Rao 	/* wakup poll()ers */
1645fe58019SAttilio Rao 	selwakeuppri(&data->ks_rsel, PZERO + 1);
1655fe58019SAttilio Rao 	/* Don't let syscall handlers wait in vain */
1665fe58019SAttilio Rao 	while ((tick = fuse_aw_pop(data))) {
1675fe58019SAttilio Rao 		fuse_lck_mtx_lock(tick->tk_aw_mtx);
1685fe58019SAttilio Rao 		fticket_set_answered(tick);
1695fe58019SAttilio Rao 		tick->tk_aw_errno = ENOTCONN;
1705fe58019SAttilio Rao 		wakeup(tick);
1715fe58019SAttilio Rao 		fuse_lck_mtx_unlock(tick->tk_aw_mtx);
1725fe58019SAttilio Rao 		FUSE_ASSERT_AW_DONE(tick);
1735fe58019SAttilio Rao 		fuse_ticket_drop(tick);
1745fe58019SAttilio Rao 	}
1755fe58019SAttilio Rao 	fuse_lck_mtx_unlock(data->aw_mtx);
1765fe58019SAttilio Rao 	FUSE_UNLOCK();
1775fe58019SAttilio Rao 
178*cf169498SAlan Somers 	SDT_PROBE2(fuse, , device, trace, 1, "device close");
1795fe58019SAttilio Rao 	return (0);
1805fe58019SAttilio Rao }
1815fe58019SAttilio Rao 
1825fe58019SAttilio Rao int
1835fe58019SAttilio Rao fuse_device_poll(struct cdev *dev, int events, struct thread *td)
1845fe58019SAttilio Rao {
1855fe58019SAttilio Rao 	struct fuse_data *data;
1865fe58019SAttilio Rao 	int error, revents = 0;
1875fe58019SAttilio Rao 
1885fe58019SAttilio Rao 	error = devfs_get_cdevpriv((void **)&data);
1895fe58019SAttilio Rao 	if (error != 0)
1905fe58019SAttilio Rao 		return (events &
1915fe58019SAttilio Rao 		    (POLLHUP|POLLIN|POLLRDNORM|POLLOUT|POLLWRNORM));
1925fe58019SAttilio Rao 
1935fe58019SAttilio Rao 	if (events & (POLLIN | POLLRDNORM)) {
1945fe58019SAttilio Rao 		fuse_lck_mtx_lock(data->ms_mtx);
1955fe58019SAttilio Rao 		if (fdata_get_dead(data) || STAILQ_FIRST(&data->ms_head))
1965fe58019SAttilio Rao 			revents |= events & (POLLIN | POLLRDNORM);
1975fe58019SAttilio Rao 		else
1985fe58019SAttilio Rao 			selrecord(td, &data->ks_rsel);
1995fe58019SAttilio Rao 		fuse_lck_mtx_unlock(data->ms_mtx);
2005fe58019SAttilio Rao 	}
2015fe58019SAttilio Rao 	if (events & (POLLOUT | POLLWRNORM)) {
2025fe58019SAttilio Rao 		revents |= events & (POLLOUT | POLLWRNORM);
2035fe58019SAttilio Rao 	}
2045fe58019SAttilio Rao 	return (revents);
2055fe58019SAttilio Rao }
2065fe58019SAttilio Rao 
2075fe58019SAttilio Rao /*
2085fe58019SAttilio Rao  * fuse_device_read hangs on the queue of VFS messages.
2095fe58019SAttilio Rao  * When it's notified that there is a new one, it picks that and
2105fe58019SAttilio Rao  * passes up to the daemon
2115fe58019SAttilio Rao  */
2125fe58019SAttilio Rao int
2135fe58019SAttilio Rao fuse_device_read(struct cdev *dev, struct uio *uio, int ioflag)
2145fe58019SAttilio Rao {
2155fe58019SAttilio Rao 	int err;
2165fe58019SAttilio Rao 	struct fuse_data *data;
2175fe58019SAttilio Rao 	struct fuse_ticket *tick;
2185fe58019SAttilio Rao 	void *buf[] = {NULL, NULL, NULL};
2195fe58019SAttilio Rao 	int buflen[3];
2205fe58019SAttilio Rao 	int i;
2215fe58019SAttilio Rao 
222*cf169498SAlan Somers 	SDT_PROBE2(fuse, , device, trace, 1, "fuse device read");
2235fe58019SAttilio Rao 
2245fe58019SAttilio Rao 	err = devfs_get_cdevpriv((void **)&data);
2255fe58019SAttilio Rao 	if (err != 0)
2265fe58019SAttilio Rao 		return (err);
2275fe58019SAttilio Rao 
2285fe58019SAttilio Rao 	fuse_lck_mtx_lock(data->ms_mtx);
2295fe58019SAttilio Rao again:
2305fe58019SAttilio Rao 	if (fdata_get_dead(data)) {
231*cf169498SAlan Somers 		SDT_PROBE2(fuse, , device, trace, 2,
232*cf169498SAlan Somers 			"we know early on that reader should be kicked so we "
233*cf169498SAlan Somers 			"don't wait for news");
2345fe58019SAttilio Rao 		fuse_lck_mtx_unlock(data->ms_mtx);
2355fe58019SAttilio Rao 		return (ENODEV);
2365fe58019SAttilio Rao 	}
2375fe58019SAttilio Rao 	if (!(tick = fuse_ms_pop(data))) {
2385fe58019SAttilio Rao 		/* check if we may block */
2395fe58019SAttilio Rao 		if (ioflag & O_NONBLOCK) {
2405fe58019SAttilio Rao 			/* get outa here soon */
2415fe58019SAttilio Rao 			fuse_lck_mtx_unlock(data->ms_mtx);
2425fe58019SAttilio Rao 			return (EAGAIN);
2435fe58019SAttilio Rao 		} else {
2445fe58019SAttilio Rao 			err = msleep(data, &data->ms_mtx, PCATCH, "fu_msg", 0);
2455fe58019SAttilio Rao 			if (err != 0) {
2465fe58019SAttilio Rao 				fuse_lck_mtx_unlock(data->ms_mtx);
2475fe58019SAttilio Rao 				return (fdata_get_dead(data) ? ENODEV : err);
2485fe58019SAttilio Rao 			}
2495fe58019SAttilio Rao 			tick = fuse_ms_pop(data);
2505fe58019SAttilio Rao 		}
2515fe58019SAttilio Rao 	}
2525fe58019SAttilio Rao 	if (!tick) {
2535fe58019SAttilio Rao 		/*
2545fe58019SAttilio Rao 		 * We can get here if fuse daemon suddenly terminates,
2555fe58019SAttilio Rao 		 * eg, by being hit by a SIGKILL
2565fe58019SAttilio Rao 		 * -- and some other cases, too, tho not totally clear, when
2575fe58019SAttilio Rao 		 * (cv_signal/wakeup_one signals the whole process ?)
2585fe58019SAttilio Rao 		 */
259*cf169498SAlan Somers 		SDT_PROBE2(fuse, , device, trace, 1, "no message on thread");
2605fe58019SAttilio Rao 		goto again;
2615fe58019SAttilio Rao 	}
2625fe58019SAttilio Rao 	fuse_lck_mtx_unlock(data->ms_mtx);
2635fe58019SAttilio Rao 
2645fe58019SAttilio Rao 	if (fdata_get_dead(data)) {
2655fe58019SAttilio Rao 		/*
2665fe58019SAttilio Rao 		 * somebody somewhere -- eg., umount routine --
2675fe58019SAttilio Rao 		 * wants this liaison finished off
2685fe58019SAttilio Rao 		 */
269*cf169498SAlan Somers 		SDT_PROBE2(fuse, , device, trace, 2, "reader is to be sacked");
2705fe58019SAttilio Rao 		if (tick) {
271*cf169498SAlan Somers 			SDT_PROBE2(fuse, , device, trace, 2, "weird -- "
272*cf169498SAlan Somers 				"\"kick\" is set tho there is message");
2735fe58019SAttilio Rao 			FUSE_ASSERT_MS_DONE(tick);
2745fe58019SAttilio Rao 			fuse_ticket_drop(tick);
2755fe58019SAttilio Rao 		}
2765fe58019SAttilio Rao 		return (ENODEV);	/* This should make the daemon get off
2775fe58019SAttilio Rao 					 * of us */
2785fe58019SAttilio Rao 	}
279*cf169498SAlan Somers 	SDT_PROBE2(fuse, , device, trace, 1,
280*cf169498SAlan Somers 		"fuse device read message successfully");
2815fe58019SAttilio Rao 
2825fe58019SAttilio Rao 	KASSERT(tick->tk_ms_bufdata || tick->tk_ms_bufsize == 0,
2835fe58019SAttilio Rao 	    ("non-null buf pointer with positive size"));
2845fe58019SAttilio Rao 
2855fe58019SAttilio Rao 	switch (tick->tk_ms_type) {
2865fe58019SAttilio Rao 	case FT_M_FIOV:
2875fe58019SAttilio Rao 		buf[0] = tick->tk_ms_fiov.base;
2885fe58019SAttilio Rao 		buflen[0] = tick->tk_ms_fiov.len;
2895fe58019SAttilio Rao 		break;
2905fe58019SAttilio Rao 	case FT_M_BUF:
2915fe58019SAttilio Rao 		buf[0] = tick->tk_ms_fiov.base;
2925fe58019SAttilio Rao 		buflen[0] = tick->tk_ms_fiov.len;
2935fe58019SAttilio Rao 		buf[1] = tick->tk_ms_bufdata;
2945fe58019SAttilio Rao 		buflen[1] = tick->tk_ms_bufsize;
2955fe58019SAttilio Rao 		break;
2965fe58019SAttilio Rao 	default:
2975fe58019SAttilio Rao 		panic("unknown message type for fuse_ticket %p", tick);
2985fe58019SAttilio Rao 	}
2995fe58019SAttilio Rao 
3005fe58019SAttilio Rao 	for (i = 0; buf[i]; i++) {
3015fe58019SAttilio Rao 		/*
3025fe58019SAttilio Rao 		 * Why not ban mercilessly stupid daemons who can't keep up
3035fe58019SAttilio Rao 		 * with us? (There is no much use of a partial read here...)
3045fe58019SAttilio Rao 		 */
3055fe58019SAttilio Rao 		/*
3065fe58019SAttilio Rao 		 * XXX note that in such cases Linux FUSE throws EIO at the
3075fe58019SAttilio Rao 		 * syscall invoker and stands back to the message queue. The
3085fe58019SAttilio Rao 		 * rationale should be made clear (and possibly adopt that
3095fe58019SAttilio Rao 		 * behaviour). Keeping the current scheme at least makes
3105fe58019SAttilio Rao 		 * fallacy as loud as possible...
3115fe58019SAttilio Rao 		 */
3125fe58019SAttilio Rao 		if (uio->uio_resid < buflen[i]) {
3135fe58019SAttilio Rao 			fdata_set_dead(data);
314*cf169498SAlan Somers 			SDT_PROBE2(fuse, , device, trace, 2,
315*cf169498SAlan Somers 			    "daemon is stupid, kick it off...");
3165fe58019SAttilio Rao 			err = ENODEV;
3175fe58019SAttilio Rao 			break;
3185fe58019SAttilio Rao 		}
3195fe58019SAttilio Rao 		err = uiomove(buf[i], buflen[i], uio);
3205fe58019SAttilio Rao 		if (err)
3215fe58019SAttilio Rao 			break;
3225fe58019SAttilio Rao 	}
3235fe58019SAttilio Rao 
3245fe58019SAttilio Rao 	FUSE_ASSERT_MS_DONE(tick);
3255fe58019SAttilio Rao 	fuse_ticket_drop(tick);
3265fe58019SAttilio Rao 
3275fe58019SAttilio Rao 	return (err);
3285fe58019SAttilio Rao }
3295fe58019SAttilio Rao 
33002295cafSConrad Meyer static inline int
3315fe58019SAttilio Rao fuse_ohead_audit(struct fuse_out_header *ohead, struct uio *uio)
3325fe58019SAttilio Rao {
3335fe58019SAttilio Rao 	if (uio->uio_resid + sizeof(struct fuse_out_header) != ohead->len) {
334*cf169498SAlan Somers 		SDT_PROBE2(fuse, , device, trace, 1, "Format error: body size "
335*cf169498SAlan Somers 			"differs from size claimed by header");
3365fe58019SAttilio Rao 		return (EINVAL);
3375fe58019SAttilio Rao 	}
3385fe58019SAttilio Rao 	if (uio->uio_resid && ohead->error) {
339*cf169498SAlan Somers 		SDT_PROBE2(fuse, , device, trace, 1,
340*cf169498SAlan Somers 			"Format error: non zero error but message had a body");
3415fe58019SAttilio Rao 		return (EINVAL);
3425fe58019SAttilio Rao 	}
3435fe58019SAttilio Rao 	/* Sanitize the linuxism of negative errnos */
3445fe58019SAttilio Rao 	ohead->error = -(ohead->error);
3455fe58019SAttilio Rao 
3465fe58019SAttilio Rao 	return (0);
3475fe58019SAttilio Rao }
3485fe58019SAttilio Rao 
349*cf169498SAlan Somers SDT_PROBE_DEFINE1(fuse, , device, fuse_device_write_bumped_into_callback,
350*cf169498SAlan Somers 		"uint64_t");
3515fe58019SAttilio Rao /*
3525fe58019SAttilio Rao  * fuse_device_write first reads the header sent by the daemon.
3535fe58019SAttilio Rao  * If that's OK, looks up ticket/callback node by the unique id seen in header.
3545fe58019SAttilio Rao  * If the callback node contains a handler function, the uio is passed over
3555fe58019SAttilio Rao  * that.
3565fe58019SAttilio Rao  */
3575fe58019SAttilio Rao static int
3585fe58019SAttilio Rao fuse_device_write(struct cdev *dev, struct uio *uio, int ioflag)
3595fe58019SAttilio Rao {
3605fe58019SAttilio Rao 	struct fuse_out_header ohead;
3615fe58019SAttilio Rao 	int err = 0;
3625fe58019SAttilio Rao 	struct fuse_data *data;
3635fe58019SAttilio Rao 	struct fuse_ticket *tick, *x_tick;
3645fe58019SAttilio Rao 	int found = 0;
3655fe58019SAttilio Rao 
3665fe58019SAttilio Rao 	err = devfs_get_cdevpriv((void **)&data);
3675fe58019SAttilio Rao 	if (err != 0)
3685fe58019SAttilio Rao 		return (err);
3695fe58019SAttilio Rao 
3705fe58019SAttilio Rao 	if (uio->uio_resid < sizeof(struct fuse_out_header)) {
371*cf169498SAlan Somers 		SDT_PROBE2(fuse, , device, trace, 1,
372*cf169498SAlan Somers 			"fuse_device_write got less than a header!");
3735fe58019SAttilio Rao 		fdata_set_dead(data);
3745fe58019SAttilio Rao 		return (EINVAL);
3755fe58019SAttilio Rao 	}
3765fe58019SAttilio Rao 	if ((err = uiomove(&ohead, sizeof(struct fuse_out_header), uio)) != 0)
3775fe58019SAttilio Rao 		return (err);
3785fe58019SAttilio Rao 
3795fe58019SAttilio Rao 	/*
3805fe58019SAttilio Rao 	 * We check header information (which is redundant) and compare it
3815fe58019SAttilio Rao 	 * with what we see. If we see some inconsistency we discard the
3825fe58019SAttilio Rao 	 * whole answer and proceed on as if it had never existed. In
3835fe58019SAttilio Rao 	 * particular, no pretender will be woken up, regardless the
3845fe58019SAttilio Rao 	 * "unique" value in the header.
3855fe58019SAttilio Rao 	 */
3865fe58019SAttilio Rao 	if ((err = fuse_ohead_audit(&ohead, uio))) {
3875fe58019SAttilio Rao 		fdata_set_dead(data);
3885fe58019SAttilio Rao 		return (err);
3895fe58019SAttilio Rao 	}
3905fe58019SAttilio Rao 	/* Pass stuff over to callback if there is one installed */
3915fe58019SAttilio Rao 
3925fe58019SAttilio Rao 	/* Looking for ticket with the unique id of header */
3935fe58019SAttilio Rao 	fuse_lck_mtx_lock(data->aw_mtx);
3945fe58019SAttilio Rao 	TAILQ_FOREACH_SAFE(tick, &data->aw_head, tk_aw_link,
3955fe58019SAttilio Rao 	    x_tick) {
396*cf169498SAlan Somers 		SDT_PROBE1(fuse, , device,
397*cf169498SAlan Somers 			fuse_device_write_bumped_into_callback,
398*cf169498SAlan Somers 			tick->tk_unique);
3995fe58019SAttilio Rao 		if (tick->tk_unique == ohead.unique) {
4005fe58019SAttilio Rao 			found = 1;
4015fe58019SAttilio Rao 			fuse_aw_remove(tick);
4025fe58019SAttilio Rao 			break;
4035fe58019SAttilio Rao 		}
4045fe58019SAttilio Rao 	}
4055fe58019SAttilio Rao 	fuse_lck_mtx_unlock(data->aw_mtx);
4065fe58019SAttilio Rao 
4075fe58019SAttilio Rao 	if (found) {
4085fe58019SAttilio Rao 		if (tick->tk_aw_handler) {
4095fe58019SAttilio Rao 			/*
4105fe58019SAttilio Rao 			 * We found a callback with proper handler. In this
4115fe58019SAttilio Rao 			 * case the out header will be 0wnd by the callback,
4125fe58019SAttilio Rao 			 * so the fun of freeing that is left for her.
4135fe58019SAttilio Rao 			 * (Then, by all chance, she'll just get that's done
4145fe58019SAttilio Rao 			 * via ticket_drop(), so no manual mucking
4155fe58019SAttilio Rao 			 * around...)
4165fe58019SAttilio Rao 			 */
417*cf169498SAlan Somers 			SDT_PROBE2(fuse, , device, trace, 1,
418*cf169498SAlan Somers 				"pass ticket to a callback");
4195fe58019SAttilio Rao 			memcpy(&tick->tk_aw_ohead, &ohead, sizeof(ohead));
4205fe58019SAttilio Rao 			err = tick->tk_aw_handler(tick, uio);
4215fe58019SAttilio Rao 		} else {
4225fe58019SAttilio Rao 			/* pretender doesn't wanna do anything with answer */
423*cf169498SAlan Somers 			SDT_PROBE2(fuse, , device, trace, 1,
424*cf169498SAlan Somers 				"stuff devalidated, so we drop it");
4255fe58019SAttilio Rao 		}
4265fe58019SAttilio Rao 
4275fe58019SAttilio Rao 		/*
4285fe58019SAttilio Rao 		 * As aw_mtx was not held during the callback execution the
4295fe58019SAttilio Rao 		 * ticket may have been inserted again.  However, this is safe
4305fe58019SAttilio Rao 		 * because fuse_ticket_drop() will deal with refcount anyway.
4315fe58019SAttilio Rao 		 */
4325fe58019SAttilio Rao 		fuse_ticket_drop(tick);
4335fe58019SAttilio Rao 	} else {
4345fe58019SAttilio Rao 		/* no callback at all! */
435*cf169498SAlan Somers 		SDT_PROBE2(fuse, , device, trace, 1,
436*cf169498SAlan Somers 			"erhm, no handler for this response");
4375fe58019SAttilio Rao 		err = EINVAL;
4385fe58019SAttilio Rao 	}
4395fe58019SAttilio Rao 
4405fe58019SAttilio Rao 	return (err);
4415fe58019SAttilio Rao }
4425fe58019SAttilio Rao 
4435fe58019SAttilio Rao int
4445fe58019SAttilio Rao fuse_device_init(void)
4455fe58019SAttilio Rao {
4465fe58019SAttilio Rao 
4475fe58019SAttilio Rao 	fuse_dev = make_dev(&fuse_device_cdevsw, 0, UID_ROOT, GID_OPERATOR,
4485fe58019SAttilio Rao 	    S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP, "fuse");
4495fe58019SAttilio Rao 	if (fuse_dev == NULL)
4505fe58019SAttilio Rao 		return (ENOMEM);
4515fe58019SAttilio Rao 	return (0);
4525fe58019SAttilio Rao }
4535fe58019SAttilio Rao 
4545fe58019SAttilio Rao void
4555fe58019SAttilio Rao fuse_device_destroy(void)
4565fe58019SAttilio Rao {
4575fe58019SAttilio Rao 
4585fe58019SAttilio Rao 	MPASS(fuse_dev != NULL);
4595fe58019SAttilio Rao 	destroy_dev(fuse_dev);
4605fe58019SAttilio Rao }
461