nbd: Don't trim unrequested bytes
PATCH v2 1/1 nbd: Don't trim unrequested bytes
Similar to commit df7b97ff, we are mishandling clients that give an unaligned NBD_CMD_TRIM request, and potentially trimming bytes that occur before their request; which in turn can cause potential unintended data loss (unlikely in practice, since most clients are sane and issue aligned trim requests). However, while we fixed read and write by switching to the byte interfaces of blk_, we don't yet have a byte interface for discard. On the other hand, trim is advisory, so rounding the user's request to simply ignore the first and last unaligned sectors (or the entire request, if it is sub-sector in length) is just fine.
---
v2: don't underflow request.len on sub-sector requests
nbd/server.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-)
nbd/server.c +14 -6
@@ -1153,12 +1153,20 @@ static void nbd_trip(void *opaque)break ;case NBD_CMD_TRIM :TRACE ("Request type is TRIM" ); - ret =blk_co_discard (exp->blk, (request.from + exp->dev_offset) - /BDRV_SECTOR_SIZE , - request.len /BDRV_SECTOR_SIZE ); -if (ret <0 ) { -LOG ("discard failed" ); - reply.error = -ret; +/* Ignore unaligned head or tail, until block layer adds byte +* interface */ +if (request.len >=BDRV_SECTOR_SIZE ) { + request.len -= (request.from + request.len) %BDRV_SECTOR_SIZE ; + ret =blk_co_discard (exp->blk, +DIV_ROUND_UP (request.from + exp->dev_offset, +BDRV_SECTOR_SIZE ), + request.len /BDRV_SECTOR_SIZE ); +if (ret <0 ) { +LOG ("discard failed" ); + reply.error = -ret; + } + }else { +TRACE ("trim request too small, ignoring" ); }if (nbd_co_send_reply (req, &reply,0 ) <0 ) { goto out;
Signature · 1 line
2.5.5
Quoted 33 lines · Eric Blake
Similar to commit
df7b97ff, we are mishandling clients that give an unalignedNBD_CMD_TRIMrequest, and potentially trimming bytes that occur before their request; which in turn can cause potential unintended data loss (unlikely in practice, since most clients are sane and issue aligned trim requests). However, while we fixed read and write by switching to the byte interfaces ofblk_, we don't yet have a byte interface for discard. On the other hand, trim is advisory, so rounding the user's request to simply ignore the first and last unaligned sectors (or the entire request, if it is sub-sector in length) is just fine.Signed-off-by Eric Blake <[email protected]>---
v2: don't underflow request.len on sub-sector requests
nbd/server.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-)nbd/server.c +0
-4@@ -1153,12 +1153,20 @@ static void nbd_trip(void *opaque)break ;case NBD_CMD_TRIM :TRACE ("Request type is TRIM" ); - ret =blk_co_discard (exp->blk, (request.from + exp->dev_offset) - /BDRV_SECTOR_SIZE , - request.len /BDRV_SECTOR_SIZE ); -if (ret <0 ) {
nbd/server.c static void nbd_trip(void *opaque) · line 1163
Quoted 10 lines
nbd/server.c +8
-2-LOG ("discard failed" ); - reply.error = -ret; +/* Ignore unaligned head or tail, until block layer adds byte +* interface */ +if (request.len >=BDRV_SECTOR_SIZE ) { + request.len -= (request.from + request.len) %BDRV_SECTOR_SIZE ; + ret =blk_co_discard (exp->blk, +DIV_ROUND_UP (request.from + exp->dev_offset, +BDRV_SECTOR_SIZE ), + request.len /BDRV_SECTOR_SIZE );
This can end up calling blk_co_discard(, , 0) - do we need to audit whether all underlying drivers handle a 0-length request, and/or special-case this in blk_co_discard() to be an explicit no-op?
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Am 25.05.2016 um 13:25 hat Eric Blake geschrieben:
Quoted 10 lines · Eric Blake
nbd/server.c static void nbd_trip(void *opaque) · line 1163
Quoted 8 lines
nbd/server.c +8
-0+/* Ignore unaligned head or tail, until block layer adds byte +* interface */ +if (request.len >=BDRV_SECTOR_SIZE ) { + request.len -= (request.from + request.len) %BDRV_SECTOR_SIZE ; + ret =blk_co_discard (exp->blk, +DIV_ROUND_UP (request.from + exp->dev_offset, +BDRV_SECTOR_SIZE ), + request.len /BDRV_SECTOR_SIZE );
Quoted 3 lines
This can end up calling
blk_co_discard(, , 0)- do we need to audit whether all underlying drivers handle a 0-length request, and/or special-case this inblk_co_discard()to be an explicit no-op?
Auditing shouldn't be too hard as we don't have many drivers that support the operation in the first place. In any case, it might be useful to add a qemu-iotest case that tries all kinds of operations with a zero length.
Kevin