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
->can_write
== -1);
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
);
195 if (b
->i
) /* A filter must not succeed unless its backend did also */
196 assert (conn
->handles
[b
->i
- 1].handle
);
201 backend_prepare (struct backend
*b
, struct connection
*conn
)
203 struct b_conn_handle
*h
= &conn
->handles
[b
->i
];
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)
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.
228 debug ("%s: finalize", b
->name
);
230 if (b
->finalize (b
, conn
, h
->handle
) == -1)
234 return backend_finalize (b
->next
, conn
);
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
);
249 backend_close (b
->next
, conn
);
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
);
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
);
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
);
298 if (h
->can_write
== -1)
299 h
->can_write
= b
->can_write (b
, conn
, h
->handle
);
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
);
311 if (h
->can_flush
== -1)
312 h
->can_flush
= b
->can_flush (b
, conn
, h
->handle
);
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
);
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
];
335 debug ("%s: can_trim", b
->name
);
338 if (h
->can_trim
== -1) {
339 r
= backend_can_write (b
, conn
);
344 h
->can_trim
= b
->can_trim (b
, conn
, h
->handle
);
350 backend_can_zero (struct backend
*b
, struct connection
*conn
)
352 struct b_conn_handle
*h
= &conn
->handles
[b
->i
];
355 debug ("%s: can_zero", b
->name
);
358 if (h
->can_zero
== -1) {
359 r
= backend_can_write (b
, conn
);
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
);
370 backend_can_fast_zero (struct backend
*b
, struct connection
*conn
)
372 struct b_conn_handle
*h
= &conn
->handles
[b
->i
];
375 debug ("%s: can_fast_zero", b
->name
);
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
);
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
];
408 debug ("%s: can_fua", b
->name
);
411 if (h
->can_fua
== -1) {
412 r
= backend_can_write (b
, conn
);
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
);
423 backend_can_multi_conn (struct backend
*b
, struct connection
*conn
)
425 struct b_conn_handle
*h
= &conn
->handles
[b
->i
];
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
);
443 if (h
->can_cache
== -1)
444 h
->can_cache
= b
->can_cache (b
, conn
, h
->handle
);
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
];
457 assert (backend_valid_range (b
, conn
, offset
, count
));
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
);
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
);
478 assert (h
->can_write
== 1);
479 assert (backend_valid_range (b
, conn
, offset
, count
));
480 assert (!(flags
& ~NBDKIT_FLAG_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
);
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
];
500 assert (h
->can_flush
== 1);
502 debug ("%s: flush", b
->name
);
504 r
= b
->flush (b
, conn
, h
->handle
, flags
, err
);
511 backend_trim (struct backend
*b
, struct connection
*conn
,
512 uint32_t count
, uint64_t offset
, uint32_t flags
,
515 struct b_conn_handle
*h
= &conn
->handles
[b
->i
];
516 bool fua
= !!(flags
& NBDKIT_FLAG_FUA
);
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
));
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
);
536 backend_zero (struct backend
*b
, struct connection
*conn
,
537 uint32_t count
, uint64_t offset
, uint32_t flags
,
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
);
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
)));
552 assert (h
->can_fua
> NBDKIT_FUA_NONE
);
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
);
563 assert (*err
!= ENOTSUP
&& *err
!= EOPNOTSUPP
);
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
];
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
587 r
= nbdkit_add_extent (extents
, offset
, count
, 0 /* allocated data */);
592 r
= b
->extents (b
, conn
, h
->handle
, count
, offset
, flags
, extents
, err
);
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
];
607 assert (h
->can_cache
> NBDKIT_CACHE_NONE
);
608 assert (backend_valid_range (b
, conn
, offset
, count
));
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 */
618 limit
= MIN (count
, sizeof buf
);
619 if (backend_pread (b
, conn
, buf
, limit
, offset
, flags
, err
) == -1)
625 r
= b
->cache (b
, conn
, h
->handle
, count
, offset
, flags
, err
);