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"
43 #include "ext4_types.h"
44 #include "ext4_misc.h"
45 #include "ext4_errno.h"
46 #include "ext4_debug.h"
48 #include "ext4_trans.h"
50 #include "ext4_dir_idx.h"
51 #include "ext4_crc32.h"
52 #include "ext4_inode.h"
57 /****************************************************************************/
59 /* Walk through a dirent block to find a checksum "dirent" at the tail */
60 static struct ext4_dir_entry_tail *
61 ext4_dir_get_tail(struct ext4_inode_ref *inode_ref,
62 struct ext4_dir_en *de)
64 struct ext4_dir_entry_tail *t;
65 struct ext4_sblock *sb = &inode_ref->fs->sb;
67 t = EXT4_DIRENT_TAIL(de, ext4_sb_get_block_size(sb));
69 if (t->reserved_zero1 || t->reserved_zero2)
71 if (to_le16(t->rec_len) != sizeof(struct ext4_dir_entry_tail))
73 if (t->reserved_ft != EXT4_DIRENTRY_DIR_CSUM)
79 #if CONFIG_META_CSUM_ENABLE
80 static uint32_t ext4_dir_csum(struct ext4_inode_ref *inode_ref,
81 struct ext4_dir_en *dirent, int size)
84 struct ext4_sblock *sb = &inode_ref->fs->sb;
85 uint32_t ino_index = to_le32(inode_ref->index);
86 uint32_t ino_gen = to_le32(ext4_inode_get_generation(inode_ref->inode));
88 /* First calculate crc32 checksum against fs uuid */
89 csum = ext4_crc32c(EXT4_CRC32_INIT, sb->uuid, sizeof(sb->uuid));
90 /* Then calculate crc32 checksum against inode number
91 * and inode generation */
92 csum = ext4_crc32c(csum, &ino_index, sizeof(ino_index));
93 csum = ext4_crc32c(csum, &ino_gen, sizeof(ino_gen));
94 /* Finally calculate crc32 checksum against directory entries */
95 csum = ext4_crc32c(csum, dirent, size);
99 #define ext4_dir_csum(...) 0
102 bool ext4_dir_csum_verify(struct ext4_inode_ref *inode_ref,
103 struct ext4_dir_en *dirent)
105 #ifdef CONFIG_META_CSUM_ENABLE
106 struct ext4_dir_entry_tail *t;
107 struct ext4_sblock *sb = &inode_ref->fs->sb;
109 /* Compute the checksum only if the filesystem supports it */
110 if (ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM)) {
111 t = ext4_dir_get_tail(inode_ref, dirent);
113 /* There is no space to hold the checksum */
117 ptrdiff_t __unused diff = (char *)t - (char *)dirent;
118 uint32_t csum = ext4_dir_csum(inode_ref, dirent, diff);
119 if (t->checksum != to_le32(csum))
127 void ext4_dir_init_entry_tail(struct ext4_dir_entry_tail *t)
129 memset(t, 0, sizeof(struct ext4_dir_entry_tail));
130 t->rec_len = to_le16(sizeof(struct ext4_dir_entry_tail));
131 t->reserved_ft = EXT4_DIRENTRY_DIR_CSUM;
134 void ext4_dir_set_csum(struct ext4_inode_ref *inode_ref,
135 struct ext4_dir_en *dirent)
137 struct ext4_dir_entry_tail *t;
138 struct ext4_sblock *sb = &inode_ref->fs->sb;
140 /* Compute the checksum only if the filesystem supports it */
141 if (ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM)) {
142 t = ext4_dir_get_tail(inode_ref, dirent);
144 /* There is no space to hold the checksum */
148 ptrdiff_t __unused diff = (char *)t - (char *)dirent;
149 uint32_t csum = ext4_dir_csum(inode_ref, dirent, diff);
150 t->checksum = to_le32(csum);
154 /**@brief Do some checks before returning iterator.
155 * @param it Iterator to be checked
156 * @param block_size Size of data block
159 static int ext4_dir_iterator_set(struct ext4_dir_iter *it,
162 uint32_t off_in_block = it->curr_off % block_size;
163 struct ext4_sblock *sb = &it->inode_ref->fs->sb;
167 /* Ensure proper alignment */
168 if ((off_in_block % 4) != 0)
171 /* Ensure that the core of the entry does not overflow the block */
172 if (off_in_block > block_size - 8)
175 struct ext4_dir_en *en;
176 en = (void *)(it->curr_blk.data + off_in_block);
178 /* Ensure that the whole entry does not overflow the block */
179 uint16_t length = ext4_dir_en_get_entry_len(en);
180 if (off_in_block + length > block_size)
183 /* Ensure the name length is not too large */
184 if (ext4_dir_en_get_name_len(sb, en) > length - 8)
187 /* Everything OK - "publish" the entry */
192 /**@brief Seek to next valid directory entry.
193 * Here can be jumped to the next data block.
194 * @param it Initialized iterator
195 * @param pos Position of the next entry
198 static int ext4_dir_iterator_seek(struct ext4_dir_iter *it, uint64_t pos)
200 struct ext4_sblock *sb = &it->inode_ref->fs->sb;
201 struct ext4_inode *inode = it->inode_ref->inode;
202 struct ext4_blockdev *bdev = it->inode_ref->fs->bdev;
203 uint64_t size = ext4_inode_get_size(sb, inode);
206 /* The iterator is not valid until we seek to the desired position */
209 /* Are we at the end? */
211 if (it->curr_blk.lb_id) {
213 r = ext4_block_set(bdev, &it->curr_blk);
214 it->curr_blk.lb_id = 0;
223 /* Compute next block address */
224 uint32_t block_size = ext4_sb_get_block_size(sb);
225 uint64_t current_blk_idx = it->curr_off / block_size;
226 uint32_t next_blk_idx = (uint32_t)(pos / block_size);
229 * If we don't have a block or are moving across block boundary,
230 * we need to get another block
232 if ((it->curr_blk.lb_id == 0) ||
233 (current_blk_idx != next_blk_idx)) {
234 if (it->curr_blk.lb_id) {
235 r = ext4_block_set(bdev, &it->curr_blk);
236 it->curr_blk.lb_id = 0;
242 ext4_fsblk_t next_blk;
243 r = ext4_fs_get_inode_dblk_idx(it->inode_ref, next_blk_idx,
248 r = ext4_trans_block_get(bdev, &it->curr_blk, next_blk);
250 it->curr_blk.lb_id = 0;
256 return ext4_dir_iterator_set(it, block_size);
259 int ext4_dir_iterator_init(struct ext4_dir_iter *it,
260 struct ext4_inode_ref *inode_ref, uint64_t pos)
262 it->inode_ref = inode_ref;
265 it->curr_blk.lb_id = 0;
267 return ext4_dir_iterator_seek(it, pos);
270 int ext4_dir_iterator_next(struct ext4_dir_iter *it)
276 skip = ext4_dir_en_get_entry_len(it->curr);
277 r = ext4_dir_iterator_seek(it, it->curr_off + skip);
281 /*Skip NULL referenced entry*/
282 if (ext4_dir_en_get_inode(it->curr) != 0)
289 int ext4_dir_iterator_fini(struct ext4_dir_iter *it)
293 if (it->curr_blk.lb_id)
294 return ext4_block_set(it->inode_ref->fs->bdev, &it->curr_blk);
299 void ext4_dir_write_entry(struct ext4_sblock *sb, struct ext4_dir_en *en,
300 uint16_t entry_len, struct ext4_inode_ref *child,
301 const char *name, size_t name_len)
303 /* Check maximum entry length */
304 ext4_assert(entry_len <= ext4_sb_get_block_size(sb));
306 /* Set type of entry */
307 switch (ext4_inode_type(sb, child->inode)) {
308 case EXT4_INODE_MODE_DIRECTORY:
309 ext4_dir_en_set_inode_type(sb, en, EXT4_DE_DIR);
311 case EXT4_INODE_MODE_FILE:
312 ext4_dir_en_set_inode_type(sb, en, EXT4_DE_REG_FILE);
314 case EXT4_INODE_MODE_SOFTLINK:
315 ext4_dir_en_set_inode_type(sb, en, EXT4_DE_SYMLINK);
318 /* FIXME: right now we only support 3 inode type. */
322 /* Set basic attributes */
323 ext4_dir_en_set_inode(en, child->index);
324 ext4_dir_en_set_entry_len(en, entry_len);
325 ext4_dir_en_set_name_len(sb, en, (uint16_t)name_len);
328 memcpy(en->name, name, name_len);
331 int ext4_dir_add_entry(struct ext4_inode_ref *parent, const char *name,
332 uint32_t name_len, struct ext4_inode_ref *child)
335 struct ext4_fs *fs = parent->fs;
336 struct ext4_sblock *sb = &parent->fs->sb;
338 #if CONFIG_DIR_INDEX_ENABLE
339 /* Index adding (if allowed) */
340 if ((ext4_sb_feature_com(sb, EXT4_FCOM_DIR_INDEX)) &&
341 (ext4_inode_has_flag(parent->inode, EXT4_INODE_FLAG_INDEX))) {
342 r = ext4_dir_dx_add_entry(parent, child, name);
344 /* Check if index is not corrupted */
345 if (r != EXT4_ERR_BAD_DX_DIR) {
352 /* Needed to clear dir index flag if corrupted */
353 ext4_inode_clear_flag(parent->inode, EXT4_INODE_FLAG_INDEX);
354 parent->dirty = true;
358 /* Linear algorithm */
360 ext4_fsblk_t fblock = 0;
361 uint32_t block_size = ext4_sb_get_block_size(sb);
362 uint64_t inode_size = ext4_inode_get_size(sb, parent->inode);
363 uint32_t total_blocks = (uint32_t)(inode_size / block_size);
365 /* Find block, where is space for new entry and try to add */
366 bool success = false;
367 for (iblock = 0; iblock < total_blocks; ++iblock) {
368 r = ext4_fs_get_inode_dblk_idx(parent, iblock, &fblock, false);
372 struct ext4_block block;
373 r = ext4_trans_block_get(fs->bdev, &block, fblock);
377 if (!ext4_dir_csum_verify(parent, (void *)block.data)) {
379 DBG_WARN "Leaf block checksum failed."
380 "Inode: %" PRIu32", "
381 "Block: %" PRIu32"\n",
386 /* If adding is successful, function can finish */
387 r = ext4_dir_try_insert_entry(sb, parent, &block, child,
392 r = ext4_block_set(fs->bdev, &block);
400 /* No free block found - needed to allocate next data block */
404 r = ext4_fs_append_inode_dblk(parent, &fblock, &iblock);
411 r = ext4_trans_block_get_noread(fs->bdev, &b, fblock);
415 /* Fill block with zeroes */
416 memset(b.data, 0, block_size);
417 struct ext4_dir_en *blk_en = (void *)b.data;
420 if (ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM)) {
421 uint16_t el = block_size - sizeof(struct ext4_dir_entry_tail);
422 ext4_dir_write_entry(sb, blk_en, el, child, name, name_len);
423 ext4_dir_init_entry_tail(EXT4_DIRENT_TAIL(b.data, block_size));
425 ext4_dir_write_entry(sb, blk_en, block_size, child, name,
429 ext4_dir_set_csum(parent, (void *)b.data);
430 ext4_trans_set_block_dirty(b.buf);
431 r = ext4_block_set(fs->bdev, &b);
436 int ext4_dir_find_entry(struct ext4_dir_search_result *result,
437 struct ext4_inode_ref *parent, const char *name,
441 struct ext4_sblock *sb = &parent->fs->sb;
444 result->block.lb_id = 0;
445 result->dentry = NULL;
447 #if CONFIG_DIR_INDEX_ENABLE
449 if ((ext4_sb_feature_com(sb, EXT4_FCOM_DIR_INDEX)) &&
450 (ext4_inode_has_flag(parent->inode, EXT4_INODE_FLAG_INDEX))) {
451 r = ext4_dir_dx_find_entry(result, parent, name_len, name);
452 /* Check if index is not corrupted */
453 if (r != EXT4_ERR_BAD_DX_DIR) {
460 /* Needed to clear dir index flag if corrupted */
461 ext4_inode_clear_flag(parent->inode, EXT4_INODE_FLAG_INDEX);
462 parent->dirty = true;
466 /* Linear algorithm */
470 uint32_t block_size = ext4_sb_get_block_size(sb);
471 uint64_t inode_size = ext4_inode_get_size(sb, parent->inode);
472 uint32_t total_blocks = (uint32_t)(inode_size / block_size);
474 /* Walk through all data blocks */
475 for (iblock = 0; iblock < total_blocks; ++iblock) {
476 /* Load block address */
477 r = ext4_fs_get_inode_dblk_idx(parent, iblock, &fblock, false);
481 /* Load data block */
483 r = ext4_trans_block_get(parent->fs->bdev, &b, fblock);
487 if (!ext4_dir_csum_verify(parent, (void *)b.data)) {
489 DBG_WARN "Leaf block checksum failed."
490 "Inode: %" PRIu32", "
491 "Block: %" PRIu32"\n",
496 /* Try to find entry in block */
497 struct ext4_dir_en *res_entry;
498 r = ext4_dir_find_in_block(&b, sb, name_len, name, &res_entry);
501 result->dentry = res_entry;
505 /* Entry not found - put block and continue to the next block */
507 r = ext4_block_set(parent->fs->bdev, &b);
515 int ext4_dir_remove_entry(struct ext4_inode_ref *parent, const char *name,
518 struct ext4_sblock *sb = &parent->fs->sb;
519 /* Check if removing from directory */
520 if (!ext4_inode_is_type(sb, parent->inode, EXT4_INODE_MODE_DIRECTORY))
523 /* Try to find entry */
524 struct ext4_dir_search_result result;
525 int rc = ext4_dir_find_entry(&result, parent, name, name_len);
529 /* Invalidate entry */
530 ext4_dir_en_set_inode(result.dentry, 0);
532 /* Store entry position in block */
533 uint32_t pos = (uint8_t *)result.dentry - result.block.data;
536 * If entry is not the first in block, it must be merged
537 * with previous entry
542 /* Start from the first entry in block */
543 struct ext4_dir_en *tmp_de =(void *)result.block.data;
544 uint16_t de_len = ext4_dir_en_get_entry_len(tmp_de);
546 /* Find direct predecessor of removed entry */
547 while ((offset + de_len) < pos) {
548 offset += ext4_dir_en_get_entry_len(tmp_de);
549 tmp_de = (void *)(result.block.data + offset);
550 de_len = ext4_dir_en_get_entry_len(tmp_de);
553 ext4_assert(de_len + offset == pos);
555 /* Add to removed entry length to predecessor's length */
557 del_len = ext4_dir_en_get_entry_len(result.dentry);
558 ext4_dir_en_set_entry_len(tmp_de, de_len + del_len);
561 ext4_dir_set_csum(parent,
562 (struct ext4_dir_en *)result.block.data);
563 ext4_trans_set_block_dirty(result.block.buf);
565 return ext4_dir_destroy_result(parent, &result);
568 int ext4_dir_try_insert_entry(struct ext4_sblock *sb,
569 struct ext4_inode_ref *inode_ref,
570 struct ext4_block *dst_blk,
571 struct ext4_inode_ref *child, const char *name,
574 /* Compute required length entry and align it to 4 bytes */
575 uint32_t block_size = ext4_sb_get_block_size(sb);
576 uint16_t required_len = sizeof(struct ext4_fake_dir_entry) + name_len;
578 if ((required_len % 4) != 0)
579 required_len += 4 - (required_len % 4);
581 /* Initialize pointers, stop means to upper bound */
582 struct ext4_dir_en *start = (void *)dst_blk->data;
583 struct ext4_dir_en *stop = (void *)(dst_blk->data + block_size);
586 * Walk through the block and check for invalid entries
587 * or entries with free space for new entry
589 while (start < stop) {
590 uint32_t inode = ext4_dir_en_get_inode(start);
591 uint16_t rec_len = ext4_dir_en_get_entry_len(start);
592 uint8_t itype = ext4_dir_en_get_inode_type(sb, start);
594 /* If invalid and large enough entry, use it */
595 if ((inode == 0) && (itype != EXT4_DIRENTRY_DIR_CSUM) &&
596 (rec_len >= required_len)) {
597 ext4_dir_write_entry(sb, start, rec_len, child, name,
599 ext4_dir_set_csum(inode_ref, (void *)dst_blk->data);
600 ext4_trans_set_block_dirty(dst_blk->buf);
605 /* Valid entry, try to split it */
608 used_len = ext4_dir_en_get_name_len(sb, start);
611 sz = sizeof(struct ext4_fake_dir_entry) + used_len;
613 if ((used_len % 4) != 0)
614 sz += 4 - (used_len % 4);
616 uint16_t free_space = rec_len - sz;
618 /* There is free space for new entry */
619 if (free_space >= required_len) {
620 /* Cut tail of current entry */
621 struct ext4_dir_en * new_entry;
622 new_entry = (void *)((uint8_t *)start + sz);
623 ext4_dir_en_set_entry_len(start, sz);
624 ext4_dir_write_entry(sb, new_entry, free_space,
625 child, name, name_len);
627 ext4_dir_set_csum(inode_ref,
628 (void *)dst_blk->data);
629 ext4_trans_set_block_dirty(dst_blk->buf);
634 /* Jump to the next entry */
635 start = (void *)((uint8_t *)start + rec_len);
638 /* No free space found for new entry */
642 int ext4_dir_find_in_block(struct ext4_block *block, struct ext4_sblock *sb,
643 size_t name_len, const char *name,
644 struct ext4_dir_en **res_entry)
646 /* Start from the first entry in block */
647 struct ext4_dir_en *de = (struct ext4_dir_en *)block->data;
649 /* Set upper bound for cycling */
650 uint8_t *addr_limit = block->data + ext4_sb_get_block_size(sb);
652 /* Walk through the block and check entries */
653 while ((uint8_t *)de < addr_limit) {
654 /* Termination condition */
655 if ((uint8_t *)de + name_len > addr_limit)
658 /* Valid entry - check it */
659 if (ext4_dir_en_get_inode(de) != 0) {
660 /* For more efficient compare only lengths firstly*/
661 uint16_t el = ext4_dir_en_get_name_len(sb, de);
662 if (el == name_len) {
664 if (memcmp(name, de->name, name_len) == 0) {
671 uint16_t de_len = ext4_dir_en_get_entry_len(de);
673 /* Corrupted entry */
677 /* Jump to next entry */
678 de = (struct ext4_dir_en *)((uint8_t *)de + de_len);
681 /* Entry not found */
685 int ext4_dir_destroy_result(struct ext4_inode_ref *parent,
686 struct ext4_dir_search_result *result)
688 if (result->block.lb_id)
689 return ext4_block_set(parent->fs->bdev, &result->block);