2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2007-2009 Google Inc. and Amit Singh
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following disclaimer
15 * in the documentation and/or other materials provided with the
17 * * Neither the name of Google Inc. nor the names of its
18 * contributors may be used to endorse or promote products derived from
19 * this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * Copyright (C) 2005 Csaba Henk.
34 * All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
45 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 #include <sys/cdefs.h>
59 __FBSDID("$FreeBSD$");
61 #include <sys/param.h>
62 #include <sys/module.h>
63 #include <sys/systm.h>
64 #include <sys/errno.h>
65 #include <sys/kernel.h>
68 #include <sys/malloc.h>
69 #include <sys/queue.h>
72 #include <sys/mutex.h>
74 #include <sys/mount.h>
76 #include <sys/vnode.h>
77 #include <sys/signalvar.h>
78 #include <sys/syscallsubr.h>
79 #include <sys/sysctl.h>
83 #include "fuse_node.h"
85 #include "fuse_internal.h"
87 SDT_PROVIDER_DECLARE(fuse);
90 * arg0: verbosity. Higher numbers give more verbose messages
91 * arg1: Textual message
93 SDT_PROBE_DEFINE2(fuse, , ipc, trace, "int", "char*");
95 static void fdisp_make_pid(struct fuse_dispatcher *fdip, enum fuse_opcode op,
96 struct fuse_data *data, uint64_t nid, pid_t pid, struct ucred *cred);
97 static void fiov_clear(struct fuse_iov *fiov);
98 static void fuse_interrupt_send(struct fuse_ticket *otick);
99 static struct fuse_ticket *fticket_alloc(struct fuse_data *data);
100 static void fticket_refresh(struct fuse_ticket *ftick);
101 static void fticket_destroy(struct fuse_ticket *ftick);
102 static int fticket_wait_answer(struct fuse_ticket *ftick);
104 fticket_aw_pull_uio(struct fuse_ticket *ftick,
107 static int fuse_body_audit(struct fuse_ticket *ftick, size_t blen);
109 static fuse_handler_t fuse_standard_handler;
111 SYSCTL_NODE(_vfs, OID_AUTO, fusefs, CTLFLAG_RW, 0, "FUSE tunables");
112 SYSCTL_STRING(_vfs_fusefs, OID_AUTO, version, CTLFLAG_RD,
113 FUSE_FREEBSD_VERSION, 0, "fuse-freebsd version");
114 static int fuse_ticket_count = 0;
116 SYSCTL_INT(_vfs_fusefs, OID_AUTO, ticket_count, CTLFLAG_RW,
117 &fuse_ticket_count, 0, "number of allocated tickets");
118 static long fuse_iov_permanent_bufsize = 1 << 19;
120 SYSCTL_LONG(_vfs_fusefs, OID_AUTO, iov_permanent_bufsize, CTLFLAG_RW,
121 &fuse_iov_permanent_bufsize, 0,
122 "limit for permanently stored buffer size for fuse_iovs");
123 static int fuse_iov_credit = 16;
125 SYSCTL_INT(_vfs_fusefs, OID_AUTO, iov_credit, CTLFLAG_RW,
127 "how many times is an oversized fuse_iov tolerated");
129 MALLOC_DEFINE(M_FUSEMSG, "fuse_msgbuf", "fuse message buffer");
130 static uma_zone_t ticket_zone;
133 * TODO: figure out how to timeout INTERRUPT requests, because the daemon may
134 * leagally never respond
136 * TODO: remove an INTERRUPT request if the daemon responds to the original
139 fuse_interrupt_callback(struct fuse_ticket *tick, struct uio *uio)
141 if (tick->tk_aw_ohead.error == EAGAIN) {
143 * There are two reasons we might get this:
144 * 1) the daemon received the INTERRUPT request before the
146 * 2) the daemon received the INTERRUPT request after it
147 * completed the original request.
148 * In the first case we should re-send the INTERRUPT. In the
149 * second, we should ignore it.
151 struct fuse_interrupt_in *fii;
152 struct fuse_data *data;
153 struct fuse_ticket *otick, *x_tick;
156 data = tick->tk_data;
157 fii = (struct fuse_interrupt_in*)((char*)tick->tk_ms_fiov.base +
158 sizeof(struct fuse_in_header));
159 fuse_lck_mtx_lock(data->aw_mtx);
160 TAILQ_FOREACH_SAFE(otick, &data->aw_head, tk_aw_link, x_tick) {
161 if (otick->tk_unique == fii->unique) {
166 fuse_lck_mtx_unlock(data->aw_mtx);
169 fuse_interrupt_send(otick);
171 /* Original is already complete; nothing to do */
175 /* Illegal FUSE_INTERRUPT response */
181 fuse_interrupt_send(struct fuse_ticket *otick)
183 struct fuse_dispatcher fdi;
184 struct fuse_interrupt_in *fii;
185 struct fuse_in_header *ftick_hdr;
186 struct fuse_data *data = otick->tk_data;
187 struct ucred reused_creds;
189 ftick_hdr = fticket_in_header(otick);
190 reused_creds.cr_uid = ftick_hdr->uid;
191 reused_creds.cr_rgid = ftick_hdr->gid;
192 fdisp_init(&fdi, sizeof(*fii));
193 fdisp_make_pid(&fdi, FUSE_INTERRUPT, data, ftick_hdr->nodeid,
194 ftick_hdr->pid, &reused_creds);
197 fii->unique = otick->tk_unique;
198 fuse_insert_callback(fdi.tick, fuse_interrupt_callback);
200 fuse_insert_message(fdi.tick);
205 fiov_init(struct fuse_iov *fiov, size_t size)
207 uint32_t msize = FU_AT_LEAST(size);
211 fiov->base = malloc(msize, M_FUSEMSG, M_WAITOK | M_ZERO);
213 fiov->allocated_size = msize;
214 fiov->credit = fuse_iov_credit;
218 fiov_teardown(struct fuse_iov *fiov)
220 MPASS(fiov->base != NULL);
221 free(fiov->base, M_FUSEMSG);
225 fiov_adjust(struct fuse_iov *fiov, size_t size)
227 if (fiov->allocated_size < size ||
228 (fuse_iov_permanent_bufsize >= 0 &&
229 fiov->allocated_size - size > fuse_iov_permanent_bufsize &&
230 --fiov->credit < 0)) {
232 fiov->base = realloc(fiov->base, FU_AT_LEAST(size), M_FUSEMSG,
235 panic("FUSE: realloc failed");
237 fiov->allocated_size = FU_AT_LEAST(size);
238 fiov->credit = fuse_iov_credit;
239 /* Clear data buffer after reallocation */
240 bzero(fiov->base, size);
241 } else if (size > fiov->len) {
242 /* Clear newly extended portion of data buffer */
243 bzero((char*)fiov->base + fiov->len, size - fiov->len);
248 /* Clear the fiov's data buffer */
250 fiov_clear(struct fuse_iov *fiov)
252 bzero(fiov->base, fiov->len);
255 /* Resize the fiov if needed, and clear it's buffer */
257 fiov_refresh(struct fuse_iov *fiov)
259 fiov_adjust(fiov, 0);
263 fticket_ctor(void *mem, int size, void *arg, int flags)
265 struct fuse_ticket *ftick = mem;
266 struct fuse_data *data = arg;
268 FUSE_ASSERT_MS_DONE(ftick);
269 FUSE_ASSERT_AW_DONE(ftick);
271 ftick->tk_data = data;
273 if (ftick->tk_unique != 0)
274 fticket_refresh(ftick);
276 /* May be truncated to 32 bits */
277 ftick->tk_unique = atomic_fetchadd_long(&data->ticketer, 1);
278 if (ftick->tk_unique == 0)
279 ftick->tk_unique = atomic_fetchadd_long(&data->ticketer, 1);
281 refcount_init(&ftick->tk_refcount, 1);
282 atomic_add_acq_int(&fuse_ticket_count, 1);
288 fticket_dtor(void *mem, int size, void *arg)
291 struct fuse_ticket *ftick = mem;
294 FUSE_ASSERT_MS_DONE(ftick);
295 FUSE_ASSERT_AW_DONE(ftick);
297 atomic_subtract_acq_int(&fuse_ticket_count, 1);
301 fticket_init(void *mem, int size, int flags)
303 struct fuse_ticket *ftick = mem;
305 bzero(ftick, sizeof(struct fuse_ticket));
307 fiov_init(&ftick->tk_ms_fiov, sizeof(struct fuse_in_header));
308 ftick->tk_ms_type = FT_M_FIOV;
310 mtx_init(&ftick->tk_aw_mtx, "fuse answer delivery mutex", NULL, MTX_DEF);
311 fiov_init(&ftick->tk_aw_fiov, 0);
312 ftick->tk_aw_type = FT_A_FIOV;
318 fticket_fini(void *mem, int size)
320 struct fuse_ticket *ftick = mem;
322 fiov_teardown(&ftick->tk_ms_fiov);
323 fiov_teardown(&ftick->tk_aw_fiov);
324 mtx_destroy(&ftick->tk_aw_mtx);
327 static inline struct fuse_ticket *
328 fticket_alloc(struct fuse_data *data)
330 return uma_zalloc_arg(ticket_zone, data, M_WAITOK);
334 fticket_destroy(struct fuse_ticket *ftick)
336 return uma_zfree(ticket_zone, ftick);
341 fticket_refresh(struct fuse_ticket *ftick)
343 FUSE_ASSERT_MS_DONE(ftick);
344 FUSE_ASSERT_AW_DONE(ftick);
346 fiov_refresh(&ftick->tk_ms_fiov);
347 ftick->tk_ms_bufdata = NULL;
348 ftick->tk_ms_bufsize = 0;
349 ftick->tk_ms_type = FT_M_FIOV;
351 bzero(&ftick->tk_aw_ohead, sizeof(struct fuse_out_header));
353 fiov_refresh(&ftick->tk_aw_fiov);
354 ftick->tk_aw_errno = 0;
355 ftick->tk_aw_bufdata = NULL;
356 ftick->tk_aw_bufsize = 0;
357 ftick->tk_aw_type = FT_A_FIOV;
362 /* Prepar the ticket to be reused, but don't clear its data buffers */
364 fticket_reset(struct fuse_ticket *ftick)
366 FUSE_ASSERT_MS_DONE(ftick);
367 FUSE_ASSERT_AW_DONE(ftick);
369 ftick->tk_ms_bufdata = NULL;
370 ftick->tk_ms_bufsize = 0;
371 ftick->tk_ms_type = FT_M_FIOV;
373 bzero(&ftick->tk_aw_ohead, sizeof(struct fuse_out_header));
375 ftick->tk_aw_errno = 0;
376 ftick->tk_aw_bufdata = NULL;
377 ftick->tk_aw_bufsize = 0;
378 ftick->tk_aw_type = FT_A_FIOV;
384 fticket_wait_answer(struct fuse_ticket *ftick)
386 struct thread *td = curthread;
387 sigset_t blockedset, oldset;
389 struct fuse_data *data;
391 fuse_lck_mtx_lock(ftick->tk_aw_mtx);
393 SIGEMPTYSET(blockedset);
394 kern_sigprocmask(td, SIG_BLOCK, &blockedset, &oldset, 0);
396 if (fticket_answered(ftick)) {
399 data = ftick->tk_data;
401 if (fdata_get_dead(data)) {
403 fticket_set_answered(ftick);
406 err = msleep(ftick, &ftick->tk_aw_mtx, PCATCH, "fu_ans",
407 data->daemon_timeout * hz);
408 kern_sigprocmask(td, SIG_SETMASK, &oldset, NULL, 0);
409 if (err == EWOULDBLOCK) {
410 SDT_PROBE2(fuse, , ipc, trace, 3,
411 "fticket_wait_answer: EWOULDBLOCK");
412 #ifdef XXXIP /* die conditionally */
413 if (!fdata_get_dead(data)) {
414 fdata_set_dead(data);
418 fticket_set_answered(ftick);
419 } else if ((err == EINTR || err == ERESTART)) {
421 * Whether we get EINTR or ERESTART depends on whether
422 * SA_RESTART was set by sigaction(2).
424 * Try to interrupt the operation and wait for an EINTR response
425 * to the original operation. If the file system does not
426 * support FUSE_INTERRUPT, then we'll just wait for it to
427 * complete like normal. If it does support FUSE_INTERRUPT,
428 * then it will either respond EINTR to the original operation,
429 * or EAGAIN to the interrupt.
433 SDT_PROBE2(fuse, , ipc, trace, 4,
434 "fticket_wait_answer: interrupt");
435 fuse_lck_mtx_unlock(ftick->tk_aw_mtx);
436 fuse_interrupt_send(ftick);
437 fuse_lck_mtx_lock(ftick->tk_aw_mtx);
439 /* TODO: return, rather than retry, for fatal signals */
442 * Block the just-delivered signal while we wait for an
445 PROC_LOCK(td->td_proc);
446 mtx_lock(&td->td_proc->p_sigacts->ps_mtx);
448 mtx_unlock(&td->td_proc->p_sigacts->ps_mtx);
449 PROC_UNLOCK(td->td_proc);
450 SIGADDSET(blockedset, sig);
451 kern_sigprocmask(curthread, SIG_BLOCK, &blockedset, NULL, 0);
454 SDT_PROBE2(fuse, , ipc, trace, 6,
455 "fticket_wait_answer: other error");
457 SDT_PROBE2(fuse, , ipc, trace, 7, "fticket_wait_answer: OK");
460 if (!(err || fticket_answered(ftick))) {
461 SDT_PROBE2(fuse, , ipc, trace, 1,
462 "FUSE: requester was woken up but still no answer");
465 fuse_lck_mtx_unlock(ftick->tk_aw_mtx);
472 fticket_aw_pull_uio(struct fuse_ticket *ftick, struct uio *uio)
475 size_t len = uio_resid(uio);
478 switch (ftick->tk_aw_type) {
480 fiov_adjust(fticket_resp(ftick), len);
481 err = uiomove(fticket_resp(ftick)->base, len, uio);
485 ftick->tk_aw_bufsize = len;
486 err = uiomove(ftick->tk_aw_bufdata, len, uio);
490 panic("FUSE: unknown answer type for ticket %p", ftick);
497 fticket_pull(struct fuse_ticket *ftick, struct uio *uio)
501 if (ftick->tk_aw_ohead.error) {
504 err = fuse_body_audit(ftick, uio_resid(uio));
506 err = fticket_aw_pull_uio(ftick, uio);
512 fdata_alloc(struct cdev *fdev, struct ucred *cred)
514 struct fuse_data *data;
516 data = malloc(sizeof(struct fuse_data), M_FUSEMSG, M_WAITOK | M_ZERO);
519 mtx_init(&data->ms_mtx, "fuse message list mutex", NULL, MTX_DEF);
520 STAILQ_INIT(&data->ms_head);
521 mtx_init(&data->aw_mtx, "fuse answer list mutex", NULL, MTX_DEF);
522 TAILQ_INIT(&data->aw_head);
523 data->daemoncred = crhold(cred);
524 data->daemon_timeout = FUSE_DEFAULT_DAEMON_TIMEOUT;
525 sx_init(&data->rename_lock, "fuse rename lock");
532 fdata_trydestroy(struct fuse_data *data)
535 MPASS(data->ref >= 0);
539 /* Driving off stage all that stuff thrown at device... */
540 mtx_destroy(&data->ms_mtx);
541 mtx_destroy(&data->aw_mtx);
542 sx_destroy(&data->rename_lock);
544 crfree(data->daemoncred);
546 free(data, M_FUSEMSG);
550 fdata_set_dead(struct fuse_data *data)
553 if (fdata_get_dead(data)) {
557 fuse_lck_mtx_lock(data->ms_mtx);
558 data->dataflags |= FSESS_DEAD;
560 selwakeuppri(&data->ks_rsel, PZERO + 1);
561 wakeup(&data->ticketer);
562 fuse_lck_mtx_unlock(data->ms_mtx);
567 fuse_ticket_fetch(struct fuse_data *data)
570 struct fuse_ticket *ftick;
572 ftick = fticket_alloc(data);
574 if (!(data->dataflags & FSESS_INITED)) {
575 /* Sleep until get answer for INIT messsage */
577 if (!(data->dataflags & FSESS_INITED) && data->ticketer > 2) {
578 err = msleep(&data->ticketer, &fuse_mtx, PCATCH | PDROP,
581 fdata_set_dead(data);
589 fuse_ticket_drop(struct fuse_ticket *ftick)
593 die = refcount_release(&ftick->tk_refcount);
595 fticket_destroy(ftick);
601 fuse_insert_callback(struct fuse_ticket *ftick, fuse_handler_t * handler)
603 if (fdata_get_dead(ftick->tk_data)) {
606 ftick->tk_aw_handler = handler;
608 fuse_lck_mtx_lock(ftick->tk_data->aw_mtx);
610 fuse_lck_mtx_unlock(ftick->tk_data->aw_mtx);
614 fuse_insert_message(struct fuse_ticket *ftick)
616 if (ftick->tk_flag & FT_DIRTY) {
617 panic("FUSE: ticket reused without being refreshed");
619 ftick->tk_flag |= FT_DIRTY;
621 if (fdata_get_dead(ftick->tk_data)) {
624 fuse_lck_mtx_lock(ftick->tk_data->ms_mtx);
626 wakeup_one(ftick->tk_data);
627 selwakeuppri(&ftick->tk_data->ks_rsel, PZERO + 1);
628 fuse_lck_mtx_unlock(ftick->tk_data->ms_mtx);
632 fuse_body_audit(struct fuse_ticket *ftick, size_t blen)
635 enum fuse_opcode opcode;
637 opcode = fticket_opcode(ftick);
641 err = (blen == sizeof(struct fuse_entry_out)) ? 0 : EINVAL;
645 panic("FUSE: a handler has been intalled for FUSE_FORGET");
649 err = (blen == sizeof(struct fuse_attr_out)) ? 0 : EINVAL;
653 err = (blen == sizeof(struct fuse_attr_out)) ? 0 : EINVAL;
657 err = (PAGE_SIZE >= blen) ? 0 : EINVAL;
661 err = (blen == sizeof(struct fuse_entry_out)) ? 0 : EINVAL;
665 err = (blen == sizeof(struct fuse_entry_out)) ? 0 : EINVAL;
669 err = (blen == sizeof(struct fuse_entry_out)) ? 0 : EINVAL;
673 err = (blen == 0) ? 0 : EINVAL;
677 err = (blen == 0) ? 0 : EINVAL;
681 err = (blen == 0) ? 0 : EINVAL;
685 err = (blen == sizeof(struct fuse_entry_out)) ? 0 : EINVAL;
689 err = (blen == sizeof(struct fuse_open_out)) ? 0 : EINVAL;
693 err = (((struct fuse_read_in *)(
694 (char *)ftick->tk_ms_fiov.base +
695 sizeof(struct fuse_in_header)
696 ))->size >= blen) ? 0 : EINVAL;
700 err = (blen == sizeof(struct fuse_write_out)) ? 0 : EINVAL;
704 if (fuse_libabi_geq(ftick->tk_data, 7, 4)) {
705 err = (blen == sizeof(struct fuse_statfs_out)) ?
708 err = (blen == FUSE_COMPAT_STATFS_SIZE) ? 0 : EINVAL;
713 err = (blen == 0) ? 0 : EINVAL;
717 err = (blen == 0) ? 0 : EINVAL;
721 err = (blen == 0) ? 0 : EINVAL;
727 * These can have varying response lengths, and 0 length
728 * isn't necessarily invalid.
733 case FUSE_REMOVEXATTR:
734 err = (blen == 0) ? 0 : EINVAL;
738 err = (blen == 0) ? 0 : EINVAL;
742 if (blen == sizeof(struct fuse_init_out) || blen == 8) {
750 err = (blen == sizeof(struct fuse_open_out)) ? 0 : EINVAL;
754 err = (((struct fuse_read_in *)(
755 (char *)ftick->tk_ms_fiov.base +
756 sizeof(struct fuse_in_header)
757 ))->size >= blen) ? 0 : EINVAL;
760 case FUSE_RELEASEDIR:
761 err = (blen == 0) ? 0 : EINVAL;
765 err = (blen == 0) ? 0 : EINVAL;
769 err = (blen == sizeof(struct fuse_lk_out)) ? 0 : EINVAL;
773 err = (blen == 0) ? 0 : EINVAL;
777 err = (blen == 0) ? 0 : EINVAL;
781 err = (blen == 0) ? 0 : EINVAL;
785 err = (blen == sizeof(struct fuse_entry_out) +
786 sizeof(struct fuse_open_out)) ? 0 : EINVAL;
790 err = (blen == 0) ? 0 : EINVAL;
794 panic("FUSE: opcodes out of sync (%d)\n", opcode);
801 fuse_setup_ihead(struct fuse_in_header *ihead, struct fuse_ticket *ftick,
802 uint64_t nid, enum fuse_opcode op, size_t blen, pid_t pid,
805 ihead->len = sizeof(*ihead) + blen;
806 ihead->unique = ftick->tk_unique;
811 ihead->uid = cred->cr_uid;
812 ihead->gid = cred->cr_rgid;
816 * fuse_standard_handler just pulls indata and wakes up pretender.
817 * Doesn't try to interpret data, that's left for the pretender.
818 * Though might do a basic size verification before the pull-in takes place
822 fuse_standard_handler(struct fuse_ticket *ftick, struct uio *uio)
826 err = fticket_pull(ftick, uio);
828 fuse_lck_mtx_lock(ftick->tk_aw_mtx);
830 if (!fticket_answered(ftick)) {
831 fticket_set_answered(ftick);
832 ftick->tk_aw_errno = err;
835 fuse_lck_mtx_unlock(ftick->tk_aw_mtx);
841 * Reinitialize a dispatcher from a pid and node id, without resizing or
842 * clearing its data buffers
845 fdisp_refresh_pid(struct fuse_dispatcher *fdip, enum fuse_opcode op,
846 struct mount *mp, uint64_t nid, pid_t pid, struct ucred *cred)
849 MPASS2(sizeof(fdip->finh) + fdip->iosize <= fdip->tick->tk_ms_fiov.len,
850 "Must use fdisp_make_pid to increase the size of the fiov");
851 fticket_reset(fdip->tick);
853 FUSE_DIMALLOC(&fdip->tick->tk_ms_fiov, fdip->finh,
854 fdip->indata, fdip->iosize);
856 fuse_setup_ihead(fdip->finh, fdip->tick, nid, op, fdip->iosize, pid,
860 /* Initialize a dispatcher from a pid and node id */
862 fdisp_make_pid(struct fuse_dispatcher *fdip, enum fuse_opcode op,
863 struct fuse_data *data, uint64_t nid, pid_t pid, struct ucred *cred)
866 fticket_refresh(fdip->tick);
868 fdip->tick = fuse_ticket_fetch(data);
871 /* FUSE_DIMALLOC will bzero the fiovs when it enlarges them */
872 FUSE_DIMALLOC(&fdip->tick->tk_ms_fiov, fdip->finh,
873 fdip->indata, fdip->iosize);
875 fuse_setup_ihead(fdip->finh, fdip->tick, nid, op, fdip->iosize, pid, cred);
879 fdisp_make(struct fuse_dispatcher *fdip, enum fuse_opcode op, struct mount *mp,
880 uint64_t nid, struct thread *td, struct ucred *cred)
882 struct fuse_data *data = fuse_get_mpdata(mp);
883 RECTIFY_TDCR(td, cred);
885 return fdisp_make_pid(fdip, op, data, nid, td->td_proc->p_pid, cred);
889 fdisp_make_vp(struct fuse_dispatcher *fdip, enum fuse_opcode op,
890 struct vnode *vp, struct thread *td, struct ucred *cred)
892 struct mount *mp = vnode_mount(vp);
893 struct fuse_data *data = fuse_get_mpdata(mp);
895 RECTIFY_TDCR(td, cred);
896 return fdisp_make_pid(fdip, op, data, VTOI(vp),
897 td->td_proc->p_pid, cred);
900 /* Refresh a fuse_dispatcher so it can be reused, but don't zero its data */
902 fdisp_refresh_vp(struct fuse_dispatcher *fdip, enum fuse_opcode op,
903 struct vnode *vp, struct thread *td, struct ucred *cred)
905 RECTIFY_TDCR(td, cred);
906 return fdisp_refresh_pid(fdip, op, vnode_mount(vp), VTOI(vp),
907 td->td_proc->p_pid, cred);
911 fdisp_refresh(struct fuse_dispatcher *fdip)
913 fticket_refresh(fdip->tick);
916 SDT_PROBE_DEFINE2(fuse, , ipc, fdisp_wait_answ_error, "char*", "int");
919 fdisp_wait_answ(struct fuse_dispatcher *fdip)
924 fuse_insert_callback(fdip->tick, fuse_standard_handler);
925 fuse_insert_message(fdip->tick);
927 if ((err = fticket_wait_answer(fdip->tick))) {
928 fuse_lck_mtx_lock(fdip->tick->tk_aw_mtx);
930 if (fticket_answered(fdip->tick)) {
932 * Just between noticing the interrupt and getting here,
933 * the standard handler has completed his job.
934 * So we drop the ticket and exit as usual.
936 SDT_PROBE2(fuse, , ipc, fdisp_wait_answ_error,
937 "IPC: interrupted, already answered", err);
938 fuse_lck_mtx_unlock(fdip->tick->tk_aw_mtx);
942 * So we were faster than the standard handler.
943 * Then by setting the answered flag we get *him*
944 * to drop the ticket.
946 SDT_PROBE2(fuse, , ipc, fdisp_wait_answ_error,
947 "IPC: interrupted, setting to answered", err);
948 fticket_set_answered(fdip->tick);
949 fuse_lck_mtx_unlock(fdip->tick->tk_aw_mtx);
954 if (fdip->tick->tk_aw_errno) {
955 SDT_PROBE2(fuse, , ipc, fdisp_wait_answ_error,
956 "IPC: explicit EIO-ing", fdip->tick->tk_aw_errno);
960 if ((err = fdip->tick->tk_aw_ohead.error)) {
961 SDT_PROBE2(fuse, , ipc, fdisp_wait_answ_error,
962 "IPC: setting status", fdip->tick->tk_aw_ohead.error);
964 * This means a "proper" fuse syscall error.
965 * We record this value so the caller will
966 * be able to know it's not a boring messaging
967 * failure, if she wishes so (and if not, she can
968 * just simply propagate the return value of this routine).
969 * [XXX Maybe a bitflag would do the job too,
970 * if other flags needed, this will be converted thusly.]
972 fdip->answ_stat = err;
975 fdip->answ = fticket_resp(fdip->tick)->base;
976 fdip->iosize = fticket_resp(fdip->tick)->len;
987 ticket_zone = uma_zcreate("fuse_ticket", sizeof(struct fuse_ticket),
988 fticket_ctor, fticket_dtor, fticket_init, fticket_fini,
993 fuse_ipc_destroy(void)
995 uma_zdestroy(ticket_zone);