2 * Copyright (c) 2006 Mathew Jacob <mjacob@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 #include <sys/param.h>
40 #include <geom/multipath/g_multipath.h>
42 #include "core/geom.h"
43 #include "misc/subr.h"
45 uint32_t lib_version = G_LIB_VERSION;
46 uint32_t version = G_MULTIPATH_VERSION;
48 static void mp_main(struct gctl_req *, unsigned int);
49 static void mp_label(struct gctl_req *);
50 static void mp_clear(struct gctl_req *);
52 struct g_command class_commands[] = {
54 "label", G_FLAG_VERBOSE | G_FLAG_LOADKLD, mp_main, G_NULL_OPTS,
55 NULL, "[-v] name prov ..."
58 "clear", G_FLAG_VERBOSE, mp_main, G_NULL_OPTS,
65 mp_main(struct gctl_req *req, unsigned int flags __unused)
69 name = gctl_get_ascii(req, "verb");
71 gctl_error(req, "No '%s' argument.", "verb");
74 if (strcmp(name, "label") == 0) {
76 } else if (strcmp(name, "clear") == 0) {
79 gctl_error(req, "Unknown command: %s.", name);
84 mp_label(struct gctl_req *req)
86 struct g_multipath_metadata md;
87 off_t disksiz = 0, msize;
91 uint32_t secsize = 0, ssize, status;
95 nargs = gctl_get_int(req, "nargs");
97 gctl_error(req, "wrong number of arguments.");
102 * First, check each provider to make sure it's the same size.
103 * This also gets us our size and sectorsize for the metadata.
105 for (i = 1; i < nargs; i++) {
106 name = gctl_get_ascii(req, "arg%d", i);
107 msize = g_get_mediasize(name);
108 ssize = g_get_sectorsize(name);
109 if (msize == 0 || ssize == 0) {
110 gctl_error(req, "cannot get information about %s: %s.",
111 name, strerror(errno));
118 if (secsize != ssize) {
119 gctl_error(req, "%s sector size %u different.",
123 if (disksiz != msize) {
124 gctl_error(req, "%s media size %ju different.",
125 name, (intmax_t)msize);
133 * Allocate a sector to write as metadata.
135 sector = malloc(secsize);
136 if (sector == NULL) {
137 gctl_error(req, "unable to allocate metadata buffer");
140 memset(sector, 0, secsize);
145 strlcpy(md.md_magic, G_MULTIPATH_MAGIC, sizeof(md.md_magic));
146 md.md_version = G_MULTIPATH_VERSION;
147 name = gctl_get_ascii(req, "arg0");
148 strlcpy(md.md_name, name, sizeof(md.md_name));
149 md.md_size = disksiz;
150 md.md_sectorsize = secsize;
151 uuid_create(&uuid, &status);
152 if (status != uuid_s_ok) {
153 gctl_error(req, "cannot create a UUID.");
156 uuid_to_string(&uuid, &ptr, &status);
157 if (status != uuid_s_ok) {
158 gctl_error(req, "cannot stringify a UUID.");
161 strlcpy(md.md_uuid, ptr, sizeof (md.md_uuid));
165 * Clear last sector first for each provider to spoil anything extant
167 for (i = 1; i < nargs; i++) {
168 name = gctl_get_ascii(req, "arg%d", i);
169 error = g_metadata_clear(name, NULL);
171 gctl_error(req, "cannot clear metadata on %s: %s.",
172 name, strerror(error));
177 multipath_metadata_encode(&md, sector);
180 * Ok, store metadata.
182 for (i = 1; i < nargs; i++) {
183 name = gctl_get_ascii(req, "arg%d", i);
184 error = g_metadata_store(name, sector, secsize);
186 fprintf(stderr, "Can't store metadata on %s: %s.\n",
187 name, strerror(error));
195 * Clear last sector first for each provider to spoil anything extant
197 for (i = 1; i < nargs; i++) {
198 name = gctl_get_ascii(req, "arg%d", i);
199 error = g_metadata_clear(name, NULL);
201 gctl_error(req, "cannot clear metadata on %s: %s.",
202 name, strerror(error));
209 mp_clear(struct gctl_req *req)
214 nargs = gctl_get_int(req, "nargs");
216 gctl_error(req, "Too few arguments.");
220 for (i = 0; i < nargs; i++) {
221 name = gctl_get_ascii(req, "arg%d", i);
222 error = g_metadata_clear(name, G_MULTIPATH_MAGIC);
224 fprintf(stderr, "Can't clear metadata on %s: %s.\n",
225 name, strerror(error));
226 gctl_error(req, "Not fully done.");