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