]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sbin/geom/class/mirror/geom_mirror.c
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r304149, and update
[FreeBSD/FreeBSD.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 <err.h>
32 #include <errno.h>
33 #include <paths.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <stdint.h>
37 #include <string.h>
38 #include <strings.h>
39 #include <assert.h>
40 #include <libgeom.h>
41 #include <geom/mirror/g_mirror.h>
42 #include <core/geom.h>
43 #include <misc/subr.h>
44
45 uint32_t lib_version = G_LIB_VERSION;
46 uint32_t version = G_MIRROR_VERSION;
47
48 #define GMIRROR_BALANCE         "load"
49 #define GMIRROR_SLICE           "4096"
50 #define GMIRROR_PRIORITY        "0"
51
52 static void mirror_main(struct gctl_req *req, unsigned flags);
53 static void mirror_activate(struct gctl_req *req);
54 static void mirror_clear(struct gctl_req *req);
55 static void mirror_dump(struct gctl_req *req);
56 static void mirror_label(struct gctl_req *req);
57 static void mirror_resize(struct gctl_req *req, unsigned flags);
58
59 struct g_command class_commands[] = {
60         { "activate", G_FLAG_VERBOSE, mirror_main, G_NULL_OPTS,
61             "[-v] name prov ..."
62         },
63         { "clear", G_FLAG_VERBOSE, mirror_main, G_NULL_OPTS,
64             "[-v] prov ..."
65         },
66         { "configure", G_FLAG_VERBOSE, NULL,
67             {
68                 { 'a', "autosync", NULL, G_TYPE_BOOL },
69                 { 'b', "balance", "", G_TYPE_STRING },
70                 { 'd', "dynamic", NULL, G_TYPE_BOOL },
71                 { 'f', "failsync", NULL, G_TYPE_BOOL },
72                 { 'F', "nofailsync", NULL, G_TYPE_BOOL },
73                 { 'h', "hardcode", NULL, G_TYPE_BOOL },
74                 { 'n', "noautosync", NULL, G_TYPE_BOOL },
75                 { 'p', "priority", "-1", G_TYPE_NUMBER },
76                 { 's', "slice", "-1", G_TYPE_NUMBER },
77                 G_OPT_SENTINEL
78             },
79             "[-adfFhnv] [-b balance] [-s slice] name\n"
80             "[-v] -p priority name prov"
81         },
82         { "create", G_FLAG_VERBOSE, NULL,
83             {
84                 { 'b', "balance", GMIRROR_BALANCE, G_TYPE_STRING },
85                 { 'F', "nofailsync", NULL, G_TYPE_BOOL },
86                 { 'n', "noautosync", NULL, G_TYPE_BOOL },
87                 { 's', "slice", GMIRROR_SLICE, G_TYPE_NUMBER },
88                 G_OPT_SENTINEL
89             },
90             "[-Fnv] [-b balance] [-s slice] name prov ..."
91         },
92         { "deactivate", G_FLAG_VERBOSE, NULL, G_NULL_OPTS,
93             "[-v] name prov ..."
94         },
95         { "destroy", G_FLAG_VERBOSE, NULL,
96             {
97                 { 'f', "force", NULL, G_TYPE_BOOL },
98                 G_OPT_SENTINEL
99             },
100             "[-fv] name ..."
101         },
102         { "dump", 0, mirror_main, G_NULL_OPTS,
103             "prov ..."
104         },
105         { "forget", G_FLAG_VERBOSE, NULL, G_NULL_OPTS,
106             "name ..."
107         },
108         { "label", G_FLAG_VERBOSE, mirror_main,
109             {
110                 { 'b', "balance", GMIRROR_BALANCE, G_TYPE_STRING },
111                 { 'F', "nofailsync", NULL, G_TYPE_BOOL },
112                 { 'h', "hardcode", NULL, G_TYPE_BOOL },
113                 { 'n', "noautosync", NULL, G_TYPE_BOOL },
114                 { 's', "slice", GMIRROR_SLICE, G_TYPE_NUMBER },
115                 G_OPT_SENTINEL
116             },
117             "[-Fhnv] [-b balance] [-s slice] name prov ..."
118         },
119         { "insert", G_FLAG_VERBOSE, NULL,
120             {
121                 { 'h', "hardcode", NULL, G_TYPE_BOOL },
122                 { 'i', "inactive", NULL, G_TYPE_BOOL },
123                 { 'p', "priority", GMIRROR_PRIORITY, G_TYPE_NUMBER },
124                 G_OPT_SENTINEL
125             },
126             "[-hiv] [-p priority] name prov ..."
127         },
128         { "rebuild", G_FLAG_VERBOSE, NULL, G_NULL_OPTS,
129             "[-v] name prov ..."
130         },
131         { "remove", G_FLAG_VERBOSE, NULL, G_NULL_OPTS,
132             "[-v] name prov ..."
133         },
134         { "resize", G_FLAG_VERBOSE, mirror_resize,
135             {
136                 { 's', "size", "*", G_TYPE_STRING },
137                 G_OPT_SENTINEL
138             },
139             "[-s size] [-v] name"
140         },
141         { "stop", G_FLAG_VERBOSE, NULL,
142             {
143                 { 'f', "force", NULL, G_TYPE_BOOL },
144                 G_OPT_SENTINEL
145             },
146             "[-fv] name ..."
147         },
148         G_CMD_SENTINEL
149 };
150
151 static int verbose = 0;
152
153 static void
154 mirror_main(struct gctl_req *req, unsigned flags)
155 {
156         const char *name;
157
158         if ((flags & G_FLAG_VERBOSE) != 0)
159                 verbose = 1;
160
161         name = gctl_get_ascii(req, "verb");
162         if (name == NULL) {
163                 gctl_error(req, "No '%s' argument.", "verb");
164                 return;
165         }
166         if (strcmp(name, "label") == 0)
167                 mirror_label(req);
168         else if (strcmp(name, "clear") == 0)
169                 mirror_clear(req);
170         else if (strcmp(name, "dump") == 0)
171                 mirror_dump(req);
172         else if (strcmp(name, "activate") == 0)
173                 mirror_activate(req);
174         else
175                 gctl_error(req, "Unknown command: %s.", name);
176 }
177
178 static void
179 mirror_label(struct gctl_req *req)
180 {
181         struct g_mirror_metadata md;
182         u_char sector[512];
183         const char *str;
184         unsigned sectorsize;
185         off_t mediasize;
186         intmax_t val;
187         int error, i, nargs, bal, hardcode;
188
189         nargs = gctl_get_int(req, "nargs");
190         if (nargs < 2) {
191                 gctl_error(req, "Too few arguments.");
192                 return;
193         }
194
195         strlcpy(md.md_magic, G_MIRROR_MAGIC, sizeof(md.md_magic));
196         md.md_version = G_MIRROR_VERSION;
197         str = gctl_get_ascii(req, "arg0");
198         strlcpy(md.md_name, str, sizeof(md.md_name));
199         md.md_mid = arc4random();
200         md.md_all = nargs - 1;
201         md.md_mflags = 0;
202         md.md_dflags = 0;
203         md.md_genid = 0;
204         md.md_syncid = 1;
205         md.md_sync_offset = 0;
206         val = gctl_get_intmax(req, "slice");
207         md.md_slice = val;
208         str = gctl_get_ascii(req, "balance");
209         bal = balance_id(str);
210         if (bal == -1) {
211                 gctl_error(req, "Invalid balance algorithm.");
212                 return;
213         }
214         md.md_balance = bal;
215         if (gctl_get_int(req, "noautosync"))
216                 md.md_mflags |= G_MIRROR_DEVICE_FLAG_NOAUTOSYNC;
217         if (gctl_get_int(req, "nofailsync"))
218                 md.md_mflags |= G_MIRROR_DEVICE_FLAG_NOFAILSYNC;
219         hardcode = gctl_get_int(req, "hardcode");
220
221         /*
222          * Calculate sectorsize by finding least common multiple from
223          * sectorsizes of every disk and find the smallest mediasize.
224          */
225         mediasize = 0;
226         sectorsize = 0;
227         for (i = 1; i < nargs; i++) {
228                 unsigned ssize;
229                 off_t msize;
230
231                 str = gctl_get_ascii(req, "arg%d", i);
232                 msize = g_get_mediasize(str);
233                 ssize = g_get_sectorsize(str);
234                 if (msize == 0 || ssize == 0) {
235                         gctl_error(req, "Can't get informations about %s: %s.",
236                             str, strerror(errno));
237                         return;
238                 }
239                 msize -= ssize;
240                 if (mediasize == 0 || (mediasize > 0 && msize < mediasize))
241                         mediasize = msize;
242                 if (sectorsize == 0)
243                         sectorsize = ssize;
244                 else
245                         sectorsize = g_lcm(sectorsize, ssize);
246         }
247         md.md_mediasize = mediasize;
248         md.md_sectorsize = sectorsize;
249         md.md_mediasize -= (md.md_mediasize % md.md_sectorsize);
250
251         /*
252          * Clear last sector first, to spoil all components if device exists.
253          */
254         for (i = 1; i < nargs; i++) {
255                 str = gctl_get_ascii(req, "arg%d", i);
256                 error = g_metadata_clear(str, NULL);
257                 if (error != 0) {
258                         gctl_error(req, "Can't store metadata on %s: %s.", str,
259                             strerror(error));
260                         return;
261                 }
262         }
263
264         /*
265          * Ok, store metadata (use disk number as priority).
266          */
267         for (i = 1; i < nargs; i++) {
268                 str = gctl_get_ascii(req, "arg%d", i);
269                 md.md_did = arc4random();
270                 md.md_priority = i - 1;
271                 md.md_provsize = g_get_mediasize(str);
272                 assert(md.md_provsize != 0);
273                 if (!hardcode)
274                         bzero(md.md_provider, sizeof(md.md_provider));
275                 else {
276                         if (strncmp(str, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
277                                 str += sizeof(_PATH_DEV) - 1;
278                         strlcpy(md.md_provider, str, sizeof(md.md_provider));
279                 }
280                 mirror_metadata_encode(&md, sector);
281                 error = g_metadata_store(str, sector, sizeof(sector));
282                 if (error != 0) {
283                         fprintf(stderr, "Can't store metadata on %s: %s.\n",
284                             str, strerror(error));
285                         gctl_error(req, "Not fully done.");
286                         continue;
287                 }
288                 if (verbose)
289                         printf("Metadata value stored on %s.\n", str);
290         }
291 }
292
293 static void
294 mirror_clear(struct gctl_req *req)
295 {
296         const char *name;
297         int error, i, nargs;
298
299         nargs = gctl_get_int(req, "nargs");
300         if (nargs < 1) {
301                 gctl_error(req, "Too few arguments.");
302                 return;
303         }
304
305         for (i = 0; i < nargs; i++) {
306                 name = gctl_get_ascii(req, "arg%d", i);
307                 error = g_metadata_clear(name, G_MIRROR_MAGIC);
308                 if (error != 0) {
309                         fprintf(stderr, "Can't clear metadata on %s: %s.\n",
310                             name, strerror(error));
311                         gctl_error(req, "Not fully done.");
312                         continue;
313                 }
314                 if (verbose)
315                         printf("Metadata cleared on %s.\n", name);
316         }
317 }
318
319 static void
320 mirror_dump(struct gctl_req *req)
321 {
322         struct g_mirror_metadata md, tmpmd;
323         const char *name;
324         int error, i, nargs;
325
326         nargs = gctl_get_int(req, "nargs");
327         if (nargs < 1) {
328                 gctl_error(req, "Too few arguments.");
329                 return;
330         }
331
332         for (i = 0; i < nargs; i++) {
333                 name = gctl_get_ascii(req, "arg%d", i);
334                 error = g_metadata_read(name, (u_char *)&tmpmd, sizeof(tmpmd),
335                     G_MIRROR_MAGIC);
336                 if (error != 0) {
337                         fprintf(stderr, "Can't read metadata from %s: %s.\n",
338                             name, strerror(error));
339                         gctl_error(req, "Not fully done.");
340                         continue;
341                 }
342                 if (mirror_metadata_decode((u_char *)&tmpmd, &md) != 0) {
343                         fprintf(stderr, "MD5 hash mismatch for %s, skipping.\n",
344                             name);
345                         gctl_error(req, "Not fully done.");
346                         continue;
347                 }
348                 printf("Metadata on %s:\n", name);
349                 mirror_metadata_dump(&md);
350                 printf("\n");
351         }
352 }
353
354 static void
355 mirror_activate(struct gctl_req *req)
356 {
357         struct g_mirror_metadata md, tmpmd;
358         const char *name, *path;
359         int error, i, nargs;
360
361         nargs = gctl_get_int(req, "nargs");
362         if (nargs < 2) {
363                 gctl_error(req, "Too few arguments.");
364                 return;
365         }
366         name = gctl_get_ascii(req, "arg0");
367
368         for (i = 1; i < nargs; i++) {
369                 path = gctl_get_ascii(req, "arg%d", i);
370                 error = g_metadata_read(path, (u_char *)&tmpmd, sizeof(tmpmd),
371                     G_MIRROR_MAGIC);
372                 if (error != 0) {
373                         fprintf(stderr, "Cannot read metadata from %s: %s.\n",
374                             path, strerror(error));
375                         gctl_error(req, "Not fully done.");
376                         continue;
377                 }
378                 if (mirror_metadata_decode((u_char *)&tmpmd, &md) != 0) {
379                         fprintf(stderr,
380                             "MD5 hash mismatch for provider %s, skipping.\n",
381                             path);
382                         gctl_error(req, "Not fully done.");
383                         continue;
384                 }
385                 if (strcmp(md.md_name, name) != 0) {
386                         fprintf(stderr,
387                             "Provider %s is not the mirror %s component.\n",
388                             path, name);
389                         gctl_error(req, "Not fully done.");
390                         continue;
391                 }
392                 md.md_dflags &= ~G_MIRROR_DISK_FLAG_INACTIVE;
393                 mirror_metadata_encode(&md, (u_char *)&tmpmd);
394                 error = g_metadata_store(path, (u_char *)&tmpmd, sizeof(tmpmd));
395                 if (error != 0) {
396                         fprintf(stderr, "Cannot write metadata from %s: %s.\n",
397                             path, strerror(error));
398                         gctl_error(req, "Not fully done.");
399                         continue;
400                 }
401                 if (verbose)
402                         printf("Provider %s activated.\n", path);
403         }
404 }
405
406 static struct gclass *
407 find_class(struct gmesh *mesh, const char *name)
408 {
409         struct gclass *classp;
410
411         LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
412                 if (strcmp(classp->lg_name, name) == 0)
413                         return (classp);
414         }
415         return (NULL);
416 }
417
418 static struct ggeom *
419 find_geom(struct gclass *classp, const char *name)
420 {
421         struct ggeom *gp;
422
423         LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
424                 if (strcmp(gp->lg_name, name) == 0)
425                         return (gp);
426         }
427         return (NULL);
428 }
429
430 static void
431 mirror_resize(struct gctl_req *req, unsigned flags __unused)
432 {
433         struct gmesh mesh;
434         struct gclass *classp;
435         struct ggeom *gp;
436         struct gprovider *pp;
437         struct gconsumer *cp;
438         off_t size;
439         int error, nargs;
440         const char *name;
441         char ssize[30];
442
443         nargs = gctl_get_int(req, "nargs");
444         if (nargs < 1) {
445                 gctl_error(req, "Too few arguments.");
446                 return;
447         }
448         error = geom_gettree(&mesh);
449         if (error)
450                 errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
451         name = gctl_get_ascii(req, "class");
452         if (name == NULL)
453                 abort();
454         classp = find_class(&mesh, name);
455         if (classp == NULL)
456                 errx(EXIT_FAILURE, "Class %s not found.", name);
457         name = gctl_get_ascii(req, "arg0");
458         if (name == NULL)
459                 abort();
460         gp = find_geom(classp, name);
461         if (gp == NULL)
462                 errx(EXIT_FAILURE, "No such geom: %s.", name);
463         pp = LIST_FIRST(&gp->lg_provider);
464         if (pp == NULL)
465                 errx(EXIT_FAILURE, "Provider of geom %s not found.", name);
466         size = pp->lg_mediasize;
467         name = gctl_get_ascii(req, "size");
468         if (name == NULL)
469                 errx(EXIT_FAILURE, "The size is not specified.");
470         if (*name == '*') {
471 #define CSZ(c)  ((c)->lg_provider->lg_mediasize - \
472     (c)->lg_provider->lg_sectorsize)
473                 /* Find the maximum possible size */
474                 LIST_FOREACH(cp, &gp->lg_consumer, lg_consumer) {
475                         if (CSZ(cp) > size)
476                                 size = CSZ(cp);
477                 }
478                 LIST_FOREACH(cp, &gp->lg_consumer, lg_consumer) {
479                         if (CSZ(cp) < size)
480                                 size = CSZ(cp);
481                 }
482 #undef CSZ
483                 if (size == pp->lg_mediasize)
484                         errx(EXIT_FAILURE,
485                             "Cannot expand provider %s\n",
486                             pp->lg_name);
487         } else {
488                 error = g_parse_lba(name, pp->lg_sectorsize, &size);
489                 if (error)
490                         errc(EXIT_FAILURE, error, "Invalid size param");
491                 size *= pp->lg_sectorsize;
492         }
493         snprintf(ssize, sizeof(ssize), "%ju", (uintmax_t)size);
494         gctl_change_param(req, "size", -1, ssize);
495         geom_deletetree(&mesh);
496         gctl_issue(req);
497 }