xref: /openbmc/qemu/block/qed-check.c (revision 36ebc7db)
1 /*
2  * QEMU Enhanced Disk Format Consistency Check
3  *
4  * Copyright IBM, Corp. 2010
5  *
6  * Authors:
7  *  Stefan Hajnoczi   <stefanha@linux.vnet.ibm.com>
8  *
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.
11  *
12  */
13 
14 #include "qemu/osdep.h"
15 #include "block/block-io.h"
16 #include "qed.h"
17 
18 typedef struct {
19     BDRVQEDState *s;
20     BdrvCheckResult *result;
21     bool fix;                           /* whether to fix invalid offsets */
22 
23     uint64_t nclusters;
24     uint32_t *used_clusters;            /* referenced cluster bitmap */
25 
26     QEDRequest request;
27 } QEDCheck;
28 
29 static bool qed_test_bit(uint32_t *bitmap, uint64_t n) {
30     return !!(bitmap[n / 32] & (1 << (n % 32)));
31 }
32 
33 static void qed_set_bit(uint32_t *bitmap, uint64_t n) {
34     bitmap[n / 32] |= 1 << (n % 32);
35 }
36 
37 /**
38  * Set bitmap bits for clusters
39  *
40  * @check:          Check structure
41  * @offset:         Starting offset in bytes
42  * @n:              Number of clusters
43  */
44 static bool qed_set_used_clusters(QEDCheck *check, uint64_t offset,
45                                   unsigned int n)
46 {
47     uint64_t cluster = qed_bytes_to_clusters(check->s, offset);
48     unsigned int corruptions = 0;
49 
50     while (n-- != 0) {
51         /* Clusters should only be referenced once */
52         if (qed_test_bit(check->used_clusters, cluster)) {
53             corruptions++;
54         }
55 
56         qed_set_bit(check->used_clusters, cluster);
57         cluster++;
58     }
59 
60     check->result->corruptions += corruptions;
61     return corruptions == 0;
62 }
63 
64 /**
65  * Check an L2 table
66  *
67  * @ret:            Number of invalid cluster offsets
68  */
69 static unsigned int qed_check_l2_table(QEDCheck *check, QEDTable *table)
70 {
71     BDRVQEDState *s = check->s;
72     unsigned int i, num_invalid = 0;
73     uint64_t last_offset = 0;
74 
75     for (i = 0; i < s->table_nelems; i++) {
76         uint64_t offset = table->offsets[i];
77 
78         if (qed_offset_is_unalloc_cluster(offset) ||
79             qed_offset_is_zero_cluster(offset)) {
80             continue;
81         }
82         check->result->bfi.allocated_clusters++;
83         if (last_offset && (last_offset + s->header.cluster_size != offset)) {
84             check->result->bfi.fragmented_clusters++;
85         }
86         last_offset = offset;
87 
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++;
95             }
96 
97             num_invalid++;
98             continue;
99         }
100 
101         qed_set_used_clusters(check, offset, 1);
102     }
103 
104     return num_invalid;
105 }
106 
107 /**
108  * Descend tables and check each cluster is referenced once only
109  */
110 static int coroutine_fn qed_check_l1_table(QEDCheck *check, QEDTable *table)
111 {
112     BDRVQEDState *s = check->s;
113     unsigned int i, num_invalid_l1 = 0;
114     int ret, last_error = 0;
115 
116     /* Mark L1 table clusters used */
117     qed_set_used_clusters(check, s->header.l1_table_offset,
118                           s->header.table_size);
119 
120     for (i = 0; i < s->table_nelems; i++) {
121         unsigned int num_invalid_l2;
122         uint64_t offset = table->offsets[i];
123 
124         if (qed_offset_is_unalloc_cluster(offset)) {
125             continue;
126         }
127 
128         /* Detect invalid L2 offset */
129         if (!qed_check_table_offset(s, offset)) {
130             /* Clear invalid offset */
131             if (check->fix) {
132                 table->offsets[i] = 0;
133                 check->result->corruptions_fixed++;
134             } else {
135                 check->result->corruptions++;
136             }
137 
138             num_invalid_l1++;
139             continue;
140         }
141 
142         if (!qed_set_used_clusters(check, offset, s->header.table_size)) {
143             continue; /* skip an invalid table */
144         }
145 
146         ret = qed_read_l2_table_sync(s, &check->request, offset);
147         if (ret) {
148             check->result->check_errors++;
149             last_error = ret;
150             continue;
151         }
152 
153         num_invalid_l2 = qed_check_l2_table(check,
154                                             check->request.l2_table->table);
155 
156         /* Write out fixed L2 table */
157         if (num_invalid_l2 > 0 && check->fix) {
158             ret = qed_write_l2_table_sync(s, &check->request, 0,
159                                           s->table_nelems, false);
160             if (ret) {
161                 check->result->check_errors++;
162                 last_error = ret;
163                 continue;
164             }
165         }
166     }
167 
168     /* Drop reference to final table */
169     qed_unref_l2_cache_entry(check->request.l2_table);
170     check->request.l2_table = NULL;
171 
172     /* Write out fixed L1 table */
173     if (num_invalid_l1 > 0 && check->fix) {
174         ret = qed_write_l1_table_sync(s, 0, s->table_nelems);
175         if (ret) {
176             check->result->check_errors++;
177             last_error = ret;
178         }
179     }
180 
181     return last_error;
182 }
183 
184 /**
185  * Check for unreferenced (leaked) clusters
186  */
187 static void qed_check_for_leaks(QEDCheck *check)
188 {
189     BDRVQEDState *s = check->s;
190     uint64_t i;
191 
192     for (i = s->header.header_size; i < check->nclusters; i++) {
193         if (!qed_test_bit(check->used_clusters, i)) {
194             check->result->leaks++;
195         }
196     }
197 }
198 
199 /**
200  * Mark an image clean once it passes check or has been repaired
201  */
202 static void qed_check_mark_clean(BDRVQEDState *s, BdrvCheckResult *result)
203 {
204     /* Skip if there were unfixable corruptions or I/O errors */
205     if (result->corruptions > 0 || result->check_errors > 0) {
206         return;
207     }
208 
209     /* Skip if image is already marked clean */
210     if (!(s->header.features & QED_F_NEED_CHECK)) {
211         return;
212     }
213 
214     /* Ensure fixes reach storage before clearing check bit */
215     bdrv_flush(s->bs);
216 
217     s->header.features &= ~QED_F_NEED_CHECK;
218     qed_write_header_sync(s);
219 }
220 
221 /* Called with table_lock held.  */
222 int coroutine_fn qed_check(BDRVQEDState *s, BdrvCheckResult *result, bool fix)
223 {
224     QEDCheck check = {
225         .s = s,
226         .result = result,
227         .nclusters = qed_bytes_to_clusters(s, s->file_size),
228         .request = { .l2_table = NULL },
229         .fix = fix,
230     };
231     int ret;
232 
233     check.used_clusters = g_try_new0(uint32_t, (check.nclusters + 31) / 32);
234     if (check.nclusters && check.used_clusters == NULL) {
235         return -ENOMEM;
236     }
237 
238     check.result->bfi.total_clusters =
239         DIV_ROUND_UP(s->header.image_size, s->header.cluster_size);
240     ret = qed_check_l1_table(&check, s->l1_table);
241     if (ret == 0) {
242         /* Only check for leaks if entire image was scanned successfully */
243         qed_check_for_leaks(&check);
244 
245         if (fix) {
246             qed_check_mark_clean(s, result);
247         }
248     }
249 
250     g_free(check.used_clusters);
251     return ret;
252 }
253