Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp921693pxb; Wed, 6 Apr 2022 04:21:30 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwiqti7no4zY0TKCZUZhiC5216yvW/UlmusltPYhcwJWStBYaJMOriWrueOiC4m/2ERgKvI X-Received: by 2002:a17:902:da8f:b0:156:9cc4:1b07 with SMTP id j15-20020a170902da8f00b001569cc41b07mr8112733plx.20.1649244090727; Wed, 06 Apr 2022 04:21:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649244090; cv=none; d=google.com; s=arc-20160816; b=f0OW/xDZquk7TBjgWEJ3VDIwjoVYuTIkF6XfMlWRZKi7886CaXuURz50oIdaPIMBy4 nQ/AjaKkzq2+WlCpFYjVQ8tgPScwdUsfnE3d4PT8kNzRxLCp0S/HQRDdwtXmgJ1KWeGH CBwfnZTq5MxhhEu2NoelUd9o8mR9VIDlz3KGAc+w/5B6OvHvQ1HodbjmwAXaAW0Y9454 MquSKPPeif4Fl7EsvjpuFrPA3HB+WJk3PyS0Hs1zbaXYWDWy2+KHepWB4ah4YFhiWDNF zDGQBF/MyKid6FYFFHpFkk4V0QfKUH8BjYMg58KiNnjWsPpXmn1TvQHEHLKCvOpv64E2 bUEA== 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=Xauv7h7m73e0ayf4S+ymcV6iyOhnoM10TYy0KzBoDEQ=; b=jfNZDTW/bib3lujOm4eh0k71dyrZqBIEsvD5XWgN3PMpvrGFAOPpG1iduBGweb+C6/ Xr5FUgl3SSApoJAIGZBP/MY54qGNfWAzPKXRRnomkGJ6k0HKy8v3zlLD2/CpI1V12ghI dTgkano+slNFrte53fbgo4qdBM2kl+ElK4abPud02lVVLllvW4Jc+a/mcaKkLVK+dLWE gw5KqFX8in2uQoLOVq0dtv5afldEjZHUvgDzZwm/FEA0yHY/DIQkJya3BtKa+rHej44O j4BfcLTWZbxym5TdY86ur3tFCfbsHeqw5tSWEuBIDajjoIvzJkWMSOD96YVQl0nx7+S3 BR2Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="xID2Orn/"; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id a38-20020a056a001d2600b004fe50440a05si1893901pfx.331.2022.04.06.04.21.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Apr 2022 04:21:30 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="xID2Orn/"; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 657135E97B0; Wed, 6 Apr 2022 02:43:12 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1353373AbiDFBoB (ORCPT + 99 others); Tue, 5 Apr 2022 21:44:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48094 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1350855AbiDEKAG (ORCPT ); Tue, 5 Apr 2022 06:00:06 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 32D486A424; Tue, 5 Apr 2022 02:51:20 -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 ams.source.kernel.org (Postfix) with ESMTPS id C45F3B81B76; Tue, 5 Apr 2022 09:51:18 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 21942C385A1; Tue, 5 Apr 2022 09:51:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1649152277; bh=VEzzR77h+TVNn8t6F21fg2748P4148M/vVkJolOx9t8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=xID2Orn/c/yCt/skZiLipFLfo2X1+Jxoi8Ox+g4SH9cb+8NH2frCtzuLylGnolAPn /Ibgtz1Ox01bG9phhF87kKPSX3zkDwOj8zm3puS7ctrrrZPVNtkeyilWpi2hxncUwk 4+KFgveMWBMo+CjQ+iR5lTGyFYJpibcL9fkOElms= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Josef Bacik , Anand Jain , David Sterba , Sasha Levin Subject: [PATCH 5.15 719/913] btrfs: harden identification of a stale device Date: Tue, 5 Apr 2022 09:29:41 +0200 Message-Id: <20220405070401.385203742@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220405070339.801210740@linuxfoundation.org> References: <20220405070339.801210740@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no 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 [ Upstream commit 770c79fb65506fc7c16459855c3839429f46cb32 ] 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: Sasha Levin --- fs/btrfs/volumes.c | 45 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 38 insertions(+), 7 deletions(-) diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c index 06a1a7c2254c..33bd94ee4690 100644 --- a/fs/btrfs/volumes.c +++ b/fs/btrfs/volumes.c @@ -530,15 +530,48 @@ btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder, return ret; } -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; + dev_t dev_old; + dev_t dev_new; + int ret; + + /* + * 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; + ret = lookup_bdev(device_name, &dev_old); + kfree(device_name); + if (ret) + return false; + + ret = lookup_bdev(path, &dev_new); + if (ret) + return false; + + if (dev_old == dev_new) + return true; + + return false; } /* @@ -571,9 +604,7 @@ static int btrfs_free_stale_devices(const char *path, &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 */ -- 2.34.1