path_cache.c 6.0 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 40 41 42 43 44 45 46 47 48 49 50 51 52

#define PATH_CACHE_HASH_MASK (LOSCFG_MAX_PATH_CACHE_SIZE - 1)
LIST_HEAD g_pathCacheHashEntrys[LOSCFG_MAX_PATH_CACHE_SIZE];

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++) {
53
        struct PathCache *nc = NULL;
W
wangchenyang 已提交
54 55
        LIST_HEAD *nhead = &g_pathCacheHashEntrys[i];

56
        LOS_DL_LIST_FOR_EACH_ENTRY(nc, nhead, struct PathCache, hashEntry) {
57
            PRINTK("    pathCache dump hash %d item %s %p %p %d\n", i,
58
                nc->name, nc->parentVnode, nc->childVnode, nc->nameLen);
W
wangchenyang 已提交
59 60 61 62 63 64 65 66
        }
    }
    PRINTK("-------->pathCache dump out\n");
}

void PathCacheMemoryDump(void)
{
    int pathCacheNum = 0;
67
    int nameSum = 0;
W
wangchenyang 已提交
68 69 70 71 72 73
    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++;
74
            nameSum += dent->nameLen;
W
wangchenyang 已提交
75 76 77
        }
    }
    PRINTK("pathCache number = %d\n", pathCacheNum);
78
    PRINTK("pathCache memory size = %d(B)\n", pathCacheNum * sizeof(struct PathCache) + nameSum);
W
wangchenyang 已提交
79 80 81 82 83
}

static uint32_t NameHash(const char *name, int len, struct Vnode *dvp)
{
    uint32_t hash;
M
mucor 已提交
84 85
    hash = LOS_HashFNV32aBuf(name, len, FNV1_32A_INIT);
    hash = LOS_HashFNV32aBuf(&dvp, sizeof(struct Vnode *), hash);
W
wangchenyang 已提交
86 87 88 89 90 91 92 93 94 95 96
    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)
{
97
    struct PathCache *nc = NULL;
W
wangchenyang 已提交
98
    size_t pathCacheSize;
M
mucor 已提交
99
    int ret;
W
wangchenyang 已提交
100 101 102 103 104 105

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

106 107
    nc = (struct PathCache*)zalloc(pathCacheSize);
    if (nc == NULL) {
W
wangchenyang 已提交
108 109 110 111
        PRINT_ERR("pathCache alloc failed, no memory!\n");
        return NULL;
    }

112
    ret = strncpy_s(nc->name, len + 1, name, len);
M
mucor 已提交
113 114 115
    if (ret != LOS_OK) {
        return NULL;
    }
W
wangchenyang 已提交
116

117 118 119
    nc->parentVnode = parent;
    nc->nameLen = len;
    nc->childVnode = vnode;
W
wangchenyang 已提交
120

121 122
    LOS_ListAdd((&(parent->childPathCaches)), (&(nc->childEntry)));
    LOS_ListAdd((&(vnode->parentPathCaches)), (&(nc->parentEntry)));
W
wangchenyang 已提交
123

124
    PathCacheInsert(parent, nc, name, len);
W
wangchenyang 已提交
125

126
    return nc;
W
wangchenyang 已提交
127 128
}

129
int PathCacheFree(struct PathCache *nc)
W
wangchenyang 已提交
130
{
131
    if (nc == NULL) {
W
wangchenyang 已提交
132 133 134 135
        PRINT_ERR("pathCache free: invalid pathCache\n");
        return -ENOENT;
    }

136 137 138 139
    LOS_ListDelete(&nc->hashEntry);
    LOS_ListDelete(&nc->parentEntry);
    LOS_ListDelete(&nc->childEntry);
    free(nc);
W
wangchenyang 已提交
140 141 142 143 144 145

    return LOS_OK;
}

int PathCacheLookup(struct Vnode *parent, const char *name, int len, struct Vnode **vnode)
{
146
    struct PathCache *nc = NULL;
W
wangchenyang 已提交
147 148 149
    int hash = NameHash(name, len, parent) & PATH_CACHE_HASH_MASK;
    LIST_HEAD *dhead = &g_pathCacheHashEntrys[hash];

150 151 152
    LOS_DL_LIST_FOR_EACH_ENTRY(nc, dhead, struct PathCache, hashEntry) {
        if (nc->parentVnode == parent && nc->nameLen == len && !strncmp(nc->name, name, len)) {
            *vnode = nc->childVnode;
W
wangchenyang 已提交
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
            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);
}