diff mbox

nbd: Don't trim unrequested bytes

Message ID 1464151079-4341-1-git-send-email-eblake@redhat.com (mailing list archive)
State New, archived
Headers show

Commit Message

Eric Blake May 25, 2016, 4:37 a.m. UTC
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.

CC: qemu-stable@nongnu.org
Signed-off-by: Eric Blake <eblake@redhat.com>
---
 nbd/server.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

Comments

Paolo Bonzini May 25, 2016, 8:31 a.m. UTC | #1
On 25/05/2016 06:37, Eric Blake wrote:
> 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.
> 
> CC: qemu-stable@nongnu.org
> Signed-off-by: Eric Blake <eblake@redhat.com>
> ---
>  nbd/server.c | 8 ++++++--
>  1 file changed, 6 insertions(+), 2 deletions(-)
> 
> diff --git a/nbd/server.c b/nbd/server.c
> index fa862cd..5aed8db 100644
> --- a/nbd/server.c
> +++ b/nbd/server.c
> @@ -1153,8 +1153,12 @@ 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,
> +        /* Ignore unaligned head or tail, until block layer adds byte
> +         * interface */
> +        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");
> 

Thanks, queued for 2.7.

Paolo
Eric Blake May 25, 2016, 10:58 a.m. UTC | #2
On 05/25/2016 02:31 AM, Paolo Bonzini wrote:
> 
> 
> On 25/05/2016 06:37, Eric Blake wrote:
>> 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.
>>
>> CC: qemu-stable@nongnu.org
>> Signed-off-by: Eric Blake <eblake@redhat.com>
>> ---
>>  nbd/server.c | 8 ++++++--
>>  1 file changed, 6 insertions(+), 2 deletions(-)
>>
>> diff --git a/nbd/server.c b/nbd/server.c
>> index fa862cd..5aed8db 100644
>> --- a/nbd/server.c
>> +++ b/nbd/server.c
>> @@ -1153,8 +1153,12 @@ 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,
>> +        /* Ignore unaligned head or tail, until block layer adds byte
>> +         * interface */
>> +        request.len -= (request.from + request.len) % BDRV_SECTOR_SIZE;

Gaaah - this version can underflow request.len.  v2 coming up.

> Thanks, queued for 2.7.

Fortunately, rebasing a staging queue is easier than dealing with an
incorrect pull request.
diff mbox

Patch

diff --git a/nbd/server.c b/nbd/server.c
index fa862cd..5aed8db 100644
--- a/nbd/server.c
+++ b/nbd/server.c
@@ -1153,8 +1153,12 @@  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,
+        /* Ignore unaligned head or tail, until block layer adds byte
+         * interface */
+        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");