geom_concat.c revision 330737
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: stable/10/sbin/geom/class/concat/geom_concat.c 330737 2018-03-10 04:17:01Z asomers $");
29
30#include <sys/param.h>
31#include <errno.h>
32#include <paths.h>
33#include <stdio.h>
34#include <stdlib.h>
35#include <string.h>
36#include <strings.h>
37#include <assert.h>
38#include <libgeom.h>
39#include <geom/concat/g_concat.h>
40
41#include "core/geom.h"
42#include "misc/subr.h"
43
44
45uint32_t lib_version = G_LIB_VERSION;
46uint32_t version = G_CONCAT_VERSION;
47
48static void concat_main(struct gctl_req *req, unsigned flags);
49static void concat_clear(struct gctl_req *req);
50static void concat_dump(struct gctl_req *req);
51static void concat_label(struct gctl_req *req);
52
53struct g_command class_commands[] = {
54	{ "clear", G_FLAG_VERBOSE, concat_main, G_NULL_OPTS,
55	    "[-v] prov ..."
56	},
57	{ "create", G_FLAG_VERBOSE | G_FLAG_LOADKLD, NULL, G_NULL_OPTS,
58	    "[-v] name prov ..."
59	},
60	{ "destroy", G_FLAG_VERBOSE, NULL,
61	    {
62		{ 'f', "force", NULL, G_TYPE_BOOL },
63		G_OPT_SENTINEL
64	    },
65	    "[-fv] name ..."
66	},
67	{ "dump", 0, concat_main, G_NULL_OPTS,
68	    "prov ..."
69	},
70	{ "label", G_FLAG_VERBOSE | G_FLAG_LOADKLD, concat_main,
71	    {
72		{ 'h', "hardcode", NULL, G_TYPE_BOOL },
73		G_OPT_SENTINEL
74	    },
75	    "[-hv] name prov ..."
76	},
77	{ "stop", G_FLAG_VERBOSE, NULL,
78	    {
79		{ 'f', "force", NULL, G_TYPE_BOOL },
80		G_OPT_SENTINEL
81	    },
82	    "[-fv] name ..."
83	},
84	G_CMD_SENTINEL
85};
86
87static int verbose = 0;
88
89static void
90concat_main(struct gctl_req *req, unsigned flags)
91{
92	const char *name;
93
94	if ((flags & G_FLAG_VERBOSE) != 0)
95		verbose = 1;
96
97	name = gctl_get_ascii(req, "verb");
98	if (name == NULL) {
99		gctl_error(req, "No '%s' argument.", "verb");
100		return;
101	}
102	if (strcmp(name, "label") == 0)
103		concat_label(req);
104	else if (strcmp(name, "clear") == 0)
105		concat_clear(req);
106	else if (strcmp(name, "dump") == 0)
107		concat_dump(req);
108	else
109		gctl_error(req, "Unknown command: %s.", name);
110}
111
112static void
113concat_label(struct gctl_req *req)
114{
115	struct g_concat_metadata md;
116	u_char sector[512];
117	const char *name;
118	int error, i, hardcode, nargs;
119
120	bzero(sector, sizeof(sector));
121	nargs = gctl_get_int(req, "nargs");
122	if (nargs < 2) {
123		gctl_error(req, "Too few arguments.");
124		return;
125	}
126	hardcode = gctl_get_int(req, "hardcode");
127
128	/*
129	 * Clear last sector first to spoil all components if device exists.
130	 */
131	for (i = 1; i < nargs; i++) {
132		name = gctl_get_ascii(req, "arg%d", i);
133		error = g_metadata_clear(name, NULL);
134		if (error != 0) {
135			gctl_error(req, "Can't store metadata on %s: %s.", name,
136			    strerror(error));
137			return;
138		}
139	}
140
141	strlcpy(md.md_magic, G_CONCAT_MAGIC, sizeof(md.md_magic));
142	md.md_version = G_CONCAT_VERSION;
143	name = gctl_get_ascii(req, "arg0");
144	strlcpy(md.md_name, name, sizeof(md.md_name));
145	md.md_id = arc4random();
146	md.md_all = nargs - 1;
147
148	/*
149	 * Ok, store metadata.
150	 */
151	for (i = 1; i < nargs; i++) {
152		name = gctl_get_ascii(req, "arg%d", i);
153		md.md_no = i - 1;
154		if (!hardcode)
155			bzero(md.md_provider, sizeof(md.md_provider));
156		else {
157			if (strncmp(name, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
158				name += sizeof(_PATH_DEV) - 1;
159			strlcpy(md.md_provider, name, sizeof(md.md_provider));
160		}
161		md.md_provsize = g_get_mediasize(name);
162		if (md.md_provsize == 0) {
163			fprintf(stderr, "Can't get mediasize of %s: %s.\n",
164			    name, strerror(errno));
165			gctl_error(req, "Not fully done.");
166			continue;
167		}
168		concat_metadata_encode(&md, sector);
169		error = g_metadata_store(name, sector, sizeof(sector));
170		if (error != 0) {
171			fprintf(stderr, "Can't store metadata on %s: %s.\n",
172			    name, strerror(error));
173			gctl_error(req, "Not fully done.");
174			continue;
175		}
176		if (verbose)
177			printf("Metadata value stored on %s.\n", name);
178	}
179}
180
181static void
182concat_clear(struct gctl_req *req)
183{
184	const char *name;
185	int error, i, nargs;
186
187	nargs = gctl_get_int(req, "nargs");
188	if (nargs < 1) {
189		gctl_error(req, "Too few arguments.");
190		return;
191	}
192
193	for (i = 0; i < nargs; i++) {
194		name = gctl_get_ascii(req, "arg%d", i);
195		error = g_metadata_clear(name, G_CONCAT_MAGIC);
196		if (error != 0) {
197			fprintf(stderr, "Can't clear metadata on %s: %s.\n",
198			    name, strerror(error));
199			gctl_error(req, "Not fully done.");
200			continue;
201		}
202		if (verbose)
203			printf("Metadata cleared on %s.\n", name);
204	}
205}
206
207static void
208concat_metadata_dump(const struct g_concat_metadata *md)
209{
210
211	printf("         Magic string: %s\n", md->md_magic);
212	printf("     Metadata version: %u\n", (u_int)md->md_version);
213	printf("          Device name: %s\n", md->md_name);
214	printf("            Device ID: %u\n", (u_int)md->md_id);
215	printf("          Disk number: %u\n", (u_int)md->md_no);
216	printf("Total number of disks: %u\n", (u_int)md->md_all);
217	printf("   Hardcoded provider: %s\n", md->md_provider);
218}
219
220static void
221concat_dump(struct gctl_req *req)
222{
223	struct g_concat_metadata md, tmpmd;
224	const char *name;
225	int error, i, nargs;
226
227	nargs = gctl_get_int(req, "nargs");
228	if (nargs < 1) {
229		gctl_error(req, "Too few arguments.");
230		return;
231	}
232
233	for (i = 0; i < nargs; i++) {
234		name = gctl_get_ascii(req, "arg%d", i);
235		error = g_metadata_read(name, (u_char *)&tmpmd, sizeof(tmpmd),
236		    G_CONCAT_MAGIC);
237		if (error != 0) {
238			fprintf(stderr, "Can't read metadata from %s: %s.\n",
239			    name, strerror(error));
240			gctl_error(req, "Not fully done.");
241			continue;
242		}
243		concat_metadata_decode((u_char *)&tmpmd, &md);
244		printf("Metadata on %s:\n", name);
245		concat_metadata_dump(&md);
246		printf("\n");
247	}
248}
249