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(fusefs);
90 * arg0: verbosity. Higher numbers give more verbose messages
91 * arg1: Textual message
93 SDT_PROBE_DEFINE2(fusefs, , 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, int err);
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 static int fuse_ticket_count = 0;
114 SYSCTL_INT(_vfs_fusefs, OID_AUTO, ticket_count, CTLFLAG_RW,
115 &fuse_ticket_count, 0, "number of allocated tickets");
116 static long fuse_iov_permanent_bufsize = 1 << 19;
118 SYSCTL_LONG(_vfs_fusefs, OID_AUTO, iov_permanent_bufsize, CTLFLAG_RW,
119 &fuse_iov_permanent_bufsize, 0,
120 "limit for permanently stored buffer size for fuse_iovs");
121 static int fuse_iov_credit = 16;
123 SYSCTL_INT(_vfs_fusefs, OID_AUTO, iov_credit, CTLFLAG_RW,
125 "how many times is an oversized fuse_iov tolerated");
127 MALLOC_DEFINE(M_FUSEMSG, "fuse_msgbuf", "fuse message buffer");
128 static uma_zone_t ticket_zone;
131 * TODO: figure out how to timeout INTERRUPT requests, because the daemon may
132 * leagally never respond
134 * TODO: remove an INTERRUPT request if the daemon responds to the original
137 fuse_interrupt_callback(struct fuse_ticket *tick, struct uio *uio)
139 struct fuse_ticket *otick, *x_tick;
140 struct fuse_interrupt_in *fii;
141 struct fuse_data *data;
142 data = tick->tk_data;
145 fii = (struct fuse_interrupt_in*)((char*)tick->tk_ms_fiov.base +
146 sizeof(struct fuse_in_header));
148 fuse_lck_mtx_lock(data->aw_mtx);
149 TAILQ_FOREACH_SAFE(otick, &data->aw_head, tk_aw_link, x_tick) {
150 if (otick->tk_unique == fii->unique) {
155 fuse_lck_mtx_unlock(data->aw_mtx);
158 /* Original is already complete. Just return */
162 /* Clear the original ticket's interrupt association */
163 otick->irq_unique = 0;
165 if (tick->tk_aw_ohead.error == EAGAIN) {
167 * There are two reasons we might get this:
168 * 1) the daemon received the INTERRUPT request before the
170 * 2) the daemon received the INTERRUPT request after it
171 * completed the original request.
172 * In the first case we should re-send the INTERRUPT. In the
173 * second, we should ignore it.
176 fuse_interrupt_send(otick, EINTR);
179 /* Illegal FUSE_INTERRUPT response */
184 /* Interrupt the operation otick. Return err as its error code */
186 fuse_interrupt_send(struct fuse_ticket *otick, int err)
188 struct fuse_dispatcher fdi;
189 struct fuse_interrupt_in *fii;
190 struct fuse_in_header *ftick_hdr;
191 struct fuse_data *data = otick->tk_data;
192 struct fuse_ticket *tick, *xtick;
193 struct ucred reused_creds;
195 if (otick->irq_unique == 0) {
197 * If the daemon hasn't yet received otick, then we can answer
198 * it ourselves and return.
200 fuse_lck_mtx_lock(data->ms_mtx);
201 STAILQ_FOREACH_SAFE(tick, &otick->tk_data->ms_head, tk_ms_link,
204 STAILQ_REMOVE(&otick->tk_data->ms_head, tick,
205 fuse_ticket, tk_ms_link);
206 otick->tk_ms_link.stqe_next = NULL;
207 fuse_lck_mtx_unlock(data->ms_mtx);
209 fuse_lck_mtx_lock(otick->tk_aw_mtx);
210 if (!fticket_answered(otick)) {
211 fticket_set_answered(otick);
212 otick->tk_aw_errno = err;
215 fuse_lck_mtx_unlock(otick->tk_aw_mtx);
217 fuse_ticket_drop(tick);
221 fuse_lck_mtx_unlock(data->ms_mtx);
224 * If the fuse daemon has already received otick, then we must
225 * send FUSE_INTERRUPT.
227 ftick_hdr = fticket_in_header(otick);
228 reused_creds.cr_uid = ftick_hdr->uid;
229 reused_creds.cr_rgid = ftick_hdr->gid;
230 fdisp_init(&fdi, sizeof(*fii));
231 fdisp_make_pid(&fdi, FUSE_INTERRUPT, data, ftick_hdr->nodeid,
232 ftick_hdr->pid, &reused_creds);
235 fii->unique = otick->tk_unique;
236 fuse_insert_callback(fdi.tick, fuse_interrupt_callback);
238 otick->irq_unique = fdi.tick->tk_unique;
239 /* Interrupt ops should be delivered ASAP */
240 fuse_insert_message(fdi.tick, true);
243 /* This ticket has already been interrupted */
248 fiov_init(struct fuse_iov *fiov, size_t size)
250 uint32_t msize = FU_AT_LEAST(size);
254 fiov->base = malloc(msize, M_FUSEMSG, M_WAITOK | M_ZERO);
256 fiov->allocated_size = msize;
257 fiov->credit = fuse_iov_credit;
261 fiov_teardown(struct fuse_iov *fiov)
263 MPASS(fiov->base != NULL);
264 free(fiov->base, M_FUSEMSG);
268 fiov_adjust(struct fuse_iov *fiov, size_t size)
270 if (fiov->allocated_size < size ||
271 (fuse_iov_permanent_bufsize >= 0 &&
272 fiov->allocated_size - size > fuse_iov_permanent_bufsize &&
273 --fiov->credit < 0)) {
275 fiov->base = realloc(fiov->base, FU_AT_LEAST(size), M_FUSEMSG,
278 panic("FUSE: realloc failed");
280 fiov->allocated_size = FU_AT_LEAST(size);
281 fiov->credit = fuse_iov_credit;
282 /* Clear data buffer after reallocation */
283 bzero(fiov->base, size);
284 } else if (size > fiov->len) {
285 /* Clear newly extended portion of data buffer */
286 bzero((char*)fiov->base + fiov->len, size - fiov->len);
291 /* Clear the fiov's data buffer */
293 fiov_clear(struct fuse_iov *fiov)
295 bzero(fiov->base, fiov->len);
298 /* Resize the fiov if needed, and clear it's buffer */
300 fiov_refresh(struct fuse_iov *fiov)
302 fiov_adjust(fiov, 0);
306 fticket_ctor(void *mem, int size, void *arg, int flags)
308 struct fuse_ticket *ftick = mem;
309 struct fuse_data *data = arg;
311 FUSE_ASSERT_MS_DONE(ftick);
312 FUSE_ASSERT_AW_DONE(ftick);
314 ftick->tk_data = data;
316 if (ftick->tk_unique != 0)
317 fticket_refresh(ftick);
319 /* May be truncated to 32 bits */
320 ftick->tk_unique = atomic_fetchadd_long(&data->ticketer, 1);
321 if (ftick->tk_unique == 0)
322 ftick->tk_unique = atomic_fetchadd_long(&data->ticketer, 1);
324 ftick->irq_unique = 0;
326 refcount_init(&ftick->tk_refcount, 1);
327 atomic_add_acq_int(&fuse_ticket_count, 1);
333 fticket_dtor(void *mem, int size, void *arg)
336 struct fuse_ticket *ftick = mem;
339 FUSE_ASSERT_MS_DONE(ftick);
340 FUSE_ASSERT_AW_DONE(ftick);
342 atomic_subtract_acq_int(&fuse_ticket_count, 1);
346 fticket_init(void *mem, int size, int flags)
348 struct fuse_ticket *ftick = mem;
350 bzero(ftick, sizeof(struct fuse_ticket));
352 fiov_init(&ftick->tk_ms_fiov, sizeof(struct fuse_in_header));
353 ftick->tk_ms_type = FT_M_FIOV;
355 mtx_init(&ftick->tk_aw_mtx, "fuse answer delivery mutex", NULL, MTX_DEF);
356 fiov_init(&ftick->tk_aw_fiov, 0);
357 ftick->tk_aw_type = FT_A_FIOV;
363 fticket_fini(void *mem, int size)
365 struct fuse_ticket *ftick = mem;
367 fiov_teardown(&ftick->tk_ms_fiov);
368 fiov_teardown(&ftick->tk_aw_fiov);
369 mtx_destroy(&ftick->tk_aw_mtx);
372 static inline struct fuse_ticket *
373 fticket_alloc(struct fuse_data *data)
375 return uma_zalloc_arg(ticket_zone, data, M_WAITOK);
379 fticket_destroy(struct fuse_ticket *ftick)
381 return uma_zfree(ticket_zone, ftick);
386 fticket_refresh(struct fuse_ticket *ftick)
388 FUSE_ASSERT_MS_DONE(ftick);
389 FUSE_ASSERT_AW_DONE(ftick);
391 fiov_refresh(&ftick->tk_ms_fiov);
392 ftick->tk_ms_bufdata = NULL;
393 ftick->tk_ms_bufsize = 0;
394 ftick->tk_ms_type = FT_M_FIOV;
396 bzero(&ftick->tk_aw_ohead, sizeof(struct fuse_out_header));
398 fiov_refresh(&ftick->tk_aw_fiov);
399 ftick->tk_aw_errno = 0;
400 ftick->tk_aw_bufdata = NULL;
401 ftick->tk_aw_bufsize = 0;
402 ftick->tk_aw_type = FT_A_FIOV;
407 /* Prepar the ticket to be reused, but don't clear its data buffers */
409 fticket_reset(struct fuse_ticket *ftick)
411 FUSE_ASSERT_MS_DONE(ftick);
412 FUSE_ASSERT_AW_DONE(ftick);
414 ftick->tk_ms_bufdata = NULL;
415 ftick->tk_ms_bufsize = 0;
416 ftick->tk_ms_type = FT_M_FIOV;
418 bzero(&ftick->tk_aw_ohead, sizeof(struct fuse_out_header));
420 ftick->tk_aw_errno = 0;
421 ftick->tk_aw_bufdata = NULL;
422 ftick->tk_aw_bufsize = 0;
423 ftick->tk_aw_type = FT_A_FIOV;
429 fticket_wait_answer(struct fuse_ticket *ftick)
431 struct thread *td = curthread;
432 sigset_t blockedset, oldset;
434 struct fuse_data *data;
435 SIGEMPTYSET(blockedset);
437 kern_sigprocmask(td, SIG_BLOCK, NULL, &oldset, 0);
439 fuse_lck_mtx_lock(ftick->tk_aw_mtx);
442 if (fticket_answered(ftick)) {
445 data = ftick->tk_data;
447 if (fdata_get_dead(data)) {
449 fticket_set_answered(ftick);
452 kern_sigprocmask(td, SIG_BLOCK, &blockedset, NULL, 0);
453 err = msleep(ftick, &ftick->tk_aw_mtx, PCATCH, "fu_ans",
454 data->daemon_timeout * hz);
455 kern_sigprocmask(td, SIG_SETMASK, &oldset, NULL, 0);
456 if (err == EWOULDBLOCK) {
457 SDT_PROBE2(fusefs, , ipc, trace, 3,
458 "fticket_wait_answer: EWOULDBLOCK");
459 #ifdef XXXIP /* die conditionally */
460 if (!fdata_get_dead(data)) {
461 fdata_set_dead(data);
465 fticket_set_answered(ftick);
466 } else if ((err == EINTR || err == ERESTART)) {
468 * Whether we get EINTR or ERESTART depends on whether
469 * SA_RESTART was set by sigaction(2).
471 * Try to interrupt the operation and wait for an EINTR response
472 * to the original operation. If the file system does not
473 * support FUSE_INTERRUPT, then we'll just wait for it to
474 * complete like normal. If it does support FUSE_INTERRUPT,
475 * then it will either respond EINTR to the original operation,
476 * or EAGAIN to the interrupt.
480 SDT_PROBE2(fusefs, , ipc, trace, 4,
481 "fticket_wait_answer: interrupt");
482 fuse_lck_mtx_unlock(ftick->tk_aw_mtx);
483 fuse_interrupt_send(ftick, err);
485 PROC_LOCK(td->td_proc);
486 mtx_lock(&td->td_proc->p_sigacts->ps_mtx);
488 mtx_unlock(&td->td_proc->p_sigacts->ps_mtx);
489 PROC_UNLOCK(td->td_proc);
491 fuse_lck_mtx_lock(ftick->tk_aw_mtx);
492 if (!sig_isfatal(td->td_proc, sig)) {
494 * Block the just-delivered signal while we wait for an
497 SIGADDSET(blockedset, sig);
500 /* Return immediately for fatal signals */
503 SDT_PROBE2(fusefs, , ipc, trace, 6,
504 "fticket_wait_answer: other error");
506 SDT_PROBE2(fusefs, , ipc, trace, 7, "fticket_wait_answer: OK");
509 if (!(err || fticket_answered(ftick))) {
510 SDT_PROBE2(fusefs, , ipc, trace, 1,
511 "FUSE: requester was woken up but still no answer");
514 fuse_lck_mtx_unlock(ftick->tk_aw_mtx);
521 fticket_aw_pull_uio(struct fuse_ticket *ftick, struct uio *uio)
524 size_t len = uio_resid(uio);
527 switch (ftick->tk_aw_type) {
529 fiov_adjust(fticket_resp(ftick), len);
530 err = uiomove(fticket_resp(ftick)->base, len, uio);
534 ftick->tk_aw_bufsize = len;
535 err = uiomove(ftick->tk_aw_bufdata, len, uio);
539 panic("FUSE: unknown answer type for ticket %p", ftick);
546 fticket_pull(struct fuse_ticket *ftick, struct uio *uio)
550 if (ftick->tk_aw_ohead.error) {
553 err = fuse_body_audit(ftick, uio_resid(uio));
555 err = fticket_aw_pull_uio(ftick, uio);
561 fdata_alloc(struct cdev *fdev, struct ucred *cred)
563 struct fuse_data *data;
565 data = malloc(sizeof(struct fuse_data), M_FUSEMSG, M_WAITOK | M_ZERO);
568 mtx_init(&data->ms_mtx, "fuse message list mutex", NULL, MTX_DEF);
569 STAILQ_INIT(&data->ms_head);
570 mtx_init(&data->aw_mtx, "fuse answer list mutex", NULL, MTX_DEF);
571 TAILQ_INIT(&data->aw_head);
572 data->daemoncred = crhold(cred);
573 data->daemon_timeout = FUSE_DEFAULT_DAEMON_TIMEOUT;
574 sx_init(&data->rename_lock, "fuse rename lock");
581 fdata_trydestroy(struct fuse_data *data)
584 MPASS(data->ref >= 0);
588 /* Driving off stage all that stuff thrown at device... */
589 mtx_destroy(&data->ms_mtx);
590 mtx_destroy(&data->aw_mtx);
591 sx_destroy(&data->rename_lock);
593 crfree(data->daemoncred);
595 free(data, M_FUSEMSG);
599 fdata_set_dead(struct fuse_data *data)
602 if (fdata_get_dead(data)) {
606 fuse_lck_mtx_lock(data->ms_mtx);
607 data->dataflags |= FSESS_DEAD;
609 selwakeuppri(&data->ks_rsel, PZERO + 1);
610 wakeup(&data->ticketer);
611 fuse_lck_mtx_unlock(data->ms_mtx);
616 fuse_ticket_fetch(struct fuse_data *data)
619 struct fuse_ticket *ftick;
621 ftick = fticket_alloc(data);
623 if (!(data->dataflags & FSESS_INITED)) {
624 /* Sleep until get answer for INIT messsage */
626 if (!(data->dataflags & FSESS_INITED) && data->ticketer > 2) {
627 err = msleep(&data->ticketer, &fuse_mtx, PCATCH | PDROP,
630 fdata_set_dead(data);
638 fuse_ticket_drop(struct fuse_ticket *ftick)
642 die = refcount_release(&ftick->tk_refcount);
644 fticket_destroy(ftick);
650 fuse_insert_callback(struct fuse_ticket *ftick, fuse_handler_t * handler)
652 if (fdata_get_dead(ftick->tk_data)) {
655 ftick->tk_aw_handler = handler;
657 fuse_lck_mtx_lock(ftick->tk_data->aw_mtx);
659 fuse_lck_mtx_unlock(ftick->tk_data->aw_mtx);
663 * Insert a new upgoing ticket into the message queue
665 * If urgent is true, insert at the front of the queue. Otherwise, insert in
669 fuse_insert_message(struct fuse_ticket *ftick, bool urgent)
671 if (ftick->tk_flag & FT_DIRTY) {
672 panic("FUSE: ticket reused without being refreshed");
674 ftick->tk_flag |= FT_DIRTY;
676 if (fdata_get_dead(ftick->tk_data)) {
679 fuse_lck_mtx_lock(ftick->tk_data->ms_mtx);
681 fuse_ms_push_head(ftick);
684 wakeup_one(ftick->tk_data);
685 selwakeuppri(&ftick->tk_data->ks_rsel, PZERO + 1);
686 fuse_lck_mtx_unlock(ftick->tk_data->ms_mtx);
690 fuse_body_audit(struct fuse_ticket *ftick, size_t blen)
693 enum fuse_opcode opcode;
695 opcode = fticket_opcode(ftick);
699 err = (blen == sizeof(struct fuse_entry_out)) ? 0 : EINVAL;
703 panic("FUSE: a handler has been intalled for FUSE_FORGET");
707 err = (blen == sizeof(struct fuse_attr_out)) ? 0 : EINVAL;
711 err = (blen == sizeof(struct fuse_attr_out)) ? 0 : EINVAL;
715 err = (PAGE_SIZE >= blen) ? 0 : EINVAL;
719 err = (blen == sizeof(struct fuse_entry_out)) ? 0 : EINVAL;
723 err = (blen == sizeof(struct fuse_entry_out)) ? 0 : EINVAL;
727 err = (blen == sizeof(struct fuse_entry_out)) ? 0 : EINVAL;
731 err = (blen == 0) ? 0 : EINVAL;
735 err = (blen == 0) ? 0 : EINVAL;
739 err = (blen == 0) ? 0 : EINVAL;
743 err = (blen == sizeof(struct fuse_entry_out)) ? 0 : EINVAL;
747 err = (blen == sizeof(struct fuse_open_out)) ? 0 : EINVAL;
751 err = (((struct fuse_read_in *)(
752 (char *)ftick->tk_ms_fiov.base +
753 sizeof(struct fuse_in_header)
754 ))->size >= blen) ? 0 : EINVAL;
758 err = (blen == sizeof(struct fuse_write_out)) ? 0 : EINVAL;
762 if (fuse_libabi_geq(ftick->tk_data, 7, 4)) {
763 err = (blen == sizeof(struct fuse_statfs_out)) ?
766 err = (blen == FUSE_COMPAT_STATFS_SIZE) ? 0 : EINVAL;
771 err = (blen == 0) ? 0 : EINVAL;
775 err = (blen == 0) ? 0 : EINVAL;
779 err = (blen == 0) ? 0 : EINVAL;
785 * These can have varying response lengths, and 0 length
786 * isn't necessarily invalid.
791 case FUSE_REMOVEXATTR:
792 err = (blen == 0) ? 0 : EINVAL;
796 err = (blen == 0) ? 0 : EINVAL;
800 if (blen == sizeof(struct fuse_init_out) || blen == 8) {
808 err = (blen == sizeof(struct fuse_open_out)) ? 0 : EINVAL;
812 err = (((struct fuse_read_in *)(
813 (char *)ftick->tk_ms_fiov.base +
814 sizeof(struct fuse_in_header)
815 ))->size >= blen) ? 0 : EINVAL;
818 case FUSE_RELEASEDIR:
819 err = (blen == 0) ? 0 : EINVAL;
823 err = (blen == 0) ? 0 : EINVAL;
827 err = (blen == sizeof(struct fuse_lk_out)) ? 0 : EINVAL;
831 err = (blen == 0) ? 0 : EINVAL;
835 err = (blen == 0) ? 0 : EINVAL;
839 err = (blen == 0) ? 0 : EINVAL;
843 err = (blen == sizeof(struct fuse_entry_out) +
844 sizeof(struct fuse_open_out)) ? 0 : EINVAL;
848 err = (blen == 0) ? 0 : EINVAL;
852 panic("FUSE: opcodes out of sync (%d)\n", opcode);
859 fuse_setup_ihead(struct fuse_in_header *ihead, struct fuse_ticket *ftick,
860 uint64_t nid, enum fuse_opcode op, size_t blen, pid_t pid,
863 ihead->len = sizeof(*ihead) + blen;
864 ihead->unique = ftick->tk_unique;
869 ihead->uid = cred->cr_uid;
870 ihead->gid = cred->cr_rgid;
874 * fuse_standard_handler just pulls indata and wakes up pretender.
875 * Doesn't try to interpret data, that's left for the pretender.
876 * Though might do a basic size verification before the pull-in takes place
880 fuse_standard_handler(struct fuse_ticket *ftick, struct uio *uio)
884 err = fticket_pull(ftick, uio);
886 fuse_lck_mtx_lock(ftick->tk_aw_mtx);
888 if (!fticket_answered(ftick)) {
889 fticket_set_answered(ftick);
890 ftick->tk_aw_errno = err;
893 fuse_lck_mtx_unlock(ftick->tk_aw_mtx);
899 * Reinitialize a dispatcher from a pid and node id, without resizing or
900 * clearing its data buffers
903 fdisp_refresh_pid(struct fuse_dispatcher *fdip, enum fuse_opcode op,
904 struct mount *mp, uint64_t nid, pid_t pid, struct ucred *cred)
907 MPASS2(sizeof(fdip->finh) + fdip->iosize <= fdip->tick->tk_ms_fiov.len,
908 "Must use fdisp_make_pid to increase the size of the fiov");
909 fticket_reset(fdip->tick);
911 FUSE_DIMALLOC(&fdip->tick->tk_ms_fiov, fdip->finh,
912 fdip->indata, fdip->iosize);
914 fuse_setup_ihead(fdip->finh, fdip->tick, nid, op, fdip->iosize, pid,
918 /* Initialize a dispatcher from a pid and node id */
920 fdisp_make_pid(struct fuse_dispatcher *fdip, enum fuse_opcode op,
921 struct fuse_data *data, uint64_t nid, pid_t pid, struct ucred *cred)
924 fticket_refresh(fdip->tick);
926 fdip->tick = fuse_ticket_fetch(data);
929 /* FUSE_DIMALLOC will bzero the fiovs when it enlarges them */
930 FUSE_DIMALLOC(&fdip->tick->tk_ms_fiov, fdip->finh,
931 fdip->indata, fdip->iosize);
933 fuse_setup_ihead(fdip->finh, fdip->tick, nid, op, fdip->iosize, pid, cred);
937 fdisp_make(struct fuse_dispatcher *fdip, enum fuse_opcode op, struct mount *mp,
938 uint64_t nid, struct thread *td, struct ucred *cred)
940 struct fuse_data *data = fuse_get_mpdata(mp);
941 RECTIFY_TDCR(td, cred);
943 return fdisp_make_pid(fdip, op, data, nid, td->td_proc->p_pid, cred);
947 fdisp_make_vp(struct fuse_dispatcher *fdip, enum fuse_opcode op,
948 struct vnode *vp, struct thread *td, struct ucred *cred)
950 struct mount *mp = vnode_mount(vp);
951 struct fuse_data *data = fuse_get_mpdata(mp);
953 RECTIFY_TDCR(td, cred);
954 return fdisp_make_pid(fdip, op, data, VTOI(vp),
955 td->td_proc->p_pid, cred);
958 /* Refresh a fuse_dispatcher so it can be reused, but don't zero its data */
960 fdisp_refresh_vp(struct fuse_dispatcher *fdip, enum fuse_opcode op,
961 struct vnode *vp, struct thread *td, struct ucred *cred)
963 RECTIFY_TDCR(td, cred);
964 return fdisp_refresh_pid(fdip, op, vnode_mount(vp), VTOI(vp),
965 td->td_proc->p_pid, cred);
969 fdisp_refresh(struct fuse_dispatcher *fdip)
971 fticket_refresh(fdip->tick);
974 SDT_PROBE_DEFINE2(fusefs, , ipc, fdisp_wait_answ_error, "char*", "int");
977 fdisp_wait_answ(struct fuse_dispatcher *fdip)
982 fuse_insert_callback(fdip->tick, fuse_standard_handler);
983 fuse_insert_message(fdip->tick, false);
985 if ((err = fticket_wait_answer(fdip->tick))) {
986 fuse_lck_mtx_lock(fdip->tick->tk_aw_mtx);
988 if (fticket_answered(fdip->tick)) {
990 * Just between noticing the interrupt and getting here,
991 * the standard handler has completed his job.
992 * So we drop the ticket and exit as usual.
994 SDT_PROBE2(fusefs, , ipc, fdisp_wait_answ_error,
995 "IPC: interrupted, already answered", err);
996 fuse_lck_mtx_unlock(fdip->tick->tk_aw_mtx);
1000 * So we were faster than the standard handler.
1001 * Then by setting the answered flag we get *him*
1002 * to drop the ticket.
1004 SDT_PROBE2(fusefs, , ipc, fdisp_wait_answ_error,
1005 "IPC: interrupted, setting to answered", err);
1006 fticket_set_answered(fdip->tick);
1007 fuse_lck_mtx_unlock(fdip->tick->tk_aw_mtx);
1012 if (fdip->tick->tk_aw_errno) {
1013 SDT_PROBE2(fusefs, , ipc, fdisp_wait_answ_error,
1014 "IPC: explicit EIO-ing", fdip->tick->tk_aw_errno);
1018 if ((err = fdip->tick->tk_aw_ohead.error)) {
1019 SDT_PROBE2(fusefs, , ipc, fdisp_wait_answ_error,
1020 "IPC: setting status", fdip->tick->tk_aw_ohead.error);
1022 * This means a "proper" fuse syscall error.
1023 * We record this value so the caller will
1024 * be able to know it's not a boring messaging
1025 * failure, if she wishes so (and if not, she can
1026 * just simply propagate the return value of this routine).
1027 * [XXX Maybe a bitflag would do the job too,
1028 * if other flags needed, this will be converted thusly.]
1030 fdip->answ_stat = err;
1033 fdip->answ = fticket_resp(fdip->tick)->base;
1034 fdip->iosize = fticket_resp(fdip->tick)->len;
1045 ticket_zone = uma_zcreate("fuse_ticket", sizeof(struct fuse_ticket),
1046 fticket_ctor, fticket_dtor, fticket_init, fticket_fini,
1051 fuse_ipc_destroy(void)
1053 uma_zdestroy(ticket_zone);