2 * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com)
\r
6 * Copyright (c) 2012 Martin Sucha
\r
7 * Copyright (c) 2012 Frantisek Princ
\r
8 * All rights reserved.
\r
10 * Redistribution and use in source and binary forms, with or without
\r
11 * modification, are permitted provided that the following conditions
\r
14 * - Redistributions of source code must retain the above copyright
\r
15 * notice, this list of conditions and the following disclaimer.
\r
16 * - Redistributions in binary form must reproduce the above copyright
\r
17 * notice, this list of conditions and the following disclaimer in the
\r
18 * documentation and/or other materials provided with the distribution.
\r
19 * - The name of the author may not be used to endorse or promote products
\r
20 * derived from this software without specific prior written permission.
\r
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
\r
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
\r
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
\r
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
\r
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
\r
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
\r
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
\r
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
\r
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
\r
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\r
34 /** @addtogroup lwext4
\r
39 * @brief Directory handle procedures.
\r
42 #include <ext4_config.h>
\r
43 #include <ext4_dir.h>
\r
44 #include <ext4_dir_idx.h>
\r
45 #include <ext4_inode.h>
\r
46 #include <ext4_fs.h>
\r
50 /****************************************************************************/
\r
52 /**@brief Do some checks before returning iterator.
\r
53 * @param it Iterator to be checked
\r
54 * @param block_size Size of data block
\r
55 * @return Error code
\r
57 static int ext4_dir_iterator_set(struct ext4_directory_iterator *it,
\r
58 uint32_t block_size)
\r
62 uint32_t offset_in_block = it->current_offset % block_size;
\r
64 /* Ensure proper alignment */
\r
65 if ((offset_in_block % 4) != 0)
\r
68 /* Ensure that the core of the entry does not overflow the block */
\r
69 if (offset_in_block > block_size - 8)
\r
72 struct ext4_directory_entry_ll *entry =
\r
73 (void *)(it->current_block.data + offset_in_block);
\r
75 /* Ensure that the whole entry does not overflow the block */
\r
76 uint16_t length = ext4_dir_entry_ll_get_entry_length(entry);
\r
77 if (offset_in_block + length > block_size)
\r
80 /* Ensure the name length is not too large */
\r
81 if (ext4_dir_entry_ll_get_name_length(&it->inode_ref->fs->sb, entry) >
\r
85 /* Everything OK - "publish" the entry */
\r
86 it->current = entry;
\r
90 /**@brief Seek to next valid directory entry.
\r
91 * Here can be jumped to the next data block.
\r
92 * @param it Initialized iterator
\r
93 * @param pos Position of the next entry
\r
94 * @return Error code
\r
96 static int ext4_dir_iterator_seek(struct ext4_directory_iterator *it,
\r
100 ext4_inode_get_size(&it->inode_ref->fs->sb, it->inode_ref->inode);
\r
102 /* The iterator is not valid until we seek to the desired position */
\r
103 it->current = NULL;
\r
105 /* Are we at the end? */
\r
107 if (it->current_block.lb_id) {
\r
110 ext4_block_set(it->inode_ref->fs->bdev, &it->current_block);
\r
111 it->current_block.lb_id = 0;
\r
117 it->current_offset = pos;
\r
121 /* Compute next block address */
\r
122 uint32_t block_size = ext4_sb_get_block_size(&it->inode_ref->fs->sb);
\r
123 uint64_t current_block_idx = it->current_offset / block_size;
\r
124 uint64_t next_block_idx = pos / block_size;
\r
127 * If we don't have a block or are moving accross block boundary,
\r
128 * we need to get another block
\r
130 if ((it->current_block.lb_id == 0) ||
\r
131 (current_block_idx != next_block_idx)) {
\r
132 if (it->current_block.lb_id) {
\r
134 ext4_block_set(it->inode_ref->fs->bdev, &it->current_block);
\r
135 it->current_block.lb_id = 0;
\r
141 uint32_t next_block_phys_idx;
\r
142 int rc = ext4_fs_get_inode_data_block_index(
\r
143 it->inode_ref, next_block_idx, &next_block_phys_idx);
\r
147 rc = ext4_block_get(it->inode_ref->fs->bdev, &it->current_block,
\r
148 next_block_phys_idx);
\r
150 it->current_block.lb_id = 0;
\r
155 it->current_offset = pos;
\r
157 return ext4_dir_iterator_set(it, block_size);
\r
160 int ext4_dir_iterator_init(struct ext4_directory_iterator *it,
\r
161 struct ext4_inode_ref *inode_ref, uint64_t pos)
\r
163 it->inode_ref = inode_ref;
\r
165 it->current_offset = 0;
\r
166 it->current_block.lb_id = 0;
\r
168 return ext4_dir_iterator_seek(it, pos);
\r
171 int ext4_dir_iterator_next(struct ext4_directory_iterator *it)
\r
177 skip = ext4_dir_entry_ll_get_entry_length(it->current);
\r
178 r = ext4_dir_iterator_seek(it, it->current_offset + skip);
\r
182 /*Skip NULL referenced entry*/
\r
183 if (it->current->inode != 0)
\r
190 int ext4_dir_iterator_fini(struct ext4_directory_iterator *it)
\r
194 if (it->current_block.lb_id)
\r
195 return ext4_block_set(it->inode_ref->fs->bdev, &it->current_block);
\r
200 void ext4_dir_write_entry(struct ext4_sblock *sb,
\r
201 struct ext4_directory_entry_ll *entry,
\r
202 uint16_t entry_len, struct ext4_inode_ref *child,
\r
203 const char *name, size_t name_len)
\r
205 /* Check maximum entry length */
\r
206 ext4_assert(entry_len <= ext4_sb_get_block_size(sb));
\r
208 /* Set basic attributes */
\r
209 ext4_dir_entry_ll_set_inode(entry, child->index);
\r
210 ext4_dir_entry_ll_set_entry_length(entry, entry_len);
\r
211 ext4_dir_entry_ll_set_name_length(sb, entry, name_len);
\r
214 memcpy(entry->name, name, name_len);
\r
216 /* Set type of entry */
\r
217 if (ext4_inode_is_type(sb, child->inode, EXT4_INODE_MODE_DIRECTORY))
\r
218 ext4_dir_entry_ll_set_inode_type(sb, entry,
\r
219 EXT4_DIRECTORY_FILETYPE_DIR);
\r
221 ext4_dir_entry_ll_set_inode_type(sb, entry,
\r
222 EXT4_DIRECTORY_FILETYPE_REG_FILE);
\r
225 int ext4_dir_add_entry(struct ext4_inode_ref *parent, const char *name,
\r
226 uint32_t name_len, struct ext4_inode_ref *child)
\r
228 struct ext4_fs *fs = parent->fs;
\r
230 #if CONFIG_DIR_INDEX_ENABLE
\r
231 /* Index adding (if allowed) */
\r
232 if ((ext4_sb_has_feature_compatible(&fs->sb,
\r
233 EXT4_FEATURE_COMPAT_DIR_INDEX)) &&
\r
234 (ext4_inode_has_flag(parent->inode, EXT4_INODE_FLAG_INDEX))) {
\r
235 int rc = ext4_dir_dx_add_entry(parent, child, name);
\r
237 /* Check if index is not corrupted */
\r
238 if (rc != EXT4_ERR_BAD_DX_DIR) {
\r
245 /* Needed to clear dir index flag if corrupted */
\r
246 ext4_inode_clear_flag(parent->inode, EXT4_INODE_FLAG_INDEX);
\r
247 parent->dirty = true;
\r
251 /* Linear algorithm */
\r
252 uint32_t iblock = 0;
\r
253 uint32_t fblock = 0;
\r
254 uint32_t block_size = ext4_sb_get_block_size(&fs->sb);
\r
255 uint32_t inode_size = ext4_inode_get_size(&fs->sb, parent->inode);
\r
256 uint32_t total_blocks = inode_size / block_size;
\r
258 /* Find block, where is space for new entry and try to add */
\r
259 bool success = false;
\r
260 for (iblock = 0; iblock < total_blocks; ++iblock) {
\r
261 int rc = ext4_fs_get_inode_data_block_index(parent, iblock, &fblock);
\r
265 struct ext4_block block;
\r
266 rc = ext4_block_get(fs->bdev, &block, fblock);
\r
270 /* If adding is successful, function can finish */
\r
271 rc = ext4_dir_try_insert_entry(&fs->sb, &block, child, name, name_len);
\r
275 rc = ext4_block_set(fs->bdev, &block);
\r
283 /* No free block found - needed to allocate next data block */
\r
287 int rc = ext4_fs_append_inode_block(parent, &fblock, &iblock);
\r
291 /* Load new block */
\r
292 struct ext4_block new_block;
\r
294 rc = ext4_block_get(fs->bdev, &new_block, fblock);
\r
298 /* Fill block with zeroes */
\r
299 memset(new_block.data, 0, block_size);
\r
300 struct ext4_directory_entry_ll *block_entry = (void *)new_block.data;
\r
301 ext4_dir_write_entry(&fs->sb, block_entry, block_size, child, name,
\r
304 /* Save new block */
\r
305 new_block.dirty = true;
\r
306 rc = ext4_block_set(fs->bdev, &new_block);
\r
311 int ext4_dir_find_entry(struct ext4_directory_search_result *result,
\r
312 struct ext4_inode_ref *parent, const char *name,
\r
315 struct ext4_sblock *sb = &parent->fs->sb;
\r
317 #if CONFIG_DIR_INDEX_ENABLE
\r
319 if ((ext4_sb_has_feature_compatible(sb, EXT4_FEATURE_COMPAT_DIR_INDEX)) &&
\r
320 (ext4_inode_has_flag(parent->inode, EXT4_INODE_FLAG_INDEX))) {
\r
321 int rc = ext4_dir_dx_find_entry(result, parent, name_len, name);
\r
323 /* Check if index is not corrupted */
\r
324 if (rc != EXT4_ERR_BAD_DX_DIR) {
\r
331 /* Needed to clear dir index flag if corrupted */
\r
332 ext4_inode_clear_flag(parent->inode, EXT4_INODE_FLAG_INDEX);
\r
333 parent->dirty = true;
\r
337 /* Linear algorithm */
\r
341 uint32_t block_size = ext4_sb_get_block_size(sb);
\r
342 uint32_t inode_size = ext4_inode_get_size(sb, parent->inode);
\r
343 uint32_t total_blocks = inode_size / block_size;
\r
345 /* Walk through all data blocks */
\r
346 for (iblock = 0; iblock < total_blocks; ++iblock) {
\r
347 /* Load block address */
\r
348 int rc = ext4_fs_get_inode_data_block_index(parent, iblock, &fblock);
\r
352 /* Load data block */
\r
353 struct ext4_block block;
\r
354 rc = ext4_block_get(parent->fs->bdev, &block, fblock);
\r
358 /* Try to find entry in block */
\r
359 struct ext4_directory_entry_ll *res_entry;
\r
360 rc = ext4_dir_find_in_block(&block, sb, name_len, name, &res_entry);
\r
362 result->block = block;
\r
363 result->dentry = res_entry;
\r
367 /* Entry not found - put block and continue to the next block */
\r
369 rc = ext4_block_set(parent->fs->bdev, &block);
\r
374 /* Entry was not found */
\r
376 result->block.lb_id = 0;
\r
377 result->dentry = NULL;
\r
382 int ext4_dir_remove_entry(struct ext4_inode_ref *parent, const char *name,
\r
385 /* Check if removing from directory */
\r
386 if (!ext4_inode_is_type(&parent->fs->sb, parent->inode,
\r
387 EXT4_INODE_MODE_DIRECTORY))
\r
390 /* Try to find entry */
\r
391 struct ext4_directory_search_result result;
\r
392 int rc = ext4_dir_find_entry(&result, parent, name, name_len);
\r
396 /* Invalidate entry */
\r
397 ext4_dir_entry_ll_set_inode(result.dentry, 0);
\r
399 /* Store entry position in block */
\r
400 uint32_t pos = (uint8_t *)result.dentry - result.block.data;
\r
403 * If entry is not the first in block, it must be merged
\r
404 * with previous entry
\r
407 uint32_t offset = 0;
\r
409 /* Start from the first entry in block */
\r
410 struct ext4_directory_entry_ll *tmp_dentry = (void *)result.block.data;
\r
411 uint16_t tmp_dentry_length =
\r
412 ext4_dir_entry_ll_get_entry_length(tmp_dentry);
\r
414 /* Find direct predecessor of removed entry */
\r
415 while ((offset + tmp_dentry_length) < pos) {
\r
416 offset += ext4_dir_entry_ll_get_entry_length(tmp_dentry);
\r
417 tmp_dentry = (void *)(result.block.data + offset);
\r
418 tmp_dentry_length = ext4_dir_entry_ll_get_entry_length(tmp_dentry);
\r
421 ext4_assert(tmp_dentry_length + offset == pos);
\r
423 /* Add to removed entry length to predecessor's length */
\r
424 uint16_t del_entry_length =
\r
425 ext4_dir_entry_ll_get_entry_length(result.dentry);
\r
426 ext4_dir_entry_ll_set_entry_length(tmp_dentry, tmp_dentry_length +
\r
430 result.block.dirty = true;
\r
432 return ext4_dir_destroy_result(parent, &result);
\r
435 int ext4_dir_try_insert_entry(struct ext4_sblock *sb,
\r
436 struct ext4_block *target_block,
\r
437 struct ext4_inode_ref *child, const char *name,
\r
440 /* Compute required length entry and align it to 4 bytes */
\r
441 uint32_t block_size = ext4_sb_get_block_size(sb);
\r
442 uint16_t required_len = sizeof(struct ext4_fake_directory_entry) + name_len;
\r
444 if ((required_len % 4) != 0)
\r
445 required_len += 4 - (required_len % 4);
\r
447 /* Initialize pointers, stop means to upper bound */
\r
448 struct ext4_directory_entry_ll *dentry = (void *)target_block->data;
\r
449 struct ext4_directory_entry_ll *stop =
\r
450 (void *)(target_block->data + block_size);
\r
453 * Walk through the block and check for invalid entries
\r
454 * or entries with free space for new entry
\r
456 while (dentry < stop) {
\r
457 uint32_t inode = ext4_dir_entry_ll_get_inode(dentry);
\r
458 uint16_t rec_len = ext4_dir_entry_ll_get_entry_length(dentry);
\r
460 /* If invalid and large enough entry, use it */
\r
461 if ((inode == 0) && (rec_len >= required_len)) {
\r
462 ext4_dir_write_entry(sb, dentry, rec_len, child, name, name_len);
\r
463 target_block->dirty = true;
\r
468 /* Valid entry, try to split it */
\r
470 uint16_t used_name_len =
\r
471 ext4_dir_entry_ll_get_name_length(sb, dentry);
\r
473 uint16_t used_space =
\r
474 sizeof(struct ext4_fake_directory_entry) + used_name_len;
\r
476 if ((used_name_len % 4) != 0)
\r
477 used_space += 4 - (used_name_len % 4);
\r
479 uint16_t free_space = rec_len - used_space;
\r
481 /* There is free space for new entry */
\r
482 if (free_space >= required_len) {
\r
483 /* Cut tail of current entry */
\r
484 ext4_dir_entry_ll_set_entry_length(dentry, used_space);
\r
485 struct ext4_directory_entry_ll *new_entry =
\r
486 (void *)((uint8_t *)dentry + used_space);
\r
487 ext4_dir_write_entry(sb, new_entry, free_space, child, name,
\r
490 target_block->dirty = true;
\r
495 /* Jump to the next entry */
\r
496 dentry = (void *)((uint8_t *)dentry + rec_len);
\r
499 /* No free space found for new entry */
\r
503 int ext4_dir_find_in_block(struct ext4_block *block, struct ext4_sblock *sb,
\r
504 size_t name_len, const char *name,
\r
505 struct ext4_directory_entry_ll **res_entry)
\r
507 /* Start from the first entry in block */
\r
508 struct ext4_directory_entry_ll *dentry =
\r
509 (struct ext4_directory_entry_ll *)block->data;
\r
511 /* Set upper bound for cycling */
\r
512 uint8_t *addr_limit = block->data + ext4_sb_get_block_size(sb);
\r
514 /* Walk through the block and check entries */
\r
515 while ((uint8_t *)dentry < addr_limit) {
\r
516 /* Termination condition */
\r
517 if ((uint8_t *)dentry + name_len > addr_limit)
\r
520 /* Valid entry - check it */
\r
521 if (dentry->inode != 0) {
\r
522 /* For more effectivity compare firstly only lengths */
\r
523 if (ext4_dir_entry_ll_get_name_length(sb, dentry) == name_len) {
\r
524 /* Compare names */
\r
525 if (memcmp((uint8_t *)name, dentry->name, name_len) == 0) {
\r
526 *res_entry = dentry;
\r
532 uint16_t dentry_len = ext4_dir_entry_ll_get_entry_length(dentry);
\r
534 /* Corrupted entry */
\r
535 if (dentry_len == 0)
\r
538 /* Jump to next entry */
\r
540 (struct ext4_directory_entry_ll *)((uint8_t *)dentry + dentry_len);
\r
543 /* Entry not found */
\r
547 int ext4_dir_destroy_result(struct ext4_inode_ref *parent,
\r
548 struct ext4_directory_search_result *result)
\r
550 if (result->block.lb_id)
\r
551 return ext4_block_set(parent->fs->bdev, &result->block);
\r