2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2006 Mathew Jacob <mjacob@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
43 #include <geom/multipath/g_multipath.h>
45 #include "core/geom.h"
46 #include "misc/subr.h"
48 uint32_t lib_version = G_LIB_VERSION;
49 uint32_t version = G_MULTIPATH_VERSION;
51 static void mp_main(struct gctl_req *, unsigned int);
52 static void mp_label(struct gctl_req *);
53 static void mp_clear(struct gctl_req *);
54 static void mp_prefer(struct gctl_req *);
56 struct g_command class_commands[] = {
58 "create", G_FLAG_VERBOSE | G_FLAG_LOADKLD, NULL,
60 { 'A', "active_active", NULL, G_TYPE_BOOL },
61 { 'R', "active_read", NULL, G_TYPE_BOOL },
64 "[-vAR] name prov ..."
67 "label", G_FLAG_VERBOSE | G_FLAG_LOADKLD, mp_main,
69 { 'A', "active_active", NULL, G_TYPE_BOOL },
70 { 'R', "active_read", NULL, G_TYPE_BOOL },
73 "[-vAR] name prov ..."
75 { "configure", G_FLAG_VERBOSE, NULL,
77 { 'A', "active_active", NULL, G_TYPE_BOOL },
78 { 'P', "active_passive", NULL, G_TYPE_BOOL },
79 { 'R', "active_read", NULL, G_TYPE_BOOL },
85 "add", G_FLAG_VERBOSE, NULL, G_NULL_OPTS,
89 "remove", G_FLAG_VERBOSE, NULL, G_NULL_OPTS,
93 "prefer", G_FLAG_VERBOSE, mp_main, G_NULL_OPTS,
97 "fail", G_FLAG_VERBOSE, NULL, G_NULL_OPTS,
101 "restore", G_FLAG_VERBOSE, NULL, G_NULL_OPTS,
105 "rotate", G_FLAG_VERBOSE, NULL, G_NULL_OPTS,
109 "getactive", G_FLAG_VERBOSE, NULL, G_NULL_OPTS,
113 "destroy", G_FLAG_VERBOSE, NULL, G_NULL_OPTS,
117 "stop", G_FLAG_VERBOSE, NULL, G_NULL_OPTS,
121 "clear", G_FLAG_VERBOSE, mp_main, G_NULL_OPTS,
128 mp_main(struct gctl_req *req, unsigned int flags __unused)
132 name = gctl_get_ascii(req, "verb");
134 gctl_error(req, "No '%s' argument.", "verb");
137 if (strcmp(name, "label") == 0) {
139 } else if (strcmp(name, "clear") == 0) {
141 } else if (strcmp(name, "prefer") == 0) {
144 gctl_error(req, "Unknown command: %s.", name);
149 mp_label(struct gctl_req *req)
151 struct g_multipath_metadata md;
152 off_t disksize = 0, msize;
153 uint8_t *sector, *rsector;
156 ssize_t secsize = 0, ssize;
158 const char *name, *name2, *mpname;
159 int error, i, nargs, fd;
161 nargs = gctl_get_int(req, "nargs");
163 gctl_error(req, "wrong number of arguments.");
168 * First, check each provider to make sure it's the same size.
169 * This also gets us our size and sectorsize for the metadata.
171 for (i = 1; i < nargs; i++) {
172 name = gctl_get_ascii(req, "arg%d", i);
173 msize = g_get_mediasize(name);
174 ssize = g_get_sectorsize(name);
175 if (msize == 0 || ssize == 0) {
176 gctl_error(req, "cannot get information about %s: %s.",
177 name, strerror(errno));
184 if (secsize != ssize) {
185 gctl_error(req, "%s sector size %ju different.",
186 name, (intmax_t)ssize);
189 if (disksize != msize) {
190 gctl_error(req, "%s media size %ju different.",
191 name, (intmax_t)msize);
201 strlcpy(md.md_magic, G_MULTIPATH_MAGIC, sizeof(md.md_magic));
202 md.md_version = G_MULTIPATH_VERSION;
203 mpname = gctl_get_ascii(req, "arg0");
204 strlcpy(md.md_name, mpname, sizeof(md.md_name));
205 md.md_size = disksize;
206 md.md_sectorsize = secsize;
207 uuid_create(&uuid, &status);
208 if (status != uuid_s_ok) {
209 gctl_error(req, "cannot create a UUID.");
212 uuid_to_string(&uuid, &ptr, &status);
213 if (status != uuid_s_ok) {
214 gctl_error(req, "cannot stringify a UUID.");
217 strlcpy(md.md_uuid, ptr, sizeof (md.md_uuid));
218 md.md_active_active = gctl_get_int(req, "active_active");
219 if (gctl_get_int(req, "active_read"))
220 md.md_active_active = 2;
224 * Allocate a sector to write as metadata.
226 sector = calloc(1, secsize);
227 if (sector == NULL) {
228 gctl_error(req, "unable to allocate metadata buffer");
231 rsector = malloc(secsize);
232 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",
284 mp_clear(struct gctl_req *req)
289 nargs = gctl_get_int(req, "nargs");
291 gctl_error(req, "Too few arguments.");
295 for (i = 0; i < nargs; i++) {
296 name = gctl_get_ascii(req, "arg%d", i);
297 error = g_metadata_clear(name, G_MULTIPATH_MAGIC);
299 fprintf(stderr, "Can't clear metadata on %s: %s.\n",
300 name, strerror(error));
301 gctl_error(req, "Not fully done.");
308 mp_prefer(struct gctl_req *req)
310 const char *name, *comp, *errstr;
313 nargs = gctl_get_int(req, "nargs");
315 gctl_error(req, "Usage: prefer GEOM PROVIDER");
318 name = gctl_get_ascii(req, "arg0");
319 comp = gctl_get_ascii(req, "arg1");
320 errstr = gctl_issue (req);
321 if (errstr != NULL) {
322 fprintf(stderr, "Can't set %s preferred provider to %s: %s.\n",