qemu/include/block/block-global-state.h
<<
>>
Prefs
   1/*
   2 * QEMU System Emulator block driver
   3 *
   4 * Copyright (c) 2003 Fabrice Bellard
   5 *
   6 * Permission is hereby granted, free of charge, to any person obtaining a copy
   7 * of this software and associated documentation files (the "Software"), to deal
   8 * in the Software without restriction, including without limitation the rights
   9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10 * copies of the Software, and to permit persons to whom the Software is
  11 * furnished to do so, subject to the following conditions:
  12 *
  13 * The above copyright notice and this permission notice shall be included in
  14 * all copies or substantial portions of the Software.
  15 *
  16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22 * THE SOFTWARE.
  23 */
  24#ifndef BLOCK_GLOBAL_STATE_H
  25#define BLOCK_GLOBAL_STATE_H
  26
  27#include "block-common.h"
  28
  29/*
  30 * Global state (GS) API. These functions run under the BQL.
  31 *
  32 * If a function modifies the graph, it also uses drain and/or
  33 * aio_context_acquire/release to be sure it has unique access.
  34 * aio_context locking is needed together with BQL because of
  35 * the thread-safe I/O API that concurrently runs and accesses
  36 * the graph without the BQL.
  37 *
  38 * It is important to note that not all of these functions are
  39 * necessarily limited to running under the BQL, but they would
  40 * require additional auditing and many small thread-safety changes
  41 * to move them into the I/O API. Often it's not worth doing that
  42 * work since the APIs are only used with the BQL held at the
  43 * moment, so they have been placed in the GS API (for now).
  44 *
  45 * These functions can call any function from this and other categories
  46 * (I/O, "I/O or GS", Common), but must be invoked only by other GS APIs.
  47 *
  48 * All functions in this header must use the macro
  49 * GLOBAL_STATE_CODE();
  50 * to catch when they are accidentally called without the BQL.
  51 */
  52
  53void bdrv_init(void);
  54BlockDriver *bdrv_find_protocol(const char *filename,
  55                                bool allow_protocol_prefix,
  56                                Error **errp);
  57BlockDriver *bdrv_find_format(const char *format_name);
  58int bdrv_create(BlockDriver *drv, const char* filename,
  59                QemuOpts *opts, Error **errp);
  60int bdrv_create_file(const char *filename, QemuOpts *opts, Error **errp);
  61
  62BlockDriverState *bdrv_new(void);
  63int bdrv_append(BlockDriverState *bs_new, BlockDriverState *bs_top,
  64                Error **errp);
  65int bdrv_replace_node(BlockDriverState *from, BlockDriverState *to,
  66                      Error **errp);
  67int bdrv_replace_child_bs(BdrvChild *child, BlockDriverState *new_bs,
  68                          Error **errp);
  69BlockDriverState *bdrv_insert_node(BlockDriverState *bs, QDict *node_options,
  70                                   int flags, Error **errp);
  71int bdrv_drop_filter(BlockDriverState *bs, Error **errp);
  72
  73BdrvChild *bdrv_open_child(const char *filename,
  74                           QDict *options, const char *bdref_key,
  75                           BlockDriverState *parent,
  76                           const BdrvChildClass *child_class,
  77                           BdrvChildRole child_role,
  78                           bool allow_none, Error **errp);
  79BlockDriverState *bdrv_open_blockdev_ref(BlockdevRef *ref, Error **errp);
  80int bdrv_set_backing_hd(BlockDriverState *bs, BlockDriverState *backing_hd,
  81                        Error **errp);
  82int bdrv_open_backing_file(BlockDriverState *bs, QDict *parent_options,
  83                           const char *bdref_key, Error **errp);
  84BlockDriverState *bdrv_open(const char *filename, const char *reference,
  85                            QDict *options, int flags, Error **errp);
  86BlockDriverState *bdrv_new_open_driver_opts(BlockDriver *drv,
  87                                            const char *node_name,
  88                                            QDict *options, int flags,
  89                                            Error **errp);
  90BlockDriverState *bdrv_new_open_driver(BlockDriver *drv, const char *node_name,
  91                                       int flags, Error **errp);
  92BlockReopenQueue *bdrv_reopen_queue(BlockReopenQueue *bs_queue,
  93                                    BlockDriverState *bs, QDict *options,
  94                                    bool keep_old_opts);
  95void bdrv_reopen_queue_free(BlockReopenQueue *bs_queue);
  96int bdrv_reopen_multiple(BlockReopenQueue *bs_queue, Error **errp);
  97int bdrv_reopen(BlockDriverState *bs, QDict *opts, bool keep_old_opts,
  98                Error **errp);
  99int bdrv_reopen_set_read_only(BlockDriverState *bs, bool read_only,
 100                              Error **errp);
 101BlockDriverState *bdrv_find_backing_image(BlockDriverState *bs,
 102                                          const char *backing_file);
 103void bdrv_refresh_filename(BlockDriverState *bs);
 104void bdrv_refresh_limits(BlockDriverState *bs, Transaction *tran, Error **errp);
 105int bdrv_commit(BlockDriverState *bs);
 106int bdrv_make_empty(BdrvChild *c, Error **errp);
 107int bdrv_change_backing_file(BlockDriverState *bs, const char *backing_file,
 108                             const char *backing_fmt, bool warn);
 109void bdrv_register(BlockDriver *bdrv);
 110int bdrv_drop_intermediate(BlockDriverState *top, BlockDriverState *base,
 111                           const char *backing_file_str);
 112BlockDriverState *bdrv_find_overlay(BlockDriverState *active,
 113                                    BlockDriverState *bs);
 114BlockDriverState *bdrv_find_base(BlockDriverState *bs);
 115bool bdrv_is_backing_chain_frozen(BlockDriverState *bs, BlockDriverState *base,
 116                                  Error **errp);
 117int bdrv_freeze_backing_chain(BlockDriverState *bs, BlockDriverState *base,
 118                              Error **errp);
 119void bdrv_unfreeze_backing_chain(BlockDriverState *bs, BlockDriverState *base);
 120
 121/*
 122 * The units of offset and total_work_size may be chosen arbitrarily by the
 123 * block driver; total_work_size may change during the course of the amendment
 124 * operation
 125 */
 126typedef void BlockDriverAmendStatusCB(BlockDriverState *bs, int64_t offset,
 127                                      int64_t total_work_size, void *opaque);
 128int bdrv_amend_options(BlockDriverState *bs_new, QemuOpts *opts,
 129                       BlockDriverAmendStatusCB *status_cb, void *cb_opaque,
 130                       bool force,
 131                       Error **errp);
 132
 133/* check if a named node can be replaced when doing drive-mirror */
 134BlockDriverState *check_to_replace_node(BlockDriverState *parent_bs,
 135                                        const char *node_name, Error **errp);
 136
 137int bdrv_activate(BlockDriverState *bs, Error **errp);
 138void bdrv_activate_all(Error **errp);
 139int bdrv_inactivate_all(void);
 140
 141int bdrv_flush_all(void);
 142void bdrv_close_all(void);
 143void bdrv_drain_all_begin(void);
 144void bdrv_drain_all_end(void);
 145void bdrv_drain_all(void);
 146
 147int bdrv_has_zero_init_1(BlockDriverState *bs);
 148int bdrv_has_zero_init(BlockDriverState *bs);
 149BlockDriverState *bdrv_find_node(const char *node_name);
 150BlockDeviceInfoList *bdrv_named_nodes_list(bool flat, Error **errp);
 151XDbgBlockGraph *bdrv_get_xdbg_block_graph(Error **errp);
 152BlockDriverState *bdrv_lookup_bs(const char *device,
 153                                 const char *node_name,
 154                                 Error **errp);
 155bool bdrv_chain_contains(BlockDriverState *top, BlockDriverState *base);
 156BlockDriverState *bdrv_next_node(BlockDriverState *bs);
 157BlockDriverState *bdrv_next_all_states(BlockDriverState *bs);
 158
 159typedef struct BdrvNextIterator {
 160    enum {
 161        BDRV_NEXT_BACKEND_ROOTS,
 162        BDRV_NEXT_MONITOR_OWNED,
 163    } phase;
 164    BlockBackend *blk;
 165    BlockDriverState *bs;
 166} BdrvNextIterator;
 167
 168BlockDriverState *bdrv_first(BdrvNextIterator *it);
 169BlockDriverState *bdrv_next(BdrvNextIterator *it);
 170void bdrv_next_cleanup(BdrvNextIterator *it);
 171
 172BlockDriverState *bdrv_next_monitor_owned(BlockDriverState *bs);
 173void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
 174                         void *opaque, bool read_only);
 175int bdrv_get_flags(BlockDriverState *bs);
 176char *bdrv_get_full_backing_filename(BlockDriverState *bs, Error **errp);
 177char *bdrv_dirname(BlockDriverState *bs, Error **errp);
 178
 179void bdrv_img_create(const char *filename, const char *fmt,
 180                     const char *base_filename, const char *base_fmt,
 181                     char *options, uint64_t img_size, int flags,
 182                     bool quiet, Error **errp);
 183
 184void bdrv_ref(BlockDriverState *bs);
 185void bdrv_unref(BlockDriverState *bs);
 186void bdrv_unref_child(BlockDriverState *parent, BdrvChild *child);
 187BdrvChild *bdrv_attach_child(BlockDriverState *parent_bs,
 188                             BlockDriverState *child_bs,
 189                             const char *child_name,
 190                             const BdrvChildClass *child_class,
 191                             BdrvChildRole child_role,
 192                             Error **errp);
 193
 194bool bdrv_op_is_blocked(BlockDriverState *bs, BlockOpType op, Error **errp);
 195void bdrv_op_block(BlockDriverState *bs, BlockOpType op, Error *reason);
 196void bdrv_op_unblock(BlockDriverState *bs, BlockOpType op, Error *reason);
 197void bdrv_op_block_all(BlockDriverState *bs, Error *reason);
 198void bdrv_op_unblock_all(BlockDriverState *bs, Error *reason);
 199bool bdrv_op_blocker_is_empty(BlockDriverState *bs);
 200
 201int bdrv_debug_breakpoint(BlockDriverState *bs, const char *event,
 202                           const char *tag);
 203int bdrv_debug_remove_breakpoint(BlockDriverState *bs, const char *tag);
 204int bdrv_debug_resume(BlockDriverState *bs, const char *tag);
 205bool bdrv_debug_is_suspended(BlockDriverState *bs, const char *tag);
 206
 207/**
 208 * Locks the AioContext of @bs if it's not the current AioContext. This avoids
 209 * double locking which could lead to deadlocks: This is a coroutine_fn, so we
 210 * know we already own the lock of the current AioContext.
 211 *
 212 * May only be called in the main thread.
 213 */
 214void coroutine_fn bdrv_co_lock(BlockDriverState *bs);
 215
 216/**
 217 * Unlocks the AioContext of @bs if it's not the current AioContext.
 218 */
 219void coroutine_fn bdrv_co_unlock(BlockDriverState *bs);
 220
 221void bdrv_set_aio_context_ignore(BlockDriverState *bs,
 222                                 AioContext *new_context, GSList **ignore);
 223int bdrv_try_set_aio_context(BlockDriverState *bs, AioContext *ctx,
 224                             Error **errp);
 225int bdrv_child_try_set_aio_context(BlockDriverState *bs, AioContext *ctx,
 226                                   BdrvChild *ignore_child, Error **errp);
 227bool bdrv_child_can_set_aio_context(BdrvChild *c, AioContext *ctx,
 228                                    GSList **ignore, Error **errp);
 229bool bdrv_can_set_aio_context(BlockDriverState *bs, AioContext *ctx,
 230                              GSList **ignore, Error **errp);
 231AioContext *bdrv_child_get_parent_aio_context(BdrvChild *c);
 232
 233int bdrv_probe_blocksizes(BlockDriverState *bs, BlockSizes *bsz);
 234int bdrv_probe_geometry(BlockDriverState *bs, HDGeometry *geo);
 235
 236void bdrv_add_child(BlockDriverState *parent, BlockDriverState *child,
 237                    Error **errp);
 238void bdrv_del_child(BlockDriverState *parent, BdrvChild *child, Error **errp);
 239
 240/**
 241 *
 242 * bdrv_register_buf/bdrv_unregister_buf:
 243 *
 244 * Register/unregister a buffer for I/O. For example, VFIO drivers are
 245 * interested to know the memory areas that would later be used for I/O, so
 246 * that they can prepare IOMMU mapping etc., to get better performance.
 247 */
 248void bdrv_register_buf(BlockDriverState *bs, void *host, size_t size);
 249void bdrv_unregister_buf(BlockDriverState *bs, void *host);
 250
 251void bdrv_cancel_in_flight(BlockDriverState *bs);
 252
 253#endif /* BLOCK_GLOBAL_STATE_H */
 254