source: mainline/uspace/lib/ext4/libext4_directory_index.c@ 8eff2b0

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

Most of comments modified by current coding style

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