更新日期: 2022/06/01 来源: https://gitee.com/weharmony/kernel_liteos_a_note
vnode_hash.c
浏览该文件的文档.
1/*
2 * Copyright (c) 2021-2021 Huawei Device Co., Ltd. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification,
5 * are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
15 * to endorse or promote products derived from this software without specific prior written
16 * permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#include "los_mux.h"
32#include "vnode.h"
33#include "fs/mount.h"
34
35
36#define VNODE_HASH_BUCKETS 128
37//用哈希表只有一个目的,就是加快对索引节点对象的搜索
38LIST_HEAD g_vnodeHashEntrys[VNODE_HASH_BUCKETS];//哈希桶链表组
39uint32_t g_vnodeHashMask = VNODE_HASH_BUCKETS - 1; //哈希掩码
40uint32_t g_vnodeHashSize = VNODE_HASH_BUCKETS; //哈希大小
41
42static LosMux g_vnodeHashMux;//哈希表互斥量
43//索引节点哈希表初始化
45{
46 int ret;
47 for (int i = 0; i < g_vnodeHashSize; i++) {//遍历初始化 128个双向链表
49 }
50
51 ret = LOS_MuxInit(&g_vnodeHashMux, NULL);
52 if (ret != LOS_OK) {
53 PRINT_ERR("Create mutex for vnode hash list fail, status: %d", ret);
54 return ret;
55 }
56
57 return LOS_OK;
58}
59///打印全部 hash 表
60void VnodeHashDump(void)
61{
62 PRINTK("-------->VnodeHashDump in\n");
63 (void)LOS_MuxLock(&g_vnodeHashMux, LOS_WAIT_FOREVER);//拿锁方式,永等
64 for (int i = 0; i < g_vnodeHashSize; i++) {
65 LIST_HEAD *nhead = &g_vnodeHashEntrys[i];
66 struct Vnode *node = NULL;
67
68 LOS_DL_LIST_FOR_EACH_ENTRY(node, nhead, struct Vnode, hashEntry) {//循环打印链表
69 PRINTK(" vnode dump: col %d item %p\n", i, node);//类似矩阵
70 }
71 }
73 PRINTK("-------->VnodeHashDump out\n");
74}
75///通过节点获取哈希索引值
76uint32_t VfsHashIndex(struct Vnode *vnode)
77{
78 if (vnode == NULL) {
79 return -EINVAL;
80 }
81 return (vnode->hash + vnode->originMount->hashseed);//用于定位在哈希表的下标
82}
83///通过哈希值和装载设备哈希种子获取哈希表索引
84static LOS_DL_LIST *VfsHashBucket(const struct Mount *mp, uint32_t hash)
85{
86 return (&g_vnodeHashEntrys[(hash + mp->hashseed) & g_vnodeHashMask]);//g_vnodeHashMask确保始终范围在[0~g_vnodeHashMask]之间
87}
88///通过哈希值获取节点信息
89int VfsHashGet(const struct Mount *mount, uint32_t hash, struct Vnode **vnode, VfsHashCmp *fn, void *arg)
90{
91 struct Vnode *curVnode = NULL;
92
93 if (mount == NULL || vnode == NULL) {
94 return -EINVAL;
95 }
96
97 (void)LOS_MuxLock(&g_vnodeHashMux, LOS_WAIT_FOREVER);//先上锁
98 LOS_DL_LIST *list = VfsHashBucket(mount, hash);//获取哈希表对应的链表项
99 LOS_DL_LIST_FOR_EACH_ENTRY(curVnode, list, struct Vnode, hashEntry) {//遍历链表
100 if (curVnode->hash != hash) {//对比哈希值找
101 continue;
102 }
103 if (curVnode->originMount != mount) {//对比原始mount值必须一致
104 continue;
105 }
106 if (fn != NULL && fn(curVnode, arg)) {//哈希值比较函数,fn由具体的文件系统提供.
107 continue;
108 }
110 *vnode = curVnode;//找到对应索引节点
111 return LOS_OK;
112 }
114 *vnode = NULL;
115 return LOS_NOK;
116}
117///从哈希链表中摘除索引节点
118void VfsHashRemove(struct Vnode *vnode)
119{
120 if (vnode == NULL) {
121 return;
122 }
123 (void)LOS_MuxLock(&g_vnodeHashMux, LOS_WAIT_FOREVER);
124 LOS_ListDelete(&vnode->hashEntry);//直接把自己摘掉就行了
126}
127///插入哈希表
128int VfsHashInsert(struct Vnode *vnode, uint32_t hash)
129{
130 if (vnode == NULL) {
131 return -EINVAL;
132 }
133 (void)LOS_MuxLock(&g_vnodeHashMux, LOS_WAIT_FOREVER);
134 vnode->hash = hash;//设置节点哈希值
135 LOS_ListHeadInsert(VfsHashBucket(vnode->originMount, hash), &vnode->hashEntry);//通过节点hashEntry 挂入哈希表对于索引链表中
137 return LOS_OK;
138}
LITE_OS_SEC_ALW_INLINE STATIC INLINE VOID LOS_ListInit(LOS_DL_LIST *list)
Definition: los_list.h:104
LITE_OS_SEC_ALW_INLINE STATIC INLINE VOID LOS_ListHeadInsert(LOS_DL_LIST *list, LOS_DL_LIST *node)
Insert a node to the head of a doubly linked list.
Definition: los_list.h:268
LITE_OS_SEC_ALW_INLINE STATIC INLINE VOID LOS_ListDelete(LOS_DL_LIST *node)
Definition: los_list.h:292
LITE_OS_SEC_TEXT UINT32 LOS_MuxInit(LosMux *mutex, const LosMuxAttr *attr)
初始化互斥锁
Definition: los_mux.c:262
LITE_OS_SEC_TEXT UINT32 LOS_MuxUnlock(LosMux *mutex)
释放锁
Definition: los_mux.c:559
LITE_OS_SEC_TEXT UINT32 LOS_MuxLock(LosMux *mutex, UINT32 timeout)
拿互斥锁,
Definition: los_mux.c:437
举例: mount /dev/mmcblk0p0 /bin1/vs/sd vfat 将/dev/mmcblk0p0 挂载到/bin1/vs/sd目录
Definition: mount.h:68
uint32_t hashseed
Definition: mount.h:79
Definition: los_mux.h:73
vnode并不包含文件名,因为 vnode和文件名是 1:N 的关系
Definition: vnode.h:164
uint32_t hash
Definition: vnode.h:167
LIST_ENTRY hashEntry
Definition: vnode.h:178
struct Mount * originMount
Definition: vnode.h:180
ARG_NUM_3 ARG_NUM_1 ARG_NUM_2 ARG_NUM_2 ARG_NUM_3 ARG_NUM_1 ARG_NUM_4 ARG_NUM_2 ARG_NUM_2 ARG_NUM_5 ARG_NUM_2 void
int VfsHashCmp(struct Vnode *vnode, void *arg)
Definition: vnode.h:228
static LOS_DL_LIST * VfsHashBucket(const struct Mount *mp, uint32_t hash)
通过哈希值和装载设备哈希种子获取哈希表索引
Definition: vnode_hash.c:84
int VfsHashInsert(struct Vnode *vnode, uint32_t hash)
插入哈希表
Definition: vnode_hash.c:128
int VfsHashGet(const struct Mount *mount, uint32_t hash, struct Vnode **vnode, VfsHashCmp *fn, void *arg)
通过哈希值获取节点信息
Definition: vnode_hash.c:89
uint32_t g_vnodeHashMask
Definition: vnode_hash.c:39
void VnodeHashDump(void)
打印全部 hash 表
Definition: vnode_hash.c:60
LIST_HEAD g_vnodeHashEntrys[VNODE_HASH_BUCKETS]
Definition: vnode_hash.c:38
uint32_t VfsHashIndex(struct Vnode *vnode)
通过节点获取哈希索引值
Definition: vnode_hash.c:76
int VnodeHashInit(void)
Definition: vnode_hash.c:44
uint32_t g_vnodeHashSize
Definition: vnode_hash.c:40
void VfsHashRemove(struct Vnode *vnode)
从哈希链表中摘除索引节点
Definition: vnode_hash.c:118
static LosMux g_vnodeHashMux
Definition: vnode_hash.c:42