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