]> Git Repo - qemu.git/blame - block/block-backend.c
Merge remote-tracking branch 'remotes/kevin/tags/for-upstream' into staging
[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 <[email protected]>,
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"
a7f53e26
MA
16#include "qapi-event.h"
17
18/* Number of coroutines to reserve per attached device model */
19#define COROUTINE_POOL_RESERVATION 64
26f54e9a
MA
20
21struct BlockBackend {
22 char *name;
23 int refcnt;
7e7d56d9 24 BlockDriverState *bs;
26f8b3a8 25 DriveInfo *legacy_dinfo; /* null unless created by drive_new() */
26f54e9a 26 QTAILQ_ENTRY(BlockBackend) link; /* for blk_backends */
a7f53e26
MA
27
28 void *dev; /* attached device model, if any */
29 /* TODO change to DeviceState when all users are qdevified */
30 const BlockDevOps *dev_ops;
31 void *dev_opaque;
26f54e9a
MA
32};
33
8fb3c76c
MA
34static void drive_info_del(DriveInfo *dinfo);
35
7e7d56d9 36/* All the BlockBackends (except for hidden ones) */
26f54e9a
MA
37static QTAILQ_HEAD(, BlockBackend) blk_backends =
38 QTAILQ_HEAD_INITIALIZER(blk_backends);
39
40/*
41 * Create a new BlockBackend with @name, with a reference count of one.
42 * @name must not be null or empty.
43 * Fail if a BlockBackend with this name already exists.
44 * Store an error through @errp on failure, unless it's null.
45 * Return the new BlockBackend on success, null on failure.
46 */
47BlockBackend *blk_new(const char *name, Error **errp)
48{
49 BlockBackend *blk;
50
51 assert(name && name[0]);
7f06d47e
MA
52 if (!id_wellformed(name)) {
53 error_setg(errp, "Invalid device name");
54 return NULL;
55 }
26f54e9a
MA
56 if (blk_by_name(name)) {
57 error_setg(errp, "Device with id '%s' already exists", name);
58 return NULL;
59 }
7f06d47e
MA
60 if (bdrv_find_node(name)) {
61 error_setg(errp,
62 "Device name '%s' conflicts with an existing node name",
63 name);
64 return NULL;
65 }
26f54e9a
MA
66
67 blk = g_new0(BlockBackend, 1);
68 blk->name = g_strdup(name);
69 blk->refcnt = 1;
70 QTAILQ_INSERT_TAIL(&blk_backends, blk, link);
71 return blk;
72}
73
7e7d56d9
MA
74/*
75 * Create a new BlockBackend with a new BlockDriverState attached.
7e7d56d9
MA
76 * Otherwise just like blk_new(), which see.
77 */
78BlockBackend *blk_new_with_bs(const char *name, Error **errp)
79{
80 BlockBackend *blk;
81 BlockDriverState *bs;
82
83 blk = blk_new(name, errp);
84 if (!blk) {
85 return NULL;
86 }
87
7f06d47e 88 bs = bdrv_new_root();
7e7d56d9
MA
89 blk->bs = bs;
90 bs->blk = blk;
91 return blk;
92}
93
26f54e9a
MA
94static void blk_delete(BlockBackend *blk)
95{
96 assert(!blk->refcnt);
a7f53e26 97 assert(!blk->dev);
7e7d56d9 98 if (blk->bs) {
9ba10c95 99 assert(blk->bs->blk == blk);
7e7d56d9 100 blk->bs->blk = NULL;
9ba10c95 101 bdrv_unref(blk->bs);
7e7d56d9
MA
102 blk->bs = NULL;
103 }
104 /* Avoid double-remove after blk_hide_on_behalf_of_do_drive_del() */
105 if (blk->name[0]) {
106 QTAILQ_REMOVE(&blk_backends, blk, link);
107 }
26f54e9a 108 g_free(blk->name);
18e46a03 109 drive_info_del(blk->legacy_dinfo);
26f54e9a
MA
110 g_free(blk);
111}
112
8fb3c76c
MA
113static void drive_info_del(DriveInfo *dinfo)
114{
115 if (!dinfo) {
116 return;
117 }
118 qemu_opts_del(dinfo->opts);
8fb3c76c
MA
119 g_free(dinfo->serial);
120 g_free(dinfo);
121}
122
26f54e9a
MA
123/*
124 * Increment @blk's reference count.
125 * @blk must not be null.
126 */
127void blk_ref(BlockBackend *blk)
128{
129 blk->refcnt++;
130}
131
132/*
133 * Decrement @blk's reference count.
134 * If this drops it to zero, destroy @blk.
135 * For convenience, do nothing if @blk is null.
136 */
137void blk_unref(BlockBackend *blk)
138{
139 if (blk) {
140 assert(blk->refcnt > 0);
141 if (!--blk->refcnt) {
142 blk_delete(blk);
143 }
144 }
145}
146
147/*
148 * Return the BlockBackend after @blk.
149 * If @blk is null, return the first one.
150 * Else, return @blk's next sibling, which may be null.
151 *
152 * To iterate over all BlockBackends, do
153 * for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
154 * ...
155 * }
156 */
157BlockBackend *blk_next(BlockBackend *blk)
158{
159 return blk ? QTAILQ_NEXT(blk, link) : QTAILQ_FIRST(&blk_backends);
160}
161
162/*
7e7d56d9
MA
163 * Return @blk's name, a non-null string.
164 * Wart: the name is empty iff @blk has been hidden with
165 * blk_hide_on_behalf_of_do_drive_del().
26f54e9a
MA
166 */
167const char *blk_name(BlockBackend *blk)
168{
169 return blk->name;
170}
171
172/*
173 * Return the BlockBackend with name @name if it exists, else null.
174 * @name must not be null.
175 */
176BlockBackend *blk_by_name(const char *name)
177{
178 BlockBackend *blk;
179
180 assert(name);
181 QTAILQ_FOREACH(blk, &blk_backends, link) {
182 if (!strcmp(name, blk->name)) {
183 return blk;
184 }
185 }
186 return NULL;
187}
7e7d56d9
MA
188
189/*
190 * Return the BlockDriverState attached to @blk if any, else null.
191 */
192BlockDriverState *blk_bs(BlockBackend *blk)
193{
194 return blk->bs;
195}
196
18e46a03
MA
197/*
198 * Return @blk's DriveInfo if any, else null.
199 */
200DriveInfo *blk_legacy_dinfo(BlockBackend *blk)
201{
202 return blk->legacy_dinfo;
203}
204
205/*
206 * Set @blk's DriveInfo to @dinfo, and return it.
207 * @blk must not have a DriveInfo set already.
208 * No other BlockBackend may have the same DriveInfo set.
209 */
210DriveInfo *blk_set_legacy_dinfo(BlockBackend *blk, DriveInfo *dinfo)
211{
212 assert(!blk->legacy_dinfo);
213 return blk->legacy_dinfo = dinfo;
214}
215
216/*
217 * Return the BlockBackend with DriveInfo @dinfo.
218 * It must exist.
219 */
220BlockBackend *blk_by_legacy_dinfo(DriveInfo *dinfo)
221{
222 BlockBackend *blk;
223
224 QTAILQ_FOREACH(blk, &blk_backends, link) {
225 if (blk->legacy_dinfo == dinfo) {
226 return blk;
227 }
228 }
229 abort();
230}
231
7e7d56d9
MA
232/*
233 * Hide @blk.
234 * @blk must not have been hidden already.
235 * Make attached BlockDriverState, if any, anonymous.
236 * Once hidden, @blk is invisible to all functions that don't receive
237 * it as argument. For example, blk_by_name() won't return it.
238 * Strictly for use by do_drive_del().
239 * TODO get rid of it!
240 */
241void blk_hide_on_behalf_of_do_drive_del(BlockBackend *blk)
242{
243 QTAILQ_REMOVE(&blk_backends, blk, link);
244 blk->name[0] = 0;
245 if (blk->bs) {
246 bdrv_make_anon(blk->bs);
247 }
248}
4be74634 249
a7f53e26
MA
250/*
251 * Attach device model @dev to @blk.
252 * Return 0 on success, -EBUSY when a device model is attached already.
253 */
4be74634 254int blk_attach_dev(BlockBackend *blk, void *dev)
a7f53e26 255/* TODO change to DeviceState *dev when all users are qdevified */
4be74634 256{
a7f53e26
MA
257 if (blk->dev) {
258 return -EBUSY;
259 }
84ebe375 260 blk_ref(blk);
a7f53e26
MA
261 blk->dev = dev;
262 bdrv_iostatus_reset(blk->bs);
263
264 /* We're expecting I/O from the device so bump up coroutine pool size */
265 qemu_coroutine_adjust_pool_size(COROUTINE_POOL_RESERVATION);
266 return 0;
4be74634
MA
267}
268
a7f53e26
MA
269/*
270 * Attach device model @dev to @blk.
271 * @blk must not have a device model attached already.
272 * TODO qdevified devices don't use this, remove when devices are qdevified
273 */
4be74634
MA
274void blk_attach_dev_nofail(BlockBackend *blk, void *dev)
275{
a7f53e26
MA
276 if (blk_attach_dev(blk, dev) < 0) {
277 abort();
278 }
4be74634
MA
279}
280
a7f53e26
MA
281/*
282 * Detach device model @dev from @blk.
283 * @dev must be currently attached to @blk.
284 */
4be74634 285void blk_detach_dev(BlockBackend *blk, void *dev)
a7f53e26 286/* TODO change to DeviceState *dev when all users are qdevified */
4be74634 287{
a7f53e26
MA
288 assert(blk->dev == dev);
289 blk->dev = NULL;
290 blk->dev_ops = NULL;
291 blk->dev_opaque = NULL;
292 bdrv_set_guest_block_size(blk->bs, 512);
293 qemu_coroutine_adjust_pool_size(-COROUTINE_POOL_RESERVATION);
84ebe375 294 blk_unref(blk);
4be74634
MA
295}
296
a7f53e26
MA
297/*
298 * Return the device model attached to @blk if any, else null.
299 */
4be74634 300void *blk_get_attached_dev(BlockBackend *blk)
a7f53e26
MA
301/* TODO change to return DeviceState * when all users are qdevified */
302{
303 return blk->dev;
304}
305
306/*
307 * Set @blk's device model callbacks to @ops.
308 * @opaque is the opaque argument to pass to the callbacks.
309 * This is for use by device models.
310 */
311void blk_set_dev_ops(BlockBackend *blk, const BlockDevOps *ops,
312 void *opaque)
313{
314 blk->dev_ops = ops;
315 blk->dev_opaque = opaque;
316}
317
318/*
319 * Notify @blk's attached device model of media change.
320 * If @load is true, notify of media load.
321 * Else, notify of media eject.
322 * Also send DEVICE_TRAY_MOVED events as appropriate.
323 */
324void blk_dev_change_media_cb(BlockBackend *blk, bool load)
325{
326 if (blk->dev_ops && blk->dev_ops->change_media_cb) {
327 bool tray_was_closed = !blk_dev_is_tray_open(blk);
328
329 blk->dev_ops->change_media_cb(blk->dev_opaque, load);
330 if (tray_was_closed) {
331 /* tray open */
332 qapi_event_send_device_tray_moved(blk_name(blk),
333 true, &error_abort);
334 }
335 if (load) {
336 /* tray close */
337 qapi_event_send_device_tray_moved(blk_name(blk),
338 false, &error_abort);
339 }
340 }
341}
342
343/*
344 * Does @blk's attached device model have removable media?
345 * %true if no device model is attached.
346 */
347bool blk_dev_has_removable_media(BlockBackend *blk)
348{
349 return !blk->dev || (blk->dev_ops && blk->dev_ops->change_media_cb);
350}
351
352/*
353 * Notify @blk's attached device model of a media eject request.
354 * If @force is true, the medium is about to be yanked out forcefully.
355 */
356void blk_dev_eject_request(BlockBackend *blk, bool force)
4be74634 357{
a7f53e26
MA
358 if (blk->dev_ops && blk->dev_ops->eject_request_cb) {
359 blk->dev_ops->eject_request_cb(blk->dev_opaque, force);
360 }
4be74634
MA
361}
362
a7f53e26
MA
363/*
364 * Does @blk's attached device model have a tray, and is it open?
365 */
366bool blk_dev_is_tray_open(BlockBackend *blk)
4be74634 367{
a7f53e26
MA
368 if (blk->dev_ops && blk->dev_ops->is_tray_open) {
369 return blk->dev_ops->is_tray_open(blk->dev_opaque);
370 }
371 return false;
372}
373
374/*
375 * Does @blk's attached device model have the medium locked?
376 * %false if the device model has no such lock.
377 */
378bool blk_dev_is_medium_locked(BlockBackend *blk)
379{
380 if (blk->dev_ops && blk->dev_ops->is_medium_locked) {
381 return blk->dev_ops->is_medium_locked(blk->dev_opaque);
382 }
383 return false;
384}
385
386/*
387 * Notify @blk's attached device model of a backend size change.
388 */
389void blk_dev_resize_cb(BlockBackend *blk)
390{
391 if (blk->dev_ops && blk->dev_ops->resize_cb) {
392 blk->dev_ops->resize_cb(blk->dev_opaque);
393 }
394}
395
396void blk_iostatus_enable(BlockBackend *blk)
397{
398 bdrv_iostatus_enable(blk->bs);
4be74634
MA
399}
400
401int blk_read(BlockBackend *blk, int64_t sector_num, uint8_t *buf,
402 int nb_sectors)
403{
404 return bdrv_read(blk->bs, sector_num, buf, nb_sectors);
405}
406
407int blk_read_unthrottled(BlockBackend *blk, int64_t sector_num, uint8_t *buf,
408 int nb_sectors)
409{
410 return bdrv_read_unthrottled(blk->bs, sector_num, buf, nb_sectors);
411}
412
413int blk_write(BlockBackend *blk, int64_t sector_num, const uint8_t *buf,
414 int nb_sectors)
415{
416 return bdrv_write(blk->bs, sector_num, buf, nb_sectors);
417}
418
419BlockAIOCB *blk_aio_write_zeroes(BlockBackend *blk, int64_t sector_num,
420 int nb_sectors, BdrvRequestFlags flags,
421 BlockCompletionFunc *cb, void *opaque)
422{
423 return bdrv_aio_write_zeroes(blk->bs, sector_num, nb_sectors, flags,
424 cb, opaque);
425}
426
427int blk_pread(BlockBackend *blk, int64_t offset, void *buf, int count)
428{
429 return bdrv_pread(blk->bs, offset, buf, count);
430}
431
432int blk_pwrite(BlockBackend *blk, int64_t offset, const void *buf, int count)
433{
434 return bdrv_pwrite(blk->bs, offset, buf, count);
435}
436
437int64_t blk_getlength(BlockBackend *blk)
438{
439 return bdrv_getlength(blk->bs);
440}
441
442void blk_get_geometry(BlockBackend *blk, uint64_t *nb_sectors_ptr)
443{
444 bdrv_get_geometry(blk->bs, nb_sectors_ptr);
445}
446
447BlockAIOCB *blk_aio_readv(BlockBackend *blk, int64_t sector_num,
448 QEMUIOVector *iov, int nb_sectors,
449 BlockCompletionFunc *cb, void *opaque)
450{
451 return bdrv_aio_readv(blk->bs, sector_num, iov, nb_sectors, cb, opaque);
452}
453
454BlockAIOCB *blk_aio_writev(BlockBackend *blk, int64_t sector_num,
455 QEMUIOVector *iov, int nb_sectors,
456 BlockCompletionFunc *cb, void *opaque)
457{
458 return bdrv_aio_writev(blk->bs, sector_num, iov, nb_sectors, cb, opaque);
459}
460
461BlockAIOCB *blk_aio_flush(BlockBackend *blk,
462 BlockCompletionFunc *cb, void *opaque)
463{
464 return bdrv_aio_flush(blk->bs, cb, opaque);
465}
466
467BlockAIOCB *blk_aio_discard(BlockBackend *blk,
468 int64_t sector_num, int nb_sectors,
469 BlockCompletionFunc *cb, void *opaque)
470{
471 return bdrv_aio_discard(blk->bs, sector_num, nb_sectors, cb, opaque);
472}
473
474void blk_aio_cancel(BlockAIOCB *acb)
475{
476 bdrv_aio_cancel(acb);
477}
478
479void blk_aio_cancel_async(BlockAIOCB *acb)
480{
481 bdrv_aio_cancel_async(acb);
482}
483
484int blk_aio_multiwrite(BlockBackend *blk, BlockRequest *reqs, int num_reqs)
485{
486 return bdrv_aio_multiwrite(blk->bs, reqs, num_reqs);
487}
488
489int blk_ioctl(BlockBackend *blk, unsigned long int req, void *buf)
490{
491 return bdrv_ioctl(blk->bs, req, buf);
492}
493
494BlockAIOCB *blk_aio_ioctl(BlockBackend *blk, unsigned long int req, void *buf,
495 BlockCompletionFunc *cb, void *opaque)
496{
497 return bdrv_aio_ioctl(blk->bs, req, buf, cb, opaque);
498}
499
500int blk_flush(BlockBackend *blk)
501{
502 return bdrv_flush(blk->bs);
503}
504
505int blk_flush_all(void)
506{
507 return bdrv_flush_all();
508}
509
510void blk_drain_all(void)
511{
512 bdrv_drain_all();
513}
514
515BlockdevOnError blk_get_on_error(BlockBackend *blk, bool is_read)
516{
517 return bdrv_get_on_error(blk->bs, is_read);
518}
519
520BlockErrorAction blk_get_error_action(BlockBackend *blk, bool is_read,
521 int error)
522{
523 return bdrv_get_error_action(blk->bs, is_read, error);
524}
525
526void blk_error_action(BlockBackend *blk, BlockErrorAction action,
527 bool is_read, int error)
528{
529 bdrv_error_action(blk->bs, action, is_read, error);
530}
531
532int blk_is_read_only(BlockBackend *blk)
533{
534 return bdrv_is_read_only(blk->bs);
535}
536
537int blk_is_sg(BlockBackend *blk)
538{
539 return bdrv_is_sg(blk->bs);
540}
541
542int blk_enable_write_cache(BlockBackend *blk)
543{
544 return bdrv_enable_write_cache(blk->bs);
545}
546
547void blk_set_enable_write_cache(BlockBackend *blk, bool wce)
548{
549 bdrv_set_enable_write_cache(blk->bs, wce);
550}
551
552int blk_is_inserted(BlockBackend *blk)
553{
554 return bdrv_is_inserted(blk->bs);
555}
556
557void blk_lock_medium(BlockBackend *blk, bool locked)
558{
559 bdrv_lock_medium(blk->bs, locked);
560}
561
562void blk_eject(BlockBackend *blk, bool eject_flag)
563{
564 bdrv_eject(blk->bs, eject_flag);
565}
566
567int blk_get_flags(BlockBackend *blk)
568{
569 return bdrv_get_flags(blk->bs);
570}
571
572void blk_set_guest_block_size(BlockBackend *blk, int align)
573{
574 bdrv_set_guest_block_size(blk->bs, align);
575}
576
577void *blk_blockalign(BlockBackend *blk, size_t size)
578{
579 return qemu_blockalign(blk ? blk->bs : NULL, size);
580}
581
582bool blk_op_is_blocked(BlockBackend *blk, BlockOpType op, Error **errp)
583{
584 return bdrv_op_is_blocked(blk->bs, op, errp);
585}
586
587void blk_op_unblock(BlockBackend *blk, BlockOpType op, Error *reason)
588{
589 bdrv_op_unblock(blk->bs, op, reason);
590}
591
592void blk_op_block_all(BlockBackend *blk, Error *reason)
593{
594 bdrv_op_block_all(blk->bs, reason);
595}
596
597void blk_op_unblock_all(BlockBackend *blk, Error *reason)
598{
599 bdrv_op_unblock_all(blk->bs, reason);
600}
601
602AioContext *blk_get_aio_context(BlockBackend *blk)
603{
604 return bdrv_get_aio_context(blk->bs);
605}
606
607void blk_set_aio_context(BlockBackend *blk, AioContext *new_context)
608{
609 bdrv_set_aio_context(blk->bs, new_context);
610}
611
612void blk_io_plug(BlockBackend *blk)
613{
614 bdrv_io_plug(blk->bs);
615}
616
617void blk_io_unplug(BlockBackend *blk)
618{
619 bdrv_io_unplug(blk->bs);
620}
621
622BlockAcctStats *blk_get_stats(BlockBackend *blk)
623{
624 return bdrv_get_stats(blk->bs);
625}
626
627void *blk_aio_get(const AIOCBInfo *aiocb_info, BlockBackend *blk,
628 BlockCompletionFunc *cb, void *opaque)
629{
630 return qemu_aio_get(aiocb_info, blk_bs(blk), cb, opaque);
631}
This page took 0.090522 seconds and 4 git commands to generate.