blob: 8601425436cdd2a695f10160bc9d581315aa49ea [file] [log] [blame]
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001/*
2 * Copyright (C) 2012 Red Hat, Inc.
3 *
4 * This file is released under the GPL.
5 */
6
7#include "dm-cache-metadata.h"
8
9#include "persistent-data/dm-array.h"
10#include "persistent-data/dm-bitset.h"
11#include "persistent-data/dm-space-map.h"
12#include "persistent-data/dm-space-map-disk.h"
13#include "persistent-data/dm-transaction-manager.h"
14
15#include <linux/device-mapper.h>
16
17/*----------------------------------------------------------------*/
18
19#define DM_MSG_PREFIX "cache metadata"
20
21#define CACHE_SUPERBLOCK_MAGIC 06142003
22#define CACHE_SUPERBLOCK_LOCATION 0
23#define CACHE_VERSION 1
24#define CACHE_METADATA_CACHE_SIZE 64
25
26/*
27 * 3 for btree insert +
28 * 2 for btree lookup used within space map
29 */
30#define CACHE_MAX_CONCURRENT_LOCKS 5
31#define SPACE_MAP_ROOT_SIZE 128
32
33enum superblock_flag_bits {
34 /* for spotting crashes that would invalidate the dirty bitset */
35 CLEAN_SHUTDOWN,
36};
37
38/*
39 * Each mapping from cache block -> origin block carries a set of flags.
40 */
41enum mapping_bits {
42 /*
43 * A valid mapping. Because we're using an array we clear this
44 * flag for an non existant mapping.
45 */
46 M_VALID = 1,
47
48 /*
49 * The data on the cache is different from that on the origin.
50 */
51 M_DIRTY = 2
52};
53
54struct cache_disk_superblock {
55 __le32 csum;
56 __le32 flags;
57 __le64 blocknr;
58
59 __u8 uuid[16];
60 __le64 magic;
61 __le32 version;
62
63 __u8 policy_name[CACHE_POLICY_NAME_SIZE];
64 __le32 policy_hint_size;
65
66 __u8 metadata_space_map_root[SPACE_MAP_ROOT_SIZE];
67 __le64 mapping_root;
68 __le64 hint_root;
69
70 __le64 discard_root;
71 __le64 discard_block_size;
72 __le64 discard_nr_blocks;
73
74 __le32 data_block_size;
75 __le32 metadata_block_size;
76 __le32 cache_blocks;
77
78 __le32 compat_flags;
79 __le32 compat_ro_flags;
80 __le32 incompat_flags;
81
82 __le32 read_hits;
83 __le32 read_misses;
84 __le32 write_hits;
85 __le32 write_misses;
Mike Snitzer4e7f5062013-03-20 17:21:27 +000086
87 __le32 policy_version[CACHE_POLICY_VERSION_SIZE];
Joe Thornberc6b4fcb2013-03-01 22:45:51 +000088} __packed;
89
90struct dm_cache_metadata {
91 struct block_device *bdev;
92 struct dm_block_manager *bm;
93 struct dm_space_map *metadata_sm;
94 struct dm_transaction_manager *tm;
95
96 struct dm_array_info info;
97 struct dm_array_info hint_info;
98 struct dm_disk_bitset discard_info;
99
100 struct rw_semaphore root_lock;
101 dm_block_t root;
102 dm_block_t hint_root;
103 dm_block_t discard_root;
104
105 sector_t discard_block_size;
106 dm_dblock_t discard_nr_blocks;
107
108 sector_t data_block_size;
109 dm_cblock_t cache_blocks;
110 bool changed:1;
111 bool clean_when_opened:1;
112
113 char policy_name[CACHE_POLICY_NAME_SIZE];
Mike Snitzer4e7f5062013-03-20 17:21:27 +0000114 unsigned policy_version[CACHE_POLICY_VERSION_SIZE];
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000115 size_t policy_hint_size;
116 struct dm_cache_statistics stats;
117};
118
119/*-------------------------------------------------------------------
120 * superblock validator
121 *-----------------------------------------------------------------*/
122
123#define SUPERBLOCK_CSUM_XOR 9031977
124
125static void sb_prepare_for_write(struct dm_block_validator *v,
126 struct dm_block *b,
127 size_t sb_block_size)
128{
129 struct cache_disk_superblock *disk_super = dm_block_data(b);
130
131 disk_super->blocknr = cpu_to_le64(dm_block_location(b));
132 disk_super->csum = cpu_to_le32(dm_bm_checksum(&disk_super->flags,
133 sb_block_size - sizeof(__le32),
134 SUPERBLOCK_CSUM_XOR));
135}
136
137static int sb_check(struct dm_block_validator *v,
138 struct dm_block *b,
139 size_t sb_block_size)
140{
141 struct cache_disk_superblock *disk_super = dm_block_data(b);
142 __le32 csum_le;
143
144 if (dm_block_location(b) != le64_to_cpu(disk_super->blocknr)) {
145 DMERR("sb_check failed: blocknr %llu: wanted %llu",
146 le64_to_cpu(disk_super->blocknr),
147 (unsigned long long)dm_block_location(b));
148 return -ENOTBLK;
149 }
150
151 if (le64_to_cpu(disk_super->magic) != CACHE_SUPERBLOCK_MAGIC) {
152 DMERR("sb_check failed: magic %llu: wanted %llu",
153 le64_to_cpu(disk_super->magic),
154 (unsigned long long)CACHE_SUPERBLOCK_MAGIC);
155 return -EILSEQ;
156 }
157
158 csum_le = cpu_to_le32(dm_bm_checksum(&disk_super->flags,
159 sb_block_size - sizeof(__le32),
160 SUPERBLOCK_CSUM_XOR));
161 if (csum_le != disk_super->csum) {
162 DMERR("sb_check failed: csum %u: wanted %u",
163 le32_to_cpu(csum_le), le32_to_cpu(disk_super->csum));
164 return -EILSEQ;
165 }
166
167 return 0;
168}
169
170static struct dm_block_validator sb_validator = {
171 .name = "superblock",
172 .prepare_for_write = sb_prepare_for_write,
173 .check = sb_check
174};
175
176/*----------------------------------------------------------------*/
177
178static int superblock_read_lock(struct dm_cache_metadata *cmd,
179 struct dm_block **sblock)
180{
181 return dm_bm_read_lock(cmd->bm, CACHE_SUPERBLOCK_LOCATION,
182 &sb_validator, sblock);
183}
184
185static int superblock_lock_zero(struct dm_cache_metadata *cmd,
186 struct dm_block **sblock)
187{
188 return dm_bm_write_lock_zero(cmd->bm, CACHE_SUPERBLOCK_LOCATION,
189 &sb_validator, sblock);
190}
191
192static int superblock_lock(struct dm_cache_metadata *cmd,
193 struct dm_block **sblock)
194{
195 return dm_bm_write_lock(cmd->bm, CACHE_SUPERBLOCK_LOCATION,
196 &sb_validator, sblock);
197}
198
199/*----------------------------------------------------------------*/
200
Joe Thornberdd8b0c22013-10-24 14:10:28 -0400201static int __superblock_all_zeroes(struct dm_block_manager *bm, bool *result)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000202{
203 int r;
204 unsigned i;
205 struct dm_block *b;
206 __le64 *data_le, zero = cpu_to_le64(0);
207 unsigned sb_block_size = dm_bm_block_size(bm) / sizeof(__le64);
208
209 /*
210 * We can't use a validator here - it may be all zeroes.
211 */
212 r = dm_bm_read_lock(bm, CACHE_SUPERBLOCK_LOCATION, NULL, &b);
213 if (r)
214 return r;
215
216 data_le = dm_block_data(b);
Joe Thornberdd8b0c22013-10-24 14:10:28 -0400217 *result = true;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000218 for (i = 0; i < sb_block_size; i++) {
219 if (data_le[i] != zero) {
Joe Thornberdd8b0c22013-10-24 14:10:28 -0400220 *result = false;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000221 break;
222 }
223 }
224
225 return dm_bm_unlock(b);
226}
227
228static void __setup_mapping_info(struct dm_cache_metadata *cmd)
229{
230 struct dm_btree_value_type vt;
231
232 vt.context = NULL;
233 vt.size = sizeof(__le64);
234 vt.inc = NULL;
235 vt.dec = NULL;
236 vt.equal = NULL;
237 dm_array_info_init(&cmd->info, cmd->tm, &vt);
238
239 if (cmd->policy_hint_size) {
240 vt.size = sizeof(__le32);
241 dm_array_info_init(&cmd->hint_info, cmd->tm, &vt);
242 }
243}
244
245static int __write_initial_superblock(struct dm_cache_metadata *cmd)
246{
247 int r;
248 struct dm_block *sblock;
249 size_t metadata_len;
250 struct cache_disk_superblock *disk_super;
251 sector_t bdev_size = i_size_read(cmd->bdev->bd_inode) >> SECTOR_SHIFT;
252
253 /* FIXME: see if we can lose the max sectors limit */
254 if (bdev_size > DM_CACHE_METADATA_MAX_SECTORS)
255 bdev_size = DM_CACHE_METADATA_MAX_SECTORS;
256
257 r = dm_sm_root_size(cmd->metadata_sm, &metadata_len);
258 if (r < 0)
259 return r;
260
261 r = dm_tm_pre_commit(cmd->tm);
262 if (r < 0)
263 return r;
264
265 r = superblock_lock_zero(cmd, &sblock);
266 if (r)
267 return r;
268
269 disk_super = dm_block_data(sblock);
270 disk_super->flags = 0;
271 memset(disk_super->uuid, 0, sizeof(disk_super->uuid));
272 disk_super->magic = cpu_to_le64(CACHE_SUPERBLOCK_MAGIC);
273 disk_super->version = cpu_to_le32(CACHE_VERSION);
Mike Snitzer4e7f5062013-03-20 17:21:27 +0000274 memset(disk_super->policy_name, 0, sizeof(disk_super->policy_name));
275 memset(disk_super->policy_version, 0, sizeof(disk_super->policy_version));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000276 disk_super->policy_hint_size = 0;
277
278 r = dm_sm_copy_root(cmd->metadata_sm, &disk_super->metadata_space_map_root,
279 metadata_len);
280 if (r < 0)
281 goto bad_locked;
282
283 disk_super->mapping_root = cpu_to_le64(cmd->root);
284 disk_super->hint_root = cpu_to_le64(cmd->hint_root);
285 disk_super->discard_root = cpu_to_le64(cmd->discard_root);
286 disk_super->discard_block_size = cpu_to_le64(cmd->discard_block_size);
287 disk_super->discard_nr_blocks = cpu_to_le64(from_dblock(cmd->discard_nr_blocks));
288 disk_super->metadata_block_size = cpu_to_le32(DM_CACHE_METADATA_BLOCK_SIZE >> SECTOR_SHIFT);
289 disk_super->data_block_size = cpu_to_le32(cmd->data_block_size);
290 disk_super->cache_blocks = cpu_to_le32(0);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000291
292 disk_super->read_hits = cpu_to_le32(0);
293 disk_super->read_misses = cpu_to_le32(0);
294 disk_super->write_hits = cpu_to_le32(0);
295 disk_super->write_misses = cpu_to_le32(0);
296
297 return dm_tm_commit(cmd->tm, sblock);
298
299bad_locked:
300 dm_bm_unlock(sblock);
301 return r;
302}
303
304static int __format_metadata(struct dm_cache_metadata *cmd)
305{
306 int r;
307
308 r = dm_tm_create_with_sm(cmd->bm, CACHE_SUPERBLOCK_LOCATION,
309 &cmd->tm, &cmd->metadata_sm);
310 if (r < 0) {
311 DMERR("tm_create_with_sm failed");
312 return r;
313 }
314
315 __setup_mapping_info(cmd);
316
317 r = dm_array_empty(&cmd->info, &cmd->root);
318 if (r < 0)
319 goto bad;
320
321 dm_disk_bitset_init(cmd->tm, &cmd->discard_info);
322
323 r = dm_bitset_empty(&cmd->discard_info, &cmd->discard_root);
324 if (r < 0)
325 goto bad;
326
327 cmd->discard_block_size = 0;
328 cmd->discard_nr_blocks = 0;
329
330 r = __write_initial_superblock(cmd);
331 if (r)
332 goto bad;
333
334 cmd->clean_when_opened = true;
335 return 0;
336
337bad:
338 dm_tm_destroy(cmd->tm);
339 dm_sm_destroy(cmd->metadata_sm);
340
341 return r;
342}
343
344static int __check_incompat_features(struct cache_disk_superblock *disk_super,
345 struct dm_cache_metadata *cmd)
346{
347 uint32_t features;
348
349 features = le32_to_cpu(disk_super->incompat_flags) & ~DM_CACHE_FEATURE_INCOMPAT_SUPP;
350 if (features) {
351 DMERR("could not access metadata due to unsupported optional features (%lx).",
352 (unsigned long)features);
353 return -EINVAL;
354 }
355
356 /*
357 * Check for read-only metadata to skip the following RDWR checks.
358 */
359 if (get_disk_ro(cmd->bdev->bd_disk))
360 return 0;
361
362 features = le32_to_cpu(disk_super->compat_ro_flags) & ~DM_CACHE_FEATURE_COMPAT_RO_SUPP;
363 if (features) {
364 DMERR("could not access metadata RDWR due to unsupported optional features (%lx).",
365 (unsigned long)features);
366 return -EINVAL;
367 }
368
369 return 0;
370}
371
372static int __open_metadata(struct dm_cache_metadata *cmd)
373{
374 int r;
375 struct dm_block *sblock;
376 struct cache_disk_superblock *disk_super;
377 unsigned long sb_flags;
378
379 r = superblock_read_lock(cmd, &sblock);
380 if (r < 0) {
381 DMERR("couldn't read lock superblock");
382 return r;
383 }
384
385 disk_super = dm_block_data(sblock);
386
387 r = __check_incompat_features(disk_super, cmd);
388 if (r < 0)
389 goto bad;
390
391 r = dm_tm_open_with_sm(cmd->bm, CACHE_SUPERBLOCK_LOCATION,
392 disk_super->metadata_space_map_root,
393 sizeof(disk_super->metadata_space_map_root),
394 &cmd->tm, &cmd->metadata_sm);
395 if (r < 0) {
396 DMERR("tm_open_with_sm failed");
397 goto bad;
398 }
399
400 __setup_mapping_info(cmd);
401 dm_disk_bitset_init(cmd->tm, &cmd->discard_info);
402 sb_flags = le32_to_cpu(disk_super->flags);
403 cmd->clean_when_opened = test_bit(CLEAN_SHUTDOWN, &sb_flags);
404 return dm_bm_unlock(sblock);
405
406bad:
407 dm_bm_unlock(sblock);
408 return r;
409}
410
411static int __open_or_format_metadata(struct dm_cache_metadata *cmd,
412 bool format_device)
413{
Joe Thornberdd8b0c22013-10-24 14:10:28 -0400414 int r;
415 bool unformatted = false;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000416
417 r = __superblock_all_zeroes(cmd->bm, &unformatted);
418 if (r)
419 return r;
420
421 if (unformatted)
422 return format_device ? __format_metadata(cmd) : -EPERM;
423
424 return __open_metadata(cmd);
425}
426
427static int __create_persistent_data_objects(struct dm_cache_metadata *cmd,
428 bool may_format_device)
429{
430 int r;
431 cmd->bm = dm_block_manager_create(cmd->bdev, DM_CACHE_METADATA_BLOCK_SIZE,
432 CACHE_METADATA_CACHE_SIZE,
433 CACHE_MAX_CONCURRENT_LOCKS);
434 if (IS_ERR(cmd->bm)) {
435 DMERR("could not create block manager");
436 return PTR_ERR(cmd->bm);
437 }
438
439 r = __open_or_format_metadata(cmd, may_format_device);
440 if (r)
441 dm_block_manager_destroy(cmd->bm);
442
443 return r;
444}
445
446static void __destroy_persistent_data_objects(struct dm_cache_metadata *cmd)
447{
448 dm_sm_destroy(cmd->metadata_sm);
449 dm_tm_destroy(cmd->tm);
450 dm_block_manager_destroy(cmd->bm);
451}
452
453typedef unsigned long (*flags_mutator)(unsigned long);
454
455static void update_flags(struct cache_disk_superblock *disk_super,
456 flags_mutator mutator)
457{
458 uint32_t sb_flags = mutator(le32_to_cpu(disk_super->flags));
459 disk_super->flags = cpu_to_le32(sb_flags);
460}
461
462static unsigned long set_clean_shutdown(unsigned long flags)
463{
464 set_bit(CLEAN_SHUTDOWN, &flags);
465 return flags;
466}
467
468static unsigned long clear_clean_shutdown(unsigned long flags)
469{
470 clear_bit(CLEAN_SHUTDOWN, &flags);
471 return flags;
472}
473
474static void read_superblock_fields(struct dm_cache_metadata *cmd,
475 struct cache_disk_superblock *disk_super)
476{
477 cmd->root = le64_to_cpu(disk_super->mapping_root);
478 cmd->hint_root = le64_to_cpu(disk_super->hint_root);
479 cmd->discard_root = le64_to_cpu(disk_super->discard_root);
480 cmd->discard_block_size = le64_to_cpu(disk_super->discard_block_size);
481 cmd->discard_nr_blocks = to_dblock(le64_to_cpu(disk_super->discard_nr_blocks));
482 cmd->data_block_size = le32_to_cpu(disk_super->data_block_size);
483 cmd->cache_blocks = to_cblock(le32_to_cpu(disk_super->cache_blocks));
484 strncpy(cmd->policy_name, disk_super->policy_name, sizeof(cmd->policy_name));
Mike Snitzer4e7f5062013-03-20 17:21:27 +0000485 cmd->policy_version[0] = le32_to_cpu(disk_super->policy_version[0]);
486 cmd->policy_version[1] = le32_to_cpu(disk_super->policy_version[1]);
487 cmd->policy_version[2] = le32_to_cpu(disk_super->policy_version[2]);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000488 cmd->policy_hint_size = le32_to_cpu(disk_super->policy_hint_size);
489
490 cmd->stats.read_hits = le32_to_cpu(disk_super->read_hits);
491 cmd->stats.read_misses = le32_to_cpu(disk_super->read_misses);
492 cmd->stats.write_hits = le32_to_cpu(disk_super->write_hits);
493 cmd->stats.write_misses = le32_to_cpu(disk_super->write_misses);
494
495 cmd->changed = false;
496}
497
498/*
499 * The mutator updates the superblock flags.
500 */
501static int __begin_transaction_flags(struct dm_cache_metadata *cmd,
502 flags_mutator mutator)
503{
504 int r;
505 struct cache_disk_superblock *disk_super;
506 struct dm_block *sblock;
507
508 r = superblock_lock(cmd, &sblock);
509 if (r)
510 return r;
511
512 disk_super = dm_block_data(sblock);
513 update_flags(disk_super, mutator);
514 read_superblock_fields(cmd, disk_super);
515
516 return dm_bm_flush_and_unlock(cmd->bm, sblock);
517}
518
519static int __begin_transaction(struct dm_cache_metadata *cmd)
520{
521 int r;
522 struct cache_disk_superblock *disk_super;
523 struct dm_block *sblock;
524
525 /*
526 * We re-read the superblock every time. Shouldn't need to do this
527 * really.
528 */
529 r = superblock_read_lock(cmd, &sblock);
530 if (r)
531 return r;
532
533 disk_super = dm_block_data(sblock);
534 read_superblock_fields(cmd, disk_super);
535 dm_bm_unlock(sblock);
536
537 return 0;
538}
539
540static int __commit_transaction(struct dm_cache_metadata *cmd,
541 flags_mutator mutator)
542{
543 int r;
544 size_t metadata_len;
545 struct cache_disk_superblock *disk_super;
546 struct dm_block *sblock;
547
548 /*
549 * We need to know if the cache_disk_superblock exceeds a 512-byte sector.
550 */
551 BUILD_BUG_ON(sizeof(struct cache_disk_superblock) > 512);
552
553 r = dm_bitset_flush(&cmd->discard_info, cmd->discard_root,
554 &cmd->discard_root);
555 if (r)
556 return r;
557
558 r = dm_tm_pre_commit(cmd->tm);
559 if (r < 0)
560 return r;
561
562 r = dm_sm_root_size(cmd->metadata_sm, &metadata_len);
563 if (r < 0)
564 return r;
565
566 r = superblock_lock(cmd, &sblock);
567 if (r)
568 return r;
569
570 disk_super = dm_block_data(sblock);
571
572 if (mutator)
573 update_flags(disk_super, mutator);
574
575 disk_super->mapping_root = cpu_to_le64(cmd->root);
576 disk_super->hint_root = cpu_to_le64(cmd->hint_root);
577 disk_super->discard_root = cpu_to_le64(cmd->discard_root);
578 disk_super->discard_block_size = cpu_to_le64(cmd->discard_block_size);
579 disk_super->discard_nr_blocks = cpu_to_le64(from_dblock(cmd->discard_nr_blocks));
580 disk_super->cache_blocks = cpu_to_le32(from_cblock(cmd->cache_blocks));
581 strncpy(disk_super->policy_name, cmd->policy_name, sizeof(disk_super->policy_name));
Mike Snitzer4e7f5062013-03-20 17:21:27 +0000582 disk_super->policy_version[0] = cpu_to_le32(cmd->policy_version[0]);
583 disk_super->policy_version[1] = cpu_to_le32(cmd->policy_version[1]);
584 disk_super->policy_version[2] = cpu_to_le32(cmd->policy_version[2]);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000585
586 disk_super->read_hits = cpu_to_le32(cmd->stats.read_hits);
587 disk_super->read_misses = cpu_to_le32(cmd->stats.read_misses);
588 disk_super->write_hits = cpu_to_le32(cmd->stats.write_hits);
589 disk_super->write_misses = cpu_to_le32(cmd->stats.write_misses);
590
591 r = dm_sm_copy_root(cmd->metadata_sm, &disk_super->metadata_space_map_root,
592 metadata_len);
593 if (r < 0) {
594 dm_bm_unlock(sblock);
595 return r;
596 }
597
598 return dm_tm_commit(cmd->tm, sblock);
599}
600
601/*----------------------------------------------------------------*/
602
603/*
604 * The mappings are held in a dm-array that has 64-bit values stored in
605 * little-endian format. The index is the cblock, the high 48bits of the
606 * value are the oblock and the low 16 bit the flags.
607 */
608#define FLAGS_MASK ((1 << 16) - 1)
609
610static __le64 pack_value(dm_oblock_t block, unsigned flags)
611{
612 uint64_t value = from_oblock(block);
613 value <<= 16;
614 value = value | (flags & FLAGS_MASK);
615 return cpu_to_le64(value);
616}
617
618static void unpack_value(__le64 value_le, dm_oblock_t *block, unsigned *flags)
619{
620 uint64_t value = le64_to_cpu(value_le);
621 uint64_t b = value >> 16;
622 *block = to_oblock(b);
623 *flags = value & FLAGS_MASK;
624}
625
626/*----------------------------------------------------------------*/
627
628struct dm_cache_metadata *dm_cache_metadata_open(struct block_device *bdev,
629 sector_t data_block_size,
630 bool may_format_device,
631 size_t policy_hint_size)
632{
633 int r;
634 struct dm_cache_metadata *cmd;
635
636 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
637 if (!cmd) {
638 DMERR("could not allocate metadata struct");
639 return NULL;
640 }
641
642 init_rwsem(&cmd->root_lock);
643 cmd->bdev = bdev;
644 cmd->data_block_size = data_block_size;
645 cmd->cache_blocks = 0;
646 cmd->policy_hint_size = policy_hint_size;
647 cmd->changed = true;
648
649 r = __create_persistent_data_objects(cmd, may_format_device);
650 if (r) {
651 kfree(cmd);
652 return ERR_PTR(r);
653 }
654
655 r = __begin_transaction_flags(cmd, clear_clean_shutdown);
656 if (r < 0) {
657 dm_cache_metadata_close(cmd);
658 return ERR_PTR(r);
659 }
660
661 return cmd;
662}
663
664void dm_cache_metadata_close(struct dm_cache_metadata *cmd)
665{
666 __destroy_persistent_data_objects(cmd);
667 kfree(cmd);
668}
669
Joe Thornberf494a9c2013-10-31 13:55:49 -0400670/*
671 * Checks that the given cache block is either unmapped or clean.
672 */
673static int block_unmapped_or_clean(struct dm_cache_metadata *cmd, dm_cblock_t b,
674 bool *result)
675{
676 int r;
677 __le64 value;
678 dm_oblock_t ob;
679 unsigned flags;
680
681 r = dm_array_get_value(&cmd->info, cmd->root, from_cblock(b), &value);
682 if (r) {
683 DMERR("block_unmapped_or_clean failed");
684 return r;
685 }
686
687 unpack_value(value, &ob, &flags);
688 *result = !((flags & M_VALID) && (flags & M_DIRTY));
689
690 return 0;
691}
692
693static int blocks_are_unmapped_or_clean(struct dm_cache_metadata *cmd,
694 dm_cblock_t begin, dm_cblock_t end,
695 bool *result)
696{
697 int r;
698 *result = true;
699
700 while (begin != end) {
701 r = block_unmapped_or_clean(cmd, begin, result);
702 if (r)
703 return r;
704
705 if (!*result) {
706 DMERR("cache block %llu is dirty",
707 (unsigned long long) from_cblock(begin));
708 return 0;
709 }
710
711 begin = to_cblock(from_cblock(begin) + 1);
712 }
713
714 return 0;
715}
716
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000717int dm_cache_resize(struct dm_cache_metadata *cmd, dm_cblock_t new_cache_size)
718{
719 int r;
Joe Thornberf494a9c2013-10-31 13:55:49 -0400720 bool clean;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000721 __le64 null_mapping = pack_value(0, 0);
722
723 down_write(&cmd->root_lock);
724 __dm_bless_for_disk(&null_mapping);
Joe Thornberf494a9c2013-10-31 13:55:49 -0400725
726 if (from_cblock(new_cache_size) < from_cblock(cmd->cache_blocks)) {
727 r = blocks_are_unmapped_or_clean(cmd, new_cache_size, cmd->cache_blocks, &clean);
728 if (r) {
729 __dm_unbless_for_disk(&null_mapping);
730 goto out;
731 }
732
733 if (!clean) {
734 DMERR("unable to shrink cache due to dirty blocks");
735 r = -EINVAL;
736 __dm_unbless_for_disk(&null_mapping);
737 goto out;
738 }
739 }
740
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000741 r = dm_array_resize(&cmd->info, cmd->root, from_cblock(cmd->cache_blocks),
742 from_cblock(new_cache_size),
743 &null_mapping, &cmd->root);
744 if (!r)
745 cmd->cache_blocks = new_cache_size;
746 cmd->changed = true;
Joe Thornberf494a9c2013-10-31 13:55:49 -0400747
748out:
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000749 up_write(&cmd->root_lock);
750
751 return r;
752}
753
754int dm_cache_discard_bitset_resize(struct dm_cache_metadata *cmd,
755 sector_t discard_block_size,
756 dm_dblock_t new_nr_entries)
757{
758 int r;
759
760 down_write(&cmd->root_lock);
761 r = dm_bitset_resize(&cmd->discard_info,
762 cmd->discard_root,
763 from_dblock(cmd->discard_nr_blocks),
764 from_dblock(new_nr_entries),
765 false, &cmd->discard_root);
766 if (!r) {
767 cmd->discard_block_size = discard_block_size;
768 cmd->discard_nr_blocks = new_nr_entries;
769 }
770
771 cmd->changed = true;
772 up_write(&cmd->root_lock);
773
774 return r;
775}
776
777static int __set_discard(struct dm_cache_metadata *cmd, dm_dblock_t b)
778{
779 return dm_bitset_set_bit(&cmd->discard_info, cmd->discard_root,
780 from_dblock(b), &cmd->discard_root);
781}
782
783static int __clear_discard(struct dm_cache_metadata *cmd, dm_dblock_t b)
784{
785 return dm_bitset_clear_bit(&cmd->discard_info, cmd->discard_root,
786 from_dblock(b), &cmd->discard_root);
787}
788
789static int __is_discarded(struct dm_cache_metadata *cmd, dm_dblock_t b,
790 bool *is_discarded)
791{
792 return dm_bitset_test_bit(&cmd->discard_info, cmd->discard_root,
793 from_dblock(b), &cmd->discard_root,
794 is_discarded);
795}
796
797static int __discard(struct dm_cache_metadata *cmd,
798 dm_dblock_t dblock, bool discard)
799{
800 int r;
801
802 r = (discard ? __set_discard : __clear_discard)(cmd, dblock);
803 if (r)
804 return r;
805
806 cmd->changed = true;
807 return 0;
808}
809
810int dm_cache_set_discard(struct dm_cache_metadata *cmd,
811 dm_dblock_t dblock, bool discard)
812{
813 int r;
814
815 down_write(&cmd->root_lock);
816 r = __discard(cmd, dblock, discard);
817 up_write(&cmd->root_lock);
818
819 return r;
820}
821
822static int __load_discards(struct dm_cache_metadata *cmd,
823 load_discard_fn fn, void *context)
824{
825 int r = 0;
826 dm_block_t b;
827 bool discard;
828
829 for (b = 0; b < from_dblock(cmd->discard_nr_blocks); b++) {
830 dm_dblock_t dblock = to_dblock(b);
831
832 if (cmd->clean_when_opened) {
833 r = __is_discarded(cmd, dblock, &discard);
834 if (r)
835 return r;
836 } else
837 discard = false;
838
839 r = fn(context, cmd->discard_block_size, dblock, discard);
840 if (r)
841 break;
842 }
843
844 return r;
845}
846
847int dm_cache_load_discards(struct dm_cache_metadata *cmd,
848 load_discard_fn fn, void *context)
849{
850 int r;
851
852 down_read(&cmd->root_lock);
853 r = __load_discards(cmd, fn, context);
854 up_read(&cmd->root_lock);
855
856 return r;
857}
858
859dm_cblock_t dm_cache_size(struct dm_cache_metadata *cmd)
860{
861 dm_cblock_t r;
862
863 down_read(&cmd->root_lock);
864 r = cmd->cache_blocks;
865 up_read(&cmd->root_lock);
866
867 return r;
868}
869
870static int __remove(struct dm_cache_metadata *cmd, dm_cblock_t cblock)
871{
872 int r;
873 __le64 value = pack_value(0, 0);
874
875 __dm_bless_for_disk(&value);
876 r = dm_array_set_value(&cmd->info, cmd->root, from_cblock(cblock),
877 &value, &cmd->root);
878 if (r)
879 return r;
880
881 cmd->changed = true;
882 return 0;
883}
884
885int dm_cache_remove_mapping(struct dm_cache_metadata *cmd, dm_cblock_t cblock)
886{
887 int r;
888
889 down_write(&cmd->root_lock);
890 r = __remove(cmd, cblock);
891 up_write(&cmd->root_lock);
892
893 return r;
894}
895
896static int __insert(struct dm_cache_metadata *cmd,
897 dm_cblock_t cblock, dm_oblock_t oblock)
898{
899 int r;
900 __le64 value = pack_value(oblock, M_VALID);
901 __dm_bless_for_disk(&value);
902
903 r = dm_array_set_value(&cmd->info, cmd->root, from_cblock(cblock),
904 &value, &cmd->root);
905 if (r)
906 return r;
907
908 cmd->changed = true;
909 return 0;
910}
911
912int dm_cache_insert_mapping(struct dm_cache_metadata *cmd,
913 dm_cblock_t cblock, dm_oblock_t oblock)
914{
915 int r;
916
917 down_write(&cmd->root_lock);
918 r = __insert(cmd, cblock, oblock);
919 up_write(&cmd->root_lock);
920
921 return r;
922}
923
924struct thunk {
925 load_mapping_fn fn;
926 void *context;
927
928 struct dm_cache_metadata *cmd;
929 bool respect_dirty_flags;
930 bool hints_valid;
931};
932
Mike Snitzerea2dd8c2013-03-20 17:21:28 +0000933static bool policy_unchanged(struct dm_cache_metadata *cmd,
934 struct dm_cache_policy *policy)
935{
936 const char *policy_name = dm_cache_policy_get_name(policy);
937 const unsigned *policy_version = dm_cache_policy_get_version(policy);
938 size_t policy_hint_size = dm_cache_policy_get_hint_size(policy);
939
940 /*
941 * Ensure policy names match.
942 */
943 if (strncmp(cmd->policy_name, policy_name, sizeof(cmd->policy_name)))
944 return false;
945
946 /*
947 * Ensure policy major versions match.
948 */
949 if (cmd->policy_version[0] != policy_version[0])
950 return false;
951
952 /*
953 * Ensure policy hint sizes match.
954 */
955 if (cmd->policy_hint_size != policy_hint_size)
956 return false;
957
958 return true;
959}
960
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000961static bool hints_array_initialized(struct dm_cache_metadata *cmd)
962{
963 return cmd->hint_root && cmd->policy_hint_size;
964}
965
966static bool hints_array_available(struct dm_cache_metadata *cmd,
Mike Snitzerea2dd8c2013-03-20 17:21:28 +0000967 struct dm_cache_policy *policy)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000968{
Mike Snitzerea2dd8c2013-03-20 17:21:28 +0000969 return cmd->clean_when_opened && policy_unchanged(cmd, policy) &&
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000970 hints_array_initialized(cmd);
971}
972
973static int __load_mapping(void *context, uint64_t cblock, void *leaf)
974{
975 int r = 0;
976 bool dirty;
977 __le64 value;
978 __le32 hint_value = 0;
979 dm_oblock_t oblock;
980 unsigned flags;
981 struct thunk *thunk = context;
982 struct dm_cache_metadata *cmd = thunk->cmd;
983
984 memcpy(&value, leaf, sizeof(value));
985 unpack_value(value, &oblock, &flags);
986
987 if (flags & M_VALID) {
988 if (thunk->hints_valid) {
989 r = dm_array_get_value(&cmd->hint_info, cmd->hint_root,
990 cblock, &hint_value);
991 if (r && r != -ENODATA)
992 return r;
993 }
994
995 dirty = thunk->respect_dirty_flags ? (flags & M_DIRTY) : true;
996 r = thunk->fn(thunk->context, oblock, to_cblock(cblock),
997 dirty, le32_to_cpu(hint_value), thunk->hints_valid);
998 }
999
1000 return r;
1001}
1002
Mike Snitzerea2dd8c2013-03-20 17:21:28 +00001003static int __load_mappings(struct dm_cache_metadata *cmd,
1004 struct dm_cache_policy *policy,
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001005 load_mapping_fn fn, void *context)
1006{
1007 struct thunk thunk;
1008
1009 thunk.fn = fn;
1010 thunk.context = context;
1011
1012 thunk.cmd = cmd;
1013 thunk.respect_dirty_flags = cmd->clean_when_opened;
Mike Snitzerea2dd8c2013-03-20 17:21:28 +00001014 thunk.hints_valid = hints_array_available(cmd, policy);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001015
1016 return dm_array_walk(&cmd->info, cmd->root, __load_mapping, &thunk);
1017}
1018
Mike Snitzerea2dd8c2013-03-20 17:21:28 +00001019int dm_cache_load_mappings(struct dm_cache_metadata *cmd,
1020 struct dm_cache_policy *policy,
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001021 load_mapping_fn fn, void *context)
1022{
1023 int r;
1024
1025 down_read(&cmd->root_lock);
Mike Snitzerea2dd8c2013-03-20 17:21:28 +00001026 r = __load_mappings(cmd, policy, fn, context);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001027 up_read(&cmd->root_lock);
1028
1029 return r;
1030}
1031
1032static int __dump_mapping(void *context, uint64_t cblock, void *leaf)
1033{
1034 int r = 0;
1035 __le64 value;
1036 dm_oblock_t oblock;
1037 unsigned flags;
1038
1039 memcpy(&value, leaf, sizeof(value));
1040 unpack_value(value, &oblock, &flags);
1041
1042 return r;
1043}
1044
1045static int __dump_mappings(struct dm_cache_metadata *cmd)
1046{
1047 return dm_array_walk(&cmd->info, cmd->root, __dump_mapping, NULL);
1048}
1049
1050void dm_cache_dump(struct dm_cache_metadata *cmd)
1051{
1052 down_read(&cmd->root_lock);
1053 __dump_mappings(cmd);
1054 up_read(&cmd->root_lock);
1055}
1056
1057int dm_cache_changed_this_transaction(struct dm_cache_metadata *cmd)
1058{
1059 int r;
1060
1061 down_read(&cmd->root_lock);
1062 r = cmd->changed;
1063 up_read(&cmd->root_lock);
1064
1065 return r;
1066}
1067
1068static int __dirty(struct dm_cache_metadata *cmd, dm_cblock_t cblock, bool dirty)
1069{
1070 int r;
1071 unsigned flags;
1072 dm_oblock_t oblock;
1073 __le64 value;
1074
1075 r = dm_array_get_value(&cmd->info, cmd->root, from_cblock(cblock), &value);
1076 if (r)
1077 return r;
1078
1079 unpack_value(value, &oblock, &flags);
1080
1081 if (((flags & M_DIRTY) && dirty) || (!(flags & M_DIRTY) && !dirty))
1082 /* nothing to be done */
1083 return 0;
1084
Joe Thornber79ed9ca2013-03-20 17:21:27 +00001085 value = pack_value(oblock, (flags & ~M_DIRTY) | (dirty ? M_DIRTY : 0));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001086 __dm_bless_for_disk(&value);
1087
1088 r = dm_array_set_value(&cmd->info, cmd->root, from_cblock(cblock),
1089 &value, &cmd->root);
1090 if (r)
1091 return r;
1092
1093 cmd->changed = true;
1094 return 0;
1095
1096}
1097
1098int dm_cache_set_dirty(struct dm_cache_metadata *cmd,
1099 dm_cblock_t cblock, bool dirty)
1100{
1101 int r;
1102
1103 down_write(&cmd->root_lock);
1104 r = __dirty(cmd, cblock, dirty);
1105 up_write(&cmd->root_lock);
1106
1107 return r;
1108}
1109
1110void dm_cache_metadata_get_stats(struct dm_cache_metadata *cmd,
1111 struct dm_cache_statistics *stats)
1112{
1113 down_read(&cmd->root_lock);
Joe Thornber8c5008f2013-05-10 14:37:18 +01001114 *stats = cmd->stats;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001115 up_read(&cmd->root_lock);
1116}
1117
1118void dm_cache_metadata_set_stats(struct dm_cache_metadata *cmd,
1119 struct dm_cache_statistics *stats)
1120{
1121 down_write(&cmd->root_lock);
Joe Thornber8c5008f2013-05-10 14:37:18 +01001122 cmd->stats = *stats;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001123 up_write(&cmd->root_lock);
1124}
1125
1126int dm_cache_commit(struct dm_cache_metadata *cmd, bool clean_shutdown)
1127{
1128 int r;
1129 flags_mutator mutator = (clean_shutdown ? set_clean_shutdown :
1130 clear_clean_shutdown);
1131
1132 down_write(&cmd->root_lock);
1133 r = __commit_transaction(cmd, mutator);
1134 if (r)
1135 goto out;
1136
1137 r = __begin_transaction(cmd);
1138
1139out:
1140 up_write(&cmd->root_lock);
1141 return r;
1142}
1143
1144int dm_cache_get_free_metadata_block_count(struct dm_cache_metadata *cmd,
1145 dm_block_t *result)
1146{
1147 int r = -EINVAL;
1148
1149 down_read(&cmd->root_lock);
1150 r = dm_sm_get_nr_free(cmd->metadata_sm, result);
1151 up_read(&cmd->root_lock);
1152
1153 return r;
1154}
1155
1156int dm_cache_get_metadata_dev_size(struct dm_cache_metadata *cmd,
1157 dm_block_t *result)
1158{
1159 int r = -EINVAL;
1160
1161 down_read(&cmd->root_lock);
1162 r = dm_sm_get_nr_blocks(cmd->metadata_sm, result);
1163 up_read(&cmd->root_lock);
1164
1165 return r;
1166}
1167
1168/*----------------------------------------------------------------*/
1169
1170static int begin_hints(struct dm_cache_metadata *cmd, struct dm_cache_policy *policy)
1171{
1172 int r;
1173 __le32 value;
1174 size_t hint_size;
1175 const char *policy_name = dm_cache_policy_get_name(policy);
Mike Snitzer4e7f5062013-03-20 17:21:27 +00001176 const unsigned *policy_version = dm_cache_policy_get_version(policy);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001177
1178 if (!policy_name[0] ||
1179 (strlen(policy_name) > sizeof(cmd->policy_name) - 1))
1180 return -EINVAL;
1181
Mike Snitzerea2dd8c2013-03-20 17:21:28 +00001182 if (!policy_unchanged(cmd, policy)) {
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001183 strncpy(cmd->policy_name, policy_name, sizeof(cmd->policy_name));
Mike Snitzer4e7f5062013-03-20 17:21:27 +00001184 memcpy(cmd->policy_version, policy_version, sizeof(cmd->policy_version));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001185
1186 hint_size = dm_cache_policy_get_hint_size(policy);
1187 if (!hint_size)
1188 return 0; /* short-circuit hints initialization */
1189 cmd->policy_hint_size = hint_size;
1190
1191 if (cmd->hint_root) {
1192 r = dm_array_del(&cmd->hint_info, cmd->hint_root);
1193 if (r)
1194 return r;
1195 }
1196
1197 r = dm_array_empty(&cmd->hint_info, &cmd->hint_root);
1198 if (r)
1199 return r;
1200
1201 value = cpu_to_le32(0);
1202 __dm_bless_for_disk(&value);
1203 r = dm_array_resize(&cmd->hint_info, cmd->hint_root, 0,
1204 from_cblock(cmd->cache_blocks),
1205 &value, &cmd->hint_root);
1206 if (r)
1207 return r;
1208 }
1209
1210 return 0;
1211}
1212
1213int dm_cache_begin_hints(struct dm_cache_metadata *cmd, struct dm_cache_policy *policy)
1214{
1215 int r;
1216
1217 down_write(&cmd->root_lock);
1218 r = begin_hints(cmd, policy);
1219 up_write(&cmd->root_lock);
1220
1221 return r;
1222}
1223
1224static int save_hint(struct dm_cache_metadata *cmd, dm_cblock_t cblock,
1225 uint32_t hint)
1226{
1227 int r;
1228 __le32 value = cpu_to_le32(hint);
1229 __dm_bless_for_disk(&value);
1230
1231 r = dm_array_set_value(&cmd->hint_info, cmd->hint_root,
1232 from_cblock(cblock), &value, &cmd->hint_root);
1233 cmd->changed = true;
1234
1235 return r;
1236}
1237
1238int dm_cache_save_hint(struct dm_cache_metadata *cmd, dm_cblock_t cblock,
1239 uint32_t hint)
1240{
1241 int r;
1242
1243 if (!hints_array_initialized(cmd))
1244 return 0;
1245
1246 down_write(&cmd->root_lock);
1247 r = save_hint(cmd, cblock, hint);
1248 up_write(&cmd->root_lock);
1249
1250 return r;
1251}
Joe Thornber2ee57d52013-10-24 14:10:29 -04001252
1253int dm_cache_metadata_all_clean(struct dm_cache_metadata *cmd, bool *result)
1254{
1255 return blocks_are_unmapped_or_clean(cmd, 0, cmd->cache_blocks, result);
1256}