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.c
|
---|
35 | * @brief Ext4 directory structure operations.
|
---|
36 | */
|
---|
37 |
|
---|
38 | #include <byteorder.h>
|
---|
39 | #include <errno.h>
|
---|
40 | #include <malloc.h>
|
---|
41 | #include <string.h>
|
---|
42 | #include "libext4.h"
|
---|
43 |
|
---|
44 | static int ext4_directory_iterator_set(ext4_directory_iterator_t *,
|
---|
45 | uint32_t);
|
---|
46 |
|
---|
47 |
|
---|
48 | uint32_t ext4_directory_entry_ll_get_inode(ext4_directory_entry_ll_t *de)
|
---|
49 | {
|
---|
50 | return uint32_t_le2host(de->inode);
|
---|
51 | }
|
---|
52 |
|
---|
53 | void ext4_directory_entry_ll_set_inode(ext4_directory_entry_ll_t *de,
|
---|
54 | uint32_t inode)
|
---|
55 | {
|
---|
56 | de->inode = host2uint32_t_le(inode);
|
---|
57 | }
|
---|
58 |
|
---|
59 | uint16_t ext4_directory_entry_ll_get_entry_length(
|
---|
60 | ext4_directory_entry_ll_t *de)
|
---|
61 | {
|
---|
62 | return uint16_t_le2host(de->entry_length);
|
---|
63 | }
|
---|
64 |
|
---|
65 | void ext4_directory_entry_ll_set_entry_length(ext4_directory_entry_ll_t *de,
|
---|
66 | uint16_t length)
|
---|
67 | {
|
---|
68 | de->entry_length = host2uint16_t_le(length);
|
---|
69 | }
|
---|
70 |
|
---|
71 | uint16_t ext4_directory_entry_ll_get_name_length(
|
---|
72 | ext4_superblock_t *sb, ext4_directory_entry_ll_t *de)
|
---|
73 | {
|
---|
74 | if (ext4_superblock_get_rev_level(sb) == 0 &&
|
---|
75 | ext4_superblock_get_minor_rev_level(sb) < 5) {
|
---|
76 |
|
---|
77 | return ((uint16_t)de->name_length_high) << 8 |
|
---|
78 | ((uint16_t)de->name_length);
|
---|
79 |
|
---|
80 | }
|
---|
81 | return de->name_length;
|
---|
82 |
|
---|
83 | }
|
---|
84 |
|
---|
85 | void ext4_directory_entry_ll_set_name_length(ext4_superblock_t *sb,
|
---|
86 | ext4_directory_entry_ll_t *de, uint16_t length)
|
---|
87 | {
|
---|
88 | de->name_length = (length << 8) >> 8;
|
---|
89 |
|
---|
90 | if (ext4_superblock_get_rev_level(sb) == 0 &&
|
---|
91 | ext4_superblock_get_minor_rev_level(sb) < 5) {
|
---|
92 |
|
---|
93 | de->name_length_high = length >> 8;
|
---|
94 | }
|
---|
95 | }
|
---|
96 |
|
---|
97 | uint8_t ext4_directory_entry_ll_get_inode_type(
|
---|
98 | ext4_superblock_t *sb, ext4_directory_entry_ll_t *de)
|
---|
99 | {
|
---|
100 | if (ext4_superblock_get_rev_level(sb) > 0 ||
|
---|
101 | ext4_superblock_get_minor_rev_level(sb) >= 5) {
|
---|
102 |
|
---|
103 | return de->inode_type;
|
---|
104 | }
|
---|
105 |
|
---|
106 | return EXT4_DIRECTORY_FILETYPE_UNKNOWN;
|
---|
107 |
|
---|
108 | }
|
---|
109 |
|
---|
110 | void ext4_directory_entry_ll_set_inode_type(
|
---|
111 | ext4_superblock_t *sb, ext4_directory_entry_ll_t *de, uint8_t type)
|
---|
112 | {
|
---|
113 | if (ext4_superblock_get_rev_level(sb) > 0 ||
|
---|
114 | ext4_superblock_get_minor_rev_level(sb) >= 5) {
|
---|
115 |
|
---|
116 | de->inode_type = type;
|
---|
117 | }
|
---|
118 |
|
---|
119 | // else do nothing
|
---|
120 |
|
---|
121 | }
|
---|
122 |
|
---|
123 | int ext4_directory_iterator_init(ext4_directory_iterator_t *it,
|
---|
124 | ext4_filesystem_t *fs, ext4_inode_ref_t *inode_ref, aoff64_t pos)
|
---|
125 | {
|
---|
126 | it->inode_ref = inode_ref;
|
---|
127 | it->fs = fs;
|
---|
128 | it->current = NULL;
|
---|
129 | it->current_offset = 0;
|
---|
130 | it->current_block = NULL;
|
---|
131 |
|
---|
132 | return ext4_directory_iterator_seek(it, pos);
|
---|
133 | }
|
---|
134 |
|
---|
135 |
|
---|
136 | int ext4_directory_iterator_next(ext4_directory_iterator_t *it)
|
---|
137 | {
|
---|
138 | uint16_t skip;
|
---|
139 |
|
---|
140 | assert(it->current != NULL);
|
---|
141 |
|
---|
142 | skip = ext4_directory_entry_ll_get_entry_length(it->current);
|
---|
143 |
|
---|
144 | return ext4_directory_iterator_seek(it, it->current_offset + skip);
|
---|
145 | }
|
---|
146 |
|
---|
147 |
|
---|
148 | int ext4_directory_iterator_seek(ext4_directory_iterator_t *it, aoff64_t pos)
|
---|
149 | {
|
---|
150 | int rc;
|
---|
151 |
|
---|
152 | uint64_t size = ext4_inode_get_size(it->fs->superblock, it->inode_ref->inode);
|
---|
153 |
|
---|
154 | /* The iterator is not valid until we seek to the desired position */
|
---|
155 | it->current = NULL;
|
---|
156 |
|
---|
157 | /* Are we at the end? */
|
---|
158 | if (pos >= size) {
|
---|
159 | if (it->current_block) {
|
---|
160 | rc = block_put(it->current_block);
|
---|
161 | it->current_block = NULL;
|
---|
162 | if (rc != EOK) {
|
---|
163 | return rc;
|
---|
164 | }
|
---|
165 | }
|
---|
166 |
|
---|
167 | it->current_offset = pos;
|
---|
168 | return EOK;
|
---|
169 | }
|
---|
170 |
|
---|
171 | uint32_t block_size = ext4_superblock_get_block_size(it->fs->superblock);
|
---|
172 | aoff64_t current_block_idx = it->current_offset / block_size;
|
---|
173 | aoff64_t next_block_idx = pos / block_size;
|
---|
174 |
|
---|
175 | /* If we don't have a block or are moving accross block boundary,
|
---|
176 | * we need to get another block
|
---|
177 | */
|
---|
178 | if (it->current_block == NULL || current_block_idx != next_block_idx) {
|
---|
179 | if (it->current_block) {
|
---|
180 | rc = block_put(it->current_block);
|
---|
181 | it->current_block = NULL;
|
---|
182 | if (rc != EOK) {
|
---|
183 | return rc;
|
---|
184 | }
|
---|
185 | }
|
---|
186 |
|
---|
187 | uint32_t next_block_phys_idx;
|
---|
188 | rc = ext4_filesystem_get_inode_data_block_index(it->inode_ref,
|
---|
189 | next_block_idx, &next_block_phys_idx);
|
---|
190 | if (rc != EOK) {
|
---|
191 | return rc;
|
---|
192 | }
|
---|
193 |
|
---|
194 | rc = block_get(&it->current_block, it->fs->device, next_block_phys_idx,
|
---|
195 | BLOCK_FLAGS_NONE);
|
---|
196 | if (rc != EOK) {
|
---|
197 | it->current_block = NULL;
|
---|
198 | return rc;
|
---|
199 | }
|
---|
200 | }
|
---|
201 |
|
---|
202 | it->current_offset = pos;
|
---|
203 |
|
---|
204 | return ext4_directory_iterator_set(it, block_size);
|
---|
205 | }
|
---|
206 |
|
---|
207 | static int ext4_directory_iterator_set(ext4_directory_iterator_t *it,
|
---|
208 | uint32_t block_size)
|
---|
209 | {
|
---|
210 |
|
---|
211 | it->current = NULL;
|
---|
212 |
|
---|
213 | uint32_t offset_in_block = it->current_offset % block_size;
|
---|
214 |
|
---|
215 | /* Ensure proper alignment */
|
---|
216 | if ((offset_in_block % 4) != 0) {
|
---|
217 | return EIO;
|
---|
218 | }
|
---|
219 |
|
---|
220 | /* Ensure that the core of the entry does not overflow the block */
|
---|
221 | if (offset_in_block > block_size - 8) {
|
---|
222 | return EIO;
|
---|
223 | }
|
---|
224 |
|
---|
225 | ext4_directory_entry_ll_t *entry = it->current_block->data + offset_in_block;
|
---|
226 |
|
---|
227 | /* Ensure that the whole entry does not overflow the block */
|
---|
228 | uint16_t length = ext4_directory_entry_ll_get_entry_length(entry);
|
---|
229 | if (offset_in_block + length > block_size) {
|
---|
230 | return EIO;
|
---|
231 | }
|
---|
232 |
|
---|
233 | /* Ensure the name length is not too large */
|
---|
234 | if (ext4_directory_entry_ll_get_name_length(it->fs->superblock,
|
---|
235 | entry) > length-8) {
|
---|
236 | return EIO;
|
---|
237 | }
|
---|
238 |
|
---|
239 | it->current = entry;
|
---|
240 | return EOK;
|
---|
241 | }
|
---|
242 |
|
---|
243 |
|
---|
244 | int ext4_directory_iterator_fini(ext4_directory_iterator_t *it)
|
---|
245 | {
|
---|
246 | int rc;
|
---|
247 |
|
---|
248 | it->fs = NULL;
|
---|
249 | it->inode_ref = NULL;
|
---|
250 | it->current = NULL;
|
---|
251 |
|
---|
252 | if (it->current_block) {
|
---|
253 | rc = block_put(it->current_block);
|
---|
254 | if (rc != EOK) {
|
---|
255 | return rc;
|
---|
256 | }
|
---|
257 | }
|
---|
258 |
|
---|
259 | return EOK;
|
---|
260 | }
|
---|
261 |
|
---|
262 | void ext4_directory_write_entry(ext4_superblock_t *sb,
|
---|
263 | ext4_directory_entry_ll_t *entry, uint16_t entry_len,
|
---|
264 | ext4_inode_ref_t *child, const char *name, size_t name_len)
|
---|
265 | {
|
---|
266 |
|
---|
267 | EXT4FS_DBG("writing entry \%s, len \%u, addr = \%u", name, entry_len, (uint32_t)entry);
|
---|
268 |
|
---|
269 | ext4_directory_entry_ll_set_inode(entry, child->index);
|
---|
270 | ext4_directory_entry_ll_set_entry_length(entry, entry_len);
|
---|
271 | ext4_directory_entry_ll_set_name_length(sb, entry, name_len);
|
---|
272 |
|
---|
273 | if (ext4_inode_is_type(sb, child->inode, EXT4_INODE_MODE_DIRECTORY)) {
|
---|
274 | ext4_directory_entry_ll_set_inode_type(
|
---|
275 | sb, entry, EXT4_DIRECTORY_FILETYPE_DIR);
|
---|
276 | } else {
|
---|
277 | ext4_directory_entry_ll_set_inode_type(
|
---|
278 | sb, entry, EXT4_DIRECTORY_FILETYPE_REG_FILE);
|
---|
279 | }
|
---|
280 | memcpy(entry->name, name, name_len);
|
---|
281 | }
|
---|
282 |
|
---|
283 | int ext4_directory_add_entry(ext4_inode_ref_t * parent,
|
---|
284 | const char *name, ext4_inode_ref_t *child)
|
---|
285 | {
|
---|
286 | int rc;
|
---|
287 |
|
---|
288 | EXT4FS_DBG("adding entry to directory \%u [ino = \%u, name = \%s]", parent->index, child->index, name);
|
---|
289 |
|
---|
290 | ext4_filesystem_t *fs = parent->fs;
|
---|
291 |
|
---|
292 | // Index adding (if allowed)
|
---|
293 | if (ext4_superblock_has_feature_compatible(fs->superblock, EXT4_FEATURE_COMPAT_DIR_INDEX) &&
|
---|
294 | ext4_inode_has_flag(parent->inode, EXT4_INODE_FLAG_INDEX)) {
|
---|
295 |
|
---|
296 | EXT4FS_DBG("index");
|
---|
297 |
|
---|
298 | rc = ext4_directory_dx_add_entry(parent, child, name);
|
---|
299 |
|
---|
300 | // Check if index is not corrupted
|
---|
301 | if (rc != EXT4_ERR_BAD_DX_DIR) {
|
---|
302 |
|
---|
303 | if (rc != EOK) {
|
---|
304 | return rc;
|
---|
305 | }
|
---|
306 |
|
---|
307 | return EOK;
|
---|
308 | }
|
---|
309 |
|
---|
310 | // Needed to clear dir index flag
|
---|
311 | ext4_inode_clear_flag(parent->inode, EXT4_INODE_FLAG_INDEX);
|
---|
312 | parent->dirty = true;
|
---|
313 |
|
---|
314 | EXT4FS_DBG("index is corrupted - doing linear algorithm, index flag cleared");
|
---|
315 | }
|
---|
316 |
|
---|
317 | // Linear algorithm
|
---|
318 |
|
---|
319 | uint32_t iblock = 0, fblock = 0;
|
---|
320 | uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
|
---|
321 | uint32_t inode_size = ext4_inode_get_size(fs->superblock, parent->inode);
|
---|
322 | uint32_t total_blocks = inode_size / block_size;
|
---|
323 |
|
---|
324 | uint32_t name_len = strlen(name);
|
---|
325 |
|
---|
326 | // Find block, where is space for new entry
|
---|
327 | bool success = false;
|
---|
328 | for (iblock = 0; iblock < total_blocks; ++iblock) {
|
---|
329 |
|
---|
330 | rc = ext4_filesystem_get_inode_data_block_index(parent, iblock, &fblock);
|
---|
331 | if (rc != EOK) {
|
---|
332 | return rc;
|
---|
333 | }
|
---|
334 |
|
---|
335 | block_t *block;
|
---|
336 | rc = block_get(&block, fs->device, fblock, BLOCK_FLAGS_NONE);
|
---|
337 | if (rc != EOK) {
|
---|
338 | return rc;
|
---|
339 | }
|
---|
340 |
|
---|
341 | rc = ext4_directory_try_insert_entry(fs->superblock, block, child, name, name_len);
|
---|
342 | if (rc == EOK) {
|
---|
343 | success = true;
|
---|
344 | }
|
---|
345 |
|
---|
346 | rc = block_put(block);
|
---|
347 | if (rc != EOK) {
|
---|
348 | return rc;
|
---|
349 | }
|
---|
350 |
|
---|
351 | if (success) {
|
---|
352 | return EOK;
|
---|
353 | }
|
---|
354 | }
|
---|
355 |
|
---|
356 | // No free block found - needed to allocate next block
|
---|
357 |
|
---|
358 | iblock = 0;
|
---|
359 | fblock = 0;
|
---|
360 | rc = ext4_filesystem_append_inode_block(parent, &fblock, &iblock);
|
---|
361 | if (rc != EOK) {
|
---|
362 | return rc;
|
---|
363 | }
|
---|
364 |
|
---|
365 | EXT4FS_DBG("using iblock \%u fblock \%u", iblock, fblock);
|
---|
366 |
|
---|
367 | // Load new block
|
---|
368 | block_t *new_block;
|
---|
369 | rc = block_get(&new_block, fs->device, fblock, BLOCK_FLAGS_NOREAD);
|
---|
370 | if (rc != EOK) {
|
---|
371 | return rc;
|
---|
372 | }
|
---|
373 |
|
---|
374 | // Fill block with zeroes
|
---|
375 | memset(new_block->data, 0, block_size);
|
---|
376 | ext4_directory_entry_ll_t *block_entry = new_block->data;
|
---|
377 | ext4_directory_write_entry(fs->superblock, block_entry, block_size, child, name, name_len);
|
---|
378 |
|
---|
379 | // Save new block
|
---|
380 | new_block->dirty = true;
|
---|
381 | rc = block_put(new_block);
|
---|
382 | if (rc != EOK) {
|
---|
383 | return rc;
|
---|
384 | }
|
---|
385 |
|
---|
386 | return EOK;
|
---|
387 | }
|
---|
388 |
|
---|
389 | int ext4_directory_find_entry(ext4_directory_search_result_t *result,
|
---|
390 | ext4_inode_ref_t *parent, const char *name)
|
---|
391 | {
|
---|
392 | int rc;
|
---|
393 | uint32_t name_len = strlen(name);
|
---|
394 |
|
---|
395 | ext4_superblock_t *sb = parent->fs->superblock;
|
---|
396 |
|
---|
397 | // Index search
|
---|
398 | if (ext4_superblock_has_feature_compatible(sb, EXT4_FEATURE_COMPAT_DIR_INDEX) &&
|
---|
399 | ext4_inode_has_flag(parent->inode, EXT4_INODE_FLAG_INDEX)) {
|
---|
400 |
|
---|
401 | rc = ext4_directory_dx_find_entry(result, parent, name_len, name);
|
---|
402 |
|
---|
403 | // Check if index is not corrupted
|
---|
404 | if (rc != EXT4_ERR_BAD_DX_DIR) {
|
---|
405 |
|
---|
406 | if (rc != EOK) {
|
---|
407 | return rc;
|
---|
408 | }
|
---|
409 | return EOK;
|
---|
410 | }
|
---|
411 |
|
---|
412 | EXT4FS_DBG("index is corrupted - doing linear search");
|
---|
413 | }
|
---|
414 |
|
---|
415 | uint32_t iblock, fblock;
|
---|
416 | uint32_t block_size = ext4_superblock_get_block_size(sb);
|
---|
417 | uint32_t inode_size = ext4_inode_get_size(sb, parent->inode);
|
---|
418 | uint32_t total_blocks = inode_size / block_size;
|
---|
419 |
|
---|
420 | for (iblock = 0; iblock < total_blocks; ++iblock) {
|
---|
421 |
|
---|
422 | rc = ext4_filesystem_get_inode_data_block_index(parent, iblock, &fblock);
|
---|
423 | if (rc != EOK) {
|
---|
424 | return rc;
|
---|
425 | }
|
---|
426 |
|
---|
427 | block_t *block;
|
---|
428 | rc = block_get(&block, parent->fs->device, fblock, BLOCK_FLAGS_NONE);
|
---|
429 | if (rc != EOK) {
|
---|
430 | return rc;
|
---|
431 | }
|
---|
432 |
|
---|
433 | // find block entry
|
---|
434 | ext4_directory_entry_ll_t *res_entry;
|
---|
435 | rc = ext4_directory_find_in_block(block, sb, name_len, name, &res_entry);
|
---|
436 | if (rc == EOK) {
|
---|
437 | result->block = block;
|
---|
438 | result->dentry = res_entry;
|
---|
439 | return EOK;
|
---|
440 | }
|
---|
441 |
|
---|
442 | rc = block_put(block);
|
---|
443 | if (rc != EOK) {
|
---|
444 | return rc;
|
---|
445 | }
|
---|
446 | }
|
---|
447 |
|
---|
448 | result->block = NULL;
|
---|
449 | result->dentry = NULL;
|
---|
450 |
|
---|
451 | return ENOENT;
|
---|
452 | }
|
---|
453 |
|
---|
454 |
|
---|
455 | int ext4_directory_remove_entry(ext4_inode_ref_t *parent, const char *name)
|
---|
456 | {
|
---|
457 | int rc;
|
---|
458 |
|
---|
459 | if (!ext4_inode_is_type(parent->fs->superblock, parent->inode,
|
---|
460 | EXT4_INODE_MODE_DIRECTORY)) {
|
---|
461 | return ENOTDIR;
|
---|
462 | }
|
---|
463 |
|
---|
464 | ext4_directory_search_result_t result;
|
---|
465 | rc = ext4_directory_find_entry(&result, parent, name);
|
---|
466 | if (rc != EOK) {
|
---|
467 | return rc;
|
---|
468 | }
|
---|
469 |
|
---|
470 | ext4_directory_entry_ll_set_inode(result.dentry, 0);
|
---|
471 |
|
---|
472 | uint32_t pos = (void *)result.dentry - result.block->data;
|
---|
473 |
|
---|
474 | uint32_t offset = 0;
|
---|
475 | if (pos != 0) {
|
---|
476 |
|
---|
477 | ext4_directory_entry_ll_t *tmp_dentry = result.block->data;
|
---|
478 | uint16_t tmp_dentry_length =
|
---|
479 | ext4_directory_entry_ll_get_entry_length(tmp_dentry);
|
---|
480 |
|
---|
481 | while ((offset + tmp_dentry_length) < pos) {
|
---|
482 | offset += ext4_directory_entry_ll_get_entry_length(tmp_dentry);
|
---|
483 | tmp_dentry = result.block->data + offset;
|
---|
484 | tmp_dentry_length =
|
---|
485 | ext4_directory_entry_ll_get_entry_length(tmp_dentry);
|
---|
486 | }
|
---|
487 |
|
---|
488 | assert(tmp_dentry_length + offset == pos);
|
---|
489 |
|
---|
490 | uint16_t del_entry_length =
|
---|
491 | ext4_directory_entry_ll_get_entry_length(result.dentry);
|
---|
492 | ext4_directory_entry_ll_set_entry_length(tmp_dentry,
|
---|
493 | tmp_dentry_length + del_entry_length);
|
---|
494 |
|
---|
495 | }
|
---|
496 |
|
---|
497 | result.block->dirty = true;
|
---|
498 |
|
---|
499 | return ext4_directory_destroy_result(&result);
|
---|
500 | }
|
---|
501 |
|
---|
502 |
|
---|
503 | int ext4_directory_try_insert_entry(ext4_superblock_t *sb,
|
---|
504 | block_t *target_block, ext4_inode_ref_t *child,
|
---|
505 | const char *name, uint32_t name_len)
|
---|
506 | {
|
---|
507 | uint32_t block_size = ext4_superblock_get_block_size(sb);
|
---|
508 | uint16_t required_len = sizeof(ext4_fake_directory_entry_t) + name_len;
|
---|
509 | if ((required_len % 4) != 0) {
|
---|
510 | required_len += 4 - (required_len % 4);
|
---|
511 | }
|
---|
512 |
|
---|
513 | ext4_directory_entry_ll_t *dentry = target_block->data;
|
---|
514 | ext4_directory_entry_ll_t *stop = target_block->data + block_size;
|
---|
515 |
|
---|
516 | while (dentry < stop) {
|
---|
517 |
|
---|
518 | uint32_t inode = ext4_directory_entry_ll_get_inode(dentry);
|
---|
519 | uint16_t rec_len = ext4_directory_entry_ll_get_entry_length(dentry);
|
---|
520 |
|
---|
521 | if ((inode == 0) && (rec_len >= required_len)) {
|
---|
522 | ext4_directory_write_entry(sb, dentry, rec_len, child, name, name_len);
|
---|
523 | target_block->dirty = true;
|
---|
524 | return EOK;
|
---|
525 | }
|
---|
526 |
|
---|
527 | if (inode != 0) {
|
---|
528 | uint16_t used_name_len =
|
---|
529 | ext4_directory_entry_ll_get_name_length(sb, dentry);
|
---|
530 |
|
---|
531 | uint16_t used_space =
|
---|
532 | sizeof(ext4_fake_directory_entry_t) + used_name_len;
|
---|
533 | if ((used_name_len % 4) != 0) {
|
---|
534 | used_space += 4 - (used_name_len % 4);
|
---|
535 | }
|
---|
536 | uint16_t free_space = rec_len - used_space;
|
---|
537 |
|
---|
538 | if (free_space >= required_len) {
|
---|
539 |
|
---|
540 | // Cut tail of current entry
|
---|
541 | ext4_directory_entry_ll_set_entry_length(dentry, used_space);
|
---|
542 | ext4_directory_entry_ll_t *new_entry =
|
---|
543 | (void *)dentry + used_space;
|
---|
544 | ext4_directory_write_entry(sb, new_entry,
|
---|
545 | free_space, child, name, name_len);
|
---|
546 |
|
---|
547 | target_block->dirty = true;
|
---|
548 | return EOK;
|
---|
549 | }
|
---|
550 | }
|
---|
551 |
|
---|
552 | dentry = (void *)dentry + rec_len;
|
---|
553 | }
|
---|
554 |
|
---|
555 | return ENOSPC;
|
---|
556 | }
|
---|
557 |
|
---|
558 | int ext4_directory_find_in_block(block_t *block,
|
---|
559 | ext4_superblock_t *sb, size_t name_len, const char *name,
|
---|
560 | ext4_directory_entry_ll_t **res_entry)
|
---|
561 | {
|
---|
562 |
|
---|
563 | ext4_directory_entry_ll_t *dentry = (ext4_directory_entry_ll_t *)block->data;
|
---|
564 | uint8_t *addr_limit = block->data + ext4_superblock_get_block_size(sb);
|
---|
565 |
|
---|
566 | while ((uint8_t *)dentry < addr_limit) {
|
---|
567 |
|
---|
568 | if ((uint8_t*) dentry + name_len > addr_limit) {
|
---|
569 | break;
|
---|
570 | }
|
---|
571 |
|
---|
572 | if (dentry->inode != 0) {
|
---|
573 | if (name_len == ext4_directory_entry_ll_get_name_length(sb, dentry)) {
|
---|
574 | // Compare names
|
---|
575 | if (bcmp((uint8_t *)name, dentry->name, name_len) == 0) {
|
---|
576 | *res_entry = dentry;
|
---|
577 | return EOK;
|
---|
578 | }
|
---|
579 | }
|
---|
580 | }
|
---|
581 |
|
---|
582 | // Goto next entry
|
---|
583 | uint16_t dentry_len = ext4_directory_entry_ll_get_entry_length(dentry);
|
---|
584 |
|
---|
585 | if (dentry_len == 0) {
|
---|
586 | return EINVAL;
|
---|
587 | }
|
---|
588 |
|
---|
589 | dentry = (ext4_directory_entry_ll_t *)((uint8_t *)dentry + dentry_len);
|
---|
590 | }
|
---|
591 |
|
---|
592 | return ENOENT;
|
---|
593 | }
|
---|
594 |
|
---|
595 | int ext4_directory_destroy_result(ext4_directory_search_result_t *result)
|
---|
596 | {
|
---|
597 | if (result->block) {
|
---|
598 | return block_put(result->block);
|
---|
599 | }
|
---|
600 |
|
---|
601 | return EOK;
|
---|
602 | }
|
---|
603 |
|
---|
604 | /**
|
---|
605 | * @}
|
---|
606 | */
|
---|