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

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

Debug messages removed

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