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