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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
|
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (c) 2017-2019 Huawei Corporation or its affiliates.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* Author: Zou Ming<zouming.zouming@huawei.com>,
* Yang Feng <philip.yang@huawei.com>
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <inttypes.h>
#include <errno.h>
#include <limits.h>
#include <fcntl.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/stat.h>
#include "common.h"
#include "nvme.h"
#include "libnvme.h"
#include "plugin.h"
#include "util/suffix.h"
#define CREATE_CMD
#include "huawei-nvme.h"
#define HW_SSD_PCI_VENDOR_ID 0x19E5
#define ARRAY_NAME_LEN 80
#define NS_NAME_LEN 40
#define MIN_ARRAY_NAME_LEN 16
#define MIN_NS_NAME_LEN 16
struct huawei_list_item {
char node[1024];
struct nvme_id_ctrl ctrl;
unsigned int nsid;
struct nvme_id_ns ns;
unsigned int block;
char ns_name[NS_NAME_LEN];
char array_name[ARRAY_NAME_LEN];
bool huawei_device;
};
struct huawei_list_element_len {
unsigned int node;
unsigned int ns_name;
unsigned int nguid;
unsigned int ns_id;
unsigned int usage;
unsigned int array_name;
};
static int huawei_get_nvme_info(int fd, struct huawei_list_item *item, const char *node)
{
int err;
int len;
struct stat nvme_stat_info;
memset(item, 0, sizeof(*item));
err = nvme_identify_ctrl(fd, &item->ctrl);
if (err)
return err;
/*identify huawei device*/
if (strstr(item->ctrl.mn, "Huawei") == NULL &&
le16_to_cpu(item->ctrl.vid) != HW_SSD_PCI_VENDOR_ID) {
item->huawei_device = false;
return 0;
}
item->huawei_device = true;
err = nvme_get_nsid(fd, &item->nsid);
err = nvme_identify_ns(fd, item->nsid, &item->ns);
if (err)
return err;
err = fstat(fd, &nvme_stat_info);
if (err < 0)
return err;
strncpy(item->node, node, sizeof(item->node));
item->node[sizeof(item->node) - 1] = '\0';
item->block = S_ISBLK(nvme_stat_info.st_mode);
if (item->ns.vs[0] == 0) {
len = snprintf(item->ns_name, NS_NAME_LEN, "%s", "----");
if (len < 0)
return -EINVAL;
} else {
memcpy(item->ns_name, item->ns.vs, NS_NAME_LEN);
item->ns_name[NS_NAME_LEN - 1] = '\0';
}
if (item->ctrl.vs[0] == 0) {
len = snprintf(item->array_name, ARRAY_NAME_LEN, "%s", "----");
if (len < 0)
return -EINVAL;
} else {
memcpy(item->array_name, item->ctrl.vs, ARRAY_NAME_LEN);
item->array_name[ARRAY_NAME_LEN - 1] = '\0';
}
return 0;
}
static void format(char *formatter, size_t fmt_sz, char *tofmt, size_t tofmtsz)
{
fmt_sz = snprintf(formatter, fmt_sz, "%-*.*s", (int)tofmtsz, (int)tofmtsz, tofmt);
/* trim() the obnoxious trailing white lines */
while (fmt_sz) {
if (formatter[fmt_sz - 1] != ' ' && formatter[fmt_sz - 1] != '\0') {
formatter[fmt_sz] = '\0';
break;
}
fmt_sz--;
}
}
static void huawei_json_print_list_items(struct huawei_list_item *list_items,
unsigned int len)
{
struct json_object *root;
struct json_object *devices;
struct json_object *device_attrs;
char formatter[128] = { 0 };
int index, i = 0;
root = json_create_object();
devices = json_create_array();
for (i = 0; i < len; i++) {
device_attrs = json_create_object();
json_object_add_value_string(device_attrs,
"DevicePath",
list_items[i].node);
if (sscanf(list_items[i].node, "/dev/nvme%d", &index) == 1)
json_object_add_value_int(device_attrs,
"Index",
index);
format(formatter, sizeof(formatter),
list_items[i].ns_name,
sizeof(list_items[i].ns_name));
json_object_add_value_string(device_attrs,
"NS Name",
formatter);
format(formatter, sizeof(formatter),
list_items[i].array_name,
sizeof(list_items[i].array_name));
json_object_add_value_string(device_attrs,
"Array Name",
formatter);
json_array_add_value_object(devices, device_attrs);
}
json_object_add_value_array(root, "Devices", devices);
json_print_object(root, NULL);
printf("\n");
json_free_object(root);
}
static void huawei_print_list_head(struct huawei_list_element_len element_len)
{
char dash[128];
int i;
for (i = 0; i < 128; i++)
dash[i] = '-';
dash[127] = '\0';
printf("%-*.*s %-*.*s %-*.*s %-*.*s %-*.*s %-*.*s\n",
element_len.node, element_len.node, "Node",
element_len.ns_name, element_len.ns_name, "NS Name",
element_len.nguid, element_len.nguid, "Nguid",
element_len.ns_id, element_len.ns_id, "NS ID",
element_len.usage, element_len.usage, "Usage",
element_len.array_name, element_len.array_name, "Array Name");
printf("%-.*s %-.*s %-.*s %-.*s %-.*s %-.*s\n",
element_len.node, dash, element_len.ns_name, dash,
element_len.nguid, dash, element_len.ns_id, dash,
element_len.usage, dash, element_len.array_name, dash);
}
static void huawei_print_list_item(struct huawei_list_item *list_item,
struct huawei_list_element_len element_len)
{
__u8 lba_index;
nvme_id_ns_flbas_to_lbaf_inuse(list_item->ns.flbas, &lba_index);
unsigned long long lba = 1ULL << list_item->ns.lbaf[lba_index].ds;
double nsze = le64_to_cpu(list_item->ns.nsze) * lba;
double nuse = le64_to_cpu(list_item->ns.nuse) * lba;
const char *s_suffix = suffix_si_get(&nsze);
const char *u_suffix = suffix_si_get(&nuse);
char usage[128];
char nguid_buf[2 * sizeof(list_item->ns.nguid) + 1];
char *nguid = nguid_buf;
int i;
sprintf(usage, "%6.2f %2sB / %6.2f %2sB", nuse, u_suffix, nsze, s_suffix);
memset(nguid, 0, sizeof(nguid_buf));
for (i = 0; i < sizeof(list_item->ns.nguid); i++)
nguid += sprintf(nguid, "%02x", list_item->ns.nguid[i]);
printf("%-*.*s %-*.*s %-*.*s %-*d %-*.*s %-*.*s\n",
element_len.node, element_len.node, list_item->node,
element_len.ns_name, element_len.ns_name, list_item->ns_name,
element_len.nguid, element_len.nguid, nguid_buf,
element_len.ns_id, list_item->nsid,
element_len.usage, element_len.usage, usage,
element_len.array_name, element_len.array_name,
list_item->array_name);
}
static unsigned int choose_len(unsigned int old_len, unsigned int cur_len, unsigned int default_len)
{
unsigned int temp_len;
temp_len = (cur_len > default_len) ? cur_len : default_len;
if (temp_len > old_len)
return temp_len;
return old_len;
}
static unsigned int huawei_get_ns_len(struct huawei_list_item *list_items, unsigned int len,
unsigned int default_len)
{
int i;
unsigned int min_len = default_len;
for (i = 0 ; i < len ; i++)
min_len = choose_len(min_len, strlen(list_items->ns_name), default_len);
return min_len;
}
static int huawei_get_array_len(struct huawei_list_item *list_items, unsigned int len,
unsigned int default_len)
{
int i;
int min_len = default_len;
for (i = 0 ; i < len ; i++)
min_len = choose_len(min_len, strlen(list_items->array_name), default_len);
return min_len;
}
static void huawei_print_list_items(struct huawei_list_item *list_items, unsigned int len)
{
unsigned int i;
struct huawei_list_element_len element_len;
element_len.node = 16;
element_len.nguid = 2 * sizeof(list_items->ns.nguid) + 1;
element_len.ns_id = 9;
element_len.usage = 26;
element_len.ns_name = huawei_get_ns_len(list_items, len, MIN_NS_NAME_LEN);
element_len.array_name = huawei_get_array_len(list_items, len, MIN_ARRAY_NAME_LEN);
huawei_print_list_head(element_len);
for (i = 0 ; i < len ; i++)
huawei_print_list_item(&list_items[i], element_len);
}
static int huawei_list(int argc, char **argv, struct command *command,
struct plugin *plugin)
{
char path[264];
struct dirent **devices;
struct huawei_list_item *list_items;
unsigned int i, n, ret;
unsigned int huawei_num = 0;
enum nvme_print_flags fmt;
const char *desc = "Retrieve basic information for the given huawei device";
struct config {
char *output_format;
};
struct config cfg = {
.output_format = "normal",
};
OPT_ARGS(opts) = {
OPT_FMT("output-format", 'o', &cfg.output_format, "Output Format: normal|json"),
OPT_END()
};
ret = argconfig_parse(argc, argv, desc, opts);
if (ret)
return ret;
ret = validate_output_format(cfg.output_format, &fmt);
if (ret < 0 || (fmt != JSON && fmt != NORMAL))
return ret;
n = scandir("/dev", &devices, nvme_namespace_filter, alphasort);
if (n <= 0)
return n;
list_items = calloc(n, sizeof(*list_items));
if (!list_items) {
fprintf(stderr, "can not allocate controller list payload\n");
ret = ENOMEM;
goto out_free_devices;
}
for (i = 0; i < n; i++) {
int fd;
snprintf(path, sizeof(path), "/dev/%s", devices[i]->d_name);
fd = open(path, O_RDONLY);
if (fd < 0) {
fprintf(stderr, "Cannot open device %s: %s\n",
path, strerror(errno));
continue;
}
ret = huawei_get_nvme_info(fd, &list_items[huawei_num], path);
if (ret) {
close(fd);
goto out_free_list_items;
}
if (list_items[huawei_num].huawei_device == true)
huawei_num++;
close(fd);
}
if (huawei_num > 0) {
if (fmt == JSON)
huawei_json_print_list_items(list_items, huawei_num);
else
huawei_print_list_items(list_items, huawei_num);
}
out_free_list_items:
free(list_items);
out_free_devices:
for (i = 0; i < n; i++)
free(devices[i]);
free(devices);
return ret;
}
static void huawei_do_id_ctrl(__u8 *vs, struct json_object *root)
{
char array_name[ARRAY_NAME_LEN + 1] = {0};
memcpy(array_name, vs, ARRAY_NAME_LEN);
if (root)
json_object_add_value_string(root, "array name", strlen(array_name) > 1 ? array_name : "NULL");
else
printf("array name : %s\n", strlen(array_name) > 1 ? array_name : "NULL");
}
static int huawei_id_ctrl(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
return __id_ctrl(argc, argv, cmd, plugin, huawei_do_id_ctrl);
}
|