xref: /kernel/uniproton/src/fs/vfs/vfs_maps.c (revision 54568cb3)
154568cb3Sopenharmony_ci/*
254568cb3Sopenharmony_ci * Copyright (c) 2022-2022 Huawei Technologies Co., Ltd. All rights reserved.
354568cb3Sopenharmony_ci *
454568cb3Sopenharmony_ci * UniProton is licensed under Mulan PSL v2.
554568cb3Sopenharmony_ci * You can use this software according to the terms and conditions of the Mulan PSL v2.
654568cb3Sopenharmony_ci * You may obtain a copy of Mulan PSL v2 at:
754568cb3Sopenharmony_ci *          http://license.coscl.org.cn/MulanPSL2
854568cb3Sopenharmony_ci * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
954568cb3Sopenharmony_ci * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
1054568cb3Sopenharmony_ci * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
1154568cb3Sopenharmony_ci * See the Mulan PSL v2 for more details.
1254568cb3Sopenharmony_ci * Create: 2022-09-21
1354568cb3Sopenharmony_ci * Description: 文件系统vfs层
1454568cb3Sopenharmony_ci */
1554568cb3Sopenharmony_ci
1654568cb3Sopenharmony_ci#include <stdlib.h>
1754568cb3Sopenharmony_ci#include "securec.h"
1854568cb3Sopenharmony_ci#include "vfs_maps.h"
1954568cb3Sopenharmony_ci#include "vfs_operations.h"
2054568cb3Sopenharmony_ci#include "prt_fs.h"
2154568cb3Sopenharmony_ci
2254568cb3Sopenharmony_cistatic struct TagFsMap *g_fsMap;
2354568cb3Sopenharmony_ci
2454568cb3Sopenharmony_cistruct TagFsMap *OsVfsGetFsMap(const char *fsType)
2554568cb3Sopenharmony_ci{
2654568cb3Sopenharmony_ci    struct TagFsMap *curr = g_fsMap;
2754568cb3Sopenharmony_ci    while (curr != NULL) {
2854568cb3Sopenharmony_ci        if ((curr->fsType != NULL) && (fsType != NULL) &&
2954568cb3Sopenharmony_ci            (strcmp(curr->fsType, fsType) == 0)) {
3054568cb3Sopenharmony_ci            return curr;
3154568cb3Sopenharmony_ci        }
3254568cb3Sopenharmony_ci        curr = curr->next;
3354568cb3Sopenharmony_ci    }
3454568cb3Sopenharmony_ci    return NULL;
3554568cb3Sopenharmony_ci}
3654568cb3Sopenharmony_ci
3754568cb3Sopenharmony_ciS32 OsVfsFsMgtDisk(const char *dev, const char *fsType, S32 *lengthArray, S32 partNum)
3854568cb3Sopenharmony_ci{
3954568cb3Sopenharmony_ci    S32 ret = FS_OK;
4054568cb3Sopenharmony_ci    (void)OsVfsLock();
4154568cb3Sopenharmony_ci    struct TagFsMap *fMap = OsVfsGetFsMap(fsType);
4254568cb3Sopenharmony_ci    if ((fMap != NULL) && (fMap->fsMgt != NULL) && (fMap->fsMgt->fdisk != NULL)) {
4354568cb3Sopenharmony_ci        ret = fMap->fsMgt->fdisk(dev, lengthArray, partNum);
4454568cb3Sopenharmony_ci    }
4554568cb3Sopenharmony_ci    (void)OsVfsUnlock();
4654568cb3Sopenharmony_ci    return ret;
4754568cb3Sopenharmony_ci}
4854568cb3Sopenharmony_ci
4954568cb3Sopenharmony_ciS32 OsVfsFsMgtFormat(const char *partName, char *fsType, void *data)
5054568cb3Sopenharmony_ci{
5154568cb3Sopenharmony_ci    S32 ret = FS_OK;
5254568cb3Sopenharmony_ci    (void)OsVfsLock();
5354568cb3Sopenharmony_ci    struct TagFsMap *fMap = OsVfsGetFsMap(fsType);
5454568cb3Sopenharmony_ci    if ((fMap != NULL) && (fMap->fsMgt != NULL) && (fMap->fsMgt->format != NULL)) {
5554568cb3Sopenharmony_ci        ret = fMap->fsMgt->format(partName, data);
5654568cb3Sopenharmony_ci    }
5754568cb3Sopenharmony_ci    (void)OsVfsUnlock();
5854568cb3Sopenharmony_ci    return ret;
5954568cb3Sopenharmony_ci}
6054568cb3Sopenharmony_ci
6154568cb3Sopenharmony_ciS32 OsFsRegister(const char *fsType, struct TagMountOps *fsMops,
6254568cb3Sopenharmony_ci                             struct TagFileOps *fsFops, struct TagFsManagement *fsMgt)
6354568cb3Sopenharmony_ci{
6454568cb3Sopenharmony_ci    if ((fsMops == NULL) || (fsFops == NULL)) {
6554568cb3Sopenharmony_ci        return FS_NOK;
6654568cb3Sopenharmony_ci    }
6754568cb3Sopenharmony_ci
6854568cb3Sopenharmony_ci    struct TagFsMap *newfs = (struct TagFsMap *)malloc(sizeof(struct TagFsMap));
6954568cb3Sopenharmony_ci    if (newfs == NULL) {
7054568cb3Sopenharmony_ci        return FS_NOK;
7154568cb3Sopenharmony_ci    }
7254568cb3Sopenharmony_ci    if (memset_s(newfs, sizeof(struct TagFsMap), 0, sizeof(struct TagFsMap)) != EOK) {
7354568cb3Sopenharmony_ci        free(newfs);
7454568cb3Sopenharmony_ci        return FS_NOK;
7554568cb3Sopenharmony_ci    }
7654568cb3Sopenharmony_ci
7754568cb3Sopenharmony_ci    newfs->fsType = strdup(fsType);
7854568cb3Sopenharmony_ci    if (newfs->fsType == NULL) {
7954568cb3Sopenharmony_ci        free(newfs);
8054568cb3Sopenharmony_ci        return FS_NOK;
8154568cb3Sopenharmony_ci    }
8254568cb3Sopenharmony_ci
8354568cb3Sopenharmony_ci    newfs->fsMops = fsMops;
8454568cb3Sopenharmony_ci    newfs->fsFops = fsFops;
8554568cb3Sopenharmony_ci    newfs->fsMgt = fsMgt;
8654568cb3Sopenharmony_ci    newfs->fsRefs = 0;
8754568cb3Sopenharmony_ci
8854568cb3Sopenharmony_ci    (void)OsVfsLock();
8954568cb3Sopenharmony_ci    newfs->next = g_fsMap;
9054568cb3Sopenharmony_ci    g_fsMap = newfs;
9154568cb3Sopenharmony_ci
9254568cb3Sopenharmony_ci    OsVfsUnlock();
9354568cb3Sopenharmony_ci    return FS_OK;
9454568cb3Sopenharmony_ci}
95