block.c (d42a8a935b8b2d567331fffa13a70918352668bb) block.c (dd62f1ca433ea60b06590884642ad2c8f8e539f2)
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

--- 1502 unchanged lines hidden (view full) ---

1511 ret = bdrv_open_backing_file(bs, backing_options, &local_err);
1512 if (ret < 0) {
1513 goto close_and_fail;
1514 }
1515 }
1516
1517 bdrv_refresh_filename(bs);
1518
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

--- 1502 unchanged lines hidden (view full) ---

1511 ret = bdrv_open_backing_file(bs, backing_options, &local_err);
1512 if (ret < 0) {
1513 goto close_and_fail;
1514 }
1515 }
1516
1517 bdrv_refresh_filename(bs);
1518
1519 /* For snapshot=on, create a temporary qcow2 overlay. bs points to the
1520 * temporary snapshot afterwards. */
1521 if (snapshot_flags) {
1522 ret = bdrv_append_temp_snapshot(bs, snapshot_flags, &local_err);
1523 if (local_err) {
1524 goto close_and_fail;
1525 }
1526 }
1527
1528 /* Check if any unknown options were used */
1529 if (options && (qdict_size(options) != 0)) {
1530 const QDictEntry *entry = qdict_first(options);
1531 if (flags & BDRV_O_PROTOCOL) {
1532 error_setg(errp, "Block protocol '%s' doesn't support the option "
1533 "'%s'", drv->format_name, entry->key);
1534 } else {
1535 error_setg(errp, "Block format '%s' used by device '%s' doesn't "

--- 15 unchanged lines hidden (view full) ---

1551 error_setg(errp,
1552 "Guest must be stopped for opening of encrypted image");
1553 ret = -EBUSY;
1554 goto close_and_fail;
1555 }
1556
1557 QDECREF(options);
1558 *pbs = bs;
1519 /* Check if any unknown options were used */
1520 if (options && (qdict_size(options) != 0)) {
1521 const QDictEntry *entry = qdict_first(options);
1522 if (flags & BDRV_O_PROTOCOL) {
1523 error_setg(errp, "Block protocol '%s' doesn't support the option "
1524 "'%s'", drv->format_name, entry->key);
1525 } else {
1526 error_setg(errp, "Block format '%s' used by device '%s' doesn't "

--- 15 unchanged lines hidden (view full) ---

1542 error_setg(errp,
1543 "Guest must be stopped for opening of encrypted image");
1544 ret = -EBUSY;
1545 goto close_and_fail;
1546 }
1547
1548 QDECREF(options);
1549 *pbs = bs;
1550
1551 /* For snapshot=on, create a temporary qcow2 overlay. bs points to the
1552 * temporary snapshot afterwards. */
1553 if (snapshot_flags) {
1554 ret = bdrv_append_temp_snapshot(bs, snapshot_flags, &local_err);
1555 if (local_err) {
1556 goto close_and_fail;
1557 }
1558 }
1559
1559 return 0;
1560
1561fail:
1562 if (file != NULL) {
1563 bdrv_unref_child(bs, file);
1564 }
1565 QDECREF(bs->options);
1566 QDECREF(options);

--- 428 unchanged lines hidden (view full) ---

1995 /* move some fields that need to stay attached to the device */
1996
1997 /* dev info */
1998 bs_dest->guest_block_size = bs_src->guest_block_size;
1999 bs_dest->copy_on_read = bs_src->copy_on_read;
2000
2001 bs_dest->enable_write_cache = bs_src->enable_write_cache;
2002
1560 return 0;
1561
1562fail:
1563 if (file != NULL) {
1564 bdrv_unref_child(bs, file);
1565 }
1566 QDECREF(bs->options);
1567 QDECREF(options);

--- 428 unchanged lines hidden (view full) ---

1996 /* move some fields that need to stay attached to the device */
1997
1998 /* dev info */
1999 bs_dest->guest_block_size = bs_src->guest_block_size;
2000 bs_dest->copy_on_read = bs_src->copy_on_read;
2001
2002 bs_dest->enable_write_cache = bs_src->enable_write_cache;
2003
2003 /* i/o throttled req */
2004 bs_dest->throttle_state = bs_src->throttle_state,
2005 bs_dest->io_limits_enabled = bs_src->io_limits_enabled;
2006 bs_dest->pending_reqs[0] = bs_src->pending_reqs[0];
2007 bs_dest->pending_reqs[1] = bs_src->pending_reqs[1];
2008 bs_dest->throttled_reqs[0] = bs_src->throttled_reqs[0];
2009 bs_dest->throttled_reqs[1] = bs_src->throttled_reqs[1];
2010 memcpy(&bs_dest->round_robin,
2011 &bs_src->round_robin,
2012 sizeof(bs_dest->round_robin));
2013 memcpy(&bs_dest->throttle_timers,
2014 &bs_src->throttle_timers,
2015 sizeof(ThrottleTimers));
2016
2017 /* r/w error */
2018 bs_dest->on_read_error = bs_src->on_read_error;
2019 bs_dest->on_write_error = bs_src->on_write_error;
2020
2021 /* i/o status */
2022 bs_dest->iostatus_enabled = bs_src->iostatus_enabled;
2023 bs_dest->iostatus = bs_src->iostatus;
2024
2025 /* dirty bitmap */
2026 bs_dest->dirty_bitmaps = bs_src->dirty_bitmaps;
2027}
2028
2029/* Fields that only need to be swapped if the contents of BDSes is swapped
2004 /* r/w error */
2005 bs_dest->on_read_error = bs_src->on_read_error;
2006 bs_dest->on_write_error = bs_src->on_write_error;
2007
2008 /* i/o status */
2009 bs_dest->iostatus_enabled = bs_src->iostatus_enabled;
2010 bs_dest->iostatus = bs_src->iostatus;
2011
2012 /* dirty bitmap */
2013 bs_dest->dirty_bitmaps = bs_src->dirty_bitmaps;
2014}
2015
2016/* Fields that only need to be swapped if the contents of BDSes is swapped
2030 * rather than pointers being changed in the parents. */
2017 * rather than pointers being changed in the parents, and throttling fields
2018 * because only bdrv_swap() messes with internals of throttling. */
2031static void bdrv_move_reference_fields(BlockDriverState *bs_dest,
2032 BlockDriverState *bs_src)
2033{
2019static void bdrv_move_reference_fields(BlockDriverState *bs_dest,
2020 BlockDriverState *bs_src)
2021{
2022 /* i/o throttled req */
2023 bs_dest->throttle_state = bs_src->throttle_state,
2024 bs_dest->io_limits_enabled = bs_src->io_limits_enabled;
2025 bs_dest->pending_reqs[0] = bs_src->pending_reqs[0];
2026 bs_dest->pending_reqs[1] = bs_src->pending_reqs[1];
2027 bs_dest->throttled_reqs[0] = bs_src->throttled_reqs[0];
2028 bs_dest->throttled_reqs[1] = bs_src->throttled_reqs[1];
2029 memcpy(&bs_dest->round_robin,
2030 &bs_src->round_robin,
2031 sizeof(bs_dest->round_robin));
2032 memcpy(&bs_dest->throttle_timers,
2033 &bs_src->throttle_timers,
2034 sizeof(ThrottleTimers));
2035
2034 /* reference count */
2035 bs_dest->refcnt = bs_src->refcnt;
2036
2037 /* job */
2038 bs_dest->job = bs_src->job;
2039
2040 /* keep the same entry in bdrv_states */
2041 bs_dest->device_list = bs_src->device_list;

--- 109 unchanged lines hidden (view full) ---

2151 child->bs->inherits_from = bs_new;
2152 }
2153 }
2154
2155 bdrv_rebind(bs_new);
2156 bdrv_rebind(bs_old);
2157}
2158
2036 /* reference count */
2037 bs_dest->refcnt = bs_src->refcnt;
2038
2039 /* job */
2040 bs_dest->job = bs_src->job;
2041
2042 /* keep the same entry in bdrv_states */
2043 bs_dest->device_list = bs_src->device_list;

--- 109 unchanged lines hidden (view full) ---

2153 child->bs->inherits_from = bs_new;
2154 }
2155 }
2156
2157 bdrv_rebind(bs_new);
2158 bdrv_rebind(bs_old);
2159}
2160
2161static void change_parent_backing_link(BlockDriverState *from,
2162 BlockDriverState *to)
2163{
2164 BdrvChild *c, *next;
2165
2166 QLIST_FOREACH_SAFE(c, &from->parents, next_parent, next) {
2167 assert(c->role != &child_backing);
2168 c->bs = to;
2169 QLIST_REMOVE(c, next_parent);
2170 QLIST_INSERT_HEAD(&to->parents, c, next_parent);
2171 bdrv_ref(to);
2172 bdrv_unref(from);
2173 }
2174 if (from->blk) {
2175 blk_set_bs(from->blk, to);
2176 if (!to->device_list.tqe_prev) {
2177 QTAILQ_INSERT_BEFORE(from, to, device_list);
2178 }
2179 QTAILQ_REMOVE(&bdrv_states, from, device_list);
2180 }
2181}
2182
2183static void swap_feature_fields(BlockDriverState *bs_top,
2184 BlockDriverState *bs_new)
2185{
2186 BlockDriverState tmp;
2187
2188 bdrv_move_feature_fields(&tmp, bs_top);
2189 bdrv_move_feature_fields(bs_top, bs_new);
2190 bdrv_move_feature_fields(bs_new, &tmp);
2191
2192 assert(!bs_new->throttle_state);
2193 if (bs_top->throttle_state) {
2194 assert(bs_top->io_limits_enabled);
2195 bdrv_io_limits_enable(bs_new, throttle_group_get_name(bs_top));
2196 bdrv_io_limits_disable(bs_top);
2197 }
2198}
2199
2159/*
2160 * Add new bs contents at the top of an image chain while the chain is
2161 * live, while keeping required fields on the top layer.
2162 *
2163 * This will modify the BlockDriverState fields, and swap contents
2164 * between bs_new and bs_top. Both bs_new and bs_top are modified.
2165 *
2166 * bs_new must not be attached to a BlockBackend.
2167 *
2168 * This function does not create any image files.
2200/*
2201 * Add new bs contents at the top of an image chain while the chain is
2202 * live, while keeping required fields on the top layer.
2203 *
2204 * This will modify the BlockDriverState fields, and swap contents
2205 * between bs_new and bs_top. Both bs_new and bs_top are modified.
2206 *
2207 * bs_new must not be attached to a BlockBackend.
2208 *
2209 * This function does not create any image files.
2210 *
2211 * bdrv_append() takes ownership of a bs_new reference and unrefs it because
2212 * that's what the callers commonly need. bs_new will be referenced by the old
2213 * parents of bs_top after bdrv_append() returns. If the caller needs to keep a
2214 * reference of its own, it must call bdrv_ref().
2169 */
2170void bdrv_append(BlockDriverState *bs_new, BlockDriverState *bs_top)
2171{
2215 */
2216void bdrv_append(BlockDriverState *bs_new, BlockDriverState *bs_top)
2217{
2172 bdrv_swap(bs_new, bs_top);
2218 assert(!bdrv_requests_pending(bs_top));
2219 assert(!bdrv_requests_pending(bs_new));
2173
2220
2174 /* The contents of 'tmp' will become bs_top, as we are
2175 * swapping bs_new and bs_top contents. */
2176 bdrv_set_backing_hd(bs_top, bs_new);
2221 bdrv_ref(bs_top);
2222 change_parent_backing_link(bs_top, bs_new);
2223
2224 /* Some fields always stay on top of the backing file chain */
2225 swap_feature_fields(bs_top, bs_new);
2226
2227 bdrv_set_backing_hd(bs_new, bs_top);
2228 bdrv_unref(bs_top);
2229
2230 /* bs_new is now referenced by its new parents, we don't need the
2231 * additional reference any more. */
2232 bdrv_unref(bs_new);
2177}
2178
2179static void bdrv_delete(BlockDriverState *bs)
2180{
2181 assert(!bs->job);
2182 assert(bdrv_op_blocker_is_empty(bs));
2183 assert(!bs->refcnt);
2184 assert(QLIST_EMPTY(&bs->dirty_bitmaps));

--- 2034 unchanged lines hidden ---
2233}
2234
2235static void bdrv_delete(BlockDriverState *bs)
2236{
2237 assert(!bs->job);
2238 assert(bdrv_op_blocker_is_empty(bs));
2239 assert(!bs->refcnt);
2240 assert(QLIST_EMPTY(&bs->dirty_bitmaps));

--- 2034 unchanged lines hidden ---