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