From nobody Mon Sep 8 08:57:28 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 20B26C6FA82 for ; Tue, 13 Sep 2022 15:06:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235382AbiIMPGn (ORCPT ); Tue, 13 Sep 2022 11:06:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55186 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235447AbiIMPE2 (ORCPT ); Tue, 13 Sep 2022 11:04:28 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 41371A1BF; Tue, 13 Sep 2022 07:30:18 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id B041F61414; Tue, 13 Sep 2022 14:28:41 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id C1A7FC433C1; Tue, 13 Sep 2022 14:28:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1663079321; bh=3PfSelEa4MEVsI+JJBE451z1V1ZOGqS2BljCPbdag0o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=kYM+Iriy/5HlsJOdrVP0Dsfx52CS0gjMXFa5uiKGWStUJllyv9XBT4GJXn1zfxcyF V8TCTprAe0P0Haj2cO78gC8C9g0XKWq3Mj1TKElZsoMX4Uho69Wz7czELVFahLPB4e IITA+rbDGiJzSU9BYy2jPXklBeZUvVHn1MdLsT8M= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Josef Bacik , Anand Jain , David Sterba Subject: [PATCH 5.4 062/108] btrfs: harden identification of a stale device Date: Tue, 13 Sep 2022 16:06:33 +0200 Message-Id: <20220913140356.286481668@linuxfoundation.org> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20220913140353.549108748@linuxfoundation.org> References: <20220913140353.549108748@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Anand Jain commit 770c79fb65506fc7c16459855c3839429f46cb32 upstream. Identifying and removing the stale device from the fs_uuids list is done by btrfs_free_stale_devices(). btrfs_free_stale_devices() in turn depends on device_path_matched() to check if the device appears in more than one btrfs_device structure. The matching of the device happens by its path, the device path. However, when device mapper is in use, the dm device paths are nothing but a link to the actual block device, which leads to the device_path_matched() failing to match. Fix this by matching the dev_t as provided by lookup_bdev() instead of plain string compare of the device paths. Reported-by: Josef Bacik Signed-off-by: Anand Jain Signed-off-by: David Sterba Signed-off-by: Greg Kroah-Hartman --- fs/btrfs/volumes.c | 44 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 7 deletions(-) --- a/fs/btrfs/volumes.c +++ b/fs/btrfs/volumes.c @@ -713,15 +713,47 @@ static void pending_bios_fn(struct btrfs run_scheduled_bios(device); } =20 -static bool device_path_matched(const char *path, struct btrfs_device *dev= ice) +/* + * Check if the device in the path matches the device in the given struct = device. + * + * Returns: + * true If it is the same device. + * false If it is not the same device or on error. + */ +static bool device_matched(const struct btrfs_device *device, const char *= path) { - int found; + char *device_name; + struct block_device *bdev_old; + struct block_device *bdev_new; + + /* + * If we are looking for a device with the matching dev_t, then skip + * device without a name (a missing device). + */ + if (!device->name) + return false; + + device_name =3D kzalloc(BTRFS_PATH_NAME_MAX, GFP_KERNEL); + if (!device_name) + return false; =20 rcu_read_lock(); - found =3D strcmp(rcu_str_deref(device->name), path); + scnprintf(device_name, BTRFS_PATH_NAME_MAX, "%s", rcu_str_deref(device->n= ame)); rcu_read_unlock(); =20 - return found =3D=3D 0; + bdev_old =3D lookup_bdev(device_name); + kfree(device_name); + if (IS_ERR(bdev_old)) + return false; + + bdev_new =3D lookup_bdev(path); + if (IS_ERR(bdev_new)) + return false; + + if (bdev_old =3D=3D bdev_new) + return true; + + return false; } =20 /* @@ -754,9 +786,7 @@ static int btrfs_free_stale_devices(cons &fs_devices->devices, dev_list) { if (skip_device && skip_device =3D=3D device) continue; - if (path && !device->name) - continue; - if (path && !device_path_matched(path, device)) + if (path && !device_matched(device, path)) continue; if (fs_devices->opened) { /* for an already deleted device return 0 */