xref: /openbmc/qemu/block/export/export.c (revision 8612c6867341e52416e63fce8b19ede8501c159a)
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*8612c686SKevin Wolf /* Callers must hold exp->ctx lock */
53c69de1beSKevin Wolf void blk_exp_ref(BlockExport *exp)
54c69de1beSKevin Wolf {
55c69de1beSKevin Wolf     assert(exp->refcount > 0);
56c69de1beSKevin Wolf     exp->refcount++;
57c69de1beSKevin Wolf }
58c69de1beSKevin Wolf 
59*8612c686SKevin Wolf /* Callers must hold exp->ctx lock */
60c69de1beSKevin Wolf void blk_exp_unref(BlockExport *exp)
61c69de1beSKevin Wolf {
62c69de1beSKevin Wolf     assert(exp->refcount > 0);
63c69de1beSKevin Wolf     if (--exp->refcount == 0) {
64c69de1beSKevin Wolf         exp->drv->delete(exp);
65c69de1beSKevin Wolf     }
66c69de1beSKevin Wolf }
67c69de1beSKevin Wolf 
689b562c64SKevin Wolf void qmp_block_export_add(BlockExportOptions *export, Error **errp)
699b562c64SKevin Wolf {
709b562c64SKevin Wolf     blk_exp_add(export, errp);
7156ee8626SKevin Wolf }
72