2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2004-2005 Pawel Jakub Dawidek <pjd@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
42 #include <geom/raid3/g_raid3.h>
43 #include <core/geom.h>
44 #include <misc/subr.h>
47 uint32_t lib_version = G_LIB_VERSION;
48 uint32_t version = G_RAID3_VERSION;
50 static void raid3_main(struct gctl_req *req, unsigned f);
51 static void raid3_clear(struct gctl_req *req);
52 static void raid3_dump(struct gctl_req *req);
53 static void raid3_label(struct gctl_req *req);
55 struct g_command class_commands[] = {
56 { "clear", G_FLAG_VERBOSE, raid3_main, G_NULL_OPTS,
59 { "configure", G_FLAG_VERBOSE, NULL,
61 { 'a', "autosync", NULL, G_TYPE_BOOL },
62 { 'd', "dynamic", NULL, G_TYPE_BOOL },
63 { 'f', "failsync", NULL, G_TYPE_BOOL },
64 { 'F', "nofailsync", NULL, G_TYPE_BOOL },
65 { 'h', "hardcode", NULL, G_TYPE_BOOL },
66 { 'n', "noautosync", NULL, G_TYPE_BOOL },
67 { 'r', "round_robin", NULL, G_TYPE_BOOL },
68 { 'R', "noround_robin", NULL, G_TYPE_BOOL },
69 { 'w', "verify", NULL, G_TYPE_BOOL },
70 { 'W', "noverify", NULL, G_TYPE_BOOL },
75 { "dump", 0, raid3_main, G_NULL_OPTS,
78 { "insert", G_FLAG_VERBOSE, NULL,
80 { 'h', "hardcode", NULL, G_TYPE_BOOL },
81 { 'n', "number", G_VAL_OPTIONAL, G_TYPE_NUMBER },
84 "[-hv] <-n number> name prov"
86 { "label", G_FLAG_VERBOSE, raid3_main,
88 { 'h', "hardcode", NULL, G_TYPE_BOOL },
89 { 'F', "nofailsync", NULL, G_TYPE_BOOL },
90 { 'n', "noautosync", NULL, G_TYPE_BOOL },
91 { 'r', "round_robin", NULL, G_TYPE_BOOL },
92 { 's', "sectorsize", "0", G_TYPE_NUMBER },
93 { 'w', "verify", NULL, G_TYPE_BOOL },
96 "[-hFnrvw] [-s blocksize] name prov prov prov ..."
98 { "rebuild", G_FLAG_VERBOSE, NULL, G_NULL_OPTS,
101 { "remove", G_FLAG_VERBOSE, NULL,
103 { 'n', "number", NULL, G_TYPE_NUMBER },
106 "[-v] <-n number> name"
108 { "stop", G_FLAG_VERBOSE, NULL,
110 { 'f', "force", NULL, G_TYPE_BOOL },
118 static int verbose = 0;
121 raid3_main(struct gctl_req *req, unsigned flags)
125 if ((flags & G_FLAG_VERBOSE) != 0)
128 name = gctl_get_ascii(req, "verb");
130 gctl_error(req, "No '%s' argument.", "verb");
133 if (strcmp(name, "label") == 0)
135 else if (strcmp(name, "clear") == 0)
137 else if (strcmp(name, "dump") == 0)
140 gctl_error(req, "Unknown command: %s.", name);
144 raid3_label(struct gctl_req *req)
146 struct g_raid3_metadata md;
149 unsigned sectorsize, ssize;
150 off_t mediasize, msize;
151 int hardcode, round_robin, verify;
154 nargs = gctl_get_int(req, "nargs");
156 gctl_error(req, "Too few arguments.");
159 if (bitcount32(nargs - 2) != 1) {
160 gctl_error(req, "Invalid number of components.");
164 strlcpy(md.md_magic, G_RAID3_MAGIC, sizeof(md.md_magic));
165 md.md_version = G_RAID3_VERSION;
166 str = gctl_get_ascii(req, "arg0");
167 strlcpy(md.md_name, str, sizeof(md.md_name));
168 md.md_id = arc4random();
169 md.md_all = nargs - 1;
174 md.md_sync_offset = 0;
175 if (gctl_get_int(req, "noautosync"))
176 md.md_mflags |= G_RAID3_DEVICE_FLAG_NOAUTOSYNC;
177 if (gctl_get_int(req, "nofailsync"))
178 md.md_mflags |= G_RAID3_DEVICE_FLAG_NOFAILSYNC;
179 round_robin = gctl_get_int(req, "round_robin");
181 md.md_mflags |= G_RAID3_DEVICE_FLAG_ROUND_ROBIN;
182 verify = gctl_get_int(req, "verify");
184 md.md_mflags |= G_RAID3_DEVICE_FLAG_VERIFY;
185 if (round_robin && verify) {
186 gctl_error(req, "Both '%c' and '%c' options given.", 'r', 'w');
189 hardcode = gctl_get_int(req, "hardcode");
192 * Calculate sectorsize by finding least common multiple from
193 * sectorsizes of every disk and find the smallest mediasize.
196 sectorsize = gctl_get_intmax(req, "sectorsize");
197 for (i = 1; i < nargs; i++) {
198 str = gctl_get_ascii(req, "arg%d", i);
199 msize = g_get_mediasize(str);
200 ssize = g_get_sectorsize(str);
201 if (msize == 0 || ssize == 0) {
202 gctl_error(req, "Can't get informations about %s: %s.",
203 str, strerror(errno));
207 if (mediasize == 0 || (mediasize > 0 && msize < mediasize))
212 sectorsize = g_lcm(sectorsize, ssize);
214 md.md_mediasize = mediasize * (nargs - 2);
215 md.md_sectorsize = sectorsize * (nargs - 2);
216 md.md_mediasize -= (md.md_mediasize % md.md_sectorsize);
218 if (md.md_sectorsize > MAXPHYS) {
219 gctl_error(req, "The blocksize is too big.");
224 * Clear last sector first, to spoil all components if device exists.
226 for (i = 1; i < nargs; i++) {
227 str = gctl_get_ascii(req, "arg%d", i);
228 error = g_metadata_clear(str, NULL);
230 gctl_error(req, "Can't store metadata on %s: %s.", str,
237 * Ok, store metadata (use disk number as priority).
239 for (i = 1; i < nargs; i++) {
240 str = gctl_get_ascii(req, "arg%d", i);
241 msize = g_get_mediasize(str);
242 ssize = g_get_sectorsize(str);
243 if (mediasize < msize - ssize) {
245 "warning: %s: only %jd bytes from %jd bytes used.\n",
246 str, (intmax_t)mediasize, (intmax_t)(msize - ssize));
250 md.md_provsize = msize;
252 bzero(md.md_provider, sizeof(md.md_provider));
254 if (strncmp(str, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
255 str += sizeof(_PATH_DEV) - 1;
256 strlcpy(md.md_provider, str, sizeof(md.md_provider));
258 if (verify && md.md_no == md.md_all - 1) {
260 * In "verify" mode, force synchronization of parity
261 * component on start.
265 raid3_metadata_encode(&md, sector);
266 error = g_metadata_store(str, sector, sizeof(sector));
268 fprintf(stderr, "Can't store metadata on %s: %s.\n",
269 str, strerror(error));
270 gctl_error(req, "Not fully done.");
274 printf("Metadata value stored on %s.\n", str);
279 raid3_clear(struct gctl_req *req)
284 nargs = gctl_get_int(req, "nargs");
286 gctl_error(req, "Too few arguments.");
290 for (i = 0; i < nargs; i++) {
291 name = gctl_get_ascii(req, "arg%d", i);
292 error = g_metadata_clear(name, G_RAID3_MAGIC);
294 fprintf(stderr, "Can't clear metadata on %s: %s.\n",
295 name, strerror(error));
296 gctl_error(req, "Not fully done.");
300 printf("Metadata cleared on %s.\n", name);
305 raid3_dump(struct gctl_req *req)
307 struct g_raid3_metadata md, tmpmd;
311 nargs = gctl_get_int(req, "nargs");
313 gctl_error(req, "Too few arguments.");
317 for (i = 0; i < nargs; i++) {
318 name = gctl_get_ascii(req, "arg%d", i);
319 error = g_metadata_read(name, (u_char *)&tmpmd, sizeof(tmpmd),
322 fprintf(stderr, "Can't read metadata from %s: %s.\n",
323 name, strerror(error));
324 gctl_error(req, "Not fully done.");
327 if (raid3_metadata_decode((u_char *)&tmpmd, &md) != 0) {
328 fprintf(stderr, "MD5 hash mismatch for %s, skipping.\n",
330 gctl_error(req, "Not fully done.");
333 printf("Metadata on %s:\n", name);
334 raid3_metadata_dump(&md);