Index: head/usr.sbin/mptutil/mpt_config.c =================================================================== --- head/usr.sbin/mptutil/mpt_config.c (revision 329844) +++ head/usr.sbin/mptutil/mpt_config.c (revision 329845) @@ -1,1201 +1,1262 @@ /*- * SPDX-License-Identifier: BSD-3-Clause * * Copyright (c) 2008 Yahoo!, Inc. * All rights reserved. * Written by: John Baldwin * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the author nor the names of any co-contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include __RCSID("$FreeBSD$"); #include #include #include #include #include #include #ifdef DEBUG #include #endif #include #include #include #include #include "mptutil.h" #ifdef DEBUG static void dump_config(CONFIG_PAGE_RAID_VOL_0 *vol); #endif static long dehumanize(const char *value) { char *vtp; long iv; if (value == NULL) return (0); iv = strtoq(value, &vtp, 0); if (vtp == value || (vtp[0] != '\0' && vtp[1] != '\0')) { return (0); } switch (vtp[0]) { case 't': case 'T': iv *= 1024; + /* FALLTHROUGH */ case 'g': case 'G': iv *= 1024; + /* FALLTHROUGH */ case 'm': case 'M': iv *= 1024; + /* FALLTHROUGH */ case 'k': case 'K': iv *= 1024; + /* FALLTHROUGH */ case '\0': break; default: return (0); } return (iv); } /* * Lock the volume by opening its /dev device read/write. This will * only work if nothing else has it opened (including mounts). We * leak the fd on purpose since this application is not long-running. */ int mpt_lock_volume(U8 VolumeBus, U8 VolumeID) { char path[MAXPATHLEN]; struct mpt_query_disk qd; int error, vfd; error = mpt_query_disk(VolumeBus, VolumeID, &qd); if (error == ENOENT) /* * This means there isn't a CAM device associated with * the volume, and thus it is already implicitly * locked, so just return. */ return (0); if (error) { 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 (error); } return (0); } static int mpt_lock_physdisk(struct mpt_standalone_disk *disk) { char path[MAXPATHLEN]; 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 (error); } return (0); } static int mpt_lookup_standalone_disk(const char *name, struct mpt_standalone_disk *disks, int ndisks, int *index) { char *cp; long bus, id; int i; /* Check for a raw : string. */ bus = strtol(name, &cp, 0); if (*cp == ':') { id = strtol(cp + 1, &cp, 0); if (*cp == '\0') { if (bus < 0 || bus > 0xff || id < 0 || id > 0xff) { return (EINVAL); } for (i = 0; i < ndisks; i++) { if (disks[i].bus == (U8)bus && disks[i].target == (U8)id) { *index = i; return (0); } } return (ENOENT); } } if (name[0] == 'd' && name[1] == 'a') { for (i = 0; i < ndisks; i++) { if (strcmp(name, disks[i].devname) == 0) { *index = i; return (0); } } return (ENOENT); } return (EINVAL); } /* * Mark a standalone disk as being a physical disk. */ static int 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; 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); return (EOPNOTSUPP); } config_page = calloc(1, sizeof(CONFIG_PAGE_RAID_PHYS_DISK_0)); config_page->Header.PageType = MPI_CONFIG_PAGETYPE_RAID_PHYSDISK; config_page->Header.PageNumber = 0; config_page->Header.PageLength = sizeof(CONFIG_PAGE_RAID_PHYS_DISK_0) / 4; config_page->PhysDiskIOC = 0; /* XXX */ config_page->PhysDiskBus = disk->bus; config_page->PhysDiskID = disk->target; /* XXX: Enclosure info for PhysDiskSettings? */ 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); if (error) return (error); *PhysDiskNum = ActionData & 0xff; return (0); } static int mpt_delete_physdisk(int fd, U8 PhysDiskNum) { return (mpt_raid_action(fd, MPI_RAID_ACTION_DELETE_PHYSDISK, 0, 0, PhysDiskNum, 0, NULL, 0, NULL, NULL, 0, NULL, NULL, 0)); } /* * MPT's firmware does not have a clear command. Instead, we * implement it by deleting each array and disk by hand. */ static int clear_config(int ac, char **av) { CONFIG_PAGE_IOC_2 *ioc2; CONFIG_PAGE_IOC_2_RAID_VOL *vol; CONFIG_PAGE_IOC_3 *ioc3; IOC_3_PHYS_DISK *disk; CONFIG_PAGE_IOC_5 *ioc5; IOC_5_HOT_SPARE *spare; int ch, error, fd, i; fd = mpt_open(mpt_unit); if (fd < 0) { error = errno; warn("mpt_open"); return (error); } ioc2 = mpt_read_ioc_page(fd, 2, NULL); if (ioc2 == NULL) { error = errno; warn("Failed to fetch volume list"); + close(fd); return (error); } /* Lock all the volumes first. */ vol = ioc2->RaidVolume; for (i = 0; i < ioc2->NumActiveVolumes; vol++, i++) { if (mpt_lock_volume(vol->VolumeBus, vol->VolumeID) < 0) { warnx("Volume %s is busy and cannot be deleted", mpt_volume_name(vol->VolumeBus, vol->VolumeID)); + free(ioc2); + close(fd); return (EBUSY); } } printf( "Are you sure you wish to clear the configuration on mpt%u? [y/N] ", mpt_unit); ch = getchar(); if (ch != 'y' && ch != 'Y') { printf("\nAborting\n"); + free(ioc2); + close(fd); return (0); } /* Delete all the volumes. */ vol = ioc2->RaidVolume; 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); if (error) warnc(error, "Failed to delete volume %s", mpt_volume_name(vol->VolumeBus, vol->VolumeID)); } free(ioc2); /* Delete all the spares. */ ioc5 = mpt_read_ioc_page(fd, 5, NULL); if (ioc5 == NULL) warn("Failed to fetch spare list"); else { spare = ioc5->HotSpare; for (i = 0; i < ioc5->NumHotSpares; spare++, i++) if (mpt_delete_physdisk(fd, spare->PhysDiskNum) < 0) warn("Failed to delete physical disk %d", spare->PhysDiskNum); free(ioc5); } /* Delete any RAID physdisks that may be left. */ ioc3 = mpt_read_ioc_page(fd, 3, NULL); if (ioc3 == NULL) warn("Failed to fetch drive list"); else { disk = ioc3->PhysDisk; for (i = 0; i < ioc3->NumPhysDisks; disk++, i++) if (mpt_delete_physdisk(fd, disk->PhysDiskNum) < 0) warn("Failed to delete physical disk %d", disk->PhysDiskNum); free(ioc3); } printf("mpt%d: Configuration cleared\n", mpt_unit); mpt_rescan_bus(-1, -1); close(fd); return (0); } MPT_COMMAND(top, clear, clear_config); #define RT_RAID0 0 #define RT_RAID1 1 #define RT_RAID1E 2 static struct raid_type_entry { const char *name; int raid_type; } raid_type_table[] = { { "raid0", RT_RAID0 }, { "raid-0", RT_RAID0 }, { "raid1", RT_RAID1 }, { "raid-1", RT_RAID1 }, { "mirror", RT_RAID1 }, { "raid1e", RT_RAID1E }, { "raid-1e", RT_RAID1E }, { NULL, 0 }, }; struct config_id_state { struct mpt_standalone_disk *sdisks; struct mpt_drive_list *list; CONFIG_PAGE_IOC_2 *ioc2; U8 target_id; int nsdisks; }; struct drive_info { CONFIG_PAGE_RAID_PHYS_DISK_0 *info; struct mpt_standalone_disk *sdisk; }; struct volume_info { int drive_count; struct drive_info *drives; }; /* Parse a comma-separated list of drives for a volume. */ static int parse_volume(int fd, int raid_type, struct config_id_state *state, char *volume_str, struct volume_info *info) { struct drive_info *dinfo; U8 PhysDiskNum; char *cp; int count, error, i; cp = volume_str; for (count = 0; cp != NULL; count++) { cp = strchr(cp, ','); if (cp != NULL) { cp++; if (*cp == ',') { warnx("Invalid drive list '%s'", volume_str); return (EINVAL); } } } /* Validate the number of drives for this volume. */ switch (raid_type) { case RT_RAID0: if (count < 2) { warnx("RAID0 requires at least 2 drives in each " "array"); return (EINVAL); } break; case RT_RAID1: if (count != 2) { warnx("RAID1 requires exactly 2 drives in each " "array"); return (EINVAL); } break; case RT_RAID1E: if (count < 3) { warnx("RAID1E requires at least 3 drives in each " "array"); return (EINVAL); } break; } /* Validate each drive. */ info->drives = calloc(count, sizeof(struct drive_info)); info->drive_count = count; for (dinfo = info->drives; (cp = strsep(&volume_str, ",")) != NULL; dinfo++) { /* If this drive is already a RAID phys just fetch the info. */ error = mpt_lookup_drive(state->list, cp, &PhysDiskNum); if (error == 0) { dinfo->info = mpt_pd_info(fd, PhysDiskNum, NULL); if (dinfo->info == NULL) return (errno); continue; } /* 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 (error); } dinfo->sdisk = &state->sdisks[i]; /* Lock the disk, we will create phys disk pages later. */ if (mpt_lock_physdisk(dinfo->sdisk) < 0) return (errno); } return (0); } /* * Add RAID physdisk pages for any standalone disks that a volume is * going to use. */ static int add_drives(int fd, struct volume_info *info, int verbose) { struct drive_info *dinfo; U8 PhysDiskNum; 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 (error); } if (verbose) printf("Added drive %s with PhysDiskNum %u\n", dinfo->sdisk->devname, PhysDiskNum); dinfo->info = mpt_pd_info(fd, PhysDiskNum, NULL); if (dinfo->info == NULL) return (errno); } } return (0); } /* * Find the next free target ID assuming that 'target_id' is the last * one used. 'target_id' should be 0xff for the initial test. */ static U8 find_next_volume(struct config_id_state *state) { CONFIG_PAGE_IOC_2_RAID_VOL *vol; int i; restart: /* Assume the current one is used. */ state->target_id++; /* Search drives first. */ for (i = 0; i < state->nsdisks; i++) if (state->sdisks[i].target == state->target_id) goto restart; for (i = 0; i < state->list->ndrives; i++) if (state->list->drives[i]->PhysDiskID == state->target_id) goto restart; /* Search volumes second. */ vol = state->ioc2->RaidVolume; for (i = 0; i < state->ioc2->NumActiveVolumes; vol++, i++) if (vol->VolumeID == state->target_id) goto restart; return (state->target_id); } /* Create a volume and populate it with drives. */ static CONFIG_PAGE_RAID_VOL_0 * build_volume(int fd, struct volume_info *info, int raid_type, long stripe_size, struct config_id_state *state, int verbose) { CONFIG_PAGE_HEADER header; CONFIG_PAGE_RAID_VOL_0 *vol; RAID_VOL0_PHYS_DISK *rdisk; struct drive_info *dinfo; U32 MinLBA; uint64_t MaxLBA; size_t page_size; int error, i; 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); errno = EOPNOTSUPP; return (NULL); } 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; vol->Header.PageNumber = 0; vol->Header.PageLength = page_size / 4; /* Properties */ vol->VolumeID = find_next_volume(state); vol->VolumeBus = 0; vol->VolumeIOC = 0; /* XXX */ vol->VolumeStatus.Flags = MPI_RAIDVOL0_STATUS_FLAG_ENABLED; vol->VolumeStatus.State = MPI_RAIDVOL0_STATUS_STATE_OPTIMAL; vol->VolumeSettings.Settings = MPI_RAIDVOL0_SETTING_USE_DEFAULTS; vol->VolumeSettings.HotSparePool = MPI_RAID_HOT_SPARE_POOL_0; vol->NumPhysDisks = info->drive_count; /* Find the smallest drive. */ MinLBA = info->drives[0].info->MaxLBA; for (i = 1; i < info->drive_count; i++) if (info->drives[i].info->MaxLBA < MinLBA) MinLBA = info->drives[i].info->MaxLBA; /* * Now chop off 512MB at the end to leave room for the * metadata. The controller might only use 64MB, but we just * chop off the max to be simple. */ MinLBA -= (512 * 1024 * 1024) / 512; switch (raid_type) { case RT_RAID0: vol->VolumeType = MPI_RAID_VOL_TYPE_IS; vol->StripeSize = stripe_size / 512; - MaxLBA = MinLBA * info->drive_count; + MaxLBA = (uint64_t)MinLBA * info->drive_count; break; case RT_RAID1: vol->VolumeType = MPI_RAID_VOL_TYPE_IM; - MaxLBA = MinLBA * (info->drive_count / 2); + MaxLBA = (uint64_t)MinLBA * (info->drive_count / 2); break; case RT_RAID1E: vol->VolumeType = MPI_RAID_VOL_TYPE_IME; vol->StripeSize = stripe_size / 512; - MaxLBA = MinLBA * info->drive_count / 2; + MaxLBA = (uint64_t)MinLBA * info->drive_count / 2; break; default: /* Pacify gcc. */ abort(); } /* * If the controller doesn't support 64-bit addressing and the * new volume is larger than 2^32 blocks, warn the user and * truncate the volume. */ if (MaxLBA >> 32 != 0 && !(state->ioc2->CapabilitiesFlags & MPI_IOCPAGE2_CAP_FLAGS_RAID_64_BIT_ADDRESSING)) { warnx( "Controller does not support volumes > 2TB, truncating volume."); MaxLBA = 0xffffffff; } vol->MaxLBA = MaxLBA; vol->MaxLBAHigh = MaxLBA >> 32; /* Populate drives. */ for (i = 0, dinfo = info->drives, rdisk = vol->PhysDisk; i < info->drive_count; i++, dinfo++, rdisk++) { if (verbose) printf("Adding drive %u (%u:%u) to volume %u:%u\n", dinfo->info->PhysDiskNum, dinfo->info->PhysDiskBus, dinfo->info->PhysDiskID, vol->VolumeBus, vol->VolumeID); if (raid_type == RT_RAID1) { if (i == 0) rdisk->PhysDiskMap = MPI_RAIDVOL0_PHYSDISK_PRIMARY; else rdisk->PhysDiskMap = MPI_RAIDVOL0_PHYSDISK_SECONDARY; } else rdisk->PhysDiskMap = i; rdisk->PhysDiskNum = dinfo->info->PhysDiskNum; } return (vol); } static int create_volume(int ac, char **av) { CONFIG_PAGE_RAID_VOL_0 *vol; struct config_id_state state; struct volume_info *info; long stripe_size; int ch, error, fd, i, quick, raid_type, verbose; #ifdef DEBUG int dump; #endif if (ac < 2) { warnx("create: volume type required"); return (EINVAL); } fd = mpt_open(mpt_unit); if (fd < 0) { error = errno; warn("mpt_open"); return (error); } /* Lookup the RAID type first. */ raid_type = -1; for (i = 0; raid_type_table[i].name != NULL; i++) if (strcasecmp(raid_type_table[i].name, av[1]) == 0) { raid_type = raid_type_table[i].raid_type; break; } if (raid_type == -1) { warnx("Unknown or unsupported volume type %s", av[1]); + close(fd); return (EINVAL); } /* Parse any options. */ optind = 2; #ifdef DEBUG dump = 0; #endif quick = 0; verbose = 0; stripe_size = 64 * 1024; while ((ch = getopt(ac, av, "dqs:v")) != -1) { switch (ch) { #ifdef DEBUG case 'd': dump = 1; break; #endif case 'q': quick = 1; break; case 's': stripe_size = dehumanize(optarg); if ((stripe_size < 512) || (!powerof2(stripe_size))) { warnx("Invalid stripe size %s", optarg); + close(fd); return (EINVAL); } break; case 'v': verbose = 1; break; case '?': default: + close(fd); return (EINVAL); } } ac -= optind; av += optind; /* 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"); + close(fd); return (error); } state.list = mpt_pd_list(fd); - if (state.list == NULL) + if (state.list == NULL) { + close(fd); return (errno); + } error = mpt_fetch_disks(fd, &state.nsdisks, &state.sdisks); if (error) { warn("Failed to fetch standalone disk list"); + close(fd); return (error); } state.target_id = 0xff; /* Parse the drive list. */ if (ac != 1) { warnx("Exactly one drive list is required"); + close(fd); return (EINVAL); } info = calloc(1, sizeof(*info)); - if (info == NULL) + if (info == NULL) { + close(fd); return (ENOMEM); + } error = parse_volume(fd, raid_type, &state, av[0], info); - if (error) + if (error) { + free(info); + close(fd); return (error); + } /* Create RAID physdisk pages for standalone disks. */ error = add_drives(fd, info, verbose); - if (error) + if (error) { + free(info); + close(fd); return (error); + } /* Build the volume. */ vol = build_volume(fd, info, raid_type, stripe_size, &state, verbose); - if (vol == NULL) + if (vol == NULL) { + free(info); + close(fd); return (errno); + } #ifdef DEBUG if (dump) { dump_config(vol); goto skip; } #endif /* Send the new volume to the controller. */ 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); if (error) { errno = error; warn("Failed to add volume"); + free(info); + close(fd); return (error); } #ifdef DEBUG skip: #endif mpt_rescan_bus(vol->VolumeBus, vol->VolumeID); /* Clean up. */ free(vol); free(info); free(state.sdisks); mpt_free_pd_list(state.list); free(state.ioc2); close(fd); return (0); } MPT_COMMAND(top, create, create_volume); static int delete_volume(int ac, char **av) { U8 VolumeBus, VolumeID; int error, fd; if (ac != 2) { warnx("delete: volume required"); return (EINVAL); } fd = mpt_open(mpt_unit); if (fd < 0) { error = errno; warn("mpt_open"); return (error); } error = mpt_lookup_volume(fd, av[1], &VolumeBus, &VolumeID); if (error) { warnc(error, "Invalid volume %s", av[1]); + close(fd); return (error); } - if (mpt_lock_volume(VolumeBus, VolumeID) < 0) + if (mpt_lock_volume(VolumeBus, VolumeID) < 0) { + close(fd); return (errno); + } 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); if (error) { warnc(error, "Failed to delete volume"); + close(fd); return (error); } mpt_rescan_bus(-1, -1); close(fd); return (0); } MPT_COMMAND(top, delete, delete_volume); static int find_volume_spare_pool(int fd, const char *name, int *pool) { CONFIG_PAGE_RAID_VOL_0 *info; CONFIG_PAGE_IOC_2 *ioc2; CONFIG_PAGE_IOC_2_RAID_VOL *vol; U8 VolumeBus, VolumeID; int error, i, j, new_pool, pool_count[7]; 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 (errno); /* * Check for an existing pool other than pool 0 (used for * global spares). */ if ((info->VolumeSettings.HotSparePool & ~MPI_RAID_HOT_SPARE_POOL_0) != 0) { *pool = 1 << (ffs(info->VolumeSettings.HotSparePool & ~MPI_RAID_HOT_SPARE_POOL_0) - 1); + free(info); return (0); } free(info); /* * Try to find a free pool. First, figure out which pools are * in use. */ ioc2 = mpt_read_ioc_page(fd, 2, NULL); if (ioc2 == NULL) { error = errno; warn("Failed to fetch volume list"); 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 (errno); for (j = 0; j < 7; j++) if (info->VolumeSettings.HotSparePool & (1 << (j + 1))) pool_count[j]++; free(info); } free(ioc2); /* Find the pool with the lowest use count. */ new_pool = 0; for (i = 1; i < 7; i++) if (pool_count[i] < pool_count[new_pool]) new_pool = i; new_pool++; /* Add this pool to the volume. */ info = mpt_vol_info(fd, VolumeBus, VolumeID, NULL); if (info == NULL) return (error); info->VolumeSettings.HotSparePool |= (1 << new_pool); 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); if (error) { warnx("Failed to add spare pool %d to %s", new_pool, mpt_volume_name(VolumeBus, VolumeID)); + free(info); return (error); } free(info); *pool = (1 << new_pool); return (0); } static int add_spare(int ac, char **av) { CONFIG_PAGE_RAID_PHYS_DISK_0 *info; struct mpt_standalone_disk *sdisks; struct mpt_drive_list *list; U8 PhysDiskNum; int error, fd, i, nsdisks, pool; if (ac < 2) { warnx("add spare: drive required"); return (EINVAL); } if (ac > 3) { warnx("add spare: extra arguments"); return (EINVAL); } fd = mpt_open(mpt_unit); if (fd < 0) { error = errno; warn("mpt_open"); return (error); } if (ac == 3) { error = find_volume_spare_pool(fd, av[2], &pool); - if (error) + if (error) { + close(fd); return (error); + } } else pool = MPI_RAID_HOT_SPARE_POOL_0; list = mpt_pd_list(fd); if (list == NULL) return (errno); error = mpt_lookup_drive(list, av[1], &PhysDiskNum); if (error) { error = mpt_fetch_disks(fd, &nsdisks, &sdisks); if (error != 0) { warn("Failed to fetch standalone disk list"); + mpt_free_pd_list(list); + mpt_free_pd_list(list); + close(fd); return (error); } if (mpt_lookup_standalone_disk(av[1], sdisks, nsdisks, &i) < 0) { error = errno; warn("Unable to lookup drive %s", av[1]); + mpt_free_pd_list(list); + mpt_free_pd_list(list); + close(fd); return (error); } - if (mpt_lock_physdisk(&sdisks[i]) < 0) + if (mpt_lock_physdisk(&sdisks[i]) < 0) { + mpt_free_pd_list(list); + mpt_free_pd_list(list); + close(fd); return (errno); + } if (mpt_create_physdisk(fd, &sdisks[i], &PhysDiskNum) < 0) { error = errno; warn("Failed to create physical disk page"); + mpt_free_pd_list(list); + mpt_free_pd_list(list); + close(fd); return (error); } free(sdisks); } mpt_free_pd_list(list); info = mpt_pd_info(fd, PhysDiskNum, NULL); if (info == NULL) { error = errno; warn("Failed to fetch drive info"); + close(fd); return (error); } info->PhysDiskSettings.HotSparePool = pool; error = mpt_raid_action(fd, MPI_RAID_ACTION_CHANGE_PHYSDISK_SETTINGS, 0, 0, PhysDiskNum, *(U32 *)&info->PhysDiskSettings, NULL, 0, NULL, NULL, 0, NULL, NULL, 0); if (error) { warnc(error, "Failed to assign spare"); + close(fd); return (error); } free(info); close(fd); return (0); } MPT_COMMAND(top, add, add_spare); static int remove_spare(int ac, char **av) { CONFIG_PAGE_RAID_PHYS_DISK_0 *info; struct mpt_drive_list *list; U8 PhysDiskNum; int error, fd; if (ac != 2) { warnx("remove spare: drive required"); return (EINVAL); } fd = mpt_open(mpt_unit); if (fd < 0) { error = errno; warn("mpt_open"); return (error); } list = mpt_pd_list(fd); - if (list == NULL) + if (list == NULL) { + close(fd); return (errno); + } error = mpt_lookup_drive(list, av[1], &PhysDiskNum); if (error) { warn("Failed to find drive %s", av[1]); + close(fd); 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"); + close(fd); return (error); } if (info->PhysDiskSettings.HotSparePool == 0) { warnx("Drive %u is not a hot spare", PhysDiskNum); + free(info); + close(fd); return (EINVAL); } if (mpt_delete_physdisk(fd, PhysDiskNum) < 0) { error = errno; warn("Failed to delete physical disk page"); + free(info); + close(fd); return (error); } mpt_rescan_bus(info->PhysDiskBus, info->PhysDiskID); free(info); close(fd); return (0); } MPT_COMMAND(top, remove, remove_spare); #ifdef DEBUG MPT_TABLE(top, pd); static int pd_create(int ac, char **av) { struct mpt_standalone_disk *disks; int error, fd, i, ndisks; U8 PhysDiskNum; if (ac != 2) { warnx("pd create: drive required"); return (EINVAL); } fd = mpt_open(mpt_unit); if (fd < 0) { error = errno; warn("mpt_open"); return (error); } error = mpt_fetch_disks(fd, &ndisks, &disks); if (error != 0) { warn("Failed to fetch standalone disk list"); return (error); } if (mpt_lookup_standalone_disk(av[1], disks, ndisks, &i) < 0) { error = errno; warn("Unable to lookup drive"); 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 (error); } free(disks); printf("Added drive %s with PhysDiskNum %u\n", av[1], PhysDiskNum); close(fd); return (0); } MPT_COMMAND(pd, create, pd_create); static int pd_delete(int ac, char **av) { CONFIG_PAGE_RAID_PHYS_DISK_0 *info; struct mpt_drive_list *list; int error, fd; U8 PhysDiskNum; if (ac != 2) { warnx("pd delete: drive required"); return (EINVAL); } fd = mpt_open(mpt_unit); if (fd < 0) { error = errno; warn("mpt_open"); return (error); } list = mpt_pd_list(fd); if (list == NULL) return (errno); if (mpt_lookup_drive(list, av[1], &PhysDiskNum) < 0) { error = errno; warn("Failed to find drive %s", av[1]); 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 (error); } if (mpt_delete_physdisk(fd, PhysDiskNum) < 0) { error = errno; warn("Failed to delete physical disk page"); return (error); } mpt_rescan_bus(info->PhysDiskBus, info->PhysDiskID); free(info); close(fd); return (0); } MPT_COMMAND(pd, delete, pd_delete); /* Display raw data about a volume config. */ static void dump_config(CONFIG_PAGE_RAID_VOL_0 *vol) { int i; printf("Volume Configuration (Debug):\n"); printf( " Page Header: Type 0x%02x Number 0x%02x Length 0x%02x(%u) Version 0x%02x\n", vol->Header.PageType, vol->Header.PageNumber, vol->Header.PageLength, vol->Header.PageLength * 4, vol->Header.PageVersion); printf(" Address: %d:%d IOC %d\n", vol->VolumeBus, vol->VolumeID, vol->VolumeIOC); printf(" Type: %d (%s)\n", vol->VolumeType, mpt_raid_level(vol->VolumeType)); printf(" Status: %s (Flags 0x%02x)\n", mpt_volstate(vol->VolumeStatus.State), vol->VolumeStatus.Flags); printf(" Settings: 0x%04x (Spare Pools 0x%02x)\n", vol->VolumeSettings.Settings, vol->VolumeSettings.HotSparePool); printf(" MaxLBA: %ju\n", (uintmax_t)vol->MaxLBAHigh << 32 | vol->MaxLBA); printf(" Stripe Size: %ld\n", (long)vol->StripeSize * 512); printf(" %d Disks:\n", vol->NumPhysDisks); for (i = 0; i < vol->NumPhysDisks; i++) printf(" Disk %d: Num 0x%02x Map 0x%02x\n", i, vol->PhysDisk[i].PhysDiskNum, vol->PhysDisk[i].PhysDiskMap); } static int debug_config(int ac, char **av) { CONFIG_PAGE_RAID_VOL_0 *vol; U8 VolumeBus, VolumeID; int error, fd; if (ac != 2) { warnx("debug: volume required"); return (EINVAL); } fd = mpt_open(mpt_unit); if (fd < 0) { error = errno; warn("mpt_open"); return (error); } 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 (error); } dump_config(vol); free(vol); close(fd); return (0); } MPT_COMMAND(top, debug, debug_config); #endif Index: head/usr.sbin/mptutil/mpt_drive.c =================================================================== --- head/usr.sbin/mptutil/mpt_drive.c (revision 329844) +++ head/usr.sbin/mptutil/mpt_drive.c (revision 329845) @@ -1,407 +1,432 @@ /*- * SPDX-License-Identifier: BSD-3-Clause * * Copyright (c) 2008 Yahoo!, Inc. * All rights reserved. * Written by: John Baldwin * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the author nor the names of any co-contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include __RCSID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include #include #include #include #include #include "mptutil.h" const char * mpt_pdstate(CONFIG_PAGE_RAID_PHYS_DISK_0 *info) { static char buf[16]; switch (info->PhysDiskStatus.State) { case MPI_PHYSDISK0_STATUS_ONLINE: if ((info->PhysDiskStatus.Flags & MPI_PHYSDISK0_STATUS_FLAG_OUT_OF_SYNC) && info->PhysDiskSettings.HotSparePool == 0) return ("REBUILD"); else return ("ONLINE"); case MPI_PHYSDISK0_STATUS_MISSING: return ("MISSING"); case MPI_PHYSDISK0_STATUS_NOT_COMPATIBLE: return ("NOT COMPATIBLE"); case MPI_PHYSDISK0_STATUS_FAILED: return ("FAILED"); case MPI_PHYSDISK0_STATUS_INITIALIZING: return ("INITIALIZING"); case MPI_PHYSDISK0_STATUS_OFFLINE_REQUESTED: return ("OFFLINE REQUESTED"); case MPI_PHYSDISK0_STATUS_FAILED_REQUESTED: return ("FAILED REQUESTED"); case MPI_PHYSDISK0_STATUS_OTHER_OFFLINE: return ("OTHER OFFLINE"); default: sprintf(buf, "PSTATE 0x%02x", info->PhysDiskStatus.State); return (buf); } } /* * There are several ways to enumerate physical disks. Unfortunately, * none of them are truly complete, so we have to build a union of all of * them. Specifically: * * - IOC2 : This gives us a list of volumes, and by walking the volumes we * can enumerate all of the drives attached to volumes including * online drives and failed drives. * - IOC3 : This gives us a list of all online physical drives including * drives that are not part of a volume nor a spare drive. It * does not include any failed drives. * - IOC5 : This gives us a list of all spare drives including failed * spares. * * The specific edge cases are that 1) a failed volume member can only be * found via IOC2, 2) a drive that is neither a volume member nor a spare * can only be found via IOC3, and 3) a failed spare can only be found via * IOC5. * * To handle this, walk all of the three lists and use the following * routine to add each drive encountered. It quietly succeeds if the * drive is already present in the list. It also sorts the list as it * inserts new drives. */ static int mpt_pd_insert(int fd, struct mpt_drive_list *list, U8 PhysDiskNum) { int i, j; /* * First, do a simple linear search to see if we have already * seen this drive. */ for (i = 0; i < list->ndrives; i++) { if (list->drives[i]->PhysDiskNum == PhysDiskNum) return (0); if (list->drives[i]->PhysDiskNum > PhysDiskNum) break; } /* * 'i' is our slot for the 'new' drive. Make room and then * read the drive info. */ for (j = list->ndrives - 1; j >= i; j--) list->drives[j + 1] = list->drives[j]; list->drives[i] = mpt_pd_info(fd, PhysDiskNum, NULL); if (list->drives[i] == NULL) return (errno); list->ndrives++; return (0); } struct mpt_drive_list * mpt_pd_list(int fd) { CONFIG_PAGE_IOC_2 *ioc2; CONFIG_PAGE_IOC_2_RAID_VOL *vol; CONFIG_PAGE_RAID_VOL_0 **volumes; RAID_VOL0_PHYS_DISK *rdisk; CONFIG_PAGE_IOC_3 *ioc3; IOC_3_PHYS_DISK *disk; CONFIG_PAGE_IOC_5 *ioc5; IOC_5_HOT_SPARE *spare; struct mpt_drive_list *list; int count, error, i, j; + size_t listsize; ioc2 = mpt_read_ioc_page(fd, 2, NULL); if (ioc2 == NULL) { error = errno; warn("Failed to fetch volume list"); errno = error; return (NULL); } ioc3 = mpt_read_ioc_page(fd, 3, NULL); if (ioc3 == NULL) { error = errno; warn("Failed to fetch drive list"); free(ioc2); errno = error; return (NULL); } ioc5 = mpt_read_ioc_page(fd, 5, NULL); if (ioc5 == NULL) { error = errno; warn("Failed to fetch spare list"); free(ioc3); free(ioc2); errno = error; return (NULL); } /* * Go ahead and read the info for all the volumes. For this * pass we figure out how many physical drives there are. */ volumes = malloc(sizeof(*volumes) * ioc2->NumActiveVolumes); count = 0; vol = ioc2->RaidVolume; for (i = 0; i < ioc2->NumActiveVolumes; vol++, i++) { volumes[i] = mpt_vol_info(fd, vol->VolumeBus, vol->VolumeID, NULL); if (volumes[i] == NULL) { error = errno; warn("Failed to read volume info"); errno = error; + free(volumes); + free(ioc5); + free(ioc3); + free(ioc2); return (NULL); } count += volumes[i]->NumPhysDisks; } count += ioc3->NumPhysDisks; count += ioc5->NumHotSpares; /* Walk the various lists enumerating drives. */ - list = malloc(sizeof(*list) + sizeof(CONFIG_PAGE_RAID_PHYS_DISK_0) * - count); - list->ndrives = 0; + listsize = sizeof(*list) + sizeof(CONFIG_PAGE_RAID_PHYS_DISK_0) * count; + list = calloc(1, listsize); for (i = 0; i < ioc2->NumActiveVolumes; i++) { rdisk = volumes[i]->PhysDisk; for (j = 0; j < volumes[i]->NumPhysDisks; rdisk++, j++) - if (mpt_pd_insert(fd, list, rdisk->PhysDiskNum) < 0) + if (mpt_pd_insert(fd, list, rdisk->PhysDiskNum) < 0) { + mpt_free_pd_list(list); + free(volumes); + free(ioc5); + free(ioc3); + free(ioc2); return (NULL); + } free(volumes[i]); } free(ioc2); free(volumes); spare = ioc5->HotSpare; for (i = 0; i < ioc5->NumHotSpares; spare++, i++) - if (mpt_pd_insert(fd, list, spare->PhysDiskNum) < 0) + if (mpt_pd_insert(fd, list, spare->PhysDiskNum) < 0) { + mpt_free_pd_list(list); + free(ioc5); + free(ioc3); return (NULL); + } free(ioc5); disk = ioc3->PhysDisk; for (i = 0; i < ioc3->NumPhysDisks; disk++, i++) - if (mpt_pd_insert(fd, list, disk->PhysDiskNum) < 0) + if (mpt_pd_insert(fd, list, disk->PhysDiskNum) < 0) { + mpt_free_pd_list(list); + free(ioc3); return (NULL); + } free(ioc3); return (list); } void mpt_free_pd_list(struct mpt_drive_list *list) { int i; for (i = 0; i < list->ndrives; i++) free(list->drives[i]); free(list); } int mpt_lookup_drive(struct mpt_drive_list *list, const char *drive, U8 *PhysDiskNum) { long val; uint8_t bus, id; char *cp; /* Look for a raw device id first. */ val = strtol(drive, &cp, 0); if (*cp == '\0') { if (val < 0 || val > 0xff) goto bad; *PhysDiskNum = val; return (0); } /* Look for a : string. */ if (*cp == ':') { if (val < 0 || val > 0xff) goto bad; bus = val; val = strtol(cp + 1, &cp, 0); if (*cp != '\0') goto bad; if (val < 0 || val > 0xff) goto bad; id = val; for (val = 0; val < list->ndrives; val++) { if (list->drives[val]->PhysDiskBus == bus && list->drives[val]->PhysDiskID == id) { *PhysDiskNum = list->drives[val]->PhysDiskNum; return (0); } } return (ENOENT); } bad: return (EINVAL); } /* Borrowed heavily from scsi_all.c:scsi_print_inquiry(). */ const char * mpt_pd_inq_string(CONFIG_PAGE_RAID_PHYS_DISK_0 *pd_info) { RAID_PHYS_DISK0_INQUIRY_DATA *inq_data; u_char vendor[9], product[17], revision[5]; static char inq_string[64]; inq_data = &pd_info->InquiryData; cam_strvis(vendor, inq_data->VendorID, sizeof(inq_data->VendorID), sizeof(vendor)); cam_strvis(product, inq_data->ProductID, sizeof(inq_data->ProductID), sizeof(product)); cam_strvis(revision, inq_data->ProductRevLevel, sizeof(inq_data->ProductRevLevel), sizeof(revision)); /* Total hack. */ if (strcmp(vendor, "ATA") == 0) snprintf(inq_string, sizeof(inq_string), "<%s %s> SATA", product, revision); else snprintf(inq_string, sizeof(inq_string), "<%s %s %s> SAS", vendor, product, revision); return (inq_string); } /* Helper function to set a drive to a given state. */ static int drive_set_state(char *drive, U8 Action, U8 State, const char *name) { CONFIG_PAGE_RAID_PHYS_DISK_0 *info; struct mpt_drive_list *list; U8 PhysDiskNum; int error, fd; fd = mpt_open(mpt_unit); if (fd < 0) { error = errno; warn("mpt_open"); return (error); } list = mpt_pd_list(fd); - if (list == NULL) + if (list == NULL) { + close(fd); return (errno); + } if (mpt_lookup_drive(list, drive, &PhysDiskNum) < 0) { error = errno; warn("Failed to find drive %s", drive); + close(fd); return (error); } mpt_free_pd_list(list); /* Get the info for this drive. */ info = mpt_pd_info(fd, PhysDiskNum, NULL); if (info == NULL) { error = errno; warn("Failed to fetch info for drive %u", PhysDiskNum); + close(fd); return (error); } /* Try to change the state. */ if (info->PhysDiskStatus.State == State) { warnx("Drive %u is already in the desired state", PhysDiskNum); + free(info); + close(fd); return (EINVAL); } error = mpt_raid_action(fd, Action, 0, 0, PhysDiskNum, 0, NULL, 0, NULL, NULL, 0, NULL, NULL, 0); if (error) { warnc(error, "Failed to set drive %u to %s", PhysDiskNum, name); + free(info); + close(fd); return (error); } free(info); close(fd); return (0); } static int fail_drive(int ac, char **av) { if (ac != 2) { warnx("fail: %s", ac > 2 ? "extra arguments" : "drive required"); return (EINVAL); } return (drive_set_state(av[1], MPI_RAID_ACTION_FAIL_PHYSDISK, MPI_PHYSDISK0_STATUS_FAILED_REQUESTED, "FAILED")); } MPT_COMMAND(top, fail, fail_drive); static int online_drive(int ac, char **av) { if (ac != 2) { warnx("online: %s", ac > 2 ? "extra arguments" : "drive required"); return (EINVAL); } return (drive_set_state(av[1], MPI_RAID_ACTION_PHYSDISK_ONLINE, MPI_PHYSDISK0_STATUS_ONLINE, "ONLINE")); } MPT_COMMAND(top, online, online_drive); static int offline_drive(int ac, char **av) { if (ac != 2) { warnx("offline: %s", ac > 2 ? "extra arguments" : "drive required"); return (EINVAL); } return (drive_set_state(av[1], MPI_RAID_ACTION_PHYSDISK_OFFLINE, MPI_PHYSDISK0_STATUS_OFFLINE_REQUESTED, "OFFLINE")); } MPT_COMMAND(top, offline, offline_drive); Index: head/usr.sbin/mptutil/mpt_evt.c =================================================================== --- head/usr.sbin/mptutil/mpt_evt.c (revision 329844) +++ head/usr.sbin/mptutil/mpt_evt.c (revision 329845) @@ -1,161 +1,167 @@ /*- * SPDX-License-Identifier: BSD-3-Clause * * Copyright (c) 2008 Yahoo!, Inc. * All rights reserved. * Written by: John Baldwin * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the author nor the names of any co-contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include __RCSID("$FreeBSD$"); #include #include #include #include #include #include #include #include "mptutil.h" static CONFIG_PAGE_LOG_0 * mpt_get_events(int fd, U16 *IOCStatus) { return (mpt_read_extended_config_page(fd, MPI_CONFIG_EXTPAGETYPE_LOG, 0, 0, 0, IOCStatus)); } /* * 1 2 3 4 5 6 7 * 1234567890123456789012345678901234567890123456789012345678901234567890 * < ID> < time > LogSequence, entry->TimeStamp, entry->LogEntryQualifier); for (i = 0; i < 14; i++) printf("%02x ", entry->LogData[i]); printf("|"); for (i = 0; i < 14; i++) printf("%c", isprint(entry->LogData[i]) ? entry->LogData[i] : '.'); printf("|\n"); printf(" "); for (i = 0; i < 14; i++) printf("%02x ", entry->LogData[i + 14]); printf("|"); for (i = 0; i < 14; i++) printf("%c", isprint(entry->LogData[i + 14]) ? entry->LogData[i + 14] : '.'); printf("|\n"); } static int event_compare(const void *first, const void *second) { MPI_LOG_0_ENTRY * const *one; MPI_LOG_0_ENTRY * const *two; one = first; two = second; return ((*one)->LogSequence - ((*two)->LogSequence)); } static int show_events(int ac, char **av) { CONFIG_PAGE_LOG_0 *log; MPI_LOG_0_ENTRY **entries; int ch, error, fd, i, num_events, verbose; fd = mpt_open(mpt_unit); if (fd < 0) { error = errno; warn("mpt_open"); return (error); } log = mpt_get_events(fd, NULL); if (log == NULL) { error = errno; warn("Failed to get event log info"); return (error); } /* Default settings. */ verbose = 0; /* Parse any options. */ optind = 1; while ((ch = getopt(ac, av, "v")) != -1) { switch (ch) { case 'v': verbose = 1; break; case '?': default: + free(log); + close(fd); return (EINVAL); } } ac -= optind; av += optind; /* Build a list of valid entries and sort them by sequence. */ entries = malloc(sizeof(MPI_LOG_0_ENTRY *) * log->NumLogEntries); - if (entries == NULL) + if (entries == NULL) { + free(log); + close(fd); return (ENOMEM); + } num_events = 0; for (i = 0; i < log->NumLogEntries; i++) { if (log->LogEntry[i].LogEntryQualifier == MPI_LOG_0_ENTRY_QUAL_ENTRY_UNUSED) continue; entries[num_events] = &log->LogEntry[i]; num_events++; } qsort(entries, num_events, sizeof(MPI_LOG_0_ENTRY *), event_compare); if (num_events == 0) printf("Event log is empty\n"); else { printf(" ID Time Type Log Data\n"); for (i = 0; i < num_events; i++) mpt_print_event(entries[i], verbose); } free(entries); + free(log); close(fd); return (0); } MPT_COMMAND(show, events, show_events); Index: head/usr.sbin/mptutil/mpt_show.c =================================================================== --- head/usr.sbin/mptutil/mpt_show.c (revision 329844) +++ head/usr.sbin/mptutil/mpt_show.c (revision 329845) @@ -1,577 +1,587 @@ /*- * SPDX-License-Identifier: BSD-3-Clause * * Copyright (c) 2008 Yahoo!, Inc. * All rights reserved. * Written by: John Baldwin * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the author nor the names of any co-contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include __RCSID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include "mptutil.h" MPT_TABLE(top, show); #define STANDALONE_STATE "ONLINE" static void format_stripe(char *buf, size_t buflen, U32 stripe) { humanize_number(buf, buflen, stripe * 512, "", HN_AUTOSCALE, HN_B | HN_NOSPACE); } static void display_stripe_map(const char *label, U32 StripeMap) { char stripe[5]; int comma, i; comma = 0; printf("%s: ", label); for (i = 0; StripeMap != 0; i++, StripeMap >>= 1) if (StripeMap & 1) { format_stripe(stripe, sizeof(stripe), 1 << i); if (comma) printf(", "); printf("%s", stripe); comma = 1; } printf("\n"); } static int show_adapter(int ac, char **av) { CONFIG_PAGE_MANUFACTURING_0 *man0; CONFIG_PAGE_IOC_2 *ioc2; CONFIG_PAGE_IOC_6 *ioc6; U16 IOCStatus; int comma, error, fd; if (ac != 1) { warnx("show adapter: extra arguments"); return (EINVAL); } fd = mpt_open(mpt_unit); if (fd < 0) { error = errno; warn("mpt_open"); return (error); } man0 = mpt_read_man_page(fd, 0, NULL); if (man0 == NULL) { error = errno; warn("Failed to get controller info"); + close(fd); return (error); } if (man0->Header.PageLength < sizeof(*man0) / 4) { warnx("Invalid controller info"); + free(man0); + close(fd); return (EINVAL); } printf("mpt%d Adapter:\n", mpt_unit); printf(" Board Name: %.16s\n", man0->BoardName); printf(" Board Assembly: %.16s\n", man0->BoardAssembly); printf(" Chip Name: %.16s\n", man0->ChipName); printf(" Chip Revision: %.16s\n", man0->ChipRevision); free(man0); ioc2 = mpt_read_ioc_page(fd, 2, &IOCStatus); if (ioc2 != NULL) { printf(" RAID Levels:"); comma = 0; if (ioc2->CapabilitiesFlags & MPI_IOCPAGE2_CAP_FLAGS_IS_SUPPORT) { printf(" RAID0"); comma = 1; } if (ioc2->CapabilitiesFlags & MPI_IOCPAGE2_CAP_FLAGS_IM_SUPPORT) { printf("%s RAID1", comma ? "," : ""); comma = 1; } if (ioc2->CapabilitiesFlags & MPI_IOCPAGE2_CAP_FLAGS_IME_SUPPORT) { printf("%s RAID1E", comma ? "," : ""); comma = 1; } if (ioc2->CapabilitiesFlags & MPI_IOCPAGE2_CAP_FLAGS_RAID_5_SUPPORT) { printf("%s RAID5", comma ? "," : ""); comma = 1; } if (ioc2->CapabilitiesFlags & MPI_IOCPAGE2_CAP_FLAGS_RAID_6_SUPPORT) { printf("%s RAID6", comma ? "," : ""); comma = 1; } if (ioc2->CapabilitiesFlags & MPI_IOCPAGE2_CAP_FLAGS_RAID_10_SUPPORT) { printf("%s RAID10", comma ? "," : ""); comma = 1; } if (ioc2->CapabilitiesFlags & MPI_IOCPAGE2_CAP_FLAGS_RAID_50_SUPPORT) { printf("%s RAID50", comma ? "," : ""); comma = 1; } if (!comma) printf(" none"); printf("\n"); free(ioc2); } else if ((IOCStatus & MPI_IOCSTATUS_MASK) != MPI_IOCSTATUS_CONFIG_INVALID_PAGE) warnx("mpt_read_ioc_page(2): %s", mpt_ioc_status(IOCStatus)); ioc6 = mpt_read_ioc_page(fd, 6, &IOCStatus); if (ioc6 != NULL) { display_stripe_map(" RAID0 Stripes", ioc6->SupportedStripeSizeMapIS); display_stripe_map(" RAID1E Stripes", ioc6->SupportedStripeSizeMapIME); printf(" RAID0 Drives/Vol: %u", ioc6->MinDrivesIS); if (ioc6->MinDrivesIS != ioc6->MaxDrivesIS) printf("-%u", ioc6->MaxDrivesIS); printf("\n"); printf(" RAID1 Drives/Vol: %u", ioc6->MinDrivesIM); if (ioc6->MinDrivesIM != ioc6->MaxDrivesIM) printf("-%u", ioc6->MaxDrivesIM); printf("\n"); printf("RAID1E Drives/Vol: %u", ioc6->MinDrivesIME); if (ioc6->MinDrivesIME != ioc6->MaxDrivesIME) printf("-%u", ioc6->MaxDrivesIME); printf("\n"); free(ioc6); } else if ((IOCStatus & MPI_IOCSTATUS_MASK) != MPI_IOCSTATUS_CONFIG_INVALID_PAGE) warnx("mpt_read_ioc_page(6): %s", mpt_ioc_status(IOCStatus)); /* TODO: Add an ioctl to fetch IOC_FACTS and print firmware version. */ close(fd); return (0); } MPT_COMMAND(show, adapter, show_adapter); static void print_vol(CONFIG_PAGE_RAID_VOL_0 *info, int state_len) { uint64_t size; const char *level, *state; char buf[6], stripe[5]; size = ((uint64_t)info->MaxLBAHigh << 32) | info->MaxLBA; humanize_number(buf, sizeof(buf), (size + 1) * 512, "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL); if (info->VolumeType == MPI_RAID_VOL_TYPE_IM) stripe[0] = '\0'; else format_stripe(stripe, sizeof(stripe), info->StripeSize); level = mpt_raid_level(info->VolumeType); state = mpt_volstate(info->VolumeStatus.State); if (state_len > 0) printf("(%6s) %-8s %6s %-*s", buf, level, stripe, state_len, state); else if (stripe[0] != '\0') printf("(%s) %s %s %s", buf, level, stripe, state); else printf("(%s) %s %s", buf, level, state); } static void print_pd(CONFIG_PAGE_RAID_PHYS_DISK_0 *info, int state_len, int location) { const char *inq, *state; char buf[6]; humanize_number(buf, sizeof(buf), ((uint64_t)info->MaxLBA + 1) * 512, "", HN_AUTOSCALE, HN_B | HN_NOSPACE |HN_DECIMAL); state = mpt_pdstate(info); if (state_len > 0) printf("(%6s) %-*s", buf, state_len, state); else printf("(%s) %s", buf, state); inq = mpt_pd_inq_string(info); if (inq != NULL) printf(" %s", inq); if (!location) return; printf(" bus %d id %d", info->PhysDiskBus, info->PhysDiskID); } static void print_standalone(struct mpt_standalone_disk *disk, int state_len, int location) { char buf[6]; humanize_number(buf, sizeof(buf), (disk->maxlba + 1) * 512, "", HN_AUTOSCALE, HN_B | HN_NOSPACE |HN_DECIMAL); if (state_len > 0) printf("(%6s) %-*s", buf, state_len, STANDALONE_STATE); else printf("(%s) %s", buf, STANDALONE_STATE); if (disk->inqstring[0] != '\0') printf(" %s", disk->inqstring); if (!location) return; printf(" bus %d id %d", disk->bus, disk->target); } static void print_spare_pools(U8 HotSparePool) { int i; if (HotSparePool == 0) { printf("none"); return; } for (i = 0; HotSparePool != 0; i++) { if (HotSparePool & 1) { printf("%d", i); if (HotSparePool == 1) break; printf(", "); } HotSparePool >>= 1; } } static int show_config(int ac, char **av) { CONFIG_PAGE_IOC_2 *ioc2; CONFIG_PAGE_IOC_2_RAID_VOL *vol; CONFIG_PAGE_IOC_5 *ioc5; IOC_5_HOT_SPARE *spare; CONFIG_PAGE_RAID_VOL_0 *vinfo; RAID_VOL0_PHYS_DISK *disk; CONFIG_PAGE_RAID_VOL_1 *vnames; CONFIG_PAGE_RAID_PHYS_DISK_0 *pinfo; struct mpt_standalone_disk *sdisks; int error, fd, i, j, nsdisks; if (ac != 1) { warnx("show config: extra arguments"); return (EINVAL); } fd = mpt_open(mpt_unit); if (fd < 0) { error = errno; warn("mpt_open"); return (error); } /* Get the config from the controller. */ ioc2 = mpt_read_ioc_page(fd, 2, NULL); ioc5 = mpt_read_ioc_page(fd, 5, NULL); if (ioc2 == NULL || ioc5 == NULL) { error = errno; warn("Failed to get config"); + free(ioc2); + close(fd); return (error); } if (mpt_fetch_disks(fd, &nsdisks, &sdisks) < 0) { error = errno; warn("Failed to get standalone drive list"); + free(ioc5); + free(ioc2); + close(fd); return (error); } /* Dump out the configuration. */ printf("mpt%d Configuration: %d volumes, %d drives\n", mpt_unit, ioc2->NumActiveVolumes, ioc2->NumActivePhysDisks + nsdisks); vol = ioc2->RaidVolume; for (i = 0; i < ioc2->NumActiveVolumes; vol++, i++) { printf(" volume %s ", mpt_volume_name(vol->VolumeBus, vol->VolumeID)); vinfo = mpt_vol_info(fd, vol->VolumeBus, vol->VolumeID, NULL); if (vinfo == NULL) { printf("%s UNKNOWN", mpt_raid_level(vol->VolumeType)); } else print_vol(vinfo, -1); vnames = mpt_vol_names(fd, vol->VolumeBus, vol->VolumeID, NULL); if (vnames != NULL) { if (vnames->Name[0] != '\0') printf(" <%s>", vnames->Name); free(vnames); } if (vinfo == NULL) { printf("\n"); continue; } printf(" spans:\n"); disk = vinfo->PhysDisk; for (j = 0; j < vinfo->NumPhysDisks; disk++, j++) { printf(" drive %u ", disk->PhysDiskNum); pinfo = mpt_pd_info(fd, disk->PhysDiskNum, NULL); if (pinfo != NULL) { print_pd(pinfo, -1, 0); free(pinfo); } printf("\n"); } if (vinfo->VolumeSettings.HotSparePool != 0) { printf(" spare pools: "); print_spare_pools(vinfo->VolumeSettings.HotSparePool); printf("\n"); } free(vinfo); } spare = ioc5->HotSpare; for (i = 0; i < ioc5->NumHotSpares; spare++, i++) { printf(" spare %u ", spare->PhysDiskNum); pinfo = mpt_pd_info(fd, spare->PhysDiskNum, NULL); if (pinfo != NULL) { print_pd(pinfo, -1, 0); free(pinfo); } printf(" backs pool %d\n", ffs(spare->HotSparePool) - 1); } for (i = 0; i < nsdisks; i++) { printf(" drive %s ", sdisks[i].devname); print_standalone(&sdisks[i], -1, 0); printf("\n"); } free(ioc2); free(ioc5); free(sdisks); close(fd); return (0); } MPT_COMMAND(show, config, show_config); static int show_volumes(int ac, char **av) { CONFIG_PAGE_IOC_2 *ioc2; CONFIG_PAGE_IOC_2_RAID_VOL *vol; CONFIG_PAGE_RAID_VOL_0 **volumes; CONFIG_PAGE_RAID_VOL_1 *vnames; int error, fd, i, len, state_len; if (ac != 1) { warnx("show volumes: extra arguments"); return (EINVAL); } fd = mpt_open(mpt_unit); if (fd < 0) { error = errno; warn("mpt_open"); return (error); } /* Get the volume list from the controller. */ ioc2 = mpt_read_ioc_page(fd, 2, NULL); if (ioc2 == NULL) { error = errno; warn("Failed to get volume list"); return (error); } /* * Go ahead and read the info for all the volumes and figure * out the maximum width of the state field. */ volumes = malloc(sizeof(*volumes) * ioc2->NumActiveVolumes); state_len = strlen("State"); vol = ioc2->RaidVolume; for (i = 0; i < ioc2->NumActiveVolumes; vol++, i++) { volumes[i] = mpt_vol_info(fd, vol->VolumeBus, vol->VolumeID, NULL); if (volumes[i] == NULL) len = strlen("UNKNOWN"); else len = strlen(mpt_volstate( volumes[i]->VolumeStatus.State)); if (len > state_len) state_len = len; } printf("mpt%d Volumes:\n", mpt_unit); printf(" Id Size Level Stripe "); len = state_len - strlen("State"); for (i = 0; i < (len + 1) / 2; i++) printf(" "); printf("State"); for (i = 0; i < len / 2; i++) printf(" "); printf(" Write-Cache Name\n"); vol = ioc2->RaidVolume; for (i = 0; i < ioc2->NumActiveVolumes; vol++, i++) { printf("%6s ", mpt_volume_name(vol->VolumeBus, vol->VolumeID)); if (volumes[i] != NULL) print_vol(volumes[i], state_len); else printf(" %-8s %-*s", mpt_raid_level(vol->VolumeType), state_len, "UNKNOWN"); if (volumes[i] != NULL) { if (volumes[i]->VolumeSettings.Settings & MPI_RAIDVOL0_SETTING_WRITE_CACHING_ENABLE) printf(" Enabled "); else printf(" Disabled "); } else printf(" "); free(volumes[i]); vnames = mpt_vol_names(fd, vol->VolumeBus, vol->VolumeID, NULL); if (vnames != NULL) { if (vnames->Name[0] != '\0') printf(" <%s>", vnames->Name); free(vnames); } printf("\n"); } + free(volumes); free(ioc2); close(fd); return (0); } MPT_COMMAND(show, volumes, show_volumes); static int show_drives(int ac, char **av) { struct mpt_drive_list *list; struct mpt_standalone_disk *sdisks; int error, fd, i, len, nsdisks, state_len; if (ac != 1) { warnx("show drives: extra arguments"); return (EINVAL); } fd = mpt_open(mpt_unit); if (fd < 0) { error = errno; warn("mpt_open"); return (error); } /* Get the drive list. */ list = mpt_pd_list(fd); if (list == NULL) { error = errno; + close(fd); warn("Failed to get drive list"); return (error); } /* Fetch the list of standalone disks for this controller. */ state_len = 0; if (mpt_fetch_disks(fd, &nsdisks, &sdisks) != 0) { nsdisks = 0; sdisks = NULL; } if (nsdisks != 0) state_len = strlen(STANDALONE_STATE); /* Walk the drive list to determine width of state column. */ for (i = 0; i < list->ndrives; i++) { len = strlen(mpt_pdstate(list->drives[i])); if (len > state_len) state_len = len; } /* List the drives. */ printf("mpt%d Physical Drives:\n", mpt_unit); for (i = 0; i < list->ndrives; i++) { printf("%4u ", list->drives[i]->PhysDiskNum); print_pd(list->drives[i], state_len, 1); printf("\n"); } mpt_free_pd_list(list); for (i = 0; i < nsdisks; i++) { printf("%4s ", sdisks[i].devname); print_standalone(&sdisks[i], state_len, 1); printf("\n"); } free(sdisks); close(fd); return (0); } MPT_COMMAND(show, drives, show_drives); #ifdef DEBUG static int show_physdisks(int ac, char **av) { CONFIG_PAGE_RAID_PHYS_DISK_0 *pinfo; U16 IOCStatus; int error, fd, i; if (ac != 1) { warnx("show drives: extra arguments"); return (EINVAL); } fd = mpt_open(mpt_unit); if (fd < 0) { error = errno; warn("mpt_open"); return (error); } /* Try to find each possible phys disk page. */ for (i = 0; i <= 0xff; i++) { pinfo = mpt_pd_info(fd, i, &IOCStatus); if (pinfo == NULL) { if ((IOCStatus & MPI_IOCSTATUS_MASK) != MPI_IOCSTATUS_CONFIG_INVALID_PAGE) warnx("mpt_pd_info(%d): %s", i, mpt_ioc_status(IOCStatus)); continue; } printf("%3u ", i); print_pd(pinfo, -1, 1); printf("\n"); } close(fd); return (0); } MPT_COMMAND(show, pd, show_physdisks); #endif Index: head/usr.sbin/mptutil/mpt_volume.c =================================================================== --- head/usr.sbin/mptutil/mpt_volume.c (revision 329844) +++ head/usr.sbin/mptutil/mpt_volume.c (revision 329845) @@ -1,260 +1,271 @@ /*- * SPDX-License-Identifier: BSD-3-Clause * * Copyright (c) 2008 Yahoo!, Inc. * All rights reserved. * Written by: John Baldwin * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the author nor the names of any co-contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include __RCSID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include #include "mptutil.h" MPT_TABLE(top, volume); const char * mpt_volstate(U8 State) { static char buf[16]; switch (State) { case MPI_RAIDVOL0_STATUS_STATE_OPTIMAL: return ("OPTIMAL"); case MPI_RAIDVOL0_STATUS_STATE_DEGRADED: return ("DEGRADED"); case MPI_RAIDVOL0_STATUS_STATE_FAILED: return ("FAILED"); case MPI_RAIDVOL0_STATUS_STATE_MISSING: return ("MISSING"); default: sprintf(buf, "VSTATE 0x%02x", State); return (buf); } } static int volume_name(int ac, char **av) { CONFIG_PAGE_RAID_VOL_1 *vnames; U8 VolumeBus, VolumeID; int error, fd; if (ac != 3) { warnx("name: volume and name required"); return (EINVAL); } if (strlen(av[2]) >= sizeof(vnames->Name)) { warnx("name: new name is too long"); return (ENOSPC); } fd = mpt_open(mpt_unit); if (fd < 0) { error = errno; warn("mpt_open"); return (error); } error = mpt_lookup_volume(fd, av[1], &VolumeBus, &VolumeID); if (error) { warnc(error, "Invalid volume: %s", av[1]); return (error); } vnames = mpt_vol_names(fd, VolumeBus, VolumeID, NULL); if (vnames == NULL) { error = errno; warn("Failed to fetch volume names"); + close(fd); return (error); } if (vnames->Header.PageType != MPI_CONFIG_PAGEATTR_CHANGEABLE) { warnx("Volume name is read only"); + free(vnames); + close(fd); return (EOPNOTSUPP); } printf("mpt%u changing volume %s name from \"%s\" to \"%s\"\n", mpt_unit, mpt_volume_name(VolumeBus, VolumeID), vnames->Name, av[2]); bzero(vnames->Name, sizeof(vnames->Name)); strcpy(vnames->Name, av[2]); if (mpt_write_config_page(fd, vnames, NULL) < 0) { error = errno; warn("Failed to set volume name"); + free(vnames); + close(fd); return (error); } free(vnames); close(fd); return (0); } MPT_COMMAND(top, name, volume_name); static int volume_status(int ac, char **av) { MPI_RAID_VOL_INDICATOR prog; RAID_VOL0_STATUS VolumeStatus; uint64_t total, remaining; float pct; U8 VolumeBus, VolumeID; int error, fd; if (ac != 2) { warnx("volume status: %s", ac > 2 ? "extra arguments" : "volume required"); return (EINVAL); } fd = mpt_open(mpt_unit); if (fd < 0) { error = errno; warn("mpt_open"); return (error); } error = mpt_lookup_volume(fd, av[1], &VolumeBus, &VolumeID); if (error) { warnc(error, "Invalid volume: %s", av[1]); + close(fd); return (error); } error = mpt_raid_action(fd, MPI_RAID_ACTION_INDICATOR_STRUCT, VolumeBus, VolumeID, 0, 0, NULL, 0, &VolumeStatus, (U32 *)&prog, sizeof(prog), NULL, NULL, 0); if (error) { warnc(error, "Fetching volume status failed"); + close(fd); return (error); } printf("Volume %s status:\n", mpt_volume_name(VolumeBus, VolumeID)); printf(" state: %s\n", mpt_volstate(VolumeStatus.State)); printf(" flags:"); if (VolumeStatus.Flags & MPI_RAIDVOL0_STATUS_FLAG_ENABLED) printf(" ENABLED"); else printf(" DISABLED"); if (VolumeStatus.Flags & MPI_RAIDVOL0_STATUS_FLAG_QUIESCED) printf(", QUIESCED"); if (VolumeStatus.Flags & MPI_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS) printf(", REBUILDING"); if (VolumeStatus.Flags & MPI_RAIDVOL0_STATUS_FLAG_VOLUME_INACTIVE) printf(", INACTIVE"); if (VolumeStatus.Flags & MPI_RAIDVOL0_STATUS_FLAG_BAD_BLOCK_TABLE_FULL) printf(", BAD BLOCK TABLE FULL"); printf("\n"); if (VolumeStatus.Flags & MPI_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS) { total = (uint64_t)prog.TotalBlocks.High << 32 | prog.TotalBlocks.Low; remaining = (uint64_t)prog.BlocksRemaining.High << 32 | prog.BlocksRemaining.Low; pct = (float)(total - remaining) * 100 / total; printf(" resync: %.2f%% complete\n", pct); } close(fd); return (0); } MPT_COMMAND(volume, status, volume_status); static int volume_cache(int ac, char **av) { CONFIG_PAGE_RAID_VOL_0 *volume; U32 Settings, NewSettings; U8 VolumeBus, VolumeID; char *s1; int error, fd; if (ac != 3) { warnx("volume cache: %s", ac > 3 ? "extra arguments" : "missing arguments"); return (EINVAL); } for (s1 = av[2]; *s1 != '\0'; s1++) *s1 = tolower(*s1); if ((strcmp(av[2], "enable")) && (strcmp(av[2], "enabled")) && (strcmp(av[2], "disable")) && (strcmp(av[2], "disabled"))) { warnx("volume cache: invalid flag, must be 'enable' or 'disable'\n"); return (EINVAL); } fd = mpt_open(mpt_unit); if (fd < 0) { error = errno; warn("mpt_open"); return (error); } error = mpt_lookup_volume(fd, av[1], &VolumeBus, &VolumeID); if (error) { warnc(error, "Invalid volume: %s", av[1]); + close(fd); return (error); } volume = mpt_vol_info(fd, VolumeBus, VolumeID, NULL); - if (volume == NULL) + if (volume == NULL) { + close(fd); return (errno); + } Settings = volume->VolumeSettings.Settings; NewSettings = Settings; if (strncmp(av[2], "enable", sizeof("enable")) == 0) NewSettings |= 0x01; if (strncmp(av[2], "disable", sizeof("disable")) == 0) NewSettings &= ~0x01; if (NewSettings == Settings) { warnx("volume cache unchanged"); + free(volume); close(fd); return (0); } volume->VolumeSettings.Settings = NewSettings; error = mpt_raid_action(fd, MPI_RAID_ACTION_CHANGE_VOLUME_SETTINGS, VolumeBus, VolumeID, 0, *(U32 *)&volume->VolumeSettings, NULL, 0, NULL, NULL, 0, NULL, NULL, 0); if (error) warnc(error, "volume cache change failed"); close(fd); return (error); } MPT_COMMAND(volume, cache, volume_cache);