• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright (c) 2023 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 
16 #ifndef STARTUP_LIBFS_DM_H
17 #define STARTUP_LIBFS_DM_H
18 
19 #include <stdint.h>
20 #include <stdbool.h>
21 #include <stdio.h>
22 #include <unistd.h>
23 #include <linux/dm-ioctl.h>
24 #include <sys/ioctl.h>
25 #include "fs_manager.h"
26 
27 #ifdef __cplusplus
28 #if __cplusplus
29 extern "C" {
30 #endif
31 #endif
32 #define MAX_PATH_LEN 256
33 #define MAX_TABLE_LEN 4096
34 #define MAX_WORDS_LEN 20
35 #define DM_VERSION0 (4)
36 #define DM_VERSION1 (0)
37 #define DM_VERSION2 (0)
38 #define DM_ALIGN_MASK (7)
39 #define DM_ALIGN(x) (((x) + DM_ALIGN_MASK) & ~DM_ALIGN_MASK)
40 #define DEVICE_MAPPER_PATH "/dev/mapper/control"
41 #define DM_DEVICE_PATH_PREFIX "/dev/block/dm-"
42 
43 #define FS_DM_RETURN_ERR_IF_NULL(__ptr)                   \
44     do {                                                  \
45         if ((__ptr) == NULL) {                            \
46             BEGET_LOGE("error, %s is NULL\n", #__ptr);    \
47             return -1;                                    \
48         }                                                 \
49     } while (0)
50 
51 typedef struct {
52     uint64_t start;
53     uint64_t length;
54     char *paras;
55     uint64_t paras_len;
56 } DmVerityTarget;
57 
58 enum DmDeviceType {
59     VERIFY,
60     LINEAR,
61     SNAPSHOT,
62     SNAPSHOTMERGE,
63     MAXNUMTYPE
64 };
65 
66 typedef struct {
67     uint64_t sectors_allocated;
68     uint64_t total_sectors;
69     uint64_t metadata_sectors;
70     char error[MAX_WORDS_LEN];
71 } StatusInfo;
72 
73 int FsDmInitDmDev(char *devPath, bool useSocket);
74 int FsDmCreateDevice(char **dmDevPath, const char *devName, DmVerityTarget *target);
75 int FsDmRemoveDevice(const char *devName);
76 int FsDmCreateLinearDevice(const char *devName, char *dmBlkName,
77                            uint64_t dmBlkNameLen, DmVerityTarget *target);
78 int LoadDmDeviceTable(int fd, const char *devName, DmVerityTarget *target, int dmType);
79 int DmGetDeviceName(int fd, const char *devName, char *outDevName, const uint64_t outDevNameLen);
80 int ActiveDmDevice(int fd, const char *devName);
81 int InitDmIo(struct dm_ioctl *io, const char *devName);
82 int CreateDmDev(int fd, const char *devName);
83 int GetDmDevPath(int fd, char **dmDevPath, const char *devName);
84 
85 bool GetDmStatusInfo(const char *name, struct dm_ioctl *io);
86 #ifdef __cplusplus
87 #if __cplusplus
88 }
89 #endif
90 #endif
91 
92 #endif // STARTUP_LIBFS_DM_H
93