Skip to contentSkip to conversation

btrfs: Do super block verification before writing it to disk

PATCH Superseded Qu Wenruo 4 participants 5 messages Apr 16, 2018 Btrfs file system

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Qu Wenruo

author
#0 lore raw

PATCH 1/1 btrfs: Do super block verification before writing it to disk

There are already 2 reports about strangely corrupted super blocks, where csum type and incompat flags get some obvious garbage, but csum still matches and all other vitals are correct.

This normally means some kernel memory corruption happens, although the cause is unknown, at least detect it and prevent further corruption.

---
 fs/btrfs/disk-io.c | 24 ++++++++++++++++++++----
 1 file changed, 20 insertions(+), 4 deletions(-)
fs/btrfs/disk-io.c +20 -4
index 23803102aa0d..10d814f03f13 100644
@@ -68,7 +68,8 @@
 static const struct extent_io_ops btree_extent_io_ops;
 static void end_workqueue_fn(struct btrfs_work *work);
 static void free_fs_root(struct btrfs_root *root);
-static int btrfs_check_super_valid(struct btrfs_fs_info *fs_info);
+static int btrfs_check_super_valid(struct btrfs_fs_info *fs_info,
+				   struct btrfs_super_block *sb);
 static void btrfs_destroy_ordered_extents(struct btrfs_root *root);
 static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans,
 				      struct btrfs_fs_info *fs_info);
@@ -2680,7 +2681,7 @@ int open_ctree(struct super_block *sb,
 
 	memcpy(fs_info->fsid, fs_info->super_copy->fsid, BTRFS_FSID_SIZE);
 
-	ret = btrfs_check_super_valid(fs_info);
+	ret = btrfs_check_super_valid(fs_info, fs_info->super_copy);
 	if (ret) {
 		btrfs_err(fs_info, "superblock contains fatal errors");
 		err = -EINVAL;
@@ -3575,6 +3576,21 @@ int write_all_supers(struct btrfs_fs_info *fs_info, int max_mirrors)
 	sb = fs_info->super_for_commit;
 	dev_item = &sb->dev_item;
 
+	/* Do extra check on the sb to be written */
+	ret = btrfs_check_super_valid(fs_info, sb);
+	if (ret) {
+		btrfs_err(fs_info, "fatal superblock corrupted detected");
+		return -EUCLEAN;
+	}
+	/*
+	 * Unknown incompat flags can't be mounted, so newly developed flags
+	 * means corruption
+	 */
+	if (btrfs_super_incompat_flags(sb) & ~BTRFS_FEATURE_INCOMPAT_SUPP) {
+		btrfs_err(fs_info, "fatal superblock corrupted detected");
+		return -EUCLEAN;
+	}
+
 	mutex_lock(&fs_info->fs_devices->device_list_mutex);
 	head = &fs_info->fs_devices->devices;
 	max_errors = btrfs_super_num_devices(fs_info->super_copy) - 1;
@@ -3985,9 +4001,9 @@ int btrfs_read_buffer(struct extent_buffer *buf, u64 parent_transid, int level,
 					      level, first_key);
 }
 
-static int btrfs_check_super_valid(struct btrfs_fs_info *fs_info)
+static int btrfs_check_super_valid(struct btrfs_fs_info *fs_info,
+				   struct btrfs_super_block *sb)
 {
-	struct btrfs_super_block *sb = fs_info->super_copy;
 	u64 nodesize = btrfs_super_nodesize(sb);
 	u64 sectorsize = btrfs_super_sectorsize(sb);
 	int ret = 0;
Signature · 1 line
2.17.0
#1 lore raw
Quoted 9 lines · Qu Wenruo

There are already 2 reports about strangely corrupted super blocks, where csum type and incompat flags get some obvious garbage, but csum still matches and all other vitals are correct.

This normally means some kernel memory corruption happens, although the cause is unknown, at least detect it and prevent further corruption.

---

Can you help point those 2 cases here?

Thanks, Anand

Quoted 61 lines
  fs/btrfs/disk-io.c | 24 ++++++++++++++++++++----
  1 file changed, 20 insertions(+), 4 deletions(-)
fs/btrfs/disk-io.c +20 -4
index 23803102aa0d..10d814f03f13 100644
@@ -68,7 +68,8 @@
  static const struct extent_io_ops btree_extent_io_ops;
  static void end_workqueue_fn(struct btrfs_work *work);
  static void free_fs_root(struct btrfs_root *root);
-static int btrfs_check_super_valid(struct btrfs_fs_info *fs_info);
+static int btrfs_check_super_valid(struct btrfs_fs_info *fs_info,
+				   struct btrfs_super_block *sb);
  static void btrfs_destroy_ordered_extents(struct btrfs_root *root);
  static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans,
  				      struct btrfs_fs_info *fs_info);
@@ -2680,7 +2681,7 @@ int open_ctree(struct super_block *sb,
 
  	memcpy(fs_info->fsid, fs_info->super_copy->fsid, BTRFS_FSID_SIZE);
 
-	ret = btrfs_check_super_valid(fs_info);
+	ret = btrfs_check_super_valid(fs_info, fs_info->super_copy);
  	if (ret) {
  		btrfs_err(fs_info, "superblock contains fatal errors");
  		err = -EINVAL;
@@ -3575,6 +3576,21 @@ int write_all_supers(struct btrfs_fs_info *fs_info, int max_mirrors)
  	sb = fs_info->super_for_commit;
  	dev_item = &sb->dev_item;
 
+	/* Do extra check on the sb to be written */
+	ret = btrfs_check_super_valid(fs_info, sb);
+	if (ret) {
+		btrfs_err(fs_info, "fatal superblock corrupted detected");
+		return -EUCLEAN;
+	}
+	/*
+	 * Unknown incompat flags can't be mounted, so newly developed flags
+	 * means corruption
+	 */
+	if (btrfs_super_incompat_flags(sb) & ~BTRFS_FEATURE_INCOMPAT_SUPP) {
+		btrfs_err(fs_info, "fatal superblock corrupted detected");
+		return -EUCLEAN;
+	}
+
  	mutex_lock(&fs_info->fs_devices->device_list_mutex);
  	head = &fs_info->fs_devices->devices;
  	max_errors = btrfs_super_num_devices(fs_info->super_copy) - 1;
@@ -3985,9 +4001,9 @@ int btrfs_read_buffer(struct extent_buffer *buf, u64 parent_transid, int level,
  					      level, first_key);
  }
 
-static int btrfs_check_super_valid(struct btrfs_fs_info *fs_info)
+static int btrfs_check_super_valid(struct btrfs_fs_info *fs_info,
+				   struct btrfs_super_block *sb)
  {
-	struct btrfs_super_block *sb = fs_info->super_copy;
  	u64 nodesize = btrfs_super_nodesize(sb);
  	u64 sectorsize = btrfs_super_sectorsize(sb);
  	int ret = 0;
#2 lore raw
Quoted 7 lines · Anand Jain
Quoted 4 lines · Qu Wenruo

There are already 2 reports about strangely corrupted super blocks, where csum type and incompat flags get some obvious garbage, but csum still matches and all other vitals are correct.

Quoted 7 lines
Quoted 5 lines

This normally means some kernel memory corruption happens, although the cause is unknown, at least detect it and prevent further corruption.

---

 Can you help point those 2 cases here?

Did you mean the reported-by tags?
If so, no problem.

Thanks,
Qu

Quoted 68 lines

Thanks, Anand

Quoted 60 lines

  fs/btrfs/disk-io.c | 24 ++++++++++++++++++++----

  1 file changed, 20 insertions(+), 4 deletions(-)
fs/btrfs/disk-io.c +3 -1
index 23803102aa0d..10d814f03f13 100644
@@ -68,7 +68,8 @@
   static const struct extent_io_ops btree_extent_io_ops;
   static void end_workqueue_fn(struct btrfs_work *work);
   static void free_fs_root(struct btrfs_root *root);
-static int btrfs_check_super_valid(struct btrfs_fs_info *fs_info);
+static int btrfs_check_super_valid(struct btrfs_fs_info *fs_info,
+                   struct btrfs_super_block *sb);
   static void btrfs_destroy_ordered_extents(struct btrfs_root *root);
   static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans,
                         struct btrfs_fs_info *fs_info);
@@ -2680,7 +2681,7 @@ int open_ctree(struct super_block *sb,
         memcpy(fs_info->fsid, fs_info->super_copy->fsid,
 BTRFS_FSID_SIZE);
   -    ret = btrfs_check_super_valid(fs_info);
+    ret = btrfs_check_super_valid(fs_info, fs_info->super_copy);
       if (ret) {
           btrfs_err(fs_info, "superblock contains fatal errors");
           err = -EINVAL;
@@ -3575,6 +3576,21 @@ int write_all_supers(struct btrfs_fs_info
 *fs_info, int max_mirrors)
       sb = fs_info->super_for_commit;
       dev_item = &sb->dev_item;

  +    /* Do extra check on the sb to be written */
+    ret = btrfs_check_super_valid(fs_info, sb);
+    if (ret) {
+        btrfs_err(fs_info, "fatal superblock corrupted detected"); +        return -EUCLEAN;
+    }
+    /*
+     * Unknown incompat flags can't be mounted, so newly developed flags +     * means corruption
+     */
+    if (btrfs_super_incompat_flags(sb) & ~BTRFS_FEATURE_INCOMPAT_SUPP) { +        btrfs_err(fs_info, "fatal superblock corrupted detected"); +        return -EUCLEAN;
+    }
+
      mutex_lock(&fs_info->fs_devices->device_list_mutex); head = &fs_info->fs_devices->devices;
      max_errors = btrfs_super_num_devices(fs_info->super_copy) - 1;

fs/btrfs/disk-io.c +2 -1
@@ -3985,9 +4001,9 @@ int btrfs_read_buffer(struct extent_buffer *buf,
 u64 parent_transid, int level,
                             level, first_key);
   }
   -static int btrfs_check_super_valid(struct btrfs_fs_info *fs_info)
+static int btrfs_check_super_valid(struct btrfs_fs_info *fs_info,
+                   struct btrfs_super_block *sb)
   {
-    struct btrfs_super_block *sb = fs_info->super_copy;
       u64 nodesize = btrfs_super_nodesize(sb);
       u64 sectorsize = btrfs_super_sectorsize(sb);

      int ret = 0;

Signature · 3 lines
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#3 lore raw
Quoted 13 lines · Qu Wenruo
Quoted 10 lines · Anand Jain
Quoted 7 lines · Qu Wenruo

There are already 2 reports about strangely corrupted super blocks, where csum type and incompat flags get some obvious garbage, but csum still matches and all other vitals are correct.

This normally means some kernel memory corruption happens, although the cause is unknown, at least detect it and prevent further corruption.

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

---

 Can you help point those 2 cases here?

Did you mean the reported-by tags?
If so, no problem.

Yes please, also describe what got corrupted in the changelog, besides the links to the bugreports.

#4 lore raw
In reply to Qu Wenruo
Quoted 41 lines · Qu Wenruo

There are already 2 reports about strangely corrupted super blocks, where csum type and incompat flags get some obvious garbage, but csum still matches and all other vitals are correct.

This normally means some kernel memory corruption happens, although the cause is unknown, at least detect it and prevent further corruption.

---
 fs/btrfs/disk-io.c | 24 ++++++++++++++++++++----
 1 file changed, 20 insertions(+), 4 deletions(-)
fs/btrfs/disk-io.c +5 -2
index 23803102aa0d..10d814f03f13 100644
@@ -68,7 +68,8 @@
 static const struct extent_io_ops btree_extent_io_ops;
 static void end_workqueue_fn(struct btrfs_work *work);
 static void free_fs_root(struct btrfs_root *root);
-static int btrfs_check_super_valid(struct btrfs_fs_info *fs_info);
+static int btrfs_check_super_valid(struct btrfs_fs_info *fs_info,
+				   struct btrfs_super_block *sb);
 static void btrfs_destroy_ordered_extents(struct btrfs_root *root);
 static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans,
 				      struct btrfs_fs_info *fs_info);
@@ -2680,7 +2681,7 @@ int open_ctree(struct super_block *sb,
 
 	memcpy(fs_info->fsid, fs_info->super_copy->fsid, BTRFS_FSID_SIZE);
 
-	ret = btrfs_check_super_valid(fs_info);
+	ret = btrfs_check_super_valid(fs_info, fs_info->super_copy);
 	if (ret) {
 		btrfs_err(fs_info, "superblock contains fatal errors");
 		err = -EINVAL;
@@ -3575,6 +3576,21 @@ int write_all_supers(struct btrfs_fs_info *fs_info, int max_mirrors)
 	sb = fs_info->super_for_commit;
 	dev_item = &sb->dev_item;
 
+	/* Do extra check on the sb to be written */
+	ret = btrfs_check_super_valid(fs_info, sb);

fs/btrfs/disk-io.c int write_all_supers(struct btrfs_fs_info *fs_info, int max_mirrors) · line 3590

Quoted 10 lines
fs/btrfs/disk-io.c +10 -0
+	if (ret) {
+		btrfs_err(fs_info, "fatal superblock corrupted detected");
+		return -EUCLEAN;
+	}
+	/*
+	 * Unknown incompat flags can't be mounted, so newly developed flags
+	 * means corruption
+	 */
+	if (btrfs_super_incompat_flags(sb) & ~BTRFS_FEATURE_INCOMPAT_SUPP) {
+		btrfs_err(fs_info, "fatal superblock corrupted detected");

The error messages could state that the corruption is detected at the pre-commit time. Otherwise it's a good idea to do the checks, they're lighweight.

Quoted 25 lines
fs/btrfs/disk-io.c +5 -2
+		return -EUCLEAN;
+	}
+
 	mutex_lock(&fs_info->fs_devices->device_list_mutex);
 	head = &fs_info->fs_devices->devices;
 	max_errors = btrfs_super_num_devices(fs_info->super_copy) - 1;
@@ -3985,9 +4001,9 @@ int btrfs_read_buffer(struct extent_buffer *buf, u64 parent_transid, int level,
 					      level, first_key);
 }
 
-static int btrfs_check_super_valid(struct btrfs_fs_info *fs_info)
+static int btrfs_check_super_valid(struct btrfs_fs_info *fs_info,
+				   struct btrfs_super_block *sb)
 {
-	struct btrfs_super_block *sb = fs_info->super_copy;
 	u64 nodesize = btrfs_super_nodesize(sb);
 	u64 sectorsize = btrfs_super_sectorsize(sb);
 	int ret = 0;
Signature · 6 lines
2.17.0

--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html