Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 18 additions & 32 deletions components/dfs/dfs_v2/filesystems/devfs/devtmpfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <dfs_dentry.h>
#include <dfs_file.h>
#include <dfs_mnt.h>
#include <dfs_vfs.h>
#include <devfs.h>
#include <unistd.h>

Expand All @@ -34,10 +35,9 @@ struct devtmpfs_file
char name[DIRENT_NAME_MAX]; /* file name */

rt_uint32_t type; /* file type */
rt_list_t subdirs; /* file subdir list */
rt_list_t sibling; /* file sibling list */
struct dfs_vfs_node node; /* file node in the devtmpfs */

struct devtmpfs_sb *sb; /* superblock ptr */
struct devtmpfs_sb *sb; /* superblock ptr */

rt_uint32_t mode;
char *link;
Expand All @@ -48,7 +48,6 @@ struct devtmpfs_sb
rt_uint32_t magic; /* TMPFS_MAGIC */
struct devtmpfs_file root; /* root dir */
rt_size_t df_size; /* df size */
rt_list_t sibling; /* sb sibling list */
struct rt_spinlock lock; /* tmpfs lock */
};

Expand Down Expand Up @@ -111,15 +110,13 @@ static int _get_subdir(const char *path, char *name)
#if 0
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这块代码本身也有些问题,不应该有#if 0的代码的。这次提交也一起拿掉吧

static int _free_subdir(struct devtmpfs_file *dfile)
{
struct devtmpfs_file *file;
rt_list_t *list, *temp_list;
struct devtmpfs_file *file, *tmp;
struct devtmpfs_sb *superblock;

RT_ASSERT(dfile->type == TMPFS_TYPE_DIR);

rt_list_for_each_safe(list, temp_list, &dfile->subdirs)
dfs_vfs_for_each_subnode(file, tmp, dfile, node)
{
file = rt_list_entry(list, struct devtmpfs_file, sibling);
if (file->type == TMPFS_TYPE_DIR)
{
_free_subdir(file);
Expand All @@ -134,7 +131,7 @@ static int _free_subdir(struct devtmpfs_file *dfile)
RT_ASSERT(superblock);

rt_spin_lock(&superblock->lock);
rt_list_remove(&(file->sibling));
dfs_vfs_remove_node(&file->node);
rt_spin_unlock(&superblock->lock);

rt_free(file);
Expand All @@ -152,14 +149,12 @@ static int devtmpfs_mount(struct dfs_mnt *mnt, unsigned long rwflag, const void
{
superblock->df_size = sizeof(struct devtmpfs_sb);
superblock->magic = TMPFS_MAGIC;
rt_list_init(&superblock->sibling);

superblock->root.name[0] = '/';
superblock->root.sb = superblock;
superblock->root.type = TMPFS_TYPE_DIR;
superblock->root.mode = S_IFDIR | (S_IRUSR | S_IRGRP | S_IROTH) | (S_IXUSR | S_IXGRP | S_IXOTH);
rt_list_init(&superblock->root.sibling);
rt_list_init(&superblock->root.subdirs);
dfs_vfs_init_node(&superblock->root.node);

rt_spin_lock_init(&superblock->lock);

Expand Down Expand Up @@ -193,8 +188,7 @@ static struct devtmpfs_file *devtmpfs_file_lookup(struct devtmpfs_sb *superblock
{
const char *subpath, *curpath, *filename = RT_NULL;
char subdir_name[DIRENT_NAME_MAX];
struct devtmpfs_file *file, *curfile;
rt_list_t *list;
struct devtmpfs_file *file, *curfile, *tmp;

subpath = path;
while (*subpath == '/' && *subpath)
Expand Down Expand Up @@ -222,9 +216,8 @@ static struct devtmpfs_file *devtmpfs_file_lookup(struct devtmpfs_sb *superblock

rt_spin_lock(&superblock->lock);

rt_list_for_each(list, &curfile->subdirs)
dfs_vfs_for_each_subnode(file, tmp, curfile, node)
{
file = rt_list_entry(list, struct devtmpfs_file, sibling);
if (filename) /* find file */
{
if (rt_strcmp(file->name, filename) == 0)
Expand Down Expand Up @@ -293,7 +286,9 @@ static int devtmpfs_stat(struct dfs_dentry *dentry, struct stat *st)

static int devtmpfs_getdents(struct dfs_file *file, struct dirent *dirp, uint32_t count)
{
struct devtmpfs_file *d_file;
rt_size_t index, end;
struct dirent *d;
struct devtmpfs_file *d_file, *n_file = RT_NULL, *tmp;
struct devtmpfs_sb *superblock;

RT_ASSERT(file);
Expand All @@ -306,11 +301,6 @@ static int devtmpfs_getdents(struct dfs_file *file, struct dirent *dirp, uint32_
d_file = devtmpfs_file_lookup(superblock, file->dentry->pathname);
if (d_file)
{
rt_size_t index, end;
struct dirent *d;
struct devtmpfs_file *n_file;
rt_list_t *list;

/* make integer count */
count = (count / sizeof(struct dirent));
if (count == 0)
Expand All @@ -322,12 +312,10 @@ static int devtmpfs_getdents(struct dfs_file *file, struct dirent *dirp, uint32_
index = 0;
count = 0;

rt_list_for_each(list, &d_file->subdirs)
dfs_vfs_for_each_subnode(n_file, tmp, d_file, node)
{
if (index >= (rt_size_t)file->fpos)
{
n_file = rt_list_entry(list, struct devtmpfs_file, sibling);

d = dirp + count;
if (n_file->type == TMPFS_TYPE_FILE)
{
Expand Down Expand Up @@ -378,8 +366,7 @@ static int devtmpfs_symlink(struct dfs_dentry *parent_dentry, const char *target

strncpy(l_file->name, linkpath, DIRENT_NAME_MAX - 1);

rt_list_init(&(l_file->subdirs));
rt_list_init(&(l_file->sibling));
dfs_vfs_init_node(&l_file->node);
l_file->sb = superblock;
l_file->type = TMPFS_TYPE_FILE;
l_file->mode = p_file->mode;
Expand All @@ -388,7 +375,7 @@ static int devtmpfs_symlink(struct dfs_dentry *parent_dentry, const char *target
l_file->link = rt_strdup(target);

rt_spin_lock(&superblock->lock);
rt_list_insert_after(&(p_file->subdirs), &(l_file->sibling));
dfs_vfs_append_node(&p_file->node, &l_file->node);
rt_spin_unlock(&superblock->lock);
}
}
Expand Down Expand Up @@ -460,7 +447,7 @@ static int devtmpfs_unlink(struct dfs_dentry *dentry)
}

rt_spin_lock(&superblock->lock);
rt_list_remove(&(d_file->sibling));
dfs_vfs_remove_node(&d_file->node);
rt_spin_unlock(&superblock->lock);

rt_free(d_file);
Expand Down Expand Up @@ -537,8 +524,7 @@ static struct dfs_vnode *devtmpfs_create_vnode(struct dfs_dentry *dentry, int ty

strncpy(d_file->name, file_name, DIRENT_NAME_MAX);

rt_list_init(&(d_file->subdirs));
rt_list_init(&(d_file->sibling));
dfs_vfs_init_node(&d_file->node);
d_file->sb = superblock;

vnode->nlink = 1;
Expand All @@ -563,7 +549,7 @@ static struct dfs_vnode *devtmpfs_create_vnode(struct dfs_dentry *dentry, int ty
d_file->mode = vnode->mode;

rt_spin_lock(&superblock->lock);
rt_list_insert_after(&(p_file->subdirs), &(d_file->sibling));
dfs_vfs_append_node(&p_file->node, &d_file->node);
rt_spin_unlock(&superblock->lock);
}

Expand Down
35 changes: 13 additions & 22 deletions components/dfs/dfs_v2/filesystems/tmpfs/dfs_tmpfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,13 @@ static int _get_subdir(const char *path, char *name)

static int _free_subdir(struct tmpfs_file *dfile)
{
struct tmpfs_file *file;
rt_list_t *list, *temp_list;
struct tmpfs_file *file = RT_NULL, *tmp;
struct tmpfs_sb *superblock;

RT_ASSERT(dfile->type == TMPFS_TYPE_DIR);

rt_list_for_each_safe(list, temp_list, &dfile->subdirs)
dfs_vfs_for_each_subnode(file, tmp, dfile, node)
{
file = rt_list_entry(list, struct tmpfs_file, sibling);
if (file->type == TMPFS_TYPE_DIR)
{
_free_subdir(file);
Expand All @@ -122,7 +120,7 @@ static int _free_subdir(struct tmpfs_file *dfile)
RT_ASSERT(superblock != NULL);

rt_spin_lock(&superblock->lock);
rt_list_remove(&(file->sibling));
dfs_vfs_remove_node(&file->node);
rt_spin_unlock(&superblock->lock);

rt_free(file);
Expand All @@ -141,13 +139,11 @@ static int dfs_tmpfs_mount(struct dfs_mnt *mnt,
{
superblock->df_size = sizeof(struct tmpfs_sb);
superblock->magic = TMPFS_MAGIC;
rt_list_init(&superblock->sibling);

superblock->root.name[0] = '/';
superblock->root.sb = superblock;
superblock->root.type = TMPFS_TYPE_DIR;
rt_list_init(&superblock->root.sibling);
rt_list_init(&superblock->root.subdirs);
dfs_vfs_init_node(&superblock->root.node);

rt_spin_lock_init(&superblock->lock);

Expand Down Expand Up @@ -236,8 +232,7 @@ struct tmpfs_file *dfs_tmpfs_lookup(struct tmpfs_sb *superblock,
{
const char *subpath, *curpath, *filename = RT_NULL;
char subdir_name[TMPFS_NAME_MAX];
struct tmpfs_file *file, *curfile;
rt_list_t *list;
struct tmpfs_file *file, *curfile, *tmp;

subpath = path;
while (*subpath == '/' && *subpath)
Expand Down Expand Up @@ -265,9 +260,8 @@ struct tmpfs_file *dfs_tmpfs_lookup(struct tmpfs_sb *superblock,

rt_spin_lock(&superblock->lock);

rt_list_for_each(list, &curfile->subdirs)
dfs_vfs_for_each_subnode(file, tmp, curfile, node)
{
file = rt_list_entry(list, struct tmpfs_file, sibling);
if (filename) /* find file */
{
if (rt_strcmp(file->name, filename) == 0)
Expand Down Expand Up @@ -503,8 +497,7 @@ static int dfs_tmpfs_getdents(struct dfs_file *file,
{
rt_size_t index, end;
struct dirent *d;
struct tmpfs_file *d_file, *n_file;
rt_list_t *list;
struct tmpfs_file *d_file, *n_file, *tmp;
struct tmpfs_sb *superblock;

d_file = (struct tmpfs_file *)file->vnode->data;
Expand All @@ -527,9 +520,8 @@ static int dfs_tmpfs_getdents(struct dfs_file *file,
index = 0;
count = 0;

rt_list_for_each(list, &d_file->subdirs)
dfs_vfs_for_each_subnode(n_file, tmp, d_file, node)
{
n_file = rt_list_entry(list, struct tmpfs_file, sibling);
if (index >= (rt_size_t)file->fpos)
{
d = dirp + count;
Expand Down Expand Up @@ -573,7 +565,7 @@ static int dfs_tmpfs_unlink(struct dfs_dentry *dentry)
return -ENOENT;

rt_spin_lock(&superblock->lock);
rt_list_remove(&(d_file->sibling));
dfs_vfs_remove_node(&d_file->node);
rt_spin_unlock(&superblock->lock);

if (rt_atomic_load(&(dentry->ref_count)) == 1)
Expand Down Expand Up @@ -631,13 +623,13 @@ static int dfs_tmpfs_rename(struct dfs_dentry *old_dentry, struct dfs_dentry *ne
RT_ASSERT(p_file != NULL);

rt_spin_lock(&superblock->lock);
rt_list_remove(&(d_file->sibling));
dfs_vfs_remove_node(&d_file->node);
rt_spin_unlock(&superblock->lock);

strncpy(d_file->name, file_name, TMPFS_NAME_MAX);

rt_spin_lock(&superblock->lock);
rt_list_insert_after(&(p_file->subdirs), &(d_file->sibling));
dfs_vfs_append_node(&p_file->node, &d_file->node);
rt_spin_unlock(&superblock->lock);

rt_free(parent_path);
Expand Down Expand Up @@ -745,8 +737,7 @@ static struct dfs_vnode *dfs_tmpfs_create_vnode(struct dfs_dentry *dentry, int t

strncpy(d_file->name, file_name, TMPFS_NAME_MAX);

rt_list_init(&(d_file->subdirs));
rt_list_init(&(d_file->sibling));
dfs_vfs_init_node(&d_file->node);
d_file->data = NULL;
d_file->size = 0;
d_file->sb = superblock;
Expand All @@ -767,7 +758,7 @@ static struct dfs_vnode *dfs_tmpfs_create_vnode(struct dfs_dentry *dentry, int t
#endif
}
rt_spin_lock(&superblock->lock);
rt_list_insert_after(&(p_file->subdirs), &(d_file->sibling));
dfs_vfs_append_node(&p_file->node, &d_file->node);
rt_spin_unlock(&superblock->lock);

vnode->mnt = dentry->mnt;
Expand Down
4 changes: 2 additions & 2 deletions components/dfs/dfs_v2/filesystems/tmpfs/dfs_tmpfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#define __DFS_TMPFS_H__

#include <rtthread.h>
#include <dfs_vfs.h>

#define TMPFS_NAME_MAX 32
#define TMPFS_MAGIC 0x0B0B0B0B
Expand All @@ -25,8 +26,7 @@ struct tmpfs_file
{
rt_uint32_t type; /* file type */
char name[TMPFS_NAME_MAX]; /* file name */
rt_list_t subdirs; /* file subdir list */
rt_list_t sibling; /* file sibling list */
struct dfs_vfs_node node; /* file node in the tmpfs */
struct tmpfs_sb *sb; /* superblock ptr */
rt_uint8_t *data; /* file date ptr */
rt_size_t size; /* file size */
Expand Down
50 changes: 50 additions & 0 deletions components/dfs/dfs_v2/include/dfs_vfs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright (c) 2006-2024, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
*/

#ifndef __DFS_VFS_H__
#define __DFS_VFS_H__

#include "dfs_file.h"
#include "dfs_fs.h"

#ifdef __cplusplus
extern "C"
{
#endif

struct dfs_vfs_node
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dfs = vfs,这样就变得重复了

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个目的是抽出一个通用的虚拟文件系统管理层。逻辑是很多调试、系统管理用的文件系统,devfs、procfs、sysfs、debugfs 等本质都是基于 tmpfs 衍生出来的,但现在 devfs、tmpfs、procfs 都有单独的文件管理实现。这不利于维护。所以希望单独抽出一个层次的抽象,专注于虚拟文件系统的文件与目录管理。

命名确实可以再讨论一下。比如 dfs_libvfs 这样是否可以。

Copy link
Copy Markdown
Contributor Author

@zmshahaha zmshahaha Jul 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

那我改libvfs吗?

{
rt_list_t subnode; /* file subnode list */
rt_list_t sibling; /* file sibling list */
};

rt_inline void dfs_vfs_init_node(struct dfs_vfs_node *node)
{
rt_list_init(&node->subnode);
rt_list_init(&node->sibling);
}

rt_inline void dfs_vfs_append_node(struct dfs_vfs_node *dir, struct dfs_vfs_node *node)
{
rt_list_insert_after(&(dir->subnode), &(node->sibling));
}

rt_inline void dfs_vfs_remove_node(struct dfs_vfs_node *node)
{
rt_list_remove(&(node->sibling));
}

#define dfs_vfs_for_each_subnode(node, tmp, dir, member) \
rt_list_for_each_entry_safe(node, tmp, &dir->member.subnode, member.sibling)

#ifdef __cplusplus
}
#endif

#endif /*__DFS_VFS_H__*/