]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sbin/geom/class/multipath/geom_multipath.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.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 <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
52 struct g_command class_commands[] = {
53         {
54                 "label", G_FLAG_VERBOSE | G_FLAG_LOADKLD, mp_main, G_NULL_OPTS,
55                 NULL, "[-v] name prov ..."
56         },
57         {
58                 "clear", G_FLAG_VERBOSE, mp_main, G_NULL_OPTS,
59                 NULL, "[-v] prov ..."
60         },
61         G_CMD_SENTINEL
62 };
63
64 static void
65 mp_main(struct gctl_req *req, unsigned int flags __unused)
66 {
67         const char *name;
68
69         name = gctl_get_ascii(req, "verb");
70         if (name == NULL) {
71                 gctl_error(req, "No '%s' argument.", "verb");
72                 return;
73         }
74         if (strcmp(name, "label") == 0) {
75                 mp_label(req);
76         } else if (strcmp(name, "clear") == 0) {
77                 mp_clear(req);
78         } else {
79                 gctl_error(req, "Unknown command: %s.", name);
80         }
81 }
82
83 static void
84 mp_label(struct gctl_req *req)
85 {
86         struct g_multipath_metadata md;
87         off_t disksiz = 0, msize;
88         uint8_t *sector;
89         char *ptr;
90         uuid_t uuid;
91         uint32_t secsize = 0, ssize, status;
92         const char *name;
93         int error, i, nargs;
94
95         nargs = gctl_get_int(req, "nargs");
96         if (nargs < 2) {
97                 gctl_error(req, "wrong number of arguments.");
98                 return;
99         }
100
101         /*
102          * First, check each provider to make sure it's the same size.
103          * This also gets us our size and sectorsize for the metadata.
104          */
105         for (i = 1; i < nargs; i++) {
106                 name = gctl_get_ascii(req, "arg%d", i);
107                 msize = g_get_mediasize(name);
108                 ssize = g_get_sectorsize(name);
109                 if (msize == 0 || ssize == 0) {
110                         gctl_error(req, "cannot get information about %s: %s.",
111                             name, strerror(errno));
112                         return;
113                 }
114                 if (i == 1) {
115                         secsize = ssize;
116                         disksiz = msize;
117                 } else {
118                         if (secsize != ssize) {
119                                 gctl_error(req, "%s sector size %u different.",
120                                     name, ssize);
121                                 return;
122                         }
123                         if (disksiz != msize) {
124                                 gctl_error(req, "%s media size %ju different.",
125                                     name, (intmax_t)msize);
126                                 return;
127                         }
128                 }
129                 
130         }
131
132         /*
133          * Allocate a sector to write as metadata.
134          */
135         sector = malloc(secsize);
136         if (sector == NULL) {
137                 gctl_error(req, "unable to allocate metadata buffer");
138                 return;
139         }
140         memset(sector, 0, secsize);
141
142         /*
143          * Generate metadata.
144          */
145         strlcpy(md.md_magic, G_MULTIPATH_MAGIC, sizeof(md.md_magic));
146         md.md_version = G_MULTIPATH_VERSION;
147         name = gctl_get_ascii(req, "arg0");
148         strlcpy(md.md_name, name, sizeof(md.md_name));
149         md.md_size = disksiz;
150         md.md_sectorsize = secsize;
151         uuid_create(&uuid, &status);
152         if (status != uuid_s_ok) {
153                 gctl_error(req, "cannot create a UUID.");
154                 return;
155         }
156         uuid_to_string(&uuid, &ptr, &status);
157         if (status != uuid_s_ok) {
158                 gctl_error(req, "cannot stringify a UUID.");
159                 return;
160         }
161         strlcpy(md.md_uuid, ptr, sizeof (md.md_uuid));
162         free(ptr);
163
164         /*
165          * Clear last sector first for each provider to spoil anything extant
166          */
167         for (i = 1; i < nargs; i++) {
168                 name = gctl_get_ascii(req, "arg%d", i);
169                 error = g_metadata_clear(name, NULL);
170                 if (error != 0) {
171                         gctl_error(req, "cannot clear metadata on %s: %s.",
172                             name, strerror(error));
173                         return;
174                 }
175         }
176
177         multipath_metadata_encode(&md, sector);
178
179         /*
180          * Ok, store metadata.
181          */
182         for (i = 1; i < nargs; i++) {
183                 name = gctl_get_ascii(req, "arg%d", i);
184                 error = g_metadata_store(name, sector, secsize);
185                 if (error != 0) {
186                         fprintf(stderr, "Can't store metadata on %s: %s.\n",
187                             name, strerror(error));
188                         goto fail;
189                 }
190         }
191         return;
192
193 fail:
194         /*
195          * Clear last sector first for each provider to spoil anything extant
196          */
197         for (i = 1; i < nargs; i++) {
198                 name = gctl_get_ascii(req, "arg%d", i);
199                 error = g_metadata_clear(name, NULL);
200                 if (error != 0) {
201                         gctl_error(req, "cannot clear metadata on %s: %s.",
202                             name, strerror(error));
203                         continue;
204                 }
205         }
206 }
207
208 static void
209 mp_clear(struct gctl_req *req)
210 {
211         const char *name;
212         int error, i, nargs;
213
214         nargs = gctl_get_int(req, "nargs");
215         if (nargs < 1) {
216                 gctl_error(req, "Too few arguments.");
217                 return;
218         }
219
220         for (i = 0; i < nargs; i++) {
221                 name = gctl_get_ascii(req, "arg%d", i);
222                 error = g_metadata_clear(name, G_MULTIPATH_MAGIC);
223                 if (error != 0) {
224                         fprintf(stderr, "Can't clear metadata on %s: %s.\n",
225                             name, strerror(error));
226                         gctl_error(req, "Not fully done.");
227                         continue;
228                 }
229         }
230 }