xref: /freebsd/lib/geom/virstor/geom_virstor.c (revision 02e9120893770924227138ba49df1edb3896112a)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2005 Ivan Voras <ivoras@freebsd.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/param.h>
29 #include <errno.h>
30 #include <paths.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <stdint.h>
34 #include <string.h>
35 #include <strings.h>
36 #include <fcntl.h>
37 #include <unistd.h>
38 #include <libgeom.h>
39 #include <err.h>
40 #include <assert.h>
41 
42 #include <core/geom.h>
43 #include <misc/subr.h>
44 
45 #include <geom/virstor/g_virstor_md.h>
46 #include <geom/virstor/g_virstor.h>
47 
48 uint32_t lib_version = G_LIB_VERSION;
49 uint32_t version = G_VIRSTOR_VERSION;
50 
51 #define	GVIRSTOR_CHUNK_SIZE	"4M"
52 #define	GVIRSTOR_VIR_SIZE	"2T"
53 
54 #if G_LIB_VERSION == 1
55 /* Support RELENG_6 */
56 #define G_TYPE_BOOL G_TYPE_NONE
57 #endif
58 
59 /*
60  * virstor_main gets called by the geom(8) utility
61  */
62 static void virstor_main(struct gctl_req *req, unsigned flags);
63 
64 struct g_command class_commands[] = {
65 	{ "clear", G_FLAG_VERBOSE, virstor_main, G_NULL_OPTS,
66 	    "[-v] prov ..."
67 	},
68 	{ "dump", 0, virstor_main, G_NULL_OPTS,
69 	    "prov ..."
70 	},
71 	{ "label", G_FLAG_VERBOSE | G_FLAG_LOADKLD, virstor_main,
72 	    {
73 		{ 'h', "hardcode", NULL, G_TYPE_BOOL},
74 		{ 'm', "chunk_size", GVIRSTOR_CHUNK_SIZE, G_TYPE_NUMBER},
75 		{ 's', "vir_size", GVIRSTOR_VIR_SIZE, G_TYPE_NUMBER},
76 		G_OPT_SENTINEL
77 	    },
78 	    "[-h] [-v] [-m chunk_size] [-s vir_size] name provider0 [provider1 ...]"
79 	},
80 	{ "destroy", G_FLAG_VERBOSE, NULL,
81 	    {
82 		{ 'f', "force", NULL, G_TYPE_BOOL},
83 		G_OPT_SENTINEL
84 	    },
85 	    "[-fv] name ..."
86 	},
87 	{ "stop", G_FLAG_VERBOSE, NULL,
88 	    {
89 		{ 'f', "force", NULL, G_TYPE_BOOL},
90 		G_OPT_SENTINEL
91 	    },
92 	    "[-fv] name ... (alias for \"destroy\")"
93 	},
94 	{ "add", G_FLAG_VERBOSE, NULL,
95 	    {
96 		{ 'h', "hardcode", NULL, G_TYPE_BOOL},
97 		G_OPT_SENTINEL
98 	    },
99 	    "[-vh] name prov [prov ...]"
100 	},
101 	{ "remove", G_FLAG_VERBOSE, NULL, G_NULL_OPTS,
102 	    "[-v] name ..."
103 	},
104 	G_CMD_SENTINEL
105 };
106 
107 static int verbose = 0;
108 
109 /* Helper functions' declarations */
110 static void virstor_clear(struct gctl_req *req);
111 static void virstor_dump(struct gctl_req *req);
112 static void virstor_label(struct gctl_req *req);
113 
114 /* Dispatcher function (no real work done here, only verbose flag recorder) */
115 static void
116 virstor_main(struct gctl_req *req, unsigned flags)
117 {
118 	const char *name;
119 
120 	if ((flags & G_FLAG_VERBOSE) != 0)
121 		verbose = 1;
122 
123 	name = gctl_get_ascii(req, "verb");
124 	if (name == NULL) {
125 		gctl_error(req, "No '%s' argument.", "verb");
126 		return;
127 	}
128 	if (strcmp(name, "label") == 0)
129 		virstor_label(req);
130 	else if (strcmp(name, "clear") == 0)
131 		virstor_clear(req);
132 	else if (strcmp(name, "dump") == 0)
133 		virstor_dump(req);
134 	else
135 		gctl_error(req, "%s: Unknown command: %s.", __func__, name);
136 
137 	/* No CTASSERT in userland
138 	CTASSERT(VIRSTOR_MAP_BLOCK_ENTRIES*VIRSTOR_MAP_ENTRY_SIZE == MAXPHYS);
139 	*/
140 }
141 
142 /*
143  * Labels a new geom Meaning: parses and checks the parameters, calculates &
144  * writes metadata to the relevant providers so when the next round of
145  * "tasting" comes (which will be just after the provider(s) are closed) geom
146  * can be instantiated with the tasted metadata.
147  */
148 static void
149 virstor_label(struct gctl_req *req)
150 {
151 	struct g_virstor_metadata md;
152 	off_t msize;
153 	unsigned char *sect;
154 	unsigned int i;
155 	size_t ssize, secsize;
156 	const char *name;
157 	char param[32];
158 	int hardcode, nargs, error;
159 	struct virstor_map_entry *map;
160 	size_t total_chunks;	/* We'll run out of memory if
161 				   this needs to be bigger. */
162 	unsigned int map_chunks; /* Chunks needed by the map (map size). */
163 	size_t map_size;	/* In bytes. */
164 	ssize_t written;
165 	int fd;
166 
167 	nargs = gctl_get_int(req, "nargs");
168 	if (nargs < 2) {
169 		gctl_error(req, "Too few arguments (%d): expecting: name "
170 		    "provider0 [provider1 ...]", nargs);
171 		return;
172 	}
173 
174 	hardcode = gctl_get_int(req, "hardcode");
175 
176 	/*
177 	 * Initialize constant parts of metadata: magic signature, version,
178 	 * name.
179 	 */
180 	bzero(&md, sizeof(md));
181 	strlcpy(md.md_magic, G_VIRSTOR_MAGIC, sizeof(md.md_magic));
182 	md.md_version = G_VIRSTOR_VERSION;
183 	name = gctl_get_ascii(req, "arg0");
184 	if (name == NULL) {
185 		gctl_error(req, "No 'arg%u' argument.", 0);
186 		return;
187 	}
188 	strlcpy(md.md_name, name, sizeof(md.md_name));
189 
190 	md.md_virsize = (off_t)gctl_get_intmax(req, "vir_size");
191 	md.md_chunk_size = gctl_get_intmax(req, "chunk_size");
192 	md.md_count = nargs - 1;
193 
194 	if (md.md_virsize == 0 || md.md_chunk_size == 0) {
195 		gctl_error(req, "Virtual size and chunk size must be non-zero");
196 		return;
197 	}
198 
199 	if (md.md_chunk_size % MAXPHYS != 0) {
200 		/* XXX: This is not strictly needed, but it's convenient to
201 		 * impose some limitations on it, so why not MAXPHYS. */
202 		size_t new_size = rounddown(md.md_chunk_size, MAXPHYS);
203 		if (new_size < md.md_chunk_size)
204 			new_size += MAXPHYS;
205 		fprintf(stderr, "Resizing chunk size to be a multiple of "
206 		    "MAXPHYS (%d kB).\n", MAXPHYS / 1024);
207 		fprintf(stderr, "New chunk size: %zu kB\n", new_size / 1024);
208 		md.md_chunk_size = new_size;
209 	}
210 
211 	if (md.md_virsize % md.md_chunk_size != 0) {
212 		off_t chunk_count = md.md_virsize / md.md_chunk_size;
213 		md.md_virsize = chunk_count * md.md_chunk_size;
214 		fprintf(stderr, "Resizing virtual size to be a multiple of "
215 		    "chunk size.\n");
216 		fprintf(stderr, "New virtual size: %zu MB\n",
217 		    (size_t)(md.md_virsize/(1024 * 1024)));
218 	}
219 
220 	msize = secsize = 0;
221 	for (i = 1; i < (unsigned)nargs; i++) {
222 		snprintf(param, sizeof(param), "arg%u", i);
223 		name = gctl_get_ascii(req, "%s", param);
224 		ssize = g_get_sectorsize(name);
225 		if (ssize == 0)
226 			fprintf(stderr, "%s for %s\n", strerror(errno), name);
227 		msize += g_get_mediasize(name);
228 		if (secsize == 0)
229 			secsize = ssize;
230 		else if (secsize != ssize) {
231 			gctl_error(req, "Devices need to have same sector size "
232 			    "(%u on %s needs to be %u).",
233 			    (u_int)ssize, name, (u_int)secsize);
234 			return;
235 		}
236 	}
237 
238 	if (secsize == 0) {
239 		gctl_error(req, "Device not specified");
240 		return;
241 	}
242 
243 	if (md.md_chunk_size % secsize != 0) {
244 		fprintf(stderr, "Error: chunk size is not a multiple of sector "
245 		    "size.");
246 		gctl_error(req, "Chunk size (in bytes) must be multiple of %u.",
247 		    (unsigned int)secsize);
248 		return;
249 	}
250 
251 	total_chunks = md.md_virsize / md.md_chunk_size;
252 	map_size = total_chunks * sizeof(*map);
253 	assert(md.md_virsize % md.md_chunk_size == 0);
254 
255 	ssize = map_size % secsize;
256 	if (ssize != 0) {
257 		size_t add_chunks = (secsize - ssize) / sizeof(*map);
258 		total_chunks += add_chunks;
259 		md.md_virsize = (off_t)total_chunks * (off_t)md.md_chunk_size;
260 		map_size = total_chunks * sizeof(*map);
261 		fprintf(stderr, "Resizing virtual size to fit virstor "
262 		    "structures.\n");
263 		fprintf(stderr, "New virtual size: %ju MB (%zu new chunks)\n",
264 		    (uintmax_t)(md.md_virsize / (1024 * 1024)), add_chunks);
265 	}
266 
267 	if (verbose)
268 		printf("Total virtual chunks: %zu (%zu MB each), %ju MB total "
269 		    "virtual size.\n",
270 		    total_chunks, (size_t)(md.md_chunk_size / (1024 * 1024)),
271 		    md.md_virsize/(1024 * 1024));
272 
273 	if ((off_t)md.md_virsize < msize)
274 		fprintf(stderr, "WARNING: Virtual storage size < Physical "
275 		    "available storage (%ju < %ju)\n", md.md_virsize, msize);
276 
277 	/* Clear last sector first to spoil all components if device exists. */
278 	if (verbose)
279 		printf("Clearing metadata on");
280 
281 	for (i = 1; i < (unsigned)nargs; i++) {
282 		snprintf(param, sizeof(param), "arg%u", i);
283 		name = gctl_get_ascii(req, "%s", param);
284 
285 		if (verbose)
286 			printf(" %s", name);
287 
288 		msize = g_get_mediasize(name);
289 		ssize = g_get_sectorsize(name);
290 		if (msize == 0 || ssize == 0) {
291 			gctl_error(req, "Can't retrieve information about "
292 			    "%s: %s.", name, strerror(errno));
293 			return;
294 		}
295 		if (msize < (off_t) MAX(md.md_chunk_size*4, map_size))
296 			gctl_error(req, "Device %s is too small", name);
297 		error = g_metadata_clear(name, NULL);
298 		if (error != 0) {
299 			gctl_error(req, "Can't clear metadata on %s: %s.", name,
300 			    strerror(error));
301 			return;
302 		}
303 	}
304 
305 
306 	/* Write allocation table to the first provider - this needs to be done
307 	 * before metadata is written because when kernel tastes it it's too
308 	 * late */
309 	name = gctl_get_ascii(req, "arg1"); /* device with metadata */
310 	if (verbose)
311 		printf(".\nWriting allocation table to %s...", name);
312 
313 	/* How many chunks does the map occupy? */
314 	map_chunks = map_size/md.md_chunk_size;
315 	if (map_size % md.md_chunk_size != 0)
316 		map_chunks++;
317 	if (verbose) {
318 		printf(" (%zu MB, %d chunks) ", map_size/(1024*1024), map_chunks);
319 		fflush(stdout);
320 	}
321 
322 	if (strncmp(name, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
323 		fd = open(name, O_RDWR);
324 	else {
325 		sprintf(param, "%s%s", _PATH_DEV, name);
326 		fd = open(param, O_RDWR);
327 	}
328 	if (fd < 0)
329 		gctl_error(req, "Cannot open provider %s to write map", name);
330 
331 	/* Do it with calloc because there might be a need to set up chunk flags
332 	 * in the future */
333 	map = calloc(total_chunks, sizeof(*map));
334 	if (map == NULL) {
335 		gctl_error(req,
336 		    "Out of memory (need %zu bytes for allocation map)",
337 		    map_size);
338 	}
339 
340 	written = pwrite(fd, map, map_size, 0);
341 	free(map);
342 	if ((size_t)written != map_size) {
343 		if (verbose) {
344 			fprintf(stderr, "\nTried to write %zu, written %zd (%s)\n",
345 			    map_size, written, strerror(errno));
346 		}
347 		gctl_error(req, "Error writing out allocation map!");
348 		return;
349 	}
350 	close (fd);
351 
352 	if (verbose)
353 		printf("\nStoring metadata on ");
354 
355 	/*
356 	 * ID is randomly generated, unique for a geom. This is used to
357 	 * recognize all providers belonging to one geom.
358 	 */
359 	md.md_id = arc4random();
360 
361 	/* Ok, store metadata. */
362 	for (i = 1; i < (unsigned)nargs; i++) {
363 		snprintf(param, sizeof(param), "arg%u", i);
364 		name = gctl_get_ascii(req, "%s", param);
365 
366 		msize = g_get_mediasize(name);
367 		ssize = g_get_sectorsize(name);
368 
369 		if (verbose)
370 			printf("%s ", name);
371 
372 		/* this provider's position/type in geom */
373 		md.no = i - 1;
374 		/* this provider's size */
375 		md.provsize = msize;
376 		/* chunk allocation info */
377 		md.chunk_count = md.provsize / md.md_chunk_size;
378 		if (verbose)
379 			printf("(%u chunks) ", md.chunk_count);
380 		/* Check to make sure last sector is unused */
381 		if ((off_t)(md.chunk_count * md.md_chunk_size) > (off_t)(msize-ssize))
382 		    md.chunk_count--;
383 		md.chunk_next = 0;
384 		if (i != 1) {
385 			md.chunk_reserved = 0;
386 			md.flags = 0;
387 		} else {
388 			md.chunk_reserved = map_chunks * 2;
389 			md.flags = VIRSTOR_PROVIDER_ALLOCATED |
390 			    VIRSTOR_PROVIDER_CURRENT;
391 			md.chunk_next = md.chunk_reserved;
392 			if (verbose)
393 				printf("(%u reserved) ", md.chunk_reserved);
394 		}
395 
396 		if (!hardcode)
397 			bzero(md.provider, sizeof(md.provider));
398 		else {
399 			/* convert "/dev/something" to "something" */
400 			if (strncmp(name, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0) {
401 				strlcpy(md.provider, name + sizeof(_PATH_DEV) - 1,
402 				    sizeof(md.provider));
403 			} else
404 				strlcpy(md.provider, name, sizeof(md.provider));
405 		}
406 		sect = calloc(ssize, sizeof(unsigned char));
407 		if (sect == NULL)
408 			err(1, "Cannot allocate sector of %zu bytes", ssize);
409 		virstor_metadata_encode(&md, sect);
410 		error = g_metadata_store(name, sect, ssize);
411 		free(sect);
412 		if (error != 0) {
413 			if (verbose)
414 				printf("\n");
415 			fprintf(stderr, "Can't store metadata on %s: %s.\n",
416 			    name, strerror(error));
417 			gctl_error(req,
418 			    "Not fully done (error storing metadata).");
419 			return;
420 		}
421 	}
422 #if 0
423 	if (verbose)
424 		printf("\n");
425 #endif
426 }
427 
428 /* Clears metadata on given provider(s) IF it's owned by us */
429 static void
430 virstor_clear(struct gctl_req *req)
431 {
432 	const char *name;
433 	char param[32];
434 	unsigned i;
435 	int nargs, error;
436 	int fd;
437 
438 	nargs = gctl_get_int(req, "nargs");
439 	if (nargs < 1) {
440 		gctl_error(req, "Too few arguments.");
441 		return;
442 	}
443 	for (i = 0; i < (unsigned)nargs; i++) {
444 		snprintf(param, sizeof(param), "arg%u", i);
445 		name = gctl_get_ascii(req, "%s", param);
446 
447 		error = g_metadata_clear(name, G_VIRSTOR_MAGIC);
448 		if (error != 0) {
449 			fprintf(stderr, "Can't clear metadata on %s: %s "
450 			    "(do I own it?)\n", name, strerror(error));
451 			gctl_error(req,
452 			    "Not fully done (can't clear metadata).");
453 			continue;
454 		}
455 		if (strncmp(name, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
456 			fd = open(name, O_RDWR);
457 		else {
458 			sprintf(param, "%s%s", _PATH_DEV, name);
459 			fd = open(param, O_RDWR);
460 		}
461 		if (fd < 0) {
462 			gctl_error(req, "Cannot clear header sector for %s",
463 			    name);
464 			continue;
465 		}
466 		if (verbose)
467 			printf("Metadata cleared on %s.\n", name);
468 	}
469 }
470 
471 /* Print some metadata information */
472 static void
473 virstor_metadata_dump(const struct g_virstor_metadata *md)
474 {
475 	printf("          Magic string: %s\n", md->md_magic);
476 	printf("      Metadata version: %u\n", (u_int) md->md_version);
477 	printf("           Device name: %s\n", md->md_name);
478 	printf("             Device ID: %u\n", (u_int) md->md_id);
479 	printf("        Provider index: %u\n", (u_int) md->no);
480 	printf("      Active providers: %u\n", (u_int) md->md_count);
481 	printf("    Hardcoded provider: %s\n",
482 	    md->provider[0] != '\0' ? md->provider : "(not hardcoded)");
483 	printf("          Virtual size: %u MB\n",
484 	    (unsigned int)(md->md_virsize/(1024 * 1024)));
485 	printf("            Chunk size: %u kB\n", md->md_chunk_size / 1024);
486 	printf("    Chunks on provider: %u\n", md->chunk_count);
487 	printf("           Chunks free: %u\n", md->chunk_count - md->chunk_next);
488 	printf("       Reserved chunks: %u\n", md->chunk_reserved);
489 }
490 
491 /* Called by geom(8) via gvirstor_main() to dump metadata information */
492 static void
493 virstor_dump(struct gctl_req *req)
494 {
495 	struct g_virstor_metadata md;
496 	u_char tmpmd[512];	/* temporary buffer */
497 	const char *name;
498 	char param[16];
499 	int nargs, error, i;
500 
501 	assert(sizeof(tmpmd) >= sizeof(md));
502 
503 	nargs = gctl_get_int(req, "nargs");
504 	if (nargs < 1) {
505 		gctl_error(req, "Too few arguments.");
506 		return;
507 	}
508 	for (i = 0; i < nargs; i++) {
509 		snprintf(param, sizeof(param), "arg%u", i);
510 		name = gctl_get_ascii(req, "%s", param);
511 
512 		error = g_metadata_read(name, (u_char *) & tmpmd, sizeof(tmpmd),
513 		    G_VIRSTOR_MAGIC);
514 		if (error != 0) {
515 			fprintf(stderr, "Can't read metadata from %s: %s.\n",
516 			    name, strerror(error));
517 			gctl_error(req,
518 			    "Not fully done (error reading metadata).");
519 			continue;
520 		}
521 		virstor_metadata_decode((u_char *) & tmpmd, &md);
522 		printf("Metadata on %s:\n", name);
523 		virstor_metadata_dump(&md);
524 		printf("\n");
525 	}
526 }
527