Skip to content

Commit ac926e1

Browse files
stefano-garzarellamstsirkin
authored andcommitted
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>
1 parent b91cf6e commit ac926e1

1 file changed

Lines changed: 23 additions & 15 deletions

File tree

drivers/vdpa/vdpa_sim/vdpa_sim_blk.c

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,28 @@
4242

4343
static char vdpasim_blk_id[VIRTIO_BLK_ID_BYTES] = "vdpa_blk_sim";
4444

45-
static bool vdpasim_blk_check_range(u64 start_sector, size_t range_size)
45+
static bool vdpasim_blk_check_range(struct vdpasim *vdpasim, u64 start_sector,
46+
u64 num_sectors, u64 max_sectors)
4647
{
47-
u64 range_sectors = range_size >> SECTOR_SHIFT;
48-
49-
if (range_size > VDPASIM_BLK_SIZE_MAX * VDPASIM_BLK_SEG_MAX)
50-
return false;
48+
if (start_sector > VDPASIM_BLK_CAPACITY) {
49+
dev_dbg(&vdpasim->vdpa.dev,
50+
"starting sector exceeds the capacity - start: 0x%llx capacity: 0x%x\n",
51+
start_sector, VDPASIM_BLK_CAPACITY);
52+
}
5153

52-
if (start_sector > VDPASIM_BLK_CAPACITY)
54+
if (num_sectors > max_sectors) {
55+
dev_dbg(&vdpasim->vdpa.dev,
56+
"number of sectors exceeds the max allowed in a request - num: 0x%llx max: 0x%llx\n",
57+
num_sectors, max_sectors);
5358
return false;
59+
}
5460

55-
if (range_sectors > VDPASIM_BLK_CAPACITY - start_sector)
61+
if (num_sectors > VDPASIM_BLK_CAPACITY - start_sector) {
62+
dev_dbg(&vdpasim->vdpa.dev,
63+
"request exceeds the capacity - start: 0x%llx num: 0x%llx capacity: 0x%x\n",
64+
start_sector, num_sectors, VDPASIM_BLK_CAPACITY);
5665
return false;
66+
}
5767

5868
return true;
5969
}
@@ -123,10 +133,9 @@ static bool vdpasim_blk_handle_req(struct vdpasim *vdpasim,
123133

124134
switch (type) {
125135
case VIRTIO_BLK_T_IN:
126-
if (!vdpasim_blk_check_range(sector, to_push)) {
127-
dev_dbg(&vdpasim->vdpa.dev,
128-
"reading over the capacity - offset: 0x%llx len: 0x%zx\n",
129-
offset, to_push);
136+
if (!vdpasim_blk_check_range(vdpasim, sector,
137+
to_push >> SECTOR_SHIFT,
138+
VDPASIM_BLK_SIZE_MAX * VDPASIM_BLK_SEG_MAX)) {
130139
status = VIRTIO_BLK_S_IOERR;
131140
break;
132141
}
@@ -146,10 +155,9 @@ static bool vdpasim_blk_handle_req(struct vdpasim *vdpasim,
146155
break;
147156

148157
case VIRTIO_BLK_T_OUT:
149-
if (!vdpasim_blk_check_range(sector, to_pull)) {
150-
dev_dbg(&vdpasim->vdpa.dev,
151-
"writing over the capacity - offset: 0x%llx len: 0x%zx\n",
152-
offset, to_pull);
158+
if (!vdpasim_blk_check_range(vdpasim, sector,
159+
to_pull >> SECTOR_SHIFT,
160+
VDPASIM_BLK_SIZE_MAX * VDPASIM_BLK_SEG_MAX)) {
153161
status = VIRTIO_BLK_S_IOERR;
154162
break;
155163
}

0 commit comments

Comments
 (0)