]>
Commit | Line | Data |
---|---|---|
75411d23 SH |
1 | /* |
2 | * QEMU Enhanced Disk Format | |
3 | * | |
4 | * Copyright IBM, Corp. 2010 | |
5 | * | |
6 | * Authors: | |
7 | * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com> | |
8 | * Anthony Liguori <aliguori@us.ibm.com> | |
9 | * | |
10 | * This work is licensed under the terms of the GNU LGPL, version 2 or later. | |
11 | * See the COPYING.LIB file in the top-level directory. | |
12 | * | |
13 | */ | |
14 | ||
80c71a24 | 15 | #include "qemu/osdep.h" |
da34e65c | 16 | #include "qapi/error.h" |
1de7afc9 | 17 | #include "qemu/timer.h" |
58369e22 | 18 | #include "qemu/bswap.h" |
eabba580 | 19 | #include "trace.h" |
75411d23 | 20 | #include "qed.h" |
7b1b5d19 | 21 | #include "qapi/qmp/qerror.h" |
caf71f86 | 22 | #include "migration/migration.h" |
8a56fdad | 23 | #include "sysemu/block-backend.h" |
75411d23 | 24 | |
d7331bed | 25 | static const AIOCBInfo qed_aiocb_info = { |
eabba580 | 26 | .aiocb_size = sizeof(QEDAIOCB), |
eabba580 SH |
27 | }; |
28 | ||
75411d23 SH |
29 | static int bdrv_qed_probe(const uint8_t *buf, int buf_size, |
30 | const char *filename) | |
31 | { | |
32 | const QEDHeader *header = (const QEDHeader *)buf; | |
33 | ||
34 | if (buf_size < sizeof(*header)) { | |
35 | return 0; | |
36 | } | |
37 | if (le32_to_cpu(header->magic) != QED_MAGIC) { | |
38 | return 0; | |
39 | } | |
40 | return 100; | |
41 | } | |
42 | ||
43 | /** | |
44 | * Check whether an image format is raw | |
45 | * | |
46 | * @fmt: Backing file format, may be NULL | |
47 | */ | |
48 | static bool qed_fmt_is_raw(const char *fmt) | |
49 | { | |
50 | return fmt && strcmp(fmt, "raw") == 0; | |
51 | } | |
52 | ||
53 | static void qed_header_le_to_cpu(const QEDHeader *le, QEDHeader *cpu) | |
54 | { | |
55 | cpu->magic = le32_to_cpu(le->magic); | |
56 | cpu->cluster_size = le32_to_cpu(le->cluster_size); | |
57 | cpu->table_size = le32_to_cpu(le->table_size); | |
58 | cpu->header_size = le32_to_cpu(le->header_size); | |
59 | cpu->features = le64_to_cpu(le->features); | |
60 | cpu->compat_features = le64_to_cpu(le->compat_features); | |
61 | cpu->autoclear_features = le64_to_cpu(le->autoclear_features); | |
62 | cpu->l1_table_offset = le64_to_cpu(le->l1_table_offset); | |
63 | cpu->image_size = le64_to_cpu(le->image_size); | |
64 | cpu->backing_filename_offset = le32_to_cpu(le->backing_filename_offset); | |
65 | cpu->backing_filename_size = le32_to_cpu(le->backing_filename_size); | |
66 | } | |
67 | ||
68 | static void qed_header_cpu_to_le(const QEDHeader *cpu, QEDHeader *le) | |
69 | { | |
70 | le->magic = cpu_to_le32(cpu->magic); | |
71 | le->cluster_size = cpu_to_le32(cpu->cluster_size); | |
72 | le->table_size = cpu_to_le32(cpu->table_size); | |
73 | le->header_size = cpu_to_le32(cpu->header_size); | |
74 | le->features = cpu_to_le64(cpu->features); | |
75 | le->compat_features = cpu_to_le64(cpu->compat_features); | |
76 | le->autoclear_features = cpu_to_le64(cpu->autoclear_features); | |
77 | le->l1_table_offset = cpu_to_le64(cpu->l1_table_offset); | |
78 | le->image_size = cpu_to_le64(cpu->image_size); | |
79 | le->backing_filename_offset = cpu_to_le32(cpu->backing_filename_offset); | |
80 | le->backing_filename_size = cpu_to_le32(cpu->backing_filename_size); | |
81 | } | |
82 | ||
b10170ac | 83 | int qed_write_header_sync(BDRVQEDState *s) |
75411d23 SH |
84 | { |
85 | QEDHeader le; | |
86 | int ret; | |
87 | ||
88 | qed_header_cpu_to_le(&s->header, &le); | |
d9ca2ea2 | 89 | ret = bdrv_pwrite(s->bs->file, 0, &le, sizeof(le)); |
75411d23 SH |
90 | if (ret != sizeof(le)) { |
91 | return ret; | |
92 | } | |
93 | return 0; | |
94 | } | |
95 | ||
01979a98 SH |
96 | typedef struct { |
97 | GenericCB gencb; | |
98 | BDRVQEDState *s; | |
99 | struct iovec iov; | |
100 | QEMUIOVector qiov; | |
101 | int nsectors; | |
102 | uint8_t *buf; | |
103 | } QEDWriteHeaderCB; | |
104 | ||
105 | static void qed_write_header_cb(void *opaque, int ret) | |
106 | { | |
107 | QEDWriteHeaderCB *write_header_cb = opaque; | |
108 | ||
109 | qemu_vfree(write_header_cb->buf); | |
110 | gencb_complete(write_header_cb, ret); | |
111 | } | |
112 | ||
113 | static void qed_write_header_read_cb(void *opaque, int ret) | |
114 | { | |
115 | QEDWriteHeaderCB *write_header_cb = opaque; | |
116 | BDRVQEDState *s = write_header_cb->s; | |
01979a98 SH |
117 | |
118 | if (ret) { | |
119 | qed_write_header_cb(write_header_cb, ret); | |
120 | return; | |
121 | } | |
122 | ||
123 | /* Update header */ | |
124 | qed_header_cpu_to_le(&s->header, (QEDHeader *)write_header_cb->buf); | |
125 | ||
0d1049c7 | 126 | bdrv_aio_writev(s->bs->file, 0, &write_header_cb->qiov, |
ad54ae80 PB |
127 | write_header_cb->nsectors, qed_write_header_cb, |
128 | write_header_cb); | |
01979a98 SH |
129 | } |
130 | ||
131 | /** | |
132 | * Update header in-place (does not rewrite backing filename or other strings) | |
133 | * | |
134 | * This function only updates known header fields in-place and does not affect | |
135 | * extra data after the QED header. | |
136 | */ | |
097310b5 | 137 | static void qed_write_header(BDRVQEDState *s, BlockCompletionFunc cb, |
01979a98 SH |
138 | void *opaque) |
139 | { | |
140 | /* We must write full sectors for O_DIRECT but cannot necessarily generate | |
141 | * the data following the header if an unrecognized compat feature is | |
142 | * active. Therefore, first read the sectors containing the header, update | |
143 | * them, and write back. | |
144 | */ | |
145 | ||
c41a73ff | 146 | int nsectors = DIV_ROUND_UP(sizeof(QEDHeader), BDRV_SECTOR_SIZE); |
01979a98 SH |
147 | size_t len = nsectors * BDRV_SECTOR_SIZE; |
148 | QEDWriteHeaderCB *write_header_cb = gencb_alloc(sizeof(*write_header_cb), | |
149 | cb, opaque); | |
150 | ||
151 | write_header_cb->s = s; | |
152 | write_header_cb->nsectors = nsectors; | |
153 | write_header_cb->buf = qemu_blockalign(s->bs, len); | |
154 | write_header_cb->iov.iov_base = write_header_cb->buf; | |
155 | write_header_cb->iov.iov_len = len; | |
156 | qemu_iovec_init_external(&write_header_cb->qiov, &write_header_cb->iov, 1); | |
157 | ||
ebb7af21 | 158 | bdrv_aio_readv(s->bs->file, 0, &write_header_cb->qiov, nsectors, |
ad54ae80 | 159 | qed_write_header_read_cb, write_header_cb); |
01979a98 SH |
160 | } |
161 | ||
75411d23 SH |
162 | static uint64_t qed_max_image_size(uint32_t cluster_size, uint32_t table_size) |
163 | { | |
164 | uint64_t table_entries; | |
165 | uint64_t l2_size; | |
166 | ||
167 | table_entries = (table_size * cluster_size) / sizeof(uint64_t); | |
168 | l2_size = table_entries * cluster_size; | |
169 | ||
170 | return l2_size * table_entries; | |
171 | } | |
172 | ||
173 | static bool qed_is_cluster_size_valid(uint32_t cluster_size) | |
174 | { | |
175 | if (cluster_size < QED_MIN_CLUSTER_SIZE || | |
176 | cluster_size > QED_MAX_CLUSTER_SIZE) { | |
177 | return false; | |
178 | } | |
179 | if (cluster_size & (cluster_size - 1)) { | |
180 | return false; /* not power of 2 */ | |
181 | } | |
182 | return true; | |
183 | } | |
184 | ||
185 | static bool qed_is_table_size_valid(uint32_t table_size) | |
186 | { | |
187 | if (table_size < QED_MIN_TABLE_SIZE || | |
188 | table_size > QED_MAX_TABLE_SIZE) { | |
189 | return false; | |
190 | } | |
191 | if (table_size & (table_size - 1)) { | |
192 | return false; /* not power of 2 */ | |
193 | } | |
194 | return true; | |
195 | } | |
196 | ||
197 | static bool qed_is_image_size_valid(uint64_t image_size, uint32_t cluster_size, | |
198 | uint32_t table_size) | |
199 | { | |
200 | if (image_size % BDRV_SECTOR_SIZE != 0) { | |
201 | return false; /* not multiple of sector size */ | |
202 | } | |
203 | if (image_size > qed_max_image_size(cluster_size, table_size)) { | |
204 | return false; /* image is too large */ | |
205 | } | |
206 | return true; | |
207 | } | |
208 | ||
209 | /** | |
210 | * Read a string of known length from the image file | |
211 | * | |
212 | * @file: Image file | |
213 | * @offset: File offset to start of string, in bytes | |
214 | * @n: String length in bytes | |
215 | * @buf: Destination buffer | |
216 | * @buflen: Destination buffer length in bytes | |
217 | * @ret: 0 on success, -errno on failure | |
218 | * | |
219 | * The string is NUL-terminated. | |
220 | */ | |
cf2ab8fc | 221 | static int qed_read_string(BdrvChild *file, uint64_t offset, size_t n, |
75411d23 SH |
222 | char *buf, size_t buflen) |
223 | { | |
224 | int ret; | |
225 | if (n >= buflen) { | |
226 | return -EINVAL; | |
227 | } | |
228 | ret = bdrv_pread(file, offset, buf, n); | |
229 | if (ret < 0) { | |
230 | return ret; | |
231 | } | |
232 | buf[n] = '\0'; | |
233 | return 0; | |
234 | } | |
235 | ||
eabba580 SH |
236 | /** |
237 | * Allocate new clusters | |
238 | * | |
239 | * @s: QED state | |
240 | * @n: Number of contiguous clusters to allocate | |
241 | * @ret: Offset of first allocated cluster | |
242 | * | |
243 | * This function only produces the offset where the new clusters should be | |
244 | * written. It updates BDRVQEDState but does not make any changes to the image | |
245 | * file. | |
246 | */ | |
247 | static uint64_t qed_alloc_clusters(BDRVQEDState *s, unsigned int n) | |
248 | { | |
249 | uint64_t offset = s->file_size; | |
250 | s->file_size += n * s->header.cluster_size; | |
251 | return offset; | |
252 | } | |
253 | ||
298800ca SH |
254 | QEDTable *qed_alloc_table(BDRVQEDState *s) |
255 | { | |
256 | /* Honor O_DIRECT memory alignment requirements */ | |
257 | return qemu_blockalign(s->bs, | |
258 | s->header.cluster_size * s->header.table_size); | |
259 | } | |
260 | ||
eabba580 SH |
261 | /** |
262 | * Allocate a new zeroed L2 table | |
263 | */ | |
264 | static CachedL2Table *qed_new_l2_table(BDRVQEDState *s) | |
265 | { | |
266 | CachedL2Table *l2_table = qed_alloc_l2_cache_entry(&s->l2_cache); | |
267 | ||
268 | l2_table->table = qed_alloc_table(s); | |
269 | l2_table->offset = qed_alloc_clusters(s, s->header.table_size); | |
270 | ||
271 | memset(l2_table->table->offsets, 0, | |
272 | s->header.cluster_size * s->header.table_size); | |
273 | return l2_table; | |
274 | } | |
275 | ||
b20123a2 PB |
276 | static void qed_aio_next_io(QEDAIOCB *acb, int ret); |
277 | ||
278 | static void qed_aio_start_io(QEDAIOCB *acb) | |
279 | { | |
280 | qed_aio_next_io(acb, 0); | |
281 | } | |
282 | ||
283 | static void qed_aio_next_io_cb(void *opaque, int ret) | |
284 | { | |
285 | QEDAIOCB *acb = opaque; | |
286 | ||
287 | qed_aio_next_io(acb, ret); | |
288 | } | |
eabba580 | 289 | |
6f321e93 SH |
290 | static void qed_plug_allocating_write_reqs(BDRVQEDState *s) |
291 | { | |
292 | assert(!s->allocating_write_reqs_plugged); | |
293 | ||
294 | s->allocating_write_reqs_plugged = true; | |
295 | } | |
296 | ||
297 | static void qed_unplug_allocating_write_reqs(BDRVQEDState *s) | |
298 | { | |
299 | QEDAIOCB *acb; | |
300 | ||
301 | assert(s->allocating_write_reqs_plugged); | |
302 | ||
303 | s->allocating_write_reqs_plugged = false; | |
304 | ||
305 | acb = QSIMPLEQ_FIRST(&s->allocating_write_reqs); | |
306 | if (acb) { | |
b20123a2 | 307 | qed_aio_start_io(acb); |
6f321e93 SH |
308 | } |
309 | } | |
310 | ||
311 | static void qed_finish_clear_need_check(void *opaque, int ret) | |
312 | { | |
313 | /* Do nothing */ | |
314 | } | |
315 | ||
316 | static void qed_flush_after_clear_need_check(void *opaque, int ret) | |
317 | { | |
318 | BDRVQEDState *s = opaque; | |
319 | ||
320 | bdrv_aio_flush(s->bs, qed_finish_clear_need_check, s); | |
321 | ||
322 | /* No need to wait until flush completes */ | |
323 | qed_unplug_allocating_write_reqs(s); | |
324 | } | |
325 | ||
326 | static void qed_clear_need_check(void *opaque, int ret) | |
327 | { | |
328 | BDRVQEDState *s = opaque; | |
329 | ||
330 | if (ret) { | |
331 | qed_unplug_allocating_write_reqs(s); | |
332 | return; | |
333 | } | |
334 | ||
335 | s->header.features &= ~QED_F_NEED_CHECK; | |
336 | qed_write_header(s, qed_flush_after_clear_need_check, s); | |
337 | } | |
338 | ||
339 | static void qed_need_check_timer_cb(void *opaque) | |
340 | { | |
341 | BDRVQEDState *s = opaque; | |
342 | ||
343 | /* The timer should only fire when allocating writes have drained */ | |
344 | assert(!QSIMPLEQ_FIRST(&s->allocating_write_reqs)); | |
345 | ||
346 | trace_qed_need_check_timer_cb(s); | |
347 | ||
2f47da5f | 348 | qed_acquire(s); |
6f321e93 SH |
349 | qed_plug_allocating_write_reqs(s); |
350 | ||
351 | /* Ensure writes are on disk before clearing flag */ | |
6653a73d | 352 | bdrv_aio_flush(s->bs->file->bs, qed_clear_need_check, s); |
2f47da5f PB |
353 | qed_release(s); |
354 | } | |
355 | ||
356 | void qed_acquire(BDRVQEDState *s) | |
357 | { | |
358 | aio_context_acquire(bdrv_get_aio_context(s->bs)); | |
359 | } | |
360 | ||
361 | void qed_release(BDRVQEDState *s) | |
362 | { | |
363 | aio_context_release(bdrv_get_aio_context(s->bs)); | |
6f321e93 SH |
364 | } |
365 | ||
366 | static void qed_start_need_check_timer(BDRVQEDState *s) | |
367 | { | |
368 | trace_qed_start_need_check_timer(s); | |
369 | ||
bc72ad67 | 370 | /* Use QEMU_CLOCK_VIRTUAL so we don't alter the image file while suspended for |
6f321e93 SH |
371 | * migration. |
372 | */ | |
bc72ad67 | 373 | timer_mod(s->need_check_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + |
73bcb24d | 374 | NANOSECONDS_PER_SECOND * QED_NEED_CHECK_TIMEOUT); |
6f321e93 SH |
375 | } |
376 | ||
377 | /* It's okay to call this multiple times or when no timer is started */ | |
378 | static void qed_cancel_need_check_timer(BDRVQEDState *s) | |
379 | { | |
380 | trace_qed_cancel_need_check_timer(s); | |
bc72ad67 | 381 | timer_del(s->need_check_timer); |
6f321e93 SH |
382 | } |
383 | ||
a8c868c3 SH |
384 | static void bdrv_qed_detach_aio_context(BlockDriverState *bs) |
385 | { | |
386 | BDRVQEDState *s = bs->opaque; | |
387 | ||
388 | qed_cancel_need_check_timer(s); | |
389 | timer_free(s->need_check_timer); | |
390 | } | |
391 | ||
392 | static void bdrv_qed_attach_aio_context(BlockDriverState *bs, | |
393 | AioContext *new_context) | |
394 | { | |
395 | BDRVQEDState *s = bs->opaque; | |
396 | ||
397 | s->need_check_timer = aio_timer_new(new_context, | |
398 | QEMU_CLOCK_VIRTUAL, SCALE_NS, | |
399 | qed_need_check_timer_cb, s); | |
400 | if (s->header.features & QED_F_NEED_CHECK) { | |
401 | qed_start_need_check_timer(s); | |
402 | } | |
403 | } | |
404 | ||
6653a73d FZ |
405 | static void bdrv_qed_drain(BlockDriverState *bs) |
406 | { | |
407 | BDRVQEDState *s = bs->opaque; | |
408 | ||
409 | /* Fire the timer immediately in order to start doing I/O as soon as the | |
410 | * header is flushed. | |
411 | */ | |
412 | if (s->need_check_timer && timer_pending(s->need_check_timer)) { | |
413 | qed_cancel_need_check_timer(s); | |
414 | qed_need_check_timer_cb(s); | |
415 | } | |
416 | } | |
417 | ||
015a1036 HR |
418 | static int bdrv_qed_open(BlockDriverState *bs, QDict *options, int flags, |
419 | Error **errp) | |
75411d23 SH |
420 | { |
421 | BDRVQEDState *s = bs->opaque; | |
422 | QEDHeader le_header; | |
423 | int64_t file_size; | |
424 | int ret; | |
425 | ||
426 | s->bs = bs; | |
eabba580 | 427 | QSIMPLEQ_INIT(&s->allocating_write_reqs); |
75411d23 | 428 | |
cf2ab8fc | 429 | ret = bdrv_pread(bs->file, 0, &le_header, sizeof(le_header)); |
75411d23 SH |
430 | if (ret < 0) { |
431 | return ret; | |
432 | } | |
75411d23 SH |
433 | qed_header_le_to_cpu(&le_header, &s->header); |
434 | ||
435 | if (s->header.magic != QED_MAGIC) { | |
76abe407 PB |
436 | error_setg(errp, "Image not in QED format"); |
437 | return -EINVAL; | |
75411d23 SH |
438 | } |
439 | if (s->header.features & ~QED_FEATURE_MASK) { | |
10b758e8 | 440 | /* image uses unsupported feature bits */ |
a55448b3 HR |
441 | error_setg(errp, "Unsupported QED features: %" PRIx64, |
442 | s->header.features & ~QED_FEATURE_MASK); | |
10b758e8 | 443 | return -ENOTSUP; |
75411d23 SH |
444 | } |
445 | if (!qed_is_cluster_size_valid(s->header.cluster_size)) { | |
446 | return -EINVAL; | |
447 | } | |
448 | ||
449 | /* Round down file size to the last cluster */ | |
9a4f4c31 | 450 | file_size = bdrv_getlength(bs->file->bs); |
75411d23 SH |
451 | if (file_size < 0) { |
452 | return file_size; | |
453 | } | |
454 | s->file_size = qed_start_of_cluster(s, file_size); | |
455 | ||
456 | if (!qed_is_table_size_valid(s->header.table_size)) { | |
457 | return -EINVAL; | |
458 | } | |
459 | if (!qed_is_image_size_valid(s->header.image_size, | |
460 | s->header.cluster_size, | |
461 | s->header.table_size)) { | |
462 | return -EINVAL; | |
463 | } | |
464 | if (!qed_check_table_offset(s, s->header.l1_table_offset)) { | |
465 | return -EINVAL; | |
466 | } | |
467 | ||
468 | s->table_nelems = (s->header.cluster_size * s->header.table_size) / | |
469 | sizeof(uint64_t); | |
786a4ea8 | 470 | s->l2_shift = ctz32(s->header.cluster_size); |
75411d23 | 471 | s->l2_mask = s->table_nelems - 1; |
786a4ea8 | 472 | s->l1_shift = s->l2_shift + ctz32(s->table_nelems); |
75411d23 | 473 | |
0adfa1ed SH |
474 | /* Header size calculation must not overflow uint32_t */ |
475 | if (s->header.header_size > UINT32_MAX / s->header.cluster_size) { | |
476 | return -EINVAL; | |
477 | } | |
478 | ||
75411d23 SH |
479 | if ((s->header.features & QED_F_BACKING_FILE)) { |
480 | if ((uint64_t)s->header.backing_filename_offset + | |
481 | s->header.backing_filename_size > | |
482 | s->header.cluster_size * s->header.header_size) { | |
483 | return -EINVAL; | |
484 | } | |
485 | ||
cf2ab8fc | 486 | ret = qed_read_string(bs->file, s->header.backing_filename_offset, |
75411d23 SH |
487 | s->header.backing_filename_size, bs->backing_file, |
488 | sizeof(bs->backing_file)); | |
489 | if (ret < 0) { | |
490 | return ret; | |
491 | } | |
492 | ||
493 | if (s->header.features & QED_F_BACKING_FORMAT_NO_PROBE) { | |
494 | pstrcpy(bs->backing_format, sizeof(bs->backing_format), "raw"); | |
495 | } | |
496 | } | |
497 | ||
498 | /* Reset unknown autoclear feature bits. This is a backwards | |
499 | * compatibility mechanism that allows images to be opened by older | |
500 | * programs, which "knock out" unknown feature bits. When an image is | |
501 | * opened by a newer program again it can detect that the autoclear | |
502 | * feature is no longer valid. | |
503 | */ | |
504 | if ((s->header.autoclear_features & ~QED_AUTOCLEAR_FEATURE_MASK) != 0 && | |
04c01a5c | 505 | !bdrv_is_read_only(bs->file->bs) && !(flags & BDRV_O_INACTIVE)) { |
75411d23 SH |
506 | s->header.autoclear_features &= QED_AUTOCLEAR_FEATURE_MASK; |
507 | ||
508 | ret = qed_write_header_sync(s); | |
509 | if (ret) { | |
510 | return ret; | |
511 | } | |
512 | ||
513 | /* From here on only known autoclear feature bits are valid */ | |
9a4f4c31 | 514 | bdrv_flush(bs->file->bs); |
75411d23 SH |
515 | } |
516 | ||
298800ca SH |
517 | s->l1_table = qed_alloc_table(s); |
518 | qed_init_l2_cache(&s->l2_cache); | |
519 | ||
520 | ret = qed_read_l1_table_sync(s); | |
01979a98 SH |
521 | if (ret) { |
522 | goto out; | |
523 | } | |
524 | ||
525 | /* If image was not closed cleanly, check consistency */ | |
058f8f16 | 526 | if (!(flags & BDRV_O_CHECK) && (s->header.features & QED_F_NEED_CHECK)) { |
01979a98 SH |
527 | /* Read-only images cannot be fixed. There is no risk of corruption |
528 | * since write operations are not possible. Therefore, allow | |
529 | * potentially inconsistent images to be opened read-only. This can | |
530 | * aid data recovery from an otherwise inconsistent image. | |
531 | */ | |
9a4f4c31 | 532 | if (!bdrv_is_read_only(bs->file->bs) && |
04c01a5c | 533 | !(flags & BDRV_O_INACTIVE)) { |
01979a98 SH |
534 | BdrvCheckResult result = {0}; |
535 | ||
536 | ret = qed_check(s, &result, true); | |
6f321e93 SH |
537 | if (ret) { |
538 | goto out; | |
539 | } | |
01979a98 SH |
540 | } |
541 | } | |
542 | ||
a8c868c3 | 543 | bdrv_qed_attach_aio_context(bs, bdrv_get_aio_context(bs)); |
6f321e93 | 544 | |
01979a98 | 545 | out: |
298800ca SH |
546 | if (ret) { |
547 | qed_free_l2_cache(&s->l2_cache); | |
548 | qemu_vfree(s->l1_table); | |
549 | } | |
75411d23 SH |
550 | return ret; |
551 | } | |
552 | ||
3baca891 | 553 | static void bdrv_qed_refresh_limits(BlockDriverState *bs, Error **errp) |
d34682cd KW |
554 | { |
555 | BDRVQEDState *s = bs->opaque; | |
556 | ||
cf081fca | 557 | bs->bl.pwrite_zeroes_alignment = s->header.cluster_size; |
d34682cd KW |
558 | } |
559 | ||
f9cb20f1 JC |
560 | /* We have nothing to do for QED reopen, stubs just return |
561 | * success */ | |
562 | static int bdrv_qed_reopen_prepare(BDRVReopenState *state, | |
563 | BlockReopenQueue *queue, Error **errp) | |
564 | { | |
565 | return 0; | |
566 | } | |
567 | ||
75411d23 SH |
568 | static void bdrv_qed_close(BlockDriverState *bs) |
569 | { | |
298800ca SH |
570 | BDRVQEDState *s = bs->opaque; |
571 | ||
a8c868c3 | 572 | bdrv_qed_detach_aio_context(bs); |
6f321e93 | 573 | |
01979a98 | 574 | /* Ensure writes reach stable storage */ |
9a4f4c31 | 575 | bdrv_flush(bs->file->bs); |
01979a98 SH |
576 | |
577 | /* Clean shutdown, no check required on next open */ | |
578 | if (s->header.features & QED_F_NEED_CHECK) { | |
579 | s->header.features &= ~QED_F_NEED_CHECK; | |
580 | qed_write_header_sync(s); | |
581 | } | |
582 | ||
298800ca SH |
583 | qed_free_l2_cache(&s->l2_cache); |
584 | qemu_vfree(s->l1_table); | |
75411d23 SH |
585 | } |
586 | ||
75411d23 SH |
587 | static int qed_create(const char *filename, uint32_t cluster_size, |
588 | uint64_t image_size, uint32_t table_size, | |
0fea6b79 | 589 | const char *backing_file, const char *backing_fmt, |
4ab15590 | 590 | QemuOpts *opts, Error **errp) |
75411d23 SH |
591 | { |
592 | QEDHeader header = { | |
593 | .magic = QED_MAGIC, | |
594 | .cluster_size = cluster_size, | |
595 | .table_size = table_size, | |
596 | .header_size = 1, | |
597 | .features = 0, | |
598 | .compat_features = 0, | |
599 | .l1_table_offset = cluster_size, | |
600 | .image_size = image_size, | |
601 | }; | |
602 | QEDHeader le_header; | |
603 | uint8_t *l1_table = NULL; | |
604 | size_t l1_size = header.cluster_size * header.table_size; | |
34b5d2c6 | 605 | Error *local_err = NULL; |
75411d23 | 606 | int ret = 0; |
8a56fdad | 607 | BlockBackend *blk; |
75411d23 | 608 | |
4ab15590 | 609 | ret = bdrv_create_file(filename, opts, &local_err); |
75411d23 | 610 | if (ret < 0) { |
0fea6b79 | 611 | error_propagate(errp, local_err); |
75411d23 SH |
612 | return ret; |
613 | } | |
614 | ||
efaa7c4e | 615 | blk = blk_new_open(filename, NULL, NULL, |
72e775c7 | 616 | BDRV_O_RDWR | BDRV_O_PROTOCOL, &local_err); |
8a56fdad | 617 | if (blk == NULL) { |
0fea6b79 | 618 | error_propagate(errp, local_err); |
8a56fdad | 619 | return -EIO; |
75411d23 SH |
620 | } |
621 | ||
8a56fdad KW |
622 | blk_set_allow_write_beyond_eof(blk, true); |
623 | ||
c743849b | 624 | /* File must start empty and grow, check truncate is supported */ |
8a56fdad | 625 | ret = blk_truncate(blk, 0); |
c743849b SH |
626 | if (ret < 0) { |
627 | goto out; | |
628 | } | |
629 | ||
75411d23 SH |
630 | if (backing_file) { |
631 | header.features |= QED_F_BACKING_FILE; | |
632 | header.backing_filename_offset = sizeof(le_header); | |
633 | header.backing_filename_size = strlen(backing_file); | |
634 | ||
635 | if (qed_fmt_is_raw(backing_fmt)) { | |
636 | header.features |= QED_F_BACKING_FORMAT_NO_PROBE; | |
637 | } | |
638 | } | |
639 | ||
640 | qed_header_cpu_to_le(&header, &le_header); | |
8341f00d | 641 | ret = blk_pwrite(blk, 0, &le_header, sizeof(le_header), 0); |
75411d23 SH |
642 | if (ret < 0) { |
643 | goto out; | |
644 | } | |
8a56fdad | 645 | ret = blk_pwrite(blk, sizeof(le_header), backing_file, |
8341f00d | 646 | header.backing_filename_size, 0); |
75411d23 SH |
647 | if (ret < 0) { |
648 | goto out; | |
649 | } | |
650 | ||
7267c094 | 651 | l1_table = g_malloc0(l1_size); |
8341f00d | 652 | ret = blk_pwrite(blk, header.l1_table_offset, l1_table, l1_size, 0); |
75411d23 SH |
653 | if (ret < 0) { |
654 | goto out; | |
655 | } | |
656 | ||
657 | ret = 0; /* success */ | |
658 | out: | |
7267c094 | 659 | g_free(l1_table); |
8a56fdad | 660 | blk_unref(blk); |
75411d23 SH |
661 | return ret; |
662 | } | |
663 | ||
7ab74849 | 664 | static int bdrv_qed_create(const char *filename, QemuOpts *opts, Error **errp) |
75411d23 SH |
665 | { |
666 | uint64_t image_size = 0; | |
667 | uint32_t cluster_size = QED_DEFAULT_CLUSTER_SIZE; | |
668 | uint32_t table_size = QED_DEFAULT_TABLE_SIZE; | |
7ab74849 CL |
669 | char *backing_file = NULL; |
670 | char *backing_fmt = NULL; | |
671 | int ret; | |
672 | ||
c2eb918e HT |
673 | image_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), |
674 | BDRV_SECTOR_SIZE); | |
7ab74849 CL |
675 | backing_file = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FILE); |
676 | backing_fmt = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FMT); | |
677 | cluster_size = qemu_opt_get_size_del(opts, | |
678 | BLOCK_OPT_CLUSTER_SIZE, | |
679 | QED_DEFAULT_CLUSTER_SIZE); | |
680 | table_size = qemu_opt_get_size_del(opts, BLOCK_OPT_TABLE_SIZE, | |
681 | QED_DEFAULT_TABLE_SIZE); | |
75411d23 SH |
682 | |
683 | if (!qed_is_cluster_size_valid(cluster_size)) { | |
5ff679b4 AG |
684 | error_setg(errp, "QED cluster size must be within range [%u, %u] " |
685 | "and power of 2", | |
686 | QED_MIN_CLUSTER_SIZE, QED_MAX_CLUSTER_SIZE); | |
7ab74849 CL |
687 | ret = -EINVAL; |
688 | goto finish; | |
75411d23 SH |
689 | } |
690 | if (!qed_is_table_size_valid(table_size)) { | |
5ff679b4 AG |
691 | error_setg(errp, "QED table size must be within range [%u, %u] " |
692 | "and power of 2", | |
693 | QED_MIN_TABLE_SIZE, QED_MAX_TABLE_SIZE); | |
7ab74849 CL |
694 | ret = -EINVAL; |
695 | goto finish; | |
75411d23 SH |
696 | } |
697 | if (!qed_is_image_size_valid(image_size, cluster_size, table_size)) { | |
5ff679b4 AG |
698 | error_setg(errp, "QED image size must be a non-zero multiple of " |
699 | "cluster size and less than %" PRIu64 " bytes", | |
700 | qed_max_image_size(cluster_size, table_size)); | |
7ab74849 CL |
701 | ret = -EINVAL; |
702 | goto finish; | |
75411d23 SH |
703 | } |
704 | ||
7ab74849 | 705 | ret = qed_create(filename, cluster_size, image_size, table_size, |
4ab15590 | 706 | backing_file, backing_fmt, opts, errp); |
7ab74849 CL |
707 | |
708 | finish: | |
709 | g_free(backing_file); | |
710 | g_free(backing_fmt); | |
711 | return ret; | |
75411d23 SH |
712 | } |
713 | ||
298800ca | 714 | typedef struct { |
4bc74be9 | 715 | BlockDriverState *bs; |
b7d5a5b8 | 716 | Coroutine *co; |
4bc74be9 PB |
717 | uint64_t pos; |
718 | int64_t status; | |
298800ca | 719 | int *pnum; |
53f1dfd1 | 720 | BlockDriverState **file; |
298800ca SH |
721 | } QEDIsAllocatedCB; |
722 | ||
723 | static void qed_is_allocated_cb(void *opaque, int ret, uint64_t offset, size_t len) | |
724 | { | |
725 | QEDIsAllocatedCB *cb = opaque; | |
4bc74be9 | 726 | BDRVQEDState *s = cb->bs->opaque; |
298800ca | 727 | *cb->pnum = len / BDRV_SECTOR_SIZE; |
4bc74be9 PB |
728 | switch (ret) { |
729 | case QED_CLUSTER_FOUND: | |
730 | offset |= qed_offset_into_cluster(s, cb->pos); | |
731 | cb->status = BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID | offset; | |
53f1dfd1 | 732 | *cb->file = cb->bs->file->bs; |
4bc74be9 PB |
733 | break; |
734 | case QED_CLUSTER_ZERO: | |
735 | cb->status = BDRV_BLOCK_ZERO; | |
736 | break; | |
737 | case QED_CLUSTER_L2: | |
738 | case QED_CLUSTER_L1: | |
739 | cb->status = 0; | |
740 | break; | |
741 | default: | |
742 | assert(ret < 0); | |
743 | cb->status = ret; | |
744 | break; | |
745 | } | |
746 | ||
b7d5a5b8 | 747 | if (cb->co) { |
b9e413dd | 748 | aio_co_wake(cb->co); |
b7d5a5b8 | 749 | } |
298800ca SH |
750 | } |
751 | ||
b6b8a333 | 752 | static int64_t coroutine_fn bdrv_qed_co_get_block_status(BlockDriverState *bs, |
b7d5a5b8 | 753 | int64_t sector_num, |
67a0fd2a FZ |
754 | int nb_sectors, int *pnum, |
755 | BlockDriverState **file) | |
75411d23 | 756 | { |
298800ca | 757 | BDRVQEDState *s = bs->opaque; |
298800ca SH |
758 | size_t len = (size_t)nb_sectors * BDRV_SECTOR_SIZE; |
759 | QEDIsAllocatedCB cb = { | |
4bc74be9 PB |
760 | .bs = bs, |
761 | .pos = (uint64_t)sector_num * BDRV_SECTOR_SIZE, | |
762 | .status = BDRV_BLOCK_OFFSET_MASK, | |
298800ca | 763 | .pnum = pnum, |
53f1dfd1 | 764 | .file = file, |
298800ca SH |
765 | }; |
766 | QEDRequest request = { .l2_table = NULL }; | |
767 | ||
4bc74be9 | 768 | qed_find_cluster(s, &request, cb.pos, len, qed_is_allocated_cb, &cb); |
298800ca | 769 | |
b7d5a5b8 | 770 | /* Now sleep if the callback wasn't invoked immediately */ |
4bc74be9 | 771 | while (cb.status == BDRV_BLOCK_OFFSET_MASK) { |
b7d5a5b8 SH |
772 | cb.co = qemu_coroutine_self(); |
773 | qemu_coroutine_yield(); | |
298800ca SH |
774 | } |
775 | ||
298800ca SH |
776 | qed_unref_l2_cache_entry(request.l2_table); |
777 | ||
4bc74be9 | 778 | return cb.status; |
75411d23 SH |
779 | } |
780 | ||
eabba580 SH |
781 | static BDRVQEDState *acb_to_s(QEDAIOCB *acb) |
782 | { | |
783 | return acb->common.bs->opaque; | |
784 | } | |
785 | ||
786 | /** | |
787 | * Read from the backing file or zero-fill if no backing file | |
788 | * | |
f06ee3d4 KW |
789 | * @s: QED state |
790 | * @pos: Byte position in device | |
791 | * @qiov: Destination I/O vector | |
792 | * @backing_qiov: Possibly shortened copy of qiov, to be allocated here | |
793 | * @cb: Completion function | |
794 | * @opaque: User data for completion function | |
eabba580 SH |
795 | * |
796 | * This function reads qiov->size bytes starting at pos from the backing file. | |
797 | * If there is no backing file then zeroes are read. | |
798 | */ | |
799 | static void qed_read_backing_file(BDRVQEDState *s, uint64_t pos, | |
800 | QEMUIOVector *qiov, | |
f06ee3d4 | 801 | QEMUIOVector **backing_qiov, |
097310b5 | 802 | BlockCompletionFunc *cb, void *opaque) |
eabba580 | 803 | { |
eabba580 SH |
804 | uint64_t backing_length = 0; |
805 | size_t size; | |
806 | ||
807 | /* If there is a backing file, get its length. Treat the absence of a | |
808 | * backing file like a zero length backing file. | |
809 | */ | |
760e0063 KW |
810 | if (s->bs->backing) { |
811 | int64_t l = bdrv_getlength(s->bs->backing->bs); | |
eabba580 SH |
812 | if (l < 0) { |
813 | cb(opaque, l); | |
814 | return; | |
815 | } | |
816 | backing_length = l; | |
817 | } | |
818 | ||
819 | /* Zero all sectors if reading beyond the end of the backing file */ | |
820 | if (pos >= backing_length || | |
821 | pos + qiov->size > backing_length) { | |
3d9b4925 | 822 | qemu_iovec_memset(qiov, 0, 0, qiov->size); |
eabba580 SH |
823 | } |
824 | ||
825 | /* Complete now if there are no backing file sectors to read */ | |
826 | if (pos >= backing_length) { | |
827 | cb(opaque, 0); | |
828 | return; | |
829 | } | |
830 | ||
831 | /* If the read straddles the end of the backing file, shorten it */ | |
832 | size = MIN((uint64_t)backing_length - pos, qiov->size); | |
833 | ||
f06ee3d4 KW |
834 | assert(*backing_qiov == NULL); |
835 | *backing_qiov = g_new(QEMUIOVector, 1); | |
836 | qemu_iovec_init(*backing_qiov, qiov->niov); | |
837 | qemu_iovec_concat(*backing_qiov, qiov, 0, size); | |
838 | ||
820100fd | 839 | BLKDBG_EVENT(s->bs->file, BLKDBG_READ_BACKING_AIO); |
ebb7af21 | 840 | bdrv_aio_readv(s->bs->backing, pos / BDRV_SECTOR_SIZE, |
f06ee3d4 | 841 | *backing_qiov, size / BDRV_SECTOR_SIZE, cb, opaque); |
eabba580 SH |
842 | } |
843 | ||
844 | typedef struct { | |
845 | GenericCB gencb; | |
846 | BDRVQEDState *s; | |
847 | QEMUIOVector qiov; | |
f06ee3d4 | 848 | QEMUIOVector *backing_qiov; |
eabba580 SH |
849 | struct iovec iov; |
850 | uint64_t offset; | |
851 | } CopyFromBackingFileCB; | |
852 | ||
853 | static void qed_copy_from_backing_file_cb(void *opaque, int ret) | |
854 | { | |
855 | CopyFromBackingFileCB *copy_cb = opaque; | |
856 | qemu_vfree(copy_cb->iov.iov_base); | |
857 | gencb_complete(©_cb->gencb, ret); | |
858 | } | |
859 | ||
860 | static void qed_copy_from_backing_file_write(void *opaque, int ret) | |
861 | { | |
862 | CopyFromBackingFileCB *copy_cb = opaque; | |
863 | BDRVQEDState *s = copy_cb->s; | |
eabba580 | 864 | |
f06ee3d4 KW |
865 | if (copy_cb->backing_qiov) { |
866 | qemu_iovec_destroy(copy_cb->backing_qiov); | |
867 | g_free(copy_cb->backing_qiov); | |
868 | copy_cb->backing_qiov = NULL; | |
869 | } | |
870 | ||
eabba580 SH |
871 | if (ret) { |
872 | qed_copy_from_backing_file_cb(copy_cb, ret); | |
873 | return; | |
874 | } | |
875 | ||
876 | BLKDBG_EVENT(s->bs->file, BLKDBG_COW_WRITE); | |
0d1049c7 | 877 | bdrv_aio_writev(s->bs->file, copy_cb->offset / BDRV_SECTOR_SIZE, |
ad54ae80 PB |
878 | ©_cb->qiov, copy_cb->qiov.size / BDRV_SECTOR_SIZE, |
879 | qed_copy_from_backing_file_cb, copy_cb); | |
eabba580 SH |
880 | } |
881 | ||
882 | /** | |
883 | * Copy data from backing file into the image | |
884 | * | |
885 | * @s: QED state | |
886 | * @pos: Byte position in device | |
887 | * @len: Number of bytes | |
888 | * @offset: Byte offset in image file | |
889 | * @cb: Completion function | |
890 | * @opaque: User data for completion function | |
891 | */ | |
892 | static void qed_copy_from_backing_file(BDRVQEDState *s, uint64_t pos, | |
893 | uint64_t len, uint64_t offset, | |
097310b5 | 894 | BlockCompletionFunc *cb, |
eabba580 SH |
895 | void *opaque) |
896 | { | |
897 | CopyFromBackingFileCB *copy_cb; | |
898 | ||
899 | /* Skip copy entirely if there is no work to do */ | |
900 | if (len == 0) { | |
901 | cb(opaque, 0); | |
902 | return; | |
903 | } | |
904 | ||
905 | copy_cb = gencb_alloc(sizeof(*copy_cb), cb, opaque); | |
906 | copy_cb->s = s; | |
907 | copy_cb->offset = offset; | |
f06ee3d4 | 908 | copy_cb->backing_qiov = NULL; |
eabba580 SH |
909 | copy_cb->iov.iov_base = qemu_blockalign(s->bs, len); |
910 | copy_cb->iov.iov_len = len; | |
911 | qemu_iovec_init_external(©_cb->qiov, ©_cb->iov, 1); | |
912 | ||
f06ee3d4 | 913 | qed_read_backing_file(s, pos, ©_cb->qiov, ©_cb->backing_qiov, |
eabba580 SH |
914 | qed_copy_from_backing_file_write, copy_cb); |
915 | } | |
916 | ||
917 | /** | |
918 | * Link one or more contiguous clusters into a table | |
919 | * | |
920 | * @s: QED state | |
921 | * @table: L2 table | |
922 | * @index: First cluster index | |
923 | * @n: Number of contiguous clusters | |
21df65b6 AL |
924 | * @cluster: First cluster offset |
925 | * | |
926 | * The cluster offset may be an allocated byte offset in the image file, the | |
927 | * zero cluster marker, or the unallocated cluster marker. | |
eabba580 SH |
928 | */ |
929 | static void qed_update_l2_table(BDRVQEDState *s, QEDTable *table, int index, | |
930 | unsigned int n, uint64_t cluster) | |
931 | { | |
932 | int i; | |
933 | for (i = index; i < index + n; i++) { | |
934 | table->offsets[i] = cluster; | |
21df65b6 AL |
935 | if (!qed_offset_is_unalloc_cluster(cluster) && |
936 | !qed_offset_is_zero_cluster(cluster)) { | |
937 | cluster += s->header.cluster_size; | |
938 | } | |
eabba580 SH |
939 | } |
940 | } | |
941 | ||
942 | static void qed_aio_complete_bh(void *opaque) | |
943 | { | |
944 | QEDAIOCB *acb = opaque; | |
1919631e | 945 | BDRVQEDState *s = acb_to_s(acb); |
097310b5 | 946 | BlockCompletionFunc *cb = acb->common.cb; |
eabba580 SH |
947 | void *user_opaque = acb->common.opaque; |
948 | int ret = acb->bh_ret; | |
eabba580 | 949 | |
8007429a | 950 | qemu_aio_unref(acb); |
eabba580 SH |
951 | |
952 | /* Invoke callback */ | |
1919631e | 953 | qed_acquire(s); |
eabba580 | 954 | cb(user_opaque, ret); |
1919631e | 955 | qed_release(s); |
eabba580 SH |
956 | } |
957 | ||
958 | static void qed_aio_complete(QEDAIOCB *acb, int ret) | |
959 | { | |
960 | BDRVQEDState *s = acb_to_s(acb); | |
961 | ||
962 | trace_qed_aio_complete(s, acb, ret); | |
963 | ||
964 | /* Free resources */ | |
965 | qemu_iovec_destroy(&acb->cur_qiov); | |
966 | qed_unref_l2_cache_entry(acb->request.l2_table); | |
967 | ||
0e71be19 SH |
968 | /* Free the buffer we may have allocated for zero writes */ |
969 | if (acb->flags & QED_AIOCB_ZERO) { | |
970 | qemu_vfree(acb->qiov->iov[0].iov_base); | |
971 | acb->qiov->iov[0].iov_base = NULL; | |
972 | } | |
973 | ||
eabba580 SH |
974 | /* Arrange for a bh to invoke the completion function */ |
975 | acb->bh_ret = ret; | |
fffb6e12 PB |
976 | aio_bh_schedule_oneshot(bdrv_get_aio_context(acb->common.bs), |
977 | qed_aio_complete_bh, acb); | |
eabba580 SH |
978 | |
979 | /* Start next allocating write request waiting behind this one. Note that | |
980 | * requests enqueue themselves when they first hit an unallocated cluster | |
981 | * but they wait until the entire request is finished before waking up the | |
982 | * next request in the queue. This ensures that we don't cycle through | |
983 | * requests multiple times but rather finish one at a time completely. | |
984 | */ | |
985 | if (acb == QSIMPLEQ_FIRST(&s->allocating_write_reqs)) { | |
986 | QSIMPLEQ_REMOVE_HEAD(&s->allocating_write_reqs, next); | |
987 | acb = QSIMPLEQ_FIRST(&s->allocating_write_reqs); | |
988 | if (acb) { | |
b20123a2 | 989 | qed_aio_start_io(acb); |
6f321e93 SH |
990 | } else if (s->header.features & QED_F_NEED_CHECK) { |
991 | qed_start_need_check_timer(s); | |
eabba580 SH |
992 | } |
993 | } | |
994 | } | |
995 | ||
996 | /** | |
997 | * Commit the current L2 table to the cache | |
998 | */ | |
999 | static void qed_commit_l2_update(void *opaque, int ret) | |
1000 | { | |
1001 | QEDAIOCB *acb = opaque; | |
1002 | BDRVQEDState *s = acb_to_s(acb); | |
1003 | CachedL2Table *l2_table = acb->request.l2_table; | |
e4fc8781 | 1004 | uint64_t l2_offset = l2_table->offset; |
eabba580 SH |
1005 | |
1006 | qed_commit_l2_cache_entry(&s->l2_cache, l2_table); | |
1007 | ||
1008 | /* This is guaranteed to succeed because we just committed the entry to the | |
1009 | * cache. | |
1010 | */ | |
e4fc8781 | 1011 | acb->request.l2_table = qed_find_l2_cache_entry(&s->l2_cache, l2_offset); |
eabba580 SH |
1012 | assert(acb->request.l2_table != NULL); |
1013 | ||
b20123a2 | 1014 | qed_aio_next_io(acb, ret); |
eabba580 SH |
1015 | } |
1016 | ||
1017 | /** | |
1018 | * Update L1 table with new L2 table offset and write it out | |
1019 | */ | |
1020 | static void qed_aio_write_l1_update(void *opaque, int ret) | |
1021 | { | |
1022 | QEDAIOCB *acb = opaque; | |
1023 | BDRVQEDState *s = acb_to_s(acb); | |
1024 | int index; | |
1025 | ||
1026 | if (ret) { | |
1027 | qed_aio_complete(acb, ret); | |
1028 | return; | |
1029 | } | |
1030 | ||
1031 | index = qed_l1_index(s, acb->cur_pos); | |
1032 | s->l1_table->offsets[index] = acb->request.l2_table->offset; | |
1033 | ||
1034 | qed_write_l1_table(s, index, 1, qed_commit_l2_update, acb); | |
1035 | } | |
1036 | ||
1037 | /** | |
1038 | * Update L2 table with new cluster offsets and write them out | |
1039 | */ | |
0e71be19 | 1040 | static void qed_aio_write_l2_update(QEDAIOCB *acb, int ret, uint64_t offset) |
eabba580 | 1041 | { |
eabba580 SH |
1042 | BDRVQEDState *s = acb_to_s(acb); |
1043 | bool need_alloc = acb->find_cluster_ret == QED_CLUSTER_L1; | |
1044 | int index; | |
1045 | ||
1046 | if (ret) { | |
1047 | goto err; | |
1048 | } | |
1049 | ||
1050 | if (need_alloc) { | |
1051 | qed_unref_l2_cache_entry(acb->request.l2_table); | |
1052 | acb->request.l2_table = qed_new_l2_table(s); | |
1053 | } | |
1054 | ||
1055 | index = qed_l2_index(s, acb->cur_pos); | |
1056 | qed_update_l2_table(s, acb->request.l2_table->table, index, acb->cur_nclusters, | |
0e71be19 | 1057 | offset); |
eabba580 SH |
1058 | |
1059 | if (need_alloc) { | |
1060 | /* Write out the whole new L2 table */ | |
1061 | qed_write_l2_table(s, &acb->request, 0, s->table_nelems, true, | |
b20123a2 | 1062 | qed_aio_write_l1_update, acb); |
eabba580 SH |
1063 | } else { |
1064 | /* Write out only the updated part of the L2 table */ | |
1065 | qed_write_l2_table(s, &acb->request, index, acb->cur_nclusters, false, | |
b20123a2 | 1066 | qed_aio_next_io_cb, acb); |
eabba580 SH |
1067 | } |
1068 | return; | |
1069 | ||
1070 | err: | |
1071 | qed_aio_complete(acb, ret); | |
1072 | } | |
1073 | ||
0e71be19 SH |
1074 | static void qed_aio_write_l2_update_cb(void *opaque, int ret) |
1075 | { | |
1076 | QEDAIOCB *acb = opaque; | |
1077 | qed_aio_write_l2_update(acb, ret, acb->cur_cluster); | |
1078 | } | |
1079 | ||
eabba580 SH |
1080 | /** |
1081 | * Flush new data clusters before updating the L2 table | |
1082 | * | |
1083 | * This flush is necessary when a backing file is in use. A crash during an | |
1084 | * allocating write could result in empty clusters in the image. If the write | |
1085 | * only touched a subregion of the cluster, then backing image sectors have | |
1086 | * been lost in the untouched region. The solution is to flush after writing a | |
1087 | * new data cluster and before updating the L2 table. | |
1088 | */ | |
1089 | static void qed_aio_write_flush_before_l2_update(void *opaque, int ret) | |
1090 | { | |
1091 | QEDAIOCB *acb = opaque; | |
1092 | BDRVQEDState *s = acb_to_s(acb); | |
1093 | ||
9a4f4c31 | 1094 | if (!bdrv_aio_flush(s->bs->file->bs, qed_aio_write_l2_update_cb, opaque)) { |
eabba580 SH |
1095 | qed_aio_complete(acb, -EIO); |
1096 | } | |
1097 | } | |
1098 | ||
1099 | /** | |
1100 | * Write data to the image file | |
1101 | */ | |
1102 | static void qed_aio_write_main(void *opaque, int ret) | |
1103 | { | |
1104 | QEDAIOCB *acb = opaque; | |
1105 | BDRVQEDState *s = acb_to_s(acb); | |
1106 | uint64_t offset = acb->cur_cluster + | |
1107 | qed_offset_into_cluster(s, acb->cur_pos); | |
097310b5 | 1108 | BlockCompletionFunc *next_fn; |
eabba580 SH |
1109 | |
1110 | trace_qed_aio_write_main(s, acb, ret, offset, acb->cur_qiov.size); | |
1111 | ||
1112 | if (ret) { | |
1113 | qed_aio_complete(acb, ret); | |
1114 | return; | |
1115 | } | |
1116 | ||
1117 | if (acb->find_cluster_ret == QED_CLUSTER_FOUND) { | |
b20123a2 | 1118 | next_fn = qed_aio_next_io_cb; |
eabba580 | 1119 | } else { |
760e0063 | 1120 | if (s->bs->backing) { |
eabba580 SH |
1121 | next_fn = qed_aio_write_flush_before_l2_update; |
1122 | } else { | |
0e71be19 | 1123 | next_fn = qed_aio_write_l2_update_cb; |
eabba580 SH |
1124 | } |
1125 | } | |
1126 | ||
1127 | BLKDBG_EVENT(s->bs->file, BLKDBG_WRITE_AIO); | |
0d1049c7 | 1128 | bdrv_aio_writev(s->bs->file, offset / BDRV_SECTOR_SIZE, |
ad54ae80 PB |
1129 | &acb->cur_qiov, acb->cur_qiov.size / BDRV_SECTOR_SIZE, |
1130 | next_fn, acb); | |
eabba580 SH |
1131 | } |
1132 | ||
1133 | /** | |
1134 | * Populate back untouched region of new data cluster | |
1135 | */ | |
1136 | static void qed_aio_write_postfill(void *opaque, int ret) | |
1137 | { | |
1138 | QEDAIOCB *acb = opaque; | |
1139 | BDRVQEDState *s = acb_to_s(acb); | |
1140 | uint64_t start = acb->cur_pos + acb->cur_qiov.size; | |
1141 | uint64_t len = | |
1142 | qed_start_of_cluster(s, start + s->header.cluster_size - 1) - start; | |
1143 | uint64_t offset = acb->cur_cluster + | |
1144 | qed_offset_into_cluster(s, acb->cur_pos) + | |
1145 | acb->cur_qiov.size; | |
1146 | ||
1147 | if (ret) { | |
1148 | qed_aio_complete(acb, ret); | |
1149 | return; | |
1150 | } | |
1151 | ||
1152 | trace_qed_aio_write_postfill(s, acb, start, len, offset); | |
1153 | qed_copy_from_backing_file(s, start, len, offset, | |
1154 | qed_aio_write_main, acb); | |
1155 | } | |
1156 | ||
1157 | /** | |
1158 | * Populate front untouched region of new data cluster | |
1159 | */ | |
1160 | static void qed_aio_write_prefill(void *opaque, int ret) | |
1161 | { | |
1162 | QEDAIOCB *acb = opaque; | |
1163 | BDRVQEDState *s = acb_to_s(acb); | |
1164 | uint64_t start = qed_start_of_cluster(s, acb->cur_pos); | |
1165 | uint64_t len = qed_offset_into_cluster(s, acb->cur_pos); | |
1166 | ||
1167 | trace_qed_aio_write_prefill(s, acb, start, len, acb->cur_cluster); | |
1168 | qed_copy_from_backing_file(s, start, len, acb->cur_cluster, | |
1169 | qed_aio_write_postfill, acb); | |
1170 | } | |
1171 | ||
0d09c797 SH |
1172 | /** |
1173 | * Check if the QED_F_NEED_CHECK bit should be set during allocating write | |
1174 | */ | |
1175 | static bool qed_should_set_need_check(BDRVQEDState *s) | |
1176 | { | |
1177 | /* The flush before L2 update path ensures consistency */ | |
760e0063 | 1178 | if (s->bs->backing) { |
0d09c797 SH |
1179 | return false; |
1180 | } | |
1181 | ||
1182 | return !(s->header.features & QED_F_NEED_CHECK); | |
1183 | } | |
1184 | ||
0e71be19 SH |
1185 | static void qed_aio_write_zero_cluster(void *opaque, int ret) |
1186 | { | |
1187 | QEDAIOCB *acb = opaque; | |
1188 | ||
1189 | if (ret) { | |
1190 | qed_aio_complete(acb, ret); | |
1191 | return; | |
1192 | } | |
1193 | ||
1194 | qed_aio_write_l2_update(acb, 0, 1); | |
1195 | } | |
1196 | ||
eabba580 SH |
1197 | /** |
1198 | * Write new data cluster | |
1199 | * | |
1200 | * @acb: Write request | |
1201 | * @len: Length in bytes | |
1202 | * | |
1203 | * This path is taken when writing to previously unallocated clusters. | |
1204 | */ | |
1205 | static void qed_aio_write_alloc(QEDAIOCB *acb, size_t len) | |
1206 | { | |
1207 | BDRVQEDState *s = acb_to_s(acb); | |
097310b5 | 1208 | BlockCompletionFunc *cb; |
eabba580 | 1209 | |
6f321e93 SH |
1210 | /* Cancel timer when the first allocating request comes in */ |
1211 | if (QSIMPLEQ_EMPTY(&s->allocating_write_reqs)) { | |
1212 | qed_cancel_need_check_timer(s); | |
1213 | } | |
1214 | ||
eabba580 SH |
1215 | /* Freeze this request if another allocating write is in progress */ |
1216 | if (acb != QSIMPLEQ_FIRST(&s->allocating_write_reqs)) { | |
1217 | QSIMPLEQ_INSERT_TAIL(&s->allocating_write_reqs, acb, next); | |
1218 | } | |
6f321e93 SH |
1219 | if (acb != QSIMPLEQ_FIRST(&s->allocating_write_reqs) || |
1220 | s->allocating_write_reqs_plugged) { | |
eabba580 SH |
1221 | return; /* wait for existing request to finish */ |
1222 | } | |
1223 | ||
1224 | acb->cur_nclusters = qed_bytes_to_clusters(s, | |
1225 | qed_offset_into_cluster(s, acb->cur_pos) + len); | |
1b093c48 | 1226 | qemu_iovec_concat(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len); |
eabba580 | 1227 | |
0e71be19 SH |
1228 | if (acb->flags & QED_AIOCB_ZERO) { |
1229 | /* Skip ahead if the clusters are already zero */ | |
1230 | if (acb->find_cluster_ret == QED_CLUSTER_ZERO) { | |
b20123a2 | 1231 | qed_aio_start_io(acb); |
0e71be19 SH |
1232 | return; |
1233 | } | |
1234 | ||
1235 | cb = qed_aio_write_zero_cluster; | |
1236 | } else { | |
1237 | cb = qed_aio_write_prefill; | |
1238 | acb->cur_cluster = qed_alloc_clusters(s, acb->cur_nclusters); | |
1239 | } | |
1240 | ||
0d09c797 SH |
1241 | if (qed_should_set_need_check(s)) { |
1242 | s->header.features |= QED_F_NEED_CHECK; | |
0e71be19 | 1243 | qed_write_header(s, cb, acb); |
0d09c797 | 1244 | } else { |
0e71be19 | 1245 | cb(acb, 0); |
01979a98 | 1246 | } |
eabba580 SH |
1247 | } |
1248 | ||
1249 | /** | |
1250 | * Write data cluster in place | |
1251 | * | |
1252 | * @acb: Write request | |
1253 | * @offset: Cluster offset in bytes | |
1254 | * @len: Length in bytes | |
1255 | * | |
1256 | * This path is taken when writing to already allocated clusters. | |
1257 | */ | |
1258 | static void qed_aio_write_inplace(QEDAIOCB *acb, uint64_t offset, size_t len) | |
1259 | { | |
0e71be19 SH |
1260 | /* Allocate buffer for zero writes */ |
1261 | if (acb->flags & QED_AIOCB_ZERO) { | |
1262 | struct iovec *iov = acb->qiov->iov; | |
1263 | ||
1264 | if (!iov->iov_base) { | |
4f4896db KW |
1265 | iov->iov_base = qemu_try_blockalign(acb->common.bs, iov->iov_len); |
1266 | if (iov->iov_base == NULL) { | |
1267 | qed_aio_complete(acb, -ENOMEM); | |
1268 | return; | |
1269 | } | |
0e71be19 SH |
1270 | memset(iov->iov_base, 0, iov->iov_len); |
1271 | } | |
1272 | } | |
1273 | ||
eabba580 SH |
1274 | /* Calculate the I/O vector */ |
1275 | acb->cur_cluster = offset; | |
1b093c48 | 1276 | qemu_iovec_concat(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len); |
eabba580 SH |
1277 | |
1278 | /* Do the actual write */ | |
1279 | qed_aio_write_main(acb, 0); | |
1280 | } | |
1281 | ||
1282 | /** | |
1283 | * Write data cluster | |
1284 | * | |
1285 | * @opaque: Write request | |
1286 | * @ret: QED_CLUSTER_FOUND, QED_CLUSTER_L2, QED_CLUSTER_L1, | |
1287 | * or -errno | |
1288 | * @offset: Cluster offset in bytes | |
1289 | * @len: Length in bytes | |
1290 | * | |
1291 | * Callback from qed_find_cluster(). | |
1292 | */ | |
1293 | static void qed_aio_write_data(void *opaque, int ret, | |
1294 | uint64_t offset, size_t len) | |
1295 | { | |
1296 | QEDAIOCB *acb = opaque; | |
1297 | ||
1298 | trace_qed_aio_write_data(acb_to_s(acb), acb, ret, offset, len); | |
1299 | ||
1300 | acb->find_cluster_ret = ret; | |
1301 | ||
1302 | switch (ret) { | |
1303 | case QED_CLUSTER_FOUND: | |
1304 | qed_aio_write_inplace(acb, offset, len); | |
1305 | break; | |
1306 | ||
1307 | case QED_CLUSTER_L2: | |
1308 | case QED_CLUSTER_L1: | |
21df65b6 | 1309 | case QED_CLUSTER_ZERO: |
eabba580 SH |
1310 | qed_aio_write_alloc(acb, len); |
1311 | break; | |
1312 | ||
1313 | default: | |
1314 | qed_aio_complete(acb, ret); | |
1315 | break; | |
1316 | } | |
1317 | } | |
1318 | ||
1319 | /** | |
1320 | * Read data cluster | |
1321 | * | |
1322 | * @opaque: Read request | |
1323 | * @ret: QED_CLUSTER_FOUND, QED_CLUSTER_L2, QED_CLUSTER_L1, | |
1324 | * or -errno | |
1325 | * @offset: Cluster offset in bytes | |
1326 | * @len: Length in bytes | |
1327 | * | |
1328 | * Callback from qed_find_cluster(). | |
1329 | */ | |
1330 | static void qed_aio_read_data(void *opaque, int ret, | |
1331 | uint64_t offset, size_t len) | |
1332 | { | |
1333 | QEDAIOCB *acb = opaque; | |
1334 | BDRVQEDState *s = acb_to_s(acb); | |
1335 | BlockDriverState *bs = acb->common.bs; | |
eabba580 SH |
1336 | |
1337 | /* Adjust offset into cluster */ | |
1338 | offset += qed_offset_into_cluster(s, acb->cur_pos); | |
1339 | ||
1340 | trace_qed_aio_read_data(s, acb, ret, offset, len); | |
1341 | ||
1342 | if (ret < 0) { | |
1343 | goto err; | |
1344 | } | |
1345 | ||
1b093c48 | 1346 | qemu_iovec_concat(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len); |
eabba580 | 1347 | |
21df65b6 AL |
1348 | /* Handle zero cluster and backing file reads */ |
1349 | if (ret == QED_CLUSTER_ZERO) { | |
3d9b4925 | 1350 | qemu_iovec_memset(&acb->cur_qiov, 0, 0, acb->cur_qiov.size); |
b20123a2 | 1351 | qed_aio_start_io(acb); |
21df65b6 AL |
1352 | return; |
1353 | } else if (ret != QED_CLUSTER_FOUND) { | |
eabba580 | 1354 | qed_read_backing_file(s, acb->cur_pos, &acb->cur_qiov, |
b20123a2 | 1355 | &acb->backing_qiov, qed_aio_next_io_cb, acb); |
eabba580 SH |
1356 | return; |
1357 | } | |
1358 | ||
1359 | BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO); | |
ebb7af21 | 1360 | bdrv_aio_readv(bs->file, offset / BDRV_SECTOR_SIZE, |
ad54ae80 | 1361 | &acb->cur_qiov, acb->cur_qiov.size / BDRV_SECTOR_SIZE, |
b20123a2 | 1362 | qed_aio_next_io_cb, acb); |
eabba580 SH |
1363 | return; |
1364 | ||
1365 | err: | |
1366 | qed_aio_complete(acb, ret); | |
1367 | } | |
1368 | ||
1369 | /** | |
1370 | * Begin next I/O or complete the request | |
1371 | */ | |
b20123a2 | 1372 | static void qed_aio_next_io(QEDAIOCB *acb, int ret) |
eabba580 | 1373 | { |
eabba580 | 1374 | BDRVQEDState *s = acb_to_s(acb); |
6e4f59bd SH |
1375 | QEDFindClusterFunc *io_fn = (acb->flags & QED_AIOCB_WRITE) ? |
1376 | qed_aio_write_data : qed_aio_read_data; | |
eabba580 SH |
1377 | |
1378 | trace_qed_aio_next_io(s, acb, ret, acb->cur_pos + acb->cur_qiov.size); | |
1379 | ||
f06ee3d4 KW |
1380 | if (acb->backing_qiov) { |
1381 | qemu_iovec_destroy(acb->backing_qiov); | |
1382 | g_free(acb->backing_qiov); | |
1383 | acb->backing_qiov = NULL; | |
1384 | } | |
1385 | ||
eabba580 SH |
1386 | /* Handle I/O error */ |
1387 | if (ret) { | |
1388 | qed_aio_complete(acb, ret); | |
1389 | return; | |
1390 | } | |
1391 | ||
1392 | acb->qiov_offset += acb->cur_qiov.size; | |
1393 | acb->cur_pos += acb->cur_qiov.size; | |
1394 | qemu_iovec_reset(&acb->cur_qiov); | |
1395 | ||
1396 | /* Complete request */ | |
1397 | if (acb->cur_pos >= acb->end_pos) { | |
1398 | qed_aio_complete(acb, 0); | |
1399 | return; | |
1400 | } | |
1401 | ||
1402 | /* Find next cluster and start I/O */ | |
1403 | qed_find_cluster(s, &acb->request, | |
1404 | acb->cur_pos, acb->end_pos - acb->cur_pos, | |
1405 | io_fn, acb); | |
1406 | } | |
1407 | ||
7c84b1b8 MA |
1408 | static BlockAIOCB *qed_aio_setup(BlockDriverState *bs, |
1409 | int64_t sector_num, | |
1410 | QEMUIOVector *qiov, int nb_sectors, | |
097310b5 | 1411 | BlockCompletionFunc *cb, |
7c84b1b8 | 1412 | void *opaque, int flags) |
eabba580 | 1413 | { |
d7331bed | 1414 | QEDAIOCB *acb = qemu_aio_get(&qed_aiocb_info, bs, cb, opaque); |
eabba580 SH |
1415 | |
1416 | trace_qed_aio_setup(bs->opaque, acb, sector_num, nb_sectors, | |
6e4f59bd | 1417 | opaque, flags); |
eabba580 | 1418 | |
6e4f59bd | 1419 | acb->flags = flags; |
eabba580 SH |
1420 | acb->qiov = qiov; |
1421 | acb->qiov_offset = 0; | |
1422 | acb->cur_pos = (uint64_t)sector_num * BDRV_SECTOR_SIZE; | |
1423 | acb->end_pos = acb->cur_pos + nb_sectors * BDRV_SECTOR_SIZE; | |
f06ee3d4 | 1424 | acb->backing_qiov = NULL; |
eabba580 SH |
1425 | acb->request.l2_table = NULL; |
1426 | qemu_iovec_init(&acb->cur_qiov, qiov->niov); | |
1427 | ||
1428 | /* Start request */ | |
b20123a2 | 1429 | qed_aio_start_io(acb); |
eabba580 SH |
1430 | return &acb->common; |
1431 | } | |
1432 | ||
7c84b1b8 MA |
1433 | static BlockAIOCB *bdrv_qed_aio_readv(BlockDriverState *bs, |
1434 | int64_t sector_num, | |
1435 | QEMUIOVector *qiov, int nb_sectors, | |
097310b5 | 1436 | BlockCompletionFunc *cb, |
7c84b1b8 | 1437 | void *opaque) |
75411d23 | 1438 | { |
6e4f59bd | 1439 | return qed_aio_setup(bs, sector_num, qiov, nb_sectors, cb, opaque, 0); |
75411d23 SH |
1440 | } |
1441 | ||
7c84b1b8 MA |
1442 | static BlockAIOCB *bdrv_qed_aio_writev(BlockDriverState *bs, |
1443 | int64_t sector_num, | |
1444 | QEMUIOVector *qiov, int nb_sectors, | |
097310b5 | 1445 | BlockCompletionFunc *cb, |
7c84b1b8 | 1446 | void *opaque) |
75411d23 | 1447 | { |
6e4f59bd SH |
1448 | return qed_aio_setup(bs, sector_num, qiov, nb_sectors, cb, |
1449 | opaque, QED_AIOCB_WRITE); | |
75411d23 SH |
1450 | } |
1451 | ||
0e71be19 SH |
1452 | typedef struct { |
1453 | Coroutine *co; | |
1454 | int ret; | |
1455 | bool done; | |
1456 | } QEDWriteZeroesCB; | |
1457 | ||
49a2e483 | 1458 | static void coroutine_fn qed_co_pwrite_zeroes_cb(void *opaque, int ret) |
0e71be19 SH |
1459 | { |
1460 | QEDWriteZeroesCB *cb = opaque; | |
1461 | ||
1462 | cb->done = true; | |
1463 | cb->ret = ret; | |
1464 | if (cb->co) { | |
b9e413dd | 1465 | aio_co_wake(cb->co); |
0e71be19 SH |
1466 | } |
1467 | } | |
1468 | ||
49a2e483 EB |
1469 | static int coroutine_fn bdrv_qed_co_pwrite_zeroes(BlockDriverState *bs, |
1470 | int64_t offset, | |
1471 | int count, | |
1472 | BdrvRequestFlags flags) | |
0e71be19 | 1473 | { |
7c84b1b8 | 1474 | BlockAIOCB *blockacb; |
ef72f76e | 1475 | BDRVQEDState *s = bs->opaque; |
0e71be19 SH |
1476 | QEDWriteZeroesCB cb = { .done = false }; |
1477 | QEMUIOVector qiov; | |
1478 | struct iovec iov; | |
1479 | ||
49a2e483 EB |
1480 | /* Fall back if the request is not aligned */ |
1481 | if (qed_offset_into_cluster(s, offset) || | |
1482 | qed_offset_into_cluster(s, count)) { | |
1483 | return -ENOTSUP; | |
ef72f76e SH |
1484 | } |
1485 | ||
0e71be19 SH |
1486 | /* Zero writes start without an I/O buffer. If a buffer becomes necessary |
1487 | * then it will be allocated during request processing. | |
1488 | */ | |
49a2e483 EB |
1489 | iov.iov_base = NULL; |
1490 | iov.iov_len = count; | |
0e71be19 SH |
1491 | |
1492 | qemu_iovec_init_external(&qiov, &iov, 1); | |
49a2e483 EB |
1493 | blockacb = qed_aio_setup(bs, offset >> BDRV_SECTOR_BITS, &qiov, |
1494 | count >> BDRV_SECTOR_BITS, | |
1495 | qed_co_pwrite_zeroes_cb, &cb, | |
0e71be19 SH |
1496 | QED_AIOCB_WRITE | QED_AIOCB_ZERO); |
1497 | if (!blockacb) { | |
1498 | return -EIO; | |
1499 | } | |
1500 | if (!cb.done) { | |
1501 | cb.co = qemu_coroutine_self(); | |
1502 | qemu_coroutine_yield(); | |
1503 | } | |
1504 | assert(cb.done); | |
1505 | return cb.ret; | |
1506 | } | |
1507 | ||
75411d23 SH |
1508 | static int bdrv_qed_truncate(BlockDriverState *bs, int64_t offset) |
1509 | { | |
77a5a000 SH |
1510 | BDRVQEDState *s = bs->opaque; |
1511 | uint64_t old_image_size; | |
1512 | int ret; | |
1513 | ||
1514 | if (!qed_is_image_size_valid(offset, s->header.cluster_size, | |
1515 | s->header.table_size)) { | |
1516 | return -EINVAL; | |
1517 | } | |
1518 | ||
1519 | /* Shrinking is currently not supported */ | |
1520 | if ((uint64_t)offset < s->header.image_size) { | |
1521 | return -ENOTSUP; | |
1522 | } | |
1523 | ||
1524 | old_image_size = s->header.image_size; | |
1525 | s->header.image_size = offset; | |
1526 | ret = qed_write_header_sync(s); | |
1527 | if (ret < 0) { | |
1528 | s->header.image_size = old_image_size; | |
1529 | } | |
1530 | return ret; | |
75411d23 SH |
1531 | } |
1532 | ||
1533 | static int64_t bdrv_qed_getlength(BlockDriverState *bs) | |
1534 | { | |
1535 | BDRVQEDState *s = bs->opaque; | |
1536 | return s->header.image_size; | |
1537 | } | |
1538 | ||
1539 | static int bdrv_qed_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) | |
1540 | { | |
1541 | BDRVQEDState *s = bs->opaque; | |
1542 | ||
1543 | memset(bdi, 0, sizeof(*bdi)); | |
1544 | bdi->cluster_size = s->header.cluster_size; | |
d68dbee8 | 1545 | bdi->is_dirty = s->header.features & QED_F_NEED_CHECK; |
95de6d70 PB |
1546 | bdi->unallocated_blocks_are_zero = true; |
1547 | bdi->can_write_zeroes_with_unmap = true; | |
75411d23 SH |
1548 | return 0; |
1549 | } | |
1550 | ||
1551 | static int bdrv_qed_change_backing_file(BlockDriverState *bs, | |
1552 | const char *backing_file, | |
1553 | const char *backing_fmt) | |
1554 | { | |
1555 | BDRVQEDState *s = bs->opaque; | |
1556 | QEDHeader new_header, le_header; | |
1557 | void *buffer; | |
1558 | size_t buffer_len, backing_file_len; | |
1559 | int ret; | |
1560 | ||
1561 | /* Refuse to set backing filename if unknown compat feature bits are | |
1562 | * active. If the image uses an unknown compat feature then we may not | |
1563 | * know the layout of data following the header structure and cannot safely | |
1564 | * add a new string. | |
1565 | */ | |
1566 | if (backing_file && (s->header.compat_features & | |
1567 | ~QED_COMPAT_FEATURE_MASK)) { | |
1568 | return -ENOTSUP; | |
1569 | } | |
1570 | ||
1571 | memcpy(&new_header, &s->header, sizeof(new_header)); | |
1572 | ||
1573 | new_header.features &= ~(QED_F_BACKING_FILE | | |
1574 | QED_F_BACKING_FORMAT_NO_PROBE); | |
1575 | ||
1576 | /* Adjust feature flags */ | |
1577 | if (backing_file) { | |
1578 | new_header.features |= QED_F_BACKING_FILE; | |
1579 | ||
1580 | if (qed_fmt_is_raw(backing_fmt)) { | |
1581 | new_header.features |= QED_F_BACKING_FORMAT_NO_PROBE; | |
1582 | } | |
1583 | } | |
1584 | ||
1585 | /* Calculate new header size */ | |
1586 | backing_file_len = 0; | |
1587 | ||
1588 | if (backing_file) { | |
1589 | backing_file_len = strlen(backing_file); | |
1590 | } | |
1591 | ||
1592 | buffer_len = sizeof(new_header); | |
1593 | new_header.backing_filename_offset = buffer_len; | |
1594 | new_header.backing_filename_size = backing_file_len; | |
1595 | buffer_len += backing_file_len; | |
1596 | ||
1597 | /* Make sure we can rewrite header without failing */ | |
1598 | if (buffer_len > new_header.header_size * new_header.cluster_size) { | |
1599 | return -ENOSPC; | |
1600 | } | |
1601 | ||
1602 | /* Prepare new header */ | |
7267c094 | 1603 | buffer = g_malloc(buffer_len); |
75411d23 SH |
1604 | |
1605 | qed_header_cpu_to_le(&new_header, &le_header); | |
1606 | memcpy(buffer, &le_header, sizeof(le_header)); | |
1607 | buffer_len = sizeof(le_header); | |
1608 | ||
feba23b1 PB |
1609 | if (backing_file) { |
1610 | memcpy(buffer + buffer_len, backing_file, backing_file_len); | |
1611 | buffer_len += backing_file_len; | |
1612 | } | |
75411d23 SH |
1613 | |
1614 | /* Write new header */ | |
d9ca2ea2 | 1615 | ret = bdrv_pwrite_sync(bs->file, 0, buffer, buffer_len); |
7267c094 | 1616 | g_free(buffer); |
75411d23 SH |
1617 | if (ret == 0) { |
1618 | memcpy(&s->header, &new_header, sizeof(new_header)); | |
1619 | } | |
1620 | return ret; | |
1621 | } | |
1622 | ||
5a8a30db | 1623 | static void bdrv_qed_invalidate_cache(BlockDriverState *bs, Error **errp) |
c82954e5 BC |
1624 | { |
1625 | BDRVQEDState *s = bs->opaque; | |
5a8a30db KW |
1626 | Error *local_err = NULL; |
1627 | int ret; | |
c82954e5 BC |
1628 | |
1629 | bdrv_qed_close(bs); | |
3456a8d1 | 1630 | |
c82954e5 | 1631 | memset(s, 0, sizeof(BDRVQEDState)); |
5a8a30db KW |
1632 | ret = bdrv_qed_open(bs, NULL, bs->open_flags, &local_err); |
1633 | if (local_err) { | |
e43bfd9c MA |
1634 | error_propagate(errp, local_err); |
1635 | error_prepend(errp, "Could not reopen qed layer: "); | |
5a8a30db KW |
1636 | return; |
1637 | } else if (ret < 0) { | |
1638 | error_setg_errno(errp, -ret, "Could not reopen qed layer"); | |
1639 | return; | |
1640 | } | |
c82954e5 BC |
1641 | } |
1642 | ||
4534ff54 KW |
1643 | static int bdrv_qed_check(BlockDriverState *bs, BdrvCheckResult *result, |
1644 | BdrvCheckMode fix) | |
75411d23 | 1645 | { |
01979a98 SH |
1646 | BDRVQEDState *s = bs->opaque; |
1647 | ||
4534ff54 | 1648 | return qed_check(s, result, !!fix); |
75411d23 SH |
1649 | } |
1650 | ||
7ab74849 CL |
1651 | static QemuOptsList qed_create_opts = { |
1652 | .name = "qed-create-opts", | |
1653 | .head = QTAILQ_HEAD_INITIALIZER(qed_create_opts.head), | |
1654 | .desc = { | |
1655 | { | |
1656 | .name = BLOCK_OPT_SIZE, | |
1657 | .type = QEMU_OPT_SIZE, | |
1658 | .help = "Virtual disk size" | |
1659 | }, | |
1660 | { | |
1661 | .name = BLOCK_OPT_BACKING_FILE, | |
1662 | .type = QEMU_OPT_STRING, | |
1663 | .help = "File name of a base image" | |
1664 | }, | |
1665 | { | |
1666 | .name = BLOCK_OPT_BACKING_FMT, | |
1667 | .type = QEMU_OPT_STRING, | |
1668 | .help = "Image format of the base image" | |
1669 | }, | |
1670 | { | |
1671 | .name = BLOCK_OPT_CLUSTER_SIZE, | |
1672 | .type = QEMU_OPT_SIZE, | |
1673 | .help = "Cluster size (in bytes)", | |
1674 | .def_value_str = stringify(QED_DEFAULT_CLUSTER_SIZE) | |
1675 | }, | |
1676 | { | |
1677 | .name = BLOCK_OPT_TABLE_SIZE, | |
1678 | .type = QEMU_OPT_SIZE, | |
1679 | .help = "L1/L2 table size (in clusters)" | |
1680 | }, | |
1681 | { /* end of list */ } | |
1682 | } | |
75411d23 SH |
1683 | }; |
1684 | ||
1685 | static BlockDriver bdrv_qed = { | |
1686 | .format_name = "qed", | |
1687 | .instance_size = sizeof(BDRVQEDState), | |
7ab74849 | 1688 | .create_opts = &qed_create_opts, |
8ee79e70 | 1689 | .supports_backing = true, |
75411d23 SH |
1690 | |
1691 | .bdrv_probe = bdrv_qed_probe, | |
1692 | .bdrv_open = bdrv_qed_open, | |
1693 | .bdrv_close = bdrv_qed_close, | |
f9cb20f1 | 1694 | .bdrv_reopen_prepare = bdrv_qed_reopen_prepare, |
c282e1fd | 1695 | .bdrv_create = bdrv_qed_create, |
3ac21627 | 1696 | .bdrv_has_zero_init = bdrv_has_zero_init_1, |
b6b8a333 | 1697 | .bdrv_co_get_block_status = bdrv_qed_co_get_block_status, |
75411d23 SH |
1698 | .bdrv_aio_readv = bdrv_qed_aio_readv, |
1699 | .bdrv_aio_writev = bdrv_qed_aio_writev, | |
49a2e483 | 1700 | .bdrv_co_pwrite_zeroes = bdrv_qed_co_pwrite_zeroes, |
75411d23 SH |
1701 | .bdrv_truncate = bdrv_qed_truncate, |
1702 | .bdrv_getlength = bdrv_qed_getlength, | |
1703 | .bdrv_get_info = bdrv_qed_get_info, | |
d34682cd | 1704 | .bdrv_refresh_limits = bdrv_qed_refresh_limits, |
75411d23 | 1705 | .bdrv_change_backing_file = bdrv_qed_change_backing_file, |
c82954e5 | 1706 | .bdrv_invalidate_cache = bdrv_qed_invalidate_cache, |
75411d23 | 1707 | .bdrv_check = bdrv_qed_check, |
a8c868c3 SH |
1708 | .bdrv_detach_aio_context = bdrv_qed_detach_aio_context, |
1709 | .bdrv_attach_aio_context = bdrv_qed_attach_aio_context, | |
6653a73d | 1710 | .bdrv_drain = bdrv_qed_drain, |
75411d23 SH |
1711 | }; |
1712 | ||
1713 | static void bdrv_qed_init(void) | |
1714 | { | |
1715 | bdrv_register(&bdrv_qed); | |
1716 | } | |
1717 | ||
1718 | block_init(bdrv_qed_init); |