From: ccardenas Date: Tue, 11 Sep 2018 04:06:32 +0000 (+0000) Subject: Fail fast when we are unable to determine disk format. X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=2919bad868e89c86058c8c59e8dc24fb9eff879f;p=openbsd Fail fast when we are unable to determine disk format. While here, minor cleanup on logging. --- diff --git a/usr.sbin/vmd/vioqcow2.c b/usr.sbin/vmd/vioqcow2.c index a893366bc3e..7c7e4857695 100644 --- a/usr.sbin/vmd/vioqcow2.c +++ b/usr.sbin/vmd/vioqcow2.c @@ -1,4 +1,4 @@ -/* $OpenBSD: vioqcow2.c,v 1.1 2018/09/09 04:09:32 ccardenas Exp $ */ +/* $OpenBSD: vioqcow2.c,v 1.2 2018/09/11 04:06:32 ccardenas Exp $ */ /* * Copyright (c) 2018 Ori Bernstein @@ -127,7 +127,7 @@ virtio_init_qcow2(struct virtio_backing *file, off_t *szp, int fd) if (diskp == NULL) return -1; if (qc2_open(diskp, fd) == -1) { - log_warnx("could not open qcow2 disk"); + log_warnx("%s: could not open qcow2 disk", __func__); free(diskp); return -1; } @@ -162,11 +162,11 @@ qc2_open(struct qcdisk *disk, int fd) int version; if (pread(fd, &header, sizeof header, 0) != sizeof header) { - log_warn("short read on header"); + log_warn("%s: short read on header", __func__); return -1; } if (strncmp(header.magic, "QFI\xfb", 4) != 0) { - log_warn("invalid magic numbers"); + log_warn("%s: invalid magic numbers", __func__); return -1; } pthread_rwlock_init(&disk->lock, NULL); @@ -196,7 +196,7 @@ qc2_open(struct qcdisk *disk, int fd) * We only know about the dirty or corrupt bits here. */ if (disk->incompatfeatures & ~(QCOW2_DIRTY|QCOW2_CORRUPT)) { - log_warn("%s: unsupported features %llx", __progname, + log_warn("%s: unsupported features %llx", __func__, disk->incompatfeatures & ~(QCOW2_DIRTY|QCOW2_CORRUPT)); return -1; } @@ -211,7 +211,7 @@ qc2_open(struct qcdisk *disk, int fd) disk->l1[i] = be64toh(disk->l1[i]); version = be32toh(header.version); if (version != 2 && version != 3) { - log_warn("%s: unknown qcow2 version %d", __progname, version); + log_warn("%s: unknown qcow2 version %d", __func__, version); return -1; } @@ -222,16 +222,16 @@ qc2_open(struct qcdisk *disk, int fd) * FIXME: we need to figure out a way of opening these things, * otherwise we just crash with a pledge violation. */ - log_warn("unsupported external snapshot images"); + log_warn("%s: unsupported external snapshot images", __func__); return -1; if (backingsz >= sizeof basepath - 1) { - log_warn("%s: snapshot path too long", __progname); + log_warn("%s: snapshot path too long", __func__); return -1; } if (pread(fd, basepath, backingsz, backingoff) != backingsz) { log_warn("%s: could not read snapshot base name", - __progname); + __func__); return -1; } basepath[backingsz] = 0; @@ -243,7 +243,7 @@ qc2_open(struct qcdisk *disk, int fd) } if (disk->base->clustersz != disk->clustersz) { log_warn("%s: all disks must share clustersize", - __progname); + __func__); free(disk->base); return -1; } @@ -414,7 +414,7 @@ xlate(struct qcdisk *disk, off_t off, int *inplace) if (inplace) *inplace = !!(cluster & QCOW2_INPLACE); if (cluster & QCOW2_COMPRESSED) { - log_warn("%s: compressed clusters unsupported", __progname); + log_warn("%s: compressed clusters unsupported", __func__); goto err; } pthread_rwlock_unlock(&disk->lock); @@ -556,7 +556,7 @@ inc_refs(struct qcdisk *disk, off_t off, int newcluster) l2cluster = disk->end; disk->end += disk->clustersz; if (ftruncate(disk->fd, disk->end) < 0) { - log_warn("refs block grow fail "); + log_warn("%s: refs block grow fail", __func__); return -1; } buf = htobe64(l2cluster); @@ -573,7 +573,8 @@ inc_refs(struct qcdisk *disk, off_t off, int newcluster) } refs = htobe16(refs); if (pwrite(disk->fd, &refs, sizeof refs, l2cluster + 2*l2idx) != 2) { - log_warn("could not write ref block"); + log_warn("%s: could not write ref block", __func__); + return -1; } return 0; } diff --git a/usr.sbin/vmd/virtio.c b/usr.sbin/vmd/virtio.c index 37faa8a0c9a..9e9c7fc0f21 100644 --- a/usr.sbin/vmd/virtio.c +++ b/usr.sbin/vmd/virtio.c @@ -1,4 +1,4 @@ -/* $OpenBSD: virtio.c,v 1.65 2018/09/09 04:09:32 ccardenas Exp $ */ +/* $OpenBSD: virtio.c,v 1.66 2018/09/11 04:06:32 ccardenas Exp $ */ /* * Copyright (c) 2015 Mike Larkin @@ -1756,7 +1756,7 @@ virtio_init_disk(struct virtio_backing *file, off_t *sz, int fd, int type) case VMDF_RAW: return virtio_init_raw(file, sz, fd); case VMDF_QCOW2: return virtio_init_qcow2(file, sz, fd); } - log_warnx("%s: invalid disk format", __progname); + log_warnx("%s: invalid disk format", __func__); return -1; } @@ -1838,8 +1838,11 @@ virtio_init(struct vmd_vm *vm, int child_cdrom, int *child_disks, vioblk[i].vm_id = vcp->vcp_id; vioblk[i].irq = pci_get_dev_irq(id); if (virtio_init_disk(&vioblk[i].file, &vioblk[i].sz, - child_disks[i], vmc->vmc_disktypes[i]) == -1) - continue; + child_disks[i], vmc->vmc_disktypes[i]) == -1) { + log_warnx("%s: unable to determine disk fmt", + __func__); + return; + } vioblk[i].sz /= 512; } } @@ -1964,8 +1967,10 @@ virtio_init(struct vmd_vm *vm, int child_cdrom, int *child_disks, vioscsi->vq[i].last_avail = 0; } if (virtio_init_disk(&vioscsi->file, &vioscsi->sz, - child_cdrom, VMDF_RAW) == -1) + child_cdrom, VMDF_RAW) == -1) { + log_warnx("%s: unable to determine iso fmt", __func__); return; + } vioscsi->locked = 0; vioscsi->lba = 0; vioscsi->n_blocks = vioscsi->sz >> 11; /* num of 2048 blocks in file */ @@ -2129,8 +2134,11 @@ vioblk_restore(int fd, struct vmop_create_params *vmc, int *child_disks) return (-1); } if (virtio_init_disk(&vioblk[i].file, &vioblk[i].sz, - child_disks[i], vmc->vmc_disktypes[i]) == -1) - continue; + child_disks[i], vmc->vmc_disktypes[i]) == -1) { + log_warnx("%s: unable to determine disk fmt", + __func__); + return (-1); + } } return (0); } @@ -2161,7 +2169,11 @@ vioscsi_restore(int fd, struct vm_create_params *vcp, int child_cdrom) return (-1); } - virtio_init_disk(&vioscsi->file, &vioscsi->sz, child_cdrom, VMDF_RAW); + if (virtio_init_disk(&vioscsi->file, &vioscsi->sz, child_cdrom, + VMDF_RAW) == -1) { + log_warnx("%s: unable to determine iso fmt", __func__); + return (-1); + } return (0); }