2 * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com)
6 * Copyright (c) 2012 Martin Sucha
7 * Copyright (c) 2012 Frantisek Princ
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
14 * - Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * - Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * - The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 /** @addtogroup lwext4
39 * @brief Directory handle procedures.
42 #include "ext4_config.h"
44 #include "ext4_dir_idx.h"
45 #include "ext4_inode.h"
50 /****************************************************************************/
52 /**@brief Do some checks before returning iterator.
53 * @param it Iterator to be checked
54 * @param block_size Size of data block
57 static int ext4_dir_iterator_set(struct ext4_directory_iterator *it,
62 uint32_t offset_in_block = it->current_offset % block_size;
64 /* Ensure proper alignment */
65 if ((offset_in_block % 4) != 0)
68 /* Ensure that the core of the entry does not overflow the block */
69 if (offset_in_block > block_size - 8)
72 struct ext4_directory_entry_ll *entry =
73 (void *)(it->current_block.data + offset_in_block);
75 /* Ensure that the whole entry does not overflow the block */
76 uint16_t length = ext4_dir_entry_ll_get_entry_length(entry);
77 if (offset_in_block + length > block_size)
80 /* Ensure the name length is not too large */
81 if (ext4_dir_entry_ll_get_name_length(&it->inode_ref->fs->sb, entry) >
85 /* Everything OK - "publish" the entry */
90 /**@brief Seek to next valid directory entry.
91 * Here can be jumped to the next data block.
92 * @param it Initialized iterator
93 * @param pos Position of the next entry
96 static int ext4_dir_iterator_seek(struct ext4_directory_iterator *it,
100 ext4_inode_get_size(&it->inode_ref->fs->sb, it->inode_ref->inode);
102 /* The iterator is not valid until we seek to the desired position */
105 /* Are we at the end? */
107 if (it->current_block.lb_id) {
109 int rc = ext4_block_set(it->inode_ref->fs->bdev,
111 it->current_block.lb_id = 0;
117 it->current_offset = pos;
121 /* Compute next block address */
122 uint32_t block_size = ext4_sb_get_block_size(&it->inode_ref->fs->sb);
123 uint64_t current_block_idx = it->current_offset / block_size;
124 uint64_t next_block_idx = pos / block_size;
127 * If we don't have a block or are moving across block boundary,
128 * we need to get another block
130 if ((it->current_block.lb_id == 0) ||
131 (current_block_idx != next_block_idx)) {
132 if (it->current_block.lb_id) {
133 int rc = ext4_block_set(it->inode_ref->fs->bdev,
135 it->current_block.lb_id = 0;
141 uint32_t next_block_phys_idx;
142 int rc = ext4_fs_get_inode_data_block_index(
143 it->inode_ref, next_block_idx, &next_block_phys_idx);
147 rc = ext4_block_get(it->inode_ref->fs->bdev, &it->current_block,
148 next_block_phys_idx);
150 it->current_block.lb_id = 0;
155 it->current_offset = pos;
157 return ext4_dir_iterator_set(it, block_size);
160 int ext4_dir_iterator_init(struct ext4_directory_iterator *it,
161 struct ext4_inode_ref *inode_ref, uint64_t pos)
163 it->inode_ref = inode_ref;
165 it->current_offset = 0;
166 it->current_block.lb_id = 0;
168 return ext4_dir_iterator_seek(it, pos);
171 int ext4_dir_iterator_next(struct ext4_directory_iterator *it)
177 skip = ext4_dir_entry_ll_get_entry_length(it->current);
178 r = ext4_dir_iterator_seek(it, it->current_offset + skip);
182 /*Skip NULL referenced entry*/
183 if (ext4_dir_entry_ll_get_inode(it->current) != 0)
190 int ext4_dir_iterator_fini(struct ext4_directory_iterator *it)
194 if (it->current_block.lb_id)
195 return ext4_block_set(it->inode_ref->fs->bdev,
201 void ext4_dir_write_entry(struct ext4_sblock *sb,
202 struct ext4_directory_entry_ll *entry,
203 uint16_t entry_len, struct ext4_inode_ref *child,
204 const char *name, size_t name_len)
206 /* Check maximum entry length */
207 ext4_assert(entry_len <= ext4_sb_get_block_size(sb));
209 /* Set type of entry */
210 switch (ext4_inode_type(sb, child->inode)) {
211 case EXT4_INODE_MODE_DIRECTORY:
212 ext4_dir_entry_ll_set_inode_type(sb, entry,
213 EXT4_DIRECTORY_FILETYPE_DIR);
215 case EXT4_INODE_MODE_FILE:
216 ext4_dir_entry_ll_set_inode_type(
217 sb, entry, EXT4_DIRECTORY_FILETYPE_REG_FILE);
219 case EXT4_INODE_MODE_SOFTLINK:
220 ext4_dir_entry_ll_set_inode_type(
221 sb, entry, EXT4_DIRECTORY_FILETYPE_SYMLINK);
224 /* FIXME: right now we only support 3 inode type. */
228 /* Set basic attributes */
229 ext4_dir_entry_ll_set_inode(entry, child->index);
230 ext4_dir_entry_ll_set_entry_length(entry, entry_len);
231 ext4_dir_entry_ll_set_name_length(sb, entry, name_len);
234 memcpy(entry->name, name, name_len);
237 int ext4_dir_add_entry(struct ext4_inode_ref *parent, const char *name,
238 uint32_t name_len, struct ext4_inode_ref *child)
240 struct ext4_fs *fs = parent->fs;
242 #if CONFIG_DIR_INDEX_ENABLE
243 /* Index adding (if allowed) */
244 if ((ext4_sb_has_feature_compatible(&fs->sb,
245 EXT4_FEATURE_COMPAT_DIR_INDEX)) &&
246 (ext4_inode_has_flag(parent->inode, EXT4_INODE_FLAG_INDEX))) {
247 int rc = ext4_dir_dx_add_entry(parent, child, name);
249 /* Check if index is not corrupted */
250 if (rc != EXT4_ERR_BAD_DX_DIR) {
257 /* Needed to clear dir index flag if corrupted */
258 ext4_inode_clear_flag(parent->inode, EXT4_INODE_FLAG_INDEX);
259 parent->dirty = true;
263 /* Linear algorithm */
266 uint32_t block_size = ext4_sb_get_block_size(&fs->sb);
267 uint32_t inode_size = ext4_inode_get_size(&fs->sb, parent->inode);
268 uint32_t total_blocks = inode_size / block_size;
270 /* Find block, where is space for new entry and try to add */
271 bool success = false;
272 for (iblock = 0; iblock < total_blocks; ++iblock) {
274 ext4_fs_get_inode_data_block_index(parent, iblock, &fblock);
278 struct ext4_block block;
279 rc = ext4_block_get(fs->bdev, &block, fblock);
283 /* If adding is successful, function can finish */
284 rc = ext4_dir_try_insert_entry(&fs->sb, &block, child, name,
289 rc = ext4_block_set(fs->bdev, &block);
297 /* No free block found - needed to allocate next data block */
301 int rc = ext4_fs_append_inode_block(parent, &fblock, &iblock);
306 struct ext4_block new_block;
308 rc = ext4_block_get(fs->bdev, &new_block, fblock);
312 /* Fill block with zeroes */
313 memset(new_block.data, 0, block_size);
314 struct ext4_directory_entry_ll *block_entry = (void *)new_block.data;
315 ext4_dir_write_entry(&fs->sb, block_entry, block_size, child, name,
319 new_block.dirty = true;
320 rc = ext4_block_set(fs->bdev, &new_block);
325 int ext4_dir_find_entry(struct ext4_directory_search_result *result,
326 struct ext4_inode_ref *parent, const char *name,
329 struct ext4_sblock *sb = &parent->fs->sb;
331 #if CONFIG_DIR_INDEX_ENABLE
333 if ((ext4_sb_has_feature_compatible(sb,
334 EXT4_FEATURE_COMPAT_DIR_INDEX)) &&
335 (ext4_inode_has_flag(parent->inode, EXT4_INODE_FLAG_INDEX))) {
336 int rc = ext4_dir_dx_find_entry(result, parent, name_len, name);
338 /* Check if index is not corrupted */
339 if (rc != EXT4_ERR_BAD_DX_DIR) {
346 /* Needed to clear dir index flag if corrupted */
347 ext4_inode_clear_flag(parent->inode, EXT4_INODE_FLAG_INDEX);
348 parent->dirty = true;
352 /* Linear algorithm */
356 uint32_t block_size = ext4_sb_get_block_size(sb);
357 uint32_t inode_size = ext4_inode_get_size(sb, parent->inode);
358 uint32_t total_blocks = inode_size / block_size;
360 /* Walk through all data blocks */
361 for (iblock = 0; iblock < total_blocks; ++iblock) {
362 /* Load block address */
364 ext4_fs_get_inode_data_block_index(parent, iblock, &fblock);
368 /* Load data block */
369 struct ext4_block block;
370 rc = ext4_block_get(parent->fs->bdev, &block, fblock);
374 /* Try to find entry in block */
375 struct ext4_directory_entry_ll *res_entry;
376 rc = ext4_dir_find_in_block(&block, sb, name_len, name,
379 result->block = block;
380 result->dentry = res_entry;
384 /* Entry not found - put block and continue to the next block */
386 rc = ext4_block_set(parent->fs->bdev, &block);
391 /* Entry was not found */
393 result->block.lb_id = 0;
394 result->dentry = NULL;
399 int ext4_dir_remove_entry(struct ext4_inode_ref *parent, const char *name,
402 /* Check if removing from directory */
403 if (!ext4_inode_is_type(&parent->fs->sb, parent->inode,
404 EXT4_INODE_MODE_DIRECTORY))
407 /* Try to find entry */
408 struct ext4_directory_search_result result;
409 int rc = ext4_dir_find_entry(&result, parent, name, name_len);
413 /* Invalidate entry */
414 ext4_dir_entry_ll_set_inode(result.dentry, 0);
416 /* Store entry position in block */
417 uint32_t pos = (uint8_t *)result.dentry - result.block.data;
420 * If entry is not the first in block, it must be merged
421 * with previous entry
426 /* Start from the first entry in block */
427 struct ext4_directory_entry_ll *tmp_dentry =
428 (void *)result.block.data;
429 uint16_t tmp_dentry_length =
430 ext4_dir_entry_ll_get_entry_length(tmp_dentry);
432 /* Find direct predecessor of removed entry */
433 while ((offset + tmp_dentry_length) < pos) {
435 ext4_dir_entry_ll_get_entry_length(tmp_dentry);
436 tmp_dentry = (void *)(result.block.data + offset);
438 ext4_dir_entry_ll_get_entry_length(tmp_dentry);
441 ext4_assert(tmp_dentry_length + offset == pos);
443 /* Add to removed entry length to predecessor's length */
444 uint16_t del_entry_length =
445 ext4_dir_entry_ll_get_entry_length(result.dentry);
446 ext4_dir_entry_ll_set_entry_length(
447 tmp_dentry, tmp_dentry_length + del_entry_length);
450 result.block.dirty = true;
452 return ext4_dir_destroy_result(parent, &result);
455 int ext4_dir_try_insert_entry(struct ext4_sblock *sb,
456 struct ext4_block *target_block,
457 struct ext4_inode_ref *child, const char *name,
460 /* Compute required length entry and align it to 4 bytes */
461 uint32_t block_size = ext4_sb_get_block_size(sb);
462 uint16_t required_len =
463 sizeof(struct ext4_fake_directory_entry) + name_len;
465 if ((required_len % 4) != 0)
466 required_len += 4 - (required_len % 4);
468 /* Initialize pointers, stop means to upper bound */
469 struct ext4_directory_entry_ll *dentry = (void *)target_block->data;
470 struct ext4_directory_entry_ll *stop =
471 (void *)(target_block->data + block_size);
474 * Walk through the block and check for invalid entries
475 * or entries with free space for new entry
477 while (dentry < stop) {
478 uint32_t inode = ext4_dir_entry_ll_get_inode(dentry);
479 uint16_t rec_len = ext4_dir_entry_ll_get_entry_length(dentry);
481 /* If invalid and large enough entry, use it */
482 if ((inode == 0) && (rec_len >= required_len)) {
483 ext4_dir_write_entry(sb, dentry, rec_len, child, name,
485 target_block->dirty = true;
490 /* Valid entry, try to split it */
492 uint16_t used_name_len =
493 ext4_dir_entry_ll_get_name_length(sb, dentry);
495 uint16_t used_space =
496 sizeof(struct ext4_fake_directory_entry) +
499 if ((used_name_len % 4) != 0)
500 used_space += 4 - (used_name_len % 4);
502 uint16_t free_space = rec_len - used_space;
504 /* There is free space for new entry */
505 if (free_space >= required_len) {
506 /* Cut tail of current entry */
507 ext4_dir_entry_ll_set_entry_length(dentry,
509 struct ext4_directory_entry_ll *new_entry =
510 (void *)((uint8_t *)dentry + used_space);
511 ext4_dir_write_entry(sb, new_entry, free_space,
512 child, name, name_len);
514 target_block->dirty = true;
519 /* Jump to the next entry */
520 dentry = (void *)((uint8_t *)dentry + rec_len);
523 /* No free space found for new entry */
527 int ext4_dir_find_in_block(struct ext4_block *block, struct ext4_sblock *sb,
528 size_t name_len, const char *name,
529 struct ext4_directory_entry_ll **res_entry)
531 /* Start from the first entry in block */
532 struct ext4_directory_entry_ll *dentry =
533 (struct ext4_directory_entry_ll *)block->data;
535 /* Set upper bound for cycling */
536 uint8_t *addr_limit = block->data + ext4_sb_get_block_size(sb);
538 /* Walk through the block and check entries */
539 while ((uint8_t *)dentry < addr_limit) {
540 /* Termination condition */
541 if ((uint8_t *)dentry + name_len > addr_limit)
544 /* Valid entry - check it */
545 if (ext4_dir_entry_ll_get_inode(dentry) != 0) {
546 /* For more efficient compare only lengths firstly*/
547 if (ext4_dir_entry_ll_get_name_length(sb, dentry) ==
550 if (memcmp((uint8_t *)name, dentry->name,
558 uint16_t dentry_len =
559 ext4_dir_entry_ll_get_entry_length(dentry);
561 /* Corrupted entry */
565 /* Jump to next entry */
566 dentry = (struct ext4_directory_entry_ll *)((uint8_t *)dentry +
570 /* Entry not found */
574 int ext4_dir_destroy_result(struct ext4_inode_ref *parent,
575 struct ext4_directory_search_result *result)
577 if (result->block.lb_id)
578 return ext4_block_set(parent->fs->bdev, &result->block);