source: mainline/uspace/lib/ext4/src/directory_index.c@ ebf5ced6

Last change on this file since ebf5ced6 was 3bacee1, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Make ccheck-fix again and commit more good files.

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