1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
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/param.h>
30 #include <errno.h>
31 #include <paths.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <strings.h>
36 #include <assert.h>
37 #include <libgeom.h>
38 #include <geom/concat/g_concat.h>
39
40 #include "core/geom.h"
41 #include "misc/subr.h"
42
43
44 uint32_t lib_version = G_LIB_VERSION;
45 uint32_t version = G_CONCAT_VERSION;
46
47 static void concat_main(struct gctl_req *req, unsigned flags);
48 static void concat_clear(struct gctl_req *req);
49 static void concat_dump(struct gctl_req *req);
50 static void concat_label(struct gctl_req *req);
51
52 struct g_command class_commands[] = {
53 { "append", G_FLAG_VERBOSE, NULL,
54 {
55 { 'h', "hardcode", NULL, G_TYPE_BOOL },
56 G_OPT_SENTINEL
57 },
58 "[-hv] name prov"
59 },
60 { "clear", G_FLAG_VERBOSE, concat_main, G_NULL_OPTS,
61 "[-v] prov ..."
62 },
63 { "create", G_FLAG_VERBOSE | G_FLAG_LOADKLD, NULL, G_NULL_OPTS,
64 "[-v] name prov ..."
65 },
66 { "destroy", G_FLAG_VERBOSE, NULL,
67 {
68 { 'f', "force", NULL, G_TYPE_BOOL },
69 G_OPT_SENTINEL
70 },
71 "[-fv] name ..."
72 },
73 { "dump", 0, concat_main, G_NULL_OPTS,
74 "prov ..."
75 },
76 { "label", G_FLAG_VERBOSE | G_FLAG_LOADKLD, concat_main,
77 {
78 { 'h', "hardcode", NULL, G_TYPE_BOOL },
79 G_OPT_SENTINEL
80 },
81 "[-hv] name prov ..."
82 },
83 { "stop", G_FLAG_VERBOSE, NULL,
84 {
85 { 'f', "force", NULL, G_TYPE_BOOL },
86 G_OPT_SENTINEL
87 },
88 "[-fv] name ..."
89 },
90 G_CMD_SENTINEL
91 };
92
93 static int verbose = 0;
94
95 static void
concat_main(struct gctl_req * req,unsigned flags)96 concat_main(struct gctl_req *req, unsigned flags)
97 {
98 const char *name;
99
100 if ((flags & G_FLAG_VERBOSE) != 0)
101 verbose = 1;
102
103 name = gctl_get_ascii(req, "verb");
104 if (name == NULL) {
105 gctl_error(req, "No '%s' argument.", "verb");
106 return;
107 }
108 if (strcmp(name, "label") == 0)
109 concat_label(req);
110 else if (strcmp(name, "clear") == 0)
111 concat_clear(req);
112 else if (strcmp(name, "dump") == 0)
113 concat_dump(req);
114 else
115 gctl_error(req, "Unknown command: %s.", name);
116 }
117
118 static void
concat_label(struct gctl_req * req)119 concat_label(struct gctl_req *req)
120 {
121 struct g_concat_metadata md;
122 u_char sector[512];
123 const char *name;
124 int error, i, hardcode, nargs;
125
126 bzero(sector, sizeof(sector));
127 nargs = gctl_get_int(req, "nargs");
128 if (nargs < 2) {
129 gctl_error(req, "Too few arguments.");
130 return;
131 }
132 hardcode = gctl_get_int(req, "hardcode");
133
134 /*
135 * Clear last sector first to spoil all components if device exists.
136 */
137 for (i = 1; i < nargs; i++) {
138 name = gctl_get_ascii(req, "arg%d", i);
139 error = g_metadata_clear(name, NULL);
140 if (error != 0) {
141 gctl_error(req, "Can't store metadata on %s: %s.", name,
142 strerror(error));
143 return;
144 }
145 }
146
147 strlcpy(md.md_magic, G_CONCAT_MAGIC, sizeof(md.md_magic));
148 md.md_version = G_CONCAT_VERSION;
149 name = gctl_get_ascii(req, "arg0");
150 strlcpy(md.md_name, name, sizeof(md.md_name));
151 md.md_id = arc4random();
152 md.md_all = nargs - 1;
153
154 /*
155 * Ok, store metadata.
156 */
157 for (i = 1; i < nargs; i++) {
158 name = gctl_get_ascii(req, "arg%d", i);
159 md.md_no = i - 1;
160 if (!hardcode)
161 bzero(md.md_provider, sizeof(md.md_provider));
162 else {
163 if (strncmp(name, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
164 name += sizeof(_PATH_DEV) - 1;
165 strlcpy(md.md_provider, name, sizeof(md.md_provider));
166 }
167 md.md_provsize = g_get_mediasize(name);
168 if (md.md_provsize == 0) {
169 fprintf(stderr, "Can't get mediasize of %s: %s.\n",
170 name, strerror(errno));
171 gctl_error(req, "Not fully done.");
172 continue;
173 }
174 concat_metadata_encode(&md, sector);
175 error = g_metadata_store(name, sector, sizeof(sector));
176 if (error != 0) {
177 fprintf(stderr, "Can't store metadata on %s: %s.\n",
178 name, strerror(error));
179 gctl_error(req, "Not fully done.");
180 continue;
181 }
182 if (verbose)
183 printf("Metadata value stored on %s.\n", name);
184 }
185 }
186
187 static void
concat_clear(struct gctl_req * req)188 concat_clear(struct gctl_req *req)
189 {
190 const char *name;
191 int error, i, nargs;
192
193 nargs = gctl_get_int(req, "nargs");
194 if (nargs < 1) {
195 gctl_error(req, "Too few arguments.");
196 return;
197 }
198
199 for (i = 0; i < nargs; i++) {
200 name = gctl_get_ascii(req, "arg%d", i);
201 error = g_metadata_clear(name, G_CONCAT_MAGIC);
202 if (error != 0) {
203 fprintf(stderr, "Can't clear metadata on %s: %s.\n",
204 name, strerror(error));
205 gctl_error(req, "Not fully done.");
206 continue;
207 }
208 if (verbose)
209 printf("Metadata cleared on %s.\n", name);
210 }
211 }
212
213 static void
concat_metadata_dump(const struct g_concat_metadata * md)214 concat_metadata_dump(const struct g_concat_metadata *md)
215 {
216
217 printf(" Magic string: %s\n", md->md_magic);
218 printf(" Metadata version: %u\n", (u_int)md->md_version);
219 printf(" Device name: %s\n", md->md_name);
220 printf(" Device ID: %u\n", (u_int)md->md_id);
221 printf(" Disk number: %u\n", (u_int)md->md_no);
222 printf("Total number of disks: %u\n", (u_int)md->md_all);
223 printf(" Hardcoded provider: %s\n", md->md_provider);
224 }
225
226 static void
concat_dump(struct gctl_req * req)227 concat_dump(struct gctl_req *req)
228 {
229 struct g_concat_metadata md, tmpmd;
230 const char *name;
231 int error, i, nargs;
232
233 nargs = gctl_get_int(req, "nargs");
234 if (nargs < 1) {
235 gctl_error(req, "Too few arguments.");
236 return;
237 }
238
239 for (i = 0; i < nargs; i++) {
240 name = gctl_get_ascii(req, "arg%d", i);
241 error = g_metadata_read(name, (u_char *)&tmpmd, sizeof(tmpmd),
242 G_CONCAT_MAGIC);
243 if (error != 0) {
244 fprintf(stderr, "Can't read metadata from %s: %s.\n",
245 name, strerror(error));
246 gctl_error(req, "Not fully done.");
247 continue;
248 }
249 concat_metadata_decode((u_char *)&tmpmd, &md);
250 printf("Metadata on %s:\n", name);
251 concat_metadata_dump(&md);
252 printf("\n");
253 }
254 }
255