plugins: Wire up nbd plugin support for NBD_INFO_INIT_STATE
[nbdkit/ericb.git] / server / backend.c
blob3ebe1f411a6e7fafb6bfb1b37d9a58f80bfdf19b
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 <inttypes.h>
38 #include <string.h>
39 #include <assert.h>
40 #include <ctype.h>
42 #include <dlfcn.h>
44 #include "internal.h"
45 #include "minmax.h"
47 /* Helpers for registering a new backend. */
49 /* Use:
50 * -D nbdkit.backend.controlpath=0 to suppress control path debugging.
51 * -D nbdkit.backend.datapath=0 to suppress data path debugging.
53 int nbdkit_debug_backend_controlpath = 1;
54 int nbdkit_debug_backend_datapath = 1;
56 #define controlpath_debug(fs, ...) \
57 do { \
58 if (nbdkit_debug_backend_controlpath) debug ((fs), ##__VA_ARGS__); \
59 } while (0)
60 #define datapath_debug(fs, ...) \
61 do { \
62 if (nbdkit_debug_backend_datapath) debug ((fs), ##__VA_ARGS__); \
63 } while (0)
65 void
66 backend_init (struct backend *b, struct backend *next, size_t index,
67 const char *filename, void *dl, const char *type)
69 b->next = next;
70 b->i = index;
71 b->type = type;
72 b->filename = strdup (filename);
73 if (b->filename == NULL) {
74 perror ("strdup");
75 exit (EXIT_FAILURE);
77 b->dl = dl;
79 debug ("registering %s %s", type, filename);
82 void
83 backend_load (struct backend *b, const char *name, void (*load) (void))
85 size_t i, len;
87 /* name is required. */
88 if (name == NULL) {
89 fprintf (stderr, "%s: %s: %s must have a .name field\n",
90 program_name, b->filename, b->type);
91 exit (EXIT_FAILURE);
94 len = strlen (name);
95 if (len == 0) {
96 fprintf (stderr, "%s: %s: %s.name field must not be empty\n",
97 program_name, b->filename, b->type);
98 exit (EXIT_FAILURE);
100 for (i = 0; i < len; ++i) {
101 unsigned char c = name[i];
103 if (!(isascii (c) && isalnum (c))) {
104 fprintf (stderr,
105 "%s: %s: %s.name ('%s') field "
106 "must contain only ASCII alphanumeric characters\n",
107 program_name, b->filename, b->type, name);
108 exit (EXIT_FAILURE);
112 /* Copy the module's name into local storage, so that name
113 * survives past unload.
115 b->name = strdup (name);
116 if (b->name == NULL) {
117 perror ("strdup");
118 exit (EXIT_FAILURE);
121 debug ("registered %s %s (name %s)", b->type, b->filename, b->name);
123 /* Apply debug flags before calling load. */
124 apply_debug_flags (b->dl, name);
126 /* Call the on-load callback if it exists. */
127 controlpath_debug ("%s: load", name);
128 if (load)
129 load ();
132 void
133 backend_unload (struct backend *b, void (*unload) (void))
135 /* Acquiring this lock prevents any other backend callbacks from running
136 * simultaneously.
138 lock_unload ();
140 controlpath_debug ("%s: unload %s", b->name, b->type);
141 if (unload)
142 unload ();
144 if (DO_DLCLOSE)
145 dlclose (b->dl);
146 free (b->filename);
148 unlock_unload ();
150 free (b->name);
154 backend_open (struct backend *b, struct connection *conn, int readonly)
156 struct b_conn_handle *h = &conn->handles[b->i];
158 controlpath_debug ("%s: open readonly=%d", b->name, readonly);
160 assert (h->handle == NULL);
161 assert ((h->state & HANDLE_OPEN) == 0);
162 assert (h->can_write == -1);
163 if (readonly)
164 h->can_write = 0;
166 /* Most filters will call next_open first, resulting in
167 * inner-to-outer ordering.
169 h->handle = b->open (b, conn, readonly);
170 controlpath_debug ("%s: open returned handle %p", b->name, h->handle);
172 if (h->handle == NULL) {
173 if (b->i) /* Do not strand backend if this layer failed */
174 backend_close (b->next, conn);
175 return -1;
178 h->state |= HANDLE_OPEN;
179 if (b->i) /* A filter must not succeed unless its backend did also */
180 assert (conn->handles[b->i - 1].handle);
181 return 0;
185 backend_prepare (struct backend *b, struct connection *conn)
187 struct b_conn_handle *h = &conn->handles[b->i];
189 assert (h->handle);
190 assert ((h->state & (HANDLE_OPEN | HANDLE_CONNECTED)) == HANDLE_OPEN);
192 /* Call these in order starting from the filter closest to the
193 * plugin, similar to typical .open order.
195 if (b->i && backend_prepare (b->next, conn) == -1)
196 return -1;
198 controlpath_debug ("%s: prepare readonly=%d", b->name, h->can_write == 0);
200 if (b->prepare (b, conn, h->handle, h->can_write == 0) == -1)
201 return -1;
202 h->state |= HANDLE_CONNECTED;
203 return 0;
207 backend_finalize (struct backend *b, struct connection *conn)
209 struct b_conn_handle *h = &conn->handles[b->i];
211 /* Call these in reverse order to .prepare above, starting from the
212 * filter furthest away from the plugin, and matching .close order.
215 controlpath_debug ("%s: finalize", b->name);
217 /* Once finalize fails, we can do nothing further on this connection */
218 if (h->state & HANDLE_FAILED)
219 return -1;
221 if (h->handle) {
222 assert (h->state & HANDLE_CONNECTED);
223 if (b->finalize (b, conn, h->handle) == -1) {
224 h->state |= HANDLE_FAILED;
225 return -1;
228 else
229 assert (! (h->state & HANDLE_CONNECTED));
231 if (b->i)
232 return backend_finalize (b->next, conn);
233 return 0;
236 void
237 backend_close (struct backend *b, struct connection *conn)
239 struct b_conn_handle *h = &conn->handles[b->i];
241 /* outer-to-inner order, opposite .open */
242 controlpath_debug ("%s: close", b->name);
244 if (h->handle) {
245 assert (h->state & HANDLE_OPEN);
246 b->close (b, conn, h->handle);
248 else
249 assert (! (h->state & HANDLE_OPEN));
250 reset_b_conn_handle (h);
251 if (b->i)
252 backend_close (b->next, conn);
255 bool
256 backend_valid_range (struct backend *b, struct connection *conn,
257 uint64_t offset, uint32_t count)
259 struct b_conn_handle *h = &conn->handles[b->i];
261 assert (h->exportsize <= INT64_MAX); /* Guaranteed by negotiation phase */
262 return count > 0 && offset <= h->exportsize &&
263 offset + count <= h->exportsize;
266 /* Wrappers for all callbacks in a filter's struct nbdkit_next_ops. */
269 backend_reopen (struct backend *b, struct connection *conn, int readonly)
271 controlpath_debug ("%s: reopen readonly=%d", b->name, readonly);
273 if (backend_finalize (b, conn) == -1)
274 return -1;
275 backend_close (b, conn);
276 if (backend_open (b, conn, readonly) == -1) {
277 backend_close (b, conn);
278 return -1;
280 if (backend_prepare (b, conn) == -1) {
281 backend_finalize (b, conn);
282 backend_close (b, conn);
283 return -1;
285 return 0;
288 int64_t
289 backend_get_size (struct backend *b, struct connection *conn)
291 struct b_conn_handle *h = &conn->handles[b->i];
293 controlpath_debug ("%s: get_size", b->name);
295 assert (h->handle && (h->state & HANDLE_CONNECTED));
296 if (h->exportsize == -1)
297 h->exportsize = b->get_size (b, conn, h->handle);
298 return h->exportsize;
302 backend_can_write (struct backend *b, struct connection *conn)
304 struct b_conn_handle *h = &conn->handles[b->i];
306 controlpath_debug ("%s: can_write", b->name);
308 assert (h->handle && (h->state & HANDLE_CONNECTED));
309 if (h->can_write == -1)
310 h->can_write = b->can_write (b, conn, h->handle);
311 return h->can_write;
315 backend_can_flush (struct backend *b, struct connection *conn)
317 struct b_conn_handle *h = &conn->handles[b->i];
319 controlpath_debug ("%s: can_flush", b->name);
321 assert (h->handle && (h->state & HANDLE_CONNECTED));
322 if (h->can_flush == -1)
323 h->can_flush = b->can_flush (b, conn, h->handle);
324 return h->can_flush;
328 backend_is_rotational (struct backend *b, struct connection *conn)
330 struct b_conn_handle *h = &conn->handles[b->i];
332 controlpath_debug ("%s: is_rotational", b->name);
334 assert (h->handle && (h->state & HANDLE_CONNECTED));
335 if (h->is_rotational == -1)
336 h->is_rotational = b->is_rotational (b, conn, h->handle);
337 return h->is_rotational;
341 backend_can_trim (struct backend *b, struct connection *conn)
343 struct b_conn_handle *h = &conn->handles[b->i];
344 int r;
346 controlpath_debug ("%s: can_trim", b->name);
348 assert (h->handle && (h->state & HANDLE_CONNECTED));
349 if (h->can_trim == -1) {
350 r = backend_can_write (b, conn);
351 if (r != 1) {
352 h->can_trim = 0;
353 return r;
355 h->can_trim = b->can_trim (b, conn, h->handle);
357 return h->can_trim;
361 backend_can_zero (struct backend *b, struct connection *conn)
363 struct b_conn_handle *h = &conn->handles[b->i];
364 int r;
366 controlpath_debug ("%s: can_zero", b->name);
368 assert (h->handle && (h->state & HANDLE_CONNECTED));
369 if (h->can_zero == -1) {
370 r = backend_can_write (b, conn);
371 if (r != 1) {
372 h->can_zero = NBDKIT_ZERO_NONE;
373 return r; /* Relies on 0 == NBDKIT_ZERO_NONE */
375 h->can_zero = b->can_zero (b, conn, h->handle);
377 return h->can_zero;
381 backend_can_fast_zero (struct backend *b, struct connection *conn)
383 struct b_conn_handle *h = &conn->handles[b->i];
384 int r;
386 controlpath_debug ("%s: can_fast_zero", b->name);
388 assert (h->handle && (h->state & HANDLE_CONNECTED));
389 if (h->can_fast_zero == -1) {
390 r = backend_can_zero (b, conn);
391 if (r < NBDKIT_ZERO_EMULATE) {
392 h->can_fast_zero = 0;
393 return r; /* Relies on 0 == NBDKIT_ZERO_NONE */
395 h->can_fast_zero = b->can_fast_zero (b, conn, h->handle);
397 return h->can_fast_zero;
401 backend_can_extents (struct backend *b, struct connection *conn)
403 struct b_conn_handle *h = &conn->handles[b->i];
405 controlpath_debug ("%s: can_extents", b->name);
407 assert (h->handle && (h->state & HANDLE_CONNECTED));
408 if (h->can_extents == -1)
409 h->can_extents = b->can_extents (b, conn, h->handle);
410 return h->can_extents;
414 backend_can_fua (struct backend *b, struct connection *conn)
416 struct b_conn_handle *h = &conn->handles[b->i];
417 int r;
419 controlpath_debug ("%s: can_fua", b->name);
421 assert (h->handle && (h->state & HANDLE_CONNECTED));
422 if (h->can_fua == -1) {
423 r = backend_can_write (b, conn);
424 if (r != 1) {
425 h->can_fua = NBDKIT_FUA_NONE;
426 return r; /* Relies on 0 == NBDKIT_FUA_NONE */
428 h->can_fua = b->can_fua (b, conn, h->handle);
430 return h->can_fua;
434 backend_can_multi_conn (struct backend *b, struct connection *conn)
436 struct b_conn_handle *h = &conn->handles[b->i];
438 assert (h->handle && (h->state & HANDLE_CONNECTED));
439 controlpath_debug ("%s: can_multi_conn", b->name);
441 if (h->can_multi_conn == -1)
442 h->can_multi_conn = b->can_multi_conn (b, conn, h->handle);
443 return h->can_multi_conn;
447 backend_can_cache (struct backend *b, struct connection *conn)
449 struct b_conn_handle *h = &conn->handles[b->i];
451 controlpath_debug ("%s: can_cache", b->name);
453 assert (h->handle && (h->state & HANDLE_CONNECTED));
454 if (h->can_cache == -1)
455 h->can_cache = b->can_cache (b, conn, h->handle);
456 return h->can_cache;
460 backend_init_sparse (struct backend *b, struct connection *conn)
462 struct b_conn_handle *h = &conn->handles[b->i];
464 controlpath_debug ("%s: init_sparse", b->name);
466 assert (h->handle && (h->state & HANDLE_CONNECTED));
467 if (h->init_sparse == -1)
468 h->init_sparse = b->init_sparse (b, conn, h->handle);
469 return h->init_sparse;
473 backend_init_zero (struct backend *b, struct connection *conn)
475 struct b_conn_handle *h = &conn->handles[b->i];
477 controlpath_debug ("%s: init_zero", b->name);
479 assert (h->handle && (h->state & HANDLE_CONNECTED));
480 if (h->init_zero == -1)
481 h->init_zero = b->init_zero (b, conn, h->handle);
482 return h->init_zero;
486 backend_pread (struct backend *b, struct connection *conn,
487 void *buf, uint32_t count, uint64_t offset,
488 uint32_t flags, int *err)
490 struct b_conn_handle *h = &conn->handles[b->i];
491 int r;
493 assert (h->handle && (h->state & HANDLE_CONNECTED));
494 assert (backend_valid_range (b, conn, offset, count));
495 assert (flags == 0);
496 datapath_debug ("%s: pread count=%" PRIu32 " offset=%" PRIu64,
497 b->name, count, offset);
499 r = b->pread (b, conn, h->handle, buf, count, offset, flags, err);
500 if (r == -1)
501 assert (*err);
502 return r;
506 backend_pwrite (struct backend *b, struct connection *conn,
507 const void *buf, uint32_t count, uint64_t offset,
508 uint32_t flags, int *err)
510 struct b_conn_handle *h = &conn->handles[b->i];
511 bool fua = !!(flags & NBDKIT_FLAG_FUA);
512 int r;
514 assert (h->handle && (h->state & HANDLE_CONNECTED));
515 assert (h->can_write == 1);
516 assert (backend_valid_range (b, conn, offset, count));
517 assert (!(flags & ~NBDKIT_FLAG_FUA));
518 if (fua)
519 assert (h->can_fua > NBDKIT_FUA_NONE);
520 datapath_debug ("%s: pwrite count=%" PRIu32 " offset=%" PRIu64 " fua=%d",
521 b->name, count, offset, fua);
523 r = b->pwrite (b, conn, h->handle, buf, count, offset, flags, err);
524 if (r == -1)
525 assert (*err);
526 return r;
530 backend_flush (struct backend *b, struct connection *conn,
531 uint32_t flags, int *err)
533 struct b_conn_handle *h = &conn->handles[b->i];
534 int r;
536 assert (h->handle && (h->state & HANDLE_CONNECTED));
537 assert (h->can_flush == 1);
538 assert (flags == 0);
539 datapath_debug ("%s: flush", b->name);
541 r = b->flush (b, conn, h->handle, flags, err);
542 if (r == -1)
543 assert (*err);
544 return r;
548 backend_trim (struct backend *b, struct connection *conn,
549 uint32_t count, uint64_t offset, uint32_t flags,
550 int *err)
552 struct b_conn_handle *h = &conn->handles[b->i];
553 bool fua = !!(flags & NBDKIT_FLAG_FUA);
554 int r;
556 assert (h->handle && (h->state & HANDLE_CONNECTED));
557 assert (h->can_write == 1);
558 assert (h->can_trim == 1);
559 assert (backend_valid_range (b, conn, offset, count));
560 assert (!(flags & ~NBDKIT_FLAG_FUA));
561 if (fua)
562 assert (h->can_fua > NBDKIT_FUA_NONE);
563 datapath_debug ("%s: trim count=%" PRIu32 " offset=%" PRIu64 " fua=%d",
564 b->name, count, offset, fua);
566 r = b->trim (b, conn, h->handle, count, offset, flags, err);
567 if (r == -1)
568 assert (*err);
569 return r;
573 backend_zero (struct backend *b, struct connection *conn,
574 uint32_t count, uint64_t offset, uint32_t flags,
575 int *err)
577 struct b_conn_handle *h = &conn->handles[b->i];
578 bool fua = !!(flags & NBDKIT_FLAG_FUA);
579 bool fast = !!(flags & NBDKIT_FLAG_FAST_ZERO);
580 int r;
582 assert (h->handle && (h->state & HANDLE_CONNECTED));
583 assert (h->can_write == 1);
584 assert (h->can_zero > NBDKIT_ZERO_NONE);
585 assert (backend_valid_range (b, conn, offset, count));
586 assert (!(flags & ~(NBDKIT_FLAG_MAY_TRIM | NBDKIT_FLAG_FUA |
587 NBDKIT_FLAG_FAST_ZERO)));
588 if (fua)
589 assert (h->can_fua > NBDKIT_FUA_NONE);
590 if (fast)
591 assert (h->can_fast_zero == 1);
592 datapath_debug ("%s: zero count=%" PRIu32 " offset=%" PRIu64
593 " may_trim=%d fua=%d fast=%d",
594 b->name, count, offset,
595 !!(flags & NBDKIT_FLAG_MAY_TRIM), fua, fast);
597 r = b->zero (b, conn, h->handle, count, offset, flags, err);
598 if (r == -1) {
599 assert (*err);
600 if (!fast)
601 assert (*err != ENOTSUP && *err != EOPNOTSUPP);
603 return r;
607 backend_extents (struct backend *b, struct connection *conn,
608 uint32_t count, uint64_t offset, uint32_t flags,
609 struct nbdkit_extents *extents, int *err)
611 struct b_conn_handle *h = &conn->handles[b->i];
612 int r;
614 assert (h->handle && (h->state & HANDLE_CONNECTED));
615 assert (h->can_extents >= 0);
616 assert (backend_valid_range (b, conn, offset, count));
617 assert (!(flags & ~NBDKIT_FLAG_REQ_ONE));
618 datapath_debug ("%s: extents count=%" PRIu32 " offset=%" PRIu64 " req_one=%d",
619 b->name, count, offset, !!(flags & NBDKIT_FLAG_REQ_ONE));
621 if (h->can_extents == 0) {
622 /* By default it is safe assume that everything in the range is
623 * allocated.
625 r = nbdkit_add_extent (extents, offset, count, 0 /* allocated data */);
626 if (r == -1)
627 *err = errno;
628 return r;
630 r = b->extents (b, conn, h->handle, count, offset, flags, extents, err);
631 if (r == -1)
632 assert (*err);
633 return r;
637 backend_cache (struct backend *b, struct connection *conn,
638 uint32_t count, uint64_t offset,
639 uint32_t flags, int *err)
641 struct b_conn_handle *h = &conn->handles[b->i];
642 int r;
644 assert (h->handle && (h->state & HANDLE_CONNECTED));
645 assert (h->can_cache > NBDKIT_CACHE_NONE);
646 assert (backend_valid_range (b, conn, offset, count));
647 assert (flags == 0);
648 datapath_debug ("%s: cache count=%" PRIu32 " offset=%" PRIu64,
649 b->name, count, offset);
651 if (h->can_cache == NBDKIT_CACHE_EMULATE) {
652 static char buf[MAX_REQUEST_SIZE]; /* data sink, never read */
653 uint32_t limit;
655 while (count) {
656 limit = MIN (count, sizeof buf);
657 if (backend_pread (b, conn, buf, limit, offset, flags, err) == -1)
658 return -1;
659 count -= limit;
661 return 0;
663 r = b->cache (b, conn, h->handle, count, offset, flags, err);
664 if (r == -1)
665 assert (*err);
666 return r;