dm-space-map-checker.c 9.5 KB
Newer Older
J
Joe Thornber 已提交
1 2 3 4 5 6 7 8 9
/*
 * Copyright (C) 2011 Red Hat, Inc.
 *
 * This file is released under the GPL.
 */

#include "dm-space-map-checker.h"

#include <linux/device-mapper.h>
10
#include <linux/export.h>
11
#include <linux/vmalloc.h>
J
Joe Thornber 已提交
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92

#ifdef CONFIG_DM_DEBUG_SPACE_MAPS

#define DM_MSG_PREFIX "space map checker"

/*----------------------------------------------------------------*/

struct count_array {
	dm_block_t nr;
	dm_block_t nr_free;

	uint32_t *counts;
};

static int ca_get_count(struct count_array *ca, dm_block_t b, uint32_t *count)
{
	if (b >= ca->nr)
		return -EINVAL;

	*count = ca->counts[b];
	return 0;
}

static int ca_count_more_than_one(struct count_array *ca, dm_block_t b, int *r)
{
	if (b >= ca->nr)
		return -EINVAL;

	*r = ca->counts[b] > 1;
	return 0;
}

static int ca_set_count(struct count_array *ca, dm_block_t b, uint32_t count)
{
	uint32_t old_count;

	if (b >= ca->nr)
		return -EINVAL;

	old_count = ca->counts[b];

	if (!count && old_count)
		ca->nr_free++;

	else if (count && !old_count)
		ca->nr_free--;

	ca->counts[b] = count;
	return 0;
}

static int ca_inc_block(struct count_array *ca, dm_block_t b)
{
	if (b >= ca->nr)
		return -EINVAL;

	ca_set_count(ca, b, ca->counts[b] + 1);
	return 0;
}

static int ca_dec_block(struct count_array *ca, dm_block_t b)
{
	if (b >= ca->nr)
		return -EINVAL;

	BUG_ON(ca->counts[b] == 0);
	ca_set_count(ca, b, ca->counts[b] - 1);
	return 0;
}

static int ca_create(struct count_array *ca, struct dm_space_map *sm)
{
	int r;
	dm_block_t nr_blocks;

	r = dm_sm_get_nr_blocks(sm, &nr_blocks);
	if (r)
		return r;

	ca->nr = nr_blocks;
	ca->nr_free = nr_blocks;
93 94 95 96 97 98 99 100

	if (!nr_blocks)
		ca->counts = NULL;
	else {
		ca->counts = vzalloc(sizeof(*ca->counts) * nr_blocks);
		if (!ca->counts)
			return -ENOMEM;
	}
J
Joe Thornber 已提交
101 102 103 104

	return 0;
}

105 106 107 108 109
static void ca_destroy(struct count_array *ca)
{
	vfree(ca->counts);
}

J
Joe Thornber 已提交
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
static int ca_load(struct count_array *ca, struct dm_space_map *sm)
{
	int r;
	uint32_t count;
	dm_block_t nr_blocks, i;

	r = dm_sm_get_nr_blocks(sm, &nr_blocks);
	if (r)
		return r;

	BUG_ON(ca->nr != nr_blocks);

	DMWARN("Loading debug space map from disk.  This may take some time");
	for (i = 0; i < nr_blocks; i++) {
		r = dm_sm_get_count(sm, i, &count);
		if (r) {
			DMERR("load failed");
			return r;
		}

		ca_set_count(ca, i, count);
	}
	DMWARN("Load complete");

	return 0;
}

static int ca_extend(struct count_array *ca, dm_block_t extra_blocks)
{
	dm_block_t nr_blocks = ca->nr + extra_blocks;
140
	uint32_t *counts = vzalloc(sizeof(*counts) * nr_blocks);
J
Joe Thornber 已提交
141 142 143
	if (!counts)
		return -ENOMEM;

144 145 146 147
	if (ca->counts) {
		memcpy(counts, ca->counts, sizeof(*counts) * ca->nr);
		ca_destroy(ca);
	}
J
Joe Thornber 已提交
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
	ca->nr = nr_blocks;
	ca->nr_free += extra_blocks;
	ca->counts = counts;
	return 0;
}

static int ca_commit(struct count_array *old, struct count_array *new)
{
	if (old->nr != new->nr) {
		BUG_ON(old->nr > new->nr);
		ca_extend(old, new->nr - old->nr);
	}

	BUG_ON(old->nr != new->nr);
	old->nr_free = new->nr_free;
	memcpy(old->counts, new->counts, sizeof(*old->counts) * old->nr);
	return 0;
}

/*----------------------------------------------------------------*/

struct sm_checker {
	struct dm_space_map sm;

	struct count_array old_counts;
	struct count_array counts;

	struct dm_space_map *real_sm;
};

static void sm_checker_destroy(struct dm_space_map *sm)
{
	struct sm_checker *smc = container_of(sm, struct sm_checker, sm);

	dm_sm_destroy(smc->real_sm);
	ca_destroy(&smc->old_counts);
	ca_destroy(&smc->counts);
	kfree(smc);
}

static int sm_checker_get_nr_blocks(struct dm_space_map *sm, dm_block_t *count)
{
	struct sm_checker *smc = container_of(sm, struct sm_checker, sm);
	int r = dm_sm_get_nr_blocks(smc->real_sm, count);
	if (!r)
		BUG_ON(smc->old_counts.nr != *count);
	return r;
}

static int sm_checker_get_nr_free(struct dm_space_map *sm, dm_block_t *count)
{
	struct sm_checker *smc = container_of(sm, struct sm_checker, sm);
	int r = dm_sm_get_nr_free(smc->real_sm, count);
	if (!r) {
		/*
		 * Slow, but we know it's correct.
		 */
		dm_block_t b, n = 0;
		for (b = 0; b < smc->old_counts.nr; b++)
			if (smc->old_counts.counts[b] == 0 &&
			    smc->counts.counts[b] == 0)
				n++;

		if (n != *count)
			DMERR("free block counts differ, checker %u, sm-disk:%u",
			      (unsigned) n, (unsigned) *count);
	}
	return r;
}

static int sm_checker_new_block(struct dm_space_map *sm, dm_block_t *b)
{
	struct sm_checker *smc = container_of(sm, struct sm_checker, sm);
	int r = dm_sm_new_block(smc->real_sm, b);

	if (!r) {
		BUG_ON(*b >= smc->old_counts.nr);
		BUG_ON(smc->old_counts.counts[*b] != 0);
		BUG_ON(*b >= smc->counts.nr);
		BUG_ON(smc->counts.counts[*b] != 0);
		ca_set_count(&smc->counts, *b, 1);
	}

	return r;
}

static int sm_checker_inc_block(struct dm_space_map *sm, dm_block_t b)
{
	struct sm_checker *smc = container_of(sm, struct sm_checker, sm);
	int r = dm_sm_inc_block(smc->real_sm, b);
	int r2 = ca_inc_block(&smc->counts, b);
	BUG_ON(r != r2);
	return r;
}

static int sm_checker_dec_block(struct dm_space_map *sm, dm_block_t b)
{
	struct sm_checker *smc = container_of(sm, struct sm_checker, sm);
	int r = dm_sm_dec_block(smc->real_sm, b);
	int r2 = ca_dec_block(&smc->counts, b);
	BUG_ON(r != r2);
	return r;
}

static int sm_checker_get_count(struct dm_space_map *sm, dm_block_t b, uint32_t *result)
{
	struct sm_checker *smc = container_of(sm, struct sm_checker, sm);
	uint32_t result2 = 0;
	int r = dm_sm_get_count(smc->real_sm, b, result);
	int r2 = ca_get_count(&smc->counts, b, &result2);

	BUG_ON(r != r2);
	if (!r)
		BUG_ON(*result != result2);
	return r;
}

static int sm_checker_count_more_than_one(struct dm_space_map *sm, dm_block_t b, int *result)
{
	struct sm_checker *smc = container_of(sm, struct sm_checker, sm);
	int result2 = 0;
	int r = dm_sm_count_is_more_than_one(smc->real_sm, b, result);
	int r2 = ca_count_more_than_one(&smc->counts, b, &result2);

	BUG_ON(r != r2);
	if (!r)
		BUG_ON(!(*result) && result2);
	return r;
}

static int sm_checker_set_count(struct dm_space_map *sm, dm_block_t b, uint32_t count)
{
	struct sm_checker *smc = container_of(sm, struct sm_checker, sm);
	uint32_t old_rc;
	int r = dm_sm_set_count(smc->real_sm, b, count);
	int r2;

	BUG_ON(b >= smc->counts.nr);
	old_rc = smc->counts.counts[b];
	r2 = ca_set_count(&smc->counts, b, count);
	BUG_ON(r != r2);

	return r;
}

static int sm_checker_commit(struct dm_space_map *sm)
{
	struct sm_checker *smc = container_of(sm, struct sm_checker, sm);
	int r;

	r = dm_sm_commit(smc->real_sm);
	if (r)
		return r;

	r = ca_commit(&smc->old_counts, &smc->counts);
	if (r)
		return r;

	return 0;
}

static int sm_checker_extend(struct dm_space_map *sm, dm_block_t extra_blocks)
{
	struct sm_checker *smc = container_of(sm, struct sm_checker, sm);
	int r = dm_sm_extend(smc->real_sm, extra_blocks);
	if (r)
		return r;

	return ca_extend(&smc->counts, extra_blocks);
}

static int sm_checker_root_size(struct dm_space_map *sm, size_t *result)
{
	struct sm_checker *smc = container_of(sm, struct sm_checker, sm);
	return dm_sm_root_size(smc->real_sm, result);
}

static int sm_checker_copy_root(struct dm_space_map *sm, void *copy_to_here_le, size_t len)
{
	struct sm_checker *smc = container_of(sm, struct sm_checker, sm);
	return dm_sm_copy_root(smc->real_sm, copy_to_here_le, len);
}

/*----------------------------------------------------------------*/

static struct dm_space_map ops_ = {
	.destroy = sm_checker_destroy,
	.get_nr_blocks = sm_checker_get_nr_blocks,
	.get_nr_free = sm_checker_get_nr_free,
	.inc_block = sm_checker_inc_block,
	.dec_block = sm_checker_dec_block,
	.new_block = sm_checker_new_block,
	.get_count = sm_checker_get_count,
	.count_is_more_than_one = sm_checker_count_more_than_one,
	.set_count = sm_checker_set_count,
	.commit = sm_checker_commit,
	.extend = sm_checker_extend,
	.root_size = sm_checker_root_size,
	.copy_root = sm_checker_copy_root
};

struct dm_space_map *dm_sm_checker_create(struct dm_space_map *sm)
{
	int r;
	struct sm_checker *smc;

354 355
	if (IS_ERR_OR_NULL(sm))
		return ERR_PTR(-EINVAL);
J
Joe Thornber 已提交
356 357 358

	smc = kmalloc(sizeof(*smc), GFP_KERNEL);
	if (!smc)
359
		return ERR_PTR(-ENOMEM);
J
Joe Thornber 已提交
360 361 362 363 364

	memcpy(&smc->sm, &ops_, sizeof(smc->sm));
	r = ca_create(&smc->old_counts, sm);
	if (r) {
		kfree(smc);
365
		return ERR_PTR(r);
J
Joe Thornber 已提交
366 367 368 369 370 371
	}

	r = ca_create(&smc->counts, sm);
	if (r) {
		ca_destroy(&smc->old_counts);
		kfree(smc);
372
		return ERR_PTR(r);
J
Joe Thornber 已提交
373 374 375 376 377 378 379 380 381
	}

	smc->real_sm = sm;

	r = ca_load(&smc->counts, sm);
	if (r) {
		ca_destroy(&smc->counts);
		ca_destroy(&smc->old_counts);
		kfree(smc);
382
		return ERR_PTR(r);
J
Joe Thornber 已提交
383 384 385 386 387 388 389
	}

	r = ca_commit(&smc->old_counts, &smc->counts);
	if (r) {
		ca_destroy(&smc->counts);
		ca_destroy(&smc->old_counts);
		kfree(smc);
390
		return ERR_PTR(r);
J
Joe Thornber 已提交
391 392 393 394 395 396 397 398 399 400 401
	}

	return &smc->sm;
}
EXPORT_SYMBOL_GPL(dm_sm_checker_create);

struct dm_space_map *dm_sm_checker_create_fresh(struct dm_space_map *sm)
{
	int r;
	struct sm_checker *smc;

402 403
	if (IS_ERR_OR_NULL(sm))
		return ERR_PTR(-EINVAL);
J
Joe Thornber 已提交
404 405 406

	smc = kmalloc(sizeof(*smc), GFP_KERNEL);
	if (!smc)
407
		return ERR_PTR(-ENOMEM);
J
Joe Thornber 已提交
408 409 410 411 412

	memcpy(&smc->sm, &ops_, sizeof(smc->sm));
	r = ca_create(&smc->old_counts, sm);
	if (r) {
		kfree(smc);
413
		return ERR_PTR(r);
J
Joe Thornber 已提交
414 415 416 417 418 419
	}

	r = ca_create(&smc->counts, sm);
	if (r) {
		ca_destroy(&smc->old_counts);
		kfree(smc);
420
		return ERR_PTR(r);
J
Joe Thornber 已提交
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
	}

	smc->real_sm = sm;
	return &smc->sm;
}
EXPORT_SYMBOL_GPL(dm_sm_checker_create_fresh);

/*----------------------------------------------------------------*/

#else

struct dm_space_map *dm_sm_checker_create(struct dm_space_map *sm)
{
	return sm;
}
EXPORT_SYMBOL_GPL(dm_sm_checker_create);

struct dm_space_map *dm_sm_checker_create_fresh(struct dm_space_map *sm)
{
	return sm;
}
EXPORT_SYMBOL_GPL(dm_sm_checker_create_fresh);

/*----------------------------------------------------------------*/

#endif