diff options
| author | gkostka <kostka.grzegorz@gmail.com> | 2013-10-07 20:35:29 +0000 |
|---|---|---|
| committer | gkostka <kostka.grzegorz@gmail.com> | 2013-10-07 20:35:29 +0000 |
| commit | aa0d32fd7de1c9edeb520cf41046f87041dbf6e0 (patch) | |
| tree | 9e2da3839fa05dc40fe168931d8bae8610683709 /src | |
| parent | 51ae85960cff4a6aef67694a82379298cffe4863 (diff) | |
Code format.
Diffstat (limited to 'src')
| -rw-r--r-- | src/ext4.h | 2 | ||||
| -rw-r--r-- | src/lwext4/ext4.c | 1617 | ||||
| -rw-r--r-- | src/lwext4/ext4_balloc.c | 932 | ||||
| -rw-r--r-- | src/lwext4/ext4_balloc.h | 15 | ||||
| -rw-r--r-- | src/lwext4/ext4_bcache.c | 292 | ||||
| -rw-r--r-- | src/lwext4/ext4_bcache.h | 82 | ||||
| -rw-r--r-- | src/lwext4/ext4_bitmap.c | 164 | ||||
| -rw-r--r-- | src/lwext4/ext4_bitmap.h | 8 | ||||
| -rw-r--r-- | src/lwext4/ext4_block_group.c | 4 | ||||
| -rw-r--r-- | src/lwext4/ext4_block_group.h | 96 | ||||
| -rw-r--r-- | src/lwext4/ext4_blockdev.c | 450 | ||||
| -rw-r--r-- | src/lwext4/ext4_blockdev.h | 88 | ||||
| -rw-r--r-- | src/lwext4/ext4_debug.c | 4 | ||||
| -rw-r--r-- | src/lwext4/ext4_debug.h | 12 | ||||
| -rw-r--r-- | src/lwext4/ext4_dir.c | 888 | ||||
| -rw-r--r-- | src/lwext4/ext4_dir.h | 24 | ||||
| -rw-r--r-- | src/lwext4/ext4_dir_idx.c | 1638 | ||||
| -rw-r--r-- | src/lwext4/ext4_dir_idx.h | 32 | ||||
| -rw-r--r-- | src/lwext4/ext4_fs.c | 1876 | ||||
| -rw-r--r-- | src/lwext4/ext4_fs.h | 10 | ||||
| -rw-r--r-- | src/lwext4/ext4_hash.c | 378 | ||||
| -rw-r--r-- | src/lwext4/ext4_hash.h | 4 | ||||
| -rw-r--r-- | src/lwext4/ext4_ialloc.c | 344 | ||||
| -rw-r--r-- | src/lwext4/ext4_inode.c | 216 | ||||
| -rw-r--r-- | src/lwext4/ext4_super.c | 86 | ||||
| -rw-r--r-- | src/lwext4/ext4_super.h | 24 | ||||
| -rw-r--r-- | src/lwext4/ext4_types.h | 526 |
27 files changed, 4914 insertions, 4898 deletions
@@ -57,7 +57,7 @@ #endif
#ifndef O_CREAT
-#define O_CREAT 0100
+#define O_CREAT 0100
#endif
#ifndef O_EXCL
diff --git a/src/lwext4/ext4.c b/src/lwext4/ext4.c index 010a327..93f7977 100644 --- a/src/lwext4/ext4.c +++ b/src/lwext4/ext4.c @@ -51,38 +51,40 @@ #include "ext4.h"
/**@brief Mount point OS dependent lock*/
-#define EXT4_MP_LOCK(_m) do { (_m)->os_locks ? (_m)->os_locks->lock() : 0; }while(0)
+#define EXT4_MP_LOCK(_m) \
+ do { (_m)->os_locks ? (_m)->os_locks->lock() : 0; }while(0)
/**@brief Mount point OS dependent unlock*/
-#define EXT4_MP_UNLOCK(_m) do { (_m)->os_locks ? (_m)->os_locks->unlock() : 0; }while(0)
+#define EXT4_MP_UNLOCK(_m) \
+ do { (_m)->os_locks ? (_m)->os_locks->unlock() : 0; }while(0)
/**@brief Mount point descrpitor.*/
struct ext4_mountpoint {
- /**@brief Mount point name (@ref ext4_mount)*/
- const char *name;
+ /**@brief Mount point name (@ref ext4_mount)*/
+ const char *name;
- /**@brief Os dependent lock/unlock functions.*/
- struct ext4_lock *os_locks;
+ /**@brief Os dependent lock/unlock functions.*/
+ struct ext4_lock *os_locks;
- /**@brief Ext4 filesystem internals.*/
- struct ext4_fs fs;
+ /**@brief Ext4 filesystem internals.*/
+ struct ext4_fs fs;
- /**@brief Dynamic alocation cache flag.*/
- bool cache_dynamic;
+ /**@brief Dynamic alocation cache flag.*/
+ bool cache_dynamic;
};
/**@brief Block devices descriptor.*/
struct _ext4_devices {
- /**@brief Block device name (@ref ext4_device_register)*/
- const char *name;
+ /**@brief Block device name (@ref ext4_device_register)*/
+ const char *name;
- /**@brief Block device handle.*/
- struct ext4_blockdev *bd;
+ /**@brief Block device handle.*/
+ struct ext4_blockdev *bd;
- /**@brief Block cache handle.*/
- struct ext4_bcache *bc;
+ /**@brief Block cache handle.*/
+ struct ext4_bcache *bc;
};
/**@brief Block devices.*/
@@ -94,1080 +96,1085 @@ struct ext4_mountpoint _mp[CONFIG_EXT4_MOUNTPOINTS_COUNT]; -int ext4_device_register(struct ext4_blockdev *bd, struct ext4_bcache *bc, const char *dev_name)
+int ext4_device_register(struct ext4_blockdev *bd, struct ext4_bcache *bc,
+ const char *dev_name)
{
- uint32_t i;
- ext4_assert(bd && dev_name);
-
- for (i = 0; i < CONFIG_EXT4_BLOCKDEVS_COUNT; ++i) {
- if(!_bdevices[i].name){
- _bdevices[i].name = dev_name;
- _bdevices[i].bd = bd;
- _bdevices[i].bc = bc;
- return EOK;
- }
- }
- return ENOSPC;
+ uint32_t i;
+ ext4_assert(bd && dev_name);
+
+ for (i = 0; i < CONFIG_EXT4_BLOCKDEVS_COUNT; ++i) {
+ if(!_bdevices[i].name){
+ _bdevices[i].name = dev_name;
+ _bdevices[i].bd = bd;
+ _bdevices[i].bc = bc;
+ return EOK;
+ }
+ }
+ return ENOSPC;
}
-/***********************************************************************************/
+/******************************************************************************/
static bool ext4_is_dots(const uint8_t *name, size_t name_size)
{
- if ((name_size == 1) && (name[0] == '.'))
- return true;
+ if ((name_size == 1) && (name[0] == '.'))
+ return true;
- if ((name_size == 2) && (name[0] == '.') && (name[1] == '.'))
- return true;
+ if ((name_size == 2) && (name[0] == '.') && (name[1] == '.'))
+ return true;
- return false;
+ return false;
}
static int ext4_has_children(bool *has_children, struct ext4_inode_ref *enode)
{
- struct ext4_fs *fs = enode->fs;
-
- /* Check if node is directory */
- if (!ext4_inode_is_type(&fs->sb, enode->inode,
- EXT4_INODE_MODE_DIRECTORY)) {
- *has_children = false;
- return EOK;
- }
-
- struct ext4_directory_iterator it;
- int rc = ext4_dir_iterator_init(&it, enode, 0);
- if (rc != EOK)
- return rc;
-
- /* Find a non-empty directory entry */
- bool found = false;
- while (it.current != NULL) {
- if (it.current->inode != 0) {
- uint16_t name_size =
- ext4_dir_entry_ll_get_name_length(&fs->sb,
- it.current);
- if (!ext4_is_dots(it.current->name, name_size)) {
- found = true;
- break;
- }
- }
-
- rc = ext4_dir_iterator_next(&it);
- if (rc != EOK) {
- ext4_dir_iterator_fini(&it);
- return rc;
- }
- }
-
- rc = ext4_dir_iterator_fini(&it);
- if (rc != EOK)
- return rc;
-
- *has_children = found;
-
- return EOK;
+ struct ext4_fs *fs = enode->fs;
+
+ /* Check if node is directory */
+ if (!ext4_inode_is_type(&fs->sb, enode->inode,
+ EXT4_INODE_MODE_DIRECTORY)) {
+ *has_children = false;
+ return EOK;
+ }
+
+ struct ext4_directory_iterator it;
+ int rc = ext4_dir_iterator_init(&it, enode, 0);
+ if (rc != EOK)
+ return rc;
+
+ /* Find a non-empty directory entry */
+ bool found = false;
+ while (it.current != NULL) {
+ if (it.current->inode != 0) {
+ uint16_t name_size =
+ ext4_dir_entry_ll_get_name_length(&fs->sb,
+ it.current);
+ if (!ext4_is_dots(it.current->name, name_size)) {
+ found = true;
+ break;
+ }
+ }
+
+ rc = ext4_dir_iterator_next(&it);
+ if (rc != EOK) {
+ ext4_dir_iterator_fini(&it);
+ return rc;
+ }
+ }
+
+ rc = ext4_dir_iterator_fini(&it);
+ if (rc != EOK)
+ return rc;
+
+ *has_children = found;
+
+ return EOK;
}
-static int ext4_link(struct ext4_mountpoint *mp, struct ext4_inode_ref *parent, struct ext4_inode_ref *child, const char *name)
+static int ext4_link(struct ext4_mountpoint *mp, struct ext4_inode_ref *parent,
+ struct ext4_inode_ref *child, const char *name)
{
- /* Check maximum name length */
- if (strlen(name) > EXT4_DIRECTORY_FILENAME_LEN)
- return EINVAL;
-
- /* Add entry to parent directory */
- int rc = ext4_dir_add_entry(parent, name,
- child);
- if (rc != EOK)
- return rc;
-
- /* Fill new dir -> add '.' and '..' entries */
- if (ext4_inode_is_type(&mp->fs.sb, child->inode,
- EXT4_INODE_MODE_DIRECTORY)) {
- rc = ext4_dir_add_entry(child, ".",
- child);
- if (rc != EOK) {
- ext4_dir_remove_entry(parent, name);
- return rc;
- }
-
- rc = ext4_dir_add_entry(child, "..",
- parent);
- if (rc != EOK) {
- ext4_dir_remove_entry(parent, name);
- ext4_dir_remove_entry(child, ".");
- return rc;
- }
+ /* Check maximum name length */
+ if (strlen(name) > EXT4_DIRECTORY_FILENAME_LEN)
+ return EINVAL;
+
+ /* Add entry to parent directory */
+ int rc = ext4_dir_add_entry(parent, name,
+ child);
+ if (rc != EOK)
+ return rc;
+
+ /* Fill new dir -> add '.' and '..' entries */
+ if (ext4_inode_is_type(&mp->fs.sb, child->inode,
+ EXT4_INODE_MODE_DIRECTORY)) {
+ rc = ext4_dir_add_entry(child, ".",
+ child);
+ if (rc != EOK) {
+ ext4_dir_remove_entry(parent, name);
+ return rc;
+ }
+
+ rc = ext4_dir_add_entry(child, "..",
+ parent);
+ if (rc != EOK) {
+ ext4_dir_remove_entry(parent, name);
+ ext4_dir_remove_entry(child, ".");
+ return rc;
+ }
#if CONFIG_DIR_INDEX_ENABLE
- /* Initialize directory index if supported */
- if (ext4_sb_check_feature_compatible(&mp->fs.sb,
- EXT4_FEATURE_COMPAT_DIR_INDEX)) {
- rc = ext4_dir_dx_init(child);
- if (rc != EOK)
- return rc;
-
- ext4_inode_set_flag(child->inode,
- EXT4_INODE_FLAG_INDEX);
- child->dirty = true;
- }
+ /* Initialize directory index if supported */
+ if (ext4_sb_check_feature_compatible(&mp->fs.sb,
+ EXT4_FEATURE_COMPAT_DIR_INDEX)) {
+ rc = ext4_dir_dx_init(child);
+ if (rc != EOK)
+ return rc;
+
+ ext4_inode_set_flag(child->inode,
+ EXT4_INODE_FLAG_INDEX);
+ child->dirty = true;
+ }
#endif
- uint16_t parent_links =
- ext4_inode_get_links_count(parent->inode);
- parent_links++;
- ext4_inode_set_links_count(parent->inode, parent_links);
+ uint16_t parent_links =
+ ext4_inode_get_links_count(parent->inode);
+ parent_links++;
+ ext4_inode_set_links_count(parent->inode, parent_links);
- parent->dirty = true;
- }
+ parent->dirty = true;
+ }
- uint16_t child_links =
- ext4_inode_get_links_count(child->inode);
- child_links++;
- ext4_inode_set_links_count(child->inode, child_links);
+ uint16_t child_links =
+ ext4_inode_get_links_count(child->inode);
+ child_links++;
+ ext4_inode_set_links_count(child->inode, child_links);
- child->dirty = true;
+ child->dirty = true;
- return EOK;
+ return EOK;
}
-static int ext4_unlink(struct ext4_mountpoint *mp, struct ext4_inode_ref *parent, struct ext4_inode_ref *child_inode_ref, const char *name)
+static int ext4_unlink(struct ext4_mountpoint *mp, struct ext4_inode_ref *parent,
+ struct ext4_inode_ref *child_inode_ref, const char *name)
{
- bool has_children;
- int rc = ext4_has_children(&has_children, child_inode_ref);
- if (rc != EOK)
- return rc;
+ bool has_children;
+ int rc = ext4_has_children(&has_children, child_inode_ref);
+ if (rc != EOK)
+ return rc;
- /* Cannot unlink non-empty node */
- if (has_children)
- return ENOTSUP;
+ /* Cannot unlink non-empty node */
+ if (has_children)
+ return ENOTSUP;
- /* Remove entry from parent directory */
+ /* Remove entry from parent directory */
- rc = ext4_dir_remove_entry(parent, name);
- if (rc != EOK)
- return rc;
+ rc = ext4_dir_remove_entry(parent, name);
+ if (rc != EOK)
+ return rc;
- uint32_t lnk_count =
- ext4_inode_get_links_count(child_inode_ref->inode);
- lnk_count--;
+ uint32_t lnk_count =
+ ext4_inode_get_links_count(child_inode_ref->inode);
+ lnk_count--;
- bool is_dir = ext4_inode_is_type(&mp->fs.sb, child_inode_ref->inode,
- EXT4_INODE_MODE_DIRECTORY);
+ bool is_dir = ext4_inode_is_type(&mp->fs.sb, child_inode_ref->inode,
+ EXT4_INODE_MODE_DIRECTORY);
- /* If directory - handle links from parent */
- if ((lnk_count <= 1) && (is_dir)) {
- ext4_assert(lnk_count == 1);
+ /* If directory - handle links from parent */
+ if ((lnk_count <= 1) && (is_dir)) {
+ ext4_assert(lnk_count == 1);
- lnk_count--;
+ lnk_count--;
- uint32_t parent_lnk_count = ext4_inode_get_links_count(
- parent->inode);
+ uint32_t parent_lnk_count = ext4_inode_get_links_count(
+ parent->inode);
- parent_lnk_count--;
- ext4_inode_set_links_count(parent->inode, parent_lnk_count);
+ parent_lnk_count--;
+ ext4_inode_set_links_count(parent->inode, parent_lnk_count);
- parent->dirty = true;
- }
+ parent->dirty = true;
+ }
- /*
- * TODO: Update timestamps of the parent
- * (when we have wall-clock time).
- *
- * ext4_inode_set_change_inode_time(parent->inode, (uint32_t) now);
- * ext4_inode_set_modification_time(parent->inode, (uint32_t) now);
- * parent->dirty = true;
- */
+ /*
+ * TODO: Update timestamps of the parent
+ * (when we have wall-clock time).
+ *
+ * ext4_inode_set_change_inode_time(parent->inode, (uint32_t) now);
+ * ext4_inode_set_modification_time(parent->inode, (uint32_t) now);
+ * parent->dirty = true;
+ */
- /*
- * TODO: Update timestamp for inode.
- *
- * ext4_inode_set_change_inode_time(child_inode_ref->inode,
- * (uint32_t) now);
- */
+ /*
+ * TODO: Update timestamp for inode.
+ *
+ * ext4_inode_set_change_inode_time(child_inode_ref->inode,
+ * (uint32_t) now);
+ */
- ext4_inode_set_deletion_time(child_inode_ref->inode, 1);
- ext4_inode_set_links_count(child_inode_ref->inode, lnk_count);
- child_inode_ref->dirty = true;
+ ext4_inode_set_deletion_time(child_inode_ref->inode, 1);
+ ext4_inode_set_links_count(child_inode_ref->inode, lnk_count);
+ child_inode_ref->dirty = true;
- return EOK;
+ return EOK;
}
/*****************************************************************************/
int ext4_mount(const char * dev_name, char *mount_point)
{
- ext4_assert(mount_point && dev_name);
- int r = EOK;
- int i;
-
- uint32_t bsize;
- struct ext4_blockdev *bd = 0;
- struct ext4_bcache *bc = 0;
- struct ext4_mountpoint *mp = 0;
-
- for (i = 0; i < CONFIG_EXT4_BLOCKDEVS_COUNT; ++i) {
- if(_bdevices[i].name){
- if(!strcmp(dev_name, _bdevices[i].name)){
- bd = _bdevices[i].bd;
- bc = _bdevices[i].bc;
- break;
- }
- }
- }
-
- if(!bd)
- return ENODEV;
-
- for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
- if(!_mp[i].name){
- _mp[i].name = mount_point;
- mp = &_mp[i];
- break;
- }
- }
-
- if(!mp)
- return ENOMEM;
-
- r = ext4_block_init(bd);
- if(r != EOK)
- return r;
-
- r = ext4_fs_init(&mp->fs, bd);
- if(r != EOK){
- ext4_block_fini(bd);
- return r;
- }
-
- bsize = ext4_sb_get_block_size(&mp->fs.sb);
- ext4_block_set_lb_size(bd, bsize);
-
-
- mp->cache_dynamic = 0;
-
- if(!bc){
- /*Automatic block cache alloc.*/
- mp->cache_dynamic = 1;
- bc = malloc(sizeof(struct ext4_bcache));
-
- r = ext4_bcache_init_dynamic(bc, 8, bsize);
- if(r != EOK){
- free(bc);
- ext4_block_fini(bd);
- return r;
- }
- }
-
- if(bsize != bc->itemsize)
- return ENOTSUP;
-
-
- /*Bind block cache to block device*/
- r = ext4_block_bind_bcache(bd, bc);
- if(r != EOK){
- ext4_block_fini(bd);
- if(mp->cache_dynamic){
- ext4_bcache_fini_dynamic(bc);
- free(bc);
- }
- return r;
- }
-
- return r;
+ ext4_assert(mount_point && dev_name);
+ int r = EOK;
+ int i;
+
+ uint32_t bsize;
+ struct ext4_blockdev *bd = 0;
+ struct ext4_bcache *bc = 0;
+ struct ext4_mountpoint *mp = 0;
+
+ for (i = 0; i < CONFIG_EXT4_BLOCKDEVS_COUNT; ++i) {
+ if(_bdevices[i].name){
+ if(!strcmp(dev_name, _bdevices[i].name)){
+ bd = _bdevices[i].bd;
+ bc = _bdevices[i].bc;
+ break;
+ }
+ }
+ }
+
+ if(!bd)
+ return ENODEV;
+
+ for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
+ if(!_mp[i].name){
+ _mp[i].name = mount_point;
+ mp = &_mp[i];
+ break;
+ }
+ }
+
+ if(!mp)
+ return ENOMEM;
+
+ r = ext4_block_init(bd);
+ if(r != EOK)
+ return r;
+
+ r = ext4_fs_init(&mp->fs, bd);
+ if(r != EOK){
+ ext4_block_fini(bd);
+ return r;
+ }
+
+ bsize = ext4_sb_get_block_size(&mp->fs.sb);
+ ext4_block_set_lb_size(bd, bsize);
+
+
+ mp->cache_dynamic = 0;
+
+ if(!bc){
+ /*Automatic block cache alloc.*/
+ mp->cache_dynamic = 1;
+ bc = malloc(sizeof(struct ext4_bcache));
+
+ r = ext4_bcache_init_dynamic(bc, 8, bsize);
+ if(r != EOK){
+ free(bc);
+ ext4_block_fini(bd);
+ return r;
+ }
+ }
+
+ if(bsize != bc->itemsize)
+ return ENOTSUP;
+
+
+ /*Bind block cache to block device*/
+ r = ext4_block_bind_bcache(bd, bc);
+ if(r != EOK){
+ ext4_block_fini(bd);
+ if(mp->cache_dynamic){
+ ext4_bcache_fini_dynamic(bc);
+ free(bc);
+ }
+ return r;
+ }
+
+ return r;
}
int ext4_umount(char *mount_point)
{
- int i;
- int r = EOK;
- struct ext4_mountpoint *mp = 0;
+ int i;
+ int r = EOK;
+ struct ext4_mountpoint *mp = 0;
- for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
- if(_mp[i].name){
- if(!strcmp(_mp[i].name, mount_point))
- mp = &_mp[i];
- break;
- }
- }
+ for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
+ if(_mp[i].name){
+ if(!strcmp(_mp[i].name, mount_point))
+ mp = &_mp[i];
+ break;
+ }
+ }
- if(!mp)
- return ENODEV;
+ if(!mp)
+ return ENODEV;
- r = ext4_fs_fini(&mp->fs);
- if(r != EOK)
- return r;
+ r = ext4_fs_fini(&mp->fs);
+ if(r != EOK)
+ return r;
- mp->name = 0;
+ mp->name = 0;
- if(mp->cache_dynamic){
- ext4_bcache_fini_dynamic(mp->fs.bdev->bc);
- free(mp->fs.bdev->bc);
- }
+ if(mp->cache_dynamic){
+ ext4_bcache_fini_dynamic(mp->fs.bdev->bc);
+ free(mp->fs.bdev->bc);
+ }
- return ext4_block_fini(mp->fs.bdev);
+ return ext4_block_fini(mp->fs.bdev);
}
-/********************************FILE OPERATIONS********************************/
+/********************************FILE OPERATIONS******************************/
static struct ext4_mountpoint* ext4_get_mount(const char *path)
{
- int i;
- for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
- if(_mp[i].name){
- if(!strncmp(_mp[i].name, path, strlen(_mp[i].name)))
- return &_mp[i];
- }
- }
- return 0;
+ int i;
+ for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
+ if(_mp[i].name){
+ if(!strncmp(_mp[i].name, path, strlen(_mp[i].name)))
+ return &_mp[i];
+ }
+ }
+ return 0;
}
static int ext4_path_check(const char *path, bool* is_goal)
{
- int i;
+ int i;
- for (i = 0; i < EXT4_DIRECTORY_FILENAME_LEN; ++i) {
+ for (i = 0; i < EXT4_DIRECTORY_FILENAME_LEN; ++i) {
- if(path[i] == '/'){
- *is_goal = false;
- return i;
- }
+ if(path[i] == '/'){
+ *is_goal = false;
+ return i;
+ }
- if(path[i] == 0){
- *is_goal = true;
- return i;
- }
- }
+ if(path[i] == 0){
+ *is_goal = true;
+ return i;
+ }
+ }
- return 0;
+ return 0;
}
static bool ext4_parse_flags(const char *flags, uint32_t *file_flags)
{
- if(!flags)
- return false;
-
- if(!strcmp(flags, "r") || !strcmp(flags, "rb")){
- *file_flags = O_RDONLY;
- return true;
- }
-
- if(!strcmp(flags, "w") || !strcmp(flags, "wb")){
- *file_flags = O_WRONLY | O_CREAT | O_TRUNC;
- return true;
- }
-
- if(!strcmp(flags, "a") || !strcmp(flags, "ab")){
- *file_flags = O_WRONLY | O_CREAT | O_APPEND ;
- return true;
- }
-
- if(!strcmp(flags, "r+") || !strcmp(flags, "rb+") || !strcmp(flags, "r+b")){
- *file_flags = O_RDWR;
- return true;
- }
-
- if(!strcmp(flags, "w+") || !strcmp(flags, "wb+") || !strcmp(flags, "w+b")){
- *file_flags = O_RDWR | O_CREAT | O_TRUNC;
- return true;
- }
-
- if(!strcmp(flags, "a+") || !strcmp(flags, "ab+") || !strcmp(flags, "a+b")){
- *file_flags = O_RDWR | O_CREAT | O_APPEND;
- return true;
- }
-
- return false;
+ if(!flags)
+ return false;
+
+ if(!strcmp(flags, "r") || !strcmp(flags, "rb")){
+ *file_flags = O_RDONLY;
+ return true;
+ }
+
+ if(!strcmp(flags, "w") || !strcmp(flags, "wb")){
+ *file_flags = O_WRONLY | O_CREAT | O_TRUNC;
+ return true;
+ }
+
+ if(!strcmp(flags, "a") || !strcmp(flags, "ab")){
+ *file_flags = O_WRONLY | O_CREAT | O_APPEND ;
+ return true;
+ }
+
+ if(!strcmp(flags, "r+") || !strcmp(flags, "rb+") || !strcmp(flags, "r+b")){
+ *file_flags = O_RDWR;
+ return true;
+ }
+
+ if(!strcmp(flags, "w+") || !strcmp(flags, "wb+") || !strcmp(flags, "w+b")){
+ *file_flags = O_RDWR | O_CREAT | O_TRUNC;
+ return true;
+ }
+
+ if(!strcmp(flags, "a+") || !strcmp(flags, "ab+") || !strcmp(flags, "a+b")){
+ *file_flags = O_RDWR | O_CREAT | O_APPEND;
+ return true;
+ }
+
+ return false;
}
/*****************************************************************************/
-static int ext4_generic_open (ext4_file *f, const char *path, const char *flags, bool file_expect)
+static int ext4_generic_open (ext4_file *f, const char *path,
+ const char *flags, bool file_expect)
{
- struct ext4_mountpoint *mp = ext4_get_mount(path);
- struct ext4_directory_search_result result;
- struct ext4_inode_ref ref;
- char entry_name[255];
- bool is_goal = false;
- uint8_t inode_type;
- int r = ENOENT;
- uint32_t next_inode;
+ struct ext4_mountpoint *mp = ext4_get_mount(path);
+ struct ext4_directory_search_result result;
+ struct ext4_inode_ref ref;
+ char entry_name[255];
+ bool is_goal = false;
+ uint8_t inode_type;
+ int r = ENOENT;
+ uint32_t next_inode;
- f->mp = 0;
+ f->mp = 0;
- if(!mp)
- return ENOENT;
+ if(!mp)
+ return ENOENT;
- if(ext4_parse_flags(flags, &f->flags) == false)
- return EINVAL;
+ if(ext4_parse_flags(flags, &f->flags) == false)
+ return EINVAL;
- /*Skip mount point*/
- path += strlen(mp->name);
+ /*Skip mount point*/
+ path += strlen(mp->name);
- /*Skip first /*/
- if(*path != '/')
- return ENOENT;
+ /*Skip first /*/
+ if(*path != '/')
+ return ENOENT;
- path++;
+ path++;
- EXT4_MP_LOCK(mp);
+ EXT4_MP_LOCK(mp);
- /*Load root*/
- r = ext4_fs_get_inode_ref(&mp->fs, EXT4_INODE_ROOT_INDEX, &ref);
+ /*Load root*/
+ r = ext4_fs_get_inode_ref(&mp->fs, EXT4_INODE_ROOT_INDEX, &ref);
- if(r != EOK)
- return r;
+ if(r != EOK)
+ return r;
- int len = ext4_path_check(path, &is_goal);
+ int len = ext4_path_check(path, &is_goal);
- /*If root open was request.*/
- if(!len && is_goal)
- goto IsGoal;
+ /*If root open was request.*/
+ if(!len && is_goal)
+ goto IsGoal;
- while(1){
+ while(1){
- len = ext4_path_check(path, &is_goal);
+ len = ext4_path_check(path, &is_goal);
- if(!len){
- r = ENOENT;
- break;
- }
+ if(!len){
+ r = ENOENT;
+ break;
+ }
- memcpy(entry_name, path, len);
- entry_name[len] = 0;
+ memcpy(entry_name, path, len);
+ entry_name[len] = 0;
- r = ext4_dir_find_entry(&result, &ref, entry_name);
- if(r != EOK){
+ r = ext4_dir_find_entry(&result, &ref, entry_name);
+ if(r != EOK){
- if(r != ENOENT)
- break;
+ if(r != ENOENT)
+ break;
- if(!(f->flags & O_CREAT))
- break;
+ if(!(f->flags & O_CREAT))
+ break;
- /*O_CREAT allows to create new entry*/
- struct ext4_inode_ref child_ref;
- r = ext4_fs_alloc_inode(&mp->fs, &child_ref, !is_goal);
- if(r != EOK)
- break;
+ /*O_CREAT allows to create new entry*/
+ struct ext4_inode_ref child_ref;
+ r = ext4_fs_alloc_inode(&mp->fs, &child_ref, !is_goal);
+ if(r != EOK)
+ break;
- /*Destroy last result*/
- ext4_dir_destroy_result(&ref, &result);
+ /*Destroy last result*/
+ ext4_dir_destroy_result(&ref, &result);
- /*Link with root dir.*/
- r = ext4_link(mp, &ref, &child_ref, entry_name);
- if(r != EOK){
- /*Fali. Free new inode.*/
- ext4_fs_free_inode(&child_ref);
- /*We do not want to write new inode. But block has to be released.*/
- child_ref.dirty = false;
- ext4_fs_put_inode_ref(&child_ref);
- break;
- }
+ /*Link with root dir.*/
+ r = ext4_link(mp, &ref, &child_ref, entry_name);
+ if(r != EOK){
+ /*Fali. Free new inode.*/
+ ext4_fs_free_inode(&child_ref);
+ /*We do not want to write new inode. But block has to be released.*/
+ child_ref.dirty = false;
+ ext4_fs_put_inode_ref(&child_ref);
+ break;
+ }
- ext4_fs_put_inode_ref(&child_ref);
+ ext4_fs_put_inode_ref(&child_ref);
- continue;
- }
+ continue;
+ }
- next_inode = result.dentry->inode;
- inode_type = ext4_dir_entry_ll_get_inode_type(&mp->fs.sb, result.dentry);
+ next_inode = result.dentry->inode;
+ inode_type = ext4_dir_entry_ll_get_inode_type(&mp->fs.sb, result.dentry);
- r = ext4_dir_destroy_result(&ref, &result);
- if(r != EOK)
- break;
+ r = ext4_dir_destroy_result(&ref, &result);
+ if(r != EOK)
+ break;
- /*If expected file error*/
- if((inode_type == EXT4_DIRECTORY_FILETYPE_REG_FILE) && !file_expect && is_goal){
- r = ENOENT;
- break;
- }
+ /*If expected file error*/
+ if((inode_type == EXT4_DIRECTORY_FILETYPE_REG_FILE) && !file_expect && is_goal){
+ r = ENOENT;
+ break;
+ }
- /*If expected directory error*/
- if((inode_type == EXT4_DIRECTORY_FILETYPE_DIR) && file_expect && is_goal){
- r = ENOENT;
- break;
- }
+ /*If expected directory error*/
+ if((inode_type == EXT4_DIRECTORY_FILETYPE_DIR) && file_expect && is_goal){
+ r = ENOENT;
+ break;
+ }
- r = ext4_fs_put_inode_ref(&ref);
- if(r != EOK)
- break;
+ r = ext4_fs_put_inode_ref(&ref);
+ if(r != EOK)
+ break;
- r = ext4_fs_get_inode_ref(&mp->fs, next_inode, &ref);
- if(r != EOK)
- break;
+ r = ext4_fs_get_inode_ref(&mp->fs, next_inode, &ref);
+ if(r != EOK)
+ break;
- if(is_goal)
- break;
+ if(is_goal)
+ break;
- path += len + 1;
- };
+ path += len + 1;
+ };
- if(r != EOK){
- ext4_fs_put_inode_ref(&ref);
- EXT4_MP_UNLOCK(mp);
- return r;
- }
+ if(r != EOK){
+ ext4_fs_put_inode_ref(&ref);
+ EXT4_MP_UNLOCK(mp);
+ return r;
+ }
- IsGoal:
- if(is_goal){
+ IsGoal:
+ if(is_goal){
- if(f->flags & O_TRUNC){
- /*Turncate.*/
- ext4_block_delay_cache_flush(mp->fs.bdev, 1);
- /*Truncate may be IO heavy. Do it with delayed cache flush mode.*/
- r = ext4_fs_truncate_inode(&ref, 0);
- ext4_block_delay_cache_flush(mp->fs.bdev, 0);
-
- if(r != EOK){
- ext4_fs_put_inode_ref(&ref);
- EXT4_MP_UNLOCK(mp);
- return r;
- }
- }
-
- f->mp = mp;
- f->fsize = ext4_inode_get_size(&f->mp->fs.sb, ref.inode);
- f->inode = ref.index;
- f->fpos = 0;
-
- if(f->flags & O_APPEND)
- f->fpos = f->fsize;
-
- }
-
- r = ext4_fs_put_inode_ref(&ref);
- EXT4_MP_UNLOCK(mp);
- return r;
+ if(f->flags & O_TRUNC){
+ /*Turncate.*/
+ ext4_block_delay_cache_flush(mp->fs.bdev, 1);
+ /*Truncate may be IO heavy. Do it with delayed cache flush mode.*/
+ r = ext4_fs_truncate_inode(&ref, 0);
+ ext4_block_delay_cache_flush(mp->fs.bdev, 0);
+
+ if(r != EOK){
+ ext4_fs_put_inode_ref(&ref);
+ EXT4_MP_UNLOCK(mp);
+ return r;
+ }
+ }
+
+ f->mp = mp;
+ f->fsize = ext4_inode_get_size(&f->mp->fs.sb, ref.inode);
+ f->inode = ref.index;
+ f->fpos = 0;
+
+ if(f->flags & O_APPEND)
+ f->fpos = f->fsize;
+
+ }
+
+ r = ext4_fs_put_inode_ref(&ref);
+ EXT4_MP_UNLOCK(mp);
+ return r;
}
/*****************************************************************************/
-int ext4_fremove(const char *path)
+int ext4_fremove(const char *path)
{
- struct ext4_mountpoint *mp = ext4_get_mount(path);
- struct ext4_directory_search_result result;
- char entry_name[255];
- bool is_goal = false;
- uint8_t inode_type;
- int r = ENOENT;
- uint32_t next_inode;
+ struct ext4_mountpoint *mp = ext4_get_mount(path);
+ struct ext4_directory_search_result result;
+ char entry_name[255];
+ bool is_goal = false;
+ uint8_t inode_type;
+ int r = ENOENT;
+ uint32_t next_inode;
- struct ext4_inode_ref parent;
- struct ext4_inode_ref child;
+ struct ext4_inode_ref parent;
+ struct ext4_inode_ref child;
- if(!mp)
- return ENOENT;
+ if(!mp)
+ return ENOENT;
- /*Skip mount point*/
- path += strlen(mp->name);
+ /*Skip mount point*/
+ path += strlen(mp->name);
- /*Skip first /*/
- if(*path != '/')
- return ENOENT;
+ /*Skip first /*/
+ if(*path != '/')
+ return ENOENT;
- path++;
+ path++;
- /*Lock mountpoint*/
- EXT4_MP_LOCK(mp);
+ /*Lock mountpoint*/
+ EXT4_MP_LOCK(mp);
- /*Load root*/
- r = ext4_fs_get_inode_ref(&mp->fs, EXT4_INODE_ROOT_INDEX, &parent);
+ /*Load root*/
+ r = ext4_fs_get_inode_ref(&mp->fs, EXT4_INODE_ROOT_INDEX, &parent);
- if(r != EOK){
- EXT4_MP_UNLOCK(mp);
- return r;
- }
+ if(r != EOK){
+ EXT4_MP_UNLOCK(mp);
+ return r;
+ }
- while(1){
+ while(1){
- int len = ext4_path_check(path, &is_goal);
+ int len = ext4_path_check(path, &is_goal);
- if(!len){
- r = ENOENT;
- break;
- }
+ if(!len){
+ r = ENOENT;
+ break;
+ }
- memcpy(entry_name, path, len);
- entry_name[len] = 0;
+ memcpy(entry_name, path, len);
+ entry_name[len] = 0;
- r = ext4_dir_find_entry(&result, &parent, entry_name);
- if(r != EOK){
- ext4_dir_destroy_result(&parent, &result);
- break;
- }
+ r = ext4_dir_find_entry(&result, &parent, entry_name);
+ if(r != EOK){
+ ext4_dir_destroy_result(&parent, &result);
+ break;
+ }
- next_inode = result.dentry->inode;
- inode_type = ext4_dir_entry_ll_get_inode_type(&mp->fs.sb, result.dentry);
+ next_inode = result.dentry->inode;
+ inode_type = ext4_dir_entry_ll_get_inode_type(&mp->fs.sb,
+ result.dentry);
- r = ext4_dir_destroy_result(&parent, &result);
- if(r != EOK)
- break;
+ r = ext4_dir_destroy_result(&parent, &result);
+ if(r != EOK)
+ break;
- /*If expected file error*/
- if((inode_type == EXT4_DIRECTORY_FILETYPE_REG_FILE) && !is_goal){
- r = ENOENT;
- break;
- }
+ /*If expected file error*/
+ if((inode_type == EXT4_DIRECTORY_FILETYPE_REG_FILE) && !is_goal){
+ r = ENOENT;
+ break;
+ }
- /*If expected directory error*/
- if((inode_type == EXT4_DIRECTORY_FILETYPE_DIR) && is_goal){
- r = ENOENT;
- break;
- }
+ /*If expected directory error*/
+ if((inode_type == EXT4_DIRECTORY_FILETYPE_DIR) && is_goal){
+ r = ENOENT;
+ break;
+ }
- if(is_goal)
- break;
+ if(is_goal)
+ break;
- r = ext4_fs_put_inode_ref(&parent);
- if(r != EOK)
- break;
+ r = ext4_fs_put_inode_ref(&parent);
+ if(r != EOK)
+ break;
- r = ext4_fs_get_inode_ref(&mp->fs, next_inode, &parent);
- if(r != EOK)
- break;
+ r = ext4_fs_get_inode_ref(&mp->fs, next_inode, &parent);
+ if(r != EOK)
+ break;
- path += len + 1;
- };
+ path += len + 1;
+ };
- if(r != EOK){
- /*No entry or other error.*/
- ext4_fs_put_inode_ref(&parent);
- EXT4_MP_UNLOCK(mp);
- return r;
- }
+ if(r != EOK){
+ /*No entry or other error.*/
+ ext4_fs_put_inode_ref(&parent);
+ EXT4_MP_UNLOCK(mp);
+ return r;
+ }
- /*We have file to delete. Load it.*/
- r = ext4_fs_get_inode_ref(&mp->fs, next_inode, &child);
- if(r != EOK){
- /*Parent free*/
- ext4_fs_put_inode_ref(&parent);
- EXT4_MP_UNLOCK(mp);
- return r;
- }
+ /*We have file to delete. Load it.*/
+ r = ext4_fs_get_inode_ref(&mp->fs, next_inode, &child);
+ if(r != EOK){
+ /*Parent free*/
+ ext4_fs_put_inode_ref(&parent);
+ EXT4_MP_UNLOCK(mp);
+ return r;
+ }
- /*Turncate.*/
- ext4_block_delay_cache_flush(mp->fs.bdev, 1);
+ /*Turncate.*/
+ ext4_block_delay_cache_flush(mp->fs.bdev, 1);
- /*Truncate may be IO heavy. Do it with delayed cache flush mode.*/
- r = ext4_fs_truncate_inode(&child, 0);
+ /*Truncate may be IO heavy. Do it with delayed cache flush mode.*/
+ r = ext4_fs_truncate_inode(&child, 0);
- ext4_block_delay_cache_flush(mp->fs.bdev, 0);
- if(r != EOK)
- goto Finish;
+ ext4_block_delay_cache_flush(mp->fs.bdev, 0);
+ if(r != EOK)
+ goto Finish;
- /*Unlink from parent.*/
- r = ext4_unlink(mp, &parent, &child, entry_name);
- if(r != EOK)
- goto Finish;
+ /*Unlink from parent.*/
+ r = ext4_unlink(mp, &parent, &child, entry_name);
+ if(r != EOK)
+ goto Finish;
- r = ext4_fs_free_inode(&child);
- if(r != EOK)
- goto Finish;
+ r = ext4_fs_free_inode(&child);
+ if(r != EOK)
+ goto Finish;
- Finish:
- ext4_fs_put_inode_ref(&child);
- ext4_fs_put_inode_ref(&parent);
- EXT4_MP_UNLOCK(mp);
- return r;
+ Finish:
+ ext4_fs_put_inode_ref(&child);
+ ext4_fs_put_inode_ref(&parent);
+ EXT4_MP_UNLOCK(mp);
+ return r;
}
-int ext4_fopen (ext4_file *f, const char *path, const char *flags)
+int ext4_fopen (ext4_file *f, const char *path, const char *flags)
{
- return ext4_generic_open(f, path, flags, true);
+ return ext4_generic_open(f, path, flags, true);
}
-int ext4_fclose(ext4_file *f)
+int ext4_fclose(ext4_file *f)
{
- ext4_assert(f && f->mp);
+ ext4_assert(f && f->mp);
- f->mp = 0;
- f->flags = 0;
- f->inode = 0;
- f->fpos = f->fsize = 0;
+ f->mp = 0;
+ f->flags = 0;
+ f->inode = 0;
+ f->fpos = f->fsize = 0;
- return EOK;
+ return EOK;
}
-int ext4_fread (ext4_file *f, void *buf, uint32_t size, uint32_t *rcnt)
+int ext4_fread (ext4_file *f, void *buf, uint32_t size, uint32_t *rcnt)
{
- int r = EOK;
- uint32_t u;
- uint32_t fblock;
- struct ext4_block b;
- uint8_t *u8_buf = buf;
- struct ext4_inode_ref ref;
- uint32_t sblock;
- uint32_t block_size;
+ int r = EOK;
+ uint32_t u;
+ uint32_t fblock;
+ struct ext4_block b;
+ uint8_t *u8_buf = buf;
+ struct ext4_inode_ref ref;
+ uint32_t sblock;
+ uint32_t block_size;
- ext4_assert(f && f->mp);
+ ext4_assert(f && f->mp);
- if(f->flags & O_WRONLY)
- return EPERM;
+ if(f->flags & O_WRONLY)
+ return EPERM;
- if(!size)
- return EOK;
+ if(!size)
+ return EOK;
- EXT4_MP_LOCK(f->mp);
+ EXT4_MP_LOCK(f->mp);
- if(rcnt)
- *rcnt = 0;
+ if(rcnt)
+ *rcnt = 0;
- r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref);
- if(r != EOK){
- EXT4_MP_UNLOCK(f->mp);
- return r;
- }
+ r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref);
+ if(r != EOK){
+ EXT4_MP_UNLOCK(f->mp);
+ return r;
+ }
- /*Sync file size*/
- f->fsize = ext4_inode_get_size(&f->mp->fs.sb, ref.inode);
+ /*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);
- size = size > (f->fsize - f->fpos) ? (f->fsize - f->fpos) : size;
+ 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;
+ sblock = (f->fpos) / block_size;
- u = (f->fpos) % block_size;
+ u = (f->fpos) % block_size;
- if(u){
+ if(u){
- uint32_t ll = size > (block_size - u) ? (block_size - u) : size;
+ uint32_t ll = size > (block_size - u) ? (block_size - u) : size;
- r = ext4_fs_get_inode_data_block_index(&ref, sblock, &fblock);
- if(r != EOK)
- goto Finish;
+ r = ext4_fs_get_inode_data_block_index(&ref, sblock, &fblock);
+ if(r != EOK)
+ goto Finish;
- r = ext4_block_get(f->mp->fs.bdev, &b, fblock);
- if(r != EOK)
- goto Finish;
+ r = ext4_block_get(f->mp->fs.bdev, &b, fblock);
+ if(r != EOK)
+ goto Finish;
- memcpy(u8_buf, b.data + u, ll);
+ memcpy(u8_buf, b.data + u, ll);
- r = ext4_block_set(f->mp->fs.bdev, &b);
- if(r != EOK)
- goto Finish;
+ r = ext4_block_set(f->mp->fs.bdev, &b);
+ if(r != EOK)
+ goto Finish;
- u8_buf += ll;
- size -= ll;
- f->fpos += ll;
+ u8_buf += ll;
+ size -= ll;
+ f->fpos += ll;
- if(rcnt)
- *rcnt += ll;
+ if(rcnt)
+ *rcnt += ll;
- sblock++;
- }
+ sblock++;
+ }
- while(size >= block_size){
+ while(size >= block_size){
- r = ext4_fs_get_inode_data_block_index(&ref, sblock, &fblock);
- if(r != EOK)
- goto Finish;
+ r = ext4_fs_get_inode_data_block_index(&ref, sblock, &fblock);
+ if(r != EOK)
+ goto Finish;
- r = ext4_block_get_direct(f->mp->fs.bdev, u8_buf, fblock);
- if(r != EOK)
- goto Finish;
+ r = ext4_block_get_direct(f->mp->fs.bdev, u8_buf, fblock);
+ if(r != EOK)
+ goto Finish;
- u8_buf += block_size;
- size -= block_size;
- f->fpos += block_size;
+ u8_buf += block_size;
+ size -= block_size;
+ f->fpos += block_size;
- if(rcnt)
- *rcnt += block_size;
+ if(rcnt)
+ *rcnt += block_size;
- sblock++;
- }
+ sblock++;
+ }
- if(size){
- r = ext4_fs_get_inode_data_block_index(&ref, sblock, &fblock);
- if(r != EOK)
- goto Finish;
+ if(size){
+ r = ext4_fs_get_inode_data_block_index(&ref, sblock, &fblock);
+ if(r != EOK)
+ goto Finish;
- r = ext4_block_get(f->mp->fs.bdev, &b, fblock);
- if(r != EOK)
- goto Finish;
+ r = ext4_block_get(f->mp->fs.bdev, &b, fblock);
+ if(r != EOK)
+ goto Finish;
- memcpy(u8_buf, b.data , size);
+ memcpy(u8_buf, b.data , size);
- r = ext4_block_set(f->mp->fs.bdev, &b);
- if(r != EOK)
- goto Finish;
+ r = ext4_block_set(f->mp->fs.bdev, &b);
+ if(r != EOK)
+ goto Finish;
- f->fpos += size;
+ f->fpos += size;
- if(rcnt)
- *rcnt += size;
- }
+ if(rcnt)
+ *rcnt += size;
+ }
- Finish:
- ext4_fs_put_inode_ref(&ref);
- EXT4_MP_UNLOCK(f->mp);
- return r;
+ Finish:
+ ext4_fs_put_inode_ref(&ref);
+ EXT4_MP_UNLOCK(f->mp);
+ return r;
}
-int ext4_fwrite(ext4_file *f, void *buf, uint32_t size, uint32_t *wcnt)
+int ext4_fwrite(ext4_file *f, void *buf, uint32_t size, uint32_t *wcnt)
{
- int r = EOK;
- uint32_t u;
- uint32_t fblock;
- struct ext4_block b;
- uint8_t *u8_buf = buf;
- struct ext4_inode_ref ref;
- uint32_t sblock;
- uint32_t file_blocks;
- uint32_t block_size;
+ int r = EOK;
+ uint32_t u;
+ uint32_t fblock;
+ struct ext4_block b;
+ uint8_t *u8_buf = buf;
+ struct ext4_inode_ref ref;
+ uint32_t sblock;
+ uint32_t file_blocks;
+ uint32_t block_size;
- ext4_assert(f && f->mp);
+ ext4_assert(f && f->mp);
- if(f->flags & O_RDONLY)
- return EPERM;
+ if(f->flags & O_RDONLY)
+ return EPERM;
- if(!size)
- return EOK;
+ if(!size)
+ return EOK;
- EXT4_MP_LOCK(f->mp);
+ EXT4_MP_LOCK(f->mp);
- if(wcnt)
- *wcnt = 0;
+ if(wcnt)
+ *wcnt = 0;
- r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref);
- if(r != EOK){
- EXT4_MP_UNLOCK(f->mp);
- return r;
- }
+ r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref);
+ if(r != EOK){
+ EXT4_MP_UNLOCK(f->mp);
+ return r;
+ }
- /*Sync file size*/
- f->fsize = ext4_inode_get_size(&f->mp->fs.sb, ref.inode);
+ /*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);
+ block_size = ext4_sb_get_block_size(&f->mp->fs.sb);
- file_blocks = (f->fsize / block_size);
+ file_blocks = (f->fsize / block_size);
- if(f->fsize % block_size)
- file_blocks++;
+ if(f->fsize % block_size)
+ file_blocks++;
- sblock = (f->fpos) / block_size;
+ sblock = (f->fpos) / block_size;
- u = (f->fpos) % block_size;
+ u = (f->fpos) % block_size;
- if(u){
- uint32_t ll = size > (block_size - u) ? (block_size - u) : size;
+ 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(r != EOK)
- goto Finish;
+ r = ext4_fs_get_inode_data_block_index(&ref, sblock, &fblock);
+ if(r != EOK)
+ goto Finish;
- r = ext4_block_get(f->mp->fs.bdev, &b, fblock);
- if(r != EOK)
- goto Finish;
+ r = ext4_block_get(f->mp->fs.bdev, &b, fblock);
+ if(r != EOK)
+ goto Finish;
- memcpy(b.data + u, u8_buf, ll);
- b.dirty = true;
+ memcpy(b.data + u, u8_buf, ll);
+ b.dirty = true;
- r = ext4_block_set(f->mp->fs.bdev, &b);
- if(r != EOK)
- goto Finish;
+ r = ext4_block_set(f->mp->fs.bdev, &b);
+ if(r != EOK)
+ goto Finish;
- u8_buf += ll;
- size -= ll;
- f->fpos += ll;
+ u8_buf += ll;
+ size -= ll;
+ f->fpos += ll;
- if(wcnt)
- *wcnt += ll;
+ if(wcnt)
+ *wcnt += ll;
- sblock++;
- }
+ sblock++;
+ }
- /*Start delay cache flush mode.*/
- r = ext4_block_delay_cache_flush(f->mp->fs.bdev, 1);
- if(r != EOK)
- goto Finish;
+ /*Start delay cache flush mode.*/
+ r = ext4_block_delay_cache_flush(f->mp->fs.bdev, 1);
+ if(r != EOK)
+ goto Finish;
- while(size >= block_size){
+ while(size >= block_size){
- if(sblock < file_blocks){
- r = ext4_fs_get_inode_data_block_index(&ref, sblock, &fblock);
- if(r != EOK)
- break;
- }
- else {
- r = ext4_fs_append_inode_block(&ref, &fblock, &sblock);
- if(r != EOK)
- break;
- }
+ if(sblock < file_blocks){
+ r = ext4_fs_get_inode_data_block_index(&ref, sblock, &fblock);
+ if(r != EOK)
+ break;
+ }
+ else {
+ r = ext4_fs_append_inode_block(&ref, &fblock, &sblock);
+ if(r != EOK)
+ break;
+ }
- r = ext4_block_set_direct(f->mp->fs.bdev, u8_buf, fblock);
- if(r != EOK)
- break;
+ r = ext4_block_set_direct(f->mp->fs.bdev, u8_buf, fblock);
+ if(r != EOK)
+ break;
- u8_buf += block_size;
- size -= block_size;
- f->fpos += block_size;
+ u8_buf += block_size;
+ size -= block_size;
+ f->fpos += block_size;
- if(wcnt)
- *wcnt += block_size;
+ if(wcnt)
+ *wcnt += block_size;
- sblock++;
- }
+ sblock++;
+ }
- /*Stop delay cache flush mode*/
- ext4_block_delay_cache_flush(f->mp->fs.bdev, 0);
+ /*Stop delay cache flush mode*/
+ ext4_block_delay_cache_flush(f->mp->fs.bdev, 0);
- if(r != EOK)
- goto Finish;
+ if(r != EOK)
+ goto Finish;
- if(size){
- if(sblock < file_blocks){
- r = ext4_fs_get_inode_data_block_index(&ref, sblock, &fblock);
- if(r != EOK)
- goto Finish;
- }
- else {
- r = ext4_fs_append_inode_block(&ref, &fblock, &sblock);
- if(r != EOK)
- goto Finish;
- }
+ if(size){
+ if(sblock < file_blocks){
+ r = ext4_fs_get_inode_data_block_index(&ref, sblock, &fblock);
+ if(r != EOK)
+ goto Finish;
+ }
+ else {
+ r = ext4_fs_append_inode_block(&ref, &fblock, &sblock);
+ if(r != EOK)
+ goto Finish;
+ }
- r = ext4_block_get(f->mp->fs.bdev, &b, fblock);
- if(r != EOK)
- goto Finish;
+ r = ext4_block_get(f->mp->fs.bdev, &b, fblock);
+ if(r != EOK)
+ goto Finish;
- memcpy(b.data, u8_buf , size);
- b.dirty = true;
+ memcpy(b.data, u8_buf , size);
+ b.dirty = true;
- r = ext4_block_set(f->mp->fs.bdev, &b);
- if(r != EOK)
- goto Finish;
+ r = ext4_block_set(f->mp->fs.bdev, &b);
+ if(r != EOK)
+ goto Finish;
- f->fpos += size;
+ f->fpos += size;
- if(wcnt)
- *wcnt += size;
- }
+ if(wcnt)
+ *wcnt += size;
+ }
- if(f->fpos > f->fsize){
- f->fsize = f->fpos;
- ext4_inode_set_size(ref.inode, f->fsize);
- ref.dirty = true;
- }
+ if(f->fpos > f->fsize){
+ f->fsize = f->fpos;
+ ext4_inode_set_size(ref.inode, f->fsize);
+ ref.dirty = true;
+ }
- Finish:
- ext4_fs_put_inode_ref(&ref);
- EXT4_MP_UNLOCK(f->mp);
- return r;
+ Finish:
+ ext4_fs_put_inode_ref(&ref);
+ EXT4_MP_UNLOCK(f->mp);
+ return r;
}
-int ext4_fseek (ext4_file *f, uint64_t offset, uint32_t origin)
+int ext4_fseek (ext4_file *f, uint64_t offset, uint32_t origin)
{
- switch(origin){
- case SEEK_SET:
+ switch(origin){
+ case SEEK_SET:
- if(offset > f->fsize)
- return EINVAL;
+ if(offset > f->fsize)
+ return EINVAL;
- f->fpos = offset;
- return EOK;
- case SEEK_CUR:
- if((offset + f->fpos) > f->fsize)
- return EINVAL;
+ f->fpos = offset;
+ return EOK;
+ case SEEK_CUR:
+ if((offset + f->fpos) > f->fsize)
+ return EINVAL;
- f->fpos += offset;
- return EOK;
- case SEEK_END:
- if(offset > f->fsize)
- return EINVAL;
- f->fpos = f->fsize - offset;
- return EOK;
+ f->fpos += offset;
+ return EOK;
+ case SEEK_END:
+ if(offset > f->fsize)
+ return EINVAL;
+ f->fpos = f->fsize - offset;
+ return EOK;
- }
+ }
- return EINVAL;
+ return EINVAL;
}
-uint64_t ext4_ftell (ext4_file *f)
+uint64_t ext4_ftell (ext4_file *f)
{
- return f->fpos;
+ return f->fpos;
}
-uint64_t ext4_fsize (ext4_file *f)
+uint64_t ext4_fsize (ext4_file *f)
{
- return f->fsize;
+ return f->fsize;
}
/*********************************DIRECTORY OPERATION***************************/
-int ext4_dir_open (ext4_dir *d, const char *path)
+int ext4_dir_open (ext4_dir *d, const char *path)
{
- return ext4_generic_open(&d->f, path, "r", false);
+ return ext4_generic_open(&d->f, path, "r", false);
}
-int ext4_dir_close(ext4_dir *d)
+int ext4_dir_close(ext4_dir *d)
{
- return ext4_fclose(&d->f);
+ return ext4_fclose(&d->f);
}
ext4_direntry* ext4_entry_get(ext4_dir *d, uint32_t id)
{
- int r;
- uint32_t i;
- ext4_direntry *de = 0;
- struct ext4_inode_ref dir;
- struct ext4_directory_iterator it;
+ int r;
+ uint32_t i;
+ ext4_direntry *de = 0;
+ struct ext4_inode_ref dir;
+ struct ext4_directory_iterator it;
- EXT4_MP_LOCK(d->f.mp);
+ EXT4_MP_LOCK(d->f.mp);
- r = ext4_fs_get_inode_ref(&d->f.mp->fs, d->f.inode, &dir);
- if(r != EOK){
- goto Finish;
- }
+ r = ext4_fs_get_inode_ref(&d->f.mp->fs, d->f.inode, &dir);
+ if(r != EOK){
+ goto Finish;
+ }
- r = ext4_dir_iterator_init(&it, &dir, 0);
- if(r != EOK){
- ext4_fs_put_inode_ref(&dir);
- goto Finish;
- }
+ r = ext4_dir_iterator_init(&it, &dir, 0);
+ if(r != EOK){
+ ext4_fs_put_inode_ref(&dir);
+ goto Finish;
+ }
- i = 0;
- while(r == EOK){
+ i = 0;
+ while(r == EOK){
- if(!it.current)
- break;
+ if(!it.current)
+ break;
- if(i == id){
- memcpy(&d->de, it.current, sizeof(ext4_direntry));
- de = &d->de;
- break;
- }
+ if(i == id){
+ memcpy(&d->de, it.current, sizeof(ext4_direntry));
+ de = &d->de;
+ break;
+ }
- i++;
- r = ext4_dir_iterator_next(&it);
- }
+ i++;
+ r = ext4_dir_iterator_next(&it);
+ }
- ext4_dir_iterator_fini(&it);
- ext4_fs_put_inode_ref(&dir);
+ ext4_dir_iterator_fini(&it);
+ ext4_fs_put_inode_ref(&dir);
- Finish:
- EXT4_MP_UNLOCK(d->f.mp);
- return de;
+ Finish:
+ EXT4_MP_UNLOCK(d->f.mp);
+ return de;
}
/**
diff --git a/src/lwext4/ext4_balloc.c b/src/lwext4/ext4_balloc.c index 40118dc..37e9fa9 100644 --- a/src/lwext4/ext4_balloc.c +++ b/src/lwext4/ext4_balloc.c @@ -51,549 +51,553 @@ static uint32_t ext4_balloc_get_bgid_of_block(struct ext4_sblock *s, uint32_t baddr) { - if(ext4_get32(s, first_data_block)) - baddr--; + if(ext4_get32(s, first_data_block)) + baddr--; - return baddr / ext4_get32(s, blocks_per_group); + return baddr / ext4_get32(s, blocks_per_group); } -uint32_t ext4_balloc_get_first_data_block_in_group(struct ext4_sblock *s, struct ext4_block_group_ref * bg_ref) +uint32_t ext4_balloc_get_first_data_block_in_group(struct ext4_sblock *s, + struct ext4_block_group_ref * bg_ref) { - uint32_t block_group_count = ext4_block_group_cnt(s); - uint32_t inode_table_first_block = ext4_bg_get_inode_table_first_block(bg_ref->block_group, s); - uint32_t block_size = ext4_sb_get_block_size(s); + uint32_t block_group_count = ext4_block_group_cnt(s); + uint32_t inode_table_first_block = + ext4_bg_get_inode_table_first_block(bg_ref->block_group, s); + uint32_t block_size = ext4_sb_get_block_size(s); - uint16_t inode_size = ext4_get16(s, inode_size); - uint32_t inodes_per_group = ext4_get32(s, inodes_per_group); + uint16_t inode_size = ext4_get16(s, inode_size); + uint32_t inodes_per_group = ext4_get32(s, inodes_per_group); - uint32_t inode_table_bytes; + uint32_t inode_table_bytes; - if (bg_ref->index < block_group_count - 1) { - inode_table_bytes = inodes_per_group * inode_size; - } else { - /* Last block group could be smaller */ - uint32_t inodes_count_total = ext4_get32(s, inodes_count); - inode_table_bytes = - (inodes_count_total - ((block_group_count - 1) * inodes_per_group)) * - inode_size; - } + if (bg_ref->index < block_group_count - 1) { + inode_table_bytes = inodes_per_group * inode_size; + } else { + /* Last block group could be smaller */ + uint32_t inodes_count_total = ext4_get32(s, inodes_count); + inode_table_bytes = + (inodes_count_total - ((block_group_count - 1) * + inodes_per_group)) * inode_size; + } - uint32_t inode_table_blocks = inode_table_bytes / block_size; + uint32_t inode_table_blocks = inode_table_bytes / block_size; - if (inode_table_bytes % block_size) - inode_table_blocks++; + if (inode_table_bytes % block_size) + inode_table_blocks++; - return inode_table_first_block + inode_table_blocks; + return inode_table_first_block + inode_table_blocks; } -int ext4_balloc_free_block(struct ext4_inode_ref *inode_ref, uint32_t baddr) +int ext4_balloc_free_block(struct ext4_inode_ref *inode_ref, uint32_t baddr) { - struct ext4_fs *fs = inode_ref->fs; - struct ext4_sblock *sb = &fs->sb; - - - uint32_t block_group = ext4_balloc_get_bgid_of_block(sb, baddr); - uint32_t index_in_group = ext4_fs_baddr2_index_in_group(sb, baddr); - - /* Load block group reference */ - struct ext4_block_group_ref bg_ref; - int rc = ext4_fs_get_block_group_ref(fs, block_group, &bg_ref); - if (rc != EOK) - return rc; - - /* Load block with bitmap */ - uint32_t bitmap_block_addr = - ext4_bg_get_block_bitmap(bg_ref.block_group, sb); - - struct ext4_block bitmap_block; - - rc = ext4_block_get(fs->bdev, &bitmap_block, bitmap_block_addr); - if (rc != EOK) - return rc; - - /* Modify bitmap */ - ext4_bmap_bit_clr(bitmap_block.data, index_in_group); - bitmap_block.dirty = true; - - /* Release block with bitmap */ - rc = ext4_block_set(fs->bdev, &bitmap_block); - if (rc != EOK) { - /* Error in saving bitmap */ - ext4_fs_put_block_group_ref(&bg_ref); - return rc; - } - - uint32_t block_size = ext4_sb_get_block_size(sb); - - /* Update superblock free blocks count */ - uint64_t sb_free_blocks = - ext4_sb_get_free_blocks_cnt(sb); - sb_free_blocks++; - ext4_sb_set_free_blocks_cnt(sb, sb_free_blocks); - - /* Update inode blocks count */ - uint64_t ino_blocks = - ext4_inode_get_blocks_count(sb, inode_ref->inode); - ino_blocks -= block_size / EXT4_INODE_BLOCK_SIZE; - ext4_inode_set_blocks_count(sb, inode_ref->inode, ino_blocks); - inode_ref->dirty = true; - - /* Update block group free blocks count */ - uint32_t free_blocks = - ext4_bg_get_free_blocks_count(bg_ref.block_group, sb); - free_blocks++; - ext4_bg_set_free_blocks_count(bg_ref.block_group, - sb, free_blocks); - - bg_ref.dirty = true; - - /* Release block group reference */ - rc = ext4_fs_put_block_group_ref(&bg_ref); - if (rc != EOK) - return rc; - - return EOK; + struct ext4_fs *fs = inode_ref->fs; + struct ext4_sblock *sb = &fs->sb; + + + uint32_t block_group = ext4_balloc_get_bgid_of_block(sb, baddr); + uint32_t index_in_group = ext4_fs_baddr2_index_in_group(sb, baddr); + + /* Load block group reference */ + struct ext4_block_group_ref bg_ref; + int rc = ext4_fs_get_block_group_ref(fs, block_group, &bg_ref); + if (rc != EOK) + return rc; + + /* Load block with bitmap */ + uint32_t bitmap_block_addr = + ext4_bg_get_block_bitmap(bg_ref.block_group, sb); + + struct ext4_block bitmap_block; + + rc = ext4_block_get(fs->bdev, &bitmap_block, bitmap_block_addr); + if (rc != EOK) + return rc; + + /* Modify bitmap */ + ext4_bmap_bit_clr(bitmap_block.data, index_in_group); + bitmap_block.dirty = true; + + /* Release block with bitmap */ + rc = ext4_block_set(fs->bdev, &bitmap_block); + if (rc != EOK) { + /* Error in saving bitmap */ + ext4_fs_put_block_group_ref(&bg_ref); + return rc; + } + + uint32_t block_size = ext4_sb_get_block_size(sb); + + /* Update superblock free blocks count */ + uint64_t sb_free_blocks = + ext4_sb_get_free_blocks_cnt(sb); + sb_free_blocks++; + ext4_sb_set_free_blocks_cnt(sb, sb_free_blocks); + + /* Update inode blocks count */ + uint64_t ino_blocks = + ext4_inode_get_blocks_count(sb, inode_ref->inode); + ino_blocks -= block_size / EXT4_INODE_BLOCK_SIZE; + ext4_inode_set_blocks_count(sb, inode_ref->inode, ino_blocks); + inode_ref->dirty = true; + + /* Update block group free blocks count */ + uint32_t free_blocks = + ext4_bg_get_free_blocks_count(bg_ref.block_group, sb); + free_blocks++; + ext4_bg_set_free_blocks_count(bg_ref.block_group, + sb, free_blocks); + + bg_ref.dirty = true; + + /* Release block group reference */ + rc = ext4_fs_put_block_group_ref(&bg_ref); + if (rc != EOK) + return rc; + + return EOK; } -int ext4_balloc_free_blocks(struct ext4_inode_ref *inode_ref, uint32_t first, uint32_t count) +int ext4_balloc_free_blocks(struct ext4_inode_ref *inode_ref, uint32_t first, uint32_t count) { - struct ext4_fs *fs = inode_ref->fs; - struct ext4_sblock *sb = &fs->sb; - - /* Compute indexes */ - uint32_t block_group_first = - ext4_balloc_get_bgid_of_block(sb, first); - uint32_t block_group_last = - ext4_balloc_get_bgid_of_block(sb, first + count - 1); - - ext4_assert(block_group_first == block_group_last); - - /* Load block group reference */ - struct ext4_block_group_ref bg_ref; - int rc = ext4_fs_get_block_group_ref(fs, block_group_first, &bg_ref); - if (rc != EOK) - return rc; - - uint32_t index_in_group_first = - ext4_fs_baddr2_index_in_group(sb, first); - - /* Load block with bitmap */ - uint32_t bitmap_block_addr = - ext4_bg_get_block_bitmap(bg_ref.block_group, sb); - - struct ext4_block bitmap_block; - - rc = ext4_block_get(fs->bdev, &bitmap_block, bitmap_block_addr); - if (rc != EOK) - return rc; - - /* Modify bitmap */ - ext4_bmap_bits_free(bitmap_block.data, index_in_group_first, count); - bitmap_block.dirty = true; - - /* Release block with bitmap */ - rc = ext4_block_set(fs->bdev, &bitmap_block); - if (rc != EOK) { - ext4_fs_put_block_group_ref(&bg_ref); - return rc; - } - - uint32_t block_size = ext4_sb_get_block_size(sb); - - /* Update superblock free blocks count */ - uint64_t sb_free_blocks = - ext4_sb_get_free_blocks_cnt(sb); - sb_free_blocks += count; - ext4_sb_set_free_blocks_cnt(sb, sb_free_blocks); - - /* Update inode blocks count */ - uint64_t ino_blocks = - ext4_inode_get_blocks_count(sb, inode_ref->inode); - ino_blocks -= count * (block_size / EXT4_INODE_BLOCK_SIZE); - ext4_inode_set_blocks_count(sb, inode_ref->inode, ino_blocks); - inode_ref->dirty = true; - - /* Update block group free blocks count */ - uint32_t free_blocks = - ext4_bg_get_free_blocks_count(bg_ref.block_group, sb); - free_blocks += count; - ext4_bg_set_free_blocks_count(bg_ref.block_group, - sb, free_blocks); - bg_ref.dirty = true; - - /* Release block group reference */ - rc = ext4_fs_put_block_group_ref(&bg_ref); - if (rc != EOK) - return rc; - - return EOK; + struct ext4_fs *fs = inode_ref->fs; + struct ext4_sblock *sb = &fs->sb; + + /* Compute indexes */ + uint32_t block_group_first = + ext4_balloc_get_bgid_of_block(sb, first); + uint32_t block_group_last = + ext4_balloc_get_bgid_of_block(sb, first + count - 1); + + ext4_assert(block_group_first == block_group_last); + + /* Load block group reference */ + struct ext4_block_group_ref bg_ref; + int rc = ext4_fs_get_block_group_ref(fs, block_group_first, &bg_ref); + if (rc != EOK) + return rc; + + uint32_t index_in_group_first = + ext4_fs_baddr2_index_in_group(sb, first); + + /* Load block with bitmap */ + uint32_t bitmap_block_addr = + ext4_bg_get_block_bitmap(bg_ref.block_group, sb); + + struct ext4_block bitmap_block; + + rc = ext4_block_get(fs->bdev, &bitmap_block, bitmap_block_addr); + if (rc != EOK) + return rc; + + /* Modify bitmap */ + ext4_bmap_bits_free(bitmap_block.data, index_in_group_first, count); + bitmap_block.dirty = true; + + /* Release block with bitmap */ + rc = ext4_block_set(fs->bdev, &bitmap_block); + if (rc != EOK) { + ext4_fs_put_block_group_ref(&bg_ref); + return rc; + } + + uint32_t block_size = ext4_sb_get_block_size(sb); + + /* Update superblock free blocks count */ + uint64_t sb_free_blocks = + ext4_sb_get_free_blocks_cnt(sb); + sb_free_blocks += count; + ext4_sb_set_free_blocks_cnt(sb, sb_free_blocks); + + /* Update inode blocks count */ + uint64_t ino_blocks = + ext4_inode_get_blocks_count(sb, inode_ref->inode); + ino_blocks -= count * (block_size / EXT4_INODE_BLOCK_SIZE); + ext4_inode_set_blocks_count(sb, inode_ref->inode, ino_blocks); + inode_ref->dirty = true; + + /* Update block group free blocks count */ + uint32_t free_blocks = + ext4_bg_get_free_blocks_count(bg_ref.block_group, sb); + free_blocks += count; + ext4_bg_set_free_blocks_count(bg_ref.block_group, + sb, free_blocks); + bg_ref.dirty = true; + + /* Release block group reference */ + rc = ext4_fs_put_block_group_ref(&bg_ref); + if (rc != EOK) + return rc; + + return EOK; } static uint32_t ext4_balloc_find_goal(struct ext4_inode_ref *inode_ref) { - uint32_t goal = 0; + uint32_t goal = 0; - struct ext4_sblock *sb = &inode_ref->fs->sb; + struct ext4_sblock *sb = &inode_ref->fs->sb; - uint64_t inode_size = ext4_inode_get_size(sb, inode_ref->inode); - uint32_t block_size = ext4_sb_get_block_size(sb); - uint32_t inode_block_count = inode_size / block_size; + uint64_t inode_size = ext4_inode_get_size(sb, inode_ref->inode); + uint32_t block_size = ext4_sb_get_block_size(sb); + uint32_t inode_block_count = inode_size / block_size; - if (inode_size % block_size != 0) - inode_block_count++; + if (inode_size % block_size != 0) + inode_block_count++; - /* If inode has some blocks, get last block address + 1 */ - if (inode_block_count > 0) { - int rc = ext4_fs_get_inode_data_block_index(inode_ref, - inode_block_count - 1, &goal); - if (rc != EOK) - return 0; + /* If inode has some blocks, get last block address + 1 */ + if (inode_block_count > 0) { + int rc = ext4_fs_get_inode_data_block_index(inode_ref, + inode_block_count - 1, &goal); + if (rc != EOK) + return 0; - if (goal != 0) { - goal++; - return goal; - } + if (goal != 0) { + goal++; + return goal; + } - /* If goal == 0, sparse file -> continue */ - } + /* If goal == 0, sparse file -> continue */ + } - /* Identify block group of inode */ + /* Identify block group of inode */ - uint32_t inodes_per_group = ext4_get32(sb, inodes_per_group); - uint32_t block_group = (inode_ref->index - 1) / inodes_per_group; - block_size = ext4_sb_get_block_size(sb); + uint32_t inodes_per_group = ext4_get32(sb, inodes_per_group); + uint32_t block_group = (inode_ref->index - 1) / inodes_per_group; + block_size = ext4_sb_get_block_size(sb); - /* Load block group reference */ - struct ext4_block_group_ref bg_ref; - int rc = ext4_fs_get_block_group_ref(inode_ref->fs, - block_group, &bg_ref); - if (rc != EOK) - return 0; + /* Load block group reference */ + struct ext4_block_group_ref bg_ref; + int rc = ext4_fs_get_block_group_ref(inode_ref->fs, + block_group, &bg_ref); + if (rc != EOK) + return 0; - /* Compute indexes */ - uint32_t block_group_count = ext4_block_group_cnt(sb); - uint32_t inode_table_first_block = - ext4_bg_get_inode_table_first_block(bg_ref.block_group, sb); - uint16_t inode_table_item_size = ext4_get16(sb, inode_size); - uint32_t inode_table_bytes; + /* Compute indexes */ + uint32_t block_group_count = ext4_block_group_cnt(sb); + uint32_t inode_table_first_block = + ext4_bg_get_inode_table_first_block(bg_ref.block_group, sb); + uint16_t inode_table_item_size = ext4_get16(sb, inode_size); + uint32_t inode_table_bytes; - /* Check for last block group */ - if (block_group < block_group_count - 1) { - inode_table_bytes = inodes_per_group * inode_table_item_size; - } else { - /* Last block group could be smaller */ - uint32_t inodes_count_total = ext4_get32(sb, inodes_count); + /* Check for last block group */ + if (block_group < block_group_count - 1) { + inode_table_bytes = inodes_per_group * inode_table_item_size; + } else { + /* Last block group could be smaller */ + uint32_t inodes_count_total = ext4_get32(sb, inodes_count); - inode_table_bytes = - (inodes_count_total - ((block_group_count - 1) * inodes_per_group)) * - inode_table_item_size; - } + inode_table_bytes = + (inodes_count_total - ((block_group_count - 1) * inodes_per_group)) * + inode_table_item_size; + } - uint32_t inode_table_blocks = inode_table_bytes / block_size; + uint32_t inode_table_blocks = inode_table_bytes / block_size; - if (inode_table_bytes % block_size) - inode_table_blocks++; + if (inode_table_bytes % block_size) + inode_table_blocks++; - goal = inode_table_first_block + inode_table_blocks; + goal = inode_table_first_block + inode_table_blocks; - ext4_fs_put_block_group_ref(&bg_ref); + ext4_fs_put_block_group_ref(&bg_ref); - return goal; + return goal; } -int ext4_balloc_alloc_block(struct ext4_inode_ref *inode_ref, uint32_t *fblock) +int ext4_balloc_alloc_block(struct ext4_inode_ref *inode_ref, uint32_t *fblock) { - uint32_t allocated_block = 0; - uint32_t bitmap_block_addr; - uint32_t rel_block_idx = 0; - - struct ext4_block bitmap_block; - - /* Find GOAL */ - uint32_t goal = ext4_balloc_find_goal(inode_ref); - if (goal == 0) { - /* no goal found => partition is full */ - return ENOSPC; - } - - struct ext4_sblock *sb = &inode_ref->fs->sb; - - /* Load block group number for goal and relative index */ - uint32_t block_group = ext4_balloc_get_bgid_of_block(sb, goal); - uint32_t index_in_group = - ext4_fs_baddr2_index_in_group(sb, goal); - - /* Load block group reference */ - struct ext4_block_group_ref bg_ref; - int rc = ext4_fs_get_block_group_ref(inode_ref->fs, - block_group, &bg_ref); - if (rc != EOK) - return rc; - - /* Compute indexes */ - uint32_t first_in_group = - ext4_balloc_get_first_data_block_in_group(sb, &bg_ref); - - uint32_t first_in_group_index = - ext4_fs_baddr2_index_in_group(sb, first_in_group); - - if (index_in_group < first_in_group_index) - index_in_group = first_in_group_index; - - /* Load block with bitmap */ - bitmap_block_addr = - ext4_bg_get_block_bitmap(bg_ref.block_group, sb); - - rc = ext4_block_get(inode_ref->fs->bdev, &bitmap_block, bitmap_block_addr); - if (rc != EOK) { - ext4_fs_put_block_group_ref(&bg_ref); - return rc; - } - - /* Check if goal is free */ - if (ext4_bmap_is_bit_clr(bitmap_block.data, index_in_group)) { - ext4_bmap_bit_set(bitmap_block.data, index_in_group); - bitmap_block.dirty = true; - rc = ext4_block_set(inode_ref->fs->bdev, &bitmap_block); - if (rc != EOK) { - ext4_fs_put_block_group_ref(&bg_ref); - return rc; - } - - allocated_block = - ext4_fs_index_in_group2_baddr(sb, index_in_group, - block_group); - - goto success; - } - - uint32_t blocks_in_group = - ext4_blocks_in_group_cnt(sb, block_group); + uint32_t allocated_block = 0; + uint32_t bitmap_block_addr; + uint32_t rel_block_idx = 0; + + struct ext4_block bitmap_block; + + /* Find GOAL */ + uint32_t goal = ext4_balloc_find_goal(inode_ref); + if (goal == 0) { + /* no goal found => partition is full */ + return ENOSPC; + } + + struct ext4_sblock *sb = &inode_ref->fs->sb; + + /* Load block group number for goal and relative index */ + uint32_t block_group = ext4_balloc_get_bgid_of_block(sb, goal); + uint32_t index_in_group = + ext4_fs_baddr2_index_in_group(sb, goal); + + /* Load block group reference */ + struct ext4_block_group_ref bg_ref; + int rc = ext4_fs_get_block_group_ref(inode_ref->fs, + block_group, &bg_ref); + if (rc != EOK) + return rc; + + /* Compute indexes */ + uint32_t first_in_group = + ext4_balloc_get_first_data_block_in_group(sb, &bg_ref); + + uint32_t first_in_group_index = + ext4_fs_baddr2_index_in_group(sb, first_in_group); + + if (index_in_group < first_in_group_index) + index_in_group = first_in_group_index; + + /* Load block with bitmap */ + bitmap_block_addr = + ext4_bg_get_block_bitmap(bg_ref.block_group, sb); + + rc = ext4_block_get(inode_ref->fs->bdev, &bitmap_block, bitmap_block_addr); + if (rc != EOK) { + ext4_fs_put_block_group_ref(&bg_ref); + return rc; + } + + /* Check if goal is free */ + if (ext4_bmap_is_bit_clr(bitmap_block.data, index_in_group)) { + ext4_bmap_bit_set(bitmap_block.data, index_in_group); + bitmap_block.dirty = true; + rc = ext4_block_set(inode_ref->fs->bdev, &bitmap_block); + if (rc != EOK) { + ext4_fs_put_block_group_ref(&bg_ref); + return rc; + } + + allocated_block = + ext4_fs_index_in_group2_baddr(sb, index_in_group, + block_group); + + goto success; + } + + uint32_t blocks_in_group = + ext4_blocks_in_group_cnt(sb, block_group); + + uint32_t end_idx = (index_in_group + 63) & ~63; + if (end_idx > blocks_in_group) + end_idx = blocks_in_group; - uint32_t end_idx = (index_in_group + 63) & ~63; - if (end_idx > blocks_in_group) - end_idx = blocks_in_group; + /* Try to find free block near to goal */ + uint32_t tmp_idx; + for (tmp_idx = index_in_group + 1; tmp_idx < end_idx; + ++tmp_idx) { + if (ext4_bmap_is_bit_clr(bitmap_block.data, tmp_idx)) { + ext4_bmap_bit_set(bitmap_block.data, tmp_idx); + + bitmap_block.dirty = true; + rc = ext4_block_set(inode_ref->fs->bdev, &bitmap_block); + if (rc != EOK) + return rc; - /* Try to find free block near to goal */ - uint32_t tmp_idx; - for (tmp_idx = index_in_group + 1; tmp_idx < end_idx; - ++tmp_idx) { - if (ext4_bmap_is_bit_clr(bitmap_block.data, tmp_idx)) { - ext4_bmap_bit_set(bitmap_block.data, tmp_idx); + allocated_block = + ext4_fs_index_in_group2_baddr(sb, tmp_idx, + block_group); - bitmap_block.dirty = true; - rc = ext4_block_set(inode_ref->fs->bdev, &bitmap_block); - if (rc != EOK) - return rc; + goto success; + } + } - allocated_block = - ext4_fs_index_in_group2_baddr(sb, tmp_idx, - block_group); - goto success; - } - } + /* Find free bit in bitmap */ + rc = ext4_bmap_bit_find_clr(bitmap_block.data, + index_in_group, blocks_in_group, &rel_block_idx); + if (rc == EOK) { + ext4_bmap_bit_set(bitmap_block.data, rel_block_idx); + bitmap_block.dirty = true; + rc = ext4_block_set(inode_ref->fs->bdev, &bitmap_block); + if (rc != EOK) + return rc; + + allocated_block = + ext4_fs_index_in_group2_baddr(sb, rel_block_idx, + block_group); + + goto success; + } + /* No free block found yet */ + ext4_block_set(inode_ref->fs->bdev, &bitmap_block); + ext4_fs_put_block_group_ref(&bg_ref); - /* Find free bit in bitmap */ - rc = ext4_bmap_bit_find_clr(bitmap_block.data, - index_in_group, blocks_in_group, &rel_block_idx); - if (rc == EOK) { - ext4_bmap_bit_set(bitmap_block.data, rel_block_idx); - bitmap_block.dirty = true; - rc = ext4_block_set(inode_ref->fs->bdev, &bitmap_block); - if (rc != EOK) - return rc; - - allocated_block = - ext4_fs_index_in_group2_baddr(sb, rel_block_idx, - block_group); - - goto success; - } + /* Try other block groups */ + uint32_t block_group_count = ext4_block_group_cnt(sb); + + uint32_t bgid = (block_group + 1) % block_group_count; + uint32_t count = block_group_count; - /* No free block found yet */ - ext4_block_set(inode_ref->fs->bdev, &bitmap_block); - ext4_fs_put_block_group_ref(&bg_ref); + while (count > 0) { + rc = ext4_fs_get_block_group_ref(inode_ref->fs, bgid, + &bg_ref); + if (rc != EOK) + return rc; - /* Try other block groups */ - uint32_t block_group_count = ext4_block_group_cnt(sb); + /* Load block with bitmap */ + bitmap_block_addr = + ext4_bg_get_block_bitmap(bg_ref.block_group, sb); - uint32_t bgid = (block_group + 1) % block_group_count; - uint32_t count = block_group_count; + rc = ext4_block_get(inode_ref->fs->bdev, &bitmap_block, + bitmap_block_addr); - while (count > 0) { - rc = ext4_fs_get_block_group_ref(inode_ref->fs, bgid, - &bg_ref); - if (rc != EOK) - return rc; + if (rc != EOK) { + ext4_fs_put_block_group_ref(&bg_ref); + return rc; + } - /* Load block with bitmap */ - bitmap_block_addr = - ext4_bg_get_block_bitmap(bg_ref.block_group, sb); + /* Compute indexes */ + first_in_group = + ext4_balloc_get_first_data_block_in_group(sb, &bg_ref); + index_in_group = + ext4_fs_baddr2_index_in_group(sb, first_in_group); + blocks_in_group = ext4_blocks_in_group_cnt(sb, bgid); - rc = ext4_block_get(inode_ref->fs->bdev, &bitmap_block, bitmap_block_addr); + first_in_group_index = + ext4_fs_baddr2_index_in_group(sb, first_in_group); - if (rc != EOK) { - ext4_fs_put_block_group_ref(&bg_ref); - return rc; - } + if (index_in_group < first_in_group_index) + index_in_group = first_in_group_index; - /* Compute indexes */ - first_in_group = - ext4_balloc_get_first_data_block_in_group(sb, &bg_ref); - index_in_group = - ext4_fs_baddr2_index_in_group(sb, first_in_group); - blocks_in_group = ext4_blocks_in_group_cnt(sb, bgid); - first_in_group_index = - ext4_fs_baddr2_index_in_group(sb, first_in_group); + rc = ext4_bmap_bit_find_clr(bitmap_block.data, + index_in_group, blocks_in_group, &rel_block_idx); - if (index_in_group < first_in_group_index) - index_in_group = first_in_group_index; + if (rc == EOK) { + ext4_bmap_bit_set(bitmap_block.data, rel_block_idx); - rc = ext4_bmap_bit_find_clr(bitmap_block.data, - index_in_group, blocks_in_group, &rel_block_idx); + bitmap_block.dirty = true; + rc = ext4_block_set(inode_ref->fs->bdev, &bitmap_block); + if (rc != EOK) + return rc; - if (rc == EOK) { + allocated_block = + ext4_fs_index_in_group2_baddr(sb, rel_block_idx, + bgid); - ext4_bmap_bit_set(bitmap_block.data, rel_block_idx); + goto success; + } - bitmap_block.dirty = true; - rc = ext4_block_set(inode_ref->fs->bdev, &bitmap_block); - if (rc != EOK) - return rc; + ext4_block_set(inode_ref->fs->bdev, &bitmap_block); + ext4_fs_put_block_group_ref(&bg_ref); - allocated_block = - ext4_fs_index_in_group2_baddr(sb, rel_block_idx, - bgid); + /* Goto next group */ + bgid = (bgid + 1) % block_group_count; + count--; + } - goto success; - } + return ENOSPC; - ext4_block_set(inode_ref->fs->bdev, &bitmap_block); - ext4_fs_put_block_group_ref(&bg_ref); + success: + /* Empty command - because of syntax */ + ; - /* Goto next group */ - bgid = (bgid + 1) % block_group_count; - count--; - } + uint32_t block_size = ext4_sb_get_block_size(sb); - return ENOSPC; + /* Update superblock free blocks count */ + uint64_t sb_free_blocks = ext4_sb_get_free_blocks_cnt(sb); + sb_free_blocks--; + ext4_sb_set_free_blocks_cnt(sb, sb_free_blocks); -success: - /* Empty command - because of syntax */ - ; + /* Update inode blocks (different block size!) count */ + uint64_t ino_blocks = + ext4_inode_get_blocks_count(sb, inode_ref->inode); + ino_blocks += block_size / EXT4_INODE_BLOCK_SIZE; + ext4_inode_set_blocks_count(sb, inode_ref->inode, ino_blocks); + inode_ref->dirty = true; - uint32_t block_size = ext4_sb_get_block_size(sb); + /* Update block group free blocks count */ + uint32_t bg_free_blocks = + ext4_bg_get_free_blocks_count(bg_ref.block_group, sb); + bg_free_blocks--; + ext4_bg_set_free_blocks_count(bg_ref.block_group, sb, + bg_free_blocks); - /* Update superblock free blocks count */ - uint64_t sb_free_blocks = ext4_sb_get_free_blocks_cnt(sb); - sb_free_blocks--; - ext4_sb_set_free_blocks_cnt(sb, sb_free_blocks); + bg_ref.dirty = true; - /* Update inode blocks (different block size!) count */ - uint64_t ino_blocks = - ext4_inode_get_blocks_count(sb, inode_ref->inode); - ino_blocks += block_size / EXT4_INODE_BLOCK_SIZE; - ext4_inode_set_blocks_count(sb, inode_ref->inode, ino_blocks); - inode_ref->dirty = true; + ext4_fs_put_block_group_ref(&bg_ref); - /* Update block group free blocks count */ - uint32_t bg_free_blocks = - ext4_bg_get_free_blocks_count(bg_ref.block_group, sb); - bg_free_blocks--; - ext4_bg_set_free_blocks_count(bg_ref.block_group, sb, - bg_free_blocks); - - bg_ref.dirty = true; - - ext4_fs_put_block_group_ref(&bg_ref); - - *fblock = allocated_block; - return EOK; + *fblock = allocated_block; + return EOK; } -int ext4_balloc_try_alloc_block(struct ext4_inode_ref *inode_ref, uint32_t baddr, bool *free) +int ext4_balloc_try_alloc_block(struct ext4_inode_ref *inode_ref, + uint32_t baddr, bool *free) { - int rc = EOK; - - struct ext4_fs *fs = inode_ref->fs; - struct ext4_sblock *sb = &fs->sb; - - /* Compute indexes */ - uint32_t block_group = ext4_balloc_get_bgid_of_block(sb, baddr); - uint32_t index_in_group = - ext4_fs_baddr2_index_in_group(sb, baddr); - - /* Load block group reference */ - struct ext4_block_group_ref bg_ref; - rc = ext4_fs_get_block_group_ref(fs, block_group, &bg_ref); - if (rc != EOK) - return rc; - - /* Load block with bitmap */ - uint32_t bitmap_block_addr = - ext4_bg_get_block_bitmap(bg_ref.block_group, sb); - - - struct ext4_block bitmap_block; - - rc = ext4_block_get(fs->bdev, &bitmap_block, bitmap_block_addr); - if (rc != EOK) - return rc; - - /* Check if block is free */ - *free = ext4_bmap_is_bit_clr(bitmap_block.data, index_in_group); - - /* Allocate block if possible */ - if (*free) { - ext4_bmap_bit_set(bitmap_block.data, index_in_group); - bitmap_block.dirty = true; - } - - /* Release block with bitmap */ - rc = ext4_block_set(fs->bdev, &bitmap_block); - if (rc != EOK) { - /* Error in saving bitmap */ - ext4_fs_put_block_group_ref(&bg_ref); - return rc; - } - - /* If block is not free, return */ - if (!(*free)) - goto terminate; - - uint32_t block_size = ext4_sb_get_block_size(sb); - - /* Update superblock free blocks count */ - uint64_t sb_free_blocks = ext4_sb_get_free_blocks_cnt(sb); - sb_free_blocks--; - ext4_sb_set_free_blocks_cnt(sb, sb_free_blocks); - - /* Update inode blocks count */ - uint64_t ino_blocks = - ext4_inode_get_blocks_count(sb, inode_ref->inode); - ino_blocks += block_size / EXT4_INODE_BLOCK_SIZE; - ext4_inode_set_blocks_count(sb, inode_ref->inode, ino_blocks); - inode_ref->dirty = true; - - /* Update block group free blocks count */ - uint32_t free_blocks = - ext4_bg_get_free_blocks_count(bg_ref.block_group, sb); - free_blocks--; - ext4_bg_set_free_blocks_count(bg_ref.block_group, - sb, free_blocks); - - bg_ref.dirty = true; - -terminate: - return ext4_fs_put_block_group_ref(&bg_ref); + int rc = EOK; + + struct ext4_fs *fs = inode_ref->fs; + struct ext4_sblock *sb = &fs->sb; + + /* Compute indexes */ + uint32_t block_group = ext4_balloc_get_bgid_of_block(sb, baddr); + uint32_t index_in_group = + ext4_fs_baddr2_index_in_group(sb, baddr); + + /* Load block group reference */ + struct ext4_block_group_ref bg_ref; + rc = ext4_fs_get_block_group_ref(fs, block_group, &bg_ref); + if (rc != EOK) + return rc; + + /* Load block with bitmap */ + uint32_t bitmap_block_addr = + ext4_bg_get_block_bitmap(bg_ref.block_group, sb); + + + struct ext4_block bitmap_block; + + rc = ext4_block_get(fs->bdev, &bitmap_block, bitmap_block_addr); + if (rc != EOK) + return rc; + + /* Check if block is free */ + *free = ext4_bmap_is_bit_clr(bitmap_block.data, index_in_group); + + /* Allocate block if possible */ + if (*free) { + ext4_bmap_bit_set(bitmap_block.data, index_in_group); + bitmap_block.dirty = true; + } + + /* Release block with bitmap */ + rc = ext4_block_set(fs->bdev, &bitmap_block); + if (rc != EOK) { + /* Error in saving bitmap */ + ext4_fs_put_block_group_ref(&bg_ref); + return rc; + } + + /* If block is not free, return */ + if (!(*free)) + goto terminate; + + uint32_t block_size = ext4_sb_get_block_size(sb); + + /* Update superblock free blocks count */ + uint64_t sb_free_blocks = ext4_sb_get_free_blocks_cnt(sb); + sb_free_blocks--; + ext4_sb_set_free_blocks_cnt(sb, sb_free_blocks); + + /* Update inode blocks count */ + uint64_t ino_blocks = + ext4_inode_get_blocks_count(sb, inode_ref->inode); + ino_blocks += block_size / EXT4_INODE_BLOCK_SIZE; + ext4_inode_set_blocks_count(sb, inode_ref->inode, ino_blocks); + inode_ref->dirty = true; + + /* Update block group free blocks count */ + uint32_t free_blocks = + ext4_bg_get_free_blocks_count(bg_ref.block_group, sb); + free_blocks--; + ext4_bg_set_free_blocks_count(bg_ref.block_group, + sb, free_blocks); + + bg_ref.dirty = true; + + terminate: + return ext4_fs_put_block_group_ref(&bg_ref); } /** diff --git a/src/lwext4/ext4_balloc.h b/src/lwext4/ext4_balloc.h index 47f030e..625c905 100644 --- a/src/lwext4/ext4_balloc.h +++ b/src/lwext4/ext4_balloc.h @@ -53,32 +53,37 @@ * @param s superblock descriptor * @param bg_ref block group reference * @return block id of the first datablock in block group*/ -uint32_t ext4_balloc_get_first_data_block_in_group(struct ext4_sblock *s, struct ext4_block_group_ref * bg_ref); +uint32_t ext4_balloc_get_first_data_block_in_group(struct ext4_sblock *s, + struct ext4_block_group_ref * bg_ref); /**@brief Free block from inode. * @param inode_ref inode reference * @param baddr block address * @return standard error code*/ -int ext4_balloc_free_block(struct ext4_inode_ref *inode_ref, uint32_t baddr); +int ext4_balloc_free_block(struct ext4_inode_ref *inode_ref, + uint32_t baddr); /**@brief Free blocks from inode. * @param inode_ref inode reference * @param baddr block address * @return standard error code*/ -int ext4_balloc_free_blocks(struct ext4_inode_ref *inode_ref, uint32_t first, uint32_t count); +int ext4_balloc_free_blocks(struct ext4_inode_ref *inode_ref, + uint32_t first, uint32_t count); /**@brief Allocate block procedure. * @param inode_ref inode reference * @param baddr allocated block address * @return standard error code*/ -int ext4_balloc_alloc_block(struct ext4_inode_ref *inode_ref, uint32_t *baddr); +int ext4_balloc_alloc_block(struct ext4_inode_ref *inode_ref, + uint32_t *baddr); /**@brief Try allocate selected block. * @param inode_ref inode reference * @param baddr block address to allocate * @param free if baddr is not allocated * @return standard error code*/ -int ext4_balloc_try_alloc_block(struct ext4_inode_ref *inode_ref, uint32_t baddr, bool *free); +int ext4_balloc_try_alloc_block(struct ext4_inode_ref *inode_ref, + uint32_t baddr, bool *free); #endif /* EXT4_BALLOC_H_ */ diff --git a/src/lwext4/ext4_bcache.c b/src/lwext4/ext4_bcache.c index 18da0ef..9a33468 100644 --- a/src/lwext4/ext4_bcache.c +++ b/src/lwext4/ext4_bcache.c @@ -45,229 +45,229 @@ int ext4_bcache_init_dynamic(struct ext4_bcache *bc, uint32_t cnt, uint32_t itemsize)
{
- ext4_assert(bc && cnt && itemsize);
+ ext4_assert(bc && cnt && itemsize);
- bc->lru_ctr= 0;
+ bc->lru_ctr= 0;
- bc->refctr = 0;
- bc->lru_id = 0;
- bc->free_delay = 0;
- bc->lba = 0;
- bc->data = 0;
+ bc->refctr = 0;
+ bc->lru_id = 0;
+ bc->free_delay = 0;
+ bc->lba = 0;
+ bc->data = 0;
- bc->refctr = malloc(cnt * sizeof(uint32_t));
- if(!bc->refctr)
- goto error;
+ bc->refctr = malloc(cnt * sizeof(uint32_t));
+ if(!bc->refctr)
+ goto error;
- bc->lru_id = malloc(cnt * sizeof(uint32_t));
- if(!bc->lru_id)
- goto error;
+ bc->lru_id = malloc(cnt * sizeof(uint32_t));
+ if(!bc->lru_id)
+ goto error;
- bc->free_delay = malloc(cnt * sizeof(uint8_t));
- if(!bc->free_delay)
- goto error;
+ bc->free_delay = malloc(cnt * sizeof(uint8_t));
+ if(!bc->free_delay)
+ goto error;
- bc->lba = malloc(cnt * sizeof(uint64_t));
- if(!bc->lba)
- goto error;
+ bc->lba = malloc(cnt * sizeof(uint64_t));
+ if(!bc->lba)
+ goto error;
- bc->data = malloc(cnt * itemsize);
- if(!bc->data)
- goto error;
+ bc->data = malloc(cnt * itemsize);
+ if(!bc->data)
+ goto error;
- memset(bc->refctr, 0, cnt * sizeof(uint32_t));
- memset(bc->lru_id, 0, cnt * sizeof(uint32_t));
- memset(bc->free_delay, 0, cnt * sizeof(uint8_t));
- memset(bc->lba, 0, cnt * sizeof(uint64_t));
+ memset(bc->refctr, 0, cnt * sizeof(uint32_t));
+ memset(bc->lru_id, 0, cnt * sizeof(uint32_t));
+ memset(bc->free_delay, 0, cnt * sizeof(uint8_t));
+ memset(bc->lba, 0, cnt * sizeof(uint64_t));
- bc->cnt = cnt;
- bc->itemsize = itemsize;
- bc->ref_blocks = 0;
- bc->max_ref_blocks = 0;
+ bc->cnt = cnt;
+ bc->itemsize = itemsize;
+ bc->ref_blocks = 0;
+ bc->max_ref_blocks = 0;
- return EOK;
+ return EOK;
- error:
+ error:
- if(bc->refctr)
- free(bc->refctr);
+ if(bc->refctr)
+ free(bc->refctr);
- if(bc->lru_id)
- free(bc->lru_id);
+ if(bc->lru_id)
+ free(bc->lru_id);
- if(bc->free_delay)
- free(bc->free_delay);
+ if(bc->free_delay)
+ free(bc->free_delay);
- if(bc->lba)
- free(bc->lba);
+ if(bc->lba)
+ free(bc->lba);
- if(bc->data)
- free(bc->data);
+ if(bc->data)
+ free(bc->data);
- return ENOMEM;
+ return ENOMEM;
}
int ext4_bcache_fini_dynamic(struct ext4_bcache *bc)
{
- if(bc->refctr)
- free(bc->refctr);
+ if(bc->refctr)
+ free(bc->refctr);
- if(bc->lru_id)
- free(bc->lru_id);
+ if(bc->lru_id)
+ free(bc->lru_id);
- if(bc->free_delay)
- free(bc->free_delay);
+ if(bc->free_delay)
+ free(bc->free_delay);
- if(bc->lba)
- free(bc->lba);
+ if(bc->lba)
+ free(bc->lba);
- if(bc->data)
- free(bc->data);
+ if(bc->data)
+ free(bc->data);
- bc->lru_ctr= 0;
+ bc->lru_ctr= 0;
- bc->refctr = 0;
- bc->lru_id = 0;
- bc->free_delay = 0;
- bc->lba = 0;
- bc->data = 0;
+ bc->refctr = 0;
+ bc->lru_id = 0;
+ bc->free_delay = 0;
+ bc->lba = 0;
+ bc->data = 0;
- return EOK;
+ return EOK;
}
int ext4_bcache_alloc(struct ext4_bcache *bc, struct ext4_block *b, bool *is_new)
{
- uint32_t i;
- ext4_assert(bc && b && is_new);
+ uint32_t i;
+ ext4_assert(bc && b && is_new);
- /*Check if valid.*/
- ext4_assert(b->lb_id);
- if(!b->lb_id){
- ext4_assert(b->lb_id);
- }
+ /*Check if valid.*/
+ ext4_assert(b->lb_id);
+ if(!b->lb_id){
+ ext4_assert(b->lb_id);
+ }
- uint32_t cache_id = bc->cnt;
- uint32_t alloc_id;
+ uint32_t cache_id = bc->cnt;
+ uint32_t alloc_id;
- *is_new = false;
+ *is_new = false;
- /*Find in free blocks (Last Recently Used).*/
- for (i = 0; i < bc->cnt; ++i) {
+ /*Find in free blocks (Last Recently Used).*/
+ for (i = 0; i < bc->cnt; ++i) {
- /*Check if block is already in cache*/
- if(b->lb_id == bc->lba[i]){
+ /*Check if block is already in cache*/
+ if(b->lb_id == bc->lba[i]){
- if(!bc->refctr[i] && !bc->free_delay[i])
- bc->ref_blocks++;
+ if(!bc->refctr[i] && !bc->free_delay[i])
+ bc->ref_blocks++;
- /*Update reference counter*/
- bc->refctr[i]++;
+ /*Update reference counter*/
+ bc->refctr[i]++;
- /*Update usage marker*/
- bc->lru_id[i] = ++bc->lru_ctr;
+ /*Update usage marker*/
+ bc->lru_id[i] = ++bc->lru_ctr;
- /*Set valid cache data*/
- b->data = bc->data + i * bc->itemsize;
+ /*Set valid cache data*/
+ b->data = bc->data + i * bc->itemsize;
- return EOK;
- }
+ return EOK;
+ }
- /*Best fit calculations.*/
- if(bc->refctr[i])
- continue;
+ /*Best fit calculations.*/
+ if(bc->refctr[i])
+ continue;
- if(bc->free_delay[i])
- continue;
+ if(bc->free_delay[i])
+ continue;
- /*Block is unreferenced, but it may exist block with
- * lower usage marker*/
+ /*Block is unreferenced, but it may exist block with
+ * lower usage marker*/
- /*First find.*/
- if(cache_id == bc->cnt){
- cache_id = i;
- alloc_id = bc->lru_id[i];
- continue;
- }
+ /*First find.*/
+ if(cache_id == bc->cnt){
+ cache_id = i;
+ alloc_id = bc->lru_id[i];
+ continue;
+ }
- /*Next find*/
- if(alloc_id <= bc->lru_id[i])
- continue;
+ /*Next find*/
+ if(alloc_id <= bc->lru_id[i])
+ continue;
- /*This block has lower alloc id marker*/
- cache_id = i;
- alloc_id = bc->lru_id[i];
- }
+ /*This block has lower alloc id marker*/
+ cache_id = i;
+ alloc_id = bc->lru_id[i];
+ }
- if(cache_id != bc->cnt){
- /*There was unreferenced block*/
- bc->lba[cache_id] = b->lb_id;
- bc->refctr[cache_id] = 1;
- bc->lru_id[cache_id] = ++bc->lru_ctr;
+ if(cache_id != bc->cnt){
+ /*There was unreferenced block*/
+ bc->lba[cache_id] = b->lb_id;
+ bc->refctr[cache_id] = 1;
+ bc->lru_id[cache_id] = ++bc->lru_ctr;
- /*Set valid cache data*/
- b->data = bc->data + cache_id * bc->itemsize;
+ /*Set valid cache data*/
+ b->data = bc->data + cache_id * bc->itemsize;
- /*Statistics*/
- bc->ref_blocks++;
- if(bc->ref_blocks > bc->max_ref_blocks)
- bc->max_ref_blocks = bc->ref_blocks;
+ /*Statistics*/
+ bc->ref_blocks++;
+ if(bc->ref_blocks > bc->max_ref_blocks)
+ bc->max_ref_blocks = bc->ref_blocks;
- /*Block needs to be read.*/
- *is_new = true;
+ /*Block needs to be read.*/
+ *is_new = true;
- return EOK;
- }
+ return EOK;
+ }
- ext4_dprintf(EXT4_DEBUG_BCACHE, "ext4_bcache_alloc: FAIL, unable to alloc block cache!\n");
- return ENOMEM;
+ ext4_dprintf(EXT4_DEBUG_BCACHE, "ext4_bcache_alloc: FAIL, unable to alloc block cache!\n");
+ return ENOMEM;
}
int ext4_bcache_free (struct ext4_bcache *bc, struct ext4_block *b, uint8_t free_delay)
{
- uint32_t i;
+ uint32_t i;
- ext4_assert(bc && b);
+ ext4_assert(bc && b);
- /*Check if valid.*/
- ext4_assert(b->lb_id);
+ /*Check if valid.*/
+ ext4_assert(b->lb_id);
- /*Block should be in cache.*/
- for (i = 0; i < bc->cnt; ++i) {
+ /*Block should be in cache.*/
+ for (i = 0; i < bc->cnt; ++i) {
- if(bc->lba[i] != b->lb_id)
- continue;
+ if(bc->lba[i] != b->lb_id)
+ continue;
- /*Check if someone don't try free unreferenced block cache.*/
- ext4_assert(bc->refctr[i]);
+ /*Check if someone don't try free unreferenced block cache.*/
+ ext4_assert(bc->refctr[i]);
- /*Just decrease reference counter*/
- if(bc->refctr[i])
- bc->refctr[i]--;
+ /*Just decrease reference counter*/
+ if(bc->refctr[i])
+ bc->refctr[i]--;
- if(free_delay)
- bc->free_delay[i] = free_delay;
+ if(free_delay)
+ bc->free_delay[i] = free_delay;
- /*Update statistics*/
- if(!bc->refctr[i] && !bc->free_delay[i])
- bc->ref_blocks--;
+ /*Update statistics*/
+ if(!bc->refctr[i] && !bc->free_delay[i])
+ bc->ref_blocks--;
- b->lb_id = 0;
- b->data = 0;
+ b->lb_id = 0;
+ b->data = 0;
- return EOK;
- }
+ return EOK;
+ }
- ext4_dprintf(EXT4_DEBUG_BCACHE, "ext4_bcache_free: FAIL, block should be in cache memory !\n");
- return EFAULT;
+ ext4_dprintf(EXT4_DEBUG_BCACHE, "ext4_bcache_free: FAIL, block should be in cache memory !\n");
+ return EFAULT;
}
bool ext4_bcache_is_full(struct ext4_bcache *bc)
{
- return (bc->cnt == bc->ref_blocks);
+ return (bc->cnt == bc->ref_blocks);
}
/**
diff --git a/src/lwext4/ext4_bcache.h b/src/lwext4/ext4_bcache.h index c1e1d45..4f4f350 100644 --- a/src/lwext4/ext4_bcache.h +++ b/src/lwext4/ext4_bcache.h @@ -44,69 +44,69 @@ /**@brief Single block descriptor.*/
struct ext4_block {
- /**@brief Dirty flag.*/
- bool dirty;
+ /**@brief Dirty flag.*/
+ bool dirty;
- /**@brief Logical block ID*/
- uint64_t lb_id;
+ /**@brief Logical block ID*/
+ uint64_t lb_id;
- /**@brief Data buffer.*/
- uint8_t *data;
+ /**@brief Data buffer.*/
+ uint8_t *data;
};
/**@brief Block cache descriptor.*/
struct ext4_bcache {
- /**@brief Item count in block cache*/
- uint32_t cnt;
+ /**@brief Item count in block cache*/
+ uint32_t cnt;
- /**@brief Item size in block cache*/
- uint32_t itemsize;
+ /**@brief Item size in block cache*/
+ uint32_t itemsize;
- /**@brief Last recently used counter.*/
- uint32_t lru_ctr;
+ /**@brief Last recently used counter.*/
+ uint32_t lru_ctr;
- /**@brief Reference count table (cnt).*/
- uint32_t *refctr;
+ /**@brief Reference count table (cnt).*/
+ uint32_t *refctr;
- /**@brief Last recently used ID table (cnt)*/
- uint32_t *lru_id;
+ /**@brief Last recently used ID table (cnt)*/
+ uint32_t *lru_id;
- /**@brief Free delay mode table (cnt)*/
- uint8_t *free_delay;
+ /**@brief Free delay mode table (cnt)*/
+ uint8_t *free_delay;
- /**@brief Logical block table (cnt).*/
- uint64_t *lba;
+ /**@brief Logical block table (cnt).*/
+ uint64_t *lba;
- /**@brief Cache data buffers (cnt * itemsize)*/
- uint8_t *data;
+ /**@brief Cache data buffers (cnt * itemsize)*/
+ uint8_t *data;
- /**@brief Currently referenced datablocks*/
- uint32_t ref_blocks;
+ /**@brief Currently referenced datablocks*/
+ uint32_t ref_blocks;
- /**@brief Maximum referenced datablocks*/
- uint32_t max_ref_blocks;
+ /**@brief Maximum referenced datablocks*/
+ uint32_t max_ref_blocks;
};
/**@brief Static initializer of block cache structure.*/
#define EXT4_BCACHE_STATIC_INSTANCE(__name, __cnt, __itemsize) \
- static uint32_t __name##_refctr[(__cnt)]; \
- static uint32_t __name##_lru_id[(__cnt)]; \
- static uint8_t __name##_free_delay[(__cnt)]; \
- static uint64_t __name##_lba[(__cnt)]; \
- static uint8_t __name##_data[(__cnt) * (__itemsize)]; \
- static struct ext4_bcache __name = { \
- .cnt = __cnt, \
- .itemsize = __itemsize, \
- .lru_ctr = 0, \
- .refctr = __name##_refctr, \
- .lru_id = __name##_lru_id, \
- .lba = __name##_lba, \
- .free_delay= __name##_free_delay, \
- .data = __name##_data, \
- }
+ static uint32_t __name##_refctr[(__cnt)]; \
+ static uint32_t __name##_lru_id[(__cnt)]; \
+ static uint8_t __name##_free_delay[(__cnt)]; \
+ static uint64_t __name##_lba[(__cnt)]; \
+ static uint8_t __name##_data[(__cnt) * (__itemsize)]; \
+ static struct ext4_bcache __name = { \
+ .cnt = __cnt, \
+ .itemsize = __itemsize, \
+ .lru_ctr = 0, \
+ .refctr = __name##_refctr, \
+ .lru_id = __name##_lru_id, \
+ .lba = __name##_lba, \
+ .free_delay= __name##_free_delay, \
+ .data = __name##_data, \
+ }
/**@brief Dynamic initialization of block cache.
diff --git a/src/lwext4/ext4_bitmap.c b/src/lwext4/ext4_bitmap.c index 5ea4cfe..67a942a 100644 --- a/src/lwext4/ext4_bitmap.c +++ b/src/lwext4/ext4_bitmap.c @@ -42,127 +42,127 @@ void ext4_bmap_bits_free(uint8_t *bmap, uint32_t sbit, uint32_t bcnt) { - uint32_t i; + uint32_t i; - i = sbit; + i = sbit; - while(i & 7){ + while(i & 7){ - if(!bcnt) - return; + if(!bcnt) + return; - ext4_bmap_bit_clr(bmap, i); + ext4_bmap_bit_clr(bmap, i); - bcnt--; - i++; - } + bcnt--; + i++; + } - sbit = i; - bmap += (sbit >> 3); + sbit = i; + bmap += (sbit >> 3); - while(bcnt >= 32){ - *(uint32_t *)bmap = 0; + while(bcnt >= 32){ + *(uint32_t *)bmap = 0; - bmap += 4; - bcnt -= 32; - sbit += 32; - } + bmap += 4; + bcnt -= 32; + sbit += 32; + } - while(bcnt >= 16){ - *(uint16_t *)bmap = 0; + while(bcnt >= 16){ + *(uint16_t *)bmap = 0; - bmap += 2; - bcnt -= 16; - sbit += 16; - } + bmap += 2; + bcnt -= 16; + sbit += 16; + } - while(bcnt >= 8){ - *bmap = 0; + while(bcnt >= 8){ + *bmap = 0; - bmap += 1; - bcnt -= 8; - sbit += 8; - } + bmap += 1; + bcnt -= 8; + sbit += 8; + } - for (i = 0; i < bcnt; ++i) { - ext4_bmap_bit_clr(bmap, i); - } + for (i = 0; i < bcnt; ++i) { + ext4_bmap_bit_clr(bmap, i); + } } int ext4_bmap_bit_find_clr(uint8_t *bmap, uint32_t sbit, uint32_t ebit, uint32_t *bit_id) { - uint32_t i; - uint32_t bcnt = ebit - sbit; + uint32_t i; + uint32_t bcnt = ebit - sbit; - i = sbit; + i = sbit; - while(i & 7){ + while(i & 7){ - if(!bcnt) - return ENOSPC; + if(!bcnt) + return ENOSPC; - if(ext4_bmap_is_bit_clr(bmap, i)){ - *bit_id = sbit; - return EOK; - } + if(ext4_bmap_is_bit_clr(bmap, i)){ + *bit_id = sbit; + return EOK; + } - i++; - bcnt--; - } + i++; + bcnt--; + } - sbit = i; - bmap += (sbit >> 3); + sbit = i; + bmap += (sbit >> 3); - while(bcnt >= 32){ - if(*(uint32_t *)bmap != 0xFFFFFFFF) - goto finish_it; + while(bcnt >= 32){ + if(*(uint32_t *)bmap != 0xFFFFFFFF) + goto finish_it; - bmap += 4; - bcnt -= 32; - sbit += 32; - } + bmap += 4; + bcnt -= 32; + sbit += 32; + } - while(bcnt >= 16){ - if(*(uint16_t *)bmap != 0xFFFF) - goto finish_it; + while(bcnt >= 16){ + if(*(uint16_t *)bmap != 0xFFFF) + goto finish_it; - bmap += 2; - bcnt -= 16; - sbit += 16; - } + bmap += 2; + bcnt -= 16; + sbit += 16; + } - finish_it: - while(bcnt >= 8){ - if(*bmap != 0xFF){ - for (i = 0; i < 8; ++i) { - if(ext4_bmap_is_bit_clr(bmap, i)){ - *bit_id = sbit + i; - return EOK; - } - } - } + finish_it: + while(bcnt >= 8){ + if(*bmap != 0xFF){ + for (i = 0; i < 8; ++i) { + if(ext4_bmap_is_bit_clr(bmap, i)){ + *bit_id = sbit + i; + return EOK; + } + } + } - bmap += 1; - bcnt -= 8; - sbit += 8; - } + bmap += 1; + bcnt -= 8; + sbit += 8; + } - for (i = 0; i < bcnt; ++i) { - if(ext4_bmap_is_bit_clr(bmap, i)){ - *bit_id = sbit + i; - return EOK; - } - } + for (i = 0; i < bcnt; ++i) { + if(ext4_bmap_is_bit_clr(bmap, i)){ + *bit_id = sbit + i; + return EOK; + } + } - return ENOSPC; + return ENOSPC; } /** diff --git a/src/lwext4/ext4_bitmap.h b/src/lwext4/ext4_bitmap.h index df1316e..6f97537 100644 --- a/src/lwext4/ext4_bitmap.h +++ b/src/lwext4/ext4_bitmap.h @@ -47,7 +47,7 @@ * @param bit bit to set*/ static inline void ext4_bmap_bit_set(uint8_t *bmap, uint32_t bit) { - *(bmap + (bit >> 3)) |= (1 << (bit & 7)); + *(bmap + (bit >> 3)) |= (1 << (bit & 7)); } /**@brief Clear bitmap bit. @@ -55,7 +55,7 @@ static inline void ext4_bmap_bit_set(uint8_t *bmap, uint32_t bit) * @param bit bit to clear*/ static inline void ext4_bmap_bit_clr(uint8_t *bmap, uint32_t bit) { - *(bmap + (bit >> 3)) &= ~(1 << (bit & 7)); + *(bmap + (bit >> 3)) &= ~(1 << (bit & 7)); } @@ -64,7 +64,7 @@ static inline void ext4_bmap_bit_clr(uint8_t *bmap, uint32_t bit) * @param bit bit to check*/ static inline bool ext4_bmap_is_bit_set(uint8_t *bmap, uint32_t bit) { - return (*(bmap + (bit >> 3)) & (1 << (bit & 7))); + return (*(bmap + (bit >> 3)) & (1 << (bit & 7))); } /**@brief Check if the bitmap bit is clear. @@ -72,7 +72,7 @@ static inline bool ext4_bmap_is_bit_set(uint8_t *bmap, uint32_t bit) * @param bit bit to check*/ static inline bool ext4_bmap_is_bit_clr(uint8_t *bmap, uint32_t bit) { - return !ext4_bmap_is_bit_set(bmap, bit); + return !ext4_bmap_is_bit_set(bmap, bit); } /**@brief Free range of bits in bitmap. diff --git a/src/lwext4/ext4_block_group.c b/src/lwext4/ext4_block_group.c index b15a853..d5def32 100644 --- a/src/lwext4/ext4_block_group.c +++ b/src/lwext4/ext4_block_group.c @@ -46,8 +46,8 @@ uint16_t ext4_bg_crc16(uint16_t crc, const uint8_t *buffer, size_t len) { - // TODO - return 0; + // TODO + return 0; } /** diff --git a/src/lwext4/ext4_block_group.h b/src/lwext4/ext4_block_group.h index 69e992f..61e51ce 100644 --- a/src/lwext4/ext4_block_group.h +++ b/src/lwext4/ext4_block_group.h @@ -52,129 +52,129 @@ static inline uint64_t ext4_bg_get_block_bitmap(struct ext4_bgroup *bg, struct ext4_sblock *s) { - uint64_t v = to_le32(bg->block_bitmap_lo); + uint64_t v = to_le32(bg->block_bitmap_lo); - if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE) - v |= (uint64_t) to_le32(bg->block_bitmap_hi) << 32; + if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE) + v |= (uint64_t) to_le32(bg->block_bitmap_hi) << 32; - return v; + return v; } static inline uint64_t ext4_bg_get_inode_bitmap(struct ext4_bgroup *bg, struct ext4_sblock *s) { - uint64_t v = to_le32(bg->inode_bitmap_lo); + uint64_t v = to_le32(bg->inode_bitmap_lo); - if (ext4_sb_get_desc_size(s)> EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE) - v |= (uint64_t) to_le32(bg->inode_bitmap_hi) << 32; + if (ext4_sb_get_desc_size(s)> EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE) + v |= (uint64_t) to_le32(bg->inode_bitmap_hi) << 32; - return v; + return v; } static inline uint64_t ext4_bg_get_inode_table_first_block(struct ext4_bgroup *bg, struct ext4_sblock *s) { - uint64_t v = to_le32(bg->inode_table_first_block_lo); + uint64_t v = to_le32(bg->inode_table_first_block_lo); - if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE) - v |= (uint64_t) to_le32(bg->inode_table_first_block_hi) << 32; + if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE) + v |= (uint64_t) to_le32(bg->inode_table_first_block_hi) << 32; - return v; + return v; } static inline uint32_t ext4_bg_get_free_blocks_count(struct ext4_bgroup *bg, struct ext4_sblock *s) { - uint32_t v = to_le16(bg->free_blocks_count_lo); + uint32_t v = to_le16(bg->free_blocks_count_lo); - if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE) - v |= (uint32_t) to_le16(bg->free_blocks_count_hi) << 16; + if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE) + v |= (uint32_t) to_le16(bg->free_blocks_count_hi) << 16; - return v; + return v; } static inline void ext4_bg_set_free_blocks_count(struct ext4_bgroup *bg, struct ext4_sblock *s, uint32_t cnt) { - bg->free_blocks_count_lo = to_le16((cnt << 16) >> 16); - if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE) - bg->free_blocks_count_hi = to_le16(cnt >> 16); + bg->free_blocks_count_lo = to_le16((cnt << 16) >> 16); + if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE) + bg->free_blocks_count_hi = to_le16(cnt >> 16); } static inline uint32_t ext4_bg_get_free_inodes_count(struct ext4_bgroup *bg, struct ext4_sblock *s) { - uint32_t v = to_le16(bg->free_inodes_count_lo); + uint32_t v = to_le16(bg->free_inodes_count_lo); - if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE) - v |= (uint32_t) to_le16(bg->free_inodes_count_hi) << 16; + if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE) + v |= (uint32_t) to_le16(bg->free_inodes_count_hi) << 16; - return v; + return v; } static inline void ext4_bg_set_free_inodes_count(struct ext4_bgroup *bg, struct ext4_sblock *s, uint32_t cnt) { - bg->free_inodes_count_lo = to_le16((cnt << 16) >> 16); - if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE) - bg->free_inodes_count_hi = to_le16(cnt >> 16); + bg->free_inodes_count_lo = to_le16((cnt << 16) >> 16); + if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE) + bg->free_inodes_count_hi = to_le16(cnt >> 16); } static inline uint32_t ext4_bg_get_used_dirs_count(struct ext4_bgroup *bg, struct ext4_sblock *s) { - uint32_t v = to_le16(bg->used_dirs_count_lo); + uint32_t v = to_le16(bg->used_dirs_count_lo); - if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE) - v |= (uint32_t) to_le16(bg->used_dirs_count_hi) << 16; + if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE) + v |= (uint32_t) to_le16(bg->used_dirs_count_hi) << 16; - return v; + return v; } static inline void ext4_bg_set_used_dirs_count(struct ext4_bgroup *bg, struct ext4_sblock *s, uint32_t cnt) { - bg->used_dirs_count_lo = to_le16((cnt << 16) >> 16); - if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE) - bg->used_dirs_count_hi = to_le16(cnt >> 16); + bg->used_dirs_count_lo = to_le16((cnt << 16) >> 16); + if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE) + bg->used_dirs_count_hi = to_le16(cnt >> 16); } static inline uint32_t ext4_bg_get_itable_unused(struct ext4_bgroup *bg, struct ext4_sblock *s) { - uint32_t v = to_le16(bg->itable_unused_lo); + uint32_t v = to_le16(bg->itable_unused_lo); - if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE) - v |= (uint32_t) to_le16(bg->itable_unused_hi) << 16; + if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE) + v |= (uint32_t) to_le16(bg->itable_unused_hi) << 16; - return v; + return v; } static inline void ext4_bg_set_itable_unused(struct ext4_bgroup *bg, struct ext4_sblock *s, uint32_t cnt) { - bg->itable_unused_lo = to_le16((cnt << 16) >> 16); - if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE) - bg->itable_unused_hi = to_le16(cnt >> 16); + bg->itable_unused_lo = to_le16((cnt << 16) >> 16); + if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE) + bg->itable_unused_hi = to_le16(cnt >> 16); } static inline void ext4_bg_set_checksum(struct ext4_bgroup *bg, uint16_t crc) { - bg->checksum = to_le16(crc); + bg->checksum = to_le16(crc); } static inline bool ext4_bg_has_flag(struct ext4_bgroup *bg, uint32_t f) { - return to_le16(bg->flags) & f; + return to_le16(bg->flags) & f; } static inline void ext4_bg_set_flag(struct ext4_bgroup *bg, uint32_t f) { - uint16_t flags = to_le16(bg->flags); - flags |= f; - bg->flags = to_le16(flags); + uint16_t flags = to_le16(bg->flags); + flags |= f; + bg->flags = to_le16(flags); } static inline void ext4_bg_clear_flag(struct ext4_bgroup *bg, uint32_t f) { - uint16_t flags = to_le16(bg->flags); - flags &= ~f; - bg->flags = to_le16(flags); + uint16_t flags = to_le16(bg->flags); + flags &= ~f; + bg->flags = to_le16(flags); } diff --git a/src/lwext4/ext4_blockdev.c b/src/lwext4/ext4_blockdev.c index 1964a24..1d0286e 100644 --- a/src/lwext4/ext4_blockdev.c +++ b/src/lwext4/ext4_blockdev.c @@ -46,281 +46,281 @@ int ext4_block_init(struct ext4_blockdev *bdev) { - int rc; - ext4_assert(bdev); + int rc; + ext4_assert(bdev); - ext4_assert(bdev->open && bdev->close && bdev->bread && bdev->bwrite); + ext4_assert(bdev->open && bdev->close && bdev->bread && bdev->bwrite); - /*Low level block init*/ - rc = bdev->open(bdev); - if(EOK != rc) - return rc; + /*Low level block init*/ + rc = bdev->open(bdev); + if(EOK != rc) + return rc; - bdev->flags |= EXT4_BDEV_INITIALIZED; + bdev->flags |= EXT4_BDEV_INITIALIZED; - return EOK; + return EOK; } int ext4_block_bind_bcache(struct ext4_blockdev *bdev, struct ext4_bcache *bc) { - ext4_assert(bdev && bc); - bdev->bc = bc; - return EOK; + ext4_assert(bdev && bc); + bdev->bc = bc; + return EOK; } void ext4_block_set_lb_size(struct ext4_blockdev *bdev, uint64_t lb_bsize) { - /*Logical block size has to be multiply of physical */ - ext4_assert(!(lb_bsize % bdev->ph_bsize)); + /*Logical block size has to be multiply of physical */ + ext4_assert(!(lb_bsize % bdev->ph_bsize)); - bdev->lg_bsize = lb_bsize; - bdev->lg_bcnt = (bdev->ph_bcnt * bdev->ph_bsize) / lb_bsize; + bdev->lg_bsize = lb_bsize; + bdev->lg_bcnt = (bdev->ph_bcnt * bdev->ph_bsize) / lb_bsize; } int ext4_block_fini(struct ext4_blockdev *bdev) { - ext4_assert(bdev); + ext4_assert(bdev); - bdev->flags &= ~(EXT4_BDEV_INITIALIZED); + bdev->flags &= ~(EXT4_BDEV_INITIALIZED); - /*Low level block fini*/ - return bdev->close(bdev); + /*Low level block fini*/ + return bdev->close(bdev); } int ext4_block_get(struct ext4_blockdev *bdev, struct ext4_block *b, uint64_t lba) { - uint64_t pba; - uint32_t pb_cnt; - bool is_new; - int r; - ext4_assert(bdev && b); + uint64_t pba; + uint32_t pb_cnt; + bool is_new; + int r; + ext4_assert(bdev && b); - if(!(bdev->flags & EXT4_BDEV_INITIALIZED)) - return EIO; + if(!(bdev->flags & EXT4_BDEV_INITIALIZED)) + return EIO; - if(!(lba < bdev->lg_bcnt)) - return ERANGE; + if(!(lba < bdev->lg_bcnt)) + return ERANGE; - b->dirty = 0; - b->lb_id = lba; + b->dirty = 0; + b->lb_id = lba; - r = ext4_bcache_alloc(bdev->bc, b, &is_new); - if(r != EOK) - return r; + r = ext4_bcache_alloc(bdev->bc, b, &is_new); + if(r != EOK) + return r; - if(!is_new){ - /*Block is in cache. Read from physical device is not required*/ - return EOK; - } + if(!is_new){ + /*Block is in cache. Read from physical device is not required*/ + return EOK; + } - if(!b->data) - return ENOMEM; + if(!b->data) + return ENOMEM; - pba = (lba * bdev->lg_bsize) / bdev->ph_bsize; - pb_cnt = bdev->lg_bsize / bdev->ph_bsize; + pba = (lba * bdev->lg_bsize) / bdev->ph_bsize; + pb_cnt = bdev->lg_bsize / bdev->ph_bsize; - r = bdev->bread(bdev, b->data, pba, pb_cnt); + r = bdev->bread(bdev, b->data, pba, pb_cnt); - if(r != EOK){ - ext4_bcache_free(bdev->bc, b, 0); - b->lb_id = 0; - return r; - } + if(r != EOK){ + ext4_bcache_free(bdev->bc, b, 0); + b->lb_id = 0; + return r; + } - bdev->bread_ctr++; + bdev->bread_ctr++; - return EOK; + return EOK; } int ext4_block_set(struct ext4_blockdev *bdev, struct ext4_block *b) { - uint64_t pba; - uint32_t pb_cnt; - uint32_t i; - int r; + uint64_t pba; + uint32_t pb_cnt; + uint32_t i; + int r; - ext4_assert(bdev && b); + ext4_assert(bdev && b); - if(!(bdev->flags & EXT4_BDEV_INITIALIZED)) - return EIO; + if(!(bdev->flags & EXT4_BDEV_INITIALIZED)) + return EIO; - /*Doesn,t need to write.*/ - if(b->dirty == false){ - ext4_bcache_free(bdev->bc, b, 0); - return EOK; - } + /*Doesn,t need to write.*/ + if(b->dirty == false){ + ext4_bcache_free(bdev->bc, b, 0); + return EOK; + } - b->dirty = false; + b->dirty = false; - /*Free cache delay mode*/ - if(bdev->cache_flush_delay){ + /*Free cache delay mode*/ + if(bdev->cache_flush_delay){ - /*Free cahe block and mark as free delayed*/ - ext4_bcache_free(bdev->bc, b, bdev->cache_flush_delay); + /*Free cahe block and mark as free delayed*/ + ext4_bcache_free(bdev->bc, b, bdev->cache_flush_delay); - /*If cache is full we have to flush it anyway :(*/ - if(ext4_bcache_is_full(bdev->bc)){ - for (i = 0; i < bdev->bc->cnt; ++i) { - /*Check if buffer free was delayed.*/ - if(!bdev->bc->free_delay[i]) - continue; + /*If cache is full we have to flush it anyway :(*/ + if(ext4_bcache_is_full(bdev->bc)){ + for (i = 0; i < bdev->bc->cnt; ++i) { + /*Check if buffer free was delayed.*/ + if(!bdev->bc->free_delay[i]) + continue; - /*Check reference counter.*/ - if(bdev->bc->refctr[i]) - continue; + /*Check reference counter.*/ + if(bdev->bc->refctr[i]) + continue; - /*Buffer free was delayed and have no reference. Flush it.*/ - r = ext4_block_set_direct(bdev, bdev->bc->data + bdev->bc->itemsize * i, bdev->bc->lba[i]); - if(r != EOK) - return r; + /*Buffer free was delayed and have no reference. Flush it.*/ + r = ext4_block_set_direct(bdev, bdev->bc->data + bdev->bc->itemsize * i, bdev->bc->lba[i]); + if(r != EOK) + return r; - /*No delayed anymore*/ - bdev->bc->free_delay[i] = 0; + /*No delayed anymore*/ + bdev->bc->free_delay[i] = 0; - /*Reduce refered block count*/ - bdev->bc->ref_blocks--; - } - } + /*Reduce refered block count*/ + bdev->bc->ref_blocks--; + } + } - return EOK; - } + return EOK; + } - pba = (b->lb_id * bdev->lg_bsize) / bdev->ph_bsize; - pb_cnt = bdev->lg_bsize / bdev->ph_bsize; + pba = (b->lb_id * bdev->lg_bsize) / bdev->ph_bsize; + pb_cnt = bdev->lg_bsize / bdev->ph_bsize; - r = bdev->bwrite(bdev, b->data, pba, pb_cnt); + r = bdev->bwrite(bdev, b->data, pba, pb_cnt); - if(r != EOK){ - b->dirty = false; - ext4_bcache_free(bdev->bc, b, 0); - return r; - } + if(r != EOK){ + b->dirty = false; + ext4_bcache_free(bdev->bc, b, 0); + return r; + } - bdev->bwrite_ctr++; - b->dirty = false; - ext4_bcache_free(bdev->bc, b, 0); - return EOK; + bdev->bwrite_ctr++; + b->dirty = false; + ext4_bcache_free(bdev->bc, b, 0); + return EOK; } int ext4_block_get_direct(struct ext4_blockdev *bdev, void *buf, uint64_t lba) { - uint64_t pba; - uint32_t pb_cnt; + uint64_t pba; + uint32_t pb_cnt; - ext4_assert(bdev && buf); + ext4_assert(bdev && buf); - pba = (lba * bdev->lg_bsize) / bdev->ph_bsize; - pb_cnt = bdev->lg_bsize / bdev->ph_bsize; + pba = (lba * bdev->lg_bsize) / bdev->ph_bsize; + pb_cnt = bdev->lg_bsize / bdev->ph_bsize; - bdev->bread_ctr++; + bdev->bread_ctr++; - return bdev->bread(bdev, buf, pba, pb_cnt); + return bdev->bread(bdev, buf, pba, pb_cnt); } int ext4_block_set_direct(struct ext4_blockdev *bdev, const void *buf, uint64_t lba) { - uint64_t pba; - uint32_t pb_cnt; + uint64_t pba; + uint32_t pb_cnt; - ext4_assert(bdev && buf); + ext4_assert(bdev && buf); - pba = (lba * bdev->lg_bsize) / bdev->ph_bsize; - pb_cnt = bdev->lg_bsize / bdev->ph_bsize; + pba = (lba * bdev->lg_bsize) / bdev->ph_bsize; + pb_cnt = bdev->lg_bsize / bdev->ph_bsize; - bdev->bwrite_ctr++; + bdev->bwrite_ctr++; - return bdev->bwrite(bdev, buf, pba, pb_cnt); + return bdev->bwrite(bdev, buf, pba, pb_cnt); } int ext4_block_writebytes(struct ext4_blockdev *bdev, uint64_t off, const void *buf, uint32_t len) { - uint64_t block_idx; - uint64_t block_end; + uint64_t block_idx; + uint64_t block_end; - uint32_t blen; + uint32_t blen; - uint32_t unalg; - int r = EOK; + uint32_t unalg; + int r = EOK; - const uint8_t *p = (void *)buf; + const uint8_t *p = (void *)buf; - ext4_assert(bdev && buf); + ext4_assert(bdev && buf); - if(!(bdev->flags & EXT4_BDEV_INITIALIZED)) - return EIO; + if(!(bdev->flags & EXT4_BDEV_INITIALIZED)) + return EIO; - block_idx = off / bdev->ph_bsize; - block_end = block_idx + len / bdev->ph_bsize; + block_idx = off / bdev->ph_bsize; + block_end = block_idx + len / bdev->ph_bsize; - if(!(block_end < bdev->ph_bcnt)) - return EINVAL; /*Ups. Out of range operation*/ + if(!(block_end < bdev->ph_bcnt)) + return EINVAL; /*Ups. Out of range operation*/ - /*OK lets deal with the first possible unaligned block*/ - unalg = (off & (bdev->ph_bsize - 1)); - if(unalg){ + /*OK lets deal with the first possible unaligned block*/ + unalg = (off & (bdev->ph_bsize - 1)); + if(unalg){ - uint32_t wlen = (bdev->ph_bsize - unalg) > len ? len : (bdev->ph_bsize - unalg); + uint32_t wlen = (bdev->ph_bsize - unalg) > len ? len : (bdev->ph_bsize - unalg); - r = bdev->bread(bdev, bdev->ph_bbuf, block_idx, 1); + r = bdev->bread(bdev, bdev->ph_bbuf, block_idx, 1); - if(r != EOK) - return r; + if(r != EOK) + return r; - memcpy(bdev->ph_bbuf + unalg, p, wlen); + memcpy(bdev->ph_bbuf + unalg, p, wlen); - r = bdev->bwrite(bdev, bdev->ph_bbuf, block_idx, 1); + r = bdev->bwrite(bdev, bdev->ph_bbuf, block_idx, 1); - if(r != EOK) - return r; + if(r != EOK) + return r; - p += wlen; - len -= wlen; - block_idx++; - } + p += wlen; + len -= wlen; + block_idx++; + } - /*Aligned data*/ - blen = len / bdev->ph_bsize; + /*Aligned data*/ + blen = len / bdev->ph_bsize; - r = bdev->bwrite(bdev, p, block_idx, blen); + r = bdev->bwrite(bdev, p, block_idx, blen); - if(r != EOK) - return r; + if(r != EOK) + return r; - p += bdev->ph_bsize * blen; - len -= bdev->ph_bsize * blen; + p += bdev->ph_bsize * blen; + len -= bdev->ph_bsize * blen; - block_idx += blen; + block_idx += blen; - /*Rest of the data*/ - if(len){ + /*Rest of the data*/ + if(len){ - r = bdev->bread(bdev, bdev->ph_bbuf, block_idx, 1); + r = bdev->bread(bdev, bdev->ph_bbuf, block_idx, 1); - if(r != EOK) - return r; + if(r != EOK) + return r; - memcpy(bdev->ph_bbuf, p, len); + memcpy(bdev->ph_bbuf, p, len); - r = bdev->bwrite(bdev, bdev->ph_bbuf, block_idx, 1); + r = bdev->bwrite(bdev, bdev->ph_bbuf, block_idx, 1); - if(r != EOK) - return r; - } + if(r != EOK) + return r; + } - return r; + return r; } @@ -329,108 +329,108 @@ int ext4_block_writebytes(struct ext4_blockdev *bdev, uint64_t off, const void * int ext4_block_readbytes(struct ext4_blockdev *bdev, uint64_t off, void *buf, uint32_t len) { - uint64_t block_idx; - uint64_t block_end; - uint32_t blen; + uint64_t block_idx; + uint64_t block_end; + uint32_t blen; - uint32_t unalg; - int r = EOK; + uint32_t unalg; + int r = EOK; - uint8_t *p = (void *)buf; + uint8_t *p = (void *)buf; - ext4_assert(bdev && buf); + ext4_assert(bdev && buf); - if(!(bdev->flags & EXT4_BDEV_INITIALIZED)) - return EIO; + if(!(bdev->flags & EXT4_BDEV_INITIALIZED)) + return EIO; - block_idx = off / bdev->ph_bsize; - block_end = block_idx + len / bdev->ph_bsize; + block_idx = off / bdev->ph_bsize; + block_end = block_idx + len / bdev->ph_bsize; - if(!(block_end < bdev->ph_bcnt)) - return EINVAL; /*Ups. Out of range operation*/ + if(!(block_end < bdev->ph_bcnt)) + return EINVAL; /*Ups. Out of range operation*/ - /*OK lets deal with the first possible unaligned block*/ - unalg = (off & (bdev->ph_bsize - 1)); - if(unalg){ + /*OK lets deal with the first possible unaligned block*/ + unalg = (off & (bdev->ph_bsize - 1)); + if(unalg){ - uint32_t rlen = (bdev->ph_bsize - unalg) > len ? len : (bdev->ph_bsize - unalg); + uint32_t rlen = (bdev->ph_bsize - unalg) > len ? len : (bdev->ph_bsize - unalg); - r = bdev->bread(bdev, bdev->ph_bbuf, block_idx, 1); + r = bdev->bread(bdev, bdev->ph_bbuf, block_idx, 1); - if(r != EOK) - return r; + if(r != EOK) + return r; - memcpy(p, bdev->ph_bbuf + unalg, rlen); + memcpy(p, bdev->ph_bbuf + unalg, rlen); - p += rlen; - len -= rlen; - block_idx++; - } + p += rlen; + len -= rlen; + block_idx++; + } - /*Aligned data*/ - blen = len / bdev->ph_bsize; + /*Aligned data*/ + blen = len / bdev->ph_bsize; - r = bdev->bread(bdev, p, block_idx, blen); + r = bdev->bread(bdev, p, block_idx, blen); - if(r != EOK) - return r; + if(r != EOK) + return r; - p += bdev->ph_bsize * blen; - len -= bdev->ph_bsize * blen; + p += bdev->ph_bsize * blen; + len -= bdev->ph_bsize * blen; - block_idx += blen; + block_idx += blen; - /*Rest of the data*/ - if(len){ + /*Rest of the data*/ + if(len){ - r = bdev->bread(bdev, bdev->ph_bbuf, block_idx, 1); + r = bdev->bread(bdev, bdev->ph_bbuf, block_idx, 1); - if(r != EOK) - return r; + if(r != EOK) + return r; - memcpy(p, bdev->ph_bbuf, len); + memcpy(p, bdev->ph_bbuf, len); - } + } - return r; + return r; } int ext4_block_delay_cache_flush(struct ext4_blockdev *bdev, uint8_t on_off) { - int r; - uint32_t i; - bdev->cache_flush_delay = on_off; + int r; + uint32_t i; + bdev->cache_flush_delay = on_off; - /*Flush all delayed cache blocks*/ - if(!on_off){ - for (i = 0; i < bdev->bc->cnt; ++i) { + /*Flush all delayed cache blocks*/ + if(!on_off){ + for (i = 0; i < bdev->bc->cnt; ++i) { - /*Check if buffer free was delayed.*/ - if(!bdev->bc->free_delay[i]) - continue; + /*Check if buffer free was delayed.*/ + if(!bdev->bc->free_delay[i]) + continue; - /*Check reference counter.*/ - if(bdev->bc->refctr[i]) - continue; + /*Check reference counter.*/ + if(bdev->bc->refctr[i]) + continue; - /*Buffer free was delayed and have no reference. Flush it.*/ - r = ext4_block_set_direct(bdev, bdev->bc->data + bdev->bc->itemsize * i, bdev->bc->lba[i]); - if(r != EOK) - return r; + /*Buffer free was delayed and have no reference. Flush it.*/ + r = ext4_block_set_direct(bdev, bdev->bc->data + bdev->bc->itemsize * i, bdev->bc->lba[i]); + if(r != EOK) + return r; - /*No delayed anymore*/ - bdev->bc->free_delay[i] = 0; + /*No delayed anymore*/ + bdev->bc->free_delay[i] = 0; - /*Reduce refered block count*/ - bdev->bc->ref_blocks--; - } - } + /*Reduce refered block count*/ + bdev->bc->ref_blocks--; + } + } - return EOK; + return EOK; } /** diff --git a/src/lwext4/ext4_blockdev.h b/src/lwext4/ext4_blockdev.h index e290283..090f1b8 100644 --- a/src/lwext4/ext4_blockdev.h +++ b/src/lwext4/ext4_blockdev.h @@ -51,67 +51,67 @@ /**@brief Definiton of the simple block device.*/ struct ext4_blockdev { - /**@brief Open device function - * @param bdev block device.*/ - int (*open)(struct ext4_blockdev *bdev); + /**@brief Open device function + * @param bdev block device.*/ + int (*open)(struct ext4_blockdev *bdev); - /**@brief Block read function. - * @param bdev block device - * @param buf output buffer - * @param blk_id block id - * @param blk_cnt block count*/ - int (*bread)(struct ext4_blockdev *bdev, void *buf, uint64_t blk_id, uint32_t blk_cnt); + /**@brief Block read function. + * @param bdev block device + * @param buf output buffer + * @param blk_id block id + * @param blk_cnt block count*/ + int (*bread)(struct ext4_blockdev *bdev, void *buf, uint64_t blk_id, uint32_t blk_cnt); - /**@brief Block write function. - * @param buf input buffer - * @param blk_id block id - * @param blk_cnt block count*/ - int (*bwrite)(struct ext4_blockdev *bdev, const void *buf, uint64_t blk_id, uint32_t blk_cnt); + /**@brief Block write function. + * @param buf input buffer + * @param blk_id block id + * @param blk_cnt block count*/ + int (*bwrite)(struct ext4_blockdev *bdev, const void *buf, uint64_t blk_id, uint32_t blk_cnt); - /**@brief Close device function. - * @param bdev block device.*/ - int (*close)(struct ext4_blockdev *bdev); + /**@brief Close device function. + * @param bdev block device.*/ + int (*close)(struct ext4_blockdev *bdev); - /**@brief Block size (bytes): physical*/ - uint32_t ph_bsize; + /**@brief Block size (bytes): physical*/ + uint32_t ph_bsize; - /**@brief Block count: physical.*/ - uint64_t ph_bcnt; + /**@brief Block count: physical.*/ + uint64_t ph_bcnt; - /**@brief Block size buffer: physical.*/ - uint8_t *ph_bbuf; + /**@brief Block size buffer: physical.*/ + uint8_t *ph_bbuf; - /**@brief Block size (bytes) logical*/ - uint32_t lg_bsize; + /**@brief Block size (bytes) logical*/ + uint32_t lg_bsize; - /**@brief Block count: phisical.*/ - uint64_t lg_bcnt; + /**@brief Block count: phisical.*/ + uint64_t lg_bcnt; - /**@brief Flags of te block device.*/ - uint8_t flags; + /**@brief Flags of te block device.*/ + uint8_t flags; - /**@brief Block cache.*/ - struct ext4_bcache *bc; + /**@brief Block cache.*/ + struct ext4_bcache *bc; - uint8_t cache_flush_delay; + uint8_t cache_flush_delay; - uint32_t bread_ctr; - uint32_t bwrite_ctr; + uint32_t bread_ctr; + uint32_t bwrite_ctr; }; /**@brief Static initialization fo the block device.*/ #define EXT4_BLOCKDEV_STATIC_INSTANCE(__name, __bsize, __bcnt, __open, __bread, __bwrite, __close) \ - static uint8_t __name##_ph_bbuf[(__bsize)]; \ - static struct ext4_blockdev __name = { \ - .open = __open, \ - .bread = __bread, \ - .bwrite = __bwrite, \ - .close = __close, \ - .ph_bsize = __bsize, \ - .ph_bcnt = __bcnt, \ - .ph_bbuf = __name##_ph_bbuf, \ -} + static uint8_t __name##_ph_bbuf[(__bsize)]; \ + static struct ext4_blockdev __name = { \ + .open = __open, \ + .bread = __bread, \ + .bwrite = __bwrite, \ + .close = __close, \ + .ph_bsize = __bsize, \ + .ph_bcnt = __bcnt, \ + .ph_bbuf = __name##_ph_bbuf, \ + } /**@brief Block device initialization. * @param bdev block device descriptor diff --git a/src/lwext4/ext4_debug.c b/src/lwext4/ext4_debug.c index 72f4721..3eeca1a 100644 --- a/src/lwext4/ext4_debug.c +++ b/src/lwext4/ext4_debug.c @@ -45,12 +45,12 @@ static uint32_t __dbg_mask__; void ext4_dmask_set(uint32_t m) { - __dbg_mask__ = m; + __dbg_mask__ = m; } uint32_t ext4_dmask_get(void) { - return __dbg_mask__; + return __dbg_mask__; } /** diff --git a/src/lwext4/ext4_debug.h b/src/lwext4/ext4_debug.h index 2d5036f..a610f40 100644 --- a/src/lwext4/ext4_debug.h +++ b/src/lwext4/ext4_debug.h @@ -88,8 +88,8 @@ uint32_t ext4_dmask_get(void); #if CONFIG_DEBUG_PRINTF /**@brief Debug printf.*/ #define ext4_dprintf(m, ...) do { \ - (m & ext4_dmask_get()) ? printf(__VA_ARGS__) : (void)0; \ - fflush(stdout); \ + (m & ext4_dmask_get()) ? printf(__VA_ARGS__) : (void)0; \ + fflush(stdout); \ }while(0) #else #define ext4_dprintf(m, ...) @@ -99,10 +99,10 @@ uint32_t ext4_dmask_get(void); #if CONFIG_DEBUG_ASSERT #define ext4_assert(_v) do { \ - if(!(_v)){ \ - printf("Assertion failed:\nModule: %s\nFunc: %s\nLine: %d\n", __FILE__, __FUNCTION__, __LINE__); \ - fflush(stdout); \ - } \ + if(!(_v)){ \ + printf("Assertion failed:\nModule: %s\nFunc: %s\nLine: %d\n", __FILE__, __FUNCTION__, __LINE__); \ + fflush(stdout); \ + } \ }while(0) #else #define ext4_assert(_v) diff --git a/src/lwext4/ext4_dir.c b/src/lwext4/ext4_dir.c index 52afd7f..3468b54 100644 --- a/src/lwext4/ext4_dir.c +++ b/src/lwext4/ext4_dir.c @@ -49,68 +49,68 @@ uint32_t ext4_dir_entry_ll_get_inode(struct ext4_directory_entry_ll *de)
{
- return to_le32(de->inode);
+ return to_le32(de->inode);
}
void ext4_dir_entry_ll_set_inode(struct ext4_directory_entry_ll *de,
- uint32_t inode)
+ uint32_t inode)
{
- de->inode = to_le32(inode);
+ de->inode = to_le32(inode);
}
uint16_t ext4_dir_entry_ll_get_entry_length(struct ext4_directory_entry_ll *de)
{
- return to_le16(de->entry_length);
+ return to_le16(de->entry_length);
}
void ext4_dir_entry_ll_set_entry_length(struct ext4_directory_entry_ll *de,
- uint16_t len)
+ uint16_t len)
{
- de->entry_length = to_le16(len);
+ de->entry_length = to_le16(len);
}
uint16_t ext4_dir_entry_ll_get_name_length(struct ext4_sblock *sb,
- struct ext4_directory_entry_ll *de)
+ struct ext4_directory_entry_ll *de)
{
- uint16_t v = de->name_length;
+ uint16_t v = de->name_length;
- if ((ext4_get32(sb, rev_level) == 0) &&
- (ext4_get32(sb, minor_rev_level) < 5))
- v |= ((uint16_t)de->name_length_high) << 8;
+ if ((ext4_get32(sb, rev_level) == 0) &&
+ (ext4_get32(sb, minor_rev_level) < 5))
+ v |= ((uint16_t)de->name_length_high) << 8;
- return v;
+ return v;
}
void ext4_dir_entry_ll_set_name_length(struct ext4_sblock *sb,
- struct ext4_directory_entry_ll *de, uint16_t len)
+ struct ext4_directory_entry_ll *de, uint16_t len)
{
- de->name_length = (len << 8) >> 8;
+ de->name_length = (len << 8) >> 8;
- if ((ext4_get32(sb, rev_level) == 0) &&
- (ext4_get32(sb, minor_rev_level) < 5))
- de->name_length_high = len >> 8;
+ if ((ext4_get32(sb, rev_level) == 0) &&
+ (ext4_get32(sb, minor_rev_level) < 5))
+ de->name_length_high = len >> 8;
}
uint8_t ext4_dir_entry_ll_get_inode_type(struct ext4_sblock *sb,
- struct ext4_directory_entry_ll *de)
+ struct ext4_directory_entry_ll *de)
{
- if ((ext4_get32(sb, rev_level) > 0) ||
- (ext4_get32(sb, minor_rev_level) >= 5))
- return de->inode_type;
+ if ((ext4_get32(sb, rev_level) > 0) ||
+ (ext4_get32(sb, minor_rev_level) >= 5))
+ return de->inode_type;
- return EXT4_DIRECTORY_FILETYPE_UNKNOWN;
+ return EXT4_DIRECTORY_FILETYPE_UNKNOWN;
}
void ext4_dir_entry_ll_set_inode_type(struct ext4_sblock *sb,
- struct ext4_directory_entry_ll *de, uint8_t type)
+ struct ext4_directory_entry_ll *de, uint8_t type)
{
- if ((ext4_get32(sb, rev_level) > 0) ||
- (ext4_get32(sb, minor_rev_level) >= 5))
- de->inode_type = type;
+ if ((ext4_get32(sb, rev_level) > 0) ||
+ (ext4_get32(sb, minor_rev_level) >= 5))
+ de->inode_type = type;
}
/*****************************************************************************/
@@ -118,492 +118,492 @@ void ext4_dir_entry_ll_set_inode_type(struct ext4_sblock *sb, static int ext4_dir_iterator_set(struct ext4_directory_iterator *it, uint32_t block_size)
{
- it->current = NULL;
+ it->current = NULL;
- uint32_t offset_in_block = it->current_offset % block_size;
+ uint32_t offset_in_block = it->current_offset % block_size;
- /* Ensure proper alignment */
- if ((offset_in_block % 4) != 0)
- return EIO;
+ /* Ensure proper alignment */
+ if ((offset_in_block % 4) != 0)
+ return EIO;
- /* Ensure that the core of the entry does not overflow the block */
- if (offset_in_block > block_size - 8)
- return EIO;
+ /* Ensure that the core of the entry does not overflow the block */
+ if (offset_in_block > block_size - 8)
+ return EIO;
- struct ext4_directory_entry_ll *entry =
- (void *)(it->current_block.data + offset_in_block);
+ struct ext4_directory_entry_ll *entry =
+ (void *)(it->current_block.data + offset_in_block);
- /* Ensure that the whole entry does not overflow the block */
- uint16_t length = ext4_dir_entry_ll_get_entry_length(entry);
- if (offset_in_block + length > block_size)
- return EIO;
+ /* Ensure that the whole entry does not overflow the block */
+ uint16_t length = ext4_dir_entry_ll_get_entry_length(entry);
+ if (offset_in_block + length > block_size)
+ return EIO;
- /* Ensure the name length is not too large */
- if (ext4_dir_entry_ll_get_name_length(
- &it->inode_ref->fs->sb, entry) > length-8)
- return EIO;
+ /* Ensure the name length is not too large */
+ if (ext4_dir_entry_ll_get_name_length(
+ &it->inode_ref->fs->sb, entry) > length-8)
+ return EIO;
- /* Everything OK - "publish" the entry */
- it->current = entry;
- return EOK;
+ /* Everything OK - "publish" the entry */
+ it->current = entry;
+ return EOK;
}
static int ext4_dir_iterator_seek(struct ext4_directory_iterator *it, uint64_t pos)
{
- uint64_t size = ext4_inode_get_size(&it->inode_ref->fs->sb,
- it->inode_ref->inode);
-
- /* The iterator is not valid until we seek to the desired position */
- it->current = NULL;
-
- /* Are we at the end? */
- if (pos >= size) {
- if (it->current_block.lb_id) {
-
- int rc = ext4_block_set(it->inode_ref->fs->bdev, &it->current_block);
- it->current_block.lb_id = 0;
-
- if (rc != EOK)
- return rc;
- }
-
- it->current_offset = pos;
- return EOK;
- }
-
- /* Compute next block address */
- uint32_t block_size =
- ext4_sb_get_block_size(&it->inode_ref->fs->sb);
- uint64_t current_block_idx = it->current_offset / block_size;
- uint64_t next_block_idx = pos / block_size;
-
- /*
- * If we don't have a block or are moving accross block boundary,
- * we need to get another block
- */
- if ((it->current_block.lb_id == 0) ||
- (current_block_idx != next_block_idx)) {
- if (it->current_block.lb_id) {
- int rc = ext4_block_set(it->inode_ref->fs->bdev, &it->current_block);
- it->current_block.lb_id = 0;
-
- if (rc != EOK)
- return rc;
- }
-
- uint32_t next_block_phys_idx;
- int rc = ext4_fs_get_inode_data_block_index(it->inode_ref,
- next_block_idx, &next_block_phys_idx);
- if (rc != EOK)
- return rc;
-
-
- rc = ext4_block_get(it->inode_ref->fs->bdev, &it->current_block,
- next_block_phys_idx);
- if (rc != EOK) {
- it->current_block.lb_id = 0;
- return rc;
- }
- }
-
- it->current_offset = pos;
-
- return ext4_dir_iterator_set(it, block_size);
+ uint64_t size = ext4_inode_get_size(&it->inode_ref->fs->sb,
+ it->inode_ref->inode);
+
+ /* The iterator is not valid until we seek to the desired position */
+ it->current = NULL;
+
+ /* Are we at the end? */
+ if (pos >= size) {
+ if (it->current_block.lb_id) {
+
+ int rc = ext4_block_set(it->inode_ref->fs->bdev, &it->current_block);
+ it->current_block.lb_id = 0;
+
+ if (rc != EOK)
+ return rc;
+ }
+
+ it->current_offset = pos;
+ return EOK;
+ }
+
+ /* Compute next block address */
+ uint32_t block_size =
+ ext4_sb_get_block_size(&it->inode_ref->fs->sb);
+ uint64_t current_block_idx = it->current_offset / block_size;
+ uint64_t next_block_idx = pos / block_size;
+
+ /*
+ * If we don't have a block or are moving accross block boundary,
+ * we need to get another block
+ */
+ if ((it->current_block.lb_id == 0) ||
+ (current_block_idx != next_block_idx)) {
+ if (it->current_block.lb_id) {
+ int rc = ext4_block_set(it->inode_ref->fs->bdev, &it->current_block);
+ it->current_block.lb_id = 0;
+
+ if (rc != EOK)
+ return rc;
+ }
+
+ uint32_t next_block_phys_idx;
+ int rc = ext4_fs_get_inode_data_block_index(it->inode_ref,
+ next_block_idx, &next_block_phys_idx);
+ if (rc != EOK)
+ return rc;
+
+
+ rc = ext4_block_get(it->inode_ref->fs->bdev, &it->current_block,
+ next_block_phys_idx);
+ if (rc != EOK) {
+ it->current_block.lb_id = 0;
+ return rc;
+ }
+ }
+
+ it->current_offset = pos;
+
+ return ext4_dir_iterator_set(it, block_size);
}
int ext4_dir_iterator_init(struct ext4_directory_iterator *it,
- struct ext4_inode_ref *inode_ref, uint64_t pos)
+ struct ext4_inode_ref *inode_ref, uint64_t pos)
{
- it->inode_ref = inode_ref;
- it->current = 0;
- it->current_offset = 0;
- it->current_block.lb_id = 0;
+ it->inode_ref = inode_ref;
+ it->current = 0;
+ it->current_offset = 0;
+ it->current_block.lb_id = 0;
- return ext4_dir_iterator_seek(it, pos);
+ return ext4_dir_iterator_seek(it, pos);
}
int ext4_dir_iterator_next(struct ext4_directory_iterator *it)
{
- uint16_t skip = ext4_dir_entry_ll_get_entry_length(it->current);
+ uint16_t skip = ext4_dir_entry_ll_get_entry_length(it->current);
- return ext4_dir_iterator_seek(it, it->current_offset + skip);
+ return ext4_dir_iterator_seek(it, it->current_offset + skip);
}
int ext4_dir_iterator_fini(struct ext4_directory_iterator *it)
{
- it->current = 0;
+ it->current = 0;
- if (it->current_block.lb_id)
- return ext4_block_set(it->inode_ref->fs->bdev, &it->current_block);
+ if (it->current_block.lb_id)
+ return ext4_block_set(it->inode_ref->fs->bdev, &it->current_block);
- return EOK;
+ return EOK;
}
void ext4_dir_write_entry(struct ext4_sblock *sb, struct ext4_directory_entry_ll *entry,
- uint16_t entry_len, struct ext4_inode_ref *child, const char *name, size_t name_len)
+ uint16_t entry_len, struct ext4_inode_ref *child, const char *name, size_t name_len)
{
- /* Check maximum entry length */
- uint32_t block_size = ext4_sb_get_block_size(sb);
- ext4_assert(entry_len <= block_size);
-
- /* Set basic attributes */
- ext4_dir_entry_ll_set_inode(entry, child->index);
- ext4_dir_entry_ll_set_entry_length(entry, entry_len);
- ext4_dir_entry_ll_set_name_length(sb, entry, name_len);
-
- /* Write name */
- memcpy(entry->name, name, name_len);
-
- /* Set type of entry */
- if (ext4_inode_is_type(sb, child->inode, EXT4_INODE_MODE_DIRECTORY))
- ext4_dir_entry_ll_set_inode_type(sb, entry,
- EXT4_DIRECTORY_FILETYPE_DIR);
- else
- ext4_dir_entry_ll_set_inode_type(sb, entry,
- EXT4_DIRECTORY_FILETYPE_REG_FILE);
+ /* Check maximum entry length */
+ uint32_t block_size = ext4_sb_get_block_size(sb);
+ ext4_assert(entry_len <= block_size);
+
+ /* Set basic attributes */
+ ext4_dir_entry_ll_set_inode(entry, child->index);
+ ext4_dir_entry_ll_set_entry_length(entry, entry_len);
+ ext4_dir_entry_ll_set_name_length(sb, entry, name_len);
+
+ /* Write name */
+ memcpy(entry->name, name, name_len);
+
+ /* Set type of entry */
+ if (ext4_inode_is_type(sb, child->inode, EXT4_INODE_MODE_DIRECTORY))
+ ext4_dir_entry_ll_set_inode_type(sb, entry,
+ EXT4_DIRECTORY_FILETYPE_DIR);
+ else
+ ext4_dir_entry_ll_set_inode_type(sb, entry,
+ EXT4_DIRECTORY_FILETYPE_REG_FILE);
}
int ext4_dir_add_entry(struct ext4_inode_ref *parent, const char *name,
- struct ext4_inode_ref *child)
+ struct ext4_inode_ref *child)
{
- struct ext4_fs *fs = parent->fs;
+ struct ext4_fs *fs = parent->fs;
#if CONFIG_DIR_INDEX_ENABLE
- /* Index adding (if allowed) */
- if ((ext4_sb_check_feature_compatible(&fs->sb,
- EXT4_FEATURE_COMPAT_DIR_INDEX)) &&
- (ext4_inode_has_flag(parent->inode, EXT4_INODE_FLAG_INDEX))) {
- int rc = 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;
-
- return EOK;
- }
-
- /* Needed to clear dir index flag if corrupted */
- ext4_inode_clear_flag(parent->inode, EXT4_INODE_FLAG_INDEX);
- parent->dirty = true;
- }
+ /* Index adding (if allowed) */
+ if ((ext4_sb_check_feature_compatible(&fs->sb,
+ EXT4_FEATURE_COMPAT_DIR_INDEX)) &&
+ (ext4_inode_has_flag(parent->inode, EXT4_INODE_FLAG_INDEX))) {
+ int rc = 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;
+
+ return EOK;
+ }
+
+ /* Needed to clear dir index flag if corrupted */
+ ext4_inode_clear_flag(parent->inode, EXT4_INODE_FLAG_INDEX);
+ parent->dirty = true;
+ }
#endif
- /* Linear algorithm */
- uint32_t iblock = 0;
- uint32_t fblock = 0;
- uint32_t block_size = ext4_sb_get_block_size(&fs->sb);
- uint32_t inode_size = ext4_inode_get_size(&fs->sb, parent->inode);
- uint32_t total_blocks = inode_size / block_size;
-
- uint32_t name_len = strlen(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);
- if (rc != EOK)
- return rc;
-
-
- struct ext4_block block;
- rc = ext4_block_get(fs->bdev, &block, fblock);
- if (rc != EOK)
- return rc;
-
- /* If adding is successful, function can finish */
- rc = ext4_dir_try_insert_entry(&fs->sb, &block,
- child, name, name_len);
- if (rc == EOK)
- success = true;
-
- rc = ext4_block_set(fs->bdev, &block);
- if (rc != EOK)
- return rc;
-
- if (success)
- return EOK;
- }
-
- /* No free block found - needed to allocate next data block */
-
- iblock = 0;
- fblock = 0;
- int rc = ext4_fs_append_inode_block(parent, &fblock, &iblock);
- if (rc != EOK)
- return rc;
-
- /* Load new block */
- struct ext4_block new_block;
-
- rc = ext4_block_get(fs->bdev, &new_block, fblock);
- if (rc != EOK)
- return rc;
-
- /* Fill block with zeroes */
- memset(new_block.data, 0, block_size);
- struct ext4_directory_entry_ll *block_entry = (void *)new_block.data;
- ext4_dir_write_entry(&fs->sb, block_entry, block_size,
- child, name, name_len);
-
- /* Save new block */
- new_block.dirty = true;
- rc = ext4_block_set(fs->bdev, &new_block);
-
- return rc;
+ /* Linear algorithm */
+ uint32_t iblock = 0;
+ uint32_t fblock = 0;
+ uint32_t block_size = ext4_sb_get_block_size(&fs->sb);
+ uint32_t inode_size = ext4_inode_get_size(&fs->sb, parent->inode);
+ uint32_t total_blocks = inode_size / block_size;
+
+ uint32_t name_len = strlen(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);
+ if (rc != EOK)
+ return rc;
+
+
+ struct ext4_block block;
+ rc = ext4_block_get(fs->bdev, &block, fblock);
+ if (rc != EOK)
+ return rc;
+
+ /* If adding is successful, function can finish */
+ rc = ext4_dir_try_insert_entry(&fs->sb, &block,
+ child, name, name_len);
+ if (rc == EOK)
+ success = true;
+
+ rc = ext4_block_set(fs->bdev, &block);
+ if (rc != EOK)
+ return rc;
+
+ if (success)
+ return EOK;
+ }
+
+ /* No free block found - needed to allocate next data block */
+
+ iblock = 0;
+ fblock = 0;
+ int rc = ext4_fs_append_inode_block(parent, &fblock, &iblock);
+ if (rc != EOK)
+ return rc;
+
+ /* Load new block */
+ struct ext4_block new_block;
+
+ rc = ext4_block_get(fs->bdev, &new_block, fblock);
+ if (rc != EOK)
+ return rc;
+
+ /* Fill block with zeroes */
+ memset(new_block.data, 0, block_size);
+ struct ext4_directory_entry_ll *block_entry = (void *)new_block.data;
+ ext4_dir_write_entry(&fs->sb, block_entry, block_size,
+ child, name, name_len);
+
+ /* Save new block */
+ new_block.dirty = true;
+ rc = ext4_block_set(fs->bdev, &new_block);
+
+ return rc;
}
int ext4_dir_find_entry(struct ext4_directory_search_result *result,
- struct ext4_inode_ref *parent, const char *name)
+ struct ext4_inode_ref *parent, const char *name)
{
- uint32_t name_len = strlen(name);
+ uint32_t name_len = strlen(name);
- struct ext4_sblock *sb = &parent->fs->sb;
+ struct ext4_sblock *sb = &parent->fs->sb;
#if CONFIG_DIR_INDEX_ENABLE
- /* Index search */
- if ((ext4_sb_check_feature_compatible(sb,
- EXT4_FEATURE_COMPAT_DIR_INDEX)) &&
- (ext4_inode_has_flag(parent->inode, EXT4_INODE_FLAG_INDEX))) {
- int rc = 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;
-
- return EOK;
- }
-
- /* Needed to clear dir index flag if corrupted */
- ext4_inode_clear_flag(parent->inode, EXT4_INODE_FLAG_INDEX);
- parent->dirty = true;
- }
+ /* Index search */
+ if ((ext4_sb_check_feature_compatible(sb,
+ EXT4_FEATURE_COMPAT_DIR_INDEX)) &&
+ (ext4_inode_has_flag(parent->inode, EXT4_INODE_FLAG_INDEX))) {
+ int rc = 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;
+
+ return EOK;
+ }
+
+ /* Needed to clear dir index flag if corrupted */
+ ext4_inode_clear_flag(parent->inode, EXT4_INODE_FLAG_INDEX);
+ parent->dirty = true;
+ }
#endif
- /* Linear algorithm */
-
- uint32_t iblock;
- uint32_t fblock;
- uint32_t block_size = ext4_sb_get_block_size(sb);
- uint32_t inode_size = ext4_inode_get_size(sb, parent->inode);
- uint32_t total_blocks = inode_size / block_size;
-
- /* 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);
- if (rc != EOK)
- return rc;
-
- /* Load data block */
- struct ext4_block block;
- rc = ext4_block_get( parent->fs->bdev, &block, fblock);
- if (rc != EOK)
- return rc;
-
- /* Try to find entry in block */
- struct ext4_directory_entry_ll *res_entry;
- rc = ext4_dir_find_in_block(&block, sb, name_len, name,
- &res_entry);
- if (rc == EOK) {
- result->block = block;
- result->dentry = res_entry;
- return EOK;
- }
-
- /* Entry not found - put block and continue to the next block */
-
- rc = ext4_block_set(parent->fs->bdev, &block);
- if (rc != EOK)
- return rc;
- }
-
- /* Entry was not found */
-
- result->block.lb_id = 0;
- result->dentry = NULL;
-
- return ENOENT;
+ /* Linear algorithm */
+
+ uint32_t iblock;
+ uint32_t fblock;
+ uint32_t block_size = ext4_sb_get_block_size(sb);
+ uint32_t inode_size = ext4_inode_get_size(sb, parent->inode);
+ uint32_t total_blocks = inode_size / block_size;
+
+ /* 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);
+ if (rc != EOK)
+ return rc;
+
+ /* Load data block */
+ struct ext4_block block;
+ rc = ext4_block_get( parent->fs->bdev, &block, fblock);
+ if (rc != EOK)
+ return rc;
+
+ /* Try to find entry in block */
+ struct ext4_directory_entry_ll *res_entry;
+ rc = ext4_dir_find_in_block(&block, sb, name_len, name,
+ &res_entry);
+ if (rc == EOK) {
+ result->block = block;
+ result->dentry = res_entry;
+ return EOK;
+ }
+
+ /* Entry not found - put block and continue to the next block */
+
+ rc = ext4_block_set(parent->fs->bdev, &block);
+ if (rc != EOK)
+ return rc;
+ }
+
+ /* Entry was not found */
+
+ result->block.lb_id = 0;
+ result->dentry = NULL;
+
+ return ENOENT;
}
int ext4_dir_remove_entry(struct ext4_inode_ref *parent, const char *name)
{
- /* Check if removing from directory */
- if (!ext4_inode_is_type(&parent->fs->sb, parent->inode,
- EXT4_INODE_MODE_DIRECTORY))
- return ENOTDIR;
-
- /* Try to find entry */
- struct ext4_directory_search_result result;
- int rc = ext4_dir_find_entry(&result, parent, name);
- if (rc != EOK)
- return rc;
-
- /* Invalidate entry */
- ext4_dir_entry_ll_set_inode(result.dentry, 0);
-
- /* Store entry position in block */
- uint32_t pos = (uint8_t *) result.dentry - result.block.data;
-
- /*
- * If entry is not the first in block, it must be merged
- * with previous entry
- */
- if (pos != 0) {
- uint32_t offset = 0;
-
- /* Start from the first entry in block */
- struct ext4_directory_entry_ll *tmp_dentry = (void *)result.block.data;
- uint16_t tmp_dentry_length =
- ext4_dir_entry_ll_get_entry_length(tmp_dentry);
-
- /* Find direct predecessor of removed entry */
- while ((offset + tmp_dentry_length) < pos) {
- offset +=
- ext4_dir_entry_ll_get_entry_length(tmp_dentry);
- tmp_dentry = (void *)(result.block.data + offset);
- tmp_dentry_length =
- ext4_dir_entry_ll_get_entry_length(tmp_dentry);
- }
-
- ext4_assert(tmp_dentry_length + offset == pos);
-
- /* Add to removed entry length to predecessor's length */
- uint16_t del_entry_length =
- ext4_dir_entry_ll_get_entry_length(result.dentry);
- ext4_dir_entry_ll_set_entry_length(tmp_dentry,
- tmp_dentry_length + del_entry_length);
- }
-
- result.block.dirty = true;
-
- return ext4_dir_destroy_result(parent, &result);
+ /* Check if removing from directory */
+ if (!ext4_inode_is_type(&parent->fs->sb, parent->inode,
+ EXT4_INODE_MODE_DIRECTORY))
+ return ENOTDIR;
+
+ /* Try to find entry */
+ struct ext4_directory_search_result result;
+ int rc = ext4_dir_find_entry(&result, parent, name);
+ if (rc != EOK)
+ return rc;
+
+ /* Invalidate entry */
+ ext4_dir_entry_ll_set_inode(result.dentry, 0);
+
+ /* Store entry position in block */
+ uint32_t pos = (uint8_t *) result.dentry - result.block.data;
+
+ /*
+ * If entry is not the first in block, it must be merged
+ * with previous entry
+ */
+ if (pos != 0) {
+ uint32_t offset = 0;
+
+ /* Start from the first entry in block */
+ struct ext4_directory_entry_ll *tmp_dentry = (void *)result.block.data;
+ uint16_t tmp_dentry_length =
+ ext4_dir_entry_ll_get_entry_length(tmp_dentry);
+
+ /* Find direct predecessor of removed entry */
+ while ((offset + tmp_dentry_length) < pos) {
+ offset +=
+ ext4_dir_entry_ll_get_entry_length(tmp_dentry);
+ tmp_dentry = (void *)(result.block.data + offset);
+ tmp_dentry_length =
+ ext4_dir_entry_ll_get_entry_length(tmp_dentry);
+ }
+
+ ext4_assert(tmp_dentry_length + offset == pos);
+
+ /* Add to removed entry length to predecessor's length */
+ uint16_t del_entry_length =
+ ext4_dir_entry_ll_get_entry_length(result.dentry);
+ ext4_dir_entry_ll_set_entry_length(tmp_dentry,
+ tmp_dentry_length + del_entry_length);
+ }
+
+ result.block.dirty = true;
+
+ return ext4_dir_destroy_result(parent, &result);
}
int ext4_dir_try_insert_entry(struct ext4_sblock *sb, struct ext4_block *target_block,
- struct ext4_inode_ref *child, const char *name, uint32_t name_len)
+ struct ext4_inode_ref *child, const char *name, uint32_t name_len)
{
- /* Compute required length entry and align it to 4 bytes */
- uint32_t block_size = ext4_sb_get_block_size(sb);
- uint16_t required_len = sizeof(struct ext4_fake_directory_entry) + name_len;
-
- if ((required_len % 4) != 0)
- required_len += 4 - (required_len % 4);
-
- /* Initialize pointers, stop means to upper bound */
- struct ext4_directory_entry_ll *dentry = (void *)target_block->data;
- struct ext4_directory_entry_ll *stop = (void *)(target_block->data + block_size);
-
- /*
- * Walk through the block and check for invalid entries
- * or entries with free space for new entry
- */
- while (dentry < stop) {
- uint32_t inode = ext4_dir_entry_ll_get_inode(dentry);
- uint16_t rec_len = ext4_dir_entry_ll_get_entry_length(dentry);
-
- /* If invalid and large enough entry, use it */
- if ((inode == 0) && (rec_len >= required_len)) {
- ext4_dir_write_entry(sb, dentry, rec_len, child,
- name, name_len);
- target_block->dirty = true;
-
- return EOK;
- }
-
- /* Valid entry, try to split it */
- if (inode != 0) {
- uint16_t used_name_len =
- ext4_dir_entry_ll_get_name_length(sb, dentry);
-
- uint16_t used_space =
- sizeof(struct ext4_fake_directory_entry) + used_name_len;
-
- if ((used_name_len % 4) != 0)
- used_space += 4 - (used_name_len % 4);
-
- uint16_t free_space = rec_len - used_space;
-
- /* There is free space for new entry */
- if (free_space >= required_len) {
- /* Cut tail of current entry */
- ext4_dir_entry_ll_set_entry_length(dentry, used_space);
- struct ext4_directory_entry_ll *new_entry =
- (void *) dentry + used_space;
- ext4_dir_write_entry(sb, new_entry,
- free_space, child, name, name_len);
-
- target_block->dirty = true;
-
- return EOK;
- }
- }
-
- /* Jump to the next entry */
- dentry = (void *) dentry + rec_len;
- }
-
- /* No free space found for new entry */
- return ENOSPC;
+ /* Compute required length entry and align it to 4 bytes */
+ uint32_t block_size = ext4_sb_get_block_size(sb);
+ uint16_t required_len = sizeof(struct ext4_fake_directory_entry) + name_len;
+
+ if ((required_len % 4) != 0)
+ required_len += 4 - (required_len % 4);
+
+ /* Initialize pointers, stop means to upper bound */
+ struct ext4_directory_entry_ll *dentry = (void *)target_block->data;
+ struct ext4_directory_entry_ll *stop = (void *)(target_block->data + block_size);
+
+ /*
+ * Walk through the block and check for invalid entries
+ * or entries with free space for new entry
+ */
+ while (dentry < stop) {
+ uint32_t inode = ext4_dir_entry_ll_get_inode(dentry);
+ uint16_t rec_len = ext4_dir_entry_ll_get_entry_length(dentry);
+
+ /* If invalid and large enough entry, use it */
+ if ((inode == 0) && (rec_len >= required_len)) {
+ ext4_dir_write_entry(sb, dentry, rec_len, child,
+ name, name_len);
+ target_block->dirty = true;
+
+ return EOK;
+ }
+
+ /* Valid entry, try to split it */
+ if (inode != 0) {
+ uint16_t used_name_len =
+ ext4_dir_entry_ll_get_name_length(sb, dentry);
+
+ uint16_t used_space =
+ sizeof(struct ext4_fake_directory_entry) + used_name_len;
+
+ if ((used_name_len % 4) != 0)
+ used_space += 4 - (used_name_len % 4);
+
+ uint16_t free_space = rec_len - used_space;
+
+ /* There is free space for new entry */
+ if (free_space >= required_len) {
+ /* Cut tail of current entry */
+ ext4_dir_entry_ll_set_entry_length(dentry, used_space);
+ struct ext4_directory_entry_ll *new_entry =
+ (void *) dentry + used_space;
+ ext4_dir_write_entry(sb, new_entry,
+ free_space, child, name, name_len);
+
+ target_block->dirty = true;
+
+ return EOK;
+ }
+ }
+
+ /* Jump to the next entry */
+ dentry = (void *) dentry + rec_len;
+ }
+
+ /* No free space found for new entry */
+ return ENOSPC;
}
int ext4_dir_find_in_block(struct ext4_block *block, struct ext4_sblock *sb,
- size_t name_len, const char *name, struct ext4_directory_entry_ll **res_entry)
+ size_t name_len, const char *name, struct ext4_directory_entry_ll **res_entry)
{
- /* Start from the first entry in block */
- struct ext4_directory_entry_ll *dentry =
- (struct ext4_directory_entry_ll *) block->data;
-
- /* Set upper bound for cycling */
- uint8_t *addr_limit = block->data + ext4_sb_get_block_size(sb);
-
- /* Walk through the block and check entries */
- while ((uint8_t *) dentry < addr_limit) {
- /* Termination condition */
- if ((uint8_t *) dentry + name_len > addr_limit)
- break;
-
- /* Valid entry - check it */
- if (dentry->inode != 0) {
- /* For more effectivity compare firstly only lengths */
- if (ext4_dir_entry_ll_get_name_length(sb, dentry) ==
- name_len) {
- /* Compare names */
- if (memcmp((uint8_t *) name, dentry->name, name_len) == 0) {
- *res_entry = dentry;
- return EOK;
- }
- }
- }
-
- uint16_t dentry_len =
- ext4_dir_entry_ll_get_entry_length(dentry);
-
- /* Corrupted entry */
- if (dentry_len == 0)
- return EINVAL;
-
- /* Jump to next entry */
- dentry = (struct ext4_directory_entry_ll *) ((uint8_t *) dentry + dentry_len);
- }
-
- /* Entry not found */
- return ENOENT;
+ /* Start from the first entry in block */
+ struct ext4_directory_entry_ll *dentry =
+ (struct ext4_directory_entry_ll *) block->data;
+
+ /* Set upper bound for cycling */
+ uint8_t *addr_limit = block->data + ext4_sb_get_block_size(sb);
+
+ /* Walk through the block and check entries */
+ while ((uint8_t *) dentry < addr_limit) {
+ /* Termination condition */
+ if ((uint8_t *) dentry + name_len > addr_limit)
+ break;
+
+ /* Valid entry - check it */
+ if (dentry->inode != 0) {
+ /* For more effectivity compare firstly only lengths */
+ if (ext4_dir_entry_ll_get_name_length(sb, dentry) ==
+ name_len) {
+ /* Compare names */
+ if (memcmp((uint8_t *) name, dentry->name, name_len) == 0) {
+ *res_entry = dentry;
+ return EOK;
+ }
+ }
+ }
+
+ uint16_t dentry_len =
+ ext4_dir_entry_ll_get_entry_length(dentry);
+
+ /* Corrupted entry */
+ if (dentry_len == 0)
+ return EINVAL;
+
+ /* Jump to next entry */
+ dentry = (struct ext4_directory_entry_ll *) ((uint8_t *) dentry + dentry_len);
+ }
+
+ /* Entry not found */
+ return ENOENT;
}
int ext4_dir_destroy_result(struct ext4_inode_ref *parent, struct ext4_directory_search_result *result)
{
- if (result->block.lb_id)
- return ext4_block_set(parent->fs->bdev, &result->block);
+ if (result->block.lb_id)
+ return ext4_block_set(parent->fs->bdev, &result->block);
- return EOK;
+ return EOK;
}
/**
diff --git a/src/lwext4/ext4_dir.h b/src/lwext4/ext4_dir.h index ae27c4d..7578ca3 100644 --- a/src/lwext4/ext4_dir.h +++ b/src/lwext4/ext4_dir.h @@ -53,49 +53,49 @@ uint32_t ext4_dir_entry_ll_get_inode(struct ext4_directory_entry_ll *de);
void ext4_dir_entry_ll_set_inode(struct ext4_directory_entry_ll *de,
- uint32_t inode);
+ uint32_t inode);
uint16_t ext4_dir_entry_ll_get_entry_length(struct ext4_directory_entry_ll *de);
void ext4_dir_entry_ll_set_entry_length(struct ext4_directory_entry_ll *de,
- uint16_t len);
+ uint16_t len);
uint16_t ext4_dir_entry_ll_get_name_length(struct ext4_sblock *sb,
- struct ext4_directory_entry_ll *de);
+ struct ext4_directory_entry_ll *de);
void ext4_dir_entry_ll_set_name_length(struct ext4_sblock *sb,
- struct ext4_directory_entry_ll *de, uint16_t len);
+ struct ext4_directory_entry_ll *de, uint16_t len);
uint8_t ext4_dir_entry_ll_get_inode_type(struct ext4_sblock *sb,
- struct ext4_directory_entry_ll *de);
+ struct ext4_directory_entry_ll *de);
void ext4_dir_entry_ll_set_inode_type(struct ext4_sblock *sb,
- struct ext4_directory_entry_ll *de, uint8_t type);
+ struct ext4_directory_entry_ll *de, uint8_t type);
int ext4_dir_iterator_init(struct ext4_directory_iterator *it,
- struct ext4_inode_ref *inode_ref, uint64_t pos);
+ struct ext4_inode_ref *inode_ref, uint64_t pos);
int ext4_dir_iterator_next(struct ext4_directory_iterator *it);
int ext4_dir_iterator_fini(struct ext4_directory_iterator *it);
void ext4_dir_write_entry(struct ext4_sblock *sb, struct ext4_directory_entry_ll *entry,
- uint16_t entry_len, struct ext4_inode_ref *child, const char *name, size_t name_len);
+ uint16_t entry_len, struct ext4_inode_ref *child, const char *name, size_t name_len);
int ext4_dir_add_entry(struct ext4_inode_ref *parent, const char *name,
- struct ext4_inode_ref *child);
+ struct ext4_inode_ref *child);
int ext4_dir_find_entry(struct ext4_directory_search_result *result,
- struct ext4_inode_ref *parent, const char *name);
+ struct ext4_inode_ref *parent, const char *name);
int ext4_dir_remove_entry(struct ext4_inode_ref *parent, const char *name);
int ext4_dir_try_insert_entry(struct ext4_sblock *sb, struct ext4_block *target_block,
- struct ext4_inode_ref *child, const char *name, uint32_t name_len);
+ struct ext4_inode_ref *child, const char *name, uint32_t name_len);
int ext4_dir_find_in_block(struct ext4_block *block, struct ext4_sblock *sb,
- size_t name_len, const char *name, struct ext4_directory_entry_ll **res_entry);
+ size_t name_len, const char *name, struct ext4_directory_entry_ll **res_entry);
int ext4_dir_destroy_result(struct ext4_inode_ref *parent, struct ext4_directory_search_result *result);
diff --git a/src/lwext4/ext4_dir_idx.c b/src/lwext4/ext4_dir_idx.c index 3724292..fe1fe8f 100644 --- a/src/lwext4/ext4_dir_idx.c +++ b/src/lwext4/ext4_dir_idx.c @@ -46,689 +46,689 @@ #include <stdlib.h>
struct ext4_dx_sort_entry {
- uint32_t hash;
- uint32_t rec_len;
- void *dentry;
+ uint32_t hash;
+ uint32_t rec_len;
+ void *dentry;
};
static int ext4_dir_dx_hash_string(struct ext4_hash_info *hinfo, int len, const char *name)
{
- return ext2_htree_hash(name, len, hinfo->seed, hinfo->hash_version, &hinfo->hash, &hinfo->minor_hash);
+ return ext2_htree_hash(name, len, hinfo->seed, hinfo->hash_version, &hinfo->hash, &hinfo->minor_hash);
}
uint8_t ext4_dir_dx_root_info_get_hash_version(
- struct ext4_directory_dx_root_info *root_info)
+ struct ext4_directory_dx_root_info *root_info)
{
- return root_info->hash_version;
+ return root_info->hash_version;
}
void ext4_dir_dx_root_info_set_hash_version(
- struct ext4_directory_dx_root_info *root_info, uint8_t v)
+ struct ext4_directory_dx_root_info *root_info, uint8_t v)
{
- root_info->hash_version = v;
+ root_info->hash_version = v;
}
uint8_t ext4_dir_dx_root_info_get_info_length(
- struct ext4_directory_dx_root_info *root_info)
+ struct ext4_directory_dx_root_info *root_info)
{
- return root_info->info_length;
+ return root_info->info_length;
}
void ext4_dir_dx_root_info_set_info_length(
- struct ext4_directory_dx_root_info *root_info, uint8_t len)
+ struct ext4_directory_dx_root_info *root_info, uint8_t len)
{
- root_info->info_length = len;
+ root_info->info_length = len;
}
uint8_t ext4_dir_dx_root_info_get_indirect_levels(
- struct ext4_directory_dx_root_info *root_info)
+ struct ext4_directory_dx_root_info *root_info)
{
- return root_info->indirect_levels;
+ return root_info->indirect_levels;
}
void ext4_dir_dx_root_info_set_indirect_levels(
- struct ext4_directory_dx_root_info *root_info, uint8_t lvl)
+ struct ext4_directory_dx_root_info *root_info, uint8_t lvl)
{
- root_info->indirect_levels = lvl;
+ root_info->indirect_levels = lvl;
}
uint16_t ext4_dir_dx_countlimit_get_limit(
- struct ext4_directory_dx_countlimit *climit)
+ struct ext4_directory_dx_countlimit *climit)
{
- return to_le16(climit->limit);
+ return to_le16(climit->limit);
}
void ext4_dir_dx_countlimit_set_limit(
- struct ext4_directory_dx_countlimit *climit, uint16_t limit)
+ struct ext4_directory_dx_countlimit *climit, uint16_t limit)
{
- climit->limit = to_le16(limit);
+ climit->limit = to_le16(limit);
}
uint16_t ext4_dir_dx_countlimit_get_count(
- struct ext4_directory_dx_countlimit *climit)
+ struct ext4_directory_dx_countlimit *climit)
{
- return to_le16(climit->count);
+ return to_le16(climit->count);
}
void ext4_dir_dx_countlimit_set_count(
- struct ext4_directory_dx_countlimit *climit, uint16_t count)
+ struct ext4_directory_dx_countlimit *climit, uint16_t count)
{
- climit->count = to_le16(count);
+ climit->count = to_le16(count);
}
uint32_t ext4_dir_dx_entry_get_hash(
- struct ext4_directory_dx_entry *entry)
+ struct ext4_directory_dx_entry *entry)
{
- return to_le32(entry->hash);
+ return to_le32(entry->hash);
}
void ext4_dir_dx_entry_set_hash(
- struct ext4_directory_dx_entry *entry, uint32_t hash)
+ struct ext4_directory_dx_entry *entry, uint32_t hash)
{
- entry->hash = to_le32(hash);
+ entry->hash = to_le32(hash);
}
uint32_t ext4_dir_dx_entry_get_block(
- struct ext4_directory_dx_entry *entry)
+ struct ext4_directory_dx_entry *entry)
{
- return to_le32(entry->block);
+ return to_le32(entry->block);
}
void ext4_dir_dx_entry_set_block(
- struct ext4_directory_dx_entry *entry, uint32_t block)
+ struct ext4_directory_dx_entry *entry, uint32_t block)
{
- entry->block = to_le32(block);
+ entry->block = to_le32(block);
}
/*****************************************************************************/
int ext4_dir_dx_init(struct ext4_inode_ref *dir)
{
- /* Load block 0, where will be index root located */
- uint32_t fblock;
- int rc = ext4_fs_get_inode_data_block_index(dir, 0,
- &fblock);
- if (rc != EOK)
- return rc;
-
- struct ext4_block block;
- rc = ext4_block_get(dir->fs->bdev, &block, fblock);
- if (rc != EOK)
- return rc;
-
- /* Initialize pointers to data structures */
- struct ext4_directory_dx_root *root = (void *)block.data;
- struct ext4_directory_dx_root_info *info = &(root->info);
-
- /* Initialize root info structure */
- uint8_t hash_version = ext4_get8(&dir->fs->sb, default_hash_version);
-
-
- ext4_dir_dx_root_info_set_hash_version(info, hash_version);
- ext4_dir_dx_root_info_set_indirect_levels(info, 0);
- ext4_dir_dx_root_info_set_info_length(info, 8);
-
- /* Set limit and current number of entries */
- struct ext4_directory_dx_countlimit *countlimit =
- (struct ext4_directory_dx_countlimit *) &root->entries;
-
- ext4_dir_dx_countlimit_set_count(countlimit, 1);
-
- uint32_t block_size =
- ext4_sb_get_block_size(&dir->fs->sb);
- uint32_t entry_space =
- block_size - 2 * sizeof(struct ext4_directory_dx_dot_entry) -
- sizeof(struct ext4_directory_dx_root_info);
- uint16_t root_limit = entry_space / sizeof(struct ext4_directory_dx_entry);
- ext4_dir_dx_countlimit_set_limit(countlimit, root_limit);
-
- /* Append new block, where will be new entries inserted in the future */
- uint32_t iblock;
- rc = ext4_fs_append_inode_block(dir, &fblock, &iblock);
- if (rc != EOK) {
- ext4_block_set(dir->fs->bdev, &block);
- return rc;
- }
-
- struct ext4_block new_block;
-
- rc = ext4_block_get(dir->fs->bdev, &new_block, fblock);
- if (rc != EOK) {
- ext4_block_set(dir->fs->bdev, &block);
- return rc;
- }
-
- /* Fill the whole block with empty entry */
- struct ext4_directory_entry_ll *block_entry = (void *)new_block.data;
-
- ext4_dir_entry_ll_set_entry_length(block_entry, block_size);
- ext4_dir_entry_ll_set_inode(block_entry, 0);
-
- new_block.dirty = true;
- rc = ext4_block_set(dir->fs->bdev, &new_block);
- if (rc != EOK) {
- ext4_block_set(dir->fs->bdev, &block);
- return rc;
- }
-
- /* Connect new block to the only entry in index */
- struct ext4_directory_dx_entry *entry = root->entries;
- ext4_dir_dx_entry_set_block(entry, iblock);
-
- block.dirty = true;
-
- return ext4_block_set(dir->fs->bdev, &block);
+ /* Load block 0, where will be index root located */
+ uint32_t fblock;
+ int rc = ext4_fs_get_inode_data_block_index(dir, 0,
+ &fblock);
+ if (rc != EOK)
+ return rc;
+
+ struct ext4_block block;
+ rc = ext4_block_get(dir->fs->bdev, &block, fblock);
+ if (rc != EOK)
+ return rc;
+
+ /* Initialize pointers to data structures */
+ struct ext4_directory_dx_root *root = (void *)block.data;
+ struct ext4_directory_dx_root_info *info = &(root->info);
+
+ /* Initialize root info structure */
+ uint8_t hash_version = ext4_get8(&dir->fs->sb, default_hash_version);
+
+
+ ext4_dir_dx_root_info_set_hash_version(info, hash_version);
+ ext4_dir_dx_root_info_set_indirect_levels(info, 0);
+ ext4_dir_dx_root_info_set_info_length(info, 8);
+
+ /* Set limit and current number of entries */
+ struct ext4_directory_dx_countlimit *countlimit =
+ (struct ext4_directory_dx_countlimit *) &root->entries;
+
+ ext4_dir_dx_countlimit_set_count(countlimit, 1);
+
+ uint32_t block_size =
+ ext4_sb_get_block_size(&dir->fs->sb);
+ uint32_t entry_space =
+ block_size - 2 * sizeof(struct ext4_directory_dx_dot_entry) -
+ sizeof(struct ext4_directory_dx_root_info);
+ uint16_t root_limit = entry_space / sizeof(struct ext4_directory_dx_entry);
+ ext4_dir_dx_countlimit_set_limit(countlimit, root_limit);
+
+ /* Append new block, where will be new entries inserted in the future */
+ uint32_t iblock;
+ rc = ext4_fs_append_inode_block(dir, &fblock, &iblock);
+ if (rc != EOK) {
+ ext4_block_set(dir->fs->bdev, &block);
+ return rc;
+ }
+
+ struct ext4_block new_block;
+
+ rc = ext4_block_get(dir->fs->bdev, &new_block, fblock);
+ if (rc != EOK) {
+ ext4_block_set(dir->fs->bdev, &block);
+ return rc;
+ }
+
+ /* Fill the whole block with empty entry */
+ struct ext4_directory_entry_ll *block_entry = (void *)new_block.data;
+
+ ext4_dir_entry_ll_set_entry_length(block_entry, block_size);
+ ext4_dir_entry_ll_set_inode(block_entry, 0);
+
+ new_block.dirty = true;
+ rc = ext4_block_set(dir->fs->bdev, &new_block);
+ if (rc != EOK) {
+ ext4_block_set(dir->fs->bdev, &block);
+ return rc;
+ }
+
+ /* Connect new block to the only entry in index */
+ struct ext4_directory_dx_entry *entry = root->entries;
+ ext4_dir_dx_entry_set_block(entry, iblock);
+
+ block.dirty = true;
+
+ return ext4_block_set(dir->fs->bdev, &block);
}
static int ext4_dir_hinfo_init(struct ext4_hash_info *hinfo,
- struct ext4_block *root_block, struct ext4_sblock *sb, size_t name_len,
- const char *name)
+ struct ext4_block *root_block, struct ext4_sblock *sb, size_t name_len,
+ const char *name)
{
- struct ext4_directory_dx_root *root =
- (struct ext4_directory_dx_root *) root_block->data;
-
- if ((root->info.hash_version != EXT2_HTREE_LEGACY) &&
- (root->info.hash_version != EXT2_HTREE_HALF_MD4) &&
- (root->info.hash_version != EXT2_HTREE_TEA))
- return EXT4_ERR_BAD_DX_DIR;
-
- /* Check unused flags */
- if (root->info.unused_flags != 0)
- return EXT4_ERR_BAD_DX_DIR;
-
- /* Check indirect levels */
- if (root->info.indirect_levels > 1)
- return EXT4_ERR_BAD_DX_DIR;
-
- /* Check if node limit is correct */
- uint32_t block_size = ext4_sb_get_block_size(sb);
- uint32_t entry_space = block_size;
- entry_space -= 2 * sizeof(struct ext4_directory_dx_dot_entry);
- entry_space -= sizeof(struct ext4_directory_dx_root_info);
- entry_space = entry_space / sizeof(struct ext4_directory_dx_entry);
-
- uint16_t limit = ext4_dir_dx_countlimit_get_limit(
- (struct ext4_directory_dx_countlimit *) &root->entries);
- if (limit != entry_space)
- return EXT4_ERR_BAD_DX_DIR;
-
- /* Check hash version and modify if necessary */
- hinfo->hash_version =
- ext4_dir_dx_root_info_get_hash_version(&root->info);
- if ((hinfo->hash_version <= EXT2_HTREE_TEA) &&
- (ext4_sb_check_flag(sb, EXT4_SUPERBLOCK_FLAGS_UNSIGNED_HASH))) {
- /* Use unsigned hash */
- hinfo->hash_version += 3;
- }
-
- /* Load hash seed from superblock */
-
- hinfo->seed = ext4_get8(sb, hash_seed);
-
- /* Compute hash value of name */
- if (name)
- return ext4_dir_dx_hash_string(hinfo, name_len, name);
-
- return EOK;
+ struct ext4_directory_dx_root *root =
+ (struct ext4_directory_dx_root *) root_block->data;
+
+ if ((root->info.hash_version != EXT2_HTREE_LEGACY) &&
+ (root->info.hash_version != EXT2_HTREE_HALF_MD4) &&
+ (root->info.hash_version != EXT2_HTREE_TEA))
+ return EXT4_ERR_BAD_DX_DIR;
+
+ /* Check unused flags */
+ if (root->info.unused_flags != 0)
+ return EXT4_ERR_BAD_DX_DIR;
+
+ /* Check indirect levels */
+ if (root->info.indirect_levels > 1)
+ return EXT4_ERR_BAD_DX_DIR;
+
+ /* Check if node limit is correct */
+ uint32_t block_size = ext4_sb_get_block_size(sb);
+ uint32_t entry_space = block_size;
+ entry_space -= 2 * sizeof(struct ext4_directory_dx_dot_entry);
+ entry_space -= sizeof(struct ext4_directory_dx_root_info);
+ entry_space = entry_space / sizeof(struct ext4_directory_dx_entry);
+
+ uint16_t limit = ext4_dir_dx_countlimit_get_limit(
+ (struct ext4_directory_dx_countlimit *) &root->entries);
+ if (limit != entry_space)
+ return EXT4_ERR_BAD_DX_DIR;
+
+ /* Check hash version and modify if necessary */
+ hinfo->hash_version =
+ ext4_dir_dx_root_info_get_hash_version(&root->info);
+ if ((hinfo->hash_version <= EXT2_HTREE_TEA) &&
+ (ext4_sb_check_flag(sb, EXT4_SUPERBLOCK_FLAGS_UNSIGNED_HASH))) {
+ /* Use unsigned hash */
+ hinfo->hash_version += 3;
+ }
+
+ /* Load hash seed from superblock */
+
+ hinfo->seed = ext4_get8(sb, hash_seed);
+
+ /* Compute hash value of name */
+ if (name)
+ return ext4_dir_dx_hash_string(hinfo, name_len, name);
+
+ return EOK;
}
static int ext4_dir_dx_get_leaf(struct ext4_hash_info *hinfo,
- struct ext4_inode_ref *inode_ref, struct ext4_block *root_block,
- struct ext4_directory_dx_block **dx_block, struct ext4_directory_dx_block *dx_blocks)
+ struct ext4_inode_ref *inode_ref, struct ext4_block *root_block,
+ struct ext4_directory_dx_block **dx_block, struct ext4_directory_dx_block *dx_blocks)
{
- struct ext4_directory_dx_block *tmp_dx_block = dx_blocks;
- struct ext4_directory_dx_root *root =
- (struct ext4_directory_dx_root *) root_block->data;
- struct ext4_directory_dx_entry *entries =
- (struct ext4_directory_dx_entry *) &root->entries;
-
- uint16_t limit = ext4_dir_dx_countlimit_get_limit(
- (struct ext4_directory_dx_countlimit *) entries);
- uint8_t indirect_level =
- ext4_dir_dx_root_info_get_indirect_levels(&root->info);
-
- struct ext4_block *tmp_block = root_block;
- struct ext4_directory_dx_entry *p;
- struct ext4_directory_dx_entry *q;
- struct ext4_directory_dx_entry *m;
- struct ext4_directory_dx_entry *at;
-
- /* Walk through the index tree */
- while (true) {
- uint16_t count = ext4_dir_dx_countlimit_get_count(
- (struct ext4_directory_dx_countlimit *) entries);
- if ((count == 0) || (count > limit))
- return EXT4_ERR_BAD_DX_DIR;
-
- /* Do binary search in every node */
- p = entries + 1;
- q = entries + count - 1;
-
- while (p <= q) {
- m = p + (q - p) / 2;
- if (ext4_dir_dx_entry_get_hash(m) > hinfo->hash)
- q = m - 1;
- else
- p = m + 1;
- }
-
- at = p - 1;
-
- /* Write results */
-
- memcpy(&tmp_dx_block->block, tmp_block, sizeof(struct ext4_block));
- tmp_dx_block->entries = entries;
- tmp_dx_block->position = at;
-
- /* Is algorithm in the leaf? */
- if (indirect_level == 0) {
- *dx_block = tmp_dx_block;
- return EOK;
- }
-
- /* Goto child node */
- uint32_t next_block = ext4_dir_dx_entry_get_block(at);
-
- indirect_level--;
-
- uint32_t fblock;
- int rc = ext4_fs_get_inode_data_block_index(inode_ref,
- next_block, &fblock);
- if (rc != EOK)
- return rc;
-
- rc = ext4_block_get(inode_ref->fs->bdev, tmp_block, fblock);
- if (rc != EOK)
- return rc;
-
- entries = ((struct ext4_directory_dx_node *) tmp_block->data)->entries;
- limit = ext4_dir_dx_countlimit_get_limit(
- (struct ext4_directory_dx_countlimit *) entries);
-
- uint16_t entry_space =
- ext4_sb_get_block_size(&inode_ref->fs->sb) -
- sizeof(struct ext4_directory_dx_dot_entry);
-
- entry_space = entry_space / sizeof(struct ext4_directory_dx_entry);
-
- if (limit != entry_space) {
- ext4_block_set(inode_ref->fs->bdev, tmp_block);
- return EXT4_ERR_BAD_DX_DIR;
- }
-
- ++tmp_dx_block;
- }
-
- /* Unreachable */
- return EOK;
+ struct ext4_directory_dx_block *tmp_dx_block = dx_blocks;
+ struct ext4_directory_dx_root *root =
+ (struct ext4_directory_dx_root *) root_block->data;
+ struct ext4_directory_dx_entry *entries =
+ (struct ext4_directory_dx_entry *) &root->entries;
+
+ uint16_t limit = ext4_dir_dx_countlimit_get_limit(
+ (struct ext4_directory_dx_countlimit *) entries);
+ uint8_t indirect_level =
+ ext4_dir_dx_root_info_get_indirect_levels(&root->info);
+
+ struct ext4_block *tmp_block = root_block;
+ struct ext4_directory_dx_entry *p;
+ struct ext4_directory_dx_entry *q;
+ struct ext4_directory_dx_entry *m;
+ struct ext4_directory_dx_entry *at;
+
+ /* Walk through the index tree */
+ while (true) {
+ uint16_t count = ext4_dir_dx_countlimit_get_count(
+ (struct ext4_directory_dx_countlimit *) entries);
+ if ((count == 0) || (count > limit))
+ return EXT4_ERR_BAD_DX_DIR;
+
+ /* Do binary search in every node */
+ p = entries + 1;
+ q = entries + count - 1;
+
+ while (p <= q) {
+ m = p + (q - p) / 2;
+ if (ext4_dir_dx_entry_get_hash(m) > hinfo->hash)
+ q = m - 1;
+ else
+ p = m + 1;
+ }
+
+ at = p - 1;
+
+ /* Write results */
+
+ memcpy(&tmp_dx_block->block, tmp_block, sizeof(struct ext4_block));
+ tmp_dx_block->entries = entries;
+ tmp_dx_block->position = at;
+
+ /* Is algorithm in the leaf? */
+ if (indirect_level == 0) {
+ *dx_block = tmp_dx_block;
+ return EOK;
+ }
+
+ /* Goto child node */
+ uint32_t next_block = ext4_dir_dx_entry_get_block(at);
+
+ indirect_level--;
+
+ uint32_t fblock;
+ int rc = ext4_fs_get_inode_data_block_index(inode_ref,
+ next_block, &fblock);
+ if (rc != EOK)
+ return rc;
+
+ rc = ext4_block_get(inode_ref->fs->bdev, tmp_block, fblock);
+ if (rc != EOK)
+ return rc;
+
+ entries = ((struct ext4_directory_dx_node *) tmp_block->data)->entries;
+ limit = ext4_dir_dx_countlimit_get_limit(
+ (struct ext4_directory_dx_countlimit *) entries);
+
+ uint16_t entry_space =
+ ext4_sb_get_block_size(&inode_ref->fs->sb) -
+ sizeof(struct ext4_directory_dx_dot_entry);
+
+ entry_space = entry_space / sizeof(struct ext4_directory_dx_entry);
+
+ if (limit != entry_space) {
+ ext4_block_set(inode_ref->fs->bdev, tmp_block);
+ return EXT4_ERR_BAD_DX_DIR;
+ }
+
+ ++tmp_dx_block;
+ }
+
+ /* Unreachable */
+ return EOK;
}
static int ext4_dir_dx_next_block(struct ext4_inode_ref *inode_ref,
- uint32_t hash, struct ext4_directory_dx_block *dx_block,
- struct ext4_directory_dx_block *dx_blocks)
+ uint32_t hash, struct ext4_directory_dx_block *dx_block,
+ struct ext4_directory_dx_block *dx_blocks)
{
- uint32_t num_handles = 0;
- struct ext4_directory_dx_block *p = dx_block;
+ uint32_t num_handles = 0;
+ struct ext4_directory_dx_block *p = dx_block;
- /* Try to find data block with next bunch of entries */
- while (true) {
- p->position++;
- uint16_t count = ext4_dir_dx_countlimit_get_count(
- (struct ext4_directory_dx_countlimit *) p->entries);
+ /* Try to find data block with next bunch of entries */
+ while (true) {
+ p->position++;
+ uint16_t count = ext4_dir_dx_countlimit_get_count(
+ (struct ext4_directory_dx_countlimit *) p->entries);
- if (p->position < p->entries + count)
- break;
+ if (p->position < p->entries + count)
+ break;
- if (p == dx_blocks)
- return EOK;
+ if (p == dx_blocks)
+ return EOK;
- num_handles++;
- p--;
- }
+ num_handles++;
+ p--;
+ }
- /* Check hash collision (if not occured - no next block cannot be used) */
- uint32_t current_hash = ext4_dir_dx_entry_get_hash(p->position);
- if ((hash & 1) == 0) {
- if ((current_hash & ~1) != hash)
- return 0;
- }
+ /* Check hash collision (if not occured - no next block cannot be used) */
+ uint32_t current_hash = ext4_dir_dx_entry_get_hash(p->position);
+ if ((hash & 1) == 0) {
+ if ((current_hash & ~1) != hash)
+ return 0;
+ }
- /* Fill new path */
- while (num_handles--) {
- uint32_t block_idx =
- ext4_dir_dx_entry_get_block(p->position);
- uint32_t block_addr;
+ /* Fill new path */
+ while (num_handles--) {
+ uint32_t block_idx =
+ ext4_dir_dx_entry_get_block(p->position);
+ uint32_t block_addr;
- int rc = ext4_fs_get_inode_data_block_index(inode_ref,
- block_idx, &block_addr);
- if (rc != EOK)
- return rc;
+ int rc = ext4_fs_get_inode_data_block_index(inode_ref,
+ block_idx, &block_addr);
+ if (rc != EOK)
+ return rc;
- struct ext4_block block;
- rc = ext4_block_get(inode_ref->fs->bdev, &block, block_addr);
- if (rc != EOK)
- return rc;
+ struct ext4_block block;
+ rc = ext4_block_get(inode_ref->fs->bdev, &block, block_addr);
+ if (rc != EOK)
+ return rc;
- p++;
+ p++;
- /* Don't forget to put old block (prevent memory leak) */
- ext4_block_set(inode_ref->fs->bdev, &p->block);
+ /* Don't forget to put old block (prevent memory leak) */
+ ext4_block_set(inode_ref->fs->bdev, &p->block);
- memcpy(&p->block, &p->block, sizeof(block));
- p->entries = ((struct ext4_directory_dx_node *) block.data)->entries;
- p->position = p->entries;
- }
+ memcpy(&p->block, &p->block, sizeof(block));
+ p->entries = ((struct ext4_directory_dx_node *) block.data)->entries;
+ p->position = p->entries;
+ }
- return ENOENT;
+ return ENOENT;
}
int ext4_dir_dx_find_entry(struct ext4_directory_search_result * result,
- struct ext4_inode_ref *inode_ref, size_t name_len, const char *name)
+ struct ext4_inode_ref *inode_ref, size_t name_len, const char *name)
{
- /* Load direct block 0 (index root) */
- uint32_t root_block_addr;
- int rc = ext4_fs_get_inode_data_block_index(inode_ref, 0,
- &root_block_addr);
- if (rc != EOK)
- return rc;
-
- struct ext4_fs *fs = inode_ref->fs;
-
- struct ext4_block root_block;
- rc = ext4_block_get(fs->bdev, &root_block, root_block_addr);
- if (rc != EOK)
- return rc;
-
- /* Initialize hash info (compute hash value) */
- struct ext4_hash_info hinfo;
- rc = ext4_dir_hinfo_init(&hinfo, &root_block, &fs->sb,
- name_len, name);
- if (rc != EOK) {
- ext4_block_set(fs->bdev, &root_block);
- return EXT4_ERR_BAD_DX_DIR;
- }
-
- /*
- * Hardcoded number 2 means maximum height of index tree,
- * specified in the Linux driver.
- */
- struct ext4_directory_dx_block dx_blocks[2];
- struct ext4_directory_dx_block *dx_block;
- struct ext4_directory_dx_block *tmp;
-
- rc = ext4_dir_dx_get_leaf(&hinfo, inode_ref, &root_block,
- &dx_block, dx_blocks);
- if (rc != EOK) {
- ext4_block_set(fs->bdev, &root_block);
- return EXT4_ERR_BAD_DX_DIR;
- }
-
- do {
- /* Load leaf block */
- uint32_t leaf_block_idx =
- ext4_dir_dx_entry_get_block(dx_block->position);
- uint32_t leaf_block_addr;
-
- rc = ext4_fs_get_inode_data_block_index(inode_ref,
- leaf_block_idx, &leaf_block_addr);
- if (rc != EOK)
- goto cleanup;
-
- struct ext4_block leaf_block;
- rc = ext4_block_get(fs->bdev, &leaf_block, leaf_block_addr);
- if (rc != EOK)
- goto cleanup;
-
- /* Linear search inside block */
- struct ext4_directory_entry_ll *res_dentry;
- rc = ext4_dir_find_in_block(&leaf_block, &fs->sb,
- name_len, name, &res_dentry);
-
- /* Found => return it */
- if (rc == EOK) {
- result->block = leaf_block;
- result->dentry = res_dentry;
- goto cleanup;
- }
-
- /* Not found, leave untouched */
- ext4_block_set(fs->bdev, &leaf_block);
-
- if (rc != ENOENT)
- goto cleanup;
-
- /* check if the next block could be checked */
- rc = ext4_dir_dx_next_block(inode_ref, hinfo.hash,
- dx_block, &dx_blocks[0]);
- if (rc < 0)
- goto cleanup;
- } while (rc == ENOENT);
-
- /* Entry not found */
- rc = ENOENT;
-
-cleanup:
- /* The whole path must be released (preventing memory leak) */
- tmp = dx_blocks;
-
- while (tmp <= dx_block) {
- ext4_block_set(fs->bdev, &tmp->block);
- ++tmp;
- }
-
- return rc;
+ /* Load direct block 0 (index root) */
+ uint32_t root_block_addr;
+ int rc = ext4_fs_get_inode_data_block_index(inode_ref, 0,
+ &root_block_addr);
+ if (rc != EOK)
+ return rc;
+
+ struct ext4_fs *fs = inode_ref->fs;
+
+ struct ext4_block root_block;
+ rc = ext4_block_get(fs->bdev, &root_block, root_block_addr);
+ if (rc != EOK)
+ return rc;
+
+ /* Initialize hash info (compute hash value) */
+ struct ext4_hash_info hinfo;
+ rc = ext4_dir_hinfo_init(&hinfo, &root_block, &fs->sb,
+ name_len, name);
+ if (rc != EOK) {
+ ext4_block_set(fs->bdev, &root_block);
+ return EXT4_ERR_BAD_DX_DIR;
+ }
+
+ /*
+ * Hardcoded number 2 means maximum height of index tree,
+ * specified in the Linux driver.
+ */
+ struct ext4_directory_dx_block dx_blocks[2];
+ struct ext4_directory_dx_block *dx_block;
+ struct ext4_directory_dx_block *tmp;
+
+ rc = ext4_dir_dx_get_leaf(&hinfo, inode_ref, &root_block,
+ &dx_block, dx_blocks);
+ if (rc != EOK) {
+ ext4_block_set(fs->bdev, &root_block);
+ return EXT4_ERR_BAD_DX_DIR;
+ }
+
+ do {
+ /* Load leaf block */
+ uint32_t leaf_block_idx =
+ ext4_dir_dx_entry_get_block(dx_block->position);
+ uint32_t leaf_block_addr;
+
+ rc = ext4_fs_get_inode_data_block_index(inode_ref,
+ leaf_block_idx, &leaf_block_addr);
+ if (rc != EOK)
+ goto cleanup;
+
+ struct ext4_block leaf_block;
+ rc = ext4_block_get(fs->bdev, &leaf_block, leaf_block_addr);
+ if (rc != EOK)
+ goto cleanup;
+
+ /* Linear search inside block */
+ struct ext4_directory_entry_ll *res_dentry;
+ rc = ext4_dir_find_in_block(&leaf_block, &fs->sb,
+ name_len, name, &res_dentry);
+
+ /* Found => return it */
+ if (rc == EOK) {
+ result->block = leaf_block;
+ result->dentry = res_dentry;
+ goto cleanup;
+ }
+
+ /* Not found, leave untouched */
+ ext4_block_set(fs->bdev, &leaf_block);
+
+ if (rc != ENOENT)
+ goto cleanup;
+
+ /* check if the next block could be checked */
+ rc = ext4_dir_dx_next_block(inode_ref, hinfo.hash,
+ dx_block, &dx_blocks[0]);
+ if (rc < 0)
+ goto cleanup;
+ } while (rc == ENOENT);
+
+ /* Entry not found */
+ rc = ENOENT;
+
+ cleanup:
+ /* The whole path must be released (preventing memory leak) */
+ tmp = dx_blocks;
+
+ while (tmp <= dx_block) {
+ ext4_block_set(fs->bdev, &tmp->block);
+ ++tmp;
+ }
+
+ return rc;
}
static int ext4_dir_dx_entry_comparator(const void *arg1, const void *arg2)
{
- struct ext4_dx_sort_entry *entry1 = (void *)arg1;
- struct ext4_dx_sort_entry *entry2 = (void *)arg2;
+ struct ext4_dx_sort_entry *entry1 = (void *)arg1;
+ struct ext4_dx_sort_entry *entry2 = (void *)arg2;
- if (entry1->hash == entry2->hash)
- return 0;
+ if (entry1->hash == entry2->hash)
+ return 0;
- if (entry1->hash < entry2->hash)
- return -1;
- else
- return 1;
+ if (entry1->hash < entry2->hash)
+ return -1;
+ else
+ return 1;
}
static void ext4_dir_dx_insert_entry(
- struct ext4_directory_dx_block *index_block, uint32_t hash, uint32_t iblock)
+ struct ext4_directory_dx_block *index_block, uint32_t hash, uint32_t iblock)
{
- struct ext4_directory_dx_entry *old_index_entry = index_block->position;
- struct ext4_directory_dx_entry *new_index_entry = old_index_entry + 1;
+ struct ext4_directory_dx_entry *old_index_entry = index_block->position;
+ struct ext4_directory_dx_entry *new_index_entry = old_index_entry + 1;
- struct ext4_directory_dx_countlimit *countlimit =
- (struct ext4_directory_dx_countlimit *) index_block->entries;
- uint32_t count = ext4_dir_dx_countlimit_get_count(countlimit);
+ struct ext4_directory_dx_countlimit *countlimit =
+ (struct ext4_directory_dx_countlimit *) index_block->entries;
+ uint32_t count = ext4_dir_dx_countlimit_get_count(countlimit);
- struct ext4_directory_dx_entry *start_index = index_block->entries;
- size_t bytes = (void *) (start_index + count) - (void *) (new_index_entry);
+ struct ext4_directory_dx_entry *start_index = index_block->entries;
+ size_t bytes = (void *) (start_index + count) - (void *) (new_index_entry);
- memmove(new_index_entry + 1, new_index_entry, bytes);
+ memmove(new_index_entry + 1, new_index_entry, bytes);
- ext4_dir_dx_entry_set_block(new_index_entry, iblock);
- ext4_dir_dx_entry_set_hash(new_index_entry, hash);
+ ext4_dir_dx_entry_set_block(new_index_entry, iblock);
+ ext4_dir_dx_entry_set_hash(new_index_entry, hash);
- ext4_dir_dx_countlimit_set_count(countlimit, count + 1);
+ ext4_dir_dx_countlimit_set_count(countlimit, count + 1);
- index_block->block.dirty = true;
+ index_block->block.dirty = true;
}
static int ext4_dir_dx_split_data(struct ext4_inode_ref *inode_ref,
- struct ext4_hash_info *hinfo, struct ext4_block *old_data_block,
- struct ext4_directory_dx_block *index_block, struct ext4_block *new_data_block)
+ struct ext4_hash_info *hinfo, struct ext4_block *old_data_block,
+ struct ext4_directory_dx_block *index_block, struct ext4_block *new_data_block)
{
- int rc = EOK;
-
- /* Allocate buffer for directory entries */
- uint32_t block_size =
- ext4_sb_get_block_size(&inode_ref->fs->sb);
-
- void *entry_buffer = malloc(block_size);
- if (entry_buffer == NULL)
- return ENOMEM;
-
- /* dot entry has the smallest size available */
- uint32_t max_entry_count =
- block_size / sizeof(struct ext4_directory_dx_dot_entry);
-
- /* Allocate sort entry */
- struct ext4_dx_sort_entry *sort_array =
- malloc(max_entry_count * sizeof(struct ext4_dx_sort_entry));
-
- if (sort_array == NULL) {
- free(entry_buffer);
- return ENOMEM;
- }
-
- uint32_t idx = 0;
- uint32_t real_size = 0;
-
- /* Initialize hinfo */
- struct ext4_hash_info tmp_hinfo;
- memcpy(&tmp_hinfo, hinfo, sizeof(struct ext4_hash_info));
-
- /* Load all valid entries to the buffer */
- struct ext4_directory_entry_ll *dentry = (void *)old_data_block->data;
- void *entry_buffer_ptr = entry_buffer;
- while ((void *)dentry < (void *)(old_data_block->data + block_size)) {
- /* Read only valid entries */
- if (ext4_dir_entry_ll_get_inode(dentry) != 0) {
- uint8_t len = ext4_dir_entry_ll_get_name_length(
- &inode_ref->fs->sb, dentry);
-
- rc = ext4_dir_dx_hash_string(&tmp_hinfo, len, (char *) dentry->name);
- if(rc != EOK) {
- free(sort_array);
- free(entry_buffer);
- return rc;
- }
-
- uint32_t rec_len = 8 + len;
-
- if ((rec_len % 4) != 0)
- rec_len += 4 - (rec_len % 4);
-
- memcpy(entry_buffer_ptr, dentry, rec_len);
-
- sort_array[idx].dentry = entry_buffer_ptr;
- sort_array[idx].rec_len = rec_len;
- sort_array[idx].hash = tmp_hinfo.hash;
-
- entry_buffer_ptr += rec_len;
- real_size += rec_len;
- idx++;
- }
-
- dentry = (void *) dentry +
- ext4_dir_entry_ll_get_entry_length(dentry);
- }
-
- /* Sort all entries */
- qsort(sort_array, idx, sizeof(struct ext4_dx_sort_entry),
- ext4_dir_dx_entry_comparator);
-
- /* Allocate new block for store the second part of entries */
- uint32_t new_fblock;
- uint32_t new_iblock;
- rc = ext4_fs_append_inode_block(inode_ref, &new_fblock,
- &new_iblock);
- if (rc != EOK) {
- free(sort_array);
- free(entry_buffer);
- return rc;
- }
-
- /* Load new block */
- struct ext4_block new_data_block_tmp;
- rc = ext4_block_get(inode_ref->fs->bdev, &new_data_block_tmp,
- new_fblock);
- if (rc != EOK) {
- free(sort_array);
- free(entry_buffer);
- return rc;
- }
-
- /*
- * Distribute entries to two blocks (by size)
- * - compute the half
- */
- uint32_t new_hash = 0;
- uint32_t current_size = 0;
- uint32_t mid = 0;
- uint32_t i;
- for ( i = 0; i < idx; ++i) {
- if ((current_size + sort_array[i].rec_len) > (real_size / 2)) {
- new_hash = sort_array[i].hash;
- mid = i;
- break;
- }
-
- current_size += sort_array[i].rec_len;
- }
-
- /* Check hash collision */
- uint32_t continued = 0;
- if (new_hash == sort_array[mid-1].hash)
- continued = 1;
-
- uint32_t offset = 0;
- void *ptr;
-
- /* First part - to the old block */
- for (i = 0; i < mid; ++i) {
- ptr = old_data_block->data + offset;
- memcpy(ptr, sort_array[i].dentry, sort_array[i].rec_len);
-
- struct ext4_directory_entry_ll *tmp = ptr;
- if (i < (mid - 1))
- ext4_dir_entry_ll_set_entry_length(tmp,
- sort_array[i].rec_len);
- else
- ext4_dir_entry_ll_set_entry_length(tmp,
- block_size - offset);
-
- offset += sort_array[i].rec_len;
- }
-
- /* Second part - to the new block */
- offset = 0;
- for (i = mid; i < idx; ++i) {
- ptr = new_data_block_tmp.data + offset;
- memcpy(ptr, sort_array[i].dentry, sort_array[i].rec_len);
-
- struct ext4_directory_entry_ll *tmp = ptr;
- if (i < (idx - 1))
- ext4_dir_entry_ll_set_entry_length(tmp,
- sort_array[i].rec_len);
- else
- ext4_dir_entry_ll_set_entry_length(tmp,
- block_size - offset);
-
- offset += sort_array[i].rec_len;
- }
-
- /* Do some steps to finish operation */
- old_data_block->dirty = true;
- new_data_block_tmp.dirty = true;
-
- free(sort_array);
- free(entry_buffer);
-
- ext4_dir_dx_insert_entry(index_block, new_hash + continued,
- new_iblock);
-
- *new_data_block = new_data_block_tmp;
-
- return EOK;
+ int rc = EOK;
+
+ /* Allocate buffer for directory entries */
+ uint32_t block_size =
+ ext4_sb_get_block_size(&inode_ref->fs->sb);
+
+ void *entry_buffer = malloc(block_size);
+ if (entry_buffer == NULL)
+ return ENOMEM;
+
+ /* dot entry has the smallest size available */
+ uint32_t max_entry_count =
+ block_size / sizeof(struct ext4_directory_dx_dot_entry);
+
+ /* Allocate sort entry */
+ struct ext4_dx_sort_entry *sort_array =
+ malloc(max_entry_count * sizeof(struct ext4_dx_sort_entry));
+
+ if (sort_array == NULL) {
+ free(entry_buffer);
+ return ENOMEM;
+ }
+
+ uint32_t idx = 0;
+ uint32_t real_size = 0;
+
+ /* Initialize hinfo */
+ struct ext4_hash_info tmp_hinfo;
+ memcpy(&tmp_hinfo, hinfo, sizeof(struct ext4_hash_info));
+
+ /* Load all valid entries to the buffer */
+ struct ext4_directory_entry_ll *dentry = (void *)old_data_block->data;
+ void *entry_buffer_ptr = entry_buffer;
+ while ((void *)dentry < (void *)(old_data_block->data + block_size)) {
+ /* Read only valid entries */
+ if (ext4_dir_entry_ll_get_inode(dentry) != 0) {
+ uint8_t len = ext4_dir_entry_ll_get_name_length(
+ &inode_ref->fs->sb, dentry);
+
+ rc = ext4_dir_dx_hash_string(&tmp_hinfo, len, (char *) dentry->name);
+ if(rc != EOK) {
+ free(sort_array);
+ free(entry_buffer);
+ return rc;
+ }
+
+ uint32_t rec_len = 8 + len;
+
+ if ((rec_len % 4) != 0)
+ rec_len += 4 - (rec_len % 4);
+
+ memcpy(entry_buffer_ptr, dentry, rec_len);
+
+ sort_array[idx].dentry = entry_buffer_ptr;
+ sort_array[idx].rec_len = rec_len;
+ sort_array[idx].hash = tmp_hinfo.hash;
+
+ entry_buffer_ptr += rec_len;
+ real_size += rec_len;
+ idx++;
+ }
+
+ dentry = (void *) dentry +
+ ext4_dir_entry_ll_get_entry_length(dentry);
+ }
+
+ /* Sort all entries */
+ qsort(sort_array, idx, sizeof(struct ext4_dx_sort_entry),
+ ext4_dir_dx_entry_comparator);
+
+ /* Allocate new block for store the second part of entries */
+ uint32_t new_fblock;
+ uint32_t new_iblock;
+ rc = ext4_fs_append_inode_block(inode_ref, &new_fblock,
+ &new_iblock);
+ if (rc != EOK) {
+ free(sort_array);
+ free(entry_buffer);
+ return rc;
+ }
+
+ /* Load new block */
+ struct ext4_block new_data_block_tmp;
+ rc = ext4_block_get(inode_ref->fs->bdev, &new_data_block_tmp,
+ new_fblock);
+ if (rc != EOK) {
+ free(sort_array);
+ free(entry_buffer);
+ return rc;
+ }
+
+ /*
+ * Distribute entries to two blocks (by size)
+ * - compute the half
+ */
+ uint32_t new_hash = 0;
+ uint32_t current_size = 0;
+ uint32_t mid = 0;
+ uint32_t i;
+ for ( i = 0; i < idx; ++i) {
+ if ((current_size + sort_array[i].rec_len) > (real_size / 2)) {
+ new_hash = sort_array[i].hash;
+ mid = i;
+ break;
+ }
+
+ current_size += sort_array[i].rec_len;
+ }
+
+ /* Check hash collision */
+ uint32_t continued = 0;
+ if (new_hash == sort_array[mid-1].hash)
+ continued = 1;
+
+ uint32_t offset = 0;
+ void *ptr;
+
+ /* First part - to the old block */
+ for (i = 0; i < mid; ++i) {
+ ptr = old_data_block->data + offset;
+ memcpy(ptr, sort_array[i].dentry, sort_array[i].rec_len);
+
+ struct ext4_directory_entry_ll *tmp = ptr;
+ if (i < (mid - 1))
+ ext4_dir_entry_ll_set_entry_length(tmp,
+ sort_array[i].rec_len);
+ else
+ ext4_dir_entry_ll_set_entry_length(tmp,
+ block_size - offset);
+
+ offset += sort_array[i].rec_len;
+ }
+
+ /* Second part - to the new block */
+ offset = 0;
+ for (i = mid; i < idx; ++i) {
+ ptr = new_data_block_tmp.data + offset;
+ memcpy(ptr, sort_array[i].dentry, sort_array[i].rec_len);
+
+ struct ext4_directory_entry_ll *tmp = ptr;
+ if (i < (idx - 1))
+ ext4_dir_entry_ll_set_entry_length(tmp,
+ sort_array[i].rec_len);
+ else
+ ext4_dir_entry_ll_set_entry_length(tmp,
+ block_size - offset);
+
+ offset += sort_array[i].rec_len;
+ }
+
+ /* Do some steps to finish operation */
+ old_data_block->dirty = true;
+ new_data_block_tmp.dirty = true;
+
+ free(sort_array);
+ free(entry_buffer);
+
+ ext4_dir_dx_insert_entry(index_block, new_hash + continued,
+ new_iblock);
+
+ *new_data_block = new_data_block_tmp;
+
+ return EOK;
}
/** Split index node and maybe some parent nodes in the tree hierarchy.
@@ -741,267 +741,267 @@ static int ext4_dir_dx_split_data(struct ext4_inode_ref *inode_ref, *
*/
static int ext4_dir_dx_split_index(struct ext4_inode_ref *inode_ref,
- struct ext4_directory_dx_block *dx_blocks, struct ext4_directory_dx_block *dx_block)
+ struct ext4_directory_dx_block *dx_blocks, struct ext4_directory_dx_block *dx_block)
{
- struct ext4_directory_dx_entry *entries;
- if (dx_block == dx_blocks)
- entries =
- ((struct ext4_directory_dx_root *) dx_block->block.data)->entries;
- else
- entries =
- ((struct ext4_directory_dx_node *) dx_block->block.data)->entries;
-
- struct ext4_directory_dx_countlimit *countlimit =
- (struct ext4_directory_dx_countlimit *) entries;
-
- uint16_t leaf_limit =
- ext4_dir_dx_countlimit_get_limit(countlimit);
- uint16_t leaf_count =
- ext4_dir_dx_countlimit_get_count(countlimit);
-
- /* Check if is necessary to split index block */
- if (leaf_limit == leaf_count) {
- size_t levels = dx_block - dx_blocks;
-
- struct ext4_directory_dx_entry *root_entries =
- ((struct ext4_directory_dx_root *) dx_blocks[0].block.data)->entries;
-
- struct ext4_directory_dx_countlimit *root_countlimit =
- (struct ext4_directory_dx_countlimit *) root_entries;
- uint16_t root_limit =
- ext4_dir_dx_countlimit_get_limit(root_countlimit);
- uint16_t root_count =
- ext4_dir_dx_countlimit_get_count(root_countlimit);
-
- /* Linux limitation */
- if ((levels > 0) && (root_limit == root_count))
- return ENOSPC;
-
- /* Add new block to directory */
- uint32_t new_fblock;
- uint32_t new_iblock;
- int rc = ext4_fs_append_inode_block(inode_ref,
- &new_fblock, &new_iblock);
- if (rc != EOK)
- return rc;
-
- /* load new block */
- struct ext4_block new_block;
- rc = ext4_block_get(inode_ref->fs->bdev, &new_block,
- new_fblock);
- if (rc != EOK)
- return rc;
-
- struct ext4_directory_dx_node *new_node = (void *)new_block.data;
- struct ext4_directory_dx_entry *new_entries = new_node->entries;
-
- uint32_t block_size =
- ext4_sb_get_block_size(&inode_ref->fs->sb);
-
- /* Split leaf node */
- if (levels > 0) {
- uint32_t count_left = leaf_count / 2;
- uint32_t count_right = leaf_count - count_left;
- uint32_t hash_right =
- ext4_dir_dx_entry_get_hash(entries + count_left);
-
- /* Copy data to new node */
- memcpy((void *) new_entries, (void *) (entries + count_left),
- count_right * sizeof(struct ext4_directory_dx_entry));
-
- /* Initialize new node */
- struct ext4_directory_dx_countlimit *left_countlimit =
- (struct ext4_directory_dx_countlimit *) entries;
- struct ext4_directory_dx_countlimit *right_countlimit =
- (struct ext4_directory_dx_countlimit *) new_entries;
-
- ext4_dir_dx_countlimit_set_count(left_countlimit, count_left);
- ext4_dir_dx_countlimit_set_count(right_countlimit, count_right);
-
- uint32_t entry_space =
- block_size - sizeof(struct ext4_fake_directory_entry);
- uint32_t node_limit =
- entry_space / sizeof(struct ext4_directory_dx_entry);
- ext4_dir_dx_countlimit_set_limit(right_countlimit, node_limit);
-
- /* Which index block is target for new entry */
- uint32_t position_index = (dx_block->position - dx_block->entries);
- if (position_index >= count_left) {
- dx_block->block.dirty = true;
-
- struct ext4_block block_tmp = dx_block->block;
-
-
- dx_block->block = new_block;
-
- dx_block->position =
- new_entries + position_index - count_left;
- dx_block->entries = new_entries;
-
- new_block = block_tmp;
- }
-
- /* Finally insert new entry */
- ext4_dir_dx_insert_entry(dx_blocks, hash_right, new_iblock);
-
- return ext4_block_set(inode_ref->fs->bdev, &new_block);
- } else {
- /* Create second level index */
-
- /* Copy data from root to child block */
- memcpy((void *) new_entries, (void *) entries,
- leaf_count * sizeof(struct ext4_directory_dx_entry));
-
- struct ext4_directory_dx_countlimit *new_countlimit =
- (struct ext4_directory_dx_countlimit *) new_entries;
-
- uint32_t entry_space =
- block_size - sizeof(struct ext4_fake_directory_entry);
- uint32_t node_limit =
- entry_space / sizeof(struct ext4_directory_dx_entry);
- ext4_dir_dx_countlimit_set_limit(new_countlimit, node_limit);
-
- /* Set values in root node */
- struct ext4_directory_dx_countlimit *new_root_countlimit =
- (struct ext4_directory_dx_countlimit *) entries;
-
- ext4_dir_dx_countlimit_set_count(new_root_countlimit, 1);
- ext4_dir_dx_entry_set_block(entries, new_iblock);
-
- ((struct ext4_directory_dx_root *)
- dx_blocks[0].block.data)->info.indirect_levels = 1;
-
- /* Add new entry to the path */
- dx_block = dx_blocks + 1;
- dx_block->position = dx_block->position - entries + new_entries;
- dx_block->entries = new_entries;
- dx_block->block = new_block;
- }
- }
-
- return EOK;
+ struct ext4_directory_dx_entry *entries;
+ if (dx_block == dx_blocks)
+ entries =
+ ((struct ext4_directory_dx_root *) dx_block->block.data)->entries;
+ else
+ entries =
+ ((struct ext4_directory_dx_node *) dx_block->block.data)->entries;
+
+ struct ext4_directory_dx_countlimit *countlimit =
+ (struct ext4_directory_dx_countlimit *) entries;
+
+ uint16_t leaf_limit =
+ ext4_dir_dx_countlimit_get_limit(countlimit);
+ uint16_t leaf_count =
+ ext4_dir_dx_countlimit_get_count(countlimit);
+
+ /* Check if is necessary to split index block */
+ if (leaf_limit == leaf_count) {
+ size_t levels = dx_block - dx_blocks;
+
+ struct ext4_directory_dx_entry *root_entries =
+ ((struct ext4_directory_dx_root *) dx_blocks[0].block.data)->entries;
+
+ struct ext4_directory_dx_countlimit *root_countlimit =
+ (struct ext4_directory_dx_countlimit *) root_entries;
+ uint16_t root_limit =
+ ext4_dir_dx_countlimit_get_limit(root_countlimit);
+ uint16_t root_count =
+ ext4_dir_dx_countlimit_get_count(root_countlimit);
+
+ /* Linux limitation */
+ if ((levels > 0) && (root_limit == root_count))
+ return ENOSPC;
+
+ /* Add new block to directory */
+ uint32_t new_fblock;
+ uint32_t new_iblock;
+ int rc = ext4_fs_append_inode_block(inode_ref,
+ &new_fblock, &new_iblock);
+ if (rc != EOK)
+ return rc;
+
+ /* load new block */
+ struct ext4_block new_block;
+ rc = ext4_block_get(inode_ref->fs->bdev, &new_block,
+ new_fblock);
+ if (rc != EOK)
+ return rc;
+
+ struct ext4_directory_dx_node *new_node = (void *)new_block.data;
+ struct ext4_directory_dx_entry *new_entries = new_node->entries;
+
+ uint32_t block_size =
+ ext4_sb_get_block_size(&inode_ref->fs->sb);
+
+ /* Split leaf node */
+ if (levels > 0) {
+ uint32_t count_left = leaf_count / 2;
+ uint32_t count_right = leaf_count - count_left;
+ uint32_t hash_right =
+ ext4_dir_dx_entry_get_hash(entries + count_left);
+
+ /* Copy data to new node */
+ memcpy((void *) new_entries, (void *) (entries + count_left),
+ count_right * sizeof(struct ext4_directory_dx_entry));
+
+ /* Initialize new node */
+ struct ext4_directory_dx_countlimit *left_countlimit =
+ (struct ext4_directory_dx_countlimit *) entries;
+ struct ext4_directory_dx_countlimit *right_countlimit =
+ (struct ext4_directory_dx_countlimit *) new_entries;
+
+ ext4_dir_dx_countlimit_set_count(left_countlimit, count_left);
+ ext4_dir_dx_countlimit_set_count(right_countlimit, count_right);
+
+ uint32_t entry_space =
+ block_size - sizeof(struct ext4_fake_directory_entry);
+ uint32_t node_limit =
+ entry_space / sizeof(struct ext4_directory_dx_entry);
+ ext4_dir_dx_countlimit_set_limit(right_countlimit, node_limit);
+
+ /* Which index block is target for new entry */
+ uint32_t position_index = (dx_block->position - dx_block->entries);
+ if (position_index >= count_left) {
+ dx_block->block.dirty = true;
+
+ struct ext4_block block_tmp = dx_block->block;
+
+
+ dx_block->block = new_block;
+
+ dx_block->position =
+ new_entries + position_index - count_left;
+ dx_block->entries = new_entries;
+
+ new_block = block_tmp;
+ }
+
+ /* Finally insert new entry */
+ ext4_dir_dx_insert_entry(dx_blocks, hash_right, new_iblock);
+
+ return ext4_block_set(inode_ref->fs->bdev, &new_block);
+ } else {
+ /* Create second level index */
+
+ /* Copy data from root to child block */
+ memcpy((void *) new_entries, (void *) entries,
+ leaf_count * sizeof(struct ext4_directory_dx_entry));
+
+ struct ext4_directory_dx_countlimit *new_countlimit =
+ (struct ext4_directory_dx_countlimit *) new_entries;
+
+ uint32_t entry_space =
+ block_size - sizeof(struct ext4_fake_directory_entry);
+ uint32_t node_limit =
+ entry_space / sizeof(struct ext4_directory_dx_entry);
+ ext4_dir_dx_countlimit_set_limit(new_countlimit, node_limit);
+
+ /* Set values in root node */
+ struct ext4_directory_dx_countlimit *new_root_countlimit =
+ (struct ext4_directory_dx_countlimit *) entries;
+
+ ext4_dir_dx_countlimit_set_count(new_root_countlimit, 1);
+ ext4_dir_dx_entry_set_block(entries, new_iblock);
+
+ ((struct ext4_directory_dx_root *)
+ dx_blocks[0].block.data)->info.indirect_levels = 1;
+
+ /* Add new entry to the path */
+ dx_block = dx_blocks + 1;
+ dx_block->position = dx_block->position - entries + new_entries;
+ dx_block->entries = new_entries;
+ dx_block->block = new_block;
+ }
+ }
+
+ return EOK;
}
int ext4_dir_dx_add_entry(struct ext4_inode_ref *parent,
- struct ext4_inode_ref *child, const char *name)
+ struct ext4_inode_ref *child, const char *name)
{
- int rc2 = EOK;
-
- /* Get direct block 0 (index root) */
- uint32_t root_block_addr;
- int rc = ext4_fs_get_inode_data_block_index(parent, 0,
- &root_block_addr);
- if (rc != EOK)
- return rc;
-
- struct ext4_fs *fs = parent->fs;
- struct ext4_block root_block;
-
- rc = ext4_block_get(fs->bdev, &root_block, root_block_addr);
- if (rc != EOK)
- return rc;
-
- /* Initialize hinfo structure (mainly compute hash) */
- uint32_t name_len = strlen(name);
- struct ext4_hash_info hinfo;
- rc = ext4_dir_hinfo_init(&hinfo, &root_block, &fs->sb,
- name_len, name);
- if (rc != EOK) {
- ext4_block_set(fs->bdev, &root_block);
- return EXT4_ERR_BAD_DX_DIR;
- }
-
- /*
- * Hardcoded number 2 means maximum height of index
- * tree defined in Linux.
- */
- struct ext4_directory_dx_block dx_blocks[2];
- struct ext4_directory_dx_block *dx_block;
- struct ext4_directory_dx_block *dx_it;
-
- rc = ext4_dir_dx_get_leaf(&hinfo, parent, &root_block,
- &dx_block, dx_blocks);
- if (rc != EOK) {
- rc = EXT4_ERR_BAD_DX_DIR;
- goto release_index;
- }
-
- /* Try to insert to existing data block */
- uint32_t leaf_block_idx =
- ext4_dir_dx_entry_get_block(dx_block->position);
- uint32_t leaf_block_addr;
- rc = ext4_fs_get_inode_data_block_index(parent, leaf_block_idx,
- &leaf_block_addr);
- if (rc != EOK)
- goto release_index;
-
- struct ext4_block target_block;
- rc = ext4_block_get(fs->bdev, &target_block, leaf_block_addr);
- if (rc != EOK)
- goto release_index;
-
- /* Check if insert operation passed */
- rc = ext4_dir_try_insert_entry(&fs->sb, &target_block, child,
- name, name_len);
- if (rc == EOK)
- goto release_target_index;
-
- /*
- * Check if there is needed to split index node
- * (and recursively also parent nodes)
- */
- rc = ext4_dir_dx_split_index(parent, dx_blocks, dx_block);
- if (rc != EOK)
- goto release_target_index;
-
- /* Split entries to two blocks (includes sorting by hash value) */
- struct ext4_block new_block;
- rc = ext4_dir_dx_split_data(parent, &hinfo, &target_block,
- dx_block, &new_block);
- if (rc != EOK) {
- rc2 = rc;
- goto release_target_index;
- }
-
- /* Where to save new entry */
- uint32_t new_block_hash =
- ext4_dir_dx_entry_get_hash(dx_block->position + 1);
- if (hinfo.hash >= new_block_hash)
- rc = ext4_dir_try_insert_entry(&fs->sb, &new_block,
- child, name, name_len);
- else
- rc = ext4_dir_try_insert_entry(&fs->sb, &target_block,
- child, name, name_len);
-
- /* Cleanup */
- rc = ext4_block_set(fs->bdev, &new_block);
- if (rc != EOK)
- return rc;
-
- /* Cleanup operations */
-
-release_target_index:
- rc2 = rc;
-
- rc = ext4_block_set(fs->bdev, &target_block);
- if (rc != EOK)
- return rc;
-
-release_index:
- if (rc != EOK)
- rc2 = rc;
-
- dx_it = dx_blocks;
-
- while (dx_it <= dx_block) {
- rc = ext4_block_set(fs->bdev, &dx_it->block);
- if (rc != EOK)
- return rc;
-
- dx_it++;
- }
-
- return rc2;
+ int rc2 = EOK;
+
+ /* Get direct block 0 (index root) */
+ uint32_t root_block_addr;
+ int rc = ext4_fs_get_inode_data_block_index(parent, 0,
+ &root_block_addr);
+ if (rc != EOK)
+ return rc;
+
+ struct ext4_fs *fs = parent->fs;
+ struct ext4_block root_block;
+
+ rc = ext4_block_get(fs->bdev, &root_block, root_block_addr);
+ if (rc != EOK)
+ return rc;
+
+ /* Initialize hinfo structure (mainly compute hash) */
+ uint32_t name_len = strlen(name);
+ struct ext4_hash_info hinfo;
+ rc = ext4_dir_hinfo_init(&hinfo, &root_block, &fs->sb,
+ name_len, name);
+ if (rc != EOK) {
+ ext4_block_set(fs->bdev, &root_block);
+ return EXT4_ERR_BAD_DX_DIR;
+ }
+
+ /*
+ * Hardcoded number 2 means maximum height of index
+ * tree defined in Linux.
+ */
+ struct ext4_directory_dx_block dx_blocks[2];
+ struct ext4_directory_dx_block *dx_block;
+ struct ext4_directory_dx_block *dx_it;
+
+ rc = ext4_dir_dx_get_leaf(&hinfo, parent, &root_block,
+ &dx_block, dx_blocks);
+ if (rc != EOK) {
+ rc = EXT4_ERR_BAD_DX_DIR;
+ goto release_index;
+ }
+
+ /* Try to insert to existing data block */
+ uint32_t leaf_block_idx =
+ ext4_dir_dx_entry_get_block(dx_block->position);
+ uint32_t leaf_block_addr;
+ rc = ext4_fs_get_inode_data_block_index(parent, leaf_block_idx,
+ &leaf_block_addr);
+ if (rc != EOK)
+ goto release_index;
+
+ struct ext4_block target_block;
+ rc = ext4_block_get(fs->bdev, &target_block, leaf_block_addr);
+ if (rc != EOK)
+ goto release_index;
+
+ /* Check if insert operation passed */
+ rc = ext4_dir_try_insert_entry(&fs->sb, &target_block, child,
+ name, name_len);
+ if (rc == EOK)
+ goto release_target_index;
+
+ /*
+ * Check if there is needed to split index node
+ * (and recursively also parent nodes)
+ */
+ rc = ext4_dir_dx_split_index(parent, dx_blocks, dx_block);
+ if (rc != EOK)
+ goto release_target_index;
+
+ /* Split entries to two blocks (includes sorting by hash value) */
+ struct ext4_block new_block;
+ rc = ext4_dir_dx_split_data(parent, &hinfo, &target_block,
+ dx_block, &new_block);
+ if (rc != EOK) {
+ rc2 = rc;
+ goto release_target_index;
+ }
+
+ /* Where to save new entry */
+ uint32_t new_block_hash =
+ ext4_dir_dx_entry_get_hash(dx_block->position + 1);
+ if (hinfo.hash >= new_block_hash)
+ rc = ext4_dir_try_insert_entry(&fs->sb, &new_block,
+ child, name, name_len);
+ else
+ rc = ext4_dir_try_insert_entry(&fs->sb, &target_block,
+ child, name, name_len);
+
+ /* Cleanup */
+ rc = ext4_block_set(fs->bdev, &new_block);
+ if (rc != EOK)
+ return rc;
+
+ /* Cleanup operations */
+
+ release_target_index:
+ rc2 = rc;
+
+ rc = ext4_block_set(fs->bdev, &target_block);
+ if (rc != EOK)
+ return rc;
+
+ release_index:
+ if (rc != EOK)
+ rc2 = rc;
+
+ dx_it = dx_blocks;
+
+ while (dx_it <= dx_block) {
+ rc = ext4_block_set(fs->bdev, &dx_it->block);
+ if (rc != EOK)
+ return rc;
+
+ dx_it++;
+ }
+
+ return rc2;
}
/**
diff --git a/src/lwext4/ext4_dir_idx.h b/src/lwext4/ext4_dir_idx.h index 65b5b18..9682d37 100644 --- a/src/lwext4/ext4_dir_idx.h +++ b/src/lwext4/ext4_dir_idx.h @@ -49,51 +49,51 @@ #include <stdbool.h>
uint8_t ext4_dir_dx_root_info_get_hash_version(
- struct ext4_directory_dx_root_info *root_info);
+ struct ext4_directory_dx_root_info *root_info);
void ext4_dir_dx_root_info_set_hash_version(
- struct ext4_directory_dx_root_info *root_info, uint8_t v);
+ struct ext4_directory_dx_root_info *root_info, uint8_t v);
uint8_t ext4_dir_dx_root_info_get_info_length(
- struct ext4_directory_dx_root_info *root_info);
+ struct ext4_directory_dx_root_info *root_info);
void ext4_dir_dx_root_info_set_info_length(
- struct ext4_directory_dx_root_info *root_info, uint8_t len);
+ struct ext4_directory_dx_root_info *root_info, uint8_t len);
uint8_t ext4_dir_dx_root_info_get_indirect_levels(
- struct ext4_directory_dx_root_info *root_info);
+ struct ext4_directory_dx_root_info *root_info);
void ext4_dir_dx_root_info_set_indirect_levels(
- struct ext4_directory_dx_root_info *root_info, uint8_t lvl);
+ struct ext4_directory_dx_root_info *root_info, uint8_t lvl);
uint16_t ext4_dir_dx_countlimit_get_limit(
- struct ext4_directory_dx_countlimit *climit);
+ struct ext4_directory_dx_countlimit *climit);
void ext4_dir_dx_countlimit_set_limit(
- struct ext4_directory_dx_countlimit *climit, uint16_t limit);
+ struct ext4_directory_dx_countlimit *climit, uint16_t limit);
uint16_t ext4_dir_dx_countlimit_get_count(
- struct ext4_directory_dx_countlimit *climit);
+ struct ext4_directory_dx_countlimit *climit);
void ext4_dir_dx_countlimit_set_count(
- struct ext4_directory_dx_countlimit *climit, uint16_t count);
+ struct ext4_directory_dx_countlimit *climit, uint16_t count);
uint32_t ext4_dir_dx_entry_get_hash(
- struct ext4_directory_dx_entry *entry);
+ struct ext4_directory_dx_entry *entry);
void ext4_dir_dx_entry_set_hash(
- struct ext4_directory_dx_entry *entry, uint32_t hash);
+ struct ext4_directory_dx_entry *entry, uint32_t hash);
uint32_t ext4_dir_dx_entry_get_block(
- struct ext4_directory_dx_entry *entry);
+ struct ext4_directory_dx_entry *entry);
void ext4_dir_dx_entry_set_block(
- struct ext4_directory_dx_entry *entry, uint32_t block);
+ struct ext4_directory_dx_entry *entry, uint32_t block);
int ext4_dir_dx_init(struct ext4_inode_ref *dir);
int ext4_dir_dx_find_entry(struct ext4_directory_search_result * result,
- struct ext4_inode_ref *inode_ref, size_t name_len, const char *name);
+ struct ext4_inode_ref *inode_ref, size_t name_len, const char *name);
int ext4_dir_dx_add_entry(struct ext4_inode_ref *parent,
- struct ext4_inode_ref *child, const char *name);
+ struct ext4_inode_ref *child, const char *name);
#endif /* EXT4_DIR_IDX_H_ */
diff --git a/src/lwext4/ext4_fs.c b/src/lwext4/ext4_fs.c index 1d2bdd4..7f8063c 100644 --- a/src/lwext4/ext4_fs.c +++ b/src/lwext4/ext4_fs.c @@ -54,118 +54,118 @@ int ext4_fs_init(struct ext4_fs *fs, struct ext4_blockdev *bdev) { - int r, i; - uint16_t tmp; - uint32_t bsize; - bool read_only = false; + int r, i; + uint16_t tmp; + uint32_t bsize; + bool read_only = false; - ext4_assert(fs && bdev); + ext4_assert(fs && bdev); - fs->bdev = bdev; + fs->bdev = bdev; - r = ext4_sb_read(fs->bdev, &fs->sb); - if(r != EOK) - return r; + r = ext4_sb_read(fs->bdev, &fs->sb); + if(r != EOK) + return r; - if(!ext4_sb_check(&fs->sb)) - return ENOTSUP; + if(!ext4_sb_check(&fs->sb)) + return ENOTSUP; - bsize = ext4_sb_get_block_size(&fs->sb); - if (bsize > EXT4_MAX_BLOCK_SIZE) - return ENXIO; + bsize = ext4_sb_get_block_size(&fs->sb); + if (bsize > EXT4_MAX_BLOCK_SIZE) + return ENXIO; - r = ext4_fs_check_features(fs, &read_only); - if(r != EOK) - return r; + r = ext4_fs_check_features(fs, &read_only); + if(r != EOK) + return r; - if(read_only) - return ENOTSUP; + if(read_only) + return ENOTSUP; - /* Compute limits for indirect block levels */ - uint32_t blocks_id = bsize / sizeof(uint32_t); + /* Compute limits for indirect block levels */ + uint32_t blocks_id = bsize / sizeof(uint32_t); - fs->inode_block_limits[0] = EXT4_INODE_DIRECT_BLOCK_COUNT; - fs->inode_blocks_per_level[0] = 1; + fs->inode_block_limits[0] = EXT4_INODE_DIRECT_BLOCK_COUNT; + fs->inode_blocks_per_level[0] = 1; - for (i = 1; i < 4; i++) { - fs->inode_blocks_per_level[i] = fs->inode_blocks_per_level[i - 1] * - blocks_id; - fs->inode_block_limits[i] = fs->inode_block_limits[i - 1] + - fs->inode_blocks_per_level[i]; - } + for (i = 1; i < 4; i++) { + fs->inode_blocks_per_level[i] = fs->inode_blocks_per_level[i - 1] * + blocks_id; + fs->inode_block_limits[i] = fs->inode_block_limits[i - 1] + + fs->inode_blocks_per_level[i]; + } - /*Validate FS*/ - tmp = ext4_get16(&fs->sb, state); - if (tmp & EXT4_SUPERBLOCK_STATE_ERROR_FS) { - ext4_dprintf(EXT4_DEBUG_FS, "Filesystem was not cleanly unmounted before \n"); - } + /*Validate FS*/ + tmp = ext4_get16(&fs->sb, state); + if (tmp & EXT4_SUPERBLOCK_STATE_ERROR_FS) { + ext4_dprintf(EXT4_DEBUG_FS, "Filesystem was not cleanly unmounted before \n"); + } - /* Mark system as mounted */ - ext4_set16(&fs->sb, state, EXT4_SUPERBLOCK_STATE_ERROR_FS); - r = ext4_sb_write(fs->bdev, &fs->sb); - if (r != EOK) - return r; + /* Mark system as mounted */ + ext4_set16(&fs->sb, state, EXT4_SUPERBLOCK_STATE_ERROR_FS); + r = ext4_sb_write(fs->bdev, &fs->sb); + if (r != EOK) + return r; - /*Update mount count*/ - ext4_set16(&fs->sb, mount_count, ext4_get16(&fs->sb, mount_count) + 1); + /*Update mount count*/ + ext4_set16(&fs->sb, mount_count, ext4_get16(&fs->sb, mount_count) + 1); - return r; + return r; } int ext4_fs_fini(struct ext4_fs *fs) { - ext4_assert(fs); + ext4_assert(fs); - /*Set superblock state*/ - ext4_set16(&fs->sb, state, EXT4_SUPERBLOCK_STATE_VALID_FS); + /*Set superblock state*/ + ext4_set16(&fs->sb, state, EXT4_SUPERBLOCK_STATE_VALID_FS); - return ext4_sb_write(fs->bdev, &fs->sb); + return ext4_sb_write(fs->bdev, &fs->sb); } int ext4_fs_check_features(struct ext4_fs *fs, bool *read_only) { - ext4_assert(fs && read_only); + ext4_assert(fs && read_only); - if(ext4_get32(&fs->sb, rev_level) == 0){ - *read_only = false; - return EOK; - } + if(ext4_get32(&fs->sb, rev_level) == 0){ + *read_only = false; + return EOK; + } - /*Check features_incompatible*/ - if ((ext4_get32(&fs->sb, features_incompatible) & (~EXT4_FEATURE_INCOMPAT_SUPP)) ) - return ENOTSUP; + /*Check features_incompatible*/ + if ((ext4_get32(&fs->sb, features_incompatible) & (~EXT4_FEATURE_INCOMPAT_SUPP)) ) + return ENOTSUP; - /*Check features_read_only*/ - if ((ext4_get32(&fs->sb, features_read_only) & (~EXT4_FEATURE_RO_COMPAT_SUPP))){ - *read_only = true; - return EOK; - } + /*Check features_read_only*/ + if ((ext4_get32(&fs->sb, features_read_only) & (~EXT4_FEATURE_RO_COMPAT_SUPP))){ + *read_only = true; + return EOK; + } - *read_only = false; + *read_only = false; - return EOK; + return EOK; } uint32_t ext4_fs_baddr2_index_in_group(struct ext4_sblock *s, uint32_t baddr) { - ext4_assert(baddr); - if(ext4_get32(s, first_data_block)) - baddr--; + ext4_assert(baddr); + if(ext4_get32(s, first_data_block)) + baddr--; - return baddr % ext4_get32(s, blocks_per_group); + return baddr % ext4_get32(s, blocks_per_group); } uint32_t ext4_fs_index_in_group2_baddr(struct ext4_sblock *s, uint32_t index, uint32_t bgid) { - if(ext4_get32(s, first_data_block)) - index++; + if(ext4_get32(s, first_data_block)) + index++; - return ext4_get32(s, blocks_per_group) * bgid + index; + return ext4_get32(s, blocks_per_group) * bgid + index; } @@ -173,1002 +173,1002 @@ uint32_t ext4_fs_index_in_group2_baddr(struct ext4_sblock *s, uint32_t index, ui static int ext4_fs_init_block_bitmap(struct ext4_block_group_ref *bg_ref) { - uint32_t i; - uint32_t bitmap_block_addr = ext4_bg_get_block_bitmap( - bg_ref->block_group, &bg_ref->fs->sb); + uint32_t i; + uint32_t bitmap_block_addr = ext4_bg_get_block_bitmap( + bg_ref->block_group, &bg_ref->fs->sb); - struct ext4_block block_bitmap; - int rc = ext4_block_get(bg_ref->fs->bdev, &block_bitmap, bitmap_block_addr); - if (rc != EOK) - return rc; + struct ext4_block block_bitmap; + int rc = ext4_block_get(bg_ref->fs->bdev, &block_bitmap, bitmap_block_addr); + if (rc != EOK) + return rc; - memset(block_bitmap.data, 0, ext4_sb_get_block_size(&bg_ref->fs->sb)); + memset(block_bitmap.data, 0, ext4_sb_get_block_size(&bg_ref->fs->sb)); - /* Determine first block and first data block in group */ - uint32_t first_idx = 0; + /* Determine first block and first data block in group */ + uint32_t first_idx = 0; - uint32_t first_data = ext4_balloc_get_first_data_block_in_group( - &bg_ref->fs->sb, bg_ref); - uint32_t first_data_idx = ext4_fs_baddr2_index_in_group( - &bg_ref->fs->sb, first_data); + uint32_t first_data = ext4_balloc_get_first_data_block_in_group( + &bg_ref->fs->sb, bg_ref); + uint32_t first_data_idx = ext4_fs_baddr2_index_in_group( + &bg_ref->fs->sb, first_data); - /* Set bits from to first block to first data block - 1 to one (allocated) */ - /*TODO: Optimize it*/ - for (i = first_idx; i < first_data_idx; ++i) - ext4_bmap_bit_set(block_bitmap.data, i); + /* Set bits from to first block to first data block - 1 to one (allocated) */ + /*TODO: Optimize it*/ + for (i = first_idx; i < first_data_idx; ++i) + ext4_bmap_bit_set(block_bitmap.data, i); - block_bitmap.dirty = true; + block_bitmap.dirty = true; - /* Save bitmap */ - return ext4_block_set(bg_ref->fs->bdev, &block_bitmap); + /* Save bitmap */ + return ext4_block_set(bg_ref->fs->bdev, &block_bitmap); } static int ext4_fs_init_inode_bitmap(struct ext4_block_group_ref *bg_ref) { - /* Load bitmap */ - uint32_t bitmap_block_addr = ext4_bg_get_inode_bitmap( - bg_ref->block_group, &bg_ref->fs->sb); + /* Load bitmap */ + uint32_t bitmap_block_addr = ext4_bg_get_inode_bitmap( + bg_ref->block_group, &bg_ref->fs->sb); - struct ext4_block block_bitmap; - int rc = ext4_block_get(bg_ref->fs->bdev, &block_bitmap, bitmap_block_addr); - if (rc != EOK) - return rc; + struct ext4_block block_bitmap; + int rc = ext4_block_get(bg_ref->fs->bdev, &block_bitmap, bitmap_block_addr); + if (rc != EOK) + return rc; - /* Initialize all bitmap bits to zero */ - uint32_t block_size = ext4_sb_get_block_size(&bg_ref->fs->sb); - uint32_t inodes_per_group = ext4_get32(&bg_ref->fs->sb, inodes_per_group); + /* Initialize all bitmap bits to zero */ + uint32_t block_size = ext4_sb_get_block_size(&bg_ref->fs->sb); + uint32_t inodes_per_group = ext4_get32(&bg_ref->fs->sb, inodes_per_group); - memset(block_bitmap.data, 0, (inodes_per_group + 7) / 8); + memset(block_bitmap.data, 0, (inodes_per_group + 7) / 8); - uint32_t start_bit = inodes_per_group; - uint32_t end_bit = block_size * 8; + uint32_t start_bit = inodes_per_group; + uint32_t end_bit = block_size * 8; - uint32_t i; - for (i = start_bit; i < ((start_bit + 7) & ~7UL); i++) - ext4_bmap_bit_set(block_bitmap.data, i); + uint32_t i; + for (i = start_bit; i < ((start_bit + 7) & ~7UL); i++) + ext4_bmap_bit_set(block_bitmap.data, i); - if (i < end_bit) - memset(block_bitmap.data + (i >> 3), 0xff, (end_bit - i) >> 3); + if (i < end_bit) + memset(block_bitmap.data + (i >> 3), 0xff, (end_bit - i) >> 3); - block_bitmap.dirty = true; + block_bitmap.dirty = true; - /* Save bitmap */ - return ext4_block_set(bg_ref->fs->bdev, &block_bitmap); + /* Save bitmap */ + return ext4_block_set(bg_ref->fs->bdev, &block_bitmap); } static int ext4_fs_init_inode_table(struct ext4_block_group_ref *bg_ref) { - struct ext4_sblock *sb = &bg_ref->fs->sb; + struct ext4_sblock *sb = &bg_ref->fs->sb; - uint32_t inode_size = ext4_get32(sb, inode_size); - uint32_t block_size = ext4_sb_get_block_size(sb); - uint32_t inodes_per_block = block_size / inode_size; - uint32_t inodes_in_group = ext4_inodes_in_group_cnt(sb, bg_ref->index); - uint32_t table_blocks = inodes_in_group / inodes_per_block; - uint32_t fblock; + uint32_t inode_size = ext4_get32(sb, inode_size); + uint32_t block_size = ext4_sb_get_block_size(sb); + uint32_t inodes_per_block = block_size / inode_size; + uint32_t inodes_in_group = ext4_inodes_in_group_cnt(sb, bg_ref->index); + uint32_t table_blocks = inodes_in_group / inodes_per_block; + uint32_t fblock; - if (inodes_in_group % inodes_per_block) - table_blocks++; + if (inodes_in_group % inodes_per_block) + table_blocks++; - /* Compute initialization bounds */ - uint32_t first_block = ext4_bg_get_inode_table_first_block( - bg_ref->block_group, sb); + /* Compute initialization bounds */ + uint32_t first_block = ext4_bg_get_inode_table_first_block( + bg_ref->block_group, sb); - uint32_t last_block = first_block + table_blocks - 1; + uint32_t last_block = first_block + table_blocks - 1; - /* Initialization of all itable blocks */ - for (fblock = first_block; fblock <= last_block; ++fblock) { + /* Initialization of all itable blocks */ + for (fblock = first_block; fblock <= last_block; ++fblock) { - struct ext4_block block; - int rc = ext4_block_get(bg_ref->fs->bdev, &block, fblock); - if (rc != EOK) - return rc; + struct ext4_block block; + int rc = ext4_block_get(bg_ref->fs->bdev, &block, fblock); + if (rc != EOK) + return rc; - memset(block.data, 0, block_size); - block.dirty = true; + memset(block.data, 0, block_size); + block.dirty = true; - ext4_block_set(bg_ref->fs->bdev, &block); - if (rc != EOK) - return rc; - } + ext4_block_set(bg_ref->fs->bdev, &block); + if (rc != EOK) + return rc; + } - return EOK; + return EOK; } int ext4_fs_get_block_group_ref(struct ext4_fs *fs, uint32_t bgid, struct ext4_block_group_ref *ref) { - /* Compute number of descriptors, that fits in one data block */ - uint32_t dsc_per_block = ext4_sb_get_block_size(&fs->sb) / - ext4_sb_get_desc_size(&fs->sb); - - /* Block group descriptor table starts at the next block after superblock */ - uint64_t block_id = ext4_get32(&fs->sb, first_data_block) + 1; - - /* Find the block containing the descriptor we are looking for */ - block_id += bgid / dsc_per_block; - uint32_t offset = (bgid % dsc_per_block) * - ext4_sb_get_desc_size(&fs->sb); - - - int rc = ext4_block_get(fs->bdev, &ref->block, block_id); - if (rc != EOK) - return rc; - - ref->block_group = (void *)(ref->block.data + offset); - ref->fs = fs; - ref->index = bgid; - ref->dirty = false; - - if (ext4_bg_has_flag(ref->block_group, - EXT4_BLOCK_GROUP_BLOCK_UNINIT)) { - rc = ext4_fs_init_block_bitmap(ref); - if (rc != EOK) { - ext4_block_set(fs->bdev, &ref->block); - return rc; - } - ext4_bg_clear_flag(ref->block_group, - EXT4_BLOCK_GROUP_BLOCK_UNINIT); - - ref->dirty = true; - } - - if (ext4_bg_has_flag(ref->block_group, - EXT4_BLOCK_GROUP_INODE_UNINIT)) { - rc = ext4_fs_init_inode_bitmap(ref); - if (rc != EOK) { - ext4_block_set(ref->fs->bdev, &ref->block); - return rc; - } - - ext4_bg_clear_flag(ref->block_group, - EXT4_BLOCK_GROUP_INODE_UNINIT); - - if (!ext4_bg_has_flag(ref->block_group, - EXT4_BLOCK_GROUP_ITABLE_ZEROED)) { - rc = ext4_fs_init_inode_table(ref); - if (rc != EOK){ - ext4_block_set(fs->bdev, &ref->block); - return rc; - } - - ext4_bg_set_flag(ref->block_group, - EXT4_BLOCK_GROUP_ITABLE_ZEROED); - } - - ref->dirty = true; - } - - return EOK; + /* Compute number of descriptors, that fits in one data block */ + uint32_t dsc_per_block = ext4_sb_get_block_size(&fs->sb) / + ext4_sb_get_desc_size(&fs->sb); + + /* Block group descriptor table starts at the next block after superblock */ + uint64_t block_id = ext4_get32(&fs->sb, first_data_block) + 1; + + /* Find the block containing the descriptor we are looking for */ + block_id += bgid / dsc_per_block; + uint32_t offset = (bgid % dsc_per_block) * + ext4_sb_get_desc_size(&fs->sb); + + + int rc = ext4_block_get(fs->bdev, &ref->block, block_id); + if (rc != EOK) + return rc; + + ref->block_group = (void *)(ref->block.data + offset); + ref->fs = fs; + ref->index = bgid; + ref->dirty = false; + + if (ext4_bg_has_flag(ref->block_group, + EXT4_BLOCK_GROUP_BLOCK_UNINIT)) { + rc = ext4_fs_init_block_bitmap(ref); + if (rc != EOK) { + ext4_block_set(fs->bdev, &ref->block); + return rc; + } + ext4_bg_clear_flag(ref->block_group, + EXT4_BLOCK_GROUP_BLOCK_UNINIT); + + ref->dirty = true; + } + + if (ext4_bg_has_flag(ref->block_group, + EXT4_BLOCK_GROUP_INODE_UNINIT)) { + rc = ext4_fs_init_inode_bitmap(ref); + if (rc != EOK) { + ext4_block_set(ref->fs->bdev, &ref->block); + return rc; + } + + ext4_bg_clear_flag(ref->block_group, + EXT4_BLOCK_GROUP_INODE_UNINIT); + + if (!ext4_bg_has_flag(ref->block_group, + EXT4_BLOCK_GROUP_ITABLE_ZEROED)) { + rc = ext4_fs_init_inode_table(ref); + if (rc != EOK){ + ext4_block_set(fs->bdev, &ref->block); + return rc; + } + + ext4_bg_set_flag(ref->block_group, + EXT4_BLOCK_GROUP_ITABLE_ZEROED); + } + + ref->dirty = true; + } + + return EOK; } static uint16_t ext4_fs_bg_checksum(struct ext4_sblock *sb, uint32_t bgid, struct ext4_bgroup *bg) { - /* If checksum not supported, 0 will be returned */ - uint16_t crc = 0; + /* If checksum not supported, 0 will be returned */ + uint16_t crc = 0; - /* Compute the checksum only if the filesystem supports it */ - if (ext4_sb_check_read_only(sb, - EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) { - void *base = bg; - void *checksum = &bg->checksum; + /* Compute the checksum only if the filesystem supports it */ + if (ext4_sb_check_read_only(sb, + EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) { + void *base = bg; + void *checksum = &bg->checksum; - uint32_t offset = (uint32_t) (checksum - base); + uint32_t offset = (uint32_t) (checksum - base); - /* Convert block group index to little endian */ - uint32_t le_group = to_le32(bgid); + /* Convert block group index to little endian */ + uint32_t le_group = to_le32(bgid); - /* Initialization */ - crc = ext4_bg_crc16(~0, sb->uuid, sizeof(sb->uuid)); + /* Initialization */ + crc = ext4_bg_crc16(~0, sb->uuid, sizeof(sb->uuid)); - /* Include index of block group */ - crc = ext4_bg_crc16(crc, (uint8_t *) &le_group, sizeof(le_group)); + /* Include index of block group */ + crc = ext4_bg_crc16(crc, (uint8_t *) &le_group, sizeof(le_group)); - /* Compute crc from the first part (stop before checksum field) */ - crc = ext4_bg_crc16(crc, (uint8_t *) bg, offset); + /* Compute crc from the first part (stop before checksum field) */ + crc = ext4_bg_crc16(crc, (uint8_t *) bg, offset); - /* Skip checksum */ - offset += sizeof(bg->checksum); + /* Skip checksum */ + offset += sizeof(bg->checksum); - /* Checksum of the rest of block group descriptor */ - if ((ext4_sb_check_feature_incompatible(sb, - EXT4_FEATURE_INCOMPAT_64BIT)) && - (offset < ext4_sb_get_desc_size(sb))) + /* Checksum of the rest of block group descriptor */ + if ((ext4_sb_check_feature_incompatible(sb, + EXT4_FEATURE_INCOMPAT_64BIT)) && + (offset < ext4_sb_get_desc_size(sb))) - crc = ext4_bg_crc16(crc, ((uint8_t *) bg) + offset, - ext4_sb_get_desc_size(sb) - offset); - } - return crc; + crc = ext4_bg_crc16(crc, ((uint8_t *) bg) + offset, + ext4_sb_get_desc_size(sb) - offset); + } + return crc; } int ext4_fs_put_block_group_ref(struct ext4_block_group_ref *ref) { - /* Check if reference modified */ - if (ref->dirty) { - /* Compute new checksum of block group */ - uint16_t checksum = - ext4_fs_bg_checksum(&ref->fs->sb, ref->index, - ref->block_group); + /* Check if reference modified */ + if (ref->dirty) { + /* Compute new checksum of block group */ + uint16_t checksum = + ext4_fs_bg_checksum(&ref->fs->sb, ref->index, + ref->block_group); - ref->block_group->checksum = to_le16(checksum); + ref->block_group->checksum = to_le16(checksum); - /* Mark block dirty for writing changes to physical device */ - ref->block.dirty = true; - } + /* Mark block dirty for writing changes to physical device */ + ref->block.dirty = true; + } - /* Put back block, that contains block group descriptor */ - return ext4_block_set(ref->fs->bdev, &ref->block); + /* Put back block, that contains block group descriptor */ + return ext4_block_set(ref->fs->bdev, &ref->block); } int ext4_fs_get_inode_ref(struct ext4_fs *fs, uint32_t index, struct ext4_inode_ref *ref) { - /* Compute number of i-nodes, that fits in one data block */ - uint32_t inodes_per_group = ext4_get32(&fs->sb, inodes_per_group); - - /* - * Inode numbers are 1-based, but it is simpler to work with 0-based - * when computing indices - */ - index -= 1; - uint32_t block_group = index / inodes_per_group; - uint32_t offset_in_group = index % inodes_per_group; - - /* Load block group, where i-node is located */ - struct ext4_block_group_ref bg_ref; - - int rc = ext4_fs_get_block_group_ref(fs, block_group, &bg_ref); - if (rc != EOK) { - return rc; - } - - /* Load block address, where i-node table is located */ - uint32_t inode_table_start = - ext4_bg_get_inode_table_first_block(bg_ref.block_group, - &fs->sb); - - /* Put back block group reference (not needed more) */ - rc = ext4_fs_put_block_group_ref(&bg_ref); - if (rc != EOK) { - return rc; - } - - /* Compute position of i-node in the block group */ - uint16_t inode_size = ext4_get16(&fs->sb, inode_size); - uint32_t block_size = ext4_sb_get_block_size(&fs->sb); - uint32_t byte_offset_in_group = offset_in_group * inode_size; - - /* Compute block address */ - uint64_t block_id = inode_table_start + (byte_offset_in_group / block_size); - - - rc = ext4_block_get(fs->bdev, &ref->block, block_id); - if (rc != EOK) { - return rc; - } - - /* Compute position of i-node in the data block */ - uint32_t offset_in_block = byte_offset_in_group % block_size; - ref->inode = (struct ext4_inode *)(ref->block.data + offset_in_block); - - /* We need to store the original value of index in the reference */ - ref->index = index + 1; - ref->fs = fs; - ref->dirty = false; - - return EOK; + /* Compute number of i-nodes, that fits in one data block */ + uint32_t inodes_per_group = ext4_get32(&fs->sb, inodes_per_group); + + /* + * Inode numbers are 1-based, but it is simpler to work with 0-based + * when computing indices + */ + index -= 1; + uint32_t block_group = index / inodes_per_group; + uint32_t offset_in_group = index % inodes_per_group; + + /* Load block group, where i-node is located */ + struct ext4_block_group_ref bg_ref; + + int rc = ext4_fs_get_block_group_ref(fs, block_group, &bg_ref); + if (rc != EOK) { + return rc; + } + + /* Load block address, where i-node table is located */ + uint32_t inode_table_start = + ext4_bg_get_inode_table_first_block(bg_ref.block_group, + &fs->sb); + + /* Put back block group reference (not needed more) */ + rc = ext4_fs_put_block_group_ref(&bg_ref); + if (rc != EOK) { + return rc; + } + + /* Compute position of i-node in the block group */ + uint16_t inode_size = ext4_get16(&fs->sb, inode_size); + uint32_t block_size = ext4_sb_get_block_size(&fs->sb); + uint32_t byte_offset_in_group = offset_in_group * inode_size; + + /* Compute block address */ + uint64_t block_id = inode_table_start + (byte_offset_in_group / block_size); + + + rc = ext4_block_get(fs->bdev, &ref->block, block_id); + if (rc != EOK) { + return rc; + } + + /* Compute position of i-node in the data block */ + uint32_t offset_in_block = byte_offset_in_group % block_size; + ref->inode = (struct ext4_inode *)(ref->block.data + offset_in_block); + + /* We need to store the original value of index in the reference */ + ref->index = index + 1; + ref->fs = fs; + ref->dirty = false; + + return EOK; } int ext4_fs_put_inode_ref(struct ext4_inode_ref *ref) { - /* Check if reference modified */ - if (ref->dirty) { - /* Mark block dirty for writing changes to physical device */ - ref->block.dirty = true; - } - - /* Put back block, that contains i-node */ - return ext4_block_set(ref->fs->bdev, &ref->block); + /* Check if reference modified */ + if (ref->dirty) { + /* Mark block dirty for writing changes to physical device */ + ref->block.dirty = true; + } + + /* Put back block, that contains i-node */ + return ext4_block_set(ref->fs->bdev, &ref->block); } int ext4_fs_alloc_inode(struct ext4_fs *fs, struct ext4_inode_ref *inode_ref, bool is_directory) { - /* Check if newly allocated i-node will be a directory */ - uint32_t i; - bool is_dir; - - is_dir = is_directory; - - /* Allocate inode by allocation algorithm */ - uint32_t index; - int rc = ext4_ialloc_alloc_inode(fs, &index, is_dir); - if (rc != EOK) - return rc; - - /* Load i-node from on-disk i-node table */ - rc = ext4_fs_get_inode_ref(fs, index, inode_ref); - if (rc != EOK) { - ext4_ialloc_free_inode(fs, index, is_dir); - return rc; - } - - /* Initialize i-node */ - struct ext4_inode *inode = inode_ref->inode; - - uint16_t mode; - if (is_dir) { - /* - * Default directory permissions to be compatible with other systems - * 0777 (octal) == rwxrwxrwx - */ - - mode = 0777; - mode |= EXT4_INODE_MODE_DIRECTORY; - ext4_inode_set_mode(&fs->sb, inode, mode); - ext4_inode_set_links_count(inode, 1); /* '.' entry */ - - } else { - /* - * Default file permissions to be compatible with other systems - * 0666 (octal) == rw-rw-rw- - */ - - mode = 0666; - mode |= EXT4_INODE_MODE_FILE; - ext4_inode_set_mode(&fs->sb, inode, mode); - ext4_inode_set_links_count(inode, 0); - } - - ext4_inode_set_uid(inode, 0); - ext4_inode_set_gid(inode, 0); - ext4_inode_set_size(inode, 0); - ext4_inode_set_access_time(inode, 0); - ext4_inode_set_change_inode_time(inode, 0); - ext4_inode_set_modification_time(inode, 0); - ext4_inode_set_deletion_time(inode, 0); - ext4_inode_set_blocks_count(&fs->sb, inode, 0); - ext4_inode_set_flags(inode, 0); - ext4_inode_set_generation(inode, 0); - - /* Reset blocks array */ - for (i = 0; i < EXT4_INODE_BLOCKS; i++) - inode->blocks[i] = 0; + /* Check if newly allocated i-node will be a directory */ + uint32_t i; + bool is_dir; + + is_dir = is_directory; + + /* Allocate inode by allocation algorithm */ + uint32_t index; + int rc = ext4_ialloc_alloc_inode(fs, &index, is_dir); + if (rc != EOK) + return rc; + + /* Load i-node from on-disk i-node table */ + rc = ext4_fs_get_inode_ref(fs, index, inode_ref); + if (rc != EOK) { + ext4_ialloc_free_inode(fs, index, is_dir); + return rc; + } + + /* Initialize i-node */ + struct ext4_inode *inode = inode_ref->inode; + + uint16_t mode; + if (is_dir) { + /* + * Default directory permissions to be compatible with other systems + * 0777 (octal) == rwxrwxrwx + */ + + mode = 0777; + mode |= EXT4_INODE_MODE_DIRECTORY; + ext4_inode_set_mode(&fs->sb, inode, mode); + ext4_inode_set_links_count(inode, 1); /* '.' entry */ + + } else { + /* + * Default file permissions to be compatible with other systems + * 0666 (octal) == rw-rw-rw- + */ + + mode = 0666; + mode |= EXT4_INODE_MODE_FILE; + ext4_inode_set_mode(&fs->sb, inode, mode); + ext4_inode_set_links_count(inode, 0); + } + + ext4_inode_set_uid(inode, 0); + ext4_inode_set_gid(inode, 0); + ext4_inode_set_size(inode, 0); + ext4_inode_set_access_time(inode, 0); + ext4_inode_set_change_inode_time(inode, 0); + ext4_inode_set_modification_time(inode, 0); + ext4_inode_set_deletion_time(inode, 0); + ext4_inode_set_blocks_count(&fs->sb, inode, 0); + ext4_inode_set_flags(inode, 0); + ext4_inode_set_generation(inode, 0); + + /* Reset blocks array */ + for (i = 0; i < EXT4_INODE_BLOCKS; i++) + inode->blocks[i] = 0; #if 0 - /* Initialize extents if needed */ - if (ext4_sb_check_feature_incompatible( - &fs->sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) { - ext4_inode_set_flag(inode, EXT4_INODE_FLAG_EXTENTS); + /* Initialize extents if needed */ + if (ext4_sb_check_feature_incompatible( + &fs->sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) { + ext4_inode_set_flag(inode, EXT4_INODE_FLAG_EXTENTS); - /* Initialize extent root header */ - ext4_extent_header_t *header = ext4_inode_get_extent_header(inode); - ext4_extent_header_set_depth(header, 0); - ext4_extent_header_set_entries_count(header, 0); - ext4_extent_header_set_generation(header, 0); - ext4_extent_header_set_magic(header, EXT4_EXTENT_MAGIC); + /* Initialize extent root header */ + ext4_extent_header_t *header = ext4_inode_get_extent_header(inode); + ext4_extent_header_set_depth(header, 0); + ext4_extent_header_set_entries_count(header, 0); + ext4_extent_header_set_generation(header, 0); + ext4_extent_header_set_magic(header, EXT4_EXTENT_MAGIC); - uint16_t max_entries = (EXT4_INODE_BLOCKS * sizeof(uint32_t) - - sizeof(ext4_extent_header_t)) / sizeof(ext4_extent_t); + uint16_t max_entries = (EXT4_INODE_BLOCKS * sizeof(uint32_t) - + sizeof(ext4_extent_header_t)) / sizeof(ext4_extent_t); - ext4_extent_header_set_max_entries_count(header, max_entries); - } + ext4_extent_header_set_max_entries_count(header, max_entries); + } #endif - inode_ref->dirty = true; + inode_ref->dirty = true; - return EOK; + return EOK; } int ext4_fs_free_inode(struct ext4_inode_ref *inode_ref) { - struct ext4_fs *fs = inode_ref->fs; - uint32_t offset; - uint32_t suboffset; - /* For extents must be data block destroyed by other way */ - if ((ext4_sb_check_feature_incompatible(&fs->sb, - EXT4_FEATURE_INCOMPAT_EXTENTS)) && - (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) { - /* Data structures are released during truncate operation... */ - goto finish; - } - - /* Release all indirect (no data) blocks */ - - /* 1) Single indirect */ - uint32_t fblock = ext4_inode_get_indirect_block(inode_ref->inode, 0); - if (fblock != 0) { - int rc = ext4_balloc_free_block(inode_ref, fblock); - if (rc != EOK) - return rc; - - ext4_inode_set_indirect_block(inode_ref->inode, 0, 0); - } - - uint32_t block_size = ext4_sb_get_block_size(&fs->sb); - uint32_t count = block_size / sizeof(uint32_t); - - struct ext4_block block; - - /* 2) Double indirect */ - fblock = ext4_inode_get_indirect_block(inode_ref->inode, 1); - if (fblock != 0) { - int rc = ext4_block_get(fs->bdev, &block, fblock); - if (rc != EOK) - return rc; - - uint32_t ind_block; - for (offset = 0; offset < count; ++offset) { - ind_block = to_le32(((uint32_t *) block.data)[offset]); - - if (ind_block != 0) { - rc = ext4_balloc_free_block(inode_ref, ind_block); - if (rc != EOK) { - ext4_block_set(fs->bdev, &block); - return rc; - } - } - } - - ext4_block_set(fs->bdev, &block); - rc = ext4_balloc_free_block(inode_ref, fblock); - if (rc != EOK) - return rc; - - ext4_inode_set_indirect_block(inode_ref->inode, 1, 0); - } - - /* 3) Tripple indirect */ - struct ext4_block subblock; - fblock = ext4_inode_get_indirect_block(inode_ref->inode, 2); - if (fblock != 0) { - int rc = ext4_block_get(fs->bdev, &block, fblock); - if (rc != EOK) - return rc; - - uint32_t ind_block; - for ( offset = 0; offset < count; ++offset) { - ind_block = to_le32(((uint32_t *) block.data)[offset]); - - if (ind_block != 0) { - rc = ext4_block_get(fs->bdev, &subblock, ind_block); - if (rc != EOK) { - ext4_block_set(fs->bdev, &block); - return rc; - } - - uint32_t ind_subblock; - for (suboffset = 0; suboffset < count; - ++suboffset) { - ind_subblock = to_le32(((uint32_t *) - subblock.data)[suboffset]); - - if (ind_subblock != 0) { - rc = ext4_balloc_free_block(inode_ref, ind_subblock); - if (rc != EOK) { - ext4_block_set(fs->bdev, &subblock); - ext4_block_set(fs->bdev, &block); - return rc; - } - } - } - - ext4_block_set(fs->bdev, &subblock); - - - rc = ext4_balloc_free_block(inode_ref, ind_block); - if (rc != EOK) { - ext4_block_set(fs->bdev, &block); - return rc; - } - } - } - - ext4_block_set(fs->bdev, &block); - rc = ext4_balloc_free_block(inode_ref, fblock); - if (rc != EOK) - return rc; - - ext4_inode_set_indirect_block(inode_ref->inode, 2, 0); - } - -finish: - /* Mark inode dirty for writing to the physical device */ - inode_ref->dirty = true; - - /* Free block with extended attributes if present */ - uint32_t xattr_block = ext4_inode_get_file_acl( - inode_ref->inode, &fs->sb); - if (xattr_block) { - int rc = ext4_balloc_free_block(inode_ref, xattr_block); - if (rc != EOK) - return rc; - - ext4_inode_set_file_acl(inode_ref->inode, &fs->sb, 0); - } - - /* Free inode by allocator */ - int rc; - if (ext4_inode_is_type(&fs->sb, inode_ref->inode, - EXT4_INODE_MODE_DIRECTORY)) - rc = ext4_ialloc_free_inode(fs, inode_ref->index, true); - else - rc = ext4_ialloc_free_inode(fs, inode_ref->index, false); - - return rc; + struct ext4_fs *fs = inode_ref->fs; + uint32_t offset; + uint32_t suboffset; + /* For extents must be data block destroyed by other way */ + if ((ext4_sb_check_feature_incompatible(&fs->sb, + EXT4_FEATURE_INCOMPAT_EXTENTS)) && + (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) { + /* Data structures are released during truncate operation... */ + goto finish; + } + + /* Release all indirect (no data) blocks */ + + /* 1) Single indirect */ + uint32_t fblock = ext4_inode_get_indirect_block(inode_ref->inode, 0); + if (fblock != 0) { + int rc = ext4_balloc_free_block(inode_ref, fblock); + if (rc != EOK) + return rc; + + ext4_inode_set_indirect_block(inode_ref->inode, 0, 0); + } + + uint32_t block_size = ext4_sb_get_block_size(&fs->sb); + uint32_t count = block_size / sizeof(uint32_t); + + struct ext4_block block; + + /* 2) Double indirect */ + fblock = ext4_inode_get_indirect_block(inode_ref->inode, 1); + if (fblock != 0) { + int rc = ext4_block_get(fs->bdev, &block, fblock); + if (rc != EOK) + return rc; + + uint32_t ind_block; + for (offset = 0; offset < count; ++offset) { + ind_block = to_le32(((uint32_t *) block.data)[offset]); + + if (ind_block != 0) { + rc = ext4_balloc_free_block(inode_ref, ind_block); + if (rc != EOK) { + ext4_block_set(fs->bdev, &block); + return rc; + } + } + } + + ext4_block_set(fs->bdev, &block); + rc = ext4_balloc_free_block(inode_ref, fblock); + if (rc != EOK) + return rc; + + ext4_inode_set_indirect_block(inode_ref->inode, 1, 0); + } + + /* 3) Tripple indirect */ + struct ext4_block subblock; + fblock = ext4_inode_get_indirect_block(inode_ref->inode, 2); + if (fblock != 0) { + int rc = ext4_block_get(fs->bdev, &block, fblock); + if (rc != EOK) + return rc; + + uint32_t ind_block; + for ( offset = 0; offset < count; ++offset) { + ind_block = to_le32(((uint32_t *) block.data)[offset]); + + if (ind_block != 0) { + rc = ext4_block_get(fs->bdev, &subblock, ind_block); + if (rc != EOK) { + ext4_block_set(fs->bdev, &block); + return rc; + } + + uint32_t ind_subblock; + for (suboffset = 0; suboffset < count; + ++suboffset) { + ind_subblock = to_le32(((uint32_t *) + subblock.data)[suboffset]); + + if (ind_subblock != 0) { + rc = ext4_balloc_free_block(inode_ref, ind_subblock); + if (rc != EOK) { + ext4_block_set(fs->bdev, &subblock); + ext4_block_set(fs->bdev, &block); + return rc; + } + } + } + + ext4_block_set(fs->bdev, &subblock); + + + rc = ext4_balloc_free_block(inode_ref, ind_block); + if (rc != EOK) { + ext4_block_set(fs->bdev, &block); + return rc; + } + } + } + + ext4_block_set(fs->bdev, &block); + rc = ext4_balloc_free_block(inode_ref, fblock); + if (rc != EOK) + return rc; + + ext4_inode_set_indirect_block(inode_ref->inode, 2, 0); + } + + finish: + /* Mark inode dirty for writing to the physical device */ + inode_ref->dirty = true; + + /* Free block with extended attributes if present */ + uint32_t xattr_block = ext4_inode_get_file_acl( + inode_ref->inode, &fs->sb); + if (xattr_block) { + int rc = ext4_balloc_free_block(inode_ref, xattr_block); + if (rc != EOK) + return rc; + + ext4_inode_set_file_acl(inode_ref->inode, &fs->sb, 0); + } + + /* Free inode by allocator */ + int rc; + if (ext4_inode_is_type(&fs->sb, inode_ref->inode, + EXT4_INODE_MODE_DIRECTORY)) + rc = ext4_ialloc_free_inode(fs, inode_ref->index, true); + else + rc = ext4_ialloc_free_inode(fs, inode_ref->index, false); + + return rc; } int ext4_fs_truncate_inode(struct ext4_inode_ref *inode_ref, - uint64_t new_size) + uint64_t new_size) { - struct ext4_sblock *sb = &inode_ref->fs->sb; - uint32_t i; - - /* Check flags, if i-node can be truncated */ - if (!ext4_inode_can_truncate(sb, inode_ref->inode)) - return EINVAL; - - /* If sizes are equal, nothing has to be done. */ - uint64_t old_size = ext4_inode_get_size(sb, inode_ref->inode); - if (old_size == new_size) - return EOK; - - /* It's not suppported to make the larger file by truncate operation */ - if (old_size < new_size) - return EINVAL; - - /* Compute how many blocks will be released */ - uint64_t size_diff = old_size - new_size; - uint32_t block_size = ext4_sb_get_block_size(sb); - uint32_t diff_blocks_count = size_diff / block_size; - if (size_diff % block_size != 0) - diff_blocks_count++; - - uint32_t old_blocks_count = old_size / block_size; - if (old_size % block_size != 0) - old_blocks_count++; - - if ((ext4_sb_check_feature_incompatible(sb, - EXT4_FEATURE_INCOMPAT_EXTENTS)) && - (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) { + struct ext4_sblock *sb = &inode_ref->fs->sb; + uint32_t i; + + /* Check flags, if i-node can be truncated */ + if (!ext4_inode_can_truncate(sb, inode_ref->inode)) + return EINVAL; + + /* If sizes are equal, nothing has to be done. */ + uint64_t old_size = ext4_inode_get_size(sb, inode_ref->inode); + if (old_size == new_size) + return EOK; + + /* It's not suppported to make the larger file by truncate operation */ + if (old_size < new_size) + return EINVAL; + + /* Compute how many blocks will be released */ + uint64_t size_diff = old_size - new_size; + uint32_t block_size = ext4_sb_get_block_size(sb); + uint32_t diff_blocks_count = size_diff / block_size; + if (size_diff % block_size != 0) + diff_blocks_count++; + + uint32_t old_blocks_count = old_size / block_size; + if (old_size % block_size != 0) + old_blocks_count++; + + if ((ext4_sb_check_feature_incompatible(sb, + EXT4_FEATURE_INCOMPAT_EXTENTS)) && + (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) { #if 0 - /* Extents require special operation */ - int rc = ext4_extent_release_blocks_from(inode_ref, - old_blocks_count - diff_blocks_count); - if (rc != EOK) - return rc; + /* Extents require special operation */ + int rc = ext4_extent_release_blocks_from(inode_ref, + old_blocks_count - diff_blocks_count); + if (rc != EOK) + return rc; #endif - } else { - /* Release data blocks from the end of file */ - - /* Starting from 1 because of logical blocks are numbered from 0 */ - for (i = 1; i <= diff_blocks_count; ++i) { - int rc = ext4_fs_release_inode_block(inode_ref, - old_blocks_count - i); - if (rc != EOK) - return rc; - } - } - - /* Update i-node */ - ext4_inode_set_size(inode_ref->inode, new_size); - inode_ref->dirty = true; - - return EOK; + } else { + /* Release data blocks from the end of file */ + + /* Starting from 1 because of logical blocks are numbered from 0 */ + for (i = 1; i <= diff_blocks_count; ++i) { + int rc = ext4_fs_release_inode_block(inode_ref, + old_blocks_count - i); + if (rc != EOK) + return rc; + } + } + + /* Update i-node */ + ext4_inode_set_size(inode_ref->inode, new_size); + inode_ref->dirty = true; + + return EOK; } int ext4_fs_get_inode_data_block_index(struct ext4_inode_ref *inode_ref, - uint64_t iblock, uint32_t *fblock) + uint64_t iblock, uint32_t *fblock) { - struct ext4_fs *fs = inode_ref->fs; + struct ext4_fs *fs = inode_ref->fs; - /* For empty file is situation simple */ - if (ext4_inode_get_size(&fs->sb, inode_ref->inode) == 0) { - *fblock = 0; - return EOK; - } + /* For empty file is situation simple */ + if (ext4_inode_get_size(&fs->sb, inode_ref->inode) == 0) { + *fblock = 0; + return EOK; + } - uint32_t current_block; + uint32_t current_block; - /* Handle i-node using extents */ - if ((ext4_sb_check_feature_incompatible(&fs->sb, - EXT4_FEATURE_INCOMPAT_EXTENTS)) && - (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) { + /* Handle i-node using extents */ + if ((ext4_sb_check_feature_incompatible(&fs->sb, + EXT4_FEATURE_INCOMPAT_EXTENTS)) && + (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) { #if 0 - int rc = ext4_extent_find_block(inode_ref, iblock, ¤t_block); - if (rc != EOK) - return rc; + int rc = ext4_extent_find_block(inode_ref, iblock, ¤t_block); + if (rc != EOK) + return rc; - *fblock = current_block; - return EOK; + *fblock = current_block; + return EOK; #endif - } - - struct ext4_inode *inode = inode_ref->inode; - - /* Direct block are read directly from array in i-node structure */ - if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) { - current_block = ext4_inode_get_direct_block(inode, (uint32_t) iblock); - *fblock = current_block; - return EOK; - } - - /* Determine indirection level of the target block */ - unsigned int level = 0; - unsigned int i; - for (i = 1; i < 4; i++) { - if (iblock < fs->inode_block_limits[i]) { - level = i; - break; - } - } - - if (level == 0) - return EIO; - - /* Compute offsets for the topmost level */ - uint64_t block_offset_in_level = - iblock - fs->inode_block_limits[level - 1]; - current_block = ext4_inode_get_indirect_block(inode, level - 1); - uint32_t offset_in_block = - block_offset_in_level / fs->inode_blocks_per_level[level - 1]; - - /* Sparse file */ - if (current_block == 0) { - *fblock = 0; - return EOK; - } - - struct ext4_block block; - - /* - * Navigate through other levels, until we find the block number - * or find null reference meaning we are dealing with sparse file - */ - while (level > 0) { - /* Load indirect block */ - int rc = ext4_block_get(fs->bdev, &block, current_block); - if (rc != EOK) - return rc; - - /* Read block address from indirect block */ - current_block = - to_le32(((uint32_t *) block.data)[offset_in_block]); - - /* Put back indirect block untouched */ - rc = ext4_block_set(fs->bdev, &block); - if (rc != EOK) - return rc; - - /* Check for sparse file */ - if (current_block == 0) { - *fblock = 0; - return EOK; - } - - /* Jump to the next level */ - level--; - - /* Termination condition - we have address of data block loaded */ - if (level == 0) - break; - - /* Visit the next level */ - block_offset_in_level %= fs->inode_blocks_per_level[level]; - offset_in_block = - block_offset_in_level / fs->inode_blocks_per_level[level - 1]; - } - - *fblock = current_block; - - return EOK; + } + + struct ext4_inode *inode = inode_ref->inode; + + /* Direct block are read directly from array in i-node structure */ + if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) { + current_block = ext4_inode_get_direct_block(inode, (uint32_t) iblock); + *fblock = current_block; + return EOK; + } + + /* Determine indirection level of the target block */ + unsigned int level = 0; + unsigned int i; + for (i = 1; i < 4; i++) { + if (iblock < fs->inode_block_limits[i]) { + level = i; + break; + } + } + + if (level == 0) + return EIO; + + /* Compute offsets for the topmost level */ + uint64_t block_offset_in_level = + iblock - fs->inode_block_limits[level - 1]; + current_block = ext4_inode_get_indirect_block(inode, level - 1); + uint32_t offset_in_block = + block_offset_in_level / fs->inode_blocks_per_level[level - 1]; + + /* Sparse file */ + if (current_block == 0) { + *fblock = 0; + return EOK; + } + + struct ext4_block block; + + /* + * Navigate through other levels, until we find the block number + * or find null reference meaning we are dealing with sparse file + */ + while (level > 0) { + /* Load indirect block */ + int rc = ext4_block_get(fs->bdev, &block, current_block); + if (rc != EOK) + return rc; + + /* Read block address from indirect block */ + current_block = + to_le32(((uint32_t *) block.data)[offset_in_block]); + + /* Put back indirect block untouched */ + rc = ext4_block_set(fs->bdev, &block); + if (rc != EOK) + return rc; + + /* Check for sparse file */ + if (current_block == 0) { + *fblock = 0; + return EOK; + } + + /* Jump to the next level */ + level--; + + /* Termination condition - we have address of data block loaded */ + if (level == 0) + break; + + /* Visit the next level */ + block_offset_in_level %= fs->inode_blocks_per_level[level]; + offset_in_block = + block_offset_in_level / fs->inode_blocks_per_level[level - 1]; + } + + *fblock = current_block; + + return EOK; } int ext4_fs_set_inode_data_block_index(struct ext4_inode_ref *inode_ref, - uint64_t iblock, uint32_t fblock) + uint64_t iblock, uint32_t fblock) { - struct ext4_fs *fs = inode_ref->fs; - - /* Handle inode using extents */ - if ((ext4_sb_check_feature_incompatible(&fs->sb, - EXT4_FEATURE_INCOMPAT_EXTENTS)) && - (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) { - /* Not reachable */ - return ENOTSUP; - } - - /* Handle simple case when we are dealing with direct reference */ - if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) { - ext4_inode_set_direct_block(inode_ref->inode, (uint32_t) iblock, fblock); - inode_ref->dirty = true; - - return EOK; - } - - /* Determine the indirection level needed to get the desired block */ - unsigned int level = 0; - unsigned int i; - for (i = 1; i < 4; i++) { - if (iblock < fs->inode_block_limits[i]) { - level = i; - break; - } - } - - if (level == 0) - return EIO; - - uint32_t block_size = ext4_sb_get_block_size(&fs->sb); - - /* Compute offsets for the topmost level */ - uint64_t block_offset_in_level = - iblock - fs->inode_block_limits[level - 1]; - uint32_t current_block = - ext4_inode_get_indirect_block(inode_ref->inode, level - 1); - uint32_t offset_in_block = - block_offset_in_level / fs->inode_blocks_per_level[level - 1]; - - uint32_t new_block_addr; - - struct ext4_block block; - struct ext4_block new_block; - - /* Is needed to allocate indirect block on the i-node level */ - if (current_block == 0) { - /* Allocate new indirect block */ - int rc = ext4_balloc_alloc_block(inode_ref, &new_block_addr); - if (rc != EOK) - return rc; - - /* Update i-node */ - ext4_inode_set_indirect_block(inode_ref->inode, level - 1, - new_block_addr); - inode_ref->dirty = true; - - /* Load newly allocated block */ - rc = ext4_block_get(fs->bdev, &new_block, new_block_addr); - if (rc != EOK) { - ext4_balloc_free_block(inode_ref, new_block_addr); - return rc; - } - - /* Initialize new block */ - memset(new_block.data, 0, block_size); - new_block.dirty = true; - - /* Put back the allocated block */ - rc = ext4_block_set(fs->bdev, &new_block); - if (rc != EOK) - return rc; - - current_block = new_block_addr; - } - - /* - * Navigate through other levels, until we find the block number - * or find null reference meaning we are dealing with sparse file - */ - while (level > 0) { - int rc = ext4_block_get(fs->bdev, &block, current_block); - if (rc != EOK) - return rc; - - current_block = - to_le32(((uint32_t *) block.data)[offset_in_block]); - - if ((level > 1) && (current_block == 0)) { - /* Allocate new block */ - rc = ext4_balloc_alloc_block(inode_ref, &new_block_addr); - if (rc != EOK) { - ext4_block_set(fs->bdev, &block); - return rc; - } - - /* Load newly allocated block */ - rc = ext4_block_get(fs->bdev, &new_block, new_block_addr); - - if (rc != EOK) { - ext4_block_set(fs->bdev, &block); - return rc; - } - - /* Initialize allocated block */ - memset(new_block.data, 0, block_size); - new_block.dirty = true; - - rc = ext4_block_set(fs->bdev, &new_block); - if (rc != EOK) { - ext4_block_set(fs->bdev, &block); - return rc; - } - - /* Write block address to the parent */ - ((uint32_t *) block.data)[offset_in_block] = - to_le32(new_block_addr); - block.dirty = true; - current_block = new_block_addr; - } - - /* Will be finished, write the fblock address */ - if (level == 1) { - ((uint32_t *) block.data)[offset_in_block] = - to_le32(fblock); - block.dirty = true; - } - - rc = ext4_block_set(fs->bdev, &block); - if (rc != EOK) - return rc; - - level--; - - /* - * If we are on the last level, break here as - * there is no next level to visit - */ - if (level == 0) - break; - - /* Visit the next level */ - block_offset_in_level %= fs->inode_blocks_per_level[level]; - offset_in_block = - block_offset_in_level / fs->inode_blocks_per_level[level - 1]; - } - - return EOK; + struct ext4_fs *fs = inode_ref->fs; + + /* Handle inode using extents */ + if ((ext4_sb_check_feature_incompatible(&fs->sb, + EXT4_FEATURE_INCOMPAT_EXTENTS)) && + (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) { + /* Not reachable */ + return ENOTSUP; + } + + /* Handle simple case when we are dealing with direct reference */ + if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) { + ext4_inode_set_direct_block(inode_ref->inode, (uint32_t) iblock, fblock); + inode_ref->dirty = true; + + return EOK; + } + + /* Determine the indirection level needed to get the desired block */ + unsigned int level = 0; + unsigned int i; + for (i = 1; i < 4; i++) { + if (iblock < fs->inode_block_limits[i]) { + level = i; + break; + } + } + + if (level == 0) + return EIO; + + uint32_t block_size = ext4_sb_get_block_size(&fs->sb); + + /* Compute offsets for the topmost level */ + uint64_t block_offset_in_level = + iblock - fs->inode_block_limits[level - 1]; + uint32_t current_block = + ext4_inode_get_indirect_block(inode_ref->inode, level - 1); + uint32_t offset_in_block = + block_offset_in_level / fs->inode_blocks_per_level[level - 1]; + + uint32_t new_block_addr; + + struct ext4_block block; + struct ext4_block new_block; + + /* Is needed to allocate indirect block on the i-node level */ + if (current_block == 0) { + /* Allocate new indirect block */ + int rc = ext4_balloc_alloc_block(inode_ref, &new_block_addr); + if (rc != EOK) + return rc; + + /* Update i-node */ + ext4_inode_set_indirect_block(inode_ref->inode, level - 1, + new_block_addr); + inode_ref->dirty = true; + + /* Load newly allocated block */ + rc = ext4_block_get(fs->bdev, &new_block, new_block_addr); + if (rc != EOK) { + ext4_balloc_free_block(inode_ref, new_block_addr); + return rc; + } + + /* Initialize new block */ + memset(new_block.data, 0, block_size); + new_block.dirty = true; + + /* Put back the allocated block */ + rc = ext4_block_set(fs->bdev, &new_block); + if (rc != EOK) + return rc; + + current_block = new_block_addr; + } + + /* + * Navigate through other levels, until we find the block number + * or find null reference meaning we are dealing with sparse file + */ + while (level > 0) { + int rc = ext4_block_get(fs->bdev, &block, current_block); + if (rc != EOK) + return rc; + + current_block = + to_le32(((uint32_t *) block.data)[offset_in_block]); + + if ((level > 1) && (current_block == 0)) { + /* Allocate new block */ + rc = ext4_balloc_alloc_block(inode_ref, &new_block_addr); + if (rc != EOK) { + ext4_block_set(fs->bdev, &block); + return rc; + } + + /* Load newly allocated block */ + rc = ext4_block_get(fs->bdev, &new_block, new_block_addr); + + if (rc != EOK) { + ext4_block_set(fs->bdev, &block); + return rc; + } + + /* Initialize allocated block */ + memset(new_block.data, 0, block_size); + new_block.dirty = true; + + rc = ext4_block_set(fs->bdev, &new_block); + if (rc != EOK) { + ext4_block_set(fs->bdev, &block); + return rc; + } + + /* Write block address to the parent */ + ((uint32_t *) block.data)[offset_in_block] = + to_le32(new_block_addr); + block.dirty = true; + current_block = new_block_addr; + } + + /* Will be finished, write the fblock address */ + if (level == 1) { + ((uint32_t *) block.data)[offset_in_block] = + to_le32(fblock); + block.dirty = true; + } + + rc = ext4_block_set(fs->bdev, &block); + if (rc != EOK) + return rc; + + level--; + + /* + * If we are on the last level, break here as + * there is no next level to visit + */ + if (level == 0) + break; + + /* Visit the next level */ + block_offset_in_level %= fs->inode_blocks_per_level[level]; + offset_in_block = + block_offset_in_level / fs->inode_blocks_per_level[level - 1]; + } + + return EOK; } int ext4_fs_release_inode_block(struct ext4_inode_ref *inode_ref, - uint32_t iblock) + uint32_t iblock) { - uint32_t fblock; - - struct ext4_fs *fs = inode_ref->fs; - - /* Extents are handled otherwise = there is not support in this function */ - ext4_assert(!(ext4_sb_check_feature_incompatible(&fs->sb, - EXT4_FEATURE_INCOMPAT_EXTENTS) && - (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS)))); - - struct ext4_inode *inode = inode_ref->inode; - - /* Handle simple case when we are dealing with direct reference */ - if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) { - fblock = ext4_inode_get_direct_block(inode, iblock); - - /* Sparse file */ - if (fblock == 0) - return EOK; - - ext4_inode_set_direct_block(inode, iblock, 0); - return ext4_balloc_free_block(inode_ref, fblock); - } - - /* Determine the indirection level needed to get the desired block */ - unsigned int level = 0; - unsigned int i; - for (i = 1; i < 4; i++) { - if (iblock < fs->inode_block_limits[i]) { - level = i; - break; - } - } - - if (level == 0) - return EIO; - - /* Compute offsets for the topmost level */ - uint64_t block_offset_in_level = - iblock - fs->inode_block_limits[level - 1]; - uint32_t current_block = - ext4_inode_get_indirect_block(inode, level - 1); - uint32_t offset_in_block = - block_offset_in_level / fs->inode_blocks_per_level[level - 1]; - - /* - * Navigate through other levels, until we find the block number - * or find null reference meaning we are dealing with sparse file - */ - struct ext4_block block; - - while (level > 0) { - - /* Sparse check */ - if (current_block == 0) - return EOK; - - int rc = ext4_block_get(fs->bdev, &block, current_block); - if (rc != EOK) - return rc; - - current_block = - to_le32(((uint32_t *) block.data)[offset_in_block]); - - /* Set zero if physical data block address found */ - if (level == 1) { - ((uint32_t *) block.data)[offset_in_block] = - to_le32(0); - block.dirty = true; - } - - rc = ext4_block_set(fs->bdev, &block); - if (rc != EOK) - return rc; - - level--; - - /* - * If we are on the last level, break here as - * there is no next level to visit - */ - if (level == 0) - break; - - /* Visit the next level */ - block_offset_in_level %= fs->inode_blocks_per_level[level]; - offset_in_block = - block_offset_in_level / fs->inode_blocks_per_level[level - 1]; - } - - fblock = current_block; - if (fblock == 0) - return EOK; - - /* Physical block is not referenced, it can be released */ - return ext4_balloc_free_block(inode_ref, fblock); + uint32_t fblock; + + struct ext4_fs *fs = inode_ref->fs; + + /* Extents are handled otherwise = there is not support in this function */ + ext4_assert(!(ext4_sb_check_feature_incompatible(&fs->sb, + EXT4_FEATURE_INCOMPAT_EXTENTS) && + (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS)))); + + struct ext4_inode *inode = inode_ref->inode; + + /* Handle simple case when we are dealing with direct reference */ + if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) { + fblock = ext4_inode_get_direct_block(inode, iblock); + + /* Sparse file */ + if (fblock == 0) + return EOK; + + ext4_inode_set_direct_block(inode, iblock, 0); + return ext4_balloc_free_block(inode_ref, fblock); + } + + /* Determine the indirection level needed to get the desired block */ + unsigned int level = 0; + unsigned int i; + for (i = 1; i < 4; i++) { + if (iblock < fs->inode_block_limits[i]) { + level = i; + break; + } + } + + if (level == 0) + return EIO; + + /* Compute offsets for the topmost level */ + uint64_t block_offset_in_level = + iblock - fs->inode_block_limits[level - 1]; + uint32_t current_block = + ext4_inode_get_indirect_block(inode, level - 1); + uint32_t offset_in_block = + block_offset_in_level / fs->inode_blocks_per_level[level - 1]; + + /* + * Navigate through other levels, until we find the block number + * or find null reference meaning we are dealing with sparse file + */ + struct ext4_block block; + + while (level > 0) { + + /* Sparse check */ + if (current_block == 0) + return EOK; + + int rc = ext4_block_get(fs->bdev, &block, current_block); + if (rc != EOK) + return rc; + + current_block = + to_le32(((uint32_t *) block.data)[offset_in_block]); + + /* Set zero if physical data block address found */ + if (level == 1) { + ((uint32_t *) block.data)[offset_in_block] = + to_le32(0); + block.dirty = true; + } + + rc = ext4_block_set(fs->bdev, &block); + if (rc != EOK) + return rc; + + level--; + + /* + * If we are on the last level, break here as + * there is no next level to visit + */ + if (level == 0) + break; + + /* Visit the next level */ + block_offset_in_level %= fs->inode_blocks_per_level[level]; + offset_in_block = + block_offset_in_level / fs->inode_blocks_per_level[level - 1]; + } + + fblock = current_block; + if (fblock == 0) + return EOK; + + /* Physical block is not referenced, it can be released */ + return ext4_balloc_free_block(inode_ref, fblock); } int ext4_fs_append_inode_block(struct ext4_inode_ref *inode_ref, - uint32_t *fblock, uint32_t *iblock) + uint32_t *fblock, uint32_t *iblock) { - /* Handle extents separately */ - if ((ext4_sb_check_feature_incompatible(&inode_ref->fs->sb, - EXT4_FEATURE_INCOMPAT_EXTENTS)) && - (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))){ + /* Handle extents separately */ + if ((ext4_sb_check_feature_incompatible(&inode_ref->fs->sb, + EXT4_FEATURE_INCOMPAT_EXTENTS)) && + (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))){ #if 0 - return ext4_extent_append_block(inode_ref, iblock, fblock, true); + return ext4_extent_append_block(inode_ref, iblock, fblock, true); #endif - } + } - struct ext4_sblock *sb = &inode_ref->fs->sb; + struct ext4_sblock *sb = &inode_ref->fs->sb; - /* Compute next block index and allocate data block */ - uint64_t inode_size = ext4_inode_get_size(sb, inode_ref->inode); - uint32_t block_size = ext4_sb_get_block_size(sb); + /* Compute next block index and allocate data block */ + uint64_t inode_size = ext4_inode_get_size(sb, inode_ref->inode); + uint32_t block_size = ext4_sb_get_block_size(sb); - /* Align size i-node size */ - if ((inode_size % block_size) != 0) - inode_size += block_size - (inode_size % block_size); + /* Align size i-node size */ + if ((inode_size % block_size) != 0) + inode_size += block_size - (inode_size % block_size); - /* Logical blocks are numbered from 0 */ - uint32_t new_block_idx = inode_size / block_size; + /* Logical blocks are numbered from 0 */ + uint32_t new_block_idx = inode_size / block_size; - /* Allocate new physical block */ - uint32_t phys_block; - int rc = ext4_balloc_alloc_block(inode_ref, &phys_block); - if (rc != EOK) - return rc; + /* Allocate new physical block */ + uint32_t phys_block; + int rc = ext4_balloc_alloc_block(inode_ref, &phys_block); + if (rc != EOK) + return rc; - /* Add physical block address to the i-node */ - rc = ext4_fs_set_inode_data_block_index(inode_ref, - new_block_idx, phys_block); - if (rc != EOK) { - ext4_balloc_free_block(inode_ref, phys_block); - return rc; - } + /* Add physical block address to the i-node */ + rc = ext4_fs_set_inode_data_block_index(inode_ref, + new_block_idx, phys_block); + if (rc != EOK) { + ext4_balloc_free_block(inode_ref, phys_block); + return rc; + } - /* Update i-node */ - ext4_inode_set_size(inode_ref->inode, inode_size + block_size); - inode_ref->dirty = true; + /* Update i-node */ + ext4_inode_set_size(inode_ref->inode, inode_size + block_size); + inode_ref->dirty = true; - *fblock = phys_block; - *iblock = new_block_idx; + *fblock = phys_block; + *iblock = new_block_idx; - return EOK; + return EOK; } /** diff --git a/src/lwext4/ext4_fs.h b/src/lwext4/ext4_fs.h index 5b622a7..8f59768 100644 --- a/src/lwext4/ext4_fs.h +++ b/src/lwext4/ext4_fs.h @@ -70,19 +70,19 @@ int ext4_fs_alloc_inode(struct ext4_fs *fs, struct ext4_inode_ref *inode_ref, bo int ext4_fs_free_inode(struct ext4_inode_ref *inode_ref); int ext4_fs_truncate_inode(struct ext4_inode_ref *inode_ref, - uint64_t new_size); + uint64_t new_size); int ext4_fs_get_inode_data_block_index(struct ext4_inode_ref *inode_ref, - uint64_t iblock, uint32_t *fblock); + uint64_t iblock, uint32_t *fblock); int ext4_fs_set_inode_data_block_index(struct ext4_inode_ref *inode_ref, - uint64_t iblock, uint32_t fblock); + uint64_t iblock, uint32_t fblock); int ext4_fs_release_inode_block(struct ext4_inode_ref *inode_ref, - uint32_t iblock); + uint32_t iblock); int ext4_fs_append_inode_block(struct ext4_inode_ref *inode_ref, - uint32_t *fblock, uint32_t *iblock); + uint32_t *fblock, uint32_t *iblock); #endif /* EXT4_FS_H_ */ diff --git a/src/lwext4/ext4_hash.c b/src/lwext4/ext4_hash.c index dc1b816..f3cdce5 100644 --- a/src/lwext4/ext4_hash.c +++ b/src/lwext4/ext4_hash.c @@ -79,18 +79,18 @@ * Rotation is separated from addition to prevent recomputation.
*/
#define FF(a, b, c, d, x, s) { \
- (a) += F ((b), (c), (d)) + (x); \
- (a) = ROTATE_LEFT ((a), (s)); \
+ (a) += F ((b), (c), (d)) + (x); \
+ (a) = ROTATE_LEFT ((a), (s)); \
}
#define GG(a, b, c, d, x, s) { \
- (a) += G ((b), (c), (d)) + (x) + (uint32_t)0x5A827999; \
- (a) = ROTATE_LEFT ((a), (s)); \
+ (a) += G ((b), (c), (d)) + (x) + (uint32_t)0x5A827999; \
+ (a) = ROTATE_LEFT ((a), (s)); \
}
#define HH(a, b, c, d, x, s) { \
- (a) += H ((b), (c), (d)) + (x) + (uint32_t)0x6ED9EBA1; \
- (a) = ROTATE_LEFT ((a), (s)); \
+ (a) += H ((b), (c), (d)) + (x) + (uint32_t)0x6ED9EBA1; \
+ (a) = ROTATE_LEFT ((a), (s)); \
}
/*
@@ -107,42 +107,42 @@ static void
ext2_half_md4(uint32_t hash[4], uint32_t data[8])
{
- uint32_t a = hash[0], b = hash[1], c = hash[2], d = hash[3];
-
- /* Round 1 */
- FF(a, b, c, d, data[0], 3);
- FF(d, a, b, c, data[1], 7);
- FF(c, d, a, b, data[2], 11);
- FF(b, c, d, a, data[3], 19);
- FF(a, b, c, d, data[4], 3);
- FF(d, a, b, c, data[5], 7);
- FF(c, d, a, b, data[6], 11);
- FF(b, c, d, a, data[7], 19);
-
- /* Round 2 */
- GG(a, b, c, d, data[1], 3);
- GG(d, a, b, c, data[3], 5);
- GG(c, d, a, b, data[5], 9);
- GG(b, c, d, a, data[7], 13);
- GG(a, b, c, d, data[0], 3);
- GG(d, a, b, c, data[2], 5);
- GG(c, d, a, b, data[4], 9);
- GG(b, c, d, a, data[6], 13);
-
- /* Round 3 */
- HH(a, b, c, d, data[3], 3);
- HH(d, a, b, c, data[7], 9);
- HH(c, d, a, b, data[2], 11);
- HH(b, c, d, a, data[6], 15);
- HH(a, b, c, d, data[1], 3);
- HH(d, a, b, c, data[5], 9);
- HH(c, d, a, b, data[0], 11);
- HH(b, c, d, a, data[4], 15);
-
- hash[0] += a;
- hash[1] += b;
- hash[2] += c;
- hash[3] += d;
+ uint32_t a = hash[0], b = hash[1], c = hash[2], d = hash[3];
+
+ /* Round 1 */
+ FF(a, b, c, d, data[0], 3);
+ FF(d, a, b, c, data[1], 7);
+ FF(c, d, a, b, data[2], 11);
+ FF(b, c, d, a, data[3], 19);
+ FF(a, b, c, d, data[4], 3);
+ FF(d, a, b, c, data[5], 7);
+ FF(c, d, a, b, data[6], 11);
+ FF(b, c, d, a, data[7], 19);
+
+ /* Round 2 */
+ GG(a, b, c, d, data[1], 3);
+ GG(d, a, b, c, data[3], 5);
+ GG(c, d, a, b, data[5], 9);
+ GG(b, c, d, a, data[7], 13);
+ GG(a, b, c, d, data[0], 3);
+ GG(d, a, b, c, data[2], 5);
+ GG(c, d, a, b, data[4], 9);
+ GG(b, c, d, a, data[6], 13);
+
+ /* Round 3 */
+ HH(a, b, c, d, data[3], 3);
+ HH(d, a, b, c, data[7], 9);
+ HH(c, d, a, b, data[2], 11);
+ HH(b, c, d, a, data[6], 15);
+ HH(a, b, c, d, data[1], 3);
+ HH(d, a, b, c, data[5], 9);
+ HH(c, d, a, b, data[0], 11);
+ HH(b, c, d, a, data[4], 15);
+
+ hash[0] += a;
+ hash[1] += b;
+ hash[2] += c;
+ hash[3] += d;
}
/*
@@ -151,168 +151,168 @@ ext2_half_md4(uint32_t hash[4], uint32_t data[8]) static void
ext2_tea(uint32_t hash[4], uint32_t data[8])
{
- uint32_t tea_delta = 0x9E3779B9;
- uint32_t sum;
- uint32_t x = hash[0], y = hash[1];
- int n = 16;
- int i = 1;
-
- while (n-- > 0) {
- sum = i * tea_delta;
- x += ((y << 4) + data[0]) ^ (y + sum) ^ ((y >> 5) + data[1]);
- y += ((x << 4) + data[2]) ^ (x + sum) ^ ((x >> 5) + data[3]);
- i++;
- }
-
- hash[0] += x;
- hash[1] += y;
+ uint32_t tea_delta = 0x9E3779B9;
+ uint32_t sum;
+ uint32_t x = hash[0], y = hash[1];
+ int n = 16;
+ int i = 1;
+
+ while (n-- > 0) {
+ sum = i * tea_delta;
+ x += ((y << 4) + data[0]) ^ (y + sum) ^ ((y >> 5) + data[1]);
+ y += ((x << 4) + data[2]) ^ (x + sum) ^ ((x >> 5) + data[3]);
+ i++;
+ }
+
+ hash[0] += x;
+ hash[1] += y;
}
static uint32_t
ext2_legacy_hash(const char *name, int len, int unsigned_char)
{
- uint32_t h0, h1 = 0x12A3FE2D, h2 = 0x37ABE8F9;
- uint32_t multi = 0x6D22F5;
- const unsigned char *uname = (const unsigned char *)name;
- const signed char *sname = (const signed char *)name;
- int val, i;
-
- for (i = 0; i < len; i++) {
- if (unsigned_char)
- val = (unsigned int)*uname++;
- else
- val = (int)*sname++;
-
- h0 = h2 + (h1 ^ (val * multi));
- if (h0 & 0x80000000)
- h0 -= 0x7FFFFFFF;
- h2 = h1;
- h1 = h0;
- }
-
- return (h1 << 1);
+ uint32_t h0, h1 = 0x12A3FE2D, h2 = 0x37ABE8F9;
+ uint32_t multi = 0x6D22F5;
+ const unsigned char *uname = (const unsigned char *)name;
+ const signed char *sname = (const signed char *)name;
+ int val, i;
+
+ for (i = 0; i < len; i++) {
+ if (unsigned_char)
+ val = (unsigned int)*uname++;
+ else
+ val = (int)*sname++;
+
+ h0 = h2 + (h1 ^ (val * multi));
+ if (h0 & 0x80000000)
+ h0 -= 0x7FFFFFFF;
+ h2 = h1;
+ h1 = h0;
+ }
+
+ return (h1 << 1);
}
static void
ext2_prep_hashbuf(const char *src, int slen, uint32_t *dst, int dlen,
- int unsigned_char)
+ int unsigned_char)
{
- uint32_t padding = slen | (slen << 8) | (slen << 16) | (slen << 24);
- uint32_t buf_val;
- int len, i;
- int buf_byte;
- const unsigned char *ubuf = (const unsigned char *)src;
- const signed char *sbuf = (const signed char *)src;
-
- if (slen > dlen)
- len = dlen;
- else
- len = slen;
-
- buf_val = padding;
-
- for (i = 0; i < len; i++) {
- if (unsigned_char)
- buf_byte = (unsigned int)ubuf[i];
- else
- buf_byte = (int)sbuf[i];
-
- if ((i % 4) == 0)
- buf_val = padding;
-
- buf_val <<= 8;
- buf_val += buf_byte;
-
- if ((i % 4) == 3) {
- *dst++ = buf_val;
- dlen -= sizeof(uint32_t);
- buf_val = padding;
- }
- }
-
- dlen -= sizeof(uint32_t);
- if (dlen >= 0)
- *dst++ = buf_val;
-
- dlen -= sizeof(uint32_t);
- while (dlen >= 0) {
- *dst++ = padding;
- dlen -= sizeof(uint32_t);
- }
+ uint32_t padding = slen | (slen << 8) | (slen << 16) | (slen << 24);
+ uint32_t buf_val;
+ int len, i;
+ int buf_byte;
+ const unsigned char *ubuf = (const unsigned char *)src;
+ const signed char *sbuf = (const signed char *)src;
+
+ if (slen > dlen)
+ len = dlen;
+ else
+ len = slen;
+
+ buf_val = padding;
+
+ for (i = 0; i < len; i++) {
+ if (unsigned_char)
+ buf_byte = (unsigned int)ubuf[i];
+ else
+ buf_byte = (int)sbuf[i];
+
+ if ((i % 4) == 0)
+ buf_val = padding;
+
+ buf_val <<= 8;
+ buf_val += buf_byte;
+
+ if ((i % 4) == 3) {
+ *dst++ = buf_val;
+ dlen -= sizeof(uint32_t);
+ buf_val = padding;
+ }
+ }
+
+ dlen -= sizeof(uint32_t);
+ if (dlen >= 0)
+ *dst++ = buf_val;
+
+ dlen -= sizeof(uint32_t);
+ while (dlen >= 0) {
+ *dst++ = padding;
+ dlen -= sizeof(uint32_t);
+ }
}
int
ext2_htree_hash(const char *name, int len,
- const uint32_t *hash_seed, int hash_version,
- uint32_t *hash_major, uint32_t *hash_minor)
+ const uint32_t *hash_seed, int hash_version,
+ uint32_t *hash_major, uint32_t *hash_minor)
{
- uint32_t hash[4];
- uint32_t data[8];
- uint32_t major = 0, minor = 0;
- int unsigned_char = 0;
-
- if (!name || !hash_major)
- return (-1);
-
- if (len < 1 || len > 255)
- goto error;
-
- hash[0] = 0x67452301;
- hash[1] = 0xEFCDAB89;
- hash[2] = 0x98BADCFE;
- hash[3] = 0x10325476;
-
- if (hash_seed)
- memcpy(hash, hash_seed, sizeof(hash));
-
- switch (hash_version) {
- case EXT2_HTREE_TEA_UNSIGNED:
- unsigned_char = 1;
- case EXT2_HTREE_TEA:
- while (len > 0) {
- ext2_prep_hashbuf(name, len, data, 16, unsigned_char);
- ext2_tea(hash, data);
- len -= 16;
- name += 16;
- }
- major = hash[0];
- minor = hash[1];
- break;
- case EXT2_HTREE_LEGACY_UNSIGNED:
- unsigned_char = 1;
- case EXT2_HTREE_LEGACY:
- major = ext2_legacy_hash(name, len, unsigned_char);
- break;
- case EXT2_HTREE_HALF_MD4_UNSIGNED:
- unsigned_char = 1;
- case EXT2_HTREE_HALF_MD4:
- while (len > 0) {
- ext2_prep_hashbuf(name, len, data, 32, unsigned_char);
- ext2_half_md4(hash, data);
- len -= 32;
- name += 32;
- }
- major = hash[0];
- minor = hash[1];
- break;
- default:
- goto error;
- }
-
- major &= ~1;
- if (major == (EXT2_HTREE_EOF << 1))
- major = (EXT2_HTREE_EOF - 1) << 1;
- *hash_major = major;
- if (hash_minor)
- *hash_minor = minor;
-
- return EOK;
-
-error:
- *hash_major = 0;
- if (hash_minor)
- *hash_minor = 0;
- return ENOTSUP;
+ uint32_t hash[4];
+ uint32_t data[8];
+ uint32_t major = 0, minor = 0;
+ int unsigned_char = 0;
+
+ if (!name || !hash_major)
+ return (-1);
+
+ if (len < 1 || len > 255)
+ goto error;
+
+ hash[0] = 0x67452301;
+ hash[1] = 0xEFCDAB89;
+ hash[2] = 0x98BADCFE;
+ hash[3] = 0x10325476;
+
+ if (hash_seed)
+ memcpy(hash, hash_seed, sizeof(hash));
+
+ switch (hash_version) {
+ case EXT2_HTREE_TEA_UNSIGNED:
+ unsigned_char = 1;
+ case EXT2_HTREE_TEA:
+ while (len > 0) {
+ ext2_prep_hashbuf(name, len, data, 16, unsigned_char);
+ ext2_tea(hash, data);
+ len -= 16;
+ name += 16;
+ }
+ major = hash[0];
+ minor = hash[1];
+ break;
+ case EXT2_HTREE_LEGACY_UNSIGNED:
+ unsigned_char = 1;
+ case EXT2_HTREE_LEGACY:
+ major = ext2_legacy_hash(name, len, unsigned_char);
+ break;
+ case EXT2_HTREE_HALF_MD4_UNSIGNED:
+ unsigned_char = 1;
+ case EXT2_HTREE_HALF_MD4:
+ while (len > 0) {
+ ext2_prep_hashbuf(name, len, data, 32, unsigned_char);
+ ext2_half_md4(hash, data);
+ len -= 32;
+ name += 32;
+ }
+ major = hash[0];
+ minor = hash[1];
+ break;
+ default:
+ goto error;
+ }
+
+ major &= ~1;
+ if (major == (EXT2_HTREE_EOF << 1))
+ major = (EXT2_HTREE_EOF - 1) << 1;
+ *hash_major = major;
+ if (hash_minor)
+ *hash_minor = minor;
+
+ return EOK;
+
+ error:
+ *hash_major = 0;
+ if (hash_minor)
+ *hash_minor = 0;
+ return ENOTSUP;
}
/**
diff --git a/src/lwext4/ext4_hash.h b/src/lwext4/ext4_hash.h index 738a5d7..6464a2c 100644 --- a/src/lwext4/ext4_hash.h +++ b/src/lwext4/ext4_hash.h @@ -50,8 +50,8 @@ * @param hash_major output value
* @return standard error code*/
int ext2_htree_hash(const char *name, int len,
- const uint32_t *hash_seed, int hash_version,
- uint32_t *hash_major, uint32_t *hash_minor);
+ const uint32_t *hash_seed, int hash_version,
+ uint32_t *hash_major, uint32_t *hash_minor);
#endif /* EXT4_HASH_H_ */
diff --git a/src/lwext4/ext4_ialloc.c b/src/lwext4/ext4_ialloc.c index 566f54c..0fcfb43 100644 --- a/src/lwext4/ext4_ialloc.c +++ b/src/lwext4/ext4_ialloc.c @@ -50,195 +50,195 @@ static uint32_t ext4_ialloc_inode2index_in_group(struct ext4_sblock *sb, uint32_t inode)
{
- uint32_t inodes_per_group = ext4_get32(sb, inodes_per_group);
- return (inode - 1) % inodes_per_group;
+ uint32_t inodes_per_group = ext4_get32(sb, inodes_per_group);
+ return (inode - 1) % inodes_per_group;
}
static uint32_t ext4_ialloc_index_in_group2inode(struct ext4_sblock *sb,
- uint32_t index, uint32_t bgid)
+ uint32_t index, uint32_t bgid)
{
- uint32_t inodes_per_group = ext4_get32(sb, inodes_per_group);
- return bgid * inodes_per_group + (index + 1);
+ uint32_t inodes_per_group = ext4_get32(sb, inodes_per_group);
+ return bgid * inodes_per_group + (index + 1);
}
static uint32_t ext4_ialloc_get_bgid_of_inode(struct ext4_sblock *sb,
- uint32_t inode)
+ uint32_t inode)
{
- uint32_t inodes_per_group = ext4_get32(sb, inodes_per_group);
- return (inode - 1) / inodes_per_group;
+ uint32_t inodes_per_group = ext4_get32(sb, inodes_per_group);
+ return (inode - 1) / inodes_per_group;
}
int ext4_ialloc_free_inode(struct ext4_fs *fs, uint32_t index, bool is_dir)
{
- struct ext4_sblock *sb = &fs->sb;
-
- /* Compute index of block group and load it */
- uint32_t block_group = ext4_ialloc_get_bgid_of_inode(sb, index);
-
- struct ext4_block_group_ref bg_ref;
- int rc = ext4_fs_get_block_group_ref(fs, block_group, &bg_ref);
- if (rc != EOK)
- return rc;
-
- /* Load i-node bitmap */
- uint32_t bitmap_block_addr = ext4_bg_get_inode_bitmap(
- bg_ref.block_group, sb);
-
- struct ext4_block bitmap_block;
- rc = ext4_block_get(fs->bdev, &bitmap_block, bitmap_block_addr);
- if (rc != EOK)
- return rc;
-
- /* Free i-node in the bitmap */
- uint32_t index_in_group = ext4_ialloc_inode2index_in_group(sb, index);
- ext4_bmap_bit_clr(bitmap_block.data, index_in_group);
- bitmap_block.dirty = true;
-
- /* Put back the block with bitmap */
- rc = ext4_block_set(fs->bdev, &bitmap_block);
- if (rc != EOK) {
- /* Error in saving bitmap */
- ext4_fs_put_block_group_ref(&bg_ref);
- return rc;
- }
-
- /* If released i-node is a directory, decrement used directories count */
- if (is_dir) {
- uint32_t bg_used_dirs = ext4_bg_get_used_dirs_count(
- bg_ref.block_group, sb);
- bg_used_dirs--;
- ext4_bg_set_used_dirs_count(bg_ref.block_group, sb,
- bg_used_dirs);
- }
-
- /* Update block group free inodes count */
- uint32_t free_inodes = ext4_bg_get_free_inodes_count(
- bg_ref.block_group, sb);
- free_inodes++;
- ext4_bg_set_free_inodes_count(bg_ref.block_group, sb,
- free_inodes);
-
- bg_ref.dirty = true;
-
- /* Put back the modified block group */
- rc = ext4_fs_put_block_group_ref(&bg_ref);
- if (rc != EOK)
- return rc;
-
- /* Update superblock free inodes count */
- ext4_set32(sb, free_inodes_count, ext4_get32(sb, free_inodes_count) + 1);
-
- return EOK;
+ struct ext4_sblock *sb = &fs->sb;
+
+ /* Compute index of block group and load it */
+ uint32_t block_group = ext4_ialloc_get_bgid_of_inode(sb, index);
+
+ struct ext4_block_group_ref bg_ref;
+ int rc = ext4_fs_get_block_group_ref(fs, block_group, &bg_ref);
+ if (rc != EOK)
+ return rc;
+
+ /* Load i-node bitmap */
+ uint32_t bitmap_block_addr = ext4_bg_get_inode_bitmap(
+ bg_ref.block_group, sb);
+
+ struct ext4_block bitmap_block;
+ rc = ext4_block_get(fs->bdev, &bitmap_block, bitmap_block_addr);
+ if (rc != EOK)
+ return rc;
+
+ /* Free i-node in the bitmap */
+ uint32_t index_in_group = ext4_ialloc_inode2index_in_group(sb, index);
+ ext4_bmap_bit_clr(bitmap_block.data, index_in_group);
+ bitmap_block.dirty = true;
+
+ /* Put back the block with bitmap */
+ rc = ext4_block_set(fs->bdev, &bitmap_block);
+ if (rc != EOK) {
+ /* Error in saving bitmap */
+ ext4_fs_put_block_group_ref(&bg_ref);
+ return rc;
+ }
+
+ /* If released i-node is a directory, decrement used directories count */
+ if (is_dir) {
+ uint32_t bg_used_dirs = ext4_bg_get_used_dirs_count(
+ bg_ref.block_group, sb);
+ bg_used_dirs--;
+ ext4_bg_set_used_dirs_count(bg_ref.block_group, sb,
+ bg_used_dirs);
+ }
+
+ /* Update block group free inodes count */
+ uint32_t free_inodes = ext4_bg_get_free_inodes_count(
+ bg_ref.block_group, sb);
+ free_inodes++;
+ ext4_bg_set_free_inodes_count(bg_ref.block_group, sb,
+ free_inodes);
+
+ bg_ref.dirty = true;
+
+ /* Put back the modified block group */
+ rc = ext4_fs_put_block_group_ref(&bg_ref);
+ if (rc != EOK)
+ return rc;
+
+ /* Update superblock free inodes count */
+ ext4_set32(sb, free_inodes_count, ext4_get32(sb, free_inodes_count) + 1);
+
+ return EOK;
}
int ext4_ialloc_alloc_inode(struct ext4_fs *fs, uint32_t *index, bool is_dir)
{
- struct ext4_sblock *sb = &fs->sb;
-
- uint32_t bgid = 0;
- uint32_t bg_count = ext4_block_group_cnt(sb);
- uint32_t sb_free_inodes = ext4_get32(sb, free_inodes_count);
- uint32_t avg_free_inodes = sb_free_inodes / bg_count;
-
- /* Try to find free i-node in all block groups */
- while (bgid < bg_count) {
- /* Load block group to check */
- struct ext4_block_group_ref bg_ref;
- int rc = ext4_fs_get_block_group_ref(fs, bgid, &bg_ref);
- if (rc != EOK)
- return rc;
-
- struct ext4_bgroup *bg = bg_ref.block_group;
-
- /* Read necessary values for algorithm */
- uint32_t free_blocks = ext4_bg_get_free_blocks_count(bg, sb);
- uint32_t free_inodes = ext4_bg_get_free_inodes_count(bg, sb);
- uint32_t used_dirs = ext4_bg_get_used_dirs_count(bg, sb);
-
- /* Check if this block group is good candidate for allocation */
- if ((free_inodes >= avg_free_inodes) && (free_blocks > 0)) {
- /* Load block with bitmap */
- uint32_t bitmap_block_addr = ext4_bg_get_inode_bitmap(
- bg_ref.block_group, sb);
-
- struct ext4_block bitmap_block;
- rc = ext4_block_get(fs->bdev, &bitmap_block, bitmap_block_addr);
- if (rc != EOK)
- return rc;
-
- /* Try to allocate i-node in the bitmap */
- uint32_t inodes_in_group = ext4_inodes_in_group_cnt(sb, bgid);
- uint32_t index_in_group;
-
- rc = ext4_bmap_bit_find_clr(bitmap_block.data, 0, inodes_in_group, &index_in_group);
- /* Block group has not any free i-node */
- if (rc == ENOSPC) {
- ext4_block_set(fs->bdev, &bitmap_block);
- ext4_fs_put_block_group_ref(&bg_ref);
- continue;
- }
-
- ext4_bmap_bit_set(bitmap_block.data, index_in_group);
-
- /* Free i-node found, save the bitmap */
- bitmap_block.dirty = true;
-
- ext4_block_set(fs->bdev, &bitmap_block);
- if (rc != EOK)
- return rc;
-
- /* Modify filesystem counters */
- free_inodes--;
- ext4_bg_set_free_inodes_count(bg, sb, free_inodes);
-
- /* Increment used directories counter */
- if (is_dir) {
- used_dirs++;
- ext4_bg_set_used_dirs_count(bg, sb, used_dirs);
- }
-
- /* Decrease unused inodes count */
- if (ext4_bg_has_flag(bg,
- EXT4_BLOCK_GROUP_ITABLE_ZEROED)) {
- uint32_t unused =
- ext4_bg_get_itable_unused(bg, sb);
-
- uint32_t inodes_in_group =
- ext4_inodes_in_group_cnt(sb, bgid);
-
- uint32_t free = inodes_in_group - unused;
-
- if (index_in_group >= free) {
- unused = inodes_in_group - (index_in_group + 1);
- ext4_bg_set_itable_unused(bg, sb, unused);
- }
- }
-
- /* Save modified block group */
- bg_ref.dirty = true;
-
- rc = ext4_fs_put_block_group_ref(&bg_ref);
- if (rc != EOK)
- return rc;
-
- /* Update superblock */
- sb_free_inodes--;
- ext4_set32(sb, free_inodes_count, sb_free_inodes);
-
-
- /* Compute the absolute i-nodex number */
- *index = ext4_ialloc_index_in_group2inode(sb, index_in_group, bgid);
-
- return EOK;
- }
-
- /* Block group not modified, put it and jump to the next block group */
- ext4_fs_put_block_group_ref(&bg_ref);
- ++bgid;
- }
-
- return ENOSPC;
+ struct ext4_sblock *sb = &fs->sb;
+
+ uint32_t bgid = 0;
+ uint32_t bg_count = ext4_block_group_cnt(sb);
+ uint32_t sb_free_inodes = ext4_get32(sb, free_inodes_count);
+ uint32_t avg_free_inodes = sb_free_inodes / bg_count;
+
+ /* Try to find free i-node in all block groups */
+ while (bgid < bg_count) {
+ /* Load block group to check */
+ struct ext4_block_group_ref bg_ref;
+ int rc = ext4_fs_get_block_group_ref(fs, bgid, &bg_ref);
+ if (rc != EOK)
+ return rc;
+
+ struct ext4_bgroup *bg = bg_ref.block_group;
+
+ /* Read necessary values for algorithm */
+ uint32_t free_blocks = ext4_bg_get_free_blocks_count(bg, sb);
+ uint32_t free_inodes = ext4_bg_get_free_inodes_count(bg, sb);
+ uint32_t used_dirs = ext4_bg_get_used_dirs_count(bg, sb);
+
+ /* Check if this block group is good candidate for allocation */
+ if ((free_inodes >= avg_free_inodes) && (free_blocks > 0)) {
+ /* Load block with bitmap */
+ uint32_t bitmap_block_addr = ext4_bg_get_inode_bitmap(
+ bg_ref.block_group, sb);
+
+ struct ext4_block bitmap_block;
+ rc = ext4_block_get(fs->bdev, &bitmap_block, bitmap_block_addr);
+ if (rc != EOK)
+ return rc;
+
+ /* Try to allocate i-node in the bitmap */
+ uint32_t inodes_in_group = ext4_inodes_in_group_cnt(sb, bgid);
+ uint32_t index_in_group;
+
+ rc = ext4_bmap_bit_find_clr(bitmap_block.data, 0, inodes_in_group, &index_in_group);
+ /* Block group has not any free i-node */
+ if (rc == ENOSPC) {
+ ext4_block_set(fs->bdev, &bitmap_block);
+ ext4_fs_put_block_group_ref(&bg_ref);
+ continue;
+ }
+
+ ext4_bmap_bit_set(bitmap_block.data, index_in_group);
+
+ /* Free i-node found, save the bitmap */
+ bitmap_block.dirty = true;
+
+ ext4_block_set(fs->bdev, &bitmap_block);
+ if (rc != EOK)
+ return rc;
+
+ /* Modify filesystem counters */
+ free_inodes--;
+ ext4_bg_set_free_inodes_count(bg, sb, free_inodes);
+
+ /* Increment used directories counter */
+ if (is_dir) {
+ used_dirs++;
+ ext4_bg_set_used_dirs_count(bg, sb, used_dirs);
+ }
+
+ /* Decrease unused inodes count */
+ if (ext4_bg_has_flag(bg,
+ EXT4_BLOCK_GROUP_ITABLE_ZEROED)) {
+ uint32_t unused =
+ ext4_bg_get_itable_unused(bg, sb);
+
+ uint32_t inodes_in_group =
+ ext4_inodes_in_group_cnt(sb, bgid);
+
+ uint32_t free = inodes_in_group - unused;
+
+ if (index_in_group >= free) {
+ unused = inodes_in_group - (index_in_group + 1);
+ ext4_bg_set_itable_unused(bg, sb, unused);
+ }
+ }
+
+ /* Save modified block group */
+ bg_ref.dirty = true;
+
+ rc = ext4_fs_put_block_group_ref(&bg_ref);
+ if (rc != EOK)
+ return rc;
+
+ /* Update superblock */
+ sb_free_inodes--;
+ ext4_set32(sb, free_inodes_count, sb_free_inodes);
+
+
+ /* Compute the absolute i-nodex number */
+ *index = ext4_ialloc_index_in_group2inode(sb, index_in_group, bgid);
+
+ return EOK;
+ }
+
+ /* Block group not modified, put it and jump to the next block group */
+ ext4_fs_put_block_group_ref(&bg_ref);
+ ++bgid;
+ }
+
+ return ENOSPC;
}
/**
diff --git a/src/lwext4/ext4_inode.c b/src/lwext4/ext4_inode.c index 74c0fae..a52eed7 100644 --- a/src/lwext4/ext4_inode.c +++ b/src/lwext4/ext4_inode.c @@ -46,291 +46,291 @@ static uint32_t ext4_inode_block_bits_count(uint32_t block_size)
{
- uint32_t bits = 8;
- uint32_t size = block_size;
+ uint32_t bits = 8;
+ uint32_t size = block_size;
- do {
- bits++;
- size = size >> 1;
- } while (size > 256);
+ do {
+ bits++;
+ size = size >> 1;
+ } while (size > 256);
- return bits;
+ return bits;
}
uint32_t ext4_inode_get_mode(struct ext4_sblock *sb, struct ext4_inode *inode)
{
- uint32_t v = to_le16(inode->mode);
+ uint32_t v = to_le16(inode->mode);
- if(ext4_get32(sb, creator_os) == EXT4_SUPERBLOCK_OS_HURD){
- v |= ((uint32_t) to_le16(inode->osd2.hurd2.mode_high)) << 16;
- }
+ if(ext4_get32(sb, creator_os) == EXT4_SUPERBLOCK_OS_HURD){
+ v |= ((uint32_t) to_le16(inode->osd2.hurd2.mode_high)) << 16;
+ }
- return v;
+ return v;
}
void ext4_inode_set_mode(struct ext4_sblock *sb, struct ext4_inode *inode, uint32_t mode)
{
- inode->mode = to_le16((mode << 16) >> 16);
+ inode->mode = to_le16((mode << 16) >> 16);
- if(ext4_get32(sb, creator_os) == EXT4_SUPERBLOCK_OS_HURD)
- inode->osd2.hurd2.mode_high = to_le16(mode >> 16);
+ if(ext4_get32(sb, creator_os) == EXT4_SUPERBLOCK_OS_HURD)
+ inode->osd2.hurd2.mode_high = to_le16(mode >> 16);
}
uint32_t ext4_inode_get_uid(struct ext4_inode *inode)
{
- return to_le32(inode->uid);
+ return to_le32(inode->uid);
}
void ext4_inode_set_uid(struct ext4_inode *inode, uint32_t uid)
{
- inode->uid = to_le32(uid);
+ inode->uid = to_le32(uid);
}
uint64_t ext4_inode_get_size(struct ext4_sblock *sb, struct ext4_inode *inode)
{
- uint64_t v = to_le32(inode->size_lo);
+ uint64_t v = to_le32(inode->size_lo);
- if ((ext4_get32(sb, rev_level) > 0) && (ext4_inode_is_type(sb, inode, EXT4_INODE_MODE_FILE)))
- v |= ((uint64_t)to_le32(inode->size_hi)) << 32;
+ if ((ext4_get32(sb, rev_level) > 0) && (ext4_inode_is_type(sb, inode, EXT4_INODE_MODE_FILE)))
+ v |= ((uint64_t)to_le32(inode->size_hi)) << 32;
- return v;
+ return v;
}
void ext4_inode_set_size(struct ext4_inode *inode, uint64_t size)
{
- inode->size_lo = to_le32((size << 32) >> 32);
- inode->size_hi = to_le32(size >> 32);
+ inode->size_lo = to_le32((size << 32) >> 32);
+ inode->size_hi = to_le32(size >> 32);
}
uint32_t ext4_inode_get_access_time(struct ext4_inode *inode)
{
- return to_le32(inode->access_time);
+ return to_le32(inode->access_time);
}
void ext4_inode_set_access_time(struct ext4_inode *inode, uint32_t time)
{
- inode->access_time = to_le32(time);
+ inode->access_time = to_le32(time);
}
uint32_t ext4_inode_get_change_inode_time(struct ext4_inode *inode)
{
- return to_le32(inode->change_inode_time);
+ return to_le32(inode->change_inode_time);
}
void ext4_inode_set_change_inode_time(struct ext4_inode *inode, uint32_t time)
{
- inode->change_inode_time = to_le32(time);
+ inode->change_inode_time = to_le32(time);
}
uint32_t ext4_inode_get_modification_time(struct ext4_inode *inode)
{
- return to_le32(inode->modification_time);
+ return to_le32(inode->modification_time);
}
void ext4_inode_set_modification_time(struct ext4_inode *inode, uint32_t time)
{
- inode->modification_time = to_le32(time);
+ inode->modification_time = to_le32(time);
}
uint32_t ext4_inode_get_deletion_time(struct ext4_inode *inode)
{
- return to_le32(inode->deletion_time);
+ return to_le32(inode->deletion_time);
}
void ext4_inode_set_deletion_time(struct ext4_inode *inode, uint32_t time)
{
- inode->deletion_time = to_le32(time);
+ inode->deletion_time = to_le32(time);
}
uint32_t ext4_inode_get_gid(struct ext4_inode *inode)
{
- return to_le32(inode->gid);
+ return to_le32(inode->gid);
}
void ext4_inode_set_gid(struct ext4_inode *inode, uint32_t gid)
{
- inode->gid = to_le32(gid);
+ inode->gid = to_le32(gid);
}
uint16_t ext4_inode_get_links_count(struct ext4_inode *inode)
{
- return to_le16(inode->links_count);
+ return to_le16(inode->links_count);
}
void ext4_inode_set_links_count(struct ext4_inode *inode, uint16_t cnt)
{
- inode->links_count = to_le16(cnt);
+ inode->links_count = to_le16(cnt);
}
uint64_t ext4_inode_get_blocks_count(struct ext4_sblock *sb, struct ext4_inode *inode)
{
- uint64_t count = to_le32(inode->blocks_count_lo);
+ uint64_t count = to_le32(inode->blocks_count_lo);
- if (ext4_sb_check_read_only(sb,
- EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
+ if (ext4_sb_check_read_only(sb,
+ EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
- /* 48-bit field */
- count = ((uint64_t) to_le16(inode->osd2.linux2.blocks_high)) << 32;
+ /* 48-bit field */
+ count = ((uint64_t) to_le16(inode->osd2.linux2.blocks_high)) << 32;
- if (ext4_inode_has_flag(inode, EXT4_INODE_FLAG_HUGE_FILE)) {
+ if (ext4_inode_has_flag(inode, EXT4_INODE_FLAG_HUGE_FILE)) {
- uint32_t block_bits = ext4_inode_block_bits_count(ext4_sb_get_block_size(sb));
- return count << (block_bits - 9);
- } else
- return count;
- }
+ uint32_t block_bits = ext4_inode_block_bits_count(ext4_sb_get_block_size(sb));
+ return count << (block_bits - 9);
+ } else
+ return count;
+ }
- return count;
+ return count;
}
int ext4_inode_set_blocks_count(struct ext4_sblock *sb, struct ext4_inode *inode, uint64_t count)
{
- /* 32-bit maximum */
- uint64_t max = 0;
- max = ~max >> 32;
+ /* 32-bit maximum */
+ uint64_t max = 0;
+ max = ~max >> 32;
- if (count <= max) {
- inode->blocks_count_lo = to_le32(count);
- inode->osd2.linux2.blocks_high = 0;
- ext4_inode_clear_flag(inode, EXT4_INODE_FLAG_HUGE_FILE);
+ if (count <= max) {
+ inode->blocks_count_lo = to_le32(count);
+ inode->osd2.linux2.blocks_high = 0;
+ ext4_inode_clear_flag(inode, EXT4_INODE_FLAG_HUGE_FILE);
- return EOK;
- }
+ return EOK;
+ }
- /* Check if there can be used huge files (many blocks) */
- if (!ext4_sb_check_read_only(sb,
- EXT4_FEATURE_RO_COMPAT_HUGE_FILE))
- return EINVAL;
+ /* Check if there can be used huge files (many blocks) */
+ if (!ext4_sb_check_read_only(sb,
+ EXT4_FEATURE_RO_COMPAT_HUGE_FILE))
+ return EINVAL;
- /* 48-bit maximum */
- max = 0;
- max = ~max >> 16;
+ /* 48-bit maximum */
+ max = 0;
+ max = ~max >> 16;
- if (count <= max) {
- inode->blocks_count_lo = to_le32(count);
- inode->osd2.linux2.blocks_high = to_le16(count >> 32);
- ext4_inode_clear_flag(inode, EXT4_INODE_FLAG_HUGE_FILE);
- } else {
- uint32_t block_bits = ext4_inode_block_bits_count(ext4_sb_get_block_size(sb));
+ if (count <= max) {
+ inode->blocks_count_lo = to_le32(count);
+ inode->osd2.linux2.blocks_high = to_le16(count >> 32);
+ ext4_inode_clear_flag(inode, EXT4_INODE_FLAG_HUGE_FILE);
+ } else {
+ uint32_t block_bits = ext4_inode_block_bits_count(ext4_sb_get_block_size(sb));
- ext4_inode_set_flag(inode, EXT4_INODE_FLAG_HUGE_FILE);
- count = count >> (block_bits - 9);
- inode->blocks_count_lo = to_le32(count);
- inode->osd2.linux2.blocks_high = to_le16(count >> 32);
- }
+ ext4_inode_set_flag(inode, EXT4_INODE_FLAG_HUGE_FILE);
+ count = count >> (block_bits - 9);
+ inode->blocks_count_lo = to_le32(count);
+ inode->osd2.linux2.blocks_high = to_le16(count >> 32);
+ }
- return EOK;
+ return EOK;
}
uint32_t ext4_inode_get_flags(struct ext4_inode *inode)
{
- return to_le32(inode->flags);
+ return to_le32(inode->flags);
}
void ext4_inode_set_flags(struct ext4_inode *inode, uint32_t flags)
{
- inode->flags = to_le32(flags);
+ inode->flags = to_le32(flags);
}
uint32_t ext4_inode_get_generation(struct ext4_inode *inode)
{
- return to_le32(inode->generation);
+ return to_le32(inode->generation);
}
void ext4_inode_set_generation(struct ext4_inode *inode, uint32_t gen)
{
- inode->generation = to_le32(gen);
+ inode->generation = to_le32(gen);
}
uint64_t ext4_inode_get_file_acl(struct ext4_inode *inode, struct ext4_sblock *sb)
{
- /*TODO: Verify it*/
- uint64_t v = to_le32(inode->file_acl_lo);
+ /*TODO: Verify it*/
+ uint64_t v = to_le32(inode->file_acl_lo);
- if (ext4_get32(sb, creator_os) == EXT4_SUPERBLOCK_OS_LINUX)
- v |= ((uint32_t) to_le16(inode->osd2.linux2.file_acl_high)) << 16;
+ if (ext4_get32(sb, creator_os) == EXT4_SUPERBLOCK_OS_LINUX)
+ v |= ((uint32_t) to_le16(inode->osd2.linux2.file_acl_high)) << 16;
- return v;
+ return v;
}
void ext4_inode_set_file_acl(struct ext4_inode *inode, struct ext4_sblock *sb, uint64_t acl)
{
- /*TODO: Verify it*/
- inode->file_acl_lo = to_le32((acl << 32) >> 32);
+ /*TODO: Verify it*/
+ inode->file_acl_lo = to_le32((acl << 32) >> 32);
- if (ext4_get32(sb, creator_os) == EXT4_SUPERBLOCK_OS_LINUX)
- inode->osd2.linux2.file_acl_high = to_le16(acl >> 32);
+ if (ext4_get32(sb, creator_os) == EXT4_SUPERBLOCK_OS_LINUX)
+ inode->osd2.linux2.file_acl_high = to_le16(acl >> 32);
}
uint32_t ext4_inode_get_direct_block(struct ext4_inode *inode, uint32_t idx)
{
- return to_le32(inode->blocks[idx]);
+ return to_le32(inode->blocks[idx]);
}
void ext4_inode_set_direct_block(struct ext4_inode *inode, uint32_t idx, uint32_t block)
{
- inode->blocks[idx] = to_le32(block);
+ inode->blocks[idx] = to_le32(block);
}
uint32_t ext4_inode_get_indirect_block(struct ext4_inode *inode, uint32_t idx)
{
- return to_le32(inode->blocks[idx + EXT4_INODE_INDIRECT_BLOCK]);
+ return to_le32(inode->blocks[idx + EXT4_INODE_INDIRECT_BLOCK]);
}
void ext4_inode_set_indirect_block(struct ext4_inode *inode, uint32_t idx, uint32_t block)
{
- inode->blocks[idx + EXT4_INODE_INDIRECT_BLOCK] = to_le32(block);
+ inode->blocks[idx + EXT4_INODE_INDIRECT_BLOCK] = to_le32(block);
}
bool ext4_inode_is_type(struct ext4_sblock *sb, struct ext4_inode *inode, uint32_t type)
{
- return (ext4_inode_get_mode(sb, inode) & EXT4_INODE_MODE_TYPE_MASK) == type;
+ return (ext4_inode_get_mode(sb, inode) & EXT4_INODE_MODE_TYPE_MASK) == type;
}
bool ext4_inode_has_flag(struct ext4_inode *inode, uint32_t f)
{
- return ext4_inode_get_flags(inode) & f;
+ return ext4_inode_get_flags(inode) & f;
}
void ext4_inode_clear_flag(struct ext4_inode *inode, uint32_t f)
{
- uint32_t flags = ext4_inode_get_flags(inode);
- flags = flags & (~f);
- ext4_inode_set_flags(inode, flags);
+ uint32_t flags = ext4_inode_get_flags(inode);
+ flags = flags & (~f);
+ ext4_inode_set_flags(inode, flags);
}
void ext4_inode_set_flag(struct ext4_inode *inode, uint32_t f)
{
- uint32_t flags = ext4_inode_get_flags(inode);
- flags = flags | f;
- ext4_inode_set_flags(inode, flags);
+ uint32_t flags = ext4_inode_get_flags(inode);
+ flags = flags | f;
+ ext4_inode_set_flags(inode, flags);
}
bool ext4_inode_can_truncate(struct ext4_sblock *sb, struct ext4_inode *inode)
{
- if ((ext4_inode_has_flag(inode, EXT4_INODE_FLAG_APPEND)) ||
- (ext4_inode_has_flag(inode, EXT4_INODE_FLAG_IMMUTABLE)))
- return false;
+ if ((ext4_inode_has_flag(inode, EXT4_INODE_FLAG_APPEND)) ||
+ (ext4_inode_has_flag(inode, EXT4_INODE_FLAG_IMMUTABLE)))
+ return false;
- if ((ext4_inode_is_type(sb, inode, EXT4_INODE_MODE_FILE)) ||
- (ext4_inode_is_type(sb, inode, EXT4_INODE_MODE_DIRECTORY)))
- return true;
+ if ((ext4_inode_is_type(sb, inode, EXT4_INODE_MODE_FILE)) ||
+ (ext4_inode_is_type(sb, inode, EXT4_INODE_MODE_DIRECTORY)))
+ return true;
- return false;
+ return false;
}
struct ext4_extent_header * ext4_inode_get_extent_header(struct ext4_inode *inode)
{
- return (struct ext4_extent_header *) inode->blocks;
+ return (struct ext4_extent_header *) inode->blocks;
}
/**
diff --git a/src/lwext4/ext4_super.c b/src/lwext4/ext4_super.c index 9735241..c40a539 100644 --- a/src/lwext4/ext4_super.c +++ b/src/lwext4/ext4_super.c @@ -45,87 +45,87 @@ uint32_t ext4_block_group_cnt(struct ext4_sblock *s) { - uint64_t blocks_count = ext4_sb_get_blocks_cnt(s); - uint32_t blocks_per_group = ext4_get32(s, blocks_per_group); + uint64_t blocks_count = ext4_sb_get_blocks_cnt(s); + uint32_t blocks_per_group = ext4_get32(s, blocks_per_group); - uint32_t block_groups_count = blocks_count / blocks_per_group; + uint32_t block_groups_count = blocks_count / blocks_per_group; - if (blocks_count % blocks_per_group) - block_groups_count++; + if (blocks_count % blocks_per_group) + block_groups_count++; - return block_groups_count; + return block_groups_count; } uint32_t ext4_blocks_in_group_cnt(struct ext4_sblock *s, uint32_t bgid) { - uint32_t block_group_count = ext4_block_group_cnt(s); - uint32_t blocks_per_group = ext4_get32(s, blocks_per_group); - uint64_t total_blocks = ext4_sb_get_blocks_cnt(s); + uint32_t block_group_count = ext4_block_group_cnt(s); + uint32_t blocks_per_group = ext4_get32(s, blocks_per_group); + uint64_t total_blocks = ext4_sb_get_blocks_cnt(s); - if (bgid < block_group_count - 1) - return blocks_per_group; + if (bgid < block_group_count - 1) + return blocks_per_group; - return (total_blocks - ((block_group_count - 1) * blocks_per_group)); + return (total_blocks - ((block_group_count - 1) * blocks_per_group)); } uint32_t ext4_inodes_in_group_cnt(struct ext4_sblock *s, uint32_t bgid) { - uint32_t block_group_count = ext4_block_group_cnt(s); - uint32_t inodes_per_group = ext4_get32(s, inodes_per_group); - uint32_t total_inodes = ext4_get32(s, inodes_count); + uint32_t block_group_count = ext4_block_group_cnt(s); + uint32_t inodes_per_group = ext4_get32(s, inodes_per_group); + uint32_t total_inodes = ext4_get32(s, inodes_count); - if (bgid < block_group_count - 1) - return inodes_per_group; + if (bgid < block_group_count - 1) + return inodes_per_group; - return (total_inodes - ((block_group_count - 1) * inodes_per_group)); + return (total_inodes - ((block_group_count - 1) * inodes_per_group)); } int ext4_sb_write(struct ext4_blockdev *bdev, struct ext4_sblock *s) { - return ext4_block_writebytes(bdev, EXT4_SUPERBLOCK_OFFSET, - s, EXT4_SUPERBLOCK_SIZE); + return ext4_block_writebytes(bdev, EXT4_SUPERBLOCK_OFFSET, + s, EXT4_SUPERBLOCK_SIZE); } int ext4_sb_read(struct ext4_blockdev *bdev, struct ext4_sblock *s) { - return ext4_block_readbytes(bdev, EXT4_SUPERBLOCK_OFFSET, - s, EXT4_SUPERBLOCK_SIZE); + return ext4_block_readbytes(bdev, EXT4_SUPERBLOCK_OFFSET, + s, EXT4_SUPERBLOCK_SIZE); } bool ext4_sb_check(struct ext4_sblock *s) { - if (ext4_get16(s, magic) != EXT4_SUPERBLOCK_MAGIC) - return false; + if (ext4_get16(s, magic) != EXT4_SUPERBLOCK_MAGIC) + return false; - if (ext4_get32(s, inodes_count) == 0) - return false; + if (ext4_get32(s, inodes_count) == 0) + return false; - if (ext4_sb_get_blocks_cnt(s) == 0) - return false; + if (ext4_sb_get_blocks_cnt(s) == 0) + return false; - if (ext4_get32(s, blocks_per_group) == 0) - return false; + if (ext4_get32(s, blocks_per_group) == 0) + return false; - if (ext4_get32(s, inodes_per_group) == 0) - return false; + if (ext4_get32(s, inodes_per_group) == 0) + return false; - if (ext4_get16(s, inode_size) < 128) - return false; + if (ext4_get16(s, inode_size) < 128) + return false; - if (ext4_get32(s, first_inode) < 11) - return false; + if (ext4_get32(s, first_inode) < 11) + return false; - if (ext4_sb_get_desc_size(s) < - EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE) - return false; + if (ext4_sb_get_desc_size(s) < + EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE) + return false; - if (ext4_sb_get_desc_size(s) > - EXT4_MAX_BLOCK_GROUP_DESCRIPTOR_SIZE) - return false; + if (ext4_sb_get_desc_size(s) > + EXT4_MAX_BLOCK_GROUP_DESCRIPTOR_SIZE) + return false; - return true; + return true; } /** diff --git a/src/lwext4/ext4_super.h b/src/lwext4/ext4_super.h index 7aba2a1..1ee4afe 100644 --- a/src/lwext4/ext4_super.h +++ b/src/lwext4/ext4_super.h @@ -54,7 +54,7 @@ * @return count of blocks*/ static inline uint64_t ext4_sb_get_blocks_cnt(struct ext4_sblock *s) { - return ((uint64_t) to_le32(s->blocks_count_hi) << 32) | to_le32(s->blocks_count_lo); + return ((uint64_t) to_le32(s->blocks_count_hi) << 32) | to_le32(s->blocks_count_lo); } /**@brief Free blocks count get stored in superblock. @@ -62,7 +62,7 @@ static inline uint64_t ext4_sb_get_blocks_cnt(struct ext4_sblock *s) * @return free blocks*/ static inline uint64_t ext4_sb_get_free_blocks_cnt(struct ext4_sblock *s) { - return ((uint64_t) to_le32(s->free_blocks_count_hi) << 32) | to_le32(s->free_blocks_count_lo); + return ((uint64_t) to_le32(s->free_blocks_count_hi) << 32) | to_le32(s->free_blocks_count_lo); } /**@brief Free blocks count set. @@ -70,8 +70,8 @@ static inline uint64_t ext4_sb_get_free_blocks_cnt(struct ext4_sblock *s) * @param cnt new value of free blocks*/ static inline void ext4_sb_set_free_blocks_cnt(struct ext4_sblock *s, uint64_t cnt) { - s->free_blocks_count_lo = to_le32((cnt << 32) >> 32); - s->free_blocks_count_hi = to_le32(cnt >> 32); + s->free_blocks_count_lo = to_le32((cnt << 32) >> 32); + s->free_blocks_count_hi = to_le32(cnt >> 32); } /**@brief Block size get from superblock. @@ -79,7 +79,7 @@ static inline void ext4_sb_set_free_blocks_cnt(struct ext4_sblock *s, uint64_t * @return block size in bytes*/ static inline uint32_t ext4_sb_get_block_size(struct ext4_sblock *s) { - return 1024 << to_le32(s->log_block_size); + return 1024 << to_le32(s->log_block_size); } /**@brief Block group descriptor size. @@ -87,10 +87,10 @@ static inline uint32_t ext4_sb_get_block_size(struct ext4_sblock *s) * @return block group descriptor size in bytes*/ static inline uint16_t ext4_sb_get_desc_size(struct ext4_sblock *s) { - uint16_t size = to_le16(s->desc_size); + uint16_t size = to_le16(s->desc_size); - return size < EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE ? - EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE : size; + return size < EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE ? + EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE : size; } /*************************Flags and features**********************************/ @@ -101,7 +101,7 @@ static inline uint16_t ext4_sb_get_desc_size(struct ext4_sblock *s) * @return true if flag is supported*/ static inline bool ext4_sb_check_flag(struct ext4_sblock *s, uint32_t v) { - return to_le32(s->flags) & v; + return to_le32(s->flags) & v; } /**@brief Support check of feature compatible. @@ -110,7 +110,7 @@ static inline bool ext4_sb_check_flag(struct ext4_sblock *s, uint32_t v) * @return true if feature is supported*/ static inline bool ext4_sb_check_feature_compatible(struct ext4_sblock *s, uint32_t v) { - return to_le32(s->features_compatible) & v; + return to_le32(s->features_compatible) & v; } @@ -120,7 +120,7 @@ static inline bool ext4_sb_check_feature_compatible(struct ext4_sblock *s, uint3 * @return true if feature is supported*/ static inline bool ext4_sb_check_feature_incompatible(struct ext4_sblock *s, uint32_t v) { - return to_le32(s->features_incompatible) & v; + return to_le32(s->features_incompatible) & v; } @@ -130,7 +130,7 @@ static inline bool ext4_sb_check_feature_incompatible(struct ext4_sblock *s, uin * @return true if flag is supported*/ static inline bool ext4_sb_check_read_only(struct ext4_sblock *s, uint32_t v) { - return to_le32(s->features_read_only) & v; + return to_le32(s->features_read_only) & v; } /**************************More complex functions*****************************/ diff --git a/src/lwext4/ext4_types.h b/src/lwext4/ext4_types.h index 5e06817..ef1a42e 100644 --- a/src/lwext4/ext4_types.h +++ b/src/lwext4/ext4_types.h @@ -52,100 +52,100 @@ * Structure of the super block */ struct ext4_sblock { - uint32_t inodes_count; /* I-nodes count */ - uint32_t blocks_count_lo; /* Blocks count */ - uint32_t reserved_blocks_count_lo; /* Reserved blocks count */ - uint32_t free_blocks_count_lo; /* Free blocks count */ - uint32_t free_inodes_count; /* Free inodes count */ - uint32_t first_data_block; /* First Data Block */ - uint32_t log_block_size; /* Block size */ - uint32_t log_frag_size; /* Obsoleted fragment size */ - uint32_t blocks_per_group; /* Number of blocks per group */ - uint32_t frags_per_group; /* Obsoleted fragments per group */ - uint32_t inodes_per_group; /* Number of inodes per group */ - uint32_t mount_time; /* Mount time */ - uint32_t write_time; /* Write time */ - uint16_t mount_count; /* Mount count */ - uint16_t max_mount_count; /* Maximal mount count */ - uint16_t magic; /* Magic signature */ - uint16_t state; /* File system state */ - uint16_t errors; /* Behaviour when detecting errors */ - uint16_t minor_rev_level; /* Minor revision level */ - uint32_t last_check_time; /* Time of last check */ - uint32_t check_interval; /* Maximum time between checks */ - uint32_t creator_os; /* Creator OS */ - uint32_t rev_level; /* Revision level */ - uint16_t def_resuid; /* Default uid for reserved blocks */ - uint16_t def_resgid; /* Default gid for reserved blocks */ - - /* Fields for EXT4_DYNAMIC_REV superblocks only. */ - uint32_t first_inode; /* First non-reserved inode */ - uint16_t inode_size; /* Size of inode structure */ - uint16_t block_group_index; /* Block group index of this superblock */ - uint32_t features_compatible; /* Compatible feature set */ - uint32_t features_incompatible; /* Incompatible feature set */ - uint32_t features_read_only; /* Readonly-compatible feature set */ - uint8_t uuid[16]; /* 128-bit uuid for volume */ - char volume_name[16]; /* Volume name */ - char last_mounted[64]; /* Directory where last mounted */ - uint32_t algorithm_usage_bitmap; /* For compression */ - - /* - * Performance hints. Directory preallocation should only - * happen if the EXT4_FEATURE_COMPAT_DIR_PREALLOC flag is on. - */ - uint8_t s_prealloc_blocks; /* Number of blocks to try to preallocate */ - uint8_t s_prealloc_dir_blocks; /* Number to preallocate for dirs */ - uint16_t s_reserved_gdt_blocks; /* Per group desc for online growth */ - - /* - * Journaling support valid if EXT4_FEATURE_COMPAT_HAS_JOURNAL set. - */ - uint8_t journal_uuid[16]; /* UUID of journal superblock */ - uint32_t journal_inode_number; /* Inode number of journal file */ - uint32_t journal_dev; /* Device number of journal file */ - uint32_t last_orphan; /* Head of list of inodes to delete */ - uint32_t hash_seed[4]; /* HTREE hash seed */ - uint8_t default_hash_version; /* Default hash version to use */ - uint8_t journal_backup_type; - uint16_t desc_size; /* Size of group descriptor */ - uint32_t default_mount_opts; /* Default mount options */ - uint32_t first_meta_bg; /* First metablock block group */ - uint32_t mkfs_time; /* When the filesystem was created */ - uint32_t journal_blocks[17]; /* Backup of the journal inode */ - - /* 64bit support valid if EXT4_FEATURE_COMPAT_64BIT */ - uint32_t blocks_count_hi; /* Blocks count */ - uint32_t reserved_blocks_count_hi; /* Reserved blocks count */ - uint32_t free_blocks_count_hi; /* Free blocks count */ - uint16_t min_extra_isize; /* All inodes have at least # bytes */ - uint16_t want_extra_isize; /* New inodes should reserve # bytes */ - uint32_t flags; /* Miscellaneous flags */ - uint16_t raid_stride; /* RAID stride */ - uint16_t mmp_interval; /* # seconds to wait in MMP checking */ - uint64_t mmp_block; /* Block for multi-mount protection */ - uint32_t raid_stripe_width; /* Blocks on all data disks (N * stride) */ - uint8_t log_groups_per_flex; /* FLEX_BG group size */ - uint8_t reserved_char_pad; - uint16_t reserved_pad; - uint64_t kbytes_written; /* Number of lifetime kilobytes written */ - uint32_t snapshot_inum; /* I-node number of active snapshot */ - uint32_t snapshot_id; /* Sequential ID of active snapshot */ - uint64_t snapshot_r_blocks_count; /* Reserved blocks for active snapshot's future use */ - uint32_t snapshot_list; /* I-node number of the head of the on-disk snapshot list */ - uint32_t error_count; /* Number of file system errors */ - uint32_t first_error_time; /* First time an error happened */ - uint32_t first_error_ino; /* I-node involved in first error */ - uint64_t first_error_block; /* Block involved of first error */ - uint8_t first_error_func[32]; /* Function where the error happened */ - uint32_t first_error_line; /* Line number where error happened */ - uint32_t last_error_time; /* Most recent time of an error */ - uint32_t last_error_ino; /* I-node involved in last error */ - uint32_t last_error_line; /* Line number where error happened */ - uint64_t last_error_block; /* Block involved of last error */ - uint8_t last_error_func[32]; /* Function where the error happened */ - uint8_t mount_opts[64]; - uint32_t padding[112]; /* Padding to the end of the block */ + uint32_t inodes_count; /* I-nodes count */ + uint32_t blocks_count_lo; /* Blocks count */ + uint32_t reserved_blocks_count_lo; /* Reserved blocks count */ + uint32_t free_blocks_count_lo; /* Free blocks count */ + uint32_t free_inodes_count; /* Free inodes count */ + uint32_t first_data_block; /* First Data Block */ + uint32_t log_block_size; /* Block size */ + uint32_t log_frag_size; /* Obsoleted fragment size */ + uint32_t blocks_per_group; /* Number of blocks per group */ + uint32_t frags_per_group; /* Obsoleted fragments per group */ + uint32_t inodes_per_group; /* Number of inodes per group */ + uint32_t mount_time; /* Mount time */ + uint32_t write_time; /* Write time */ + uint16_t mount_count; /* Mount count */ + uint16_t max_mount_count; /* Maximal mount count */ + uint16_t magic; /* Magic signature */ + uint16_t state; /* File system state */ + uint16_t errors; /* Behaviour when detecting errors */ + uint16_t minor_rev_level; /* Minor revision level */ + uint32_t last_check_time; /* Time of last check */ + uint32_t check_interval; /* Maximum time between checks */ + uint32_t creator_os; /* Creator OS */ + uint32_t rev_level; /* Revision level */ + uint16_t def_resuid; /* Default uid for reserved blocks */ + uint16_t def_resgid; /* Default gid for reserved blocks */ + + /* Fields for EXT4_DYNAMIC_REV superblocks only. */ + uint32_t first_inode; /* First non-reserved inode */ + uint16_t inode_size; /* Size of inode structure */ + uint16_t block_group_index; /* Block group index of this superblock */ + uint32_t features_compatible; /* Compatible feature set */ + uint32_t features_incompatible; /* Incompatible feature set */ + uint32_t features_read_only; /* Readonly-compatible feature set */ + uint8_t uuid[16]; /* 128-bit uuid for volume */ + char volume_name[16]; /* Volume name */ + char last_mounted[64]; /* Directory where last mounted */ + uint32_t algorithm_usage_bitmap; /* For compression */ + + /* + * Performance hints. Directory preallocation should only + * happen if the EXT4_FEATURE_COMPAT_DIR_PREALLOC flag is on. + */ + uint8_t s_prealloc_blocks; /* Number of blocks to try to preallocate */ + uint8_t s_prealloc_dir_blocks; /* Number to preallocate for dirs */ + uint16_t s_reserved_gdt_blocks; /* Per group desc for online growth */ + + /* + * Journaling support valid if EXT4_FEATURE_COMPAT_HAS_JOURNAL set. + */ + uint8_t journal_uuid[16]; /* UUID of journal superblock */ + uint32_t journal_inode_number; /* Inode number of journal file */ + uint32_t journal_dev; /* Device number of journal file */ + uint32_t last_orphan; /* Head of list of inodes to delete */ + uint32_t hash_seed[4]; /* HTREE hash seed */ + uint8_t default_hash_version; /* Default hash version to use */ + uint8_t journal_backup_type; + uint16_t desc_size; /* Size of group descriptor */ + uint32_t default_mount_opts; /* Default mount options */ + uint32_t first_meta_bg; /* First metablock block group */ + uint32_t mkfs_time; /* When the filesystem was created */ + uint32_t journal_blocks[17]; /* Backup of the journal inode */ + + /* 64bit support valid if EXT4_FEATURE_COMPAT_64BIT */ + uint32_t blocks_count_hi; /* Blocks count */ + uint32_t reserved_blocks_count_hi; /* Reserved blocks count */ + uint32_t free_blocks_count_hi; /* Free blocks count */ + uint16_t min_extra_isize; /* All inodes have at least # bytes */ + uint16_t want_extra_isize; /* New inodes should reserve # bytes */ + uint32_t flags; /* Miscellaneous flags */ + uint16_t raid_stride; /* RAID stride */ + uint16_t mmp_interval; /* # seconds to wait in MMP checking */ + uint64_t mmp_block; /* Block for multi-mount protection */ + uint32_t raid_stripe_width; /* Blocks on all data disks (N * stride) */ + uint8_t log_groups_per_flex; /* FLEX_BG group size */ + uint8_t reserved_char_pad; + uint16_t reserved_pad; + uint64_t kbytes_written; /* Number of lifetime kilobytes written */ + uint32_t snapshot_inum; /* I-node number of active snapshot */ + uint32_t snapshot_id; /* Sequential ID of active snapshot */ + uint64_t snapshot_r_blocks_count; /* Reserved blocks for active snapshot's future use */ + uint32_t snapshot_list; /* I-node number of the head of the on-disk snapshot list */ + uint32_t error_count; /* Number of file system errors */ + uint32_t first_error_time; /* First time an error happened */ + uint32_t first_error_ino; /* I-node involved in first error */ + uint64_t first_error_block; /* Block involved of first error */ + uint8_t first_error_func[32]; /* Function where the error happened */ + uint32_t first_error_line; /* Line number where error happened */ + uint32_t last_error_time; /* Most recent time of an error */ + uint32_t last_error_ino; /* I-node involved in last error */ + uint32_t last_error_line; /* Line number where error happened */ + uint64_t last_error_block; /* Block involved of last error */ + uint8_t last_error_func[32]; /* Function where the error happened */ + uint8_t mount_opts[64]; + uint32_t padding[112]; /* Padding to the end of the block */ } __attribute__((packed)); @@ -217,24 +217,24 @@ struct ext4_sblock { #define EXT4_FEATURE_COMPAT_SUPP (EXT4_FEATURE_COMPAT_DIR_INDEX) #define EXT4_FEATURE_INCOMPAT_SUPP \ - (EXT4_FEATURE_INCOMPAT_FILETYPE | \ - EXT4_FEATURE_INCOMPAT_EXTENTS | \ - EXT4_FEATURE_INCOMPAT_64BIT) + (EXT4_FEATURE_INCOMPAT_FILETYPE | \ + EXT4_FEATURE_INCOMPAT_EXTENTS | \ + EXT4_FEATURE_INCOMPAT_64BIT) #define EXT4_FEATURE_RO_COMPAT_SUPP \ - (EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER | \ - EXT4_FEATURE_RO_COMPAT_DIR_NLINK | \ - EXT4_FEATURE_RO_COMPAT_HUGE_FILE | \ - EXT4_FEATURE_RO_COMPAT_LARGE_FILE | \ - EXT4_FEATURE_RO_COMPAT_GDT_CSUM | \ - EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE) + (EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER | \ + EXT4_FEATURE_RO_COMPAT_DIR_NLINK | \ + EXT4_FEATURE_RO_COMPAT_HUGE_FILE | \ + EXT4_FEATURE_RO_COMPAT_LARGE_FILE | \ + EXT4_FEATURE_RO_COMPAT_GDT_CSUM | \ + EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE) struct ext4_fs { - struct ext4_blockdev *bdev; - struct ext4_sblock sb; + struct ext4_blockdev *bdev; + struct ext4_sblock sb; - uint64_t inode_block_limits[4]; - uint64_t inode_blocks_per_level[4]; + uint64_t inode_block_limits[4]; + uint64_t inode_blocks_per_level[4]; }; @@ -246,33 +246,33 @@ struct ext4_fs { * Structure of a blocks group descriptor */ struct ext4_bgroup { - uint32_t block_bitmap_lo; /* Blocks bitmap block */ - uint32_t inode_bitmap_lo; /* Inodes bitmap block */ - uint32_t inode_table_first_block_lo; /* Inodes table block */ - uint16_t free_blocks_count_lo; /* Free blocks count */ - uint16_t free_inodes_count_lo; /* Free inodes count */ - uint16_t used_dirs_count_lo; /* Directories count */ - uint16_t flags; /* EXT4_BG_flags (INODE_UNINIT, etc) */ - uint32_t reserved[2]; /* Likely block/inode bitmap checksum */ - uint16_t itable_unused_lo; /* Unused inodes count */ - uint16_t checksum; /* crc16(sb_uuid+group+desc) */ - - uint32_t block_bitmap_hi; /* Blocks bitmap block MSB */ - uint32_t inode_bitmap_hi; /* I-nodes bitmap block MSB */ - uint32_t inode_table_first_block_hi; /* I-nodes table block MSB */ - uint16_t free_blocks_count_hi; /* Free blocks count MSB */ - uint16_t free_inodes_count_hi; /* Free i-nodes count MSB */ - uint16_t used_dirs_count_hi; /* Directories count MSB */ - uint16_t itable_unused_hi; /* Unused inodes count MSB */ - uint32_t reserved2[3]; /* Padding */ + uint32_t block_bitmap_lo; /* Blocks bitmap block */ + uint32_t inode_bitmap_lo; /* Inodes bitmap block */ + uint32_t inode_table_first_block_lo; /* Inodes table block */ + uint16_t free_blocks_count_lo; /* Free blocks count */ + uint16_t free_inodes_count_lo; /* Free inodes count */ + uint16_t used_dirs_count_lo; /* Directories count */ + uint16_t flags; /* EXT4_BG_flags (INODE_UNINIT, etc) */ + uint32_t reserved[2]; /* Likely block/inode bitmap checksum */ + uint16_t itable_unused_lo; /* Unused inodes count */ + uint16_t checksum; /* crc16(sb_uuid+group+desc) */ + + uint32_t block_bitmap_hi; /* Blocks bitmap block MSB */ + uint32_t inode_bitmap_hi; /* I-nodes bitmap block MSB */ + uint32_t inode_table_first_block_hi; /* I-nodes table block MSB */ + uint16_t free_blocks_count_hi; /* Free blocks count MSB */ + uint16_t free_inodes_count_hi; /* Free i-nodes count MSB */ + uint16_t used_dirs_count_hi; /* Directories count MSB */ + uint16_t itable_unused_hi; /* Unused inodes count MSB */ + uint32_t reserved2[3]; /* Padding */ } ; struct ext4_block_group_ref { - struct ext4_block block; - struct ext4_bgroup *block_group; - struct ext4_fs *fs; - uint32_t index; - bool dirty; + struct ext4_block block; + struct ext4_bgroup *block_group; + struct ext4_fs *fs; + uint32_t index; + bool dirty; }; #define EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE 32 @@ -295,49 +295,49 @@ struct ext4_block_group_ref { * Structure of an inode on the disk */ struct ext4_inode { - uint16_t mode; /* File mode */ - uint16_t uid; /* Low 16 bits of owner uid */ - uint32_t size_lo; /* Size in bytes */ - uint32_t access_time; /* Access time */ - uint32_t change_inode_time; /* I-node change time */ - uint32_t modification_time; /* Modification time */ - uint32_t deletion_time; /* Deletion time */ - uint16_t gid; /* Low 16 bits of group id */ - uint16_t links_count; /* Links count */ - uint32_t blocks_count_lo; /* Blocks count */ - uint32_t flags; /* File flags */ - uint32_t unused_osd1; /* OS dependent - not used in HelenOS */ - uint32_t blocks[EXT4_INODE_BLOCKS]; /* Pointers to blocks */ - uint32_t generation; /* File version (for NFS) */ - uint32_t file_acl_lo; /* File ACL */ - uint32_t size_hi; - uint32_t obso_faddr; /* Obsoleted fragment address */ - - union { - struct { - uint16_t blocks_high; - uint16_t file_acl_high; - uint16_t uid_high; - uint16_t gid_high; - uint32_t reserved2; - } linux2; - struct { - uint16_t reserved1; - uint16_t mode_high; - uint16_t uid_high; - uint16_t gid_high; - uint32_t author; - } hurd2; - } __attribute__ ((packed)) osd2; - - uint16_t extra_isize; - uint16_t pad1; - uint32_t ctime_extra; /* Extra change time (nsec << 2 | epoch) */ - uint32_t mtime_extra; /* Extra Modification time (nsec << 2 | epoch) */ - uint32_t atime_extra; /* Extra Access time (nsec << 2 | epoch) */ - uint32_t crtime; /* File creation time */ - uint32_t crtime_extra; /* Extra file creation time (nsec << 2 | epoch) */ - uint32_t version_hi; /* High 32 bits for 64-bit version */ + uint16_t mode; /* File mode */ + uint16_t uid; /* Low 16 bits of owner uid */ + uint32_t size_lo; /* Size in bytes */ + uint32_t access_time; /* Access time */ + uint32_t change_inode_time; /* I-node change time */ + uint32_t modification_time; /* Modification time */ + uint32_t deletion_time; /* Deletion time */ + uint16_t gid; /* Low 16 bits of group id */ + uint16_t links_count; /* Links count */ + uint32_t blocks_count_lo; /* Blocks count */ + uint32_t flags; /* File flags */ + uint32_t unused_osd1; /* OS dependent - not used in HelenOS */ + uint32_t blocks[EXT4_INODE_BLOCKS]; /* Pointers to blocks */ + uint32_t generation; /* File version (for NFS) */ + uint32_t file_acl_lo; /* File ACL */ + uint32_t size_hi; + uint32_t obso_faddr; /* Obsoleted fragment address */ + + union { + struct { + uint16_t blocks_high; + uint16_t file_acl_high; + uint16_t uid_high; + uint16_t gid_high; + uint32_t reserved2; + } linux2; + struct { + uint16_t reserved1; + uint16_t mode_high; + uint16_t uid_high; + uint16_t gid_high; + uint32_t author; + } hurd2; + } __attribute__ ((packed)) osd2; + + uint16_t extra_isize; + uint16_t pad1; + uint32_t ctime_extra; /* Extra change time (nsec << 2 | epoch) */ + uint32_t mtime_extra; /* Extra Modification time (nsec << 2 | epoch) */ + uint32_t atime_extra; /* Extra Access time (nsec << 2 | epoch) */ + uint32_t crtime; /* File creation time */ + uint32_t crtime_extra; /* Extra file creation time (nsec << 2 | epoch) */ + uint32_t version_hi; /* High 32 bits for 64-bit version */ } __attribute__ ((packed)) ; #define EXT4_INODE_MODE_FIFO 0x1000 @@ -382,11 +382,11 @@ struct ext4_inode { #define EXT4_INODE_ROOT_INDEX 2 struct ext4_inode_ref { - struct ext4_block block; - struct ext4_inode *inode; - struct ext4_fs *fs; - uint32_t index; - bool dirty; + struct ext4_block block; + struct ext4_inode *inode; + struct ext4_fs *fs; + uint32_t index; + bool dirty; } ; @@ -405,80 +405,80 @@ struct ext4_inode_ref { * Linked list directory entry structure */ struct ext4_directory_entry_ll { - uint32_t inode; /* I-node for the entry */ - uint16_t entry_length; /* Distance to the next directory entry */ - uint8_t name_length; /* Lower 8 bits of name length */ + uint32_t inode; /* I-node for the entry */ + uint16_t entry_length; /* Distance to the next directory entry */ + uint8_t name_length; /* Lower 8 bits of name length */ - union { - uint8_t name_length_high; /* Higher 8 bits of name length */ - uint8_t inode_type; /* Type of referenced inode (in rev >= 0.5) */ - } __attribute__ ((packed)); + union { + uint8_t name_length_high; /* Higher 8 bits of name length */ + uint8_t inode_type; /* Type of referenced inode (in rev >= 0.5) */ + } __attribute__ ((packed)); - uint8_t name[EXT4_DIRECTORY_FILENAME_LEN]; /* Entry name */ + uint8_t name[EXT4_DIRECTORY_FILENAME_LEN]; /* Entry name */ } __attribute__((packed)) ; struct ext4_directory_iterator { - struct ext4_inode_ref *inode_ref; - struct ext4_block current_block; - uint64_t current_offset; - struct ext4_directory_entry_ll *current; + struct ext4_inode_ref *inode_ref; + struct ext4_block current_block; + uint64_t current_offset; + struct ext4_directory_entry_ll *current; }; struct ext4_directory_search_result { - struct ext4_block block; - struct ext4_directory_entry_ll *dentry; + struct ext4_block block; + struct ext4_directory_entry_ll *dentry; }; /* Structures for indexed directory */ struct ext4_directory_dx_countlimit { - uint16_t limit; - uint16_t count; + uint16_t limit; + uint16_t count; } ; struct ext4_directory_dx_dot_entry { - uint32_t inode; - uint16_t entry_length; - uint8_t name_length; - uint8_t inode_type; - uint8_t name[4]; + uint32_t inode; + uint16_t entry_length; + uint8_t name_length; + uint8_t inode_type; + uint8_t name[4]; } ; struct ext4_directory_dx_root_info { - uint32_t reserved_zero; - uint8_t hash_version; - uint8_t info_length; - uint8_t indirect_levels; - uint8_t unused_flags; + uint32_t reserved_zero; + uint8_t hash_version; + uint8_t info_length; + uint8_t indirect_levels; + uint8_t unused_flags; } ; struct ext4_directory_dx_entry { - uint32_t hash; - uint32_t block; + uint32_t hash; + uint32_t block; } ; struct ext4_directory_dx_root { - struct ext4_directory_dx_dot_entry dots[2]; - struct ext4_directory_dx_root_info info; - struct ext4_directory_dx_entry entries[0]; + struct ext4_directory_dx_dot_entry dots[2]; + struct ext4_directory_dx_root_info info; + struct ext4_directory_dx_entry entries[0]; }; struct ext4_fake_directory_entry { - uint32_t inode; - uint16_t entry_length; - uint8_t name_length; - uint8_t inode_type; + uint32_t inode; + uint16_t entry_length; + uint8_t name_length; + uint8_t inode_type; }; struct ext4_directory_dx_node { - struct ext4_fake_directory_entry fake; - struct ext4_directory_dx_entry entries[0]; + struct ext4_fake_directory_entry fake; + struct ext4_directory_dx_entry entries[0]; }; struct ext4_directory_dx_block { - struct ext4_block block; - struct ext4_directory_dx_entry *entries; - struct ext4_directory_dx_entry *position; + struct ext4_block block; + struct ext4_directory_dx_entry *entries; + struct ext4_directory_dx_entry *position; } ; #define EXT4_ERR_BAD_DX_DIR (-75000) @@ -489,10 +489,10 @@ struct ext4_directory_dx_block { * It's used at the bottom of the tree. */ struct ext4_extent { - uint32_t first_block; /* First logical block extent covers */ - uint16_t block_count; /* Number of blocks covered by extent */ - uint16_t start_hi; /* High 16 bits of physical block */ - uint32_t start_lo; /* Low 32 bits of physical block */ + uint32_t first_block; /* First logical block extent covers */ + uint16_t block_count; /* Number of blocks covered by extent */ + uint16_t start_hi; /* High 16 bits of physical block */ + uint32_t start_lo; /* Low 32 bits of physical block */ } ; /* @@ -500,44 +500,44 @@ struct ext4_extent { * It's used at all the levels except the bottom. */ struct ext4_extent_index { - uint32_t first_block; /* Index covers logical blocks from 'block' */ - - /** - * Pointer to the physical block of the next - * level. leaf or next index could be there - * high 16 bits of physical block - */ - uint32_t leaf_lo; - uint16_t leaf_hi; - uint16_t padding; + uint32_t first_block; /* Index covers logical blocks from 'block' */ + + /** + * Pointer to the physical block of the next + * level. leaf or next index could be there + * high 16 bits of physical block + */ + uint32_t leaf_lo; + uint16_t leaf_hi; + uint16_t padding; } ; /* * Each block (leaves and indexes), even inode-stored has header. */ struct ext4_extent_header { - uint16_t magic; - uint16_t entries_count; /* Number of valid entries */ - uint16_t max_entries_count; /* Capacity of store in entries */ - uint16_t depth; /* Has tree real underlying blocks? */ - uint32_t generation; /* generation of the tree */ + uint16_t magic; + uint16_t entries_count; /* Number of valid entries */ + uint16_t max_entries_count; /* Capacity of store in entries */ + uint16_t depth; /* Has tree real underlying blocks? */ + uint32_t generation; /* generation of the tree */ } ; struct ext4_extent_path { - struct ext4_block block; - uint16_t depth; - struct ext4_extent_header *header; - struct ext4_extent_index *index; - struct ext4_extent *extent; + struct ext4_block block; + uint16_t depth; + struct ext4_extent_header *header; + struct ext4_extent_index *index; + struct ext4_extent *extent; } ; #define EXT4_EXTENT_MAGIC 0xF30A #define EXT4_EXTENT_FIRST(header) \ - ((struct ext4_extent *) (((void *) (header)) + sizeof(struct ext4_extent_header))) + ((struct ext4_extent *) (((void *) (header)) + sizeof(struct ext4_extent_header))) #define EXT4_EXTENT_FIRST_INDEX(header) \ - ((struct ext4_extent_index *) (((void *) (header)) + sizeof(struct ext4_extent_header))) + ((struct ext4_extent_index *) (((void *) (header)) + sizeof(struct ext4_extent_header))) /* EXT3 HTree directory indexing */ @@ -552,10 +552,10 @@ struct ext4_extent_path { struct ext4_hash_info { - uint32_t hash; - uint32_t minor_hash; - uint32_t hash_version; - const uint32_t *seed; + uint32_t hash; + uint32_t minor_hash; + uint32_t hash_version; + const uint32_t *seed; }; /*****************************************************************************/ @@ -564,28 +564,28 @@ struct ext4_hash_info { #ifdef CONFIG_BIG_ENDIAN static inline uint64_t to_le64(uint64_t n) { - return ((n & 0xff) << 56) | - ((n & 0xff00) << 40) | - ((n & 0xff0000) << 24) | - ((n & 0xff000000LL) << 8) | - ((n & 0xff00000000LL) >> 8) | - ((n & 0xff0000000000LL) >> 24) | - ((n & 0xff000000000000LL) >> 40) | - ((n & 0xff00000000000000LL) >> 56); + return ((n & 0xff) << 56) | + ((n & 0xff00) << 40) | + ((n & 0xff0000) << 24) | + ((n & 0xff000000LL) << 8) | + ((n & 0xff00000000LL) >> 8) | + ((n & 0xff0000000000LL) >> 24) | + ((n & 0xff000000000000LL) >> 40) | + ((n & 0xff00000000000000LL) >> 56); } static inline uint32_t to_le32(uint32_t n) { - return ((n & 0xff) << 24) | - ((n & 0xff00) << 8) | - ((n & 0xff0000) >> 8) | - ((n & 0xff000000) >> 24); + return ((n & 0xff) << 24) | + ((n & 0xff00) << 8) | + ((n & 0xff0000) >> 8) | + ((n & 0xff000000) >> 24); } static inline uint16_t to_le16(uint16_t n) { - return ((n & 0xff) << 8) | - ((n & 0xff00) >> 8); + return ((n & 0xff) << 8) | + ((n & 0xff00) >> 8); } |
