Update Red Hat Copyright Notices
[nbdkit.git] / server / backend.c
blobef7df67cd094ecbc50b623a12210c21bbca4dddb
1 /* nbdkit
2 * Copyright Red Hat
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>
41 #include <dlfcn.h>
43 #include "ascii-ctype.h"
44 #include "minmax.h"
46 #include "internal.h"
48 /* Helpers for registering a new backend. */
50 /* Use:
51 * -D nbdkit.backend.controlpath=0 to suppress control path debugging.
52 * -D nbdkit.backend.datapath=0 to suppress data path debugging.
54 NBDKIT_DLL_PUBLIC int nbdkit_debug_backend_controlpath = 1;
55 NBDKIT_DLL_PUBLIC int nbdkit_debug_backend_datapath = 1;
57 #define controlpath_debug(fs, ...) \
58 do { \
59 if (nbdkit_debug_backend_controlpath) debug ((fs), ##__VA_ARGS__); \
60 } while (0)
61 #define datapath_debug(fs, ...) \
62 do { \
63 if (nbdkit_debug_backend_datapath) debug ((fs), ##__VA_ARGS__); \
64 } while (0)
66 void
67 backend_init (struct backend *b, struct backend *next, size_t index,
68 const char *filename, void *dl, const char *type)
70 b->next = next;
71 b->i = index;
72 b->type = type;
73 b->filename = strdup (filename);
74 if (b->filename == NULL) {
75 perror ("strdup");
76 exit (EXIT_FAILURE);
78 b->dl = dl;
80 debug ("registering %s %s", type, filename);
83 void
84 backend_load (struct backend *b, const char *name, void (*load) (void))
86 size_t i, len;
88 /* name is required. */
89 if (name == NULL) {
90 fprintf (stderr, "%s: %s: %s must have a .name field\n",
91 program_name, b->filename, b->type);
92 exit (EXIT_FAILURE);
95 len = strlen (name);
96 if (len == 0) {
97 fprintf (stderr, "%s: %s: %s.name field must not be empty\n",
98 program_name, b->filename, b->type);
99 exit (EXIT_FAILURE);
101 if (! ascii_isalnum (*name)) {
102 fprintf (stderr,
103 "%s: %s: %s.name ('%s') field must begin with an "
104 "ASCII alphanumeric character\n",
105 program_name, b->filename, b->type, name);
106 exit (EXIT_FAILURE);
108 for (i = 1; i < len; ++i) {
109 unsigned char c = name[i];
111 if (! ascii_isalnum (c) && c != '-') {
112 fprintf (stderr,
113 "%s: %s: %s.name ('%s') field must contain only "
114 "ASCII alphanumeric or dash characters\n",
115 program_name, b->filename, b->type, name);
116 exit (EXIT_FAILURE);
120 /* Copy the module's name into local storage, so that name
121 * survives past unload.
123 b->name = strdup (name);
124 if (b->name == NULL) {
125 perror ("strdup");
126 exit (EXIT_FAILURE);
129 debug ("registered %s %s (name %s)", b->type, b->filename, b->name);
131 /* Apply debug flags before calling load. */
132 apply_debug_flags (b->dl, name);
134 /* Call the on-load callback if it exists. */
135 controlpath_debug ("%s: load", name);
136 if (load)
137 load ();
140 void
141 backend_unload (struct backend *b, void (*unload) (void))
143 /* Acquiring this lock prevents any other backend callbacks from running
144 * simultaneously.
146 lock_unload ();
148 controlpath_debug ("%s: unload %s", b->name, b->type);
149 if (unload)
150 unload ();
152 if (DO_DLCLOSE)
153 dlclose (b->dl);
154 free (b->filename);
156 unlock_unload ();
158 free (b->name);
162 backend_list_exports (struct backend *b, int readonly,
163 struct nbdkit_exports *exports)
165 GET_CONN;
166 size_t count;
168 controlpath_debug ("%s: list_exports readonly=%d tls=%d",
169 b->name, readonly, conn->using_tls);
171 assert (conn->top_context == NULL);
173 if (b->list_exports (b, readonly, conn->using_tls, exports) == -1 ||
174 exports_resolve_default (exports, b, readonly) == -1) {
175 controlpath_debug ("%s: list_exports failed", b->name);
176 return -1;
179 count = nbdkit_exports_count (exports);
180 controlpath_debug ("%s: list_exports returned %zu names", b->name, count);
181 return 0;
184 const char *
185 backend_default_export (struct backend *b, int readonly)
187 GET_CONN;
188 const char *s;
190 controlpath_debug ("%s: default_export readonly=%d tls=%d",
191 b->name, readonly, conn->using_tls);
193 if (conn->default_exportname[b->i] == NULL) {
194 assert (conn->top_context == NULL);
195 s = b->default_export (b, readonly, conn->using_tls);
196 /* Ignore over-length strings. XXX Also ignore non-UTF8? */
197 if (s && strnlen (s, NBD_MAX_STRING + 1) > NBD_MAX_STRING) {
198 controlpath_debug ("%s: default_export: ignoring invalid string",
199 b->name);
200 s = NULL;
202 if (s) {
203 /* Best effort caching */
204 conn->default_exportname[b->i] = strdup (s);
205 if (conn->default_exportname[b->i] == NULL)
206 return s;
209 return conn->default_exportname[b->i];
212 static struct nbdkit_next_ops next_ops = {
213 .prepare = backend_prepare,
214 .finalize = backend_finalize,
215 .export_description = backend_export_description,
216 .get_size = backend_get_size,
217 .block_size = backend_block_size,
218 .can_write = backend_can_write,
219 .can_flush = backend_can_flush,
220 .is_rotational = backend_is_rotational,
221 .can_trim = backend_can_trim,
222 .can_zero = backend_can_zero,
223 .can_fast_zero = backend_can_fast_zero,
224 .can_extents = backend_can_extents,
225 .can_fua = backend_can_fua,
226 .can_multi_conn = backend_can_multi_conn,
227 .can_cache = backend_can_cache,
228 .pread = backend_pread,
229 .pwrite = backend_pwrite,
230 .flush = backend_flush,
231 .trim = backend_trim,
232 .zero = backend_zero,
233 .extents = backend_extents,
234 .cache = backend_cache,
237 struct context *
238 backend_open (struct backend *b, int readonly, const char *exportname,
239 int shared)
241 struct connection *conn = threadlocal_get_conn ();
242 bool using_tls;
243 struct context *c;
245 if (shared)
246 using_tls = tls == 2;
247 else {
248 assert (conn);
249 using_tls = conn->using_tls;
252 c = malloc (sizeof *c);
253 if (c == NULL) {
254 nbdkit_error ("malloc: %m");
255 return NULL;
257 PUSH_CONTEXT_FOR_SCOPE (c);
259 controlpath_debug ("%s: open readonly=%d exportname=\"%s\" tls=%d",
260 b->name, readonly, exportname, using_tls);
262 c->next = next_ops;
263 c->handle = NULL;
264 c->b = b;
265 c->c_next = NULL;
266 c->conn = shared ? NULL : conn;
267 c->state = 0;
268 c->exportsize = -1;
269 c->minimum_block_size = c->preferred_block_size = c->maximum_block_size = -1;
270 c->can_write = readonly ? 0 : -1;
271 c->can_flush = -1;
272 c->is_rotational = -1;
273 c->can_trim = -1;
274 c->can_zero = -1;
275 c->can_fast_zero = -1;
276 c->can_fua = -1;
277 c->can_multi_conn = -1;
278 c->can_extents = -1;
279 c->can_cache = -1;
281 /* Determine the canonical name for default export */
282 if (!*exportname && c->conn) {
283 exportname = backend_default_export (b, readonly);
284 if (exportname == NULL) {
285 nbdkit_error ("default export (\"\") not permitted");
286 free (c);
287 return NULL;
291 /* Most filters will call next_open first, resulting in
292 * inner-to-outer ordering.
294 c->handle = b->open (c, readonly, exportname, using_tls);
295 controlpath_debug ("%s: open returned handle %p", b->name, c->handle);
297 if (c->handle == NULL) {
298 if (b->i && c->c_next != NULL)
299 backend_close (c->c_next);
300 free (c);
301 return NULL;
304 c->state |= HANDLE_OPEN;
305 return c;
309 backend_prepare (struct context *c)
311 PUSH_CONTEXT_FOR_SCOPE (c);
312 struct backend *b = c->b;
314 assert (c->handle);
315 assert (c->state & HANDLE_OPEN);
317 if (c->state & HANDLE_CONNECTED)
318 return 0;
320 /* Call these in order starting from the filter closest to the
321 * plugin, similar to typical .open order. But remember that
322 * a filter may skip opening its backend.
324 if (b->i && c->c_next != NULL && backend_prepare (c->c_next) == -1)
325 return -1;
327 controlpath_debug ("%s: prepare readonly=%d", b->name, c->can_write == 0);
329 if (b->prepare (c, c->can_write == 0) == -1)
330 return -1;
331 c->state |= HANDLE_CONNECTED;
332 return 0;
336 backend_finalize (struct context *c)
338 PUSH_CONTEXT_FOR_SCOPE (c);
339 struct backend *b = c->b;
341 /* Call these in reverse order to .prepare above, starting from the
342 * filter furthest away from the plugin, and matching .close order.
345 /* Once finalize fails, we can do nothing further on this connection */
346 if (c->state & HANDLE_FAILED)
347 return -1;
349 if (c->state & HANDLE_CONNECTED) {
350 assert (c->state & HANDLE_OPEN && c->handle);
351 controlpath_debug ("%s: finalize", b->name);
352 if (b->finalize (c) == -1) {
353 c->state |= HANDLE_FAILED;
354 return -1;
358 if (b->i && c->c_next != NULL)
359 return backend_finalize (c->c_next);
360 return 0;
363 void
364 backend_close (struct context *c)
366 PUSH_CONTEXT_FOR_SCOPE (c);
367 struct backend *b = c->b;
368 struct context *c_next = c->c_next;
370 /* outer-to-inner order, opposite .open */
371 assert (c->handle);
372 assert (c->state & HANDLE_OPEN);
373 controlpath_debug ("%s: close", b->name);
374 b->close (c);
375 free (c);
376 if (c_next != NULL)
377 backend_close (c_next);
380 bool
381 backend_valid_range (struct context *c, uint64_t offset, uint32_t count)
383 assert (c->exportsize <= INT64_MAX); /* Guaranteed by negotiation phase */
384 return count > 0 && offset <= c->exportsize &&
385 offset + count <= c->exportsize;
388 /* Wrappers for all callbacks in a filter's struct nbdkit_next_ops. */
390 const char *
391 backend_export_description (struct context *c)
393 PUSH_CONTEXT_FOR_SCOPE (c);
394 struct backend *b = c->b;
395 const char *s;
397 controlpath_debug ("%s: export_description", b->name);
399 assert (c->handle && (c->state & HANDLE_CONNECTED));
400 /* Caching is not useful for this value. */
401 s = b->export_description (c);
403 /* Ignore over-length strings. XXX Also ignore non-UTF8? */
404 if (s && strnlen (s, NBD_MAX_STRING + 1) > NBD_MAX_STRING) {
405 controlpath_debug ("%s: export_description: ignoring invalid string",
406 b->name);
407 s = NULL;
409 return s;
412 int64_t
413 backend_get_size (struct context *c)
415 PUSH_CONTEXT_FOR_SCOPE (c);
416 struct backend *b = c->b;
418 assert (c->handle && (c->state & HANDLE_CONNECTED));
419 if (c->exportsize == -1) {
420 controlpath_debug ("%s: get_size", b->name);
421 c->exportsize = b->get_size (c);
423 return c->exportsize;
427 backend_block_size (struct context *c,
428 uint32_t *minimum, uint32_t *preferred, uint32_t *maximum)
430 PUSH_CONTEXT_FOR_SCOPE (c);
431 struct backend *b = c->b;
432 int r;
434 assert (c->handle && (c->state & HANDLE_CONNECTED));
435 if (c->minimum_block_size != -1) {
436 *minimum = c->minimum_block_size;
437 *preferred = c->preferred_block_size;
438 *maximum = c->maximum_block_size;
439 return 0;
441 else {
442 controlpath_debug ("%s: block_size", b->name);
443 r = b->block_size (c, minimum, preferred, maximum);
444 if (r == 0) {
445 c->minimum_block_size = *minimum;
446 c->preferred_block_size = *preferred;
447 c->maximum_block_size = *maximum;
449 return r;
454 backend_can_write (struct context *c)
456 PUSH_CONTEXT_FOR_SCOPE (c);
457 struct backend *b = c->b;
459 assert (c->handle && (c->state & HANDLE_CONNECTED));
460 if (c->can_write == -1) {
461 controlpath_debug ("%s: can_write", b->name);
462 c->can_write = b->can_write (c);
464 return c->can_write;
468 backend_can_flush (struct context *c)
470 PUSH_CONTEXT_FOR_SCOPE (c);
471 struct backend *b = c->b;
473 assert (c->handle && (c->state & HANDLE_CONNECTED));
474 if (c->can_flush == -1) {
475 controlpath_debug ("%s: can_flush", b->name);
476 c->can_flush = b->can_flush (c);
478 return c->can_flush;
482 backend_is_rotational (struct context *c)
484 PUSH_CONTEXT_FOR_SCOPE (c);
485 struct backend *b = c->b;
487 assert (c->handle && (c->state & HANDLE_CONNECTED));
488 if (c->is_rotational == -1) {
489 controlpath_debug ("%s: is_rotational", b->name);
490 c->is_rotational = b->is_rotational (c);
492 return c->is_rotational;
496 backend_can_trim (struct context *c)
498 PUSH_CONTEXT_FOR_SCOPE (c);
499 struct backend *b = c->b;
500 int r;
502 assert (c->handle && (c->state & HANDLE_CONNECTED));
503 if (c->can_trim == -1) {
504 controlpath_debug ("%s: can_trim", b->name);
505 r = backend_can_write (c);
506 if (r != 1) {
507 c->can_trim = 0;
508 return r;
510 c->can_trim = b->can_trim (c);
512 return c->can_trim;
516 backend_can_zero (struct context *c)
518 PUSH_CONTEXT_FOR_SCOPE (c);
519 struct backend *b = c->b;
520 int r;
522 assert (c->handle && (c->state & HANDLE_CONNECTED));
523 if (c->can_zero == -1) {
524 controlpath_debug ("%s: can_zero", b->name);
525 r = backend_can_write (c);
526 if (r != 1) {
527 c->can_zero = NBDKIT_ZERO_NONE;
528 return r; /* Relies on 0 == NBDKIT_ZERO_NONE */
530 c->can_zero = b->can_zero (c);
532 return c->can_zero;
536 backend_can_fast_zero (struct context *c)
538 PUSH_CONTEXT_FOR_SCOPE (c);
539 struct backend *b = c->b;
540 int r;
542 assert (c->handle && (c->state & HANDLE_CONNECTED));
543 if (c->can_fast_zero == -1) {
544 controlpath_debug ("%s: can_fast_zero", b->name);
545 r = backend_can_zero (c);
546 if (r < NBDKIT_ZERO_EMULATE) {
547 c->can_fast_zero = 0;
548 return r; /* Relies on 0 == NBDKIT_ZERO_NONE */
550 c->can_fast_zero = b->can_fast_zero (c);
552 return c->can_fast_zero;
556 backend_can_extents (struct context *c)
558 PUSH_CONTEXT_FOR_SCOPE (c);
559 struct backend *b = c->b;
561 assert (c->handle && (c->state & HANDLE_CONNECTED));
562 if (c->can_extents == -1) {
563 controlpath_debug ("%s: can_extents", b->name);
564 c->can_extents = b->can_extents (c);
566 return c->can_extents;
570 backend_can_fua (struct context *c)
572 PUSH_CONTEXT_FOR_SCOPE (c);
573 struct backend *b = c->b;
574 int r;
576 assert (c->handle && (c->state & HANDLE_CONNECTED));
577 if (c->can_fua == -1) {
578 controlpath_debug ("%s: can_fua", b->name);
579 r = backend_can_write (c);
580 if (r != 1) {
581 c->can_fua = NBDKIT_FUA_NONE;
582 return r; /* Relies on 0 == NBDKIT_FUA_NONE */
584 c->can_fua = b->can_fua (c);
586 return c->can_fua;
590 backend_can_multi_conn (struct context *c)
592 PUSH_CONTEXT_FOR_SCOPE (c);
593 struct backend *b = c->b;
595 assert (c->handle && (c->state & HANDLE_CONNECTED));
596 if (c->can_multi_conn == -1) {
597 controlpath_debug ("%s: can_multi_conn", b->name);
598 c->can_multi_conn = b->can_multi_conn (c);
600 return c->can_multi_conn;
604 backend_can_cache (struct context *c)
606 PUSH_CONTEXT_FOR_SCOPE (c);
607 struct backend *b = c->b;
609 assert (c->handle && (c->state & HANDLE_CONNECTED));
610 if (c->can_cache == -1) {
611 controlpath_debug ("%s: can_cache", b->name);
612 c->can_cache = b->can_cache (c);
614 return c->can_cache;
618 backend_pread (struct context *c,
619 void *buf, uint32_t count, uint64_t offset,
620 uint32_t flags, int *err)
622 PUSH_CONTEXT_FOR_SCOPE (c);
623 struct backend *b = c->b;
624 int r;
626 assert (c->handle && (c->state & HANDLE_CONNECTED));
627 assert (backend_valid_range (c, offset, count));
628 assert (flags == 0);
629 datapath_debug ("%s: pread count=%" PRIu32 " offset=%" PRIu64,
630 b->name, count, offset);
632 r = b->pread (c, buf, count, offset, flags, err);
633 if (r == -1)
634 assert (*err);
635 return r;
639 backend_pwrite (struct context *c,
640 const void *buf, uint32_t count, uint64_t offset,
641 uint32_t flags, int *err)
643 PUSH_CONTEXT_FOR_SCOPE (c);
644 struct backend *b = c->b;
645 bool fua = !!(flags & NBDKIT_FLAG_FUA);
646 int r;
648 assert (c->handle && (c->state & HANDLE_CONNECTED));
649 assert (c->can_write == 1);
650 assert (backend_valid_range (c, offset, count));
651 assert (!(flags & ~NBDKIT_FLAG_FUA));
652 if (fua)
653 assert (c->can_fua > NBDKIT_FUA_NONE);
654 datapath_debug ("%s: pwrite count=%" PRIu32 " offset=%" PRIu64 " fua=%d",
655 b->name, count, offset, fua);
657 r = b->pwrite (c, buf, count, offset, flags, err);
658 if (r == -1)
659 assert (*err);
660 return r;
664 backend_flush (struct context *c,
665 uint32_t flags, int *err)
667 PUSH_CONTEXT_FOR_SCOPE (c);
668 struct backend *b = c->b;
669 int r;
671 assert (c->handle && (c->state & HANDLE_CONNECTED));
672 assert (c->can_flush == 1);
673 assert (flags == 0);
674 datapath_debug ("%s: flush", b->name);
676 r = b->flush (c, flags, err);
677 if (r == -1)
678 assert (*err);
679 return r;
683 backend_trim (struct context *c,
684 uint32_t count, uint64_t offset, uint32_t flags,
685 int *err)
687 PUSH_CONTEXT_FOR_SCOPE (c);
688 struct backend *b = c->b;
689 bool fua = !!(flags & NBDKIT_FLAG_FUA);
690 int r;
692 assert (c->handle && (c->state & HANDLE_CONNECTED));
693 assert (c->can_write == 1);
694 assert (c->can_trim == 1);
695 assert (backend_valid_range (c, offset, count));
696 assert (!(flags & ~NBDKIT_FLAG_FUA));
697 if (fua)
698 assert (c->can_fua > NBDKIT_FUA_NONE);
699 datapath_debug ("%s: trim count=%" PRIu32 " offset=%" PRIu64 " fua=%d",
700 b->name, count, offset, fua);
702 r = b->trim (c, count, offset, flags, err);
703 if (r == -1)
704 assert (*err);
705 return r;
709 backend_zero (struct context *c,
710 uint32_t count, uint64_t offset, uint32_t flags,
711 int *err)
713 PUSH_CONTEXT_FOR_SCOPE (c);
714 struct backend *b = c->b;
715 bool fua = !!(flags & NBDKIT_FLAG_FUA);
716 bool fast = !!(flags & NBDKIT_FLAG_FAST_ZERO);
717 int r;
719 assert (c->handle && (c->state & HANDLE_CONNECTED));
720 assert (c->can_write == 1);
721 assert (c->can_zero > NBDKIT_ZERO_NONE);
722 assert (backend_valid_range (c, offset, count));
723 assert (!(flags & ~(NBDKIT_FLAG_MAY_TRIM | NBDKIT_FLAG_FUA |
724 NBDKIT_FLAG_FAST_ZERO)));
725 if (fua)
726 assert (c->can_fua > NBDKIT_FUA_NONE);
727 if (fast)
728 assert (c->can_fast_zero == 1);
729 datapath_debug ("%s: zero count=%" PRIu32 " offset=%" PRIu64
730 " may_trim=%d fua=%d fast=%d",
731 b->name, count, offset,
732 !!(flags & NBDKIT_FLAG_MAY_TRIM), fua, fast);
734 if (c->can_zero == NBDKIT_ZERO_NATIVE)
735 r = b->zero (c, count, offset, flags, err);
736 else { /* NBDKIT_ZERO_EMULATE */
737 int writeflags = 0;
738 bool need_flush = false;
740 if (fast) {
741 *err = ENOTSUP;
742 return -1;
745 if (fua) {
746 if (c->can_fua == NBDKIT_FUA_EMULATE)
747 need_flush = true;
748 else
749 writeflags = NBDKIT_FLAG_FUA;
752 while (count) {
753 /* Always contains zeroes, but we can't use const or else gcc 9
754 * will use .rodata instead of .bss and inflate the binary size.
756 static /* const */ char buffer[MAX_REQUEST_SIZE];
757 uint32_t limit = MIN (count, MAX_REQUEST_SIZE);
759 if (limit == count && need_flush)
760 writeflags = NBDKIT_FLAG_FUA;
762 if (backend_pwrite (c, buffer, limit, offset, writeflags, err) == -1)
763 return -1;
764 offset += limit;
765 count -= limit;
767 r = 0;
770 if (r == -1) {
771 assert (*err);
772 if (!fast)
773 assert (*err != ENOTSUP && *err != EOPNOTSUPP);
775 return r;
779 backend_extents (struct context *c,
780 uint32_t count, uint64_t offset, uint32_t flags,
781 struct nbdkit_extents *extents, int *err)
783 PUSH_CONTEXT_FOR_SCOPE (c);
784 struct backend *b = c->b;
785 int r;
787 assert (c->handle && (c->state & HANDLE_CONNECTED));
788 assert (c->can_extents >= 0);
789 assert (backend_valid_range (c, offset, count));
790 assert (!(flags & ~NBDKIT_FLAG_REQ_ONE));
791 datapath_debug ("%s: extents count=%" PRIu32 " offset=%" PRIu64 " req_one=%d",
792 b->name, count, offset, !!(flags & NBDKIT_FLAG_REQ_ONE));
794 if (c->can_extents == 0) {
795 /* By default it is safe assume that everything in the range is
796 * allocated.
798 r = nbdkit_add_extent (extents, offset, count, 0 /* allocated data */);
799 if (r == -1)
800 *err = errno;
801 return r;
803 r = b->extents (c, count, offset, flags, extents, err);
804 if (r == -1)
805 assert (*err);
806 return r;
810 backend_cache (struct context *c,
811 uint32_t count, uint64_t offset,
812 uint32_t flags, int *err)
814 PUSH_CONTEXT_FOR_SCOPE (c);
815 struct backend *b = c->b;
816 int r;
818 assert (c->handle && (c->state & HANDLE_CONNECTED));
819 assert (c->can_cache > NBDKIT_CACHE_NONE);
820 assert (backend_valid_range (c, offset, count));
821 assert (flags == 0);
822 datapath_debug ("%s: cache count=%" PRIu32 " offset=%" PRIu64,
823 b->name, count, offset);
825 if (c->can_cache == NBDKIT_CACHE_EMULATE) {
826 static char buf[MAX_REQUEST_SIZE]; /* data sink, never read */
827 uint32_t limit;
829 while (count) {
830 limit = MIN (count, sizeof buf);
831 if (backend_pread (c, buf, limit, offset, flags, err) == -1)
832 return -1;
833 count -= limit;
834 offset += limit;
836 return 0;
838 r = b->cache (c, count, offset, flags, err);
839 if (r == -1)
840 assert (*err);
841 return r;