]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/fs/fuse/fuse_ipc.c
Fix typos from r344664
[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 struct fuse_ticket *fticket_alloc(struct fuse_data *data);
96 static void fticket_refresh(struct fuse_ticket *ftick);
97 static void fticket_destroy(struct fuse_ticket *ftick);
98 static int fticket_wait_answer(struct fuse_ticket *ftick);
99 static inline int 
100 fticket_aw_pull_uio(struct fuse_ticket *ftick,
101     struct uio *uio);
102
103 static int fuse_body_audit(struct fuse_ticket *ftick, size_t blen);
104
105 static fuse_handler_t fuse_standard_handler;
106
107 SYSCTL_NODE(_vfs, OID_AUTO, fuse, CTLFLAG_RW, 0, "FUSE tunables");
108 SYSCTL_STRING(_vfs_fuse, OID_AUTO, version, CTLFLAG_RD,
109     FUSE_FREEBSD_VERSION, 0, "fuse-freebsd version");
110 static int fuse_ticket_count = 0;
111
112 SYSCTL_INT(_vfs_fuse, OID_AUTO, ticket_count, CTLFLAG_RW,
113     &fuse_ticket_count, 0, "number of allocated tickets");
114 static long fuse_iov_permanent_bufsize = 1 << 19;
115
116 SYSCTL_LONG(_vfs_fuse, OID_AUTO, iov_permanent_bufsize, CTLFLAG_RW,
117     &fuse_iov_permanent_bufsize, 0,
118     "limit for permanently stored buffer size for fuse_iovs");
119 static int fuse_iov_credit = 16;
120
121 SYSCTL_INT(_vfs_fuse, OID_AUTO, iov_credit, CTLFLAG_RW,
122     &fuse_iov_credit, 0,
123     "how many times is an oversized fuse_iov tolerated");
124
125 MALLOC_DEFINE(M_FUSEMSG, "fuse_msgbuf", "fuse message buffer");
126 static uma_zone_t ticket_zone;
127
128 static void
129 fuse_block_sigs(sigset_t *oldset)
130 {
131         sigset_t newset;
132
133         SIGFILLSET(newset);
134         SIGDELSET(newset, SIGKILL);
135         if (kern_sigprocmask(curthread, SIG_BLOCK, &newset, oldset, 0))
136                 panic("%s: Invalid operation for kern_sigprocmask()",
137                     __func__);
138 }
139
140 static void
141 fuse_restore_sigs(sigset_t *oldset)
142 {
143
144         if (kern_sigprocmask(curthread, SIG_SETMASK, oldset, NULL, 0))
145                 panic("%s: Invalid operation for kern_sigprocmask()",
146                     __func__);
147 }
148
149 void
150 fiov_init(struct fuse_iov *fiov, size_t size)
151 {
152         uint32_t msize = FU_AT_LEAST(size);
153
154         fiov->len = 0;
155
156         fiov->base = malloc(msize, M_FUSEMSG, M_WAITOK | M_ZERO);
157
158         fiov->allocated_size = msize;
159         fiov->credit = fuse_iov_credit;
160 }
161
162 void
163 fiov_teardown(struct fuse_iov *fiov)
164 {
165         MPASS(fiov->base != NULL);
166         free(fiov->base, M_FUSEMSG);
167 }
168
169 void
170 fiov_adjust(struct fuse_iov *fiov, size_t size)
171 {
172         if (fiov->allocated_size < size ||
173             (fuse_iov_permanent_bufsize >= 0 &&
174             fiov->allocated_size - size > fuse_iov_permanent_bufsize &&
175             --fiov->credit < 0)) {
176
177                 fiov->base = realloc(fiov->base, FU_AT_LEAST(size), M_FUSEMSG,
178                     M_WAITOK | M_ZERO);
179                 if (!fiov->base) {
180                         panic("FUSE: realloc failed");
181                 }
182                 fiov->allocated_size = FU_AT_LEAST(size);
183                 fiov->credit = fuse_iov_credit;
184         }
185         fiov->len = size;
186 }
187
188 void
189 fiov_refresh(struct fuse_iov *fiov)
190 {
191         bzero(fiov->base, fiov->len);
192         fiov_adjust(fiov, 0);
193 }
194
195 static int
196 fticket_ctor(void *mem, int size, void *arg, int flags)
197 {
198         struct fuse_ticket *ftick = mem;
199         struct fuse_data *data = arg;
200
201         FUSE_ASSERT_MS_DONE(ftick);
202         FUSE_ASSERT_AW_DONE(ftick);
203
204         ftick->tk_data = data;
205
206         if (ftick->tk_unique != 0)
207                 fticket_refresh(ftick);
208
209         /* May be truncated to 32 bits */
210         ftick->tk_unique = atomic_fetchadd_long(&data->ticketer, 1);
211         if (ftick->tk_unique == 0)
212                 ftick->tk_unique = atomic_fetchadd_long(&data->ticketer, 1);
213
214         refcount_init(&ftick->tk_refcount, 1);
215         atomic_add_acq_int(&fuse_ticket_count, 1);
216
217         return 0;
218 }
219
220 static void
221 fticket_dtor(void *mem, int size, void *arg)
222 {
223         struct fuse_ticket *ftick = mem;
224
225         FUSE_ASSERT_MS_DONE(ftick);
226         FUSE_ASSERT_AW_DONE(ftick);
227
228         atomic_subtract_acq_int(&fuse_ticket_count, 1);
229 }
230
231 static int
232 fticket_init(void *mem, int size, int flags)
233 {
234         struct fuse_ticket *ftick = mem;
235
236         bzero(ftick, sizeof(struct fuse_ticket));
237
238         fiov_init(&ftick->tk_ms_fiov, sizeof(struct fuse_in_header));
239         ftick->tk_ms_type = FT_M_FIOV;
240
241         mtx_init(&ftick->tk_aw_mtx, "fuse answer delivery mutex", NULL, MTX_DEF);
242         fiov_init(&ftick->tk_aw_fiov, 0);
243         ftick->tk_aw_type = FT_A_FIOV;
244
245         return 0;
246 }
247
248 static void
249 fticket_fini(void *mem, int size)
250 {
251         struct fuse_ticket *ftick = mem;
252
253         fiov_teardown(&ftick->tk_ms_fiov);
254         fiov_teardown(&ftick->tk_aw_fiov);
255         mtx_destroy(&ftick->tk_aw_mtx);
256 }
257
258 static inline struct fuse_ticket *
259 fticket_alloc(struct fuse_data *data)
260 {
261         return uma_zalloc_arg(ticket_zone, data, M_WAITOK);
262 }
263
264 static inline void
265 fticket_destroy(struct fuse_ticket *ftick)
266 {
267         return uma_zfree(ticket_zone, ftick);
268 }
269
270 static  inline
271 void
272 fticket_refresh(struct fuse_ticket *ftick)
273 {
274         FUSE_ASSERT_MS_DONE(ftick);
275         FUSE_ASSERT_AW_DONE(ftick);
276
277         fiov_refresh(&ftick->tk_ms_fiov);
278         ftick->tk_ms_bufdata = NULL;
279         ftick->tk_ms_bufsize = 0;
280         ftick->tk_ms_type = FT_M_FIOV;
281
282         bzero(&ftick->tk_aw_ohead, sizeof(struct fuse_out_header));
283
284         fiov_refresh(&ftick->tk_aw_fiov);
285         ftick->tk_aw_errno = 0;
286         ftick->tk_aw_bufdata = NULL;
287         ftick->tk_aw_bufsize = 0;
288         ftick->tk_aw_type = FT_A_FIOV;
289
290         ftick->tk_flag = 0;
291 }
292
293 static int
294 fticket_wait_answer(struct fuse_ticket *ftick)
295 {
296         sigset_t tset;
297         int err = 0;
298         struct fuse_data *data;
299
300         fuse_lck_mtx_lock(ftick->tk_aw_mtx);
301
302         if (fticket_answered(ftick)) {
303                 goto out;
304         }
305         data = ftick->tk_data;
306
307         if (fdata_get_dead(data)) {
308                 err = ENOTCONN;
309                 fticket_set_answered(ftick);
310                 goto out;
311         }
312         fuse_block_sigs(&tset);
313         err = msleep(ftick, &ftick->tk_aw_mtx, PCATCH, "fu_ans",
314             data->daemon_timeout * hz);
315         fuse_restore_sigs(&tset);
316         if (err == EAGAIN) {            /* same as EWOULDBLOCK */
317 #ifdef XXXIP                            /* die conditionally */
318                 if (!fdata_get_dead(data)) {
319                         fdata_set_dead(data);
320                 }
321 #endif
322                 err = ETIMEDOUT;
323                 fticket_set_answered(ftick);
324         }
325 out:
326         if (!(err || fticket_answered(ftick))) {
327                 SDT_PROBE2(fuse, , ipc, trace, 1,
328                         "FUSE: requester was woken up but still no answer");
329                 err = ENXIO;
330         }
331         fuse_lck_mtx_unlock(ftick->tk_aw_mtx);
332
333         return err;
334 }
335
336 static  inline
337 int
338 fticket_aw_pull_uio(struct fuse_ticket *ftick, struct uio *uio)
339 {
340         int err = 0;
341         size_t len = uio_resid(uio);
342
343         if (len) {
344                 switch (ftick->tk_aw_type) {
345                 case FT_A_FIOV:
346                         fiov_adjust(fticket_resp(ftick), len);
347                         err = uiomove(fticket_resp(ftick)->base, len, uio);
348                         break;
349
350                 case FT_A_BUF:
351                         ftick->tk_aw_bufsize = len;
352                         err = uiomove(ftick->tk_aw_bufdata, len, uio);
353                         break;
354
355                 default:
356                         panic("FUSE: unknown answer type for ticket %p", ftick);
357                 }
358         }
359         return err;
360 }
361
362 int
363 fticket_pull(struct fuse_ticket *ftick, struct uio *uio)
364 {
365         int err = 0;
366
367         if (ftick->tk_aw_ohead.error) {
368                 return 0;
369         }
370         err = fuse_body_audit(ftick, uio_resid(uio));
371         if (!err) {
372                 err = fticket_aw_pull_uio(ftick, uio);
373         }
374         return err;
375 }
376
377 struct fuse_data *
378 fdata_alloc(struct cdev *fdev, struct ucred *cred)
379 {
380         struct fuse_data *data;
381
382         data = malloc(sizeof(struct fuse_data), M_FUSEMSG, M_WAITOK | M_ZERO);
383
384         data->fdev = fdev;
385         mtx_init(&data->ms_mtx, "fuse message list mutex", NULL, MTX_DEF);
386         STAILQ_INIT(&data->ms_head);
387         mtx_init(&data->aw_mtx, "fuse answer list mutex", NULL, MTX_DEF);
388         TAILQ_INIT(&data->aw_head);
389         data->daemoncred = crhold(cred);
390         data->daemon_timeout = FUSE_DEFAULT_DAEMON_TIMEOUT;
391         sx_init(&data->rename_lock, "fuse rename lock");
392         data->ref = 1;
393
394         return data;
395 }
396
397 void
398 fdata_trydestroy(struct fuse_data *data)
399 {
400         data->ref--;
401         MPASS(data->ref >= 0);
402         if (data->ref != 0)
403                 return;
404
405         /* Driving off stage all that stuff thrown at device... */
406         mtx_destroy(&data->ms_mtx);
407         mtx_destroy(&data->aw_mtx);
408         sx_destroy(&data->rename_lock);
409
410         crfree(data->daemoncred);
411
412         free(data, M_FUSEMSG);
413 }
414
415 void
416 fdata_set_dead(struct fuse_data *data)
417 {
418         FUSE_LOCK();
419         if (fdata_get_dead(data)) {
420                 FUSE_UNLOCK();
421                 return;
422         }
423         fuse_lck_mtx_lock(data->ms_mtx);
424         data->dataflags |= FSESS_DEAD;
425         wakeup_one(data);
426         selwakeuppri(&data->ks_rsel, PZERO + 1);
427         wakeup(&data->ticketer);
428         fuse_lck_mtx_unlock(data->ms_mtx);
429         FUSE_UNLOCK();
430 }
431
432 struct fuse_ticket *
433 fuse_ticket_fetch(struct fuse_data *data)
434 {
435         int err = 0;
436         struct fuse_ticket *ftick;
437
438         ftick = fticket_alloc(data);
439
440         if (!(data->dataflags & FSESS_INITED)) {
441                 /* Sleep until get answer for INIT messsage */
442                 FUSE_LOCK();
443                 if (!(data->dataflags & FSESS_INITED) && data->ticketer > 2) {
444                         err = msleep(&data->ticketer, &fuse_mtx, PCATCH | PDROP,
445                             "fu_ini", 0);
446                         if (err)
447                                 fdata_set_dead(data);
448                 } else
449                         FUSE_UNLOCK();
450         }
451         return ftick;
452 }
453
454 int
455 fuse_ticket_drop(struct fuse_ticket *ftick)
456 {
457         int die;
458
459         die = refcount_release(&ftick->tk_refcount);
460         if (die)
461                 fticket_destroy(ftick);
462
463         return die;
464 }
465
466 void
467 fuse_insert_callback(struct fuse_ticket *ftick, fuse_handler_t * handler)
468 {
469         if (fdata_get_dead(ftick->tk_data)) {
470                 return;
471         }
472         ftick->tk_aw_handler = handler;
473
474         fuse_lck_mtx_lock(ftick->tk_data->aw_mtx);
475         fuse_aw_push(ftick);
476         fuse_lck_mtx_unlock(ftick->tk_data->aw_mtx);
477 }
478
479 void
480 fuse_insert_message(struct fuse_ticket *ftick)
481 {
482         if (ftick->tk_flag & FT_DIRTY) {
483                 panic("FUSE: ticket reused without being refreshed");
484         }
485         ftick->tk_flag |= FT_DIRTY;
486
487         if (fdata_get_dead(ftick->tk_data)) {
488                 return;
489         }
490         fuse_lck_mtx_lock(ftick->tk_data->ms_mtx);
491         fuse_ms_push(ftick);
492         wakeup_one(ftick->tk_data);
493         selwakeuppri(&ftick->tk_data->ks_rsel, PZERO + 1);
494         fuse_lck_mtx_unlock(ftick->tk_data->ms_mtx);
495 }
496
497 static int
498 fuse_body_audit(struct fuse_ticket *ftick, size_t blen)
499 {
500         int err = 0;
501         enum fuse_opcode opcode;
502
503         opcode = fticket_opcode(ftick);
504
505         switch (opcode) {
506         case FUSE_LOOKUP:
507                 err = (blen == sizeof(struct fuse_entry_out)) ? 0 : EINVAL;
508                 break;
509
510         case FUSE_FORGET:
511                 panic("FUSE: a handler has been intalled for FUSE_FORGET");
512                 break;
513
514         case FUSE_GETATTR:
515                 err = (blen == sizeof(struct fuse_attr_out)) ? 0 : EINVAL;
516                 break;
517
518         case FUSE_SETATTR:
519                 err = (blen == sizeof(struct fuse_attr_out)) ? 0 : EINVAL;
520                 break;
521
522         case FUSE_READLINK:
523                 err = (PAGE_SIZE >= blen) ? 0 : EINVAL;
524                 break;
525
526         case FUSE_SYMLINK:
527                 err = (blen == sizeof(struct fuse_entry_out)) ? 0 : EINVAL;
528                 break;
529
530         case FUSE_MKNOD:
531                 err = (blen == sizeof(struct fuse_entry_out)) ? 0 : EINVAL;
532                 break;
533
534         case FUSE_MKDIR:
535                 err = (blen == sizeof(struct fuse_entry_out)) ? 0 : EINVAL;
536                 break;
537
538         case FUSE_UNLINK:
539                 err = (blen == 0) ? 0 : EINVAL;
540                 break;
541
542         case FUSE_RMDIR:
543                 err = (blen == 0) ? 0 : EINVAL;
544                 break;
545
546         case FUSE_RENAME:
547                 err = (blen == 0) ? 0 : EINVAL;
548                 break;
549
550         case FUSE_LINK:
551                 err = (blen == sizeof(struct fuse_entry_out)) ? 0 : EINVAL;
552                 break;
553
554         case FUSE_OPEN:
555                 err = (blen == sizeof(struct fuse_open_out)) ? 0 : EINVAL;
556                 break;
557
558         case FUSE_READ:
559                 err = (((struct fuse_read_in *)(
560                     (char *)ftick->tk_ms_fiov.base +
561                     sizeof(struct fuse_in_header)
562                     ))->size >= blen) ? 0 : EINVAL;
563                 break;
564
565         case FUSE_WRITE:
566                 err = (blen == sizeof(struct fuse_write_out)) ? 0 : EINVAL;
567                 break;
568
569         case FUSE_STATFS:
570                 if (fuse_libabi_geq(ftick->tk_data, 7, 4)) {
571                         err = (blen == sizeof(struct fuse_statfs_out)) ? 
572                           0 : EINVAL;
573                 } else {
574                         err = (blen == FUSE_COMPAT_STATFS_SIZE) ? 0 : EINVAL;
575                 }
576                 break;
577
578         case FUSE_RELEASE:
579                 err = (blen == 0) ? 0 : EINVAL;
580                 break;
581
582         case FUSE_FSYNC:
583                 err = (blen == 0) ? 0 : EINVAL;
584                 break;
585
586         case FUSE_SETXATTR:
587                 err = (blen == 0) ? 0 : EINVAL;
588                 break;
589
590         case FUSE_GETXATTR:
591         case FUSE_LISTXATTR:
592                 /*
593                  * These can have varying response lengths, and 0 length
594                  * isn't necessarily invalid.
595                  */
596                 err = 0;
597                 break;
598
599         case FUSE_REMOVEXATTR:
600                 err = (blen == 0) ? 0 : EINVAL;
601                 break;
602
603         case FUSE_FLUSH:
604                 err = (blen == 0) ? 0 : EINVAL;
605                 break;
606
607         case FUSE_INIT:
608                 if (blen == sizeof(struct fuse_init_out) || blen == 8) {
609                         err = 0;
610                 } else {
611                         err = EINVAL;
612                 }
613                 break;
614
615         case FUSE_OPENDIR:
616                 err = (blen == sizeof(struct fuse_open_out)) ? 0 : EINVAL;
617                 break;
618
619         case FUSE_READDIR:
620                 err = (((struct fuse_read_in *)(
621                     (char *)ftick->tk_ms_fiov.base +
622                     sizeof(struct fuse_in_header)
623                     ))->size >= blen) ? 0 : EINVAL;
624                 break;
625
626         case FUSE_RELEASEDIR:
627                 err = (blen == 0) ? 0 : EINVAL;
628                 break;
629
630         case FUSE_FSYNCDIR:
631                 err = (blen == 0) ? 0 : EINVAL;
632                 break;
633
634         case FUSE_GETLK:
635                 panic("FUSE: no response body format check for FUSE_GETLK");
636                 break;
637
638         case FUSE_SETLK:
639                 panic("FUSE: no response body format check for FUSE_SETLK");
640                 break;
641
642         case FUSE_SETLKW:
643                 panic("FUSE: no response body format check for FUSE_SETLKW");
644                 break;
645
646         case FUSE_ACCESS:
647                 err = (blen == 0) ? 0 : EINVAL;
648                 break;
649
650         case FUSE_CREATE:
651                 err = (blen == sizeof(struct fuse_entry_out) +
652                     sizeof(struct fuse_open_out)) ? 0 : EINVAL;
653                 break;
654
655         case FUSE_DESTROY:
656                 err = (blen == 0) ? 0 : EINVAL;
657                 break;
658
659         default:
660                 panic("FUSE: opcodes out of sync (%d)\n", opcode);
661         }
662
663         return err;
664 }
665
666 static inline void
667 fuse_setup_ihead(struct fuse_in_header *ihead, struct fuse_ticket *ftick,
668     uint64_t nid, enum fuse_opcode op, size_t blen, pid_t pid,
669     struct ucred *cred)
670 {
671         ihead->len = sizeof(*ihead) + blen;
672         ihead->unique = ftick->tk_unique;
673         ihead->nodeid = nid;
674         ihead->opcode = op;
675
676         ihead->pid = pid;
677         ihead->uid = cred->cr_uid;
678         ihead->gid = cred->cr_rgid;
679 }
680
681 /*
682  * fuse_standard_handler just pulls indata and wakes up pretender.
683  * Doesn't try to interpret data, that's left for the pretender.
684  * Though might do a basic size verification before the pull-in takes place
685  */
686
687 static int
688 fuse_standard_handler(struct fuse_ticket *ftick, struct uio *uio)
689 {
690         int err = 0;
691
692         err = fticket_pull(ftick, uio);
693
694         fuse_lck_mtx_lock(ftick->tk_aw_mtx);
695
696         if (!fticket_answered(ftick)) {
697                 fticket_set_answered(ftick);
698                 ftick->tk_aw_errno = err;
699                 wakeup(ftick);
700         }
701         fuse_lck_mtx_unlock(ftick->tk_aw_mtx);
702
703         return err;
704 }
705
706 void
707 fdisp_make_pid(struct fuse_dispatcher *fdip, enum fuse_opcode op,
708     struct mount *mp, uint64_t nid, pid_t pid, struct ucred *cred)
709 {
710         struct fuse_data *data = fuse_get_mpdata(mp);
711
712         if (fdip->tick) {
713                 fticket_refresh(fdip->tick);
714         } else {
715                 fdip->tick = fuse_ticket_fetch(data);
716         }
717
718         FUSE_DIMALLOC(&fdip->tick->tk_ms_fiov, fdip->finh,
719             fdip->indata, fdip->iosize);
720
721         fuse_setup_ihead(fdip->finh, fdip->tick, nid, op, fdip->iosize, pid, cred);
722 }
723
724 void
725 fdisp_make(struct fuse_dispatcher *fdip, enum fuse_opcode op, struct mount *mp,
726     uint64_t nid, struct thread *td, struct ucred *cred)
727 {
728         RECTIFY_TDCR(td, cred);
729
730         return fdisp_make_pid(fdip, op, mp, nid, td->td_proc->p_pid, cred);
731 }
732
733 void
734 fdisp_make_vp(struct fuse_dispatcher *fdip, enum fuse_opcode op,
735     struct vnode *vp, struct thread *td, struct ucred *cred)
736 {
737         RECTIFY_TDCR(td, cred);
738         return fdisp_make_pid(fdip, op, vnode_mount(vp), VTOI(vp),
739             td->td_proc->p_pid, cred);
740 }
741
742 SDT_PROBE_DEFINE2(fuse, , ipc, fdisp_wait_answ_error, "char*", "int");
743
744 int
745 fdisp_wait_answ(struct fuse_dispatcher *fdip)
746 {
747         int err = 0;
748
749         fdip->answ_stat = 0;
750         fuse_insert_callback(fdip->tick, fuse_standard_handler);
751         fuse_insert_message(fdip->tick);
752
753         if ((err = fticket_wait_answer(fdip->tick))) {
754                 fuse_lck_mtx_lock(fdip->tick->tk_aw_mtx);
755
756                 if (fticket_answered(fdip->tick)) {
757                         /*
758                          * Just between noticing the interrupt and getting here,
759                          * the standard handler has completed his job.
760                          * So we drop the ticket and exit as usual.
761                          */
762                         SDT_PROBE2(fuse, , ipc, fdisp_wait_answ_error,
763                                 "IPC: interrupted, already answered", err);
764                         fuse_lck_mtx_unlock(fdip->tick->tk_aw_mtx);
765                         goto out;
766                 } else {
767                         /*
768                          * So we were faster than the standard handler.
769                          * Then by setting the answered flag we get *him*
770                          * to drop the ticket.
771                          */
772                         SDT_PROBE2(fuse, , ipc, fdisp_wait_answ_error,
773                                 "IPC: interrupted, setting to answered", err);
774                         fticket_set_answered(fdip->tick);
775                         fuse_lck_mtx_unlock(fdip->tick->tk_aw_mtx);
776                         return err;
777                 }
778         }
779
780         if (fdip->tick->tk_aw_errno) {
781                 SDT_PROBE2(fuse, , ipc, fdisp_wait_answ_error,
782                         "IPC: explicit EIO-ing", fdip->tick->tk_aw_errno);
783                 err = EIO;
784                 goto out;
785         }
786         if ((err = fdip->tick->tk_aw_ohead.error)) {
787                 SDT_PROBE2(fuse, , ipc, fdisp_wait_answ_error,
788                         "IPC: setting status", fdip->tick->tk_aw_ohead.error);
789                 /*
790                  * This means a "proper" fuse syscall error.
791                  * We record this value so the caller will
792                  * be able to know it's not a boring messaging
793                  * failure, if she wishes so (and if not, she can
794                  * just simply propagate the return value of this routine).
795                  * [XXX Maybe a bitflag would do the job too,
796                  * if other flags needed, this will be converted thusly.]
797                  */
798                 fdip->answ_stat = err;
799                 goto out;
800         }
801         fdip->answ = fticket_resp(fdip->tick)->base;
802         fdip->iosize = fticket_resp(fdip->tick)->len;
803
804         return 0;
805
806 out:
807         return err;
808 }
809
810 void
811 fuse_ipc_init(void)
812 {
813         ticket_zone = uma_zcreate("fuse_ticket", sizeof(struct fuse_ticket),
814             fticket_ctor, fticket_dtor, fticket_init, fticket_fini,
815             UMA_ALIGN_PTR, 0);
816 }
817
818 void
819 fuse_ipc_destroy(void)
820 {
821         uma_zdestroy(ticket_zone);
822 }