Add GEOM_MIRROR class which provide RAID1 functionality and has many useful
features. The gmirror(8) utility should be used for control of this class. There is no manual page yet, but I'm working on it with keramida@. Many useful tests provided by: simon (thank you!) Some ideas from: scottl, simon, phk
This commit is contained in:
parent
bd58d1d222
commit
fa4a1febf7
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=132904
11
sbin/geom/class/mirror/Makefile
Normal file
11
sbin/geom/class/mirror/Makefile
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
# $FreeBSD$
|
||||||
|
|
||||||
|
.PATH: ${.CURDIR}/../../misc
|
||||||
|
|
||||||
|
CLASS= mirror
|
||||||
|
|
||||||
|
NOMAN= notyet
|
||||||
|
DPADD= ${LIBMD}
|
||||||
|
LDADD= -lmd
|
||||||
|
|
||||||
|
.include <bsd.lib.mk>
|
408
sbin/geom/class/mirror/geom_mirror.c
Normal file
408
sbin/geom/class/mirror/geom_mirror.c
Normal file
@ -0,0 +1,408 @@
|
|||||||
|
/*-
|
||||||
|
* Copyright (c) 2004 Pawel Jakub Dawidek <pjd@FreeBSD.org>
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
* 1. Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
|
||||||
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
|
||||||
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||||
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||||
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||||
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||||
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||||
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||||
|
* SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <sys/cdefs.h>
|
||||||
|
__FBSDID("$FreeBSD$");
|
||||||
|
|
||||||
|
#include <sys/param.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <strings.h>
|
||||||
|
#include <assert.h>
|
||||||
|
#include <libgeom.h>
|
||||||
|
#include <geom/mirror/g_mirror.h>
|
||||||
|
#include <core/geom.h>
|
||||||
|
#include <misc/subr.h>
|
||||||
|
|
||||||
|
|
||||||
|
uint32_t lib_version = G_LIB_VERSION;
|
||||||
|
uint32_t version = G_MIRROR_VERSION;
|
||||||
|
|
||||||
|
static char label_balance[] = "split", configure_balance[] = "none";
|
||||||
|
static intmax_t label_slice = 4096, configure_slice = -1;
|
||||||
|
|
||||||
|
static void mirror_main(struct gctl_req *req, unsigned f);
|
||||||
|
static void mirror_activate(struct gctl_req *req);
|
||||||
|
static void mirror_clear(struct gctl_req *req);
|
||||||
|
static void mirror_dump(struct gctl_req *req);
|
||||||
|
static void mirror_label(struct gctl_req *req);
|
||||||
|
|
||||||
|
struct g_command class_commands[] = {
|
||||||
|
{ "activate", G_FLAG_VERBOSE, mirror_main, G_NULL_OPTS },
|
||||||
|
{ "clear", G_FLAG_VERBOSE, mirror_main, G_NULL_OPTS },
|
||||||
|
{ "configure", G_FLAG_VERBOSE, NULL,
|
||||||
|
{
|
||||||
|
{ 'a', "autosync", NULL, G_TYPE_NONE },
|
||||||
|
{ 'b', "balance", configure_balance, G_TYPE_STRING },
|
||||||
|
{ 'n', "noautosync", NULL, G_TYPE_NONE },
|
||||||
|
{ 's', "slice", &configure_slice, G_TYPE_NUMBER },
|
||||||
|
G_OPT_SENTINEL
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{ "deactivate", G_FLAG_VERBOSE, NULL, G_NULL_OPTS },
|
||||||
|
{ "dump", 0, mirror_main, G_NULL_OPTS },
|
||||||
|
{ "forget", G_FLAG_VERBOSE, NULL, G_NULL_OPTS },
|
||||||
|
{ "label", G_FLAG_VERBOSE, mirror_main,
|
||||||
|
{
|
||||||
|
{ 'b', "balance", label_balance, G_TYPE_STRING },
|
||||||
|
{ 'n', "noautosync", NULL, G_TYPE_NONE },
|
||||||
|
{ 's', "slice", &label_slice, G_TYPE_NUMBER },
|
||||||
|
G_OPT_SENTINEL
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{ "insert", G_FLAG_VERBOSE, NULL,
|
||||||
|
{
|
||||||
|
{ 'i', "inactive", NULL, G_TYPE_NONE },
|
||||||
|
G_OPT_SENTINEL
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{ "rebuild", G_FLAG_VERBOSE, NULL, G_NULL_OPTS },
|
||||||
|
{ "remove", G_FLAG_VERBOSE, NULL, G_NULL_OPTS },
|
||||||
|
{ "stop", G_FLAG_VERBOSE, NULL,
|
||||||
|
{
|
||||||
|
{ 'f', "force", NULL, G_TYPE_NONE },
|
||||||
|
G_OPT_SENTINEL
|
||||||
|
}
|
||||||
|
},
|
||||||
|
G_CMD_SENTINEL
|
||||||
|
};
|
||||||
|
|
||||||
|
static int verbose = 0;
|
||||||
|
|
||||||
|
void usage(const char *);
|
||||||
|
void
|
||||||
|
usage(const char *comm)
|
||||||
|
{
|
||||||
|
fprintf(stderr,
|
||||||
|
"usage: %s label [-nv] [-b balance] [-s slice] name dev1 [dev2 [...]]\n"
|
||||||
|
" %s clear [-v] dev1 [dev2 [...]]\n"
|
||||||
|
" %s dump dev1 [dev2 [...]]\n"
|
||||||
|
" %s configure [-anv] [-b balance] [-s slice] name\n"
|
||||||
|
" %s rebuild [-v] name dev1 [dev2 [...]]\n"
|
||||||
|
" %s insert [-iv] name dev1 [dev2 [...]]\n"
|
||||||
|
" %s remove [-v] name dev1 [dev2 [...]]\n"
|
||||||
|
" %s activate [-v] name dev1 [dev2 [...]]\n"
|
||||||
|
" %s deactivate [-v] name dev1 [dev2 [...]]\n"
|
||||||
|
" %s forget dev1 [dev2 [...]]\n"
|
||||||
|
" %s stop [-fv] name\n",
|
||||||
|
comm, comm, comm, comm, comm, comm, comm, comm, comm, comm, comm);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
mirror_main(struct gctl_req *req, unsigned f)
|
||||||
|
{
|
||||||
|
const char *name;
|
||||||
|
|
||||||
|
if ((f & G_FLAG_VERBOSE) != 0)
|
||||||
|
verbose = 1;
|
||||||
|
|
||||||
|
name = gctl_get_asciiparam(req, "verb");
|
||||||
|
if (name == NULL) {
|
||||||
|
gctl_error(req, "No '%s' argument.", "verb");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (strcmp(name, "label") == 0)
|
||||||
|
mirror_label(req);
|
||||||
|
else if (strcmp(name, "clear") == 0)
|
||||||
|
mirror_clear(req);
|
||||||
|
else if (strcmp(name, "dump") == 0)
|
||||||
|
mirror_dump(req);
|
||||||
|
else if (strcmp(name, "activate") == 0)
|
||||||
|
mirror_activate(req);
|
||||||
|
else
|
||||||
|
gctl_error(req, "Unknown command: %s.", name);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
mirror_label(struct gctl_req *req)
|
||||||
|
{
|
||||||
|
struct g_mirror_metadata md;
|
||||||
|
u_char sector[512];
|
||||||
|
const char *str;
|
||||||
|
char param[16];
|
||||||
|
int *nargs, *noautosync, bal, error, i;
|
||||||
|
unsigned sectorsize;
|
||||||
|
off_t mediasize;
|
||||||
|
intmax_t *valp;
|
||||||
|
|
||||||
|
nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
|
||||||
|
if (nargs == NULL) {
|
||||||
|
gctl_error(req, "No '%s' argument.", "nargs");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (*nargs < 2) {
|
||||||
|
gctl_error(req, "Too few arguments.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
strlcpy(md.md_magic, G_MIRROR_MAGIC, sizeof(md.md_magic));
|
||||||
|
md.md_version = G_MIRROR_VERSION;
|
||||||
|
str = gctl_get_asciiparam(req, "arg0");
|
||||||
|
if (str == NULL) {
|
||||||
|
gctl_error(req, "No 'arg%u' argument.", 0);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
strlcpy(md.md_name, str, sizeof(md.md_name));
|
||||||
|
md.md_mid = arc4random();
|
||||||
|
md.md_all = *nargs - 1;
|
||||||
|
md.md_mflags = 0;
|
||||||
|
md.md_dflags = 0;
|
||||||
|
md.md_syncid = 1;
|
||||||
|
md.md_sync_offset = 0;
|
||||||
|
valp = gctl_get_paraml(req, "slice", sizeof(*valp));
|
||||||
|
if (valp == NULL) {
|
||||||
|
gctl_error(req, "No '%s' argument.", "slice");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
md.md_slice = *valp;
|
||||||
|
str = gctl_get_asciiparam(req, "balance");
|
||||||
|
if (str == NULL) {
|
||||||
|
gctl_error(req, "No '%s' argument.", "balance");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
bal = balance_id(str);
|
||||||
|
if (bal == -1) {
|
||||||
|
gctl_error(req, "Wrong balance algorithm.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
md.md_balance = bal;
|
||||||
|
noautosync = gctl_get_paraml(req, "noautosync", sizeof(*noautosync));
|
||||||
|
if (noautosync == NULL) {
|
||||||
|
gctl_error(req, "No '%s' argument.", "noautosync");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (*noautosync)
|
||||||
|
md.md_mflags |= G_MIRROR_DEVICE_FLAG_NOAUTOSYNC;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Calculate sectorsize by finding least common multiple from
|
||||||
|
* sectorsizes of every disk and find the smallest mediasize.
|
||||||
|
*/
|
||||||
|
mediasize = 0;
|
||||||
|
sectorsize = 0;
|
||||||
|
for (i = 1; i < *nargs; i++) {
|
||||||
|
unsigned ssize;
|
||||||
|
off_t msize;
|
||||||
|
|
||||||
|
snprintf(param, sizeof(param), "arg%u", i);
|
||||||
|
str = gctl_get_asciiparam(req, param);
|
||||||
|
|
||||||
|
msize = g_get_mediasize(str);
|
||||||
|
ssize = g_get_sectorsize(str);
|
||||||
|
if (msize == 0 || ssize == 0) {
|
||||||
|
gctl_error(req, "Can't get informations about %s: %s.",
|
||||||
|
str, strerror(errno));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
msize -= ssize;
|
||||||
|
if (mediasize == 0 || (mediasize > 0 && msize < mediasize))
|
||||||
|
mediasize = msize;
|
||||||
|
if (sectorsize == 0)
|
||||||
|
sectorsize = ssize;
|
||||||
|
else
|
||||||
|
sectorsize = g_lcm(sectorsize, ssize);
|
||||||
|
}
|
||||||
|
md.md_mediasize = mediasize;
|
||||||
|
md.md_sectorsize = sectorsize;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Clear last sector first, to spoil all components if device exists.
|
||||||
|
*/
|
||||||
|
for (i = 1; i < *nargs; i++) {
|
||||||
|
snprintf(param, sizeof(param), "arg%u", i);
|
||||||
|
str = gctl_get_asciiparam(req, param);
|
||||||
|
|
||||||
|
error = g_metadata_clear(str, NULL);
|
||||||
|
if (error != 0) {
|
||||||
|
gctl_error(req, "Can't store metadata on %s: %s.", str,
|
||||||
|
strerror(error));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Ok, store metadata (use disk number as priority).
|
||||||
|
*/
|
||||||
|
for (i = 1; i < *nargs; i++) {
|
||||||
|
snprintf(param, sizeof(param), "arg%u", i);
|
||||||
|
str = gctl_get_asciiparam(req, param);
|
||||||
|
|
||||||
|
md.md_did = arc4random();
|
||||||
|
md.md_priority = i - 1;
|
||||||
|
mirror_metadata_encode(&md, sector);
|
||||||
|
error = g_metadata_store(str, sector, sizeof(sector));
|
||||||
|
if (error != 0) {
|
||||||
|
fprintf(stderr, "Can't store metadata on %s: %s.\n",
|
||||||
|
str, strerror(error));
|
||||||
|
gctl_error(req, "Not fully done.");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (verbose)
|
||||||
|
printf("Metadata value stored on %s.\n", str);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
mirror_clear(struct gctl_req *req)
|
||||||
|
{
|
||||||
|
const char *name;
|
||||||
|
char param[16];
|
||||||
|
int *nargs, error, i;
|
||||||
|
|
||||||
|
nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
|
||||||
|
if (nargs == NULL) {
|
||||||
|
gctl_error(req, "No '%s' argument.", "nargs");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (*nargs < 1) {
|
||||||
|
gctl_error(req, "Too few arguments.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (i = 0; i < *nargs; i++) {
|
||||||
|
snprintf(param, sizeof(param), "arg%u", i);
|
||||||
|
name = gctl_get_asciiparam(req, param);
|
||||||
|
|
||||||
|
error = g_metadata_clear(name, G_MIRROR_MAGIC);
|
||||||
|
if (error != 0) {
|
||||||
|
fprintf(stderr, "Can't clear metadata on %s: %s.\n",
|
||||||
|
name, strerror(error));
|
||||||
|
gctl_error(req, "Not fully done.");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (verbose)
|
||||||
|
printf("Metadata cleared on %s.\n", name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
mirror_dump(struct gctl_req *req)
|
||||||
|
{
|
||||||
|
struct g_mirror_metadata md, tmpmd;
|
||||||
|
const char *name;
|
||||||
|
char param[16];
|
||||||
|
int *nargs, error, i;
|
||||||
|
|
||||||
|
nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
|
||||||
|
if (nargs == NULL) {
|
||||||
|
gctl_error(req, "No '%s' argument.", "nargs");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (*nargs < 1) {
|
||||||
|
gctl_error(req, "Too few arguments.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (i = 0; i < *nargs; i++) {
|
||||||
|
snprintf(param, sizeof(param), "arg%u", i);
|
||||||
|
name = gctl_get_asciiparam(req, param);
|
||||||
|
|
||||||
|
error = g_metadata_read(name, (u_char *)&tmpmd, sizeof(tmpmd),
|
||||||
|
G_MIRROR_MAGIC);
|
||||||
|
if (error != 0) {
|
||||||
|
fprintf(stderr, "Can't read metadata from %s: %s.\n",
|
||||||
|
name, strerror(error));
|
||||||
|
gctl_error(req, "Not fully done.");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (mirror_metadata_decode((u_char *)&tmpmd, &md) != 0) {
|
||||||
|
fprintf(stderr, "MD5 hash mismatch for %s, skipping.\n",
|
||||||
|
name);
|
||||||
|
gctl_error(req, "Not fully done.");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
printf("Metadata on %s:\n", name);
|
||||||
|
mirror_metadata_dump(&md);
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
mirror_activate(struct gctl_req *req)
|
||||||
|
{
|
||||||
|
struct g_mirror_metadata md, tmpmd;
|
||||||
|
const char *name, *path;
|
||||||
|
int *nargs, error, i;
|
||||||
|
char param[16];
|
||||||
|
|
||||||
|
nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
|
||||||
|
if (nargs == NULL) {
|
||||||
|
gctl_error(req, "No '%s' argument.", "nargs");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (*nargs < 2) {
|
||||||
|
gctl_error(req, "Too few arguments.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
name = gctl_get_asciiparam(req, "arg0");
|
||||||
|
if (name == NULL) {
|
||||||
|
gctl_error(req, "No 'arg%u' argument.", 0);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (i = 1; i < *nargs; i++) {
|
||||||
|
snprintf(param, sizeof(param), "arg%u", i);
|
||||||
|
path = gctl_get_asciiparam(req, param);
|
||||||
|
|
||||||
|
error = g_metadata_read(path, (u_char *)&tmpmd, sizeof(tmpmd),
|
||||||
|
G_MIRROR_MAGIC);
|
||||||
|
if (error != 0) {
|
||||||
|
fprintf(stderr, "Cannot read metadata from %s: %s.\n",
|
||||||
|
path, strerror(error));
|
||||||
|
gctl_error(req, "Not fully done.");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (mirror_metadata_decode((u_char *)&tmpmd, &md) != 0) {
|
||||||
|
fprintf(stderr,
|
||||||
|
"MD5 hash mismatch for provider %s, skipping.\n",
|
||||||
|
path);
|
||||||
|
gctl_error(req, "Not fully done.");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (strcmp(md.md_name, name) != 0) {
|
||||||
|
fprintf(stderr,
|
||||||
|
"Provider %s is not the mirror %s component.\n",
|
||||||
|
path, name);
|
||||||
|
gctl_error(req, "Not fully done.");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
md.md_dflags &= ~G_MIRROR_DISK_FLAG_INACTIVE;
|
||||||
|
mirror_metadata_encode(&md, (u_char *)&tmpmd);
|
||||||
|
error = g_metadata_store(path, (u_char *)&tmpmd, sizeof(tmpmd));
|
||||||
|
if (error != 0) {
|
||||||
|
fprintf(stderr, "Cannot write metadata from %s: %s.\n",
|
||||||
|
path, strerror(error));
|
||||||
|
gctl_error(req, "Not fully done.");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (verbose)
|
||||||
|
printf("Provider %s activated.\n", path);
|
||||||
|
}
|
||||||
|
}
|
2591
sys/geom/mirror/g_mirror.c
Normal file
2591
sys/geom/mirror/g_mirror.c
Normal file
File diff suppressed because it is too large
Load Diff
350
sys/geom/mirror/g_mirror.h
Normal file
350
sys/geom/mirror/g_mirror.h
Normal file
@ -0,0 +1,350 @@
|
|||||||
|
/*-
|
||||||
|
* Copyright (c) 2004 Pawel Jakub Dawidek <pjd@FreeBSD.org>
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
* 1. Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
|
||||||
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
|
||||||
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||||
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||||
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||||
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||||
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||||
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||||
|
* SUCH DAMAGE.
|
||||||
|
*
|
||||||
|
* $FreeBSD$
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _G_MIRROR_H_
|
||||||
|
#define _G_MIRROR_H_
|
||||||
|
|
||||||
|
#include <sys/endian.h>
|
||||||
|
#include <sys/md5.h>
|
||||||
|
|
||||||
|
#define G_MIRROR_CLASS_NAME "MIRROR"
|
||||||
|
|
||||||
|
#define G_MIRROR_MAGIC "GEOM::MIRROR"
|
||||||
|
#define G_MIRROR_VERSION 0
|
||||||
|
|
||||||
|
#define G_MIRROR_BALANCE_NONE 0
|
||||||
|
#define G_MIRROR_BALANCE_ROUND_ROBIN 1
|
||||||
|
#define G_MIRROR_BALANCE_LOAD 2
|
||||||
|
#define G_MIRROR_BALANCE_SPLIT 3
|
||||||
|
#define G_MIRROR_BALANCE_MIN G_MIRROR_BALANCE_NONE
|
||||||
|
#define G_MIRROR_BALANCE_MAX G_MIRROR_BALANCE_SPLIT
|
||||||
|
|
||||||
|
#define G_MIRROR_DISK_FLAG_DIRTY 0x0000000000000001ULL
|
||||||
|
#define G_MIRROR_DISK_FLAG_SYNCHRONIZING 0x0000000000000002ULL
|
||||||
|
#define G_MIRROR_DISK_FLAG_FORCE_SYNC 0x0000000000000004ULL
|
||||||
|
#define G_MIRROR_DISK_FLAG_INACTIVE 0x0000000000000008ULL
|
||||||
|
#define G_MIRROR_DISK_FLAG_MASK (G_MIRROR_DISK_FLAG_DIRTY | \
|
||||||
|
G_MIRROR_DISK_FLAG_SYNCHRONIZING | \
|
||||||
|
G_MIRROR_DISK_FLAG_FORCE_SYNC | \
|
||||||
|
G_MIRROR_DISK_FLAG_INACTIVE)
|
||||||
|
|
||||||
|
#define G_MIRROR_DEVICE_FLAG_NOAUTOSYNC 0x0000000000000001ULL
|
||||||
|
#define G_MIRROR_DEVICE_FLAG_MASK (G_MIRROR_DEVICE_FLAG_NOAUTOSYNC)
|
||||||
|
|
||||||
|
#ifdef _KERNEL
|
||||||
|
extern u_int g_mirror_debug;
|
||||||
|
|
||||||
|
#define G_MIRROR_DEBUG(lvl, ...) do { \
|
||||||
|
if (g_mirror_debug >= (lvl)) { \
|
||||||
|
printf("GEOM_MIRROR"); \
|
||||||
|
if (g_mirror_debug > 0) \
|
||||||
|
printf("[%u]", lvl); \
|
||||||
|
printf(": "); \
|
||||||
|
printf(__VA_ARGS__); \
|
||||||
|
printf("\n"); \
|
||||||
|
} \
|
||||||
|
} while (0)
|
||||||
|
#define G_MIRROR_LOGREQ(lvl, bp, ...) do { \
|
||||||
|
if (g_mirror_debug >= (lvl)) { \
|
||||||
|
printf("GEOM_MIRROR"); \
|
||||||
|
if (g_mirror_debug > 0) \
|
||||||
|
printf("[%u]", lvl); \
|
||||||
|
printf(": "); \
|
||||||
|
printf(__VA_ARGS__); \
|
||||||
|
printf(" "); \
|
||||||
|
g_print_bio(bp); \
|
||||||
|
printf("\n"); \
|
||||||
|
} \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Informations needed for synchronization.
|
||||||
|
*/
|
||||||
|
struct g_mirror_disk_sync {
|
||||||
|
struct g_consumer *ds_consumer; /* Consumer connected to our mirror. */
|
||||||
|
off_t ds_offset; /* Offset of next request to send. */
|
||||||
|
off_t ds_offset_done; /* Offset of already synchronized
|
||||||
|
region. */
|
||||||
|
u_int ds_syncid; /* Disk's synchronization ID. */
|
||||||
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Informations needed for synchronization.
|
||||||
|
*/
|
||||||
|
struct g_mirror_device_sync {
|
||||||
|
struct g_geom *ds_geom; /* Synchronization geom. */
|
||||||
|
size_t ds_block; /* Synchronization request size. */
|
||||||
|
u_int ds_ndisks; /* Number of disks in SYNCHRONIZING
|
||||||
|
state. */
|
||||||
|
uma_zone_t ds_zone; /* UMA zone for synchronization
|
||||||
|
blocks. */
|
||||||
|
};
|
||||||
|
|
||||||
|
#define G_MIRROR_DISK_STATE_NONE 0
|
||||||
|
#define G_MIRROR_DISK_STATE_NEW 1
|
||||||
|
#define G_MIRROR_DISK_STATE_ACTIVE 2
|
||||||
|
#define G_MIRROR_DISK_STATE_STALE 3
|
||||||
|
#define G_MIRROR_DISK_STATE_SYNCHRONIZING 4
|
||||||
|
#define G_MIRROR_DISK_STATE_DISCONNECTED 5
|
||||||
|
#define G_MIRROR_DISK_STATE_DESTROY 6
|
||||||
|
struct g_mirror_disk {
|
||||||
|
uint32_t d_id; /* Disk ID. */
|
||||||
|
struct g_consumer *d_consumer; /* Consumer. */
|
||||||
|
struct g_mirror_softc *d_softc; /* Back-pointer to softc. */
|
||||||
|
struct proc *d_worker;
|
||||||
|
int d_state; /* Disk state. */
|
||||||
|
u_int d_priority; /* Disk priority. */
|
||||||
|
struct bintime d_delay; /* Disk delay. */
|
||||||
|
struct bintime d_last_used; /* When disk was last used. */
|
||||||
|
uint64_t d_flags; /* Additional flags. */
|
||||||
|
struct g_mirror_disk_sync d_sync;/* Sync information. */
|
||||||
|
LIST_ENTRY(g_mirror_disk) d_next;
|
||||||
|
};
|
||||||
|
#define d_name d_consumer->provider->name
|
||||||
|
|
||||||
|
#define G_MIRROR_EVENT_DONTWAIT 0x1
|
||||||
|
#define G_MIRROR_EVENT_WAIT 0x2
|
||||||
|
#define G_MIRROR_EVENT_DEVICE 0x4
|
||||||
|
#define G_MIRROR_EVENT_DONE 0x8
|
||||||
|
struct g_mirror_event {
|
||||||
|
struct g_mirror_disk *e_disk;
|
||||||
|
int e_state;
|
||||||
|
int e_flags;
|
||||||
|
int e_error;
|
||||||
|
TAILQ_ENTRY(g_mirror_event) e_next;
|
||||||
|
};
|
||||||
|
|
||||||
|
#define G_MIRROR_DEVICE_FLAG_DESTROY 0x0100000000000000ULL
|
||||||
|
#define G_MIRROR_DEVICE_FLAG_WAIT 0x0200000000000000ULL
|
||||||
|
|
||||||
|
#define G_MIRROR_DEVICE_STATE_STARTING 0
|
||||||
|
#define G_MIRROR_DEVICE_STATE_RUNNING 1
|
||||||
|
|
||||||
|
#define G_MIRROR_BUMP_ON_FIRST_WRITE 1
|
||||||
|
#define G_MIRROR_BUMP_IMMEDIATELY 2
|
||||||
|
struct g_mirror_softc {
|
||||||
|
u_int sc_state; /* Device state. */
|
||||||
|
uint32_t sc_slice; /* Slice size. */
|
||||||
|
uint8_t sc_balance; /* Balance algorithm. */
|
||||||
|
uint64_t sc_mediasize; /* Device size. */
|
||||||
|
uint32_t sc_sectorsize; /* Sector size. */
|
||||||
|
uint64_t sc_flags; /* Additional flags. */
|
||||||
|
|
||||||
|
struct g_geom *sc_geom;
|
||||||
|
struct g_provider *sc_provider;
|
||||||
|
|
||||||
|
uint32_t sc_id; /* Mirror unique ID. */
|
||||||
|
|
||||||
|
struct bio_queue_head sc_queue;
|
||||||
|
struct mtx sc_queue_mtx;
|
||||||
|
struct proc *sc_worker;
|
||||||
|
|
||||||
|
LIST_HEAD(, g_mirror_disk) sc_disks;
|
||||||
|
u_int sc_ndisks; /* Number of disks. */
|
||||||
|
struct g_mirror_disk *sc_hint;
|
||||||
|
|
||||||
|
u_int sc_syncid; /* Synchronization ID. */
|
||||||
|
int sc_bump_syncid;
|
||||||
|
struct g_mirror_device_sync sc_sync;
|
||||||
|
|
||||||
|
TAILQ_HEAD(, g_mirror_event) sc_events;
|
||||||
|
struct mtx sc_events_mtx;
|
||||||
|
|
||||||
|
struct callout sc_callout;
|
||||||
|
};
|
||||||
|
#define sc_name sc_geom->name
|
||||||
|
|
||||||
|
u_int g_mirror_ndisks(struct g_mirror_softc *sc, int state);
|
||||||
|
int g_mirror_destroy(struct g_mirror_softc *sc, boolean_t force);
|
||||||
|
int g_mirror_event_send(void *arg, int state, int flags);
|
||||||
|
struct g_mirror_metadata;
|
||||||
|
void g_mirror_fill_metadata(struct g_mirror_softc *sc,
|
||||||
|
struct g_mirror_disk *disk, struct g_mirror_metadata *md);
|
||||||
|
void g_mirror_update_metadata(struct g_mirror_disk *disk);
|
||||||
|
|
||||||
|
g_ctl_req_t g_mirror_config;
|
||||||
|
#endif /* _KERNEL */
|
||||||
|
|
||||||
|
struct g_mirror_metadata {
|
||||||
|
char md_magic[16]; /* Magic value. */
|
||||||
|
uint32_t md_version; /* Version number. */
|
||||||
|
char md_name[16]; /* Mirror name. */
|
||||||
|
uint32_t md_mid; /* Mirror unique ID. */
|
||||||
|
uint32_t md_did; /* Disk unique ID. */
|
||||||
|
uint8_t md_all; /* Number of disks in mirror. */
|
||||||
|
uint32_t md_syncid; /* Synchronization ID. */
|
||||||
|
uint8_t md_priority; /* Disk priority. */
|
||||||
|
uint32_t md_slice; /* Slice size. */
|
||||||
|
uint8_t md_balance; /* Balance type. */
|
||||||
|
uint64_t md_mediasize; /* Size of the smallest
|
||||||
|
disk in mirror. */
|
||||||
|
uint32_t md_sectorsize; /* Sector size. */
|
||||||
|
uint64_t md_sync_offset; /* Synchronized offset. */
|
||||||
|
uint64_t md_mflags; /* Additional mirror flags. */
|
||||||
|
uint64_t md_dflags; /* Additional disk flags. */
|
||||||
|
u_char md_hash[16]; /* MD5 hash. */
|
||||||
|
};
|
||||||
|
static __inline void
|
||||||
|
mirror_metadata_encode(struct g_mirror_metadata *md, u_char *data)
|
||||||
|
{
|
||||||
|
MD5_CTX ctx;
|
||||||
|
|
||||||
|
bcopy(md->md_magic, data, 16);
|
||||||
|
le32enc(data + 16, md->md_version);
|
||||||
|
bcopy(md->md_name, data + 20, 16);
|
||||||
|
le32enc(data + 36, md->md_mid);
|
||||||
|
le32enc(data + 40, md->md_did);
|
||||||
|
*(data + 44) = md->md_all;
|
||||||
|
le32enc(data + 45, md->md_syncid);
|
||||||
|
*(data + 49) = md->md_priority;
|
||||||
|
le32enc(data + 50, md->md_slice);
|
||||||
|
*(data + 54) = md->md_balance;
|
||||||
|
le64enc(data + 55, md->md_mediasize);
|
||||||
|
le32enc(data + 63, md->md_sectorsize);
|
||||||
|
le64enc(data + 67, md->md_sync_offset);
|
||||||
|
le64enc(data + 75, md->md_mflags);
|
||||||
|
le64enc(data + 83, md->md_dflags);
|
||||||
|
MD5Init(&ctx);
|
||||||
|
MD5Update(&ctx, data, 91);
|
||||||
|
MD5Final(md->md_hash, &ctx);
|
||||||
|
bcopy(md->md_hash, data + 91, 16);
|
||||||
|
}
|
||||||
|
static __inline int
|
||||||
|
mirror_metadata_decode(const u_char *data, struct g_mirror_metadata *md)
|
||||||
|
{
|
||||||
|
MD5_CTX ctx;
|
||||||
|
|
||||||
|
bcopy(data, md->md_magic, 16);
|
||||||
|
md->md_version = le32dec(data + 16);
|
||||||
|
bcopy(data + 20, md->md_name, 16);
|
||||||
|
md->md_mid = le32dec(data + 36);
|
||||||
|
md->md_did = le32dec(data + 40);
|
||||||
|
md->md_all = *(data + 44);
|
||||||
|
md->md_syncid = le32dec(data + 45);
|
||||||
|
md->md_priority = *(data + 49);
|
||||||
|
md->md_slice = le32dec(data + 50);
|
||||||
|
md->md_balance = *(data + 54);
|
||||||
|
md->md_mediasize = le64dec(data + 55);
|
||||||
|
md->md_sectorsize = le32dec(data + 63);
|
||||||
|
md->md_sync_offset = le64dec(data + 67);
|
||||||
|
md->md_mflags = le64dec(data + 75);
|
||||||
|
md->md_dflags = le64dec(data + 83);
|
||||||
|
bcopy(data + 91, md->md_hash, 16);
|
||||||
|
MD5Init(&ctx);
|
||||||
|
MD5Update(&ctx, data, 91);
|
||||||
|
MD5Final(md->md_hash, &ctx);
|
||||||
|
if (bcmp(md->md_hash, data + 91, 16) != 0)
|
||||||
|
return (EINVAL);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
static __inline const char *
|
||||||
|
balance_name(u_int balance)
|
||||||
|
{
|
||||||
|
static const char *algorithms[] = {
|
||||||
|
[G_MIRROR_BALANCE_NONE] = "none",
|
||||||
|
[G_MIRROR_BALANCE_ROUND_ROBIN] = "round-robin",
|
||||||
|
[G_MIRROR_BALANCE_LOAD] = "load",
|
||||||
|
[G_MIRROR_BALANCE_SPLIT] = "split",
|
||||||
|
[G_MIRROR_BALANCE_MAX + 1] = "unknown"
|
||||||
|
};
|
||||||
|
|
||||||
|
if (balance > G_MIRROR_BALANCE_MAX)
|
||||||
|
balance = G_MIRROR_BALANCE_MAX + 1;
|
||||||
|
|
||||||
|
return (algorithms[balance]);
|
||||||
|
}
|
||||||
|
|
||||||
|
static __inline int
|
||||||
|
balance_id(const char *name)
|
||||||
|
{
|
||||||
|
static const char *algorithms[] = {
|
||||||
|
[G_MIRROR_BALANCE_NONE] = "none",
|
||||||
|
[G_MIRROR_BALANCE_ROUND_ROBIN] = "round-robin",
|
||||||
|
[G_MIRROR_BALANCE_LOAD] = "load",
|
||||||
|
[G_MIRROR_BALANCE_SPLIT] = "split"
|
||||||
|
};
|
||||||
|
int n;
|
||||||
|
|
||||||
|
for (n = G_MIRROR_BALANCE_MIN; n <= G_MIRROR_BALANCE_MAX; n++) {
|
||||||
|
if (strcmp(name, algorithms[n]) == 0)
|
||||||
|
return (n);
|
||||||
|
}
|
||||||
|
return (-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
static __inline void
|
||||||
|
mirror_metadata_dump(const struct g_mirror_metadata *md)
|
||||||
|
{
|
||||||
|
static const char hex[] = "0123456789abcdef";
|
||||||
|
char hash[16 * 2 + 1];
|
||||||
|
u_int i;
|
||||||
|
|
||||||
|
printf(" magic: %s\n", md->md_magic);
|
||||||
|
printf(" version: %u\n", (u_int)md->md_version);
|
||||||
|
printf(" name: %s\n", md->md_name);
|
||||||
|
printf(" mid: %u\n", (u_int)md->md_mid);
|
||||||
|
printf(" did: %u\n", (u_int)md->md_did);
|
||||||
|
printf(" all: %u\n", (u_int)md->md_all);
|
||||||
|
printf(" syncid: %u\n", (u_int)md->md_syncid);
|
||||||
|
printf(" priority: %u\n", (u_int)md->md_priority);
|
||||||
|
printf(" slice: %u\n", (u_int)md->md_slice);
|
||||||
|
printf(" balance: %s\n", balance_name((u_int)md->md_balance));
|
||||||
|
printf(" mediasize: %jd\n", (intmax_t)md->md_mediasize);
|
||||||
|
printf("sectorsize: %u\n", (u_int)md->md_sectorsize);
|
||||||
|
printf("syncoffset: %jd\n", (intmax_t)md->md_sync_offset);
|
||||||
|
printf(" mflags:");
|
||||||
|
if (md->md_mflags == 0)
|
||||||
|
printf(" NONE");
|
||||||
|
else {
|
||||||
|
if ((md->md_mflags & G_MIRROR_DEVICE_FLAG_NOAUTOSYNC) != 0)
|
||||||
|
printf(" NOAUTOSYNC");
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
printf(" dflags:");
|
||||||
|
if (md->md_mflags == 0)
|
||||||
|
printf(" NONE");
|
||||||
|
else {
|
||||||
|
if ((md->md_dflags & G_MIRROR_DISK_FLAG_DIRTY) != 0)
|
||||||
|
printf(" DIRTY");
|
||||||
|
if ((md->md_dflags & G_MIRROR_DISK_FLAG_SYNCHRONIZING) != 0)
|
||||||
|
printf(" SYNCHRONIZING");
|
||||||
|
if ((md->md_dflags & G_MIRROR_DISK_FLAG_FORCE_SYNC) != 0)
|
||||||
|
printf(" FORCE_SYNC");
|
||||||
|
if ((md->md_dflags & G_MIRROR_DISK_FLAG_INACTIVE) != 0)
|
||||||
|
printf(" INACTIVE");
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
bzero(hash, sizeof(hash));
|
||||||
|
for (i = 0; i < 16; i++) {
|
||||||
|
hash[i * 2] = hex[md->md_hash[i] >> 4];
|
||||||
|
hash[i * 2 + 1] = hex[md->md_hash[i] & 0x0f];
|
||||||
|
}
|
||||||
|
printf(" MD5 hash: %s\n", hash);
|
||||||
|
}
|
||||||
|
#endif /* !_G_MIRROR_H_ */
|
617
sys/geom/mirror/g_mirror_ctl.c
Normal file
617
sys/geom/mirror/g_mirror_ctl.c
Normal file
@ -0,0 +1,617 @@
|
|||||||
|
/*-
|
||||||
|
* Copyright (c) 2004 Pawel Jakub Dawidek <pjd@FreeBSD.org>
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
* 1. Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
|
||||||
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
|
||||||
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||||
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||||
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||||
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||||
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||||
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||||
|
* SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <sys/cdefs.h>
|
||||||
|
__FBSDID("$FreeBSD$");
|
||||||
|
|
||||||
|
#include <sys/param.h>
|
||||||
|
#include <sys/systm.h>
|
||||||
|
#include <sys/kernel.h>
|
||||||
|
#include <sys/module.h>
|
||||||
|
#include <sys/lock.h>
|
||||||
|
#include <sys/mutex.h>
|
||||||
|
#include <sys/bio.h>
|
||||||
|
#include <sys/sysctl.h>
|
||||||
|
#include <sys/malloc.h>
|
||||||
|
#include <sys/bitstring.h>
|
||||||
|
#include <vm/uma.h>
|
||||||
|
#include <machine/atomic.h>
|
||||||
|
#include <geom/geom.h>
|
||||||
|
#include <sys/proc.h>
|
||||||
|
#include <sys/kthread.h>
|
||||||
|
#include <geom/mirror/g_mirror.h>
|
||||||
|
|
||||||
|
|
||||||
|
static struct g_mirror_softc *
|
||||||
|
g_mirror_find_device(struct g_class *mp, const char *name)
|
||||||
|
{
|
||||||
|
struct g_mirror_softc *sc;
|
||||||
|
struct g_geom *gp;
|
||||||
|
|
||||||
|
g_topology_assert();
|
||||||
|
LIST_FOREACH(gp, &mp->geom, geom) {
|
||||||
|
sc = gp->softc;
|
||||||
|
if (sc == NULL)
|
||||||
|
continue;
|
||||||
|
if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) != 0)
|
||||||
|
continue;
|
||||||
|
if (strcmp(gp->name, name) == 0 ||
|
||||||
|
strcmp(sc->sc_name, name) == 0) {
|
||||||
|
return (sc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return (NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
static struct g_mirror_disk *
|
||||||
|
g_mirror_find_disk(struct g_mirror_softc *sc, const char *name)
|
||||||
|
{
|
||||||
|
struct g_mirror_disk *disk;
|
||||||
|
|
||||||
|
g_topology_assert();
|
||||||
|
LIST_FOREACH(disk, &sc->sc_disks, d_next) {
|
||||||
|
if (disk->d_consumer == NULL)
|
||||||
|
continue;
|
||||||
|
if (disk->d_consumer->provider == NULL)
|
||||||
|
continue;
|
||||||
|
if (strcmp(disk->d_consumer->provider->name, name) == 0)
|
||||||
|
return (disk);
|
||||||
|
}
|
||||||
|
return (NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
g_mirror_ctl_configure(struct gctl_req *req, struct g_class *mp)
|
||||||
|
{
|
||||||
|
struct g_mirror_softc *sc;
|
||||||
|
struct g_mirror_disk *disk;
|
||||||
|
const char *name, *balancep;
|
||||||
|
intmax_t *slicep;
|
||||||
|
uint32_t slice;
|
||||||
|
uint8_t balance;
|
||||||
|
int *nargs, *autosync, *noautosync, do_sync = 0;
|
||||||
|
|
||||||
|
g_topology_assert();
|
||||||
|
nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
|
||||||
|
if (*nargs != 1) {
|
||||||
|
gctl_error(req, "Invalid number of arguments.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
name = gctl_get_asciiparam(req, "arg0");
|
||||||
|
sc = g_mirror_find_device(mp, name);
|
||||||
|
if (sc == NULL) {
|
||||||
|
gctl_error(req, "No such device: %s.", name);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (g_mirror_ndisks(sc, -1) < sc->sc_ndisks) {
|
||||||
|
gctl_error(req, "Not all disks connected.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
balancep = gctl_get_asciiparam(req, "balance");
|
||||||
|
if (strcmp(balancep, "none") == 0)
|
||||||
|
balance = sc->sc_balance;
|
||||||
|
else {
|
||||||
|
if (balance_id(balancep) == -1) {
|
||||||
|
gctl_error(req, "Invalid balance algorithm.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
balance = balance_id(balancep);
|
||||||
|
}
|
||||||
|
slicep = gctl_get_paraml(req, "slice", sizeof(*slicep));
|
||||||
|
if (*slicep == -1)
|
||||||
|
slice = sc->sc_slice;
|
||||||
|
else
|
||||||
|
slice = *slicep;
|
||||||
|
autosync = gctl_get_paraml(req, "autosync", sizeof(*autosync));
|
||||||
|
if (autosync == NULL) {
|
||||||
|
gctl_error(req, "No '%s' argument.", "autosync");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
noautosync = gctl_get_paraml(req, "noautosync", sizeof(*noautosync));
|
||||||
|
if (noautosync == NULL) {
|
||||||
|
gctl_error(req, "No '%s' argument.", "noautosync");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (sc->sc_balance == balance && sc->sc_slice == slice && !*autosync &&
|
||||||
|
!*noautosync) {
|
||||||
|
gctl_error(req, "Nothing has changed.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (*autosync && *noautosync) {
|
||||||
|
gctl_error(req, "'%s' and '%s' specified.", "autosync",
|
||||||
|
"noautosync");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
sc->sc_balance = balance;
|
||||||
|
sc->sc_slice = slice;
|
||||||
|
if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOAUTOSYNC) != 0) {
|
||||||
|
if (*autosync) {
|
||||||
|
sc->sc_flags &= ~G_MIRROR_DEVICE_FLAG_NOAUTOSYNC;
|
||||||
|
do_sync = 1;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (*noautosync)
|
||||||
|
sc->sc_flags |= G_MIRROR_DEVICE_FLAG_NOAUTOSYNC;
|
||||||
|
}
|
||||||
|
LIST_FOREACH(disk, &sc->sc_disks, d_next) {
|
||||||
|
if (do_sync) {
|
||||||
|
if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING)
|
||||||
|
disk->d_flags &= ~G_MIRROR_DISK_FLAG_FORCE_SYNC;
|
||||||
|
}
|
||||||
|
g_mirror_update_metadata(disk);
|
||||||
|
if (do_sync) {
|
||||||
|
if (disk->d_state == G_MIRROR_DISK_STATE_STALE) {
|
||||||
|
g_mirror_event_send(disk,
|
||||||
|
G_MIRROR_DISK_STATE_DISCONNECTED,
|
||||||
|
G_MIRROR_EVENT_DONTWAIT);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
g_mirror_ctl_rebuild(struct gctl_req *req, struct g_class *mp)
|
||||||
|
{
|
||||||
|
struct g_mirror_softc *sc;
|
||||||
|
struct g_mirror_disk *disk;
|
||||||
|
const char *name;
|
||||||
|
char param[16];
|
||||||
|
int *nargs;
|
||||||
|
u_int i;
|
||||||
|
|
||||||
|
g_topology_assert();
|
||||||
|
nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
|
||||||
|
if (nargs == NULL) {
|
||||||
|
gctl_error(req, "No '%s' argument.", "nargs");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (*nargs < 2) {
|
||||||
|
gctl_error(req, "Too few arguments.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
name = gctl_get_asciiparam(req, "arg0");
|
||||||
|
if (name == NULL) {
|
||||||
|
gctl_error(req, "No 'arg%u' argument.", 0);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
sc = g_mirror_find_device(mp, name);
|
||||||
|
if (sc == NULL) {
|
||||||
|
gctl_error(req, "No such device: %s.", name);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (i = 1; i < (u_int)*nargs; i++) {
|
||||||
|
snprintf(param, sizeof(param), "arg%u", i);
|
||||||
|
name = gctl_get_asciiparam(req, param);
|
||||||
|
if (name == NULL) {
|
||||||
|
gctl_error(req, "No 'arg%u' argument.", i);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
disk = g_mirror_find_disk(sc, name);
|
||||||
|
if (disk == NULL) {
|
||||||
|
gctl_error(req, "No such provider: %s.", name);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) == 1 &&
|
||||||
|
disk->d_state == G_MIRROR_DISK_STATE_ACTIVE) {
|
||||||
|
/*
|
||||||
|
* This is the last active disk. There will be nothing
|
||||||
|
* to rebuild it from, so deny this request.
|
||||||
|
*/
|
||||||
|
gctl_error(req,
|
||||||
|
"Provider %s is the last active provider in %s.",
|
||||||
|
name, sc->sc_geom->name);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
* Do rebuild by resetting syncid and disconnecting disk.
|
||||||
|
* It'll be retasted, connected to the mirror and
|
||||||
|
* synchronized.
|
||||||
|
*/
|
||||||
|
disk->d_sync.ds_syncid = 0;
|
||||||
|
if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOAUTOSYNC) != 0)
|
||||||
|
disk->d_flags |= G_MIRROR_DISK_FLAG_FORCE_SYNC;
|
||||||
|
g_mirror_update_metadata(disk);
|
||||||
|
g_mirror_event_send(disk, G_MIRROR_DISK_STATE_DISCONNECTED,
|
||||||
|
G_MIRROR_EVENT_WAIT);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
g_mirror_ctl_insert(struct gctl_req *req, struct g_class *mp)
|
||||||
|
{
|
||||||
|
struct g_mirror_softc *sc;
|
||||||
|
struct g_mirror_disk *disk;
|
||||||
|
struct g_mirror_metadata md;
|
||||||
|
struct g_provider *pp;
|
||||||
|
struct g_consumer *cp;
|
||||||
|
const char *name;
|
||||||
|
char param[16];
|
||||||
|
u_char *sector;
|
||||||
|
u_int i, n;
|
||||||
|
int error, *nargs, *inactive;
|
||||||
|
struct {
|
||||||
|
struct g_provider *provider;
|
||||||
|
struct g_consumer *consumer;
|
||||||
|
} *disks;
|
||||||
|
|
||||||
|
g_topology_assert();
|
||||||
|
nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
|
||||||
|
if (nargs == NULL) {
|
||||||
|
gctl_error(req, "No '%s' argument.", "nargs");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (*nargs < 2) {
|
||||||
|
gctl_error(req, "Too few arguments.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
inactive = gctl_get_paraml(req, "inactive", sizeof(*inactive));
|
||||||
|
if (inactive == NULL) {
|
||||||
|
gctl_error(req, "No '%s' argument.", "inactive");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
name = gctl_get_asciiparam(req, "arg0");
|
||||||
|
if (name == NULL) {
|
||||||
|
gctl_error(req, "No 'arg%u' argument.", 0);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
sc = g_mirror_find_device(mp, name);
|
||||||
|
if (sc == NULL) {
|
||||||
|
gctl_error(req, "No such device: %s.", name);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (g_mirror_ndisks(sc, -1) < sc->sc_ndisks) {
|
||||||
|
gctl_error(req, "Not all disks connected.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
disks = g_malloc(sizeof(*disks) * (*nargs), M_WAITOK | M_ZERO);
|
||||||
|
for (i = 1, n = 0; i < (u_int)*nargs; i++) {
|
||||||
|
snprintf(param, sizeof(param), "arg%u", i);
|
||||||
|
name = gctl_get_asciiparam(req, param);
|
||||||
|
if (name == NULL) {
|
||||||
|
gctl_error(req, "No 'arg%u' argument.", i);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
|
||||||
|
name += strlen("/dev/");
|
||||||
|
pp = g_provider_by_name(name);
|
||||||
|
if (pp == NULL) {
|
||||||
|
gctl_error(req, "Unknown provider %s.", name);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (sc->sc_provider->mediasize > pp->mediasize) {
|
||||||
|
gctl_error(req, "Provider %s too small.", name);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if ((sc->sc_provider->sectorsize % pp->sectorsize) != 0) {
|
||||||
|
gctl_error(req, "Invalid sectorsize of provider %s.",
|
||||||
|
name);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
cp = g_new_consumer(sc->sc_geom);
|
||||||
|
if (g_attach(cp, pp) != 0) {
|
||||||
|
g_destroy_consumer(cp);
|
||||||
|
gctl_error(req, "Cannot attach to provider %s.", name);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (g_access(cp, 0, 1, 1) != 0) {
|
||||||
|
g_detach(cp);
|
||||||
|
g_destroy_consumer(cp);
|
||||||
|
gctl_error(req, "Cannot access provider %s.", name);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
disks[n].provider = pp;
|
||||||
|
disks[n].consumer = cp;
|
||||||
|
n++;
|
||||||
|
}
|
||||||
|
if (n == 0) {
|
||||||
|
g_free(disks);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
sc->sc_ndisks += n;
|
||||||
|
again:
|
||||||
|
for (i = 0; i < n; i++) {
|
||||||
|
if (disks[i].consumer == NULL)
|
||||||
|
continue;
|
||||||
|
g_mirror_fill_metadata(sc, NULL, &md);
|
||||||
|
if (*inactive)
|
||||||
|
md.md_dflags |= G_MIRROR_DISK_FLAG_INACTIVE;
|
||||||
|
pp = disks[i].provider;
|
||||||
|
sector = g_malloc(pp->sectorsize, M_WAITOK);
|
||||||
|
mirror_metadata_encode(&md, sector);
|
||||||
|
error = g_write_data(disks[i].consumer,
|
||||||
|
pp->mediasize - pp->sectorsize, sector, pp->sectorsize);
|
||||||
|
g_free(sector);
|
||||||
|
if (error != 0) {
|
||||||
|
gctl_error(req, "Cannot store metadata on %s.",
|
||||||
|
pp->name);
|
||||||
|
g_access(disks[i].consumer, 0, -1, -1);
|
||||||
|
g_detach(disks[i].consumer);
|
||||||
|
g_destroy_consumer(disks[i].consumer);
|
||||||
|
disks[i].consumer = NULL;
|
||||||
|
disks[i].provider = NULL;
|
||||||
|
sc->sc_ndisks--;
|
||||||
|
goto again;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (i == 0) {
|
||||||
|
/* All writes failed. */
|
||||||
|
g_free(disks);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
LIST_FOREACH(disk, &sc->sc_disks, d_next) {
|
||||||
|
g_mirror_update_metadata(disk);
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
* Release provider and wait for retaste.
|
||||||
|
*/
|
||||||
|
for (i = 0; i < n; i++) {
|
||||||
|
if (disks[i].consumer == NULL)
|
||||||
|
continue;
|
||||||
|
g_access(disks[i].consumer, 0, -1, -1);
|
||||||
|
g_detach(disks[i].consumer);
|
||||||
|
g_destroy_consumer(disks[i].consumer);
|
||||||
|
}
|
||||||
|
g_free(disks);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
g_mirror_ctl_remove(struct gctl_req *req, struct g_class *mp)
|
||||||
|
{
|
||||||
|
struct g_mirror_softc *sc;
|
||||||
|
struct g_mirror_disk *disk;
|
||||||
|
const char *name;
|
||||||
|
char param[16];
|
||||||
|
int *nargs;
|
||||||
|
u_int i;
|
||||||
|
|
||||||
|
g_topology_assert();
|
||||||
|
nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
|
||||||
|
if (nargs == NULL) {
|
||||||
|
gctl_error(req, "No '%s' argument.", "nargs");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (*nargs < 2) {
|
||||||
|
gctl_error(req, "Too few arguments.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
name = gctl_get_asciiparam(req, "arg0");
|
||||||
|
if (name == NULL) {
|
||||||
|
gctl_error(req, "No 'arg%u' argument.", 0);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
sc = g_mirror_find_device(mp, name);
|
||||||
|
if (sc == NULL) {
|
||||||
|
gctl_error(req, "No such device: %s.", name);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (g_mirror_ndisks(sc, -1) < sc->sc_ndisks) {
|
||||||
|
gctl_error(req, "Not all disks connected.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (i = 1; i < (u_int)*nargs; i++) {
|
||||||
|
snprintf(param, sizeof(param), "arg%u", i);
|
||||||
|
name = gctl_get_asciiparam(req, param);
|
||||||
|
if (name == NULL) {
|
||||||
|
gctl_error(req, "No 'arg%u' argument.", i);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
disk = g_mirror_find_disk(sc, name);
|
||||||
|
if (disk == NULL) {
|
||||||
|
gctl_error(req, "No such provider: %s.", name);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
g_mirror_event_send(disk, G_MIRROR_DISK_STATE_DESTROY,
|
||||||
|
G_MIRROR_EVENT_WAIT);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
g_mirror_ctl_deactivate(struct gctl_req *req, struct g_class *mp)
|
||||||
|
{
|
||||||
|
struct g_mirror_softc *sc;
|
||||||
|
struct g_mirror_disk *disk;
|
||||||
|
const char *name;
|
||||||
|
char param[16];
|
||||||
|
int *nargs;
|
||||||
|
u_int i;
|
||||||
|
|
||||||
|
g_topology_assert();
|
||||||
|
nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
|
||||||
|
if (nargs == NULL) {
|
||||||
|
gctl_error(req, "No '%s' argument.", "nargs");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (*nargs < 2) {
|
||||||
|
gctl_error(req, "Too few arguments.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
name = gctl_get_asciiparam(req, "arg0");
|
||||||
|
if (name == NULL) {
|
||||||
|
gctl_error(req, "No 'arg%u' argument.", 0);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
sc = g_mirror_find_device(mp, name);
|
||||||
|
if (sc == NULL) {
|
||||||
|
gctl_error(req, "No such device: %s.", name);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (i = 1; i < (u_int)*nargs; i++) {
|
||||||
|
snprintf(param, sizeof(param), "arg%u", i);
|
||||||
|
name = gctl_get_asciiparam(req, param);
|
||||||
|
if (name == NULL) {
|
||||||
|
gctl_error(req, "No 'arg%u' argument.", i);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
disk = g_mirror_find_disk(sc, name);
|
||||||
|
if (disk == NULL) {
|
||||||
|
gctl_error(req, "No such provider: %s.", name);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
* Do rebuild by resetting syncid and disconnecting disk.
|
||||||
|
* It'll be retasted, connected to the mirror and
|
||||||
|
* synchronized.
|
||||||
|
*/
|
||||||
|
disk->d_flags |= G_MIRROR_DISK_FLAG_INACTIVE;
|
||||||
|
disk->d_flags &= ~G_MIRROR_DISK_FLAG_FORCE_SYNC;
|
||||||
|
g_mirror_update_metadata(disk);
|
||||||
|
sc->sc_bump_syncid = G_MIRROR_BUMP_ON_FIRST_WRITE;
|
||||||
|
g_mirror_event_send(disk, G_MIRROR_DISK_STATE_DISCONNECTED,
|
||||||
|
G_MIRROR_EVENT_WAIT);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
g_mirror_ctl_forget(struct gctl_req *req, struct g_class *mp)
|
||||||
|
{
|
||||||
|
struct g_mirror_softc *sc;
|
||||||
|
struct g_mirror_disk *disk;
|
||||||
|
const char *name;
|
||||||
|
char param[16];
|
||||||
|
int *nargs;
|
||||||
|
u_int i;
|
||||||
|
|
||||||
|
g_topology_assert();
|
||||||
|
nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
|
||||||
|
if (nargs == NULL) {
|
||||||
|
gctl_error(req, "No '%s' argument.", "nargs");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (*nargs < 1) {
|
||||||
|
gctl_error(req, "Missing device(s).");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (i = 0; i < (u_int)*nargs; i++) {
|
||||||
|
snprintf(param, sizeof(param), "arg%u", i);
|
||||||
|
name = gctl_get_asciiparam(req, param);
|
||||||
|
if (name == NULL) {
|
||||||
|
gctl_error(req, "No 'arg%u' argument.", i);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
sc = g_mirror_find_device(mp, name);
|
||||||
|
if (sc == NULL) {
|
||||||
|
gctl_error(req, "No such device: %s.", name);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (g_mirror_ndisks(sc, -1) == sc->sc_ndisks) {
|
||||||
|
G_MIRROR_DEBUG(1,
|
||||||
|
"All disks connected in %s, skipping.",
|
||||||
|
sc->sc_name);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
sc->sc_ndisks = g_mirror_ndisks(sc, -1);
|
||||||
|
LIST_FOREACH(disk, &sc->sc_disks, d_next) {
|
||||||
|
g_mirror_update_metadata(disk);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
g_mirror_ctl_stop(struct gctl_req *req, struct g_class *mp)
|
||||||
|
{
|
||||||
|
struct g_mirror_softc *sc;
|
||||||
|
int *force, *nargs, error;
|
||||||
|
const char *name;
|
||||||
|
char param[16];
|
||||||
|
u_int i;
|
||||||
|
|
||||||
|
g_topology_assert();
|
||||||
|
|
||||||
|
nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
|
||||||
|
if (nargs == NULL) {
|
||||||
|
gctl_error(req, "No '%s' argument.", "nargs");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (*nargs < 1) {
|
||||||
|
gctl_error(req, "Missing device(s).");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
force = gctl_get_paraml(req, "force", sizeof(*force));
|
||||||
|
if (force == NULL) {
|
||||||
|
gctl_error(req, "No '%s' argument.", "force");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (i = 0; i < (u_int)*nargs; i++) {
|
||||||
|
snprintf(param, sizeof(param), "arg%u", i);
|
||||||
|
name = gctl_get_asciiparam(req, param);
|
||||||
|
if (name == NULL) {
|
||||||
|
gctl_error(req, "No 'arg%u' argument.", i);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
sc = g_mirror_find_device(mp, name);
|
||||||
|
if (sc == NULL) {
|
||||||
|
gctl_error(req, "No such device: %s.", name);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
error = g_mirror_destroy(sc, *force);
|
||||||
|
if (error != 0) {
|
||||||
|
gctl_error(req, "Cannot destroy device %s (error=%d).",
|
||||||
|
sc->sc_geom->name, error);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
g_mirror_config(struct gctl_req *req, struct g_class *mp, const char *verb)
|
||||||
|
{
|
||||||
|
uint32_t *version;
|
||||||
|
|
||||||
|
g_topology_assert();
|
||||||
|
|
||||||
|
version = gctl_get_paraml(req, "version", sizeof(*version));
|
||||||
|
if (version == NULL) {
|
||||||
|
gctl_error(req, "No '%s' argument.", "version");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (*version != G_MIRROR_VERSION) {
|
||||||
|
gctl_error(req, "Userland and kernel parts are out of sync.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (strcmp(verb, "configure") == 0)
|
||||||
|
g_mirror_ctl_configure(req, mp);
|
||||||
|
else if (strcmp(verb, "rebuild") == 0)
|
||||||
|
g_mirror_ctl_rebuild(req, mp);
|
||||||
|
else if (strcmp(verb, "insert") == 0)
|
||||||
|
g_mirror_ctl_insert(req, mp);
|
||||||
|
else if (strcmp(verb, "remove") == 0)
|
||||||
|
g_mirror_ctl_remove(req, mp);
|
||||||
|
else if (strcmp(verb, "deactivate") == 0)
|
||||||
|
g_mirror_ctl_deactivate(req, mp);
|
||||||
|
else if (strcmp(verb, "forget") == 0)
|
||||||
|
g_mirror_ctl_forget(req, mp);
|
||||||
|
else if (strcmp(verb, "stop") == 0)
|
||||||
|
g_mirror_ctl_stop(req, mp);
|
||||||
|
else
|
||||||
|
gctl_error(req, "Unknown verb.");
|
||||||
|
}
|
9
sys/modules/geom/geom_mirror/Makefile
Normal file
9
sys/modules/geom/geom_mirror/Makefile
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
# $FreeBSD$
|
||||||
|
|
||||||
|
.PATH: ${.CURDIR}/../../../geom/mirror
|
||||||
|
|
||||||
|
KMOD= geom_mirror
|
||||||
|
SRCS= g_mirror.c
|
||||||
|
SRCS+= g_mirror_ctl.c
|
||||||
|
|
||||||
|
.include <bsd.kmod.mk>
|
8
tools/regression/geom_mirror/Makefile
Normal file
8
tools/regression/geom_mirror/Makefile
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
#
|
||||||
|
# $FreeBSD$
|
||||||
|
#
|
||||||
|
# Regression tests for geom_mirror.
|
||||||
|
#
|
||||||
|
|
||||||
|
test:
|
||||||
|
@sh runtests.sh
|
10
tools/regression/geom_mirror/runtests.sh
Normal file
10
tools/regression/geom_mirror/runtests.sh
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
# $FreeBSD$
|
||||||
|
|
||||||
|
dir=`dirname $0`
|
||||||
|
|
||||||
|
gmirror load >/dev/null 2>&1
|
||||||
|
for ts in `dirname $0`/test-*.sh; do
|
||||||
|
sh $ts
|
||||||
|
done
|
||||||
|
gmirror unload >/dev/null 2>&1
|
32
tools/regression/geom_mirror/test-1.sh
Normal file
32
tools/regression/geom_mirror/test-1.sh
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
# $FreeBSD$
|
||||||
|
|
||||||
|
name="test"
|
||||||
|
base=`basename $0`
|
||||||
|
us0=45
|
||||||
|
us1=`expr $us0 + 1`
|
||||||
|
us2=`expr $us0 + 2`
|
||||||
|
|
||||||
|
mdconfig -a -t malloc -s 1M -u $us0 || exit 1
|
||||||
|
mdconfig -a -t malloc -s 2M -u $us1 || exit 1
|
||||||
|
mdconfig -a -t malloc -s 3M -u $us2 || exit 1
|
||||||
|
sleep 1
|
||||||
|
|
||||||
|
gmirror label $name /dev/md${us0} /dev/md${us1} /dev/md${us2} || exit 1
|
||||||
|
|
||||||
|
# Size of created device should be 1MB - 512b.
|
||||||
|
|
||||||
|
size=`diskinfo /dev/mirror/${name} | awk '{print $3}'`
|
||||||
|
|
||||||
|
if [ $size -eq 1048064 ]; then
|
||||||
|
echo "PASS"
|
||||||
|
else
|
||||||
|
echo "FAIL"
|
||||||
|
fi
|
||||||
|
|
||||||
|
gmirror remove $name md${us0}
|
||||||
|
gmirror remove $name md${us1}
|
||||||
|
gmirror remove $name md${us2}
|
||||||
|
mdconfig -d -u $us0
|
||||||
|
mdconfig -d -u $us1
|
||||||
|
mdconfig -d -u $us2
|
57
tools/regression/geom_mirror/test-2.sh
Normal file
57
tools/regression/geom_mirror/test-2.sh
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
# $FreeBSD$
|
||||||
|
|
||||||
|
name="test"
|
||||||
|
base=`basename $0`
|
||||||
|
balance="round-robin"
|
||||||
|
us0=45
|
||||||
|
us1=`expr $us0 + 1`
|
||||||
|
us2=`expr $us0 + 2`
|
||||||
|
ddbs=2048
|
||||||
|
nblocks1=1024
|
||||||
|
nblocks2=`expr $nblocks1 / \( $ddbs / 512 \)`
|
||||||
|
src=`mktemp /tmp/$base.XXXXXX` || exit 1
|
||||||
|
dst=`mktemp /tmp/$base.XXXXXX` || exit 1
|
||||||
|
|
||||||
|
dd if=/dev/random of=${src} bs=$ddbs count=$nblocks2 >/dev/null 2>&1
|
||||||
|
|
||||||
|
mdconfig -a -t malloc -s `expr $nblocks1 + 1` -u $us0 || exit 1
|
||||||
|
mdconfig -a -t malloc -s `expr $nblocks1 + 1` -u $us1 || exit 1
|
||||||
|
mdconfig -a -t malloc -s `expr $nblocks1 + 1` -u $us2 || exit 1
|
||||||
|
sleep 1
|
||||||
|
|
||||||
|
gmirror label -b $balance $name /dev/md${us0} /dev/md${us1} /dev/md${us2} || exit 1
|
||||||
|
|
||||||
|
dd if=${src} of=/dev/mirror/${name} bs=$ddbs count=$nblocks2 >/dev/null 2>&1
|
||||||
|
|
||||||
|
dd if=/dev/mirror/${name} of=${dst} bs=$ddbs count=$nblocks2 >/dev/null 2>&1
|
||||||
|
if [ `md5 -q ${src}` != `md5 -q ${dst}` ]; then
|
||||||
|
echo "FAIL"
|
||||||
|
else
|
||||||
|
echo "PASS"
|
||||||
|
fi
|
||||||
|
dd if=/dev/md${us0} of=${dst} bs=$ddbs count=$nblocks2 >/dev/null 2>&1
|
||||||
|
if [ `md5 -q ${src}` != `md5 -q ${dst}` ]; then
|
||||||
|
echo "FAIL"
|
||||||
|
else
|
||||||
|
echo "PASS"
|
||||||
|
fi
|
||||||
|
dd if=/dev/md${us1} of=${dst} bs=$ddbs count=$nblocks2 >/dev/null 2>&1
|
||||||
|
if [ `md5 -q ${src}` != `md5 -q ${dst}` ]; then
|
||||||
|
echo "FAIL"
|
||||||
|
else
|
||||||
|
echo "PASS"
|
||||||
|
fi
|
||||||
|
|
||||||
|
dd if=/dev/md${us2} of=${dst} bs=$ddbs count=$nblocks2 >/dev/null 2>&1
|
||||||
|
if [ `md5 -q ${src}` != `md5 -q ${dst}` ]; then
|
||||||
|
echo "FAIL"
|
||||||
|
else
|
||||||
|
echo "PASS"
|
||||||
|
fi
|
||||||
|
|
||||||
|
gmirror remove $name md${us0} md${us1} md${us2}
|
||||||
|
mdconfig -d -u $us0
|
||||||
|
mdconfig -d -u $us1
|
||||||
|
mdconfig -d -u $us2
|
||||||
|
rm -f ${src} ${dst}
|
68
tools/regression/geom_mirror/test-3.sh
Normal file
68
tools/regression/geom_mirror/test-3.sh
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
# $FreeBSD$
|
||||||
|
|
||||||
|
name="test"
|
||||||
|
base=`basename $0`
|
||||||
|
balance="round-robin"
|
||||||
|
us0=45
|
||||||
|
us1=`expr $us0 + 1`
|
||||||
|
us2=`expr $us0 + 2`
|
||||||
|
ddbs=2048
|
||||||
|
nblocks1=1024
|
||||||
|
nblocks2=`expr $nblocks1 / \( $ddbs / 512 \)`
|
||||||
|
src=`mktemp /tmp/$base.XXXXXX` || exit 1
|
||||||
|
dst=`mktemp /tmp/$base.XXXXXX` || exit 1
|
||||||
|
|
||||||
|
dd if=/dev/random of=${src} bs=$ddbs count=$nblocks2 >/dev/null 2>&1
|
||||||
|
|
||||||
|
mdconfig -a -t malloc -s `expr $nblocks1 + 1` -u $us0 || exit 1
|
||||||
|
mdconfig -a -t malloc -s `expr $nblocks1 + 1` -u $us1 || exit 1
|
||||||
|
mdconfig -a -t malloc -s `expr $nblocks1 + 1` -u $us2 || exit 1
|
||||||
|
sleep 1
|
||||||
|
|
||||||
|
gmirror label -b $balance $name /dev/md${us0} /dev/md${us1} /dev/md${us2} || exit 1
|
||||||
|
|
||||||
|
dd if=${src} of=/dev/mirror/${name} bs=$ddbs count=$nblocks2 >/dev/null 2>&1
|
||||||
|
|
||||||
|
dd if=/dev/mirror/${name} of=${dst} bs=$ddbs count=$nblocks2 >/dev/null 2>&1
|
||||||
|
if [ `md5 -q ${src}` != `md5 -q ${dst}` ]; then
|
||||||
|
echo "FAIL"
|
||||||
|
else
|
||||||
|
echo "PASS"
|
||||||
|
fi
|
||||||
|
|
||||||
|
gmirror remove $name md${us0}
|
||||||
|
dd if=/dev/mirror/${name} of=${dst} bs=$ddbs count=$nblocks2 >/dev/null 2>&1
|
||||||
|
if [ `md5 -q ${src}` != `md5 -q ${dst}` ]; then
|
||||||
|
echo "FAIL"
|
||||||
|
else
|
||||||
|
echo "PASS"
|
||||||
|
fi
|
||||||
|
|
||||||
|
gmirror remove $name md${us1}
|
||||||
|
dd if=/dev/mirror/${name} of=${dst} bs=$ddbs count=$nblocks2 >/dev/null 2>&1
|
||||||
|
if [ `md5 -q ${src}` != `md5 -q ${dst}` ]; then
|
||||||
|
echo "FAIL"
|
||||||
|
else
|
||||||
|
echo "PASS"
|
||||||
|
fi
|
||||||
|
|
||||||
|
gmirror remove $name md${us2}
|
||||||
|
dd if=/dev/mirror/${name} of=${dst} bs=$ddbs count=$nblocks2 >/dev/null 2>&1
|
||||||
|
if [ `md5 -q ${src}` != `md5 -q ${dst}` ]; then
|
||||||
|
echo "FAIL"
|
||||||
|
else
|
||||||
|
echo "PASS"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# mirror/${name} should be removed.
|
||||||
|
if [ -c /dev/${name} ]; then
|
||||||
|
echo "FAIL"
|
||||||
|
else
|
||||||
|
echo "PASS"
|
||||||
|
fi
|
||||||
|
|
||||||
|
mdconfig -d -u $us0
|
||||||
|
mdconfig -d -u $us1
|
||||||
|
mdconfig -d -u $us2
|
||||||
|
rm -f ${src} ${dst}
|
68
tools/regression/geom_mirror/test-4.sh
Normal file
68
tools/regression/geom_mirror/test-4.sh
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
# $FreeBSD$
|
||||||
|
|
||||||
|
name="test"
|
||||||
|
base=`basename $0`
|
||||||
|
balance="load"
|
||||||
|
us0=45
|
||||||
|
us1=`expr $us0 + 1`
|
||||||
|
us2=`expr $us0 + 2`
|
||||||
|
ddbs=2048
|
||||||
|
nblocks1=1024
|
||||||
|
nblocks2=`expr $nblocks1 / \( $ddbs / 512 \)`
|
||||||
|
src=`mktemp /tmp/$base.XXXXXX` || exit 1
|
||||||
|
dst=`mktemp /tmp/$base.XXXXXX` || exit 1
|
||||||
|
|
||||||
|
dd if=/dev/random of=${src} bs=$ddbs count=$nblocks2 >/dev/null 2>&1
|
||||||
|
|
||||||
|
mdconfig -a -t malloc -s `expr $nblocks1 + 1` -u $us0 || exit 1
|
||||||
|
mdconfig -a -t malloc -s `expr $nblocks1 + 1` -u $us1 || exit 1
|
||||||
|
mdconfig -a -t malloc -s `expr $nblocks1 + 1` -u $us2 || exit 1
|
||||||
|
sleep 1
|
||||||
|
|
||||||
|
gmirror label -b $balance $name /dev/md${us0} /dev/md${us1} /dev/md${us2} || exit 1
|
||||||
|
|
||||||
|
dd if=${src} of=/dev/mirror/${name} bs=$ddbs count=$nblocks2 >/dev/null 2>&1
|
||||||
|
|
||||||
|
dd if=/dev/mirror/${name} of=${dst} bs=$ddbs count=$nblocks2 >/dev/null 2>&1
|
||||||
|
if [ `md5 -q ${src}` != `md5 -q ${dst}` ]; then
|
||||||
|
echo "FAIL"
|
||||||
|
else
|
||||||
|
echo "PASS"
|
||||||
|
fi
|
||||||
|
|
||||||
|
gmirror remove $name md${us0}
|
||||||
|
dd if=/dev/mirror/${name} of=${dst} bs=$ddbs count=$nblocks2 >/dev/null 2>&1
|
||||||
|
if [ `md5 -q ${src}` != `md5 -q ${dst}` ]; then
|
||||||
|
echo "FAIL"
|
||||||
|
else
|
||||||
|
echo "PASS"
|
||||||
|
fi
|
||||||
|
|
||||||
|
gmirror remove $name md${us1}
|
||||||
|
dd if=/dev/mirror/${name} of=${dst} bs=$ddbs count=$nblocks2 >/dev/null 2>&1
|
||||||
|
if [ `md5 -q ${src}` != `md5 -q ${dst}` ]; then
|
||||||
|
echo "FAIL"
|
||||||
|
else
|
||||||
|
echo "PASS"
|
||||||
|
fi
|
||||||
|
|
||||||
|
gmirror remove $name md${us2}
|
||||||
|
dd if=/dev/mirror/${name} of=${dst} bs=$ddbs count=$nblocks2 >/dev/null 2>&1
|
||||||
|
if [ `md5 -q ${src}` != `md5 -q ${dst}` ]; then
|
||||||
|
echo "FAIL"
|
||||||
|
else
|
||||||
|
echo "PASS"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# mirror/${name} should be removed.
|
||||||
|
if [ -c /dev/${name} ]; then
|
||||||
|
echo "FAIL"
|
||||||
|
else
|
||||||
|
echo "PASS"
|
||||||
|
fi
|
||||||
|
|
||||||
|
mdconfig -d -u $us0
|
||||||
|
mdconfig -d -u $us1
|
||||||
|
mdconfig -d -u $us2
|
||||||
|
rm -f ${src} ${dst}
|
68
tools/regression/geom_mirror/test-5.sh
Normal file
68
tools/regression/geom_mirror/test-5.sh
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
# $FreeBSD$
|
||||||
|
|
||||||
|
name="test"
|
||||||
|
base=`basename $0`
|
||||||
|
balance="split"
|
||||||
|
us0=45
|
||||||
|
us1=`expr $us0 + 1`
|
||||||
|
us2=`expr $us0 + 2`
|
||||||
|
ddbs=8192
|
||||||
|
nblocks1=1024
|
||||||
|
nblocks2=`expr $nblocks1 / \( $ddbs / 512 \)`
|
||||||
|
src=`mktemp /tmp/$base.XXXXXX` || exit 1
|
||||||
|
dst=`mktemp /tmp/$base.XXXXXX` || exit 1
|
||||||
|
|
||||||
|
dd if=/dev/random of=${src} bs=$ddbs count=$nblocks2 >/dev/null 2>&1
|
||||||
|
|
||||||
|
mdconfig -a -t malloc -s `expr $nblocks1 + 1` -u $us0 || exit 1
|
||||||
|
mdconfig -a -t malloc -s `expr $nblocks1 + 1` -u $us1 || exit 1
|
||||||
|
mdconfig -a -t malloc -s `expr $nblocks1 + 1` -u $us2 || exit 1
|
||||||
|
sleep 1
|
||||||
|
|
||||||
|
gmirror label -b $balance -s `expr $ddbs / 2` $name /dev/md${us0} /dev/md${us1} /dev/md${us2} || exit 1
|
||||||
|
|
||||||
|
dd if=${src} of=/dev/mirror/${name} bs=$ddbs count=$nblocks2 >/dev/null 2>&1
|
||||||
|
|
||||||
|
dd if=/dev/mirror/${name} of=${dst} bs=$ddbs count=$nblocks2 >/dev/null 2>&1
|
||||||
|
if [ `md5 -q ${src}` != `md5 -q ${dst}` ]; then
|
||||||
|
echo "FAIL"
|
||||||
|
else
|
||||||
|
echo "PASS"
|
||||||
|
fi
|
||||||
|
|
||||||
|
gmirror remove $name md${us0}
|
||||||
|
dd if=/dev/mirror/${name} of=${dst} bs=$ddbs count=$nblocks2 >/dev/null 2>&1
|
||||||
|
if [ `md5 -q ${src}` != `md5 -q ${dst}` ]; then
|
||||||
|
echo "FAIL"
|
||||||
|
else
|
||||||
|
echo "PASS"
|
||||||
|
fi
|
||||||
|
|
||||||
|
gmirror remove $name md${us1}
|
||||||
|
dd if=/dev/mirror/${name} of=${dst} bs=$ddbs count=$nblocks2 >/dev/null 2>&1
|
||||||
|
if [ `md5 -q ${src}` != `md5 -q ${dst}` ]; then
|
||||||
|
echo "FAIL"
|
||||||
|
else
|
||||||
|
echo "PASS"
|
||||||
|
fi
|
||||||
|
|
||||||
|
gmirror remove $name md${us2}
|
||||||
|
dd if=/dev/mirror/${name} of=${dst} bs=$ddbs count=$nblocks2 >/dev/null 2>&1
|
||||||
|
if [ `md5 -q ${src}` != `md5 -q ${dst}` ]; then
|
||||||
|
echo "FAIL"
|
||||||
|
else
|
||||||
|
echo "PASS"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# mirror/${name} should be removed.
|
||||||
|
if [ -c /dev/${name} ]; then
|
||||||
|
echo "FAIL"
|
||||||
|
else
|
||||||
|
echo "PASS"
|
||||||
|
fi
|
||||||
|
|
||||||
|
mdconfig -d -u $us0
|
||||||
|
mdconfig -d -u $us1
|
||||||
|
mdconfig -d -u $us2
|
||||||
|
rm -f ${src} ${dst}
|
50
tools/regression/geom_mirror/test-6.sh
Normal file
50
tools/regression/geom_mirror/test-6.sh
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
# $FreeBSD$
|
||||||
|
|
||||||
|
name="test"
|
||||||
|
base=`basename $0`
|
||||||
|
balance="split"
|
||||||
|
us0=45
|
||||||
|
us1=`expr $us0 + 1`
|
||||||
|
us2=`expr $us0 + 2`
|
||||||
|
ddbs=8192
|
||||||
|
nblocks1=1024
|
||||||
|
nblocks2=`expr $nblocks1 / \( $ddbs / 512 \)`
|
||||||
|
src=`mktemp /tmp/$base.XXXXXX` || exit 1
|
||||||
|
dst=`mktemp /tmp/$base.XXXXXX` || exit 1
|
||||||
|
|
||||||
|
dd if=/dev/random of=${src} bs=$ddbs count=$nblocks2 >/dev/null 2>&1
|
||||||
|
|
||||||
|
mdconfig -a -t malloc -s `expr $nblocks1 + 1` -u $us0 || exit 1
|
||||||
|
mdconfig -a -t malloc -s `expr $nblocks1 + 1` -u $us1 || exit 1
|
||||||
|
mdconfig -a -t malloc -s `expr $nblocks1 + 1` -u $us2 || exit 1
|
||||||
|
sleep 1
|
||||||
|
|
||||||
|
gmirror label -b $balance -s `expr $ddbs / 2` $name /dev/md${us0} /dev/md${us1} || exit 1
|
||||||
|
|
||||||
|
dd if=${src} of=/dev/mirror/${name} bs=$ddbs count=$nblocks2 >/dev/null 2>&1
|
||||||
|
dd if=/dev/zero of=/dev/md${us2} bs=$ddbs count=$nblocks2 >/dev/null 2>&1
|
||||||
|
|
||||||
|
dd if=/dev/mirror/${name} of=${dst} bs=$ddbs count=$nblocks2 >/dev/null 2>&1
|
||||||
|
if [ `md5 -q ${src}` != `md5 -q ${dst}` ]; then
|
||||||
|
echo "FAIL"
|
||||||
|
else
|
||||||
|
echo "PASS"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Connect disk to the mirror.
|
||||||
|
gmirror insert ${name} md${us2}
|
||||||
|
# Wait for synchronization.
|
||||||
|
sleep 1
|
||||||
|
dd if=/dev/md${us2} of=${dst} bs=$ddbs count=$nblocks2 >/dev/null 2>&1
|
||||||
|
if [ `md5 -q ${src}` != `md5 -q ${dst}` ]; then
|
||||||
|
echo "FAIL"
|
||||||
|
else
|
||||||
|
echo "PASS"
|
||||||
|
fi
|
||||||
|
|
||||||
|
gmirror remove $name md${us0} md${us1} md${us2}
|
||||||
|
mdconfig -d -u $us0
|
||||||
|
mdconfig -d -u $us1
|
||||||
|
mdconfig -d -u $us2
|
||||||
|
rm -f ${src} ${dst}
|
Loading…
Reference in New Issue
Block a user