1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2004-2005 Pawel Jakub Dawidek <pjd@FreeBSD.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/param.h>
30 #include <errno.h>
31 #include <paths.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <stdint.h>
35 #include <string.h>
36 #include <strings.h>
37 #include <assert.h>
38 #include <libgeom.h>
39 #include <geom/raid3/g_raid3.h>
40 #include <core/geom.h>
41 #include <misc/subr.h>
42
43
44 uint32_t lib_version = G_LIB_VERSION;
45 uint32_t version = G_RAID3_VERSION;
46
47 static void raid3_main(struct gctl_req *req, unsigned f);
48 static void raid3_clear(struct gctl_req *req);
49 static void raid3_dump(struct gctl_req *req);
50 static void raid3_label(struct gctl_req *req);
51
52 struct g_command class_commands[] = {
53 { "clear", G_FLAG_VERBOSE, raid3_main, G_NULL_OPTS,
54 "[-v] prov ..."
55 },
56 { "configure", G_FLAG_VERBOSE, NULL,
57 {
58 { 'a', "autosync", NULL, G_TYPE_BOOL },
59 { 'd', "dynamic", NULL, G_TYPE_BOOL },
60 { 'f', "failsync", NULL, G_TYPE_BOOL },
61 { 'F', "nofailsync", NULL, G_TYPE_BOOL },
62 { 'h', "hardcode", NULL, G_TYPE_BOOL },
63 { 'n', "noautosync", NULL, G_TYPE_BOOL },
64 { 'r', "round_robin", NULL, G_TYPE_BOOL },
65 { 'R', "noround_robin", NULL, G_TYPE_BOOL },
66 { 'w', "verify", NULL, G_TYPE_BOOL },
67 { 'W', "noverify", NULL, G_TYPE_BOOL },
68 G_OPT_SENTINEL
69 },
70 "[-adfFhnrRvwW] name"
71 },
72 { "dump", 0, raid3_main, G_NULL_OPTS,
73 "prov ..."
74 },
75 { "insert", G_FLAG_VERBOSE, NULL,
76 {
77 { 'h', "hardcode", NULL, G_TYPE_BOOL },
78 { 'n', "number", G_VAL_OPTIONAL, G_TYPE_NUMBER },
79 G_OPT_SENTINEL
80 },
81 "[-hv] <-n number> name prov"
82 },
83 { "label", G_FLAG_VERBOSE, raid3_main,
84 {
85 { 'h', "hardcode", NULL, G_TYPE_BOOL },
86 { 'F', "nofailsync", NULL, G_TYPE_BOOL },
87 { 'n', "noautosync", NULL, G_TYPE_BOOL },
88 { 'r', "round_robin", NULL, G_TYPE_BOOL },
89 { 's', "sectorsize", "0", G_TYPE_NUMBER },
90 { 'w', "verify", NULL, G_TYPE_BOOL },
91 G_OPT_SENTINEL
92 },
93 "[-hFnrvw] [-s blocksize] name prov prov prov ..."
94 },
95 { "rebuild", G_FLAG_VERBOSE, NULL, G_NULL_OPTS,
96 "[-v] name prov"
97 },
98 { "remove", G_FLAG_VERBOSE, NULL,
99 {
100 { 'n', "number", NULL, G_TYPE_NUMBER },
101 G_OPT_SENTINEL
102 },
103 "[-v] <-n number> name"
104 },
105 { "stop", G_FLAG_VERBOSE, NULL,
106 {
107 { 'f', "force", NULL, G_TYPE_BOOL },
108 G_OPT_SENTINEL
109 },
110 "[-fv] name ..."
111 },
112 G_CMD_SENTINEL
113 };
114
115 static int verbose = 0;
116
117 static void
raid3_main(struct gctl_req * req,unsigned flags)118 raid3_main(struct gctl_req *req, unsigned flags)
119 {
120 const char *name;
121
122 if ((flags & G_FLAG_VERBOSE) != 0)
123 verbose = 1;
124
125 name = gctl_get_ascii(req, "verb");
126 if (name == NULL) {
127 gctl_error(req, "No '%s' argument.", "verb");
128 return;
129 }
130 if (strcmp(name, "label") == 0)
131 raid3_label(req);
132 else if (strcmp(name, "clear") == 0)
133 raid3_clear(req);
134 else if (strcmp(name, "dump") == 0)
135 raid3_dump(req);
136 else
137 gctl_error(req, "Unknown command: %s.", name);
138 }
139
140 static void
raid3_label(struct gctl_req * req)141 raid3_label(struct gctl_req *req)
142 {
143 struct g_raid3_metadata md;
144 u_char sector[512];
145 const char *str;
146 unsigned sectorsize, ssize;
147 off_t mediasize, msize;
148 int hardcode, round_robin, verify;
149 int error, i, nargs;
150
151 bzero(sector, sizeof(sector));
152 nargs = gctl_get_int(req, "nargs");
153 if (nargs < 4) {
154 gctl_error(req, "Too few arguments.");
155 return;
156 }
157 if (bitcount32(nargs - 2) != 1) {
158 gctl_error(req, "Invalid number of components.");
159 return;
160 }
161
162 strlcpy(md.md_magic, G_RAID3_MAGIC, sizeof(md.md_magic));
163 md.md_version = G_RAID3_VERSION;
164 str = gctl_get_ascii(req, "arg0");
165 strlcpy(md.md_name, str, sizeof(md.md_name));
166 md.md_id = arc4random();
167 md.md_all = nargs - 1;
168 md.md_mflags = 0;
169 md.md_dflags = 0;
170 md.md_genid = 0;
171 md.md_syncid = 1;
172 md.md_sync_offset = 0;
173 if (gctl_get_int(req, "noautosync"))
174 md.md_mflags |= G_RAID3_DEVICE_FLAG_NOAUTOSYNC;
175 if (gctl_get_int(req, "nofailsync"))
176 md.md_mflags |= G_RAID3_DEVICE_FLAG_NOFAILSYNC;
177 round_robin = gctl_get_int(req, "round_robin");
178 if (round_robin)
179 md.md_mflags |= G_RAID3_DEVICE_FLAG_ROUND_ROBIN;
180 verify = gctl_get_int(req, "verify");
181 if (verify)
182 md.md_mflags |= G_RAID3_DEVICE_FLAG_VERIFY;
183 if (round_robin && verify) {
184 gctl_error(req, "Both '%c' and '%c' options given.", 'r', 'w');
185 return;
186 }
187 hardcode = gctl_get_int(req, "hardcode");
188
189 /*
190 * Calculate sectorsize by finding least common multiple from
191 * sectorsizes of every disk and find the smallest mediasize.
192 */
193 mediasize = 0;
194 sectorsize = gctl_get_intmax(req, "sectorsize");
195 for (i = 1; i < nargs; i++) {
196 str = gctl_get_ascii(req, "arg%d", i);
197 msize = g_get_mediasize(str);
198 ssize = g_get_sectorsize(str);
199 if (msize == 0 || ssize == 0) {
200 gctl_error(req, "Can't get informations about %s: %s.",
201 str, strerror(errno));
202 return;
203 }
204 msize -= ssize;
205 if (mediasize == 0 || (mediasize > 0 && msize < mediasize))
206 mediasize = msize;
207 if (sectorsize == 0)
208 sectorsize = ssize;
209 else
210 sectorsize = g_lcm(sectorsize, ssize);
211 }
212 md.md_mediasize = mediasize * (nargs - 2);
213 md.md_sectorsize = sectorsize * (nargs - 2);
214 md.md_mediasize -= (md.md_mediasize % md.md_sectorsize);
215
216 if (md.md_sectorsize > MAXPHYS) {
217 gctl_error(req, "The blocksize is too big.");
218 return;
219 }
220
221 /*
222 * Clear last sector first, to spoil all components if device exists.
223 */
224 for (i = 1; i < nargs; i++) {
225 str = gctl_get_ascii(req, "arg%d", i);
226 error = g_metadata_clear(str, NULL);
227 if (error != 0) {
228 gctl_error(req, "Can't store metadata on %s: %s.", str,
229 strerror(error));
230 return;
231 }
232 }
233
234 /*
235 * Ok, store metadata (use disk number as priority).
236 */
237 for (i = 1; i < nargs; i++) {
238 str = gctl_get_ascii(req, "arg%d", i);
239 msize = g_get_mediasize(str);
240 ssize = g_get_sectorsize(str);
241 if (mediasize < msize - ssize) {
242 fprintf(stderr,
243 "warning: %s: only %jd bytes from %jd bytes used.\n",
244 str, (intmax_t)mediasize, (intmax_t)(msize - ssize));
245 }
246
247 md.md_no = i - 1;
248 md.md_provsize = msize;
249 if (!hardcode)
250 bzero(md.md_provider, sizeof(md.md_provider));
251 else {
252 if (strncmp(str, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
253 str += sizeof(_PATH_DEV) - 1;
254 strlcpy(md.md_provider, str, sizeof(md.md_provider));
255 }
256 if (verify && md.md_no == md.md_all - 1) {
257 /*
258 * In "verify" mode, force synchronization of parity
259 * component on start.
260 */
261 md.md_syncid = 0;
262 }
263 raid3_metadata_encode(&md, sector);
264 error = g_metadata_store(str, sector, sizeof(sector));
265 if (error != 0) {
266 fprintf(stderr, "Can't store metadata on %s: %s.\n",
267 str, strerror(error));
268 gctl_error(req, "Not fully done.");
269 continue;
270 }
271 if (verbose)
272 printf("Metadata value stored on %s.\n", str);
273 }
274 }
275
276 static void
raid3_clear(struct gctl_req * req)277 raid3_clear(struct gctl_req *req)
278 {
279 const char *name;
280 int error, i, nargs;
281
282 nargs = gctl_get_int(req, "nargs");
283 if (nargs < 1) {
284 gctl_error(req, "Too few arguments.");
285 return;
286 }
287
288 for (i = 0; i < nargs; i++) {
289 name = gctl_get_ascii(req, "arg%d", i);
290 error = g_metadata_clear(name, G_RAID3_MAGIC);
291 if (error != 0) {
292 fprintf(stderr, "Can't clear metadata on %s: %s.\n",
293 name, strerror(error));
294 gctl_error(req, "Not fully done.");
295 continue;
296 }
297 if (verbose)
298 printf("Metadata cleared on %s.\n", name);
299 }
300 }
301
302 static void
raid3_dump(struct gctl_req * req)303 raid3_dump(struct gctl_req *req)
304 {
305 struct g_raid3_metadata md, tmpmd;
306 const char *name;
307 int error, i, nargs;
308
309 nargs = gctl_get_int(req, "nargs");
310 if (nargs < 1) {
311 gctl_error(req, "Too few arguments.");
312 return;
313 }
314
315 for (i = 0; i < nargs; i++) {
316 name = gctl_get_ascii(req, "arg%d", i);
317 error = g_metadata_read(name, (u_char *)&tmpmd, sizeof(tmpmd),
318 G_RAID3_MAGIC);
319 if (error != 0) {
320 fprintf(stderr, "Can't read metadata from %s: %s.\n",
321 name, strerror(error));
322 gctl_error(req, "Not fully done.");
323 continue;
324 }
325 if (raid3_metadata_decode((u_char *)&tmpmd, &md) != 0) {
326 fprintf(stderr, "MD5 hash mismatch for %s, skipping.\n",
327 name);
328 gctl_error(req, "Not fully done.");
329 continue;
330 }
331 printf("Metadata on %s:\n", name);
332 raid3_metadata_dump(&md);
333 printf("\n");
334 }
335 }
336