-
Notifications
You must be signed in to change notification settings - Fork 25
/
inode.c
1653 lines (1443 loc) · 47.5 KB
/
inode.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
/*
* BRIEF DESCRIPTION
*
* Inode methods (allocate/free/read/write).
*
* Copyright 2012-2013 Intel Corporation
* Copyright 2009-2011 Marco Stornelli <marco.stornelli@gmail.com>
* Copyright 2003 Sony Corporation
* Copyright 2003 Matsushita Electric Industrial Co., Ltd.
* 2003-2004 (c) MontaVista Software, Inc. , Steve Longerbeam
* This file is licensed under the terms of the GNU General Public
* License version 2. This program is licensed "as is" without any
* warranty of any kind, whether express or implied.
*/
#include <linux/fs.h>
#include <linux/aio.h>
#include <linux/sched.h>
#include <linux/highuid.h>
#include <linux/module.h>
#include <linux/mpage.h>
#include <linux/backing-dev.h>
#include <linux/types.h>
#include <linux/ratelimit.h>
#include "pmfs.h"
#include "xip.h"
unsigned int blk_type_to_shift[PMFS_BLOCK_TYPE_MAX] = {12, 21, 30};
uint32_t blk_type_to_size[PMFS_BLOCK_TYPE_MAX] = {0x1000, 0x200000, 0x40000000};
/*
* allocate a data block for inode and return it's absolute blocknr.
* Zeroes out the block if zero set. Increments inode->i_blocks.
*/
static int pmfs_new_data_block(struct super_block *sb, struct pmfs_inode *pi,
unsigned long *blocknr, int zero)
{
unsigned int data_bits = blk_type_to_shift[pi->i_blk_type];
int errval = pmfs_new_block(sb, blocknr, pi->i_blk_type, zero);
if (!errval) {
pmfs_memunlock_inode(sb, pi);
le64_add_cpu(&pi->i_blocks,
(1 << (data_bits - sb->s_blocksize_bits)));
pmfs_memlock_inode(sb, pi);
}
return errval;
}
/*
* find the offset to the block represented by the given inode's file
* relative block number.
*/
u64 pmfs_find_data_block(struct inode *inode, unsigned long file_blocknr)
{
struct super_block *sb = inode->i_sb;
struct pmfs_inode *pi = pmfs_get_inode(sb, inode->i_ino);
u32 blk_shift;
unsigned long blk_offset, blocknr = file_blocknr;
unsigned int data_bits = blk_type_to_shift[pi->i_blk_type];
unsigned int meta_bits = META_BLK_SHIFT;
u64 bp;
/* convert the 4K blocks into the actual blocks the inode is using */
blk_shift = data_bits - sb->s_blocksize_bits;
blk_offset = file_blocknr & ((1 << blk_shift) - 1);
blocknr = file_blocknr >> blk_shift;
if (blocknr >= (1UL << (pi->height * meta_bits)))
return 0;
bp = __pmfs_find_data_block(sb, pi, blocknr);
pmfs_dbg1("find_data_block %lx, %x %llx blk_p %p blk_shift %x"
" blk_offset %lx\n", file_blocknr, pi->height, bp,
pmfs_get_block(sb, bp), blk_shift, blk_offset);
if (bp == 0)
return 0;
return bp + (blk_offset << sb->s_blocksize_bits);
}
/* recursive_find_region: recursively search the btree to find hole or data
* in the specified range
* Input:
* block: points to the root of the b-tree
* height: height of the btree
* first_blocknr: first block in the specified range
* last_blocknr: last_blocknr in the specified range
* @data_found: indicates whether data blocks were found
* @hole_found: indicates whether a hole was found
* hole: whether we are looking for a hole or data
*/
static int recursive_find_region(struct super_block *sb, __le64 block,
u32 height, unsigned long first_blocknr, unsigned long last_blocknr,
int *data_found, int *hole_found, int hole)
{
unsigned int meta_bits = META_BLK_SHIFT;
__le64 *node;
unsigned long first_blk, last_blk, node_bits, blocks = 0;
unsigned int first_index, last_index, i;
node_bits = (height - 1) * meta_bits;
first_index = first_blocknr >> node_bits;
last_index = last_blocknr >> node_bits;
node = pmfs_get_block(sb, le64_to_cpu(block));
for (i = first_index; i <= last_index; i++) {
if (height == 1 || node[i] == 0) {
if (node[i]) {
*data_found = 1;
if (!hole)
goto done;
} else {
*hole_found = 1;
}
if (!*hole_found || !hole)
blocks += (1UL << node_bits);
} else {
first_blk = (i == first_index) ? (first_blocknr &
((1 << node_bits) - 1)) : 0;
last_blk = (i == last_index) ? (last_blocknr &
((1 << node_bits) - 1)) : (1 << node_bits) - 1;
blocks += recursive_find_region(sb, node[i], height - 1,
first_blk, last_blk, data_found, hole_found,
hole);
if (!hole && *data_found)
goto done;
/* cond_resched(); */
}
}
done:
return blocks;
}
/*
* find the file offset for SEEK_DATA/SEEK_HOLE
*/
unsigned long pmfs_find_region(struct inode *inode, loff_t *offset, int hole)
{
struct super_block *sb = inode->i_sb;
struct pmfs_inode *pi = pmfs_get_inode(sb, inode->i_ino);
unsigned int data_bits = blk_type_to_shift[pi->i_blk_type];
unsigned long first_blocknr, last_blocknr;
unsigned long blocks = 0, offset_in_block;
int data_found = 0, hole_found = 0;
if (*offset >= inode->i_size)
return -ENXIO;
if (!inode->i_blocks || !pi->root) {
if (hole)
return inode->i_size;
else
return -ENXIO;
}
offset_in_block = *offset & ((1UL << data_bits) - 1);
if (pi->height == 0) {
data_found = 1;
goto out;
}
first_blocknr = *offset >> data_bits;
last_blocknr = inode->i_size >> data_bits;
pmfs_dbg_verbose("find_region offset %llx, first_blocknr %lx,"
" last_blocknr %lx hole %d\n",
*offset, first_blocknr, last_blocknr, hole);
blocks = recursive_find_region(inode->i_sb, pi->root, pi->height,
first_blocknr, last_blocknr, &data_found, &hole_found, hole);
out:
/* Searching data but only hole found till the end */
if (!hole && !data_found && hole_found)
return -ENXIO;
if (data_found && !hole_found) {
/* Searching data but we are already into them */
if (hole)
/* Searching hole but only data found, go to the end */
*offset = inode->i_size;
return 0;
}
/* Searching for hole, hole found and starting inside an hole */
if (hole && hole_found && !blocks) {
/* we found data after it */
if (!data_found)
/* last hole */
*offset = inode->i_size;
return 0;
}
if (offset_in_block) {
blocks--;
*offset += (blocks << data_bits) +
((1 << data_bits) - offset_in_block);
} else {
*offset += blocks << data_bits;
}
return 0;
}
/* examine the meta-data block node up to the end_idx for any non-null
* pointers. if found return false, else return true.
* required to determine if a meta-data block contains no pointers and hence
* can be freed.
*/
static inline bool is_empty_meta_block(__le64 *node, unsigned int start_idx,
unsigned int end_idx)
{
int i, last_idx = (1 << META_BLK_SHIFT) - 1;
for (i = 0; i < start_idx; i++)
if (unlikely(node[i]))
return false;
for (i = end_idx + 1; i <= last_idx; i++)
if (unlikely(node[i]))
return false;
return true;
}
/* recursive_truncate_blocks: recursively deallocate a range of blocks from
* first_blocknr to last_blocknr in the inode's btree.
* Input:
* block: points to the root of the b-tree where the blocks need to be allocated
* height: height of the btree
* first_blocknr: first block in the specified range
* last_blocknr: last_blocknr in the specified range
* end: last byte offset of the range
*/
static int recursive_truncate_blocks(struct super_block *sb, __le64 block,
u32 height, u32 btype, unsigned long first_blocknr,
unsigned long last_blocknr, bool *meta_empty)
{
unsigned long blocknr, first_blk, last_blk;
unsigned int node_bits, first_index, last_index, i;
__le64 *node;
unsigned int freed = 0, bzero;
int start, end;
bool mpty, all_range_freed = true;
struct pmfs_sb_info *sbi = PMFS_SB(sb);
node = pmfs_get_block(sb, le64_to_cpu(block));
node_bits = (height - 1) * META_BLK_SHIFT;
start = first_index = first_blocknr >> node_bits;
end = last_index = last_blocknr >> node_bits;
if (height == 1) {
struct pmfs_blocknode *start_hint = NULL;
mutex_lock(&sbi->s_lock);
for (i = first_index; i <= last_index; i++) {
if (unlikely(!node[i]))
continue;
/* Freeing the data block */
blocknr = pmfs_get_blocknr(sb, le64_to_cpu(node[i]),
btype);
__pmfs_free_block(sb, blocknr, btype, &start_hint);
freed++;
}
mutex_unlock(&sbi->s_lock);
} else {
for (i = first_index; i <= last_index; i++) {
if (unlikely(!node[i]))
continue;
first_blk = (i == first_index) ? (first_blocknr &
((1 << node_bits) - 1)) : 0;
last_blk = (i == last_index) ? (last_blocknr &
((1 << node_bits) - 1)) : (1 << node_bits) - 1;
freed += recursive_truncate_blocks(sb, node[i],
height - 1, btype, first_blk, last_blk, &mpty);
/* cond_resched(); */
if (mpty) {
/* Freeing the meta-data block */
blocknr = pmfs_get_blocknr(sb, le64_to_cpu(
node[i]), PMFS_BLOCK_TYPE_4K);
pmfs_free_block(sb, blocknr,PMFS_BLOCK_TYPE_4K);
} else {
if (i == first_index)
start++;
else if (i == last_index)
end--;
all_range_freed = false;
}
}
}
if (all_range_freed &&
is_empty_meta_block(node, first_index, last_index)) {
*meta_empty = true;
} else {
/* Zero-out the freed range if the meta-block in not empty */
if (start <= end) {
bzero = (end - start + 1) * sizeof(u64);
pmfs_memunlock_block(sb, node);
memset(&node[start], 0, bzero);
pmfs_memlock_block(sb, node);
pmfs_flush_buffer(&node[start], bzero, false);
}
*meta_empty = false;
}
return freed;
}
unsigned int pmfs_free_inode_subtree(struct super_block *sb,
__le64 root, u32 height, u32 btype, unsigned long last_blocknr)
{
unsigned long first_blocknr;
unsigned int freed;
bool mpty;
timing_t free_time;
if (!root)
return 0;
PMFS_START_TIMING(free_tree_t, free_time);
if (height == 0) {
first_blocknr = pmfs_get_blocknr(sb, le64_to_cpu(root),
btype);
pmfs_free_block(sb, first_blocknr, btype);
freed = 1;
} else {
first_blocknr = 0;
freed = recursive_truncate_blocks(sb, root, height, btype,
first_blocknr, last_blocknr, &mpty);
BUG_ON(!mpty);
first_blocknr = pmfs_get_blocknr(sb, le64_to_cpu(root),
PMFS_BLOCK_TYPE_4K);
pmfs_free_block(sb, first_blocknr,PMFS_BLOCK_TYPE_4K);
}
PMFS_END_TIMING(free_tree_t, free_time);
return freed;
}
static void pmfs_decrease_btree_height(struct super_block *sb,
struct pmfs_inode *pi, unsigned long newsize, __le64 newroot)
{
unsigned int height = pi->height, new_height = 0;
unsigned long blocknr, last_blocknr;
__le64 *root;
char b[8];
if (pi->i_blocks == 0 || newsize == 0) {
/* root must be NULL */
BUG_ON(newroot != 0);
goto update_root_and_height;
}
last_blocknr = ((newsize + pmfs_inode_blk_size(pi) - 1) >>
pmfs_inode_blk_shift(pi)) - 1;
while (last_blocknr > 0) {
last_blocknr = last_blocknr >> META_BLK_SHIFT;
new_height++;
}
if (height == new_height)
return;
pmfs_dbg_verbose("reducing tree height %x->%x\n", height, new_height);
while (height > new_height) {
/* freeing the meta block */
root = pmfs_get_block(sb, le64_to_cpu(newroot));
blocknr = pmfs_get_blocknr(sb, le64_to_cpu(newroot),
PMFS_BLOCK_TYPE_4K);
newroot = root[0];
pmfs_free_block(sb, blocknr, PMFS_BLOCK_TYPE_4K);
height--;
}
update_root_and_height:
/* pi->height and pi->root need to be atomically updated. use
* cmpxchg16 here. The following is dependent on a specific layout of
* inode fields */
*(u64 *)b = *(u64 *)pi;
/* pi->height is at offset 2 from pi */
b[2] = (u8)new_height;
/* TODO: the following function assumes cmpxchg16b instruction writes
* 16 bytes atomically. Confirm if it is really true. */
cmpxchg_double_local((u64 *)pi, &pi->root, *(u64 *)pi, pi->root,
*(u64 *)b, newroot);
}
static unsigned long pmfs_inode_count_iblocks_recursive(struct super_block *sb,
__le64 block, u32 height)
{
__le64 *node;
unsigned int i;
unsigned long i_blocks = 0;
if (height == 0)
return 1;
node = pmfs_get_block(sb, le64_to_cpu(block));
for (i = 0; i < (1 << META_BLK_SHIFT); i++) {
if (node[i] == 0)
continue;
i_blocks += pmfs_inode_count_iblocks_recursive(sb, node[i],
height - 1);
}
return i_blocks;
}
static inline unsigned long pmfs_inode_count_iblocks (struct super_block *sb,
struct pmfs_inode *pi, __le64 root)
{
unsigned long iblocks;
if (root == 0)
return 0;
iblocks = pmfs_inode_count_iblocks_recursive(sb, root, pi->height);
return (iblocks << (pmfs_inode_blk_shift(pi) - sb->s_blocksize_bits));
}
/* Support for sparse files: even though pi->i_size may indicate a certain
* last_blocknr, it may not be true for sparse files. Specifically, last_blocknr
* can not be more than the maximum allowed by the inode's tree height.
*/
static inline unsigned long pmfs_sparse_last_blocknr(unsigned int height,
unsigned long last_blocknr)
{
if (last_blocknr >= (1UL << (height * META_BLK_SHIFT)))
last_blocknr = (1UL << (height * META_BLK_SHIFT)) - 1;
return last_blocknr;
}
/*
* Free data blocks from inode in the range start <=> end
*/
static void __pmfs_truncate_blocks(struct inode *inode, loff_t start,
loff_t end)
{
struct super_block *sb = inode->i_sb;
struct pmfs_inode *pi = pmfs_get_inode(sb, inode->i_ino);
unsigned long first_blocknr, last_blocknr;
__le64 root;
unsigned int freed = 0;
unsigned int data_bits = blk_type_to_shift[pi->i_blk_type];
unsigned int meta_bits = META_BLK_SHIFT;
bool mpty;
inode->i_mtime = inode->i_ctime = current_time(inode);
if (!pi->root)
goto end_truncate_blocks;
pmfs_dbg_verbose("truncate: pi %p iblocks %llx %llx %llx %x %llx\n", pi,
pi->i_blocks, start, end, pi->height, pi->i_size);
first_blocknr = (start + (1UL << data_bits) - 1) >> data_bits;
if (pi->i_flags & cpu_to_le32(PMFS_EOFBLOCKS_FL)) {
last_blocknr = (1UL << (pi->height * meta_bits)) - 1;
} else {
if (end == 0)
goto end_truncate_blocks;
last_blocknr = (end - 1) >> data_bits;
last_blocknr = pmfs_sparse_last_blocknr(pi->height,
last_blocknr);
}
if (first_blocknr > last_blocknr)
goto end_truncate_blocks;
root = pi->root;
if (pi->height == 0) {
first_blocknr = pmfs_get_blocknr(sb, le64_to_cpu(root),
pi->i_blk_type);
pmfs_free_block(sb, first_blocknr, pi->i_blk_type);
root = 0;
freed = 1;
} else {
freed = recursive_truncate_blocks(sb, root, pi->height,
pi->i_blk_type, first_blocknr, last_blocknr, &mpty);
if (mpty) {
first_blocknr = pmfs_get_blocknr(sb, le64_to_cpu(root),
PMFS_BLOCK_TYPE_4K);
pmfs_free_block(sb, first_blocknr, PMFS_BLOCK_TYPE_4K);
root = 0;
}
}
/* if we are called during mount, a power/system failure had happened.
* Don't trust inode->i_blocks; recalculate it by rescanning the inode
*/
if (pmfs_is_mounting(sb))
inode->i_blocks = pmfs_inode_count_iblocks(sb, pi, root);
else
inode->i_blocks -= (freed * (1 << (data_bits -
sb->s_blocksize_bits)));
pmfs_memunlock_inode(sb, pi);
pi->i_blocks = cpu_to_le64(inode->i_blocks);
pi->i_mtime = cpu_to_le32(inode->i_mtime.tv_sec);
pi->i_ctime = cpu_to_le32(inode->i_ctime.tv_sec);
pmfs_decrease_btree_height(sb, pi, start, root);
/* Check for the flag EOFBLOCKS is still valid after the set size */
check_eof_blocks(sb, pi, inode->i_size);
pmfs_memlock_inode(sb, pi);
/* now flush the inode's first cacheline which was modified */
pmfs_flush_buffer(pi, 1, false);
return;
end_truncate_blocks:
/* we still need to update ctime and mtime */
pmfs_memunlock_inode(sb, pi);
pi->i_mtime = cpu_to_le32(inode->i_mtime.tv_sec);
pi->i_ctime = cpu_to_le32(inode->i_ctime.tv_sec);
pmfs_memlock_inode(sb, pi);
pmfs_flush_buffer(pi, 1, false);
}
static int pmfs_increase_btree_height(struct super_block *sb,
struct pmfs_inode *pi, u32 new_height)
{
u32 height = pi->height;
__le64 *root, prev_root = pi->root;
unsigned long blocknr;
int errval = 0;
pmfs_dbg_verbose("increasing tree height %x:%x\n", height, new_height);
while (height < new_height) {
/* allocate the meta block */
errval = pmfs_new_block(sb, &blocknr, PMFS_BLOCK_TYPE_4K, 1);
if (errval) {
pmfs_err(sb, "failed to increase btree height\n");
break;
}
blocknr = pmfs_get_block_off(sb, blocknr, PMFS_BLOCK_TYPE_4K);
root = pmfs_get_block(sb, blocknr);
pmfs_memunlock_block(sb, root);
root[0] = prev_root;
pmfs_memlock_block(sb, root);
pmfs_flush_buffer(root, sizeof(*root), false);
prev_root = cpu_to_le64(blocknr);
height++;
}
pmfs_memunlock_inode(sb, pi);
pi->root = prev_root;
pi->height = height;
pmfs_memlock_inode(sb, pi);
return errval;
}
/* recursive_alloc_blocks: recursively allocate a range of blocks from
* first_blocknr to last_blocknr in the inode's btree.
* Input:
* block: points to the root of the b-tree where the blocks need to be allocated
* height: height of the btree
* first_blocknr: first block in the specified range
* last_blocknr: last_blocknr in the specified range
* zero: whether to zero-out the allocated block(s)
*/
static int recursive_alloc_blocks(pmfs_transaction_t *trans,
struct super_block *sb, struct pmfs_inode *pi, __le64 block, u32 height,
unsigned long first_blocknr, unsigned long last_blocknr, bool new_node,
bool zero)
{
int i, errval;
unsigned int meta_bits = META_BLK_SHIFT, node_bits;
__le64 *node;
bool journal_saved = 0;
unsigned long blocknr, first_blk, last_blk;
unsigned int first_index, last_index;
unsigned int flush_bytes;
node = pmfs_get_block(sb, le64_to_cpu(block));
node_bits = (height - 1) * meta_bits;
first_index = first_blocknr >> node_bits;
last_index = last_blocknr >> node_bits;
for (i = first_index; i <= last_index; i++) {
if (height == 1) {
if (node[i] == 0) {
errval = pmfs_new_data_block(sb, pi, &blocknr,
zero);
if (errval) {
pmfs_dbg_verbose("alloc data blk failed"
" %d\n", errval);
/* For later recovery in truncate... */
pmfs_memunlock_inode(sb, pi);
pi->i_flags |= cpu_to_le32(
PMFS_EOFBLOCKS_FL);
pmfs_memlock_inode(sb, pi);
return errval;
}
/* save the meta-data into the journal before
* modifying */
if (new_node == 0 && journal_saved == 0) {
int le_size = (last_index - i + 1) << 3;
pmfs_add_logentry(sb, trans, &node[i],
le_size, LE_DATA);
journal_saved = 1;
}
pmfs_memunlock_block(sb, node);
node[i] = cpu_to_le64(pmfs_get_block_off(sb,
blocknr, pi->i_blk_type));
pmfs_memlock_block(sb, node);
}
} else {
if (node[i] == 0) {
/* allocate the meta block */
errval = pmfs_new_block(sb, &blocknr,
PMFS_BLOCK_TYPE_4K, 1);
if (errval) {
pmfs_dbg_verbose("alloc meta blk"
" failed\n");
goto fail;
}
/* save the meta-data into the journal before
* modifying */
if (new_node == 0 && journal_saved == 0) {
int le_size = (last_index - i + 1) << 3;
pmfs_add_logentry(sb, trans, &node[i],
le_size, LE_DATA);
journal_saved = 1;
}
pmfs_memunlock_block(sb, node);
node[i] = cpu_to_le64(pmfs_get_block_off(sb,
blocknr, PMFS_BLOCK_TYPE_4K));
pmfs_memlock_block(sb, node);
new_node = 1;
}
first_blk = (i == first_index) ? (first_blocknr &
((1 << node_bits) - 1)) : 0;
last_blk = (i == last_index) ? (last_blocknr &
((1 << node_bits) - 1)) : (1 << node_bits) - 1;
errval = recursive_alloc_blocks(trans, sb, pi, node[i],
height - 1, first_blk, last_blk, new_node, zero);
if (errval < 0)
goto fail;
}
}
if (new_node || trans == NULL) {
/* if the changes were not logged, flush the cachelines we may
* have modified */
flush_bytes = (last_index - first_index + 1) * sizeof(node[0]);
pmfs_flush_buffer(&node[first_index], flush_bytes, false);
}
errval = 0;
fail:
return errval;
}
int __pmfs_alloc_blocks(pmfs_transaction_t *trans, struct super_block *sb,
struct pmfs_inode *pi, unsigned long file_blocknr, unsigned int num,
bool zero)
{
int errval;
unsigned long max_blocks;
unsigned int height;
unsigned int data_bits = blk_type_to_shift[pi->i_blk_type];
unsigned int blk_shift, meta_bits = META_BLK_SHIFT;
unsigned long blocknr, first_blocknr, last_blocknr, total_blocks;
timing_t alloc_time;
/* convert the 4K blocks into the actual blocks the inode is using */
blk_shift = data_bits - sb->s_blocksize_bits;
PMFS_START_TIMING(alloc_blocks_t, alloc_time);
first_blocknr = file_blocknr >> blk_shift;
last_blocknr = (file_blocknr + num - 1) >> blk_shift;
pmfs_dbg_verbose("alloc_blocks height %d file_blocknr %lx num %x, "
"first blocknr 0x%lx, last_blocknr 0x%lx\n",
pi->height, file_blocknr, num, first_blocknr, last_blocknr);
height = pi->height;
blk_shift = height * meta_bits;
max_blocks = 0x1UL << blk_shift;
if (last_blocknr > max_blocks - 1) {
/* B-tree height increases as a result of this allocation */
total_blocks = last_blocknr >> blk_shift;
while (total_blocks > 0) {
total_blocks = total_blocks >> meta_bits;
height++;
}
if (height > 3) {
pmfs_dbg("[%s:%d] Max file size. Cant grow the file\n",
__func__, __LINE__);
errval = -ENOSPC;
goto fail;
}
}
if (!pi->root) {
if (height == 0) {
__le64 root;
errval = pmfs_new_data_block(sb, pi, &blocknr, zero);
if (errval) {
pmfs_dbg_verbose("[%s:%d] failed: alloc data"
" block\n", __func__, __LINE__);
goto fail;
}
root = cpu_to_le64(pmfs_get_block_off(sb, blocknr,
pi->i_blk_type));
pmfs_memunlock_inode(sb, pi);
pi->root = root;
pi->height = height;
pmfs_memlock_inode(sb, pi);
} else {
errval = pmfs_increase_btree_height(sb, pi, height);
if (errval) {
pmfs_dbg_verbose("[%s:%d] failed: inc btree"
" height\n", __func__, __LINE__);
goto fail;
}
errval = recursive_alloc_blocks(trans, sb, pi, pi->root,
pi->height, first_blocknr, last_blocknr, 1, zero);
if (errval < 0)
goto fail;
}
} else {
/* Go forward only if the height of the tree is non-zero. */
if (height == 0)
return 0;
if (height > pi->height) {
errval = pmfs_increase_btree_height(sb, pi, height);
if (errval) {
pmfs_dbg_verbose("Err: inc height %x:%x tot %lx"
"\n", pi->height, height, total_blocks);
goto fail;
}
}
errval = recursive_alloc_blocks(trans, sb, pi, pi->root, height,
first_blocknr, last_blocknr, 0, zero);
if (errval < 0)
goto fail;
}
PMFS_END_TIMING(alloc_blocks_t, alloc_time);
return 0;
fail:
PMFS_END_TIMING(alloc_blocks_t, alloc_time);
return errval;
}
/*
* Allocate num data blocks for inode, starting at given file-relative
* block number.
*/
inline int pmfs_alloc_blocks(pmfs_transaction_t *trans, struct inode *inode,
unsigned long file_blocknr, unsigned int num, bool zero)
{
struct super_block *sb = inode->i_sb;
struct pmfs_inode *pi = pmfs_get_inode(sb, inode->i_ino);
int errval;
errval = __pmfs_alloc_blocks(trans, sb, pi, file_blocknr, num, zero);
inode->i_blocks = le64_to_cpu(pi->i_blocks);
return errval;
}
/* Initialize the inode table. The pmfs_inode struct corresponding to the
* inode table has already been zero'd out */
int pmfs_init_inode_table(struct super_block *sb)
{
struct pmfs_inode *pi = pmfs_get_inode_table(sb);
struct pmfs_sb_info *sbi = PMFS_SB(sb);
unsigned long num_blocks = 0, init_inode_table_size;
int errval;
if (sbi->num_inodes == 0) {
/* initial inode table size was not specified. */
if (sbi->initsize >= PMFS_LARGE_INODE_TABLE_THREASHOLD)
init_inode_table_size = PMFS_LARGE_INODE_TABLE_SIZE;
else
init_inode_table_size = PMFS_DEF_BLOCK_SIZE_4K;
} else {
init_inode_table_size = sbi->num_inodes << PMFS_INODE_BITS;
}
pmfs_memunlock_inode(sb, pi);
pi->i_mode = 0;
pi->i_uid = 0;
pi->i_gid = 0;
pi->i_links_count = cpu_to_le16(1);
pi->i_flags = 0;
pi->height = 0;
pi->i_dtime = 0;
if (init_inode_table_size >= PMFS_LARGE_INODE_TABLE_SIZE)
pi->i_blk_type = PMFS_BLOCK_TYPE_2M;
else
pi->i_blk_type = PMFS_BLOCK_TYPE_4K;
num_blocks = (init_inode_table_size + pmfs_inode_blk_size(pi) - 1) >>
pmfs_inode_blk_shift(pi);
pi->i_size = cpu_to_le64(num_blocks << pmfs_inode_blk_shift(pi));
/* pmfs_sync_inode(pi); */
pmfs_memlock_inode(sb, pi);
sbi->s_inodes_count = num_blocks <<
(pmfs_inode_blk_shift(pi) - PMFS_INODE_BITS);
/* calculate num_blocks in terms of 4k blocksize */
num_blocks = num_blocks << (pmfs_inode_blk_shift(pi) -
sb->s_blocksize_bits);
errval = __pmfs_alloc_blocks(NULL, sb, pi, 0, num_blocks, true);
if (errval != 0) {
pmfs_err(sb, "Err: initializing the Inode Table: %d\n", errval);
return errval;
}
/* inode 0 is considered invalid and hence never used */
sbi->s_free_inodes_count =
(sbi->s_inodes_count - PMFS_FREE_INODE_HINT_START);
sbi->s_free_inode_hint = (PMFS_FREE_INODE_HINT_START);
return 0;
}
static int pmfs_read_inode(struct inode *inode, struct pmfs_inode *pi)
{
int ret = -EIO;
#if 0
if (pmfs_calc_checksum((u8 *)pi, PMFS_INODE_SIZE)) {
pmfs_err(inode->i_sb, "checksum error in inode %lx\n",
(u64)inode->i_ino);
goto bad_inode;
}
#endif
inode->i_mode = le16_to_cpu(pi->i_mode);
i_uid_write(inode, le32_to_cpu(pi->i_uid));
i_gid_write(inode, le32_to_cpu(pi->i_gid));
set_nlink(inode, le16_to_cpu(pi->i_links_count));
inode->i_size = le64_to_cpu(pi->i_size);
inode->i_atime.tv_sec = le32_to_cpu(pi->i_atime);
inode->i_ctime.tv_sec = le32_to_cpu(pi->i_ctime);
inode->i_mtime.tv_sec = le32_to_cpu(pi->i_mtime);
inode->i_atime.tv_nsec = inode->i_mtime.tv_nsec =
inode->i_ctime.tv_nsec = 0;
inode->i_generation = le32_to_cpu(pi->i_generation);
pmfs_set_inode_flags(inode, pi);
/* check if the inode is active. */
if (inode->i_nlink == 0 &&
(inode->i_mode == 0 || le32_to_cpu(pi->i_dtime))) {
/* this inode is deleted */
ret = -ESTALE;
goto bad_inode;
}
inode->i_blocks = le64_to_cpu(pi->i_blocks);
inode->i_mapping->a_ops = &pmfs_aops_xip;
switch (inode->i_mode & S_IFMT) {
case S_IFREG:
inode->i_op = &pmfs_file_inode_operations;
inode->i_fop = &pmfs_xip_file_operations;
break;
case S_IFDIR:
inode->i_op = &pmfs_dir_inode_operations;
inode->i_fop = &pmfs_dir_operations;
break;
case S_IFLNK:
inode->i_op = &pmfs_symlink_inode_operations;
break;
default:
inode->i_size = 0;
inode->i_op = &pmfs_special_inode_operations;
init_special_inode(inode, inode->i_mode,
le32_to_cpu(pi->dev.rdev));
break;
}
return 0;
bad_inode:
make_bad_inode(inode);
return ret;
}
static void pmfs_update_inode(struct inode *inode, struct pmfs_inode *pi)
{
pmfs_memunlock_inode(inode->i_sb, pi);
pi->i_mode = cpu_to_le16(inode->i_mode);
pi->i_uid = cpu_to_le32(i_uid_read(inode));
pi->i_gid = cpu_to_le32(i_gid_read(inode));
pi->i_links_count = cpu_to_le16(inode->i_nlink);
pi->i_size = cpu_to_le64(inode->i_size);
pi->i_blocks = cpu_to_le64(inode->i_blocks);
pi->i_atime = cpu_to_le32(inode->i_atime.tv_sec);
pi->i_ctime = cpu_to_le32(inode->i_ctime.tv_sec);
pi->i_mtime = cpu_to_le32(inode->i_mtime.tv_sec);
pi->i_generation = cpu_to_le32(inode->i_generation);
pmfs_get_inode_flags(inode, pi);
if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
pi->dev.rdev = cpu_to_le32(inode->i_rdev);
pmfs_memlock_inode(inode->i_sb, pi);
}
/*
* NOTE! When we get the inode, we're the only people
* that have access to it, and as such there are no
* race conditions we have to worry about. The inode
* is not on the hash-lists, and it cannot be reached
* through the filesystem because the directory entry
* has been deleted earlier.
*/
static int pmfs_free_inode(struct inode *inode)
{
struct super_block *sb = inode->i_sb;
struct pmfs_sb_info *sbi = PMFS_SB(sb);
struct pmfs_inode *pi;
unsigned long inode_nr;
pmfs_transaction_t *trans;
int err = 0;
mutex_lock(&PMFS_SB(sb)->inode_table_mutex);
pmfs_dbg_verbose("free_inode: %lx free_nodes %x tot nodes %x hint %x\n",
inode->i_ino, sbi->s_free_inodes_count, sbi->s_inodes_count,
sbi->s_free_inode_hint);
inode_nr = inode->i_ino >> PMFS_INODE_BITS;
pi = pmfs_get_inode(sb, inode->i_ino);
trans = pmfs_new_transaction(sb, MAX_INODE_LENTRIES);
if (IS_ERR(trans)) {
err = PTR_ERR(trans);
goto out;
}
pmfs_add_logentry(sb, trans, pi, MAX_DATA_PER_LENTRY, LE_DATA);
pmfs_memunlock_inode(sb, pi);
pi->root = 0;
/* pi->i_links_count = 0;
pi->i_xattr = 0; */
pi->i_size = 0;
pi->i_dtime = cpu_to_le32(get_seconds());
pmfs_memlock_inode(sb, pi);
pmfs_commit_transaction(sb, trans);
/* increment s_free_inodes_count */
if (inode_nr < (sbi->s_free_inode_hint))
sbi->s_free_inode_hint = (inode_nr);
sbi->s_free_inodes_count += 1;
if ((sbi->s_free_inodes_count) ==
(sbi->s_inodes_count) - PMFS_FREE_INODE_HINT_START) {
/* filesystem is empty */
pmfs_dbg_verbose("fs is empty!\n");
sbi->s_free_inode_hint = (PMFS_FREE_INODE_HINT_START);
}
pmfs_dbg_verbose("free_inode: free_nodes %x total_nodes %x hint %x\n",
sbi->s_free_inodes_count, sbi->s_inodes_count,
sbi->s_free_inode_hint);
out:
mutex_unlock(&PMFS_SB(sb)->inode_table_mutex);
return err;
}
struct inode *pmfs_iget(struct super_block *sb, unsigned long ino)
{
struct inode *inode;
struct pmfs_inode *pi;
int err;
inode = iget_locked(sb, ino);
if (unlikely(!inode))
return ERR_PTR(-ENOMEM);
if (!(inode->i_state & I_NEW))
return inode;
pi = pmfs_get_inode(sb, ino);
if (!pi) {
err = -EACCES;
goto fail;
}
err = pmfs_read_inode(inode, pi);
if (unlikely(err))
goto fail;
inode->i_ino = ino;
unlock_new_inode(inode);
return inode;