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