2 * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 /** @addtogroup lwext4
33 * @file ext4_dir_idx.c
34 * @brief Directory indexing procedures.
37 #include <ext4_config.h>
38 #include <ext4_types.h>
39 #include <ext4_misc.h>
40 #include <ext4_errno.h>
41 #include <ext4_debug.h>
43 #include <ext4_trans.h>
44 #include <ext4_dir_idx.h>
46 #include <ext4_blockdev.h>
48 #include <ext4_super.h>
49 #include <ext4_inode.h>
50 #include <ext4_crc32.h>
51 #include <ext4_hash.h>
56 /**@brief Get hash version used in directory index.
57 * @param root_info Pointer to root info structure of index
58 * @return Hash algorithm version
61 ext4_dir_dx_rinfo_get_hash_version(struct ext4_dir_idx_rinfo *ri)
63 return ri->hash_version;
66 /**@brief Set hash version, that will be used in directory index.
67 * @param root_info Pointer to root info structure of index
68 * @param v Hash algorithm version
71 ext4_dir_dx_rinfo_set_hash_version(struct ext4_dir_idx_rinfo *ri, uint8_t v)
76 /**@brief Get length of root_info structure in bytes.
77 * @param root_info Pointer to root info structure of index
78 * @return Length of the structure
81 ext4_dir_dx_rinfo_get_info_length(struct ext4_dir_idx_rinfo *ri)
83 return ri->info_length;
86 /**@brief Set length of root_info structure in bytes.
87 * @param root_info Pointer to root info structure of index
88 * @param info_length Length of the structure
91 ext4_dir_dx_root_info_set_info_length(struct ext4_dir_idx_rinfo *ri,
94 ri->info_length = len;
97 /**@brief Get number of indirect levels of HTree.
98 * @param root_info Pointer to root info structure of index
99 * @return Height of HTree (actually only 0 or 1)
101 static inline uint8_t
102 ext4_dir_dx_rinfo_get_indirect_levels(struct ext4_dir_idx_rinfo *ri)
104 return ri->indirect_levels;
107 /**@brief Set number of indirect levels of HTree.
108 * @param root_info Pointer to root info structure of index
109 * @param lvl Height of HTree (actually only 0 or 1)
112 ext4_dir_dx_rinfo_set_indirect_levels(struct ext4_dir_idx_rinfo *ri, uint8_t l)
114 ri->indirect_levels = l;
117 /**@brief Get maximum number of index node entries.
118 * @param climit Pointer to counlimit structure
119 * @return Maximum of entries in node
121 static inline uint16_t
122 ext4_dir_dx_climit_get_limit(struct ext4_dir_idx_climit *climit)
124 return to_le16(climit->limit);
127 /**@brief Set maximum number of index node entries.
128 * @param climit Pointer to counlimit structure
129 * @param limit Maximum of entries in node
132 ext4_dir_dx_climit_set_limit(struct ext4_dir_idx_climit *climit, uint16_t limit)
134 climit->limit = to_le16(limit);
137 /**@brief Get current number of index node entries.
138 * @param climit Pointer to counlimit structure
139 * @return Number of entries in node
141 static inline uint16_t
142 ext4_dir_dx_climit_get_count(struct ext4_dir_idx_climit *climit)
144 return to_le16(climit->count);
147 /**@brief Set current number of index node entries.
148 * @param climit Pointer to counlimit structure
149 * @param count Number of entries in node
152 ext4_dir_dx_climit_set_count(struct ext4_dir_idx_climit *climit, uint16_t count)
154 climit->count = to_le16(count);
157 /**@brief Get hash value of index entry.
158 * @param entry Pointer to index entry
161 static inline uint32_t
162 ext4_dir_dx_entry_get_hash(struct ext4_dir_idx_entry *entry)
164 return to_le32(entry->hash);
167 /**@brief Set hash value of index entry.
168 * @param entry Pointer to index entry
169 * @param hash Hash value
172 ext4_dir_dx_entry_set_hash(struct ext4_dir_idx_entry *entry, uint32_t hash)
174 entry->hash = to_le32(hash);
177 /**@brief Get block address where child node is located.
178 * @param entry Pointer to index entry
179 * @return Block address of child node
181 static inline uint32_t
182 ext4_dir_dx_entry_get_block(struct ext4_dir_idx_entry *entry)
184 return to_le32(entry->block);
187 /**@brief Set block address where child node is located.
188 * @param entry Pointer to index entry
189 * @param block Block address of child node
192 ext4_dir_dx_entry_set_block(struct ext4_dir_idx_entry *entry, uint32_t block)
194 entry->block = to_le32(block);
197 /**@brief Sort entry item.*/
198 struct ext4_dx_sort_entry {
204 static int ext4_dir_dx_hash_string(struct ext4_hash_info *hinfo, int len,
207 return ext2_htree_hash(name, len, hinfo->seed, hinfo->hash_version,
208 &hinfo->hash, &hinfo->minor_hash);
211 #if CONFIG_META_CSUM_ENABLE
212 static uint32_t ext4_dir_dx_checksum(struct ext4_inode_ref *inode_ref, void *de,
213 int count_offset, int count,
214 struct ext4_dir_idx_tail *t)
216 uint32_t orig_cum, csum = 0;
217 struct ext4_sblock *sb = &inode_ref->fs->sb;
220 /* Compute the checksum only if the filesystem supports it */
221 if (ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM)) {
222 uint32_t ino_index = to_le32(inode_ref->index);
224 ino_gen = to_le32(ext4_inode_get_generation(inode_ref->inode));
226 sz = count_offset + (count * sizeof(struct ext4_dir_idx_tail));
227 orig_cum = t->checksum;
229 /* First calculate crc32 checksum against fs uuid */
230 csum = ext4_crc32c(EXT4_CRC32_INIT, sb->uuid, sizeof(sb->uuid));
231 /* Then calculate crc32 checksum against inode number
232 * and inode generation */
233 csum = ext4_crc32c(csum, &ino_index, sizeof(ino_index));
234 csum = ext4_crc32c(csum, &ino_gen, sizeof(ino_gen));
235 /* After that calculate crc32 checksum against all the dx_entry */
236 csum = ext4_crc32c(csum, de, sz);
237 /* Finally calculate crc32 checksum for dx_tail */
238 csum = ext4_crc32c(csum, t, sizeof(struct ext4_dir_idx_tail));
239 t->checksum = orig_cum;
244 static struct ext4_dir_idx_climit *
245 ext4_dir_dx_get_climit(struct ext4_inode_ref *inode_ref,
246 struct ext4_dir_en *dirent, int *offset)
248 struct ext4_dir_en *dp;
249 struct ext4_dir_idx_root *root;
250 struct ext4_sblock *sb = &inode_ref->fs->sb;
251 uint32_t block_size = ext4_sb_get_block_size(sb);
252 uint16_t entry_len = ext4_dir_en_get_entry_len(dirent);
256 if (entry_len == 12) {
257 root = (struct ext4_dir_idx_root *)dirent;
258 dp = (struct ext4_dir_en *)&root->dots[1];
259 if (ext4_dir_en_get_entry_len(dp) != (block_size - 12))
261 if (root->info.reserved_zero)
263 if (root->info.info_length != sizeof(struct ext4_dir_idx_rinfo))
266 } else if (entry_len == block_size) {
273 *offset = count_offset;
274 return (struct ext4_dir_idx_climit *)(((char *)dirent) + count_offset);
279 * Currently we do not verify the checksum of HTree node.
281 static bool ext4_dir_dx_csum_verify(struct ext4_inode_ref *inode_ref,
282 struct ext4_dir_en *de)
284 struct ext4_sblock *sb = &inode_ref->fs->sb;
285 uint32_t block_size = ext4_sb_get_block_size(sb);
286 int coff, limit, cnt;
288 if (ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM)) {
289 struct ext4_dir_idx_climit *climit;
290 climit = ext4_dir_dx_get_climit(inode_ref, de, &coff);
292 /* Directory seems corrupted. */
295 struct ext4_dir_idx_tail *t;
296 limit = ext4_dir_dx_climit_get_limit(climit);
297 cnt = ext4_dir_dx_climit_get_count(climit);
298 if (coff + (limit * sizeof(struct ext4_dir_idx_entry)) >
299 (block_size - sizeof(struct ext4_dir_idx_tail))) {
300 /* There is no space to hold the checksum */
303 t = (void *)(((struct ext4_dir_idx_entry *)climit) + limit);
306 c = to_le32(ext4_dir_dx_checksum(inode_ref, de, coff, cnt, t));
307 if (t->checksum != c)
314 static void ext4_dir_set_dx_csum(struct ext4_inode_ref *inode_ref,
315 struct ext4_dir_en *dirent)
317 int coff, limit, count;
318 struct ext4_sblock *sb = &inode_ref->fs->sb;
319 uint32_t block_size = ext4_sb_get_block_size(sb);
321 if (ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM)) {
322 struct ext4_dir_idx_climit *climit;
323 climit = ext4_dir_dx_get_climit(inode_ref, dirent, &coff);
325 /* Directory seems corrupted. */
328 struct ext4_dir_idx_tail *t;
329 limit = ext4_dir_dx_climit_get_limit(climit);
330 count = ext4_dir_dx_climit_get_count(climit);
331 if (coff + (limit * sizeof(struct ext4_dir_idx_entry)) >
332 (block_size - sizeof(struct ext4_dir_idx_tail))) {
333 /* There is no space to hold the checksum */
337 t = (void *)(((struct ext4_dir_idx_entry *)climit) + limit);
338 t->checksum = to_le32(ext4_dir_dx_checksum(inode_ref, dirent,
343 #define ext4_dir_dx_csum_verify(...) true
344 #define ext4_dir_set_dx_csum(...)
347 /****************************************************************************/
349 int ext4_dir_dx_init(struct ext4_inode_ref *dir, struct ext4_inode_ref *parent)
351 /* Load block 0, where will be index root located */
355 (ext4_inode_get_size(&dir->fs->sb, dir->inode)
356 < EXT4_DIR_DX_INIT_BCNT)
358 struct ext4_sblock *sb = &dir->fs->sb;
359 uint32_t block_size = ext4_sb_get_block_size(&dir->fs->sb);
360 struct ext4_block block;
365 rc = ext4_fs_init_inode_dblk_idx(dir, iblock, &fblock);
367 rc = ext4_fs_append_inode_dblk(dir, &fblock, &iblock);
372 rc = ext4_trans_block_get_noread(dir->fs->bdev, &block, fblock);
376 /* Initialize pointers to data structures */
377 struct ext4_dir_idx_root *root = (void *)block.data;
378 struct ext4_dir_idx_rinfo *info = &(root->info);
380 memset(root, 0, sizeof(struct ext4_dir_idx_root));
381 struct ext4_dir_en *de;
383 /* Initialize dot entries */
384 de = (struct ext4_dir_en *)root->dots;
385 ext4_dir_write_entry(sb, de, 12, dir, ".", strlen("."));
387 de = (struct ext4_dir_en *)(root->dots + 1);
388 uint16_t elen = block_size - 12;
389 ext4_dir_write_entry(sb, de, elen, parent, "..", strlen(".."));
391 /* Initialize root info structure */
392 uint8_t hash_version = ext4_get8(&dir->fs->sb, default_hash_version);
394 ext4_dir_dx_rinfo_set_hash_version(info, hash_version);
395 ext4_dir_dx_rinfo_set_indirect_levels(info, 0);
396 ext4_dir_dx_root_info_set_info_length(info, 8);
398 /* Set limit and current number of entries */
399 struct ext4_dir_idx_climit *climit;
400 climit = (struct ext4_dir_idx_climit *)&root->en;
402 ext4_dir_dx_climit_set_count(climit, 1);
404 uint32_t entry_space;
405 entry_space = block_size - 2 * sizeof(struct ext4_dir_idx_dot_en) -
406 sizeof(struct ext4_dir_idx_rinfo);
408 if (ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM))
409 entry_space -= sizeof(struct ext4_dir_idx_tail);
411 uint16_t root_limit = entry_space / sizeof(struct ext4_dir_idx_entry);
412 ext4_dir_dx_climit_set_limit(climit, root_limit);
414 /* Append new block, where will be new entries inserted in the future */
417 rc = ext4_fs_init_inode_dblk_idx(dir, iblock, &fblock);
419 rc = ext4_fs_append_inode_dblk(dir, &fblock, &iblock);
422 ext4_block_set(dir->fs->bdev, &block);
426 struct ext4_block new_block;
427 rc = ext4_trans_block_get_noread(dir->fs->bdev, &new_block, fblock);
429 ext4_block_set(dir->fs->bdev, &block);
433 /* Fill the whole block with empty entry */
434 struct ext4_dir_en *be = (void *)new_block.data;
436 if (ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM)) {
437 uint16_t len = block_size - sizeof(struct ext4_dir_entry_tail);
438 ext4_dir_en_set_entry_len(be, len);
439 ext4_dir_en_set_name_len(sb, be, 0);
440 ext4_dir_en_set_inode_type(sb, be, EXT4_DE_UNKNOWN);
441 ext4_dir_init_entry_tail(EXT4_DIRENT_TAIL(be, block_size));
442 ext4_dir_set_csum(dir, be);
444 ext4_dir_en_set_entry_len(be, block_size);
447 ext4_dir_en_set_inode(be, 0);
449 ext4_trans_set_block_dirty(new_block.buf);
450 rc = ext4_block_set(dir->fs->bdev, &new_block);
452 ext4_block_set(dir->fs->bdev, &block);
456 /* Connect new block to the only entry in index */
457 struct ext4_dir_idx_entry *entry = root->en;
458 ext4_dir_dx_entry_set_block(entry, iblock);
460 ext4_dir_set_dx_csum(dir, (struct ext4_dir_en *)block.data);
461 ext4_trans_set_block_dirty(block.buf);
463 return ext4_block_set(dir->fs->bdev, &block);
466 /**@brief Initialize hash info structure necessary for index operations.
467 * @param hinfo Pointer to hinfo to be initialized
468 * @param root_block Root block (number 0) of index
469 * @param sb Pointer to superblock
470 * @param name_len Length of name to be computed hash value from
471 * @param name Name to be computed hash value from
472 * @return Standard error code
474 static int ext4_dir_hinfo_init(struct ext4_hash_info *hinfo,
475 struct ext4_block *root_block,
476 struct ext4_sblock *sb, size_t name_len,
479 struct ext4_dir_idx_root *root;
481 root = (struct ext4_dir_idx_root *)root_block->data;
482 if ((root->info.hash_version != EXT2_HTREE_LEGACY) &&
483 (root->info.hash_version != EXT2_HTREE_HALF_MD4) &&
484 (root->info.hash_version != EXT2_HTREE_TEA))
485 return EXT4_ERR_BAD_DX_DIR;
487 /* Check unused flags */
488 if (root->info.unused_flags != 0)
489 return EXT4_ERR_BAD_DX_DIR;
491 /* Check indirect levels */
492 if (root->info.indirect_levels > 1)
493 return EXT4_ERR_BAD_DX_DIR;
495 /* Check if node limit is correct */
496 uint32_t block_size = ext4_sb_get_block_size(sb);
497 uint32_t entry_space = block_size;
498 entry_space -= 2 * sizeof(struct ext4_dir_idx_dot_en);
499 entry_space -= sizeof(struct ext4_dir_idx_rinfo);
500 if (ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM))
501 entry_space -= sizeof(struct ext4_dir_idx_tail);
502 entry_space = entry_space / sizeof(struct ext4_dir_idx_entry);
504 struct ext4_dir_idx_climit *climit = (void *)&root->en;
505 uint16_t limit = ext4_dir_dx_climit_get_limit(climit);
506 if (limit != entry_space)
507 return EXT4_ERR_BAD_DX_DIR;
509 /* Check hash version and modify if necessary */
510 hinfo->hash_version = ext4_dir_dx_rinfo_get_hash_version(&root->info);
511 if ((hinfo->hash_version <= EXT2_HTREE_TEA) &&
512 (ext4_sb_check_flag(sb, EXT4_SUPERBLOCK_FLAGS_UNSIGNED_HASH))) {
513 /* Use unsigned hash */
514 hinfo->hash_version += 3;
517 /* Load hash seed from superblock */
518 hinfo->seed = ext4_get8(sb, hash_seed);
520 /* Compute hash value of name */
522 return ext4_dir_dx_hash_string(hinfo, name_len, name);
527 /**@brief Walk through index tree and load leaf with corresponding hash value.
528 * @param hinfo Initialized hash info structure
529 * @param inode_ref Current i-node
530 * @param root_block Root block (iblock 0), where is root node located
531 * @param dx_block Pointer to leaf node in dx_blocks array
532 * @param dx_blocks Array with the whole path from root to leaf
533 * @return Standard error code
535 static int ext4_dir_dx_get_leaf(struct ext4_hash_info *hinfo,
536 struct ext4_inode_ref *inode_ref,
537 struct ext4_block *root_block,
538 struct ext4_dir_idx_block **dx_block,
539 struct ext4_dir_idx_block *dx_blocks)
541 struct ext4_dir_idx_root *root;
542 struct ext4_dir_idx_entry *entries;
543 struct ext4_dir_idx_entry *p;
544 struct ext4_dir_idx_entry *q;
545 struct ext4_dir_idx_entry *m;
546 struct ext4_dir_idx_entry *at;
550 uint16_t entry_space;
554 struct ext4_dir_idx_block *tmp_dx_blk = dx_blocks;
555 struct ext4_block *tmp_blk = root_block;
556 struct ext4_sblock *sb = &inode_ref->fs->sb;
558 block_size = ext4_sb_get_block_size(sb);
559 root = (struct ext4_dir_idx_root *)root_block->data;
560 entries = (struct ext4_dir_idx_entry *)&root->en;
561 limit = ext4_dir_dx_climit_get_limit((void *)entries);
562 ind_level = ext4_dir_dx_rinfo_get_indirect_levels(&root->info);
564 /* Walk through the index tree */
566 uint16_t cnt = ext4_dir_dx_climit_get_count((void *)entries);
567 if ((cnt == 0) || (cnt > limit))
568 return EXT4_ERR_BAD_DX_DIR;
570 /* Do binary search in every node */
572 q = entries + cnt - 1;
576 if (ext4_dir_dx_entry_get_hash(m) > hinfo->hash)
585 memcpy(&tmp_dx_blk->b, tmp_blk, sizeof(struct ext4_block));
586 tmp_dx_blk->entries = entries;
587 tmp_dx_blk->position = at;
589 /* Is algorithm in the leaf? */
590 if (ind_level == 0) {
591 *dx_block = tmp_dx_blk;
595 /* Goto child node */
596 uint32_t n_blk = ext4_dir_dx_entry_get_block(at);
600 r = ext4_fs_get_inode_dblk_idx(inode_ref, n_blk, &fblk, false);
604 r = ext4_trans_block_get(inode_ref->fs->bdev, tmp_blk, fblk);
608 entries = ((struct ext4_dir_idx_node *)tmp_blk->data)->entries;
609 limit = ext4_dir_dx_climit_get_limit((void *)entries);
611 entry_space = block_size - sizeof(struct ext4_fake_dir_entry);
612 if (ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM))
613 entry_space -= sizeof(struct ext4_dir_idx_tail);
615 entry_space = entry_space / sizeof(struct ext4_dir_idx_entry);
617 if (limit != entry_space) {
618 ext4_block_set(inode_ref->fs->bdev, tmp_blk);
619 return EXT4_ERR_BAD_DX_DIR;
622 if (!ext4_dir_dx_csum_verify(inode_ref, (void *)tmp_blk->data)) {
623 ext4_dbg(DEBUG_DIR_IDX,
624 DBG_WARN "HTree checksum failed."
625 "Inode: %" PRIu32", "
626 "Block: %" PRIu32"\n",
638 /**@brief Check if the the next block would be checked during entry search.
639 * @param inode_ref Directory i-node
640 * @param hash Hash value to check
641 * @param dx_block Current block
642 * @param dx_blocks Array with path from root to leaf node
643 * @return Standard Error code
645 static int ext4_dir_dx_next_block(struct ext4_inode_ref *inode_ref,
647 struct ext4_dir_idx_block *dx_block,
648 struct ext4_dir_idx_block *dx_blocks)
651 uint32_t num_handles = 0;
652 ext4_fsblk_t blk_adr;
653 struct ext4_dir_idx_block *p = dx_block;
655 /* Try to find data block with next bunch of entries */
657 uint16_t cnt = ext4_dir_dx_climit_get_count((void *)p->entries);
660 if (p->position < p->entries + cnt)
670 /* Check hash collision (if not occurred - no next block cannot be
672 uint32_t current_hash = ext4_dir_dx_entry_get_hash(p->position);
673 if ((hash & 1) == 0) {
674 if ((current_hash & ~1) != hash)
679 while (num_handles--) {
680 uint32_t blk = ext4_dir_dx_entry_get_block(p->position);
681 r = ext4_fs_get_inode_dblk_idx(inode_ref, blk, &blk_adr, false);
686 r = ext4_trans_block_get(inode_ref->fs->bdev, &b, blk_adr);
690 if (!ext4_dir_dx_csum_verify(inode_ref, (void *)b.data)) {
691 ext4_dbg(DEBUG_DIR_IDX,
692 DBG_WARN "HTree checksum failed."
693 "Inode: %" PRIu32", "
694 "Block: %" PRIu32"\n",
701 /* Don't forget to put old block (prevent memory leak) */
702 r = ext4_block_set(inode_ref->fs->bdev, &p->b);
706 memcpy(&p->b, &b, sizeof(b));
707 p->entries = ((struct ext4_dir_idx_node *)b.data)->entries;
708 p->position = p->entries;
714 int ext4_dir_dx_find_entry(struct ext4_dir_search_result *result,
715 struct ext4_inode_ref *inode_ref, size_t name_len,
718 /* Load direct block 0 (index root) */
719 ext4_fsblk_t root_block_addr;
722 rc = ext4_fs_get_inode_dblk_idx(inode_ref, 0, &root_block_addr, false);
726 struct ext4_fs *fs = inode_ref->fs;
728 struct ext4_block root_block;
729 rc = ext4_trans_block_get(fs->bdev, &root_block, root_block_addr);
733 if (!ext4_dir_dx_csum_verify(inode_ref, (void *)root_block.data)) {
734 ext4_dbg(DEBUG_DIR_IDX,
735 DBG_WARN "HTree root checksum failed."
736 "Inode: %" PRIu32", "
737 "Block: %" PRIu32"\n",
742 /* Initialize hash info (compute hash value) */
743 struct ext4_hash_info hinfo;
744 rc = ext4_dir_hinfo_init(&hinfo, &root_block, &fs->sb, name_len, name);
746 ext4_block_set(fs->bdev, &root_block);
747 return EXT4_ERR_BAD_DX_DIR;
751 * Hardcoded number 2 means maximum height of index tree,
752 * specified in the Linux driver.
754 struct ext4_dir_idx_block dx_blocks[2];
755 struct ext4_dir_idx_block *dx_block;
756 struct ext4_dir_idx_block *tmp;
758 rc = ext4_dir_dx_get_leaf(&hinfo, inode_ref, &root_block, &dx_block,
761 ext4_block_set(fs->bdev, &root_block);
762 return EXT4_ERR_BAD_DX_DIR;
766 /* Load leaf block */
767 uint32_t leaf_blk_idx;
768 ext4_fsblk_t leaf_block_addr;
771 leaf_blk_idx = ext4_dir_dx_entry_get_block(dx_block->position);
772 rc = ext4_fs_get_inode_dblk_idx(inode_ref, leaf_blk_idx,
773 &leaf_block_addr, false);
777 rc = ext4_trans_block_get(fs->bdev, &b, leaf_block_addr);
781 if (!ext4_dir_csum_verify(inode_ref, (void *)b.data)) {
782 ext4_dbg(DEBUG_DIR_IDX,
783 DBG_WARN "HTree leaf block checksum failed."
784 "Inode: %" PRIu32", "
785 "Block: %" PRIu32"\n",
790 /* Linear search inside block */
791 struct ext4_dir_en *de;
792 rc = ext4_dir_find_in_block(&b, &fs->sb, name_len, name, &de);
794 /* Found => return it */
801 /* Not found, leave untouched */
802 rc2 = ext4_block_set(fs->bdev, &b);
809 /* check if the next block could be checked */
810 rc = ext4_dir_dx_next_block(inode_ref, hinfo.hash, dx_block,
814 } while (rc == ENOENT);
816 /* Entry not found */
820 /* The whole path must be released (preventing memory leak) */
823 while (tmp <= dx_block) {
824 rc2 = ext4_block_set(fs->bdev, &tmp->b);
825 if (rc == EOK && rc2 != EOK)
833 /**@brief Compare function used to pass in quicksort implementation.
834 * It can compare two entries by hash value.
835 * @param arg1 First entry
836 * @param arg2 Second entry
837 * @param dummy Unused parameter, can be NULL
839 * @return Classic compare result
840 * (0: equal, -1: arg1 < arg2, 1: arg1 > arg2)
842 static int ext4_dir_dx_entry_comparator(const void *arg1, const void *arg2)
844 struct ext4_dx_sort_entry *entry1 = (void *)arg1;
845 struct ext4_dx_sort_entry *entry2 = (void *)arg2;
847 if (entry1->hash == entry2->hash)
850 if (entry1->hash < entry2->hash)
856 /**@brief Insert new index entry to block.
857 * Note that space for new entry must be checked by caller.
858 * @param inode_ref Directory i-node
859 * @param index_block Block where to insert new entry
860 * @param hash Hash value covered by child node
861 * @param iblock Logical number of child block
865 ext4_dir_dx_insert_entry(struct ext4_inode_ref *inode_ref __unused,
866 struct ext4_dir_idx_block *index_block,
867 uint32_t hash, uint32_t iblock)
869 struct ext4_dir_idx_entry *old_index_entry = index_block->position;
870 struct ext4_dir_idx_entry *new_index_entry = old_index_entry + 1;
871 struct ext4_dir_idx_climit *climit = (void *)index_block->entries;
872 struct ext4_dir_idx_entry *start_index = index_block->entries;
873 uint32_t count = ext4_dir_dx_climit_get_count(climit);
876 bytes = (uint8_t *)(start_index + count) - (uint8_t *)(new_index_entry);
878 memmove(new_index_entry + 1, new_index_entry, bytes);
880 ext4_dir_dx_entry_set_block(new_index_entry, iblock);
881 ext4_dir_dx_entry_set_hash(new_index_entry, hash);
882 ext4_dir_dx_climit_set_count(climit, count + 1);
883 ext4_dir_set_dx_csum(inode_ref, (void *)index_block->b.data);
884 ext4_trans_set_block_dirty(index_block->b.buf);
887 /**@brief Split directory entries to two parts preventing node overflow.
888 * @param inode_ref Directory i-node
889 * @param hinfo Hash info
890 * @param old_data_block Block with data to be split
891 * @param index_block Block where index entries are located
892 * @param new_data_block Output value for newly allocated data block
894 static int ext4_dir_dx_split_data(struct ext4_inode_ref *inode_ref,
895 struct ext4_hash_info *hinfo,
896 struct ext4_block *old_data_block,
897 struct ext4_dir_idx_block *index_block,
898 struct ext4_block *new_data_block)
901 struct ext4_sblock *sb = &inode_ref->fs->sb;
902 uint32_t block_size = ext4_sb_get_block_size(&inode_ref->fs->sb);
904 /* Allocate buffer for directory entries */
905 uint8_t *entry_buffer = ext4_malloc(block_size);
906 if (entry_buffer == NULL)
909 /* dot entry has the smallest size available */
910 uint32_t max_ecnt = block_size / sizeof(struct ext4_dir_idx_dot_en);
912 /* Allocate sort entry */
913 struct ext4_dx_sort_entry *sort;
915 sort = ext4_malloc(max_ecnt * sizeof(struct ext4_dx_sort_entry));
917 ext4_free(entry_buffer);
922 uint32_t real_size = 0;
924 /* Initialize hinfo */
925 struct ext4_hash_info hinfo_tmp;
926 memcpy(&hinfo_tmp, hinfo, sizeof(struct ext4_hash_info));
928 /* Load all valid entries to the buffer */
929 struct ext4_dir_en *de = (void *)old_data_block->data;
930 uint8_t *entry_buffer_ptr = entry_buffer;
931 while ((void *)de < (void *)(old_data_block->data + block_size)) {
932 /* Read only valid entries */
933 if (ext4_dir_en_get_inode(de) && de->name_len) {
934 uint16_t len = ext4_dir_en_get_name_len(sb, de);
935 rc = ext4_dir_dx_hash_string(&hinfo_tmp, len,
939 ext4_free(entry_buffer);
943 uint32_t rec_len = 8 + len;
944 if ((rec_len % 4) != 0)
945 rec_len += 4 - (rec_len % 4);
947 memcpy(entry_buffer_ptr, de, rec_len);
949 sort[idx].dentry = entry_buffer_ptr;
950 sort[idx].rec_len = rec_len;
951 sort[idx].hash = hinfo_tmp.hash;
953 entry_buffer_ptr += rec_len;
954 real_size += rec_len;
958 size_t elen = ext4_dir_en_get_entry_len(de);
959 de = (void *)((uint8_t *)de + elen);
962 qsort(sort, idx, sizeof(struct ext4_dx_sort_entry),
963 ext4_dir_dx_entry_comparator);
965 /* Allocate new block for store the second part of entries */
966 ext4_fsblk_t new_fblock;
968 rc = ext4_fs_append_inode_dblk(inode_ref, &new_fblock, &new_iblock);
971 ext4_free(entry_buffer);
976 struct ext4_block new_data_block_tmp;
977 rc = ext4_trans_block_get_noread(inode_ref->fs->bdev, &new_data_block_tmp,
981 ext4_free(entry_buffer);
986 * Distribute entries to two blocks (by size)
989 uint32_t new_hash = 0;
990 uint32_t current_size = 0;
993 for (i = 0; i < idx; ++i) {
994 if ((current_size + sort[i].rec_len) > (block_size / 2)) {
995 new_hash = sort[i].hash;
1000 current_size += sort[i].rec_len;
1003 /* Check hash collision */
1004 uint32_t continued = 0;
1005 if (new_hash == sort[mid - 1].hash)
1010 if (ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM))
1011 block_size -= sizeof(struct ext4_dir_entry_tail);
1013 /* First part - to the old block */
1014 for (i = 0; i < mid; ++i) {
1015 ptr = old_data_block->data + off;
1016 memcpy(ptr, sort[i].dentry, sort[i].rec_len);
1018 struct ext4_dir_en *t = ptr;
1020 ext4_dir_en_set_entry_len(t, sort[i].rec_len);
1022 ext4_dir_en_set_entry_len(t, block_size - off);
1024 off += sort[i].rec_len;
1027 /* Second part - to the new block */
1029 for (i = mid; i < idx; ++i) {
1030 ptr = new_data_block_tmp.data + off;
1031 memcpy(ptr, sort[i].dentry, sort[i].rec_len);
1033 struct ext4_dir_en *t = ptr;
1035 ext4_dir_en_set_entry_len(t, sort[i].rec_len);
1037 ext4_dir_en_set_entry_len(t, block_size - off);
1039 off += sort[i].rec_len;
1042 block_size = ext4_sb_get_block_size(&inode_ref->fs->sb);
1044 /* Do some steps to finish operation */
1045 sb = &inode_ref->fs->sb;
1046 if (ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM)) {
1047 struct ext4_dir_entry_tail *t;
1049 t = EXT4_DIRENT_TAIL(old_data_block->data, block_size);
1050 ext4_dir_init_entry_tail(t);
1051 t = EXT4_DIRENT_TAIL(new_data_block_tmp.data, block_size);
1052 ext4_dir_init_entry_tail(t);
1054 ext4_dir_set_csum(inode_ref, (void *)old_data_block->data);
1055 ext4_dir_set_csum(inode_ref, (void *)new_data_block_tmp.data);
1056 ext4_trans_set_block_dirty(old_data_block->buf);
1057 ext4_trans_set_block_dirty(new_data_block_tmp.buf);
1060 ext4_free(entry_buffer);
1062 ext4_dir_dx_insert_entry(inode_ref, index_block, new_hash + continued,
1065 *new_data_block = new_data_block_tmp;
1069 /**@brief Split index node and maybe some parent nodes in the tree hierarchy.
1070 * @param inode_ref Directory i-node
1071 * @param dx_blocks Array with path from root to leaf node
1072 * @param dx_block Leaf block to be split if needed
1073 * @return Error code
1076 ext4_dir_dx_split_index(struct ext4_inode_ref *ino_ref,
1077 struct ext4_dir_idx_block *dx_blks,
1078 struct ext4_dir_idx_block *dxb,
1079 struct ext4_dir_idx_block **new_dx_block)
1081 struct ext4_sblock *sb = &ino_ref->fs->sb;
1082 struct ext4_dir_idx_entry *e;
1085 uint32_t block_size = ext4_sb_get_block_size(&ino_ref->fs->sb);
1086 uint32_t entry_space = block_size - sizeof(struct ext4_fake_dir_entry);
1087 uint32_t node_limit = entry_space / sizeof(struct ext4_dir_idx_entry);
1089 bool meta_csum = ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM);
1092 e = ((struct ext4_dir_idx_root *)dxb->b.data)->en;
1094 e = ((struct ext4_dir_idx_node *)dxb->b.data)->entries;
1096 struct ext4_dir_idx_climit *climit = (struct ext4_dir_idx_climit *)e;
1098 uint16_t leaf_limit = ext4_dir_dx_climit_get_limit(climit);
1099 uint16_t leaf_count = ext4_dir_dx_climit_get_count(climit);
1101 /* Check if is necessary to split index block */
1102 if (leaf_limit == leaf_count) {
1103 struct ext4_dir_idx_entry *ren;
1104 ptrdiff_t levels = dxb - dx_blks;
1106 ren = ((struct ext4_dir_idx_root *)dx_blks[0].b.data)->en;
1107 struct ext4_dir_idx_climit *rclimit = (void *)ren;
1108 uint16_t root_limit = ext4_dir_dx_climit_get_limit(rclimit);
1109 uint16_t root_count = ext4_dir_dx_climit_get_count(rclimit);
1112 /* Linux limitation */
1113 if ((levels > 0) && (root_limit == root_count))
1116 /* Add new block to directory */
1117 ext4_fsblk_t new_fblk;
1119 r = ext4_fs_append_inode_dblk(ino_ref, &new_fblk, &new_iblk);
1123 /* load new block */
1124 struct ext4_block b;
1125 r = ext4_trans_block_get_noread(ino_ref->fs->bdev, &b, new_fblk);
1129 struct ext4_dir_idx_node *new_node = (void *)b.data;
1130 struct ext4_dir_idx_entry *new_en = new_node->entries;
1132 memset(&new_node->fake, 0, sizeof(struct ext4_fake_dir_entry));
1133 new_node->fake.entry_length = block_size;
1135 /* Split leaf node */
1137 uint32_t count_left = leaf_count / 2;
1138 uint32_t count_right = leaf_count - count_left;
1139 uint32_t hash_right;
1142 struct ext4_dir_idx_climit *left_climit;
1143 struct ext4_dir_idx_climit *right_climit;
1145 hash_right = ext4_dir_dx_entry_get_hash(e + count_left);
1146 /* Copy data to new node */
1147 sz = count_right * sizeof(struct ext4_dir_idx_entry);
1148 memcpy(new_en, e + count_left, sz);
1150 /* Initialize new node */
1151 left_climit = (struct ext4_dir_idx_climit *)e;
1152 right_climit = (struct ext4_dir_idx_climit *)new_en;
1154 ext4_dir_dx_climit_set_count(left_climit, count_left);
1155 ext4_dir_dx_climit_set_count(right_climit, count_right);
1158 entry_space -= sizeof(struct ext4_dir_idx_tail);
1160 ext4_dir_dx_climit_set_limit(right_climit, node_limit);
1162 /* Which index block is target for new entry */
1163 uint32_t position_index =
1164 (dxb->position - dxb->entries);
1165 if (position_index >= count_left) {
1166 ext4_dir_set_dx_csum(
1168 (struct ext4_dir_en *)
1170 ext4_trans_set_block_dirty(dxb->b.buf);
1172 struct ext4_block block_tmp = dxb->b;
1177 new_en + position_index - count_left;
1178 dxb->entries = new_en;
1183 /* Finally insert new entry */
1184 ext4_dir_dx_insert_entry(ino_ref, dx_blks, hash_right,
1186 ext4_dir_set_dx_csum(ino_ref, (void*)dx_blks[0].b.data);
1187 ext4_dir_set_dx_csum(ino_ref, (void*)dx_blks[1].b.data);
1188 ext4_trans_set_block_dirty(dx_blks[0].b.buf);
1189 ext4_trans_set_block_dirty(dx_blks[1].b.buf);
1191 ext4_dir_set_dx_csum(ino_ref, (void *)b.data);
1192 ext4_trans_set_block_dirty(b.buf);
1193 return ext4_block_set(ino_ref->fs->bdev, &b);
1196 /* Copy data from root to child block */
1197 sz = leaf_count * sizeof(struct ext4_dir_idx_entry);
1198 memcpy(new_en, e, sz);
1200 struct ext4_dir_idx_climit *new_climit = (void*)new_en;
1202 entry_space -= sizeof(struct ext4_dir_idx_tail);
1204 ext4_dir_dx_climit_set_limit(new_climit, node_limit);
1206 /* Set values in root node */
1207 struct ext4_dir_idx_climit *new_root_climit = (void *)e;
1209 ext4_dir_dx_climit_set_count(new_root_climit, 1);
1210 ext4_dir_dx_entry_set_block(e, new_iblk);
1212 struct ext4_dir_idx_root *r = (void *)dx_blks[0].b.data;
1213 r->info.indirect_levels = 1;
1215 /* Add new entry to the path */
1217 dxb->position = dx_blks->position - e + new_en;
1218 dxb->entries = new_en;
1220 *new_dx_block = dxb;
1222 ext4_dir_set_dx_csum(ino_ref, (void*)dx_blks[0].b.data);
1223 ext4_dir_set_dx_csum(ino_ref, (void*)dx_blks[1].b.data);
1224 ext4_trans_set_block_dirty(dx_blks[0].b.buf);
1225 ext4_trans_set_block_dirty(dx_blks[1].b.buf);
1232 int ext4_dir_dx_add_entry(struct ext4_inode_ref *parent,
1233 struct ext4_inode_ref *child, const char *name, uint32_t name_len)
1237 /* Get direct block 0 (index root) */
1238 ext4_fsblk_t rblock_addr;
1239 r = ext4_fs_get_inode_dblk_idx(parent, 0, &rblock_addr, false);
1243 struct ext4_fs *fs = parent->fs;
1244 struct ext4_block root_blk;
1246 r = ext4_trans_block_get(fs->bdev, &root_blk, rblock_addr);
1250 if (!ext4_dir_dx_csum_verify(parent, (void*)root_blk.data)) {
1251 ext4_dbg(DEBUG_DIR_IDX,
1252 DBG_WARN "HTree root checksum failed."
1253 "Inode: %" PRIu32", "
1254 "Block: %" PRIu32"\n",
1259 /* Initialize hinfo structure (mainly compute hash) */
1260 struct ext4_hash_info hinfo;
1261 r = ext4_dir_hinfo_init(&hinfo, &root_blk, &fs->sb, name_len, name);
1263 ext4_block_set(fs->bdev, &root_blk);
1264 return EXT4_ERR_BAD_DX_DIR;
1268 * Hardcoded number 2 means maximum height of index
1269 * tree defined in Linux.
1271 struct ext4_dir_idx_block dx_blks[2];
1272 struct ext4_dir_idx_block *dx_blk;
1273 struct ext4_dir_idx_block *dx_it;
1275 r = ext4_dir_dx_get_leaf(&hinfo, parent, &root_blk, &dx_blk, dx_blks);
1277 r = EXT4_ERR_BAD_DX_DIR;
1281 /* Try to insert to existing data block */
1282 uint32_t leaf_block_idx = ext4_dir_dx_entry_get_block(dx_blk->position);
1283 ext4_fsblk_t leaf_block_addr;
1284 r = ext4_fs_get_inode_dblk_idx(parent, leaf_block_idx,
1285 &leaf_block_addr, false);
1290 * Check if there is needed to split index node
1291 * (and recursively also parent nodes)
1293 r = ext4_dir_dx_split_index(parent, dx_blks, dx_blk, &dx_blk);
1295 goto release_target_index;
1297 struct ext4_block target_block;
1298 r = ext4_trans_block_get(fs->bdev, &target_block, leaf_block_addr);
1302 if (!ext4_dir_csum_verify(parent,(void *)target_block.data)) {
1303 ext4_dbg(DEBUG_DIR_IDX,
1304 DBG_WARN "HTree leaf block checksum failed."
1305 "Inode: %" PRIu32", "
1306 "Block: %" PRIu32"\n",
1311 /* Check if insert operation passed */
1312 r = ext4_dir_try_insert_entry(&fs->sb, parent, &target_block, child,
1315 goto release_target_index;
1317 /* Split entries to two blocks (includes sorting by hash value) */
1318 struct ext4_block new_block;
1319 r = ext4_dir_dx_split_data(parent, &hinfo, &target_block, dx_blk,
1323 goto release_target_index;
1326 /* Where to save new entry */
1327 uint32_t blk_hash = ext4_dir_dx_entry_get_hash(dx_blk->position + 1);
1328 if (hinfo.hash >= blk_hash)
1329 r = ext4_dir_try_insert_entry(&fs->sb, parent, &new_block,
1330 child, name, name_len);
1332 r = ext4_dir_try_insert_entry(&fs->sb, parent, &target_block,
1333 child, name, name_len);
1336 r = ext4_block_set(fs->bdev, &new_block);
1340 /* Cleanup operations */
1342 release_target_index:
1345 r = ext4_block_set(fs->bdev, &target_block);
1355 while (dx_it <= dx_blk) {
1356 r = ext4_block_set(fs->bdev, &dx_it->b);
1366 int ext4_dir_dx_reset_parent_inode(struct ext4_inode_ref *dir,
1367 uint32_t parent_inode)
1369 /* Load block 0, where will be index root located */
1370 ext4_fsblk_t fblock;
1371 int rc = ext4_fs_get_inode_dblk_idx(dir, 0, &fblock, false);
1375 struct ext4_block block;
1376 rc = ext4_trans_block_get(dir->fs->bdev, &block, fblock);
1380 if (!ext4_dir_dx_csum_verify(dir, (void *)block.data)) {
1381 ext4_dbg(DEBUG_DIR_IDX,
1382 DBG_WARN "HTree root checksum failed."
1383 "Inode: %" PRIu32", "
1384 "Block: %" PRIu32"\n",
1389 /* Initialize pointers to data structures */
1390 struct ext4_dir_idx_root *root = (void *)block.data;
1392 /* Fill the inode field with a new parent ino. */
1393 ext4_dx_dot_en_set_inode(&root->dots[1], parent_inode);
1395 ext4_dir_set_dx_csum(dir, (void *)block.data);
1396 ext4_trans_set_block_dirty(block.buf);
1398 return ext4_block_set(dir->fs->bdev, &block);