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
37 * This is the method for dealing with BSD disklabels. It has been
38 * extensively (by my standards at least) commented, in the vain hope that
39 * it will serve as the source in future copy&paste operations.
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
45 #include <sys/param.h>
46 #include <sys/endian.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/fcntl.h>
52 #include <sys/malloc.h>
54 #include <sys/mutex.h>
56 #include <sys/errno.h>
57 #include <sys/disklabel.h>
60 #include <geom/geom.h>
61 #include <geom/geom_slice.h>
63 #define BSD_CLASS_NAME "BSD"
65 #define ALPHA_LABEL_OFFSET 64
66 #define HISTORIC_LABEL_OFFSET 512
68 #define LABELSIZE (148 + 16 * MAXPARTITIONS)
70 static void g_bsd_hotwrite(void *arg, int flag);
72 * Our private data about one instance. All the rest is handled by the
73 * slice code and stored in its softc, so this is just the stuff
74 * specific to BSD disklabels.
80 struct disklabel ondisk;
81 u_char label[LABELSIZE];
86 * Modify our slicer to match proposed disklabel, if possible.
87 * This is where we make sure we don't do something stupid.
90 g_bsd_modify(struct g_geom *gp, u_char *label)
93 struct partition *ppp;
95 struct g_consumer *cp;
96 struct g_bsd_softc *ms;
106 error = bsd_disklabel_le_dec(label, &dl, MAXPARTITIONS);
111 /* Get dimensions of our device. */
112 cp = LIST_FIRST(&gp->consumer);
113 secsize = cp->provider->sectorsize;
115 /* ... or a smaller sector size. */
116 if (dl.d_secsize < secsize) {
120 /* ... or a non-multiple sector size. */
121 if (dl.d_secsize % secsize != 0) {
125 /* Historical braindamage... */
126 rawoffset = (off_t)dl.d_partitions[RAW_PART].p_offset * dl.d_secsize;
128 for (i = 0; i < dl.d_npartitions; i++) {
129 ppp = &dl.d_partitions[i];
130 if (ppp->p_size == 0)
132 o = (off_t)ppp->p_offset * dl.d_secsize;
138 if (rawoffset != 0 && (off_t)rawoffset != ms->mbroffset)
139 printf("WARNING: Expected rawoffset %jd, found %jd\n",
140 (intmax_t)ms->mbroffset/dl.d_secsize,
141 (intmax_t)rawoffset/dl.d_secsize);
143 /* Don't munge open partitions. */
144 for (i = 0; i < dl.d_npartitions; i++) {
145 ppp = &dl.d_partitions[i];
147 o = (off_t)ppp->p_offset * dl.d_secsize;
150 error = g_slice_config(gp, i, G_SLICE_CONFIG_CHECK,
152 (off_t)ppp->p_size * dl.d_secsize,
154 "%s%c", gp->name, 'a' + i);
159 /* Look good, go for it... */
160 for (u = 0; u < gsp->nslice; u++) {
161 ppp = &dl.d_partitions[u];
162 o = (off_t)ppp->p_offset * dl.d_secsize;
165 g_slice_config(gp, u, G_SLICE_CONFIG_SET,
167 (off_t)ppp->p_size * dl.d_secsize,
169 "%s%c", gp->name, 'a' + u);
172 /* Update our softc */
174 if (label != ms->label)
175 bcopy(label, ms->label, LABELSIZE);
176 ms->rawoffset = rawoffset;
179 * In order to avoid recursively attaching to the same
180 * on-disk label (it's usually visible through the 'c'
181 * partition) we calculate an MD5 and ask if other BSD's
182 * below us love that label. If they do, we don't.
185 MD5Update(&md5sum, ms->label, sizeof(ms->label));
186 MD5Final(ms->labelsum, &md5sum);
192 * This is an internal helper function, called multiple times from the taste
193 * function to try to locate a disklabel on the disk. More civilized formats
194 * will not need this, as there is only one possible place on disk to look
195 * for the magic spot.
199 g_bsd_try(struct g_geom *gp, struct g_slicer *gsp, struct g_consumer *cp, int secsize, struct g_bsd_softc *ms, off_t offset)
203 struct disklabel *dl;
207 * We need to read entire aligned sectors, and we assume that the
208 * disklabel does not span sectors, so one sector is enough.
210 secoff = offset % secsize;
211 buf = g_read_data(cp, offset - secoff, secsize, NULL);
215 /* Decode into our native format. */
217 error = bsd_disklabel_le_dec(buf + secoff, dl, MAXPARTITIONS);
219 bcopy(buf + secoff, ms->label, LABELSIZE);
221 /* Remember to free the buffer g_read_data() gave us. */
224 ms->labeloffset = offset;
229 * This function writes the current label to disk, possibly updating
230 * the alpha SRM checksum.
234 g_bsd_writelabel(struct g_geom *gp, u_char *bootcode)
238 struct g_consumer *cp;
239 struct g_slicer *gsp;
240 struct g_bsd_softc *ms;
247 cp = LIST_FIRST(&gp->consumer);
248 /* Get sector size, we need it to read data. */
249 secsize = cp->provider->sectorsize;
250 secoff = ms->labeloffset % secsize;
251 if (bootcode == NULL) {
252 buf = g_read_data(cp, ms->labeloffset - secoff, secsize, &error);
255 bcopy(ms->label, buf + secoff, sizeof(ms->label));
258 bcopy(ms->label, buf + ms->labeloffset, sizeof(ms->label));
260 if (ms->labeloffset == ALPHA_LABEL_OFFSET) {
262 for (i = 0; i < 63; i++)
263 sum += le64dec(buf + i * 8);
264 le64enc(buf + 504, sum);
266 if (bootcode == NULL) {
267 error = g_write_data(cp, ms->labeloffset - secoff, buf, secsize);
270 error = g_write_data(cp, 0, bootcode, BBSIZE);
276 * If the user tries to overwrite our disklabel through an open partition
277 * or via a magicwrite config call, we end up here and try to prevent
278 * footshooting as best we can.
281 g_bsd_hotwrite(void *arg, int flag)
285 struct g_slicer *gsp;
287 struct g_bsd_softc *ms;
293 * We should never get canceled, because that would amount to a removal
294 * of the geom while there was outstanding I/O requests.
296 KASSERT(flag != EV_CANCEL, ("g_bsd_hotwrite cancelled"));
298 gp = bp->bio_to->geom;
301 gsl = &gsp->slices[bp->bio_to->index];
302 p = (u_char*)bp->bio_data + ms->labeloffset
303 - (bp->bio_offset + gsl->offset);
304 error = g_bsd_modify(gp, p);
306 g_io_deliver(bp, EPERM);
309 g_slice_finish_hot(bp);
313 * This start routine is only called for non-trivial requests, all the
314 * trivial ones are handled autonomously by the slice code.
315 * For requests we handle here, we must call the g_io_deliver() on the
316 * bio, and return non-zero to indicate to the slice code that we did so.
317 * This code executes in the "DOWN" I/O path, this means:
319 * * Don't grab the topology lock.
320 * * Don't call biowait, g_getattr(), g_setattr() or g_read_data()
323 g_bsd_ioctl(struct g_provider *pp, u_long cmd, void *data, int fflag, struct thread *td)
326 struct g_bsd_softc *ms;
327 struct g_slicer *gsp;
337 /* Return a copy of the disklabel to userland. */
338 bsd_disklabel_le_dec(ms->label, data, MAXPARTITIONS);
341 struct g_consumer *cp;
347 if (!(fflag & FWRITE))
349 /* The disklabel to set is the ioctl argument. */
350 buf = g_malloc(BBSIZE, M_WAITOK);
352 error = copyin(p, buf, BBSIZE);
354 /* XXX: Rude, but supposedly safe */
357 /* Validate and modify our slice instance to match. */
358 error = g_bsd_modify(gp, buf + ms->labeloffset);
360 cp = LIST_FIRST(&gp->consumer);
361 if (ms->labeloffset == ALPHA_LABEL_OFFSET) {
363 for (i = 0; i < 63; i++)
364 sum += le64dec(buf + i * 8);
365 le64enc(buf + 504, sum);
367 error = g_write_data(cp, 0, buf, BBSIZE);
377 if (!(fflag & FWRITE))
379 label = g_malloc(LABELSIZE, M_WAITOK);
380 /* The disklabel to set is the ioctl argument. */
381 bsd_disklabel_le_enc(label, data);
385 /* Validate and modify our slice instance to match. */
386 error = g_bsd_modify(gp, label);
387 if (error == 0 && cmd == DIOCWDINFO)
388 error = g_bsd_writelabel(gp, NULL);
400 g_bsd_start(struct bio *bp)
403 struct g_bsd_softc *ms;
404 struct g_slicer *gsp;
406 gp = bp->bio_to->geom;
409 if (bp->bio_cmd == BIO_GETATTR) {
410 if (g_handleattr(bp, "BSD::labelsum", ms->labelsum,
411 sizeof(ms->labelsum)))
418 * Dump configuration information in XML format.
419 * Notice that the function is called once for the geom and once for each
420 * consumer and provider. We let g_slice_dumpconf() do most of the work.
423 g_bsd_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp)
425 struct g_bsd_softc *ms;
426 struct g_slicer *gsp;
430 g_slice_dumpconf(sb, indent, gp, cp, pp);
431 if (indent != NULL && pp == NULL && cp == NULL) {
432 sbuf_printf(sb, "%s<labeloffset>%jd</labeloffset>\n",
433 indent, (intmax_t)ms->labeloffset);
434 sbuf_printf(sb, "%s<rawoffset>%jd</rawoffset>\n",
435 indent, (intmax_t)ms->rawoffset);
436 sbuf_printf(sb, "%s<mbroffset>%jd</mbroffset>\n",
437 indent, (intmax_t)ms->mbroffset);
438 } else if (pp != NULL) {
440 sbuf_printf(sb, " ty %d",
441 ms->ondisk.d_partitions[pp->index].p_fstype);
443 sbuf_printf(sb, "%s<type>%d</type>\n", indent,
444 ms->ondisk.d_partitions[pp->index].p_fstype);
449 * The taste function is called from the event-handler, with the topology
450 * lock already held and a provider to examine. The flags are unused.
452 * If flags == G_TF_NORMAL, the idea is to take a bite of the provider and
453 * if we find valid, consistent magic on it, build a geom on it.
455 * There may be cases where the operator would like to put a BSD-geom on
456 * providers which do not meet all of the requirements. This can be done
457 * by instead passing the G_TF_INSIST flag, which will override these
460 * The final flags value is G_TF_TRANSPARENT, which instructs the method
461 * to put a geom on top of the provider and configure it to be as transparent
462 * as possible. This is not really relevant to the BSD method and therefore
463 * not implemented here.
466 static struct uuid freebsd_slice = GPT_ENT_TYPE_FREEBSD;
468 static struct g_geom *
469 g_bsd_taste(struct g_class *mp, struct g_provider *pp, int flags)
472 struct g_consumer *cp;
474 struct g_bsd_softc *ms;
476 struct g_slicer *gsp;
481 g_trace(G_T_TOPOLOGY, "bsd_taste(%s,%s)", mp->name, pp->name);
484 /* We don't implement transparent inserts. */
485 if (flags == G_TF_TRANSPARENT)
489 * BSD labels are a subclass of the general "slicing" topology so
490 * a lot of the work can be done by the common "slice" code.
491 * Create a geom with space for MAXPARTITIONS providers, one consumer
492 * and a softc structure for us. Specify the provider to attach
493 * the consumer to and our "start" routine for special requests.
494 * The provider is opened with mode (1,0,0) so we can do reads
497 gp = g_slice_new(mp, MAXPARTITIONS, pp, &cp, &ms,
498 sizeof(*ms), g_bsd_start);
502 /* Get the geom_slicer softc from the geom. */
506 * The do...while loop here allows us to have multiple escapes
507 * using a simple "break". This improves code clarity without
508 * ending up in deep nesting and without using goto or come from.
512 * If the provider is an MBR we will only auto attach
513 * to type 165 slices in the G_TF_NORMAL case. We will
514 * attach to any other type.
516 error = g_getattr("MBR::type", cp, &i);
518 if (i != 165 && flags == G_TF_NORMAL)
520 error = g_getattr("MBR::offset", cp, &ms->mbroffset);
525 /* Same thing if we are inside a PC98 */
526 error = g_getattr("PC98::type", cp, &i);
528 if (i != 0xc494 && flags == G_TF_NORMAL)
530 error = g_getattr("PC98::offset", cp, &ms->mbroffset);
535 /* Same thing if we are inside a GPT */
536 error = g_getattr("GPT::type", cp, &uuid);
538 if (memcmp(&uuid, &freebsd_slice, sizeof(uuid)) != 0 &&
539 flags == G_TF_NORMAL)
543 /* Get sector size, we need it to read data. */
544 secsize = cp->provider->sectorsize;
548 /* First look for a label at the start of the second sector. */
549 error = g_bsd_try(gp, gsp, cp, secsize, ms, secsize);
552 * If sector size is not 512 the label still can be at
553 * offset 512, not at the start of the second sector. At least
554 * it's true for labels created by the FreeBSD's bsdlabel(8).
556 if (error && secsize != HISTORIC_LABEL_OFFSET)
557 error = g_bsd_try(gp, gsp, cp, secsize, ms,
558 HISTORIC_LABEL_OFFSET);
560 /* Next, look for alpha labels */
562 error = g_bsd_try(gp, gsp, cp, secsize, ms,
565 /* If we didn't find a label, punt. */
570 * In order to avoid recursively attaching to the same
571 * on-disk label (it's usually visible through the 'c'
572 * partition) we calculate an MD5 and ask if other BSD's
573 * below us love that label. If they do, we don't.
576 MD5Update(&md5sum, ms->label, sizeof(ms->label));
577 MD5Final(ms->labelsum, &md5sum);
579 error = g_getattr("BSD::labelsum", cp, &hash);
580 if (!error && !bcmp(ms->labelsum, hash, sizeof(hash)))
584 * Process the found disklabel, and modify our "slice"
585 * instance to match it, if possible.
587 error = g_bsd_modify(gp, ms->label);
590 /* Success or failure, we can close our provider now. */
591 g_access(cp, -1, 0, 0);
593 /* If we have configured any providers, return the new geom. */
594 if (gsp->nprovider > 0) {
595 g_slice_conf_hot(gp, 0, ms->labeloffset, LABELSIZE,
596 G_SLICE_HOT_ALLOW, G_SLICE_HOT_DENY, G_SLICE_HOT_CALL);
597 gsp->hot = g_bsd_hotwrite;
601 * ...else push the "self-destruct" button, by spoiling our own
602 * consumer. This triggers a call to g_slice_spoiled which will
603 * dismantle what was setup.
611 struct g_bsd_softc *ms;
617 g_bsd_callconfig(void *arg, int flag)
622 hp->error = g_bsd_modify(hp->gp, hp->label);
624 hp->error = g_bsd_writelabel(hp->gp, NULL);
628 * NB! curthread is user process which GCTL'ed.
631 g_bsd_config(struct gctl_req *req, struct g_class *mp, char const *verb)
637 struct g_slicer *gsp;
638 struct g_consumer *cp;
639 struct g_bsd_softc *ms;
642 gp = gctl_get_geom(req, mp, "geom");
645 cp = LIST_FIRST(&gp->consumer);
648 if (!strcmp(verb, "read mbroffset")) {
649 gctl_set_param_err(req, "mbroffset", &ms->mbroffset,
650 sizeof(ms->mbroffset));
652 } else if (!strcmp(verb, "write label")) {
653 label = gctl_get_paraml(req, "label", LABELSIZE);
657 h0h0.ms = gsp->softc;
660 /* XXX: Does this reference register with our selfdestruct code ? */
661 error = g_access(cp, 1, 1, 1);
663 gctl_error(req, "could not access consumer");
666 g_bsd_callconfig(&h0h0, 0);
668 g_access(cp, -1, -1, -1);
669 } else if (!strcmp(verb, "write bootcode")) {
670 label = gctl_get_paraml(req, "bootcode", BBSIZE);
673 /* XXX: Does this reference register with our selfdestruct code ? */
674 error = g_access(cp, 1, 1, 1);
676 gctl_error(req, "could not access consumer");
679 error = g_bsd_writelabel(gp, label);
680 g_access(cp, -1, -1, -1);
682 gctl_error(req, "Unknown verb parameter");
688 /* Finally, register with GEOM infrastructure. */
689 static struct g_class g_bsd_class = {
690 .name = BSD_CLASS_NAME,
691 .version = G_VERSION,
692 .taste = g_bsd_taste,
693 .ctlreq = g_bsd_config,
694 .dumpconf = g_bsd_dumpconf,
695 .ioctl = g_bsd_ioctl,
698 DECLARE_GEOM_CLASS(g_bsd_class, g_bsd);