USB: usb-storage: unusual_devs update for Super TOP SATA bridge
[linux/fpc-iii.git] / drivers / md / persistent-data / dm-space-map-checker.c
blobfc90c11620adb026c9842d6e95497248d02556e8
1 /*
2 * Copyright (C) 2011 Red Hat, Inc.
4 * This file is released under the GPL.
5 */
7 #include "dm-space-map-checker.h"
9 #include <linux/device-mapper.h>
10 #include <linux/export.h>
11 #include <linux/vmalloc.h>
13 #ifdef CONFIG_DM_DEBUG_SPACE_MAPS
15 #define DM_MSG_PREFIX "space map checker"
17 /*----------------------------------------------------------------*/
19 struct count_array {
20 dm_block_t nr;
21 dm_block_t nr_free;
23 uint32_t *counts;
26 static int ca_get_count(struct count_array *ca, dm_block_t b, uint32_t *count)
28 if (b >= ca->nr)
29 return -EINVAL;
31 *count = ca->counts[b];
32 return 0;
35 static int ca_count_more_than_one(struct count_array *ca, dm_block_t b, int *r)
37 if (b >= ca->nr)
38 return -EINVAL;
40 *r = ca->counts[b] > 1;
41 return 0;
44 static int ca_set_count(struct count_array *ca, dm_block_t b, uint32_t count)
46 uint32_t old_count;
48 if (b >= ca->nr)
49 return -EINVAL;
51 old_count = ca->counts[b];
53 if (!count && old_count)
54 ca->nr_free++;
56 else if (count && !old_count)
57 ca->nr_free--;
59 ca->counts[b] = count;
60 return 0;
63 static int ca_inc_block(struct count_array *ca, dm_block_t b)
65 if (b >= ca->nr)
66 return -EINVAL;
68 ca_set_count(ca, b, ca->counts[b] + 1);
69 return 0;
72 static int ca_dec_block(struct count_array *ca, dm_block_t b)
74 if (b >= ca->nr)
75 return -EINVAL;
77 BUG_ON(ca->counts[b] == 0);
78 ca_set_count(ca, b, ca->counts[b] - 1);
79 return 0;
82 static int ca_create(struct count_array *ca, struct dm_space_map *sm)
84 int r;
85 dm_block_t nr_blocks;
87 r = dm_sm_get_nr_blocks(sm, &nr_blocks);
88 if (r)
89 return r;
91 ca->nr = nr_blocks;
92 ca->nr_free = nr_blocks;
94 if (!nr_blocks)
95 ca->counts = NULL;
96 else {
97 ca->counts = vzalloc(sizeof(*ca->counts) * nr_blocks);
98 if (!ca->counts)
99 return -ENOMEM;
102 return 0;
105 static void ca_destroy(struct count_array *ca)
107 vfree(ca->counts);
110 static int ca_load(struct count_array *ca, struct dm_space_map *sm)
112 int r;
113 uint32_t count;
114 dm_block_t nr_blocks, i;
116 r = dm_sm_get_nr_blocks(sm, &nr_blocks);
117 if (r)
118 return r;
120 BUG_ON(ca->nr != nr_blocks);
122 DMWARN("Loading debug space map from disk. This may take some time");
123 for (i = 0; i < nr_blocks; i++) {
124 r = dm_sm_get_count(sm, i, &count);
125 if (r) {
126 DMERR("load failed");
127 return r;
130 ca_set_count(ca, i, count);
132 DMWARN("Load complete");
134 return 0;
137 static int ca_extend(struct count_array *ca, dm_block_t extra_blocks)
139 dm_block_t nr_blocks = ca->nr + extra_blocks;
140 uint32_t *counts = vzalloc(sizeof(*counts) * nr_blocks);
141 if (!counts)
142 return -ENOMEM;
144 if (ca->counts) {
145 memcpy(counts, ca->counts, sizeof(*counts) * ca->nr);
146 ca_destroy(ca);
148 ca->nr = nr_blocks;
149 ca->nr_free += extra_blocks;
150 ca->counts = counts;
151 return 0;
154 static int ca_commit(struct count_array *old, struct count_array *new)
156 if (old->nr != new->nr) {
157 BUG_ON(old->nr > new->nr);
158 ca_extend(old, new->nr - old->nr);
161 BUG_ON(old->nr != new->nr);
162 old->nr_free = new->nr_free;
163 memcpy(old->counts, new->counts, sizeof(*old->counts) * old->nr);
164 return 0;
167 /*----------------------------------------------------------------*/
169 struct sm_checker {
170 struct dm_space_map sm;
172 struct count_array old_counts;
173 struct count_array counts;
175 struct dm_space_map *real_sm;
178 static void sm_checker_destroy(struct dm_space_map *sm)
180 struct sm_checker *smc = container_of(sm, struct sm_checker, sm);
182 dm_sm_destroy(smc->real_sm);
183 ca_destroy(&smc->old_counts);
184 ca_destroy(&smc->counts);
185 kfree(smc);
188 static int sm_checker_get_nr_blocks(struct dm_space_map *sm, dm_block_t *count)
190 struct sm_checker *smc = container_of(sm, struct sm_checker, sm);
191 int r = dm_sm_get_nr_blocks(smc->real_sm, count);
192 if (!r)
193 BUG_ON(smc->old_counts.nr != *count);
194 return r;
197 static int sm_checker_get_nr_free(struct dm_space_map *sm, dm_block_t *count)
199 struct sm_checker *smc = container_of(sm, struct sm_checker, sm);
200 int r = dm_sm_get_nr_free(smc->real_sm, count);
201 if (!r) {
203 * Slow, but we know it's correct.
205 dm_block_t b, n = 0;
206 for (b = 0; b < smc->old_counts.nr; b++)
207 if (smc->old_counts.counts[b] == 0 &&
208 smc->counts.counts[b] == 0)
209 n++;
211 if (n != *count)
212 DMERR("free block counts differ, checker %u, sm-disk:%u",
213 (unsigned) n, (unsigned) *count);
215 return r;
218 static int sm_checker_new_block(struct dm_space_map *sm, dm_block_t *b)
220 struct sm_checker *smc = container_of(sm, struct sm_checker, sm);
221 int r = dm_sm_new_block(smc->real_sm, b);
223 if (!r) {
224 BUG_ON(*b >= smc->old_counts.nr);
225 BUG_ON(smc->old_counts.counts[*b] != 0);
226 BUG_ON(*b >= smc->counts.nr);
227 BUG_ON(smc->counts.counts[*b] != 0);
228 ca_set_count(&smc->counts, *b, 1);
231 return r;
234 static int sm_checker_inc_block(struct dm_space_map *sm, dm_block_t b)
236 struct sm_checker *smc = container_of(sm, struct sm_checker, sm);
237 int r = dm_sm_inc_block(smc->real_sm, b);
238 int r2 = ca_inc_block(&smc->counts, b);
239 BUG_ON(r != r2);
240 return r;
243 static int sm_checker_dec_block(struct dm_space_map *sm, dm_block_t b)
245 struct sm_checker *smc = container_of(sm, struct sm_checker, sm);
246 int r = dm_sm_dec_block(smc->real_sm, b);
247 int r2 = ca_dec_block(&smc->counts, b);
248 BUG_ON(r != r2);
249 return r;
252 static int sm_checker_get_count(struct dm_space_map *sm, dm_block_t b, uint32_t *result)
254 struct sm_checker *smc = container_of(sm, struct sm_checker, sm);
255 uint32_t result2 = 0;
256 int r = dm_sm_get_count(smc->real_sm, b, result);
257 int r2 = ca_get_count(&smc->counts, b, &result2);
259 BUG_ON(r != r2);
260 if (!r)
261 BUG_ON(*result != result2);
262 return r;
265 static int sm_checker_count_more_than_one(struct dm_space_map *sm, dm_block_t b, int *result)
267 struct sm_checker *smc = container_of(sm, struct sm_checker, sm);
268 int result2 = 0;
269 int r = dm_sm_count_is_more_than_one(smc->real_sm, b, result);
270 int r2 = ca_count_more_than_one(&smc->counts, b, &result2);
272 BUG_ON(r != r2);
273 if (!r)
274 BUG_ON(!(*result) && result2);
275 return r;
278 static int sm_checker_set_count(struct dm_space_map *sm, dm_block_t b, uint32_t count)
280 struct sm_checker *smc = container_of(sm, struct sm_checker, sm);
281 uint32_t old_rc;
282 int r = dm_sm_set_count(smc->real_sm, b, count);
283 int r2;
285 BUG_ON(b >= smc->counts.nr);
286 old_rc = smc->counts.counts[b];
287 r2 = ca_set_count(&smc->counts, b, count);
288 BUG_ON(r != r2);
290 return r;
293 static int sm_checker_commit(struct dm_space_map *sm)
295 struct sm_checker *smc = container_of(sm, struct sm_checker, sm);
296 int r;
298 r = dm_sm_commit(smc->real_sm);
299 if (r)
300 return r;
302 r = ca_commit(&smc->old_counts, &smc->counts);
303 if (r)
304 return r;
306 return 0;
309 static int sm_checker_extend(struct dm_space_map *sm, dm_block_t extra_blocks)
311 struct sm_checker *smc = container_of(sm, struct sm_checker, sm);
312 int r = dm_sm_extend(smc->real_sm, extra_blocks);
313 if (r)
314 return r;
316 return ca_extend(&smc->counts, extra_blocks);
319 static int sm_checker_root_size(struct dm_space_map *sm, size_t *result)
321 struct sm_checker *smc = container_of(sm, struct sm_checker, sm);
322 return dm_sm_root_size(smc->real_sm, result);
325 static int sm_checker_copy_root(struct dm_space_map *sm, void *copy_to_here_le, size_t len)
327 struct sm_checker *smc = container_of(sm, struct sm_checker, sm);
328 return dm_sm_copy_root(smc->real_sm, copy_to_here_le, len);
331 /*----------------------------------------------------------------*/
333 static struct dm_space_map ops_ = {
334 .destroy = sm_checker_destroy,
335 .get_nr_blocks = sm_checker_get_nr_blocks,
336 .get_nr_free = sm_checker_get_nr_free,
337 .inc_block = sm_checker_inc_block,
338 .dec_block = sm_checker_dec_block,
339 .new_block = sm_checker_new_block,
340 .get_count = sm_checker_get_count,
341 .count_is_more_than_one = sm_checker_count_more_than_one,
342 .set_count = sm_checker_set_count,
343 .commit = sm_checker_commit,
344 .extend = sm_checker_extend,
345 .root_size = sm_checker_root_size,
346 .copy_root = sm_checker_copy_root
349 struct dm_space_map *dm_sm_checker_create(struct dm_space_map *sm)
351 int r;
352 struct sm_checker *smc;
354 if (IS_ERR_OR_NULL(sm))
355 return ERR_PTR(-EINVAL);
357 smc = kmalloc(sizeof(*smc), GFP_KERNEL);
358 if (!smc)
359 return ERR_PTR(-ENOMEM);
361 memcpy(&smc->sm, &ops_, sizeof(smc->sm));
362 r = ca_create(&smc->old_counts, sm);
363 if (r) {
364 kfree(smc);
365 return ERR_PTR(r);
368 r = ca_create(&smc->counts, sm);
369 if (r) {
370 ca_destroy(&smc->old_counts);
371 kfree(smc);
372 return ERR_PTR(r);
375 smc->real_sm = sm;
377 r = ca_load(&smc->counts, sm);
378 if (r) {
379 ca_destroy(&smc->counts);
380 ca_destroy(&smc->old_counts);
381 kfree(smc);
382 return ERR_PTR(r);
385 r = ca_commit(&smc->old_counts, &smc->counts);
386 if (r) {
387 ca_destroy(&smc->counts);
388 ca_destroy(&smc->old_counts);
389 kfree(smc);
390 return ERR_PTR(r);
393 return &smc->sm;
395 EXPORT_SYMBOL_GPL(dm_sm_checker_create);
397 struct dm_space_map *dm_sm_checker_create_fresh(struct dm_space_map *sm)
399 int r;
400 struct sm_checker *smc;
402 if (IS_ERR_OR_NULL(sm))
403 return ERR_PTR(-EINVAL);
405 smc = kmalloc(sizeof(*smc), GFP_KERNEL);
406 if (!smc)
407 return ERR_PTR(-ENOMEM);
409 memcpy(&smc->sm, &ops_, sizeof(smc->sm));
410 r = ca_create(&smc->old_counts, sm);
411 if (r) {
412 kfree(smc);
413 return ERR_PTR(r);
416 r = ca_create(&smc->counts, sm);
417 if (r) {
418 ca_destroy(&smc->old_counts);
419 kfree(smc);
420 return ERR_PTR(r);
423 smc->real_sm = sm;
424 return &smc->sm;
426 EXPORT_SYMBOL_GPL(dm_sm_checker_create_fresh);
428 /*----------------------------------------------------------------*/
430 #else
432 struct dm_space_map *dm_sm_checker_create(struct dm_space_map *sm)
434 return sm;
436 EXPORT_SYMBOL_GPL(dm_sm_checker_create);
438 struct dm_space_map *dm_sm_checker_create_fresh(struct dm_space_map *sm)
440 return sm;
442 EXPORT_SYMBOL_GPL(dm_sm_checker_create_fresh);
444 /*----------------------------------------------------------------*/
446 #endif