]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/fs/fuse/fuse_device.c
fusefs: support kqueue for /dev/fuse
[FreeBSD/FreeBSD.git] / sys / fs / fuse / fuse_device.c
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2007-2009 Google Inc.
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/types.h>
62 #include <sys/module.h>
63 #include <sys/systm.h>
64 #include <sys/errno.h>
65 #include <sys/param.h>
66 #include <sys/kernel.h>
67 #include <sys/conf.h>
68 #include <sys/uio.h>
69 #include <sys/malloc.h>
70 #include <sys/queue.h>
71 #include <sys/lock.h>
72 #include <sys/sx.h>
73 #include <sys/mutex.h>
74 #include <sys/proc.h>
75 #include <sys/mount.h>
76 #include <sys/sdt.h>
77 #include <sys/stat.h>
78 #include <sys/fcntl.h>
79 #include <sys/sysctl.h>
80 #include <sys/poll.h>
81 #include <sys/selinfo.h>
82
83 #include "fuse.h"
84 #include "fuse_ipc.h"
85
86 SDT_PROVIDER_DECLARE(fusefs);
87 /* 
88  * Fuse trace probe:
89  * arg0: verbosity.  Higher numbers give more verbose messages
90  * arg1: Textual message
91  */
92 SDT_PROBE_DEFINE2(fusefs, , device, trace, "int", "char*");
93
94 static struct cdev *fuse_dev;
95
96 static d_kqfilter_t fuse_device_filter;
97 static d_open_t fuse_device_open;
98 static d_poll_t fuse_device_poll;
99 static d_read_t fuse_device_read;
100 static d_write_t fuse_device_write;
101
102 static struct cdevsw fuse_device_cdevsw = {
103         .d_kqfilter = fuse_device_filter,
104         .d_open = fuse_device_open,
105         .d_name = "fuse",
106         .d_poll = fuse_device_poll,
107         .d_read = fuse_device_read,
108         .d_write = fuse_device_write,
109         .d_version = D_VERSION,
110 };
111
112 static int fuse_device_filt_read(struct knote *kn, long hint);
113 static void fuse_device_filt_detach(struct knote *kn);
114
115 struct filterops fuse_device_rfiltops = {
116         .f_isfd = 1,
117         .f_detach = fuse_device_filt_detach,
118         .f_event = fuse_device_filt_read,
119 };
120
121 /****************************
122  *
123  * >>> Fuse device op defs
124  *
125  ****************************/
126
127 static void
128 fdata_dtor(void *arg)
129 {
130         struct fuse_data *fdata;
131         struct fuse_ticket *tick;
132
133         fdata = arg;
134         if (fdata == NULL)
135                 return;
136
137         fdata_set_dead(fdata);
138
139         FUSE_LOCK();
140         fuse_lck_mtx_lock(fdata->aw_mtx);
141         /* wakup poll()ers */
142         selwakeuppri(&fdata->ks_rsel, PZERO + 1);
143         /* Don't let syscall handlers wait in vain */
144         while ((tick = fuse_aw_pop(fdata))) {
145                 fuse_lck_mtx_lock(tick->tk_aw_mtx);
146                 fticket_set_answered(tick);
147                 tick->tk_aw_errno = ENOTCONN;
148                 wakeup(tick);
149                 fuse_lck_mtx_unlock(tick->tk_aw_mtx);
150                 FUSE_ASSERT_AW_DONE(tick);
151                 fuse_ticket_drop(tick);
152         }
153         fuse_lck_mtx_unlock(fdata->aw_mtx);
154         FUSE_UNLOCK();
155
156         fdata_trydestroy(fdata);
157 }
158
159 static int
160 fuse_device_filter(struct cdev *dev, struct knote *kn)
161 {
162         struct fuse_data *data;
163         int error;
164
165         error = devfs_get_cdevpriv((void **)&data);
166
167         /* EVFILT_WRITE is not supported; the device is always ready to write */
168         if (error == 0 && kn->kn_filter == EVFILT_READ) {
169                 kn->kn_fop = &fuse_device_rfiltops;
170                 kn->kn_hook = data;
171                 knlist_add(&data->ks_rsel.si_note, kn, 0);
172                 error = 0;
173         } else if (error == 0) {
174                 error = EINVAL;
175                 kn->kn_data = error;
176         }
177
178         return (error);
179 }
180
181 static void
182 fuse_device_filt_detach(struct knote *kn)
183 {
184         struct fuse_data *data;
185
186         data = (struct fuse_data*)kn->kn_hook;
187         MPASS(data != NULL);
188         knlist_remove(&data->ks_rsel.si_note, kn, 0);
189         kn->kn_hook = NULL;
190 }
191
192 static int
193 fuse_device_filt_read(struct knote *kn, long hint)
194 {
195         struct fuse_data *data;
196         int ready;
197
198         data = (struct fuse_data*)kn->kn_hook;
199         MPASS(data != NULL);
200
201         mtx_assert(&data->ms_mtx, MA_OWNED);
202         if (fdata_get_dead(data)) {
203                 kn->kn_flags |= EV_EOF;
204                 kn->kn_fflags = ENODEV;
205                 kn->kn_data = 1;
206                 ready = 1;
207         } else if (STAILQ_FIRST(&data->ms_head)) {
208                 /* 
209                  * There is at least one event to read.
210                  * TODO: keep a counter of the number of events to read
211                  */
212                 kn->kn_data = 1;
213                 ready = 1;
214         } else {
215                 ready = 0;
216         }
217
218         return (ready);
219 }
220
221 /*
222  * Resources are set up on a per-open basis
223  */
224 static int
225 fuse_device_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
226 {
227         struct fuse_data *fdata;
228         int error;
229
230         SDT_PROBE2(fusefs, , device, trace, 1, "device open");
231
232         fdata = fdata_alloc(dev, td->td_ucred);
233         error = devfs_set_cdevpriv(fdata, fdata_dtor);
234         if (error != 0)
235                 fdata_trydestroy(fdata);
236         else
237                 SDT_PROBE2(fusefs, , device, trace, 1, "device open success");
238         return (error);
239 }
240
241 int
242 fuse_device_poll(struct cdev *dev, int events, struct thread *td)
243 {
244         struct fuse_data *data;
245         int error, revents = 0;
246
247         error = devfs_get_cdevpriv((void **)&data);
248         if (error != 0)
249                 return (events &
250                     (POLLHUP|POLLIN|POLLRDNORM|POLLOUT|POLLWRNORM));
251
252         if (events & (POLLIN | POLLRDNORM)) {
253                 fuse_lck_mtx_lock(data->ms_mtx);
254                 if (fdata_get_dead(data) || STAILQ_FIRST(&data->ms_head))
255                         revents |= events & (POLLIN | POLLRDNORM);
256                 else
257                         selrecord(td, &data->ks_rsel);
258                 fuse_lck_mtx_unlock(data->ms_mtx);
259         }
260         if (events & (POLLOUT | POLLWRNORM)) {
261                 revents |= events & (POLLOUT | POLLWRNORM);
262         }
263         return (revents);
264 }
265
266 /*
267  * fuse_device_read hangs on the queue of VFS messages.
268  * When it's notified that there is a new one, it picks that and
269  * passes up to the daemon
270  */
271 int
272 fuse_device_read(struct cdev *dev, struct uio *uio, int ioflag)
273 {
274         int err;
275         struct fuse_data *data;
276         struct fuse_ticket *tick;
277         void *buf[] = {NULL, NULL, NULL};
278         int buflen[3];
279         int i;
280
281         SDT_PROBE2(fusefs, , device, trace, 1, "fuse device read");
282
283         err = devfs_get_cdevpriv((void **)&data);
284         if (err != 0)
285                 return (err);
286
287         fuse_lck_mtx_lock(data->ms_mtx);
288 again:
289         if (fdata_get_dead(data)) {
290                 SDT_PROBE2(fusefs, , device, trace, 2,
291                         "we know early on that reader should be kicked so we "
292                         "don't wait for news");
293                 fuse_lck_mtx_unlock(data->ms_mtx);
294                 return (ENODEV);
295         }
296         if (!(tick = fuse_ms_pop(data))) {
297                 /* check if we may block */
298                 if (ioflag & O_NONBLOCK) {
299                         /* get outa here soon */
300                         fuse_lck_mtx_unlock(data->ms_mtx);
301                         return (EAGAIN);
302                 } else {
303                         err = msleep(data, &data->ms_mtx, PCATCH, "fu_msg", 0);
304                         if (err != 0) {
305                                 fuse_lck_mtx_unlock(data->ms_mtx);
306                                 return (fdata_get_dead(data) ? ENODEV : err);
307                         }
308                         tick = fuse_ms_pop(data);
309                 }
310         }
311         if (!tick) {
312                 /*
313                  * We can get here if fuse daemon suddenly terminates,
314                  * eg, by being hit by a SIGKILL
315                  * -- and some other cases, too, tho not totally clear, when
316                  * (cv_signal/wakeup_one signals the whole process ?)
317                  */
318                 SDT_PROBE2(fusefs, , device, trace, 1, "no message on thread");
319                 goto again;
320         }
321         fuse_lck_mtx_unlock(data->ms_mtx);
322
323         if (fdata_get_dead(data)) {
324                 /*
325                  * somebody somewhere -- eg., umount routine --
326                  * wants this liaison finished off
327                  */
328                 SDT_PROBE2(fusefs, , device, trace, 2,
329                         "reader is to be sacked");
330                 if (tick) {
331                         SDT_PROBE2(fusefs, , device, trace, 2, "weird -- "
332                                 "\"kick\" is set tho there is message");
333                         FUSE_ASSERT_MS_DONE(tick);
334                         fuse_ticket_drop(tick);
335                 }
336                 return (ENODEV);        /* This should make the daemon get off
337                                          * of us */
338         }
339         SDT_PROBE2(fusefs, , device, trace, 1,
340                 "fuse device read message successfully");
341
342         KASSERT(tick->tk_ms_bufdata || tick->tk_ms_bufsize == 0,
343             ("non-null buf pointer with positive size"));
344
345         switch (tick->tk_ms_type) {
346         case FT_M_FIOV:
347                 buf[0] = tick->tk_ms_fiov.base;
348                 buflen[0] = tick->tk_ms_fiov.len;
349                 break;
350         case FT_M_BUF:
351                 buf[0] = tick->tk_ms_fiov.base;
352                 buflen[0] = tick->tk_ms_fiov.len;
353                 buf[1] = tick->tk_ms_bufdata;
354                 buflen[1] = tick->tk_ms_bufsize;
355                 break;
356         default:
357                 panic("unknown message type for fuse_ticket %p", tick);
358         }
359
360         for (i = 0; buf[i]; i++) {
361                 /*
362                  * Why not ban mercilessly stupid daemons who can't keep up
363                  * with us? (There is no much use of a partial read here...)
364                  */
365                 /*
366                  * XXX note that in such cases Linux FUSE throws EIO at the
367                  * syscall invoker and stands back to the message queue. The
368                  * rationale should be made clear (and possibly adopt that
369                  * behaviour). Keeping the current scheme at least makes
370                  * fallacy as loud as possible...
371                  */
372                 if (uio->uio_resid < buflen[i]) {
373                         fdata_set_dead(data);
374                         SDT_PROBE2(fusefs, , device, trace, 2,
375                             "daemon is stupid, kick it off...");
376                         err = ENODEV;
377                         break;
378                 }
379                 err = uiomove(buf[i], buflen[i], uio);
380                 if (err)
381                         break;
382         }
383
384         FUSE_ASSERT_MS_DONE(tick);
385         fuse_ticket_drop(tick);
386
387         return (err);
388 }
389
390 static inline int
391 fuse_ohead_audit(struct fuse_out_header *ohead, struct uio *uio)
392 {
393         if (uio->uio_resid + sizeof(struct fuse_out_header) != ohead->len) {
394                 SDT_PROBE2(fusefs, , device, trace, 1,
395                         "Format error: body size "
396                         "differs from size claimed by header");
397                 return (EINVAL);
398         }
399         if (uio->uio_resid && ohead->error) {
400                 SDT_PROBE2(fusefs, , device, trace, 1, 
401                         "Format error: non zero error but message had a body");
402                 return (EINVAL);
403         }
404         /* Sanitize the linuxism of negative errnos */
405         ohead->error = -(ohead->error);
406
407         return (0);
408 }
409
410 SDT_PROBE_DEFINE1(fusefs, , device, fuse_device_write_missing_ticket,
411         "uint64_t");
412 SDT_PROBE_DEFINE1(fusefs, , device, fuse_device_write_found,
413         "struct fuse_ticket*");
414 /*
415  * fuse_device_write first reads the header sent by the daemon.
416  * If that's OK, looks up ticket/callback node by the unique id seen in header.
417  * If the callback node contains a handler function, the uio is passed over
418  * that.
419  */
420 static int
421 fuse_device_write(struct cdev *dev, struct uio *uio, int ioflag)
422 {
423         struct fuse_out_header ohead;
424         int err = 0;
425         struct fuse_data *data;
426         struct fuse_ticket *tick, *itick, *x_tick;
427         int found = 0;
428
429         err = devfs_get_cdevpriv((void **)&data);
430         if (err != 0)
431                 return (err);
432
433         if (uio->uio_resid < sizeof(struct fuse_out_header)) {
434                 SDT_PROBE2(fusefs, , device, trace, 1,
435                         "fuse_device_write got less than a header!");
436                 fdata_set_dead(data);
437                 return (EINVAL);
438         }
439         if ((err = uiomove(&ohead, sizeof(struct fuse_out_header), uio)) != 0)
440                 return (err);
441
442         /*
443          * We check header information (which is redundant) and compare it
444          * with what we see. If we see some inconsistency we discard the
445          * whole answer and proceed on as if it had never existed. In
446          * particular, no pretender will be woken up, regardless the
447          * "unique" value in the header.
448          */
449         if ((err = fuse_ohead_audit(&ohead, uio))) {
450                 fdata_set_dead(data);
451                 return (err);
452         }
453         /* Pass stuff over to callback if there is one installed */
454
455         /* Looking for ticket with the unique id of header */
456         fuse_lck_mtx_lock(data->aw_mtx);
457         TAILQ_FOREACH_SAFE(tick, &data->aw_head, tk_aw_link,
458             x_tick) {
459                 if (tick->tk_unique == ohead.unique) {
460                         SDT_PROBE1(fusefs, , device, fuse_device_write_found,
461                                 tick);
462                         found = 1;
463                         fuse_aw_remove(tick);
464                         break;
465                 }
466         }
467         if (found && tick->irq_unique > 0) {
468                 /* 
469                  * Discard the FUSE_INTERRUPT ticket that tried to interrupt
470                  * this operation
471                  */
472                 TAILQ_FOREACH_SAFE(itick, &data->aw_head, tk_aw_link,
473                     x_tick) {
474                         if (itick->tk_unique == tick->irq_unique) {
475                                 fuse_aw_remove(itick);
476                                 break;
477                         }
478                 }
479                 tick->irq_unique = 0;
480         }
481         fuse_lck_mtx_unlock(data->aw_mtx);
482
483         if (found) {
484                 if (tick->tk_aw_handler) {
485                         /*
486                          * We found a callback with proper handler. In this
487                          * case the out header will be 0wnd by the callback,
488                          * so the fun of freeing that is left for her.
489                          * (Then, by all chance, she'll just get that's done
490                          * via ticket_drop(), so no manual mucking
491                          * around...)
492                          */
493                         SDT_PROBE2(fusefs, , device, trace, 1,
494                                 "pass ticket to a callback");
495                         memcpy(&tick->tk_aw_ohead, &ohead, sizeof(ohead));
496                         err = tick->tk_aw_handler(tick, uio);
497                 } else {
498                         /* pretender doesn't wanna do anything with answer */
499                         SDT_PROBE2(fusefs, , device, trace, 1,
500                                 "stuff devalidated, so we drop it");
501                 }
502
503                 /*
504                  * As aw_mtx was not held during the callback execution the
505                  * ticket may have been inserted again.  However, this is safe
506                  * because fuse_ticket_drop() will deal with refcount anyway.
507                  */
508                 fuse_ticket_drop(tick);
509         } else {
510                 /* no callback at all! */
511                 SDT_PROBE1(fusefs, , device, fuse_device_write_missing_ticket, 
512                         ohead.unique);
513                 if (ohead.error == EAGAIN) {
514                         /* 
515                          * This was probably a response to a FUSE_INTERRUPT
516                          * operation whose original operation is already
517                          * complete.  We can't store FUSE_INTERRUPT tickets
518                          * indefinitely because their responses are optional.
519                          * So we delete them when the original operation
520                          * completes.  And sadly the fuse_header_out doesn't
521                          * identify the opcode, so we have to guess.
522                          */
523                         err = 0;
524                 } else {
525                         err = EINVAL;
526                 }
527         }
528
529         return (err);
530 }
531
532 int
533 fuse_device_init(void)
534 {
535
536         fuse_dev = make_dev(&fuse_device_cdevsw, 0, UID_ROOT, GID_OPERATOR,
537             S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH, "fuse");
538         if (fuse_dev == NULL)
539                 return (ENOMEM);
540         return (0);
541 }
542
543 void
544 fuse_device_destroy(void)
545 {
546
547         MPASS(fuse_dev != NULL);
548         destroy_dev(fuse_dev);
549 }