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