4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
43 #include "ascii-ctype.h"
48 /* Helpers for registering a new backend. */
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, ...) \
59 if (nbdkit_debug_backend_controlpath) debug ((fs), ##__VA_ARGS__); \
61 #define datapath_debug(fs, ...) \
63 if (nbdkit_debug_backend_datapath) debug ((fs), ##__VA_ARGS__); \
67 backend_init (struct backend
*b
, struct backend
*next
, size_t index
,
68 const char *filename
, void *dl
, const char *type
)
73 b
->filename
= strdup (filename
);
74 if (b
->filename
== NULL
) {
80 debug ("registering %s %s", type
, filename
);
84 backend_load (struct backend
*b
, const char *name
, void (*load
) (void))
88 /* name is required. */
90 fprintf (stderr
, "%s: %s: %s must have a .name field\n",
91 program_name
, b
->filename
, b
->type
);
97 fprintf (stderr
, "%s: %s: %s.name field must not be empty\n",
98 program_name
, b
->filename
, b
->type
);
101 if (! ascii_isalnum (*name
)) {
103 "%s: %s: %s.name ('%s') field must begin with an "
104 "ASCII alphanumeric character\n",
105 program_name
, b
->filename
, b
->type
, name
);
108 for (i
= 1; i
< len
; ++i
) {
109 unsigned char c
= name
[i
];
111 if (! ascii_isalnum (c
) && c
!= '-') {
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
);
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
) {
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
);
141 backend_unload (struct backend
*b
, void (*unload
) (void))
143 /* Acquiring this lock prevents any other backend callbacks from running
148 controlpath_debug ("%s: unload %s", b
->name
, b
->type
);
162 backend_list_exports (struct backend
*b
, int readonly
,
163 struct nbdkit_exports
*exports
)
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
);
179 count
= nbdkit_exports_count (exports
);
180 controlpath_debug ("%s: list_exports returned %zu names", b
->name
, count
);
185 backend_default_export (struct backend
*b
, int readonly
)
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",
203 /* Best effort caching */
204 conn
->default_exportname
[b
->i
] = strdup (s
);
205 if (conn
->default_exportname
[b
->i
] == NULL
)
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
,
238 backend_open (struct backend
*b
, int readonly
, const char *exportname
,
241 struct connection
*conn
= threadlocal_get_conn ();
246 using_tls
= tls
== 2;
249 using_tls
= conn
->using_tls
;
252 c
= malloc (sizeof *c
);
254 nbdkit_error ("malloc: %m");
257 PUSH_CONTEXT_FOR_SCOPE (c
);
259 controlpath_debug ("%s: open readonly=%d exportname=\"%s\" tls=%d",
260 b
->name
, readonly
, exportname
, using_tls
);
266 c
->conn
= shared
? NULL
: conn
;
269 c
->minimum_block_size
= c
->preferred_block_size
= c
->maximum_block_size
= -1;
270 c
->can_write
= readonly
? 0 : -1;
272 c
->is_rotational
= -1;
275 c
->can_fast_zero
= -1;
277 c
->can_multi_conn
= -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");
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
);
304 c
->state
|= HANDLE_OPEN
;
309 backend_prepare (struct context
*c
)
311 PUSH_CONTEXT_FOR_SCOPE (c
);
312 struct backend
*b
= c
->b
;
315 assert (c
->state
& HANDLE_OPEN
);
317 if (c
->state
& HANDLE_CONNECTED
)
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)
327 controlpath_debug ("%s: prepare readonly=%d", b
->name
, c
->can_write
== 0);
329 if (b
->prepare (c
, c
->can_write
== 0) == -1)
331 c
->state
|= HANDLE_CONNECTED
;
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
)
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
;
358 if (b
->i
&& c
->c_next
!= NULL
)
359 return backend_finalize (c
->c_next
);
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 */
372 assert (c
->state
& HANDLE_OPEN
);
373 controlpath_debug ("%s: close", b
->name
);
377 backend_close (c_next
);
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. */
391 backend_export_description (struct context
*c
)
393 PUSH_CONTEXT_FOR_SCOPE (c
);
394 struct backend
*b
= c
->b
;
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",
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
;
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
;
442 controlpath_debug ("%s: block_size", b
->name
);
443 r
= b
->block_size (c
, minimum
, preferred
, maximum
);
445 c
->minimum_block_size
= *minimum
;
446 c
->preferred_block_size
= *preferred
;
447 c
->maximum_block_size
= *maximum
;
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
);
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
);
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
;
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
);
510 c
->can_trim
= b
->can_trim (c
);
516 backend_can_zero (struct context
*c
)
518 PUSH_CONTEXT_FOR_SCOPE (c
);
519 struct backend
*b
= c
->b
;
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
);
527 c
->can_zero
= NBDKIT_ZERO_NONE
;
528 return r
; /* Relies on 0 == NBDKIT_ZERO_NONE */
530 c
->can_zero
= b
->can_zero (c
);
536 backend_can_fast_zero (struct context
*c
)
538 PUSH_CONTEXT_FOR_SCOPE (c
);
539 struct backend
*b
= c
->b
;
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
;
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
);
581 c
->can_fua
= NBDKIT_FUA_NONE
;
582 return r
; /* Relies on 0 == NBDKIT_FUA_NONE */
584 c
->can_fua
= b
->can_fua (c
);
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
);
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
;
626 assert (c
->handle
&& (c
->state
& HANDLE_CONNECTED
));
627 assert (backend_valid_range (c
, offset
, count
));
629 datapath_debug ("%s: pread count=%" PRIu32
" offset=%" PRIu64
,
630 b
->name
, count
, offset
);
632 r
= b
->pread (c
, buf
, count
, offset
, flags
, err
);
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
);
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
));
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
);
664 backend_flush (struct context
*c
,
665 uint32_t flags
, int *err
)
667 PUSH_CONTEXT_FOR_SCOPE (c
);
668 struct backend
*b
= c
->b
;
671 assert (c
->handle
&& (c
->state
& HANDLE_CONNECTED
));
672 assert (c
->can_flush
== 1);
674 datapath_debug ("%s: flush", b
->name
);
676 r
= b
->flush (c
, flags
, err
);
683 backend_trim (struct context
*c
,
684 uint32_t count
, uint64_t offset
, uint32_t flags
,
687 PUSH_CONTEXT_FOR_SCOPE (c
);
688 struct backend
*b
= c
->b
;
689 bool fua
= !!(flags
& NBDKIT_FLAG_FUA
);
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
));
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
);
709 backend_zero (struct context
*c
,
710 uint32_t count
, uint64_t offset
, uint32_t flags
,
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
);
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
)));
726 assert (c
->can_fua
> NBDKIT_FUA_NONE
);
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 */
738 bool need_flush
= false;
746 if (c
->can_fua
== NBDKIT_FUA_EMULATE
)
749 writeflags
= NBDKIT_FLAG_FUA
;
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)
773 assert (*err
!= ENOTSUP
&& *err
!= EOPNOTSUPP
);
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
;
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
798 r
= nbdkit_add_extent (extents
, offset
, count
, 0 /* allocated data */);
803 r
= b
->extents (c
, count
, offset
, flags
, extents
, err
);
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
;
818 assert (c
->handle
&& (c
->state
& HANDLE_CONNECTED
));
819 assert (c
->can_cache
> NBDKIT_CACHE_NONE
);
820 assert (backend_valid_range (c
, offset
, count
));
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 */
830 limit
= MIN (count
, sizeof buf
);
831 if (backend_pread (c
, buf
, limit
, offset
, flags
, err
) == -1)
838 r
= b
->cache (c
, count
, offset
, flags
, err
);