Return-Path: From: Sasha Levin To: "linux-kernel@vger.kernel.org" , "stable@vger.kernel.org" CC: Liu Bo , David Sterba , "Sasha Levin" Subject: [PATCH AUTOSEL for 4.14 003/100] Btrfs: bail out gracefully rather than BUG_ON Date: Wed, 24 Jan 2018 04:14:22 +0000 Message-ID: <20180124041414.32065-3-alexander.levin@microsoft.com> References: <20180124041414.32065-1-alexander.levin@microsoft.com> In-Reply-To: <20180124041414.32065-1-alexander.levin@microsoft.com> Content-Language: en-US Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Sender: linux-kernel-owner@vger.kernel.org List-ID: From: Liu Bo [ Upstream commit 56a0e706fcf870270878d6d72b71092ae42d229c ] 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. Signed-off-by: Liu Bo Reported-by: Guillaume Bouchard Signed-off-by: David Sterba Signed-off-by: Sasha Levin --- fs/btrfs/inode.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c index d94e3f68b9b1..c71afd424900 100644 --- a/fs/btrfs/inode.c +++ b/fs/btrfs/inode.c @@ -5500,6 +5500,14 @@ static int btrfs_inode_by_name(struct inode *dir, st= ruct dentry *dentry, goto out_err; =20 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); + if (location->type !=3D BTRFS_INODE_ITEM_KEY && + location->type !=3D BTRFS_ROOT_ITEM_KEY) { + btrfs_warn(root->fs_info, +"%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, locat= ion(%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; } =20 - BUG_ON(location.type !=3D BTRFS_ROOT_ITEM_KEY); - index =3D srcu_read_lock(&fs_info->subvol_srcu); ret =3D fixup_tree_root_location(fs_info, dir, dentry, &location, &sub_root); --=20 2.11.0