blob: 2874f222c31383dc25f3b3ef81c2983a528653d9 [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;
Christoph Hellwig6dcbb522021-10-18 12:11:05 +0200337 sector_t bdev_size = bdev_nr_sectors(cmd->bdev);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000338
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));
Mike Snitzerfd2fa952018-08-02 16:08:52 -0400366 disk_super->policy_hint_size = cpu_to_le32(0);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000367
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 */
Christoph Hellwig1e0dcca2021-01-09 11:42:49 +0100452 if (bdev_read_only(cmd->bdev))
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000453 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");
Ye Bind16ff192020-09-01 14:25:42 +0800540 r = PTR_ERR(cmd->bm);
541 cmd->bm = NULL;
542 return r;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000543 }
544
545 r = __open_or_format_metadata(cmd, may_format_device);
Ye Bind16ff192020-09-01 14:25:42 +0800546 if (r) {
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000547 dm_block_manager_destroy(cmd->bm);
Ye Bind16ff192020-09-01 14:25:42 +0800548 cmd->bm = NULL;
549 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000550
551 return r;
552}
553
554static void __destroy_persistent_data_objects(struct dm_cache_metadata *cmd)
555{
556 dm_sm_destroy(cmd->metadata_sm);
557 dm_tm_destroy(cmd->tm);
558 dm_block_manager_destroy(cmd->bm);
559}
560
561typedef unsigned long (*flags_mutator)(unsigned long);
562
563static void update_flags(struct cache_disk_superblock *disk_super,
564 flags_mutator mutator)
565{
566 uint32_t sb_flags = mutator(le32_to_cpu(disk_super->flags));
567 disk_super->flags = cpu_to_le32(sb_flags);
568}
569
570static unsigned long set_clean_shutdown(unsigned long flags)
571{
572 set_bit(CLEAN_SHUTDOWN, &flags);
573 return flags;
574}
575
576static unsigned long clear_clean_shutdown(unsigned long flags)
577{
578 clear_bit(CLEAN_SHUTDOWN, &flags);
579 return flags;
580}
581
582static void read_superblock_fields(struct dm_cache_metadata *cmd,
583 struct cache_disk_superblock *disk_super)
584{
Joe Thornber629d0a82016-09-22 06:15:21 -0400585 cmd->version = le32_to_cpu(disk_super->version);
Joe Thornber028ae9f2015-04-22 16:42:35 -0400586 cmd->flags = le32_to_cpu(disk_super->flags);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000587 cmd->root = le64_to_cpu(disk_super->mapping_root);
588 cmd->hint_root = le64_to_cpu(disk_super->hint_root);
589 cmd->discard_root = le64_to_cpu(disk_super->discard_root);
590 cmd->discard_block_size = le64_to_cpu(disk_super->discard_block_size);
Joe Thornber1bad9bc2014-11-07 14:47:07 +0000591 cmd->discard_nr_blocks = to_dblock(le64_to_cpu(disk_super->discard_nr_blocks));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000592 cmd->data_block_size = le32_to_cpu(disk_super->data_block_size);
593 cmd->cache_blocks = to_cblock(le32_to_cpu(disk_super->cache_blocks));
594 strncpy(cmd->policy_name, disk_super->policy_name, sizeof(cmd->policy_name));
Mike Snitzer4e7f5062013-03-20 17:21:27 +0000595 cmd->policy_version[0] = le32_to_cpu(disk_super->policy_version[0]);
596 cmd->policy_version[1] = le32_to_cpu(disk_super->policy_version[1]);
597 cmd->policy_version[2] = le32_to_cpu(disk_super->policy_version[2]);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000598 cmd->policy_hint_size = le32_to_cpu(disk_super->policy_hint_size);
599
600 cmd->stats.read_hits = le32_to_cpu(disk_super->read_hits);
601 cmd->stats.read_misses = le32_to_cpu(disk_super->read_misses);
602 cmd->stats.write_hits = le32_to_cpu(disk_super->write_hits);
603 cmd->stats.write_misses = le32_to_cpu(disk_super->write_misses);
604
Joe Thornber629d0a82016-09-22 06:15:21 -0400605 if (separate_dirty_bits(cmd))
606 cmd->dirty_root = le64_to_cpu(disk_super->dirty_root);
607
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000608 cmd->changed = false;
609}
610
611/*
612 * The mutator updates the superblock flags.
613 */
614static int __begin_transaction_flags(struct dm_cache_metadata *cmd,
615 flags_mutator mutator)
616{
617 int r;
618 struct cache_disk_superblock *disk_super;
619 struct dm_block *sblock;
620
621 r = superblock_lock(cmd, &sblock);
622 if (r)
623 return r;
624
625 disk_super = dm_block_data(sblock);
626 update_flags(disk_super, mutator);
627 read_superblock_fields(cmd, disk_super);
Joe Thornbera9d45392014-03-27 14:13:20 +0000628 dm_bm_unlock(sblock);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000629
Joe Thornbera9d45392014-03-27 14:13:20 +0000630 return dm_bm_flush(cmd->bm);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000631}
632
633static int __begin_transaction(struct dm_cache_metadata *cmd)
634{
635 int r;
636 struct cache_disk_superblock *disk_super;
637 struct dm_block *sblock;
638
639 /*
640 * We re-read the superblock every time. Shouldn't need to do this
641 * really.
642 */
643 r = superblock_read_lock(cmd, &sblock);
644 if (r)
645 return r;
646
647 disk_super = dm_block_data(sblock);
648 read_superblock_fields(cmd, disk_super);
649 dm_bm_unlock(sblock);
650
651 return 0;
652}
653
654static int __commit_transaction(struct dm_cache_metadata *cmd,
655 flags_mutator mutator)
656{
657 int r;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000658 struct cache_disk_superblock *disk_super;
659 struct dm_block *sblock;
660
661 /*
662 * We need to know if the cache_disk_superblock exceeds a 512-byte sector.
663 */
664 BUILD_BUG_ON(sizeof(struct cache_disk_superblock) > 512);
665
Joe Thornber629d0a82016-09-22 06:15:21 -0400666 if (separate_dirty_bits(cmd)) {
667 r = dm_bitset_flush(&cmd->dirty_info, cmd->dirty_root,
668 &cmd->dirty_root);
669 if (r)
670 return r;
671 }
672
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000673 r = dm_bitset_flush(&cmd->discard_info, cmd->discard_root,
674 &cmd->discard_root);
675 if (r)
676 return r;
677
678 r = dm_tm_pre_commit(cmd->tm);
679 if (r < 0)
680 return r;
681
Joe Thornber5a320832014-03-27 14:13:23 +0000682 r = __save_sm_root(cmd);
683 if (r)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000684 return r;
685
686 r = superblock_lock(cmd, &sblock);
687 if (r)
688 return r;
689
690 disk_super = dm_block_data(sblock);
691
Joe Thornber3201ac42015-10-22 18:10:55 +0100692 disk_super->flags = cpu_to_le32(cmd->flags);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000693 if (mutator)
694 update_flags(disk_super, mutator);
695
696 disk_super->mapping_root = cpu_to_le64(cmd->root);
Joe Thornber629d0a82016-09-22 06:15:21 -0400697 if (separate_dirty_bits(cmd))
698 disk_super->dirty_root = cpu_to_le64(cmd->dirty_root);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000699 disk_super->hint_root = cpu_to_le64(cmd->hint_root);
700 disk_super->discard_root = cpu_to_le64(cmd->discard_root);
701 disk_super->discard_block_size = cpu_to_le64(cmd->discard_block_size);
Joe Thornber1bad9bc2014-11-07 14:47:07 +0000702 disk_super->discard_nr_blocks = cpu_to_le64(from_dblock(cmd->discard_nr_blocks));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000703 disk_super->cache_blocks = cpu_to_le32(from_cblock(cmd->cache_blocks));
704 strncpy(disk_super->policy_name, cmd->policy_name, sizeof(disk_super->policy_name));
Mike Snitzer4e7f5062013-03-20 17:21:27 +0000705 disk_super->policy_version[0] = cpu_to_le32(cmd->policy_version[0]);
706 disk_super->policy_version[1] = cpu_to_le32(cmd->policy_version[1]);
707 disk_super->policy_version[2] = cpu_to_le32(cmd->policy_version[2]);
Mike Snitzerfd2fa952018-08-02 16:08:52 -0400708 disk_super->policy_hint_size = cpu_to_le32(cmd->policy_hint_size);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000709
710 disk_super->read_hits = cpu_to_le32(cmd->stats.read_hits);
711 disk_super->read_misses = cpu_to_le32(cmd->stats.read_misses);
712 disk_super->write_hits = cpu_to_le32(cmd->stats.write_hits);
713 disk_super->write_misses = cpu_to_le32(cmd->stats.write_misses);
Joe Thornber5a320832014-03-27 14:13:23 +0000714 __copy_sm_root(cmd, disk_super);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000715
716 return dm_tm_commit(cmd->tm, sblock);
717}
718
719/*----------------------------------------------------------------*/
720
721/*
722 * The mappings are held in a dm-array that has 64-bit values stored in
723 * little-endian format. The index is the cblock, the high 48bits of the
724 * value are the oblock and the low 16 bit the flags.
725 */
726#define FLAGS_MASK ((1 << 16) - 1)
727
728static __le64 pack_value(dm_oblock_t block, unsigned flags)
729{
730 uint64_t value = from_oblock(block);
731 value <<= 16;
732 value = value | (flags & FLAGS_MASK);
733 return cpu_to_le64(value);
734}
735
736static void unpack_value(__le64 value_le, dm_oblock_t *block, unsigned *flags)
737{
738 uint64_t value = le64_to_cpu(value_le);
739 uint64_t b = value >> 16;
740 *block = to_oblock(b);
741 *flags = value & FLAGS_MASK;
742}
743
744/*----------------------------------------------------------------*/
745
Joe Thornber9b1cc9f2015-01-23 10:00:07 +0000746static struct dm_cache_metadata *metadata_open(struct block_device *bdev,
747 sector_t data_block_size,
748 bool may_format_device,
Joe Thornber629d0a82016-09-22 06:15:21 -0400749 size_t policy_hint_size,
750 unsigned metadata_version)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000751{
752 int r;
753 struct dm_cache_metadata *cmd;
754
755 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
756 if (!cmd) {
757 DMERR("could not allocate metadata struct");
Joe Thornber766a7882015-01-28 12:07:46 +0000758 return ERR_PTR(-ENOMEM);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000759 }
760
Joe Thornber629d0a82016-09-22 06:15:21 -0400761 cmd->version = metadata_version;
Elena Reshetova6bdd0792017-10-20 10:37:37 +0300762 refcount_set(&cmd->ref_count, 1);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000763 init_rwsem(&cmd->root_lock);
764 cmd->bdev = bdev;
765 cmd->data_block_size = data_block_size;
766 cmd->cache_blocks = 0;
767 cmd->policy_hint_size = policy_hint_size;
768 cmd->changed = true;
Joe Thornber028ae9f2015-04-22 16:42:35 -0400769 cmd->fail_io = false;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000770
771 r = __create_persistent_data_objects(cmd, may_format_device);
772 if (r) {
773 kfree(cmd);
774 return ERR_PTR(r);
775 }
776
777 r = __begin_transaction_flags(cmd, clear_clean_shutdown);
778 if (r < 0) {
779 dm_cache_metadata_close(cmd);
780 return ERR_PTR(r);
781 }
782
783 return cmd;
784}
785
Joe Thornber9b1cc9f2015-01-23 10:00:07 +0000786/*
787 * We keep a little list of ref counted metadata objects to prevent two
788 * different target instances creating separate bufio instances. This is
789 * an issue if a table is reloaded before the suspend.
790 */
791static DEFINE_MUTEX(table_lock);
792static LIST_HEAD(table);
793
794static struct dm_cache_metadata *lookup(struct block_device *bdev)
795{
796 struct dm_cache_metadata *cmd;
797
798 list_for_each_entry(cmd, &table, list)
799 if (cmd->bdev == bdev) {
Elena Reshetova6bdd0792017-10-20 10:37:37 +0300800 refcount_inc(&cmd->ref_count);
Joe Thornber9b1cc9f2015-01-23 10:00:07 +0000801 return cmd;
802 }
803
804 return NULL;
805}
806
807static struct dm_cache_metadata *lookup_or_open(struct block_device *bdev,
808 sector_t data_block_size,
809 bool may_format_device,
Joe Thornber629d0a82016-09-22 06:15:21 -0400810 size_t policy_hint_size,
811 unsigned metadata_version)
Joe Thornber9b1cc9f2015-01-23 10:00:07 +0000812{
813 struct dm_cache_metadata *cmd, *cmd2;
814
815 mutex_lock(&table_lock);
816 cmd = lookup(bdev);
817 mutex_unlock(&table_lock);
818
819 if (cmd)
820 return cmd;
821
Joe Thornber629d0a82016-09-22 06:15:21 -0400822 cmd = metadata_open(bdev, data_block_size, may_format_device,
823 policy_hint_size, metadata_version);
Joe Thornber766a7882015-01-28 12:07:46 +0000824 if (!IS_ERR(cmd)) {
Joe Thornber9b1cc9f2015-01-23 10:00:07 +0000825 mutex_lock(&table_lock);
826 cmd2 = lookup(bdev);
827 if (cmd2) {
828 mutex_unlock(&table_lock);
829 __destroy_persistent_data_objects(cmd);
830 kfree(cmd);
831 return cmd2;
832 }
833 list_add(&cmd->list, &table);
834 mutex_unlock(&table_lock);
835 }
836
837 return cmd;
838}
839
840static bool same_params(struct dm_cache_metadata *cmd, sector_t data_block_size)
841{
842 if (cmd->data_block_size != data_block_size) {
Mike Snitzer07d93882016-10-03 11:56:22 -0400843 DMERR("data_block_size (%llu) different from that in metadata (%llu)",
Joe Thornber9b1cc9f2015-01-23 10:00:07 +0000844 (unsigned long long) data_block_size,
845 (unsigned long long) cmd->data_block_size);
846 return false;
847 }
848
849 return true;
850}
851
852struct dm_cache_metadata *dm_cache_metadata_open(struct block_device *bdev,
853 sector_t data_block_size,
854 bool may_format_device,
Joe Thornber629d0a82016-09-22 06:15:21 -0400855 size_t policy_hint_size,
856 unsigned metadata_version)
Joe Thornber9b1cc9f2015-01-23 10:00:07 +0000857{
Joe Thornber629d0a82016-09-22 06:15:21 -0400858 struct dm_cache_metadata *cmd = lookup_or_open(bdev, data_block_size, may_format_device,
859 policy_hint_size, metadata_version);
Joe Thornber766a7882015-01-28 12:07:46 +0000860
861 if (!IS_ERR(cmd) && !same_params(cmd, data_block_size)) {
Joe Thornber9b1cc9f2015-01-23 10:00:07 +0000862 dm_cache_metadata_close(cmd);
Joe Thornber766a7882015-01-28 12:07:46 +0000863 return ERR_PTR(-EINVAL);
Joe Thornber9b1cc9f2015-01-23 10:00:07 +0000864 }
865
866 return cmd;
867}
868
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000869void dm_cache_metadata_close(struct dm_cache_metadata *cmd)
870{
Elena Reshetova6bdd0792017-10-20 10:37:37 +0300871 if (refcount_dec_and_test(&cmd->ref_count)) {
Joe Thornber9b1cc9f2015-01-23 10:00:07 +0000872 mutex_lock(&table_lock);
873 list_del(&cmd->list);
874 mutex_unlock(&table_lock);
875
Joe Thornber028ae9f2015-04-22 16:42:35 -0400876 if (!cmd->fail_io)
877 __destroy_persistent_data_objects(cmd);
Joe Thornber9b1cc9f2015-01-23 10:00:07 +0000878 kfree(cmd);
879 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +0000880}
881
Joe Thornberf494a9c2013-10-31 13:55:49 -0400882/*
883 * Checks that the given cache block is either unmapped or clean.
884 */
Joe Thornber629d0a82016-09-22 06:15:21 -0400885static int block_clean_combined_dirty(struct dm_cache_metadata *cmd, dm_cblock_t b,
886 bool *result)
Joe Thornberf494a9c2013-10-31 13:55:49 -0400887{
888 int r;
889 __le64 value;
890 dm_oblock_t ob;
891 unsigned flags;
892
893 r = dm_array_get_value(&cmd->info, cmd->root, from_cblock(b), &value);
Joe Thornber629d0a82016-09-22 06:15:21 -0400894 if (r)
Joe Thornberf494a9c2013-10-31 13:55:49 -0400895 return r;
Joe Thornberf494a9c2013-10-31 13:55:49 -0400896
897 unpack_value(value, &ob, &flags);
898 *result = !((flags & M_VALID) && (flags & M_DIRTY));
899
900 return 0;
901}
902
Joe Thornber629d0a82016-09-22 06:15:21 -0400903static int blocks_are_clean_combined_dirty(struct dm_cache_metadata *cmd,
904 dm_cblock_t begin, dm_cblock_t end,
905 bool *result)
Joe Thornberf494a9c2013-10-31 13:55:49 -0400906{
907 int r;
908 *result = true;
909
910 while (begin != end) {
Joe Thornber629d0a82016-09-22 06:15:21 -0400911 r = block_clean_combined_dirty(cmd, begin, result);
912 if (r) {
913 DMERR("block_clean_combined_dirty failed");
Joe Thornberf494a9c2013-10-31 13:55:49 -0400914 return r;
Joe Thornber629d0a82016-09-22 06:15:21 -0400915 }
Joe Thornberf494a9c2013-10-31 13:55:49 -0400916
917 if (!*result) {
918 DMERR("cache block %llu is dirty",
919 (unsigned long long) from_cblock(begin));
920 return 0;
921 }
922
923 begin = to_cblock(from_cblock(begin) + 1);
924 }
925
926 return 0;
927}
928
Joe Thornber629d0a82016-09-22 06:15:21 -0400929static int blocks_are_clean_separate_dirty(struct dm_cache_metadata *cmd,
930 dm_cblock_t begin, dm_cblock_t end,
931 bool *result)
932{
933 int r;
934 bool dirty_flag;
935 *result = true;
936
Mike Snitzer687cf442018-11-09 11:56:03 -0500937 if (from_cblock(cmd->cache_blocks) == 0)
938 /* Nothing to do */
939 return 0;
940
Mike Snitzer7f1b2152016-10-04 15:00:47 -0400941 r = dm_bitset_cursor_begin(&cmd->dirty_info, cmd->dirty_root,
Joe Thornber0d963b62017-03-20 11:54:11 -0400942 from_cblock(cmd->cache_blocks), &cmd->dirty_cursor);
Mike Snitzer7f1b2152016-10-04 15:00:47 -0400943 if (r) {
944 DMERR("%s: dm_bitset_cursor_begin for dirty failed", __func__);
945 return r;
946 }
947
948 r = dm_bitset_cursor_skip(&cmd->dirty_cursor, from_cblock(begin));
949 if (r) {
950 DMERR("%s: dm_bitset_cursor_skip for dirty failed", __func__);
951 dm_bitset_cursor_end(&cmd->dirty_cursor);
952 return r;
953 }
954
Joe Thornber629d0a82016-09-22 06:15:21 -0400955 while (begin != end) {
956 /*
957 * We assume that unmapped blocks have their dirty bit
958 * cleared.
959 */
Mike Snitzer7f1b2152016-10-04 15:00:47 -0400960 dirty_flag = dm_bitset_cursor_get_value(&cmd->dirty_cursor);
Joe Thornber629d0a82016-09-22 06:15:21 -0400961 if (dirty_flag) {
Mike Snitzer7f1b2152016-10-04 15:00:47 -0400962 DMERR("%s: cache block %llu is dirty", __func__,
Joe Thornber629d0a82016-09-22 06:15:21 -0400963 (unsigned long long) from_cblock(begin));
Mike Snitzer7f1b2152016-10-04 15:00:47 -0400964 dm_bitset_cursor_end(&cmd->dirty_cursor);
Joe Thornber629d0a82016-09-22 06:15:21 -0400965 *result = false;
966 return 0;
967 }
968
Joe Thornber0d963b62017-03-20 11:54:11 -0400969 begin = to_cblock(from_cblock(begin) + 1);
970 if (begin == end)
971 break;
972
Mike Snitzer7f1b2152016-10-04 15:00:47 -0400973 r = dm_bitset_cursor_next(&cmd->dirty_cursor);
974 if (r) {
975 DMERR("%s: dm_bitset_cursor_next for dirty failed", __func__);
976 dm_bitset_cursor_end(&cmd->dirty_cursor);
977 return r;
978 }
Joe Thornber629d0a82016-09-22 06:15:21 -0400979 }
980
Mike Snitzer7f1b2152016-10-04 15:00:47 -0400981 dm_bitset_cursor_end(&cmd->dirty_cursor);
982
Joe Thornber629d0a82016-09-22 06:15:21 -0400983 return 0;
984}
985
986static int blocks_are_unmapped_or_clean(struct dm_cache_metadata *cmd,
987 dm_cblock_t begin, dm_cblock_t end,
988 bool *result)
989{
990 if (separate_dirty_bits(cmd))
991 return blocks_are_clean_separate_dirty(cmd, begin, end, result);
992 else
993 return blocks_are_clean_combined_dirty(cmd, begin, end, result);
994}
995
Mike Snitzer95673662016-04-12 12:14:46 -0400996static bool cmd_write_lock(struct dm_cache_metadata *cmd)
997{
998 down_write(&cmd->root_lock);
999 if (cmd->fail_io || dm_bm_is_read_only(cmd->bm)) {
1000 up_write(&cmd->root_lock);
1001 return false;
Joe Thornberd14fcf32016-03-10 16:20:58 +00001002 }
Mike Snitzer95673662016-04-12 12:14:46 -04001003 return true;
1004}
Joe Thornber028ae9f2015-04-22 16:42:35 -04001005
Mike Snitzer95673662016-04-12 12:14:46 -04001006#define WRITE_LOCK(cmd) \
1007 do { \
1008 if (!cmd_write_lock((cmd))) \
1009 return -EINVAL; \
1010 } while(0)
1011
1012#define WRITE_LOCK_VOID(cmd) \
1013 do { \
1014 if (!cmd_write_lock((cmd))) \
1015 return; \
1016 } while(0)
Joe Thornber028ae9f2015-04-22 16:42:35 -04001017
1018#define WRITE_UNLOCK(cmd) \
Mike Snitzer95673662016-04-12 12:14:46 -04001019 up_write(&(cmd)->root_lock)
Joe Thornber028ae9f2015-04-22 16:42:35 -04001020
Mike Snitzer95673662016-04-12 12:14:46 -04001021static bool cmd_read_lock(struct dm_cache_metadata *cmd)
1022{
Ahmed Samy6545b602016-04-17 05:37:09 +00001023 down_read(&cmd->root_lock);
Mike Snitzer95673662016-04-12 12:14:46 -04001024 if (cmd->fail_io) {
Ahmed Samy6545b602016-04-17 05:37:09 +00001025 up_read(&cmd->root_lock);
Mike Snitzer95673662016-04-12 12:14:46 -04001026 return false;
Joe Thornberd14fcf32016-03-10 16:20:58 +00001027 }
Mike Snitzer95673662016-04-12 12:14:46 -04001028 return true;
1029}
Joe Thornberd14fcf32016-03-10 16:20:58 +00001030
Mike Snitzer95673662016-04-12 12:14:46 -04001031#define READ_LOCK(cmd) \
1032 do { \
1033 if (!cmd_read_lock((cmd))) \
1034 return -EINVAL; \
1035 } while(0)
1036
1037#define READ_LOCK_VOID(cmd) \
1038 do { \
1039 if (!cmd_read_lock((cmd))) \
1040 return; \
1041 } while(0)
Joe Thornberd14fcf32016-03-10 16:20:58 +00001042
1043#define READ_UNLOCK(cmd) \
Mike Snitzer95673662016-04-12 12:14:46 -04001044 up_read(&(cmd)->root_lock)
Joe Thornberd14fcf32016-03-10 16:20:58 +00001045
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001046int dm_cache_resize(struct dm_cache_metadata *cmd, dm_cblock_t new_cache_size)
1047{
1048 int r;
Joe Thornberf494a9c2013-10-31 13:55:49 -04001049 bool clean;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001050 __le64 null_mapping = pack_value(0, 0);
1051
Joe Thornber028ae9f2015-04-22 16:42:35 -04001052 WRITE_LOCK(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001053 __dm_bless_for_disk(&null_mapping);
Joe Thornberf494a9c2013-10-31 13:55:49 -04001054
1055 if (from_cblock(new_cache_size) < from_cblock(cmd->cache_blocks)) {
1056 r = blocks_are_unmapped_or_clean(cmd, new_cache_size, cmd->cache_blocks, &clean);
1057 if (r) {
1058 __dm_unbless_for_disk(&null_mapping);
1059 goto out;
1060 }
1061
1062 if (!clean) {
1063 DMERR("unable to shrink cache due to dirty blocks");
1064 r = -EINVAL;
1065 __dm_unbless_for_disk(&null_mapping);
1066 goto out;
1067 }
1068 }
1069
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001070 r = dm_array_resize(&cmd->info, cmd->root, from_cblock(cmd->cache_blocks),
1071 from_cblock(new_cache_size),
1072 &null_mapping, &cmd->root);
Joe Thornber629d0a82016-09-22 06:15:21 -04001073 if (r)
1074 goto out;
1075
1076 if (separate_dirty_bits(cmd)) {
1077 r = dm_bitset_resize(&cmd->dirty_info, cmd->dirty_root,
1078 from_cblock(cmd->cache_blocks), from_cblock(new_cache_size),
1079 false, &cmd->dirty_root);
1080 if (r)
1081 goto out;
1082 }
1083
1084 cmd->cache_blocks = new_cache_size;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001085 cmd->changed = true;
Joe Thornberf494a9c2013-10-31 13:55:49 -04001086
1087out:
Joe Thornber028ae9f2015-04-22 16:42:35 -04001088 WRITE_UNLOCK(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001089
1090 return r;
1091}
1092
1093int dm_cache_discard_bitset_resize(struct dm_cache_metadata *cmd,
1094 sector_t discard_block_size,
Joe Thornber1bad9bc2014-11-07 14:47:07 +00001095 dm_dblock_t new_nr_entries)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001096{
1097 int r;
1098
Joe Thornber028ae9f2015-04-22 16:42:35 -04001099 WRITE_LOCK(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001100 r = dm_bitset_resize(&cmd->discard_info,
1101 cmd->discard_root,
Joe Thornber1bad9bc2014-11-07 14:47:07 +00001102 from_dblock(cmd->discard_nr_blocks),
1103 from_dblock(new_nr_entries),
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001104 false, &cmd->discard_root);
1105 if (!r) {
1106 cmd->discard_block_size = discard_block_size;
1107 cmd->discard_nr_blocks = new_nr_entries;
1108 }
1109
1110 cmd->changed = true;
Joe Thornber028ae9f2015-04-22 16:42:35 -04001111 WRITE_UNLOCK(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001112
1113 return r;
1114}
1115
Joe Thornber1bad9bc2014-11-07 14:47:07 +00001116static int __set_discard(struct dm_cache_metadata *cmd, dm_dblock_t b)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001117{
1118 return dm_bitset_set_bit(&cmd->discard_info, cmd->discard_root,
Joe Thornber1bad9bc2014-11-07 14:47:07 +00001119 from_dblock(b), &cmd->discard_root);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001120}
1121
Joe Thornber1bad9bc2014-11-07 14:47:07 +00001122static int __clear_discard(struct dm_cache_metadata *cmd, dm_dblock_t b)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001123{
1124 return dm_bitset_clear_bit(&cmd->discard_info, cmd->discard_root,
Joe Thornber1bad9bc2014-11-07 14:47:07 +00001125 from_dblock(b), &cmd->discard_root);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001126}
1127
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001128static int __discard(struct dm_cache_metadata *cmd,
Joe Thornber1bad9bc2014-11-07 14:47:07 +00001129 dm_dblock_t dblock, bool discard)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001130{
1131 int r;
1132
1133 r = (discard ? __set_discard : __clear_discard)(cmd, dblock);
1134 if (r)
1135 return r;
1136
1137 cmd->changed = true;
1138 return 0;
1139}
1140
1141int dm_cache_set_discard(struct dm_cache_metadata *cmd,
Joe Thornber1bad9bc2014-11-07 14:47:07 +00001142 dm_dblock_t dblock, bool discard)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001143{
1144 int r;
1145
Joe Thornber028ae9f2015-04-22 16:42:35 -04001146 WRITE_LOCK(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001147 r = __discard(cmd, dblock, discard);
Joe Thornber028ae9f2015-04-22 16:42:35 -04001148 WRITE_UNLOCK(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001149
1150 return r;
1151}
1152
1153static int __load_discards(struct dm_cache_metadata *cmd,
1154 load_discard_fn fn, void *context)
1155{
1156 int r = 0;
Joe Thornberae4a46a2016-10-03 14:16:20 -04001157 uint32_t b;
1158 struct dm_bitset_cursor c;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001159
Joe Thornberae4a46a2016-10-03 14:16:20 -04001160 if (from_dblock(cmd->discard_nr_blocks) == 0)
1161 /* nothing to do */
1162 return 0;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001163
Joe Thornberae4a46a2016-10-03 14:16:20 -04001164 if (cmd->clean_when_opened) {
1165 r = dm_bitset_flush(&cmd->discard_info, cmd->discard_root, &cmd->discard_root);
1166 if (r)
1167 return r;
1168
1169 r = dm_bitset_cursor_begin(&cmd->discard_info, cmd->discard_root,
1170 from_dblock(cmd->discard_nr_blocks), &c);
1171 if (r)
1172 return r;
1173
Nikos Tsironise28adc32019-04-17 17:19:18 +03001174 for (b = 0; ; b++) {
Joe Thornberae4a46a2016-10-03 14:16:20 -04001175 r = fn(context, cmd->discard_block_size, to_dblock(b),
1176 dm_bitset_cursor_get_value(&c));
1177 if (r)
1178 break;
Nikos Tsironise28adc32019-04-17 17:19:18 +03001179
1180 if (b >= (from_dblock(cmd->discard_nr_blocks) - 1))
1181 break;
1182
1183 r = dm_bitset_cursor_next(&c);
1184 if (r)
1185 break;
Joe Thornberae4a46a2016-10-03 14:16:20 -04001186 }
1187
1188 dm_bitset_cursor_end(&c);
1189
1190 } else {
1191 for (b = 0; b < from_dblock(cmd->discard_nr_blocks); b++) {
1192 r = fn(context, cmd->discard_block_size, to_dblock(b), false);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001193 if (r)
1194 return r;
Joe Thornberae4a46a2016-10-03 14:16:20 -04001195 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001196 }
1197
1198 return r;
1199}
1200
1201int dm_cache_load_discards(struct dm_cache_metadata *cmd,
1202 load_discard_fn fn, void *context)
1203{
1204 int r;
1205
Joe Thornberd14fcf32016-03-10 16:20:58 +00001206 READ_LOCK(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001207 r = __load_discards(cmd, fn, context);
Joe Thornberd14fcf32016-03-10 16:20:58 +00001208 READ_UNLOCK(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001209
1210 return r;
1211}
1212
Joe Thornberd14fcf32016-03-10 16:20:58 +00001213int dm_cache_size(struct dm_cache_metadata *cmd, dm_cblock_t *result)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001214{
Joe Thornberd14fcf32016-03-10 16:20:58 +00001215 READ_LOCK(cmd);
1216 *result = cmd->cache_blocks;
1217 READ_UNLOCK(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001218
Joe Thornberd14fcf32016-03-10 16:20:58 +00001219 return 0;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001220}
1221
1222static int __remove(struct dm_cache_metadata *cmd, dm_cblock_t cblock)
1223{
1224 int r;
1225 __le64 value = pack_value(0, 0);
1226
1227 __dm_bless_for_disk(&value);
1228 r = dm_array_set_value(&cmd->info, cmd->root, from_cblock(cblock),
1229 &value, &cmd->root);
1230 if (r)
1231 return r;
1232
1233 cmd->changed = true;
1234 return 0;
1235}
1236
1237int dm_cache_remove_mapping(struct dm_cache_metadata *cmd, dm_cblock_t cblock)
1238{
1239 int r;
1240
Joe Thornber028ae9f2015-04-22 16:42:35 -04001241 WRITE_LOCK(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001242 r = __remove(cmd, cblock);
Joe Thornber028ae9f2015-04-22 16:42:35 -04001243 WRITE_UNLOCK(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001244
1245 return r;
1246}
1247
1248static int __insert(struct dm_cache_metadata *cmd,
1249 dm_cblock_t cblock, dm_oblock_t oblock)
1250{
1251 int r;
1252 __le64 value = pack_value(oblock, M_VALID);
1253 __dm_bless_for_disk(&value);
1254
1255 r = dm_array_set_value(&cmd->info, cmd->root, from_cblock(cblock),
1256 &value, &cmd->root);
1257 if (r)
1258 return r;
1259
1260 cmd->changed = true;
1261 return 0;
1262}
1263
1264int dm_cache_insert_mapping(struct dm_cache_metadata *cmd,
1265 dm_cblock_t cblock, dm_oblock_t oblock)
1266{
1267 int r;
1268
Joe Thornber028ae9f2015-04-22 16:42:35 -04001269 WRITE_LOCK(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001270 r = __insert(cmd, cblock, oblock);
Joe Thornber028ae9f2015-04-22 16:42:35 -04001271 WRITE_UNLOCK(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001272
1273 return r;
1274}
1275
1276struct thunk {
1277 load_mapping_fn fn;
1278 void *context;
1279
1280 struct dm_cache_metadata *cmd;
1281 bool respect_dirty_flags;
1282 bool hints_valid;
1283};
1284
Mike Snitzerea2dd8c2013-03-20 17:21:28 +00001285static bool policy_unchanged(struct dm_cache_metadata *cmd,
1286 struct dm_cache_policy *policy)
1287{
1288 const char *policy_name = dm_cache_policy_get_name(policy);
1289 const unsigned *policy_version = dm_cache_policy_get_version(policy);
1290 size_t policy_hint_size = dm_cache_policy_get_hint_size(policy);
1291
1292 /*
1293 * Ensure policy names match.
1294 */
1295 if (strncmp(cmd->policy_name, policy_name, sizeof(cmd->policy_name)))
1296 return false;
1297
1298 /*
1299 * Ensure policy major versions match.
1300 */
1301 if (cmd->policy_version[0] != policy_version[0])
1302 return false;
1303
1304 /*
1305 * Ensure policy hint sizes match.
1306 */
1307 if (cmd->policy_hint_size != policy_hint_size)
1308 return false;
1309
1310 return true;
1311}
1312
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001313static bool hints_array_initialized(struct dm_cache_metadata *cmd)
1314{
1315 return cmd->hint_root && cmd->policy_hint_size;
1316}
1317
1318static bool hints_array_available(struct dm_cache_metadata *cmd,
Mike Snitzerea2dd8c2013-03-20 17:21:28 +00001319 struct dm_cache_policy *policy)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001320{
Mike Snitzerea2dd8c2013-03-20 17:21:28 +00001321 return cmd->clean_when_opened && policy_unchanged(cmd, policy) &&
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001322 hints_array_initialized(cmd);
1323}
1324
Joe Thornber629d0a82016-09-22 06:15:21 -04001325static int __load_mapping_v1(struct dm_cache_metadata *cmd,
1326 uint64_t cb, bool hints_valid,
1327 struct dm_array_cursor *mapping_cursor,
1328 struct dm_array_cursor *hint_cursor,
1329 load_mapping_fn fn, void *context)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001330{
1331 int r = 0;
Joe Thornberf1779402016-09-20 06:16:00 -04001332
1333 __le64 mapping;
1334 __le32 hint = 0;
1335
1336 __le64 *mapping_value_le;
1337 __le32 *hint_value_le;
1338
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001339 dm_oblock_t oblock;
1340 unsigned flags;
Ilya Dryomov5b1fe7b2018-08-09 12:38:28 +02001341 bool dirty = true;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001342
Joe Thornberf1779402016-09-20 06:16:00 -04001343 dm_array_cursor_get_value(mapping_cursor, (void **) &mapping_value_le);
1344 memcpy(&mapping, mapping_value_le, sizeof(mapping));
1345 unpack_value(mapping, &oblock, &flags);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001346
1347 if (flags & M_VALID) {
Joe Thornberf1779402016-09-20 06:16:00 -04001348 if (hints_valid) {
1349 dm_array_cursor_get_value(hint_cursor, (void **) &hint_value_le);
1350 memcpy(&hint, hint_value_le, sizeof(hint));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001351 }
Ilya Dryomov5b1fe7b2018-08-09 12:38:28 +02001352 if (cmd->clean_when_opened)
1353 dirty = flags & M_DIRTY;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001354
Ilya Dryomov5b1fe7b2018-08-09 12:38:28 +02001355 r = fn(context, oblock, to_cblock(cb), dirty,
Joe Thornberf1779402016-09-20 06:16:00 -04001356 le32_to_cpu(hint), hints_valid);
Mike Snitzer48551052016-10-04 15:22:17 -04001357 if (r) {
1358 DMERR("policy couldn't load cache block %llu",
1359 (unsigned long long) from_cblock(to_cblock(cb)));
1360 }
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001361 }
1362
1363 return r;
1364}
1365
Joe Thornber629d0a82016-09-22 06:15:21 -04001366static int __load_mapping_v2(struct dm_cache_metadata *cmd,
1367 uint64_t cb, bool hints_valid,
1368 struct dm_array_cursor *mapping_cursor,
1369 struct dm_array_cursor *hint_cursor,
1370 struct dm_bitset_cursor *dirty_cursor,
1371 load_mapping_fn fn, void *context)
1372{
1373 int r = 0;
1374
1375 __le64 mapping;
1376 __le32 hint = 0;
1377
1378 __le64 *mapping_value_le;
1379 __le32 *hint_value_le;
1380
1381 dm_oblock_t oblock;
1382 unsigned flags;
Ilya Dryomov5b1fe7b2018-08-09 12:38:28 +02001383 bool dirty = true;
Joe Thornber629d0a82016-09-22 06:15:21 -04001384
1385 dm_array_cursor_get_value(mapping_cursor, (void **) &mapping_value_le);
1386 memcpy(&mapping, mapping_value_le, sizeof(mapping));
1387 unpack_value(mapping, &oblock, &flags);
1388
1389 if (flags & M_VALID) {
1390 if (hints_valid) {
1391 dm_array_cursor_get_value(hint_cursor, (void **) &hint_value_le);
1392 memcpy(&hint, hint_value_le, sizeof(hint));
1393 }
Ilya Dryomov5b1fe7b2018-08-09 12:38:28 +02001394 if (cmd->clean_when_opened)
1395 dirty = dm_bitset_cursor_get_value(dirty_cursor);
Joe Thornber629d0a82016-09-22 06:15:21 -04001396
Joe Thornber629d0a82016-09-22 06:15:21 -04001397 r = fn(context, oblock, to_cblock(cb), dirty,
1398 le32_to_cpu(hint), hints_valid);
Mike Snitzer48551052016-10-04 15:22:17 -04001399 if (r) {
1400 DMERR("policy couldn't load cache block %llu",
1401 (unsigned long long) from_cblock(to_cblock(cb)));
1402 }
Joe Thornber629d0a82016-09-22 06:15:21 -04001403 }
1404
1405 return r;
1406}
1407
Mike Snitzerea2dd8c2013-03-20 17:21:28 +00001408static int __load_mappings(struct dm_cache_metadata *cmd,
1409 struct dm_cache_policy *policy,
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001410 load_mapping_fn fn, void *context)
1411{
Joe Thornberf1779402016-09-20 06:16:00 -04001412 int r;
1413 uint64_t cb;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001414
Joe Thornberf1779402016-09-20 06:16:00 -04001415 bool hints_valid = hints_array_available(cmd, policy);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001416
Joe Thornberf1779402016-09-20 06:16:00 -04001417 if (from_cblock(cmd->cache_blocks) == 0)
1418 /* Nothing to do */
1419 return 0;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001420
Joe Thornberf1779402016-09-20 06:16:00 -04001421 r = dm_array_cursor_begin(&cmd->info, cmd->root, &cmd->mapping_cursor);
1422 if (r)
1423 return r;
1424
1425 if (hints_valid) {
1426 r = dm_array_cursor_begin(&cmd->hint_info, cmd->hint_root, &cmd->hint_cursor);
1427 if (r) {
1428 dm_array_cursor_end(&cmd->mapping_cursor);
1429 return r;
1430 }
1431 }
1432
Joe Thornber629d0a82016-09-22 06:15:21 -04001433 if (separate_dirty_bits(cmd)) {
1434 r = dm_bitset_cursor_begin(&cmd->dirty_info, cmd->dirty_root,
1435 from_cblock(cmd->cache_blocks),
1436 &cmd->dirty_cursor);
1437 if (r) {
1438 dm_array_cursor_end(&cmd->hint_cursor);
1439 dm_array_cursor_end(&cmd->mapping_cursor);
1440 return r;
1441 }
1442 }
1443
Joe Thornberf1779402016-09-20 06:16:00 -04001444 for (cb = 0; ; cb++) {
Joe Thornber629d0a82016-09-22 06:15:21 -04001445 if (separate_dirty_bits(cmd))
1446 r = __load_mapping_v2(cmd, cb, hints_valid,
1447 &cmd->mapping_cursor,
1448 &cmd->hint_cursor,
1449 &cmd->dirty_cursor,
1450 fn, context);
1451 else
1452 r = __load_mapping_v1(cmd, cb, hints_valid,
1453 &cmd->mapping_cursor, &cmd->hint_cursor,
1454 fn, context);
Joe Thornberf1779402016-09-20 06:16:00 -04001455 if (r)
1456 goto out;
1457
1458 /*
1459 * We need to break out before we move the cursors.
1460 */
1461 if (cb >= (from_cblock(cmd->cache_blocks) - 1))
1462 break;
1463
1464 r = dm_array_cursor_next(&cmd->mapping_cursor);
1465 if (r) {
1466 DMERR("dm_array_cursor_next for mapping failed");
1467 goto out;
1468 }
1469
1470 if (hints_valid) {
1471 r = dm_array_cursor_next(&cmd->hint_cursor);
1472 if (r) {
Joe Thornber4561ffc2018-09-24 16:19:30 -04001473 dm_array_cursor_end(&cmd->hint_cursor);
1474 hints_valid = false;
Joe Thornberf1779402016-09-20 06:16:00 -04001475 }
1476 }
Joe Thornber629d0a82016-09-22 06:15:21 -04001477
1478 if (separate_dirty_bits(cmd)) {
1479 r = dm_bitset_cursor_next(&cmd->dirty_cursor);
1480 if (r) {
1481 DMERR("dm_bitset_cursor_next for dirty failed");
1482 goto out;
1483 }
1484 }
Joe Thornberf1779402016-09-20 06:16:00 -04001485 }
1486out:
1487 dm_array_cursor_end(&cmd->mapping_cursor);
1488 if (hints_valid)
1489 dm_array_cursor_end(&cmd->hint_cursor);
1490
Joe Thornber629d0a82016-09-22 06:15:21 -04001491 if (separate_dirty_bits(cmd))
1492 dm_bitset_cursor_end(&cmd->dirty_cursor);
1493
Joe Thornberf1779402016-09-20 06:16:00 -04001494 return r;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001495}
1496
Mike Snitzerea2dd8c2013-03-20 17:21:28 +00001497int dm_cache_load_mappings(struct dm_cache_metadata *cmd,
1498 struct dm_cache_policy *policy,
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001499 load_mapping_fn fn, void *context)
1500{
1501 int r;
1502
Joe Thornberd14fcf32016-03-10 16:20:58 +00001503 READ_LOCK(cmd);
Mike Snitzerea2dd8c2013-03-20 17:21:28 +00001504 r = __load_mappings(cmd, policy, fn, context);
Joe Thornberd14fcf32016-03-10 16:20:58 +00001505 READ_UNLOCK(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001506
1507 return r;
1508}
1509
1510static int __dump_mapping(void *context, uint64_t cblock, void *leaf)
1511{
1512 int r = 0;
1513 __le64 value;
1514 dm_oblock_t oblock;
1515 unsigned flags;
1516
1517 memcpy(&value, leaf, sizeof(value));
1518 unpack_value(value, &oblock, &flags);
1519
1520 return r;
1521}
1522
1523static int __dump_mappings(struct dm_cache_metadata *cmd)
1524{
1525 return dm_array_walk(&cmd->info, cmd->root, __dump_mapping, NULL);
1526}
1527
1528void dm_cache_dump(struct dm_cache_metadata *cmd)
1529{
Joe Thornberd14fcf32016-03-10 16:20:58 +00001530 READ_LOCK_VOID(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001531 __dump_mappings(cmd);
Joe Thornberd14fcf32016-03-10 16:20:58 +00001532 READ_UNLOCK(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001533}
1534
1535int dm_cache_changed_this_transaction(struct dm_cache_metadata *cmd)
1536{
1537 int r;
1538
Joe Thornberd14fcf32016-03-10 16:20:58 +00001539 READ_LOCK(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001540 r = cmd->changed;
Joe Thornberd14fcf32016-03-10 16:20:58 +00001541 READ_UNLOCK(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001542
1543 return r;
1544}
1545
1546static int __dirty(struct dm_cache_metadata *cmd, dm_cblock_t cblock, bool dirty)
1547{
1548 int r;
1549 unsigned flags;
1550 dm_oblock_t oblock;
1551 __le64 value;
1552
1553 r = dm_array_get_value(&cmd->info, cmd->root, from_cblock(cblock), &value);
1554 if (r)
1555 return r;
1556
1557 unpack_value(value, &oblock, &flags);
1558
1559 if (((flags & M_DIRTY) && dirty) || (!(flags & M_DIRTY) && !dirty))
1560 /* nothing to be done */
1561 return 0;
1562
Joe Thornber79ed9ca2013-03-20 17:21:27 +00001563 value = pack_value(oblock, (flags & ~M_DIRTY) | (dirty ? M_DIRTY : 0));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001564 __dm_bless_for_disk(&value);
1565
1566 r = dm_array_set_value(&cmd->info, cmd->root, from_cblock(cblock),
1567 &value, &cmd->root);
1568 if (r)
1569 return r;
1570
1571 cmd->changed = true;
1572 return 0;
1573
1574}
1575
Joe Thornber629d0a82016-09-22 06:15:21 -04001576static int __set_dirty_bits_v1(struct dm_cache_metadata *cmd, unsigned nr_bits, unsigned long *bits)
1577{
1578 int r;
1579 unsigned i;
1580 for (i = 0; i < nr_bits; i++) {
1581 r = __dirty(cmd, to_cblock(i), test_bit(i, bits));
1582 if (r)
1583 return r;
1584 }
1585
1586 return 0;
1587}
1588
Joe Thornber683bb1a2016-09-22 10:45:21 -04001589static int is_dirty_callback(uint32_t index, bool *value, void *context)
1590{
1591 unsigned long *bits = context;
1592 *value = test_bit(index, bits);
1593 return 0;
1594}
1595
Joe Thornber629d0a82016-09-22 06:15:21 -04001596static int __set_dirty_bits_v2(struct dm_cache_metadata *cmd, unsigned nr_bits, unsigned long *bits)
1597{
1598 int r = 0;
Joe Thornber629d0a82016-09-22 06:15:21 -04001599
1600 /* nr_bits is really just a sanity check */
1601 if (nr_bits != from_cblock(cmd->cache_blocks)) {
1602 DMERR("dirty bitset is wrong size");
1603 return -EINVAL;
1604 }
1605
Joe Thornber683bb1a2016-09-22 10:45:21 -04001606 r = dm_bitset_del(&cmd->dirty_info, cmd->dirty_root);
1607 if (r)
1608 return r;
Joe Thornber629d0a82016-09-22 06:15:21 -04001609
1610 cmd->changed = true;
Joe Thornber683bb1a2016-09-22 10:45:21 -04001611 return dm_bitset_new(&cmd->dirty_info, &cmd->dirty_root, nr_bits, is_dirty_callback, bits);
Joe Thornber629d0a82016-09-22 06:15:21 -04001612}
1613
1614int dm_cache_set_dirty_bits(struct dm_cache_metadata *cmd,
1615 unsigned nr_bits,
1616 unsigned long *bits)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001617{
1618 int r;
1619
Joe Thornber028ae9f2015-04-22 16:42:35 -04001620 WRITE_LOCK(cmd);
Joe Thornber629d0a82016-09-22 06:15:21 -04001621 if (separate_dirty_bits(cmd))
1622 r = __set_dirty_bits_v2(cmd, nr_bits, bits);
1623 else
1624 r = __set_dirty_bits_v1(cmd, nr_bits, bits);
Joe Thornber028ae9f2015-04-22 16:42:35 -04001625 WRITE_UNLOCK(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001626
1627 return r;
1628}
1629
1630void dm_cache_metadata_get_stats(struct dm_cache_metadata *cmd,
1631 struct dm_cache_statistics *stats)
1632{
Joe Thornberd14fcf32016-03-10 16:20:58 +00001633 READ_LOCK_VOID(cmd);
Joe Thornber8c5008f2013-05-10 14:37:18 +01001634 *stats = cmd->stats;
Joe Thornberd14fcf32016-03-10 16:20:58 +00001635 READ_UNLOCK(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001636}
1637
1638void dm_cache_metadata_set_stats(struct dm_cache_metadata *cmd,
1639 struct dm_cache_statistics *stats)
1640{
Joe Thornber028ae9f2015-04-22 16:42:35 -04001641 WRITE_LOCK_VOID(cmd);
Joe Thornber8c5008f2013-05-10 14:37:18 +01001642 cmd->stats = *stats;
Joe Thornber028ae9f2015-04-22 16:42:35 -04001643 WRITE_UNLOCK(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001644}
1645
1646int dm_cache_commit(struct dm_cache_metadata *cmd, bool clean_shutdown)
1647{
Mike Snitzer10add84e2017-05-05 14:40:13 -04001648 int r = -EINVAL;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001649 flags_mutator mutator = (clean_shutdown ? set_clean_shutdown :
1650 clear_clean_shutdown);
1651
Joe Thornber028ae9f2015-04-22 16:42:35 -04001652 WRITE_LOCK(cmd);
Mike Snitzer10add84e2017-05-05 14:40:13 -04001653 if (cmd->fail_io)
1654 goto out;
1655
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001656 r = __commit_transaction(cmd, mutator);
1657 if (r)
1658 goto out;
1659
1660 r = __begin_transaction(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001661out:
Joe Thornber028ae9f2015-04-22 16:42:35 -04001662 WRITE_UNLOCK(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001663 return r;
1664}
1665
1666int dm_cache_get_free_metadata_block_count(struct dm_cache_metadata *cmd,
1667 dm_block_t *result)
1668{
1669 int r = -EINVAL;
1670
Joe Thornberd14fcf32016-03-10 16:20:58 +00001671 READ_LOCK(cmd);
Mike Snitzer10add84e2017-05-05 14:40:13 -04001672 if (!cmd->fail_io)
1673 r = dm_sm_get_nr_free(cmd->metadata_sm, result);
Joe Thornberd14fcf32016-03-10 16:20:58 +00001674 READ_UNLOCK(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001675
1676 return r;
1677}
1678
1679int dm_cache_get_metadata_dev_size(struct dm_cache_metadata *cmd,
1680 dm_block_t *result)
1681{
1682 int r = -EINVAL;
1683
Joe Thornberd14fcf32016-03-10 16:20:58 +00001684 READ_LOCK(cmd);
Mike Snitzer10add84e2017-05-05 14:40:13 -04001685 if (!cmd->fail_io)
1686 r = dm_sm_get_nr_blocks(cmd->metadata_sm, result);
Joe Thornberd14fcf32016-03-10 16:20:58 +00001687 READ_UNLOCK(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001688
1689 return r;
1690}
1691
1692/*----------------------------------------------------------------*/
1693
Joe Thornber4e781b42016-09-15 09:23:46 -04001694static int get_hint(uint32_t index, void *value_le, void *context)
1695{
1696 uint32_t value;
1697 struct dm_cache_policy *policy = context;
1698
1699 value = policy_get_hint(policy, to_cblock(index));
1700 *((__le32 *) value_le) = cpu_to_le32(value);
1701
1702 return 0;
1703}
1704
1705/*
1706 * It's quicker to always delete the hint array, and recreate with
1707 * dm_array_new().
1708 */
1709static int write_hints(struct dm_cache_metadata *cmd, struct dm_cache_policy *policy)
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001710{
1711 int r;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001712 size_t hint_size;
1713 const char *policy_name = dm_cache_policy_get_name(policy);
Mike Snitzer4e7f5062013-03-20 17:21:27 +00001714 const unsigned *policy_version = dm_cache_policy_get_version(policy);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001715
1716 if (!policy_name[0] ||
1717 (strlen(policy_name) > sizeof(cmd->policy_name) - 1))
1718 return -EINVAL;
1719
Joe Thornber4e781b42016-09-15 09:23:46 -04001720 strncpy(cmd->policy_name, policy_name, sizeof(cmd->policy_name));
1721 memcpy(cmd->policy_version, policy_version, sizeof(cmd->policy_version));
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001722
Joe Thornber4e781b42016-09-15 09:23:46 -04001723 hint_size = dm_cache_policy_get_hint_size(policy);
1724 if (!hint_size)
1725 return 0; /* short-circuit hints initialization */
1726 cmd->policy_hint_size = hint_size;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001727
Joe Thornber4e781b42016-09-15 09:23:46 -04001728 if (cmd->hint_root) {
1729 r = dm_array_del(&cmd->hint_info, cmd->hint_root);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001730 if (r)
1731 return r;
1732 }
1733
Joe Thornber4e781b42016-09-15 09:23:46 -04001734 return dm_array_new(&cmd->hint_info, &cmd->hint_root,
1735 from_cblock(cmd->cache_blocks),
1736 get_hint, policy);
Joe Thornber05966612014-04-03 16:16:44 +01001737}
1738
1739int dm_cache_write_hints(struct dm_cache_metadata *cmd, struct dm_cache_policy *policy)
1740{
1741 int r;
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001742
Joe Thornber028ae9f2015-04-22 16:42:35 -04001743 WRITE_LOCK(cmd);
Joe Thornber05966612014-04-03 16:16:44 +01001744 r = write_hints(cmd, policy);
Joe Thornber028ae9f2015-04-22 16:42:35 -04001745 WRITE_UNLOCK(cmd);
Joe Thornberc6b4fcb2013-03-01 22:45:51 +00001746
1747 return r;
1748}
Joe Thornber2ee57d52013-10-24 14:10:29 -04001749
1750int dm_cache_metadata_all_clean(struct dm_cache_metadata *cmd, bool *result)
1751{
Joe Thornberd14fcf32016-03-10 16:20:58 +00001752 int r;
1753
1754 READ_LOCK(cmd);
1755 r = blocks_are_unmapped_or_clean(cmd, 0, cmd->cache_blocks, result);
1756 READ_UNLOCK(cmd);
1757
1758 return r;
Joe Thornber2ee57d52013-10-24 14:10:29 -04001759}
Joe Thornber028ae9f2015-04-22 16:42:35 -04001760
1761void dm_cache_metadata_set_read_only(struct dm_cache_metadata *cmd)
1762{
1763 WRITE_LOCK_VOID(cmd);
1764 dm_bm_set_read_only(cmd->bm);
1765 WRITE_UNLOCK(cmd);
1766}
1767
1768void dm_cache_metadata_set_read_write(struct dm_cache_metadata *cmd)
1769{
1770 WRITE_LOCK_VOID(cmd);
1771 dm_bm_set_read_write(cmd->bm);
1772 WRITE_UNLOCK(cmd);
1773}
1774
1775int dm_cache_metadata_set_needs_check(struct dm_cache_metadata *cmd)
1776{
1777 int r;
1778 struct dm_block *sblock;
1779 struct cache_disk_superblock *disk_super;
1780
Joe Thornberd14fcf32016-03-10 16:20:58 +00001781 WRITE_LOCK(cmd);
Joe Thornber028ae9f2015-04-22 16:42:35 -04001782 set_bit(NEEDS_CHECK, &cmd->flags);
1783
1784 r = superblock_lock(cmd, &sblock);
1785 if (r) {
1786 DMERR("couldn't read superblock");
1787 goto out;
1788 }
1789
1790 disk_super = dm_block_data(sblock);
1791 disk_super->flags = cpu_to_le32(cmd->flags);
1792
1793 dm_bm_unlock(sblock);
1794
1795out:
Joe Thornberd14fcf32016-03-10 16:20:58 +00001796 WRITE_UNLOCK(cmd);
Joe Thornber028ae9f2015-04-22 16:42:35 -04001797 return r;
1798}
1799
Joe Thornberd14fcf32016-03-10 16:20:58 +00001800int dm_cache_metadata_needs_check(struct dm_cache_metadata *cmd, bool *result)
Joe Thornber028ae9f2015-04-22 16:42:35 -04001801{
Joe Thornberd14fcf32016-03-10 16:20:58 +00001802 READ_LOCK(cmd);
1803 *result = !!test_bit(NEEDS_CHECK, &cmd->flags);
1804 READ_UNLOCK(cmd);
Joe Thornber028ae9f2015-04-22 16:42:35 -04001805
Joe Thornberd14fcf32016-03-10 16:20:58 +00001806 return 0;
Joe Thornber028ae9f2015-04-22 16:42:35 -04001807}
1808
1809int dm_cache_metadata_abort(struct dm_cache_metadata *cmd)
1810{
1811 int r;
1812
1813 WRITE_LOCK(cmd);
1814 __destroy_persistent_data_objects(cmd);
1815 r = __create_persistent_data_objects(cmd, false);
1816 if (r)
1817 cmd->fail_io = true;
1818 WRITE_UNLOCK(cmd);
1819
1820 return r;
1821}