1 /*- 2 * Copyright (c) 2008 Yahoo!, Inc. 3 * All rights reserved. 4 * Written by: John Baldwin <jhb@FreeBSD.org> 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. Neither the name of the author nor the names of any co-contributors 15 * may be used to endorse or promote products derived from this software 16 * without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 */ 30 31 #include <sys/cdefs.h> 32 __RCSID("$FreeBSD$"); 33 34 #include <sys/param.h> 35 #include <sys/errno.h> 36 #include <err.h> 37 #include <libutil.h> 38 #include <stdio.h> 39 #include <stdlib.h> 40 #include <string.h> 41 #include <unistd.h> 42 #include <ctype.h> 43 #include "mptutil.h" 44 45 MPT_TABLE(top, volume); 46 47 const char * 48 mpt_volstate(U8 State) 49 { 50 static char buf[16]; 51 52 switch (State) { 53 case MPI_RAIDVOL0_STATUS_STATE_OPTIMAL: 54 return ("OPTIMAL"); 55 case MPI_RAIDVOL0_STATUS_STATE_DEGRADED: 56 return ("DEGRADED"); 57 case MPI_RAIDVOL0_STATUS_STATE_FAILED: 58 return ("FAILED"); 59 case MPI_RAIDVOL0_STATUS_STATE_MISSING: 60 return ("MISSING"); 61 default: 62 sprintf(buf, "VSTATE 0x%02x", State); 63 return (buf); 64 } 65 } 66 67 static int 68 volume_name(int ac, char **av) 69 { 70 CONFIG_PAGE_RAID_VOL_1 *vnames; 71 U8 VolumeBus, VolumeID; 72 int error, fd; 73 74 if (ac != 3) { 75 warnx("name: volume and name required"); 76 return (EINVAL); 77 } 78 79 if (strlen(av[2]) >= sizeof(vnames->Name)) { 80 warnx("name: new name is too long"); 81 return (ENOSPC); 82 } 83 84 fd = mpt_open(mpt_unit); 85 if (fd < 0) { 86 error = errno; 87 warn("mpt_open"); 88 return (error); 89 } 90 91 error = mpt_lookup_volume(fd, av[1], &VolumeBus, &VolumeID); 92 if (error) { 93 warnc(error, "Invalid volume: %s", av[1]); 94 return (error); 95 } 96 97 vnames = mpt_vol_names(fd, VolumeBus, VolumeID, NULL); 98 if (vnames == NULL) { 99 error = errno; 100 warn("Failed to fetch volume names"); 101 return (error); 102 } 103 104 if (vnames->Header.PageType != MPI_CONFIG_PAGEATTR_CHANGEABLE) { 105 warnx("Volume name is read only"); 106 return (EOPNOTSUPP); 107 } 108 printf("mpt%u changing volume %s name from \"%s\" to \"%s\"\n", 109 mpt_unit, mpt_volume_name(VolumeBus, VolumeID), vnames->Name, 110 av[2]); 111 bzero(vnames->Name, sizeof(vnames->Name)); 112 strcpy(vnames->Name, av[2]); 113 114 if (mpt_write_config_page(fd, vnames, NULL) < 0) { 115 error = errno; 116 warn("Failed to set volume name"); 117 return (error); 118 } 119 120 free(vnames); 121 close(fd); 122 123 return (0); 124 } 125 MPT_COMMAND(top, name, volume_name); 126 127 static int 128 volume_status(int ac, char **av) 129 { 130 MPI_RAID_VOL_INDICATOR prog; 131 RAID_VOL0_STATUS VolumeStatus; 132 uint64_t total, remaining; 133 float pct; 134 U8 VolumeBus, VolumeID; 135 int error, fd; 136 137 if (ac != 2) { 138 warnx("volume status: %s", ac > 2 ? "extra arguments" : 139 "volume required"); 140 return (EINVAL); 141 } 142 143 fd = mpt_open(mpt_unit); 144 if (fd < 0) { 145 error = errno; 146 warn("mpt_open"); 147 return (error); 148 } 149 150 error = mpt_lookup_volume(fd, av[1], &VolumeBus, &VolumeID); 151 if (error) { 152 warnc(error, "Invalid volume: %s", av[1]); 153 return (error); 154 } 155 156 error = mpt_raid_action(fd, MPI_RAID_ACTION_INDICATOR_STRUCT, VolumeBus, 157 VolumeID, 0, 0, NULL, 0, &VolumeStatus, (U32 *)&prog, sizeof(prog), 158 NULL, NULL, 0); 159 if (error) { 160 warnc(error, "Fetching volume status failed"); 161 return (error); 162 } 163 164 printf("Volume %s status:\n", mpt_volume_name(VolumeBus, VolumeID)); 165 printf(" state: %s\n", mpt_volstate(VolumeStatus.State)); 166 printf(" flags:"); 167 if (VolumeStatus.Flags & MPI_RAIDVOL0_STATUS_FLAG_ENABLED) 168 printf(" ENABLED"); 169 else 170 printf(" DISABLED"); 171 if (VolumeStatus.Flags & MPI_RAIDVOL0_STATUS_FLAG_QUIESCED) 172 printf(", QUIESCED"); 173 if (VolumeStatus.Flags & MPI_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS) 174 printf(", REBUILDING"); 175 if (VolumeStatus.Flags & MPI_RAIDVOL0_STATUS_FLAG_VOLUME_INACTIVE) 176 printf(", INACTIVE"); 177 if (VolumeStatus.Flags & MPI_RAIDVOL0_STATUS_FLAG_BAD_BLOCK_TABLE_FULL) 178 printf(", BAD BLOCK TABLE FULL"); 179 printf("\n"); 180 if (VolumeStatus.Flags & MPI_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS) { 181 total = (uint64_t)prog.TotalBlocks.High << 32 | 182 prog.TotalBlocks.Low; 183 remaining = (uint64_t)prog.BlocksRemaining.High << 32 | 184 prog.BlocksRemaining.Low; 185 pct = (float)(total - remaining) * 100 / total; 186 printf(" resync: %.2f%% complete\n", pct); 187 } 188 189 close(fd); 190 return (0); 191 } 192 MPT_COMMAND(volume, status, volume_status); 193 194 static int 195 volume_cache(int ac, char **av) 196 { 197 CONFIG_PAGE_RAID_VOL_0 *volume; 198 U32 Settings, NewSettings; 199 U8 VolumeBus, VolumeID; 200 char *s1; 201 int error, fd; 202 203 if (ac != 3) { 204 warnx("volume cache: %s", ac > 3 ? "extra arguments" : 205 "missing arguments"); 206 return (EINVAL); 207 } 208 209 for (s1 = av[2]; *s1 != '\0'; s1++) 210 *s1 = tolower(*s1); 211 if ((strcmp(av[2], "enable")) && (strcmp(av[2], "enabled")) && 212 (strcmp(av[2], "disable")) && (strcmp(av[2], "disabled"))) { 213 warnx("volume cache: invalid flag, must be 'enable' or 'disable'\n"); 214 return (EINVAL); 215 } 216 217 fd = mpt_open(mpt_unit); 218 if (fd < 0) { 219 error = errno; 220 warn("mpt_open"); 221 return (error); 222 } 223 224 error = mpt_lookup_volume(fd, av[1], &VolumeBus, &VolumeID); 225 if (error) { 226 warnc(error, "Invalid volume: %s", av[1]); 227 return (error); 228 } 229 230 volume = mpt_vol_info(fd, VolumeBus, VolumeID, NULL); 231 if (volume == NULL) 232 return (errno); 233 234 Settings = volume->VolumeSettings.Settings; 235 236 NewSettings = Settings; 237 if (strncmp(av[2], "enable", sizeof("enable")) == 0) 238 NewSettings |= 0x01; 239 if (strncmp(av[2], "disable", sizeof("disable")) == 0) 240 NewSettings &= ~0x01; 241 242 if (NewSettings == Settings) { 243 warnx("volume cache unchanged"); 244 close(fd); 245 return (0); 246 } 247 248 volume->VolumeSettings.Settings = NewSettings; 249 error = mpt_raid_action(fd, MPI_RAID_ACTION_CHANGE_VOLUME_SETTINGS, 250 VolumeBus, VolumeID, 0, *(U32 *)&volume->VolumeSettings, NULL, 0, 251 NULL, NULL, 0, NULL, NULL, 0); 252 if (error) 253 warnc(error, "volume cache change failed"); 254 255 close(fd); 256 return (error); 257 } 258 MPT_COMMAND(volume, cache, volume_cache); 259