ext4_extent: some comments cleanup in ext4_ext_correct_indexes.
[lwext4.git] / lwext4 / ext4_dir.c
index 5e09133f57a94098a13d79324e6fd9a1a390c949..1caa73b2928c8d05c83decbb5a69042cf5bf01d4 100644 (file)
@@ -42,7 +42,7 @@
 #include "ext4_config.h"
 #include "ext4_dir.h"
 #include "ext4_dir_idx.h"
-#include "ext4_crc32c.h"
+#include "ext4_crc32.h"
 #include "ext4_inode.h"
 #include "ext4_fs.h"
 
@@ -53,7 +53,7 @@
 /* Walk through a dirent block to find a checksum "dirent" at the tail */
 static struct ext4_dir_entry_tail *
 ext4_dir_get_tail(struct ext4_inode_ref *inode_ref,
-               struct ext4_dir_entry_ll *de)
+               struct ext4_dir_en *de)
 {
        struct ext4_dir_entry_tail *t;
        struct ext4_sblock *sb = &inode_ref->fs->sb;
@@ -72,7 +72,7 @@ ext4_dir_get_tail(struct ext4_inode_ref *inode_ref,
 
 #if CONFIG_META_CSUM_ENABLE
 static uint32_t ext4_dir_csum(struct ext4_inode_ref *inode_ref,
-                             struct ext4_dir_entry_ll *dirent, int size)
+                             struct ext4_dir_en *dirent, int size)
 {
        uint32_t csum;
        struct ext4_sblock *sb = &inode_ref->fs->sb;
@@ -94,7 +94,7 @@ static uint32_t ext4_dir_csum(struct ext4_inode_ref *inode_ref,
 #endif
 
 bool ext4_dir_csum_verify(struct ext4_inode_ref *inode_ref,
-                             struct ext4_dir_entry_ll *dirent)
+                             struct ext4_dir_en *dirent)
 {
 #ifdef CONFIG_META_CSUM_ENABLE
        struct ext4_dir_entry_tail *t;
@@ -126,7 +126,7 @@ void ext4_dir_init_entry_tail(struct ext4_dir_entry_tail *t)
 }
 
 void ext4_dir_set_csum(struct ext4_inode_ref *inode_ref,
-                          struct ext4_dir_entry_ll *dirent)
+                          struct ext4_dir_en *dirent)
 {
        struct ext4_dir_entry_tail *t;
        struct ext4_sblock *sb = &inode_ref->fs->sb;
@@ -150,7 +150,7 @@ void ext4_dir_set_csum(struct ext4_inode_ref *inode_ref,
  * @param block_size Size of data block
  * @return Error code
  */
-static int ext4_dir_iterator_set(struct ext4_dir_iterator *it,
+static int ext4_dir_iterator_set(struct ext4_dir_iter *it,
                                 uint32_t block_size)
 {
        uint32_t off_in_block = it->curr_off % block_size;
@@ -166,16 +166,16 @@ static int ext4_dir_iterator_set(struct ext4_dir_iterator *it,
        if (off_in_block > block_size - 8)
                return EIO;
 
-       struct ext4_dir_entry_ll *en;
+       struct ext4_dir_en *en;
        en = (void *)(it->curr_blk.data + off_in_block);
 
        /* Ensure that the whole entry does not overflow the block */
-       uint16_t length = ext4_dir_entry_ll_get_entry_length(en);
+       uint16_t length = ext4_dir_en_get_entry_len(en);
        if (off_in_block + length > block_size)
                return EIO;
 
        /* Ensure the name length is not too large */
-       if (ext4_dir_entry_ll_get_name_length(sb, en) > length - 8)
+       if (ext4_dir_en_get_name_len(sb, en) > length - 8)
                return EIO;
 
        /* Everything OK - "publish" the entry */
@@ -189,7 +189,7 @@ static int ext4_dir_iterator_set(struct ext4_dir_iterator *it,
  * @param pos Position of the next entry
  * @return Error code
  */
-static int ext4_dir_iterator_seek(struct ext4_dir_iterator *it, uint64_t pos)
+static int ext4_dir_iterator_seek(struct ext4_dir_iter *it, uint64_t pos)
 {
        struct ext4_sblock *sb = &it->inode_ref->fs->sb;
        struct ext4_inode *inode = it->inode_ref->inode;
@@ -234,12 +234,12 @@ static int ext4_dir_iterator_seek(struct ext4_dir_iterator *it, uint64_t pos)
                }
 
                ext4_fsblk_t next_blk;
-               r = ext4_fs_get_inode_data_block_index(it->inode_ref,
-                                               next_blk_idx, &next_blk, false);
+               r = ext4_fs_get_inode_dblk_idx(it->inode_ref, next_blk_idx,
+                                              &next_blk, false);
                if (r != EOK)
                        return r;
 
-               r = ext4_block_get(bdev, &it->curr_blk, next_blk);
+               r = ext4_trans_block_get(bdev, &it->curr_blk, next_blk);
                if (r != EOK) {
                        it->curr_blk.lb_id = 0;
                        return r;
@@ -250,7 +250,7 @@ static int ext4_dir_iterator_seek(struct ext4_dir_iterator *it, uint64_t pos)
        return ext4_dir_iterator_set(it, block_size);
 }
 
-int ext4_dir_iterator_init(struct ext4_dir_iterator *it,
+int ext4_dir_iterator_init(struct ext4_dir_iter *it,
                           struct ext4_inode_ref *inode_ref, uint64_t pos)
 {
        it->inode_ref = inode_ref;
@@ -261,26 +261,26 @@ int ext4_dir_iterator_init(struct ext4_dir_iterator *it,
        return ext4_dir_iterator_seek(it, pos);
 }
 
-int ext4_dir_iterator_next(struct ext4_dir_iterator *it)
+int ext4_dir_iterator_next(struct ext4_dir_iter *it)
 {
        int r = EOK;
        uint16_t skip;
 
        while (r == EOK) {
-               skip = ext4_dir_entry_ll_get_entry_length(it->curr);
+               skip = ext4_dir_en_get_entry_len(it->curr);
                r = ext4_dir_iterator_seek(it, it->curr_off + skip);
 
                if (!it->curr)
                        break;
                /*Skip NULL referenced entry*/
-               if (ext4_dir_entry_ll_get_inode(it->curr) != 0)
+               if (ext4_dir_en_get_inode(it->curr) != 0)
                        break;
        }
 
        return r;
 }
 
-int ext4_dir_iterator_fini(struct ext4_dir_iterator *it)
+int ext4_dir_iterator_fini(struct ext4_dir_iter *it)
 {
        it->curr = 0;
 
@@ -290,7 +290,7 @@ int ext4_dir_iterator_fini(struct ext4_dir_iterator *it)
        return EOK;
 }
 
-void ext4_dir_write_entry(struct ext4_sblock *sb, struct ext4_dir_entry_ll *en,
+void ext4_dir_write_entry(struct ext4_sblock *sb, struct ext4_dir_en *en,
                          uint16_t entry_len, struct ext4_inode_ref *child,
                          const char *name, size_t name_len)
 {
@@ -300,13 +300,13 @@ void ext4_dir_write_entry(struct ext4_sblock *sb, struct ext4_dir_entry_ll *en,
        /* Set type of entry */
        switch (ext4_inode_type(sb, child->inode)) {
        case EXT4_INODE_MODE_DIRECTORY:
-               ext4_dir_entry_ll_set_inode_type(sb, en, EXT4_DE_DIR);
+               ext4_dir_en_set_inode_type(sb, en, EXT4_DE_DIR);
                break;
        case EXT4_INODE_MODE_FILE:
-               ext4_dir_entry_ll_set_inode_type(sb, en, EXT4_DE_REG_FILE);
+               ext4_dir_en_set_inode_type(sb, en, EXT4_DE_REG_FILE);
                break;
        case EXT4_INODE_MODE_SOFTLINK:
-               ext4_dir_entry_ll_set_inode_type(sb, en, EXT4_DE_SYMLINK);
+               ext4_dir_en_set_inode_type(sb, en, EXT4_DE_SYMLINK);
                break;
        default:
                /* FIXME: right now we only support 3 inode type. */
@@ -314,9 +314,9 @@ void ext4_dir_write_entry(struct ext4_sblock *sb, struct ext4_dir_entry_ll *en,
        }
 
        /* Set basic attributes */
-       ext4_dir_entry_ll_set_inode(en, child->index);
-       ext4_dir_entry_ll_set_entry_length(en, entry_len);
-       ext4_dir_entry_ll_set_name_length(sb, en, name_len);
+       ext4_dir_en_set_inode(en, child->index);
+       ext4_dir_en_set_entry_len(en, entry_len);
+       ext4_dir_en_set_name_len(sb, en, name_len);
 
        /* Write name */
        memcpy(en->name, name, name_len);
@@ -325,6 +325,7 @@ void ext4_dir_write_entry(struct ext4_sblock *sb, struct ext4_dir_entry_ll *en,
 int ext4_dir_add_entry(struct ext4_inode_ref *parent, const char *name,
                       uint32_t name_len, struct ext4_inode_ref *child)
 {
+       int r;
        struct ext4_fs *fs = parent->fs;
        struct ext4_sblock *sb = &parent->fs->sb;
 
@@ -332,12 +333,12 @@ int ext4_dir_add_entry(struct ext4_inode_ref *parent, const char *name,
        /* Index adding (if allowed) */
        if ((ext4_sb_feature_com(sb, EXT4_FCOM_DIR_INDEX)) &&
            (ext4_inode_has_flag(parent->inode, EXT4_INODE_FLAG_INDEX))) {
-               int rc = ext4_dir_dx_add_entry(parent, child, name);
+               r = ext4_dir_dx_add_entry(parent, child, name);
 
                /* Check if index is not corrupted */
-               if (rc != EXT4_ERR_BAD_DX_DIR) {
-                       if (rc != EOK)
-                               return rc;
+               if (r != EXT4_ERR_BAD_DX_DIR) {
+                       if (r != EOK)
+                               return r;
 
                        return EOK;
                }
@@ -358,15 +359,14 @@ int ext4_dir_add_entry(struct ext4_inode_ref *parent, const char *name,
        /* Find block, where is space for new entry and try to add */
        bool success = false;
        for (iblock = 0; iblock < total_blocks; ++iblock) {
-               int rc = ext4_fs_get_inode_data_block_index(parent, iblock,
-                                                       &fblock, false);
-               if (rc != EOK)
-                       return rc;
+               r = ext4_fs_get_inode_dblk_idx(parent, iblock, &fblock, false);
+               if (r != EOK)
+                       return r;
 
                struct ext4_block block;
-               rc = ext4_block_get(fs->bdev, &block, fblock);
-               if (rc != EOK)
-                       return rc;
+               r = ext4_trans_block_get(fs->bdev, &block, fblock);
+               if (r != EOK)
+                       return r;
 
                if (!ext4_dir_csum_verify(parent, (void *)block.data)) {
                        ext4_dbg(DEBUG_DIR,
@@ -378,14 +378,14 @@ int ext4_dir_add_entry(struct ext4_inode_ref *parent, const char *name,
                }
 
                /* If adding is successful, function can finish */
-               rc = ext4_dir_try_insert_entry(sb, parent, &block, child,
+               r = ext4_dir_try_insert_entry(sb, parent, &block, child,
                                                name, name_len);
-               if (rc == EOK)
+               if (r == EOK)
                        success = true;
 
-               rc = ext4_block_set(fs->bdev, &block);
-               if (rc != EOK)
-                       return rc;
+               r = ext4_block_set(fs->bdev, &block);
+               if (r != EOK)
+                       return r;
 
                if (success)
                        return EOK;
@@ -395,20 +395,20 @@ int ext4_dir_add_entry(struct ext4_inode_ref *parent, const char *name,
 
        iblock = 0;
        fblock = 0;
-       int rc = ext4_fs_append_inode_block(parent, &fblock, &iblock);
-       if (rc != EOK)
-               return rc;
+       r = ext4_fs_append_inode_dblk(parent, &fblock, &iblock);
+       if (r != EOK)
+               return r;
 
        /* Load new block */
        struct ext4_block b;
 
-       rc = ext4_block_get_noread(fs->bdev, &b, fblock);
-       if (rc != EOK)
-               return rc;
+       r = ext4_trans_block_get_noread(fs->bdev, &b, fblock);
+       if (r != EOK)
+               return r;
 
        /* Fill block with zeroes */
        memset(b.data, 0, block_size);
-       struct ext4_dir_entry_ll *blk_en = (void *)b.data;
+       struct ext4_dir_en *blk_en = (void *)b.data;
 
        /* Save new block */
        if (ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM)) {
@@ -421,16 +421,17 @@ int ext4_dir_add_entry(struct ext4_inode_ref *parent, const char *name,
        }
 
        ext4_dir_set_csum(parent, (void *)b.data);
-       b.dirty = true;
-       rc = ext4_block_set(fs->bdev, &b);
+       ext4_trans_set_block_dirty(b.buf);
+       r = ext4_block_set(fs->bdev, &b);
 
-       return rc;
+       return r;
 }
 
 int ext4_dir_find_entry(struct ext4_dir_search_result *result,
                        struct ext4_inode_ref *parent, const char *name,
                        uint32_t name_len)
 {
+       int r;
        struct ext4_sblock *sb = &parent->fs->sb;
 
        /* Entry clear */
@@ -441,12 +442,11 @@ int ext4_dir_find_entry(struct ext4_dir_search_result *result,
        /* Index search */
        if ((ext4_sb_feature_com(sb, EXT4_FCOM_DIR_INDEX)) &&
            (ext4_inode_has_flag(parent->inode, EXT4_INODE_FLAG_INDEX))) {
-               int rc = ext4_dir_dx_find_entry(result, parent, name_len, name);
-
+               r = ext4_dir_dx_find_entry(result, parent, name_len, name);
                /* Check if index is not corrupted */
-               if (rc != EXT4_ERR_BAD_DX_DIR) {
-                       if (rc != EOK)
-                               return rc;
+               if (r != EXT4_ERR_BAD_DX_DIR) {
+                       if (r != EOK)
+                               return r;
 
                        return EOK;
                }
@@ -468,18 +468,15 @@ int ext4_dir_find_entry(struct ext4_dir_search_result *result,
        /* Walk through all data blocks */
        for (iblock = 0; iblock < total_blocks; ++iblock) {
                /* Load block address */
-               int rc =
-                   ext4_fs_get_inode_data_block_index(parent,
-                                   iblock, &fblock,
-                                   false);
-               if (rc != EOK)
-                       return rc;
+               r = ext4_fs_get_inode_dblk_idx(parent, iblock, &fblock, false);
+               if (r != EOK)
+                       return r;
 
                /* Load data block */
                struct ext4_block b;
-               rc = ext4_block_get(parent->fs->bdev, &b, fblock);
-               if (rc != EOK)
-                       return rc;
+               r = ext4_trans_block_get(parent->fs->bdev, &b, fblock);
+               if (r != EOK)
+                       return r;
 
                if (!ext4_dir_csum_verify(parent, (void *)b.data)) {
                        ext4_dbg(DEBUG_DIR,
@@ -491,9 +488,9 @@ int ext4_dir_find_entry(struct ext4_dir_search_result *result,
                }
 
                /* Try to find entry in block */
-               struct ext4_dir_entry_ll *res_entry;
-               rc = ext4_dir_find_in_block(&b, sb, name_len, name, &res_entry);
-               if (rc == EOK) {
+               struct ext4_dir_en *res_entry;
+               r = ext4_dir_find_in_block(&b, sb, name_len, name, &res_entry);
+               if (r == EOK) {
                        result->block = b;
                        result->dentry = res_entry;
                        return EOK;
@@ -501,9 +498,9 @@ int ext4_dir_find_entry(struct ext4_dir_search_result *result,
 
                /* Entry not found - put block and continue to the next block */
 
-               rc = ext4_block_set(parent->fs->bdev, &b);
-               if (rc != EOK)
-                       return rc;
+               r = ext4_block_set(parent->fs->bdev, &b);
+               if (r != EOK)
+                       return r;
        }
 
        return ENOENT;
@@ -524,7 +521,7 @@ int ext4_dir_remove_entry(struct ext4_inode_ref *parent, const char *name,
                return rc;
 
        /* Invalidate entry */
-       ext4_dir_entry_ll_set_inode(result.dentry, 0);
+       ext4_dir_en_set_inode(result.dentry, 0);
 
        /* Store entry position in block */
        uint32_t pos = (uint8_t *)result.dentry - result.block.data;
@@ -537,27 +534,27 @@ int ext4_dir_remove_entry(struct ext4_inode_ref *parent, const char *name,
                uint32_t offset = 0;
 
                /* Start from the first entry in block */
-               struct ext4_dir_entry_ll *tmp_de =(void *)result.block.data;
-               uint16_t de_len = ext4_dir_entry_ll_get_entry_length(tmp_de);
+               struct ext4_dir_en *tmp_de =(void *)result.block.data;
+               uint16_t de_len = ext4_dir_en_get_entry_len(tmp_de);
 
                /* Find direct predecessor of removed entry */
                while ((offset + de_len) < pos) {
-                       offset += ext4_dir_entry_ll_get_entry_length(tmp_de);
+                       offset += ext4_dir_en_get_entry_len(tmp_de);
                        tmp_de = (void *)(result.block.data + offset);
-                       de_len = ext4_dir_entry_ll_get_entry_length(tmp_de);
+                       de_len = ext4_dir_en_get_entry_len(tmp_de);
                }
 
                ext4_assert(de_len + offset == pos);
 
                /* Add to removed entry length to predecessor's length */
                uint16_t del_len;
-               del_len = ext4_dir_entry_ll_get_entry_length(result.dentry);
-               ext4_dir_entry_ll_set_entry_length(tmp_de, de_len + del_len);
+               del_len = ext4_dir_en_get_entry_len(result.dentry);
+               ext4_dir_en_set_entry_len(tmp_de, de_len + del_len);
        }
 
        ext4_dir_set_csum(parent,
-                       (struct ext4_dir_entry_ll *)result.block.data);
-       result.block.dirty = true;
+                       (struct ext4_dir_en *)result.block.data);
+       ext4_trans_set_block_dirty(result.block.buf);
 
        return ext4_dir_destroy_result(parent, &result);
 }
@@ -576,17 +573,17 @@ int ext4_dir_try_insert_entry(struct ext4_sblock *sb,
                required_len += 4 - (required_len % 4);
 
        /* Initialize pointers, stop means to upper bound */
-       struct ext4_dir_entry_ll *start = (void *)dst_blk->data;
-       struct ext4_dir_entry_ll *stop = (void *)(dst_blk->data + block_size);
+       struct ext4_dir_en *start = (void *)dst_blk->data;
+       struct ext4_dir_en *stop = (void *)(dst_blk->data + block_size);
 
        /*
         * Walk through the block and check for invalid entries
         * or entries with free space for new entry
         */
        while (start < stop) {
-               uint32_t inode = ext4_dir_entry_ll_get_inode(start);
-               uint16_t rec_len = ext4_dir_entry_ll_get_entry_length(start);
-               uint8_t itype = ext4_dir_entry_ll_get_inode_type(sb, start);
+               uint32_t inode = ext4_dir_en_get_inode(start);
+               uint16_t rec_len = ext4_dir_en_get_entry_len(start);
+               uint8_t itype = ext4_dir_en_get_inode_type(sb, start);
 
                /* If invalid and large enough entry, use it */
                if ((inode == 0) && (itype != EXT4_DIRENTRY_DIR_CSUM) &&
@@ -594,7 +591,7 @@ int ext4_dir_try_insert_entry(struct ext4_sblock *sb,
                        ext4_dir_write_entry(sb, start, rec_len, child, name,
                                             name_len);
                        ext4_dir_set_csum(inode_ref, (void *)dst_blk->data);
-                       dst_blk->dirty = true;
+                       ext4_trans_set_block_dirty(dst_blk->buf);
 
                        return EOK;
                }
@@ -602,7 +599,7 @@ int ext4_dir_try_insert_entry(struct ext4_sblock *sb,
                /* Valid entry, try to split it */
                if (inode != 0) {
                        uint16_t used_len;
-                       used_len = ext4_dir_entry_ll_get_name_length(sb, start);
+                       used_len = ext4_dir_en_get_name_len(sb, start);
 
                        uint16_t sz;
                        sz = sizeof(struct ext4_fake_dir_entry) + used_len;
@@ -615,15 +612,15 @@ int ext4_dir_try_insert_entry(struct ext4_sblock *sb,
                        /* There is free space for new entry */
                        if (free_space >= required_len) {
                                /* Cut tail of current entry */
-                               struct ext4_dir_entry_ll * new_entry;
+                               struct ext4_dir_en * new_entry;
                                new_entry = (void *)((uint8_t *)start + sz);
-                               ext4_dir_entry_ll_set_entry_length(start, sz);
+                               ext4_dir_en_set_entry_len(start, sz);
                                ext4_dir_write_entry(sb, new_entry, free_space,
                                                     child, name, name_len);
 
                                ext4_dir_set_csum(inode_ref,
                                                  (void *)dst_blk->data);
-                               dst_blk->dirty = true;
+                               ext4_trans_set_block_dirty(dst_blk->buf);
                                return EOK;
                        }
                }
@@ -638,10 +635,10 @@ int ext4_dir_try_insert_entry(struct ext4_sblock *sb,
 
 int ext4_dir_find_in_block(struct ext4_block *block, struct ext4_sblock *sb,
                           size_t name_len, const char *name,
-                          struct ext4_dir_entry_ll **res_entry)
+                          struct ext4_dir_en **res_entry)
 {
        /* Start from the first entry in block */
-       struct ext4_dir_entry_ll *de = (struct ext4_dir_entry_ll *)block->data;
+       struct ext4_dir_en *de = (struct ext4_dir_en *)block->data;
 
        /* Set upper bound for cycling */
        uint8_t *addr_limit = block->data + ext4_sb_get_block_size(sb);
@@ -653,9 +650,9 @@ int ext4_dir_find_in_block(struct ext4_block *block, struct ext4_sblock *sb,
                        break;
 
                /* Valid entry - check it */
-               if (ext4_dir_entry_ll_get_inode(de) != 0) {
+               if (ext4_dir_en_get_inode(de) != 0) {
                        /* For more efficient compare only lengths firstly*/
-                       uint16_t el = ext4_dir_entry_ll_get_name_length(sb, de);
+                       uint16_t el = ext4_dir_en_get_name_len(sb, de);
                        if (el == name_len) {
                                /* Compare names */
                                if (memcmp(name, de->name, name_len) == 0) {
@@ -665,14 +662,14 @@ int ext4_dir_find_in_block(struct ext4_block *block, struct ext4_sblock *sb,
                        }
                }
 
-               uint16_t de_len = ext4_dir_entry_ll_get_entry_length(de);
+               uint16_t de_len = ext4_dir_en_get_entry_len(de);
 
                /* Corrupted entry */
                if (de_len == 0)
                        return EINVAL;
 
                /* Jump to next entry */
-               de = (struct ext4_dir_entry_ll *)((uint8_t *)de + de_len);
+               de = (struct ext4_dir_en *)((uint8_t *)de + de_len);
        }
 
        /* Entry not found */