forked from linux-nvme/nvme-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.c
More file actions
284 lines (226 loc) · 5.72 KB
/
lib.c
File metadata and controls
284 lines (226 loc) · 5.72 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
// 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 <libnvme.h>
#include "cleanup.h"
#include "private.h"
#include <libgen.h>
#include <strings.h>
#include <fcntl.h>
#include <sys/ioctl.h>
static bool nvme_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);
}
struct nvme_global_ctx *nvme_create_global_ctx(FILE *fp, int log_level)
{
struct nvme_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 = nvme_mi_probe_enabled_default();
return ctx;
}
void nvme_free_global_ctx(struct nvme_global_ctx *ctx)
{
struct nvme_host *h, *_h;
nvme_mi_ep_t ep, tmp;
if (!ctx)
return;
freeifaddrs(ctx->ifaddrs_cache); /* NULL-safe */
ctx->ifaddrs_cache = NULL;
free(ctx->options);
nvme_for_each_host_safe(ctx, h, _h)
__nvme_free_host(h);
nvme_mi_for_each_endpoint_safe(ctx, ep, tmp)
nvme_mi_close(ep);
free(ctx->config_file);
free(ctx->application);
free(ctx);
}
void nvme_set_dry_run(struct nvme_global_ctx *ctx, bool enable)
{
ctx->dry_run = enable;
}
void nvme_set_ioctl_probing(struct nvme_global_ctx *ctx, bool enable)
{
ctx->ioctl_probing = enable;
}
void nvme_transport_handle_set_submit_entry(struct nvme_transport_handle *hdl,
void *(*submit_entry)(struct nvme_transport_handle *hdl,
struct nvme_passthru_cmd *cmd))
{
hdl->submit_entry = submit_entry;
if (!hdl->submit_exit)
hdl->submit_exit = __nvme_submit_exit;
}
void nvme_transport_handle_set_submit_exit(struct nvme_transport_handle *hdl,
void (*submit_exit)(struct nvme_transport_handle *hdl,
struct nvme_passthru_cmd *cmd,
int err, void *user_data))
{
hdl->submit_exit = submit_exit;
if (!hdl->submit_exit)
hdl->submit_exit = __nvme_submit_exit;
}
void nvme_transport_handle_set_decide_retry(struct nvme_transport_handle *hdl,
bool (*decide_retry)(struct nvme_transport_handle *hdl,
struct nvme_passthru_cmd *cmd, int err))
{
hdl->decide_retry = decide_retry;
if (!hdl->decide_retry)
hdl->decide_retry = __nvme_decide_retry;
}
static int __nvme_transport_handle_open_direct(
struct nvme_transport_handle *hdl, const char *devname)
{
struct nvme_passthru_cmd dummy = { 0 };
_cleanup_free_ char *path = NULL;
char *name;
int ret, id, ns;
bool c = true;
name = nvme_basename(devname);
hdl->type = NVME_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;
} else if (!S_ISBLK(hdl->stat.st_mode)) {
return -EINVAL;
}
if (hdl->ctx->ioctl_probing) {
ret = ioctl(hdl->fd, NVME_IOCTL_ADMIN64_CMD, &dummy);
if (ret > 0)
hdl->ioctl64 = true;
}
return 0;
}
void __nvme_transport_handle_close_direct(struct nvme_transport_handle *hdl)
{
close(hdl->fd);
free(hdl);
}
struct nvme_transport_handle *__nvme_create_transport_handle(
struct nvme_global_ctx *ctx)
{
struct nvme_transport_handle *hdl;
hdl = calloc(1, sizeof(*hdl));
if (!hdl)
return NULL;
hdl->ctx = ctx;
hdl->submit_entry = __nvme_submit_entry;
hdl->submit_exit = __nvme_submit_exit;
hdl->decide_retry = __nvme_decide_retry;
return hdl;
}
int nvme_open(struct nvme_global_ctx *ctx, const char *name,
struct nvme_transport_handle **hdlp)
{
struct nvme_transport_handle *hdl;
int ret;
hdl = __nvme_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 = NVME_TRANSPORT_HANDLE_TYPE_DIRECT;
hdl->fd = 0xFD;
if (!strcmp(name, "NVME_TEST_FD64"))
hdl->ioctl64 = true;
*hdlp = hdl;
return 0;
}
if (!strncmp(name, "mctp:", strlen("mctp:")))
ret = __nvme_transport_handle_open_mi(hdl, name);
else
ret = __nvme_transport_handle_open_direct(hdl, name);
if (ret) {
nvme_close(hdl);
return ret;
}
*hdlp = hdl;
return 0;
}
void nvme_close(struct nvme_transport_handle *hdl)
{
if (!hdl)
return;
free(hdl->name);
switch (hdl->type) {
case NVME_TRANSPORT_HANDLE_TYPE_DIRECT:
__nvme_transport_handle_close_direct(hdl);
break;
case NVME_TRANSPORT_HANDLE_TYPE_MI:
__nvme_transport_handle_close_mi(hdl);
break;
case NVME_TRANSPORT_HANDLE_TYPE_UNKNOWN:
free(hdl);
break;
}
}
int nvme_transport_handle_get_fd(struct nvme_transport_handle *hdl)
{
return hdl->fd;
}
const char *nvme_transport_handle_get_name(struct nvme_transport_handle *hdl)
{
return basename(hdl->name);
}
bool nvme_transport_handle_is_blkdev(struct nvme_transport_handle *hdl)
{
return S_ISBLK(hdl->stat.st_mode);
}
bool nvme_transport_handle_is_chardev(struct nvme_transport_handle *hdl)
{
return S_ISCHR(hdl->stat.st_mode);
}
bool nvme_transport_handle_is_direct(struct nvme_transport_handle *hdl)
{
return hdl->type == NVME_TRANSPORT_HANDLE_TYPE_DIRECT;
}
bool nvme_transport_handle_is_mi(struct nvme_transport_handle *hdl)
{
return hdl->type == NVME_TRANSPORT_HANDLE_TYPE_MI;
}