1 /*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include <fcntl.h>
16 #include <stdlib.h>
17 #include <errno.h>
18 #include <limits.h>
19 #include <sys/types.h>
20 #include <sys/sysmacros.h>
21
22 #include "securec.h"
23 #include "beget_ext.h"
24 #include "fs_dm_snapshot.h"
25
26 #ifdef __cplusplus
27 #if __cplusplus
28 extern "C" {
29 #endif
30 #endif
31
FsDmCreateSnapshotDevice(const char * devName,char * dmDevPath,uint64_t dmDevPathLen,DmSnapshotTarget * target)32 int FsDmCreateSnapshotDevice(const char *devName, char *dmDevPath, uint64_t dmDevPathLen, DmSnapshotTarget *target)
33 {
34 if (devName == NULL || dmDevPath == NULL || target == NULL) {
35 BEGET_LOGE("argc is null");
36 return -1;
37 }
38 int fd = open(DEVICE_MAPPER_PATH, O_RDWR | O_CLOEXEC);
39 if (fd < 0) {
40 BEGET_LOGE("error %d, open %s", errno, DEVICE_MAPPER_PATH);
41 return -1;
42 }
43 int rc = 0;
44 do {
45 rc = CreateDmDev(fd, devName);
46 if (rc != 0) {
47 BEGET_LOGE("error %d, create dm device fail", rc);
48 break;
49 }
50 rc = LoadDmDeviceTable(fd, devName, target, SNAPSHOT);
51 if (rc != 0) {
52 BEGET_LOGE("error %d, load device table fail", rc);
53 break;
54 }
55 rc = ActiveDmDevice(fd, devName);
56 if (rc != 0) {
57 BEGET_LOGE("error %d, active device fail", rc);
58 break;
59 }
60
61 rc = DmGetDeviceName(fd, devName, dmDevPath, dmDevPathLen);
62 if (rc != 0) {
63 BEGET_LOGE("get dm device name failed");
64 break;
65 }
66
67 BEGET_LOGI("fs create snapshot device success, dev is [%s] dmDevPath is [%s]", devName, dmDevPath);
68 } while (0);
69 close(fd);
70 return rc;
71 }
72
FsDmSwitchToSnapshotMerge(const char * devName,DmSnapshotTarget * target)73 int FsDmSwitchToSnapshotMerge(const char *devName, DmSnapshotTarget *target)
74 {
75 if (devName == NULL || target == NULL) {
76 BEGET_LOGE("argc is null");
77 return -1;
78 }
79 int fd = open(DEVICE_MAPPER_PATH, O_RDWR | O_CLOEXEC);
80 if (fd < 0) {
81 BEGET_LOGE("error %d, open %s", errno, DEVICE_MAPPER_PATH);
82 return -1;
83 }
84 int rc = 0;
85 do {
86 rc = LoadDmDeviceTable(fd, devName, target, SNAPSHOTMERGE);
87 if (rc != 0) {
88 BEGET_LOGE("error %d, load device table fail", rc);
89 break;
90 }
91 rc = ActiveDmDevice(fd, devName);
92 if (rc != 0) {
93 BEGET_LOGE("error %d, active device fail", rc);
94 break;
95 }
96 BEGET_LOGI("fs switch snapshot merge success, dev is %s", devName);
97 } while (0);
98 close(fd);
99 return rc;
100 }
101
ParseStatusText(char * data,StatusInfo * processInfo)102 INIT_STATIC bool ParseStatusText(char *data, StatusInfo *processInfo)
103 {
104 BEGET_LOGI("ParseStatusText start \"%s\"", data);
105 int args = sscanf_s(data, "%lu %lu %lu", &processInfo->sectors_allocated,
106 &processInfo->total_sectors, &processInfo->metadata_sectors);
107 if (args == 3) { // 3 is parameters
108 BEGET_LOGI("ParseStatusText success. sectors_allocated %d total_sectors %d metadata_sectors %d",
109 processInfo->sectors_allocated, processInfo->total_sectors, processInfo->metadata_sectors);
110 return true;
111 }
112 if (strlen(data) >= sizeof(processInfo->error)) {
113 BEGET_LOGE("data len err, data is = %s", data);
114 return false;
115 }
116 if (strcpy_s(processInfo->error, sizeof(processInfo->error), data) != EOK) {
117 BEGET_LOGE("strcpy_s err, errno %d", errno);
118 return false;
119 }
120 if (strcmp(processInfo->error, "Invalid") == 0 ||
121 strcmp(processInfo->error, "Overflow") == 0 ||
122 strcmp(processInfo->error, "Merge failed") == 0 ||
123 strcmp(processInfo->error, "Unknown") == 0 ||
124 strcmp(processInfo->error, "") == 0) {
125 BEGET_LOGW("processInfo->error is \"%s\"", processInfo->error);
126 return true;
127 }
128 BEGET_LOGE("could not parse snapshot processInfo \"%s\": wrong format", processInfo->error);
129 return false;
130 }
131
GetDmSnapshotStatus(const char * name,const char * targetType,StatusInfo * processInfo)132 bool GetDmSnapshotStatus(const char *name, const char *targetType, StatusInfo *processInfo)
133 {
134 if (name == NULL || targetType == NULL || processInfo == NULL) {
135 BEGET_LOGE("argc is null");
136 return false;
137 }
138 BEGET_LOGI("GetDmSnapshotStatus start, name %s, targetType %s", name, targetType);
139 size_t bufferLen = MAX_TABLE_LEN * sizeof(char);
140 int fd = open(DEVICE_MAPPER_PATH, O_RDWR | O_CLOEXEC);
141 BEGET_ERROR_CHECK(fd >= 0, return false, "open error %d", errno);
142 char *buffer = (char *)calloc(1, bufferLen);
143 BEGET_ERROR_CHECK(buffer != NULL, close(fd); return false, "calloc buffer err");
144 struct dm_ioctl *io = NULL;
145 bool ret = false;
146 do {
147 io = (struct dm_ioctl *)buffer;
148 int rc = InitDmIo(io, name);
149 BEGET_ERROR_CHECK(rc == 0, break, "error %d, init Dm io", rc);
150 io->data_size = bufferLen;
151 io->data_start = sizeof(*io);
152 io->flags = 0;
153 if (ioctl(fd, DM_TABLE_STATUS, io) != 0) {
154 BEGET_LOGE("DM_TABLE_STATUS failed for %s", name);
155 break;
156 }
157 if (io->flags & DM_BUFFER_FULL_FLAG) {
158 BEGET_LOGE("io flags err");
159 break;
160 }
161 size_t cursor = io->data_start;
162 struct dm_target_spec* spec = (struct dm_target_spec*)(&buffer[cursor]);
163 if (cursor + sizeof(struct dm_target_spec) > io->data_size) {
164 BEGET_LOGE("len err");
165 break;
166 }
167 size_t dataOffset = cursor + sizeof(struct dm_target_spec);
168 if ((size_t)spec->next <= sizeof(struct dm_target_spec)) {
169 BEGET_LOGE("spec->next = %u, len err, dataOffset = %zu", spec->next, dataOffset);
170 break;
171 }
172 size_t dataLen = (size_t)spec->next - sizeof(struct dm_target_spec);
173 char *data = (char *)calloc(1, dataLen + 1);
174 BEGET_ERROR_CHECK(data != NULL, break, "calloc dataLen = %zu err", dataLen);
175 rc = strncpy_s(data, dataLen, buffer + dataOffset, dataLen);
176 BEGET_ERROR_CHECK(rc == EOK, free(data); break, "strncpy_s err");
177 if (strcmp(spec->target_type, targetType) != 0 || !ParseStatusText(data, processInfo)) {
178 BEGET_LOGE("get snapshot status fail");
179 free(data);
180 break;
181 }
182 free(data);
183 ret = true;
184 } while (0);
185 BEGET_LOGI("GetDmSnapshotStatus end. ret %d", ret);
186 close(fd);
187 free(buffer);
188 return ret;
189 }
190
GetDmMergeProcess(const char * name,StatusInfo * processInfo)191 bool GetDmMergeProcess(const char *name, StatusInfo *processInfo)
192 {
193 return GetDmSnapshotStatus(name, "snapshot-merge", processInfo);
194 }
195
196 #ifdef __cplusplus
197 #if __cplusplus
198 }
199 #endif
200 #endif