xref: /freebsd/lib/geom/multipath/geom_multipath.c (revision 4e8d558c)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2006 Mathew Jacob <mjacob@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  *
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
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <errno.h>
33 #include <paths.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <stdint.h>
37 #include <string.h>
38 #include <strings.h>
39 #include <assert.h>
40 #include <libgeom.h>
41 #include <unistd.h>
42 #include <uuid.h>
43 #include <geom/multipath/g_multipath.h>
44 
45 #include "core/geom.h"
46 #include "misc/subr.h"
47 
48 uint32_t lib_version = G_LIB_VERSION;
49 uint32_t version = G_MULTIPATH_VERSION;
50 
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 *);
55 
56 struct g_command class_commands[] = {
57 	{
58 		"create", G_FLAG_VERBOSE | G_FLAG_LOADKLD, NULL,
59 		{
60 			{ 'A', "active_active", NULL, G_TYPE_BOOL },
61 			{ 'R', "active_read", NULL, G_TYPE_BOOL },
62 			G_OPT_SENTINEL
63 		},
64 		"[-vAR] name prov ..."
65 	},
66 	{
67 		"label", G_FLAG_VERBOSE | G_FLAG_LOADKLD, mp_main,
68 		{
69 			{ 'A', "active_active", NULL, G_TYPE_BOOL },
70 			{ 'R', "active_read", NULL, G_TYPE_BOOL },
71 			G_OPT_SENTINEL
72 		},
73 		"[-vAR] name prov ..."
74 	},
75 	{ "configure", G_FLAG_VERBOSE, NULL,
76 		{
77 			{ 'A', "active_active", NULL, G_TYPE_BOOL },
78 			{ 'P', "active_passive", NULL, G_TYPE_BOOL },
79 			{ 'R', "active_read", NULL, G_TYPE_BOOL },
80 			G_OPT_SENTINEL
81 		},
82 		"[-vAPR] name"
83 	},
84 	{
85 		"add", G_FLAG_VERBOSE, NULL, G_NULL_OPTS,
86 		"[-v] name prov"
87 	},
88 	{
89 		"remove", G_FLAG_VERBOSE, NULL, G_NULL_OPTS,
90 		"[-v] name prov"
91 	},
92 	{
93 		"prefer", G_FLAG_VERBOSE, mp_main, G_NULL_OPTS,
94 		"[-v] prov ..."
95 	},
96 	{
97 		"fail", G_FLAG_VERBOSE, NULL, G_NULL_OPTS,
98 		"[-v] name prov"
99 	},
100 	{
101 		"restore", G_FLAG_VERBOSE, NULL, G_NULL_OPTS,
102 		"[-v] name prov"
103 	},
104 	{
105 		"rotate", G_FLAG_VERBOSE, NULL, G_NULL_OPTS,
106 		"[-v] name"
107 	},
108 	{
109 		"getactive", G_FLAG_VERBOSE, NULL, G_NULL_OPTS,
110 		"[-v] name"
111 	},
112 	{
113 		"destroy", G_FLAG_VERBOSE, NULL, G_NULL_OPTS,
114 		"[-v] name"
115 	},
116 	{
117 		"stop", G_FLAG_VERBOSE, NULL, G_NULL_OPTS,
118 		"[-v] name"
119 	},
120 	{
121 		"clear", G_FLAG_VERBOSE, mp_main, G_NULL_OPTS,
122 		"[-v] prov ..."
123 	},
124 	G_CMD_SENTINEL
125 };
126 
127 static void
128 mp_main(struct gctl_req *req, unsigned int flags __unused)
129 {
130 	const char *name;
131 
132 	name = gctl_get_ascii(req, "verb");
133 	if (name == NULL) {
134 		gctl_error(req, "No '%s' argument.", "verb");
135 		return;
136 	}
137 	if (strcmp(name, "label") == 0) {
138 		mp_label(req);
139 	} else if (strcmp(name, "clear") == 0) {
140 		mp_clear(req);
141 	} else if (strcmp(name, "prefer") == 0) {
142 		mp_prefer(req);
143 	} else {
144 		gctl_error(req, "Unknown command: %s.", name);
145 	}
146 }
147 
148 static void
149 mp_label(struct gctl_req *req)
150 {
151 	struct g_multipath_metadata md;
152 	off_t disksize = 0, msize;
153 	uint8_t *sector, *rsector;
154 	char *ptr;
155 	uuid_t uuid;
156 	ssize_t secsize = 0, ssize;
157 	uint32_t status;
158 	const char *name, *name2, *mpname;
159 	int error, i, nargs, fd;
160 
161 	nargs = gctl_get_int(req, "nargs");
162 	if (nargs < 2) {
163 		gctl_error(req, "wrong number of arguments.");
164 		return;
165 	}
166 
167 	/*
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.
170 	 */
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));
178 			return;
179 		}
180 		if (i == 1) {
181 			secsize = ssize;
182 			disksize = msize;
183 		} else {
184 			if (secsize != ssize) {
185 				gctl_error(req, "%s sector size %ju different.",
186 				    name, (intmax_t)ssize);
187 				return;
188 			}
189 			if (disksize != msize) {
190 				gctl_error(req, "%s media size %ju different.",
191 				    name, (intmax_t)msize);
192 				return;
193 			}
194 		}
195 
196 	}
197 
198 	/*
199 	 * Generate metadata.
200 	 */
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.");
210 		return;
211 	}
212 	uuid_to_string(&uuid, &ptr, &status);
213 	if (status != uuid_s_ok) {
214 		gctl_error(req, "cannot stringify a UUID.");
215 		return;
216 	}
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;
221 	free(ptr);
222 
223 	/*
224 	 * Allocate a sector to write as metadata.
225 	 */
226 	sector = calloc(1, secsize);
227 	if (sector == NULL) {
228 		gctl_error(req, "unable to allocate metadata buffer");
229 		return;
230 	}
231 	rsector = malloc(secsize);
232 	if (rsector == NULL) {
233 		gctl_error(req, "unable to allocate metadata buffer");
234 		goto done;
235 	}
236 
237 	/*
238 	 * encode the metadata
239 	 */
240 	multipath_metadata_encode(&md, sector);
241 
242 	/*
243 	 * Store metadata on the initial provider.
244 	 */
245 	name = gctl_get_ascii(req, "arg1");
246 	error = g_metadata_store(name, sector, secsize);
247 	if (error != 0) {
248 		gctl_error(req, "cannot store metadata on %s: %s.", name, strerror(error));
249 		goto done;
250 	}
251 
252 	/*
253 	 * Now touch the rest of the providers to hint retaste.
254 	 */
255 	for (i = 2; i < nargs; i++) {
256 		name2 = gctl_get_ascii(req, "arg%d", i);
257 		fd = g_open(name2, 1);
258 		if (fd < 0) {
259 			fprintf(stderr, "Unable to open %s: %s.\n",
260 			    name2, strerror(errno));
261 			continue;
262 		}
263 		if (pread(fd, rsector, secsize, disksize - secsize) !=
264 		    (ssize_t)secsize) {
265 			fprintf(stderr, "Unable to read metadata from %s: %s.\n",
266 			    name2, strerror(errno));
267 			g_close(fd);
268 			continue;
269 		}
270 		g_close(fd);
271 		if (memcmp(sector, rsector, secsize)) {
272 			fprintf(stderr, "No metadata found on %s."
273 			    " It is not a path of %s.\n",
274 			    name2, name);
275 		}
276 	}
277 done:
278 	free(rsector);
279 	free(sector);
280 }
281 
282 
283 static void
284 mp_clear(struct gctl_req *req)
285 {
286 	const char *name;
287 	int error, i, nargs;
288 
289 	nargs = gctl_get_int(req, "nargs");
290 	if (nargs < 1) {
291 		gctl_error(req, "Too few arguments.");
292 		return;
293 	}
294 
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);
298 		if (error != 0) {
299 			fprintf(stderr, "Can't clear metadata on %s: %s.\n",
300 			    name, strerror(error));
301 			gctl_error(req, "Not fully done.");
302 			continue;
303 		}
304 	}
305 }
306 
307 static void
308 mp_prefer(struct gctl_req *req)
309 {
310 	const char *name, *comp, *errstr;
311 	int nargs;
312 
313 	nargs = gctl_get_int(req, "nargs");
314 	if (nargs != 2) {
315 		gctl_error(req, "Usage: prefer GEOM PROVIDER");
316 		return;
317 	}
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",
323 		    name, comp, errstr);
324 	}
325 }
326