Skip to content

Commit 0ae537e

Browse files
authored
[components][dfs]separate dfs fs data structure ops (#9205)
separate dfs fs data structure ops
1 parent 3436aa6 commit 0ae537e

File tree

4 files changed

+83
-56
lines changed

4 files changed

+83
-56
lines changed

components/dfs/dfs_v2/filesystems/devfs/devtmpfs.c

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <dfs_dentry.h>
1919
#include <dfs_file.h>
2020
#include <dfs_mnt.h>
21+
#include <dfs_vfs.h>
2122
#include <devfs.h>
2223
#include <unistd.h>
2324

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

3637
rt_uint32_t type; /* file type */
37-
rt_list_t subdirs; /* file subdir list */
38-
rt_list_t sibling; /* file sibling list */
38+
struct dfs_vfs_node node; /* file node in the devtmpfs */
3939

40-
struct devtmpfs_sb *sb; /* superblock ptr */
40+
struct devtmpfs_sb *sb; /* superblock ptr */
4141

4242
rt_uint32_t mode;
4343
char *link;
@@ -48,7 +48,6 @@ struct devtmpfs_sb
4848
rt_uint32_t magic; /* TMPFS_MAGIC */
4949
struct devtmpfs_file root; /* root dir */
5050
rt_size_t df_size; /* df size */
51-
rt_list_t sibling; /* sb sibling list */
5251
struct rt_spinlock lock; /* tmpfs lock */
5352
};
5453

@@ -111,15 +110,13 @@ static int _get_subdir(const char *path, char *name)
111110
#if 0
112111
static int _free_subdir(struct devtmpfs_file *dfile)
113112
{
114-
struct devtmpfs_file *file;
115-
rt_list_t *list, *temp_list;
113+
struct devtmpfs_file *file, *tmp;
116114
struct devtmpfs_sb *superblock;
117115

118116
RT_ASSERT(dfile->type == TMPFS_TYPE_DIR);
119117

120-
rt_list_for_each_safe(list, temp_list, &dfile->subdirs)
118+
dfs_vfs_for_each_subnode(file, tmp, dfile, node)
121119
{
122-
file = rt_list_entry(list, struct devtmpfs_file, sibling);
123120
if (file->type == TMPFS_TYPE_DIR)
124121
{
125122
_free_subdir(file);
@@ -134,7 +131,7 @@ static int _free_subdir(struct devtmpfs_file *dfile)
134131
RT_ASSERT(superblock);
135132

136133
rt_spin_lock(&superblock->lock);
137-
rt_list_remove(&(file->sibling));
134+
dfs_vfs_remove_node(&file->node);
138135
rt_spin_unlock(&superblock->lock);
139136

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

157153
superblock->root.name[0] = '/';
158154
superblock->root.sb = superblock;
159155
superblock->root.type = TMPFS_TYPE_DIR;
160156
superblock->root.mode = S_IFDIR | (S_IRUSR | S_IRGRP | S_IROTH) | (S_IXUSR | S_IXGRP | S_IXOTH);
161-
rt_list_init(&superblock->root.sibling);
162-
rt_list_init(&superblock->root.subdirs);
157+
dfs_vfs_init_node(&superblock->root.node);
163158

164159
rt_spin_lock_init(&superblock->lock);
165160

@@ -193,8 +188,7 @@ static struct devtmpfs_file *devtmpfs_file_lookup(struct devtmpfs_sb *superblock
193188
{
194189
const char *subpath, *curpath, *filename = RT_NULL;
195190
char subdir_name[DIRENT_NAME_MAX];
196-
struct devtmpfs_file *file, *curfile;
197-
rt_list_t *list;
191+
struct devtmpfs_file *file, *curfile, *tmp;
198192

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

223217
rt_spin_lock(&superblock->lock);
224218

225-
rt_list_for_each(list, &curfile->subdirs)
219+
dfs_vfs_for_each_subnode(file, tmp, curfile, node)
226220
{
227-
file = rt_list_entry(list, struct devtmpfs_file, sibling);
228221
if (filename) /* find file */
229222
{
230223
if (rt_strcmp(file->name, filename) == 0)
@@ -293,7 +286,9 @@ static int devtmpfs_stat(struct dfs_dentry *dentry, struct stat *st)
293286

294287
static int devtmpfs_getdents(struct dfs_file *file, struct dirent *dirp, uint32_t count)
295288
{
296-
struct devtmpfs_file *d_file;
289+
rt_size_t index, end;
290+
struct dirent *d;
291+
struct devtmpfs_file *d_file, *n_file = RT_NULL, *tmp;
297292
struct devtmpfs_sb *superblock;
298293

299294
RT_ASSERT(file);
@@ -306,11 +301,6 @@ static int devtmpfs_getdents(struct dfs_file *file, struct dirent *dirp, uint32_
306301
d_file = devtmpfs_file_lookup(superblock, file->dentry->pathname);
307302
if (d_file)
308303
{
309-
rt_size_t index, end;
310-
struct dirent *d;
311-
struct devtmpfs_file *n_file;
312-
rt_list_t *list;
313-
314304
/* make integer count */
315305
count = (count / sizeof(struct dirent));
316306
if (count == 0)
@@ -322,12 +312,10 @@ static int devtmpfs_getdents(struct dfs_file *file, struct dirent *dirp, uint32_
322312
index = 0;
323313
count = 0;
324314

325-
rt_list_for_each(list, &d_file->subdirs)
315+
dfs_vfs_for_each_subnode(n_file, tmp, d_file, node)
326316
{
327317
if (index >= (rt_size_t)file->fpos)
328318
{
329-
n_file = rt_list_entry(list, struct devtmpfs_file, sibling);
330-
331319
d = dirp + count;
332320
if (n_file->type == TMPFS_TYPE_FILE)
333321
{
@@ -378,8 +366,7 @@ static int devtmpfs_symlink(struct dfs_dentry *parent_dentry, const char *target
378366

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

381-
rt_list_init(&(l_file->subdirs));
382-
rt_list_init(&(l_file->sibling));
369+
dfs_vfs_init_node(&l_file->node);
383370
l_file->sb = superblock;
384371
l_file->type = TMPFS_TYPE_FILE;
385372
l_file->mode = p_file->mode;
@@ -388,7 +375,7 @@ static int devtmpfs_symlink(struct dfs_dentry *parent_dentry, const char *target
388375
l_file->link = rt_strdup(target);
389376

390377
rt_spin_lock(&superblock->lock);
391-
rt_list_insert_after(&(p_file->subdirs), &(l_file->sibling));
378+
dfs_vfs_append_node(&p_file->node, &l_file->node);
392379
rt_spin_unlock(&superblock->lock);
393380
}
394381
}
@@ -460,7 +447,7 @@ static int devtmpfs_unlink(struct dfs_dentry *dentry)
460447
}
461448

462449
rt_spin_lock(&superblock->lock);
463-
rt_list_remove(&(d_file->sibling));
450+
dfs_vfs_remove_node(&d_file->node);
464451
rt_spin_unlock(&superblock->lock);
465452

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

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

540-
rt_list_init(&(d_file->subdirs));
541-
rt_list_init(&(d_file->sibling));
527+
dfs_vfs_init_node(&d_file->node);
542528
d_file->sb = superblock;
543529

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

565551
rt_spin_lock(&superblock->lock);
566-
rt_list_insert_after(&(p_file->subdirs), &(d_file->sibling));
552+
dfs_vfs_append_node(&p_file->node, &d_file->node);
567553
rt_spin_unlock(&superblock->lock);
568554
}
569555

components/dfs/dfs_v2/filesystems/tmpfs/dfs_tmpfs.c

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,13 @@ static int _get_subdir(const char *path, char *name)
9999

100100
static int _free_subdir(struct tmpfs_file *dfile)
101101
{
102-
struct tmpfs_file *file;
103-
rt_list_t *list, *temp_list;
102+
struct tmpfs_file *file = RT_NULL, *tmp;
104103
struct tmpfs_sb *superblock;
105104

106105
RT_ASSERT(dfile->type == TMPFS_TYPE_DIR);
107106

108-
rt_list_for_each_safe(list, temp_list, &dfile->subdirs)
107+
dfs_vfs_for_each_subnode(file, tmp, dfile, node)
109108
{
110-
file = rt_list_entry(list, struct tmpfs_file, sibling);
111109
if (file->type == TMPFS_TYPE_DIR)
112110
{
113111
_free_subdir(file);
@@ -122,7 +120,7 @@ static int _free_subdir(struct tmpfs_file *dfile)
122120
RT_ASSERT(superblock != NULL);
123121

124122
rt_spin_lock(&superblock->lock);
125-
rt_list_remove(&(file->sibling));
123+
dfs_vfs_remove_node(&file->node);
126124
rt_spin_unlock(&superblock->lock);
127125

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

146143
superblock->root.name[0] = '/';
147144
superblock->root.sb = superblock;
148145
superblock->root.type = TMPFS_TYPE_DIR;
149-
rt_list_init(&superblock->root.sibling);
150-
rt_list_init(&superblock->root.subdirs);
146+
dfs_vfs_init_node(&superblock->root.node);
151147

152148
rt_spin_lock_init(&superblock->lock);
153149

@@ -236,8 +232,7 @@ struct tmpfs_file *dfs_tmpfs_lookup(struct tmpfs_sb *superblock,
236232
{
237233
const char *subpath, *curpath, *filename = RT_NULL;
238234
char subdir_name[TMPFS_NAME_MAX];
239-
struct tmpfs_file *file, *curfile;
240-
rt_list_t *list;
235+
struct tmpfs_file *file, *curfile, *tmp;
241236

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

266261
rt_spin_lock(&superblock->lock);
267262

268-
rt_list_for_each(list, &curfile->subdirs)
263+
dfs_vfs_for_each_subnode(file, tmp, curfile, node)
269264
{
270-
file = rt_list_entry(list, struct tmpfs_file, sibling);
271265
if (filename) /* find file */
272266
{
273267
if (rt_strcmp(file->name, filename) == 0)
@@ -503,8 +497,7 @@ static int dfs_tmpfs_getdents(struct dfs_file *file,
503497
{
504498
rt_size_t index, end;
505499
struct dirent *d;
506-
struct tmpfs_file *d_file, *n_file;
507-
rt_list_t *list;
500+
struct tmpfs_file *d_file, *n_file, *tmp;
508501
struct tmpfs_sb *superblock;
509502

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

530-
rt_list_for_each(list, &d_file->subdirs)
523+
dfs_vfs_for_each_subnode(n_file, tmp, d_file, node)
531524
{
532-
n_file = rt_list_entry(list, struct tmpfs_file, sibling);
533525
if (index >= (rt_size_t)file->fpos)
534526
{
535527
d = dirp + count;
@@ -573,7 +565,7 @@ static int dfs_tmpfs_unlink(struct dfs_dentry *dentry)
573565
return -ENOENT;
574566

575567
rt_spin_lock(&superblock->lock);
576-
rt_list_remove(&(d_file->sibling));
568+
dfs_vfs_remove_node(&d_file->node);
577569
rt_spin_unlock(&superblock->lock);
578570

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

633625
rt_spin_lock(&superblock->lock);
634-
rt_list_remove(&(d_file->sibling));
626+
dfs_vfs_remove_node(&d_file->node);
635627
rt_spin_unlock(&superblock->lock);
636628

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

639631
rt_spin_lock(&superblock->lock);
640-
rt_list_insert_after(&(p_file->subdirs), &(d_file->sibling));
632+
dfs_vfs_append_node(&p_file->node, &d_file->node);
641633
rt_spin_unlock(&superblock->lock);
642634

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

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

748-
rt_list_init(&(d_file->subdirs));
749-
rt_list_init(&(d_file->sibling));
740+
dfs_vfs_init_node(&d_file->node);
750741
d_file->data = NULL;
751742
d_file->size = 0;
752743
d_file->sb = superblock;
@@ -767,7 +758,7 @@ static struct dfs_vnode *dfs_tmpfs_create_vnode(struct dfs_dentry *dentry, int t
767758
#endif
768759
}
769760
rt_spin_lock(&superblock->lock);
770-
rt_list_insert_after(&(p_file->subdirs), &(d_file->sibling));
761+
dfs_vfs_append_node(&p_file->node, &d_file->node);
771762
rt_spin_unlock(&superblock->lock);
772763

773764
vnode->mnt = dentry->mnt;

components/dfs/dfs_v2/filesystems/tmpfs/dfs_tmpfs.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#define __DFS_TMPFS_H__
1313

1414
#include <rtthread.h>
15+
#include <dfs_vfs.h>
1516

1617
#define TMPFS_NAME_MAX 32
1718
#define TMPFS_MAGIC 0x0B0B0B0B
@@ -25,8 +26,7 @@ struct tmpfs_file
2526
{
2627
rt_uint32_t type; /* file type */
2728
char name[TMPFS_NAME_MAX]; /* file name */
28-
rt_list_t subdirs; /* file subdir list */
29-
rt_list_t sibling; /* file sibling list */
29+
struct dfs_vfs_node node; /* file node in the tmpfs */
3030
struct tmpfs_sb *sb; /* superblock ptr */
3131
rt_uint8_t *data; /* file date ptr */
3232
rt_size_t size; /* file size */
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright (c) 2006-2024, RT-Thread Development Team
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Change Logs:
7+
* Date Author Notes
8+
*/
9+
10+
#ifndef __DFS_VFS_H__
11+
#define __DFS_VFS_H__
12+
13+
#include "dfs_file.h"
14+
#include "dfs_fs.h"
15+
16+
#ifdef __cplusplus
17+
extern "C"
18+
{
19+
#endif
20+
21+
struct dfs_vfs_node
22+
{
23+
rt_list_t subnode; /* file subnode list */
24+
rt_list_t sibling; /* file sibling list */
25+
};
26+
27+
rt_inline void dfs_vfs_init_node(struct dfs_vfs_node *node)
28+
{
29+
rt_list_init(&node->subnode);
30+
rt_list_init(&node->sibling);
31+
}
32+
33+
rt_inline void dfs_vfs_append_node(struct dfs_vfs_node *dir, struct dfs_vfs_node *node)
34+
{
35+
rt_list_insert_after(&(dir->subnode), &(node->sibling));
36+
}
37+
38+
rt_inline void dfs_vfs_remove_node(struct dfs_vfs_node *node)
39+
{
40+
rt_list_remove(&(node->sibling));
41+
}
42+
43+
#define dfs_vfs_for_each_subnode(node, tmp, dir, member) \
44+
rt_list_for_each_entry_safe(node, tmp, &dir->member.subnode, member.sibling)
45+
46+
#ifdef __cplusplus
47+
}
48+
#endif
49+
50+
#endif /*__DFS_VFS_H__*/

0 commit comments

Comments
 (0)