source: mainline/uspace/lib/ext4/libext4_directory_index.c@ 9fc72fb3

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

completed comments for directory index

  • Property mode set to 100644
File size: 31.3 KB
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
[e6f6260]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
[e6f6260]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
[e6f6260]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
[e6f6260]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
[e6f6260]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
[e6f6260]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
[e6f6260]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
318 // Check unused flags
319 if (root->info.unused_flags != 0) {
320 EXT4FS_DBG("ERR: unused_flags = \%u", root->info.unused_flags);
321 return EXT4_ERR_BAD_DX_DIR;
322 }
323
324 // Check indirect levels
325 if (root->info.indirect_levels > 1) {
326 EXT4FS_DBG("ERR: indirect_levels = \%u", root->info.indirect_levels);
327 return EXT4_ERR_BAD_DX_DIR;
328 }
329
[e6f6260]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
[e6f6260]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))) {
346 // 3 is magic from ext4 linux implementation
347 hinfo->hash_version += 3;
348 }
349
[e6f6260]350 // Load hash seed from superblock
[0dc91833]351 hinfo->seed = ext4_superblock_get_hash_seed(sb);
352
[e6f6260]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
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
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
[e6f6260]411 // Write results
[0dc91833]412 tmp_dx_block->block = tmp_block;
413 tmp_dx_block->entries = entries;
414 tmp_dx_block->position = at;
415
[e6f6260]416 // Is algorithm in the leaf?
[0dc91833]417 if (indirect_level == 0) {
418 *dx_block = tmp_dx_block;
419 return EOK;
420 }
421
[e6f6260]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
456 // Unreachable
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
[dfac604]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
[dfac604]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
[dfac604]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
[dfac604]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
[e6f6260]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
[e6f6260]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
[e6f6260]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 {
[e6f6260]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
[e6f6260]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
[0dc91833]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
[e63ce679]606 // Not found, leave untouched
[0dc91833]607 block_put(leaf_block);
608
[e63ce679]609 if (rc != ENOENT) {
610 goto cleanup;
[0dc91833]611 }
612
[e6f6260]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
[e6f6260]621 // Entry not found
[e8d054a]622 rc = ENOENT;
[e63ce679]623
624cleanup:
625
[e6f6260]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
[e8d054a]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
[1d68621]715 // dot entry has the smallest size available
716 uint32_t max_entry_count = block_size / sizeof(ext4_directory_dx_dot_entry_t);
[c4f318d6]717
[e8d054a]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
[e8d054a]728 // Initialize hinfo
[1d68621]729 ext4_hash_info_t tmp_hinfo;
730 memcpy(&tmp_hinfo, hinfo, sizeof(ext4_hash_info_t));
731
[e8d054a]732 // Load all valid entries to the buffer
733 ext4_directory_entry_ll_t *dentry = old_data_block->data;
734 void *entry_buffer_ptr = entry_buffer;
[ccac91e]735 while ((void *)dentry < old_data_block->data + block_size) {
[c4f318d6]736
[412f813]737 // Read only valid entries
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
[e6f6260]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
[e6f6260]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
[ccac91e]778 // Load new block
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
[e6f6260]788 // Distribute entries to two blocks (by size)
789 // Compute the half
[ccac91e]790 uint32_t new_hash = 0;
791 uint32_t current_size = 0;
[1d68621]792 uint32_t mid = 0;
793 for (uint32_t i = 0; i < idx; ++i) {
794 if ((current_size + sort_array[i].rec_len) > (real_size / 2)) {
[ccac91e]795 new_hash = sort_array[i].hash;
796 mid = i;
797 break;
798 }
799
800 current_size += sort_array[i].rec_len;
801 }
802
[e6f6260]803 // Check hash collision
[412f813]804 uint32_t continued = 0;
805 if (new_hash == sort_array[mid-1].hash) {
806 continued = 1;
807 }
808
[ccac91e]809 uint32_t offset = 0;
810 void *ptr;
[f577058]811
[ccac91e]812 // First part - to the old block
[1d68621]813 for (uint32_t i = 0; i < mid; ++i) {
[ccac91e]814 ptr = old_data_block->data + offset;
815 memcpy(ptr, sort_array[i].dentry, sort_array[i].rec_len);
816
817 ext4_directory_entry_ll_t *tmp = ptr;
818 if (i < (mid - 1)) {
819 ext4_directory_entry_ll_set_entry_length(tmp, sort_array[i].rec_len);
820 } else {
821 ext4_directory_entry_ll_set_entry_length(tmp, block_size - offset);
822 }
823
824 offset += sort_array[i].rec_len;
825 }
826
827 // Second part - to the new block
828 offset = 0;
[1d68621]829 for (uint32_t i = mid; i < idx; ++i) {
[ccac91e]830 ptr = new_data_block_tmp->data + offset;
831 memcpy(ptr, sort_array[i].dentry, sort_array[i].rec_len);
832
833 ext4_directory_entry_ll_t *tmp = ptr;
834 if (i < (idx - 1)) {
835 ext4_directory_entry_ll_set_entry_length(tmp, sort_array[i].rec_len);
836 } else {
837 ext4_directory_entry_ll_set_entry_length(tmp, block_size - offset);
838 }
839
840 offset += sort_array[i].rec_len;
841 }
842
[e6f6260]843 // Do some steps to finish operation
[ccac91e]844 old_data_block->dirty = true;
845 new_data_block_tmp->dirty = true;
846
847 free(sort_array);
848 free(entry_buffer);
[f577058]849
[c0964cd7]850 ext4_directory_dx_insert_entry(index_block, new_hash + continued, new_iblock);
[ccac91e]851
852 *new_data_block = new_data_block_tmp;
[f577058]853
854 return EOK;
855}
856
[dfac604]857/** Split index node and maybe some parent nodes in the tree hierarchy.
[e6f6260]858 *
[dfac604]859 * @param inode_ref directory i-node
860 * @param dx_blocks array with path from root to leaf node
861 * @param dx_block leaf block to be split if needed
862 * @return error code
[e6f6260]863 */
[dfac604]864static int ext4_directory_dx_split_index(ext4_inode_ref_t *inode_ref,
865 ext4_directory_dx_block_t *dx_blocks, ext4_directory_dx_block_t *dx_block)
[565b6ff]866{
[2f2feadb]867 int rc;
[565b6ff]868
[2f2feadb]869 ext4_directory_dx_entry_t *entries;
870 if (dx_block == dx_blocks) {
871 entries = ((ext4_directory_dx_root_t *) dx_block->block->data)->entries;
872 } else {
873 entries = ((ext4_directory_dx_node_t *) dx_block->block->data)->entries;
[565b6ff]874 }
875
[2f2feadb]876 ext4_directory_dx_countlimit_t *countlimit =
877 (ext4_directory_dx_countlimit_t *)entries;
878 uint16_t leaf_limit = ext4_directory_dx_countlimit_get_limit(countlimit);
879 uint16_t leaf_count = ext4_directory_dx_countlimit_get_count(countlimit);
[565b6ff]880
[2f2feadb]881 // Check if is necessary to split index block
[1ebb66f]882 if (leaf_limit == leaf_count) {
[5c83612b]883
[c0964cd7]884 unsigned int levels = dx_block - dx_blocks;
885
[2f2feadb]886 ext4_directory_dx_entry_t *root_entries =
887 ((ext4_directory_dx_root_t *)dx_blocks[0].block->data)->entries;
888
889 ext4_directory_dx_countlimit_t *root_countlimit =
890 (ext4_directory_dx_countlimit_t *)root_entries;
891 uint16_t root_limit =
892 ext4_directory_dx_countlimit_get_limit(root_countlimit);
893 uint16_t root_count =
894 ext4_directory_dx_countlimit_get_count(root_countlimit);
[c0964cd7]895
[dfac604]896 // Linux limitation
[c0964cd7]897 if ((levels > 0) && (root_limit == root_count)) {
898 EXT4FS_DBG("Directory index is full");
[2f2feadb]899 return ENOSPC;
[c0964cd7]900 }
901
[dfac604]902 // Add new block to directory
[c0964cd7]903 uint32_t new_fblock;
904 uint32_t new_iblock;
[5b16912]905 rc = ext4_filesystem_append_inode_block(
906 inode_ref, &new_fblock, &new_iblock);
[c0964cd7]907 if (rc != EOK) {
[2f2feadb]908 return rc;
[c0964cd7]909 }
910
[dfac604]911 // load new block
[c0964cd7]912 block_t * new_block;
[dfac604]913 rc = block_get(&new_block, inode_ref->fs->device,
914 new_fblock, BLOCK_FLAGS_NOREAD);
[c0964cd7]915 if (rc != EOK) {
[2f2feadb]916 return rc;
[c0964cd7]917 }
918
[6f731f0a]919 ext4_directory_dx_node_t *new_node = new_block->data;
920 ext4_directory_dx_entry_t *new_entries = new_node->entries;
921
[dfac604]922 uint32_t block_size = ext4_superblock_get_block_size(
923 inode_ref->fs->superblock);
[2f2feadb]924
[dfac604]925 // Split leaf node
[c0964cd7]926 if (levels > 0) {
[dfac604]927
[c0964cd7]928 uint32_t count_left = leaf_count / 2;
929 uint32_t count_right = leaf_count - count_left;
[2f2feadb]930 uint32_t hash_right =
931 ext4_directory_dx_entry_get_hash(entries + count_left);
[c0964cd7]932
[dfac604]933 // Copy data to new node
[c0964cd7]934 memcpy((void *) new_entries, (void *) (entries + count_left),
935 count_right * sizeof(ext4_directory_dx_entry_t));
936
[dfac604]937 // Initialize new node
[2f2feadb]938 ext4_directory_dx_countlimit_t *left_countlimit =
939 (ext4_directory_dx_countlimit_t *)entries;
940 ext4_directory_dx_countlimit_t *right_countlimit =
941 (ext4_directory_dx_countlimit_t *)new_entries;
[c0964cd7]942
943 ext4_directory_dx_countlimit_set_count(left_countlimit, count_left);
944 ext4_directory_dx_countlimit_set_count(right_countlimit, count_right);
945
[2f2feadb]946 uint32_t entry_space = block_size - sizeof(ext4_fake_directory_entry_t);
947 uint32_t node_limit = entry_space / sizeof(ext4_directory_dx_entry_t);
948 ext4_directory_dx_countlimit_set_limit(right_countlimit, node_limit);
[c0964cd7]949
[2f2feadb]950 // Which index block is target for new entry
951 uint32_t position_index = (dx_block->position - dx_block->entries);
952 if (position_index >= count_left) {
[c0964cd7]953
[2f2feadb]954 dx_block->block->dirty = true;
955
956 block_t *block_tmp = dx_block->block;
957 dx_block->block = new_block;
958 dx_block->position = new_entries + position_index - count_left;
959 dx_block->entries = new_entries;
960
961 new_block = block_tmp;
962
963 }
[c0964cd7]964
[dfac604]965 // Finally insert new entry
[2f2feadb]966 ext4_directory_dx_insert_entry(dx_blocks, hash_right, new_iblock);
[c0964cd7]967
[2f2feadb]968 return block_put(new_block);
[c0964cd7]969
970 } else {
[6f731f0a]971
[dfac604]972 // Create second level index
973
974 // Copy data from root to child block
[6f731f0a]975 memcpy((void *) new_entries, (void *) entries,
976 leaf_count * sizeof(ext4_directory_dx_entry_t));
977
978 ext4_directory_dx_countlimit_t *new_countlimit =
979 (ext4_directory_dx_countlimit_t *)new_entries;
980
[2f2feadb]981 uint32_t entry_space = block_size - sizeof(ext4_fake_directory_entry_t);
982 uint32_t node_limit = entry_space / sizeof(ext4_directory_dx_entry_t);
983 ext4_directory_dx_countlimit_set_limit(new_countlimit, node_limit);
[6f731f0a]984
[2f2feadb]985 // Set values in root node
986 ext4_directory_dx_countlimit_t *new_root_countlimit =
987 (ext4_directory_dx_countlimit_t *)entries;
[6f731f0a]988
[2f2feadb]989 ext4_directory_dx_countlimit_set_count(new_root_countlimit, 1);
990 ext4_directory_dx_entry_set_block(entries, new_iblock);
[6f731f0a]991
[2f2feadb]992 ((ext4_directory_dx_root_t *)dx_blocks[0].block->data)->info.indirect_levels = 1;
[6f731f0a]993
[dfac604]994 // Add new entry to the path
[2f2feadb]995 dx_block = dx_blocks + 1;
996 dx_block->position = dx_block->position - entries + new_entries;
997 dx_block->entries = new_entries;
998 dx_block->block = new_block;
[c0964cd7]999 }
[2f2feadb]1000
1001 }
1002
1003 return EOK;
1004}
1005
[dfac604]1006/** Add new entry to indexed directory
1007 *
1008 * @param parent directory i-node
1009 * @param child i-node to be referenced from directory entry
1010 * @param name name of new directory entry
1011 * @return error code
1012 */
[1ac1ab4]1013int ext4_directory_dx_add_entry(ext4_inode_ref_t *parent,
1014 ext4_inode_ref_t *child, const char *name)
[2f2feadb]1015{
1016 int rc = EOK;
1017 int rc2 = EOK;
1018
1019 // get direct block 0 (index root)
1020 uint32_t root_block_addr;
[1ac1ab4]1021 rc = ext4_filesystem_get_inode_data_block_index(parent, 0, &root_block_addr);
[2f2feadb]1022 if (rc != EOK) {
1023 return rc;
1024 }
1025
[1ac1ab4]1026 ext4_filesystem_t *fs = parent->fs;
1027
[2f2feadb]1028 block_t *root_block;
1029 rc = block_get(&root_block, fs->device, root_block_addr, BLOCK_FLAGS_NONE);
1030 if (rc != EOK) {
1031 return rc;
1032 }
1033
[dfac604]1034 // Initialize hinfo structure (mainly compute hash)
[2f2feadb]1035 uint32_t name_len = strlen(name);
1036 ext4_hash_info_t hinfo;
1037 rc = ext4_directory_hinfo_init(&hinfo, root_block, fs->superblock, name_len, name);
1038 if (rc != EOK) {
1039 block_put(root_block);
[7eb033ce]1040 EXT4FS_DBG("hinfo initialization error");
[2f2feadb]1041 return EXT4_ERR_BAD_DX_DIR;
1042 }
1043
[dfac604]1044 // Hardcoded number 2 means maximum height of index tree defined in linux
[2f2feadb]1045 ext4_directory_dx_block_t dx_blocks[2];
1046 ext4_directory_dx_block_t *dx_block, *dx_it;
[1ac1ab4]1047 rc = ext4_directory_dx_get_leaf(&hinfo, parent, root_block, &dx_block, dx_blocks);
[2f2feadb]1048 if (rc != EOK) {
[7eb033ce]1049 EXT4FS_DBG("get leaf error");
[2f2feadb]1050 rc = EXT4_ERR_BAD_DX_DIR;
1051 goto release_index;
1052 }
1053
1054
1055 // Try to insert to existing data block
1056 uint32_t leaf_block_idx = ext4_directory_dx_entry_get_block(dx_block->position);
1057 uint32_t leaf_block_addr;
[1ac1ab4]1058 rc = ext4_filesystem_get_inode_data_block_index(parent, leaf_block_idx, &leaf_block_addr);
[2f2feadb]1059 if (rc != EOK) {
1060 goto release_index;
1061 }
1062
1063
1064 block_t *target_block;
1065 rc = block_get(&target_block, fs->device, leaf_block_addr, BLOCK_FLAGS_NONE);
1066 if (rc != EOK) {
1067 goto release_index;
1068 }
1069
[dfac604]1070 // Check if insert operation passed
[2f2feadb]1071 rc = ext4_directory_try_insert_entry(fs->superblock, target_block, child, name, name_len);
1072 if (rc == EOK) {
1073 goto release_target_index;
1074 }
1075
[dfac604]1076 // Check if there is needed to split index node
1077 // (and recursively also parent nodes)
1078 rc = ext4_directory_dx_split_index(parent, dx_blocks, dx_block);
[2f2feadb]1079 if (rc != EOK) {
1080 goto release_target_index;
[c4f318d6]1081 }
[5c83612b]1082
[dfac604]1083 // Split entries to two blocks (includes sorting by hash value)
[c4f318d6]1084 block_t *new_block = NULL;
[e6f6260]1085 rc = ext4_directory_dx_split_data(parent, &hinfo, target_block, dx_block, &new_block);
[c4f318d6]1086 if (rc != EOK) {
[e63ce679]1087 rc2 = rc;
1088 goto release_target_index;
[c4f318d6]1089 }
[5c83612b]1090
[d0d7afb]1091 // Where to save new entry
[1d68621]1092 uint32_t new_block_hash = ext4_directory_dx_entry_get_hash(dx_block->position + 1);
[c4f318d6]1093 if (hinfo.hash >= new_block_hash) {
[d0d7afb]1094 rc = ext4_directory_try_insert_entry(fs->superblock, new_block, child, name, name_len);
[c4f318d6]1095 } else {
[d0d7afb]1096 rc = ext4_directory_try_insert_entry(fs->superblock, target_block, child, name, name_len);
[c4f318d6]1097 }
[5c83612b]1098
[dfac604]1099 // Cleanup
[d0d7afb]1100 rc = block_put(new_block);
1101 if (rc != EOK) {
1102 EXT4FS_DBG("error writing new block");
[60b8b99]1103 return rc;
[d0d7afb]1104 }
[c4f318d6]1105
[dfac604]1106 // Cleanup operations
1107
[e63ce679]1108release_target_index:
[c4f318d6]1109
[d0d7afb]1110 rc2 = rc;
[c4f318d6]1111
[412f813]1112 rc = block_put(target_block);
1113 if (rc != EOK) {
[e8d054a]1114 EXT4FS_DBG("error writing target block");
[60b8b99]1115 return rc;
[412f813]1116 }
[c4f318d6]1117
[e63ce679]1118release_index:
[60b8b99]1119
1120 if (rc != EOK) {
1121 rc2 = rc;
1122 }
1123
[e63ce679]1124 dx_it = dx_blocks;
[c4f318d6]1125
1126 while (dx_it <= dx_block) {
[412f813]1127 rc = block_put(dx_it->block);
1128 if (rc != EOK) {
[e8d054a]1129 EXT4FS_DBG("error writing index block");
[60b8b99]1130 return rc;
[412f813]1131 }
[c4f318d6]1132 dx_it++;
1133 }
1134
[d0d7afb]1135 return rc2;
[565b6ff]1136}
[0dc91833]1137
1138
1139/**
1140 * @}
1141 */
Note: See TracBrowser for help on using the repository browser.