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