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
38 LIST_HEAD g_vnodeHashEntrys[VNODE_HASH_BUCKETS];
39 uint32_t g_vnodeHashMask = VNODE_HASH_BUCKETS - 1;
40 uint32_t g_vnodeHashSize = VNODE_HASH_BUCKETS;
41
42 static LosMux g_vnodeHashMux;
43
VnodeHashInit(void)44 int VnodeHashInit(void)
45 {
46 int ret;
47 for (int i = 0; i < g_vnodeHashSize; i++) {
48 LOS_ListInit(&g_vnodeHashEntrys[i]);
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
VnodeHashDump(void)60 void 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 }
72 (void)LOS_MuxUnlock(&g_vnodeHashMux);
73 PRINTK("-------->VnodeHashDump out\n");
74 }
75
VfsHashIndex(struct Vnode * vnode)76 uint32_t VfsHashIndex(struct Vnode *vnode)
77 {
78 if (vnode == NULL) {
79 return -EINVAL;
80 }
81 return (vnode->hash + vnode->originMount->hashseed);
82 }
83
VfsHashBucket(const struct Mount * mp,uint32_t hash)84 static LOS_DL_LIST *VfsHashBucket(const struct Mount *mp, uint32_t hash)
85 {
86 return (&g_vnodeHashEntrys[(hash + mp->hashseed) & g_vnodeHashMask]);
87 }
88
VfsHashGet(const struct Mount * mount,uint32_t hash,struct Vnode ** vnode,VfsHashCmp * fn,void * arg)89 int 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) {
104 continue;
105 }
106 if (fn != NULL && fn(curVnode, arg)) {
107 continue;
108 }
109 (void)LOS_MuxUnlock(&g_vnodeHashMux);
110 *vnode = curVnode;
111 return LOS_OK;
112 }
113 (void)LOS_MuxUnlock(&g_vnodeHashMux);
114 *vnode = NULL;
115 return LOS_NOK;
116 }
117
VfsHashRemove(struct Vnode * vnode)118 void 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);
125 (void)LOS_MuxUnlock(&g_vnodeHashMux);
126 }
127
VfsHashInsert(struct Vnode * vnode,uint32_t hash)128 int 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);
136 (void)LOS_MuxUnlock(&g_vnodeHashMux);
137 return LOS_OK;
138 }
139