forked from gkostka/lwext4
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ext4_dir_idx.c
1403 lines (1173 loc) · 39 KB
/
ext4_dir_idx.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) 2013 Grzegorz Kostka ([email protected])
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* - The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/** @addtogroup lwext4
* @{
*/
/**
* @file ext4_dir_idx.c
* @brief Directory indexing procedures.
*/
#include <ext4_config.h>
#include <ext4_types.h>
#include <ext4_misc.h>
#include <ext4_errno.h>
#include <ext4_debug.h>
#include <ext4_trans.h>
#include <ext4_dir_idx.h>
#include <ext4_dir.h>
#include <ext4_blockdev.h>
#include <ext4_fs.h>
#include <ext4_super.h>
#include <ext4_inode.h>
#include <ext4_crc32.h>
#include <ext4_hash.h>
#include <string.h>
#include <stdlib.h>
/**@brief Get hash version used in directory index.
* @param root_info Pointer to root info structure of index
* @return Hash algorithm version
*/
static inline uint8_t
ext4_dir_dx_rinfo_get_hash_version(struct ext4_dir_idx_rinfo *ri)
{
return ri->hash_version;
}
/**@brief Set hash version, that will be used in directory index.
* @param root_info Pointer to root info structure of index
* @param v Hash algorithm version
*/
static inline void
ext4_dir_dx_rinfo_set_hash_version(struct ext4_dir_idx_rinfo *ri, uint8_t v)
{
ri->hash_version = v;
}
/**@brief Get length of root_info structure in bytes.
* @param root_info Pointer to root info structure of index
* @return Length of the structure
*/
static inline uint8_t
ext4_dir_dx_rinfo_get_info_length(struct ext4_dir_idx_rinfo *ri)
{
return ri->info_length;
}
/**@brief Set length of root_info structure in bytes.
* @param root_info Pointer to root info structure of index
* @param info_length Length of the structure
*/
static inline void
ext4_dir_dx_root_info_set_info_length(struct ext4_dir_idx_rinfo *ri,
uint8_t len)
{
ri->info_length = len;
}
/**@brief Get number of indirect levels of HTree.
* @param root_info Pointer to root info structure of index
* @return Height of HTree (actually only 0 or 1)
*/
static inline uint8_t
ext4_dir_dx_rinfo_get_indirect_levels(struct ext4_dir_idx_rinfo *ri)
{
return ri->indirect_levels;
}
/**@brief Set number of indirect levels of HTree.
* @param root_info Pointer to root info structure of index
* @param lvl Height of HTree (actually only 0 or 1)
*/
static inline void
ext4_dir_dx_rinfo_set_indirect_levels(struct ext4_dir_idx_rinfo *ri, uint8_t l)
{
ri->indirect_levels = l;
}
/**@brief Get maximum number of index node entries.
* @param climit Pointer to counlimit structure
* @return Maximum of entries in node
*/
static inline uint16_t
ext4_dir_dx_climit_get_limit(struct ext4_dir_idx_climit *climit)
{
return to_le16(climit->limit);
}
/**@brief Set maximum number of index node entries.
* @param climit Pointer to counlimit structure
* @param limit Maximum of entries in node
*/
static inline void
ext4_dir_dx_climit_set_limit(struct ext4_dir_idx_climit *climit, uint16_t limit)
{
climit->limit = to_le16(limit);
}
/**@brief Get current number of index node entries.
* @param climit Pointer to counlimit structure
* @return Number of entries in node
*/
static inline uint16_t
ext4_dir_dx_climit_get_count(struct ext4_dir_idx_climit *climit)
{
return to_le16(climit->count);
}
/**@brief Set current number of index node entries.
* @param climit Pointer to counlimit structure
* @param count Number of entries in node
*/
static inline void
ext4_dir_dx_climit_set_count(struct ext4_dir_idx_climit *climit, uint16_t count)
{
climit->count = to_le16(count);
}
/**@brief Get hash value of index entry.
* @param entry Pointer to index entry
* @return Hash value
*/
static inline uint32_t
ext4_dir_dx_entry_get_hash(struct ext4_dir_idx_entry *entry)
{
return to_le32(entry->hash);
}
/**@brief Set hash value of index entry.
* @param entry Pointer to index entry
* @param hash Hash value
*/
static inline void
ext4_dir_dx_entry_set_hash(struct ext4_dir_idx_entry *entry, uint32_t hash)
{
entry->hash = to_le32(hash);
}
/**@brief Get block address where child node is located.
* @param entry Pointer to index entry
* @return Block address of child node
*/
static inline uint32_t
ext4_dir_dx_entry_get_block(struct ext4_dir_idx_entry *entry)
{
return to_le32(entry->block);
}
/**@brief Set block address where child node is located.
* @param entry Pointer to index entry
* @param block Block address of child node
*/
static inline void
ext4_dir_dx_entry_set_block(struct ext4_dir_idx_entry *entry, uint32_t block)
{
entry->block = to_le32(block);
}
/**@brief Sort entry item.*/
struct ext4_dx_sort_entry {
uint32_t hash;
uint32_t rec_len;
void *dentry;
};
static int ext4_dir_dx_hash_string(struct ext4_hash_info *hinfo, int len,
const char *name)
{
return ext2_htree_hash(name, len, hinfo->seed, hinfo->hash_version,
&hinfo->hash, &hinfo->minor_hash);
}
#if CONFIG_META_CSUM_ENABLE
static uint32_t ext4_dir_dx_checksum(struct ext4_inode_ref *inode_ref, void *de,
int count_offset, int count,
struct ext4_dir_idx_tail *t)
{
uint32_t orig_cum, csum = 0;
struct ext4_sblock *sb = &inode_ref->fs->sb;
int sz;
/* Compute the checksum only if the filesystem supports it */
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;
ino_gen = to_le32(ext4_inode_get_generation(inode_ref->inode));
sz = count_offset + (count * sizeof(struct ext4_dir_idx_tail));
orig_cum = t->checksum;
t->checksum = 0;
/* First calculate crc32 checksum against fs uuid */
csum = ext4_crc32c(EXT4_CRC32_INIT, sb->uuid, sizeof(sb->uuid));
/* Then calculate crc32 checksum against inode number
* and inode generation */
csum = ext4_crc32c(csum, &ino_index, sizeof(ino_index));
csum = ext4_crc32c(csum, &ino_gen, sizeof(ino_gen));
/* After that calculate crc32 checksum against all the dx_entry */
csum = ext4_crc32c(csum, de, sz);
/* Finally calculate crc32 checksum for dx_tail */
csum = ext4_crc32c(csum, t, sizeof(struct ext4_dir_idx_tail));
t->checksum = orig_cum;
}
return csum;
}
static struct ext4_dir_idx_climit *
ext4_dir_dx_get_climit(struct ext4_inode_ref *inode_ref,
struct ext4_dir_en *dirent, int *offset)
{
struct ext4_dir_en *dp;
struct ext4_dir_idx_root *root;
struct ext4_sblock *sb = &inode_ref->fs->sb;
uint32_t block_size = ext4_sb_get_block_size(sb);
uint16_t entry_len = ext4_dir_en_get_entry_len(dirent);
int count_offset;
if (entry_len == 12) {
root = (struct ext4_dir_idx_root *)dirent;
dp = (struct ext4_dir_en *)&root->dots[1];
if (ext4_dir_en_get_entry_len(dp) != (block_size - 12))
return NULL;
if (root->info.reserved_zero)
return NULL;
if (root->info.info_length != sizeof(struct ext4_dir_idx_rinfo))
return NULL;
count_offset = 32;
} else if (entry_len == block_size) {
count_offset = 8;
} else {
return NULL;
}
if (offset)
*offset = count_offset;
return (struct ext4_dir_idx_climit *)(((char *)dirent) + count_offset);
}
/*
* BIG FAT NOTES:
* Currently we do not verify the checksum of HTree node.
*/
static bool ext4_dir_dx_csum_verify(struct ext4_inode_ref *inode_ref,
struct ext4_dir_en *de)
{
struct ext4_sblock *sb = &inode_ref->fs->sb;
uint32_t block_size = ext4_sb_get_block_size(sb);
int coff, limit, cnt;
if (ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM)) {
struct ext4_dir_idx_climit *climit;
climit = ext4_dir_dx_get_climit(inode_ref, de, &coff);
if (!climit) {
/* Directory seems corrupted. */
return true;
}
struct ext4_dir_idx_tail *t;
limit = ext4_dir_dx_climit_get_limit(climit);
cnt = ext4_dir_dx_climit_get_count(climit);
if (coff + (limit * sizeof(struct ext4_dir_idx_entry)) >
(block_size - sizeof(struct ext4_dir_idx_tail))) {
/* There is no space to hold the checksum */
return true;
}
t = (void *)(((struct ext4_dir_idx_entry *)climit) + limit);
uint32_t c;
c = to_le32(ext4_dir_dx_checksum(inode_ref, de, coff, cnt, t));
if (t->checksum != c)
return false;
}
return true;
}
static void ext4_dir_set_dx_csum(struct ext4_inode_ref *inode_ref,
struct ext4_dir_en *dirent)
{
int coff, limit, count;
struct ext4_sblock *sb = &inode_ref->fs->sb;
uint32_t block_size = ext4_sb_get_block_size(sb);
if (ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM)) {
struct ext4_dir_idx_climit *climit;
climit = ext4_dir_dx_get_climit(inode_ref, dirent, &coff);
if (!climit) {
/* Directory seems corrupted. */
return;
}
struct ext4_dir_idx_tail *t;
limit = ext4_dir_dx_climit_get_limit(climit);
count = ext4_dir_dx_climit_get_count(climit);
if (coff + (limit * sizeof(struct ext4_dir_idx_entry)) >
(block_size - sizeof(struct ext4_dir_idx_tail))) {
/* There is no space to hold the checksum */
return;
}
t = (void *)(((struct ext4_dir_idx_entry *)climit) + limit);
t->checksum = to_le32(ext4_dir_dx_checksum(inode_ref, dirent,
coff, count, t));
}
}
#else
#define ext4_dir_dx_csum_verify(...) true
#define ext4_dir_set_dx_csum(...)
#endif
/****************************************************************************/
int ext4_dir_dx_init(struct ext4_inode_ref *dir, struct ext4_inode_ref *parent)
{
/* Load block 0, where will be index root located */
ext4_fsblk_t fblock;
uint32_t iblock = 0;
bool need_append =
(ext4_inode_get_size(&dir->fs->sb, dir->inode)
< EXT4_DIR_DX_INIT_BCNT)
? true : false;
struct ext4_sblock *sb = &dir->fs->sb;
uint32_t block_size = ext4_sb_get_block_size(&dir->fs->sb);
struct ext4_block block;
int rc;
if (!need_append)
rc = ext4_fs_init_inode_dblk_idx(dir, iblock, &fblock);
else
rc = ext4_fs_append_inode_dblk(dir, &fblock, &iblock);
if (rc != EOK)
return rc;
rc = ext4_trans_block_get_noread(dir->fs->bdev, &block, fblock);
if (rc != EOK)
return rc;
/* Initialize pointers to data structures */
struct ext4_dir_idx_root *root = (void *)block.data;
struct ext4_dir_idx_rinfo *info = &(root->info);
memset(root, 0, sizeof(struct ext4_dir_idx_root));
struct ext4_dir_en *de;
/* Initialize dot entries */
de = (struct ext4_dir_en *)root->dots;
ext4_dir_write_entry(sb, de, 12, dir, ".", strlen("."));
de = (struct ext4_dir_en *)(root->dots + 1);
uint16_t elen = block_size - 12;
ext4_dir_write_entry(sb, de, elen, parent, "..", strlen(".."));
/* Initialize root info structure */
uint8_t hash_version = ext4_get8(&dir->fs->sb, default_hash_version);
ext4_dir_dx_rinfo_set_hash_version(info, hash_version);
ext4_dir_dx_rinfo_set_indirect_levels(info, 0);
ext4_dir_dx_root_info_set_info_length(info, 8);
/* Set limit and current number of entries */
struct ext4_dir_idx_climit *climit;
climit = (struct ext4_dir_idx_climit *)&root->en;
ext4_dir_dx_climit_set_count(climit, 1);
uint32_t entry_space;
entry_space = block_size - 2 * sizeof(struct ext4_dir_idx_dot_en) -
sizeof(struct ext4_dir_idx_rinfo);
if (ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM))
entry_space -= sizeof(struct ext4_dir_idx_tail);
uint16_t root_limit = entry_space / sizeof(struct ext4_dir_idx_entry);
ext4_dir_dx_climit_set_limit(climit, root_limit);
/* Append new block, where will be new entries inserted in the future */
iblock++;
if (!need_append)
rc = ext4_fs_init_inode_dblk_idx(dir, iblock, &fblock);
else
rc = ext4_fs_append_inode_dblk(dir, &fblock, &iblock);
if (rc != EOK) {
ext4_block_set(dir->fs->bdev, &block);
return rc;
}
struct ext4_block new_block;
rc = ext4_trans_block_get_noread(dir->fs->bdev, &new_block, fblock);
if (rc != EOK) {
ext4_block_set(dir->fs->bdev, &block);
return rc;
}
/* Fill the whole block with empty entry */
struct ext4_dir_en *be = (void *)new_block.data;
if (ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM)) {
uint16_t len = block_size - sizeof(struct ext4_dir_entry_tail);
ext4_dir_en_set_entry_len(be, len);
ext4_dir_en_set_name_len(sb, be, 0);
ext4_dir_en_set_inode_type(sb, be, EXT4_DE_UNKNOWN);
ext4_dir_init_entry_tail(EXT4_DIRENT_TAIL(be, block_size));
ext4_dir_set_csum(dir, be);
} else {
ext4_dir_en_set_entry_len(be, block_size);
}
ext4_dir_en_set_inode(be, 0);
ext4_trans_set_block_dirty(new_block.buf);
rc = ext4_block_set(dir->fs->bdev, &new_block);
if (rc != EOK) {
ext4_block_set(dir->fs->bdev, &block);
return rc;
}
/* Connect new block to the only entry in index */
struct ext4_dir_idx_entry *entry = root->en;
ext4_dir_dx_entry_set_block(entry, iblock);
ext4_dir_set_dx_csum(dir, (struct ext4_dir_en *)block.data);
ext4_trans_set_block_dirty(block.buf);
return ext4_block_set(dir->fs->bdev, &block);
}
/**@brief Initialize hash info structure necessary for index operations.
* @param hinfo Pointer to hinfo to be initialized
* @param root_block Root block (number 0) of index
* @param sb Pointer to superblock
* @param name_len Length of name to be computed hash value from
* @param name Name to be computed hash value from
* @return Standard error code
*/
static int ext4_dir_hinfo_init(struct ext4_hash_info *hinfo,
struct ext4_block *root_block,
struct ext4_sblock *sb, size_t name_len,
const char *name)
{
struct ext4_dir_idx_root *root;
root = (struct ext4_dir_idx_root *)root_block->data;
if ((root->info.hash_version != EXT2_HTREE_LEGACY) &&
(root->info.hash_version != EXT2_HTREE_HALF_MD4) &&
(root->info.hash_version != EXT2_HTREE_TEA))
return EXT4_ERR_BAD_DX_DIR;
/* Check unused flags */
if (root->info.unused_flags != 0)
return EXT4_ERR_BAD_DX_DIR;
/* Check indirect levels */
if (root->info.indirect_levels > 1)
return EXT4_ERR_BAD_DX_DIR;
/* Check if node limit is correct */
uint32_t block_size = ext4_sb_get_block_size(sb);
uint32_t entry_space = block_size;
entry_space -= 2 * sizeof(struct ext4_dir_idx_dot_en);
entry_space -= sizeof(struct ext4_dir_idx_rinfo);
if (ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM))
entry_space -= sizeof(struct ext4_dir_idx_tail);
entry_space = entry_space / sizeof(struct ext4_dir_idx_entry);
struct ext4_dir_idx_climit *climit = (void *)&root->en;
uint16_t limit = ext4_dir_dx_climit_get_limit(climit);
if (limit != entry_space)
return EXT4_ERR_BAD_DX_DIR;
/* Check hash version and modify if necessary */
hinfo->hash_version = ext4_dir_dx_rinfo_get_hash_version(&root->info);
if ((hinfo->hash_version <= EXT2_HTREE_TEA) &&
(ext4_sb_check_flag(sb, EXT4_SUPERBLOCK_FLAGS_UNSIGNED_HASH))) {
/* Use unsigned hash */
hinfo->hash_version += 3;
}
/* Load hash seed from superblock */
hinfo->seed = ext4_get8(sb, hash_seed);
/* Compute hash value of name */
if (name)
return ext4_dir_dx_hash_string(hinfo, name_len, name);
return EOK;
}
/**@brief Walk through index tree and load leaf with corresponding hash value.
* @param hinfo Initialized hash info structure
* @param inode_ref Current i-node
* @param root_block Root block (iblock 0), where is root node located
* @param dx_block Pointer to leaf node in dx_blocks array
* @param dx_blocks Array with the whole path from root to leaf
* @return Standard error code
*/
static int ext4_dir_dx_get_leaf(struct ext4_hash_info *hinfo,
struct ext4_inode_ref *inode_ref,
struct ext4_block *root_block,
struct ext4_dir_idx_block **dx_block,
struct ext4_dir_idx_block *dx_blocks)
{
struct ext4_dir_idx_root *root;
struct ext4_dir_idx_entry *entries;
struct ext4_dir_idx_entry *p;
struct ext4_dir_idx_entry *q;
struct ext4_dir_idx_entry *m;
struct ext4_dir_idx_entry *at;
ext4_fsblk_t fblk;
uint32_t block_size;
uint16_t limit;
uint16_t entry_space;
uint8_t ind_level;
int r;
struct ext4_dir_idx_block *tmp_dx_blk = dx_blocks;
struct ext4_block *tmp_blk = root_block;
struct ext4_sblock *sb = &inode_ref->fs->sb;
block_size = ext4_sb_get_block_size(sb);
root = (struct ext4_dir_idx_root *)root_block->data;
entries = (struct ext4_dir_idx_entry *)&root->en;
limit = ext4_dir_dx_climit_get_limit((void *)entries);
ind_level = ext4_dir_dx_rinfo_get_indirect_levels(&root->info);
/* Walk through the index tree */
while (true) {
uint16_t cnt = ext4_dir_dx_climit_get_count((void *)entries);
if ((cnt == 0) || (cnt > limit))
return EXT4_ERR_BAD_DX_DIR;
/* Do binary search in every node */
p = entries + 1;
q = entries + cnt - 1;
while (p <= q) {
m = p + (q - p) / 2;
if (ext4_dir_dx_entry_get_hash(m) > hinfo->hash)
q = m - 1;
else
p = m + 1;
}
at = p - 1;
/* Write results */
memcpy(&tmp_dx_blk->b, tmp_blk, sizeof(struct ext4_block));
tmp_dx_blk->entries = entries;
tmp_dx_blk->position = at;
/* Is algorithm in the leaf? */
if (ind_level == 0) {
*dx_block = tmp_dx_blk;
return EOK;
}
/* Goto child node */
uint32_t n_blk = ext4_dir_dx_entry_get_block(at);
ind_level--;
r = ext4_fs_get_inode_dblk_idx(inode_ref, n_blk, &fblk, false);
if (r != EOK)
return r;
r = ext4_trans_block_get(inode_ref->fs->bdev, tmp_blk, fblk);
if (r != EOK)
return r;
entries = ((struct ext4_dir_idx_node *)tmp_blk->data)->entries;
limit = ext4_dir_dx_climit_get_limit((void *)entries);
entry_space = block_size - sizeof(struct ext4_fake_dir_entry);
if (ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM))
entry_space -= sizeof(struct ext4_dir_idx_tail);
entry_space = entry_space / sizeof(struct ext4_dir_idx_entry);
if (limit != entry_space) {
ext4_block_set(inode_ref->fs->bdev, tmp_blk);
return EXT4_ERR_BAD_DX_DIR;
}
if (!ext4_dir_dx_csum_verify(inode_ref, (void *)tmp_blk->data)) {
ext4_dbg(DEBUG_DIR_IDX,
DBG_WARN "HTree checksum failed."
"Inode: %" PRIu32", "
"Block: %" PRIu32"\n",
inode_ref->index,
n_blk);
}
++tmp_dx_blk;
}
/* Unreachable */
return EOK;
}
/**@brief Check if the the next block would be checked during entry search.
* @param inode_ref Directory i-node
* @param hash Hash value to check
* @param dx_block Current block
* @param dx_blocks Array with path from root to leaf node
* @return Standard Error code
*/
static int ext4_dir_dx_next_block(struct ext4_inode_ref *inode_ref,
uint32_t hash,
struct ext4_dir_idx_block *dx_block,
struct ext4_dir_idx_block *dx_blocks)
{
int r;
uint32_t num_handles = 0;
ext4_fsblk_t blk_adr;
struct ext4_dir_idx_block *p = dx_block;
/* Try to find data block with next bunch of entries */
while (true) {
uint16_t cnt = ext4_dir_dx_climit_get_count((void *)p->entries);
p->position++;
if (p->position < p->entries + cnt)
break;
if (p == dx_blocks)
return EOK;
num_handles++;
p--;
}
/* Check hash collision (if not occurred - no next block cannot be
* used)*/
uint32_t current_hash = ext4_dir_dx_entry_get_hash(p->position);
if ((hash & 1) == 0) {
if ((current_hash & ~1) != hash)
return 0;
}
/* Fill new path */
while (num_handles--) {
uint32_t blk = ext4_dir_dx_entry_get_block(p->position);
r = ext4_fs_get_inode_dblk_idx(inode_ref, blk, &blk_adr, false);
if (r != EOK)
return r;
struct ext4_block b;
r = ext4_trans_block_get(inode_ref->fs->bdev, &b, blk_adr);
if (r != EOK)
return r;
if (!ext4_dir_dx_csum_verify(inode_ref, (void *)b.data)) {
ext4_dbg(DEBUG_DIR_IDX,
DBG_WARN "HTree checksum failed."
"Inode: %" PRIu32", "
"Block: %" PRIu32"\n",
inode_ref->index,
blk);
}
p++;
/* Don't forget to put old block (prevent memory leak) */
r = ext4_block_set(inode_ref->fs->bdev, &p->b);
if (r != EOK)
return r;
memcpy(&p->b, &b, sizeof(b));
p->entries = ((struct ext4_dir_idx_node *)b.data)->entries;
p->position = p->entries;
}
return ENOENT;
}
int ext4_dir_dx_find_entry(struct ext4_dir_search_result *result,
struct ext4_inode_ref *inode_ref, size_t name_len,
const char *name)
{
/* Load direct block 0 (index root) */
ext4_fsblk_t root_block_addr;
int rc2;
int rc;
rc = ext4_fs_get_inode_dblk_idx(inode_ref, 0, &root_block_addr, false);
if (rc != EOK)
return rc;
struct ext4_fs *fs = inode_ref->fs;
struct ext4_block root_block;
rc = ext4_trans_block_get(fs->bdev, &root_block, root_block_addr);
if (rc != EOK)
return rc;
if (!ext4_dir_dx_csum_verify(inode_ref, (void *)root_block.data)) {
ext4_dbg(DEBUG_DIR_IDX,
DBG_WARN "HTree root checksum failed."
"Inode: %" PRIu32", "
"Block: %" PRIu32"\n",
inode_ref->index,
(uint32_t)0);
}
/* Initialize hash info (compute hash value) */
struct ext4_hash_info hinfo;
rc = ext4_dir_hinfo_init(&hinfo, &root_block, &fs->sb, name_len, name);
if (rc != EOK) {
ext4_block_set(fs->bdev, &root_block);
return EXT4_ERR_BAD_DX_DIR;
}
/*
* Hardcoded number 2 means maximum height of index tree,
* specified in the Linux driver.
*/
struct ext4_dir_idx_block dx_blocks[2];
struct ext4_dir_idx_block *dx_block;
struct ext4_dir_idx_block *tmp;
rc = ext4_dir_dx_get_leaf(&hinfo, inode_ref, &root_block, &dx_block,
dx_blocks);
if (rc != EOK) {
ext4_block_set(fs->bdev, &root_block);
return EXT4_ERR_BAD_DX_DIR;
}
do {
/* Load leaf block */
uint32_t leaf_blk_idx;
ext4_fsblk_t leaf_block_addr;
struct ext4_block b;
leaf_blk_idx = ext4_dir_dx_entry_get_block(dx_block->position);
rc = ext4_fs_get_inode_dblk_idx(inode_ref, leaf_blk_idx,
&leaf_block_addr, false);
if (rc != EOK)
goto cleanup;
rc = ext4_trans_block_get(fs->bdev, &b, leaf_block_addr);
if (rc != EOK)
goto cleanup;
if (!ext4_dir_csum_verify(inode_ref, (void *)b.data)) {
ext4_dbg(DEBUG_DIR_IDX,
DBG_WARN "HTree leaf block checksum failed."
"Inode: %" PRIu32", "
"Block: %" PRIu32"\n",
inode_ref->index,
leaf_blk_idx);
}
/* Linear search inside block */
struct ext4_dir_en *de;
rc = ext4_dir_find_in_block(&b, &fs->sb, name_len, name, &de);
/* Found => return it */
if (rc == EOK) {
result->block = b;
result->dentry = de;
goto cleanup;
}
/* Not found, leave untouched */
rc2 = ext4_block_set(fs->bdev, &b);
if (rc2 != EOK)
goto cleanup;
if (rc != ENOENT)
goto cleanup;
/* check if the next block could be checked */
rc = ext4_dir_dx_next_block(inode_ref, hinfo.hash, dx_block,
&dx_blocks[0]);
if (rc < 0)
goto cleanup;
} while (rc == ENOENT);
/* Entry not found */
rc = ENOENT;
cleanup:
/* The whole path must be released (preventing memory leak) */
tmp = dx_blocks;
while (tmp <= dx_block) {
rc2 = ext4_block_set(fs->bdev, &tmp->b);
if (rc == EOK && rc2 != EOK)
rc = rc2;
++tmp;
}
return rc;
}
/**@brief Compare function used to pass in quicksort implementation.
* It can compare two entries by hash value.
* @param arg1 First entry
* @param arg2 Second entry
* @param dummy Unused parameter, can be NULL
*
* @return Classic compare result
* (0: equal, -1: arg1 < arg2, 1: arg1 > arg2)
*/
static int ext4_dir_dx_entry_comparator(const void *arg1, const void *arg2)
{
struct ext4_dx_sort_entry *entry1 = (void *)arg1;
struct ext4_dx_sort_entry *entry2 = (void *)arg2;
if (entry1->hash == entry2->hash)
return 0;
if (entry1->hash < entry2->hash)
return -1;
else
return 1;
}
/**@brief Insert new index entry to block.
* Note that space for new entry must be checked by caller.
* @param inode_ref Directory i-node
* @param index_block Block where to insert new entry
* @param hash Hash value covered by child node
* @param iblock Logical number of child block
*
*/
static void
ext4_dir_dx_insert_entry(struct ext4_inode_ref *inode_ref __unused,
struct ext4_dir_idx_block *index_block,
uint32_t hash, uint32_t iblock)
{
struct ext4_dir_idx_entry *old_index_entry = index_block->position;
struct ext4_dir_idx_entry *new_index_entry = old_index_entry + 1;
struct ext4_dir_idx_climit *climit = (void *)index_block->entries;
struct ext4_dir_idx_entry *start_index = index_block->entries;
uint32_t count = ext4_dir_dx_climit_get_count(climit);
size_t bytes;
bytes = (uint8_t *)(start_index + count) - (uint8_t *)(new_index_entry);
memmove(new_index_entry + 1, new_index_entry, bytes);
ext4_dir_dx_entry_set_block(new_index_entry, iblock);
ext4_dir_dx_entry_set_hash(new_index_entry, hash);
ext4_dir_dx_climit_set_count(climit, count + 1);
ext4_dir_set_dx_csum(inode_ref, (void *)index_block->b.data);
ext4_trans_set_block_dirty(index_block->b.buf);
}
/**@brief Split directory entries to two parts preventing node overflow.
* @param inode_ref Directory i-node
* @param hinfo Hash info
* @param old_data_block Block with data to be split
* @param index_block Block where index entries are located
* @param new_data_block Output value for newly allocated data block
*/
static int ext4_dir_dx_split_data(struct ext4_inode_ref *inode_ref,
struct ext4_hash_info *hinfo,
struct ext4_block *old_data_block,
struct ext4_dir_idx_block *index_block,
struct ext4_block *new_data_block)
{
int rc = EOK;
struct ext4_sblock *sb = &inode_ref->fs->sb;
uint32_t block_size = ext4_sb_get_block_size(&inode_ref->fs->sb);
/* Allocate buffer for directory entries */
uint8_t *entry_buffer = ext4_malloc(block_size);
if (entry_buffer == NULL)
return ENOMEM;
/* dot entry has the smallest size available */
uint32_t max_ecnt = block_size / sizeof(struct ext4_dir_idx_dot_en);
/* Allocate sort entry */
struct ext4_dx_sort_entry *sort;
sort = ext4_malloc(max_ecnt * sizeof(struct ext4_dx_sort_entry));
if (sort == NULL) {
ext4_free(entry_buffer);
return ENOMEM;
}
uint32_t idx = 0;
uint32_t real_size = 0;
/* Initialize hinfo */
struct ext4_hash_info hinfo_tmp;
memcpy(&hinfo_tmp, hinfo, sizeof(struct ext4_hash_info));
/* Load all valid entries to the buffer */
struct ext4_dir_en *de = (void *)old_data_block->data;
uint8_t *entry_buffer_ptr = entry_buffer;
while ((void *)de < (void *)(old_data_block->data + block_size)) {
/* Read only valid entries */
if (ext4_dir_en_get_inode(de) && de->name_len) {
uint16_t len = ext4_dir_en_get_name_len(sb, de);
rc = ext4_dir_dx_hash_string(&hinfo_tmp, len,
(char *)de->name);
if (rc != EOK) {
ext4_free(sort);
ext4_free(entry_buffer);
return rc;
}
uint32_t rec_len = 8 + len;
if ((rec_len % 4) != 0)
rec_len += 4 - (rec_len % 4);
memcpy(entry_buffer_ptr, de, rec_len);
sort[idx].dentry = entry_buffer_ptr;
sort[idx].rec_len = rec_len;
sort[idx].hash = hinfo_tmp.hash;
entry_buffer_ptr += rec_len;
real_size += rec_len;
idx++;
}
size_t elen = ext4_dir_en_get_entry_len(de);
de = (void *)((uint8_t *)de + elen);
}
qsort(sort, idx, sizeof(struct ext4_dx_sort_entry),
ext4_dir_dx_entry_comparator);
/* Allocate new block for store the second part of entries */
ext4_fsblk_t new_fblock;
uint32_t new_iblock;
rc = ext4_fs_append_inode_dblk(inode_ref, &new_fblock, &new_iblock);
if (rc != EOK) {
ext4_free(sort);
ext4_free(entry_buffer);
return rc;
}
/* Load new block */
struct ext4_block new_data_block_tmp;
rc = ext4_trans_block_get_noread(inode_ref->fs->bdev, &new_data_block_tmp,
new_fblock);
if (rc != EOK) {
ext4_free(sort);
ext4_free(entry_buffer);
return rc;
}
/*
* Distribute entries to two blocks (by size)
* - compute the half
*/
uint32_t new_hash = 0;
uint32_t current_size = 0;
uint32_t mid = 0;
uint32_t i;
for (i = 0; i < idx; ++i) {
if ((current_size + sort[i].rec_len) > (block_size / 2)) {
new_hash = sort[i].hash;
mid = i;
break;
}
current_size += sort[i].rec_len;