geom_sunlabel.c revision 113895
192372Sphk/*- 292372Sphk * Copyright (c) 2002 Poul-Henning Kamp 392372Sphk * Copyright (c) 2002 Networks Associates Technology, Inc. 492372Sphk * All rights reserved. 592372Sphk * 692372Sphk * This software was developed for the FreeBSD Project by Poul-Henning Kamp 792372Sphk * and NAI Labs, the Security Research Division of Network Associates, Inc. 892372Sphk * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the 992372Sphk * DARPA CHATS research program. 1092372Sphk * 1192372Sphk * Redistribution and use in source and binary forms, with or without 1292372Sphk * modification, are permitted provided that the following conditions 1392372Sphk * are met: 1492372Sphk * 1. Redistributions of source code must retain the above copyright 1592372Sphk * notice, this list of conditions and the following disclaimer. 1692372Sphk * 2. Redistributions in binary form must reproduce the above copyright 1792372Sphk * notice, this list of conditions and the following disclaimer in the 1892372Sphk * documentation and/or other materials provided with the distribution. 1992372Sphk * 3. The names of the authors may not be used to endorse or promote 2092372Sphk * products derived from this software without specific prior written 2192372Sphk * permission. 2292372Sphk * 2392372Sphk * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 2492372Sphk * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2592372Sphk * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2692372Sphk * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 2792372Sphk * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2892372Sphk * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2992372Sphk * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3092372Sphk * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3192372Sphk * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3292372Sphk * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3392372Sphk * SUCH DAMAGE. 3492372Sphk * 3592372Sphk * $FreeBSD: head/sys/geom/geom_sunlabel.c 113895 2003-04-23 08:23:01Z phk $ 3692372Sphk */ 3792372Sphk 3892372Sphk 3992372Sphk#include <sys/param.h> 40113011Sphk#include <sys/endian.h> 4192372Sphk#include <sys/systm.h> 4292372Sphk#include <sys/kernel.h> 4392372Sphk#include <sys/conf.h> 4492372Sphk#include <sys/bio.h> 4592372Sphk#include <sys/malloc.h> 4692372Sphk#include <sys/lock.h> 4792372Sphk#include <sys/mutex.h> 48113819Sphk#include <sys/sun_disklabel.h> 4992372Sphk#include <geom/geom.h> 5092372Sphk#include <geom/geom_slice.h> 5192372Sphk#include <machine/endian.h> 5292372Sphk 5397075Sphk#define SUNLABEL_CLASS_NAME "SUN" 5492372Sphk 5592372Sphkstruct g_sunlabel_softc { 56110183Sphk int sectorsize; 57106100Sphk int nheads; 58106100Sphk int nsects; 59106100Sphk int nalt; 6092372Sphk}; 6192372Sphk 6292372Sphkstatic int 63110183Sphkg_sunlabel_modify(struct g_geom *gp, struct g_sunlabel_softc *ms, u_char *sec0) 64110183Sphk{ 65110183Sphk int i, error; 66110183Sphk u_int u, v, csize; 67113819Sphk struct sun_disklabel sl; 68110183Sphk 69113819Sphk error = sunlabel_dec(sec0, &sl); 70113819Sphk if (error) 71113819Sphk return (error); 72110183Sphk 73113819Sphk csize = sl.sl_ntracks * sl.sl_nsectors; 74110183Sphk 75113819Sphk for (i = 0; i < SUN_NPART; i++) { 76113819Sphk v = sl.sl_part[i].sdkp_cyloffset; 77113819Sphk u = sl.sl_part[i].sdkp_nsectors; 78110183Sphk error = g_slice_config(gp, i, G_SLICE_CONFIG_CHECK, 79110183Sphk ((off_t)v * csize) << 9ULL, 80110183Sphk ((off_t)u) << 9ULL, 81110183Sphk ms->sectorsize, 82110183Sphk "%s%c", gp->name, 'a' + i); 83110183Sphk if (error) 84110183Sphk return (error); 85110183Sphk } 86113819Sphk for (i = 0; i < SUN_NPART; i++) { 87113819Sphk v = sl.sl_part[i].sdkp_cyloffset; 88113819Sphk u = sl.sl_part[i].sdkp_nsectors; 89110183Sphk g_slice_config(gp, i, G_SLICE_CONFIG_SET, 90110183Sphk ((off_t)v * csize) << 9ULL, 91110183Sphk ((off_t)u) << 9ULL, 92110183Sphk ms->sectorsize, 93110183Sphk "%s%c", gp->name, 'a' + i); 94110183Sphk } 95113819Sphk ms->nalt = sl.sl_acylinders; 96113819Sphk ms->nheads = sl.sl_ntracks; 97113819Sphk ms->nsects = sl.sl_nsectors; 98110183Sphk 99110183Sphk return (0); 100110183Sphk} 101110183Sphk 102113821Sphkstatic void 103113821Sphkg_sunlabel_hotwrite(void *arg, int flag) 104113821Sphk{ 105113821Sphk struct bio *bp; 106113821Sphk struct g_geom *gp; 107113821Sphk struct g_slicer *gsp; 108113821Sphk struct g_slice *gsl; 109113821Sphk struct g_sunlabel_softc *ms; 110113821Sphk u_char *p; 111113821Sphk int error; 112113821Sphk 113113821Sphk KASSERT(flag != EV_CANCEL, ("g_sunlabel_hotwrite cancelled")); 114113821Sphk bp = arg; 115113821Sphk gp = bp->bio_to->geom; 116113821Sphk gsp = gp->softc; 117113821Sphk ms = gsp->softc; 118113821Sphk gsl = &gsp->slices[bp->bio_to->index]; 119113821Sphk /* 120113821Sphk * XXX: For all practical purposes, this whould be equvivalent to 121113821Sphk * XXX: "p = (u_char *)bp->bio_data;" because the label is always 122113821Sphk * XXX: in the first sector and we refuse sectors smaller than the 123113821Sphk * XXX: label. 124113821Sphk */ 125113821Sphk p = (u_char *)bp->bio_data - (bp->bio_offset + gsl->offset); 126113821Sphk 127113821Sphk error = g_sunlabel_modify(gp, ms, p); 128113821Sphk if (error) { 129113821Sphk g_io_deliver(bp, EPERM); 130113821Sphk return; 131113821Sphk } 132113821Sphk g_slice_finish_hot(bp); 133113821Sphk} 134113821Sphk 13592372Sphkstatic void 136107953Sphkg_sunlabel_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp) 13792372Sphk{ 138106100Sphk struct g_slicer *gsp; 139106100Sphk struct g_sunlabel_softc *ms; 14092372Sphk 141106100Sphk gsp = gp->softc; 142106100Sphk ms = gsp->softc; 14392372Sphk g_slice_dumpconf(sb, indent, gp, cp, pp); 144106100Sphk if (indent == NULL) { 145106100Sphk sbuf_printf(sb, " sc %u hd %u alt %u", 146106100Sphk ms->nsects, ms->nheads, ms->nalt); 147106100Sphk } 14892372Sphk} 14992372Sphk 150113895Sphkstruct h0h0 { 151113895Sphk struct g_geom *gp; 152113895Sphk struct g_sunlabel_softc *ms; 153113895Sphk u_char *label; 154113895Sphk int error; 155113895Sphk}; 156113895Sphk 157113895Sphkstatic void 158113895Sphkg_sunlabel_callconfig(void *arg, int flag) 159113895Sphk{ 160113895Sphk struct h0h0 *hp; 161113895Sphk 162113895Sphk hp = arg; 163113895Sphk hp->error = g_sunlabel_modify(hp->gp, hp->ms, hp->label); 164113895Sphk if (!hp->error) 165113895Sphk hp->error = g_write_data(LIST_FIRST(&hp->gp->consumer), 166113895Sphk 0, hp->label, SUN_SIZE); 167113895Sphk wakeup(hp); 168113895Sphk} 169113895Sphk 170113895Sphk/* 171113895Sphk * NB! curthread is user process which GCTL'ed. 172113895Sphk */ 173113895Sphkstatic int 174113895Sphkg_sunlabel_config(struct gctl_req *req, struct g_geom *gp, const char *verb) 175113895Sphk{ 176113895Sphk u_char *label; 177113895Sphk int error, i; 178113895Sphk struct h0h0 h0h0; 179113895Sphk struct g_slicer *gsp; 180113895Sphk struct g_consumer *cp; 181113895Sphk 182113895Sphk g_topology_assert(); 183113895Sphk cp = LIST_FIRST(&gp->consumer); 184113895Sphk gsp = gp->softc; 185113895Sphk if (!strcmp(verb, "write label")) { 186113895Sphk label = gctl_get_paraml(req, "label", SUN_SIZE); 187113895Sphk if (label == NULL) 188113895Sphk return (EINVAL); 189113895Sphk h0h0.gp = gp; 190113895Sphk h0h0.ms = gsp->softc; 191113895Sphk h0h0.label = label; 192113895Sphk h0h0.error = -1; 193113895Sphk /* XXX: Does this reference register with our selfdestruct code ? */ 194113895Sphk error = g_access_rel(cp, 1, 1, 1); 195113895Sphk if (error) 196113895Sphk return (error); 197113895Sphk error = g_call_me(g_sunlabel_callconfig, &h0h0, gp, NULL); 198113895Sphk if (!error) { 199113895Sphk g_topology_unlock(); 200113895Sphk do 201113895Sphk tsleep(&h0h0, PRIBIO, "g_sunlabel_config", hz); 202113895Sphk while (h0h0.error == -1); 203113895Sphk g_topology_lock(); 204113895Sphk error = h0h0.error; 205113895Sphk } 206113895Sphk g_access_rel(cp, -1, -1, -1); 207113895Sphk g_free(label); 208113895Sphk } else if (!strcmp(verb, "write bootcode")) { 209113895Sphk label = gctl_get_paraml(req, "bootcode", SUN_BOOTSIZE); 210113895Sphk if (label == NULL) 211113895Sphk return (EINVAL); 212113895Sphk /* XXX: Does this reference register with our selfdestruct code ? */ 213113895Sphk error = g_access_rel(cp, 1, 1, 1); 214113895Sphk if (error) 215113895Sphk return (error); 216113895Sphk for (i = 0; i < SUN_NPART; i++) { 217113895Sphk if (gsp->slices[i].length <= SUN_BOOTSIZE) 218113895Sphk continue; 219113895Sphk g_write_data(cp, 220113895Sphk gsp->slices[i].offset + SUN_SIZE, label + SUN_SIZE, 221113895Sphk SUN_BOOTSIZE - SUN_SIZE); 222113895Sphk } 223113895Sphk g_access_rel(cp, -1, -1, -1); 224113895Sphk g_free(label); 225113895Sphk } else { 226113895Sphk return (gctl_error(req, "Unknown verb parameter")); 227113895Sphk } 228113895Sphk 229113895Sphk return (error); 230113895Sphk} 231113895Sphk 23292372Sphkstatic struct g_geom * 23393250Sphkg_sunlabel_taste(struct g_class *mp, struct g_provider *pp, int flags) 23492372Sphk{ 23592372Sphk struct g_geom *gp; 23692372Sphk struct g_consumer *cp; 237113819Sphk int error, npart; 23892372Sphk u_char *buf; 23992372Sphk struct g_sunlabel_softc *ms; 24092372Sphk off_t mediasize; 24194287Sphk struct g_slicer *gsp; 24292372Sphk 24392372Sphk g_trace(G_T_TOPOLOGY, "g_sunlabel_taste(%s,%s)", mp->name, pp->name); 24492372Sphk g_topology_assert(); 24592372Sphk if (flags == G_TF_NORMAL && 24693358Sphk !strcmp(pp->geom->class->name, SUNLABEL_CLASS_NAME)) 24792372Sphk return (NULL); 248113879Sphk gp = g_slice_new(mp, 8, pp, &cp, &ms, sizeof *ms, NULL); 24992372Sphk if (gp == NULL) 25092372Sphk return (NULL); 25194287Sphk gsp = gp->softc; 25292372Sphk gp->dumpconf = g_sunlabel_dumpconf; 25392372Sphk npart = 0; 254113285Sphk do { 25592372Sphk if (gp->rank != 2 && flags == G_TF_NORMAL) 25692372Sphk break; 257110183Sphk ms->sectorsize = cp->provider->sectorsize; 258110183Sphk if (ms->sectorsize < 512) 259105551Sphk break; 260105551Sphk mediasize = cp->provider->mediasize; 261113880Sphk g_topology_unlock(); 262110183Sphk buf = g_read_data(cp, 0, ms->sectorsize, &error); 263113880Sphk g_topology_lock(); 26492372Sphk if (buf == NULL || error != 0) 26592372Sphk break; 266110183Sphk 267110183Sphk g_sunlabel_modify(gp, ms, buf); 26892372Sphk 26992372Sphk break; 270110183Sphk } while (0); 271107956Sphk g_access_rel(cp, -1, 0, 0); 272107956Sphk if (LIST_EMPTY(&gp->provider)) { 273107956Sphk g_std_spoiled(cp); 274107956Sphk return (NULL); 275110183Sphk } else { 276113821Sphk g_slice_conf_hot(gp, 0, 0, SUN_SIZE, 277113821Sphk G_SLICE_HOT_ALLOW, G_SLICE_HOT_DENY, G_SLICE_HOT_CALL); 278113821Sphk gsp->hot = g_sunlabel_hotwrite; 279107956Sphk } 280107956Sphk return (gp); 28192372Sphk} 28292372Sphk 28393248Sphkstatic struct g_class g_sunlabel_class = { 284112552Sphk .name = SUNLABEL_CLASS_NAME, 285112552Sphk .taste = g_sunlabel_taste, 286113895Sphk .config_geom = g_sunlabel_config, 28798066Sphk G_CLASS_INITIALIZER 28892372Sphk}; 28992372Sphk 29093248SphkDECLARE_GEOM_CLASS(g_sunlabel_class, g_sunlabel); 291