]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sbin/geom/class/stripe/geom_stripe.c
This commit was generated by cvs2svn to compensate for changes in r149245,
[FreeBSD/FreeBSD.git] / sbin / geom / class / stripe / geom_stripe.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/stripe/g_stripe.h>
41
42 #include "core/geom.h"
43 #include "misc/subr.h"
44
45
46 uint32_t lib_version = G_LIB_VERSION;
47 uint32_t version = G_STRIPE_VERSION;
48
49 static intmax_t stripesize = 4096;
50
51 static void stripe_main(struct gctl_req *req, unsigned flags);
52 static void stripe_clear(struct gctl_req *req);
53 static void stripe_dump(struct gctl_req *req);
54 static void stripe_label(struct gctl_req *req);
55
56 struct g_command class_commands[] = {
57         { "clear", G_FLAG_VERBOSE, stripe_main, G_NULL_OPTS,
58             "[-v] prov ..."
59         },
60         { "create", G_FLAG_VERBOSE | G_FLAG_LOADKLD, NULL,
61             {
62                 { 's', "stripesize", &stripesize, G_TYPE_NUMBER },
63                 G_OPT_SENTINEL
64             },
65             "[-hv] [-s stripesize] name prov prov ..."
66         },
67         { "destroy", G_FLAG_VERBOSE, NULL,
68             {
69                 { 'f', "force", NULL, G_TYPE_NONE },
70                 G_OPT_SENTINEL
71             },
72             "[-fv] name ..."
73         },
74         { "dump", 0, stripe_main, G_NULL_OPTS,
75             "dump prov ..."
76         },
77         { "label", G_FLAG_VERBOSE | G_FLAG_LOADKLD, stripe_main,
78             {
79                 { 'h', "hardcode", NULL, G_TYPE_NONE },
80                 { 's', "stripesize", &stripesize, G_TYPE_NUMBER },
81                 G_OPT_SENTINEL
82             },
83             "[-hv] [-s stripesize] name prov prov ..."
84         },
85         { "stop", G_FLAG_VERBOSE, NULL,
86             {
87                 { 'f', "force", NULL, G_TYPE_NONE },
88                 G_OPT_SENTINEL
89             },
90             "[-fv] name ..."
91         },
92         G_CMD_SENTINEL
93 };
94
95 static int verbose = 0;
96
97 static void
98 stripe_main(struct gctl_req *req, unsigned flags)
99 {
100         const char *name;
101
102         if ((flags & G_FLAG_VERBOSE) != 0)
103                 verbose = 1;
104
105         name = gctl_get_asciiparam(req, "verb");
106         if (name == NULL) {
107                 gctl_error(req, "No '%s' argument.", "verb");
108                 return;
109         }
110         if (strcmp(name, "label") == 0)
111                 stripe_label(req);
112         else if (strcmp(name, "clear") == 0)
113                 stripe_clear(req);
114         else if (strcmp(name, "dump") == 0)
115                 stripe_dump(req);
116         else
117                 gctl_error(req, "Unknown command: %s.", name);
118 }
119
120 static void
121 stripe_label(struct gctl_req *req)
122 {
123         struct g_stripe_metadata md;
124         intmax_t *stripesizep;
125         off_t compsize, msize;
126         u_char sector[512];
127         unsigned i, ssize, secsize;
128         const char *name;
129         char param[16];
130         int *hardcode, *nargs, error;
131
132         nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
133         if (nargs == NULL) {
134                 gctl_error(req, "No '%s' argument.", "nargs");
135                 return;
136         }
137         if (*nargs <= 2) {
138                 gctl_error(req, "Too few arguments.");
139                 return;
140         }
141         hardcode = gctl_get_paraml(req, "hardcode", sizeof(*hardcode));
142         if (hardcode == NULL) {
143                 gctl_error(req, "No '%s' argument.", "hardcode");
144                 return;
145         }
146
147         /*
148          * Clear last sector first to spoil all components if device exists.
149          */
150         compsize = 0;
151         secsize = 0;
152         for (i = 1; i < (unsigned)*nargs; i++) {
153                 snprintf(param, sizeof(param), "arg%u", i);
154                 name = gctl_get_asciiparam(req, param);
155
156                 msize = g_get_mediasize(name);
157                 ssize = g_get_sectorsize(name);
158                 if (msize == 0 || ssize == 0) {
159                         gctl_error(req, "Can't get informations about %s: %s.",
160                             name, strerror(errno));
161                         return;
162                 }
163                 msize -= ssize;
164                 if (compsize == 0 || (compsize > 0 && msize < compsize))
165                         compsize = msize;
166                 if (secsize == 0)
167                         secsize = ssize;
168                 else
169                         secsize = g_lcm(secsize, ssize);
170
171                 error = g_metadata_clear(name, NULL);
172                 if (error != 0) {
173                         gctl_error(req, "Can't store metadata on %s: %s.", name,
174                             strerror(error));
175                         return;
176                 }
177         }
178
179         strlcpy(md.md_magic, G_STRIPE_MAGIC, sizeof(md.md_magic));
180         md.md_version = G_STRIPE_VERSION;
181         name = gctl_get_asciiparam(req, "arg0");
182         if (name == NULL) {
183                 gctl_error(req, "No 'arg%u' argument.", 0);
184                 return;
185         }
186         strlcpy(md.md_name, name, sizeof(md.md_name));
187         md.md_id = arc4random();
188         md.md_all = *nargs - 1;
189         stripesizep = gctl_get_paraml(req, "stripesize", sizeof(*stripesizep));
190         if (stripesizep == NULL) {
191                 gctl_error(req, "No '%s' argument.", "stripesize");
192                 return;
193         }
194         if ((*stripesizep % secsize) != 0) {
195                 gctl_error(req, "Stripesize should be multiple of %u.",
196                     secsize);
197                 return;
198         }
199         md.md_stripesize = *stripesizep;
200
201         /*
202          * Ok, store metadata.
203          */
204         for (i = 1; i < (unsigned)*nargs; i++) {
205                 snprintf(param, sizeof(param), "arg%u", i);
206                 name = gctl_get_asciiparam(req, param);
207
208                 msize = g_get_mediasize(name);
209                 ssize = g_get_sectorsize(name);
210                 if (compsize < msize - ssize) {
211                         fprintf(stderr,
212                             "warning: %s: only %jd bytes from %jd bytes used.\n",
213                             name, (intmax_t)compsize, (intmax_t)(msize - ssize));
214                 }
215
216                 md.md_no = i - 1;
217                 md.md_provsize = msize;
218                 if (!*hardcode)
219                         bzero(md.md_provider, sizeof(md.md_provider));
220                 else {
221                         if (strncmp(name, _PATH_DEV, strlen(_PATH_DEV)) == 0)
222                                 name += strlen(_PATH_DEV);
223                         strlcpy(md.md_provider, name, sizeof(md.md_provider));
224                 }
225                 stripe_metadata_encode(&md, sector);
226                 error = g_metadata_store(name, sector, sizeof(sector));
227                 if (error != 0) {
228                         fprintf(stderr, "Can't store metadata on %s: %s.\n",
229                             name, strerror(error));
230                         gctl_error(req, "Not fully done.");
231                         continue;
232                 }
233                 if (verbose)
234                         printf("Metadata value stored on %s.\n", name);
235         }
236 }
237
238 static void
239 stripe_clear(struct gctl_req *req)
240 {
241         const char *name;
242         char param[16];
243         unsigned i;
244         int *nargs, error;
245
246         nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
247         if (nargs == NULL) {
248                 gctl_error(req, "No '%s' argument.", "nargs");
249                 return;
250         }
251         if (*nargs < 1) {
252                 gctl_error(req, "Too few arguments.");
253                 return;
254         }
255
256         for (i = 0; i < (unsigned)*nargs; i++) {
257                 snprintf(param, sizeof(param), "arg%u", i);
258                 name = gctl_get_asciiparam(req, param);
259
260                 error = g_metadata_clear(name, G_STRIPE_MAGIC);
261                 if (error != 0) {
262                         fprintf(stderr, "Can't clear metadata on %s: %s.\n",
263                             name, strerror(error));
264                         gctl_error(req, "Not fully done.");
265                         continue;
266                 }
267                 if (verbose)
268                         printf("Metadata cleared on %s.\n", name); 
269         }
270 }
271
272 static void 
273 stripe_metadata_dump(const struct g_stripe_metadata *md)
274 {
275
276         printf("         Magic string: %s\n", md->md_magic); 
277         printf("     Metadata version: %u\n", (u_int)md->md_version);
278         printf("          Device name: %s\n", md->md_name);
279         printf("            Device ID: %u\n", (u_int)md->md_id);
280         printf("          Disk number: %u\n", (u_int)md->md_no);
281         printf("Total number of disks: %u\n", (u_int)md->md_all);
282         printf("          Stripe size: %u\n", (u_int)md->md_stripesize);
283         printf("   Hardcoded provider: %s\n", md->md_provider);
284 }
285
286 static void
287 stripe_dump(struct gctl_req *req)
288 {
289         struct g_stripe_metadata md, tmpmd;
290         const char *name;
291         char param[16];
292         int *nargs, error, i;
293
294         nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
295         if (nargs == NULL) {
296                 gctl_error(req, "No '%s' argument.", "nargs");
297                 return;
298         }
299         if (*nargs < 1) {
300                 gctl_error(req, "Too few arguments.");
301                 return;
302         }
303
304         for (i = 0; i < *nargs; i++) {
305                 snprintf(param, sizeof(param), "arg%u", i);
306                 name = gctl_get_asciiparam(req, param);
307
308                 error = g_metadata_read(name, (u_char *)&tmpmd, sizeof(tmpmd),
309                     G_STRIPE_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                 stripe_metadata_decode((u_char *)&tmpmd, &md);
317                 printf("Metadata on %s:\n", name);
318                 stripe_metadata_dump(&md);
319                 printf("\n");
320         }
321 }