]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/mptutil/mpt_volume.c
Merge ACPICA 20180105.
[FreeBSD/FreeBSD.git] / usr.sbin / mptutil / mpt_volume.c
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/cdefs.h>
34 __RCSID("$FreeBSD$");
35
36 #include <sys/param.h>
37 #include <sys/errno.h>
38 #include <err.h>
39 #include <libutil.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <unistd.h>
44 #include <ctype.h>
45 #include "mptutil.h"
46
47 MPT_TABLE(top, volume);
48
49 const char *
50 mpt_volstate(U8 State)
51 {
52         static char buf[16];
53
54         switch (State) {
55         case MPI_RAIDVOL0_STATUS_STATE_OPTIMAL:
56                 return ("OPTIMAL");
57         case MPI_RAIDVOL0_STATUS_STATE_DEGRADED:
58                 return ("DEGRADED");
59         case MPI_RAIDVOL0_STATUS_STATE_FAILED:
60                 return ("FAILED");
61         case MPI_RAIDVOL0_STATUS_STATE_MISSING:
62                 return ("MISSING");
63         default:
64                 sprintf(buf, "VSTATE 0x%02x", State);
65                 return (buf);
66         }
67 }
68
69 static int
70 volume_name(int ac, char **av)
71 {
72         CONFIG_PAGE_RAID_VOL_1 *vnames;
73         U8 VolumeBus, VolumeID;
74         int error, fd;
75
76         if (ac != 3) {
77                 warnx("name: volume and name required");
78                 return (EINVAL);
79         }
80
81         if (strlen(av[2]) >= sizeof(vnames->Name)) {
82                 warnx("name: new name is too long");
83                 return (ENOSPC);
84         }
85
86         fd = mpt_open(mpt_unit);
87         if (fd < 0) {
88                 error = errno;
89                 warn("mpt_open");
90                 return (error);
91         }
92
93         error = mpt_lookup_volume(fd, av[1], &VolumeBus, &VolumeID);
94         if (error) {
95                 warnc(error, "Invalid volume: %s", av[1]);
96                 return (error);
97         }
98
99         vnames = mpt_vol_names(fd, VolumeBus, VolumeID, NULL);
100         if (vnames == NULL) {
101                 error = errno;
102                 warn("Failed to fetch volume names");
103                 return (error);
104         }
105
106         if (vnames->Header.PageType != MPI_CONFIG_PAGEATTR_CHANGEABLE) {
107                 warnx("Volume name is read only");
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                 return (error);
120         }
121
122         free(vnames);
123         close(fd);
124
125         return (0);
126 }
127 MPT_COMMAND(top, name, volume_name);
128
129 static int
130 volume_status(int ac, char **av)
131 {
132         MPI_RAID_VOL_INDICATOR prog;
133         RAID_VOL0_STATUS VolumeStatus;
134         uint64_t total, remaining;
135         float pct;
136         U8 VolumeBus, VolumeID;
137         int error, fd;
138
139         if (ac != 2) {
140                 warnx("volume status: %s", ac > 2 ? "extra arguments" :
141                     "volume required");
142                 return (EINVAL);
143         }
144
145         fd = mpt_open(mpt_unit);
146         if (fd < 0) {
147                 error = errno;
148                 warn("mpt_open");
149                 return (error);
150         }
151
152         error = mpt_lookup_volume(fd, av[1], &VolumeBus, &VolumeID);
153         if (error) {
154                 warnc(error, "Invalid volume: %s", av[1]);
155                 return (error);
156         }
157
158         error = mpt_raid_action(fd, MPI_RAID_ACTION_INDICATOR_STRUCT, VolumeBus,
159             VolumeID, 0, 0, NULL, 0, &VolumeStatus, (U32 *)&prog, sizeof(prog),
160             NULL, NULL, 0);
161         if (error) {
162                 warnc(error, "Fetching volume status failed");
163                 return (error);
164         }
165
166         printf("Volume %s status:\n", mpt_volume_name(VolumeBus, VolumeID));
167         printf("    state: %s\n", mpt_volstate(VolumeStatus.State));
168         printf("    flags:");
169         if (VolumeStatus.Flags & MPI_RAIDVOL0_STATUS_FLAG_ENABLED)
170                 printf(" ENABLED");
171         else
172                 printf(" DISABLED");
173         if (VolumeStatus.Flags & MPI_RAIDVOL0_STATUS_FLAG_QUIESCED)
174                 printf(", QUIESCED");
175         if (VolumeStatus.Flags & MPI_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS)
176                 printf(", REBUILDING");
177         if (VolumeStatus.Flags & MPI_RAIDVOL0_STATUS_FLAG_VOLUME_INACTIVE)
178                 printf(", INACTIVE");
179         if (VolumeStatus.Flags & MPI_RAIDVOL0_STATUS_FLAG_BAD_BLOCK_TABLE_FULL)
180                 printf(", BAD BLOCK TABLE FULL");
181         printf("\n");
182         if (VolumeStatus.Flags & MPI_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS) {
183                 total = (uint64_t)prog.TotalBlocks.High << 32 |
184                     prog.TotalBlocks.Low;
185                 remaining = (uint64_t)prog.BlocksRemaining.High << 32 |
186                     prog.BlocksRemaining.Low;
187                 pct = (float)(total - remaining) * 100 / total;
188                 printf("   resync: %.2f%% complete\n", pct);
189         }
190
191         close(fd);
192         return (0);
193 }
194 MPT_COMMAND(volume, status, volume_status);
195
196 static int
197 volume_cache(int ac, char **av)
198 {
199         CONFIG_PAGE_RAID_VOL_0 *volume;
200         U32 Settings, NewSettings;
201         U8 VolumeBus, VolumeID;
202         char *s1;
203         int error, fd;
204
205         if (ac != 3) {
206                 warnx("volume cache: %s", ac > 3 ? "extra arguments" :
207                     "missing arguments");
208                 return (EINVAL);
209         }
210
211         for (s1 = av[2]; *s1 != '\0'; s1++)
212                 *s1 = tolower(*s1);
213         if ((strcmp(av[2], "enable")) && (strcmp(av[2], "enabled")) &&
214             (strcmp(av[2], "disable")) && (strcmp(av[2], "disabled"))) {
215                 warnx("volume cache: invalid flag, must be 'enable' or 'disable'\n");
216                 return (EINVAL);
217         }
218
219         fd = mpt_open(mpt_unit);
220         if (fd < 0) {
221                 error = errno;
222                 warn("mpt_open");
223                 return (error);
224         }
225
226         error = mpt_lookup_volume(fd, av[1], &VolumeBus, &VolumeID);
227         if (error) {
228                 warnc(error, "Invalid volume: %s", av[1]);
229                 return (error);
230         }
231
232         volume = mpt_vol_info(fd, VolumeBus, VolumeID, NULL);
233         if (volume == NULL)
234                 return (errno);
235
236         Settings = volume->VolumeSettings.Settings;
237
238         NewSettings = Settings;
239         if (strncmp(av[2], "enable", sizeof("enable")) == 0)
240                 NewSettings |= 0x01;
241         if (strncmp(av[2], "disable", sizeof("disable")) == 0)
242                 NewSettings &= ~0x01;
243
244         if (NewSettings == Settings) {
245                 warnx("volume cache unchanged");
246                 close(fd);
247                 return (0);
248         }
249
250         volume->VolumeSettings.Settings = NewSettings;
251         error = mpt_raid_action(fd, MPI_RAID_ACTION_CHANGE_VOLUME_SETTINGS,
252             VolumeBus, VolumeID, 0, *(U32 *)&volume->VolumeSettings, NULL, 0,
253             NULL, NULL, 0, NULL, NULL, 0);
254         if (error)
255                 warnc(error, "volume cache change failed");
256
257         close(fd);
258         return (error);
259 }
260 MPT_COMMAND(volume, cache, volume_cache);