Skip to content

Commit 8041aa5

Browse files
committed
lib: move library definition into new headers
Refactor all library specific definitions and declarations into two headers. lib-types.h contains the forward declarations for all types used in libnvme. lib.h contains the interfaces and declarations that need to interact with the library. Signed-off-by: Daniel Wagner <[email protected]>
1 parent 4ca0e37 commit 8041aa5

16 files changed

Lines changed: 488 additions & 439 deletions

File tree

libnvme/src/libnvme.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,18 @@
1414
extern "C" {
1515
#endif
1616

17-
#include <nvme/types.h>
1817
#include <nvme/cmds.h>
19-
#include <nvme/linux.h>
20-
#include <nvme/ioctl.h>
21-
#include <nvme/nbft.h>
2218
#include <nvme/fabrics.h>
2319
#include <nvme/filters.h>
20+
#include <nvme/ioctl.h>
21+
#include <nvme/lib-types.h>
22+
#include <nvme/lib.h>
23+
#include <nvme/linux.h>
24+
#include <nvme/log.h>
25+
#include <nvme/nbft.h>
2426
#include <nvme/tree.h>
27+
#include <nvme/types.h>
2528
#include <nvme/util.h>
26-
#include <nvme/log.h>
2729

2830
#ifdef __cplusplus
2931
}

libnvme/src/meson.build

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ sources = [
1212
'nvme/fabrics.c',
1313
'nvme/filters.c',
1414
'nvme/ioctl.c',
15+
'nvme/lib.c',
1516
'nvme/linux.c',
1617
'nvme/log.c',
1718
'nvme/mi-mctp.c',
@@ -101,6 +102,8 @@ install_headers(
101102
'nvme/fabrics.h',
102103
'nvme/filters.h',
103104
'nvme/ioctl.h',
105+
'nvme/lib-types.h',
106+
'nvme/lib.h',
104107
'nvme/linux.h',
105108
'nvme/log.h',
106109
'nvme/mi.h',

libnvme/src/nvme/ioctl.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
#include <linux/types.h>
1313

14+
#include <nvme/lib-types.h>
15+
1416
/**
1517
* DOC: ioctl.h
1618
*
@@ -117,8 +119,6 @@ struct nvme_uring_cmd {
117119
__u32 rsvd2;
118120
};
119121

120-
struct nvme_transport_handle;
121-
122122
/**
123123
* nvme_submit_admin_passthru() - Submit an nvme passthrough admin command
124124
* @hdl: Transport handle

libnvme/src/nvme/lib-types.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// SPDX-License-Identifier: LGPL-2.1-or-later
2+
/*
3+
* This file is part of libnvme.
4+
* Copyright (c) 2020 Western Digital Corporation or its affiliates.
5+
*
6+
* Authors: Keith Busch <[email protected]>
7+
* Chaitanya Kulkarni <[email protected]>
8+
*/
9+
#pragma once
10+
11+
struct nvme_global_ctx;
12+
struct nvme_passthru_cmd;
13+
struct nvme_transport_handle;

libnvme/src/nvme/lib.c

Lines changed: 260 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,260 @@
1+
// SPDX-License-Identifier: LGPL-2.1-or-later
2+
/*
3+
* This file is part of libnvme.
4+
* Copyright (c) 2020 Western Digital Corporation or its affiliates.
5+
*
6+
* Authors: Keith Busch <[email protected]>
7+
* Chaitanya Kulkarni <[email protected]>
8+
*/
9+
10+
#include <libnvme.h>
11+
12+
#include "cleanup.h"
13+
#include "private.h"
14+
15+
#include <fcntl.h>
16+
#include <sys/ioctl.h>
17+
18+
struct nvme_global_ctx *nvme_create_global_ctx(FILE *fp, int log_level)
19+
{
20+
struct nvme_global_ctx *ctx;
21+
int fd;
22+
23+
ctx = calloc(1, sizeof(*ctx));
24+
if (!ctx)
25+
return NULL;
26+
27+
if (fp) {
28+
fd = fileno(fp);
29+
if (fd < 0) {
30+
free(ctx);
31+
return NULL;
32+
}
33+
} else
34+
fd = STDERR_FILENO;
35+
36+
ctx->log.fd = fd;
37+
ctx->log.level = log_level;
38+
39+
list_head_init(&ctx->hosts);
40+
list_head_init(&ctx->endpoints);
41+
42+
ctx->ioctl_probing = true;
43+
44+
return ctx;
45+
}
46+
47+
void nvme_free_global_ctx(struct nvme_global_ctx *ctx)
48+
{
49+
struct nvme_host *h, *_h;
50+
51+
if (!ctx)
52+
return;
53+
54+
freeifaddrs(ctx->ifaddrs_cache); /* NULL-safe */
55+
ctx->ifaddrs_cache = NULL;
56+
57+
free(ctx->options);
58+
nvme_for_each_host_safe(ctx, h, _h)
59+
__nvme_free_host(h);
60+
free(ctx->config_file);
61+
free(ctx->application);
62+
free(ctx);
63+
}
64+
65+
void nvme_set_dry_run(struct nvme_global_ctx *ctx, bool enable)
66+
{
67+
ctx->dry_run = enable;
68+
}
69+
70+
void nvme_set_ioctl_probing(struct nvme_global_ctx *ctx, bool enable)
71+
{
72+
ctx->ioctl_probing = enable;
73+
}
74+
75+
void nvme_transport_handle_set_submit_entry(struct nvme_transport_handle *hdl,
76+
void *(*submit_entry)(struct nvme_transport_handle *hdl,
77+
struct nvme_passthru_cmd *cmd))
78+
{
79+
hdl->submit_entry = submit_entry;
80+
if (!hdl->submit_exit)
81+
hdl->submit_exit = __nvme_submit_exit;
82+
}
83+
84+
void nvme_transport_handle_set_submit_exit(struct nvme_transport_handle *hdl,
85+
void (*submit_exit)(struct nvme_transport_handle *hdl,
86+
struct nvme_passthru_cmd *cmd,
87+
int err, void *user_data))
88+
{
89+
hdl->submit_exit = submit_exit;
90+
if (!hdl->submit_exit)
91+
hdl->submit_exit = __nvme_submit_exit;
92+
}
93+
94+
void nvme_transport_handle_set_decide_retry(struct nvme_transport_handle *hdl,
95+
bool (*decide_retry)(struct nvme_transport_handle *hdl,
96+
struct nvme_passthru_cmd *cmd, int err))
97+
{
98+
hdl->decide_retry = decide_retry;
99+
if (!hdl->decide_retry)
100+
hdl->decide_retry = __nvme_decide_retry;
101+
}
102+
103+
static int __nvme_transport_handle_open_direct(struct nvme_transport_handle *hdl, const char *devname)
104+
{
105+
struct nvme_passthru_cmd dummy = { 0 };
106+
_cleanup_free_ char *path = NULL;
107+
char *name = basename(devname);
108+
int ret, id, ns;
109+
bool c = true;
110+
111+
hdl->type = NVME_TRANSPORT_HANDLE_TYPE_DIRECT;
112+
113+
ret = sscanf(name, "nvme%dn%d", &id, &ns);
114+
if (ret == 2)
115+
c = false;
116+
else if (ret != 1 && sscanf(name, "ng%dn%d", &id, &ns) != 2)
117+
return -EINVAL;
118+
119+
ret = asprintf(&path, "%s/%s", "/dev", name);
120+
if (ret < 0)
121+
return -ENOMEM;
122+
123+
hdl->fd = open(path, O_RDONLY);
124+
if (hdl->fd < 0)
125+
return -errno;
126+
127+
ret = fstat(hdl->fd, &hdl->stat);
128+
if (ret < 0)
129+
return -errno;
130+
131+
if (c) {
132+
if (!S_ISCHR(hdl->stat.st_mode))
133+
return -EINVAL;
134+
} else if (!S_ISBLK(hdl->stat.st_mode)) {
135+
return -EINVAL;
136+
}
137+
138+
if (hdl->ctx->ioctl_probing) {
139+
ret = ioctl(hdl->fd, NVME_IOCTL_ADMIN64_CMD, &dummy);
140+
if (ret > 0)
141+
hdl->ioctl64 = true;
142+
}
143+
144+
return 0;
145+
}
146+
147+
void __nvme_transport_handle_close_direct(struct nvme_transport_handle *hdl)
148+
{
149+
close(hdl->fd);
150+
free(hdl);
151+
}
152+
153+
struct nvme_transport_handle *__nvme_create_transport_handle(struct nvme_global_ctx *ctx)
154+
{
155+
struct nvme_transport_handle *hdl;
156+
157+
hdl = calloc(1, sizeof(*hdl));
158+
if (!hdl)
159+
return NULL;
160+
161+
hdl->ctx = ctx;
162+
hdl->submit_entry = __nvme_submit_entry;
163+
hdl->submit_exit = __nvme_submit_exit;
164+
hdl->decide_retry = __nvme_decide_retry;
165+
166+
return hdl;
167+
}
168+
169+
int nvme_open(struct nvme_global_ctx *ctx, const char *name,
170+
struct nvme_transport_handle **hdlp)
171+
{
172+
struct nvme_transport_handle *hdl;
173+
int ret;
174+
175+
hdl = __nvme_create_transport_handle(ctx);
176+
if (!hdl)
177+
return -ENOMEM;
178+
179+
hdl->name = strdup(name);
180+
if (!hdl->name) {
181+
free(hdl);
182+
return -ENOMEM;
183+
}
184+
185+
if (!strncmp(name, "NVME_TEST_FD", 12)) {
186+
hdl->type = NVME_TRANSPORT_HANDLE_TYPE_DIRECT;
187+
hdl->fd = 0xFD;
188+
189+
if (!strcmp(name, "NVME_TEST_FD64"))
190+
hdl->ioctl64 = true;
191+
192+
*hdlp = hdl;
193+
return 0;
194+
}
195+
196+
if (!strncmp(name, "mctp:", strlen("mctp:")))
197+
ret = __nvme_transport_handle_open_mi(hdl, name);
198+
else
199+
ret = __nvme_transport_handle_open_direct(hdl, name);
200+
201+
if (ret) {
202+
nvme_close(hdl);
203+
return ret;
204+
}
205+
206+
*hdlp = hdl;
207+
208+
return 0;
209+
}
210+
211+
void nvme_close(struct nvme_transport_handle *hdl)
212+
{
213+
if (!hdl)
214+
return;
215+
216+
free(hdl->name);
217+
218+
switch (hdl->type) {
219+
case NVME_TRANSPORT_HANDLE_TYPE_DIRECT:
220+
__nvme_transport_handle_close_direct(hdl);
221+
break;
222+
case NVME_TRANSPORT_HANDLE_TYPE_MI:
223+
__nvme_transport_handle_close_mi(hdl);
224+
break;
225+
case NVME_TRANSPORT_HANDLE_TYPE_UNKNOWN:
226+
free(hdl);
227+
break;
228+
}
229+
}
230+
231+
int nvme_transport_handle_get_fd(struct nvme_transport_handle *hdl)
232+
{
233+
return hdl->fd;
234+
}
235+
236+
const char *nvme_transport_handle_get_name(struct nvme_transport_handle *hdl)
237+
{
238+
return basename(hdl->name);
239+
}
240+
241+
bool nvme_transport_handle_is_blkdev(struct nvme_transport_handle *hdl)
242+
{
243+
return S_ISBLK(hdl->stat.st_mode);
244+
}
245+
246+
bool nvme_transport_handle_is_chardev(struct nvme_transport_handle *hdl)
247+
{
248+
return S_ISCHR(hdl->stat.st_mode);
249+
}
250+
251+
bool nvme_transport_handle_is_direct(struct nvme_transport_handle *hdl)
252+
{
253+
return hdl->type == NVME_TRANSPORT_HANDLE_TYPE_DIRECT;
254+
}
255+
256+
bool nvme_transport_handle_is_mi(struct nvme_transport_handle *hdl)
257+
{
258+
return hdl->type == NVME_TRANSPORT_HANDLE_TYPE_MI;
259+
}
260+

0 commit comments

Comments
 (0)