block/export: Allocate BlockExport in blk_exp_add()
[qemu.git] / block / export / export.c
1 /*
2 * Common block export infrastructure
3 *
4 * Copyright (c) 2012, 2020 Red Hat, Inc.
5 *
6 * Authors:
7 * Paolo Bonzini <pbonzini@redhat.com>
8 * Kevin Wolf <kwolf@redhat.com>
9 *
10 * This work is licensed under the terms of the GNU GPL, version 2 or
11 * later. See the COPYING file in the top-level directory.
12 */
13
14 #include "qemu/osdep.h"
15
16 #include "block/block.h"
17 #include "sysemu/block-backend.h"
18 #include "block/export.h"
19 #include "block/nbd.h"
20 #include "qapi/error.h"
21 #include "qapi/qapi-commands-block-export.h"
22
23 static const BlockExportDriver *blk_exp_drivers[] = {
24 &blk_exp_nbd,
25 };
26
27 static const BlockExportDriver *blk_exp_find_driver(BlockExportType type)
28 {
29 int i;
30
31 for (i = 0; i < ARRAY_SIZE(blk_exp_drivers); i++) {
32 if (blk_exp_drivers[i]->type == type) {
33 return blk_exp_drivers[i];
34 }
35 }
36 return NULL;
37 }
38
39 BlockExport *blk_exp_add(BlockExportOptions *export, Error **errp)
40 {
41 const BlockExportDriver *drv;
42 BlockExport *exp;
43 int ret;
44
45 drv = blk_exp_find_driver(export->type);
46 if (!drv) {
47 error_setg(errp, "No driver found for the requested export type");
48 return NULL;
49 }
50
51 assert(drv->instance_size >= sizeof(BlockExport));
52 exp = g_malloc0(drv->instance_size);
53 *exp = (BlockExport) {
54 .drv = drv,
55 .refcount = 1,
56 };
57
58 ret = drv->create(exp, export, errp);
59 if (ret < 0) {
60 g_free(exp);
61 return NULL;
62 }
63
64 return exp;
65 }
66
67 /* Callers must hold exp->ctx lock */
68 void blk_exp_ref(BlockExport *exp)
69 {
70 assert(exp->refcount > 0);
71 exp->refcount++;
72 }
73
74 /* Callers must hold exp->ctx lock */
75 void blk_exp_unref(BlockExport *exp)
76 {
77 assert(exp->refcount > 0);
78 if (--exp->refcount == 0) {
79 exp->drv->delete(exp);
80 g_free(exp);
81 }
82 }
83
84 void qmp_block_export_add(BlockExportOptions *export, Error **errp)
85 {
86 blk_exp_add(export, errp);
87 }