2 * Copyright (c) 2006 Ruslan Ermilov <ru@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
36 #include <geom/cache/g_cache.h>
38 #include "core/geom.h"
39 #include "misc/subr.h"
42 uint32_t lib_version = G_LIB_VERSION;
43 uint32_t version = G_CACHE_VERSION;
45 #define GCACHE_BLOCKSIZE "65536"
46 #define GCACHE_SIZE "100"
48 static void cache_main(struct gctl_req *req, unsigned flags);
49 static void cache_clear(struct gctl_req *req);
50 static void cache_dump(struct gctl_req *req);
51 static void cache_label(struct gctl_req *req);
53 struct g_command class_commands[] = {
54 { "clear", G_FLAG_VERBOSE, cache_main, G_NULL_OPTS,
57 { "configure", G_FLAG_VERBOSE, NULL,
59 { 'b', "blocksize", "0", G_TYPE_NUMBER },
60 { 's', "size", "0", G_TYPE_NUMBER },
63 "[-v] [-b blocksize] [-s size] name"
65 { "create", G_FLAG_VERBOSE | G_FLAG_LOADKLD, NULL,
67 { 'b', "blocksize", GCACHE_BLOCKSIZE, G_TYPE_NUMBER },
68 { 's', "size", GCACHE_SIZE, G_TYPE_NUMBER },
71 "[-v] [-b blocksize] [-s size] name prov"
73 { "destroy", G_FLAG_VERBOSE, NULL,
75 { 'f', "force", NULL, G_TYPE_BOOL },
80 { "dump", 0, cache_main, G_NULL_OPTS,
83 { "label", G_FLAG_VERBOSE | G_FLAG_LOADKLD, cache_main,
85 { 'b', "blocksize", GCACHE_BLOCKSIZE, G_TYPE_NUMBER },
86 { 's', "size", GCACHE_SIZE, G_TYPE_NUMBER },
89 "[-v] [-b blocksize] [-s size] name prov"
91 { "reset", G_FLAG_VERBOSE, NULL, G_NULL_OPTS,
94 { "stop", G_FLAG_VERBOSE, NULL,
96 { 'f', "force", NULL, G_TYPE_BOOL },
104 static int verbose = 0;
107 cache_main(struct gctl_req *req, unsigned flags)
111 if ((flags & G_FLAG_VERBOSE) != 0)
114 name = gctl_get_ascii(req, "verb");
116 gctl_error(req, "No '%s' argument.", "verb");
119 if (strcmp(name, "label") == 0)
121 else if (strcmp(name, "clear") == 0)
123 else if (strcmp(name, "dump") == 0)
126 gctl_error(req, "Unknown command: %s.", name);
130 cache_label(struct gctl_req *req)
132 struct g_cache_metadata md;
138 nargs = gctl_get_int(req, "nargs");
140 gctl_error(req, "Invalid number of arguments.");
144 strlcpy(md.md_magic, G_CACHE_MAGIC, sizeof(md.md_magic));
145 md.md_version = G_CACHE_VERSION;
146 name = gctl_get_ascii(req, "arg0");
147 strlcpy(md.md_name, name, sizeof(md.md_name));
148 val = gctl_get_intmax(req, "blocksize");
150 val = gctl_get_intmax(req, "size");
153 name = gctl_get_ascii(req, "arg1");
154 md.md_provsize = g_get_mediasize(name);
155 if (md.md_provsize == 0) {
156 fprintf(stderr, "Can't get mediasize of %s: %s.\n",
157 name, strerror(errno));
158 gctl_error(req, "Not fully done.");
161 cache_metadata_encode(&md, sector);
162 error = g_metadata_store(name, sector, sizeof(sector));
164 fprintf(stderr, "Can't store metadata on %s: %s.\n",
165 name, strerror(error));
166 gctl_error(req, "Not fully done.");
170 printf("Metadata value stored on %s.\n", name);
174 cache_clear(struct gctl_req *req)
179 nargs = gctl_get_int(req, "nargs");
181 gctl_error(req, "Too few arguments.");
185 for (i = 0; i < nargs; i++) {
186 name = gctl_get_ascii(req, "arg%d", i);
187 error = g_metadata_clear(name, G_CACHE_MAGIC);
189 fprintf(stderr, "Can't clear metadata on %s: %s.\n",
190 name, strerror(error));
191 gctl_error(req, "Not fully done.");
195 printf("Metadata cleared on %s.\n", name);
200 cache_metadata_dump(const struct g_cache_metadata *md)
203 printf(" Magic string: %s\n", md->md_magic);
204 printf(" Metadata version: %u\n", (u_int)md->md_version);
205 printf(" Device name: %s\n", md->md_name);
206 printf(" Block size: %u\n", (u_int)md->md_bsize);
207 printf(" Cache size: %u\n", (u_int)md->md_size);
208 printf(" Provider size: %ju\n", (uintmax_t)md->md_provsize);
212 cache_dump(struct gctl_req *req)
214 struct g_cache_metadata md, tmpmd;
218 nargs = gctl_get_int(req, "nargs");
220 gctl_error(req, "Too few arguments.");
224 for (i = 0; i < nargs; i++) {
225 name = gctl_get_ascii(req, "arg%d", i);
226 error = g_metadata_read(name, (u_char *)&tmpmd, sizeof(tmpmd),
229 fprintf(stderr, "Can't read metadata from %s: %s.\n",
230 name, strerror(error));
231 gctl_error(req, "Not fully done.");
234 cache_metadata_decode((u_char *)&tmpmd, &md);
235 printf("Metadata on %s:\n", name);
236 cache_metadata_dump(&md);