]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sbin/geom/class/raid3/geom_raid3.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / sbin / geom / class / raid3 / geom_raid3.c
1 /*-
2  * Copyright (c) 2004-2005 Pawel Jakub Dawidek <pjd@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
30 #include <sys/param.h>
31 #include <errno.h>
32 #include <paths.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <stdint.h>
36 #include <string.h>
37 #include <strings.h>
38 #include <assert.h>
39 #include <libgeom.h>
40 #include <geom/raid3/g_raid3.h>
41 #include <core/geom.h>
42 #include <misc/subr.h>
43
44
45 uint32_t lib_version = G_LIB_VERSION;
46 uint32_t version = G_RAID3_VERSION;
47
48 static void raid3_main(struct gctl_req *req, unsigned f);
49 static void raid3_clear(struct gctl_req *req);
50 static void raid3_dump(struct gctl_req *req);
51 static void raid3_label(struct gctl_req *req);
52
53 struct g_command class_commands[] = {
54         { "clear", G_FLAG_VERBOSE, raid3_main, G_NULL_OPTS, NULL,
55             "[-v] prov ..."
56         },
57         { "configure", G_FLAG_VERBOSE, NULL,
58             {
59                 { 'a', "autosync", NULL, G_TYPE_BOOL },
60                 { 'd', "dynamic", NULL, G_TYPE_BOOL },
61                 { 'f', "failsync", NULL, G_TYPE_BOOL },
62                 { 'F', "nofailsync", NULL, G_TYPE_BOOL },
63                 { 'h', "hardcode", NULL, G_TYPE_BOOL },
64                 { 'n', "noautosync", NULL, G_TYPE_BOOL },
65                 { 'r', "round_robin", NULL, G_TYPE_BOOL },
66                 { 'R', "noround_robin", NULL, G_TYPE_BOOL },
67                 { 'w', "verify", NULL, G_TYPE_BOOL },
68                 { 'W', "noverify", NULL, G_TYPE_BOOL },
69                 G_OPT_SENTINEL
70             },
71             NULL, "[-adfFhnrRvwW] name"
72         },
73         { "dump", 0, raid3_main, G_NULL_OPTS, NULL,
74             "prov ..."
75         },
76         { "insert", G_FLAG_VERBOSE, NULL,
77             {
78                 { 'h', "hardcode", NULL, G_TYPE_BOOL },
79                 { 'n', "number", NULL, G_TYPE_NUMBER },
80                 G_OPT_SENTINEL
81             },
82             NULL, "[-hv] <-n number> name prov"
83         },
84         { "label", G_FLAG_VERBOSE, raid3_main,
85             {
86                 { 'h', "hardcode", NULL, G_TYPE_BOOL },
87                 { 'F', "nofailsync", NULL, G_TYPE_BOOL },
88                 { 'n', "noautosync", NULL, G_TYPE_BOOL },
89                 { 'r', "round_robin", NULL, G_TYPE_BOOL },
90                 { 'w', "verify", NULL, G_TYPE_BOOL },
91                 G_OPT_SENTINEL
92             },
93             NULL, "[-hFnrvw] name prov prov prov ..."
94         },
95         { "rebuild", G_FLAG_VERBOSE, NULL, G_NULL_OPTS, NULL,
96             "[-v] name prov"
97         },
98         { "remove", G_FLAG_VERBOSE, NULL,
99             {
100                 { 'n', "number", NULL, G_TYPE_NUMBER },
101                 G_OPT_SENTINEL
102             },
103             NULL, "[-v] <-n number> name"
104         },
105         { "stop", G_FLAG_VERBOSE, NULL,
106             {
107                 { 'f', "force", NULL, G_TYPE_BOOL },
108                 G_OPT_SENTINEL
109             },
110             NULL, "[-fv] name ..."
111         },
112         G_CMD_SENTINEL
113 };
114
115 static int verbose = 0;
116
117 static void
118 raid3_main(struct gctl_req *req, unsigned flags)
119 {
120         const char *name;
121
122         if ((flags & G_FLAG_VERBOSE) != 0)
123                 verbose = 1;
124
125         name = gctl_get_ascii(req, "verb");
126         if (name == NULL) {
127                 gctl_error(req, "No '%s' argument.", "verb");
128                 return;
129         }
130         if (strcmp(name, "label") == 0)
131                 raid3_label(req);
132         else if (strcmp(name, "clear") == 0)
133                 raid3_clear(req);
134         else if (strcmp(name, "dump") == 0)
135                 raid3_dump(req);
136         else
137                 gctl_error(req, "Unknown command: %s.", name);
138 }
139
140 static void
141 raid3_label(struct gctl_req *req)
142 {
143         struct g_raid3_metadata md;
144         u_char sector[512];
145         const char *str;
146         unsigned sectorsize, ssize;
147         off_t mediasize, msize;
148         int hardcode, round_robin, verify;
149         int error, i, nargs;
150
151         nargs = gctl_get_int(req, "nargs");
152         if (nargs < 4) {
153                 gctl_error(req, "Too few arguments.");
154                 return;
155         }
156         if (bitcount32(nargs - 2) != 1) {
157                 gctl_error(req, "Invalid number of components.");
158                 return;
159         }
160
161         strlcpy(md.md_magic, G_RAID3_MAGIC, sizeof(md.md_magic));
162         md.md_version = G_RAID3_VERSION;
163         str = gctl_get_ascii(req, "arg0");
164         strlcpy(md.md_name, str, sizeof(md.md_name));
165         md.md_id = arc4random();
166         md.md_all = nargs - 1;
167         md.md_mflags = 0;
168         md.md_dflags = 0;
169         md.md_genid = 0;
170         md.md_syncid = 1;
171         md.md_sync_offset = 0;
172         if (gctl_get_int(req, "noautosync"))
173                 md.md_mflags |= G_RAID3_DEVICE_FLAG_NOAUTOSYNC;
174         if (gctl_get_int(req, "nofailsync"))
175                 md.md_mflags |= G_RAID3_DEVICE_FLAG_NOFAILSYNC;
176         round_robin = gctl_get_int(req, "round_robin");
177         if (round_robin)
178                 md.md_mflags |= G_RAID3_DEVICE_FLAG_ROUND_ROBIN;
179         verify = gctl_get_int(req, "verify");
180         if (verify)
181                 md.md_mflags |= G_RAID3_DEVICE_FLAG_VERIFY;
182         if (round_robin && verify) {
183                 gctl_error(req, "Both '%c' and '%c' options given.", 'r', 'w');
184                 return;
185         }
186         hardcode = gctl_get_int(req, "hardcode");
187
188         /*
189          * Calculate sectorsize by finding least common multiple from
190          * sectorsizes of every disk and find the smallest mediasize.
191          */
192         mediasize = 0;
193         sectorsize = 0;
194         for (i = 1; i < nargs; i++) {
195                 str = gctl_get_ascii(req, "arg%d", i);
196                 msize = g_get_mediasize(str);
197                 ssize = g_get_sectorsize(str);
198                 if (msize == 0 || ssize == 0) {
199                         gctl_error(req, "Can't get informations about %s: %s.",
200                             str, strerror(errno));
201                         return;
202                 }
203                 msize -= ssize;
204                 if (mediasize == 0 || (mediasize > 0 && msize < mediasize))
205                         mediasize = msize;
206                 if (sectorsize == 0)
207                         sectorsize = ssize;
208                 else
209                         sectorsize = g_lcm(sectorsize, ssize);
210         }
211         md.md_mediasize = mediasize * (nargs - 2);
212         md.md_sectorsize = sectorsize * (nargs - 2);
213         md.md_mediasize -= (md.md_mediasize % md.md_sectorsize);
214
215         /*
216          * Clear last sector first, to spoil all components if device exists.
217          */
218         for (i = 1; i < nargs; i++) {
219                 str = gctl_get_ascii(req, "arg%d", i);
220                 error = g_metadata_clear(str, NULL);
221                 if (error != 0) {
222                         gctl_error(req, "Can't store metadata on %s: %s.", str,
223                             strerror(error));
224                         return;
225                 }
226         }
227
228         /*
229          * Ok, store metadata (use disk number as priority).
230          */
231         for (i = 1; i < nargs; i++) {
232                 str = gctl_get_ascii(req, "arg%d", i);
233                 msize = g_get_mediasize(str);
234                 ssize = g_get_sectorsize(str);
235                 if (mediasize < msize - ssize) {
236                         fprintf(stderr,
237                             "warning: %s: only %jd bytes from %jd bytes used.\n",
238                             str, (intmax_t)mediasize, (intmax_t)(msize - ssize));
239                 }
240
241                 md.md_no = i - 1;
242                 md.md_provsize = msize;
243                 if (!hardcode)
244                         bzero(md.md_provider, sizeof(md.md_provider));
245                 else {
246                         if (strncmp(str, _PATH_DEV, strlen(_PATH_DEV)) == 0)
247                                 str += strlen(_PATH_DEV);
248                         strlcpy(md.md_provider, str, sizeof(md.md_provider));
249                 }
250                 if (verify && md.md_no == md.md_all - 1) {
251                         /*
252                          * In "verify" mode, force synchronization of parity
253                          * component on start.
254                          */
255                         md.md_syncid = 0;
256                 }
257                 raid3_metadata_encode(&md, sector);
258                 error = g_metadata_store(str, sector, sizeof(sector));
259                 if (error != 0) {
260                         fprintf(stderr, "Can't store metadata on %s: %s.\n",
261                             str, strerror(error));
262                         gctl_error(req, "Not fully done.");
263                         continue;
264                 }
265                 if (verbose)
266                         printf("Metadata value stored on %s.\n", str);
267         }
268 }
269
270 static void
271 raid3_clear(struct gctl_req *req)
272 {
273         const char *name;
274         int error, i, nargs;
275
276         nargs = gctl_get_int(req, "nargs");
277         if (nargs < 1) {
278                 gctl_error(req, "Too few arguments.");
279                 return;
280         }
281
282         for (i = 0; i < nargs; i++) {
283                 name = gctl_get_ascii(req, "arg%d", i);
284                 error = g_metadata_clear(name, G_RAID3_MAGIC);
285                 if (error != 0) {
286                         fprintf(stderr, "Can't clear metadata on %s: %s.\n",
287                             name, strerror(error));
288                         gctl_error(req, "Not fully done.");
289                         continue;
290                 }
291                 if (verbose)
292                         printf("Metadata cleared on %s.\n", name);
293         }
294 }
295
296 static void
297 raid3_dump(struct gctl_req *req)
298 {
299         struct g_raid3_metadata md, tmpmd;
300         const char *name;
301         int error, i, nargs;
302
303         nargs = gctl_get_int(req, "nargs");
304         if (nargs < 1) {
305                 gctl_error(req, "Too few arguments.");
306                 return;
307         }
308
309         for (i = 0; i < nargs; i++) {
310                 name = gctl_get_ascii(req, "arg%d", i);
311                 error = g_metadata_read(name, (u_char *)&tmpmd, sizeof(tmpmd),
312                     G_RAID3_MAGIC);
313                 if (error != 0) {
314                         fprintf(stderr, "Can't read metadata from %s: %s.\n",
315                             name, strerror(error));
316                         gctl_error(req, "Not fully done.");
317                         continue;
318                 }
319                 if (raid3_metadata_decode((u_char *)&tmpmd, &md) != 0) {
320                         fprintf(stderr, "MD5 hash mismatch for %s, skipping.\n",
321                             name);
322                         gctl_error(req, "Not fully done.");
323                         continue;
324                 }
325                 printf("Metadata on %s:\n", name);
326                 raid3_metadata_dump(&md);
327                 printf("\n");
328         }
329 }