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