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
|
---|
35 | * @brief Ext4 directory index operations.
|
---|
36 | */
|
---|
37 |
|
---|
38 | #include <byteorder.h>
|
---|
39 | #include <errno.h>
|
---|
40 | #include <malloc.h>
|
---|
41 | #include <sort.h>
|
---|
42 | #include <string.h>
|
---|
43 | #include "libext4.h"
|
---|
44 |
|
---|
45 |
|
---|
46 | uint8_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 |
|
---|
52 | void 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 |
|
---|
58 | uint8_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 |
|
---|
64 | void 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 |
|
---|
70 | uint8_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 |
|
---|
76 | void 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 |
|
---|
82 | uint16_t ext4_directory_dx_countlimit_get_limit(
|
---|
83 | ext4_directory_dx_countlimit_t *countlimit)
|
---|
84 | {
|
---|
85 | return uint16_t_le2host(countlimit->limit);
|
---|
86 | }
|
---|
87 |
|
---|
88 | void 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 |
|
---|
94 | uint16_t ext4_directory_dx_countlimit_get_count(
|
---|
95 | ext4_directory_dx_countlimit_t *countlimit)
|
---|
96 | {
|
---|
97 | return uint16_t_le2host(countlimit->count);
|
---|
98 | }
|
---|
99 |
|
---|
100 | void 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 |
|
---|
106 | uint32_t ext4_directory_dx_entry_get_hash(ext4_directory_dx_entry_t *entry)
|
---|
107 | {
|
---|
108 | return uint32_t_le2host(entry->hash);
|
---|
109 | }
|
---|
110 |
|
---|
111 | void 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 |
|
---|
117 | uint32_t ext4_directory_dx_entry_get_block(ext4_directory_dx_entry_t *entry)
|
---|
118 | {
|
---|
119 | return uint32_t_le2host(entry->block);
|
---|
120 | }
|
---|
121 |
|
---|
122 | void 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 |
|
---|
132 | static 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 |
|
---|
136 | ext4_directory_dx_root_t *root = (ext4_directory_dx_root_t *)root_block->data;
|
---|
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 |
|
---|
156 | uint32_t block_size = ext4_superblock_get_block_size(sb);
|
---|
157 |
|
---|
158 | uint32_t entry_space = block_size;
|
---|
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 |
|
---|
163 | uint16_t limit = ext4_directory_dx_countlimit_get_limit((ext4_directory_dx_countlimit_t *)&root->entries);
|
---|
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 |
|
---|
184 | static 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;
|
---|
189 |
|
---|
190 | ext4_directory_dx_block_t *tmp_dx_block = dx_blocks;
|
---|
191 |
|
---|
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;
|
---|
194 |
|
---|
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);
|
---|
197 |
|
---|
198 | block_t *tmp_block = root_block;
|
---|
199 | ext4_directory_dx_entry_t *p, *q, *m, *at;
|
---|
200 | while (true) {
|
---|
201 |
|
---|
202 | uint16_t count = ext4_directory_dx_countlimit_get_count((ext4_directory_dx_countlimit_t *)entries);
|
---|
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 |
|
---|
230 | uint32_t next_block = ext4_directory_dx_entry_get_block(at);
|
---|
231 |
|
---|
232 | indirect_level--;
|
---|
233 |
|
---|
234 | uint32_t fblock;
|
---|
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 |
|
---|
248 | uint16_t entry_space = ext4_superblock_get_block_size(fs->superblock)
|
---|
249 | - sizeof(ext4_directory_dx_dot_entry_t);
|
---|
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 |
|
---|
266 | static 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 | {
|
---|
269 | int rc;
|
---|
270 |
|
---|
271 |
|
---|
272 | uint32_t num_handles = 0;
|
---|
273 | ext4_directory_dx_block_t *p = handle;
|
---|
274 |
|
---|
275 | while (1) {
|
---|
276 |
|
---|
277 | p->position++;
|
---|
278 | uint16_t count = ext4_directory_dx_countlimit_get_count((ext4_directory_dx_countlimit_t *)p->entries);
|
---|
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 |
|
---|
292 | uint32_t current_hash = ext4_directory_dx_entry_get_hash(p->position);
|
---|
293 |
|
---|
294 | if ((hash & 1) == 0) {
|
---|
295 | if ((current_hash & ~1) != hash) {
|
---|
296 | return 0;
|
---|
297 | }
|
---|
298 | }
|
---|
299 |
|
---|
300 |
|
---|
301 | while (num_handles--) {
|
---|
302 |
|
---|
303 | uint32_t block_idx = ext4_directory_dx_entry_get_block(p->position);
|
---|
304 | uint32_t block_addr;
|
---|
305 | rc = ext4_filesystem_get_inode_data_block_index(fs, inode, block_idx, &block_addr);
|
---|
306 | if (rc != EOK) {
|
---|
307 | return rc;
|
---|
308 | }
|
---|
309 |
|
---|
310 | block_t *block;
|
---|
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 |
|
---|
328 | int 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)
|
---|
330 | {
|
---|
331 | int rc;
|
---|
332 |
|
---|
333 | // get direct block 0 (index root)
|
---|
334 | uint32_t root_block_addr;
|
---|
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 |
|
---|
340 | block_t *root_block;
|
---|
341 | rc = block_get(&root_block, fs->device, root_block_addr, BLOCK_FLAGS_NONE);
|
---|
342 | if (rc != EOK) {
|
---|
343 | return rc;
|
---|
344 | }
|
---|
345 |
|
---|
346 | ext4_hash_info_t hinfo;
|
---|
347 | rc = ext4_directory_hinfo_init(&hinfo, root_block, fs->superblock, name_len, name);
|
---|
348 | if (rc != EOK) {
|
---|
349 | block_put(root_block);
|
---|
350 | return EXT4_ERR_BAD_DX_DIR;
|
---|
351 | }
|
---|
352 |
|
---|
353 | // Hardcoded number 2 means maximum height of index tree !!!
|
---|
354 | ext4_directory_dx_block_t dx_blocks[2];
|
---|
355 | ext4_directory_dx_block_t *dx_block, *tmp;
|
---|
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 |
|
---|
363 | do {
|
---|
364 |
|
---|
365 | uint32_t leaf_block_idx = ext4_directory_dx_entry_get_block(dx_block->position);
|
---|
366 | uint32_t leaf_block_addr;
|
---|
367 | rc = ext4_filesystem_get_inode_data_block_index(fs, inode_ref->inode, leaf_block_idx, &leaf_block_addr);
|
---|
368 | if (rc != EOK) {
|
---|
369 | goto cleanup;
|
---|
370 | }
|
---|
371 |
|
---|
372 | block_t *leaf_block;
|
---|
373 | rc = block_get(&leaf_block, fs->device, leaf_block_addr, BLOCK_FLAGS_NONE);
|
---|
374 | if (rc != EOK) {
|
---|
375 | goto cleanup;
|
---|
376 | }
|
---|
377 |
|
---|
378 | ext4_directory_entry_ll_t *res_dentry;
|
---|
379 | rc = ext4_directory_find_in_block(leaf_block, fs->superblock, name_len, name, &res_dentry);
|
---|
380 |
|
---|
381 | // Found => return it
|
---|
382 | if (rc == EOK) {
|
---|
383 | result->block = leaf_block;
|
---|
384 | result->dentry = res_dentry;
|
---|
385 | goto cleanup;
|
---|
386 | }
|
---|
387 |
|
---|
388 | // Not found, leave untouched
|
---|
389 | block_put(leaf_block);
|
---|
390 |
|
---|
391 | if (rc != ENOENT) {
|
---|
392 | goto cleanup;
|
---|
393 | }
|
---|
394 |
|
---|
395 | rc = ext4_directory_dx_next_block(fs, inode_ref->inode, hinfo.hash, dx_block, &dx_blocks[0]);
|
---|
396 | if (rc < 0) {
|
---|
397 | goto cleanup;
|
---|
398 | }
|
---|
399 |
|
---|
400 | } while (rc == 1);
|
---|
401 |
|
---|
402 | rc = ENOENT;
|
---|
403 |
|
---|
404 | cleanup:
|
---|
405 |
|
---|
406 | tmp = dx_blocks;
|
---|
407 | while (tmp <= dx_block) {
|
---|
408 | block_put(tmp->block);
|
---|
409 | ++tmp;
|
---|
410 | }
|
---|
411 | return rc;
|
---|
412 | }
|
---|
413 |
|
---|
414 | typedef struct ext4_dx_sort_entry {
|
---|
415 | uint32_t hash;
|
---|
416 | uint32_t rec_len;
|
---|
417 | void *dentry;
|
---|
418 | } ext4_dx_sort_entry_t;
|
---|
419 |
|
---|
420 | static 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 |
|
---|
437 | static 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 |
|
---|
460 | static int ext4_directory_dx_split_data(ext4_filesystem_t *fs,
|
---|
461 | ext4_inode_ref_t *inode_ref, ext4_hash_info_t *hinfo,
|
---|
462 | block_t *old_data_block, ext4_directory_dx_block_t *index_block, block_t **new_data_block)
|
---|
463 | {
|
---|
464 | int rc = EOK;
|
---|
465 |
|
---|
466 | // Allocate buffer for directory entries
|
---|
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 |
|
---|
473 | // dot entry has the smallest size available
|
---|
474 | uint32_t max_entry_count = block_size / sizeof(ext4_directory_dx_dot_entry_t);
|
---|
475 |
|
---|
476 | // Allocate sort entry
|
---|
477 | ext4_dx_sort_entry_t *sort_array = malloc(max_entry_count * sizeof(ext4_dx_sort_entry_t));
|
---|
478 | if (sort_array == NULL) {
|
---|
479 | free(entry_buffer);
|
---|
480 | return ENOMEM;
|
---|
481 | }
|
---|
482 |
|
---|
483 | uint32_t idx = 0;
|
---|
484 | uint32_t real_size = 0;
|
---|
485 |
|
---|
486 | // Initialize hinfo
|
---|
487 | ext4_hash_info_t tmp_hinfo;
|
---|
488 | memcpy(&tmp_hinfo, hinfo, sizeof(ext4_hash_info_t));
|
---|
489 |
|
---|
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;
|
---|
493 | while ((void *)dentry < old_data_block->data + block_size) {
|
---|
494 |
|
---|
495 | // Read only valid entries
|
---|
496 | if (ext4_directory_entry_ll_get_inode(dentry) != 0) {
|
---|
497 |
|
---|
498 | uint8_t len = ext4_directory_entry_ll_get_name_length(fs->superblock, dentry);
|
---|
499 | ext4_hash_string(&tmp_hinfo, len, (char *)dentry->name);
|
---|
500 |
|
---|
501 | uint32_t rec_len = 8 + len;
|
---|
502 |
|
---|
503 | if ((rec_len % 4) != 0) {
|
---|
504 | rec_len += 4 - (rec_len % 4);
|
---|
505 | }
|
---|
506 |
|
---|
507 | memcpy(entry_buffer_ptr, dentry, rec_len);
|
---|
508 |
|
---|
509 | sort_array[idx].dentry = entry_buffer_ptr;
|
---|
510 | sort_array[idx].rec_len = rec_len;
|
---|
511 | sort_array[idx].hash = tmp_hinfo.hash;
|
---|
512 |
|
---|
513 | entry_buffer_ptr += rec_len;
|
---|
514 | real_size += rec_len;
|
---|
515 | idx++;
|
---|
516 | }
|
---|
517 |
|
---|
518 | dentry = (void *)dentry + ext4_directory_entry_ll_get_entry_length(dentry);
|
---|
519 | }
|
---|
520 |
|
---|
521 | qsort(sort_array, idx, sizeof(ext4_dx_sort_entry_t), dx_entry_comparator, NULL);
|
---|
522 |
|
---|
523 | uint32_t new_fblock;
|
---|
524 | uint32_t new_iblock;
|
---|
525 | rc = ext4_directory_append_block(fs, inode_ref, &new_fblock, &new_iblock);
|
---|
526 | if (rc != EOK) {
|
---|
527 | free(sort_array);
|
---|
528 | free(entry_buffer);
|
---|
529 | return rc;
|
---|
530 | }
|
---|
531 |
|
---|
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;
|
---|
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)) {
|
---|
547 | new_hash = sort_array[i].hash;
|
---|
548 | mid = i;
|
---|
549 | break;
|
---|
550 | }
|
---|
551 |
|
---|
552 | current_size += sort_array[i].rec_len;
|
---|
553 | }
|
---|
554 |
|
---|
555 | uint32_t continued = 0;
|
---|
556 | if (new_hash == sort_array[mid-1].hash) {
|
---|
557 | continued = 1;
|
---|
558 | }
|
---|
559 |
|
---|
560 | uint32_t offset = 0;
|
---|
561 | void *ptr;
|
---|
562 |
|
---|
563 | // First part - to the old block
|
---|
564 | for (uint32_t i = 0; i < mid; ++i) {
|
---|
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;
|
---|
580 | for (uint32_t i = mid; i < idx; ++i) {
|
---|
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);
|
---|
599 |
|
---|
600 | ext4_directory_dx_insert_entry(index_block, new_hash + continued, new_iblock);
|
---|
601 |
|
---|
602 | *new_data_block = new_data_block_tmp;
|
---|
603 |
|
---|
604 | return EOK;
|
---|
605 | }
|
---|
606 |
|
---|
607 |
|
---|
608 | static 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)
|
---|
611 | {
|
---|
612 | int rc;
|
---|
613 |
|
---|
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;
|
---|
619 | }
|
---|
620 |
|
---|
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);
|
---|
625 |
|
---|
626 | // Check if is necessary to split index block
|
---|
627 | if (leaf_limit == leaf_count) {
|
---|
628 | EXT4FS_DBG("need to split index block !!!");
|
---|
629 |
|
---|
630 | unsigned int levels = dx_block - dx_blocks;
|
---|
631 |
|
---|
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);
|
---|
641 |
|
---|
642 | if ((levels > 0) && (root_limit == root_count)) {
|
---|
643 | EXT4FS_DBG("Directory index is full");
|
---|
644 | return ENOSPC;
|
---|
645 | }
|
---|
646 |
|
---|
647 | uint32_t new_fblock;
|
---|
648 | uint32_t new_iblock;
|
---|
649 | rc = ext4_directory_append_block(fs, inode_ref, &new_fblock, &new_iblock);
|
---|
650 | if (rc != EOK) {
|
---|
651 | return rc;
|
---|
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) {
|
---|
658 | return rc;
|
---|
659 | }
|
---|
660 |
|
---|
661 | ext4_directory_dx_node_t *new_node = new_block->data;
|
---|
662 | ext4_directory_dx_entry_t *new_entries = new_node->entries;
|
---|
663 |
|
---|
664 | uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
|
---|
665 |
|
---|
666 | if (levels > 0) {
|
---|
667 | EXT4FS_DBG("split index leaf node");
|
---|
668 | uint32_t count_left = leaf_count / 2;
|
---|
669 | uint32_t count_right = leaf_count - count_left;
|
---|
670 | uint32_t hash_right =
|
---|
671 | ext4_directory_dx_entry_get_hash(entries + count_left);
|
---|
672 |
|
---|
673 | memcpy((void *) new_entries, (void *) (entries + count_left),
|
---|
674 | count_right * sizeof(ext4_directory_dx_entry_t));
|
---|
675 |
|
---|
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;
|
---|
680 |
|
---|
681 | ext4_directory_dx_countlimit_set_count(left_countlimit, count_left);
|
---|
682 | ext4_directory_dx_countlimit_set_count(right_countlimit, count_right);
|
---|
683 |
|
---|
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);
|
---|
687 |
|
---|
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) {
|
---|
691 |
|
---|
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 | }
|
---|
702 |
|
---|
703 | ext4_directory_dx_insert_entry(dx_blocks, hash_right, new_iblock);
|
---|
704 |
|
---|
705 | return block_put(new_block);
|
---|
706 |
|
---|
707 | } else {
|
---|
708 | EXT4FS_DBG("create second level");
|
---|
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 |
|
---|
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);
|
---|
719 |
|
---|
720 | // Set values in root node
|
---|
721 | ext4_directory_dx_countlimit_t *new_root_countlimit =
|
---|
722 | (ext4_directory_dx_countlimit_t *)entries;
|
---|
723 |
|
---|
724 | ext4_directory_dx_countlimit_set_count(new_root_countlimit, 1);
|
---|
725 | ext4_directory_dx_entry_set_block(entries, new_iblock);
|
---|
726 |
|
---|
727 | ((ext4_directory_dx_root_t *)dx_blocks[0].block->data)->info.indirect_levels = 1;
|
---|
728 |
|
---|
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;
|
---|
734 | }
|
---|
735 |
|
---|
736 | }
|
---|
737 |
|
---|
738 | return EOK;
|
---|
739 | }
|
---|
740 |
|
---|
741 | int 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;
|
---|
804 | }
|
---|
805 |
|
---|
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) {
|
---|
809 | rc2 = rc;
|
---|
810 | goto release_target_index;
|
---|
811 | }
|
---|
812 |
|
---|
813 | // Where to save new entry
|
---|
814 | uint32_t new_block_hash = ext4_directory_dx_entry_get_hash(dx_block->position + 1);
|
---|
815 | if (hinfo.hash >= new_block_hash) {
|
---|
816 | rc = ext4_directory_try_insert_entry(fs->superblock, new_block, child, name, name_len);
|
---|
817 | } else {
|
---|
818 | rc = ext4_directory_try_insert_entry(fs->superblock, target_block, child, name, name_len);
|
---|
819 | }
|
---|
820 |
|
---|
821 |
|
---|
822 |
|
---|
823 |
|
---|
824 | rc = block_put(new_block);
|
---|
825 | if (rc != EOK) {
|
---|
826 | EXT4FS_DBG("error writing new block");
|
---|
827 | return rc;
|
---|
828 | }
|
---|
829 |
|
---|
830 | release_target_index:
|
---|
831 |
|
---|
832 | rc2 = rc;
|
---|
833 |
|
---|
834 | rc = block_put(target_block);
|
---|
835 | if (rc != EOK) {
|
---|
836 | EXT4FS_DBG("error writing target block");
|
---|
837 | return rc;
|
---|
838 | }
|
---|
839 |
|
---|
840 | release_index:
|
---|
841 |
|
---|
842 | if (rc != EOK) {
|
---|
843 | rc2 = rc;
|
---|
844 | }
|
---|
845 |
|
---|
846 | dx_it = dx_blocks;
|
---|
847 |
|
---|
848 | while (dx_it <= dx_block) {
|
---|
849 | rc = block_put(dx_it->block);
|
---|
850 | if (rc != EOK) {
|
---|
851 | EXT4FS_DBG("error writing index block");
|
---|
852 | return rc;
|
---|
853 | }
|
---|
854 | dx_it++;
|
---|
855 | }
|
---|
856 |
|
---|
857 | return rc2;
|
---|
858 | }
|
---|
859 |
|
---|
860 |
|
---|
861 | /**
|
---|
862 | * @}
|
---|
863 | */
|
---|