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