Received: by 2002:a05:6358:bb9e:b0:b9:5105:a5b4 with SMTP id df30csp4372187rwb; Tue, 6 Sep 2022 06:42:54 -0700 (PDT) X-Google-Smtp-Source: AA6agR6odnTg+rIFJA7ChEKZGSUQVKoBOTNoFiLzyi4VreJRG9YI6JF6oqIGKUlplF4gbBuodPLp X-Received: by 2002:a17:907:2ce7:b0:741:4973:3fc9 with SMTP id hz7-20020a1709072ce700b0074149733fc9mr33371482ejc.678.1662471774518; Tue, 06 Sep 2022 06:42:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662471774; cv=none; d=google.com; s=arc-20160816; b=D0ntmUOY9oxmNA/D8wO80Q2pFUzK1oSQ2rtRtgaXefmZoOSRkVvlukUgh+QdNhs1Jf Tr5s7HObKfoqVTO6vVlVe8UAYJ4H2WHnvd9v+1Duy/B3OkvLs42TCYCBlJjT8f/ACi6p jtVzUmmdIohfNMpSThTyPtOEITAGeKnPXWz5y6i12iDvqybuYeMmMZOWsLvJo0gGuqVY Gh3C6DKdF8fvNuMFBHC8C2rrEHRQaJkO2HN8X2VlmkXzxRR/pKZVdb9fzYCvlrl28ukM 58JaZgiktZRfMmEdh3y9fDPm9Wtp1hTSRjwgwtzsZri+oFcszYfawCS0BClA40oZZ/fB +nSw== 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=NTZvhHWSsI3MpUiDWE9W9YhhwTuK0X4EEg8zhXEuuPA=; b=F76vO73z+JxzcduX2f5W0JHCsdn9dAo0l9UsjIpTNBu2MJ+8OiD5XyHHnnJfQitDxP ZNxTr/ngyGYr1PKHrpKujyh+prpHxO01m5gVeDXwGNqJZ4LPrheRnHABrCgob9b2lch1 1e8sky7RI3FrB9puwA33lNDkdb4i1jvOdd4+ApzM0ZIZ+x3yhNji0x1fVHjURH2Q+z6m umfOcpzFveACLIuvk9cpK4ORLV3FCi+BM/LzCm7E+nLar0jFJ9hwszR+HTYZRzMLM0cf pQyNPP9AhZZCz42/WjktW+So4/pzRq1pzzHcmPfMV5YN+ajgayFWzbaxTDLVQKaEIqG6 eJaA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=07HjAYw8; 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 p13-20020a056402154d00b0044da16b4e30si5968541edx.306.2022.09.06.06.42.25; Tue, 06 Sep 2022 06:42:54 -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=07HjAYw8; 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 S234226AbiIFNl2 (ORCPT + 99 others); Tue, 6 Sep 2022 09:41:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38314 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234091AbiIFNkS (ORCPT ); Tue, 6 Sep 2022 09:40:18 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EDBF77DF6E; Tue, 6 Sep 2022 06:37:08 -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 3D3E961545; Tue, 6 Sep 2022 13:35:36 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 45E94C433C1; Tue, 6 Sep 2022 13:35:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1662471335; bh=PyZMdcACp8aTscjyxRfpRWX7/YNAAuHy+coEThDCwoM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=07HjAYw80ureUN+dIRbYhVvO9wzmyBZ5xtSflug6JnSOrDL035nwZHKdI9U1tIBXF DSkzuzeabhfk6V9rSmek3fmrL1jCn0zSBb3w9OLx/0j2VloEYzWMLoxU9KrtXRrNXG mzE2FAf3J//iHieNjCQl2z7Ve1gSRC15kIfss2II= 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.10 74/80] btrfs: harden identification of a stale device Date: Tue, 6 Sep 2022 15:31:11 +0200 Message-Id: <20220906132820.223588978@linuxfoundation.org> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20220906132816.936069583@linuxfoundation.org> References: <20220906132816.936069583@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 @@ -540,15 +540,47 @@ error: 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; + 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; } /* @@ -581,9 +613,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 */