-
Notifications
You must be signed in to change notification settings - Fork 2
/
bbuild.c
1594 lines (1346 loc) · 38.5 KB
/
bbuild.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
/*
* NOVA Recovery routines.
*
* Copyright 2015-2016 Regents of the University of California,
* UCSD Non-Volatile Systems Lab, Andiry Xu <[email protected]>
* Copyright 2012-2013 Intel Corporation
* Copyright 2009-2011 Marco Stornelli <[email protected]>
* Copyright 2003 Sony Corporation
* Copyright 2003 Matsushita Electric Industrial Co., Ltd.
* 2003-2004 (c) MontaVista Software, Inc. , Steve Longerbeam
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <linux/fs.h>
#include <linux/bitops.h>
#include <linux/slab.h>
#include <linux/random.h>
#include <linux/delay.h>
#include "nova.h"
#include "journal.h"
#include "super.h"
#include "inode.h"
#include "log.h"
void nova_init_header(struct super_block *sb,
struct nova_inode_info_header *sih, u16 i_mode)
{
sih->log_pages = 0;
sih->i_size = 0;
sih->ino = 0;
sih->i_blocks = 0;
sih->pi_addr = 0;
sih->alter_pi_addr = 0;
INIT_RADIX_TREE(&sih->tree, GFP_ATOMIC);
sih->rb_tree = RB_ROOT;
sih->vma_tree = RB_ROOT;
sih->num_vmas = 0;
INIT_LIST_HEAD(&sih->list);
sih->i_mode = i_mode;
sih->i_flags = 0;
sih->valid_entries = 0;
sih->num_entries = 0;
sih->last_setattr = 0;
sih->last_link_change = 0;
sih->last_dentry = 0;
sih->trans_id = 0;
sih->log_head = 0;
sih->log_tail = 0;
sih->alter_log_head = 0;
sih->alter_log_tail = 0;
sih->i_blk_type = NOVA_DEFAULT_BLOCK_TYPE;
}
static inline void set_scan_bm(unsigned long bit,
struct single_scan_bm *scan_bm)
{
set_bit(bit, scan_bm->bitmap);
}
inline void set_bm(unsigned long bit, struct scan_bitmap *bm,
enum bm_type type)
{
switch (type) {
case BM_4K:
set_scan_bm(bit, &bm->scan_bm_4K);
break;
case BM_2M:
set_scan_bm(bit, &bm->scan_bm_2M);
break;
case BM_1G:
set_scan_bm(bit, &bm->scan_bm_1G);
break;
default:
break;
}
}
static inline int get_block_cpuid(struct nova_sb_info *sbi,
unsigned long blocknr)
{
return blocknr / sbi->per_list_blocks;
}
static int nova_failure_insert_inodetree(struct super_block *sb,
unsigned long ino_low, unsigned long ino_high)
{
struct nova_sb_info *sbi = NOVA_SB(sb);
struct inode_map *inode_map;
struct nova_range_node *prev = NULL, *next = NULL;
struct nova_range_node *new_node;
unsigned long internal_low, internal_high;
int cpu;
struct rb_root *tree;
int ret;
if (ino_low > ino_high) {
nova_err(sb, "%s: ino low %lu, ino high %lu\n",
__func__, ino_low, ino_high);
BUG();
}
cpu = ino_low % sbi->cpus;
if (ino_high % sbi->cpus != cpu) {
nova_err(sb, "%s: ino low %lu, ino high %lu\n",
__func__, ino_low, ino_high);
BUG();
}
internal_low = ino_low / sbi->cpus;
internal_high = ino_high / sbi->cpus;
inode_map = &sbi->inode_maps[cpu];
tree = &inode_map->inode_inuse_tree;
mutex_lock(&inode_map->inode_table_mutex);
ret = nova_find_free_slot(tree, internal_low, internal_high,
&prev, &next);
if (ret) {
nova_dbg("%s: ino %lu - %lu already exists!: %d\n",
__func__, ino_low, ino_high, ret);
mutex_unlock(&inode_map->inode_table_mutex);
return ret;
}
if (prev && next && (internal_low == prev->range_high + 1) &&
(internal_high + 1 == next->range_low)) {
/* fits the hole */
rb_erase(&next->node, tree);
inode_map->num_range_node_inode--;
prev->range_high = next->range_high;
nova_update_range_node_checksum(prev);
nova_free_inode_node(next);
goto finish;
}
if (prev && (internal_low == prev->range_high + 1)) {
/* Aligns left */
prev->range_high += internal_high - internal_low + 1;
nova_update_range_node_checksum(prev);
goto finish;
}
if (next && (internal_high + 1 == next->range_low)) {
/* Aligns right */
next->range_low -= internal_high - internal_low + 1;
nova_update_range_node_checksum(next);
goto finish;
}
/* Aligns somewhere in the middle */
new_node = nova_alloc_inode_node(sb);
NOVA_ASSERT(new_node);
new_node->range_low = internal_low;
new_node->range_high = internal_high;
nova_update_range_node_checksum(new_node);
ret = nova_insert_inodetree(sbi, new_node, cpu);
if (ret) {
nova_err(sb, "%s failed\n", __func__);
nova_free_inode_node(new_node);
goto finish;
}
inode_map->num_range_node_inode++;
finish:
mutex_unlock(&inode_map->inode_table_mutex);
return ret;
}
static void nova_destroy_blocknode_tree(struct super_block *sb, int cpu)
{
struct free_list *free_list;
free_list = nova_get_free_list(sb, cpu);
nova_destroy_range_node_tree(sb, &free_list->block_free_tree);
}
static void nova_destroy_blocknode_trees(struct super_block *sb)
{
struct nova_sb_info *sbi = NOVA_SB(sb);
int i;
for (i = 0; i < sbi->cpus; i++)
nova_destroy_blocknode_tree(sb, i);
}
static int nova_init_blockmap_from_inode(struct super_block *sb)
{
struct nova_sb_info *sbi = NOVA_SB(sb);
struct nova_inode *pi = nova_get_inode_by_ino(sb, NOVA_BLOCKNODE_INO);
struct nova_inode_info_header sih;
struct free_list *free_list;
struct nova_range_node_lowhigh *entry;
struct nova_range_node *blknode;
size_t size = sizeof(struct nova_range_node_lowhigh);
u64 curr_p;
u64 cpuid;
int ret = 0;
/* FIXME: Backup inode for BLOCKNODE */
ret = nova_get_head_tail(sb, pi, &sih);
if (ret)
goto out;
sih.ino = NOVA_BLOCKNODE_INO;
curr_p = sih.log_head;
if (curr_p == 0) {
nova_dbg("%s: pi head is 0!\n", __func__);
return -EINVAL;
}
while (curr_p != sih.log_tail) {
if (is_last_entry(curr_p, size))
curr_p = next_log_page(sb, curr_p);
if (curr_p == 0) {
nova_dbg("%s: curr_p is NULL!\n", __func__);
NOVA_ASSERT(0);
ret = -EINVAL;
break;
}
entry = (struct nova_range_node_lowhigh *)nova_get_block(sb,
curr_p);
blknode = nova_alloc_blocknode(sb);
if (blknode == NULL)
NOVA_ASSERT(0);
blknode->range_low = le64_to_cpu(entry->range_low);
blknode->range_high = le64_to_cpu(entry->range_high);
nova_update_range_node_checksum(blknode);
cpuid = get_block_cpuid(sbi, blknode->range_low);
/* FIXME: Assume NR_CPUS not change */
free_list = nova_get_free_list(sb, cpuid);
ret = nova_insert_blocktree(&free_list->block_free_tree,
blknode);
if (ret) {
nova_err(sb, "%s failed\n", __func__);
nova_free_blocknode(blknode);
NOVA_ASSERT(0);
nova_destroy_blocknode_trees(sb);
goto out;
}
free_list->num_blocknode++;
if (free_list->num_blocknode == 1)
free_list->first_node = blknode;
free_list->last_node = blknode;
free_list->num_free_blocks +=
blknode->range_high - blknode->range_low + 1;
curr_p += sizeof(struct nova_range_node_lowhigh);
}
out:
nova_free_inode_log(sb, pi, &sih);
return ret;
}
static void nova_destroy_inode_trees(struct super_block *sb)
{
struct nova_sb_info *sbi = NOVA_SB(sb);
struct inode_map *inode_map;
int i;
for (i = 0; i < sbi->cpus; i++) {
inode_map = &sbi->inode_maps[i];
nova_destroy_range_node_tree(sb,
&inode_map->inode_inuse_tree);
}
}
#define CPUID_MASK 0xff00000000000000
static int nova_init_inode_list_from_inode(struct super_block *sb)
{
struct nova_sb_info *sbi = NOVA_SB(sb);
struct nova_inode *pi = nova_get_inode_by_ino(sb, NOVA_INODELIST_INO);
struct nova_inode_info_header sih;
struct nova_range_node_lowhigh *entry;
struct nova_range_node *range_node;
struct inode_map *inode_map;
size_t size = sizeof(struct nova_range_node_lowhigh);
unsigned long num_inode_node = 0;
u64 curr_p;
unsigned long cpuid;
int ret;
/* FIXME: Backup inode for INODELIST */
ret = nova_get_head_tail(sb, pi, &sih);
if (ret)
goto out;
sih.ino = NOVA_INODELIST_INO;
sbi->s_inodes_used_count = 0;
curr_p = sih.log_head;
if (curr_p == 0) {
nova_dbg("%s: pi head is 0!\n", __func__);
return -EINVAL;
}
while (curr_p != sih.log_tail) {
if (is_last_entry(curr_p, size))
curr_p = next_log_page(sb, curr_p);
if (curr_p == 0) {
nova_dbg("%s: curr_p is NULL!\n", __func__);
NOVA_ASSERT(0);
}
entry = (struct nova_range_node_lowhigh *)nova_get_block(sb,
curr_p);
range_node = nova_alloc_inode_node(sb);
if (range_node == NULL)
NOVA_ASSERT(0);
cpuid = (entry->range_low & CPUID_MASK) >> 56;
if (cpuid >= sbi->cpus) {
nova_err(sb, "Invalid cpuid %lu\n", cpuid);
nova_free_inode_node(range_node);
NOVA_ASSERT(0);
nova_destroy_inode_trees(sb);
goto out;
}
range_node->range_low = entry->range_low & ~CPUID_MASK;
range_node->range_high = entry->range_high;
nova_update_range_node_checksum(range_node);
ret = nova_insert_inodetree(sbi, range_node, cpuid);
if (ret) {
nova_err(sb, "%s failed, %d\n", __func__, cpuid);
nova_free_inode_node(range_node);
NOVA_ASSERT(0);
nova_destroy_inode_trees(sb);
goto out;
}
sbi->s_inodes_used_count +=
range_node->range_high - range_node->range_low + 1;
num_inode_node++;
inode_map = &sbi->inode_maps[cpuid];
inode_map->num_range_node_inode++;
if (!inode_map->first_inode_range)
inode_map->first_inode_range = range_node;
curr_p += sizeof(struct nova_range_node_lowhigh);
}
nova_dbg("%s: %lu inode nodes\n", __func__, num_inode_node);
out:
nova_free_inode_log(sb, pi, &sih);
return ret;
}
static u64 nova_append_range_node_entry(struct super_block *sb,
struct nova_range_node *curr, u64 tail, unsigned long cpuid)
{
u64 curr_p;
size_t size = sizeof(struct nova_range_node_lowhigh);
struct nova_range_node_lowhigh *entry;
curr_p = tail;
if (!nova_range_node_checksum_ok(curr)) {
nova_dbg("%s: range node checksum failure\n", __func__);
goto out;
}
if (curr_p == 0 || (is_last_entry(curr_p, size) &&
next_log_page(sb, curr_p) == 0)) {
nova_dbg("%s: inode log reaches end?\n", __func__);
goto out;
}
if (is_last_entry(curr_p, size))
curr_p = next_log_page(sb, curr_p);
entry = (struct nova_range_node_lowhigh *)nova_get_block(sb, curr_p);
nova_memunlock_range(sb, entry, size);
entry->range_low = cpu_to_le64(curr->range_low);
if (cpuid)
entry->range_low |= cpu_to_le64(cpuid << 56);
entry->range_high = cpu_to_le64(curr->range_high);
nova_memlock_range(sb, entry, size);
nova_dbgv("append entry block low 0x%lx, high 0x%lx\n",
curr->range_low, curr->range_high);
nova_flush_buffer(entry, sizeof(struct nova_range_node_lowhigh), 0);
out:
return curr_p;
}
static u64 nova_save_range_nodes_to_log(struct super_block *sb,
struct rb_root *tree, u64 temp_tail, unsigned long cpuid)
{
struct nova_range_node *curr;
struct rb_node *temp;
size_t size = sizeof(struct nova_range_node_lowhigh);
u64 curr_entry = 0;
/* Save in increasing order */
temp = rb_first(tree);
while (temp) {
curr = container_of(temp, struct nova_range_node, node);
curr_entry = nova_append_range_node_entry(sb, curr,
temp_tail, cpuid);
temp_tail = curr_entry + size;
temp = rb_next(temp);
rb_erase(&curr->node, tree);
nova_free_range_node(curr);
}
return temp_tail;
}
static u64 nova_save_free_list_blocknodes(struct super_block *sb, int cpu,
u64 temp_tail)
{
struct free_list *free_list;
free_list = nova_get_free_list(sb, cpu);
temp_tail = nova_save_range_nodes_to_log(sb,
&free_list->block_free_tree, temp_tail, 0);
return temp_tail;
}
void nova_save_inode_list_to_log(struct super_block *sb)
{
struct nova_inode *pi = nova_get_inode_by_ino(sb, NOVA_INODELIST_INO);
struct nova_inode_info_header sih;
struct nova_sb_info *sbi = NOVA_SB(sb);
unsigned long num_blocks;
unsigned long num_nodes = 0;
struct inode_map *inode_map;
unsigned long i;
u64 temp_tail;
u64 new_block;
int allocated;
sih.ino = NOVA_INODELIST_INO;
sih.i_blk_type = NOVA_DEFAULT_BLOCK_TYPE;
sih.i_blocks = 0;
for (i = 0; i < sbi->cpus; i++) {
inode_map = &sbi->inode_maps[i];
num_nodes += inode_map->num_range_node_inode;
}
num_blocks = num_nodes / RANGENODE_PER_PAGE;
if (num_nodes % RANGENODE_PER_PAGE)
num_blocks++;
allocated = nova_allocate_inode_log_pages(sb, &sih, num_blocks,
&new_block, ANY_CPU, 0);
if (allocated != num_blocks) {
nova_dbg("Error saving inode list: %d\n", allocated);
return;
}
temp_tail = new_block;
for (i = 0; i < sbi->cpus; i++) {
inode_map = &sbi->inode_maps[i];
temp_tail = nova_save_range_nodes_to_log(sb,
&inode_map->inode_inuse_tree, temp_tail, i);
}
nova_memunlock_inode(sb, pi);
pi->alter_log_head = pi->alter_log_tail = 0;
pi->log_head = new_block;
nova_update_tail(pi, temp_tail);
nova_flush_buffer(&pi->log_head, CACHELINE_SIZE, 0);
nova_memlock_inode(sb, pi);
nova_dbg("%s: %lu inode nodes, pi head 0x%llx, tail 0x%llx\n",
__func__, num_nodes, pi->log_head, pi->log_tail);
}
void nova_save_blocknode_mappings_to_log(struct super_block *sb)
{
struct nova_inode *pi = nova_get_inode_by_ino(sb, NOVA_BLOCKNODE_INO);
struct nova_inode_info_header sih;
struct nova_sb_info *sbi = NOVA_SB(sb);
struct free_list *free_list;
unsigned long num_blocknode = 0;
unsigned long num_pages;
int allocated;
u64 new_block = 0;
u64 temp_tail;
int i;
sih.ino = NOVA_BLOCKNODE_INO;
sih.i_blk_type = NOVA_DEFAULT_BLOCK_TYPE;
/* Allocate log pages before save blocknode mappings */
for (i = 0; i < sbi->cpus; i++) {
free_list = nova_get_free_list(sb, i);
num_blocknode += free_list->num_blocknode;
nova_dbgv("%s: free list %d: %lu nodes\n", __func__,
i, free_list->num_blocknode);
}
num_pages = num_blocknode / RANGENODE_PER_PAGE;
if (num_blocknode % RANGENODE_PER_PAGE)
num_pages++;
allocated = nova_allocate_inode_log_pages(sb, &sih, num_pages,
&new_block, ANY_CPU, 0);
if (allocated != num_pages) {
nova_dbg("Error saving blocknode mappings: %d\n", allocated);
return;
}
temp_tail = new_block;
for (i = 0; i < sbi->cpus; i++)
temp_tail = nova_save_free_list_blocknodes(sb, i, temp_tail);
/* Finally update log head and tail */
nova_memunlock_inode(sb, pi);
pi->alter_log_head = pi->alter_log_tail = 0;
pi->log_head = new_block;
nova_update_tail(pi, temp_tail);
nova_flush_buffer(&pi->log_head, CACHELINE_SIZE, 0);
nova_memlock_inode(sb, pi);
nova_dbg("%s: %lu blocknodes, %lu log pages, pi head 0x%llx, tail 0x%llx\n",
__func__, num_blocknode, num_pages,
pi->log_head, pi->log_tail);
}
static int nova_insert_blocknode_map(struct super_block *sb,
int cpuid, unsigned long low, unsigned long high)
{
struct free_list *free_list;
struct rb_root *tree;
struct nova_range_node *blknode = NULL;
unsigned long num_blocks = 0;
int ret;
num_blocks = high - low + 1;
nova_dbgv("%s: cpu %d, low %lu, high %lu, num %lu\n",
__func__, cpuid, low, high, num_blocks);
free_list = nova_get_free_list(sb, cpuid);
tree = &(free_list->block_free_tree);
blknode = nova_alloc_blocknode(sb);
if (blknode == NULL)
return -ENOMEM;
blknode->range_low = low;
blknode->range_high = high;
nova_update_range_node_checksum(blknode);
ret = nova_insert_blocktree(tree, blknode);
if (ret) {
nova_err(sb, "%s failed\n", __func__);
nova_free_blocknode(blknode);
goto out;
}
if (!free_list->first_node)
free_list->first_node = blknode;
free_list->last_node = blknode;
free_list->num_blocknode++;
free_list->num_free_blocks += num_blocks;
out:
return ret;
}
static int __nova_build_blocknode_map(struct super_block *sb,
unsigned long *bitmap, unsigned long bsize, unsigned long scale)
{
struct nova_sb_info *sbi = NOVA_SB(sb);
struct free_list *free_list;
unsigned long next = 0;
unsigned long low = 0;
unsigned long start, end;
int cpuid = 0;
free_list = nova_get_free_list(sb, cpuid);
start = free_list->block_start;
end = free_list->block_end + 1;
while (1) {
next = find_next_zero_bit(bitmap, end, start);
if (next == bsize)
break;
if (next == end) {
if (cpuid == sbi->cpus - 1)
break;
cpuid++;
free_list = nova_get_free_list(sb, cpuid);
start = free_list->block_start;
end = free_list->block_end + 1;
continue;
}
low = next;
next = find_next_bit(bitmap, end, next);
if (nova_insert_blocknode_map(sb, cpuid,
low << scale, (next << scale) - 1)) {
nova_dbg("Error: could not insert %lu - %lu\n",
low << scale, ((next << scale) - 1));
}
start = next;
if (next == bsize)
break;
if (next == end) {
if (cpuid == sbi->cpus - 1)
break;
cpuid++;
free_list = nova_get_free_list(sb, cpuid);
start = free_list->block_start;
end = free_list->block_end + 1;
}
}
return 0;
}
static void nova_update_4K_map(struct super_block *sb,
struct scan_bitmap *bm, unsigned long *bitmap,
unsigned long bsize, unsigned long scale)
{
unsigned long next = 0;
unsigned long low = 0;
int i;
while (1) {
next = find_next_bit(bitmap, bsize, next);
if (next == bsize)
break;
low = next;
next = find_next_zero_bit(bitmap, bsize, next);
for (i = (low << scale); i < (next << scale); i++)
set_bm(i, bm, BM_4K);
if (next == bsize)
break;
}
}
struct scan_bitmap *global_bm[MAX_CPUS];
static int nova_build_blocknode_map(struct super_block *sb,
unsigned long initsize)
{
struct nova_sb_info *sbi = NOVA_SB(sb);
struct scan_bitmap *bm;
struct scan_bitmap *final_bm;
unsigned long *src, *dst;
int i, j;
int num;
int ret;
final_bm = kzalloc(sizeof(struct scan_bitmap), GFP_KERNEL);
if (!final_bm)
return -ENOMEM;
final_bm->scan_bm_4K.bitmap_size =
(initsize >> (PAGE_SHIFT + 0x3));
/* Alloc memory to hold the block alloc bitmap */
final_bm->scan_bm_4K.bitmap = kzalloc(final_bm->scan_bm_4K.bitmap_size,
GFP_KERNEL);
if (!final_bm->scan_bm_4K.bitmap) {
kfree(final_bm);
return -ENOMEM;
}
/*
* We are using free lists. Set 2M and 1G blocks in 4K map,
* and use 4K map to rebuild block map.
*/
for (i = 0; i < sbi->cpus; i++) {
bm = global_bm[i];
nova_update_4K_map(sb, bm, bm->scan_bm_2M.bitmap,
bm->scan_bm_2M.bitmap_size * 8, PAGE_SHIFT_2M - 12);
nova_update_4K_map(sb, bm, bm->scan_bm_1G.bitmap,
bm->scan_bm_1G.bitmap_size * 8, PAGE_SHIFT_1G - 12);
}
/* Merge per-CPU bms to the final single bm */
num = final_bm->scan_bm_4K.bitmap_size / sizeof(unsigned long);
if (final_bm->scan_bm_4K.bitmap_size % sizeof(unsigned long))
num++;
for (i = 0; i < sbi->cpus; i++) {
bm = global_bm[i];
src = (unsigned long *)bm->scan_bm_4K.bitmap;
dst = (unsigned long *)final_bm->scan_bm_4K.bitmap;
for (j = 0; j < num; j++)
dst[j] |= src[j];
}
ret = __nova_build_blocknode_map(sb, final_bm->scan_bm_4K.bitmap,
final_bm->scan_bm_4K.bitmap_size * 8, PAGE_SHIFT - 12);
kfree(final_bm->scan_bm_4K.bitmap);
kfree(final_bm);
return ret;
}
static void free_bm(struct super_block *sb)
{
struct nova_sb_info *sbi = NOVA_SB(sb);
struct scan_bitmap *bm;
int i;
for (i = 0; i < sbi->cpus; i++) {
bm = global_bm[i];
if (bm) {
kfree(bm->scan_bm_4K.bitmap);
kfree(bm->scan_bm_2M.bitmap);
kfree(bm->scan_bm_1G.bitmap);
kfree(bm);
}
}
}
static int alloc_bm(struct super_block *sb, unsigned long initsize)
{
struct nova_sb_info *sbi = NOVA_SB(sb);
struct scan_bitmap *bm;
int i;
for (i = 0; i < sbi->cpus; i++) {
bm = kzalloc(sizeof(struct scan_bitmap), GFP_KERNEL);
if (!bm)
return -ENOMEM;
global_bm[i] = bm;
bm->scan_bm_4K.bitmap_size =
(initsize >> (PAGE_SHIFT + 0x3));
bm->scan_bm_2M.bitmap_size =
(initsize >> (PAGE_SHIFT_2M + 0x3));
bm->scan_bm_1G.bitmap_size =
(initsize >> (PAGE_SHIFT_1G + 0x3));
/* Alloc memory to hold the block alloc bitmap */
bm->scan_bm_4K.bitmap = kzalloc(bm->scan_bm_4K.bitmap_size,
GFP_KERNEL);
bm->scan_bm_2M.bitmap = kzalloc(bm->scan_bm_2M.bitmap_size,
GFP_KERNEL);
bm->scan_bm_1G.bitmap = kzalloc(bm->scan_bm_1G.bitmap_size,
GFP_KERNEL);
if (!bm->scan_bm_4K.bitmap || !bm->scan_bm_2M.bitmap ||
!bm->scan_bm_1G.bitmap)
return -ENOMEM;
}
return 0;
}
/************************** NOVA recovery ****************************/
#define MAX_PGOFF 262144
struct task_ring {
u64 addr0[512];
u64 addr1[512]; /* Second inode address */
int num;
int inodes_used_count;
u64 *entry_array;
u64 *nvmm_array;
};
static struct task_ring *task_rings;
static struct task_struct **threads;
wait_queue_head_t finish_wq;
int *finished;
static int nova_traverse_inode_log(struct super_block *sb,
struct nova_inode *pi, struct scan_bitmap *bm, u64 head)
{
u64 curr_p;
u64 next;
curr_p = head;
if (curr_p == 0)
return 0;
BUG_ON(curr_p & (PAGE_SIZE - 1));
set_bm(curr_p >> PAGE_SHIFT, bm, BM_4K);
next = next_log_page(sb, curr_p);
while (next > 0) {
curr_p = next;
BUG_ON(curr_p & (PAGE_SIZE - 1));
set_bm(curr_p >> PAGE_SHIFT, bm, BM_4K);
next = next_log_page(sb, curr_p);
}
return 0;
}
static void nova_traverse_dir_inode_log(struct super_block *sb,
struct nova_inode *pi, struct scan_bitmap *bm)
{
nova_traverse_inode_log(sb, pi, bm, pi->log_head);
if (metadata_csum)
nova_traverse_inode_log(sb, pi, bm, pi->alter_log_head);
}
static unsigned int nova_check_old_entry(struct super_block *sb,
struct nova_inode_info_header *sih, u64 entry_addr,
unsigned long pgoff, unsigned int num_free,
u64 epoch_id, struct task_ring *ring, unsigned long base,
struct scan_bitmap *bm)
{
struct nova_file_write_entry *entry;
struct nova_file_write_entry *entryc, entry_copy;
unsigned long old_nvmm, nvmm;
unsigned long index;
int i;
int ret;
entry = (struct nova_file_write_entry *)entry_addr;
if (!entry)
return 0;
if (metadata_csum == 0)
entryc = entry;
else {
entryc = &entry_copy;
if (!nova_verify_entry_csum(sb, entry, entryc))
return 0;
}
old_nvmm = get_nvmm(sb, sih, entryc, pgoff);
ret = nova_append_data_to_snapshot(sb, entryc, old_nvmm,
num_free, epoch_id);
if (ret != 0)
return ret;
index = pgoff - base;
for (i = 0; i < num_free; i++) {
nvmm = ring->nvmm_array[index];
if (nvmm)
set_bm(nvmm, bm, BM_4K);
index++;
}
return ret;
}
static int nova_set_ring_array(struct super_block *sb,
struct nova_inode_info_header *sih, struct nova_file_write_entry *entry,
struct nova_file_write_entry *entryc, struct task_ring *ring,
unsigned long base, struct scan_bitmap *bm)
{
unsigned long start, end;
unsigned long pgoff, old_pgoff = 0;
unsigned long index;
unsigned int num_free = 0;
u64 old_entry = 0;
u64 epoch_id = entryc->epoch_id;
start = entryc->pgoff;
if (start < base)
start = base;
end = entryc->pgoff + entryc->num_pages;
if (end > base + MAX_PGOFF)
end = base + MAX_PGOFF;
for (pgoff = start; pgoff < end; pgoff++) {
index = pgoff - base;
if (ring->nvmm_array[index]) {
if (ring->entry_array[index] != old_entry) {
if (old_entry)
nova_check_old_entry(sb, sih, old_entry,
old_pgoff, num_free,
epoch_id, ring, base,
bm);
old_entry = ring->entry_array[index];
old_pgoff = pgoff;
num_free = 1;
} else {
num_free++;
}
}
}
if (old_entry)
nova_check_old_entry(sb, sih, old_entry, old_pgoff,
num_free, epoch_id, ring, base, bm);
for (pgoff = start; pgoff < end; pgoff++) {
index = pgoff - base;
ring->entry_array[index] = (u64)entry;
ring->nvmm_array[index] = (u64)(entryc->block >> PAGE_SHIFT)
+ pgoff - entryc->pgoff;
}
return 0;
}
static int nova_set_file_bm(struct super_block *sb,
struct nova_inode_info_header *sih, struct task_ring *ring,
struct scan_bitmap *bm, unsigned long base, unsigned long last_blocknr)
{
unsigned long nvmm, pgoff;
if (last_blocknr >= base + MAX_PGOFF)
last_blocknr = MAX_PGOFF - 1;
else
last_blocknr -= base;
for (pgoff = 0; pgoff <= last_blocknr; pgoff++) {
nvmm = ring->nvmm_array[pgoff];
if (nvmm) {
set_bm(nvmm, bm, BM_4K);
ring->nvmm_array[pgoff] = 0;
ring->entry_array[pgoff] = 0;
}
}
return 0;
}
/* entry given to this function is a copy in dram */
static void nova_ring_setattr_entry(struct super_block *sb,
struct nova_inode_info_header *sih,
struct nova_setattr_logentry *entry, struct task_ring *ring,
unsigned long base, unsigned int data_bits, struct scan_bitmap *bm)
{
unsigned long first_blocknr, last_blocknr;
unsigned long pgoff, old_pgoff = 0;
unsigned long index;
unsigned int num_free = 0;
u64 old_entry = 0;
loff_t start, end;
u64 epoch_id = entry->epoch_id;
if (sih->i_size <= entry->size)
goto out;
start = entry->size;
end = sih->i_size;
first_blocknr = (start + (1UL << data_bits) - 1) >> data_bits;
if (end > 0)
last_blocknr = (end - 1) >> data_bits;
else
last_blocknr = 0;
if (first_blocknr > last_blocknr)
goto out;
if (first_blocknr < base)
first_blocknr = base;
if (last_blocknr > base + MAX_PGOFF - 1)
last_blocknr = base + MAX_PGOFF - 1;
for (pgoff = first_blocknr; pgoff <= last_blocknr; pgoff++) {
index = pgoff - base;
if (ring->nvmm_array[index]) {
if (ring->entry_array[index] != old_entry) {
if (old_entry)
nova_check_old_entry(sb, sih, old_entry,
old_pgoff, num_free,
epoch_id, ring, base,
bm);
old_entry = ring->entry_array[index];
old_pgoff = pgoff;
num_free = 1;
} else {
num_free++;
}
}
}
if (old_entry)
nova_check_old_entry(sb, sih, old_entry, old_pgoff,
num_free, epoch_id, ring, base, bm);
for (pgoff = first_blocknr; pgoff <= last_blocknr; pgoff++) {
index = pgoff - base;
ring->nvmm_array[index] = 0;
ring->entry_array[index] = 0;
}
out:
sih->i_size = entry->size;