geom_raid3.c revision 134539
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 134539 2004-08-30 22:08:00Z 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	exit(EXIT_FAILURE);
118}
119
120static void
121raid3_main(struct gctl_req *req, unsigned flags)
122{
123	const char *name;
124
125	if ((flags & G_FLAG_VERBOSE) != 0)
126		verbose = 1;
127
128	name = gctl_get_asciiparam(req, "verb");
129	if (name == NULL) {
130		gctl_error(req, "No '%s' argument.", "verb");
131		return;
132	}
133	if (strcmp(name, "label") == 0)
134		raid3_label(req);
135	else if (strcmp(name, "clear") == 0)
136		raid3_clear(req);
137	else if (strcmp(name, "dump") == 0)
138		raid3_dump(req);
139	else
140		gctl_error(req, "Unknown command: %s.", name);
141}
142
143static void
144raid3_label(struct gctl_req *req)
145{
146	struct g_raid3_metadata md;
147	u_char sector[512];
148	const char *str;
149	char param[16];
150	int *hardcode, *nargs, *noautosync, *round_robin, *verify;
151	int error, i;
152	unsigned sectorsize, ssize;
153	off_t mediasize, msize;
154
155	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
156	if (nargs == NULL) {
157		gctl_error(req, "No '%s' argument.", "nargs");
158		return;
159	}
160	if (*nargs < 4) {
161		gctl_error(req, "Too few arguments.");
162		return;
163	}
164#ifndef BITCOUNT
165#define	BITCOUNT(x)	(((BX_(x) + (BX_(x) >> 4)) & 0x0F0F0F0F) % 255)
166#define	BX_(x)		((x) - (((x) >> 1) & 0x77777777) -		\
167			 (((x) >> 2) & 0x33333333) - (((x) >> 3) & 0x11111111))
168#endif
169	if (BITCOUNT(*nargs - 2) != 1) {
170		gctl_error(req, "Invalid number of components.");
171		return;
172	}
173
174	strlcpy(md.md_magic, G_RAID3_MAGIC, sizeof(md.md_magic));
175	md.md_version = G_RAID3_VERSION;
176	str = gctl_get_asciiparam(req, "arg0");
177	if (str == NULL) {
178		gctl_error(req, "No 'arg%u' argument.", 0);
179		return;
180	}
181	strlcpy(md.md_name, str, sizeof(md.md_name));
182	md.md_all = *nargs - 1;
183	md.md_mflags = 0;
184	md.md_dflags = 0;
185	md.md_syncid = 1;
186	md.md_sync_offset = 0;
187	noautosync = gctl_get_paraml(req, "noautosync", sizeof(*noautosync));
188	if (noautosync == NULL) {
189		gctl_error(req, "No '%s' argument.", "noautosync");
190		return;
191	}
192	if (*noautosync)
193		md.md_mflags |= G_RAID3_DEVICE_FLAG_NOAUTOSYNC;
194	round_robin = gctl_get_paraml(req, "round_robin", sizeof(*round_robin));
195	if (round_robin == NULL) {
196		gctl_error(req, "No '%s' argument.", "round_robin");
197		return;
198	}
199	if (*round_robin)
200		md.md_mflags |= G_RAID3_DEVICE_FLAG_ROUND_ROBIN;
201	verify = gctl_get_paraml(req, "verify", sizeof(*verify));
202	if (verify == NULL) {
203		gctl_error(req, "No '%s' argument.", "verify");
204		return;
205	}
206	if (*verify)
207		md.md_mflags |= G_RAID3_DEVICE_FLAG_VERIFY;
208	if (*round_robin && *verify) {
209		gctl_error(req, "Both '%c' and '%c' options given.", 'r', 'w');
210		return;
211	}
212	hardcode = gctl_get_paraml(req, "hardcode", sizeof(*hardcode));
213	if (hardcode == NULL) {
214		gctl_error(req, "No '%s' argument.", "hardcode");
215		return;
216	}
217
218	/*
219	 * Calculate sectorsize by finding least common multiple from
220	 * sectorsizes of every disk and find the smallest mediasize.
221	 */
222	mediasize = 0;
223	sectorsize = 0;
224	for (i = 1; i < *nargs; i++) {
225		snprintf(param, sizeof(param), "arg%u", i);
226		str = gctl_get_asciiparam(req, param);
227
228		msize = g_get_mediasize(str);
229		ssize = g_get_sectorsize(str);
230		if (msize == 0 || ssize == 0) {
231			gctl_error(req, "Can't get informations about %s: %s.",
232			    str, strerror(errno));
233			return;
234		}
235		msize -= ssize;
236		if (mediasize == 0 || (mediasize > 0 && msize < mediasize))
237			mediasize = msize;
238		if (sectorsize == 0)
239			sectorsize = ssize;
240		else
241			sectorsize = g_lcm(sectorsize, ssize);
242	}
243	md.md_mediasize = mediasize * (*nargs - 2);
244	md.md_sectorsize = sectorsize * (*nargs - 2);
245
246	/*
247	 * Clear last sector first, to spoil all components if device exists.
248	 */
249	for (i = 1; i < *nargs; i++) {
250		snprintf(param, sizeof(param), "arg%u", i);
251		str = gctl_get_asciiparam(req, param);
252
253		error = g_metadata_clear(str, NULL);
254		if (error != 0) {
255			gctl_error(req, "Can't store metadata on %s: %s.", str,
256			    strerror(error));
257			return;
258		}
259	}
260
261	/*
262	 * Ok, store metadata (use disk number as priority).
263	 */
264	for (i = 1; i < *nargs; i++) {
265		snprintf(param, sizeof(param), "arg%u", i);
266		str = gctl_get_asciiparam(req, param);
267
268		msize = g_get_mediasize(str) - g_get_sectorsize(str);
269		if (mediasize < msize) {
270			fprintf(stderr,
271			    "warning: %s: only %jd bytes from %jd bytes used.\n",
272			    str, (intmax_t)mediasize, (intmax_t)msize);
273		}
274
275		md.md_no = i - 1;
276		if (!*hardcode)
277			bzero(md.md_provider, sizeof(md.md_provider));
278		else {
279			if (strncmp(str, _PATH_DEV, strlen(_PATH_DEV)) == 0)
280				str += strlen(_PATH_DEV);
281			strlcpy(md.md_provider, str, sizeof(md.md_provider));
282		}
283		if (*verify && md.md_no == md.md_all - 1) {
284			/*
285			 * In "verify" mode, force synchronization of parity
286			 * component on start.
287			 */
288			md.md_syncid = 0;
289		}
290		raid3_metadata_encode(&md, sector);
291		error = g_metadata_store(str, sector, sizeof(sector));
292		if (error != 0) {
293			fprintf(stderr, "Can't store metadata on %s: %s.\n",
294			    str, strerror(error));
295			gctl_error(req, "Not fully done.");
296			continue;
297		}
298		if (verbose)
299			printf("Metadata value stored on %s.\n", str);
300	}
301}
302
303static void
304raid3_clear(struct gctl_req *req)
305{
306	const char *name;
307	char param[16];
308	int *nargs, error, i;
309
310	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
311	if (nargs == NULL) {
312		gctl_error(req, "No '%s' argument.", "nargs");
313		return;
314	}
315	if (*nargs < 1) {
316		gctl_error(req, "Too few arguments.");
317		return;
318	}
319
320	for (i = 0; i < *nargs; i++) {
321		snprintf(param, sizeof(param), "arg%u", i);
322		name = gctl_get_asciiparam(req, param);
323
324		error = g_metadata_clear(name, G_RAID3_MAGIC);
325		if (error != 0) {
326			fprintf(stderr, "Can't clear metadata on %s: %s.\n",
327			    name, strerror(error));
328			gctl_error(req, "Not fully done.");
329			continue;
330		}
331		if (verbose)
332			printf("Metadata cleared on %s.\n", name);
333	}
334}
335
336static void
337raid3_dump(struct gctl_req *req)
338{
339	struct g_raid3_metadata md, tmpmd;
340	const char *name;
341	char param[16];
342	int *nargs, error, i;
343
344	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
345	if (nargs == NULL) {
346		gctl_error(req, "No '%s' argument.", "nargs");
347		return;
348	}
349	if (*nargs < 1) {
350		gctl_error(req, "Too few arguments.");
351		return;
352	}
353
354	for (i = 0; i < *nargs; i++) {
355		snprintf(param, sizeof(param), "arg%u", i);
356		name = gctl_get_asciiparam(req, param);
357
358		error = g_metadata_read(name, (u_char *)&tmpmd, sizeof(tmpmd),
359		    G_RAID3_MAGIC);
360		if (error != 0) {
361			fprintf(stderr, "Can't read metadata from %s: %s.\n",
362			    name, strerror(error));
363			gctl_error(req, "Not fully done.");
364			continue;
365		}
366		if (raid3_metadata_decode((u_char *)&tmpmd, &md) != 0) {
367			fprintf(stderr, "MD5 hash mismatch for %s, skipping.\n",
368			    name);
369			gctl_error(req, "Not fully done.");
370			continue;
371		}
372		printf("Metadata on %s:\n", name);
373		raid3_metadata_dump(&md);
374		printf("\n");
375	}
376}
377