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