Skip to contentSkip to conversation

nbd: Don't trim unrequested bytes

PATCH Superseded Eric Blake 2 participants 3 messages May 25, 2016

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Eric Blake

author
#0 lore raw

PATCH 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.

---
 nbd/server.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
nbd/server.c +6 -2
index fa862cd..5aed8db 100644
@@ -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");
Signature · 1 line
2.5.5
#1 lore raw
Quoted 28 lines · Eric Blake

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.

---
 nbd/server.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
nbd/server.c +0 -1
index fa862cd..5aed8db 100644
@@ -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)

nbd/server.c static void nbd_trip(void *opaque) · line 1164

Quoted 10 lines
nbd/server.c +6 -1
-                                       / 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

author
#2 lore raw
Quoted 21 lines · Paolo Bonzini
Quoted 18 lines · Eric Blake

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.

---
 nbd/server.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
Quoted 13 lines

nbd/server.c static void nbd_trip(void *opaque) · line 1158

Quoted 13 lines
nbd/server.c +3 -2
index fa862cd..5aed8db 100644
@@ -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.

Quoted 1 line

Thanks, queued for 2.7.

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

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org