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 *);
51 static void mp_add(struct gctl_req *);
53 struct g_command class_commands[] = {
55 "label", G_FLAG_VERBOSE | G_FLAG_LOADKLD, mp_main, G_NULL_OPTS,
56 NULL, "[-v] name prov ..."
59 "add", G_FLAG_VERBOSE | G_FLAG_LOADKLD, mp_main, G_NULL_OPTS,
60 NULL, "[-v] name prov ..."
63 "destroy", G_FLAG_VERBOSE, NULL, G_NULL_OPTS,
67 "clear", G_FLAG_VERBOSE, mp_main, G_NULL_OPTS,
71 "rotate", G_FLAG_VERBOSE, NULL, G_NULL_OPTS,
75 "getactive", G_FLAG_VERBOSE, NULL, G_NULL_OPTS,
82 mp_main(struct gctl_req *req, unsigned int flags __unused)
86 name = gctl_get_ascii(req, "verb");
88 gctl_error(req, "No '%s' argument.", "verb");
91 if (strcmp(name, "label") == 0) {
93 } else if (strcmp(name, "add") == 0) {
95 } else if (strcmp(name, "clear") == 0) {
98 gctl_error(req, "Unknown command: %s.", name);
103 mp_label(struct gctl_req *req)
105 struct g_multipath_metadata md;
106 off_t disksiz = 0, msize;
110 uint32_t secsize = 0, ssize, status;
111 const char *name, *mpname;
114 nargs = gctl_get_int(req, "nargs");
116 gctl_error(req, "wrong number of arguments.");
121 * First, check each provider to make sure it's the same size.
122 * This also gets us our size and sectorsize for the metadata.
124 for (i = 1; i < nargs; i++) {
125 name = gctl_get_ascii(req, "arg%d", i);
126 msize = g_get_mediasize(name);
127 ssize = g_get_sectorsize(name);
128 if (msize == 0 || ssize == 0) {
129 gctl_error(req, "cannot get information about %s: %s.",
130 name, strerror(errno));
137 if (secsize != ssize) {
138 gctl_error(req, "%s sector size %u different.",
142 if (disksiz != msize) {
143 gctl_error(req, "%s media size %ju different.",
144 name, (intmax_t)msize);
152 * Allocate a sector to write as metadata.
154 sector = malloc(secsize);
155 if (sector == NULL) {
156 gctl_error(req, "unable to allocate metadata buffer");
159 memset(sector, 0, secsize);
164 strlcpy(md.md_magic, G_MULTIPATH_MAGIC, sizeof(md.md_magic));
165 md.md_version = G_MULTIPATH_VERSION;
166 mpname = gctl_get_ascii(req, "arg0");
167 strlcpy(md.md_name, mpname, sizeof(md.md_name));
168 md.md_size = disksiz;
169 md.md_sectorsize = secsize;
170 uuid_create(&uuid, &status);
171 if (status != uuid_s_ok) {
172 gctl_error(req, "cannot create a UUID.");
175 uuid_to_string(&uuid, &ptr, &status);
176 if (status != uuid_s_ok) {
177 gctl_error(req, "cannot stringify a UUID.");
180 strlcpy(md.md_uuid, ptr, sizeof (md.md_uuid));
184 * Clear metadata on initial provider first.
186 name = gctl_get_ascii(req, "arg1");
187 error = g_metadata_clear(name, NULL);
189 gctl_error(req, "cannot clear metadata on %s: %s.", name, strerror(error));
194 * encode the metadata
196 multipath_metadata_encode(&md, sector);
199 * Store metadata on the initial provider.
201 error = g_metadata_store(name, sector, secsize);
203 gctl_error(req, "cannot store metadata on %s: %s.", name, strerror(error));
208 * Now add the rest of the providers.
210 error = gctl_change_param(req, "verb", -1, "add");
212 gctl_error(req, "unable to change verb to \"add\": %s.", strerror(error));
215 for (i = 2; i < nargs; i++) {
216 error = gctl_change_param(req, "arg1", -1, gctl_get_ascii(req, "arg%d", i));
218 gctl_error(req, "unable to add %s to %s: %s.", gctl_get_ascii(req, "arg%d", i), mpname, strerror(error));
226 mp_clear(struct gctl_req *req)
231 name = gctl_get_ascii(req, "arg1");
232 error = g_metadata_clear(name, G_MULTIPATH_MAGIC);
234 fprintf(stderr, "Can't clear metadata on %s: %s.\n", name, strerror(error));
235 gctl_error(req, "Not fully done.");
240 mp_add(struct gctl_req *req)
244 errstr = gctl_issue(req);
245 if (errstr != NULL && errstr[0] != '\0') {
246 gctl_error(req, "%s", errstr);