#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/conf.h>
#include <sys/disklabel.h>
#include <sys/disklabel64.h>
#include <sys/diskslice.h>
#include <sys/disk.h>
#include <sys/kern_syscall.h>
#include <sys/buf2.h>
#define PALIGN_SIZE (1024 * 1024)
#define PALIGN_MASK (PALIGN_SIZE - 1)
static int
l64_getpartbounds(struct diskslices *ssp, disklabel_t lp, u_int32_t part,
u_int64_t *start, u_int64_t *blocks)
{
struct partition64 *pp;
if (part >= lp.lab64->d_npartitions)
return (EINVAL);
pp = &lp.lab64->d_partitions[part];
if ((pp->p_boffset & (ssp->dss_secsize - 1)) ||
(pp->p_bsize & (ssp->dss_secsize - 1))) {
return (EINVAL);
}
*start = pp->p_boffset / ssp->dss_secsize;
*blocks = pp->p_bsize / ssp->dss_secsize;
return(0);
}
static void
l64_loadpartinfo(disklabel_t lp, u_int32_t part, struct partinfo *dpart)
{
struct partition64 *pp;
const size_t uuid_size = sizeof(struct uuid);
if (part < lp.lab64->d_npartitions) {
pp = &lp.lab64->d_partitions[part];
dpart->fstype_uuid = pp->p_type_uuid;
dpart->storage_uuid = pp->p_stor_uuid;
dpart->fstype = pp->p_fstype;
} else {
bzero(&dpart->fstype_uuid, uuid_size);
bzero(&dpart->storage_uuid, uuid_size);
dpart->fstype = 0;
}
}
static u_int32_t
l64_getnumparts(disklabel_t lp)
{
return(lp.lab64->d_npartitions);
}
static int
l64_getpackname(disklabel_t lp, char *buf, size_t bytes)
{
size_t slen;
if (lp.lab64->d_packname[0] == 0) {
buf[0] = 0;
return -1;
}
slen = strnlen(lp.lab64->d_packname, sizeof(lp.lab64->d_packname));
if (slen >= bytes)
slen = bytes - 1;
bcopy(lp.lab64->d_packname, buf, slen);
buf[slen] = 0;
return 0;
}
static void
l64_freedisklabel(disklabel_t *lpp)
{
kfree((*lpp).lab64, M_DEVBUF);
(*lpp).lab64 = NULL;
}
static const char *
l64_readdisklabel(cdev_t dev, struct diskslice *sp, disklabel_t *lpp,
struct disk_info *info)
{
struct buf *bp;
struct disklabel64 *dlp;
const char *msg;
uint32_t savecrc;
size_t dlpcrcsize;
size_t bpsize;
int secsize;
secsize = info->d_media_blksize;
bpsize = roundup2(sizeof(*dlp), secsize);
bp = getpbuf_mem(NULL);
KKASSERT(bpsize <= bp->b_bufsize);
bp->b_bio1.bio_offset = 0;
bp->b_bio1.bio_done = biodone_sync;
bp->b_bio1.bio_flags |= BIO_SYNC;
bp->b_bcount = bpsize;
bp->b_flags &= ~B_INVAL;
bp->b_flags |= B_FAILONDIS;
bp->b_cmd = BUF_CMD_READ;
dev_dstrategy(dev, &bp->b_bio1);
if (biowait(&bp->b_bio1, "labrd")) {
msg = "I/O error";
} else {
dlp = (struct disklabel64 *)bp->b_data;
dlpcrcsize = offsetof(struct disklabel64,
d_partitions[dlp->d_npartitions]) -
offsetof(struct disklabel64, d_magic);
savecrc = dlp->d_crc;
dlp->d_crc = 0;
if (dlp->d_magic != DISKMAGIC64) {
msg = "no disk label";
} else if (dlp->d_npartitions > MAXPARTITIONS64) {
msg = "disklabel64 corrupted, too many partitions";
} else if (savecrc != crc32(&dlp->d_magic, dlpcrcsize)) {
msg = "disklabel64 corrupted, bad CRC";
} else {
dlp->d_crc = savecrc;
(*lpp).lab64 = kmalloc(sizeof(*dlp),
M_DEVBUF, M_WAITOK|M_ZERO);
*(*lpp).lab64 = *dlp;
msg = NULL;
}
}
bp->b_flags |= B_INVAL | B_AGE;
relpbuf(bp, NULL);
return (msg);
}
static int
l64_setdisklabel(disklabel_t olpx, disklabel_t nlpx, struct diskslices *ssp,
struct diskslice *sp, u_int32_t *openmask)
{
struct disklabel64 *olp, *nlp;
struct partition64 *opp, *npp;
uint32_t savecrc;
uint64_t slicebsize;
size_t nlpcrcsize;
int i;
olp = olpx.lab64;
nlp = nlpx.lab64;
slicebsize = (uint64_t)sp->ds_size * ssp->dss_secsize;
if (nlp->d_magic != DISKMAGIC64)
return (EINVAL);
if (nlp->d_npartitions > MAXPARTITIONS64)
return (EINVAL);
savecrc = nlp->d_crc;
nlp->d_crc = 0;
nlpcrcsize = offsetof(struct disklabel64,
d_partitions[nlp->d_npartitions]) -
offsetof(struct disklabel64, d_magic);
if (crc32(&nlp->d_magic, nlpcrcsize) != savecrc) {
nlp->d_crc = savecrc;
return (EINVAL);
}
nlp->d_crc = savecrc;
i = 0;
while (i < MAXPARTITIONS64) {
if (openmask[i >> 5] == 0) {
i += 32;
continue;
}
if ((openmask[i >> 5] & (1 << (i & 31))) == 0) {
++i;
continue;
}
if (nlp->d_npartitions <= i)
return (EBUSY);
opp = &olp->d_partitions[i];
npp = &nlp->d_partitions[i];
if (npp->p_boffset != opp->p_boffset ||
npp->p_bsize < opp->p_bsize) {
return (EBUSY);
}
if (bcmp(&npp->p_type_uuid, &opp->p_type_uuid,
sizeof(npp->p_type_uuid)) != 0) {
return (EBUSY);
}
if (bcmp(&npp->p_stor_uuid, &opp->p_stor_uuid,
sizeof(npp->p_stor_uuid)) != 0) {
return (EBUSY);
}
++i;
}
if (nlp->d_total_size > slicebsize)
return (ENOSPC);
if (nlp->d_total_size & (ssp->dss_secsize - 1))
return (EINVAL);
if (nlp->d_bbase & (ssp->dss_secsize - 1))
return (EINVAL);
if (nlp->d_pbase & (ssp->dss_secsize - 1))
return (EINVAL);
if (nlp->d_pstop & (ssp->dss_secsize - 1))
return (EINVAL);
if (nlp->d_abase & (ssp->dss_secsize - 1))
return (EINVAL);
for (i = 0; i < nlp->d_npartitions; ++i) {
npp = &nlp->d_partitions[i];
if (npp->p_bsize == 0) {
if (npp->p_boffset != 0)
return (EINVAL);
continue;
}
if (npp->p_boffset & (ssp->dss_secsize - 1))
return (EINVAL);
if (npp->p_bsize & (ssp->dss_secsize - 1))
return (EINVAL);
if (npp->p_boffset < nlp->d_pbase)
return (ENOSPC);
if (npp->p_boffset + npp->p_bsize > nlp->d_total_size)
return (ENOSPC);
}
nlp->d_crc = 0;
nlp->d_crc = crc32(&nlp->d_magic, nlpcrcsize);
*olp = *nlp;
return (0);
}
static int
l64_writedisklabel(cdev_t dev, struct diskslices *ssp,
struct diskslice *sp, disklabel_t lpx)
{
struct disklabel64 *lp;
struct disklabel64 *dlp;
struct buf *bp;
int error = 0;
size_t bpsize;
int secsize;
lp = lpx.lab64;
secsize = ssp->dss_secsize;
bpsize = roundup2(sizeof(*lp), secsize);
bp = getpbuf_mem(NULL);
KKASSERT(bpsize <= bp->b_bufsize);
bp->b_bio1.bio_offset = 0;
bp->b_bio1.bio_done = biodone_sync;
bp->b_bio1.bio_flags |= BIO_SYNC;
bp->b_bcount = bpsize;
bp->b_flags |= B_FAILONDIS;
bp->b_flags &= ~B_INVAL;
bp->b_cmd = BUF_CMD_READ;
KKASSERT(dkpart(dev) == WHOLE_SLICE_PART);
dev_dstrategy(dev, &bp->b_bio1);
error = biowait(&bp->b_bio1, "labrd");
if (error)
goto done;
dlp = (void *)bp->b_data;
bcopy(&lp->d_magic, &dlp->d_magic,
sizeof(*lp) - offsetof(struct disklabel64, d_magic));
bp->b_cmd = BUF_CMD_WRITE;
bp->b_bio1.bio_done = biodone_sync;
bp->b_bio1.bio_flags |= BIO_SYNC;
KKASSERT(dkpart(dev) == WHOLE_SLICE_PART);
dev_dstrategy(dev, &bp->b_bio1);
error = biowait(&bp->b_bio1, "labwr");
done:
bp->b_flags |= B_INVAL | B_AGE;
relpbuf(bp, NULL);
return (error);
}
static disklabel_t
l64_clone_label(struct disk_info *info, struct diskslice *sp)
{
struct disklabel64 *lp;
disklabel_t res;
uint32_t blksize = info->d_media_blksize;
size_t lpcrcsize;
lp = kmalloc(sizeof *lp, M_DEVBUF, M_WAITOK | M_ZERO);
if (sp)
lp->d_total_size = (uint64_t)sp->ds_size * blksize;
else
lp->d_total_size = info->d_media_blocks * blksize;
lp->d_magic = DISKMAGIC64;
lp->d_align = blksize;
lp->d_npartitions = MAXPARTITIONS64;
lp->d_pstop = lp->d_total_size;
lp->d_partitions[2].p_boffset = 0;
lp->d_partitions[2].p_bsize = lp->d_total_size;
if (info->d_dsflags & DSO_COMPATPARTA) {
lp->d_partitions[0].p_boffset = 0;
lp->d_partitions[0].p_bsize = lp->d_total_size;
}
lpcrcsize = offsetof(struct disklabel64,
d_partitions[lp->d_npartitions]) -
offsetof(struct disklabel64, d_magic);
lp->d_crc = crc32(&lp->d_magic, lpcrcsize);
res.lab64 = lp;
return (res);
}
static void
l64_makevirginlabel(disklabel_t lpx, struct diskslices *ssp,
struct diskslice *sp, struct disk_info *info)
{
struct disklabel64 *lp = lpx.lab64;
struct partition64 *pp;
uint32_t blksize;
uint32_t ressize;
uint64_t blkmask;
uint64_t doffset;
size_t lpcrcsize;
doffset = sp->ds_offset * info->d_media_blksize;
bzero(lp, sizeof(*lp));
if ((blksize = info->d_media_blksize) < 4096)
blksize = 4096;
blkmask = blksize - 1;
if (sp)
lp->d_total_size = (uint64_t)sp->ds_size * ssp->dss_secsize;
else
lp->d_total_size = info->d_media_blocks * info->d_media_blksize;
lp->d_magic = DISKMAGIC64;
lp->d_align = blksize;
lp->d_npartitions = MAXPARTITIONS64;
kern_uuidgen(&lp->d_stor_uuid, 1);
ressize = offsetof(struct disklabel64, d_partitions[RESPARTITIONS64]);
ressize = (ressize + (uint32_t)blkmask) & ~blkmask;
lp->d_bbase = ressize;
lp->d_pbase = lp->d_bbase + ((BOOT2SIZE64 + blkmask) & ~blkmask);
lp->d_abase = lp->d_total_size - ressize;
lp->d_pbase = ((doffset + lp->d_pbase + PALIGN_MASK) &
~(uint64_t)PALIGN_MASK) - doffset;
lp->d_pstop = ((lp->d_abase - lp->d_pbase) &
~(uint64_t)PALIGN_MASK) + lp->d_pbase;
if (info->d_dsflags & DSO_COMPATPARTA) {
pp = &lp->d_partitions[0];
pp->p_boffset = lp->d_pbase;
pp->p_bsize = lp->d_pstop - lp->d_pbase;
}
lpcrcsize = offsetof(struct disklabel64,
d_partitions[lp->d_npartitions]) -
offsetof(struct disklabel64, d_magic);
lp->d_crc = crc32(&lp->d_magic, lpcrcsize);
}
static void
l64_adjust_label_reserved(struct diskslices *ssp, int slice,
struct diskslice *sp)
{
struct disklabel64 *lp = sp->ds_label.lab64;
sp->ds_reserved = lp->d_bbase / ssp->dss_secsize;
}
struct disklabel_ops disklabel64_ops = {
.labelsize = sizeof(struct disklabel64),
.op_readdisklabel = l64_readdisklabel,
.op_setdisklabel = l64_setdisklabel,
.op_writedisklabel = l64_writedisklabel,
.op_clone_label = l64_clone_label,
.op_adjust_label_reserved = l64_adjust_label_reserved,
.op_getpartbounds = l64_getpartbounds,
.op_loadpartinfo = l64_loadpartinfo,
.op_getnumparts = l64_getnumparts,
.op_getpackname = l64_getpackname,
.op_makevirginlabel = l64_makevirginlabel,
.op_freedisklabel = l64_freedisklabel
};