geom_stripe.c revision 142727
1/*-
2 * Copyright (c) 2004-2005 Pawel Jakub Dawidek <pjd@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 *
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
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/sbin/geom/class/stripe/geom_stripe.c 142727 2005-02-27 23:07:47Z pjd $");
29
30#include <sys/param.h>
31#include <errno.h>
32#include <paths.h>
33#include <stdio.h>
34#include <stdlib.h>
35#include <stdint.h>
36#include <string.h>
37#include <strings.h>
38#include <assert.h>
39#include <libgeom.h>
40#include <geom/stripe/g_stripe.h>
41
42#include "core/geom.h"
43#include "misc/subr.h"
44
45
46uint32_t lib_version = G_LIB_VERSION;
47uint32_t version = G_STRIPE_VERSION;
48
49static intmax_t stripesize = 4096;
50
51static void stripe_main(struct gctl_req *req, unsigned flags);
52static void stripe_clear(struct gctl_req *req);
53static void stripe_dump(struct gctl_req *req);
54static void stripe_label(struct gctl_req *req);
55
56struct g_command class_commands[] = {
57	{ "clear", G_FLAG_VERBOSE, stripe_main, G_NULL_OPTS },
58	{ "create", G_FLAG_VERBOSE | G_FLAG_LOADKLD, NULL,
59	    {
60		{ 's', "stripesize", &stripesize, G_TYPE_NUMBER },
61		G_OPT_SENTINEL
62	    }
63	},
64	{ "destroy", G_FLAG_VERBOSE, NULL,
65	    {
66		{ 'f', "force", NULL, G_TYPE_NONE },
67		G_OPT_SENTINEL
68	    }
69	},
70	{ "dump", 0, stripe_main, G_NULL_OPTS },
71	{ "label", G_FLAG_VERBOSE | G_FLAG_LOADKLD, stripe_main,
72	    {
73		{ 'h', "hardcode", NULL, G_TYPE_NONE },
74		{ 's', "stripesize", &stripesize, G_TYPE_NUMBER },
75		G_OPT_SENTINEL
76	    }
77	},
78	{ "stop", G_FLAG_VERBOSE, NULL,
79	    {
80		{ 'f', "force", NULL, G_TYPE_NONE },
81		G_OPT_SENTINEL
82	    }
83	},
84	G_CMD_SENTINEL
85};
86
87static int verbose = 0;
88
89void usage(const char *name);
90void
91usage(const char *name)
92{
93
94	fprintf(stderr, "usage: %s create [-hv] [-s stripesize] name prov prov ...\n", name);
95	fprintf(stderr, "       %s destroy [-fv] name ...\n", name);
96	fprintf(stderr, "       %s label [-hv] [-s stripesize] name prov prov ...\n", name);
97	fprintf(stderr, "       %s stop [-fv] name ...\n", name);
98	fprintf(stderr, "       %s clear [-v] prov ...\n", name);
99	fprintf(stderr, "       %s dump prov ...\n", name);
100}
101
102static void
103stripe_main(struct gctl_req *req, unsigned flags)
104{
105	const char *name;
106
107	if ((flags & G_FLAG_VERBOSE) != 0)
108		verbose = 1;
109
110	name = gctl_get_asciiparam(req, "verb");
111	if (name == NULL) {
112		gctl_error(req, "No '%s' argument.", "verb");
113		return;
114	}
115	if (strcmp(name, "label") == 0)
116		stripe_label(req);
117	else if (strcmp(name, "clear") == 0)
118		stripe_clear(req);
119	else if (strcmp(name, "dump") == 0)
120		stripe_dump(req);
121	else
122		gctl_error(req, "Unknown command: %s.", name);
123}
124
125static void
126stripe_label(struct gctl_req *req)
127{
128	struct g_stripe_metadata md;
129	intmax_t *stripesizep;
130	off_t compsize, msize;
131	u_char sector[512];
132	unsigned i, ssize, secsize;
133	const char *name;
134	char param[16];
135	int *hardcode, *nargs, error;
136
137	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
138	if (nargs == NULL) {
139		gctl_error(req, "No '%s' argument.", "nargs");
140		return;
141	}
142	if (*nargs <= 2) {
143		gctl_error(req, "Too few arguments.");
144		return;
145	}
146	hardcode = gctl_get_paraml(req, "hardcode", sizeof(*hardcode));
147	if (hardcode == NULL) {
148		gctl_error(req, "No '%s' argument.", "hardcode");
149		return;
150	}
151
152	/*
153	 * Clear last sector first to spoil all components if device exists.
154	 */
155	compsize = 0;
156	secsize = 0;
157	for (i = 1; i < (unsigned)*nargs; i++) {
158		snprintf(param, sizeof(param), "arg%u", i);
159		name = gctl_get_asciiparam(req, param);
160
161		msize = g_get_mediasize(name);
162		ssize = g_get_sectorsize(name);
163		if (msize == 0 || ssize == 0) {
164			gctl_error(req, "Can't get informations about %s: %s.",
165			    name, strerror(errno));
166			return;
167		}
168		msize -= ssize;
169		if (compsize == 0 || (compsize > 0 && msize < compsize))
170			compsize = msize;
171		if (secsize == 0)
172			secsize = ssize;
173		else
174			secsize = g_lcm(secsize, ssize);
175
176		error = g_metadata_clear(name, NULL);
177		if (error != 0) {
178			gctl_error(req, "Can't store metadata on %s: %s.", name,
179			    strerror(error));
180			return;
181		}
182	}
183
184	strlcpy(md.md_magic, G_STRIPE_MAGIC, sizeof(md.md_magic));
185	md.md_version = G_STRIPE_VERSION;
186	name = gctl_get_asciiparam(req, "arg0");
187	if (name == NULL) {
188		gctl_error(req, "No 'arg%u' argument.", 0);
189		return;
190	}
191	strlcpy(md.md_name, name, sizeof(md.md_name));
192	md.md_id = arc4random();
193	md.md_all = *nargs - 1;
194	stripesizep = gctl_get_paraml(req, "stripesize", sizeof(*stripesizep));
195	if (stripesizep == NULL) {
196		gctl_error(req, "No '%s' argument.", "stripesize");
197		return;
198	}
199	if ((*stripesizep % secsize) != 0) {
200		gctl_error(req, "Stripesize should be multiple of %u.",
201		    secsize);
202		return;
203	}
204	md.md_stripesize = *stripesizep;
205
206	/*
207	 * Ok, store metadata.
208	 */
209	for (i = 1; i < (unsigned)*nargs; i++) {
210		snprintf(param, sizeof(param), "arg%u", i);
211		name = gctl_get_asciiparam(req, param);
212
213		msize = g_get_mediasize(name);
214		ssize = g_get_sectorsize(name);
215		if (compsize < msize - ssize) {
216			fprintf(stderr,
217			    "warning: %s: only %jd bytes from %jd bytes used.\n",
218			    name, (intmax_t)compsize, (intmax_t)(msize - ssize));
219		}
220
221		md.md_no = i - 1;
222		md.md_provsize = msize;
223		if (!*hardcode)
224			bzero(md.md_provider, sizeof(md.md_provider));
225		else {
226			if (strncmp(name, _PATH_DEV, strlen(_PATH_DEV)) == 0)
227				name += strlen(_PATH_DEV);
228			strlcpy(md.md_provider, name, sizeof(md.md_provider));
229		}
230		stripe_metadata_encode(&md, sector);
231		error = g_metadata_store(name, sector, sizeof(sector));
232		if (error != 0) {
233			fprintf(stderr, "Can't store metadata on %s: %s.\n",
234			    name, strerror(error));
235			gctl_error(req, "Not fully done.");
236			continue;
237		}
238		if (verbose)
239			printf("Metadata value stored on %s.\n", name);
240	}
241}
242
243static void
244stripe_clear(struct gctl_req *req)
245{
246	const char *name;
247	char param[16];
248	unsigned i;
249	int *nargs, error;
250
251	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
252	if (nargs == NULL) {
253		gctl_error(req, "No '%s' argument.", "nargs");
254		return;
255	}
256	if (*nargs < 1) {
257		gctl_error(req, "Too few arguments.");
258		return;
259	}
260
261	for (i = 0; i < (unsigned)*nargs; i++) {
262		snprintf(param, sizeof(param), "arg%u", i);
263		name = gctl_get_asciiparam(req, param);
264
265		error = g_metadata_clear(name, G_STRIPE_MAGIC);
266		if (error != 0) {
267			fprintf(stderr, "Can't clear metadata on %s: %s.\n",
268			    name, strerror(error));
269			gctl_error(req, "Not fully done.");
270			continue;
271		}
272		if (verbose)
273			printf("Metadata cleared on %s.\n", name);
274	}
275}
276
277static void
278stripe_metadata_dump(const struct g_stripe_metadata *md)
279{
280
281	printf("         Magic string: %s\n", md->md_magic);
282	printf("     Metadata version: %u\n", (u_int)md->md_version);
283	printf("          Device name: %s\n", md->md_name);
284	printf("            Device ID: %u\n", (u_int)md->md_id);
285	printf("          Disk number: %u\n", (u_int)md->md_no);
286	printf("Total number of disks: %u\n", (u_int)md->md_all);
287	printf("          Stripe size: %u\n", (u_int)md->md_stripesize);
288	printf("   Hardcoded provider: %s\n", md->md_provider);
289}
290
291static void
292stripe_dump(struct gctl_req *req)
293{
294	struct g_stripe_metadata md, tmpmd;
295	const char *name;
296	char param[16];
297	int *nargs, error, i;
298
299	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
300	if (nargs == NULL) {
301		gctl_error(req, "No '%s' argument.", "nargs");
302		return;
303	}
304	if (*nargs < 1) {
305		gctl_error(req, "Too few arguments.");
306		return;
307	}
308
309	for (i = 0; i < *nargs; i++) {
310		snprintf(param, sizeof(param), "arg%u", i);
311		name = gctl_get_asciiparam(req, param);
312
313		error = g_metadata_read(name, (u_char *)&tmpmd, sizeof(tmpmd),
314		    G_STRIPE_MAGIC);
315		if (error != 0) {
316			fprintf(stderr, "Can't read metadata from %s: %s.\n",
317			    name, strerror(error));
318			gctl_error(req, "Not fully done.");
319			continue;
320		}
321		stripe_metadata_decode((u_char *)&tmpmd, &md);
322		printf("Metadata on %s:\n", name);
323		stripe_metadata_dump(&md);
324		printf("\n");
325	}
326}
327