-
Notifications
You must be signed in to change notification settings - Fork 710
Expand file tree
/
Copy pathlib.c
More file actions
319 lines (257 loc) · 6.8 KB
/
lib.c
File metadata and controls
319 lines (257 loc) · 6.8 KB
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// SPDX-License-Identifier: LGPL-2.1-or-later
/*
* This file is part of libnvme.
* Copyright (c) 2020 Western Digital Corporation or its affiliates.
*
* Authors: Keith Busch <[email protected]>
* Chaitanya Kulkarni <[email protected]>
*/
#include <fcntl.h>
#include <libgen.h>
#include <strings.h>
#ifdef CONFIG_FABRICS
#include <sys/types.h>
#include <ifaddrs.h>
#endif
#include <sys/ioctl.h>
#include <libnvme.h>
#include "cleanup.h"
#include "cleanup-linux.h"
#include "private.h"
#include "private-mi.h"
#include "compiler-attributes.h"
static bool libnvme_mi_probe_enabled_default(void)
{
char *val;
val = getenv("LIBNVME_MI_PROBE_ENABLED");
if (!val)
return true;
return strcmp(val, "0") &&
strcasecmp(val, "false") &&
strncasecmp(val, "disable", 7);
}
__public struct libnvme_global_ctx *libnvme_create_global_ctx(FILE *fp, int log_level)
{
struct libnvme_global_ctx *ctx;
int fd;
ctx = calloc(1, sizeof(*ctx));
if (!ctx)
return NULL;
if (fp) {
fd = fileno(fp);
if (fd < 0) {
free(ctx);
return NULL;
}
} else
fd = STDERR_FILENO;
ctx->log.fd = fd;
ctx->log.level = log_level;
list_head_init(&ctx->hosts);
list_head_init(&ctx->endpoints);
ctx->ioctl_probing = true;
ctx->mi_probe_enabled = libnvme_mi_probe_enabled_default();
return ctx;
}
__public void libnvme_free_global_ctx(struct libnvme_global_ctx *ctx)
{
struct libnvme_host *h, *_h;
#ifdef CONFIG_MI
libnvme_mi_ep_t ep, tmp;
#endif
if (!ctx)
return;
#ifdef CONFIG_FABRICS
freeifaddrs(ctx->ifaddrs_cache); /* NULL-safe */
ctx->ifaddrs_cache = NULL;
free(ctx->options);
#endif
libnvme_for_each_host_safe(ctx, h, _h)
__libnvme_free_host(h);
#ifdef CONFIG_MI
libnvme_mi_for_each_endpoint_safe(ctx, ep, tmp)
libnvme_mi_close(ep);
#endif
free(ctx->config_file);
free(ctx->application);
libnvme_close_uring(ctx);
free(ctx);
}
__public void libnvme_set_dry_run(struct libnvme_global_ctx *ctx, bool enable)
{
ctx->dry_run = enable;
}
__public void libnvme_set_ioctl_probing(struct libnvme_global_ctx *ctx, bool enable)
{
ctx->ioctl_probing = enable;
}
__public void libnvme_transport_handle_set_submit_entry(struct libnvme_transport_handle *hdl,
void *(*submit_entry)(struct libnvme_transport_handle *hdl,
struct libnvme_passthru_cmd *cmd))
{
hdl->submit_entry = submit_entry;
if (!hdl->submit_exit)
hdl->submit_exit = __libnvme_submit_exit;
}
__public void libnvme_transport_handle_set_submit_exit(struct libnvme_transport_handle *hdl,
void (*submit_exit)(struct libnvme_transport_handle *hdl,
struct libnvme_passthru_cmd *cmd,
int err, void *user_data))
{
hdl->submit_exit = submit_exit;
if (!hdl->submit_exit)
hdl->submit_exit = __libnvme_submit_exit;
}
__public void libnvme_transport_handle_set_decide_retry(struct libnvme_transport_handle *hdl,
bool (*decide_retry)(struct libnvme_transport_handle *hdl,
struct libnvme_passthru_cmd *cmd, int err))
{
hdl->decide_retry = decide_retry;
if (!hdl->decide_retry)
hdl->decide_retry = __libnvme_decide_retry;
}
__public void libnvme_transport_handle_set_timeout(
struct libnvme_transport_handle *hdl, __u32 timeout_ms)
{
hdl->timeout = timeout_ms;
}
static int __nvme_transport_handle_open_direct(
struct libnvme_transport_handle *hdl, const char *devname)
{
struct libnvme_passthru_cmd dummy = { 0 };
__cleanup_free char *path = NULL;
char *name;
int ret, id, ns = 0;
bool c = true;
name = libnvme_basename(devname);
hdl->type = LIBNVME_TRANSPORT_HANDLE_TYPE_DIRECT;
ret = sscanf(name, "nvme%dn%d", &id, &ns);
if (ret == 2)
c = false;
else if (ret != 1 && sscanf(name, "ng%dn%d", &id, &ns) != 2)
return -EINVAL;
ret = asprintf(&path, "%s/%s", "/dev", name);
if (ret < 0)
return -ENOMEM;
hdl->fd = open(path, O_RDONLY);
if (hdl->fd < 0)
return -errno;
ret = fstat(hdl->fd, &hdl->stat);
if (ret < 0)
return -errno;
if (c) {
if (!S_ISCHR(hdl->stat.st_mode))
return -EINVAL;
ret = __libnvme_transport_handle_open_uring(hdl);
if (ret && ret != -ENOTSUP) {
close(hdl->fd);
return ret;
}
} else if (!S_ISBLK(hdl->stat.st_mode)) {
return -EINVAL;
}
if (hdl->ctx->ioctl_probing) {
/* avoid kernel logging 'cmd does not match nsid' */
dummy.nsid = ns;
ret = ioctl(hdl->fd, LIBNVME_IOCTL_ADMIN64_CMD, &dummy);
if (ret > 0) {
hdl->ioctl_admin64 = true;
ret = ioctl(hdl->fd, LIBNVME_IOCTL_IO64_CMD, &dummy);
if (ret != -1 || errno != ENOTTY)
hdl->ioctl_io64 = true;
}
}
return 0;
}
void __libnvme_transport_handle_close_direct(
struct libnvme_transport_handle *hdl)
{
close(hdl->fd);
free(hdl);
}
struct libnvme_transport_handle *__libnvme_create_transport_handle(
struct libnvme_global_ctx *ctx)
{
struct libnvme_transport_handle *hdl;
hdl = calloc(1, sizeof(*hdl));
if (!hdl)
return NULL;
hdl->ctx = ctx;
hdl->submit_entry = __libnvme_submit_entry;
hdl->submit_exit = __libnvme_submit_exit;
hdl->decide_retry = __libnvme_decide_retry;
return hdl;
}
__public int libnvme_open(struct libnvme_global_ctx *ctx, const char *name,
struct libnvme_transport_handle **hdlp)
{
struct libnvme_transport_handle *hdl;
int ret;
hdl = __libnvme_create_transport_handle(ctx);
if (!hdl)
return -ENOMEM;
hdl->name = strdup(name);
if (!hdl->name) {
free(hdl);
return -ENOMEM;
}
if (!strncmp(name, "NVME_TEST_FD", 12)) {
hdl->type = LIBNVME_TRANSPORT_HANDLE_TYPE_DIRECT;
hdl->fd = 0xFD;
if (!strcmp(name, "NVME_TEST_FD64"))
hdl->ioctl_admin64 = true;
*hdlp = hdl;
return 0;
}
if (!strncmp(name, "mctp:", strlen("mctp:")))
ret = __libnvme_transport_handle_open_mi(hdl, name);
else
ret = __nvme_transport_handle_open_direct(hdl, name);
if (ret) {
libnvme_close(hdl);
return ret;
}
*hdlp = hdl;
return 0;
}
__public void libnvme_close(struct libnvme_transport_handle *hdl)
{
if (!hdl)
return;
free(hdl->name);
switch (hdl->type) {
case LIBNVME_TRANSPORT_HANDLE_TYPE_DIRECT:
__libnvme_transport_handle_close_direct(hdl);
break;
case LIBNVME_TRANSPORT_HANDLE_TYPE_MI:
__libnvme_transport_handle_close_mi(hdl);
break;
case LIBNVME_TRANSPORT_HANDLE_TYPE_UNKNOWN:
free(hdl);
break;
}
}
__public int libnvme_transport_handle_get_fd(struct libnvme_transport_handle *hdl)
{
return hdl->fd;
}
__public const char *libnvme_transport_handle_get_name(struct libnvme_transport_handle *hdl)
{
return basename(hdl->name);
}
__public bool libnvme_transport_handle_is_ctrl(struct libnvme_transport_handle *hdl)
{
return S_ISCHR(hdl->stat.st_mode);
}
__public bool libnvme_transport_handle_is_ns(struct libnvme_transport_handle *hdl)
{
return S_ISBLK(hdl->stat.st_mode);
}
__public bool libnvme_transport_handle_is_direct(struct libnvme_transport_handle *hdl)
{
return hdl->type == LIBNVME_TRANSPORT_HANDLE_TYPE_DIRECT;
}
__public bool libnvme_transport_handle_is_mi(struct libnvme_transport_handle *hdl)
{
return hdl->type == LIBNVME_TRANSPORT_HANDLE_TYPE_MI;
}