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