OSDN Git Service

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