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 36116196Sobrien#include <sys/cdefs.h> 37116196Sobrien__FBSDID("$FreeBSD: releng/10.3/sys/geom/geom_sunlabel.c 223921 2011-07-11 05:22:31Z ae $"); 3892372Sphk 3992372Sphk#include <sys/param.h> 40113011Sphk#include <sys/endian.h> 4192372Sphk#include <sys/systm.h> 42219029Snetchild#include <sys/sysctl.h> 4392372Sphk#include <sys/kernel.h> 4492372Sphk#include <sys/conf.h> 4592372Sphk#include <sys/bio.h> 4692372Sphk#include <sys/malloc.h> 4792372Sphk#include <sys/lock.h> 4892372Sphk#include <sys/mutex.h> 49144934Spjd#include <sys/md5.h> 50223921Sae#include <sys/sbuf.h> 51113819Sphk#include <sys/sun_disklabel.h> 5292372Sphk#include <geom/geom.h> 5392372Sphk#include <geom/geom_slice.h> 5492372Sphk#include <machine/endian.h> 5592372Sphk 56219029SnetchildFEATURE(geom_sunlabel, "GEOM Sun/Solaris partitioning support"); 57219029Snetchild 5897075Sphk#define SUNLABEL_CLASS_NAME "SUN" 5992372Sphk 6092372Sphkstruct g_sunlabel_softc { 61110183Sphk int sectorsize; 62106100Sphk int nheads; 63106100Sphk int nsects; 64106100Sphk int nalt; 65144934Spjd u_char labelsum[16]; 6692372Sphk}; 6792372Sphk 6892372Sphkstatic int 69110183Sphkg_sunlabel_modify(struct g_geom *gp, struct g_sunlabel_softc *ms, u_char *sec0) 70110183Sphk{ 71110183Sphk int i, error; 72110183Sphk u_int u, v, csize; 73113819Sphk struct sun_disklabel sl; 74144934Spjd MD5_CTX md5sum; 75110183Sphk 76113819Sphk error = sunlabel_dec(sec0, &sl); 77113819Sphk if (error) 78113819Sphk return (error); 79110183Sphk 80113819Sphk csize = sl.sl_ntracks * sl.sl_nsectors; 81110183Sphk 82113819Sphk for (i = 0; i < SUN_NPART; i++) { 83113819Sphk v = sl.sl_part[i].sdkp_cyloffset; 84113819Sphk u = sl.sl_part[i].sdkp_nsectors; 85110183Sphk error = g_slice_config(gp, i, G_SLICE_CONFIG_CHECK, 86110183Sphk ((off_t)v * csize) << 9ULL, 87110183Sphk ((off_t)u) << 9ULL, 88110183Sphk ms->sectorsize, 89110183Sphk "%s%c", gp->name, 'a' + i); 90110183Sphk if (error) 91110183Sphk return (error); 92110183Sphk } 93113819Sphk for (i = 0; i < SUN_NPART; i++) { 94113819Sphk v = sl.sl_part[i].sdkp_cyloffset; 95113819Sphk u = sl.sl_part[i].sdkp_nsectors; 96110183Sphk g_slice_config(gp, i, G_SLICE_CONFIG_SET, 97110183Sphk ((off_t)v * csize) << 9ULL, 98110183Sphk ((off_t)u) << 9ULL, 99110183Sphk ms->sectorsize, 100110183Sphk "%s%c", gp->name, 'a' + i); 101110183Sphk } 102113819Sphk ms->nalt = sl.sl_acylinders; 103113819Sphk ms->nheads = sl.sl_ntracks; 104113819Sphk ms->nsects = sl.sl_nsectors; 105110183Sphk 106144934Spjd /* 107144934Spjd * Calculate MD5 from the first sector and use it for avoiding 108144934Spjd * recursive labels creation. 109144934Spjd */ 110144934Spjd MD5Init(&md5sum); 111144934Spjd MD5Update(&md5sum, sec0, ms->sectorsize); 112144934Spjd MD5Final(ms->labelsum, &md5sum); 113144934Spjd 114110183Sphk return (0); 115110183Sphk} 116110183Sphk 117113821Sphkstatic void 118113821Sphkg_sunlabel_hotwrite(void *arg, int flag) 119113821Sphk{ 120113821Sphk struct bio *bp; 121113821Sphk struct g_geom *gp; 122113821Sphk struct g_slicer *gsp; 123113821Sphk struct g_slice *gsl; 124113821Sphk struct g_sunlabel_softc *ms; 125113821Sphk u_char *p; 126113821Sphk int error; 127113821Sphk 128113821Sphk KASSERT(flag != EV_CANCEL, ("g_sunlabel_hotwrite cancelled")); 129113821Sphk bp = arg; 130113821Sphk gp = bp->bio_to->geom; 131113821Sphk gsp = gp->softc; 132113821Sphk ms = gsp->softc; 133113821Sphk gsl = &gsp->slices[bp->bio_to->index]; 134113821Sphk /* 135113821Sphk * XXX: For all practical purposes, this whould be equvivalent to 136113821Sphk * XXX: "p = (u_char *)bp->bio_data;" because the label is always 137113821Sphk * XXX: in the first sector and we refuse sectors smaller than the 138113821Sphk * XXX: label. 139113821Sphk */ 140113821Sphk p = (u_char *)bp->bio_data - (bp->bio_offset + gsl->offset); 141113821Sphk 142113821Sphk error = g_sunlabel_modify(gp, ms, p); 143113821Sphk if (error) { 144113821Sphk g_io_deliver(bp, EPERM); 145113821Sphk return; 146113821Sphk } 147113821Sphk g_slice_finish_hot(bp); 148113821Sphk} 149113821Sphk 15092372Sphkstatic void 151107953Sphkg_sunlabel_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp) 15292372Sphk{ 153106100Sphk struct g_slicer *gsp; 154106100Sphk struct g_sunlabel_softc *ms; 15592372Sphk 156106100Sphk gsp = gp->softc; 157106100Sphk ms = gsp->softc; 15892372Sphk g_slice_dumpconf(sb, indent, gp, cp, pp); 159106100Sphk if (indent == NULL) { 160106100Sphk sbuf_printf(sb, " sc %u hd %u alt %u", 161106100Sphk ms->nsects, ms->nheads, ms->nalt); 162106100Sphk } 16392372Sphk} 16492372Sphk 165115512Sphkstruct g_hh01 { 166113895Sphk struct g_geom *gp; 167113895Sphk struct g_sunlabel_softc *ms; 168113895Sphk u_char *label; 169113895Sphk int error; 170113895Sphk}; 171113895Sphk 172113895Sphkstatic void 173113895Sphkg_sunlabel_callconfig(void *arg, int flag) 174113895Sphk{ 175115512Sphk struct g_hh01 *hp; 176113895Sphk 177113895Sphk hp = arg; 178113895Sphk hp->error = g_sunlabel_modify(hp->gp, hp->ms, hp->label); 179113895Sphk if (!hp->error) 180113895Sphk hp->error = g_write_data(LIST_FIRST(&hp->gp->consumer), 181113895Sphk 0, hp->label, SUN_SIZE); 182113895Sphk} 183113895Sphk 184113895Sphk/* 185113895Sphk * NB! curthread is user process which GCTL'ed. 186113895Sphk */ 187115624Sphkstatic void 188115624Sphkg_sunlabel_config(struct gctl_req *req, struct g_class *mp, const char *verb) 189113895Sphk{ 190113895Sphk u_char *label; 191113895Sphk int error, i; 192115512Sphk struct g_hh01 h0h0; 193113895Sphk struct g_slicer *gsp; 194115624Sphk struct g_geom *gp; 195113895Sphk struct g_consumer *cp; 196113895Sphk 197113895Sphk g_topology_assert(); 198115624Sphk gp = gctl_get_geom(req, mp, "geom"); 199115624Sphk if (gp == NULL) 200115624Sphk return; 201113895Sphk cp = LIST_FIRST(&gp->consumer); 202113895Sphk gsp = gp->softc; 203113895Sphk if (!strcmp(verb, "write label")) { 204113895Sphk label = gctl_get_paraml(req, "label", SUN_SIZE); 205113895Sphk if (label == NULL) 206115624Sphk return; 207113895Sphk h0h0.gp = gp; 208113895Sphk h0h0.ms = gsp->softc; 209113895Sphk h0h0.label = label; 210113895Sphk h0h0.error = -1; 211113895Sphk /* XXX: Does this reference register with our selfdestruct code ? */ 212125755Sphk error = g_access(cp, 1, 1, 1); 213114459Sphk if (error) { 214115624Sphk gctl_error(req, "could not access consumer"); 215115624Sphk return; 216114459Sphk } 217115624Sphk g_sunlabel_callconfig(&h0h0, 0); 218125755Sphk g_access(cp, -1, -1, -1); 219113895Sphk } else if (!strcmp(verb, "write bootcode")) { 220113895Sphk label = gctl_get_paraml(req, "bootcode", SUN_BOOTSIZE); 221113895Sphk if (label == NULL) 222115624Sphk return; 223113895Sphk /* XXX: Does this reference register with our selfdestruct code ? */ 224125755Sphk error = g_access(cp, 1, 1, 1); 225114459Sphk if (error) { 226115624Sphk gctl_error(req, "could not access consumer"); 227115624Sphk return; 228114459Sphk } 229113895Sphk for (i = 0; i < SUN_NPART; i++) { 230113895Sphk if (gsp->slices[i].length <= SUN_BOOTSIZE) 231113895Sphk continue; 232113895Sphk g_write_data(cp, 233113895Sphk gsp->slices[i].offset + SUN_SIZE, label + SUN_SIZE, 234113895Sphk SUN_BOOTSIZE - SUN_SIZE); 235113895Sphk } 236125755Sphk g_access(cp, -1, -1, -1); 237113895Sphk } else { 238115624Sphk gctl_error(req, "Unknown verb parameter"); 239113895Sphk } 240113895Sphk} 241113895Sphk 242144934Spjdstatic int 243144934Spjdg_sunlabel_start(struct bio *bp) 244144934Spjd{ 245144934Spjd struct g_sunlabel_softc *mp; 246144934Spjd struct g_slicer *gsp; 247144934Spjd 248144934Spjd gsp = bp->bio_to->geom->softc; 249144934Spjd mp = gsp->softc; 250144934Spjd if (bp->bio_cmd == BIO_GETATTR) { 251144934Spjd if (g_handleattr(bp, "SUN::labelsum", mp->labelsum, 252144934Spjd sizeof(mp->labelsum))) 253144934Spjd return (1); 254144934Spjd } 255144934Spjd return (0); 256144934Spjd} 257144934Spjd 25892372Sphkstatic struct g_geom * 25993250Sphkg_sunlabel_taste(struct g_class *mp, struct g_provider *pp, int flags) 26092372Sphk{ 26192372Sphk struct g_geom *gp; 26292372Sphk struct g_consumer *cp; 26392372Sphk struct g_sunlabel_softc *ms; 26494287Sphk struct g_slicer *gsp; 265144934Spjd u_char *buf, hash[16]; 266144934Spjd MD5_CTX md5sum; 267144934Spjd int error; 26892372Sphk 26992372Sphk g_trace(G_T_TOPOLOGY, "g_sunlabel_taste(%s,%s)", mp->name, pp->name); 27092372Sphk g_topology_assert(); 27192372Sphk if (flags == G_TF_NORMAL && 27293358Sphk !strcmp(pp->geom->class->name, SUNLABEL_CLASS_NAME)) 27392372Sphk return (NULL); 274144934Spjd gp = g_slice_new(mp, 8, pp, &cp, &ms, sizeof *ms, g_sunlabel_start); 27592372Sphk if (gp == NULL) 27692372Sphk return (NULL); 27794287Sphk gsp = gp->softc; 278113285Sphk do { 279110183Sphk ms->sectorsize = cp->provider->sectorsize; 280110183Sphk if (ms->sectorsize < 512) 281105551Sphk break; 282113880Sphk g_topology_unlock(); 283152971Ssobomax buf = g_read_data(cp, 0, ms->sectorsize, NULL); 284113880Sphk g_topology_lock(); 285152967Ssobomax if (buf == NULL) 28692372Sphk break; 287144934Spjd 288144934Spjd /* 289144934Spjd * Calculate MD5 from the first sector and use it for avoiding 290144934Spjd * recursive labels creation. 291144934Spjd */ 292144934Spjd MD5Init(&md5sum); 293144934Spjd MD5Update(&md5sum, buf, ms->sectorsize); 294144934Spjd MD5Final(ms->labelsum, &md5sum); 295144934Spjd 296144934Spjd error = g_getattr("SUN::labelsum", cp, &hash); 297144934Spjd if (!error && !bcmp(ms->labelsum, hash, sizeof(hash))) { 298144934Spjd g_free(buf); 299144934Spjd break; 300144934Spjd } 301144934Spjd 302110183Sphk g_sunlabel_modify(gp, ms, buf); 303114459Sphk g_free(buf); 30492372Sphk 30592372Sphk break; 306110183Sphk } while (0); 307125755Sphk g_access(cp, -1, 0, 0); 308107956Sphk if (LIST_EMPTY(&gp->provider)) { 309114505Sphk g_slice_spoiled(cp); 310107956Sphk return (NULL); 311107956Sphk } 312114533Sphk g_slice_conf_hot(gp, 0, 0, SUN_SIZE, 313114533Sphk G_SLICE_HOT_ALLOW, G_SLICE_HOT_DENY, G_SLICE_HOT_CALL); 314114533Sphk gsp->hot = g_sunlabel_hotwrite; 315107956Sphk return (gp); 31692372Sphk} 31792372Sphk 31893248Sphkstatic struct g_class g_sunlabel_class = { 319112552Sphk .name = SUNLABEL_CLASS_NAME, 320133318Sphk .version = G_VERSION, 321112552Sphk .taste = g_sunlabel_taste, 322115624Sphk .ctlreq = g_sunlabel_config, 323133314Sphk .dumpconf = g_sunlabel_dumpconf, 32492372Sphk}; 32592372Sphk 32693248SphkDECLARE_GEOM_CLASS(g_sunlabel_class, g_sunlabel); 327