156ee8626SKevin Wolf /* 256ee8626SKevin Wolf * Common block export infrastructure 356ee8626SKevin Wolf * 456ee8626SKevin Wolf * Copyright (c) 2012, 2020 Red Hat, Inc. 556ee8626SKevin Wolf * 656ee8626SKevin Wolf * Authors: 756ee8626SKevin Wolf * Paolo Bonzini <pbonzini@redhat.com> 856ee8626SKevin Wolf * Kevin Wolf <kwolf@redhat.com> 956ee8626SKevin Wolf * 1056ee8626SKevin Wolf * This work is licensed under the terms of the GNU GPL, version 2 or 1156ee8626SKevin Wolf * later. See the COPYING file in the top-level directory. 1256ee8626SKevin Wolf */ 1356ee8626SKevin Wolf 1456ee8626SKevin Wolf #include "qemu/osdep.h" 1556ee8626SKevin Wolf 169b562c64SKevin Wolf #include "block/block.h" 179b562c64SKevin Wolf #include "sysemu/block-backend.h" 1856ee8626SKevin Wolf #include "block/export.h" 1956ee8626SKevin Wolf #include "block/nbd.h" 2056ee8626SKevin Wolf #include "qapi/error.h" 2156ee8626SKevin Wolf #include "qapi/qapi-commands-block-export.h" 2256ee8626SKevin Wolf 2356ee8626SKevin Wolf static const BlockExportDriver *blk_exp_drivers[] = { 2456ee8626SKevin Wolf &blk_exp_nbd, 2556ee8626SKevin Wolf }; 2656ee8626SKevin Wolf 2756ee8626SKevin Wolf static const BlockExportDriver *blk_exp_find_driver(BlockExportType type) 2856ee8626SKevin Wolf { 2956ee8626SKevin Wolf int i; 3056ee8626SKevin Wolf 3156ee8626SKevin Wolf for (i = 0; i < ARRAY_SIZE(blk_exp_drivers); i++) { 3256ee8626SKevin Wolf if (blk_exp_drivers[i]->type == type) { 3356ee8626SKevin Wolf return blk_exp_drivers[i]; 3456ee8626SKevin Wolf } 3556ee8626SKevin Wolf } 3656ee8626SKevin Wolf return NULL; 3756ee8626SKevin Wolf } 3856ee8626SKevin Wolf 399b562c64SKevin Wolf BlockExport *blk_exp_add(BlockExportOptions *export, Error **errp) 4056ee8626SKevin Wolf { 4156ee8626SKevin Wolf const BlockExportDriver *drv; 4256ee8626SKevin Wolf 4356ee8626SKevin Wolf drv = blk_exp_find_driver(export->type); 4456ee8626SKevin Wolf if (!drv) { 4556ee8626SKevin Wolf error_setg(errp, "No driver found for the requested export type"); 469b562c64SKevin Wolf return NULL; 4756ee8626SKevin Wolf } 4856ee8626SKevin Wolf 499b562c64SKevin Wolf return drv->create(export, errp); 509b562c64SKevin Wolf } 519b562c64SKevin Wolf 52*c69de1beSKevin Wolf void blk_exp_ref(BlockExport *exp) 53*c69de1beSKevin Wolf { 54*c69de1beSKevin Wolf assert(exp->refcount > 0); 55*c69de1beSKevin Wolf exp->refcount++; 56*c69de1beSKevin Wolf } 57*c69de1beSKevin Wolf 58*c69de1beSKevin Wolf void blk_exp_unref(BlockExport *exp) 59*c69de1beSKevin Wolf { 60*c69de1beSKevin Wolf assert(exp->refcount > 0); 61*c69de1beSKevin Wolf if (--exp->refcount == 0) { 62*c69de1beSKevin Wolf exp->drv->delete(exp); 63*c69de1beSKevin Wolf } 64*c69de1beSKevin Wolf } 65*c69de1beSKevin Wolf 669b562c64SKevin Wolf void qmp_block_export_add(BlockExportOptions *export, Error **errp) 679b562c64SKevin Wolf { 689b562c64SKevin Wolf blk_exp_add(export, errp); 6956ee8626SKevin Wolf } 70