]>
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 | ||
453e53e2 KW |
1003 | ret = qed_write_l1_table(s, index, 1); |
1004 | qed_commit_l2_update(acb, ret); | |
eabba580 SH |
1005 | } |
1006 | ||
1007 | /** | |
1008 | * Update L2 table with new cluster offsets and write them out | |
1009 | */ | |
0e71be19 | 1010 | static void qed_aio_write_l2_update(QEDAIOCB *acb, int ret, uint64_t offset) |
eabba580 | 1011 | { |
eabba580 SH |
1012 | BDRVQEDState *s = acb_to_s(acb); |
1013 | bool need_alloc = acb->find_cluster_ret == QED_CLUSTER_L1; | |
1014 | int index; | |
1015 | ||
1016 | if (ret) { | |
1017 | goto err; | |
1018 | } | |
1019 | ||
1020 | if (need_alloc) { | |
1021 | qed_unref_l2_cache_entry(acb->request.l2_table); | |
1022 | acb->request.l2_table = qed_new_l2_table(s); | |
1023 | } | |
1024 | ||
1025 | index = qed_l2_index(s, acb->cur_pos); | |
1026 | qed_update_l2_table(s, acb->request.l2_table->table, index, acb->cur_nclusters, | |
0e71be19 | 1027 | offset); |
eabba580 SH |
1028 | |
1029 | if (need_alloc) { | |
1030 | /* Write out the whole new L2 table */ | |
453e53e2 KW |
1031 | ret = qed_write_l2_table(s, &acb->request, 0, s->table_nelems, true); |
1032 | qed_aio_write_l1_update(acb, ret); | |
eabba580 SH |
1033 | } else { |
1034 | /* Write out only the updated part of the L2 table */ | |
453e53e2 KW |
1035 | ret = qed_write_l2_table(s, &acb->request, index, acb->cur_nclusters, |
1036 | false); | |
1037 | qed_aio_next_io(acb, ret); | |
eabba580 SH |
1038 | } |
1039 | return; | |
1040 | ||
1041 | err: | |
1042 | qed_aio_complete(acb, ret); | |
1043 | } | |
1044 | ||
0e71be19 SH |
1045 | static void qed_aio_write_l2_update_cb(void *opaque, int ret) |
1046 | { | |
1047 | QEDAIOCB *acb = opaque; | |
1048 | qed_aio_write_l2_update(acb, ret, acb->cur_cluster); | |
1049 | } | |
1050 | ||
eabba580 SH |
1051 | /** |
1052 | * Flush new data clusters before updating the L2 table | |
1053 | * | |
1054 | * This flush is necessary when a backing file is in use. A crash during an | |
1055 | * allocating write could result in empty clusters in the image. If the write | |
1056 | * only touched a subregion of the cluster, then backing image sectors have | |
1057 | * been lost in the untouched region. The solution is to flush after writing a | |
1058 | * new data cluster and before updating the L2 table. | |
1059 | */ | |
1060 | static void qed_aio_write_flush_before_l2_update(void *opaque, int ret) | |
1061 | { | |
1062 | QEDAIOCB *acb = opaque; | |
1063 | BDRVQEDState *s = acb_to_s(acb); | |
1064 | ||
9a4f4c31 | 1065 | if (!bdrv_aio_flush(s->bs->file->bs, qed_aio_write_l2_update_cb, opaque)) { |
eabba580 SH |
1066 | qed_aio_complete(acb, -EIO); |
1067 | } | |
1068 | } | |
1069 | ||
1070 | /** | |
1071 | * Write data to the image file | |
1072 | */ | |
1073 | static void qed_aio_write_main(void *opaque, int ret) | |
1074 | { | |
1075 | QEDAIOCB *acb = opaque; | |
1076 | BDRVQEDState *s = acb_to_s(acb); | |
1077 | uint64_t offset = acb->cur_cluster + | |
1078 | qed_offset_into_cluster(s, acb->cur_pos); | |
097310b5 | 1079 | BlockCompletionFunc *next_fn; |
eabba580 SH |
1080 | |
1081 | trace_qed_aio_write_main(s, acb, ret, offset, acb->cur_qiov.size); | |
1082 | ||
1083 | if (ret) { | |
1084 | qed_aio_complete(acb, ret); | |
1085 | return; | |
1086 | } | |
1087 | ||
1088 | if (acb->find_cluster_ret == QED_CLUSTER_FOUND) { | |
b20123a2 | 1089 | next_fn = qed_aio_next_io_cb; |
eabba580 | 1090 | } else { |
760e0063 | 1091 | if (s->bs->backing) { |
eabba580 SH |
1092 | next_fn = qed_aio_write_flush_before_l2_update; |
1093 | } else { | |
0e71be19 | 1094 | next_fn = qed_aio_write_l2_update_cb; |
eabba580 SH |
1095 | } |
1096 | } | |
1097 | ||
1098 | BLKDBG_EVENT(s->bs->file, BLKDBG_WRITE_AIO); | |
0d1049c7 | 1099 | bdrv_aio_writev(s->bs->file, offset / BDRV_SECTOR_SIZE, |
ad54ae80 PB |
1100 | &acb->cur_qiov, acb->cur_qiov.size / BDRV_SECTOR_SIZE, |
1101 | next_fn, acb); | |
eabba580 SH |
1102 | } |
1103 | ||
1104 | /** | |
b4ac32f3 | 1105 | * Populate untouched regions of new data cluster |
eabba580 | 1106 | */ |
b4ac32f3 | 1107 | static void qed_aio_write_cow(void *opaque, int ret) |
eabba580 SH |
1108 | { |
1109 | QEDAIOCB *acb = opaque; | |
1110 | BDRVQEDState *s = acb_to_s(acb); | |
b4ac32f3 | 1111 | uint64_t start, len, offset; |
eabba580 | 1112 | |
b4ac32f3 KW |
1113 | /* Populate front untouched region of new data cluster */ |
1114 | start = qed_start_of_cluster(s, acb->cur_pos); | |
1115 | len = qed_offset_into_cluster(s, acb->cur_pos); | |
1116 | ||
1117 | trace_qed_aio_write_prefill(s, acb, start, len, acb->cur_cluster); | |
1118 | ret = qed_copy_from_backing_file(s, start, len, acb->cur_cluster); | |
eabba580 SH |
1119 | if (ret) { |
1120 | qed_aio_complete(acb, ret); | |
1121 | return; | |
1122 | } | |
1123 | ||
b4ac32f3 KW |
1124 | /* Populate back untouched region of new data cluster */ |
1125 | start = acb->cur_pos + acb->cur_qiov.size; | |
1126 | len = qed_start_of_cluster(s, start + s->header.cluster_size - 1) - start; | |
1127 | offset = acb->cur_cluster + | |
1128 | qed_offset_into_cluster(s, acb->cur_pos) + | |
1129 | acb->cur_qiov.size; | |
eabba580 | 1130 | |
b4ac32f3 KW |
1131 | trace_qed_aio_write_postfill(s, acb, start, len, offset); |
1132 | ret = qed_copy_from_backing_file(s, start, len, offset); | |
eabba580 | 1133 | |
b4ac32f3 | 1134 | qed_aio_write_main(acb, ret); |
eabba580 SH |
1135 | } |
1136 | ||
0d09c797 SH |
1137 | /** |
1138 | * Check if the QED_F_NEED_CHECK bit should be set during allocating write | |
1139 | */ | |
1140 | static bool qed_should_set_need_check(BDRVQEDState *s) | |
1141 | { | |
1142 | /* The flush before L2 update path ensures consistency */ | |
760e0063 | 1143 | if (s->bs->backing) { |
0d09c797 SH |
1144 | return false; |
1145 | } | |
1146 | ||
1147 | return !(s->header.features & QED_F_NEED_CHECK); | |
1148 | } | |
1149 | ||
0e71be19 SH |
1150 | static void qed_aio_write_zero_cluster(void *opaque, int ret) |
1151 | { | |
1152 | QEDAIOCB *acb = opaque; | |
1153 | ||
1154 | if (ret) { | |
1155 | qed_aio_complete(acb, ret); | |
1156 | return; | |
1157 | } | |
1158 | ||
1159 | qed_aio_write_l2_update(acb, 0, 1); | |
1160 | } | |
1161 | ||
eabba580 SH |
1162 | /** |
1163 | * Write new data cluster | |
1164 | * | |
1165 | * @acb: Write request | |
1166 | * @len: Length in bytes | |
1167 | * | |
1168 | * This path is taken when writing to previously unallocated clusters. | |
1169 | */ | |
1170 | static void qed_aio_write_alloc(QEDAIOCB *acb, size_t len) | |
1171 | { | |
1172 | BDRVQEDState *s = acb_to_s(acb); | |
097310b5 | 1173 | BlockCompletionFunc *cb; |
f13d712b | 1174 | int ret; |
eabba580 | 1175 | |
6f321e93 SH |
1176 | /* Cancel timer when the first allocating request comes in */ |
1177 | if (QSIMPLEQ_EMPTY(&s->allocating_write_reqs)) { | |
1178 | qed_cancel_need_check_timer(s); | |
1179 | } | |
1180 | ||
eabba580 SH |
1181 | /* Freeze this request if another allocating write is in progress */ |
1182 | if (acb != QSIMPLEQ_FIRST(&s->allocating_write_reqs)) { | |
1183 | QSIMPLEQ_INSERT_TAIL(&s->allocating_write_reqs, acb, next); | |
1184 | } | |
6f321e93 SH |
1185 | if (acb != QSIMPLEQ_FIRST(&s->allocating_write_reqs) || |
1186 | s->allocating_write_reqs_plugged) { | |
eabba580 SH |
1187 | return; /* wait for existing request to finish */ |
1188 | } | |
1189 | ||
1190 | acb->cur_nclusters = qed_bytes_to_clusters(s, | |
1191 | qed_offset_into_cluster(s, acb->cur_pos) + len); | |
1b093c48 | 1192 | qemu_iovec_concat(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len); |
eabba580 | 1193 | |
0e71be19 SH |
1194 | if (acb->flags & QED_AIOCB_ZERO) { |
1195 | /* Skip ahead if the clusters are already zero */ | |
1196 | if (acb->find_cluster_ret == QED_CLUSTER_ZERO) { | |
b20123a2 | 1197 | qed_aio_start_io(acb); |
0e71be19 SH |
1198 | return; |
1199 | } | |
1200 | ||
1201 | cb = qed_aio_write_zero_cluster; | |
1202 | } else { | |
b4ac32f3 | 1203 | cb = qed_aio_write_cow; |
0e71be19 SH |
1204 | acb->cur_cluster = qed_alloc_clusters(s, acb->cur_nclusters); |
1205 | } | |
1206 | ||
0d09c797 SH |
1207 | if (qed_should_set_need_check(s)) { |
1208 | s->header.features |= QED_F_NEED_CHECK; | |
f13d712b KW |
1209 | ret = qed_write_header(s); |
1210 | cb(acb, ret); | |
0d09c797 | 1211 | } else { |
0e71be19 | 1212 | cb(acb, 0); |
01979a98 | 1213 | } |
eabba580 SH |
1214 | } |
1215 | ||
1216 | /** | |
1217 | * Write data cluster in place | |
1218 | * | |
1219 | * @acb: Write request | |
1220 | * @offset: Cluster offset in bytes | |
1221 | * @len: Length in bytes | |
1222 | * | |
1223 | * This path is taken when writing to already allocated clusters. | |
1224 | */ | |
1225 | static void qed_aio_write_inplace(QEDAIOCB *acb, uint64_t offset, size_t len) | |
1226 | { | |
0e71be19 SH |
1227 | /* Allocate buffer for zero writes */ |
1228 | if (acb->flags & QED_AIOCB_ZERO) { | |
1229 | struct iovec *iov = acb->qiov->iov; | |
1230 | ||
1231 | if (!iov->iov_base) { | |
4f4896db KW |
1232 | iov->iov_base = qemu_try_blockalign(acb->common.bs, iov->iov_len); |
1233 | if (iov->iov_base == NULL) { | |
1234 | qed_aio_complete(acb, -ENOMEM); | |
1235 | return; | |
1236 | } | |
0e71be19 SH |
1237 | memset(iov->iov_base, 0, iov->iov_len); |
1238 | } | |
1239 | } | |
1240 | ||
eabba580 SH |
1241 | /* Calculate the I/O vector */ |
1242 | acb->cur_cluster = offset; | |
1b093c48 | 1243 | qemu_iovec_concat(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len); |
eabba580 SH |
1244 | |
1245 | /* Do the actual write */ | |
1246 | qed_aio_write_main(acb, 0); | |
1247 | } | |
1248 | ||
1249 | /** | |
1250 | * Write data cluster | |
1251 | * | |
1252 | * @opaque: Write request | |
1253 | * @ret: QED_CLUSTER_FOUND, QED_CLUSTER_L2, QED_CLUSTER_L1, | |
1254 | * or -errno | |
1255 | * @offset: Cluster offset in bytes | |
1256 | * @len: Length in bytes | |
eabba580 SH |
1257 | */ |
1258 | static void qed_aio_write_data(void *opaque, int ret, | |
1259 | uint64_t offset, size_t len) | |
1260 | { | |
1261 | QEDAIOCB *acb = opaque; | |
1262 | ||
1263 | trace_qed_aio_write_data(acb_to_s(acb), acb, ret, offset, len); | |
1264 | ||
1265 | acb->find_cluster_ret = ret; | |
1266 | ||
1267 | switch (ret) { | |
1268 | case QED_CLUSTER_FOUND: | |
1269 | qed_aio_write_inplace(acb, offset, len); | |
1270 | break; | |
1271 | ||
1272 | case QED_CLUSTER_L2: | |
1273 | case QED_CLUSTER_L1: | |
21df65b6 | 1274 | case QED_CLUSTER_ZERO: |
eabba580 SH |
1275 | qed_aio_write_alloc(acb, len); |
1276 | break; | |
1277 | ||
1278 | default: | |
1279 | qed_aio_complete(acb, ret); | |
1280 | break; | |
1281 | } | |
1282 | } | |
1283 | ||
1284 | /** | |
1285 | * Read data cluster | |
1286 | * | |
1287 | * @opaque: Read request | |
1288 | * @ret: QED_CLUSTER_FOUND, QED_CLUSTER_L2, QED_CLUSTER_L1, | |
1289 | * or -errno | |
1290 | * @offset: Cluster offset in bytes | |
1291 | * @len: Length in bytes | |
eabba580 SH |
1292 | */ |
1293 | static void qed_aio_read_data(void *opaque, int ret, | |
1294 | uint64_t offset, size_t len) | |
1295 | { | |
1296 | QEDAIOCB *acb = opaque; | |
1297 | BDRVQEDState *s = acb_to_s(acb); | |
1298 | BlockDriverState *bs = acb->common.bs; | |
eabba580 SH |
1299 | |
1300 | /* Adjust offset into cluster */ | |
1301 | offset += qed_offset_into_cluster(s, acb->cur_pos); | |
1302 | ||
1303 | trace_qed_aio_read_data(s, acb, ret, offset, len); | |
1304 | ||
1305 | if (ret < 0) { | |
1306 | goto err; | |
1307 | } | |
1308 | ||
1b093c48 | 1309 | qemu_iovec_concat(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len); |
eabba580 | 1310 | |
21df65b6 AL |
1311 | /* Handle zero cluster and backing file reads */ |
1312 | if (ret == QED_CLUSTER_ZERO) { | |
3d9b4925 | 1313 | qemu_iovec_memset(&acb->cur_qiov, 0, 0, acb->cur_qiov.size); |
b20123a2 | 1314 | qed_aio_start_io(acb); |
21df65b6 AL |
1315 | return; |
1316 | } else if (ret != QED_CLUSTER_FOUND) { | |
e85c5281 KW |
1317 | ret = qed_read_backing_file(s, acb->cur_pos, &acb->cur_qiov, |
1318 | &acb->backing_qiov); | |
1319 | qed_aio_next_io(acb, ret); | |
eabba580 SH |
1320 | return; |
1321 | } | |
1322 | ||
1323 | BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO); | |
3e248cdc KW |
1324 | ret = bdrv_preadv(bs->file, offset, &acb->cur_qiov); |
1325 | if (ret < 0) { | |
1326 | goto err; | |
1327 | } | |
1328 | qed_aio_next_io(acb, 0); | |
eabba580 SH |
1329 | return; |
1330 | ||
1331 | err: | |
1332 | qed_aio_complete(acb, ret); | |
1333 | } | |
1334 | ||
1335 | /** | |
1336 | * Begin next I/O or complete the request | |
1337 | */ | |
b20123a2 | 1338 | static void qed_aio_next_io(QEDAIOCB *acb, int ret) |
eabba580 | 1339 | { |
eabba580 | 1340 | BDRVQEDState *s = acb_to_s(acb); |
6e4f59bd SH |
1341 | QEDFindClusterFunc *io_fn = (acb->flags & QED_AIOCB_WRITE) ? |
1342 | qed_aio_write_data : qed_aio_read_data; | |
0f21b7a1 KW |
1343 | uint64_t offset; |
1344 | size_t len; | |
eabba580 SH |
1345 | |
1346 | trace_qed_aio_next_io(s, acb, ret, acb->cur_pos + acb->cur_qiov.size); | |
1347 | ||
f06ee3d4 KW |
1348 | if (acb->backing_qiov) { |
1349 | qemu_iovec_destroy(acb->backing_qiov); | |
1350 | g_free(acb->backing_qiov); | |
1351 | acb->backing_qiov = NULL; | |
1352 | } | |
1353 | ||
eabba580 SH |
1354 | /* Handle I/O error */ |
1355 | if (ret) { | |
1356 | qed_aio_complete(acb, ret); | |
1357 | return; | |
1358 | } | |
1359 | ||
1360 | acb->qiov_offset += acb->cur_qiov.size; | |
1361 | acb->cur_pos += acb->cur_qiov.size; | |
1362 | qemu_iovec_reset(&acb->cur_qiov); | |
1363 | ||
1364 | /* Complete request */ | |
1365 | if (acb->cur_pos >= acb->end_pos) { | |
1366 | qed_aio_complete(acb, 0); | |
1367 | return; | |
1368 | } | |
1369 | ||
1370 | /* Find next cluster and start I/O */ | |
0f21b7a1 KW |
1371 | len = acb->end_pos - acb->cur_pos; |
1372 | ret = qed_find_cluster(s, &acb->request, acb->cur_pos, &len, &offset); | |
1373 | io_fn(acb, ret, offset, len); | |
eabba580 SH |
1374 | } |
1375 | ||
7c84b1b8 MA |
1376 | static BlockAIOCB *qed_aio_setup(BlockDriverState *bs, |
1377 | int64_t sector_num, | |
1378 | QEMUIOVector *qiov, int nb_sectors, | |
097310b5 | 1379 | BlockCompletionFunc *cb, |
7c84b1b8 | 1380 | void *opaque, int flags) |
eabba580 | 1381 | { |
d7331bed | 1382 | QEDAIOCB *acb = qemu_aio_get(&qed_aiocb_info, bs, cb, opaque); |
eabba580 SH |
1383 | |
1384 | trace_qed_aio_setup(bs->opaque, acb, sector_num, nb_sectors, | |
6e4f59bd | 1385 | opaque, flags); |
eabba580 | 1386 | |
6e4f59bd | 1387 | acb->flags = flags; |
eabba580 SH |
1388 | acb->qiov = qiov; |
1389 | acb->qiov_offset = 0; | |
1390 | acb->cur_pos = (uint64_t)sector_num * BDRV_SECTOR_SIZE; | |
1391 | acb->end_pos = acb->cur_pos + nb_sectors * BDRV_SECTOR_SIZE; | |
f06ee3d4 | 1392 | acb->backing_qiov = NULL; |
eabba580 SH |
1393 | acb->request.l2_table = NULL; |
1394 | qemu_iovec_init(&acb->cur_qiov, qiov->niov); | |
1395 | ||
1396 | /* Start request */ | |
b20123a2 | 1397 | qed_aio_start_io(acb); |
eabba580 SH |
1398 | return &acb->common; |
1399 | } | |
1400 | ||
7c84b1b8 MA |
1401 | static BlockAIOCB *bdrv_qed_aio_readv(BlockDriverState *bs, |
1402 | int64_t sector_num, | |
1403 | QEMUIOVector *qiov, int nb_sectors, | |
097310b5 | 1404 | BlockCompletionFunc *cb, |
7c84b1b8 | 1405 | void *opaque) |
75411d23 | 1406 | { |
6e4f59bd | 1407 | return qed_aio_setup(bs, sector_num, qiov, nb_sectors, cb, opaque, 0); |
75411d23 SH |
1408 | } |
1409 | ||
7c84b1b8 MA |
1410 | static BlockAIOCB *bdrv_qed_aio_writev(BlockDriverState *bs, |
1411 | int64_t sector_num, | |
1412 | QEMUIOVector *qiov, int nb_sectors, | |
097310b5 | 1413 | BlockCompletionFunc *cb, |
7c84b1b8 | 1414 | void *opaque) |
75411d23 | 1415 | { |
6e4f59bd SH |
1416 | return qed_aio_setup(bs, sector_num, qiov, nb_sectors, cb, |
1417 | opaque, QED_AIOCB_WRITE); | |
75411d23 SH |
1418 | } |
1419 | ||
0e71be19 SH |
1420 | typedef struct { |
1421 | Coroutine *co; | |
1422 | int ret; | |
1423 | bool done; | |
1424 | } QEDWriteZeroesCB; | |
1425 | ||
49a2e483 | 1426 | static void coroutine_fn qed_co_pwrite_zeroes_cb(void *opaque, int ret) |
0e71be19 SH |
1427 | { |
1428 | QEDWriteZeroesCB *cb = opaque; | |
1429 | ||
1430 | cb->done = true; | |
1431 | cb->ret = ret; | |
1432 | if (cb->co) { | |
b9e413dd | 1433 | aio_co_wake(cb->co); |
0e71be19 SH |
1434 | } |
1435 | } | |
1436 | ||
49a2e483 EB |
1437 | static int coroutine_fn bdrv_qed_co_pwrite_zeroes(BlockDriverState *bs, |
1438 | int64_t offset, | |
1439 | int count, | |
1440 | BdrvRequestFlags flags) | |
0e71be19 | 1441 | { |
7c84b1b8 | 1442 | BlockAIOCB *blockacb; |
ef72f76e | 1443 | BDRVQEDState *s = bs->opaque; |
0e71be19 SH |
1444 | QEDWriteZeroesCB cb = { .done = false }; |
1445 | QEMUIOVector qiov; | |
1446 | struct iovec iov; | |
1447 | ||
49a2e483 EB |
1448 | /* Fall back if the request is not aligned */ |
1449 | if (qed_offset_into_cluster(s, offset) || | |
1450 | qed_offset_into_cluster(s, count)) { | |
1451 | return -ENOTSUP; | |
ef72f76e SH |
1452 | } |
1453 | ||
0e71be19 SH |
1454 | /* Zero writes start without an I/O buffer. If a buffer becomes necessary |
1455 | * then it will be allocated during request processing. | |
1456 | */ | |
49a2e483 EB |
1457 | iov.iov_base = NULL; |
1458 | iov.iov_len = count; | |
0e71be19 SH |
1459 | |
1460 | qemu_iovec_init_external(&qiov, &iov, 1); | |
49a2e483 EB |
1461 | blockacb = qed_aio_setup(bs, offset >> BDRV_SECTOR_BITS, &qiov, |
1462 | count >> BDRV_SECTOR_BITS, | |
1463 | qed_co_pwrite_zeroes_cb, &cb, | |
0e71be19 SH |
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 | ||
4bff28b8 | 1476 | static int bdrv_qed_truncate(BlockDriverState *bs, int64_t offset, Error **errp) |
75411d23 | 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)) { | |
f59adb32 | 1484 | error_setg(errp, "Invalid image size specified"); |
77a5a000 SH |
1485 | return -EINVAL; |
1486 | } | |
1487 | ||
77a5a000 | 1488 | if ((uint64_t)offset < s->header.image_size) { |
f59adb32 | 1489 | error_setg(errp, "Shrinking images is currently not supported"); |
77a5a000 SH |
1490 | return -ENOTSUP; |
1491 | } | |
1492 | ||
1493 | old_image_size = s->header.image_size; | |
1494 | s->header.image_size = offset; | |
1495 | ret = qed_write_header_sync(s); | |
1496 | if (ret < 0) { | |
1497 | s->header.image_size = old_image_size; | |
f59adb32 | 1498 | error_setg_errno(errp, -ret, "Failed to update the image size"); |
77a5a000 SH |
1499 | } |
1500 | return ret; | |
75411d23 SH |
1501 | } |
1502 | ||
1503 | static int64_t bdrv_qed_getlength(BlockDriverState *bs) | |
1504 | { | |
1505 | BDRVQEDState *s = bs->opaque; | |
1506 | return s->header.image_size; | |
1507 | } | |
1508 | ||
1509 | static int bdrv_qed_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) | |
1510 | { | |
1511 | BDRVQEDState *s = bs->opaque; | |
1512 | ||
1513 | memset(bdi, 0, sizeof(*bdi)); | |
1514 | bdi->cluster_size = s->header.cluster_size; | |
d68dbee8 | 1515 | bdi->is_dirty = s->header.features & QED_F_NEED_CHECK; |
95de6d70 PB |
1516 | bdi->unallocated_blocks_are_zero = true; |
1517 | bdi->can_write_zeroes_with_unmap = true; | |
75411d23 SH |
1518 | return 0; |
1519 | } | |
1520 | ||
1521 | static int bdrv_qed_change_backing_file(BlockDriverState *bs, | |
1522 | const char *backing_file, | |
1523 | const char *backing_fmt) | |
1524 | { | |
1525 | BDRVQEDState *s = bs->opaque; | |
1526 | QEDHeader new_header, le_header; | |
1527 | void *buffer; | |
1528 | size_t buffer_len, backing_file_len; | |
1529 | int ret; | |
1530 | ||
1531 | /* Refuse to set backing filename if unknown compat feature bits are | |
1532 | * active. If the image uses an unknown compat feature then we may not | |
1533 | * know the layout of data following the header structure and cannot safely | |
1534 | * add a new string. | |
1535 | */ | |
1536 | if (backing_file && (s->header.compat_features & | |
1537 | ~QED_COMPAT_FEATURE_MASK)) { | |
1538 | return -ENOTSUP; | |
1539 | } | |
1540 | ||
1541 | memcpy(&new_header, &s->header, sizeof(new_header)); | |
1542 | ||
1543 | new_header.features &= ~(QED_F_BACKING_FILE | | |
1544 | QED_F_BACKING_FORMAT_NO_PROBE); | |
1545 | ||
1546 | /* Adjust feature flags */ | |
1547 | if (backing_file) { | |
1548 | new_header.features |= QED_F_BACKING_FILE; | |
1549 | ||
1550 | if (qed_fmt_is_raw(backing_fmt)) { | |
1551 | new_header.features |= QED_F_BACKING_FORMAT_NO_PROBE; | |
1552 | } | |
1553 | } | |
1554 | ||
1555 | /* Calculate new header size */ | |
1556 | backing_file_len = 0; | |
1557 | ||
1558 | if (backing_file) { | |
1559 | backing_file_len = strlen(backing_file); | |
1560 | } | |
1561 | ||
1562 | buffer_len = sizeof(new_header); | |
1563 | new_header.backing_filename_offset = buffer_len; | |
1564 | new_header.backing_filename_size = backing_file_len; | |
1565 | buffer_len += backing_file_len; | |
1566 | ||
1567 | /* Make sure we can rewrite header without failing */ | |
1568 | if (buffer_len > new_header.header_size * new_header.cluster_size) { | |
1569 | return -ENOSPC; | |
1570 | } | |
1571 | ||
1572 | /* Prepare new header */ | |
7267c094 | 1573 | buffer = g_malloc(buffer_len); |
75411d23 SH |
1574 | |
1575 | qed_header_cpu_to_le(&new_header, &le_header); | |
1576 | memcpy(buffer, &le_header, sizeof(le_header)); | |
1577 | buffer_len = sizeof(le_header); | |
1578 | ||
feba23b1 PB |
1579 | if (backing_file) { |
1580 | memcpy(buffer + buffer_len, backing_file, backing_file_len); | |
1581 | buffer_len += backing_file_len; | |
1582 | } | |
75411d23 SH |
1583 | |
1584 | /* Write new header */ | |
d9ca2ea2 | 1585 | ret = bdrv_pwrite_sync(bs->file, 0, buffer, buffer_len); |
7267c094 | 1586 | g_free(buffer); |
75411d23 SH |
1587 | if (ret == 0) { |
1588 | memcpy(&s->header, &new_header, sizeof(new_header)); | |
1589 | } | |
1590 | return ret; | |
1591 | } | |
1592 | ||
5a8a30db | 1593 | static void bdrv_qed_invalidate_cache(BlockDriverState *bs, Error **errp) |
c82954e5 BC |
1594 | { |
1595 | BDRVQEDState *s = bs->opaque; | |
5a8a30db KW |
1596 | Error *local_err = NULL; |
1597 | int ret; | |
c82954e5 BC |
1598 | |
1599 | bdrv_qed_close(bs); | |
3456a8d1 | 1600 | |
c82954e5 | 1601 | memset(s, 0, sizeof(BDRVQEDState)); |
4e4bf5c4 | 1602 | ret = bdrv_qed_do_open(bs, NULL, bs->open_flags, &local_err); |
5a8a30db | 1603 | if (local_err) { |
e43bfd9c MA |
1604 | error_propagate(errp, local_err); |
1605 | error_prepend(errp, "Could not reopen qed layer: "); | |
5a8a30db KW |
1606 | return; |
1607 | } else if (ret < 0) { | |
1608 | error_setg_errno(errp, -ret, "Could not reopen qed layer"); | |
1609 | return; | |
1610 | } | |
c82954e5 BC |
1611 | } |
1612 | ||
4534ff54 KW |
1613 | static int bdrv_qed_check(BlockDriverState *bs, BdrvCheckResult *result, |
1614 | BdrvCheckMode fix) | |
75411d23 | 1615 | { |
01979a98 SH |
1616 | BDRVQEDState *s = bs->opaque; |
1617 | ||
4534ff54 | 1618 | return qed_check(s, result, !!fix); |
75411d23 SH |
1619 | } |
1620 | ||
7ab74849 CL |
1621 | static QemuOptsList qed_create_opts = { |
1622 | .name = "qed-create-opts", | |
1623 | .head = QTAILQ_HEAD_INITIALIZER(qed_create_opts.head), | |
1624 | .desc = { | |
1625 | { | |
1626 | .name = BLOCK_OPT_SIZE, | |
1627 | .type = QEMU_OPT_SIZE, | |
1628 | .help = "Virtual disk size" | |
1629 | }, | |
1630 | { | |
1631 | .name = BLOCK_OPT_BACKING_FILE, | |
1632 | .type = QEMU_OPT_STRING, | |
1633 | .help = "File name of a base image" | |
1634 | }, | |
1635 | { | |
1636 | .name = BLOCK_OPT_BACKING_FMT, | |
1637 | .type = QEMU_OPT_STRING, | |
1638 | .help = "Image format of the base image" | |
1639 | }, | |
1640 | { | |
1641 | .name = BLOCK_OPT_CLUSTER_SIZE, | |
1642 | .type = QEMU_OPT_SIZE, | |
1643 | .help = "Cluster size (in bytes)", | |
1644 | .def_value_str = stringify(QED_DEFAULT_CLUSTER_SIZE) | |
1645 | }, | |
1646 | { | |
1647 | .name = BLOCK_OPT_TABLE_SIZE, | |
1648 | .type = QEMU_OPT_SIZE, | |
1649 | .help = "L1/L2 table size (in clusters)" | |
1650 | }, | |
1651 | { /* end of list */ } | |
1652 | } | |
75411d23 SH |
1653 | }; |
1654 | ||
1655 | static BlockDriver bdrv_qed = { | |
1656 | .format_name = "qed", | |
1657 | .instance_size = sizeof(BDRVQEDState), | |
7ab74849 | 1658 | .create_opts = &qed_create_opts, |
8ee79e70 | 1659 | .supports_backing = true, |
75411d23 SH |
1660 | |
1661 | .bdrv_probe = bdrv_qed_probe, | |
1662 | .bdrv_open = bdrv_qed_open, | |
1663 | .bdrv_close = bdrv_qed_close, | |
f9cb20f1 | 1664 | .bdrv_reopen_prepare = bdrv_qed_reopen_prepare, |
862f215f | 1665 | .bdrv_child_perm = bdrv_format_default_perms, |
c282e1fd | 1666 | .bdrv_create = bdrv_qed_create, |
3ac21627 | 1667 | .bdrv_has_zero_init = bdrv_has_zero_init_1, |
b6b8a333 | 1668 | .bdrv_co_get_block_status = bdrv_qed_co_get_block_status, |
75411d23 SH |
1669 | .bdrv_aio_readv = bdrv_qed_aio_readv, |
1670 | .bdrv_aio_writev = bdrv_qed_aio_writev, | |
49a2e483 | 1671 | .bdrv_co_pwrite_zeroes = bdrv_qed_co_pwrite_zeroes, |
75411d23 SH |
1672 | .bdrv_truncate = bdrv_qed_truncate, |
1673 | .bdrv_getlength = bdrv_qed_getlength, | |
1674 | .bdrv_get_info = bdrv_qed_get_info, | |
d34682cd | 1675 | .bdrv_refresh_limits = bdrv_qed_refresh_limits, |
75411d23 | 1676 | .bdrv_change_backing_file = bdrv_qed_change_backing_file, |
c82954e5 | 1677 | .bdrv_invalidate_cache = bdrv_qed_invalidate_cache, |
75411d23 | 1678 | .bdrv_check = bdrv_qed_check, |
a8c868c3 SH |
1679 | .bdrv_detach_aio_context = bdrv_qed_detach_aio_context, |
1680 | .bdrv_attach_aio_context = bdrv_qed_attach_aio_context, | |
6653a73d | 1681 | .bdrv_drain = bdrv_qed_drain, |
75411d23 SH |
1682 | }; |
1683 | ||
1684 | static void bdrv_qed_init(void) | |
1685 | { | |
1686 | bdrv_register(&bdrv_qed); | |
1687 | } | |
1688 | ||
1689 | block_init(bdrv_qed_init); |