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 | /** Initialize index structure of new directory.
|
---|
212 | *
|
---|
213 | * @param dir pointer to directory i-node
|
---|
214 | * @return error code
|
---|
215 | */
|
---|
216 | int ext4_directory_dx_init(ext4_inode_ref_t *dir)
|
---|
217 | {
|
---|
218 | int rc;
|
---|
219 |
|
---|
220 | /* Load block 0, where will be index root located */
|
---|
221 | uint32_t fblock;
|
---|
222 | rc = ext4_filesystem_get_inode_data_block_index(dir, 0, &fblock);
|
---|
223 | if (rc != EOK) {
|
---|
224 | return rc;
|
---|
225 | }
|
---|
226 |
|
---|
227 | block_t *block;
|
---|
228 | rc = block_get(&block, dir->fs->device, fblock, BLOCK_FLAGS_NONE);
|
---|
229 | if (rc != EOK) {
|
---|
230 | return rc;
|
---|
231 | }
|
---|
232 |
|
---|
233 | /* Initialize pointers to data structures */
|
---|
234 | ext4_directory_dx_root_t *root = block->data;
|
---|
235 | ext4_directory_dx_root_info_t *info = &(root->info);
|
---|
236 |
|
---|
237 | /* Initialize root info structure */
|
---|
238 | uint8_t hash_version =
|
---|
239 | ext4_superblock_get_default_hash_version(dir->fs->superblock);
|
---|
240 |
|
---|
241 | ext4_directory_dx_root_info_set_hash_version(info, hash_version);
|
---|
242 | ext4_directory_dx_root_info_set_indirect_levels(info, 0);
|
---|
243 | ext4_directory_dx_root_info_set_info_length(info, 8);
|
---|
244 |
|
---|
245 | /* Set limit and current number of entries */
|
---|
246 | ext4_directory_dx_countlimit_t *countlimit =
|
---|
247 | (ext4_directory_dx_countlimit_t *)&root->entries;
|
---|
248 | ext4_directory_dx_countlimit_set_count(countlimit, 1);
|
---|
249 |
|
---|
250 | uint32_t block_size = ext4_superblock_get_block_size(dir->fs->superblock);
|
---|
251 | uint32_t entry_space = block_size - 2 * sizeof(ext4_directory_dx_dot_entry_t)
|
---|
252 | - sizeof(ext4_directory_dx_root_info_t);
|
---|
253 | uint16_t root_limit = entry_space / sizeof(ext4_directory_dx_entry_t);
|
---|
254 | ext4_directory_dx_countlimit_set_limit(countlimit, root_limit);
|
---|
255 |
|
---|
256 | /* Append new block, where will be new entries inserted in the future */
|
---|
257 | uint32_t iblock;
|
---|
258 | rc = ext4_filesystem_append_inode_block(dir, &fblock, &iblock);
|
---|
259 | if (rc != EOK) {
|
---|
260 | block_put(block);
|
---|
261 | return rc;
|
---|
262 | }
|
---|
263 |
|
---|
264 | block_t *new_block;
|
---|
265 | rc = block_get(&new_block, dir->fs->device, fblock, BLOCK_FLAGS_NOREAD);
|
---|
266 | if (rc != EOK) {
|
---|
267 | block_put(block);
|
---|
268 | return rc;
|
---|
269 | }
|
---|
270 |
|
---|
271 | /* Fill the whole block with empty entry */
|
---|
272 | ext4_directory_entry_ll_t *block_entry = new_block->data;
|
---|
273 | ext4_directory_entry_ll_set_entry_length(block_entry, block_size);
|
---|
274 | ext4_directory_entry_ll_set_inode(block_entry, 0);
|
---|
275 |
|
---|
276 | new_block->dirty = true;
|
---|
277 | rc = block_put(new_block);
|
---|
278 | if (rc != EOK) {
|
---|
279 | block_put(block);
|
---|
280 | return rc;
|
---|
281 | }
|
---|
282 |
|
---|
283 | /* Connect new block to the only entry in index */
|
---|
284 | ext4_directory_dx_entry_t *entry = root->entries;
|
---|
285 | ext4_directory_dx_entry_set_block(entry, iblock);
|
---|
286 |
|
---|
287 | block->dirty = true;
|
---|
288 |
|
---|
289 | rc = block_put(block);
|
---|
290 | if (rc != EOK) {
|
---|
291 | return rc;
|
---|
292 | }
|
---|
293 |
|
---|
294 | return EOK;
|
---|
295 | }
|
---|
296 |
|
---|
297 | /** Initialize hash info structure necessary for index operations.
|
---|
298 | *
|
---|
299 | * @param hinfo pointer to hinfo to be initialized
|
---|
300 | * @param root_block root block (number 0) of index
|
---|
301 | * @param sb pointer to superblock
|
---|
302 | * @param name_len length of name to be computed hash value from
|
---|
303 | * @param name name to be computed hash value from
|
---|
304 | * @return error code
|
---|
305 | */
|
---|
306 | static int ext4_directory_hinfo_init(ext4_hash_info_t *hinfo, block_t *root_block,
|
---|
307 | ext4_superblock_t *sb, size_t name_len, const char *name)
|
---|
308 | {
|
---|
309 |
|
---|
310 | ext4_directory_dx_root_t *root = (ext4_directory_dx_root_t *)root_block->data;
|
---|
311 |
|
---|
312 | if (root->info.hash_version != EXT4_HASH_VERSION_TEA &&
|
---|
313 | root->info.hash_version != EXT4_HASH_VERSION_HALF_MD4 &&
|
---|
314 | root->info.hash_version != EXT4_HASH_VERSION_LEGACY) {
|
---|
315 | return EXT4_ERR_BAD_DX_DIR;
|
---|
316 | }
|
---|
317 |
|
---|
318 | /* Check unused flags */
|
---|
319 | if (root->info.unused_flags != 0) {
|
---|
320 | EXT4FS_DBG("ERR: unused_flags = \%u", root->info.unused_flags);
|
---|
321 | return EXT4_ERR_BAD_DX_DIR;
|
---|
322 | }
|
---|
323 |
|
---|
324 | /* Check indirect levels */
|
---|
325 | if (root->info.indirect_levels > 1) {
|
---|
326 | EXT4FS_DBG("ERR: indirect_levels = \%u", root->info.indirect_levels);
|
---|
327 | return EXT4_ERR_BAD_DX_DIR;
|
---|
328 | }
|
---|
329 |
|
---|
330 | /* Check if node limit is correct */
|
---|
331 | uint32_t block_size = ext4_superblock_get_block_size(sb);
|
---|
332 | uint32_t entry_space = block_size;
|
---|
333 | entry_space -= 2 * sizeof(ext4_directory_dx_dot_entry_t);
|
---|
334 | entry_space -= sizeof(ext4_directory_dx_root_info_t);
|
---|
335 | entry_space = entry_space / sizeof(ext4_directory_dx_entry_t);
|
---|
336 |
|
---|
337 | uint16_t limit = ext4_directory_dx_countlimit_get_limit((ext4_directory_dx_countlimit_t *)&root->entries);
|
---|
338 | if (limit != entry_space) {
|
---|
339 | return EXT4_ERR_BAD_DX_DIR;
|
---|
340 | }
|
---|
341 |
|
---|
342 | /* Check hash version and modify if necessary */
|
---|
343 | hinfo->hash_version = ext4_directory_dx_root_info_get_hash_version(&root->info);
|
---|
344 | if ((hinfo->hash_version <= EXT4_HASH_VERSION_TEA)
|
---|
345 | && (ext4_superblock_has_flag(sb, EXT4_SUPERBLOCK_FLAGS_UNSIGNED_HASH))) {
|
---|
346 | /* 3 is magic from ext4 linux implementation */
|
---|
347 | hinfo->hash_version += 3;
|
---|
348 | }
|
---|
349 |
|
---|
350 | /* Load hash seed from superblock */
|
---|
351 | hinfo->seed = ext4_superblock_get_hash_seed(sb);
|
---|
352 |
|
---|
353 | /* Compute hash value of name */
|
---|
354 | if (name) {
|
---|
355 | ext4_hash_string(hinfo, name_len, name);
|
---|
356 | }
|
---|
357 |
|
---|
358 | return EOK;
|
---|
359 | }
|
---|
360 |
|
---|
361 | /** Walk through index tree and load leaf with corresponding hash value.
|
---|
362 | *
|
---|
363 | * @param hinfo initialized hash info structure
|
---|
364 | * @param inode_ref current i-node
|
---|
365 | * @param root_block root block (iblock 0), where is root node located
|
---|
366 | * @param dx_block pointer to leaf node in dx_blocks array
|
---|
367 | * @param dx_blocks array with the whole path from root to leaf
|
---|
368 | * @return error code
|
---|
369 | */
|
---|
370 | static int ext4_directory_dx_get_leaf(ext4_hash_info_t *hinfo,
|
---|
371 | ext4_inode_ref_t *inode_ref, block_t *root_block,
|
---|
372 | ext4_directory_dx_block_t **dx_block, ext4_directory_dx_block_t *dx_blocks)
|
---|
373 | {
|
---|
374 | int rc;
|
---|
375 |
|
---|
376 | ext4_directory_dx_block_t *tmp_dx_block = dx_blocks;
|
---|
377 |
|
---|
378 | ext4_directory_dx_root_t *root = (ext4_directory_dx_root_t *)root_block->data;
|
---|
379 | ext4_directory_dx_entry_t *entries = (ext4_directory_dx_entry_t *)&root->entries;
|
---|
380 |
|
---|
381 | uint16_t limit = ext4_directory_dx_countlimit_get_limit((ext4_directory_dx_countlimit_t *)entries);
|
---|
382 | uint8_t indirect_level = ext4_directory_dx_root_info_get_indirect_levels(&root->info);
|
---|
383 |
|
---|
384 | block_t *tmp_block = root_block;
|
---|
385 | ext4_directory_dx_entry_t *p, *q, *m, *at;
|
---|
386 |
|
---|
387 | /* Walk through the index tree */
|
---|
388 | while (true) {
|
---|
389 |
|
---|
390 | uint16_t count = ext4_directory_dx_countlimit_get_count((ext4_directory_dx_countlimit_t *)entries);
|
---|
391 | if ((count == 0) || (count > limit)) {
|
---|
392 | return EXT4_ERR_BAD_DX_DIR;
|
---|
393 | }
|
---|
394 |
|
---|
395 |
|
---|
396 | /* Do binary search in every node */
|
---|
397 | p = entries + 1;
|
---|
398 | q = entries + count - 1;
|
---|
399 |
|
---|
400 | while (p <= q) {
|
---|
401 | m = p + (q - p) / 2;
|
---|
402 | if (ext4_directory_dx_entry_get_hash(m) > hinfo->hash) {
|
---|
403 | q = m - 1;
|
---|
404 | } else {
|
---|
405 | p = m + 1;
|
---|
406 | }
|
---|
407 | }
|
---|
408 |
|
---|
409 | at = p - 1;
|
---|
410 |
|
---|
411 | /* Write results */
|
---|
412 | tmp_dx_block->block = tmp_block;
|
---|
413 | tmp_dx_block->entries = entries;
|
---|
414 | tmp_dx_block->position = at;
|
---|
415 |
|
---|
416 | /* Is algorithm in the leaf? */
|
---|
417 | if (indirect_level == 0) {
|
---|
418 | *dx_block = tmp_dx_block;
|
---|
419 | return EOK;
|
---|
420 | }
|
---|
421 |
|
---|
422 | /* Goto child node */
|
---|
423 | uint32_t next_block = ext4_directory_dx_entry_get_block(at);
|
---|
424 |
|
---|
425 | indirect_level--;
|
---|
426 |
|
---|
427 | uint32_t fblock;
|
---|
428 | rc = ext4_filesystem_get_inode_data_block_index(
|
---|
429 | inode_ref, next_block, &fblock);
|
---|
430 | if (rc != EOK) {
|
---|
431 | return rc;
|
---|
432 | }
|
---|
433 |
|
---|
434 | rc = block_get(&tmp_block, inode_ref->fs->device, fblock, BLOCK_FLAGS_NONE);
|
---|
435 | if (rc != EOK) {
|
---|
436 | return rc;
|
---|
437 | }
|
---|
438 |
|
---|
439 | entries = ((ext4_directory_dx_node_t *) tmp_block->data)->entries;
|
---|
440 | limit = ext4_directory_dx_countlimit_get_limit(
|
---|
441 | (ext4_directory_dx_countlimit_t *)entries);
|
---|
442 |
|
---|
443 | uint16_t entry_space = ext4_superblock_get_block_size(inode_ref->fs->superblock)
|
---|
444 | - sizeof(ext4_directory_dx_dot_entry_t);
|
---|
445 | entry_space = entry_space / sizeof(ext4_directory_dx_entry_t);
|
---|
446 |
|
---|
447 |
|
---|
448 | if (limit != entry_space) {
|
---|
449 | block_put(tmp_block);
|
---|
450 | return EXT4_ERR_BAD_DX_DIR;
|
---|
451 | }
|
---|
452 |
|
---|
453 | ++tmp_dx_block;
|
---|
454 | }
|
---|
455 |
|
---|
456 | /* Unreachable */
|
---|
457 | return EOK;
|
---|
458 | }
|
---|
459 |
|
---|
460 |
|
---|
461 | /** Check if the the next block would be checked during entry search.
|
---|
462 | *
|
---|
463 | * @param inode_ref directory i-node
|
---|
464 | * @param hash hash value to check
|
---|
465 | * @param dx_block current block
|
---|
466 | * @param dx_blocks aray with path from root to leaf node
|
---|
467 | * @return error code
|
---|
468 | */
|
---|
469 | static int ext4_directory_dx_next_block(ext4_inode_ref_t *inode_ref, uint32_t hash,
|
---|
470 | ext4_directory_dx_block_t *dx_block, ext4_directory_dx_block_t *dx_blocks)
|
---|
471 | {
|
---|
472 | int rc;
|
---|
473 |
|
---|
474 | uint32_t num_handles = 0;
|
---|
475 | ext4_directory_dx_block_t *p = dx_block;
|
---|
476 |
|
---|
477 | /* Try to find data block with next bunch of entries */
|
---|
478 | while (1) {
|
---|
479 |
|
---|
480 | p->position++;
|
---|
481 | uint16_t count = ext4_directory_dx_countlimit_get_count(
|
---|
482 | (ext4_directory_dx_countlimit_t *)p->entries);
|
---|
483 |
|
---|
484 | if (p->position < p->entries + count) {
|
---|
485 | break;
|
---|
486 | }
|
---|
487 |
|
---|
488 | if (p == dx_blocks) {
|
---|
489 | return 0;
|
---|
490 | }
|
---|
491 |
|
---|
492 | num_handles++;
|
---|
493 | p--;
|
---|
494 | }
|
---|
495 |
|
---|
496 | /* Check hash collision (if not occured - no next block cannot be used) */
|
---|
497 | uint32_t current_hash = ext4_directory_dx_entry_get_hash(p->position);
|
---|
498 | if ((hash & 1) == 0) {
|
---|
499 | if ((current_hash & ~1) != hash) {
|
---|
500 | return 0;
|
---|
501 | }
|
---|
502 | }
|
---|
503 |
|
---|
504 | /* Fill new path */
|
---|
505 | while (num_handles--) {
|
---|
506 |
|
---|
507 | uint32_t block_idx = ext4_directory_dx_entry_get_block(p->position);
|
---|
508 | uint32_t block_addr;
|
---|
509 | rc = ext4_filesystem_get_inode_data_block_index(inode_ref, block_idx, &block_addr);
|
---|
510 | if (rc != EOK) {
|
---|
511 | return rc;
|
---|
512 | }
|
---|
513 |
|
---|
514 | block_t *block;
|
---|
515 | rc = block_get(&block, inode_ref->fs->device, block_addr, BLOCK_FLAGS_NONE);
|
---|
516 | if (rc != EOK) {
|
---|
517 | return rc;
|
---|
518 | }
|
---|
519 |
|
---|
520 | p++;
|
---|
521 |
|
---|
522 | /* Don't forget to put old block (prevent memory leak) */
|
---|
523 | block_put(p->block);
|
---|
524 |
|
---|
525 | p->block = block;
|
---|
526 | p->entries = ((ext4_directory_dx_node_t *) block->data)->entries;
|
---|
527 | p->position = p->entries;
|
---|
528 | }
|
---|
529 |
|
---|
530 | return 1;
|
---|
531 |
|
---|
532 | }
|
---|
533 |
|
---|
534 | /** Try to find directory entry using directory index.
|
---|
535 | *
|
---|
536 | * @param result output value - if entry will be found,
|
---|
537 | * than will be passed through this parameter
|
---|
538 | * @param inode_ref directory i-node
|
---|
539 | * @param name_len length of name to be found
|
---|
540 | * @param name name to be found
|
---|
541 | * @return error code
|
---|
542 | */
|
---|
543 | int ext4_directory_dx_find_entry(ext4_directory_search_result_t *result,
|
---|
544 | ext4_inode_ref_t *inode_ref, size_t name_len, const char *name)
|
---|
545 | {
|
---|
546 | int rc;
|
---|
547 |
|
---|
548 | /* Load direct block 0 (index root) */
|
---|
549 | uint32_t root_block_addr;
|
---|
550 | rc = ext4_filesystem_get_inode_data_block_index(inode_ref, 0, &root_block_addr);
|
---|
551 | if (rc != EOK) {
|
---|
552 | return rc;
|
---|
553 | }
|
---|
554 |
|
---|
555 | ext4_filesystem_t *fs = inode_ref->fs;
|
---|
556 |
|
---|
557 | block_t *root_block;
|
---|
558 | rc = block_get(&root_block, fs->device, root_block_addr, BLOCK_FLAGS_NONE);
|
---|
559 | if (rc != EOK) {
|
---|
560 | return rc;
|
---|
561 | }
|
---|
562 |
|
---|
563 | /* Initialize hash info (compute hash value) */
|
---|
564 | ext4_hash_info_t hinfo;
|
---|
565 | rc = ext4_directory_hinfo_init(&hinfo, root_block, fs->superblock, name_len, name);
|
---|
566 | if (rc != EOK) {
|
---|
567 | block_put(root_block);
|
---|
568 | return EXT4_ERR_BAD_DX_DIR;
|
---|
569 | }
|
---|
570 |
|
---|
571 | /* Hardcoded number 2 means maximum height of index tree, specified in linux driver */
|
---|
572 | ext4_directory_dx_block_t dx_blocks[2];
|
---|
573 | ext4_directory_dx_block_t *dx_block, *tmp;
|
---|
574 | rc = ext4_directory_dx_get_leaf(&hinfo, inode_ref, root_block, &dx_block, dx_blocks);
|
---|
575 | if (rc != EOK) {
|
---|
576 | block_put(root_block);
|
---|
577 | return EXT4_ERR_BAD_DX_DIR;
|
---|
578 | }
|
---|
579 |
|
---|
580 | do {
|
---|
581 | /* Load leaf block */
|
---|
582 | uint32_t leaf_block_idx = ext4_directory_dx_entry_get_block(dx_block->position);
|
---|
583 | uint32_t leaf_block_addr;
|
---|
584 | rc = ext4_filesystem_get_inode_data_block_index(inode_ref, leaf_block_idx, &leaf_block_addr);
|
---|
585 | if (rc != EOK) {
|
---|
586 | goto cleanup;
|
---|
587 | }
|
---|
588 |
|
---|
589 | block_t *leaf_block;
|
---|
590 | rc = block_get(&leaf_block, fs->device, leaf_block_addr, BLOCK_FLAGS_NONE);
|
---|
591 | if (rc != EOK) {
|
---|
592 | goto cleanup;
|
---|
593 | }
|
---|
594 |
|
---|
595 | /* Linear search inside block */
|
---|
596 | ext4_directory_entry_ll_t *res_dentry;
|
---|
597 | rc = ext4_directory_find_in_block(leaf_block, fs->superblock, name_len, name, &res_dentry);
|
---|
598 |
|
---|
599 | /* Found => return it */
|
---|
600 | if (rc == EOK) {
|
---|
601 | result->block = leaf_block;
|
---|
602 | result->dentry = res_dentry;
|
---|
603 | goto cleanup;
|
---|
604 | }
|
---|
605 |
|
---|
606 | /* Not found, leave untouched */
|
---|
607 | block_put(leaf_block);
|
---|
608 |
|
---|
609 | if (rc != ENOENT) {
|
---|
610 | goto cleanup;
|
---|
611 | }
|
---|
612 |
|
---|
613 | /* check if the next block could be checked */
|
---|
614 | rc = ext4_directory_dx_next_block(inode_ref, hinfo.hash, dx_block, &dx_blocks[0]);
|
---|
615 | if (rc < 0) {
|
---|
616 | goto cleanup;
|
---|
617 | }
|
---|
618 |
|
---|
619 | } while (rc == 1);
|
---|
620 |
|
---|
621 | /* Entry not found */
|
---|
622 | rc = ENOENT;
|
---|
623 |
|
---|
624 | cleanup:
|
---|
625 |
|
---|
626 | /* The whole path must be released (preventing memory leak) */
|
---|
627 | tmp = dx_blocks;
|
---|
628 | while (tmp <= dx_block) {
|
---|
629 | block_put(tmp->block);
|
---|
630 | ++tmp;
|
---|
631 | }
|
---|
632 | return rc;
|
---|
633 | }
|
---|
634 |
|
---|
635 | /** Compare function used to pass in quicksort implementation.
|
---|
636 | *
|
---|
637 | * It can compare two entries by hash value.
|
---|
638 | *
|
---|
639 | * @param arg1 first entry
|
---|
640 | * @param arg2 second entry
|
---|
641 | * @param dummy unused parameter, can be NULL
|
---|
642 | * @return classic compare result (0: equal, -1: arg1 < arg2, 1: arg1 > arg2)
|
---|
643 | */
|
---|
644 | static int ext4_directory_dx_entry_comparator(void *arg1, void *arg2, void *dummy)
|
---|
645 | {
|
---|
646 | ext4_dx_sort_entry_t *entry1 = arg1;
|
---|
647 | ext4_dx_sort_entry_t *entry2 = arg2;
|
---|
648 |
|
---|
649 | if (entry1->hash == entry2->hash) {
|
---|
650 | return 0;
|
---|
651 | }
|
---|
652 |
|
---|
653 | if (entry1->hash < entry2->hash) {
|
---|
654 | return -1;
|
---|
655 | } else {
|
---|
656 | return 1;
|
---|
657 | }
|
---|
658 |
|
---|
659 | }
|
---|
660 |
|
---|
661 | /** Insert new index entry to block.
|
---|
662 | *
|
---|
663 | * Note that space for new entry must be checked by caller.
|
---|
664 | *
|
---|
665 | * @param index_block block where to insert new entry
|
---|
666 | * @param hash hash value covered by child node
|
---|
667 | * @param iblock logical number of child block
|
---|
668 | *
|
---|
669 | */
|
---|
670 | static void ext4_directory_dx_insert_entry(
|
---|
671 | ext4_directory_dx_block_t *index_block, uint32_t hash, uint32_t iblock)
|
---|
672 | {
|
---|
673 | ext4_directory_dx_entry_t *old_index_entry = index_block->position;
|
---|
674 | ext4_directory_dx_entry_t *new_index_entry = old_index_entry + 1;
|
---|
675 |
|
---|
676 | ext4_directory_dx_countlimit_t *countlimit =
|
---|
677 | (ext4_directory_dx_countlimit_t *)index_block->entries;
|
---|
678 | uint32_t count = ext4_directory_dx_countlimit_get_count(countlimit);
|
---|
679 |
|
---|
680 | ext4_directory_dx_entry_t *start_index = index_block->entries;
|
---|
681 | size_t bytes = (void *)(start_index + count) - (void *)(new_index_entry);
|
---|
682 |
|
---|
683 | memmove(new_index_entry + 1, new_index_entry, bytes);
|
---|
684 |
|
---|
685 | ext4_directory_dx_entry_set_block(new_index_entry, iblock);
|
---|
686 | ext4_directory_dx_entry_set_hash(new_index_entry, hash);
|
---|
687 |
|
---|
688 | ext4_directory_dx_countlimit_set_count(countlimit, count + 1);
|
---|
689 |
|
---|
690 | index_block->block->dirty = true;
|
---|
691 | }
|
---|
692 |
|
---|
693 | /** Split directory entries to two parts preventing node overflow.
|
---|
694 | *
|
---|
695 | * @param inode_ref directory i-node
|
---|
696 | * @param hinfo hash info
|
---|
697 | * @param old_data_block block with data to be split
|
---|
698 | * @param index_block block where index entries are located
|
---|
699 | * @param new_data_block output value for newly allocated data block
|
---|
700 | */
|
---|
701 | static int ext4_directory_dx_split_data(ext4_inode_ref_t *inode_ref,
|
---|
702 | ext4_hash_info_t *hinfo, block_t *old_data_block,
|
---|
703 | ext4_directory_dx_block_t *index_block, block_t **new_data_block)
|
---|
704 | {
|
---|
705 | int rc = EOK;
|
---|
706 |
|
---|
707 | /* Allocate buffer for directory entries */
|
---|
708 | uint32_t block_size =
|
---|
709 | ext4_superblock_get_block_size(inode_ref->fs->superblock);
|
---|
710 | void *entry_buffer = malloc(block_size);
|
---|
711 | if (entry_buffer == NULL) {
|
---|
712 | return ENOMEM;
|
---|
713 | }
|
---|
714 |
|
---|
715 | /* dot entry has the smallest size available */
|
---|
716 | uint32_t max_entry_count = block_size / sizeof(ext4_directory_dx_dot_entry_t);
|
---|
717 |
|
---|
718 | /* Allocate sort entry */
|
---|
719 | ext4_dx_sort_entry_t *sort_array = malloc(max_entry_count * sizeof(ext4_dx_sort_entry_t));
|
---|
720 | if (sort_array == NULL) {
|
---|
721 | free(entry_buffer);
|
---|
722 | return ENOMEM;
|
---|
723 | }
|
---|
724 |
|
---|
725 | uint32_t idx = 0;
|
---|
726 | uint32_t real_size = 0;
|
---|
727 |
|
---|
728 | /* Initialize hinfo */
|
---|
729 | ext4_hash_info_t tmp_hinfo;
|
---|
730 | memcpy(&tmp_hinfo, hinfo, sizeof(ext4_hash_info_t));
|
---|
731 |
|
---|
732 | /* Load all valid entries to the buffer */
|
---|
733 | ext4_directory_entry_ll_t *dentry = old_data_block->data;
|
---|
734 | void *entry_buffer_ptr = entry_buffer;
|
---|
735 | while ((void *)dentry < old_data_block->data + block_size) {
|
---|
736 |
|
---|
737 | /* Read only valid entries */
|
---|
738 | if (ext4_directory_entry_ll_get_inode(dentry) != 0) {
|
---|
739 |
|
---|
740 | uint8_t len = ext4_directory_entry_ll_get_name_length(
|
---|
741 | inode_ref->fs->superblock, dentry);
|
---|
742 | ext4_hash_string(&tmp_hinfo, len, (char *)dentry->name);
|
---|
743 |
|
---|
744 | uint32_t rec_len = 8 + len;
|
---|
745 |
|
---|
746 | if ((rec_len % 4) != 0) {
|
---|
747 | rec_len += 4 - (rec_len % 4);
|
---|
748 | }
|
---|
749 |
|
---|
750 | memcpy(entry_buffer_ptr, dentry, rec_len);
|
---|
751 |
|
---|
752 | sort_array[idx].dentry = entry_buffer_ptr;
|
---|
753 | sort_array[idx].rec_len = rec_len;
|
---|
754 | sort_array[idx].hash = tmp_hinfo.hash;
|
---|
755 |
|
---|
756 | entry_buffer_ptr += rec_len;
|
---|
757 | real_size += rec_len;
|
---|
758 | idx++;
|
---|
759 | }
|
---|
760 |
|
---|
761 | dentry = (void *)dentry + ext4_directory_entry_ll_get_entry_length(dentry);
|
---|
762 | }
|
---|
763 |
|
---|
764 | /* Sort all entries */
|
---|
765 | qsort(sort_array, idx, sizeof(ext4_dx_sort_entry_t),
|
---|
766 | ext4_directory_dx_entry_comparator, NULL);
|
---|
767 |
|
---|
768 | /* Allocate new block for store the second part of entries */
|
---|
769 | uint32_t new_fblock;
|
---|
770 | uint32_t new_iblock;
|
---|
771 | rc = ext4_filesystem_append_inode_block(inode_ref, &new_fblock, &new_iblock);
|
---|
772 | if (rc != EOK) {
|
---|
773 | free(sort_array);
|
---|
774 | free(entry_buffer);
|
---|
775 | return rc;
|
---|
776 | }
|
---|
777 |
|
---|
778 | /* Load new block */
|
---|
779 | block_t *new_data_block_tmp;
|
---|
780 | rc = block_get(&new_data_block_tmp, inode_ref->fs->device,
|
---|
781 | new_fblock, BLOCK_FLAGS_NOREAD);
|
---|
782 | if (rc != EOK) {
|
---|
783 | free(sort_array);
|
---|
784 | free(entry_buffer);
|
---|
785 | return rc;
|
---|
786 | }
|
---|
787 |
|
---|
788 | /* Distribute entries to two blocks (by size)
|
---|
789 | * - compute the half
|
---|
790 | */
|
---|
791 | uint32_t new_hash = 0;
|
---|
792 | uint32_t current_size = 0;
|
---|
793 | uint32_t mid = 0;
|
---|
794 | for (uint32_t i = 0; i < idx; ++i) {
|
---|
795 | if ((current_size + sort_array[i].rec_len) > (real_size / 2)) {
|
---|
796 | new_hash = sort_array[i].hash;
|
---|
797 | mid = i;
|
---|
798 | break;
|
---|
799 | }
|
---|
800 |
|
---|
801 | current_size += sort_array[i].rec_len;
|
---|
802 | }
|
---|
803 |
|
---|
804 | /* Check hash collision */
|
---|
805 | uint32_t continued = 0;
|
---|
806 | if (new_hash == sort_array[mid-1].hash) {
|
---|
807 | continued = 1;
|
---|
808 | }
|
---|
809 |
|
---|
810 | uint32_t offset = 0;
|
---|
811 | void *ptr;
|
---|
812 |
|
---|
813 | /* First part - to the old block */
|
---|
814 | for (uint32_t i = 0; i < mid; ++i) {
|
---|
815 | ptr = old_data_block->data + offset;
|
---|
816 | memcpy(ptr, sort_array[i].dentry, sort_array[i].rec_len);
|
---|
817 |
|
---|
818 | ext4_directory_entry_ll_t *tmp = ptr;
|
---|
819 | if (i < (mid - 1)) {
|
---|
820 | ext4_directory_entry_ll_set_entry_length(tmp, sort_array[i].rec_len);
|
---|
821 | } else {
|
---|
822 | ext4_directory_entry_ll_set_entry_length(tmp, block_size - offset);
|
---|
823 | }
|
---|
824 |
|
---|
825 | offset += sort_array[i].rec_len;
|
---|
826 | }
|
---|
827 |
|
---|
828 | /* Second part - to the new block */
|
---|
829 | offset = 0;
|
---|
830 | for (uint32_t i = mid; i < idx; ++i) {
|
---|
831 | ptr = new_data_block_tmp->data + offset;
|
---|
832 | memcpy(ptr, sort_array[i].dentry, sort_array[i].rec_len);
|
---|
833 |
|
---|
834 | ext4_directory_entry_ll_t *tmp = ptr;
|
---|
835 | if (i < (idx - 1)) {
|
---|
836 | ext4_directory_entry_ll_set_entry_length(tmp, sort_array[i].rec_len);
|
---|
837 | } else {
|
---|
838 | ext4_directory_entry_ll_set_entry_length(tmp, block_size - offset);
|
---|
839 | }
|
---|
840 |
|
---|
841 | offset += sort_array[i].rec_len;
|
---|
842 | }
|
---|
843 |
|
---|
844 | /* Do some steps to finish operation */
|
---|
845 | old_data_block->dirty = true;
|
---|
846 | new_data_block_tmp->dirty = true;
|
---|
847 |
|
---|
848 | free(sort_array);
|
---|
849 | free(entry_buffer);
|
---|
850 |
|
---|
851 | ext4_directory_dx_insert_entry(index_block, new_hash + continued, new_iblock);
|
---|
852 |
|
---|
853 | *new_data_block = new_data_block_tmp;
|
---|
854 |
|
---|
855 | return EOK;
|
---|
856 | }
|
---|
857 |
|
---|
858 | /** Split index node and maybe some parent nodes in the tree hierarchy.
|
---|
859 | *
|
---|
860 | * @param inode_ref directory i-node
|
---|
861 | * @param dx_blocks array with path from root to leaf node
|
---|
862 | * @param dx_block leaf block to be split if needed
|
---|
863 | * @return error code
|
---|
864 | */
|
---|
865 | static int ext4_directory_dx_split_index(ext4_inode_ref_t *inode_ref,
|
---|
866 | ext4_directory_dx_block_t *dx_blocks, ext4_directory_dx_block_t *dx_block)
|
---|
867 | {
|
---|
868 | int rc;
|
---|
869 |
|
---|
870 | ext4_directory_dx_entry_t *entries;
|
---|
871 | if (dx_block == dx_blocks) {
|
---|
872 | entries = ((ext4_directory_dx_root_t *) dx_block->block->data)->entries;
|
---|
873 | } else {
|
---|
874 | entries = ((ext4_directory_dx_node_t *) dx_block->block->data)->entries;
|
---|
875 | }
|
---|
876 |
|
---|
877 | ext4_directory_dx_countlimit_t *countlimit =
|
---|
878 | (ext4_directory_dx_countlimit_t *)entries;
|
---|
879 | uint16_t leaf_limit = ext4_directory_dx_countlimit_get_limit(countlimit);
|
---|
880 | uint16_t leaf_count = ext4_directory_dx_countlimit_get_count(countlimit);
|
---|
881 |
|
---|
882 | /* Check if is necessary to split index block */
|
---|
883 | if (leaf_limit == leaf_count) {
|
---|
884 |
|
---|
885 | unsigned int levels = dx_block - dx_blocks;
|
---|
886 |
|
---|
887 | ext4_directory_dx_entry_t *root_entries =
|
---|
888 | ((ext4_directory_dx_root_t *)dx_blocks[0].block->data)->entries;
|
---|
889 |
|
---|
890 | ext4_directory_dx_countlimit_t *root_countlimit =
|
---|
891 | (ext4_directory_dx_countlimit_t *)root_entries;
|
---|
892 | uint16_t root_limit =
|
---|
893 | ext4_directory_dx_countlimit_get_limit(root_countlimit);
|
---|
894 | uint16_t root_count =
|
---|
895 | ext4_directory_dx_countlimit_get_count(root_countlimit);
|
---|
896 |
|
---|
897 | /* Linux limitation */
|
---|
898 | if ((levels > 0) && (root_limit == root_count)) {
|
---|
899 | EXT4FS_DBG("Directory index is full");
|
---|
900 | return ENOSPC;
|
---|
901 | }
|
---|
902 |
|
---|
903 | /* Add new block to directory */
|
---|
904 | uint32_t new_fblock;
|
---|
905 | uint32_t new_iblock;
|
---|
906 | rc = ext4_filesystem_append_inode_block(
|
---|
907 | inode_ref, &new_fblock, &new_iblock);
|
---|
908 | if (rc != EOK) {
|
---|
909 | return rc;
|
---|
910 | }
|
---|
911 |
|
---|
912 | /* load new block */
|
---|
913 | block_t * new_block;
|
---|
914 | rc = block_get(&new_block, inode_ref->fs->device,
|
---|
915 | new_fblock, BLOCK_FLAGS_NOREAD);
|
---|
916 | if (rc != EOK) {
|
---|
917 | return rc;
|
---|
918 | }
|
---|
919 |
|
---|
920 | ext4_directory_dx_node_t *new_node = new_block->data;
|
---|
921 | ext4_directory_dx_entry_t *new_entries = new_node->entries;
|
---|
922 |
|
---|
923 | uint32_t block_size = ext4_superblock_get_block_size(
|
---|
924 | inode_ref->fs->superblock);
|
---|
925 |
|
---|
926 | /* Split leaf node */
|
---|
927 | if (levels > 0) {
|
---|
928 |
|
---|
929 | uint32_t count_left = leaf_count / 2;
|
---|
930 | uint32_t count_right = leaf_count - count_left;
|
---|
931 | uint32_t hash_right =
|
---|
932 | ext4_directory_dx_entry_get_hash(entries + count_left);
|
---|
933 |
|
---|
934 | /* Copy data to new node */
|
---|
935 | memcpy((void *) new_entries, (void *) (entries + count_left),
|
---|
936 | count_right * sizeof(ext4_directory_dx_entry_t));
|
---|
937 |
|
---|
938 | /* Initialize new node */
|
---|
939 | ext4_directory_dx_countlimit_t *left_countlimit =
|
---|
940 | (ext4_directory_dx_countlimit_t *)entries;
|
---|
941 | ext4_directory_dx_countlimit_t *right_countlimit =
|
---|
942 | (ext4_directory_dx_countlimit_t *)new_entries;
|
---|
943 |
|
---|
944 | ext4_directory_dx_countlimit_set_count(left_countlimit, count_left);
|
---|
945 | ext4_directory_dx_countlimit_set_count(right_countlimit, count_right);
|
---|
946 |
|
---|
947 | uint32_t entry_space = block_size - sizeof(ext4_fake_directory_entry_t);
|
---|
948 | uint32_t node_limit = entry_space / sizeof(ext4_directory_dx_entry_t);
|
---|
949 | ext4_directory_dx_countlimit_set_limit(right_countlimit, node_limit);
|
---|
950 |
|
---|
951 | /* Which index block is target for new entry */
|
---|
952 | uint32_t position_index = (dx_block->position - dx_block->entries);
|
---|
953 | if (position_index >= count_left) {
|
---|
954 |
|
---|
955 | dx_block->block->dirty = true;
|
---|
956 |
|
---|
957 | block_t *block_tmp = dx_block->block;
|
---|
958 | dx_block->block = new_block;
|
---|
959 | dx_block->position = new_entries + position_index - count_left;
|
---|
960 | dx_block->entries = new_entries;
|
---|
961 |
|
---|
962 | new_block = block_tmp;
|
---|
963 |
|
---|
964 | }
|
---|
965 |
|
---|
966 | /* Finally insert new entry */
|
---|
967 | ext4_directory_dx_insert_entry(dx_blocks, hash_right, new_iblock);
|
---|
968 |
|
---|
969 | return block_put(new_block);
|
---|
970 |
|
---|
971 | } else {
|
---|
972 |
|
---|
973 | /* Create second level index */
|
---|
974 |
|
---|
975 | /* Copy data from root to child block */
|
---|
976 | memcpy((void *) new_entries, (void *) entries,
|
---|
977 | leaf_count * sizeof(ext4_directory_dx_entry_t));
|
---|
978 |
|
---|
979 | ext4_directory_dx_countlimit_t *new_countlimit =
|
---|
980 | (ext4_directory_dx_countlimit_t *)new_entries;
|
---|
981 |
|
---|
982 | uint32_t entry_space = block_size - sizeof(ext4_fake_directory_entry_t);
|
---|
983 | uint32_t node_limit = entry_space / sizeof(ext4_directory_dx_entry_t);
|
---|
984 | ext4_directory_dx_countlimit_set_limit(new_countlimit, node_limit);
|
---|
985 |
|
---|
986 | /* Set values in root node */
|
---|
987 | ext4_directory_dx_countlimit_t *new_root_countlimit =
|
---|
988 | (ext4_directory_dx_countlimit_t *)entries;
|
---|
989 |
|
---|
990 | ext4_directory_dx_countlimit_set_count(new_root_countlimit, 1);
|
---|
991 | ext4_directory_dx_entry_set_block(entries, new_iblock);
|
---|
992 |
|
---|
993 | ((ext4_directory_dx_root_t *)dx_blocks[0].block->data)->info.indirect_levels = 1;
|
---|
994 |
|
---|
995 | /* Add new entry to the path */
|
---|
996 | dx_block = dx_blocks + 1;
|
---|
997 | dx_block->position = dx_block->position - entries + new_entries;
|
---|
998 | dx_block->entries = new_entries;
|
---|
999 | dx_block->block = new_block;
|
---|
1000 | }
|
---|
1001 |
|
---|
1002 | }
|
---|
1003 |
|
---|
1004 | return EOK;
|
---|
1005 | }
|
---|
1006 |
|
---|
1007 | /** Add new entry to indexed directory
|
---|
1008 | *
|
---|
1009 | * @param parent directory i-node
|
---|
1010 | * @param child i-node to be referenced from directory entry
|
---|
1011 | * @param name name of new directory entry
|
---|
1012 | * @return error code
|
---|
1013 | */
|
---|
1014 | int ext4_directory_dx_add_entry(ext4_inode_ref_t *parent,
|
---|
1015 | ext4_inode_ref_t *child, const char *name)
|
---|
1016 | {
|
---|
1017 | int rc = EOK;
|
---|
1018 | int rc2 = EOK;
|
---|
1019 |
|
---|
1020 | /* get direct block 0 (index root) */
|
---|
1021 | uint32_t root_block_addr;
|
---|
1022 | rc = ext4_filesystem_get_inode_data_block_index(parent, 0, &root_block_addr);
|
---|
1023 | if (rc != EOK) {
|
---|
1024 | return rc;
|
---|
1025 | }
|
---|
1026 |
|
---|
1027 | ext4_filesystem_t *fs = parent->fs;
|
---|
1028 |
|
---|
1029 | block_t *root_block;
|
---|
1030 | rc = block_get(&root_block, fs->device, root_block_addr, BLOCK_FLAGS_NONE);
|
---|
1031 | if (rc != EOK) {
|
---|
1032 | return rc;
|
---|
1033 | }
|
---|
1034 |
|
---|
1035 | /* Initialize hinfo structure (mainly compute hash) */
|
---|
1036 | uint32_t name_len = strlen(name);
|
---|
1037 | ext4_hash_info_t hinfo;
|
---|
1038 | rc = ext4_directory_hinfo_init(&hinfo, root_block, fs->superblock, name_len, name);
|
---|
1039 | if (rc != EOK) {
|
---|
1040 | block_put(root_block);
|
---|
1041 | EXT4FS_DBG("hinfo initialization error");
|
---|
1042 | return EXT4_ERR_BAD_DX_DIR;
|
---|
1043 | }
|
---|
1044 |
|
---|
1045 | /* Hardcoded number 2 means maximum height of index tree defined in linux */
|
---|
1046 | ext4_directory_dx_block_t dx_blocks[2];
|
---|
1047 | ext4_directory_dx_block_t *dx_block, *dx_it;
|
---|
1048 | rc = ext4_directory_dx_get_leaf(&hinfo, parent, root_block, &dx_block, dx_blocks);
|
---|
1049 | if (rc != EOK) {
|
---|
1050 | EXT4FS_DBG("get leaf error");
|
---|
1051 | rc = EXT4_ERR_BAD_DX_DIR;
|
---|
1052 | goto release_index;
|
---|
1053 | }
|
---|
1054 |
|
---|
1055 |
|
---|
1056 | /* Try to insert to existing data block */
|
---|
1057 | uint32_t leaf_block_idx = ext4_directory_dx_entry_get_block(dx_block->position);
|
---|
1058 | uint32_t leaf_block_addr;
|
---|
1059 | rc = ext4_filesystem_get_inode_data_block_index(parent, leaf_block_idx, &leaf_block_addr);
|
---|
1060 | if (rc != EOK) {
|
---|
1061 | goto release_index;
|
---|
1062 | }
|
---|
1063 |
|
---|
1064 |
|
---|
1065 | block_t *target_block;
|
---|
1066 | rc = block_get(&target_block, fs->device, leaf_block_addr, BLOCK_FLAGS_NONE);
|
---|
1067 | if (rc != EOK) {
|
---|
1068 | goto release_index;
|
---|
1069 | }
|
---|
1070 |
|
---|
1071 | /* Check if insert operation passed */
|
---|
1072 | rc = ext4_directory_try_insert_entry(fs->superblock, target_block, child, name, name_len);
|
---|
1073 | if (rc == EOK) {
|
---|
1074 | goto release_target_index;
|
---|
1075 | }
|
---|
1076 |
|
---|
1077 | /* Check if there is needed to split index node
|
---|
1078 | * (and recursively also parent nodes)
|
---|
1079 | */
|
---|
1080 | rc = ext4_directory_dx_split_index(parent, dx_blocks, dx_block);
|
---|
1081 | if (rc != EOK) {
|
---|
1082 | goto release_target_index;
|
---|
1083 | }
|
---|
1084 |
|
---|
1085 | /* Split entries to two blocks (includes sorting by hash value) */
|
---|
1086 | block_t *new_block = NULL;
|
---|
1087 | rc = ext4_directory_dx_split_data(parent, &hinfo, target_block, dx_block, &new_block);
|
---|
1088 | if (rc != EOK) {
|
---|
1089 | rc2 = rc;
|
---|
1090 | goto release_target_index;
|
---|
1091 | }
|
---|
1092 |
|
---|
1093 | /* Where to save new entry */
|
---|
1094 | uint32_t new_block_hash = ext4_directory_dx_entry_get_hash(dx_block->position + 1);
|
---|
1095 | if (hinfo.hash >= new_block_hash) {
|
---|
1096 | rc = ext4_directory_try_insert_entry(fs->superblock, new_block, child, name, name_len);
|
---|
1097 | } else {
|
---|
1098 | rc = ext4_directory_try_insert_entry(fs->superblock, target_block, child, name, name_len);
|
---|
1099 | }
|
---|
1100 |
|
---|
1101 | /* Cleanup */
|
---|
1102 | rc = block_put(new_block);
|
---|
1103 | if (rc != EOK) {
|
---|
1104 | EXT4FS_DBG("error writing new block");
|
---|
1105 | return rc;
|
---|
1106 | }
|
---|
1107 |
|
---|
1108 | /* Cleanup operations */
|
---|
1109 |
|
---|
1110 | release_target_index:
|
---|
1111 |
|
---|
1112 | rc2 = rc;
|
---|
1113 |
|
---|
1114 | rc = block_put(target_block);
|
---|
1115 | if (rc != EOK) {
|
---|
1116 | EXT4FS_DBG("error writing target block");
|
---|
1117 | return rc;
|
---|
1118 | }
|
---|
1119 |
|
---|
1120 | release_index:
|
---|
1121 |
|
---|
1122 | if (rc != EOK) {
|
---|
1123 | rc2 = rc;
|
---|
1124 | }
|
---|
1125 |
|
---|
1126 | dx_it = dx_blocks;
|
---|
1127 |
|
---|
1128 | while (dx_it <= dx_block) {
|
---|
1129 | rc = block_put(dx_it->block);
|
---|
1130 | if (rc != EOK) {
|
---|
1131 | EXT4FS_DBG("error writing index block");
|
---|
1132 | return rc;
|
---|
1133 | }
|
---|
1134 | dx_it++;
|
---|
1135 | }
|
---|
1136 |
|
---|
1137 | return rc2;
|
---|
1138 | }
|
---|
1139 |
|
---|
1140 |
|
---|
1141 | /**
|
---|
1142 | * @}
|
---|
1143 | */
|
---|