blob: 5a5ef996a26a7baa80b8fed217d89f6c1d8aad34 [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
Joe Thornber53d49812013-10-16 10:59:26 +010023
24/*
25 * defines a range of metadata versions that this module can handle.
26 */
27#define MIN_CACHE_VERSION 1
Joe Thornber629d0a82016-09-22 06:15:21 -040028#define MAX_CACHE_VERSION 2
Joe Thornber53d49812013-10-16 10:59:26 +010029
Joe Thornberc6b4fcb2013-03-01 22:45:51 +000030#define CACHE_METADATA_CACHE_SIZE 64
31
32/*
33 * 3 for btree insert +
34 * 2 for btree lookup used within space map
35 */
36#define CACHE_MAX_CONCURRENT_LOCKS 5
37#define SPACE_MAP_ROOT_SIZE 128
38
39enum superblock_flag_bits {
40 /* for spotting crashes that would invalidate the dirty bitset */
41 CLEAN_SHUTDOWN,
Joe Thornber028ae9f2015-04-22 16:42:35 -040042 /* metadata must be checked using the tools */
43 NEEDS_CHECK,
Joe Thornberc6b4fcb2013-03-01 22:45:51 +000044};
45
46/*
47 * Each mapping from cache block -> origin block carries a set of flags.
48 */
49enum mapping_bits {
50 /*
51 * A valid mapping. Because we're using an array we clear this
52 * flag for an non existant mapping.
53 */
54 M_VALID = 1,
55
56 /*
57 * The data on the cache is different from that on the origin.
Joe Thornber629d0a82016-09-22 06:15:21 -040058 * This flag is only used by metadata format 1.
Joe Thornberc6b4fcb2013-03-01 22:45:51 +000059 */
60 M_DIRTY = 2
61};
62
63struct cache_disk_superblock {
64 __le32 csum;
65 __le32 flags;
66 __le64 blocknr;
67
68 __u8 uuid[16];
69 __le64 magic;
70 __le32 version;
71
72 __u8 policy_name[CACHE_POLICY_NAME_SIZE];
73 __le32 policy_hint_size;
74
75 __u8 metadata_space_map_root[SPACE_MAP_ROOT_SIZE];
76 __le64 mapping_root;
77 __le64 hint_root;
78
79 __le64 discard_root;
80 __le64 discard_block_size;
81 __le64 discard_nr_blocks;
82
83 __le32 data_block_size;
84 __le32 metadata_block_size;
85 __le32 cache_blocks;
86
87 __le32 compat_flags;
88 __le32 compat_ro_flags;
89 __le32 incompat_flags;
90
91 __le32 read_hits;
92 __le32 read_misses;
93 __le32 write_hits;
94 __le32 write_misses;
Mike Snitzer4e7f5062013-03-20 17:21:27 +000095
96 __le32 policy_version[CACHE_POLICY_VERSION_SIZE];
Joe Thornber629d0a82016-09-22 06:15:21 -040097
98 /*
99 * Metadata format 2 fields.
100 */
101 __le64 dirty_root;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000102} __packed;
103
104struct dm_cache_metadata {
Joe Thornber9b1cc9f2015-01-23 10:00:07 +0000105 atomic_t ref_count;
106 struct list_head list;
107
Joe Thornber629d0a82016-09-22 06:15:21 -0400108 unsigned version;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000109 struct block_device *bdev;
110 struct dm_block_manager *bm;
111 struct dm_space_map *metadata_sm;
112 struct dm_transaction_manager *tm;
113
114 struct dm_array_info info;
115 struct dm_array_info hint_info;
116 struct dm_disk_bitset discard_info;
117
118 struct rw_semaphore root_lock;
Joe Thornber028ae9f2015-04-22 16:42:35 -0400119 unsigned long flags;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000120 dm_block_t root;
121 dm_block_t hint_root;
122 dm_block_t discard_root;
123
124 sector_t discard_block_size;
Joe Thornber1bad9bc2014-11-07 14:47:07 +0000125 dm_dblock_t discard_nr_blocks;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000126
127 sector_t data_block_size;
128 dm_cblock_t cache_blocks;
129 bool changed:1;
130 bool clean_when_opened:1;
131
132 char policy_name[CACHE_POLICY_NAME_SIZE];
Mike Snitzer4e7f5062013-03-20 17:21:27 +0000133 unsigned policy_version[CACHE_POLICY_VERSION_SIZE];
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000134 size_t policy_hint_size;
135 struct dm_cache_statistics stats;
Joe Thornber5a320832014-03-27 14:13:23 +0000136
137 /*
138 * Reading the space map root can fail, so we read it into this
139 * buffer before the superblock is locked and updated.
140 */
141 __u8 metadata_space_map_root[SPACE_MAP_ROOT_SIZE];
Joe Thornber028ae9f2015-04-22 16:42:35 -0400142
143 /*
144 * Set if a transaction has to be aborted but the attempt to roll
145 * back to the previous (good) transaction failed. The only
146 * metadata operation permissible in this state is the closing of
147 * the device.
148 */
149 bool fail_io:1;
Joe Thornberf1779402016-09-20 06:16:00 -0400150
151 /*
Joe Thornber629d0a82016-09-22 06:15:21 -0400152 * Metadata format 2 fields.
153 */
154 dm_block_t dirty_root;
155 struct dm_disk_bitset dirty_info;
156
157 /*
Joe Thornberf1779402016-09-20 06:16:00 -0400158 * These structures are used when loading metadata. They're too
159 * big to put on the stack.
160 */
161 struct dm_array_cursor mapping_cursor;
162 struct dm_array_cursor hint_cursor;
Joe Thornber629d0a82016-09-22 06:15:21 -0400163 struct dm_bitset_cursor dirty_cursor;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000164};
165
166/*-------------------------------------------------------------------
167 * superblock validator
168 *-----------------------------------------------------------------*/
169
170#define SUPERBLOCK_CSUM_XOR 9031977
171
172static void sb_prepare_for_write(struct dm_block_validator *v,
173 struct dm_block *b,
174 size_t sb_block_size)
175{
176 struct cache_disk_superblock *disk_super = dm_block_data(b);
177
178 disk_super->blocknr = cpu_to_le64(dm_block_location(b));
179 disk_super->csum = cpu_to_le32(dm_bm_checksum(&disk_super->flags,
180 sb_block_size - sizeof(__le32),
181 SUPERBLOCK_CSUM_XOR));
182}
183
Joe Thornber53d49812013-10-16 10:59:26 +0100184static int check_metadata_version(struct cache_disk_superblock *disk_super)
185{
186 uint32_t metadata_version = le32_to_cpu(disk_super->version);
Joe Thornber629d0a82016-09-22 06:15:21 -0400187
Joe Thornber53d49812013-10-16 10:59:26 +0100188 if (metadata_version < MIN_CACHE_VERSION || metadata_version > MAX_CACHE_VERSION) {
189 DMERR("Cache metadata version %u found, but only versions between %u and %u supported.",
190 metadata_version, MIN_CACHE_VERSION, MAX_CACHE_VERSION);
191 return -EINVAL;
192 }
193
194 return 0;
195}
196
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000197static int sb_check(struct dm_block_validator *v,
198 struct dm_block *b,
199 size_t sb_block_size)
200{
201 struct cache_disk_superblock *disk_super = dm_block_data(b);
202 __le32 csum_le;
203
204 if (dm_block_location(b) != le64_to_cpu(disk_super->blocknr)) {
205 DMERR("sb_check failed: blocknr %llu: wanted %llu",
206 le64_to_cpu(disk_super->blocknr),
207 (unsigned long long)dm_block_location(b));
208 return -ENOTBLK;
209 }
210
211 if (le64_to_cpu(disk_super->magic) != CACHE_SUPERBLOCK_MAGIC) {
212 DMERR("sb_check failed: magic %llu: wanted %llu",
213 le64_to_cpu(disk_super->magic),
214 (unsigned long long)CACHE_SUPERBLOCK_MAGIC);
215 return -EILSEQ;
216 }
217
218 csum_le = cpu_to_le32(dm_bm_checksum(&disk_super->flags,
219 sb_block_size - sizeof(__le32),
220 SUPERBLOCK_CSUM_XOR));
221 if (csum_le != disk_super->csum) {
222 DMERR("sb_check failed: csum %u: wanted %u",
223 le32_to_cpu(csum_le), le32_to_cpu(disk_super->csum));
224 return -EILSEQ;
225 }
226
Joe Thornber53d49812013-10-16 10:59:26 +0100227 return check_metadata_version(disk_super);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000228}
229
230static struct dm_block_validator sb_validator = {
231 .name = "superblock",
232 .prepare_for_write = sb_prepare_for_write,
233 .check = sb_check
234};
235
236/*----------------------------------------------------------------*/
237
238static int superblock_read_lock(struct dm_cache_metadata *cmd,
239 struct dm_block **sblock)
240{
241 return dm_bm_read_lock(cmd->bm, CACHE_SUPERBLOCK_LOCATION,
242 &sb_validator, sblock);
243}
244
245static int superblock_lock_zero(struct dm_cache_metadata *cmd,
246 struct dm_block **sblock)
247{
248 return dm_bm_write_lock_zero(cmd->bm, CACHE_SUPERBLOCK_LOCATION,
249 &sb_validator, sblock);
250}
251
252static int superblock_lock(struct dm_cache_metadata *cmd,
253 struct dm_block **sblock)
254{
255 return dm_bm_write_lock(cmd->bm, CACHE_SUPERBLOCK_LOCATION,
256 &sb_validator, sblock);
257}
258
259/*----------------------------------------------------------------*/
260
Joe Thornberdd8b0c22013-10-24 14:10:28 -0400261static int __superblock_all_zeroes(struct dm_block_manager *bm, bool *result)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000262{
263 int r;
264 unsigned i;
265 struct dm_block *b;
266 __le64 *data_le, zero = cpu_to_le64(0);
267 unsigned sb_block_size = dm_bm_block_size(bm) / sizeof(__le64);
268
269 /*
270 * We can't use a validator here - it may be all zeroes.
271 */
272 r = dm_bm_read_lock(bm, CACHE_SUPERBLOCK_LOCATION, NULL, &b);
273 if (r)
274 return r;
275
276 data_le = dm_block_data(b);
Joe Thornberdd8b0c22013-10-24 14:10:28 -0400277 *result = true;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000278 for (i = 0; i < sb_block_size; i++) {
279 if (data_le[i] != zero) {
Joe Thornberdd8b0c22013-10-24 14:10:28 -0400280 *result = false;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000281 break;
282 }
283 }
284
Mikulas Patocka4c7da062015-10-22 16:46:59 -0400285 dm_bm_unlock(b);
286
287 return 0;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000288}
289
290static void __setup_mapping_info(struct dm_cache_metadata *cmd)
291{
292 struct dm_btree_value_type vt;
293
294 vt.context = NULL;
295 vt.size = sizeof(__le64);
296 vt.inc = NULL;
297 vt.dec = NULL;
298 vt.equal = NULL;
299 dm_array_info_init(&cmd->info, cmd->tm, &vt);
300
301 if (cmd->policy_hint_size) {
302 vt.size = sizeof(__le32);
303 dm_array_info_init(&cmd->hint_info, cmd->tm, &vt);
304 }
305}
306
Joe Thornber5a320832014-03-27 14:13:23 +0000307static int __save_sm_root(struct dm_cache_metadata *cmd)
308{
309 int r;
310 size_t metadata_len;
311
312 r = dm_sm_root_size(cmd->metadata_sm, &metadata_len);
313 if (r < 0)
314 return r;
315
316 return dm_sm_copy_root(cmd->metadata_sm, &cmd->metadata_space_map_root,
317 metadata_len);
318}
319
320static void __copy_sm_root(struct dm_cache_metadata *cmd,
321 struct cache_disk_superblock *disk_super)
322{
323 memcpy(&disk_super->metadata_space_map_root,
324 &cmd->metadata_space_map_root,
325 sizeof(cmd->metadata_space_map_root));
326}
327
Joe Thornber629d0a82016-09-22 06:15:21 -0400328static bool separate_dirty_bits(struct dm_cache_metadata *cmd)
329{
330 return cmd->version >= 2;
331}
332
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000333static int __write_initial_superblock(struct dm_cache_metadata *cmd)
334{
335 int r;
336 struct dm_block *sblock;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000337 struct cache_disk_superblock *disk_super;
338 sector_t bdev_size = i_size_read(cmd->bdev->bd_inode) >> SECTOR_SHIFT;
339
340 /* FIXME: see if we can lose the max sectors limit */
341 if (bdev_size > DM_CACHE_METADATA_MAX_SECTORS)
342 bdev_size = DM_CACHE_METADATA_MAX_SECTORS;
343
Joe Thornber5a320832014-03-27 14:13:23 +0000344 r = dm_tm_pre_commit(cmd->tm);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000345 if (r < 0)
346 return r;
347
Joe Thornber5a320832014-03-27 14:13:23 +0000348 /*
349 * dm_sm_copy_root() can fail. So we need to do it before we start
350 * updating the superblock.
351 */
352 r = __save_sm_root(cmd);
353 if (r)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000354 return r;
355
356 r = superblock_lock_zero(cmd, &sblock);
357 if (r)
358 return r;
359
360 disk_super = dm_block_data(sblock);
361 disk_super->flags = 0;
362 memset(disk_super->uuid, 0, sizeof(disk_super->uuid));
363 disk_super->magic = cpu_to_le64(CACHE_SUPERBLOCK_MAGIC);
Joe Thornber629d0a82016-09-22 06:15:21 -0400364 disk_super->version = cpu_to_le32(cmd->version);
Mike Snitzer4e7f5062013-03-20 17:21:27 +0000365 memset(disk_super->policy_name, 0, sizeof(disk_super->policy_name));
366 memset(disk_super->policy_version, 0, sizeof(disk_super->policy_version));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000367 disk_super->policy_hint_size = 0;
368
Joe Thornber5a320832014-03-27 14:13:23 +0000369 __copy_sm_root(cmd, disk_super);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000370
371 disk_super->mapping_root = cpu_to_le64(cmd->root);
372 disk_super->hint_root = cpu_to_le64(cmd->hint_root);
373 disk_super->discard_root = cpu_to_le64(cmd->discard_root);
374 disk_super->discard_block_size = cpu_to_le64(cmd->discard_block_size);
Joe Thornber1bad9bc2014-11-07 14:47:07 +0000375 disk_super->discard_nr_blocks = cpu_to_le64(from_dblock(cmd->discard_nr_blocks));
Mike Snitzer895b47d2014-07-14 15:37:18 -0400376 disk_super->metadata_block_size = cpu_to_le32(DM_CACHE_METADATA_BLOCK_SIZE);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000377 disk_super->data_block_size = cpu_to_le32(cmd->data_block_size);
378 disk_super->cache_blocks = cpu_to_le32(0);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000379
380 disk_super->read_hits = cpu_to_le32(0);
381 disk_super->read_misses = cpu_to_le32(0);
382 disk_super->write_hits = cpu_to_le32(0);
383 disk_super->write_misses = cpu_to_le32(0);
384
Joe Thornber629d0a82016-09-22 06:15:21 -0400385 if (separate_dirty_bits(cmd))
386 disk_super->dirty_root = cpu_to_le64(cmd->dirty_root);
387
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000388 return dm_tm_commit(cmd->tm, sblock);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000389}
390
391static int __format_metadata(struct dm_cache_metadata *cmd)
392{
393 int r;
394
395 r = dm_tm_create_with_sm(cmd->bm, CACHE_SUPERBLOCK_LOCATION,
396 &cmd->tm, &cmd->metadata_sm);
397 if (r < 0) {
398 DMERR("tm_create_with_sm failed");
399 return r;
400 }
401
402 __setup_mapping_info(cmd);
403
404 r = dm_array_empty(&cmd->info, &cmd->root);
405 if (r < 0)
406 goto bad;
407
Joe Thornber629d0a82016-09-22 06:15:21 -0400408 if (separate_dirty_bits(cmd)) {
409 dm_disk_bitset_init(cmd->tm, &cmd->dirty_info);
410 r = dm_bitset_empty(&cmd->dirty_info, &cmd->dirty_root);
411 if (r < 0)
412 goto bad;
413 }
414
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000415 dm_disk_bitset_init(cmd->tm, &cmd->discard_info);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000416 r = dm_bitset_empty(&cmd->discard_info, &cmd->discard_root);
417 if (r < 0)
418 goto bad;
419
420 cmd->discard_block_size = 0;
421 cmd->discard_nr_blocks = 0;
422
423 r = __write_initial_superblock(cmd);
424 if (r)
425 goto bad;
426
427 cmd->clean_when_opened = true;
428 return 0;
429
430bad:
431 dm_tm_destroy(cmd->tm);
432 dm_sm_destroy(cmd->metadata_sm);
433
434 return r;
435}
436
437static int __check_incompat_features(struct cache_disk_superblock *disk_super,
438 struct dm_cache_metadata *cmd)
439{
Joe Thornber629d0a82016-09-22 06:15:21 -0400440 uint32_t incompat_flags, features;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000441
Joe Thornber629d0a82016-09-22 06:15:21 -0400442 incompat_flags = le32_to_cpu(disk_super->incompat_flags);
443 features = incompat_flags & ~DM_CACHE_FEATURE_INCOMPAT_SUPP;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000444 if (features) {
445 DMERR("could not access metadata due to unsupported optional features (%lx).",
446 (unsigned long)features);
447 return -EINVAL;
448 }
449
450 /*
451 * Check for read-only metadata to skip the following RDWR checks.
452 */
453 if (get_disk_ro(cmd->bdev->bd_disk))
454 return 0;
455
456 features = le32_to_cpu(disk_super->compat_ro_flags) & ~DM_CACHE_FEATURE_COMPAT_RO_SUPP;
457 if (features) {
458 DMERR("could not access metadata RDWR due to unsupported optional features (%lx).",
459 (unsigned long)features);
460 return -EINVAL;
461 }
462
463 return 0;
464}
465
466static int __open_metadata(struct dm_cache_metadata *cmd)
467{
468 int r;
469 struct dm_block *sblock;
470 struct cache_disk_superblock *disk_super;
471 unsigned long sb_flags;
472
473 r = superblock_read_lock(cmd, &sblock);
474 if (r < 0) {
475 DMERR("couldn't read lock superblock");
476 return r;
477 }
478
479 disk_super = dm_block_data(sblock);
480
Mike Snitzer048e5a02014-07-14 16:59:39 -0400481 /* Verify the data block size hasn't changed */
482 if (le32_to_cpu(disk_super->data_block_size) != cmd->data_block_size) {
483 DMERR("changing the data block size (from %u to %llu) is not supported",
484 le32_to_cpu(disk_super->data_block_size),
485 (unsigned long long)cmd->data_block_size);
486 r = -EINVAL;
487 goto bad;
488 }
489
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000490 r = __check_incompat_features(disk_super, cmd);
491 if (r < 0)
492 goto bad;
493
494 r = dm_tm_open_with_sm(cmd->bm, CACHE_SUPERBLOCK_LOCATION,
495 disk_super->metadata_space_map_root,
496 sizeof(disk_super->metadata_space_map_root),
497 &cmd->tm, &cmd->metadata_sm);
498 if (r < 0) {
499 DMERR("tm_open_with_sm failed");
500 goto bad;
501 }
502
503 __setup_mapping_info(cmd);
Joe Thornber629d0a82016-09-22 06:15:21 -0400504 dm_disk_bitset_init(cmd->tm, &cmd->dirty_info);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000505 dm_disk_bitset_init(cmd->tm, &cmd->discard_info);
506 sb_flags = le32_to_cpu(disk_super->flags);
507 cmd->clean_when_opened = test_bit(CLEAN_SHUTDOWN, &sb_flags);
Mikulas Patocka4c7da062015-10-22 16:46:59 -0400508 dm_bm_unlock(sblock);
509
510 return 0;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000511
512bad:
513 dm_bm_unlock(sblock);
514 return r;
515}
516
517static int __open_or_format_metadata(struct dm_cache_metadata *cmd,
518 bool format_device)
519{
Joe Thornberdd8b0c22013-10-24 14:10:28 -0400520 int r;
521 bool unformatted = false;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000522
523 r = __superblock_all_zeroes(cmd->bm, &unformatted);
524 if (r)
525 return r;
526
527 if (unformatted)
528 return format_device ? __format_metadata(cmd) : -EPERM;
529
530 return __open_metadata(cmd);
531}
532
533static int __create_persistent_data_objects(struct dm_cache_metadata *cmd,
534 bool may_format_device)
535{
536 int r;
Mike Snitzer895b47d2014-07-14 15:37:18 -0400537 cmd->bm = dm_block_manager_create(cmd->bdev, DM_CACHE_METADATA_BLOCK_SIZE << SECTOR_SHIFT,
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000538 CACHE_METADATA_CACHE_SIZE,
539 CACHE_MAX_CONCURRENT_LOCKS);
540 if (IS_ERR(cmd->bm)) {
541 DMERR("could not create block manager");
542 return PTR_ERR(cmd->bm);
543 }
544
545 r = __open_or_format_metadata(cmd, may_format_device);
546 if (r)
547 dm_block_manager_destroy(cmd->bm);
548
549 return r;
550}
551
552static void __destroy_persistent_data_objects(struct dm_cache_metadata *cmd)
553{
554 dm_sm_destroy(cmd->metadata_sm);
555 dm_tm_destroy(cmd->tm);
556 dm_block_manager_destroy(cmd->bm);
557}
558
559typedef unsigned long (*flags_mutator)(unsigned long);
560
561static void update_flags(struct cache_disk_superblock *disk_super,
562 flags_mutator mutator)
563{
564 uint32_t sb_flags = mutator(le32_to_cpu(disk_super->flags));
565 disk_super->flags = cpu_to_le32(sb_flags);
566}
567
568static unsigned long set_clean_shutdown(unsigned long flags)
569{
570 set_bit(CLEAN_SHUTDOWN, &flags);
571 return flags;
572}
573
574static unsigned long clear_clean_shutdown(unsigned long flags)
575{
576 clear_bit(CLEAN_SHUTDOWN, &flags);
577 return flags;
578}
579
580static void read_superblock_fields(struct dm_cache_metadata *cmd,
581 struct cache_disk_superblock *disk_super)
582{
Joe Thornber629d0a82016-09-22 06:15:21 -0400583 cmd->version = le32_to_cpu(disk_super->version);
Joe Thornber028ae9f2015-04-22 16:42:35 -0400584 cmd->flags = le32_to_cpu(disk_super->flags);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000585 cmd->root = le64_to_cpu(disk_super->mapping_root);
586 cmd->hint_root = le64_to_cpu(disk_super->hint_root);
587 cmd->discard_root = le64_to_cpu(disk_super->discard_root);
588 cmd->discard_block_size = le64_to_cpu(disk_super->discard_block_size);
Joe Thornber1bad9bc2014-11-07 14:47:07 +0000589 cmd->discard_nr_blocks = to_dblock(le64_to_cpu(disk_super->discard_nr_blocks));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000590 cmd->data_block_size = le32_to_cpu(disk_super->data_block_size);
591 cmd->cache_blocks = to_cblock(le32_to_cpu(disk_super->cache_blocks));
592 strncpy(cmd->policy_name, disk_super->policy_name, sizeof(cmd->policy_name));
Mike Snitzer4e7f5062013-03-20 17:21:27 +0000593 cmd->policy_version[0] = le32_to_cpu(disk_super->policy_version[0]);
594 cmd->policy_version[1] = le32_to_cpu(disk_super->policy_version[1]);
595 cmd->policy_version[2] = le32_to_cpu(disk_super->policy_version[2]);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000596 cmd->policy_hint_size = le32_to_cpu(disk_super->policy_hint_size);
597
598 cmd->stats.read_hits = le32_to_cpu(disk_super->read_hits);
599 cmd->stats.read_misses = le32_to_cpu(disk_super->read_misses);
600 cmd->stats.write_hits = le32_to_cpu(disk_super->write_hits);
601 cmd->stats.write_misses = le32_to_cpu(disk_super->write_misses);
602
Joe Thornber629d0a82016-09-22 06:15:21 -0400603 if (separate_dirty_bits(cmd))
604 cmd->dirty_root = le64_to_cpu(disk_super->dirty_root);
605
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000606 cmd->changed = false;
607}
608
609/*
610 * The mutator updates the superblock flags.
611 */
612static int __begin_transaction_flags(struct dm_cache_metadata *cmd,
613 flags_mutator mutator)
614{
615 int r;
616 struct cache_disk_superblock *disk_super;
617 struct dm_block *sblock;
618
619 r = superblock_lock(cmd, &sblock);
620 if (r)
621 return r;
622
623 disk_super = dm_block_data(sblock);
624 update_flags(disk_super, mutator);
625 read_superblock_fields(cmd, disk_super);
Joe Thornbera9d45392014-03-27 14:13:20 +0000626 dm_bm_unlock(sblock);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000627
Joe Thornbera9d45392014-03-27 14:13:20 +0000628 return dm_bm_flush(cmd->bm);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000629}
630
631static int __begin_transaction(struct dm_cache_metadata *cmd)
632{
633 int r;
634 struct cache_disk_superblock *disk_super;
635 struct dm_block *sblock;
636
637 /*
638 * We re-read the superblock every time. Shouldn't need to do this
639 * really.
640 */
641 r = superblock_read_lock(cmd, &sblock);
642 if (r)
643 return r;
644
645 disk_super = dm_block_data(sblock);
646 read_superblock_fields(cmd, disk_super);
647 dm_bm_unlock(sblock);
648
649 return 0;
650}
651
652static int __commit_transaction(struct dm_cache_metadata *cmd,
653 flags_mutator mutator)
654{
655 int r;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000656 struct cache_disk_superblock *disk_super;
657 struct dm_block *sblock;
658
659 /*
660 * We need to know if the cache_disk_superblock exceeds a 512-byte sector.
661 */
662 BUILD_BUG_ON(sizeof(struct cache_disk_superblock) > 512);
663
Joe Thornber629d0a82016-09-22 06:15:21 -0400664 if (separate_dirty_bits(cmd)) {
665 r = dm_bitset_flush(&cmd->dirty_info, cmd->dirty_root,
666 &cmd->dirty_root);
667 if (r)
668 return r;
669 }
670
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000671 r = dm_bitset_flush(&cmd->discard_info, cmd->discard_root,
672 &cmd->discard_root);
673 if (r)
674 return r;
675
676 r = dm_tm_pre_commit(cmd->tm);
677 if (r < 0)
678 return r;
679
Joe Thornber5a320832014-03-27 14:13:23 +0000680 r = __save_sm_root(cmd);
681 if (r)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000682 return r;
683
684 r = superblock_lock(cmd, &sblock);
685 if (r)
686 return r;
687
688 disk_super = dm_block_data(sblock);
689
Joe Thornber3201ac42015-10-22 18:10:55 +0100690 disk_super->flags = cpu_to_le32(cmd->flags);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000691 if (mutator)
692 update_flags(disk_super, mutator);
693
694 disk_super->mapping_root = cpu_to_le64(cmd->root);
Joe Thornber629d0a82016-09-22 06:15:21 -0400695 if (separate_dirty_bits(cmd))
696 disk_super->dirty_root = cpu_to_le64(cmd->dirty_root);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000697 disk_super->hint_root = cpu_to_le64(cmd->hint_root);
698 disk_super->discard_root = cpu_to_le64(cmd->discard_root);
699 disk_super->discard_block_size = cpu_to_le64(cmd->discard_block_size);
Joe Thornber1bad9bc2014-11-07 14:47:07 +0000700 disk_super->discard_nr_blocks = cpu_to_le64(from_dblock(cmd->discard_nr_blocks));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000701 disk_super->cache_blocks = cpu_to_le32(from_cblock(cmd->cache_blocks));
702 strncpy(disk_super->policy_name, cmd->policy_name, sizeof(disk_super->policy_name));
Mike Snitzer4e7f5062013-03-20 17:21:27 +0000703 disk_super->policy_version[0] = cpu_to_le32(cmd->policy_version[0]);
704 disk_super->policy_version[1] = cpu_to_le32(cmd->policy_version[1]);
705 disk_super->policy_version[2] = cpu_to_le32(cmd->policy_version[2]);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000706
707 disk_super->read_hits = cpu_to_le32(cmd->stats.read_hits);
708 disk_super->read_misses = cpu_to_le32(cmd->stats.read_misses);
709 disk_super->write_hits = cpu_to_le32(cmd->stats.write_hits);
710 disk_super->write_misses = cpu_to_le32(cmd->stats.write_misses);
Joe Thornber5a320832014-03-27 14:13:23 +0000711 __copy_sm_root(cmd, disk_super);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000712
713 return dm_tm_commit(cmd->tm, sblock);
714}
715
716/*----------------------------------------------------------------*/
717
718/*
719 * The mappings are held in a dm-array that has 64-bit values stored in
720 * little-endian format. The index is the cblock, the high 48bits of the
721 * value are the oblock and the low 16 bit the flags.
722 */
723#define FLAGS_MASK ((1 << 16) - 1)
724
725static __le64 pack_value(dm_oblock_t block, unsigned flags)
726{
727 uint64_t value = from_oblock(block);
728 value <<= 16;
729 value = value | (flags & FLAGS_MASK);
730 return cpu_to_le64(value);
731}
732
733static void unpack_value(__le64 value_le, dm_oblock_t *block, unsigned *flags)
734{
735 uint64_t value = le64_to_cpu(value_le);
736 uint64_t b = value >> 16;
737 *block = to_oblock(b);
738 *flags = value & FLAGS_MASK;
739}
740
741/*----------------------------------------------------------------*/
742
Joe Thornber9b1cc9f2015-01-23 10:00:07 +0000743static struct dm_cache_metadata *metadata_open(struct block_device *bdev,
744 sector_t data_block_size,
745 bool may_format_device,
Joe Thornber629d0a82016-09-22 06:15:21 -0400746 size_t policy_hint_size,
747 unsigned metadata_version)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000748{
749 int r;
750 struct dm_cache_metadata *cmd;
751
752 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
753 if (!cmd) {
754 DMERR("could not allocate metadata struct");
Joe Thornber766a7882015-01-28 12:07:46 +0000755 return ERR_PTR(-ENOMEM);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000756 }
757
Joe Thornber629d0a82016-09-22 06:15:21 -0400758 cmd->version = metadata_version;
Joe Thornber9b1cc9f2015-01-23 10:00:07 +0000759 atomic_set(&cmd->ref_count, 1);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000760 init_rwsem(&cmd->root_lock);
761 cmd->bdev = bdev;
762 cmd->data_block_size = data_block_size;
763 cmd->cache_blocks = 0;
764 cmd->policy_hint_size = policy_hint_size;
765 cmd->changed = true;
Joe Thornber028ae9f2015-04-22 16:42:35 -0400766 cmd->fail_io = false;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000767
768 r = __create_persistent_data_objects(cmd, may_format_device);
769 if (r) {
770 kfree(cmd);
771 return ERR_PTR(r);
772 }
773
774 r = __begin_transaction_flags(cmd, clear_clean_shutdown);
775 if (r < 0) {
776 dm_cache_metadata_close(cmd);
777 return ERR_PTR(r);
778 }
779
780 return cmd;
781}
782
Joe Thornber9b1cc9f2015-01-23 10:00:07 +0000783/*
784 * We keep a little list of ref counted metadata objects to prevent two
785 * different target instances creating separate bufio instances. This is
786 * an issue if a table is reloaded before the suspend.
787 */
788static DEFINE_MUTEX(table_lock);
789static LIST_HEAD(table);
790
791static struct dm_cache_metadata *lookup(struct block_device *bdev)
792{
793 struct dm_cache_metadata *cmd;
794
795 list_for_each_entry(cmd, &table, list)
796 if (cmd->bdev == bdev) {
797 atomic_inc(&cmd->ref_count);
798 return cmd;
799 }
800
801 return NULL;
802}
803
804static struct dm_cache_metadata *lookup_or_open(struct block_device *bdev,
805 sector_t data_block_size,
806 bool may_format_device,
Joe Thornber629d0a82016-09-22 06:15:21 -0400807 size_t policy_hint_size,
808 unsigned metadata_version)
Joe Thornber9b1cc9f2015-01-23 10:00:07 +0000809{
810 struct dm_cache_metadata *cmd, *cmd2;
811
812 mutex_lock(&table_lock);
813 cmd = lookup(bdev);
814 mutex_unlock(&table_lock);
815
816 if (cmd)
817 return cmd;
818
Joe Thornber629d0a82016-09-22 06:15:21 -0400819 cmd = metadata_open(bdev, data_block_size, may_format_device,
820 policy_hint_size, metadata_version);
Joe Thornber766a7882015-01-28 12:07:46 +0000821 if (!IS_ERR(cmd)) {
Joe Thornber9b1cc9f2015-01-23 10:00:07 +0000822 mutex_lock(&table_lock);
823 cmd2 = lookup(bdev);
824 if (cmd2) {
825 mutex_unlock(&table_lock);
826 __destroy_persistent_data_objects(cmd);
827 kfree(cmd);
828 return cmd2;
829 }
830 list_add(&cmd->list, &table);
831 mutex_unlock(&table_lock);
832 }
833
834 return cmd;
835}
836
837static bool same_params(struct dm_cache_metadata *cmd, sector_t data_block_size)
838{
839 if (cmd->data_block_size != data_block_size) {
Mike Snitzer07d93882016-10-03 11:56:22 -0400840 DMERR("data_block_size (%llu) different from that in metadata (%llu)",
Joe Thornber9b1cc9f2015-01-23 10:00:07 +0000841 (unsigned long long) data_block_size,
842 (unsigned long long) cmd->data_block_size);
843 return false;
844 }
845
846 return true;
847}
848
849struct dm_cache_metadata *dm_cache_metadata_open(struct block_device *bdev,
850 sector_t data_block_size,
851 bool may_format_device,
Joe Thornber629d0a82016-09-22 06:15:21 -0400852 size_t policy_hint_size,
853 unsigned metadata_version)
Joe Thornber9b1cc9f2015-01-23 10:00:07 +0000854{
Joe Thornber629d0a82016-09-22 06:15:21 -0400855 struct dm_cache_metadata *cmd = lookup_or_open(bdev, data_block_size, may_format_device,
856 policy_hint_size, metadata_version);
Joe Thornber766a7882015-01-28 12:07:46 +0000857
858 if (!IS_ERR(cmd) && !same_params(cmd, data_block_size)) {
Joe Thornber9b1cc9f2015-01-23 10:00:07 +0000859 dm_cache_metadata_close(cmd);
Joe Thornber766a7882015-01-28 12:07:46 +0000860 return ERR_PTR(-EINVAL);
Joe Thornber9b1cc9f2015-01-23 10:00:07 +0000861 }
862
863 return cmd;
864}
865
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000866void dm_cache_metadata_close(struct dm_cache_metadata *cmd)
867{
Joe Thornber9b1cc9f2015-01-23 10:00:07 +0000868 if (atomic_dec_and_test(&cmd->ref_count)) {
869 mutex_lock(&table_lock);
870 list_del(&cmd->list);
871 mutex_unlock(&table_lock);
872
Joe Thornber028ae9f2015-04-22 16:42:35 -0400873 if (!cmd->fail_io)
874 __destroy_persistent_data_objects(cmd);
Joe Thornber9b1cc9f2015-01-23 10:00:07 +0000875 kfree(cmd);
876 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000877}
878
Joe Thornberf494a9c2013-10-31 13:55:49 -0400879/*
880 * Checks that the given cache block is either unmapped or clean.
881 */
Joe Thornber629d0a82016-09-22 06:15:21 -0400882static int block_clean_combined_dirty(struct dm_cache_metadata *cmd, dm_cblock_t b,
883 bool *result)
Joe Thornberf494a9c2013-10-31 13:55:49 -0400884{
885 int r;
886 __le64 value;
887 dm_oblock_t ob;
888 unsigned flags;
889
890 r = dm_array_get_value(&cmd->info, cmd->root, from_cblock(b), &value);
Joe Thornber629d0a82016-09-22 06:15:21 -0400891 if (r)
Joe Thornberf494a9c2013-10-31 13:55:49 -0400892 return r;
Joe Thornberf494a9c2013-10-31 13:55:49 -0400893
894 unpack_value(value, &ob, &flags);
895 *result = !((flags & M_VALID) && (flags & M_DIRTY));
896
897 return 0;
898}
899
Joe Thornber629d0a82016-09-22 06:15:21 -0400900static int blocks_are_clean_combined_dirty(struct dm_cache_metadata *cmd,
901 dm_cblock_t begin, dm_cblock_t end,
902 bool *result)
Joe Thornberf494a9c2013-10-31 13:55:49 -0400903{
904 int r;
905 *result = true;
906
907 while (begin != end) {
Joe Thornber629d0a82016-09-22 06:15:21 -0400908 r = block_clean_combined_dirty(cmd, begin, result);
909 if (r) {
910 DMERR("block_clean_combined_dirty failed");
Joe Thornberf494a9c2013-10-31 13:55:49 -0400911 return r;
Joe Thornber629d0a82016-09-22 06:15:21 -0400912 }
Joe Thornberf494a9c2013-10-31 13:55:49 -0400913
914 if (!*result) {
915 DMERR("cache block %llu is dirty",
916 (unsigned long long) from_cblock(begin));
917 return 0;
918 }
919
920 begin = to_cblock(from_cblock(begin) + 1);
921 }
922
923 return 0;
924}
925
Joe Thornber629d0a82016-09-22 06:15:21 -0400926static int blocks_are_clean_separate_dirty(struct dm_cache_metadata *cmd,
927 dm_cblock_t begin, dm_cblock_t end,
928 bool *result)
929{
930 int r;
931 bool dirty_flag;
932 *result = true;
933
934 // FIXME: use a cursor so we can benefit from preloading metadata.
935 while (begin != end) {
936 /*
937 * We assume that unmapped blocks have their dirty bit
938 * cleared.
939 */
940 r = dm_bitset_test_bit(&cmd->dirty_info, cmd->dirty_root,
941 from_cblock(begin), &cmd->dirty_root, &dirty_flag);
942 if (r)
943 return r;
944
945 if (dirty_flag) {
946 DMERR("cache block %llu is dirty",
947 (unsigned long long) from_cblock(begin));
948 *result = false;
949 return 0;
950 }
951
952 begin = to_cblock(from_cblock(begin) + 1);
953 }
954
955 return 0;
956}
957
958static int blocks_are_unmapped_or_clean(struct dm_cache_metadata *cmd,
959 dm_cblock_t begin, dm_cblock_t end,
960 bool *result)
961{
962 if (separate_dirty_bits(cmd))
963 return blocks_are_clean_separate_dirty(cmd, begin, end, result);
964 else
965 return blocks_are_clean_combined_dirty(cmd, begin, end, result);
966}
967
Mike Snitzer95673662016-04-12 12:14:46 -0400968static bool cmd_write_lock(struct dm_cache_metadata *cmd)
969{
970 down_write(&cmd->root_lock);
971 if (cmd->fail_io || dm_bm_is_read_only(cmd->bm)) {
972 up_write(&cmd->root_lock);
973 return false;
Joe Thornberd14fcf32016-03-10 16:20:58 +0000974 }
Mike Snitzer95673662016-04-12 12:14:46 -0400975 return true;
976}
Joe Thornber028ae9f2015-04-22 16:42:35 -0400977
Mike Snitzer95673662016-04-12 12:14:46 -0400978#define WRITE_LOCK(cmd) \
979 do { \
980 if (!cmd_write_lock((cmd))) \
981 return -EINVAL; \
982 } while(0)
983
984#define WRITE_LOCK_VOID(cmd) \
985 do { \
986 if (!cmd_write_lock((cmd))) \
987 return; \
988 } while(0)
Joe Thornber028ae9f2015-04-22 16:42:35 -0400989
990#define WRITE_UNLOCK(cmd) \
Mike Snitzer95673662016-04-12 12:14:46 -0400991 up_write(&(cmd)->root_lock)
Joe Thornber028ae9f2015-04-22 16:42:35 -0400992
Mike Snitzer95673662016-04-12 12:14:46 -0400993static bool cmd_read_lock(struct dm_cache_metadata *cmd)
994{
Ahmed Samy6545b602016-04-17 05:37:09 +0000995 down_read(&cmd->root_lock);
Mike Snitzer95673662016-04-12 12:14:46 -0400996 if (cmd->fail_io) {
Ahmed Samy6545b602016-04-17 05:37:09 +0000997 up_read(&cmd->root_lock);
Mike Snitzer95673662016-04-12 12:14:46 -0400998 return false;
Joe Thornberd14fcf32016-03-10 16:20:58 +0000999 }
Mike Snitzer95673662016-04-12 12:14:46 -04001000 return true;
1001}
Joe Thornberd14fcf32016-03-10 16:20:58 +00001002
Mike Snitzer95673662016-04-12 12:14:46 -04001003#define READ_LOCK(cmd) \
1004 do { \
1005 if (!cmd_read_lock((cmd))) \
1006 return -EINVAL; \
1007 } while(0)
1008
1009#define READ_LOCK_VOID(cmd) \
1010 do { \
1011 if (!cmd_read_lock((cmd))) \
1012 return; \
1013 } while(0)
Joe Thornberd14fcf32016-03-10 16:20:58 +00001014
1015#define READ_UNLOCK(cmd) \
Mike Snitzer95673662016-04-12 12:14:46 -04001016 up_read(&(cmd)->root_lock)
Joe Thornberd14fcf32016-03-10 16:20:58 +00001017
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001018int dm_cache_resize(struct dm_cache_metadata *cmd, dm_cblock_t new_cache_size)
1019{
1020 int r;
Joe Thornberf494a9c2013-10-31 13:55:49 -04001021 bool clean;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001022 __le64 null_mapping = pack_value(0, 0);
1023
Joe Thornber028ae9f2015-04-22 16:42:35 -04001024 WRITE_LOCK(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001025 __dm_bless_for_disk(&null_mapping);
Joe Thornberf494a9c2013-10-31 13:55:49 -04001026
1027 if (from_cblock(new_cache_size) < from_cblock(cmd->cache_blocks)) {
1028 r = blocks_are_unmapped_or_clean(cmd, new_cache_size, cmd->cache_blocks, &clean);
1029 if (r) {
1030 __dm_unbless_for_disk(&null_mapping);
1031 goto out;
1032 }
1033
1034 if (!clean) {
1035 DMERR("unable to shrink cache due to dirty blocks");
1036 r = -EINVAL;
1037 __dm_unbless_for_disk(&null_mapping);
1038 goto out;
1039 }
1040 }
1041
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001042 r = dm_array_resize(&cmd->info, cmd->root, from_cblock(cmd->cache_blocks),
1043 from_cblock(new_cache_size),
1044 &null_mapping, &cmd->root);
Joe Thornber629d0a82016-09-22 06:15:21 -04001045 if (r)
1046 goto out;
1047
1048 if (separate_dirty_bits(cmd)) {
1049 r = dm_bitset_resize(&cmd->dirty_info, cmd->dirty_root,
1050 from_cblock(cmd->cache_blocks), from_cblock(new_cache_size),
1051 false, &cmd->dirty_root);
1052 if (r)
1053 goto out;
1054 }
1055
1056 cmd->cache_blocks = new_cache_size;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001057 cmd->changed = true;
Joe Thornberf494a9c2013-10-31 13:55:49 -04001058
1059out:
Joe Thornber028ae9f2015-04-22 16:42:35 -04001060 WRITE_UNLOCK(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001061
1062 return r;
1063}
1064
1065int dm_cache_discard_bitset_resize(struct dm_cache_metadata *cmd,
1066 sector_t discard_block_size,
Joe Thornber1bad9bc2014-11-07 14:47:07 +00001067 dm_dblock_t new_nr_entries)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001068{
1069 int r;
1070
Joe Thornber028ae9f2015-04-22 16:42:35 -04001071 WRITE_LOCK(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001072 r = dm_bitset_resize(&cmd->discard_info,
1073 cmd->discard_root,
Joe Thornber1bad9bc2014-11-07 14:47:07 +00001074 from_dblock(cmd->discard_nr_blocks),
1075 from_dblock(new_nr_entries),
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001076 false, &cmd->discard_root);
1077 if (!r) {
1078 cmd->discard_block_size = discard_block_size;
1079 cmd->discard_nr_blocks = new_nr_entries;
1080 }
1081
1082 cmd->changed = true;
Joe Thornber028ae9f2015-04-22 16:42:35 -04001083 WRITE_UNLOCK(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001084
1085 return r;
1086}
1087
Joe Thornber1bad9bc2014-11-07 14:47:07 +00001088static int __set_discard(struct dm_cache_metadata *cmd, dm_dblock_t b)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001089{
1090 return dm_bitset_set_bit(&cmd->discard_info, cmd->discard_root,
Joe Thornber1bad9bc2014-11-07 14:47:07 +00001091 from_dblock(b), &cmd->discard_root);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001092}
1093
Joe Thornber1bad9bc2014-11-07 14:47:07 +00001094static int __clear_discard(struct dm_cache_metadata *cmd, dm_dblock_t b)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001095{
1096 return dm_bitset_clear_bit(&cmd->discard_info, cmd->discard_root,
Joe Thornber1bad9bc2014-11-07 14:47:07 +00001097 from_dblock(b), &cmd->discard_root);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001098}
1099
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001100static int __discard(struct dm_cache_metadata *cmd,
Joe Thornber1bad9bc2014-11-07 14:47:07 +00001101 dm_dblock_t dblock, bool discard)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001102{
1103 int r;
1104
1105 r = (discard ? __set_discard : __clear_discard)(cmd, dblock);
1106 if (r)
1107 return r;
1108
1109 cmd->changed = true;
1110 return 0;
1111}
1112
1113int dm_cache_set_discard(struct dm_cache_metadata *cmd,
Joe Thornber1bad9bc2014-11-07 14:47:07 +00001114 dm_dblock_t dblock, bool discard)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001115{
1116 int r;
1117
Joe Thornber028ae9f2015-04-22 16:42:35 -04001118 WRITE_LOCK(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001119 r = __discard(cmd, dblock, discard);
Joe Thornber028ae9f2015-04-22 16:42:35 -04001120 WRITE_UNLOCK(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001121
1122 return r;
1123}
1124
1125static int __load_discards(struct dm_cache_metadata *cmd,
1126 load_discard_fn fn, void *context)
1127{
1128 int r = 0;
Joe Thornberae4a46a2016-10-03 14:16:20 -04001129 uint32_t b;
1130 struct dm_bitset_cursor c;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001131
Joe Thornberae4a46a2016-10-03 14:16:20 -04001132 if (from_dblock(cmd->discard_nr_blocks) == 0)
1133 /* nothing to do */
1134 return 0;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001135
Joe Thornberae4a46a2016-10-03 14:16:20 -04001136 if (cmd->clean_when_opened) {
1137 r = dm_bitset_flush(&cmd->discard_info, cmd->discard_root, &cmd->discard_root);
1138 if (r)
1139 return r;
1140
1141 r = dm_bitset_cursor_begin(&cmd->discard_info, cmd->discard_root,
1142 from_dblock(cmd->discard_nr_blocks), &c);
1143 if (r)
1144 return r;
1145
1146 for (b = 0; b < from_dblock(cmd->discard_nr_blocks); b++) {
1147 r = fn(context, cmd->discard_block_size, to_dblock(b),
1148 dm_bitset_cursor_get_value(&c));
1149 if (r)
1150 break;
1151 }
1152
1153 dm_bitset_cursor_end(&c);
1154
1155 } else {
1156 for (b = 0; b < from_dblock(cmd->discard_nr_blocks); b++) {
1157 r = fn(context, cmd->discard_block_size, to_dblock(b), false);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001158 if (r)
1159 return r;
Joe Thornberae4a46a2016-10-03 14:16:20 -04001160 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001161 }
1162
1163 return r;
1164}
1165
1166int dm_cache_load_discards(struct dm_cache_metadata *cmd,
1167 load_discard_fn fn, void *context)
1168{
1169 int r;
1170
Joe Thornberd14fcf32016-03-10 16:20:58 +00001171 READ_LOCK(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001172 r = __load_discards(cmd, fn, context);
Joe Thornberd14fcf32016-03-10 16:20:58 +00001173 READ_UNLOCK(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001174
1175 return r;
1176}
1177
Joe Thornberd14fcf32016-03-10 16:20:58 +00001178int dm_cache_size(struct dm_cache_metadata *cmd, dm_cblock_t *result)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001179{
Joe Thornberd14fcf32016-03-10 16:20:58 +00001180 READ_LOCK(cmd);
1181 *result = cmd->cache_blocks;
1182 READ_UNLOCK(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001183
Joe Thornberd14fcf32016-03-10 16:20:58 +00001184 return 0;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001185}
1186
1187static int __remove(struct dm_cache_metadata *cmd, dm_cblock_t cblock)
1188{
1189 int r;
1190 __le64 value = pack_value(0, 0);
1191
1192 __dm_bless_for_disk(&value);
1193 r = dm_array_set_value(&cmd->info, cmd->root, from_cblock(cblock),
1194 &value, &cmd->root);
1195 if (r)
1196 return r;
1197
1198 cmd->changed = true;
1199 return 0;
1200}
1201
1202int dm_cache_remove_mapping(struct dm_cache_metadata *cmd, dm_cblock_t cblock)
1203{
1204 int r;
1205
Joe Thornber028ae9f2015-04-22 16:42:35 -04001206 WRITE_LOCK(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001207 r = __remove(cmd, cblock);
Joe Thornber028ae9f2015-04-22 16:42:35 -04001208 WRITE_UNLOCK(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001209
1210 return r;
1211}
1212
1213static int __insert(struct dm_cache_metadata *cmd,
1214 dm_cblock_t cblock, dm_oblock_t oblock)
1215{
1216 int r;
1217 __le64 value = pack_value(oblock, M_VALID);
1218 __dm_bless_for_disk(&value);
1219
1220 r = dm_array_set_value(&cmd->info, cmd->root, from_cblock(cblock),
1221 &value, &cmd->root);
1222 if (r)
1223 return r;
1224
1225 cmd->changed = true;
1226 return 0;
1227}
1228
1229int dm_cache_insert_mapping(struct dm_cache_metadata *cmd,
1230 dm_cblock_t cblock, dm_oblock_t oblock)
1231{
1232 int r;
1233
Joe Thornber028ae9f2015-04-22 16:42:35 -04001234 WRITE_LOCK(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001235 r = __insert(cmd, cblock, oblock);
Joe Thornber028ae9f2015-04-22 16:42:35 -04001236 WRITE_UNLOCK(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001237
1238 return r;
1239}
1240
1241struct thunk {
1242 load_mapping_fn fn;
1243 void *context;
1244
1245 struct dm_cache_metadata *cmd;
1246 bool respect_dirty_flags;
1247 bool hints_valid;
1248};
1249
Mike Snitzerea2dd8c2013-03-20 17:21:28 +00001250static bool policy_unchanged(struct dm_cache_metadata *cmd,
1251 struct dm_cache_policy *policy)
1252{
1253 const char *policy_name = dm_cache_policy_get_name(policy);
1254 const unsigned *policy_version = dm_cache_policy_get_version(policy);
1255 size_t policy_hint_size = dm_cache_policy_get_hint_size(policy);
1256
1257 /*
1258 * Ensure policy names match.
1259 */
1260 if (strncmp(cmd->policy_name, policy_name, sizeof(cmd->policy_name)))
1261 return false;
1262
1263 /*
1264 * Ensure policy major versions match.
1265 */
1266 if (cmd->policy_version[0] != policy_version[0])
1267 return false;
1268
1269 /*
1270 * Ensure policy hint sizes match.
1271 */
1272 if (cmd->policy_hint_size != policy_hint_size)
1273 return false;
1274
1275 return true;
1276}
1277
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001278static bool hints_array_initialized(struct dm_cache_metadata *cmd)
1279{
1280 return cmd->hint_root && cmd->policy_hint_size;
1281}
1282
1283static bool hints_array_available(struct dm_cache_metadata *cmd,
Mike Snitzerea2dd8c2013-03-20 17:21:28 +00001284 struct dm_cache_policy *policy)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001285{
Mike Snitzerea2dd8c2013-03-20 17:21:28 +00001286 return cmd->clean_when_opened && policy_unchanged(cmd, policy) &&
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001287 hints_array_initialized(cmd);
1288}
1289
Joe Thornber629d0a82016-09-22 06:15:21 -04001290static int __load_mapping_v1(struct dm_cache_metadata *cmd,
1291 uint64_t cb, bool hints_valid,
1292 struct dm_array_cursor *mapping_cursor,
1293 struct dm_array_cursor *hint_cursor,
1294 load_mapping_fn fn, void *context)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001295{
1296 int r = 0;
Joe Thornberf1779402016-09-20 06:16:00 -04001297
1298 __le64 mapping;
1299 __le32 hint = 0;
1300
1301 __le64 *mapping_value_le;
1302 __le32 *hint_value_le;
1303
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001304 dm_oblock_t oblock;
1305 unsigned flags;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001306
Joe Thornberf1779402016-09-20 06:16:00 -04001307 dm_array_cursor_get_value(mapping_cursor, (void **) &mapping_value_le);
1308 memcpy(&mapping, mapping_value_le, sizeof(mapping));
1309 unpack_value(mapping, &oblock, &flags);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001310
1311 if (flags & M_VALID) {
Joe Thornberf1779402016-09-20 06:16:00 -04001312 if (hints_valid) {
1313 dm_array_cursor_get_value(hint_cursor, (void **) &hint_value_le);
1314 memcpy(&hint, hint_value_le, sizeof(hint));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001315 }
1316
Joe Thornberf1779402016-09-20 06:16:00 -04001317 r = fn(context, oblock, to_cblock(cb), flags & M_DIRTY,
1318 le32_to_cpu(hint), hints_valid);
Mike Snitzer48551052016-10-04 15:22:17 -04001319 if (r) {
1320 DMERR("policy couldn't load cache block %llu",
1321 (unsigned long long) from_cblock(to_cblock(cb)));
1322 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001323 }
1324
1325 return r;
1326}
1327
Joe Thornber629d0a82016-09-22 06:15:21 -04001328static int __load_mapping_v2(struct dm_cache_metadata *cmd,
1329 uint64_t cb, bool hints_valid,
1330 struct dm_array_cursor *mapping_cursor,
1331 struct dm_array_cursor *hint_cursor,
1332 struct dm_bitset_cursor *dirty_cursor,
1333 load_mapping_fn fn, void *context)
1334{
1335 int r = 0;
1336
1337 __le64 mapping;
1338 __le32 hint = 0;
1339
1340 __le64 *mapping_value_le;
1341 __le32 *hint_value_le;
1342
1343 dm_oblock_t oblock;
1344 unsigned flags;
1345 bool dirty;
1346
1347 dm_array_cursor_get_value(mapping_cursor, (void **) &mapping_value_le);
1348 memcpy(&mapping, mapping_value_le, sizeof(mapping));
1349 unpack_value(mapping, &oblock, &flags);
1350
1351 if (flags & M_VALID) {
1352 if (hints_valid) {
1353 dm_array_cursor_get_value(hint_cursor, (void **) &hint_value_le);
1354 memcpy(&hint, hint_value_le, sizeof(hint));
1355 }
1356
1357 dirty = dm_bitset_cursor_get_value(dirty_cursor);
1358 r = fn(context, oblock, to_cblock(cb), dirty,
1359 le32_to_cpu(hint), hints_valid);
Mike Snitzer48551052016-10-04 15:22:17 -04001360 if (r) {
1361 DMERR("policy couldn't load cache block %llu",
1362 (unsigned long long) from_cblock(to_cblock(cb)));
1363 }
Joe Thornber629d0a82016-09-22 06:15:21 -04001364 }
1365
1366 return r;
1367}
1368
Mike Snitzerea2dd8c2013-03-20 17:21:28 +00001369static int __load_mappings(struct dm_cache_metadata *cmd,
1370 struct dm_cache_policy *policy,
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001371 load_mapping_fn fn, void *context)
1372{
Joe Thornberf1779402016-09-20 06:16:00 -04001373 int r;
1374 uint64_t cb;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001375
Joe Thornberf1779402016-09-20 06:16:00 -04001376 bool hints_valid = hints_array_available(cmd, policy);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001377
Joe Thornberf1779402016-09-20 06:16:00 -04001378 if (from_cblock(cmd->cache_blocks) == 0)
1379 /* Nothing to do */
1380 return 0;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001381
Joe Thornberf1779402016-09-20 06:16:00 -04001382 r = dm_array_cursor_begin(&cmd->info, cmd->root, &cmd->mapping_cursor);
1383 if (r)
1384 return r;
1385
1386 if (hints_valid) {
1387 r = dm_array_cursor_begin(&cmd->hint_info, cmd->hint_root, &cmd->hint_cursor);
1388 if (r) {
1389 dm_array_cursor_end(&cmd->mapping_cursor);
1390 return r;
1391 }
1392 }
1393
Joe Thornber629d0a82016-09-22 06:15:21 -04001394 if (separate_dirty_bits(cmd)) {
1395 r = dm_bitset_cursor_begin(&cmd->dirty_info, cmd->dirty_root,
1396 from_cblock(cmd->cache_blocks),
1397 &cmd->dirty_cursor);
1398 if (r) {
1399 dm_array_cursor_end(&cmd->hint_cursor);
1400 dm_array_cursor_end(&cmd->mapping_cursor);
1401 return r;
1402 }
1403 }
1404
Joe Thornberf1779402016-09-20 06:16:00 -04001405 for (cb = 0; ; cb++) {
Joe Thornber629d0a82016-09-22 06:15:21 -04001406 if (separate_dirty_bits(cmd))
1407 r = __load_mapping_v2(cmd, cb, hints_valid,
1408 &cmd->mapping_cursor,
1409 &cmd->hint_cursor,
1410 &cmd->dirty_cursor,
1411 fn, context);
1412 else
1413 r = __load_mapping_v1(cmd, cb, hints_valid,
1414 &cmd->mapping_cursor, &cmd->hint_cursor,
1415 fn, context);
Joe Thornberf1779402016-09-20 06:16:00 -04001416 if (r)
1417 goto out;
1418
1419 /*
1420 * We need to break out before we move the cursors.
1421 */
1422 if (cb >= (from_cblock(cmd->cache_blocks) - 1))
1423 break;
1424
1425 r = dm_array_cursor_next(&cmd->mapping_cursor);
1426 if (r) {
1427 DMERR("dm_array_cursor_next for mapping failed");
1428 goto out;
1429 }
1430
1431 if (hints_valid) {
1432 r = dm_array_cursor_next(&cmd->hint_cursor);
1433 if (r) {
1434 DMERR("dm_array_cursor_next for hint failed");
1435 goto out;
1436 }
1437 }
Joe Thornber629d0a82016-09-22 06:15:21 -04001438
1439 if (separate_dirty_bits(cmd)) {
1440 r = dm_bitset_cursor_next(&cmd->dirty_cursor);
1441 if (r) {
1442 DMERR("dm_bitset_cursor_next for dirty failed");
1443 goto out;
1444 }
1445 }
Joe Thornberf1779402016-09-20 06:16:00 -04001446 }
1447out:
1448 dm_array_cursor_end(&cmd->mapping_cursor);
1449 if (hints_valid)
1450 dm_array_cursor_end(&cmd->hint_cursor);
1451
Joe Thornber629d0a82016-09-22 06:15:21 -04001452 if (separate_dirty_bits(cmd))
1453 dm_bitset_cursor_end(&cmd->dirty_cursor);
1454
Joe Thornberf1779402016-09-20 06:16:00 -04001455 return r;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001456}
1457
Mike Snitzerea2dd8c2013-03-20 17:21:28 +00001458int dm_cache_load_mappings(struct dm_cache_metadata *cmd,
1459 struct dm_cache_policy *policy,
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001460 load_mapping_fn fn, void *context)
1461{
1462 int r;
1463
Joe Thornberd14fcf32016-03-10 16:20:58 +00001464 READ_LOCK(cmd);
Mike Snitzerea2dd8c2013-03-20 17:21:28 +00001465 r = __load_mappings(cmd, policy, fn, context);
Joe Thornberd14fcf32016-03-10 16:20:58 +00001466 READ_UNLOCK(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001467
1468 return r;
1469}
1470
1471static int __dump_mapping(void *context, uint64_t cblock, void *leaf)
1472{
1473 int r = 0;
1474 __le64 value;
1475 dm_oblock_t oblock;
1476 unsigned flags;
1477
1478 memcpy(&value, leaf, sizeof(value));
1479 unpack_value(value, &oblock, &flags);
1480
1481 return r;
1482}
1483
1484static int __dump_mappings(struct dm_cache_metadata *cmd)
1485{
1486 return dm_array_walk(&cmd->info, cmd->root, __dump_mapping, NULL);
1487}
1488
1489void dm_cache_dump(struct dm_cache_metadata *cmd)
1490{
Joe Thornberd14fcf32016-03-10 16:20:58 +00001491 READ_LOCK_VOID(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001492 __dump_mappings(cmd);
Joe Thornberd14fcf32016-03-10 16:20:58 +00001493 READ_UNLOCK(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001494}
1495
1496int dm_cache_changed_this_transaction(struct dm_cache_metadata *cmd)
1497{
1498 int r;
1499
Joe Thornberd14fcf32016-03-10 16:20:58 +00001500 READ_LOCK(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001501 r = cmd->changed;
Joe Thornberd14fcf32016-03-10 16:20:58 +00001502 READ_UNLOCK(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001503
1504 return r;
1505}
1506
1507static int __dirty(struct dm_cache_metadata *cmd, dm_cblock_t cblock, bool dirty)
1508{
1509 int r;
1510 unsigned flags;
1511 dm_oblock_t oblock;
1512 __le64 value;
1513
1514 r = dm_array_get_value(&cmd->info, cmd->root, from_cblock(cblock), &value);
1515 if (r)
1516 return r;
1517
1518 unpack_value(value, &oblock, &flags);
1519
1520 if (((flags & M_DIRTY) && dirty) || (!(flags & M_DIRTY) && !dirty))
1521 /* nothing to be done */
1522 return 0;
1523
Joe Thornber79ed9ca2013-03-20 17:21:27 +00001524 value = pack_value(oblock, (flags & ~M_DIRTY) | (dirty ? M_DIRTY : 0));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001525 __dm_bless_for_disk(&value);
1526
1527 r = dm_array_set_value(&cmd->info, cmd->root, from_cblock(cblock),
1528 &value, &cmd->root);
1529 if (r)
1530 return r;
1531
1532 cmd->changed = true;
1533 return 0;
1534
1535}
1536
Joe Thornber629d0a82016-09-22 06:15:21 -04001537static int __set_dirty_bits_v1(struct dm_cache_metadata *cmd, unsigned nr_bits, unsigned long *bits)
1538{
1539 int r;
1540 unsigned i;
1541 for (i = 0; i < nr_bits; i++) {
1542 r = __dirty(cmd, to_cblock(i), test_bit(i, bits));
1543 if (r)
1544 return r;
1545 }
1546
1547 return 0;
1548}
1549
1550static int __set_dirty_bits_v2(struct dm_cache_metadata *cmd, unsigned nr_bits, unsigned long *bits)
1551{
1552 int r = 0;
1553 unsigned i;
1554
1555 /* nr_bits is really just a sanity check */
1556 if (nr_bits != from_cblock(cmd->cache_blocks)) {
1557 DMERR("dirty bitset is wrong size");
1558 return -EINVAL;
1559 }
1560
1561 for (i = 0; i < nr_bits; i++) {
1562 if (test_bit(i, bits))
1563 r = dm_bitset_set_bit(&cmd->dirty_info, cmd->dirty_root, i, &cmd->dirty_root);
1564 else
1565 r = dm_bitset_clear_bit(&cmd->dirty_info, cmd->dirty_root, i, &cmd->dirty_root);
1566
1567 if (r)
1568 return r;
1569 }
1570
1571 cmd->changed = true;
1572 return dm_bitset_flush(&cmd->dirty_info, cmd->dirty_root, &cmd->dirty_root);
1573}
1574
1575int dm_cache_set_dirty_bits(struct dm_cache_metadata *cmd,
1576 unsigned nr_bits,
1577 unsigned long *bits)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001578{
1579 int r;
1580
Joe Thornber028ae9f2015-04-22 16:42:35 -04001581 WRITE_LOCK(cmd);
Joe Thornber629d0a82016-09-22 06:15:21 -04001582 if (separate_dirty_bits(cmd))
1583 r = __set_dirty_bits_v2(cmd, nr_bits, bits);
1584 else
1585 r = __set_dirty_bits_v1(cmd, nr_bits, bits);
Joe Thornber028ae9f2015-04-22 16:42:35 -04001586 WRITE_UNLOCK(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001587
1588 return r;
1589}
1590
1591void dm_cache_metadata_get_stats(struct dm_cache_metadata *cmd,
1592 struct dm_cache_statistics *stats)
1593{
Joe Thornberd14fcf32016-03-10 16:20:58 +00001594 READ_LOCK_VOID(cmd);
Joe Thornber8c5008f2013-05-10 14:37:18 +01001595 *stats = cmd->stats;
Joe Thornberd14fcf32016-03-10 16:20:58 +00001596 READ_UNLOCK(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001597}
1598
1599void dm_cache_metadata_set_stats(struct dm_cache_metadata *cmd,
1600 struct dm_cache_statistics *stats)
1601{
Joe Thornber028ae9f2015-04-22 16:42:35 -04001602 WRITE_LOCK_VOID(cmd);
Joe Thornber8c5008f2013-05-10 14:37:18 +01001603 cmd->stats = *stats;
Joe Thornber028ae9f2015-04-22 16:42:35 -04001604 WRITE_UNLOCK(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001605}
1606
1607int dm_cache_commit(struct dm_cache_metadata *cmd, bool clean_shutdown)
1608{
1609 int r;
1610 flags_mutator mutator = (clean_shutdown ? set_clean_shutdown :
1611 clear_clean_shutdown);
1612
Joe Thornber028ae9f2015-04-22 16:42:35 -04001613 WRITE_LOCK(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001614 r = __commit_transaction(cmd, mutator);
1615 if (r)
1616 goto out;
1617
1618 r = __begin_transaction(cmd);
1619
1620out:
Joe Thornber028ae9f2015-04-22 16:42:35 -04001621 WRITE_UNLOCK(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001622 return r;
1623}
1624
1625int dm_cache_get_free_metadata_block_count(struct dm_cache_metadata *cmd,
1626 dm_block_t *result)
1627{
1628 int r = -EINVAL;
1629
Joe Thornberd14fcf32016-03-10 16:20:58 +00001630 READ_LOCK(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001631 r = dm_sm_get_nr_free(cmd->metadata_sm, result);
Joe Thornberd14fcf32016-03-10 16:20:58 +00001632 READ_UNLOCK(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001633
1634 return r;
1635}
1636
1637int dm_cache_get_metadata_dev_size(struct dm_cache_metadata *cmd,
1638 dm_block_t *result)
1639{
1640 int r = -EINVAL;
1641
Joe Thornberd14fcf32016-03-10 16:20:58 +00001642 READ_LOCK(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001643 r = dm_sm_get_nr_blocks(cmd->metadata_sm, result);
Joe Thornberd14fcf32016-03-10 16:20:58 +00001644 READ_UNLOCK(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001645
1646 return r;
1647}
1648
1649/*----------------------------------------------------------------*/
1650
Joe Thornber4e781b42016-09-15 09:23:46 -04001651static int get_hint(uint32_t index, void *value_le, void *context)
1652{
1653 uint32_t value;
1654 struct dm_cache_policy *policy = context;
1655
1656 value = policy_get_hint(policy, to_cblock(index));
1657 *((__le32 *) value_le) = cpu_to_le32(value);
1658
1659 return 0;
1660}
1661
1662/*
1663 * It's quicker to always delete the hint array, and recreate with
1664 * dm_array_new().
1665 */
1666static int write_hints(struct dm_cache_metadata *cmd, struct dm_cache_policy *policy)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001667{
1668 int r;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001669 size_t hint_size;
1670 const char *policy_name = dm_cache_policy_get_name(policy);
Mike Snitzer4e7f5062013-03-20 17:21:27 +00001671 const unsigned *policy_version = dm_cache_policy_get_version(policy);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001672
1673 if (!policy_name[0] ||
1674 (strlen(policy_name) > sizeof(cmd->policy_name) - 1))
1675 return -EINVAL;
1676
Joe Thornber4e781b42016-09-15 09:23:46 -04001677 strncpy(cmd->policy_name, policy_name, sizeof(cmd->policy_name));
1678 memcpy(cmd->policy_version, policy_version, sizeof(cmd->policy_version));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001679
Joe Thornber4e781b42016-09-15 09:23:46 -04001680 hint_size = dm_cache_policy_get_hint_size(policy);
1681 if (!hint_size)
1682 return 0; /* short-circuit hints initialization */
1683 cmd->policy_hint_size = hint_size;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001684
Joe Thornber4e781b42016-09-15 09:23:46 -04001685 if (cmd->hint_root) {
1686 r = dm_array_del(&cmd->hint_info, cmd->hint_root);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001687 if (r)
1688 return r;
1689 }
1690
Joe Thornber4e781b42016-09-15 09:23:46 -04001691 return dm_array_new(&cmd->hint_info, &cmd->hint_root,
1692 from_cblock(cmd->cache_blocks),
1693 get_hint, policy);
Joe Thornber05966612014-04-03 16:16:44 +01001694}
1695
1696int dm_cache_write_hints(struct dm_cache_metadata *cmd, struct dm_cache_policy *policy)
1697{
1698 int r;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001699
Joe Thornber028ae9f2015-04-22 16:42:35 -04001700 WRITE_LOCK(cmd);
Joe Thornber05966612014-04-03 16:16:44 +01001701 r = write_hints(cmd, policy);
Joe Thornber028ae9f2015-04-22 16:42:35 -04001702 WRITE_UNLOCK(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001703
1704 return r;
1705}
Joe Thornber2ee57d52013-10-24 14:10:29 -04001706
1707int dm_cache_metadata_all_clean(struct dm_cache_metadata *cmd, bool *result)
1708{
Joe Thornberd14fcf32016-03-10 16:20:58 +00001709 int r;
1710
1711 READ_LOCK(cmd);
1712 r = blocks_are_unmapped_or_clean(cmd, 0, cmd->cache_blocks, result);
1713 READ_UNLOCK(cmd);
1714
1715 return r;
Joe Thornber2ee57d52013-10-24 14:10:29 -04001716}
Joe Thornber028ae9f2015-04-22 16:42:35 -04001717
1718void dm_cache_metadata_set_read_only(struct dm_cache_metadata *cmd)
1719{
1720 WRITE_LOCK_VOID(cmd);
1721 dm_bm_set_read_only(cmd->bm);
1722 WRITE_UNLOCK(cmd);
1723}
1724
1725void dm_cache_metadata_set_read_write(struct dm_cache_metadata *cmd)
1726{
1727 WRITE_LOCK_VOID(cmd);
1728 dm_bm_set_read_write(cmd->bm);
1729 WRITE_UNLOCK(cmd);
1730}
1731
1732int dm_cache_metadata_set_needs_check(struct dm_cache_metadata *cmd)
1733{
1734 int r;
1735 struct dm_block *sblock;
1736 struct cache_disk_superblock *disk_super;
1737
Joe Thornberd14fcf32016-03-10 16:20:58 +00001738 WRITE_LOCK(cmd);
Joe Thornber028ae9f2015-04-22 16:42:35 -04001739 set_bit(NEEDS_CHECK, &cmd->flags);
1740
1741 r = superblock_lock(cmd, &sblock);
1742 if (r) {
1743 DMERR("couldn't read superblock");
1744 goto out;
1745 }
1746
1747 disk_super = dm_block_data(sblock);
1748 disk_super->flags = cpu_to_le32(cmd->flags);
1749
1750 dm_bm_unlock(sblock);
1751
1752out:
Joe Thornberd14fcf32016-03-10 16:20:58 +00001753 WRITE_UNLOCK(cmd);
Joe Thornber028ae9f2015-04-22 16:42:35 -04001754 return r;
1755}
1756
Joe Thornberd14fcf32016-03-10 16:20:58 +00001757int dm_cache_metadata_needs_check(struct dm_cache_metadata *cmd, bool *result)
Joe Thornber028ae9f2015-04-22 16:42:35 -04001758{
Joe Thornberd14fcf32016-03-10 16:20:58 +00001759 READ_LOCK(cmd);
1760 *result = !!test_bit(NEEDS_CHECK, &cmd->flags);
1761 READ_UNLOCK(cmd);
Joe Thornber028ae9f2015-04-22 16:42:35 -04001762
Joe Thornberd14fcf32016-03-10 16:20:58 +00001763 return 0;
Joe Thornber028ae9f2015-04-22 16:42:35 -04001764}
1765
1766int dm_cache_metadata_abort(struct dm_cache_metadata *cmd)
1767{
1768 int r;
1769
1770 WRITE_LOCK(cmd);
1771 __destroy_persistent_data_objects(cmd);
1772 r = __create_persistent_data_objects(cmd, false);
1773 if (r)
1774 cmd->fail_io = true;
1775 WRITE_UNLOCK(cmd);
1776
1777 return r;
1778}