]> git.proxmox.com Git - ceph.git/blob - ceph/src/pmdk/src/tools/pmempool/common.h
import ceph 16.2.7
[ceph.git] / ceph / src / pmdk / src / tools / pmempool / common.h
1 /* SPDX-License-Identifier: BSD-3-Clause */
2 /* Copyright 2014-2020, Intel Corporation */
3
4 /*
5 * common.h -- declarations of common functions
6 */
7
8 #include <stdint.h>
9 #include <stddef.h>
10 #include <stdarg.h>
11 #include <stdbool.h>
12
13 #include "queue.h"
14 #include "log.h"
15 #include "blk.h"
16 #include "libpmemobj.h"
17 #include "lane.h"
18 #include "ulog.h"
19 #include "memops.h"
20 #include "pmalloc.h"
21 #include "list.h"
22 #include "obj.h"
23 #include "memblock.h"
24 #include "heap_layout.h"
25 #include "tx.h"
26 #include "heap.h"
27 #include "btt_layout.h"
28 #include "page_size.h"
29
30 /* XXX - modify Linux makefiles to generate srcversion.h and remove #ifdef */
31 #ifdef _WIN32
32 #include "srcversion.h"
33 #endif
34
35 #define COUNT_OF(x) (sizeof(x) / sizeof(0[x]))
36
37 #define OPT_SHIFT 12
38 #define OPT_MASK (~((1 << OPT_SHIFT) - 1))
39 #define OPT_LOG (1 << (PMEM_POOL_TYPE_LOG + OPT_SHIFT))
40 #define OPT_BLK (1 << (PMEM_POOL_TYPE_BLK + OPT_SHIFT))
41 #define OPT_OBJ (1 << (PMEM_POOL_TYPE_OBJ + OPT_SHIFT))
42 #define OPT_BTT (1 << (PMEM_POOL_TYPE_BTT + OPT_SHIFT))
43 #define OPT_ALL (OPT_LOG | OPT_BLK | OPT_OBJ | OPT_BTT)
44
45 #define OPT_REQ_SHIFT 8
46 #define OPT_REQ_MASK ((1 << OPT_REQ_SHIFT) - 1)
47 #define _OPT_REQ(c, n) ((c) << (OPT_REQ_SHIFT * (n)))
48 #define OPT_REQ0(c) _OPT_REQ(c, 0)
49 #define OPT_REQ1(c) _OPT_REQ(c, 1)
50 #define OPT_REQ2(c) _OPT_REQ(c, 2)
51 #define OPT_REQ3(c) _OPT_REQ(c, 3)
52 #define OPT_REQ4(c) _OPT_REQ(c, 4)
53 #define OPT_REQ5(c) _OPT_REQ(c, 5)
54 #define OPT_REQ6(c) _OPT_REQ(c, 6)
55 #define OPT_REQ7(c) _OPT_REQ(c, 7)
56
57 #ifndef min
58 #define min(a, b) ((a) < (b) ? (a) : (b))
59 #endif
60
61 #define FOREACH_RANGE(range, ranges)\
62 PMDK_LIST_FOREACH(range, &(ranges)->head, next)
63
64 #define PLIST_OFF_TO_PTR(pop, off)\
65 ((off) == 0 ? NULL : (void *)((uintptr_t)(pop) + (off) - OBJ_OOB_SIZE))
66
67 #define ENTRY_TO_ALLOC_HDR(entry)\
68 ((void *)((uintptr_t)(entry) - sizeof(struct allocation_header)))
69
70 #define OBJH_FROM_PTR(ptr)\
71 ((void *)((uintptr_t)(ptr) - sizeof(struct legacy_object_header)))
72
73 #define DEFAULT_HDR_SIZE PMEM_PAGESIZE
74 #define DEFAULT_DESC_SIZE PMEM_PAGESIZE
75 #define POOL_HDR_DESC_SIZE (DEFAULT_HDR_SIZE + DEFAULT_DESC_SIZE)
76
77 #define PTR_TO_ALLOC_HDR(ptr)\
78 ((void *)((uintptr_t)(ptr) -\
79 sizeof(struct legacy_object_header)))
80
81 #define OBJH_TO_PTR(objh)\
82 ((void *)((uintptr_t)(objh) + sizeof(struct legacy_object_header)))
83
84 /* invalid answer for ask_* functions */
85 #define INV_ANS '\0'
86
87 #define FORMAT_PRINTF(a, b) __attribute__((__format__(__printf__, (a), (b))))
88
89 /*
90 * pmem_pool_type_t -- pool types
91 */
92 typedef enum {
93 PMEM_POOL_TYPE_LOG = 0x01,
94 PMEM_POOL_TYPE_BLK = 0x02,
95 PMEM_POOL_TYPE_OBJ = 0x04,
96 PMEM_POOL_TYPE_BTT = 0x08,
97 PMEM_POOL_TYPE_ALL = 0x0f,
98 PMEM_POOL_TYPE_UNKNOWN = 0x80,
99 } pmem_pool_type_t;
100
101 struct option_requirement {
102 int opt;
103 pmem_pool_type_t type;
104 uint64_t req;
105 };
106
107 struct options {
108 const struct option *opts;
109 size_t noptions;
110 char *bitmap;
111 const struct option_requirement *req;
112 };
113
114 struct pmem_pool_params {
115 pmem_pool_type_t type;
116 char signature[POOL_HDR_SIG_LEN];
117 uint64_t size;
118 mode_t mode;
119 int is_poolset;
120 int is_part;
121 int is_checksum_ok;
122 union {
123 struct {
124 uint64_t bsize;
125 } blk;
126 struct {
127 char layout[PMEMOBJ_MAX_LAYOUT];
128 } obj;
129 };
130 };
131
132 struct pool_set_file {
133 int fd;
134 char *fname;
135 void *addr;
136 size_t size;
137 struct pool_set *poolset;
138 size_t replica;
139 time_t mtime;
140 mode_t mode;
141 bool fileio;
142 };
143
144 struct pool_set_file *pool_set_file_open(const char *fname,
145 int rdonly, int check);
146 void pool_set_file_close(struct pool_set_file *file);
147 int pool_set_file_read(struct pool_set_file *file, void *buff,
148 size_t nbytes, uint64_t off);
149 int pool_set_file_write(struct pool_set_file *file, void *buff,
150 size_t nbytes, uint64_t off);
151 int pool_set_file_set_replica(struct pool_set_file *file, size_t replica);
152 size_t pool_set_file_nreplicas(struct pool_set_file *file);
153 void *pool_set_file_map(struct pool_set_file *file, uint64_t offset);
154 void pool_set_file_persist(struct pool_set_file *file,
155 const void *addr, size_t len);
156
157 struct range {
158 PMDK_LIST_ENTRY(range) next;
159 uint64_t first;
160 uint64_t last;
161 };
162
163 struct ranges {
164 PMDK_LIST_HEAD(rangeshead, range) head;
165 };
166
167 pmem_pool_type_t pmem_pool_type_parse_hdr(const struct pool_hdr *hdrp);
168 pmem_pool_type_t pmem_pool_type(const void *base_pool_addr);
169 int pmem_pool_checksum(const void *base_pool_addr);
170 pmem_pool_type_t pmem_pool_type_parse_str(const char *str);
171 uint64_t pmem_pool_get_min_size(pmem_pool_type_t type);
172 int pmem_pool_parse_params(const char *fname, struct pmem_pool_params *paramsp,
173 int check);
174 int util_poolset_map(const char *fname, struct pool_set **poolset, int rdonly);
175 struct options *util_options_alloc(const struct option *options,
176 size_t nopts, const struct option_requirement *req);
177 void util_options_free(struct options *opts);
178 int util_options_verify(const struct options *opts, pmem_pool_type_t type);
179 int util_options_getopt(int argc, char *argv[], const char *optstr,
180 const struct options *opts);
181 pmem_pool_type_t util_get_pool_type_second_page(const void *pool_base_addr);
182 int util_parse_mode(const char *str, mode_t *mode);
183 int util_parse_ranges(const char *str, struct ranges *rangesp,
184 struct range entire);
185 int util_ranges_add(struct ranges *rangesp, struct range range);
186 void util_ranges_clear(struct ranges *rangesp);
187 int util_ranges_contain(const struct ranges *rangesp, uint64_t n);
188 int util_ranges_empty(const struct ranges *rangesp);
189 int util_check_memory(const uint8_t *buff, size_t len, uint8_t val);
190 int util_parse_chunk_types(const char *str, uint64_t *types);
191 int util_parse_lane_sections(const char *str, uint64_t *types);
192 char ask(char op, char *answers, char def_ans, const char *fmt, va_list ap);
193 char ask_Yn(char op, const char *fmt, ...) FORMAT_PRINTF(2, 3);
194 char ask_yN(char op, const char *fmt, ...) FORMAT_PRINTF(2, 3);
195 unsigned util_heap_max_zone(size_t size);
196
197 int util_pool_clear_badblocks(const char *path, int create);
198
199 static const struct range ENTIRE_UINT64 = {
200 { NULL, NULL }, /* range */
201 0, /* first */
202 UINT64_MAX /* last */
203 };