Received: by 2002:a05:6358:489b:b0:bb:da1:e618 with SMTP id x27csp6826231rwn; Tue, 13 Sep 2022 09:29:04 -0700 (PDT) X-Google-Smtp-Source: AA6agR7gWWwuK+juf/eZ4/Y7RoEC4IjCCmen7/kaEVyAg7jVC/m5qmM/OM1HEeQXhakBLWRQLXW/ X-Received: by 2002:a65:6951:0:b0:42b:b13:b253 with SMTP id w17-20020a656951000000b0042b0b13b253mr29149527pgq.555.1663086544709; Tue, 13 Sep 2022 09:29:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663086544; cv=none; d=google.com; s=arc-20160816; b=mmxEIW973ry/KZEZ5qn38ZJnO8YrttXSxUM72wJuUPkoojLMYQd7cO/OBNl9I3CogS Iq0wORCKjas3nW7+z1kZNxel6SLBnz3c6oFIrH3bg91KpU7j/Jqxzhm3MvE1sHF2c8rG 5R7NMd9n4jb56T2tHxW/ObRqDqHe/1Xguf57JEyVt8CG21JzYbyqv+UXuCDa3xPSZI57 Pco8K+dKKptVhpt3pKOQMEn2rC8DpEsFpdWvMWTV9Wwyt4nBCOhWfiY47l+RZRTxARNV 3XcLmRw6iKjKH2TWmRYF5P+5IKE8TqswpbsBUDbjS3S0XKDa52j++tlrE13bXHtUn8Sl i1DA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=R2buz5Y6kZmhtwezvYyw9VKAWf/ssn5gEID0S0fAWN0=; b=D9p5IIwd1LqrwLY/8mVOJhzGeMomkgskp6451kMTcexebqcqO1raqH8kP86hXTSRJ9 WfaB28WVEhkWm/HktzKt5oTO7eetEN90TredDAqfJXLUTpkZPMWWDeAHHaDz+WUCFcLn RrRhLn/SKKJ43c+0mtCF9wK/cn1tDgajukVgoOGaCoqkTMv/nFCTzruvLHcMC7vCwitm pnfIOuTZF6ZZdF+Ea6EDxeCZuFmmfpkt2ekXSijipvTG9tLQt6y8KPi284h5UGqtrTDu w9RvPrXanffWmmcy4mIa6cOAtilx1oXWEUg+n48AslMdbSt7BOndK3NxsomF2PY3cR9v o+BA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=kYM+Iriy; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id w136-20020a627b8e000000b0052e7d706a6fsi2264655pfc.149.2022.09.13.09.28.50; Tue, 13 Sep 2022 09:29:04 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=kYM+Iriy; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232323AbiIMPGn (ORCPT + 99 others); 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-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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); } -static bool device_path_matched(const char *path, struct btrfs_device *device) +/* + * 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 = kzalloc(BTRFS_PATH_NAME_MAX, GFP_KERNEL); + if (!device_name) + return false; rcu_read_lock(); - found = strcmp(rcu_str_deref(device->name), path); + scnprintf(device_name, BTRFS_PATH_NAME_MAX, "%s", rcu_str_deref(device->name)); rcu_read_unlock(); - return found == 0; + bdev_old = lookup_bdev(device_name); + kfree(device_name); + if (IS_ERR(bdev_old)) + return false; + + bdev_new = lookup_bdev(path); + if (IS_ERR(bdev_new)) + return false; + + if (bdev_old == bdev_new) + return true; + + return false; } /* @@ -754,9 +786,7 @@ static int btrfs_free_stale_devices(cons &fs_devices->devices, dev_list) { if (skip_device && skip_device == 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 */