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>
41 #include <geom/multipath/g_multipath.h>
43 #include "core/geom.h"
44 #include "misc/subr.h"
46 uint32_t lib_version = G_LIB_VERSION;
47 uint32_t version = G_MULTIPATH_VERSION;
49 static void mp_main(struct gctl_req *, unsigned int);
50 static void mp_label(struct gctl_req *);
51 static void mp_clear(struct gctl_req *);
52 static void mp_prefer(struct gctl_req *);
54 struct g_command class_commands[] = {
56 "create", G_FLAG_VERBOSE | G_FLAG_LOADKLD, NULL,
58 { 'A', "active_active", NULL, G_TYPE_BOOL },
59 { 'R', "active_read", NULL, G_TYPE_BOOL },
62 "[-vAR] name prov ..."
65 "label", G_FLAG_VERBOSE | G_FLAG_LOADKLD, mp_main,
67 { 'A', "active_active", NULL, G_TYPE_BOOL },
68 { 'R', "active_read", NULL, G_TYPE_BOOL },
71 "[-vAR] name prov ..."
73 { "configure", G_FLAG_VERBOSE, NULL,
75 { 'A', "active_active", NULL, G_TYPE_BOOL },
76 { 'P', "active_passive", NULL, G_TYPE_BOOL },
77 { 'R', "active_read", NULL, G_TYPE_BOOL },
83 "add", G_FLAG_VERBOSE, NULL, G_NULL_OPTS,
87 "remove", G_FLAG_VERBOSE, NULL, G_NULL_OPTS,
91 "prefer", G_FLAG_VERBOSE, mp_main, G_NULL_OPTS,
95 "fail", G_FLAG_VERBOSE, NULL, G_NULL_OPTS,
99 "restore", G_FLAG_VERBOSE, NULL, G_NULL_OPTS,
103 "rotate", G_FLAG_VERBOSE, NULL, G_NULL_OPTS,
107 "getactive", G_FLAG_VERBOSE, NULL, G_NULL_OPTS,
111 "destroy", G_FLAG_VERBOSE, NULL, G_NULL_OPTS,
115 "stop", G_FLAG_VERBOSE, NULL, G_NULL_OPTS,
119 "clear", G_FLAG_VERBOSE, mp_main, G_NULL_OPTS,
126 mp_main(struct gctl_req *req, unsigned int flags __unused)
130 name = gctl_get_ascii(req, "verb");
132 gctl_error(req, "No '%s' argument.", "verb");
135 if (strcmp(name, "label") == 0) {
137 } else if (strcmp(name, "clear") == 0) {
139 } else if (strcmp(name, "prefer") == 0) {
142 gctl_error(req, "Unknown command: %s.", name);
147 mp_label(struct gctl_req *req)
149 struct g_multipath_metadata md;
150 off_t disksize = 0, msize;
151 uint8_t *sector, *rsector;
154 ssize_t secsize = 0, ssize;
156 const char *name, *name2, *mpname;
157 int error, i, nargs, fd;
159 nargs = gctl_get_int(req, "nargs");
161 gctl_error(req, "wrong number of arguments.");
166 * First, check each provider to make sure it's the same size.
167 * This also gets us our size and sectorsize for the metadata.
169 for (i = 1; i < nargs; i++) {
170 name = gctl_get_ascii(req, "arg%d", i);
171 msize = g_get_mediasize(name);
172 ssize = g_get_sectorsize(name);
173 if (msize == 0 || ssize == 0) {
174 gctl_error(req, "cannot get information about %s: %s.",
175 name, strerror(errno));
182 if (secsize != ssize) {
183 gctl_error(req, "%s sector size %ju different.",
184 name, (intmax_t)ssize);
187 if (disksize != msize) {
188 gctl_error(req, "%s media size %ju different.",
189 name, (intmax_t)msize);
199 strlcpy(md.md_magic, G_MULTIPATH_MAGIC, sizeof(md.md_magic));
200 md.md_version = G_MULTIPATH_VERSION;
201 mpname = gctl_get_ascii(req, "arg0");
202 strlcpy(md.md_name, mpname, sizeof(md.md_name));
203 md.md_size = disksize;
204 md.md_sectorsize = secsize;
205 uuid_create(&uuid, &status);
206 if (status != uuid_s_ok) {
207 gctl_error(req, "cannot create a UUID.");
210 uuid_to_string(&uuid, &ptr, &status);
211 if (status != uuid_s_ok) {
212 gctl_error(req, "cannot stringify a UUID.");
215 strlcpy(md.md_uuid, ptr, sizeof (md.md_uuid));
216 md.md_active_active = gctl_get_int(req, "active_active");
217 if (gctl_get_int(req, "active_read"))
218 md.md_active_active = 2;
222 * Allocate a sector to write as metadata.
224 sector = malloc(secsize);
225 if (sector == NULL) {
226 gctl_error(req, "unable to allocate metadata buffer");
229 memset(sector, 0, secsize);
230 rsector = malloc(secsize);
231 if (rsector == NULL) {
233 gctl_error(req, "unable to allocate metadata buffer");
238 * encode the metadata
240 multipath_metadata_encode(&md, sector);
243 * Store metadata on the initial provider.
245 name = gctl_get_ascii(req, "arg1");
246 error = g_metadata_store(name, sector, secsize);
248 gctl_error(req, "cannot store metadata on %s: %s.", name, strerror(error));
253 * Now touch the rest of the providers to hint retaste.
255 for (i = 2; i < nargs; i++) {
256 name2 = gctl_get_ascii(req, "arg%d", i);
257 fd = g_open(name2, 1);
259 fprintf(stderr, "Unable to open %s: %s.\n",
260 name2, strerror(errno));
263 if (pread(fd, rsector, secsize, disksize - secsize) !=
265 fprintf(stderr, "Unable to read metadata from %s: %s.\n",
266 name2, strerror(errno));
271 if (memcmp(sector, rsector, secsize)) {
272 fprintf(stderr, "No metadata found on %s."
273 " It is not a path of %s.\n",
281 mp_clear(struct gctl_req *req)
286 nargs = gctl_get_int(req, "nargs");
288 gctl_error(req, "Too few arguments.");
292 for (i = 0; i < nargs; i++) {
293 name = gctl_get_ascii(req, "arg%d", i);
294 error = g_metadata_clear(name, G_MULTIPATH_MAGIC);
296 fprintf(stderr, "Can't clear metadata on %s: %s.\n",
297 name, strerror(error));
298 gctl_error(req, "Not fully done.");
305 mp_prefer(struct gctl_req *req)
307 const char *name, *comp, *errstr;
310 nargs = gctl_get_int(req, "nargs");
312 gctl_error(req, "Usage: prefer GEOM PROVIDER");
315 name = gctl_get_ascii(req, "arg0");
316 comp = gctl_get_ascii(req, "arg1");
317 errstr = gctl_issue (req);
318 if (errstr != NULL) {
319 fprintf(stderr, "Can't set %s preferred provider to %s: %s.\n",