1e4b0a90eSBrooks Davis /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3e4b0a90eSBrooks Davis *
4e4b0a90eSBrooks Davis * Copyright (c) 2006 Mathew Jacob <mjacob@FreeBSD.org>
5e4b0a90eSBrooks Davis * All rights reserved.
6e4b0a90eSBrooks Davis *
7e4b0a90eSBrooks Davis * Redistribution and use in source and binary forms, with or without
8e4b0a90eSBrooks Davis * modification, are permitted provided that the following conditions
9e4b0a90eSBrooks Davis * are met:
10e4b0a90eSBrooks Davis * 1. Redistributions of source code must retain the above copyright
11e4b0a90eSBrooks Davis * notice, this list of conditions and the following disclaimer.
12e4b0a90eSBrooks Davis * 2. Redistributions in binary form must reproduce the above copyright
13e4b0a90eSBrooks Davis * notice, this list of conditions and the following disclaimer in the
14e4b0a90eSBrooks Davis * documentation and/or other materials provided with the distribution.
15e4b0a90eSBrooks Davis *
16e4b0a90eSBrooks Davis * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
17e4b0a90eSBrooks Davis * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18e4b0a90eSBrooks Davis * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19e4b0a90eSBrooks Davis * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
20e4b0a90eSBrooks Davis * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21e4b0a90eSBrooks Davis * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22e4b0a90eSBrooks Davis * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23e4b0a90eSBrooks Davis * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24e4b0a90eSBrooks Davis * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25e4b0a90eSBrooks Davis * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26e4b0a90eSBrooks Davis * SUCH DAMAGE.
27e4b0a90eSBrooks Davis */
28e4b0a90eSBrooks Davis
29e4b0a90eSBrooks Davis #include <sys/param.h>
30e4b0a90eSBrooks Davis #include <errno.h>
31e4b0a90eSBrooks Davis #include <paths.h>
32e4b0a90eSBrooks Davis #include <stdio.h>
33e4b0a90eSBrooks Davis #include <stdlib.h>
34e4b0a90eSBrooks Davis #include <stdint.h>
35e4b0a90eSBrooks Davis #include <string.h>
36e4b0a90eSBrooks Davis #include <strings.h>
37e4b0a90eSBrooks Davis #include <assert.h>
38e4b0a90eSBrooks Davis #include <libgeom.h>
39e4b0a90eSBrooks Davis #include <unistd.h>
40e4b0a90eSBrooks Davis #include <uuid.h>
41e4b0a90eSBrooks Davis #include <geom/multipath/g_multipath.h>
42e4b0a90eSBrooks Davis
43e4b0a90eSBrooks Davis #include "core/geom.h"
44e4b0a90eSBrooks Davis #include "misc/subr.h"
45e4b0a90eSBrooks Davis
46e4b0a90eSBrooks Davis uint32_t lib_version = G_LIB_VERSION;
47e4b0a90eSBrooks Davis uint32_t version = G_MULTIPATH_VERSION;
48e4b0a90eSBrooks Davis
49e4b0a90eSBrooks Davis static void mp_main(struct gctl_req *, unsigned int);
50e4b0a90eSBrooks Davis static void mp_label(struct gctl_req *);
51e4b0a90eSBrooks Davis static void mp_clear(struct gctl_req *);
52e4b0a90eSBrooks Davis static void mp_prefer(struct gctl_req *);
53e4b0a90eSBrooks Davis
54e4b0a90eSBrooks Davis struct g_command class_commands[] = {
55e4b0a90eSBrooks Davis {
56e4b0a90eSBrooks Davis "create", G_FLAG_VERBOSE | G_FLAG_LOADKLD, NULL,
57e4b0a90eSBrooks Davis {
58e4b0a90eSBrooks Davis { 'A', "active_active", NULL, G_TYPE_BOOL },
59e4b0a90eSBrooks Davis { 'R', "active_read", NULL, G_TYPE_BOOL },
60e4b0a90eSBrooks Davis G_OPT_SENTINEL
61e4b0a90eSBrooks Davis },
62e4b0a90eSBrooks Davis "[-vAR] name prov ..."
63e4b0a90eSBrooks Davis },
64e4b0a90eSBrooks Davis {
65e4b0a90eSBrooks Davis "label", G_FLAG_VERBOSE | G_FLAG_LOADKLD, mp_main,
66e4b0a90eSBrooks Davis {
67e4b0a90eSBrooks Davis { 'A', "active_active", NULL, G_TYPE_BOOL },
68e4b0a90eSBrooks Davis { 'R', "active_read", NULL, G_TYPE_BOOL },
69e4b0a90eSBrooks Davis G_OPT_SENTINEL
70e4b0a90eSBrooks Davis },
71e4b0a90eSBrooks Davis "[-vAR] name prov ..."
72e4b0a90eSBrooks Davis },
73e4b0a90eSBrooks Davis { "configure", G_FLAG_VERBOSE, NULL,
74e4b0a90eSBrooks Davis {
75e4b0a90eSBrooks Davis { 'A', "active_active", NULL, G_TYPE_BOOL },
76e4b0a90eSBrooks Davis { 'P', "active_passive", NULL, G_TYPE_BOOL },
77e4b0a90eSBrooks Davis { 'R', "active_read", NULL, G_TYPE_BOOL },
78e4b0a90eSBrooks Davis G_OPT_SENTINEL
79e4b0a90eSBrooks Davis },
80e4b0a90eSBrooks Davis "[-vAPR] name"
81e4b0a90eSBrooks Davis },
82e4b0a90eSBrooks Davis {
83e4b0a90eSBrooks Davis "add", G_FLAG_VERBOSE, NULL, G_NULL_OPTS,
84e4b0a90eSBrooks Davis "[-v] name prov"
85e4b0a90eSBrooks Davis },
86e4b0a90eSBrooks Davis {
87e4b0a90eSBrooks Davis "remove", G_FLAG_VERBOSE, NULL, G_NULL_OPTS,
88e4b0a90eSBrooks Davis "[-v] name prov"
89e4b0a90eSBrooks Davis },
90e4b0a90eSBrooks Davis {
91e4b0a90eSBrooks Davis "prefer", G_FLAG_VERBOSE, mp_main, G_NULL_OPTS,
92e4b0a90eSBrooks Davis "[-v] prov ..."
93e4b0a90eSBrooks Davis },
94e4b0a90eSBrooks Davis {
95e4b0a90eSBrooks Davis "fail", G_FLAG_VERBOSE, NULL, G_NULL_OPTS,
96e4b0a90eSBrooks Davis "[-v] name prov"
97e4b0a90eSBrooks Davis },
98e4b0a90eSBrooks Davis {
99e4b0a90eSBrooks Davis "restore", G_FLAG_VERBOSE, NULL, G_NULL_OPTS,
100e4b0a90eSBrooks Davis "[-v] name prov"
101e4b0a90eSBrooks Davis },
102e4b0a90eSBrooks Davis {
103e4b0a90eSBrooks Davis "rotate", G_FLAG_VERBOSE, NULL, G_NULL_OPTS,
104e4b0a90eSBrooks Davis "[-v] name"
105e4b0a90eSBrooks Davis },
106e4b0a90eSBrooks Davis {
107e4b0a90eSBrooks Davis "getactive", G_FLAG_VERBOSE, NULL, G_NULL_OPTS,
108e4b0a90eSBrooks Davis "[-v] name"
109e4b0a90eSBrooks Davis },
110e4b0a90eSBrooks Davis {
111e4b0a90eSBrooks Davis "destroy", G_FLAG_VERBOSE, NULL, G_NULL_OPTS,
112e4b0a90eSBrooks Davis "[-v] name"
113e4b0a90eSBrooks Davis },
114e4b0a90eSBrooks Davis {
115e4b0a90eSBrooks Davis "stop", G_FLAG_VERBOSE, NULL, G_NULL_OPTS,
116e4b0a90eSBrooks Davis "[-v] name"
117e4b0a90eSBrooks Davis },
118e4b0a90eSBrooks Davis {
119e4b0a90eSBrooks Davis "clear", G_FLAG_VERBOSE, mp_main, G_NULL_OPTS,
120e4b0a90eSBrooks Davis "[-v] prov ..."
121e4b0a90eSBrooks Davis },
122e4b0a90eSBrooks Davis G_CMD_SENTINEL
123e4b0a90eSBrooks Davis };
124e4b0a90eSBrooks Davis
125e4b0a90eSBrooks Davis static void
mp_main(struct gctl_req * req,unsigned int flags __unused)126e4b0a90eSBrooks Davis mp_main(struct gctl_req *req, unsigned int flags __unused)
127e4b0a90eSBrooks Davis {
128e4b0a90eSBrooks Davis const char *name;
129e4b0a90eSBrooks Davis
130e4b0a90eSBrooks Davis name = gctl_get_ascii(req, "verb");
131e4b0a90eSBrooks Davis if (name == NULL) {
132e4b0a90eSBrooks Davis gctl_error(req, "No '%s' argument.", "verb");
133e4b0a90eSBrooks Davis return;
134e4b0a90eSBrooks Davis }
135e4b0a90eSBrooks Davis if (strcmp(name, "label") == 0) {
136e4b0a90eSBrooks Davis mp_label(req);
137e4b0a90eSBrooks Davis } else if (strcmp(name, "clear") == 0) {
138e4b0a90eSBrooks Davis mp_clear(req);
139e4b0a90eSBrooks Davis } else if (strcmp(name, "prefer") == 0) {
140e4b0a90eSBrooks Davis mp_prefer(req);
141e4b0a90eSBrooks Davis } else {
142e4b0a90eSBrooks Davis gctl_error(req, "Unknown command: %s.", name);
143e4b0a90eSBrooks Davis }
144e4b0a90eSBrooks Davis }
145e4b0a90eSBrooks Davis
146e4b0a90eSBrooks Davis static void
mp_label(struct gctl_req * req)147e4b0a90eSBrooks Davis mp_label(struct gctl_req *req)
148e4b0a90eSBrooks Davis {
149e4b0a90eSBrooks Davis struct g_multipath_metadata md;
150e4b0a90eSBrooks Davis off_t disksize = 0, msize;
151e4b0a90eSBrooks Davis uint8_t *sector, *rsector;
152e4b0a90eSBrooks Davis char *ptr;
153e4b0a90eSBrooks Davis uuid_t uuid;
154e4b0a90eSBrooks Davis ssize_t secsize = 0, ssize;
155e4b0a90eSBrooks Davis uint32_t status;
156e4b0a90eSBrooks Davis const char *name, *name2, *mpname;
157e4b0a90eSBrooks Davis int error, i, nargs, fd;
158e4b0a90eSBrooks Davis
159e4b0a90eSBrooks Davis nargs = gctl_get_int(req, "nargs");
160e4b0a90eSBrooks Davis if (nargs < 2) {
161e4b0a90eSBrooks Davis gctl_error(req, "wrong number of arguments.");
162e4b0a90eSBrooks Davis return;
163e4b0a90eSBrooks Davis }
164e4b0a90eSBrooks Davis
165e4b0a90eSBrooks Davis /*
166e4b0a90eSBrooks Davis * First, check each provider to make sure it's the same size.
167e4b0a90eSBrooks Davis * This also gets us our size and sectorsize for the metadata.
168e4b0a90eSBrooks Davis */
169e4b0a90eSBrooks Davis for (i = 1; i < nargs; i++) {
170e4b0a90eSBrooks Davis name = gctl_get_ascii(req, "arg%d", i);
171e4b0a90eSBrooks Davis msize = g_get_mediasize(name);
172e4b0a90eSBrooks Davis ssize = g_get_sectorsize(name);
173e4b0a90eSBrooks Davis if (msize == 0 || ssize == 0) {
174e4b0a90eSBrooks Davis gctl_error(req, "cannot get information about %s: %s.",
175e4b0a90eSBrooks Davis name, strerror(errno));
176e4b0a90eSBrooks Davis return;
177e4b0a90eSBrooks Davis }
178e4b0a90eSBrooks Davis if (i == 1) {
179e4b0a90eSBrooks Davis secsize = ssize;
180e4b0a90eSBrooks Davis disksize = msize;
181e4b0a90eSBrooks Davis } else {
182e4b0a90eSBrooks Davis if (secsize != ssize) {
183e4b0a90eSBrooks Davis gctl_error(req, "%s sector size %ju different.",
184e4b0a90eSBrooks Davis name, (intmax_t)ssize);
185e4b0a90eSBrooks Davis return;
186e4b0a90eSBrooks Davis }
187e4b0a90eSBrooks Davis if (disksize != msize) {
188e4b0a90eSBrooks Davis gctl_error(req, "%s media size %ju different.",
189e4b0a90eSBrooks Davis name, (intmax_t)msize);
190e4b0a90eSBrooks Davis return;
191e4b0a90eSBrooks Davis }
192e4b0a90eSBrooks Davis }
193e4b0a90eSBrooks Davis
194e4b0a90eSBrooks Davis }
195e4b0a90eSBrooks Davis
196e4b0a90eSBrooks Davis /*
197e4b0a90eSBrooks Davis * Generate metadata.
198e4b0a90eSBrooks Davis */
199e4b0a90eSBrooks Davis strlcpy(md.md_magic, G_MULTIPATH_MAGIC, sizeof(md.md_magic));
200e4b0a90eSBrooks Davis md.md_version = G_MULTIPATH_VERSION;
201e4b0a90eSBrooks Davis mpname = gctl_get_ascii(req, "arg0");
202e4b0a90eSBrooks Davis strlcpy(md.md_name, mpname, sizeof(md.md_name));
203e4b0a90eSBrooks Davis md.md_size = disksize;
204e4b0a90eSBrooks Davis md.md_sectorsize = secsize;
205e4b0a90eSBrooks Davis uuid_create(&uuid, &status);
206e4b0a90eSBrooks Davis if (status != uuid_s_ok) {
207e4b0a90eSBrooks Davis gctl_error(req, "cannot create a UUID.");
208e4b0a90eSBrooks Davis return;
209e4b0a90eSBrooks Davis }
210e4b0a90eSBrooks Davis uuid_to_string(&uuid, &ptr, &status);
211e4b0a90eSBrooks Davis if (status != uuid_s_ok) {
212e4b0a90eSBrooks Davis gctl_error(req, "cannot stringify a UUID.");
213e4b0a90eSBrooks Davis return;
214e4b0a90eSBrooks Davis }
215e4b0a90eSBrooks Davis strlcpy(md.md_uuid, ptr, sizeof (md.md_uuid));
216e4b0a90eSBrooks Davis md.md_active_active = gctl_get_int(req, "active_active");
217e4b0a90eSBrooks Davis if (gctl_get_int(req, "active_read"))
218e4b0a90eSBrooks Davis md.md_active_active = 2;
219e4b0a90eSBrooks Davis free(ptr);
220e4b0a90eSBrooks Davis
221e4b0a90eSBrooks Davis /*
222e4b0a90eSBrooks Davis * Allocate a sector to write as metadata.
223e4b0a90eSBrooks Davis */
224e4b0a90eSBrooks Davis sector = calloc(1, secsize);
225e4b0a90eSBrooks Davis if (sector == NULL) {
226e4b0a90eSBrooks Davis gctl_error(req, "unable to allocate metadata buffer");
227e4b0a90eSBrooks Davis return;
228e4b0a90eSBrooks Davis }
229e4b0a90eSBrooks Davis rsector = malloc(secsize);
230e4b0a90eSBrooks Davis if (rsector == NULL) {
231e4b0a90eSBrooks Davis gctl_error(req, "unable to allocate metadata buffer");
232e4b0a90eSBrooks Davis goto done;
233e4b0a90eSBrooks Davis }
234e4b0a90eSBrooks Davis
235e4b0a90eSBrooks Davis /*
236e4b0a90eSBrooks Davis * encode the metadata
237e4b0a90eSBrooks Davis */
238e4b0a90eSBrooks Davis multipath_metadata_encode(&md, sector);
239e4b0a90eSBrooks Davis
240e4b0a90eSBrooks Davis /*
241e4b0a90eSBrooks Davis * Store metadata on the initial provider.
242e4b0a90eSBrooks Davis */
243e4b0a90eSBrooks Davis name = gctl_get_ascii(req, "arg1");
244e4b0a90eSBrooks Davis error = g_metadata_store(name, sector, secsize);
245e4b0a90eSBrooks Davis if (error != 0) {
246e4b0a90eSBrooks Davis gctl_error(req, "cannot store metadata on %s: %s.", name, strerror(error));
247e4b0a90eSBrooks Davis goto done;
248e4b0a90eSBrooks Davis }
249e4b0a90eSBrooks Davis
250e4b0a90eSBrooks Davis /*
251e4b0a90eSBrooks Davis * Now touch the rest of the providers to hint retaste.
252e4b0a90eSBrooks Davis */
253e4b0a90eSBrooks Davis for (i = 2; i < nargs; i++) {
254e4b0a90eSBrooks Davis name2 = gctl_get_ascii(req, "arg%d", i);
255e4b0a90eSBrooks Davis fd = g_open(name2, 1);
256e4b0a90eSBrooks Davis if (fd < 0) {
257e4b0a90eSBrooks Davis fprintf(stderr, "Unable to open %s: %s.\n",
258e4b0a90eSBrooks Davis name2, strerror(errno));
259e4b0a90eSBrooks Davis continue;
260e4b0a90eSBrooks Davis }
261e4b0a90eSBrooks Davis if (pread(fd, rsector, secsize, disksize - secsize) !=
262e4b0a90eSBrooks Davis (ssize_t)secsize) {
263e4b0a90eSBrooks Davis fprintf(stderr, "Unable to read metadata from %s: %s.\n",
264e4b0a90eSBrooks Davis name2, strerror(errno));
265e4b0a90eSBrooks Davis g_close(fd);
266e4b0a90eSBrooks Davis continue;
267e4b0a90eSBrooks Davis }
268e4b0a90eSBrooks Davis g_close(fd);
269e4b0a90eSBrooks Davis if (memcmp(sector, rsector, secsize)) {
270e4b0a90eSBrooks Davis fprintf(stderr, "No metadata found on %s."
271e4b0a90eSBrooks Davis " It is not a path of %s.\n",
272e4b0a90eSBrooks Davis name2, name);
273e4b0a90eSBrooks Davis }
274e4b0a90eSBrooks Davis }
275e4b0a90eSBrooks Davis done:
276e4b0a90eSBrooks Davis free(rsector);
277e4b0a90eSBrooks Davis free(sector);
278e4b0a90eSBrooks Davis }
279e4b0a90eSBrooks Davis
280e4b0a90eSBrooks Davis
281e4b0a90eSBrooks Davis static void
mp_clear(struct gctl_req * req)282e4b0a90eSBrooks Davis mp_clear(struct gctl_req *req)
283e4b0a90eSBrooks Davis {
284e4b0a90eSBrooks Davis const char *name;
285e4b0a90eSBrooks Davis int error, i, nargs;
286e4b0a90eSBrooks Davis
287e4b0a90eSBrooks Davis nargs = gctl_get_int(req, "nargs");
288e4b0a90eSBrooks Davis if (nargs < 1) {
289e4b0a90eSBrooks Davis gctl_error(req, "Too few arguments.");
290e4b0a90eSBrooks Davis return;
291e4b0a90eSBrooks Davis }
292e4b0a90eSBrooks Davis
293e4b0a90eSBrooks Davis for (i = 0; i < nargs; i++) {
294e4b0a90eSBrooks Davis name = gctl_get_ascii(req, "arg%d", i);
295e4b0a90eSBrooks Davis error = g_metadata_clear(name, G_MULTIPATH_MAGIC);
296e4b0a90eSBrooks Davis if (error != 0) {
297e4b0a90eSBrooks Davis fprintf(stderr, "Can't clear metadata on %s: %s.\n",
298e4b0a90eSBrooks Davis name, strerror(error));
299e4b0a90eSBrooks Davis gctl_error(req, "Not fully done.");
300e4b0a90eSBrooks Davis continue;
301e4b0a90eSBrooks Davis }
302e4b0a90eSBrooks Davis }
303e4b0a90eSBrooks Davis }
304e4b0a90eSBrooks Davis
305e4b0a90eSBrooks Davis static void
mp_prefer(struct gctl_req * req)306e4b0a90eSBrooks Davis mp_prefer(struct gctl_req *req)
307e4b0a90eSBrooks Davis {
308e4b0a90eSBrooks Davis const char *name, *comp, *errstr;
309e4b0a90eSBrooks Davis int nargs;
310e4b0a90eSBrooks Davis
311e4b0a90eSBrooks Davis nargs = gctl_get_int(req, "nargs");
312e4b0a90eSBrooks Davis if (nargs != 2) {
313e4b0a90eSBrooks Davis gctl_error(req, "Usage: prefer GEOM PROVIDER");
314e4b0a90eSBrooks Davis return;
315e4b0a90eSBrooks Davis }
316e4b0a90eSBrooks Davis name = gctl_get_ascii(req, "arg0");
317e4b0a90eSBrooks Davis comp = gctl_get_ascii(req, "arg1");
318e4b0a90eSBrooks Davis errstr = gctl_issue (req);
319e4b0a90eSBrooks Davis if (errstr != NULL) {
320e4b0a90eSBrooks Davis fprintf(stderr, "Can't set %s preferred provider to %s: %s.\n",
321e4b0a90eSBrooks Davis name, comp, errstr);
322e4b0a90eSBrooks Davis }
323e4b0a90eSBrooks Davis }
324