Skip to content

Commit 890cbcc

Browse files
committed
nbd: Fix large trim/zero requests
Although qemu as NBD client limits requests to <2G, the NBD protocol allows clients to send requests almost all the way up to 4G. But because our block layer is not yet 64-bit clean, we accidentally wrap such requests into a negative size, and fail with EIO instead of performing the intended operation. The bug is visible in modern systems with something as simple as: $ qemu-img create -f qcow2 /tmp/image.img 5G $ sudo qemu-nbd --connect=/dev/nbd0 /tmp/image.img $ sudo blkdiscard /dev/nbd0 or with user-space only: $ truncate --size=3G file $ qemu-nbd -f raw file $ nbdsh -u nbd://localhost:10809 -c 'h.trim(3*1024*1024*1024,0)' Although both blk_co_pdiscard and blk_pwrite_zeroes currently return 0 on success, this is also a good time to fix our code to a more robust paradigm that treats all non-negative values as success. Alas, our iotests do not currently make it easy to add external dependencies on blkdiscard or nbdsh, so we have to rely on manual testing for now. This patch can be reverted when we later improve the overall block layer to be 64-bit clean, but for now, a minimal fix was deemed less risky prior to release. CC: [email protected] Fixes: 1f4d6d1 Fixes: 1c6c4bb Fixes: systemd/systemd#16242 Signed-off-by: Eric Blake <[email protected]> Message-Id: <[email protected]> Reviewed-by: Vladimir Sementsov-Ogievskiy <[email protected]> [eblake: rework success tests to use >=0]
1 parent 1b242c3 commit 890cbcc

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

nbd/server.c

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2378,8 +2378,17 @@ static coroutine_fn int nbd_handle_request(NBDClient *client,
23782378
if (request->flags & NBD_CMD_FLAG_FAST_ZERO) {
23792379
flags |= BDRV_REQ_NO_FALLBACK;
23802380
}
2381-
ret = blk_pwrite_zeroes(exp->blk, request->from + exp->dev_offset,
2382-
request->len, flags);
2381+
ret = 0;
2382+
/* FIXME simplify this when blk_pwrite_zeroes switches to 64-bit */
2383+
while (ret >= 0 && request->len) {
2384+
int align = client->check_align ?: 1;
2385+
int len = MIN(request->len, QEMU_ALIGN_DOWN(BDRV_REQUEST_MAX_BYTES,
2386+
align));
2387+
ret = blk_pwrite_zeroes(exp->blk, request->from + exp->dev_offset,
2388+
len, flags);
2389+
request->len -= len;
2390+
request->from += len;
2391+
}
23832392
return nbd_send_generic_reply(client, request->handle, ret,
23842393
"writing to file failed", errp);
23852394

@@ -2393,9 +2402,18 @@ static coroutine_fn int nbd_handle_request(NBDClient *client,
23932402
"flush failed", errp);
23942403

23952404
case NBD_CMD_TRIM:
2396-
ret = blk_co_pdiscard(exp->blk, request->from + exp->dev_offset,
2397-
request->len);
2398-
if (ret == 0 && request->flags & NBD_CMD_FLAG_FUA) {
2405+
ret = 0;
2406+
/* FIXME simplify this when blk_co_pdiscard switches to 64-bit */
2407+
while (ret >= 0 && request->len) {
2408+
int align = client->check_align ?: 1;
2409+
int len = MIN(request->len, QEMU_ALIGN_DOWN(BDRV_REQUEST_MAX_BYTES,
2410+
align));
2411+
ret = blk_co_pdiscard(exp->blk, request->from + exp->dev_offset,
2412+
len);
2413+
request->len -= len;
2414+
request->from += len;
2415+
}
2416+
if (ret >= 0 && request->flags & NBD_CMD_FLAG_FUA) {
23992417
ret = blk_co_flush(exp->blk);
24002418
}
24012419
return nbd_send_generic_reply(client, request->handle, ret,

0 commit comments

Comments
 (0)