source: mainline/uspace/lib/ext4/libext4_directory_index.c@ e6f6260

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since e6f6260 was e6f6260, checked in by Frantisek Princ <frantisek.princ@…>, 13 years ago

next part of directory index comments

  • Property mode set to 100644
File size: 30.3 KB
Line 
1/*
2 * Copyright (c) 2012 Frantisek Princ
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
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.
16 *
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.
27 */
28
29/** @addtogroup libext4
30 * @{
31 */
32
33/**
34 * @file libext4_directory_index.c
35 * @brief Ext4 directory index operations.
36 */
37
38#include <byteorder.h>
39#include <errno.h>
40#include <malloc.h>
41#include <sort.h>
42#include <string.h>
43#include "libext4.h"
44
45/** Type entry to pass to sorting algorithm.
46 *
47 */
48typedef struct ext4_dx_sort_entry {
49 uint32_t hash;
50 uint32_t rec_len;
51 void *dentry;
52} ext4_dx_sort_entry_t;
53
54
55/** Get hash version used in directory index.
56 *
57 * @param root_info pointer to root info structure of index
58 * @return hash algorithm version
59 */
60uint8_t ext4_directory_dx_root_info_get_hash_version(
61 ext4_directory_dx_root_info_t *root_info)
62{
63 return root_info->hash_version;
64}
65
66/** Set hash version, that will be used in directory index.
67 *
68 * @param root_info pointer to root info structure of index
69 * @param version hash algorithm version
70 */
71void ext4_directory_dx_root_info_set_hash_version(
72 ext4_directory_dx_root_info_t *root_info, uint8_t version)
73{
74 root_info->hash_version = version;
75}
76
77/** Get length of root_info structure in bytes.
78 *
79 * @param root_info pointer to root info structure of index
80 * @return length of the structure
81 */
82uint8_t ext4_directory_dx_root_info_get_info_length(
83 ext4_directory_dx_root_info_t *root_info)
84{
85 return root_info->info_length;
86}
87
88/** Set length of root_info structure in bytes.
89 *
90 * @param root_info pointer to root info structure of index
91 * @param info_length length of the structure
92 */
93void ext4_directory_dx_root_info_set_info_length(
94 ext4_directory_dx_root_info_t *root_info, uint8_t info_length)
95{
96 root_info->info_length = info_length;
97}
98
99/** Get number of indirect levels of HTree.
100 *
101 * @param root_info pointer to root info structure of index
102 * @return height of HTree (actually only 0 or 1)
103 */
104uint8_t ext4_directory_dx_root_info_get_indirect_levels(
105 ext4_directory_dx_root_info_t *root_info)
106{
107 return root_info->indirect_levels;
108}
109
110/** Set number of indirect levels of HTree.
111 *
112 * @param root_info pointer to root info structure of index
113 * @param levels height of HTree (actually only 0 or 1)
114 */
115void ext4_directory_dx_root_info_set_indirect_levels(
116 ext4_directory_dx_root_info_t *root_info, uint8_t levels)
117{
118 root_info->indirect_levels = levels;
119}
120
121/** Get maximum number of index node entries.
122 *
123 * @param countlimit pointer to counlimit structure
124 * @return maximum of entries in node
125 */
126uint16_t ext4_directory_dx_countlimit_get_limit(
127 ext4_directory_dx_countlimit_t *countlimit)
128{
129 return uint16_t_le2host(countlimit->limit);
130}
131
132/** Set maximum number of index node entries.
133 *
134 * @param countlimit pointer to counlimit structure
135 * @param limit maximum of entries in node
136 */
137void ext4_directory_dx_countlimit_set_limit(
138 ext4_directory_dx_countlimit_t *countlimit, uint16_t limit)
139{
140 countlimit->limit = host2uint16_t_le(limit);
141}
142
143/** Get current number of index node entries.
144 *
145 * @param countlimit pointer to counlimit structure
146 * @return number of entries in node
147 */
148uint16_t ext4_directory_dx_countlimit_get_count(
149 ext4_directory_dx_countlimit_t *countlimit)
150{
151 return uint16_t_le2host(countlimit->count);
152}
153
154/** Set current number of index node entries.
155 *
156 * @param countlimit pointer to counlimit structure
157 * @param count number of entries in node
158 */
159void ext4_directory_dx_countlimit_set_count(
160 ext4_directory_dx_countlimit_t *countlimit, uint16_t count)
161{
162 countlimit->count = host2uint16_t_le(count);
163}
164
165/** Get hash value of index entry.
166 *
167 * @param entry pointer to index entry
168 * @return hash value
169 */
170uint32_t ext4_directory_dx_entry_get_hash(ext4_directory_dx_entry_t *entry)
171{
172 return uint32_t_le2host(entry->hash);
173}
174
175
176/** Set hash value of index entry.
177 *
178 * @param entry pointer to index entry
179 * @param hash hash value
180 */
181void ext4_directory_dx_entry_set_hash(ext4_directory_dx_entry_t *entry,
182 uint32_t hash)
183{
184 entry->hash = host2uint32_t_le(hash);
185}
186
187/** Get block address where child node is located.
188 *
189 * @param entry pointer to index entry
190 * @return block address of child node
191 */
192uint32_t ext4_directory_dx_entry_get_block(ext4_directory_dx_entry_t *entry)
193{
194 return uint32_t_le2host(entry->block);
195}
196
197/** Set block address where child node is located.
198 *
199 * @param entry pointer to index entry
200 * @param block block address of child node
201 */
202void ext4_directory_dx_entry_set_block(ext4_directory_dx_entry_t *entry,
203 uint32_t block)
204{
205 entry->block = host2uint32_t_le(block);
206}
207
208
209/**************************************************************************/
210
211/** Initialize index structure of new directory.
212 *
213 * @param dir pointer to directory i-node
214 * @return error code
215 */
216int ext4_directory_dx_init(ext4_inode_ref_t *dir)
217{
218 int rc;
219
220 // Load block 0, where will be index root located
221 uint32_t fblock;
222 rc = ext4_filesystem_get_inode_data_block_index(dir, 0, &fblock);
223 if (rc != EOK) {
224 return rc;
225 }
226
227 block_t *block;
228 rc = block_get(&block, dir->fs->device, fblock, BLOCK_FLAGS_NONE);
229 if (rc != EOK) {
230 return rc;
231 }
232
233 // Initialize pointers to data structures
234 ext4_directory_dx_root_t *root = block->data;
235 ext4_directory_dx_root_info_t *info = &(root->info);
236
237 // Initialize root info structure
238 uint8_t hash_version =
239 ext4_superblock_get_default_hash_version(dir->fs->superblock);
240
241 ext4_directory_dx_root_info_set_hash_version(info, hash_version);
242 ext4_directory_dx_root_info_set_indirect_levels(info, 0);
243 ext4_directory_dx_root_info_set_info_length(info, 8);
244
245 // Set limit and current number of entries
246 ext4_directory_dx_countlimit_t *countlimit =
247 (ext4_directory_dx_countlimit_t *)&root->entries;
248 ext4_directory_dx_countlimit_set_count(countlimit, 1);
249
250 uint32_t block_size = ext4_superblock_get_block_size(dir->fs->superblock);
251 uint32_t entry_space = block_size - 2 * sizeof(ext4_directory_dx_dot_entry_t)
252 - sizeof(ext4_directory_dx_root_info_t);
253 uint16_t root_limit = entry_space / sizeof(ext4_directory_dx_entry_t);
254 ext4_directory_dx_countlimit_set_limit(countlimit, root_limit);
255
256 // Append new block, where will be new entries inserted in the future
257 uint32_t iblock;
258 rc = ext4_filesystem_append_inode_block(dir, &fblock, &iblock);
259 if (rc != EOK) {
260 block_put(block);
261 return rc;
262 }
263
264 block_t *new_block;
265 rc = block_get(&new_block, dir->fs->device, fblock, BLOCK_FLAGS_NOREAD);
266 if (rc != EOK) {
267 block_put(block);
268 return rc;
269 }
270
271 // Fill the whole block with empty entry
272 ext4_directory_entry_ll_t *block_entry = new_block->data;
273 ext4_directory_entry_ll_set_entry_length(block_entry, block_size);
274 ext4_directory_entry_ll_set_inode(block_entry, 0);
275
276 new_block->dirty = true;
277 rc = block_put(new_block);
278 if (rc != EOK) {
279 block_put(block);
280 return rc;
281 }
282
283 // Connect new block to the only entry in index
284 ext4_directory_dx_entry_t *entry = root->entries;
285 ext4_directory_dx_entry_set_block(entry, iblock);
286
287 block->dirty = true;
288
289 rc = block_put(block);
290 if (rc != EOK) {
291 return rc;
292 }
293
294 return EOK;
295}
296
297/** Initialize hash info structure necessary for index operations.
298 *
299 * @param hinfo pointer to hinfo to be initialized
300 * @param root_block root block (number 0) of index
301 * @param sb pointer to superblock
302 * @param name_len length of name to be computed hash value from
303 * @param name name to be computed hash value from
304 * @return error code
305 */
306static int ext4_directory_hinfo_init(ext4_hash_info_t *hinfo, block_t *root_block,
307 ext4_superblock_t *sb, size_t name_len, const char *name)
308{
309
310 ext4_directory_dx_root_t *root = (ext4_directory_dx_root_t *)root_block->data;
311
312 if (root->info.hash_version != EXT4_HASH_VERSION_TEA &&
313 root->info.hash_version != EXT4_HASH_VERSION_HALF_MD4 &&
314 root->info.hash_version != EXT4_HASH_VERSION_LEGACY) {
315 return EXT4_ERR_BAD_DX_DIR;
316 }
317
318 // Check unused flags
319 if (root->info.unused_flags != 0) {
320 EXT4FS_DBG("ERR: unused_flags = \%u", root->info.unused_flags);
321 return EXT4_ERR_BAD_DX_DIR;
322 }
323
324 // Check indirect levels
325 if (root->info.indirect_levels > 1) {
326 EXT4FS_DBG("ERR: indirect_levels = \%u", root->info.indirect_levels);
327 return EXT4_ERR_BAD_DX_DIR;
328 }
329
330 // Check if node limit is correct
331 uint32_t block_size = ext4_superblock_get_block_size(sb);
332 uint32_t entry_space = block_size;
333 entry_space -= 2 * sizeof(ext4_directory_dx_dot_entry_t);
334 entry_space -= sizeof(ext4_directory_dx_root_info_t);
335 entry_space = entry_space / sizeof(ext4_directory_dx_entry_t);
336
337 uint16_t limit = ext4_directory_dx_countlimit_get_limit((ext4_directory_dx_countlimit_t *)&root->entries);
338 if (limit != entry_space) {
339 return EXT4_ERR_BAD_DX_DIR;
340 }
341
342 // Check hash version and modify if necessary
343 hinfo->hash_version = ext4_directory_dx_root_info_get_hash_version(&root->info);
344 if ((hinfo->hash_version <= EXT4_HASH_VERSION_TEA)
345 && (ext4_superblock_has_flag(sb, EXT4_SUPERBLOCK_FLAGS_UNSIGNED_HASH))) {
346 // 3 is magic from ext4 linux implementation
347 hinfo->hash_version += 3;
348 }
349
350 // Load hash seed from superblock
351 hinfo->seed = ext4_superblock_get_hash_seed(sb);
352
353 // Compute hash value of name
354 if (name) {
355 ext4_hash_string(hinfo, name_len, name);
356 }
357
358 return EOK;
359}
360
361/** Walk through index tree and load leaf with corresponding hash value.
362 *
363 * @param hinfo initialized hash info structure
364 * @param inode_ref current i-node
365 * @param root_block root block (iblock 0), where is root node located
366 * @param dx_block pointer to leaf node in dx_blocks array
367 * @param dx_blocks array with the whole path from root to leaf
368 * @return error code
369 */
370static int ext4_directory_dx_get_leaf(ext4_hash_info_t *hinfo,
371 ext4_inode_ref_t *inode_ref, block_t *root_block,
372 ext4_directory_dx_block_t **dx_block, ext4_directory_dx_block_t *dx_blocks)
373{
374 int rc;
375
376 ext4_directory_dx_block_t *tmp_dx_block = dx_blocks;
377
378 ext4_directory_dx_root_t *root = (ext4_directory_dx_root_t *)root_block->data;
379 ext4_directory_dx_entry_t *entries = (ext4_directory_dx_entry_t *)&root->entries;
380
381 uint16_t limit = ext4_directory_dx_countlimit_get_limit((ext4_directory_dx_countlimit_t *)entries);
382 uint8_t indirect_level = ext4_directory_dx_root_info_get_indirect_levels(&root->info);
383
384 block_t *tmp_block = root_block;
385 ext4_directory_dx_entry_t *p, *q, *m, *at;
386
387 // Walk through the index tree
388 while (true) {
389
390 uint16_t count = ext4_directory_dx_countlimit_get_count((ext4_directory_dx_countlimit_t *)entries);
391 if ((count == 0) || (count > limit)) {
392 return EXT4_ERR_BAD_DX_DIR;
393 }
394
395
396 // Do binary search in every node
397 p = entries + 1;
398 q = entries + count - 1;
399
400 while (p <= q) {
401 m = p + (q - p) / 2;
402 if (ext4_directory_dx_entry_get_hash(m) > hinfo->hash) {
403 q = m - 1;
404 } else {
405 p = m + 1;
406 }
407 }
408
409 at = p - 1;
410
411 // Write results
412 tmp_dx_block->block = tmp_block;
413 tmp_dx_block->entries = entries;
414 tmp_dx_block->position = at;
415
416 // Is algorithm in the leaf?
417 if (indirect_level == 0) {
418 *dx_block = tmp_dx_block;
419 return EOK;
420 }
421
422 // Goto child node
423 uint32_t next_block = ext4_directory_dx_entry_get_block(at);
424
425 indirect_level--;
426
427 uint32_t fblock;
428 rc = ext4_filesystem_get_inode_data_block_index(
429 inode_ref, next_block, &fblock);
430 if (rc != EOK) {
431 return rc;
432 }
433
434 rc = block_get(&tmp_block, inode_ref->fs->device, fblock, BLOCK_FLAGS_NONE);
435 if (rc != EOK) {
436 return rc;
437 }
438
439 entries = ((ext4_directory_dx_node_t *) tmp_block->data)->entries;
440 limit = ext4_directory_dx_countlimit_get_limit(
441 (ext4_directory_dx_countlimit_t *)entries);
442
443 uint16_t entry_space = ext4_superblock_get_block_size(inode_ref->fs->superblock)
444 - sizeof(ext4_directory_dx_dot_entry_t);
445 entry_space = entry_space / sizeof(ext4_directory_dx_entry_t);
446
447
448 if (limit != entry_space) {
449 block_put(tmp_block);
450 return EXT4_ERR_BAD_DX_DIR;
451 }
452
453 ++tmp_dx_block;
454 }
455
456 // Unreachable
457 return EOK;
458}
459
460
461/** Check if the the next block would be checked during entry search.
462 *
463 * @param inode_ref directory i-node
464 * @param hash hash value to check
465 * @param dx_block current block
466 * @param dx_blocks aray with path from root to leaf node
467 * @return error code
468 */
469static int ext4_directory_dx_next_block(ext4_inode_ref_t *inode_ref, uint32_t hash,
470 ext4_directory_dx_block_t *dx_block, ext4_directory_dx_block_t *dx_blocks)
471{
472 int rc;
473
474 uint32_t num_handles = 0;
475 ext4_directory_dx_block_t *p = dx_block;
476
477 // TODO comment
478 while (1) {
479
480 p->position++;
481 uint16_t count = ext4_directory_dx_countlimit_get_count((ext4_directory_dx_countlimit_t *)p->entries);
482
483 if (p->position < p->entries + count) {
484 break;
485 }
486
487 if (p == dx_blocks) {
488 return 0;
489 }
490
491 num_handles++;
492 p--;
493 }
494
495 // TODO comment
496 uint32_t current_hash = ext4_directory_dx_entry_get_hash(p->position);
497 if ((hash & 1) == 0) {
498 if ((current_hash & ~1) != hash) {
499 return 0;
500 }
501 }
502
503 // TODO comment
504 while (num_handles--) {
505
506 uint32_t block_idx = ext4_directory_dx_entry_get_block(p->position);
507 uint32_t block_addr;
508 rc = ext4_filesystem_get_inode_data_block_index(inode_ref, block_idx, &block_addr);
509 if (rc != EOK) {
510 return rc;
511 }
512
513 block_t *block;
514 rc = block_get(&block, inode_ref->fs->device, block_addr, BLOCK_FLAGS_NONE);
515 if (rc != EOK) {
516 return rc;
517 }
518
519 p++;
520
521 block_put(p->block);
522 p->block = block;
523 p->entries = ((ext4_directory_dx_node_t *) block->data)->entries;
524 p->position = p->entries;
525 }
526
527 return 1;
528
529}
530
531/** Try to find directory entry using directory index.
532 *
533 * @param result output value - if entry will be found,
534 * than will be passed through this parameter
535 * @param inode_ref directory i-node
536 * @param name_len length of name to be found
537 * @param name name to be found
538 * @return error code
539 */
540int ext4_directory_dx_find_entry(ext4_directory_search_result_t *result,
541 ext4_inode_ref_t *inode_ref, size_t name_len, const char *name)
542{
543 int rc;
544
545 // Load direct block 0 (index root)
546 uint32_t root_block_addr;
547 rc = ext4_filesystem_get_inode_data_block_index(inode_ref, 0, &root_block_addr);
548 if (rc != EOK) {
549 return rc;
550 }
551
552 ext4_filesystem_t *fs = inode_ref->fs;
553
554 block_t *root_block;
555 rc = block_get(&root_block, fs->device, root_block_addr, BLOCK_FLAGS_NONE);
556 if (rc != EOK) {
557 return rc;
558 }
559
560 // Initialize hash info (compute hash value)
561 ext4_hash_info_t hinfo;
562 rc = ext4_directory_hinfo_init(&hinfo, root_block, fs->superblock, name_len, name);
563 if (rc != EOK) {
564 block_put(root_block);
565 return EXT4_ERR_BAD_DX_DIR;
566 }
567
568 // Hardcoded number 2 means maximum height of index tree, specified in linux driver
569 ext4_directory_dx_block_t dx_blocks[2];
570 ext4_directory_dx_block_t *dx_block, *tmp;
571 rc = ext4_directory_dx_get_leaf(&hinfo, inode_ref, root_block, &dx_block, dx_blocks);
572 if (rc != EOK) {
573 block_put(root_block);
574 return EXT4_ERR_BAD_DX_DIR;
575 }
576
577 do {
578 // Load leaf block
579 uint32_t leaf_block_idx = ext4_directory_dx_entry_get_block(dx_block->position);
580 uint32_t leaf_block_addr;
581 rc = ext4_filesystem_get_inode_data_block_index(inode_ref, leaf_block_idx, &leaf_block_addr);
582 if (rc != EOK) {
583 goto cleanup;
584 }
585
586 block_t *leaf_block;
587 rc = block_get(&leaf_block, fs->device, leaf_block_addr, BLOCK_FLAGS_NONE);
588 if (rc != EOK) {
589 goto cleanup;
590 }
591
592 // Linear search inside block
593 ext4_directory_entry_ll_t *res_dentry;
594 rc = ext4_directory_find_in_block(leaf_block, fs->superblock, name_len, name, &res_dentry);
595
596 // Found => return it
597 if (rc == EOK) {
598 result->block = leaf_block;
599 result->dentry = res_dentry;
600 goto cleanup;
601 }
602
603 // Not found, leave untouched
604 block_put(leaf_block);
605
606 if (rc != ENOENT) {
607 goto cleanup;
608 }
609
610 // check if the next block could be checked
611 rc = ext4_directory_dx_next_block(inode_ref, hinfo.hash, dx_block, &dx_blocks[0]);
612 if (rc < 0) {
613 goto cleanup;
614 }
615
616 } while (rc == 1);
617
618 // Entry not found
619 rc = ENOENT;
620
621cleanup:
622
623 // The whole path must be released (preventing memory leak)
624 tmp = dx_blocks;
625 while (tmp <= dx_block) {
626 block_put(tmp->block);
627 ++tmp;
628 }
629 return rc;
630}
631
632/** Compare function used to pass in quicksort implementation.
633 *
634 * It can compare two entries by hash value.
635 *
636 * @param arg1 first entry
637 * @param arg2 second entry
638 * @param dummy unused parameter, can be NULL
639 * @return classic compare result (0: equal, -1: arg1 < arg2, 1: arg1 > arg2)
640 */
641static int ext4_directory_dx_entry_comparator(void *arg1, void *arg2, void *dummy)
642{
643 ext4_dx_sort_entry_t *entry1 = arg1;
644 ext4_dx_sort_entry_t *entry2 = arg2;
645
646 if (entry1->hash == entry2->hash) {
647 return 0;
648 }
649
650 if (entry1->hash < entry2->hash) {
651 return -1;
652 } else {
653 return 1;
654 }
655
656}
657
658/** Insert new index entry to block.
659 *
660 * Note that space for new entry must be checked by caller.
661 *
662 * @param index_block block where to insert new entry
663 * @param hash hash value covered by child node
664 * @param iblock logical number of child block
665 *
666 */
667static void ext4_directory_dx_insert_entry(
668 ext4_directory_dx_block_t *index_block, uint32_t hash, uint32_t iblock)
669{
670 ext4_directory_dx_entry_t *old_index_entry = index_block->position;
671 ext4_directory_dx_entry_t *new_index_entry = old_index_entry + 1;
672
673 ext4_directory_dx_countlimit_t *countlimit =
674 (ext4_directory_dx_countlimit_t *)index_block->entries;
675 uint32_t count = ext4_directory_dx_countlimit_get_count(countlimit);
676
677 ext4_directory_dx_entry_t *start_index = index_block->entries;
678 size_t bytes = (void *)(start_index + count) - (void *)(new_index_entry);
679
680 memmove(new_index_entry + 1, new_index_entry, bytes);
681
682 ext4_directory_dx_entry_set_block(new_index_entry, iblock);
683 ext4_directory_dx_entry_set_hash(new_index_entry, hash);
684
685 ext4_directory_dx_countlimit_set_count(countlimit, count + 1);
686
687 index_block->block->dirty = true;
688}
689
690/** Split directory entries to two parts preventing node overflow.
691 *
692 * @param inode_ref directory i-node
693 * @param hinfo hash info
694 * @param old_data_block block with data to be split
695 * @param index_block block where index entries are located
696 * @param new_data_block output value for newly allocated data block
697 */
698static int ext4_directory_dx_split_data(ext4_inode_ref_t *inode_ref,
699 ext4_hash_info_t *hinfo, block_t *old_data_block,
700 ext4_directory_dx_block_t *index_block, block_t **new_data_block)
701{
702 int rc = EOK;
703
704 // Allocate buffer for directory entries
705 uint32_t block_size =
706 ext4_superblock_get_block_size(inode_ref->fs->superblock);
707 void *entry_buffer = malloc(block_size);
708 if (entry_buffer == NULL) {
709 return ENOMEM;
710 }
711
712 // dot entry has the smallest size available
713 uint32_t max_entry_count = block_size / sizeof(ext4_directory_dx_dot_entry_t);
714
715 // Allocate sort entry
716 ext4_dx_sort_entry_t *sort_array = malloc(max_entry_count * sizeof(ext4_dx_sort_entry_t));
717 if (sort_array == NULL) {
718 free(entry_buffer);
719 return ENOMEM;
720 }
721
722 uint32_t idx = 0;
723 uint32_t real_size = 0;
724
725 // Initialize hinfo
726 ext4_hash_info_t tmp_hinfo;
727 memcpy(&tmp_hinfo, hinfo, sizeof(ext4_hash_info_t));
728
729 // Load all valid entries to the buffer
730 ext4_directory_entry_ll_t *dentry = old_data_block->data;
731 void *entry_buffer_ptr = entry_buffer;
732 while ((void *)dentry < old_data_block->data + block_size) {
733
734 // Read only valid entries
735 if (ext4_directory_entry_ll_get_inode(dentry) != 0) {
736
737 uint8_t len = ext4_directory_entry_ll_get_name_length(
738 inode_ref->fs->superblock, dentry);
739 ext4_hash_string(&tmp_hinfo, len, (char *)dentry->name);
740
741 uint32_t rec_len = 8 + len;
742
743 if ((rec_len % 4) != 0) {
744 rec_len += 4 - (rec_len % 4);
745 }
746
747 memcpy(entry_buffer_ptr, dentry, rec_len);
748
749 sort_array[idx].dentry = entry_buffer_ptr;
750 sort_array[idx].rec_len = rec_len;
751 sort_array[idx].hash = tmp_hinfo.hash;
752
753 entry_buffer_ptr += rec_len;
754 real_size += rec_len;
755 idx++;
756 }
757
758 dentry = (void *)dentry + ext4_directory_entry_ll_get_entry_length(dentry);
759 }
760
761 // Sort all entries
762 qsort(sort_array, idx, sizeof(ext4_dx_sort_entry_t),
763 ext4_directory_dx_entry_comparator, NULL);
764
765 // Allocate new block for store the second part of entries
766 uint32_t new_fblock;
767 uint32_t new_iblock;
768 rc = ext4_filesystem_append_inode_block(inode_ref, &new_fblock, &new_iblock);
769 if (rc != EOK) {
770 free(sort_array);
771 free(entry_buffer);
772 return rc;
773 }
774
775 // Load new block
776 block_t *new_data_block_tmp;
777 rc = block_get(&new_data_block_tmp, inode_ref->fs->device,
778 new_fblock, BLOCK_FLAGS_NOREAD);
779 if (rc != EOK) {
780 free(sort_array);
781 free(entry_buffer);
782 return rc;
783 }
784
785 // Distribute entries to two blocks (by size)
786 // Compute the half
787 uint32_t new_hash = 0;
788 uint32_t current_size = 0;
789 uint32_t mid = 0;
790 for (uint32_t i = 0; i < idx; ++i) {
791 if ((current_size + sort_array[i].rec_len) > (real_size / 2)) {
792 new_hash = sort_array[i].hash;
793 mid = i;
794 break;
795 }
796
797 current_size += sort_array[i].rec_len;
798 }
799
800 // Check hash collision
801 uint32_t continued = 0;
802 if (new_hash == sort_array[mid-1].hash) {
803 continued = 1;
804 }
805
806 uint32_t offset = 0;
807 void *ptr;
808
809 // First part - to the old block
810 for (uint32_t i = 0; i < mid; ++i) {
811 ptr = old_data_block->data + offset;
812 memcpy(ptr, sort_array[i].dentry, sort_array[i].rec_len);
813
814 ext4_directory_entry_ll_t *tmp = ptr;
815 if (i < (mid - 1)) {
816 ext4_directory_entry_ll_set_entry_length(tmp, sort_array[i].rec_len);
817 } else {
818 ext4_directory_entry_ll_set_entry_length(tmp, block_size - offset);
819 }
820
821 offset += sort_array[i].rec_len;
822 }
823
824 // Second part - to the new block
825 offset = 0;
826 for (uint32_t i = mid; i < idx; ++i) {
827 ptr = new_data_block_tmp->data + offset;
828 memcpy(ptr, sort_array[i].dentry, sort_array[i].rec_len);
829
830 ext4_directory_entry_ll_t *tmp = ptr;
831 if (i < (idx - 1)) {
832 ext4_directory_entry_ll_set_entry_length(tmp, sort_array[i].rec_len);
833 } else {
834 ext4_directory_entry_ll_set_entry_length(tmp, block_size - offset);
835 }
836
837 offset += sort_array[i].rec_len;
838 }
839
840 // Do some steps to finish operation
841 old_data_block->dirty = true;
842 new_data_block_tmp->dirty = true;
843
844 free(sort_array);
845 free(entry_buffer);
846
847 ext4_directory_dx_insert_entry(index_block, new_hash + continued, new_iblock);
848
849 *new_data_block = new_data_block_tmp;
850
851 return EOK;
852}
853
854/** TODO start comments from here
855 *
856 */
857static int ext4_directory_dx_split_index(ext4_filesystem_t *fs,
858 ext4_inode_ref_t *inode_ref, ext4_directory_dx_block_t *dx_blocks,
859 ext4_directory_dx_block_t *dx_block)
860{
861 int rc;
862
863 ext4_directory_dx_entry_t *entries;
864 if (dx_block == dx_blocks) {
865 entries = ((ext4_directory_dx_root_t *) dx_block->block->data)->entries;
866 } else {
867 entries = ((ext4_directory_dx_node_t *) dx_block->block->data)->entries;
868 }
869
870 ext4_directory_dx_countlimit_t *countlimit =
871 (ext4_directory_dx_countlimit_t *)entries;
872 uint16_t leaf_limit = ext4_directory_dx_countlimit_get_limit(countlimit);
873 uint16_t leaf_count = ext4_directory_dx_countlimit_get_count(countlimit);
874
875 // Check if is necessary to split index block
876 if (leaf_limit == leaf_count) {
877 EXT4FS_DBG("need to split index block !!!");
878
879 unsigned int levels = dx_block - dx_blocks;
880
881 ext4_directory_dx_entry_t *root_entries =
882 ((ext4_directory_dx_root_t *)dx_blocks[0].block->data)->entries;
883
884 ext4_directory_dx_countlimit_t *root_countlimit =
885 (ext4_directory_dx_countlimit_t *)root_entries;
886 uint16_t root_limit =
887 ext4_directory_dx_countlimit_get_limit(root_countlimit);
888 uint16_t root_count =
889 ext4_directory_dx_countlimit_get_count(root_countlimit);
890
891 if ((levels > 0) && (root_limit == root_count)) {
892 EXT4FS_DBG("Directory index is full");
893 return ENOSPC;
894 }
895
896 uint32_t new_fblock;
897 uint32_t new_iblock;
898 rc = ext4_filesystem_append_inode_block(
899 inode_ref, &new_fblock, &new_iblock);
900 if (rc != EOK) {
901 return rc;
902 }
903
904 // New block allocated
905 block_t * new_block;
906 rc = block_get(&new_block, fs->device, new_fblock, BLOCK_FLAGS_NOREAD);
907 if (rc != EOK) {
908 return rc;
909 }
910
911 ext4_directory_dx_node_t *new_node = new_block->data;
912 ext4_directory_dx_entry_t *new_entries = new_node->entries;
913
914 uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
915
916 if (levels > 0) {
917 EXT4FS_DBG("split index leaf node");
918 uint32_t count_left = leaf_count / 2;
919 uint32_t count_right = leaf_count - count_left;
920 uint32_t hash_right =
921 ext4_directory_dx_entry_get_hash(entries + count_left);
922
923 memcpy((void *) new_entries, (void *) (entries + count_left),
924 count_right * sizeof(ext4_directory_dx_entry_t));
925
926 ext4_directory_dx_countlimit_t *left_countlimit =
927 (ext4_directory_dx_countlimit_t *)entries;
928 ext4_directory_dx_countlimit_t *right_countlimit =
929 (ext4_directory_dx_countlimit_t *)new_entries;
930
931 ext4_directory_dx_countlimit_set_count(left_countlimit, count_left);
932 ext4_directory_dx_countlimit_set_count(right_countlimit, count_right);
933
934 uint32_t entry_space = block_size - sizeof(ext4_fake_directory_entry_t);
935 uint32_t node_limit = entry_space / sizeof(ext4_directory_dx_entry_t);
936 ext4_directory_dx_countlimit_set_limit(right_countlimit, node_limit);
937
938 // Which index block is target for new entry
939 uint32_t position_index = (dx_block->position - dx_block->entries);
940 if (position_index >= count_left) {
941
942 dx_block->block->dirty = true;
943
944 block_t *block_tmp = dx_block->block;
945 dx_block->block = new_block;
946 dx_block->position = new_entries + position_index - count_left;
947 dx_block->entries = new_entries;
948
949 new_block = block_tmp;
950
951 }
952
953 ext4_directory_dx_insert_entry(dx_blocks, hash_right, new_iblock);
954
955 return block_put(new_block);
956
957 } else {
958 EXT4FS_DBG("create second level");
959
960 memcpy((void *) new_entries, (void *) entries,
961 leaf_count * sizeof(ext4_directory_dx_entry_t));
962
963 ext4_directory_dx_countlimit_t *new_countlimit =
964 (ext4_directory_dx_countlimit_t *)new_entries;
965
966 uint32_t entry_space = block_size - sizeof(ext4_fake_directory_entry_t);
967 uint32_t node_limit = entry_space / sizeof(ext4_directory_dx_entry_t);
968 ext4_directory_dx_countlimit_set_limit(new_countlimit, node_limit);
969
970 // Set values in root node
971 ext4_directory_dx_countlimit_t *new_root_countlimit =
972 (ext4_directory_dx_countlimit_t *)entries;
973
974 ext4_directory_dx_countlimit_set_count(new_root_countlimit, 1);
975 ext4_directory_dx_entry_set_block(entries, new_iblock);
976
977 ((ext4_directory_dx_root_t *)dx_blocks[0].block->data)->info.indirect_levels = 1;
978
979 /* Add new access path frame */
980 dx_block = dx_blocks + 1;
981 dx_block->position = dx_block->position - entries + new_entries;
982 dx_block->entries = new_entries;
983 dx_block->block = new_block;
984 }
985
986 }
987
988 return EOK;
989}
990
991int ext4_directory_dx_add_entry(ext4_inode_ref_t *parent,
992 ext4_inode_ref_t *child, const char *name)
993{
994 int rc = EOK;
995 int rc2 = EOK;
996
997 // get direct block 0 (index root)
998 uint32_t root_block_addr;
999 rc = ext4_filesystem_get_inode_data_block_index(parent, 0, &root_block_addr);
1000 if (rc != EOK) {
1001 return rc;
1002 }
1003
1004 ext4_filesystem_t *fs = parent->fs;
1005
1006 block_t *root_block;
1007 rc = block_get(&root_block, fs->device, root_block_addr, BLOCK_FLAGS_NONE);
1008 if (rc != EOK) {
1009 return rc;
1010 }
1011
1012 uint32_t name_len = strlen(name);
1013 ext4_hash_info_t hinfo;
1014 rc = ext4_directory_hinfo_init(&hinfo, root_block, fs->superblock, name_len, name);
1015 if (rc != EOK) {
1016 block_put(root_block);
1017 EXT4FS_DBG("hinfo initialization error");
1018 return EXT4_ERR_BAD_DX_DIR;
1019 }
1020
1021 // Hardcoded number 2 means maximum height of index tree !!!
1022 ext4_directory_dx_block_t dx_blocks[2];
1023 ext4_directory_dx_block_t *dx_block, *dx_it;
1024 rc = ext4_directory_dx_get_leaf(&hinfo, parent, root_block, &dx_block, dx_blocks);
1025 if (rc != EOK) {
1026 EXT4FS_DBG("get leaf error");
1027 rc = EXT4_ERR_BAD_DX_DIR;
1028 goto release_index;
1029 }
1030
1031
1032 // Try to insert to existing data block
1033 uint32_t leaf_block_idx = ext4_directory_dx_entry_get_block(dx_block->position);
1034 uint32_t leaf_block_addr;
1035 rc = ext4_filesystem_get_inode_data_block_index(parent, leaf_block_idx, &leaf_block_addr);
1036 if (rc != EOK) {
1037 goto release_index;
1038 }
1039
1040
1041 block_t *target_block;
1042 rc = block_get(&target_block, fs->device, leaf_block_addr, BLOCK_FLAGS_NONE);
1043 if (rc != EOK) {
1044 goto release_index;
1045 }
1046
1047
1048 rc = ext4_directory_try_insert_entry(fs->superblock, target_block, child, name, name_len);
1049 if (rc == EOK) {
1050 goto release_target_index;
1051 }
1052
1053 EXT4FS_DBG("no free space found");
1054
1055 rc = ext4_directory_dx_split_index(fs, parent, dx_blocks, dx_block);
1056 if (rc != EOK) {
1057 goto release_target_index;
1058 }
1059
1060 block_t *new_block = NULL;
1061 rc = ext4_directory_dx_split_data(parent, &hinfo, target_block, dx_block, &new_block);
1062 if (rc != EOK) {
1063 rc2 = rc;
1064 goto release_target_index;
1065 }
1066
1067 // Where to save new entry
1068 uint32_t new_block_hash = ext4_directory_dx_entry_get_hash(dx_block->position + 1);
1069 if (hinfo.hash >= new_block_hash) {
1070 rc = ext4_directory_try_insert_entry(fs->superblock, new_block, child, name, name_len);
1071 } else {
1072 rc = ext4_directory_try_insert_entry(fs->superblock, target_block, child, name, name_len);
1073 }
1074
1075
1076
1077
1078 rc = block_put(new_block);
1079 if (rc != EOK) {
1080 EXT4FS_DBG("error writing new block");
1081 return rc;
1082 }
1083
1084release_target_index:
1085
1086 rc2 = rc;
1087
1088 rc = block_put(target_block);
1089 if (rc != EOK) {
1090 EXT4FS_DBG("error writing target block");
1091 return rc;
1092 }
1093
1094release_index:
1095
1096 if (rc != EOK) {
1097 rc2 = rc;
1098 }
1099
1100 dx_it = dx_blocks;
1101
1102 while (dx_it <= dx_block) {
1103 rc = block_put(dx_it->block);
1104 if (rc != EOK) {
1105 EXT4FS_DBG("error writing index block");
1106 return rc;
1107 }
1108 dx_it++;
1109 }
1110
1111 return rc2;
1112}
1113
1114
1115/**
1116 * @}
1117 */
Note: See TracBrowser for help on using the repository browser.