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/stripe/g_stripe.h>
44 #include "core/geom.h"
45 #include "misc/subr.h"
48 uint32_t lib_version = G_LIB_VERSION;
49 uint32_t version = G_STRIPE_VERSION;
51 #define GSTRIPE_STRIPESIZE "65536"
53 static void stripe_main(struct gctl_req *req, unsigned flags);
54 static void stripe_clear(struct gctl_req *req);
55 static void stripe_dump(struct gctl_req *req);
56 static void stripe_label(struct gctl_req *req);
58 struct g_command class_commands[] = {
59 { "clear", G_FLAG_VERBOSE, stripe_main, G_NULL_OPTS,
62 { "create", G_FLAG_VERBOSE | G_FLAG_LOADKLD, NULL,
64 { 's', "stripesize", GSTRIPE_STRIPESIZE, G_TYPE_NUMBER },
67 "[-v] [-s stripesize] name prov prov ..."
69 { "destroy", G_FLAG_VERBOSE, NULL,
71 { 'f', "force", NULL, G_TYPE_BOOL },
76 { "dump", 0, stripe_main, G_NULL_OPTS,
79 { "label", G_FLAG_VERBOSE | G_FLAG_LOADKLD, stripe_main,
81 { 'h', "hardcode", NULL, G_TYPE_BOOL },
82 { 's', "stripesize", GSTRIPE_STRIPESIZE, G_TYPE_NUMBER },
85 "[-hv] [-s stripesize] name prov prov ..."
87 { "stop", G_FLAG_VERBOSE, NULL,
89 { 'f', "force", NULL, G_TYPE_BOOL },
97 static int verbose = 0;
100 stripe_main(struct gctl_req *req, unsigned flags)
104 if ((flags & G_FLAG_VERBOSE) != 0)
107 name = gctl_get_ascii(req, "verb");
109 gctl_error(req, "No '%s' argument.", "verb");
112 if (strcmp(name, "label") == 0)
114 else if (strcmp(name, "clear") == 0)
116 else if (strcmp(name, "dump") == 0)
119 gctl_error(req, "Unknown command: %s.", name);
123 stripe_label(struct gctl_req *req)
125 struct g_stripe_metadata md;
127 off_t compsize, msize;
129 unsigned ssize, secsize;
131 int error, i, nargs, hardcode;
133 bzero(sector, sizeof(sector));
134 nargs = gctl_get_int(req, "nargs");
136 gctl_error(req, "Too few arguments.");
139 hardcode = gctl_get_int(req, "hardcode");
142 * Clear last sector first to spoil all components if device exists.
146 for (i = 1; i < nargs; i++) {
147 name = gctl_get_ascii(req, "arg%d", i);
148 msize = g_get_mediasize(name);
149 ssize = g_get_sectorsize(name);
150 if (msize == 0 || ssize == 0) {
151 gctl_error(req, "Can't get informations about %s: %s.",
152 name, strerror(errno));
156 if (compsize == 0 || (compsize > 0 && msize < compsize))
161 secsize = g_lcm(secsize, ssize);
163 error = g_metadata_clear(name, NULL);
165 gctl_error(req, "Can't store metadata on %s: %s.", name,
171 strlcpy(md.md_magic, G_STRIPE_MAGIC, sizeof(md.md_magic));
172 md.md_version = G_STRIPE_VERSION;
173 name = gctl_get_ascii(req, "arg0");
174 strlcpy(md.md_name, name, sizeof(md.md_name));
175 md.md_id = arc4random();
176 md.md_all = nargs - 1;
177 stripesize = gctl_get_intmax(req, "stripesize");
178 if ((stripesize % secsize) != 0) {
179 gctl_error(req, "Stripesize should be multiple of %u.",
183 md.md_stripesize = stripesize;
186 * Ok, store metadata.
188 for (i = 1; i < nargs; i++) {
189 name = gctl_get_ascii(req, "arg%d", i);
190 msize = g_get_mediasize(name);
191 ssize = g_get_sectorsize(name);
192 if (compsize < msize - ssize) {
194 "warning: %s: only %jd bytes from %jd bytes used.\n",
195 name, (intmax_t)compsize, (intmax_t)(msize - ssize));
199 md.md_provsize = msize;
201 bzero(md.md_provider, sizeof(md.md_provider));
203 if (strncmp(name, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
204 name += sizeof(_PATH_DEV) - 1;
205 strlcpy(md.md_provider, name, sizeof(md.md_provider));
207 stripe_metadata_encode(&md, sector);
208 error = g_metadata_store(name, sector, sizeof(sector));
210 fprintf(stderr, "Can't store metadata on %s: %s.\n",
211 name, strerror(error));
212 gctl_error(req, "Not fully done.");
216 printf("Metadata value stored on %s.\n", name);
221 stripe_clear(struct gctl_req *req)
226 nargs = gctl_get_int(req, "nargs");
228 gctl_error(req, "Too few arguments.");
232 for (i = 0; i < nargs; i++) {
233 name = gctl_get_ascii(req, "arg%d", i);
234 error = g_metadata_clear(name, G_STRIPE_MAGIC);
236 fprintf(stderr, "Can't clear metadata on %s: %s.\n",
237 name, strerror(error));
238 gctl_error(req, "Not fully done.");
242 printf("Metadata cleared on %s.\n", name);
247 stripe_metadata_dump(const struct g_stripe_metadata *md)
250 printf(" Magic string: %s\n", md->md_magic);
251 printf(" Metadata version: %u\n", (u_int)md->md_version);
252 printf(" Device name: %s\n", md->md_name);
253 printf(" Device ID: %u\n", (u_int)md->md_id);
254 printf(" Disk number: %u\n", (u_int)md->md_no);
255 printf("Total number of disks: %u\n", (u_int)md->md_all);
256 printf(" Stripe size: %u\n", (u_int)md->md_stripesize);
257 printf(" Hardcoded provider: %s\n", md->md_provider);
261 stripe_dump(struct gctl_req *req)
263 struct g_stripe_metadata md, tmpmd;
267 nargs = gctl_get_int(req, "nargs");
269 gctl_error(req, "Too few arguments.");
273 for (i = 0; i < nargs; i++) {
274 name = gctl_get_ascii(req, "arg%d", i);
275 error = g_metadata_read(name, (u_char *)&tmpmd, sizeof(tmpmd),
278 fprintf(stderr, "Can't read metadata from %s: %s.\n",
279 name, strerror(error));
280 gctl_error(req, "Not fully done.");
283 stripe_metadata_decode((u_char *)&tmpmd, &md);
284 printf("Metadata on %s:\n", name);
285 stripe_metadata_dump(&md);