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