]> Git Repo - qemu.git/blame - block/qed.c
qapi/expr.py: enable pylint checks
[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 395 if (ret < 0) {
15ce94a6 396 error_setg(errp, "Failed to read QED header");
75411d23
SH
397 return ret;
398 }
75411d23
SH
399 qed_header_le_to_cpu(&le_header, &s->header);
400
401 if (s->header.magic != QED_MAGIC) {
76abe407
PB
402 error_setg(errp, "Image not in QED format");
403 return -EINVAL;
75411d23
SH
404 }
405 if (s->header.features & ~QED_FEATURE_MASK) {
10b758e8 406 /* image uses unsupported feature bits */
a55448b3
HR
407 error_setg(errp, "Unsupported QED features: %" PRIx64,
408 s->header.features & ~QED_FEATURE_MASK);
10b758e8 409 return -ENOTSUP;
75411d23
SH
410 }
411 if (!qed_is_cluster_size_valid(s->header.cluster_size)) {
15ce94a6 412 error_setg(errp, "QED cluster size is invalid");
75411d23
SH
413 return -EINVAL;
414 }
415
416 /* Round down file size to the last cluster */
9a4f4c31 417 file_size = bdrv_getlength(bs->file->bs);
75411d23 418 if (file_size < 0) {
15ce94a6 419 error_setg(errp, "Failed to get file length");
75411d23
SH
420 return file_size;
421 }
422 s->file_size = qed_start_of_cluster(s, file_size);
423
424 if (!qed_is_table_size_valid(s->header.table_size)) {
15ce94a6 425 error_setg(errp, "QED table size is invalid");
75411d23
SH
426 return -EINVAL;
427 }
428 if (!qed_is_image_size_valid(s->header.image_size,
429 s->header.cluster_size,
430 s->header.table_size)) {
15ce94a6 431 error_setg(errp, "QED image size is invalid");
75411d23
SH
432 return -EINVAL;
433 }
434 if (!qed_check_table_offset(s, s->header.l1_table_offset)) {
15ce94a6 435 error_setg(errp, "QED table offset is invalid");
75411d23
SH
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) {
15ce94a6 447 error_setg(errp, "QED header size is too large");
0adfa1ed
SH
448 return -EINVAL;
449 }
450
75411d23
SH
451 if ((s->header.features & QED_F_BACKING_FILE)) {
452 if ((uint64_t)s->header.backing_filename_offset +
453 s->header.backing_filename_size >
454 s->header.cluster_size * s->header.header_size) {
15ce94a6 455 error_setg(errp, "QED backing filename offset is invalid");
75411d23
SH
456 return -EINVAL;
457 }
458
cf2ab8fc 459 ret = qed_read_string(bs->file, s->header.backing_filename_offset,
998c2019
HR
460 s->header.backing_filename_size,
461 bs->auto_backing_file,
462 sizeof(bs->auto_backing_file));
75411d23 463 if (ret < 0) {
15ce94a6 464 error_setg(errp, "Failed to read backing filename");
75411d23
SH
465 return ret;
466 }
998c2019
HR
467 pstrcpy(bs->backing_file, sizeof(bs->backing_file),
468 bs->auto_backing_file);
75411d23
SH
469
470 if (s->header.features & QED_F_BACKING_FORMAT_NO_PROBE) {
471 pstrcpy(bs->backing_format, sizeof(bs->backing_format), "raw");
472 }
473 }
474
475 /* Reset unknown autoclear feature bits. This is a backwards
476 * compatibility mechanism that allows images to be opened by older
477 * programs, which "knock out" unknown feature bits. When an image is
478 * opened by a newer program again it can detect that the autoclear
479 * feature is no longer valid.
480 */
481 if ((s->header.autoclear_features & ~QED_AUTOCLEAR_FEATURE_MASK) != 0 &&
04c01a5c 482 !bdrv_is_read_only(bs->file->bs) && !(flags & BDRV_O_INACTIVE)) {
75411d23
SH
483 s->header.autoclear_features &= QED_AUTOCLEAR_FEATURE_MASK;
484
485 ret = qed_write_header_sync(s);
486 if (ret) {
15ce94a6 487 error_setg(errp, "Failed to update header");
75411d23
SH
488 return ret;
489 }
490
491 /* From here on only known autoclear feature bits are valid */
9a4f4c31 492 bdrv_flush(bs->file->bs);
75411d23
SH
493 }
494
298800ca
SH
495 s->l1_table = qed_alloc_table(s);
496 qed_init_l2_cache(&s->l2_cache);
497
498 ret = qed_read_l1_table_sync(s);
01979a98 499 if (ret) {
15ce94a6 500 error_setg(errp, "Failed to read L1 table");
01979a98
SH
501 goto out;
502 }
503
504 /* If image was not closed cleanly, check consistency */
058f8f16 505 if (!(flags & BDRV_O_CHECK) && (s->header.features & QED_F_NEED_CHECK)) {
01979a98
SH
506 /* Read-only images cannot be fixed. There is no risk of corruption
507 * since write operations are not possible. Therefore, allow
508 * potentially inconsistent images to be opened read-only. This can
509 * aid data recovery from an otherwise inconsistent image.
510 */
9a4f4c31 511 if (!bdrv_is_read_only(bs->file->bs) &&
04c01a5c 512 !(flags & BDRV_O_INACTIVE)) {
01979a98
SH
513 BdrvCheckResult result = {0};
514
515 ret = qed_check(s, &result, true);
6f321e93 516 if (ret) {
15ce94a6 517 error_setg(errp, "Image corrupted");
6f321e93
SH
518 goto out;
519 }
01979a98
SH
520 }
521 }
522
a8c868c3 523 bdrv_qed_attach_aio_context(bs, bdrv_get_aio_context(bs));
6f321e93 524
01979a98 525out:
298800ca
SH
526 if (ret) {
527 qed_free_l2_cache(&s->l2_cache);
528 qemu_vfree(s->l1_table);
529 }
75411d23
SH
530 return ret;
531}
532
9fb4dfc5
PB
533typedef struct QEDOpenCo {
534 BlockDriverState *bs;
535 QDict *options;
536 int flags;
537 Error **errp;
538 int ret;
539} QEDOpenCo;
540
541static void coroutine_fn bdrv_qed_open_entry(void *opaque)
542{
543 QEDOpenCo *qoc = opaque;
544 BDRVQEDState *s = qoc->bs->opaque;
545
546 qemu_co_mutex_lock(&s->table_lock);
547 qoc->ret = bdrv_qed_do_open(qoc->bs, qoc->options, qoc->flags, qoc->errp);
548 qemu_co_mutex_unlock(&s->table_lock);
549}
550
4e4bf5c4
KW
551static int bdrv_qed_open(BlockDriverState *bs, QDict *options, int flags,
552 Error **errp)
553{
9fb4dfc5
PB
554 QEDOpenCo qoc = {
555 .bs = bs,
556 .options = options,
557 .flags = flags,
558 .errp = errp,
559 .ret = -EINPROGRESS
560 };
561
8b1869da
HR
562 bs->file = bdrv_open_child(NULL, options, "file", bs, &child_of_bds,
563 BDRV_CHILD_IMAGE, false, errp);
4e4bf5c4
KW
564 if (!bs->file) {
565 return -EINVAL;
566 }
567
61c7887e 568 bdrv_qed_init_state(bs);
9fb4dfc5
PB
569 if (qemu_in_coroutine()) {
570 bdrv_qed_open_entry(&qoc);
571 } else {
4720cbee 572 assert(qemu_get_current_aio_context() == qemu_get_aio_context());
9fb4dfc5
PB
573 qemu_coroutine_enter(qemu_coroutine_create(bdrv_qed_open_entry, &qoc));
574 BDRV_POLL_WHILE(bs, qoc.ret == -EINPROGRESS);
575 }
576 BDRV_POLL_WHILE(bs, qoc.ret == -EINPROGRESS);
577 return qoc.ret;
4e4bf5c4
KW
578}
579
3baca891 580static void bdrv_qed_refresh_limits(BlockDriverState *bs, Error **errp)
d34682cd
KW
581{
582 BDRVQEDState *s = bs->opaque;
583
cf081fca 584 bs->bl.pwrite_zeroes_alignment = s->header.cluster_size;
d34682cd
KW
585}
586
f9cb20f1
JC
587/* We have nothing to do for QED reopen, stubs just return
588 * success */
589static int bdrv_qed_reopen_prepare(BDRVReopenState *state,
590 BlockReopenQueue *queue, Error **errp)
591{
592 return 0;
593}
594
75411d23
SH
595static void bdrv_qed_close(BlockDriverState *bs)
596{
298800ca
SH
597 BDRVQEDState *s = bs->opaque;
598
a8c868c3 599 bdrv_qed_detach_aio_context(bs);
6f321e93 600
01979a98 601 /* Ensure writes reach stable storage */
9a4f4c31 602 bdrv_flush(bs->file->bs);
01979a98
SH
603
604 /* Clean shutdown, no check required on next open */
605 if (s->header.features & QED_F_NEED_CHECK) {
606 s->header.features &= ~QED_F_NEED_CHECK;
607 qed_write_header_sync(s);
608 }
609
298800ca
SH
610 qed_free_l2_cache(&s->l2_cache);
611 qemu_vfree(s->l1_table);
75411d23
SH
612}
613
959355a4
KW
614static int coroutine_fn bdrv_qed_co_create(BlockdevCreateOptions *opts,
615 Error **errp)
75411d23 616{
959355a4
KW
617 BlockdevCreateOptionsQed *qed_opts;
618 BlockBackend *blk = NULL;
619 BlockDriverState *bs = NULL;
620
621 QEDHeader header;
75411d23
SH
622 QEDHeader le_header;
623 uint8_t *l1_table = NULL;
959355a4 624 size_t l1_size;
75411d23 625 int ret = 0;
75411d23 626
959355a4
KW
627 assert(opts->driver == BLOCKDEV_DRIVER_QED);
628 qed_opts = &opts->u.qed;
629
630 /* Validate options and set default values */
631 if (!qed_opts->has_cluster_size) {
632 qed_opts->cluster_size = QED_DEFAULT_CLUSTER_SIZE;
633 }
634 if (!qed_opts->has_table_size) {
635 qed_opts->table_size = QED_DEFAULT_TABLE_SIZE;
75411d23
SH
636 }
637
959355a4
KW
638 if (!qed_is_cluster_size_valid(qed_opts->cluster_size)) {
639 error_setg(errp, "QED cluster size must be within range [%u, %u] "
640 "and power of 2",
641 QED_MIN_CLUSTER_SIZE, QED_MAX_CLUSTER_SIZE);
642 return -EINVAL;
643 }
644 if (!qed_is_table_size_valid(qed_opts->table_size)) {
645 error_setg(errp, "QED table size must be within range [%u, %u] "
646 "and power of 2",
647 QED_MIN_TABLE_SIZE, QED_MAX_TABLE_SIZE);
648 return -EINVAL;
649 }
650 if (!qed_is_image_size_valid(qed_opts->size, qed_opts->cluster_size,
651 qed_opts->table_size))
652 {
653 error_setg(errp, "QED image size must be a non-zero multiple of "
654 "cluster size and less than %" PRIu64 " bytes",
655 qed_max_image_size(qed_opts->cluster_size,
656 qed_opts->table_size));
657 return -EINVAL;
658 }
659
660 /* Create BlockBackend to write to the image */
661 bs = bdrv_open_blockdev_ref(qed_opts->file, errp);
662 if (bs == NULL) {
8a56fdad 663 return -EIO;
75411d23
SH
664 }
665
a3aeeab5
EB
666 blk = blk_new_with_bs(bs, BLK_PERM_WRITE | BLK_PERM_RESIZE, BLK_PERM_ALL,
667 errp);
668 if (!blk) {
669 ret = -EPERM;
959355a4
KW
670 goto out;
671 }
8a56fdad
KW
672 blk_set_allow_write_beyond_eof(blk, true);
673
959355a4
KW
674 /* Prepare image format */
675 header = (QEDHeader) {
676 .magic = QED_MAGIC,
677 .cluster_size = qed_opts->cluster_size,
678 .table_size = qed_opts->table_size,
679 .header_size = 1,
680 .features = 0,
681 .compat_features = 0,
682 .l1_table_offset = qed_opts->cluster_size,
683 .image_size = qed_opts->size,
684 };
685
686 l1_size = header.cluster_size * header.table_size;
687
e8d04f92
HR
688 /*
689 * The QED format associates file length with allocation status,
690 * so a new file (which is empty) must have a length of 0.
691 */
8c6242b6 692 ret = blk_truncate(blk, 0, true, PREALLOC_MODE_OFF, 0, errp);
c743849b
SH
693 if (ret < 0) {
694 goto out;
695 }
696
959355a4 697 if (qed_opts->has_backing_file) {
75411d23
SH
698 header.features |= QED_F_BACKING_FILE;
699 header.backing_filename_offset = sizeof(le_header);
959355a4 700 header.backing_filename_size = strlen(qed_opts->backing_file);
75411d23 701
959355a4
KW
702 if (qed_opts->has_backing_fmt) {
703 const char *backing_fmt = BlockdevDriver_str(qed_opts->backing_fmt);
704 if (qed_fmt_is_raw(backing_fmt)) {
705 header.features |= QED_F_BACKING_FORMAT_NO_PROBE;
706 }
75411d23
SH
707 }
708 }
709
710 qed_header_cpu_to_le(&header, &le_header);
8341f00d 711 ret = blk_pwrite(blk, 0, &le_header, sizeof(le_header), 0);
75411d23
SH
712 if (ret < 0) {
713 goto out;
714 }
959355a4 715 ret = blk_pwrite(blk, sizeof(le_header), qed_opts->backing_file,
8341f00d 716 header.backing_filename_size, 0);
75411d23
SH
717 if (ret < 0) {
718 goto out;
719 }
720
7267c094 721 l1_table = g_malloc0(l1_size);
8341f00d 722 ret = blk_pwrite(blk, header.l1_table_offset, l1_table, l1_size, 0);
75411d23
SH
723 if (ret < 0) {
724 goto out;
725 }
726
727 ret = 0; /* success */
728out:
7267c094 729 g_free(l1_table);
8a56fdad 730 blk_unref(blk);
959355a4 731 bdrv_unref(bs);
75411d23
SH
732 return ret;
733}
734
b92902df
ML
735static int coroutine_fn bdrv_qed_co_create_opts(BlockDriver *drv,
736 const char *filename,
efc75e2a
SH
737 QemuOpts *opts,
738 Error **errp)
75411d23 739{
959355a4 740 BlockdevCreateOptions *create_options = NULL;
92adf9db 741 QDict *qdict;
959355a4
KW
742 Visitor *v;
743 BlockDriverState *bs = NULL;
7ab74849
CL
744 int ret;
745
959355a4
KW
746 static const QDictRenames opt_renames[] = {
747 { BLOCK_OPT_BACKING_FILE, "backing-file" },
748 { BLOCK_OPT_BACKING_FMT, "backing-fmt" },
749 { BLOCK_OPT_CLUSTER_SIZE, "cluster-size" },
750 { BLOCK_OPT_TABLE_SIZE, "table-size" },
751 { NULL, NULL },
752 };
753
754 /* Parse options and convert legacy syntax */
755 qdict = qemu_opts_to_qdict_filtered(opts, NULL, &qed_create_opts, true);
756
757 if (!qdict_rename_keys(qdict, opt_renames, errp)) {
7ab74849 758 ret = -EINVAL;
959355a4 759 goto fail;
75411d23 760 }
959355a4
KW
761
762 /* Create and open the file (protocol layer) */
668f62ec 763 ret = bdrv_create_file(filename, opts, errp);
959355a4 764 if (ret < 0) {
959355a4
KW
765 goto fail;
766 }
767
768 bs = bdrv_open(filename, NULL, NULL,
769 BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_PROTOCOL, errp);
770 if (bs == NULL) {
771 ret = -EIO;
772 goto fail;
773 }
774
775 /* Now get the QAPI type BlockdevCreateOptions */
776 qdict_put_str(qdict, "driver", "qed");
777 qdict_put_str(qdict, "file", bs->node_name);
778
af91062e
MA
779 v = qobject_input_visitor_new_flat_confused(qdict, errp);
780 if (!v) {
7ab74849 781 ret = -EINVAL;
959355a4 782 goto fail;
75411d23 783 }
959355a4 784
b11a093c 785 visit_type_BlockdevCreateOptions(v, NULL, &create_options, errp);
959355a4 786 visit_free(v);
b11a093c 787 if (!create_options) {
7ab74849 788 ret = -EINVAL;
959355a4 789 goto fail;
75411d23
SH
790 }
791
959355a4
KW
792 /* Silently round up size */
793 assert(create_options->driver == BLOCKDEV_DRIVER_QED);
794 create_options->u.qed.size =
795 ROUND_UP(create_options->u.qed.size, BDRV_SECTOR_SIZE);
796
797 /* Create the qed image (format layer) */
798 ret = bdrv_qed_co_create(create_options, errp);
7ab74849 799
959355a4 800fail:
cb3e7f08 801 qobject_unref(qdict);
959355a4
KW
802 bdrv_unref(bs);
803 qapi_free_BlockdevCreateOptions(create_options);
7ab74849 804 return ret;
75411d23
SH
805}
806
b8d739fd
EB
807static int coroutine_fn bdrv_qed_co_block_status(BlockDriverState *bs,
808 bool want_zero,
809 int64_t pos, int64_t bytes,
810 int64_t *pnum, int64_t *map,
811 BlockDriverState **file)
298800ca 812{
b8d739fd
EB
813 BDRVQEDState *s = bs->opaque;
814 size_t len = MIN(bytes, SIZE_MAX);
815 int status;
816 QEDRequest request = { .l2_table = NULL };
817 uint64_t offset;
818 int ret;
819
820 qemu_co_mutex_lock(&s->table_lock);
821 ret = qed_find_cluster(s, &request, pos, &len, &offset);
822
823 *pnum = len;
4bc74be9
PB
824 switch (ret) {
825 case QED_CLUSTER_FOUND:
b8d739fd
EB
826 *map = offset | qed_offset_into_cluster(s, pos);
827 status = BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID;
828 *file = bs->file->bs;
4bc74be9
PB
829 break;
830 case QED_CLUSTER_ZERO:
b8d739fd 831 status = BDRV_BLOCK_ZERO;
4bc74be9
PB
832 break;
833 case QED_CLUSTER_L2:
834 case QED_CLUSTER_L1:
b8d739fd 835 status = 0;
4bc74be9
PB
836 break;
837 default:
838 assert(ret < 0);
b8d739fd 839 status = ret;
4bc74be9
PB
840 break;
841 }
842
298800ca 843 qed_unref_l2_cache_entry(request.l2_table);
1f01e50b 844 qemu_co_mutex_unlock(&s->table_lock);
298800ca 845
b8d739fd 846 return status;
75411d23
SH
847}
848
eabba580
SH
849static BDRVQEDState *acb_to_s(QEDAIOCB *acb)
850{
48cc565e 851 return acb->bs->opaque;
eabba580
SH
852}
853
854/**
855 * Read from the backing file or zero-fill if no backing file
856 *
f06ee3d4
KW
857 * @s: QED state
858 * @pos: Byte position in device
859 * @qiov: Destination I/O vector
eabba580
SH
860 *
861 * This function reads qiov->size bytes starting at pos from the backing file.
862 * If there is no backing file then zeroes are read.
863 */
87f0d882 864static int coroutine_fn qed_read_backing_file(BDRVQEDState *s, uint64_t pos,
365fed51 865 QEMUIOVector *qiov)
eabba580 866{
760e0063 867 if (s->bs->backing) {
365fed51
EB
868 BLKDBG_EVENT(s->bs->file, BLKDBG_READ_BACKING_AIO);
869 return bdrv_co_preadv(s->bs->backing, pos, qiov->size, qiov, 0);
e85c5281 870 }
365fed51 871 qemu_iovec_memset(qiov, 0, 0, qiov->size);
e85c5281 872 return 0;
eabba580
SH
873}
874
eabba580
SH
875/**
876 * Copy data from backing file into the image
877 *
878 * @s: QED state
879 * @pos: Byte position in device
880 * @len: Number of bytes
881 * @offset: Byte offset in image file
eabba580 882 */
87f0d882
KW
883static int coroutine_fn qed_copy_from_backing_file(BDRVQEDState *s,
884 uint64_t pos, uint64_t len,
885 uint64_t offset)
eabba580 886{
0f7aa24d 887 QEMUIOVector qiov;
e85c5281 888 int ret;
eabba580
SH
889
890 /* Skip copy entirely if there is no work to do */
891 if (len == 0) {
b4ac32f3 892 return 0;
eabba580
SH
893 }
894
342544f9 895 qemu_iovec_init_buf(&qiov, qemu_blockalign(s->bs, len), len);
0f7aa24d 896
365fed51 897 ret = qed_read_backing_file(s, pos, &qiov);
0f7aa24d
KW
898
899 if (ret) {
900 goto out;
901 }
eabba580 902
0f7aa24d 903 BLKDBG_EVENT(s->bs->file, BLKDBG_COW_WRITE);
0f714ec7 904 ret = bdrv_co_pwritev(s->bs->file, offset, qiov.size, &qiov, 0);
0f7aa24d
KW
905 if (ret < 0) {
906 goto out;
907 }
908 ret = 0;
909out:
342544f9 910 qemu_vfree(qemu_iovec_buf(&qiov));
b4ac32f3 911 return ret;
eabba580
SH
912}
913
914/**
915 * Link one or more contiguous clusters into a table
916 *
917 * @s: QED state
918 * @table: L2 table
919 * @index: First cluster index
920 * @n: Number of contiguous clusters
21df65b6
AL
921 * @cluster: First cluster offset
922 *
923 * The cluster offset may be an allocated byte offset in the image file, the
924 * zero cluster marker, or the unallocated cluster marker.
1f01e50b
PB
925 *
926 * Called with table_lock held.
eabba580 927 */
87f0d882
KW
928static void coroutine_fn qed_update_l2_table(BDRVQEDState *s, QEDTable *table,
929 int index, unsigned int n,
930 uint64_t cluster)
eabba580
SH
931{
932 int i;
933 for (i = index; i < index + n; i++) {
934 table->offsets[i] = cluster;
21df65b6
AL
935 if (!qed_offset_is_unalloc_cluster(cluster) &&
936 !qed_offset_is_zero_cluster(cluster)) {
937 cluster += s->header.cluster_size;
938 }
eabba580
SH
939 }
940}
941
1f01e50b 942/* Called with table_lock held. */
87f0d882 943static void coroutine_fn qed_aio_complete(QEDAIOCB *acb)
eabba580 944{
1919631e 945 BDRVQEDState *s = acb_to_s(acb);
eabba580
SH
946
947 /* Free resources */
948 qemu_iovec_destroy(&acb->cur_qiov);
949 qed_unref_l2_cache_entry(acb->request.l2_table);
950
0e71be19
SH
951 /* Free the buffer we may have allocated for zero writes */
952 if (acb->flags & QED_AIOCB_ZERO) {
953 qemu_vfree(acb->qiov->iov[0].iov_base);
954 acb->qiov->iov[0].iov_base = NULL;
955 }
956
eabba580
SH
957 /* Start next allocating write request waiting behind this one. Note that
958 * requests enqueue themselves when they first hit an unallocated cluster
959 * but they wait until the entire request is finished before waking up the
960 * next request in the queue. This ensures that we don't cycle through
961 * requests multiple times but rather finish one at a time completely.
962 */
0806c3b5
KW
963 if (acb == s->allocating_acb) {
964 s->allocating_acb = NULL;
965 if (!qemu_co_queue_empty(&s->allocating_write_reqs)) {
1f01e50b 966 qemu_co_queue_next(&s->allocating_write_reqs);
6f321e93
SH
967 } else if (s->header.features & QED_F_NEED_CHECK) {
968 qed_start_need_check_timer(s);
eabba580
SH
969 }
970 }
971}
972
973/**
fae25ac7 974 * Update L1 table with new L2 table offset and write it out
1f01e50b
PB
975 *
976 * Called with table_lock held.
eabba580 977 */
87f0d882 978static int coroutine_fn qed_aio_write_l1_update(QEDAIOCB *acb)
eabba580 979{
eabba580
SH
980 BDRVQEDState *s = acb_to_s(acb);
981 CachedL2Table *l2_table = acb->request.l2_table;
e4fc8781 982 uint64_t l2_offset = l2_table->offset;
fb18de21 983 int index, ret;
eabba580 984
fae25ac7
KW
985 index = qed_l1_index(s, acb->cur_pos);
986 s->l1_table->offsets[index] = l2_table->offset;
987
988 ret = qed_write_l1_table(s, index, 1);
989
990 /* Commit the current L2 table to the cache */
eabba580
SH
991 qed_commit_l2_cache_entry(&s->l2_cache, l2_table);
992
993 /* This is guaranteed to succeed because we just committed the entry to the
994 * cache.
995 */
e4fc8781 996 acb->request.l2_table = qed_find_l2_cache_entry(&s->l2_cache, l2_offset);
eabba580
SH
997 assert(acb->request.l2_table != NULL);
998
fb18de21 999 return ret;
eabba580
SH
1000}
1001
eabba580
SH
1002
1003/**
1004 * Update L2 table with new cluster offsets and write them out
1f01e50b
PB
1005 *
1006 * Called with table_lock held.
eabba580 1007 */
87f0d882 1008static int coroutine_fn qed_aio_write_l2_update(QEDAIOCB *acb, uint64_t offset)
eabba580 1009{
eabba580
SH
1010 BDRVQEDState *s = acb_to_s(acb);
1011 bool need_alloc = acb->find_cluster_ret == QED_CLUSTER_L1;
88d2dd72 1012 int index, ret;
eabba580
SH
1013
1014 if (need_alloc) {
1015 qed_unref_l2_cache_entry(acb->request.l2_table);
1016 acb->request.l2_table = qed_new_l2_table(s);
1017 }
1018
1019 index = qed_l2_index(s, acb->cur_pos);
1020 qed_update_l2_table(s, acb->request.l2_table->table, index, acb->cur_nclusters,
0e71be19 1021 offset);
eabba580
SH
1022
1023 if (need_alloc) {
1024 /* Write out the whole new L2 table */
453e53e2 1025 ret = qed_write_l2_table(s, &acb->request, 0, s->table_nelems, true);
fb18de21 1026 if (ret) {
88d2dd72 1027 return ret;
fb18de21 1028 }
88d2dd72 1029 return qed_aio_write_l1_update(acb);
eabba580
SH
1030 } else {
1031 /* Write out only the updated part of the L2 table */
453e53e2
KW
1032 ret = qed_write_l2_table(s, &acb->request, index, acb->cur_nclusters,
1033 false);
88d2dd72
KW
1034 if (ret) {
1035 return ret;
1036 }
eabba580 1037 }
88d2dd72 1038 return 0;
eabba580
SH
1039}
1040
eabba580
SH
1041/**
1042 * Write data to the image file
1f01e50b
PB
1043 *
1044 * Called with table_lock *not* held.
eabba580 1045 */
87f0d882 1046static int coroutine_fn qed_aio_write_main(QEDAIOCB *acb)
eabba580 1047{
eabba580
SH
1048 BDRVQEDState *s = acb_to_s(acb);
1049 uint64_t offset = acb->cur_cluster +
1050 qed_offset_into_cluster(s, acb->cur_pos);
eabba580 1051
eaf0bc56 1052 trace_qed_aio_write_main(s, acb, 0, offset, acb->cur_qiov.size);
eabba580 1053
a4d8f1ae 1054 BLKDBG_EVENT(s->bs->file, BLKDBG_WRITE_AIO);
e7569c18
PB
1055 return bdrv_co_pwritev(s->bs->file, offset, acb->cur_qiov.size,
1056 &acb->cur_qiov, 0);
eabba580
SH
1057}
1058
1059/**
b4ac32f3 1060 * Populate untouched regions of new data cluster
1f01e50b
PB
1061 *
1062 * Called with table_lock held.
eabba580 1063 */
87f0d882 1064static int coroutine_fn qed_aio_write_cow(QEDAIOCB *acb)
eabba580 1065{
eabba580 1066 BDRVQEDState *s = acb_to_s(acb);
b4ac32f3 1067 uint64_t start, len, offset;
a101341a 1068 int ret;
eabba580 1069
1f01e50b
PB
1070 qemu_co_mutex_unlock(&s->table_lock);
1071
b4ac32f3
KW
1072 /* Populate front untouched region of new data cluster */
1073 start = qed_start_of_cluster(s, acb->cur_pos);
1074 len = qed_offset_into_cluster(s, acb->cur_pos);
1075
1076 trace_qed_aio_write_prefill(s, acb, start, len, acb->cur_cluster);
1077 ret = qed_copy_from_backing_file(s, start, len, acb->cur_cluster);
a101341a 1078 if (ret < 0) {
1f01e50b 1079 goto out;
eabba580
SH
1080 }
1081
b4ac32f3
KW
1082 /* Populate back untouched region of new data cluster */
1083 start = acb->cur_pos + acb->cur_qiov.size;
1084 len = qed_start_of_cluster(s, start + s->header.cluster_size - 1) - start;
1085 offset = acb->cur_cluster +
1086 qed_offset_into_cluster(s, acb->cur_pos) +
1087 acb->cur_qiov.size;
eabba580 1088
b4ac32f3
KW
1089 trace_qed_aio_write_postfill(s, acb, start, len, offset);
1090 ret = qed_copy_from_backing_file(s, start, len, offset);
eaf0bc56 1091 if (ret < 0) {
1f01e50b 1092 goto out;
eaf0bc56 1093 }
a101341a 1094
e7569c18
PB
1095 ret = qed_aio_write_main(acb);
1096 if (ret < 0) {
1f01e50b 1097 goto out;
e7569c18
PB
1098 }
1099
1100 if (s->bs->backing) {
1101 /*
1102 * Flush new data clusters before updating the L2 table
1103 *
1104 * This flush is necessary when a backing file is in use. A crash
1105 * during an allocating write could result in empty clusters in the
1106 * image. If the write only touched a subregion of the cluster,
1107 * then backing image sectors have been lost in the untouched
1108 * region. The solution is to flush after writing a new data
1109 * cluster and before updating the L2 table.
1110 */
1111 ret = bdrv_co_flush(s->bs->file->bs);
e7569c18
PB
1112 }
1113
1f01e50b
PB
1114out:
1115 qemu_co_mutex_lock(&s->table_lock);
1116 return ret;
eabba580
SH
1117}
1118
0d09c797
SH
1119/**
1120 * Check if the QED_F_NEED_CHECK bit should be set during allocating write
1121 */
1122static bool qed_should_set_need_check(BDRVQEDState *s)
1123{
1124 /* The flush before L2 update path ensures consistency */
760e0063 1125 if (s->bs->backing) {
0d09c797
SH
1126 return false;
1127 }
1128
1129 return !(s->header.features & QED_F_NEED_CHECK);
1130}
1131
eabba580
SH
1132/**
1133 * Write new data cluster
1134 *
1135 * @acb: Write request
1136 * @len: Length in bytes
1137 *
1138 * This path is taken when writing to previously unallocated clusters.
1f01e50b
PB
1139 *
1140 * Called with table_lock held.
eabba580 1141 */
87f0d882 1142static int coroutine_fn qed_aio_write_alloc(QEDAIOCB *acb, size_t len)
eabba580
SH
1143{
1144 BDRVQEDState *s = acb_to_s(acb);
f13d712b 1145 int ret;
eabba580 1146
6f321e93 1147 /* Cancel timer when the first allocating request comes in */
0806c3b5 1148 if (s->allocating_acb == NULL) {
6f321e93
SH
1149 qed_cancel_need_check_timer(s);
1150 }
1151
eabba580 1152 /* Freeze this request if another allocating write is in progress */
0806c3b5
KW
1153 if (s->allocating_acb != acb || s->allocating_write_reqs_plugged) {
1154 if (s->allocating_acb != NULL) {
1f01e50b 1155 qemu_co_queue_wait(&s->allocating_write_reqs, &s->table_lock);
0806c3b5
KW
1156 assert(s->allocating_acb == NULL);
1157 }
1158 s->allocating_acb = acb;
1159 return -EAGAIN; /* start over with looking up table entries */
eabba580
SH
1160 }
1161
1162 acb->cur_nclusters = qed_bytes_to_clusters(s,
1163 qed_offset_into_cluster(s, acb->cur_pos) + len);
1b093c48 1164 qemu_iovec_concat(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len);
eabba580 1165
0e71be19
SH
1166 if (acb->flags & QED_AIOCB_ZERO) {
1167 /* Skip ahead if the clusters are already zero */
1168 if (acb->find_cluster_ret == QED_CLUSTER_ZERO) {
d6daddcd 1169 return 0;
0e71be19 1170 }
e7569c18 1171 acb->cur_cluster = 1;
0e71be19 1172 } else {
0e71be19
SH
1173 acb->cur_cluster = qed_alloc_clusters(s, acb->cur_nclusters);
1174 }
1175
0d09c797
SH
1176 if (qed_should_set_need_check(s)) {
1177 s->header.features |= QED_F_NEED_CHECK;
f13d712b 1178 ret = qed_write_header(s);
a101341a 1179 if (ret < 0) {
d6daddcd 1180 return ret;
a101341a
KW
1181 }
1182 }
1183
e7569c18 1184 if (!(acb->flags & QED_AIOCB_ZERO)) {
a101341a 1185 ret = qed_aio_write_cow(acb);
e7569c18
PB
1186 if (ret < 0) {
1187 return ret;
1188 }
01979a98 1189 }
e7569c18
PB
1190
1191 return qed_aio_write_l2_update(acb, acb->cur_cluster);
eabba580
SH
1192}
1193
1194/**
1195 * Write data cluster in place
1196 *
1197 * @acb: Write request
1198 * @offset: Cluster offset in bytes
1199 * @len: Length in bytes
1200 *
1201 * This path is taken when writing to already allocated clusters.
1f01e50b
PB
1202 *
1203 * Called with table_lock held.
eabba580 1204 */
87f0d882
KW
1205static int coroutine_fn qed_aio_write_inplace(QEDAIOCB *acb, uint64_t offset,
1206 size_t len)
eabba580 1207{
1f01e50b
PB
1208 BDRVQEDState *s = acb_to_s(acb);
1209 int r;
1210
1211 qemu_co_mutex_unlock(&s->table_lock);
1212
0e71be19
SH
1213 /* Allocate buffer for zero writes */
1214 if (acb->flags & QED_AIOCB_ZERO) {
1215 struct iovec *iov = acb->qiov->iov;
1216
1217 if (!iov->iov_base) {
48cc565e 1218 iov->iov_base = qemu_try_blockalign(acb->bs, iov->iov_len);
4f4896db 1219 if (iov->iov_base == NULL) {
1f01e50b
PB
1220 r = -ENOMEM;
1221 goto out;
4f4896db 1222 }
0e71be19
SH
1223 memset(iov->iov_base, 0, iov->iov_len);
1224 }
1225 }
1226
eabba580
SH
1227 /* Calculate the I/O vector */
1228 acb->cur_cluster = offset;
1b093c48 1229 qemu_iovec_concat(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len);
eabba580 1230
1f01e50b
PB
1231 /* Do the actual write. */
1232 r = qed_aio_write_main(acb);
1233out:
1234 qemu_co_mutex_lock(&s->table_lock);
1235 return r;
eabba580
SH
1236}
1237
1238/**
1239 * Write data cluster
1240 *
1241 * @opaque: Write request
0596be7e 1242 * @ret: QED_CLUSTER_FOUND, QED_CLUSTER_L2 or QED_CLUSTER_L1
eabba580
SH
1243 * @offset: Cluster offset in bytes
1244 * @len: Length in bytes
1f01e50b
PB
1245 *
1246 * Called with table_lock held.
eabba580 1247 */
87f0d882
KW
1248static int coroutine_fn qed_aio_write_data(void *opaque, int ret,
1249 uint64_t offset, size_t len)
eabba580
SH
1250{
1251 QEDAIOCB *acb = opaque;
1252
1253 trace_qed_aio_write_data(acb_to_s(acb), acb, ret, offset, len);
1254
1255 acb->find_cluster_ret = ret;
1256
1257 switch (ret) {
1258 case QED_CLUSTER_FOUND:
0596be7e 1259 return qed_aio_write_inplace(acb, offset, len);
eabba580
SH
1260
1261 case QED_CLUSTER_L2:
1262 case QED_CLUSTER_L1:
21df65b6 1263 case QED_CLUSTER_ZERO:
0596be7e 1264 return qed_aio_write_alloc(acb, len);
eabba580
SH
1265
1266 default:
0596be7e 1267 g_assert_not_reached();
d6daddcd 1268 }
eabba580
SH
1269}
1270
1271/**
1272 * Read data cluster
1273 *
1274 * @opaque: Read request
0596be7e 1275 * @ret: QED_CLUSTER_FOUND, QED_CLUSTER_L2 or QED_CLUSTER_L1
eabba580
SH
1276 * @offset: Cluster offset in bytes
1277 * @len: Length in bytes
1f01e50b
PB
1278 *
1279 * Called with table_lock held.
eabba580 1280 */
87f0d882
KW
1281static int coroutine_fn qed_aio_read_data(void *opaque, int ret,
1282 uint64_t offset, size_t len)
eabba580
SH
1283{
1284 QEDAIOCB *acb = opaque;
1285 BDRVQEDState *s = acb_to_s(acb);
48cc565e 1286 BlockDriverState *bs = acb->bs;
1f01e50b
PB
1287 int r;
1288
1289 qemu_co_mutex_unlock(&s->table_lock);
eabba580
SH
1290
1291 /* Adjust offset into cluster */
1292 offset += qed_offset_into_cluster(s, acb->cur_pos);
1293
1294 trace_qed_aio_read_data(s, acb, ret, offset, len);
1295
1b093c48 1296 qemu_iovec_concat(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len);
eabba580 1297
1f01e50b
PB
1298 /* Handle zero cluster and backing file reads, otherwise read
1299 * data cluster directly.
1300 */
21df65b6 1301 if (ret == QED_CLUSTER_ZERO) {
3d9b4925 1302 qemu_iovec_memset(&acb->cur_qiov, 0, 0, acb->cur_qiov.size);
1f01e50b 1303 r = 0;
21df65b6 1304 } else if (ret != QED_CLUSTER_FOUND) {
365fed51 1305 r = qed_read_backing_file(s, acb->cur_pos, &acb->cur_qiov);
1f01e50b
PB
1306 } else {
1307 BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO);
1308 r = bdrv_co_preadv(bs->file, offset, acb->cur_qiov.size,
1309 &acb->cur_qiov, 0);
eabba580
SH
1310 }
1311
1f01e50b
PB
1312 qemu_co_mutex_lock(&s->table_lock);
1313 return r;
eabba580
SH
1314}
1315
1316/**
1317 * Begin next I/O or complete the request
1318 */
87f0d882 1319static int coroutine_fn qed_aio_next_io(QEDAIOCB *acb)
eabba580 1320{
eabba580 1321 BDRVQEDState *s = acb_to_s(acb);
0f21b7a1
KW
1322 uint64_t offset;
1323 size_t len;
dddf8db1 1324 int ret;
eabba580 1325
1f01e50b 1326 qemu_co_mutex_lock(&s->table_lock);
01859874
KW
1327 while (1) {
1328 trace_qed_aio_next_io(s, acb, 0, acb->cur_pos + acb->cur_qiov.size);
eabba580 1329
01859874
KW
1330 acb->qiov_offset += acb->cur_qiov.size;
1331 acb->cur_pos += acb->cur_qiov.size;
1332 qemu_iovec_reset(&acb->cur_qiov);
eabba580 1333
01859874
KW
1334 /* Complete request */
1335 if (acb->cur_pos >= acb->end_pos) {
48cc565e
KW
1336 ret = 0;
1337 break;
01859874 1338 }
eabba580 1339
01859874
KW
1340 /* Find next cluster and start I/O */
1341 len = acb->end_pos - acb->cur_pos;
1342 ret = qed_find_cluster(s, &acb->request, acb->cur_pos, &len, &offset);
1343 if (ret < 0) {
48cc565e 1344 break;
01859874 1345 }
0596be7e 1346
01859874
KW
1347 if (acb->flags & QED_AIOCB_WRITE) {
1348 ret = qed_aio_write_data(acb, ret, offset, len);
1349 } else {
1350 ret = qed_aio_read_data(acb, ret, offset, len);
1351 }
0596be7e 1352
0806c3b5 1353 if (ret < 0 && ret != -EAGAIN) {
48cc565e 1354 break;
0596be7e 1355 }
0596be7e 1356 }
eabba580 1357
48cc565e
KW
1358 trace_qed_aio_complete(s, acb, ret);
1359 qed_aio_complete(acb);
1f01e50b 1360 qemu_co_mutex_unlock(&s->table_lock);
48cc565e 1361 return ret;
89f89709
KW
1362}
1363
1364static int coroutine_fn qed_co_request(BlockDriverState *bs, int64_t sector_num,
1365 QEMUIOVector *qiov, int nb_sectors,
1366 int flags)
1367{
48cc565e
KW
1368 QEDAIOCB acb = {
1369 .bs = bs,
1370 .cur_pos = (uint64_t) sector_num * BDRV_SECTOR_SIZE,
1371 .end_pos = (sector_num + nb_sectors) * BDRV_SECTOR_SIZE,
1372 .qiov = qiov,
1373 .flags = flags,
89f89709 1374 };
48cc565e 1375 qemu_iovec_init(&acb.cur_qiov, qiov->niov);
eabba580 1376
48cc565e 1377 trace_qed_aio_setup(bs->opaque, &acb, sector_num, nb_sectors, NULL, flags);
eabba580
SH
1378
1379 /* Start request */
48cc565e 1380 return qed_aio_next_io(&acb);
75411d23
SH
1381}
1382
89f89709
KW
1383static int coroutine_fn bdrv_qed_co_readv(BlockDriverState *bs,
1384 int64_t sector_num, int nb_sectors,
1385 QEMUIOVector *qiov)
75411d23 1386{
89f89709 1387 return qed_co_request(bs, sector_num, qiov, nb_sectors, 0);
75411d23
SH
1388}
1389
89f89709
KW
1390static int coroutine_fn bdrv_qed_co_writev(BlockDriverState *bs,
1391 int64_t sector_num, int nb_sectors,
e18a58b4 1392 QEMUIOVector *qiov, int flags)
0e71be19 1393{
e18a58b4 1394 assert(!flags);
89f89709 1395 return qed_co_request(bs, sector_num, qiov, nb_sectors, QED_AIOCB_WRITE);
0e71be19
SH
1396}
1397
49a2e483
EB
1398static int coroutine_fn bdrv_qed_co_pwrite_zeroes(BlockDriverState *bs,
1399 int64_t offset,
f5a5ca79 1400 int bytes,
49a2e483 1401 BdrvRequestFlags flags)
0e71be19 1402{
ef72f76e 1403 BDRVQEDState *s = bs->opaque;
342544f9
VSO
1404
1405 /*
1406 * Zero writes start without an I/O buffer. If a buffer becomes necessary
1407 * then it will be allocated during request processing.
1408 */
1409 QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, NULL, bytes);
0e71be19 1410
49a2e483
EB
1411 /* Fall back if the request is not aligned */
1412 if (qed_offset_into_cluster(s, offset) ||
f5a5ca79 1413 qed_offset_into_cluster(s, bytes)) {
49a2e483 1414 return -ENOTSUP;
ef72f76e
SH
1415 }
1416
89f89709 1417 return qed_co_request(bs, offset >> BDRV_SECTOR_BITS, &qiov,
f5a5ca79 1418 bytes >> BDRV_SECTOR_BITS,
89f89709 1419 QED_AIOCB_WRITE | QED_AIOCB_ZERO);
0e71be19
SH
1420}
1421
061ca8a3
KW
1422static int coroutine_fn bdrv_qed_co_truncate(BlockDriverState *bs,
1423 int64_t offset,
c80d8b06 1424 bool exact,
061ca8a3 1425 PreallocMode prealloc,
92b92799 1426 BdrvRequestFlags flags,
061ca8a3 1427 Error **errp)
75411d23 1428{
77a5a000
SH
1429 BDRVQEDState *s = bs->opaque;
1430 uint64_t old_image_size;
1431 int ret;
1432
8243ccb7
HR
1433 if (prealloc != PREALLOC_MODE_OFF) {
1434 error_setg(errp, "Unsupported preallocation mode '%s'",
977c736f 1435 PreallocMode_str(prealloc));
8243ccb7
HR
1436 return -ENOTSUP;
1437 }
1438
77a5a000
SH
1439 if (!qed_is_image_size_valid(offset, s->header.cluster_size,
1440 s->header.table_size)) {
f59adb32 1441 error_setg(errp, "Invalid image size specified");
77a5a000
SH
1442 return -EINVAL;
1443 }
1444
77a5a000 1445 if ((uint64_t)offset < s->header.image_size) {
f59adb32 1446 error_setg(errp, "Shrinking images is currently not supported");
77a5a000
SH
1447 return -ENOTSUP;
1448 }
1449
1450 old_image_size = s->header.image_size;
1451 s->header.image_size = offset;
1452 ret = qed_write_header_sync(s);
1453 if (ret < 0) {
1454 s->header.image_size = old_image_size;
f59adb32 1455 error_setg_errno(errp, -ret, "Failed to update the image size");
77a5a000
SH
1456 }
1457 return ret;
75411d23
SH
1458}
1459
1460static int64_t bdrv_qed_getlength(BlockDriverState *bs)
1461{
1462 BDRVQEDState *s = bs->opaque;
1463 return s->header.image_size;
1464}
1465
1466static int bdrv_qed_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1467{
1468 BDRVQEDState *s = bs->opaque;
1469
1470 memset(bdi, 0, sizeof(*bdi));
1471 bdi->cluster_size = s->header.cluster_size;
d68dbee8 1472 bdi->is_dirty = s->header.features & QED_F_NEED_CHECK;
75411d23
SH
1473 return 0;
1474}
1475
1476static int bdrv_qed_change_backing_file(BlockDriverState *bs,
1477 const char *backing_file,
1478 const char *backing_fmt)
1479{
1480 BDRVQEDState *s = bs->opaque;
1481 QEDHeader new_header, le_header;
1482 void *buffer;
1483 size_t buffer_len, backing_file_len;
1484 int ret;
1485
1486 /* Refuse to set backing filename if unknown compat feature bits are
1487 * active. If the image uses an unknown compat feature then we may not
1488 * know the layout of data following the header structure and cannot safely
1489 * add a new string.
1490 */
1491 if (backing_file && (s->header.compat_features &
1492 ~QED_COMPAT_FEATURE_MASK)) {
1493 return -ENOTSUP;
1494 }
1495
1496 memcpy(&new_header, &s->header, sizeof(new_header));
1497
1498 new_header.features &= ~(QED_F_BACKING_FILE |
1499 QED_F_BACKING_FORMAT_NO_PROBE);
1500
1501 /* Adjust feature flags */
1502 if (backing_file) {
1503 new_header.features |= QED_F_BACKING_FILE;
1504
1505 if (qed_fmt_is_raw(backing_fmt)) {
1506 new_header.features |= QED_F_BACKING_FORMAT_NO_PROBE;
1507 }
1508 }
1509
1510 /* Calculate new header size */
1511 backing_file_len = 0;
1512
1513 if (backing_file) {
1514 backing_file_len = strlen(backing_file);
1515 }
1516
1517 buffer_len = sizeof(new_header);
1518 new_header.backing_filename_offset = buffer_len;
1519 new_header.backing_filename_size = backing_file_len;
1520 buffer_len += backing_file_len;
1521
1522 /* Make sure we can rewrite header without failing */
1523 if (buffer_len > new_header.header_size * new_header.cluster_size) {
1524 return -ENOSPC;
1525 }
1526
1527 /* Prepare new header */
7267c094 1528 buffer = g_malloc(buffer_len);
75411d23
SH
1529
1530 qed_header_cpu_to_le(&new_header, &le_header);
1531 memcpy(buffer, &le_header, sizeof(le_header));
1532 buffer_len = sizeof(le_header);
1533
feba23b1
PB
1534 if (backing_file) {
1535 memcpy(buffer + buffer_len, backing_file, backing_file_len);
1536 buffer_len += backing_file_len;
1537 }
75411d23
SH
1538
1539 /* Write new header */
d9ca2ea2 1540 ret = bdrv_pwrite_sync(bs->file, 0, buffer, buffer_len);
7267c094 1541 g_free(buffer);
75411d23
SH
1542 if (ret == 0) {
1543 memcpy(&s->header, &new_header, sizeof(new_header));
1544 }
1545 return ret;
1546}
1547
2b148f39
PB
1548static void coroutine_fn bdrv_qed_co_invalidate_cache(BlockDriverState *bs,
1549 Error **errp)
c82954e5 1550{
1f01e50b 1551 BDRVQEDState *s = bs->opaque;
5a8a30db 1552 int ret;
c82954e5
BC
1553
1554 bdrv_qed_close(bs);
3456a8d1 1555
61c7887e 1556 bdrv_qed_init_state(bs);
2b148f39 1557 qemu_co_mutex_lock(&s->table_lock);
15ce94a6 1558 ret = bdrv_qed_do_open(bs, NULL, bs->open_flags, errp);
2b148f39 1559 qemu_co_mutex_unlock(&s->table_lock);
15ce94a6
VSO
1560 if (ret < 0) {
1561 error_prepend(errp, "Could not reopen qed layer: ");
5a8a30db 1562 }
c82954e5
BC
1563}
1564
54277a2a
VSO
1565static int coroutine_fn bdrv_qed_co_check(BlockDriverState *bs,
1566 BdrvCheckResult *result,
1567 BdrvCheckMode fix)
75411d23 1568{
01979a98 1569 BDRVQEDState *s = bs->opaque;
2fd61638 1570 int ret;
01979a98 1571
2fd61638
PB
1572 qemu_co_mutex_lock(&s->table_lock);
1573 ret = qed_check(s, result, !!fix);
1574 qemu_co_mutex_unlock(&s->table_lock);
1575
1576 return ret;
75411d23
SH
1577}
1578
7ab74849
CL
1579static QemuOptsList qed_create_opts = {
1580 .name = "qed-create-opts",
1581 .head = QTAILQ_HEAD_INITIALIZER(qed_create_opts.head),
1582 .desc = {
1583 {
1584 .name = BLOCK_OPT_SIZE,
1585 .type = QEMU_OPT_SIZE,
1586 .help = "Virtual disk size"
1587 },
1588 {
1589 .name = BLOCK_OPT_BACKING_FILE,
1590 .type = QEMU_OPT_STRING,
1591 .help = "File name of a base image"
1592 },
1593 {
1594 .name = BLOCK_OPT_BACKING_FMT,
1595 .type = QEMU_OPT_STRING,
1596 .help = "Image format of the base image"
1597 },
1598 {
1599 .name = BLOCK_OPT_CLUSTER_SIZE,
1600 .type = QEMU_OPT_SIZE,
1601 .help = "Cluster size (in bytes)",
1602 .def_value_str = stringify(QED_DEFAULT_CLUSTER_SIZE)
1603 },
1604 {
1605 .name = BLOCK_OPT_TABLE_SIZE,
1606 .type = QEMU_OPT_SIZE,
1607 .help = "L1/L2 table size (in clusters)"
1608 },
1609 { /* end of list */ }
1610 }
75411d23
SH
1611};
1612
1613static BlockDriver bdrv_qed = {
1614 .format_name = "qed",
1615 .instance_size = sizeof(BDRVQEDState),
7ab74849 1616 .create_opts = &qed_create_opts,
d67066d8 1617 .is_format = true,
8ee79e70 1618 .supports_backing = true,
75411d23
SH
1619
1620 .bdrv_probe = bdrv_qed_probe,
1621 .bdrv_open = bdrv_qed_open,
1622 .bdrv_close = bdrv_qed_close,
f9cb20f1 1623 .bdrv_reopen_prepare = bdrv_qed_reopen_prepare,
69dca43d 1624 .bdrv_child_perm = bdrv_default_perms,
959355a4 1625 .bdrv_co_create = bdrv_qed_co_create,
efc75e2a 1626 .bdrv_co_create_opts = bdrv_qed_co_create_opts,
3ac21627 1627 .bdrv_has_zero_init = bdrv_has_zero_init_1,
b8d739fd 1628 .bdrv_co_block_status = bdrv_qed_co_block_status,
89f89709
KW
1629 .bdrv_co_readv = bdrv_qed_co_readv,
1630 .bdrv_co_writev = bdrv_qed_co_writev,
49a2e483 1631 .bdrv_co_pwrite_zeroes = bdrv_qed_co_pwrite_zeroes,
061ca8a3 1632 .bdrv_co_truncate = bdrv_qed_co_truncate,
75411d23
SH
1633 .bdrv_getlength = bdrv_qed_getlength,
1634 .bdrv_get_info = bdrv_qed_get_info,
d34682cd 1635 .bdrv_refresh_limits = bdrv_qed_refresh_limits,
75411d23 1636 .bdrv_change_backing_file = bdrv_qed_change_backing_file,
2b148f39 1637 .bdrv_co_invalidate_cache = bdrv_qed_co_invalidate_cache,
2fd61638 1638 .bdrv_co_check = bdrv_qed_co_check,
a8c868c3
SH
1639 .bdrv_detach_aio_context = bdrv_qed_detach_aio_context,
1640 .bdrv_attach_aio_context = bdrv_qed_attach_aio_context,
f8ea8dac 1641 .bdrv_co_drain_begin = bdrv_qed_co_drain_begin,
75411d23
SH
1642};
1643
1644static void bdrv_qed_init(void)
1645{
1646 bdrv_register(&bdrv_qed);
1647}
1648
1649block_init(bdrv_qed_init);
This page took 0.763594 seconds and 4 git commands to generate.