xref: /freebsd/lib/geom/raid3/geom_raid3.c (revision af23369a6deaaeb612ab266eb88b8bb8d560c322)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <errno.h>
34 #include <paths.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <stdint.h>
38 #include <string.h>
39 #include <strings.h>
40 #include <assert.h>
41 #include <libgeom.h>
42 #include <geom/raid3/g_raid3.h>
43 #include <core/geom.h>
44 #include <misc/subr.h>
45 
46 
47 uint32_t lib_version = G_LIB_VERSION;
48 uint32_t version = G_RAID3_VERSION;
49 
50 static void raid3_main(struct gctl_req *req, unsigned f);
51 static void raid3_clear(struct gctl_req *req);
52 static void raid3_dump(struct gctl_req *req);
53 static void raid3_label(struct gctl_req *req);
54 
55 struct g_command class_commands[] = {
56 	{ "clear", G_FLAG_VERBOSE, raid3_main, G_NULL_OPTS,
57 	    "[-v] prov ..."
58 	},
59 	{ "configure", G_FLAG_VERBOSE, NULL,
60 	    {
61 		{ 'a', "autosync", NULL, G_TYPE_BOOL },
62 		{ 'd', "dynamic", NULL, G_TYPE_BOOL },
63 		{ 'f', "failsync", NULL, G_TYPE_BOOL },
64 		{ 'F', "nofailsync", NULL, G_TYPE_BOOL },
65 		{ 'h', "hardcode", NULL, G_TYPE_BOOL },
66 		{ 'n', "noautosync", NULL, G_TYPE_BOOL },
67 		{ 'r', "round_robin", NULL, G_TYPE_BOOL },
68 		{ 'R', "noround_robin", NULL, G_TYPE_BOOL },
69 		{ 'w', "verify", NULL, G_TYPE_BOOL },
70 		{ 'W', "noverify", NULL, G_TYPE_BOOL },
71 		G_OPT_SENTINEL
72 	    },
73 	    "[-adfFhnrRvwW] name"
74 	},
75 	{ "dump", 0, raid3_main, G_NULL_OPTS,
76 	    "prov ..."
77 	},
78 	{ "insert", G_FLAG_VERBOSE, NULL,
79 	    {
80 		{ 'h', "hardcode", NULL, G_TYPE_BOOL },
81 		{ 'n', "number", G_VAL_OPTIONAL, G_TYPE_NUMBER },
82 		G_OPT_SENTINEL
83 	    },
84 	    "[-hv] <-n number> name prov"
85 	},
86 	{ "label", G_FLAG_VERBOSE, raid3_main,
87 	    {
88 		{ 'h', "hardcode", NULL, G_TYPE_BOOL },
89 		{ 'F', "nofailsync", NULL, G_TYPE_BOOL },
90 		{ 'n', "noautosync", NULL, G_TYPE_BOOL },
91 		{ 'r', "round_robin", NULL, G_TYPE_BOOL },
92 		{ 's', "sectorsize", "0", G_TYPE_NUMBER },
93 		{ 'w', "verify", NULL, G_TYPE_BOOL },
94 		G_OPT_SENTINEL
95 	    },
96 	    "[-hFnrvw] [-s blocksize] name prov prov prov ..."
97 	},
98 	{ "rebuild", G_FLAG_VERBOSE, NULL, G_NULL_OPTS,
99 	    "[-v] name prov"
100 	},
101 	{ "remove", G_FLAG_VERBOSE, NULL,
102 	    {
103 		{ 'n', "number", NULL, G_TYPE_NUMBER },
104 		G_OPT_SENTINEL
105 	    },
106 	    "[-v] <-n number> name"
107 	},
108 	{ "stop", G_FLAG_VERBOSE, NULL,
109 	    {
110 		{ 'f', "force", NULL, G_TYPE_BOOL },
111 		G_OPT_SENTINEL
112 	    },
113 	    "[-fv] name ..."
114 	},
115 	G_CMD_SENTINEL
116 };
117 
118 static int verbose = 0;
119 
120 static void
121 raid3_main(struct gctl_req *req, unsigned flags)
122 {
123 	const char *name;
124 
125 	if ((flags & G_FLAG_VERBOSE) != 0)
126 		verbose = 1;
127 
128 	name = gctl_get_ascii(req, "verb");
129 	if (name == NULL) {
130 		gctl_error(req, "No '%s' argument.", "verb");
131 		return;
132 	}
133 	if (strcmp(name, "label") == 0)
134 		raid3_label(req);
135 	else if (strcmp(name, "clear") == 0)
136 		raid3_clear(req);
137 	else if (strcmp(name, "dump") == 0)
138 		raid3_dump(req);
139 	else
140 		gctl_error(req, "Unknown command: %s.", name);
141 }
142 
143 static void
144 raid3_label(struct gctl_req *req)
145 {
146 	struct g_raid3_metadata md;
147 	u_char sector[512];
148 	const char *str;
149 	unsigned sectorsize, ssize;
150 	off_t mediasize, msize;
151 	int hardcode, round_robin, verify;
152 	int error, i, nargs;
153 
154 	bzero(sector, sizeof(sector));
155 	nargs = gctl_get_int(req, "nargs");
156 	if (nargs < 4) {
157 		gctl_error(req, "Too few arguments.");
158 		return;
159 	}
160 	if (bitcount32(nargs - 2) != 1) {
161 		gctl_error(req, "Invalid number of components.");
162 		return;
163 	}
164 
165 	strlcpy(md.md_magic, G_RAID3_MAGIC, sizeof(md.md_magic));
166 	md.md_version = G_RAID3_VERSION;
167 	str = gctl_get_ascii(req, "arg0");
168 	strlcpy(md.md_name, str, sizeof(md.md_name));
169 	md.md_id = arc4random();
170 	md.md_all = nargs - 1;
171 	md.md_mflags = 0;
172 	md.md_dflags = 0;
173 	md.md_genid = 0;
174 	md.md_syncid = 1;
175 	md.md_sync_offset = 0;
176 	if (gctl_get_int(req, "noautosync"))
177 		md.md_mflags |= G_RAID3_DEVICE_FLAG_NOAUTOSYNC;
178 	if (gctl_get_int(req, "nofailsync"))
179 		md.md_mflags |= G_RAID3_DEVICE_FLAG_NOFAILSYNC;
180 	round_robin = gctl_get_int(req, "round_robin");
181 	if (round_robin)
182 		md.md_mflags |= G_RAID3_DEVICE_FLAG_ROUND_ROBIN;
183 	verify = gctl_get_int(req, "verify");
184 	if (verify)
185 		md.md_mflags |= G_RAID3_DEVICE_FLAG_VERIFY;
186 	if (round_robin && verify) {
187 		gctl_error(req, "Both '%c' and '%c' options given.", 'r', 'w');
188 		return;
189 	}
190 	hardcode = gctl_get_int(req, "hardcode");
191 
192 	/*
193 	 * Calculate sectorsize by finding least common multiple from
194 	 * sectorsizes of every disk and find the smallest mediasize.
195 	 */
196 	mediasize = 0;
197 	sectorsize = gctl_get_intmax(req, "sectorsize");
198 	for (i = 1; i < nargs; i++) {
199 		str = gctl_get_ascii(req, "arg%d", i);
200 		msize = g_get_mediasize(str);
201 		ssize = g_get_sectorsize(str);
202 		if (msize == 0 || ssize == 0) {
203 			gctl_error(req, "Can't get informations about %s: %s.",
204 			    str, strerror(errno));
205 			return;
206 		}
207 		msize -= ssize;
208 		if (mediasize == 0 || (mediasize > 0 && msize < mediasize))
209 			mediasize = msize;
210 		if (sectorsize == 0)
211 			sectorsize = ssize;
212 		else
213 			sectorsize = g_lcm(sectorsize, ssize);
214 	}
215 	md.md_mediasize = mediasize * (nargs - 2);
216 	md.md_sectorsize = sectorsize * (nargs - 2);
217 	md.md_mediasize -= (md.md_mediasize % md.md_sectorsize);
218 
219 	if (md.md_sectorsize > MAXPHYS) {
220 		gctl_error(req, "The blocksize is too big.");
221 		return;
222 	}
223 
224 	/*
225 	 * Clear last sector first, to spoil all components if device exists.
226 	 */
227 	for (i = 1; i < nargs; i++) {
228 		str = gctl_get_ascii(req, "arg%d", i);
229 		error = g_metadata_clear(str, NULL);
230 		if (error != 0) {
231 			gctl_error(req, "Can't store metadata on %s: %s.", str,
232 			    strerror(error));
233 			return;
234 		}
235 	}
236 
237 	/*
238 	 * Ok, store metadata (use disk number as priority).
239 	 */
240 	for (i = 1; i < nargs; i++) {
241 		str = gctl_get_ascii(req, "arg%d", i);
242 		msize = g_get_mediasize(str);
243 		ssize = g_get_sectorsize(str);
244 		if (mediasize < msize - ssize) {
245 			fprintf(stderr,
246 			    "warning: %s: only %jd bytes from %jd bytes used.\n",
247 			    str, (intmax_t)mediasize, (intmax_t)(msize - ssize));
248 		}
249 
250 		md.md_no = i - 1;
251 		md.md_provsize = msize;
252 		if (!hardcode)
253 			bzero(md.md_provider, sizeof(md.md_provider));
254 		else {
255 			if (strncmp(str, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
256 				str += sizeof(_PATH_DEV) - 1;
257 			strlcpy(md.md_provider, str, sizeof(md.md_provider));
258 		}
259 		if (verify && md.md_no == md.md_all - 1) {
260 			/*
261 			 * In "verify" mode, force synchronization of parity
262 			 * component on start.
263 			 */
264 			md.md_syncid = 0;
265 		}
266 		raid3_metadata_encode(&md, sector);
267 		error = g_metadata_store(str, sector, sizeof(sector));
268 		if (error != 0) {
269 			fprintf(stderr, "Can't store metadata on %s: %s.\n",
270 			    str, strerror(error));
271 			gctl_error(req, "Not fully done.");
272 			continue;
273 		}
274 		if (verbose)
275 			printf("Metadata value stored on %s.\n", str);
276 	}
277 }
278 
279 static void
280 raid3_clear(struct gctl_req *req)
281 {
282 	const char *name;
283 	int error, i, nargs;
284 
285 	nargs = gctl_get_int(req, "nargs");
286 	if (nargs < 1) {
287 		gctl_error(req, "Too few arguments.");
288 		return;
289 	}
290 
291 	for (i = 0; i < nargs; i++) {
292 		name = gctl_get_ascii(req, "arg%d", i);
293 		error = g_metadata_clear(name, G_RAID3_MAGIC);
294 		if (error != 0) {
295 			fprintf(stderr, "Can't clear metadata on %s: %s.\n",
296 			    name, strerror(error));
297 			gctl_error(req, "Not fully done.");
298 			continue;
299 		}
300 		if (verbose)
301 			printf("Metadata cleared on %s.\n", name);
302 	}
303 }
304 
305 static void
306 raid3_dump(struct gctl_req *req)
307 {
308 	struct g_raid3_metadata md, tmpmd;
309 	const char *name;
310 	int error, i, nargs;
311 
312 	nargs = gctl_get_int(req, "nargs");
313 	if (nargs < 1) {
314 		gctl_error(req, "Too few arguments.");
315 		return;
316 	}
317 
318 	for (i = 0; i < nargs; i++) {
319 		name = gctl_get_ascii(req, "arg%d", i);
320 		error = g_metadata_read(name, (u_char *)&tmpmd, sizeof(tmpmd),
321 		    G_RAID3_MAGIC);
322 		if (error != 0) {
323 			fprintf(stderr, "Can't read metadata from %s: %s.\n",
324 			    name, strerror(error));
325 			gctl_error(req, "Not fully done.");
326 			continue;
327 		}
328 		if (raid3_metadata_decode((u_char *)&tmpmd, &md) != 0) {
329 			fprintf(stderr, "MD5 hash mismatch for %s, skipping.\n",
330 			    name);
331 			gctl_error(req, "Not fully done.");
332 			continue;
333 		}
334 		printf("Metadata on %s:\n", name);
335 		raid3_metadata_dump(&md);
336 		printf("\n");
337 	}
338 }
339