2 * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com)
\r
3 * All rights reserved.
\r
5 * Redistribution and use in source and binary forms, with or without
\r
6 * modification, are permitted provided that the following conditions
\r
9 * - Redistributions of source code must retain the above copyright
\r
10 * notice, this list of conditions and the following disclaimer.
\r
11 * - Redistributions in binary form must reproduce the above copyright
\r
12 * notice, this list of conditions and the following disclaimer in the
\r
13 * documentation and/or other materials provided with the distribution.
\r
14 * - The name of the author may not be used to endorse or promote products
\r
15 * derived from this software without specific prior written permission.
\r
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
\r
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
\r
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
\r
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
\r
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
\r
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
\r
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
\r
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
\r
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
\r
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\r
29 /** @addtogroup lwext4
\r
33 * @file ext4_dir_idx.c
\r
34 * @brief Directory indexing procedures.
\r
37 #include <ext4_config.h>
\r
38 #include <ext4_dir_idx.h>
\r
39 #include <ext4_dir.h>
\r
40 #include <ext4_blockdev.h>
\r
41 #include <ext4_fs.h>
\r
42 #include <ext4_super.h>
\r
43 #include <ext4_hash.h>
\r
48 /**@brief Sort entry item.*/
\r
49 struct ext4_dx_sort_entry {
\r
56 static int ext4_dir_dx_hash_string(struct ext4_hash_info *hinfo, int len,
\r
59 return ext2_htree_hash(name, len, hinfo->seed, hinfo->hash_version,
\r
60 &hinfo->hash, &hinfo->minor_hash);
\r
65 /****************************************************************************/
\r
67 int ext4_dir_dx_init(struct ext4_inode_ref *dir)
\r
69 /* Load block 0, where will be index root located */
\r
71 int rc = ext4_fs_get_inode_data_block_index(dir, 0,
\r
76 struct ext4_block block;
\r
77 rc = ext4_block_get(dir->fs->bdev, &block, fblock);
\r
81 /* Initialize pointers to data structures */
\r
82 struct ext4_directory_dx_root *root = (void *)block.data;
\r
83 struct ext4_directory_dx_root_info *info = &(root->info);
\r
85 /* Initialize root info structure */
\r
86 uint8_t hash_version = ext4_get8(&dir->fs->sb, default_hash_version);
\r
88 ext4_dir_dx_root_info_set_hash_version(info, hash_version);
\r
89 ext4_dir_dx_root_info_set_indirect_levels(info, 0);
\r
90 ext4_dir_dx_root_info_set_info_length(info, 8);
\r
92 /* Set limit and current number of entries */
\r
93 struct ext4_directory_dx_countlimit *countlimit =
\r
94 (struct ext4_directory_dx_countlimit *) &root->entries;
\r
96 ext4_dir_dx_countlimit_set_count(countlimit, 1);
\r
98 uint32_t block_size =
\r
99 ext4_sb_get_block_size(&dir->fs->sb);
\r
100 uint32_t entry_space =
\r
101 block_size - 2 * sizeof(struct ext4_directory_dx_dot_entry) -
\r
102 sizeof(struct ext4_directory_dx_root_info);
\r
103 uint16_t root_limit = entry_space / sizeof(struct ext4_directory_dx_entry);
\r
104 ext4_dir_dx_countlimit_set_limit(countlimit, root_limit);
\r
106 /* Append new block, where will be new entries inserted in the future */
\r
108 rc = ext4_fs_append_inode_block(dir, &fblock, &iblock);
\r
110 ext4_block_set(dir->fs->bdev, &block);
\r
114 struct ext4_block new_block;
\r
116 rc = ext4_block_get(dir->fs->bdev, &new_block, fblock);
\r
118 ext4_block_set(dir->fs->bdev, &block);
\r
122 /* Fill the whole block with empty entry */
\r
123 struct ext4_directory_entry_ll *block_entry = (void *)new_block.data;
\r
125 ext4_dir_entry_ll_set_entry_length(block_entry, block_size);
\r
126 ext4_dir_entry_ll_set_inode(block_entry, 0);
\r
128 new_block.dirty = true;
\r
129 rc = ext4_block_set(dir->fs->bdev, &new_block);
\r
131 ext4_block_set(dir->fs->bdev, &block);
\r
135 /* Connect new block to the only entry in index */
\r
136 struct ext4_directory_dx_entry *entry = root->entries;
\r
137 ext4_dir_dx_entry_set_block(entry, iblock);
\r
139 block.dirty = true;
\r
141 return ext4_block_set(dir->fs->bdev, &block);
\r
144 /**@brief Initialize hash info structure necessary for index operations.
\r
145 * @param hinfo Pointer to hinfo to be initialized
\r
146 * @param root_block Root block (number 0) of index
\r
147 * @param sb Pointer to superblock
\r
148 * @param name_len Length of name to be computed hash value from
\r
149 * @param name Name to be computed hash value from
\r
150 * @return Standard error code
\r
152 static int ext4_dir_hinfo_init(struct ext4_hash_info *hinfo,
\r
153 struct ext4_block *root_block, struct ext4_sblock *sb, size_t name_len,
\r
156 struct ext4_directory_dx_root *root =
\r
157 (struct ext4_directory_dx_root *) root_block->data;
\r
159 if ((root->info.hash_version != EXT2_HTREE_LEGACY) &&
\r
160 (root->info.hash_version != EXT2_HTREE_HALF_MD4) &&
\r
161 (root->info.hash_version != EXT2_HTREE_TEA))
\r
162 return EXT4_ERR_BAD_DX_DIR;
\r
164 /* Check unused flags */
\r
165 if (root->info.unused_flags != 0)
\r
166 return EXT4_ERR_BAD_DX_DIR;
\r
168 /* Check indirect levels */
\r
169 if (root->info.indirect_levels > 1)
\r
170 return EXT4_ERR_BAD_DX_DIR;
\r
172 /* Check if node limit is correct */
\r
173 uint32_t block_size = ext4_sb_get_block_size(sb);
\r
174 uint32_t entry_space = block_size;
\r
175 entry_space -= 2 * sizeof(struct ext4_directory_dx_dot_entry);
\r
176 entry_space -= sizeof(struct ext4_directory_dx_root_info);
\r
177 entry_space = entry_space / sizeof(struct ext4_directory_dx_entry);
\r
179 uint16_t limit = ext4_dir_dx_countlimit_get_limit(
\r
180 (struct ext4_directory_dx_countlimit *) &root->entries);
\r
181 if (limit != entry_space)
\r
182 return EXT4_ERR_BAD_DX_DIR;
\r
184 /* Check hash version and modify if necessary */
\r
185 hinfo->hash_version =
\r
186 ext4_dir_dx_root_info_get_hash_version(&root->info);
\r
187 if ((hinfo->hash_version <= EXT2_HTREE_TEA) &&
\r
188 (ext4_sb_check_flag(sb, EXT4_SUPERBLOCK_FLAGS_UNSIGNED_HASH))) {
\r
189 /* Use unsigned hash */
\r
190 hinfo->hash_version += 3;
\r
193 /* Load hash seed from superblock */
\r
195 hinfo->seed = ext4_get8(sb, hash_seed);
\r
197 /* Compute hash value of name */
\r
199 return ext4_dir_dx_hash_string(hinfo, name_len, name);
\r
204 /**@brief Walk through index tree and load leaf with corresponding hash value.
\r
205 * @param hinfo Initialized hash info structure
\r
206 * @param inode_ref Current i-node
\r
207 * @param root_block Root block (iblock 0), where is root node located
\r
208 * @param dx_block Pointer to leaf node in dx_blocks array
\r
209 * @param dx_blocks Array with the whole path from root to leaf
\r
210 * @return Standard error code
\r
212 static int ext4_dir_dx_get_leaf(struct ext4_hash_info *hinfo,
\r
213 struct ext4_inode_ref *inode_ref, struct ext4_block *root_block,
\r
214 struct ext4_directory_dx_block **dx_block,
\r
215 struct ext4_directory_dx_block *dx_blocks)
\r
217 struct ext4_directory_dx_block *tmp_dx_block = dx_blocks;
\r
218 struct ext4_directory_dx_root *root =
\r
219 (struct ext4_directory_dx_root *) root_block->data;
\r
220 struct ext4_directory_dx_entry *entries =
\r
221 (struct ext4_directory_dx_entry *) &root->entries;
\r
223 uint16_t limit = ext4_dir_dx_countlimit_get_limit(
\r
224 (struct ext4_directory_dx_countlimit *) entries);
\r
225 uint8_t indirect_level =
\r
226 ext4_dir_dx_root_info_get_indirect_levels(&root->info);
\r
228 struct ext4_block *tmp_block = root_block;
\r
229 struct ext4_directory_dx_entry *p;
\r
230 struct ext4_directory_dx_entry *q;
\r
231 struct ext4_directory_dx_entry *m;
\r
232 struct ext4_directory_dx_entry *at;
\r
234 /* Walk through the index tree */
\r
236 uint16_t count = ext4_dir_dx_countlimit_get_count(
\r
237 (struct ext4_directory_dx_countlimit *) entries);
\r
238 if ((count == 0) || (count > limit))
\r
239 return EXT4_ERR_BAD_DX_DIR;
\r
241 /* Do binary search in every node */
\r
243 q = entries + count - 1;
\r
246 m = p + (q - p) / 2;
\r
247 if (ext4_dir_dx_entry_get_hash(m) > hinfo->hash)
\r
255 /* Write results */
\r
257 memcpy(&tmp_dx_block->block, tmp_block, sizeof(struct ext4_block));
\r
258 tmp_dx_block->entries = entries;
\r
259 tmp_dx_block->position = at;
\r
261 /* Is algorithm in the leaf? */
\r
262 if (indirect_level == 0) {
\r
263 *dx_block = tmp_dx_block;
\r
267 /* Goto child node */
\r
268 uint32_t next_block = ext4_dir_dx_entry_get_block(at);
\r
273 int rc = ext4_fs_get_inode_data_block_index(inode_ref,
\r
274 next_block, &fblock);
\r
278 rc = ext4_block_get(inode_ref->fs->bdev, tmp_block, fblock);
\r
282 entries = ((struct ext4_directory_dx_node *) tmp_block->data)->entries;
\r
283 limit = ext4_dir_dx_countlimit_get_limit(
\r
284 (struct ext4_directory_dx_countlimit *) entries);
\r
286 uint16_t entry_space =
\r
287 ext4_sb_get_block_size(&inode_ref->fs->sb) -
\r
288 sizeof(struct ext4_fake_directory_entry);
\r
290 entry_space = entry_space / sizeof(struct ext4_directory_dx_entry);
\r
292 if (limit != entry_space) {
\r
293 ext4_block_set(inode_ref->fs->bdev, tmp_block);
\r
294 return EXT4_ERR_BAD_DX_DIR;
\r
304 /**@brief Check if the the next block would be checked during entry search.
\r
305 * @param inode_ref Directory i-node
\r
306 * @param hash Hash value to check
\r
307 * @param dx_block Current block
\r
308 * @param dx_blocks Array with path from root to leaf node
\r
309 * @return Standard Error codee
\r
311 static int ext4_dir_dx_next_block(struct ext4_inode_ref *inode_ref,
\r
312 uint32_t hash, struct ext4_directory_dx_block *dx_block,
\r
313 struct ext4_directory_dx_block *dx_blocks)
\r
315 uint32_t num_handles = 0;
\r
316 struct ext4_directory_dx_block *p = dx_block;
\r
318 /* Try to find data block with next bunch of entries */
\r
321 uint16_t count = ext4_dir_dx_countlimit_get_count(
\r
322 (struct ext4_directory_dx_countlimit *) p->entries);
\r
324 if (p->position < p->entries + count)
\r
327 if (p == dx_blocks)
\r
334 /* Check hash collision (if not occured - no next block cannot be used)*/
\r
335 uint32_t current_hash = ext4_dir_dx_entry_get_hash(p->position);
\r
336 if ((hash & 1) == 0) {
\r
337 if ((current_hash & ~1) != hash)
\r
341 /* Fill new path */
\r
342 while (num_handles--) {
\r
343 uint32_t block_idx =
\r
344 ext4_dir_dx_entry_get_block(p->position);
\r
345 uint32_t block_addr;
\r
347 int rc = ext4_fs_get_inode_data_block_index(inode_ref,
\r
348 block_idx, &block_addr);
\r
353 struct ext4_block block;
\r
354 rc = ext4_block_get(inode_ref->fs->bdev, &block, block_addr);
\r
360 /* Don't forget to put old block (prevent memory leak) */
\r
361 rc = ext4_block_set(inode_ref->fs->bdev, &p->block);
\r
366 memcpy(&p->block, &p->block, sizeof(block));
\r
367 p->entries = ((struct ext4_directory_dx_node *) block.data)->entries;
\r
368 p->position = p->entries;
\r
376 int ext4_dir_dx_find_entry(struct ext4_directory_search_result * result,
\r
377 struct ext4_inode_ref *inode_ref, size_t name_len, const char *name)
\r
379 /* Load direct block 0 (index root) */
\r
380 uint32_t root_block_addr;
\r
382 int rc = ext4_fs_get_inode_data_block_index(inode_ref, 0,
\r
387 struct ext4_fs *fs = inode_ref->fs;
\r
389 struct ext4_block root_block;
\r
390 rc = ext4_block_get(fs->bdev, &root_block, root_block_addr);
\r
394 /* Initialize hash info (compute hash value) */
\r
395 struct ext4_hash_info hinfo;
\r
396 rc = ext4_dir_hinfo_init(&hinfo, &root_block, &fs->sb,
\r
399 ext4_block_set(fs->bdev, &root_block);
\r
400 return EXT4_ERR_BAD_DX_DIR;
\r
404 * Hardcoded number 2 means maximum height of index tree,
\r
405 * specified in the Linux driver.
\r
407 struct ext4_directory_dx_block dx_blocks[2];
\r
408 struct ext4_directory_dx_block *dx_block;
\r
409 struct ext4_directory_dx_block *tmp;
\r
411 rc = ext4_dir_dx_get_leaf(&hinfo, inode_ref, &root_block,
\r
412 &dx_block, dx_blocks);
\r
414 ext4_block_set(fs->bdev, &root_block);
\r
415 return EXT4_ERR_BAD_DX_DIR;
\r
419 /* Load leaf block */
\r
420 uint32_t leaf_block_idx =
\r
421 ext4_dir_dx_entry_get_block(dx_block->position);
\r
422 uint32_t leaf_block_addr;
\r
424 rc = ext4_fs_get_inode_data_block_index(inode_ref,
\r
425 leaf_block_idx, &leaf_block_addr);
\r
429 struct ext4_block leaf_block;
\r
430 rc = ext4_block_get(fs->bdev, &leaf_block, leaf_block_addr);
\r
434 /* Linear search inside block */
\r
435 struct ext4_directory_entry_ll *res_dentry;
\r
436 rc = ext4_dir_find_in_block(&leaf_block, &fs->sb,
\r
437 name_len, name, &res_dentry);
\r
439 /* Found => return it */
\r
441 result->block = leaf_block;
\r
442 result->dentry = res_dentry;
\r
446 /* Not found, leave untouched */
\r
447 rc2 = ext4_block_set(fs->bdev, &leaf_block);
\r
454 /* check if the next block could be checked */
\r
455 rc = ext4_dir_dx_next_block(inode_ref, hinfo.hash,
\r
456 dx_block, &dx_blocks[0]);
\r
459 } while (rc == ENOENT);
\r
461 /* Entry not found */
\r
465 /* The whole path must be released (preventing memory leak) */
\r
468 while (tmp <= dx_block) {
\r
469 rc2 = ext4_block_set(fs->bdev, &tmp->block);
\r
470 if (rc == EOK && rc2 != EOK)
\r
478 #if CONFIG_DIR_INDEX_COMB_SORT
\r
479 #define SWAP_ENTRY(se1, se2) do { \
\r
480 struct ext4_dx_sort_entry tmp = se1; \
\r
485 static void comb_sort(struct ext4_dx_sort_entry *se, uint32_t count)
\r
487 struct ext4_dx_sort_entry *p, *q, *top = se + count - 1;
\r
490 while (count > 2) {
\r
491 count = (count * 10) / 13;
\r
494 for (p = top, q = p - count; q >= se; p--, q--)
\r
495 if (p->hash < q->hash)
\r
496 SWAP_ENTRY(*p, *q);
\r
503 if (q[1].hash >= q[0].hash)
\r
505 SWAP_ENTRY(*(q+1), *q);
\r
512 /**@brief Compare function used to pass in quicksort implementation.
\r
513 * It can compare two entries by hash value.
\r
514 * @param arg1 First entry
\r
515 * @param arg2 Second entry
\r
516 * @param dummy Unused parameter, can be NULL
\r
518 * @return Classic compare result
\r
519 * (0: equal, -1: arg1 < arg2, 1: arg1 > arg2)
\r
521 static int ext4_dir_dx_entry_comparator(const void *arg1, const void *arg2)
\r
523 struct ext4_dx_sort_entry *entry1 = (void *)arg1;
\r
524 struct ext4_dx_sort_entry *entry2 = (void *)arg2;
\r
526 if (entry1->hash == entry2->hash)
\r
529 if (entry1->hash < entry2->hash)
\r
536 /**@brief Insert new index entry to block.
\r
537 * Note that space for new entry must be checked by caller.
\r
538 * @param index_block Block where to insert new entry
\r
539 * @param hash Hash value covered by child node
\r
540 * @param iblock Logical number of child block
\r
543 static void ext4_dir_dx_insert_entry(
\r
544 struct ext4_directory_dx_block *index_block, uint32_t hash,
\r
547 struct ext4_directory_dx_entry *old_index_entry = index_block->position;
\r
548 struct ext4_directory_dx_entry *new_index_entry = old_index_entry + 1;
\r
550 struct ext4_directory_dx_countlimit *countlimit =
\r
551 (struct ext4_directory_dx_countlimit *) index_block->entries;
\r
552 uint32_t count = ext4_dir_dx_countlimit_get_count(countlimit);
\r
554 struct ext4_directory_dx_entry *start_index = index_block->entries;
\r
555 size_t bytes = (uint8_t *) (start_index + count) - (uint8_t *) (new_index_entry);
\r
557 memmove(new_index_entry + 1, new_index_entry, bytes);
\r
559 ext4_dir_dx_entry_set_block(new_index_entry, iblock);
\r
560 ext4_dir_dx_entry_set_hash(new_index_entry, hash);
\r
562 ext4_dir_dx_countlimit_set_count(countlimit, count + 1);
\r
564 index_block->block.dirty = true;
\r
567 /**@brief Split directory entries to two parts preventing node overflow.
\r
568 * @param inode_ref Directory i-node
\r
569 * @param hinfo Hash info
\r
570 * @param old_data_block Block with data to be split
\r
571 * @param index_block Block where index entries are located
\r
572 * @param new_data_block Output value for newly allocated data block
\r
574 static int ext4_dir_dx_split_data(struct ext4_inode_ref *inode_ref,
\r
575 struct ext4_hash_info *hinfo, struct ext4_block *old_data_block,
\r
576 struct ext4_directory_dx_block *index_block,
\r
577 struct ext4_block *new_data_block)
\r
581 /* Allocate buffer for directory entries */
\r
582 uint32_t block_size =
\r
583 ext4_sb_get_block_size(&inode_ref->fs->sb);
\r
585 uint8_t *entry_buffer = malloc(block_size);
\r
586 if (entry_buffer == NULL)
\r
589 /* dot entry has the smallest size available */
\r
590 uint32_t max_entry_count =
\r
591 block_size / sizeof(struct ext4_directory_dx_dot_entry);
\r
593 /* Allocate sort entry */
\r
594 struct ext4_dx_sort_entry *sort_array =
\r
595 malloc(max_entry_count * sizeof(struct ext4_dx_sort_entry));
\r
597 if (sort_array == NULL) {
\r
598 free(entry_buffer);
\r
603 uint32_t real_size = 0;
\r
605 /* Initialize hinfo */
\r
606 struct ext4_hash_info tmp_hinfo;
\r
607 memcpy(&tmp_hinfo, hinfo, sizeof(struct ext4_hash_info));
\r
609 /* Load all valid entries to the buffer */
\r
610 struct ext4_directory_entry_ll *dentry = (void *)old_data_block->data;
\r
611 uint8_t *entry_buffer_ptr = entry_buffer;
\r
612 while ((void *)dentry < (void *)(old_data_block->data + block_size)) {
\r
613 /* Read only valid entries */
\r
614 if (ext4_dir_entry_ll_get_inode(dentry) && dentry->name_length) {
\r
615 uint8_t len = ext4_dir_entry_ll_get_name_length(
\r
616 &inode_ref->fs->sb, dentry);
\r
618 rc = ext4_dir_dx_hash_string(&tmp_hinfo, len, (char*)dentry->name);
\r
621 free(entry_buffer);
\r
625 uint32_t rec_len = 8 + len;
\r
627 if ((rec_len % 4) != 0)
\r
628 rec_len += 4 - (rec_len % 4);
\r
630 memcpy(entry_buffer_ptr, dentry, rec_len);
\r
632 sort_array[idx].dentry = entry_buffer_ptr;
\r
633 sort_array[idx].rec_len = rec_len;
\r
634 sort_array[idx].hash = tmp_hinfo.hash;
\r
636 entry_buffer_ptr += rec_len;
\r
637 real_size += rec_len;
\r
641 dentry = (void *)((uint8_t *)dentry +
\r
642 ext4_dir_entry_ll_get_entry_length(dentry));
\r
645 /* Sort all entries */
\r
646 #if CONFIG_DIR_INDEX_COMB_SORT
\r
647 comb_sort(sort_array, idx);
\r
649 qsort(sort_array, idx, sizeof(struct ext4_dx_sort_entry),
\r
650 ext4_dir_dx_entry_comparator);
\r
652 /* Allocate new block for store the second part of entries */
\r
653 uint32_t new_fblock;
\r
654 uint32_t new_iblock;
\r
655 rc = ext4_fs_append_inode_block(inode_ref, &new_fblock,
\r
659 free(entry_buffer);
\r
663 /* Load new block */
\r
664 struct ext4_block new_data_block_tmp;
\r
665 rc = ext4_block_get(inode_ref->fs->bdev, &new_data_block_tmp,
\r
669 free(entry_buffer);
\r
674 * Distribute entries to two blocks (by size)
\r
675 * - compute the half
\r
677 uint32_t new_hash = 0;
\r
678 uint32_t current_size = 0;
\r
681 for ( i = 0; i < idx; ++i) {
\r
682 if ((current_size + sort_array[i].rec_len) > (block_size / 2)) {
\r
683 new_hash = sort_array[i].hash;
\r
688 current_size += sort_array[i].rec_len;
\r
691 /* Check hash collision */
\r
692 uint32_t continued = 0;
\r
693 if (new_hash == sort_array[mid-1].hash)
\r
696 uint32_t offset = 0;
\r
699 /* First part - to the old block */
\r
700 for (i = 0; i < mid; ++i) {
\r
701 ptr = old_data_block->data + offset;
\r
702 memcpy(ptr, sort_array[i].dentry, sort_array[i].rec_len);
\r
704 struct ext4_directory_entry_ll *tmp = ptr;
\r
706 ext4_dir_entry_ll_set_entry_length(tmp,
\r
707 sort_array[i].rec_len);
\r
709 ext4_dir_entry_ll_set_entry_length(tmp,
\r
710 block_size - offset);
\r
712 offset += sort_array[i].rec_len;
\r
715 /* Second part - to the new block */
\r
717 for (i = mid; i < idx; ++i) {
\r
718 ptr = new_data_block_tmp.data + offset;
\r
719 memcpy(ptr, sort_array[i].dentry, sort_array[i].rec_len);
\r
721 struct ext4_directory_entry_ll *tmp = ptr;
\r
723 ext4_dir_entry_ll_set_entry_length(tmp,
\r
724 sort_array[i].rec_len);
\r
726 ext4_dir_entry_ll_set_entry_length(tmp,
\r
727 block_size - offset);
\r
729 offset += sort_array[i].rec_len;
\r
732 /* Do some steps to finish operation */
\r
733 old_data_block->dirty = true;
\r
734 new_data_block_tmp.dirty = true;
\r
737 free(entry_buffer);
\r
739 ext4_dir_dx_insert_entry(index_block, new_hash + continued,
\r
742 *new_data_block = new_data_block_tmp;
\r
747 /**@brief Split index node and maybe some parent nodes in the tree hierarchy.
\r
748 * @param inode_ref Directory i-node
\r
749 * @param dx_blocks Array with path from root to leaf node
\r
750 * @param dx_block Leaf block to be split if needed
\r
751 * @return Error code
\r
753 static int ext4_dir_dx_split_index(struct ext4_inode_ref *inode_ref,
\r
754 struct ext4_directory_dx_block *dx_blocks,
\r
755 struct ext4_directory_dx_block *dx_block,
\r
756 struct ext4_directory_dx_block **new_dx_block)
\r
758 struct ext4_directory_dx_entry *entries;
\r
760 if (dx_block == dx_blocks)
\r
762 ((struct ext4_directory_dx_root *) dx_block->block.data)->entries;
\r
765 ((struct ext4_directory_dx_node *) dx_block->block.data)->entries;
\r
767 struct ext4_directory_dx_countlimit *countlimit =
\r
768 (struct ext4_directory_dx_countlimit *) entries;
\r
770 uint16_t leaf_limit =
\r
771 ext4_dir_dx_countlimit_get_limit(countlimit);
\r
772 uint16_t leaf_count =
\r
773 ext4_dir_dx_countlimit_get_count(countlimit);
\r
775 /* Check if is necessary to split index block */
\r
776 if (leaf_limit == leaf_count) {
\r
777 size_t levels = dx_block - dx_blocks;
\r
779 struct ext4_directory_dx_entry *root_entries =
\r
780 ((struct ext4_directory_dx_root *) dx_blocks[0].block.data)->entries;
\r
782 struct ext4_directory_dx_countlimit *root_countlimit =
\r
783 (struct ext4_directory_dx_countlimit *) root_entries;
\r
784 uint16_t root_limit =
\r
785 ext4_dir_dx_countlimit_get_limit(root_countlimit);
\r
786 uint16_t root_count =
\r
787 ext4_dir_dx_countlimit_get_count(root_countlimit);
\r
789 /* Linux limitation */
\r
790 if ((levels > 0) && (root_limit == root_count))
\r
793 /* Add new block to directory */
\r
794 uint32_t new_fblock;
\r
795 uint32_t new_iblock;
\r
796 int rc = ext4_fs_append_inode_block(inode_ref,
\r
797 &new_fblock, &new_iblock);
\r
801 /* load new block */
\r
802 struct ext4_block new_block;
\r
803 rc = ext4_block_get(inode_ref->fs->bdev, &new_block,
\r
808 struct ext4_directory_dx_node *new_node = (void *)new_block.data;
\r
809 struct ext4_directory_dx_entry *new_entries = new_node->entries;
\r
811 memset(&new_node->fake, 0, sizeof(struct ext4_fake_directory_entry));
\r
813 uint32_t block_size =
\r
814 ext4_sb_get_block_size(&inode_ref->fs->sb);
\r
816 new_node->fake.entry_length = block_size;
\r
818 /* Split leaf node */
\r
820 uint32_t count_left = leaf_count / 2;
\r
821 uint32_t count_right = leaf_count - count_left;
\r
822 uint32_t hash_right =
\r
823 ext4_dir_dx_entry_get_hash(entries + count_left);
\r
825 /* Copy data to new node */
\r
826 memcpy((void *) new_entries, (void *) (entries + count_left),
\r
827 count_right * sizeof(struct ext4_directory_dx_entry));
\r
829 /* Initialize new node */
\r
830 struct ext4_directory_dx_countlimit *left_countlimit =
\r
831 (struct ext4_directory_dx_countlimit *) entries;
\r
832 struct ext4_directory_dx_countlimit *right_countlimit =
\r
833 (struct ext4_directory_dx_countlimit *) new_entries;
\r
835 ext4_dir_dx_countlimit_set_count(left_countlimit, count_left);
\r
836 ext4_dir_dx_countlimit_set_count(right_countlimit, count_right);
\r
838 uint32_t entry_space =
\r
839 block_size - sizeof(struct ext4_fake_directory_entry);
\r
840 uint32_t node_limit =
\r
841 entry_space / sizeof(struct ext4_directory_dx_entry);
\r
842 ext4_dir_dx_countlimit_set_limit(right_countlimit, node_limit);
\r
844 /* Which index block is target for new entry */
\r
845 uint32_t position_index = (dx_block->position - dx_block->entries);
\r
846 if (position_index >= count_left) {
\r
847 dx_block->block.dirty = true;
\r
849 struct ext4_block block_tmp = dx_block->block;
\r
852 dx_block->block = new_block;
\r
854 dx_block->position =
\r
855 new_entries + position_index - count_left;
\r
856 dx_block->entries = new_entries;
\r
858 new_block = block_tmp;
\r
861 /* Finally insert new entry */
\r
862 ext4_dir_dx_insert_entry(dx_blocks, hash_right, new_iblock);
\r
863 dx_blocks[0].block.dirty = true;
\r
864 dx_blocks[1].block.dirty = true;
\r
866 new_block.dirty = true;
\r
867 return ext4_block_set(inode_ref->fs->bdev, &new_block);
\r
869 /* Create second level index */
\r
871 /* Copy data from root to child block */
\r
872 memcpy((void *) new_entries, (void *) entries,
\r
873 leaf_count * sizeof(struct ext4_directory_dx_entry));
\r
875 struct ext4_directory_dx_countlimit *new_countlimit =
\r
876 (struct ext4_directory_dx_countlimit *) new_entries;
\r
878 uint32_t entry_space =
\r
879 block_size - sizeof(struct ext4_fake_directory_entry);
\r
880 uint32_t node_limit =
\r
881 entry_space / sizeof(struct ext4_directory_dx_entry);
\r
882 ext4_dir_dx_countlimit_set_limit(new_countlimit, node_limit);
\r
884 /* Set values in root node */
\r
885 struct ext4_directory_dx_countlimit *new_root_countlimit =
\r
886 (struct ext4_directory_dx_countlimit *) entries;
\r
888 ext4_dir_dx_countlimit_set_count(new_root_countlimit, 1);
\r
889 ext4_dir_dx_entry_set_block(entries, new_iblock);
\r
891 ((struct ext4_directory_dx_root *)
\r
892 dx_blocks[0].block.data)->info.indirect_levels = 1;
\r
894 /* Add new entry to the path */
\r
895 dx_block = dx_blocks + 1;
\r
896 dx_block->position = dx_blocks->position - entries + new_entries;
\r
897 dx_block->entries = new_entries;
\r
898 dx_block->block = new_block;
\r
900 *new_dx_block = dx_block;
\r
902 dx_blocks[0].block.dirty = true;
\r
903 dx_blocks[1].block.dirty = true;
\r
910 int ext4_dir_dx_add_entry(struct ext4_inode_ref *parent,
\r
911 struct ext4_inode_ref *child, const char *name)
\r
915 /* Get direct block 0 (index root) */
\r
916 uint32_t root_block_addr;
\r
917 int rc = ext4_fs_get_inode_data_block_index(parent, 0,
\r
922 struct ext4_fs *fs = parent->fs;
\r
923 struct ext4_block root_block;
\r
925 rc = ext4_block_get(fs->bdev, &root_block, root_block_addr);
\r
929 /* Initialize hinfo structure (mainly compute hash) */
\r
930 uint32_t name_len = strlen(name);
\r
931 struct ext4_hash_info hinfo;
\r
932 rc = ext4_dir_hinfo_init(&hinfo, &root_block, &fs->sb,
\r
935 ext4_block_set(fs->bdev, &root_block);
\r
936 return EXT4_ERR_BAD_DX_DIR;
\r
940 * Hardcoded number 2 means maximum height of index
\r
941 * tree defined in Linux.
\r
943 struct ext4_directory_dx_block dx_blocks[2];
\r
944 struct ext4_directory_dx_block *dx_block;
\r
945 struct ext4_directory_dx_block *dx_it;
\r
947 rc = ext4_dir_dx_get_leaf(&hinfo, parent, &root_block,
\r
948 &dx_block, dx_blocks);
\r
950 rc = EXT4_ERR_BAD_DX_DIR;
\r
951 goto release_index;
\r
954 /* Try to insert to existing data block */
\r
955 uint32_t leaf_block_idx =
\r
956 ext4_dir_dx_entry_get_block(dx_block->position);
\r
957 uint32_t leaf_block_addr;
\r
958 rc = ext4_fs_get_inode_data_block_index(parent, leaf_block_idx,
\r
961 goto release_index;
\r
964 * Check if there is needed to split index node
\r
965 * (and recursively also parent nodes)
\r
967 rc = ext4_dir_dx_split_index(parent, dx_blocks, dx_block, &dx_block);
\r
969 goto release_target_index;
\r
971 struct ext4_block target_block;
\r
972 rc = ext4_block_get(fs->bdev, &target_block, leaf_block_addr);
\r
974 goto release_index;
\r
977 /* Check if insert operation passed */
\r
978 rc = ext4_dir_try_insert_entry(&fs->sb, &target_block, child,
\r
981 goto release_target_index;
\r
984 /* Split entries to two blocks (includes sorting by hash value) */
\r
985 struct ext4_block new_block;
\r
986 rc = ext4_dir_dx_split_data(parent, &hinfo, &target_block,
\r
987 dx_block, &new_block);
\r
990 goto release_target_index;
\r
993 /* Where to save new entry */
\r
994 uint32_t new_block_hash =
\r
995 ext4_dir_dx_entry_get_hash(dx_block->position + 1);
\r
996 if (hinfo.hash >= new_block_hash)
\r
997 rc = ext4_dir_try_insert_entry(&fs->sb, &new_block,
\r
998 child, name, name_len);
\r
1000 rc = ext4_dir_try_insert_entry(&fs->sb, &target_block,
\r
1001 child, name, name_len);
\r
1004 rc = ext4_block_set(fs->bdev, &new_block);
\r
1008 /* Cleanup operations */
\r
1010 release_target_index:
\r
1013 rc = ext4_block_set(fs->bdev, &target_block);
\r
1021 dx_it = dx_blocks;
\r
1023 while (dx_it <= dx_block) {
\r
1024 rc = ext4_block_set(fs->bdev, &dx_it->block);
\r