geom_raid3.c revision 135370
1/*-
2 * Copyright (c) 2004 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/raid3/geom_raid3.c 135370 2004-09-17 08:41:15Z 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/raid3/g_raid3.h>
41#include <core/geom.h>
42#include <misc/subr.h>
43
44
45uint32_t lib_version = G_LIB_VERSION;
46uint32_t version = G_RAID3_VERSION;
47
48static void raid3_main(struct gctl_req *req, unsigned f);
49static void raid3_clear(struct gctl_req *req);
50static void raid3_dump(struct gctl_req *req);
51static void raid3_label(struct gctl_req *req);
52
53struct g_command class_commands[] = {
54	{ "clear", G_FLAG_VERBOSE, raid3_main, G_NULL_OPTS },
55	{ "configure", G_FLAG_VERBOSE, NULL,
56	    {
57		{ 'a', "autosync", NULL, G_TYPE_NONE },
58		{ 'd', "dynamic", NULL, G_TYPE_NONE },
59		{ 'h', "hardcode", NULL, G_TYPE_NONE },
60		{ 'n', "noautosync", NULL, G_TYPE_NONE },
61		{ 'r', "round_robin", NULL, G_TYPE_NONE },
62		{ 'R', "noround_robin", NULL, G_TYPE_NONE },
63		{ 'w', "verify", NULL, G_TYPE_NONE },
64		{ 'W', "noverify", NULL, G_TYPE_NONE },
65		G_OPT_SENTINEL
66	    }
67	},
68	{ "dump", 0, raid3_main, G_NULL_OPTS },
69	{ "insert", G_FLAG_VERBOSE, NULL,
70	    {
71		{ 'h', "hardcode", NULL, G_TYPE_NONE },
72		{ 'n', "number", NULL, G_TYPE_NUMBER },
73		G_OPT_SENTINEL
74	    }
75	},
76	{ "label", G_FLAG_VERBOSE, raid3_main,
77	    {
78		{ 'h', "hardcode", NULL, G_TYPE_NONE },
79		{ 'n', "noautosync", NULL, G_TYPE_NONE },
80		{ 'r', "round_robin", NULL, G_TYPE_NONE },
81		{ 'w', "verify", NULL, G_TYPE_NONE },
82		G_OPT_SENTINEL
83	    }
84	},
85	{ "rebuild", G_FLAG_VERBOSE, NULL, G_NULL_OPTS },
86	{ "remove", G_FLAG_VERBOSE, NULL,
87	    {
88		{ 'n', "number", NULL, G_TYPE_NUMBER },
89		G_OPT_SENTINEL
90	    }
91	},
92	{ "stop", G_FLAG_VERBOSE, NULL,
93	    {
94		{ 'f', "force", NULL, G_TYPE_NONE },
95		G_OPT_SENTINEL
96	    }
97	},
98	G_CMD_SENTINEL
99};
100
101static int verbose = 0;
102
103void usage(const char *);
104void
105usage(const char *comm)
106{
107	fprintf(stderr,
108	    "usage: %s label [-hnrvw] name prov prov prov [prov [...]]\n"
109	    "       %s clear [-v] prov [prov [...]]\n"
110	    "       %s dump prov [prov [...]]\n"
111	    "       %s configure [-adhnrRvwW] name\n"
112	    "       %s rebuild [-v] name prov\n"
113	    "       %s insert [-hv] <-n number> name prov\n"
114	    "       %s remove [-v] <-n number> name\n"
115	    "       %s stop [-fv] name [...]\n",
116	    comm, comm, comm, comm, comm, comm, comm, comm);
117}
118
119static void
120raid3_main(struct gctl_req *req, unsigned flags)
121{
122	const char *name;
123
124	if ((flags & G_FLAG_VERBOSE) != 0)
125		verbose = 1;
126
127	name = gctl_get_asciiparam(req, "verb");
128	if (name == NULL) {
129		gctl_error(req, "No '%s' argument.", "verb");
130		return;
131	}
132	if (strcmp(name, "label") == 0)
133		raid3_label(req);
134	else if (strcmp(name, "clear") == 0)
135		raid3_clear(req);
136	else if (strcmp(name, "dump") == 0)
137		raid3_dump(req);
138	else
139		gctl_error(req, "Unknown command: %s.", name);
140}
141
142static void
143raid3_label(struct gctl_req *req)
144{
145	struct g_raid3_metadata md;
146	u_char sector[512];
147	const char *str;
148	char param[16];
149	int *hardcode, *nargs, *noautosync, *round_robin, *verify;
150	int error, i;
151	unsigned sectorsize, ssize;
152	off_t mediasize, msize;
153
154	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
155	if (nargs == NULL) {
156		gctl_error(req, "No '%s' argument.", "nargs");
157		return;
158	}
159	if (*nargs < 4) {
160		gctl_error(req, "Too few arguments.");
161		return;
162	}
163#ifndef BITCOUNT
164#define	BITCOUNT(x)	(((BX_(x) + (BX_(x) >> 4)) & 0x0F0F0F0F) % 255)
165#define	BX_(x)		((x) - (((x) >> 1) & 0x77777777) -		\
166			 (((x) >> 2) & 0x33333333) - (((x) >> 3) & 0x11111111))
167#endif
168	if (BITCOUNT(*nargs - 2) != 1) {
169		gctl_error(req, "Invalid number of components.");
170		return;
171	}
172
173	strlcpy(md.md_magic, G_RAID3_MAGIC, sizeof(md.md_magic));
174	md.md_version = G_RAID3_VERSION;
175	str = gctl_get_asciiparam(req, "arg0");
176	if (str == NULL) {
177		gctl_error(req, "No 'arg%u' argument.", 0);
178		return;
179	}
180	strlcpy(md.md_name, str, sizeof(md.md_name));
181	md.md_all = *nargs - 1;
182	md.md_mflags = 0;
183	md.md_dflags = 0;
184	md.md_syncid = 1;
185	md.md_sync_offset = 0;
186	noautosync = gctl_get_paraml(req, "noautosync", sizeof(*noautosync));
187	if (noautosync == NULL) {
188		gctl_error(req, "No '%s' argument.", "noautosync");
189		return;
190	}
191	if (*noautosync)
192		md.md_mflags |= G_RAID3_DEVICE_FLAG_NOAUTOSYNC;
193	round_robin = gctl_get_paraml(req, "round_robin", sizeof(*round_robin));
194	if (round_robin == NULL) {
195		gctl_error(req, "No '%s' argument.", "round_robin");
196		return;
197	}
198	if (*round_robin)
199		md.md_mflags |= G_RAID3_DEVICE_FLAG_ROUND_ROBIN;
200	verify = gctl_get_paraml(req, "verify", sizeof(*verify));
201	if (verify == NULL) {
202		gctl_error(req, "No '%s' argument.", "verify");
203		return;
204	}
205	if (*verify)
206		md.md_mflags |= G_RAID3_DEVICE_FLAG_VERIFY;
207	if (*round_robin && *verify) {
208		gctl_error(req, "Both '%c' and '%c' options given.", 'r', 'w');
209		return;
210	}
211	hardcode = gctl_get_paraml(req, "hardcode", sizeof(*hardcode));
212	if (hardcode == NULL) {
213		gctl_error(req, "No '%s' argument.", "hardcode");
214		return;
215	}
216
217	/*
218	 * Calculate sectorsize by finding least common multiple from
219	 * sectorsizes of every disk and find the smallest mediasize.
220	 */
221	mediasize = 0;
222	sectorsize = 0;
223	for (i = 1; i < *nargs; i++) {
224		snprintf(param, sizeof(param), "arg%u", i);
225		str = gctl_get_asciiparam(req, param);
226
227		msize = g_get_mediasize(str);
228		ssize = g_get_sectorsize(str);
229		if (msize == 0 || ssize == 0) {
230			gctl_error(req, "Can't get informations about %s: %s.",
231			    str, strerror(errno));
232			return;
233		}
234		msize -= ssize;
235		if (mediasize == 0 || (mediasize > 0 && msize < mediasize))
236			mediasize = msize;
237		if (sectorsize == 0)
238			sectorsize = ssize;
239		else
240			sectorsize = g_lcm(sectorsize, ssize);
241	}
242	md.md_mediasize = mediasize * (*nargs - 2);
243	md.md_sectorsize = sectorsize * (*nargs - 2);
244
245	/*
246	 * Clear last sector first, to spoil all components if device exists.
247	 */
248	for (i = 1; i < *nargs; i++) {
249		snprintf(param, sizeof(param), "arg%u", i);
250		str = gctl_get_asciiparam(req, param);
251
252		error = g_metadata_clear(str, NULL);
253		if (error != 0) {
254			gctl_error(req, "Can't store metadata on %s: %s.", str,
255			    strerror(error));
256			return;
257		}
258	}
259
260	/*
261	 * Ok, store metadata (use disk number as priority).
262	 */
263	for (i = 1; i < *nargs; i++) {
264		snprintf(param, sizeof(param), "arg%u", i);
265		str = gctl_get_asciiparam(req, param);
266
267		msize = g_get_mediasize(str) - g_get_sectorsize(str);
268		if (mediasize < msize) {
269			fprintf(stderr,
270			    "warning: %s: only %jd bytes from %jd bytes used.\n",
271			    str, (intmax_t)mediasize, (intmax_t)msize);
272		}
273
274		md.md_no = i - 1;
275		if (!*hardcode)
276			bzero(md.md_provider, sizeof(md.md_provider));
277		else {
278			if (strncmp(str, _PATH_DEV, strlen(_PATH_DEV)) == 0)
279				str += strlen(_PATH_DEV);
280			strlcpy(md.md_provider, str, sizeof(md.md_provider));
281		}
282		if (*verify && md.md_no == md.md_all - 1) {
283			/*
284			 * In "verify" mode, force synchronization of parity
285			 * component on start.
286			 */
287			md.md_syncid = 0;
288		}
289		raid3_metadata_encode(&md, sector);
290		error = g_metadata_store(str, sector, sizeof(sector));
291		if (error != 0) {
292			fprintf(stderr, "Can't store metadata on %s: %s.\n",
293			    str, strerror(error));
294			gctl_error(req, "Not fully done.");
295			continue;
296		}
297		if (verbose)
298			printf("Metadata value stored on %s.\n", str);
299	}
300}
301
302static void
303raid3_clear(struct gctl_req *req)
304{
305	const char *name;
306	char param[16];
307	int *nargs, error, i;
308
309	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
310	if (nargs == NULL) {
311		gctl_error(req, "No '%s' argument.", "nargs");
312		return;
313	}
314	if (*nargs < 1) {
315		gctl_error(req, "Too few arguments.");
316		return;
317	}
318
319	for (i = 0; i < *nargs; i++) {
320		snprintf(param, sizeof(param), "arg%u", i);
321		name = gctl_get_asciiparam(req, param);
322
323		error = g_metadata_clear(name, G_RAID3_MAGIC);
324		if (error != 0) {
325			fprintf(stderr, "Can't clear metadata on %s: %s.\n",
326			    name, strerror(error));
327			gctl_error(req, "Not fully done.");
328			continue;
329		}
330		if (verbose)
331			printf("Metadata cleared on %s.\n", name);
332	}
333}
334
335static void
336raid3_dump(struct gctl_req *req)
337{
338	struct g_raid3_metadata md, tmpmd;
339	const char *name;
340	char param[16];
341	int *nargs, error, i;
342
343	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
344	if (nargs == NULL) {
345		gctl_error(req, "No '%s' argument.", "nargs");
346		return;
347	}
348	if (*nargs < 1) {
349		gctl_error(req, "Too few arguments.");
350		return;
351	}
352
353	for (i = 0; i < *nargs; i++) {
354		snprintf(param, sizeof(param), "arg%u", i);
355		name = gctl_get_asciiparam(req, param);
356
357		error = g_metadata_read(name, (u_char *)&tmpmd, sizeof(tmpmd),
358		    G_RAID3_MAGIC);
359		if (error != 0) {
360			fprintf(stderr, "Can't read metadata from %s: %s.\n",
361			    name, strerror(error));
362			gctl_error(req, "Not fully done.");
363			continue;
364		}
365		if (raid3_metadata_decode((u_char *)&tmpmd, &md) != 0) {
366			fprintf(stderr, "MD5 hash mismatch for %s, skipping.\n",
367			    name);
368			gctl_error(req, "Not fully done.");
369			continue;
370		}
371		printf("Metadata on %s:\n", name);
372		raid3_metadata_dump(&md);
373		printf("\n");
374	}
375}
376