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