server: Move prepare/finalize/close recursion to backend.c
[nbdkit/ericb.git] / server / backend.c
blobd13cc1077ced8447630f4309180ad189b0f3dc42
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->can_write == -1);
180 if (readonly)
181 h->can_write = 0;
183 /* Most filters will call next_open first, resulting in
184 * inner-to-outer ordering.
186 h->handle = b->open (b, conn, readonly);
187 debug ("%s: open returned handle %p", b->name, h->handle);
189 if (h->handle == NULL) {
190 if (b->i) /* Do not strand backend if this layer failed */
191 backend_close (b->next, conn);
192 return -1;
195 if (b->i) /* A filter must not succeed unless its backend did also */
196 assert (conn->handles[b->i - 1].handle);
197 return 0;
201 backend_prepare (struct backend *b, struct connection *conn)
203 struct b_conn_handle *h = &conn->handles[b->i];
205 assert (h->handle);
207 /* Call these in order starting from the filter closest to the
208 * plugin, similar to typical .open order.
210 if (b->i && backend_prepare (b->next, conn) == -1)
211 return -1;
213 debug ("%s: prepare readonly=%d", b->name, h->can_write == 0);
215 return b->prepare (b, conn, h->handle, h->can_write == 0);
219 backend_finalize (struct backend *b, struct connection *conn)
221 struct b_conn_handle *h = &conn->handles[b->i];
223 /* Call these in reverse order to .prepare above, starting from the
224 * filter furthest away from the plugin, and matching .close order.
227 assert (h->handle);
228 debug ("%s: finalize", b->name);
230 if (b->finalize (b, conn, h->handle) == -1)
231 return -1;
233 if (b->i)
234 return backend_finalize (b->next, conn);
235 return 0;
238 void
239 backend_close (struct backend *b, struct connection *conn)
241 struct b_conn_handle *h = &conn->handles[b->i];
243 /* outer-to-inner order, opposite .open */
244 debug ("%s: close", b->name);
246 b->close (b, conn, h->handle);
247 reset_b_conn_handle (h);
248 if (b->i)
249 backend_close (b->next, conn);
252 bool
253 backend_valid_range (struct backend *b, struct connection *conn,
254 uint64_t offset, uint32_t count)
256 struct b_conn_handle *h = &conn->handles[b->i];
258 assert (h->exportsize <= INT64_MAX); /* Guaranteed by negotiation phase */
259 return count > 0 && offset <= h->exportsize &&
260 offset + count <= h->exportsize;
263 /* Wrappers for all callbacks in a filter's struct nbdkit_next_ops. */
266 backend_reopen (struct backend *b, struct connection *conn, int readonly)
268 struct b_conn_handle *h = &conn->handles[b->i];
270 debug ("%s: reopen readonly=%d", b->name, readonly);
272 if (h->handle != NULL)
273 backend_close (b, conn);
274 return backend_open (b, conn, readonly);
277 int64_t
278 backend_get_size (struct backend *b, struct connection *conn)
280 struct b_conn_handle *h = &conn->handles[b->i];
282 debug ("%s: get_size", b->name);
284 assert (h->handle);
285 if (h->exportsize == -1)
286 h->exportsize = b->get_size (b, conn, h->handle);
287 return h->exportsize;
291 backend_can_write (struct backend *b, struct connection *conn)
293 struct b_conn_handle *h = &conn->handles[b->i];
295 debug ("%s: can_write", b->name);
297 assert (h->handle);
298 if (h->can_write == -1)
299 h->can_write = b->can_write (b, conn, h->handle);
300 return h->can_write;
304 backend_can_flush (struct backend *b, struct connection *conn)
306 struct b_conn_handle *h = &conn->handles[b->i];
308 debug ("%s: can_flush", b->name);
310 assert (h->handle);
311 if (h->can_flush == -1)
312 h->can_flush = b->can_flush (b, conn, h->handle);
313 return h->can_flush;
317 backend_is_rotational (struct backend *b, struct connection *conn)
319 struct b_conn_handle *h = &conn->handles[b->i];
321 debug ("%s: is_rotational", b->name);
323 assert (h->handle);
324 if (h->is_rotational == -1)
325 h->is_rotational = b->is_rotational (b, conn, h->handle);
326 return h->is_rotational;
330 backend_can_trim (struct backend *b, struct connection *conn)
332 struct b_conn_handle *h = &conn->handles[b->i];
333 int r;
335 debug ("%s: can_trim", b->name);
337 assert (h->handle);
338 if (h->can_trim == -1) {
339 r = backend_can_write (b, conn);
340 if (r != 1) {
341 h->can_trim = 0;
342 return r;
344 h->can_trim = b->can_trim (b, conn, h->handle);
346 return h->can_trim;
350 backend_can_zero (struct backend *b, struct connection *conn)
352 struct b_conn_handle *h = &conn->handles[b->i];
353 int r;
355 debug ("%s: can_zero", b->name);
357 assert (h->handle);
358 if (h->can_zero == -1) {
359 r = backend_can_write (b, conn);
360 if (r != 1) {
361 h->can_zero = NBDKIT_ZERO_NONE;
362 return r; /* Relies on 0 == NBDKIT_ZERO_NONE */
364 h->can_zero = b->can_zero (b, conn, h->handle);
366 return h->can_zero;
370 backend_can_fast_zero (struct backend *b, struct connection *conn)
372 struct b_conn_handle *h = &conn->handles[b->i];
373 int r;
375 debug ("%s: can_fast_zero", b->name);
377 assert (h->handle);
378 if (h->can_fast_zero == -1) {
379 r = backend_can_zero (b, conn);
380 if (r < NBDKIT_ZERO_EMULATE) {
381 h->can_fast_zero = 0;
382 return r; /* Relies on 0 == NBDKIT_ZERO_NONE */
384 h->can_fast_zero = b->can_fast_zero (b, conn, h->handle);
386 return h->can_fast_zero;
390 backend_can_extents (struct backend *b, struct connection *conn)
392 struct b_conn_handle *h = &conn->handles[b->i];
394 debug ("%s: can_extents", b->name);
396 assert (h->handle);
397 if (h->can_extents == -1)
398 h->can_extents = b->can_extents (b, conn, h->handle);
399 return h->can_extents;
403 backend_can_fua (struct backend *b, struct connection *conn)
405 struct b_conn_handle *h = &conn->handles[b->i];
406 int r;
408 debug ("%s: can_fua", b->name);
410 assert (h->handle);
411 if (h->can_fua == -1) {
412 r = backend_can_write (b, conn);
413 if (r != 1) {
414 h->can_fua = NBDKIT_FUA_NONE;
415 return r; /* Relies on 0 == NBDKIT_FUA_NONE */
417 h->can_fua = b->can_fua (b, conn, h->handle);
419 return h->can_fua;
423 backend_can_multi_conn (struct backend *b, struct connection *conn)
425 struct b_conn_handle *h = &conn->handles[b->i];
427 assert (h->handle);
428 debug ("%s: can_multi_conn", b->name);
430 if (h->can_multi_conn == -1)
431 h->can_multi_conn = b->can_multi_conn (b, conn, h->handle);
432 return h->can_multi_conn;
436 backend_can_cache (struct backend *b, struct connection *conn)
438 struct b_conn_handle *h = &conn->handles[b->i];
440 debug ("%s: can_cache", b->name);
442 assert (h->handle);
443 if (h->can_cache == -1)
444 h->can_cache = b->can_cache (b, conn, h->handle);
445 return h->can_cache;
449 backend_pread (struct backend *b, struct connection *conn,
450 void *buf, uint32_t count, uint64_t offset,
451 uint32_t flags, int *err)
453 struct b_conn_handle *h = &conn->handles[b->i];
454 int r;
456 assert (h->handle);
457 assert (backend_valid_range (b, conn, offset, count));
458 assert (flags == 0);
459 debug ("%s: pread count=%" PRIu32 " offset=%" PRIu64,
460 b->name, count, offset);
462 r = b->pread (b, conn, h->handle, buf, count, offset, flags, err);
463 if (r == -1)
464 assert (*err);
465 return r;
469 backend_pwrite (struct backend *b, struct connection *conn,
470 const void *buf, uint32_t count, uint64_t offset,
471 uint32_t flags, int *err)
473 struct b_conn_handle *h = &conn->handles[b->i];
474 bool fua = !!(flags & NBDKIT_FLAG_FUA);
475 int r;
477 assert (h->handle);
478 assert (h->can_write == 1);
479 assert (backend_valid_range (b, conn, offset, count));
480 assert (!(flags & ~NBDKIT_FLAG_FUA));
481 if (fua)
482 assert (h->can_fua > NBDKIT_FUA_NONE);
483 debug ("%s: pwrite count=%" PRIu32 " offset=%" PRIu64 " fua=%d",
484 b->name, count, offset, fua);
486 r = b->pwrite (b, conn, h->handle, buf, count, offset, flags, err);
487 if (r == -1)
488 assert (*err);
489 return r;
493 backend_flush (struct backend *b, struct connection *conn,
494 uint32_t flags, int *err)
496 struct b_conn_handle *h = &conn->handles[b->i];
497 int r;
499 assert (h->handle);
500 assert (h->can_flush == 1);
501 assert (flags == 0);
502 debug ("%s: flush", b->name);
504 r = b->flush (b, conn, h->handle, flags, err);
505 if (r == -1)
506 assert (*err);
507 return r;
511 backend_trim (struct backend *b, struct connection *conn,
512 uint32_t count, uint64_t offset, uint32_t flags,
513 int *err)
515 struct b_conn_handle *h = &conn->handles[b->i];
516 bool fua = !!(flags & NBDKIT_FLAG_FUA);
517 int r;
519 assert (h->handle);
520 assert (h->can_write == 1);
521 assert (h->can_trim == 1);
522 assert (backend_valid_range (b, conn, offset, count));
523 assert (!(flags & ~NBDKIT_FLAG_FUA));
524 if (fua)
525 assert (h->can_fua > NBDKIT_FUA_NONE);
526 debug ("%s: trim count=%" PRIu32 " offset=%" PRIu64 " fua=%d",
527 b->name, count, offset, fua);
529 r = b->trim (b, conn, h->handle, count, offset, flags, err);
530 if (r == -1)
531 assert (*err);
532 return r;
536 backend_zero (struct backend *b, struct connection *conn,
537 uint32_t count, uint64_t offset, uint32_t flags,
538 int *err)
540 struct b_conn_handle *h = &conn->handles[b->i];
541 bool fua = !!(flags & NBDKIT_FLAG_FUA);
542 bool fast = !!(flags & NBDKIT_FLAG_FAST_ZERO);
543 int r;
545 assert (h->handle);
546 assert (h->can_write == 1);
547 assert (h->can_zero > NBDKIT_ZERO_NONE);
548 assert (backend_valid_range (b, conn, offset, count));
549 assert (!(flags & ~(NBDKIT_FLAG_MAY_TRIM | NBDKIT_FLAG_FUA |
550 NBDKIT_FLAG_FAST_ZERO)));
551 if (fua)
552 assert (h->can_fua > NBDKIT_FUA_NONE);
553 if (fast)
554 assert (h->can_fast_zero == 1);
555 debug ("%s: zero count=%" PRIu32 " offset=%" PRIu64
556 " may_trim=%d fua=%d fast=%d",
557 b->name, count, offset, !!(flags & NBDKIT_FLAG_MAY_TRIM), fua, fast);
559 r = b->zero (b, conn, h->handle, count, offset, flags, err);
560 if (r == -1) {
561 assert (*err);
562 if (!fast)
563 assert (*err != ENOTSUP && *err != EOPNOTSUPP);
565 return r;
569 backend_extents (struct backend *b, struct connection *conn,
570 uint32_t count, uint64_t offset, uint32_t flags,
571 struct nbdkit_extents *extents, int *err)
573 struct b_conn_handle *h = &conn->handles[b->i];
574 int r;
576 assert (h->handle);
577 assert (h->can_extents >= 0);
578 assert (backend_valid_range (b, conn, offset, count));
579 assert (!(flags & ~NBDKIT_FLAG_REQ_ONE));
580 debug ("%s: extents count=%" PRIu32 " offset=%" PRIu64 " req_one=%d",
581 b->name, count, offset, !!(flags & NBDKIT_FLAG_REQ_ONE));
583 if (h->can_extents == 0) {
584 /* By default it is safe assume that everything in the range is
585 * allocated.
587 r = nbdkit_add_extent (extents, offset, count, 0 /* allocated data */);
588 if (r == -1)
589 *err = errno;
590 return r;
592 r = b->extents (b, conn, h->handle, count, offset, flags, extents, err);
593 if (r == -1)
594 assert (*err);
595 return r;
599 backend_cache (struct backend *b, struct connection *conn,
600 uint32_t count, uint64_t offset,
601 uint32_t flags, int *err)
603 struct b_conn_handle *h = &conn->handles[b->i];
604 int r;
606 assert (h->handle);
607 assert (h->can_cache > NBDKIT_CACHE_NONE);
608 assert (backend_valid_range (b, conn, offset, count));
609 assert (flags == 0);
610 debug ("%s: cache count=%" PRIu32 " offset=%" PRIu64,
611 b->name, count, offset);
613 if (h->can_cache == NBDKIT_CACHE_EMULATE) {
614 static char buf[MAX_REQUEST_SIZE]; /* data sink, never read */
615 uint32_t limit;
617 while (count) {
618 limit = MIN (count, sizeof buf);
619 if (backend_pread (b, conn, buf, limit, offset, flags, err) == -1)
620 return -1;
621 count -= limit;
623 return 0;
625 r = b->cache (b, conn, h->handle, count, offset, flags, err);
626 if (r == -1)
627 assert (*err);
628 return r;