geom_sunlabel.c revision 114459
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 114459 2003-05-01 21:07:08Z 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}
168113895Sphk
169113895Sphk/*
170113895Sphk * NB! curthread is user process which GCTL'ed.
171113895Sphk */
172113895Sphkstatic int
173113895Sphkg_sunlabel_config(struct gctl_req *req, struct g_geom *gp, const char *verb)
174113895Sphk{
175113895Sphk	u_char *label;
176113895Sphk	int error, i;
177113895Sphk	struct h0h0 h0h0;
178113895Sphk	struct g_slicer *gsp;
179113895Sphk	struct g_consumer *cp;
180113895Sphk
181113895Sphk	g_topology_assert();
182113895Sphk	cp = LIST_FIRST(&gp->consumer);
183113895Sphk	gsp = gp->softc;
184113895Sphk	if (!strcmp(verb, "write label")) {
185113895Sphk		label = gctl_get_paraml(req, "label", SUN_SIZE);
186113895Sphk		if (label == NULL)
187113895Sphk			return (EINVAL);
188113895Sphk		h0h0.gp = gp;
189113895Sphk		h0h0.ms = gsp->softc;
190113895Sphk		h0h0.label = label;
191113895Sphk		h0h0.error = -1;
192113895Sphk		/* XXX: Does this reference register with our selfdestruct code ? */
193113895Sphk		error = g_access_rel(cp, 1, 1, 1);
194114459Sphk		if (error) {
195114459Sphk			g_free(label);
196113895Sphk			return (error);
197114459Sphk		}
198113937Sphk		g_topology_unlock();
199113940Sphk		g_waitfor_event(g_sunlabel_callconfig, &h0h0, M_WAITOK, gp, NULL);
200113937Sphk		g_topology_lock();
201113937Sphk		error = h0h0.error;
202113895Sphk		g_access_rel(cp, -1, -1, -1);
203113895Sphk		g_free(label);
204113895Sphk	} else if (!strcmp(verb, "write bootcode")) {
205113895Sphk		label = gctl_get_paraml(req, "bootcode", SUN_BOOTSIZE);
206113895Sphk		if (label == NULL)
207113895Sphk			return (EINVAL);
208113895Sphk		/* XXX: Does this reference register with our selfdestruct code ? */
209113895Sphk		error = g_access_rel(cp, 1, 1, 1);
210114459Sphk		if (error) {
211114459Sphk			g_free(label);
212113895Sphk			return (error);
213114459Sphk		}
214113895Sphk		for (i = 0; i < SUN_NPART; i++) {
215113895Sphk			if (gsp->slices[i].length <= SUN_BOOTSIZE)
216113895Sphk				continue;
217113895Sphk			g_write_data(cp,
218113895Sphk			    gsp->slices[i].offset + SUN_SIZE, label + SUN_SIZE,
219113895Sphk			    SUN_BOOTSIZE - SUN_SIZE);
220113895Sphk		}
221113895Sphk		g_access_rel(cp, -1, -1, -1);
222113895Sphk		g_free(label);
223113895Sphk	} else {
224113895Sphk		return (gctl_error(req, "Unknown verb parameter"));
225113895Sphk	}
226113895Sphk
227113895Sphk	return (error);
228113895Sphk}
229113895Sphk
23092372Sphkstatic struct g_geom *
23193250Sphkg_sunlabel_taste(struct g_class *mp, struct g_provider *pp, int flags)
23292372Sphk{
23392372Sphk	struct g_geom *gp;
23492372Sphk	struct g_consumer *cp;
235113819Sphk	int error, npart;
23692372Sphk	u_char *buf;
23792372Sphk	struct g_sunlabel_softc *ms;
23892372Sphk	off_t mediasize;
23994287Sphk	struct g_slicer *gsp;
24092372Sphk
24192372Sphk	g_trace(G_T_TOPOLOGY, "g_sunlabel_taste(%s,%s)", mp->name, pp->name);
24292372Sphk	g_topology_assert();
24392372Sphk	if (flags == G_TF_NORMAL &&
24493358Sphk	    !strcmp(pp->geom->class->name, SUNLABEL_CLASS_NAME))
24592372Sphk		return (NULL);
246113879Sphk	gp = g_slice_new(mp, 8, pp, &cp, &ms, sizeof *ms, NULL);
24792372Sphk	if (gp == NULL)
24892372Sphk		return (NULL);
24994287Sphk	gsp = gp->softc;
25092372Sphk	gp->dumpconf = g_sunlabel_dumpconf;
25192372Sphk	npart = 0;
252113285Sphk	do {
25392372Sphk		if (gp->rank != 2 && flags == G_TF_NORMAL)
25492372Sphk			break;
255110183Sphk		ms->sectorsize = cp->provider->sectorsize;
256110183Sphk		if (ms->sectorsize < 512)
257105551Sphk			break;
258105551Sphk		mediasize = cp->provider->mediasize;
259113880Sphk		g_topology_unlock();
260110183Sphk		buf = g_read_data(cp, 0, ms->sectorsize, &error);
261113880Sphk		g_topology_lock();
26292372Sphk		if (buf == NULL || error != 0)
26392372Sphk			break;
264110183Sphk
265110183Sphk		g_sunlabel_modify(gp, ms, buf);
266114459Sphk		g_free(buf);
26792372Sphk
26892372Sphk		break;
269110183Sphk	} while (0);
270107956Sphk	g_access_rel(cp, -1, 0, 0);
271107956Sphk	if (LIST_EMPTY(&gp->provider)) {
272107956Sphk		g_std_spoiled(cp);
273107956Sphk		return (NULL);
274110183Sphk	} else {
275113821Sphk		g_slice_conf_hot(gp, 0, 0, SUN_SIZE,
276113821Sphk		    G_SLICE_HOT_ALLOW, G_SLICE_HOT_DENY, G_SLICE_HOT_CALL);
277113821Sphk		gsp->hot = g_sunlabel_hotwrite;
278107956Sphk	}
279107956Sphk	return (gp);
28092372Sphk}
28192372Sphk
28293248Sphkstatic struct g_class g_sunlabel_class = {
283112552Sphk	.name = SUNLABEL_CLASS_NAME,
284112552Sphk	.taste = g_sunlabel_taste,
285113895Sphk	.config_geom = g_sunlabel_config,
28698066Sphk	G_CLASS_INITIALIZER
28792372Sphk};
28892372Sphk
28993248SphkDECLARE_GEOM_CLASS(g_sunlabel_class, g_sunlabel);
290