-
Notifications
You must be signed in to change notification settings - Fork 509
/
Copy pathregion_namespace_ndctl.c
289 lines (234 loc) · 5.73 KB
/
region_namespace_ndctl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// SPDX-License-Identifier: BSD-3-Clause
/* Copyright 2020-2023, Intel Corporation */
/*
* region_namespace_ndctl.c -- common ndctl functions
*/
#include <ndctl/libndctl.h>
#include <daxctl/libdaxctl.h>
#include <sys/sysmacros.h>
#include <fcntl.h>
#include "libpmem2.h"
#include "pmem2_utils.h"
#include "region_namespace_ndctl.h"
#include "region_namespace.h"
#include "out.h"
#include "alloc.h"
/*
* ndctl_match_devdax -- (internal) returns 0 if the devdax matches
* with the given file, 1 if it doesn't match,
* and a negative value in case of an error.
*/
static int
ndctl_match_devdax(dev_t st_rdev, const char *devname)
{
LOG(3, "st_rdev %lu devname %s", st_rdev, devname);
int ret = 0;
char *path;
os_stat_t stat;
if (*devname == '\0')
return 1;
path = Malloc(PATH_MAX * sizeof(char));
if (path == NULL) {
ERR("!Malloc");
return PMEM2_E_ERRNO;
}
if (util_snprintf(path, PATH_MAX, "/dev/%s", devname) < 0) {
ERR("!snprintf");
ret = PMEM2_E_ERRNO;
goto end;
}
if (os_stat(path, &stat)) {
ERR("!stat %s", path);
ret = PMEM2_E_ERRNO;
goto end;
}
if (st_rdev != stat.st_rdev) {
LOG(10, "skipping not matching device: %s", path);
ret = 1;
goto end;
}
LOG(4, "found matching device: %s", path);
end:
Free(path);
return ret;
}
#define BUFF_LENGTH 64
/*
* ndctl_match_fsdax -- (internal) returns 0 if the device matches
* with the given file, 1 if it doesn't match,
* and a negative value in case of an error.
*/
static int
ndctl_match_fsdax(dev_t st_dev, const char *devname)
{
LOG(3, "st_dev %lu devname %s", st_dev, devname);
int ret = 0;
char *path;
char dev_id[BUFF_LENGTH];
if (*devname == '\0')
return 1;
path = Malloc(PATH_MAX * sizeof(char));
if (path == NULL) {
ERR("!Malloc");
return PMEM2_E_ERRNO;
}
if (util_snprintf(path, PATH_MAX, "/sys/block/%s/dev", devname) < 0) {
ERR("!snprintf");
ret = PMEM2_E_ERRNO;
goto end;
}
if (util_snprintf(dev_id, BUFF_LENGTH, "%d:%d",
major(st_dev), minor(st_dev)) < 0) {
ERR("!snprintf");
ret = PMEM2_E_ERRNO;
goto end;
}
int fd = os_open(path, O_RDONLY);
if (fd < 0) {
ERR("!open \"%s\"", path);
ret = PMEM2_E_ERRNO;
goto end;
}
char buff[BUFF_LENGTH];
ssize_t nread = read(fd, buff, BUFF_LENGTH);
if (nread < 0) {
ERR("!read");
int oerrno = errno; /* save the errno */
os_close(fd);
errno = oerrno;
ret = PMEM2_E_ERRNO;
goto end;
}
os_close(fd);
if (nread == 0) {
ERR("%s is empty", path);
ret = PMEM2_E_INVALID_DEV_FORMAT;
goto end;
}
if (buff[nread - 1] != '\n') {
ERR("%s doesn't end with new line", path);
ret = PMEM2_E_INVALID_DEV_FORMAT;
goto end;
}
buff[nread - 1] = '\0';
if (strcmp(buff, dev_id) != 0) {
LOG(10, "skipping not matching device: %s", path);
ret = 1;
goto end;
}
LOG(4, "found matching device: %s", path);
end:
Free(path);
return ret;
}
/*
* pmem2_region_namespace -- returns the region
* (and optionally the namespace)
* where the given file is located
*/
int
pmem2_region_namespace(struct ndctl_ctx *ctx,
const struct pmem2_source *src,
struct ndctl_region **pregion,
struct ndctl_namespace **pndns)
{
LOG(3, "ctx %p src %p pregion %p pnamespace %p",
ctx, src, pregion, pndns);
struct ndctl_bus *bus;
struct ndctl_region *region;
struct ndctl_namespace *ndns;
if (pregion)
*pregion = NULL;
if (pndns)
*pndns = NULL;
if (src->value.ftype == PMEM2_FTYPE_DIR) {
ERR("cannot check region or namespace of a directory");
return PMEM2_E_INVALID_FILE_TYPE;
}
FOREACH_BUS_REGION_NAMESPACE(ctx, bus, region, ndns) {
struct ndctl_btt *btt;
struct ndctl_dax *dax = NULL;
struct ndctl_pfn *pfn;
const char *devname;
if ((dax = ndctl_namespace_get_dax(ndns))) {
if (src->value.ftype == PMEM2_FTYPE_REG)
continue;
ASSERTeq(src->value.ftype, PMEM2_FTYPE_DEVDAX);
struct daxctl_region *dax_region;
dax_region = ndctl_dax_get_daxctl_region(dax);
if (!dax_region) {
ERR("!cannot find dax region");
return PMEM2_E_DAX_REGION_NOT_FOUND;
}
struct daxctl_dev *dev;
daxctl_dev_foreach(dax_region, dev) {
devname = daxctl_dev_get_devname(dev);
int ret = ndctl_match_devdax(src->value.st_rdev,
devname);
if (ret < 0)
return ret;
if (ret == 0) {
if (pregion)
*pregion = region;
if (pndns)
*pndns = ndns;
return 0;
}
}
} else {
if (src->value.ftype == PMEM2_FTYPE_DEVDAX)
continue;
ASSERTeq(src->value.ftype, PMEM2_FTYPE_REG);
if ((btt = ndctl_namespace_get_btt(ndns))) {
devname = ndctl_btt_get_block_device(btt);
} else if ((pfn = ndctl_namespace_get_pfn(ndns))) {
devname = ndctl_pfn_get_block_device(pfn);
} else {
devname =
ndctl_namespace_get_block_device(ndns);
}
int ret = ndctl_match_fsdax(src->value.st_dev, devname);
if (ret < 0)
return ret;
if (ret == 0) {
if (pregion)
*pregion = region;
if (pndns)
*pndns = ndns;
return 0;
}
}
}
LOG(10, "did not found any matching device");
return 0;
}
/*
* pmem2_region_get_id -- returns the region id
*/
int
pmem2_get_region_id(const struct pmem2_source *src, unsigned *region_id)
{
LOG(3, "src %p region_id %p", src, region_id);
struct ndctl_region *region;
struct ndctl_namespace *ndns;
struct ndctl_ctx *ctx;
errno = ndctl_new(&ctx) * (-1);
if (errno) {
ERR("!ndctl_new");
return PMEM2_E_ERRNO;
}
int rv = pmem2_region_namespace(ctx, src, ®ion, &ndns);
if (rv) {
LOG(1, "getting region and namespace failed");
goto end;
}
if (!region) {
ERR("unknown region");
rv = PMEM2_E_DAX_REGION_NOT_FOUND;
goto end;
}
*region_id = ndctl_region_get_id(region);
end:
ndctl_unref(ctx);
return rv;
}