1 /*
2 * Copyright (c) 2022-2022 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 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
10 * of conditions and the following disclaimer in the documentation and/or other materials
11 * provided with the distribution.
12 *
13 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
14 * to endorse or promote products derived from this software without specific prior written
15 * permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #include "vfs_maps.h"
31 #include "vfs_operations.h"
32 #include <stdlib.h>
33 #include "securec.h"
34 #include "los_debug.h"
35 #include "los_compiler.h"
36 #include "los_fs.h"
37
38 struct FsMap *g_fsMap = NULL;
39
VfsFsMapGet(const char * fsType)40 struct FsMap *VfsFsMapGet(const char *fsType)
41 {
42 struct FsMap *curr = g_fsMap;
43
44 (void)LOS_FsLock();
45 while (curr != NULL) {
46 if ((curr->fsType != NULL) && (fsType != NULL) &&
47 (strcmp(curr->fsType, fsType) == 0)) {
48 LOS_FsUnlock();
49 return curr;
50 }
51 curr = curr->next;
52 }
53
54 LOS_FsUnlock();
55 return NULL;
56 }
57
OsFsRegister(const char * fsType,const struct MountOps * fsMops,const struct FileOps * fsFops,const struct FsManagement * fsMgt)58 int OsFsRegister(const char *fsType, const struct MountOps *fsMops,
59 const struct FileOps *fsFops, const struct FsManagement *fsMgt)
60 {
61 size_t len;
62 if ((fsMops == NULL) || (fsFops == NULL)) {
63 VFS_ERRNO_SET(EINVAL);
64 return (int)LOS_NOK;
65 }
66
67 struct FsMap *newfs = (struct FsMap *)LOSCFG_FS_MALLOC_HOOK(sizeof(struct FsMap));
68 if (newfs == NULL) {
69 PRINT_ERR("Fs register malloc failed, fsType %s.\n", fsType);
70 VFS_ERRNO_SET(ENOMEM);
71 return (int)LOS_NOK;
72 }
73 (void)memset_s(newfs, sizeof(struct FsMap), 0, sizeof(struct FsMap));
74
75 len = strlen(fsType) + 1;
76 newfs->fsType = LOSCFG_FS_MALLOC_HOOK(len);
77 if (newfs->fsType == NULL) {
78 LOSCFG_FS_FREE_HOOK(newfs);
79 VFS_ERRNO_SET(ENOMEM);
80 return (int)LOS_NOK;
81 }
82 (void)strcpy_s((char *)newfs->fsType, len, fsType);
83
84 newfs->fsMops = fsMops;
85 newfs->fsFops = fsFops;
86 newfs->fsMgt = fsMgt;
87 newfs->fsRefs = 0;
88
89 (void)LOS_FsLock();
90 newfs->next = g_fsMap;
91 g_fsMap = newfs;
92
93 LOS_FsUnlock();
94 return LOS_OK;
95 }
96
LOS_FsRegister(const char * fsType,const struct MountOps * fsMops,const struct FileOps * fsFops,const struct FsManagement * fsMgt)97 int LOS_FsRegister(const char *fsType, const struct MountOps *fsMops,
98 const struct FileOps *fsFops, const struct FsManagement *fsMgt)
99 {
100 if (VfsFsMapGet(fsType) != NULL) {
101 PRINT_ERR("fsType has been registered or fsType error\n");
102 VFS_ERRNO_SET(EINVAL);
103 return (int)LOS_NOK;
104 }
105
106 return OsFsRegister(fsType, fsMops, fsFops, fsMgt);
107 }
108