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