Skip to content

Commit

Permalink
15004 diskinfo: get rid of topo calls if there are no -c or -P options
Browse files Browse the repository at this point in the history
Reviewed by: Marco van Wieringen <mvw@planets.elm.net>
Reviewed by: Toomas Soome <tsoome@me.com>
Approved by: Gordon Ross <gordon.w.ross@gmail.com>
  • Loading branch information
alhazred authored and citrus-it committed Sep 24, 2022
1 parent 088ae41 commit 2b766db
Showing 1 changed file with 45 additions and 29 deletions.
74 changes: 45 additions & 29 deletions usr/src/cmd/diskinfo/diskinfo.c
Expand Up @@ -12,6 +12,7 @@
/*
* Copyright (c) 2018 Joyent Inc., All rights reserved.
* Copyright 2021 RackTop Systems, Inc.
* Copyright 2021 Tintri by DDN, Inc. All rights reserved.
* Copyright 2022 Oxide Computer Company
*/

Expand Down Expand Up @@ -214,7 +215,7 @@ populate_physical(topo_hdl_t *hp, di_phys_t *pp)
static void
enumerate_disks(di_opts_t *opts)
{
topo_hdl_t *hp;
topo_hdl_t *hp = NULL;
dm_descriptor_t *media;
int e, i;
int filter[] = { DM_DT_FIXED, -1 };
Expand All @@ -241,16 +242,25 @@ enumerate_disks(di_opts_t *opts)
err(-1, "failed to obtain media descriptors");
}

e = 0;
hp = topo_open(TOPO_VERSION, NULL, &e);
if (hp == NULL) {
errx(-1, "unable to obtain topo handle: %s", topo_strerror(e));
}
/*
* We only need to walk topo if we're intending to display
* condensed or physical information. If we don't need it, we leave
* hp = NULL.
*/
if (opts->di_condensed || opts->di_physical) {
e = 0;
hp = topo_open(TOPO_VERSION, NULL, &e);
if (hp == NULL) {
errx(-1, "unable to obtain topo handle: %s",
topo_strerror(e));
}

e = 0;
(void) topo_snap_hold(hp, NULL, &e);
if (e != 0) {
errx(-1, "unable to hold topo snapshot: %s", topo_strerror(e));
e = 0;
(void) topo_snap_hold(hp, NULL, &e);
if (e != 0) {
errx(-1, "unable to hold topo snapshot: %s",
topo_strerror(e));
}
}

for (i = 0; media != NULL && media[i] != 0; i++) {
Expand Down Expand Up @@ -311,9 +321,27 @@ enumerate_disks(di_opts_t *opts)
(device[len - 1] >= '0' && device[len - 1] <= '9'))
device[len - 2] = '\0';

bzero(&phys, sizeof (phys));
phys.dp_dev = device;
populate_physical(hp, &phys);
if (hp != NULL) {
bzero(&phys, sizeof (phys));
phys.dp_dev = device;
populate_physical(hp, &phys);

if (opts->di_parseable) {
(void) snprintf(slotname, sizeof (slotname),
"%d,%d", phys.dp_chassis, phys.dp_slot);
} else if (phys.dp_slotname != NULL &&
phys.dp_chassis != -1) {
(void) snprintf(slotname, sizeof (slotname),
"[%d] %s", phys.dp_chassis,
phys.dp_slotname);
} else if (phys.dp_slotname != NULL) {
(void) snprintf(slotname, sizeof (slotname),
"%s", phys.dp_slotname);
} else {
slotname[0] = '-';
slotname[1] = '\0';
}
}

/*
* The size is given in blocks, so multiply the number
Expand All @@ -332,20 +360,6 @@ enumerate_disks(di_opts_t *opts)
"%7.2f GiB", total_in_GiB);
}

if (opts->di_parseable) {
(void) snprintf(slotname, sizeof (slotname), "%d,%d",
phys.dp_chassis, phys.dp_slot);
} else if (phys.dp_slotname != NULL && phys.dp_chassis != -1) {
(void) snprintf(slotname, sizeof (slotname),
"[%d] %s", phys.dp_chassis, phys.dp_slotname);
} else if (phys.dp_slotname != NULL) {
(void) snprintf(slotname, sizeof (slotname),
"%s", phys.dp_slotname);
} else {
slotname[0] = '-';
slotname[1] = '\0';
}

if (opts->di_condensed) {
(void) snprintf(statestr, sizeof (statestr), "%c%c%c%c",
condensed_tristate(phys.dp_faulty, 'F'),
Expand Down Expand Up @@ -405,8 +419,10 @@ enumerate_disks(di_opts_t *opts)
}

dm_free_descriptors(media);
topo_snap_release(hp);
topo_close(hp);
if (hp != NULL) {
topo_snap_release(hp);
topo_close(hp);
}
}

int
Expand Down

0 comments on commit 2b766db

Please sign in to comment.