blob: 0d7212410e2156addae298dfdc5e5a0ba4cbf690 [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>
Elena Reshetova6bdd0792017-10-20 10:37:37 +030016#include <linux/refcount.h>
Joe Thornberc6b4fcb2013-03-01 22:45:51 +000017
18/*----------------------------------------------------------------*/
19
20#define DM_MSG_PREFIX "cache metadata"
21
22#define CACHE_SUPERBLOCK_MAGIC 06142003
23#define CACHE_SUPERBLOCK_LOCATION 0
Joe Thornber53d49812013-10-16 10:59:26 +010024
25/*
26 * defines a range of metadata versions that this module can handle.
27 */
28#define MIN_CACHE_VERSION 1
Joe Thornber629d0a82016-09-22 06:15:21 -040029#define MAX_CACHE_VERSION 2
Joe Thornber53d49812013-10-16 10:59:26 +010030
Joe Thornberc6b4fcb2013-03-01 22:45:51 +000031/*
32 * 3 for btree insert +
33 * 2 for btree lookup used within space map
34 */
35#define CACHE_MAX_CONCURRENT_LOCKS 5
36#define SPACE_MAP_ROOT_SIZE 128
37
38enum superblock_flag_bits {
39 /* for spotting crashes that would invalidate the dirty bitset */
40 CLEAN_SHUTDOWN,
Joe Thornber028ae9f2015-04-22 16:42:35 -040041 /* metadata must be checked using the tools */
42 NEEDS_CHECK,
Joe Thornberc6b4fcb2013-03-01 22:45:51 +000043};
44
45/*
46 * Each mapping from cache block -> origin block carries a set of flags.
47 */
48enum mapping_bits {
49 /*
50 * A valid mapping. Because we're using an array we clear this
51 * flag for an non existant mapping.
52 */
53 M_VALID = 1,
54
55 /*
56 * The data on the cache is different from that on the origin.
Joe Thornber629d0a82016-09-22 06:15:21 -040057 * This flag is only used by metadata format 1.
Joe Thornberc6b4fcb2013-03-01 22:45:51 +000058 */
59 M_DIRTY = 2
60};
61
62struct cache_disk_superblock {
63 __le32 csum;
64 __le32 flags;
65 __le64 blocknr;
66
67 __u8 uuid[16];
68 __le64 magic;
69 __le32 version;
70
71 __u8 policy_name[CACHE_POLICY_NAME_SIZE];
72 __le32 policy_hint_size;
73
74 __u8 metadata_space_map_root[SPACE_MAP_ROOT_SIZE];
75 __le64 mapping_root;
76 __le64 hint_root;
77
78 __le64 discard_root;
79 __le64 discard_block_size;
80 __le64 discard_nr_blocks;
81
82 __le32 data_block_size;
83 __le32 metadata_block_size;
84 __le32 cache_blocks;
85
86 __le32 compat_flags;
87 __le32 compat_ro_flags;
88 __le32 incompat_flags;
89
90 __le32 read_hits;
91 __le32 read_misses;
92 __le32 write_hits;
93 __le32 write_misses;
Mike Snitzer4e7f5062013-03-20 17:21:27 +000094
95 __le32 policy_version[CACHE_POLICY_VERSION_SIZE];
Joe Thornber629d0a82016-09-22 06:15:21 -040096
97 /*
98 * Metadata format 2 fields.
99 */
100 __le64 dirty_root;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000101} __packed;
102
103struct dm_cache_metadata {
Elena Reshetova6bdd0792017-10-20 10:37:37 +0300104 refcount_t ref_count;
Joe Thornber9b1cc9f2015-01-23 10:00:07 +0000105 struct list_head list;
106
Joe Thornber629d0a82016-09-22 06:15:21 -0400107 unsigned version;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000108 struct block_device *bdev;
109 struct dm_block_manager *bm;
110 struct dm_space_map *metadata_sm;
111 struct dm_transaction_manager *tm;
112
113 struct dm_array_info info;
114 struct dm_array_info hint_info;
115 struct dm_disk_bitset discard_info;
116
117 struct rw_semaphore root_lock;
Joe Thornber028ae9f2015-04-22 16:42:35 -0400118 unsigned long flags;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000119 dm_block_t root;
120 dm_block_t hint_root;
121 dm_block_t discard_root;
122
123 sector_t discard_block_size;
Joe Thornber1bad9bc2014-11-07 14:47:07 +0000124 dm_dblock_t discard_nr_blocks;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000125
126 sector_t data_block_size;
127 dm_cblock_t cache_blocks;
128 bool changed:1;
129 bool clean_when_opened:1;
130
131 char policy_name[CACHE_POLICY_NAME_SIZE];
Mike Snitzer4e7f5062013-03-20 17:21:27 +0000132 unsigned policy_version[CACHE_POLICY_VERSION_SIZE];
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000133 size_t policy_hint_size;
134 struct dm_cache_statistics stats;
Joe Thornber5a320832014-03-27 14:13:23 +0000135
136 /*
137 * Reading the space map root can fail, so we read it into this
138 * buffer before the superblock is locked and updated.
139 */
140 __u8 metadata_space_map_root[SPACE_MAP_ROOT_SIZE];
Joe Thornber028ae9f2015-04-22 16:42:35 -0400141
142 /*
143 * Set if a transaction has to be aborted but the attempt to roll
144 * back to the previous (good) transaction failed. The only
145 * metadata operation permissible in this state is the closing of
146 * the device.
147 */
148 bool fail_io:1;
Joe Thornberf1779402016-09-20 06:16:00 -0400149
150 /*
Joe Thornber629d0a82016-09-22 06:15:21 -0400151 * Metadata format 2 fields.
152 */
153 dm_block_t dirty_root;
154 struct dm_disk_bitset dirty_info;
155
156 /*
Joe Thornberf1779402016-09-20 06:16:00 -0400157 * These structures are used when loading metadata. They're too
158 * big to put on the stack.
159 */
160 struct dm_array_cursor mapping_cursor;
161 struct dm_array_cursor hint_cursor;
Joe Thornber629d0a82016-09-22 06:15:21 -0400162 struct dm_bitset_cursor dirty_cursor;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000163};
164
165/*-------------------------------------------------------------------
166 * superblock validator
167 *-----------------------------------------------------------------*/
168
169#define SUPERBLOCK_CSUM_XOR 9031977
170
171static void sb_prepare_for_write(struct dm_block_validator *v,
172 struct dm_block *b,
173 size_t sb_block_size)
174{
175 struct cache_disk_superblock *disk_super = dm_block_data(b);
176
177 disk_super->blocknr = cpu_to_le64(dm_block_location(b));
178 disk_super->csum = cpu_to_le32(dm_bm_checksum(&disk_super->flags,
179 sb_block_size - sizeof(__le32),
180 SUPERBLOCK_CSUM_XOR));
181}
182
Joe Thornber53d49812013-10-16 10:59:26 +0100183static int check_metadata_version(struct cache_disk_superblock *disk_super)
184{
185 uint32_t metadata_version = le32_to_cpu(disk_super->version);
Joe Thornber629d0a82016-09-22 06:15:21 -0400186
Joe Thornber53d49812013-10-16 10:59:26 +0100187 if (metadata_version < MIN_CACHE_VERSION || metadata_version > MAX_CACHE_VERSION) {
188 DMERR("Cache metadata version %u found, but only versions between %u and %u supported.",
189 metadata_version, MIN_CACHE_VERSION, MAX_CACHE_VERSION);
190 return -EINVAL;
191 }
192
193 return 0;
194}
195
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000196static int sb_check(struct dm_block_validator *v,
197 struct dm_block *b,
198 size_t sb_block_size)
199{
200 struct cache_disk_superblock *disk_super = dm_block_data(b);
201 __le32 csum_le;
202
203 if (dm_block_location(b) != le64_to_cpu(disk_super->blocknr)) {
204 DMERR("sb_check failed: blocknr %llu: wanted %llu",
205 le64_to_cpu(disk_super->blocknr),
206 (unsigned long long)dm_block_location(b));
207 return -ENOTBLK;
208 }
209
210 if (le64_to_cpu(disk_super->magic) != CACHE_SUPERBLOCK_MAGIC) {
211 DMERR("sb_check failed: magic %llu: wanted %llu",
212 le64_to_cpu(disk_super->magic),
213 (unsigned long long)CACHE_SUPERBLOCK_MAGIC);
214 return -EILSEQ;
215 }
216
217 csum_le = cpu_to_le32(dm_bm_checksum(&disk_super->flags,
218 sb_block_size - sizeof(__le32),
219 SUPERBLOCK_CSUM_XOR));
220 if (csum_le != disk_super->csum) {
221 DMERR("sb_check failed: csum %u: wanted %u",
222 le32_to_cpu(csum_le), le32_to_cpu(disk_super->csum));
223 return -EILSEQ;
224 }
225
Joe Thornber53d49812013-10-16 10:59:26 +0100226 return check_metadata_version(disk_super);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000227}
228
229static struct dm_block_validator sb_validator = {
230 .name = "superblock",
231 .prepare_for_write = sb_prepare_for_write,
232 .check = sb_check
233};
234
235/*----------------------------------------------------------------*/
236
237static int superblock_read_lock(struct dm_cache_metadata *cmd,
238 struct dm_block **sblock)
239{
240 return dm_bm_read_lock(cmd->bm, CACHE_SUPERBLOCK_LOCATION,
241 &sb_validator, sblock);
242}
243
244static int superblock_lock_zero(struct dm_cache_metadata *cmd,
245 struct dm_block **sblock)
246{
247 return dm_bm_write_lock_zero(cmd->bm, CACHE_SUPERBLOCK_LOCATION,
248 &sb_validator, sblock);
249}
250
251static int superblock_lock(struct dm_cache_metadata *cmd,
252 struct dm_block **sblock)
253{
254 return dm_bm_write_lock(cmd->bm, CACHE_SUPERBLOCK_LOCATION,
255 &sb_validator, sblock);
256}
257
258/*----------------------------------------------------------------*/
259
Joe Thornberdd8b0c22013-10-24 14:10:28 -0400260static int __superblock_all_zeroes(struct dm_block_manager *bm, bool *result)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000261{
262 int r;
263 unsigned i;
264 struct dm_block *b;
265 __le64 *data_le, zero = cpu_to_le64(0);
266 unsigned sb_block_size = dm_bm_block_size(bm) / sizeof(__le64);
267
268 /*
269 * We can't use a validator here - it may be all zeroes.
270 */
271 r = dm_bm_read_lock(bm, CACHE_SUPERBLOCK_LOCATION, NULL, &b);
272 if (r)
273 return r;
274
275 data_le = dm_block_data(b);
Joe Thornberdd8b0c22013-10-24 14:10:28 -0400276 *result = true;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000277 for (i = 0; i < sb_block_size; i++) {
278 if (data_le[i] != zero) {
Joe Thornberdd8b0c22013-10-24 14:10:28 -0400279 *result = false;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000280 break;
281 }
282 }
283
Mikulas Patocka4c7da062015-10-22 16:46:59 -0400284 dm_bm_unlock(b);
285
286 return 0;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000287}
288
289static void __setup_mapping_info(struct dm_cache_metadata *cmd)
290{
291 struct dm_btree_value_type vt;
292
293 vt.context = NULL;
294 vt.size = sizeof(__le64);
295 vt.inc = NULL;
296 vt.dec = NULL;
297 vt.equal = NULL;
298 dm_array_info_init(&cmd->info, cmd->tm, &vt);
299
300 if (cmd->policy_hint_size) {
301 vt.size = sizeof(__le32);
302 dm_array_info_init(&cmd->hint_info, cmd->tm, &vt);
303 }
304}
305
Joe Thornber5a320832014-03-27 14:13:23 +0000306static int __save_sm_root(struct dm_cache_metadata *cmd)
307{
308 int r;
309 size_t metadata_len;
310
311 r = dm_sm_root_size(cmd->metadata_sm, &metadata_len);
312 if (r < 0)
313 return r;
314
315 return dm_sm_copy_root(cmd->metadata_sm, &cmd->metadata_space_map_root,
316 metadata_len);
317}
318
319static void __copy_sm_root(struct dm_cache_metadata *cmd,
320 struct cache_disk_superblock *disk_super)
321{
322 memcpy(&disk_super->metadata_space_map_root,
323 &cmd->metadata_space_map_root,
324 sizeof(cmd->metadata_space_map_root));
325}
326
Joe Thornber629d0a82016-09-22 06:15:21 -0400327static bool separate_dirty_bits(struct dm_cache_metadata *cmd)
328{
329 return cmd->version >= 2;
330}
331
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000332static int __write_initial_superblock(struct dm_cache_metadata *cmd)
333{
334 int r;
335 struct dm_block *sblock;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000336 struct cache_disk_superblock *disk_super;
337 sector_t bdev_size = i_size_read(cmd->bdev->bd_inode) >> SECTOR_SHIFT;
338
339 /* FIXME: see if we can lose the max sectors limit */
340 if (bdev_size > DM_CACHE_METADATA_MAX_SECTORS)
341 bdev_size = DM_CACHE_METADATA_MAX_SECTORS;
342
Joe Thornber5a320832014-03-27 14:13:23 +0000343 r = dm_tm_pre_commit(cmd->tm);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000344 if (r < 0)
345 return r;
346
Joe Thornber5a320832014-03-27 14:13:23 +0000347 /*
348 * dm_sm_copy_root() can fail. So we need to do it before we start
349 * updating the superblock.
350 */
351 r = __save_sm_root(cmd);
352 if (r)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000353 return r;
354
355 r = superblock_lock_zero(cmd, &sblock);
356 if (r)
357 return r;
358
359 disk_super = dm_block_data(sblock);
360 disk_super->flags = 0;
361 memset(disk_super->uuid, 0, sizeof(disk_super->uuid));
362 disk_super->magic = cpu_to_le64(CACHE_SUPERBLOCK_MAGIC);
Joe Thornber629d0a82016-09-22 06:15:21 -0400363 disk_super->version = cpu_to_le32(cmd->version);
Mike Snitzer4e7f5062013-03-20 17:21:27 +0000364 memset(disk_super->policy_name, 0, sizeof(disk_super->policy_name));
365 memset(disk_super->policy_version, 0, sizeof(disk_super->policy_version));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000366 disk_super->policy_hint_size = 0;
367
Joe Thornber5a320832014-03-27 14:13:23 +0000368 __copy_sm_root(cmd, disk_super);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000369
370 disk_super->mapping_root = cpu_to_le64(cmd->root);
371 disk_super->hint_root = cpu_to_le64(cmd->hint_root);
372 disk_super->discard_root = cpu_to_le64(cmd->discard_root);
373 disk_super->discard_block_size = cpu_to_le64(cmd->discard_block_size);
Joe Thornber1bad9bc2014-11-07 14:47:07 +0000374 disk_super->discard_nr_blocks = cpu_to_le64(from_dblock(cmd->discard_nr_blocks));
Mike Snitzer895b47d2014-07-14 15:37:18 -0400375 disk_super->metadata_block_size = cpu_to_le32(DM_CACHE_METADATA_BLOCK_SIZE);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000376 disk_super->data_block_size = cpu_to_le32(cmd->data_block_size);
377 disk_super->cache_blocks = cpu_to_le32(0);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000378
379 disk_super->read_hits = cpu_to_le32(0);
380 disk_super->read_misses = cpu_to_le32(0);
381 disk_super->write_hits = cpu_to_le32(0);
382 disk_super->write_misses = cpu_to_le32(0);
383
Joe Thornber629d0a82016-09-22 06:15:21 -0400384 if (separate_dirty_bits(cmd))
385 disk_super->dirty_root = cpu_to_le64(cmd->dirty_root);
386
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000387 return dm_tm_commit(cmd->tm, sblock);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000388}
389
390static int __format_metadata(struct dm_cache_metadata *cmd)
391{
392 int r;
393
394 r = dm_tm_create_with_sm(cmd->bm, CACHE_SUPERBLOCK_LOCATION,
395 &cmd->tm, &cmd->metadata_sm);
396 if (r < 0) {
397 DMERR("tm_create_with_sm failed");
398 return r;
399 }
400
401 __setup_mapping_info(cmd);
402
403 r = dm_array_empty(&cmd->info, &cmd->root);
404 if (r < 0)
405 goto bad;
406
Joe Thornber629d0a82016-09-22 06:15:21 -0400407 if (separate_dirty_bits(cmd)) {
408 dm_disk_bitset_init(cmd->tm, &cmd->dirty_info);
409 r = dm_bitset_empty(&cmd->dirty_info, &cmd->dirty_root);
410 if (r < 0)
411 goto bad;
412 }
413
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000414 dm_disk_bitset_init(cmd->tm, &cmd->discard_info);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000415 r = dm_bitset_empty(&cmd->discard_info, &cmd->discard_root);
416 if (r < 0)
417 goto bad;
418
419 cmd->discard_block_size = 0;
420 cmd->discard_nr_blocks = 0;
421
422 r = __write_initial_superblock(cmd);
423 if (r)
424 goto bad;
425
426 cmd->clean_when_opened = true;
427 return 0;
428
429bad:
430 dm_tm_destroy(cmd->tm);
431 dm_sm_destroy(cmd->metadata_sm);
432
433 return r;
434}
435
436static int __check_incompat_features(struct cache_disk_superblock *disk_super,
437 struct dm_cache_metadata *cmd)
438{
Joe Thornber629d0a82016-09-22 06:15:21 -0400439 uint32_t incompat_flags, features;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000440
Joe Thornber629d0a82016-09-22 06:15:21 -0400441 incompat_flags = le32_to_cpu(disk_super->incompat_flags);
442 features = incompat_flags & ~DM_CACHE_FEATURE_INCOMPAT_SUPP;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000443 if (features) {
444 DMERR("could not access metadata due to unsupported optional features (%lx).",
445 (unsigned long)features);
446 return -EINVAL;
447 }
448
449 /*
450 * Check for read-only metadata to skip the following RDWR checks.
451 */
452 if (get_disk_ro(cmd->bdev->bd_disk))
453 return 0;
454
455 features = le32_to_cpu(disk_super->compat_ro_flags) & ~DM_CACHE_FEATURE_COMPAT_RO_SUPP;
456 if (features) {
457 DMERR("could not access metadata RDWR due to unsupported optional features (%lx).",
458 (unsigned long)features);
459 return -EINVAL;
460 }
461
462 return 0;
463}
464
465static int __open_metadata(struct dm_cache_metadata *cmd)
466{
467 int r;
468 struct dm_block *sblock;
469 struct cache_disk_superblock *disk_super;
470 unsigned long sb_flags;
471
472 r = superblock_read_lock(cmd, &sblock);
473 if (r < 0) {
474 DMERR("couldn't read lock superblock");
475 return r;
476 }
477
478 disk_super = dm_block_data(sblock);
479
Mike Snitzer048e5a02014-07-14 16:59:39 -0400480 /* Verify the data block size hasn't changed */
481 if (le32_to_cpu(disk_super->data_block_size) != cmd->data_block_size) {
482 DMERR("changing the data block size (from %u to %llu) is not supported",
483 le32_to_cpu(disk_super->data_block_size),
484 (unsigned long long)cmd->data_block_size);
485 r = -EINVAL;
486 goto bad;
487 }
488
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000489 r = __check_incompat_features(disk_super, cmd);
490 if (r < 0)
491 goto bad;
492
493 r = dm_tm_open_with_sm(cmd->bm, CACHE_SUPERBLOCK_LOCATION,
494 disk_super->metadata_space_map_root,
495 sizeof(disk_super->metadata_space_map_root),
496 &cmd->tm, &cmd->metadata_sm);
497 if (r < 0) {
498 DMERR("tm_open_with_sm failed");
499 goto bad;
500 }
501
502 __setup_mapping_info(cmd);
Joe Thornber629d0a82016-09-22 06:15:21 -0400503 dm_disk_bitset_init(cmd->tm, &cmd->dirty_info);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000504 dm_disk_bitset_init(cmd->tm, &cmd->discard_info);
505 sb_flags = le32_to_cpu(disk_super->flags);
506 cmd->clean_when_opened = test_bit(CLEAN_SHUTDOWN, &sb_flags);
Mikulas Patocka4c7da062015-10-22 16:46:59 -0400507 dm_bm_unlock(sblock);
508
509 return 0;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000510
511bad:
512 dm_bm_unlock(sblock);
513 return r;
514}
515
516static int __open_or_format_metadata(struct dm_cache_metadata *cmd,
517 bool format_device)
518{
Joe Thornberdd8b0c22013-10-24 14:10:28 -0400519 int r;
520 bool unformatted = false;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000521
522 r = __superblock_all_zeroes(cmd->bm, &unformatted);
523 if (r)
524 return r;
525
526 if (unformatted)
527 return format_device ? __format_metadata(cmd) : -EPERM;
528
529 return __open_metadata(cmd);
530}
531
532static int __create_persistent_data_objects(struct dm_cache_metadata *cmd,
533 bool may_format_device)
534{
535 int r;
Mike Snitzer895b47d2014-07-14 15:37:18 -0400536 cmd->bm = dm_block_manager_create(cmd->bdev, DM_CACHE_METADATA_BLOCK_SIZE << SECTOR_SHIFT,
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000537 CACHE_MAX_CONCURRENT_LOCKS);
538 if (IS_ERR(cmd->bm)) {
539 DMERR("could not create block manager");
540 return PTR_ERR(cmd->bm);
541 }
542
543 r = __open_or_format_metadata(cmd, may_format_device);
544 if (r)
545 dm_block_manager_destroy(cmd->bm);
546
547 return r;
548}
549
550static void __destroy_persistent_data_objects(struct dm_cache_metadata *cmd)
551{
552 dm_sm_destroy(cmd->metadata_sm);
553 dm_tm_destroy(cmd->tm);
554 dm_block_manager_destroy(cmd->bm);
555}
556
557typedef unsigned long (*flags_mutator)(unsigned long);
558
559static void update_flags(struct cache_disk_superblock *disk_super,
560 flags_mutator mutator)
561{
562 uint32_t sb_flags = mutator(le32_to_cpu(disk_super->flags));
563 disk_super->flags = cpu_to_le32(sb_flags);
564}
565
566static unsigned long set_clean_shutdown(unsigned long flags)
567{
568 set_bit(CLEAN_SHUTDOWN, &flags);
569 return flags;
570}
571
572static unsigned long clear_clean_shutdown(unsigned long flags)
573{
574 clear_bit(CLEAN_SHUTDOWN, &flags);
575 return flags;
576}
577
578static void read_superblock_fields(struct dm_cache_metadata *cmd,
579 struct cache_disk_superblock *disk_super)
580{
Joe Thornber629d0a82016-09-22 06:15:21 -0400581 cmd->version = le32_to_cpu(disk_super->version);
Joe Thornber028ae9f2015-04-22 16:42:35 -0400582 cmd->flags = le32_to_cpu(disk_super->flags);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000583 cmd->root = le64_to_cpu(disk_super->mapping_root);
584 cmd->hint_root = le64_to_cpu(disk_super->hint_root);
585 cmd->discard_root = le64_to_cpu(disk_super->discard_root);
586 cmd->discard_block_size = le64_to_cpu(disk_super->discard_block_size);
Joe Thornber1bad9bc2014-11-07 14:47:07 +0000587 cmd->discard_nr_blocks = to_dblock(le64_to_cpu(disk_super->discard_nr_blocks));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000588 cmd->data_block_size = le32_to_cpu(disk_super->data_block_size);
589 cmd->cache_blocks = to_cblock(le32_to_cpu(disk_super->cache_blocks));
590 strncpy(cmd->policy_name, disk_super->policy_name, sizeof(cmd->policy_name));
Mike Snitzer4e7f5062013-03-20 17:21:27 +0000591 cmd->policy_version[0] = le32_to_cpu(disk_super->policy_version[0]);
592 cmd->policy_version[1] = le32_to_cpu(disk_super->policy_version[1]);
593 cmd->policy_version[2] = le32_to_cpu(disk_super->policy_version[2]);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000594 cmd->policy_hint_size = le32_to_cpu(disk_super->policy_hint_size);
595
596 cmd->stats.read_hits = le32_to_cpu(disk_super->read_hits);
597 cmd->stats.read_misses = le32_to_cpu(disk_super->read_misses);
598 cmd->stats.write_hits = le32_to_cpu(disk_super->write_hits);
599 cmd->stats.write_misses = le32_to_cpu(disk_super->write_misses);
600
Joe Thornber629d0a82016-09-22 06:15:21 -0400601 if (separate_dirty_bits(cmd))
602 cmd->dirty_root = le64_to_cpu(disk_super->dirty_root);
603
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000604 cmd->changed = false;
605}
606
607/*
608 * The mutator updates the superblock flags.
609 */
610static int __begin_transaction_flags(struct dm_cache_metadata *cmd,
611 flags_mutator mutator)
612{
613 int r;
614 struct cache_disk_superblock *disk_super;
615 struct dm_block *sblock;
616
617 r = superblock_lock(cmd, &sblock);
618 if (r)
619 return r;
620
621 disk_super = dm_block_data(sblock);
622 update_flags(disk_super, mutator);
623 read_superblock_fields(cmd, disk_super);
Joe Thornbera9d45392014-03-27 14:13:20 +0000624 dm_bm_unlock(sblock);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000625
Joe Thornbera9d45392014-03-27 14:13:20 +0000626 return dm_bm_flush(cmd->bm);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000627}
628
629static int __begin_transaction(struct dm_cache_metadata *cmd)
630{
631 int r;
632 struct cache_disk_superblock *disk_super;
633 struct dm_block *sblock;
634
635 /*
636 * We re-read the superblock every time. Shouldn't need to do this
637 * really.
638 */
639 r = superblock_read_lock(cmd, &sblock);
640 if (r)
641 return r;
642
643 disk_super = dm_block_data(sblock);
644 read_superblock_fields(cmd, disk_super);
645 dm_bm_unlock(sblock);
646
647 return 0;
648}
649
650static int __commit_transaction(struct dm_cache_metadata *cmd,
651 flags_mutator mutator)
652{
653 int r;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000654 struct cache_disk_superblock *disk_super;
655 struct dm_block *sblock;
656
657 /*
658 * We need to know if the cache_disk_superblock exceeds a 512-byte sector.
659 */
660 BUILD_BUG_ON(sizeof(struct cache_disk_superblock) > 512);
661
Joe Thornber629d0a82016-09-22 06:15:21 -0400662 if (separate_dirty_bits(cmd)) {
663 r = dm_bitset_flush(&cmd->dirty_info, cmd->dirty_root,
664 &cmd->dirty_root);
665 if (r)
666 return r;
667 }
668
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000669 r = dm_bitset_flush(&cmd->discard_info, cmd->discard_root,
670 &cmd->discard_root);
671 if (r)
672 return r;
673
674 r = dm_tm_pre_commit(cmd->tm);
675 if (r < 0)
676 return r;
677
Joe Thornber5a320832014-03-27 14:13:23 +0000678 r = __save_sm_root(cmd);
679 if (r)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000680 return r;
681
682 r = superblock_lock(cmd, &sblock);
683 if (r)
684 return r;
685
686 disk_super = dm_block_data(sblock);
687
Joe Thornber3201ac42015-10-22 18:10:55 +0100688 disk_super->flags = cpu_to_le32(cmd->flags);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000689 if (mutator)
690 update_flags(disk_super, mutator);
691
692 disk_super->mapping_root = cpu_to_le64(cmd->root);
Joe Thornber629d0a82016-09-22 06:15:21 -0400693 if (separate_dirty_bits(cmd))
694 disk_super->dirty_root = cpu_to_le64(cmd->dirty_root);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000695 disk_super->hint_root = cpu_to_le64(cmd->hint_root);
696 disk_super->discard_root = cpu_to_le64(cmd->discard_root);
697 disk_super->discard_block_size = cpu_to_le64(cmd->discard_block_size);
Joe Thornber1bad9bc2014-11-07 14:47:07 +0000698 disk_super->discard_nr_blocks = cpu_to_le64(from_dblock(cmd->discard_nr_blocks));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000699 disk_super->cache_blocks = cpu_to_le32(from_cblock(cmd->cache_blocks));
700 strncpy(disk_super->policy_name, cmd->policy_name, sizeof(disk_super->policy_name));
Mike Snitzer4e7f5062013-03-20 17:21:27 +0000701 disk_super->policy_version[0] = cpu_to_le32(cmd->policy_version[0]);
702 disk_super->policy_version[1] = cpu_to_le32(cmd->policy_version[1]);
703 disk_super->policy_version[2] = cpu_to_le32(cmd->policy_version[2]);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000704
705 disk_super->read_hits = cpu_to_le32(cmd->stats.read_hits);
706 disk_super->read_misses = cpu_to_le32(cmd->stats.read_misses);
707 disk_super->write_hits = cpu_to_le32(cmd->stats.write_hits);
708 disk_super->write_misses = cpu_to_le32(cmd->stats.write_misses);
Joe Thornber5a320832014-03-27 14:13:23 +0000709 __copy_sm_root(cmd, disk_super);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000710
711 return dm_tm_commit(cmd->tm, sblock);
712}
713
714/*----------------------------------------------------------------*/
715
716/*
717 * The mappings are held in a dm-array that has 64-bit values stored in
718 * little-endian format. The index is the cblock, the high 48bits of the
719 * value are the oblock and the low 16 bit the flags.
720 */
721#define FLAGS_MASK ((1 << 16) - 1)
722
723static __le64 pack_value(dm_oblock_t block, unsigned flags)
724{
725 uint64_t value = from_oblock(block);
726 value <<= 16;
727 value = value | (flags & FLAGS_MASK);
728 return cpu_to_le64(value);
729}
730
731static void unpack_value(__le64 value_le, dm_oblock_t *block, unsigned *flags)
732{
733 uint64_t value = le64_to_cpu(value_le);
734 uint64_t b = value >> 16;
735 *block = to_oblock(b);
736 *flags = value & FLAGS_MASK;
737}
738
739/*----------------------------------------------------------------*/
740
Joe Thornber9b1cc9f2015-01-23 10:00:07 +0000741static struct dm_cache_metadata *metadata_open(struct block_device *bdev,
742 sector_t data_block_size,
743 bool may_format_device,
Joe Thornber629d0a82016-09-22 06:15:21 -0400744 size_t policy_hint_size,
745 unsigned metadata_version)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000746{
747 int r;
748 struct dm_cache_metadata *cmd;
749
750 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
751 if (!cmd) {
752 DMERR("could not allocate metadata struct");
Joe Thornber766a7882015-01-28 12:07:46 +0000753 return ERR_PTR(-ENOMEM);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000754 }
755
Joe Thornber629d0a82016-09-22 06:15:21 -0400756 cmd->version = metadata_version;
Elena Reshetova6bdd0792017-10-20 10:37:37 +0300757 refcount_set(&cmd->ref_count, 1);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000758 init_rwsem(&cmd->root_lock);
759 cmd->bdev = bdev;
760 cmd->data_block_size = data_block_size;
761 cmd->cache_blocks = 0;
762 cmd->policy_hint_size = policy_hint_size;
763 cmd->changed = true;
Joe Thornber028ae9f2015-04-22 16:42:35 -0400764 cmd->fail_io = false;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000765
766 r = __create_persistent_data_objects(cmd, may_format_device);
767 if (r) {
768 kfree(cmd);
769 return ERR_PTR(r);
770 }
771
772 r = __begin_transaction_flags(cmd, clear_clean_shutdown);
773 if (r < 0) {
774 dm_cache_metadata_close(cmd);
775 return ERR_PTR(r);
776 }
777
778 return cmd;
779}
780
Joe Thornber9b1cc9f2015-01-23 10:00:07 +0000781/*
782 * We keep a little list of ref counted metadata objects to prevent two
783 * different target instances creating separate bufio instances. This is
784 * an issue if a table is reloaded before the suspend.
785 */
786static DEFINE_MUTEX(table_lock);
787static LIST_HEAD(table);
788
789static struct dm_cache_metadata *lookup(struct block_device *bdev)
790{
791 struct dm_cache_metadata *cmd;
792
793 list_for_each_entry(cmd, &table, list)
794 if (cmd->bdev == bdev) {
Elena Reshetova6bdd0792017-10-20 10:37:37 +0300795 refcount_inc(&cmd->ref_count);
Joe Thornber9b1cc9f2015-01-23 10:00:07 +0000796 return cmd;
797 }
798
799 return NULL;
800}
801
802static struct dm_cache_metadata *lookup_or_open(struct block_device *bdev,
803 sector_t data_block_size,
804 bool may_format_device,
Joe Thornber629d0a82016-09-22 06:15:21 -0400805 size_t policy_hint_size,
806 unsigned metadata_version)
Joe Thornber9b1cc9f2015-01-23 10:00:07 +0000807{
808 struct dm_cache_metadata *cmd, *cmd2;
809
810 mutex_lock(&table_lock);
811 cmd = lookup(bdev);
812 mutex_unlock(&table_lock);
813
814 if (cmd)
815 return cmd;
816
Joe Thornber629d0a82016-09-22 06:15:21 -0400817 cmd = metadata_open(bdev, data_block_size, may_format_device,
818 policy_hint_size, metadata_version);
Joe Thornber766a7882015-01-28 12:07:46 +0000819 if (!IS_ERR(cmd)) {
Joe Thornber9b1cc9f2015-01-23 10:00:07 +0000820 mutex_lock(&table_lock);
821 cmd2 = lookup(bdev);
822 if (cmd2) {
823 mutex_unlock(&table_lock);
824 __destroy_persistent_data_objects(cmd);
825 kfree(cmd);
826 return cmd2;
827 }
828 list_add(&cmd->list, &table);
829 mutex_unlock(&table_lock);
830 }
831
832 return cmd;
833}
834
835static bool same_params(struct dm_cache_metadata *cmd, sector_t data_block_size)
836{
837 if (cmd->data_block_size != data_block_size) {
Mike Snitzer07d93882016-10-03 11:56:22 -0400838 DMERR("data_block_size (%llu) different from that in metadata (%llu)",
Joe Thornber9b1cc9f2015-01-23 10:00:07 +0000839 (unsigned long long) data_block_size,
840 (unsigned long long) cmd->data_block_size);
841 return false;
842 }
843
844 return true;
845}
846
847struct dm_cache_metadata *dm_cache_metadata_open(struct block_device *bdev,
848 sector_t data_block_size,
849 bool may_format_device,
Joe Thornber629d0a82016-09-22 06:15:21 -0400850 size_t policy_hint_size,
851 unsigned metadata_version)
Joe Thornber9b1cc9f2015-01-23 10:00:07 +0000852{
Joe Thornber629d0a82016-09-22 06:15:21 -0400853 struct dm_cache_metadata *cmd = lookup_or_open(bdev, data_block_size, may_format_device,
854 policy_hint_size, metadata_version);
Joe Thornber766a7882015-01-28 12:07:46 +0000855
856 if (!IS_ERR(cmd) && !same_params(cmd, data_block_size)) {
Joe Thornber9b1cc9f2015-01-23 10:00:07 +0000857 dm_cache_metadata_close(cmd);
Joe Thornber766a7882015-01-28 12:07:46 +0000858 return ERR_PTR(-EINVAL);
Joe Thornber9b1cc9f2015-01-23 10:00:07 +0000859 }
860
861 return cmd;
862}
863
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000864void dm_cache_metadata_close(struct dm_cache_metadata *cmd)
865{
Elena Reshetova6bdd0792017-10-20 10:37:37 +0300866 if (refcount_dec_and_test(&cmd->ref_count)) {
Joe Thornber9b1cc9f2015-01-23 10:00:07 +0000867 mutex_lock(&table_lock);
868 list_del(&cmd->list);
869 mutex_unlock(&table_lock);
870
Joe Thornber028ae9f2015-04-22 16:42:35 -0400871 if (!cmd->fail_io)
872 __destroy_persistent_data_objects(cmd);
Joe Thornber9b1cc9f2015-01-23 10:00:07 +0000873 kfree(cmd);
874 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000875}
876
Joe Thornberf494a9c2013-10-31 13:55:49 -0400877/*
878 * Checks that the given cache block is either unmapped or clean.
879 */
Joe Thornber629d0a82016-09-22 06:15:21 -0400880static int block_clean_combined_dirty(struct dm_cache_metadata *cmd, dm_cblock_t b,
881 bool *result)
Joe Thornberf494a9c2013-10-31 13:55:49 -0400882{
883 int r;
884 __le64 value;
885 dm_oblock_t ob;
886 unsigned flags;
887
888 r = dm_array_get_value(&cmd->info, cmd->root, from_cblock(b), &value);
Joe Thornber629d0a82016-09-22 06:15:21 -0400889 if (r)
Joe Thornberf494a9c2013-10-31 13:55:49 -0400890 return r;
Joe Thornberf494a9c2013-10-31 13:55:49 -0400891
892 unpack_value(value, &ob, &flags);
893 *result = !((flags & M_VALID) && (flags & M_DIRTY));
894
895 return 0;
896}
897
Joe Thornber629d0a82016-09-22 06:15:21 -0400898static int blocks_are_clean_combined_dirty(struct dm_cache_metadata *cmd,
899 dm_cblock_t begin, dm_cblock_t end,
900 bool *result)
Joe Thornberf494a9c2013-10-31 13:55:49 -0400901{
902 int r;
903 *result = true;
904
905 while (begin != end) {
Joe Thornber629d0a82016-09-22 06:15:21 -0400906 r = block_clean_combined_dirty(cmd, begin, result);
907 if (r) {
908 DMERR("block_clean_combined_dirty failed");
Joe Thornberf494a9c2013-10-31 13:55:49 -0400909 return r;
Joe Thornber629d0a82016-09-22 06:15:21 -0400910 }
Joe Thornberf494a9c2013-10-31 13:55:49 -0400911
912 if (!*result) {
913 DMERR("cache block %llu is dirty",
914 (unsigned long long) from_cblock(begin));
915 return 0;
916 }
917
918 begin = to_cblock(from_cblock(begin) + 1);
919 }
920
921 return 0;
922}
923
Joe Thornber629d0a82016-09-22 06:15:21 -0400924static int blocks_are_clean_separate_dirty(struct dm_cache_metadata *cmd,
925 dm_cblock_t begin, dm_cblock_t end,
926 bool *result)
927{
928 int r;
929 bool dirty_flag;
930 *result = true;
931
Mike Snitzer7f1b2152016-10-04 15:00:47 -0400932 r = dm_bitset_cursor_begin(&cmd->dirty_info, cmd->dirty_root,
Joe Thornber0d963b62017-03-20 11:54:11 -0400933 from_cblock(cmd->cache_blocks), &cmd->dirty_cursor);
Mike Snitzer7f1b2152016-10-04 15:00:47 -0400934 if (r) {
935 DMERR("%s: dm_bitset_cursor_begin for dirty failed", __func__);
936 return r;
937 }
938
939 r = dm_bitset_cursor_skip(&cmd->dirty_cursor, from_cblock(begin));
940 if (r) {
941 DMERR("%s: dm_bitset_cursor_skip for dirty failed", __func__);
942 dm_bitset_cursor_end(&cmd->dirty_cursor);
943 return r;
944 }
945
Joe Thornber629d0a82016-09-22 06:15:21 -0400946 while (begin != end) {
947 /*
948 * We assume that unmapped blocks have their dirty bit
949 * cleared.
950 */
Mike Snitzer7f1b2152016-10-04 15:00:47 -0400951 dirty_flag = dm_bitset_cursor_get_value(&cmd->dirty_cursor);
Joe Thornber629d0a82016-09-22 06:15:21 -0400952 if (dirty_flag) {
Mike Snitzer7f1b2152016-10-04 15:00:47 -0400953 DMERR("%s: cache block %llu is dirty", __func__,
Joe Thornber629d0a82016-09-22 06:15:21 -0400954 (unsigned long long) from_cblock(begin));
Mike Snitzer7f1b2152016-10-04 15:00:47 -0400955 dm_bitset_cursor_end(&cmd->dirty_cursor);
Joe Thornber629d0a82016-09-22 06:15:21 -0400956 *result = false;
957 return 0;
958 }
959
Joe Thornber0d963b62017-03-20 11:54:11 -0400960 begin = to_cblock(from_cblock(begin) + 1);
961 if (begin == end)
962 break;
963
Mike Snitzer7f1b2152016-10-04 15:00:47 -0400964 r = dm_bitset_cursor_next(&cmd->dirty_cursor);
965 if (r) {
966 DMERR("%s: dm_bitset_cursor_next for dirty failed", __func__);
967 dm_bitset_cursor_end(&cmd->dirty_cursor);
968 return r;
969 }
Joe Thornber629d0a82016-09-22 06:15:21 -0400970 }
971
Mike Snitzer7f1b2152016-10-04 15:00:47 -0400972 dm_bitset_cursor_end(&cmd->dirty_cursor);
973
Joe Thornber629d0a82016-09-22 06:15:21 -0400974 return 0;
975}
976
977static int blocks_are_unmapped_or_clean(struct dm_cache_metadata *cmd,
978 dm_cblock_t begin, dm_cblock_t end,
979 bool *result)
980{
981 if (separate_dirty_bits(cmd))
982 return blocks_are_clean_separate_dirty(cmd, begin, end, result);
983 else
984 return blocks_are_clean_combined_dirty(cmd, begin, end, result);
985}
986
Mike Snitzer95673662016-04-12 12:14:46 -0400987static bool cmd_write_lock(struct dm_cache_metadata *cmd)
988{
989 down_write(&cmd->root_lock);
990 if (cmd->fail_io || dm_bm_is_read_only(cmd->bm)) {
991 up_write(&cmd->root_lock);
992 return false;
Joe Thornberd14fcf32016-03-10 16:20:58 +0000993 }
Mike Snitzer95673662016-04-12 12:14:46 -0400994 return true;
995}
Joe Thornber028ae9f2015-04-22 16:42:35 -0400996
Mike Snitzer95673662016-04-12 12:14:46 -0400997#define WRITE_LOCK(cmd) \
998 do { \
999 if (!cmd_write_lock((cmd))) \
1000 return -EINVAL; \
1001 } while(0)
1002
1003#define WRITE_LOCK_VOID(cmd) \
1004 do { \
1005 if (!cmd_write_lock((cmd))) \
1006 return; \
1007 } while(0)
Joe Thornber028ae9f2015-04-22 16:42:35 -04001008
1009#define WRITE_UNLOCK(cmd) \
Mike Snitzer95673662016-04-12 12:14:46 -04001010 up_write(&(cmd)->root_lock)
Joe Thornber028ae9f2015-04-22 16:42:35 -04001011
Mike Snitzer95673662016-04-12 12:14:46 -04001012static bool cmd_read_lock(struct dm_cache_metadata *cmd)
1013{
Ahmed Samy6545b602016-04-17 05:37:09 +00001014 down_read(&cmd->root_lock);
Mike Snitzer95673662016-04-12 12:14:46 -04001015 if (cmd->fail_io) {
Ahmed Samy6545b602016-04-17 05:37:09 +00001016 up_read(&cmd->root_lock);
Mike Snitzer95673662016-04-12 12:14:46 -04001017 return false;
Joe Thornberd14fcf32016-03-10 16:20:58 +00001018 }
Mike Snitzer95673662016-04-12 12:14:46 -04001019 return true;
1020}
Joe Thornberd14fcf32016-03-10 16:20:58 +00001021
Mike Snitzer95673662016-04-12 12:14:46 -04001022#define READ_LOCK(cmd) \
1023 do { \
1024 if (!cmd_read_lock((cmd))) \
1025 return -EINVAL; \
1026 } while(0)
1027
1028#define READ_LOCK_VOID(cmd) \
1029 do { \
1030 if (!cmd_read_lock((cmd))) \
1031 return; \
1032 } while(0)
Joe Thornberd14fcf32016-03-10 16:20:58 +00001033
1034#define READ_UNLOCK(cmd) \
Mike Snitzer95673662016-04-12 12:14:46 -04001035 up_read(&(cmd)->root_lock)
Joe Thornberd14fcf32016-03-10 16:20:58 +00001036
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001037int dm_cache_resize(struct dm_cache_metadata *cmd, dm_cblock_t new_cache_size)
1038{
1039 int r;
Joe Thornberf494a9c2013-10-31 13:55:49 -04001040 bool clean;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001041 __le64 null_mapping = pack_value(0, 0);
1042
Joe Thornber028ae9f2015-04-22 16:42:35 -04001043 WRITE_LOCK(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001044 __dm_bless_for_disk(&null_mapping);
Joe Thornberf494a9c2013-10-31 13:55:49 -04001045
1046 if (from_cblock(new_cache_size) < from_cblock(cmd->cache_blocks)) {
1047 r = blocks_are_unmapped_or_clean(cmd, new_cache_size, cmd->cache_blocks, &clean);
1048 if (r) {
1049 __dm_unbless_for_disk(&null_mapping);
1050 goto out;
1051 }
1052
1053 if (!clean) {
1054 DMERR("unable to shrink cache due to dirty blocks");
1055 r = -EINVAL;
1056 __dm_unbless_for_disk(&null_mapping);
1057 goto out;
1058 }
1059 }
1060
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001061 r = dm_array_resize(&cmd->info, cmd->root, from_cblock(cmd->cache_blocks),
1062 from_cblock(new_cache_size),
1063 &null_mapping, &cmd->root);
Joe Thornber629d0a82016-09-22 06:15:21 -04001064 if (r)
1065 goto out;
1066
1067 if (separate_dirty_bits(cmd)) {
1068 r = dm_bitset_resize(&cmd->dirty_info, cmd->dirty_root,
1069 from_cblock(cmd->cache_blocks), from_cblock(new_cache_size),
1070 false, &cmd->dirty_root);
1071 if (r)
1072 goto out;
1073 }
1074
1075 cmd->cache_blocks = new_cache_size;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001076 cmd->changed = true;
Joe Thornberf494a9c2013-10-31 13:55:49 -04001077
1078out:
Joe Thornber028ae9f2015-04-22 16:42:35 -04001079 WRITE_UNLOCK(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001080
1081 return r;
1082}
1083
1084int dm_cache_discard_bitset_resize(struct dm_cache_metadata *cmd,
1085 sector_t discard_block_size,
Joe Thornber1bad9bc2014-11-07 14:47:07 +00001086 dm_dblock_t new_nr_entries)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001087{
1088 int r;
1089
Joe Thornber028ae9f2015-04-22 16:42:35 -04001090 WRITE_LOCK(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001091 r = dm_bitset_resize(&cmd->discard_info,
1092 cmd->discard_root,
Joe Thornber1bad9bc2014-11-07 14:47:07 +00001093 from_dblock(cmd->discard_nr_blocks),
1094 from_dblock(new_nr_entries),
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001095 false, &cmd->discard_root);
1096 if (!r) {
1097 cmd->discard_block_size = discard_block_size;
1098 cmd->discard_nr_blocks = new_nr_entries;
1099 }
1100
1101 cmd->changed = true;
Joe Thornber028ae9f2015-04-22 16:42:35 -04001102 WRITE_UNLOCK(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001103
1104 return r;
1105}
1106
Joe Thornber1bad9bc2014-11-07 14:47:07 +00001107static int __set_discard(struct dm_cache_metadata *cmd, dm_dblock_t b)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001108{
1109 return dm_bitset_set_bit(&cmd->discard_info, cmd->discard_root,
Joe Thornber1bad9bc2014-11-07 14:47:07 +00001110 from_dblock(b), &cmd->discard_root);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001111}
1112
Joe Thornber1bad9bc2014-11-07 14:47:07 +00001113static int __clear_discard(struct dm_cache_metadata *cmd, dm_dblock_t b)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001114{
1115 return dm_bitset_clear_bit(&cmd->discard_info, cmd->discard_root,
Joe Thornber1bad9bc2014-11-07 14:47:07 +00001116 from_dblock(b), &cmd->discard_root);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001117}
1118
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001119static int __discard(struct dm_cache_metadata *cmd,
Joe Thornber1bad9bc2014-11-07 14:47:07 +00001120 dm_dblock_t dblock, bool discard)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001121{
1122 int r;
1123
1124 r = (discard ? __set_discard : __clear_discard)(cmd, dblock);
1125 if (r)
1126 return r;
1127
1128 cmd->changed = true;
1129 return 0;
1130}
1131
1132int dm_cache_set_discard(struct dm_cache_metadata *cmd,
Joe Thornber1bad9bc2014-11-07 14:47:07 +00001133 dm_dblock_t dblock, bool discard)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001134{
1135 int r;
1136
Joe Thornber028ae9f2015-04-22 16:42:35 -04001137 WRITE_LOCK(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001138 r = __discard(cmd, dblock, discard);
Joe Thornber028ae9f2015-04-22 16:42:35 -04001139 WRITE_UNLOCK(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001140
1141 return r;
1142}
1143
1144static int __load_discards(struct dm_cache_metadata *cmd,
1145 load_discard_fn fn, void *context)
1146{
1147 int r = 0;
Joe Thornberae4a46a2016-10-03 14:16:20 -04001148 uint32_t b;
1149 struct dm_bitset_cursor c;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001150
Joe Thornberae4a46a2016-10-03 14:16:20 -04001151 if (from_dblock(cmd->discard_nr_blocks) == 0)
1152 /* nothing to do */
1153 return 0;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001154
Joe Thornberae4a46a2016-10-03 14:16:20 -04001155 if (cmd->clean_when_opened) {
1156 r = dm_bitset_flush(&cmd->discard_info, cmd->discard_root, &cmd->discard_root);
1157 if (r)
1158 return r;
1159
1160 r = dm_bitset_cursor_begin(&cmd->discard_info, cmd->discard_root,
1161 from_dblock(cmd->discard_nr_blocks), &c);
1162 if (r)
1163 return r;
1164
1165 for (b = 0; b < from_dblock(cmd->discard_nr_blocks); b++) {
1166 r = fn(context, cmd->discard_block_size, to_dblock(b),
1167 dm_bitset_cursor_get_value(&c));
1168 if (r)
1169 break;
1170 }
1171
1172 dm_bitset_cursor_end(&c);
1173
1174 } else {
1175 for (b = 0; b < from_dblock(cmd->discard_nr_blocks); b++) {
1176 r = fn(context, cmd->discard_block_size, to_dblock(b), false);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001177 if (r)
1178 return r;
Joe Thornberae4a46a2016-10-03 14:16:20 -04001179 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001180 }
1181
1182 return r;
1183}
1184
1185int dm_cache_load_discards(struct dm_cache_metadata *cmd,
1186 load_discard_fn fn, void *context)
1187{
1188 int r;
1189
Joe Thornberd14fcf32016-03-10 16:20:58 +00001190 READ_LOCK(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001191 r = __load_discards(cmd, fn, context);
Joe Thornberd14fcf32016-03-10 16:20:58 +00001192 READ_UNLOCK(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001193
1194 return r;
1195}
1196
Joe Thornberd14fcf32016-03-10 16:20:58 +00001197int dm_cache_size(struct dm_cache_metadata *cmd, dm_cblock_t *result)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001198{
Joe Thornberd14fcf32016-03-10 16:20:58 +00001199 READ_LOCK(cmd);
1200 *result = cmd->cache_blocks;
1201 READ_UNLOCK(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001202
Joe Thornberd14fcf32016-03-10 16:20:58 +00001203 return 0;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001204}
1205
1206static int __remove(struct dm_cache_metadata *cmd, dm_cblock_t cblock)
1207{
1208 int r;
1209 __le64 value = pack_value(0, 0);
1210
1211 __dm_bless_for_disk(&value);
1212 r = dm_array_set_value(&cmd->info, cmd->root, from_cblock(cblock),
1213 &value, &cmd->root);
1214 if (r)
1215 return r;
1216
1217 cmd->changed = true;
1218 return 0;
1219}
1220
1221int dm_cache_remove_mapping(struct dm_cache_metadata *cmd, dm_cblock_t cblock)
1222{
1223 int r;
1224
Joe Thornber028ae9f2015-04-22 16:42:35 -04001225 WRITE_LOCK(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001226 r = __remove(cmd, cblock);
Joe Thornber028ae9f2015-04-22 16:42:35 -04001227 WRITE_UNLOCK(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001228
1229 return r;
1230}
1231
1232static int __insert(struct dm_cache_metadata *cmd,
1233 dm_cblock_t cblock, dm_oblock_t oblock)
1234{
1235 int r;
1236 __le64 value = pack_value(oblock, M_VALID);
1237 __dm_bless_for_disk(&value);
1238
1239 r = dm_array_set_value(&cmd->info, cmd->root, from_cblock(cblock),
1240 &value, &cmd->root);
1241 if (r)
1242 return r;
1243
1244 cmd->changed = true;
1245 return 0;
1246}
1247
1248int dm_cache_insert_mapping(struct dm_cache_metadata *cmd,
1249 dm_cblock_t cblock, dm_oblock_t oblock)
1250{
1251 int r;
1252
Joe Thornber028ae9f2015-04-22 16:42:35 -04001253 WRITE_LOCK(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001254 r = __insert(cmd, cblock, oblock);
Joe Thornber028ae9f2015-04-22 16:42:35 -04001255 WRITE_UNLOCK(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001256
1257 return r;
1258}
1259
1260struct thunk {
1261 load_mapping_fn fn;
1262 void *context;
1263
1264 struct dm_cache_metadata *cmd;
1265 bool respect_dirty_flags;
1266 bool hints_valid;
1267};
1268
Mike Snitzerea2dd8c2013-03-20 17:21:28 +00001269static bool policy_unchanged(struct dm_cache_metadata *cmd,
1270 struct dm_cache_policy *policy)
1271{
1272 const char *policy_name = dm_cache_policy_get_name(policy);
1273 const unsigned *policy_version = dm_cache_policy_get_version(policy);
1274 size_t policy_hint_size = dm_cache_policy_get_hint_size(policy);
1275
1276 /*
1277 * Ensure policy names match.
1278 */
1279 if (strncmp(cmd->policy_name, policy_name, sizeof(cmd->policy_name)))
1280 return false;
1281
1282 /*
1283 * Ensure policy major versions match.
1284 */
1285 if (cmd->policy_version[0] != policy_version[0])
1286 return false;
1287
1288 /*
1289 * Ensure policy hint sizes match.
1290 */
1291 if (cmd->policy_hint_size != policy_hint_size)
1292 return false;
1293
1294 return true;
1295}
1296
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001297static bool hints_array_initialized(struct dm_cache_metadata *cmd)
1298{
1299 return cmd->hint_root && cmd->policy_hint_size;
1300}
1301
1302static bool hints_array_available(struct dm_cache_metadata *cmd,
Mike Snitzerea2dd8c2013-03-20 17:21:28 +00001303 struct dm_cache_policy *policy)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001304{
Mike Snitzerea2dd8c2013-03-20 17:21:28 +00001305 return cmd->clean_when_opened && policy_unchanged(cmd, policy) &&
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001306 hints_array_initialized(cmd);
1307}
1308
Joe Thornber629d0a82016-09-22 06:15:21 -04001309static int __load_mapping_v1(struct dm_cache_metadata *cmd,
1310 uint64_t cb, bool hints_valid,
1311 struct dm_array_cursor *mapping_cursor,
1312 struct dm_array_cursor *hint_cursor,
1313 load_mapping_fn fn, void *context)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001314{
1315 int r = 0;
Joe Thornberf1779402016-09-20 06:16:00 -04001316
1317 __le64 mapping;
1318 __le32 hint = 0;
1319
1320 __le64 *mapping_value_le;
1321 __le32 *hint_value_le;
1322
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001323 dm_oblock_t oblock;
1324 unsigned flags;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001325
Joe Thornberf1779402016-09-20 06:16:00 -04001326 dm_array_cursor_get_value(mapping_cursor, (void **) &mapping_value_le);
1327 memcpy(&mapping, mapping_value_le, sizeof(mapping));
1328 unpack_value(mapping, &oblock, &flags);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001329
1330 if (flags & M_VALID) {
Joe Thornberf1779402016-09-20 06:16:00 -04001331 if (hints_valid) {
1332 dm_array_cursor_get_value(hint_cursor, (void **) &hint_value_le);
1333 memcpy(&hint, hint_value_le, sizeof(hint));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001334 }
1335
Joe Thornberf1779402016-09-20 06:16:00 -04001336 r = fn(context, oblock, to_cblock(cb), flags & M_DIRTY,
1337 le32_to_cpu(hint), hints_valid);
Mike Snitzer48551052016-10-04 15:22:17 -04001338 if (r) {
1339 DMERR("policy couldn't load cache block %llu",
1340 (unsigned long long) from_cblock(to_cblock(cb)));
1341 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001342 }
1343
1344 return r;
1345}
1346
Joe Thornber629d0a82016-09-22 06:15:21 -04001347static int __load_mapping_v2(struct dm_cache_metadata *cmd,
1348 uint64_t cb, bool hints_valid,
1349 struct dm_array_cursor *mapping_cursor,
1350 struct dm_array_cursor *hint_cursor,
1351 struct dm_bitset_cursor *dirty_cursor,
1352 load_mapping_fn fn, void *context)
1353{
1354 int r = 0;
1355
1356 __le64 mapping;
1357 __le32 hint = 0;
1358
1359 __le64 *mapping_value_le;
1360 __le32 *hint_value_le;
1361
1362 dm_oblock_t oblock;
1363 unsigned flags;
1364 bool dirty;
1365
1366 dm_array_cursor_get_value(mapping_cursor, (void **) &mapping_value_le);
1367 memcpy(&mapping, mapping_value_le, sizeof(mapping));
1368 unpack_value(mapping, &oblock, &flags);
1369
1370 if (flags & M_VALID) {
1371 if (hints_valid) {
1372 dm_array_cursor_get_value(hint_cursor, (void **) &hint_value_le);
1373 memcpy(&hint, hint_value_le, sizeof(hint));
1374 }
1375
1376 dirty = dm_bitset_cursor_get_value(dirty_cursor);
1377 r = fn(context, oblock, to_cblock(cb), dirty,
1378 le32_to_cpu(hint), hints_valid);
Mike Snitzer48551052016-10-04 15:22:17 -04001379 if (r) {
1380 DMERR("policy couldn't load cache block %llu",
1381 (unsigned long long) from_cblock(to_cblock(cb)));
1382 }
Joe Thornber629d0a82016-09-22 06:15:21 -04001383 }
1384
1385 return r;
1386}
1387
Mike Snitzerea2dd8c2013-03-20 17:21:28 +00001388static int __load_mappings(struct dm_cache_metadata *cmd,
1389 struct dm_cache_policy *policy,
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001390 load_mapping_fn fn, void *context)
1391{
Joe Thornberf1779402016-09-20 06:16:00 -04001392 int r;
1393 uint64_t cb;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001394
Joe Thornberf1779402016-09-20 06:16:00 -04001395 bool hints_valid = hints_array_available(cmd, policy);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001396
Joe Thornberf1779402016-09-20 06:16:00 -04001397 if (from_cblock(cmd->cache_blocks) == 0)
1398 /* Nothing to do */
1399 return 0;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001400
Joe Thornberf1779402016-09-20 06:16:00 -04001401 r = dm_array_cursor_begin(&cmd->info, cmd->root, &cmd->mapping_cursor);
1402 if (r)
1403 return r;
1404
1405 if (hints_valid) {
1406 r = dm_array_cursor_begin(&cmd->hint_info, cmd->hint_root, &cmd->hint_cursor);
1407 if (r) {
1408 dm_array_cursor_end(&cmd->mapping_cursor);
1409 return r;
1410 }
1411 }
1412
Joe Thornber629d0a82016-09-22 06:15:21 -04001413 if (separate_dirty_bits(cmd)) {
1414 r = dm_bitset_cursor_begin(&cmd->dirty_info, cmd->dirty_root,
1415 from_cblock(cmd->cache_blocks),
1416 &cmd->dirty_cursor);
1417 if (r) {
1418 dm_array_cursor_end(&cmd->hint_cursor);
1419 dm_array_cursor_end(&cmd->mapping_cursor);
1420 return r;
1421 }
1422 }
1423
Joe Thornberf1779402016-09-20 06:16:00 -04001424 for (cb = 0; ; cb++) {
Joe Thornber629d0a82016-09-22 06:15:21 -04001425 if (separate_dirty_bits(cmd))
1426 r = __load_mapping_v2(cmd, cb, hints_valid,
1427 &cmd->mapping_cursor,
1428 &cmd->hint_cursor,
1429 &cmd->dirty_cursor,
1430 fn, context);
1431 else
1432 r = __load_mapping_v1(cmd, cb, hints_valid,
1433 &cmd->mapping_cursor, &cmd->hint_cursor,
1434 fn, context);
Joe Thornberf1779402016-09-20 06:16:00 -04001435 if (r)
1436 goto out;
1437
1438 /*
1439 * We need to break out before we move the cursors.
1440 */
1441 if (cb >= (from_cblock(cmd->cache_blocks) - 1))
1442 break;
1443
1444 r = dm_array_cursor_next(&cmd->mapping_cursor);
1445 if (r) {
1446 DMERR("dm_array_cursor_next for mapping failed");
1447 goto out;
1448 }
1449
1450 if (hints_valid) {
1451 r = dm_array_cursor_next(&cmd->hint_cursor);
1452 if (r) {
1453 DMERR("dm_array_cursor_next for hint failed");
1454 goto out;
1455 }
1456 }
Joe Thornber629d0a82016-09-22 06:15:21 -04001457
1458 if (separate_dirty_bits(cmd)) {
1459 r = dm_bitset_cursor_next(&cmd->dirty_cursor);
1460 if (r) {
1461 DMERR("dm_bitset_cursor_next for dirty failed");
1462 goto out;
1463 }
1464 }
Joe Thornberf1779402016-09-20 06:16:00 -04001465 }
1466out:
1467 dm_array_cursor_end(&cmd->mapping_cursor);
1468 if (hints_valid)
1469 dm_array_cursor_end(&cmd->hint_cursor);
1470
Joe Thornber629d0a82016-09-22 06:15:21 -04001471 if (separate_dirty_bits(cmd))
1472 dm_bitset_cursor_end(&cmd->dirty_cursor);
1473
Joe Thornberf1779402016-09-20 06:16:00 -04001474 return r;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001475}
1476
Mike Snitzerea2dd8c2013-03-20 17:21:28 +00001477int dm_cache_load_mappings(struct dm_cache_metadata *cmd,
1478 struct dm_cache_policy *policy,
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001479 load_mapping_fn fn, void *context)
1480{
1481 int r;
1482
Joe Thornberd14fcf32016-03-10 16:20:58 +00001483 READ_LOCK(cmd);
Mike Snitzerea2dd8c2013-03-20 17:21:28 +00001484 r = __load_mappings(cmd, policy, fn, context);
Joe Thornberd14fcf32016-03-10 16:20:58 +00001485 READ_UNLOCK(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001486
1487 return r;
1488}
1489
1490static int __dump_mapping(void *context, uint64_t cblock, void *leaf)
1491{
1492 int r = 0;
1493 __le64 value;
1494 dm_oblock_t oblock;
1495 unsigned flags;
1496
1497 memcpy(&value, leaf, sizeof(value));
1498 unpack_value(value, &oblock, &flags);
1499
1500 return r;
1501}
1502
1503static int __dump_mappings(struct dm_cache_metadata *cmd)
1504{
1505 return dm_array_walk(&cmd->info, cmd->root, __dump_mapping, NULL);
1506}
1507
1508void dm_cache_dump(struct dm_cache_metadata *cmd)
1509{
Joe Thornberd14fcf32016-03-10 16:20:58 +00001510 READ_LOCK_VOID(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001511 __dump_mappings(cmd);
Joe Thornberd14fcf32016-03-10 16:20:58 +00001512 READ_UNLOCK(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001513}
1514
1515int dm_cache_changed_this_transaction(struct dm_cache_metadata *cmd)
1516{
1517 int r;
1518
Joe Thornberd14fcf32016-03-10 16:20:58 +00001519 READ_LOCK(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001520 r = cmd->changed;
Joe Thornberd14fcf32016-03-10 16:20:58 +00001521 READ_UNLOCK(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001522
1523 return r;
1524}
1525
1526static int __dirty(struct dm_cache_metadata *cmd, dm_cblock_t cblock, bool dirty)
1527{
1528 int r;
1529 unsigned flags;
1530 dm_oblock_t oblock;
1531 __le64 value;
1532
1533 r = dm_array_get_value(&cmd->info, cmd->root, from_cblock(cblock), &value);
1534 if (r)
1535 return r;
1536
1537 unpack_value(value, &oblock, &flags);
1538
1539 if (((flags & M_DIRTY) && dirty) || (!(flags & M_DIRTY) && !dirty))
1540 /* nothing to be done */
1541 return 0;
1542
Joe Thornber79ed9ca2013-03-20 17:21:27 +00001543 value = pack_value(oblock, (flags & ~M_DIRTY) | (dirty ? M_DIRTY : 0));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001544 __dm_bless_for_disk(&value);
1545
1546 r = dm_array_set_value(&cmd->info, cmd->root, from_cblock(cblock),
1547 &value, &cmd->root);
1548 if (r)
1549 return r;
1550
1551 cmd->changed = true;
1552 return 0;
1553
1554}
1555
Joe Thornber629d0a82016-09-22 06:15:21 -04001556static int __set_dirty_bits_v1(struct dm_cache_metadata *cmd, unsigned nr_bits, unsigned long *bits)
1557{
1558 int r;
1559 unsigned i;
1560 for (i = 0; i < nr_bits; i++) {
1561 r = __dirty(cmd, to_cblock(i), test_bit(i, bits));
1562 if (r)
1563 return r;
1564 }
1565
1566 return 0;
1567}
1568
Joe Thornber683bb1a2016-09-22 10:45:21 -04001569static int is_dirty_callback(uint32_t index, bool *value, void *context)
1570{
1571 unsigned long *bits = context;
1572 *value = test_bit(index, bits);
1573 return 0;
1574}
1575
Joe Thornber629d0a82016-09-22 06:15:21 -04001576static int __set_dirty_bits_v2(struct dm_cache_metadata *cmd, unsigned nr_bits, unsigned long *bits)
1577{
1578 int r = 0;
Joe Thornber629d0a82016-09-22 06:15:21 -04001579
1580 /* nr_bits is really just a sanity check */
1581 if (nr_bits != from_cblock(cmd->cache_blocks)) {
1582 DMERR("dirty bitset is wrong size");
1583 return -EINVAL;
1584 }
1585
Joe Thornber683bb1a2016-09-22 10:45:21 -04001586 r = dm_bitset_del(&cmd->dirty_info, cmd->dirty_root);
1587 if (r)
1588 return r;
Joe Thornber629d0a82016-09-22 06:15:21 -04001589
1590 cmd->changed = true;
Joe Thornber683bb1a2016-09-22 10:45:21 -04001591 return dm_bitset_new(&cmd->dirty_info, &cmd->dirty_root, nr_bits, is_dirty_callback, bits);
Joe Thornber629d0a82016-09-22 06:15:21 -04001592}
1593
1594int dm_cache_set_dirty_bits(struct dm_cache_metadata *cmd,
1595 unsigned nr_bits,
1596 unsigned long *bits)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001597{
1598 int r;
1599
Joe Thornber028ae9f2015-04-22 16:42:35 -04001600 WRITE_LOCK(cmd);
Joe Thornber629d0a82016-09-22 06:15:21 -04001601 if (separate_dirty_bits(cmd))
1602 r = __set_dirty_bits_v2(cmd, nr_bits, bits);
1603 else
1604 r = __set_dirty_bits_v1(cmd, nr_bits, bits);
Joe Thornber028ae9f2015-04-22 16:42:35 -04001605 WRITE_UNLOCK(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001606
1607 return r;
1608}
1609
1610void dm_cache_metadata_get_stats(struct dm_cache_metadata *cmd,
1611 struct dm_cache_statistics *stats)
1612{
Joe Thornberd14fcf32016-03-10 16:20:58 +00001613 READ_LOCK_VOID(cmd);
Joe Thornber8c5008f2013-05-10 14:37:18 +01001614 *stats = cmd->stats;
Joe Thornberd14fcf32016-03-10 16:20:58 +00001615 READ_UNLOCK(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001616}
1617
1618void dm_cache_metadata_set_stats(struct dm_cache_metadata *cmd,
1619 struct dm_cache_statistics *stats)
1620{
Joe Thornber028ae9f2015-04-22 16:42:35 -04001621 WRITE_LOCK_VOID(cmd);
Joe Thornber8c5008f2013-05-10 14:37:18 +01001622 cmd->stats = *stats;
Joe Thornber028ae9f2015-04-22 16:42:35 -04001623 WRITE_UNLOCK(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001624}
1625
1626int dm_cache_commit(struct dm_cache_metadata *cmd, bool clean_shutdown)
1627{
Mike Snitzer10add84e2017-05-05 14:40:13 -04001628 int r = -EINVAL;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001629 flags_mutator mutator = (clean_shutdown ? set_clean_shutdown :
1630 clear_clean_shutdown);
1631
Joe Thornber028ae9f2015-04-22 16:42:35 -04001632 WRITE_LOCK(cmd);
Mike Snitzer10add84e2017-05-05 14:40:13 -04001633 if (cmd->fail_io)
1634 goto out;
1635
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001636 r = __commit_transaction(cmd, mutator);
1637 if (r)
1638 goto out;
1639
1640 r = __begin_transaction(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001641out:
Joe Thornber028ae9f2015-04-22 16:42:35 -04001642 WRITE_UNLOCK(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001643 return r;
1644}
1645
1646int dm_cache_get_free_metadata_block_count(struct dm_cache_metadata *cmd,
1647 dm_block_t *result)
1648{
1649 int r = -EINVAL;
1650
Joe Thornberd14fcf32016-03-10 16:20:58 +00001651 READ_LOCK(cmd);
Mike Snitzer10add84e2017-05-05 14:40:13 -04001652 if (!cmd->fail_io)
1653 r = dm_sm_get_nr_free(cmd->metadata_sm, result);
Joe Thornberd14fcf32016-03-10 16:20:58 +00001654 READ_UNLOCK(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001655
1656 return r;
1657}
1658
1659int dm_cache_get_metadata_dev_size(struct dm_cache_metadata *cmd,
1660 dm_block_t *result)
1661{
1662 int r = -EINVAL;
1663
Joe Thornberd14fcf32016-03-10 16:20:58 +00001664 READ_LOCK(cmd);
Mike Snitzer10add84e2017-05-05 14:40:13 -04001665 if (!cmd->fail_io)
1666 r = dm_sm_get_nr_blocks(cmd->metadata_sm, result);
Joe Thornberd14fcf32016-03-10 16:20:58 +00001667 READ_UNLOCK(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001668
1669 return r;
1670}
1671
1672/*----------------------------------------------------------------*/
1673
Joe Thornber4e781b42016-09-15 09:23:46 -04001674static int get_hint(uint32_t index, void *value_le, void *context)
1675{
1676 uint32_t value;
1677 struct dm_cache_policy *policy = context;
1678
1679 value = policy_get_hint(policy, to_cblock(index));
1680 *((__le32 *) value_le) = cpu_to_le32(value);
1681
1682 return 0;
1683}
1684
1685/*
1686 * It's quicker to always delete the hint array, and recreate with
1687 * dm_array_new().
1688 */
1689static int write_hints(struct dm_cache_metadata *cmd, struct dm_cache_policy *policy)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001690{
1691 int r;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001692 size_t hint_size;
1693 const char *policy_name = dm_cache_policy_get_name(policy);
Mike Snitzer4e7f5062013-03-20 17:21:27 +00001694 const unsigned *policy_version = dm_cache_policy_get_version(policy);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001695
1696 if (!policy_name[0] ||
1697 (strlen(policy_name) > sizeof(cmd->policy_name) - 1))
1698 return -EINVAL;
1699
Joe Thornber4e781b42016-09-15 09:23:46 -04001700 strncpy(cmd->policy_name, policy_name, sizeof(cmd->policy_name));
1701 memcpy(cmd->policy_version, policy_version, sizeof(cmd->policy_version));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001702
Joe Thornber4e781b42016-09-15 09:23:46 -04001703 hint_size = dm_cache_policy_get_hint_size(policy);
1704 if (!hint_size)
1705 return 0; /* short-circuit hints initialization */
1706 cmd->policy_hint_size = hint_size;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001707
Joe Thornber4e781b42016-09-15 09:23:46 -04001708 if (cmd->hint_root) {
1709 r = dm_array_del(&cmd->hint_info, cmd->hint_root);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001710 if (r)
1711 return r;
1712 }
1713
Joe Thornber4e781b42016-09-15 09:23:46 -04001714 return dm_array_new(&cmd->hint_info, &cmd->hint_root,
1715 from_cblock(cmd->cache_blocks),
1716 get_hint, policy);
Joe Thornber05966612014-04-03 16:16:44 +01001717}
1718
1719int dm_cache_write_hints(struct dm_cache_metadata *cmd, struct dm_cache_policy *policy)
1720{
1721 int r;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001722
Joe Thornber028ae9f2015-04-22 16:42:35 -04001723 WRITE_LOCK(cmd);
Joe Thornber05966612014-04-03 16:16:44 +01001724 r = write_hints(cmd, policy);
Joe Thornber028ae9f2015-04-22 16:42:35 -04001725 WRITE_UNLOCK(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001726
1727 return r;
1728}
Joe Thornber2ee57d52013-10-24 14:10:29 -04001729
1730int dm_cache_metadata_all_clean(struct dm_cache_metadata *cmd, bool *result)
1731{
Joe Thornberd14fcf32016-03-10 16:20:58 +00001732 int r;
1733
1734 READ_LOCK(cmd);
1735 r = blocks_are_unmapped_or_clean(cmd, 0, cmd->cache_blocks, result);
1736 READ_UNLOCK(cmd);
1737
1738 return r;
Joe Thornber2ee57d52013-10-24 14:10:29 -04001739}
Joe Thornber028ae9f2015-04-22 16:42:35 -04001740
1741void dm_cache_metadata_set_read_only(struct dm_cache_metadata *cmd)
1742{
1743 WRITE_LOCK_VOID(cmd);
1744 dm_bm_set_read_only(cmd->bm);
1745 WRITE_UNLOCK(cmd);
1746}
1747
1748void dm_cache_metadata_set_read_write(struct dm_cache_metadata *cmd)
1749{
1750 WRITE_LOCK_VOID(cmd);
1751 dm_bm_set_read_write(cmd->bm);
1752 WRITE_UNLOCK(cmd);
1753}
1754
1755int dm_cache_metadata_set_needs_check(struct dm_cache_metadata *cmd)
1756{
1757 int r;
1758 struct dm_block *sblock;
1759 struct cache_disk_superblock *disk_super;
1760
Joe Thornberd14fcf32016-03-10 16:20:58 +00001761 WRITE_LOCK(cmd);
Joe Thornber028ae9f2015-04-22 16:42:35 -04001762 set_bit(NEEDS_CHECK, &cmd->flags);
1763
1764 r = superblock_lock(cmd, &sblock);
1765 if (r) {
1766 DMERR("couldn't read superblock");
1767 goto out;
1768 }
1769
1770 disk_super = dm_block_data(sblock);
1771 disk_super->flags = cpu_to_le32(cmd->flags);
1772
1773 dm_bm_unlock(sblock);
1774
1775out:
Joe Thornberd14fcf32016-03-10 16:20:58 +00001776 WRITE_UNLOCK(cmd);
Joe Thornber028ae9f2015-04-22 16:42:35 -04001777 return r;
1778}
1779
Joe Thornberd14fcf32016-03-10 16:20:58 +00001780int dm_cache_metadata_needs_check(struct dm_cache_metadata *cmd, bool *result)
Joe Thornber028ae9f2015-04-22 16:42:35 -04001781{
Joe Thornberd14fcf32016-03-10 16:20:58 +00001782 READ_LOCK(cmd);
1783 *result = !!test_bit(NEEDS_CHECK, &cmd->flags);
1784 READ_UNLOCK(cmd);
Joe Thornber028ae9f2015-04-22 16:42:35 -04001785
Joe Thornberd14fcf32016-03-10 16:20:58 +00001786 return 0;
Joe Thornber028ae9f2015-04-22 16:42:35 -04001787}
1788
1789int dm_cache_metadata_abort(struct dm_cache_metadata *cmd)
1790{
1791 int r;
1792
1793 WRITE_LOCK(cmd);
1794 __destroy_persistent_data_objects(cmd);
1795 r = __create_persistent_data_objects(cmd, false);
1796 if (r)
1797 cmd->fail_io = true;
1798 WRITE_UNLOCK(cmd);
1799
1800 return r;
1801}