dm-cache-metadata.c 34.8 KB
Newer Older
J
Joe Thornber 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * Copyright (C) 2012 Red Hat, Inc.
 *
 * This file is released under the GPL.
 */

#include "dm-cache-metadata.h"

#include "persistent-data/dm-array.h"
#include "persistent-data/dm-bitset.h"
#include "persistent-data/dm-space-map.h"
#include "persistent-data/dm-space-map-disk.h"
#include "persistent-data/dm-transaction-manager.h"

#include <linux/device-mapper.h>

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

#define DM_MSG_PREFIX   "cache metadata"

#define CACHE_SUPERBLOCK_MAGIC 06142003
#define CACHE_SUPERBLOCK_LOCATION 0
23 24 25 26 27 28 29

/*
 * defines a range of metadata versions that this module can handle.
 */
#define MIN_CACHE_VERSION 1
#define MAX_CACHE_VERSION 1

J
Joe Thornber 已提交
30 31 32 33 34 35 36 37 38 39 40 41
#define CACHE_METADATA_CACHE_SIZE 64

/*
 *  3 for btree insert +
 *  2 for btree lookup used within space map
 */
#define CACHE_MAX_CONCURRENT_LOCKS 5
#define SPACE_MAP_ROOT_SIZE 128

enum superblock_flag_bits {
	/* for spotting crashes that would invalidate the dirty bitset */
	CLEAN_SHUTDOWN,
42 43
	/* metadata must be checked using the tools */
	NEEDS_CHECK,
J
Joe Thornber 已提交
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 93
};

/*
 * Each mapping from cache block -> origin block carries a set of flags.
 */
enum mapping_bits {
	/*
	 * A valid mapping.  Because we're using an array we clear this
	 * flag for an non existant mapping.
	 */
	M_VALID = 1,

	/*
	 * The data on the cache is different from that on the origin.
	 */
	M_DIRTY = 2
};

struct cache_disk_superblock {
	__le32 csum;
	__le32 flags;
	__le64 blocknr;

	__u8 uuid[16];
	__le64 magic;
	__le32 version;

	__u8 policy_name[CACHE_POLICY_NAME_SIZE];
	__le32 policy_hint_size;

	__u8 metadata_space_map_root[SPACE_MAP_ROOT_SIZE];
	__le64 mapping_root;
	__le64 hint_root;

	__le64 discard_root;
	__le64 discard_block_size;
	__le64 discard_nr_blocks;

	__le32 data_block_size;
	__le32 metadata_block_size;
	__le32 cache_blocks;

	__le32 compat_flags;
	__le32 compat_ro_flags;
	__le32 incompat_flags;

	__le32 read_hits;
	__le32 read_misses;
	__le32 write_hits;
	__le32 write_misses;
94 95

	__le32 policy_version[CACHE_POLICY_VERSION_SIZE];
J
Joe Thornber 已提交
96 97 98
} __packed;

struct dm_cache_metadata {
99 100 101
	atomic_t ref_count;
	struct list_head list;

J
Joe Thornber 已提交
102 103 104 105 106 107 108 109 110 111
	struct block_device *bdev;
	struct dm_block_manager *bm;
	struct dm_space_map *metadata_sm;
	struct dm_transaction_manager *tm;

	struct dm_array_info info;
	struct dm_array_info hint_info;
	struct dm_disk_bitset discard_info;

	struct rw_semaphore root_lock;
112
	unsigned long flags;
J
Joe Thornber 已提交
113 114 115 116 117
	dm_block_t root;
	dm_block_t hint_root;
	dm_block_t discard_root;

	sector_t discard_block_size;
118
	dm_dblock_t discard_nr_blocks;
J
Joe Thornber 已提交
119 120 121 122 123 124 125

	sector_t data_block_size;
	dm_cblock_t cache_blocks;
	bool changed:1;
	bool clean_when_opened:1;

	char policy_name[CACHE_POLICY_NAME_SIZE];
126
	unsigned policy_version[CACHE_POLICY_VERSION_SIZE];
J
Joe Thornber 已提交
127 128
	size_t policy_hint_size;
	struct dm_cache_statistics stats;
129 130 131 132 133 134

	/*
	 * Reading the space map root can fail, so we read it into this
	 * buffer before the superblock is locked and updated.
	 */
	__u8 metadata_space_map_root[SPACE_MAP_ROOT_SIZE];
135 136 137 138 139 140 141 142

	/*
	 * Set if a transaction has to be aborted but the attempt to roll
	 * back to the previous (good) transaction failed.  The only
	 * metadata operation permissible in this state is the closing of
	 * the device.
	 */
	bool fail_io:1;
J
Joe Thornber 已提交
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
};

/*-------------------------------------------------------------------
 * superblock validator
 *-----------------------------------------------------------------*/

#define SUPERBLOCK_CSUM_XOR 9031977

static void sb_prepare_for_write(struct dm_block_validator *v,
				 struct dm_block *b,
				 size_t sb_block_size)
{
	struct cache_disk_superblock *disk_super = dm_block_data(b);

	disk_super->blocknr = cpu_to_le64(dm_block_location(b));
	disk_super->csum = cpu_to_le32(dm_bm_checksum(&disk_super->flags,
						      sb_block_size - sizeof(__le32),
						      SUPERBLOCK_CSUM_XOR));
}

163 164 165 166 167 168 169 170 171 172 173 174
static int check_metadata_version(struct cache_disk_superblock *disk_super)
{
	uint32_t metadata_version = le32_to_cpu(disk_super->version);
	if (metadata_version < MIN_CACHE_VERSION || metadata_version > MAX_CACHE_VERSION) {
		DMERR("Cache metadata version %u found, but only versions between %u and %u supported.",
		      metadata_version, MIN_CACHE_VERSION, MAX_CACHE_VERSION);
		return -EINVAL;
	}

	return 0;
}

J
Joe Thornber 已提交
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
static int sb_check(struct dm_block_validator *v,
		    struct dm_block *b,
		    size_t sb_block_size)
{
	struct cache_disk_superblock *disk_super = dm_block_data(b);
	__le32 csum_le;

	if (dm_block_location(b) != le64_to_cpu(disk_super->blocknr)) {
		DMERR("sb_check failed: blocknr %llu: wanted %llu",
		      le64_to_cpu(disk_super->blocknr),
		      (unsigned long long)dm_block_location(b));
		return -ENOTBLK;
	}

	if (le64_to_cpu(disk_super->magic) != CACHE_SUPERBLOCK_MAGIC) {
		DMERR("sb_check failed: magic %llu: wanted %llu",
		      le64_to_cpu(disk_super->magic),
		      (unsigned long long)CACHE_SUPERBLOCK_MAGIC);
		return -EILSEQ;
	}

	csum_le = cpu_to_le32(dm_bm_checksum(&disk_super->flags,
					     sb_block_size - sizeof(__le32),
					     SUPERBLOCK_CSUM_XOR));
	if (csum_le != disk_super->csum) {
		DMERR("sb_check failed: csum %u: wanted %u",
		      le32_to_cpu(csum_le), le32_to_cpu(disk_super->csum));
		return -EILSEQ;
	}

205
	return check_metadata_version(disk_super);
J
Joe Thornber 已提交
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
}

static struct dm_block_validator sb_validator = {
	.name = "superblock",
	.prepare_for_write = sb_prepare_for_write,
	.check = sb_check
};

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

static int superblock_read_lock(struct dm_cache_metadata *cmd,
				struct dm_block **sblock)
{
	return dm_bm_read_lock(cmd->bm, CACHE_SUPERBLOCK_LOCATION,
			       &sb_validator, sblock);
}

static int superblock_lock_zero(struct dm_cache_metadata *cmd,
				struct dm_block **sblock)
{
	return dm_bm_write_lock_zero(cmd->bm, CACHE_SUPERBLOCK_LOCATION,
				     &sb_validator, sblock);
}

static int superblock_lock(struct dm_cache_metadata *cmd,
			   struct dm_block **sblock)
{
	return dm_bm_write_lock(cmd->bm, CACHE_SUPERBLOCK_LOCATION,
				&sb_validator, sblock);
}

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

239
static int __superblock_all_zeroes(struct dm_block_manager *bm, bool *result)
J
Joe Thornber 已提交
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
{
	int r;
	unsigned i;
	struct dm_block *b;
	__le64 *data_le, zero = cpu_to_le64(0);
	unsigned sb_block_size = dm_bm_block_size(bm) / sizeof(__le64);

	/*
	 * We can't use a validator here - it may be all zeroes.
	 */
	r = dm_bm_read_lock(bm, CACHE_SUPERBLOCK_LOCATION, NULL, &b);
	if (r)
		return r;

	data_le = dm_block_data(b);
255
	*result = true;
J
Joe Thornber 已提交
256 257
	for (i = 0; i < sb_block_size; i++) {
		if (data_le[i] != zero) {
258
			*result = false;
J
Joe Thornber 已提交
259 260 261 262
			break;
		}
	}

263 264 265
	dm_bm_unlock(b);

	return 0;
J
Joe Thornber 已提交
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
}

static void __setup_mapping_info(struct dm_cache_metadata *cmd)
{
	struct dm_btree_value_type vt;

	vt.context = NULL;
	vt.size = sizeof(__le64);
	vt.inc = NULL;
	vt.dec = NULL;
	vt.equal = NULL;
	dm_array_info_init(&cmd->info, cmd->tm, &vt);

	if (cmd->policy_hint_size) {
		vt.size = sizeof(__le32);
		dm_array_info_init(&cmd->hint_info, cmd->tm, &vt);
	}
}

285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
static int __save_sm_root(struct dm_cache_metadata *cmd)
{
	int r;
	size_t metadata_len;

	r = dm_sm_root_size(cmd->metadata_sm, &metadata_len);
	if (r < 0)
		return r;

	return dm_sm_copy_root(cmd->metadata_sm, &cmd->metadata_space_map_root,
			       metadata_len);
}

static void __copy_sm_root(struct dm_cache_metadata *cmd,
			   struct cache_disk_superblock *disk_super)
{
	memcpy(&disk_super->metadata_space_map_root,
	       &cmd->metadata_space_map_root,
	       sizeof(cmd->metadata_space_map_root));
}

J
Joe Thornber 已提交
306 307 308 309 310 311 312 313 314 315 316
static int __write_initial_superblock(struct dm_cache_metadata *cmd)
{
	int r;
	struct dm_block *sblock;
	struct cache_disk_superblock *disk_super;
	sector_t bdev_size = i_size_read(cmd->bdev->bd_inode) >> SECTOR_SHIFT;

	/* FIXME: see if we can lose the max sectors limit */
	if (bdev_size > DM_CACHE_METADATA_MAX_SECTORS)
		bdev_size = DM_CACHE_METADATA_MAX_SECTORS;

317
	r = dm_tm_pre_commit(cmd->tm);
J
Joe Thornber 已提交
318 319 320
	if (r < 0)
		return r;

321 322 323 324 325 326
	/*
	 * dm_sm_copy_root() can fail.  So we need to do it before we start
	 * updating the superblock.
	 */
	r = __save_sm_root(cmd);
	if (r)
J
Joe Thornber 已提交
327 328 329 330 331 332 333 334 335 336
		return r;

	r = superblock_lock_zero(cmd, &sblock);
	if (r)
		return r;

	disk_super = dm_block_data(sblock);
	disk_super->flags = 0;
	memset(disk_super->uuid, 0, sizeof(disk_super->uuid));
	disk_super->magic = cpu_to_le64(CACHE_SUPERBLOCK_MAGIC);
337
	disk_super->version = cpu_to_le32(MAX_CACHE_VERSION);
338 339
	memset(disk_super->policy_name, 0, sizeof(disk_super->policy_name));
	memset(disk_super->policy_version, 0, sizeof(disk_super->policy_version));
J
Joe Thornber 已提交
340 341
	disk_super->policy_hint_size = 0;

342
	__copy_sm_root(cmd, disk_super);
J
Joe Thornber 已提交
343 344 345 346 347

	disk_super->mapping_root = cpu_to_le64(cmd->root);
	disk_super->hint_root = cpu_to_le64(cmd->hint_root);
	disk_super->discard_root = cpu_to_le64(cmd->discard_root);
	disk_super->discard_block_size = cpu_to_le64(cmd->discard_block_size);
348
	disk_super->discard_nr_blocks = cpu_to_le64(from_dblock(cmd->discard_nr_blocks));
349
	disk_super->metadata_block_size = cpu_to_le32(DM_CACHE_METADATA_BLOCK_SIZE);
J
Joe Thornber 已提交
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
	disk_super->data_block_size = cpu_to_le32(cmd->data_block_size);
	disk_super->cache_blocks = cpu_to_le32(0);

	disk_super->read_hits = cpu_to_le32(0);
	disk_super->read_misses = cpu_to_le32(0);
	disk_super->write_hits = cpu_to_le32(0);
	disk_super->write_misses = cpu_to_le32(0);

	return dm_tm_commit(cmd->tm, sblock);
}

static int __format_metadata(struct dm_cache_metadata *cmd)
{
	int r;

	r = dm_tm_create_with_sm(cmd->bm, CACHE_SUPERBLOCK_LOCATION,
				 &cmd->tm, &cmd->metadata_sm);
	if (r < 0) {
		DMERR("tm_create_with_sm failed");
		return r;
	}

	__setup_mapping_info(cmd);

	r = dm_array_empty(&cmd->info, &cmd->root);
	if (r < 0)
		goto bad;

	dm_disk_bitset_init(cmd->tm, &cmd->discard_info);

	r = dm_bitset_empty(&cmd->discard_info, &cmd->discard_root);
	if (r < 0)
		goto bad;

	cmd->discard_block_size = 0;
	cmd->discard_nr_blocks = 0;

	r = __write_initial_superblock(cmd);
	if (r)
		goto bad;

	cmd->clean_when_opened = true;
	return 0;

bad:
	dm_tm_destroy(cmd->tm);
	dm_sm_destroy(cmd->metadata_sm);

	return r;
}

static int __check_incompat_features(struct cache_disk_superblock *disk_super,
				     struct dm_cache_metadata *cmd)
{
	uint32_t features;

	features = le32_to_cpu(disk_super->incompat_flags) & ~DM_CACHE_FEATURE_INCOMPAT_SUPP;
	if (features) {
		DMERR("could not access metadata due to unsupported optional features (%lx).",
		      (unsigned long)features);
		return -EINVAL;
	}

	/*
	 * Check for read-only metadata to skip the following RDWR checks.
	 */
	if (get_disk_ro(cmd->bdev->bd_disk))
		return 0;

	features = le32_to_cpu(disk_super->compat_ro_flags) & ~DM_CACHE_FEATURE_COMPAT_RO_SUPP;
	if (features) {
		DMERR("could not access metadata RDWR due to unsupported optional features (%lx).",
		      (unsigned long)features);
		return -EINVAL;
	}

	return 0;
}

static int __open_metadata(struct dm_cache_metadata *cmd)
{
	int r;
	struct dm_block *sblock;
	struct cache_disk_superblock *disk_super;
	unsigned long sb_flags;

	r = superblock_read_lock(cmd, &sblock);
	if (r < 0) {
		DMERR("couldn't read lock superblock");
		return r;
	}

	disk_super = dm_block_data(sblock);

444 445 446 447 448 449 450 451 452
	/* Verify the data block size hasn't changed */
	if (le32_to_cpu(disk_super->data_block_size) != cmd->data_block_size) {
		DMERR("changing the data block size (from %u to %llu) is not supported",
		      le32_to_cpu(disk_super->data_block_size),
		      (unsigned long long)cmd->data_block_size);
		r = -EINVAL;
		goto bad;
	}

J
Joe Thornber 已提交
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
	r = __check_incompat_features(disk_super, cmd);
	if (r < 0)
		goto bad;

	r = dm_tm_open_with_sm(cmd->bm, CACHE_SUPERBLOCK_LOCATION,
			       disk_super->metadata_space_map_root,
			       sizeof(disk_super->metadata_space_map_root),
			       &cmd->tm, &cmd->metadata_sm);
	if (r < 0) {
		DMERR("tm_open_with_sm failed");
		goto bad;
	}

	__setup_mapping_info(cmd);
	dm_disk_bitset_init(cmd->tm, &cmd->discard_info);
	sb_flags = le32_to_cpu(disk_super->flags);
	cmd->clean_when_opened = test_bit(CLEAN_SHUTDOWN, &sb_flags);
470 471 472
	dm_bm_unlock(sblock);

	return 0;
J
Joe Thornber 已提交
473 474 475 476 477 478 479 480 481

bad:
	dm_bm_unlock(sblock);
	return r;
}

static int __open_or_format_metadata(struct dm_cache_metadata *cmd,
				     bool format_device)
{
482 483
	int r;
	bool unformatted = false;
J
Joe Thornber 已提交
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498

	r = __superblock_all_zeroes(cmd->bm, &unformatted);
	if (r)
		return r;

	if (unformatted)
		return format_device ? __format_metadata(cmd) : -EPERM;

	return __open_metadata(cmd);
}

static int __create_persistent_data_objects(struct dm_cache_metadata *cmd,
					    bool may_format_device)
{
	int r;
499
	cmd->bm = dm_block_manager_create(cmd->bdev, DM_CACHE_METADATA_BLOCK_SIZE << SECTOR_SHIFT,
J
Joe Thornber 已提交
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
					  CACHE_METADATA_CACHE_SIZE,
					  CACHE_MAX_CONCURRENT_LOCKS);
	if (IS_ERR(cmd->bm)) {
		DMERR("could not create block manager");
		return PTR_ERR(cmd->bm);
	}

	r = __open_or_format_metadata(cmd, may_format_device);
	if (r)
		dm_block_manager_destroy(cmd->bm);

	return r;
}

static void __destroy_persistent_data_objects(struct dm_cache_metadata *cmd)
{
	dm_sm_destroy(cmd->metadata_sm);
	dm_tm_destroy(cmd->tm);
	dm_block_manager_destroy(cmd->bm);
}

typedef unsigned long (*flags_mutator)(unsigned long);

static void update_flags(struct cache_disk_superblock *disk_super,
			 flags_mutator mutator)
{
	uint32_t sb_flags = mutator(le32_to_cpu(disk_super->flags));
	disk_super->flags = cpu_to_le32(sb_flags);
}

static unsigned long set_clean_shutdown(unsigned long flags)
{
	set_bit(CLEAN_SHUTDOWN, &flags);
	return flags;
}

static unsigned long clear_clean_shutdown(unsigned long flags)
{
	clear_bit(CLEAN_SHUTDOWN, &flags);
	return flags;
}

static void read_superblock_fields(struct dm_cache_metadata *cmd,
				   struct cache_disk_superblock *disk_super)
{
545
	cmd->flags = le32_to_cpu(disk_super->flags);
J
Joe Thornber 已提交
546 547 548 549
	cmd->root = le64_to_cpu(disk_super->mapping_root);
	cmd->hint_root = le64_to_cpu(disk_super->hint_root);
	cmd->discard_root = le64_to_cpu(disk_super->discard_root);
	cmd->discard_block_size = le64_to_cpu(disk_super->discard_block_size);
550
	cmd->discard_nr_blocks = to_dblock(le64_to_cpu(disk_super->discard_nr_blocks));
J
Joe Thornber 已提交
551 552 553
	cmd->data_block_size = le32_to_cpu(disk_super->data_block_size);
	cmd->cache_blocks = to_cblock(le32_to_cpu(disk_super->cache_blocks));
	strncpy(cmd->policy_name, disk_super->policy_name, sizeof(cmd->policy_name));
554 555 556
	cmd->policy_version[0] = le32_to_cpu(disk_super->policy_version[0]);
	cmd->policy_version[1] = le32_to_cpu(disk_super->policy_version[1]);
	cmd->policy_version[2] = le32_to_cpu(disk_super->policy_version[2]);
J
Joe Thornber 已提交
557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
	cmd->policy_hint_size = le32_to_cpu(disk_super->policy_hint_size);

	cmd->stats.read_hits = le32_to_cpu(disk_super->read_hits);
	cmd->stats.read_misses = le32_to_cpu(disk_super->read_misses);
	cmd->stats.write_hits = le32_to_cpu(disk_super->write_hits);
	cmd->stats.write_misses = le32_to_cpu(disk_super->write_misses);

	cmd->changed = false;
}

/*
 * The mutator updates the superblock flags.
 */
static int __begin_transaction_flags(struct dm_cache_metadata *cmd,
				     flags_mutator mutator)
{
	int r;
	struct cache_disk_superblock *disk_super;
	struct dm_block *sblock;

	r = superblock_lock(cmd, &sblock);
	if (r)
		return r;

	disk_super = dm_block_data(sblock);
	update_flags(disk_super, mutator);
	read_superblock_fields(cmd, disk_super);
584
	dm_bm_unlock(sblock);
J
Joe Thornber 已提交
585

586
	return dm_bm_flush(cmd->bm);
J
Joe Thornber 已提交
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
}

static int __begin_transaction(struct dm_cache_metadata *cmd)
{
	int r;
	struct cache_disk_superblock *disk_super;
	struct dm_block *sblock;

	/*
	 * We re-read the superblock every time.  Shouldn't need to do this
	 * really.
	 */
	r = superblock_read_lock(cmd, &sblock);
	if (r)
		return r;

	disk_super = dm_block_data(sblock);
	read_superblock_fields(cmd, disk_super);
	dm_bm_unlock(sblock);

	return 0;
}

static int __commit_transaction(struct dm_cache_metadata *cmd,
				flags_mutator mutator)
{
	int r;
	struct cache_disk_superblock *disk_super;
	struct dm_block *sblock;

	/*
	 * We need to know if the cache_disk_superblock exceeds a 512-byte sector.
	 */
	BUILD_BUG_ON(sizeof(struct cache_disk_superblock) > 512);

	r = dm_bitset_flush(&cmd->discard_info, cmd->discard_root,
			    &cmd->discard_root);
	if (r)
		return r;

	r = dm_tm_pre_commit(cmd->tm);
	if (r < 0)
		return r;

631 632
	r = __save_sm_root(cmd);
	if (r)
J
Joe Thornber 已提交
633 634 635 636 637 638 639 640
		return r;

	r = superblock_lock(cmd, &sblock);
	if (r)
		return r;

	disk_super = dm_block_data(sblock);

641
	disk_super->flags = cpu_to_le32(cmd->flags);
J
Joe Thornber 已提交
642 643 644 645 646 647 648
	if (mutator)
		update_flags(disk_super, mutator);

	disk_super->mapping_root = cpu_to_le64(cmd->root);
	disk_super->hint_root = cpu_to_le64(cmd->hint_root);
	disk_super->discard_root = cpu_to_le64(cmd->discard_root);
	disk_super->discard_block_size = cpu_to_le64(cmd->discard_block_size);
649
	disk_super->discard_nr_blocks = cpu_to_le64(from_dblock(cmd->discard_nr_blocks));
J
Joe Thornber 已提交
650 651
	disk_super->cache_blocks = cpu_to_le32(from_cblock(cmd->cache_blocks));
	strncpy(disk_super->policy_name, cmd->policy_name, sizeof(disk_super->policy_name));
652 653 654
	disk_super->policy_version[0] = cpu_to_le32(cmd->policy_version[0]);
	disk_super->policy_version[1] = cpu_to_le32(cmd->policy_version[1]);
	disk_super->policy_version[2] = cpu_to_le32(cmd->policy_version[2]);
J
Joe Thornber 已提交
655 656 657 658 659

	disk_super->read_hits = cpu_to_le32(cmd->stats.read_hits);
	disk_super->read_misses = cpu_to_le32(cmd->stats.read_misses);
	disk_super->write_hits = cpu_to_le32(cmd->stats.write_hits);
	disk_super->write_misses = cpu_to_le32(cmd->stats.write_misses);
660
	__copy_sm_root(cmd, disk_super);
J
Joe Thornber 已提交
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691

	return dm_tm_commit(cmd->tm, sblock);
}

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

/*
 * The mappings are held in a dm-array that has 64-bit values stored in
 * little-endian format.  The index is the cblock, the high 48bits of the
 * value are the oblock and the low 16 bit the flags.
 */
#define FLAGS_MASK ((1 << 16) - 1)

static __le64 pack_value(dm_oblock_t block, unsigned flags)
{
	uint64_t value = from_oblock(block);
	value <<= 16;
	value = value | (flags & FLAGS_MASK);
	return cpu_to_le64(value);
}

static void unpack_value(__le64 value_le, dm_oblock_t *block, unsigned *flags)
{
	uint64_t value = le64_to_cpu(value_le);
	uint64_t b = value >> 16;
	*block = to_oblock(b);
	*flags = value & FLAGS_MASK;
}

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

692 693 694 695
static struct dm_cache_metadata *metadata_open(struct block_device *bdev,
					       sector_t data_block_size,
					       bool may_format_device,
					       size_t policy_hint_size)
J
Joe Thornber 已提交
696 697 698 699 700 701 702
{
	int r;
	struct dm_cache_metadata *cmd;

	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
	if (!cmd) {
		DMERR("could not allocate metadata struct");
703
		return ERR_PTR(-ENOMEM);
J
Joe Thornber 已提交
704 705
	}

706
	atomic_set(&cmd->ref_count, 1);
J
Joe Thornber 已提交
707 708 709 710 711 712
	init_rwsem(&cmd->root_lock);
	cmd->bdev = bdev;
	cmd->data_block_size = data_block_size;
	cmd->cache_blocks = 0;
	cmd->policy_hint_size = policy_hint_size;
	cmd->changed = true;
713
	cmd->fail_io = false;
J
Joe Thornber 已提交
714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729

	r = __create_persistent_data_objects(cmd, may_format_device);
	if (r) {
		kfree(cmd);
		return ERR_PTR(r);
	}

	r = __begin_transaction_flags(cmd, clear_clean_shutdown);
	if (r < 0) {
		dm_cache_metadata_close(cmd);
		return ERR_PTR(r);
	}

	return cmd;
}

730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765
/*
 * We keep a little list of ref counted metadata objects to prevent two
 * different target instances creating separate bufio instances.  This is
 * an issue if a table is reloaded before the suspend.
 */
static DEFINE_MUTEX(table_lock);
static LIST_HEAD(table);

static struct dm_cache_metadata *lookup(struct block_device *bdev)
{
	struct dm_cache_metadata *cmd;

	list_for_each_entry(cmd, &table, list)
		if (cmd->bdev == bdev) {
			atomic_inc(&cmd->ref_count);
			return cmd;
		}

	return NULL;
}

static struct dm_cache_metadata *lookup_or_open(struct block_device *bdev,
						sector_t data_block_size,
						bool may_format_device,
						size_t policy_hint_size)
{
	struct dm_cache_metadata *cmd, *cmd2;

	mutex_lock(&table_lock);
	cmd = lookup(bdev);
	mutex_unlock(&table_lock);

	if (cmd)
		return cmd;

	cmd = metadata_open(bdev, data_block_size, may_format_device, policy_hint_size);
766
	if (!IS_ERR(cmd)) {
767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800
		mutex_lock(&table_lock);
		cmd2 = lookup(bdev);
		if (cmd2) {
			mutex_unlock(&table_lock);
			__destroy_persistent_data_objects(cmd);
			kfree(cmd);
			return cmd2;
		}
		list_add(&cmd->list, &table);
		mutex_unlock(&table_lock);
	}

	return cmd;
}

static bool same_params(struct dm_cache_metadata *cmd, sector_t data_block_size)
{
	if (cmd->data_block_size != data_block_size) {
		DMERR("data_block_size (%llu) different from that in metadata (%llu)\n",
		      (unsigned long long) data_block_size,
		      (unsigned long long) cmd->data_block_size);
		return false;
	}

	return true;
}

struct dm_cache_metadata *dm_cache_metadata_open(struct block_device *bdev,
						 sector_t data_block_size,
						 bool may_format_device,
						 size_t policy_hint_size)
{
	struct dm_cache_metadata *cmd = lookup_or_open(bdev, data_block_size,
						       may_format_device, policy_hint_size);
801 802

	if (!IS_ERR(cmd) && !same_params(cmd, data_block_size)) {
803
		dm_cache_metadata_close(cmd);
804
		return ERR_PTR(-EINVAL);
805 806 807 808 809
	}

	return cmd;
}

J
Joe Thornber 已提交
810 811
void dm_cache_metadata_close(struct dm_cache_metadata *cmd)
{
812 813 814 815 816
	if (atomic_dec_and_test(&cmd->ref_count)) {
		mutex_lock(&table_lock);
		list_del(&cmd->list);
		mutex_unlock(&table_lock);

817 818
		if (!cmd->fail_io)
			__destroy_persistent_data_objects(cmd);
819 820
		kfree(cmd);
	}
J
Joe Thornber 已提交
821 822
}

J
Joe Thornber 已提交
823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869
/*
 * Checks that the given cache block is either unmapped or clean.
 */
static int block_unmapped_or_clean(struct dm_cache_metadata *cmd, dm_cblock_t b,
				   bool *result)
{
	int r;
	__le64 value;
	dm_oblock_t ob;
	unsigned flags;

	r = dm_array_get_value(&cmd->info, cmd->root, from_cblock(b), &value);
	if (r) {
		DMERR("block_unmapped_or_clean failed");
		return r;
	}

	unpack_value(value, &ob, &flags);
	*result = !((flags & M_VALID) && (flags & M_DIRTY));

	return 0;
}

static int blocks_are_unmapped_or_clean(struct dm_cache_metadata *cmd,
					dm_cblock_t begin, dm_cblock_t end,
					bool *result)
{
	int r;
	*result = true;

	while (begin != end) {
		r = block_unmapped_or_clean(cmd, begin, result);
		if (r)
			return r;

		if (!*result) {
			DMERR("cache block %llu is dirty",
			      (unsigned long long) from_cblock(begin));
			return 0;
		}

		begin = to_cblock(from_cblock(begin) + 1);
	}

	return 0;
}

870 871 872 873
#define WRITE_LOCK(cmd)	\
	down_write(&cmd->root_lock); \
	if (cmd->fail_io || dm_bm_is_read_only(cmd->bm)) { \
		up_write(&cmd->root_lock); \
874
		return -EINVAL; \
875
	}
876 877

#define WRITE_LOCK_VOID(cmd) \
878 879 880
	down_write(&cmd->root_lock); \
	if (cmd->fail_io || dm_bm_is_read_only(cmd->bm)) { \
		up_write(&cmd->root_lock); \
881
		return; \
882
	}
883 884 885 886

#define WRITE_UNLOCK(cmd) \
	up_write(&cmd->root_lock)

887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903
#define READ_LOCK(cmd) \
	down_read(&cmd->root_lock); \
	if (cmd->fail_io || dm_bm_is_read_only(cmd->bm)) { \
		up_read(&cmd->root_lock); \
		return -EINVAL; \
	}

#define READ_LOCK_VOID(cmd)	\
	down_read(&cmd->root_lock); \
	if (cmd->fail_io || dm_bm_is_read_only(cmd->bm)) { \
		up_read(&cmd->root_lock); \
		return; \
	}

#define READ_UNLOCK(cmd) \
	up_read(&cmd->root_lock)

J
Joe Thornber 已提交
904 905 906
int dm_cache_resize(struct dm_cache_metadata *cmd, dm_cblock_t new_cache_size)
{
	int r;
J
Joe Thornber 已提交
907
	bool clean;
J
Joe Thornber 已提交
908 909
	__le64 null_mapping = pack_value(0, 0);

910
	WRITE_LOCK(cmd);
J
Joe Thornber 已提交
911
	__dm_bless_for_disk(&null_mapping);
J
Joe Thornber 已提交
912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927

	if (from_cblock(new_cache_size) < from_cblock(cmd->cache_blocks)) {
		r = blocks_are_unmapped_or_clean(cmd, new_cache_size, cmd->cache_blocks, &clean);
		if (r) {
			__dm_unbless_for_disk(&null_mapping);
			goto out;
		}

		if (!clean) {
			DMERR("unable to shrink cache due to dirty blocks");
			r = -EINVAL;
			__dm_unbless_for_disk(&null_mapping);
			goto out;
		}
	}

J
Joe Thornber 已提交
928 929 930 931 932 933
	r = dm_array_resize(&cmd->info, cmd->root, from_cblock(cmd->cache_blocks),
			    from_cblock(new_cache_size),
			    &null_mapping, &cmd->root);
	if (!r)
		cmd->cache_blocks = new_cache_size;
	cmd->changed = true;
J
Joe Thornber 已提交
934 935

out:
936
	WRITE_UNLOCK(cmd);
J
Joe Thornber 已提交
937 938 939 940 941 942

	return r;
}

int dm_cache_discard_bitset_resize(struct dm_cache_metadata *cmd,
				   sector_t discard_block_size,
943
				   dm_dblock_t new_nr_entries)
J
Joe Thornber 已提交
944 945 946
{
	int r;

947
	WRITE_LOCK(cmd);
J
Joe Thornber 已提交
948 949
	r = dm_bitset_resize(&cmd->discard_info,
			     cmd->discard_root,
950 951
			     from_dblock(cmd->discard_nr_blocks),
			     from_dblock(new_nr_entries),
J
Joe Thornber 已提交
952 953 954 955 956 957 958
			     false, &cmd->discard_root);
	if (!r) {
		cmd->discard_block_size = discard_block_size;
		cmd->discard_nr_blocks = new_nr_entries;
	}

	cmd->changed = true;
959
	WRITE_UNLOCK(cmd);
J
Joe Thornber 已提交
960 961 962 963

	return r;
}

964
static int __set_discard(struct dm_cache_metadata *cmd, dm_dblock_t b)
J
Joe Thornber 已提交
965 966
{
	return dm_bitset_set_bit(&cmd->discard_info, cmd->discard_root,
967
				 from_dblock(b), &cmd->discard_root);
J
Joe Thornber 已提交
968 969
}

970
static int __clear_discard(struct dm_cache_metadata *cmd, dm_dblock_t b)
J
Joe Thornber 已提交
971 972
{
	return dm_bitset_clear_bit(&cmd->discard_info, cmd->discard_root,
973
				   from_dblock(b), &cmd->discard_root);
J
Joe Thornber 已提交
974 975
}

976
static int __is_discarded(struct dm_cache_metadata *cmd, dm_dblock_t b,
J
Joe Thornber 已提交
977 978 979
			  bool *is_discarded)
{
	return dm_bitset_test_bit(&cmd->discard_info, cmd->discard_root,
980
				  from_dblock(b), &cmd->discard_root,
J
Joe Thornber 已提交
981 982 983 984
				  is_discarded);
}

static int __discard(struct dm_cache_metadata *cmd,
985
		     dm_dblock_t dblock, bool discard)
J
Joe Thornber 已提交
986 987 988 989 990 991 992 993 994 995 996 997
{
	int r;

	r = (discard ? __set_discard : __clear_discard)(cmd, dblock);
	if (r)
		return r;

	cmd->changed = true;
	return 0;
}

int dm_cache_set_discard(struct dm_cache_metadata *cmd,
998
			 dm_dblock_t dblock, bool discard)
J
Joe Thornber 已提交
999 1000 1001
{
	int r;

1002
	WRITE_LOCK(cmd);
J
Joe Thornber 已提交
1003
	r = __discard(cmd, dblock, discard);
1004
	WRITE_UNLOCK(cmd);
J
Joe Thornber 已提交
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015

	return r;
}

static int __load_discards(struct dm_cache_metadata *cmd,
			   load_discard_fn fn, void *context)
{
	int r = 0;
	dm_block_t b;
	bool discard;

1016 1017
	for (b = 0; b < from_dblock(cmd->discard_nr_blocks); b++) {
		dm_dblock_t dblock = to_dblock(b);
J
Joe Thornber 已提交
1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038

		if (cmd->clean_when_opened) {
			r = __is_discarded(cmd, dblock, &discard);
			if (r)
				return r;
		} else
			discard = false;

		r = fn(context, cmd->discard_block_size, dblock, discard);
		if (r)
			break;
	}

	return r;
}

int dm_cache_load_discards(struct dm_cache_metadata *cmd,
			   load_discard_fn fn, void *context)
{
	int r;

1039
	READ_LOCK(cmd);
J
Joe Thornber 已提交
1040
	r = __load_discards(cmd, fn, context);
1041
	READ_UNLOCK(cmd);
J
Joe Thornber 已提交
1042 1043 1044 1045

	return r;
}

1046
int dm_cache_size(struct dm_cache_metadata *cmd, dm_cblock_t *result)
J
Joe Thornber 已提交
1047
{
1048 1049 1050
	READ_LOCK(cmd);
	*result = cmd->cache_blocks;
	READ_UNLOCK(cmd);
J
Joe Thornber 已提交
1051

1052
	return 0;
J
Joe Thornber 已提交
1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
}

static int __remove(struct dm_cache_metadata *cmd, dm_cblock_t cblock)
{
	int r;
	__le64 value = pack_value(0, 0);

	__dm_bless_for_disk(&value);
	r = dm_array_set_value(&cmd->info, cmd->root, from_cblock(cblock),
			       &value, &cmd->root);
	if (r)
		return r;

	cmd->changed = true;
	return 0;
}

int dm_cache_remove_mapping(struct dm_cache_metadata *cmd, dm_cblock_t cblock)
{
	int r;

1074
	WRITE_LOCK(cmd);
J
Joe Thornber 已提交
1075
	r = __remove(cmd, cblock);
1076
	WRITE_UNLOCK(cmd);
J
Joe Thornber 已提交
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101

	return r;
}

static int __insert(struct dm_cache_metadata *cmd,
		    dm_cblock_t cblock, dm_oblock_t oblock)
{
	int r;
	__le64 value = pack_value(oblock, M_VALID);
	__dm_bless_for_disk(&value);

	r = dm_array_set_value(&cmd->info, cmd->root, from_cblock(cblock),
			       &value, &cmd->root);
	if (r)
		return r;

	cmd->changed = true;
	return 0;
}

int dm_cache_insert_mapping(struct dm_cache_metadata *cmd,
			    dm_cblock_t cblock, dm_oblock_t oblock)
{
	int r;

1102
	WRITE_LOCK(cmd);
J
Joe Thornber 已提交
1103
	r = __insert(cmd, cblock, oblock);
1104
	WRITE_UNLOCK(cmd);
J
Joe Thornber 已提交
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117

	return r;
}

struct thunk {
	load_mapping_fn fn;
	void *context;

	struct dm_cache_metadata *cmd;
	bool respect_dirty_flags;
	bool hints_valid;
};

1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145
static bool policy_unchanged(struct dm_cache_metadata *cmd,
			     struct dm_cache_policy *policy)
{
	const char *policy_name = dm_cache_policy_get_name(policy);
	const unsigned *policy_version = dm_cache_policy_get_version(policy);
	size_t policy_hint_size = dm_cache_policy_get_hint_size(policy);

	/*
	 * Ensure policy names match.
	 */
	if (strncmp(cmd->policy_name, policy_name, sizeof(cmd->policy_name)))
		return false;

	/*
	 * Ensure policy major versions match.
	 */
	if (cmd->policy_version[0] != policy_version[0])
		return false;

	/*
	 * Ensure policy hint sizes match.
	 */
	if (cmd->policy_hint_size != policy_hint_size)
		return false;

	return true;
}

J
Joe Thornber 已提交
1146 1147 1148 1149 1150 1151
static bool hints_array_initialized(struct dm_cache_metadata *cmd)
{
	return cmd->hint_root && cmd->policy_hint_size;
}

static bool hints_array_available(struct dm_cache_metadata *cmd,
1152
				  struct dm_cache_policy *policy)
J
Joe Thornber 已提交
1153
{
1154
	return cmd->clean_when_opened && policy_unchanged(cmd, policy) &&
J
Joe Thornber 已提交
1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187
		hints_array_initialized(cmd);
}

static int __load_mapping(void *context, uint64_t cblock, void *leaf)
{
	int r = 0;
	bool dirty;
	__le64 value;
	__le32 hint_value = 0;
	dm_oblock_t oblock;
	unsigned flags;
	struct thunk *thunk = context;
	struct dm_cache_metadata *cmd = thunk->cmd;

	memcpy(&value, leaf, sizeof(value));
	unpack_value(value, &oblock, &flags);

	if (flags & M_VALID) {
		if (thunk->hints_valid) {
			r = dm_array_get_value(&cmd->hint_info, cmd->hint_root,
					       cblock, &hint_value);
			if (r && r != -ENODATA)
				return r;
		}

		dirty = thunk->respect_dirty_flags ? (flags & M_DIRTY) : true;
		r = thunk->fn(thunk->context, oblock, to_cblock(cblock),
			      dirty, le32_to_cpu(hint_value), thunk->hints_valid);
	}

	return r;
}

1188 1189
static int __load_mappings(struct dm_cache_metadata *cmd,
			   struct dm_cache_policy *policy,
J
Joe Thornber 已提交
1190 1191 1192 1193 1194 1195 1196 1197 1198
			   load_mapping_fn fn, void *context)
{
	struct thunk thunk;

	thunk.fn = fn;
	thunk.context = context;

	thunk.cmd = cmd;
	thunk.respect_dirty_flags = cmd->clean_when_opened;
1199
	thunk.hints_valid = hints_array_available(cmd, policy);
J
Joe Thornber 已提交
1200 1201 1202 1203

	return dm_array_walk(&cmd->info, cmd->root, __load_mapping, &thunk);
}

1204 1205
int dm_cache_load_mappings(struct dm_cache_metadata *cmd,
			   struct dm_cache_policy *policy,
J
Joe Thornber 已提交
1206 1207 1208 1209
			   load_mapping_fn fn, void *context)
{
	int r;

1210
	READ_LOCK(cmd);
1211
	r = __load_mappings(cmd, policy, fn, context);
1212
	READ_UNLOCK(cmd);
J
Joe Thornber 已提交
1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236

	return r;
}

static int __dump_mapping(void *context, uint64_t cblock, void *leaf)
{
	int r = 0;
	__le64 value;
	dm_oblock_t oblock;
	unsigned flags;

	memcpy(&value, leaf, sizeof(value));
	unpack_value(value, &oblock, &flags);

	return r;
}

static int __dump_mappings(struct dm_cache_metadata *cmd)
{
	return dm_array_walk(&cmd->info, cmd->root, __dump_mapping, NULL);
}

void dm_cache_dump(struct dm_cache_metadata *cmd)
{
1237
	READ_LOCK_VOID(cmd);
J
Joe Thornber 已提交
1238
	__dump_mappings(cmd);
1239
	READ_UNLOCK(cmd);
J
Joe Thornber 已提交
1240 1241 1242 1243 1244 1245
}

int dm_cache_changed_this_transaction(struct dm_cache_metadata *cmd)
{
	int r;

1246
	READ_LOCK(cmd);
J
Joe Thornber 已提交
1247
	r = cmd->changed;
1248
	READ_UNLOCK(cmd);
J
Joe Thornber 已提交
1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269

	return r;
}

static int __dirty(struct dm_cache_metadata *cmd, dm_cblock_t cblock, bool dirty)
{
	int r;
	unsigned flags;
	dm_oblock_t oblock;
	__le64 value;

	r = dm_array_get_value(&cmd->info, cmd->root, from_cblock(cblock), &value);
	if (r)
		return r;

	unpack_value(value, &oblock, &flags);

	if (((flags & M_DIRTY) && dirty) || (!(flags & M_DIRTY) && !dirty))
		/* nothing to be done */
		return 0;

1270
	value = pack_value(oblock, (flags & ~M_DIRTY) | (dirty ? M_DIRTY : 0));
J
Joe Thornber 已提交
1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287
	__dm_bless_for_disk(&value);

	r = dm_array_set_value(&cmd->info, cmd->root, from_cblock(cblock),
			       &value, &cmd->root);
	if (r)
		return r;

	cmd->changed = true;
	return 0;

}

int dm_cache_set_dirty(struct dm_cache_metadata *cmd,
		       dm_cblock_t cblock, bool dirty)
{
	int r;

1288
	WRITE_LOCK(cmd);
J
Joe Thornber 已提交
1289
	r = __dirty(cmd, cblock, dirty);
1290
	WRITE_UNLOCK(cmd);
J
Joe Thornber 已提交
1291 1292 1293 1294 1295 1296 1297

	return r;
}

void dm_cache_metadata_get_stats(struct dm_cache_metadata *cmd,
				 struct dm_cache_statistics *stats)
{
1298
	READ_LOCK_VOID(cmd);
1299
	*stats = cmd->stats;
1300
	READ_UNLOCK(cmd);
J
Joe Thornber 已提交
1301 1302 1303 1304 1305
}

void dm_cache_metadata_set_stats(struct dm_cache_metadata *cmd,
				 struct dm_cache_statistics *stats)
{
1306
	WRITE_LOCK_VOID(cmd);
1307
	cmd->stats = *stats;
1308
	WRITE_UNLOCK(cmd);
J
Joe Thornber 已提交
1309 1310 1311 1312 1313 1314 1315 1316
}

int dm_cache_commit(struct dm_cache_metadata *cmd, bool clean_shutdown)
{
	int r;
	flags_mutator mutator = (clean_shutdown ? set_clean_shutdown :
				 clear_clean_shutdown);

1317
	WRITE_LOCK(cmd);
J
Joe Thornber 已提交
1318 1319 1320 1321 1322 1323 1324
	r = __commit_transaction(cmd, mutator);
	if (r)
		goto out;

	r = __begin_transaction(cmd);

out:
1325
	WRITE_UNLOCK(cmd);
J
Joe Thornber 已提交
1326 1327 1328 1329 1330 1331 1332 1333
	return r;
}

int dm_cache_get_free_metadata_block_count(struct dm_cache_metadata *cmd,
					   dm_block_t *result)
{
	int r = -EINVAL;

1334
	READ_LOCK(cmd);
J
Joe Thornber 已提交
1335
	r = dm_sm_get_nr_free(cmd->metadata_sm, result);
1336
	READ_UNLOCK(cmd);
J
Joe Thornber 已提交
1337 1338 1339 1340 1341 1342 1343 1344 1345

	return r;
}

int dm_cache_get_metadata_dev_size(struct dm_cache_metadata *cmd,
				   dm_block_t *result)
{
	int r = -EINVAL;

1346
	READ_LOCK(cmd);
J
Joe Thornber 已提交
1347
	r = dm_sm_get_nr_blocks(cmd->metadata_sm, result);
1348
	READ_UNLOCK(cmd);
J
Joe Thornber 已提交
1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360

	return r;
}

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

static int begin_hints(struct dm_cache_metadata *cmd, struct dm_cache_policy *policy)
{
	int r;
	__le32 value;
	size_t hint_size;
	const char *policy_name = dm_cache_policy_get_name(policy);
1361
	const unsigned *policy_version = dm_cache_policy_get_version(policy);
J
Joe Thornber 已提交
1362 1363 1364 1365 1366

	if (!policy_name[0] ||
	    (strlen(policy_name) > sizeof(cmd->policy_name) - 1))
		return -EINVAL;

1367
	if (!policy_unchanged(cmd, policy)) {
J
Joe Thornber 已提交
1368
		strncpy(cmd->policy_name, policy_name, sizeof(cmd->policy_name));
1369
		memcpy(cmd->policy_version, policy_version, sizeof(cmd->policy_version));
J
Joe Thornber 已提交
1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397

		hint_size = dm_cache_policy_get_hint_size(policy);
		if (!hint_size)
			return 0; /* short-circuit hints initialization */
		cmd->policy_hint_size = hint_size;

		if (cmd->hint_root) {
			r = dm_array_del(&cmd->hint_info, cmd->hint_root);
			if (r)
				return r;
		}

		r = dm_array_empty(&cmd->hint_info, &cmd->hint_root);
		if (r)
			return r;

		value = cpu_to_le32(0);
		__dm_bless_for_disk(&value);
		r = dm_array_resize(&cmd->hint_info, cmd->hint_root, 0,
				    from_cblock(cmd->cache_blocks),
				    &value, &cmd->hint_root);
		if (r)
			return r;
	}

	return 0;
}

J
Joe Thornber 已提交
1398
static int save_hint(void *context, dm_cblock_t cblock, dm_oblock_t oblock, uint32_t hint)
J
Joe Thornber 已提交
1399
{
J
Joe Thornber 已提交
1400 1401
	struct dm_cache_metadata *cmd = context;
	__le32 value = cpu_to_le32(hint);
J
Joe Thornber 已提交
1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412
	int r;

	__dm_bless_for_disk(&value);

	r = dm_array_set_value(&cmd->hint_info, cmd->hint_root,
			       from_cblock(cblock), &value, &cmd->hint_root);
	cmd->changed = true;

	return r;
}

J
Joe Thornber 已提交
1413
static int write_hints(struct dm_cache_metadata *cmd, struct dm_cache_policy *policy)
J
Joe Thornber 已提交
1414 1415 1416
{
	int r;

J
Joe Thornber 已提交
1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428
	r = begin_hints(cmd, policy);
	if (r) {
		DMERR("begin_hints failed");
		return r;
	}

	return policy_walk_mappings(policy, save_hint, cmd);
}

int dm_cache_write_hints(struct dm_cache_metadata *cmd, struct dm_cache_policy *policy)
{
	int r;
J
Joe Thornber 已提交
1429

1430
	WRITE_LOCK(cmd);
J
Joe Thornber 已提交
1431
	r = write_hints(cmd, policy);
1432
	WRITE_UNLOCK(cmd);
J
Joe Thornber 已提交
1433 1434 1435

	return r;
}
J
Joe Thornber 已提交
1436 1437 1438

int dm_cache_metadata_all_clean(struct dm_cache_metadata *cmd, bool *result)
{
1439 1440 1441 1442 1443 1444 1445
	int r;

	READ_LOCK(cmd);
	r = blocks_are_unmapped_or_clean(cmd, 0, cmd->cache_blocks, result);
	READ_UNLOCK(cmd);

	return r;
J
Joe Thornber 已提交
1446
}
1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467

void dm_cache_metadata_set_read_only(struct dm_cache_metadata *cmd)
{
	WRITE_LOCK_VOID(cmd);
	dm_bm_set_read_only(cmd->bm);
	WRITE_UNLOCK(cmd);
}

void dm_cache_metadata_set_read_write(struct dm_cache_metadata *cmd)
{
	WRITE_LOCK_VOID(cmd);
	dm_bm_set_read_write(cmd->bm);
	WRITE_UNLOCK(cmd);
}

int dm_cache_metadata_set_needs_check(struct dm_cache_metadata *cmd)
{
	int r;
	struct dm_block *sblock;
	struct cache_disk_superblock *disk_super;

1468
	WRITE_LOCK(cmd);
1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482
	set_bit(NEEDS_CHECK, &cmd->flags);

	r = superblock_lock(cmd, &sblock);
	if (r) {
		DMERR("couldn't read superblock");
		goto out;
	}

	disk_super = dm_block_data(sblock);
	disk_super->flags = cpu_to_le32(cmd->flags);

	dm_bm_unlock(sblock);

out:
1483
	WRITE_UNLOCK(cmd);
1484 1485 1486
	return r;
}

1487
int dm_cache_metadata_needs_check(struct dm_cache_metadata *cmd, bool *result)
1488
{
1489 1490 1491
	READ_LOCK(cmd);
	*result = !!test_bit(NEEDS_CHECK, &cmd->flags);
	READ_UNLOCK(cmd);
1492

1493
	return 0;
1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508
}

int dm_cache_metadata_abort(struct dm_cache_metadata *cmd)
{
	int r;

	WRITE_LOCK(cmd);
	__destroy_persistent_data_objects(cmd);
	r = __create_persistent_data_objects(cmd, false);
	if (r)
		cmd->fail_io = true;
	WRITE_UNLOCK(cmd);

	return r;
}