151369649SPedro F. Giffuni /*- 251369649SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 351369649SPedro F. Giffuni * 45fe58019SAttilio Rao * Copyright (c) 2007-2009 Google Inc. and Amit Singh 55fe58019SAttilio Rao * All rights reserved. 65fe58019SAttilio Rao * 75fe58019SAttilio Rao * Redistribution and use in source and binary forms, with or without 85fe58019SAttilio Rao * modification, are permitted provided that the following conditions are 95fe58019SAttilio Rao * met: 105fe58019SAttilio Rao * 115fe58019SAttilio Rao * * Redistributions of source code must retain the above copyright 125fe58019SAttilio Rao * notice, this list of conditions and the following disclaimer. 135fe58019SAttilio Rao * * Redistributions in binary form must reproduce the above 145fe58019SAttilio Rao * copyright notice, this list of conditions and the following disclaimer 155fe58019SAttilio Rao * in the documentation and/or other materials provided with the 165fe58019SAttilio Rao * distribution. 175fe58019SAttilio Rao * * Neither the name of Google Inc. nor the names of its 185fe58019SAttilio Rao * contributors may be used to endorse or promote products derived from 195fe58019SAttilio Rao * this software without specific prior written permission. 205fe58019SAttilio Rao * 215fe58019SAttilio Rao * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 225fe58019SAttilio Rao * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 235fe58019SAttilio Rao * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 245fe58019SAttilio Rao * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 255fe58019SAttilio Rao * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 265fe58019SAttilio Rao * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 275fe58019SAttilio Rao * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 285fe58019SAttilio Rao * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 295fe58019SAttilio Rao * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 305fe58019SAttilio Rao * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 315fe58019SAttilio Rao * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 325fe58019SAttilio Rao * 335fe58019SAttilio Rao * Copyright (C) 2005 Csaba Henk. 345fe58019SAttilio Rao * All rights reserved. 355fe58019SAttilio Rao * 368aafc8c3SAlan Somers * Copyright (c) 2019 The FreeBSD Foundation 378aafc8c3SAlan Somers * 388aafc8c3SAlan Somers * Portions of this software were developed by BFF Storage Systems, LLC under 398aafc8c3SAlan Somers * sponsorship from the FreeBSD Foundation. 408aafc8c3SAlan Somers * 415fe58019SAttilio Rao * Redistribution and use in source and binary forms, with or without 425fe58019SAttilio Rao * modification, are permitted provided that the following conditions 435fe58019SAttilio Rao * are met: 445fe58019SAttilio Rao * 1. Redistributions of source code must retain the above copyright 455fe58019SAttilio Rao * notice, this list of conditions and the following disclaimer. 465fe58019SAttilio Rao * 2. Redistributions in binary form must reproduce the above copyright 475fe58019SAttilio Rao * notice, this list of conditions and the following disclaimer in the 485fe58019SAttilio Rao * documentation and/or other materials provided with the distribution. 495fe58019SAttilio Rao * 505fe58019SAttilio Rao * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND 515fe58019SAttilio Rao * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 525fe58019SAttilio Rao * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 535fe58019SAttilio Rao * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE 545fe58019SAttilio Rao * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 555fe58019SAttilio Rao * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 565fe58019SAttilio Rao * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 575fe58019SAttilio Rao * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 585fe58019SAttilio Rao * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 595fe58019SAttilio Rao * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 605fe58019SAttilio Rao * SUCH DAMAGE. 615fe58019SAttilio Rao */ 625fe58019SAttilio Rao 63cf169498SAlan Somers #include <sys/param.h> 645fe58019SAttilio Rao #include <sys/module.h> 655fe58019SAttilio Rao #include <sys/systm.h> 66560a55d0SAlan Somers #include <sys/counter.h> 675fe58019SAttilio Rao #include <sys/errno.h> 685fe58019SAttilio Rao #include <sys/kernel.h> 695fe58019SAttilio Rao #include <sys/conf.h> 705fe58019SAttilio Rao #include <sys/uio.h> 715fe58019SAttilio Rao #include <sys/malloc.h> 725fe58019SAttilio Rao #include <sys/queue.h> 735fe58019SAttilio Rao #include <sys/lock.h> 745fe58019SAttilio Rao #include <sys/sx.h> 755fe58019SAttilio Rao #include <sys/mutex.h> 765fe58019SAttilio Rao #include <sys/proc.h> 775fe58019SAttilio Rao #include <sys/mount.h> 78cf169498SAlan Somers #include <sys/sdt.h> 795fe58019SAttilio Rao #include <sys/vnode.h> 805fe58019SAttilio Rao #include <sys/signalvar.h> 815fe58019SAttilio Rao #include <sys/syscallsubr.h> 825fe58019SAttilio Rao #include <sys/sysctl.h> 835fe58019SAttilio Rao #include <vm/uma.h> 845fe58019SAttilio Rao 855fe58019SAttilio Rao #include "fuse.h" 865fe58019SAttilio Rao #include "fuse_node.h" 875fe58019SAttilio Rao #include "fuse_ipc.h" 885fe58019SAttilio Rao #include "fuse_internal.h" 895fe58019SAttilio Rao 90419e7ff6SAlan Somers SDT_PROVIDER_DECLARE(fusefs); 91cf169498SAlan Somers /* 92cf169498SAlan Somers * Fuse trace probe: 93cf169498SAlan Somers * arg0: verbosity. Higher numbers give more verbose messages 94cf169498SAlan Somers * arg1: Textual message 95cf169498SAlan Somers */ 96419e7ff6SAlan Somers SDT_PROBE_DEFINE2(fusefs, , ipc, trace, "int", "char*"); 975fe58019SAttilio Rao 98723c7768SAlan Somers static void fdisp_make_pid(struct fuse_dispatcher *fdip, enum fuse_opcode op, 99723c7768SAlan Somers struct fuse_data *data, uint64_t nid, pid_t pid, struct ucred *cred); 1003d070fdcSAlan Somers static void fuse_interrupt_send(struct fuse_ticket *otick, int err); 1015fe58019SAttilio Rao static struct fuse_ticket *fticket_alloc(struct fuse_data *data); 1025fe58019SAttilio Rao static void fticket_refresh(struct fuse_ticket *ftick); 1035403f2c1SAlan Somers static inline void fticket_reset(struct fuse_ticket *ftick); 1045fe58019SAttilio Rao static void fticket_destroy(struct fuse_ticket *ftick); 1055fe58019SAttilio Rao static int fticket_wait_answer(struct fuse_ticket *ftick); 10602295cafSConrad Meyer static inline int 1075fe58019SAttilio Rao fticket_aw_pull_uio(struct fuse_ticket *ftick, 1085fe58019SAttilio Rao struct uio *uio); 1095fe58019SAttilio Rao 1105fe58019SAttilio Rao static int fuse_body_audit(struct fuse_ticket *ftick, size_t blen); 1115fe58019SAttilio Rao 1125fe58019SAttilio Rao static fuse_handler_t fuse_standard_handler; 1135fe58019SAttilio Rao 114560a55d0SAlan Somers static counter_u64_t fuse_ticket_count; 115560a55d0SAlan Somers SYSCTL_COUNTER_U64(_vfs_fusefs_stats, OID_AUTO, ticket_count, CTLFLAG_RD, 116560a55d0SAlan Somers &fuse_ticket_count, "Number of allocated tickets"); 1175fe58019SAttilio Rao 1185fe58019SAttilio Rao static long fuse_iov_permanent_bufsize = 1 << 19; 1195fe58019SAttilio Rao 120123af6ecSAlan Somers SYSCTL_LONG(_vfs_fusefs, OID_AUTO, iov_permanent_bufsize, CTLFLAG_RW, 1215fe58019SAttilio Rao &fuse_iov_permanent_bufsize, 0, 1225fe58019SAttilio Rao "limit for permanently stored buffer size for fuse_iovs"); 1235fe58019SAttilio Rao static int fuse_iov_credit = 16; 1245fe58019SAttilio Rao 125123af6ecSAlan Somers SYSCTL_INT(_vfs_fusefs, OID_AUTO, iov_credit, CTLFLAG_RW, 1265fe58019SAttilio Rao &fuse_iov_credit, 0, 1275fe58019SAttilio Rao "how many times is an oversized fuse_iov tolerated"); 1285fe58019SAttilio Rao 1295fe58019SAttilio Rao MALLOC_DEFINE(M_FUSEMSG, "fuse_msgbuf", "fuse message buffer"); 1305fe58019SAttilio Rao static uma_zone_t ticket_zone; 1315fe58019SAttilio Rao 132723c7768SAlan Somers /* 133723c7768SAlan Somers * TODO: figure out how to timeout INTERRUPT requests, because the daemon may 134723c7768SAlan Somers * leagally never respond 135723c7768SAlan Somers */ 136723c7768SAlan Somers static int 137723c7768SAlan Somers fuse_interrupt_callback(struct fuse_ticket *tick, struct uio *uio) 1385fe58019SAttilio Rao { 139a1542146SAlan Somers struct fuse_ticket *otick, *x_tick; 140a1542146SAlan Somers struct fuse_interrupt_in *fii; 141102c7ac0SAlan Somers struct fuse_data *data = tick->tk_data; 142a1542146SAlan Somers bool found = false; 143a1542146SAlan Somers 144a1542146SAlan Somers fii = (struct fuse_interrupt_in*)((char*)tick->tk_ms_fiov.base + 145a1542146SAlan Somers sizeof(struct fuse_in_header)); 146a1542146SAlan Somers 147a1542146SAlan Somers fuse_lck_mtx_lock(data->aw_mtx); 148a1542146SAlan Somers TAILQ_FOREACH_SAFE(otick, &data->aw_head, tk_aw_link, x_tick) { 149a1542146SAlan Somers if (otick->tk_unique == fii->unique) { 150a1542146SAlan Somers found = true; 151a1542146SAlan Somers break; 152a1542146SAlan Somers } 153a1542146SAlan Somers } 154a1542146SAlan Somers fuse_lck_mtx_unlock(data->aw_mtx); 155a1542146SAlan Somers 156a1542146SAlan Somers if (!found) { 157a1542146SAlan Somers /* Original is already complete. Just return */ 158a1542146SAlan Somers return 0; 159a1542146SAlan Somers } 160a1542146SAlan Somers 161a1542146SAlan Somers /* Clear the original ticket's interrupt association */ 162a1542146SAlan Somers otick->irq_unique = 0; 163a1542146SAlan Somers 164102c7ac0SAlan Somers if (tick->tk_aw_ohead.error == ENOSYS) { 165102c7ac0SAlan Somers fsess_set_notimpl(data->mp, FUSE_INTERRUPT); 166102c7ac0SAlan Somers return 0; 167102c7ac0SAlan Somers } else if (tick->tk_aw_ohead.error == EAGAIN) { 168723c7768SAlan Somers /* 169723c7768SAlan Somers * There are two reasons we might get this: 170723c7768SAlan Somers * 1) the daemon received the INTERRUPT request before the 171723c7768SAlan Somers * original, or 172723c7768SAlan Somers * 2) the daemon received the INTERRUPT request after it 173723c7768SAlan Somers * completed the original request. 174723c7768SAlan Somers * In the first case we should re-send the INTERRUPT. In the 175723c7768SAlan Somers * second, we should ignore it. 176723c7768SAlan Somers */ 177723c7768SAlan Somers /* Resend */ 1783d070fdcSAlan Somers fuse_interrupt_send(otick, EINTR); 179723c7768SAlan Somers return 0; 180723c7768SAlan Somers } else { 181723c7768SAlan Somers /* Illegal FUSE_INTERRUPT response */ 182723c7768SAlan Somers return EINVAL; 183723c7768SAlan Somers } 1845fe58019SAttilio Rao } 1855fe58019SAttilio Rao 1863d070fdcSAlan Somers /* Interrupt the operation otick. Return err as its error code */ 187723c7768SAlan Somers void 1883d070fdcSAlan Somers fuse_interrupt_send(struct fuse_ticket *otick, int err) 1895fe58019SAttilio Rao { 190723c7768SAlan Somers struct fuse_dispatcher fdi; 191723c7768SAlan Somers struct fuse_interrupt_in *fii; 192723c7768SAlan Somers struct fuse_in_header *ftick_hdr; 193723c7768SAlan Somers struct fuse_data *data = otick->tk_data; 1943d070fdcSAlan Somers struct fuse_ticket *tick, *xtick; 195723c7768SAlan Somers struct ucred reused_creds; 19661b0a927SAlan Somers gid_t reused_groups[1]; 1975fe58019SAttilio Rao 198a1542146SAlan Somers if (otick->irq_unique == 0) { 1993d070fdcSAlan Somers /* 2003d070fdcSAlan Somers * If the daemon hasn't yet received otick, then we can answer 2013d070fdcSAlan Somers * it ourselves and return. 2023d070fdcSAlan Somers */ 2033d070fdcSAlan Somers fuse_lck_mtx_lock(data->ms_mtx); 2043d070fdcSAlan Somers STAILQ_FOREACH_SAFE(tick, &otick->tk_data->ms_head, tk_ms_link, 2053d070fdcSAlan Somers xtick) { 2063d070fdcSAlan Somers if (tick == otick) { 2073d070fdcSAlan Somers STAILQ_REMOVE(&otick->tk_data->ms_head, tick, 2083d070fdcSAlan Somers fuse_ticket, tk_ms_link); 2090a7c63e0SAlan Somers otick->tk_data->ms_count--; 2103d070fdcSAlan Somers otick->tk_ms_link.stqe_next = NULL; 2113d070fdcSAlan Somers fuse_lck_mtx_unlock(data->ms_mtx); 2123d070fdcSAlan Somers 2133d070fdcSAlan Somers fuse_lck_mtx_lock(otick->tk_aw_mtx); 2143d070fdcSAlan Somers if (!fticket_answered(otick)) { 2153d070fdcSAlan Somers fticket_set_answered(otick); 2163d070fdcSAlan Somers otick->tk_aw_errno = err; 2173d070fdcSAlan Somers wakeup(otick); 2183d070fdcSAlan Somers } 2193d070fdcSAlan Somers fuse_lck_mtx_unlock(otick->tk_aw_mtx); 2203d070fdcSAlan Somers 2213d070fdcSAlan Somers fuse_ticket_drop(tick); 2223d070fdcSAlan Somers return; 2233d070fdcSAlan Somers } 2243d070fdcSAlan Somers } 2253d070fdcSAlan Somers fuse_lck_mtx_unlock(data->ms_mtx); 2263d070fdcSAlan Somers 2273d070fdcSAlan Somers /* 228102c7ac0SAlan Somers * If the fuse daemon doesn't support interrupts, then there's 229102c7ac0SAlan Somers * nothing more that we can do 230102c7ac0SAlan Somers */ 23137df9d3bSAlan Somers if (fsess_not_impl(data->mp, FUSE_INTERRUPT)) 232102c7ac0SAlan Somers return; 233102c7ac0SAlan Somers 234102c7ac0SAlan Somers /* 2353d070fdcSAlan Somers * If the fuse daemon has already received otick, then we must 2363d070fdcSAlan Somers * send FUSE_INTERRUPT. 2373d070fdcSAlan Somers */ 238723c7768SAlan Somers ftick_hdr = fticket_in_header(otick); 239723c7768SAlan Somers reused_creds.cr_uid = ftick_hdr->uid; 24061b0a927SAlan Somers reused_groups[0] = ftick_hdr->gid; 24161b0a927SAlan Somers reused_creds.cr_groups = reused_groups; 242723c7768SAlan Somers fdisp_init(&fdi, sizeof(*fii)); 243723c7768SAlan Somers fdisp_make_pid(&fdi, FUSE_INTERRUPT, data, ftick_hdr->nodeid, 244723c7768SAlan Somers ftick_hdr->pid, &reused_creds); 245723c7768SAlan Somers 246723c7768SAlan Somers fii = fdi.indata; 247723c7768SAlan Somers fii->unique = otick->tk_unique; 248723c7768SAlan Somers fuse_insert_callback(fdi.tick, fuse_interrupt_callback); 249723c7768SAlan Somers 250a1542146SAlan Somers otick->irq_unique = fdi.tick->tk_unique; 251268c28edSAlan Somers /* Interrupt ops should be delivered ASAP */ 252268c28edSAlan Somers fuse_insert_message(fdi.tick, true); 253723c7768SAlan Somers fdisp_destroy(&fdi); 254a1542146SAlan Somers } else { 255a1542146SAlan Somers /* This ticket has already been interrupted */ 256a1542146SAlan Somers } 2575fe58019SAttilio Rao } 2585fe58019SAttilio Rao 2595fe58019SAttilio Rao void 2605fe58019SAttilio Rao fiov_init(struct fuse_iov *fiov, size_t size) 2615fe58019SAttilio Rao { 2625fe58019SAttilio Rao uint32_t msize = FU_AT_LEAST(size); 2635fe58019SAttilio Rao 2645fe58019SAttilio Rao fiov->len = 0; 2655fe58019SAttilio Rao 2665fe58019SAttilio Rao fiov->base = malloc(msize, M_FUSEMSG, M_WAITOK | M_ZERO); 2675fe58019SAttilio Rao 2685fe58019SAttilio Rao fiov->allocated_size = msize; 2695fe58019SAttilio Rao fiov->credit = fuse_iov_credit; 2705fe58019SAttilio Rao } 2715fe58019SAttilio Rao 2725fe58019SAttilio Rao void 2735fe58019SAttilio Rao fiov_teardown(struct fuse_iov *fiov) 2745fe58019SAttilio Rao { 2755fe58019SAttilio Rao MPASS(fiov->base != NULL); 2765fe58019SAttilio Rao free(fiov->base, M_FUSEMSG); 2775fe58019SAttilio Rao } 2785fe58019SAttilio Rao 2795fe58019SAttilio Rao void 2805fe58019SAttilio Rao fiov_adjust(struct fuse_iov *fiov, size_t size) 2815fe58019SAttilio Rao { 2825fe58019SAttilio Rao if (fiov->allocated_size < size || 2835fe58019SAttilio Rao (fuse_iov_permanent_bufsize >= 0 && 2845fe58019SAttilio Rao fiov->allocated_size - size > fuse_iov_permanent_bufsize && 2855fe58019SAttilio Rao --fiov->credit < 0)) { 2865fe58019SAttilio Rao fiov->base = realloc(fiov->base, FU_AT_LEAST(size), M_FUSEMSG, 2875fe58019SAttilio Rao M_WAITOK | M_ZERO); 2885fe58019SAttilio Rao if (!fiov->base) { 2895fe58019SAttilio Rao panic("FUSE: realloc failed"); 2905fe58019SAttilio Rao } 2915fe58019SAttilio Rao fiov->allocated_size = FU_AT_LEAST(size); 2925fe58019SAttilio Rao fiov->credit = fuse_iov_credit; 2938d013becSAlan Somers /* Clear data buffer after reallocation */ 2948d013becSAlan Somers bzero(fiov->base, size); 2958d013becSAlan Somers } else if (size > fiov->len) { 2968d013becSAlan Somers /* Clear newly extended portion of data buffer */ 2978d013becSAlan Somers bzero((char*)fiov->base + fiov->len, size - fiov->len); 2985fe58019SAttilio Rao } 2995fe58019SAttilio Rao fiov->len = size; 3005fe58019SAttilio Rao } 3015fe58019SAttilio Rao 30212292a99SAlan Somers /* Resize the fiov if needed, and clear it's buffer */ 3035fe58019SAttilio Rao void 3045fe58019SAttilio Rao fiov_refresh(struct fuse_iov *fiov) 3055fe58019SAttilio Rao { 3065fe58019SAttilio Rao fiov_adjust(fiov, 0); 3075fe58019SAttilio Rao } 3085fe58019SAttilio Rao 3095fe58019SAttilio Rao static int 3105fe58019SAttilio Rao fticket_ctor(void *mem, int size, void *arg, int flags) 3115fe58019SAttilio Rao { 3125fe58019SAttilio Rao struct fuse_ticket *ftick = mem; 3135fe58019SAttilio Rao struct fuse_data *data = arg; 3145fe58019SAttilio Rao 3155fe58019SAttilio Rao FUSE_ASSERT_MS_DONE(ftick); 3165fe58019SAttilio Rao FUSE_ASSERT_AW_DONE(ftick); 3175fe58019SAttilio Rao 3185fe58019SAttilio Rao ftick->tk_data = data; 319a1542146SAlan Somers ftick->irq_unique = 0; 3205fe58019SAttilio Rao refcount_init(&ftick->tk_refcount, 1); 321560a55d0SAlan Somers counter_u64_add(fuse_ticket_count, 1); 3225fe58019SAttilio Rao 3235403f2c1SAlan Somers fticket_refresh(ftick); 3245403f2c1SAlan Somers 3255fe58019SAttilio Rao return 0; 3265fe58019SAttilio Rao } 3275fe58019SAttilio Rao 3285fe58019SAttilio Rao static void 3295fe58019SAttilio Rao fticket_dtor(void *mem, int size, void *arg) 3305fe58019SAttilio Rao { 331f220ef0bSAlan Somers #ifdef INVARIANTS 3325fe58019SAttilio Rao struct fuse_ticket *ftick = mem; 333f220ef0bSAlan Somers #endif 3345fe58019SAttilio Rao 3355fe58019SAttilio Rao FUSE_ASSERT_MS_DONE(ftick); 3365fe58019SAttilio Rao FUSE_ASSERT_AW_DONE(ftick); 3375fe58019SAttilio Rao 338560a55d0SAlan Somers counter_u64_add(fuse_ticket_count, -1); 3395fe58019SAttilio Rao } 3405fe58019SAttilio Rao 3415fe58019SAttilio Rao static int 3425fe58019SAttilio Rao fticket_init(void *mem, int size, int flags) 3435fe58019SAttilio Rao { 3445fe58019SAttilio Rao struct fuse_ticket *ftick = mem; 3455fe58019SAttilio Rao 3465fe58019SAttilio Rao bzero(ftick, sizeof(struct fuse_ticket)); 3475fe58019SAttilio Rao 3485fe58019SAttilio Rao fiov_init(&ftick->tk_ms_fiov, sizeof(struct fuse_in_header)); 3495fe58019SAttilio Rao 3505fe58019SAttilio Rao mtx_init(&ftick->tk_aw_mtx, "fuse answer delivery mutex", NULL, MTX_DEF); 3515fe58019SAttilio Rao fiov_init(&ftick->tk_aw_fiov, 0); 3525fe58019SAttilio Rao 3535fe58019SAttilio Rao return 0; 3545fe58019SAttilio Rao } 3555fe58019SAttilio Rao 3565fe58019SAttilio Rao static void 3575fe58019SAttilio Rao fticket_fini(void *mem, int size) 3585fe58019SAttilio Rao { 3595fe58019SAttilio Rao struct fuse_ticket *ftick = mem; 3605fe58019SAttilio Rao 3615fe58019SAttilio Rao fiov_teardown(&ftick->tk_ms_fiov); 3625fe58019SAttilio Rao fiov_teardown(&ftick->tk_aw_fiov); 3635fe58019SAttilio Rao mtx_destroy(&ftick->tk_aw_mtx); 3645fe58019SAttilio Rao } 3655fe58019SAttilio Rao 36602295cafSConrad Meyer static inline struct fuse_ticket * 3675fe58019SAttilio Rao fticket_alloc(struct fuse_data *data) 3685fe58019SAttilio Rao { 3695fe58019SAttilio Rao return uma_zalloc_arg(ticket_zone, data, M_WAITOK); 3705fe58019SAttilio Rao } 3715fe58019SAttilio Rao 37202295cafSConrad Meyer static inline void 3735fe58019SAttilio Rao fticket_destroy(struct fuse_ticket *ftick) 3745fe58019SAttilio Rao { 3755fe58019SAttilio Rao return uma_zfree(ticket_zone, ftick); 3765fe58019SAttilio Rao } 3775fe58019SAttilio Rao 3785403f2c1SAlan Somers /* Prepare the ticket to be reused and clear its data buffers */ 3795403f2c1SAlan Somers static inline void 3805fe58019SAttilio Rao fticket_refresh(struct fuse_ticket *ftick) 3815fe58019SAttilio Rao { 3825403f2c1SAlan Somers fticket_reset(ftick); 3835fe58019SAttilio Rao 3845fe58019SAttilio Rao fiov_refresh(&ftick->tk_ms_fiov); 3855fe58019SAttilio Rao fiov_refresh(&ftick->tk_aw_fiov); 3865fe58019SAttilio Rao } 3875fe58019SAttilio Rao 3885403f2c1SAlan Somers /* Prepare the ticket to be reused, but don't clear its data buffers */ 38912292a99SAlan Somers static inline void 39012292a99SAlan Somers fticket_reset(struct fuse_ticket *ftick) 39112292a99SAlan Somers { 3925403f2c1SAlan Somers struct fuse_data *data = ftick->tk_data; 3935403f2c1SAlan Somers 39412292a99SAlan Somers FUSE_ASSERT_MS_DONE(ftick); 39512292a99SAlan Somers FUSE_ASSERT_AW_DONE(ftick); 39612292a99SAlan Somers 39712292a99SAlan Somers bzero(&ftick->tk_aw_ohead, sizeof(struct fuse_out_header)); 39812292a99SAlan Somers 39912292a99SAlan Somers ftick->tk_aw_errno = 0; 40012292a99SAlan Somers ftick->tk_flag = 0; 4015403f2c1SAlan Somers 4025403f2c1SAlan Somers /* May be truncated to 32 bits on LP32 arches */ 4035403f2c1SAlan Somers ftick->tk_unique = atomic_fetchadd_long(&data->ticketer, 1); 4045403f2c1SAlan Somers if (ftick->tk_unique == 0) 4055403f2c1SAlan Somers ftick->tk_unique = atomic_fetchadd_long(&data->ticketer, 1); 40612292a99SAlan Somers } 40712292a99SAlan Somers 4085fe58019SAttilio Rao static int 4095fe58019SAttilio Rao fticket_wait_answer(struct fuse_ticket *ftick) 4105fe58019SAttilio Rao { 411723c7768SAlan Somers struct thread *td = curthread; 412723c7768SAlan Somers sigset_t blockedset, oldset; 413ebbfe00eSAlan Somers int err = 0, stops_deferred; 414ed74f781SAlan Somers struct fuse_data *data = ftick->tk_data; 415f0596245SAlan Somers bool interrupted = false; 4163d070fdcSAlan Somers 41737df9d3bSAlan Somers if (fsess_maybe_impl(ftick->tk_data->mp, FUSE_INTERRUPT) && 418ed74f781SAlan Somers data->dataflags & FSESS_INTR) { 419ebbfe00eSAlan Somers SIGEMPTYSET(blockedset); 420d5024ba2SAlan Somers } else { 421f0596245SAlan Somers /* Block all signals except (implicitly) SIGKILL */ 422d5024ba2SAlan Somers SIGFILLSET(blockedset); 423d5024ba2SAlan Somers } 424ebbfe00eSAlan Somers stops_deferred = sigdeferstop(SIGDEFERSTOP_SILENT); 4253d070fdcSAlan Somers kern_sigprocmask(td, SIG_BLOCK, NULL, &oldset, 0); 4265fe58019SAttilio Rao 427c02ccc7eSAlan Somers fuse_lck_mtx_lock(ftick->tk_aw_mtx); 428c02ccc7eSAlan Somers 429723c7768SAlan Somers retry: 4305fe58019SAttilio Rao if (fticket_answered(ftick)) { 4315fe58019SAttilio Rao goto out; 4325fe58019SAttilio Rao } 4335fe58019SAttilio Rao 4345fe58019SAttilio Rao if (fdata_get_dead(data)) { 4355fe58019SAttilio Rao err = ENOTCONN; 4365fe58019SAttilio Rao fticket_set_answered(ftick); 4375fe58019SAttilio Rao goto out; 4385fe58019SAttilio Rao } 4393d070fdcSAlan Somers kern_sigprocmask(td, SIG_BLOCK, &blockedset, NULL, 0); 4405fe58019SAttilio Rao err = msleep(ftick, &ftick->tk_aw_mtx, PCATCH, "fu_ans", 4415fe58019SAttilio Rao data->daemon_timeout * hz); 442723c7768SAlan Somers kern_sigprocmask(td, SIG_SETMASK, &oldset, NULL, 0); 443723c7768SAlan Somers if (err == EWOULDBLOCK) { 444419e7ff6SAlan Somers SDT_PROBE2(fusefs, , ipc, trace, 3, 445723c7768SAlan Somers "fticket_wait_answer: EWOULDBLOCK"); 4465fe58019SAttilio Rao #ifdef XXXIP /* die conditionally */ 4475fe58019SAttilio Rao if (!fdata_get_dead(data)) { 4485fe58019SAttilio Rao fdata_set_dead(data); 4495fe58019SAttilio Rao } 4505fe58019SAttilio Rao #endif 4515fe58019SAttilio Rao err = ETIMEDOUT; 4525fe58019SAttilio Rao fticket_set_answered(ftick); 453723c7768SAlan Somers } else if ((err == EINTR || err == ERESTART)) { 454723c7768SAlan Somers /* 455723c7768SAlan Somers * Whether we get EINTR or ERESTART depends on whether 456723c7768SAlan Somers * SA_RESTART was set by sigaction(2). 457723c7768SAlan Somers * 458723c7768SAlan Somers * Try to interrupt the operation and wait for an EINTR response 459723c7768SAlan Somers * to the original operation. If the file system does not 460723c7768SAlan Somers * support FUSE_INTERRUPT, then we'll just wait for it to 461723c7768SAlan Somers * complete like normal. If it does support FUSE_INTERRUPT, 462723c7768SAlan Somers * then it will either respond EINTR to the original operation, 463723c7768SAlan Somers * or EAGAIN to the interrupt. 464723c7768SAlan Somers */ 465d26d63a4SAlan Somers sigset_t tmpset; 466723c7768SAlan Somers 467419e7ff6SAlan Somers SDT_PROBE2(fusefs, , ipc, trace, 4, 468723c7768SAlan Somers "fticket_wait_answer: interrupt"); 469723c7768SAlan Somers fuse_lck_mtx_unlock(ftick->tk_aw_mtx); 4703d070fdcSAlan Somers fuse_interrupt_send(ftick, err); 471723c7768SAlan Somers 472723c7768SAlan Somers PROC_LOCK(td->td_proc); 473723c7768SAlan Somers mtx_lock(&td->td_proc->p_sigacts->ps_mtx); 474d26d63a4SAlan Somers tmpset = td->td_proc->p_siglist; 475d26d63a4SAlan Somers SIGSETOR(tmpset, td->td_siglist); 476723c7768SAlan Somers mtx_unlock(&td->td_proc->p_sigacts->ps_mtx); 477723c7768SAlan Somers PROC_UNLOCK(td->td_proc); 4783d070fdcSAlan Somers 4793d070fdcSAlan Somers fuse_lck_mtx_lock(ftick->tk_aw_mtx); 480f0596245SAlan Somers if (!interrupted && !SIGISMEMBER(tmpset, SIGKILL)) { 481a1542146SAlan Somers /* 482f0596245SAlan Somers * Block all signals while we wait for an interrupt 483f0596245SAlan Somers * response. The protocol doesn't discriminate between 484f0596245SAlan Somers * different signals. 485a1542146SAlan Somers */ 486f0596245SAlan Somers SIGFILLSET(blockedset); 487f0596245SAlan Somers interrupted = true; 488723c7768SAlan Somers goto retry; 489a1542146SAlan Somers } else { 490f0596245SAlan Somers /* 491f0596245SAlan Somers * Return immediately for fatal signals, or if this is 492f0596245SAlan Somers * the second interruption. We should only be 493f0596245SAlan Somers * interrupted twice if the thread is stopped, for 494f0596245SAlan Somers * example during sigexit. 495f0596245SAlan Somers */ 496a1542146SAlan Somers } 497723c7768SAlan Somers } else if (err) { 498419e7ff6SAlan Somers SDT_PROBE2(fusefs, , ipc, trace, 6, 499723c7768SAlan Somers "fticket_wait_answer: other error"); 500723c7768SAlan Somers } else { 501419e7ff6SAlan Somers SDT_PROBE2(fusefs, , ipc, trace, 7, "fticket_wait_answer: OK"); 5025fe58019SAttilio Rao } 5035fe58019SAttilio Rao out: 5045fe58019SAttilio Rao if (!(err || fticket_answered(ftick))) { 505419e7ff6SAlan Somers SDT_PROBE2(fusefs, , ipc, trace, 1, 506cf169498SAlan Somers "FUSE: requester was woken up but still no answer"); 5075fe58019SAttilio Rao err = ENXIO; 5085fe58019SAttilio Rao } 5095fe58019SAttilio Rao fuse_lck_mtx_unlock(ftick->tk_aw_mtx); 510ebbfe00eSAlan Somers sigallowstop(stops_deferred); 5115fe58019SAttilio Rao 5125fe58019SAttilio Rao return err; 5135fe58019SAttilio Rao } 5145fe58019SAttilio Rao 51502295cafSConrad Meyer static inline 5165fe58019SAttilio Rao int 5175fe58019SAttilio Rao fticket_aw_pull_uio(struct fuse_ticket *ftick, struct uio *uio) 5185fe58019SAttilio Rao { 5195fe58019SAttilio Rao int err = 0; 5205fe58019SAttilio Rao size_t len = uio_resid(uio); 5215fe58019SAttilio Rao 5225fe58019SAttilio Rao if (len) { 5235fe58019SAttilio Rao fiov_adjust(fticket_resp(ftick), len); 5245fe58019SAttilio Rao err = uiomove(fticket_resp(ftick)->base, len, uio); 5255fe58019SAttilio Rao } 5265fe58019SAttilio Rao return err; 5275fe58019SAttilio Rao } 5285fe58019SAttilio Rao 5295fe58019SAttilio Rao int 5305fe58019SAttilio Rao fticket_pull(struct fuse_ticket *ftick, struct uio *uio) 5315fe58019SAttilio Rao { 5325fe58019SAttilio Rao int err = 0; 5335fe58019SAttilio Rao 5345fe58019SAttilio Rao if (ftick->tk_aw_ohead.error) { 5355fe58019SAttilio Rao return 0; 5365fe58019SAttilio Rao } 5375fe58019SAttilio Rao err = fuse_body_audit(ftick, uio_resid(uio)); 5385fe58019SAttilio Rao if (!err) { 5395fe58019SAttilio Rao err = fticket_aw_pull_uio(ftick, uio); 5405fe58019SAttilio Rao } 5415fe58019SAttilio Rao return err; 5425fe58019SAttilio Rao } 5435fe58019SAttilio Rao 5445fe58019SAttilio Rao struct fuse_data * 5455fe58019SAttilio Rao fdata_alloc(struct cdev *fdev, struct ucred *cred) 5465fe58019SAttilio Rao { 5475fe58019SAttilio Rao struct fuse_data *data; 5485fe58019SAttilio Rao 5495fe58019SAttilio Rao data = malloc(sizeof(struct fuse_data), M_FUSEMSG, M_WAITOK | M_ZERO); 5505fe58019SAttilio Rao 5515fe58019SAttilio Rao data->fdev = fdev; 5525fe58019SAttilio Rao mtx_init(&data->ms_mtx, "fuse message list mutex", NULL, MTX_DEF); 5535fe58019SAttilio Rao STAILQ_INIT(&data->ms_head); 5540a7c63e0SAlan Somers data->ms_count = 0; 5553429092cSAlan Somers knlist_init_mtx(&data->ks_rsel.si_note, &data->ms_mtx); 5565fe58019SAttilio Rao mtx_init(&data->aw_mtx, "fuse answer list mutex", NULL, MTX_DEF); 5575fe58019SAttilio Rao TAILQ_INIT(&data->aw_head); 5585fe58019SAttilio Rao data->daemoncred = crhold(cred); 5595fe58019SAttilio Rao data->daemon_timeout = FUSE_DEFAULT_DAEMON_TIMEOUT; 5605fe58019SAttilio Rao sx_init(&data->rename_lock, "fuse rename lock"); 5615fe58019SAttilio Rao data->ref = 1; 5625fe58019SAttilio Rao 5635fe58019SAttilio Rao return data; 5645fe58019SAttilio Rao } 5655fe58019SAttilio Rao 5665fe58019SAttilio Rao void 5675fe58019SAttilio Rao fdata_trydestroy(struct fuse_data *data) 5685fe58019SAttilio Rao { 5695fe58019SAttilio Rao data->ref--; 5705fe58019SAttilio Rao MPASS(data->ref >= 0); 5715fe58019SAttilio Rao if (data->ref != 0) 5725fe58019SAttilio Rao return; 5735fe58019SAttilio Rao 5745fe58019SAttilio Rao /* Driving off stage all that stuff thrown at device... */ 5755fe58019SAttilio Rao sx_destroy(&data->rename_lock); 5765fe58019SAttilio Rao crfree(data->daemoncred); 5773429092cSAlan Somers mtx_destroy(&data->aw_mtx); 5783429092cSAlan Somers knlist_delete(&data->ks_rsel.si_note, curthread, 0); 5793429092cSAlan Somers knlist_destroy(&data->ks_rsel.si_note); 5803429092cSAlan Somers mtx_destroy(&data->ms_mtx); 5815fe58019SAttilio Rao 5825fe58019SAttilio Rao free(data, M_FUSEMSG); 5835fe58019SAttilio Rao } 5845fe58019SAttilio Rao 5855fe58019SAttilio Rao void 5865fe58019SAttilio Rao fdata_set_dead(struct fuse_data *data) 5875fe58019SAttilio Rao { 5885fe58019SAttilio Rao FUSE_LOCK(); 5895fe58019SAttilio Rao if (fdata_get_dead(data)) { 5905fe58019SAttilio Rao FUSE_UNLOCK(); 5915fe58019SAttilio Rao return; 5925fe58019SAttilio Rao } 5935fe58019SAttilio Rao fuse_lck_mtx_lock(data->ms_mtx); 5945fe58019SAttilio Rao data->dataflags |= FSESS_DEAD; 5955fe58019SAttilio Rao wakeup_one(data); 5965fe58019SAttilio Rao selwakeuppri(&data->ks_rsel, PZERO + 1); 5975fe58019SAttilio Rao wakeup(&data->ticketer); 5985fe58019SAttilio Rao fuse_lck_mtx_unlock(data->ms_mtx); 5995fe58019SAttilio Rao FUSE_UNLOCK(); 6005fe58019SAttilio Rao } 6015fe58019SAttilio Rao 6025fe58019SAttilio Rao struct fuse_ticket * 6035fe58019SAttilio Rao fuse_ticket_fetch(struct fuse_data *data) 6045fe58019SAttilio Rao { 6055fe58019SAttilio Rao int err = 0; 6065fe58019SAttilio Rao struct fuse_ticket *ftick; 6075fe58019SAttilio Rao 6085fe58019SAttilio Rao ftick = fticket_alloc(data); 6095fe58019SAttilio Rao 6105fe58019SAttilio Rao if (!(data->dataflags & FSESS_INITED)) { 611*8b007a74SJose Luis Duran /* Sleep until get answer for INIT message */ 6125fe58019SAttilio Rao FUSE_LOCK(); 6135fe58019SAttilio Rao if (!(data->dataflags & FSESS_INITED) && data->ticketer > 2) { 6145fe58019SAttilio Rao err = msleep(&data->ticketer, &fuse_mtx, PCATCH | PDROP, 6155fe58019SAttilio Rao "fu_ini", 0); 6165fe58019SAttilio Rao if (err) 6175fe58019SAttilio Rao fdata_set_dead(data); 6185fe58019SAttilio Rao } else 6195fe58019SAttilio Rao FUSE_UNLOCK(); 6205fe58019SAttilio Rao } 6215fe58019SAttilio Rao return ftick; 6225fe58019SAttilio Rao } 6235fe58019SAttilio Rao 6245fe58019SAttilio Rao int 6255fe58019SAttilio Rao fuse_ticket_drop(struct fuse_ticket *ftick) 6265fe58019SAttilio Rao { 6275fe58019SAttilio Rao int die; 6285fe58019SAttilio Rao 6295fe58019SAttilio Rao die = refcount_release(&ftick->tk_refcount); 6305fe58019SAttilio Rao if (die) 6315fe58019SAttilio Rao fticket_destroy(ftick); 6325fe58019SAttilio Rao 6335fe58019SAttilio Rao return die; 6345fe58019SAttilio Rao } 6355fe58019SAttilio Rao 6365fe58019SAttilio Rao void 6375fe58019SAttilio Rao fuse_insert_callback(struct fuse_ticket *ftick, fuse_handler_t * handler) 6385fe58019SAttilio Rao { 6395fe58019SAttilio Rao if (fdata_get_dead(ftick->tk_data)) { 6405fe58019SAttilio Rao return; 6415fe58019SAttilio Rao } 6425fe58019SAttilio Rao ftick->tk_aw_handler = handler; 6435fe58019SAttilio Rao 6445fe58019SAttilio Rao fuse_lck_mtx_lock(ftick->tk_data->aw_mtx); 6455fe58019SAttilio Rao fuse_aw_push(ftick); 6465fe58019SAttilio Rao fuse_lck_mtx_unlock(ftick->tk_data->aw_mtx); 6475fe58019SAttilio Rao } 6485fe58019SAttilio Rao 649268c28edSAlan Somers /* 650268c28edSAlan Somers * Insert a new upgoing ticket into the message queue 651268c28edSAlan Somers * 652268c28edSAlan Somers * If urgent is true, insert at the front of the queue. Otherwise, insert in 653268c28edSAlan Somers * FIFO order. 654268c28edSAlan Somers */ 6555fe58019SAttilio Rao void 656268c28edSAlan Somers fuse_insert_message(struct fuse_ticket *ftick, bool urgent) 6575fe58019SAttilio Rao { 6585fe58019SAttilio Rao if (ftick->tk_flag & FT_DIRTY) { 6595fe58019SAttilio Rao panic("FUSE: ticket reused without being refreshed"); 6605fe58019SAttilio Rao } 6615fe58019SAttilio Rao ftick->tk_flag |= FT_DIRTY; 6625fe58019SAttilio Rao 6635fe58019SAttilio Rao if (fdata_get_dead(ftick->tk_data)) { 6645fe58019SAttilio Rao return; 6655fe58019SAttilio Rao } 6665fe58019SAttilio Rao fuse_lck_mtx_lock(ftick->tk_data->ms_mtx); 667268c28edSAlan Somers if (urgent) 668268c28edSAlan Somers fuse_ms_push_head(ftick); 669268c28edSAlan Somers else 6705fe58019SAttilio Rao fuse_ms_push(ftick); 6715fe58019SAttilio Rao wakeup_one(ftick->tk_data); 6725fe58019SAttilio Rao selwakeuppri(&ftick->tk_data->ks_rsel, PZERO + 1); 6733429092cSAlan Somers KNOTE_LOCKED(&ftick->tk_data->ks_rsel.si_note, 0); 6745fe58019SAttilio Rao fuse_lck_mtx_unlock(ftick->tk_data->ms_mtx); 6755fe58019SAttilio Rao } 6765fe58019SAttilio Rao 6775fe58019SAttilio Rao static int 6785fe58019SAttilio Rao fuse_body_audit(struct fuse_ticket *ftick, size_t blen) 6795fe58019SAttilio Rao { 6805fe58019SAttilio Rao int err = 0; 6815fe58019SAttilio Rao enum fuse_opcode opcode; 6825fe58019SAttilio Rao 6835fe58019SAttilio Rao opcode = fticket_opcode(ftick); 6845fe58019SAttilio Rao 6855fe58019SAttilio Rao switch (opcode) { 686a1c9f4adSAlan Somers case FUSE_BMAP: 687a1c9f4adSAlan Somers err = (blen == sizeof(struct fuse_bmap_out)) ? 0 : EINVAL; 688a1c9f4adSAlan Somers break; 689a1c9f4adSAlan Somers 69016bd2d47SAlan Somers case FUSE_LINK: 6915fe58019SAttilio Rao case FUSE_LOOKUP: 69216bd2d47SAlan Somers case FUSE_MKDIR: 69316bd2d47SAlan Somers case FUSE_MKNOD: 69416bd2d47SAlan Somers case FUSE_SYMLINK: 69516bd2d47SAlan Somers if (fuse_libabi_geq(ftick->tk_data, 7, 9)) { 69616bd2d47SAlan Somers err = (blen == sizeof(struct fuse_entry_out)) ? 69716bd2d47SAlan Somers 0 : EINVAL; 69816bd2d47SAlan Somers } else { 69916bd2d47SAlan Somers err = (blen == FUSE_COMPAT_ENTRY_OUT_SIZE) ? 0 : EINVAL; 70016bd2d47SAlan Somers } 7015fe58019SAttilio Rao break; 7025fe58019SAttilio Rao 7035fe58019SAttilio Rao case FUSE_FORGET: 7045fe58019SAttilio Rao panic("FUSE: a handler has been intalled for FUSE_FORGET"); 7055fe58019SAttilio Rao break; 7065fe58019SAttilio Rao 7075fe58019SAttilio Rao case FUSE_GETATTR: 7085fe58019SAttilio Rao case FUSE_SETATTR: 70916bd2d47SAlan Somers if (fuse_libabi_geq(ftick->tk_data, 7, 9)) { 71016bd2d47SAlan Somers err = (blen == sizeof(struct fuse_attr_out)) ? 71116bd2d47SAlan Somers 0 : EINVAL; 71216bd2d47SAlan Somers } else { 71316bd2d47SAlan Somers err = (blen == FUSE_COMPAT_ATTR_OUT_SIZE) ? 0 : EINVAL; 71416bd2d47SAlan Somers } 7155fe58019SAttilio Rao break; 7165fe58019SAttilio Rao 7175fe58019SAttilio Rao case FUSE_READLINK: 7185fe58019SAttilio Rao err = (PAGE_SIZE >= blen) ? 0 : EINVAL; 7195fe58019SAttilio Rao break; 7205fe58019SAttilio Rao 7215fe58019SAttilio Rao case FUSE_UNLINK: 7225fe58019SAttilio Rao err = (blen == 0) ? 0 : EINVAL; 7235fe58019SAttilio Rao break; 7245fe58019SAttilio Rao 7255fe58019SAttilio Rao case FUSE_RMDIR: 7265fe58019SAttilio Rao err = (blen == 0) ? 0 : EINVAL; 7275fe58019SAttilio Rao break; 7285fe58019SAttilio Rao 7295fe58019SAttilio Rao case FUSE_RENAME: 7305fe58019SAttilio Rao err = (blen == 0) ? 0 : EINVAL; 7315fe58019SAttilio Rao break; 7325fe58019SAttilio Rao 7335fe58019SAttilio Rao case FUSE_OPEN: 7345fe58019SAttilio Rao err = (blen == sizeof(struct fuse_open_out)) ? 0 : EINVAL; 7355fe58019SAttilio Rao break; 7365fe58019SAttilio Rao 7375fe58019SAttilio Rao case FUSE_READ: 7385fe58019SAttilio Rao err = (((struct fuse_read_in *)( 7395fe58019SAttilio Rao (char *)ftick->tk_ms_fiov.base + 7405fe58019SAttilio Rao sizeof(struct fuse_in_header) 7415fe58019SAttilio Rao ))->size >= blen) ? 0 : EINVAL; 7425fe58019SAttilio Rao break; 7435fe58019SAttilio Rao 7445fe58019SAttilio Rao case FUSE_WRITE: 7455fe58019SAttilio Rao err = (blen == sizeof(struct fuse_write_out)) ? 0 : EINVAL; 7465fe58019SAttilio Rao break; 7475fe58019SAttilio Rao 7485fe58019SAttilio Rao case FUSE_STATFS: 7495fe58019SAttilio Rao if (fuse_libabi_geq(ftick->tk_data, 7, 4)) { 7505fe58019SAttilio Rao err = (blen == sizeof(struct fuse_statfs_out)) ? 7515fe58019SAttilio Rao 0 : EINVAL; 7525fe58019SAttilio Rao } else { 7535fe58019SAttilio Rao err = (blen == FUSE_COMPAT_STATFS_SIZE) ? 0 : EINVAL; 7545fe58019SAttilio Rao } 7555fe58019SAttilio Rao break; 7565fe58019SAttilio Rao 7575fe58019SAttilio Rao case FUSE_RELEASE: 7585fe58019SAttilio Rao err = (blen == 0) ? 0 : EINVAL; 7595fe58019SAttilio Rao break; 7605fe58019SAttilio Rao 7615fe58019SAttilio Rao case FUSE_FSYNC: 7625fe58019SAttilio Rao err = (blen == 0) ? 0 : EINVAL; 7635fe58019SAttilio Rao break; 7645fe58019SAttilio Rao 7655fe58019SAttilio Rao case FUSE_SETXATTR: 76604660064SFedor Uporov err = (blen == 0) ? 0 : EINVAL; 7675fe58019SAttilio Rao break; 7685fe58019SAttilio Rao 7695fe58019SAttilio Rao case FUSE_GETXATTR: 7705fe58019SAttilio Rao case FUSE_LISTXATTR: 77104660064SFedor Uporov /* 77204660064SFedor Uporov * These can have varying response lengths, and 0 length 77304660064SFedor Uporov * isn't necessarily invalid. 77404660064SFedor Uporov */ 77504660064SFedor Uporov err = 0; 7765fe58019SAttilio Rao break; 7775fe58019SAttilio Rao 7785fe58019SAttilio Rao case FUSE_REMOVEXATTR: 77904660064SFedor Uporov err = (blen == 0) ? 0 : EINVAL; 7805fe58019SAttilio Rao break; 7815fe58019SAttilio Rao 7825fe58019SAttilio Rao case FUSE_FLUSH: 7835fe58019SAttilio Rao err = (blen == 0) ? 0 : EINVAL; 7845fe58019SAttilio Rao break; 7855fe58019SAttilio Rao 7865fe58019SAttilio Rao case FUSE_INIT: 78787ff949aSAlan Somers if (blen == sizeof(struct fuse_init_out) || 78887ff949aSAlan Somers blen == FUSE_COMPAT_INIT_OUT_SIZE || 78987ff949aSAlan Somers blen == FUSE_COMPAT_22_INIT_OUT_SIZE) { 7905fe58019SAttilio Rao err = 0; 7915fe58019SAttilio Rao } else { 7925fe58019SAttilio Rao err = EINVAL; 7935fe58019SAttilio Rao } 7945fe58019SAttilio Rao break; 7955fe58019SAttilio Rao 7965fe58019SAttilio Rao case FUSE_OPENDIR: 7975fe58019SAttilio Rao err = (blen == sizeof(struct fuse_open_out)) ? 0 : EINVAL; 7985fe58019SAttilio Rao break; 7995fe58019SAttilio Rao 8005fe58019SAttilio Rao case FUSE_READDIR: 8015fe58019SAttilio Rao err = (((struct fuse_read_in *)( 8025fe58019SAttilio Rao (char *)ftick->tk_ms_fiov.base + 8035fe58019SAttilio Rao sizeof(struct fuse_in_header) 8045fe58019SAttilio Rao ))->size >= blen) ? 0 : EINVAL; 8055fe58019SAttilio Rao break; 8065fe58019SAttilio Rao 8075fe58019SAttilio Rao case FUSE_RELEASEDIR: 8085fe58019SAttilio Rao err = (blen == 0) ? 0 : EINVAL; 8095fe58019SAttilio Rao break; 8105fe58019SAttilio Rao 8115fe58019SAttilio Rao case FUSE_FSYNCDIR: 8125fe58019SAttilio Rao err = (blen == 0) ? 0 : EINVAL; 8135fe58019SAttilio Rao break; 8145fe58019SAttilio Rao 8155fe58019SAttilio Rao case FUSE_GETLK: 816f067b609SAlan Somers err = (blen == sizeof(struct fuse_lk_out)) ? 0 : EINVAL; 8175fe58019SAttilio Rao break; 8185fe58019SAttilio Rao 8195fe58019SAttilio Rao case FUSE_SETLK: 820f067b609SAlan Somers err = (blen == 0) ? 0 : EINVAL; 8215fe58019SAttilio Rao break; 8225fe58019SAttilio Rao 8235fe58019SAttilio Rao case FUSE_SETLKW: 824f067b609SAlan Somers err = (blen == 0) ? 0 : EINVAL; 8255fe58019SAttilio Rao break; 8265fe58019SAttilio Rao 8275fe58019SAttilio Rao case FUSE_ACCESS: 8285fe58019SAttilio Rao err = (blen == 0) ? 0 : EINVAL; 8295fe58019SAttilio Rao break; 8305fe58019SAttilio Rao 8315fe58019SAttilio Rao case FUSE_CREATE: 83216bd2d47SAlan Somers if (fuse_libabi_geq(ftick->tk_data, 7, 9)) { 8335fe58019SAttilio Rao err = (blen == sizeof(struct fuse_entry_out) + 8345fe58019SAttilio Rao sizeof(struct fuse_open_out)) ? 0 : EINVAL; 83516bd2d47SAlan Somers } else { 83616bd2d47SAlan Somers err = (blen == FUSE_COMPAT_ENTRY_OUT_SIZE + 83716bd2d47SAlan Somers sizeof(struct fuse_open_out)) ? 0 : EINVAL; 83816bd2d47SAlan Somers } 8395fe58019SAttilio Rao break; 8405fe58019SAttilio Rao 8415fe58019SAttilio Rao case FUSE_DESTROY: 8425fe58019SAttilio Rao err = (blen == 0) ? 0 : EINVAL; 8435fe58019SAttilio Rao break; 8445fe58019SAttilio Rao 845398c88c7SAlan Somers case FUSE_FALLOCATE: 846398c88c7SAlan Somers err = (blen == 0) ? 0 : EINVAL; 847398c88c7SAlan Somers break; 848398c88c7SAlan Somers 84937df9d3bSAlan Somers case FUSE_LSEEK: 85037df9d3bSAlan Somers err = (blen == sizeof(struct fuse_lseek_out)) ? 0 : EINVAL; 85137df9d3bSAlan Somers break; 85237df9d3bSAlan Somers 85392bbfe1fSAlan Somers case FUSE_COPY_FILE_RANGE: 85492bbfe1fSAlan Somers err = (blen == sizeof(struct fuse_write_out)) ? 0 : EINVAL; 85592bbfe1fSAlan Somers break; 85692bbfe1fSAlan Somers 8575fe58019SAttilio Rao default: 8585fe58019SAttilio Rao panic("FUSE: opcodes out of sync (%d)\n", opcode); 8595fe58019SAttilio Rao } 8605fe58019SAttilio Rao 8615fe58019SAttilio Rao return err; 8625fe58019SAttilio Rao } 8635fe58019SAttilio Rao 86402295cafSConrad Meyer static inline void 86502295cafSConrad Meyer fuse_setup_ihead(struct fuse_in_header *ihead, struct fuse_ticket *ftick, 86602295cafSConrad Meyer uint64_t nid, enum fuse_opcode op, size_t blen, pid_t pid, 8675fe58019SAttilio Rao struct ucred *cred) 8685fe58019SAttilio Rao { 8695fe58019SAttilio Rao ihead->len = sizeof(*ihead) + blen; 8705fe58019SAttilio Rao ihead->unique = ftick->tk_unique; 8715fe58019SAttilio Rao ihead->nodeid = nid; 8725fe58019SAttilio Rao ihead->opcode = op; 8735fe58019SAttilio Rao 8745fe58019SAttilio Rao ihead->pid = pid; 8755fe58019SAttilio Rao ihead->uid = cred->cr_uid; 87661b0a927SAlan Somers ihead->gid = cred->cr_groups[0]; 8775fe58019SAttilio Rao } 8785fe58019SAttilio Rao 8795fe58019SAttilio Rao /* 8805fe58019SAttilio Rao * fuse_standard_handler just pulls indata and wakes up pretender. 8815fe58019SAttilio Rao * Doesn't try to interpret data, that's left for the pretender. 8825fe58019SAttilio Rao * Though might do a basic size verification before the pull-in takes place 8835fe58019SAttilio Rao */ 8845fe58019SAttilio Rao 8855fe58019SAttilio Rao static int 8865fe58019SAttilio Rao fuse_standard_handler(struct fuse_ticket *ftick, struct uio *uio) 8875fe58019SAttilio Rao { 8885fe58019SAttilio Rao int err = 0; 8895fe58019SAttilio Rao 8905fe58019SAttilio Rao err = fticket_pull(ftick, uio); 8915fe58019SAttilio Rao 8925fe58019SAttilio Rao fuse_lck_mtx_lock(ftick->tk_aw_mtx); 8935fe58019SAttilio Rao 8945fe58019SAttilio Rao if (!fticket_answered(ftick)) { 8955fe58019SAttilio Rao fticket_set_answered(ftick); 8965fe58019SAttilio Rao ftick->tk_aw_errno = err; 8975fe58019SAttilio Rao wakeup(ftick); 8985fe58019SAttilio Rao } 8995fe58019SAttilio Rao fuse_lck_mtx_unlock(ftick->tk_aw_mtx); 9005fe58019SAttilio Rao 9015fe58019SAttilio Rao return err; 9025fe58019SAttilio Rao } 9035fe58019SAttilio Rao 90412292a99SAlan Somers /* 90512292a99SAlan Somers * Reinitialize a dispatcher from a pid and node id, without resizing or 90612292a99SAlan Somers * clearing its data buffers 90712292a99SAlan Somers */ 90812292a99SAlan Somers static void 90912292a99SAlan Somers fdisp_refresh_pid(struct fuse_dispatcher *fdip, enum fuse_opcode op, 91012292a99SAlan Somers struct mount *mp, uint64_t nid, pid_t pid, struct ucred *cred) 91112292a99SAlan Somers { 91212292a99SAlan Somers MPASS(fdip->tick); 9138d013becSAlan Somers MPASS2(sizeof(fdip->finh) + fdip->iosize <= fdip->tick->tk_ms_fiov.len, 9148d013becSAlan Somers "Must use fdisp_make_pid to increase the size of the fiov"); 91512292a99SAlan Somers fticket_reset(fdip->tick); 91612292a99SAlan Somers 91712292a99SAlan Somers FUSE_DIMALLOC(&fdip->tick->tk_ms_fiov, fdip->finh, 91812292a99SAlan Somers fdip->indata, fdip->iosize); 91912292a99SAlan Somers 92012292a99SAlan Somers fuse_setup_ihead(fdip->finh, fdip->tick, nid, op, fdip->iosize, pid, 92112292a99SAlan Somers cred); 92212292a99SAlan Somers } 92312292a99SAlan Somers 92412292a99SAlan Somers /* Initialize a dispatcher from a pid and node id */ 92512292a99SAlan Somers static void 92602295cafSConrad Meyer fdisp_make_pid(struct fuse_dispatcher *fdip, enum fuse_opcode op, 927723c7768SAlan Somers struct fuse_data *data, uint64_t nid, pid_t pid, struct ucred *cred) 9285fe58019SAttilio Rao { 9295fe58019SAttilio Rao if (fdip->tick) { 9305fe58019SAttilio Rao fticket_refresh(fdip->tick); 9315fe58019SAttilio Rao } else { 9325fe58019SAttilio Rao fdip->tick = fuse_ticket_fetch(data); 9335fe58019SAttilio Rao } 9345fe58019SAttilio Rao 9358d013becSAlan Somers /* FUSE_DIMALLOC will bzero the fiovs when it enlarges them */ 9365fe58019SAttilio Rao FUSE_DIMALLOC(&fdip->tick->tk_ms_fiov, fdip->finh, 9375fe58019SAttilio Rao fdip->indata, fdip->iosize); 9385fe58019SAttilio Rao 9395fe58019SAttilio Rao fuse_setup_ihead(fdip->finh, fdip->tick, nid, op, fdip->iosize, pid, cred); 9405fe58019SAttilio Rao } 9415fe58019SAttilio Rao 9425fe58019SAttilio Rao void 94302295cafSConrad Meyer fdisp_make(struct fuse_dispatcher *fdip, enum fuse_opcode op, struct mount *mp, 94402295cafSConrad Meyer uint64_t nid, struct thread *td, struct ucred *cred) 9455fe58019SAttilio Rao { 946723c7768SAlan Somers struct fuse_data *data = fuse_get_mpdata(mp); 9475fe58019SAttilio Rao RECTIFY_TDCR(td, cred); 9485fe58019SAttilio Rao 949723c7768SAlan Somers return fdisp_make_pid(fdip, op, data, nid, td->td_proc->p_pid, cred); 9505fe58019SAttilio Rao } 9515fe58019SAttilio Rao 9525fe58019SAttilio Rao void 95302295cafSConrad Meyer fdisp_make_vp(struct fuse_dispatcher *fdip, enum fuse_opcode op, 95402295cafSConrad Meyer struct vnode *vp, struct thread *td, struct ucred *cred) 9555fe58019SAttilio Rao { 956723c7768SAlan Somers struct mount *mp = vnode_mount(vp); 957723c7768SAlan Somers struct fuse_data *data = fuse_get_mpdata(mp); 958723c7768SAlan Somers 9595fe58019SAttilio Rao RECTIFY_TDCR(td, cred); 960723c7768SAlan Somers return fdisp_make_pid(fdip, op, data, VTOI(vp), 9615fe58019SAttilio Rao td->td_proc->p_pid, cred); 9625fe58019SAttilio Rao } 9635fe58019SAttilio Rao 96412292a99SAlan Somers /* Refresh a fuse_dispatcher so it can be reused, but don't zero its data */ 96512292a99SAlan Somers void 96612292a99SAlan Somers fdisp_refresh_vp(struct fuse_dispatcher *fdip, enum fuse_opcode op, 96712292a99SAlan Somers struct vnode *vp, struct thread *td, struct ucred *cred) 96812292a99SAlan Somers { 96912292a99SAlan Somers RECTIFY_TDCR(td, cred); 97012292a99SAlan Somers return fdisp_refresh_pid(fdip, op, vnode_mount(vp), VTOI(vp), 97112292a99SAlan Somers td->td_proc->p_pid, cred); 97212292a99SAlan Somers } 97312292a99SAlan Somers 974419e7ff6SAlan Somers SDT_PROBE_DEFINE2(fusefs, , ipc, fdisp_wait_answ_error, "char*", "int"); 975cf169498SAlan Somers 9765fe58019SAttilio Rao int 9775fe58019SAttilio Rao fdisp_wait_answ(struct fuse_dispatcher *fdip) 9785fe58019SAttilio Rao { 9795fe58019SAttilio Rao int err = 0; 9805fe58019SAttilio Rao 9815fe58019SAttilio Rao fdip->answ_stat = 0; 9825fe58019SAttilio Rao fuse_insert_callback(fdip->tick, fuse_standard_handler); 983268c28edSAlan Somers fuse_insert_message(fdip->tick, false); 9845fe58019SAttilio Rao 9855fe58019SAttilio Rao if ((err = fticket_wait_answer(fdip->tick))) { 9865fe58019SAttilio Rao fuse_lck_mtx_lock(fdip->tick->tk_aw_mtx); 9875fe58019SAttilio Rao 9885fe58019SAttilio Rao if (fticket_answered(fdip->tick)) { 9895fe58019SAttilio Rao /* 9905fe58019SAttilio Rao * Just between noticing the interrupt and getting here, 9915fe58019SAttilio Rao * the standard handler has completed his job. 9925fe58019SAttilio Rao * So we drop the ticket and exit as usual. 9935fe58019SAttilio Rao */ 994419e7ff6SAlan Somers SDT_PROBE2(fusefs, , ipc, fdisp_wait_answ_error, 995cf169498SAlan Somers "IPC: interrupted, already answered", err); 9965fe58019SAttilio Rao fuse_lck_mtx_unlock(fdip->tick->tk_aw_mtx); 9975fe58019SAttilio Rao goto out; 9985fe58019SAttilio Rao } else { 9995fe58019SAttilio Rao /* 10005fe58019SAttilio Rao * So we were faster than the standard handler. 10015fe58019SAttilio Rao * Then by setting the answered flag we get *him* 10025fe58019SAttilio Rao * to drop the ticket. 10035fe58019SAttilio Rao */ 1004419e7ff6SAlan Somers SDT_PROBE2(fusefs, , ipc, fdisp_wait_answ_error, 1005cf169498SAlan Somers "IPC: interrupted, setting to answered", err); 10065fe58019SAttilio Rao fticket_set_answered(fdip->tick); 10075fe58019SAttilio Rao fuse_lck_mtx_unlock(fdip->tick->tk_aw_mtx); 10085fe58019SAttilio Rao return err; 10095fe58019SAttilio Rao } 10105fe58019SAttilio Rao } 10115fe58019SAttilio Rao 10127e0aac24SAlan Somers if (fdip->tick->tk_aw_errno == ENOTCONN) { 10137e0aac24SAlan Somers /* The daemon died while we were waiting for a response */ 10147e0aac24SAlan Somers err = ENOTCONN; 10157e0aac24SAlan Somers goto out; 10167e0aac24SAlan Somers } else if (fdip->tick->tk_aw_errno) { 10177e0aac24SAlan Somers /* 10187e0aac24SAlan Somers * There was some sort of communication error with the daemon 10197e0aac24SAlan Somers * that the client wouldn't understand. 10207e0aac24SAlan Somers */ 1021419e7ff6SAlan Somers SDT_PROBE2(fusefs, , ipc, fdisp_wait_answ_error, 1022cf169498SAlan Somers "IPC: explicit EIO-ing", fdip->tick->tk_aw_errno); 10235fe58019SAttilio Rao err = EIO; 10245fe58019SAttilio Rao goto out; 10255fe58019SAttilio Rao } 10265fe58019SAttilio Rao if ((err = fdip->tick->tk_aw_ohead.error)) { 1027419e7ff6SAlan Somers SDT_PROBE2(fusefs, , ipc, fdisp_wait_answ_error, 1028cf169498SAlan Somers "IPC: setting status", fdip->tick->tk_aw_ohead.error); 10295fe58019SAttilio Rao /* 10305fe58019SAttilio Rao * This means a "proper" fuse syscall error. 10315fe58019SAttilio Rao * We record this value so the caller will 10325fe58019SAttilio Rao * be able to know it's not a boring messaging 10335fe58019SAttilio Rao * failure, if she wishes so (and if not, she can 10345fe58019SAttilio Rao * just simply propagate the return value of this routine). 10355fe58019SAttilio Rao * [XXX Maybe a bitflag would do the job too, 10365fe58019SAttilio Rao * if other flags needed, this will be converted thusly.] 10375fe58019SAttilio Rao */ 10385fe58019SAttilio Rao fdip->answ_stat = err; 10395fe58019SAttilio Rao goto out; 10405fe58019SAttilio Rao } 10415fe58019SAttilio Rao fdip->answ = fticket_resp(fdip->tick)->base; 10425fe58019SAttilio Rao fdip->iosize = fticket_resp(fdip->tick)->len; 10435fe58019SAttilio Rao 10445fe58019SAttilio Rao return 0; 10455fe58019SAttilio Rao 10465fe58019SAttilio Rao out: 10475fe58019SAttilio Rao return err; 10485fe58019SAttilio Rao } 10495fe58019SAttilio Rao 10505fe58019SAttilio Rao void 10515fe58019SAttilio Rao fuse_ipc_init(void) 10525fe58019SAttilio Rao { 10535fe58019SAttilio Rao ticket_zone = uma_zcreate("fuse_ticket", sizeof(struct fuse_ticket), 10545fe58019SAttilio Rao fticket_ctor, fticket_dtor, fticket_init, fticket_fini, 10555fe58019SAttilio Rao UMA_ALIGN_PTR, 0); 1056560a55d0SAlan Somers fuse_ticket_count = counter_u64_alloc(M_WAITOK); 10575fe58019SAttilio Rao } 10585fe58019SAttilio Rao 10595fe58019SAttilio Rao void 10605fe58019SAttilio Rao fuse_ipc_destroy(void) 10615fe58019SAttilio Rao { 1062560a55d0SAlan Somers counter_u64_free(fuse_ticket_count); 10635fe58019SAttilio Rao uma_zdestroy(ticket_zone); 10645fe58019SAttilio Rao } 10650b9a5c6fSAlan Somers 10660b9a5c6fSAlan Somers SDT_PROBE_DEFINE3(fusefs,, ipc, warn, "struct fuse_data*", "unsigned", "char*"); 10670b9a5c6fSAlan Somers void 10680b9a5c6fSAlan Somers fuse_warn(struct fuse_data *data, unsigned flag, const char *msg) 10690b9a5c6fSAlan Somers { 10700b9a5c6fSAlan Somers SDT_PROBE3(fusefs, , ipc, warn, data, flag, msg); 10710b9a5c6fSAlan Somers if (!(data->dataflags & flag)) { 10720b9a5c6fSAlan Somers printf("WARNING: FUSE protocol violation for server mounted at " 10730b9a5c6fSAlan Somers "%s: %s " 10740b9a5c6fSAlan Somers "This warning will not be repeated.\n", 10750b9a5c6fSAlan Somers data->mp->mnt_stat.f_mntonname, msg); 10760b9a5c6fSAlan Somers data->dataflags |= flag; 10770b9a5c6fSAlan Somers } 10780b9a5c6fSAlan Somers } 1079