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

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

refactored code of index node splitting

  • Property mode set to 100644
File size: 22.8 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
[2f2feadb]608static int ext4_directory_dx_split_index(ext4_filesystem_t *fs,
609 ext4_inode_ref_t *inode_ref, ext4_directory_dx_block_t *dx_blocks,
610 ext4_directory_dx_block_t *dx_block)
[565b6ff]611{
[2f2feadb]612 int rc;
[565b6ff]613
[2f2feadb]614 ext4_directory_dx_entry_t *entries;
615 if (dx_block == dx_blocks) {
616 entries = ((ext4_directory_dx_root_t *) dx_block->block->data)->entries;
617 } else {
618 entries = ((ext4_directory_dx_node_t *) dx_block->block->data)->entries;
[565b6ff]619 }
620
[2f2feadb]621 ext4_directory_dx_countlimit_t *countlimit =
622 (ext4_directory_dx_countlimit_t *)entries;
623 uint16_t leaf_limit = ext4_directory_dx_countlimit_get_limit(countlimit);
624 uint16_t leaf_count = ext4_directory_dx_countlimit_get_count(countlimit);
[565b6ff]625
[2f2feadb]626 // Check if is necessary to split index block
[1ebb66f]627 if (leaf_limit == leaf_count) {
[5c83612b]628 EXT4FS_DBG("need to split index block !!!");
629
[c0964cd7]630 unsigned int levels = dx_block - dx_blocks;
631
[2f2feadb]632 ext4_directory_dx_entry_t *root_entries =
633 ((ext4_directory_dx_root_t *)dx_blocks[0].block->data)->entries;
634
635 ext4_directory_dx_countlimit_t *root_countlimit =
636 (ext4_directory_dx_countlimit_t *)root_entries;
637 uint16_t root_limit =
638 ext4_directory_dx_countlimit_get_limit(root_countlimit);
639 uint16_t root_count =
640 ext4_directory_dx_countlimit_get_count(root_countlimit);
[c0964cd7]641
642 if ((levels > 0) && (root_limit == root_count)) {
643 EXT4FS_DBG("Directory index is full");
[2f2feadb]644 return ENOSPC;
[c0964cd7]645 }
646
647 uint32_t new_fblock;
648 uint32_t new_iblock;
[2f2feadb]649 rc = ext4_directory_append_block(fs, inode_ref, &new_fblock, &new_iblock);
[c0964cd7]650 if (rc != EOK) {
[2f2feadb]651 return rc;
[c0964cd7]652 }
653
654 // New block allocated
655 block_t * new_block;
656 rc = block_get(&new_block, fs->device, new_fblock, BLOCK_FLAGS_NOREAD);
657 if (rc != EOK) {
[2f2feadb]658 return rc;
[c0964cd7]659 }
660
[6f731f0a]661 ext4_directory_dx_node_t *new_node = new_block->data;
662 ext4_directory_dx_entry_t *new_entries = new_node->entries;
663
[2f2feadb]664 uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
665
[c0964cd7]666 if (levels > 0) {
[2f2feadb]667 EXT4FS_DBG("split index leaf node");
[c0964cd7]668 uint32_t count_left = leaf_count / 2;
669 uint32_t count_right = leaf_count - count_left;
[2f2feadb]670 uint32_t hash_right =
671 ext4_directory_dx_entry_get_hash(entries + count_left);
[c0964cd7]672
673 memcpy((void *) new_entries, (void *) (entries + count_left),
674 count_right * sizeof(ext4_directory_dx_entry_t));
675
[2f2feadb]676 ext4_directory_dx_countlimit_t *left_countlimit =
677 (ext4_directory_dx_countlimit_t *)entries;
678 ext4_directory_dx_countlimit_t *right_countlimit =
679 (ext4_directory_dx_countlimit_t *)new_entries;
[c0964cd7]680
681 ext4_directory_dx_countlimit_set_count(left_countlimit, count_left);
682 ext4_directory_dx_countlimit_set_count(right_countlimit, count_right);
683
[2f2feadb]684 uint32_t entry_space = block_size - sizeof(ext4_fake_directory_entry_t);
685 uint32_t node_limit = entry_space / sizeof(ext4_directory_dx_entry_t);
686 ext4_directory_dx_countlimit_set_limit(right_countlimit, node_limit);
[c0964cd7]687
[2f2feadb]688 // Which index block is target for new entry
689 uint32_t position_index = (dx_block->position - dx_block->entries);
690 if (position_index >= count_left) {
[c0964cd7]691
[2f2feadb]692 dx_block->block->dirty = true;
693
694 block_t *block_tmp = dx_block->block;
695 dx_block->block = new_block;
696 dx_block->position = new_entries + position_index - count_left;
697 dx_block->entries = new_entries;
698
699 new_block = block_tmp;
700
701 }
[c0964cd7]702
[2f2feadb]703 ext4_directory_dx_insert_entry(dx_blocks, hash_right, new_iblock);
[c0964cd7]704
[2f2feadb]705 return block_put(new_block);
[c0964cd7]706
707 } else {
708 EXT4FS_DBG("create second level");
[6f731f0a]709
710 memcpy((void *) new_entries, (void *) entries,
711 leaf_count * sizeof(ext4_directory_dx_entry_t));
712
713 ext4_directory_dx_countlimit_t *new_countlimit =
714 (ext4_directory_dx_countlimit_t *)new_entries;
715
[2f2feadb]716 uint32_t entry_space = block_size - sizeof(ext4_fake_directory_entry_t);
717 uint32_t node_limit = entry_space / sizeof(ext4_directory_dx_entry_t);
718 ext4_directory_dx_countlimit_set_limit(new_countlimit, node_limit);
[6f731f0a]719
[2f2feadb]720 // Set values in root node
721 ext4_directory_dx_countlimit_t *new_root_countlimit =
722 (ext4_directory_dx_countlimit_t *)entries;
[6f731f0a]723
[2f2feadb]724 ext4_directory_dx_countlimit_set_count(new_root_countlimit, 1);
725 ext4_directory_dx_entry_set_block(entries, new_iblock);
[6f731f0a]726
[2f2feadb]727 ((ext4_directory_dx_root_t *)dx_blocks[0].block->data)->info.indirect_levels = 1;
[6f731f0a]728
[2f2feadb]729 /* Add new access path frame */
730 dx_block = dx_blocks + 1;
731 dx_block->position = dx_block->position - entries + new_entries;
732 dx_block->entries = new_entries;
733 dx_block->block = new_block;
[c0964cd7]734 }
[2f2feadb]735
736 }
737
738 return EOK;
739}
740
741int ext4_directory_dx_add_entry(ext4_filesystem_t *fs,
742 ext4_inode_ref_t *parent, ext4_inode_ref_t *child, const char *name)
743{
744 int rc = EOK;
745 int rc2 = EOK;
746
747 // get direct block 0 (index root)
748 uint32_t root_block_addr;
749 rc = ext4_filesystem_get_inode_data_block_index(fs, parent->inode, 0, &root_block_addr);
750 if (rc != EOK) {
751 return rc;
752 }
753
754 block_t *root_block;
755 rc = block_get(&root_block, fs->device, root_block_addr, BLOCK_FLAGS_NONE);
756 if (rc != EOK) {
757 return rc;
758 }
759
760 uint32_t name_len = strlen(name);
761 ext4_hash_info_t hinfo;
762 rc = ext4_directory_hinfo_init(&hinfo, root_block, fs->superblock, name_len, name);
763 if (rc != EOK) {
764 block_put(root_block);
765 return EXT4_ERR_BAD_DX_DIR;
766 }
767
768 // Hardcoded number 2 means maximum height of index tree !!!
769 ext4_directory_dx_block_t dx_blocks[2];
770 ext4_directory_dx_block_t *dx_block, *dx_it;
771 rc = ext4_directory_dx_get_leaf(&hinfo, fs, parent->inode, root_block, &dx_block, dx_blocks);
772 if (rc != EOK) {
773 rc = EXT4_ERR_BAD_DX_DIR;
774 goto release_index;
775 }
776
777
778 // Try to insert to existing data block
779 uint32_t leaf_block_idx = ext4_directory_dx_entry_get_block(dx_block->position);
780 uint32_t leaf_block_addr;
781 rc = ext4_filesystem_get_inode_data_block_index(fs, parent->inode, leaf_block_idx, &leaf_block_addr);
782 if (rc != EOK) {
783 goto release_index;
784 }
785
786
787 block_t *target_block;
788 rc = block_get(&target_block, fs->device, leaf_block_addr, BLOCK_FLAGS_NONE);
789 if (rc != EOK) {
790 goto release_index;
791 }
792
793
794 rc = ext4_directory_try_insert_entry(fs->superblock, target_block, child, name, name_len);
795 if (rc == EOK) {
796 goto release_target_index;
797 }
798
799 EXT4FS_DBG("no free space found");
800
801 rc = ext4_directory_dx_split_index(fs, parent, dx_blocks, dx_block);
802 if (rc != EOK) {
803 goto release_target_index;
[c4f318d6]804 }
[5c83612b]805
[c4f318d6]806 block_t *new_block = NULL;
807 rc = ext4_directory_dx_split_data(fs, parent, &hinfo, target_block, dx_block, &new_block);
808 if (rc != EOK) {
[e63ce679]809 rc2 = rc;
810 goto release_target_index;
[c4f318d6]811 }
[5c83612b]812
[d0d7afb]813 // Where to save new entry
[1d68621]814 uint32_t new_block_hash = ext4_directory_dx_entry_get_hash(dx_block->position + 1);
[c4f318d6]815 if (hinfo.hash >= new_block_hash) {
[d0d7afb]816 rc = ext4_directory_try_insert_entry(fs->superblock, new_block, child, name, name_len);
[c4f318d6]817 } else {
[d0d7afb]818 rc = ext4_directory_try_insert_entry(fs->superblock, target_block, child, name, name_len);
[c4f318d6]819 }
[5c83612b]820
[60b8b99]821
822
823
[d0d7afb]824 rc = block_put(new_block);
825 if (rc != EOK) {
826 EXT4FS_DBG("error writing new block");
[60b8b99]827 return rc;
[d0d7afb]828 }
[c4f318d6]829
[e63ce679]830release_target_index:
[c4f318d6]831
[d0d7afb]832 rc2 = rc;
[c4f318d6]833
[412f813]834 rc = block_put(target_block);
835 if (rc != EOK) {
[e8d054a]836 EXT4FS_DBG("error writing target block");
[60b8b99]837 return rc;
[412f813]838 }
[c4f318d6]839
[e63ce679]840release_index:
[60b8b99]841
842 if (rc != EOK) {
843 rc2 = rc;
844 }
845
[e63ce679]846 dx_it = dx_blocks;
[c4f318d6]847
848 while (dx_it <= dx_block) {
[412f813]849 rc = block_put(dx_it->block);
850 if (rc != EOK) {
[e8d054a]851 EXT4FS_DBG("error writing index block");
[60b8b99]852 return rc;
[412f813]853 }
[c4f318d6]854 dx_it++;
855 }
856
[d0d7afb]857 return rc2;
[565b6ff]858}
[0dc91833]859
860
861/**
862 * @}
863 */
Note: See TracBrowser for help on using the repository browser.