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 {
68 static int g_sunlabel_once = 0;
71 g_sunlabel_modify(struct g_geom *gp, struct g_sunlabel_softc *ms, u_char *sec0)
75 struct sun_disklabel sl;
78 error = sunlabel_dec(sec0, &sl);
82 csize = sl.sl_ntracks * sl.sl_nsectors;
84 for (i = 0; i < SUN_NPART; i++) {
85 v = sl.sl_part[i].sdkp_cyloffset;
86 u = sl.sl_part[i].sdkp_nsectors;
87 error = g_slice_config(gp, i, G_SLICE_CONFIG_CHECK,
88 ((off_t)v * csize) << 9ULL,
91 "%s%c", gp->name, 'a' + i);
95 for (i = 0; i < SUN_NPART; i++) {
96 v = sl.sl_part[i].sdkp_cyloffset;
97 u = sl.sl_part[i].sdkp_nsectors;
98 g_slice_config(gp, i, G_SLICE_CONFIG_SET,
99 ((off_t)v * csize) << 9ULL,
102 "%s%c", gp->name, 'a' + i);
104 ms->nalt = sl.sl_acylinders;
105 ms->nheads = sl.sl_ntracks;
106 ms->nsects = sl.sl_nsectors;
109 * Calculate MD5 from the first sector and use it for avoiding
110 * recursive labels creation.
113 MD5Update(&md5sum, sec0, ms->sectorsize);
114 MD5Final(ms->labelsum, &md5sum);
120 g_sunlabel_hotwrite(void *arg, int flag)
124 struct g_slicer *gsp;
126 struct g_sunlabel_softc *ms;
130 KASSERT(flag != EV_CANCEL, ("g_sunlabel_hotwrite cancelled"));
132 gp = bp->bio_to->geom;
135 gsl = &gsp->slices[bp->bio_to->index];
137 * XXX: For all practical purposes, this whould be equvivalent to
138 * XXX: "p = (u_char *)bp->bio_data;" because the label is always
139 * XXX: in the first sector and we refuse sectors smaller than the
142 p = (u_char *)bp->bio_data - (bp->bio_offset + gsl->offset);
144 error = g_sunlabel_modify(gp, ms, p);
146 g_io_deliver(bp, EPERM);
149 g_slice_finish_hot(bp);
153 g_sunlabel_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp)
155 struct g_slicer *gsp;
156 struct g_sunlabel_softc *ms;
160 g_slice_dumpconf(sb, indent, gp, cp, pp);
161 if (indent == NULL) {
162 sbuf_printf(sb, " sc %u hd %u alt %u",
163 ms->nsects, ms->nheads, ms->nalt);
169 struct g_sunlabel_softc *ms;
175 g_sunlabel_callconfig(void *arg, int flag)
180 hp->error = g_sunlabel_modify(hp->gp, hp->ms, hp->label);
182 hp->error = g_write_data(LIST_FIRST(&hp->gp->consumer),
183 0, hp->label, SUN_SIZE);
187 * NB! curthread is user process which GCTL'ed.
190 g_sunlabel_config(struct gctl_req *req, struct g_class *mp, const char *verb)
195 struct g_slicer *gsp;
197 struct g_consumer *cp;
200 gp = gctl_get_geom(req, mp, "geom");
203 cp = LIST_FIRST(&gp->consumer);
205 if (!strcmp(verb, "write label")) {
206 label = gctl_get_paraml(req, "label", SUN_SIZE);
210 h0h0.ms = gsp->softc;
213 /* XXX: Does this reference register with our selfdestruct code ? */
214 error = g_access(cp, 1, 1, 1);
216 gctl_error(req, "could not access consumer");
219 g_sunlabel_callconfig(&h0h0, 0);
220 g_access(cp, -1, -1, -1);
221 } else if (!strcmp(verb, "write bootcode")) {
222 label = gctl_get_paraml(req, "bootcode", SUN_BOOTSIZE);
225 /* XXX: Does this reference register with our selfdestruct code ? */
226 error = g_access(cp, 1, 1, 1);
228 gctl_error(req, "could not access consumer");
231 for (i = 0; i < SUN_NPART; i++) {
232 if (gsp->slices[i].length <= SUN_BOOTSIZE)
235 gsp->slices[i].offset + SUN_SIZE, label + SUN_SIZE,
236 SUN_BOOTSIZE - SUN_SIZE);
238 g_access(cp, -1, -1, -1);
240 gctl_error(req, "Unknown verb parameter");
245 g_sunlabel_start(struct bio *bp)
247 struct g_sunlabel_softc *mp;
248 struct g_slicer *gsp;
250 gsp = bp->bio_to->geom->softc;
252 if (bp->bio_cmd == BIO_GETATTR) {
253 if (g_handleattr(bp, "SUN::labelsum", mp->labelsum,
254 sizeof(mp->labelsum)))
260 static struct g_geom *
261 g_sunlabel_taste(struct g_class *mp, struct g_provider *pp, int flags)
264 struct g_consumer *cp;
265 struct g_sunlabel_softc *ms;
266 struct g_slicer *gsp;
267 u_char *buf, hash[16];
271 g_trace(G_T_TOPOLOGY, "g_sunlabel_taste(%s,%s)", mp->name, pp->name);
273 if (flags == G_TF_NORMAL &&
274 !strcmp(pp->geom->class->name, SUNLABEL_CLASS_NAME))
276 gp = g_slice_new(mp, 8, pp, &cp, &ms, sizeof *ms, g_sunlabel_start);
281 ms->sectorsize = cp->provider->sectorsize;
282 if (ms->sectorsize < 512)
285 buf = g_read_data(cp, 0, ms->sectorsize, NULL);
291 * Calculate MD5 from the first sector and use it for avoiding
292 * recursive labels creation.
295 MD5Update(&md5sum, buf, ms->sectorsize);
296 MD5Final(ms->labelsum, &md5sum);
298 error = g_getattr("SUN::labelsum", cp, &hash);
299 if (!error && !bcmp(ms->labelsum, hash, sizeof(hash))) {
304 g_sunlabel_modify(gp, ms, buf);
309 g_access(cp, -1, 0, 0);
310 if (LIST_EMPTY(&gp->provider)) {
314 g_slice_conf_hot(gp, 0, 0, SUN_SIZE,
315 G_SLICE_HOT_ALLOW, G_SLICE_HOT_DENY, G_SLICE_HOT_CALL);
316 gsp->hot = g_sunlabel_hotwrite;
317 if (!g_sunlabel_once) {
320 "WARNING: geom_sunlabel (geom %s) is deprecated, "
321 "use gpart instead.\n", gp->name);
326 static struct g_class g_sunlabel_class = {
327 .name = SUNLABEL_CLASS_NAME,
328 .version = G_VERSION,
329 .taste = g_sunlabel_taste,
330 .ctlreq = g_sunlabel_config,
331 .dumpconf = g_sunlabel_dumpconf,
334 DECLARE_GEOM_CLASS(g_sunlabel_class, g_sunlabel);
335 MODULE_VERSION(geom_sunlabel, 0);