Skip to contentSkip to conversation

block/iscsi: avoid data corruption with cache=writeback

PATCH Applied Peter Lieven 3 participants 4 messages Jan 16, 2017 – Jan 20, 2017 Block layer

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 block/iscsi: avoid data corruption with cache=writeback

nb_cls_shrunk in iscsi_allocmap_update can become -1 if the request starts and ends within the same cluster. This results in passing -1 to bitmap_set and bitmap_clear and they don't handle negative values properly. In the end this leads to data corruption.

Fixes e1123a3b40a1a9a625a29c8ed4debb7e206ea690
---
 block/iscsi.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
block/iscsi.c +6 -2
index 6aeeb9e..1860f1b 100644
@@ -499,14 +499,18 @@ iscsi_allocmap_update(IscsiLun *iscsilun, int64_t sector_num,
     if (allocated) {
         bitmap_set(iscsilun->allocmap, cl_num_expanded, nb_cls_expanded);
     } else {
-        bitmap_clear(iscsilun->allocmap, cl_num_shrunk, nb_cls_shrunk);
+        if (nb_cls_shrunk > 0) {
+            bitmap_clear(iscsilun->allocmap, cl_num_shrunk, nb_cls_shrunk);
+        }
     }
 
     if (iscsilun->allocmap_valid == NULL) {
         return;
     }
     if (valid) {
-        bitmap_set(iscsilun->allocmap_valid, cl_num_shrunk, nb_cls_shrunk);
+        if (nb_cls_shrunk > 0) {
+            bitmap_set(iscsilun->allocmap_valid, cl_num_shrunk, nb_cls_shrunk);
+        }
     } else {
         bitmap_clear(iscsilun->allocmap_valid, cl_num_expanded,
                      nb_cls_expanded);
Signature · 1 line
1.9.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 42 lines · Peter Lieven

nb_cls_shrunk in iscsi_allocmap_update can become -1 if the request starts and ends within the same cluster. This results in passing -1 to bitmap_set and bitmap_clear and they don't handle negative values properly. In the end this leads to data corruption.

Fixes e1123a3b40a1a9a625a29c8ed4debb7e206ea690
---
 block/iscsi.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
block/iscsi.c +6 -2
index 6aeeb9e..1860f1b 100644
@@ -499,14 +499,18 @@ iscsi_allocmap_update(IscsiLun *iscsilun, int64_t sector_num,
     if (allocated) {
         bitmap_set(iscsilun->allocmap, cl_num_expanded, nb_cls_expanded);
     } else {
-        bitmap_clear(iscsilun->allocmap, cl_num_shrunk, nb_cls_shrunk);
+        if (nb_cls_shrunk > 0) {
+            bitmap_clear(iscsilun->allocmap, cl_num_shrunk, nb_cls_shrunk);
+        }
     }
 
     if (iscsilun->allocmap_valid == NULL) {
         return;
     }
     if (valid) {
-        bitmap_set(iscsilun->allocmap_valid, cl_num_shrunk, nb_cls_shrunk);
+        if (nb_cls_shrunk > 0) {
+            bitmap_set(iscsilun->allocmap_valid, cl_num_shrunk, nb_cls_shrunk);
+        }
     } else {
         bitmap_clear(iscsilun->allocmap_valid, cl_num_expanded,
                      nb_cls_expanded);
Signature · 1 line
1.9.1

It's probably a good idea to add assertions parameter in bitmap_*.

#2 lore raw

Am 17.01.2017 um 12:28 schrieb Fam Zheng:

Quoted 28 lines · Peter Lieven
Quoted 27 lines

nb_cls_shrunk in iscsi_allocmap_update can become -1 if the request starts and ends within the same cluster. This results in passing -1 to bitmap_set and bitmap_clear and they don't handle negative values properly. In the end this leads to data corruption.

Fixes e1123a3b40a1a9a625a29c8ed4debb7e206ea690
---
  block/iscsi.c | 8 ++++++--
  1 file changed, 6 insertions(+), 2 deletions(-)
block/iscsi.c +3 -1
index 6aeeb9e..1860f1b 100644
@@ -499,14 +499,18 @@ iscsi_allocmap_update(IscsiLun *iscsilun, int64_t sector_num,
      if (allocated) {
          bitmap_set(iscsilun->allocmap, cl_num_expanded, nb_cls_expanded);
      } else {
-        bitmap_clear(iscsilun->allocmap, cl_num_shrunk, nb_cls_shrunk);
+        if (nb_cls_shrunk > 0) {
+            bitmap_clear(iscsilun->allocmap, cl_num_shrunk, nb_cls_shrunk);
+        }
      }
 
Quoted 16 lines
Quoted 15 lines
block/iscsi.c +3 -1
      if (iscsilun->allocmap_valid == NULL) {
          return;
      }
      if (valid) {
-        bitmap_set(iscsilun->allocmap_valid, cl_num_shrunk, nb_cls_shrunk);
+        if (nb_cls_shrunk > 0) {
+            bitmap_set(iscsilun->allocmap_valid, cl_num_shrunk, nb_cls_shrunk);
+        }
      } else {
          bitmap_clear(iscsilun->allocmap_valid, cl_num_expanded,
                       nb_cls_expanded);
Signature · 1 line
1.9.1

It's probably a good idea to add assertions parameter in bitmap_*.

yes, i will send a follow-up patch.

Peter

#3 lore raw
In reply to Peter Lieven
Quoted 6 lines · Peter Lieven

nb_cls_shrunk in iscsi_allocmap_update can become -1 if the request starts and ends within the same cluster. This results in passing -1 to bitmap_set and bitmap_clear and they don't handle negative values properly. In the end this leads to data corruption.

Quoted 6 lines
Fixes e1123a3b40a1a9a625a29c8ed4debb7e206ea690
---
 block/iscsi.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

Queued, thanks.

Paolo