Skip to content

Commit 99925c9

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 1cb5e2a commit 99925c9

16 files changed

Lines changed: 492 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: 262 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,262 @@
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(
104+
struct nvme_transport_handle *hdl, const char *devname)
105+
{
106+
struct nvme_passthru_cmd dummy = { 0 };
107+
_cleanup_free_ char *path = NULL;
108+
char *name = basename(devname);
109+
int ret, id, ns;
110+
bool c = true;
111+
112+
hdl->type = NVME_TRANSPORT_HANDLE_TYPE_DIRECT;
113+
114+
ret = sscanf(name, "nvme%dn%d", &id, &ns);
115+
if (ret == 2)
116+
c = false;
117+
else if (ret != 1 && sscanf(name, "ng%dn%d", &id, &ns) != 2)
118+
return -EINVAL;
119+
120+
ret = asprintf(&path, "%s/%s", "/dev", name);
121+
if (ret < 0)
122+
return -ENOMEM;
123+
124+
hdl->fd = open(path, O_RDONLY);
125+
if (hdl->fd < 0)
126+
return -errno;
127+
128+
ret = fstat(hdl->fd, &hdl->stat);
129+
if (ret < 0)
130+
return -errno;
131+
132+
if (c) {
133+
if (!S_ISCHR(hdl->stat.st_mode))
134+
return -EINVAL;
135+
} else if (!S_ISBLK(hdl->stat.st_mode)) {
136+
return -EINVAL;
137+
}
138+
139+
if (hdl->ctx->ioctl_probing) {
140+
ret = ioctl(hdl->fd, NVME_IOCTL_ADMIN64_CMD, &dummy);
141+
if (ret > 0)
142+
hdl->ioctl64 = true;
143+
}
144+
145+
return 0;
146+
}
147+
148+
void __nvme_transport_handle_close_direct(struct nvme_transport_handle *hdl)
149+
{
150+
close(hdl->fd);
151+
free(hdl);
152+
}
153+
154+
struct nvme_transport_handle *__nvme_create_transport_handle(
155+
struct nvme_global_ctx *ctx)
156+
{
157+
struct nvme_transport_handle *hdl;
158+
159+
hdl = calloc(1, sizeof(*hdl));
160+
if (!hdl)
161+
return NULL;
162+
163+
hdl->ctx = ctx;
164+
hdl->submit_entry = __nvme_submit_entry;
165+
hdl->submit_exit = __nvme_submit_exit;
166+
hdl->decide_retry = __nvme_decide_retry;
167+
168+
return hdl;
169+
}
170+
171+
int nvme_open(struct nvme_global_ctx *ctx, const char *name,
172+
struct nvme_transport_handle **hdlp)
173+
{
174+
struct nvme_transport_handle *hdl;
175+
int ret;
176+
177+
hdl = __nvme_create_transport_handle(ctx);
178+
if (!hdl)
179+
return -ENOMEM;
180+
181+
hdl->name = strdup(name);
182+
if (!hdl->name) {
183+
free(hdl);
184+
return -ENOMEM;
185+
}
186+
187+
if (!strncmp(name, "NVME_TEST_FD", 12)) {
188+
hdl->type = NVME_TRANSPORT_HANDLE_TYPE_DIRECT;
189+
hdl->fd = 0xFD;
190+
191+
if (!strcmp(name, "NVME_TEST_FD64"))
192+
hdl->ioctl64 = true;
193+
194+
*hdlp = hdl;
195+
return 0;
196+
}
197+
198+
if (!strncmp(name, "mctp:", strlen("mctp:")))
199+
ret = __nvme_transport_handle_open_mi(hdl, name);
200+
else
201+
ret = __nvme_transport_handle_open_direct(hdl, name);
202+
203+
if (ret) {
204+
nvme_close(hdl);
205+
return ret;
206+
}
207+
208+
*hdlp = hdl;
209+
210+
return 0;
211+
}
212+
213+
void nvme_close(struct nvme_transport_handle *hdl)
214+
{
215+
if (!hdl)
216+
return;
217+
218+
free(hdl->name);
219+
220+
switch (hdl->type) {
221+
case NVME_TRANSPORT_HANDLE_TYPE_DIRECT:
222+
__nvme_transport_handle_close_direct(hdl);
223+
break;
224+
case NVME_TRANSPORT_HANDLE_TYPE_MI:
225+
__nvme_transport_handle_close_mi(hdl);
226+
break;
227+
case NVME_TRANSPORT_HANDLE_TYPE_UNKNOWN:
228+
free(hdl);
229+
break;
230+
}
231+
}
232+
233+
int nvme_transport_handle_get_fd(struct nvme_transport_handle *hdl)
234+
{
235+
return hdl->fd;
236+
}
237+
238+
const char *nvme_transport_handle_get_name(struct nvme_transport_handle *hdl)
239+
{
240+
return basename(hdl->name);
241+
}
242+
243+
bool nvme_transport_handle_is_blkdev(struct nvme_transport_handle *hdl)
244+
{
245+
return S_ISBLK(hdl->stat.st_mode);
246+
}
247+
248+
bool nvme_transport_handle_is_chardev(struct nvme_transport_handle *hdl)
249+
{
250+
return S_ISCHR(hdl->stat.st_mode);
251+
}
252+
253+
bool nvme_transport_handle_is_direct(struct nvme_transport_handle *hdl)
254+
{
255+
return hdl->type == NVME_TRANSPORT_HANDLE_TYPE_DIRECT;
256+
}
257+
258+
bool nvme_transport_handle_is_mi(struct nvme_transport_handle *hdl)
259+
{
260+
return hdl->type == NVME_TRANSPORT_HANDLE_TYPE_MI;
261+
}
262+

0 commit comments

Comments
 (0)