]>
git.proxmox.com Git - mirror_qemu.git/blob - block/block-backend.c
fb91680f0365488ddcdfd66f6adc2d145e850eb2
4 * Copyright (C) 2014 Red Hat, Inc.
7 * Markus Armbruster <armbru@redhat.com>,
9 * This work is licensed under the terms of the GNU LGPL, version 2.1
10 * or later. See the COPYING.LIB file in the top-level directory.
13 #include "sysemu/block-backend.h"
14 #include "block/block_int.h"
15 #include "sysemu/blockdev.h"
21 DriveInfo
*legacy_dinfo
;
22 QTAILQ_ENTRY(BlockBackend
) link
; /* for blk_backends */
25 static void drive_info_del(DriveInfo
*dinfo
);
27 /* All the BlockBackends (except for hidden ones) */
28 static QTAILQ_HEAD(, BlockBackend
) blk_backends
=
29 QTAILQ_HEAD_INITIALIZER(blk_backends
);
32 * Create a new BlockBackend with @name, with a reference count of one.
33 * @name must not be null or empty.
34 * Fail if a BlockBackend with this name already exists.
35 * Store an error through @errp on failure, unless it's null.
36 * Return the new BlockBackend on success, null on failure.
38 BlockBackend
*blk_new(const char *name
, Error
**errp
)
42 assert(name
&& name
[0]);
43 if (!id_wellformed(name
)) {
44 error_setg(errp
, "Invalid device name");
47 if (blk_by_name(name
)) {
48 error_setg(errp
, "Device with id '%s' already exists", name
);
51 if (bdrv_find_node(name
)) {
53 "Device name '%s' conflicts with an existing node name",
58 blk
= g_new0(BlockBackend
, 1);
59 blk
->name
= g_strdup(name
);
61 QTAILQ_INSERT_TAIL(&blk_backends
, blk
, link
);
66 * Create a new BlockBackend with a new BlockDriverState attached.
67 * Otherwise just like blk_new(), which see.
69 BlockBackend
*blk_new_with_bs(const char *name
, Error
**errp
)
74 blk
= blk_new(name
, errp
);
85 static void blk_delete(BlockBackend
*blk
)
89 assert(blk
->bs
->blk
== blk
);
94 /* Avoid double-remove after blk_hide_on_behalf_of_do_drive_del() */
96 QTAILQ_REMOVE(&blk_backends
, blk
, link
);
99 drive_info_del(blk
->legacy_dinfo
);
103 static void drive_info_del(DriveInfo
*dinfo
)
108 qemu_opts_del(dinfo
->opts
);
110 g_free(dinfo
->serial
);
115 * Increment @blk's reference count.
116 * @blk must not be null.
118 void blk_ref(BlockBackend
*blk
)
124 * Decrement @blk's reference count.
125 * If this drops it to zero, destroy @blk.
126 * For convenience, do nothing if @blk is null.
128 void blk_unref(BlockBackend
*blk
)
131 assert(blk
->refcnt
> 0);
132 if (!--blk
->refcnt
) {
139 * Return the BlockBackend after @blk.
140 * If @blk is null, return the first one.
141 * Else, return @blk's next sibling, which may be null.
143 * To iterate over all BlockBackends, do
144 * for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
148 BlockBackend
*blk_next(BlockBackend
*blk
)
150 return blk
? QTAILQ_NEXT(blk
, link
) : QTAILQ_FIRST(&blk_backends
);
154 * Return @blk's name, a non-null string.
155 * Wart: the name is empty iff @blk has been hidden with
156 * blk_hide_on_behalf_of_do_drive_del().
158 const char *blk_name(BlockBackend
*blk
)
164 * Return the BlockBackend with name @name if it exists, else null.
165 * @name must not be null.
167 BlockBackend
*blk_by_name(const char *name
)
172 QTAILQ_FOREACH(blk
, &blk_backends
, link
) {
173 if (!strcmp(name
, blk
->name
)) {
181 * Return the BlockDriverState attached to @blk if any, else null.
183 BlockDriverState
*blk_bs(BlockBackend
*blk
)
189 * Return @blk's DriveInfo if any, else null.
191 DriveInfo
*blk_legacy_dinfo(BlockBackend
*blk
)
193 return blk
->legacy_dinfo
;
197 * Set @blk's DriveInfo to @dinfo, and return it.
198 * @blk must not have a DriveInfo set already.
199 * No other BlockBackend may have the same DriveInfo set.
201 DriveInfo
*blk_set_legacy_dinfo(BlockBackend
*blk
, DriveInfo
*dinfo
)
203 assert(!blk
->legacy_dinfo
);
204 return blk
->legacy_dinfo
= dinfo
;
208 * Return the BlockBackend with DriveInfo @dinfo.
211 BlockBackend
*blk_by_legacy_dinfo(DriveInfo
*dinfo
)
215 QTAILQ_FOREACH(blk
, &blk_backends
, link
) {
216 if (blk
->legacy_dinfo
== dinfo
) {
225 * @blk must not have been hidden already.
226 * Make attached BlockDriverState, if any, anonymous.
227 * Once hidden, @blk is invisible to all functions that don't receive
228 * it as argument. For example, blk_by_name() won't return it.
229 * Strictly for use by do_drive_del().
230 * TODO get rid of it!
232 void blk_hide_on_behalf_of_do_drive_del(BlockBackend
*blk
)
234 QTAILQ_REMOVE(&blk_backends
, blk
, link
);
237 bdrv_make_anon(blk
->bs
);