xref: /illumos-gate/usr/src/cmd/diskinfo/diskinfo.c (revision 609febc9a48c79a089214cb5d882759a72a38513)
1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source.  A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*
13  * Copyright (c) 2018 Joyent Inc., All rights reserved.
14  * Copyright 2021 RackTop Systems, Inc.
15  * Copyright 2021 Tintri by DDN, Inc. All rights reserved.
16  * Copyright 2022 Oxide Computer Company
17  */
18 
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <fcntl.h>
22 #include <errno.h>
23 #include <string.h>
24 #include <stdio.h>
25 #include <unistd.h>
26 #include <limits.h>
27 #include <assert.h>
28 #include <ctype.h>
29 #include <stdarg.h>
30 #include <strings.h>
31 #include <err.h>
32 
33 #include <libdiskmgt.h>
34 #include <sys/nvpair.h>
35 #include <sys/param.h>
36 #include <sys/ccompile.h>
37 
38 #include <fm/libtopo.h>
39 #include <fm/topo_hc.h>
40 #include <fm/topo_list.h>
41 #include <sys/fm/protocol.h>
42 #include <modules/common/disk/disk.h>
43 
44 typedef struct di_opts {
45 	boolean_t di_scripted;
46 	boolean_t di_parseable;
47 	boolean_t di_physical;
48 	boolean_t di_condensed;
49 } di_opts_t;
50 
51 typedef struct di_phys {
52 	const char *dp_dev;
53 	const char *dp_serial;
54 	const char *dp_slotname;
55 	int dp_chassis;
56 	int dp_slot;
57 	int dp_faulty;
58 	int dp_locate;
59 } di_phys_t;
60 
61 static void
62 usage(const char *execname)
63 {
64 	(void) fprintf(stderr, "Usage: %s [-Hp] [{-c|-P}]\n", execname);
65 }
66 
67 static void
68 nvlist_query_string(nvlist_t *nvl, const char *label, char **val)
69 {
70 	if (nvlist_lookup_string(nvl, label, val) != 0)
71 		*val = "-";
72 }
73 
74 static const char *
75 display_string(const char *label)
76 {
77 	return ((label) ? label : "-");
78 }
79 
80 static const char *
81 display_tristate(int val)
82 {
83 	if (val == 0)
84 		return ("no");
85 	if (val == 1)
86 		return ("yes");
87 
88 	return ("-");
89 }
90 
91 static char
92 condensed_tristate(int val, char c)
93 {
94 	if (val == 0)
95 		return ('-');
96 	if (val == 1)
97 		return (c);
98 
99 	return ('?');
100 }
101 static int
102 disk_walker(topo_hdl_t *hp, tnode_t *np, void *arg)
103 {
104 	di_phys_t *pp = arg;
105 	tnode_t *pnp;
106 	tnode_t *ppnp;
107 	topo_faclist_t fl;
108 	topo_faclist_t *lp;
109 	int e;
110 	topo_led_state_t mode;
111 	topo_led_type_t type;
112 	char *name, *slotname, *serial;
113 
114 	if (strcmp(topo_node_name(np), DISK) != 0)
115 		return (TOPO_WALK_NEXT);
116 
117 	if (topo_prop_get_string(np, TOPO_PGROUP_STORAGE,
118 	    TOPO_STORAGE_LOGICAL_DISK_NAME, &name, &e) != 0) {
119 		return (TOPO_WALK_NEXT);
120 	}
121 
122 	if (strcmp(name, pp->dp_dev) != 0)
123 		return (TOPO_WALK_NEXT);
124 
125 	if (topo_prop_get_string(np, TOPO_PGROUP_STORAGE,
126 	    TOPO_STORAGE_SERIAL_NUM, &serial, &e) == 0) {
127 		pp->dp_serial = serial;
128 	}
129 
130 	pnp = topo_node_parent(np);
131 	ppnp = topo_node_parent(pnp);
132 	pp->dp_chassis = topo_node_instance(ppnp);
133 	if (strcmp(topo_node_name(pnp), BAY) == 0) {
134 		if (topo_node_facility(hp, pnp, TOPO_FAC_TYPE_INDICATOR,
135 		    TOPO_FAC_TYPE_ANY, &fl, &e) == 0) {
136 			for (lp = topo_list_next(&fl.tf_list); lp != NULL;
137 			    lp = topo_list_next(lp)) {
138 				uint32_t prop;
139 
140 				if (topo_prop_get_uint32(lp->tf_node,
141 				    TOPO_PGROUP_FACILITY, TOPO_FACILITY_TYPE,
142 				    &prop, &e) != 0) {
143 					continue;
144 				}
145 				type = (topo_led_type_t)prop;
146 
147 				if (topo_prop_get_uint32(lp->tf_node,
148 				    TOPO_PGROUP_FACILITY, TOPO_LED_MODE,
149 				    &prop, &e) != 0) {
150 					continue;
151 				}
152 				mode = (topo_led_state_t)prop;
153 
154 				switch (type) {
155 				case TOPO_LED_TYPE_SERVICE:
156 					pp->dp_faulty = mode ? 1 : 0;
157 					break;
158 				case TOPO_LED_TYPE_LOCATE:
159 					pp->dp_locate = mode ? 1 : 0;
160 					break;
161 				default:
162 					break;
163 				}
164 			}
165 		}
166 
167 		if (topo_prop_get_string(pnp, TOPO_PGROUP_PROTOCOL,
168 		    TOPO_PROP_LABEL, &slotname, &e) == 0) {
169 			pp->dp_slotname = slotname;
170 		}
171 
172 		pp->dp_slot = topo_node_instance(pnp);
173 	} else if (strcmp(topo_node_name(pnp), USB_DEVICE) == 0) {
174 		if (topo_prop_get_string(pnp, TOPO_PGROUP_PROTOCOL,
175 		    TOPO_PROP_LABEL, &slotname, &e) == 0) {
176 			pp->dp_slotname = slotname;
177 		}
178 
179 		/*
180 		 * Override dp_chassis for USB devices since they show up
181 		 * everywhere in the name space and may not be under a logical
182 		 * bay.
183 		 */
184 		pp->dp_chassis = -1;
185 	}
186 
187 	return (TOPO_WALK_TERMINATE);
188 }
189 
190 static void
191 populate_physical(topo_hdl_t *hp, di_phys_t *pp)
192 {
193 	int e;
194 	topo_walk_t *wp;
195 
196 	pp->dp_faulty = pp->dp_locate = -1;
197 	pp->dp_chassis = pp->dp_slot = -1;
198 
199 	e = 0;
200 	wp = topo_walk_init(hp, FM_FMRI_SCHEME_HC, disk_walker, pp, &e);
201 	if (wp == NULL) {
202 		errx(-1, "unable to initialise topo walker: %s",
203 		    topo_strerror(e));
204 	}
205 
206 	while ((e = topo_walk_step(wp, TOPO_WALK_CHILD)) == TOPO_WALK_NEXT)
207 		;
208 
209 	if (e == TOPO_WALK_ERR)
210 		errx(-1, "topo walk failed");
211 
212 	topo_walk_fini(wp);
213 }
214 
215 static void
216 enumerate_disks(di_opts_t *opts)
217 {
218 	topo_hdl_t *hp = NULL;
219 	dm_descriptor_t *media;
220 	int e, i;
221 	int filter[] = { DM_DT_FIXED, -1 };
222 	dm_descriptor_t *disk, *controller;
223 	nvlist_t *mattrs, *dattrs, *cattrs = NULL;
224 
225 	uint64_t size, total;
226 	uint32_t blocksize;
227 	double total_in_GiB;
228 	char sizestr[32];
229 	char slotname[32];
230 	char statestr[8];
231 
232 	char *vid, *pid, *opath, *c, *ctype = NULL;
233 	boolean_t removable;
234 	boolean_t ssd;
235 	char device[MAXPATHLEN];
236 	di_phys_t phys;
237 	size_t len;
238 
239 	e = 0;
240 	if ((media = dm_get_descriptors(DM_MEDIA, filter, &e)) == NULL) {
241 		errno = e;
242 		err(-1, "failed to obtain media descriptors");
243 	}
244 
245 	/*
246 	 * We only need to walk topo if we're intending to display
247 	 * condensed or physical information. If we don't need it, we leave
248 	 * hp = NULL.
249 	 */
250 	if (opts->di_condensed || opts->di_physical) {
251 		e = 0;
252 		hp = topo_open(TOPO_VERSION, NULL, &e);
253 		if (hp == NULL) {
254 			errx(-1, "unable to obtain topo handle: %s",
255 			    topo_strerror(e));
256 		}
257 
258 		e = 0;
259 		(void) topo_snap_hold(hp, NULL, &e);
260 		if (e != 0) {
261 			errx(-1, "unable to hold topo snapshot: %s",
262 			    topo_strerror(e));
263 		}
264 	}
265 
266 	for (i = 0; media != NULL && media[i] != 0; i++) {
267 		if ((disk = dm_get_associated_descriptors(media[i],
268 		    DM_DRIVE, &e)) == NULL) {
269 			continue;
270 		}
271 
272 		/*
273 		 * The attributes depend on us being able to get the media
274 		 * info with DKIOCGMEDIAINFO which may not be the case for
275 		 * disks which are failing.
276 		 */
277 		if ((mattrs = dm_get_attributes(media[i], &e)) == NULL) {
278 			continue;
279 		}
280 
281 		e = nvlist_lookup_uint64(mattrs, DM_SIZE, &size);
282 		assert(e == 0);
283 		e = nvlist_lookup_uint32(mattrs, DM_BLOCKSIZE, &blocksize);
284 		assert(e == 0);
285 		nvlist_free(mattrs);
286 
287 		dattrs = dm_get_attributes(disk[0], &e);
288 
289 		nvlist_query_string(dattrs, DM_VENDOR_ID, &vid);
290 		nvlist_query_string(dattrs, DM_PRODUCT_ID, &pid);
291 		nvlist_query_string(dattrs, DM_OPATH, &opath);
292 
293 		removable = B_FALSE;
294 		if (nvlist_lookup_boolean(dattrs, DM_REMOVABLE) == 0)
295 			removable = B_TRUE;
296 
297 		ssd = B_FALSE;
298 		if (nvlist_lookup_boolean(dattrs, DM_SOLIDSTATE) == 0)
299 			ssd = B_TRUE;
300 
301 		if ((controller = dm_get_associated_descriptors(disk[0],
302 		    DM_CONTROLLER, &e)) != NULL) {
303 			cattrs = dm_get_attributes(controller[0], &e);
304 			nvlist_query_string(cattrs, DM_CTYPE, &ctype);
305 			ctype = strdup(ctype);
306 			for (c = ctype; *c != '\0'; c++)
307 				*c = toupper(*c);
308 		}
309 
310 		/*
311 		 * Parse full device path to only show the device name,
312 		 * i.e. c0t1d0.  Many paths will reference a particular
313 		 * slice (c0t1d0s0), so remove the slice if present.
314 		 */
315 		if ((c = strrchr(opath, '/')) != NULL)
316 			(void) strlcpy(device, c + 1, sizeof (device));
317 		else
318 			(void) strlcpy(device, opath, sizeof (device));
319 		len = strlen(device);
320 		if (device[len - 2] == 's' &&
321 		    (device[len - 1] >= '0' && device[len - 1] <= '9'))
322 			device[len - 2] = '\0';
323 
324 		if (hp != NULL) {
325 			bzero(&phys, sizeof (phys));
326 			phys.dp_dev = device;
327 			populate_physical(hp, &phys);
328 
329 			if (opts->di_parseable) {
330 				(void) snprintf(slotname, sizeof (slotname),
331 				    "%d,%d", phys.dp_chassis, phys.dp_slot);
332 			} else if (phys.dp_slotname != NULL &&
333 			    phys.dp_chassis != -1) {
334 				(void) snprintf(slotname, sizeof (slotname),
335 				    "[%d] %s", phys.dp_chassis,
336 				    phys.dp_slotname);
337 			} else if (phys.dp_slotname != NULL) {
338 				(void) snprintf(slotname, sizeof (slotname),
339 				    "%s", phys.dp_slotname);
340 			} else {
341 				slotname[0] = '-';
342 				slotname[1] = '\0';
343 			}
344 		}
345 
346 		/*
347 		 * The size is given in blocks, so multiply the number
348 		 * of blocks by the block size to get the total size,
349 		 * then convert to GiB.
350 		 */
351 		total = size * blocksize;
352 
353 		if (opts->di_parseable) {
354 			(void) snprintf(sizestr, sizeof (sizestr),
355 			    "%llu", total);
356 		} else {
357 			total_in_GiB = (double)total /
358 			    1024.0 / 1024.0 / 1024.0;
359 			(void) snprintf(sizestr, sizeof (sizestr),
360 			    "%7.2f GiB", total_in_GiB);
361 		}
362 
363 		if (opts->di_condensed) {
364 			(void) snprintf(statestr, sizeof (statestr), "%c%c%c%c",
365 			    condensed_tristate(phys.dp_faulty, 'F'),
366 			    condensed_tristate(phys.dp_locate, 'L'),
367 			    condensed_tristate(removable, 'R'),
368 			    condensed_tristate(ssd, 'S'));
369 		}
370 
371 		if (opts->di_physical) {
372 			if (opts->di_scripted) {
373 				printf("%s\t%s\t%s\t%s\t%s\t%s\t%s\n",
374 				    device, vid, pid,
375 				    display_string(phys.dp_serial),
376 				    display_tristate(phys.dp_faulty),
377 				    display_tristate(phys.dp_locate), slotname);
378 			} else {
379 				printf("%-22s  %-8s %-16s "
380 				    "%-20s %-3s %-3s %s\n",
381 				    device, vid, pid,
382 				    display_string(phys.dp_serial),
383 				    display_tristate(phys.dp_faulty),
384 				    display_tristate(phys.dp_locate), slotname);
385 			}
386 		} else if (opts->di_condensed) {
387 			if (opts->di_scripted) {
388 				printf("%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n",
389 				    ctype, device, vid, pid,
390 				    display_string(phys.dp_serial),
391 				    sizestr, statestr, slotname);
392 			} else {
393 				printf("%-7s %-22s  %-8s %-16s "
394 				    "%-20s\n\t%-13s %-4s %s\n",
395 				    ctype, device, vid, pid,
396 				    display_string(phys.dp_serial),
397 				    sizestr, statestr, slotname);
398 			}
399 		} else {
400 			if (opts->di_scripted) {
401 				printf("%s\t%s\t%s\t%s\t%s\t%s\t%s\n",
402 				    ctype, device, vid, pid, sizestr,
403 				    display_tristate(removable),
404 				    display_tristate(ssd));
405 			} else {
406 				printf("%-7s %-22s  %-8s %-16s "
407 				    "%-13s %-3s %-3s\n", ctype, device,
408 				    vid, pid, sizestr,
409 				    display_tristate(removable),
410 				    display_tristate(ssd));
411 			}
412 		}
413 
414 		free(ctype);
415 		nvlist_free(cattrs);
416 		nvlist_free(dattrs);
417 		dm_free_descriptors(controller);
418 		dm_free_descriptors(disk);
419 	}
420 
421 	dm_free_descriptors(media);
422 	if (hp != NULL) {
423 		topo_snap_release(hp);
424 		topo_close(hp);
425 	}
426 }
427 
428 int
429 main(int argc, char *argv[])
430 {
431 	char c;
432 
433 	di_opts_t opts = {
434 		.di_condensed = B_FALSE,
435 		.di_scripted = B_FALSE,
436 		.di_physical = B_FALSE,
437 		.di_parseable = B_FALSE
438 	};
439 
440 	while ((c = getopt(argc, argv, ":cHPp")) != EOF) {
441 		switch (c) {
442 		case 'c':
443 			if (opts.di_physical) {
444 				usage(argv[0]);
445 				errx(1, "-c and -P are mutually exclusive");
446 			}
447 			opts.di_condensed = B_TRUE;
448 			break;
449 		case 'H':
450 			opts.di_scripted = B_TRUE;
451 			break;
452 		case 'P':
453 			if (opts.di_condensed) {
454 				usage(argv[0]);
455 				errx(1, "-c and -P are mutually exclusive");
456 			}
457 			opts.di_physical = B_TRUE;
458 			break;
459 		case 'p':
460 			opts.di_parseable = B_TRUE;
461 			break;
462 		case '?':
463 			usage(argv[0]);
464 			errx(1, "unknown option -%c", optopt);
465 		default:
466 			errx(-1, "unexpected error on option -%c", optopt);
467 		}
468 	}
469 
470 	if (!opts.di_scripted) {
471 		if (opts.di_physical) {
472 			printf("DISK                    VID      PID"
473 			    "              SERIAL               FLT LOC"
474 			    " LOCATION\n");
475 		} else if (opts.di_condensed) {
476 			printf("TYPE    DISK                    VID      PID"
477 			    "              SERIAL\n");
478 			printf("\tSIZE          FLRS LOCATION\n");
479 		} else {
480 			printf("TYPE    DISK                    VID      PID"
481 			    "              SIZE          RMV SSD\n");
482 		}
483 	}
484 
485 	enumerate_disks(&opts);
486 
487 	return (0);
488 }
489