geom_mirror.c revision 260503
1/*-
2 * Copyright (c) 2004-2009 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: stable/10/sbin/geom/class/mirror/geom_mirror.c 260503 2014-01-10 07:48:36Z ae $");
29
30#include <sys/param.h>
31#include <err.h>
32#include <errno.h>
33#include <paths.h>
34#include <stdio.h>
35#include <stdlib.h>
36#include <stdint.h>
37#include <string.h>
38#include <strings.h>
39#include <assert.h>
40#include <libgeom.h>
41#include <geom/mirror/g_mirror.h>
42#include <core/geom.h>
43#include <misc/subr.h>
44
45uint32_t lib_version = G_LIB_VERSION;
46uint32_t version = G_MIRROR_VERSION;
47
48#define	GMIRROR_BALANCE		"load"
49#define	GMIRROR_SLICE		"4096"
50#define	GMIRROR_PRIORITY	"0"
51
52static void mirror_main(struct gctl_req *req, unsigned flags);
53static void mirror_activate(struct gctl_req *req);
54static void mirror_clear(struct gctl_req *req);
55static void mirror_dump(struct gctl_req *req);
56static void mirror_label(struct gctl_req *req);
57static void mirror_resize(struct gctl_req *req, unsigned flags);
58
59struct g_command class_commands[] = {
60	{ "activate", G_FLAG_VERBOSE, mirror_main, G_NULL_OPTS,
61	    "[-v] name prov ..."
62	},
63	{ "clear", G_FLAG_VERBOSE, mirror_main, G_NULL_OPTS,
64	    "[-v] prov ..."
65	},
66	{ "configure", G_FLAG_VERBOSE, NULL,
67	    {
68		{ 'a', "autosync", NULL, G_TYPE_BOOL },
69		{ 'b', "balance", "", G_TYPE_STRING },
70		{ 'd', "dynamic", NULL, G_TYPE_BOOL },
71		{ 'f', "failsync", NULL, G_TYPE_BOOL },
72		{ 'F', "nofailsync", NULL, G_TYPE_BOOL },
73		{ 'h', "hardcode", NULL, G_TYPE_BOOL },
74		{ 'n', "noautosync", NULL, G_TYPE_BOOL },
75		{ 'p', "priority", "-1", G_TYPE_NUMBER },
76		{ 's', "slice", "-1", G_TYPE_NUMBER },
77		G_OPT_SENTINEL
78	    },
79	    "[-adfFhnv] [-b balance] [-s slice] name\n"
80	    "[-v] -p priority name prov"
81	},
82	{ "deactivate", G_FLAG_VERBOSE, NULL, G_NULL_OPTS,
83	    "[-v] name prov ..."
84	},
85	{ "destroy", G_FLAG_VERBOSE, NULL,
86	    {
87		{ 'f', "force", NULL, G_TYPE_BOOL },
88		G_OPT_SENTINEL
89	    },
90	    "[-fv] name ..."
91	},
92	{ "dump", 0, mirror_main, G_NULL_OPTS,
93	    "prov ..."
94	},
95	{ "forget", G_FLAG_VERBOSE, NULL, G_NULL_OPTS,
96	    "name ..."
97	},
98	{ "label", G_FLAG_VERBOSE, mirror_main,
99	    {
100		{ 'b', "balance", GMIRROR_BALANCE, G_TYPE_STRING },
101		{ 'F', "nofailsync", NULL, G_TYPE_BOOL },
102		{ 'h', "hardcode", NULL, G_TYPE_BOOL },
103		{ 'n', "noautosync", NULL, G_TYPE_BOOL },
104		{ 's', "slice", GMIRROR_SLICE, G_TYPE_NUMBER },
105		G_OPT_SENTINEL
106	    },
107	    "[-Fhnv] [-b balance] [-s slice] name prov ..."
108	},
109	{ "insert", G_FLAG_VERBOSE, NULL,
110	    {
111		{ 'h', "hardcode", NULL, G_TYPE_BOOL },
112		{ 'i', "inactive", NULL, G_TYPE_BOOL },
113		{ 'p', "priority", GMIRROR_PRIORITY, G_TYPE_NUMBER },
114		G_OPT_SENTINEL
115	    },
116	    "[-hiv] [-p priority] name prov ..."
117	},
118	{ "rebuild", G_FLAG_VERBOSE, NULL, G_NULL_OPTS,
119	    "[-v] name prov ..."
120	},
121	{ "remove", G_FLAG_VERBOSE, NULL, G_NULL_OPTS,
122	    "[-v] name prov ..."
123	},
124	{ "resize", G_FLAG_VERBOSE, mirror_resize,
125	    {
126		{ 's', "size", "*", G_TYPE_STRING },
127		G_OPT_SENTINEL
128	    },
129	    "[-s size] [-v] name"
130	},
131	{ "stop", G_FLAG_VERBOSE, NULL,
132	    {
133		{ 'f', "force", NULL, G_TYPE_BOOL },
134		G_OPT_SENTINEL
135	    },
136	    "[-fv] name ..."
137	},
138	G_CMD_SENTINEL
139};
140
141static int verbose = 0;
142
143static void
144mirror_main(struct gctl_req *req, unsigned flags)
145{
146	const char *name;
147
148	if ((flags & G_FLAG_VERBOSE) != 0)
149		verbose = 1;
150
151	name = gctl_get_ascii(req, "verb");
152	if (name == NULL) {
153		gctl_error(req, "No '%s' argument.", "verb");
154		return;
155	}
156	if (strcmp(name, "label") == 0)
157		mirror_label(req);
158	else if (strcmp(name, "clear") == 0)
159		mirror_clear(req);
160	else if (strcmp(name, "dump") == 0)
161		mirror_dump(req);
162	else if (strcmp(name, "activate") == 0)
163		mirror_activate(req);
164	else
165		gctl_error(req, "Unknown command: %s.", name);
166}
167
168static void
169mirror_label(struct gctl_req *req)
170{
171	struct g_mirror_metadata md;
172	u_char sector[512];
173	const char *str;
174	unsigned sectorsize;
175	off_t mediasize;
176	intmax_t val;
177	int error, i, nargs, bal, hardcode;
178
179	nargs = gctl_get_int(req, "nargs");
180	if (nargs < 2) {
181		gctl_error(req, "Too few arguments.");
182		return;
183	}
184
185	strlcpy(md.md_magic, G_MIRROR_MAGIC, sizeof(md.md_magic));
186	md.md_version = G_MIRROR_VERSION;
187	str = gctl_get_ascii(req, "arg0");
188	strlcpy(md.md_name, str, sizeof(md.md_name));
189	md.md_mid = arc4random();
190	md.md_all = nargs - 1;
191	md.md_mflags = 0;
192	md.md_dflags = 0;
193	md.md_genid = 0;
194	md.md_syncid = 1;
195	md.md_sync_offset = 0;
196	val = gctl_get_intmax(req, "slice");
197	md.md_slice = val;
198	str = gctl_get_ascii(req, "balance");
199	bal = balance_id(str);
200	if (bal == -1) {
201		gctl_error(req, "Invalid balance algorithm.");
202		return;
203	}
204	md.md_balance = bal;
205	if (gctl_get_int(req, "noautosync"))
206		md.md_mflags |= G_MIRROR_DEVICE_FLAG_NOAUTOSYNC;
207	if (gctl_get_int(req, "nofailsync"))
208		md.md_mflags |= G_MIRROR_DEVICE_FLAG_NOFAILSYNC;
209	hardcode = gctl_get_int(req, "hardcode");
210
211	/*
212	 * Calculate sectorsize by finding least common multiple from
213	 * sectorsizes of every disk and find the smallest mediasize.
214	 */
215	mediasize = 0;
216	sectorsize = 0;
217	for (i = 1; i < nargs; i++) {
218		unsigned ssize;
219		off_t msize;
220
221		str = gctl_get_ascii(req, "arg%d", i);
222		msize = g_get_mediasize(str);
223		ssize = g_get_sectorsize(str);
224		if (msize == 0 || ssize == 0) {
225			gctl_error(req, "Can't get informations about %s: %s.",
226			    str, strerror(errno));
227			return;
228		}
229		msize -= ssize;
230		if (mediasize == 0 || (mediasize > 0 && msize < mediasize))
231			mediasize = msize;
232		if (sectorsize == 0)
233			sectorsize = ssize;
234		else
235			sectorsize = g_lcm(sectorsize, ssize);
236	}
237	md.md_mediasize = mediasize;
238	md.md_sectorsize = sectorsize;
239	md.md_mediasize -= (md.md_mediasize % md.md_sectorsize);
240
241	/*
242	 * Clear last sector first, to spoil all components if device exists.
243	 */
244	for (i = 1; i < nargs; i++) {
245		str = gctl_get_ascii(req, "arg%d", i);
246		error = g_metadata_clear(str, NULL);
247		if (error != 0) {
248			gctl_error(req, "Can't store metadata on %s: %s.", str,
249			    strerror(error));
250			return;
251		}
252	}
253
254	/*
255	 * Ok, store metadata (use disk number as priority).
256	 */
257	for (i = 1; i < nargs; i++) {
258		str = gctl_get_ascii(req, "arg%d", i);
259		md.md_did = arc4random();
260		md.md_priority = i - 1;
261		md.md_provsize = g_get_mediasize(str);
262		assert(md.md_provsize != 0);
263		if (!hardcode)
264			bzero(md.md_provider, sizeof(md.md_provider));
265		else {
266			if (strncmp(str, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
267				str += sizeof(_PATH_DEV) - 1;
268			strlcpy(md.md_provider, str, sizeof(md.md_provider));
269		}
270		mirror_metadata_encode(&md, sector);
271		error = g_metadata_store(str, sector, sizeof(sector));
272		if (error != 0) {
273			fprintf(stderr, "Can't store metadata on %s: %s.\n",
274			    str, strerror(error));
275			gctl_error(req, "Not fully done.");
276			continue;
277		}
278		if (verbose)
279			printf("Metadata value stored on %s.\n", str);
280	}
281}
282
283static void
284mirror_clear(struct gctl_req *req)
285{
286	const char *name;
287	int error, i, nargs;
288
289	nargs = gctl_get_int(req, "nargs");
290	if (nargs < 1) {
291		gctl_error(req, "Too few arguments.");
292		return;
293	}
294
295	for (i = 0; i < nargs; i++) {
296		name = gctl_get_ascii(req, "arg%d", i);
297		error = g_metadata_clear(name, G_MIRROR_MAGIC);
298		if (error != 0) {
299			fprintf(stderr, "Can't clear metadata on %s: %s.\n",
300			    name, strerror(error));
301			gctl_error(req, "Not fully done.");
302			continue;
303		}
304		if (verbose)
305			printf("Metadata cleared on %s.\n", name);
306	}
307}
308
309static void
310mirror_dump(struct gctl_req *req)
311{
312	struct g_mirror_metadata md, tmpmd;
313	const char *name;
314	int error, i, nargs;
315
316	nargs = gctl_get_int(req, "nargs");
317	if (nargs < 1) {
318		gctl_error(req, "Too few arguments.");
319		return;
320	}
321
322	for (i = 0; i < nargs; i++) {
323		name = gctl_get_ascii(req, "arg%d", i);
324		error = g_metadata_read(name, (u_char *)&tmpmd, sizeof(tmpmd),
325		    G_MIRROR_MAGIC);
326		if (error != 0) {
327			fprintf(stderr, "Can't read metadata from %s: %s.\n",
328			    name, strerror(error));
329			gctl_error(req, "Not fully done.");
330			continue;
331		}
332		if (mirror_metadata_decode((u_char *)&tmpmd, &md) != 0) {
333			fprintf(stderr, "MD5 hash mismatch for %s, skipping.\n",
334			    name);
335			gctl_error(req, "Not fully done.");
336			continue;
337		}
338		printf("Metadata on %s:\n", name);
339		mirror_metadata_dump(&md);
340		printf("\n");
341	}
342}
343
344static void
345mirror_activate(struct gctl_req *req)
346{
347	struct g_mirror_metadata md, tmpmd;
348	const char *name, *path;
349	int error, i, nargs;
350
351	nargs = gctl_get_int(req, "nargs");
352	if (nargs < 2) {
353		gctl_error(req, "Too few arguments.");
354		return;
355	}
356	name = gctl_get_ascii(req, "arg0");
357
358	for (i = 1; i < nargs; i++) {
359		path = gctl_get_ascii(req, "arg%d", i);
360		error = g_metadata_read(path, (u_char *)&tmpmd, sizeof(tmpmd),
361		    G_MIRROR_MAGIC);
362		if (error != 0) {
363			fprintf(stderr, "Cannot read metadata from %s: %s.\n",
364			    path, strerror(error));
365			gctl_error(req, "Not fully done.");
366			continue;
367		}
368		if (mirror_metadata_decode((u_char *)&tmpmd, &md) != 0) {
369			fprintf(stderr,
370			    "MD5 hash mismatch for provider %s, skipping.\n",
371			    path);
372			gctl_error(req, "Not fully done.");
373			continue;
374		}
375		if (strcmp(md.md_name, name) != 0) {
376			fprintf(stderr,
377			    "Provider %s is not the mirror %s component.\n",
378			    path, name);
379			gctl_error(req, "Not fully done.");
380			continue;
381		}
382		md.md_dflags &= ~G_MIRROR_DISK_FLAG_INACTIVE;
383		mirror_metadata_encode(&md, (u_char *)&tmpmd);
384		error = g_metadata_store(path, (u_char *)&tmpmd, sizeof(tmpmd));
385		if (error != 0) {
386			fprintf(stderr, "Cannot write metadata from %s: %s.\n",
387			    path, strerror(error));
388			gctl_error(req, "Not fully done.");
389			continue;
390		}
391		if (verbose)
392			printf("Provider %s activated.\n", path);
393	}
394}
395
396static struct gclass *
397find_class(struct gmesh *mesh, const char *name)
398{
399	struct gclass *classp;
400
401	LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
402		if (strcmp(classp->lg_name, name) == 0)
403			return (classp);
404	}
405	return (NULL);
406}
407
408static struct ggeom *
409find_geom(struct gclass *classp, const char *name)
410{
411	struct ggeom *gp;
412
413	LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
414		if (strcmp(gp->lg_name, name) == 0)
415			return (gp);
416	}
417	return (NULL);
418}
419
420static void
421mirror_resize(struct gctl_req *req, unsigned flags __unused)
422{
423	struct gmesh mesh;
424	struct gclass *classp;
425	struct ggeom *gp;
426	struct gprovider *pp;
427	struct gconsumer *cp;
428	off_t size;
429	int error, nargs;
430	const char *name;
431	char ssize[30];
432
433	nargs = gctl_get_int(req, "nargs");
434	if (nargs < 1) {
435		gctl_error(req, "Too few arguments.");
436		return;
437	}
438	error = geom_gettree(&mesh);
439	if (error)
440		errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
441	name = gctl_get_ascii(req, "class");
442	if (name == NULL)
443		abort();
444	classp = find_class(&mesh, name);
445	if (classp == NULL)
446		errx(EXIT_FAILURE, "Class %s not found.", name);
447	name = gctl_get_ascii(req, "arg0");
448	if (name == NULL)
449		abort();
450	gp = find_geom(classp, name);
451	if (gp == NULL)
452		errx(EXIT_FAILURE, "No such geom: %s.", name);
453	pp = LIST_FIRST(&gp->lg_provider);
454	if (pp == NULL)
455		errx(EXIT_FAILURE, "Provider of geom %s not found.", name);
456	size = pp->lg_mediasize;
457	name = gctl_get_ascii(req, "size");
458	if (name == NULL)
459		errx(EXIT_FAILURE, "The size is not specified.");
460	if (*name == '*') {
461#define	CSZ(c)	((c)->lg_provider->lg_mediasize - \
462    (c)->lg_provider->lg_sectorsize)
463		/* Find the maximum possible size */
464		LIST_FOREACH(cp, &gp->lg_consumer, lg_consumer) {
465			if (CSZ(cp) > size)
466				size = CSZ(cp);
467		}
468		LIST_FOREACH(cp, &gp->lg_consumer, lg_consumer) {
469			if (CSZ(cp) < size)
470				size = CSZ(cp);
471		}
472#undef CSZ
473		if (size == pp->lg_mediasize)
474			errx(EXIT_FAILURE,
475			    "Cannot expand provider %s\n",
476			    pp->lg_name);
477	} else {
478		error = g_parse_lba(name, pp->lg_sectorsize, &size);
479		if (error)
480			errc(EXIT_FAILURE, error, "Invalid size param");
481		size *= pp->lg_sectorsize;
482	}
483	snprintf(ssize, sizeof(ssize), "%ju", (uintmax_t)size);
484	gctl_change_param(req, "size", -1, ssize);
485	geom_deletetree(&mesh);
486	gctl_issue(req);
487}
488