server: Remember .open(readonly) status
[nbdkit/ericb.git] / server / filters.c
bloba8d7dd7ca385d8509719b11044884a7d81aa002f
1 /* nbdkit
2 * Copyright (C) 2013-2019 Red Hat Inc.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * * Neither the name of Red Hat nor the names of its contributors may be
16 * used to endorse or promote products derived from this software without
17 * specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
33 #include <config.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <stdint.h>
38 #include <string.h>
39 #include <inttypes.h>
40 #include <assert.h>
42 #include "internal.h"
44 /* We extend the generic backend struct with extra fields relating
45 * to this filter.
47 struct backend_filter {
48 struct backend backend;
49 struct nbdkit_filter filter;
52 /* Literally a backend + a connection pointer. This is the
53 * implementation of ‘void *nxdata’ in the filter API.
55 struct b_conn {
56 struct backend *b;
57 struct connection *conn;
60 /* Note this frees the whole chain. */
61 static void
62 filter_free (struct backend *b)
64 struct backend_filter *f = container_of (b, struct backend_filter, backend);
66 b->next->free (b->next);
68 backend_unload (b, f->filter.unload);
69 free (f);
72 static int
73 filter_thread_model (struct backend *b)
75 struct backend_filter *f = container_of (b, struct backend_filter, backend);
76 int filter_thread_model = NBDKIT_THREAD_MODEL_PARALLEL;
77 int thread_model = b->next->thread_model (b->next);
79 if (f->filter.thread_model) {
80 filter_thread_model = f->filter.thread_model ();
81 if (filter_thread_model == -1)
82 exit (EXIT_FAILURE);
85 if (filter_thread_model < thread_model) /* more serialized */
86 thread_model = filter_thread_model;
88 return thread_model;
91 /* This is actually passing the request through to the final plugin,
92 * hence the function name.
94 static const char *
95 plugin_name (struct backend *b)
97 return b->next->plugin_name (b->next);
100 static const char *
101 filter_version (struct backend *b)
103 struct backend_filter *f = container_of (b, struct backend_filter, backend);
105 return f->filter._version;
108 static void
109 filter_usage (struct backend *b)
111 struct backend_filter *f = container_of (b, struct backend_filter, backend);
112 const char *p;
114 printf ("filter: %s", b->name);
115 if (f->filter.longname)
116 printf (" (%s)", f->filter.longname);
117 printf ("\n");
118 printf ("(%s)\n", b->filename);
119 if (f->filter.description) {
120 printf ("%s", f->filter.description);
121 if ((p = strrchr (f->filter.description, '\n')) == NULL || p[1])
122 printf ("\n");
124 if (f->filter.config_help) {
125 printf ("%s", f->filter.config_help);
126 if ((p = strrchr (f->filter.config_help, '\n')) == NULL || p[1])
127 printf ("\n");
131 static void
132 filter_dump_fields (struct backend *b)
134 b->next->dump_fields (b->next);
137 static int
138 next_config (void *nxdata, const char *key, const char *value)
140 struct backend *b = nxdata;
141 b->config (b, key, value);
142 return 0;
145 static void
146 filter_config (struct backend *b, const char *key, const char *value)
148 struct backend_filter *f = container_of (b, struct backend_filter, backend);
150 debug ("%s: config key=%s, value=%s",
151 b->name, key, value);
153 if (f->filter.config) {
154 if (f->filter.config (next_config, b->next, key, value) == -1)
155 exit (EXIT_FAILURE);
157 else
158 b->next->config (b->next, key, value);
161 static int
162 next_config_complete (void *nxdata)
164 struct backend *b = nxdata;
165 b->config_complete (b);
166 return 0;
169 static void
170 filter_config_complete (struct backend *b)
172 struct backend_filter *f = container_of (b, struct backend_filter, backend);
174 debug ("%s: config_complete", b->name);
176 if (f->filter.config_complete) {
177 if (f->filter.config_complete (next_config_complete, b->next) == -1)
178 exit (EXIT_FAILURE);
180 else
181 b->next->config_complete (b->next);
184 /* magic_config_key only applies to plugins, so this passes the
185 * request through to the plugin (hence the name).
187 static const char *
188 plugin_magic_config_key (struct backend *b)
190 return b->next->magic_config_key (b->next);
193 static int
194 next_open (void *nxdata, int readonly)
196 struct b_conn *b_conn = nxdata;
198 return backend_open (b_conn->b, b_conn->conn, readonly);
201 static int
202 filter_open (struct backend *b, struct connection *conn, int readonly)
204 struct backend_filter *f = container_of (b, struct backend_filter, backend);
205 struct b_conn nxdata = { .b = b->next, .conn = conn };
206 void *handle;
208 if (f->filter.open) {
209 handle = f->filter.open (next_open, &nxdata, readonly);
210 if (handle == NULL)
211 return -1;
212 backend_set_handle (b, conn, handle);
213 return 0;
215 else
216 return backend_open (b->next, conn, readonly);
219 static void
220 filter_close (struct backend *b, struct connection *conn)
222 struct backend_filter *f = container_of (b, struct backend_filter, backend);
223 void *handle = connection_get_handle (conn, b->i);
225 debug ("%s: close", b->name);
227 if (f->filter.close)
228 f->filter.close (handle);
229 b->next->close (b->next, conn);
232 /* The next_functions structure contains pointers to backend
233 * functions. However because these functions are all expecting a
234 * backend and a connection, we cannot call them directly, but must
235 * write some next_* functions that unpack the two parameters from a
236 * single ‘void *nxdata’ struct pointer (‘b_conn’).
239 static int64_t
240 next_get_size (void *nxdata)
242 struct b_conn *b_conn = nxdata;
243 return backend_get_size (b_conn->b, b_conn->conn);
246 static int
247 next_can_write (void *nxdata)
249 struct b_conn *b_conn = nxdata;
250 return backend_can_write (b_conn->b, b_conn->conn);
253 static int
254 next_can_flush (void *nxdata)
256 struct b_conn *b_conn = nxdata;
257 return backend_can_flush (b_conn->b, b_conn->conn);
260 static int
261 next_is_rotational (void *nxdata)
263 struct b_conn *b_conn = nxdata;
264 return backend_is_rotational (b_conn->b, b_conn->conn);
267 static int
268 next_can_trim (void *nxdata)
270 struct b_conn *b_conn = nxdata;
271 return backend_can_trim (b_conn->b, b_conn->conn);
274 static int
275 next_can_zero (void *nxdata)
277 struct b_conn *b_conn = nxdata;
278 return backend_can_zero (b_conn->b, b_conn->conn);
281 static int
282 next_can_extents (void *nxdata)
284 struct b_conn *b_conn = nxdata;
285 return backend_can_extents (b_conn->b, b_conn->conn);
288 static int
289 next_can_fua (void *nxdata)
291 struct b_conn *b_conn = nxdata;
292 return backend_can_fua (b_conn->b, b_conn->conn);
295 static int
296 next_can_multi_conn (void *nxdata)
298 struct b_conn *b_conn = nxdata;
299 return backend_can_multi_conn (b_conn->b, b_conn->conn);
302 static int
303 next_can_cache (void *nxdata)
305 struct b_conn *b_conn = nxdata;
306 return backend_can_cache (b_conn->b, b_conn->conn);
309 static int
310 next_pread (void *nxdata, void *buf, uint32_t count, uint64_t offset,
311 uint32_t flags, int *err)
313 struct b_conn *b_conn = nxdata;
314 return backend_pread (b_conn->b, b_conn->conn, buf, count, offset, flags,
315 err);
318 static int
319 next_pwrite (void *nxdata, const void *buf, uint32_t count, uint64_t offset,
320 uint32_t flags, int *err)
322 struct b_conn *b_conn = nxdata;
323 return backend_pwrite (b_conn->b, b_conn->conn, buf, count, offset, flags,
324 err);
327 static int
328 next_flush (void *nxdata, uint32_t flags, int *err)
330 struct b_conn *b_conn = nxdata;
331 return backend_flush (b_conn->b, b_conn->conn, flags, err);
334 static int
335 next_trim (void *nxdata, uint32_t count, uint64_t offset, uint32_t flags,
336 int *err)
338 struct b_conn *b_conn = nxdata;
339 return backend_trim (b_conn->b, b_conn->conn, count, offset, flags, err);
342 static int
343 next_zero (void *nxdata, uint32_t count, uint64_t offset, uint32_t flags,
344 int *err)
346 struct b_conn *b_conn = nxdata;
347 return backend_zero (b_conn->b, b_conn->conn, count, offset, flags, err);
350 static int
351 next_extents (void *nxdata, uint32_t count, uint64_t offset, uint32_t flags,
352 struct nbdkit_extents *extents, int *err)
354 struct b_conn *b_conn = nxdata;
355 return backend_extents (b_conn->b, b_conn->conn, count, offset, flags,
356 extents, err);
359 static int
360 next_cache (void *nxdata, uint32_t count, uint64_t offset,
361 uint32_t flags, int *err)
363 struct b_conn *b_conn = nxdata;
364 return backend_cache (b_conn->b, b_conn->conn, count, offset, flags, err);
367 static struct nbdkit_next_ops next_ops = {
368 .get_size = next_get_size,
369 .can_write = next_can_write,
370 .can_flush = next_can_flush,
371 .is_rotational = next_is_rotational,
372 .can_trim = next_can_trim,
373 .can_zero = next_can_zero,
374 .can_extents = next_can_extents,
375 .can_fua = next_can_fua,
376 .can_multi_conn = next_can_multi_conn,
377 .can_cache = next_can_cache,
378 .pread = next_pread,
379 .pwrite = next_pwrite,
380 .flush = next_flush,
381 .trim = next_trim,
382 .zero = next_zero,
383 .extents = next_extents,
384 .cache = next_cache,
387 static int
388 filter_prepare (struct backend *b, struct connection *conn)
390 struct backend_filter *f = container_of (b, struct backend_filter, backend);
391 void *handle = connection_get_handle (conn, b->i);
392 struct b_conn nxdata = { .b = b->next, .conn = conn };
394 debug ("%s: prepare", b->name);
396 /* Call these in order starting from the filter closest to the
397 * plugin.
399 if (b->next->prepare (b->next, conn) == -1)
400 return -1;
402 if (f->filter.prepare &&
403 f->filter.prepare (&next_ops, &nxdata, handle) == -1)
404 return -1;
406 return 0;
409 static int
410 filter_finalize (struct backend *b, struct connection *conn)
412 struct backend_filter *f = container_of (b, struct backend_filter, backend);
413 void *handle = connection_get_handle (conn, b->i);
414 struct b_conn nxdata = { .b = b->next, .conn = conn };
416 debug ("%s: finalize", b->name);
418 /* Call these in reverse order to .prepare above, starting from the
419 * filter furthest away from the plugin.
421 if (f->filter.finalize &&
422 f->filter.finalize (&next_ops, &nxdata, handle) == -1)
423 return -1;
425 return b->next->finalize (b->next, conn);
428 static int64_t
429 filter_get_size (struct backend *b, struct connection *conn)
431 struct backend_filter *f = container_of (b, struct backend_filter, backend);
432 void *handle = connection_get_handle (conn, b->i);
433 struct b_conn nxdata = { .b = b->next, .conn = conn };
435 if (f->filter.get_size)
436 return f->filter.get_size (&next_ops, &nxdata, handle);
437 else
438 return backend_get_size (b->next, conn);
441 static int
442 filter_can_write (struct backend *b, struct connection *conn)
444 struct backend_filter *f = container_of (b, struct backend_filter, backend);
445 void *handle = connection_get_handle (conn, b->i);
446 struct b_conn nxdata = { .b = b->next, .conn = conn };
448 if (f->filter.can_write)
449 return f->filter.can_write (&next_ops, &nxdata, handle);
450 else
451 return backend_can_write (b->next, conn);
454 static int
455 filter_can_flush (struct backend *b, struct connection *conn)
457 struct backend_filter *f = container_of (b, struct backend_filter, backend);
458 void *handle = connection_get_handle (conn, b->i);
459 struct b_conn nxdata = { .b = b->next, .conn = conn };
461 if (f->filter.can_flush)
462 return f->filter.can_flush (&next_ops, &nxdata, handle);
463 else
464 return backend_can_flush (b->next, conn);
467 static int
468 filter_is_rotational (struct backend *b, struct connection *conn)
470 struct backend_filter *f = container_of (b, struct backend_filter, backend);
471 void *handle = connection_get_handle (conn, b->i);
472 struct b_conn nxdata = { .b = b->next, .conn = conn };
474 if (f->filter.is_rotational)
475 return f->filter.is_rotational (&next_ops, &nxdata, handle);
476 else
477 return backend_is_rotational (b->next, conn);
480 static int
481 filter_can_trim (struct backend *b, struct connection *conn)
483 struct backend_filter *f = container_of (b, struct backend_filter, backend);
484 void *handle = connection_get_handle (conn, b->i);
485 struct b_conn nxdata = { .b = b->next, .conn = conn };
487 if (f->filter.can_trim)
488 return f->filter.can_trim (&next_ops, &nxdata, handle);
489 else
490 return backend_can_trim (b->next, conn);
493 static int
494 filter_can_zero (struct backend *b, struct connection *conn)
496 struct backend_filter *f = container_of (b, struct backend_filter, backend);
497 void *handle = connection_get_handle (conn, b->i);
498 struct b_conn nxdata = { .b = b->next, .conn = conn };
500 if (f->filter.can_zero)
501 return f->filter.can_zero (&next_ops, &nxdata, handle);
502 else
503 return backend_can_zero (b->next, conn);
506 static int
507 filter_can_extents (struct backend *b, struct connection *conn)
509 struct backend_filter *f = container_of (b, struct backend_filter, backend);
510 void *handle = connection_get_handle (conn, b->i);
511 struct b_conn nxdata = { .b = b->next, .conn = conn };
513 if (f->filter.can_extents)
514 return f->filter.can_extents (&next_ops, &nxdata, handle);
515 else
516 return backend_can_extents (b->next, conn);
519 static int
520 filter_can_fua (struct backend *b, struct connection *conn)
522 struct backend_filter *f = container_of (b, struct backend_filter, backend);
523 void *handle = connection_get_handle (conn, b->i);
524 struct b_conn nxdata = { .b = b->next, .conn = conn };
526 if (f->filter.can_fua)
527 return f->filter.can_fua (&next_ops, &nxdata, handle);
528 else
529 return backend_can_fua (b->next, conn);
532 static int
533 filter_can_multi_conn (struct backend *b, struct connection *conn)
535 struct backend_filter *f = container_of (b, struct backend_filter, backend);
536 void *handle = connection_get_handle (conn, b->i);
537 struct b_conn nxdata = { .b = b->next, .conn = conn };
539 if (f->filter.can_multi_conn)
540 return f->filter.can_multi_conn (&next_ops, &nxdata, handle);
541 else
542 return backend_can_multi_conn (b->next, conn);
545 static int
546 filter_can_cache (struct backend *b, struct connection *conn)
548 struct backend_filter *f = container_of (b, struct backend_filter, backend);
549 void *handle = connection_get_handle (conn, b->i);
550 struct b_conn nxdata = { .b = b->next, .conn = conn };
552 if (f->filter.can_cache)
553 return f->filter.can_cache (&next_ops, &nxdata, handle);
554 else
555 return backend_can_cache (b->next, conn);
558 static int
559 filter_pread (struct backend *b, struct connection *conn,
560 void *buf, uint32_t count, uint64_t offset,
561 uint32_t flags, int *err)
563 struct backend_filter *f = container_of (b, struct backend_filter, backend);
564 void *handle = connection_get_handle (conn, b->i);
565 struct b_conn nxdata = { .b = b->next, .conn = conn };
567 if (f->filter.pread)
568 return f->filter.pread (&next_ops, &nxdata, handle,
569 buf, count, offset, flags, err);
570 else
571 return backend_pread (b->next, conn, buf, count, offset, flags, err);
574 static int
575 filter_pwrite (struct backend *b, struct connection *conn,
576 const void *buf, uint32_t count, uint64_t offset,
577 uint32_t flags, int *err)
579 struct backend_filter *f = container_of (b, struct backend_filter, backend);
580 void *handle = connection_get_handle (conn, b->i);
581 struct b_conn nxdata = { .b = b->next, .conn = conn };
583 if (f->filter.pwrite)
584 return f->filter.pwrite (&next_ops, &nxdata, handle,
585 buf, count, offset, flags, err);
586 else
587 return backend_pwrite (b->next, conn, buf, count, offset, flags, err);
590 static int
591 filter_flush (struct backend *b, struct connection *conn, uint32_t flags,
592 int *err)
594 struct backend_filter *f = container_of (b, struct backend_filter, backend);
595 void *handle = connection_get_handle (conn, b->i);
596 struct b_conn nxdata = { .b = b->next, .conn = conn };
598 if (f->filter.flush)
599 return f->filter.flush (&next_ops, &nxdata, handle, flags, err);
600 else
601 return backend_flush (b->next, conn, flags, err);
604 static int
605 filter_trim (struct backend *b, struct connection *conn,
606 uint32_t count, uint64_t offset,
607 uint32_t flags, int *err)
609 struct backend_filter *f = container_of (b, struct backend_filter, backend);
610 void *handle = connection_get_handle (conn, b->i);
611 struct b_conn nxdata = { .b = b->next, .conn = conn };
613 if (f->filter.trim)
614 return f->filter.trim (&next_ops, &nxdata, handle, count, offset, flags,
615 err);
616 else
617 return backend_trim (b->next, conn, count, offset, flags, err);
620 static int
621 filter_zero (struct backend *b, struct connection *conn,
622 uint32_t count, uint64_t offset, uint32_t flags, int *err)
624 struct backend_filter *f = container_of (b, struct backend_filter, backend);
625 void *handle = connection_get_handle (conn, b->i);
626 struct b_conn nxdata = { .b = b->next, .conn = conn };
628 if (f->filter.zero)
629 return f->filter.zero (&next_ops, &nxdata, handle,
630 count, offset, flags, err);
631 else
632 return backend_zero (b->next, conn, count, offset, flags, err);
635 static int
636 filter_extents (struct backend *b, struct connection *conn,
637 uint32_t count, uint64_t offset, uint32_t flags,
638 struct nbdkit_extents *extents, int *err)
640 struct backend_filter *f = container_of (b, struct backend_filter, backend);
641 void *handle = connection_get_handle (conn, b->i);
642 struct b_conn nxdata = { .b = b->next, .conn = conn };
644 if (f->filter.extents)
645 return f->filter.extents (&next_ops, &nxdata, handle,
646 count, offset, flags,
647 extents, err);
648 else
649 return backend_extents (b->next, conn, count, offset, flags,
650 extents, err);
653 static int
654 filter_cache (struct backend *b, struct connection *conn,
655 uint32_t count, uint64_t offset,
656 uint32_t flags, int *err)
658 struct backend_filter *f = container_of (b, struct backend_filter, backend);
659 void *handle = connection_get_handle (conn, b->i);
660 struct b_conn nxdata = { .b = b->next, .conn = conn };
663 if (f->filter.cache)
664 return f->filter.cache (&next_ops, &nxdata, handle,
665 count, offset, flags, err);
666 else
667 return backend_cache (b->next, conn, count, offset, flags, err);
670 static struct backend filter_functions = {
671 .free = filter_free,
672 .thread_model = filter_thread_model,
673 .plugin_name = plugin_name,
674 .usage = filter_usage,
675 .version = filter_version,
676 .dump_fields = filter_dump_fields,
677 .config = filter_config,
678 .config_complete = filter_config_complete,
679 .magic_config_key = plugin_magic_config_key,
680 .open = filter_open,
681 .prepare = filter_prepare,
682 .finalize = filter_finalize,
683 .close = filter_close,
684 .get_size = filter_get_size,
685 .can_write = filter_can_write,
686 .can_flush = filter_can_flush,
687 .is_rotational = filter_is_rotational,
688 .can_trim = filter_can_trim,
689 .can_zero = filter_can_zero,
690 .can_extents = filter_can_extents,
691 .can_fua = filter_can_fua,
692 .can_multi_conn = filter_can_multi_conn,
693 .can_cache = filter_can_cache,
694 .pread = filter_pread,
695 .pwrite = filter_pwrite,
696 .flush = filter_flush,
697 .trim = filter_trim,
698 .zero = filter_zero,
699 .extents = filter_extents,
700 .cache = filter_cache,
703 /* Register and load a filter. */
704 struct backend *
705 filter_register (struct backend *next, size_t index, const char *filename,
706 void *dl, struct nbdkit_filter *(*filter_init) (void))
708 struct backend_filter *f;
709 const struct nbdkit_filter *filter;
711 f = calloc (1, sizeof *f);
712 if (f == NULL) {
713 perror ("strdup");
714 exit (EXIT_FAILURE);
717 f->backend = filter_functions;
718 backend_init (&f->backend, next, index, filename, dl, "filter");
720 /* Call the initialization function which returns the address of the
721 * filter's own 'struct nbdkit_filter'.
723 filter = filter_init ();
724 if (!filter) {
725 fprintf (stderr, "%s: %s: filter registration function failed\n",
726 program_name, filename);
727 exit (EXIT_FAILURE);
730 /* We do not provide API or ABI guarantees for filters, other than
731 * the ABI position and API contents of _api_version and _version to
732 * diagnose mismatch from the current nbdkit version.
734 if (filter->_api_version != NBDKIT_FILTER_API_VERSION) {
735 fprintf (stderr,
736 "%s: %s: filter is incompatible with this version of nbdkit "
737 "(_api_version = %d, need %d)\n",
738 program_name, filename, filter->_api_version,
739 NBDKIT_FILTER_API_VERSION);
740 exit (EXIT_FAILURE);
742 if (filter->_version == NULL ||
743 strcmp (filter->_version, PACKAGE_VERSION) != 0) {
744 fprintf (stderr,
745 "%s: %s: filter is incompatible with this version of nbdkit "
746 "(_version = %s, need %s)\n",
747 program_name, filename, filter->_version ?: "<null>",
748 PACKAGE_VERSION);
749 exit (EXIT_FAILURE);
752 f->filter = *filter;
754 backend_load (&f->backend, f->filter.name, f->filter.load);
756 return (struct backend *) f;