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_crc32c.h"
46 #include "ext4_inode.h"
51 /****************************************************************************/
53 /* Walk through a dirent block to find a checksum "dirent" at the tail */
54 static struct ext4_dir_entry_tail *
55 ext4_dir_get_tail(struct ext4_inode_ref *inode_ref,
56 struct ext4_dir_en *de)
58 struct ext4_dir_entry_tail *t;
59 struct ext4_sblock *sb = &inode_ref->fs->sb;
61 t = EXT4_DIRENT_TAIL(de, ext4_sb_get_block_size(sb));
63 if (t->reserved_zero1 || t->reserved_zero2)
65 if (to_le16(t->rec_len) != sizeof(struct ext4_dir_entry_tail))
67 if (t->reserved_ft != EXT4_DIRENTRY_DIR_CSUM)
73 #if CONFIG_META_CSUM_ENABLE
74 static uint32_t ext4_dir_csum(struct ext4_inode_ref *inode_ref,
75 struct ext4_dir_en *dirent, int size)
78 struct ext4_sblock *sb = &inode_ref->fs->sb;
79 uint32_t ino_index = to_le32(inode_ref->index);
80 uint32_t ino_gen = to_le32(ext4_inode_get_generation(inode_ref->inode));
82 /* First calculate crc32 checksum against fs uuid */
83 csum = ext4_crc32c(EXT4_CRC32_INIT, sb->uuid, sizeof(sb->uuid));
84 /* Then calculate crc32 checksum against inode number
85 * and inode generation */
86 csum = ext4_crc32c(csum, &ino_index, sizeof(ino_index));
87 csum = ext4_crc32c(csum, &ino_gen, sizeof(ino_gen));
88 /* Finally calculate crc32 checksum against directory entries */
89 csum = ext4_crc32c(csum, dirent, size);
93 #define ext4_dir_csum(...) 0
96 bool ext4_dir_csum_verify(struct ext4_inode_ref *inode_ref,
97 struct ext4_dir_en *dirent)
99 #ifdef CONFIG_META_CSUM_ENABLE
100 struct ext4_dir_entry_tail *t;
101 struct ext4_sblock *sb = &inode_ref->fs->sb;
103 /* Compute the checksum only if the filesystem supports it */
104 if (ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM)) {
105 t = ext4_dir_get_tail(inode_ref, dirent);
107 /* There is no space to hold the checksum */
111 ptrdiff_t __unused diff = (char *)t - (char *)dirent;
112 uint32_t csum = ext4_dir_csum(inode_ref, dirent, diff);
113 if (t->checksum != to_le32(csum))
121 void ext4_dir_init_entry_tail(struct ext4_dir_entry_tail *t)
123 memset(t, 0, sizeof(struct ext4_dir_entry_tail));
124 t->rec_len = to_le16(sizeof(struct ext4_dir_entry_tail));
125 t->reserved_ft = EXT4_DIRENTRY_DIR_CSUM;
128 void ext4_dir_set_csum(struct ext4_inode_ref *inode_ref,
129 struct ext4_dir_en *dirent)
131 struct ext4_dir_entry_tail *t;
132 struct ext4_sblock *sb = &inode_ref->fs->sb;
134 /* Compute the checksum only if the filesystem supports it */
135 if (ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM)) {
136 t = ext4_dir_get_tail(inode_ref, dirent);
138 /* There is no space to hold the checksum */
142 ptrdiff_t __unused diff = (char *)t - (char *)dirent;
143 uint32_t csum = ext4_dir_csum(inode_ref, dirent, diff);
144 t->checksum = to_le32(csum);
148 /**@brief Do some checks before returning iterator.
149 * @param it Iterator to be checked
150 * @param block_size Size of data block
153 static int ext4_dir_iterator_set(struct ext4_dir_iter *it,
156 uint32_t off_in_block = it->curr_off % block_size;
157 struct ext4_sblock *sb = &it->inode_ref->fs->sb;
161 /* Ensure proper alignment */
162 if ((off_in_block % 4) != 0)
165 /* Ensure that the core of the entry does not overflow the block */
166 if (off_in_block > block_size - 8)
169 struct ext4_dir_en *en;
170 en = (void *)(it->curr_blk.data + off_in_block);
172 /* Ensure that the whole entry does not overflow the block */
173 uint16_t length = ext4_dir_en_get_entry_len(en);
174 if (off_in_block + length > block_size)
177 /* Ensure the name length is not too large */
178 if (ext4_dir_en_get_name_len(sb, en) > length - 8)
181 /* Everything OK - "publish" the entry */
186 /**@brief Seek to next valid directory entry.
187 * Here can be jumped to the next data block.
188 * @param it Initialized iterator
189 * @param pos Position of the next entry
192 static int ext4_dir_iterator_seek(struct ext4_dir_iter *it, uint64_t pos)
194 struct ext4_sblock *sb = &it->inode_ref->fs->sb;
195 struct ext4_inode *inode = it->inode_ref->inode;
196 struct ext4_blockdev *bdev = it->inode_ref->fs->bdev;
197 uint64_t size = ext4_inode_get_size(sb, inode);
200 /* The iterator is not valid until we seek to the desired position */
203 /* Are we at the end? */
205 if (it->curr_blk.lb_id) {
207 r = ext4_block_set(bdev, &it->curr_blk);
208 it->curr_blk.lb_id = 0;
217 /* Compute next block address */
218 uint32_t block_size = ext4_sb_get_block_size(sb);
219 uint64_t current_blk_idx = it->curr_off / block_size;
220 uint32_t next_blk_idx = pos / block_size;
223 * If we don't have a block or are moving across block boundary,
224 * we need to get another block
226 if ((it->curr_blk.lb_id == 0) ||
227 (current_blk_idx != next_blk_idx)) {
228 if (it->curr_blk.lb_id) {
229 r = ext4_block_set(bdev, &it->curr_blk);
230 it->curr_blk.lb_id = 0;
236 ext4_fsblk_t next_blk;
237 r = ext4_fs_get_inode_data_block_index(it->inode_ref,
238 next_blk_idx, &next_blk, false);
242 r = ext4_block_get(bdev, &it->curr_blk, next_blk);
244 it->curr_blk.lb_id = 0;
250 return ext4_dir_iterator_set(it, block_size);
253 int ext4_dir_iterator_init(struct ext4_dir_iter *it,
254 struct ext4_inode_ref *inode_ref, uint64_t pos)
256 it->inode_ref = inode_ref;
259 it->curr_blk.lb_id = 0;
261 return ext4_dir_iterator_seek(it, pos);
264 int ext4_dir_iterator_next(struct ext4_dir_iter *it)
270 skip = ext4_dir_en_get_entry_len(it->curr);
271 r = ext4_dir_iterator_seek(it, it->curr_off + skip);
275 /*Skip NULL referenced entry*/
276 if (ext4_dir_en_get_inode(it->curr) != 0)
283 int ext4_dir_iterator_fini(struct ext4_dir_iter *it)
287 if (it->curr_blk.lb_id)
288 return ext4_block_set(it->inode_ref->fs->bdev, &it->curr_blk);
293 void ext4_dir_write_entry(struct ext4_sblock *sb, struct ext4_dir_en *en,
294 uint16_t entry_len, struct ext4_inode_ref *child,
295 const char *name, size_t name_len)
297 /* Check maximum entry length */
298 ext4_assert(entry_len <= ext4_sb_get_block_size(sb));
300 /* Set type of entry */
301 switch (ext4_inode_type(sb, child->inode)) {
302 case EXT4_INODE_MODE_DIRECTORY:
303 ext4_dir_en_set_inode_type(sb, en, EXT4_DE_DIR);
305 case EXT4_INODE_MODE_FILE:
306 ext4_dir_en_set_inode_type(sb, en, EXT4_DE_REG_FILE);
308 case EXT4_INODE_MODE_SOFTLINK:
309 ext4_dir_en_set_inode_type(sb, en, EXT4_DE_SYMLINK);
312 /* FIXME: right now we only support 3 inode type. */
316 /* Set basic attributes */
317 ext4_dir_en_set_inode(en, child->index);
318 ext4_dir_en_set_entry_len(en, entry_len);
319 ext4_dir_en_set_name_len(sb, en, name_len);
322 memcpy(en->name, name, name_len);
325 int ext4_dir_add_entry(struct ext4_inode_ref *parent, const char *name,
326 uint32_t name_len, struct ext4_inode_ref *child)
328 struct ext4_fs *fs = parent->fs;
329 struct ext4_sblock *sb = &parent->fs->sb;
331 #if CONFIG_DIR_INDEX_ENABLE
332 /* Index adding (if allowed) */
333 if ((ext4_sb_feature_com(sb, EXT4_FCOM_DIR_INDEX)) &&
334 (ext4_inode_has_flag(parent->inode, EXT4_INODE_FLAG_INDEX))) {
335 int rc = ext4_dir_dx_add_entry(parent, child, name);
337 /* Check if index is not corrupted */
338 if (rc != EXT4_ERR_BAD_DX_DIR) {
345 /* Needed to clear dir index flag if corrupted */
346 ext4_inode_clear_flag(parent->inode, EXT4_INODE_FLAG_INDEX);
347 parent->dirty = true;
351 /* Linear algorithm */
353 ext4_fsblk_t fblock = 0;
354 uint32_t block_size = ext4_sb_get_block_size(sb);
355 uint32_t inode_size = ext4_inode_get_size(sb, parent->inode);
356 uint32_t total_blocks = inode_size / block_size;
358 /* Find block, where is space for new entry and try to add */
359 bool success = false;
360 for (iblock = 0; iblock < total_blocks; ++iblock) {
361 int rc = ext4_fs_get_inode_data_block_index(parent, iblock,
366 struct ext4_block block;
367 rc = ext4_block_get(fs->bdev, &block, fblock);
371 if (!ext4_dir_csum_verify(parent, (void *)block.data)) {
373 DBG_WARN "Leaf block checksum failed."
374 "Inode: %" PRIu32", "
375 "Block: %" PRIu32"\n",
380 /* If adding is successful, function can finish */
381 rc = ext4_dir_try_insert_entry(sb, parent, &block, child,
386 rc = ext4_block_set(fs->bdev, &block);
394 /* No free block found - needed to allocate next data block */
398 int rc = ext4_fs_append_inode_block(parent, &fblock, &iblock);
405 rc = ext4_block_get_noread(fs->bdev, &b, fblock);
409 /* Fill block with zeroes */
410 memset(b.data, 0, block_size);
411 struct ext4_dir_en *blk_en = (void *)b.data;
414 if (ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM)) {
415 uint16_t el = block_size - sizeof(struct ext4_dir_entry_tail);
416 ext4_dir_write_entry(sb, blk_en, el, child, name, name_len);
417 ext4_dir_init_entry_tail(EXT4_DIRENT_TAIL(b.data, block_size));
419 ext4_dir_write_entry(sb, blk_en, block_size, child, name,
423 ext4_dir_set_csum(parent, (void *)b.data);
425 rc = ext4_block_set(fs->bdev, &b);
430 int ext4_dir_find_entry(struct ext4_dir_search_result *result,
431 struct ext4_inode_ref *parent, const char *name,
434 struct ext4_sblock *sb = &parent->fs->sb;
437 result->block.lb_id = 0;
438 result->dentry = NULL;
440 #if CONFIG_DIR_INDEX_ENABLE
442 if ((ext4_sb_feature_com(sb, EXT4_FCOM_DIR_INDEX)) &&
443 (ext4_inode_has_flag(parent->inode, EXT4_INODE_FLAG_INDEX))) {
444 int rc = ext4_dir_dx_find_entry(result, parent, name_len, name);
446 /* Check if index is not corrupted */
447 if (rc != EXT4_ERR_BAD_DX_DIR) {
454 /* Needed to clear dir index flag if corrupted */
455 ext4_inode_clear_flag(parent->inode, EXT4_INODE_FLAG_INDEX);
456 parent->dirty = true;
460 /* Linear algorithm */
464 uint32_t block_size = ext4_sb_get_block_size(sb);
465 uint32_t inode_size = ext4_inode_get_size(sb, parent->inode);
466 uint32_t total_blocks = inode_size / block_size;
468 /* Walk through all data blocks */
469 for (iblock = 0; iblock < total_blocks; ++iblock) {
470 /* Load block address */
472 ext4_fs_get_inode_data_block_index(parent,
478 /* Load data block */
480 rc = ext4_block_get(parent->fs->bdev, &b, fblock);
484 if (!ext4_dir_csum_verify(parent, (void *)b.data)) {
486 DBG_WARN "Leaf block checksum failed."
487 "Inode: %" PRIu32", "
488 "Block: %" PRIu32"\n",
493 /* Try to find entry in block */
494 struct ext4_dir_en *res_entry;
495 rc = ext4_dir_find_in_block(&b, sb, name_len, name, &res_entry);
498 result->dentry = res_entry;
502 /* Entry not found - put block and continue to the next block */
504 rc = ext4_block_set(parent->fs->bdev, &b);
512 int ext4_dir_remove_entry(struct ext4_inode_ref *parent, const char *name,
515 struct ext4_sblock *sb = &parent->fs->sb;
516 /* Check if removing from directory */
517 if (!ext4_inode_is_type(sb, parent->inode, EXT4_INODE_MODE_DIRECTORY))
520 /* Try to find entry */
521 struct ext4_dir_search_result result;
522 int rc = ext4_dir_find_entry(&result, parent, name, name_len);
526 /* Invalidate entry */
527 ext4_dir_en_set_inode(result.dentry, 0);
529 /* Store entry position in block */
530 uint32_t pos = (uint8_t *)result.dentry - result.block.data;
533 * If entry is not the first in block, it must be merged
534 * with previous entry
539 /* Start from the first entry in block */
540 struct ext4_dir_en *tmp_de =(void *)result.block.data;
541 uint16_t de_len = ext4_dir_en_get_entry_len(tmp_de);
543 /* Find direct predecessor of removed entry */
544 while ((offset + de_len) < pos) {
545 offset += ext4_dir_en_get_entry_len(tmp_de);
546 tmp_de = (void *)(result.block.data + offset);
547 de_len = ext4_dir_en_get_entry_len(tmp_de);
550 ext4_assert(de_len + offset == pos);
552 /* Add to removed entry length to predecessor's length */
554 del_len = ext4_dir_en_get_entry_len(result.dentry);
555 ext4_dir_en_set_entry_len(tmp_de, de_len + del_len);
558 ext4_dir_set_csum(parent,
559 (struct ext4_dir_en *)result.block.data);
560 result.block.dirty = true;
562 return ext4_dir_destroy_result(parent, &result);
565 int ext4_dir_try_insert_entry(struct ext4_sblock *sb,
566 struct ext4_inode_ref *inode_ref,
567 struct ext4_block *dst_blk,
568 struct ext4_inode_ref *child, const char *name,
571 /* Compute required length entry and align it to 4 bytes */
572 uint32_t block_size = ext4_sb_get_block_size(sb);
573 uint16_t required_len = sizeof(struct ext4_fake_dir_entry) + name_len;
575 if ((required_len % 4) != 0)
576 required_len += 4 - (required_len % 4);
578 /* Initialize pointers, stop means to upper bound */
579 struct ext4_dir_en *start = (void *)dst_blk->data;
580 struct ext4_dir_en *stop = (void *)(dst_blk->data + block_size);
583 * Walk through the block and check for invalid entries
584 * or entries with free space for new entry
586 while (start < stop) {
587 uint32_t inode = ext4_dir_en_get_inode(start);
588 uint16_t rec_len = ext4_dir_en_get_entry_len(start);
589 uint8_t itype = ext4_dir_en_get_inode_type(sb, start);
591 /* If invalid and large enough entry, use it */
592 if ((inode == 0) && (itype != EXT4_DIRENTRY_DIR_CSUM) &&
593 (rec_len >= required_len)) {
594 ext4_dir_write_entry(sb, start, rec_len, child, name,
596 ext4_dir_set_csum(inode_ref, (void *)dst_blk->data);
597 dst_blk->dirty = true;
602 /* Valid entry, try to split it */
605 used_len = ext4_dir_en_get_name_len(sb, start);
608 sz = sizeof(struct ext4_fake_dir_entry) + used_len;
610 if ((used_len % 4) != 0)
611 sz += 4 - (used_len % 4);
613 uint16_t free_space = rec_len - sz;
615 /* There is free space for new entry */
616 if (free_space >= required_len) {
617 /* Cut tail of current entry */
618 struct ext4_dir_en * new_entry;
619 new_entry = (void *)((uint8_t *)start + sz);
620 ext4_dir_en_set_entry_len(start, sz);
621 ext4_dir_write_entry(sb, new_entry, free_space,
622 child, name, name_len);
624 ext4_dir_set_csum(inode_ref,
625 (void *)dst_blk->data);
626 dst_blk->dirty = true;
631 /* Jump to the next entry */
632 start = (void *)((uint8_t *)start + rec_len);
635 /* No free space found for new entry */
639 int ext4_dir_find_in_block(struct ext4_block *block, struct ext4_sblock *sb,
640 size_t name_len, const char *name,
641 struct ext4_dir_en **res_entry)
643 /* Start from the first entry in block */
644 struct ext4_dir_en *de = (struct ext4_dir_en *)block->data;
646 /* Set upper bound for cycling */
647 uint8_t *addr_limit = block->data + ext4_sb_get_block_size(sb);
649 /* Walk through the block and check entries */
650 while ((uint8_t *)de < addr_limit) {
651 /* Termination condition */
652 if ((uint8_t *)de + name_len > addr_limit)
655 /* Valid entry - check it */
656 if (ext4_dir_en_get_inode(de) != 0) {
657 /* For more efficient compare only lengths firstly*/
658 uint16_t el = ext4_dir_en_get_name_len(sb, de);
659 if (el == name_len) {
661 if (memcmp(name, de->name, name_len) == 0) {
668 uint16_t de_len = ext4_dir_en_get_entry_len(de);
670 /* Corrupted entry */
674 /* Jump to next entry */
675 de = (struct ext4_dir_en *)((uint8_t *)de + de_len);
678 /* Entry not found */
682 int ext4_dir_destroy_result(struct ext4_inode_ref *parent,
683 struct ext4_dir_search_result *result)
685 if (result->block.lb_id)
686 return ext4_block_set(parent->fs->bdev, &result->block);