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