Kevin Wolf | 4938109 | 2011-01-10 17:15:10 +0100 | [diff] [blame] | 1 | /* |
| 2 | * L2/refcount table cache for the QCOW2 format |
| 3 | * |
| 4 | * Copyright (c) 2010 Kevin Wolf <kwolf@redhat.com> |
| 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 | |
Paolo Bonzini | 737e150 | 2012-12-17 18:19:44 +0100 | [diff] [blame] | 25 | #include "block/block_int.h" |
Kevin Wolf | 4938109 | 2011-01-10 17:15:10 +0100 | [diff] [blame] | 26 | #include "qemu-common.h" |
| 27 | #include "qcow2.h" |
Kevin Wolf | 3cce16f | 2012-03-01 18:36:21 +0100 | [diff] [blame] | 28 | #include "trace.h" |
Kevin Wolf | 4938109 | 2011-01-10 17:15:10 +0100 | [diff] [blame] | 29 | |
| 30 | typedef struct Qcow2CachedTable { |
| 31 | void* table; |
| 32 | int64_t offset; |
| 33 | bool dirty; |
| 34 | int cache_hits; |
| 35 | int ref; |
| 36 | } Qcow2CachedTable; |
| 37 | |
| 38 | struct Qcow2Cache { |
Kevin Wolf | 4938109 | 2011-01-10 17:15:10 +0100 | [diff] [blame] | 39 | Qcow2CachedTable* entries; |
| 40 | struct Qcow2Cache* depends; |
Jes Sorensen | bf59502 | 2011-01-27 11:25:03 +0100 | [diff] [blame] | 41 | int size; |
Kevin Wolf | 3de0a29 | 2011-01-14 15:55:38 +0100 | [diff] [blame] | 42 | bool depends_on_flush; |
Kevin Wolf | 4938109 | 2011-01-10 17:15:10 +0100 | [diff] [blame] | 43 | }; |
| 44 | |
Paolo Bonzini | 6af4e9e | 2012-06-06 00:04:55 +0200 | [diff] [blame] | 45 | Qcow2Cache *qcow2_cache_create(BlockDriverState *bs, int num_tables) |
Kevin Wolf | 4938109 | 2011-01-10 17:15:10 +0100 | [diff] [blame] | 46 | { |
| 47 | BDRVQcowState *s = bs->opaque; |
| 48 | Qcow2Cache *c; |
| 49 | int i; |
| 50 | |
Max Reitz | 02004bd | 2014-08-18 22:07:32 +0200 | [diff] [blame] | 51 | c = g_new0(Qcow2Cache, 1); |
Kevin Wolf | 4938109 | 2011-01-10 17:15:10 +0100 | [diff] [blame] | 52 | c->size = num_tables; |
Max Reitz | 02004bd | 2014-08-18 22:07:32 +0200 | [diff] [blame] | 53 | c->entries = g_try_new0(Qcow2CachedTable, num_tables); |
| 54 | if (!c->entries) { |
| 55 | goto fail; |
| 56 | } |
Kevin Wolf | 4938109 | 2011-01-10 17:15:10 +0100 | [diff] [blame] | 57 | |
| 58 | for (i = 0; i < c->size; i++) { |
Kevin Wolf | de82815 | 2014-05-20 17:12:47 +0200 | [diff] [blame] | 59 | c->entries[i].table = qemu_try_blockalign(bs->file, s->cluster_size); |
| 60 | if (c->entries[i].table == NULL) { |
| 61 | goto fail; |
| 62 | } |
Kevin Wolf | 4938109 | 2011-01-10 17:15:10 +0100 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | return c; |
Kevin Wolf | de82815 | 2014-05-20 17:12:47 +0200 | [diff] [blame] | 66 | |
| 67 | fail: |
Max Reitz | 02004bd | 2014-08-18 22:07:32 +0200 | [diff] [blame] | 68 | if (c->entries) { |
| 69 | for (i = 0; i < c->size; i++) { |
| 70 | qemu_vfree(c->entries[i].table); |
| 71 | } |
Kevin Wolf | de82815 | 2014-05-20 17:12:47 +0200 | [diff] [blame] | 72 | } |
| 73 | g_free(c->entries); |
| 74 | g_free(c); |
| 75 | return NULL; |
Kevin Wolf | 4938109 | 2011-01-10 17:15:10 +0100 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | int qcow2_cache_destroy(BlockDriverState* bs, Qcow2Cache *c) |
| 79 | { |
| 80 | int i; |
| 81 | |
| 82 | for (i = 0; i < c->size; i++) { |
| 83 | assert(c->entries[i].ref == 0); |
| 84 | qemu_vfree(c->entries[i].table); |
| 85 | } |
| 86 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 87 | g_free(c->entries); |
| 88 | g_free(c); |
Kevin Wolf | 4938109 | 2011-01-10 17:15:10 +0100 | [diff] [blame] | 89 | |
| 90 | return 0; |
| 91 | } |
| 92 | |
| 93 | static int qcow2_cache_flush_dependency(BlockDriverState *bs, Qcow2Cache *c) |
| 94 | { |
| 95 | int ret; |
| 96 | |
| 97 | ret = qcow2_cache_flush(bs, c->depends); |
| 98 | if (ret < 0) { |
| 99 | return ret; |
| 100 | } |
| 101 | |
| 102 | c->depends = NULL; |
Kevin Wolf | 3de0a29 | 2011-01-14 15:55:38 +0100 | [diff] [blame] | 103 | c->depends_on_flush = false; |
| 104 | |
Kevin Wolf | 4938109 | 2011-01-10 17:15:10 +0100 | [diff] [blame] | 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | static int qcow2_cache_entry_flush(BlockDriverState *bs, Qcow2Cache *c, int i) |
| 109 | { |
| 110 | BDRVQcowState *s = bs->opaque; |
Kevin Wolf | 3de0a29 | 2011-01-14 15:55:38 +0100 | [diff] [blame] | 111 | int ret = 0; |
Kevin Wolf | 4938109 | 2011-01-10 17:15:10 +0100 | [diff] [blame] | 112 | |
| 113 | if (!c->entries[i].dirty || !c->entries[i].offset) { |
| 114 | return 0; |
| 115 | } |
| 116 | |
Kevin Wolf | 3cce16f | 2012-03-01 18:36:21 +0100 | [diff] [blame] | 117 | trace_qcow2_cache_entry_flush(qemu_coroutine_self(), |
| 118 | c == s->l2_table_cache, i); |
| 119 | |
Kevin Wolf | 4938109 | 2011-01-10 17:15:10 +0100 | [diff] [blame] | 120 | if (c->depends) { |
| 121 | ret = qcow2_cache_flush_dependency(bs, c); |
Kevin Wolf | 3de0a29 | 2011-01-14 15:55:38 +0100 | [diff] [blame] | 122 | } else if (c->depends_on_flush) { |
| 123 | ret = bdrv_flush(bs->file); |
| 124 | if (ret >= 0) { |
| 125 | c->depends_on_flush = false; |
Kevin Wolf | 4938109 | 2011-01-10 17:15:10 +0100 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | |
Kevin Wolf | 3de0a29 | 2011-01-14 15:55:38 +0100 | [diff] [blame] | 129 | if (ret < 0) { |
| 130 | return ret; |
| 131 | } |
| 132 | |
Kevin Wolf | 29c1a73 | 2011-01-10 17:17:28 +0100 | [diff] [blame] | 133 | if (c == s->refcount_block_cache) { |
Max Reitz | 231bb26 | 2013-10-10 11:09:23 +0200 | [diff] [blame] | 134 | ret = qcow2_pre_write_overlap_check(bs, QCOW2_OL_REFCOUNT_BLOCK, |
Max Reitz | cf93980 | 2013-08-30 14:34:26 +0200 | [diff] [blame] | 135 | c->entries[i].offset, s->cluster_size); |
| 136 | } else if (c == s->l2_table_cache) { |
Max Reitz | 231bb26 | 2013-10-10 11:09:23 +0200 | [diff] [blame] | 137 | ret = qcow2_pre_write_overlap_check(bs, QCOW2_OL_ACTIVE_L2, |
Max Reitz | cf93980 | 2013-08-30 14:34:26 +0200 | [diff] [blame] | 138 | c->entries[i].offset, s->cluster_size); |
| 139 | } else { |
Max Reitz | 231bb26 | 2013-10-10 11:09:23 +0200 | [diff] [blame] | 140 | ret = qcow2_pre_write_overlap_check(bs, 0, |
Max Reitz | cf93980 | 2013-08-30 14:34:26 +0200 | [diff] [blame] | 141 | c->entries[i].offset, s->cluster_size); |
| 142 | } |
| 143 | |
| 144 | if (ret < 0) { |
| 145 | return ret; |
| 146 | } |
| 147 | |
| 148 | if (c == s->refcount_block_cache) { |
Kevin Wolf | 29c1a73 | 2011-01-10 17:17:28 +0100 | [diff] [blame] | 149 | BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_UPDATE_PART); |
| 150 | } else if (c == s->l2_table_cache) { |
| 151 | BLKDBG_EVENT(bs->file, BLKDBG_L2_UPDATE); |
| 152 | } |
| 153 | |
Kevin Wolf | 4938109 | 2011-01-10 17:15:10 +0100 | [diff] [blame] | 154 | ret = bdrv_pwrite(bs->file, c->entries[i].offset, c->entries[i].table, |
| 155 | s->cluster_size); |
| 156 | if (ret < 0) { |
| 157 | return ret; |
| 158 | } |
| 159 | |
| 160 | c->entries[i].dirty = false; |
| 161 | |
| 162 | return 0; |
| 163 | } |
| 164 | |
| 165 | int qcow2_cache_flush(BlockDriverState *bs, Qcow2Cache *c) |
| 166 | { |
Kevin Wolf | 3cce16f | 2012-03-01 18:36:21 +0100 | [diff] [blame] | 167 | BDRVQcowState *s = bs->opaque; |
Kevin Wolf | 4938109 | 2011-01-10 17:15:10 +0100 | [diff] [blame] | 168 | int result = 0; |
| 169 | int ret; |
| 170 | int i; |
| 171 | |
Kevin Wolf | 3cce16f | 2012-03-01 18:36:21 +0100 | [diff] [blame] | 172 | trace_qcow2_cache_flush(qemu_coroutine_self(), c == s->l2_table_cache); |
| 173 | |
Kevin Wolf | 4938109 | 2011-01-10 17:15:10 +0100 | [diff] [blame] | 174 | for (i = 0; i < c->size; i++) { |
| 175 | ret = qcow2_cache_entry_flush(bs, c, i); |
| 176 | if (ret < 0 && result != -ENOSPC) { |
| 177 | result = ret; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | if (result == 0) { |
| 182 | ret = bdrv_flush(bs->file); |
| 183 | if (ret < 0) { |
| 184 | result = ret; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | return result; |
| 189 | } |
| 190 | |
| 191 | int qcow2_cache_set_dependency(BlockDriverState *bs, Qcow2Cache *c, |
| 192 | Qcow2Cache *dependency) |
| 193 | { |
| 194 | int ret; |
| 195 | |
| 196 | if (dependency->depends) { |
| 197 | ret = qcow2_cache_flush_dependency(bs, dependency); |
| 198 | if (ret < 0) { |
| 199 | return ret; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | if (c->depends && (c->depends != dependency)) { |
| 204 | ret = qcow2_cache_flush_dependency(bs, c); |
| 205 | if (ret < 0) { |
| 206 | return ret; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | c->depends = dependency; |
| 211 | return 0; |
| 212 | } |
| 213 | |
Kevin Wolf | 3de0a29 | 2011-01-14 15:55:38 +0100 | [diff] [blame] | 214 | void qcow2_cache_depends_on_flush(Qcow2Cache *c) |
| 215 | { |
| 216 | c->depends_on_flush = true; |
| 217 | } |
| 218 | |
Max Reitz | e7108fe | 2013-09-03 10:09:51 +0200 | [diff] [blame] | 219 | int qcow2_cache_empty(BlockDriverState *bs, Qcow2Cache *c) |
| 220 | { |
| 221 | int ret, i; |
| 222 | |
| 223 | ret = qcow2_cache_flush(bs, c); |
| 224 | if (ret < 0) { |
| 225 | return ret; |
| 226 | } |
| 227 | |
| 228 | for (i = 0; i < c->size; i++) { |
| 229 | assert(c->entries[i].ref == 0); |
| 230 | c->entries[i].offset = 0; |
| 231 | c->entries[i].cache_hits = 0; |
| 232 | } |
| 233 | |
| 234 | return 0; |
| 235 | } |
| 236 | |
Kevin Wolf | 4938109 | 2011-01-10 17:15:10 +0100 | [diff] [blame] | 237 | static int qcow2_cache_find_entry_to_replace(Qcow2Cache *c) |
| 238 | { |
| 239 | int i; |
| 240 | int min_count = INT_MAX; |
| 241 | int min_index = -1; |
| 242 | |
| 243 | |
| 244 | for (i = 0; i < c->size; i++) { |
| 245 | if (c->entries[i].ref) { |
| 246 | continue; |
| 247 | } |
| 248 | |
| 249 | if (c->entries[i].cache_hits < min_count) { |
| 250 | min_index = i; |
| 251 | min_count = c->entries[i].cache_hits; |
| 252 | } |
| 253 | |
| 254 | /* Give newer hits priority */ |
| 255 | /* TODO Check how to optimize the replacement strategy */ |
| 256 | c->entries[i].cache_hits /= 2; |
| 257 | } |
| 258 | |
| 259 | if (min_index == -1) { |
| 260 | /* This can't happen in current synchronous code, but leave the check |
| 261 | * here as a reminder for whoever starts using AIO with the cache */ |
| 262 | abort(); |
| 263 | } |
| 264 | return min_index; |
| 265 | } |
| 266 | |
| 267 | static int qcow2_cache_do_get(BlockDriverState *bs, Qcow2Cache *c, |
| 268 | uint64_t offset, void **table, bool read_from_disk) |
| 269 | { |
| 270 | BDRVQcowState *s = bs->opaque; |
| 271 | int i; |
| 272 | int ret; |
| 273 | |
Kevin Wolf | 3cce16f | 2012-03-01 18:36:21 +0100 | [diff] [blame] | 274 | trace_qcow2_cache_get(qemu_coroutine_self(), c == s->l2_table_cache, |
| 275 | offset, read_from_disk); |
| 276 | |
Kevin Wolf | 4938109 | 2011-01-10 17:15:10 +0100 | [diff] [blame] | 277 | /* Check if the table is already cached */ |
| 278 | for (i = 0; i < c->size; i++) { |
| 279 | if (c->entries[i].offset == offset) { |
| 280 | goto found; |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | /* If not, write a table back and replace it */ |
| 285 | i = qcow2_cache_find_entry_to_replace(c); |
Kevin Wolf | 3cce16f | 2012-03-01 18:36:21 +0100 | [diff] [blame] | 286 | trace_qcow2_cache_get_replace_entry(qemu_coroutine_self(), |
| 287 | c == s->l2_table_cache, i); |
Kevin Wolf | 4938109 | 2011-01-10 17:15:10 +0100 | [diff] [blame] | 288 | if (i < 0) { |
| 289 | return i; |
| 290 | } |
| 291 | |
| 292 | ret = qcow2_cache_entry_flush(bs, c, i); |
| 293 | if (ret < 0) { |
| 294 | return ret; |
| 295 | } |
| 296 | |
Kevin Wolf | 3cce16f | 2012-03-01 18:36:21 +0100 | [diff] [blame] | 297 | trace_qcow2_cache_get_read(qemu_coroutine_self(), |
| 298 | c == s->l2_table_cache, i); |
Kevin Wolf | 4938109 | 2011-01-10 17:15:10 +0100 | [diff] [blame] | 299 | c->entries[i].offset = 0; |
| 300 | if (read_from_disk) { |
Kevin Wolf | 29c1a73 | 2011-01-10 17:17:28 +0100 | [diff] [blame] | 301 | if (c == s->l2_table_cache) { |
| 302 | BLKDBG_EVENT(bs->file, BLKDBG_L2_LOAD); |
| 303 | } |
| 304 | |
Kevin Wolf | 4938109 | 2011-01-10 17:15:10 +0100 | [diff] [blame] | 305 | ret = bdrv_pread(bs->file, offset, c->entries[i].table, s->cluster_size); |
| 306 | if (ret < 0) { |
| 307 | return ret; |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | /* Give the table some hits for the start so that it won't be replaced |
| 312 | * immediately. The number 32 is completely arbitrary. */ |
| 313 | c->entries[i].cache_hits = 32; |
| 314 | c->entries[i].offset = offset; |
| 315 | |
| 316 | /* And return the right table */ |
| 317 | found: |
| 318 | c->entries[i].cache_hits++; |
| 319 | c->entries[i].ref++; |
| 320 | *table = c->entries[i].table; |
Kevin Wolf | 3cce16f | 2012-03-01 18:36:21 +0100 | [diff] [blame] | 321 | |
| 322 | trace_qcow2_cache_get_done(qemu_coroutine_self(), |
| 323 | c == s->l2_table_cache, i); |
| 324 | |
Kevin Wolf | 4938109 | 2011-01-10 17:15:10 +0100 | [diff] [blame] | 325 | return 0; |
| 326 | } |
| 327 | |
| 328 | int qcow2_cache_get(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset, |
| 329 | void **table) |
| 330 | { |
| 331 | return qcow2_cache_do_get(bs, c, offset, table, true); |
| 332 | } |
| 333 | |
| 334 | int qcow2_cache_get_empty(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset, |
| 335 | void **table) |
| 336 | { |
| 337 | return qcow2_cache_do_get(bs, c, offset, table, false); |
| 338 | } |
| 339 | |
| 340 | int qcow2_cache_put(BlockDriverState *bs, Qcow2Cache *c, void **table) |
| 341 | { |
| 342 | int i; |
| 343 | |
| 344 | for (i = 0; i < c->size; i++) { |
| 345 | if (c->entries[i].table == *table) { |
| 346 | goto found; |
| 347 | } |
| 348 | } |
| 349 | return -ENOENT; |
| 350 | |
| 351 | found: |
| 352 | c->entries[i].ref--; |
| 353 | *table = NULL; |
| 354 | |
| 355 | assert(c->entries[i].ref >= 0); |
Paolo Bonzini | 6af4e9e | 2012-06-06 00:04:55 +0200 | [diff] [blame] | 356 | return 0; |
Kevin Wolf | 4938109 | 2011-01-10 17:15:10 +0100 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | void qcow2_cache_entry_mark_dirty(Qcow2Cache *c, void *table) |
| 360 | { |
| 361 | int i; |
| 362 | |
| 363 | for (i = 0; i < c->size; i++) { |
| 364 | if (c->entries[i].table == table) { |
| 365 | goto found; |
| 366 | } |
| 367 | } |
| 368 | abort(); |
| 369 | |
| 370 | found: |
| 371 | c->entries[i].dirty = true; |
| 372 | } |