Просмотр исходного кода

dfs_fd结构分成两层,增加一级dfs_fnode结构体,
所有文件系统改为使用fnode->fs来取初始文件系统

shaojinchun 5 лет назад
Родитель
Сommit
9c96c85e1a

+ 18 - 18
components/dfs/filesystems/cromfs/dfs_cromfs.c

@@ -640,14 +640,14 @@ static int dfs_cromfs_read(struct dfs_fd *file, void *buf, size_t count)
     cromfs_info *ci;
     uint32_t length;
 
-    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)
     {
@@ -690,7 +690,7 @@ 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;
@@ -784,8 +784,8 @@ static int dfs_cromfs_close(struct dfs_fd *file)
     cromfs_info *ci;
     rt_err_t result;
 
-    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);
@@ -793,7 +793,7 @@ static int dfs_cromfs_close(struct dfs_fd *file)
         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;
 }
 
@@ -808,13 +808,13 @@ static int dfs_cromfs_open(struct dfs_fd *file)
     int is_dir;
     rt_err_t result;
 
-    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;
 
-    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 +824,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 +833,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 +859,11 @@ 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;
@@ -915,7 +915,7 @@ static int dfs_cromfs_getdents(struct dfs_fd *file, struct dirent *dirp, uint32_
     void *di_mem;
     rt_err_t result;
 
-    fi = (file_info *)file->data;
+    fi = (file_info *)file->fnode->data;
     ci = fi->ci;
 
     RT_ASSERT(fi->buff == NULL);
@@ -951,7 +951,7 @@ 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;
 

+ 20 - 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,12 +165,12 @@ 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;
 
@@ -178,16 +178,16 @@ int dfs_device_fs_open(struct dfs_fd *file)
     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 +198,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 +264,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 */

+ 19 - 19
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)
@@ -344,16 +344,16 @@ int dfs_elm_open(struct dfs_fd *file)
     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,18 +392,18 @@ 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 */
@@ -423,10 +423,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,7 +449,7 @@ 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;
 
@@ -459,7 +459,7 @@ 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;
         fd = (FIL *)(file->data);
@@ -514,7 +514,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 +537,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,7 +548,7 @@ 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;
 
@@ -570,7 +570,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,7 +586,7 @@ 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;

+ 25 - 25
components/dfs/filesystems/jffs2/dfs_jffs2.c

@@ -215,8 +215,8 @@ 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));
@@ -224,7 +224,7 @@ static int dfs_jffs2_open(struct dfs_fd* file)
         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 '/' */
@@ -267,7 +267,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 +292,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,10 +313,10 @@ 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);
@@ -352,8 +352,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;
@@ -384,8 +384,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;
@@ -414,13 +414,13 @@ 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);
@@ -451,8 +451,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
@@ -494,15 +494,15 @@ 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);

+ 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

+ 27 - 27
components/dfs/filesystems/nfs/dfs_nfs.c

@@ -559,12 +559,12 @@ 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 +629,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,7 +676,7 @@ 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);
     }
@@ -692,11 +692,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 +714,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 +727,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 +744,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 +770,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 +781,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 +798,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;
@@ -1086,8 +1086,8 @@ int nfs_getdents(struct dfs_fd *file, struct dirent *dirp, uint32_t count)
     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);

+ 22 - 22
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,7 +225,7 @@ 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 ++;
                 strncpy(dirent->name, name_ptr, RAMFS_NAME_MAX);
@@ -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 - 14
components/dfs/filesystems/romfs/dfs_romfs.c

@@ -133,7 +133,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 +141,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 +157,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 +168,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 +179,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 +247,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 +261,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 */

+ 2 - 2
components/dfs/include/dfs.h

@@ -105,8 +105,8 @@ int fdt_fd_new(struct dfs_fdtable *fdt);
 struct dfs_fd *fdt_fd_get(struct dfs_fdtable* fdt, int fd, int ref_inc_nr);
 void fdt_fd_release(struct dfs_fdtable* fdt, struct dfs_fd *fd);
 int fd_new(void);
-struct dfs_fd *dfs_fd_get(int fd);
-void dfs_fd_release(struct dfs_fd *fd);
+struct dfs_fd *fd_get(int fd, int ref_inc_nr);
+void fd_release(struct dfs_fd *fd);
 int fd_is_open(const char *pathname);
 
 struct dfs_fdtable *dfs_fdtable_get(void);

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

@@ -36,9 +36,9 @@ 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) */
@@ -49,11 +49,19 @@ struct dfs_fd
 
     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
+{
+    int idx;                    /* the idx in fdt */
+    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 */
+};
+
 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);

+ 60 - 37
components/dfs/src/dfs.c

@@ -142,7 +142,8 @@ void dfs_fd_unlock(void)
 
 static int fd_alloc(struct dfs_fdtable *fdt, int startfd)
 {
-    int idx;
+    int idx = 0;
+    struct dfs_fd *fd = NULL;
 
     /* find an empty fd entry */
     for (idx = startfd; idx < (int)fdt->maxfd; idx++)
@@ -156,15 +157,19 @@ static int fd_alloc(struct dfs_fdtable *fdt, int startfd)
     /* allocate a larger FD container */
     if (idx == fdt->maxfd && fdt->maxfd < DFS_FD_MAX)
     {
-        int cnt, index;
-        struct dfs_fd **fds;
+        int cnt = 0;
+        int index = 0;
+        struct dfs_fd **fds = NULL;
 
         /* increase the number of FD with 4 step length */
         cnt = fdt->maxfd + 4;
         cnt = cnt > DFS_FD_MAX ? DFS_FD_MAX : cnt;
 
         fds = (struct dfs_fd **)rt_realloc(fdt->fds, cnt * sizeof(struct dfs_fd *));
-        if (fds == NULL) goto __exit; /* return fdt->maxfd */
+        if (fds == NULL) /* return fdt->maxfd */
+        {
+            return fdt->maxfd;
+        }
 
         /* clean the new allocated fds */
         for (index = fdt->maxfd; index < cnt; index ++)
@@ -179,12 +184,27 @@ static int fd_alloc(struct dfs_fdtable *fdt, int startfd)
     /* allocate  'struct dfs_fd' */
     if (idx < (int)fdt->maxfd && fdt->fds[idx] == RT_NULL)
     {
-        fdt->fds[idx] = (struct dfs_fd *)rt_calloc(1, sizeof(struct dfs_fd));
-        if (fdt->fds[idx] == RT_NULL)
-            idx = fdt->maxfd;
+        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->idx = idx;
+        fdt->fds[idx] = fd;
     }
 
-__exit:
     return idx;
 }
 
@@ -196,8 +216,7 @@ __exit:
  */
 int fdt_fd_new(struct dfs_fdtable *fdt)
 {
-    struct dfs_fd *d;
-    int idx;
+    int idx = 0;
 
     /* lock filesystem */
     dfs_fd_lock();
@@ -213,10 +232,6 @@ int fdt_fd_new(struct dfs_fdtable *fdt)
         goto __result;
     }
 
-    d = fdt->fds[idx];
-    d->ref_count = 1;
-    d->magic = DFS_FD_MAGIC;
-
 __result:
     dfs_fd_unlock();
     return idx + DFS_FD_OFFSET;
@@ -224,7 +239,7 @@ __result:
 
 int fd_new(void)
 {
-    struct dfs_fdtable *fdt;
+    struct dfs_fdtable *fdt = NULL;
 
     fdt = dfs_fdtable_get();
     return fdt_fd_new(fdt);
@@ -270,12 +285,12 @@ struct dfs_fd *fdt_fd_get(struct dfs_fdtable* fdt, int fd, int ref_inc_nr)
     return d;
 }
 
-struct dfs_fd *dfs_fd_get(int fd)
+struct dfs_fd *fd_get(int fd, int ref_inc_nr)
 {
     struct dfs_fdtable *fdt;
 
     fdt = dfs_fdtable_get();
-    return fdt_fd_get(fdt, fd, 0);
+    return fdt_fd_get(fdt, fd, ref_inc_nr);
 }
 
 /**
@@ -285,31 +300,39 @@ struct dfs_fd *dfs_fd_get(int fd)
  */
 void fdt_fd_release(struct dfs_fdtable* fdt, struct dfs_fd *fd)
 {
+    int idx = -1;
+
     RT_ASSERT(fd != NULL);
+    RT_ASSERT(fdt != NULL);
 
     dfs_fd_lock();
 
-    fd->ref_count --;
+    idx = fd->idx;
+    /* check fd */
+    RT_ASSERT(fdt->fds[idx] == fd);
+    RT_ASSERT(fd->magic == DFS_FD_MAGIC);
+
+    fd->ref_count--;
 
     /* clear this fd entry */
     if (fd->ref_count == 0)
     {
-        int index;
-
-        for (index = 0; index < (int)fdt->maxfd; index ++)
+        struct dfs_fnode *fnode = fd->fnode;
+        if (fnode)
         {
-            if (fdt->fds[index] == fd)
+            fnode->ref_count--;
+            if (fnode->ref_count == 0)
             {
-                rt_free(fd);
-                fdt->fds[index] = 0;
-                break;
+                rt_free(fnode);
             }
         }
+        rt_free(fd);
+        fdt->fds[idx] = 0;
     }
     dfs_fd_unlock();
 }
 
-void dfs_fd_release(struct dfs_fd *fd)
+void fd_release(struct dfs_fd *fd)
 {
     struct dfs_fdtable *fdt;
 
@@ -359,9 +382,9 @@ int fd_is_open(const char *pathname)
         for (index = 0; index < fdt->maxfd; index++)
         {
             fd = fdt->fds[index];
-            if (fd == NULL || fd->fops == NULL || fd->path == NULL) continue;
+            if (fd == NULL || fd->fnode->fops == NULL || fd->fnode->path == NULL) continue;
 
-            if (fd->fs == fs && strcmp(fd->path, mountpath) == 0)
+            if (fd->fnode->fs == fs && strcmp(fd->fnode->path, mountpath) == 0)
             {
                 /* found file in file descriptor table */
                 rt_free(fullpath);
@@ -581,20 +604,20 @@ int list_fd(void)
     {
         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");
+            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
             {

+ 47 - 46
components/dfs/src/dfs_file.c

@@ -59,56 +59,56 @@ int dfs_file_open(struct dfs_fd *fd, const char *path, int flags)
     }
 
     LOG_D("open in filesystem:%s", fs->ops->name);
-    fd->fs    = fs;             /* set file system */
-    fd->fops  = fs->ops->fops;  /* set file ops */
+    fd->fnode->fs    = fs;             /* set file system */
+    fd->fnode->fops  = fs->ops->fops;  /* set file ops */
 
     /* initialize the fd item */
-    fd->type  = FT_REGULAR;
-    fd->flags = flags;
-    fd->size  = 0;
+    fd->fnode->type  = FT_REGULAR;
+    fd->fnode->flags = flags;
+    fd->fnode->size  = 0;
     fd->pos   = 0;
-    fd->data  = fs;
+    fd->fnode->data  = fs;
 
     if (!(fs->ops->flags & DFS_FS_FLAG_FULLPATH))
     {
         if (dfs_subdir(fs->path, fullpath) == NULL)
-            fd->path = rt_strdup("/");
+            fd->fnode->path = rt_strdup("/");
         else
-            fd->path = rt_strdup(dfs_subdir(fs->path, fullpath));
+            fd->fnode->path = rt_strdup(dfs_subdir(fs->path, fullpath));
         rt_free(fullpath);
-        LOG_D("Actual file path: %s", fd->path);
+        LOG_D("Actual file path: %s", fd->fnode->path);
     }
     else
     {
-        fd->path = fullpath;
+        fd->fnode->path = fullpath;
     }
 
     /* specific file system open routine */
-    if (fd->fops->open == NULL)
+    if (fd->fnode->fops->open == NULL)
     {
         /* clear fd */
-        rt_free(fd->path);
-        fd->path = NULL;
+        rt_free(fd->fnode->path);
+        fd->fnode->path = NULL;
 
         return -ENOSYS;
     }
 
-    if ((result = fd->fops->open(fd)) < 0)
+    if ((result = fd->fnode->fops->open(fd)) < 0)
     {
         /* clear fd */
-        rt_free(fd->path);
-        fd->path = NULL;
+        rt_free(fd->fnode->path);
+        fd->fnode->path = NULL;
 
         LOG_D("%s open failed", fullpath);
 
         return result;
     }
 
-    fd->flags |= DFS_F_OPEN;
+    fd->fnode->flags |= DFS_F_OPEN;
     if (flags & O_DIRECTORY)
     {
-        fd->type = FT_DIRECTORY;
-        fd->flags |= DFS_F_DIRECTORY;
+        fd->fnode->type = FT_DIRECTORY;
+        fd->fnode->flags |= DFS_F_DIRECTORY;
     }
 
     LOG_D("open successful");
@@ -129,15 +129,15 @@ int dfs_file_close(struct dfs_fd *fd)
     if (fd == NULL)
         return -ENXIO;
 
-    if (fd->fops->close != NULL)
-        result = fd->fops->close(fd);
+    if (fd->fnode->fops->close != NULL)
+        result = fd->fnode->fops->close(fd);
 
     /* close fd error, return */
     if (result < 0)
         return result;
 
-    rt_free(fd->path);
-    fd->path = NULL;
+    rt_free(fd->fnode->path);
+    fd->fnode->path = NULL;
 
     return result;
 }
@@ -157,27 +157,27 @@ int dfs_file_ioctl(struct dfs_fd *fd, int cmd, void *args)
         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;
 }
@@ -199,11 +199,11 @@ int dfs_file_read(struct dfs_fd *fd, void *buf, size_t len)
     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 +220,11 @@ 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 || fd->fnode->type != FT_DIRECTORY)
         return -EINVAL;
 
-    if (fd->fops->getdents != NULL)
-        return fd->fops->getdents(fd, dirp, nbytes);
+    if (fd->fnode->fops->getdents != NULL)
+        return fd->fnode->fops->getdents(fd, dirp, nbytes);
 
     return -ENOSYS;
 }
@@ -296,10 +296,10 @@ 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 +314,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 +335,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)
@@ -494,17 +494,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,7 +512,8 @@ 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 dfs_fnode fnode;
+static struct dfs_fd fd = {0, 0, 0, 0, &fnode};
 static struct dirent dirent;
 void ls(const char *pathname)
 {

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

@@ -272,6 +272,9 @@ 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)
         {

+ 26 - 27
components/dfs/src/dfs_posix.c

@@ -41,13 +41,13 @@ int open(const char *file, int flags, ...)
 
         return -1;
     }
-    d = dfs_fd_get(fd);
+    d = fd_get(fd, 0);
 
     result = dfs_file_open(d, file, flags);
     if (result < 0)
     {
         /* release the ref-count of fd */
-        dfs_fd_release(d);
+        fd_release(d);
 
         rt_set_errno(result);
 
@@ -71,7 +71,7 @@ int close(int fd)
     int result;
     struct dfs_fd *d;
 
-    d = fd_get(fd);
+    d = fd_get(fd, 0);
     if (d == NULL)
     {
         rt_set_errno(-EBADF);
@@ -80,7 +80,6 @@ int close(int fd)
     }
 
     result = dfs_file_close(d);
-    fd_put(d);
 
     if (result < 0)
     {
@@ -89,7 +88,7 @@ int close(int fd)
         return -1;
     }
 
-    fd_put(d);
+    fd_release(d);
 
     return 0;
 }
@@ -116,7 +115,7 @@ int read(int fd, void *buf, size_t len)
     struct dfs_fd *d;
 
     /* get the fd */
-    d = dfs_fd_get(fd);
+    d = fd_get(fd, 0);
     if (d == NULL)
     {
         rt_set_errno(-EBADF);
@@ -156,7 +155,7 @@ int write(int fd, const void *buf, size_t len)
     struct dfs_fd *d;
 
     /* get the fd */
-    d = dfs_fd_get(fd);
+    d = fd_get(fd, 0);
     if (d == NULL)
     {
         rt_set_errno(-EBADF);
@@ -191,7 +190,7 @@ off_t lseek(int fd, off_t offset, int whence)
     int result;
     struct dfs_fd *d;
 
-    d = dfs_fd_get(fd);
+    d = fd_get(fd, 0);
     if (d == NULL)
     {
         rt_set_errno(-EBADF);
@@ -209,7 +208,7 @@ off_t lseek(int fd, off_t offset, int whence)
         break;
 
     case SEEK_END:
-        offset += d->size;
+        offset += d->fnode->size;
         break;
 
     default:
@@ -325,7 +324,7 @@ int fstat(int fildes, struct stat *buf)
     struct dfs_fd *d;
 
     /* get the fd */
-    d = dfs_fd_get(fildes);
+    d = fd_get(fildes, 0);
     if (d == NULL)
     {
         rt_set_errno(-EBADF);
@@ -338,13 +337,13 @@ 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;
 
     return RT_EOK;
@@ -368,7 +367,7 @@ int fsync(int fildes)
     struct dfs_fd *d;
 
     /* get the fd */
-    d = dfs_fd_get(fildes);
+    d = fd_get(fildes, 0);
     if (d == NULL)
     {
         rt_set_errno(-EBADF);
@@ -399,7 +398,7 @@ int fcntl(int fildes, int cmd, ...)
     struct dfs_fd *d;
 
     /* get the fd */
-    d = dfs_fd_get(fildes);
+    d = fd_get(fildes, 0);
     if (d)
     {
         void *arg;
@@ -464,7 +463,7 @@ int ftruncate(int fd, off_t length)
     int result;
     struct dfs_fd *d;
 
-    d = dfs_fd_get(fd);
+    d = fd_get(fd, 0);
     if (d == NULL)
     {
         rt_set_errno(-EBADF);
@@ -537,20 +536,20 @@ int mkdir(const char *path, mode_t mode)
         return -1;
     }
 
-    d = dfs_fd_get(fd);
+    d = fd_get(fd, 0);
 
     result = dfs_file_open(d, path, O_DIRECTORY | O_CREAT);
 
     if (result < 0)
     {
-        dfs_fd_release(d);
+        fd_release(d);
         rt_set_errno(result);
 
         return -1;
     }
 
     dfs_file_close(d);
-    dfs_fd_release(d);
+    fd_release(d);
 
     return 0;
 }
@@ -607,7 +606,7 @@ DIR *opendir(const char *name)
 
         return NULL;
     }
-    d = dfs_fd_get(fd);
+    d = fd_get(fd, 0);
 
     result = dfs_file_open(d, name, O_RDONLY | O_DIRECTORY);
     if (result >= 0)
@@ -617,7 +616,7 @@ DIR *opendir(const char *name)
         if (t == NULL)
         {
             dfs_file_close(d);
-            dfs_fd_release(d);
+            fd_release(d);
         }
         else
         {
@@ -630,7 +629,7 @@ DIR *opendir(const char *name)
     }
 
     /* open failed */
-    dfs_fd_release(d);
+    fd_release(d);
     rt_set_errno(result);
 
     return NULL;
@@ -651,7 +650,7 @@ struct dirent *readdir(DIR *d)
     int result;
     struct dfs_fd *fd;
 
-    fd = dfs_fd_get(d->fd);
+    fd = fd_get(d->fd, 0);
     if (fd == NULL)
     {
         rt_set_errno(-EBADF);
@@ -699,7 +698,7 @@ long telldir(DIR *d)
     struct dfs_fd *fd;
     long result;
 
-    fd = dfs_fd_get(d->fd);
+    fd = fd_get(d->fd, 0);
     if (fd == NULL)
     {
         rt_set_errno(-EBADF);
@@ -724,7 +723,7 @@ void seekdir(DIR *d, off_t offset)
 {
     struct dfs_fd *fd;
 
-    fd = dfs_fd_get(d->fd);
+    fd = fd_get(d->fd, 0);
     if (fd == NULL)
     {
         rt_set_errno(-EBADF);
@@ -748,7 +747,7 @@ void rewinddir(DIR *d)
 {
     struct dfs_fd *fd;
 
-    fd = dfs_fd_get(d->fd);
+    fd = fd_get(d->fd, 0);
     if (fd == NULL)
     {
         rt_set_errno(-EBADF);
@@ -775,7 +774,7 @@ int closedir(DIR *d)
     int result;
     struct dfs_fd *fd;
 
-    fd = dfs_fd_get(d->fd);
+    fd = fd_get(d->fd, 0);
     if (fd == NULL)
     {
         rt_set_errno(-EBADF);
@@ -784,7 +783,7 @@ int closedir(DIR *d)
     }
 
     result = dfs_file_close(fd);
-    dfs_fd_release(fd);
+    fd_release(fd);
 
     rt_free(d);
 

+ 3 - 3
components/dfs/src/poll.c

@@ -128,17 +128,17 @@ static int do_pollfd(struct pollfd *pollfd, rt_pollreq_t *req)
 
     if (fd >= 0)
     {
-        struct dfs_fd *f = dfs_fd_get(fd);
+        struct dfs_fd *f = fd_get(fd, 0);
         mask = POLLNVAL;
 
         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)

+ 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;

+ 11 - 11
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,7 +39,7 @@ 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 ++;
@@ -65,13 +65,13 @@ 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 --;
@@ -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;

+ 7 - 7
components/lwp/lwp_console.c

@@ -214,10 +214,10 @@ static int console_fops_open(struct dfs_fd *fd)
     int ret;
     struct rt_device * device;
 
-    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);
+    ret = rt_device_open(device, fd->fnode->flags);
     return ret;
 }
 
@@ -226,7 +226,7 @@ static int console_fops_close(struct dfs_fd *fd)
     int ret;
     struct rt_device * device;
 
-    device = (struct rt_device *)fd->data;
+    device = (struct rt_device *)fd->fnode->data;
     RT_ASSERT(device != RT_NULL);
 
     ret = rt_device_close(device);
@@ -242,7 +242,7 @@ static int console_fops_read(struct dfs_fd *fd, void *buf, size_t count)
     struct rt_wqueue *wq;
     int wait_ret;
 
-    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 +258,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;
         }
@@ -281,7 +281,7 @@ static int console_fops_write(struct dfs_fd *fd, const void *buf, size_t count)
     int size;
     struct rt_device * device;
 
-    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;
@@ -296,7 +296,7 @@ static int console_fops_poll(struct dfs_fd *fd, struct rt_pollreq *req)
     struct rt_wqueue *wq;
     struct rt_lwp *lwp;
 
-    device = (struct rt_device *)fd->data;
+    device = (struct rt_device *)fd->fnode->data;
     RT_ASSERT(device != RT_NULL);
 
     console = (struct rt_console_device *)device;

+ 9 - 9
components/lwp/lwp_ipc.c

@@ -846,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)
     {
@@ -865,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)
     {
@@ -912,17 +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;
+        d->fnode->data = (void *)ch;
     }
     else
     {
@@ -942,7 +942,7 @@ static rt_channel_t fd_2_channel(int fdt_type, int fd)
     {
         rt_channel_t ch;
 
-        ch = (rt_channel_t)d->data;
+        ch = (rt_channel_t)d->fnode->data;
         if (ch)
         {
             return ch;

+ 1 - 1
components/lwp/lwp_syscall.c

@@ -2339,7 +2339,7 @@ int sys_getdents(int fd, struct libc_dirent *dirp, size_t nbytes)
         rt_set_errno(ENOMEM);
         return -1;
     }
-    dfs_fd = dfs_fd_get(fd);
+    dfs_fd = fd_get(fd, 0);
     ret = dfs_file_getdents(dfs_fd, rtt_dirp, nbytes);
     if (ret)
     {

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

@@ -21,41 +21,41 @@
 int dfs_net_getsocket(int fd)
 {
     int socket;
-    struct dfs_fd *_dfs_fd; 
+    struct dfs_fd *_dfs_fd;
 
-    _dfs_fd = dfs_fd_get(fd);
+    _dfs_fd = fd_get(fd, 0);
     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;
 
     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);
 }
@@ -67,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;

+ 18 - 18
components/net/sal_socket/socket/net_sockets.c

@@ -37,21 +37,21 @@ int accept(int s, struct sockaddr *addr, socklen_t *addrlen)
             return -1;
         }
 
-        d = dfs_fd_get(fd);
+        d = fd_get(fd, 0);
         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;
+            d->fnode->data = (void *) new_socket;
 
             return fd;
         }
@@ -86,7 +86,7 @@ int shutdown(int s, int how)
         return -1;
     }
 
-    d = dfs_fd_get(s);
+    d = fd_get(s, 0);
     if (d == NULL)
     {
         rt_set_errno(-EBADF);
@@ -204,29 +204,29 @@ int socket(int domain, int type, int protocol)
 
         return -1;
     }
-    d = dfs_fd_get(fd);
+    d = fd_get(fd, 0);
 
     /* create socket  and then put it to the dfs_fd */
     socket = sal_socket(domain, type, 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 */
-        dfs_fd_release(d);
+        fd_release(d);
 
         rt_set_errno(-ENOMEM);
 
@@ -250,7 +250,7 @@ int closesocket(int s)
         return -1;
     }
 
-    d = dfs_fd_get(s);
+    d = fd_get(s, 0);
     if (d == RT_NULL)
     {
         rt_set_errno(-EBADF);
@@ -268,7 +268,7 @@ int closesocket(int s)
     }
 
     /* socket has been closed, delete it from file system fd */
-    dfs_fd_release(d);
+    fd_release(d);
 
     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);