2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2002 Poul-Henning Kamp
5 * Copyright (c) 2002 Networks Associates Technology, Inc.
8 * This software was developed for the FreeBSD Project by Poul-Henning Kamp
9 * and NAI Labs, the Security Research Division of Network Associates, Inc.
10 * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
11 * DARPA CHATS research program.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. The names of the authors may not be used to endorse or promote
22 * products derived from this software without specific prior written
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
41 #include <sys/param.h>
42 #include <sys/endian.h>
43 #include <sys/systm.h>
44 #include <sys/sysctl.h>
45 #include <sys/kernel.h>
48 #include <sys/malloc.h>
50 #include <sys/mutex.h>
53 #include <sys/sun_disklabel.h>
54 #include <geom/geom.h>
55 #include <geom/geom_slice.h>
56 #include <machine/endian.h>
58 FEATURE(geom_sunlabel, "GEOM Sun/Solaris partitioning support");
60 #define SUNLABEL_CLASS_NAME "SUN"
62 struct g_sunlabel_softc {
70 static int g_sunlabel_once = 0;
73 g_sunlabel_modify(struct g_geom *gp, struct g_sunlabel_softc *ms, u_char *sec0)
77 struct sun_disklabel sl;
80 error = sunlabel_dec(sec0, &sl);
84 csize = sl.sl_ntracks * sl.sl_nsectors;
86 for (i = 0; i < SUN_NPART; i++) {
87 v = sl.sl_part[i].sdkp_cyloffset;
88 u = sl.sl_part[i].sdkp_nsectors;
89 error = g_slice_config(gp, i, G_SLICE_CONFIG_CHECK,
90 ((off_t)v * csize) << 9ULL,
93 "%s%c", gp->name, 'a' + i);
97 for (i = 0; i < SUN_NPART; i++) {
98 v = sl.sl_part[i].sdkp_cyloffset;
99 u = sl.sl_part[i].sdkp_nsectors;
100 g_slice_config(gp, i, G_SLICE_CONFIG_SET,
101 ((off_t)v * csize) << 9ULL,
104 "%s%c", gp->name, 'a' + i);
106 ms->nalt = sl.sl_acylinders;
107 ms->nheads = sl.sl_ntracks;
108 ms->nsects = sl.sl_nsectors;
111 * Calculate MD5 from the first sector and use it for avoiding
112 * recursive labels creation.
115 MD5Update(&md5sum, sec0, ms->sectorsize);
116 MD5Final(ms->labelsum, &md5sum);
122 g_sunlabel_hotwrite(void *arg, int flag)
126 struct g_slicer *gsp;
128 struct g_sunlabel_softc *ms;
132 KASSERT(flag != EV_CANCEL, ("g_sunlabel_hotwrite cancelled"));
134 gp = bp->bio_to->geom;
137 gsl = &gsp->slices[bp->bio_to->index];
139 * XXX: For all practical purposes, this whould be equvivalent to
140 * XXX: "p = (u_char *)bp->bio_data;" because the label is always
141 * XXX: in the first sector and we refuse sectors smaller than the
144 p = (u_char *)bp->bio_data - (bp->bio_offset + gsl->offset);
146 error = g_sunlabel_modify(gp, ms, p);
148 g_io_deliver(bp, EPERM);
151 g_slice_finish_hot(bp);
155 g_sunlabel_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp)
157 struct g_slicer *gsp;
158 struct g_sunlabel_softc *ms;
162 g_slice_dumpconf(sb, indent, gp, cp, pp);
163 if (indent == NULL) {
164 sbuf_printf(sb, " sc %u hd %u alt %u",
165 ms->nsects, ms->nheads, ms->nalt);
171 struct g_sunlabel_softc *ms;
177 g_sunlabel_callconfig(void *arg, int flag)
182 hp->error = g_sunlabel_modify(hp->gp, hp->ms, hp->label);
184 hp->error = g_write_data(LIST_FIRST(&hp->gp->consumer),
185 0, hp->label, SUN_SIZE);
189 * NB! curthread is user process which GCTL'ed.
192 g_sunlabel_config(struct gctl_req *req, struct g_class *mp, const char *verb)
197 struct g_slicer *gsp;
199 struct g_consumer *cp;
202 gp = gctl_get_geom(req, mp, "geom");
205 cp = LIST_FIRST(&gp->consumer);
207 if (!strcmp(verb, "write label")) {
208 label = gctl_get_paraml(req, "label", SUN_SIZE);
212 h0h0.ms = gsp->softc;
215 /* XXX: Does this reference register with our selfdestruct code ? */
216 error = g_access(cp, 1, 1, 1);
218 gctl_error(req, "could not access consumer");
221 g_sunlabel_callconfig(&h0h0, 0);
222 g_access(cp, -1, -1, -1);
223 } else if (!strcmp(verb, "write bootcode")) {
224 label = gctl_get_paraml(req, "bootcode", SUN_BOOTSIZE);
227 /* XXX: Does this reference register with our selfdestruct code ? */
228 error = g_access(cp, 1, 1, 1);
230 gctl_error(req, "could not access consumer");
233 for (i = 0; i < SUN_NPART; i++) {
234 if (gsp->slices[i].length <= SUN_BOOTSIZE)
237 gsp->slices[i].offset + SUN_SIZE, label + SUN_SIZE,
238 SUN_BOOTSIZE - SUN_SIZE);
240 g_access(cp, -1, -1, -1);
242 gctl_error(req, "Unknown verb parameter");
247 g_sunlabel_start(struct bio *bp)
249 struct g_sunlabel_softc *mp;
250 struct g_slicer *gsp;
252 gsp = bp->bio_to->geom->softc;
254 if (bp->bio_cmd == BIO_GETATTR) {
255 if (g_handleattr(bp, "SUN::labelsum", mp->labelsum,
256 sizeof(mp->labelsum)))
262 static struct g_geom *
263 g_sunlabel_taste(struct g_class *mp, struct g_provider *pp, int flags)
266 struct g_consumer *cp;
267 struct g_sunlabel_softc *ms;
268 struct g_slicer *gsp;
269 u_char *buf, hash[16];
273 g_trace(G_T_TOPOLOGY, "g_sunlabel_taste(%s,%s)", mp->name, pp->name);
275 if (flags == G_TF_NORMAL &&
276 !strcmp(pp->geom->class->name, SUNLABEL_CLASS_NAME))
278 gp = g_slice_new(mp, 8, pp, &cp, &ms, sizeof *ms, g_sunlabel_start);
283 ms->sectorsize = cp->provider->sectorsize;
284 if (ms->sectorsize < 512)
287 buf = g_read_data(cp, 0, ms->sectorsize, NULL);
293 * Calculate MD5 from the first sector and use it for avoiding
294 * recursive labels creation.
297 MD5Update(&md5sum, buf, ms->sectorsize);
298 MD5Final(ms->labelsum, &md5sum);
300 error = g_getattr("SUN::labelsum", cp, &hash);
301 if (!error && !bcmp(ms->labelsum, hash, sizeof(hash))) {
306 g_sunlabel_modify(gp, ms, buf);
311 g_access(cp, -1, 0, 0);
312 if (LIST_EMPTY(&gp->provider)) {
316 g_slice_conf_hot(gp, 0, 0, SUN_SIZE,
317 G_SLICE_HOT_ALLOW, G_SLICE_HOT_DENY, G_SLICE_HOT_CALL);
318 gsp->hot = g_sunlabel_hotwrite;
319 if (!g_sunlabel_once) {
322 "WARNING: geom_sunlabel (geom %s) is deprecated, "
323 "use gpart instead.\n", gp->name);
328 static struct g_class g_sunlabel_class = {
329 .name = SUNLABEL_CLASS_NAME,
330 .version = G_VERSION,
331 .taste = g_sunlabel_taste,
332 .ctlreq = g_sunlabel_config,
333 .dumpconf = g_sunlabel_dumpconf,
336 DECLARE_GEOM_CLASS(g_sunlabel_class, g_sunlabel);
337 MODULE_VERSION(geom_sunlabel, 0);