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