]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - sbin/geom/class/mirror/geom_mirror.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / sbin / geom / class / mirror / geom_mirror.c
1 /*-
2  * Copyright (c) 2004-2009 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/mirror/g_mirror.h>
41 #include <core/geom.h>
42 #include <misc/subr.h>
43
44 uint32_t lib_version = G_LIB_VERSION;
45 uint32_t version = G_MIRROR_VERSION;
46
47 #define GMIRROR_BALANCE         "load"
48 #define GMIRROR_SLICE           "4096"
49 #define GMIRROR_PRIORITY        "0"
50
51 static void mirror_main(struct gctl_req *req, unsigned flags);
52 static void mirror_activate(struct gctl_req *req);
53 static void mirror_clear(struct gctl_req *req);
54 static void mirror_dump(struct gctl_req *req);
55 static void mirror_label(struct gctl_req *req);
56
57 struct g_command class_commands[] = {
58         { "activate", G_FLAG_VERBOSE, mirror_main, G_NULL_OPTS,
59             "[-v] name prov ..."
60         },
61         { "clear", G_FLAG_VERBOSE, mirror_main, G_NULL_OPTS,
62             "[-v] prov ..."
63         },
64         { "configure", G_FLAG_VERBOSE, NULL,
65             {
66                 { 'a', "autosync", NULL, G_TYPE_BOOL },
67                 { 'b', "balance", "", G_TYPE_STRING },
68                 { 'd', "dynamic", NULL, G_TYPE_BOOL },
69                 { 'f', "failsync", NULL, G_TYPE_BOOL },
70                 { 'F', "nofailsync", NULL, G_TYPE_BOOL },
71                 { 'h', "hardcode", NULL, G_TYPE_BOOL },
72                 { 'n', "noautosync", NULL, G_TYPE_BOOL },
73                 { 'p', "priority", "-1", G_TYPE_NUMBER },
74                 { 's', "slice", "-1", G_TYPE_NUMBER },
75                 G_OPT_SENTINEL
76             },
77             "[-adfFhnv] [-b balance] [-s slice] name\n"
78             "[-v] -p priority name prov"
79         },
80         { "deactivate", G_FLAG_VERBOSE, NULL, G_NULL_OPTS,
81             "[-v] name prov ..."
82         },
83         { "dump", 0, mirror_main, G_NULL_OPTS,
84             "prov ..."
85         },
86         { "forget", G_FLAG_VERBOSE, NULL, G_NULL_OPTS,
87             "name ..."
88         },
89         { "label", G_FLAG_VERBOSE, mirror_main,
90             {
91                 { 'b', "balance", GMIRROR_BALANCE, G_TYPE_STRING },
92                 { 'F', "nofailsync", NULL, G_TYPE_BOOL },
93                 { 'h', "hardcode", NULL, G_TYPE_BOOL },
94                 { 'n', "noautosync", NULL, G_TYPE_BOOL },
95                 { 's', "slice", GMIRROR_SLICE, G_TYPE_NUMBER },
96                 G_OPT_SENTINEL
97             },
98             "[-Fhnv] [-b balance] [-s slice] name prov ..."
99         },
100         { "insert", G_FLAG_VERBOSE, NULL,
101             {
102                 { 'h', "hardcode", NULL, G_TYPE_BOOL },
103                 { 'i', "inactive", NULL, G_TYPE_BOOL },
104                 { 'p', "priority", GMIRROR_PRIORITY, G_TYPE_NUMBER },
105                 G_OPT_SENTINEL
106             },
107             "[-hiv] [-p priority] name prov ..."
108         },
109         { "rebuild", G_FLAG_VERBOSE, NULL, G_NULL_OPTS,
110             "[-v] name prov ..."
111         },
112         { "remove", G_FLAG_VERBOSE, NULL, G_NULL_OPTS,
113             "[-v] name prov ..."
114         },
115         { "stop", G_FLAG_VERBOSE, NULL,
116             {
117                 { 'f', "force", NULL, G_TYPE_BOOL },
118                 G_OPT_SENTINEL
119             },
120             "[-fv] name ..."
121         },
122         G_CMD_SENTINEL
123 };
124
125 static int verbose = 0;
126
127 static void
128 mirror_main(struct gctl_req *req, unsigned flags)
129 {
130         const char *name;
131
132         if ((flags & G_FLAG_VERBOSE) != 0)
133                 verbose = 1;
134
135         name = gctl_get_ascii(req, "verb");
136         if (name == NULL) {
137                 gctl_error(req, "No '%s' argument.", "verb");
138                 return;
139         }
140         if (strcmp(name, "label") == 0)
141                 mirror_label(req);
142         else if (strcmp(name, "clear") == 0)
143                 mirror_clear(req);
144         else if (strcmp(name, "dump") == 0)
145                 mirror_dump(req);
146         else if (strcmp(name, "activate") == 0)
147                 mirror_activate(req);
148         else
149                 gctl_error(req, "Unknown command: %s.", name);
150 }
151
152 static void
153 mirror_label(struct gctl_req *req)
154 {
155         struct g_mirror_metadata md;
156         u_char sector[512];
157         const char *str;
158         unsigned sectorsize;
159         off_t mediasize;
160         intmax_t val;
161         int error, i, nargs, bal, hardcode;
162
163         nargs = gctl_get_int(req, "nargs");
164         if (nargs < 2) {
165                 gctl_error(req, "Too few arguments.");
166                 return;
167         }
168
169         strlcpy(md.md_magic, G_MIRROR_MAGIC, sizeof(md.md_magic));
170         md.md_version = G_MIRROR_VERSION;
171         str = gctl_get_ascii(req, "arg0");
172         strlcpy(md.md_name, str, sizeof(md.md_name));
173         md.md_mid = arc4random();
174         md.md_all = nargs - 1;
175         md.md_mflags = 0;
176         md.md_dflags = 0;
177         md.md_genid = 0;
178         md.md_syncid = 1;
179         md.md_sync_offset = 0;
180         val = gctl_get_intmax(req, "slice");
181         md.md_slice = val;
182         str = gctl_get_ascii(req, "balance");
183         bal = balance_id(str);
184         if (bal == -1) {
185                 gctl_error(req, "Invalid balance algorithm.");
186                 return;
187         }
188         md.md_balance = bal;
189         if (gctl_get_int(req, "noautosync"))
190                 md.md_mflags |= G_MIRROR_DEVICE_FLAG_NOAUTOSYNC;
191         if (gctl_get_int(req, "nofailsync"))
192                 md.md_mflags |= G_MIRROR_DEVICE_FLAG_NOFAILSYNC;
193         hardcode = gctl_get_int(req, "hardcode");
194
195         /*
196          * Calculate sectorsize by finding least common multiple from
197          * sectorsizes of every disk and find the smallest mediasize.
198          */
199         mediasize = 0;
200         sectorsize = 0;
201         for (i = 1; i < nargs; i++) {
202                 unsigned ssize;
203                 off_t msize;
204
205                 str = gctl_get_ascii(req, "arg%d", i);
206                 msize = g_get_mediasize(str);
207                 ssize = g_get_sectorsize(str);
208                 if (msize == 0 || ssize == 0) {
209                         gctl_error(req, "Can't get informations about %s: %s.",
210                             str, strerror(errno));
211                         return;
212                 }
213                 msize -= ssize;
214                 if (mediasize == 0 || (mediasize > 0 && msize < mediasize))
215                         mediasize = msize;
216                 if (sectorsize == 0)
217                         sectorsize = ssize;
218                 else
219                         sectorsize = g_lcm(sectorsize, ssize);
220         }
221         md.md_mediasize = mediasize;
222         md.md_sectorsize = sectorsize;
223         md.md_mediasize -= (md.md_mediasize % md.md_sectorsize);
224
225         /*
226          * Clear last sector first, to spoil all components if device exists.
227          */
228         for (i = 1; i < nargs; i++) {
229                 str = gctl_get_ascii(req, "arg%d", i);
230                 error = g_metadata_clear(str, NULL);
231                 if (error != 0) {
232                         gctl_error(req, "Can't store metadata on %s: %s.", str,
233                             strerror(error));
234                         return;
235                 }
236         }
237
238         /*
239          * Ok, store metadata (use disk number as priority).
240          */
241         for (i = 1; i < nargs; i++) {
242                 str = gctl_get_ascii(req, "arg%d", i);
243                 md.md_did = arc4random();
244                 md.md_priority = i - 1;
245                 md.md_provsize = g_get_mediasize(str);
246                 assert(md.md_provsize != 0);
247                 if (!hardcode)
248                         bzero(md.md_provider, sizeof(md.md_provider));
249                 else {
250                         if (strncmp(str, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
251                                 str += sizeof(_PATH_DEV) - 1;
252                         strlcpy(md.md_provider, str, sizeof(md.md_provider));
253                 }
254                 mirror_metadata_encode(&md, sector);
255                 error = g_metadata_store(str, sector, sizeof(sector));
256                 if (error != 0) {
257                         fprintf(stderr, "Can't store metadata on %s: %s.\n",
258                             str, strerror(error));
259                         gctl_error(req, "Not fully done.");
260                         continue;
261                 }
262                 if (verbose)
263                         printf("Metadata value stored on %s.\n", str);
264         }
265 }
266
267 static void
268 mirror_clear(struct gctl_req *req)
269 {
270         const char *name;
271         int error, i, nargs;
272
273         nargs = gctl_get_int(req, "nargs");
274         if (nargs < 1) {
275                 gctl_error(req, "Too few arguments.");
276                 return;
277         }
278
279         for (i = 0; i < nargs; i++) {
280                 name = gctl_get_ascii(req, "arg%d", i);
281                 error = g_metadata_clear(name, G_MIRROR_MAGIC);
282                 if (error != 0) {
283                         fprintf(stderr, "Can't clear metadata on %s: %s.\n",
284                             name, strerror(error));
285                         gctl_error(req, "Not fully done.");
286                         continue;
287                 }
288                 if (verbose)
289                         printf("Metadata cleared on %s.\n", name);
290         }
291 }
292
293 static void
294 mirror_dump(struct gctl_req *req)
295 {
296         struct g_mirror_metadata md, tmpmd;
297         const char *name;
298         int error, i, nargs;
299
300         nargs = gctl_get_int(req, "nargs");
301         if (nargs < 1) {
302                 gctl_error(req, "Too few arguments.");
303                 return;
304         }
305
306         for (i = 0; i < nargs; i++) {
307                 name = gctl_get_ascii(req, "arg%d", i);
308                 error = g_metadata_read(name, (u_char *)&tmpmd, sizeof(tmpmd),
309                     G_MIRROR_MAGIC);
310                 if (error != 0) {
311                         fprintf(stderr, "Can't read metadata from %s: %s.\n",
312                             name, strerror(error));
313                         gctl_error(req, "Not fully done.");
314                         continue;
315                 }
316                 if (mirror_metadata_decode((u_char *)&tmpmd, &md) != 0) {
317                         fprintf(stderr, "MD5 hash mismatch for %s, skipping.\n",
318                             name);
319                         gctl_error(req, "Not fully done.");
320                         continue;
321                 }
322                 printf("Metadata on %s:\n", name);
323                 mirror_metadata_dump(&md);
324                 printf("\n");
325         }
326 }
327
328 static void
329 mirror_activate(struct gctl_req *req)
330 {
331         struct g_mirror_metadata md, tmpmd;
332         const char *name, *path;
333         int error, i, nargs;
334
335         nargs = gctl_get_int(req, "nargs");
336         if (nargs < 2) {
337                 gctl_error(req, "Too few arguments.");
338                 return;
339         }
340         name = gctl_get_ascii(req, "arg0");
341
342         for (i = 1; i < nargs; i++) {
343                 path = gctl_get_ascii(req, "arg%d", i);
344                 error = g_metadata_read(path, (u_char *)&tmpmd, sizeof(tmpmd),
345                     G_MIRROR_MAGIC);
346                 if (error != 0) {
347                         fprintf(stderr, "Cannot read metadata from %s: %s.\n",
348                             path, strerror(error));
349                         gctl_error(req, "Not fully done.");
350                         continue;
351                 }
352                 if (mirror_metadata_decode((u_char *)&tmpmd, &md) != 0) {
353                         fprintf(stderr,
354                             "MD5 hash mismatch for provider %s, skipping.\n",
355                             path);
356                         gctl_error(req, "Not fully done.");
357                         continue;
358                 }
359                 if (strcmp(md.md_name, name) != 0) {
360                         fprintf(stderr,
361                             "Provider %s is not the mirror %s component.\n",
362                             path, name);
363                         gctl_error(req, "Not fully done.");
364                         continue;
365                 }
366                 md.md_dflags &= ~G_MIRROR_DISK_FLAG_INACTIVE;
367                 mirror_metadata_encode(&md, (u_char *)&tmpmd);
368                 error = g_metadata_store(path, (u_char *)&tmpmd, sizeof(tmpmd));
369                 if (error != 0) {
370                         fprintf(stderr, "Cannot write metadata from %s: %s.\n",
371                             path, strerror(error));
372                         gctl_error(req, "Not fully done.");
373                         continue;
374                 }
375                 if (verbose)
376                         printf("Provider %s activated.\n", path);
377         }
378 }