2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2004-2009 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>
43 #include <geom/mirror/g_mirror.h>
44 #include <core/geom.h>
45 #include <misc/subr.h>
47 uint32_t lib_version = G_LIB_VERSION;
48 uint32_t version = G_MIRROR_VERSION;
50 #define GMIRROR_BALANCE "load"
51 #define GMIRROR_SLICE "4096"
52 #define GMIRROR_PRIORITY "0"
54 static void mirror_main(struct gctl_req *req, unsigned flags);
55 static void mirror_activate(struct gctl_req *req);
56 static void mirror_clear(struct gctl_req *req);
57 static void mirror_dump(struct gctl_req *req);
58 static void mirror_label(struct gctl_req *req);
59 static void mirror_resize(struct gctl_req *req, unsigned flags);
61 struct g_command class_commands[] = {
62 { "activate", G_FLAG_VERBOSE, mirror_main, G_NULL_OPTS,
65 { "clear", G_FLAG_VERBOSE, mirror_main, G_NULL_OPTS,
68 { "configure", G_FLAG_VERBOSE, NULL,
70 { 'a', "autosync", NULL, G_TYPE_BOOL },
71 { 'b', "balance", "", G_TYPE_STRING },
72 { 'd', "dynamic", NULL, G_TYPE_BOOL },
73 { 'f', "failsync", NULL, G_TYPE_BOOL },
74 { 'F', "nofailsync", NULL, G_TYPE_BOOL },
75 { 'h', "hardcode", NULL, G_TYPE_BOOL },
76 { 'n', "noautosync", NULL, G_TYPE_BOOL },
77 { 'p', "priority", "-1", G_TYPE_NUMBER },
78 { 's', "slice", "-1", G_TYPE_NUMBER },
81 "[-adfFhnv] [-b balance] [-s slice] name\n"
82 "[-v] -p priority name prov"
84 { "create", G_FLAG_VERBOSE, NULL,
86 { 'b', "balance", GMIRROR_BALANCE, G_TYPE_STRING },
87 { 'F', "nofailsync", NULL, G_TYPE_BOOL },
88 { 'n', "noautosync", NULL, G_TYPE_BOOL },
89 { 's', "slice", GMIRROR_SLICE, G_TYPE_NUMBER },
92 "[-Fnv] [-b balance] [-s slice] name prov ..."
94 { "deactivate", G_FLAG_VERBOSE, NULL, G_NULL_OPTS,
97 { "destroy", G_FLAG_VERBOSE, NULL,
99 { 'f', "force", NULL, G_TYPE_BOOL },
104 { "dump", 0, mirror_main, G_NULL_OPTS,
107 { "forget", G_FLAG_VERBOSE, NULL, G_NULL_OPTS,
110 { "label", G_FLAG_VERBOSE, mirror_main,
112 { 'b', "balance", GMIRROR_BALANCE, G_TYPE_STRING },
113 { 'F', "nofailsync", NULL, G_TYPE_BOOL },
114 { 'h', "hardcode", NULL, G_TYPE_BOOL },
115 { 'n', "noautosync", NULL, G_TYPE_BOOL },
116 { 's', "slice", GMIRROR_SLICE, G_TYPE_NUMBER },
119 "[-Fhnv] [-b balance] [-s slice] name prov ..."
121 { "insert", G_FLAG_VERBOSE, NULL,
123 { 'h', "hardcode", NULL, G_TYPE_BOOL },
124 { 'i', "inactive", NULL, G_TYPE_BOOL },
125 { 'p', "priority", GMIRROR_PRIORITY, G_TYPE_NUMBER },
128 "[-hiv] [-p priority] name prov ..."
130 { "rebuild", G_FLAG_VERBOSE, NULL, G_NULL_OPTS,
133 { "remove", G_FLAG_VERBOSE, NULL, G_NULL_OPTS,
136 { "resize", G_FLAG_VERBOSE, mirror_resize,
138 { 's', "size", "*", G_TYPE_STRING },
141 "[-s size] [-v] name"
143 { "stop", G_FLAG_VERBOSE, NULL,
145 { 'f', "force", NULL, G_TYPE_BOOL },
153 static int verbose = 0;
156 mirror_main(struct gctl_req *req, unsigned flags)
160 if ((flags & G_FLAG_VERBOSE) != 0)
163 name = gctl_get_ascii(req, "verb");
165 gctl_error(req, "No '%s' argument.", "verb");
168 if (strcmp(name, "label") == 0)
170 else if (strcmp(name, "clear") == 0)
172 else if (strcmp(name, "dump") == 0)
174 else if (strcmp(name, "activate") == 0)
175 mirror_activate(req);
177 gctl_error(req, "Unknown command: %s.", name);
181 mirror_label(struct gctl_req *req)
183 struct g_mirror_metadata md;
189 int error, i, nargs, bal, hardcode;
191 bzero(sector, sizeof(sector));
192 nargs = gctl_get_int(req, "nargs");
194 gctl_error(req, "Too few arguments.");
198 strlcpy(md.md_magic, G_MIRROR_MAGIC, sizeof(md.md_magic));
199 md.md_version = G_MIRROR_VERSION;
200 str = gctl_get_ascii(req, "arg0");
201 strlcpy(md.md_name, str, sizeof(md.md_name));
202 md.md_mid = arc4random();
203 md.md_all = nargs - 1;
208 md.md_sync_offset = 0;
209 val = gctl_get_intmax(req, "slice");
211 str = gctl_get_ascii(req, "balance");
212 bal = balance_id(str);
214 gctl_error(req, "Invalid balance algorithm.");
218 if (gctl_get_int(req, "noautosync"))
219 md.md_mflags |= G_MIRROR_DEVICE_FLAG_NOAUTOSYNC;
220 if (gctl_get_int(req, "nofailsync"))
221 md.md_mflags |= G_MIRROR_DEVICE_FLAG_NOFAILSYNC;
222 hardcode = gctl_get_int(req, "hardcode");
225 * Calculate sectorsize by finding least common multiple from
226 * sectorsizes of every disk and find the smallest mediasize.
230 for (i = 1; i < nargs; i++) {
234 str = gctl_get_ascii(req, "arg%d", i);
235 msize = g_get_mediasize(str);
236 ssize = g_get_sectorsize(str);
237 if (msize == 0 || ssize == 0) {
238 gctl_error(req, "Can't get informations about %s: %s.",
239 str, strerror(errno));
243 if (mediasize == 0 || (mediasize > 0 && msize < mediasize))
248 sectorsize = g_lcm(sectorsize, ssize);
250 md.md_mediasize = mediasize;
251 md.md_sectorsize = sectorsize;
252 md.md_mediasize -= (md.md_mediasize % md.md_sectorsize);
255 * Clear last sector first, to spoil all components if device exists.
257 for (i = 1; i < nargs; i++) {
258 str = gctl_get_ascii(req, "arg%d", i);
259 error = g_metadata_clear(str, NULL);
261 gctl_error(req, "Can't store metadata on %s: %s.", str,
268 * Ok, store metadata (use disk number as priority).
270 for (i = 1; i < nargs; i++) {
271 str = gctl_get_ascii(req, "arg%d", i);
272 md.md_did = arc4random();
273 md.md_priority = i - 1;
274 md.md_provsize = g_get_mediasize(str);
275 assert(md.md_provsize != 0);
277 bzero(md.md_provider, sizeof(md.md_provider));
279 if (strncmp(str, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
280 str += sizeof(_PATH_DEV) - 1;
281 strlcpy(md.md_provider, str, sizeof(md.md_provider));
283 mirror_metadata_encode(&md, sector);
284 error = g_metadata_store(str, sector, sizeof(sector));
286 fprintf(stderr, "Can't store metadata on %s: %s.\n",
287 str, strerror(error));
288 gctl_error(req, "Not fully done.");
292 printf("Metadata value stored on %s.\n", str);
297 mirror_clear(struct gctl_req *req)
302 nargs = gctl_get_int(req, "nargs");
304 gctl_error(req, "Too few arguments.");
308 for (i = 0; i < nargs; i++) {
309 name = gctl_get_ascii(req, "arg%d", i);
310 error = g_metadata_clear(name, G_MIRROR_MAGIC);
312 fprintf(stderr, "Can't clear metadata on %s: %s.\n",
313 name, strerror(error));
314 gctl_error(req, "Not fully done.");
318 printf("Metadata cleared on %s.\n", name);
323 mirror_dump(struct gctl_req *req)
325 struct g_mirror_metadata md, tmpmd;
329 nargs = gctl_get_int(req, "nargs");
331 gctl_error(req, "Too few arguments.");
335 for (i = 0; i < nargs; i++) {
336 name = gctl_get_ascii(req, "arg%d", i);
337 error = g_metadata_read(name, (u_char *)&tmpmd, sizeof(tmpmd),
340 fprintf(stderr, "Can't read metadata from %s: %s.\n",
341 name, strerror(error));
342 gctl_error(req, "Not fully done.");
345 if (mirror_metadata_decode((u_char *)&tmpmd, &md) != 0) {
346 fprintf(stderr, "MD5 hash mismatch for %s, skipping.\n",
348 gctl_error(req, "Not fully done.");
351 printf("Metadata on %s:\n", name);
352 mirror_metadata_dump(&md);
358 mirror_activate(struct gctl_req *req)
360 struct g_mirror_metadata md, tmpmd;
361 const char *name, *path;
364 nargs = gctl_get_int(req, "nargs");
366 gctl_error(req, "Too few arguments.");
369 name = gctl_get_ascii(req, "arg0");
371 for (i = 1; i < nargs; i++) {
372 path = gctl_get_ascii(req, "arg%d", i);
373 error = g_metadata_read(path, (u_char *)&tmpmd, sizeof(tmpmd),
376 fprintf(stderr, "Cannot read metadata from %s: %s.\n",
377 path, strerror(error));
378 gctl_error(req, "Not fully done.");
381 if (mirror_metadata_decode((u_char *)&tmpmd, &md) != 0) {
383 "MD5 hash mismatch for provider %s, skipping.\n",
385 gctl_error(req, "Not fully done.");
388 if (strcmp(md.md_name, name) != 0) {
390 "Provider %s is not the mirror %s component.\n",
392 gctl_error(req, "Not fully done.");
395 md.md_dflags &= ~G_MIRROR_DISK_FLAG_INACTIVE;
396 mirror_metadata_encode(&md, (u_char *)&tmpmd);
397 error = g_metadata_store(path, (u_char *)&tmpmd, sizeof(tmpmd));
399 fprintf(stderr, "Cannot write metadata from %s: %s.\n",
400 path, strerror(error));
401 gctl_error(req, "Not fully done.");
405 printf("Provider %s activated.\n", path);
409 static struct gclass *
410 find_class(struct gmesh *mesh, const char *name)
412 struct gclass *classp;
414 LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
415 if (strcmp(classp->lg_name, name) == 0)
421 static struct ggeom *
422 find_geom(struct gclass *classp, const char *name)
426 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
427 if (strcmp(gp->lg_name, name) == 0)
434 mirror_resize(struct gctl_req *req, unsigned flags __unused)
437 struct gclass *classp;
439 struct gprovider *pp;
440 struct gconsumer *cp;
443 const char *name, *g;
446 nargs = gctl_get_int(req, "nargs");
448 errx(EXIT_FAILURE, "Invalid number of arguments.");
449 name = gctl_get_ascii(req, "class");
452 g = gctl_get_ascii(req, "arg0");
455 error = geom_gettree_geom(&mesh, name, g, 1);
457 errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
458 classp = find_class(&mesh, name);
460 errx(EXIT_FAILURE, "Class %s not found.", name);
461 gp = find_geom(classp, g);
463 errx(EXIT_FAILURE, "No such geom: %s.", g);
464 pp = LIST_FIRST(&gp->lg_provider);
466 errx(EXIT_FAILURE, "Provider of geom %s not found.", g);
467 size = pp->lg_mediasize;
468 name = gctl_get_ascii(req, "size");
470 errx(EXIT_FAILURE, "The size is not specified.");
472 #define CSZ(c) ((c)->lg_provider->lg_mediasize - \
473 (c)->lg_provider->lg_sectorsize)
474 /* Find the maximum possible size */
475 LIST_FOREACH(cp, &gp->lg_consumer, lg_consumer) {
479 LIST_FOREACH(cp, &gp->lg_consumer, lg_consumer) {
484 if (size == pp->lg_mediasize)
486 "Cannot expand provider %s\n",
489 error = g_parse_lba(name, pp->lg_sectorsize, &size);
491 errc(EXIT_FAILURE, error, "Invalid size param");
492 size *= pp->lg_sectorsize;
494 snprintf(ssize, sizeof(ssize), "%ju", (uintmax_t)size);
495 gctl_change_param(req, "size", -1, ssize);
496 geom_deletetree(&mesh);