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