1 | /*
|
---|
2 | * Copyright (c) 2012 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 | /** Type entry to pass to sorting algorithm.
|
---|
46 | *
|
---|
47 | */
|
---|
48 | typedef struct ext4_dx_sort_entry {
|
---|
49 | uint32_t hash;
|
---|
50 | uint32_t rec_len;
|
---|
51 | void *dentry;
|
---|
52 | } ext4_dx_sort_entry_t;
|
---|
53 |
|
---|
54 |
|
---|
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 | */
|
---|
60 | uint8_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 |
|
---|
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 | */
|
---|
71 | void 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 |
|
---|
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 | */
|
---|
82 | uint8_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 |
|
---|
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 | */
|
---|
93 | void 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 |
|
---|
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 | */
|
---|
104 | uint8_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 |
|
---|
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 | */
|
---|
115 | void 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 |
|
---|
121 | /** Get maximum number of index node entries.
|
---|
122 | *
|
---|
123 | * @param countlimit pointer to counlimit structure
|
---|
124 | * @return maximum of entries in node
|
---|
125 | */
|
---|
126 | uint16_t ext4_directory_dx_countlimit_get_limit(
|
---|
127 | ext4_directory_dx_countlimit_t *countlimit)
|
---|
128 | {
|
---|
129 | return uint16_t_le2host(countlimit->limit);
|
---|
130 | }
|
---|
131 |
|
---|
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 | */
|
---|
137 | void 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 |
|
---|
143 | /** Get current number of index node entries.
|
---|
144 | *
|
---|
145 | * @param countlimit pointer to counlimit structure
|
---|
146 | * @return number of entries in node
|
---|
147 | */
|
---|
148 | uint16_t ext4_directory_dx_countlimit_get_count(
|
---|
149 | ext4_directory_dx_countlimit_t *countlimit)
|
---|
150 | {
|
---|
151 | return uint16_t_le2host(countlimit->count);
|
---|
152 | }
|
---|
153 |
|
---|
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 | */
|
---|
159 | void 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 |
|
---|
165 | /** Get hash value of index entry.
|
---|
166 | *
|
---|
167 | * @param entry pointer to index entry
|
---|
168 | * @return hash value
|
---|
169 | */
|
---|
170 | uint32_t ext4_directory_dx_entry_get_hash(ext4_directory_dx_entry_t *entry)
|
---|
171 | {
|
---|
172 | return uint32_t_le2host(entry->hash);
|
---|
173 | }
|
---|
174 |
|
---|
175 |
|
---|
176 | /** Set hash value of index entry.
|
---|
177 | *
|
---|
178 | * @param entry pointer to index entry
|
---|
179 | * @param hash hash value
|
---|
180 | */
|
---|
181 | void 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 |
|
---|
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 | */
|
---|
192 | uint32_t ext4_directory_dx_entry_get_block(ext4_directory_dx_entry_t *entry)
|
---|
193 | {
|
---|
194 | return uint32_t_le2host(entry->block);
|
---|
195 | }
|
---|
196 |
|
---|
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 | */
|
---|
202 | void 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 |
|
---|
211 | /** TODO comment all function
|
---|
212 | *
|
---|
213 | */
|
---|
214 | int ext4_directory_dx_init(ext4_inode_ref_t *dir)
|
---|
215 | {
|
---|
216 | int rc;
|
---|
217 |
|
---|
218 | uint32_t fblock;
|
---|
219 | rc = ext4_filesystem_get_inode_data_block_index(dir, 0, &fblock);
|
---|
220 | if (rc != EOK) {
|
---|
221 | return rc;
|
---|
222 | }
|
---|
223 |
|
---|
224 | block_t *block;
|
---|
225 | rc = block_get(&block, dir->fs->device, fblock, BLOCK_FLAGS_NONE);
|
---|
226 | if (rc != EOK) {
|
---|
227 | return rc;
|
---|
228 | }
|
---|
229 |
|
---|
230 | EXT4FS_DBG("fblock = \%u", fblock);
|
---|
231 |
|
---|
232 | ext4_directory_dx_root_t *root = block->data;
|
---|
233 |
|
---|
234 | ext4_directory_entry_ll_t *dot = (ext4_directory_entry_ll_t *)&root->dots[0];
|
---|
235 | ext4_directory_entry_ll_t *dot_dot = (ext4_directory_entry_ll_t *)&root->dots[1];
|
---|
236 |
|
---|
237 |
|
---|
238 | // TODO why the commented lines??
|
---|
239 | EXT4FS_DBG("dot len = \%u, dotdot len = \%u", ext4_directory_entry_ll_get_entry_length(dot), ext4_directory_entry_ll_get_entry_length(dot_dot));
|
---|
240 |
|
---|
241 | // uint32_t block_size = ext4_superblock_get_block_size(dir->fs->superblock);
|
---|
242 | // uint16_t len = block_size - sizeof(ext4_directory_dx_dot_entry_t);
|
---|
243 |
|
---|
244 | // ext4_directory_entry_ll_set_entry_length(dot_dot, len);
|
---|
245 |
|
---|
246 | ext4_directory_dx_root_info_t *info = &(root->info);
|
---|
247 |
|
---|
248 | uint8_t hash_version =
|
---|
249 | ext4_superblock_get_default_hash_version(dir->fs->superblock);
|
---|
250 |
|
---|
251 | ext4_directory_dx_root_info_set_hash_version(info, hash_version);
|
---|
252 | ext4_directory_dx_root_info_set_indirect_levels(info, 0);
|
---|
253 | ext4_directory_dx_root_info_set_info_length(info, 8);
|
---|
254 |
|
---|
255 | ext4_directory_dx_countlimit_t *countlimit =
|
---|
256 | (ext4_directory_dx_countlimit_t *)&root->entries;
|
---|
257 | ext4_directory_dx_countlimit_set_count(countlimit, 1);
|
---|
258 |
|
---|
259 | uint32_t block_size = ext4_superblock_get_block_size(dir->fs->superblock);
|
---|
260 | uint32_t entry_space = block_size - 2 * sizeof(ext4_directory_dx_dot_entry_t)
|
---|
261 | - sizeof(ext4_directory_dx_root_info_t);
|
---|
262 | uint16_t root_limit = entry_space / sizeof(ext4_directory_dx_entry_t);
|
---|
263 | ext4_directory_dx_countlimit_set_limit(countlimit, root_limit);
|
---|
264 |
|
---|
265 | uint32_t iblock;
|
---|
266 | rc = ext4_filesystem_append_inode_block(dir, &fblock, &iblock);
|
---|
267 | if (rc != EOK) {
|
---|
268 | block_put(block);
|
---|
269 | return rc;
|
---|
270 | }
|
---|
271 |
|
---|
272 | block_t *new_block;
|
---|
273 | rc = block_get(&new_block, dir->fs->device, fblock, BLOCK_FLAGS_NOREAD);
|
---|
274 | if (rc != EOK) {
|
---|
275 | block_put(block);
|
---|
276 | return rc;
|
---|
277 | }
|
---|
278 |
|
---|
279 | ext4_directory_entry_ll_t *block_entry = new_block->data;
|
---|
280 | ext4_directory_entry_ll_set_entry_length(block_entry, block_size);
|
---|
281 | ext4_directory_entry_ll_set_inode(block_entry, 0);
|
---|
282 |
|
---|
283 | new_block->dirty = true;
|
---|
284 | rc = block_put(new_block);
|
---|
285 | if (rc != EOK) {
|
---|
286 | block_put(block);
|
---|
287 | return rc;
|
---|
288 | }
|
---|
289 |
|
---|
290 | ext4_directory_dx_entry_t *entry = root->entries;
|
---|
291 | ext4_directory_dx_entry_set_block(entry, iblock);
|
---|
292 |
|
---|
293 | block->dirty = true;
|
---|
294 |
|
---|
295 | rc = block_put(block);
|
---|
296 | if (rc != EOK) {
|
---|
297 | return rc;
|
---|
298 | }
|
---|
299 |
|
---|
300 | return EOK;
|
---|
301 | }
|
---|
302 |
|
---|
303 | static int ext4_directory_hinfo_init(ext4_hash_info_t *hinfo, block_t *root_block,
|
---|
304 | ext4_superblock_t *sb, size_t name_len, const char *name)
|
---|
305 | {
|
---|
306 |
|
---|
307 | ext4_directory_dx_root_t *root = (ext4_directory_dx_root_t *)root_block->data;
|
---|
308 |
|
---|
309 | if (root->info.hash_version != EXT4_HASH_VERSION_TEA &&
|
---|
310 | root->info.hash_version != EXT4_HASH_VERSION_HALF_MD4 &&
|
---|
311 | root->info.hash_version != EXT4_HASH_VERSION_LEGACY) {
|
---|
312 | return EXT4_ERR_BAD_DX_DIR;
|
---|
313 | }
|
---|
314 |
|
---|
315 | // Check unused flags
|
---|
316 | if (root->info.unused_flags != 0) {
|
---|
317 | EXT4FS_DBG("ERR: unused_flags = \%u", root->info.unused_flags);
|
---|
318 | return EXT4_ERR_BAD_DX_DIR;
|
---|
319 | }
|
---|
320 |
|
---|
321 | // Check indirect levels
|
---|
322 | if (root->info.indirect_levels > 1) {
|
---|
323 | EXT4FS_DBG("ERR: indirect_levels = \%u", root->info.indirect_levels);
|
---|
324 | return EXT4_ERR_BAD_DX_DIR;
|
---|
325 | }
|
---|
326 |
|
---|
327 | uint32_t block_size = ext4_superblock_get_block_size(sb);
|
---|
328 |
|
---|
329 | uint32_t entry_space = block_size;
|
---|
330 | entry_space -= 2 * sizeof(ext4_directory_dx_dot_entry_t);
|
---|
331 | entry_space -= sizeof(ext4_directory_dx_root_info_t);
|
---|
332 | entry_space = entry_space / sizeof(ext4_directory_dx_entry_t);
|
---|
333 |
|
---|
334 | uint16_t limit = ext4_directory_dx_countlimit_get_limit((ext4_directory_dx_countlimit_t *)&root->entries);
|
---|
335 | if (limit != entry_space) {
|
---|
336 | return EXT4_ERR_BAD_DX_DIR;
|
---|
337 | }
|
---|
338 |
|
---|
339 | hinfo->hash_version = ext4_directory_dx_root_info_get_hash_version(&root->info);
|
---|
340 | if ((hinfo->hash_version <= EXT4_HASH_VERSION_TEA)
|
---|
341 | && (ext4_superblock_has_flag(sb, EXT4_SUPERBLOCK_FLAGS_UNSIGNED_HASH))) {
|
---|
342 | // 3 is magic from ext4 linux implementation
|
---|
343 | hinfo->hash_version += 3;
|
---|
344 | }
|
---|
345 |
|
---|
346 | hinfo->seed = ext4_superblock_get_hash_seed(sb);
|
---|
347 |
|
---|
348 | if (name) {
|
---|
349 | ext4_hash_string(hinfo, name_len, name);
|
---|
350 | }
|
---|
351 |
|
---|
352 | return EOK;
|
---|
353 | }
|
---|
354 |
|
---|
355 | static int ext4_directory_dx_get_leaf(ext4_hash_info_t *hinfo,
|
---|
356 | ext4_inode_ref_t *inode_ref, block_t *root_block,
|
---|
357 | ext4_directory_dx_block_t **dx_block, ext4_directory_dx_block_t *dx_blocks)
|
---|
358 | {
|
---|
359 | int rc;
|
---|
360 |
|
---|
361 | ext4_directory_dx_block_t *tmp_dx_block = dx_blocks;
|
---|
362 |
|
---|
363 | ext4_directory_dx_root_t *root = (ext4_directory_dx_root_t *)root_block->data;
|
---|
364 | ext4_directory_dx_entry_t *entries = (ext4_directory_dx_entry_t *)&root->entries;
|
---|
365 |
|
---|
366 | uint16_t limit = ext4_directory_dx_countlimit_get_limit((ext4_directory_dx_countlimit_t *)entries);
|
---|
367 | uint8_t indirect_level = ext4_directory_dx_root_info_get_indirect_levels(&root->info);
|
---|
368 |
|
---|
369 | block_t *tmp_block = root_block;
|
---|
370 | ext4_directory_dx_entry_t *p, *q, *m, *at;
|
---|
371 | while (true) {
|
---|
372 |
|
---|
373 | uint16_t count = ext4_directory_dx_countlimit_get_count((ext4_directory_dx_countlimit_t *)entries);
|
---|
374 | if ((count == 0) || (count > limit)) {
|
---|
375 | return EXT4_ERR_BAD_DX_DIR;
|
---|
376 | }
|
---|
377 |
|
---|
378 | p = entries + 1;
|
---|
379 | q = entries + count - 1;
|
---|
380 |
|
---|
381 | while (p <= q) {
|
---|
382 | m = p + (q - p) / 2;
|
---|
383 | if (ext4_directory_dx_entry_get_hash(m) > hinfo->hash) {
|
---|
384 | q = m - 1;
|
---|
385 | } else {
|
---|
386 | p = m + 1;
|
---|
387 | }
|
---|
388 | }
|
---|
389 |
|
---|
390 | at = p - 1;
|
---|
391 |
|
---|
392 | tmp_dx_block->block = tmp_block;
|
---|
393 | tmp_dx_block->entries = entries;
|
---|
394 | tmp_dx_block->position = at;
|
---|
395 |
|
---|
396 | if (indirect_level == 0) {
|
---|
397 | *dx_block = tmp_dx_block;
|
---|
398 | return EOK;
|
---|
399 | }
|
---|
400 |
|
---|
401 | uint32_t next_block = ext4_directory_dx_entry_get_block(at);
|
---|
402 |
|
---|
403 | indirect_level--;
|
---|
404 |
|
---|
405 | uint32_t fblock;
|
---|
406 | rc = ext4_filesystem_get_inode_data_block_index(
|
---|
407 | inode_ref, next_block, &fblock);
|
---|
408 | if (rc != EOK) {
|
---|
409 | return rc;
|
---|
410 | }
|
---|
411 |
|
---|
412 | rc = block_get(&tmp_block, inode_ref->fs->device, fblock, BLOCK_FLAGS_NONE);
|
---|
413 | if (rc != EOK) {
|
---|
414 | return rc;
|
---|
415 | }
|
---|
416 |
|
---|
417 | entries = ((ext4_directory_dx_node_t *) tmp_block->data)->entries;
|
---|
418 | limit = ext4_directory_dx_countlimit_get_limit(
|
---|
419 | (ext4_directory_dx_countlimit_t *)entries);
|
---|
420 |
|
---|
421 | uint16_t entry_space = ext4_superblock_get_block_size(inode_ref->fs->superblock)
|
---|
422 | - sizeof(ext4_directory_dx_dot_entry_t);
|
---|
423 | entry_space = entry_space / sizeof(ext4_directory_dx_entry_t);
|
---|
424 |
|
---|
425 |
|
---|
426 | if (limit != entry_space) {
|
---|
427 | block_put(tmp_block);
|
---|
428 | return EXT4_ERR_BAD_DX_DIR;
|
---|
429 | }
|
---|
430 |
|
---|
431 | ++tmp_dx_block;
|
---|
432 | }
|
---|
433 |
|
---|
434 | // Unreachable
|
---|
435 | return EOK;
|
---|
436 | }
|
---|
437 |
|
---|
438 |
|
---|
439 | static int ext4_directory_dx_next_block(ext4_inode_ref_t *inode_ref, uint32_t hash,
|
---|
440 | ext4_directory_dx_block_t *handle, ext4_directory_dx_block_t *handles)
|
---|
441 | {
|
---|
442 | int rc;
|
---|
443 |
|
---|
444 |
|
---|
445 | uint32_t num_handles = 0;
|
---|
446 | ext4_directory_dx_block_t *p = handle;
|
---|
447 |
|
---|
448 | while (1) {
|
---|
449 |
|
---|
450 | p->position++;
|
---|
451 | uint16_t count = ext4_directory_dx_countlimit_get_count((ext4_directory_dx_countlimit_t *)p->entries);
|
---|
452 |
|
---|
453 | if (p->position < p->entries + count) {
|
---|
454 | break;
|
---|
455 | }
|
---|
456 |
|
---|
457 | if (p == handles) {
|
---|
458 | return 0;
|
---|
459 | }
|
---|
460 |
|
---|
461 | num_handles++;
|
---|
462 | p--;
|
---|
463 | }
|
---|
464 |
|
---|
465 | uint32_t current_hash = ext4_directory_dx_entry_get_hash(p->position);
|
---|
466 |
|
---|
467 | if ((hash & 1) == 0) {
|
---|
468 | if ((current_hash & ~1) != hash) {
|
---|
469 | return 0;
|
---|
470 | }
|
---|
471 | }
|
---|
472 |
|
---|
473 |
|
---|
474 | while (num_handles--) {
|
---|
475 |
|
---|
476 | uint32_t block_idx = ext4_directory_dx_entry_get_block(p->position);
|
---|
477 | uint32_t block_addr;
|
---|
478 | rc = ext4_filesystem_get_inode_data_block_index(inode_ref, block_idx, &block_addr);
|
---|
479 | if (rc != EOK) {
|
---|
480 | return rc;
|
---|
481 | }
|
---|
482 |
|
---|
483 | block_t *block;
|
---|
484 | rc = block_get(&block, inode_ref->fs->device, block_addr, BLOCK_FLAGS_NONE);
|
---|
485 | if (rc != EOK) {
|
---|
486 | return rc;
|
---|
487 | }
|
---|
488 |
|
---|
489 | p++;
|
---|
490 |
|
---|
491 | block_put(p->block);
|
---|
492 | p->block = block;
|
---|
493 | p->entries = ((ext4_directory_dx_node_t *) block->data)->entries;
|
---|
494 | p->position = p->entries;
|
---|
495 | }
|
---|
496 |
|
---|
497 | return 1;
|
---|
498 |
|
---|
499 | }
|
---|
500 |
|
---|
501 | int ext4_directory_dx_find_entry(ext4_directory_search_result_t *result,
|
---|
502 | ext4_inode_ref_t *inode_ref, size_t name_len, const char *name)
|
---|
503 | {
|
---|
504 | int rc;
|
---|
505 |
|
---|
506 | // get direct block 0 (index root)
|
---|
507 | uint32_t root_block_addr;
|
---|
508 | rc = ext4_filesystem_get_inode_data_block_index(inode_ref, 0, &root_block_addr);
|
---|
509 | if (rc != EOK) {
|
---|
510 | return rc;
|
---|
511 | }
|
---|
512 |
|
---|
513 | ext4_filesystem_t *fs = inode_ref->fs;
|
---|
514 |
|
---|
515 | block_t *root_block;
|
---|
516 | rc = block_get(&root_block, fs->device, root_block_addr, BLOCK_FLAGS_NONE);
|
---|
517 | if (rc != EOK) {
|
---|
518 | return rc;
|
---|
519 | }
|
---|
520 |
|
---|
521 | ext4_hash_info_t hinfo;
|
---|
522 | rc = ext4_directory_hinfo_init(&hinfo, root_block, fs->superblock, name_len, name);
|
---|
523 | if (rc != EOK) {
|
---|
524 | block_put(root_block);
|
---|
525 | return EXT4_ERR_BAD_DX_DIR;
|
---|
526 | }
|
---|
527 |
|
---|
528 | // Hardcoded number 2 means maximum height of index tree !!!
|
---|
529 | ext4_directory_dx_block_t dx_blocks[2];
|
---|
530 | ext4_directory_dx_block_t *dx_block, *tmp;
|
---|
531 | rc = ext4_directory_dx_get_leaf(&hinfo, inode_ref, root_block, &dx_block, dx_blocks);
|
---|
532 | if (rc != EOK) {
|
---|
533 | block_put(root_block);
|
---|
534 | return EXT4_ERR_BAD_DX_DIR;
|
---|
535 | }
|
---|
536 |
|
---|
537 |
|
---|
538 | do {
|
---|
539 |
|
---|
540 | uint32_t leaf_block_idx = ext4_directory_dx_entry_get_block(dx_block->position);
|
---|
541 | uint32_t leaf_block_addr;
|
---|
542 | rc = ext4_filesystem_get_inode_data_block_index(inode_ref, leaf_block_idx, &leaf_block_addr);
|
---|
543 | if (rc != EOK) {
|
---|
544 | goto cleanup;
|
---|
545 | }
|
---|
546 |
|
---|
547 | block_t *leaf_block;
|
---|
548 | rc = block_get(&leaf_block, fs->device, leaf_block_addr, BLOCK_FLAGS_NONE);
|
---|
549 | if (rc != EOK) {
|
---|
550 | goto cleanup;
|
---|
551 | }
|
---|
552 |
|
---|
553 | ext4_directory_entry_ll_t *res_dentry;
|
---|
554 | rc = ext4_directory_find_in_block(leaf_block, fs->superblock, name_len, name, &res_dentry);
|
---|
555 |
|
---|
556 | // Found => return it
|
---|
557 | if (rc == EOK) {
|
---|
558 | result->block = leaf_block;
|
---|
559 | result->dentry = res_dentry;
|
---|
560 | goto cleanup;
|
---|
561 | }
|
---|
562 |
|
---|
563 | // Not found, leave untouched
|
---|
564 | block_put(leaf_block);
|
---|
565 |
|
---|
566 | if (rc != ENOENT) {
|
---|
567 | goto cleanup;
|
---|
568 | }
|
---|
569 |
|
---|
570 | rc = ext4_directory_dx_next_block(inode_ref, hinfo.hash, dx_block, &dx_blocks[0]);
|
---|
571 | if (rc < 0) {
|
---|
572 | goto cleanup;
|
---|
573 | }
|
---|
574 |
|
---|
575 | } while (rc == 1);
|
---|
576 |
|
---|
577 | rc = ENOENT;
|
---|
578 |
|
---|
579 | cleanup:
|
---|
580 |
|
---|
581 | tmp = dx_blocks;
|
---|
582 | while (tmp <= dx_block) {
|
---|
583 | block_put(tmp->block);
|
---|
584 | ++tmp;
|
---|
585 | }
|
---|
586 | return rc;
|
---|
587 | }
|
---|
588 |
|
---|
589 | static int ext4_directory_dx_entry_comparator(void *arg1, void *arg2, void *dummy)
|
---|
590 | {
|
---|
591 | ext4_dx_sort_entry_t *entry1 = arg1;
|
---|
592 | ext4_dx_sort_entry_t *entry2 = arg2;
|
---|
593 |
|
---|
594 | if (entry1->hash == entry2->hash) {
|
---|
595 | return 0;
|
---|
596 | }
|
---|
597 |
|
---|
598 | if (entry1->hash < entry2->hash) {
|
---|
599 | return -1;
|
---|
600 | } else {
|
---|
601 | return 1;
|
---|
602 | }
|
---|
603 |
|
---|
604 | }
|
---|
605 |
|
---|
606 | static void ext4_directory_dx_insert_entry(
|
---|
607 | ext4_directory_dx_block_t *index_block, uint32_t hash, uint32_t iblock)
|
---|
608 | {
|
---|
609 | ext4_directory_dx_entry_t *old_index_entry = index_block->position;
|
---|
610 | ext4_directory_dx_entry_t *new_index_entry = old_index_entry + 1;
|
---|
611 |
|
---|
612 | ext4_directory_dx_countlimit_t *countlimit =
|
---|
613 | (ext4_directory_dx_countlimit_t *)index_block->entries;
|
---|
614 | uint32_t count = ext4_directory_dx_countlimit_get_count(countlimit);
|
---|
615 |
|
---|
616 | ext4_directory_dx_entry_t *start_index = index_block->entries;
|
---|
617 | size_t bytes = (void *)(start_index + count) - (void *)(new_index_entry);
|
---|
618 |
|
---|
619 | memmove(new_index_entry + 1, new_index_entry, bytes);
|
---|
620 |
|
---|
621 | ext4_directory_dx_entry_set_block(new_index_entry, iblock);
|
---|
622 | ext4_directory_dx_entry_set_hash(new_index_entry, hash);
|
---|
623 |
|
---|
624 | ext4_directory_dx_countlimit_set_count(countlimit, count + 1);
|
---|
625 |
|
---|
626 | index_block->block->dirty = true;
|
---|
627 | }
|
---|
628 |
|
---|
629 | static int ext4_directory_dx_split_data(ext4_filesystem_t *fs,
|
---|
630 | ext4_inode_ref_t *inode_ref, ext4_hash_info_t *hinfo,
|
---|
631 | block_t *old_data_block, ext4_directory_dx_block_t *index_block, block_t **new_data_block)
|
---|
632 | {
|
---|
633 | int rc = EOK;
|
---|
634 |
|
---|
635 | // Allocate buffer for directory entries
|
---|
636 | uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
|
---|
637 | void *entry_buffer = malloc(block_size);
|
---|
638 | if (entry_buffer == NULL) {
|
---|
639 | return ENOMEM;
|
---|
640 | }
|
---|
641 |
|
---|
642 | // dot entry has the smallest size available
|
---|
643 | uint32_t max_entry_count = block_size / sizeof(ext4_directory_dx_dot_entry_t);
|
---|
644 |
|
---|
645 | // Allocate sort entry
|
---|
646 | ext4_dx_sort_entry_t *sort_array = malloc(max_entry_count * sizeof(ext4_dx_sort_entry_t));
|
---|
647 | if (sort_array == NULL) {
|
---|
648 | free(entry_buffer);
|
---|
649 | return ENOMEM;
|
---|
650 | }
|
---|
651 |
|
---|
652 | uint32_t idx = 0;
|
---|
653 | uint32_t real_size = 0;
|
---|
654 |
|
---|
655 | // Initialize hinfo
|
---|
656 | ext4_hash_info_t tmp_hinfo;
|
---|
657 | memcpy(&tmp_hinfo, hinfo, sizeof(ext4_hash_info_t));
|
---|
658 |
|
---|
659 | // Load all valid entries to the buffer
|
---|
660 | ext4_directory_entry_ll_t *dentry = old_data_block->data;
|
---|
661 | void *entry_buffer_ptr = entry_buffer;
|
---|
662 | while ((void *)dentry < old_data_block->data + block_size) {
|
---|
663 |
|
---|
664 | // Read only valid entries
|
---|
665 | if (ext4_directory_entry_ll_get_inode(dentry) != 0) {
|
---|
666 |
|
---|
667 | uint8_t len = ext4_directory_entry_ll_get_name_length(fs->superblock, dentry);
|
---|
668 | ext4_hash_string(&tmp_hinfo, len, (char *)dentry->name);
|
---|
669 |
|
---|
670 | uint32_t rec_len = 8 + len;
|
---|
671 |
|
---|
672 | if ((rec_len % 4) != 0) {
|
---|
673 | rec_len += 4 - (rec_len % 4);
|
---|
674 | }
|
---|
675 |
|
---|
676 | memcpy(entry_buffer_ptr, dentry, rec_len);
|
---|
677 |
|
---|
678 | sort_array[idx].dentry = entry_buffer_ptr;
|
---|
679 | sort_array[idx].rec_len = rec_len;
|
---|
680 | sort_array[idx].hash = tmp_hinfo.hash;
|
---|
681 |
|
---|
682 | entry_buffer_ptr += rec_len;
|
---|
683 | real_size += rec_len;
|
---|
684 | idx++;
|
---|
685 | }
|
---|
686 |
|
---|
687 | dentry = (void *)dentry + ext4_directory_entry_ll_get_entry_length(dentry);
|
---|
688 | }
|
---|
689 |
|
---|
690 | qsort(sort_array, idx, sizeof(ext4_dx_sort_entry_t),
|
---|
691 | ext4_directory_dx_entry_comparator, NULL);
|
---|
692 |
|
---|
693 | uint32_t new_fblock;
|
---|
694 | uint32_t new_iblock;
|
---|
695 | rc = ext4_filesystem_append_inode_block(inode_ref, &new_fblock, &new_iblock);
|
---|
696 | if (rc != EOK) {
|
---|
697 | free(sort_array);
|
---|
698 | free(entry_buffer);
|
---|
699 | return rc;
|
---|
700 | }
|
---|
701 |
|
---|
702 | // Load new block
|
---|
703 | block_t *new_data_block_tmp;
|
---|
704 | rc = block_get(&new_data_block_tmp, fs->device, new_fblock, BLOCK_FLAGS_NOREAD);
|
---|
705 | if (rc != EOK) {
|
---|
706 | free(sort_array);
|
---|
707 | free(entry_buffer);
|
---|
708 | return rc;
|
---|
709 | }
|
---|
710 |
|
---|
711 | // Distribute entries to splitted blocks (by size)
|
---|
712 | uint32_t new_hash = 0;
|
---|
713 | uint32_t current_size = 0;
|
---|
714 | uint32_t mid = 0;
|
---|
715 | for (uint32_t i = 0; i < idx; ++i) {
|
---|
716 | if ((current_size + sort_array[i].rec_len) > (real_size / 2)) {
|
---|
717 | new_hash = sort_array[i].hash;
|
---|
718 | mid = i;
|
---|
719 | break;
|
---|
720 | }
|
---|
721 |
|
---|
722 | current_size += sort_array[i].rec_len;
|
---|
723 | }
|
---|
724 |
|
---|
725 | uint32_t continued = 0;
|
---|
726 | if (new_hash == sort_array[mid-1].hash) {
|
---|
727 | continued = 1;
|
---|
728 | }
|
---|
729 |
|
---|
730 | uint32_t offset = 0;
|
---|
731 | void *ptr;
|
---|
732 |
|
---|
733 | // First part - to the old block
|
---|
734 | for (uint32_t i = 0; i < mid; ++i) {
|
---|
735 | ptr = old_data_block->data + offset;
|
---|
736 | memcpy(ptr, sort_array[i].dentry, sort_array[i].rec_len);
|
---|
737 |
|
---|
738 | ext4_directory_entry_ll_t *tmp = ptr;
|
---|
739 | if (i < (mid - 1)) {
|
---|
740 | ext4_directory_entry_ll_set_entry_length(tmp, sort_array[i].rec_len);
|
---|
741 | } else {
|
---|
742 | ext4_directory_entry_ll_set_entry_length(tmp, block_size - offset);
|
---|
743 | }
|
---|
744 |
|
---|
745 | offset += sort_array[i].rec_len;
|
---|
746 | }
|
---|
747 |
|
---|
748 | // Second part - to the new block
|
---|
749 | offset = 0;
|
---|
750 | for (uint32_t i = mid; i < idx; ++i) {
|
---|
751 | ptr = new_data_block_tmp->data + offset;
|
---|
752 | memcpy(ptr, sort_array[i].dentry, sort_array[i].rec_len);
|
---|
753 |
|
---|
754 | ext4_directory_entry_ll_t *tmp = ptr;
|
---|
755 | if (i < (idx - 1)) {
|
---|
756 | ext4_directory_entry_ll_set_entry_length(tmp, sort_array[i].rec_len);
|
---|
757 | } else {
|
---|
758 | ext4_directory_entry_ll_set_entry_length(tmp, block_size - offset);
|
---|
759 | }
|
---|
760 |
|
---|
761 | offset += sort_array[i].rec_len;
|
---|
762 | }
|
---|
763 |
|
---|
764 | old_data_block->dirty = true;
|
---|
765 | new_data_block_tmp->dirty = true;
|
---|
766 |
|
---|
767 | free(sort_array);
|
---|
768 | free(entry_buffer);
|
---|
769 |
|
---|
770 | ext4_directory_dx_insert_entry(index_block, new_hash + continued, new_iblock);
|
---|
771 |
|
---|
772 | *new_data_block = new_data_block_tmp;
|
---|
773 |
|
---|
774 | return EOK;
|
---|
775 | }
|
---|
776 |
|
---|
777 |
|
---|
778 | static int ext4_directory_dx_split_index(ext4_filesystem_t *fs,
|
---|
779 | ext4_inode_ref_t *inode_ref, ext4_directory_dx_block_t *dx_blocks,
|
---|
780 | ext4_directory_dx_block_t *dx_block)
|
---|
781 | {
|
---|
782 | int rc;
|
---|
783 |
|
---|
784 | ext4_directory_dx_entry_t *entries;
|
---|
785 | if (dx_block == dx_blocks) {
|
---|
786 | entries = ((ext4_directory_dx_root_t *) dx_block->block->data)->entries;
|
---|
787 | } else {
|
---|
788 | entries = ((ext4_directory_dx_node_t *) dx_block->block->data)->entries;
|
---|
789 | }
|
---|
790 |
|
---|
791 | ext4_directory_dx_countlimit_t *countlimit =
|
---|
792 | (ext4_directory_dx_countlimit_t *)entries;
|
---|
793 | uint16_t leaf_limit = ext4_directory_dx_countlimit_get_limit(countlimit);
|
---|
794 | uint16_t leaf_count = ext4_directory_dx_countlimit_get_count(countlimit);
|
---|
795 |
|
---|
796 | // Check if is necessary to split index block
|
---|
797 | if (leaf_limit == leaf_count) {
|
---|
798 | EXT4FS_DBG("need to split index block !!!");
|
---|
799 |
|
---|
800 | unsigned int levels = dx_block - dx_blocks;
|
---|
801 |
|
---|
802 | ext4_directory_dx_entry_t *root_entries =
|
---|
803 | ((ext4_directory_dx_root_t *)dx_blocks[0].block->data)->entries;
|
---|
804 |
|
---|
805 | ext4_directory_dx_countlimit_t *root_countlimit =
|
---|
806 | (ext4_directory_dx_countlimit_t *)root_entries;
|
---|
807 | uint16_t root_limit =
|
---|
808 | ext4_directory_dx_countlimit_get_limit(root_countlimit);
|
---|
809 | uint16_t root_count =
|
---|
810 | ext4_directory_dx_countlimit_get_count(root_countlimit);
|
---|
811 |
|
---|
812 | if ((levels > 0) && (root_limit == root_count)) {
|
---|
813 | EXT4FS_DBG("Directory index is full");
|
---|
814 | return ENOSPC;
|
---|
815 | }
|
---|
816 |
|
---|
817 | uint32_t new_fblock;
|
---|
818 | uint32_t new_iblock;
|
---|
819 | rc = ext4_filesystem_append_inode_block(
|
---|
820 | inode_ref, &new_fblock, &new_iblock);
|
---|
821 | if (rc != EOK) {
|
---|
822 | return rc;
|
---|
823 | }
|
---|
824 |
|
---|
825 | // New block allocated
|
---|
826 | block_t * new_block;
|
---|
827 | rc = block_get(&new_block, fs->device, new_fblock, BLOCK_FLAGS_NOREAD);
|
---|
828 | if (rc != EOK) {
|
---|
829 | return rc;
|
---|
830 | }
|
---|
831 |
|
---|
832 | ext4_directory_dx_node_t *new_node = new_block->data;
|
---|
833 | ext4_directory_dx_entry_t *new_entries = new_node->entries;
|
---|
834 |
|
---|
835 | uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
|
---|
836 |
|
---|
837 | if (levels > 0) {
|
---|
838 | EXT4FS_DBG("split index leaf node");
|
---|
839 | uint32_t count_left = leaf_count / 2;
|
---|
840 | uint32_t count_right = leaf_count - count_left;
|
---|
841 | uint32_t hash_right =
|
---|
842 | ext4_directory_dx_entry_get_hash(entries + count_left);
|
---|
843 |
|
---|
844 | memcpy((void *) new_entries, (void *) (entries + count_left),
|
---|
845 | count_right * sizeof(ext4_directory_dx_entry_t));
|
---|
846 |
|
---|
847 | ext4_directory_dx_countlimit_t *left_countlimit =
|
---|
848 | (ext4_directory_dx_countlimit_t *)entries;
|
---|
849 | ext4_directory_dx_countlimit_t *right_countlimit =
|
---|
850 | (ext4_directory_dx_countlimit_t *)new_entries;
|
---|
851 |
|
---|
852 | ext4_directory_dx_countlimit_set_count(left_countlimit, count_left);
|
---|
853 | ext4_directory_dx_countlimit_set_count(right_countlimit, count_right);
|
---|
854 |
|
---|
855 | uint32_t entry_space = block_size - sizeof(ext4_fake_directory_entry_t);
|
---|
856 | uint32_t node_limit = entry_space / sizeof(ext4_directory_dx_entry_t);
|
---|
857 | ext4_directory_dx_countlimit_set_limit(right_countlimit, node_limit);
|
---|
858 |
|
---|
859 | // Which index block is target for new entry
|
---|
860 | uint32_t position_index = (dx_block->position - dx_block->entries);
|
---|
861 | if (position_index >= count_left) {
|
---|
862 |
|
---|
863 | dx_block->block->dirty = true;
|
---|
864 |
|
---|
865 | block_t *block_tmp = dx_block->block;
|
---|
866 | dx_block->block = new_block;
|
---|
867 | dx_block->position = new_entries + position_index - count_left;
|
---|
868 | dx_block->entries = new_entries;
|
---|
869 |
|
---|
870 | new_block = block_tmp;
|
---|
871 |
|
---|
872 | }
|
---|
873 |
|
---|
874 | ext4_directory_dx_insert_entry(dx_blocks, hash_right, new_iblock);
|
---|
875 |
|
---|
876 | return block_put(new_block);
|
---|
877 |
|
---|
878 | } else {
|
---|
879 | EXT4FS_DBG("create second level");
|
---|
880 |
|
---|
881 | memcpy((void *) new_entries, (void *) entries,
|
---|
882 | leaf_count * sizeof(ext4_directory_dx_entry_t));
|
---|
883 |
|
---|
884 | ext4_directory_dx_countlimit_t *new_countlimit =
|
---|
885 | (ext4_directory_dx_countlimit_t *)new_entries;
|
---|
886 |
|
---|
887 | uint32_t entry_space = block_size - sizeof(ext4_fake_directory_entry_t);
|
---|
888 | uint32_t node_limit = entry_space / sizeof(ext4_directory_dx_entry_t);
|
---|
889 | ext4_directory_dx_countlimit_set_limit(new_countlimit, node_limit);
|
---|
890 |
|
---|
891 | // Set values in root node
|
---|
892 | ext4_directory_dx_countlimit_t *new_root_countlimit =
|
---|
893 | (ext4_directory_dx_countlimit_t *)entries;
|
---|
894 |
|
---|
895 | ext4_directory_dx_countlimit_set_count(new_root_countlimit, 1);
|
---|
896 | ext4_directory_dx_entry_set_block(entries, new_iblock);
|
---|
897 |
|
---|
898 | ((ext4_directory_dx_root_t *)dx_blocks[0].block->data)->info.indirect_levels = 1;
|
---|
899 |
|
---|
900 | /* Add new access path frame */
|
---|
901 | dx_block = dx_blocks + 1;
|
---|
902 | dx_block->position = dx_block->position - entries + new_entries;
|
---|
903 | dx_block->entries = new_entries;
|
---|
904 | dx_block->block = new_block;
|
---|
905 | }
|
---|
906 |
|
---|
907 | }
|
---|
908 |
|
---|
909 | return EOK;
|
---|
910 | }
|
---|
911 |
|
---|
912 | int ext4_directory_dx_add_entry(ext4_inode_ref_t *parent,
|
---|
913 | ext4_inode_ref_t *child, const char *name)
|
---|
914 | {
|
---|
915 | int rc = EOK;
|
---|
916 | int rc2 = EOK;
|
---|
917 |
|
---|
918 | // get direct block 0 (index root)
|
---|
919 | uint32_t root_block_addr;
|
---|
920 | rc = ext4_filesystem_get_inode_data_block_index(parent, 0, &root_block_addr);
|
---|
921 | if (rc != EOK) {
|
---|
922 | return rc;
|
---|
923 | }
|
---|
924 |
|
---|
925 | ext4_filesystem_t *fs = parent->fs;
|
---|
926 |
|
---|
927 | block_t *root_block;
|
---|
928 | rc = block_get(&root_block, fs->device, root_block_addr, BLOCK_FLAGS_NONE);
|
---|
929 | if (rc != EOK) {
|
---|
930 | return rc;
|
---|
931 | }
|
---|
932 |
|
---|
933 | uint32_t name_len = strlen(name);
|
---|
934 | ext4_hash_info_t hinfo;
|
---|
935 | rc = ext4_directory_hinfo_init(&hinfo, root_block, fs->superblock, name_len, name);
|
---|
936 | if (rc != EOK) {
|
---|
937 | block_put(root_block);
|
---|
938 | EXT4FS_DBG("hinfo initialization error");
|
---|
939 | return EXT4_ERR_BAD_DX_DIR;
|
---|
940 | }
|
---|
941 |
|
---|
942 | // Hardcoded number 2 means maximum height of index tree !!!
|
---|
943 | ext4_directory_dx_block_t dx_blocks[2];
|
---|
944 | ext4_directory_dx_block_t *dx_block, *dx_it;
|
---|
945 | rc = ext4_directory_dx_get_leaf(&hinfo, parent, root_block, &dx_block, dx_blocks);
|
---|
946 | if (rc != EOK) {
|
---|
947 | EXT4FS_DBG("get leaf error");
|
---|
948 | rc = EXT4_ERR_BAD_DX_DIR;
|
---|
949 | goto release_index;
|
---|
950 | }
|
---|
951 |
|
---|
952 |
|
---|
953 | // Try to insert to existing data block
|
---|
954 | uint32_t leaf_block_idx = ext4_directory_dx_entry_get_block(dx_block->position);
|
---|
955 | uint32_t leaf_block_addr;
|
---|
956 | rc = ext4_filesystem_get_inode_data_block_index(parent, leaf_block_idx, &leaf_block_addr);
|
---|
957 | if (rc != EOK) {
|
---|
958 | goto release_index;
|
---|
959 | }
|
---|
960 |
|
---|
961 |
|
---|
962 | block_t *target_block;
|
---|
963 | rc = block_get(&target_block, fs->device, leaf_block_addr, BLOCK_FLAGS_NONE);
|
---|
964 | if (rc != EOK) {
|
---|
965 | goto release_index;
|
---|
966 | }
|
---|
967 |
|
---|
968 |
|
---|
969 | rc = ext4_directory_try_insert_entry(fs->superblock, target_block, child, name, name_len);
|
---|
970 | if (rc == EOK) {
|
---|
971 | goto release_target_index;
|
---|
972 | }
|
---|
973 |
|
---|
974 | EXT4FS_DBG("no free space found");
|
---|
975 |
|
---|
976 | rc = ext4_directory_dx_split_index(fs, parent, dx_blocks, dx_block);
|
---|
977 | if (rc != EOK) {
|
---|
978 | goto release_target_index;
|
---|
979 | }
|
---|
980 |
|
---|
981 | block_t *new_block = NULL;
|
---|
982 | rc = ext4_directory_dx_split_data(fs, parent, &hinfo, target_block, dx_block, &new_block);
|
---|
983 | if (rc != EOK) {
|
---|
984 | rc2 = rc;
|
---|
985 | goto release_target_index;
|
---|
986 | }
|
---|
987 |
|
---|
988 | // Where to save new entry
|
---|
989 | uint32_t new_block_hash = ext4_directory_dx_entry_get_hash(dx_block->position + 1);
|
---|
990 | if (hinfo.hash >= new_block_hash) {
|
---|
991 | rc = ext4_directory_try_insert_entry(fs->superblock, new_block, child, name, name_len);
|
---|
992 | } else {
|
---|
993 | rc = ext4_directory_try_insert_entry(fs->superblock, target_block, child, name, name_len);
|
---|
994 | }
|
---|
995 |
|
---|
996 |
|
---|
997 |
|
---|
998 |
|
---|
999 | rc = block_put(new_block);
|
---|
1000 | if (rc != EOK) {
|
---|
1001 | EXT4FS_DBG("error writing new block");
|
---|
1002 | return rc;
|
---|
1003 | }
|
---|
1004 |
|
---|
1005 | release_target_index:
|
---|
1006 |
|
---|
1007 | rc2 = rc;
|
---|
1008 |
|
---|
1009 | rc = block_put(target_block);
|
---|
1010 | if (rc != EOK) {
|
---|
1011 | EXT4FS_DBG("error writing target block");
|
---|
1012 | return rc;
|
---|
1013 | }
|
---|
1014 |
|
---|
1015 | release_index:
|
---|
1016 |
|
---|
1017 | if (rc != EOK) {
|
---|
1018 | rc2 = rc;
|
---|
1019 | }
|
---|
1020 |
|
---|
1021 | dx_it = dx_blocks;
|
---|
1022 |
|
---|
1023 | while (dx_it <= dx_block) {
|
---|
1024 | rc = block_put(dx_it->block);
|
---|
1025 | if (rc != EOK) {
|
---|
1026 | EXT4FS_DBG("error writing index block");
|
---|
1027 | return rc;
|
---|
1028 | }
|
---|
1029 | dx_it++;
|
---|
1030 | }
|
---|
1031 |
|
---|
1032 | return rc2;
|
---|
1033 | }
|
---|
1034 |
|
---|
1035 |
|
---|
1036 | /**
|
---|
1037 | * @}
|
---|
1038 | */
|
---|