Block cache free optimized.
authorgkostka <kostka.grzegorz@gmail.com>
Wed, 9 Oct 2013 20:07:58 +0000 (20:07 +0000)
committergkostka <kostka.grzegorz@gmail.com>
Wed, 9 Oct 2013 20:07:58 +0000 (20:07 +0000)
Stack usage improvement.

src/ext4.h
src/lwext4/ext4.c
src/lwext4/ext4_bcache.c
src/lwext4/ext4_bcache.h
src/lwext4/ext4_blockdev.c
src/lwext4/ext4_dir.c
src/lwext4/ext4_dir.h

index 4be5e249240f35f0c25f5ddd038628e29e90c311..318e512edb66d1237439676e49dd9ef9a7b975ee 100644 (file)
@@ -200,7 +200,7 @@ int     ext4_fremove(const char *path);
  *  |---------------------------------------------------------------|\r
  *  |   w+ or wb+ or w+b        O_RDWR|O_CREAT|O_TRUNC              |\r
  *  |---------------------------------------------------------------|\r
- *  |   a+ or ab+ or a+b      O_RDWR|O_CREAT|O_APPEND               |\r
+ *  |   a+ or ab+ or a+b        O_RDWR|O_CREAT|O_APPEND             |\r
  *  |---------------------------------------------------------------|\r
  *\r
  * @return     standard error code*/\r
index a935a80a215ef665db107979fae70a94dabb1196..011e612b91f8dae72789a56736f74b2fd0c65c92 100644 (file)
@@ -48,7 +48,7 @@
 #include <stdlib.h>\r
 #include <string.h>\r
 \r
-#include "ext4.h"\r
+#include <ext4.h>\r
 \r
 /**@brief      Mount point OS dependent lock*/\r
 #define EXT4_MP_LOCK(_m)    \\r
@@ -175,14 +175,14 @@ static int ext4_has_children(bool *has_children, struct ext4_inode_ref *enode)
 \r
 \r
 static int ext4_link(struct ext4_mountpoint *mp, struct ext4_inode_ref *parent,\r
-        struct ext4_inode_ref *child, const char *name)\r
+        struct ext4_inode_ref *child, const char *name, uint32_t name_len)\r
 {\r
     /* Check maximum name length */\r
-    if (strlen(name) > EXT4_DIRECTORY_FILENAME_LEN)\r
+    if(name_len > EXT4_DIRECTORY_FILENAME_LEN)\r
         return EINVAL;\r
 \r
     /* Add entry to parent directory */\r
-    int rc = ext4_dir_add_entry(parent, name,\r
+    int rc = ext4_dir_add_entry(parent, name, name_len,\r
             child);\r
     if (rc != EOK)\r
         return rc;\r
@@ -190,18 +190,18 @@ static int ext4_link(struct ext4_mountpoint *mp, struct ext4_inode_ref *parent,
     /* Fill new dir -> add '.' and '..' entries */\r
     if (ext4_inode_is_type(&mp->fs.sb, child->inode,\r
             EXT4_INODE_MODE_DIRECTORY)) {\r
-        rc = ext4_dir_add_entry(child, ".",\r
+        rc = ext4_dir_add_entry(child, ".", strlen("."),\r
                 child);\r
         if (rc != EOK) {\r
-            ext4_dir_remove_entry(parent, name);\r
+            ext4_dir_remove_entry(parent, name, strlen(name));\r
             return rc;\r
         }\r
 \r
-        rc = ext4_dir_add_entry(child, "..",\r
+        rc = ext4_dir_add_entry(child, "..", strlen(".."),\r
                 parent);\r
         if (rc != EOK) {\r
-            ext4_dir_remove_entry(parent, name);\r
-            ext4_dir_remove_entry(child, ".");\r
+            ext4_dir_remove_entry(parent, name, strlen(name));\r
+            ext4_dir_remove_entry(child, ".", strlen("."));\r
             return rc;\r
         }\r
 \r
@@ -239,7 +239,7 @@ static int ext4_link(struct ext4_mountpoint *mp, struct ext4_inode_ref *parent,
 \r
 static int ext4_unlink(struct ext4_mountpoint *mp,\r
     struct ext4_inode_ref *parent, struct ext4_inode_ref *child_inode_ref,\r
-    const char *name)\r
+    const char *name, uint32_t name_len)\r
 {\r
     bool has_children;\r
     int rc = ext4_has_children(&has_children, child_inode_ref);\r
@@ -252,7 +252,7 @@ static int ext4_unlink(struct ext4_mountpoint *mp,
 \r
     /* Remove entry from parent directory */\r
 \r
-    rc = ext4_dir_remove_entry(parent, name);\r
+    rc = ext4_dir_remove_entry(parent, name, name_len);\r
     if (rc != EOK)\r
         return rc;\r
 \r
@@ -501,7 +501,6 @@ static int ext4_generic_open (ext4_file *f, const char *path,
     struct ext4_mountpoint *mp = ext4_get_mount(path);\r
     struct ext4_directory_search_result result;\r
     struct ext4_inode_ref      ref;\r
-    char       entry_name[255];\r
     bool       is_goal = false;\r
     uint8_t    inode_type;\r
     int                r = ENOENT;\r
@@ -547,10 +546,7 @@ static int ext4_generic_open (ext4_file *f, const char *path,
             break;\r
         }\r
 \r
-        memcpy(entry_name, path, len);\r
-        entry_name[len] = 0;\r
-\r
-        r = ext4_dir_find_entry(&result, &ref, entry_name);\r
+        r = ext4_dir_find_entry(&result, &ref, path, len);\r
         if(r != EOK){\r
 \r
             if(r != ENOENT)\r
@@ -569,7 +565,7 @@ static int ext4_generic_open (ext4_file *f, const char *path,
             ext4_dir_destroy_result(&ref, &result);\r
 \r
             /*Link with root dir.*/\r
-            r = ext4_link(mp, &ref, &child_ref, entry_name);\r
+            r = ext4_link(mp, &ref, &child_ref, path, len);\r
             if(r != EOK){\r
                 /*Fali. Free new inode.*/\r
                 ext4_fs_free_inode(&child_ref);\r
@@ -665,7 +661,6 @@ int ext4_fremove(const char *path)
 {\r
     struct ext4_mountpoint *mp = ext4_get_mount(path);\r
     struct ext4_directory_search_result result;\r
-    char       entry_name[255];\r
     bool       is_goal = false;\r
     uint8_t    inode_type;\r
     int                r = ENOENT;\r
@@ -673,6 +668,7 @@ int ext4_fremove(const char *path)
 \r
     struct ext4_inode_ref      parent;\r
     struct ext4_inode_ref      child;\r
+    int len = 0;\r
 \r
     if(!mp)\r
         return ENOENT;\r
@@ -700,17 +696,14 @@ int ext4_fremove(const char *path)
 \r
     while(1){\r
 \r
-        int len = ext4_path_check(path, &is_goal);\r
+        len = ext4_path_check(path, &is_goal);\r
 \r
         if(!len){\r
             r = ENOENT;\r
             break;\r
         }\r
 \r
-        memcpy(entry_name, path, len);\r
-        entry_name[len] = 0;\r
-\r
-        r = ext4_dir_find_entry(&result, &parent, entry_name);\r
+        r = ext4_dir_find_entry(&result, &parent, path, len);\r
         if(r != EOK){\r
             ext4_dir_destroy_result(&parent, &result);\r
             break;\r
@@ -779,7 +772,7 @@ int ext4_fremove(const char *path)
         goto Finish;\r
 \r
     /*Unlink from parent.*/\r
-    r = ext4_unlink(mp, &parent, &child, entry_name);\r
+    r = ext4_unlink(mp, &parent, &child, path, len);\r
     if(r != EOK)\r
         goto Finish;\r
 \r
index bf2ce1eaf748acb27f4d6bb086f8f4a1a93f4c9e..e4e3dd55c70bb63cc2a7e991a233d159e2a5bbac 100644 (file)
@@ -169,8 +169,9 @@ int ext4_bcache_alloc(struct ext4_bcache *bc, struct ext4_block *b,
             /*Update usage marker*/\r
             bc->lru_id[i] = ++bc->lru_ctr;\r
 \r
-            /*Set valid cache data*/\r
+            /*Set valid cache data and id*/\r
             b->data = bc->data + i * bc->itemsize;\r
+            b->cache_id = i;\r
 \r
             return EOK;\r
         }\r
@@ -208,8 +209,9 @@ int ext4_bcache_alloc(struct ext4_bcache *bc, struct ext4_block *b,
         bc->refctr[cache_id]   = 1;\r
         bc->lru_id[cache_id] = ++bc->lru_ctr;\r
 \r
-        /*Set valid cache data*/\r
+        /*Set valid cache data and id*/\r
         b->data = bc->data + cache_id * bc->itemsize;\r
+        b->cache_id = cache_id;\r
 \r
         /*Statistics*/\r
         bc->ref_blocks++;\r
@@ -231,44 +233,36 @@ int ext4_bcache_alloc(struct ext4_bcache *bc, struct ext4_block *b,
 int ext4_bcache_free (struct ext4_bcache *bc, struct ext4_block *b,\r
     uint8_t free_delay)\r
 {\r
-    uint32_t i;\r
-\r
     ext4_assert(bc && b);\r
 \r
     /*Check if valid.*/\r
     ext4_assert(b->lb_id);\r
 \r
     /*Block should be in cache.*/\r
-    for (i = 0; i < bc->cnt; ++i) {\r
+    ext4_assert(b->cache_id < bc->cnt);\r
 \r
-        if(bc->lba[i] != b->lb_id)\r
-            continue;\r
-\r
-        /*Check if someone don't try free unreferenced block cache.*/\r
-        ext4_assert(bc->refctr[i]);\r
+    /*Check if someone don't try free unreferenced block cache.*/\r
+    ext4_assert(bc->refctr[b->cache_id]);\r
 \r
-        /*Just decrease reference counter*/\r
-        if(bc->refctr[i])\r
-            bc->refctr[i]--;\r
+    /*Just decrease reference counter*/\r
+    if(bc->refctr[b->cache_id])\r
+        bc->refctr[b->cache_id]--;\r
 \r
+    if(free_delay)\r
+        bc->free_delay[b->cache_id] = free_delay;\r
 \r
-        if(free_delay)\r
-            bc->free_delay[i] = free_delay;\r
+    /*Update statistics*/\r
+    if(!bc->refctr[b->cache_id] && !bc->free_delay[b->cache_id])\r
+        bc->ref_blocks--;\r
 \r
-        /*Update statistics*/\r
-        if(!bc->refctr[i] && !bc->free_delay[i])\r
-            bc->ref_blocks--;\r
+    b->lb_id    = 0;\r
+    b->data     = 0;\r
+    b->cache_id = 0;\r
 \r
-        b->lb_id = 0;\r
-        b->data  = 0;\r
+    return EOK;\r
+}\r
 \r
-        return EOK;\r
-    }\r
 \r
-    ext4_dprintf(EXT4_DEBUG_BCACHE,\r
-        "ext4_bcache_free: FAIL, block should be in cache memory !\n");\r
-    return EFAULT;\r
-}\r
 \r
 bool ext4_bcache_is_full(struct ext4_bcache *bc)\r
 {\r
index 767e59893fffd19ee0657112abdfa99a1027598f..c64841b6cae60711ee5057edf22436f8758c8f29 100644 (file)
@@ -50,6 +50,9 @@ struct        ext4_block {
     /**@brief  Logical block ID*/\r
     uint64_t   lb_id;\r
 \r
+    /**@brief   Cache id*/\r
+    uint32_t    cache_id;\r
+\r
     /**@brief  Data buffer.*/\r
     uint8_t            *data;\r
 };\r
index 46f1ec82dc15136626c1a8d2a346103139a853b7..8d88e37c2b75c27b340131870acdd0b229329d5f 100644 (file)
@@ -155,9 +155,6 @@ int ext4_block_set(struct   ext4_blockdev *bdev, struct     ext4_block *b)
         return EOK;
     }
 
-    b->dirty = false;
-
-
     /*Free cache delay mode*/
     if(bdev->cache_flush_delay){
 
index 905743d859bbce4b6cd31d5b51d5ba4156351b56..4f0211976430064ffcd7c2f227ce1f14e1aa5d63 100644 (file)
@@ -269,7 +269,7 @@ void ext4_dir_write_entry(struct ext4_sblock *sb,
 }\r
 \r
 int ext4_dir_add_entry(struct ext4_inode_ref *parent, const char *name,\r
-        struct ext4_inode_ref *child)\r
+        uint32_t name_len, struct ext4_inode_ref *child)\r
 {\r
     struct ext4_fs *fs = parent->fs;\r
 \r
@@ -301,7 +301,6 @@ int ext4_dir_add_entry(struct ext4_inode_ref *parent, const char *name,
     uint32_t inode_size = ext4_inode_get_size(&fs->sb, parent->inode);\r
     uint32_t total_blocks = inode_size / block_size;\r
 \r
-    uint32_t name_len = strlen(name);\r
 \r
     /* Find block, where is space for new entry and try to add */\r
     bool success = false;\r
@@ -360,10 +359,8 @@ int ext4_dir_add_entry(struct ext4_inode_ref *parent, const char *name,
 }\r
 \r
 int ext4_dir_find_entry(struct ext4_directory_search_result *result,\r
-        struct ext4_inode_ref *parent, const char *name)\r
+        struct ext4_inode_ref *parent, const char *name, uint32_t name_len)\r
 {\r
-    uint32_t name_len = strlen(name);\r
-\r
     struct ext4_sblock *sb = &parent->fs->sb;\r
 \r
 \r
@@ -436,7 +433,8 @@ int ext4_dir_find_entry(struct ext4_directory_search_result *result,
     return ENOENT;\r
 }\r
 \r
-int ext4_dir_remove_entry(struct ext4_inode_ref *parent, const char *name)\r
+int ext4_dir_remove_entry(struct ext4_inode_ref *parent, const char *name,\r
+    uint32_t name_len)\r
 {\r
     /* Check if removing from directory */\r
     if (!ext4_inode_is_type(&parent->fs->sb, parent->inode,\r
@@ -445,7 +443,7 @@ int ext4_dir_remove_entry(struct ext4_inode_ref *parent, const char *name)
 \r
     /* Try to find entry */\r
     struct ext4_directory_search_result result;\r
-    int rc = ext4_dir_find_entry(&result, parent, name);\r
+    int rc = ext4_dir_find_entry(&result, parent, name, name_len);\r
     if (rc != EOK)\r
         return rc;\r
 \r
index edb23098f538fc20b8c431d550954226558a142b..5b65ed8725a3b46d4b102c745287eddb484cbe28 100644 (file)
@@ -84,13 +84,14 @@ void ext4_dir_write_entry(struct ext4_sblock *sb,
     struct ext4_directory_entry_ll *entry, uint16_t entry_len,\r
     struct ext4_inode_ref *child,  const char *name, size_t name_len);\r
 \r
-int  ext4_dir_add_entry(struct ext4_inode_ref *parent, const char *name,\r
-    struct ext4_inode_ref *child);\r
+int ext4_dir_add_entry(struct ext4_inode_ref *parent, const char *name,\r
+        uint32_t name_len, struct ext4_inode_ref *child);\r
 \r
 int ext4_dir_find_entry(struct ext4_directory_search_result *result,\r
-    struct ext4_inode_ref *parent, const char *name);\r
+        struct ext4_inode_ref *parent, const char *name, uint32_t name_len);\r
 \r
-int ext4_dir_remove_entry(struct ext4_inode_ref *parent, const char *name);\r
+int ext4_dir_remove_entry(struct ext4_inode_ref *parent, const char *name,\r
+    uint32_t name_len);\r
 \r
 int ext4_dir_try_insert_entry(struct ext4_sblock *sb,\r
     struct ext4_block *target_block, struct ext4_inode_ref *child,\r