forked from linux-nvme/nvme-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
netapp-nvme.c
293 lines (253 loc) · 7.82 KB
/
netapp-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
/*
* Copyright (c) 2018 NetApp, Inc.
*
* 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.
*
*/
#include <stdio.h>
#include <dirent.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include "nvme.h"
#include "nvme-ioctl.h"
#include "json.h"
#include "suffix.h"
#define CREATE_CMD
#include "netapp-nvme.h"
enum {
NNORMAL,
NJSON,
NCOLUMN,
};
static const char *dev_path = "/dev/";
struct smdevice_info {
int nsid;
struct nvme_id_ctrl ctrl;
struct nvme_id_ns ns;
char dev[265];
};
#define ARRAY_LABEL_LEN 60
#define VOLUME_LABEL_LEN 60
/*
* Format of the string isn't tightly controlled yet. For now, squash UCS-2 into
* ASCII. dst buffer must be at least count + 1 bytes long
*/
static void netapp_convert_string(char *dst, char *src, unsigned int count)
{
int i;
if (!dst || !src || !count)
return;
memset(dst, 0, count + 1);
for (i = 0; i < count; i++)
dst[i] = src[i * 2 + 1];
}
static void netapp_nguid_to_str(char *str, __u8 *nguid)
{
int i;
memset(str, 0, 33);
for (i = 0; i < 16; i++)
str += sprintf(str, "%02x", nguid[i]);
}
static void netapp_smdevice_json(struct json_array *devices, char *devname,
char *arrayname, char *volname, int nsid, char *nguid,
char *ctrl, char *astate, char *size, long long lba,
long long nsze)
{
struct json_object *device_attrs;
device_attrs = json_create_object();
json_object_add_value_string(device_attrs, "Device", devname);
json_object_add_value_string(device_attrs, "Array_Name", arrayname);
json_object_add_value_string(device_attrs, "Volume_Name", volname);
json_object_add_value_int(device_attrs, "NSID", nsid);
json_object_add_value_string(device_attrs, "Volume_ID", nguid);
json_object_add_value_string(device_attrs, "Controller", ctrl);
json_object_add_value_string(device_attrs, "Access_State", astate);
json_object_add_value_string(device_attrs, "Size", size);
json_object_add_value_int(device_attrs, "LBA_Data_Size", lba);
json_object_add_value_int(device_attrs, "Namespace_Size", nsze);
json_array_add_value_object(devices, device_attrs);
}
static void netapp_smdevices_print(struct smdevice_info *devices, int count, int format)
{
struct json_object *root = NULL;
struct json_array *json_devices = NULL;
int i, slta;
char array_label[ARRAY_LABEL_LEN / 2 + 1];
char volume_label[VOLUME_LABEL_LEN / 2 + 1];
char nguid_str[33];
char basestr[] = "%s, Array Name %s, Volume Name %s, NSID %d, "
"Volume ID %s, Controller %c, Access State %s, %s\n";
char columnstr[] = "%-16s %-30s %-30s %4d %32s %c %-12s %9s\n";
char *formatstr = basestr; // default to "normal" output format
if (format == NCOLUMN) {
/* for column output, change output string and print column headers */
formatstr = columnstr;
printf("%-16s %-30s %-30s %-4s %-32s %-4s %-12s %-9s\n",
"Device", "Array Name", "Volume Name", "NSID",
"Volume ID", "Ctrl", "Access State", " Size");
printf("%-16s %-30s %-30s %-4s %-32s %-4s %-12s %-9s\n",
"----------------", "------------------------------",
"------------------------------", "----",
"--------------------------------", "----",
"------------", "---------");
}
else if (format == NJSON) {
/* prepare for json output */
root = json_create_object();
json_devices = json_create_array();
}
for (i = 0; i < count; i++) {
long long int lba = 1 << devices[i].ns.lbaf[(devices[i].ns.flbas & 0x0F)].ds;
double nsze = le64_to_cpu(devices[i].ns.nsze) * lba;
const char *s_suffix = suffix_si_get(&nsze);
char size[128];
sprintf(size, "%.2f%sB", nsze, s_suffix);
netapp_convert_string(array_label, (char *)&devices[i].ctrl.vs[20],
ARRAY_LABEL_LEN / 2);
slta = devices[i].ctrl.vs[0] & 0x1;
netapp_convert_string(volume_label, (char *)devices[i].ns.vs,
VOLUME_LABEL_LEN / 2);
netapp_nguid_to_str(nguid_str, devices[i].ns.nguid);
if (format == NJSON)
netapp_smdevice_json(json_devices, devices[i].dev,
array_label, volume_label, devices[i].nsid,
nguid_str, slta ? "A" : "B", "unknown", size,
lba, le64_to_cpu(devices[i].ns.nsze));
else
printf(formatstr, devices[i].dev, array_label,
volume_label, devices[i].nsid, nguid_str,
slta ? 'A' : 'B', "unknown", size);
}
if (format == NJSON) {
/* complete the json output */
json_object_add_value_array(root, "SMdevices", json_devices);
json_print_object(root, NULL);
}
}
static int netapp_smdevices_get_info(int fd, struct smdevice_info *item,
const char *dev)
{
int err;
err = nvme_identify_ctrl(fd, &item->ctrl);
if (err) {
fprintf(stderr, "Identify Controler failed to %s (%s)\n", dev,
strerror(err));
return 0;
}
if (strncmp("NetApp E-Series", item->ctrl.mn, 15) != 0)
return 0; // not the right model of controller
item->nsid = nvme_get_nsid(fd);
err = nvme_identify_ns(fd, item->nsid, 0, &item->ns);
if (err) {
fprintf(stderr, "Unable to identify namespace for %s (%s)\n",
dev, strerror(err));
return 0;
}
strncpy(item->dev, dev, sizeof(item->dev));
return 1;
}
static int netapp_nvme_filter(const struct dirent *d)
{
char path[264];
struct stat bd;
int ctrl, ns, partition;
if (d->d_name[0] == '.')
return 0;
if (strstr(d->d_name, "nvme")) {
snprintf(path, sizeof(path), "%s%s", dev_path, d->d_name);
if (stat(path, &bd))
return 0;
if (!S_ISBLK(bd.st_mode))
return 0;
if (sscanf(d->d_name, "nvme%dn%d", &ctrl, &ns) != 2)
return 0;
if (sscanf(d->d_name, "nvme%dn%dp%d", &ctrl, &ns, &partition) == 3)
return 0;
return 1;
}
return 0;
}
static int netapp_output_format(char *format)
{
if (!format)
return -EINVAL;
if (!strcmp(format, "normal"))
return NNORMAL;
if (!strcmp(format, "json"))
return NJSON;
if (!strcmp(format, "column"))
return NCOLUMN;
return -EINVAL;
}
/* handler for 'nvme netapp smdevices' */
static int netapp_smdevices(int argc, char **argv, struct command *command,
struct plugin *plugin)
{
const char *desc = "Display information about E-Series volumes.";
struct config {
char *output_format;
};
struct config cfg = {
.output_format = "normal",
};
struct dirent **devices;
int num, i, fd, ret, fmt;
struct smdevice_info *smdevices;
char path[264];
int num_smdevices = 0;
const struct argconfig_commandline_options opts[] = {
{"output-format", 'o', "FMT", CFG_STRING, &cfg.output_format,
required_argument, "Output Format: normal|json|column"},
{NULL}
};
ret = argconfig_parse(argc, argv, desc, opts, &cfg, sizeof(cfg));
if (ret < 0)
return ret;
fmt = netapp_output_format(cfg.output_format);
if (fmt != NNORMAL && fmt != NCOLUMN && fmt != NJSON) {
fprintf(stderr, "Unrecognized output format: %s\n", cfg.output_format);
return -EINVAL;
}
num = scandir(dev_path, &devices, netapp_nvme_filter, alphasort);
if (num <= 0) {
fprintf(stderr, "No NVMe devices detected.\n");
return num;
}
smdevices = calloc(num, sizeof(*smdevices));
if (!smdevices) {
fprintf(stderr, "Unable to allocate memory for devices.\n");
return ENOMEM;
}
for (i = 0; i < num; i++) {
snprintf(path, sizeof(path), "%s%s", dev_path,
devices[i]->d_name);
fd = open(path, O_RDONLY);
if (fd < 0) {
fprintf(stderr, "Unable to open %s: %s\n", path,
strerror(errno));
continue;
}
num_smdevices += netapp_smdevices_get_info(fd,
&smdevices[num_smdevices], path);
close(fd);
}
if (num_smdevices)
netapp_smdevices_print(smdevices, num_smdevices, fmt);
for (i = 0; i < num; i++)
free(devices[i]);
free(devices);
free(smdevices);
return 0;
}