2 * Copyright (c) 2002 Poul-Henning Kamp
3 * Copyright (c) 2002 Networks Associates Technology, Inc.
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.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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
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
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
39 #include <sys/param.h>
40 #include <sys/endian.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
45 #include <sys/malloc.h>
47 #include <sys/mutex.h>
49 #include <sys/sun_disklabel.h>
50 #include <geom/geom.h>
51 #include <geom/geom_slice.h>
52 #include <machine/endian.h>
54 #define SUNLABEL_CLASS_NAME "SUN"
56 struct g_sunlabel_softc {
65 g_sunlabel_modify(struct g_geom *gp, struct g_sunlabel_softc *ms, u_char *sec0)
69 struct sun_disklabel sl;
72 error = sunlabel_dec(sec0, &sl);
76 csize = sl.sl_ntracks * sl.sl_nsectors;
78 for (i = 0; i < SUN_NPART; i++) {
79 v = sl.sl_part[i].sdkp_cyloffset;
80 u = sl.sl_part[i].sdkp_nsectors;
81 error = g_slice_config(gp, i, G_SLICE_CONFIG_CHECK,
82 ((off_t)v * csize) << 9ULL,
85 "%s%c", gp->name, 'a' + i);
89 for (i = 0; i < SUN_NPART; i++) {
90 v = sl.sl_part[i].sdkp_cyloffset;
91 u = sl.sl_part[i].sdkp_nsectors;
92 g_slice_config(gp, i, G_SLICE_CONFIG_SET,
93 ((off_t)v * csize) << 9ULL,
96 "%s%c", gp->name, 'a' + i);
98 ms->nalt = sl.sl_acylinders;
99 ms->nheads = sl.sl_ntracks;
100 ms->nsects = sl.sl_nsectors;
103 * Calculate MD5 from the first sector and use it for avoiding
104 * recursive labels creation.
107 MD5Update(&md5sum, sec0, ms->sectorsize);
108 MD5Final(ms->labelsum, &md5sum);
114 g_sunlabel_hotwrite(void *arg, int flag)
118 struct g_slicer *gsp;
120 struct g_sunlabel_softc *ms;
124 KASSERT(flag != EV_CANCEL, ("g_sunlabel_hotwrite cancelled"));
126 gp = bp->bio_to->geom;
129 gsl = &gsp->slices[bp->bio_to->index];
131 * XXX: For all practical purposes, this whould be equvivalent to
132 * XXX: "p = (u_char *)bp->bio_data;" because the label is always
133 * XXX: in the first sector and we refuse sectors smaller than the
136 p = (u_char *)bp->bio_data - (bp->bio_offset + gsl->offset);
138 error = g_sunlabel_modify(gp, ms, p);
140 g_io_deliver(bp, EPERM);
143 g_slice_finish_hot(bp);
147 g_sunlabel_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp)
149 struct g_slicer *gsp;
150 struct g_sunlabel_softc *ms;
154 g_slice_dumpconf(sb, indent, gp, cp, pp);
155 if (indent == NULL) {
156 sbuf_printf(sb, " sc %u hd %u alt %u",
157 ms->nsects, ms->nheads, ms->nalt);
163 struct g_sunlabel_softc *ms;
169 g_sunlabel_callconfig(void *arg, int flag)
174 hp->error = g_sunlabel_modify(hp->gp, hp->ms, hp->label);
176 hp->error = g_write_data(LIST_FIRST(&hp->gp->consumer),
177 0, hp->label, SUN_SIZE);
181 * NB! curthread is user process which GCTL'ed.
184 g_sunlabel_config(struct gctl_req *req, struct g_class *mp, const char *verb)
189 struct g_slicer *gsp;
191 struct g_consumer *cp;
194 gp = gctl_get_geom(req, mp, "geom");
197 cp = LIST_FIRST(&gp->consumer);
199 if (!strcmp(verb, "write label")) {
200 label = gctl_get_paraml(req, "label", SUN_SIZE);
204 h0h0.ms = gsp->softc;
207 /* XXX: Does this reference register with our selfdestruct code ? */
208 error = g_access(cp, 1, 1, 1);
210 gctl_error(req, "could not access consumer");
213 g_sunlabel_callconfig(&h0h0, 0);
214 g_access(cp, -1, -1, -1);
215 } else if (!strcmp(verb, "write bootcode")) {
216 label = gctl_get_paraml(req, "bootcode", SUN_BOOTSIZE);
219 /* XXX: Does this reference register with our selfdestruct code ? */
220 error = g_access(cp, 1, 1, 1);
222 gctl_error(req, "could not access consumer");
225 for (i = 0; i < SUN_NPART; i++) {
226 if (gsp->slices[i].length <= SUN_BOOTSIZE)
229 gsp->slices[i].offset + SUN_SIZE, label + SUN_SIZE,
230 SUN_BOOTSIZE - SUN_SIZE);
232 g_access(cp, -1, -1, -1);
234 gctl_error(req, "Unknown verb parameter");
239 g_sunlabel_start(struct bio *bp)
241 struct g_sunlabel_softc *mp;
242 struct g_slicer *gsp;
244 gsp = bp->bio_to->geom->softc;
246 if (bp->bio_cmd == BIO_GETATTR) {
247 if (g_handleattr(bp, "SUN::labelsum", mp->labelsum,
248 sizeof(mp->labelsum)))
254 static struct g_geom *
255 g_sunlabel_taste(struct g_class *mp, struct g_provider *pp, int flags)
258 struct g_consumer *cp;
259 struct g_sunlabel_softc *ms;
260 struct g_slicer *gsp;
261 u_char *buf, hash[16];
265 g_trace(G_T_TOPOLOGY, "g_sunlabel_taste(%s,%s)", mp->name, pp->name);
267 if (flags == G_TF_NORMAL &&
268 !strcmp(pp->geom->class->name, SUNLABEL_CLASS_NAME))
270 gp = g_slice_new(mp, 8, pp, &cp, &ms, sizeof *ms, g_sunlabel_start);
275 ms->sectorsize = cp->provider->sectorsize;
276 if (ms->sectorsize < 512)
279 buf = g_read_data(cp, 0, ms->sectorsize, NULL);
285 * Calculate MD5 from the first sector and use it for avoiding
286 * recursive labels creation.
289 MD5Update(&md5sum, buf, ms->sectorsize);
290 MD5Final(ms->labelsum, &md5sum);
292 error = g_getattr("SUN::labelsum", cp, &hash);
293 if (!error && !bcmp(ms->labelsum, hash, sizeof(hash))) {
298 g_sunlabel_modify(gp, ms, buf);
303 g_access(cp, -1, 0, 0);
304 if (LIST_EMPTY(&gp->provider)) {
308 g_slice_conf_hot(gp, 0, 0, SUN_SIZE,
309 G_SLICE_HOT_ALLOW, G_SLICE_HOT_DENY, G_SLICE_HOT_CALL);
310 gsp->hot = g_sunlabel_hotwrite;
314 static struct g_class g_sunlabel_class = {
315 .name = SUNLABEL_CLASS_NAME,
316 .version = G_VERSION,
317 .taste = g_sunlabel_taste,
318 .ctlreq = g_sunlabel_config,
319 .dumpconf = g_sunlabel_dumpconf,
322 DECLARE_GEOM_CLASS(g_sunlabel_class, g_sunlabel);