forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dm-era-target.c
1747 lines (1413 loc) · 38.6 KB
/
dm-era-target.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "dm.h"
#include "persistent-data/dm-transaction-manager.h"
#include "persistent-data/dm-bitset.h"
#include "persistent-data/dm-space-map.h"
#include <linux/dm-io.h>
#include <linux/dm-kcopyd.h>
#include <linux/init.h>
#include <linux/mempool.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/vmalloc.h>
#define DM_MSG_PREFIX "era"
#define SUPERBLOCK_LOCATION 0
#define SUPERBLOCK_MAGIC 2126579579
#define SUPERBLOCK_CSUM_XOR 146538381
#define MIN_ERA_VERSION 1
#define MAX_ERA_VERSION 1
#define INVALID_WRITESET_ROOT SUPERBLOCK_LOCATION
#define MIN_BLOCK_SIZE 8
/*----------------------------------------------------------------
* Writeset
*--------------------------------------------------------------*/
struct writeset_metadata {
uint32_t nr_bits;
dm_block_t root;
};
struct writeset {
struct writeset_metadata md;
/*
* An in core copy of the bits to save constantly doing look ups on
* disk.
*/
unsigned long *bits;
};
/*
* This does not free off the on disk bitset as this will normally be done
* after digesting into the era array.
*/
static void writeset_free(struct writeset *ws)
{
vfree(ws->bits);
}
static int setup_on_disk_bitset(struct dm_disk_bitset *info,
unsigned nr_bits, dm_block_t *root)
{
int r;
r = dm_bitset_empty(info, root);
if (r)
return r;
return dm_bitset_resize(info, *root, 0, nr_bits, false, root);
}
static size_t bitset_size(unsigned nr_bits)
{
return sizeof(unsigned long) * dm_div_up(nr_bits, BITS_PER_LONG);
}
/*
* Allocates memory for the in core bitset.
*/
static int writeset_alloc(struct writeset *ws, dm_block_t nr_blocks)
{
ws->md.nr_bits = nr_blocks;
ws->md.root = INVALID_WRITESET_ROOT;
ws->bits = vzalloc(bitset_size(nr_blocks));
if (!ws->bits) {
DMERR("%s: couldn't allocate in memory bitset", __func__);
return -ENOMEM;
}
return 0;
}
/*
* Wipes the in-core bitset, and creates a new on disk bitset.
*/
static int writeset_init(struct dm_disk_bitset *info, struct writeset *ws)
{
int r;
memset(ws->bits, 0, bitset_size(ws->md.nr_bits));
r = setup_on_disk_bitset(info, ws->md.nr_bits, &ws->md.root);
if (r) {
DMERR("%s: setup_on_disk_bitset failed", __func__);
return r;
}
return 0;
}
static bool writeset_marked(struct writeset *ws, dm_block_t block)
{
return test_bit(block, ws->bits);
}
static int writeset_marked_on_disk(struct dm_disk_bitset *info,
struct writeset_metadata *m, dm_block_t block,
bool *result)
{
dm_block_t old = m->root;
/*
* The bitset was flushed when it was archived, so we know there'll
* be no change to the root.
*/
int r = dm_bitset_test_bit(info, m->root, block, &m->root, result);
if (r) {
DMERR("%s: dm_bitset_test_bit failed", __func__);
return r;
}
BUG_ON(m->root != old);
return r;
}
/*
* Returns < 0 on error, 0 if the bit wasn't previously set, 1 if it was.
*/
static int writeset_test_and_set(struct dm_disk_bitset *info,
struct writeset *ws, uint32_t block)
{
int r;
if (!test_and_set_bit(block, ws->bits)) {
r = dm_bitset_set_bit(info, ws->md.root, block, &ws->md.root);
if (r) {
/* FIXME: fail mode */
return r;
}
return 0;
}
return 1;
}
/*----------------------------------------------------------------
* On disk metadata layout
*--------------------------------------------------------------*/
#define SPACE_MAP_ROOT_SIZE 128
#define UUID_LEN 16
struct writeset_disk {
__le32 nr_bits;
__le64 root;
} __packed;
struct superblock_disk {
__le32 csum;
__le32 flags;
__le64 blocknr;
__u8 uuid[UUID_LEN];
__le64 magic;
__le32 version;
__u8 metadata_space_map_root[SPACE_MAP_ROOT_SIZE];
__le32 data_block_size;
__le32 metadata_block_size;
__le32 nr_blocks;
__le32 current_era;
struct writeset_disk current_writeset;
/*
* Only these two fields are valid within the metadata snapshot.
*/
__le64 writeset_tree_root;
__le64 era_array_root;
__le64 metadata_snap;
} __packed;
/*----------------------------------------------------------------
* Superblock validation
*--------------------------------------------------------------*/
static void sb_prepare_for_write(struct dm_block_validator *v,
struct dm_block *b,
size_t sb_block_size)
{
struct superblock_disk *disk = dm_block_data(b);
disk->blocknr = cpu_to_le64(dm_block_location(b));
disk->csum = cpu_to_le32(dm_bm_checksum(&disk->flags,
sb_block_size - sizeof(__le32),
SUPERBLOCK_CSUM_XOR));
}
static int check_metadata_version(struct superblock_disk *disk)
{
uint32_t metadata_version = le32_to_cpu(disk->version);
if (metadata_version < MIN_ERA_VERSION || metadata_version > MAX_ERA_VERSION) {
DMERR("Era metadata version %u found, but only versions between %u and %u supported.",
metadata_version, MIN_ERA_VERSION, MAX_ERA_VERSION);
return -EINVAL;
}
return 0;
}
static int sb_check(struct dm_block_validator *v,
struct dm_block *b,
size_t sb_block_size)
{
struct superblock_disk *disk = dm_block_data(b);
__le32 csum_le;
if (dm_block_location(b) != le64_to_cpu(disk->blocknr)) {
DMERR("sb_check failed: blocknr %llu: wanted %llu",
le64_to_cpu(disk->blocknr),
(unsigned long long)dm_block_location(b));
return -ENOTBLK;
}
if (le64_to_cpu(disk->magic) != SUPERBLOCK_MAGIC) {
DMERR("sb_check failed: magic %llu: wanted %llu",
le64_to_cpu(disk->magic),
(unsigned long long) SUPERBLOCK_MAGIC);
return -EILSEQ;
}
csum_le = cpu_to_le32(dm_bm_checksum(&disk->flags,
sb_block_size - sizeof(__le32),
SUPERBLOCK_CSUM_XOR));
if (csum_le != disk->csum) {
DMERR("sb_check failed: csum %u: wanted %u",
le32_to_cpu(csum_le), le32_to_cpu(disk->csum));
return -EILSEQ;
}
return check_metadata_version(disk);
}
static struct dm_block_validator sb_validator = {
.name = "superblock",
.prepare_for_write = sb_prepare_for_write,
.check = sb_check
};
/*----------------------------------------------------------------
* Low level metadata handling
*--------------------------------------------------------------*/
#define DM_ERA_METADATA_BLOCK_SIZE 4096
#define DM_ERA_METADATA_CACHE_SIZE 64
#define ERA_MAX_CONCURRENT_LOCKS 5
struct era_metadata {
struct block_device *bdev;
struct dm_block_manager *bm;
struct dm_space_map *sm;
struct dm_transaction_manager *tm;
dm_block_t block_size;
uint32_t nr_blocks;
uint32_t current_era;
/*
* We preallocate 2 writesets. When an era rolls over we
* switch between them. This means the allocation is done at
* preresume time, rather than on the io path.
*/
struct writeset writesets[2];
struct writeset *current_writeset;
dm_block_t writeset_tree_root;
dm_block_t era_array_root;
struct dm_disk_bitset bitset_info;
struct dm_btree_info writeset_tree_info;
struct dm_array_info era_array_info;
dm_block_t metadata_snap;
/*
* A flag that is set whenever a writeset has been archived.
*/
bool archived_writesets;
/*
* Reading the space map root can fail, so we read it into this
* buffer before the superblock is locked and updated.
*/
__u8 metadata_space_map_root[SPACE_MAP_ROOT_SIZE];
};
static int superblock_read_lock(struct era_metadata *md,
struct dm_block **sblock)
{
return dm_bm_read_lock(md->bm, SUPERBLOCK_LOCATION,
&sb_validator, sblock);
}
static int superblock_lock_zero(struct era_metadata *md,
struct dm_block **sblock)
{
return dm_bm_write_lock_zero(md->bm, SUPERBLOCK_LOCATION,
&sb_validator, sblock);
}
static int superblock_lock(struct era_metadata *md,
struct dm_block **sblock)
{
return dm_bm_write_lock(md->bm, SUPERBLOCK_LOCATION,
&sb_validator, sblock);
}
/* FIXME: duplication with cache and thin */
static int superblock_all_zeroes(struct dm_block_manager *bm, bool *result)
{
int r;
unsigned i;
struct dm_block *b;
__le64 *data_le, zero = cpu_to_le64(0);
unsigned sb_block_size = dm_bm_block_size(bm) / sizeof(__le64);
/*
* We can't use a validator here - it may be all zeroes.
*/
r = dm_bm_read_lock(bm, SUPERBLOCK_LOCATION, NULL, &b);
if (r)
return r;
data_le = dm_block_data(b);
*result = true;
for (i = 0; i < sb_block_size; i++) {
if (data_le[i] != zero) {
*result = false;
break;
}
}
return dm_bm_unlock(b);
}
/*----------------------------------------------------------------*/
static void ws_pack(const struct writeset_metadata *core, struct writeset_disk *disk)
{
disk->nr_bits = cpu_to_le32(core->nr_bits);
disk->root = cpu_to_le64(core->root);
}
static void ws_unpack(const struct writeset_disk *disk, struct writeset_metadata *core)
{
core->nr_bits = le32_to_cpu(disk->nr_bits);
core->root = le64_to_cpu(disk->root);
}
static void ws_inc(void *context, const void *value)
{
struct era_metadata *md = context;
struct writeset_disk ws_d;
dm_block_t b;
memcpy(&ws_d, value, sizeof(ws_d));
b = le64_to_cpu(ws_d.root);
dm_tm_inc(md->tm, b);
}
static void ws_dec(void *context, const void *value)
{
struct era_metadata *md = context;
struct writeset_disk ws_d;
dm_block_t b;
memcpy(&ws_d, value, sizeof(ws_d));
b = le64_to_cpu(ws_d.root);
dm_bitset_del(&md->bitset_info, b);
}
static int ws_eq(void *context, const void *value1, const void *value2)
{
return !memcmp(value1, value2, sizeof(struct writeset_metadata));
}
/*----------------------------------------------------------------*/
static void setup_writeset_tree_info(struct era_metadata *md)
{
struct dm_btree_value_type *vt = &md->writeset_tree_info.value_type;
md->writeset_tree_info.tm = md->tm;
md->writeset_tree_info.levels = 1;
vt->context = md;
vt->size = sizeof(struct writeset_disk);
vt->inc = ws_inc;
vt->dec = ws_dec;
vt->equal = ws_eq;
}
static void setup_era_array_info(struct era_metadata *md)
{
struct dm_btree_value_type vt;
vt.context = NULL;
vt.size = sizeof(__le32);
vt.inc = NULL;
vt.dec = NULL;
vt.equal = NULL;
dm_array_info_init(&md->era_array_info, md->tm, &vt);
}
static void setup_infos(struct era_metadata *md)
{
dm_disk_bitset_init(md->tm, &md->bitset_info);
setup_writeset_tree_info(md);
setup_era_array_info(md);
}
/*----------------------------------------------------------------*/
static int create_fresh_metadata(struct era_metadata *md)
{
int r;
r = dm_tm_create_with_sm(md->bm, SUPERBLOCK_LOCATION,
&md->tm, &md->sm);
if (r < 0) {
DMERR("dm_tm_create_with_sm failed");
return r;
}
setup_infos(md);
r = dm_btree_empty(&md->writeset_tree_info, &md->writeset_tree_root);
if (r) {
DMERR("couldn't create new writeset tree");
goto bad;
}
r = dm_array_empty(&md->era_array_info, &md->era_array_root);
if (r) {
DMERR("couldn't create era array");
goto bad;
}
return 0;
bad:
dm_sm_destroy(md->sm);
dm_tm_destroy(md->tm);
return r;
}
static int save_sm_root(struct era_metadata *md)
{
int r;
size_t metadata_len;
r = dm_sm_root_size(md->sm, &metadata_len);
if (r < 0)
return r;
return dm_sm_copy_root(md->sm, &md->metadata_space_map_root,
metadata_len);
}
static void copy_sm_root(struct era_metadata *md, struct superblock_disk *disk)
{
memcpy(&disk->metadata_space_map_root,
&md->metadata_space_map_root,
sizeof(md->metadata_space_map_root));
}
/*
* Writes a superblock, including the static fields that don't get updated
* with every commit (possible optimisation here). 'md' should be fully
* constructed when this is called.
*/
static void prepare_superblock(struct era_metadata *md, struct superblock_disk *disk)
{
disk->magic = cpu_to_le64(SUPERBLOCK_MAGIC);
disk->flags = cpu_to_le32(0ul);
/* FIXME: can't keep blanking the uuid (uuid is currently unused though) */
memset(disk->uuid, 0, sizeof(disk->uuid));
disk->version = cpu_to_le32(MAX_ERA_VERSION);
copy_sm_root(md, disk);
disk->data_block_size = cpu_to_le32(md->block_size);
disk->metadata_block_size = cpu_to_le32(DM_ERA_METADATA_BLOCK_SIZE >> SECTOR_SHIFT);
disk->nr_blocks = cpu_to_le32(md->nr_blocks);
disk->current_era = cpu_to_le32(md->current_era);
ws_pack(&md->current_writeset->md, &disk->current_writeset);
disk->writeset_tree_root = cpu_to_le64(md->writeset_tree_root);
disk->era_array_root = cpu_to_le64(md->era_array_root);
disk->metadata_snap = cpu_to_le64(md->metadata_snap);
}
static int write_superblock(struct era_metadata *md)
{
int r;
struct dm_block *sblock;
struct superblock_disk *disk;
r = save_sm_root(md);
if (r) {
DMERR("%s: save_sm_root failed", __func__);
return r;
}
r = superblock_lock_zero(md, &sblock);
if (r)
return r;
disk = dm_block_data(sblock);
prepare_superblock(md, disk);
return dm_tm_commit(md->tm, sblock);
}
/*
* Assumes block_size and the infos are set.
*/
static int format_metadata(struct era_metadata *md)
{
int r;
r = create_fresh_metadata(md);
if (r)
return r;
r = write_superblock(md);
if (r) {
dm_sm_destroy(md->sm);
dm_tm_destroy(md->tm);
return r;
}
return 0;
}
static int open_metadata(struct era_metadata *md)
{
int r;
struct dm_block *sblock;
struct superblock_disk *disk;
r = superblock_read_lock(md, &sblock);
if (r) {
DMERR("couldn't read_lock superblock");
return r;
}
disk = dm_block_data(sblock);
r = dm_tm_open_with_sm(md->bm, SUPERBLOCK_LOCATION,
disk->metadata_space_map_root,
sizeof(disk->metadata_space_map_root),
&md->tm, &md->sm);
if (r) {
DMERR("dm_tm_open_with_sm failed");
goto bad;
}
setup_infos(md);
md->block_size = le32_to_cpu(disk->data_block_size);
md->nr_blocks = le32_to_cpu(disk->nr_blocks);
md->current_era = le32_to_cpu(disk->current_era);
md->writeset_tree_root = le64_to_cpu(disk->writeset_tree_root);
md->era_array_root = le64_to_cpu(disk->era_array_root);
md->metadata_snap = le64_to_cpu(disk->metadata_snap);
md->archived_writesets = true;
return dm_bm_unlock(sblock);
bad:
dm_bm_unlock(sblock);
return r;
}
static int open_or_format_metadata(struct era_metadata *md,
bool may_format)
{
int r;
bool unformatted = false;
r = superblock_all_zeroes(md->bm, &unformatted);
if (r)
return r;
if (unformatted)
return may_format ? format_metadata(md) : -EPERM;
return open_metadata(md);
}
static int create_persistent_data_objects(struct era_metadata *md,
bool may_format)
{
int r;
md->bm = dm_block_manager_create(md->bdev, DM_ERA_METADATA_BLOCK_SIZE,
DM_ERA_METADATA_CACHE_SIZE,
ERA_MAX_CONCURRENT_LOCKS);
if (IS_ERR(md->bm)) {
DMERR("could not create block manager");
return PTR_ERR(md->bm);
}
r = open_or_format_metadata(md, may_format);
if (r)
dm_block_manager_destroy(md->bm);
return r;
}
static void destroy_persistent_data_objects(struct era_metadata *md)
{
dm_sm_destroy(md->sm);
dm_tm_destroy(md->tm);
dm_block_manager_destroy(md->bm);
}
/*
* This waits until all era_map threads have picked up the new filter.
*/
static void swap_writeset(struct era_metadata *md, struct writeset *new_writeset)
{
rcu_assign_pointer(md->current_writeset, new_writeset);
synchronize_rcu();
}
/*----------------------------------------------------------------
* Writesets get 'digested' into the main era array.
*
* We're using a coroutine here so the worker thread can do the digestion,
* thus avoiding synchronisation of the metadata. Digesting a whole
* writeset in one go would cause too much latency.
*--------------------------------------------------------------*/
struct digest {
uint32_t era;
unsigned nr_bits, current_bit;
struct writeset_metadata writeset;
__le32 value;
struct dm_disk_bitset info;
int (*step)(struct era_metadata *, struct digest *);
};
static int metadata_digest_lookup_writeset(struct era_metadata *md,
struct digest *d);
static int metadata_digest_remove_writeset(struct era_metadata *md,
struct digest *d)
{
int r;
uint64_t key = d->era;
r = dm_btree_remove(&md->writeset_tree_info, md->writeset_tree_root,
&key, &md->writeset_tree_root);
if (r) {
DMERR("%s: dm_btree_remove failed", __func__);
return r;
}
d->step = metadata_digest_lookup_writeset;
return 0;
}
#define INSERTS_PER_STEP 100
static int metadata_digest_transcribe_writeset(struct era_metadata *md,
struct digest *d)
{
int r;
bool marked;
unsigned b, e = min(d->current_bit + INSERTS_PER_STEP, d->nr_bits);
for (b = d->current_bit; b < e; b++) {
r = writeset_marked_on_disk(&d->info, &d->writeset, b, &marked);
if (r) {
DMERR("%s: writeset_marked_on_disk failed", __func__);
return r;
}
if (!marked)
continue;
__dm_bless_for_disk(&d->value);
r = dm_array_set_value(&md->era_array_info, md->era_array_root,
b, &d->value, &md->era_array_root);
if (r) {
DMERR("%s: dm_array_set_value failed", __func__);
return r;
}
}
if (b == d->nr_bits)
d->step = metadata_digest_remove_writeset;
else
d->current_bit = b;
return 0;
}
static int metadata_digest_lookup_writeset(struct era_metadata *md,
struct digest *d)
{
int r;
uint64_t key;
struct writeset_disk disk;
r = dm_btree_find_lowest_key(&md->writeset_tree_info,
md->writeset_tree_root, &key);
if (r < 0)
return r;
d->era = key;
r = dm_btree_lookup(&md->writeset_tree_info,
md->writeset_tree_root, &key, &disk);
if (r) {
if (r == -ENODATA) {
d->step = NULL;
return 0;
}
DMERR("%s: dm_btree_lookup failed", __func__);
return r;
}
ws_unpack(&disk, &d->writeset);
d->value = cpu_to_le32(key);
d->nr_bits = min(d->writeset.nr_bits, md->nr_blocks);
d->current_bit = 0;
d->step = metadata_digest_transcribe_writeset;
return 0;
}
static int metadata_digest_start(struct era_metadata *md, struct digest *d)
{
if (d->step)
return 0;
memset(d, 0, sizeof(*d));
/*
* We initialise another bitset info to avoid any caching side
* effects with the previous one.
*/
dm_disk_bitset_init(md->tm, &d->info);
d->step = metadata_digest_lookup_writeset;
return 0;
}
/*----------------------------------------------------------------
* High level metadata interface. Target methods should use these, and not
* the lower level ones.
*--------------------------------------------------------------*/
static struct era_metadata *metadata_open(struct block_device *bdev,
sector_t block_size,
bool may_format)
{
int r;
struct era_metadata *md = kzalloc(sizeof(*md), GFP_KERNEL);
if (!md)
return NULL;
md->bdev = bdev;
md->block_size = block_size;
md->writesets[0].md.root = INVALID_WRITESET_ROOT;
md->writesets[1].md.root = INVALID_WRITESET_ROOT;
md->current_writeset = &md->writesets[0];
r = create_persistent_data_objects(md, may_format);
if (r) {
kfree(md);
return ERR_PTR(r);
}
return md;
}
static void metadata_close(struct era_metadata *md)
{
destroy_persistent_data_objects(md);
kfree(md);
}
static bool valid_nr_blocks(dm_block_t n)
{
/*
* dm_bitset restricts us to 2^32. test_bit & co. restrict us
* further to 2^31 - 1
*/
return n < (1ull << 31);
}
static int metadata_resize(struct era_metadata *md, void *arg)
{
int r;
dm_block_t *new_size = arg;
__le32 value;
if (!valid_nr_blocks(*new_size)) {
DMERR("Invalid number of origin blocks %llu",
(unsigned long long) *new_size);
return -EINVAL;
}
writeset_free(&md->writesets[0]);
writeset_free(&md->writesets[1]);
r = writeset_alloc(&md->writesets[0], *new_size);
if (r) {
DMERR("%s: writeset_alloc failed for writeset 0", __func__);
return r;
}
r = writeset_alloc(&md->writesets[1], *new_size);
if (r) {
DMERR("%s: writeset_alloc failed for writeset 1", __func__);
return r;
}
value = cpu_to_le32(0u);
__dm_bless_for_disk(&value);
r = dm_array_resize(&md->era_array_info, md->era_array_root,
md->nr_blocks, *new_size,
&value, &md->era_array_root);
if (r) {
DMERR("%s: dm_array_resize failed", __func__);
return r;
}
md->nr_blocks = *new_size;
return 0;
}
static int metadata_era_archive(struct era_metadata *md)
{
int r;
uint64_t keys[1];
struct writeset_disk value;
r = dm_bitset_flush(&md->bitset_info, md->current_writeset->md.root,
&md->current_writeset->md.root);
if (r) {
DMERR("%s: dm_bitset_flush failed", __func__);
return r;
}
ws_pack(&md->current_writeset->md, &value);
md->current_writeset->md.root = INVALID_WRITESET_ROOT;
keys[0] = md->current_era;
__dm_bless_for_disk(&value);
r = dm_btree_insert(&md->writeset_tree_info, md->writeset_tree_root,
keys, &value, &md->writeset_tree_root);
if (r) {
DMERR("%s: couldn't insert writeset into btree", __func__);
/* FIXME: fail mode */
return r;
}
md->archived_writesets = true;
return 0;
}
static struct writeset *next_writeset(struct era_metadata *md)
{
return (md->current_writeset == &md->writesets[0]) ?
&md->writesets[1] : &md->writesets[0];
}
static int metadata_new_era(struct era_metadata *md)
{
int r;
struct writeset *new_writeset = next_writeset(md);
r = writeset_init(&md->bitset_info, new_writeset);
if (r) {
DMERR("%s: writeset_init failed", __func__);
return r;
}
swap_writeset(md, new_writeset);
md->current_era++;
return 0;
}
static int metadata_era_rollover(struct era_metadata *md)
{
int r;
if (md->current_writeset->md.root != INVALID_WRITESET_ROOT) {
r = metadata_era_archive(md);
if (r) {
DMERR("%s: metadata_archive_era failed", __func__);
/* FIXME: fail mode? */
return r;
}
}
r = metadata_new_era(md);
if (r) {
DMERR("%s: new era failed", __func__);
/* FIXME: fail mode */
return r;
}
return 0;
}
static bool metadata_current_marked(struct era_metadata *md, dm_block_t block)
{
bool r;
struct writeset *ws;
rcu_read_lock();
ws = rcu_dereference(md->current_writeset);
r = writeset_marked(ws, block);
rcu_read_unlock();
return r;
}
static int metadata_commit(struct era_metadata *md)
{
int r;
struct dm_block *sblock;
if (md->current_writeset->md.root != SUPERBLOCK_LOCATION) {
r = dm_bitset_flush(&md->bitset_info, md->current_writeset->md.root,
&md->current_writeset->md.root);
if (r) {
DMERR("%s: bitset flush failed", __func__);
return r;
}
}
r = save_sm_root(md);
if (r) {
DMERR("%s: save_sm_root failed", __func__);
return r;
}
r = dm_tm_pre_commit(md->tm);
if (r) {
DMERR("%s: pre commit failed", __func__);
return r;
}
r = superblock_lock(md, &sblock);
if (r) {
DMERR("%s: superblock lock failed", __func__);
return r;
}
prepare_superblock(md, dm_block_data(sblock));
return dm_tm_commit(md->tm, sblock);
}
static int metadata_checkpoint(struct era_metadata *md)
{
/*
* For now we just rollover, but later I want to put a check in to
* avoid this if the filter is still pretty fresh.
*/
return metadata_era_rollover(md);
}
/*
* Metadata snapshots allow userland to access era data.
*/
static int metadata_take_snap(struct era_metadata *md)
{
int r, inc;
struct dm_block *clone;
if (md->metadata_snap != SUPERBLOCK_LOCATION) {