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