Skip to content

Commit f31826d

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 a59b113 commit f31826d

16 files changed

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

0 commit comments

Comments
 (0)