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> 76cf169498SAlan 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" 84*c2d70d6eSAlan Somers #include "fuse_internal.h" 855fe58019SAttilio Rao #include "fuse_ipc.h" 865fe58019SAttilio Rao 87419e7ff6SAlan Somers SDT_PROVIDER_DECLARE(fusefs); 88cf169498SAlan Somers /* 89cf169498SAlan Somers * Fuse trace probe: 90cf169498SAlan Somers * arg0: verbosity. Higher numbers give more verbose messages 91cf169498SAlan Somers * arg1: Textual message 92cf169498SAlan Somers */ 93419e7ff6SAlan Somers SDT_PROBE_DEFINE2(fusefs, , device, trace, "int", "char*"); 945fe58019SAttilio Rao 955fe58019SAttilio Rao static struct cdev *fuse_dev; 965fe58019SAttilio Rao 973429092cSAlan Somers static d_kqfilter_t fuse_device_filter; 985fe58019SAttilio Rao static d_open_t fuse_device_open; 995fe58019SAttilio Rao static d_poll_t fuse_device_poll; 1005fe58019SAttilio Rao static d_read_t fuse_device_read; 1015fe58019SAttilio Rao static d_write_t fuse_device_write; 1025fe58019SAttilio Rao 1035fe58019SAttilio Rao static struct cdevsw fuse_device_cdevsw = { 1043429092cSAlan Somers .d_kqfilter = fuse_device_filter, 1055fe58019SAttilio Rao .d_open = fuse_device_open, 1065fe58019SAttilio Rao .d_name = "fuse", 1075fe58019SAttilio Rao .d_poll = fuse_device_poll, 1085fe58019SAttilio Rao .d_read = fuse_device_read, 1095fe58019SAttilio Rao .d_write = fuse_device_write, 1105fe58019SAttilio Rao .d_version = D_VERSION, 1115fe58019SAttilio Rao }; 1125fe58019SAttilio Rao 1133429092cSAlan Somers static int fuse_device_filt_read(struct knote *kn, long hint); 1143429092cSAlan Somers static void fuse_device_filt_detach(struct knote *kn); 1153429092cSAlan Somers 1163429092cSAlan Somers struct filterops fuse_device_rfiltops = { 1173429092cSAlan Somers .f_isfd = 1, 1183429092cSAlan Somers .f_detach = fuse_device_filt_detach, 1193429092cSAlan Somers .f_event = fuse_device_filt_read, 1203429092cSAlan Somers }; 1213429092cSAlan Somers 1225fe58019SAttilio Rao /**************************** 1235fe58019SAttilio Rao * 1245fe58019SAttilio Rao * >>> Fuse device op defs 1255fe58019SAttilio Rao * 1265fe58019SAttilio Rao ****************************/ 1275fe58019SAttilio Rao 1285fe58019SAttilio Rao static void 1295fe58019SAttilio Rao fdata_dtor(void *arg) 1305fe58019SAttilio Rao { 1315fe58019SAttilio Rao struct fuse_data *fdata; 1328b73a4c5SAlan Somers struct fuse_ticket *tick; 1335fe58019SAttilio Rao 1345fe58019SAttilio Rao fdata = arg; 1358b73a4c5SAlan Somers if (fdata == NULL) 1368b73a4c5SAlan Somers return; 1378b73a4c5SAlan Somers 1388b73a4c5SAlan Somers fdata_set_dead(fdata); 1398b73a4c5SAlan Somers 1408b73a4c5SAlan Somers FUSE_LOCK(); 1418b73a4c5SAlan Somers fuse_lck_mtx_lock(fdata->aw_mtx); 1428b73a4c5SAlan Somers /* wakup poll()ers */ 1438b73a4c5SAlan Somers selwakeuppri(&fdata->ks_rsel, PZERO + 1); 1448b73a4c5SAlan Somers /* Don't let syscall handlers wait in vain */ 1458b73a4c5SAlan Somers while ((tick = fuse_aw_pop(fdata))) { 1468b73a4c5SAlan Somers fuse_lck_mtx_lock(tick->tk_aw_mtx); 1478b73a4c5SAlan Somers fticket_set_answered(tick); 1488b73a4c5SAlan Somers tick->tk_aw_errno = ENOTCONN; 1498b73a4c5SAlan Somers wakeup(tick); 1508b73a4c5SAlan Somers fuse_lck_mtx_unlock(tick->tk_aw_mtx); 1518b73a4c5SAlan Somers FUSE_ASSERT_AW_DONE(tick); 1528b73a4c5SAlan Somers fuse_ticket_drop(tick); 1538b73a4c5SAlan Somers } 1548b73a4c5SAlan Somers fuse_lck_mtx_unlock(fdata->aw_mtx); 1558b73a4c5SAlan Somers FUSE_UNLOCK(); 1568b73a4c5SAlan Somers 1575fe58019SAttilio Rao fdata_trydestroy(fdata); 1585fe58019SAttilio Rao } 1595fe58019SAttilio Rao 1603429092cSAlan Somers static int 1613429092cSAlan Somers fuse_device_filter(struct cdev *dev, struct knote *kn) 1623429092cSAlan Somers { 1633429092cSAlan Somers struct fuse_data *data; 1643429092cSAlan Somers int error; 1653429092cSAlan Somers 1663429092cSAlan Somers error = devfs_get_cdevpriv((void **)&data); 1673429092cSAlan Somers 1683429092cSAlan Somers /* EVFILT_WRITE is not supported; the device is always ready to write */ 1693429092cSAlan Somers if (error == 0 && kn->kn_filter == EVFILT_READ) { 1703429092cSAlan Somers kn->kn_fop = &fuse_device_rfiltops; 1713429092cSAlan Somers kn->kn_hook = data; 1723429092cSAlan Somers knlist_add(&data->ks_rsel.si_note, kn, 0); 1733429092cSAlan Somers error = 0; 1743429092cSAlan Somers } else if (error == 0) { 1753429092cSAlan Somers error = EINVAL; 1763429092cSAlan Somers kn->kn_data = error; 1773429092cSAlan Somers } 1783429092cSAlan Somers 1793429092cSAlan Somers return (error); 1803429092cSAlan Somers } 1813429092cSAlan Somers 1823429092cSAlan Somers static void 1833429092cSAlan Somers fuse_device_filt_detach(struct knote *kn) 1843429092cSAlan Somers { 1853429092cSAlan Somers struct fuse_data *data; 1863429092cSAlan Somers 1873429092cSAlan Somers data = (struct fuse_data*)kn->kn_hook; 1883429092cSAlan Somers MPASS(data != NULL); 1893429092cSAlan Somers knlist_remove(&data->ks_rsel.si_note, kn, 0); 1903429092cSAlan Somers kn->kn_hook = NULL; 1913429092cSAlan Somers } 1923429092cSAlan Somers 1933429092cSAlan Somers static int 1943429092cSAlan Somers fuse_device_filt_read(struct knote *kn, long hint) 1953429092cSAlan Somers { 1963429092cSAlan Somers struct fuse_data *data; 1973429092cSAlan Somers int ready; 1983429092cSAlan Somers 1993429092cSAlan Somers data = (struct fuse_data*)kn->kn_hook; 2003429092cSAlan Somers MPASS(data != NULL); 2013429092cSAlan Somers 2023429092cSAlan Somers mtx_assert(&data->ms_mtx, MA_OWNED); 2033429092cSAlan Somers if (fdata_get_dead(data)) { 2043429092cSAlan Somers kn->kn_flags |= EV_EOF; 2053429092cSAlan Somers kn->kn_fflags = ENODEV; 2063429092cSAlan Somers kn->kn_data = 1; 2073429092cSAlan Somers ready = 1; 2083429092cSAlan Somers } else if (STAILQ_FIRST(&data->ms_head)) { 2090a7c63e0SAlan Somers MPASS(data->ms_count >= 1); 2100a7c63e0SAlan Somers kn->kn_data = data->ms_count; 2113429092cSAlan Somers ready = 1; 2123429092cSAlan Somers } else { 2133429092cSAlan Somers ready = 0; 2143429092cSAlan Somers } 2153429092cSAlan Somers 2163429092cSAlan Somers return (ready); 2173429092cSAlan Somers } 2183429092cSAlan Somers 2195fe58019SAttilio Rao /* 2205fe58019SAttilio Rao * Resources are set up on a per-open basis 2215fe58019SAttilio Rao */ 2225fe58019SAttilio Rao static int 2235fe58019SAttilio Rao fuse_device_open(struct cdev *dev, int oflags, int devtype, struct thread *td) 2245fe58019SAttilio Rao { 2255fe58019SAttilio Rao struct fuse_data *fdata; 2265fe58019SAttilio Rao int error; 2275fe58019SAttilio Rao 228419e7ff6SAlan Somers SDT_PROBE2(fusefs, , device, trace, 1, "device open"); 2295fe58019SAttilio Rao 2305fe58019SAttilio Rao fdata = fdata_alloc(dev, td->td_ucred); 2315fe58019SAttilio Rao error = devfs_set_cdevpriv(fdata, fdata_dtor); 2325fe58019SAttilio Rao if (error != 0) 2335fe58019SAttilio Rao fdata_trydestroy(fdata); 2345fe58019SAttilio Rao else 235419e7ff6SAlan Somers SDT_PROBE2(fusefs, , device, trace, 1, "device open success"); 2365fe58019SAttilio Rao return (error); 2375fe58019SAttilio Rao } 2385fe58019SAttilio Rao 2395fe58019SAttilio Rao int 2405fe58019SAttilio Rao fuse_device_poll(struct cdev *dev, int events, struct thread *td) 2415fe58019SAttilio Rao { 2425fe58019SAttilio Rao struct fuse_data *data; 2435fe58019SAttilio Rao int error, revents = 0; 2445fe58019SAttilio Rao 2455fe58019SAttilio Rao error = devfs_get_cdevpriv((void **)&data); 2465fe58019SAttilio Rao if (error != 0) 2475fe58019SAttilio Rao return (events & 2485fe58019SAttilio Rao (POLLHUP|POLLIN|POLLRDNORM|POLLOUT|POLLWRNORM)); 2495fe58019SAttilio Rao 2505fe58019SAttilio Rao if (events & (POLLIN | POLLRDNORM)) { 2515fe58019SAttilio Rao fuse_lck_mtx_lock(data->ms_mtx); 2525fe58019SAttilio Rao if (fdata_get_dead(data) || STAILQ_FIRST(&data->ms_head)) 2535fe58019SAttilio Rao revents |= events & (POLLIN | POLLRDNORM); 2545fe58019SAttilio Rao else 2555fe58019SAttilio Rao selrecord(td, &data->ks_rsel); 2565fe58019SAttilio Rao fuse_lck_mtx_unlock(data->ms_mtx); 2575fe58019SAttilio Rao } 2585fe58019SAttilio Rao if (events & (POLLOUT | POLLWRNORM)) { 2595fe58019SAttilio Rao revents |= events & (POLLOUT | POLLWRNORM); 2605fe58019SAttilio Rao } 2615fe58019SAttilio Rao return (revents); 2625fe58019SAttilio Rao } 2635fe58019SAttilio Rao 2645fe58019SAttilio Rao /* 2655fe58019SAttilio Rao * fuse_device_read hangs on the queue of VFS messages. 2665fe58019SAttilio Rao * When it's notified that there is a new one, it picks that and 2675fe58019SAttilio Rao * passes up to the daemon 2685fe58019SAttilio Rao */ 2695fe58019SAttilio Rao int 2705fe58019SAttilio Rao fuse_device_read(struct cdev *dev, struct uio *uio, int ioflag) 2715fe58019SAttilio Rao { 2725fe58019SAttilio Rao int err; 2735fe58019SAttilio Rao struct fuse_data *data; 2745fe58019SAttilio Rao struct fuse_ticket *tick; 2755fe58019SAttilio Rao void *buf[] = {NULL, NULL, NULL}; 2765fe58019SAttilio Rao int buflen[3]; 2775fe58019SAttilio Rao int i; 2785fe58019SAttilio Rao 279419e7ff6SAlan Somers SDT_PROBE2(fusefs, , device, trace, 1, "fuse device read"); 2805fe58019SAttilio Rao 2815fe58019SAttilio Rao err = devfs_get_cdevpriv((void **)&data); 2825fe58019SAttilio Rao if (err != 0) 2835fe58019SAttilio Rao return (err); 2845fe58019SAttilio Rao 2855fe58019SAttilio Rao fuse_lck_mtx_lock(data->ms_mtx); 2865fe58019SAttilio Rao again: 2875fe58019SAttilio Rao if (fdata_get_dead(data)) { 288419e7ff6SAlan Somers SDT_PROBE2(fusefs, , device, trace, 2, 289cf169498SAlan Somers "we know early on that reader should be kicked so we " 290cf169498SAlan Somers "don't wait for news"); 2915fe58019SAttilio Rao fuse_lck_mtx_unlock(data->ms_mtx); 2925fe58019SAttilio Rao return (ENODEV); 2935fe58019SAttilio Rao } 2945fe58019SAttilio Rao if (!(tick = fuse_ms_pop(data))) { 2955fe58019SAttilio Rao /* check if we may block */ 2965fe58019SAttilio Rao if (ioflag & O_NONBLOCK) { 2975fe58019SAttilio Rao /* get outa here soon */ 2985fe58019SAttilio Rao fuse_lck_mtx_unlock(data->ms_mtx); 2995fe58019SAttilio Rao return (EAGAIN); 3005fe58019SAttilio Rao } else { 3015fe58019SAttilio Rao err = msleep(data, &data->ms_mtx, PCATCH, "fu_msg", 0); 3025fe58019SAttilio Rao if (err != 0) { 3035fe58019SAttilio Rao fuse_lck_mtx_unlock(data->ms_mtx); 3045fe58019SAttilio Rao return (fdata_get_dead(data) ? ENODEV : err); 3055fe58019SAttilio Rao } 3065fe58019SAttilio Rao tick = fuse_ms_pop(data); 3075fe58019SAttilio Rao } 3085fe58019SAttilio Rao } 3095fe58019SAttilio Rao if (!tick) { 3105fe58019SAttilio Rao /* 3115fe58019SAttilio Rao * We can get here if fuse daemon suddenly terminates, 3125fe58019SAttilio Rao * eg, by being hit by a SIGKILL 3135fe58019SAttilio Rao * -- and some other cases, too, tho not totally clear, when 3145fe58019SAttilio Rao * (cv_signal/wakeup_one signals the whole process ?) 3155fe58019SAttilio Rao */ 316419e7ff6SAlan Somers SDT_PROBE2(fusefs, , device, trace, 1, "no message on thread"); 3175fe58019SAttilio Rao goto again; 3185fe58019SAttilio Rao } 3195fe58019SAttilio Rao fuse_lck_mtx_unlock(data->ms_mtx); 3205fe58019SAttilio Rao 3215fe58019SAttilio Rao if (fdata_get_dead(data)) { 3225fe58019SAttilio Rao /* 3235fe58019SAttilio Rao * somebody somewhere -- eg., umount routine -- 3245fe58019SAttilio Rao * wants this liaison finished off 3255fe58019SAttilio Rao */ 326419e7ff6SAlan Somers SDT_PROBE2(fusefs, , device, trace, 2, 327419e7ff6SAlan Somers "reader is to be sacked"); 3285fe58019SAttilio Rao if (tick) { 329419e7ff6SAlan Somers SDT_PROBE2(fusefs, , device, trace, 2, "weird -- " 330cf169498SAlan Somers "\"kick\" is set tho there is message"); 3315fe58019SAttilio Rao FUSE_ASSERT_MS_DONE(tick); 3325fe58019SAttilio Rao fuse_ticket_drop(tick); 3335fe58019SAttilio Rao } 3345fe58019SAttilio Rao return (ENODEV); /* This should make the daemon get off 3355fe58019SAttilio Rao * of us */ 3365fe58019SAttilio Rao } 337419e7ff6SAlan Somers SDT_PROBE2(fusefs, , device, trace, 1, 338cf169498SAlan Somers "fuse device read message successfully"); 3395fe58019SAttilio Rao 3405fe58019SAttilio Rao KASSERT(tick->tk_ms_bufdata || tick->tk_ms_bufsize == 0, 3415fe58019SAttilio Rao ("non-null buf pointer with positive size")); 3425fe58019SAttilio Rao 3435fe58019SAttilio Rao switch (tick->tk_ms_type) { 3445fe58019SAttilio Rao case FT_M_FIOV: 3455fe58019SAttilio Rao buf[0] = tick->tk_ms_fiov.base; 3465fe58019SAttilio Rao buflen[0] = tick->tk_ms_fiov.len; 3475fe58019SAttilio Rao break; 3485fe58019SAttilio Rao case FT_M_BUF: 3495fe58019SAttilio Rao buf[0] = tick->tk_ms_fiov.base; 3505fe58019SAttilio Rao buflen[0] = tick->tk_ms_fiov.len; 3515fe58019SAttilio Rao buf[1] = tick->tk_ms_bufdata; 3525fe58019SAttilio Rao buflen[1] = tick->tk_ms_bufsize; 3535fe58019SAttilio Rao break; 3545fe58019SAttilio Rao default: 3555fe58019SAttilio Rao panic("unknown message type for fuse_ticket %p", tick); 3565fe58019SAttilio Rao } 3575fe58019SAttilio Rao 3585fe58019SAttilio Rao for (i = 0; buf[i]; i++) { 3595fe58019SAttilio Rao /* 3605fe58019SAttilio Rao * Why not ban mercilessly stupid daemons who can't keep up 3615fe58019SAttilio Rao * with us? (There is no much use of a partial read here...) 3625fe58019SAttilio Rao */ 3635fe58019SAttilio Rao /* 3645fe58019SAttilio Rao * XXX note that in such cases Linux FUSE throws EIO at the 3655fe58019SAttilio Rao * syscall invoker and stands back to the message queue. The 3665fe58019SAttilio Rao * rationale should be made clear (and possibly adopt that 3675fe58019SAttilio Rao * behaviour). Keeping the current scheme at least makes 3685fe58019SAttilio Rao * fallacy as loud as possible... 3695fe58019SAttilio Rao */ 3705fe58019SAttilio Rao if (uio->uio_resid < buflen[i]) { 3715fe58019SAttilio Rao fdata_set_dead(data); 372419e7ff6SAlan Somers SDT_PROBE2(fusefs, , device, trace, 2, 373cf169498SAlan Somers "daemon is stupid, kick it off..."); 3745fe58019SAttilio Rao err = ENODEV; 3755fe58019SAttilio Rao break; 3765fe58019SAttilio Rao } 3775fe58019SAttilio Rao err = uiomove(buf[i], buflen[i], uio); 3785fe58019SAttilio Rao if (err) 3795fe58019SAttilio Rao break; 3805fe58019SAttilio Rao } 3815fe58019SAttilio Rao 3825fe58019SAttilio Rao FUSE_ASSERT_MS_DONE(tick); 3835fe58019SAttilio Rao fuse_ticket_drop(tick); 3845fe58019SAttilio Rao 3855fe58019SAttilio Rao return (err); 3865fe58019SAttilio Rao } 3875fe58019SAttilio Rao 38802295cafSConrad Meyer static inline int 3895fe58019SAttilio Rao fuse_ohead_audit(struct fuse_out_header *ohead, struct uio *uio) 3905fe58019SAttilio Rao { 3915fe58019SAttilio Rao if (uio->uio_resid + sizeof(struct fuse_out_header) != ohead->len) { 392419e7ff6SAlan Somers SDT_PROBE2(fusefs, , device, trace, 1, 393419e7ff6SAlan Somers "Format error: body size " 394cf169498SAlan Somers "differs from size claimed by header"); 3955fe58019SAttilio Rao return (EINVAL); 3965fe58019SAttilio Rao } 397*c2d70d6eSAlan Somers if (uio->uio_resid && ohead->unique != 0 && ohead->error) { 398419e7ff6SAlan Somers SDT_PROBE2(fusefs, , device, trace, 1, 399cf169498SAlan Somers "Format error: non zero error but message had a body"); 4005fe58019SAttilio Rao return (EINVAL); 4015fe58019SAttilio Rao } 4025fe58019SAttilio Rao 4035fe58019SAttilio Rao return (0); 4045fe58019SAttilio Rao } 4055fe58019SAttilio Rao 406*c2d70d6eSAlan Somers SDT_PROBE_DEFINE1(fusefs, , device, fuse_device_write_notify, 407*c2d70d6eSAlan Somers "struct fuse_out_header*"); 408419e7ff6SAlan Somers SDT_PROBE_DEFINE1(fusefs, , device, fuse_device_write_missing_ticket, 409419e7ff6SAlan Somers "uint64_t"); 410419e7ff6SAlan Somers SDT_PROBE_DEFINE1(fusefs, , device, fuse_device_write_found, 411419e7ff6SAlan Somers "struct fuse_ticket*"); 4125fe58019SAttilio Rao /* 4135fe58019SAttilio Rao * fuse_device_write first reads the header sent by the daemon. 4145fe58019SAttilio Rao * If that's OK, looks up ticket/callback node by the unique id seen in header. 4155fe58019SAttilio Rao * If the callback node contains a handler function, the uio is passed over 4165fe58019SAttilio Rao * that. 4175fe58019SAttilio Rao */ 4185fe58019SAttilio Rao static int 4195fe58019SAttilio Rao fuse_device_write(struct cdev *dev, struct uio *uio, int ioflag) 4205fe58019SAttilio Rao { 4215fe58019SAttilio Rao struct fuse_out_header ohead; 4225fe58019SAttilio Rao int err = 0; 4235fe58019SAttilio Rao struct fuse_data *data; 424*c2d70d6eSAlan Somers struct mount *mp; 425a1542146SAlan Somers struct fuse_ticket *tick, *itick, *x_tick; 4265fe58019SAttilio Rao int found = 0; 4275fe58019SAttilio Rao 4285fe58019SAttilio Rao err = devfs_get_cdevpriv((void **)&data); 4295fe58019SAttilio Rao if (err != 0) 4305fe58019SAttilio Rao return (err); 431*c2d70d6eSAlan Somers mp = data->mp; 4325fe58019SAttilio Rao 4335fe58019SAttilio Rao if (uio->uio_resid < sizeof(struct fuse_out_header)) { 434419e7ff6SAlan Somers SDT_PROBE2(fusefs, , device, trace, 1, 435cf169498SAlan Somers "fuse_device_write got less than a header!"); 4365fe58019SAttilio Rao fdata_set_dead(data); 4375fe58019SAttilio Rao return (EINVAL); 4385fe58019SAttilio Rao } 4395fe58019SAttilio Rao if ((err = uiomove(&ohead, sizeof(struct fuse_out_header), uio)) != 0) 4405fe58019SAttilio Rao return (err); 4415fe58019SAttilio Rao 4425fe58019SAttilio Rao /* 4435fe58019SAttilio Rao * We check header information (which is redundant) and compare it 4445fe58019SAttilio Rao * with what we see. If we see some inconsistency we discard the 4455fe58019SAttilio Rao * whole answer and proceed on as if it had never existed. In 4465fe58019SAttilio Rao * particular, no pretender will be woken up, regardless the 4475fe58019SAttilio Rao * "unique" value in the header. 4485fe58019SAttilio Rao */ 4495fe58019SAttilio Rao if ((err = fuse_ohead_audit(&ohead, uio))) { 4505fe58019SAttilio Rao fdata_set_dead(data); 4515fe58019SAttilio Rao return (err); 4525fe58019SAttilio Rao } 4535fe58019SAttilio Rao /* Pass stuff over to callback if there is one installed */ 4545fe58019SAttilio Rao 4555fe58019SAttilio Rao /* Looking for ticket with the unique id of header */ 4565fe58019SAttilio Rao fuse_lck_mtx_lock(data->aw_mtx); 4575fe58019SAttilio Rao TAILQ_FOREACH_SAFE(tick, &data->aw_head, tk_aw_link, 4585fe58019SAttilio Rao x_tick) { 4595fe58019SAttilio Rao if (tick->tk_unique == ohead.unique) { 460419e7ff6SAlan Somers SDT_PROBE1(fusefs, , device, fuse_device_write_found, 461723c7768SAlan Somers tick); 4625fe58019SAttilio Rao found = 1; 4635fe58019SAttilio Rao fuse_aw_remove(tick); 4645fe58019SAttilio Rao break; 4655fe58019SAttilio Rao } 4665fe58019SAttilio Rao } 467a1542146SAlan Somers if (found && tick->irq_unique > 0) { 468a1542146SAlan Somers /* 469a1542146SAlan Somers * Discard the FUSE_INTERRUPT ticket that tried to interrupt 470a1542146SAlan Somers * this operation 471a1542146SAlan Somers */ 472a1542146SAlan Somers TAILQ_FOREACH_SAFE(itick, &data->aw_head, tk_aw_link, 473a1542146SAlan Somers x_tick) { 474a1542146SAlan Somers if (itick->tk_unique == tick->irq_unique) { 475a1542146SAlan Somers fuse_aw_remove(itick); 476a1542146SAlan Somers break; 477a1542146SAlan Somers } 478a1542146SAlan Somers } 479a1542146SAlan Somers tick->irq_unique = 0; 480a1542146SAlan Somers } 4815fe58019SAttilio Rao fuse_lck_mtx_unlock(data->aw_mtx); 4825fe58019SAttilio Rao 4835fe58019SAttilio Rao if (found) { 4845fe58019SAttilio Rao if (tick->tk_aw_handler) { 4855fe58019SAttilio Rao /* 4865fe58019SAttilio Rao * We found a callback with proper handler. In this 4875fe58019SAttilio Rao * case the out header will be 0wnd by the callback, 4885fe58019SAttilio Rao * so the fun of freeing that is left for her. 4895fe58019SAttilio Rao * (Then, by all chance, she'll just get that's done 4905fe58019SAttilio Rao * via ticket_drop(), so no manual mucking 4915fe58019SAttilio Rao * around...) 4925fe58019SAttilio Rao */ 493419e7ff6SAlan Somers SDT_PROBE2(fusefs, , device, trace, 1, 494cf169498SAlan Somers "pass ticket to a callback"); 495*c2d70d6eSAlan Somers /* Sanitize the linuxism of negative errnos */ 496*c2d70d6eSAlan Somers ohead.error *= -1; 4975fe58019SAttilio Rao memcpy(&tick->tk_aw_ohead, &ohead, sizeof(ohead)); 4985fe58019SAttilio Rao err = tick->tk_aw_handler(tick, uio); 4995fe58019SAttilio Rao } else { 5005fe58019SAttilio Rao /* pretender doesn't wanna do anything with answer */ 501419e7ff6SAlan Somers SDT_PROBE2(fusefs, , device, trace, 1, 502cf169498SAlan Somers "stuff devalidated, so we drop it"); 5035fe58019SAttilio Rao } 5045fe58019SAttilio Rao 5055fe58019SAttilio Rao /* 5065fe58019SAttilio Rao * As aw_mtx was not held during the callback execution the 5075fe58019SAttilio Rao * ticket may have been inserted again. However, this is safe 5085fe58019SAttilio Rao * because fuse_ticket_drop() will deal with refcount anyway. 5095fe58019SAttilio Rao */ 5105fe58019SAttilio Rao fuse_ticket_drop(tick); 511*c2d70d6eSAlan Somers } else if (ohead.unique == 0){ 512*c2d70d6eSAlan Somers /* unique == 0 means asynchronous notification */ 513*c2d70d6eSAlan Somers SDT_PROBE1(fusefs, , device, fuse_device_write_notify, &ohead); 514*c2d70d6eSAlan Somers switch (ohead.error) { 515*c2d70d6eSAlan Somers case FUSE_NOTIFY_INVAL_ENTRY: 516*c2d70d6eSAlan Somers err = fuse_internal_invalidate_entry(mp, uio); 517*c2d70d6eSAlan Somers break; 518*c2d70d6eSAlan Somers case FUSE_NOTIFY_POLL: 519*c2d70d6eSAlan Somers case FUSE_NOTIFY_INVAL_INODE: 520*c2d70d6eSAlan Somers default: 521*c2d70d6eSAlan Somers /* Not implemented */ 522*c2d70d6eSAlan Somers err = ENOSYS; 523*c2d70d6eSAlan Somers } 5245fe58019SAttilio Rao } else { 5255fe58019SAttilio Rao /* no callback at all! */ 526419e7ff6SAlan Somers SDT_PROBE1(fusefs, , device, fuse_device_write_missing_ticket, 527723c7768SAlan Somers ohead.unique); 528*c2d70d6eSAlan Somers if (ohead.error == -EAGAIN) { 529a1542146SAlan Somers /* 530a1542146SAlan Somers * This was probably a response to a FUSE_INTERRUPT 531a1542146SAlan Somers * operation whose original operation is already 532a1542146SAlan Somers * complete. We can't store FUSE_INTERRUPT tickets 533a1542146SAlan Somers * indefinitely because their responses are optional. 534a1542146SAlan Somers * So we delete them when the original operation 535a1542146SAlan Somers * completes. And sadly the fuse_header_out doesn't 536a1542146SAlan Somers * identify the opcode, so we have to guess. 537a1542146SAlan Somers */ 538a1542146SAlan Somers err = 0; 539a1542146SAlan Somers } else { 5405fe58019SAttilio Rao err = EINVAL; 5415fe58019SAttilio Rao } 542a1542146SAlan Somers } 5435fe58019SAttilio Rao 5445fe58019SAttilio Rao return (err); 5455fe58019SAttilio Rao } 5465fe58019SAttilio Rao 5475fe58019SAttilio Rao int 5485fe58019SAttilio Rao fuse_device_init(void) 5495fe58019SAttilio Rao { 5505fe58019SAttilio Rao 5515fe58019SAttilio Rao fuse_dev = make_dev(&fuse_device_cdevsw, 0, UID_ROOT, GID_OPERATOR, 552b4227f34SAlan Somers S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH, "fuse"); 5535fe58019SAttilio Rao if (fuse_dev == NULL) 5545fe58019SAttilio Rao return (ENOMEM); 5555fe58019SAttilio Rao return (0); 5565fe58019SAttilio Rao } 5575fe58019SAttilio Rao 5585fe58019SAttilio Rao void 5595fe58019SAttilio Rao fuse_device_destroy(void) 5605fe58019SAttilio Rao { 5615fe58019SAttilio Rao 5625fe58019SAttilio Rao MPASS(fuse_dev != NULL); 5635fe58019SAttilio Rao destroy_dev(fuse_dev); 5645fe58019SAttilio Rao } 565