Skip to content

Commit

Permalink
nbd/server: Optimize final chunk of sparse read
Browse files Browse the repository at this point in the history
If we are careful to handle 0-length read requests correctly,
we can optimize our sparse read to send the NBD_REPLY_FLAG_DONE
bit on our last OFFSET_DATA or OFFSET_HOLE chunk rather than
needing a separate chunk.

Signed-off-by: Eric Blake <[email protected]>
Message-Id: <[email protected]>
Reviewed-by: Vladimir Sementsov-Ogievskiy <[email protected]>
  • Loading branch information
ebblake committed Jan 8, 2018
1 parent 418638d commit e2de325
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions nbd/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -1339,12 +1339,14 @@ static int coroutine_fn nbd_co_send_sparse_read(NBDClient *client,
offset + progress,
size - progress, &pnum, NULL,
NULL);
bool final;

if (status < 0) {
error_setg_errno(errp, -status, "unable to check for holes");
return status;
}
assert(pnum && pnum <= size - progress);
final = progress + pnum == size;
if (status & BDRV_BLOCK_ZERO) {
NBDStructuredReadHole chunk;
struct iovec iov[] = {
Expand All @@ -1353,7 +1355,8 @@ static int coroutine_fn nbd_co_send_sparse_read(NBDClient *client,

trace_nbd_co_send_structured_read_hole(handle, offset + progress,
pnum);
set_be_chunk(&chunk.h, 0, NBD_REPLY_TYPE_OFFSET_HOLE,
set_be_chunk(&chunk.h, final ? NBD_REPLY_FLAG_DONE : 0,
NBD_REPLY_TYPE_OFFSET_HOLE,
handle, sizeof(chunk) - sizeof(chunk.h));
stq_be_p(&chunk.offset, offset + progress);
stl_be_p(&chunk.length, pnum);
Expand All @@ -1366,7 +1369,7 @@ static int coroutine_fn nbd_co_send_sparse_read(NBDClient *client,
break;
}
ret = nbd_co_send_structured_read(client, handle, offset + progress,
data + progress, pnum, false,
data + progress, pnum, final,
errp);
}

Expand All @@ -1375,9 +1378,6 @@ static int coroutine_fn nbd_co_send_sparse_read(NBDClient *client,
}
progress += pnum;
}
if (!ret) {
ret = nbd_co_send_structured_done(client, handle, errp);
}
return ret;
}

Expand Down Expand Up @@ -1542,7 +1542,8 @@ static coroutine_fn void nbd_trip(void *opaque)
}
}

if (client->structured_reply && !(request.flags & NBD_CMD_FLAG_DF)) {
if (client->structured_reply && !(request.flags & NBD_CMD_FLAG_DF) &&
request.len) {
ret = nbd_co_send_sparse_read(req->client, request.handle,
request.from, req->data, request.len,
&local_err);
Expand Down

0 comments on commit e2de325

Please sign in to comment.