source: mainline/uspace/lib/ext4/libext4_directory_index.c@ 81a7858

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

completed comments for directory index

  • Property mode set to 100644
File size: 31.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 // Try to find data block with next bunch of entries
478 while (1) {
479
480 p->position++;
481 uint16_t count = ext4_directory_dx_countlimit_get_count(
482 (ext4_directory_dx_countlimit_t *)p->entries);
483
484 if (p->position < p->entries + count) {
485 break;
486 }
487
488 if (p == dx_blocks) {
489 return 0;
490 }
491
492 num_handles++;
493 p--;
494 }
495
496 // Check hash collision (if not occured - no next block cannot be used)
497 uint32_t current_hash = ext4_directory_dx_entry_get_hash(p->position);
498 if ((hash & 1) == 0) {
499 if ((current_hash & ~1) != hash) {
500 return 0;
501 }
502 }
503
504 // Fill new path
505 while (num_handles--) {
506
507 uint32_t block_idx = ext4_directory_dx_entry_get_block(p->position);
508 uint32_t block_addr;
509 rc = ext4_filesystem_get_inode_data_block_index(inode_ref, block_idx, &block_addr);
510 if (rc != EOK) {
511 return rc;
512 }
513
514 block_t *block;
515 rc = block_get(&block, inode_ref->fs->device, block_addr, BLOCK_FLAGS_NONE);
516 if (rc != EOK) {
517 return rc;
518 }
519
520 p++;
521
522 // Don't forget to put old block (prevent memory leak)
523 block_put(p->block);
524
525 p->block = block;
526 p->entries = ((ext4_directory_dx_node_t *) block->data)->entries;
527 p->position = p->entries;
528 }
529
530 return 1;
531
532}
533
534/** Try to find directory entry using directory index.
535 *
536 * @param result output value - if entry will be found,
537 * than will be passed through this parameter
538 * @param inode_ref directory i-node
539 * @param name_len length of name to be found
540 * @param name name to be found
541 * @return error code
542 */
543int ext4_directory_dx_find_entry(ext4_directory_search_result_t *result,
544 ext4_inode_ref_t *inode_ref, size_t name_len, const char *name)
545{
546 int rc;
547
548 // Load direct block 0 (index root)
549 uint32_t root_block_addr;
550 rc = ext4_filesystem_get_inode_data_block_index(inode_ref, 0, &root_block_addr);
551 if (rc != EOK) {
552 return rc;
553 }
554
555 ext4_filesystem_t *fs = inode_ref->fs;
556
557 block_t *root_block;
558 rc = block_get(&root_block, fs->device, root_block_addr, BLOCK_FLAGS_NONE);
559 if (rc != EOK) {
560 return rc;
561 }
562
563 // Initialize hash info (compute hash value)
564 ext4_hash_info_t hinfo;
565 rc = ext4_directory_hinfo_init(&hinfo, root_block, fs->superblock, name_len, name);
566 if (rc != EOK) {
567 block_put(root_block);
568 return EXT4_ERR_BAD_DX_DIR;
569 }
570
571 // Hardcoded number 2 means maximum height of index tree, specified in linux driver
572 ext4_directory_dx_block_t dx_blocks[2];
573 ext4_directory_dx_block_t *dx_block, *tmp;
574 rc = ext4_directory_dx_get_leaf(&hinfo, inode_ref, root_block, &dx_block, dx_blocks);
575 if (rc != EOK) {
576 block_put(root_block);
577 return EXT4_ERR_BAD_DX_DIR;
578 }
579
580 do {
581 // Load leaf block
582 uint32_t leaf_block_idx = ext4_directory_dx_entry_get_block(dx_block->position);
583 uint32_t leaf_block_addr;
584 rc = ext4_filesystem_get_inode_data_block_index(inode_ref, leaf_block_idx, &leaf_block_addr);
585 if (rc != EOK) {
586 goto cleanup;
587 }
588
589 block_t *leaf_block;
590 rc = block_get(&leaf_block, fs->device, leaf_block_addr, BLOCK_FLAGS_NONE);
591 if (rc != EOK) {
592 goto cleanup;
593 }
594
595 // Linear search inside block
596 ext4_directory_entry_ll_t *res_dentry;
597 rc = ext4_directory_find_in_block(leaf_block, fs->superblock, name_len, name, &res_dentry);
598
599 // Found => return it
600 if (rc == EOK) {
601 result->block = leaf_block;
602 result->dentry = res_dentry;
603 goto cleanup;
604 }
605
606 // Not found, leave untouched
607 block_put(leaf_block);
608
609 if (rc != ENOENT) {
610 goto cleanup;
611 }
612
613 // check if the next block could be checked
614 rc = ext4_directory_dx_next_block(inode_ref, hinfo.hash, dx_block, &dx_blocks[0]);
615 if (rc < 0) {
616 goto cleanup;
617 }
618
619 } while (rc == 1);
620
621 // Entry not found
622 rc = ENOENT;
623
624cleanup:
625
626 // The whole path must be released (preventing memory leak)
627 tmp = dx_blocks;
628 while (tmp <= dx_block) {
629 block_put(tmp->block);
630 ++tmp;
631 }
632 return rc;
633}
634
635/** Compare function used to pass in quicksort implementation.
636 *
637 * It can compare two entries by hash value.
638 *
639 * @param arg1 first entry
640 * @param arg2 second entry
641 * @param dummy unused parameter, can be NULL
642 * @return classic compare result (0: equal, -1: arg1 < arg2, 1: arg1 > arg2)
643 */
644static int ext4_directory_dx_entry_comparator(void *arg1, void *arg2, void *dummy)
645{
646 ext4_dx_sort_entry_t *entry1 = arg1;
647 ext4_dx_sort_entry_t *entry2 = arg2;
648
649 if (entry1->hash == entry2->hash) {
650 return 0;
651 }
652
653 if (entry1->hash < entry2->hash) {
654 return -1;
655 } else {
656 return 1;
657 }
658
659}
660
661/** Insert new index entry to block.
662 *
663 * Note that space for new entry must be checked by caller.
664 *
665 * @param index_block block where to insert new entry
666 * @param hash hash value covered by child node
667 * @param iblock logical number of child block
668 *
669 */
670static void ext4_directory_dx_insert_entry(
671 ext4_directory_dx_block_t *index_block, uint32_t hash, uint32_t iblock)
672{
673 ext4_directory_dx_entry_t *old_index_entry = index_block->position;
674 ext4_directory_dx_entry_t *new_index_entry = old_index_entry + 1;
675
676 ext4_directory_dx_countlimit_t *countlimit =
677 (ext4_directory_dx_countlimit_t *)index_block->entries;
678 uint32_t count = ext4_directory_dx_countlimit_get_count(countlimit);
679
680 ext4_directory_dx_entry_t *start_index = index_block->entries;
681 size_t bytes = (void *)(start_index + count) - (void *)(new_index_entry);
682
683 memmove(new_index_entry + 1, new_index_entry, bytes);
684
685 ext4_directory_dx_entry_set_block(new_index_entry, iblock);
686 ext4_directory_dx_entry_set_hash(new_index_entry, hash);
687
688 ext4_directory_dx_countlimit_set_count(countlimit, count + 1);
689
690 index_block->block->dirty = true;
691}
692
693/** Split directory entries to two parts preventing node overflow.
694 *
695 * @param inode_ref directory i-node
696 * @param hinfo hash info
697 * @param old_data_block block with data to be split
698 * @param index_block block where index entries are located
699 * @param new_data_block output value for newly allocated data block
700 */
701static int ext4_directory_dx_split_data(ext4_inode_ref_t *inode_ref,
702 ext4_hash_info_t *hinfo, block_t *old_data_block,
703 ext4_directory_dx_block_t *index_block, block_t **new_data_block)
704{
705 int rc = EOK;
706
707 // Allocate buffer for directory entries
708 uint32_t block_size =
709 ext4_superblock_get_block_size(inode_ref->fs->superblock);
710 void *entry_buffer = malloc(block_size);
711 if (entry_buffer == NULL) {
712 return ENOMEM;
713 }
714
715 // dot entry has the smallest size available
716 uint32_t max_entry_count = block_size / sizeof(ext4_directory_dx_dot_entry_t);
717
718 // Allocate sort entry
719 ext4_dx_sort_entry_t *sort_array = malloc(max_entry_count * sizeof(ext4_dx_sort_entry_t));
720 if (sort_array == NULL) {
721 free(entry_buffer);
722 return ENOMEM;
723 }
724
725 uint32_t idx = 0;
726 uint32_t real_size = 0;
727
728 // Initialize hinfo
729 ext4_hash_info_t tmp_hinfo;
730 memcpy(&tmp_hinfo, hinfo, sizeof(ext4_hash_info_t));
731
732 // Load all valid entries to the buffer
733 ext4_directory_entry_ll_t *dentry = old_data_block->data;
734 void *entry_buffer_ptr = entry_buffer;
735 while ((void *)dentry < old_data_block->data + block_size) {
736
737 // Read only valid entries
738 if (ext4_directory_entry_ll_get_inode(dentry) != 0) {
739
740 uint8_t len = ext4_directory_entry_ll_get_name_length(
741 inode_ref->fs->superblock, dentry);
742 ext4_hash_string(&tmp_hinfo, len, (char *)dentry->name);
743
744 uint32_t rec_len = 8 + len;
745
746 if ((rec_len % 4) != 0) {
747 rec_len += 4 - (rec_len % 4);
748 }
749
750 memcpy(entry_buffer_ptr, dentry, rec_len);
751
752 sort_array[idx].dentry = entry_buffer_ptr;
753 sort_array[idx].rec_len = rec_len;
754 sort_array[idx].hash = tmp_hinfo.hash;
755
756 entry_buffer_ptr += rec_len;
757 real_size += rec_len;
758 idx++;
759 }
760
761 dentry = (void *)dentry + ext4_directory_entry_ll_get_entry_length(dentry);
762 }
763
764 // Sort all entries
765 qsort(sort_array, idx, sizeof(ext4_dx_sort_entry_t),
766 ext4_directory_dx_entry_comparator, NULL);
767
768 // Allocate new block for store the second part of entries
769 uint32_t new_fblock;
770 uint32_t new_iblock;
771 rc = ext4_filesystem_append_inode_block(inode_ref, &new_fblock, &new_iblock);
772 if (rc != EOK) {
773 free(sort_array);
774 free(entry_buffer);
775 return rc;
776 }
777
778 // Load new block
779 block_t *new_data_block_tmp;
780 rc = block_get(&new_data_block_tmp, inode_ref->fs->device,
781 new_fblock, BLOCK_FLAGS_NOREAD);
782 if (rc != EOK) {
783 free(sort_array);
784 free(entry_buffer);
785 return rc;
786 }
787
788 // Distribute entries to two blocks (by size)
789 // Compute the half
790 uint32_t new_hash = 0;
791 uint32_t current_size = 0;
792 uint32_t mid = 0;
793 for (uint32_t i = 0; i < idx; ++i) {
794 if ((current_size + sort_array[i].rec_len) > (real_size / 2)) {
795 new_hash = sort_array[i].hash;
796 mid = i;
797 break;
798 }
799
800 current_size += sort_array[i].rec_len;
801 }
802
803 // Check hash collision
804 uint32_t continued = 0;
805 if (new_hash == sort_array[mid-1].hash) {
806 continued = 1;
807 }
808
809 uint32_t offset = 0;
810 void *ptr;
811
812 // First part - to the old block
813 for (uint32_t i = 0; i < mid; ++i) {
814 ptr = old_data_block->data + offset;
815 memcpy(ptr, sort_array[i].dentry, sort_array[i].rec_len);
816
817 ext4_directory_entry_ll_t *tmp = ptr;
818 if (i < (mid - 1)) {
819 ext4_directory_entry_ll_set_entry_length(tmp, sort_array[i].rec_len);
820 } else {
821 ext4_directory_entry_ll_set_entry_length(tmp, block_size - offset);
822 }
823
824 offset += sort_array[i].rec_len;
825 }
826
827 // Second part - to the new block
828 offset = 0;
829 for (uint32_t i = mid; i < idx; ++i) {
830 ptr = new_data_block_tmp->data + offset;
831 memcpy(ptr, sort_array[i].dentry, sort_array[i].rec_len);
832
833 ext4_directory_entry_ll_t *tmp = ptr;
834 if (i < (idx - 1)) {
835 ext4_directory_entry_ll_set_entry_length(tmp, sort_array[i].rec_len);
836 } else {
837 ext4_directory_entry_ll_set_entry_length(tmp, block_size - offset);
838 }
839
840 offset += sort_array[i].rec_len;
841 }
842
843 // Do some steps to finish operation
844 old_data_block->dirty = true;
845 new_data_block_tmp->dirty = true;
846
847 free(sort_array);
848 free(entry_buffer);
849
850 ext4_directory_dx_insert_entry(index_block, new_hash + continued, new_iblock);
851
852 *new_data_block = new_data_block_tmp;
853
854 return EOK;
855}
856
857/** Split index node and maybe some parent nodes in the tree hierarchy.
858 *
859 * @param inode_ref directory i-node
860 * @param dx_blocks array with path from root to leaf node
861 * @param dx_block leaf block to be split if needed
862 * @return error code
863 */
864static int ext4_directory_dx_split_index(ext4_inode_ref_t *inode_ref,
865 ext4_directory_dx_block_t *dx_blocks, ext4_directory_dx_block_t *dx_block)
866{
867 int rc;
868
869 ext4_directory_dx_entry_t *entries;
870 if (dx_block == dx_blocks) {
871 entries = ((ext4_directory_dx_root_t *) dx_block->block->data)->entries;
872 } else {
873 entries = ((ext4_directory_dx_node_t *) dx_block->block->data)->entries;
874 }
875
876 ext4_directory_dx_countlimit_t *countlimit =
877 (ext4_directory_dx_countlimit_t *)entries;
878 uint16_t leaf_limit = ext4_directory_dx_countlimit_get_limit(countlimit);
879 uint16_t leaf_count = ext4_directory_dx_countlimit_get_count(countlimit);
880
881 // Check if is necessary to split index block
882 if (leaf_limit == leaf_count) {
883
884 unsigned int levels = dx_block - dx_blocks;
885
886 ext4_directory_dx_entry_t *root_entries =
887 ((ext4_directory_dx_root_t *)dx_blocks[0].block->data)->entries;
888
889 ext4_directory_dx_countlimit_t *root_countlimit =
890 (ext4_directory_dx_countlimit_t *)root_entries;
891 uint16_t root_limit =
892 ext4_directory_dx_countlimit_get_limit(root_countlimit);
893 uint16_t root_count =
894 ext4_directory_dx_countlimit_get_count(root_countlimit);
895
896 // Linux limitation
897 if ((levels > 0) && (root_limit == root_count)) {
898 EXT4FS_DBG("Directory index is full");
899 return ENOSPC;
900 }
901
902 // Add new block to directory
903 uint32_t new_fblock;
904 uint32_t new_iblock;
905 rc = ext4_filesystem_append_inode_block(
906 inode_ref, &new_fblock, &new_iblock);
907 if (rc != EOK) {
908 return rc;
909 }
910
911 // load new block
912 block_t * new_block;
913 rc = block_get(&new_block, inode_ref->fs->device,
914 new_fblock, BLOCK_FLAGS_NOREAD);
915 if (rc != EOK) {
916 return rc;
917 }
918
919 ext4_directory_dx_node_t *new_node = new_block->data;
920 ext4_directory_dx_entry_t *new_entries = new_node->entries;
921
922 uint32_t block_size = ext4_superblock_get_block_size(
923 inode_ref->fs->superblock);
924
925 // Split leaf node
926 if (levels > 0) {
927
928 uint32_t count_left = leaf_count / 2;
929 uint32_t count_right = leaf_count - count_left;
930 uint32_t hash_right =
931 ext4_directory_dx_entry_get_hash(entries + count_left);
932
933 // Copy data to new node
934 memcpy((void *) new_entries, (void *) (entries + count_left),
935 count_right * sizeof(ext4_directory_dx_entry_t));
936
937 // Initialize new node
938 ext4_directory_dx_countlimit_t *left_countlimit =
939 (ext4_directory_dx_countlimit_t *)entries;
940 ext4_directory_dx_countlimit_t *right_countlimit =
941 (ext4_directory_dx_countlimit_t *)new_entries;
942
943 ext4_directory_dx_countlimit_set_count(left_countlimit, count_left);
944 ext4_directory_dx_countlimit_set_count(right_countlimit, count_right);
945
946 uint32_t entry_space = block_size - sizeof(ext4_fake_directory_entry_t);
947 uint32_t node_limit = entry_space / sizeof(ext4_directory_dx_entry_t);
948 ext4_directory_dx_countlimit_set_limit(right_countlimit, node_limit);
949
950 // Which index block is target for new entry
951 uint32_t position_index = (dx_block->position - dx_block->entries);
952 if (position_index >= count_left) {
953
954 dx_block->block->dirty = true;
955
956 block_t *block_tmp = dx_block->block;
957 dx_block->block = new_block;
958 dx_block->position = new_entries + position_index - count_left;
959 dx_block->entries = new_entries;
960
961 new_block = block_tmp;
962
963 }
964
965 // Finally insert new entry
966 ext4_directory_dx_insert_entry(dx_blocks, hash_right, new_iblock);
967
968 return block_put(new_block);
969
970 } else {
971
972 // Create second level index
973
974 // Copy data from root to child block
975 memcpy((void *) new_entries, (void *) entries,
976 leaf_count * sizeof(ext4_directory_dx_entry_t));
977
978 ext4_directory_dx_countlimit_t *new_countlimit =
979 (ext4_directory_dx_countlimit_t *)new_entries;
980
981 uint32_t entry_space = block_size - sizeof(ext4_fake_directory_entry_t);
982 uint32_t node_limit = entry_space / sizeof(ext4_directory_dx_entry_t);
983 ext4_directory_dx_countlimit_set_limit(new_countlimit, node_limit);
984
985 // Set values in root node
986 ext4_directory_dx_countlimit_t *new_root_countlimit =
987 (ext4_directory_dx_countlimit_t *)entries;
988
989 ext4_directory_dx_countlimit_set_count(new_root_countlimit, 1);
990 ext4_directory_dx_entry_set_block(entries, new_iblock);
991
992 ((ext4_directory_dx_root_t *)dx_blocks[0].block->data)->info.indirect_levels = 1;
993
994 // Add new entry to the path
995 dx_block = dx_blocks + 1;
996 dx_block->position = dx_block->position - entries + new_entries;
997 dx_block->entries = new_entries;
998 dx_block->block = new_block;
999 }
1000
1001 }
1002
1003 return EOK;
1004}
1005
1006/** Add new entry to indexed directory
1007 *
1008 * @param parent directory i-node
1009 * @param child i-node to be referenced from directory entry
1010 * @param name name of new directory entry
1011 * @return error code
1012 */
1013int ext4_directory_dx_add_entry(ext4_inode_ref_t *parent,
1014 ext4_inode_ref_t *child, const char *name)
1015{
1016 int rc = EOK;
1017 int rc2 = EOK;
1018
1019 // get direct block 0 (index root)
1020 uint32_t root_block_addr;
1021 rc = ext4_filesystem_get_inode_data_block_index(parent, 0, &root_block_addr);
1022 if (rc != EOK) {
1023 return rc;
1024 }
1025
1026 ext4_filesystem_t *fs = parent->fs;
1027
1028 block_t *root_block;
1029 rc = block_get(&root_block, fs->device, root_block_addr, BLOCK_FLAGS_NONE);
1030 if (rc != EOK) {
1031 return rc;
1032 }
1033
1034 // Initialize hinfo structure (mainly compute hash)
1035 uint32_t name_len = strlen(name);
1036 ext4_hash_info_t hinfo;
1037 rc = ext4_directory_hinfo_init(&hinfo, root_block, fs->superblock, name_len, name);
1038 if (rc != EOK) {
1039 block_put(root_block);
1040 EXT4FS_DBG("hinfo initialization error");
1041 return EXT4_ERR_BAD_DX_DIR;
1042 }
1043
1044 // Hardcoded number 2 means maximum height of index tree defined in linux
1045 ext4_directory_dx_block_t dx_blocks[2];
1046 ext4_directory_dx_block_t *dx_block, *dx_it;
1047 rc = ext4_directory_dx_get_leaf(&hinfo, parent, root_block, &dx_block, dx_blocks);
1048 if (rc != EOK) {
1049 EXT4FS_DBG("get leaf error");
1050 rc = EXT4_ERR_BAD_DX_DIR;
1051 goto release_index;
1052 }
1053
1054
1055 // Try to insert to existing data block
1056 uint32_t leaf_block_idx = ext4_directory_dx_entry_get_block(dx_block->position);
1057 uint32_t leaf_block_addr;
1058 rc = ext4_filesystem_get_inode_data_block_index(parent, leaf_block_idx, &leaf_block_addr);
1059 if (rc != EOK) {
1060 goto release_index;
1061 }
1062
1063
1064 block_t *target_block;
1065 rc = block_get(&target_block, fs->device, leaf_block_addr, BLOCK_FLAGS_NONE);
1066 if (rc != EOK) {
1067 goto release_index;
1068 }
1069
1070 // Check if insert operation passed
1071 rc = ext4_directory_try_insert_entry(fs->superblock, target_block, child, name, name_len);
1072 if (rc == EOK) {
1073 goto release_target_index;
1074 }
1075
1076 // Check if there is needed to split index node
1077 // (and recursively also parent nodes)
1078 rc = ext4_directory_dx_split_index(parent, dx_blocks, dx_block);
1079 if (rc != EOK) {
1080 goto release_target_index;
1081 }
1082
1083 // Split entries to two blocks (includes sorting by hash value)
1084 block_t *new_block = NULL;
1085 rc = ext4_directory_dx_split_data(parent, &hinfo, target_block, dx_block, &new_block);
1086 if (rc != EOK) {
1087 rc2 = rc;
1088 goto release_target_index;
1089 }
1090
1091 // Where to save new entry
1092 uint32_t new_block_hash = ext4_directory_dx_entry_get_hash(dx_block->position + 1);
1093 if (hinfo.hash >= new_block_hash) {
1094 rc = ext4_directory_try_insert_entry(fs->superblock, new_block, child, name, name_len);
1095 } else {
1096 rc = ext4_directory_try_insert_entry(fs->superblock, target_block, child, name, name_len);
1097 }
1098
1099 // Cleanup
1100 rc = block_put(new_block);
1101 if (rc != EOK) {
1102 EXT4FS_DBG("error writing new block");
1103 return rc;
1104 }
1105
1106 // Cleanup operations
1107
1108release_target_index:
1109
1110 rc2 = rc;
1111
1112 rc = block_put(target_block);
1113 if (rc != EOK) {
1114 EXT4FS_DBG("error writing target block");
1115 return rc;
1116 }
1117
1118release_index:
1119
1120 if (rc != EOK) {
1121 rc2 = rc;
1122 }
1123
1124 dx_it = dx_blocks;
1125
1126 while (dx_it <= dx_block) {
1127 rc = block_put(dx_it->block);
1128 if (rc != EOK) {
1129 EXT4FS_DBG("error writing index block");
1130 return rc;
1131 }
1132 dx_it++;
1133 }
1134
1135 return rc2;
1136}
1137
1138
1139/**
1140 * @}
1141 */
Note: See TracBrowser for help on using the repository browser.