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 bzero(sector, sizeof(sector));
155 nargs = gctl_get_int(req, "nargs");
157 gctl_error(req, "Too few arguments.");
160 if (bitcount32(nargs - 2) != 1) {
161 gctl_error(req, "Invalid number of components.");
165 strlcpy(md.md_magic, G_RAID3_MAGIC, sizeof(md.md_magic));
166 md.md_version = G_RAID3_VERSION;
167 str = gctl_get_ascii(req, "arg0");
168 strlcpy(md.md_name, str, sizeof(md.md_name));
169 md.md_id = arc4random();
170 md.md_all = nargs - 1;
175 md.md_sync_offset = 0;
176 if (gctl_get_int(req, "noautosync"))
177 md.md_mflags |= G_RAID3_DEVICE_FLAG_NOAUTOSYNC;
178 if (gctl_get_int(req, "nofailsync"))
179 md.md_mflags |= G_RAID3_DEVICE_FLAG_NOFAILSYNC;
180 round_robin = gctl_get_int(req, "round_robin");
182 md.md_mflags |= G_RAID3_DEVICE_FLAG_ROUND_ROBIN;
183 verify = gctl_get_int(req, "verify");
185 md.md_mflags |= G_RAID3_DEVICE_FLAG_VERIFY;
186 if (round_robin && verify) {
187 gctl_error(req, "Both '%c' and '%c' options given.", 'r', 'w');
190 hardcode = gctl_get_int(req, "hardcode");
193 * Calculate sectorsize by finding least common multiple from
194 * sectorsizes of every disk and find the smallest mediasize.
197 sectorsize = gctl_get_intmax(req, "sectorsize");
198 for (i = 1; i < nargs; i++) {
199 str = gctl_get_ascii(req, "arg%d", i);
200 msize = g_get_mediasize(str);
201 ssize = g_get_sectorsize(str);
202 if (msize == 0 || ssize == 0) {
203 gctl_error(req, "Can't get informations about %s: %s.",
204 str, strerror(errno));
208 if (mediasize == 0 || (mediasize > 0 && msize < mediasize))
213 sectorsize = g_lcm(sectorsize, ssize);
215 md.md_mediasize = mediasize * (nargs - 2);
216 md.md_sectorsize = sectorsize * (nargs - 2);
217 md.md_mediasize -= (md.md_mediasize % md.md_sectorsize);
219 if (md.md_sectorsize > MAXPHYS) {
220 gctl_error(req, "The blocksize is too big.");
225 * Clear last sector first, to spoil all components if device exists.
227 for (i = 1; i < nargs; i++) {
228 str = gctl_get_ascii(req, "arg%d", i);
229 error = g_metadata_clear(str, NULL);
231 gctl_error(req, "Can't store metadata on %s: %s.", str,
238 * Ok, store metadata (use disk number as priority).
240 for (i = 1; i < nargs; i++) {
241 str = gctl_get_ascii(req, "arg%d", i);
242 msize = g_get_mediasize(str);
243 ssize = g_get_sectorsize(str);
244 if (mediasize < msize - ssize) {
246 "warning: %s: only %jd bytes from %jd bytes used.\n",
247 str, (intmax_t)mediasize, (intmax_t)(msize - ssize));
251 md.md_provsize = msize;
253 bzero(md.md_provider, sizeof(md.md_provider));
255 if (strncmp(str, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
256 str += sizeof(_PATH_DEV) - 1;
257 strlcpy(md.md_provider, str, sizeof(md.md_provider));
259 if (verify && md.md_no == md.md_all - 1) {
261 * In "verify" mode, force synchronization of parity
262 * component on start.
266 raid3_metadata_encode(&md, sector);
267 error = g_metadata_store(str, sector, sizeof(sector));
269 fprintf(stderr, "Can't store metadata on %s: %s.\n",
270 str, strerror(error));
271 gctl_error(req, "Not fully done.");
275 printf("Metadata value stored on %s.\n", str);
280 raid3_clear(struct gctl_req *req)
285 nargs = gctl_get_int(req, "nargs");
287 gctl_error(req, "Too few arguments.");
291 for (i = 0; i < nargs; i++) {
292 name = gctl_get_ascii(req, "arg%d", i);
293 error = g_metadata_clear(name, G_RAID3_MAGIC);
295 fprintf(stderr, "Can't clear metadata on %s: %s.\n",
296 name, strerror(error));
297 gctl_error(req, "Not fully done.");
301 printf("Metadata cleared on %s.\n", name);
306 raid3_dump(struct gctl_req *req)
308 struct g_raid3_metadata md, tmpmd;
312 nargs = gctl_get_int(req, "nargs");
314 gctl_error(req, "Too few arguments.");
318 for (i = 0; i < nargs; i++) {
319 name = gctl_get_ascii(req, "arg%d", i);
320 error = g_metadata_read(name, (u_char *)&tmpmd, sizeof(tmpmd),
323 fprintf(stderr, "Can't read metadata from %s: %s.\n",
324 name, strerror(error));
325 gctl_error(req, "Not fully done.");
328 if (raid3_metadata_decode((u_char *)&tmpmd, &md) != 0) {
329 fprintf(stderr, "MD5 hash mismatch for %s, skipping.\n",
331 gctl_error(req, "Not fully done.");
334 printf("Metadata on %s:\n", name);
335 raid3_metadata_dump(&md);