xref: /freebsd/lib/geom/stripe/geom_stripe.c (revision 5e3190f700637fcfc1a52daeaa4a031fdd2557c7)
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/cdefs.h>
30 #include <sys/param.h>
31 #include <errno.h>
32 #include <paths.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <stdint.h>
36 #include <string.h>
37 #include <strings.h>
38 #include <assert.h>
39 #include <libgeom.h>
40 #include <geom/stripe/g_stripe.h>
41 
42 #include "core/geom.h"
43 #include "misc/subr.h"
44 
45 
46 uint32_t lib_version = G_LIB_VERSION;
47 uint32_t version = G_STRIPE_VERSION;
48 
49 #define	GSTRIPE_STRIPESIZE	"65536"
50 
51 static void stripe_main(struct gctl_req *req, unsigned flags);
52 static void stripe_clear(struct gctl_req *req);
53 static void stripe_dump(struct gctl_req *req);
54 static void stripe_label(struct gctl_req *req);
55 
56 struct g_command class_commands[] = {
57 	{ "clear", G_FLAG_VERBOSE, stripe_main, G_NULL_OPTS,
58 	    "[-v] prov ..."
59 	},
60 	{ "create", G_FLAG_VERBOSE | G_FLAG_LOADKLD, NULL,
61 	    {
62 		{ 's', "stripesize", GSTRIPE_STRIPESIZE, G_TYPE_NUMBER },
63 		G_OPT_SENTINEL
64 	    },
65 	    "[-v] [-s stripesize] name prov prov ..."
66 	},
67 	{ "destroy", G_FLAG_VERBOSE, NULL,
68 	    {
69 		{ 'f', "force", NULL, G_TYPE_BOOL },
70 		G_OPT_SENTINEL
71 	    },
72 	    "[-fv] name ..."
73 	},
74 	{ "dump", 0, stripe_main, G_NULL_OPTS,
75 	    "prov ..."
76 	},
77 	{ "label", G_FLAG_VERBOSE | G_FLAG_LOADKLD, stripe_main,
78 	    {
79 		{ 'h', "hardcode", NULL, G_TYPE_BOOL },
80 		{ 's', "stripesize", GSTRIPE_STRIPESIZE, G_TYPE_NUMBER },
81 		G_OPT_SENTINEL
82 	    },
83 	    "[-hv] [-s stripesize] name prov prov ..."
84 	},
85 	{ "stop", G_FLAG_VERBOSE, NULL,
86 	    {
87 		{ 'f', "force", NULL, G_TYPE_BOOL },
88 		G_OPT_SENTINEL
89 	    },
90 	    "[-fv] name ..."
91 	},
92 	G_CMD_SENTINEL
93 };
94 
95 static int verbose = 0;
96 
97 static void
98 stripe_main(struct gctl_req *req, unsigned flags)
99 {
100 	const char *name;
101 
102 	if ((flags & G_FLAG_VERBOSE) != 0)
103 		verbose = 1;
104 
105 	name = gctl_get_ascii(req, "verb");
106 	if (name == NULL) {
107 		gctl_error(req, "No '%s' argument.", "verb");
108 		return;
109 	}
110 	if (strcmp(name, "label") == 0)
111 		stripe_label(req);
112 	else if (strcmp(name, "clear") == 0)
113 		stripe_clear(req);
114 	else if (strcmp(name, "dump") == 0)
115 		stripe_dump(req);
116 	else
117 		gctl_error(req, "Unknown command: %s.", name);
118 }
119 
120 static void
121 stripe_label(struct gctl_req *req)
122 {
123 	struct g_stripe_metadata md;
124 	intmax_t stripesize;
125 	off_t compsize, msize;
126 	u_char sector[512];
127 	unsigned ssize, secsize;
128 	const char *name;
129 	int error, i, nargs, hardcode;
130 
131 	bzero(sector, sizeof(sector));
132 	nargs = gctl_get_int(req, "nargs");
133 	if (nargs < 3) {
134 		gctl_error(req, "Too few arguments.");
135 		return;
136 	}
137 	hardcode = gctl_get_int(req, "hardcode");
138 
139 	/*
140 	 * Clear last sector first to spoil all components if device exists.
141 	 */
142 	compsize = 0;
143 	secsize = 0;
144 	for (i = 1; i < nargs; i++) {
145 		name = gctl_get_ascii(req, "arg%d", i);
146 		msize = g_get_mediasize(name);
147 		ssize = g_get_sectorsize(name);
148 		if (msize == 0 || ssize == 0) {
149 			gctl_error(req, "Can't get informations about %s: %s.",
150 			    name, strerror(errno));
151 			return;
152 		}
153 		msize -= ssize;
154 		if (compsize == 0 || (compsize > 0 && msize < compsize))
155 			compsize = msize;
156 		if (secsize == 0)
157 			secsize = ssize;
158 		else
159 			secsize = g_lcm(secsize, ssize);
160 
161 		error = g_metadata_clear(name, NULL);
162 		if (error != 0) {
163 			gctl_error(req, "Can't store metadata on %s: %s.", name,
164 			    strerror(error));
165 			return;
166 		}
167 	}
168 
169 	strlcpy(md.md_magic, G_STRIPE_MAGIC, sizeof(md.md_magic));
170 	md.md_version = G_STRIPE_VERSION;
171 	name = gctl_get_ascii(req, "arg0");
172 	strlcpy(md.md_name, name, sizeof(md.md_name));
173 	md.md_id = arc4random();
174 	md.md_all = nargs - 1;
175 	stripesize = gctl_get_intmax(req, "stripesize");
176 	if ((stripesize % secsize) != 0) {
177 		gctl_error(req, "Stripesize should be multiple of %u.",
178 		    secsize);
179 		return;
180 	}
181 	md.md_stripesize = stripesize;
182 
183 	/*
184 	 * Ok, store metadata.
185 	 */
186 	for (i = 1; i < nargs; i++) {
187 		name = gctl_get_ascii(req, "arg%d", i);
188 		msize = g_get_mediasize(name);
189 		ssize = g_get_sectorsize(name);
190 		if (compsize < msize - ssize) {
191 			fprintf(stderr,
192 			    "warning: %s: only %jd bytes from %jd bytes used.\n",
193 			    name, (intmax_t)compsize, (intmax_t)(msize - ssize));
194 		}
195 
196 		md.md_no = i - 1;
197 		md.md_provsize = msize;
198 		if (!hardcode)
199 			bzero(md.md_provider, sizeof(md.md_provider));
200 		else {
201 			if (strncmp(name, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
202 				name += sizeof(_PATH_DEV) - 1;
203 			strlcpy(md.md_provider, name, sizeof(md.md_provider));
204 		}
205 		stripe_metadata_encode(&md, sector);
206 		error = g_metadata_store(name, sector, sizeof(sector));
207 		if (error != 0) {
208 			fprintf(stderr, "Can't store metadata on %s: %s.\n",
209 			    name, strerror(error));
210 			gctl_error(req, "Not fully done.");
211 			continue;
212 		}
213 		if (verbose)
214 			printf("Metadata value stored on %s.\n", name);
215 	}
216 }
217 
218 static void
219 stripe_clear(struct gctl_req *req)
220 {
221 	const char *name;
222 	int error, i, nargs;
223 
224 	nargs = gctl_get_int(req, "nargs");
225 	if (nargs < 1) {
226 		gctl_error(req, "Too few arguments.");
227 		return;
228 	}
229 
230 	for (i = 0; i < nargs; i++) {
231 		name = gctl_get_ascii(req, "arg%d", i);
232 		error = g_metadata_clear(name, G_STRIPE_MAGIC);
233 		if (error != 0) {
234 			fprintf(stderr, "Can't clear metadata on %s: %s.\n",
235 			    name, strerror(error));
236 			gctl_error(req, "Not fully done.");
237 			continue;
238 		}
239 		if (verbose)
240 			printf("Metadata cleared on %s.\n", name);
241 	}
242 }
243 
244 static void
245 stripe_metadata_dump(const struct g_stripe_metadata *md)
246 {
247 
248 	printf("         Magic string: %s\n", md->md_magic);
249 	printf("     Metadata version: %u\n", (u_int)md->md_version);
250 	printf("          Device name: %s\n", md->md_name);
251 	printf("            Device ID: %u\n", (u_int)md->md_id);
252 	printf("          Disk number: %u\n", (u_int)md->md_no);
253 	printf("Total number of disks: %u\n", (u_int)md->md_all);
254 	printf("          Stripe size: %u\n", (u_int)md->md_stripesize);
255 	printf("   Hardcoded provider: %s\n", md->md_provider);
256 }
257 
258 static void
259 stripe_dump(struct gctl_req *req)
260 {
261 	struct g_stripe_metadata md, tmpmd;
262 	const char *name;
263 	int error, i, nargs;
264 
265 	nargs = gctl_get_int(req, "nargs");
266 	if (nargs < 1) {
267 		gctl_error(req, "Too few arguments.");
268 		return;
269 	}
270 
271 	for (i = 0; i < nargs; i++) {
272 		name = gctl_get_ascii(req, "arg%d", i);
273 		error = g_metadata_read(name, (u_char *)&tmpmd, sizeof(tmpmd),
274 		    G_STRIPE_MAGIC);
275 		if (error != 0) {
276 			fprintf(stderr, "Can't read metadata from %s: %s.\n",
277 			    name, strerror(error));
278 			gctl_error(req, "Not fully done.");
279 			continue;
280 		}
281 		stripe_metadata_decode((u_char *)&tmpmd, &md);
282 		printf("Metadata on %s:\n", name);
283 		stripe_metadata_dump(&md);
284 		printf("\n");
285 	}
286 }
287