]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sbin/geom/class/multipath/geom_multipath.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.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 <uuid.h>
40 #include <geom/multipath/g_multipath.h>
41
42 #include "core/geom.h"
43 #include "misc/subr.h"
44
45 uint32_t lib_version = G_LIB_VERSION;
46 uint32_t version = G_MULTIPATH_VERSION;
47
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 *);
52
53 struct g_command class_commands[] = {
54         {
55                 "label", G_FLAG_VERBOSE | G_FLAG_LOADKLD, mp_main, G_NULL_OPTS,
56                 NULL, "[-v] name prov ..."
57         },
58         {
59                 "add", G_FLAG_VERBOSE | G_FLAG_LOADKLD, mp_main, G_NULL_OPTS,
60                 NULL, "[-v] name prov ..."
61         },
62         {
63                 "destroy", G_FLAG_VERBOSE, NULL, G_NULL_OPTS,
64                 NULL, "[-v] prov ..."
65         },
66         {
67                 "clear", G_FLAG_VERBOSE, mp_main, G_NULL_OPTS,
68                 NULL, "[-v] prov ..."
69         },
70         {
71                 "rotate", G_FLAG_VERBOSE, NULL, G_NULL_OPTS,
72                 NULL, "[-v] prov ..."
73         },
74         {
75                 "getactive", G_FLAG_VERBOSE, NULL, G_NULL_OPTS,
76                 NULL, "[-v] prov ..."
77         },
78         G_CMD_SENTINEL
79 };
80
81 static void
82 mp_main(struct gctl_req *req, unsigned int flags __unused)
83 {
84         const char *name;
85
86         name = gctl_get_ascii(req, "verb");
87         if (name == NULL) {
88                 gctl_error(req, "No '%s' argument.", "verb");
89                 return;
90         }
91         if (strcmp(name, "label") == 0) {
92                 mp_label(req);
93         } else if (strcmp(name, "add") == 0) {
94                 mp_add(req);
95         } else if (strcmp(name, "clear") == 0) {
96                 mp_clear(req);
97         } else {
98                 gctl_error(req, "Unknown command: %s.", name);
99         }
100 }
101
102 static void
103 mp_label(struct gctl_req *req)
104 {
105         struct g_multipath_metadata md;
106         off_t disksiz = 0, msize;
107         uint8_t *sector;
108         char *ptr;
109         uuid_t uuid;
110         uint32_t secsize = 0, ssize, status;
111         const char *name, *mpname;
112         int error, i, nargs;
113
114         nargs = gctl_get_int(req, "nargs");
115         if (nargs < 2) {
116                 gctl_error(req, "wrong number of arguments.");
117                 return;
118         }
119
120         /*
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.
123          */
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));
131                         return;
132                 }
133                 if (i == 1) {
134                         secsize = ssize;
135                         disksiz = msize;
136                 } else {
137                         if (secsize != ssize) {
138                                 gctl_error(req, "%s sector size %u different.",
139                                     name, ssize);
140                                 return;
141                         }
142                         if (disksiz != msize) {
143                                 gctl_error(req, "%s media size %ju different.",
144                                     name, (intmax_t)msize);
145                                 return;
146                         }
147                 }
148                 
149         }
150
151         /*
152          * Allocate a sector to write as metadata.
153          */
154         sector = malloc(secsize);
155         if (sector == NULL) {
156                 gctl_error(req, "unable to allocate metadata buffer");
157                 return;
158         }
159         memset(sector, 0, secsize);
160
161         /*
162          * Generate metadata.
163          */
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.");
173                 return;
174         }
175         uuid_to_string(&uuid, &ptr, &status);
176         if (status != uuid_s_ok) {
177                 gctl_error(req, "cannot stringify a UUID.");
178                 return;
179         }
180         strlcpy(md.md_uuid, ptr, sizeof (md.md_uuid));
181         free(ptr);
182
183         /*
184          * Clear metadata on initial provider first.
185          */
186         name = gctl_get_ascii(req, "arg1");
187         error = g_metadata_clear(name, NULL);
188         if (error != 0) {
189                 gctl_error(req, "cannot clear metadata on %s: %s.", name, strerror(error));
190                 return;
191         }
192
193         /*
194          * encode the metadata
195          */
196         multipath_metadata_encode(&md, sector);
197
198         /*
199          * Store metadata on the initial provider.
200          */
201         error = g_metadata_store(name, sector, secsize);
202         if (error != 0) {
203                 gctl_error(req, "cannot store metadata on %s: %s.", name, strerror(error));
204                 return;
205         }
206
207         /*
208          * Now add the rest of the providers.
209          */
210         error = gctl_change_param(req, "verb", -1, "add");
211         if (error) {
212                 gctl_error(req, "unable to change verb to \"add\": %s.", strerror(error));
213                 return;
214         }
215         for (i = 2; i < nargs; i++) {
216                 error = gctl_change_param(req, "arg1", -1, gctl_get_ascii(req, "arg%d", i));
217                 if (error) {
218                         gctl_error(req, "unable to add %s to %s: %s.", gctl_get_ascii(req, "arg%d", i), mpname, strerror(error));
219                         continue;
220                 }
221                 mp_add(req);
222         }
223 }
224
225 static void
226 mp_clear(struct gctl_req *req)
227 {
228         const char *name;
229         int error;
230
231         name = gctl_get_ascii(req, "arg1");
232         error = g_metadata_clear(name, G_MULTIPATH_MAGIC);
233         if (error != 0) {
234                 fprintf(stderr, "Can't clear metadata on %s: %s.\n", name, strerror(error));
235                 gctl_error(req, "Not fully done.");
236         }
237 }
238
239 static void
240 mp_add(struct gctl_req *req)
241 {
242         const char *errstr;
243
244         errstr = gctl_issue(req);
245         if (errstr != NULL && errstr[0] != '\0') {
246                 gctl_error(req, "%s", errstr);
247         }
248 }