geom_cache.c revision 169586
1/*-
2 * Copyright (c) 2006 Ruslan Ermilov <ru@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 AUTHOR 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 AUTHOR 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: head/sbin/geom/class/cache/geom_cache.c 169586 2007-05-15 20:25:18Z marcel $");
29
30#include <errno.h>
31#include <stdio.h>
32#include <stdint.h>
33#include <string.h>
34#include <strings.h>
35#include <libgeom.h>
36#include <geom/cache/g_cache.h>
37
38#include "core/geom.h"
39#include "misc/subr.h"
40
41
42uint32_t lib_version = G_LIB_VERSION;
43uint32_t version = G_CACHE_VERSION;
44
45static intmax_t blocksize_label = 65536;
46static intmax_t size_label = 100;
47static intmax_t blocksize_configure = 0;
48static intmax_t size_configure = 0;
49
50static void cache_main(struct gctl_req *req, unsigned flags);
51static void cache_clear(struct gctl_req *req);
52static void cache_dump(struct gctl_req *req);
53static void cache_label(struct gctl_req *req);
54
55struct g_command class_commands[] = {
56	{ "clear", G_FLAG_VERBOSE, cache_main, G_NULL_OPTS, NULL,
57	    "[-v] prov ..."
58	},
59	{ "configure", G_FLAG_VERBOSE, NULL,
60	    {
61		{ 'b', "blocksize", &blocksize_configure, G_TYPE_NUMBER },
62		{ 's', "size", &size_configure, G_TYPE_NUMBER },
63		G_OPT_SENTINEL
64	    },
65	    NULL, "[-v] [-b blocksize] [-s size] name"
66	},
67	{ "create", G_FLAG_VERBOSE | G_FLAG_LOADKLD, NULL,
68	    {
69		{ 'b', "blocksize", &blocksize_label, G_TYPE_NUMBER },
70		{ 's', "size", &size_label, G_TYPE_NUMBER },
71		G_OPT_SENTINEL
72	    },
73	    NULL, "[-v] [-b blocksize] [-s size] name prov"
74	},
75	{ "destroy", G_FLAG_VERBOSE, NULL,
76	    {
77		{ 'f', "force", NULL, G_TYPE_BOOL },
78		G_OPT_SENTINEL
79	    },
80	    NULL, "[-fv] name ..."
81	},
82	{ "dump", 0, cache_main, G_NULL_OPTS, NULL,
83	    "prov ..."
84	},
85	{ "label", G_FLAG_VERBOSE | G_FLAG_LOADKLD, cache_main,
86	    {
87		{ 'b', "blocksize", &blocksize_label, G_TYPE_NUMBER },
88		{ 's', "size", &size_label, G_TYPE_NUMBER },
89		G_OPT_SENTINEL
90	    },
91	    NULL, "[-v] [-b blocksize] [-s size] name prov"
92	},
93	{ "reset", G_FLAG_VERBOSE, NULL, G_NULL_OPTS, NULL,
94	    "[-v] name ..."
95	},
96	{ "stop", G_FLAG_VERBOSE, NULL,
97	    {
98		{ 'f', "force", NULL, G_TYPE_BOOL },
99		G_OPT_SENTINEL
100	    },
101	    NULL, "[-fv] name ..."
102	},
103	G_CMD_SENTINEL
104};
105
106static int verbose = 0;
107
108static void
109cache_main(struct gctl_req *req, unsigned flags)
110{
111	const char *name;
112
113	if ((flags & G_FLAG_VERBOSE) != 0)
114		verbose = 1;
115
116	name = gctl_get_ascii(req, "verb");
117	if (name == NULL) {
118		gctl_error(req, "No '%s' argument.", "verb");
119		return;
120	}
121	if (strcmp(name, "label") == 0)
122		cache_label(req);
123	else if (strcmp(name, "clear") == 0)
124		cache_clear(req);
125	else if (strcmp(name, "dump") == 0)
126		cache_dump(req);
127	else
128		gctl_error(req, "Unknown command: %s.", name);
129}
130
131static void
132cache_label(struct gctl_req *req)
133{
134	struct g_cache_metadata md;
135	u_char sector[512];
136	const char *name;
137	int error, nargs;
138	intmax_t val;
139
140	nargs = gctl_get_int(req, "nargs");
141	if (nargs != 2) {
142		gctl_error(req, "Invalid number of arguments.");
143		return;
144	}
145
146	strlcpy(md.md_magic, G_CACHE_MAGIC, sizeof(md.md_magic));
147	md.md_version = G_CACHE_VERSION;
148	name = gctl_get_ascii(req, "arg0");
149	strlcpy(md.md_name, name, sizeof(md.md_name));
150	val = gctl_get_intmax(req, "blocksize");
151	md.md_bsize = val;
152	val = gctl_get_intmax(req, "size");
153	md.md_size = val;
154
155	name = gctl_get_ascii(req, "arg1");
156	md.md_provsize = g_get_mediasize(name);
157	if (md.md_provsize == 0) {
158		fprintf(stderr, "Can't get mediasize of %s: %s.\n",
159		    name, strerror(errno));
160		gctl_error(req, "Not fully done.");
161		return;
162	}
163	cache_metadata_encode(&md, sector);
164	error = g_metadata_store(name, sector, sizeof(sector));
165	if (error != 0) {
166		fprintf(stderr, "Can't store metadata on %s: %s.\n",
167		    name, strerror(error));
168		gctl_error(req, "Not fully done.");
169		return;
170	}
171	if (verbose)
172		printf("Metadata value stored on %s.\n", name);
173}
174
175static void
176cache_clear(struct gctl_req *req)
177{
178	const char *name;
179	int error, i, nargs;
180
181	nargs = gctl_get_int(req, "nargs");
182	if (nargs < 1) {
183		gctl_error(req, "Too few arguments.");
184		return;
185	}
186
187	for (i = 0; i < nargs; i++) {
188		name = gctl_get_ascii(req, "arg%d", i);
189		error = g_metadata_clear(name, G_CACHE_MAGIC);
190		if (error != 0) {
191			fprintf(stderr, "Can't clear metadata on %s: %s.\n",
192			    name, strerror(error));
193			gctl_error(req, "Not fully done.");
194			continue;
195		}
196		if (verbose)
197			printf("Metadata cleared on %s.\n", name);
198	}
199}
200
201static void
202cache_metadata_dump(const struct g_cache_metadata *md)
203{
204
205	printf("         Magic string: %s\n", md->md_magic);
206	printf("     Metadata version: %u\n", (u_int)md->md_version);
207	printf("          Device name: %s\n", md->md_name);
208	printf("           Block size: %u\n", (u_int)md->md_bsize);
209	printf("           Cache size: %u\n", (u_int)md->md_size);
210	printf("        Provider size: %ju\n", (uintmax_t)md->md_provsize);
211}
212
213static void
214cache_dump(struct gctl_req *req)
215{
216	struct g_cache_metadata md, tmpmd;
217	const char *name;
218	int error, i, nargs;
219
220	nargs = gctl_get_int(req, "nargs");
221	if (nargs < 1) {
222		gctl_error(req, "Too few arguments.");
223		return;
224	}
225
226	for (i = 0; i < nargs; i++) {
227		name = gctl_get_ascii(req, "arg%d", i);
228		error = g_metadata_read(name, (u_char *)&tmpmd, sizeof(tmpmd),
229		    G_CACHE_MAGIC);
230		if (error != 0) {
231			fprintf(stderr, "Can't read metadata from %s: %s.\n",
232			    name, strerror(error));
233			gctl_error(req, "Not fully done.");
234			continue;
235		}
236		cache_metadata_decode((u_char *)&tmpmd, &md);
237		printf("Metadata on %s:\n", name);
238		cache_metadata_dump(&md);
239		printf("\n");
240	}
241}
242