forked from linux-nvme/nvme-cli
-
Notifications
You must be signed in to change notification settings - Fork 3
/
lnvm-nvme.c
365 lines (302 loc) · 11 KB
/
lnvm-nvme.c
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#include <stdio.h>
#include <errno.h>
#include "nvme.h"
#include "nvme-print.h"
#include "nvme-ioctl.h"
#include "plugin.h"
#include "nvme-lightnvm.h"
#include "argconfig.h"
#include "suffix.h"
#define CREATE_CMD
#include "lnvm-nvme.h"
static int lnvm_init(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
const char *desc = "Initialize LightNVM device. A LightNVM/Open-Channel SSD"\
" must have a media manager associated before it can "\
" be exposed to the user. The default is to initialize"
" the general media manager on top of the device.\n\n"
"Example:"
" lnvm-init -d nvme0n1";
const char *devname = "identifier of desired device. e.g. nvme0n1.";
const char *mmtype = "media manager to initialize on top of device. Default: gennvm.";
int ret;
struct config
{
char *devname;
char *mmtype;
};
struct config cfg = {
.devname = "",
.mmtype = "gennvm",
};
const struct argconfig_commandline_options command_line_options[] = {
{"device-name", 'd', "DEVICE", CFG_STRING, &cfg.devname, required_argument, devname},
{"mediamgr-name", 'm', "MM", CFG_STRING, &cfg.mmtype, required_argument, mmtype},
{NULL}
};
ret = argconfig_parse(argc, argv, desc, command_line_options, &cfg, sizeof(cfg));
if (ret < 0)
return ret;
if (!strlen(cfg.devname)) {
fprintf(stderr, "device name missing %d\n", (int)strlen(cfg.devname));
return -EINVAL;
}
return lnvm_do_init(cfg.devname, cfg.mmtype);
}
static int lnvm_list(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
const char *desc = "List all devices registered with LightNVM.";
int ret;
const struct argconfig_commandline_options command_line_options[] = {
{NULL}
};
ret = argconfig_parse(argc, argv, desc, command_line_options, NULL, 0);
if (ret < 0)
return ret;
return lnvm_do_list_devices();
}
static int lnvm_info(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
const char *desc = "Show general information and registered target types with LightNVM";
int ret;
const struct argconfig_commandline_options command_line_options[] = {
{NULL}
};
ret = argconfig_parse(argc, argv, desc, command_line_options, NULL, 0);
if (ret < 0)
return ret;
return lnvm_do_info();
}
static int lnvm_id_ns(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
const char *desc = "Send an Identify Geometry command to the "\
"given LightNVM device, returns properties of the specified "\
"namespace in either human-readable or binary format.";
const char *raw_binary = "show infos in binary format";
const char *human_readable = "show infos in readable format";
const char *namespace_id = "identifier of desired namespace. default: 1";
unsigned int fd, flags = 0;
struct config {
__u32 namespace_id;
int raw_binary;
int human_readable;
};
struct config cfg = {
.namespace_id = 1,
};
const struct argconfig_commandline_options command_line_options[] = {
{"namespace-id", 'n', "NUM", CFG_POSITIVE, &cfg.namespace_id, required_argument, namespace_id},
{"raw-binary", 'b', "FLAG", CFG_NONE, &cfg.raw_binary, no_argument, raw_binary},
{"human-readable", 'H', "FLAG", CFG_NONE, &cfg.human_readable, no_argument, human_readable},
{NULL}
};
fd = parse_and_open(argc, argv, desc, command_line_options, &cfg, sizeof(cfg));
if (fd < 0)
return fd;
if (cfg.human_readable)
flags |= HUMAN;
else if (cfg.raw_binary)
flags |= RAW;
return lnvm_do_id_ns(fd, cfg.namespace_id, flags);
}
static int lnvm_create_tgt(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
const char *desc = "Instantiate a target on top of a LightNVM enabled device.";
const char *devname = "identifier of desired device. e.g. nvme0n1.";
const char *tgtname = "target name of the device to initialize. e.g. target0.";
const char *tgttype = "identifier of target type. e.g. pblk.";
const char *lun_begin = "Define begin of luns to use for target.";
const char *lun_end = "Define set of luns to use for target.";
const char *over_prov = "Define over-provision percentage for target.";
const char *flag_factory = "Create target in factory mode";
int flags;
int ret;
struct config
{
char *devname;
char *tgtname;
char *tgttype;
__u32 lun_begin;
__u32 lun_end;
__u32 over_prov;
/* flags */
__u32 factory;
};
struct config cfg = {
.devname = "",
.tgtname = "",
.tgttype = "",
.lun_begin = -1,
.lun_end = -1,
.over_prov = -1,
.factory = 0,
};
const struct argconfig_commandline_options command_line_options[] = {
{"device-name", 'd', "DEVICE", CFG_STRING, &cfg.devname, required_argument, devname},
{"target-name", 'n', "TARGET", CFG_STRING, &cfg.tgtname, required_argument, tgtname},
{"target-type", 't', "TARGETTYPE", CFG_STRING, &cfg.tgttype, required_argument, tgttype},
{"lun-begin", 'b', "NUM", CFG_POSITIVE, &cfg.lun_begin, required_argument, lun_begin},
{"lun-end", 'e', "NUM", CFG_POSITIVE, &cfg.lun_end, required_argument, lun_end},
{"over-prov", 'o', "NUM", CFG_POSITIVE, &cfg.over_prov, required_argument, over_prov},
{"factory", 'f', "FLAG", CFG_NONE, &cfg.factory, no_argument, flag_factory},
{NULL}
};
ret = argconfig_parse(argc, argv, desc, command_line_options, &cfg, sizeof(cfg));
if (ret < 0)
return ret;
if (!strlen(cfg.devname)) {
fprintf(stderr, "device name missing %d\n", (int)strlen(cfg.devname));
return -EINVAL;
}
if (!strlen(cfg.tgtname)) {
fprintf(stderr, "target name missing\n");
return -EINVAL;
}
if (!strlen(cfg.tgttype)) {
fprintf(stderr, "target type missing\n");
return -EINVAL;
}
flags = 0;
if (cfg.factory)
flags |= NVM_TARGET_FACTORY;
return lnvm_do_create_tgt(cfg.devname, cfg.tgtname, cfg.tgttype, cfg.lun_begin, cfg.lun_end, cfg.over_prov, flags);
}
static int lnvm_remove_tgt(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
const char *desc = "Remove an initialized LightNVM target.";
const char *tgtname = "target name of the device to initialize. e.g. target0.";
int ret;
struct config
{
char *tgtname;
};
struct config cfg = {
.tgtname = "",
};
const struct argconfig_commandline_options command_line_options[] = {
{"target-name", 'n', "TARGET", CFG_STRING, &cfg.tgtname, required_argument, tgtname},
{NULL}
};
ret = argconfig_parse(argc, argv, desc, command_line_options, &cfg, sizeof(cfg));
if (ret < 0)
return ret;
if (!strlen(cfg.tgtname)) {
fprintf(stderr, "target name missing\n");
return -EINVAL;
}
return lnvm_do_remove_tgt(cfg.tgtname);
}
static int lnvm_factory_init(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
const char *desc = "Factory initialize a LightNVM enabled device.";
const char *devname = "identifier of desired device. e.g. nvme0n1.";
const char *erase_only_marked = "only erase marked blocks. default: all blocks.";
const char *host_marks = "remove host side blocks list. default: keep.";
const char *bb_marks = "remove grown bad blocks list. default: keep";
int ret;
struct config
{
char *devname;
int erase_only_marked;
int clear_host_marks;
int clear_bb_marks;
};
struct config cfg = {
.devname = "",
};
const struct argconfig_commandline_options command_line_options[] = {
{"device-name", 'd', "DEVICE", CFG_STRING, &cfg.devname, required_argument, devname},
{"erase-only-marked", 'e', "", CFG_NONE, &cfg.erase_only_marked, no_argument, erase_only_marked},
{"clear-host-side-blks", 's', "", CFG_NONE, &cfg.clear_host_marks, no_argument, host_marks},
{"clear-bb-blks", 'b', "", CFG_NONE, &cfg.clear_bb_marks, no_argument, bb_marks},
{NULL}
};
ret = argconfig_parse(argc, argv, desc, command_line_options, &cfg,
sizeof(cfg));
if (ret < 0)
return ret;
if (!strlen(cfg.devname)) {
fprintf(stderr, "device name missing %d\n", (int)strlen(cfg.devname));
return -EINVAL;
}
return lnvm_do_factory_init(cfg.devname, cfg.erase_only_marked,
cfg.clear_host_marks, cfg.clear_bb_marks);
}
static int lnvm_get_bbtbl(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
const char *desc = "Receive bad block table from a LightNVM compatible"\
" device.";
const char *namespace = "(optional) desired namespace";
const char *ch = "channel identifier";
const char *lun = "lun identifier (within a channel)";
const char *raw_binary = "show infos in binary format";
unsigned int fd, flags = 0;
struct config
{
__u32 namespace_id;
__u16 lunid;
__u16 chid;
int raw_binary;
};
struct config cfg = {
.namespace_id = 1,
.lunid = 0,
.chid = 0,
};
const struct argconfig_commandline_options command_line_options[] = {
{"namespace-id", 'n', "NUM", CFG_POSITIVE, &cfg.namespace_id, required_argument, namespace},
{"channel-id", 'c', "", CFG_SHORT, &cfg.chid, required_argument, ch},
{"lun-id", 'l', "", CFG_SHORT, &cfg.lunid, required_argument, lun},
{"raw-binary", 'b', "FLAG", CFG_NONE, &cfg.raw_binary, no_argument, raw_binary},
{NULL}
};
fd = parse_and_open(argc, argv, desc, command_line_options, &cfg, sizeof(cfg));
if (cfg.raw_binary)
flags |= RAW;
return lnvm_do_get_bbtbl(fd, cfg.namespace_id, cfg.lunid, cfg.chid,
flags);
}
static int lnvm_set_bbtbl(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
const char *desc = "Update bad block table on a LightNVM compatible"\
" device.";
const char *namespace = "(optional) desired namespace";
const char *ch = "channel identifier";
const char *lun = "lun identifier (within a channel)";
const char *pln = "plane identifier (within a lun)";
const char *blk = "block identifier (within a plane)";
const char *value = "value to update the specific block to.";
int fd;
struct config
{
__u32 namespace_id;
__u16 lunid;
__u16 chid;
__u16 plnid;
__u16 blkid;
__u16 value;
};
struct config cfg = {
.namespace_id = 1,
.lunid = 0,
.chid = 0,
.plnid = 0,
.blkid = 0,
.value = 0,
};
const struct argconfig_commandline_options command_line_options[] = {
{"namespace-id", 'n', "NUM", CFG_POSITIVE, &cfg.namespace_id, required_argument, namespace},
{"channel-id", 'c', "NUM", CFG_SHORT, &cfg.chid, required_argument, ch},
{"lun-id", 'l', "NUM", CFG_SHORT, &cfg.lunid, required_argument, lun},
{"plane-id", 'p', "NUM", CFG_SHORT, &cfg.plnid, required_argument, pln},
{"block-id", 'b', "NUM", CFG_SHORT, &cfg.blkid, required_argument, blk},
{"value", 'v', "NUM", CFG_SHORT, &cfg.value, required_argument, value},
{NULL}
};
fd = parse_and_open(argc, argv, desc, command_line_options, &cfg, sizeof(cfg));
printf("Updating: Ch.: %u LUN: %u Plane: %u Block: %u -> %u\n",
cfg.chid, cfg.lunid, cfg.plnid, cfg.blkid, cfg.value);
return lnvm_do_set_bbtbl(fd, cfg.namespace_id,
cfg.chid, cfg.lunid, cfg.plnid, cfg.blkid,
cfg.value);
}