瀏覽代碼

!137 增加dup及输入输出重定向支持
Merge pull request !137 from jesven/sys_dup

bernard 5 年之前
父節點
當前提交
c8759f7b0d

+ 259 - 113
components/dfs/filesystems/cromfs/dfs_cromfs.c

@@ -123,18 +123,18 @@ struct cromfs_avl_struct
     file_info *fi;
 };
 
-static void cromfs_avl_remove(struct cromfs_avl_struct * node_to_delete, struct cromfs_avl_struct ** ptree);
-static void cromfs_avl_insert(struct cromfs_avl_struct * new_node, struct cromfs_avl_struct ** ptree);
-static struct cromfs_avl_struct* cromfs_avl_find(avl_key_t key, struct cromfs_avl_struct* ptree);
+static void cromfs_avl_remove(struct cromfs_avl_struct *node_to_delete, struct cromfs_avl_struct **ptree);
+static void cromfs_avl_insert(struct cromfs_avl_struct *new_node, struct cromfs_avl_struct **ptree);
+static struct cromfs_avl_struct* cromfs_avl_find(avl_key_t key, struct cromfs_avl_struct *ptree);
 
-static void cromfs_avl_rebalance(struct cromfs_avl_struct *** nodeplaces_ptr, int count)
+static void cromfs_avl_rebalance(struct cromfs_avl_struct ***nodeplaces_ptr, int count)
 {
-    for ( ; count > 0 ; count--)
+    for (;count > 0; count--)
     {
-        struct cromfs_avl_struct ** nodeplace = *--nodeplaces_ptr;
-        struct cromfs_avl_struct * node = *nodeplace;
-        struct cromfs_avl_struct * nodeleft = node->avl_left;
-        struct cromfs_avl_struct * noderight = node->avl_right;
+        struct cromfs_avl_struct **nodeplace = *--nodeplaces_ptr;
+        struct cromfs_avl_struct *node = *nodeplace;
+        struct cromfs_avl_struct *nodeleft = node->avl_left;
+        struct cromfs_avl_struct *noderight = node->avl_right;
         int heightleft = heightof(nodeleft);
         int heightright = heightof(noderight);
         if (heightright + 1 < heightleft)
@@ -144,7 +144,8 @@ static void cromfs_avl_rebalance(struct cromfs_avl_struct *** nodeplaces_ptr, in
             int heightleftright = heightof(nodeleftright);
             if (heightof(nodeleftleft) >= heightleftright)
             {
-                node->avl_left = nodeleftright; nodeleft->avl_right = node;
+                node->avl_left = nodeleftright;
+                nodeleft->avl_right = node;
                 nodeleft->avl_height = 1 + (node->avl_height = 1 + heightleftright);
                 *nodeplace = nodeleft;
             }
@@ -161,12 +162,13 @@ static void cromfs_avl_rebalance(struct cromfs_avl_struct *** nodeplaces_ptr, in
         }
         else if (heightleft + 1 < heightright)
         {
-            struct cromfs_avl_struct * noderightright = noderight->avl_right;
-            struct cromfs_avl_struct * noderightleft = noderight->avl_left;
+            struct cromfs_avl_struct *noderightright = noderight->avl_right;
+            struct cromfs_avl_struct *noderightleft = noderight->avl_left;
             int heightrightleft = heightof(noderightleft);
             if (heightof(noderightright) >= heightrightleft)
             {
-                node->avl_right = noderightleft; noderight->avl_left = node;
+                node->avl_right = noderightleft;
+                noderight->avl_left = node;
                 noderight->avl_height = 1 + (node->avl_height = 1 + heightrightleft);
                 *nodeplace = noderight;
             }
@@ -184,38 +186,50 @@ static void cromfs_avl_rebalance(struct cromfs_avl_struct *** nodeplaces_ptr, in
         else {
             int height = (heightleft<heightright ? heightright : heightleft) + 1;
             if (height == node->avl_height)
+            {
                 break;
+            }
             node->avl_height = height;
         }
     }
 }
 
-static void cromfs_avl_remove(struct cromfs_avl_struct * node_to_delete, struct cromfs_avl_struct ** ptree)
+static void cromfs_avl_remove(struct cromfs_avl_struct *node_to_delete, struct cromfs_avl_struct **ptree)
 {
     avl_key_t key = node_to_delete->avl_key;
-    struct cromfs_avl_struct ** nodeplace = ptree;
-    struct cromfs_avl_struct ** stack[avl_maxheight];
+    struct cromfs_avl_struct **nodeplace = ptree;
+    struct cromfs_avl_struct **stack[avl_maxheight];
     uint32_t stack_count = 0;
-    struct cromfs_avl_struct *** stack_ptr = &stack[0]; /* = &stack[stackcount] */
-    struct cromfs_avl_struct ** nodeplace_to_delete;
+    struct cromfs_avl_struct ***stack_ptr = &stack[0]; /* = &stack[stackcount] */
+    struct cromfs_avl_struct **nodeplace_to_delete;
     for (;;)
     {
-        struct cromfs_avl_struct * node = *nodeplace;
+        struct cromfs_avl_struct *node = *nodeplace;
         if (node == AVL_EMPTY)
+        {
             return;
-        *stack_ptr++ = nodeplace; stack_count++;
+        }
+        *stack_ptr++ = nodeplace;
+        stack_count++;
         if (key == node->avl_key)
+        {
             break;
+        }
         if (key < node->avl_key)
+        {
             nodeplace = &node->avl_left;
+        }
         else
+        {
             nodeplace = &node->avl_right;
+        }
     }
     nodeplace_to_delete = nodeplace;
     if (node_to_delete->avl_left == AVL_EMPTY)
     {
         *nodeplace_to_delete = node_to_delete->avl_right;
-        stack_ptr--; stack_count--;
+        stack_ptr--;
+        stack_count--;
     }
     else
     {
@@ -226,8 +240,11 @@ static void cromfs_avl_remove(struct cromfs_avl_struct * node_to_delete, struct
         {
             node = *nodeplace;
             if (node->avl_right == AVL_EMPTY)
+            {
                 break;
-            *stack_ptr++ = nodeplace; stack_count++;
+            }
+            *stack_ptr++ = nodeplace;
+            stack_count++;
             nodeplace = &node->avl_right;
         }
         *nodeplace = node->avl_left;
@@ -240,23 +257,30 @@ static void cromfs_avl_remove(struct cromfs_avl_struct * node_to_delete, struct
     cromfs_avl_rebalance(stack_ptr,stack_count);
 }
 
-static void cromfs_avl_insert(struct cromfs_avl_struct * new_node, struct cromfs_avl_struct ** ptree)
+static void cromfs_avl_insert(struct cromfs_avl_struct *new_node, struct cromfs_avl_struct **ptree)
 {
     avl_key_t key = new_node->avl_key;
-    struct cromfs_avl_struct ** nodeplace = ptree;
-    struct cromfs_avl_struct ** stack[avl_maxheight];
+    struct cromfs_avl_struct **nodeplace = ptree;
+    struct cromfs_avl_struct **stack[avl_maxheight];
     int stack_count = 0;
-    struct cromfs_avl_struct *** stack_ptr = &stack[0]; /* = &stack[stackcount] */
+    struct cromfs_avl_struct ***stack_ptr = &stack[0]; /* = &stack[stackcount] */
     for (;;)
     {
         struct cromfs_avl_struct * node = *nodeplace;
         if (node == AVL_EMPTY)
+        {
             break;
-        *stack_ptr++ = nodeplace; stack_count++;
+        }
+        *stack_ptr++ = nodeplace;
+        stack_count++;
         if (key < node->avl_key)
+        {
             nodeplace = &node->avl_left;
+        }
         else
+        {
             nodeplace = &node->avl_right;
+        }
     }
     new_node->avl_left = AVL_EMPTY;
     new_node->avl_right = AVL_EMPTY;
@@ -270,13 +294,21 @@ static struct cromfs_avl_struct* cromfs_avl_find(avl_key_t key, struct cromfs_av
     for (;;)
     {
         if (ptree == AVL_EMPTY)
+        {
             return (struct cromfs_avl_struct *)0;
+        }
         if (key == ptree->avl_key)
+        {
             break;
+        }
         if (key < ptree->avl_key)
+        {
             ptree = ptree->avl_left;
+        }
         else
+        {
             ptree = ptree->avl_right;
+        }
     }
     return ptree;
 }
@@ -286,37 +318,47 @@ static struct cromfs_avl_struct* cromfs_avl_find(avl_key_t key, struct cromfs_av
 static uint32_t cromfs_read_bytes(cromfs_info *ci, uint32_t pos, void *buf, uint32_t size)
 {
     if (pos >= ci->partition_size || pos + size > ci->partition_size)
+    {
         return 0;
+    }
     return ci->read_bytes(ci, pos, buf, size);
 }
 
 static uint32_t cromfs_noblk_read_bytes(cromfs_info *ci, uint32_t pos, void *buf, uint32_t size)
 {
-    uint32_t ret;
+    uint32_t ret = 0;
 
     ret = rt_device_read(ci->device, pos, buf, size);
     if (ret != size)
+    {
         return 0;
+    }
     else
+    {
         return ret;
+    }
 }
 
 static uint32_t cromfs_blk_read_bytes(cromfs_info *ci, uint32_t pos, void *buf, uint32_t size)
 {
     uint32_t ret = 0;
     uint32_t size_bak = size;
-    uint32_t start_blk;
-    uint32_t end_blk;
-    uint32_t off_s;
-    uint32_t sector_nr;
-    uint8_t *block_buff;
-    uint32_t ret_len;
+    uint32_t start_blk = 0;
+    uint32_t end_blk = 0;
+    uint32_t off_s = 0;
+    uint32_t sector_nr = 0;
+    uint8_t *block_buff = NULL;
+    uint32_t ret_len = 0;
 
     if (!size || !buf)
+    {
         return 0;
+    }
     block_buff = (uint8_t *)malloc(2 * ci->bytes_per_sector);
     if (!block_buff)
+    {
         return 0;
+    }
     start_blk = pos / ci->bytes_per_sector;
     off_s = pos % ci->bytes_per_sector;
     end_blk = (pos + size - 1) / ci->bytes_per_sector;
@@ -326,14 +368,18 @@ static uint32_t cromfs_blk_read_bytes(cromfs_info *ci, uint32_t pos, void *buf,
     {
         ret_len = rt_device_read(ci->device, start_blk, block_buff, sector_nr + 1);
         if (ret_len != sector_nr + 1)
+        {
             goto end;
+        }
         memcpy(buf, block_buff + off_s, size);
     }
     else
     {
         ret_len = rt_device_read(ci->device, start_blk, block_buff, 1);
         if (ret_len != 1)
+        {
             goto end;
+        }
         memcpy(buf, block_buff + off_s, ci->bytes_per_sector - off_s);
         off_s = (ci->bytes_per_sector - off_s);
         size -= off_s;
@@ -343,14 +389,18 @@ static uint32_t cromfs_blk_read_bytes(cromfs_info *ci, uint32_t pos, void *buf,
         {
             ret_len = rt_device_read(ci->device, start_blk, (char*)buf + off_s, sector_nr);
             if (ret_len != sector_nr)
+            {
                 goto end;
+            }
             start_blk += sector_nr;
             off_s += (sector_nr * ci->bytes_per_sector);
             size -= (sector_nr * ci->bytes_per_sector);
         }
         ret_len = rt_device_read(ci->device, start_blk, block_buff, 1);
         if (ret_len != 1)
+        {
             goto end;
+        }
         memcpy((char*)buf + off_s, block_buff, size);
     }
     ret = size_bak;
@@ -363,9 +413,9 @@ end:
 
 static uint8_t *cromfs_dirent_cache_get(cromfs_info *ci, uint32_t pos, uint32_t size)
 {
-    rt_list_t *l;
-    cromfs_dirent_cache *dir;
-    uint32_t len;
+    rt_list_t *l = NULL;
+    cromfs_dirent_cache *dir = NULL;
+    uint32_t len = 0;
 
     /* find */
     for (l = ci->cromfs_dirent_cache_head.next; l != &ci->cromfs_dirent_cache_head; l = l->next)
@@ -391,19 +441,21 @@ static uint8_t *cromfs_dirent_cache_get(cromfs_info *ci, uint32_t pos, uint32_t
     }
     dir = (cromfs_dirent_cache *)malloc(sizeof *dir);
     if (!dir)
-        return RT_NULL;
+    {
+        return NULL;
+    }
     dir->buff = (uint8_t *)malloc(size);
     if (!dir->buff)
     {
         free(dir);
-        return RT_NULL;
+        return NULL;
     }
     len = cromfs_read_bytes(ci, pos, dir->buff, size);
     if (len != size)
     {
         free(dir->buff);
         free(dir);
-        return RT_NULL;
+        return NULL;
     }
     rt_list_insert_after(&ci->cromfs_dirent_cache_head, (rt_list_t *)dir);
     ci->cromfs_dirent_cache_nr++;
@@ -414,8 +466,8 @@ static uint8_t *cromfs_dirent_cache_get(cromfs_info *ci, uint32_t pos, uint32_t
 
 static void cromfs_dirent_cache_destroy(cromfs_info *ci)
 {
-    rt_list_t *l;
-    cromfs_dirent_cache *dir;
+    rt_list_t *l = NULL;
+    cromfs_dirent_cache *dir = NULL;
 
     while ((l = ci->cromfs_dirent_cache_head.next) != &ci->cromfs_dirent_cache_head)
     {
@@ -432,12 +484,14 @@ static void cromfs_dirent_cache_destroy(cromfs_info *ci)
 static int dfs_cromfs_mount(struct dfs_filesystem *fs, unsigned long rwflag, const void *data)
 {
     struct rt_device_blk_geometry geometry;
-    uint32_t len;
+    uint32_t len = 0;
     cromfs_info *ci = NULL;
 
     ci = (cromfs_info *)malloc(sizeof *ci);
     if (!ci)
+    {
         return -ENOMEM;
+    }
 
     memset(ci, 0, sizeof *ci);
     ci->device = fs->dev_id;
@@ -474,8 +528,8 @@ static int dfs_cromfs_mount(struct dfs_filesystem *fs, unsigned long rwflag, con
 
 static int dfs_cromfs_unmount(struct dfs_filesystem *fs)
 {
-    rt_err_t result;
-    cromfs_info *ci;
+    rt_err_t result = RT_EOK;
+    cromfs_info *ci = NULL;
 
     ci = (cromfs_info *)fs->data;
 
@@ -490,14 +544,16 @@ static int dfs_cromfs_unmount(struct dfs_filesystem *fs)
     while (ci->cromfs_avl_root)
     {
         struct cromfs_avl_struct *node;
-        file_info *fi;
+        file_info *fi = NULL;
 
         node = ci->cromfs_avl_root;
         fi = node->fi;
         cromfs_avl_remove(node, &ci->cromfs_avl_root);
         free(node);
         if (fi->buff)
+        {
             free(fi->buff);
+        }
         free(fi);
     }
 
@@ -515,13 +571,15 @@ static int dfs_cromfs_ioctl(struct dfs_fd *file, int cmd, void *args)
 
 static uint32_t cromfs_lookup(cromfs_info *ci, const char *path, int* is_dir, uint32_t *size, uint32_t *osize)
 {
-    uint32_t cur_size, cur_pos, cur_osize;
-    const char *subpath, *subpath_end;
-    void *di_mem;
-    int isdir;
+    uint32_t cur_size = 0, cur_pos = 0, cur_osize = 0;
+    const char *subpath = NULL, *subpath_end = NULL;
+    void *di_mem = NULL;
+    int isdir = 0;
 
     if (path[0] == '\0')
+    {
         return CROMFS_POS_ERROR;
+    }
 
     cur_size = ci->part_info.root_dir_size;
     cur_osize = 0;
@@ -531,33 +589,43 @@ static uint32_t cromfs_lookup(cromfs_info *ci, const char *path, int* is_dir, ui
     subpath_end = path;
     while (1)
     {
-        cromfs_dirent_item *di_iter;
-        int found;
+        cromfs_dirent_item *di_iter = NULL;
+        int found = 0;
 
         /* skip /// */
         while (*subpath_end && *subpath_end == '/')
-            subpath_end ++;
+        {
+            subpath_end++;
+        }
         subpath = subpath_end;
         while ((*subpath_end != '/') && *subpath_end)
-            subpath_end ++;
+        {
+            subpath_end++;
+        }
         if (*subpath == '\0')
+        {
             break;
+        }
 
         /* if not dir or empty dir, error */
         if (!isdir || !cur_size)
+        {
             return CROMFS_POS_ERROR;
+        }
 
         /* find subpath */
         di_mem = cromfs_dirent_cache_get(ci, cur_pos, cur_size);
         if (!di_mem)
+        {
             return CROMFS_POS_ERROR;
+        }
 
         found = 0;
         di_iter = (cromfs_dirent_item *)di_mem;
         while (1)
         {
             uint32_t name_len = subpath_end - subpath;
-            uint32_t name_block;
+            uint32_t name_block = 0;
 
             if (di_iter->dirent.name_size == name_len)
             {
@@ -568,19 +636,27 @@ static uint32_t cromfs_lookup(cromfs_info *ci, const char *path, int* is_dir, ui
                     cur_osize = di_iter->dirent.file_origin_size;
                     cur_pos = di_iter->dirent.parition_pos;
                     if (di_iter->dirent.attr == CROMFS_DIRENT_ATTR_DIR)
+                    {
                         isdir = 1;
+                    }
                     else
+                    {
                         isdir = 0;
+                    }
                     break;
                 }
             }
             name_block = (di_iter->dirent.name_size + CROMFS_ALIGN_SIZE_MASK) >> CROMFS_ALIGN_SIZE_BIT;
             di_iter += (1 + name_block);
             if ((uint32_t)di_iter - (uint32_t)di_mem >= cur_size)
+            {
                 break;
+            }
         }
         if (!found)
+        {
             return CROMFS_POS_ERROR;
+        }
     }
     *size = cur_size;
     *osize = cur_osize;
@@ -590,12 +666,14 @@ static uint32_t cromfs_lookup(cromfs_info *ci, const char *path, int* is_dir, ui
 
 static uint32_t dfs_cromfs_lookup(cromfs_info *ci, const char *path, int* is_dir, uint32_t *size, uint32_t *osize)
 {
-    rt_err_t result;
-    uint32_t ret;
+    rt_err_t result = RT_EOK;
+    uint32_t ret = 0;
 
     result =  rt_mutex_take(&ci->lock, RT_WAITING_FOREVER);
     if (result != RT_EOK)
+    {
         return CROMFS_POS_ERROR;
+    }
     ret = cromfs_lookup(ci, path, is_dir, size, osize);
     rt_mutex_release(&ci->lock);
     return ret;
@@ -604,13 +682,13 @@ static uint32_t dfs_cromfs_lookup(cromfs_info *ci, const char *path, int* is_dir
 static int fill_file_data(file_info *fi)
 {
     int ret = -1;
-    cromfs_info *ci;
+    cromfs_info *ci = NULL;
     void *compressed_file_buff = NULL;
-    uint32_t size, osize;
+    uint32_t size = 0, osize = 0;
 
     if (!fi->data_valid)
     {
-        RT_ASSERT(fi->buff != RT_NULL);
+        RT_ASSERT(fi->buff != NULL);
 
         ci = fi->ci;
         osize = fi->size;
@@ -618,36 +696,48 @@ static int fill_file_data(file_info *fi)
 
         compressed_file_buff = (void *)malloc(size);
         if (!compressed_file_buff)
+        {
             goto end;
+        }
         if (cromfs_read_bytes(ci, fi->partition_pos, compressed_file_buff, size) != size)
+        {
             goto end;
+        }
         if (uncompress((uint8_t *)fi->buff, (uLongf *)&osize, (uint8_t *)compressed_file_buff, size) != Z_OK)
+        {
             goto end;
+        }
         fi->data_valid = 1;
     }
     ret = 0;
 end:
     if (compressed_file_buff)
+    {
         free(compressed_file_buff);
+    }
     return ret;
 }
 
 static int dfs_cromfs_read(struct dfs_fd *file, void *buf, size_t count)
 {
-    rt_err_t result;
-    struct dfs_filesystem *fs;
-    file_info *fi;
-    cromfs_info *ci;
-    uint32_t length;
+    rt_err_t result = RT_EOK;
+    struct dfs_filesystem *fs = NULL;
+    file_info *fi = NULL;
+    cromfs_info *ci = NULL;
+    uint32_t length = 0;
 
-    fs = (struct dfs_filesystem *)file->fs;
+    fs = (struct dfs_filesystem *)file->fnode->fs;
     ci = (cromfs_info *)fs->data;
-    fi = (file_info *)file->data;
+    fi = (file_info *)file->fnode->data;
 
-    if (count < file->size - file->pos)
+    if (count < file->fnode->size - file->pos)
+    {
         length = count;
+    }
     else
-        length = file->size - file->pos;
+    {
+        length = file->fnode->size - file->pos;
+    }
 
     if (length > 0)
     {
@@ -655,31 +745,41 @@ static int dfs_cromfs_read(struct dfs_fd *file, void *buf, size_t count)
 
         if (fi->buff)
         {
-            int fill_ret;
+            int fill_ret = 0;
 
             result =  rt_mutex_take(&ci->lock, RT_WAITING_FOREVER);
             if (result != RT_EOK)
+            {
                 return 0;
+            }
             fill_ret = fill_file_data(fi);
             rt_mutex_release(&ci->lock);
             if (fill_ret < 0)
+            {
                 return 0;
+            }
 
             memcpy(buf, fi->buff + file->pos, length);
         }
         else
         {
-            void *di_mem;
+            void *di_mem = NULL;
 
             result =  rt_mutex_take(&ci->lock, RT_WAITING_FOREVER);
             if (result != RT_EOK)
+            {
                 return 0;
+            }
             di_mem = cromfs_dirent_cache_get(ci, fi->partition_pos, fi->size);
             if (di_mem)
+            {
                 memcpy(buf, (char*)di_mem + file->pos, length);
+            }
             rt_mutex_release(&ci->lock);
             if (!di_mem)
+            {
                 return 0;
+            }
         }
         /* update file current position */
         file->pos += length;
@@ -690,12 +790,11 @@ static int dfs_cromfs_read(struct dfs_fd *file, void *buf, size_t count)
 
 static int dfs_cromfs_lseek(struct dfs_fd *file, off_t offset)
 {
-    if (offset <= file->size)
+    if (offset <= file->fnode->size)
     {
         file->pos = offset;
         return file->pos;
     }
-
     return -EIO;
 }
 
@@ -706,7 +805,9 @@ static file_info *get_file_info(cromfs_info *ci, uint32_t partition_pos, int inc
     if (node)
     {
         if (inc_ref)
+        {
             node->fi->ref++;
+        }
         return node->fi;
     }
     return NULL;
@@ -716,11 +817,13 @@ static file_info *inset_file_info(cromfs_info *ci, uint32_t partition_pos, int i
 {
     file_info *fi = NULL;
     void *file_buff = NULL;
-    struct cromfs_avl_struct* node = NULL;
+    struct cromfs_avl_struct *node = NULL;
 
     fi = (file_info *)malloc(sizeof *fi);
     if (!fi)
+    {
         goto err;
+    }
     fi->partition_pos = partition_pos;
     fi->ci = ci;
     if (is_dir)
@@ -736,7 +839,9 @@ static file_info *inset_file_info(cromfs_info *ci, uint32_t partition_pos, int i
         {
             file_buff = (void *)malloc(osize);
             if (!file_buff)
+            {
                 goto err;
+            }
         }
     }
     fi->buff = file_buff;
@@ -744,23 +849,29 @@ static file_info *inset_file_info(cromfs_info *ci, uint32_t partition_pos, int i
 
     node = (struct cromfs_avl_struct *)malloc(sizeof *node);
     if (!node)
+    {
         goto err;
+    }
     node->avl_key = partition_pos;
     node->fi = fi;
     cromfs_avl_insert(node, &ci->cromfs_avl_root);
     return fi;
 err:
     if (file_buff)
+    {
         free(file_buff);
+    }
     if (fi)
+    {
         free(fi);
+    }
     return NULL;
 }
 
 static void deref_file_info(cromfs_info *ci, uint32_t partition_pos)
 {
     struct cromfs_avl_struct* node = cromfs_avl_find(partition_pos, ci->cromfs_avl_root);
-    file_info *fi;
+    file_info *fi = NULL;
 
     if (node)
     {
@@ -771,7 +882,9 @@ static void deref_file_info(cromfs_info *ci, uint32_t partition_pos)
             cromfs_avl_remove(node, &ci->cromfs_avl_root);
             free(node);
             if (fi->buff)
+            {
                 free(fi->buff);
+            }
             free(fi);
         }
     }
@@ -779,42 +892,59 @@ static void deref_file_info(cromfs_info *ci, uint32_t partition_pos)
 
 static int dfs_cromfs_close(struct dfs_fd *file)
 {
-    file_info *fi;
-    struct dfs_filesystem *fs;
-    cromfs_info *ci;
-    rt_err_t result;
+    file_info *fi = NULL;
+    struct dfs_filesystem *fs = NULL;
+    cromfs_info *ci = NULL;
+    rt_err_t result = 0;
+
+    RT_ASSERT(file->fnode->ref_count > 0);
+    if (file->fnode->ref_count > 1)
+    {
+        return 0;
+    }
 
-    fi = (file_info *)file->data;
-    fs = (struct dfs_filesystem *)file->fs;
+    fi = (file_info *)file->fnode->data;
+    fs = (struct dfs_filesystem *)file->fnode->fs;
     ci = (cromfs_info *)fs->data;
 
     result =  rt_mutex_take(&ci->lock, RT_WAITING_FOREVER);
     if (result != RT_EOK)
+    {
         return -RT_ERROR;
+    }
     deref_file_info(ci, fi->partition_pos);
     rt_mutex_release(&ci->lock);
-    file->data = NULL;
+    file->fnode->data = NULL;
     return RT_EOK;
 }
 
 static int dfs_cromfs_open(struct dfs_fd *file)
 {
-    int ret;
-    struct dfs_filesystem *fs;
-    file_info *fi;
-    cromfs_info *ci;
-    uint32_t file_pos;
-    uint32_t size, osize;
-    int is_dir;
-    rt_err_t result;
+    int ret = 0;
+    struct dfs_filesystem *fs = NULL;
+    file_info *fi = NULL;
+    cromfs_info *ci = NULL;
+    uint32_t file_pos = 0;
+    uint32_t size = 0, osize = 0;
+    int is_dir = 0;
+    rt_err_t result = RT_EOK;
 
-    if (file->flags & (O_CREAT | O_WRONLY | O_APPEND | O_TRUNC | O_RDWR))
+    if (file->fnode->flags & (O_CREAT | O_WRONLY | O_APPEND | O_TRUNC | O_RDWR))
+    {
         return -EINVAL;
+    }
+
+    RT_ASSERT(file->fnode->ref_count > 0);
+    if (file->fnode->ref_count > 1)
+    {
+        file->pos = 0;
+        return 0;
+    }
 
-    fs = (struct dfs_filesystem *)file->fs;
+    fs = file->fnode->fs;
     ci = (cromfs_info *)fs->data;
 
-    file_pos = dfs_cromfs_lookup(ci, file->path, &is_dir, &size, &osize);
+    file_pos = dfs_cromfs_lookup(ci, file->fnode->path, &is_dir, &size, &osize);
     if (file_pos == CROMFS_POS_ERROR)
     {
         ret = -ENOENT;
@@ -824,7 +954,7 @@ static int dfs_cromfs_open(struct dfs_fd *file)
     /* entry is a directory file type */
     if (is_dir)
     {
-        if (!(file->flags & O_DIRECTORY))
+        if (!(file->fnode->flags & O_DIRECTORY))
         {
             ret = -ENOENT;
             goto end;
@@ -833,7 +963,7 @@ static int dfs_cromfs_open(struct dfs_fd *file)
     else
     {
         /* entry is a file, but open it as a directory */
-        if (file->flags & O_DIRECTORY)
+        if (file->fnode->flags & O_DIRECTORY)
         {
             ret = -ENOENT;
             goto end;
@@ -859,11 +989,15 @@ static int dfs_cromfs_open(struct dfs_fd *file)
         goto end;
     }
 
-    file->data = fi;
+    file->fnode->data = fi;
     if (is_dir)
-        file->size = size;
+    {
+        file->fnode->size = size;
+    }
     else
-        file->size = osize;
+    {
+        file->fnode->size = osize;
+    }
     file->pos = 0;
 
     ret = RT_EOK;
@@ -873,16 +1007,18 @@ end:
 
 static int dfs_cromfs_stat(struct dfs_filesystem *fs, const char *path, struct stat *st)
 {
-    uint32_t size, osize;
-    int is_dir;
-    cromfs_info *ci;
-    uint32_t file_pos;
+    uint32_t size = 0, osize = 0;
+    int is_dir = 0;
+    cromfs_info *ci = NULL;
+    uint32_t file_pos = 0;
 
     ci = (cromfs_info *)fs->data;
 
     file_pos = dfs_cromfs_lookup(ci, path, &is_dir, &size, &osize);
     if (file_pos == CROMFS_POS_ERROR)
+    {
         return -ENOENT;
+    }
 
     st->st_dev = 0;
     st->st_mode = S_IFREG | S_IRUSR | S_IRGRP | S_IROTH |
@@ -906,26 +1042,30 @@ static int dfs_cromfs_stat(struct dfs_filesystem *fs, const char *path, struct s
 
 static int dfs_cromfs_getdents(struct dfs_fd *file, struct dirent *dirp, uint32_t count)
 {
-    uint32_t index;
-    uint8_t *name;
-    struct dirent *d;
-    file_info *fi;
-    cromfs_info *ci;
-    cromfs_dirent_item *dirent, *sub_dirent;
-    void *di_mem;
-    rt_err_t result;
+    uint32_t index = 0;
+    uint8_t *name = NULL;
+    struct dirent *d = NULL;
+    file_info *fi = NULL;
+    cromfs_info *ci = NULL;
+    cromfs_dirent_item *dirent = NULL, *sub_dirent = NULL;
+    void *di_mem = NULL;
+    rt_err_t result = RT_EOK;
 
-    fi = (file_info *)file->data;
+    fi = (file_info *)file->fnode->data;
     ci = fi->ci;
 
     RT_ASSERT(fi->buff == NULL);
 
     if (!fi->size)
+    {
         return -EINVAL;
+    }
 
     dirent = (cromfs_dirent_item *)malloc(fi->size);
     if (!dirent)
+    {
         return -ENOMEM;
+    }
 
     result =  rt_mutex_take(&ci->lock, RT_WAITING_FOREVER);
     if (result != RT_EOK)
@@ -935,7 +1075,9 @@ static int dfs_cromfs_getdents(struct dfs_fd *file, struct dirent *dirp, uint32_
     }
     di_mem = cromfs_dirent_cache_get(ci, fi->partition_pos, fi->size);
     if (di_mem)
+    {
         memcpy(dirent, di_mem, fi->size);
+    }
     rt_mutex_release(&ci->lock);
     if (!di_mem)
     {
@@ -951,9 +1093,9 @@ static int dfs_cromfs_getdents(struct dfs_fd *file, struct dirent *dirp, uint32_
         return -EINVAL;
     }
 
-    for (index = 0; index < count && file->pos < file->size; index ++)
+    for (index = 0; index < count && file->pos < file->fnode->size; index++)
     {
-        uint32_t name_size;
+        uint32_t name_size = 0;
 
         d = dirp + index;
         sub_dirent = &dirent[file->pos >> CROMFS_ALIGN_SIZE_BIT];
@@ -961,9 +1103,13 @@ static int dfs_cromfs_getdents(struct dfs_fd *file, struct dirent *dirp, uint32_
 
         /* fill dirent */
         if (sub_dirent->dirent.attr == CROMFS_DIRENT_ATTR_DIR)
+        {
             d->d_type = DT_DIR;
+        }
         else
+        {
             d->d_type = DT_REG;
+        }
 
         d->d_namlen = sub_dirent->dirent.name_size;
         d->d_reclen = (rt_uint16_t)sizeof(struct dirent);

+ 22 - 20
components/dfs/filesystems/devfs/devfs.c

@@ -37,7 +37,7 @@ int dfs_device_fs_ioctl(struct dfs_fd *file, int cmd, void *args)
     RT_ASSERT(file != RT_NULL);
 
     /* get device handler */
-    dev_id = (rt_device_t)file->data;
+    dev_id = (rt_device_t)file->fnode->data;
     RT_ASSERT(dev_id != RT_NULL);
 
     /* close device handler */
@@ -56,7 +56,7 @@ int dfs_device_fs_read(struct dfs_fd *file, void *buf, size_t count)
     RT_ASSERT(file != RT_NULL);
 
     /* get device handler */
-    dev_id = (rt_device_t)file->data;
+    dev_id = (rt_device_t)file->fnode->data;
     RT_ASSERT(dev_id != RT_NULL);
 
     /* read device data */
@@ -74,7 +74,7 @@ int dfs_device_fs_write(struct dfs_fd *file, const void *buf, size_t count)
     RT_ASSERT(file != RT_NULL);
 
     /* get device handler */
-    dev_id = (rt_device_t)file->data;
+    dev_id = (rt_device_t)file->fnode->data;
     RT_ASSERT(dev_id != RT_NULL);
 
     /* read device data */
@@ -91,11 +91,11 @@ int dfs_device_fs_close(struct dfs_fd *file)
 
     RT_ASSERT(file != RT_NULL);
 
-    if (file->type == FT_DIRECTORY)
+    if (file->fnode->type == FT_DIRECTORY)
     {
         struct device_dirent *root_dirent;
 
-        root_dirent = (struct device_dirent *)file->data;
+        root_dirent = (struct device_dirent *)file->fnode->data;
         RT_ASSERT(root_dirent != RT_NULL);
 
         /* release dirent */
@@ -104,14 +104,14 @@ int dfs_device_fs_close(struct dfs_fd *file)
     }
 
     /* get device handler */
-    dev_id = (rt_device_t)file->data;
+    dev_id = (rt_device_t)file->fnode->data;
     RT_ASSERT(dev_id != RT_NULL);
 
     /* close device handler */
     result = rt_device_close(dev_id);
     if (result == RT_EOK)
     {
-        file->data = RT_NULL;
+        file->fnode->data = RT_NULL;
 
         return RT_EOK;
     }
@@ -126,8 +126,8 @@ int dfs_device_fs_open(struct dfs_fd *file)
     rt_base_t level;
 
     /* open root directory */
-    if ((file->path[0] == '/') && (file->path[1] == '\0') &&
-        (file->flags & O_DIRECTORY))
+    if ((file->fnode->path[0] == '/') && (file->fnode->path[1] == '\0') &&
+        (file->fnode->flags & O_DIRECTORY))
     {
         struct rt_object *object;
         struct rt_list_node *node;
@@ -165,29 +165,31 @@ int dfs_device_fs_open(struct dfs_fd *file)
         rt_hw_interrupt_enable(level);
 
         /* set data */
-        file->data = root_dirent;
+        file->fnode->data = root_dirent;
 
         return RT_EOK;
     }
 
-    device = rt_device_find(&file->path[1]);
+    device = rt_device_find(&file->fnode->path[1]);
     if (device == RT_NULL)
+    {
         return -ENODEV;
+    }
 
 #ifdef RT_USING_POSIX
     if (device->fops)
     {
         /* use device fops */
-        file->fops = device->fops;
-        file->data = (void *)device;
+        file->fnode->fops = device->fops;
+        file->fnode->data = (void *)device;
 
         /* use fops */
-        if (file->fops->open)
+        if (file->fnode->fops->open)
         {
-            result = file->fops->open(file);
+            result = file->fnode->fops->open(file);
             if (result == RT_EOK || result == -RT_ENOSYS)
             {
-                file->type = FT_DEVICE;
+                file->fnode->type = FT_DEVICE;
                 return 0;
             }
         }
@@ -198,13 +200,13 @@ int dfs_device_fs_open(struct dfs_fd *file)
         result = rt_device_open(device, RT_DEVICE_OFLAG_RDWR);
         if (result == RT_EOK || result == -RT_ENOSYS)
         {
-            file->data = device;
-            file->type = FT_DEVICE;
+            file->fnode->data = device;
+            file->fnode->type = FT_DEVICE;
             return RT_EOK;
         }
     }
 
-    file->data = RT_NULL;
+    file->fnode->data = RT_NULL;
     /* open device failed. */
     return -EIO;
 }
@@ -264,7 +266,7 @@ int dfs_device_fs_getdents(struct dfs_fd *file, struct dirent *dirp, uint32_t co
     struct dirent *d;
     struct device_dirent *root_dirent;
 
-    root_dirent = (struct device_dirent *)file->data;
+    root_dirent = (struct device_dirent *)file->fnode->data;
     RT_ASSERT(root_dirent != RT_NULL);
 
     /* make integer count */

+ 36 - 22
components/dfs/filesystems/elmfat/dfs_elm.c

@@ -330,7 +330,7 @@ int dfs_elm_open(struct dfs_fd *file)
 
 #if (_VOLUMES > 1)
     int vol;
-    struct dfs_filesystem *fs = (struct dfs_filesystem *)file->data;
+    struct dfs_filesystem *fs = file->fnode->fs;
     extern int elm_get_vol(FATFS * fat);
 
     if (fs == NULL)
@@ -342,18 +342,20 @@ int dfs_elm_open(struct dfs_fd *file)
         return -ENOENT;
     drivers_fn = (char *)rt_malloc(256);
     if (drivers_fn == RT_NULL)
+    {
         return -ENOMEM;
+    }
 
-    rt_snprintf(drivers_fn, 256, "%d:%s", vol, file->path);
+    rt_snprintf(drivers_fn, 256, "%d:%s", vol, file->fnode->path);
 #else
-    drivers_fn = file->path;
+    drivers_fn = file->fnode->path;
 #endif
 
-    if (file->flags & O_DIRECTORY)
+    if (file->fnode->flags & O_DIRECTORY)
     {
         DIR *dir;
 
-        if (file->flags & O_CREAT)
+        if (file->fnode->flags & O_CREAT)
         {
             result = f_mkdir(drivers_fn);
             if (result != FR_OK)
@@ -392,19 +394,29 @@ int dfs_elm_open(struct dfs_fd *file)
     {
         mode = FA_READ;
 
-        if (file->flags & O_WRONLY)
+        if (file->fnode->flags & O_WRONLY)
+        {
             mode |= FA_WRITE;
-        if ((file->flags & O_ACCMODE) & O_RDWR)
+        }
+        if ((file->fnode->flags & O_ACCMODE) & O_RDWR)
+        {
             mode |= FA_WRITE;
+        }
         /* Opens the file, if it is existing. If not, a new file is created. */
-        if (file->flags & O_CREAT)
+        if (file->fnode->flags & O_CREAT)
+        {
             mode |= FA_OPEN_ALWAYS;
+        }
         /* Creates a new file. If the file is existing, it is truncated and overwritten. */
-        if (file->flags & O_TRUNC)
+        if (file->fnode->flags & O_TRUNC)
+        {
             mode |= FA_CREATE_ALWAYS;
+        }
         /* Creates a new file. The function fails if the file is already existing. */
-        if (file->flags & O_EXCL)
+        if (file->fnode->flags & O_EXCL)
+        {
             mode |= FA_CREATE_NEW;
+        }
 
         /* allocate a fd */
         fd = (FIL *)rt_malloc(sizeof(FIL));
@@ -423,10 +435,10 @@ int dfs_elm_open(struct dfs_fd *file)
         if (result == FR_OK)
         {
             file->pos  = fd->fptr;
-            file->size = f_size(fd);
+            file->fnode->size = f_size(fd);
             file->data = fd;
 
-            if (file->flags & O_APPEND)
+            if (file->fnode->flags & O_APPEND)
             {
                 /* seek to the end of file */
                 f_lseek(fd, f_size(fd));
@@ -449,9 +461,9 @@ int dfs_elm_close(struct dfs_fd *file)
     FRESULT result;
 
     result = FR_OK;
-    if (file->type == FT_DIRECTORY)
+    if (file->fnode->type == FT_DIRECTORY)
     {
-        DIR *dir;
+        DIR *dir = RT_NULL;
 
         dir = (DIR *)(file->data);
         RT_ASSERT(dir != RT_NULL);
@@ -459,9 +471,9 @@ int dfs_elm_close(struct dfs_fd *file)
         /* release memory */
         rt_free(dir);
     }
-    else if (file->type == FT_REGULAR)
+    else if (file->fnode->type == FT_REGULAR)
     {
-        FIL *fd;
+        FIL *fd = RT_NULL;
         fd = (FIL *)(file->data);
         RT_ASSERT(fd != RT_NULL);
 
@@ -514,7 +526,7 @@ int dfs_elm_read(struct dfs_fd *file, void *buf, size_t len)
     FRESULT result;
     UINT byte_read;
 
-    if (file->type == FT_DIRECTORY)
+    if (file->fnode->type == FT_DIRECTORY)
     {
         return -EISDIR;
     }
@@ -537,7 +549,7 @@ int dfs_elm_write(struct dfs_fd *file, const void *buf, size_t len)
     FRESULT result;
     UINT byte_write;
 
-    if (file->type == FT_DIRECTORY)
+    if (file->fnode->type == FT_DIRECTORY)
     {
         return -EISDIR;
     }
@@ -548,9 +560,11 @@ int dfs_elm_write(struct dfs_fd *file, const void *buf, size_t len)
     result = f_write(fd, buf, len, &byte_write);
     /* update position and file size */
     file->pos  = fd->fptr;
-    file->size = f_size(fd);
+    file->fnode->size = f_size(fd);
     if (result == FR_OK)
+    {
         return byte_write;
+    }
 
     return elm_result_to_dfs(result);
 }
@@ -570,7 +584,7 @@ int dfs_elm_flush(struct dfs_fd *file)
 int dfs_elm_lseek(struct dfs_fd *file, off_t offset)
 {
     FRESULT result = FR_OK;
-    if (file->type == FT_REGULAR)
+    if (file->fnode->type == FT_REGULAR)
     {
         FIL *fd;
 
@@ -586,10 +600,10 @@ int dfs_elm_lseek(struct dfs_fd *file, off_t offset)
             return fd->fptr;
         }
     }
-    else if (file->type == FT_DIRECTORY)
+    else if (file->fnode->type == FT_DIRECTORY)
     {
         /* which is a directory */
-        DIR *dir;
+        DIR *dir = RT_NULL;
 
         dir = (DIR *)(file->data);
         RT_ASSERT(dir != RT_NULL);

+ 85 - 27
components/dfs/filesystems/jffs2/dfs_jffs2.c

@@ -102,14 +102,20 @@ static int dfs_jffs2_mount(struct dfs_filesystem* fs,
     for (index = 0; index < DEVICE_PART_MAX; index ++)
     {
         if (device_partition[index].dev == RT_NULL)
+        {
             break;
+        }
     }
     if (index == DEVICE_PART_MAX)
+    {
         return -ENOSPC;
+    }
 
     mte = rt_malloc(sizeof(struct cyg_mtab_entry));
     if (mte == RT_NULL)
+    {
         return -ENOMEM;
+    }
 
     mte->name = fs->path;
     mte->fsname = "jffs2";
@@ -192,7 +198,9 @@ static int dfs_jffs2_statfs(struct dfs_filesystem* fs,
 
     result = _find_fs(&mte, fs->dev_id);
     if (result)
+    {
         return -ENOENT;
+    }
 
     RT_ASSERT(mte->data != 0);
 
@@ -215,20 +223,26 @@ static int dfs_jffs2_open(struct dfs_fd* file)
     struct dfs_filesystem *fs;
     struct cyg_mtab_entry * mte;
 
-    oflag = file->flags;
-    fs = (struct dfs_filesystem *)file->data;
+    oflag = file->fnode->flags;
+    fs = file->fnode->fs;
     RT_ASSERT(fs != RT_NULL);
 
     jffs2_file = rt_malloc(sizeof(cyg_file));
     if (jffs2_file == RT_NULL)
+    {
         return -ENOMEM;
+    }
 
     /* just escape '/' provided by dfs code */
-    name = file->path;
+    name = file->fnode->path;
     if ((name[0] == '/') && (name[1] == 0))
+    {
         name = jffs2_root_path;
+    }
     else /* name[0] still will be '/' */
-        name ++;
+    {
+        name++;
+    }
 
     result = _find_fs(&mte, fs->dev_id);
     if (result)
@@ -267,7 +281,7 @@ static int dfs_jffs2_open(struct dfs_fd* file)
         jffs2_file->f_offset = 2;
 #endif
         /* save this pointer, it will be used by dfs_jffs2_getdents*/
-        file->data = jffs2_file;
+        file->fnode->data = jffs2_file;
         return 0;
     }
     /* regular file operations */
@@ -292,17 +306,17 @@ static int dfs_jffs2_open(struct dfs_fd* file)
 
     /* save this pointer, it will be used when calling read(), write(),
     flush(), lessk(), and will be rt_free when calling close()*/
-    file->data = jffs2_file;
+    file->fnode->data = jffs2_file;
     file->pos = jffs2_file->f_offset;
-    file->size = 0;
-    jffs2_file_lseek(jffs2_file, (off_t *)(&(file->size)), SEEK_END);
+    file->fnode->size = 0;
+    jffs2_file_lseek(jffs2_file, (off_t *)(&(file->fnode->size)), SEEK_END);
     jffs2_file->f_offset = (off_t)file->pos;
     rt_mutex_release(&jffs2_lock);
 
     if (oflag & O_APPEND)
     {
-        file->pos = file->size;
-        jffs2_file->f_offset = file->size;
+        file->pos = file->fnode->size;
+        jffs2_file->f_offset = file->fnode->size;
     }
 
     return 0;
@@ -313,16 +327,18 @@ static int dfs_jffs2_close(struct dfs_fd* file)
     int result;
     cyg_file * jffs2_file;
 
-    RT_ASSERT(file->data != NULL);
-    jffs2_file = (cyg_file *)(file->data);
+    RT_ASSERT(file->fnode->data != NULL);
+    jffs2_file = (cyg_file *)(file->fnode->data);
 
-    if (file->flags & O_DIRECTORY) /* operations about dir */
+    if (file->fnode->flags & O_DIRECTORY) /* operations about dir */
     {
         rt_mutex_take(&jffs2_lock, RT_WAITING_FOREVER);
         result = jffs2_dir_colse(jffs2_file);
         rt_mutex_release(&jffs2_lock);
         if (result)
+        {
             return jffs2_result_to_dfs(result);
+        }
 
         rt_free(jffs2_file);
         return 0;
@@ -332,7 +348,9 @@ static int dfs_jffs2_close(struct dfs_fd* file)
     result = jffs2_file_colse(jffs2_file);
     rt_mutex_release(&jffs2_lock);
     if (result)
+    {
         return jffs2_result_to_dfs(result);
+    }
 
     /* release memory */
     rt_free(jffs2_file);
@@ -352,8 +370,8 @@ static int dfs_jffs2_read(struct dfs_fd* file, void* buf, size_t len)
     int char_read;
     int result;
 
-    RT_ASSERT(file->data != NULL);
-    jffs2_file = (cyg_file *)(file->data);
+    RT_ASSERT(file->fnode->data != NULL);
+    jffs2_file = (cyg_file *)(file->fnode->data);
     uio_s.uio_iov = &iovec;
     uio_s.uio_iov->iov_base = buf;
     uio_s.uio_iov->iov_len = len;
@@ -366,7 +384,9 @@ static int dfs_jffs2_read(struct dfs_fd* file, void* buf, size_t len)
     result = jffs2_file_read(jffs2_file, &uio_s);
     rt_mutex_release(&jffs2_lock);
     if (result)
+    {
         return jffs2_result_to_dfs(result);
+    }
 
     /* update position */
     file->pos = jffs2_file->f_offset;
@@ -384,8 +404,8 @@ static int dfs_jffs2_write(struct dfs_fd* file,
     int char_write;
     int result;
 
-    RT_ASSERT(file->data != NULL);
-    jffs2_file = (cyg_file *)(file->data);
+    RT_ASSERT(file->fnode->data != NULL);
+    jffs2_file = (cyg_file *)(file->fnode->data);
     uio_s.uio_iov = &iovec;
     uio_s.uio_iov->iov_base = (void *)buf;
     uio_s.uio_iov->iov_len = len;
@@ -398,7 +418,9 @@ static int dfs_jffs2_write(struct dfs_fd* file,
     result = jffs2_file_write(jffs2_file, &uio_s);
     rt_mutex_release(&jffs2_lock);
     if (result)
+    {
         return jffs2_result_to_dfs(result);
+    }
 
     /* update position */
     file->pos = jffs2_file->f_offset;
@@ -414,20 +436,22 @@ static int dfs_jffs2_flush(struct dfs_fd* file)
 
 /* fixme warning: the offset is rt_off_t, so maybe the size of a file is must <= 2G*/
 static int dfs_jffs2_lseek(struct dfs_fd* file,
-                    rt_off_t offset)
+                    off_t offset)
 {
     cyg_file * jffs2_file;
     int result;
 
-    RT_ASSERT(file->data != NULL);
-    jffs2_file = (cyg_file *)(file->data);
+    RT_ASSERT(file->fnode->data != NULL);
+    jffs2_file = (cyg_file *)(file->fnode->data);
 
     /* set offset as current offset */
     rt_mutex_take(&jffs2_lock, RT_WAITING_FOREVER);
     result = jffs2_file_lseek(jffs2_file, &offset, SEEK_SET);
     rt_mutex_release(&jffs2_lock);
     if (result)
+    {
         return jffs2_result_to_dfs(result);
+    }
     /* update file position */
     file->pos = offset;
     return offset;
@@ -451,8 +475,8 @@ static int dfs_jffs2_getdents(struct dfs_fd* file,
 #endif
     int result;
 
-    RT_ASSERT(file->data != RT_NULL);
-    jffs2_file = (cyg_file*)(file->data);
+    RT_ASSERT(file->fnode->data != RT_NULL);
+    jffs2_file = (cyg_file*)(file->fnode->data);
     mte = jffs2_file->f_mte;
 
     //set jffs2_d
@@ -479,7 +503,9 @@ static int dfs_jffs2_getdents(struct dfs_fd* file,
         rt_mutex_release(&jffs2_lock);
         /* if met a error or all entry are read over, break while*/
         if (result || jffs2_d.d_name[0] == 0)
+        {
             break;
+        }
 
 #if defined (CYGPKG_FS_JFFS2_RET_DIRENT_DTYPE)
         switch(jffs2_d.d_type & JFFS2_S_IFMT)
@@ -494,20 +520,28 @@ static int dfs_jffs2_getdents(struct dfs_fd* file,
                 return -ENOMEM;
 
         /* make a right entry */
-        if ((file->path[0] == '/') )
+        if (file->fnode->path[0] == '/')
         {
-            if (file->path[1] == 0)
+            if (file->fnode->path[1] == 0)
+            {
                 strcpy(fullname, jffs2_d.d_name);
+            }
             else
-                rt_sprintf(fullname, "%s/%s", file->path+1, jffs2_d.d_name);
+            {
+                rt_sprintf(fullname, "%s/%s", file->fnode->path+1, jffs2_d.d_name);
+            }
         }
         else
-            rt_sprintf(fullname, "%s/%s", file->path, jffs2_d.d_name);
+        {
+            rt_sprintf(fullname, "%s/%s", file->fnode->path, jffs2_d.d_name);
+        }
         rt_mutex_take(&jffs2_lock, RT_WAITING_FOREVER);
         result = jffs2_porting_stat(mte, mte->root, fullname, (void *)&s);
         rt_mutex_release(&jffs2_lock);
         if (result)
+        {
             return jffs2_result_to_dfs(result);
+        }
 
         rt_free(fullname);
         /* convert to dfs stat structure */
@@ -523,12 +557,16 @@ static int dfs_jffs2_getdents(struct dfs_fd* file,
         d->d_reclen = (rt_uint16_t)sizeof(struct dirent);
         rt_strncpy(d->d_name, jffs2_d.d_name, d->d_namlen + 1);
 
-        index ++;
+        index++;
         if (index * sizeof(struct dirent) >= count)
+        {
             break;
+        }
     }
     if (result)
+    {
         return jffs2_result_to_dfs(result);
+    }
     return index * sizeof(struct dirent);
 }
 
@@ -540,11 +578,15 @@ static int dfs_jffs2_unlink(struct dfs_filesystem* fs, const char* path)
 
     result = _find_fs(&mte, fs->dev_id);
     if (result)
+    {
         return -ENOENT;
+    }
 
     /* deal path */
     if (path[0] == '/')
+    {
         path++;
+    }
 
     /* judge file type, dir is to be delete by rmdir, others by unlink */
     rt_mutex_take(&jffs2_lock, RT_WAITING_FOREVER);
@@ -570,7 +612,9 @@ static int dfs_jffs2_unlink(struct dfs_filesystem* fs, const char* path)
     }
     rt_mutex_release(&jffs2_lock);
     if (result)
+    {
         return jffs2_result_to_dfs(result);
+    }
     return 0;
 }
 
@@ -583,17 +627,25 @@ static int dfs_jffs2_rename(struct dfs_filesystem* fs,
 
     result = _find_fs(&mte, fs->dev_id);
     if (result)
+    {
         return -ENOENT;
+    }
 
     if (*oldpath == '/')
+    {
         oldpath += 1;
+    }
     if (*newpath == '/')
+    {
         newpath += 1;
+    }
     rt_mutex_take(&jffs2_lock, RT_WAITING_FOREVER);
     result = jffs2_rename(mte, mte->root, oldpath, mte->root, newpath);
     rt_mutex_release(&jffs2_lock);
     if (result)
+    {
         return jffs2_result_to_dfs(result);
+    }
     return 0;
 }
 
@@ -607,18 +659,24 @@ static int dfs_jffs2_stat(struct dfs_filesystem* fs, const char *path, struct st
     RT_ASSERT(!((path[0] == '/') && (path[1] == 0)));
 
     if (path[0] == '/')
+    {
         path++;
+    }
 
     result = _find_fs(&mte, fs->dev_id);
     if (result)
+    {
         return -ENOENT;
+    }
 
     rt_mutex_take(&jffs2_lock, RT_WAITING_FOREVER);
     result = jffs2_porting_stat(mte, mte->root, path, (void *)&s);
     rt_mutex_release(&jffs2_lock);
 
     if (result)
+    {
         return jffs2_result_to_dfs(result);
+    }
     /* convert to dfs stat structure */
     switch(s.st_mode & JFFS2_S_IFMT)
     {

+ 4 - 0
components/dfs/filesystems/jffs2/include/port/sys/stat.h

@@ -82,11 +82,15 @@ typedef unsigned int mode_t;
 #endif
 */
 
+/*
 typedef unsigned short nlink_t;
 typedef long off_t;
+*/
 
+/*
 typedef unsigned short gid_t;
 typedef unsigned short uid_t;
+*/
 typedef int pid_t;
 
 //

+ 3 - 3
components/dfs/filesystems/jffs2/porting.h

@@ -22,9 +22,9 @@ struct jffs2_stat {
     unsigned short  st_uid;      /* User ID of the file owner */
     unsigned short  st_gid;      /* Group ID of the file's group */
     long  st_size;     /* File size (regular files only) */
-    long  st_atime;    /* Last access time */
-    long  st_mtime;    /* Last data modification time */
-    long  st_ctime;    /* Last file status change time */
+    struct timespec  st_atim;    /* Last access time */
+    struct timespec  st_mtim;    /* Last data modification time */
+    struct timespec  st_ctim;    /* Last file status change time */
 };
 
 struct jffs2_dirent

+ 28 - 31
components/dfs/filesystems/nfs/dfs_nfs.c

@@ -559,12 +559,11 @@ int nfs_read(struct dfs_fd *file, void *buf, size_t count)
     nfs_file *fd;
     nfs_filesystem *nfs;
 
-    if (file->type == FT_DIRECTORY)
+    if (file->fnode->type == FT_DIRECTORY)
         return -EISDIR;
 
-
-    RT_ASSERT(file->data != NULL);
-    struct dfs_filesystem *dfs_nfs  = ((struct dfs_filesystem *)(file->data));
+    RT_ASSERT(file->fnode->data != NULL);
+    struct dfs_filesystem *dfs_nfs  = ((struct dfs_filesystem *)(file->fnode->data));
     nfs = (struct nfs_filesystem *)(dfs_nfs->data);
     fd = (nfs_file *)(nfs->data);
     RT_ASSERT(fd != NULL);
@@ -629,11 +628,11 @@ int nfs_write(struct dfs_fd *file, const void *buf, size_t count)
     nfs_file *fd;
     nfs_filesystem *nfs;
 
-    if (file->type == FT_DIRECTORY)
+    if (file->fnode->type == FT_DIRECTORY)
         return -EISDIR;
 
-    RT_ASSERT(file->data != NULL);
-    struct dfs_filesystem *dfs_nfs  = ((struct dfs_filesystem *)(file->data));
+    RT_ASSERT(file->fnode->data != NULL);
+    struct dfs_filesystem *dfs_nfs  = ((struct dfs_filesystem *)(file->fnode->data));
     nfs = (struct nfs_filesystem *)(dfs_nfs->data);
     fd = (nfs_file *)(nfs->data);
     RT_ASSERT(fd != NULL);
@@ -676,11 +675,10 @@ int nfs_write(struct dfs_fd *file, const void *buf, size_t count)
             file->pos = fd->offset;
             /* update file size */
             if (fd->size < fd->offset) fd->size = fd->offset;
-            file->size = fd->size;
+            file->fnode->size = fd->size;
         }
         xdr_free((xdrproc_t)xdr_WRITE3res, (char *)&res);
-    }
-    while (count > 0);
+    } while (count > 0);
 
     xdr_free((xdrproc_t)xdr_WRITE3res, (char *)&res);
 
@@ -692,11 +690,11 @@ int nfs_lseek(struct dfs_fd *file, off_t offset)
     nfs_file *fd;
     nfs_filesystem *nfs;
 
-    if (file->type == FT_DIRECTORY)
+    if (file->fnode->type == FT_DIRECTORY)
         return -EISDIR;
 
-    RT_ASSERT(file->data != NULL);
-    struct dfs_filesystem *dfs_nfs  = ((struct dfs_filesystem *)(file->data));
+    RT_ASSERT(file->fnode->data != NULL);
+    struct dfs_filesystem *dfs_nfs  = ((struct dfs_filesystem *)(file->fnode->data));
     nfs = (struct nfs_filesystem *)(dfs_nfs->data);
     fd = (nfs_file *)(nfs->data);
     RT_ASSERT(fd != NULL);
@@ -714,11 +712,11 @@ int nfs_lseek(struct dfs_fd *file, off_t offset)
 int nfs_close(struct dfs_fd *file)
 {
     nfs_filesystem *nfs;
-    RT_ASSERT(file->data != NULL);
-    struct dfs_filesystem *dfs_nfs  = ((struct dfs_filesystem *)(file->data));
+    RT_ASSERT(file->fnode->data != NULL);
+    struct dfs_filesystem *dfs_nfs  = ((struct dfs_filesystem *)(file->fnode->data));
     nfs = (struct nfs_filesystem *)(dfs_nfs->data);
 
-    if (file->type == FT_DIRECTORY)
+    if (file->fnode->type == FT_DIRECTORY)
     {
         struct nfs_dir *dir;
 
@@ -727,7 +725,7 @@ int nfs_close(struct dfs_fd *file)
         xdr_free((xdrproc_t)xdr_READDIR3res, (char *)&dir->res);
         rt_free(dir);
     }
-    else if (file->type == FT_REGULAR)
+    else if (file->fnode->type == FT_REGULAR)
     {
         struct nfs_file *fd;
 
@@ -744,23 +742,23 @@ int nfs_close(struct dfs_fd *file)
 int nfs_open(struct dfs_fd *file)
 {
     nfs_filesystem *nfs;
-    RT_ASSERT(file->data != NULL);
-    struct dfs_filesystem *dfs_nfs  = ((struct dfs_filesystem *)(file->data));
+    RT_ASSERT(file->fnode->data != NULL);
+    struct dfs_filesystem *dfs_nfs  = file->fnode->fs;
     nfs = (struct nfs_filesystem *)(dfs_nfs->data);
     RT_ASSERT(nfs != NULL);
 
-    if (file->flags & O_DIRECTORY)
+    if (file->fnode->flags & O_DIRECTORY)
     {
         nfs_dir *dir;
 
-        if (file->flags & O_CREAT)
+        if (file->fnode->flags & O_CREAT)
         {
-            if (nfs_mkdir(nfs, file->path, 0755) < 0)
+            if (nfs_mkdir(nfs, file->fnode->path, 0755) < 0)
                 return -EAGAIN;
         }
 
         /* open directory */
-        dir = nfs_opendir(nfs, file->path);
+        dir = nfs_opendir(nfs, file->fnode->path);
         if (dir == NULL) return -ENOENT;
         nfs->data = dir;
     }
@@ -770,9 +768,9 @@ int nfs_open(struct dfs_fd *file)
         nfs_fh3 *handle;
 
         /* create file */
-        if (file->flags & O_CREAT)
+        if (file->fnode->flags & O_CREAT)
         {
-            if (nfs_create(nfs, file->path, 0664) < 0)
+            if (nfs_create(nfs, file->fnode->path, 0664) < 0)
                 return -EAGAIN;
         }
 
@@ -781,7 +779,7 @@ int nfs_open(struct dfs_fd *file)
         if (fp == NULL)
             return -ENOMEM;
 
-        handle = get_handle(nfs, file->path);
+        handle = get_handle(nfs, file->fnode->path);
         if (handle == NULL)
         {
             rt_free(fp);
@@ -798,14 +796,14 @@ int nfs_open(struct dfs_fd *file)
         xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle);
         rt_free(handle);
 
-        if (file->flags & O_APPEND)
+        if (file->fnode->flags & O_APPEND)
         {
             fp->offset = fp->size;
         }
 
         /* set private file */
         nfs->data = fp;
-        file->size = fp->size;
+        file->fnode->size = fp->size;
     }
 
     return 0;
@@ -1085,9 +1083,8 @@ int nfs_getdents(struct dfs_fd *file, struct dirent *dirp, uint32_t count)
     nfs_filesystem *nfs;
     char *name;
 
-
-    RT_ASSERT(file->data != NULL);
-    struct dfs_filesystem *dfs_nfs  = ((struct dfs_filesystem *)(file->data));
+    RT_ASSERT(file->fnode->data != NULL);
+    struct dfs_filesystem *dfs_nfs  = ((struct dfs_filesystem *)(file->fnode->data));
     nfs = (struct nfs_filesystem *)(dfs_nfs->data);
     dir = (nfs_dir *)(nfs->data);
     RT_ASSERT(dir != NULL);

+ 23 - 23
components/dfs/filesystems/ramfs/dfs_ramfs.c

@@ -97,13 +97,13 @@ int dfs_ramfs_read(struct dfs_fd *file, void *buf, size_t count)
     rt_size_t length;
     struct ramfs_dirent *dirent;
 
-    dirent = (struct ramfs_dirent *)file->data;
+    dirent = (struct ramfs_dirent *)file->fnode->data;
     RT_ASSERT(dirent != NULL);
 
-    if (count < file->size - file->pos)
+    if (count < file->fnode->size - file->pos)
         length = count;
     else
-        length = file->size - file->pos;
+        length = file->fnode->size - file->pos;
 
     if (length > 0)
         memcpy(buf, &(dirent->data[file->pos]), length);
@@ -119,13 +119,13 @@ int dfs_ramfs_write(struct dfs_fd *fd, const void *buf, size_t count)
     struct ramfs_dirent *dirent;
     struct dfs_ramfs *ramfs;
 
-    dirent = (struct ramfs_dirent *)fd->data;
+    dirent = (struct ramfs_dirent *)fd->fnode->data;
     RT_ASSERT(dirent != NULL);
 
     ramfs = dirent->fs;
     RT_ASSERT(ramfs != NULL);
 
-    if (count + fd->pos > fd->size)
+    if (count + fd->pos > fd->fnode->size)
     {
         rt_uint8_t *ptr;
         ptr = rt_memheap_realloc(&(ramfs->memheap), dirent->data, fd->pos + count);
@@ -139,7 +139,7 @@ int dfs_ramfs_write(struct dfs_fd *fd, const void *buf, size_t count)
         /* update dirent and file size */
         dirent->data = ptr;
         dirent->size = fd->pos + count;
-        fd->size = dirent->size;
+        fd->fnode->size = dirent->size;
     }
 
     if (count > 0)
@@ -153,7 +153,7 @@ int dfs_ramfs_write(struct dfs_fd *fd, const void *buf, size_t count)
 
 int dfs_ramfs_lseek(struct dfs_fd *file, off_t offset)
 {
-    if (offset <= (off_t)file->size)
+    if (offset <= (off_t)file->fnode->size)
     {
         file->pos = offset;
 
@@ -165,7 +165,7 @@ int dfs_ramfs_lseek(struct dfs_fd *file, off_t offset)
 
 int dfs_ramfs_close(struct dfs_fd *file)
 {
-    file->data = NULL;
+    file->fnode->data = NULL;
 
     return RT_EOK;
 }
@@ -177,25 +177,25 @@ int dfs_ramfs_open(struct dfs_fd *file)
     struct ramfs_dirent *dirent;
     struct dfs_filesystem *fs;
 
-    fs = (struct dfs_filesystem *)file->data;
+    fs = file->fnode->fs;
 
     ramfs = (struct dfs_ramfs *)fs->data;
     RT_ASSERT(ramfs != NULL);
 
-    if (file->flags & O_DIRECTORY)
+    if (file->fnode->flags & O_DIRECTORY)
     {
-        if (file->flags & O_CREAT)
+        if (file->fnode->flags & O_CREAT)
         {
             return -ENOSPC;
         }
 
         /* open directory */
-        dirent = dfs_ramfs_lookup(ramfs, file->path, &size);
+        dirent = dfs_ramfs_lookup(ramfs, file->fnode->path, &size);
         if (dirent == NULL)
             return -ENOENT;
         if (dirent == &(ramfs->root)) /* it's root directory */
         {
-            if (!(file->flags & O_DIRECTORY))
+            if (!(file->fnode->flags & O_DIRECTORY))
             {
                 return -ENOENT;
             }
@@ -203,7 +203,7 @@ int dfs_ramfs_open(struct dfs_fd *file)
     }
     else
     {
-        dirent = dfs_ramfs_lookup(ramfs, file->path, &size);
+        dirent = dfs_ramfs_lookup(ramfs, file->fnode->path, &size);
         if (dirent == &(ramfs->root)) /* it's root directory */
         {
             return -ENOENT;
@@ -211,7 +211,7 @@ int dfs_ramfs_open(struct dfs_fd *file)
 
         if (dirent == NULL)
         {
-            if (file->flags & O_CREAT || file->flags & O_WRONLY)
+            if (file->fnode->flags & O_CREAT || file->fnode->flags & O_WRONLY)
             {
                 char *name_ptr;
 
@@ -225,9 +225,9 @@ int dfs_ramfs_open(struct dfs_fd *file)
                 }
 
                 /* remove '/' separator */
-                name_ptr = file->path;
+                name_ptr = file->fnode->path;
                 while (*name_ptr == '/' && *name_ptr)
-                    name_ptr ++;
+                    name_ptr++;
                 strncpy(dirent->name, name_ptr, RAMFS_NAME_MAX);
 
                 rt_list_init(&(dirent->list));
@@ -245,7 +245,7 @@ int dfs_ramfs_open(struct dfs_fd *file)
         /* Creates a new file.
          * If the file is existing, it is truncated and overwritten.
          */
-        if (file->flags & O_TRUNC)
+        if (file->fnode->flags & O_TRUNC)
         {
             dirent->size = 0;
             if (dirent->data != NULL)
@@ -256,10 +256,10 @@ int dfs_ramfs_open(struct dfs_fd *file)
         }
     }
 
-    file->data = dirent;
-    file->size = dirent->size;
-    if (file->flags & O_APPEND)
-        file->pos = file->size;
+    file->fnode->data = dirent;
+    file->fnode->size = dirent->size;
+    if (file->fnode->flags & O_APPEND)
+        file->pos = file->fnode->size;
     else
         file->pos = 0;
 
@@ -299,7 +299,7 @@ int dfs_ramfs_getdents(struct dfs_fd *file,
     struct ramfs_dirent *dirent;
     struct dfs_ramfs *ramfs;
 
-    dirent = (struct ramfs_dirent *)file->data;
+    dirent = (struct ramfs_dirent *)file->fnode->data;
 
     ramfs  = dirent->fs;
     RT_ASSERT(ramfs != RT_NULL);

+ 14 - 17
components/dfs/filesystems/romfs/dfs_romfs.c

@@ -112,9 +112,6 @@ struct romfs_dirent *dfs_romfs_lookup(struct romfs_dirent *root_dirent, const ch
                 else
                 {
                     /* return file dirent */
-                    if (subpath != NULL)
-                        break; /* not the end of path */
-
                     return &dirent[index];
                 }
             }
@@ -133,7 +130,7 @@ int dfs_romfs_read(struct dfs_fd *file, void *buf, size_t count)
     rt_size_t length;
     struct romfs_dirent *dirent;
 
-    dirent = (struct romfs_dirent *)file->data;
+    dirent = (struct romfs_dirent *)file->fnode->data;
     RT_ASSERT(dirent != NULL);
 
     if (check_dirent(dirent) != 0)
@@ -141,10 +138,10 @@ int dfs_romfs_read(struct dfs_fd *file, void *buf, size_t count)
         return -EIO;
     }
 
-    if (count < file->size - file->pos)
+    if (count < file->fnode->size - file->pos)
         length = count;
     else
-        length = file->size - file->pos;
+        length = file->fnode->size - file->pos;
 
     if (length > 0)
         memcpy(buf, &(dirent->data[file->pos]), length);
@@ -157,7 +154,7 @@ int dfs_romfs_read(struct dfs_fd *file, void *buf, size_t count)
 
 int dfs_romfs_lseek(struct dfs_fd *file, off_t offset)
 {
-    if (offset <= file->size)
+    if (offset <= file->fnode->size)
     {
         file->pos = offset;
         return file->pos;
@@ -168,7 +165,7 @@ int dfs_romfs_lseek(struct dfs_fd *file, off_t offset)
 
 int dfs_romfs_close(struct dfs_fd *file)
 {
-    file->data = NULL;
+    file->fnode->data = NULL;
     return RT_EOK;
 }
 
@@ -179,34 +176,34 @@ int dfs_romfs_open(struct dfs_fd *file)
     struct romfs_dirent *root_dirent;
     struct dfs_filesystem *fs;
 
-    fs = (struct dfs_filesystem *)file->data;
+    fs = file->fnode->fs;
     root_dirent = (struct romfs_dirent *)fs->data;
 
     if (check_dirent(root_dirent) != 0)
         return -EIO;
 
-    if (file->flags & (O_CREAT | O_WRONLY | O_APPEND | O_TRUNC | O_RDWR))
+    if (file->fnode->flags & (O_CREAT | O_WRONLY | O_APPEND | O_TRUNC | O_RDWR))
         return -EINVAL;
 
-    dirent = dfs_romfs_lookup(root_dirent, file->path, &size);
+    dirent = dfs_romfs_lookup(root_dirent, file->fnode->path, &size);
     if (dirent == NULL)
         return -ENOENT;
 
     /* entry is a directory file type */
     if (dirent->type == ROMFS_DIRENT_DIR)
     {
-        if (!(file->flags & O_DIRECTORY))
+        if (!(file->fnode->flags & O_DIRECTORY))
             return -ENOENT;
     }
     else
     {
         /* entry is a file, but open it as a directory */
-        if (file->flags & O_DIRECTORY)
+        if (file->fnode->flags & O_DIRECTORY)
             return -ENOENT;
     }
 
-    file->data = dirent;
-    file->size = size;
+    file->fnode->data = dirent;
+    file->fnode->size = size;
     file->pos = 0;
 
     return RT_EOK;
@@ -247,7 +244,7 @@ int dfs_romfs_getdents(struct dfs_fd *file, struct dirent *dirp, uint32_t count)
     struct dirent *d;
     struct romfs_dirent *dirent, *sub_dirent;
 
-    dirent = (struct romfs_dirent *)file->data;
+    dirent = (struct romfs_dirent *)file->fnode->data;
     if (check_dirent(dirent) != 0)
         return -EIO;
     RT_ASSERT(dirent->type == ROMFS_DIRENT_DIR);
@@ -261,7 +258,7 @@ int dfs_romfs_getdents(struct dfs_fd *file, struct dirent *dirp, uint32_t count)
         return -EINVAL;
 
     index = 0;
-    for (index = 0; index < count && file->pos < file->size; index ++)
+    for (index = 0; index < count && file->pos < file->fnode->size; index++)
     {
         d = dirp + index;
 

+ 22 - 22
components/dfs/filesystems/uffs/dfs_uffs.c

@@ -281,14 +281,14 @@ static int dfs_uffs_open(struct dfs_fd *file)
     int oflag, mode;
     char *file_path;
 
-    oflag = file->flags;
+    oflag = file->fnode->flags;
     if (oflag & O_DIRECTORY)   /* operations about dir */
     {
         uffs_DIR *dir;
 
         if (oflag & O_CREAT)   /* create a dir*/
         {
-            if (uffs_mkdir(file->path) < 0)
+            if (uffs_mkdir(file->fnode->path) < 0)
                 return uffs_result_to_dfs(uffs_get_error());
         }
         /* open dir */
@@ -296,8 +296,8 @@ static int dfs_uffs_open(struct dfs_fd *file)
         if (file_path == RT_NULL)
             return -ENOMEM;
 
-        if (file->path[0] == '/' && !(file->path[1] == 0))
-            rt_snprintf(file_path, FILE_PATH_MAX, "%s/", file->path);
+        if (file->fnode->path[0] == '/' && !(file->fnode->path[1] == 0))
+            rt_snprintf(file_path, FILE_PATH_MAX, "%s/", file->fnode->path);
         else
         {
             file_path[0] = '/';
@@ -312,7 +312,7 @@ static int dfs_uffs_open(struct dfs_fd *file)
             return uffs_result_to_dfs(uffs_get_error());
         }
         /* save this pointer,will used by  dfs_uffs_getdents*/
-        file->data = dir;
+        file->fnode->data = dir;
         rt_free(file_path);
         return RT_EOK;
     }
@@ -330,7 +330,7 @@ static int dfs_uffs_open(struct dfs_fd *file)
     /* Creates a new file. The function fails if the file is already existing. */
     if (oflag & O_EXCL) mode |= UO_EXCL;
 
-    fd = uffs_open(file->path, mode);
+    fd = uffs_open(file->fnode->path, mode);
     if (fd < 0)
     {
         return uffs_result_to_dfs(uffs_get_error());
@@ -339,9 +339,9 @@ static int dfs_uffs_open(struct dfs_fd *file)
     /* save this pointer, it will be used when calling read(), write(),
      * flush(), seek(), and will be free when calling close()*/
 
-    file->data = (void *)fd;
+    file->fnode->data = (void *)fd;
     file->pos  = uffs_seek(fd, 0, USEEK_CUR);
-    file->size = uffs_seek(fd, 0, USEEK_END);
+    file->fnode->size = uffs_seek(fd, 0, USEEK_END);
     uffs_seek(fd, file->pos, USEEK_SET);
 
     if (oflag & O_APPEND)
@@ -356,17 +356,17 @@ static int dfs_uffs_close(struct dfs_fd *file)
     int oflag;
     int fd;
 
-    oflag = file->flags;
+    oflag = file->fnode->flags;
     if (oflag & O_DIRECTORY)
     {
         /* operations about dir */
-        if (uffs_closedir((uffs_DIR *)(file->data)) < 0)
+        if (uffs_closedir((uffs_DIR *)(file->fnode->data)) < 0)
             return uffs_result_to_dfs(uffs_get_error());
 
         return 0;
     }
     /* regular file operations */
-    fd = (int)(file->data);
+    fd = (int)(file->fnode->data);
 
     if (uffs_close(fd) == 0)
         return 0;
@@ -384,7 +384,7 @@ static int dfs_uffs_read(struct dfs_fd *file, void *buf, size_t len)
     int fd;
     int char_read;
 
-    fd = (int)(file->data);
+    fd = (int)(file->fnode->data);
     char_read = uffs_read(fd, buf, len);
     if (char_read < 0)
         return uffs_result_to_dfs(uffs_get_error());
@@ -401,7 +401,7 @@ static int dfs_uffs_write(struct dfs_fd *file,
     int fd;
     int char_write;
 
-    fd = (int)(file->data);
+    fd = (int)(file->fnode->data);
 
     char_write = uffs_write(fd, buf, len);
     if (char_write < 0)
@@ -417,7 +417,7 @@ static int dfs_uffs_flush(struct dfs_fd *file)
     int fd;
     int result;
 
-    fd = (int)(file->data);
+    fd = (int)(file->fnode->data);
 
     result = uffs_flush(fd);
     if (result < 0)
@@ -445,19 +445,19 @@ static int dfs_uffs_seek(struct dfs_fd *file,
     int result;
 
     /* set offset as current offset */
-    if (file->type == FT_DIRECTORY)
+    if (file->fnode->type == FT_DIRECTORY)
     {
-        uffs_rewinddir((uffs_DIR *)(file->data));
-        result = uffs_seekdir((uffs_DIR *)(file->data), offset / sizeof(struct dirent));
+        uffs_rewinddir((uffs_DIR *)(file->fnode->data));
+        result = uffs_seekdir((uffs_DIR *)(file->fnode->data), offset / sizeof(struct dirent));
         if (result >= 0)
         {
             file->pos = offset;
             return offset;
         }
     }
-    else if (file->type == FT_REGULAR)
+    else if (file->fnode->type == FT_REGULAR)
     {
-        result = uffs_seek((int)(file->data), offset, USEEK_SET);
+        result = uffs_seek((int)(file->fnode->data), offset, USEEK_SET);
         if (result >= 0)
             return offset;
     }
@@ -477,7 +477,7 @@ static int dfs_uffs_getdents(
     uffs_DIR *dir;
     struct uffs_dirent *uffs_d;
 
-    dir = (uffs_DIR *)(file->data);
+    dir = (uffs_DIR *)(file->fnode->data);
     RT_ASSERT(dir != RT_NULL);
 
     /* round count, count is always 1 */
@@ -504,8 +504,8 @@ static int dfs_uffs_getdents(
             return (uffs_result_to_dfs(uffs_get_error()));
         }
 
-        if (file->path[0] == '/' && !(file->path[1] == 0))
-            rt_snprintf(file_path, FILE_PATH_MAX, "%s/%s", file->path, uffs_d->d_name);
+        if (file->fnode->path[0] == '/' && !(file->fnode->path[1] == 0))
+            rt_snprintf(file_path, FILE_PATH_MAX, "%s/%s", file->fnode->path, uffs_d->d_name);
         else
             rt_strncpy(file_path, uffs_d->d_name, FILE_PATH_MAX);
 

+ 3 - 3
components/dfs/filesystems/uffs/src/inc/uffs/uffs_fd.h

@@ -97,9 +97,9 @@ struct uffs_stat {
     long		st_size;    /* total size, in bytes */
     int			st_blksize; /* blocksize for filesystem I/O */
     int			st_blocks;  /* number of blocks allocated */
-    unsigned int	st_atime;   /* time of last access */
-    unsigned int	st_mtime;   /* time of last modification */
-    unsigned int	st_ctime;   /* time of last status change */
+    struct timespec st_atim;   /* time of last access */
+    struct timespec st_mtim;   /* time of last modification */
+    struct timespec st_ctim;   /* time of last status change */
 };
 
 /* POSIX complaint file system APIs */

+ 7 - 5
components/dfs/include/dfs.h

@@ -31,8 +31,8 @@
 /*
  * skip stdin/stdout/stderr normally
  */
-#ifndef DFS_FD_OFFSET
-#define DFS_FD_OFFSET           3
+#ifndef DFS_STDIO_OFFSET
+#define DFS_STDIO_OFFSET           3
 #endif
 
 #ifndef DFS_PATH_MAX
@@ -103,11 +103,13 @@ void dfs_fd_unlock(void);
 /* FD APIs */
 int fdt_fd_new(struct dfs_fdtable *fdt);
 struct dfs_fd *fdt_fd_get(struct dfs_fdtable* fdt, int fd);
-void fdt_fd_put(struct dfs_fdtable* fdt, struct dfs_fd *fd);
+void fdt_fd_release(struct dfs_fdtable* fdt, int fd);
 int fd_new(void);
+int fd_associate(struct dfs_fdtable *fdt, int fd, struct dfs_fd *file);
 struct dfs_fd *fd_get(int fd);
-void fd_put(struct dfs_fd *fd);
-int fd_is_open(const char *pathname);
+void fd_release(int fd);
+
+void fd_init(struct dfs_fd *fd);
 
 struct dfs_fdtable *dfs_fdtable_get(void);
 struct dfs_fdtable *dfs_fdtable_get_global(void);

+ 15 - 4
components/dfs/include/dfs_file.h

@@ -36,24 +36,35 @@ struct dfs_file_ops
 
 /* file descriptor */
 #define DFS_FD_MAGIC     0xfdfd
-struct dfs_fd
+
+struct dfs_fnode
 {
-    uint16_t magic;              /* file descriptor magic number */
     uint16_t type;               /* Type (regular or socket) */
 
     char *path;                  /* Name (below mount point) */
+    char *fullpath;              /* Full path is hash key */
     int ref_count;               /* Descriptor reference count */
+    rt_list_t list;              /* The node of fnode hash table */
 
     struct dfs_filesystem *fs;
     const struct dfs_file_ops *fops;
 
     uint32_t flags;              /* Descriptor flags */
     size_t   size;               /* Size in bytes */
-    off_t    pos;                /* Current file position */
-
     void *data;                  /* Specific file system data */
 };
 
+struct dfs_fd
+{
+    uint16_t magic;              /* file descriptor magic number */
+    int ref_count;               /* Descriptor reference count */
+    off_t    pos;                /* Current file position */
+    struct dfs_fnode *fnode;     /* file node struct */
+    void *data;                  /* Specific fd data */
+};
+
+void dfs_fnode_mgr_init(void);
+int dfs_file_is_open(const char *pathname);
 int dfs_file_open(struct dfs_fd *fd, const char *path, int flags);
 int dfs_file_close(struct dfs_fd *fd);
 int dfs_file_ioctl(struct dfs_fd *fd, int cmd, void *args);

+ 241 - 130
components/dfs/src/dfs.c

@@ -56,6 +56,9 @@ int dfs_init(void)
         return 0;
     }
 
+    /* init fnode hash table */
+    dfs_fnode_mgr_init();
+
     /* clear filesystem operations table */
     memset((void *)filesystem_operation_table, 0, sizeof(filesystem_operation_table));
     /* clear filesystem table */
@@ -140,51 +143,90 @@ void dfs_fd_unlock(void)
     rt_mutex_release(&fdlock);
 }
 
-static int fd_alloc(struct dfs_fdtable *fdt, int startfd)
+static int fd_slot_expand(struct dfs_fdtable *fdt, int fd)
 {
-    int idx;
+    int nr;
+    int index;
+    struct dfs_fd **fds = NULL;
 
-    /* find an empty fd entry */
-    for (idx = startfd; idx < (int)fdt->maxfd; idx++)
+    if (fd < fdt->maxfd)
     {
-        if (fdt->fds[idx] == RT_NULL)
-            break;
-        if (fdt->fds[idx]->ref_count == 0)
-            break;
+        return fd;
+    }
+    if (fd >= DFS_FD_MAX)
+    {
+        return -1;
+    }
+
+    nr = ((fd + 4) & ~3);
+    if (nr > DFS_FD_MAX)
+    {
+        nr = DFS_FD_MAX;
+    }
+    fds = (struct dfs_fd **)rt_realloc(fdt->fds, nr * sizeof(struct dfs_fd *));
+    if (!fds)
+    {
+        return -1;
     }
 
-    /* allocate a larger FD container */
-    if (idx == fdt->maxfd && fdt->maxfd < DFS_FD_MAX)
+    /* clean the new allocated fds */
+    for (index = fdt->maxfd; index < nr; index++)
     {
-        int cnt, index;
-        struct dfs_fd **fds;
+        fds[index] = NULL;
+    }
+    fdt->fds   = fds;
+    fdt->maxfd = nr;
 
-        /* increase the number of FD with 4 step length */
-        cnt = fdt->maxfd + 4;
-        cnt = cnt > DFS_FD_MAX ? DFS_FD_MAX : cnt;
+    return fd;
+}
 
-        fds = (struct dfs_fd **)rt_realloc(fdt->fds, cnt * sizeof(struct dfs_fd *));
-        if (fds == NULL) goto __exit; /* return fdt->maxfd */
+static int fd_slot_alloc(struct dfs_fdtable *fdt, int startfd)
+{
+    int idx;
 
-        /* clean the new allocated fds */
-        for (index = fdt->maxfd; index < cnt; index ++)
+    /* find an empty fd slot */
+    for (idx = startfd; idx < (int)fdt->maxfd; idx++)
+    {
+        if (fdt->fds[idx] == RT_NULL)
         {
-            fds[index] = NULL;
+            return idx;
         }
+    }
 
-        fdt->fds   = fds;
-        fdt->maxfd = cnt;
+    idx = fdt->maxfd;
+    if (idx < startfd)
+    {
+        idx = startfd;
+    }
+    if (fd_slot_expand(fdt, idx) < 0)
+    {
+        return -1;
     }
+    return idx;
+}
+
+static int fd_alloc(struct dfs_fdtable *fdt, int startfd)
+{
+    int idx;
+    struct dfs_fd *fd = NULL;
+
+    idx = fd_slot_alloc(fdt, startfd);
 
     /* allocate  'struct dfs_fd' */
-    if (idx < (int)fdt->maxfd && fdt->fds[idx] == RT_NULL)
+    if (idx < 0)
     {
-        fdt->fds[idx] = (struct dfs_fd *)rt_calloc(1, sizeof(struct dfs_fd));
-        if (fdt->fds[idx] == RT_NULL)
-            idx = fdt->maxfd;
+        return -1;
+    }
+    fd = (struct dfs_fd *)rt_calloc(1, sizeof(struct dfs_fd));
+    if (!fd)
+    {
+        return -1;
     }
+    fd->ref_count = 1;
+    fd->magic = DFS_FD_MAGIC;
+    fd->fnode = NULL;
+    fdt->fds[idx] = fd;
 
-__exit:
     return idx;
 }
 
@@ -196,35 +238,27 @@ __exit:
  */
 int fdt_fd_new(struct dfs_fdtable *fdt)
 {
-    struct dfs_fd *d;
     int idx;
 
     /* lock filesystem */
     dfs_fd_lock();
 
     /* find an empty fd entry */
-    idx = fd_alloc(fdt, 0);
+    idx = fd_alloc(fdt, DFS_STDIO_OFFSET);
 
     /* can't find an empty fd entry */
-    if (idx == fdt->maxfd)
+    if (idx < 0)
     {
-        idx = -(1 + DFS_FD_OFFSET);
         LOG_E("DFS fd new is failed! Could not found an empty fd entry.");
-        goto __result;
     }
 
-    d = fdt->fds[idx];
-    d->ref_count = 1;
-    d->magic = DFS_FD_MAGIC;
-
-__result:
     dfs_fd_unlock();
-    return idx + DFS_FD_OFFSET;
+    return idx;
 }
 
 int fd_new(void)
 {
-    struct dfs_fdtable *fdt;
+    struct dfs_fdtable *fdt = NULL;
 
     fdt = dfs_fdtable_get();
     return fdt_fd_new(fdt);
@@ -239,18 +273,15 @@ int fd_new(void)
  * @return NULL on on this file descriptor or the file descriptor structure
  * pointer.
  */
+
 struct dfs_fd *fdt_fd_get(struct dfs_fdtable* fdt, int fd)
 {
     struct dfs_fd *d;
 
-#if defined(RT_USING_DFS_DEVFS) && defined(RT_USING_POSIX)
-    if ((0 <= fd) && (fd <= 2))
-        fd = libc_stdio_get_console();
-#endif
-
-    fd = fd - DFS_FD_OFFSET;
     if (fd < 0 || fd >= (int)fdt->maxfd)
+    {
         return NULL;
+    }
 
     dfs_fd_lock();
     d = fdt->fds[fd];
@@ -262,8 +293,6 @@ struct dfs_fd *fdt_fd_get(struct dfs_fdtable* fdt, int fd)
         return NULL;
     }
 
-    /* increase the reference count */
-    d->ref_count ++;
     dfs_fd_unlock();
 
     return d;
@@ -282,99 +311,181 @@ struct dfs_fd *fd_get(int fd)
  *
  * This function will put the file descriptor.
  */
-void fdt_fd_put(struct dfs_fdtable* fdt, struct dfs_fd *fd)
+void fdt_fd_release(struct dfs_fdtable* fdt, int fd)
 {
-    RT_ASSERT(fd != NULL);
+    struct dfs_fd *fd_slot = NULL;
+
+    RT_ASSERT(fdt != NULL);
 
     dfs_fd_lock();
 
-    fd->ref_count --;
+    if ((fd < 0) || (fd >= fdt->maxfd))
+    {
+        dfs_fd_unlock();
+        return;
+    }
 
-    /* clear this fd entry */
-    if (fd->ref_count == 0)
+    fd_slot = fdt->fds[fd];
+    if (fd_slot == NULL)
     {
-        int index;
+        dfs_fd_unlock();
+        return;
+    }
+    fdt->fds[fd] = NULL;
+
+    /* check fd */
+    RT_ASSERT(fd_slot->magic == DFS_FD_MAGIC);
 
-        for (index = 0; index < (int)fdt->maxfd; index ++)
+    fd_slot->ref_count--;
+
+    /* clear this fd entry */
+    if (fd_slot->ref_count == 0)
+    {
+        struct dfs_fnode *fnode = fd_slot->fnode;
+        if (fnode)
         {
-            if (fdt->fds[index] == fd)
-            {
-                rt_free(fd);
-                fdt->fds[index] = 0;
-                break;
-            }
+            fnode->ref_count--;
         }
+        rt_free(fd_slot);
     }
     dfs_fd_unlock();
 }
 
-void fd_put(struct dfs_fd *fd)
+void fd_release(int fd)
 {
     struct dfs_fdtable *fdt;
 
     fdt = dfs_fdtable_get();
-    fdt_fd_put(fdt, fd);
+    fdt_fd_release(fdt, fd);
 }
 
-/**
- * @ingroup Fd
- *
- * This function will return whether this file has been opend.
- *
- * @param pathname the file path name.
- *
- * @return 0 on file has been open successfully, -1 on open failed.
- */
-int fd_is_open(const char *pathname)
+int sys_dup(int oldfd)
 {
-    char *fullpath;
-    unsigned int index;
-    struct dfs_filesystem *fs;
-    struct dfs_fd *fd;
-    struct dfs_fdtable *fdt;
+    int newfd = -1;
+    struct dfs_fdtable *fdt = NULL;
 
+    dfs_fd_lock();
+    /* check old fd */
     fdt = dfs_fdtable_get();
-    fullpath = dfs_normalize_path(NULL, pathname);
-    if (fullpath != NULL)
+    if ((oldfd < 0) || (oldfd >= fdt->maxfd))
     {
-        char *mountpath;
-        fs = dfs_filesystem_lookup(fullpath);
-        if (fs == NULL)
-        {
-            /* can't find mounted file system */
-            rt_free(fullpath);
+        goto exit;
+    }
+    if (!fdt->fds[oldfd])
+    {
+        goto exit;
+    }
+    /* get a new fd */
+    newfd = fd_slot_alloc(fdt, DFS_STDIO_OFFSET);
+    if (newfd >= 0)
+    {
+        fdt->fds[newfd] = fdt->fds[oldfd];
+        /* inc ref_count */
+        fdt->fds[newfd]->ref_count++;
+    }
+exit:
+    dfs_fd_unlock();
+    return newfd;
+}
 
-            return -1;
+int sys_dup2(int oldfd, int newfd)
+{
+    struct dfs_fdtable *fdt = NULL;
+    int ret = 0;
+    int retfd = -1;
+
+    dfs_fd_lock();
+    /* check old fd */
+    fdt = dfs_fdtable_get();
+    if ((oldfd < 0) || (oldfd >= fdt->maxfd))
+    {
+        goto exit;
+    }
+    if (!fdt->fds[oldfd])
+    {
+        goto exit;
+    }
+    if (newfd < 0)
+    {
+        goto exit;
+    }
+    if (newfd >= fdt->maxfd)
+    {
+        newfd = fd_slot_expand(fdt, newfd);
+        if (newfd < 0)
+        {
+            goto exit;
         }
+    }
+    if (fdt->fds[newfd] == fdt->fds[oldfd])
+    {
+        /* ok, return newfd */
+        retfd = newfd;
+        goto exit;
+    }
 
-        /* get file path name under mounted file system */
-        if (fs->path[0] == '/' && fs->path[1] == '\0')
-            mountpath = fullpath;
-        else
-            mountpath = fullpath + strlen(fs->path);
+    if (fdt->fds[newfd])
+    {
+        ret = dfs_file_close(fdt->fds[newfd]);
+        if (ret < 0)
+        {
+            goto exit;
+        }
+        fd_release(newfd);
+    }
 
-        dfs_fd_lock();
+    fdt->fds[newfd] = fdt->fds[oldfd];
+    /* inc ref_count */
+    fdt->fds[newfd]->ref_count++;
+    retfd = newfd;
+exit:
+    dfs_fd_unlock();
+    return retfd;
+}
 
-        for (index = 0; index < fdt->maxfd; index++)
-        {
-            fd = fdt->fds[index];
-            if (fd == NULL || fd->fops == NULL || fd->path == NULL) continue;
+int fd_associate(struct dfs_fdtable *fdt, int fd, struct dfs_fd *file)
+{
+    int retfd = -1;
 
-            if (fd->fs == fs && strcmp(fd->path, mountpath) == 0)
-            {
-                /* found file in file descriptor table */
-                rt_free(fullpath);
-                dfs_fd_unlock();
+    if (!file)
+    {
+        return retfd;
+    }
+    if (!fdt)
+    {
+        return retfd;
+    }
 
-                return 0;
-            }
-        }
-        dfs_fd_unlock();
+    dfs_fd_lock();
+    /* check old fd */
+    if ((fd < 0) || (fd >= fdt->maxfd))
+    {
+        goto exit;
+    }
 
-        rt_free(fullpath);
+    if (fdt->fds[fd])
+    {
+        goto exit;
     }
+    /* inc ref_count */
+    file->ref_count++;
+    fdt->fds[fd] = file;
+    retfd = fd;
+exit:
+    dfs_fd_unlock();
+    return retfd;
+}
 
-    return -1;
+void fd_init(struct dfs_fd *fd)
+{
+    if (fd)
+    {
+        fd->magic = DFS_FD_MAGIC;
+        fd->ref_count = 1;
+        fd->pos = 0;
+        fd->fnode = NULL;
+        fd->data = NULL;
+    }
 }
 
 /**
@@ -395,7 +506,7 @@ const char *dfs_subdir(const char *directory, const char *filename)
     dir = filename + strlen(directory);
     if ((*dir != '/') && (dir != filename))
     {
-        dir --;
+        dir--;
     }
 
     return dir;
@@ -460,14 +571,14 @@ char *dfs_normalize_path(const char *directory, const char *filename)
 
         if (c == '.')
         {
-            if (!src[1]) src ++; /* '.' and ends */
+            if (!src[1]) src++; /* '.' and ends */
             else if (src[1] == '/')
             {
                 /* './' case */
                 src += 2;
 
                 while ((*src == '/') && (*src != '\0'))
-                    src ++;
+                    src++;
                 continue;
             }
             else if (src[1] == '.')
@@ -484,7 +595,7 @@ char *dfs_normalize_path(const char *directory, const char *filename)
                     src += 3;
 
                     while ((*src == '/') && (*src != '\0'))
-                        src ++;
+                        src++;
                     goto up_one;
                 }
             }
@@ -492,15 +603,15 @@ char *dfs_normalize_path(const char *directory, const char *filename)
 
         /* copy up the next '/' and erase all '/' */
         while ((c = *src++) != '\0' && c != '/')
-            *dst ++ = c;
+            *dst++ = c;
 
         if (c == '/')
         {
-            *dst ++ = '/';
+            *dst++ = '/';
             while (c == '/')
                 c = *src++;
 
-            src --;
+            src--;
         }
         else if (!c)
             break;
@@ -508,20 +619,20 @@ char *dfs_normalize_path(const char *directory, const char *filename)
         continue;
 
 up_one:
-        dst --;
+        dst--;
         if (dst < dst0)
         {
             rt_free(fullpath);
             return NULL;
         }
         while (dst0 < dst && dst[-1] != '/')
-            dst --;
+            dst--;
     }
 
     *dst = '\0';
 
     /* remove '/' in the end of path if exist */
-    dst --;
+    dst--;
     if ((dst != fullpath) && (*dst == '/'))
         *dst = '\0';
 
@@ -576,24 +687,24 @@ int list_fd(void)
 
     rt_kprintf("fd type    ref magic  path\n");
     rt_kprintf("-- ------  --- ----- ------\n");
-    for (index = 0; index < (int)fd_table->maxfd; index ++)
+    for (index = 0; index < (int)fd_table->maxfd; index++)
     {
         struct dfs_fd *fd = fd_table->fds[index];
 
-        if (fd && fd->fops)
+        if (fd && fd->fnode->fops)
         {
-            rt_kprintf("%2d ", index + DFS_FD_OFFSET);
-            if (fd->type == FT_DIRECTORY)    rt_kprintf("%-7.7s ", "dir");
-            else if (fd->type == FT_REGULAR) rt_kprintf("%-7.7s ", "file");
-            else if (fd->type == FT_SOCKET)  rt_kprintf("%-7.7s ", "socket");
-            else if (fd->type == FT_USER)    rt_kprintf("%-7.7s ", "user");
-            else if (fd->type == FT_DEVICE)   rt_kprintf("%-7.7s ", "device");
+            rt_kprintf("%2d ", index);
+            if (fd->fnode->type == FT_DIRECTORY)    rt_kprintf("%-7.7s ", "dir");
+            else if (fd->fnode->type == FT_REGULAR) rt_kprintf("%-7.7s ", "file");
+            else if (fd->fnode->type == FT_SOCKET)  rt_kprintf("%-7.7s ", "socket");
+            else if (fd->fnode->type == FT_USER)    rt_kprintf("%-7.7s ", "user");
+            else if (fd->fnode->type == FT_DEVICE)   rt_kprintf("%-7.7s ", "device");
             else rt_kprintf("%-8.8s ", "unknown");
-            rt_kprintf("%3d ", fd->ref_count);
+            rt_kprintf("%3d ", fd->fnode->ref_count);
             rt_kprintf("%04x  ", fd->magic);
-            if (fd->path)
+            if (fd->fnode->path)
             {
-                rt_kprintf("%s\n", fd->path);
+                rt_kprintf("%s\n", fd->fnode->path);
             }
             else
             {
@@ -624,12 +735,12 @@ int mount(int argc, char *argv[])
         rt_kprintf("filesystem  device  mountpoint\n");
         rt_kprintf("----------  ------  ----------\n");
         for (iter = &filesystem_table[0];
-             iter < &filesystem_table[DFS_FILESYSTEMS_MAX]; iter++)
+                iter < &filesystem_table[DFS_FILESYSTEMS_MAX]; iter++)
         {
             if ((iter != NULL) && (iter->path != NULL))
             {
                 rt_kprintf("%-10s  %-6s  %-s\n",
-                           iter->ops->name, iter->dev_id->parent.name, iter->path);
+                        iter->ops->name, iter->dev_id->parent.name, iter->path);
             }
         }
         return 0;

+ 295 - 93
components/dfs/src/dfs_file.c

@@ -15,12 +15,101 @@
 #include <dfs_file.h>
 #include <dfs_private.h>
 
+#define DFS_FNODE_HASH_NR 128
+
+struct dfs_fnode_mgr
+{
+    struct rt_mutex lock;
+    rt_list_t head[DFS_FNODE_HASH_NR];
+};
+
+static struct dfs_fnode_mgr dfs_fm;
+
+void dfs_fnode_mgr_init(void)
+{
+    int i = 0;
+
+    rt_mutex_init(&dfs_fm.lock, "dfs_mgr", RT_IPC_FLAG_PRIO);
+    for (i = 0; i < DFS_FNODE_HASH_NR; i++)
+    {
+        rt_list_init(&dfs_fm.head[i]);
+    }
+}
+
+/* BKDR Hash Function */
+static unsigned int bkdr_hash(const char *str)
+{
+    unsigned int seed = 131; // 31 131 1313 13131 131313 etc..
+    unsigned int hash = 0;
+
+    while (*str)
+    {
+        hash = hash * seed + (*str++);
+    }
+
+    return (hash % DFS_FNODE_HASH_NR);
+}
+
+static struct dfs_fnode *dfs_fnode_find(const char *path, rt_list_t **hash_head)
+{
+    struct dfs_fnode *fnode = NULL;
+    int hash = bkdr_hash(path);
+    rt_list_t *hh;
+
+    hh = dfs_fm.head[hash].next;
+
+    if (hash_head)
+    {
+        *hash_head = &dfs_fm.head[hash];
+    }
+
+    while (hh != &dfs_fm.head[hash])
+    {
+        fnode = rt_container_of(hh, struct dfs_fnode, list);
+        if (rt_strcmp(path, fnode->fullpath) == 0)
+        {
+            /* found */
+            return fnode;
+        }
+        hh = hh->next;
+    }
+    return NULL;
+}
+
 /**
  * @addtogroup FileApi
  */
 
 /*@{*/
 
+/**
+ * This function will return whether this file has been opend.
+ *
+ * @param pathname the file path name.
+ *
+ * @return 0 on file has been open successfully, -1 on open failed.
+ */
+int dfs_file_is_open(const char *pathname)
+{
+    char *fullpath = NULL;
+    struct dfs_fnode *fnode = NULL;
+    int ret = 0;
+
+    fullpath = dfs_normalize_path(NULL, pathname);
+
+    rt_mutex_take(&dfs_fm.lock, RT_WAITING_FOREVER);
+    fnode = dfs_fnode_find(fullpath, NULL);
+    if (fnode)
+    {
+        ret = 1;
+    }
+    rt_mutex_release(&dfs_fm.lock);
+
+    rt_free(fullpath);
+    return ret;
+}
+
+
 /**
  * this function will open a file which specified by path with specified flags.
  *
@@ -35,6 +124,8 @@ int dfs_file_open(struct dfs_fd *fd, const char *path, int flags)
     struct dfs_filesystem *fs;
     char *fullpath;
     int result;
+    struct dfs_fnode *fnode = NULL;
+    rt_list_t *hash_head;
 
     /* parameter check */
     if (fd == NULL)
@@ -49,67 +140,110 @@ int dfs_file_open(struct dfs_fd *fd, const char *path, int flags)
 
     LOG_D("open file:%s", fullpath);
 
-    /* find filesystem */
-    fs = dfs_filesystem_lookup(fullpath);
-    if (fs == NULL)
+    rt_mutex_take(&dfs_fm.lock, RT_WAITING_FOREVER);
+    /* fnode find */
+    fnode = dfs_fnode_find(fullpath, &hash_head);
+    if (fnode)
     {
-        rt_free(fullpath); /* release path */
-
-        return -ENOENT;
+        fnode->ref_count++;
+        fd->pos   = 0;
+        fd->fnode = fnode;
+        rt_mutex_release(&dfs_fm.lock);
     }
+    else
+    {
+        /* find filesystem */
+        fs = dfs_filesystem_lookup(fullpath);
+        if (fs == NULL)
+        {
+            rt_mutex_release(&dfs_fm.lock);
+            rt_free(fullpath); /* release path */
+            return -ENOENT;
+        }
 
-    LOG_D("open in filesystem:%s", fs->ops->name);
-    fd->fs    = fs;             /* set file system */
-    fd->fops  = fs->ops->fops;  /* set file ops */
+        fnode = rt_calloc(1, sizeof(struct dfs_fnode));
+        if (!fnode)
+        {
+            rt_mutex_release(&dfs_fm.lock);
+            rt_free(fullpath); /* release path */
+            return -ENOMEM;
+        }
+        fnode->ref_count = 1;
 
-    /* initialize the fd item */
-    fd->type  = FT_REGULAR;
-    fd->flags = flags;
-    fd->size  = 0;
-    fd->pos   = 0;
-    fd->data  = fs;
+        LOG_D("open in filesystem:%s", fs->ops->name);
+        fnode->fs    = fs;             /* set file system */
+        fnode->fops  = fs->ops->fops;  /* set file ops */
 
-    if (!(fs->ops->flags & DFS_FS_FLAG_FULLPATH))
-    {
-        if (dfs_subdir(fs->path, fullpath) == NULL)
-            fd->path = rt_strdup("/");
+        /* initialize the fd item */
+        fnode->type  = FT_REGULAR;
+        fnode->flags = flags;
+
+        if (!(fs->ops->flags & DFS_FS_FLAG_FULLPATH))
+        {
+            if (dfs_subdir(fs->path, fullpath) == NULL)
+                fnode->path = rt_strdup("/");
+            else
+                fnode->path = rt_strdup(dfs_subdir(fs->path, fullpath));
+            LOG_D("Actual file path: %s", fnode->path);
+        }
         else
-            fd->path = rt_strdup(dfs_subdir(fs->path, fullpath));
-        rt_free(fullpath);
-        LOG_D("Actual file path: %s", fd->path);
-    }
-    else
-    {
-        fd->path = fullpath;
-    }
+        {
+            fnode->path = fullpath;
+        }
+        fnode->fullpath = fullpath;
 
-    /* specific file system open routine */
-    if (fd->fops->open == NULL)
-    {
-        /* clear fd */
-        rt_free(fd->path);
-        fd->path = NULL;
+        /* specific file system open routine */
+        if (fnode->fops->open == NULL)
+        {
+            rt_mutex_release(&dfs_fm.lock);
+            /* clear fd */
+            if (fnode->path != fnode->fullpath)
+            {
+                rt_free(fnode->fullpath);
+            }
+            rt_free(fnode->path);
+            rt_free(fnode);
 
-        return -ENOSYS;
+            return -ENOSYS;
+        }
+
+        fd->pos   = 0;
+        fd->fnode = fnode;
+
+        /* insert fnode to hash */
+        rt_list_insert_after(hash_head, &fnode->list);
     }
 
-    if ((result = fd->fops->open(fd)) < 0)
+    if ((result = fnode->fops->open(fd)) < 0)
     {
-        /* clear fd */
-        rt_free(fd->path);
-        fd->path = NULL;
+        fnode->ref_count--;
+        if (fnode->ref_count == 0)
+        {
+            /* remove from hash */
+            rt_list_remove(&fnode->list);
+            /* clear fd */
+            if (fnode->path != fnode->fullpath)
+            {
+                rt_free(fnode->fullpath);
+            }
+            rt_free(fnode->path);
+            fd->fnode = NULL;
+            rt_free(fnode);
+            rt_mutex_release(&dfs_fm.lock);
+        }
 
         LOG_D("%s open failed", fullpath);
 
         return result;
     }
 
-    fd->flags |= DFS_F_OPEN;
+    fnode->flags |= DFS_F_OPEN;
     if (flags & O_DIRECTORY)
     {
-        fd->type = FT_DIRECTORY;
-        fd->flags |= DFS_F_DIRECTORY;
+        fnode->type = FT_DIRECTORY;
+        fnode->flags |= DFS_F_DIRECTORY;
     }
+    rt_mutex_release(&dfs_fm.lock);
 
     LOG_D("open successful");
     return 0;
@@ -124,20 +258,52 @@ int dfs_file_open(struct dfs_fd *fd, const char *path, int flags)
  */
 int dfs_file_close(struct dfs_fd *fd)
 {
+    struct dfs_fnode *fnode = NULL;
     int result = 0;
 
     if (fd == NULL)
+    {
         return -ENXIO;
+    }
+
+    if (fd->ref_count == 1)
+    {
+        rt_mutex_take(&dfs_fm.lock, RT_WAITING_FOREVER);
+        fnode = fd->fnode;
 
-    if (fd->fops->close != NULL)
-        result = fd->fops->close(fd);
+        if (fnode->ref_count <= 0)
+        {
+            rt_mutex_release(&dfs_fm.lock);
+            return -ENXIO;
+        }
 
-    /* close fd error, return */
-    if (result < 0)
-        return result;
+        if (fnode->fops->close != NULL)
+        {
+            result = fnode->fops->close(fd);
+        }
+
+        /* close fd error, return */
+        if (result < 0)
+        {
+            rt_mutex_release(&dfs_fm.lock);
+            return result;
+        }
 
-    rt_free(fd->path);
-    fd->path = NULL;
+        if (fnode->ref_count == 1)
+        {
+            /* remove from hash */
+            rt_list_remove(&fnode->list);
+            fd->fnode = NULL;
+
+            if (fnode->path != fnode->fullpath)
+            {
+                rt_free(fnode->fullpath);
+            }
+            rt_free(fnode->path);
+            rt_free(fnode);
+            rt_mutex_release(&dfs_fm.lock);
+        }
+    }
 
     return result;
 }
@@ -154,30 +320,34 @@ int dfs_file_close(struct dfs_fd *fd)
 int dfs_file_ioctl(struct dfs_fd *fd, int cmd, void *args)
 {
     if (fd == NULL)
+    {
         return -EINVAL;
+    }
 
     /* regular file system fd */
-    if (fd->type == FT_REGULAR || fd->type == FT_DEVICE)
+    if (fd->fnode->type == FT_REGULAR || fd->fnode->type == FT_DEVICE)
     {
         switch (cmd)
         {
         case F_GETFL:
-            return fd->flags; /* return flags */
+            return fd->fnode->flags; /* return flags */
         case F_SETFL:
             {
                 int flags = (int)(rt_base_t)args;
                 int mask  = O_NONBLOCK | O_APPEND;
 
                 flags &= mask;
-                fd->flags &= ~mask;
-                fd->flags |= flags;
+                fd->fnode->flags &= ~mask;
+                fd->fnode->flags |= flags;
             }
             return 0;
         }
     }
 
-    if (fd->fops->ioctl != NULL)
-        return fd->fops->ioctl(fd, cmd, args);
+    if (fd->fnode->fops->ioctl != NULL)
+    {
+        return fd->fnode->fops->ioctl(fd, cmd, args);
+    }
 
     return -ENOSYS;
 }
@@ -197,13 +367,19 @@ int dfs_file_read(struct dfs_fd *fd, void *buf, size_t len)
     int result = 0;
 
     if (fd == NULL)
+    {
         return -EINVAL;
+    }
 
-    if (fd->fops->read == NULL)
+    if (fd->fnode->fops->read == NULL)
+    {
         return -ENOSYS;
+    }
 
-    if ((result = fd->fops->read(fd, buf, len)) < 0)
-        fd->flags |= DFS_F_EOF;
+    if ((result = fd->fnode->fops->read(fd, buf, len)) < 0)
+    {
+        fd->fnode->flags |= DFS_F_EOF;
+    }
 
     return result;
 }
@@ -220,11 +396,20 @@ int dfs_file_read(struct dfs_fd *fd, void *buf, size_t len)
 int dfs_file_getdents(struct dfs_fd *fd, struct dirent *dirp, size_t nbytes)
 {
     /* parameter check */
-    if (fd == NULL || fd->type != FT_DIRECTORY)
+    if (fd == NULL)
+    {
         return -EINVAL;
+    }
 
-    if (fd->fops->getdents != NULL)
-        return fd->fops->getdents(fd, dirp, nbytes);
+    if (fd->fnode->type != FT_DIRECTORY)
+    {
+        return -EINVAL;
+    }
+
+    if (fd->fnode->fops->getdents != NULL)
+    {
+        return fd->fnode->fops->getdents(fd, dirp, nbytes);
+    }
 
     return -ENOSYS;
 }
@@ -249,17 +434,17 @@ int dfs_file_unlink(const char *path)
         return -EINVAL;
     }
 
-    /* get filesystem */
-    if ((fs = dfs_filesystem_lookup(fullpath)) == NULL)
+    /* Check whether file is already open */
+    if (dfs_file_is_open(fullpath))
     {
-        result = -ENOENT;
+        result = -EBUSY;
         goto __exit;
     }
 
-    /* Check whether file is already open */
-    if (fd_is_open(fullpath) == 0)
+    /* get filesystem */
+    if ((fs = dfs_filesystem_lookup(fullpath)) == NULL)
     {
-        result = -EBUSY;
+        result = -ENOENT;
         goto __exit;
     }
 
@@ -294,12 +479,16 @@ __exit:
 int dfs_file_write(struct dfs_fd *fd, const void *buf, size_t len)
 {
     if (fd == NULL)
+    {
         return -EINVAL;
+    }
 
-    if (fd->fops->write == NULL)
+    if (fd->fnode->fops->write == NULL)
+    {
         return -ENOSYS;
+    }
 
-    return fd->fops->write(fd, buf, len);
+    return fd->fnode->fops->write(fd, buf, len);
 }
 
 /**
@@ -314,10 +503,10 @@ int dfs_file_flush(struct dfs_fd *fd)
     if (fd == NULL)
         return -EINVAL;
 
-    if (fd->fops->flush == NULL)
+    if (fd->fnode->fops->flush == NULL)
         return -ENOSYS;
 
-    return fd->fops->flush(fd);
+    return fd->fnode->fops->flush(fd);
 }
 
 /**
@@ -335,10 +524,10 @@ int dfs_file_lseek(struct dfs_fd *fd, off_t offset)
     if (fd == NULL)
         return -EINVAL;
 
-    if (fd->fops->lseek == NULL)
+    if (fd->fnode->fops->lseek == NULL)
         return -ENOSYS;
 
-    result = fd->fops->lseek(fd, offset);
+    result = fd->fnode->fops->lseek(fd, offset);
 
     /* update current position */
     if (result >= 0)
@@ -425,11 +614,10 @@ int dfs_file_stat(const char *path, struct stat *buf)
  */
 int dfs_file_rename(const char *oldpath, const char *newpath)
 {
-    int result;
-    struct dfs_filesystem *oldfs, *newfs;
-    char *oldfullpath, *newfullpath;
+    int result = RT_EOK;
+    struct dfs_filesystem *oldfs = NULL, *newfs = NULL;
+    char *oldfullpath = NULL, *newfullpath = NULL;
 
-    result = RT_EOK;
     newfullpath = NULL;
     oldfullpath = NULL;
 
@@ -440,6 +628,12 @@ int dfs_file_rename(const char *oldpath, const char *newpath)
         goto __exit;
     }
 
+    if (dfs_file_is_open((const char *)oldfullpath))
+    {
+        result = -EBUSY;
+        goto __exit;
+    }
+
     newfullpath = dfs_normalize_path(NULL, newpath);
     if (newfullpath == NULL)
     {
@@ -473,8 +667,14 @@ int dfs_file_rename(const char *oldpath, const char *newpath)
     }
 
 __exit:
-    rt_free(oldfullpath);
-    rt_free(newfullpath);
+    if (oldfullpath)
+    {
+        rt_free(oldfullpath);
+    }
+    if (newfullpath)
+    {
+        rt_free(newfullpath);
+    }
 
     /* not at same file system, return EXDEV */
     return result;
@@ -494,17 +694,17 @@ int dfs_file_ftruncate(struct dfs_fd *fd, off_t length)
     int result;
 
     /* fd is null or not a regular file system fd, or length is invalid */
-    if (fd == NULL || fd->type != FT_REGULAR || length < 0)
+    if (fd == NULL || fd->fnode->type != FT_REGULAR || length < 0)
         return -EINVAL;
 
-    if (fd->fops->ioctl == NULL)
+    if (fd->fnode->fops->ioctl == NULL)
         return -ENOSYS;
 
-    result = fd->fops->ioctl(fd, RT_FIOFTRUNCATE, (void*)&length);
+    result = fd->fnode->fops->ioctl(fd, RT_FIOFTRUNCATE, (void*)&length);
 
     /* update current size */
     if (result == 0)
-        fd->size = length;
+        fd->fnode->size = length;
 
     return result;
 }
@@ -512,10 +712,10 @@ int dfs_file_ftruncate(struct dfs_fd *fd, off_t length)
 #ifdef RT_USING_FINSH
 #include <finsh.h>
 
-static struct dfs_fd fd;
-static struct dirent dirent;
 void ls(const char *pathname)
 {
+    struct dfs_fd fd;
+    struct dirent dirent;
     struct stat stat;
     int length;
     char *fullpath, *path;
@@ -537,6 +737,7 @@ void ls(const char *pathname)
         path = (char *)pathname;
     }
 
+    fd_init(&fd);
     /* list directory */
     if (dfs_file_open(&fd, path, O_DIRECTORY) == 0)
     {
@@ -570,8 +771,7 @@ void ls(const char *pathname)
                     rt_kprintf("BAD file: %s\n", dirent.d_name);
                 rt_free(fullpath);
             }
-        }
-        while (length > 0);
+        } while (length > 0);
 
         dfs_file_close(&fd);
     }
@@ -595,9 +795,11 @@ FINSH_FUNCTION_EXPORT(rm, remove files or directories);
 
 void cat(const char *filename)
 {
-    uint32_t length;
+    struct dfs_fd fd;
+    uint32_t length = 0;
     char buffer[81];
 
+    fd_init(&fd);
     if (dfs_file_open(&fd, filename, O_RDONLY) < 0)
     {
         rt_kprintf("Open %s failed\n", filename);
@@ -613,8 +815,7 @@ void cat(const char *filename)
             buffer[length] = '\0';
             rt_kprintf("%s", buffer);
         }
-    }
-    while (length > 0);
+    } while (length > 0);
 
     dfs_file_close(&fd);
 }
@@ -623,6 +824,7 @@ FINSH_FUNCTION_EXPORT(cat, print file);
 #define BUF_SZ  4096
 static void copyfile(const char *src, const char *dst)
 {
+    struct dfs_fd fd;
     struct dfs_fd src_fd;
     rt_uint8_t *block_ptr;
     rt_int32_t read_bytes;
@@ -635,6 +837,7 @@ static void copyfile(const char *src, const char *dst)
         return;
     }
 
+    fd_init(&src_fd);
     if (dfs_file_open(&src_fd, src, O_RDONLY) < 0)
     {
         rt_free(block_ptr);
@@ -642,6 +845,7 @@ static void copyfile(const char *src, const char *dst)
 
         return;
     }
+    fd_init(&fd);
     if (dfs_file_open(&fd, dst, O_WRONLY | O_CREAT) < 0)
     {
         rt_free(block_ptr);
@@ -667,8 +871,7 @@ static void copyfile(const char *src, const char *dst)
                 break;
             }
         }
-    }
-    while (read_bytes > 0);
+    } while (read_bytes > 0);
 
     dfs_file_close(&src_fd);
     dfs_file_close(&fd);
@@ -733,8 +936,7 @@ static void copydir(const char *src, const char *dst)
             rt_free(src_entry_full);
             rt_free(dst_entry_full);
         }
-    }
-    while (length > 0);
+    } while (length > 0);
 
     dfs_file_close(&cpfd);
 }

+ 1 - 0
components/dfs/src/dfs_fs.c

@@ -273,6 +273,7 @@ int dfs_mount(const char   *device_name,
     {
         struct dfs_fd fd;
 
+        fd_init(&fd);
         if (dfs_file_open(&fd, fullpath, O_RDONLY | O_DIRECTORY) < 0)
         {
             rt_free(fullpath);

+ 10 - 49
components/dfs/src/dfs_posix.c

@@ -47,17 +47,13 @@ int open(const char *file, int flags, ...)
     if (result < 0)
     {
         /* release the ref-count of fd */
-        fd_put(d);
-        fd_put(d);
+        fd_release(fd);
 
         rt_set_errno(result);
 
         return -1;
     }
 
-    /* release the ref-count of fd */
-    fd_put(d);
-
     return fd;
 }
 RTM_EXPORT(open);
@@ -84,7 +80,6 @@ int close(int fd)
     }
 
     result = dfs_file_close(d);
-    fd_put(d);
 
     if (result < 0)
     {
@@ -93,7 +88,7 @@ int close(int fd)
         return -1;
     }
 
-    fd_put(d);
+    fd_release(fd);
 
     return 0;
 }
@@ -131,15 +126,11 @@ int read(int fd, void *buf, size_t len)
     result = dfs_file_read(d, buf, len);
     if (result < 0)
     {
-        fd_put(d);
         rt_set_errno(result);
 
         return -1;
     }
 
-    /* release the ref-count of fd */
-    fd_put(d);
-
     return result;
 }
 RTM_EXPORT(read);
@@ -175,15 +166,11 @@ int write(int fd, const void *buf, size_t len)
     result = dfs_file_write(d, buf, len);
     if (result < 0)
     {
-        fd_put(d);
         rt_set_errno(result);
 
         return -1;
     }
 
-    /* release the ref-count of fd */
-    fd_put(d);
-
     return result;
 }
 RTM_EXPORT(write);
@@ -221,11 +208,10 @@ off_t lseek(int fd, off_t offset, int whence)
         break;
 
     case SEEK_END:
-        offset += d->size;
+        offset += d->fnode->size;
         break;
 
     default:
-        fd_put(d);
         rt_set_errno(-EINVAL);
 
         return -1;
@@ -233,7 +219,6 @@ off_t lseek(int fd, off_t offset, int whence)
 
     if (offset < 0)
     {
-        fd_put(d);
         rt_set_errno(-EINVAL);
 
         return -1;
@@ -241,15 +226,11 @@ off_t lseek(int fd, off_t offset, int whence)
     result = dfs_file_lseek(d, offset);
     if (result < 0)
     {
-        fd_put(d);
         rt_set_errno(result);
 
         return -1;
     }
 
-    /* release the ref-count of fd */
-    fd_put(d);
-
     return offset;
 }
 RTM_EXPORT(lseek);
@@ -356,17 +337,15 @@ int fstat(int fildes, struct stat *buf)
 
     buf->st_mode = S_IFREG | S_IRUSR | S_IRGRP | S_IROTH |
                    S_IWUSR | S_IWGRP | S_IWOTH;
-    if (d->type == FT_DIRECTORY)
+    if (d->fnode->type == FT_DIRECTORY)
     {
         buf->st_mode &= ~S_IFREG;
         buf->st_mode |= S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH;
     }
 
-    buf->st_size    = d->size;
+    buf->st_size    = d->fnode->size;
     buf->st_mtime   = 0;
 
-    fd_put(d);
-
     return RT_EOK;
 }
 RTM_EXPORT(fstat);
@@ -397,7 +376,6 @@ int fsync(int fildes)
 
     ret = dfs_file_flush(d);
 
-    fd_put(d);
     return ret;
 }
 RTM_EXPORT(fsync);
@@ -431,7 +409,6 @@ int fcntl(int fildes, int cmd, ...)
         va_end(ap);
 
         ret = dfs_file_ioctl(d, cmd, arg);
-        fd_put(d);
     }
     else ret = -EBADF;
 
@@ -496,7 +473,6 @@ int ftruncate(int fd, off_t length)
 
     if (length < 0)
     {
-        fd_put(d);
         rt_set_errno(-EINVAL);
 
         return -1;
@@ -504,15 +480,11 @@ int ftruncate(int fd, off_t length)
     result = dfs_file_ftruncate(d, length);
     if (result < 0)
     {
-        fd_put(d);
         rt_set_errno(result);
 
         return -1;
     }
 
-    /* release the ref-count of fd */
-    fd_put(d);
-
     return 0;
 }
 RTM_EXPORT(ftruncate);
@@ -570,16 +542,14 @@ int mkdir(const char *path, mode_t mode)
 
     if (result < 0)
     {
-        fd_put(d);
-        fd_put(d);
+        fd_release(fd);
         rt_set_errno(result);
 
         return -1;
     }
 
     dfs_file_close(d);
-    fd_put(d);
-    fd_put(d);
+    fd_release(fd);
 
     return 0;
 }
@@ -646,7 +616,7 @@ DIR *opendir(const char *name)
         if (t == NULL)
         {
             dfs_file_close(d);
-            fd_put(d);
+            fd_release(fd);
         }
         else
         {
@@ -654,14 +624,12 @@ DIR *opendir(const char *name)
 
             t->fd = fd;
         }
-        fd_put(d);
 
         return t;
     }
 
     /* open failed */
-    fd_put(d);
-    fd_put(d);
+    fd_release(fd);
     rt_set_errno(result);
 
     return NULL;
@@ -704,7 +672,6 @@ struct dirent *readdir(DIR *d)
                                    sizeof(d->buf) - 1);
         if (result <= 0)
         {
-            fd_put(fd);
             rt_set_errno(result);
 
             return NULL;
@@ -714,8 +681,6 @@ struct dirent *readdir(DIR *d)
         d->cur = 0; /* current entry index */
     }
 
-    fd_put(fd);
-
     return (struct dirent *)(d->buf + d->cur);
 }
 RTM_EXPORT(readdir);
@@ -742,7 +707,6 @@ long telldir(DIR *d)
     }
 
     result = fd->pos - d->num + d->cur;
-    fd_put(fd);
 
     return result;
 }
@@ -770,7 +734,6 @@ void seekdir(DIR *d, off_t offset)
     /* seek to the offset position of directory */
     if (dfs_file_lseek(fd, offset) >= 0)
         d->num = d->cur = 0;
-    fd_put(fd);
 }
 RTM_EXPORT(seekdir);
 
@@ -795,7 +758,6 @@ void rewinddir(DIR *d)
     /* seek to the beginning of directory */
     if (dfs_file_lseek(fd, 0) >= 0)
         d->num = d->cur = 0;
-    fd_put(fd);
 }
 RTM_EXPORT(rewinddir);
 
@@ -821,9 +783,8 @@ int closedir(DIR *d)
     }
 
     result = dfs_file_close(fd);
-    fd_put(fd);
+    fd_release(d->fd);
 
-    fd_put(fd);
     rt_free(d);
 
     if (result < 0)

+ 2 - 4
components/dfs/src/poll.c

@@ -134,23 +134,21 @@ static int do_pollfd(struct pollfd *pollfd, rt_pollreq_t *req)
         if (f)
         {
             mask = POLLMASK_DEFAULT;
-            if (f->fops->poll)
+            if (f->fnode->fops->poll)
             {
                 req->_key = pollfd->events | POLLERR | POLLHUP;
 
-                mask = f->fops->poll(f, req);
+                mask = f->fnode->fops->poll(f, req);
 
                 /* dealwith the device return error -1*/
                 if (mask < 0)
                 {
-                    fd_put(f);
                     pollfd->revents = 0;
                     return mask;
                 }
             }
             /* Mask out unneeded events. */
             mask &= pollfd->events | POLLERR | POLLHUP;
-            fd_put(f);
         }
     }
     pollfd->revents = mask;

+ 11 - 11
components/drivers/serial/serial.c

@@ -65,10 +65,10 @@ static int serial_fops_open(struct dfs_fd *fd)
     rt_uint16_t flags = 0;
     rt_device_t device;
 
-    device = (rt_device_t)fd->data;
+    device = (rt_device_t)fd->fnode->data;
     RT_ASSERT(device != RT_NULL);
 
-    switch (fd->flags & O_ACCMODE)
+    switch (fd->fnode->flags & O_ACCMODE)
     {
     case O_RDONLY:
         LOG_D("fops open: O_RDONLY!");
@@ -83,11 +83,11 @@ static int serial_fops_open(struct dfs_fd *fd)
         flags = RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_RDWR;
         break;
     default:
-        LOG_E("fops open: unknown mode - %d!", fd->flags & O_ACCMODE);
+        LOG_E("fops open: unknown mode - %d!", fd->fnode->flags & O_ACCMODE);
         break;
     }
 
-    if ((fd->flags & O_ACCMODE) != O_WRONLY)
+    if ((fd->fnode->flags & O_ACCMODE) != O_WRONLY)
         rt_device_set_rx_indicate(device, serial_fops_rx_ind);
     ret = rt_device_open(device, flags);
     if (ret == RT_EOK) return 0;
@@ -99,7 +99,7 @@ static int serial_fops_close(struct dfs_fd *fd)
 {
     rt_device_t device;
 
-    device = (rt_device_t)fd->data;
+    device = (rt_device_t)fd->fnode->data;
 
     rt_device_set_rx_indicate(device, RT_NULL);
     rt_device_close(device);
@@ -111,7 +111,7 @@ static int serial_fops_ioctl(struct dfs_fd *fd, int cmd, void *args)
 {
     rt_device_t device;
 
-    device = (rt_device_t)fd->data;
+    device = (rt_device_t)fd->fnode->data;
     switch (cmd)
     {
     case FIONREAD:
@@ -129,14 +129,14 @@ static int serial_fops_read(struct dfs_fd *fd, void *buf, size_t count)
     rt_device_t device;
     int wait_ret;
 
-    device = (rt_device_t)fd->data;
+    device = (rt_device_t)fd->fnode->data;
 
     do
     {
         size = rt_device_read(device, -1,  buf, count);
         if (size <= 0)
         {
-            if (fd->flags & O_NONBLOCK)
+            if (fd->fnode->flags & O_NONBLOCK)
             {
                 break;
             }
@@ -160,7 +160,7 @@ static int serial_fops_write(struct dfs_fd *fd, const void *buf, size_t count)
 {
     rt_device_t device;
 
-    device = (rt_device_t)fd->data;
+    device = (rt_device_t)fd->fnode->data;
     return rt_device_write(device, -1, buf, count);
 }
 
@@ -171,13 +171,13 @@ static int serial_fops_poll(struct dfs_fd *fd, struct rt_pollreq *req)
     rt_device_t device;
     struct rt_serial_device *serial;
 
-    device = (rt_device_t)fd->data;
+    device = (rt_device_t)fd->fnode->data;
     RT_ASSERT(device != RT_NULL);
 
     serial = (struct rt_serial_device *)device;
 
     /* only support POLLIN */
-    flags = fd->flags & O_ACCMODE;
+    flags = fd->fnode->flags & O_ACCMODE;
     if (flags == O_RDONLY || flags == O_RDWR)
     {
         rt_base_t level;

+ 22 - 22
components/drivers/src/pipe.c

@@ -23,7 +23,7 @@ static int pipe_fops_open(struct dfs_fd *fd)
     rt_device_t device;
     rt_pipe_t *pipe;
 
-    pipe = (rt_pipe_t *)fd->data;
+    pipe = (rt_pipe_t *)fd->fnode->data;
     if (!pipe) return -1;
 
     device = &(pipe->parent);
@@ -39,20 +39,20 @@ static int pipe_fops_open(struct dfs_fd *fd)
         }
     }
 
-    switch (fd->flags & O_ACCMODE)
+    switch (fd->fnode->flags & O_ACCMODE)
     {
     case O_RDONLY:
-        pipe->readers ++;
+        pipe->readers++;
         break;
     case O_WRONLY:
-        pipe->writers ++;
+        pipe->writers++;
         break;
     case O_RDWR:
-        pipe->readers ++;
-        pipe->writers ++;
+        pipe->readers++;
+        pipe->writers++;
         break;
     }
-    device->ref_count ++;
+    device->ref_count++;
 
 __exit:
     rt_mutex_release(&(pipe->lock));
@@ -65,23 +65,23 @@ static int pipe_fops_close(struct dfs_fd *fd)
     rt_device_t device;
     rt_pipe_t *pipe;
 
-    pipe = (rt_pipe_t *)fd->data;
+    pipe = (rt_pipe_t *)fd->fnode->data;
     if (!pipe) return -1;
 
     device = &(pipe->parent);
     rt_mutex_take(&(pipe->lock), RT_WAITING_FOREVER);
 
-    switch (fd->flags & O_ACCMODE)
+    switch (fd->fnode->flags & O_ACCMODE)
     {
     case O_RDONLY:
-        pipe->readers --;
+        pipe->readers--;
         break;
     case O_WRONLY:
-        pipe->writers --;
+        pipe->writers--;
         break;
     case O_RDWR:
-        pipe->readers --;
-        pipe->writers --;
+        pipe->readers--;
+        pipe->writers--;
         break;
     }
 
@@ -101,7 +101,7 @@ static int pipe_fops_close(struct dfs_fd *fd)
             rt_ringbuffer_destroy(pipe->fifo);
         pipe->fifo = RT_NULL;
     }
-    device->ref_count --;
+    device->ref_count--;
 
     rt_mutex_release(&(pipe->lock));
 
@@ -119,7 +119,7 @@ static int pipe_fops_ioctl(struct dfs_fd *fd, int cmd, void *args)
     rt_pipe_t *pipe;
     int ret = 0;
 
-    pipe = (rt_pipe_t *)fd->data;
+    pipe = (rt_pipe_t *)fd->fnode->data;
 
     switch (cmd)
     {
@@ -142,7 +142,7 @@ static int pipe_fops_read(struct dfs_fd *fd, void *buf, size_t count)
     int len = 0;
     rt_pipe_t *pipe;
 
-    pipe = (rt_pipe_t *)fd->data;
+    pipe = (rt_pipe_t *)fd->fnode->data;
 
     /* no process has the pipe open for writing, return end-of-file */
     if (pipe->writers == 0)
@@ -165,7 +165,7 @@ static int pipe_fops_read(struct dfs_fd *fd, void *buf, size_t count)
         }
         else
         {
-            if (fd->flags & O_NONBLOCK)
+            if (fd->fnode->flags & O_NONBLOCK)
             {
                 len = -EAGAIN;
                 goto out;
@@ -194,7 +194,7 @@ static int pipe_fops_write(struct dfs_fd *fd, const void *buf, size_t count)
     int ret = 0;
     uint8_t *pbuf;
 
-    pipe = (rt_pipe_t *)fd->data;
+    pipe = (rt_pipe_t *)fd->fnode->data;
 
     if (pipe->readers == 0)
     {
@@ -228,7 +228,7 @@ static int pipe_fops_write(struct dfs_fd *fd, const void *buf, size_t count)
         }
         else
         {
-            if (fd->flags & O_NONBLOCK)
+            if (fd->fnode->flags & O_NONBLOCK)
             {
                 if (ret == 0)
                 {
@@ -261,12 +261,12 @@ static int pipe_fops_poll(struct dfs_fd *fd, rt_pollreq_t *req)
     int mask = 0;
     rt_pipe_t *pipe;
     int mode = 0;
-    pipe = (rt_pipe_t *)fd->data;
+    pipe = (rt_pipe_t *)fd->fnode->data;
 
     rt_poll_add(&(pipe->reader_queue), req);
     rt_poll_add(&(pipe->writer_queue), req);
 
-    switch (fd->flags & O_ACCMODE)
+    switch (fd->fnode->flags & O_ACCMODE)
     {
     case O_RDONLY:
         mode = 1;
@@ -564,7 +564,7 @@ int pipe(int fildes[2])
 int mkfifo(const char *path, mode_t mode)
 {
     rt_pipe_t *pipe;
-    
+
     pipe = rt_pipe_create(path, PIPE_BUFSZ);
     if (pipe == RT_NULL)
     {

+ 12 - 1
components/libc/compilers/musl/stdio.c

@@ -12,12 +12,14 @@
 
 #include <rtthread.h>
 #include "libc.h"
+#include "dfs.h"
 
 #define STDIO_DEVICE_NAME_MAX   32
 
 int	_EXFUN(fileno, (FILE *));
 
 static FILE* std_console = NULL;
+int sys_dup2(int oldfd, int new);
 
 int libc_stdio_set_console(const char* device_name, int mode)
 {
@@ -45,7 +47,16 @@ int libc_stdio_set_console(const char* device_name, int mode)
         std_console = fp;
     }
 
-    if (std_console) return fileno(std_console);
+    if (std_console)
+    {
+        int fd = fileno(std_console);
+
+        /* set fd (0, 1, 2) */
+        sys_dup2(fd, 0);
+        sys_dup2(fd, 1);
+        sys_dup2(fd, 2);
+        return fd;
+    }
 
     return -1;
 }

+ 12 - 15
components/lwp/lwp.c

@@ -749,25 +749,22 @@ void lwp_cleanup(struct rt_thread *tid)
 
 static void lwp_copy_stdio_fdt(struct rt_lwp *lwp)
 {
-    int fd;
     struct dfs_fd *d;
     struct dfs_fdtable *lwp_fdt;
 
-    fd = libc_stdio_get_console();
-    d = fd_get(fd);
-    fd_put(d);
-
-    fd = fd - DFS_FD_OFFSET;
-    if (d == NULL)
-    {
-        return;
-    }
-
     lwp_fdt = &lwp->fdt;
-    lwp_fdt->fds = rt_malloc(sizeof(void *) * (fd + 1));
-    rt_memset(lwp_fdt->fds, 0, sizeof(void *) * (fd + 1));
-    lwp_fdt->fds[fd] = d;
-    lwp_fdt->maxfd = fd + 1;
+    /* init 4 fds */
+    lwp_fdt->fds = rt_calloc(4, sizeof(void *));
+    if (lwp_fdt->fds)
+    {
+        lwp_fdt->maxfd = 4;
+        d = fd_get(0);
+        fd_associate(lwp_fdt, 0, d);
+        d = fd_get(1);
+        fd_associate(lwp_fdt, 1, d);
+        d = fd_get(2);
+        fd_associate(lwp_fdt, 2, d);
+    }
 
     return;
 }

+ 58 - 52
components/lwp/lwp_console.c

@@ -45,8 +45,8 @@ rt_inline struct rt_wqueue *wait_queue_current_get(void)
 
 static void console_wakeup_check(struct rt_console_device *console)
 {
-    rt_size_t len;
-    struct rt_wqueue *wq;
+    rt_size_t len = 0;
+    struct rt_wqueue *wq = NULL;
 
     len = rt_ringbuffer_data_len(&console->input_rb);
     if (len)
@@ -58,9 +58,9 @@ static void console_wakeup_check(struct rt_console_device *console)
 
 static void console_rx_notify(struct rt_device *dev)
 {
-    struct rt_console_device *console;
-    rt_size_t len;
-    rt_uint8_t ch;
+    struct rt_console_device *console = NULL;
+    rt_size_t len = 0;
+    rt_uint8_t ch = 0;
 
     console = (struct rt_console_device *)dev;
     RT_ASSERT(console != RT_NULL);
@@ -95,7 +95,7 @@ static void console_rx_notify(struct rt_device *dev)
 
 void rt_console_set_foreground(struct rt_lwp *lwp)
 {
-    rt_base_t level;
+    rt_base_t level = 0;
 
     level = rt_hw_interrupt_disable();
     if (_console.init_flag != CONSOLE_INIT_FLAG_INITED)
@@ -111,8 +111,8 @@ exit:
 
 struct rt_lwp * rt_console_get_foreground(void)
 {
-    struct rt_lwp *lwp;
-    rt_base_t level;
+    struct rt_lwp *lwp = RT_NULL;
+    rt_base_t level = 0;
 
     level = rt_hw_interrupt_disable();
     lwp = _console.foreground;
@@ -135,9 +135,9 @@ static void iodev_close(struct rt_console_device *console)
 
 static rt_err_t iodev_open(struct rt_console_device *console)
 {
-    rt_err_t ret;
+    rt_err_t ret = RT_EOK;
     struct rt_device_notify rx_notify;
-    rt_uint16_t oflags;
+    rt_uint16_t oflags = 0;
 
     rt_device_control(console->iodev, RT_DEVICE_CTRL_CONSOLE_OFLAG, &oflags);
 
@@ -155,8 +155,8 @@ static rt_err_t iodev_open(struct rt_console_device *console)
 
 struct rt_device *rt_console_get_iodev(void)
 {
-    rt_base_t level;
-    struct rt_device *iodev;
+    rt_base_t level = 0;
+    struct rt_device *iodev = RT_NULL;
 
     level = rt_hw_interrupt_disable();
     iodev = _console.iodev;
@@ -166,9 +166,9 @@ struct rt_device *rt_console_get_iodev(void)
 
 struct rt_device *rt_console_set_iodev(struct rt_device *iodev)
 {
-    rt_base_t level;
-    struct rt_device *io_before;
-    struct rt_console_device *console;
+    rt_base_t level = 0;
+    struct rt_device *io_before = RT_NULL;
+    struct rt_console_device *console = RT_NULL;
 
     RT_ASSERT(iodev != RT_NULL);
 
@@ -211,38 +211,44 @@ exit:
 /* fops for console */
 static int console_fops_open(struct dfs_fd *fd)
 {
-    int ret;
-    struct rt_device * device;
+    int ret = 0;
+    struct rt_device *device = RT_NULL;
 
-    device = (struct rt_device *)fd->data;
+    device = (struct rt_device *)fd->fnode->data;
     RT_ASSERT(device != RT_NULL);
 
-    ret = rt_device_open(device, fd->flags);
+    if (fd->fnode->ref_count == 1)
+    {
+        ret = rt_device_open(device, fd->fnode->flags);
+    }
     return ret;
 }
 
 static int console_fops_close(struct dfs_fd *fd)
 {
-    int ret;
-    struct rt_device * device;
+    int ret = 0;
+    struct rt_device *device = RT_NULL;
 
-    device = (struct rt_device *)fd->data;
+    device = (struct rt_device *)fd->fnode->data;
     RT_ASSERT(device != RT_NULL);
 
-    ret = rt_device_close(device);
+    if (fd->fnode->ref_count == 1)
+    {
+        ret = rt_device_close(device);
+    }
     return ret;
 }
 
 static int console_fops_read(struct dfs_fd *fd, void *buf, size_t count)
 {
-    rt_base_t level;
+    rt_base_t level = 0;
     int size = 0;
-    struct rt_console_device *console;
-    struct rt_lwp *lwp;
-    struct rt_wqueue *wq;
-    int wait_ret;
+    struct rt_console_device *console = RT_NULL;
+    struct rt_lwp *lwp = RT_NULL;
+    struct rt_wqueue *wq = RT_NULL;
+    int wait_ret = 0;
 
-    console = (struct rt_console_device *)fd->data;
+    console = (struct rt_console_device *)fd->fnode->data;
     RT_ASSERT(console != RT_NULL);
     RT_ASSERT(console->init_flag == CONSOLE_INIT_FLAG_INITED);
 
@@ -258,7 +264,7 @@ static int console_fops_read(struct dfs_fd *fd, void *buf, size_t count)
         {
             break;
         }
-        if (fd->flags & O_NONBLOCK)
+        if (fd->fnode->flags & O_NONBLOCK)
         {
             break;
         }
@@ -278,10 +284,10 @@ static int console_fops_read(struct dfs_fd *fd, void *buf, size_t count)
 
 static int console_fops_write(struct dfs_fd *fd, const void *buf, size_t count)
 {
-    int size;
-    struct rt_device * device;
+    int size = 0;
+    struct rt_device *device = RT_NULL;
 
-    device = (struct rt_device *)fd->data;
+    device = (struct rt_device *)fd->fnode->data;
     RT_ASSERT(device != RT_NULL);
     size = rt_device_write(device, -1, buf, count);
     return size;
@@ -289,14 +295,14 @@ static int console_fops_write(struct dfs_fd *fd, const void *buf, size_t count)
 
 static int console_fops_poll(struct dfs_fd *fd, struct rt_pollreq *req)
 {
-    rt_base_t level;
+    rt_base_t level = 0;
     int mask = POLLOUT;
-    struct rt_device * device;
-    struct rt_console_device *console;
-    struct rt_wqueue *wq;
-    struct rt_lwp *lwp;
+    struct rt_device *device = RT_NULL;
+    struct rt_console_device *console = RT_NULL;
+    struct rt_wqueue *wq = RT_NULL;
+    struct rt_lwp *lwp = RT_NULL;
 
-    device = (struct rt_device *)fd->data;
+    device = (struct rt_device *)fd->fnode->data;
     RT_ASSERT(device != RT_NULL);
 
     console = (struct rt_console_device *)device;
@@ -343,9 +349,9 @@ const static struct dfs_file_ops _console_fops =
  */
 static rt_err_t rt_console_init(struct rt_device *dev)
 {
-    rt_base_t level;
-    rt_err_t result;
-    struct rt_console_device *console;
+    rt_base_t level = 0;
+    rt_err_t result = RT_EOK;
+    struct rt_console_device *console = RT_NULL;
 
     RT_ASSERT(dev != RT_NULL);
 
@@ -370,7 +376,7 @@ exit:
 static rt_err_t rt_console_open(struct rt_device *dev, rt_uint16_t oflag)
 {
     rt_err_t result = RT_EOK;
-    struct rt_console_device *console;
+    struct rt_console_device *console = RT_NULL;
 
     RT_ASSERT(dev != RT_NULL);
     console = (struct rt_console_device *)dev;
@@ -382,7 +388,7 @@ static rt_err_t rt_console_open(struct rt_device *dev, rt_uint16_t oflag)
 static rt_err_t rt_console_close(struct rt_device *dev)
 {
     rt_err_t result = RT_EOK;
-    struct rt_console_device *console;
+    struct rt_console_device *console = RT_NULL;
 
     console = (struct rt_console_device *)dev;
     RT_ASSERT(console != RT_NULL);
@@ -395,10 +401,10 @@ static rt_size_t rt_console_read(struct rt_device *dev,
         void             *buffer,
         rt_size_t         size)
 {
-    rt_base_t level;
+    rt_base_t level = 0;
     rt_size_t len = 0;
-    struct rt_lwp *lwp;
-    struct rt_console_device *console;
+    struct rt_lwp *lwp = RT_NULL;
+    struct rt_console_device *console = RT_NULL;
 
     console = (struct rt_console_device *)dev;
     RT_ASSERT(console != RT_NULL);
@@ -431,9 +437,9 @@ static rt_size_t rt_console_write(struct rt_device *dev,
         const void       *buffer,
         rt_size_t         size)
 {
-    rt_base_t level;
+    rt_base_t level = 0;
     rt_size_t len = 0;
-    struct rt_console_device *console;
+    struct rt_console_device *console = RT_NULL;
 
     console = (struct rt_console_device *)dev;
     RT_ASSERT(console != RT_NULL);
@@ -463,9 +469,9 @@ const static struct rt_device_ops console_ops =
  */
 rt_err_t rt_console_register(const char *name, struct rt_device *iodev)
 {
-    rt_base_t level;
-    rt_err_t ret;
-    struct rt_device *device;
+    rt_base_t level = 0;
+    rt_err_t ret = RT_EOK;
+    struct rt_device *device = RT_NULL;
     struct rt_console_device *console = &_console;
 
     level = rt_hw_interrupt_disable();

+ 12 - 15
components/lwp/lwp_ipc.c

@@ -798,7 +798,7 @@ static struct dfs_fd *lwp_fd_get(int fdt_type, int fd)
     return fdt_fd_get(fdt, fd);
 }
 
-static void lwp_fd_put(int fdt_type, struct dfs_fd *fd)
+static void lwp_fd_release(int fdt_type, int fd)
 {
     struct dfs_fdtable *fdt;
 
@@ -810,7 +810,7 @@ static void lwp_fd_put(int fdt_type, struct dfs_fd *fd)
     {
         fdt = dfs_fdtable_get();
     }
-    fdt_fd_put(fdt, fd);
+    fdt_fd_release(fdt, fd);
 }
 
 static int _chfd_alloc(int fdt_type)
@@ -837,8 +837,7 @@ static void _chfd_free(int fd, int fdt_type)
     {
         return;
     }
-    lwp_fd_put(fdt_type, d);
-    lwp_fd_put(fdt_type, d);
+    lwp_fd_release(fdt_type, fd);
 }
 
 /* for fops */
@@ -847,7 +846,7 @@ static int channel_fops_poll(struct dfs_fd *file, struct rt_pollreq *req)
     int mask = POLLOUT;
     rt_channel_t ch;
 
-    ch = (rt_channel_t)file->data;
+    ch = (rt_channel_t)file->fnode->data;
     rt_poll_add(&(ch->reader_queue), req);
     if (ch->stat != RT_IPC_STAT_IDLE)
     {
@@ -866,7 +865,7 @@ static int channel_fops_close(struct dfs_fd *file)
     rt_base_t level;
 
     level = rt_hw_interrupt_disable();
-    ch = (rt_channel_t)file->data;
+    ch = (rt_channel_t)file->fnode->data;
     ch->ref--;
     if (ch->ref == 0)
     {
@@ -913,18 +912,17 @@ int lwp_channel_open(int fdt_type, const char *name, int flags)
 
         d = lwp_fd_get(fdt_type, fd);
 
-        d->type = FT_USER;
-        d->path = NULL;
+        d->fnode->type = FT_USER;
+        d->fnode->path = NULL;
 
-        d->fops = &channel_fops;
+        d->fnode->fops = &channel_fops;
 
-        d->flags = O_RDWR; /* set flags as read and write */
-        d->size = 0;
+        d->fnode->flags = O_RDWR; /* set flags as read and write */
+        d->fnode->size = 0;
         d->pos = 0;
 
         /* set socket to the data of dfs_fd */
-        d->data = (void *)ch;
-        lwp_fd_put(fdt_type, d);
+        d->fnode->data = (void *)ch;
     }
     else
     {
@@ -944,8 +942,7 @@ static rt_channel_t fd_2_channel(int fdt_type, int fd)
     {
         rt_channel_t ch;
 
-        ch = (rt_channel_t)d->data;
-        lwp_fd_put(fdt_type, d);
+        ch = (rt_channel_t)d->fnode->data;
         if (ch)
         {
             return ch;

+ 9 - 15
components/lwp/lwp_pid.c

@@ -41,25 +41,19 @@ int libc_stdio_get_console(void);
 
 static void __exit_files(struct rt_lwp *lwp)
 {
-    int consolefd;          /* the console fd, which must not be closed */
+    int fd = lwp->fdt.maxfd - 1;
 
-    consolefd = libc_stdio_get_console();
-    consolefd = consolefd - DFS_FD_OFFSET;
-
-    while (lwp->fdt.maxfd > 0)
+    while (fd >= 0)
     {
-        if (consolefd != lwp->fdt.maxfd - 1)  /* skip the console fd */
-        {
-            struct dfs_fd *d;
+        struct dfs_fd *d;
 
-            d = lwp->fdt.fds[lwp->fdt.maxfd - 1];
-            if (d)
-            {
-                dfs_file_close(d);
-                rt_free(d);
-            }
+        d = lwp->fdt.fds[fd];
+        if (d)
+        {
+            dfs_file_close(d);
+            fdt_fd_release(&lwp->fdt, fd);
         }
-        lwp->fdt.maxfd --;
+        fd--;
     }
 }
 

+ 6 - 6
components/lwp/lwp_syscall.c

@@ -418,7 +418,7 @@ ssize_t sys_read(int fd, void *buf, size_t nbyte)
     }
 
     ret = read(fd, kmem, nbyte);
-    if (ret)
+    if (ret > 0)
     {
         lwp_put_to_user(buf, kmem, ret);
     }
@@ -513,10 +513,6 @@ int sys_open(const char *name, int flag, ...)
 /* syscall: "close" ret: "int" args: "int" */
 int sys_close(int fd)
 {
-    if ((0 <= fd) && (fd <= DFS_FD_OFFSET))
-    {
-        return 0;
-    }
     return close(fd);
 }
 
@@ -2341,7 +2337,6 @@ int sys_getdents(int fd, struct libc_dirent *dirp, size_t nbytes)
     }
     dfs_fd = fd_get(fd);
     ret = dfs_file_getdents(dfs_fd, rtt_dirp, nbytes);
-    fd_put(dfs_fd);
     if (ret)
     {
         size_t i = 0;
@@ -2516,6 +2511,9 @@ int sys_clock_getres(clockid_t clk, struct timespec *ts)
 int sys_futex(int *uaddr, int op, int val, void *timeout, void *uaddr2, int val3);
 int sys_pmutex(void *umutex, int op, void *arg);
 
+int sys_dup(int oldfd);
+int sys_dup2(int oldfd, int new);
+
 const static void* func_table[] =
 {
     (void*)sys_exit,            /* 01 */
@@ -2666,6 +2664,8 @@ const static void* func_table[] =
     (void *)sys_clone,           /* 130 */
     (void *)sys_futex,
     (void *)sys_pmutex,
+    (void *)sys_dup,
+    (void *)sys_dup2,
 };
 
 const void *lwp_get_sys_api(rt_uint32_t number)

+ 8 - 9
components/net/sal_socket/dfs_net/dfs_net.c

@@ -21,42 +21,41 @@
 int dfs_net_getsocket(int fd)
 {
     int socket;
-    struct dfs_fd *_dfs_fd; 
+    struct dfs_fd *_dfs_fd;
 
     _dfs_fd = fd_get(fd);
     if (_dfs_fd == NULL) return -1;
 
-    if (_dfs_fd->type != FT_SOCKET) socket = -1;
-    else socket = (int)_dfs_fd->data;
+    if (_dfs_fd->fnode->type != FT_SOCKET) socket = -1;
+    else socket = (int)_dfs_fd->fnode->data;
 
-    fd_put(_dfs_fd); /* put this dfs fd */
     return socket;
 }
 
 static int dfs_net_ioctl(struct dfs_fd* file, int cmd, void* args)
 {
-    int socket = (int) file->data;
+    int socket = (int) file->fnode->data;
 
     return sal_ioctlsocket(socket, cmd, args);
 }
 
 static int dfs_net_read(struct dfs_fd* file, void *buf, size_t count)
 {
-    int socket = (int) file->data;
+    int socket = (int) file->fnode->data;
 
     return sal_recvfrom(socket, buf, count, 0, NULL, NULL);
 }
 
 static int dfs_net_write(struct dfs_fd *file, const void *buf, size_t count)
 {
-    int socket = (int) file->data;
+    int socket = (int) file->fnode->data;
 
     return sal_sendto(socket, buf, count, 0, NULL, 0);
 }
 
 static int dfs_net_close(struct dfs_fd* file)
 {
-    int socket = (int) file->data;
+    int socket = (int) file->fnode->data;
 
     return sal_closesocket(socket);
 }
@@ -68,7 +67,7 @@ static int dfs_net_poll(struct dfs_fd *file, struct rt_pollreq *req)
     return sal_poll(file, req);
 }
 
-const struct dfs_file_ops _net_fops = 
+const struct dfs_file_ops _net_fops =
 {
     NULL,    /* open     */
     dfs_net_close,

+ 1 - 1
components/net/sal_socket/impl/af_inet_lwip.c

@@ -243,7 +243,7 @@ static int inet_poll(struct dfs_fd *file, struct rt_pollreq *req)
     struct lwip_sock *sock;
     struct sal_socket *sal_sock;
 
-    sal_sock = sal_get_socket((int) file->data);
+    sal_sock = sal_get_socket((int) file->fnode->data);
     if(!sal_sock)
     {
         return -1;

+ 15 - 25
components/net/sal_socket/socket/net_sockets.c

@@ -41,20 +41,17 @@ int accept(int s, struct sockaddr *addr, socklen_t *addrlen)
         if(d)
         {
             /* this is a socket fd */
-            d->type = FT_SOCKET;
-            d->path = NULL;
+            d->fnode->type = FT_SOCKET;
+            d->fnode->path = NULL;
 
-            d->fops = dfs_net_get_fops();
+            d->fnode->fops = dfs_net_get_fops();
 
-            d->flags = O_RDWR; /* set flags as read and write */
-            d->size = 0;
+            d->fnode->flags = O_RDWR; /* set flags as read and write */
+            d->fnode->size = 0;
             d->pos = 0;
 
             /* set socket to the data of dfs_fd */
-            d->data = (void *) new_socket;
-
-            /* release the ref-count of fd */
-            fd_put(d);
+            d->fnode->data = (void *) new_socket;
 
             return fd;
         }
@@ -95,7 +92,7 @@ int shutdown(int s, int how)
         rt_set_errno(-EBADF);
         return -1;
     }
-    
+
     if (sal_shutdown(socket, how) == 0)
     {
         error = 0;
@@ -105,8 +102,6 @@ int shutdown(int s, int how)
         rt_set_errno(-ENOTSOCK);
         error = -1;
     }
-    
-    fd_put(d);
 
     return error;
 }
@@ -216,32 +211,28 @@ int socket(int domain, int type, int protocol)
     if (socket >= 0)
     {
         /* this is a socket fd */
-        d->type = FT_SOCKET;
-        d->path = NULL;
+        d->fnode->type = FT_SOCKET;
+        d->fnode->path = NULL;
 
-        d->fops = dfs_net_get_fops();
+        d->fnode->fops = dfs_net_get_fops();
 
-        d->flags = O_RDWR; /* set flags as read and write */
-        d->size = 0;
+        d->fnode->flags = O_RDWR; /* set flags as read and write */
+        d->fnode->size = 0;
         d->pos = 0;
 
         /* set socket to the data of dfs_fd */
-        d->data = (void *) socket;
+        d->fnode->data = (void *) socket;
     }
     else
     {
         /* release fd */
-        fd_put(d);
-        fd_put(d);
+        fd_release(fd);
 
         rt_set_errno(-ENOMEM);
 
         return -1;
     }
 
-    /* release the ref-count of fd */
-    fd_put(d);
-
     return fd;
 }
 RTM_EXPORT(socket);
@@ -277,8 +268,7 @@ int closesocket(int s)
     }
 
     /* socket has been closed, delete it from file system fd */
-    fd_put(d);
-    fd_put(d);
+    fd_release(s);
 
     return error;
 }

+ 1 - 1
components/net/sal_socket/src/sal_socket.c

@@ -1020,7 +1020,7 @@ int sal_poll(struct dfs_fd *file, struct rt_pollreq *req)
 {
     struct sal_socket *sock;
     struct sal_proto_family *pf;
-    int socket = (int) file->data;
+    int socket = (int) file->fnode->data;
 
     /* get the socket object by socket descriptor */
     SAL_SOCKET_OBJ_GET(sock, socket);