shithub: lwext4

Download patch

ref: 8cdd8add659ccbcaccf45694eaca14bd3094fa1d
parent: 5f8ffa50abd0840ea008547df257524abc880da5
author: gkostka <kostka.grzegorz@gmail.com>
date: Wed Oct 9 16:07:58 EDT 2013

Block cache free optimized.
Stack usage improvement.

--- a/src/ext4.h
+++ b/src/ext4.h
@@ -200,7 +200,7 @@
  *  |---------------------------------------------------------------|
  *  |   w+ or wb+ or w+b        O_RDWR|O_CREAT|O_TRUNC              |
  *  |---------------------------------------------------------------|
- *  |   a+ or ab+ or a+b      O_RDWR|O_CREAT|O_APPEND               |
+ *  |   a+ or ab+ or a+b        O_RDWR|O_CREAT|O_APPEND             |
  *  |---------------------------------------------------------------|
  *
  * @return	standard error code*/
--- a/src/lwext4/ext4.c
+++ b/src/lwext4/ext4.c
@@ -48,7 +48,7 @@
 #include <stdlib.h>
 #include <string.h>
 
-#include "ext4.h"
+#include <ext4.h>
 
 /**@brief	Mount point OS dependent lock*/
 #define EXT4_MP_LOCK(_m)    \
@@ -175,14 +175,14 @@
 
 
 static int ext4_link(struct ext4_mountpoint *mp, struct ext4_inode_ref *parent,
-        struct ext4_inode_ref *child, const char *name)
+        struct ext4_inode_ref *child, const char *name, uint32_t name_len)
 {
     /* Check maximum name length */
-    if (strlen(name) > EXT4_DIRECTORY_FILENAME_LEN)
+    if(name_len > EXT4_DIRECTORY_FILENAME_LEN)
         return EINVAL;
 
     /* Add entry to parent directory */
-    int rc = ext4_dir_add_entry(parent, name,
+    int rc = ext4_dir_add_entry(parent, name, name_len,
             child);
     if (rc != EOK)
         return rc;
@@ -190,18 +190,18 @@
     /* 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, ".",
+        rc = ext4_dir_add_entry(child, ".", strlen("."),
                 child);
         if (rc != EOK) {
-            ext4_dir_remove_entry(parent, name);
+            ext4_dir_remove_entry(parent, name, strlen(name));
             return rc;
         }
 
-        rc = ext4_dir_add_entry(child, "..",
+        rc = ext4_dir_add_entry(child, "..", strlen(".."),
                 parent);
         if (rc != EOK) {
-            ext4_dir_remove_entry(parent, name);
-            ext4_dir_remove_entry(child, ".");
+            ext4_dir_remove_entry(parent, name, strlen(name));
+            ext4_dir_remove_entry(child, ".", strlen("."));
             return rc;
         }
 
@@ -239,7 +239,7 @@
 
 static int ext4_unlink(struct ext4_mountpoint *mp,
     struct ext4_inode_ref *parent, struct ext4_inode_ref *child_inode_ref,
-    const char *name)
+    const char *name, uint32_t name_len)
 {
     bool has_children;
     int rc = ext4_has_children(&has_children, child_inode_ref);
@@ -252,7 +252,7 @@
 
     /* Remove entry from parent directory */
 
-    rc = ext4_dir_remove_entry(parent, name);
+    rc = ext4_dir_remove_entry(parent, name, name_len);
     if (rc != EOK)
         return rc;
 
@@ -501,7 +501,6 @@
     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;
@@ -547,10 +546,7 @@
             break;
         }
 
-        memcpy(entry_name, path, len);
-        entry_name[len] = 0;
-
-        r = ext4_dir_find_entry(&result, &ref, entry_name);
+        r = ext4_dir_find_entry(&result, &ref, path, len);
         if(r != EOK){
 
             if(r != ENOENT)
@@ -569,7 +565,7 @@
             ext4_dir_destroy_result(&ref, &result);
 
             /*Link with root dir.*/
-            r = ext4_link(mp, &ref, &child_ref, entry_name);
+            r = ext4_link(mp, &ref, &child_ref, path, len);
             if(r != EOK){
                 /*Fali. Free new inode.*/
                 ext4_fs_free_inode(&child_ref);
@@ -665,7 +661,6 @@
 {
     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;
@@ -673,6 +668,7 @@
 
     struct ext4_inode_ref	parent;
     struct ext4_inode_ref	child;
+    int len = 0;
 
     if(!mp)
         return ENOENT;
@@ -700,7 +696,7 @@
 
     while(1){
 
-        int len = ext4_path_check(path, &is_goal);
+        len = ext4_path_check(path, &is_goal);
 
         if(!len){
             r = ENOENT;
@@ -707,10 +703,7 @@
             break;
         }
 
-        memcpy(entry_name, path, len);
-        entry_name[len] = 0;
-
-        r = ext4_dir_find_entry(&result, &parent, entry_name);
+        r = ext4_dir_find_entry(&result, &parent, path, len);
         if(r != EOK){
             ext4_dir_destroy_result(&parent, &result);
             break;
@@ -779,7 +772,7 @@
         goto Finish;
 
     /*Unlink from parent.*/
-    r = ext4_unlink(mp, &parent, &child, entry_name);
+    r = ext4_unlink(mp, &parent, &child, path, len);
     if(r != EOK)
         goto Finish;
 
--- a/src/lwext4/ext4_bcache.c
+++ b/src/lwext4/ext4_bcache.c
@@ -169,8 +169,9 @@
             /*Update usage marker*/
             bc->lru_id[i] = ++bc->lru_ctr;
 
-            /*Set valid cache data*/
+            /*Set valid cache data and id*/
             b->data = bc->data + i * bc->itemsize;
+            b->cache_id = i;
 
             return EOK;
         }
@@ -208,8 +209,9 @@
         bc->refctr[cache_id]   = 1;
         bc->lru_id[cache_id] = ++bc->lru_ctr;
 
-        /*Set valid cache data*/
+        /*Set valid cache data and id*/
         b->data = bc->data + cache_id * bc->itemsize;
+        b->cache_id = cache_id;
 
         /*Statistics*/
         bc->ref_blocks++;
@@ -231,8 +233,6 @@
 int ext4_bcache_free (struct ext4_bcache *bc, struct ext4_block *b,
     uint8_t free_delay)
 {
-    uint32_t i;
-
     ext4_assert(bc && b);
 
     /*Check if valid.*/
@@ -239,36 +239,30 @@
     ext4_assert(b->lb_id);
 
     /*Block should be in cache.*/
-    for (i = 0; i < bc->cnt; ++i) {
+    ext4_assert(b->cache_id < bc->cnt);
 
-        if(bc->lba[i] != b->lb_id)
-            continue;
+    /*Check if someone don't try free unreferenced block cache.*/
+    ext4_assert(bc->refctr[b->cache_id]);
 
-        /*Check if someone don't try free unreferenced block cache.*/
-        ext4_assert(bc->refctr[i]);
+    /*Just decrease reference counter*/
+    if(bc->refctr[b->cache_id])
+        bc->refctr[b->cache_id]--;
 
-        /*Just decrease reference counter*/
-        if(bc->refctr[i])
-            bc->refctr[i]--;
+    if(free_delay)
+        bc->free_delay[b->cache_id] = free_delay;
 
+    /*Update statistics*/
+    if(!bc->refctr[b->cache_id] && !bc->free_delay[b->cache_id])
+        bc->ref_blocks--;
 
-        if(free_delay)
-            bc->free_delay[i] = free_delay;
+    b->lb_id    = 0;
+    b->data     = 0;
+    b->cache_id = 0;
 
-        /*Update statistics*/
-        if(!bc->refctr[i] && !bc->free_delay[i])
-            bc->ref_blocks--;
+    return EOK;
+}
 
-        b->lb_id = 0;
-        b->data  = 0;
 
-        return EOK;
-    }
-
-    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)
 {
--- a/src/lwext4/ext4_bcache.h
+++ b/src/lwext4/ext4_bcache.h
@@ -50,6 +50,9 @@
     /**@brief	Logical block ID*/
     uint64_t	lb_id;
 
+    /**@brief   Cache id*/
+    uint32_t    cache_id;
+
     /**@brief	Data buffer.*/
     uint8_t		*data;
 };
--- a/src/lwext4/ext4_blockdev.c
+++ b/src/lwext4/ext4_blockdev.c
@@ -155,9 +155,6 @@
         return EOK;
     }
 
-    b->dirty = false;
-
-
     /*Free cache delay mode*/
     if(bdev->cache_flush_delay){
 
--- a/src/lwext4/ext4_dir.c
+++ b/src/lwext4/ext4_dir.c
@@ -269,7 +269,7 @@
 }
 
 int ext4_dir_add_entry(struct ext4_inode_ref *parent, const char *name,
-        struct ext4_inode_ref *child)
+        uint32_t name_len, struct ext4_inode_ref *child)
 {
     struct ext4_fs *fs = parent->fs;
 
@@ -301,7 +301,6 @@
     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;
@@ -360,10 +359,8 @@
 }
 
 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)
 {
-    uint32_t name_len = strlen(name);
-
     struct ext4_sblock *sb = &parent->fs->sb;
 
 
@@ -436,7 +433,8 @@
     return ENOENT;
 }
 
-int ext4_dir_remove_entry(struct ext4_inode_ref *parent, const char *name)
+int ext4_dir_remove_entry(struct ext4_inode_ref *parent, const char *name,
+    uint32_t name_len)
 {
     /* Check if removing from directory */
     if (!ext4_inode_is_type(&parent->fs->sb, parent->inode,
@@ -445,7 +443,7 @@
 
     /* Try to find entry */
     struct ext4_directory_search_result result;
-    int rc = ext4_dir_find_entry(&result, parent, name);
+    int rc = ext4_dir_find_entry(&result, parent, name, name_len);
     if (rc != EOK)
         return rc;
 
--- a/src/lwext4/ext4_dir.h
+++ b/src/lwext4/ext4_dir.h
@@ -84,13 +84,14 @@
     struct ext4_directory_entry_ll *entry, 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);
+int ext4_dir_add_entry(struct ext4_inode_ref *parent, const char *name,
+        uint32_t name_len, 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, uint32_t name_len);
 
-int ext4_dir_remove_entry(struct ext4_inode_ref *parent, const char *name);
+int ext4_dir_remove_entry(struct ext4_inode_ref *parent, const char *name,
+    uint32_t name_len);
 
 int ext4_dir_try_insert_entry(struct ext4_sblock *sb,
     struct ext4_block *target_block, struct ext4_inode_ref *child,