source: mainline/uspace/lib/ext4/libext4_directory_index.c@ 2f619d5

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

debug message deleted

  • Property mode set to 100644
File size: 22.5 KB
RevLine 
[0dc91833]1/*
2 * Copyright (c) 2011 Frantisek Princ
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup libext4
30 * @{
31 */
32
33/**
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
[343ccfd]45
46uint8_t ext4_directory_dx_root_info_get_hash_version(
47 ext4_directory_dx_root_info_t *root_info)
48{
49 return root_info->hash_version;
50}
51
52void ext4_directory_dx_root_info_set_hash_version(
53 ext4_directory_dx_root_info_t *root_info, uint8_t version)
54{
55 root_info->hash_version = version;
56}
57
58uint8_t ext4_directory_dx_root_info_get_info_length(
59 ext4_directory_dx_root_info_t *root_info)
60{
61 return root_info->info_length;
62}
63
64void ext4_directory_dx_root_info_set_info_length(
65 ext4_directory_dx_root_info_t *root_info, uint8_t info_length)
66{
67 root_info->info_length = info_length;
68}
69
70uint8_t ext4_directory_dx_root_info_get_indirect_levels(
71 ext4_directory_dx_root_info_t *root_info)
72{
73 return root_info->indirect_levels;
74}
75
76void ext4_directory_dx_root_info_set_indirect_levels(
77 ext4_directory_dx_root_info_t *root_info, uint8_t levels)
78{
79 root_info->indirect_levels = levels;
80}
81
82uint16_t ext4_directory_dx_countlimit_get_limit(
83 ext4_directory_dx_countlimit_t *countlimit)
84{
85 return uint16_t_le2host(countlimit->limit);
86}
87
88void ext4_directory_dx_countlimit_set_limit(
89 ext4_directory_dx_countlimit_t *countlimit, uint16_t limit)
90{
91 countlimit->limit = host2uint16_t_le(limit);
92}
93
94uint16_t ext4_directory_dx_countlimit_get_count(
95 ext4_directory_dx_countlimit_t *countlimit)
96{
97 return uint16_t_le2host(countlimit->count);
98}
99
100void ext4_directory_dx_countlimit_set_count(
101 ext4_directory_dx_countlimit_t *countlimit, uint16_t count)
102{
103 countlimit->count = host2uint16_t_le(count);
104}
105
106uint32_t ext4_directory_dx_entry_get_hash(ext4_directory_dx_entry_t *entry)
107{
108 return uint32_t_le2host(entry->hash);
109}
110
111void ext4_directory_dx_entry_set_hash(ext4_directory_dx_entry_t *entry,
112 uint32_t hash)
113{
114 entry->hash = host2uint32_t_le(hash);
115}
116
117uint32_t ext4_directory_dx_entry_get_block(ext4_directory_dx_entry_t *entry)
118{
119 return uint32_t_le2host(entry->block);
120}
121
122void ext4_directory_dx_entry_set_block(ext4_directory_dx_entry_t *entry,
123 uint32_t block)
124{
125 entry->block = host2uint32_t_le(block);
126}
127
128
129/**************************************************************************/
130
131
[0dc91833]132static int ext4_directory_hinfo_init(ext4_hash_info_t *hinfo, block_t *root_block,
133 ext4_superblock_t *sb, size_t name_len, const char *name)
134{
135
[d9bbe45]136 ext4_directory_dx_root_t *root = (ext4_directory_dx_root_t *)root_block->data;
[0dc91833]137
138 if (root->info.hash_version != EXT4_HASH_VERSION_TEA &&
139 root->info.hash_version != EXT4_HASH_VERSION_HALF_MD4 &&
140 root->info.hash_version != EXT4_HASH_VERSION_LEGACY) {
141 return EXT4_ERR_BAD_DX_DIR;
142 }
143
144 // Check unused flags
145 if (root->info.unused_flags != 0) {
146 EXT4FS_DBG("ERR: unused_flags = \%u", root->info.unused_flags);
147 return EXT4_ERR_BAD_DX_DIR;
148 }
149
150 // Check indirect levels
151 if (root->info.indirect_levels > 1) {
152 EXT4FS_DBG("ERR: indirect_levels = \%u", root->info.indirect_levels);
153 return EXT4_ERR_BAD_DX_DIR;
154 }
155
[d9bbe45]156 uint32_t block_size = ext4_superblock_get_block_size(sb);
[0dc91833]157
[d9bbe45]158 uint32_t entry_space = block_size;
[0dc91833]159 entry_space -= 2 * sizeof(ext4_directory_dx_dot_entry_t);
160 entry_space -= sizeof(ext4_directory_dx_root_info_t);
161 entry_space = entry_space / sizeof(ext4_directory_dx_entry_t);
162
[d9bbe45]163 uint16_t limit = ext4_directory_dx_countlimit_get_limit((ext4_directory_dx_countlimit_t *)&root->entries);
[0dc91833]164 if (limit != entry_space) {
165 return EXT4_ERR_BAD_DX_DIR;
166 }
167
168 hinfo->hash_version = ext4_directory_dx_root_info_get_hash_version(&root->info);
169 if ((hinfo->hash_version <= EXT4_HASH_VERSION_TEA)
170 && (ext4_superblock_has_flag(sb, EXT4_SUPERBLOCK_FLAGS_UNSIGNED_HASH))) {
171 // 3 is magic from ext4 linux implementation
172 hinfo->hash_version += 3;
173 }
174
175 hinfo->seed = ext4_superblock_get_hash_seed(sb);
176
177 if (name) {
178 ext4_hash_string(hinfo, name_len, name);
179 }
180
181 return EOK;
182}
183
184static int ext4_directory_dx_get_leaf(ext4_hash_info_t *hinfo,
185 ext4_filesystem_t *fs, ext4_inode_t *inode, block_t *root_block,
186 ext4_directory_dx_block_t **dx_block, ext4_directory_dx_block_t *dx_blocks)
187{
188 int rc;
[d9bbe45]189
[0dc91833]190 ext4_directory_dx_block_t *tmp_dx_block = dx_blocks;
191
[d9bbe45]192 ext4_directory_dx_root_t *root = (ext4_directory_dx_root_t *)root_block->data;
193 ext4_directory_dx_entry_t *entries = (ext4_directory_dx_entry_t *)&root->entries;
[0dc91833]194
[d9bbe45]195 uint16_t limit = ext4_directory_dx_countlimit_get_limit((ext4_directory_dx_countlimit_t *)entries);
196 uint8_t indirect_level = ext4_directory_dx_root_info_get_indirect_levels(&root->info);
[0dc91833]197
[d9bbe45]198 block_t *tmp_block = root_block;
199 ext4_directory_dx_entry_t *p, *q, *m, *at;
[0dc91833]200 while (true) {
201
[d9bbe45]202 uint16_t count = ext4_directory_dx_countlimit_get_count((ext4_directory_dx_countlimit_t *)entries);
[0dc91833]203 if ((count == 0) || (count > limit)) {
204 return EXT4_ERR_BAD_DX_DIR;
205 }
206
207 p = entries + 1;
208 q = entries + count - 1;
209
210 while (p <= q) {
211 m = p + (q - p) / 2;
212 if (ext4_directory_dx_entry_get_hash(m) > hinfo->hash) {
213 q = m - 1;
214 } else {
215 p = m + 1;
216 }
217 }
218
219 at = p - 1;
220
221 tmp_dx_block->block = tmp_block;
222 tmp_dx_block->entries = entries;
223 tmp_dx_block->position = at;
224
225 if (indirect_level == 0) {
226 *dx_block = tmp_dx_block;
227 return EOK;
228 }
229
[d9bbe45]230 uint32_t next_block = ext4_directory_dx_entry_get_block(at);
[0dc91833]231
232 indirect_level--;
233
[d9bbe45]234 uint32_t fblock;
[0dc91833]235 rc = ext4_filesystem_get_inode_data_block_index(fs, inode, next_block, &fblock);
236 if (rc != EOK) {
237 return rc;
238 }
239
240 rc = block_get(&tmp_block, fs->device, fblock, BLOCK_FLAGS_NONE);
241 if (rc != EOK) {
242 return rc;
243 }
244
245 entries = ((ext4_directory_dx_node_t *) tmp_block->data)->entries;
246 limit = ext4_directory_dx_countlimit_get_limit((ext4_directory_dx_countlimit_t *)entries);
247
[d9bbe45]248 uint16_t entry_space = ext4_superblock_get_block_size(fs->superblock)
249 - sizeof(ext4_directory_dx_dot_entry_t);
[0dc91833]250 entry_space = entry_space / sizeof(ext4_directory_dx_entry_t);
251
252
253 if (limit != entry_space) {
254 block_put(tmp_block);
255 return EXT4_ERR_BAD_DX_DIR;
256 }
257
258 ++tmp_dx_block;
259 }
260
261 // Unreachable
262 return EOK;
263}
264
265
266static int ext4_directory_dx_next_block(ext4_filesystem_t *fs, ext4_inode_t *inode, uint32_t hash,
267 ext4_directory_dx_block_t *handle, ext4_directory_dx_block_t *handles)
268{
[d9bbe45]269 int rc;
270
[0dc91833]271
[d9bbe45]272 uint32_t num_handles = 0;
273 ext4_directory_dx_block_t *p = handle;
[0dc91833]274
275 while (1) {
276
277 p->position++;
[d9bbe45]278 uint16_t count = ext4_directory_dx_countlimit_get_count((ext4_directory_dx_countlimit_t *)p->entries);
[0dc91833]279
280 if (p->position < p->entries + count) {
281 break;
282 }
283
284 if (p == handles) {
285 return 0;
286 }
287
288 num_handles++;
289 p--;
290 }
291
[d9bbe45]292 uint32_t current_hash = ext4_directory_dx_entry_get_hash(p->position);
[0dc91833]293
294 if ((hash & 1) == 0) {
295 if ((current_hash & ~1) != hash) {
296 return 0;
297 }
298 }
299
[d9bbe45]300
[0dc91833]301 while (num_handles--) {
302
[d9bbe45]303 uint32_t block_idx = ext4_directory_dx_entry_get_block(p->position);
304 uint32_t block_addr;
[0dc91833]305 rc = ext4_filesystem_get_inode_data_block_index(fs, inode, block_idx, &block_addr);
306 if (rc != EOK) {
307 return rc;
308 }
309
[d9bbe45]310 block_t *block;
[0dc91833]311 rc = block_get(&block, fs->device, block_addr, BLOCK_FLAGS_NONE);
312 if (rc != EOK) {
313 return rc;
314 }
315
316 p++;
317
318 block_put(p->block);
319 p->block = block;
320 p->entries = ((ext4_directory_dx_node_t *) block->data)->entries;
321 p->position = p->entries;
322 }
323
324 return 1;
325
326}
327
[7689590]328int ext4_directory_dx_find_entry(ext4_directory_search_result_t *result,
329 ext4_filesystem_t *fs, ext4_inode_ref_t *inode_ref, size_t name_len, const char *name)
[0dc91833]330{
331 int rc;
332
333 // get direct block 0 (index root)
[d9bbe45]334 uint32_t root_block_addr;
[0dc91833]335 rc = ext4_filesystem_get_inode_data_block_index(fs, inode_ref->inode, 0, &root_block_addr);
336 if (rc != EOK) {
337 return rc;
338 }
339
[d9bbe45]340 block_t *root_block;
[0dc91833]341 rc = block_get(&root_block, fs->device, root_block_addr, BLOCK_FLAGS_NONE);
342 if (rc != EOK) {
343 return rc;
344 }
345
[d9bbe45]346 ext4_hash_info_t hinfo;
[7689590]347 rc = ext4_directory_hinfo_init(&hinfo, root_block, fs->superblock, name_len, name);
[0dc91833]348 if (rc != EOK) {
349 block_put(root_block);
350 return EXT4_ERR_BAD_DX_DIR;
351 }
352
[565b6ff]353 // Hardcoded number 2 means maximum height of index tree !!!
[d9bbe45]354 ext4_directory_dx_block_t dx_blocks[2];
[e63ce679]355 ext4_directory_dx_block_t *dx_block, *tmp;
[0dc91833]356 rc = ext4_directory_dx_get_leaf(&hinfo, fs, inode_ref->inode, root_block, &dx_block, dx_blocks);
357 if (rc != EOK) {
358 block_put(root_block);
359 return EXT4_ERR_BAD_DX_DIR;
360 }
361
362
[d9bbe45]363 do {
[0dc91833]364
[d9bbe45]365 uint32_t leaf_block_idx = ext4_directory_dx_entry_get_block(dx_block->position);
366 uint32_t leaf_block_addr;
[0dc91833]367 rc = ext4_filesystem_get_inode_data_block_index(fs, inode_ref->inode, leaf_block_idx, &leaf_block_addr);
368 if (rc != EOK) {
[e63ce679]369 goto cleanup;
[0dc91833]370 }
371
[d9bbe45]372 block_t *leaf_block;
[0dc91833]373 rc = block_get(&leaf_block, fs->device, leaf_block_addr, BLOCK_FLAGS_NONE);
374 if (rc != EOK) {
[e63ce679]375 goto cleanup;
[0dc91833]376 }
377
[d9bbe45]378 ext4_directory_entry_ll_t *res_dentry;
[7689590]379 rc = ext4_directory_find_in_block(leaf_block, fs->superblock, name_len, name, &res_dentry);
380
[0dc91833]381 // Found => return it
[7689590]382 if (rc == EOK) {
383 result->block = leaf_block;
384 result->dentry = res_dentry;
[e8d054a]385 goto cleanup;
[0dc91833]386 }
387
[e63ce679]388 // Not found, leave untouched
[0dc91833]389 block_put(leaf_block);
390
[e63ce679]391 if (rc != ENOENT) {
392 goto cleanup;
[0dc91833]393 }
394
395 rc = ext4_directory_dx_next_block(fs, inode_ref->inode, hinfo.hash, dx_block, &dx_blocks[0]);
396 if (rc < 0) {
[e63ce679]397 goto cleanup;
[0dc91833]398 }
399
400 } while (rc == 1);
401
[e8d054a]402 rc = ENOENT;
[e63ce679]403
404cleanup:
405
406 tmp = dx_blocks;
407 while (tmp <= dx_block) {
408 block_put(tmp->block);
409 ++tmp;
410 }
411 return rc;
[0dc91833]412}
413
[f577058]414typedef struct ext4_dx_sort_entry {
415 uint32_t hash;
[ccac91e]416 uint32_t rec_len;
[c4f318d6]417 void *dentry;
[f577058]418} ext4_dx_sort_entry_t;
419
420static int dx_entry_comparator(void *arg1, void *arg2, void *dummy)
421{
422 ext4_dx_sort_entry_t *entry1 = arg1;
423 ext4_dx_sort_entry_t *entry2 = arg2;
424
425 if (entry1->hash == entry2->hash) {
426 return 0;
427 }
428
429 if (entry1->hash < entry2->hash) {
430 return -1;
431 } else {
432 return 1;
433 }
434
435}
436
[c0964cd7]437static void ext4_directory_dx_insert_entry(
438 ext4_directory_dx_block_t *index_block, uint32_t hash, uint32_t iblock)
439{
440 ext4_directory_dx_entry_t *old_index_entry = index_block->position;
441 ext4_directory_dx_entry_t *new_index_entry = old_index_entry + 1;
442
443 ext4_directory_dx_countlimit_t *countlimit =
444 (ext4_directory_dx_countlimit_t *)index_block->entries;
445 uint32_t count = ext4_directory_dx_countlimit_get_count(countlimit);
446
447 ext4_directory_dx_entry_t *start_index = index_block->entries;
448 size_t bytes = (void *)(start_index + count) - (void *)(new_index_entry);
449
450 memmove(new_index_entry + 1, new_index_entry, bytes);
451
452 ext4_directory_dx_entry_set_block(new_index_entry, iblock);
453 ext4_directory_dx_entry_set_hash(new_index_entry, hash);
454
455 ext4_directory_dx_countlimit_set_count(countlimit, count + 1);
456
457 index_block->block->dirty = true;
458}
459
[f577058]460static int ext4_directory_dx_split_data(ext4_filesystem_t *fs,
461 ext4_inode_ref_t *inode_ref, ext4_hash_info_t *hinfo,
[ccac91e]462 block_t *old_data_block, ext4_directory_dx_block_t *index_block, block_t **new_data_block)
[f577058]463{
[d0d7afb]464 int rc = EOK;
[f577058]465
[e8d054a]466 // Allocate buffer for directory entries
[f577058]467 uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
468 void *entry_buffer = malloc(block_size);
469 if (entry_buffer == NULL) {
470 return ENOMEM;
471 }
472
[1d68621]473 // dot entry has the smallest size available
474 uint32_t max_entry_count = block_size / sizeof(ext4_directory_dx_dot_entry_t);
[c4f318d6]475
[e8d054a]476 // Allocate sort entry
[1d68621]477 ext4_dx_sort_entry_t *sort_array = malloc(max_entry_count * sizeof(ext4_dx_sort_entry_t));
[f577058]478 if (sort_array == NULL) {
479 free(entry_buffer);
480 return ENOMEM;
481 }
482
[1d68621]483 uint32_t idx = 0;
[ccac91e]484 uint32_t real_size = 0;
[1d68621]485
[e8d054a]486 // Initialize hinfo
[1d68621]487 ext4_hash_info_t tmp_hinfo;
488 memcpy(&tmp_hinfo, hinfo, sizeof(ext4_hash_info_t));
489
[e8d054a]490 // Load all valid entries to the buffer
491 ext4_directory_entry_ll_t *dentry = old_data_block->data;
492 void *entry_buffer_ptr = entry_buffer;
[ccac91e]493 while ((void *)dentry < old_data_block->data + block_size) {
[c4f318d6]494
[412f813]495 // Read only valid entries
496 if (ext4_directory_entry_ll_get_inode(dentry) != 0) {
[f577058]497
[412f813]498 uint8_t len = ext4_directory_entry_ll_get_name_length(fs->superblock, dentry);
[e8d054a]499 ext4_hash_string(&tmp_hinfo, len, (char *)dentry->name);
[ccac91e]500
[412f813]501 uint32_t rec_len = 8 + len;
502
503 if ((rec_len % 4) != 0) {
504 rec_len += 4 - (rec_len % 4);
505 }
[ccac91e]506
[412f813]507 memcpy(entry_buffer_ptr, dentry, rec_len);
[ccac91e]508
[412f813]509 sort_array[idx].dentry = entry_buffer_ptr;
510 sort_array[idx].rec_len = rec_len;
511 sort_array[idx].hash = tmp_hinfo.hash;
[f577058]512
[412f813]513 entry_buffer_ptr += rec_len;
514 real_size += rec_len;
515 idx++;
516 }
[ccac91e]517
[f577058]518 dentry = (void *)dentry + ext4_directory_entry_ll_get_entry_length(dentry);
519 }
520
[1d68621]521 qsort(sort_array, idx, sizeof(ext4_dx_sort_entry_t), dx_entry_comparator, NULL);
[f577058]522
523 uint32_t new_fblock;
[c4f318d6]524 uint32_t new_iblock;
525 rc = ext4_directory_append_block(fs, inode_ref, &new_fblock, &new_iblock);
[f577058]526 if (rc != EOK) {
527 free(sort_array);
528 free(entry_buffer);
529 return rc;
530 }
531
[ccac91e]532 // Load new block
533 block_t *new_data_block_tmp;
534 rc = block_get(&new_data_block_tmp, fs->device, new_fblock, BLOCK_FLAGS_NOREAD);
535 if (rc != EOK) {
536 free(sort_array);
537 free(entry_buffer);
538 return rc;
539 }
540
541 // Distribute entries to splitted blocks (by size)
542 uint32_t new_hash = 0;
543 uint32_t current_size = 0;
[1d68621]544 uint32_t mid = 0;
545 for (uint32_t i = 0; i < idx; ++i) {
546 if ((current_size + sort_array[i].rec_len) > (real_size / 2)) {
[ccac91e]547 new_hash = sort_array[i].hash;
548 mid = i;
549 break;
550 }
551
552 current_size += sort_array[i].rec_len;
553 }
554
[412f813]555 uint32_t continued = 0;
556 if (new_hash == sort_array[mid-1].hash) {
557 continued = 1;
558 }
559
[ccac91e]560 uint32_t offset = 0;
561 void *ptr;
[f577058]562
[ccac91e]563 // First part - to the old block
[1d68621]564 for (uint32_t i = 0; i < mid; ++i) {
[ccac91e]565 ptr = old_data_block->data + offset;
566 memcpy(ptr, sort_array[i].dentry, sort_array[i].rec_len);
567
568 ext4_directory_entry_ll_t *tmp = ptr;
569 if (i < (mid - 1)) {
570 ext4_directory_entry_ll_set_entry_length(tmp, sort_array[i].rec_len);
571 } else {
572 ext4_directory_entry_ll_set_entry_length(tmp, block_size - offset);
573 }
574
575 offset += sort_array[i].rec_len;
576 }
577
578 // Second part - to the new block
579 offset = 0;
[1d68621]580 for (uint32_t i = mid; i < idx; ++i) {
[ccac91e]581 ptr = new_data_block_tmp->data + offset;
582 memcpy(ptr, sort_array[i].dentry, sort_array[i].rec_len);
583
584 ext4_directory_entry_ll_t *tmp = ptr;
585 if (i < (idx - 1)) {
586 ext4_directory_entry_ll_set_entry_length(tmp, sort_array[i].rec_len);
587 } else {
588 ext4_directory_entry_ll_set_entry_length(tmp, block_size - offset);
589 }
590
591 offset += sort_array[i].rec_len;
592 }
593
594 old_data_block->dirty = true;
595 new_data_block_tmp->dirty = true;
596
597 free(sort_array);
598 free(entry_buffer);
[f577058]599
[c0964cd7]600 ext4_directory_dx_insert_entry(index_block, new_hash + continued, new_iblock);
[ccac91e]601
602 *new_data_block = new_data_block_tmp;
[f577058]603
604 return EOK;
605}
606
607
[565b6ff]608int ext4_directory_dx_add_entry(ext4_filesystem_t *fs,
[d0d7afb]609 ext4_inode_ref_t *parent, ext4_inode_ref_t *child, const char *name)
[565b6ff]610{
[d0d7afb]611 int rc = EOK;
[e63ce679]612 int rc2 = EOK;
[565b6ff]613
614 // get direct block 0 (index root)
615 uint32_t root_block_addr;
616 rc = ext4_filesystem_get_inode_data_block_index(fs, parent->inode, 0, &root_block_addr);
617 if (rc != EOK) {
618 return rc;
619 }
620
621 block_t *root_block;
622 rc = block_get(&root_block, fs->device, root_block_addr, BLOCK_FLAGS_NONE);
623 if (rc != EOK) {
624 return rc;
625 }
626
[d0d7afb]627 uint32_t name_len = strlen(name);
[565b6ff]628 ext4_hash_info_t hinfo;
[d0d7afb]629 rc = ext4_directory_hinfo_init(&hinfo, root_block, fs->superblock, name_len, name);
[565b6ff]630 if (rc != EOK) {
631 block_put(root_block);
632 return EXT4_ERR_BAD_DX_DIR;
633 }
634
635 // Hardcoded number 2 means maximum height of index tree !!!
636 ext4_directory_dx_block_t dx_blocks[2];
[e63ce679]637 ext4_directory_dx_block_t *dx_block, *dx_it;
[565b6ff]638 rc = ext4_directory_dx_get_leaf(&hinfo, fs, parent->inode, root_block, &dx_block, dx_blocks);
639 if (rc != EOK) {
[e63ce679]640 rc = EXT4_ERR_BAD_DX_DIR;
641 goto release_index;
[565b6ff]642 }
643
644
[5c83612b]645 // Try to insert to existing data block
646 uint32_t leaf_block_idx = ext4_directory_dx_entry_get_block(dx_block->position);
647 uint32_t leaf_block_addr;
648 rc = ext4_filesystem_get_inode_data_block_index(fs, parent->inode, leaf_block_idx, &leaf_block_addr);
649 if (rc != EOK) {
[e63ce679]650 goto release_index;
[5c83612b]651 }
652
653
[ccac91e]654 block_t *target_block;
655 rc = block_get(&target_block, fs->device, leaf_block_addr, BLOCK_FLAGS_NONE);
[5c83612b]656 if (rc != EOK) {
[e63ce679]657 goto release_index;
[5c83612b]658 }
659
[d0d7afb]660 rc = ext4_directory_try_insert_entry(fs->superblock, target_block, child, name, name_len);
661 if (rc == EOK) {
[e63ce679]662 goto release_target_index;
[5c83612b]663 }
664
[1ebb66f]665 EXT4FS_DBG("no free space found");
[5c83612b]666
[d0d7afb]667 uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
668
[5c83612b]669 ext4_directory_dx_entry_t *entries = ((ext4_directory_dx_node_t *) dx_block->block->data)->entries;
[1ebb66f]670 uint16_t leaf_limit = ext4_directory_dx_countlimit_get_limit((ext4_directory_dx_countlimit_t *)entries);
671 uint16_t leaf_count = ext4_directory_dx_countlimit_get_count((ext4_directory_dx_countlimit_t *)entries);
[5c83612b]672
[c0964cd7]673 ext4_directory_dx_entry_t *root_entries = ((ext4_directory_dx_node_t *) dx_blocks[0].block->data)->entries;
[5c83612b]674
[1ebb66f]675 if (leaf_limit == leaf_count) {
[5c83612b]676 EXT4FS_DBG("need to split index block !!!");
677
[c0964cd7]678 unsigned int levels = dx_block - dx_blocks;
679
680 uint16_t root_limit = ext4_directory_dx_countlimit_get_limit((ext4_directory_dx_countlimit_t *)root_entries);
681 uint16_t root_count = ext4_directory_dx_countlimit_get_count((ext4_directory_dx_countlimit_t *)root_entries);
682
683 if ((levels > 0) && (root_limit == root_count)) {
684 EXT4FS_DBG("Directory index is full");
[e63ce679]685 rc = ENOSPC;
686 goto release_target_index;
[c0964cd7]687 }
688
689 uint32_t new_fblock;
690 uint32_t new_iblock;
691 rc = ext4_directory_append_block(fs, parent, &new_fblock, &new_iblock);
692 if (rc != EOK) {
[e63ce679]693 goto release_target_index;
[c0964cd7]694 }
695
696 // New block allocated
697 block_t * new_block;
698 rc = block_get(&new_block, fs->device, new_fblock, BLOCK_FLAGS_NOREAD);
699 if (rc != EOK) {
[e63ce679]700 goto release_target_index;
[c0964cd7]701 }
702
703 // Initialize block
704 memset(new_block->data, 0, block_size);
705
[6f731f0a]706 ext4_directory_dx_node_t *new_node = new_block->data;
707 ext4_directory_dx_entry_t *new_entries = new_node->entries;
708
[c0964cd7]709 if (levels > 0) {
710 EXT4FS_DBG("split index");
711 uint32_t count_left = leaf_count / 2;
712 uint32_t count_right = leaf_count - count_left;
713 uint32_t hash_right = ext4_directory_dx_entry_get_hash(entries);
714
715 memcpy((void *) new_entries, (void *) (entries + count_left),
716 count_right * sizeof(ext4_directory_dx_entry_t));
717
718 ext4_directory_dx_countlimit_t *left_countlimit = (ext4_directory_dx_countlimit_t *)entries;
719 ext4_directory_dx_countlimit_t *right_countlimit = (ext4_directory_dx_countlimit_t *)new_entries;
720
721 ext4_directory_dx_countlimit_set_count(left_countlimit, count_left);
722 ext4_directory_dx_countlimit_set_count(right_countlimit, count_right);
723
724 uint32_t entry_space = block_size - sizeof(ext4_fake_directory_entry_t);
725 uint32_t node_limit = entry_space / sizeof(ext4_directory_dx_entry_t);
726 ext4_directory_dx_countlimit_set_limit(right_countlimit, node_limit);
727
728 // Which index block is target for new entry
729 uint32_t position_index = (dx_block->position - dx_block->entries);
730 if (position_index >= count_left) {
731 dx_block->position = new_entries + position_index - count_left;
732 dx_block->entries = new_entries;
733 entries = dx_block->entries;
734
735 dx_block->block->dirty = true;
736 block_put(dx_block->block);
737 dx_block->block = new_block;
738 }
739
740 ext4_directory_dx_insert_entry(dx_blocks, hash_right, new_iblock);
741
742
743 } else {
744 EXT4FS_DBG("create second level");
[6f731f0a]745
746 memcpy((void *) new_entries, (void *) entries,
747 leaf_count * sizeof(ext4_directory_dx_entry_t));
748
749 ext4_directory_dx_countlimit_t *new_countlimit =
750 (ext4_directory_dx_countlimit_t *)new_entries;
751
752 uint32_t entry_space = block_size - sizeof(ext4_fake_directory_entry_t);
753 uint32_t node_limit = entry_space / sizeof(ext4_directory_dx_entry_t);
754 ext4_directory_dx_countlimit_set_limit(new_countlimit, node_limit);
755
756 // Set values in root node
757 ext4_directory_dx_countlimit_t *new_root_countlimit =
758 (ext4_directory_dx_countlimit_t *)entries;
759
760 ext4_directory_dx_countlimit_set_limit(new_root_countlimit, 1);
761 ext4_directory_dx_entry_set_block(entries, new_iblock);
762
763 ((ext4_directory_dx_root_t *)dx_blocks[0].block->data)->info.indirect_levels = 1;
764
765 /* Add new access path frame */
766 dx_block = dx_blocks + 1;
767 dx_block->position = dx_block->position - entries + new_entries;
768 dx_block->entries = new_entries;
769 entries = dx_block->entries;
770 dx_block->block = new_block;
[c0964cd7]771 }
[c4f318d6]772 }
[5c83612b]773
[c4f318d6]774 block_t *new_block = NULL;
775 rc = ext4_directory_dx_split_data(fs, parent, &hinfo, target_block, dx_block, &new_block);
776 if (rc != EOK) {
[e63ce679]777 rc2 = rc;
778 goto release_target_index;
[c4f318d6]779 }
[5c83612b]780
[d0d7afb]781 // Where to save new entry
[1d68621]782 uint32_t new_block_hash = ext4_directory_dx_entry_get_hash(dx_block->position + 1);
[c4f318d6]783 if (hinfo.hash >= new_block_hash) {
[d0d7afb]784 rc = ext4_directory_try_insert_entry(fs->superblock, new_block, child, name, name_len);
[c4f318d6]785 } else {
[d0d7afb]786 rc = ext4_directory_try_insert_entry(fs->superblock, target_block, child, name, name_len);
[c4f318d6]787 }
[5c83612b]788
[e63ce679]789// TODO check rc handling
[d0d7afb]790 rc = block_put(new_block);
791 if (rc != EOK) {
792 EXT4FS_DBG("error writing new block");
793 }
[c4f318d6]794
[565b6ff]795
[e63ce679]796release_target_index:
[c4f318d6]797
[d0d7afb]798 rc2 = rc;
[c4f318d6]799
[412f813]800 rc = block_put(target_block);
801 if (rc != EOK) {
[e8d054a]802 EXT4FS_DBG("error writing target block");
803// return rc;
[412f813]804 }
[c4f318d6]805
[e63ce679]806release_index:
807 dx_it = dx_blocks;
[c4f318d6]808
809 while (dx_it <= dx_block) {
[412f813]810 rc = block_put(dx_it->block);
811 if (rc != EOK) {
[e8d054a]812 EXT4FS_DBG("error writing index block");
813// return rc;
[412f813]814 }
[c4f318d6]815 dx_it++;
816 }
817
[d0d7afb]818 return rc2;
[565b6ff]819}
[0dc91833]820
821
822/**
823 * @}
824 */
Note: See TracBrowser for help on using the repository browser.