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/sysctl.h>
43 #include <sys/kernel.h>
46 #include <sys/malloc.h>
48 #include <sys/mutex.h>
51 #include <sys/sun_disklabel.h>
52 #include <geom/geom.h>
53 #include <geom/geom_slice.h>
54 #include <machine/endian.h>
56 FEATURE(geom_sunlabel, "GEOM Sun/Solaris partitioning support");
58 #define SUNLABEL_CLASS_NAME "SUN"
60 struct g_sunlabel_softc {
69 g_sunlabel_modify(struct g_geom *gp, struct g_sunlabel_softc *ms, u_char *sec0)
73 struct sun_disklabel sl;
76 error = sunlabel_dec(sec0, &sl);
80 csize = sl.sl_ntracks * sl.sl_nsectors;
82 for (i = 0; i < SUN_NPART; i++) {
83 v = sl.sl_part[i].sdkp_cyloffset;
84 u = sl.sl_part[i].sdkp_nsectors;
85 error = g_slice_config(gp, i, G_SLICE_CONFIG_CHECK,
86 ((off_t)v * csize) << 9ULL,
89 "%s%c", gp->name, 'a' + i);
93 for (i = 0; i < SUN_NPART; i++) {
94 v = sl.sl_part[i].sdkp_cyloffset;
95 u = sl.sl_part[i].sdkp_nsectors;
96 g_slice_config(gp, i, G_SLICE_CONFIG_SET,
97 ((off_t)v * csize) << 9ULL,
100 "%s%c", gp->name, 'a' + i);
102 ms->nalt = sl.sl_acylinders;
103 ms->nheads = sl.sl_ntracks;
104 ms->nsects = sl.sl_nsectors;
107 * Calculate MD5 from the first sector and use it for avoiding
108 * recursive labels creation.
111 MD5Update(&md5sum, sec0, ms->sectorsize);
112 MD5Final(ms->labelsum, &md5sum);
118 g_sunlabel_hotwrite(void *arg, int flag)
122 struct g_slicer *gsp;
124 struct g_sunlabel_softc *ms;
128 KASSERT(flag != EV_CANCEL, ("g_sunlabel_hotwrite cancelled"));
130 gp = bp->bio_to->geom;
133 gsl = &gsp->slices[bp->bio_to->index];
135 * XXX: For all practical purposes, this whould be equvivalent to
136 * XXX: "p = (u_char *)bp->bio_data;" because the label is always
137 * XXX: in the first sector and we refuse sectors smaller than the
140 p = (u_char *)bp->bio_data - (bp->bio_offset + gsl->offset);
142 error = g_sunlabel_modify(gp, ms, p);
144 g_io_deliver(bp, EPERM);
147 g_slice_finish_hot(bp);
151 g_sunlabel_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp)
153 struct g_slicer *gsp;
154 struct g_sunlabel_softc *ms;
158 g_slice_dumpconf(sb, indent, gp, cp, pp);
159 if (indent == NULL) {
160 sbuf_printf(sb, " sc %u hd %u alt %u",
161 ms->nsects, ms->nheads, ms->nalt);
167 struct g_sunlabel_softc *ms;
173 g_sunlabel_callconfig(void *arg, int flag)
178 hp->error = g_sunlabel_modify(hp->gp, hp->ms, hp->label);
180 hp->error = g_write_data(LIST_FIRST(&hp->gp->consumer),
181 0, hp->label, SUN_SIZE);
185 * NB! curthread is user process which GCTL'ed.
188 g_sunlabel_config(struct gctl_req *req, struct g_class *mp, const char *verb)
193 struct g_slicer *gsp;
195 struct g_consumer *cp;
198 gp = gctl_get_geom(req, mp, "geom");
201 cp = LIST_FIRST(&gp->consumer);
203 if (!strcmp(verb, "write label")) {
204 label = gctl_get_paraml(req, "label", SUN_SIZE);
208 h0h0.ms = gsp->softc;
211 /* XXX: Does this reference register with our selfdestruct code ? */
212 error = g_access(cp, 1, 1, 1);
214 gctl_error(req, "could not access consumer");
217 g_sunlabel_callconfig(&h0h0, 0);
218 g_access(cp, -1, -1, -1);
219 } else if (!strcmp(verb, "write bootcode")) {
220 label = gctl_get_paraml(req, "bootcode", SUN_BOOTSIZE);
223 /* XXX: Does this reference register with our selfdestruct code ? */
224 error = g_access(cp, 1, 1, 1);
226 gctl_error(req, "could not access consumer");
229 for (i = 0; i < SUN_NPART; i++) {
230 if (gsp->slices[i].length <= SUN_BOOTSIZE)
233 gsp->slices[i].offset + SUN_SIZE, label + SUN_SIZE,
234 SUN_BOOTSIZE - SUN_SIZE);
236 g_access(cp, -1, -1, -1);
238 gctl_error(req, "Unknown verb parameter");
243 g_sunlabel_start(struct bio *bp)
245 struct g_sunlabel_softc *mp;
246 struct g_slicer *gsp;
248 gsp = bp->bio_to->geom->softc;
250 if (bp->bio_cmd == BIO_GETATTR) {
251 if (g_handleattr(bp, "SUN::labelsum", mp->labelsum,
252 sizeof(mp->labelsum)))
258 static struct g_geom *
259 g_sunlabel_taste(struct g_class *mp, struct g_provider *pp, int flags)
262 struct g_consumer *cp;
263 struct g_sunlabel_softc *ms;
264 struct g_slicer *gsp;
265 u_char *buf, hash[16];
269 g_trace(G_T_TOPOLOGY, "g_sunlabel_taste(%s,%s)", mp->name, pp->name);
271 if (flags == G_TF_NORMAL &&
272 !strcmp(pp->geom->class->name, SUNLABEL_CLASS_NAME))
274 gp = g_slice_new(mp, 8, pp, &cp, &ms, sizeof *ms, g_sunlabel_start);
279 ms->sectorsize = cp->provider->sectorsize;
280 if (ms->sectorsize < 512)
283 buf = g_read_data(cp, 0, ms->sectorsize, NULL);
289 * Calculate MD5 from the first sector and use it for avoiding
290 * recursive labels creation.
293 MD5Update(&md5sum, buf, ms->sectorsize);
294 MD5Final(ms->labelsum, &md5sum);
296 error = g_getattr("SUN::labelsum", cp, &hash);
297 if (!error && !bcmp(ms->labelsum, hash, sizeof(hash))) {
302 g_sunlabel_modify(gp, ms, buf);
307 g_access(cp, -1, 0, 0);
308 if (LIST_EMPTY(&gp->provider)) {
312 g_slice_conf_hot(gp, 0, 0, SUN_SIZE,
313 G_SLICE_HOT_ALLOW, G_SLICE_HOT_DENY, G_SLICE_HOT_CALL);
314 gsp->hot = g_sunlabel_hotwrite;
318 static struct g_class g_sunlabel_class = {
319 .name = SUNLABEL_CLASS_NAME,
320 .version = G_VERSION,
321 .taste = g_sunlabel_taste,
322 .ctlreq = g_sunlabel_config,
323 .dumpconf = g_sunlabel_dumpconf,
326 DECLARE_GEOM_CLASS(g_sunlabel_class, g_sunlabel);