2002-03-11 21:42:35 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 2002 Poul-Henning Kamp
|
|
|
|
* Copyright (c) 2002 Networks Associates Technology, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This software was developed for the FreeBSD Project by Poul-Henning Kamp
|
|
|
|
* and NAI Labs, the Security Research Division of Network Associates, Inc.
|
|
|
|
* under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
|
|
|
|
* DARPA CHATS research program.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. The names of the authors may not be used to endorse or promote
|
|
|
|
* products derived from this software without specific prior written
|
|
|
|
* permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
2005-01-06 18:27:30 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2002-09-27 21:54:37 +00:00
|
|
|
* This is the method for dealing with BSD disklabels. It has been
|
|
|
|
* extensively (by my standards at least) commented, in the vain hope that
|
2003-01-06 20:10:41 +00:00
|
|
|
* it will serve as the source in future copy&paste operations.
|
2002-03-11 21:42:35 +00:00
|
|
|
*/
|
|
|
|
|
2003-06-11 06:49:16 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2002-03-11 21:42:35 +00:00
|
|
|
#include <sys/param.h>
|
2003-04-03 11:36:53 +00:00
|
|
|
#include <sys/endian.h>
|
2002-03-11 21:42:35 +00:00
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/kernel.h>
|
2004-12-12 10:09:05 +00:00
|
|
|
#include <sys/fcntl.h>
|
2002-03-11 21:42:35 +00:00
|
|
|
#include <sys/conf.h>
|
|
|
|
#include <sys/bio.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/lock.h>
|
|
|
|
#include <sys/mutex.h>
|
2003-01-06 20:10:41 +00:00
|
|
|
#include <sys/md5.h>
|
2002-03-11 21:42:35 +00:00
|
|
|
#include <sys/errno.h>
|
|
|
|
#include <sys/disklabel.h>
|
2007-12-06 09:20:27 +00:00
|
|
|
#include <sys/gpt.h>
|
|
|
|
#include <sys/uuid.h>
|
2002-03-11 21:42:35 +00:00
|
|
|
#include <geom/geom.h>
|
|
|
|
#include <geom/geom_slice.h>
|
|
|
|
|
2002-10-20 08:43:56 +00:00
|
|
|
#define BSD_CLASS_NAME "BSD"
|
2002-03-11 21:42:35 +00:00
|
|
|
|
2002-11-08 15:31:34 +00:00
|
|
|
#define ALPHA_LABEL_OFFSET 64
|
2005-11-30 22:54:41 +00:00
|
|
|
#define HISTORIC_LABEL_OFFSET 512
|
2002-11-08 15:31:34 +00:00
|
|
|
|
2003-05-02 22:46:13 +00:00
|
|
|
#define LABELSIZE (148 + 16 * MAXPARTITIONS)
|
|
|
|
|
2003-04-19 10:14:39 +00:00
|
|
|
static void g_bsd_hotwrite(void *arg, int flag);
|
2002-09-27 21:54:37 +00:00
|
|
|
/*
|
|
|
|
* Our private data about one instance. All the rest is handled by the
|
2002-10-20 08:43:56 +00:00
|
|
|
* slice code and stored in its softc, so this is just the stuff
|
2002-09-27 21:54:37 +00:00
|
|
|
* specific to BSD disklabels.
|
|
|
|
*/
|
2002-03-11 21:42:35 +00:00
|
|
|
struct g_bsd_softc {
|
2002-04-23 19:54:02 +00:00
|
|
|
off_t labeloffset;
|
2002-10-28 07:50:47 +00:00
|
|
|
off_t mbroffset;
|
2002-09-27 21:54:37 +00:00
|
|
|
off_t rawoffset;
|
2002-03-11 21:42:35 +00:00
|
|
|
struct disklabel ondisk;
|
2003-05-02 22:46:13 +00:00
|
|
|
u_char label[LABELSIZE];
|
2003-01-06 20:10:41 +00:00
|
|
|
u_char labelsum[16];
|
2002-03-11 21:42:35 +00:00
|
|
|
};
|
|
|
|
|
2002-03-24 14:27:31 +00:00
|
|
|
/*
|
2002-09-27 21:54:37 +00:00
|
|
|
* Modify our slicer to match proposed disklabel, if possible.
|
2003-05-02 22:46:13 +00:00
|
|
|
* This is where we make sure we don't do something stupid.
|
2002-09-27 21:54:37 +00:00
|
|
|
*/
|
|
|
|
static int
|
2003-05-02 22:46:13 +00:00
|
|
|
g_bsd_modify(struct g_geom *gp, u_char *label)
|
2002-09-27 21:54:37 +00:00
|
|
|
{
|
|
|
|
int i, error;
|
|
|
|
struct partition *ppp;
|
|
|
|
struct g_slicer *gsp;
|
|
|
|
struct g_consumer *cp;
|
2003-04-19 10:14:39 +00:00
|
|
|
struct g_bsd_softc *ms;
|
2002-12-16 22:33:27 +00:00
|
|
|
u_int secsize, u;
|
2003-05-31 19:30:52 +00:00
|
|
|
off_t rawoffset, o;
|
2003-05-02 22:46:13 +00:00
|
|
|
struct disklabel dl;
|
|
|
|
MD5_CTX md5sum;
|
2002-09-27 21:54:37 +00:00
|
|
|
|
2003-05-02 22:46:13 +00:00
|
|
|
g_topology_assert();
|
|
|
|
gsp = gp->softc;
|
|
|
|
ms = gsp->softc;
|
2002-09-27 21:54:37 +00:00
|
|
|
|
2003-05-02 22:46:13 +00:00
|
|
|
error = bsd_disklabel_le_dec(label, &dl, MAXPARTITIONS);
|
|
|
|
if (error) {
|
|
|
|
return (error);
|
|
|
|
}
|
2002-09-27 21:54:37 +00:00
|
|
|
|
2002-10-20 08:43:56 +00:00
|
|
|
/* Get dimensions of our device. */
|
2002-09-27 21:54:37 +00:00
|
|
|
cp = LIST_FIRST(&gp->consumer);
|
2002-10-20 20:28:24 +00:00
|
|
|
secsize = cp->provider->sectorsize;
|
2002-09-27 21:54:37 +00:00
|
|
|
|
2002-10-20 08:43:56 +00:00
|
|
|
/* ... or a smaller sector size. */
|
2003-05-02 22:46:13 +00:00
|
|
|
if (dl.d_secsize < secsize) {
|
2002-09-27 21:54:37 +00:00
|
|
|
return (EINVAL);
|
2003-05-02 22:46:13 +00:00
|
|
|
}
|
2002-09-27 21:54:37 +00:00
|
|
|
|
2002-10-20 08:43:56 +00:00
|
|
|
/* ... or a non-multiple sector size. */
|
2003-05-02 22:46:13 +00:00
|
|
|
if (dl.d_secsize % secsize != 0) {
|
2002-09-27 21:54:37 +00:00
|
|
|
return (EINVAL);
|
2003-05-02 22:46:13 +00:00
|
|
|
}
|
2002-09-27 21:54:37 +00:00
|
|
|
|
2003-05-02 22:46:13 +00:00
|
|
|
/* Historical braindamage... */
|
|
|
|
rawoffset = (off_t)dl.d_partitions[RAW_PART].p_offset * dl.d_secsize;
|
2003-05-04 19:26:31 +00:00
|
|
|
|
2003-05-02 22:46:13 +00:00
|
|
|
for (i = 0; i < dl.d_npartitions; i++) {
|
|
|
|
ppp = &dl.d_partitions[i];
|
|
|
|
if (ppp->p_size == 0)
|
|
|
|
continue;
|
|
|
|
o = (off_t)ppp->p_offset * dl.d_secsize;
|
|
|
|
|
|
|
|
if (o < rawoffset)
|
|
|
|
rawoffset = 0;
|
|
|
|
}
|
2003-05-05 06:46:49 +00:00
|
|
|
|
2003-05-06 19:36:13 +00:00
|
|
|
if (rawoffset != 0 && (off_t)rawoffset != ms->mbroffset)
|
2003-05-05 06:46:49 +00:00
|
|
|
printf("WARNING: Expected rawoffset %jd, found %jd\n",
|
|
|
|
(intmax_t)ms->mbroffset/dl.d_secsize,
|
2003-05-06 19:36:13 +00:00
|
|
|
(intmax_t)rawoffset/dl.d_secsize);
|
2002-09-27 21:54:37 +00:00
|
|
|
|
2002-10-20 08:43:56 +00:00
|
|
|
/* Don't munge open partitions. */
|
2003-05-02 22:46:13 +00:00
|
|
|
for (i = 0; i < dl.d_npartitions; i++) {
|
|
|
|
ppp = &dl.d_partitions[i];
|
2002-09-27 21:54:37 +00:00
|
|
|
|
2003-05-02 22:46:13 +00:00
|
|
|
o = (off_t)ppp->p_offset * dl.d_secsize;
|
|
|
|
if (o == 0)
|
|
|
|
o = rawoffset;
|
2002-09-27 21:54:37 +00:00
|
|
|
error = g_slice_config(gp, i, G_SLICE_CONFIG_CHECK,
|
2003-05-02 22:46:13 +00:00
|
|
|
o - rawoffset,
|
|
|
|
(off_t)ppp->p_size * dl.d_secsize,
|
|
|
|
dl.d_secsize,
|
2002-09-27 21:54:37 +00:00
|
|
|
"%s%c", gp->name, 'a' + i);
|
2003-05-02 22:46:13 +00:00
|
|
|
if (error)
|
2002-09-27 21:54:37 +00:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Look good, go for it... */
|
2002-12-16 22:33:27 +00:00
|
|
|
for (u = 0; u < gsp->nslice; u++) {
|
2003-05-02 22:46:13 +00:00
|
|
|
ppp = &dl.d_partitions[u];
|
|
|
|
o = (off_t)ppp->p_offset * dl.d_secsize;
|
|
|
|
if (o == 0)
|
|
|
|
o = rawoffset;
|
2002-12-16 22:33:27 +00:00
|
|
|
g_slice_config(gp, u, G_SLICE_CONFIG_SET,
|
2003-05-02 22:46:13 +00:00
|
|
|
o - rawoffset,
|
|
|
|
(off_t)ppp->p_size * dl.d_secsize,
|
|
|
|
dl.d_secsize,
|
2002-12-16 22:33:27 +00:00
|
|
|
"%s%c", gp->name, 'a' + u);
|
2002-09-27 21:54:37 +00:00
|
|
|
}
|
|
|
|
|
2003-05-02 22:46:13 +00:00
|
|
|
/* Update our softc */
|
|
|
|
ms->ondisk = dl;
|
|
|
|
if (label != ms->label)
|
|
|
|
bcopy(label, ms->label, LABELSIZE);
|
|
|
|
ms->rawoffset = rawoffset;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* In order to avoid recursively attaching to the same
|
|
|
|
* on-disk label (it's usually visible through the 'c'
|
|
|
|
* partition) we calculate an MD5 and ask if other BSD's
|
|
|
|
* below us love that label. If they do, we don't.
|
|
|
|
*/
|
|
|
|
MD5Init(&md5sum);
|
|
|
|
MD5Update(&md5sum, ms->label, sizeof(ms->label));
|
|
|
|
MD5Final(ms->labelsum, &md5sum);
|
|
|
|
|
|
|
|
return (0);
|
2002-03-24 14:27:31 +00:00
|
|
|
}
|
|
|
|
|
2002-09-27 21:54:37 +00:00
|
|
|
/*
|
|
|
|
* This is an internal helper function, called multiple times from the taste
|
|
|
|
* function to try to locate a disklabel on the disk. More civilized formats
|
|
|
|
* will not need this, as there is only one possible place on disk to look
|
|
|
|
* for the magic spot.
|
|
|
|
*/
|
|
|
|
|
2002-03-24 14:27:31 +00:00
|
|
|
static int
|
2002-12-02 20:23:54 +00:00
|
|
|
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)
|
2002-03-24 14:27:31 +00:00
|
|
|
{
|
|
|
|
int error;
|
|
|
|
u_char *buf;
|
2002-04-23 19:54:02 +00:00
|
|
|
struct disklabel *dl;
|
2002-05-19 19:00:25 +00:00
|
|
|
off_t secoff;
|
2002-03-24 14:27:31 +00:00
|
|
|
|
2002-09-27 21:54:37 +00:00
|
|
|
/*
|
|
|
|
* We need to read entire aligned sectors, and we assume that the
|
|
|
|
* disklabel does not span sectors, so one sector is enough.
|
|
|
|
*/
|
2002-05-19 19:00:25 +00:00
|
|
|
secoff = offset % secsize;
|
2005-11-30 22:15:00 +00:00
|
|
|
buf = g_read_data(cp, offset - secoff, secsize, NULL);
|
2005-11-30 19:24:51 +00:00
|
|
|
if (buf == NULL)
|
2002-10-20 08:43:56 +00:00
|
|
|
return (ENOENT);
|
2002-09-27 21:54:37 +00:00
|
|
|
|
2002-10-20 08:43:56 +00:00
|
|
|
/* Decode into our native format. */
|
2002-09-27 21:54:37 +00:00
|
|
|
dl = &ms->ondisk;
|
2003-05-02 22:46:13 +00:00
|
|
|
error = bsd_disklabel_le_dec(buf + secoff, dl, MAXPARTITIONS);
|
|
|
|
if (!error)
|
|
|
|
bcopy(buf + secoff, ms->label, LABELSIZE);
|
2002-09-27 21:54:37 +00:00
|
|
|
|
2002-10-20 08:43:56 +00:00
|
|
|
/* Remember to free the buffer g_read_data() gave us. */
|
2002-03-24 14:27:31 +00:00
|
|
|
g_free(buf);
|
2002-09-27 21:54:37 +00:00
|
|
|
|
2003-04-19 10:14:39 +00:00
|
|
|
ms->labeloffset = offset;
|
2002-10-20 08:43:56 +00:00
|
|
|
return (error);
|
2002-03-24 14:27:31 +00:00
|
|
|
}
|
|
|
|
|
2003-05-03 08:01:34 +00:00
|
|
|
/*
|
|
|
|
* This function writes the current label to disk, possibly updating
|
|
|
|
* the alpha SRM checksum.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int
|
|
|
|
g_bsd_writelabel(struct g_geom *gp, u_char *bootcode)
|
|
|
|
{
|
|
|
|
off_t secoff;
|
|
|
|
u_int secsize;
|
|
|
|
struct g_consumer *cp;
|
|
|
|
struct g_slicer *gsp;
|
|
|
|
struct g_bsd_softc *ms;
|
|
|
|
u_char *buf;
|
|
|
|
uint64_t sum;
|
|
|
|
int error, i;
|
|
|
|
|
|
|
|
gsp = gp->softc;
|
|
|
|
ms = gsp->softc;
|
|
|
|
cp = LIST_FIRST(&gp->consumer);
|
|
|
|
/* Get sector size, we need it to read data. */
|
|
|
|
secsize = cp->provider->sectorsize;
|
|
|
|
secoff = ms->labeloffset % secsize;
|
|
|
|
if (bootcode == NULL) {
|
|
|
|
buf = g_read_data(cp, ms->labeloffset - secoff, secsize, &error);
|
2005-11-30 19:24:51 +00:00
|
|
|
if (buf == NULL)
|
2003-05-03 08:01:34 +00:00
|
|
|
return (error);
|
|
|
|
bcopy(ms->label, buf + secoff, sizeof(ms->label));
|
|
|
|
} else {
|
|
|
|
buf = bootcode;
|
|
|
|
bcopy(ms->label, buf + ms->labeloffset, sizeof(ms->label));
|
|
|
|
}
|
|
|
|
if (ms->labeloffset == ALPHA_LABEL_OFFSET) {
|
|
|
|
sum = 0;
|
|
|
|
for (i = 0; i < 63; i++)
|
|
|
|
sum += le64dec(buf + i * 8);
|
|
|
|
le64enc(buf + 504, sum);
|
|
|
|
}
|
|
|
|
if (bootcode == NULL) {
|
|
|
|
error = g_write_data(cp, ms->labeloffset - secoff, buf, secsize);
|
|
|
|
g_free(buf);
|
|
|
|
} else {
|
|
|
|
error = g_write_data(cp, 0, bootcode, BBSIZE);
|
|
|
|
}
|
|
|
|
return(error);
|
|
|
|
}
|
|
|
|
|
2002-12-02 20:23:54 +00:00
|
|
|
/*
|
|
|
|
* If the user tries to overwrite our disklabel through an open partition
|
|
|
|
* or via a magicwrite config call, we end up here and try to prevent
|
|
|
|
* footshooting as best we can.
|
|
|
|
*/
|
|
|
|
static void
|
2003-04-02 21:10:04 +00:00
|
|
|
g_bsd_hotwrite(void *arg, int flag)
|
2002-12-02 20:23:54 +00:00
|
|
|
{
|
|
|
|
struct bio *bp;
|
|
|
|
struct g_geom *gp;
|
|
|
|
struct g_slicer *gsp;
|
2002-12-13 21:31:13 +00:00
|
|
|
struct g_slice *gsl;
|
2002-12-02 20:23:54 +00:00
|
|
|
struct g_bsd_softc *ms;
|
|
|
|
u_char *p;
|
|
|
|
int error;
|
|
|
|
|
2003-05-02 22:46:13 +00:00
|
|
|
g_topology_assert();
|
2003-04-19 10:14:39 +00:00
|
|
|
/*
|
|
|
|
* We should never get canceled, because that would amount to a removal
|
|
|
|
* of the geom while there was outstanding I/O requests.
|
|
|
|
*/
|
2003-04-02 21:10:04 +00:00
|
|
|
KASSERT(flag != EV_CANCEL, ("g_bsd_hotwrite cancelled"));
|
2002-12-02 20:23:54 +00:00
|
|
|
bp = arg;
|
|
|
|
gp = bp->bio_to->geom;
|
|
|
|
gsp = gp->softc;
|
|
|
|
ms = gsp->softc;
|
2002-12-13 21:31:13 +00:00
|
|
|
gsl = &gsp->slices[bp->bio_to->index];
|
2002-12-16 22:33:27 +00:00
|
|
|
p = (u_char*)bp->bio_data + ms->labeloffset
|
2002-12-13 21:31:13 +00:00
|
|
|
- (bp->bio_offset + gsl->offset);
|
2003-05-02 22:46:13 +00:00
|
|
|
error = g_bsd_modify(gp, p);
|
2002-12-02 20:23:54 +00:00
|
|
|
if (error) {
|
|
|
|
g_io_deliver(bp, EPERM);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
g_slice_finish_hot(bp);
|
|
|
|
}
|
|
|
|
|
2002-09-27 21:54:37 +00:00
|
|
|
/*-
|
|
|
|
* This start routine is only called for non-trivial requests, all the
|
|
|
|
* trivial ones are handled autonomously by the slice code.
|
|
|
|
* For requests we handle here, we must call the g_io_deliver() on the
|
|
|
|
* bio, and return non-zero to indicate to the slice code that we did so.
|
|
|
|
* This code executes in the "DOWN" I/O path, this means:
|
|
|
|
* * No sleeping.
|
|
|
|
* * Don't grab the topology lock.
|
|
|
|
* * Don't call biowait, g_getattr(), g_setattr() or g_read_data()
|
|
|
|
*/
|
2002-03-11 21:42:35 +00:00
|
|
|
static int
|
2004-12-12 10:09:05 +00:00
|
|
|
g_bsd_ioctl(struct g_provider *pp, u_long cmd, void *data, int fflag, struct thread *td)
|
2002-03-11 21:42:35 +00:00
|
|
|
{
|
|
|
|
struct g_geom *gp;
|
|
|
|
struct g_bsd_softc *ms;
|
|
|
|
struct g_slicer *gsp;
|
2003-09-01 20:45:32 +00:00
|
|
|
u_char *label;
|
2002-09-27 21:54:37 +00:00
|
|
|
int error;
|
2002-03-11 21:42:35 +00:00
|
|
|
|
2003-09-01 20:45:32 +00:00
|
|
|
gp = pp->geom;
|
2002-03-11 21:42:35 +00:00
|
|
|
gsp = gp->softc;
|
|
|
|
ms = gsp->softc;
|
2002-09-27 21:54:37 +00:00
|
|
|
|
2003-09-01 20:45:32 +00:00
|
|
|
switch(cmd) {
|
2002-09-27 21:54:37 +00:00
|
|
|
case DIOCGDINFO:
|
2002-10-20 08:43:56 +00:00
|
|
|
/* Return a copy of the disklabel to userland. */
|
2003-09-01 20:45:32 +00:00
|
|
|
bsd_disklabel_le_dec(ms->label, data, MAXPARTITIONS);
|
|
|
|
return(0);
|
|
|
|
case DIOCBSDBB: {
|
|
|
|
struct g_consumer *cp;
|
|
|
|
u_char *buf;
|
|
|
|
void *p;
|
|
|
|
int error, i;
|
|
|
|
uint64_t sum;
|
|
|
|
|
2004-12-12 10:09:05 +00:00
|
|
|
if (!(fflag & FWRITE))
|
|
|
|
return (EPERM);
|
2003-09-01 20:45:32 +00:00
|
|
|
/* The disklabel to set is the ioctl argument. */
|
|
|
|
buf = g_malloc(BBSIZE, M_WAITOK);
|
|
|
|
p = *(void **)data;
|
|
|
|
error = copyin(p, buf, BBSIZE);
|
|
|
|
if (!error) {
|
|
|
|
/* XXX: Rude, but supposedly safe */
|
|
|
|
DROP_GIANT();
|
|
|
|
g_topology_lock();
|
|
|
|
/* Validate and modify our slice instance to match. */
|
|
|
|
error = g_bsd_modify(gp, buf + ms->labeloffset);
|
|
|
|
if (!error) {
|
|
|
|
cp = LIST_FIRST(&gp->consumer);
|
|
|
|
if (ms->labeloffset == ALPHA_LABEL_OFFSET) {
|
|
|
|
sum = 0;
|
|
|
|
for (i = 0; i < 63; i++)
|
|
|
|
sum += le64dec(buf + i * 8);
|
|
|
|
le64enc(buf + 504, sum);
|
|
|
|
}
|
|
|
|
error = g_write_data(cp, 0, buf, BBSIZE);
|
|
|
|
}
|
|
|
|
g_topology_unlock();
|
|
|
|
PICKUP_GIANT();
|
|
|
|
}
|
|
|
|
g_free(buf);
|
|
|
|
return (error);
|
|
|
|
}
|
2002-09-27 21:54:37 +00:00
|
|
|
case DIOCSDINFO:
|
2003-09-01 20:45:32 +00:00
|
|
|
case DIOCWDINFO: {
|
2004-12-12 10:09:05 +00:00
|
|
|
if (!(fflag & FWRITE))
|
|
|
|
return (EPERM);
|
2005-03-16 20:48:13 +00:00
|
|
|
label = g_malloc(LABELSIZE, M_WAITOK);
|
2003-09-01 20:45:32 +00:00
|
|
|
/* The disklabel to set is the ioctl argument. */
|
|
|
|
bsd_disklabel_le_enc(label, data);
|
|
|
|
|
|
|
|
DROP_GIANT();
|
|
|
|
g_topology_lock();
|
|
|
|
/* Validate and modify our slice instance to match. */
|
|
|
|
error = g_bsd_modify(gp, label);
|
|
|
|
if (error == 0 && cmd == DIOCWDINFO)
|
|
|
|
error = g_bsd_writelabel(gp, NULL);
|
|
|
|
g_topology_unlock();
|
|
|
|
PICKUP_GIANT();
|
|
|
|
g_free(label);
|
|
|
|
return(error);
|
|
|
|
}
|
2002-09-27 21:54:37 +00:00
|
|
|
default:
|
2003-09-01 20:45:32 +00:00
|
|
|
return (ENOIOCTL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
g_bsd_start(struct bio *bp)
|
|
|
|
{
|
|
|
|
struct g_geom *gp;
|
|
|
|
struct g_bsd_softc *ms;
|
|
|
|
struct g_slicer *gsp;
|
|
|
|
|
|
|
|
gp = bp->bio_to->geom;
|
|
|
|
gsp = gp->softc;
|
|
|
|
ms = gsp->softc;
|
|
|
|
if (bp->bio_cmd == BIO_GETATTR) {
|
|
|
|
if (g_handleattr(bp, "BSD::labelsum", ms->labelsum,
|
|
|
|
sizeof(ms->labelsum)))
|
|
|
|
return (1);
|
2002-03-16 09:24:19 +00:00
|
|
|
}
|
2003-09-01 20:45:32 +00:00
|
|
|
return (0);
|
2002-03-11 21:42:35 +00:00
|
|
|
}
|
|
|
|
|
2002-09-27 21:54:37 +00:00
|
|
|
/*
|
|
|
|
* Dump configuration information in XML format.
|
|
|
|
* Notice that the function is called once for the geom and once for each
|
|
|
|
* consumer and provider. We let g_slice_dumpconf() do most of the work.
|
|
|
|
*/
|
2002-03-11 21:42:35 +00:00
|
|
|
static void
|
2002-12-16 22:33:27 +00:00
|
|
|
g_bsd_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp)
|
2002-03-11 21:42:35 +00:00
|
|
|
{
|
2002-04-23 19:54:02 +00:00
|
|
|
struct g_bsd_softc *ms;
|
2002-03-11 21:42:35 +00:00
|
|
|
struct g_slicer *gsp;
|
|
|
|
|
|
|
|
gsp = gp->softc;
|
|
|
|
ms = gsp->softc;
|
2002-10-28 07:50:47 +00:00
|
|
|
g_slice_dumpconf(sb, indent, gp, cp, pp);
|
|
|
|
if (indent != NULL && pp == NULL && cp == NULL) {
|
2002-09-13 11:41:25 +00:00
|
|
|
sbuf_printf(sb, "%s<labeloffset>%jd</labeloffset>\n",
|
|
|
|
indent, (intmax_t)ms->labeloffset);
|
2002-10-28 07:50:47 +00:00
|
|
|
sbuf_printf(sb, "%s<rawoffset>%jd</rawoffset>\n",
|
|
|
|
indent, (intmax_t)ms->rawoffset);
|
|
|
|
sbuf_printf(sb, "%s<mbroffset>%jd</mbroffset>\n",
|
|
|
|
indent, (intmax_t)ms->mbroffset);
|
2003-01-10 19:44:14 +00:00
|
|
|
} else if (pp != NULL) {
|
|
|
|
if (indent == NULL)
|
|
|
|
sbuf_printf(sb, " ty %d",
|
2003-05-02 22:46:13 +00:00
|
|
|
ms->ondisk.d_partitions[pp->index].p_fstype);
|
2003-01-10 19:44:14 +00:00
|
|
|
else
|
|
|
|
sbuf_printf(sb, "%s<type>%d</type>\n", indent,
|
2003-05-02 22:46:13 +00:00
|
|
|
ms->ondisk.d_partitions[pp->index].p_fstype);
|
2002-03-11 21:42:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-09-27 21:54:37 +00:00
|
|
|
/*
|
|
|
|
* The taste function is called from the event-handler, with the topology
|
|
|
|
* lock already held and a provider to examine. The flags are unused.
|
|
|
|
*
|
|
|
|
* If flags == G_TF_NORMAL, the idea is to take a bite of the provider and
|
|
|
|
* if we find valid, consistent magic on it, build a geom on it.
|
|
|
|
*
|
|
|
|
* There may be cases where the operator would like to put a BSD-geom on
|
|
|
|
* providers which do not meet all of the requirements. This can be done
|
|
|
|
* by instead passing the G_TF_INSIST flag, which will override these
|
|
|
|
* checks.
|
|
|
|
*
|
|
|
|
* The final flags value is G_TF_TRANSPARENT, which instructs the method
|
|
|
|
* to put a geom on top of the provider and configure it to be as transparent
|
|
|
|
* as possible. This is not really relevant to the BSD method and therefore
|
|
|
|
* not implemented here.
|
|
|
|
*/
|
|
|
|
|
2007-12-06 09:20:27 +00:00
|
|
|
static struct uuid freebsd_slice = GPT_ENT_TYPE_FREEBSD;
|
|
|
|
|
2002-03-11 21:42:35 +00:00
|
|
|
static struct g_geom *
|
2002-03-26 22:07:38 +00:00
|
|
|
g_bsd_taste(struct g_class *mp, struct g_provider *pp, int flags)
|
2002-03-11 21:42:35 +00:00
|
|
|
{
|
|
|
|
struct g_geom *gp;
|
|
|
|
struct g_consumer *cp;
|
2002-09-27 21:54:37 +00:00
|
|
|
int error, i;
|
2002-03-11 21:42:35 +00:00
|
|
|
struct g_bsd_softc *ms;
|
|
|
|
u_int secsize;
|
2002-04-09 15:43:32 +00:00
|
|
|
struct g_slicer *gsp;
|
2003-01-06 20:10:41 +00:00
|
|
|
u_char hash[16];
|
2003-05-02 22:46:13 +00:00
|
|
|
MD5_CTX md5sum;
|
2007-12-06 09:20:27 +00:00
|
|
|
struct uuid uuid;
|
2002-03-11 21:42:35 +00:00
|
|
|
|
|
|
|
g_trace(G_T_TOPOLOGY, "bsd_taste(%s,%s)", mp->name, pp->name);
|
|
|
|
g_topology_assert();
|
2002-09-27 21:54:37 +00:00
|
|
|
|
2002-10-20 08:43:56 +00:00
|
|
|
/* We don't implement transparent inserts. */
|
2002-09-27 21:54:37 +00:00
|
|
|
if (flags == G_TF_TRANSPARENT)
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* BSD labels are a subclass of the general "slicing" topology so
|
|
|
|
* a lot of the work can be done by the common "slice" code.
|
|
|
|
* Create a geom with space for MAXPARTITIONS providers, one consumer
|
|
|
|
* and a softc structure for us. Specify the provider to attach
|
|
|
|
* the consumer to and our "start" routine for special requests.
|
|
|
|
* The provider is opened with mode (1,0,0) so we can do reads
|
|
|
|
* from it.
|
|
|
|
*/
|
|
|
|
gp = g_slice_new(mp, MAXPARTITIONS, pp, &cp, &ms,
|
2002-10-20 08:43:56 +00:00
|
|
|
sizeof(*ms), g_bsd_start);
|
2002-03-11 21:42:35 +00:00
|
|
|
if (gp == NULL)
|
|
|
|
return (NULL);
|
2002-09-27 21:54:37 +00:00
|
|
|
|
2002-10-20 08:43:56 +00:00
|
|
|
/* Get the geom_slicer softc from the geom. */
|
2002-09-27 21:54:37 +00:00
|
|
|
gsp = gp->softc;
|
2002-10-20 08:43:56 +00:00
|
|
|
|
2002-09-27 21:54:37 +00:00
|
|
|
/*
|
|
|
|
* The do...while loop here allows us to have multiple escapes
|
|
|
|
* using a simple "break". This improves code clarity without
|
|
|
|
* ending up in deep nesting and without using goto or come from.
|
|
|
|
*/
|
|
|
|
do {
|
|
|
|
/*
|
|
|
|
* If the provider is an MBR we will only auto attach
|
|
|
|
* to type 165 slices in the G_TF_NORMAL case. We will
|
2003-01-06 20:10:41 +00:00
|
|
|
* attach to any other type.
|
2002-09-27 21:54:37 +00:00
|
|
|
*/
|
2002-04-09 15:43:32 +00:00
|
|
|
error = g_getattr("MBR::type", cp, &i);
|
2002-12-02 20:23:54 +00:00
|
|
|
if (!error) {
|
|
|
|
if (i != 165 && flags == G_TF_NORMAL)
|
|
|
|
break;
|
|
|
|
error = g_getattr("MBR::offset", cp, &ms->mbroffset);
|
|
|
|
if (error)
|
|
|
|
break;
|
|
|
|
}
|
2002-09-27 21:54:37 +00:00
|
|
|
|
2002-12-02 20:23:54 +00:00
|
|
|
/* Same thing if we are inside a PC98 */
|
2002-11-07 16:42:37 +00:00
|
|
|
error = g_getattr("PC98::type", cp, &i);
|
2002-12-02 20:23:54 +00:00
|
|
|
if (!error) {
|
|
|
|
if (i != 0xc494 && flags == G_TF_NORMAL)
|
|
|
|
break;
|
|
|
|
error = g_getattr("PC98::offset", cp, &ms->mbroffset);
|
|
|
|
if (error)
|
|
|
|
break;
|
|
|
|
}
|
2002-10-28 07:50:47 +00:00
|
|
|
|
2007-12-06 09:20:27 +00:00
|
|
|
/* Same thing if we are inside a GPT */
|
|
|
|
error = g_getattr("GPT::type", cp, &uuid);
|
|
|
|
if (!error) {
|
|
|
|
if (memcmp(&uuid, &freebsd_slice, sizeof(uuid)) != 0 &&
|
|
|
|
flags == G_TF_NORMAL)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2002-09-27 21:54:37 +00:00
|
|
|
/* Get sector size, we need it to read data. */
|
2002-10-20 20:28:24 +00:00
|
|
|
secsize = cp->provider->sectorsize;
|
|
|
|
if (secsize < 512)
|
2002-09-27 21:54:37 +00:00
|
|
|
break;
|
|
|
|
|
2002-10-20 08:43:56 +00:00
|
|
|
/* First look for a label at the start of the second sector. */
|
2002-12-02 20:23:54 +00:00
|
|
|
error = g_bsd_try(gp, gsp, cp, secsize, ms, secsize);
|
2002-09-27 21:54:37 +00:00
|
|
|
|
2005-11-30 22:54:41 +00:00
|
|
|
/*
|
|
|
|
* If sector size is not 512 the label still can be at
|
|
|
|
* offset 512, not at the start of the second sector. At least
|
|
|
|
* it's true for labels created by the FreeBSD's bsdlabel(8).
|
|
|
|
*/
|
|
|
|
if (error && secsize != HISTORIC_LABEL_OFFSET)
|
|
|
|
error = g_bsd_try(gp, gsp, cp, secsize, ms,
|
|
|
|
HISTORIC_LABEL_OFFSET);
|
|
|
|
|
2002-11-08 15:31:34 +00:00
|
|
|
/* Next, look for alpha labels */
|
2002-09-06 08:50:28 +00:00
|
|
|
if (error)
|
2002-12-02 20:23:54 +00:00
|
|
|
error = g_bsd_try(gp, gsp, cp, secsize, ms,
|
2002-11-08 15:31:34 +00:00
|
|
|
ALPHA_LABEL_OFFSET);
|
2002-09-27 21:54:37 +00:00
|
|
|
|
2002-10-20 08:43:56 +00:00
|
|
|
/* If we didn't find a label, punt. */
|
2002-03-24 14:27:31 +00:00
|
|
|
if (error)
|
2002-03-11 21:42:35 +00:00
|
|
|
break;
|
2002-09-27 21:54:37 +00:00
|
|
|
|
2003-01-06 20:10:41 +00:00
|
|
|
/*
|
|
|
|
* In order to avoid recursively attaching to the same
|
|
|
|
* on-disk label (it's usually visible through the 'c'
|
|
|
|
* partition) we calculate an MD5 and ask if other BSD's
|
|
|
|
* below us love that label. If they do, we don't.
|
|
|
|
*/
|
|
|
|
MD5Init(&md5sum);
|
2003-05-02 22:46:13 +00:00
|
|
|
MD5Update(&md5sum, ms->label, sizeof(ms->label));
|
2003-01-06 20:10:41 +00:00
|
|
|
MD5Final(ms->labelsum, &md5sum);
|
|
|
|
|
|
|
|
error = g_getattr("BSD::labelsum", cp, &hash);
|
2003-06-01 09:18:49 +00:00
|
|
|
if (!error && !bcmp(ms->labelsum, hash, sizeof(hash)))
|
2003-01-06 20:10:41 +00:00
|
|
|
break;
|
|
|
|
|
2002-09-27 21:54:37 +00:00
|
|
|
/*
|
2002-10-20 08:43:56 +00:00
|
|
|
* Process the found disklabel, and modify our "slice"
|
2002-09-27 21:54:37 +00:00
|
|
|
* instance to match it, if possible.
|
|
|
|
*/
|
2003-05-02 22:46:13 +00:00
|
|
|
error = g_bsd_modify(gp, ms->label);
|
2002-09-27 21:54:37 +00:00
|
|
|
} while (0);
|
|
|
|
|
2003-01-13 08:44:03 +00:00
|
|
|
/* Success or failure, we can close our provider now. */
|
2004-02-14 17:59:44 +00:00
|
|
|
g_access(cp, -1, 0, 0);
|
2002-09-27 21:54:37 +00:00
|
|
|
|
|
|
|
/* If we have configured any providers, return the new geom. */
|
2003-05-02 22:46:13 +00:00
|
|
|
if (gsp->nprovider > 0) {
|
|
|
|
g_slice_conf_hot(gp, 0, ms->labeloffset, LABELSIZE,
|
|
|
|
G_SLICE_HOT_ALLOW, G_SLICE_HOT_DENY, G_SLICE_HOT_CALL);
|
|
|
|
gsp->hot = g_bsd_hotwrite;
|
2002-03-11 21:42:35 +00:00
|
|
|
return (gp);
|
2003-05-02 22:46:13 +00:00
|
|
|
}
|
2002-09-27 21:54:37 +00:00
|
|
|
/*
|
|
|
|
* ...else push the "self-destruct" button, by spoiling our own
|
2003-05-02 06:33:59 +00:00
|
|
|
* consumer. This triggers a call to g_slice_spoiled which will
|
2002-09-27 21:54:37 +00:00
|
|
|
* dismantle what was setup.
|
|
|
|
*/
|
2003-05-02 06:33:59 +00:00
|
|
|
g_slice_spoiled(cp);
|
2002-03-11 21:42:35 +00:00
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
2003-05-03 08:01:34 +00:00
|
|
|
struct h0h0 {
|
|
|
|
struct g_geom *gp;
|
|
|
|
struct g_bsd_softc *ms;
|
|
|
|
u_char *label;
|
|
|
|
int error;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
g_bsd_callconfig(void *arg, int flag)
|
|
|
|
{
|
|
|
|
struct h0h0 *hp;
|
|
|
|
|
|
|
|
hp = arg;
|
|
|
|
hp->error = g_bsd_modify(hp->gp, hp->label);
|
|
|
|
if (!hp->error)
|
|
|
|
hp->error = g_bsd_writelabel(hp->gp, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* NB! curthread is user process which GCTL'ed.
|
|
|
|
*/
|
2003-06-01 13:47:51 +00:00
|
|
|
static void
|
|
|
|
g_bsd_config(struct gctl_req *req, struct g_class *mp, char const *verb)
|
2003-05-03 08:01:34 +00:00
|
|
|
{
|
|
|
|
u_char *label;
|
2003-05-31 19:30:52 +00:00
|
|
|
int error;
|
2003-05-03 08:01:34 +00:00
|
|
|
struct h0h0 h0h0;
|
2003-06-01 13:47:51 +00:00
|
|
|
struct g_geom *gp;
|
2003-05-03 08:01:34 +00:00
|
|
|
struct g_slicer *gsp;
|
|
|
|
struct g_consumer *cp;
|
2003-05-04 19:25:48 +00:00
|
|
|
struct g_bsd_softc *ms;
|
2003-05-03 08:01:34 +00:00
|
|
|
|
|
|
|
g_topology_assert();
|
2003-06-01 13:47:51 +00:00
|
|
|
gp = gctl_get_geom(req, mp, "geom");
|
|
|
|
if (gp == NULL)
|
|
|
|
return;
|
2003-05-03 08:01:34 +00:00
|
|
|
cp = LIST_FIRST(&gp->consumer);
|
|
|
|
gsp = gp->softc;
|
2003-05-04 19:25:48 +00:00
|
|
|
ms = gsp->softc;
|
|
|
|
if (!strcmp(verb, "read mbroffset")) {
|
2006-04-07 16:19:48 +00:00
|
|
|
gctl_set_param_err(req, "mbroffset", &ms->mbroffset,
|
|
|
|
sizeof(ms->mbroffset));
|
2003-06-01 13:47:51 +00:00
|
|
|
return;
|
2003-05-04 19:25:48 +00:00
|
|
|
} else if (!strcmp(verb, "write label")) {
|
2003-05-03 08:01:34 +00:00
|
|
|
label = gctl_get_paraml(req, "label", LABELSIZE);
|
|
|
|
if (label == NULL)
|
2003-06-01 13:47:51 +00:00
|
|
|
return;
|
2003-05-03 08:01:34 +00:00
|
|
|
h0h0.gp = gp;
|
|
|
|
h0h0.ms = gsp->softc;
|
|
|
|
h0h0.label = label;
|
|
|
|
h0h0.error = -1;
|
|
|
|
/* XXX: Does this reference register with our selfdestruct code ? */
|
2004-02-12 22:42:11 +00:00
|
|
|
error = g_access(cp, 1, 1, 1);
|
2003-05-03 08:01:34 +00:00
|
|
|
if (error) {
|
2003-06-01 13:47:51 +00:00
|
|
|
gctl_error(req, "could not access consumer");
|
|
|
|
return;
|
2003-05-03 08:01:34 +00:00
|
|
|
}
|
2003-06-01 13:47:51 +00:00
|
|
|
g_bsd_callconfig(&h0h0, 0);
|
2003-05-03 08:01:34 +00:00
|
|
|
error = h0h0.error;
|
2004-02-12 22:42:11 +00:00
|
|
|
g_access(cp, -1, -1, -1);
|
2003-05-03 08:01:34 +00:00
|
|
|
} else if (!strcmp(verb, "write bootcode")) {
|
|
|
|
label = gctl_get_paraml(req, "bootcode", BBSIZE);
|
|
|
|
if (label == NULL)
|
2003-06-01 13:47:51 +00:00
|
|
|
return;
|
2003-05-03 08:01:34 +00:00
|
|
|
/* XXX: Does this reference register with our selfdestruct code ? */
|
2004-02-12 22:42:11 +00:00
|
|
|
error = g_access(cp, 1, 1, 1);
|
2003-05-03 08:01:34 +00:00
|
|
|
if (error) {
|
2003-06-01 13:47:51 +00:00
|
|
|
gctl_error(req, "could not access consumer");
|
|
|
|
return;
|
2003-05-03 08:01:34 +00:00
|
|
|
}
|
|
|
|
error = g_bsd_writelabel(gp, label);
|
2004-02-12 22:42:11 +00:00
|
|
|
g_access(cp, -1, -1, -1);
|
2003-05-03 08:01:34 +00:00
|
|
|
} else {
|
2003-06-01 13:47:51 +00:00
|
|
|
gctl_error(req, "Unknown verb parameter");
|
2003-05-03 08:01:34 +00:00
|
|
|
}
|
|
|
|
|
2003-06-01 13:47:51 +00:00
|
|
|
return;
|
2003-05-03 08:01:34 +00:00
|
|
|
}
|
|
|
|
|
2002-10-20 08:43:56 +00:00
|
|
|
/* Finally, register with GEOM infrastructure. */
|
2002-09-27 21:54:37 +00:00
|
|
|
static struct g_class g_bsd_class = {
|
2003-03-24 19:30:15 +00:00
|
|
|
.name = BSD_CLASS_NAME,
|
2004-08-08 07:57:53 +00:00
|
|
|
.version = G_VERSION,
|
2003-03-24 19:30:15 +00:00
|
|
|
.taste = g_bsd_taste,
|
2003-06-01 13:47:51 +00:00
|
|
|
.ctlreq = g_bsd_config,
|
2004-08-08 06:49:07 +00:00
|
|
|
.dumpconf = g_bsd_dumpconf,
|
|
|
|
.ioctl = g_bsd_ioctl,
|
2002-03-11 21:42:35 +00:00
|
|
|
};
|
|
|
|
|
2002-03-26 21:40:06 +00:00
|
|
|
DECLARE_GEOM_CLASS(g_bsd_class, g_bsd);
|