2 * Copyright (c) 2005 Ivan Voras <ivoras@freebsd.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
29 #include <sys/param.h>
43 #include <core/geom.h>
44 #include <misc/subr.h>
46 #include <geom/virstor/g_virstor_md.h>
47 #include <geom/virstor/g_virstor.h>
49 uint32_t lib_version = G_LIB_VERSION;
50 uint32_t version = G_VIRSTOR_VERSION;
51 static intmax_t chunk_size = 4 * 1024 * 1024; /* in kB (default: 4 MB) */
52 static intmax_t vir_size = 2ULL << 40; /* in MB (default: 2 TB) */
54 #if G_LIB_VERSION == 1
55 /* Support RELENG_6 */
56 #define G_TYPE_BOOL G_TYPE_NONE
60 * virstor_main gets called by the geom(8) utility
62 static void virstor_main(struct gctl_req *req, unsigned flags);
64 struct g_command class_commands[] = {
65 {"clear", G_FLAG_VERBOSE, virstor_main, G_NULL_OPTS, NULL,
68 {"dump", 0, virstor_main, G_NULL_OPTS, NULL,
71 {"label", G_FLAG_VERBOSE | G_FLAG_LOADKLD, virstor_main,
73 {'h', "hardcode", NULL, G_TYPE_BOOL},
74 {'m', "chunk_size", &chunk_size, G_TYPE_NUMBER},
75 {'s', "vir_size", &vir_size, G_TYPE_NUMBER},
78 NULL, "[-h] [-v] [-m chunk_size] [-s vir_size] name provider0 [provider1 ...]"
80 {"destroy", G_FLAG_VERBOSE, NULL,
82 {'f', "force", NULL, G_TYPE_BOOL},
85 NULL, "[-fv] name ..."
87 {"stop", G_FLAG_VERBOSE, NULL,
89 {'f', "force", NULL, G_TYPE_BOOL},
92 NULL, "[-fv] name ... (alias for \"destroy\")"
94 {"add", G_FLAG_VERBOSE, NULL,
96 {'h', "hardcode", NULL, G_TYPE_BOOL},
99 NULL, "[-vh] name prov [prov ...]"
101 {"remove", G_FLAG_VERBOSE, NULL, G_NULL_OPTS, NULL,
107 static int verbose = 0;
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);
114 /* Dispatcher function (no real work done here, only verbose flag recorder) */
116 virstor_main(struct gctl_req *req, unsigned flags)
120 if ((flags & G_FLAG_VERBOSE) != 0)
123 name = gctl_get_ascii(req, "verb");
125 gctl_error(req, "No '%s' argument.", "verb");
128 if (strcmp(name, "label") == 0)
130 else if (strcmp(name, "clear") == 0)
132 else if (strcmp(name, "dump") == 0)
135 gctl_error(req, "%s: Unknown command: %s.", __func__, name);
137 /* No CTASSERT in userland
138 CTASSERT(VIRSTOR_MAP_BLOCK_ENTRIES*VIRSTOR_MAP_ENTRY_SIZE == MAXPHYS);
143 pathgen(const char *name, char *path, size_t size)
146 if (strncmp(name, _PATH_DEV, strlen(_PATH_DEV)) != 0)
147 snprintf(path, size, "%s%s", _PATH_DEV, name);
149 strlcpy(path, name, size);
153 my_g_metadata_store(const char *name, u_char *md, size_t size)
155 char path[MAXPATHLEN];
162 pathgen(name, path, sizeof(path));
166 fd = open(path, O_RDWR);
169 mediasize = g_get_mediasize(name);
170 if (mediasize == 0) {
174 sectorsize = g_get_sectorsize(name);
175 if (sectorsize == 0) {
179 assert(sectorsize >= size);
180 sector = malloc(sectorsize);
181 if (sector == NULL) {
185 bcopy(md, sector, size);
186 if ((abc = pwrite(fd, sector, sectorsize, mediasize - sectorsize)) !=
187 (ssize_t)sectorsize) {
199 * Labels a new geom Meaning: parses and checks the parameters, calculates &
200 * writes metadata to the relevant providers so when the next round of
201 * "tasting" comes (which will be just after the provider(s) are closed) geom
202 * can be instantiated with the tasted metadata.
205 virstor_label(struct gctl_req *req)
207 struct g_virstor_metadata md;
211 size_t ssize, secsize;
214 int hardcode, nargs, error;
215 struct virstor_map_entry *map;
216 size_t total_chunks; /* We'll run out of memory if
217 this needs to be bigger. */
218 unsigned int map_chunks; /* Chunks needed by the map (map size). */
219 size_t map_size; /* In bytes. */
223 nargs = gctl_get_int(req, "nargs");
225 gctl_error(req, "Too few arguments (%d): expecting: name "
226 "provider0 [provider1 ...]", nargs);
230 hardcode = gctl_get_int(req, "hardcode");
233 * Initialize constant parts of metadata: magic signature, version,
236 bzero(&md, sizeof(md));
237 strlcpy(md.md_magic, G_VIRSTOR_MAGIC, sizeof(md.md_magic));
238 md.md_version = G_VIRSTOR_VERSION;
239 name = gctl_get_ascii(req, "arg0");
241 gctl_error(req, "No 'arg%u' argument.", 0);
244 strlcpy(md.md_name, name, sizeof(md.md_name));
246 md.md_virsize = (off_t)gctl_get_intmax(req, "vir_size");
247 md.md_chunk_size = gctl_get_intmax(req, "chunk_size");
248 md.md_count = nargs - 1;
250 if (md.md_virsize == 0 || md.md_chunk_size == 0) {
251 gctl_error(req, "Virtual size and chunk size must be non-zero");
255 if (md.md_chunk_size % MAXPHYS != 0) {
256 /* XXX: This is not strictly needed, but it's convenient to
257 * impose some limitations on it, so why not MAXPHYS. */
258 size_t new_size = (md.md_chunk_size / MAXPHYS) * MAXPHYS;
259 if (new_size < md.md_chunk_size)
261 fprintf(stderr, "Resizing chunk size to be a multiple of "
262 "MAXPHYS (%d kB).\n", MAXPHYS / 1024);
263 fprintf(stderr, "New chunk size: %zu kB\n", new_size / 1024);
264 md.md_chunk_size = new_size;
267 if (md.md_virsize % md.md_chunk_size != 0) {
268 off_t chunk_count = md.md_virsize / md.md_chunk_size;
269 md.md_virsize = chunk_count * md.md_chunk_size;
270 fprintf(stderr, "Resizing virtual size to be a multiple of "
272 fprintf(stderr, "New virtual size: %zu MB\n",
273 (size_t)(md.md_virsize/(1024 * 1024)));
276 msize = secsize = ssize = 0;
277 for (i = 1; i < (unsigned)nargs; i++) {
278 snprintf(param, sizeof(param), "arg%u", i);
279 name = gctl_get_ascii(req, param);
280 ssize = g_get_sectorsize(name);
282 fprintf(stderr, "%s for %s\n", strerror(errno), name);
283 msize += g_get_mediasize(name);
286 else if (secsize != ssize) {
287 gctl_error(req, "Devices need to have same sector size "
288 "(%u on %s needs to be %u).",
289 (u_int)ssize, name, (u_int)secsize);
294 if (md.md_chunk_size % secsize != 0) {
295 fprintf(stderr, "Error: chunk size is not a multiple of sector "
297 gctl_error(req, "Chunk size (in bytes) must be multiple of %u.",
298 (unsigned int)secsize);
302 total_chunks = md.md_virsize / md.md_chunk_size;
303 map_size = total_chunks * sizeof(*map);
304 assert(md.md_virsize % md.md_chunk_size == 0);
306 ssize = map_size % secsize;
308 size_t add_chunks = (secsize - ssize) / sizeof(*map);
309 total_chunks += add_chunks;
310 md.md_virsize = (off_t)total_chunks * (off_t)md.md_chunk_size;
311 map_size = total_chunks * sizeof(*map);
312 fprintf(stderr, "Resizing virtual size to fit virstor "
314 fprintf(stderr, "New virtual size: %ju MB (%zu new chunks)\n",
315 (uintmax_t)(md.md_virsize / (1024 * 1024)), add_chunks);
319 printf("Total virtual chunks: %zu (%zu MB each), %ju MB total "
321 total_chunks, (size_t)(md.md_chunk_size / (1024 * 1024)),
322 md.md_virsize/(1024 * 1024));
324 if ((off_t)md.md_virsize < msize)
325 fprintf(stderr, "WARNING: Virtual storage size < Physical "
326 "available storage (%ju < %ju)\n", md.md_virsize, msize);
328 /* Clear last sector first to spoil all components if device exists. */
330 printf("Clearing metadata on");
332 for (i = 1; i < (unsigned)nargs; i++) {
333 snprintf(param, sizeof(param), "arg%u", i);
334 name = gctl_get_ascii(req, param);
339 msize = g_get_mediasize(name);
340 ssize = g_get_sectorsize(name);
341 if (msize == 0 || ssize == 0) {
342 gctl_error(req, "Can't retrieve information about "
343 "%s: %s.", name, strerror(errno));
346 if (msize < MAX(md.md_chunk_size*4, map_size))
347 gctl_error(req, "Device %s is too small", name);
348 error = g_metadata_clear(name, NULL);
350 gctl_error(req, "Can't clear metadata on %s: %s.", name,
357 /* Write allocation table to the first provider - this needs to be done
358 * before metadata is written because when kernel tastes it it's too
360 name = gctl_get_ascii(req, "arg1"); /* device with metadata */
362 printf(".\nWriting allocation table to %s...", name);
364 /* How many chunks does the map occupy? */
365 map_chunks = map_size/md.md_chunk_size;
366 if (map_size % md.md_chunk_size != 0)
369 printf(" (%zu MB, %d chunks) ", map_size/(1024*1024), map_chunks);
373 if (strncmp(name, _PATH_DEV, strlen(_PATH_DEV)) == 0)
374 fd = open(name, O_RDWR);
376 sprintf(param, "%s%s", _PATH_DEV, name);
377 fd = open(param, O_RDWR);
380 gctl_error(req, "Cannot open provider %s to write map", name);
382 /* Do it with calloc because there might be a need to set up chunk flags
384 map = calloc(total_chunks, sizeof(*map));
387 "Out of memory (need %zu bytes for allocation map)",
391 written = pwrite(fd, map, map_size, 0);
393 if ((size_t)written != map_size) {
395 fprintf(stderr, "\nTried to write %zu, written %zd (%s)\n",
396 map_size, written, strerror(errno));
398 gctl_error(req, "Error writing out allocation map!");
404 printf("\nStoring metadata on ");
407 * ID is randomly generated, unique for a geom. This is used to
408 * recognize all providers belonging to one geom.
410 md.md_id = arc4random();
412 /* Ok, store metadata. */
413 for (i = 1; i < (unsigned)nargs; i++) {
414 snprintf(param, sizeof(param), "arg%u", i);
415 name = gctl_get_ascii(req, param);
417 msize = g_get_mediasize(name);
418 ssize = g_get_sectorsize(name);
423 /* this provider's position/type in geom */
425 /* this provider's size */
427 /* chunk allocation info */
428 md.chunk_count = md.provsize / md.md_chunk_size;
430 printf("(%u chunks) ", md.chunk_count);
431 /* Check to make sure last sector is unused */
432 if ((off_t)(md.chunk_count) * md.md_chunk_size > msize-ssize)
436 md.chunk_reserved = 0;
439 md.chunk_reserved = map_chunks * 2;
440 md.flags = VIRSTOR_PROVIDER_ALLOCATED |
441 VIRSTOR_PROVIDER_CURRENT;
442 md.chunk_next = md.chunk_reserved;
444 printf("(%u reserved) ", md.chunk_reserved);
448 bzero(md.provider, sizeof(md.provider));
450 /* convert "/dev/something" to "something" */
451 if (strncmp(name, _PATH_DEV, strlen(_PATH_DEV)) == 0) {
452 strlcpy(md.provider, name + strlen(_PATH_DEV),
453 sizeof(md.provider));
455 strlcpy(md.provider, name, sizeof(md.provider));
457 sect = malloc(ssize);
460 err(1, "Cannot allocate sector of %zu bytes", ssize);
461 virstor_metadata_encode(&md, sect);
462 error = my_g_metadata_store(name, sect, ssize);
467 fprintf(stderr, "Can't store metadata on %s: %s.\n",
468 name, strerror(error));
470 "Not fully done (error storing metadata).");
480 /* Clears metadata on given provider(s) IF it's owned by us */
482 virstor_clear(struct gctl_req *req)
490 nargs = gctl_get_int(req, "nargs");
492 gctl_error(req, "Too few arguments.");
495 for (i = 0; i < (unsigned)nargs; i++) {
496 snprintf(param, sizeof(param), "arg%u", i);
497 name = gctl_get_ascii(req, param);
499 error = g_metadata_clear(name, G_VIRSTOR_MAGIC);
501 fprintf(stderr, "Can't clear metadata on %s: %s "
502 "(do I own it?)\n", name, strerror(error));
504 "Not fully done (can't clear metadata).");
507 if (strncmp(name, _PATH_DEV, strlen(_PATH_DEV)) == 0)
508 fd = open(name, O_RDWR);
510 sprintf(param, "%s%s", _PATH_DEV, name);
511 fd = open(param, O_RDWR);
514 gctl_error(req, "Cannot clear header sector for %s",
519 printf("Metadata cleared on %s.\n", name);
523 /* Print some metadata information */
525 virstor_metadata_dump(const struct g_virstor_metadata *md)
527 printf(" Magic string: %s\n", md->md_magic);
528 printf(" Metadata version: %u\n", (u_int) md->md_version);
529 printf(" Device name: %s\n", md->md_name);
530 printf(" Device ID: %u\n", (u_int) md->md_id);
531 printf(" Provider index: %u\n", (u_int) md->no);
532 printf(" Active providers: %u\n", (u_int) md->md_count);
533 printf(" Hardcoded provider: %s\n",
534 md->provider[0] != '\0' ? md->provider : "(not hardcoded)");
535 printf(" Virtual size: %u MB\n",
536 (unsigned int)(md->md_virsize/(1024 * 1024)));
537 printf(" Chunk size: %u kB\n", md->md_chunk_size / 1024);
538 printf(" Chunks on provider: %u\n", md->chunk_count);
539 printf(" Chunks free: %u\n", md->chunk_count - md->chunk_next);
540 printf(" Reserved chunks: %u\n", md->chunk_reserved);
543 /* Called by geom(8) via gvirstor_main() to dump metadata information */
545 virstor_dump(struct gctl_req *req)
547 struct g_virstor_metadata md;
548 u_char tmpmd[512]; /* temporary buffer */
553 assert(sizeof(tmpmd) >= sizeof(md));
555 nargs = gctl_get_int(req, "nargs");
557 gctl_error(req, "Too few arguments.");
560 for (i = 0; i < nargs; i++) {
561 snprintf(param, sizeof(param), "arg%u", i);
562 name = gctl_get_ascii(req, param);
564 error = g_metadata_read(name, (u_char *) & tmpmd, sizeof(tmpmd),
567 fprintf(stderr, "Can't read metadata from %s: %s.\n",
568 name, strerror(error));
570 "Not fully done (error reading metadata).");
573 virstor_metadata_decode((u_char *) & tmpmd, &md);
574 printf("Metadata on %s:\n", name);
575 virstor_metadata_dump(&md);