2 * QEMU Enhanced Disk Format Consistency Check
4 * Copyright IBM, Corp. 2010
7 * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
9 * This work is licensed under the terms of the GNU LGPL, version 2 or later.
10 * See the COPYING.LIB file in the top-level directory.
14 #include "qemu/osdep.h"
19 BdrvCheckResult *result;
20 bool fix; /* whether to fix invalid offsets */
23 uint32_t *used_clusters; /* referenced cluster bitmap */
28 static bool qed_test_bit(uint32_t *bitmap, uint64_t n) {
29 return !!(bitmap[n / 32] & (1 << (n % 32)));
32 static void qed_set_bit(uint32_t *bitmap, uint64_t n) {
33 bitmap[n / 32] |= 1 << (n % 32);
37 * Set bitmap bits for clusters
39 * @check: Check structure
40 * @offset: Starting offset in bytes
41 * @n: Number of clusters
43 static bool qed_set_used_clusters(QEDCheck *check, uint64_t offset,
46 uint64_t cluster = qed_bytes_to_clusters(check->s, offset);
47 unsigned int corruptions = 0;
50 /* Clusters should only be referenced once */
51 if (qed_test_bit(check->used_clusters, cluster)) {
55 qed_set_bit(check->used_clusters, cluster);
59 check->result->corruptions += corruptions;
60 return corruptions == 0;
66 * @ret: Number of invalid cluster offsets
68 static unsigned int qed_check_l2_table(QEDCheck *check, QEDTable *table)
70 BDRVQEDState *s = check->s;
71 unsigned int i, num_invalid = 0;
72 uint64_t last_offset = 0;
74 for (i = 0; i < s->table_nelems; i++) {
75 uint64_t offset = table->offsets[i];
77 if (qed_offset_is_unalloc_cluster(offset) ||
78 qed_offset_is_zero_cluster(offset)) {
81 check->result->bfi.allocated_clusters++;
82 if (last_offset && (last_offset + s->header.cluster_size != offset)) {
83 check->result->bfi.fragmented_clusters++;
87 /* Detect invalid cluster offset */
88 if (!qed_check_cluster_offset(s, offset)) {
90 table->offsets[i] = 0;
91 check->result->corruptions_fixed++;
93 check->result->corruptions++;
100 qed_set_used_clusters(check, offset, 1);
107 * Descend tables and check each cluster is referenced once only
109 static int coroutine_fn qed_check_l1_table(QEDCheck *check, QEDTable *table)
111 BDRVQEDState *s = check->s;
112 unsigned int i, num_invalid_l1 = 0;
113 int ret, last_error = 0;
115 /* Mark L1 table clusters used */
116 qed_set_used_clusters(check, s->header.l1_table_offset,
117 s->header.table_size);
119 for (i = 0; i < s->table_nelems; i++) {
120 unsigned int num_invalid_l2;
121 uint64_t offset = table->offsets[i];
123 if (qed_offset_is_unalloc_cluster(offset)) {
127 /* Detect invalid L2 offset */
128 if (!qed_check_table_offset(s, offset)) {
129 /* Clear invalid offset */
131 table->offsets[i] = 0;
132 check->result->corruptions_fixed++;
134 check->result->corruptions++;
141 if (!qed_set_used_clusters(check, offset, s->header.table_size)) {
142 continue; /* skip an invalid table */
145 ret = qed_read_l2_table_sync(s, &check->request, offset);
147 check->result->check_errors++;
152 num_invalid_l2 = qed_check_l2_table(check,
153 check->request.l2_table->table);
155 /* Write out fixed L2 table */
156 if (num_invalid_l2 > 0 && check->fix) {
157 ret = qed_write_l2_table_sync(s, &check->request, 0,
158 s->table_nelems, false);
160 check->result->check_errors++;
167 /* Drop reference to final table */
168 qed_unref_l2_cache_entry(check->request.l2_table);
169 check->request.l2_table = NULL;
171 /* Write out fixed L1 table */
172 if (num_invalid_l1 > 0 && check->fix) {
173 ret = qed_write_l1_table_sync(s, 0, s->table_nelems);
175 check->result->check_errors++;
184 * Check for unreferenced (leaked) clusters
186 static void qed_check_for_leaks(QEDCheck *check)
188 BDRVQEDState *s = check->s;
191 for (i = s->header.header_size; i < check->nclusters; i++) {
192 if (!qed_test_bit(check->used_clusters, i)) {
193 check->result->leaks++;
199 * Mark an image clean once it passes check or has been repaired
201 static void qed_check_mark_clean(BDRVQEDState *s, BdrvCheckResult *result)
203 /* Skip if there were unfixable corruptions or I/O errors */
204 if (result->corruptions > 0 || result->check_errors > 0) {
208 /* Skip if image is already marked clean */
209 if (!(s->header.features & QED_F_NEED_CHECK)) {
213 /* Ensure fixes reach storage before clearing check bit */
216 s->header.features &= ~QED_F_NEED_CHECK;
217 qed_write_header_sync(s);
220 /* Called with table_lock held. */
221 int coroutine_fn qed_check(BDRVQEDState *s, BdrvCheckResult *result, bool fix)
226 .nclusters = qed_bytes_to_clusters(s, s->file_size),
227 .request = { .l2_table = NULL },
232 check.used_clusters = g_try_new0(uint32_t, (check.nclusters + 31) / 32);
233 if (check.nclusters && check.used_clusters == NULL) {
237 check.result->bfi.total_clusters =
238 DIV_ROUND_UP(s->header.image_size, s->header.cluster_size);
239 ret = qed_check_l1_table(&check, s->l1_table);
241 /* Only check for leaks if entire image was scanned successfully */
242 qed_check_for_leaks(&check);
245 qed_check_mark_clean(s, result);
249 g_free(check.used_clusters);