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