source: mainline/uspace/lib/ext4/libext4_directory_index.c@ 1f63d9d

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 1f63d9d was e5a1ace3, checked in by Maurizio Lombardi <m.lombardi85@…>, 12 years ago

libext4: do not ignore error codes

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