path_cache.c 6.6 KB
Newer Older
W
wangchenyang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/*
 * Copyright (c) 2021-2021 Huawei Device Co., Ltd. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this list of
 *    conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
 *    of conditions and the following disclaimer in the documentation and/or other materials
 *    provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
 *    to endorse or promote products derived from this software without specific prior written
 *    permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

M
mucor 已提交
31
#include "path_cache.h"
W
wangchenyang 已提交
32
#include "los_config.h"
M
mucor 已提交
33
#include "los_hash.h"
W
wangchenyang 已提交
34 35
#include "stdlib.h"
#include "limits.h"
M
mucor 已提交
36
#include "vnode.h"
W
wangchenyang 已提交
37 38 39

#define PATH_CACHE_HASH_MASK (LOSCFG_MAX_PATH_CACHE_SIZE - 1)
LIST_HEAD g_pathCacheHashEntrys[LOSCFG_MAX_PATH_CACHE_SIZE];
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
#ifdef LOSCFG_DEBUG_VERSION
static int g_totalPathCacheHit = 0;
static int g_totalPathCacheTry = 0;
#define TRACE_TRY_CACHE() do { g_totalPathCacheTry++; } while (0)
#define TRACE_HIT_CACHE(pc) do { pc->hit++; g_totalPathCacheHit++; } while (0)

void ResetPathCacheHitInfo(int *hit, int *try)
{
    *hit = g_totalPathCacheHit;
    *try = g_totalPathCacheTry;
    g_totalPathCacheHit = 0;
    g_totalPathCacheTry = 0;
}
#else
#define TRACE_TRY_CACHE()
#define TRACE_HIT_CACHE(pc)
#endif
W
wangchenyang 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69

int PathCacheInit(void)
{
    for (int i = 0; i < LOSCFG_MAX_PATH_CACHE_SIZE; i++) {
        LOS_ListInit(&g_pathCacheHashEntrys[i]);
    }
    return LOS_OK;
}

void PathCacheDump(void)
{
    PRINTK("-------->pathCache dump in\n");
    for (int i = 0; i < LOSCFG_MAX_PATH_CACHE_SIZE; i++) {
70
        struct PathCache *pc = NULL;
W
wangchenyang 已提交
71 72
        LIST_HEAD *nhead = &g_pathCacheHashEntrys[i];

73
        LOS_DL_LIST_FOR_EACH_ENTRY(pc, nhead, struct PathCache, hashEntry) {
74
            PRINTK("    pathCache dump hash %d item %s %p %p %d\n", i,
75
                pc->name, pc->parentVnode, pc->childVnode, pc->nameLen);
W
wangchenyang 已提交
76 77 78 79 80 81 82 83
        }
    }
    PRINTK("-------->pathCache dump out\n");
}

void PathCacheMemoryDump(void)
{
    int pathCacheNum = 0;
84
    int nameSum = 0;
W
wangchenyang 已提交
85 86 87 88 89 90
    for (int i = 0; i < LOSCFG_MAX_PATH_CACHE_SIZE; i++) {
        LIST_HEAD *dhead = &g_pathCacheHashEntrys[i];
        struct PathCache *dent = NULL;

        LOS_DL_LIST_FOR_EACH_ENTRY(dent, dhead, struct PathCache, hashEntry) {
            pathCacheNum++;
91
            nameSum += dent->nameLen;
W
wangchenyang 已提交
92 93 94
        }
    }
    PRINTK("pathCache number = %d\n", pathCacheNum);
95
    PRINTK("pathCache memory size = %d(B)\n", pathCacheNum * sizeof(struct PathCache) + nameSum);
W
wangchenyang 已提交
96 97 98 99 100
}

static uint32_t NameHash(const char *name, int len, struct Vnode *dvp)
{
    uint32_t hash;
M
mucor 已提交
101 102
    hash = LOS_HashFNV32aBuf(name, len, FNV1_32A_INIT);
    hash = LOS_HashFNV32aBuf(&dvp, sizeof(struct Vnode *), hash);
W
wangchenyang 已提交
103 104 105 106 107 108 109 110 111 112 113
    return hash;
}

static void PathCacheInsert(struct Vnode *parent, struct PathCache *cache, const char* name, int len)
{
    int hash = NameHash(name, len, parent) & PATH_CACHE_HASH_MASK;
    LOS_ListAdd(&g_pathCacheHashEntrys[hash], &cache->hashEntry);
}

struct PathCache *PathCacheAlloc(struct Vnode *parent, struct Vnode *vnode, const char *name, uint8_t len)
{
114
    struct PathCache *pc = NULL;
W
wangchenyang 已提交
115
    size_t pathCacheSize;
M
mucor 已提交
116
    int ret;
W
wangchenyang 已提交
117 118 119 120 121 122

    if (name == NULL || len > NAME_MAX || parent == NULL || vnode == NULL) {
        return NULL;
    }
    pathCacheSize = sizeof(struct PathCache) + len + 1;

123 124
    pc = (struct PathCache*)zalloc(pathCacheSize);
    if (pc == NULL) {
W
wangchenyang 已提交
125 126 127 128
        PRINT_ERR("pathCache alloc failed, no memory!\n");
        return NULL;
    }

129
    ret = strncpy_s(pc->name, len + 1, name, len);
M
mucor 已提交
130 131 132
    if (ret != LOS_OK) {
        return NULL;
    }
W
wangchenyang 已提交
133

134 135 136
    pc->parentVnode = parent;
    pc->nameLen = len;
    pc->childVnode = vnode;
W
wangchenyang 已提交
137

138 139
    LOS_ListAdd((&(parent->childPathCaches)), (&(pc->childEntry)));
    LOS_ListAdd((&(vnode->parentPathCaches)), (&(pc->parentEntry)));
W
wangchenyang 已提交
140

141
    PathCacheInsert(parent, pc, name, len);
W
wangchenyang 已提交
142

143
    return pc;
W
wangchenyang 已提交
144 145
}

146
int PathCacheFree(struct PathCache *pc)
W
wangchenyang 已提交
147
{
148
    if (pc == NULL) {
W
wangchenyang 已提交
149 150 151 152
        PRINT_ERR("pathCache free: invalid pathCache\n");
        return -ENOENT;
    }

153 154 155 156
    LOS_ListDelete(&pc->hashEntry);
    LOS_ListDelete(&pc->parentEntry);
    LOS_ListDelete(&pc->childEntry);
    free(pc);
W
wangchenyang 已提交
157 158 159 160 161 162

    return LOS_OK;
}

int PathCacheLookup(struct Vnode *parent, const char *name, int len, struct Vnode **vnode)
{
163
    struct PathCache *pc = NULL;
W
wangchenyang 已提交
164 165 166
    int hash = NameHash(name, len, parent) & PATH_CACHE_HASH_MASK;
    LIST_HEAD *dhead = &g_pathCacheHashEntrys[hash];

167 168 169 170 171
    TRACE_TRY_CACHE();
    LOS_DL_LIST_FOR_EACH_ENTRY(pc, dhead, struct PathCache, hashEntry) {
        if (pc->parentVnode == parent && pc->nameLen == len && !strncmp(pc->name, name, len)) {
            *vnode = pc->childVnode;
            TRACE_HIT_CACHE(pc);
W
wangchenyang 已提交
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
            return LOS_OK;
        }
    }
    return -ENOENT;
}

static void FreeChildPathCache(struct Vnode *vnode)
{
    struct PathCache *item = NULL;
    struct PathCache *nextItem = NULL;

    LOS_DL_LIST_FOR_EACH_ENTRY_SAFE(item, nextItem, &(vnode->childPathCaches), struct PathCache, childEntry) {
        PathCacheFree(item);
    }
}

static void FreeParentPathCache(struct Vnode *vnode)
{
    struct PathCache *item = NULL;
    struct PathCache *nextItem = NULL;

    LOS_DL_LIST_FOR_EACH_ENTRY_SAFE(item, nextItem, &(vnode->parentPathCaches), struct PathCache, parentEntry) {
        PathCacheFree(item);
    }
}

void VnodePathCacheFree(struct Vnode *vnode)
{
    if (vnode == NULL) {
        return;
    }
    FreeParentPathCache(vnode);
    FreeChildPathCache(vnode);
}
206 207 208 209 210

LIST_HEAD* GetPathCacheList()
{
    return g_pathCacheHashEntrys;
}