Sblock fill by using to_le32/to_le_16 macros & drop gdt_reserved blocks
[lwext4.git] / lwext4 / ext4.c
index 7ef875551c6ca6cc2842f6d74b0e263a411bb3eb..789c12d77588be77a26165ede1d32e16635b3c4e 100644 (file)
@@ -35,6 +35,7 @@
  */
 
 #include "ext4_config.h"
+#include "ext4.h"
 #include "ext4_blockdev.h"
 #include "ext4_types.h"
 #include "ext4_debug.h"
@@ -45,7 +46,7 @@
 #include "ext4_super.h"
 #include "ext4_dir_idx.h"
 #include "ext4_xattr.h"
-#include "ext4.h"
+
 
 #include <stdlib.h>
 #include <string.h>
@@ -146,7 +147,7 @@ static int ext4_has_children(bool *has_children, struct ext4_inode_ref *enode)
                return EOK;
        }
 
-       struct ext4_directory_iterator it;
+       struct ext4_dir_iterator it;
        int rc = ext4_dir_iterator_init(&it, enode, 0);
        if (rc != EOK)
                return rc;
@@ -198,35 +199,36 @@ static int ext4_link(struct ext4_mountpoint *mp, struct ext4_inode_ref *parent,
        if (ext4_inode_is_type(&mp->fs.sb, child->inode,
                               EXT4_INODE_MODE_DIRECTORY) &&
            !rename) {
-               rc = ext4_dir_add_entry(child, ".", strlen("."), child);
-               if (rc != EOK) {
-                       ext4_dir_remove_entry(parent, name, strlen(name));
-                       return rc;
-               }
-
-               rc = ext4_dir_add_entry(child, "..", strlen(".."), parent);
-               if (rc != EOK) {
-                       ext4_dir_remove_entry(parent, name, strlen(name));
-                       ext4_dir_remove_entry(child, ".", strlen("."));
-                       return rc;
-               }
-
-               /*New empty directory. Two links (. and ..) */
-               ext4_inode_set_links_count(child->inode, 2);
 
 #if CONFIG_DIR_INDEX_ENABLE
                /* Initialize directory index if supported */
-               if (ext4_sb_has_feature_compatible(
-                       &mp->fs.sb, EXT4_FEATURE_COMPAT_DIR_INDEX)) {
-                       rc = ext4_dir_dx_init(child);
+               if (ext4_sb_feature_com(&mp->fs.sb, EXT4_FCOM_DIR_INDEX)) {
+                       rc = ext4_dir_dx_init(child, parent);
                        if (rc != EOK)
                                return rc;
 
                        ext4_inode_set_flag(child->inode,
                                            EXT4_INODE_FLAG_INDEX);
                        child->dirty = true;
-               }
+               } else
 #endif
+               {
+                       rc = ext4_dir_add_entry(child, ".", strlen("."), child);
+                       if (rc != EOK) {
+                               ext4_dir_remove_entry(parent, name, strlen(name));
+                               return rc;
+                       }
+
+                       rc = ext4_dir_add_entry(child, "..", strlen(".."), parent);
+                       if (rc != EOK) {
+                               ext4_dir_remove_entry(parent, name, strlen(name));
+                               ext4_dir_remove_entry(child, ".", strlen("."));
+                               return rc;
+                       }
+               }
+
+               /*New empty directory. Two links (. and ..) */
+               ext4_inode_set_links_count(child->inode, 2);
 
                ext4_fs_inode_links_count_inc(parent);
                child->dirty = true;
@@ -241,7 +243,7 @@ static int ext4_link(struct ext4_mountpoint *mp, struct ext4_inode_ref *parent,
                        int has_flag_index =
                                ext4_inode_has_flag(child->inode,
                                                    EXT4_INODE_FLAG_INDEX);
-                       struct ext4_directory_search_result result;
+                       struct ext4_dir_search_result result;
                        if (!has_flag_index) {
                                rc = ext4_dir_find_entry(&result,
                                                         child, "..",
@@ -581,12 +583,12 @@ static int ext4_generic_open2(ext4_file *f, const char *path, int flags,
                              uint32_t *name_off)
 {
        bool is_goal = false;
-       uint8_t inode_type = EXT4_DIRENTRY_UNKNOWN;
+       uint32_t inode_mode = EXT4_INODE_MODE_DIRECTORY;
        uint32_t next_inode;
 
        int r;
        struct ext4_mountpoint *mp = ext4_get_mount(path);
-       struct ext4_directory_search_result result;
+       struct ext4_dir_search_result result;
        struct ext4_inode_ref ref;
 
        f->mp = 0;
@@ -671,20 +673,37 @@ static int ext4_generic_open2(ext4_file *f, const char *path, int flags,
                        *parent_inode = ref.index;
 
                next_inode = ext4_dir_entry_ll_get_inode(result.dentry);
-               inode_type =
-                   ext4_dir_entry_ll_get_inode_type(&mp->fs.sb, result.dentry);
+               if (ext4_sb_feature_incom(&mp->fs.sb, EXT4_FINCOM_FILETYPE)) {
+                       int inode_type =
+                               ext4_dir_entry_ll_get_inode_type(&mp->fs.sb,
+                                               result.dentry);
+                       inode_mode = ext4_fs_correspond_inode_mode(inode_type);
+               } else {
+                       struct ext4_inode_ref child_ref;
+                       r = ext4_fs_get_inode_ref(&mp->fs, next_inode,
+                                       &child_ref);
+                       if (r != EOK)
+                               break;
+
+                       inode_mode = ext4_inode_type(&mp->fs.sb,
+                                       child_ref.inode);
+
+                       ext4_fs_put_inode_ref(&child_ref);
+               }
 
                r = ext4_dir_destroy_result(&ref, &result);
                if (r != EOK)
                        break;
 
                /*If expected file error*/
-               if (inode_type != EXT4_DIRENTRY_DIR && !is_goal) {
+               if (inode_mode != EXT4_INODE_MODE_DIRECTORY &&
+                   !is_goal) {
                        r = ENOENT;
                        break;
                }
                if (filetype != EXT4_DIRENTRY_UNKNOWN) {
-                       if ((inode_type != filetype) && is_goal) {
+                       if ((inode_mode != ext4_fs_correspond_inode_mode(filetype)) &&
+                           is_goal) {
                                r = ENOENT;
                                break;
                        }
@@ -715,7 +734,7 @@ static int ext4_generic_open2(ext4_file *f, const char *path, int flags,
        if (is_goal) {
 
                if ((f->flags & O_TRUNC) &&
-                   (inode_type == EXT4_DIRENTRY_REG_FILE)) {
+                   (inode_mode == EXT4_INODE_MODE_FILE)) {
 
                        r = ext4_fs_truncate_inode(&ref, 0);
                        if (r != EOK) {
@@ -763,12 +782,12 @@ static int __ext4_create_hardlink(const char *path,
                bool rename)
 {
        bool is_goal = false;
-       uint8_t inode_type = EXT4_DIRENTRY_DIR;
+       uint32_t inode_mode = EXT4_INODE_MODE_DIRECTORY;
        uint32_t next_inode;
 
        int r;
        struct ext4_mountpoint *mp = ext4_get_mount(path);
-       struct ext4_directory_search_result result;
+       struct ext4_dir_search_result result;
        struct ext4_inode_ref ref;
 
        if (!mp)
@@ -818,14 +837,29 @@ static int __ext4_create_hardlink(const char *path,
                }
 
                next_inode = result.dentry->inode;
-               inode_type =
-                       ext4_dir_entry_ll_get_inode_type(&mp->fs.sb, result.dentry);
+               if (ext4_sb_feature_incom(&mp->fs.sb, EXT4_FINCOM_FILETYPE)) {
+                       int inode_type =
+                               ext4_dir_entry_ll_get_inode_type(&mp->fs.sb,
+                                               result.dentry);
+                       inode_mode = ext4_fs_correspond_inode_mode(inode_type);
+               } else {
+                       struct ext4_inode_ref child_ref;
+                       r = ext4_fs_get_inode_ref(&mp->fs, next_inode,
+                                       &child_ref);
+                       if (r != EOK)
+                               break;
+
+                       inode_mode = ext4_inode_type(&mp->fs.sb,
+                                       child_ref.inode);
+
+                       ext4_fs_put_inode_ref(&child_ref);
+               }
 
                r = ext4_dir_destroy_result(&ref, &result);
                if (r != EOK)
                        break;
 
-               if (inode_type == EXT4_DIRENTRY_REG_FILE) {
+               if (inode_mode != EXT4_INODE_MODE_DIRECTORY) {
                        if (is_goal)
                                r = EEXIST;
                        else
@@ -1254,11 +1288,15 @@ int ext4_ftruncate(ext4_file *f, uint64_t size)
 
 int ext4_fread(ext4_file *f, void *buf, size_t size, size_t *rcnt)
 {
-       uint32_t u;
-       ext4_fsblk_t fblock;
-       uint32_t fblock_cnt;
-       uint32_t sblock;
+       uint32_t unaligned;
+       uint32_t iblock_idx;
+       uint32_t iblock_last;
        uint32_t block_size;
+
+       ext4_fsblk_t fblock;
+       ext4_fsblk_t fblock_start;
+       uint32_t fblock_count;
+
        uint8_t *u8_buf = buf;
        int r;
        struct ext4_block b;
@@ -1288,8 +1326,10 @@ int ext4_fread(ext4_file *f, void *buf, size_t size, size_t *rcnt)
 
        block_size = ext4_sb_get_block_size(&f->mp->fs.sb);
        size = size > (f->fsize - f->fpos) ? (f->fsize - f->fpos) : size;
-       sblock = (f->fpos) / block_size;
-       u = (f->fpos) % block_size;
+
+       iblock_idx = (f->fpos) / block_size;
+       iblock_last = (f->fpos + size) / block_size;
+       unaligned = (f->fpos) % block_size;
 
        /*If the size of symlink is smaller than 60 bytes*/
        if (ext4_inode_is_type(&f->mp->fs.sb, ref.inode, EXT4_INODE_MODE_SOFTLINK)
@@ -1297,10 +1337,10 @@ int ext4_fread(ext4_file *f, void *buf, size_t size, size_t *rcnt)
                        && !ext4_inode_get_blocks_count(&f->mp->fs.sb, ref.inode)) {
                char *content = (char *)ref.inode->blocks;
                if (f->fpos < f->fsize) {
-                       r = (u + size > f->fsize)
-                               ?(f->fsize - u)
+                       r = (unaligned + size > f->fsize)
+                               ?(f->fsize - unaligned)
                                :(size);
-                       memcpy(buf, content + u, r);
+                       memcpy(buf, content + unaligned, r);
                        if (rcnt)
                                *rcnt = r;
 
@@ -1315,12 +1355,10 @@ int ext4_fread(ext4_file *f, void *buf, size_t size, size_t *rcnt)
                goto Finish;
        }
 
-       if (u) {
-
-               uint32_t ll = size > (block_size - u) ? (block_size - u) : size;
-
-               r = ext4_fs_get_inode_data_block_index(&ref,
-                               sblock, &fblock,
+       if (unaligned) {
+               uint32_t l = size > (block_size - unaligned) ?
+                               (block_size - unaligned) : size;
+               r = ext4_fs_get_inode_data_block_index(&ref, iblock_idx, &fblock,
                                true);
                if (r != EOK)
                        goto Finish;
@@ -1331,56 +1369,65 @@ int ext4_fread(ext4_file *f, void *buf, size_t size, size_t *rcnt)
                        if (r != EOK)
                                goto Finish;
 
-                       memcpy(u8_buf, b.data + u, ll);
+                       memcpy(u8_buf, b.data + unaligned, l);
 
                        r = ext4_block_set(f->mp->fs.bdev, &b);
                        if (r != EOK)
                                goto Finish;
                } else {
                        /* Yes, we do. */
-                       memset(u8_buf, 0, ll);
+                       memset(u8_buf, 0, l);
                }
 
-               u8_buf += ll;
-               size -= ll;
-               f->fpos += ll;
+               u8_buf += l;
+               size -= l;
+               f->fpos += l;
 
                if (rcnt)
-                       *rcnt += ll;
+                       *rcnt += l;
 
-               sblock++;
+               iblock_idx++;
        }
 
-       fblock_cnt = 0;
+       fblock_start = 0;
+       fblock_count = 0;
        while (size >= block_size) {
+               while (iblock_idx < iblock_last) {
+                       r = ext4_fs_get_inode_data_block_index(&ref, iblock_idx,
+                                                              &fblock, true);
+                       if (r != EOK)
+                               goto Finish;
 
-               r = ext4_fs_get_inode_data_block_index(&ref, sblock,
-                                                      &fblock,
-                                                      true);
-               if (r != EOK)
-                       goto Finish;
+                       iblock_idx++;
 
-               sblock++;
-               fblock_cnt = 1;
+                       if (!fblock_start)
+                               fblock_start = fblock;
 
-               r = ext4_blocks_get_direct(f->mp->fs.bdev, u8_buf, fblock,
-                                          fblock_cnt);
+                       if ((fblock_start + fblock_count) != fblock)
+                               break;
+
+                       fblock_count++;
+               }
+
+               r = ext4_blocks_get_direct(f->mp->fs.bdev, u8_buf, fblock_start,
+                                          fblock_count);
                if (r != EOK)
                        goto Finish;
 
-               size -= block_size * fblock_cnt;
-               u8_buf += block_size * fblock_cnt;
-               f->fpos += block_size * fblock_cnt;
+               size -= block_size * fblock_count;
+               u8_buf += block_size * fblock_count;
+               f->fpos += block_size * fblock_count;
 
                if (rcnt)
-                       *rcnt += block_size * fblock_cnt;
+                       *rcnt += block_size * fblock_count;
 
+               fblock_start = fblock;
+               fblock_count = 1;
        }
 
        if (size) {
-               r = ext4_fs_get_inode_data_block_index(&ref,
-                                               sblock, &fblock,
-                                               true);
+               r = ext4_fs_get_inode_data_block_index(&ref, iblock_idx, &fblock,
+                               true);
                if (r != EOK)
                        goto Finish;
 
@@ -1408,18 +1455,20 @@ Finish:
 
 int ext4_fwrite(ext4_file *f, const void *buf, size_t size, size_t *wcnt)
 {
-       uint32_t u;
-       ext4_fsblk_t fblock;
-
-       uint32_t sblock;
-       uint32_t file_blocks;
+       uint32_t unaligned;
+       uint32_t iblock_idx;
+       uint32_t iblock_last;
+       uint32_t ifile_blocks;
        uint32_t block_size;
-       uint32_t fblock_cnt;
+
+       uint32_t fblock_count;
+       ext4_fsblk_t fblock;
+       ext4_fsblk_t fblock_start;
 
        struct ext4_block b;
        struct ext4_inode_ref ref;
        const uint8_t *u8_buf = buf;
-       int r;
+       int r, rr = EOK;
 
        ext4_assert(f && f->mp);
 
@@ -1442,19 +1491,18 @@ int ext4_fwrite(ext4_file *f, const void *buf, size_t size, size_t *wcnt)
 
        /*Sync file size*/
        f->fsize = ext4_inode_get_size(&f->mp->fs.sb, ref.inode);
-
        block_size = ext4_sb_get_block_size(&f->mp->fs.sb);
 
-       file_blocks = (f->fsize + block_size - 1) / block_size;
-
-       sblock = (f->fpos) / block_size;
+       iblock_last = (f->fpos + size) / block_size;
+       iblock_idx = (f->fpos) / block_size;
+       ifile_blocks = (f->fsize + block_size - 1) / block_size;
 
-       u = (f->fpos) % block_size;
+       unaligned = (f->fpos) % block_size;
 
-       if (u) {
-               uint32_t ll = size > (block_size - u) ? (block_size - u) : size;
-
-               r = ext4_fs_init_inode_data_block_index(&ref, sblock, &fblock);
+       if (unaligned) {
+               uint32_t l = size > (block_size - unaligned) ?
+                               (block_size - unaligned) : size;
+               r = ext4_fs_init_inode_data_block_index(&ref, iblock_idx, &fblock);
                if (r != EOK)
                        goto Finish;
 
@@ -1462,21 +1510,21 @@ int ext4_fwrite(ext4_file *f, const void *buf, size_t size, size_t *wcnt)
                if (r != EOK)
                        goto Finish;
 
-               memcpy(b.data + u, u8_buf, ll);
+               memcpy(b.data + unaligned, u8_buf, l);
                b.dirty = true;
 
                r = ext4_block_set(f->mp->fs.bdev, &b);
                if (r != EOK)
                        goto Finish;
 
-               u8_buf += ll;
-               size -= ll;
-               f->fpos += ll;
+               u8_buf += l;
+               size -= l;
+               f->fpos += l;
 
                if (wcnt)
-                       *wcnt += ll;
+                       *wcnt += l;
 
-               sblock++;
+               iblock_idx++;
        }
 
        /*Start write back cache mode.*/
@@ -1484,36 +1532,61 @@ int ext4_fwrite(ext4_file *f, const void *buf, size_t size, size_t *wcnt)
        if (r != EOK)
                goto Finish;
 
-       fblock_cnt = 0;
+       fblock_start = 0;
+       fblock_count = 0;
        while (size >= block_size) {
 
-               if (sblock < file_blocks) {
-                       r = ext4_fs_init_inode_data_block_index(
-                           &ref, sblock, &fblock);
-                       if (r != EOK)
-                               break;
-               } else {
-                       r = ext4_fs_append_inode_block(&ref, &fblock,
-                                                      &sblock);
-                       if (r != EOK)
+               while (iblock_idx < iblock_last) {
+                       if (iblock_idx < ifile_blocks) {
+                               r = ext4_fs_init_inode_data_block_index(
+                                   &ref, iblock_idx, &fblock);
+                               if (r != EOK)
+                                       goto Finish;
+                       } else {
+                               rr = ext4_fs_append_inode_block(&ref, &fblock,
+                                                              &iblock_idx);
+                               if (rr != EOK) {
+                                       /* Unable to append more blocks. But some
+                                        * block might be allocated already.
+                                        * */
+                                       break;
+                               }
+                       }
+
+                       iblock_idx++;
+
+                       if (!fblock_start) {
+                               fblock_start = fblock;
+                       }
+
+                       if ((fblock_start + fblock_count) != fblock)
                                break;
-               }
 
-               sblock++;
-               fblock_cnt = 1;
+                       fblock_count++;
+               }
 
-               r = ext4_blocks_set_direct(f->mp->fs.bdev, u8_buf, fblock,
-                                          fblock_cnt);
+               r = ext4_blocks_set_direct(f->mp->fs.bdev, u8_buf, fblock_start,
+                                          fblock_count);
                if (r != EOK)
                        break;
 
-               size -= block_size * fblock_cnt;
-               u8_buf += block_size * fblock_cnt;
-               f->fpos += block_size * fblock_cnt;
+               size -= block_size * fblock_count;
+               u8_buf += block_size * fblock_count;
+               f->fpos += block_size * fblock_count;
 
                if (wcnt)
-                       *wcnt += block_size * fblock_cnt;
+                       *wcnt += block_size * fblock_count;
 
+               fblock_start = fblock;
+               fblock_count = 1;
+
+               if (rr != EOK) {
+                       /*ext4_fs_append_inode_block has failed and no
+                        * more blocks might be written. But node size
+                        * should be updated.*/
+                       r = rr;
+                       goto out_fsize;
+               }
        }
 
        /*Stop write back cache mode*/
@@ -1523,15 +1596,16 @@ int ext4_fwrite(ext4_file *f, const void *buf, size_t size, size_t *wcnt)
                goto Finish;
 
        if (size) {
-               if (sblock < file_blocks) {
-                       r = ext4_fs_init_inode_data_block_index(&ref, sblock,
+               if (iblock_idx < ifile_blocks) {
+                       r = ext4_fs_init_inode_data_block_index(&ref, iblock_idx,
                                                               &fblock);
                        if (r != EOK)
                                goto Finish;
                } else {
-                       r = ext4_fs_append_inode_block(&ref, &fblock, &sblock);
+                       r = ext4_fs_append_inode_block(&ref, &fblock, &iblock_idx);
                        if (r != EOK)
-                               goto Finish;
+                               /*Node size sholud be updated.*/
+                               goto out_fsize;
                }
 
                r = ext4_block_get(f->mp->fs.bdev, &b, fblock);
@@ -1551,6 +1625,7 @@ int ext4_fwrite(ext4_file *f, const void *buf, size_t size, size_t *wcnt)
                        *wcnt += size;
        }
 
+out_fsize:
        if (f->fpos > f->fsize) {
                f->fsize = f->fpos;
                ext4_inode_set_size(ref.inode, f->fsize);
@@ -1802,9 +1877,8 @@ static int ext4_fsymlink_set(ext4_file *f, const void *buf, uint32_t size)
 
        /*If the size of symlink is smaller than 60 bytes*/
        if (size < sizeof(ref.inode->blocks)) {
-               char *content = (char *)ref.inode->blocks;
-               memset(content, 0, sizeof(ref.inode->blocks));
-               memcpy(content, buf, size);
+               memset(ref.inode->blocks, 0, sizeof(ref.inode->blocks));
+               memcpy(ref.inode->blocks, buf, size);
                ext4_inode_clear_flag(ref.inode, EXT4_INODE_FLAG_EXTENTS);
        } else {
                ext4_fs_inode_blocks_init(&f->mp->fs, &ref);
@@ -1882,8 +1956,6 @@ int ext4_readlink(const char *path, char *buf, size_t bufsize, size_t *rcnt)
        if (!buf)
                return EINVAL;
 
-       memset(buf, 0, sizeof(bufsize));
-
        filetype = EXT4_DIRENTRY_SYMLINK;
 
        EXT4_MP_LOCK(mp);
@@ -2156,7 +2228,7 @@ int ext4_dir_rm(const char *path)
        struct ext4_mountpoint *mp = ext4_get_mount(path);
        struct ext4_inode_ref current;
        struct ext4_inode_ref child;
-       struct ext4_directory_iterator it;
+       struct ext4_dir_iterator it;
 
        uint32_t name_off;
        uint32_t inode_up;
@@ -2407,7 +2479,7 @@ const ext4_direntry *ext4_dir_entry_next(ext4_dir *d)
        int r;
        ext4_direntry *de = 0;
        struct ext4_inode_ref dir;
-       struct ext4_directory_iterator it;
+       struct ext4_dir_iterator it;
 
        EXT4_MP_LOCK(d->f.mp);