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