xref: /kernel/liteos_a/fs/vfs/path_cache.c (revision 0d163575)
10d163575Sopenharmony_ci/*
20d163575Sopenharmony_ci * Copyright (c) 2021-2021 Huawei Device Co., Ltd. All rights reserved.
30d163575Sopenharmony_ci *
40d163575Sopenharmony_ci * Redistribution and use in source and binary forms, with or without modification,
50d163575Sopenharmony_ci * are permitted provided that the following conditions are met:
60d163575Sopenharmony_ci *
70d163575Sopenharmony_ci * 1. Redistributions of source code must retain the above copyright notice, this list of
80d163575Sopenharmony_ci *    conditions and the following disclaimer.
90d163575Sopenharmony_ci *
100d163575Sopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright notice, this list
110d163575Sopenharmony_ci *    of conditions and the following disclaimer in the documentation and/or other materials
120d163575Sopenharmony_ci *    provided with the distribution.
130d163575Sopenharmony_ci *
140d163575Sopenharmony_ci * 3. Neither the name of the copyright holder nor the names of its contributors may be used
150d163575Sopenharmony_ci *    to endorse or promote products derived from this software without specific prior written
160d163575Sopenharmony_ci *    permission.
170d163575Sopenharmony_ci *
180d163575Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
190d163575Sopenharmony_ci * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
200d163575Sopenharmony_ci * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
210d163575Sopenharmony_ci * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
220d163575Sopenharmony_ci * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
230d163575Sopenharmony_ci * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
240d163575Sopenharmony_ci * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
250d163575Sopenharmony_ci * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
260d163575Sopenharmony_ci * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
270d163575Sopenharmony_ci * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
280d163575Sopenharmony_ci * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
290d163575Sopenharmony_ci */
300d163575Sopenharmony_ci
310d163575Sopenharmony_ci#include "path_cache.h"
320d163575Sopenharmony_ci#include "los_config.h"
330d163575Sopenharmony_ci#include "los_hash.h"
340d163575Sopenharmony_ci#include "stdlib.h"
350d163575Sopenharmony_ci#include "limits.h"
360d163575Sopenharmony_ci#include "vnode.h"
370d163575Sopenharmony_ci
380d163575Sopenharmony_ci#define PATH_CACHE_HASH_MASK (LOSCFG_MAX_PATH_CACHE_SIZE - 1)
390d163575Sopenharmony_ciLIST_HEAD g_pathCacheHashEntrys[LOSCFG_MAX_PATH_CACHE_SIZE];
400d163575Sopenharmony_ci#ifdef LOSCFG_DEBUG_VERSION
410d163575Sopenharmony_cistatic int g_totalPathCacheHit = 0;
420d163575Sopenharmony_cistatic int g_totalPathCacheTry = 0;
430d163575Sopenharmony_ci#define TRACE_TRY_CACHE() do { g_totalPathCacheTry++; } while (0)
440d163575Sopenharmony_ci#define TRACE_HIT_CACHE(pc) do { pc->hit++; g_totalPathCacheHit++; } while (0)
450d163575Sopenharmony_ci
460d163575Sopenharmony_civoid ResetPathCacheHitInfo(int *hit, int *try)
470d163575Sopenharmony_ci{
480d163575Sopenharmony_ci    *hit = g_totalPathCacheHit;
490d163575Sopenharmony_ci    *try = g_totalPathCacheTry;
500d163575Sopenharmony_ci    g_totalPathCacheHit = 0;
510d163575Sopenharmony_ci    g_totalPathCacheTry = 0;
520d163575Sopenharmony_ci}
530d163575Sopenharmony_ci#else
540d163575Sopenharmony_ci#define TRACE_TRY_CACHE()
550d163575Sopenharmony_ci#define TRACE_HIT_CACHE(pc)
560d163575Sopenharmony_ci#endif
570d163575Sopenharmony_ci
580d163575Sopenharmony_ciint PathCacheInit(void)
590d163575Sopenharmony_ci{
600d163575Sopenharmony_ci    for (int i = 0; i < LOSCFG_MAX_PATH_CACHE_SIZE; i++) {
610d163575Sopenharmony_ci        LOS_ListInit(&g_pathCacheHashEntrys[i]);
620d163575Sopenharmony_ci    }
630d163575Sopenharmony_ci    return LOS_OK;
640d163575Sopenharmony_ci}
650d163575Sopenharmony_ci
660d163575Sopenharmony_civoid PathCacheDump(void)
670d163575Sopenharmony_ci{
680d163575Sopenharmony_ci    PRINTK("-------->pathCache dump in\n");
690d163575Sopenharmony_ci    for (int i = 0; i < LOSCFG_MAX_PATH_CACHE_SIZE; i++) {
700d163575Sopenharmony_ci        struct PathCache *pc = NULL;
710d163575Sopenharmony_ci        LIST_HEAD *nhead = &g_pathCacheHashEntrys[i];
720d163575Sopenharmony_ci
730d163575Sopenharmony_ci        LOS_DL_LIST_FOR_EACH_ENTRY(pc, nhead, struct PathCache, hashEntry) {
740d163575Sopenharmony_ci            PRINTK("    pathCache dump hash %d item %s %p %p %d\n", i,
750d163575Sopenharmony_ci                pc->name, pc->parentVnode, pc->childVnode, pc->nameLen);
760d163575Sopenharmony_ci        }
770d163575Sopenharmony_ci    }
780d163575Sopenharmony_ci    PRINTK("-------->pathCache dump out\n");
790d163575Sopenharmony_ci}
800d163575Sopenharmony_ci
810d163575Sopenharmony_civoid PathCacheMemoryDump(void)
820d163575Sopenharmony_ci{
830d163575Sopenharmony_ci    int pathCacheNum = 0;
840d163575Sopenharmony_ci    int nameSum = 0;
850d163575Sopenharmony_ci    for (int i = 0; i < LOSCFG_MAX_PATH_CACHE_SIZE; i++) {
860d163575Sopenharmony_ci        LIST_HEAD *dhead = &g_pathCacheHashEntrys[i];
870d163575Sopenharmony_ci        struct PathCache *dent = NULL;
880d163575Sopenharmony_ci
890d163575Sopenharmony_ci        LOS_DL_LIST_FOR_EACH_ENTRY(dent, dhead, struct PathCache, hashEntry) {
900d163575Sopenharmony_ci            pathCacheNum++;
910d163575Sopenharmony_ci            nameSum += dent->nameLen;
920d163575Sopenharmony_ci        }
930d163575Sopenharmony_ci    }
940d163575Sopenharmony_ci    PRINTK("pathCache number = %d\n", pathCacheNum);
950d163575Sopenharmony_ci    PRINTK("pathCache memory size = %d(B)\n", pathCacheNum * sizeof(struct PathCache) + nameSum);
960d163575Sopenharmony_ci}
970d163575Sopenharmony_ci
980d163575Sopenharmony_cistatic uint32_t NameHash(const char *name, int len, struct Vnode *dvp)
990d163575Sopenharmony_ci{
1000d163575Sopenharmony_ci    uint32_t hash;
1010d163575Sopenharmony_ci    hash = LOS_HashFNV32aBuf(name, len, FNV1_32A_INIT);
1020d163575Sopenharmony_ci    hash = LOS_HashFNV32aBuf(&dvp, sizeof(struct Vnode *), hash);
1030d163575Sopenharmony_ci    return hash;
1040d163575Sopenharmony_ci}
1050d163575Sopenharmony_ci
1060d163575Sopenharmony_cistatic void PathCacheInsert(struct Vnode *parent, struct PathCache *cache, const char* name, int len)
1070d163575Sopenharmony_ci{
1080d163575Sopenharmony_ci    int hash = NameHash(name, len, parent) & PATH_CACHE_HASH_MASK;
1090d163575Sopenharmony_ci    LOS_ListAdd(&g_pathCacheHashEntrys[hash], &cache->hashEntry);
1100d163575Sopenharmony_ci}
1110d163575Sopenharmony_ci
1120d163575Sopenharmony_cistruct PathCache *PathCacheAlloc(struct Vnode *parent, struct Vnode *vnode, const char *name, uint8_t len)
1130d163575Sopenharmony_ci{
1140d163575Sopenharmony_ci    struct PathCache *pc = NULL;
1150d163575Sopenharmony_ci    size_t pathCacheSize;
1160d163575Sopenharmony_ci    int ret;
1170d163575Sopenharmony_ci
1180d163575Sopenharmony_ci    if (name == NULL || len > NAME_MAX || parent == NULL || vnode == NULL) {
1190d163575Sopenharmony_ci        return NULL;
1200d163575Sopenharmony_ci    }
1210d163575Sopenharmony_ci    pathCacheSize = sizeof(struct PathCache) + len + 1;
1220d163575Sopenharmony_ci
1230d163575Sopenharmony_ci    pc = (struct PathCache*)zalloc(pathCacheSize);
1240d163575Sopenharmony_ci    if (pc == NULL) {
1250d163575Sopenharmony_ci        PRINT_ERR("pathCache alloc failed, no memory!\n");
1260d163575Sopenharmony_ci        return NULL;
1270d163575Sopenharmony_ci    }
1280d163575Sopenharmony_ci
1290d163575Sopenharmony_ci    ret = strncpy_s(pc->name, len + 1, name, len);
1300d163575Sopenharmony_ci    if (ret != LOS_OK) {
1310d163575Sopenharmony_ci        free(pc);
1320d163575Sopenharmony_ci        return NULL;
1330d163575Sopenharmony_ci    }
1340d163575Sopenharmony_ci
1350d163575Sopenharmony_ci    pc->parentVnode = parent;
1360d163575Sopenharmony_ci    pc->nameLen = len;
1370d163575Sopenharmony_ci    pc->childVnode = vnode;
1380d163575Sopenharmony_ci
1390d163575Sopenharmony_ci    LOS_ListAdd((&(parent->childPathCaches)), (&(pc->childEntry)));
1400d163575Sopenharmony_ci    LOS_ListAdd((&(vnode->parentPathCaches)), (&(pc->parentEntry)));
1410d163575Sopenharmony_ci
1420d163575Sopenharmony_ci    PathCacheInsert(parent, pc, name, len);
1430d163575Sopenharmony_ci
1440d163575Sopenharmony_ci    return pc;
1450d163575Sopenharmony_ci}
1460d163575Sopenharmony_ci
1470d163575Sopenharmony_ciint PathCacheFree(struct PathCache *pc)
1480d163575Sopenharmony_ci{
1490d163575Sopenharmony_ci    if (pc == NULL) {
1500d163575Sopenharmony_ci        PRINT_ERR("pathCache free: invalid pathCache\n");
1510d163575Sopenharmony_ci        return -ENOENT;
1520d163575Sopenharmony_ci    }
1530d163575Sopenharmony_ci
1540d163575Sopenharmony_ci    LOS_ListDelete(&pc->hashEntry);
1550d163575Sopenharmony_ci    LOS_ListDelete(&pc->parentEntry);
1560d163575Sopenharmony_ci    LOS_ListDelete(&pc->childEntry);
1570d163575Sopenharmony_ci    free(pc);
1580d163575Sopenharmony_ci
1590d163575Sopenharmony_ci    return LOS_OK;
1600d163575Sopenharmony_ci}
1610d163575Sopenharmony_ci
1620d163575Sopenharmony_ciint PathCacheLookup(struct Vnode *parent, const char *name, int len, struct Vnode **vnode)
1630d163575Sopenharmony_ci{
1640d163575Sopenharmony_ci    struct PathCache *pc = NULL;
1650d163575Sopenharmony_ci    int hash = NameHash(name, len, parent) & PATH_CACHE_HASH_MASK;
1660d163575Sopenharmony_ci    LIST_HEAD *dhead = &g_pathCacheHashEntrys[hash];
1670d163575Sopenharmony_ci
1680d163575Sopenharmony_ci    TRACE_TRY_CACHE();
1690d163575Sopenharmony_ci    LOS_DL_LIST_FOR_EACH_ENTRY(pc, dhead, struct PathCache, hashEntry) {
1700d163575Sopenharmony_ci        if (pc->parentVnode == parent && pc->nameLen == len && !strncmp(pc->name, name, len)) {
1710d163575Sopenharmony_ci            *vnode = pc->childVnode;
1720d163575Sopenharmony_ci            TRACE_HIT_CACHE(pc);
1730d163575Sopenharmony_ci            return LOS_OK;
1740d163575Sopenharmony_ci        }
1750d163575Sopenharmony_ci    }
1760d163575Sopenharmony_ci    return -ENOENT;
1770d163575Sopenharmony_ci}
1780d163575Sopenharmony_ci
1790d163575Sopenharmony_cistatic void FreeChildPathCache(struct Vnode *vnode)
1800d163575Sopenharmony_ci{
1810d163575Sopenharmony_ci    struct PathCache *item = NULL;
1820d163575Sopenharmony_ci    struct PathCache *nextItem = NULL;
1830d163575Sopenharmony_ci
1840d163575Sopenharmony_ci    LOS_DL_LIST_FOR_EACH_ENTRY_SAFE(item, nextItem, &(vnode->childPathCaches), struct PathCache, childEntry) {
1850d163575Sopenharmony_ci        PathCacheFree(item);
1860d163575Sopenharmony_ci    }
1870d163575Sopenharmony_ci}
1880d163575Sopenharmony_ci
1890d163575Sopenharmony_cistatic void FreeParentPathCache(struct Vnode *vnode)
1900d163575Sopenharmony_ci{
1910d163575Sopenharmony_ci    struct PathCache *item = NULL;
1920d163575Sopenharmony_ci    struct PathCache *nextItem = NULL;
1930d163575Sopenharmony_ci
1940d163575Sopenharmony_ci    LOS_DL_LIST_FOR_EACH_ENTRY_SAFE(item, nextItem, &(vnode->parentPathCaches), struct PathCache, parentEntry) {
1950d163575Sopenharmony_ci        PathCacheFree(item);
1960d163575Sopenharmony_ci    }
1970d163575Sopenharmony_ci}
1980d163575Sopenharmony_ci
1990d163575Sopenharmony_civoid VnodePathCacheFree(struct Vnode *vnode)
2000d163575Sopenharmony_ci{
2010d163575Sopenharmony_ci    if (vnode == NULL) {
2020d163575Sopenharmony_ci        return;
2030d163575Sopenharmony_ci    }
2040d163575Sopenharmony_ci    FreeParentPathCache(vnode);
2050d163575Sopenharmony_ci    FreeChildPathCache(vnode);
2060d163575Sopenharmony_ci}
2070d163575Sopenharmony_ci
2080d163575Sopenharmony_ciLIST_HEAD* GetPathCacheList()
2090d163575Sopenharmony_ci{
2100d163575Sopenharmony_ci    return g_pathCacheHashEntrys;
2110d163575Sopenharmony_ci}
212