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
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
45 /* Helpers for registering a new backend. */
47 /* Set all debug flags which apply to this backend. */
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
;
58 /* Synthesize the name of the variable. */
59 if (asprintf (&var
, "%s_debug_%s", name
, flag
->flag
) == -1) {
64 /* Find the symbol. */
65 sym
= dlsym (dl
, var
);
68 "%s: -D %s.%s: %s does not contain a "
69 "global variable called %s\n",
70 program_name
, name
, flag
->flag
, name
, var
);
77 /* Mark this flag as used. */
84 backend_init (struct backend
*b
, struct backend
*next
, size_t index
,
85 const char *filename
, void *dl
, const char *type
)
90 b
->filename
= strdup (filename
);
91 if (b
->filename
== NULL
) {
97 debug ("registering %s %s", type
, filename
);
101 backend_load (struct backend
*b
, const char *name
, void (*load
) (void))
105 /* name is required. */
107 fprintf (stderr
, "%s: %s: %s must have a .name field\n",
108 program_name
, b
->filename
, b
->type
);
114 fprintf (stderr
, "%s: %s: %s.name field must not be empty\n",
115 program_name
, b
->filename
, b
->type
);
118 for (i
= 0; i
< len
; ++i
) {
119 unsigned char c
= name
[i
];
121 if (!(isascii (c
) && isalnum (c
))) {
123 "%s: %s: %s.name ('%s') field "
124 "must contain only ASCII alphanumeric characters\n",
125 program_name
, b
->filename
, b
->type
, name
);
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
) {
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
);
151 backend_unload (struct backend
*b
, void (*unload
) (void))
153 /* Acquiring this lock prevents any other backend callbacks from running
158 debug ("%s: unload %s", b
->name
, b
->type
);
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);
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
);
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
);
203 backend_prepare (struct backend
*b
, struct connection
*conn
)
205 struct b_conn_handle
*h
= &conn
->handles
[b
->i
];
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)
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)
220 h
->state
|= HANDLE_CONNECTED
;
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
)
240 assert (h
->state
& HANDLE_CONNECTED
);
241 if (b
->finalize (b
, conn
, h
->handle
) == -1) {
242 h
->state
|= HANDLE_FAILED
;
247 assert (! (h
->state
& HANDLE_CONNECTED
));
250 return backend_finalize (b
->next
, conn
);
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
);
263 assert (h
->state
& HANDLE_OPEN
);
264 b
->close (b
, conn
, h
->handle
);
267 assert (! (h
->state
& HANDLE_OPEN
));
268 reset_b_conn_handle (h
);
270 backend_close (b
->next
, conn
);
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)
293 backend_close (b
, conn
);
294 if (backend_open (b
, conn
, readonly
) == -1) {
295 backend_close (b
, conn
);
298 if (backend_prepare (b
, conn
) == -1) {
299 backend_finalize (b
, conn
);
300 backend_close (b
, conn
);
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
);
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
);
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
];
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
);
373 h
->can_trim
= b
->can_trim (b
, conn
, h
->handle
);
379 backend_can_zero (struct backend
*b
, struct connection
*conn
)
381 struct b_conn_handle
*h
= &conn
->handles
[b
->i
];
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
);
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
);
399 backend_can_fast_zero (struct backend
*b
, struct connection
*conn
)
401 struct b_conn_handle
*h
= &conn
->handles
[b
->i
];
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
];
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
);
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
);
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
);
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
];
485 assert (h
->handle
&& (h
->state
& HANDLE_CONNECTED
));
486 assert (backend_valid_range (b
, conn
, offset
, count
));
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
);
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
);
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
));
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
);
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
];
528 assert (h
->handle
&& (h
->state
& HANDLE_CONNECTED
));
529 assert (h
->can_flush
== 1);
531 debug ("%s: flush", b
->name
);
533 r
= b
->flush (b
, conn
, h
->handle
, flags
, err
);
540 backend_trim (struct backend
*b
, struct connection
*conn
,
541 uint32_t count
, uint64_t offset
, uint32_t flags
,
544 struct b_conn_handle
*h
= &conn
->handles
[b
->i
];
545 bool fua
= !!(flags
& NBDKIT_FLAG_FUA
);
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
));
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
);
565 backend_zero (struct backend
*b
, struct connection
*conn
,
566 uint32_t count
, uint64_t offset
, uint32_t flags
,
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
);
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
)));
581 assert (h
->can_fua
> NBDKIT_FUA_NONE
);
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
);
592 assert (*err
!= ENOTSUP
&& *err
!= EOPNOTSUPP
);
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
];
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
616 r
= nbdkit_add_extent (extents
, offset
, count
, 0 /* allocated data */);
621 r
= b
->extents (b
, conn
, h
->handle
, count
, offset
, flags
, extents
, err
);
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
];
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
));
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 */
647 limit
= MIN (count
, sizeof buf
);
648 if (backend_pread (b
, conn
, buf
, limit
, offset
, flags
, err
) == -1)
654 r
= b
->cache (b
, conn
, h
->handle
, count
, offset
, flags
, err
);