• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "jffs2_hash.h"
32 
33 #ifdef LOSCFG_FS_JFFS
34 
Jffs2HashInit(LosMux * lock,LOS_DL_LIST * heads)35 int Jffs2HashInit(LosMux *lock, LOS_DL_LIST *heads)
36 {
37     int ret;
38     for (int i = 0; i < JFFS2_NODE_HASH_BUCKETS; i++) {
39         LOS_ListInit(&heads[i]);
40     }
41 
42     ret = LOS_MuxInit(lock, NULL);
43     if (ret != LOS_OK) {
44         PRINT_ERR("Create mutex for vnode hash list fail, status: %d", ret);
45         return ret;
46     }
47 
48     return LOS_OK;
49 }
50 
Jffs2HashDeinit(LosMux * lock)51 int Jffs2HashDeinit(LosMux *lock)
52 {
53     int ret;
54     ret = LOS_MuxDestroy(lock);
55     if (ret != LOS_OK) {
56         PRINT_ERR("Destroy mutex for vnode hash list fail, status: %d", ret);
57         return ret;
58     }
59 
60     return LOS_OK;
61 }
62 
Jffs2HashDump(LosMux * lock,LOS_DL_LIST * heads)63 void Jffs2HashDump(LosMux *lock, LOS_DL_LIST *heads)
64 {
65     PRINTK("-------->Jffs2HashDump in\n");
66     (void)LOS_MuxLock(lock, LOS_WAIT_FOREVER);
67     for (int i = 0; i < JFFS2_NODE_HASH_BUCKETS; i++) {
68         LIST_HEAD *nhead = &heads[i];
69         struct jffs2_inode *node = NULL;
70 
71         LOS_DL_LIST_FOR_EACH_ENTRY(node, nhead, struct jffs2_inode, i_hashlist) {
72             PRINTK("    vnode dump: col %d item %p\n", i, node);
73         }
74     }
75     (void)LOS_MuxUnlock(lock);
76     PRINTK("-------->Jffs2HashDump out\n");
77 }
78 
Jffs2HashBucket(LOS_DL_LIST * heads,const uint32_t ino)79 static LOS_DL_LIST *Jffs2HashBucket(LOS_DL_LIST *heads, const uint32_t ino)
80 {
81     LOS_DL_LIST *head = &(heads[ino & JFFS2_NODE_HASH_MASK]);
82     return head;
83 }
84 
Jffs2HashGet(LosMux * lock,LOS_DL_LIST * heads,const void * sb,const uint32_t ino,struct jffs2_inode ** ppNode)85 int Jffs2HashGet(LosMux *lock, LOS_DL_LIST *heads, const void *sb, const uint32_t ino, struct jffs2_inode **ppNode)
86 {
87     struct jffs2_inode *node = NULL;
88 
89     while (1) {
90         (void)LOS_MuxLock(lock, LOS_WAIT_FOREVER);
91         LOS_DL_LIST *list = Jffs2HashBucket(heads, ino);
92         LOS_DL_LIST_FOR_EACH_ENTRY(node, list, struct jffs2_inode, i_hashlist) {
93             if (node->i_ino != ino)
94                 continue;
95             if (node->i_sb != sb)
96                 continue;
97             (void)LOS_MuxUnlock(lock);
98             *ppNode = node;
99             return 0;
100         }
101         (void)LOS_MuxUnlock(lock);
102         *ppNode = NULL;
103         return 0;
104     }
105 }
106 
Jffs2HashRemove(LosMux * lock,struct jffs2_inode * node)107 void Jffs2HashRemove(LosMux *lock, struct jffs2_inode *node)
108 {
109     (void)LOS_MuxLock(lock, LOS_WAIT_FOREVER);
110     LOS_ListDelete(&node->i_hashlist);
111     (void)LOS_MuxUnlock(lock);
112 }
113 
Jffs2HashInsert(LosMux * lock,LOS_DL_LIST * heads,struct jffs2_inode * node,const uint32_t ino)114 int Jffs2HashInsert(LosMux *lock, LOS_DL_LIST *heads, struct jffs2_inode *node, const uint32_t ino)
115 {
116     (void)LOS_MuxLock(lock, LOS_WAIT_FOREVER);
117     LOS_ListHeadInsert(Jffs2HashBucket(heads, ino), &node->i_hashlist);
118     (void)LOS_MuxUnlock(lock);
119     return 0;
120 }
121 
122 #endif
123 
124