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