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