geom_sunlabel.c revision 133314
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: head/sys/geom/geom_sunlabel.c 133314 2004-08-08 06:49:07Z phk $");
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
150115512Sphkstruct g_hh01 {
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{
160115512Sphk	struct g_hh01 *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 */
172115624Sphkstatic void
173115624Sphkg_sunlabel_config(struct gctl_req *req, struct g_class *mp, const char *verb)
174113895Sphk{
175113895Sphk	u_char *label;
176113895Sphk	int error, i;
177115512Sphk	struct g_hh01 h0h0;
178113895Sphk	struct g_slicer *gsp;
179115624Sphk	struct g_geom *gp;
180113895Sphk	struct g_consumer *cp;
181113895Sphk
182113895Sphk	g_topology_assert();
183115624Sphk	gp = gctl_get_geom(req, mp, "geom");
184115624Sphk	if (gp == NULL)
185115624Sphk		return;
186113895Sphk	cp = LIST_FIRST(&gp->consumer);
187113895Sphk	gsp = gp->softc;
188113895Sphk	if (!strcmp(verb, "write label")) {
189113895Sphk		label = gctl_get_paraml(req, "label", SUN_SIZE);
190113895Sphk		if (label == NULL)
191115624Sphk			return;
192113895Sphk		h0h0.gp = gp;
193113895Sphk		h0h0.ms = gsp->softc;
194113895Sphk		h0h0.label = label;
195113895Sphk		h0h0.error = -1;
196113895Sphk		/* XXX: Does this reference register with our selfdestruct code ? */
197125755Sphk		error = g_access(cp, 1, 1, 1);
198114459Sphk		if (error) {
199115624Sphk			gctl_error(req, "could not access consumer");
200115624Sphk			return;
201114459Sphk		}
202115624Sphk		g_sunlabel_callconfig(&h0h0, 0);
203125755Sphk		g_access(cp, -1, -1, -1);
204113895Sphk	} else if (!strcmp(verb, "write bootcode")) {
205113895Sphk		label = gctl_get_paraml(req, "bootcode", SUN_BOOTSIZE);
206113895Sphk		if (label == NULL)
207115624Sphk			return;
208113895Sphk		/* XXX: Does this reference register with our selfdestruct code ? */
209125755Sphk		error = g_access(cp, 1, 1, 1);
210114459Sphk		if (error) {
211115624Sphk			gctl_error(req, "could not access consumer");
212115624Sphk			return;
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		}
221125755Sphk		g_access(cp, -1, -1, -1);
222113895Sphk	} else {
223115624Sphk		gctl_error(req, "Unknown verb parameter");
224113895Sphk	}
225113895Sphk}
226113895Sphk
22792372Sphkstatic struct g_geom *
22893250Sphkg_sunlabel_taste(struct g_class *mp, struct g_provider *pp, int flags)
22992372Sphk{
23092372Sphk	struct g_geom *gp;
23192372Sphk	struct g_consumer *cp;
232115512Sphk	int error;
23392372Sphk	u_char *buf;
23492372Sphk	struct g_sunlabel_softc *ms;
23594287Sphk	struct g_slicer *gsp;
23692372Sphk
23792372Sphk	g_trace(G_T_TOPOLOGY, "g_sunlabel_taste(%s,%s)", mp->name, pp->name);
23892372Sphk	g_topology_assert();
23992372Sphk	if (flags == G_TF_NORMAL &&
24093358Sphk	    !strcmp(pp->geom->class->name, SUNLABEL_CLASS_NAME))
24192372Sphk		return (NULL);
242113879Sphk	gp = g_slice_new(mp, 8, pp, &cp, &ms, sizeof *ms, NULL);
24392372Sphk	if (gp == NULL)
24492372Sphk		return (NULL);
24594287Sphk	gsp = gp->softc;
246113285Sphk	do {
24792372Sphk		if (gp->rank != 2 && flags == G_TF_NORMAL)
24892372Sphk			break;
249110183Sphk		ms->sectorsize = cp->provider->sectorsize;
250110183Sphk		if (ms->sectorsize < 512)
251105551Sphk			break;
252113880Sphk		g_topology_unlock();
253110183Sphk		buf = g_read_data(cp, 0, ms->sectorsize, &error);
254113880Sphk		g_topology_lock();
25592372Sphk		if (buf == NULL || error != 0)
25692372Sphk			break;
257110183Sphk
258110183Sphk		g_sunlabel_modify(gp, ms, buf);
259114459Sphk		g_free(buf);
26092372Sphk
26192372Sphk		break;
262110183Sphk	} while (0);
263125755Sphk	g_access(cp, -1, 0, 0);
264107956Sphk	if (LIST_EMPTY(&gp->provider)) {
265114505Sphk		g_slice_spoiled(cp);
266107956Sphk		return (NULL);
267107956Sphk	}
268114533Sphk	g_slice_conf_hot(gp, 0, 0, SUN_SIZE,
269114533Sphk	    G_SLICE_HOT_ALLOW, G_SLICE_HOT_DENY, G_SLICE_HOT_CALL);
270114533Sphk	gsp->hot = g_sunlabel_hotwrite;
271107956Sphk	return (gp);
27292372Sphk}
27392372Sphk
27493248Sphkstatic struct g_class g_sunlabel_class = {
275112552Sphk	.name = SUNLABEL_CLASS_NAME,
276112552Sphk	.taste = g_sunlabel_taste,
277115624Sphk	.ctlreq = g_sunlabel_config,
278133314Sphk	.dumpconf = g_sunlabel_dumpconf,
27992372Sphk};
28092372Sphk
28193248SphkDECLARE_GEOM_CLASS(g_sunlabel_class, g_sunlabel);
282