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$");
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	nargs = gctl_get_int(req, "nargs");
121	if (nargs < 2) {
122		gctl_error(req, "Too few arguments.");
123		return;
124	}
125	hardcode = gctl_get_int(req, "hardcode");
126
127	/*
128	 * Clear last sector first to spoil all components if device exists.
129	 */
130	for (i = 1; i < nargs; i++) {
131		name = gctl_get_ascii(req, "arg%d", i);
132		error = g_metadata_clear(name, NULL);
133		if (error != 0) {
134			gctl_error(req, "Can't store metadata on %s: %s.", name,
135			    strerror(error));
136			return;
137		}
138	}
139
140	strlcpy(md.md_magic, G_CONCAT_MAGIC, sizeof(md.md_magic));
141	md.md_version = G_CONCAT_VERSION;
142	name = gctl_get_ascii(req, "arg0");
143	strlcpy(md.md_name, name, sizeof(md.md_name));
144	md.md_id = arc4random();
145	md.md_all = nargs - 1;
146
147	/*
148	 * Ok, store metadata.
149	 */
150	for (i = 1; i < nargs; i++) {
151		name = gctl_get_ascii(req, "arg%d", i);
152		md.md_no = i - 1;
153		if (!hardcode)
154			bzero(md.md_provider, sizeof(md.md_provider));
155		else {
156			if (strncmp(name, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
157				name += sizeof(_PATH_DEV) - 1;
158			strlcpy(md.md_provider, name, sizeof(md.md_provider));
159		}
160		md.md_provsize = g_get_mediasize(name);
161		if (md.md_provsize == 0) {
162			fprintf(stderr, "Can't get mediasize of %s: %s.\n",
163			    name, strerror(errno));
164			gctl_error(req, "Not fully done.");
165			continue;
166		}
167		concat_metadata_encode(&md, sector);
168		error = g_metadata_store(name, sector, sizeof(sector));
169		if (error != 0) {
170			fprintf(stderr, "Can't store metadata on %s: %s.\n",
171			    name, strerror(error));
172			gctl_error(req, "Not fully done.");
173			continue;
174		}
175		if (verbose)
176			printf("Metadata value stored on %s.\n", name);
177	}
178}
179
180static void
181concat_clear(struct gctl_req *req)
182{
183	const char *name;
184	int error, i, nargs;
185
186	nargs = gctl_get_int(req, "nargs");
187	if (nargs < 1) {
188		gctl_error(req, "Too few arguments.");
189		return;
190	}
191
192	for (i = 0; i < nargs; i++) {
193		name = gctl_get_ascii(req, "arg%d", i);
194		error = g_metadata_clear(name, G_CONCAT_MAGIC);
195		if (error != 0) {
196			fprintf(stderr, "Can't clear metadata on %s: %s.\n",
197			    name, strerror(error));
198			gctl_error(req, "Not fully done.");
199			continue;
200		}
201		if (verbose)
202			printf("Metadata cleared on %s.\n", name);
203	}
204}
205
206static void
207concat_metadata_dump(const struct g_concat_metadata *md)
208{
209
210	printf("         Magic string: %s\n", md->md_magic);
211	printf("     Metadata version: %u\n", (u_int)md->md_version);
212	printf("          Device name: %s\n", md->md_name);
213	printf("            Device ID: %u\n", (u_int)md->md_id);
214	printf("          Disk number: %u\n", (u_int)md->md_no);
215	printf("Total number of disks: %u\n", (u_int)md->md_all);
216	printf("   Hardcoded provider: %s\n", md->md_provider);
217}
218
219static void
220concat_dump(struct gctl_req *req)
221{
222	struct g_concat_metadata md, tmpmd;
223	const char *name;
224	int error, i, nargs;
225
226	nargs = gctl_get_int(req, "nargs");
227	if (nargs < 1) {
228		gctl_error(req, "Too few arguments.");
229		return;
230	}
231
232	for (i = 0; i < nargs; i++) {
233		name = gctl_get_ascii(req, "arg%d", i);
234		error = g_metadata_read(name, (u_char *)&tmpmd, sizeof(tmpmd),
235		    G_CONCAT_MAGIC);
236		if (error != 0) {
237			fprintf(stderr, "Can't read metadata from %s: %s.\n",
238			    name, strerror(error));
239			gctl_error(req, "Not fully done.");
240			continue;
241		}
242		concat_metadata_decode((u_char *)&tmpmd, &md);
243		printf("Metadata on %s:\n", name);
244		concat_metadata_dump(&md);
245		printf("\n");
246	}
247}
248