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