hw/audio/virtio-snd-pci: fix the PCI class code
[qemu/ar7.git] / block / qed-check.c
blob6a01b94f9c9c27352d3fec2c5c6777d484a35213
1 /*
2 * QEMU Enhanced Disk Format Consistency Check
4 * Copyright IBM, Corp. 2010
6 * Authors:
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"
15 #include "block/block-io.h"
16 #include "qed.h"
18 typedef struct {
19 BDRVQEDState *s;
20 BdrvCheckResult *result;
21 bool fix; /* whether to fix invalid offsets */
23 uint64_t nclusters;
24 uint32_t *used_clusters; /* referenced cluster bitmap */
26 QEDRequest request;
27 } QEDCheck;
29 static bool qed_test_bit(uint32_t *bitmap, uint64_t n) {
30 return !!(bitmap[n / 32] & (1 << (n % 32)));
33 static void qed_set_bit(uint32_t *bitmap, uint64_t n) {
34 bitmap[n / 32] |= 1 << (n % 32);
37 /**
38 * Set bitmap bits for clusters
40 * @check: Check structure
41 * @offset: Starting offset in bytes
42 * @n: Number of clusters
44 static bool qed_set_used_clusters(QEDCheck *check, uint64_t offset,
45 unsigned int n)
47 uint64_t cluster = qed_bytes_to_clusters(check->s, offset);
48 unsigned int corruptions = 0;
50 while (n-- != 0) {
51 /* Clusters should only be referenced once */
52 if (qed_test_bit(check->used_clusters, cluster)) {
53 corruptions++;
56 qed_set_bit(check->used_clusters, cluster);
57 cluster++;
60 check->result->corruptions += corruptions;
61 return corruptions == 0;
64 /**
65 * Check an L2 table
67 * @ret: Number of invalid cluster offsets
69 static unsigned int qed_check_l2_table(QEDCheck *check, QEDTable *table)
71 BDRVQEDState *s = check->s;
72 unsigned int i, num_invalid = 0;
73 uint64_t last_offset = 0;
75 for (i = 0; i < s->table_nelems; i++) {
76 uint64_t offset = table->offsets[i];
78 if (qed_offset_is_unalloc_cluster(offset) ||
79 qed_offset_is_zero_cluster(offset)) {
80 continue;
82 check->result->bfi.allocated_clusters++;
83 if (last_offset && (last_offset + s->header.cluster_size != offset)) {
84 check->result->bfi.fragmented_clusters++;
86 last_offset = offset;
88 /* Detect invalid cluster offset */
89 if (!qed_check_cluster_offset(s, offset)) {
90 if (check->fix) {
91 table->offsets[i] = 0;
92 check->result->corruptions_fixed++;
93 } else {
94 check->result->corruptions++;
97 num_invalid++;
98 continue;
101 qed_set_used_clusters(check, offset, 1);
104 return num_invalid;
108 * Descend tables and check each cluster is referenced once only
110 static int coroutine_fn GRAPH_RDLOCK
111 qed_check_l1_table(QEDCheck *check, QEDTable *table)
113 BDRVQEDState *s = check->s;
114 unsigned int i, num_invalid_l1 = 0;
115 int ret, last_error = 0;
117 /* Mark L1 table clusters used */
118 qed_set_used_clusters(check, s->header.l1_table_offset,
119 s->header.table_size);
121 for (i = 0; i < s->table_nelems; i++) {
122 unsigned int num_invalid_l2;
123 uint64_t offset = table->offsets[i];
125 if (qed_offset_is_unalloc_cluster(offset)) {
126 continue;
129 /* Detect invalid L2 offset */
130 if (!qed_check_table_offset(s, offset)) {
131 /* Clear invalid offset */
132 if (check->fix) {
133 table->offsets[i] = 0;
134 check->result->corruptions_fixed++;
135 } else {
136 check->result->corruptions++;
139 num_invalid_l1++;
140 continue;
143 if (!qed_set_used_clusters(check, offset, s->header.table_size)) {
144 continue; /* skip an invalid table */
147 ret = qed_read_l2_table_sync(s, &check->request, offset);
148 if (ret) {
149 check->result->check_errors++;
150 last_error = ret;
151 continue;
154 num_invalid_l2 = qed_check_l2_table(check,
155 check->request.l2_table->table);
157 /* Write out fixed L2 table */
158 if (num_invalid_l2 > 0 && check->fix) {
159 ret = qed_write_l2_table_sync(s, &check->request, 0,
160 s->table_nelems, false);
161 if (ret) {
162 check->result->check_errors++;
163 last_error = ret;
164 continue;
169 /* Drop reference to final table */
170 qed_unref_l2_cache_entry(check->request.l2_table);
171 check->request.l2_table = NULL;
173 /* Write out fixed L1 table */
174 if (num_invalid_l1 > 0 && check->fix) {
175 ret = qed_write_l1_table_sync(s, 0, s->table_nelems);
176 if (ret) {
177 check->result->check_errors++;
178 last_error = ret;
182 return last_error;
186 * Check for unreferenced (leaked) clusters
188 static void qed_check_for_leaks(QEDCheck *check)
190 BDRVQEDState *s = check->s;
191 uint64_t i;
193 for (i = s->header.header_size; i < check->nclusters; i++) {
194 if (!qed_test_bit(check->used_clusters, i)) {
195 check->result->leaks++;
201 * Mark an image clean once it passes check or has been repaired
203 static void coroutine_fn GRAPH_RDLOCK
204 qed_check_mark_clean(BDRVQEDState *s, BdrvCheckResult *result)
206 /* Skip if there were unfixable corruptions or I/O errors */
207 if (result->corruptions > 0 || result->check_errors > 0) {
208 return;
211 /* Skip if image is already marked clean */
212 if (!(s->header.features & QED_F_NEED_CHECK)) {
213 return;
216 /* Ensure fixes reach storage before clearing check bit */
217 bdrv_co_flush(s->bs);
219 s->header.features &= ~QED_F_NEED_CHECK;
220 qed_write_header_sync(s);
223 /* Called with table_lock held. */
224 int coroutine_fn qed_check(BDRVQEDState *s, BdrvCheckResult *result, bool fix)
226 QEDCheck check = {
227 .s = s,
228 .result = result,
229 .nclusters = qed_bytes_to_clusters(s, s->file_size),
230 .request = { .l2_table = NULL },
231 .fix = fix,
233 int ret;
235 check.used_clusters = g_try_new0(uint32_t, (check.nclusters + 31) / 32);
236 if (check.nclusters && check.used_clusters == NULL) {
237 return -ENOMEM;
240 check.result->bfi.total_clusters =
241 DIV_ROUND_UP(s->header.image_size, s->header.cluster_size);
242 ret = qed_check_l1_table(&check, s->l1_table);
243 if (ret == 0) {
244 /* Only check for leaks if entire image was scanned successfully */
245 qed_check_for_leaks(&check);
247 if (fix) {
248 qed_check_mark_clean(s, result);
252 g_free(check.used_clusters);
253 return ret;