summaryrefslogtreecommitdiffstats
path: root/usr.sbin/mptutil/mpt_config.c
diff options
context:
space:
mode:
authorjhb <jhb@FreeBSD.org>2010-11-09 19:28:06 +0000
committerjhb <jhb@FreeBSD.org>2010-11-09 19:28:06 +0000
commit8b07784b8bd29bc95ce1ab6451d72bf76538701a (patch)
treec60959fbcab6d9ff9a53430479a507a3697a058c /usr.sbin/mptutil/mpt_config.c
parentc98c4f39f2a4229b2a08876e2fa97e572b886f34 (diff)
downloadFreeBSD-src-8b07784b8bd29bc95ce1ab6451d72bf76538701a.zip
FreeBSD-src-8b07784b8bd29bc95ce1ab6451d72bf76538701a.tar.gz
- Save errno values before calling warn(3) so that errors are correctly
reported. - Check for malloc() failures. Submitted by: gcooper MFC after: 1 week
Diffstat (limited to 'usr.sbin/mptutil/mpt_config.c')
-rw-r--r--usr.sbin/mptutil/mpt_config.c215
1 files changed, 128 insertions, 87 deletions
diff --git a/usr.sbin/mptutil/mpt_config.c b/usr.sbin/mptutil/mpt_config.c
index 3874df3..6247bb4 100644
--- a/usr.sbin/mptutil/mpt_config.c
+++ b/usr.sbin/mptutil/mpt_config.c
@@ -102,15 +102,15 @@ mpt_lock_volume(U8 VolumeBus, U8 VolumeID)
*/
return (0);
if (error) {
- errno = error;
- warn("Unable to lookup volume device name");
- return (-1);
+ warnc(error, "Unable to lookup volume device name");
+ return (error);
}
snprintf(path, sizeof(path), "%s%s", _PATH_DEV, qd.devname);
vfd = open(path, O_RDWR);
if (vfd < 0) {
+ error = errno;
warn("Unable to lock volume %s", qd.devname);
- return (-1);
+ return (error);
}
return (0);
}
@@ -119,13 +119,14 @@ static int
mpt_lock_physdisk(struct mpt_standalone_disk *disk)
{
char path[MAXPATHLEN];
- int dfd;
+ int dfd, error;
snprintf(path, sizeof(path), "%s%s", _PATH_DEV, disk->devname);
dfd = open(path, O_RDWR);
if (dfd < 0) {
+ error = errno;
warn("Unable to lock disk %s", disk->devname);
- return (-1);
+ return (error);
}
return (0);
}
@@ -144,8 +145,7 @@ mpt_lookup_standalone_disk(const char *name, struct mpt_standalone_disk *disks,
id = strtol(cp + 1, &cp, 0);
if (*cp == '\0') {
if (bus < 0 || bus > 0xff || id < 0 || id > 0xff) {
- errno = EINVAL;
- return (-1);
+ return (EINVAL);
}
for (i = 0; i < ndisks; i++) {
if (disks[i].bus == (U8)bus &&
@@ -154,8 +154,7 @@ mpt_lookup_standalone_disk(const char *name, struct mpt_standalone_disk *disks,
return (0);
}
}
- errno = ENOENT;
- return (-1);
+ return (ENOENT);
}
}
@@ -166,12 +165,10 @@ mpt_lookup_standalone_disk(const char *name, struct mpt_standalone_disk *disks,
return (0);
}
}
- errno = ENOENT;
- return (-1);
+ return (ENOENT);
}
- errno = EINVAL;
- return (-1);
+ return (EINVAL);
}
/*
@@ -182,16 +179,17 @@ mpt_create_physdisk(int fd, struct mpt_standalone_disk *disk, U8 *PhysDiskNum)
{
CONFIG_PAGE_HEADER header;
CONFIG_PAGE_RAID_PHYS_DISK_0 *config_page;
+ int error;
U32 ActionData;
- if (mpt_read_config_page_header(fd, MPI_CONFIG_PAGETYPE_RAID_PHYSDISK,
- 0, 0, &header, NULL) < 0)
- return (-1);
+ error = mpt_read_config_page_header(fd, MPI_CONFIG_PAGETYPE_RAID_PHYSDISK,
+ 0, 0, &header, NULL);
+ if (error)
+ return (error);
if (header.PageVersion > MPI_RAIDPHYSDISKPAGE0_PAGEVERSION) {
warnx("Unsupported RAID physdisk page 0 version %d",
header.PageVersion);
- errno = EOPNOTSUPP;
- return (-1);
+ return (EOPNOTSUPP);
}
config_page = calloc(1, sizeof(CONFIG_PAGE_RAID_PHYS_DISK_0));
config_page->Header.PageType = MPI_CONFIG_PAGETYPE_RAID_PHYSDISK;
@@ -203,10 +201,11 @@ mpt_create_physdisk(int fd, struct mpt_standalone_disk *disk, U8 *PhysDiskNum)
config_page->PhysDiskID = disk->target;
/* XXX: Enclosure info for PhysDiskSettings? */
- if (mpt_raid_action(fd, MPI_RAID_ACTION_CREATE_PHYSDISK, 0, 0, 0, 0,
+ error = mpt_raid_action(fd, MPI_RAID_ACTION_CREATE_PHYSDISK, 0, 0, 0, 0,
config_page, sizeof(CONFIG_PAGE_RAID_PHYS_DISK_0), NULL,
- &ActionData, sizeof(ActionData), NULL, NULL, 1) < 0)
- return (-1);
+ &ActionData, sizeof(ActionData), NULL, NULL, 1);
+ if (error)
+ return (error);
*PhysDiskNum = ActionData & 0xff;
return (0);
}
@@ -232,18 +231,20 @@ clear_config(int ac, char **av)
IOC_3_PHYS_DISK *disk;
CONFIG_PAGE_IOC_5 *ioc5;
IOC_5_HOT_SPARE *spare;
- int ch, fd, i;
+ int ch, error, fd, i;
fd = mpt_open(mpt_unit);
if (fd < 0) {
+ error = errno;
warn("mpt_open");
- return (errno);
+ return (error);
}
ioc2 = mpt_read_ioc_page(fd, 2, NULL);
if (ioc2 == NULL) {
+ error = errno;
warn("Failed to fetch volume list");
- return (errno);
+ return (error);
}
/* Lock all the volumes first. */
@@ -267,14 +268,16 @@ clear_config(int ac, char **av)
/* Delete all the volumes. */
vol = ioc2->RaidVolume;
- for (i = 0; i < ioc2->NumActiveVolumes; vol++, i++)
- if (mpt_raid_action(fd, MPI_RAID_ACTION_DELETE_VOLUME,
+ for (i = 0; i < ioc2->NumActiveVolumes; vol++, i++) {
+ error = mpt_raid_action(fd, MPI_RAID_ACTION_DELETE_VOLUME,
vol->VolumeBus, vol->VolumeID, 0,
MPI_RAID_ACTION_ADATA_DEL_PHYS_DISKS |
MPI_RAID_ACTION_ADATA_ZERO_LBA0, NULL, 0, NULL, NULL, 0,
- NULL, NULL, 0) < 0)
- warn("Failed to delete volume %s",
+ NULL, NULL, 0);
+ if (error)
+ warnc(error, "Failed to delete volume %s",
mpt_volume_name(vol->VolumeBus, vol->VolumeID));
+ }
free(ioc2);
/* Delete all the spares. */
@@ -411,8 +414,9 @@ parse_volume(int fd, int raid_type, struct config_id_state *state,
/* See if it is a standalone disk. */
if (mpt_lookup_standalone_disk(cp, state->sdisks,
state->nsdisks, &i) < 0) {
+ error = errno;
warn("Unable to lookup drive %s", cp);
- return (errno);
+ return (error);
}
dinfo->sdisk = &state->sdisks[i];
@@ -433,17 +437,18 @@ add_drives(int fd, struct volume_info *info, int verbose)
{
struct drive_info *dinfo;
U8 PhysDiskNum;
- int i;
+ int error, i;
for (i = 0, dinfo = info->drives; i < info->drive_count;
i++, dinfo++) {
if (dinfo->info == NULL) {
if (mpt_create_physdisk(fd, dinfo->sdisk,
&PhysDiskNum) < 0) {
+ error = errno;
warn(
"Failed to create physical disk page for %s",
dinfo->sdisk->devname);
- return (errno);
+ return (error);
}
if (verbose)
printf("Added drive %s with PhysDiskNum %u\n",
@@ -500,11 +505,14 @@ build_volume(int fd, struct volume_info *info, int raid_type, long stripe_size,
U32 MinLBA;
uint64_t MaxLBA;
size_t page_size;
- int i;
+ int error, i;
- if (mpt_read_config_page_header(fd, MPI_CONFIG_PAGETYPE_RAID_VOLUME,
- 0, 0, &header, NULL) < 0)
+ error = mpt_read_config_page_header(fd, MPI_CONFIG_PAGETYPE_RAID_VOLUME,
+ 0, 0, &header, NULL);
+ if (error) {
+ errno = error;
return (NULL);
+ }
if (header.PageVersion > MPI_RAIDVOLPAGE0_PAGEVERSION) {
warnx("Unsupported RAID volume page 0 version %d",
header.PageVersion);
@@ -514,6 +522,8 @@ build_volume(int fd, struct volume_info *info, int raid_type, long stripe_size,
page_size = sizeof(CONFIG_PAGE_RAID_VOL_0) +
sizeof(RAID_VOL0_PHYS_DISK) * (info->drive_count - 1);
vol = calloc(1, page_size);
+ if (vol == NULL)
+ return (NULL);
/* Header */
vol->Header.PageType = MPI_CONFIG_PAGETYPE_RAID_VOLUME;
@@ -607,8 +617,8 @@ create_volume(int ac, char **av)
CONFIG_PAGE_RAID_VOL_0 *vol;
struct config_id_state state;
struct volume_info *info;
- int ch, error, fd, i, raid_type, verbose, quick;
long stripe_size;
+ int ch, error, fd, i, quick, raid_type, verbose;
#ifdef DEBUG
int dump;
#endif
@@ -620,8 +630,9 @@ create_volume(int ac, char **av)
fd = mpt_open(mpt_unit);
if (fd < 0) {
+ error = errno;
warn("mpt_open");
- return (errno);
+ return (error);
}
/* Lookup the RAID type first. */
@@ -677,8 +688,9 @@ create_volume(int ac, char **av)
/* Fetch existing config data. */
state.ioc2 = mpt_read_ioc_page(fd, 2, NULL);
if (state.ioc2 == NULL) {
+ error = errno;
warn("Failed to read volume list");
- return (errno);
+ return (error);
}
state.list = mpt_pd_list(fd);
if (state.list == NULL)
@@ -696,6 +708,8 @@ create_volume(int ac, char **av)
return (EINVAL);
}
info = calloc(1, sizeof(*info));
+ if (info == NULL)
+ return (ENOMEM);
error = parse_volume(fd, raid_type, &state, av[0], info);
if (error)
return (error);
@@ -707,6 +721,8 @@ create_volume(int ac, char **av)
/* Build the volume. */
vol = build_volume(fd, info, raid_type, stripe_size, &state, verbose);
+ if (vol == NULL)
+ return (errno);
#ifdef DEBUG
if (dump) {
@@ -716,12 +732,13 @@ create_volume(int ac, char **av)
#endif
/* Send the new volume to the controller. */
- if (mpt_raid_action(fd, MPI_RAID_ACTION_CREATE_VOLUME, vol->VolumeBus,
+ error = mpt_raid_action(fd, MPI_RAID_ACTION_CREATE_VOLUME, vol->VolumeBus,
vol->VolumeID, 0, quick ? MPI_RAID_ACTION_ADATA_DO_NOT_SYNC : 0,
- vol, vol->Header.PageLength * 4, NULL, NULL, 0, NULL, NULL, 1) <
- 0) {
+ vol, vol->Header.PageLength * 4, NULL, NULL, 0, NULL, NULL, 1);
+ if (error) {
+ errno = error;
warn("Failed to add volume");
- return (errno);
+ return (error);
}
#ifdef DEBUG
@@ -745,7 +762,7 @@ static int
delete_volume(int ac, char **av)
{
U8 VolumeBus, VolumeID;
- int fd;
+ int error, fd;
if (ac != 2) {
warnx("delete: volume required");
@@ -754,24 +771,27 @@ delete_volume(int ac, char **av)
fd = mpt_open(mpt_unit);
if (fd < 0) {
+ error = errno;
warn("mpt_open");
- return (errno);
+ return (error);
}
- if (mpt_lookup_volume(fd, av[1], &VolumeBus, &VolumeID) < 0) {
- warn("Invalid volume %s", av[1]);
- return (errno);
+ error = mpt_lookup_volume(fd, av[1], &VolumeBus, &VolumeID);
+ if (error) {
+ warnc(error, "Invalid volume %s", av[1]);
+ return (error);
}
if (mpt_lock_volume(VolumeBus, VolumeID) < 0)
return (errno);
- if (mpt_raid_action(fd, MPI_RAID_ACTION_DELETE_VOLUME, VolumeBus,
+ error = mpt_raid_action(fd, MPI_RAID_ACTION_DELETE_VOLUME, VolumeBus,
VolumeID, 0, MPI_RAID_ACTION_ADATA_DEL_PHYS_DISKS |
MPI_RAID_ACTION_ADATA_ZERO_LBA0, NULL, 0, NULL, NULL, 0, NULL,
- NULL, 0) < 0) {
- warn("Failed to delete volume");
- return (errno);
+ NULL, 0);
+ if (error) {
+ warnc(error, "Failed to delete volume");
+ return (error);
}
mpt_rescan_bus(-1, -1);
@@ -788,16 +808,17 @@ find_volume_spare_pool(int fd, const char *name, int *pool)
CONFIG_PAGE_IOC_2 *ioc2;
CONFIG_PAGE_IOC_2_RAID_VOL *vol;
U8 VolumeBus, VolumeID;
- int i, j, new_pool, pool_count[7];
+ int error, i, j, new_pool, pool_count[7];
- if (mpt_lookup_volume(fd, name, &VolumeBus, &VolumeID) < 0) {
- warn("Invalid volume %s", name);
- return (-1);
+ error = mpt_lookup_volume(fd, name, &VolumeBus, &VolumeID);
+ if (error) {
+ warnc(error, "Invalid volume %s", name);
+ return (error);
}
info = mpt_vol_info(fd, VolumeBus, VolumeID, NULL);
if (info == NULL)
- return (-1);
+ return (errno);
/*
* Check for an existing pool other than pool 0 (used for
@@ -817,15 +838,16 @@ find_volume_spare_pool(int fd, const char *name, int *pool)
*/
ioc2 = mpt_read_ioc_page(fd, 2, NULL);
if (ioc2 == NULL) {
+ error = errno;
warn("Failed to fetch volume list");
- return (-1);
+ return (error);
}
bzero(pool_count, sizeof(pool_count));
vol = ioc2->RaidVolume;
for (i = 0; i < ioc2->NumActiveVolumes; vol++, i++) {
info = mpt_vol_info(fd, vol->VolumeBus, vol->VolumeID, NULL);
if (info == NULL)
- return (-1);
+ return (errno);
for (j = 0; j < 7; j++)
if (info->VolumeSettings.HotSparePool & (1 << (j + 1)))
pool_count[j]++;
@@ -843,14 +865,15 @@ find_volume_spare_pool(int fd, const char *name, int *pool)
/* Add this pool to the volume. */
info = mpt_vol_info(fd, VolumeBus, VolumeID, NULL);
if (info == NULL)
- return (-1);
+ return (error);
info->VolumeSettings.HotSparePool |= (1 << new_pool);
- if (mpt_raid_action(fd, MPI_RAID_ACTION_CHANGE_VOLUME_SETTINGS,
+ error = mpt_raid_action(fd, MPI_RAID_ACTION_CHANGE_VOLUME_SETTINGS,
VolumeBus, VolumeID, 0, *(U32 *)&info->VolumeSettings, NULL, 0,
- NULL, NULL, 0, NULL, NULL, 0) < 0) {
+ NULL, NULL, 0, NULL, NULL, 0);
+ if (error) {
warnx("Failed to add spare pool %d to %s", new_pool,
mpt_volume_name(VolumeBus, VolumeID));
- return (-1);
+ return (error);
}
free(info);
@@ -878,13 +901,15 @@ add_spare(int ac, char **av)
fd = mpt_open(mpt_unit);
if (fd < 0) {
+ error = errno;
warn("mpt_open");
- return (errno);
+ return (error);
}
if (ac == 3) {
- if (find_volume_spare_pool(fd, av[2], &pool) < 0)
- return (errno);
+ error = find_volume_spare_pool(fd, av[2], &pool);
+ if (error)
+ return (error);
} else
pool = MPI_RAID_HOT_SPARE_POOL_0;
@@ -902,16 +927,18 @@ add_spare(int ac, char **av)
if (mpt_lookup_standalone_disk(av[1], sdisks, nsdisks, &i) <
0) {
+ error = errno;
warn("Unable to lookup drive %s", av[1]);
- return (errno);
+ return (error);
}
if (mpt_lock_physdisk(&sdisks[i]) < 0)
return (errno);
if (mpt_create_physdisk(fd, &sdisks[i], &PhysDiskNum) < 0) {
+ error = errno;
warn("Failed to create physical disk page");
- return (errno);
+ return (error);
}
free(sdisks);
}
@@ -919,8 +946,9 @@ add_spare(int ac, char **av)
info = mpt_pd_info(fd, PhysDiskNum, NULL);
if (info == NULL) {
+ error = errno;
warn("Failed to fetch drive info");
- return (errno);
+ return (error);
}
info->PhysDiskSettings.HotSparePool = pool;
@@ -928,8 +956,8 @@ add_spare(int ac, char **av)
0, PhysDiskNum, *(U32 *)&info->PhysDiskSettings, NULL, 0, NULL,
NULL, 0, NULL, NULL, 0);
if (error) {
- warn("Failed to assign spare");
- return (errno);
+ warnc(error, "Failed to assign spare");
+ return (error);
}
free(info);
@@ -954,8 +982,9 @@ remove_spare(int ac, char **av)
fd = mpt_open(mpt_unit);
if (fd < 0) {
+ error = errno;
warn("mpt_open");
- return (errno);
+ return (error);
}
list = mpt_pd_list(fd);
@@ -972,8 +1001,9 @@ remove_spare(int ac, char **av)
info = mpt_pd_info(fd, PhysDiskNum, NULL);
if (info == NULL) {
+ error = errno;
warn("Failed to fetch drive info");
- return (errno);
+ return (error);
}
if (info->PhysDiskSettings.HotSparePool == 0) {
@@ -982,8 +1012,9 @@ remove_spare(int ac, char **av)
}
if (mpt_delete_physdisk(fd, PhysDiskNum) < 0) {
+ error = errno;
warn("Failed to delete physical disk page");
- return (errno);
+ return (error);
}
mpt_rescan_bus(info->PhysDiskBus, info->PhysDiskID);
@@ -1011,8 +1042,9 @@ pd_create(int ac, char **av)
fd = mpt_open(mpt_unit);
if (fd < 0) {
+ error = errno;
warn("mpt_open");
- return (errno);
+ return (error);
}
error = mpt_fetch_disks(fd, &ndisks, &disks);
@@ -1022,16 +1054,18 @@ pd_create(int ac, char **av)
}
if (mpt_lookup_standalone_disk(av[1], disks, ndisks, &i) < 0) {
+ error = errno;
warn("Unable to lookup drive");
- return (errno);
+ return (error);
}
if (mpt_lock_physdisk(&disks[i]) < 0)
return (errno);
if (mpt_create_physdisk(fd, &disks[i], &PhysDiskNum) < 0) {
+ error = errno;
warn("Failed to create physical disk page");
- return (errno);
+ return (error);
}
free(disks);
@@ -1048,7 +1082,7 @@ pd_delete(int ac, char **av)
{
CONFIG_PAGE_RAID_PHYS_DISK_0 *info;
struct mpt_drive_list *list;
- int fd;
+ int error, fd;
U8 PhysDiskNum;
if (ac != 2) {
@@ -1058,8 +1092,9 @@ pd_delete(int ac, char **av)
fd = mpt_open(mpt_unit);
if (fd < 0) {
+ error = errno;
warn("mpt_open");
- return (errno);
+ return (error);
}
list = mpt_pd_list(fd);
@@ -1067,20 +1102,23 @@ pd_delete(int ac, char **av)
return (errno);
if (mpt_lookup_drive(list, av[1], &PhysDiskNum) < 0) {
+ error = errno;
warn("Failed to find drive %s", av[1]);
- return (errno);
+ return (error);
}
mpt_free_pd_list(list);
info = mpt_pd_info(fd, PhysDiskNum, NULL);
if (info == NULL) {
+ error = errno;
warn("Failed to fetch drive info");
- return (errno);
+ return (error);
}
if (mpt_delete_physdisk(fd, PhysDiskNum) < 0) {
+ error = errno;
warn("Failed to delete physical disk page");
- return (errno);
+ return (error);
}
mpt_rescan_bus(info->PhysDiskBus, info->PhysDiskID);
@@ -1126,7 +1164,7 @@ debug_config(int ac, char **av)
{
CONFIG_PAGE_RAID_VOL_0 *vol;
U8 VolumeBus, VolumeID;
- int fd;
+ int error, fd;
if (ac != 2) {
warnx("debug: volume required");
@@ -1135,19 +1173,22 @@ debug_config(int ac, char **av)
fd = mpt_open(mpt_unit);
if (fd < 0) {
+ error = errno;
warn("mpt_open");
- return (errno);
+ return (error);
}
- if (mpt_lookup_volume(fd, av[1], &VolumeBus, &VolumeID) < 0) {
- warn("Invalid volume: %s", av[1]);
- return (errno);
+ error = mpt_lookup_volume(fd, av[1], &VolumeBus, &VolumeID);
+ if (error) {
+ warnc(error, "Invalid volume: %s", av[1]);
+ return (error);
}
vol = mpt_vol_info(fd, VolumeBus, VolumeID, NULL);
if (vol == NULL) {
+ error = errno;
warn("Failed to get volume info");
- return (errno);
+ return (error);
}
dump_config(vol);
OpenPOWER on IntegriCloud