python: Make robust against traceback.format_exception returning error.
[nbdkit/ericb.git] / server / backend.c
blob47a9b17603b1fcb0d663fd6dc3ad6b9a92fa2cd7
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 <assert.h>
36 #include <ctype.h>
37 #include <dlfcn.h>
38 #include <inttypes.h>
39 #include <stdio.h>
40 #include <string.h>
42 #include "internal.h"
43 #include "minmax.h"
45 /* Helpers for registering a new backend. */
47 /* Set all debug flags which apply to this backend. */
48 static void
49 set_debug_flags (void *dl, const char *name)
51 struct debug_flag *flag;
53 for (flag = debug_flags; flag != NULL; flag = flag->next) {
54 if (!flag->used && strcmp (name, flag->name) == 0) {
55 CLEANUP_FREE char *var = NULL;
56 int *sym;
58 /* Synthesize the name of the variable. */
59 if (asprintf (&var, "%s_debug_%s", name, flag->flag) == -1) {
60 perror ("asprintf");
61 exit (EXIT_FAILURE);
64 /* Find the symbol. */
65 sym = dlsym (dl, var);
66 if (sym == NULL) {
67 fprintf (stderr,
68 "%s: -D %s.%s: %s does not contain a "
69 "global variable called %s\n",
70 program_name, name, flag->flag, name, var);
71 exit (EXIT_FAILURE);
74 /* Set the flag. */
75 *sym = flag->value;
77 /* Mark this flag as used. */
78 flag->used = true;
83 void
84 backend_init (struct backend *b, struct backend *next, size_t index,
85 const char *filename, void *dl, const char *type)
87 b->next = next;
88 b->i = index;
89 b->type = type;
90 b->filename = strdup (filename);
91 if (b->filename == NULL) {
92 perror ("strdup");
93 exit (EXIT_FAILURE);
95 b->dl = dl;
97 debug ("registering %s %s", type, filename);
100 void
101 backend_load (struct backend *b, const char *name, void (*load) (void))
103 size_t i, len;
105 /* name is required. */
106 if (name == NULL) {
107 fprintf (stderr, "%s: %s: %s must have a .name field\n",
108 program_name, b->filename, b->type);
109 exit (EXIT_FAILURE);
112 len = strlen (name);
113 if (len == 0) {
114 fprintf (stderr, "%s: %s: %s.name field must not be empty\n",
115 program_name, b->filename, b->type);
116 exit (EXIT_FAILURE);
118 for (i = 0; i < len; ++i) {
119 unsigned char c = name[i];
121 if (!(isascii (c) && isalnum (c))) {
122 fprintf (stderr,
123 "%s: %s: %s.name ('%s') field "
124 "must contain only ASCII alphanumeric characters\n",
125 program_name, b->filename, b->type, name);
126 exit (EXIT_FAILURE);
130 /* Copy the module's name into local storage, so that name
131 * survives past unload.
133 b->name = strdup (name);
134 if (b->name == NULL) {
135 perror ("strdup");
136 exit (EXIT_FAILURE);
139 debug ("registered %s %s (name %s)", b->type, b->filename, b->name);
141 /* Set debug flags before calling load. */
142 set_debug_flags (b->dl, name);
144 /* Call the on-load callback if it exists. */
145 debug ("%s: load", name);
146 if (load)
147 load ();
150 void
151 backend_unload (struct backend *b, void (*unload) (void))
153 /* Acquiring this lock prevents any other backend callbacks from running
154 * simultaneously.
156 lock_unload ();
158 debug ("%s: unload %s", b->name, b->type);
159 if (unload)
160 unload ();
162 if (DO_DLCLOSE)
163 dlclose (b->dl);
164 free (b->filename);
166 unlock_unload ();
168 free (b->name);
172 backend_open (struct backend *b, struct connection *conn, int readonly)
174 struct b_conn_handle *h = &conn->handles[b->i];
176 debug ("%s: open readonly=%d", b->name, readonly);
178 assert (h->handle == NULL);
179 assert ((h->state & HANDLE_OPEN) == 0);
180 assert (h->can_write == -1);
181 if (readonly)
182 h->can_write = 0;
184 /* Most filters will call next_open first, resulting in
185 * inner-to-outer ordering.
187 h->handle = b->open (b, conn, readonly);
188 debug ("%s: open returned handle %p", b->name, h->handle);
190 if (h->handle == NULL) {
191 if (b->i) /* Do not strand backend if this layer failed */
192 backend_close (b->next, conn);
193 return -1;
196 h->state |= HANDLE_OPEN;
197 if (b->i) /* A filter must not succeed unless its backend did also */
198 assert (conn->handles[b->i - 1].handle);
199 return 0;
203 backend_prepare (struct backend *b, struct connection *conn)
205 struct b_conn_handle *h = &conn->handles[b->i];
207 assert (h->handle);
208 assert ((h->state & (HANDLE_OPEN | HANDLE_CONNECTED)) == HANDLE_OPEN);
210 /* Call these in order starting from the filter closest to the
211 * plugin, similar to typical .open order.
213 if (b->i && backend_prepare (b->next, conn) == -1)
214 return -1;
216 debug ("%s: prepare readonly=%d", b->name, h->can_write == 0);
218 if (b->prepare (b, conn, h->handle, h->can_write == 0) == -1)
219 return -1;
220 h->state |= HANDLE_CONNECTED;
221 return 0;
225 backend_finalize (struct backend *b, struct connection *conn)
227 struct b_conn_handle *h = &conn->handles[b->i];
229 /* Call these in reverse order to .prepare above, starting from the
230 * filter furthest away from the plugin, and matching .close order.
233 debug ("%s: finalize", b->name);
235 /* Once finalize fails, we can do nothing further on this connection */
236 if (h->state & HANDLE_FAILED)
237 return -1;
239 if (h->handle) {
240 assert (h->state & HANDLE_CONNECTED);
241 if (b->finalize (b, conn, h->handle) == -1) {
242 h->state |= HANDLE_FAILED;
243 return -1;
246 else
247 assert (! (h->state & HANDLE_CONNECTED));
249 if (b->i)
250 return backend_finalize (b->next, conn);
251 return 0;
254 void
255 backend_close (struct backend *b, struct connection *conn)
257 struct b_conn_handle *h = &conn->handles[b->i];
259 /* outer-to-inner order, opposite .open */
260 debug ("%s: close", b->name);
262 if (h->handle) {
263 assert (h->state & HANDLE_OPEN);
264 b->close (b, conn, h->handle);
266 else
267 assert (! (h->state & HANDLE_OPEN));
268 reset_b_conn_handle (h);
269 if (b->i)
270 backend_close (b->next, conn);
273 bool
274 backend_valid_range (struct backend *b, struct connection *conn,
275 uint64_t offset, uint32_t count)
277 struct b_conn_handle *h = &conn->handles[b->i];
279 assert (h->exportsize <= INT64_MAX); /* Guaranteed by negotiation phase */
280 return count > 0 && offset <= h->exportsize &&
281 offset + count <= h->exportsize;
284 /* Wrappers for all callbacks in a filter's struct nbdkit_next_ops. */
287 backend_reopen (struct backend *b, struct connection *conn, int readonly)
289 debug ("%s: reopen readonly=%d", b->name, readonly);
291 if (backend_finalize (b, conn) == -1)
292 return -1;
293 backend_close (b, conn);
294 if (backend_open (b, conn, readonly) == -1) {
295 backend_close (b, conn);
296 return -1;
298 if (backend_prepare (b, conn) == -1) {
299 backend_finalize (b, conn);
300 backend_close (b, conn);
301 return -1;
303 return 0;
306 int64_t
307 backend_get_size (struct backend *b, struct connection *conn)
309 struct b_conn_handle *h = &conn->handles[b->i];
311 debug ("%s: get_size", b->name);
313 assert (h->handle && (h->state & HANDLE_CONNECTED));
314 if (h->exportsize == -1)
315 h->exportsize = b->get_size (b, conn, h->handle);
316 return h->exportsize;
320 backend_can_write (struct backend *b, struct connection *conn)
322 struct b_conn_handle *h = &conn->handles[b->i];
324 debug ("%s: can_write", b->name);
326 assert (h->handle && (h->state & HANDLE_CONNECTED));
327 if (h->can_write == -1)
328 h->can_write = b->can_write (b, conn, h->handle);
329 return h->can_write;
333 backend_can_flush (struct backend *b, struct connection *conn)
335 struct b_conn_handle *h = &conn->handles[b->i];
337 debug ("%s: can_flush", b->name);
339 assert (h->handle && (h->state & HANDLE_CONNECTED));
340 if (h->can_flush == -1)
341 h->can_flush = b->can_flush (b, conn, h->handle);
342 return h->can_flush;
346 backend_is_rotational (struct backend *b, struct connection *conn)
348 struct b_conn_handle *h = &conn->handles[b->i];
350 debug ("%s: is_rotational", b->name);
352 assert (h->handle && (h->state & HANDLE_CONNECTED));
353 if (h->is_rotational == -1)
354 h->is_rotational = b->is_rotational (b, conn, h->handle);
355 return h->is_rotational;
359 backend_can_trim (struct backend *b, struct connection *conn)
361 struct b_conn_handle *h = &conn->handles[b->i];
362 int r;
364 debug ("%s: can_trim", b->name);
366 assert (h->handle && (h->state & HANDLE_CONNECTED));
367 if (h->can_trim == -1) {
368 r = backend_can_write (b, conn);
369 if (r != 1) {
370 h->can_trim = 0;
371 return r;
373 h->can_trim = b->can_trim (b, conn, h->handle);
375 return h->can_trim;
379 backend_can_zero (struct backend *b, struct connection *conn)
381 struct b_conn_handle *h = &conn->handles[b->i];
382 int r;
384 debug ("%s: can_zero", b->name);
386 assert (h->handle && (h->state & HANDLE_CONNECTED));
387 if (h->can_zero == -1) {
388 r = backend_can_write (b, conn);
389 if (r != 1) {
390 h->can_zero = NBDKIT_ZERO_NONE;
391 return r; /* Relies on 0 == NBDKIT_ZERO_NONE */
393 h->can_zero = b->can_zero (b, conn, h->handle);
395 return h->can_zero;
399 backend_can_fast_zero (struct backend *b, struct connection *conn)
401 struct b_conn_handle *h = &conn->handles[b->i];
402 int r;
404 debug ("%s: can_fast_zero", b->name);
406 assert (h->handle && (h->state & HANDLE_CONNECTED));
407 if (h->can_fast_zero == -1) {
408 r = backend_can_zero (b, conn);
409 if (r < NBDKIT_ZERO_EMULATE) {
410 h->can_fast_zero = 0;
411 return r; /* Relies on 0 == NBDKIT_ZERO_NONE */
413 h->can_fast_zero = b->can_fast_zero (b, conn, h->handle);
415 return h->can_fast_zero;
419 backend_can_extents (struct backend *b, struct connection *conn)
421 struct b_conn_handle *h = &conn->handles[b->i];
423 debug ("%s: can_extents", b->name);
425 assert (h->handle && (h->state & HANDLE_CONNECTED));
426 if (h->can_extents == -1)
427 h->can_extents = b->can_extents (b, conn, h->handle);
428 return h->can_extents;
432 backend_can_fua (struct backend *b, struct connection *conn)
434 struct b_conn_handle *h = &conn->handles[b->i];
435 int r;
437 debug ("%s: can_fua", b->name);
439 assert (h->handle && (h->state & HANDLE_CONNECTED));
440 if (h->can_fua == -1) {
441 r = backend_can_write (b, conn);
442 if (r != 1) {
443 h->can_fua = NBDKIT_FUA_NONE;
444 return r; /* Relies on 0 == NBDKIT_FUA_NONE */
446 h->can_fua = b->can_fua (b, conn, h->handle);
448 return h->can_fua;
452 backend_can_multi_conn (struct backend *b, struct connection *conn)
454 struct b_conn_handle *h = &conn->handles[b->i];
456 assert (h->handle && (h->state & HANDLE_CONNECTED));
457 debug ("%s: can_multi_conn", b->name);
459 if (h->can_multi_conn == -1)
460 h->can_multi_conn = b->can_multi_conn (b, conn, h->handle);
461 return h->can_multi_conn;
465 backend_can_cache (struct backend *b, struct connection *conn)
467 struct b_conn_handle *h = &conn->handles[b->i];
469 debug ("%s: can_cache", b->name);
471 assert (h->handle && (h->state & HANDLE_CONNECTED));
472 if (h->can_cache == -1)
473 h->can_cache = b->can_cache (b, conn, h->handle);
474 return h->can_cache;
478 backend_pread (struct backend *b, struct connection *conn,
479 void *buf, uint32_t count, uint64_t offset,
480 uint32_t flags, int *err)
482 struct b_conn_handle *h = &conn->handles[b->i];
483 int r;
485 assert (h->handle && (h->state & HANDLE_CONNECTED));
486 assert (backend_valid_range (b, conn, offset, count));
487 assert (flags == 0);
488 debug ("%s: pread count=%" PRIu32 " offset=%" PRIu64,
489 b->name, count, offset);
491 r = b->pread (b, conn, h->handle, buf, count, offset, flags, err);
492 if (r == -1)
493 assert (*err);
494 return r;
498 backend_pwrite (struct backend *b, struct connection *conn,
499 const void *buf, uint32_t count, uint64_t offset,
500 uint32_t flags, int *err)
502 struct b_conn_handle *h = &conn->handles[b->i];
503 bool fua = !!(flags & NBDKIT_FLAG_FUA);
504 int r;
506 assert (h->handle && (h->state & HANDLE_CONNECTED));
507 assert (h->can_write == 1);
508 assert (backend_valid_range (b, conn, offset, count));
509 assert (!(flags & ~NBDKIT_FLAG_FUA));
510 if (fua)
511 assert (h->can_fua > NBDKIT_FUA_NONE);
512 debug ("%s: pwrite count=%" PRIu32 " offset=%" PRIu64 " fua=%d",
513 b->name, count, offset, fua);
515 r = b->pwrite (b, conn, h->handle, buf, count, offset, flags, err);
516 if (r == -1)
517 assert (*err);
518 return r;
522 backend_flush (struct backend *b, struct connection *conn,
523 uint32_t flags, int *err)
525 struct b_conn_handle *h = &conn->handles[b->i];
526 int r;
528 assert (h->handle && (h->state & HANDLE_CONNECTED));
529 assert (h->can_flush == 1);
530 assert (flags == 0);
531 debug ("%s: flush", b->name);
533 r = b->flush (b, conn, h->handle, flags, err);
534 if (r == -1)
535 assert (*err);
536 return r;
540 backend_trim (struct backend *b, struct connection *conn,
541 uint32_t count, uint64_t offset, uint32_t flags,
542 int *err)
544 struct b_conn_handle *h = &conn->handles[b->i];
545 bool fua = !!(flags & NBDKIT_FLAG_FUA);
546 int r;
548 assert (h->handle && (h->state & HANDLE_CONNECTED));
549 assert (h->can_write == 1);
550 assert (h->can_trim == 1);
551 assert (backend_valid_range (b, conn, offset, count));
552 assert (!(flags & ~NBDKIT_FLAG_FUA));
553 if (fua)
554 assert (h->can_fua > NBDKIT_FUA_NONE);
555 debug ("%s: trim count=%" PRIu32 " offset=%" PRIu64 " fua=%d",
556 b->name, count, offset, fua);
558 r = b->trim (b, conn, h->handle, count, offset, flags, err);
559 if (r == -1)
560 assert (*err);
561 return r;
565 backend_zero (struct backend *b, struct connection *conn,
566 uint32_t count, uint64_t offset, uint32_t flags,
567 int *err)
569 struct b_conn_handle *h = &conn->handles[b->i];
570 bool fua = !!(flags & NBDKIT_FLAG_FUA);
571 bool fast = !!(flags & NBDKIT_FLAG_FAST_ZERO);
572 int r;
574 assert (h->handle && (h->state & HANDLE_CONNECTED));
575 assert (h->can_write == 1);
576 assert (h->can_zero > NBDKIT_ZERO_NONE);
577 assert (backend_valid_range (b, conn, offset, count));
578 assert (!(flags & ~(NBDKIT_FLAG_MAY_TRIM | NBDKIT_FLAG_FUA |
579 NBDKIT_FLAG_FAST_ZERO)));
580 if (fua)
581 assert (h->can_fua > NBDKIT_FUA_NONE);
582 if (fast)
583 assert (h->can_fast_zero == 1);
584 debug ("%s: zero count=%" PRIu32 " offset=%" PRIu64
585 " may_trim=%d fua=%d fast=%d",
586 b->name, count, offset, !!(flags & NBDKIT_FLAG_MAY_TRIM), fua, fast);
588 r = b->zero (b, conn, h->handle, count, offset, flags, err);
589 if (r == -1) {
590 assert (*err);
591 if (!fast)
592 assert (*err != ENOTSUP && *err != EOPNOTSUPP);
594 return r;
598 backend_extents (struct backend *b, struct connection *conn,
599 uint32_t count, uint64_t offset, uint32_t flags,
600 struct nbdkit_extents *extents, int *err)
602 struct b_conn_handle *h = &conn->handles[b->i];
603 int r;
605 assert (h->handle && (h->state & HANDLE_CONNECTED));
606 assert (h->can_extents >= 0);
607 assert (backend_valid_range (b, conn, offset, count));
608 assert (!(flags & ~NBDKIT_FLAG_REQ_ONE));
609 debug ("%s: extents count=%" PRIu32 " offset=%" PRIu64 " req_one=%d",
610 b->name, count, offset, !!(flags & NBDKIT_FLAG_REQ_ONE));
612 if (h->can_extents == 0) {
613 /* By default it is safe assume that everything in the range is
614 * allocated.
616 r = nbdkit_add_extent (extents, offset, count, 0 /* allocated data */);
617 if (r == -1)
618 *err = errno;
619 return r;
621 r = b->extents (b, conn, h->handle, count, offset, flags, extents, err);
622 if (r == -1)
623 assert (*err);
624 return r;
628 backend_cache (struct backend *b, struct connection *conn,
629 uint32_t count, uint64_t offset,
630 uint32_t flags, int *err)
632 struct b_conn_handle *h = &conn->handles[b->i];
633 int r;
635 assert (h->handle && (h->state & HANDLE_CONNECTED));
636 assert (h->can_cache > NBDKIT_CACHE_NONE);
637 assert (backend_valid_range (b, conn, offset, count));
638 assert (flags == 0);
639 debug ("%s: cache count=%" PRIu32 " offset=%" PRIu64,
640 b->name, count, offset);
642 if (h->can_cache == NBDKIT_CACHE_EMULATE) {
643 static char buf[MAX_REQUEST_SIZE]; /* data sink, never read */
644 uint32_t limit;
646 while (count) {
647 limit = MIN (count, sizeof buf);
648 if (backend_pread (b, conn, buf, limit, offset, flags, err) == -1)
649 return -1;
650 count -= limit;
652 return 0;
654 r = b->cache (b, conn, h->handle, count, offset, flags, err);
655 if (r == -1)
656 assert (*err);
657 return r;