forked from gkostka/lwext4
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ext4_extent.c
2140 lines (1843 loc) · 57.2 KB
/
ext4_extent.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
/*
* Copyright (c) 2017 Grzegorz Kostka (kostka.grzegorz@gmail.com)
* Copyright (c) 2017 Kaho Ng (ngkaho1234@gmail.com)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*/
#include <ext4_config.h>
#include <ext4_types.h>
#include <ext4_misc.h>
#include <ext4_errno.h>
#include <ext4_debug.h>
#include <ext4_blockdev.h>
#include <ext4_trans.h>
#include <ext4_fs.h>
#include <ext4_super.h>
#include <ext4_crc32.h>
#include <ext4_balloc.h>
#include <ext4_extent.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include <stddef.h>
#if CONFIG_EXTENTS_ENABLE
/*
* used by extent splitting.
*/
#define EXT4_EXT_MARK_UNWRIT1 0x02 /* mark first half unwritten */
#define EXT4_EXT_MARK_UNWRIT2 0x04 /* mark second half unwritten */
#define EXT4_EXT_DATA_VALID1 0x08 /* first half contains valid data */
#define EXT4_EXT_DATA_VALID2 0x10 /* second half contains valid data */
#define EXT4_EXT_NO_COMBINE 0x20 /* do not combine two extents */
#define EXT4_EXT_UNWRITTEN_MASK (1L << 15)
#define EXT4_EXT_MAX_LEN_WRITTEN (1L << 15)
#define EXT4_EXT_MAX_LEN_UNWRITTEN \
(EXT4_EXT_MAX_LEN_WRITTEN - 1)
#define EXT4_EXT_GET_LEN(ex) to_le16((ex)->block_count)
#define EXT4_EXT_GET_LEN_UNWRITTEN(ex) \
(EXT4_EXT_GET_LEN(ex) & ~(EXT4_EXT_UNWRITTEN_MASK))
#define EXT4_EXT_SET_LEN(ex, count) \
((ex)->block_count = to_le16(count))
#define EXT4_EXT_IS_UNWRITTEN(ex) \
(EXT4_EXT_GET_LEN(ex) > EXT4_EXT_MAX_LEN_WRITTEN)
#define EXT4_EXT_SET_UNWRITTEN(ex) \
((ex)->block_count |= to_le16(EXT4_EXT_UNWRITTEN_MASK))
#define EXT4_EXT_SET_WRITTEN(ex) \
((ex)->block_count &= ~(to_le16(EXT4_EXT_UNWRITTEN_MASK)))
/*
* Array of ext4_ext_path contains path to some extent.
* Creation/lookup routines use it for traversal/splitting/etc.
* Truncate uses it to simulate recursive walking.
*/
struct ext4_extent_path {
ext4_fsblk_t p_block;
struct ext4_block block;
int32_t depth;
int32_t maxdepth;
struct ext4_extent_header *header;
struct ext4_extent_index *index;
struct ext4_extent *extent;
};
#pragma pack(push, 1)
/*
* This is the extent tail on-disk structure.
* All other extent structures are 12 bytes long. It turns out that
* block_size % 12 >= 4 for at least all powers of 2 greater than 512, which
* covers all valid ext4 block sizes. Therefore, this tail structure can be
* crammed into the end of the block without having to rebalance the tree.
*/
struct ext4_extent_tail
{
uint32_t et_checksum; /* crc32c(uuid+inum+extent_block) */
};
/*
* This is the extent on-disk structure.
* It's used at the bottom of the tree.
*/
struct ext4_extent {
uint32_t first_block; /* First logical block extent covers */
uint16_t block_count; /* Number of blocks covered by extent */
uint16_t start_hi; /* High 16 bits of physical block */
uint32_t start_lo; /* Low 32 bits of physical block */
};
/*
* This is index on-disk structure.
* It's used at all the levels except the bottom.
*/
struct ext4_extent_index {
uint32_t first_block; /* Index covers logical blocks from 'block' */
/**
* Pointer to the physical block of the next
* level. leaf or next index could be there
* high 16 bits of physical block
*/
uint32_t leaf_lo;
uint16_t leaf_hi;
uint16_t padding;
};
/*
* Each block (leaves and indexes), even inode-stored has header.
*/
struct ext4_extent_header {
uint16_t magic;
uint16_t entries_count; /* Number of valid entries */
uint16_t max_entries_count; /* Capacity of store in entries */
uint16_t depth; /* Has tree real underlying blocks? */
uint32_t generation; /* generation of the tree */
};
#pragma pack(pop)
#define EXT4_EXTENT_MAGIC 0xF30A
#define EXT4_EXTENT_FIRST(header) \
((struct ext4_extent *)(((char *)(header)) + \
sizeof(struct ext4_extent_header)))
#define EXT4_EXTENT_FIRST_INDEX(header) \
((struct ext4_extent_index *)(((char *)(header)) + \
sizeof(struct ext4_extent_header)))
/*
* EXT_INIT_MAX_LEN is the maximum number of blocks we can have in an
* initialized extent. This is 2^15 and not (2^16 - 1), since we use the
* MSB of ee_len field in the extent datastructure to signify if this
* particular extent is an initialized extent or an uninitialized (i.e.
* preallocated).
* EXT_UNINIT_MAX_LEN is the maximum number of blocks we can have in an
* uninitialized extent.
* If ee_len is <= 0x8000, it is an initialized extent. Otherwise, it is an
* uninitialized one. In other words, if MSB of ee_len is set, it is an
* uninitialized extent with only one special scenario when ee_len = 0x8000.
* In this case we can not have an uninitialized extent of zero length and
* thus we make it as a special case of initialized extent with 0x8000 length.
* This way we get better extent-to-group alignment for initialized extents.
* Hence, the maximum number of blocks we can have in an *initialized*
* extent is 2^15 (32768) and in an *uninitialized* extent is 2^15-1 (32767).
*/
#define EXT_INIT_MAX_LEN (1L << 15)
#define EXT_UNWRITTEN_MAX_LEN (EXT_INIT_MAX_LEN - 1)
#define EXT_EXTENT_SIZE sizeof(struct ext4_extent)
#define EXT_INDEX_SIZE sizeof(struct ext4_extent_idx)
#define EXT_FIRST_EXTENT(__hdr__) \
((struct ext4_extent *)(((char *)(__hdr__)) + \
sizeof(struct ext4_extent_header)))
#define EXT_FIRST_INDEX(__hdr__) \
((struct ext4_extent_index *)(((char *)(__hdr__)) + \
sizeof(struct ext4_extent_header)))
#define EXT_HAS_FREE_INDEX(__path__) \
(to_le16((__path__)->header->entries_count) < \
to_le16((__path__)->header->max_entries_count))
#define EXT_LAST_EXTENT(__hdr__) \
(EXT_FIRST_EXTENT((__hdr__)) + to_le16((__hdr__)->entries_count) - 1)
#define EXT_LAST_INDEX(__hdr__) \
(EXT_FIRST_INDEX((__hdr__)) + to_le16((__hdr__)->entries_count) - 1)
#define EXT_MAX_EXTENT(__hdr__) \
(EXT_FIRST_EXTENT((__hdr__)) + to_le16((__hdr__)->max_entries_count) - 1)
#define EXT_MAX_INDEX(__hdr__) \
(EXT_FIRST_INDEX((__hdr__)) + to_le16((__hdr__)->max_entries_count) - 1)
#define EXT4_EXTENT_TAIL_OFFSET(hdr) \
(sizeof(struct ext4_extent_header) + \
(sizeof(struct ext4_extent) * to_le16((hdr)->max_entries_count)))
/**@brief Get logical number of the block covered by extent.
* @param extent Extent to load number from
* @return Logical number of the first block covered by extent */
static inline uint32_t ext4_extent_get_first_block(struct ext4_extent *extent)
{
return to_le32(extent->first_block);
}
/**@brief Set logical number of the first block covered by extent.
* @param extent Extent to set number to
* @param iblock Logical number of the first block covered by extent */
static inline void ext4_extent_set_first_block(struct ext4_extent *extent,
uint32_t iblock)
{
extent->first_block = to_le32(iblock);
}
/**@brief Get number of blocks covered by extent.
* @param extent Extent to load count from
* @return Number of blocks covered by extent */
static inline uint16_t ext4_extent_get_block_count(struct ext4_extent *extent)
{
if (EXT4_EXT_IS_UNWRITTEN(extent))
return EXT4_EXT_GET_LEN_UNWRITTEN(extent);
else
return EXT4_EXT_GET_LEN(extent);
}
/**@brief Set number of blocks covered by extent.
* @param extent Extent to load count from
* @param count Number of blocks covered by extent
* @param unwritten Whether the extent is unwritten or not */
static inline void ext4_extent_set_block_count(struct ext4_extent *extent,
uint16_t count, bool unwritten)
{
EXT4_EXT_SET_LEN(extent, count);
if (unwritten)
EXT4_EXT_SET_UNWRITTEN(extent);
}
/**@brief Get physical number of the first block covered by extent.
* @param extent Extent to load number
* @return Physical number of the first block covered by extent */
static inline uint64_t ext4_extent_get_start(struct ext4_extent *extent)
{
return ((uint64_t)to_le16(extent->start_hi)) << 32 |
((uint64_t)to_le32(extent->start_lo));
}
/**@brief Set physical number of the first block covered by extent.
* @param extent Extent to load number
* @param fblock Physical number of the first block covered by extent */
static inline void ext4_extent_set_start(struct ext4_extent *extent, uint64_t fblock)
{
extent->start_lo = to_le32((fblock << 32) >> 32);
extent->start_hi = to_le16((uint16_t)(fblock >> 32));
}
/**@brief Get logical number of the block covered by extent index.
* @param index Extent index to load number from
* @return Logical number of the first block covered by extent index */
static inline uint32_t
ext4_extent_index_get_first_block(struct ext4_extent_index *index)
{
return to_le32(index->first_block);
}
/**@brief Set logical number of the block covered by extent index.
* @param index Extent index to set number to
* @param iblock Logical number of the first block covered by extent index */
static inline void
ext4_extent_index_set_first_block(struct ext4_extent_index *index,
uint32_t iblock)
{
index->first_block = to_le32(iblock);
}
/**@brief Get physical number of block where the child node is located.
* @param index Extent index to load number from
* @return Physical number of the block with child node */
static inline uint64_t
ext4_extent_index_get_leaf(struct ext4_extent_index *index)
{
return ((uint64_t)to_le16(index->leaf_hi)) << 32 |
((uint64_t)to_le32(index->leaf_lo));
}
/**@brief Set physical number of block where the child node is located.
* @param index Extent index to set number to
* @param fblock Ohysical number of the block with child node */
static inline void ext4_extent_index_set_leaf(struct ext4_extent_index *index,
uint64_t fblock)
{
index->leaf_lo = to_le32((fblock << 32) >> 32);
index->leaf_hi = to_le16((uint16_t)(fblock >> 32));
}
/**@brief Get magic value from extent header.
* @param header Extent header to load value from
* @return Magic value of extent header */
static inline uint16_t
ext4_extent_header_get_magic(struct ext4_extent_header *header)
{
return to_le16(header->magic);
}
/**@brief Set magic value to extent header.
* @param header Extent header to set value to
* @param magic Magic value of extent header */
static inline void ext4_extent_header_set_magic(struct ext4_extent_header *header,
uint16_t magic)
{
header->magic = to_le16(magic);
}
/**@brief Get number of entries from extent header
* @param header Extent header to get value from
* @return Number of entries covered by extent header */
static inline uint16_t
ext4_extent_header_get_entries_count(struct ext4_extent_header *header)
{
return to_le16(header->entries_count);
}
/**@brief Set number of entries to extent header
* @param header Extent header to set value to
* @param count Number of entries covered by extent header */
static inline void
ext4_extent_header_set_entries_count(struct ext4_extent_header *header,
uint16_t count)
{
header->entries_count = to_le16(count);
}
/**@brief Get maximum number of entries from extent header
* @param header Extent header to get value from
* @return Maximum number of entries covered by extent header */
static inline uint16_t
ext4_extent_header_get_max_entries_count(struct ext4_extent_header *header)
{
return to_le16(header->max_entries_count);
}
/**@brief Set maximum number of entries to extent header
* @param header Extent header to set value to
* @param max_count Maximum number of entries covered by extent header */
static inline void
ext4_extent_header_set_max_entries_count(struct ext4_extent_header *header,
uint16_t max_count)
{
header->max_entries_count = to_le16(max_count);
}
/**@brief Get depth of extent subtree.
* @param header Extent header to get value from
* @return Depth of extent subtree */
static inline uint16_t
ext4_extent_header_get_depth(struct ext4_extent_header *header)
{
return to_le16(header->depth);
}
/**@brief Set depth of extent subtree.
* @param header Extent header to set value to
* @param depth Depth of extent subtree */
static inline void
ext4_extent_header_set_depth(struct ext4_extent_header *header, uint16_t depth)
{
header->depth = to_le16(depth);
}
/**@brief Get generation from extent header
* @param header Extent header to get value from
* @return Generation */
static inline uint32_t
ext4_extent_header_get_generation(struct ext4_extent_header *header)
{
return to_le32(header->generation);
}
/**@brief Set generation to extent header
* @param header Extent header to set value to
* @param generation Generation */
static inline void
ext4_extent_header_set_generation(struct ext4_extent_header *header,
uint32_t generation)
{
header->generation = to_le32(generation);
}
void ext4_extent_tree_init(struct ext4_inode_ref *inode_ref)
{
/* Initialize extent root header */
struct ext4_extent_header *header =
ext4_inode_get_extent_header(inode_ref->inode);
ext4_extent_header_set_depth(header, 0);
ext4_extent_header_set_entries_count(header, 0);
ext4_extent_header_set_generation(header, 0);
ext4_extent_header_set_magic(header, EXT4_EXTENT_MAGIC);
uint16_t max_entries = (EXT4_INODE_BLOCKS * sizeof(uint32_t) -
sizeof(struct ext4_extent_header)) /
sizeof(struct ext4_extent);
ext4_extent_header_set_max_entries_count(header, max_entries);
inode_ref->dirty = true;
}
static struct ext4_extent_tail *
find_ext4_extent_tail(struct ext4_extent_header *eh)
{
return (struct ext4_extent_tail *)(((char *)eh) +
EXT4_EXTENT_TAIL_OFFSET(eh));
}
static struct ext4_extent_header *ext_inode_hdr(struct ext4_inode *inode)
{
return (struct ext4_extent_header *)inode->blocks;
}
static struct ext4_extent_header *ext_block_hdr(struct ext4_block *block)
{
return (struct ext4_extent_header *)block->data;
}
static uint16_t ext_depth(struct ext4_inode *inode)
{
return to_le16(ext_inode_hdr(inode)->depth);
}
static uint16_t ext4_ext_get_actual_len(struct ext4_extent *ext)
{
return (to_le16(ext->block_count) <= EXT_INIT_MAX_LEN
? to_le16(ext->block_count)
: (to_le16(ext->block_count) - EXT_INIT_MAX_LEN));
}
static void ext4_ext_mark_initialized(struct ext4_extent *ext)
{
ext->block_count = to_le16(ext4_ext_get_actual_len(ext));
}
static void ext4_ext_mark_unwritten(struct ext4_extent *ext)
{
ext->block_count |= to_le16(EXT_INIT_MAX_LEN);
}
static int ext4_ext_is_unwritten(struct ext4_extent *ext)
{
/* Extent with ee_len of 0x8000 is treated as an initialized extent */
return (to_le16(ext->block_count) > EXT_INIT_MAX_LEN);
}
/*
* ext4_ext_pblock:
* combine low and high parts of physical block number into ext4_fsblk_t
*/
static ext4_fsblk_t ext4_ext_pblock(struct ext4_extent *ex)
{
ext4_fsblk_t block;
block = to_le32(ex->start_lo);
block |= ((ext4_fsblk_t)to_le16(ex->start_hi) << 31) << 1;
return block;
}
/*
* ext4_idx_pblock:
* combine low and high parts of a leaf physical block number into ext4_fsblk_t
*/
static ext4_fsblk_t ext4_idx_pblock(struct ext4_extent_index *ix)
{
ext4_fsblk_t block;
block = to_le32(ix->leaf_lo);
block |= ((ext4_fsblk_t)to_le16(ix->leaf_hi) << 31) << 1;
return block;
}
/*
* ext4_ext_store_pblock:
* stores a large physical block number into an extent struct,
* breaking it into parts
*/
static void ext4_ext_store_pblock(struct ext4_extent *ex, ext4_fsblk_t pb)
{
ex->start_lo = to_le32((uint32_t)(pb & 0xffffffff));
ex->start_hi = to_le16((uint16_t)((pb >> 32)) & 0xffff);
}
/*
* ext4_idx_store_pblock:
* stores a large physical block number into an index struct,
* breaking it into parts
*/
static void ext4_idx_store_pblock(struct ext4_extent_index *ix, ext4_fsblk_t pb)
{
ix->leaf_lo = to_le32((uint32_t)(pb & 0xffffffff));
ix->leaf_hi = to_le16((uint16_t)((pb >> 32)) & 0xffff);
}
static int ext4_allocate_single_block(struct ext4_inode_ref *inode_ref,
ext4_fsblk_t goal, ext4_fsblk_t *blockp)
{
return ext4_balloc_alloc_block(inode_ref, goal, blockp);
}
static ext4_fsblk_t ext4_new_meta_blocks(struct ext4_inode_ref *inode_ref,
ext4_fsblk_t goal,
uint32_t flags __unused,
uint32_t *count, int *errp)
{
ext4_fsblk_t block = 0;
*errp = ext4_allocate_single_block(inode_ref, goal, &block);
if (count)
*count = 1;
return block;
}
static void ext4_ext_free_blocks(struct ext4_inode_ref *inode_ref,
ext4_fsblk_t block, uint32_t count,
uint32_t flags __unused)
{
ext4_balloc_free_blocks(inode_ref, block, count);
}
static uint16_t ext4_ext_space_block(struct ext4_inode_ref *inode_ref)
{
uint16_t size;
uint32_t block_size = ext4_sb_get_block_size(&inode_ref->fs->sb);
size = (block_size - sizeof(struct ext4_extent_header)) /
sizeof(struct ext4_extent);
#ifdef AGGRESSIVE_TEST
if (size > 6)
size = 6;
#endif
return size;
}
static uint16_t ext4_ext_space_block_idx(struct ext4_inode_ref *inode_ref)
{
uint16_t size;
uint32_t block_size = ext4_sb_get_block_size(&inode_ref->fs->sb);
size = (block_size - sizeof(struct ext4_extent_header)) /
sizeof(struct ext4_extent_index);
#ifdef AGGRESSIVE_TEST
if (size > 5)
size = 5;
#endif
return size;
}
static uint16_t ext4_ext_space_root(struct ext4_inode_ref *inode_ref)
{
uint16_t size;
size = sizeof(inode_ref->inode->blocks);
size -= sizeof(struct ext4_extent_header);
size /= sizeof(struct ext4_extent);
#ifdef AGGRESSIVE_TEST
if (size > 3)
size = 3;
#endif
return size;
}
static uint16_t ext4_ext_space_root_idx(struct ext4_inode_ref *inode_ref)
{
uint16_t size;
size = sizeof(inode_ref->inode->blocks);
size -= sizeof(struct ext4_extent_header);
size /= sizeof(struct ext4_extent_index);
#ifdef AGGRESSIVE_TEST
if (size > 4)
size = 4;
#endif
return size;
}
static uint16_t ext4_ext_max_entries(struct ext4_inode_ref *inode_ref,
uint32_t depth)
{
uint16_t max;
if (depth == ext_depth(inode_ref->inode)) {
if (depth == 0)
max = ext4_ext_space_root(inode_ref);
else
max = ext4_ext_space_root_idx(inode_ref);
} else {
if (depth == 0)
max = ext4_ext_space_block(inode_ref);
else
max = ext4_ext_space_block_idx(inode_ref);
}
return max;
}
static ext4_fsblk_t ext4_ext_find_goal(struct ext4_inode_ref *inode_ref,
struct ext4_extent_path *path,
ext4_lblk_t block)
{
if (path) {
uint32_t depth = path->depth;
struct ext4_extent *ex;
/*
* Try to predict block placement assuming that we are
* filling in a file which will eventually be
* non-sparse --- i.e., in the case of libbfd writing
* an ELF object sections out-of-order but in a way
* the eventually results in a contiguous object or
* executable file, or some database extending a table
* space file. However, this is actually somewhat
* non-ideal if we are writing a sparse file such as
* qemu or KVM writing a raw image file that is going
* to stay fairly sparse, since it will end up
* fragmenting the file system's free space. Maybe we
* should have some hueristics or some way to allow
* userspace to pass a hint to file system,
* especially if the latter case turns out to be
* common.
*/
ex = path[depth].extent;
if (ex) {
ext4_fsblk_t ext_pblk = ext4_ext_pblock(ex);
ext4_lblk_t ext_block = to_le32(ex->first_block);
if (block > ext_block)
return ext_pblk + (block - ext_block);
else
return ext_pblk - (ext_block - block);
}
/* it looks like index is empty;
* try to find starting block from index itself */
if (path[depth].block.lb_id)
return path[depth].block.lb_id;
}
/* OK. use inode's group */
return ext4_fs_inode_to_goal_block(inode_ref);
}
/*
* Allocation for a meta data block
*/
static ext4_fsblk_t ext4_ext_new_meta_block(struct ext4_inode_ref *inode_ref,
struct ext4_extent_path *path,
struct ext4_extent *ex, int *err,
uint32_t flags)
{
ext4_fsblk_t goal, newblock;
goal = ext4_ext_find_goal(inode_ref, path, to_le32(ex->first_block));
newblock = ext4_new_meta_blocks(inode_ref, goal, flags, NULL, err);
return newblock;
}
#if CONFIG_META_CSUM_ENABLE
static uint32_t ext4_ext_block_csum(struct ext4_inode_ref *inode_ref,
struct ext4_extent_header *eh)
{
uint32_t checksum = 0;
struct ext4_sblock *sb = &inode_ref->fs->sb;
if (ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM)) {
uint32_t ino_index = to_le32(inode_ref->index);
uint32_t ino_gen =
to_le32(ext4_inode_get_generation(inode_ref->inode));
/* First calculate crc32 checksum against fs uuid */
checksum =
ext4_crc32c(EXT4_CRC32_INIT, sb->uuid, sizeof(sb->uuid));
/* Then calculate crc32 checksum against inode number
* and inode generation */
checksum = ext4_crc32c(checksum, &ino_index, sizeof(ino_index));
checksum = ext4_crc32c(checksum, &ino_gen, sizeof(ino_gen));
/* Finally calculate crc32 checksum against
* the entire extent block up to the checksum field */
checksum =
ext4_crc32c(checksum, eh, EXT4_EXTENT_TAIL_OFFSET(eh));
}
return checksum;
}
#else
#define ext4_ext_block_csum(...) 0
#endif
static void
ext4_extent_block_csum_set(struct ext4_inode_ref *inode_ref __unused,
struct ext4_extent_header *eh)
{
struct ext4_extent_tail *tail;
tail = find_ext4_extent_tail(eh);
tail->et_checksum = to_le32(ext4_ext_block_csum(inode_ref, eh));
}
static int ext4_ext_dirty(struct ext4_inode_ref *inode_ref,
struct ext4_extent_path *path)
{
if (path->block.lb_id)
ext4_trans_set_block_dirty(path->block.buf);
else
inode_ref->dirty = true;
return EOK;
}
static void ext4_ext_drop_refs(struct ext4_inode_ref *inode_ref,
struct ext4_extent_path *path, bool keep_other)
{
int32_t depth, i;
if (!path)
return;
if (keep_other)
depth = 0;
else
depth = path->depth;
for (i = 0; i <= depth; i++, path++) {
if (path->block.lb_id) {
if (ext4_bcache_test_flag(path->block.buf, BC_DIRTY))
ext4_extent_block_csum_set(inode_ref,
path->header);
ext4_block_set(inode_ref->fs->bdev, &path->block);
}
}
}
/*
* Check that whether the basic information inside the extent header
* is correct or not.
*/
static int ext4_ext_check(struct ext4_inode_ref *inode_ref,
struct ext4_extent_header *eh, uint16_t depth,
ext4_fsblk_t pblk __unused)
{
struct ext4_extent_tail *tail;
struct ext4_sblock *sb = &inode_ref->fs->sb;
const char *error_msg;
(void)error_msg;
if (to_le16(eh->magic) != EXT4_EXTENT_MAGIC) {
error_msg = "invalid magic";
goto corrupted;
}
if (to_le16(eh->depth) != depth) {
error_msg = "unexpected eh_depth";
goto corrupted;
}
if (eh->max_entries_count == 0) {
error_msg = "invalid eh_max";
goto corrupted;
}
if (to_le16(eh->entries_count) > to_le16(eh->max_entries_count)) {
error_msg = "invalid eh_entries";
goto corrupted;
}
tail = find_ext4_extent_tail(eh);
if (ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM)) {
if (tail->et_checksum !=
to_le32(ext4_ext_block_csum(inode_ref, eh))) {
ext4_dbg(DEBUG_EXTENT,
DBG_WARN "Extent block checksum failed."
"Blocknr: %" PRIu64 "\n",
pblk);
}
}
return EOK;
corrupted:
ext4_dbg(DEBUG_EXTENT, "Bad extents B+ tree block: %s. "
"Blocknr: %" PRId64 "\n",
error_msg, pblk);
return EIO;
}
static int read_extent_tree_block(struct ext4_inode_ref *inode_ref,
ext4_fsblk_t pblk, int32_t depth,
struct ext4_block *bh,
uint32_t flags __unused)
{
int err;
err = ext4_trans_block_get(inode_ref->fs->bdev, bh, pblk);
if (err != EOK)
goto errout;
err = ext4_ext_check(inode_ref, ext_block_hdr(bh), depth, pblk);
if (err != EOK)
goto errout;
return EOK;
errout:
if (bh->lb_id)
ext4_block_set(inode_ref->fs->bdev, bh);
return err;
}
/*
* ext4_ext_binsearch_idx:
* binary search for the closest index of the given block
* the header must be checked before calling this
*/
static void ext4_ext_binsearch_idx(struct ext4_extent_path *path,
ext4_lblk_t block)
{
struct ext4_extent_header *eh = path->header;
struct ext4_extent_index *r, *l, *m;
l = EXT_FIRST_INDEX(eh) + 1;
r = EXT_LAST_INDEX(eh);
while (l <= r) {
m = l + (r - l) / 2;
if (block < to_le32(m->first_block))
r = m - 1;
else
l = m + 1;
}
path->index = l - 1;
}
/*
* ext4_ext_binsearch:
* binary search for closest extent of the given block
* the header must be checked before calling this
*/
static void ext4_ext_binsearch(struct ext4_extent_path *path, ext4_lblk_t block)
{
struct ext4_extent_header *eh = path->header;
struct ext4_extent *r, *l, *m;
if (eh->entries_count == 0) {
/*
* this leaf is empty:
* we get such a leaf in split/add case
*/
return;
}
l = EXT_FIRST_EXTENT(eh) + 1;
r = EXT_LAST_EXTENT(eh);
while (l <= r) {
m = l + (r - l) / 2;
if (block < to_le32(m->first_block))
r = m - 1;
else
l = m + 1;
}
path->extent = l - 1;
}
static int ext4_find_extent(struct ext4_inode_ref *inode_ref, ext4_lblk_t block,
struct ext4_extent_path **orig_path, uint32_t flags)
{
struct ext4_extent_header *eh;
struct ext4_block bh = EXT4_BLOCK_ZERO();
ext4_fsblk_t buf_block = 0;
struct ext4_extent_path *path = *orig_path;
int32_t depth, ppos = 0;
int32_t i;
int ret;
eh = ext_inode_hdr(inode_ref->inode);
depth = ext_depth(inode_ref->inode);
if (path) {
ext4_ext_drop_refs(inode_ref, path, 0);
if (depth > path[0].maxdepth) {
ext4_free(path);
*orig_path = path = NULL;
}
}
if (!path) {
int32_t path_depth = depth + 1;
/* account possible depth increase */
path = ext4_calloc(1, sizeof(struct ext4_extent_path) *
(path_depth + 1));
if (!path)
return ENOMEM;
path[0].maxdepth = path_depth;
}
path[0].header = eh;
path[0].block = bh;
i = depth;
/* walk through the tree */
while (i) {
ext4_ext_binsearch_idx(path + ppos, block);
path[ppos].p_block = ext4_idx_pblock(path[ppos].index);
path[ppos].depth = i;
path[ppos].extent = NULL;
buf_block = path[ppos].p_block;
i--;
ppos++;
if (!path[ppos].block.lb_id ||
path[ppos].block.lb_id != buf_block) {
ret = read_extent_tree_block(inode_ref, buf_block, i,
&bh, flags);
if (ret != EOK) {
goto err;
}
if (ppos > depth) {
ext4_block_set(inode_ref->fs->bdev, &bh);
ret = EIO;
goto err;
}
eh = ext_block_hdr(&bh);
path[ppos].block = bh;
path[ppos].header = eh;
}
}
path[ppos].depth = i;
path[ppos].extent = NULL;
path[ppos].index = NULL;
/* find extent */
ext4_ext_binsearch(path + ppos, block);
/* if not an empty leaf */
if (path[ppos].extent)
path[ppos].p_block = ext4_ext_pblock(path[ppos].extent);
*orig_path = path;
ret = EOK;
return ret;
err:
ext4_ext_drop_refs(inode_ref, path, 0);
ext4_free(path);
if (orig_path)
*orig_path = NULL;
return ret;
}
static void ext4_ext_init_header(struct ext4_inode_ref *inode_ref,
struct ext4_extent_header *eh, int32_t depth)
{
eh->entries_count = 0;
eh->max_entries_count = to_le16(ext4_ext_max_entries(inode_ref, depth));
eh->magic = to_le16(EXT4_EXTENT_MAGIC);
eh->depth = depth;
}
static int ext4_ext_insert_index(struct ext4_inode_ref *inode_ref,
struct ext4_extent_path *path, int at,
ext4_lblk_t insert_index,
ext4_fsblk_t insert_block, bool set_to_ix)
{
struct ext4_extent_index *ix;
struct ext4_extent_path *curp = path + at;
int len, err;
struct ext4_extent_header *eh;
if (curp->index && insert_index == to_le32(curp->index->first_block))
return EIO;
if (to_le16(curp->header->entries_count) ==
to_le16(curp->header->max_entries_count))
return EIO;
eh = curp->header;
if (curp->index == NULL) {
ix = EXT_FIRST_INDEX(eh);
curp->index = ix;
} else if (insert_index > to_le32(curp->index->first_block)) {
/* insert after */
ix = curp->index + 1;
} else {
/* insert before */
ix = curp->index;
}
if (ix > EXT_MAX_INDEX(eh))
return EIO;
len = EXT_LAST_INDEX(eh) - ix + 1;
ext4_assert(len >= 0);
if (len > 0)
memmove(ix + 1, ix, len * sizeof(struct ext4_extent_index));
ix->first_block = to_le32(insert_index);
ext4_idx_store_pblock(ix, insert_block);
eh->entries_count = to_le16(to_le16(eh->entries_count) + 1);
if (ix > EXT_LAST_INDEX(eh)) {
err = EIO;
goto out;
}
err = ext4_ext_dirty(inode_ref, curp);
out:
if (err == EOK && set_to_ix) {
curp->index = ix;