1/*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2004-2005 Pawel Jakub Dawidek <pjd@FreeBSD.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include <sys/param.h>
30#include <errno.h>
31#include <paths.h>
32#include <stdio.h>
33#include <stdlib.h>
34#include <stdint.h>
35#include <string.h>
36#include <strings.h>
37#include <assert.h>
38#include <libgeom.h>
39#include <geom/stripe/g_stripe.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_STRIPE_VERSION;
47
48#define	GSTRIPE_STRIPESIZE	"65536"
49
50static void stripe_main(struct gctl_req *req, unsigned flags);
51static void stripe_clear(struct gctl_req *req);
52static void stripe_dump(struct gctl_req *req);
53static void stripe_label(struct gctl_req *req);
54
55struct g_command class_commands[] = {
56	{ "clear", G_FLAG_VERBOSE, stripe_main, G_NULL_OPTS,
57	    "[-v] prov ..."
58	},
59	{ "create", G_FLAG_VERBOSE | G_FLAG_LOADKLD, NULL,
60	    {
61		{ 's', "stripesize", GSTRIPE_STRIPESIZE, G_TYPE_NUMBER },
62		G_OPT_SENTINEL
63	    },
64	    "[-v] [-s stripesize] name prov prov ..."
65	},
66	{ "destroy", G_FLAG_VERBOSE, NULL,
67	    {
68		{ 'f', "force", NULL, G_TYPE_BOOL },
69		G_OPT_SENTINEL
70	    },
71	    "[-fv] name ..."
72	},
73	{ "dump", 0, stripe_main, G_NULL_OPTS,
74	    "prov ..."
75	},
76	{ "label", G_FLAG_VERBOSE | G_FLAG_LOADKLD, stripe_main,
77	    {
78		{ 'h', "hardcode", NULL, G_TYPE_BOOL },
79		{ 's', "stripesize", GSTRIPE_STRIPESIZE, G_TYPE_NUMBER },
80		G_OPT_SENTINEL
81	    },
82	    "[-hv] [-s stripesize] name prov prov ..."
83	},
84	{ "stop", G_FLAG_VERBOSE, NULL,
85	    {
86		{ 'f', "force", NULL, G_TYPE_BOOL },
87		G_OPT_SENTINEL
88	    },
89	    "[-fv] name ..."
90	},
91	G_CMD_SENTINEL
92};
93
94static int verbose = 0;
95
96static void
97stripe_main(struct gctl_req *req, unsigned flags)
98{
99	const char *name;
100
101	if ((flags & G_FLAG_VERBOSE) != 0)
102		verbose = 1;
103
104	name = gctl_get_ascii(req, "verb");
105	if (name == NULL) {
106		gctl_error(req, "No '%s' argument.", "verb");
107		return;
108	}
109	if (strcmp(name, "label") == 0)
110		stripe_label(req);
111	else if (strcmp(name, "clear") == 0)
112		stripe_clear(req);
113	else if (strcmp(name, "dump") == 0)
114		stripe_dump(req);
115	else
116		gctl_error(req, "Unknown command: %s.", name);
117}
118
119static void
120stripe_label(struct gctl_req *req)
121{
122	struct g_stripe_metadata md;
123	intmax_t stripesize;
124	off_t compsize, msize;
125	u_char sector[512];
126	unsigned ssize, secsize;
127	const char *name;
128	int error, i, nargs, hardcode;
129
130	bzero(sector, sizeof(sector));
131	nargs = gctl_get_int(req, "nargs");
132	if (nargs < 3) {
133		gctl_error(req, "Too few arguments.");
134		return;
135	}
136	hardcode = gctl_get_int(req, "hardcode");
137
138	/*
139	 * Clear last sector first to spoil all components if device exists.
140	 */
141	compsize = 0;
142	secsize = 0;
143	for (i = 1; i < nargs; i++) {
144		name = gctl_get_ascii(req, "arg%d", i);
145		msize = g_get_mediasize(name);
146		ssize = g_get_sectorsize(name);
147		if (msize == 0 || ssize == 0) {
148			gctl_error(req, "Can't get informations about %s: %s.",
149			    name, strerror(errno));
150			return;
151		}
152		msize -= ssize;
153		if (compsize == 0 || (compsize > 0 && msize < compsize))
154			compsize = msize;
155		if (secsize == 0)
156			secsize = ssize;
157		else
158			secsize = g_lcm(secsize, ssize);
159
160		error = g_metadata_clear(name, NULL);
161		if (error != 0) {
162			gctl_error(req, "Can't store metadata on %s: %s.", name,
163			    strerror(error));
164			return;
165		}
166	}
167
168	strlcpy(md.md_magic, G_STRIPE_MAGIC, sizeof(md.md_magic));
169	md.md_version = G_STRIPE_VERSION;
170	name = gctl_get_ascii(req, "arg0");
171	strlcpy(md.md_name, name, sizeof(md.md_name));
172	md.md_id = arc4random();
173	md.md_all = nargs - 1;
174	stripesize = gctl_get_intmax(req, "stripesize");
175	if ((stripesize % secsize) != 0) {
176		gctl_error(req, "Stripesize should be multiple of %u.",
177		    secsize);
178		return;
179	}
180	md.md_stripesize = stripesize;
181
182	/*
183	 * Ok, store metadata.
184	 */
185	for (i = 1; i < nargs; i++) {
186		name = gctl_get_ascii(req, "arg%d", i);
187		msize = g_get_mediasize(name);
188		ssize = g_get_sectorsize(name);
189		if (compsize < msize - ssize) {
190			fprintf(stderr,
191			    "warning: %s: only %jd bytes from %jd bytes used.\n",
192			    name, (intmax_t)compsize, (intmax_t)(msize - ssize));
193		}
194
195		md.md_no = i - 1;
196		md.md_provsize = msize;
197		if (!hardcode)
198			bzero(md.md_provider, sizeof(md.md_provider));
199		else {
200			if (strncmp(name, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
201				name += sizeof(_PATH_DEV) - 1;
202			strlcpy(md.md_provider, name, sizeof(md.md_provider));
203		}
204		stripe_metadata_encode(&md, sector);
205		error = g_metadata_store(name, sector, sizeof(sector));
206		if (error != 0) {
207			fprintf(stderr, "Can't store metadata on %s: %s.\n",
208			    name, strerror(error));
209			gctl_error(req, "Not fully done.");
210			continue;
211		}
212		if (verbose)
213			printf("Metadata value stored on %s.\n", name);
214	}
215}
216
217static void
218stripe_clear(struct gctl_req *req)
219{
220	const char *name;
221	int error, i, nargs;
222
223	nargs = gctl_get_int(req, "nargs");
224	if (nargs < 1) {
225		gctl_error(req, "Too few arguments.");
226		return;
227	}
228
229	for (i = 0; i < nargs; i++) {
230		name = gctl_get_ascii(req, "arg%d", i);
231		error = g_metadata_clear(name, G_STRIPE_MAGIC);
232		if (error != 0) {
233			fprintf(stderr, "Can't clear metadata on %s: %s.\n",
234			    name, strerror(error));
235			gctl_error(req, "Not fully done.");
236			continue;
237		}
238		if (verbose)
239			printf("Metadata cleared on %s.\n", name);
240	}
241}
242
243static void
244stripe_metadata_dump(const struct g_stripe_metadata *md)
245{
246
247	printf("         Magic string: %s\n", md->md_magic);
248	printf("     Metadata version: %u\n", (u_int)md->md_version);
249	printf("          Device name: %s\n", md->md_name);
250	printf("            Device ID: %u\n", (u_int)md->md_id);
251	printf("          Disk number: %u\n", (u_int)md->md_no);
252	printf("Total number of disks: %u\n", (u_int)md->md_all);
253	printf("          Stripe size: %u\n", (u_int)md->md_stripesize);
254	printf("   Hardcoded provider: %s\n", md->md_provider);
255}
256
257static void
258stripe_dump(struct gctl_req *req)
259{
260	struct g_stripe_metadata md, tmpmd;
261	const char *name;
262	int error, i, nargs;
263
264	nargs = gctl_get_int(req, "nargs");
265	if (nargs < 1) {
266		gctl_error(req, "Too few arguments.");
267		return;
268	}
269
270	for (i = 0; i < nargs; i++) {
271		name = gctl_get_ascii(req, "arg%d", i);
272		error = g_metadata_read(name, (u_char *)&tmpmd, sizeof(tmpmd),
273		    G_STRIPE_MAGIC);
274		if (error != 0) {
275			fprintf(stderr, "Can't read metadata from %s: %s.\n",
276			    name, strerror(error));
277			gctl_error(req, "Not fully done.");
278			continue;
279		}
280		stripe_metadata_decode((u_char *)&tmpmd, &md);
281		printf("Metadata on %s:\n", name);
282		stripe_metadata_dump(&md);
283		printf("\n");
284	}
285}
286