xref: /openbmc/qemu/block/block-backend.c (revision e2462113)
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"
15 #include "sysemu/blockdev.h"
16 #include "qapi-event.h"
17 
18 /* Number of coroutines to reserve per attached device model */
19 #define COROUTINE_POOL_RESERVATION 64
20 
21 struct BlockBackend {
22     char *name;
23     int refcnt;
24     BlockDriverState *bs;
25     DriveInfo *legacy_dinfo;    /* null unless created by drive_new() */
26     QTAILQ_ENTRY(BlockBackend) link; /* for blk_backends */
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;
32 };
33 
34 static void drive_info_del(DriveInfo *dinfo);
35 
36 /* All the BlockBackends (except for hidden ones) */
37 static 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  */
47 BlockBackend *blk_new(const char *name, Error **errp)
48 {
49     BlockBackend *blk;
50 
51     assert(name && name[0]);
52     if (!id_wellformed(name)) {
53         error_setg(errp, "Invalid device name");
54         return NULL;
55     }
56     if (blk_by_name(name)) {
57         error_setg(errp, "Device with id '%s' already exists", name);
58         return NULL;
59     }
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     }
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 
74 /*
75  * Create a new BlockBackend with a new BlockDriverState attached.
76  * Otherwise just like blk_new(), which see.
77  */
78 BlockBackend *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 
88     bs = bdrv_new_root();
89     blk->bs = bs;
90     bs->blk = blk;
91     return blk;
92 }
93 
94 static void blk_delete(BlockBackend *blk)
95 {
96     assert(!blk->refcnt);
97     assert(!blk->dev);
98     if (blk->bs) {
99         assert(blk->bs->blk == blk);
100         blk->bs->blk = NULL;
101         bdrv_unref(blk->bs);
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     }
108     g_free(blk->name);
109     drive_info_del(blk->legacy_dinfo);
110     g_free(blk);
111 }
112 
113 static void drive_info_del(DriveInfo *dinfo)
114 {
115     if (!dinfo) {
116         return;
117     }
118     qemu_opts_del(dinfo->opts);
119     g_free(dinfo->serial);
120     g_free(dinfo);
121 }
122 
123 /*
124  * Increment @blk's reference count.
125  * @blk must not be null.
126  */
127 void 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  */
137 void 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  */
157 BlockBackend *blk_next(BlockBackend *blk)
158 {
159     return blk ? QTAILQ_NEXT(blk, link) : QTAILQ_FIRST(&blk_backends);
160 }
161 
162 /*
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().
166  */
167 const 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  */
176 BlockBackend *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 }
188 
189 /*
190  * Return the BlockDriverState attached to @blk if any, else null.
191  */
192 BlockDriverState *blk_bs(BlockBackend *blk)
193 {
194     return blk->bs;
195 }
196 
197 /*
198  * Return @blk's DriveInfo if any, else null.
199  */
200 DriveInfo *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  */
210 DriveInfo *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  */
220 BlockBackend *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 
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  */
241 void 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 }
249 
250 /*
251  * Attach device model @dev to @blk.
252  * Return 0 on success, -EBUSY when a device model is attached already.
253  */
254 int blk_attach_dev(BlockBackend *blk, void *dev)
255 /* TODO change to DeviceState *dev when all users are qdevified */
256 {
257     if (blk->dev) {
258         return -EBUSY;
259     }
260     blk_ref(blk);
261     blk->dev = dev;
262     bdrv_iostatus_reset(blk->bs);
263     return 0;
264 }
265 
266 /*
267  * Attach device model @dev to @blk.
268  * @blk must not have a device model attached already.
269  * TODO qdevified devices don't use this, remove when devices are qdevified
270  */
271 void blk_attach_dev_nofail(BlockBackend *blk, void *dev)
272 {
273     if (blk_attach_dev(blk, dev) < 0) {
274         abort();
275     }
276 }
277 
278 /*
279  * Detach device model @dev from @blk.
280  * @dev must be currently attached to @blk.
281  */
282 void blk_detach_dev(BlockBackend *blk, void *dev)
283 /* TODO change to DeviceState *dev when all users are qdevified */
284 {
285     assert(blk->dev == dev);
286     blk->dev = NULL;
287     blk->dev_ops = NULL;
288     blk->dev_opaque = NULL;
289     bdrv_set_guest_block_size(blk->bs, 512);
290     blk_unref(blk);
291 }
292 
293 /*
294  * Return the device model attached to @blk if any, else null.
295  */
296 void *blk_get_attached_dev(BlockBackend *blk)
297 /* TODO change to return DeviceState * when all users are qdevified */
298 {
299     return blk->dev;
300 }
301 
302 /*
303  * Set @blk's device model callbacks to @ops.
304  * @opaque is the opaque argument to pass to the callbacks.
305  * This is for use by device models.
306  */
307 void blk_set_dev_ops(BlockBackend *blk, const BlockDevOps *ops,
308                      void *opaque)
309 {
310     blk->dev_ops = ops;
311     blk->dev_opaque = opaque;
312 }
313 
314 /*
315  * Notify @blk's attached device model of media change.
316  * If @load is true, notify of media load.
317  * Else, notify of media eject.
318  * Also send DEVICE_TRAY_MOVED events as appropriate.
319  */
320 void blk_dev_change_media_cb(BlockBackend *blk, bool load)
321 {
322     if (blk->dev_ops && blk->dev_ops->change_media_cb) {
323         bool tray_was_closed = !blk_dev_is_tray_open(blk);
324 
325         blk->dev_ops->change_media_cb(blk->dev_opaque, load);
326         if (tray_was_closed) {
327             /* tray open */
328             qapi_event_send_device_tray_moved(blk_name(blk),
329                                               true, &error_abort);
330         }
331         if (load) {
332             /* tray close */
333             qapi_event_send_device_tray_moved(blk_name(blk),
334                                               false, &error_abort);
335         }
336     }
337 }
338 
339 /*
340  * Does @blk's attached device model have removable media?
341  * %true if no device model is attached.
342  */
343 bool blk_dev_has_removable_media(BlockBackend *blk)
344 {
345     return !blk->dev || (blk->dev_ops && blk->dev_ops->change_media_cb);
346 }
347 
348 /*
349  * Notify @blk's attached device model of a media eject request.
350  * If @force is true, the medium is about to be yanked out forcefully.
351  */
352 void blk_dev_eject_request(BlockBackend *blk, bool force)
353 {
354     if (blk->dev_ops && blk->dev_ops->eject_request_cb) {
355         blk->dev_ops->eject_request_cb(blk->dev_opaque, force);
356     }
357 }
358 
359 /*
360  * Does @blk's attached device model have a tray, and is it open?
361  */
362 bool blk_dev_is_tray_open(BlockBackend *blk)
363 {
364     if (blk->dev_ops && blk->dev_ops->is_tray_open) {
365         return blk->dev_ops->is_tray_open(blk->dev_opaque);
366     }
367     return false;
368 }
369 
370 /*
371  * Does @blk's attached device model have the medium locked?
372  * %false if the device model has no such lock.
373  */
374 bool blk_dev_is_medium_locked(BlockBackend *blk)
375 {
376     if (blk->dev_ops && blk->dev_ops->is_medium_locked) {
377         return blk->dev_ops->is_medium_locked(blk->dev_opaque);
378     }
379     return false;
380 }
381 
382 /*
383  * Notify @blk's attached device model of a backend size change.
384  */
385 void blk_dev_resize_cb(BlockBackend *blk)
386 {
387     if (blk->dev_ops && blk->dev_ops->resize_cb) {
388         blk->dev_ops->resize_cb(blk->dev_opaque);
389     }
390 }
391 
392 void blk_iostatus_enable(BlockBackend *blk)
393 {
394     bdrv_iostatus_enable(blk->bs);
395 }
396 
397 int blk_read(BlockBackend *blk, int64_t sector_num, uint8_t *buf,
398              int nb_sectors)
399 {
400     return bdrv_read(blk->bs, sector_num, buf, nb_sectors);
401 }
402 
403 int blk_read_unthrottled(BlockBackend *blk, int64_t sector_num, uint8_t *buf,
404                          int nb_sectors)
405 {
406     return bdrv_read_unthrottled(blk->bs, sector_num, buf, nb_sectors);
407 }
408 
409 int blk_write(BlockBackend *blk, int64_t sector_num, const uint8_t *buf,
410               int nb_sectors)
411 {
412     return bdrv_write(blk->bs, sector_num, buf, nb_sectors);
413 }
414 
415 BlockAIOCB *blk_aio_write_zeroes(BlockBackend *blk, int64_t sector_num,
416                                  int nb_sectors, BdrvRequestFlags flags,
417                                  BlockCompletionFunc *cb, void *opaque)
418 {
419     return bdrv_aio_write_zeroes(blk->bs, sector_num, nb_sectors, flags,
420                                  cb, opaque);
421 }
422 
423 int blk_pread(BlockBackend *blk, int64_t offset, void *buf, int count)
424 {
425     return bdrv_pread(blk->bs, offset, buf, count);
426 }
427 
428 int blk_pwrite(BlockBackend *blk, int64_t offset, const void *buf, int count)
429 {
430     return bdrv_pwrite(blk->bs, offset, buf, count);
431 }
432 
433 int64_t blk_getlength(BlockBackend *blk)
434 {
435     return bdrv_getlength(blk->bs);
436 }
437 
438 void blk_get_geometry(BlockBackend *blk, uint64_t *nb_sectors_ptr)
439 {
440     bdrv_get_geometry(blk->bs, nb_sectors_ptr);
441 }
442 
443 BlockAIOCB *blk_aio_readv(BlockBackend *blk, int64_t sector_num,
444                           QEMUIOVector *iov, int nb_sectors,
445                           BlockCompletionFunc *cb, void *opaque)
446 {
447     return bdrv_aio_readv(blk->bs, sector_num, iov, nb_sectors, cb, opaque);
448 }
449 
450 BlockAIOCB *blk_aio_writev(BlockBackend *blk, int64_t sector_num,
451                            QEMUIOVector *iov, int nb_sectors,
452                            BlockCompletionFunc *cb, void *opaque)
453 {
454     return bdrv_aio_writev(blk->bs, sector_num, iov, nb_sectors, cb, opaque);
455 }
456 
457 BlockAIOCB *blk_aio_flush(BlockBackend *blk,
458                           BlockCompletionFunc *cb, void *opaque)
459 {
460     return bdrv_aio_flush(blk->bs, cb, opaque);
461 }
462 
463 BlockAIOCB *blk_aio_discard(BlockBackend *blk,
464                             int64_t sector_num, int nb_sectors,
465                             BlockCompletionFunc *cb, void *opaque)
466 {
467     return bdrv_aio_discard(blk->bs, sector_num, nb_sectors, cb, opaque);
468 }
469 
470 void blk_aio_cancel(BlockAIOCB *acb)
471 {
472     bdrv_aio_cancel(acb);
473 }
474 
475 void blk_aio_cancel_async(BlockAIOCB *acb)
476 {
477     bdrv_aio_cancel_async(acb);
478 }
479 
480 int blk_aio_multiwrite(BlockBackend *blk, BlockRequest *reqs, int num_reqs)
481 {
482     return bdrv_aio_multiwrite(blk->bs, reqs, num_reqs);
483 }
484 
485 int blk_ioctl(BlockBackend *blk, unsigned long int req, void *buf)
486 {
487     return bdrv_ioctl(blk->bs, req, buf);
488 }
489 
490 BlockAIOCB *blk_aio_ioctl(BlockBackend *blk, unsigned long int req, void *buf,
491                           BlockCompletionFunc *cb, void *opaque)
492 {
493     return bdrv_aio_ioctl(blk->bs, req, buf, cb, opaque);
494 }
495 
496 int blk_co_discard(BlockBackend *blk, int64_t sector_num, int nb_sectors)
497 {
498     return bdrv_co_discard(blk->bs, sector_num, nb_sectors);
499 }
500 
501 int blk_co_flush(BlockBackend *blk)
502 {
503     return bdrv_co_flush(blk->bs);
504 }
505 
506 int blk_flush(BlockBackend *blk)
507 {
508     return bdrv_flush(blk->bs);
509 }
510 
511 int blk_flush_all(void)
512 {
513     return bdrv_flush_all();
514 }
515 
516 void blk_drain_all(void)
517 {
518     bdrv_drain_all();
519 }
520 
521 BlockdevOnError blk_get_on_error(BlockBackend *blk, bool is_read)
522 {
523     return bdrv_get_on_error(blk->bs, is_read);
524 }
525 
526 BlockErrorAction blk_get_error_action(BlockBackend *blk, bool is_read,
527                                       int error)
528 {
529     return bdrv_get_error_action(blk->bs, is_read, error);
530 }
531 
532 void blk_error_action(BlockBackend *blk, BlockErrorAction action,
533                       bool is_read, int error)
534 {
535     bdrv_error_action(blk->bs, action, is_read, error);
536 }
537 
538 int blk_is_read_only(BlockBackend *blk)
539 {
540     return bdrv_is_read_only(blk->bs);
541 }
542 
543 int blk_is_sg(BlockBackend *blk)
544 {
545     return bdrv_is_sg(blk->bs);
546 }
547 
548 int blk_enable_write_cache(BlockBackend *blk)
549 {
550     return bdrv_enable_write_cache(blk->bs);
551 }
552 
553 void blk_set_enable_write_cache(BlockBackend *blk, bool wce)
554 {
555     bdrv_set_enable_write_cache(blk->bs, wce);
556 }
557 
558 void blk_invalidate_cache(BlockBackend *blk, Error **errp)
559 {
560     bdrv_invalidate_cache(blk->bs, errp);
561 }
562 
563 int blk_is_inserted(BlockBackend *blk)
564 {
565     return bdrv_is_inserted(blk->bs);
566 }
567 
568 void blk_lock_medium(BlockBackend *blk, bool locked)
569 {
570     bdrv_lock_medium(blk->bs, locked);
571 }
572 
573 void blk_eject(BlockBackend *blk, bool eject_flag)
574 {
575     bdrv_eject(blk->bs, eject_flag);
576 }
577 
578 int blk_get_flags(BlockBackend *blk)
579 {
580     return bdrv_get_flags(blk->bs);
581 }
582 
583 int blk_get_max_transfer_length(BlockBackend *blk)
584 {
585     return blk->bs->bl.max_transfer_length;
586 }
587 
588 void blk_set_guest_block_size(BlockBackend *blk, int align)
589 {
590     bdrv_set_guest_block_size(blk->bs, align);
591 }
592 
593 void *blk_blockalign(BlockBackend *blk, size_t size)
594 {
595     return qemu_blockalign(blk ? blk->bs : NULL, size);
596 }
597 
598 bool blk_op_is_blocked(BlockBackend *blk, BlockOpType op, Error **errp)
599 {
600     return bdrv_op_is_blocked(blk->bs, op, errp);
601 }
602 
603 void blk_op_unblock(BlockBackend *blk, BlockOpType op, Error *reason)
604 {
605     bdrv_op_unblock(blk->bs, op, reason);
606 }
607 
608 void blk_op_block_all(BlockBackend *blk, Error *reason)
609 {
610     bdrv_op_block_all(blk->bs, reason);
611 }
612 
613 void blk_op_unblock_all(BlockBackend *blk, Error *reason)
614 {
615     bdrv_op_unblock_all(blk->bs, reason);
616 }
617 
618 AioContext *blk_get_aio_context(BlockBackend *blk)
619 {
620     return bdrv_get_aio_context(blk->bs);
621 }
622 
623 void blk_set_aio_context(BlockBackend *blk, AioContext *new_context)
624 {
625     bdrv_set_aio_context(blk->bs, new_context);
626 }
627 
628 void blk_add_aio_context_notifier(BlockBackend *blk,
629         void (*attached_aio_context)(AioContext *new_context, void *opaque),
630         void (*detach_aio_context)(void *opaque), void *opaque)
631 {
632     bdrv_add_aio_context_notifier(blk->bs, attached_aio_context,
633                                   detach_aio_context, opaque);
634 }
635 
636 void blk_remove_aio_context_notifier(BlockBackend *blk,
637                                      void (*attached_aio_context)(AioContext *,
638                                                                   void *),
639                                      void (*detach_aio_context)(void *),
640                                      void *opaque)
641 {
642     bdrv_remove_aio_context_notifier(blk->bs, attached_aio_context,
643                                      detach_aio_context, opaque);
644 }
645 
646 void blk_add_close_notifier(BlockBackend *blk, Notifier *notify)
647 {
648     bdrv_add_close_notifier(blk->bs, notify);
649 }
650 
651 void blk_io_plug(BlockBackend *blk)
652 {
653     bdrv_io_plug(blk->bs);
654 }
655 
656 void blk_io_unplug(BlockBackend *blk)
657 {
658     bdrv_io_unplug(blk->bs);
659 }
660 
661 BlockAcctStats *blk_get_stats(BlockBackend *blk)
662 {
663     return bdrv_get_stats(blk->bs);
664 }
665 
666 void *blk_aio_get(const AIOCBInfo *aiocb_info, BlockBackend *blk,
667                   BlockCompletionFunc *cb, void *opaque)
668 {
669     return qemu_aio_get(aiocb_info, blk_bs(blk), cb, opaque);
670 }
671