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 * Copyright (c) 2019 The FreeBSD Foundation
37 *
38 * Portions of this software were developed by BFF Storage Systems, LLC under
39 * sponsorship from the FreeBSD Foundation.
40 *
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
43 * are met:
44 * 1. Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in the
48 * documentation and/or other materials provided with the distribution.
49 *
50 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * SUCH DAMAGE.
61 */
62
63 #include <sys/cdefs.h>
64 #include <sys/types.h>
65 #include <sys/param.h>
66 #include <sys/module.h>
67 #include <sys/systm.h>
68 #include <sys/errno.h>
69 #include <sys/param.h>
70 #include <sys/kernel.h>
71 #include <sys/conf.h>
72 #include <sys/uio.h>
73 #include <sys/malloc.h>
74 #include <sys/queue.h>
75 #include <sys/lock.h>
76 #include <sys/sx.h>
77 #include <sys/mutex.h>
78 #include <sys/proc.h>
79 #include <sys/mount.h>
80 #include <sys/sdt.h>
81 #include <sys/stat.h>
82 #include <sys/fcntl.h>
83 #include <sys/sysctl.h>
84 #include <sys/poll.h>
85 #include <sys/selinfo.h>
86
87 #include "fuse.h"
88 #include "fuse_internal.h"
89 #include "fuse_ipc.h"
90
91 #include <compat/linux/linux_errno.h>
92 #include <compat/linux/linux_errno.inc>
93
94 SDT_PROVIDER_DECLARE(fusefs);
95 /*
96 * Fuse trace probe:
97 * arg0: verbosity. Higher numbers give more verbose messages
98 * arg1: Textual message
99 */
100 SDT_PROBE_DEFINE2(fusefs, , device, trace, "int", "char*");
101
102 static struct cdev *fuse_dev;
103
104 static d_kqfilter_t fuse_device_filter;
105 static d_open_t fuse_device_open;
106 static d_poll_t fuse_device_poll;
107 static d_read_t fuse_device_read;
108 static d_write_t fuse_device_write;
109
110 static struct cdevsw fuse_device_cdevsw = {
111 .d_kqfilter = fuse_device_filter,
112 .d_open = fuse_device_open,
113 .d_name = "fuse",
114 .d_poll = fuse_device_poll,
115 .d_read = fuse_device_read,
116 .d_write = fuse_device_write,
117 .d_version = D_VERSION,
118 };
119
120 static int fuse_device_filt_read(struct knote *kn, long hint);
121 static int fuse_device_filt_write(struct knote *kn, long hint);
122 static void fuse_device_filt_detach(struct knote *kn);
123
124 static const struct filterops fuse_device_rfiltops = {
125 .f_isfd = 1,
126 .f_detach = fuse_device_filt_detach,
127 .f_event = fuse_device_filt_read,
128 };
129
130 static const struct filterops fuse_device_wfiltops = {
131 .f_isfd = 1,
132 .f_event = fuse_device_filt_write,
133 };
134
135 /****************************
136 *
137 * >>> Fuse device op defs
138 *
139 ****************************/
140
141 static void
fdata_dtor(void * arg)142 fdata_dtor(void *arg)
143 {
144 struct fuse_data *fdata;
145 struct fuse_ticket *tick;
146
147 fdata = arg;
148 if (fdata == NULL)
149 return;
150
151 fdata_set_dead(fdata);
152
153 FUSE_LOCK();
154 fuse_lck_mtx_lock(fdata->aw_mtx);
155 /* wakup poll()ers */
156 selwakeuppri(&fdata->ks_rsel, PZERO + 1);
157 /* Don't let syscall handlers wait in vain */
158 while ((tick = fuse_aw_pop(fdata))) {
159 fuse_lck_mtx_lock(tick->tk_aw_mtx);
160 fticket_set_answered(tick);
161 tick->tk_aw_errno = ENOTCONN;
162 wakeup(tick);
163 fuse_lck_mtx_unlock(tick->tk_aw_mtx);
164 FUSE_ASSERT_AW_DONE(tick);
165 fuse_ticket_drop(tick);
166 }
167 fuse_lck_mtx_unlock(fdata->aw_mtx);
168
169 /* Cleanup unsent operations */
170 fuse_lck_mtx_lock(fdata->ms_mtx);
171 while ((tick = fuse_ms_pop(fdata))) {
172 fuse_ticket_drop(tick);
173 }
174 fuse_lck_mtx_unlock(fdata->ms_mtx);
175 FUSE_UNLOCK();
176
177 fdata_trydestroy(fdata);
178 }
179
180 static int
fuse_device_filter(struct cdev * dev,struct knote * kn)181 fuse_device_filter(struct cdev *dev, struct knote *kn)
182 {
183 struct fuse_data *data;
184 int error;
185
186 error = devfs_get_cdevpriv((void **)&data);
187
188 if (error == 0 && kn->kn_filter == EVFILT_READ) {
189 kn->kn_fop = &fuse_device_rfiltops;
190 kn->kn_hook = data;
191 knlist_add(&data->ks_rsel.si_note, kn, 0);
192 error = 0;
193 } else if (error == 0 && kn->kn_filter == EVFILT_WRITE) {
194 kn->kn_fop = &fuse_device_wfiltops;
195 error = 0;
196 } else if (error == 0) {
197 error = EINVAL;
198 kn->kn_data = error;
199 }
200
201 return (error);
202 }
203
204 static void
fuse_device_filt_detach(struct knote * kn)205 fuse_device_filt_detach(struct knote *kn)
206 {
207 struct fuse_data *data;
208
209 data = (struct fuse_data*)kn->kn_hook;
210 MPASS(data != NULL);
211 knlist_remove(&data->ks_rsel.si_note, kn, 0);
212 kn->kn_hook = NULL;
213 }
214
215 static int
fuse_device_filt_read(struct knote * kn,long hint)216 fuse_device_filt_read(struct knote *kn, long hint)
217 {
218 struct fuse_data *data;
219 int ready;
220
221 data = (struct fuse_data*)kn->kn_hook;
222 MPASS(data != NULL);
223
224 mtx_assert(&data->ms_mtx, MA_OWNED);
225 if (fdata_get_dead(data)) {
226 kn->kn_flags |= EV_EOF;
227 kn->kn_fflags = ENODEV;
228 kn->kn_data = 1;
229 ready = 1;
230 } else if (STAILQ_FIRST(&data->ms_head)) {
231 MPASS(data->ms_count >= 1);
232 kn->kn_data = data->ms_count;
233 ready = 1;
234 } else {
235 ready = 0;
236 }
237
238 return (ready);
239 }
240
241 static int
fuse_device_filt_write(struct knote * kn,long hint)242 fuse_device_filt_write(struct knote *kn, long hint)
243 {
244
245 kn->kn_data = 0;
246
247 /* The device is always ready to write, so we return 1*/
248 return (1);
249 }
250
251 /*
252 * Resources are set up on a per-open basis
253 */
254 static int
fuse_device_open(struct cdev * dev,int oflags,int devtype,struct thread * td)255 fuse_device_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
256 {
257 struct fuse_data *fdata;
258 int error;
259
260 SDT_PROBE2(fusefs, , device, trace, 1, "device open");
261
262 fdata = fdata_alloc(dev, td->td_ucred);
263 error = devfs_set_cdevpriv(fdata, fdata_dtor);
264 if (error != 0)
265 fdata_trydestroy(fdata);
266 else
267 SDT_PROBE2(fusefs, , device, trace, 1, "device open success");
268 return (error);
269 }
270
271 int
fuse_device_poll(struct cdev * dev,int events,struct thread * td)272 fuse_device_poll(struct cdev *dev, int events, struct thread *td)
273 {
274 struct fuse_data *data;
275 int error, revents = 0;
276
277 error = devfs_get_cdevpriv((void **)&data);
278 if (error != 0)
279 return (events &
280 (POLLHUP|POLLIN|POLLRDNORM|POLLOUT|POLLWRNORM));
281
282 if (events & (POLLIN | POLLRDNORM)) {
283 fuse_lck_mtx_lock(data->ms_mtx);
284 if (fdata_get_dead(data) || STAILQ_FIRST(&data->ms_head))
285 revents |= events & (POLLIN | POLLRDNORM);
286 else
287 selrecord(td, &data->ks_rsel);
288 fuse_lck_mtx_unlock(data->ms_mtx);
289 }
290 if (events & (POLLOUT | POLLWRNORM)) {
291 revents |= events & (POLLOUT | POLLWRNORM);
292 }
293 return (revents);
294 }
295
296 /*
297 * fuse_device_read hangs on the queue of VFS messages.
298 * When it's notified that there is a new one, it picks that and
299 * passes up to the daemon
300 */
301 int
fuse_device_read(struct cdev * dev,struct uio * uio,int ioflag)302 fuse_device_read(struct cdev *dev, struct uio *uio, int ioflag)
303 {
304 int err;
305 struct fuse_data *data;
306 struct fuse_ticket *tick;
307 void *buf;
308 int buflen;
309
310 SDT_PROBE2(fusefs, , device, trace, 1, "fuse device read");
311
312 err = devfs_get_cdevpriv((void **)&data);
313 if (err != 0)
314 return (err);
315
316 fuse_lck_mtx_lock(data->ms_mtx);
317 again:
318 if (fdata_get_dead(data)) {
319 SDT_PROBE2(fusefs, , device, trace, 2,
320 "we know early on that reader should be kicked so we "
321 "don't wait for news");
322 fuse_lck_mtx_unlock(data->ms_mtx);
323 return (ENODEV);
324 }
325 if (!(tick = fuse_ms_pop(data))) {
326 /* check if we may block */
327 if (ioflag & O_NONBLOCK) {
328 /* get outa here soon */
329 fuse_lck_mtx_unlock(data->ms_mtx);
330 return (EAGAIN);
331 } else {
332 err = msleep(data, &data->ms_mtx, PCATCH, "fu_msg", 0);
333 if (err != 0) {
334 fuse_lck_mtx_unlock(data->ms_mtx);
335 return (fdata_get_dead(data) ? ENODEV : err);
336 }
337 tick = fuse_ms_pop(data);
338 }
339 }
340 if (!tick) {
341 /*
342 * We can get here if fuse daemon suddenly terminates,
343 * eg, by being hit by a SIGKILL
344 * -- and some other cases, too, tho not totally clear, when
345 * (cv_signal/wakeup_one signals the whole process ?)
346 */
347 SDT_PROBE2(fusefs, , device, trace, 1, "no message on thread");
348 goto again;
349 }
350 fuse_lck_mtx_unlock(data->ms_mtx);
351
352 if (fdata_get_dead(data)) {
353 /*
354 * somebody somewhere -- eg., umount routine --
355 * wants this liaison finished off
356 */
357 SDT_PROBE2(fusefs, , device, trace, 2,
358 "reader is to be sacked");
359 if (tick) {
360 SDT_PROBE2(fusefs, , device, trace, 2, "weird -- "
361 "\"kick\" is set tho there is message");
362 FUSE_ASSERT_MS_DONE(tick);
363 fuse_ticket_drop(tick);
364 }
365 return (ENODEV); /* This should make the daemon get off
366 * of us */
367 }
368 SDT_PROBE2(fusefs, , device, trace, 1,
369 "fuse device read message successfully");
370
371 buf = tick->tk_ms_fiov.base;
372 buflen = tick->tk_ms_fiov.len;
373
374 /*
375 * Why not ban mercilessly stupid daemons who can't keep up
376 * with us? (There is no much use of a partial read here...)
377 */
378 /*
379 * XXX note that in such cases Linux FUSE throws EIO at the
380 * syscall invoker and stands back to the message queue. The
381 * rationale should be made clear (and possibly adopt that
382 * behaviour). Keeping the current scheme at least makes
383 * fallacy as loud as possible...
384 */
385 if (uio->uio_resid < buflen) {
386 fdata_set_dead(data);
387 SDT_PROBE2(fusefs, , device, trace, 2,
388 "daemon is stupid, kick it off...");
389 err = ENODEV;
390 } else {
391 err = uiomove(buf, buflen, uio);
392 }
393
394 FUSE_ASSERT_MS_DONE(tick);
395 fuse_ticket_drop(tick);
396
397 return (err);
398 }
399
400 static inline int
fuse_ohead_audit(struct fuse_out_header * ohead,struct uio * uio)401 fuse_ohead_audit(struct fuse_out_header *ohead, struct uio *uio)
402 {
403 if (uio->uio_resid + sizeof(struct fuse_out_header) != ohead->len) {
404 SDT_PROBE2(fusefs, , device, trace, 1,
405 "Format error: body size "
406 "differs from size claimed by header");
407 return (EINVAL);
408 }
409 if (uio->uio_resid && ohead->unique != 0 && ohead->error) {
410 SDT_PROBE2(fusefs, , device, trace, 1,
411 "Format error: non zero error but message had a body");
412 return (EINVAL);
413 }
414
415 return (0);
416 }
417
418 SDT_PROBE_DEFINE1(fusefs, , device, fuse_device_write_notify,
419 "struct fuse_out_header*");
420 SDT_PROBE_DEFINE1(fusefs, , device, fuse_device_write_missing_ticket,
421 "uint64_t");
422 SDT_PROBE_DEFINE1(fusefs, , device, fuse_device_write_found,
423 "struct fuse_ticket*");
424 /*
425 * fuse_device_write first reads the header sent by the daemon.
426 * If that's OK, looks up ticket/callback node by the unique id seen in header.
427 * If the callback node contains a handler function, the uio is passed over
428 * that.
429 */
430 static int
fuse_device_write(struct cdev * dev,struct uio * uio,int ioflag)431 fuse_device_write(struct cdev *dev, struct uio *uio, int ioflag)
432 {
433 struct fuse_out_header ohead;
434 int err = 0;
435 struct fuse_data *data;
436 struct mount *mp;
437 struct fuse_ticket *tick, *itick, *x_tick;
438 int found = 0;
439
440 err = devfs_get_cdevpriv((void **)&data);
441 if (err != 0)
442 return (err);
443
444 if (uio->uio_resid < sizeof(struct fuse_out_header)) {
445 SDT_PROBE2(fusefs, , device, trace, 1,
446 "fuse_device_write got less than a header!");
447 fdata_set_dead(data);
448 return (EINVAL);
449 }
450 if ((err = uiomove(&ohead, sizeof(struct fuse_out_header), uio)) != 0)
451 return (err);
452
453 if (data->linux_errnos != 0 && ohead.error != 0) {
454 err = -ohead.error;
455 if (err < 0 || err >= nitems(linux_to_bsd_errtbl))
456 return (EINVAL);
457
458 /* '-', because it will get flipped again below */
459 ohead.error = -linux_to_bsd_errtbl[err];
460 }
461
462 /*
463 * We check header information (which is redundant) and compare it
464 * with what we see. If we see some inconsistency we discard the
465 * whole answer and proceed on as if it had never existed. In
466 * particular, no pretender will be woken up, regardless the
467 * "unique" value in the header.
468 */
469 if ((err = fuse_ohead_audit(&ohead, uio))) {
470 fdata_set_dead(data);
471 return (err);
472 }
473 /* Pass stuff over to callback if there is one installed */
474
475 /* Looking for ticket with the unique id of header */
476 fuse_lck_mtx_lock(data->aw_mtx);
477 TAILQ_FOREACH_SAFE(tick, &data->aw_head, tk_aw_link,
478 x_tick) {
479 if (tick->tk_unique == ohead.unique) {
480 SDT_PROBE1(fusefs, , device, fuse_device_write_found,
481 tick);
482 found = 1;
483 fuse_aw_remove(tick);
484 break;
485 }
486 }
487 if (found && tick->irq_unique > 0) {
488 /*
489 * Discard the FUSE_INTERRUPT ticket that tried to interrupt
490 * this operation
491 */
492 TAILQ_FOREACH_SAFE(itick, &data->aw_head, tk_aw_link,
493 x_tick) {
494 if (itick->tk_unique == tick->irq_unique) {
495 fuse_aw_remove(itick);
496 fuse_ticket_drop(itick);
497 break;
498 }
499 }
500 tick->irq_unique = 0;
501 }
502 fuse_lck_mtx_unlock(data->aw_mtx);
503
504 if (found) {
505 if (tick->tk_aw_handler) {
506 /*
507 * We found a callback with proper handler. In this
508 * case the out header will be 0wnd by the callback,
509 * so the fun of freeing that is left for her.
510 * (Then, by all chance, she'll just get that's done
511 * via ticket_drop(), so no manual mucking
512 * around...)
513 */
514 SDT_PROBE2(fusefs, , device, trace, 1,
515 "pass ticket to a callback");
516 /* Sanitize the linuxism of negative errnos */
517 ohead.error *= -1;
518 if (ohead.error < 0 || ohead.error > ELAST) {
519 /* Illegal error code */
520 ohead.error = EIO;
521 memcpy(&tick->tk_aw_ohead, &ohead,
522 sizeof(ohead));
523 tick->tk_aw_handler(tick, uio);
524 err = EINVAL;
525 } else {
526 memcpy(&tick->tk_aw_ohead, &ohead,
527 sizeof(ohead));
528 err = tick->tk_aw_handler(tick, uio);
529 }
530 } else {
531 /* pretender doesn't wanna do anything with answer */
532 SDT_PROBE2(fusefs, , device, trace, 1,
533 "stuff devalidated, so we drop it");
534 }
535
536 /*
537 * As aw_mtx was not held during the callback execution the
538 * ticket may have been inserted again. However, this is safe
539 * because fuse_ticket_drop() will deal with refcount anyway.
540 */
541 fuse_ticket_drop(tick);
542 } else if (ohead.unique == 0){
543 /* unique == 0 means asynchronous notification */
544 SDT_PROBE1(fusefs, , device, fuse_device_write_notify, &ohead);
545 mp = data->mp;
546 vfs_ref(mp);
547 err = vfs_busy(mp, 0);
548 vfs_rel(mp);
549 if (err)
550 return (err);
551
552 switch (ohead.error) {
553 case FUSE_NOTIFY_INVAL_ENTRY:
554 err = fuse_internal_invalidate_entry(mp, uio);
555 break;
556 case FUSE_NOTIFY_INVAL_INODE:
557 err = fuse_internal_invalidate_inode(mp, uio);
558 break;
559 case FUSE_NOTIFY_RETRIEVE:
560 case FUSE_NOTIFY_STORE:
561 /*
562 * Unimplemented. I don't know of any file systems
563 * that use them, and the protocol isn't sound anyway,
564 * since the notification messages don't include the
565 * inode's generation number. Without that, it's
566 * possible to manipulate the cache of the wrong vnode.
567 * Finally, it's not defined what this message should
568 * do for a file with dirty cache.
569 */
570 case FUSE_NOTIFY_POLL:
571 /* Unimplemented. See comments in fuse_vnops */
572 default:
573 /* Not implemented */
574 err = ENOSYS;
575 }
576 vfs_unbusy(mp);
577 } else {
578 /* no callback at all! */
579 SDT_PROBE1(fusefs, , device, fuse_device_write_missing_ticket,
580 ohead.unique);
581 if (ohead.error == -EAGAIN) {
582 /*
583 * This was probably a response to a FUSE_INTERRUPT
584 * operation whose original operation is already
585 * complete. We can't store FUSE_INTERRUPT tickets
586 * indefinitely because their responses are optional.
587 * So we delete them when the original operation
588 * completes. And sadly the fuse_header_out doesn't
589 * identify the opcode, so we have to guess.
590 */
591 err = 0;
592 } else {
593 err = EINVAL;
594 }
595 }
596
597 return (err);
598 }
599
600 int
fuse_device_init(void)601 fuse_device_init(void)
602 {
603
604 fuse_dev = make_dev(&fuse_device_cdevsw, 0, UID_ROOT, GID_OPERATOR,
605 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH, "fuse");
606 if (fuse_dev == NULL)
607 return (ENOMEM);
608 return (0);
609 }
610
611 void
fuse_device_destroy(void)612 fuse_device_destroy(void)
613 {
614
615 MPASS(fuse_dev != NULL);
616 destroy_dev(fuse_dev);
617 }
618