geom_cache.c revision 330737
1285SN/A/*-
2461SN/A * Copyright (c) 2006 Ruslan Ermilov <ru@FreeBSD.org>
3285SN/A * All rights reserved.
4285SN/A *
5285SN/A * Redistribution and use in source and binary forms, with or without
6285SN/A * modification, are permitted provided that the following conditions
7285SN/A * are met:
8285SN/A * 1. Redistributions of source code must retain the above copyright
9285SN/A *    notice, this list of conditions and the following disclaimer.
10285SN/A * 2. Redistributions in binary form must reproduce the above copyright
11285SN/A *    notice, this list of conditions and the following disclaimer in the
12285SN/A *    documentation and/or other materials provided with the distribution.
13285SN/A *
14285SN/A * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15285SN/A * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16285SN/A * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17285SN/A * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18285SN/A * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19285SN/A * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20285SN/A * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21285SN/A * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22285SN/A * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23285SN/A * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24285SN/A * SUCH DAMAGE.
25285SN/A */
26285SN/A
27285SN/A#include <sys/cdefs.h>
28285SN/A__FBSDID("$FreeBSD: stable/10/sbin/geom/class/cache/geom_cache.c 330737 2018-03-10 04:17:01Z asomers $");
29285SN/A
30285SN/A#include <errno.h>
31285SN/A#include <stdio.h>
32285SN/A#include <stdint.h>
33285SN/A#include <string.h>
34285SN/A#include <strings.h>
35285SN/A#include <libgeom.h>
36285SN/A#include <geom/cache/g_cache.h>
37285SN/A
38285SN/A#include "core/geom.h"
39285SN/A#include "misc/subr.h"
40285SN/A
41285SN/A
42285SN/Auint32_t lib_version = G_LIB_VERSION;
43285SN/Auint32_t version = G_CACHE_VERSION;
44
45#define	GCACHE_BLOCKSIZE	"65536"
46#define	GCACHE_SIZE		"100"
47
48static void cache_main(struct gctl_req *req, unsigned flags);
49static void cache_clear(struct gctl_req *req);
50static void cache_dump(struct gctl_req *req);
51static void cache_label(struct gctl_req *req);
52
53struct g_command class_commands[] = {
54	{ "clear", G_FLAG_VERBOSE, cache_main, G_NULL_OPTS,
55	    "[-v] prov ..."
56	},
57	{ "configure", G_FLAG_VERBOSE, NULL,
58	    {
59		{ 'b', "blocksize", "0", G_TYPE_NUMBER },
60		{ 's', "size", "0", G_TYPE_NUMBER },
61		G_OPT_SENTINEL
62	    },
63	    "[-v] [-b blocksize] [-s size] name"
64	},
65	{ "create", G_FLAG_VERBOSE | G_FLAG_LOADKLD, NULL,
66	    {
67		{ 'b', "blocksize", GCACHE_BLOCKSIZE, G_TYPE_NUMBER },
68		{ 's', "size", GCACHE_SIZE, G_TYPE_NUMBER },
69		G_OPT_SENTINEL
70	    },
71	    "[-v] [-b blocksize] [-s size] name prov"
72	},
73	{ "destroy", G_FLAG_VERBOSE, NULL,
74	    {
75		{ 'f', "force", NULL, G_TYPE_BOOL },
76		G_OPT_SENTINEL
77	    },
78	    "[-fv] name ..."
79	},
80	{ "dump", 0, cache_main, G_NULL_OPTS,
81	    "prov ..."
82	},
83	{ "label", G_FLAG_VERBOSE | G_FLAG_LOADKLD, cache_main,
84	    {
85		{ 'b', "blocksize", GCACHE_BLOCKSIZE, G_TYPE_NUMBER },
86		{ 's', "size", GCACHE_SIZE, G_TYPE_NUMBER },
87		G_OPT_SENTINEL
88	    },
89	    "[-v] [-b blocksize] [-s size] name prov"
90	},
91	{ "reset", G_FLAG_VERBOSE, NULL, G_NULL_OPTS,
92	    "[-v] name ..."
93	},
94	{ "stop", G_FLAG_VERBOSE, NULL,
95	    {
96		{ 'f', "force", NULL, G_TYPE_BOOL },
97		G_OPT_SENTINEL
98	    },
99	    "[-fv] name ..."
100	},
101	G_CMD_SENTINEL
102};
103
104static int verbose = 0;
105
106static void
107cache_main(struct gctl_req *req, unsigned flags)
108{
109	const char *name;
110
111	if ((flags & G_FLAG_VERBOSE) != 0)
112		verbose = 1;
113
114	name = gctl_get_ascii(req, "verb");
115	if (name == NULL) {
116		gctl_error(req, "No '%s' argument.", "verb");
117		return;
118	}
119	if (strcmp(name, "label") == 0)
120		cache_label(req);
121	else if (strcmp(name, "clear") == 0)
122		cache_clear(req);
123	else if (strcmp(name, "dump") == 0)
124		cache_dump(req);
125	else
126		gctl_error(req, "Unknown command: %s.", name);
127}
128
129static void
130cache_label(struct gctl_req *req)
131{
132	struct g_cache_metadata md;
133	u_char sector[512];
134	const char *name;
135	int error, nargs;
136	intmax_t val;
137
138	bzero(sector, sizeof(sector));
139	nargs = gctl_get_int(req, "nargs");
140	if (nargs != 2) {
141		gctl_error(req, "Invalid number of arguments.");
142		return;
143	}
144
145	strlcpy(md.md_magic, G_CACHE_MAGIC, sizeof(md.md_magic));
146	md.md_version = G_CACHE_VERSION;
147	name = gctl_get_ascii(req, "arg0");
148	strlcpy(md.md_name, name, sizeof(md.md_name));
149	val = gctl_get_intmax(req, "blocksize");
150	md.md_bsize = val;
151	val = gctl_get_intmax(req, "size");
152	md.md_size = val;
153
154	name = gctl_get_ascii(req, "arg1");
155	md.md_provsize = g_get_mediasize(name);
156	if (md.md_provsize == 0) {
157		fprintf(stderr, "Can't get mediasize of %s: %s.\n",
158		    name, strerror(errno));
159		gctl_error(req, "Not fully done.");
160		return;
161	}
162	cache_metadata_encode(&md, sector);
163	error = g_metadata_store(name, sector, sizeof(sector));
164	if (error != 0) {
165		fprintf(stderr, "Can't store metadata on %s: %s.\n",
166		    name, strerror(error));
167		gctl_error(req, "Not fully done.");
168		return;
169	}
170	if (verbose)
171		printf("Metadata value stored on %s.\n", name);
172}
173
174static void
175cache_clear(struct gctl_req *req)
176{
177	const char *name;
178	int error, i, nargs;
179
180	nargs = gctl_get_int(req, "nargs");
181	if (nargs < 1) {
182		gctl_error(req, "Too few arguments.");
183		return;
184	}
185
186	for (i = 0; i < nargs; i++) {
187		name = gctl_get_ascii(req, "arg%d", i);
188		error = g_metadata_clear(name, G_CACHE_MAGIC);
189		if (error != 0) {
190			fprintf(stderr, "Can't clear metadata on %s: %s.\n",
191			    name, strerror(error));
192			gctl_error(req, "Not fully done.");
193			continue;
194		}
195		if (verbose)
196			printf("Metadata cleared on %s.\n", name);
197	}
198}
199
200static void
201cache_metadata_dump(const struct g_cache_metadata *md)
202{
203
204	printf("         Magic string: %s\n", md->md_magic);
205	printf("     Metadata version: %u\n", (u_int)md->md_version);
206	printf("          Device name: %s\n", md->md_name);
207	printf("           Block size: %u\n", (u_int)md->md_bsize);
208	printf("           Cache size: %u\n", (u_int)md->md_size);
209	printf("        Provider size: %ju\n", (uintmax_t)md->md_provsize);
210}
211
212static void
213cache_dump(struct gctl_req *req)
214{
215	struct g_cache_metadata md, tmpmd;
216	const char *name;
217	int error, i, nargs;
218
219	nargs = gctl_get_int(req, "nargs");
220	if (nargs < 1) {
221		gctl_error(req, "Too few arguments.");
222		return;
223	}
224
225	for (i = 0; i < nargs; i++) {
226		name = gctl_get_ascii(req, "arg%d", i);
227		error = g_metadata_read(name, (u_char *)&tmpmd, sizeof(tmpmd),
228		    G_CACHE_MAGIC);
229		if (error != 0) {
230			fprintf(stderr, "Can't read metadata from %s: %s.\n",
231			    name, strerror(error));
232			gctl_error(req, "Not fully done.");
233			continue;
234		}
235		cache_metadata_decode((u_char *)&tmpmd, &md);
236		printf("Metadata on %s:\n", name);
237		cache_metadata_dump(&md);
238		printf("\n");
239	}
240}
241