Przeglądaj źródła

dfs修改: fnode的申请释放改由dfs的open和close层负责

shaojinchun 5 lat temu
rodzic
commit
19329f1759

+ 1 - 14
components/dfs/src/dfs.c

@@ -184,23 +184,14 @@ static int fd_alloc(struct dfs_fdtable *fdt, int startfd)
     /* allocate  'struct dfs_fd' */
     if (idx < (int)fdt->maxfd && fdt->fds[idx] == RT_NULL)
     {
-        struct dfs_fnode *fnode = rt_calloc(1, sizeof(struct dfs_fnode));
-
-        if (!fnode)
-        {
-            return fdt->maxfd;
-        }
-        fnode->ref_count = 1;
-
         fd = (struct dfs_fd *)rt_calloc(1, sizeof(struct dfs_fd));
         if (!fd)
         {
-            rt_free(fnode);
             return fdt->maxfd;
         }
         fd->ref_count = 1;
         fd->magic = DFS_FD_MAGIC;
-        fd->fnode = fnode;
+        fd->fnode = NULL;
         fd->idx = idx;
         fdt->fds[idx] = fd;
     }
@@ -321,10 +312,6 @@ void fdt_fd_release(struct dfs_fdtable* fdt, struct dfs_fd *fd)
         if (fnode)
         {
             fnode->ref_count--;
-            if (fnode->ref_count == 0)
-            {
-                rt_free(fnode);
-            }
         }
         rt_free(fd);
         fdt->fds[idx] = 0;

+ 16 - 2
components/dfs/src/dfs_file.c

@@ -35,6 +35,7 @@ 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;
 
     /* parameter check */
     if (fd == NULL)
@@ -58,6 +59,14 @@ int dfs_file_open(struct dfs_fd *fd, const char *path, int flags)
         return -ENOENT;
     }
 
+    fnode = rt_calloc(1, sizeof(struct dfs_fnode));
+    if (!fnode)
+    {
+        return -ENOMEM;
+    }
+    fnode->ref_count = 1;
+    fd->fnode = fnode;
+
     LOG_D("open in filesystem:%s", fs->ops->name);
     fd->fnode->fs    = fs;             /* set file system */
     fd->fnode->fops  = fs->ops->fops;  /* set file ops */
@@ -89,6 +98,8 @@ int dfs_file_open(struct dfs_fd *fd, const char *path, int flags)
         /* clear fd */
         rt_free(fd->fnode->path);
         fd->fnode->path = NULL;
+        rt_free(fd->fnode);
+        fd->fnode = NULL;
 
         return -ENOSYS;
     }
@@ -98,6 +109,8 @@ int dfs_file_open(struct dfs_fd *fd, const char *path, int flags)
         /* clear fd */
         rt_free(fd->fnode->path);
         fd->fnode->path = NULL;
+        rt_free(fd->fnode);
+        fd->fnode = NULL;
 
         LOG_D("%s open failed", fullpath);
 
@@ -138,6 +151,8 @@ int dfs_file_close(struct dfs_fd *fd)
 
     rt_free(fd->fnode->path);
     fd->fnode->path = NULL;
+    rt_free(fd->fnode);
+    fd->fnode = NULL;
 
     return result;
 }
@@ -512,8 +527,7 @@ int dfs_file_ftruncate(struct dfs_fd *fd, off_t length)
 #ifdef RT_USING_FINSH
 #include <finsh.h>
 
-static struct dfs_fnode fnode;
-static struct dfs_fd fd = {0, 0, 0, 0, &fnode};
+static struct dfs_fd fd;
 static struct dirent dirent;
 void ls(const char *pathname)
 {

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

@@ -272,9 +272,6 @@ int dfs_mount(const char   *device_name,
     if ((strcmp(fullpath, "/") != 0) && (strcmp(fullpath, "/dev") != 0))
     {
         struct dfs_fd fd;
-        struct dfs_fnode fnode;
-
-        fd.fnode = &fnode;
 
         if (dfs_file_open(&fd, fullpath, O_RDONLY | O_DIRECTORY) < 0)
         {