geom_sunlabel.c revision 115512
1/*- 2 * Copyright (c) 2002 Poul-Henning Kamp 3 * Copyright (c) 2002 Networks Associates Technology, Inc. 4 * All rights reserved. 5 * 6 * This software was developed for the FreeBSD Project by Poul-Henning Kamp 7 * and NAI Labs, the Security Research Division of Network Associates, Inc. 8 * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the 9 * DARPA CHATS research program. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. The names of the authors may not be used to endorse or promote 20 * products derived from this software without specific prior written 21 * permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 * 35 * $FreeBSD: head/sys/geom/geom_sunlabel.c 115512 2003-05-31 19:37:21Z phk $ 36 */ 37 38 39#include <sys/param.h> 40#include <sys/endian.h> 41#include <sys/systm.h> 42#include <sys/kernel.h> 43#include <sys/conf.h> 44#include <sys/bio.h> 45#include <sys/malloc.h> 46#include <sys/lock.h> 47#include <sys/mutex.h> 48#include <sys/sun_disklabel.h> 49#include <geom/geom.h> 50#include <geom/geom_slice.h> 51#include <machine/endian.h> 52 53#define SUNLABEL_CLASS_NAME "SUN" 54 55struct g_sunlabel_softc { 56 int sectorsize; 57 int nheads; 58 int nsects; 59 int nalt; 60}; 61 62static int 63g_sunlabel_modify(struct g_geom *gp, struct g_sunlabel_softc *ms, u_char *sec0) 64{ 65 int i, error; 66 u_int u, v, csize; 67 struct sun_disklabel sl; 68 69 error = sunlabel_dec(sec0, &sl); 70 if (error) 71 return (error); 72 73 csize = sl.sl_ntracks * sl.sl_nsectors; 74 75 for (i = 0; i < SUN_NPART; i++) { 76 v = sl.sl_part[i].sdkp_cyloffset; 77 u = sl.sl_part[i].sdkp_nsectors; 78 error = g_slice_config(gp, i, G_SLICE_CONFIG_CHECK, 79 ((off_t)v * csize) << 9ULL, 80 ((off_t)u) << 9ULL, 81 ms->sectorsize, 82 "%s%c", gp->name, 'a' + i); 83 if (error) 84 return (error); 85 } 86 for (i = 0; i < SUN_NPART; i++) { 87 v = sl.sl_part[i].sdkp_cyloffset; 88 u = sl.sl_part[i].sdkp_nsectors; 89 g_slice_config(gp, i, G_SLICE_CONFIG_SET, 90 ((off_t)v * csize) << 9ULL, 91 ((off_t)u) << 9ULL, 92 ms->sectorsize, 93 "%s%c", gp->name, 'a' + i); 94 } 95 ms->nalt = sl.sl_acylinders; 96 ms->nheads = sl.sl_ntracks; 97 ms->nsects = sl.sl_nsectors; 98 99 return (0); 100} 101 102static void 103g_sunlabel_hotwrite(void *arg, int flag) 104{ 105 struct bio *bp; 106 struct g_geom *gp; 107 struct g_slicer *gsp; 108 struct g_slice *gsl; 109 struct g_sunlabel_softc *ms; 110 u_char *p; 111 int error; 112 113 KASSERT(flag != EV_CANCEL, ("g_sunlabel_hotwrite cancelled")); 114 bp = arg; 115 gp = bp->bio_to->geom; 116 gsp = gp->softc; 117 ms = gsp->softc; 118 gsl = &gsp->slices[bp->bio_to->index]; 119 /* 120 * XXX: For all practical purposes, this whould be equvivalent to 121 * XXX: "p = (u_char *)bp->bio_data;" because the label is always 122 * XXX: in the first sector and we refuse sectors smaller than the 123 * XXX: label. 124 */ 125 p = (u_char *)bp->bio_data - (bp->bio_offset + gsl->offset); 126 127 error = g_sunlabel_modify(gp, ms, p); 128 if (error) { 129 g_io_deliver(bp, EPERM); 130 return; 131 } 132 g_slice_finish_hot(bp); 133} 134 135static void 136g_sunlabel_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp) 137{ 138 struct g_slicer *gsp; 139 struct g_sunlabel_softc *ms; 140 141 gsp = gp->softc; 142 ms = gsp->softc; 143 g_slice_dumpconf(sb, indent, gp, cp, pp); 144 if (indent == NULL) { 145 sbuf_printf(sb, " sc %u hd %u alt %u", 146 ms->nsects, ms->nheads, ms->nalt); 147 } 148} 149 150struct g_hh01 { 151 struct g_geom *gp; 152 struct g_sunlabel_softc *ms; 153 u_char *label; 154 int error; 155}; 156 157static void 158g_sunlabel_callconfig(void *arg, int flag) 159{ 160 struct g_hh01 *hp; 161 162 hp = arg; 163 hp->error = g_sunlabel_modify(hp->gp, hp->ms, hp->label); 164 if (!hp->error) 165 hp->error = g_write_data(LIST_FIRST(&hp->gp->consumer), 166 0, hp->label, SUN_SIZE); 167} 168 169/* 170 * NB! curthread is user process which GCTL'ed. 171 */ 172static int 173g_sunlabel_config(struct gctl_req *req, struct g_geom *gp, const char *verb) 174{ 175 u_char *label; 176 int error, i; 177 struct g_hh01 h0h0; 178 struct g_slicer *gsp; 179 struct g_consumer *cp; 180 181 g_topology_assert(); 182 cp = LIST_FIRST(&gp->consumer); 183 gsp = gp->softc; 184 if (!strcmp(verb, "write label")) { 185 label = gctl_get_paraml(req, "label", SUN_SIZE); 186 if (label == NULL) 187 return (EINVAL); 188 h0h0.gp = gp; 189 h0h0.ms = gsp->softc; 190 h0h0.label = label; 191 h0h0.error = -1; 192 /* XXX: Does this reference register with our selfdestruct code ? */ 193 error = g_access_rel(cp, 1, 1, 1); 194 if (error) { 195 g_free(label); 196 return (error); 197 } 198 g_topology_unlock(); 199 g_waitfor_event(g_sunlabel_callconfig, &h0h0, M_WAITOK, gp, NULL); 200 g_topology_lock(); 201 error = h0h0.error; 202 g_access_rel(cp, -1, -1, -1); 203 g_free(label); 204 } else if (!strcmp(verb, "write bootcode")) { 205 label = gctl_get_paraml(req, "bootcode", SUN_BOOTSIZE); 206 if (label == NULL) 207 return (EINVAL); 208 /* XXX: Does this reference register with our selfdestruct code ? */ 209 error = g_access_rel(cp, 1, 1, 1); 210 if (error) { 211 g_free(label); 212 return (error); 213 } 214 for (i = 0; i < SUN_NPART; i++) { 215 if (gsp->slices[i].length <= SUN_BOOTSIZE) 216 continue; 217 g_write_data(cp, 218 gsp->slices[i].offset + SUN_SIZE, label + SUN_SIZE, 219 SUN_BOOTSIZE - SUN_SIZE); 220 } 221 g_access_rel(cp, -1, -1, -1); 222 g_free(label); 223 } else { 224 return (gctl_error(req, "Unknown verb parameter")); 225 } 226 227 return (error); 228} 229 230static struct g_geom * 231g_sunlabel_taste(struct g_class *mp, struct g_provider *pp, int flags) 232{ 233 struct g_geom *gp; 234 struct g_consumer *cp; 235 int error; 236 u_char *buf; 237 struct g_sunlabel_softc *ms; 238 struct g_slicer *gsp; 239 240 g_trace(G_T_TOPOLOGY, "g_sunlabel_taste(%s,%s)", mp->name, pp->name); 241 g_topology_assert(); 242 if (flags == G_TF_NORMAL && 243 !strcmp(pp->geom->class->name, SUNLABEL_CLASS_NAME)) 244 return (NULL); 245 gp = g_slice_new(mp, 8, pp, &cp, &ms, sizeof *ms, NULL); 246 if (gp == NULL) 247 return (NULL); 248 gsp = gp->softc; 249 gp->dumpconf = g_sunlabel_dumpconf; 250 do { 251 if (gp->rank != 2 && flags == G_TF_NORMAL) 252 break; 253 ms->sectorsize = cp->provider->sectorsize; 254 if (ms->sectorsize < 512) 255 break; 256 g_topology_unlock(); 257 buf = g_read_data(cp, 0, ms->sectorsize, &error); 258 g_topology_lock(); 259 if (buf == NULL || error != 0) 260 break; 261 262 g_sunlabel_modify(gp, ms, buf); 263 g_free(buf); 264 265 break; 266 } while (0); 267 g_access_rel(cp, -1, 0, 0); 268 if (LIST_EMPTY(&gp->provider)) { 269 g_slice_spoiled(cp); 270 return (NULL); 271 } 272 g_slice_conf_hot(gp, 0, 0, SUN_SIZE, 273 G_SLICE_HOT_ALLOW, G_SLICE_HOT_DENY, G_SLICE_HOT_CALL); 274 gsp->hot = g_sunlabel_hotwrite; 275 return (gp); 276} 277 278static struct g_class g_sunlabel_class = { 279 .name = SUNLABEL_CLASS_NAME, 280 .taste = g_sunlabel_taste, 281 .config_geom = g_sunlabel_config, 282}; 283 284DECLARE_GEOM_CLASS(g_sunlabel_class, g_sunlabel); 285