RT-Thread RTOS 1.2.0
An open source embedded real-time operating system
载入中...
搜索中...
未找到
dfs_vnode.c
浏览该文件的文档.
1/*
2 * Copyright (c) 2006-2023, RT-Thread Development Team
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Change Logs:
7 * Date Author Notes
8 * 2023-05-05 Bernard Implement vnode in dfs v2.0
9 */
10
11#include <dfs_file.h>
12#include <dfs_mnt.h>
13#ifdef RT_USING_PAGECACHE
14#include "dfs_pcache.h"
15#endif
16
17#define DBG_TAG "DFS.vnode"
18#define DBG_LVL DBG_WARNING
19#include <rtdbg.h>
20
21int dfs_vnode_init(struct dfs_vnode *vnode, int type, const struct dfs_file_ops *fops)
22{
23 if (vnode)
24 {
25 rt_memset(vnode, 0, sizeof(struct dfs_vnode));
26
27 vnode->type = type;
28 rt_atomic_store(&(vnode->ref_count), 1);
29 vnode->mnt = RT_NULL;
30 vnode->fops = fops;
31 }
32
33 return 0;
34}
35
37{
38 struct dfs_vnode *vnode = rt_calloc(1, sizeof(struct dfs_vnode));
39 if (!vnode)
40 {
41 LOG_E("create a vnode failed.");
42 return RT_NULL;
43 }
44
45 rt_atomic_store(&(vnode->ref_count), 1);
46
47 LOG_I("create a vnode: %p", vnode);
48
49 return vnode;
50}
51
52int dfs_vnode_destroy(struct dfs_vnode* vnode)
53{
54 rt_err_t ret = RT_EOK;
55
56 if (vnode)
57 {
58 ret = dfs_file_lock();
59 if (ret == RT_EOK)
60 {
61 if (rt_atomic_load(&(vnode->ref_count)) == 1)
62 {
63 LOG_I("free a vnode: %p", vnode);
64#ifdef RT_USING_PAGECACHE
65 if (vnode->aspace)
66 {
67 dfs_aspace_destroy(vnode->aspace);
68 }
69#endif
70 if (vnode->mnt)
71 {
72 DLOG(msg, "vnode", vnode->mnt->fs_ops->name, DLOG_MSG, "fs_ops->free_vnode");
73 vnode->mnt->fs_ops->free_vnode(vnode);
74 }
75 else
76 {
77 DLOG(msg, "vnode", "vnode", DLOG_MSG, "destroy vnode(mnt=NULL)");
78 }
79
81
82 rt_free(vnode);
83 }
84 else
85 {
87 }
88 }
89 }
90
91 return 0;
92}
93
94struct dfs_vnode *dfs_vnode_ref(struct dfs_vnode *vnode)
95{
96 if (vnode)
97 {
98 rt_atomic_add(&(vnode->ref_count), 1);
99
100 DLOG(note, "vnode", "vnode ref_count=%d", rt_atomic_load(&(vnode->ref_count)));
101 }
102
103 return vnode;
104}
105
106void dfs_vnode_unref(struct dfs_vnode *vnode)
107{
108 rt_err_t ret = RT_EOK;
109
110 if (vnode)
111 {
112 ret = dfs_file_lock();
113 if (ret == RT_EOK)
114 {
115 rt_atomic_sub(&(vnode->ref_count), 1);
116 DLOG(note, "vnode", "vnode ref_count=%d", rt_atomic_load(&(vnode->ref_count)));
117#ifdef RT_USING_PAGECACHE
118 if (vnode->aspace)
119 {
120 dfs_aspace_destroy(vnode->aspace);
121 }
122#endif
123 if (rt_atomic_load(&(vnode->ref_count)) == 0)
124 {
125 LOG_I("free a vnode: %p", vnode);
126 DLOG(msg, "vnode", "vnode", DLOG_MSG, "free vnode, ref_count=0");
127
128 if (vnode->mnt)
129 {
130 DLOG(msg, "vnode", vnode->mnt->fs_ops->name, DLOG_MSG, "fs_ops->free_vnode");
131 vnode->mnt->fs_ops->free_vnode(vnode);
132 }
133
135
136 rt_free(vnode);
137 }
138 else
139 {
141 DLOG(note, "vnode", "vnode ref_count=%d", rt_atomic_load(&(vnode->ref_count)));
142 }
143 }
144 }
145
146 return;
147}
int dfs_vnode_destroy(struct dfs_vnode *vnode)
void dfs_vnode_unref(struct dfs_vnode *vnode)
int dfs_vnode_init(struct dfs_vnode *vnode, int type, const struct dfs_file_ops *fops)
struct dfs_vnode * dfs_vnode_create(void)
struct dfs_vnode * dfs_vnode_ref(struct dfs_vnode *vnode)
void dfs_file_unlock(void)
定义 dfs.c:164
rt_err_t dfs_file_lock(void)
定义 dfs.c:147
rt_weak void * rt_calloc(rt_size_t count, rt_size_t size)
This function will contiguously allocate enough space for count objects that are size bytes of memory...
rt_weak void rt_free(void *ptr)
This function will release the previously allocated memory block by rt_malloc. The released memory bl...
#define rt_atomic_sub(ptr, v)
#define rt_atomic_add(ptr, v)
#define rt_atomic_store(ptr, v)
#define rt_atomic_load(ptr)
#define DLOG(...)
定义 rtdbg.h:53
#define LOG_E(fmt,...)
#define LOG_I(...)
rt_base_t rt_err_t
#define RT_NULL
int(* free_vnode)(struct dfs_vnode *vnode)
const char * name
const struct dfs_filesystem_ops * fs_ops
struct dfs_aspace * aspace
struct dfs_mnt * mnt
const struct dfs_file_ops * fops
rt_atomic_t ref_count