2 * Copyright (c) 2004-2005 Pawel Jakub Dawidek <pjd@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
40 #include <geom/stripe/g_stripe.h>
42 #include "core/geom.h"
43 #include "misc/subr.h"
46 uint32_t lib_version = G_LIB_VERSION;
47 uint32_t version = G_STRIPE_VERSION;
49 static intmax_t default_stripesize = 4096;
51 static void stripe_main(struct gctl_req *req, unsigned flags);
52 static void stripe_clear(struct gctl_req *req);
53 static void stripe_dump(struct gctl_req *req);
54 static void stripe_label(struct gctl_req *req);
56 struct g_command class_commands[] = {
57 { "clear", G_FLAG_VERBOSE, stripe_main, G_NULL_OPTS, NULL,
60 { "create", G_FLAG_VERBOSE | G_FLAG_LOADKLD, NULL,
62 { 's', "stripesize", &default_stripesize, G_TYPE_NUMBER },
65 NULL, "[-hv] [-s stripesize] name prov prov ..."
67 { "destroy", G_FLAG_VERBOSE, NULL,
69 { 'f', "force", NULL, G_TYPE_BOOL },
72 NULL, "[-fv] name ..."
74 { "dump", 0, stripe_main, G_NULL_OPTS, NULL,
77 { "label", G_FLAG_VERBOSE | G_FLAG_LOADKLD, stripe_main,
79 { 'h', "hardcode", NULL, G_TYPE_BOOL },
80 { 's', "stripesize", &default_stripesize, G_TYPE_NUMBER },
83 NULL, "[-hv] [-s stripesize] name prov prov ..."
85 { "stop", G_FLAG_VERBOSE, NULL,
87 { 'f', "force", NULL, G_TYPE_BOOL },
90 NULL, "[-fv] name ..."
95 static int verbose = 0;
98 stripe_main(struct gctl_req *req, unsigned flags)
102 if ((flags & G_FLAG_VERBOSE) != 0)
105 name = gctl_get_ascii(req, "verb");
107 gctl_error(req, "No '%s' argument.", "verb");
110 if (strcmp(name, "label") == 0)
112 else if (strcmp(name, "clear") == 0)
114 else if (strcmp(name, "dump") == 0)
117 gctl_error(req, "Unknown command: %s.", name);
121 stripe_label(struct gctl_req *req)
123 struct g_stripe_metadata md;
125 off_t compsize, msize;
127 unsigned ssize, secsize;
129 int error, i, nargs, hardcode;
131 nargs = gctl_get_int(req, "nargs");
133 gctl_error(req, "Too few arguments.");
136 hardcode = gctl_get_int(req, "hardcode");
139 * Clear last sector first to spoil all components if device exists.
143 for (i = 1; i < nargs; i++) {
144 name = gctl_get_ascii(req, "arg%d", i);
145 msize = g_get_mediasize(name);
146 ssize = g_get_sectorsize(name);
147 if (msize == 0 || ssize == 0) {
148 gctl_error(req, "Can't get informations about %s: %s.",
149 name, strerror(errno));
153 if (compsize == 0 || (compsize > 0 && msize < compsize))
158 secsize = g_lcm(secsize, ssize);
160 error = g_metadata_clear(name, NULL);
162 gctl_error(req, "Can't store metadata on %s: %s.", name,
168 strlcpy(md.md_magic, G_STRIPE_MAGIC, sizeof(md.md_magic));
169 md.md_version = G_STRIPE_VERSION;
170 name = gctl_get_ascii(req, "arg0");
171 strlcpy(md.md_name, name, sizeof(md.md_name));
172 md.md_id = arc4random();
173 md.md_all = nargs - 1;
174 stripesize = gctl_get_intmax(req, "stripesize");
175 if ((stripesize % secsize) != 0) {
176 gctl_error(req, "Stripesize should be multiple of %u.",
180 md.md_stripesize = stripesize;
183 * Ok, store metadata.
185 for (i = 1; i < nargs; i++) {
186 name = gctl_get_ascii(req, "arg%d", i);
187 msize = g_get_mediasize(name);
188 ssize = g_get_sectorsize(name);
189 if (compsize < msize - ssize) {
191 "warning: %s: only %jd bytes from %jd bytes used.\n",
192 name, (intmax_t)compsize, (intmax_t)(msize - ssize));
196 md.md_provsize = msize;
198 bzero(md.md_provider, sizeof(md.md_provider));
200 if (strncmp(name, _PATH_DEV, strlen(_PATH_DEV)) == 0)
201 name += strlen(_PATH_DEV);
202 strlcpy(md.md_provider, name, sizeof(md.md_provider));
204 stripe_metadata_encode(&md, sector);
205 error = g_metadata_store(name, sector, sizeof(sector));
207 fprintf(stderr, "Can't store metadata on %s: %s.\n",
208 name, strerror(error));
209 gctl_error(req, "Not fully done.");
213 printf("Metadata value stored on %s.\n", name);
218 stripe_clear(struct gctl_req *req)
223 nargs = gctl_get_int(req, "nargs");
225 gctl_error(req, "Too few arguments.");
229 for (i = 0; i < nargs; i++) {
230 name = gctl_get_ascii(req, "arg%d", i);
231 error = g_metadata_clear(name, G_STRIPE_MAGIC);
233 fprintf(stderr, "Can't clear metadata on %s: %s.\n",
234 name, strerror(error));
235 gctl_error(req, "Not fully done.");
239 printf("Metadata cleared on %s.\n", name);
244 stripe_metadata_dump(const struct g_stripe_metadata *md)
247 printf(" Magic string: %s\n", md->md_magic);
248 printf(" Metadata version: %u\n", (u_int)md->md_version);
249 printf(" Device name: %s\n", md->md_name);
250 printf(" Device ID: %u\n", (u_int)md->md_id);
251 printf(" Disk number: %u\n", (u_int)md->md_no);
252 printf("Total number of disks: %u\n", (u_int)md->md_all);
253 printf(" Stripe size: %u\n", (u_int)md->md_stripesize);
254 printf(" Hardcoded provider: %s\n", md->md_provider);
258 stripe_dump(struct gctl_req *req)
260 struct g_stripe_metadata md, tmpmd;
264 nargs = gctl_get_int(req, "nargs");
266 gctl_error(req, "Too few arguments.");
270 for (i = 0; i < nargs; i++) {
271 name = gctl_get_ascii(req, "arg%d", i);
272 error = g_metadata_read(name, (u_char *)&tmpmd, sizeof(tmpmd),
275 fprintf(stderr, "Can't read metadata from %s: %s.\n",
276 name, strerror(error));
277 gctl_error(req, "Not fully done.");
280 stripe_metadata_decode((u_char *)&tmpmd, &md);
281 printf("Metadata on %s:\n", name);
282 stripe_metadata_dump(&md);