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