blockdev: Fix blockdev-add not to create DriveInfo
[qemu/kevin.git] / block / block-backend.c
blobae30873df13fc48abb1625014cd3558bf681274b
1 /*
2 * QEMU Block backends
4 * Copyright (C) 2014 Red Hat, Inc.
6 * Authors:
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"
17 struct BlockBackend {
18 char *name;
19 int refcnt;
20 BlockDriverState *bs;
21 DriveInfo *legacy_dinfo; /* null unless created by drive_new() */
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)
40 BlockBackend *blk;
42 assert(name && name[0]);
43 if (!id_wellformed(name)) {
44 error_setg(errp, "Invalid device name");
45 return NULL;
47 if (blk_by_name(name)) {
48 error_setg(errp, "Device with id '%s' already exists", name);
49 return NULL;
51 if (bdrv_find_node(name)) {
52 error_setg(errp,
53 "Device name '%s' conflicts with an existing node name",
54 name);
55 return NULL;
58 blk = g_new0(BlockBackend, 1);
59 blk->name = g_strdup(name);
60 blk->refcnt = 1;
61 QTAILQ_INSERT_TAIL(&blk_backends, blk, link);
62 return blk;
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)
71 BlockBackend *blk;
72 BlockDriverState *bs;
74 blk = blk_new(name, errp);
75 if (!blk) {
76 return NULL;
79 bs = bdrv_new_root();
80 blk->bs = bs;
81 bs->blk = blk;
82 return blk;
85 static void blk_delete(BlockBackend *blk)
87 assert(!blk->refcnt);
88 if (blk->bs) {
89 assert(blk->bs->blk == blk);
90 blk->bs->blk = NULL;
91 bdrv_unref(blk->bs);
92 blk->bs = NULL;
94 /* Avoid double-remove after blk_hide_on_behalf_of_do_drive_del() */
95 if (blk->name[0]) {
96 QTAILQ_REMOVE(&blk_backends, blk, link);
98 g_free(blk->name);
99 drive_info_del(blk->legacy_dinfo);
100 g_free(blk);
103 static void drive_info_del(DriveInfo *dinfo)
105 if (!dinfo) {
106 return;
108 qemu_opts_del(dinfo->opts);
109 g_free(dinfo->serial);
110 g_free(dinfo);
114 * Increment @blk's reference count.
115 * @blk must not be null.
117 void blk_ref(BlockBackend *blk)
119 blk->refcnt++;
123 * Decrement @blk's reference count.
124 * If this drops it to zero, destroy @blk.
125 * For convenience, do nothing if @blk is null.
127 void blk_unref(BlockBackend *blk)
129 if (blk) {
130 assert(blk->refcnt > 0);
131 if (!--blk->refcnt) {
132 blk_delete(blk);
138 * Return the BlockBackend after @blk.
139 * If @blk is null, return the first one.
140 * Else, return @blk's next sibling, which may be null.
142 * To iterate over all BlockBackends, do
143 * for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
144 * ...
147 BlockBackend *blk_next(BlockBackend *blk)
149 return blk ? QTAILQ_NEXT(blk, link) : QTAILQ_FIRST(&blk_backends);
153 * Return @blk's name, a non-null string.
154 * Wart: the name is empty iff @blk has been hidden with
155 * blk_hide_on_behalf_of_do_drive_del().
157 const char *blk_name(BlockBackend *blk)
159 return blk->name;
163 * Return the BlockBackend with name @name if it exists, else null.
164 * @name must not be null.
166 BlockBackend *blk_by_name(const char *name)
168 BlockBackend *blk;
170 assert(name);
171 QTAILQ_FOREACH(blk, &blk_backends, link) {
172 if (!strcmp(name, blk->name)) {
173 return blk;
176 return NULL;
180 * Return the BlockDriverState attached to @blk if any, else null.
182 BlockDriverState *blk_bs(BlockBackend *blk)
184 return blk->bs;
188 * Return @blk's DriveInfo if any, else null.
190 DriveInfo *blk_legacy_dinfo(BlockBackend *blk)
192 return blk->legacy_dinfo;
196 * Set @blk's DriveInfo to @dinfo, and return it.
197 * @blk must not have a DriveInfo set already.
198 * No other BlockBackend may have the same DriveInfo set.
200 DriveInfo *blk_set_legacy_dinfo(BlockBackend *blk, DriveInfo *dinfo)
202 assert(!blk->legacy_dinfo);
203 return blk->legacy_dinfo = dinfo;
207 * Return the BlockBackend with DriveInfo @dinfo.
208 * It must exist.
210 BlockBackend *blk_by_legacy_dinfo(DriveInfo *dinfo)
212 BlockBackend *blk;
214 QTAILQ_FOREACH(blk, &blk_backends, link) {
215 if (blk->legacy_dinfo == dinfo) {
216 return blk;
219 abort();
223 * Hide @blk.
224 * @blk must not have been hidden already.
225 * Make attached BlockDriverState, if any, anonymous.
226 * Once hidden, @blk is invisible to all functions that don't receive
227 * it as argument. For example, blk_by_name() won't return it.
228 * Strictly for use by do_drive_del().
229 * TODO get rid of it!
231 void blk_hide_on_behalf_of_do_drive_del(BlockBackend *blk)
233 QTAILQ_REMOVE(&blk_backends, blk, link);
234 blk->name[0] = 0;
235 if (blk->bs) {
236 bdrv_make_anon(blk->bs);
240 void blk_iostatus_enable(BlockBackend *blk)
242 bdrv_iostatus_enable(blk->bs);
245 int blk_attach_dev(BlockBackend *blk, void *dev)
247 return bdrv_attach_dev(blk->bs, dev);
250 void blk_attach_dev_nofail(BlockBackend *blk, void *dev)
252 bdrv_attach_dev_nofail(blk->bs, dev);
255 void blk_detach_dev(BlockBackend *blk, void *dev)
257 bdrv_detach_dev(blk->bs, dev);
260 void *blk_get_attached_dev(BlockBackend *blk)
262 return bdrv_get_attached_dev(blk->bs);
265 void blk_set_dev_ops(BlockBackend *blk, const BlockDevOps *ops, void *opaque)
267 bdrv_set_dev_ops(blk->bs, ops, opaque);
270 int blk_read(BlockBackend *blk, int64_t sector_num, uint8_t *buf,
271 int nb_sectors)
273 return bdrv_read(blk->bs, sector_num, buf, nb_sectors);
276 int blk_read_unthrottled(BlockBackend *blk, int64_t sector_num, uint8_t *buf,
277 int nb_sectors)
279 return bdrv_read_unthrottled(blk->bs, sector_num, buf, nb_sectors);
282 int blk_write(BlockBackend *blk, int64_t sector_num, const uint8_t *buf,
283 int nb_sectors)
285 return bdrv_write(blk->bs, sector_num, buf, nb_sectors);
288 BlockAIOCB *blk_aio_write_zeroes(BlockBackend *blk, int64_t sector_num,
289 int nb_sectors, BdrvRequestFlags flags,
290 BlockCompletionFunc *cb, void *opaque)
292 return bdrv_aio_write_zeroes(blk->bs, sector_num, nb_sectors, flags,
293 cb, opaque);
296 int blk_pread(BlockBackend *blk, int64_t offset, void *buf, int count)
298 return bdrv_pread(blk->bs, offset, buf, count);
301 int blk_pwrite(BlockBackend *blk, int64_t offset, const void *buf, int count)
303 return bdrv_pwrite(blk->bs, offset, buf, count);
306 int64_t blk_getlength(BlockBackend *blk)
308 return bdrv_getlength(blk->bs);
311 void blk_get_geometry(BlockBackend *blk, uint64_t *nb_sectors_ptr)
313 bdrv_get_geometry(blk->bs, nb_sectors_ptr);
316 BlockAIOCB *blk_aio_readv(BlockBackend *blk, int64_t sector_num,
317 QEMUIOVector *iov, int nb_sectors,
318 BlockCompletionFunc *cb, void *opaque)
320 return bdrv_aio_readv(blk->bs, sector_num, iov, nb_sectors, cb, opaque);
323 BlockAIOCB *blk_aio_writev(BlockBackend *blk, int64_t sector_num,
324 QEMUIOVector *iov, int nb_sectors,
325 BlockCompletionFunc *cb, void *opaque)
327 return bdrv_aio_writev(blk->bs, sector_num, iov, nb_sectors, cb, opaque);
330 BlockAIOCB *blk_aio_flush(BlockBackend *blk,
331 BlockCompletionFunc *cb, void *opaque)
333 return bdrv_aio_flush(blk->bs, cb, opaque);
336 BlockAIOCB *blk_aio_discard(BlockBackend *blk,
337 int64_t sector_num, int nb_sectors,
338 BlockCompletionFunc *cb, void *opaque)
340 return bdrv_aio_discard(blk->bs, sector_num, nb_sectors, cb, opaque);
343 void blk_aio_cancel(BlockAIOCB *acb)
345 bdrv_aio_cancel(acb);
348 void blk_aio_cancel_async(BlockAIOCB *acb)
350 bdrv_aio_cancel_async(acb);
353 int blk_aio_multiwrite(BlockBackend *blk, BlockRequest *reqs, int num_reqs)
355 return bdrv_aio_multiwrite(blk->bs, reqs, num_reqs);
358 int blk_ioctl(BlockBackend *blk, unsigned long int req, void *buf)
360 return bdrv_ioctl(blk->bs, req, buf);
363 BlockAIOCB *blk_aio_ioctl(BlockBackend *blk, unsigned long int req, void *buf,
364 BlockCompletionFunc *cb, void *opaque)
366 return bdrv_aio_ioctl(blk->bs, req, buf, cb, opaque);
369 int blk_flush(BlockBackend *blk)
371 return bdrv_flush(blk->bs);
374 int blk_flush_all(void)
376 return bdrv_flush_all();
379 void blk_drain_all(void)
381 bdrv_drain_all();
384 BlockdevOnError blk_get_on_error(BlockBackend *blk, bool is_read)
386 return bdrv_get_on_error(blk->bs, is_read);
389 BlockErrorAction blk_get_error_action(BlockBackend *blk, bool is_read,
390 int error)
392 return bdrv_get_error_action(blk->bs, is_read, error);
395 void blk_error_action(BlockBackend *blk, BlockErrorAction action,
396 bool is_read, int error)
398 bdrv_error_action(blk->bs, action, is_read, error);
401 int blk_is_read_only(BlockBackend *blk)
403 return bdrv_is_read_only(blk->bs);
406 int blk_is_sg(BlockBackend *blk)
408 return bdrv_is_sg(blk->bs);
411 int blk_enable_write_cache(BlockBackend *blk)
413 return bdrv_enable_write_cache(blk->bs);
416 void blk_set_enable_write_cache(BlockBackend *blk, bool wce)
418 bdrv_set_enable_write_cache(blk->bs, wce);
421 int blk_is_inserted(BlockBackend *blk)
423 return bdrv_is_inserted(blk->bs);
426 void blk_lock_medium(BlockBackend *blk, bool locked)
428 bdrv_lock_medium(blk->bs, locked);
431 void blk_eject(BlockBackend *blk, bool eject_flag)
433 bdrv_eject(blk->bs, eject_flag);
436 int blk_get_flags(BlockBackend *blk)
438 return bdrv_get_flags(blk->bs);
441 void blk_set_guest_block_size(BlockBackend *blk, int align)
443 bdrv_set_guest_block_size(blk->bs, align);
446 void *blk_blockalign(BlockBackend *blk, size_t size)
448 return qemu_blockalign(blk ? blk->bs : NULL, size);
451 bool blk_op_is_blocked(BlockBackend *blk, BlockOpType op, Error **errp)
453 return bdrv_op_is_blocked(blk->bs, op, errp);
456 void blk_op_unblock(BlockBackend *blk, BlockOpType op, Error *reason)
458 bdrv_op_unblock(blk->bs, op, reason);
461 void blk_op_block_all(BlockBackend *blk, Error *reason)
463 bdrv_op_block_all(blk->bs, reason);
466 void blk_op_unblock_all(BlockBackend *blk, Error *reason)
468 bdrv_op_unblock_all(blk->bs, reason);
471 AioContext *blk_get_aio_context(BlockBackend *blk)
473 return bdrv_get_aio_context(blk->bs);
476 void blk_set_aio_context(BlockBackend *blk, AioContext *new_context)
478 bdrv_set_aio_context(blk->bs, new_context);
481 void blk_io_plug(BlockBackend *blk)
483 bdrv_io_plug(blk->bs);
486 void blk_io_unplug(BlockBackend *blk)
488 bdrv_io_unplug(blk->bs);
491 BlockAcctStats *blk_get_stats(BlockBackend *blk)
493 return bdrv_get_stats(blk->bs);
496 void *blk_aio_get(const AIOCBInfo *aiocb_info, BlockBackend *blk,
497 BlockCompletionFunc *cb, void *opaque)
499 return qemu_aio_get(aiocb_info, blk_bs(blk), cb, opaque);