2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2006 Ruslan Ermilov <ru@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
38 #include <geom/cache/g_cache.h>
40 #include "core/geom.h"
41 #include "misc/subr.h"
44 uint32_t lib_version = G_LIB_VERSION;
45 uint32_t version = G_CACHE_VERSION;
47 #define GCACHE_BLOCKSIZE "65536"
48 #define GCACHE_SIZE "100"
50 static void cache_main(struct gctl_req *req, unsigned flags);
51 static void cache_clear(struct gctl_req *req);
52 static void cache_dump(struct gctl_req *req);
53 static void cache_label(struct gctl_req *req);
55 struct g_command class_commands[] = {
56 { "clear", G_FLAG_VERBOSE, cache_main, G_NULL_OPTS,
59 { "configure", G_FLAG_VERBOSE, NULL,
61 { 'b', "blocksize", "0", G_TYPE_NUMBER },
62 { 's', "size", "0", G_TYPE_NUMBER },
65 "[-v] [-b blocksize] [-s size] name"
67 { "create", G_FLAG_VERBOSE | G_FLAG_LOADKLD, NULL,
69 { 'b', "blocksize", GCACHE_BLOCKSIZE, G_TYPE_NUMBER },
70 { 's', "size", GCACHE_SIZE, G_TYPE_NUMBER },
73 "[-v] [-b blocksize] [-s size] name prov"
75 { "destroy", G_FLAG_VERBOSE, NULL,
77 { 'f', "force", NULL, G_TYPE_BOOL },
82 { "dump", 0, cache_main, G_NULL_OPTS,
85 { "label", G_FLAG_VERBOSE | G_FLAG_LOADKLD, cache_main,
87 { 'b', "blocksize", GCACHE_BLOCKSIZE, G_TYPE_NUMBER },
88 { 's', "size", GCACHE_SIZE, G_TYPE_NUMBER },
91 "[-v] [-b blocksize] [-s size] name prov"
93 { "reset", G_FLAG_VERBOSE, NULL, G_NULL_OPTS,
96 { "stop", G_FLAG_VERBOSE, NULL,
98 { 'f', "force", NULL, G_TYPE_BOOL },
106 static int verbose = 0;
109 cache_main(struct gctl_req *req, unsigned flags)
113 if ((flags & G_FLAG_VERBOSE) != 0)
116 name = gctl_get_ascii(req, "verb");
118 gctl_error(req, "No '%s' argument.", "verb");
121 if (strcmp(name, "label") == 0)
123 else if (strcmp(name, "clear") == 0)
125 else if (strcmp(name, "dump") == 0)
128 gctl_error(req, "Unknown command: %s.", name);
132 cache_label(struct gctl_req *req)
134 struct g_cache_metadata md;
140 bzero(sector, sizeof(sector));
141 nargs = gctl_get_int(req, "nargs");
143 gctl_error(req, "Invalid number of arguments.");
147 strlcpy(md.md_magic, G_CACHE_MAGIC, sizeof(md.md_magic));
148 md.md_version = G_CACHE_VERSION;
149 name = gctl_get_ascii(req, "arg0");
150 strlcpy(md.md_name, name, sizeof(md.md_name));
151 val = gctl_get_intmax(req, "blocksize");
153 val = gctl_get_intmax(req, "size");
156 name = gctl_get_ascii(req, "arg1");
157 md.md_provsize = g_get_mediasize(name);
158 if (md.md_provsize == 0) {
159 fprintf(stderr, "Can't get mediasize of %s: %s.\n",
160 name, strerror(errno));
161 gctl_error(req, "Not fully done.");
164 cache_metadata_encode(&md, sector);
165 error = g_metadata_store(name, sector, sizeof(sector));
167 fprintf(stderr, "Can't store metadata on %s: %s.\n",
168 name, strerror(error));
169 gctl_error(req, "Not fully done.");
173 printf("Metadata value stored on %s.\n", name);
177 cache_clear(struct gctl_req *req)
182 nargs = gctl_get_int(req, "nargs");
184 gctl_error(req, "Too few arguments.");
188 for (i = 0; i < nargs; i++) {
189 name = gctl_get_ascii(req, "arg%d", i);
190 error = g_metadata_clear(name, G_CACHE_MAGIC);
192 fprintf(stderr, "Can't clear metadata on %s: %s.\n",
193 name, strerror(error));
194 gctl_error(req, "Not fully done.");
198 printf("Metadata cleared on %s.\n", name);
203 cache_metadata_dump(const struct g_cache_metadata *md)
206 printf(" Magic string: %s\n", md->md_magic);
207 printf(" Metadata version: %u\n", (u_int)md->md_version);
208 printf(" Device name: %s\n", md->md_name);
209 printf(" Block size: %u\n", (u_int)md->md_bsize);
210 printf(" Cache size: %u\n", (u_int)md->md_size);
211 printf(" Provider size: %ju\n", (uintmax_t)md->md_provsize);
215 cache_dump(struct gctl_req *req)
217 struct g_cache_metadata md, tmpmd;
221 nargs = gctl_get_int(req, "nargs");
223 gctl_error(req, "Too few arguments.");
227 for (i = 0; i < nargs; i++) {
228 name = gctl_get_ascii(req, "arg%d", i);
229 error = g_metadata_read(name, (u_char *)&tmpmd, sizeof(tmpmd),
232 fprintf(stderr, "Can't read metadata from %s: %s.\n",
233 name, strerror(error));
234 gctl_error(req, "Not fully done.");
237 cache_metadata_decode((u_char *)&tmpmd, &md);
238 printf("Metadata on %s:\n", name);
239 cache_metadata_dump(&md);