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