toeplitz: Add comment.
[dragonfly.git] / usr.sbin / mptutil / mpt_volume.c
blobb75e56f0a47bdafec111a7c2e59c0a31434f863a
1 /*-
2 * Copyright (c) 2008 Yahoo!, Inc.
3 * All rights reserved.
4 * Written by: John Baldwin <jhb@FreeBSD.org>
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.
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.
30 * $FreeBSD: src/usr.sbin/mptutil/mpt_volume.c,v 1.2 2010/11/09 19:28:06 jhb Exp $
33 #include <sys/param.h>
34 #include <sys/errno.h>
35 #include <err.h>
36 #include <libutil.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <unistd.h>
41 #include <ctype.h>
42 #include "mptutil.h"
44 MPT_TABLE(top, volume);
46 const char *
47 mpt_volstate(U8 State)
49 static char buf[16];
51 switch (State) {
52 case MPI_RAIDVOL0_STATUS_STATE_OPTIMAL:
53 return ("OPTIMAL");
54 case MPI_RAIDVOL0_STATUS_STATE_DEGRADED:
55 return ("DEGRADED");
56 case MPI_RAIDVOL0_STATUS_STATE_FAILED:
57 return ("FAILED");
58 case MPI_RAIDVOL0_STATUS_STATE_MISSING:
59 return ("MISSING");
60 default:
61 sprintf(buf, "VSTATE 0x%02x", State);
62 return (buf);
66 static int
67 volume_name(int ac, char **av)
69 CONFIG_PAGE_RAID_VOL_1 *vnames;
70 U8 VolumeBus, VolumeID;
71 int error, fd;
73 if (ac != 3) {
74 warnx("name: volume and name required");
75 return (EINVAL);
78 if (strlen(av[2]) >= sizeof(vnames->Name)) {
79 warnx("name: new name is too long");
80 return (ENOSPC);
83 fd = mpt_open(mpt_unit);
84 if (fd < 0) {
85 error = errno;
86 warn("mpt_open");
87 return (error);
90 error = mpt_lookup_volume(fd, av[1], &VolumeBus, &VolumeID);
91 if (error) {
92 warnc(error, "Invalid volume: %s", av[1]);
93 return (error);
96 vnames = mpt_vol_names(fd, VolumeBus, VolumeID, NULL);
97 if (vnames == NULL) {
98 error = errno;
99 warn("Failed to fetch volume names");
100 return (error);
103 if (vnames->Header.PageType != MPI_CONFIG_PAGEATTR_CHANGEABLE) {
104 warnx("Volume name is read only");
105 return (EOPNOTSUPP);
107 printf("mpt%u changing volume %s name from \"%s\" to \"%s\"\n",
108 mpt_unit, mpt_volume_name(VolumeBus, VolumeID), vnames->Name,
109 av[2]);
110 bzero(vnames->Name, sizeof(vnames->Name));
111 strcpy(vnames->Name, av[2]);
113 if (mpt_write_config_page(fd, vnames, NULL) < 0) {
114 error = errno;
115 warn("Failed to set volume name");
116 return (error);
119 free(vnames);
120 close(fd);
122 return (0);
124 MPT_COMMAND(top, name, volume_name);
126 static int
127 volume_status(int ac, char **av)
129 MPI_RAID_VOL_INDICATOR prog;
130 RAID_VOL0_STATUS VolumeStatus;
131 uint64_t total, remaining;
132 float pct;
133 U8 VolumeBus, VolumeID;
134 int error, fd;
136 if (ac != 2) {
137 warnx("volume status: %s", ac > 2 ? "extra arguments" :
138 "volume required");
139 return (EINVAL);
142 fd = mpt_open(mpt_unit);
143 if (fd < 0) {
144 error = errno;
145 warn("mpt_open");
146 return (error);
149 error = mpt_lookup_volume(fd, av[1], &VolumeBus, &VolumeID);
150 if (error) {
151 warnc(error, "Invalid volume: %s", av[1]);
152 return (error);
155 error = mpt_raid_action(fd, MPI_RAID_ACTION_INDICATOR_STRUCT, VolumeBus,
156 VolumeID, 0, 0, NULL, 0, &VolumeStatus, (U32 *)&prog, sizeof(prog),
157 NULL, NULL, 0);
158 if (error) {
159 warnc(error, "Fetching volume status failed");
160 return (error);
163 printf("Volume %s status:\n", mpt_volume_name(VolumeBus, VolumeID));
164 printf(" state: %s\n", mpt_volstate(VolumeStatus.State));
165 printf(" flags:");
166 if (VolumeStatus.Flags & MPI_RAIDVOL0_STATUS_FLAG_ENABLED)
167 printf(" ENABLED");
168 else
169 printf(" DISABLED");
170 if (VolumeStatus.Flags & MPI_RAIDVOL0_STATUS_FLAG_QUIESCED)
171 printf(", QUIESCED");
172 if (VolumeStatus.Flags & MPI_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS)
173 printf(", REBUILDING");
174 if (VolumeStatus.Flags & MPI_RAIDVOL0_STATUS_FLAG_VOLUME_INACTIVE)
175 printf(", INACTIVE");
176 if (VolumeStatus.Flags & MPI_RAIDVOL0_STATUS_FLAG_BAD_BLOCK_TABLE_FULL)
177 printf(", BAD BLOCK TABLE FULL");
178 printf("\n");
179 if (VolumeStatus.Flags & MPI_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS) {
180 total = (uint64_t)prog.TotalBlocks.High << 32 |
181 prog.TotalBlocks.Low;
182 remaining = (uint64_t)prog.BlocksRemaining.High << 32 |
183 prog.BlocksRemaining.Low;
184 pct = (float)(total - remaining) * 100 / total;
185 printf(" resync: %.2f%% complete\n", pct);
188 close(fd);
189 return (0);
191 MPT_COMMAND(volume, status, volume_status);
193 static int
194 volume_cache(int ac, char **av)
196 CONFIG_PAGE_RAID_VOL_0 *volume;
197 U32 Settings, NewSettings;
198 U8 VolumeBus, VolumeID;
199 char *s1;
200 int error, fd;
202 if (ac != 3) {
203 warnx("volume cache: %s", ac > 3 ? "extra arguments" :
204 "missing arguments");
205 return (EINVAL);
208 for (s1 = av[2]; *s1 != '\0'; s1++)
209 *s1 = tolower(*s1);
210 if ((strcmp(av[2], "enable")) && (strcmp(av[2], "enabled")) &&
211 (strcmp(av[2], "disable")) && (strcmp(av[2], "disabled"))) {
212 warnx("volume cache: invalid flag, must be 'enable' or 'disable'\n");
213 return (EINVAL);
216 fd = mpt_open(mpt_unit);
217 if (fd < 0) {
218 error = errno;
219 warn("mpt_open");
220 return (error);
223 error = mpt_lookup_volume(fd, av[1], &VolumeBus, &VolumeID);
224 if (error) {
225 warnc(error, "Invalid volume: %s", av[1]);
226 return (error);
229 volume = mpt_vol_info(fd, VolumeBus, VolumeID, NULL);
230 if (volume == NULL)
231 return (errno);
233 Settings = volume->VolumeSettings.Settings;
235 NewSettings = Settings;
236 if (strncmp(av[2], "enable", sizeof("enable")) == 0)
237 NewSettings |= 0x01;
238 if (strncmp(av[2], "disable", sizeof("disable")) == 0)
239 NewSettings &= ~0x01;
241 if (NewSettings == Settings) {
242 warnx("volume cache unchanged");
243 close(fd);
244 return (0);
247 volume->VolumeSettings.Settings = NewSettings;
248 error = mpt_raid_action(fd, MPI_RAID_ACTION_CHANGE_VOLUME_SETTINGS,
249 VolumeBus, VolumeID, 0, *(U32 *)&volume->VolumeSettings, NULL, 0,
250 NULL, NULL, 0, NULL, NULL, 0);
251 if (error)
252 warnc(error, "volume cache change failed");
254 close(fd);
255 return (error);
257 MPT_COMMAND(volume, cache, volume_cache);