]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/fs/fuse/fuse_ipc.c
fusefs: don't send FUSE_INTERRUPT for ops that are still in-kernel
[FreeBSD/FreeBSD.git] / sys / fs / fuse / fuse_ipc.c
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2007-2009 Google Inc. and Amit Singh
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are
9  * met:
10  *
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
16  *   distribution.
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.
20  *
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.
32  *
33  * Copyright (C) 2005 Csaba Henk.
34  * All rights reserved.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
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.
44  *
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
55  * SUCH DAMAGE.
56  */
57
58 #include <sys/cdefs.h>
59 __FBSDID("$FreeBSD$");
60
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>
66 #include <sys/conf.h>
67 #include <sys/uio.h>
68 #include <sys/malloc.h>
69 #include <sys/queue.h>
70 #include <sys/lock.h>
71 #include <sys/sx.h>
72 #include <sys/mutex.h>
73 #include <sys/proc.h>
74 #include <sys/mount.h>
75 #include <sys/sdt.h>
76 #include <sys/vnode.h>
77 #include <sys/signalvar.h>
78 #include <sys/syscallsubr.h>
79 #include <sys/sysctl.h>
80 #include <vm/uma.h>
81
82 #include "fuse.h"
83 #include "fuse_node.h"
84 #include "fuse_ipc.h"
85 #include "fuse_internal.h"
86
87 SDT_PROVIDER_DECLARE(fuse);
88 /* 
89  * Fuse trace probe:
90  * arg0: verbosity.  Higher numbers give more verbose messages
91  * arg1: Textual message
92  */
93 SDT_PROBE_DEFINE2(fuse, , ipc, trace, "int", "char*");
94
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);
103 static inline int 
104 fticket_aw_pull_uio(struct fuse_ticket *ftick,
105     struct uio *uio);
106
107 static int fuse_body_audit(struct fuse_ticket *ftick, size_t blen);
108
109 static fuse_handler_t fuse_standard_handler;
110
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;
115
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;
119
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;
124
125 SYSCTL_INT(_vfs_fusefs, OID_AUTO, iov_credit, CTLFLAG_RW,
126     &fuse_iov_credit, 0,
127     "how many times is an oversized fuse_iov tolerated");
128
129 MALLOC_DEFINE(M_FUSEMSG, "fuse_msgbuf", "fuse message buffer");
130 static uma_zone_t ticket_zone;
131
132 /* 
133  * TODO: figure out how to timeout INTERRUPT requests, because the daemon may
134  * leagally never respond
135  * 
136  * TODO: remove an INTERRUPT request if the daemon responds to the original
137  */
138 static int
139 fuse_interrupt_callback(struct fuse_ticket *tick, struct uio *uio)
140 {
141         struct fuse_ticket *otick, *x_tick;
142         struct fuse_interrupt_in *fii;
143         struct fuse_data *data;
144         data = tick->tk_data;
145         bool found = false;
146
147         fii = (struct fuse_interrupt_in*)((char*)tick->tk_ms_fiov.base +
148                 sizeof(struct fuse_in_header));
149
150         fuse_lck_mtx_lock(data->aw_mtx);
151         TAILQ_FOREACH_SAFE(otick, &data->aw_head, tk_aw_link, x_tick) {
152                 if (otick->tk_unique == fii->unique) {
153                         found = true;
154                         break;
155                 }
156         }
157         fuse_lck_mtx_unlock(data->aw_mtx);
158
159         if (!found) {
160                 /* Original is already complete.  Just return */
161                 return 0;
162         }
163
164         /* Clear the original ticket's interrupt association */
165         otick->irq_unique = 0;
166
167         if (tick->tk_aw_ohead.error == EAGAIN) {
168                 /* 
169                  * There are two reasons we might get this:
170                  * 1) the daemon received the INTERRUPT request before the
171                  *    original, or
172                  * 2) the daemon received the INTERRUPT request after it
173                  *    completed the original request.
174                  * In the first case we should re-send the INTERRUPT.  In the
175                  * second, we should ignore it.
176                  */
177                 /* Resend */
178                 fuse_interrupt_send(otick, EINTR);
179                 return 0;
180         } else {
181                 /* Illegal FUSE_INTERRUPT response */
182                 return EINVAL;
183         }
184 }
185
186 /* Interrupt the operation otick.  Return err as its error code */
187 void
188 fuse_interrupt_send(struct fuse_ticket *otick, int err)
189 {
190         struct fuse_dispatcher fdi;
191         struct fuse_interrupt_in *fii;
192         struct fuse_in_header *ftick_hdr;
193         struct fuse_data *data = otick->tk_data;
194         struct fuse_ticket *tick, *xtick;
195         struct ucred reused_creds;
196
197         if (otick->irq_unique == 0) {
198                 /* 
199                  * If the daemon hasn't yet received otick, then we can answer
200                  * it ourselves and return.
201                  */
202                 fuse_lck_mtx_lock(data->ms_mtx);
203                 STAILQ_FOREACH_SAFE(tick, &otick->tk_data->ms_head, tk_ms_link,
204                         xtick) {
205                         if (tick == otick) {
206                                 STAILQ_REMOVE(&otick->tk_data->ms_head, tick,
207                                         fuse_ticket, tk_ms_link);
208                                 otick->tk_ms_link.stqe_next = NULL;
209                                 fuse_lck_mtx_unlock(data->ms_mtx);
210
211                                 fuse_lck_mtx_lock(otick->tk_aw_mtx);
212                                 if (!fticket_answered(otick)) {
213                                         fticket_set_answered(otick);
214                                         otick->tk_aw_errno = err;
215                                         wakeup(otick);
216                                 }
217                                 fuse_lck_mtx_unlock(otick->tk_aw_mtx);
218
219                                 fuse_ticket_drop(tick);
220                                 return;
221                         }
222                 }
223                 fuse_lck_mtx_unlock(data->ms_mtx);
224
225                 /* 
226                  * If the fuse daemon has already received otick, then we must
227                  * send FUSE_INTERRUPT.
228                  */
229                 ftick_hdr = fticket_in_header(otick);
230                 reused_creds.cr_uid = ftick_hdr->uid;
231                 reused_creds.cr_rgid = ftick_hdr->gid;
232                 fdisp_init(&fdi, sizeof(*fii));
233                 fdisp_make_pid(&fdi, FUSE_INTERRUPT, data, ftick_hdr->nodeid,
234                         ftick_hdr->pid, &reused_creds);
235
236                 fii = fdi.indata;
237                 fii->unique = otick->tk_unique;
238                 fuse_insert_callback(fdi.tick, fuse_interrupt_callback);
239
240                 otick->irq_unique = fdi.tick->tk_unique;
241                 fuse_insert_message(fdi.tick);
242                 fdisp_destroy(&fdi);
243         } else {
244                 /* This ticket has already been interrupted */
245         }
246 }
247
248 void
249 fiov_init(struct fuse_iov *fiov, size_t size)
250 {
251         uint32_t msize = FU_AT_LEAST(size);
252
253         fiov->len = 0;
254
255         fiov->base = malloc(msize, M_FUSEMSG, M_WAITOK | M_ZERO);
256
257         fiov->allocated_size = msize;
258         fiov->credit = fuse_iov_credit;
259 }
260
261 void
262 fiov_teardown(struct fuse_iov *fiov)
263 {
264         MPASS(fiov->base != NULL);
265         free(fiov->base, M_FUSEMSG);
266 }
267
268 void
269 fiov_adjust(struct fuse_iov *fiov, size_t size)
270 {
271         if (fiov->allocated_size < size ||
272             (fuse_iov_permanent_bufsize >= 0 &&
273             fiov->allocated_size - size > fuse_iov_permanent_bufsize &&
274             --fiov->credit < 0)) {
275
276                 fiov->base = realloc(fiov->base, FU_AT_LEAST(size), M_FUSEMSG,
277                     M_WAITOK | M_ZERO);
278                 if (!fiov->base) {
279                         panic("FUSE: realloc failed");
280                 }
281                 fiov->allocated_size = FU_AT_LEAST(size);
282                 fiov->credit = fuse_iov_credit;
283                 /* Clear data buffer after reallocation */
284                 bzero(fiov->base, size);
285         } else if (size > fiov->len) {
286                 /* Clear newly extended portion of data buffer */
287                 bzero((char*)fiov->base + fiov->len, size - fiov->len);
288         }
289         fiov->len = size;
290 }
291
292 /* Clear the fiov's data buffer */
293 static void
294 fiov_clear(struct fuse_iov *fiov)
295 {
296         bzero(fiov->base, fiov->len);
297 }
298
299 /* Resize the fiov if needed, and clear it's buffer */
300 void
301 fiov_refresh(struct fuse_iov *fiov)
302 {
303         fiov_adjust(fiov, 0);
304 }
305
306 static int
307 fticket_ctor(void *mem, int size, void *arg, int flags)
308 {
309         struct fuse_ticket *ftick = mem;
310         struct fuse_data *data = arg;
311
312         FUSE_ASSERT_MS_DONE(ftick);
313         FUSE_ASSERT_AW_DONE(ftick);
314
315         ftick->tk_data = data;
316
317         if (ftick->tk_unique != 0)
318                 fticket_refresh(ftick);
319
320         /* May be truncated to 32 bits */
321         ftick->tk_unique = atomic_fetchadd_long(&data->ticketer, 1);
322         if (ftick->tk_unique == 0)
323                 ftick->tk_unique = atomic_fetchadd_long(&data->ticketer, 1);
324
325         ftick->irq_unique = 0;
326
327         refcount_init(&ftick->tk_refcount, 1);
328         atomic_add_acq_int(&fuse_ticket_count, 1);
329
330         return 0;
331 }
332
333 static void
334 fticket_dtor(void *mem, int size, void *arg)
335 {
336 #ifdef INVARIANTS
337         struct fuse_ticket *ftick = mem;
338 #endif
339
340         FUSE_ASSERT_MS_DONE(ftick);
341         FUSE_ASSERT_AW_DONE(ftick);
342
343         atomic_subtract_acq_int(&fuse_ticket_count, 1);
344 }
345
346 static int
347 fticket_init(void *mem, int size, int flags)
348 {
349         struct fuse_ticket *ftick = mem;
350
351         bzero(ftick, sizeof(struct fuse_ticket));
352
353         fiov_init(&ftick->tk_ms_fiov, sizeof(struct fuse_in_header));
354         ftick->tk_ms_type = FT_M_FIOV;
355
356         mtx_init(&ftick->tk_aw_mtx, "fuse answer delivery mutex", NULL, MTX_DEF);
357         fiov_init(&ftick->tk_aw_fiov, 0);
358         ftick->tk_aw_type = FT_A_FIOV;
359
360         return 0;
361 }
362
363 static void
364 fticket_fini(void *mem, int size)
365 {
366         struct fuse_ticket *ftick = mem;
367
368         fiov_teardown(&ftick->tk_ms_fiov);
369         fiov_teardown(&ftick->tk_aw_fiov);
370         mtx_destroy(&ftick->tk_aw_mtx);
371 }
372
373 static inline struct fuse_ticket *
374 fticket_alloc(struct fuse_data *data)
375 {
376         return uma_zalloc_arg(ticket_zone, data, M_WAITOK);
377 }
378
379 static inline void
380 fticket_destroy(struct fuse_ticket *ftick)
381 {
382         return uma_zfree(ticket_zone, ftick);
383 }
384
385 static inline
386 void
387 fticket_refresh(struct fuse_ticket *ftick)
388 {
389         FUSE_ASSERT_MS_DONE(ftick);
390         FUSE_ASSERT_AW_DONE(ftick);
391
392         fiov_refresh(&ftick->tk_ms_fiov);
393         ftick->tk_ms_bufdata = NULL;
394         ftick->tk_ms_bufsize = 0;
395         ftick->tk_ms_type = FT_M_FIOV;
396
397         bzero(&ftick->tk_aw_ohead, sizeof(struct fuse_out_header));
398
399         fiov_refresh(&ftick->tk_aw_fiov);
400         ftick->tk_aw_errno = 0;
401         ftick->tk_aw_bufdata = NULL;
402         ftick->tk_aw_bufsize = 0;
403         ftick->tk_aw_type = FT_A_FIOV;
404
405         ftick->tk_flag = 0;
406 }
407
408 /* Prepar the ticket to be reused, but don't clear its data buffers */
409 static inline void
410 fticket_reset(struct fuse_ticket *ftick)
411 {
412         FUSE_ASSERT_MS_DONE(ftick);
413         FUSE_ASSERT_AW_DONE(ftick);
414
415         ftick->tk_ms_bufdata = NULL;
416         ftick->tk_ms_bufsize = 0;
417         ftick->tk_ms_type = FT_M_FIOV;
418
419         bzero(&ftick->tk_aw_ohead, sizeof(struct fuse_out_header));
420
421         ftick->tk_aw_errno = 0;
422         ftick->tk_aw_bufdata = NULL;
423         ftick->tk_aw_bufsize = 0;
424         ftick->tk_aw_type = FT_A_FIOV;
425
426         ftick->tk_flag = 0;
427 }
428
429 static int
430 fticket_wait_answer(struct fuse_ticket *ftick)
431 {
432         struct thread *td = curthread;
433         sigset_t blockedset, oldset;
434         int err = 0;
435         struct fuse_data *data;
436         SIGEMPTYSET(blockedset);
437
438         kern_sigprocmask(td, SIG_BLOCK, NULL, &oldset, 0);
439
440         fuse_lck_mtx_lock(ftick->tk_aw_mtx);
441
442 retry:
443         if (fticket_answered(ftick)) {
444                 goto out;
445         }
446         data = ftick->tk_data;
447
448         if (fdata_get_dead(data)) {
449                 err = ENOTCONN;
450                 fticket_set_answered(ftick);
451                 goto out;
452         }
453         kern_sigprocmask(td, SIG_BLOCK, &blockedset, NULL, 0);
454         err = msleep(ftick, &ftick->tk_aw_mtx, PCATCH, "fu_ans",
455             data->daemon_timeout * hz);
456         kern_sigprocmask(td, SIG_SETMASK, &oldset, NULL, 0);
457         if (err == EWOULDBLOCK) {
458                 SDT_PROBE2(fuse, , ipc, trace, 3,
459                         "fticket_wait_answer: EWOULDBLOCK");
460 #ifdef XXXIP                            /* die conditionally */
461                 if (!fdata_get_dead(data)) {
462                         fdata_set_dead(data);
463                 }
464 #endif
465                 err = ETIMEDOUT;
466                 fticket_set_answered(ftick);
467         } else if ((err == EINTR || err == ERESTART)) {
468                 /*
469                  * Whether we get EINTR or ERESTART depends on whether
470                  * SA_RESTART was set by sigaction(2).
471                  *
472                  * Try to interrupt the operation and wait for an EINTR response
473                  * to the original operation.  If the file system does not
474                  * support FUSE_INTERRUPT, then we'll just wait for it to
475                  * complete like normal.  If it does support FUSE_INTERRUPT,
476                  * then it will either respond EINTR to the original operation,
477                  * or EAGAIN to the interrupt.
478                  */
479                 int sig;
480
481                 SDT_PROBE2(fuse, , ipc, trace, 4,
482                         "fticket_wait_answer: interrupt");
483                 fuse_lck_mtx_unlock(ftick->tk_aw_mtx);
484                 fuse_interrupt_send(ftick, err);
485
486                 PROC_LOCK(td->td_proc);
487                 mtx_lock(&td->td_proc->p_sigacts->ps_mtx);
488                 sig = cursig(td);
489                 mtx_unlock(&td->td_proc->p_sigacts->ps_mtx);
490                 PROC_UNLOCK(td->td_proc);
491
492                 fuse_lck_mtx_lock(ftick->tk_aw_mtx);
493                 if (!sig_isfatal(td->td_proc, sig)) {
494                         /* 
495                          * Block the just-delivered signal while we wait for an
496                          * interrupt response
497                          */
498                         SIGADDSET(blockedset, sig);
499                         goto retry;
500                 } else {
501                         /* Return immediately for fatal signals */
502                 }
503         } else if (err) {
504                 SDT_PROBE2(fuse, , ipc, trace, 6,
505                         "fticket_wait_answer: other error");
506         } else {
507                 SDT_PROBE2(fuse, , ipc, trace, 7, "fticket_wait_answer: OK");
508         }
509 out:
510         if (!(err || fticket_answered(ftick))) {
511                 SDT_PROBE2(fuse, , ipc, trace, 1,
512                         "FUSE: requester was woken up but still no answer");
513                 err = ENXIO;
514         }
515         fuse_lck_mtx_unlock(ftick->tk_aw_mtx);
516
517         return err;
518 }
519
520 static  inline
521 int
522 fticket_aw_pull_uio(struct fuse_ticket *ftick, struct uio *uio)
523 {
524         int err = 0;
525         size_t len = uio_resid(uio);
526
527         if (len) {
528                 switch (ftick->tk_aw_type) {
529                 case FT_A_FIOV:
530                         fiov_adjust(fticket_resp(ftick), len);
531                         err = uiomove(fticket_resp(ftick)->base, len, uio);
532                         break;
533
534                 case FT_A_BUF:
535                         ftick->tk_aw_bufsize = len;
536                         err = uiomove(ftick->tk_aw_bufdata, len, uio);
537                         break;
538
539                 default:
540                         panic("FUSE: unknown answer type for ticket %p", ftick);
541                 }
542         }
543         return err;
544 }
545
546 int
547 fticket_pull(struct fuse_ticket *ftick, struct uio *uio)
548 {
549         int err = 0;
550
551         if (ftick->tk_aw_ohead.error) {
552                 return 0;
553         }
554         err = fuse_body_audit(ftick, uio_resid(uio));
555         if (!err) {
556                 err = fticket_aw_pull_uio(ftick, uio);
557         }
558         return err;
559 }
560
561 struct fuse_data *
562 fdata_alloc(struct cdev *fdev, struct ucred *cred)
563 {
564         struct fuse_data *data;
565
566         data = malloc(sizeof(struct fuse_data), M_FUSEMSG, M_WAITOK | M_ZERO);
567
568         data->fdev = fdev;
569         mtx_init(&data->ms_mtx, "fuse message list mutex", NULL, MTX_DEF);
570         STAILQ_INIT(&data->ms_head);
571         mtx_init(&data->aw_mtx, "fuse answer list mutex", NULL, MTX_DEF);
572         TAILQ_INIT(&data->aw_head);
573         data->daemoncred = crhold(cred);
574         data->daemon_timeout = FUSE_DEFAULT_DAEMON_TIMEOUT;
575         sx_init(&data->rename_lock, "fuse rename lock");
576         data->ref = 1;
577
578         return data;
579 }
580
581 void
582 fdata_trydestroy(struct fuse_data *data)
583 {
584         data->ref--;
585         MPASS(data->ref >= 0);
586         if (data->ref != 0)
587                 return;
588
589         /* Driving off stage all that stuff thrown at device... */
590         mtx_destroy(&data->ms_mtx);
591         mtx_destroy(&data->aw_mtx);
592         sx_destroy(&data->rename_lock);
593
594         crfree(data->daemoncred);
595
596         free(data, M_FUSEMSG);
597 }
598
599 void
600 fdata_set_dead(struct fuse_data *data)
601 {
602         FUSE_LOCK();
603         if (fdata_get_dead(data)) {
604                 FUSE_UNLOCK();
605                 return;
606         }
607         fuse_lck_mtx_lock(data->ms_mtx);
608         data->dataflags |= FSESS_DEAD;
609         wakeup_one(data);
610         selwakeuppri(&data->ks_rsel, PZERO + 1);
611         wakeup(&data->ticketer);
612         fuse_lck_mtx_unlock(data->ms_mtx);
613         FUSE_UNLOCK();
614 }
615
616 struct fuse_ticket *
617 fuse_ticket_fetch(struct fuse_data *data)
618 {
619         int err = 0;
620         struct fuse_ticket *ftick;
621
622         ftick = fticket_alloc(data);
623
624         if (!(data->dataflags & FSESS_INITED)) {
625                 /* Sleep until get answer for INIT messsage */
626                 FUSE_LOCK();
627                 if (!(data->dataflags & FSESS_INITED) && data->ticketer > 2) {
628                         err = msleep(&data->ticketer, &fuse_mtx, PCATCH | PDROP,
629                             "fu_ini", 0);
630                         if (err)
631                                 fdata_set_dead(data);
632                 } else
633                         FUSE_UNLOCK();
634         }
635         return ftick;
636 }
637
638 int
639 fuse_ticket_drop(struct fuse_ticket *ftick)
640 {
641         int die;
642
643         die = refcount_release(&ftick->tk_refcount);
644         if (die)
645                 fticket_destroy(ftick);
646
647         return die;
648 }
649
650 void
651 fuse_insert_callback(struct fuse_ticket *ftick, fuse_handler_t * handler)
652 {
653         if (fdata_get_dead(ftick->tk_data)) {
654                 return;
655         }
656         ftick->tk_aw_handler = handler;
657
658         fuse_lck_mtx_lock(ftick->tk_data->aw_mtx);
659         fuse_aw_push(ftick);
660         fuse_lck_mtx_unlock(ftick->tk_data->aw_mtx);
661 }
662
663 void
664 fuse_insert_message(struct fuse_ticket *ftick)
665 {
666         if (ftick->tk_flag & FT_DIRTY) {
667                 panic("FUSE: ticket reused without being refreshed");
668         }
669         ftick->tk_flag |= FT_DIRTY;
670
671         if (fdata_get_dead(ftick->tk_data)) {
672                 return;
673         }
674         fuse_lck_mtx_lock(ftick->tk_data->ms_mtx);
675         fuse_ms_push(ftick);
676         wakeup_one(ftick->tk_data);
677         selwakeuppri(&ftick->tk_data->ks_rsel, PZERO + 1);
678         fuse_lck_mtx_unlock(ftick->tk_data->ms_mtx);
679 }
680
681 static int
682 fuse_body_audit(struct fuse_ticket *ftick, size_t blen)
683 {
684         int err = 0;
685         enum fuse_opcode opcode;
686
687         opcode = fticket_opcode(ftick);
688
689         switch (opcode) {
690         case FUSE_LOOKUP:
691                 err = (blen == sizeof(struct fuse_entry_out)) ? 0 : EINVAL;
692                 break;
693
694         case FUSE_FORGET:
695                 panic("FUSE: a handler has been intalled for FUSE_FORGET");
696                 break;
697
698         case FUSE_GETATTR:
699                 err = (blen == sizeof(struct fuse_attr_out)) ? 0 : EINVAL;
700                 break;
701
702         case FUSE_SETATTR:
703                 err = (blen == sizeof(struct fuse_attr_out)) ? 0 : EINVAL;
704                 break;
705
706         case FUSE_READLINK:
707                 err = (PAGE_SIZE >= blen) ? 0 : EINVAL;
708                 break;
709
710         case FUSE_SYMLINK:
711                 err = (blen == sizeof(struct fuse_entry_out)) ? 0 : EINVAL;
712                 break;
713
714         case FUSE_MKNOD:
715                 err = (blen == sizeof(struct fuse_entry_out)) ? 0 : EINVAL;
716                 break;
717
718         case FUSE_MKDIR:
719                 err = (blen == sizeof(struct fuse_entry_out)) ? 0 : EINVAL;
720                 break;
721
722         case FUSE_UNLINK:
723                 err = (blen == 0) ? 0 : EINVAL;
724                 break;
725
726         case FUSE_RMDIR:
727                 err = (blen == 0) ? 0 : EINVAL;
728                 break;
729
730         case FUSE_RENAME:
731                 err = (blen == 0) ? 0 : EINVAL;
732                 break;
733
734         case FUSE_LINK:
735                 err = (blen == sizeof(struct fuse_entry_out)) ? 0 : EINVAL;
736                 break;
737
738         case FUSE_OPEN:
739                 err = (blen == sizeof(struct fuse_open_out)) ? 0 : EINVAL;
740                 break;
741
742         case FUSE_READ:
743                 err = (((struct fuse_read_in *)(
744                     (char *)ftick->tk_ms_fiov.base +
745                     sizeof(struct fuse_in_header)
746                     ))->size >= blen) ? 0 : EINVAL;
747                 break;
748
749         case FUSE_WRITE:
750                 err = (blen == sizeof(struct fuse_write_out)) ? 0 : EINVAL;
751                 break;
752
753         case FUSE_STATFS:
754                 if (fuse_libabi_geq(ftick->tk_data, 7, 4)) {
755                         err = (blen == sizeof(struct fuse_statfs_out)) ? 
756                           0 : EINVAL;
757                 } else {
758                         err = (blen == FUSE_COMPAT_STATFS_SIZE) ? 0 : EINVAL;
759                 }
760                 break;
761
762         case FUSE_RELEASE:
763                 err = (blen == 0) ? 0 : EINVAL;
764                 break;
765
766         case FUSE_FSYNC:
767                 err = (blen == 0) ? 0 : EINVAL;
768                 break;
769
770         case FUSE_SETXATTR:
771                 err = (blen == 0) ? 0 : EINVAL;
772                 break;
773
774         case FUSE_GETXATTR:
775         case FUSE_LISTXATTR:
776                 /*
777                  * These can have varying response lengths, and 0 length
778                  * isn't necessarily invalid.
779                  */
780                 err = 0;
781                 break;
782
783         case FUSE_REMOVEXATTR:
784                 err = (blen == 0) ? 0 : EINVAL;
785                 break;
786
787         case FUSE_FLUSH:
788                 err = (blen == 0) ? 0 : EINVAL;
789                 break;
790
791         case FUSE_INIT:
792                 if (blen == sizeof(struct fuse_init_out) || blen == 8) {
793                         err = 0;
794                 } else {
795                         err = EINVAL;
796                 }
797                 break;
798
799         case FUSE_OPENDIR:
800                 err = (blen == sizeof(struct fuse_open_out)) ? 0 : EINVAL;
801                 break;
802
803         case FUSE_READDIR:
804                 err = (((struct fuse_read_in *)(
805                     (char *)ftick->tk_ms_fiov.base +
806                     sizeof(struct fuse_in_header)
807                     ))->size >= blen) ? 0 : EINVAL;
808                 break;
809
810         case FUSE_RELEASEDIR:
811                 err = (blen == 0) ? 0 : EINVAL;
812                 break;
813
814         case FUSE_FSYNCDIR:
815                 err = (blen == 0) ? 0 : EINVAL;
816                 break;
817
818         case FUSE_GETLK:
819                 err = (blen == sizeof(struct fuse_lk_out)) ? 0 : EINVAL;
820                 break;
821
822         case FUSE_SETLK:
823                 err = (blen == 0) ? 0 : EINVAL;
824                 break;
825
826         case FUSE_SETLKW:
827                 err = (blen == 0) ? 0 : EINVAL;
828                 break;
829
830         case FUSE_ACCESS:
831                 err = (blen == 0) ? 0 : EINVAL;
832                 break;
833
834         case FUSE_CREATE:
835                 err = (blen == sizeof(struct fuse_entry_out) +
836                     sizeof(struct fuse_open_out)) ? 0 : EINVAL;
837                 break;
838
839         case FUSE_DESTROY:
840                 err = (blen == 0) ? 0 : EINVAL;
841                 break;
842
843         default:
844                 panic("FUSE: opcodes out of sync (%d)\n", opcode);
845         }
846
847         return err;
848 }
849
850 static inline void
851 fuse_setup_ihead(struct fuse_in_header *ihead, struct fuse_ticket *ftick,
852     uint64_t nid, enum fuse_opcode op, size_t blen, pid_t pid,
853     struct ucred *cred)
854 {
855         ihead->len = sizeof(*ihead) + blen;
856         ihead->unique = ftick->tk_unique;
857         ihead->nodeid = nid;
858         ihead->opcode = op;
859
860         ihead->pid = pid;
861         ihead->uid = cred->cr_uid;
862         ihead->gid = cred->cr_rgid;
863 }
864
865 /*
866  * fuse_standard_handler just pulls indata and wakes up pretender.
867  * Doesn't try to interpret data, that's left for the pretender.
868  * Though might do a basic size verification before the pull-in takes place
869  */
870
871 static int
872 fuse_standard_handler(struct fuse_ticket *ftick, struct uio *uio)
873 {
874         int err = 0;
875
876         err = fticket_pull(ftick, uio);
877
878         fuse_lck_mtx_lock(ftick->tk_aw_mtx);
879
880         if (!fticket_answered(ftick)) {
881                 fticket_set_answered(ftick);
882                 ftick->tk_aw_errno = err;
883                 wakeup(ftick);
884         }
885         fuse_lck_mtx_unlock(ftick->tk_aw_mtx);
886
887         return err;
888 }
889
890 /*
891  * Reinitialize a dispatcher from a pid and node id, without resizing or
892  * clearing its data buffers
893  */
894 static void
895 fdisp_refresh_pid(struct fuse_dispatcher *fdip, enum fuse_opcode op,
896     struct mount *mp, uint64_t nid, pid_t pid, struct ucred *cred)
897 {
898         MPASS(fdip->tick);
899         MPASS2(sizeof(fdip->finh) + fdip->iosize <= fdip->tick->tk_ms_fiov.len,
900                 "Must use fdisp_make_pid to increase the size of the fiov");
901         fticket_reset(fdip->tick);
902
903         FUSE_DIMALLOC(&fdip->tick->tk_ms_fiov, fdip->finh,
904             fdip->indata, fdip->iosize);
905
906         fuse_setup_ihead(fdip->finh, fdip->tick, nid, op, fdip->iosize, pid,
907                 cred);
908 }
909
910 /* Initialize a dispatcher from a pid and node id */
911 static void
912 fdisp_make_pid(struct fuse_dispatcher *fdip, enum fuse_opcode op,
913     struct fuse_data *data, uint64_t nid, pid_t pid, struct ucred *cred)
914 {
915         if (fdip->tick) {
916                 fticket_refresh(fdip->tick);
917         } else {
918                 fdip->tick = fuse_ticket_fetch(data);
919         }
920
921         /* FUSE_DIMALLOC will bzero the fiovs when it enlarges them */
922         FUSE_DIMALLOC(&fdip->tick->tk_ms_fiov, fdip->finh,
923             fdip->indata, fdip->iosize);
924
925         fuse_setup_ihead(fdip->finh, fdip->tick, nid, op, fdip->iosize, pid, cred);
926 }
927
928 void
929 fdisp_make(struct fuse_dispatcher *fdip, enum fuse_opcode op, struct mount *mp,
930     uint64_t nid, struct thread *td, struct ucred *cred)
931 {
932         struct fuse_data *data = fuse_get_mpdata(mp);
933         RECTIFY_TDCR(td, cred);
934
935         return fdisp_make_pid(fdip, op, data, nid, td->td_proc->p_pid, cred);
936 }
937
938 void
939 fdisp_make_vp(struct fuse_dispatcher *fdip, enum fuse_opcode op,
940     struct vnode *vp, struct thread *td, struct ucred *cred)
941 {
942         struct mount *mp = vnode_mount(vp);
943         struct fuse_data *data = fuse_get_mpdata(mp);
944
945         RECTIFY_TDCR(td, cred);
946         return fdisp_make_pid(fdip, op, data, VTOI(vp),
947             td->td_proc->p_pid, cred);
948 }
949
950 /* Refresh a fuse_dispatcher so it can be reused, but don't zero its data */
951 void
952 fdisp_refresh_vp(struct fuse_dispatcher *fdip, enum fuse_opcode op,
953     struct vnode *vp, struct thread *td, struct ucred *cred)
954 {
955         RECTIFY_TDCR(td, cred);
956         return fdisp_refresh_pid(fdip, op, vnode_mount(vp), VTOI(vp),
957             td->td_proc->p_pid, cred);
958 }
959
960 void
961 fdisp_refresh(struct fuse_dispatcher *fdip)
962 {
963         fticket_refresh(fdip->tick);
964 }
965
966 SDT_PROBE_DEFINE2(fuse, , ipc, fdisp_wait_answ_error, "char*", "int");
967
968 int
969 fdisp_wait_answ(struct fuse_dispatcher *fdip)
970 {
971         int err = 0;
972
973         fdip->answ_stat = 0;
974         fuse_insert_callback(fdip->tick, fuse_standard_handler);
975         fuse_insert_message(fdip->tick);
976
977         if ((err = fticket_wait_answer(fdip->tick))) {
978                 fuse_lck_mtx_lock(fdip->tick->tk_aw_mtx);
979
980                 if (fticket_answered(fdip->tick)) {
981                         /*
982                          * Just between noticing the interrupt and getting here,
983                          * the standard handler has completed his job.
984                          * So we drop the ticket and exit as usual.
985                          */
986                         SDT_PROBE2(fuse, , ipc, fdisp_wait_answ_error,
987                                 "IPC: interrupted, already answered", err);
988                         fuse_lck_mtx_unlock(fdip->tick->tk_aw_mtx);
989                         goto out;
990                 } else {
991                         /*
992                          * So we were faster than the standard handler.
993                          * Then by setting the answered flag we get *him*
994                          * to drop the ticket.
995                          */
996                         SDT_PROBE2(fuse, , ipc, fdisp_wait_answ_error,
997                                 "IPC: interrupted, setting to answered", err);
998                         fticket_set_answered(fdip->tick);
999                         fuse_lck_mtx_unlock(fdip->tick->tk_aw_mtx);
1000                         return err;
1001                 }
1002         }
1003
1004         if (fdip->tick->tk_aw_errno) {
1005                 SDT_PROBE2(fuse, , ipc, fdisp_wait_answ_error,
1006                         "IPC: explicit EIO-ing", fdip->tick->tk_aw_errno);
1007                 err = EIO;
1008                 goto out;
1009         }
1010         if ((err = fdip->tick->tk_aw_ohead.error)) {
1011                 SDT_PROBE2(fuse, , ipc, fdisp_wait_answ_error,
1012                         "IPC: setting status", fdip->tick->tk_aw_ohead.error);
1013                 /*
1014                  * This means a "proper" fuse syscall error.
1015                  * We record this value so the caller will
1016                  * be able to know it's not a boring messaging
1017                  * failure, if she wishes so (and if not, she can
1018                  * just simply propagate the return value of this routine).
1019                  * [XXX Maybe a bitflag would do the job too,
1020                  * if other flags needed, this will be converted thusly.]
1021                  */
1022                 fdip->answ_stat = err;
1023                 goto out;
1024         }
1025         fdip->answ = fticket_resp(fdip->tick)->base;
1026         fdip->iosize = fticket_resp(fdip->tick)->len;
1027
1028         return 0;
1029
1030 out:
1031         return err;
1032 }
1033
1034 void
1035 fuse_ipc_init(void)
1036 {
1037         ticket_zone = uma_zcreate("fuse_ticket", sizeof(struct fuse_ticket),
1038             fticket_ctor, fticket_dtor, fticket_init, fticket_fini,
1039             UMA_ALIGN_PTR, 0);
1040 }
1041
1042 void
1043 fuse_ipc_destroy(void)
1044 {
1045         uma_zdestroy(ticket_zone);
1046 }