-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathballoc.c
1168 lines (966 loc) · 28.1 KB
/
balloc.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 <linux/fs.h>
#include <linux/slab.h>
#include <linux/buffer_head.h>
#include <linux/blkdev.h>
#include <linux/rwlock.h>
#include "aeon.h"
#include "aeon_super.h"
#include "aeon_extents.h"
#include "aeon_balloc.h"
#ifdef CONFIG_AEON_FS_NUMA
int aeon_alloc_block_free_lists(struct super_block *sb)
{
struct aeon_sb_info *sbi = AEON_SB(sb);
struct free_list *free_list;
struct numa_maps *nm;
int numa_id;
int cpu_id;
sbi->nm = aeon_alloc_numa_maps(sb);
if (!sbi->nm)
return -ENOMEM;
for (numa_id = 0; numa_id < sbi->numa_nodes; numa_id++) {
sbi->nm[numa_id].free_lists = aeon_alloc_free_lists(sb);
if (!sbi->nm->free_lists)
return -ENOMEM;
}
for (numa_id = 0; numa_id < sbi->numa_nodes; numa_id++) {
int list_id = 0;
nm = &sbi->nm[numa_id];
nm->map_id = 0;
nm->max_id = sbi->cpus/sbi->numa_nodes;
nm->numa_id = numa_id;
for (cpu_id = 0; cpu_id < sbi->cpus; cpu_id++) {
if (cpu_to_mem(cpu_id) != numa_id)
continue;
free_list = &sbi->nm[numa_id].free_lists[list_id++];
free_list->block_free_tree = RB_ROOT;
spin_lock_init(&free_list->s_lock);
free_list->index = cpu_id;
free_list->numa_index = numa_id;
}
}
for (numa_id = 0; numa_id < sbi->numa_nodes; numa_id++) {
int list_id = 0;
nm = &sbi->nm[numa_id];
for (cpu_id = 0; cpu_id < sbi->cpus; cpu_id++) {
if (cpu_to_mem(cpu_id) != numa_id)
continue;
aeon_dbgv("%d,%d,%d numa %d cpuid %d\n",
numa_id, cpu_id,
nm->numa_id,
nm->free_lists[list_id].numa_index,
nm->free_lists[list_id].index);
list_id++;
}
}
return 0;
}
#else
int aeon_alloc_block_free_lists(struct super_block *sb)
{
struct aeon_sb_info *sbi = AEON_SB(sb);
struct free_list *free_list;
int i;
sbi->free_lists = aeon_alloc_free_lists(sb);
if (!sbi->free_lists)
return -ENOMEM;
for (i = 0; i < sbi->cpus; i++) {
free_list = aeon_get_free_list(sb, i);
free_list->block_free_tree = RB_ROOT;
spin_lock_init(&free_list->s_lock);
free_list->index = i;
}
return 0;
}
#endif
static void
do_aeon_save_block_range_nodes(struct super_block *sb, struct rb_root *tree,
int cpu_id, int num_nodes)
{
struct aeon_range_node *curr;
struct rb_node *temp;
struct aeon_region_table *art = aeon_get_rtable(sb, cpu_id);
u64 start_addr = (u64)art + AEON_INODE_SIZE;
__le32 *range_low;
__le32 *range_high;
int count = 0;
art->range_nodes = le16_to_cpu(num_nodes);
temp = rb_first(tree);
while (temp) {
curr = container_of(temp, struct aeon_range_node, node);
temp = rb_next(temp);
range_low = (__le32 *)(start_addr + count * 32);
range_high = range_low + 1;
*range_low = curr->range_low;
*range_high = curr->range_high;
count += 2;
}
}
/* 4k page - aeon_region_table */
#define MAX_NODES (AEON_DEF_BLOCK_SIZE_4K - AEON_INODE_SIZE) / 32
static void aeon_save_free_lists(struct super_block *sb)
{
struct aeon_sb_info *sbi = AEON_SB(sb);
struct free_list *free_list;
struct rb_root *tree;
struct aeon_range_node *curr;
struct rb_node *temp;
int i;
int num_nodes = 0;
for (i = 0; i < sbi->cpus; i++) {
free_list = aeon_get_free_list(sb, i);
tree = &free_list->block_free_tree;
temp = rb_first(tree);
while (temp) {
curr = container_of(temp, struct aeon_range_node, node);
temp = rb_next(temp);
num_nodes += 2;
}
if (num_nodes < MAX_NODES)
do_aeon_save_block_range_nodes(sb, tree, i, num_nodes);
else
BUG(); /*TODO expand it */
num_nodes = 0;
}
}
void aeon_delete_free_lists(struct super_block *sb)
{
struct aeon_sb_info *sbi = AEON_SB(sb);
struct free_list *free_list;
struct rb_root *disposal;
int i;
aeon_save_free_lists(sb);
for (i = 0; i < sbi->cpus; i++) {
free_list = aeon_get_free_list(sb, i);
disposal = &free_list->block_free_tree;
aeon_destroy_range_node_tree(sb, disposal);
free_list->first_node = NULL;
free_list->last_node = NULL;
}
aeon_free_free_lists(sb);
sbi->free_lists = NULL;
}
unsigned long aeon_count_free_blocks(struct super_block *sb)
{
struct aeon_sb_info *sbi = AEON_SB(sb);
struct free_list *free_list;
unsigned long num_free_blocks = 0;
int i;
for (i = 0; i < sbi->cpus; i++) {
free_list = aeon_get_free_list(sb, i);
num_free_blocks += free_list->num_free_blocks;
}
return num_free_blocks;
}
static int aeon_insert_blocktree(struct rb_root *tree,
struct aeon_range_node *new_node)
{
int ret;
ret = aeon_insert_range_node(tree, new_node, NODE_BLOCK);
if (ret)
aeon_dbg("ERROR: %s failed %d\n", __func__, ret);
return ret;
}
static void aeon_init_free_list(struct super_block *sb,
struct free_list *free_list, int index)
{
struct aeon_sb_info *sbi = AEON_SB(sb);
unsigned long per_list_blocks;
per_list_blocks = sbi->num_blocks / sbi->cpus;
free_list->block_start = per_list_blocks * index;
free_list->block_end = free_list->block_start + per_list_blocks - 1;
sbi->last_addr = free_list->block_end << AEON_SHIFT;
}
static void do_aeon_rebuild_blockmap(struct super_block *sb, int cpu_id,
u64 start_addr, int num_nodes)
{
struct free_list *free_list;
struct aeon_range_node *blknode;
struct rb_root *tree;
int offset = 0;
int ret;
__le32 *range_low;
__le32 *range_high;
free_list = aeon_get_free_list(sb, cpu_id);
tree = &(free_list->block_free_tree);
aeon_init_free_list(sb, free_list, cpu_id);
for (offset = 0; offset < num_nodes; offset+=2) {
range_low = (__le32 *)(start_addr + offset * 32);
range_high = (__le32 *)(range_low + 1);
blknode = aeon_alloc_block_node(sb);
blknode->range_low = le32_to_cpu(*range_low);
blknode->range_high = le32_to_cpu(*range_high);
ret = aeon_insert_blocktree(tree, blknode);
if (ret) {
aeon_err(sb, "%s failed\n", __func__);
aeon_free_block_node(blknode);
return;
}
if (offset == 0)
free_list->first_node = blknode;
free_list->last_node = blknode;
free_list->num_blocknode++;
}
}
static void aeon_rebuild_blockmap(struct super_block *sb)
{
struct aeon_sb_info *sbi = AEON_SB(sb);
struct inode_map *inode_map;
struct aeon_region_table *art;
u64 head_addr = AEON_HEAD(sb) + AEON_SB_SIZE + AEON_INODE_SIZE;
u64 start_addr;
__le32 *table_blocknr;
int i;
for (i = 0; i < sbi->cpus; i++) {
unsigned long blocknr;
int num_nodes;
inode_map = aeon_get_inode_map(sb, i);
table_blocknr = (__le32 *)(i * 32 + head_addr);
blocknr = le32_to_cpu(*table_blocknr);
blocknr <<= AEON_SHIFT;
inode_map->i_table_addr = (void *)(blocknr + AEON_HEAD(sb));
art = aeon_get_rtable(sb, i);
num_nodes = art->range_nodes;
start_addr = (u64)art + AEON_INODE_SIZE;
do_aeon_rebuild_blockmap(sb, i, start_addr, num_nodes);
}
}
void aeon_init_blockmap(struct super_block *sb)
{
struct aeon_sb_info *sbi = AEON_SB(sb);
struct rb_root *tree;
struct free_list *free_list;
struct aeon_range_node *blknode;
int ret;
int i;
sbi->per_list_blocks = sbi->num_blocks / sbi->cpus;
if (!(sbi->s_mount_opt & AEON_MOUNT_FORMAT)) {
aeon_rebuild_blockmap(sb);
return;
}
for (i = 0; i < sbi->cpus; i++) {
free_list = aeon_get_free_list(sb, i);
tree = &(free_list->block_free_tree);
aeon_init_free_list(sb, free_list, i);
free_list->num_free_blocks = free_list->block_end -
free_list->block_start + 1;
blknode = aeon_alloc_block_node(sb);
if (i == 0)
blknode->range_low = free_list->block_start + 1;
else
blknode->range_low = free_list->block_start;
blknode->range_high = free_list->block_end;
ret = aeon_insert_blocktree(tree, blknode);
if (ret) {
aeon_err(sb, "%s failed\n", __func__);
aeon_free_block_node(blknode);
return;
}
free_list->first_node = blknode;
free_list->last_node = blknode;
free_list->num_blocknode = 1;
}
}
static inline
int aeon_rbtree_compare_rangenode(struct aeon_range_node *curr,
unsigned long key, enum node_type type)
{
if (type == NODE_DIR) {
if (key < curr->hash)
return -1;
if (key > curr->hash)
return 1;
return 0;
}
if (type == NODE_EXTENT) {
if (key < curr->offset)
return -1;
if (key >= (curr->offset + curr->length))
return 1;
return 0;
}
/* Block and inode */
if (key < curr->range_low)
return -1;
if (key > curr->range_high)
return 1;
return 0;
}
int aeon_insert_range_node(struct rb_root *tree,
struct aeon_range_node *new_node, enum node_type type)
{
struct aeon_range_node *curr;
struct rb_node **temp, *parent;
int compVal;
temp = &(tree->rb_node);
parent = NULL;
while (*temp) {
curr = container_of(*temp, struct aeon_range_node, node);
compVal = aeon_rbtree_compare_rangenode(curr, new_node->range_low, type);
parent = *temp;
if (compVal == -1)
temp = &((*temp)->rb_left);
else if (compVal == 1)
temp = &((*temp)->rb_right);
else {
aeon_dbg("%s: type %d entry %lu - %lu already exists: "
"%lu - %lu\n",
__func__, type, new_node->range_low,
new_node->range_high, curr->range_low,
curr->range_high);
return -EINVAL;
}
}
rb_link_node(&new_node->node, parent, temp);
rb_insert_color(&new_node->node, tree);
return 0;
}
bool aeon_find_range_node(struct rb_root *tree, unsigned long key,
enum node_type type, struct aeon_range_node **ret_node)
{
struct aeon_range_node *curr = NULL;
struct rb_node *temp;
int compVal;
bool found = false;
temp = tree->rb_node;
while (temp) {
curr = container_of(temp, struct aeon_range_node, node);
compVal = aeon_rbtree_compare_rangenode(curr, key, type);
if (compVal == -1)
temp = temp->rb_left;
else if (compVal == 1)
temp = temp->rb_right;
else {
found = true;
break;
}
}
*ret_node = curr;
return found;
}
void aeon_destroy_range_node_tree(struct super_block *sb, struct rb_root *tree)
{
struct aeon_range_node *curr;
struct rb_node *temp;
temp = rb_first(tree);
while (temp) {
curr = container_of(temp, struct aeon_range_node, node);
temp = rb_next(temp);
rb_erase(&curr->node, tree);
aeon_free_dir_node(curr);
}
}
static int aeon_find_free_slot(struct rb_root *tree, unsigned long range_low,
unsigned long range_high,
struct aeon_range_node **prev,
struct aeon_range_node **next)
{
struct aeon_range_node *ret_node = NULL;
struct rb_node *temp;
bool ret = false;
ret = aeon_find_range_node(tree, range_low, NODE_BLOCK, &ret_node);
if (ret) {
aeon_dbg("%s ERROR: %lu - %lu already in free list\n",
__func__, range_low, range_high);
return -EINVAL;
}
if (!ret_node)
*prev = *next = NULL;
else if (ret_node->range_high < range_low) {
*prev = ret_node;
temp = rb_next(&ret_node->node);
if (temp)
*next = container_of(temp, struct aeon_range_node, node);
else
*next = NULL;
} else if (ret_node->range_low > range_high) {
*next = ret_node;
temp = rb_prev(&ret_node->node);
if (temp)
*prev = container_of(temp, struct aeon_range_node, node);
else
*prev = NULL;
} else {
aeon_dbg("%s ERROR: %lu - %lu overlaps with existing node %lu - %lu\n",
__func__, range_low, range_high, ret_node->range_low,
ret_node->range_high);
return -EINVAL;
}
return 0;
}
int aeon_insert_blocks_into_free_list(struct super_block *sb,
unsigned long blocknr,
int num, unsigned short btype)
{
struct aeon_sb_info *sbi = AEON_SB(sb);
struct free_list *free_list;
struct rb_root *tree;
struct aeon_range_node *prev = NULL;
struct aeon_range_node *next = NULL;
struct aeon_range_node *curr_node;
struct aeon_region_table *art;
unsigned long block_low;
unsigned long block_high;
unsigned long num_blocks = 0;
int cpu_id;
int ret;
bool new_node_used = false;
if (num <= 0) {
aeon_err(sb, "less zero blocks can't be freed\n");
return -EINVAL;
}
curr_node = aeon_alloc_block_node(sb);
if (curr_node == NULL)
return -ENOMEM;
cpu_id = blocknr / sbi->per_list_blocks;
free_list = aeon_get_free_list(sb, cpu_id);
art = aeon_get_rtable(sb, cpu_id);
spin_lock(&free_list->s_lock);
tree = &(free_list->block_free_tree);
num_blocks = aeon_get_numblocks(btype) * num;
block_low = blocknr;
block_high = blocknr + num_blocks - 1;
if (blocknr < free_list->block_start ||
blocknr + num > free_list->block_end + 1) {
aeon_err(sb, "free blocks %lu to %lu, free list %d, start %lu, end %lu\n",
blocknr, blocknr + num - 1,
free_list->index,
free_list->block_start,
free_list->block_end);
ret = -EIO;
goto out;
}
ret = aeon_find_free_slot(tree, block_low, block_high, &prev, &next);
if (ret) {
aeon_err(sb, "find free slot fail: %d\n", ret);
goto out;
}
if (prev && next && (block_low == prev->range_high + 1) &&
(block_high + 1 == next->range_low)) {
rb_erase(&next->node, tree);
free_list->num_blocknode--;
prev->range_high = next->range_high;
if (free_list->last_node == next)
free_list->last_node = prev;
aeon_free_block_node(next);
goto block_found;
}
if (prev && (block_low == prev->range_high + 1)) {
prev->range_high += num_blocks;
goto block_found;
}
if (next && (block_high + 1 == next->range_low)) {
next->range_low -= num_blocks;
goto block_found;
}
curr_node->range_low = block_low;
curr_node->range_high = block_high;
new_node_used = true;
ret = aeon_insert_blocktree(tree, curr_node);
if (ret) {
new_node_used = false;
goto out;
}
if (!prev)
free_list->first_node = curr_node;
if (!next)
free_list->last_node = curr_node;
free_list->num_blocknode++;
block_found:
free_list->num_free_blocks += num_blocks;
art->num_free_blocks += cpu_to_le64(num_blocks);
art->alloc_data_count--;
art->alloc_data_pages -= cpu_to_le64(num_blocks);
art->freed_data_count++;
art->freed_data_pages += cpu_to_le64(num_blocks);
out:
spin_unlock(&free_list->s_lock);
if (new_node_used == false)
aeon_free_block_node(curr_node);
return ret;
}
static int not_enough_blocks(struct free_list *free_list,
unsigned long num_blocks)
{
struct aeon_range_node *first = free_list->first_node;
struct aeon_range_node *last = free_list->last_node;
if (free_list->num_free_blocks < num_blocks || !first || !last) {
aeon_dbgv("%s: num_free_blocks=%ld; num_blocks=%ld; first=0x%p; last=0x%p",
__func__, free_list->num_free_blocks, num_blocks,
first, last);
return 1;
}
return 0;
}
/* Find out the free list with most free blocks */
#ifdef CONFIG_AEON_FS_NUMA
static struct free_list *aeon_get_candidate_free_list(struct super_block *sb)
{
struct aeon_sb_info *sbi = AEON_SB(sb);
struct free_list *free_list;
int numa_id = 0;
int cpu_id = 0;
int num_free_blocks = 0;
int i;
int j;
for (i = 0; i < sbi->numa_nodes; i++) {
for (j = 0; j < sbi->cpus/sbi->numa_nodes; j++) {
free_list = &sbi->nm[i].free_lists[j];
if (free_list->num_free_blocks > num_free_blocks) {
numa_id = i;
cpu_id = j;
num_free_blocks = free_list->num_free_blocks;
}
}
}
return &sbi->nm[numa_id].free_lists[cpu_id];
}
#else
static int aeon_get_candidate_free_list(struct super_block *sb)
{
struct aeon_sb_info *sbi = AEON_SB(sb);
struct free_list *free_list;
int cpuid = 0;
int num_free_blocks = 0;
int i;
for (i = 0; i < sbi->cpus; i++) {
free_list = aeon_get_free_list(sb, i);
if (free_list->num_free_blocks > num_free_blocks) {
cpuid = i;
num_free_blocks = free_list->num_free_blocks;
}
}
return cpuid;
}
#endif
/* Return how many blocks allocated */
static long aeon_alloc_blocks_in_free_list(struct super_block *sb,
struct free_list *free_list,
unsigned short btype,
unsigned long num_blocks,
unsigned long *new_blocknr)
{
struct rb_root *tree;
struct aeon_range_node *curr;
struct aeon_range_node *next = NULL;
struct aeon_range_node *prev = NULL;
struct rb_node *temp;
struct rb_node *next_node;
struct rb_node *prev_node;
unsigned long curr_blocks;
bool found = 0;
unsigned long step = 0;
if (!free_list->first_node || free_list->num_free_blocks == 0) {
aeon_err(sb, "%s: Can't alloc. free_list->first_node=0x%p free_list->num_free_blocks = %lu",
__func__, free_list->first_node,
free_list->num_free_blocks);
return -ENOSPC;
}
tree = &(free_list->block_free_tree);
temp = &(free_list->first_node->node);
while (temp) {
step++;
curr = container_of(temp, struct aeon_range_node, node);
curr_blocks = curr->range_high - curr->range_low + 1;
if (num_blocks >= curr_blocks) {
/* Superpage allocation must succeed */
if (btype > 0 && num_blocks > curr_blocks)
goto next;
/* Otherwise, allocate the whole blocknode */
if (curr == free_list->first_node) {
next_node = rb_next(temp);
if (next_node)
next = container_of(next_node, struct aeon_range_node, node);
free_list->first_node = next;
}
if (curr == free_list->last_node) {
prev_node = rb_prev(temp);
if (prev_node)
prev = container_of(prev_node, struct aeon_range_node, node);
free_list->last_node = prev;
}
rb_erase(&curr->node, tree);
free_list->num_blocknode--;
num_blocks = curr_blocks;
*new_blocknr = curr->range_low;
aeon_free_block_node(curr);
found = 1;
break;
}
/* Allocate partial blocknode */
*new_blocknr = curr->range_low;
curr->range_low += num_blocks;
found = 1;
break;
next:
temp = rb_next(temp);
}
if (free_list->num_free_blocks < num_blocks) {
aeon_dbg("%s: free list %d has %lu free blocks,"
"but allocated %lu blocks?\n",
__func__, free_list->index,
free_list->num_free_blocks, num_blocks);
return -ENOSPC;
}
if (found)
free_list->num_free_blocks -= num_blocks;
else {
aeon_dbg("%s: Can't alloc. found = %d", __func__, found);
return -ENOSPC;
}
return num_blocks;
}
static int aeon_new_blocks(struct super_block *sb, unsigned long *blocknr,
unsigned int num, unsigned short btype, int cpuid)
{
struct free_list *free_list;
struct aeon_sb_info *sbi = AEON_SB(sb);
struct inode_map *inode_map;
struct aeon_region_table *art;
unsigned long num_blocks = 0;
unsigned long new_blocknr = 0;
long ret_blocks = 0;
int retried = 0;
num_blocks = num * aeon_get_numblocks(btype);
#ifdef CONFIG_AEON_FS_NUMA
#else
if (cpuid == ANY_CPU)
cpuid = aeon_get_cpuid(sb);
#endif
#ifdef CONFIG_AEON_FS_NUMA
free_list = aeon_get_numa_list(sb);
cpuid = free_list->index;
#else
retry:
free_list = aeon_get_free_list(sb, cpuid);
spin_lock(&free_list->s_lock);
#endif
#ifdef CONFIG_AEON_FS_NUMA
numa_retry:
spin_lock(&free_list->s_lock);
if (not_enough_blocks(free_list, num_blocks)) {
aeon_dbgv("%s: cpu %d, free_blocks %lu, required %lu, blocknode %lu\n",
__func__, cpuid, free_list->num_free_blocks,
num_blocks, free_list->num_blocknode);
if (retried >= sbi->numa_nodes) {
dump_stack();
goto alloc;
}
spin_unlock(&free_list->s_lock);
free_list = aeon_get_candidate_free_list(sb);
retried++;
goto numa_retry;
}
#else
if (not_enough_blocks(free_list, num_blocks)) {
aeon_dbgv("%s: cpu %d, free_blocks %lu, required %lu, blocknode %lu\n",
__func__, cpuid, free_list->num_free_blocks,
num_blocks, free_list->num_blocknode);
if (retried >= sbi->cpus-1) {
dump_stack();
goto alloc;
}
spin_unlock(&free_list->s_lock);
cpuid = aeon_get_candidate_free_list(sb);
retried++;
goto retry;
}
#endif
alloc:
inode_map = aeon_get_inode_map(sb, cpuid);
art = AEON_R_TABLE(inode_map);
ret_blocks = aeon_alloc_blocks_in_free_list(sb, free_list, btype,
num_blocks, &new_blocknr);
if (ret_blocks > 0) {
art->alloc_data_count++;
art->alloc_data_pages += cpu_to_le64(ret_blocks);
art->num_free_blocks = cpu_to_le64(free_list->num_free_blocks);
art->b_range_low += cpu_to_le32(ret_blocks);
}
spin_unlock(&free_list->s_lock);
if (ret_blocks <= 0 || new_blocknr == 0) {
aeon_dbg("%s: not able to allocate %d blocks. ret_blocks=%ld; new_blocknr=%lu",
__func__, num, ret_blocks, new_blocknr);
return -ENOSPC;
}
*blocknr = new_blocknr;
return ret_blocks / aeon_get_numblocks(btype);
}
// Allocate data blocks. The offset for the allocated block comes back in
// blocknr. Return the number of blocks allocated.
int aeon_new_data_blocks(struct super_block *sb,
struct aeon_inode_info_header *sih, unsigned long *blocknr,
unsigned long start_blk, unsigned long num, int cpu)
{
int allocated;
allocated = aeon_new_blocks(sb, blocknr, num,
sih->i_blk_type, cpu);
if (allocated < 0) {
aeon_dbg("FAILED: Inode (prev)sih->ino, start blk"
"%lu, alloc %d data blocks from %lu to %lu\n",
start_blk, allocated, *blocknr,
*blocknr + allocated - 1);
}
return allocated;
}
/**
* aeon_dax_get_blocks - The function tries to lookup the requested blocks.
* Also it allocates new blcoks if these are needed.
*
* return > 0, # of blocks mapped or allocated.
*/
int aeon_dax_get_blocks(struct inode *inode, unsigned long iblock,
unsigned long max_blocks, u32 *bno, bool *new,
bool *boundary, int create)
{
struct super_block *sb = inode->i_sb;
struct aeon_inode_info *si = AEON_I(inode);
struct aeon_inode_info_header *sih = &si->header;
struct aeon_inode *pi = aeon_get_inode(sb, sih);
struct aeon_extent_header *aeh;
struct aeon_extent *ae;
unsigned long new_d_blocknr = 0;
int allocated;
int length = 0;
int err = -ENOSPC;
if (!pi)
return -ENOENT;
ae = aeon_search_extent(sb, sih, iblock);
if (ae) {
unsigned long offset;
offset = le32_to_cpu(ae->ex_offset);
*bno = le64_to_cpu(ae->ex_block);
length = le16_to_cpu(ae->ex_length) - (iblock - offset);
*bno += (iblock - offset);
return length;
}
if (!create) {
length = 0;
return length;
}
mutex_lock(&sih->truncate_mutex);
aeh = aeon_get_extent_header(pi);
if (!pi->i_exblocks) {
pi->i_new = 0;
pi->i_exblocks++;
aeon_init_extent_header(aeh);
}
allocated = aeon_new_data_blocks(sb, sih, &new_d_blocknr,
iblock, max_blocks, ANY_CPU);
if (allocated <= 0) {
aeon_err(sb, "failed to get data blocks\n");
mutex_unlock(&sih->truncate_mutex);
return err;
}
err = aeon_update_extent(sb, inode, new_d_blocknr, iblock, allocated);
if (err) {
aeon_err(sb, "failed to update extent\n");
mutex_unlock(&sih->truncate_mutex);
return err;
}
*bno = new_d_blocknr;
clean_bdev_aliases(sb->s_bdev, *bno, allocated);
err = sb_issue_zeroout(sb, *bno, allocated, GFP_NOFS);
if (err) {
aeon_err(sb, "%s: ERROR\n", __func__);
mutex_unlock(&sih->truncate_mutex);
return err;
}
*new = true;
mutex_unlock(&sih->truncate_mutex);
return allocated;
}
static void icache_create(struct aeon_sb_info *sbi,
struct inode_map *inode_map)
{
struct icache *init;
init = kmalloc(sizeof(struct icache), GFP_KERNEL);
inode_map->im = init;
INIT_LIST_HEAD(&inode_map->im->imem_list);
}
static void do_aeon_init_new_inode_block(struct super_block *sb,
int cpu_id, u32 ino)
{
struct aeon_sb_info *sbi = AEON_SB(sb);
struct inode_map *inode_map = aeon_get_inode_map(sb, cpu_id);
struct free_list *free_list = aeon_get_free_list(sb, cpu_id);
struct rb_root *tree;
struct rb_node *temp;
u64 addr = AEON_HEAD(sb) + AEON_SB_SIZE + AEON_INODE_SIZE;
__le32 *table_blocknr;
unsigned long table_addr;
struct aeon_range_node *node;
unsigned long blocknr = 0;
u64 temp_addr;
int prealloc_blocks = AEON_PAGES_FOR_INODE+1;
if (!(sbi->s_mount_opt & AEON_MOUNT_FORMAT)) {
struct aeon_region_table *art;
//table_blocknr = (__le32 *)(cpu_id * 32 + addr);
//blocknr = le64_to_cpu(*table_blocknr);
//inode_map->i_table_addr = (void *)((blocknr << AEON_SHIFT) +
// AEON_HEAD(sb));
//art = AEON_R_TABLE(inode_map);
art = aeon_get_rtable(sb, cpu_id);
free_list->num_free_blocks = le64_to_cpu(art->num_free_blocks);
icache_create(sbi, inode_map);
return;
}
spin_lock(&free_list->s_lock);
tree = &(free_list->block_free_tree);
temp = &(free_list->first_node->node);
node = container_of(temp, struct aeon_range_node, node);
blocknr = node->range_low;
temp_addr = (blocknr << AEON_SHIFT) + AEON_HEAD(sb);
memset((void *)temp_addr, 0, 4096 * prealloc_blocks);
node->range_low += prealloc_blocks;
free_list->num_free_blocks -= prealloc_blocks;
table_blocknr = (__le32 *)(cpu_id * 32 + addr);
*table_blocknr = cpu_to_le32(blocknr);
table_addr = blocknr << AEON_SHIFT;
inode_map->i_table_addr = (void *)(table_addr + AEON_HEAD(sb));
spin_unlock(&free_list->s_lock);
icache_create(sbi, inode_map);
}
static void aeon_register_next_inode_block(struct super_block *sb,
struct inode_map *inode_map,
struct aeon_region_table *art,
unsigned long blocknr)
{
struct aeon_inode *pi;
unsigned long prev_blocknr = le64_to_cpu(art->i_blocknr);