geom_raid3.c revision 134168
1108983Simp/*-
2108983Simp * Copyright (c) 2004 Pawel Jakub Dawidek <pjd@FreeBSD.org>
3108983Simp * All rights reserved.
4108983Simp *
5108983Simp * Redistribution and use in source and binary forms, with or without
6108983Simp * modification, are permitted provided that the following conditions
7108983Simp * are met:
8108983Simp * 1. Redistributions of source code must retain the above copyright
9108983Simp *    notice, this list of conditions and the following disclaimer.
10108983Simp * 2. Redistributions in binary form must reproduce the above copyright
11108983Simp *    notice, this list of conditions and the following disclaimer in the
12108983Simp *    documentation and/or other materials provided with the distribution.
13108983Simp *
14108983Simp * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15108983Simp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16108983Simp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17108983Simp * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18108983Simp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19108983Simp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20148471Simp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21108983Simp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22139027Sbrueffer * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23146969Smarius * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24139027Sbrueffer * SUCH DAMAGE.
25108983Simp */
26108983Simp
27108983Simp#include <sys/cdefs.h>
28108983Simp__FBSDID("$FreeBSD: head/sbin/geom/class/raid3/geom_raid3.c 134168 2004-08-22 16:21:12Z pjd $");
29108983Simp
30108983Simp#include <sys/param.h>
31159126Sthompsa#include <errno.h>
32159126Sthompsa#include <paths.h>
33108983Simp#include <stdio.h>
34159126Sthompsa#include <stdlib.h>
35159126Sthompsa#include <stdint.h>
36159126Sthompsa#include <string.h>
37159126Sthompsa#include <strings.h>
38108983Simp#include <assert.h>
39108983Simp#include <libgeom.h>
40159126Sthompsa#include <geom/raid3/g_raid3.h>
41159126Sthompsa#include <core/geom.h>
42159126Sthompsa#include <misc/subr.h>
43159126Sthompsa
44108983Simp
45108983Simpuint32_t lib_version = G_LIB_VERSION;
46147088Sbrooksuint32_t version = G_RAID3_VERSION;
47147088Sbrooks
48147088Sbrooksstatic void raid3_main(struct gctl_req *req, unsigned f);
49147088Sbrooksstatic void raid3_clear(struct gctl_req *req);
50147088Sbrooksstatic void raid3_dump(struct gctl_req *req);
51147088Sbrooksstatic void raid3_label(struct gctl_req *req);
52147088Sbrooks
53147088Sbrooksstruct g_command class_commands[] = {
54147088Sbrooks	{ "clear", G_FLAG_VERBOSE, raid3_main, G_NULL_OPTS },
55148642Ssam	{ "configure", G_FLAG_VERBOSE, NULL,
56175683Smtm	    {
57147088Sbrooks		{ 'a', "autosync", NULL, G_TYPE_NONE },
58147088Sbrooks		{ 'd', "dynamic", NULL, G_TYPE_NONE },
59148642Ssam		{ 'h', "hardcode", NULL, G_TYPE_NONE },
60148642Ssam		{ 'n', "noautosync", NULL, G_TYPE_NONE },
61148642Ssam		{ 'r', "round_robin", NULL, G_TYPE_NONE },
62148642Ssam		{ 'R', "noround_robin", NULL, G_TYPE_NONE },
63148642Ssam		{ 'w', "verify", NULL, G_TYPE_NONE },
64148642Ssam		{ 'W', "noverify", NULL, G_TYPE_NONE },
65148642Ssam		G_OPT_SENTINEL
66148642Ssam	    }
67148642Ssam	},
68148642Ssam	{ "dump", 0, raid3_main, G_NULL_OPTS },
69148642Ssam	{ "insert", G_FLAG_VERBOSE, NULL,
70148642Ssam	    {
71148642Ssam		{ 'h', "hardcode", NULL, G_TYPE_NONE },
72148642Ssam		{ 'n', "number", NULL, G_TYPE_NUMBER },
73148642Ssam		G_OPT_SENTINEL
74148642Ssam	    }
75148642Ssam	},
76175683Smtm	{ "label", G_FLAG_VERBOSE, raid3_main,
77148642Ssam	    {
78148642Ssam		{ 'h', "hardcode", NULL, G_TYPE_NONE },
79108983Simp		{ 'n', "noautosync", NULL, G_TYPE_NONE },
80108983Simp		{ 'r', "round_robin", NULL, G_TYPE_NONE },
81108983Simp		{ 'w', "verify", NULL, G_TYPE_NONE },
82139281Sbrueffer		G_OPT_SENTINEL
83108983Simp	    }
84108983Simp	},
85108983Simp	{ "rebuild", G_FLAG_VERBOSE, NULL, G_NULL_OPTS },
86108983Simp	{ "remove", G_FLAG_VERBOSE, NULL,
87108983Simp	    {
88108983Simp		{ 'n', "number", NULL, G_TYPE_NUMBER },
89108983Simp		G_OPT_SENTINEL
90108983Simp	    }
91152326Semax	},
92152326Semax	{ "stop", G_FLAG_VERBOSE, NULL,
93152326Semax	    {
94175683Smtm		{ 'f', "force", NULL, G_TYPE_NONE },
95152326Semax		G_OPT_SENTINEL
96152326Semax	    }
97152326Semax	},
98175683Smtm	G_CMD_SENTINEL
99152326Semax};
100152326Semax
101139281Sbruefferstatic int verbose = 0;
102134584Sbrooks
103134584Sbrooksvoid usage(const char *);
104156782Semaxvoid
105134584Sbrooksusage(const char *comm)
106134584Sbrooks{
107134584Sbrooks	fprintf(stderr,
108156331Semax	    "usage: %s label [-hnrvw] name prov prov prov [prov [...]]\n"
109134584Sbrooks	    "       %s clear [-v] prov [prov [...]]\n"
110134584Sbrooks	    "       %s dump prov [prov [...]]\n"
111138175Siedowse	    "       %s configure [-adhnrRvwW] name\n"
112138175Siedowse	    "       %s rebuild [-v] name prov\n"
113138175Siedowse	    "       %s insert [-hv] <-n number> name prov\n"
114138175Siedowse	    "       %s remove [-v] <-n number> name\n"
115175683Smtm	    "       %s stop [-fv] name [...]\n",
116138175Siedowse	    comm, comm, comm, comm, comm, comm, comm, comm);
117138175Siedowse	exit(EXIT_FAILURE);
118179804Skmacy}
119179804Skmacy
120179804Skmacystatic void
121179804Skmacyraid3_main(struct gctl_req *req, unsigned flags)
122179804Skmacy{
123153300Siedowse	const char *name;
124153300Siedowse
125153300Siedowse	if ((flags & G_FLAG_VERBOSE) != 0)
126153300Siedowse		verbose = 1;
127153300Siedowse
128153300Siedowse	name = gctl_get_asciiparam(req, "verb");
129153300Siedowse	if (name == NULL) {
130153300Siedowse		gctl_error(req, "No '%s' argument.", "verb");
131153300Siedowse		return;
132153300Siedowse	}
133153300Siedowse	if (strcmp(name, "label") == 0)
134153300Siedowse		raid3_label(req);
135153300Siedowse	else if (strcmp(name, "clear") == 0)
136153300Siedowse		raid3_clear(req);
137153300Siedowse	else if (strcmp(name, "dump") == 0)
138153300Siedowse		raid3_dump(req);
139153300Siedowse	else
140153300Siedowse		gctl_error(req, "Unknown command: %s.", name);
141153300Siedowse}
142153300Siedowse
143153300Siedowsestatic void
144153300Siedowseraid3_label(struct gctl_req *req)
145153300Siedowse{
146153300Siedowse	struct g_raid3_metadata md;
147153300Siedowse	u_char sector[512];
148153300Siedowse	const char *str;
149153300Siedowse	char param[16];
150153300Siedowse	int *hardcode, *nargs, *noautosync, *round_robin, *verify;
151153300Siedowse	int error, i;
152108983Simp	unsigned sectorsize;
153148471Simp	off_t mediasize;
154148471Simp
155108983Simp	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
156108983Simp	if (nargs == NULL) {
157108983Simp		gctl_error(req, "No '%s' argument.", "nargs");
158131646Simp		return;
159108983Simp	}
160108983Simp	if (*nargs < 4) {
161108983Simp		gctl_error(req, "Too few arguments.");
162114799Simp		return;
163166754Simp	}
164166754Simp#ifndef BITCOUNT
165119254Simp#define	BITCOUNT(x)	(((BX_(x) + (BX_(x) >> 4)) & 0x0F0F0F0F) % 255)
166114852Simp#define	BX_(x)		((x) - (((x) >> 1) & 0x77777777) -		\
167119254Simp			 (((x) >> 2) & 0x33333333) - (((x) >> 3) & 0x11111111))
168108983Simp#endif
169166701Sjoerg	if (BITCOUNT(*nargs - 2) != 1) {
170166701Sjoerg		gctl_error(req, "Invalid number of components.");
171166701Sjoerg		return;
172166701Sjoerg	}
173166701Sjoerg
174166701Sjoerg	strlcpy(md.md_magic, G_RAID3_MAGIC, sizeof(md.md_magic));
175166701Sjoerg	md.md_version = G_RAID3_VERSION;
176166701Sjoerg	str = gctl_get_asciiparam(req, "arg0");
177166701Sjoerg	if (str == NULL) {
178166701Sjoerg		gctl_error(req, "No 'arg%u' argument.", 0);
179166701Sjoerg		return;
180166701Sjoerg	}
181166701Sjoerg	strlcpy(md.md_name, str, sizeof(md.md_name));
182166701Sjoerg	md.md_all = *nargs - 1;
183166701Sjoerg	md.md_mflags = 0;
184166701Sjoerg	md.md_dflags = 0;
185166701Sjoerg	md.md_syncid = 1;
186166701Sjoerg	md.md_sync_offset = 0;
187166701Sjoerg	noautosync = gctl_get_paraml(req, "noautosync", sizeof(*noautosync));
188166701Sjoerg	if (noautosync == NULL) {
189166701Sjoerg		gctl_error(req, "No '%s' argument.", "noautosync");
190166701Sjoerg		return;
191166701Sjoerg	}
192166701Sjoerg	if (*noautosync)
193166701Sjoerg		md.md_mflags |= G_RAID3_DEVICE_FLAG_NOAUTOSYNC;
194166701Sjoerg	round_robin = gctl_get_paraml(req, "round_robin", sizeof(*round_robin));
195166701Sjoerg	if (round_robin == NULL) {
196166701Sjoerg		gctl_error(req, "No '%s' argument.", "round_robin");
197166701Sjoerg		return;
198166701Sjoerg	}
199139281Sbrueffer	if (*round_robin)
200123626Snjl		md.md_mflags |= G_RAID3_DEVICE_FLAG_ROUND_ROBIN;
201123626Snjl	verify = gctl_get_paraml(req, "verify", sizeof(*verify));
202123626Snjl	if (verify == NULL) {
203125366Snjl		gctl_error(req, "No '%s' argument.", "verify");
204123626Snjl		return;
205123626Snjl	}
206125366Snjl	if (*verify)
207125366Snjl		md.md_mflags |= G_RAID3_DEVICE_FLAG_VERIFY;
208125366Snjl	if (*round_robin && *verify) {
209125366Snjl		gctl_error(req, "Both '%c' and '%c' options given.", 'r', 'w');
210125366Snjl		return;
211125366Snjl	}
212125366Snjl	hardcode = gctl_get_paraml(req, "hardcode", sizeof(*hardcode));
213125366Snjl	if (hardcode == NULL) {
214125366Snjl		gctl_error(req, "No '%s' argument.", "hardcode");
215125366Snjl		return;
216168495Spjd	}
217168495Spjd
218168495Spjd	/*
219168495Spjd	 * Calculate sectorsize by finding least common multiple from
220168495Spjd	 * sectorsizes of every disk and find the smallest mediasize.
221168495Spjd	 */
222168495Spjd	mediasize = 0;
223168495Spjd	sectorsize = 0;
224168495Spjd	for (i = 1; i < *nargs; i++) {
225168495Spjd		unsigned ssize;
226168497Spjd		off_t msize;
227168495Spjd
228168495Spjd		snprintf(param, sizeof(param), "arg%u", i);
229168495Spjd		str = gctl_get_asciiparam(req, param);
230168495Spjd
231168495Spjd		msize = g_get_mediasize(str);
232168495Spjd		ssize = g_get_sectorsize(str);
233168495Spjd		if (msize == 0 || ssize == 0) {
234168495Spjd			gctl_error(req, "Can't get informations about %s: %s.",
235168495Spjd			    str, strerror(errno));
236168495Spjd			return;
237168495Spjd		}
238168495Spjd		msize -= ssize;
239168495Spjd		if (mediasize == 0 || (mediasize > 0 && msize < mediasize))
240168495Spjd			mediasize = msize;
241168495Spjd		if (sectorsize == 0)
242168495Spjd			sectorsize = ssize;
243168495Spjd		else
244168495Spjd			sectorsize = g_lcm(sectorsize, ssize);
245168495Spjd	}
246168495Spjd	md.md_mediasize = mediasize * (*nargs - 2);
247170976Snjl	md.md_sectorsize = sectorsize * (*nargs - 2);
248170976Snjl
249170976Snjl	/*
250170976Snjl	 * Clear last sector first, to spoil all components if device exists.
251170976Snjl	 */
252170976Snjl	for (i = 1; i < *nargs; i++) {
253170976Snjl		snprintf(param, sizeof(param), "arg%u", i);
254170976Snjl		str = gctl_get_asciiparam(req, param);
255170976Snjl
256170976Snjl		error = g_metadata_clear(str, NULL);
257170976Snjl		if (error != 0) {
258170976Snjl			gctl_error(req, "Can't store metadata on %s: %s.", str,
259170976Snjl			    strerror(error));
260108983Simp			return;
261108983Simp		}
262108983Simp	}
263108983Simp
264108983Simp	/*
265108983Simp	 * Ok, store metadata (use disk number as priority).
266108983Simp	 */
267108983Simp	for (i = 1; i < *nargs; i++) {
268108983Simp		snprintf(param, sizeof(param), "arg%u", i);
269108983Simp		str = gctl_get_asciiparam(req, param);
270108983Simp
271108983Simp		md.md_no = i - 1;
272108983Simp		if (!*hardcode)
273108983Simp			bzero(md.md_provider, sizeof(md.md_provider));
274108983Simp		else {
275108983Simp			if (strncmp(str, _PATH_DEV, strlen(_PATH_DEV)) == 0)
276108983Simp				str += strlen(_PATH_DEV);
277108983Simp			strlcpy(md.md_provider, str, sizeof(md.md_provider));
278108983Simp		}
279108983Simp		raid3_metadata_encode(&md, sector);
280108983Simp		error = g_metadata_store(str, sector, sizeof(sector));
281108983Simp		if (error != 0) {
282108983Simp			fprintf(stderr, "Can't store metadata on %s: %s.\n",
283121493Snjl			    str, strerror(error));
284121493Snjl			gctl_error(req, "Not fully done.");
285121493Snjl			continue;
286121493Snjl		}
287121493Snjl		if (verbose)
288121493Snjl			printf("Metadata value stored on %s.\n", str);
289121493Snjl	}
290121493Snjl}
291121493Snjl
292121493Snjlstatic void
293121493Snjlraid3_clear(struct gctl_req *req)
294121493Snjl{
295121493Snjl	const char *name;
296121493Snjl	char param[16];
297121493Snjl	int *nargs, error, i;
298170976Snjl
299121493Snjl	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
300121493Snjl	if (nargs == NULL) {
301121493Snjl		gctl_error(req, "No '%s' argument.", "nargs");
302121493Snjl		return;
303121493Snjl	}
304121493Snjl	if (*nargs < 1) {
305121493Snjl		gctl_error(req, "Too few arguments.");
306121493Snjl		return;
307121493Snjl	}
308121493Snjl
309121493Snjl	for (i = 0; i < *nargs; i++) {
310108983Simp		snprintf(param, sizeof(param), "arg%u", i);
311		name = gctl_get_asciiparam(req, param);
312
313		error = g_metadata_clear(name, G_RAID3_MAGIC);
314		if (error != 0) {
315			fprintf(stderr, "Can't clear metadata on %s: %s.\n",
316			    name, strerror(error));
317			gctl_error(req, "Not fully done.");
318			continue;
319		}
320		if (verbose)
321			printf("Metadata cleared on %s.\n", name);
322	}
323}
324
325static void
326raid3_dump(struct gctl_req *req)
327{
328	struct g_raid3_metadata md, tmpmd;
329	const char *name;
330	char param[16];
331	int *nargs, error, i;
332
333	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
334	if (nargs == NULL) {
335		gctl_error(req, "No '%s' argument.", "nargs");
336		return;
337	}
338	if (*nargs < 1) {
339		gctl_error(req, "Too few arguments.");
340		return;
341	}
342
343	for (i = 0; i < *nargs; i++) {
344		snprintf(param, sizeof(param), "arg%u", i);
345		name = gctl_get_asciiparam(req, param);
346
347		error = g_metadata_read(name, (u_char *)&tmpmd, sizeof(tmpmd),
348		    G_RAID3_MAGIC);
349		if (error != 0) {
350			fprintf(stderr, "Can't read metadata from %s: %s.\n",
351			    name, strerror(error));
352			gctl_error(req, "Not fully done.");
353			continue;
354		}
355		if (raid3_metadata_decode((u_char *)&tmpmd, &md) != 0) {
356			fprintf(stderr, "MD5 hash mismatch for %s, skipping.\n",
357			    name);
358			gctl_error(req, "Not fully done.");
359			continue;
360		}
361		printf("Metadata on %s:\n", name);
362		raid3_metadata_dump(&md);
363		printf("\n");
364	}
365}
366