• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2022 Huawei Technologies Co., Ltd. All rights reserved.
3  *
4  * UniProton is licensed under Mulan PSL v2.
5  * You can use this software according to the terms and conditions of the Mulan PSL v2.
6  * You may obtain a copy of Mulan PSL v2 at:
7  *          http://license.coscl.org.cn/MulanPSL2
8  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
9  * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
10  * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
11  * See the Mulan PSL v2 for more details.
12  * Create: 2022-09-21
13  * Description: 文件系统vfs层
14  */
15 #include "stdlib.h"
16 #include "string.h"
17 #include "securec.h"
18 #include "vfs_partition.h"
19 #include "vfs_operations.h"
20 #include "prt_fs.h"
21 #include "vfs_maps.h"
22 #include "vfs_mount.h"
23 
24 static struct TagDeviceDesc *g_deviceList;
25 
OsGetPartIdByPartName(const char * partName)26 S32 OsGetPartIdByPartName(const char *partName)
27 {
28     if (partName == NULL) {
29         return FS_NOK;
30     }
31 
32     /* p 字符后面是 partId */
33     char *p = strrchr(partName, 'p');
34     if (p + 1 != NULL) {
35         return atoi(p + 1);
36     }
37 
38     return FS_NOK;
39 }
40 
OsGetDevIdByDevName(const char * dev)41 S32 OsGetDevIdByDevName(const char *dev)
42 {
43     if (dev == NULL) {
44         return FS_NOK;
45     }
46 
47     /* dev后面是 deviceId */
48     char *p = (char *)dev + strlen(dev) - 1;
49     if (p != NULL) {
50         return atoi(p);
51     }
52 
53     return FS_NOK;
54 }
55 
OsGetDeviceList(void)56 struct TagDeviceDesc *OsGetDeviceList(void)
57 {
58     return g_deviceList;
59 }
60 
OsFreeDeviceDesc(struct TagDeviceDesc * prev)61 static void OsFreeDeviceDesc(struct TagDeviceDesc *prev)
62 {
63     if (prev == NULL) {
64         return;
65     }
66     if (prev->dDev != NULL) {
67         free((void *)prev->dDev);
68     }
69     if (prev->dFsType != NULL) {
70         free((void *)prev->dFsType);
71     }
72     if (prev->dAddrArray != NULL) {
73         free((void *)prev->dAddrArray);
74     }
75     if (prev->dLengthArray != NULL) {
76         free((void *)prev->dLengthArray);
77     }
78     free(prev);
79 }
80 
OsAddDevice(const char * dev,const char * fsType,S32 * lengthArray,S32 * addrArray,S32 partNum)81 static S32 OsAddDevice(const char *dev, const char *fsType, S32 *lengthArray,
82                                    S32 *addrArray, S32 partNum)
83 {
84     struct TagDeviceDesc *prev = NULL;
85     for (prev = g_deviceList; prev != NULL; prev = prev->dNext) {
86         if (strcmp(prev->dDev, dev) == 0) {
87             errno = -EEXIST;
88             return FS_NOK;
89         }
90     }
91 
92     if (addrArray == NULL) {
93         errno = -EFAULT;
94         return FS_NOK;
95     }
96 
97     prev = (struct TagDeviceDesc *)malloc(sizeof(struct TagDeviceDesc));
98     if (prev == NULL) {
99         errno = -ENOMEM;
100         return FS_NOK;
101     }
102     prev->dDev = strdup(dev);
103     prev->dFsType  = strdup(fsType);
104     prev->dAddrArray = (S32 *)malloc(partNum * sizeof(S32));
105     if (prev->dDev == NULL || prev->dFsType == NULL || prev->dAddrArray == NULL) {
106         OsFreeDeviceDesc(prev);
107         errno = -ENOMEM;
108         return FS_NOK;
109     }
110     (void)memcpy_s(prev->dAddrArray, partNum * sizeof(S32), addrArray, partNum * sizeof(S32));
111 
112     if (lengthArray != NULL) {
113         prev->dLengthArray = (S32 *)malloc(partNum * sizeof(S32));
114         if (prev->dLengthArray == NULL) {
115             OsFreeDeviceDesc(prev);
116             errno = -ENOMEM;
117             return FS_NOK;
118         }
119         (void)memcpy_s(prev->dLengthArray, partNum * sizeof(S32), lengthArray, partNum * sizeof(S32));
120     }
121 
122     prev->dNext = g_deviceList;
123     prev->dPartNum = partNum;
124     g_deviceList = prev;
125     return FS_OK;
126 }
127 
128 
PRT_DiskPartition(const char * dev,const char * fsType,S32 * lengthArray,S32 * addrArray,S32 partNum)129 S32 PRT_DiskPartition(const char *dev, const char *fsType, S32 *lengthArray,
130                       S32 *addrArray, S32 partNum)
131 {
132     S32 ret = OsVfsFsMgtDisk(dev, fsType, lengthArray, partNum);
133     if (ret != FS_OK) {
134         return ret;
135     }
136 
137     return OsAddDevice(dev, fsType, lengthArray, addrArray, partNum);
138 }
139 
PRT_PartitionFormat(const char * partName,char * fsType,void * data)140 S32 PRT_PartitionFormat(const char *partName, char *fsType, void *data)
141 {
142     S32 ret = OsVfsFindMountPoint(fsType);
143     if (ret == FS_OK) {
144         errno = EBUSY;
145         return FS_NOK;
146     }
147 
148     return OsVfsFsMgtFormat(partName, fsType, data);
149 }
150