From patchwork Tue Nov 15 13:02:51 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Huth X-Patchwork-Id: 695022 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [103.22.144.68]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3tJ6y50271z9t26 for ; Wed, 16 Nov 2016 00:03:08 +1100 (AEDT) Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 3tJ6y44ytLzDsxl for ; Wed, 16 Nov 2016 00:03:08 +1100 (AEDT) X-Original-To: slof@lists.ozlabs.org Delivered-To: slof@lists.ozlabs.org Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 3tJ6xv6SqrzDsxl for ; Wed, 16 Nov 2016 00:02:59 +1100 (AEDT) Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 4ECF9A73A9; Tue, 15 Nov 2016 13:02:58 +0000 (UTC) Received: from thh440s.redhat.com (ovpn-116-123.ams2.redhat.com [10.36.116.123]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id uAFD2qAG031445; Tue, 15 Nov 2016 08:02:57 -0500 From: Thomas Huth To: slof@lists.ozlabs.org Date: Tue, 15 Nov 2016 14:02:51 +0100 Message-Id: <1479214972-19153-4-git-send-email-thuth@redhat.com> In-Reply-To: <1479214972-19153-1-git-send-email-thuth@redhat.com> References: <1479214972-19153-1-git-send-email-thuth@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Tue, 15 Nov 2016 13:02:58 +0000 (UTC) Subject: [SLOF] [PATCH v2 3/4] virtio: Implement block write support X-BeenThere: slof@lists.ozlabs.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "Patches for https://github.com/aik/SLOF" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: slof-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: "SLOF" Refactor the virtio-block code a little bit to provide block write access, too. Write access to the first 34 sectors is not allowed, though, to avoid that the user / client program accidentially destroys the partition table. Signed-off-by: Thomas Huth Reviewed-by: Nikunj A Dadhania --- board-qemu/slof/virtio-block.fs | 13 +++++++++++++ lib/libvirtio/virtio-blk.c | 27 +++++++++++++++------------ lib/libvirtio/virtio-blk.h | 3 ++- lib/libvirtio/virtio.code | 11 ++++++++++- lib/libvirtio/virtio.in | 1 + 5 files changed, 41 insertions(+), 14 deletions(-) diff --git a/board-qemu/slof/virtio-block.fs b/board-qemu/slof/virtio-block.fs index bc9013e..b3065b2 100644 --- a/board-qemu/slof/virtio-block.fs +++ b/board-qemu/slof/virtio-block.fs @@ -49,6 +49,15 @@ virtio-setup-vd VALUE virtiodev virtiodev virtio-blk-read ; +: write-blocks ( addr block# #blocks -- #written ) + \ Do not allow writes to the partition table (GPT is in first 34 sectors) + over 22 < IF + ." virtio-blk ERROR: Write access to partition table is not allowed." cr + 3drop 0 EXIT + THEN + virtiodev virtio-blk-write +; + \ Standard node "open" function : open ( -- okay? ) open 0= IF false EXIT THEN @@ -79,6 +88,10 @@ virtio-setup-vd VALUE virtiodev s" read" deblocker @ $call-method ; +: write ( addr len -- actual ) + s" write" deblocker @ $call-method +; + \ Set disk alias if none is set yet : (set-alias) s" disk" get-next-alias ?dup IF diff --git a/lib/libvirtio/virtio-blk.c b/lib/libvirtio/virtio-blk.c index 07ec104..cdfdd10 100644 --- a/lib/libvirtio/virtio-blk.c +++ b/lib/libvirtio/virtio-blk.c @@ -112,15 +112,17 @@ static void fill_blk_hdr(struct virtio_blk_req *blkhdr, bool is_modern, } /** - * Read blocks + * Read / write blocks * @param reg pointer to "reg" property * @param buf pointer to destination buffer - * @param blocknum block number of the first block that should be read - * @param cnt amount of blocks that should be read - * @return number of blocks that have been read successfully + * @param blocknum block number of the first block that should be transfered + * @param cnt amount of blocks that should be transfered + * @param type VIRTIO_BLK_T_OUT for write, VIRTIO_BLK_T_IN for read transfers + * @return number of blocks that have been transfered successfully */ int -virtioblk_read(struct virtio_device *dev, char *buf, uint64_t blocknum, long cnt) +virtioblk_transfer(struct virtio_device *dev, char *buf, uint64_t blocknum, + long cnt, unsigned int type) { struct vring_desc *desc; int id; @@ -136,15 +138,15 @@ virtioblk_read(struct virtio_device *dev, char *buf, uint64_t blocknum, long cnt uint16_t last_used_idx, avail_idx; int blk_size = DEFAULT_SECTOR_SIZE; - //printf("virtioblk_read: dev=%p buf=%p blocknum=%li count=%li\n", - // dev, buf, blocknum, cnt); + //printf("virtioblk_transfer: dev=%p buf=%p blocknum=%lli cnt=%li type=%i\n", + // dev, buf, blocknum, cnt, type); /* Check whether request is within disk capacity */ capacity = virtio_get_config(dev, offset_of(struct virtio_blk_cfg, capacity), sizeof(capacity)); if (blocknum + cnt - 1 > capacity) { - puts("virtioblk_read: Access beyond end of device!"); + puts("virtioblk_transfer: Access beyond end of device!"); return 0; } @@ -152,7 +154,7 @@ virtioblk_read(struct virtio_device *dev, char *buf, uint64_t blocknum, long cnt offset_of(struct virtio_blk_cfg, blk_size), sizeof(blk_size)); if (blk_size % DEFAULT_SECTOR_SIZE) { - fprintf(stderr, "virtio-blk: Unaligned sector read %d\n", blk_size); + fprintf(stderr, "virtio-blk: Unaligned sector size %d\n", blk_size); return 0; } @@ -167,7 +169,7 @@ virtioblk_read(struct virtio_device *dev, char *buf, uint64_t blocknum, long cnt current_used_idx = &vq_used->idx; /* Set up header */ - fill_blk_hdr(&blkhdr, dev->is_modern, VIRTIO_BLK_T_IN | VIRTIO_BLK_T_BARRIER, + fill_blk_hdr(&blkhdr, dev->is_modern, type | VIRTIO_BLK_T_BARRIER, 1, blocknum * blk_size / DEFAULT_SECTOR_SIZE); /* Determine descriptor index */ @@ -182,7 +184,7 @@ virtioblk_read(struct virtio_device *dev, char *buf, uint64_t blocknum, long cnt /* Set up virtqueue descriptor for data */ desc = &vq_desc[(id + 1) % vq_size]; virtio_fill_desc(desc, dev->is_modern, (uint64_t)buf, cnt * blk_size, - VRING_DESC_F_NEXT | VRING_DESC_F_WRITE, + VRING_DESC_F_NEXT | ((type & 1) ? 0 : VRING_DESC_F_WRITE), (id + 2) % vq_size); /* Set up virtqueue descriptor for status */ @@ -209,7 +211,8 @@ virtioblk_read(struct virtio_device *dev, char *buf, uint64_t blocknum, long cnt if (status == 0) return cnt; - printf("virtioblk_read failed! status = %i\n", status); + printf("virtioblk_transfer failed! type=%i, status = %i\n", + type, status); return 0; } diff --git a/lib/libvirtio/virtio-blk.h b/lib/libvirtio/virtio-blk.h index 2e7b592..21f0adc 100644 --- a/lib/libvirtio/virtio-blk.h +++ b/lib/libvirtio/virtio-blk.h @@ -55,6 +55,7 @@ struct virtio_blk_req { extern int virtioblk_init(struct virtio_device *dev); extern void virtioblk_shutdown(struct virtio_device *dev); -extern int virtioblk_read(struct virtio_device *dev, char *buf, uint64_t blocknum, long cnt); +extern int virtioblk_transfer(struct virtio_device *dev, char *buf, + uint64_t blocknum, long cnt, unsigned int type); #endif /* _VIRTIO_BLK_H */ diff --git a/lib/libvirtio/virtio.code b/lib/libvirtio/virtio.code index 971a3cf..b8262ad 100644 --- a/lib/libvirtio/virtio.code +++ b/lib/libvirtio/virtio.code @@ -70,7 +70,16 @@ PRIM(virtio_X2d_blk_X2d_read) long cnt = TOS.n; POP; long blkno = TOS.n; POP; void *buf = TOS.a; - TOS.n = virtioblk_read(dev, buf, blkno, cnt); + TOS.n = virtioblk_transfer(dev, buf, blkno, cnt, VIRTIO_BLK_T_IN); +MIRP + +// : virtio-blk-write ( buf blkno cnt dev -- #written ) +PRIM(virtio_X2d_blk_X2d_write) + void *dev = TOS.a; POP; + long cnt = TOS.n; POP; + long blkno = TOS.n; POP; + void *buf = TOS.a; + TOS.n = virtioblk_transfer(dev, buf, blkno, cnt, VIRTIO_BLK_T_OUT); MIRP /******** virtio-fs ********/ diff --git a/lib/libvirtio/virtio.in b/lib/libvirtio/virtio.in index d2b1641..41a9cd0 100644 --- a/lib/libvirtio/virtio.in +++ b/lib/libvirtio/virtio.in @@ -20,6 +20,7 @@ cod(virtio-set-qaddr) cod(virtio-blk-init) cod(virtio-blk-shutdown) cod(virtio-blk-read) +cod(virtio-blk-write) cod(virtio-scsi-init) cod(virtio-scsi-shutdown)