1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778
|
/* nbdkit
* Copyright Red Hat
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of Red Hat nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <inttypes.h>
#include <assert.h>
#include "internal.h"
/* We extend the generic backend struct with extra fields relating
* to this filter.
*/
struct backend_filter {
struct backend backend;
struct nbdkit_filter filter;
};
/* Note this frees the whole chain. */
static void
filter_free (struct backend *b)
{
struct backend_filter *f = container_of (b, struct backend_filter, backend);
b->next->free (b->next);
backend_unload (b, f->filter.unload);
free (f);
}
static int
filter_thread_model (struct backend *b)
{
struct backend_filter *f = container_of (b, struct backend_filter, backend);
int filter_thread_model = NBDKIT_THREAD_MODEL_PARALLEL;
int model = b->next->thread_model (b->next);
if (f->filter.thread_model) {
filter_thread_model = f->filter.thread_model (model);
if (filter_thread_model == -1)
exit (EXIT_FAILURE);
}
if (filter_thread_model < model) /* more serialized */
model = filter_thread_model;
return model;
}
/* This is actually passing the request through to the final plugin,
* hence the function name.
*/
static const char *
plugin_name (struct backend *b)
{
return b->next->plugin_name (b->next);
}
static const char *
filter_version (struct backend *b)
{
struct backend_filter *f = container_of (b, struct backend_filter, backend);
return f->filter._version;
}
static void
filter_usage (struct backend *b)
{
struct backend_filter *f = container_of (b, struct backend_filter, backend);
const char *p;
printf ("filter: %s", b->name);
if (f->filter.longname)
printf (" (%s)", f->filter.longname);
printf ("\n");
printf ("(%s)\n", b->filename);
if (f->filter.description) {
printf ("%s", f->filter.description);
if ((p = strrchr (f->filter.description, '\n')) == NULL || p[1])
printf ("\n");
}
if (f->filter.config_help) {
printf ("%s", f->filter.config_help);
if ((p = strrchr (f->filter.config_help, '\n')) == NULL || p[1])
printf ("\n");
}
}
/* This implements the --dump-plugin option which can dump additional
* fields for any filters present.
*/
static void
filter_dump_fields (struct backend *b)
{
struct backend_filter *f = container_of (b, struct backend_filter, backend);
char *path;
b->next->dump_fields (b->next);
debug ("%s: dump_plugin", b->name);
/* Dump some information about the filter. */
path = nbdkit_realpath (b->filename);
printf ("%s_path=%s\n", b->name, path);
free (path);
printf ("%s_name=%s\n", b->name, b->name);
/* We could add filter struct fields here, but they are not so
* interesting for filters because they are always tied to the exact
* current version of nbdkit so we can determine which fields are
* present just by looking at the source.
*/
/* Custom fields. */
if (f->filter.dump_plugin) {
f->filter.dump_plugin ();
}
}
static int
next_config (struct backend *b, const char *key, const char *value)
{
b->config (b, key, value);
return 0;
}
static void
filter_config (struct backend *b, const char *key, const char *value)
{
struct backend_filter *f = container_of (b, struct backend_filter, backend);
debug ("%s: config key=%s, value=%s",
b->name, key, value);
if (f->filter.config) {
if (f->filter.config (next_config, b->next, key, value) == -1)
exit (EXIT_FAILURE);
}
else
b->next->config (b->next, key, value);
}
static int
next_config_complete (struct backend *b)
{
b->config_complete (b);
return 0;
}
static void
filter_config_complete (struct backend *b)
{
struct backend_filter *f = container_of (b, struct backend_filter, backend);
debug ("%s: config_complete", b->name);
if (f->filter.config_complete) {
if (f->filter.config_complete (next_config_complete, b->next) == -1)
exit (EXIT_FAILURE);
}
else
b->next->config_complete (b->next);
}
static void
filter_get_ready (struct backend *b)
{
struct backend_filter *f = container_of (b, struct backend_filter, backend);
b->next->get_ready (b->next); /* exits on failure */
debug ("%s: get_ready thread_model=%d", b->name, thread_model);
if (f->filter.get_ready) {
if (f->filter.get_ready (thread_model) == -1)
exit (EXIT_FAILURE);
}
}
static void
filter_after_fork (struct backend *b)
{
struct backend_filter *f = container_of (b, struct backend_filter, backend);
b->next->after_fork (b->next); /* exits on failure */
debug ("%s: after_fork", b->name);
if (f->filter.after_fork) {
if (f->filter.after_fork (b->next) == -1)
exit (EXIT_FAILURE);
}
}
static void
filter_cleanup (struct backend *b)
{
struct backend_filter *f = container_of (b, struct backend_filter, backend);
debug ("%s: cleanup", b->name);
if (f->filter.cleanup)
f->filter.cleanup (b->next);
b->next->cleanup (b->next);
}
static int
filter_preconnect (struct backend *b, int readonly)
{
struct backend_filter *f = container_of (b, struct backend_filter, backend);
debug ("%s: preconnect", b->name);
if (f->filter.preconnect)
return f->filter.preconnect (b->next->preconnect, b->next, readonly);
else
return b->next->preconnect (b->next, readonly);
}
/* magic_config_key only applies to plugins, so this passes the
* request through to the plugin (hence the name).
*/
static const char *
plugin_magic_config_key (struct backend *b)
{
return b->next->magic_config_key (b->next);
}
static int
filter_list_exports (struct backend *b, int readonly, int is_tls,
struct nbdkit_exports *exports)
{
struct backend_filter *f = container_of (b, struct backend_filter, backend);
if (f->filter.list_exports)
return f->filter.list_exports (backend_list_exports, b->next,
readonly, is_tls, exports);
return backend_list_exports (b->next, readonly, exports);
}
static const char *
filter_default_export (struct backend *b, int readonly, int is_tls)
{
struct backend_filter *f = container_of (b, struct backend_filter, backend);
if (f->filter.default_export)
return f->filter.default_export (backend_default_export, b->next,
readonly, is_tls);
return backend_default_export (b->next, readonly);
}
static int
next_open (struct context *c, int readonly, const char *exportname)
{
struct backend *b = nbdkit_context_get_backend (c);
struct context *c_next = nbdkit_next_context_open (b, readonly, exportname,
false);
struct context *old;
if (c_next == NULL)
return -1;
old = nbdkit_context_set_next (c, c_next);
assert (old == NULL);
return 0;
}
static void *
filter_open (struct context *c, int readonly, const char *exportname,
int is_tls)
{
struct backend *b = c->b;
struct backend_filter *f = container_of (b, struct backend_filter, backend);
void *handle;
/* Most filters will call next_open first, resulting in
* inner-to-outer ordering.
*/
if (f->filter.open)
handle = f->filter.open (next_open, c, readonly, exportname,
is_tls);
else if (next_open (c, readonly, exportname) == -1)
handle = NULL;
else
handle = NBDKIT_HANDLE_NOT_NEEDED;
return handle;
}
static void
filter_close (struct context *c)
{
struct backend *b = c->b;
struct backend_filter *f = container_of (b, struct backend_filter, backend);
assert (c->handle);
if (f->filter.close)
f->filter.close (c->handle);
}
static int
filter_prepare (struct context *c, int readonly)
{
struct backend *b = c->b;
struct backend_filter *f = container_of (b, struct backend_filter, backend);
struct context *c_next = c->c_next;
if (f->filter.prepare &&
f->filter.prepare (c_next, c->handle, readonly) == -1)
return -1;
return 0;
}
static int
filter_finalize (struct context *c)
{
struct backend *b = c->b;
struct backend_filter *f = container_of (b, struct backend_filter, backend);
struct context *c_next = c->c_next;
if (f->filter.finalize &&
f->filter.finalize (c_next, c->handle) == -1)
return -1;
return 0;
}
static const char *
filter_export_description (struct context *c)
{
struct backend *b = c->b;
struct backend_filter *f = container_of (b, struct backend_filter, backend);
struct context *c_next = c->c_next;
if (f->filter.export_description)
return f->filter.export_description (c_next, c->handle);
else
return backend_export_description (c_next);
}
static int64_t
filter_get_size (struct context *c)
{
struct backend *b = c->b;
struct backend_filter *f = container_of (b, struct backend_filter, backend);
struct context *c_next = c->c_next;
if (f->filter.get_size)
return f->filter.get_size (c_next, c->handle);
else
return backend_get_size (c_next);
}
static int
filter_block_size (struct context *c,
uint32_t *minimum, uint32_t *preferred, uint32_t *maximum)
{
struct backend *b = c->b;
struct backend_filter *f = container_of (b, struct backend_filter, backend);
struct context *c_next = c->c_next;
if (f->filter.block_size)
return f->filter.block_size (c_next, c->handle,
minimum, preferred, maximum);
else
return backend_block_size (c_next,
minimum, preferred, maximum);
}
static int
filter_can_write (struct context *c)
{
struct backend *b = c->b;
struct backend_filter *f = container_of (b, struct backend_filter, backend);
struct context *c_next = c->c_next;
if (f->filter.can_write)
return f->filter.can_write (c_next, c->handle);
else
return backend_can_write (c_next);
}
static int
filter_can_flush (struct context *c)
{
struct backend *b = c->b;
struct backend_filter *f = container_of (b, struct backend_filter, backend);
struct context *c_next = c->c_next;
if (f->filter.can_flush)
return f->filter.can_flush (c_next, c->handle);
else
return backend_can_flush (c_next);
}
static int
filter_is_rotational (struct context *c)
{
struct backend *b = c->b;
struct backend_filter *f = container_of (b, struct backend_filter, backend);
struct context *c_next = c->c_next;
if (f->filter.is_rotational)
return f->filter.is_rotational (c_next, c->handle);
else
return backend_is_rotational (c_next);
}
static int
filter_can_trim (struct context *c)
{
struct backend *b = c->b;
struct backend_filter *f = container_of (b, struct backend_filter, backend);
struct context *c_next = c->c_next;
if (f->filter.can_trim)
return f->filter.can_trim (c_next, c->handle);
else
return backend_can_trim (c_next);
}
static int
filter_can_zero (struct context *c)
{
struct backend *b = c->b;
struct backend_filter *f = container_of (b, struct backend_filter, backend);
struct context *c_next = c->c_next;
if (f->filter.can_zero)
return f->filter.can_zero (c_next, c->handle);
else
return backend_can_zero (c_next);
}
static int
filter_can_fast_zero (struct context *c)
{
struct backend *b = c->b;
struct backend_filter *f = container_of (b, struct backend_filter, backend);
struct context *c_next = c->c_next;
if (f->filter.can_fast_zero)
return f->filter.can_fast_zero (c_next, c->handle);
else
return backend_can_fast_zero (c_next);
}
static int
filter_can_extents (struct context *c)
{
struct backend *b = c->b;
struct backend_filter *f = container_of (b, struct backend_filter, backend);
struct context *c_next = c->c_next;
if (f->filter.can_extents)
return f->filter.can_extents (c_next, c->handle);
else
return backend_can_extents (c_next);
}
static int
filter_can_fua (struct context *c)
{
struct backend *b = c->b;
struct backend_filter *f = container_of (b, struct backend_filter, backend);
struct context *c_next = c->c_next;
if (f->filter.can_fua)
return f->filter.can_fua (c_next, c->handle);
else
return backend_can_fua (c_next);
}
static int
filter_can_multi_conn (struct context *c)
{
struct backend *b = c->b;
struct backend_filter *f = container_of (b, struct backend_filter, backend);
struct context *c_next = c->c_next;
if (f->filter.can_multi_conn)
return f->filter.can_multi_conn (c_next, c->handle);
else
return backend_can_multi_conn (c_next);
}
static int
filter_can_cache (struct context *c)
{
struct backend *b = c->b;
struct backend_filter *f = container_of (b, struct backend_filter, backend);
struct context *c_next = c->c_next;
if (f->filter.can_cache)
return f->filter.can_cache (c_next, c->handle);
else
return backend_can_cache (c_next);
}
static int
filter_pread (struct context *c,
void *buf, uint32_t count, uint64_t offset,
uint32_t flags, int *err)
{
struct backend *b = c->b;
struct backend_filter *f = container_of (b, struct backend_filter, backend);
struct context *c_next = c->c_next;
if (f->filter.pread)
return f->filter.pread (c_next, c->handle,
buf, count, offset, flags, err);
else
return backend_pread (c_next, buf, count, offset, flags, err);
}
static int
filter_pwrite (struct context *c,
const void *buf, uint32_t count, uint64_t offset,
uint32_t flags, int *err)
{
struct backend *b = c->b;
struct backend_filter *f = container_of (b, struct backend_filter, backend);
struct context *c_next = c->c_next;
if (f->filter.pwrite)
return f->filter.pwrite (c_next, c->handle,
buf, count, offset, flags, err);
else
return backend_pwrite (c_next, buf, count, offset, flags, err);
}
static int
filter_flush (struct context *c,
uint32_t flags, int *err)
{
struct backend *b = c->b;
struct backend_filter *f = container_of (b, struct backend_filter, backend);
struct context *c_next = c->c_next;
if (f->filter.flush)
return f->filter.flush (c_next, c->handle, flags, err);
else
return backend_flush (c_next, flags, err);
}
static int
filter_trim (struct context *c,
uint32_t count, uint64_t offset,
uint32_t flags, int *err)
{
struct backend *b = c->b;
struct backend_filter *f = container_of (b, struct backend_filter, backend);
struct context *c_next = c->c_next;
if (f->filter.trim)
return f->filter.trim (c_next, c->handle, count, offset,
flags, err);
else
return backend_trim (c_next, count, offset, flags, err);
}
static int
filter_zero (struct context *c,
uint32_t count, uint64_t offset, uint32_t flags, int *err)
{
struct backend *b = c->b;
struct backend_filter *f = container_of (b, struct backend_filter, backend);
struct context *c_next = c->c_next;
if (f->filter.zero)
return f->filter.zero (c_next, c->handle,
count, offset, flags, err);
else
return backend_zero (c_next, count, offset, flags, err);
}
static int
filter_extents (struct context *c,
uint32_t count, uint64_t offset, uint32_t flags,
struct nbdkit_extents *extents, int *err)
{
struct backend *b = c->b;
struct backend_filter *f = container_of (b, struct backend_filter, backend);
struct context *c_next = c->c_next;
if (f->filter.extents)
return f->filter.extents (c_next, c->handle,
count, offset, flags,
extents, err);
else
return backend_extents (c_next, count, offset, flags,
extents, err);
}
static int
filter_cache (struct context *c,
uint32_t count, uint64_t offset,
uint32_t flags, int *err)
{
struct backend *b = c->b;
struct backend_filter *f = container_of (b, struct backend_filter, backend);
struct context *c_next = c->c_next;
if (f->filter.cache)
return f->filter.cache (c_next, c->handle,
count, offset, flags, err);
else
return backend_cache (c_next, count, offset, flags, err);
}
static struct backend filter_functions = {
.free = filter_free,
.thread_model = filter_thread_model,
.plugin_name = plugin_name,
.usage = filter_usage,
.version = filter_version,
.dump_fields = filter_dump_fields,
.config = filter_config,
.config_complete = filter_config_complete,
.magic_config_key = plugin_magic_config_key,
.get_ready = filter_get_ready,
.after_fork = filter_after_fork,
.cleanup = filter_cleanup,
.preconnect = filter_preconnect,
.list_exports = filter_list_exports,
.default_export = filter_default_export,
.open = filter_open,
.prepare = filter_prepare,
.finalize = filter_finalize,
.close = filter_close,
.export_description = filter_export_description,
.get_size = filter_get_size,
.block_size = filter_block_size,
.can_write = filter_can_write,
.can_flush = filter_can_flush,
.is_rotational = filter_is_rotational,
.can_trim = filter_can_trim,
.can_zero = filter_can_zero,
.can_fast_zero = filter_can_fast_zero,
.can_extents = filter_can_extents,
.can_fua = filter_can_fua,
.can_multi_conn = filter_can_multi_conn,
.can_cache = filter_can_cache,
.pread = filter_pread,
.pwrite = filter_pwrite,
.flush = filter_flush,
.trim = filter_trim,
.zero = filter_zero,
.extents = filter_extents,
.cache = filter_cache,
};
/* Register and load a filter. */
struct backend *
filter_register (struct backend *next, size_t index, const char *filename,
void *dl, filter_init_function filter_init)
{
struct backend_filter *f;
const struct nbdkit_filter *filter;
f = calloc (1, sizeof *f);
if (f == NULL) {
perror ("strdup");
exit (EXIT_FAILURE);
}
f->backend = filter_functions;
backend_init (&f->backend, next, index, filename, dl, "filter");
/* Call the initialization function which returns the address of the
* filter's own 'struct nbdkit_filter'.
*/
filter = filter_init ();
if (!filter) {
fprintf (stderr, "%s: %s: filter registration function failed\n",
program_name, filename);
exit (EXIT_FAILURE);
}
/* We do not provide API or ABI guarantees for filters, other than
* the ABI position and API contents of _api_version and _version to
* diagnose mismatch from the current nbdkit version.
*/
if (filter->_api_version != NBDKIT_FILTER_API_VERSION) {
fprintf (stderr,
"%s: %s: filter is incompatible with this version of nbdkit "
"(_api_version = %d, need %d)\n",
program_name, filename, filter->_api_version,
NBDKIT_FILTER_API_VERSION);
exit (EXIT_FAILURE);
}
if (filter->_version == NULL ||
strcmp (filter->_version, PACKAGE_VERSION) != 0) {
fprintf (stderr,
"%s: %s: filter is incompatible with this version of nbdkit "
"(_version = %s, need %s)\n",
program_name, filename, filter->_version ?: "<null>",
PACKAGE_VERSION);
exit (EXIT_FAILURE);
}
f->filter = *filter;
backend_load (&f->backend, f->filter.name, f->filter.load);
return (struct backend *) f;
}
NBDKIT_DLL_PUBLIC struct backend *
nbdkit_context_get_backend (struct context *c)
{
assert (c);
return c->b->next;
}
NBDKIT_DLL_PUBLIC struct context *
nbdkit_next_context_open (struct backend *b,
int readonly, const char *exportname, int shared)
{
struct context *c = threadlocal_get_context ();
assert (b);
assert (!c || b == c->b->next);
return backend_open (b, readonly, exportname, shared || !c || !c->conn);
}
NBDKIT_DLL_PUBLIC void
nbdkit_next_context_close (struct context *c)
{
if (c)
backend_close (c);
}
NBDKIT_DLL_PUBLIC struct context *
nbdkit_context_set_next (struct context *c, struct context *next)
{
struct context *old;
assert (c);
if (next)
assert (next->b == c->b->next);
old = c->c_next;
c->c_next = next;
return old;
}
|