]> git.proxmox.com Git - mirror_qemu.git/blame - block/block-backend.c
blockdev: Convert qmp_eject(), qmp_change_blockdev() to BlockBackend
[mirror_qemu.git] / block / block-backend.c
CommitLineData
26f54e9a
MA
1/*
2 * QEMU Block backends
3 *
4 * Copyright (C) 2014 Red Hat, Inc.
5 *
6 * Authors:
7 * Markus Armbruster <armbru@redhat.com>,
8 *
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.
11 */
12
13#include "sysemu/block-backend.h"
14#include "block/block_int.h"
18e46a03 15#include "sysemu/blockdev.h"
26f54e9a
MA
16
17struct BlockBackend {
18 char *name;
19 int refcnt;
7e7d56d9 20 BlockDriverState *bs;
26f8b3a8 21 DriveInfo *legacy_dinfo; /* null unless created by drive_new() */
26f54e9a
MA
22 QTAILQ_ENTRY(BlockBackend) link; /* for blk_backends */
23};
24
8fb3c76c
MA
25static void drive_info_del(DriveInfo *dinfo);
26
7e7d56d9 27/* All the BlockBackends (except for hidden ones) */
26f54e9a
MA
28static QTAILQ_HEAD(, BlockBackend) blk_backends =
29 QTAILQ_HEAD_INITIALIZER(blk_backends);
30
31/*
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.
37 */
38BlockBackend *blk_new(const char *name, Error **errp)
39{
40 BlockBackend *blk;
41
42 assert(name && name[0]);
7f06d47e
MA
43 if (!id_wellformed(name)) {
44 error_setg(errp, "Invalid device name");
45 return NULL;
46 }
26f54e9a
MA
47 if (blk_by_name(name)) {
48 error_setg(errp, "Device with id '%s' already exists", name);
49 return NULL;
50 }
7f06d47e
MA
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;
56 }
26f54e9a
MA
57
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;
63}
64
7e7d56d9
MA
65/*
66 * Create a new BlockBackend with a new BlockDriverState attached.
7e7d56d9
MA
67 * Otherwise just like blk_new(), which see.
68 */
69BlockBackend *blk_new_with_bs(const char *name, Error **errp)
70{
71 BlockBackend *blk;
72 BlockDriverState *bs;
73
74 blk = blk_new(name, errp);
75 if (!blk) {
76 return NULL;
77 }
78
7f06d47e 79 bs = bdrv_new_root();
7e7d56d9
MA
80 blk->bs = bs;
81 bs->blk = blk;
82 return blk;
83}
84
26f54e9a
MA
85static void blk_delete(BlockBackend *blk)
86{
87 assert(!blk->refcnt);
7e7d56d9 88 if (blk->bs) {
9ba10c95 89 assert(blk->bs->blk == blk);
7e7d56d9 90 blk->bs->blk = NULL;
9ba10c95 91 bdrv_unref(blk->bs);
7e7d56d9
MA
92 blk->bs = NULL;
93 }
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);
97 }
26f54e9a 98 g_free(blk->name);
18e46a03 99 drive_info_del(blk->legacy_dinfo);
26f54e9a
MA
100 g_free(blk);
101}
102
8fb3c76c
MA
103static void drive_info_del(DriveInfo *dinfo)
104{
105 if (!dinfo) {
106 return;
107 }
108 qemu_opts_del(dinfo->opts);
8fb3c76c
MA
109 g_free(dinfo->serial);
110 g_free(dinfo);
111}
112
26f54e9a
MA
113/*
114 * Increment @blk's reference count.
115 * @blk must not be null.
116 */
117void blk_ref(BlockBackend *blk)
118{
119 blk->refcnt++;
120}
121
122/*
123 * Decrement @blk's reference count.
124 * If this drops it to zero, destroy @blk.
125 * For convenience, do nothing if @blk is null.
126 */
127void blk_unref(BlockBackend *blk)
128{
129 if (blk) {
130 assert(blk->refcnt > 0);
131 if (!--blk->refcnt) {
132 blk_delete(blk);
133 }
134 }
135}
136
137/*
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.
141 *
142 * To iterate over all BlockBackends, do
143 * for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
144 * ...
145 * }
146 */
147BlockBackend *blk_next(BlockBackend *blk)
148{
149 return blk ? QTAILQ_NEXT(blk, link) : QTAILQ_FIRST(&blk_backends);
150}
151
152/*
7e7d56d9
MA
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().
26f54e9a
MA
156 */
157const char *blk_name(BlockBackend *blk)
158{
159 return blk->name;
160}
161
162/*
163 * Return the BlockBackend with name @name if it exists, else null.
164 * @name must not be null.
165 */
166BlockBackend *blk_by_name(const char *name)
167{
168 BlockBackend *blk;
169
170 assert(name);
171 QTAILQ_FOREACH(blk, &blk_backends, link) {
172 if (!strcmp(name, blk->name)) {
173 return blk;
174 }
175 }
176 return NULL;
177}
7e7d56d9
MA
178
179/*
180 * Return the BlockDriverState attached to @blk if any, else null.
181 */
182BlockDriverState *blk_bs(BlockBackend *blk)
183{
184 return blk->bs;
185}
186
18e46a03
MA
187/*
188 * Return @blk's DriveInfo if any, else null.
189 */
190DriveInfo *blk_legacy_dinfo(BlockBackend *blk)
191{
192 return blk->legacy_dinfo;
193}
194
195/*
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.
199 */
200DriveInfo *blk_set_legacy_dinfo(BlockBackend *blk, DriveInfo *dinfo)
201{
202 assert(!blk->legacy_dinfo);
203 return blk->legacy_dinfo = dinfo;
204}
205
206/*
207 * Return the BlockBackend with DriveInfo @dinfo.
208 * It must exist.
209 */
210BlockBackend *blk_by_legacy_dinfo(DriveInfo *dinfo)
211{
212 BlockBackend *blk;
213
214 QTAILQ_FOREACH(blk, &blk_backends, link) {
215 if (blk->legacy_dinfo == dinfo) {
216 return blk;
217 }
218 }
219 abort();
220}
221
7e7d56d9
MA
222/*
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!
230 */
231void blk_hide_on_behalf_of_do_drive_del(BlockBackend *blk)
232{
233 QTAILQ_REMOVE(&blk_backends, blk, link);
234 blk->name[0] = 0;
235 if (blk->bs) {
236 bdrv_make_anon(blk->bs);
237 }
238}
4be74634
MA
239
240void blk_iostatus_enable(BlockBackend *blk)
241{
242 bdrv_iostatus_enable(blk->bs);
243}
244
245int blk_attach_dev(BlockBackend *blk, void *dev)
246{
247 return bdrv_attach_dev(blk->bs, dev);
248}
249
250void blk_attach_dev_nofail(BlockBackend *blk, void *dev)
251{
252 bdrv_attach_dev_nofail(blk->bs, dev);
253}
254
255void blk_detach_dev(BlockBackend *blk, void *dev)
256{
257 bdrv_detach_dev(blk->bs, dev);
258}
259
260void *blk_get_attached_dev(BlockBackend *blk)
261{
262 return bdrv_get_attached_dev(blk->bs);
263}
264
265void blk_set_dev_ops(BlockBackend *blk, const BlockDevOps *ops, void *opaque)
266{
267 bdrv_set_dev_ops(blk->bs, ops, opaque);
268}
269
270int blk_read(BlockBackend *blk, int64_t sector_num, uint8_t *buf,
271 int nb_sectors)
272{
273 return bdrv_read(blk->bs, sector_num, buf, nb_sectors);
274}
275
276int blk_read_unthrottled(BlockBackend *blk, int64_t sector_num, uint8_t *buf,
277 int nb_sectors)
278{
279 return bdrv_read_unthrottled(blk->bs, sector_num, buf, nb_sectors);
280}
281
282int blk_write(BlockBackend *blk, int64_t sector_num, const uint8_t *buf,
283 int nb_sectors)
284{
285 return bdrv_write(blk->bs, sector_num, buf, nb_sectors);
286}
287
288BlockAIOCB *blk_aio_write_zeroes(BlockBackend *blk, int64_t sector_num,
289 int nb_sectors, BdrvRequestFlags flags,
290 BlockCompletionFunc *cb, void *opaque)
291{
292 return bdrv_aio_write_zeroes(blk->bs, sector_num, nb_sectors, flags,
293 cb, opaque);
294}
295
296int blk_pread(BlockBackend *blk, int64_t offset, void *buf, int count)
297{
298 return bdrv_pread(blk->bs, offset, buf, count);
299}
300
301int blk_pwrite(BlockBackend *blk, int64_t offset, const void *buf, int count)
302{
303 return bdrv_pwrite(blk->bs, offset, buf, count);
304}
305
306int64_t blk_getlength(BlockBackend *blk)
307{
308 return bdrv_getlength(blk->bs);
309}
310
311void blk_get_geometry(BlockBackend *blk, uint64_t *nb_sectors_ptr)
312{
313 bdrv_get_geometry(blk->bs, nb_sectors_ptr);
314}
315
316BlockAIOCB *blk_aio_readv(BlockBackend *blk, int64_t sector_num,
317 QEMUIOVector *iov, int nb_sectors,
318 BlockCompletionFunc *cb, void *opaque)
319{
320 return bdrv_aio_readv(blk->bs, sector_num, iov, nb_sectors, cb, opaque);
321}
322
323BlockAIOCB *blk_aio_writev(BlockBackend *blk, int64_t sector_num,
324 QEMUIOVector *iov, int nb_sectors,
325 BlockCompletionFunc *cb, void *opaque)
326{
327 return bdrv_aio_writev(blk->bs, sector_num, iov, nb_sectors, cb, opaque);
328}
329
330BlockAIOCB *blk_aio_flush(BlockBackend *blk,
331 BlockCompletionFunc *cb, void *opaque)
332{
333 return bdrv_aio_flush(blk->bs, cb, opaque);
334}
335
336BlockAIOCB *blk_aio_discard(BlockBackend *blk,
337 int64_t sector_num, int nb_sectors,
338 BlockCompletionFunc *cb, void *opaque)
339{
340 return bdrv_aio_discard(blk->bs, sector_num, nb_sectors, cb, opaque);
341}
342
343void blk_aio_cancel(BlockAIOCB *acb)
344{
345 bdrv_aio_cancel(acb);
346}
347
348void blk_aio_cancel_async(BlockAIOCB *acb)
349{
350 bdrv_aio_cancel_async(acb);
351}
352
353int blk_aio_multiwrite(BlockBackend *blk, BlockRequest *reqs, int num_reqs)
354{
355 return bdrv_aio_multiwrite(blk->bs, reqs, num_reqs);
356}
357
358int blk_ioctl(BlockBackend *blk, unsigned long int req, void *buf)
359{
360 return bdrv_ioctl(blk->bs, req, buf);
361}
362
363BlockAIOCB *blk_aio_ioctl(BlockBackend *blk, unsigned long int req, void *buf,
364 BlockCompletionFunc *cb, void *opaque)
365{
366 return bdrv_aio_ioctl(blk->bs, req, buf, cb, opaque);
367}
368
369int blk_flush(BlockBackend *blk)
370{
371 return bdrv_flush(blk->bs);
372}
373
374int blk_flush_all(void)
375{
376 return bdrv_flush_all();
377}
378
379void blk_drain_all(void)
380{
381 bdrv_drain_all();
382}
383
384BlockdevOnError blk_get_on_error(BlockBackend *blk, bool is_read)
385{
386 return bdrv_get_on_error(blk->bs, is_read);
387}
388
389BlockErrorAction blk_get_error_action(BlockBackend *blk, bool is_read,
390 int error)
391{
392 return bdrv_get_error_action(blk->bs, is_read, error);
393}
394
395void blk_error_action(BlockBackend *blk, BlockErrorAction action,
396 bool is_read, int error)
397{
398 bdrv_error_action(blk->bs, action, is_read, error);
399}
400
401int blk_is_read_only(BlockBackend *blk)
402{
403 return bdrv_is_read_only(blk->bs);
404}
405
406int blk_is_sg(BlockBackend *blk)
407{
408 return bdrv_is_sg(blk->bs);
409}
410
411int blk_enable_write_cache(BlockBackend *blk)
412{
413 return bdrv_enable_write_cache(blk->bs);
414}
415
416void blk_set_enable_write_cache(BlockBackend *blk, bool wce)
417{
418 bdrv_set_enable_write_cache(blk->bs, wce);
419}
420
421int blk_is_inserted(BlockBackend *blk)
422{
423 return bdrv_is_inserted(blk->bs);
424}
425
426void blk_lock_medium(BlockBackend *blk, bool locked)
427{
428 bdrv_lock_medium(blk->bs, locked);
429}
430
431void blk_eject(BlockBackend *blk, bool eject_flag)
432{
433 bdrv_eject(blk->bs, eject_flag);
434}
435
436int blk_get_flags(BlockBackend *blk)
437{
438 return bdrv_get_flags(blk->bs);
439}
440
441void blk_set_guest_block_size(BlockBackend *blk, int align)
442{
443 bdrv_set_guest_block_size(blk->bs, align);
444}
445
446void *blk_blockalign(BlockBackend *blk, size_t size)
447{
448 return qemu_blockalign(blk ? blk->bs : NULL, size);
449}
450
451bool blk_op_is_blocked(BlockBackend *blk, BlockOpType op, Error **errp)
452{
453 return bdrv_op_is_blocked(blk->bs, op, errp);
454}
455
456void blk_op_unblock(BlockBackend *blk, BlockOpType op, Error *reason)
457{
458 bdrv_op_unblock(blk->bs, op, reason);
459}
460
461void blk_op_block_all(BlockBackend *blk, Error *reason)
462{
463 bdrv_op_block_all(blk->bs, reason);
464}
465
466void blk_op_unblock_all(BlockBackend *blk, Error *reason)
467{
468 bdrv_op_unblock_all(blk->bs, reason);
469}
470
471AioContext *blk_get_aio_context(BlockBackend *blk)
472{
473 return bdrv_get_aio_context(blk->bs);
474}
475
476void blk_set_aio_context(BlockBackend *blk, AioContext *new_context)
477{
478 bdrv_set_aio_context(blk->bs, new_context);
479}
480
481void blk_io_plug(BlockBackend *blk)
482{
483 bdrv_io_plug(blk->bs);
484}
485
486void blk_io_unplug(BlockBackend *blk)
487{
488 bdrv_io_unplug(blk->bs);
489}
490
491BlockAcctStats *blk_get_stats(BlockBackend *blk)
492{
493 return bdrv_get_stats(blk->bs);
494}
495
496void *blk_aio_get(const AIOCBInfo *aiocb_info, BlockBackend *blk,
497 BlockCompletionFunc *cb, void *opaque)
498{
499 return qemu_aio_get(aiocb_info, blk_bs(blk), cb, opaque);
500}