vdpa_sim_blk: make vdpasim_blk_check_range usable by other requests
Next patches will add handling of other requests, where will be useful to reuse vdpasim_blk_check_range(). So let's make it more generic by adding the `max_sectors` parameter, since different requests allow different numbers of maximum sectors. Let's also print the messages directly in vdpasim_blk_check_range() to avoid duplicate prints. Signed-off-by: Stefano Garzarella <sgarzare@redhat.com> Message-Id: <20220811083632.77525-3-sgarzare@redhat.com> Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
This commit is contained in:
parent
b91cf6e95b
commit
ac926e1b46
@ -42,18 +42,28 @@
|
|||||||
|
|
||||||
static char vdpasim_blk_id[VIRTIO_BLK_ID_BYTES] = "vdpa_blk_sim";
|
static char vdpasim_blk_id[VIRTIO_BLK_ID_BYTES] = "vdpa_blk_sim";
|
||||||
|
|
||||||
static bool vdpasim_blk_check_range(u64 start_sector, size_t range_size)
|
static bool vdpasim_blk_check_range(struct vdpasim *vdpasim, u64 start_sector,
|
||||||
|
u64 num_sectors, u64 max_sectors)
|
||||||
{
|
{
|
||||||
u64 range_sectors = range_size >> SECTOR_SHIFT;
|
if (start_sector > VDPASIM_BLK_CAPACITY) {
|
||||||
|
dev_dbg(&vdpasim->vdpa.dev,
|
||||||
|
"starting sector exceeds the capacity - start: 0x%llx capacity: 0x%x\n",
|
||||||
|
start_sector, VDPASIM_BLK_CAPACITY);
|
||||||
|
}
|
||||||
|
|
||||||
if (range_size > VDPASIM_BLK_SIZE_MAX * VDPASIM_BLK_SEG_MAX)
|
if (num_sectors > max_sectors) {
|
||||||
|
dev_dbg(&vdpasim->vdpa.dev,
|
||||||
|
"number of sectors exceeds the max allowed in a request - num: 0x%llx max: 0x%llx\n",
|
||||||
|
num_sectors, max_sectors);
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
if (start_sector > VDPASIM_BLK_CAPACITY)
|
if (num_sectors > VDPASIM_BLK_CAPACITY - start_sector) {
|
||||||
return false;
|
dev_dbg(&vdpasim->vdpa.dev,
|
||||||
|
"request exceeds the capacity - start: 0x%llx num: 0x%llx capacity: 0x%x\n",
|
||||||
if (range_sectors > VDPASIM_BLK_CAPACITY - start_sector)
|
start_sector, num_sectors, VDPASIM_BLK_CAPACITY);
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -123,10 +133,9 @@ static bool vdpasim_blk_handle_req(struct vdpasim *vdpasim,
|
|||||||
|
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case VIRTIO_BLK_T_IN:
|
case VIRTIO_BLK_T_IN:
|
||||||
if (!vdpasim_blk_check_range(sector, to_push)) {
|
if (!vdpasim_blk_check_range(vdpasim, sector,
|
||||||
dev_dbg(&vdpasim->vdpa.dev,
|
to_push >> SECTOR_SHIFT,
|
||||||
"reading over the capacity - offset: 0x%llx len: 0x%zx\n",
|
VDPASIM_BLK_SIZE_MAX * VDPASIM_BLK_SEG_MAX)) {
|
||||||
offset, to_push);
|
|
||||||
status = VIRTIO_BLK_S_IOERR;
|
status = VIRTIO_BLK_S_IOERR;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -146,10 +155,9 @@ static bool vdpasim_blk_handle_req(struct vdpasim *vdpasim,
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case VIRTIO_BLK_T_OUT:
|
case VIRTIO_BLK_T_OUT:
|
||||||
if (!vdpasim_blk_check_range(sector, to_pull)) {
|
if (!vdpasim_blk_check_range(vdpasim, sector,
|
||||||
dev_dbg(&vdpasim->vdpa.dev,
|
to_pull >> SECTOR_SHIFT,
|
||||||
"writing over the capacity - offset: 0x%llx len: 0x%zx\n",
|
VDPASIM_BLK_SIZE_MAX * VDPASIM_BLK_SEG_MAX)) {
|
||||||
offset, to_pull);
|
|
||||||
status = VIRTIO_BLK_S_IOERR;
|
status = VIRTIO_BLK_S_IOERR;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user