Skip to contentSkip to conversation

Btrfs: bail out gracefully rather than BUG_ON

PATCH In review Liu Bo 3 participants 6 messages Oct 30, 2017 – Nov 13, 2017 Btrfs file system

lore
Share & tools
lore

Liu Bo

author
#0 lore raw

PATCH 1/1 Btrfs: bail out gracefully rather than BUG_ON

If a file's DIR_ITEM key is invalid (due to memory errors) and gets written to disk, a future lookup_path can end up with kernel panic due to BUG_ON().

This gets rid of the BUG_ON(), meanwhile output the corrupted key and return ENOENT if it's invalid.

---
The diff doesn't show the logic well, 'goto out_err' will return with assigning 0 to location->objectid, and the caller already has a check for (location->objectid == 0) to return -ENOENT.

 fs/btrfs/inode.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
fs/btrfs/inode.c +8 -2
index d94e3f6..916cdc9 100644
@@ -5500,6 +5500,14 @@ static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
 		goto out_err;
 
 	btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
+	if (location->type != BTRFS_INODE_ITEM_KEY &&
+	    location->type != BTRFS_ROOT_ITEM_KEY) {
+		btrfs_warn(root->fs_info,
+	  "%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
+			   __func__, name, btrfs_ino(BTRFS_I(dir)),
+			   location->objectid, location->type, location->offset);
+		goto out_err;
+	}
 out:
 	btrfs_free_path(path);
 	return ret;
@@ -5816,8 +5824,6 @@ struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
 		return inode;
 	}
 
-	BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
-
 	index = srcu_read_lock(&fs_info->subvol_srcu);
 	ret = fixup_tree_root_location(fs_info, dir, dentry,
 				       &location, &sub_root);
Signature · 1 line
2.9.4
author
#1 lore raw
Quoted 7 lines · Liu Bo

If a file's DIR_ITEM key is invalid (due to memory errors) and gets written to disk, a future lookup_path can end up with kernel panic due to BUG_ON().

This gets rid of the BUG_ON(), meanwhile output the corrupted key and return ENOENT if it's invalid.

The kernel panic is originally

Thanks,

-liubo

Quoted 44 lines

---
The diff doesn't show the logic well, 'goto out_err' will return with assigning 0 to location->objectid, and the caller already has a check for (location->objectid == 0) to return -ENOENT.

 fs/btrfs/inode.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
fs/btrfs/inode.c +8 -2
index d94e3f6..916cdc9 100644
@@ -5500,6 +5500,14 @@ static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
 		goto out_err;
 
 	btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
+	if (location->type != BTRFS_INODE_ITEM_KEY &&
+	    location->type != BTRFS_ROOT_ITEM_KEY) {
+		btrfs_warn(root->fs_info,
+	  "%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
+			   __func__, name, btrfs_ino(BTRFS_I(dir)),
+			   location->objectid, location->type, location->offset);
+		goto out_err;
+	}
 out:
 	btrfs_free_path(path);
 	return ret;
@@ -5816,8 +5824,6 @@ struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
 		return inode;
 	}
 
-	BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
-
 	index = srcu_read_lock(&fs_info->subvol_srcu);
 	ret = fixup_tree_root_location(fs_info, dir, dentry,
 				       &location, &sub_root);
Signature · 6 lines
2.9.4

--
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
#2 lore raw
In reply to Liu Bo
Quoted 21 lines · Liu Bo

If a file's DIR_ITEM key is invalid (due to memory errors) and gets written to disk, a future lookup_path can end up with kernel panic due to BUG_ON().

This gets rid of the BUG_ON(), meanwhile output the corrupted key and return ENOENT if it's invalid.

---
The diff doesn't show the logic well, 'goto out_err' will return with assigning 0 to location->objectid, and the caller already has a check for (location->objectid == 0) to return -ENOENT.

 fs/btrfs/inode.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
fs/btrfs/inode.c +0 -0
index d94e3f6..916cdc9 100644

fs/btrfs/inode.c static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, · line 5509

Quoted 10 lines
fs/btrfs/inode.c +7 -0
@@ -5500,6 +5500,14 @@ static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
 		goto out_err;
 
 	btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
+	if (location->type != BTRFS_INODE_ITEM_KEY &&
+	    location->type != BTRFS_ROOT_ITEM_KEY) {
+		btrfs_warn(root->fs_info,
+	  "%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
+			   __func__, name, btrfs_ino(BTRFS_I(dir)),
+			   location->objectid, location->type, location->offset);
+		goto out_err;

If this situation happens it's possible that ret is still 0 so the error handling in btrfs_lookup_dentry might not trigger. How about just setting -EUCLEAN if we execute the if branch?

Quoted 14 lines
fs/btrfs/inode.c +1 -2
+	}
 out:
 	btrfs_free_path(path);
 	return ret;
@@ -5816,8 +5824,6 @@ struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
 		return inode;
 	}
 
-	BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
-
 	index = srcu_read_lock(&fs_info->subvol_srcu);
 	ret = fixup_tree_root_location(fs_info, dir, dentry,
 				       &location, &sub_root);
author
#3 lore raw
Quoted 26 lines · Nikolay Borisov
Quoted 23 lines · Liu Bo

If a file's DIR_ITEM key is invalid (due to memory errors) and gets written to disk, a future lookup_path can end up with kernel panic due to BUG_ON().

This gets rid of the BUG_ON(), meanwhile output the corrupted key and return ENOENT if it's invalid.

---
The diff doesn't show the logic well, 'goto out_err' will return with assigning 0 to location->objectid, and the caller already has a check for (location->objectid == 0) to return -ENOENT.

 fs/btrfs/inode.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
fs/btrfs/inode.c +0 -0
index d94e3f6..916cdc9 100644
@@ -5500,6 +5500,14 @@ static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
 		goto out_err;
 
Quoted 13 lines

fs/btrfs/inode.c static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, · line 5509

Quoted 8 lines
fs/btrfs/inode.c +7 -0
 	btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
+	if (location->type != BTRFS_INODE_ITEM_KEY &&
+	    location->type != BTRFS_ROOT_ITEM_KEY) {
+		btrfs_warn(root->fs_info,
+	  "%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
+			   __func__, name, btrfs_ino(BTRFS_I(dir)),
+			   location->objectid, location->type, location->offset);
+		goto out_err;

If this situation happens it's possible that ret is still 0 so the error handling in btrfs_lookup_dentry might not trigger. How about just setting -EUCLEAN if we execute the if branch?

Thanks for the comment. So it's not going to trigger the (ret < 0) check, instead it tries to trigger the one following (ret < 0):

        if (location.objectid == 0)
                return ERR_PTR(-ENOENT);

which is consistent to the case when we didn't find the btrfs_dir_item (di is NULL, returned by btrfs_lookup_dir_item()).

thanks,
-liubo

Quoted 14 lines
Quoted 14 lines
fs/btrfs/inode.c +1 -2
+	}
 out:
 	btrfs_free_path(path);
 	return ret;
@@ -5816,8 +5824,6 @@ struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
 		return inode;
 	}
 
-	BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
-
 	index = srcu_read_lock(&fs_info->subvol_srcu);
 	ret = fixup_tree_root_location(fs_info, dir, dentry,
 				       &location, &sub_root);
#4 lore raw
Quoted 41 lines · Liu Bo
Quoted 26 lines · Nikolay Borisov
Quoted 23 lines · Liu Bo

If a file's DIR_ITEM key is invalid (due to memory errors) and gets written to disk, a future lookup_path can end up with kernel panic due to BUG_ON().

This gets rid of the BUG_ON(), meanwhile output the corrupted key and return ENOENT if it's invalid.

---
The diff doesn't show the logic well, 'goto out_err' will return with assigning 0 to location->objectid, and the caller already has a check for (location->objectid == 0) to return -ENOENT.

 fs/btrfs/inode.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
fs/btrfs/inode.c +0 -0
index d94e3f6..916cdc9 100644
@@ -5500,6 +5500,14 @@ static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
 		goto out_err;
 
Quoted 13 lines

fs/btrfs/inode.c static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, · line 5509

Quoted 8 lines
fs/btrfs/inode.c +7 -0
 	btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
+	if (location->type != BTRFS_INODE_ITEM_KEY &&
+	    location->type != BTRFS_ROOT_ITEM_KEY) {
+		btrfs_warn(root->fs_info,
+	  "%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
+			   __func__, name, btrfs_ino(BTRFS_I(dir)),
+			   location->objectid, location->type, location->offset);
+		goto out_err;

If this situation happens it's possible that ret is still 0 so the error handling in btrfs_lookup_dentry might not trigger. How about just setting -EUCLEAN if we execute the if branch?

Quoted 8 lines

Thanks for the comment. So it's not going to trigger the (ret < 0) check, instead it tries to trigger the one following (ret < 0):

        if (location.objectid == 0)
                return ERR_PTR(-ENOENT);

which is consistent to the case when we didn't find the btrfs_dir_item (di is NULL, returned by btrfs_lookup_dir_item()).

You are correct, i should have read 2 lines down ;)

Quoted 20 lines

thanks,
-liubo

Quoted 14 lines
Quoted 14 lines
fs/btrfs/inode.c +1 -2
+	}
 out:
 	btrfs_free_path(path);
 	return ret;
@@ -5816,8 +5824,6 @@ struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
 		return inode;
 	}
 
-	BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
-
 	index = srcu_read_lock(&fs_info->subvol_srcu);
 	ret = fixup_tree_root_location(fs_info, dir, dentry,
 				       &location, &sub_root);
Reviewed-by on v1 1/1
#5 lore raw
In reply to Liu Bo
Quoted 8 lines · Liu Bo

If a file's DIR_ITEM key is invalid (due to memory errors) and gets written to disk, a future lookup_path can end up with kernel panic due to BUG_ON().

This gets rid of the BUG_ON(), meanwhile output the corrupted key and return ENOENT if it's invalid.

Quoted 4 lines

---
The diff doesn't show the logic well, 'goto out_err' will return with assigning 0 to location->objectid, and the caller already has a check for (location->objectid == 0) to return -ENOENT.

Feel free to send a cleanup patch.