From patchwork Mon Apr 3 05:46:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anand Jain X-Patchwork-Id: 13197679 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 C9784C761A6 for ; Mon, 3 Apr 2023 05:46:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231150AbjDCFqg (ORCPT ); Mon, 3 Apr 2023 01:46:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51136 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229545AbjDCFqf (ORCPT ); Mon, 3 Apr 2023 01:46:35 -0400 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 394B75B9A; Sun, 2 Apr 2023 22:46:34 -0700 (PDT) Received: from pps.filterd (m0333520.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 3330qGdC020222; Mon, 3 Apr 2023 05:46:30 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : content-transfer-encoding : content-type : mime-version; s=corp-2022-7-12; bh=hSyFJZbhNnIGNRXG2mlEKTCqemUEAi39FiSlSUpatpM=; b=FDoSZoP1ir2OBrvnEMQsY5PQbsn8nDQSEQ0sicU4qr+tyFBgvmkb4fRv7ZZb39MYy05O GIDUVoNpFePg+B+AImDYxr/oyQRMYu9lumzz7QlonnvlbzKBxJmuqSq5ypiji2OCRyyj 57Hr8MJT5xvowZXmJOYjbW/VF1fCa4ImLInXphD8+vr788VOr59koCUogoRj/KWOdJsi nF+IeclUsphi5Fw+CsNFEMi+nYWbQCV7jqJh8RVIEXalZHrN3E92YF5DKfJ85TPbtoHO 2mca/noB5X6DLGkHb4k51Iviqn0ieyg64d/vEo21s+rLtTLtUYn7b4kEw4ocYVggJ7JL MA== Received: from iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta03.appoci.oracle.com [130.35.103.27]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3ppcnct5hr-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 03 Apr 2023 05:46:30 +0000 Received: from pps.filterd (iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (8.17.1.19/8.17.1.19) with ESMTP id 3335KUgi028928; Mon, 3 Apr 2023 05:46:29 GMT Received: from nam11-dm6-obe.outbound.protection.outlook.com (mail-dm6nam11lp2169.outbound.protection.outlook.com [104.47.57.169]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 3pptumk21m-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 03 Apr 2023 05:46:29 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=WDVMprmXj70bfHEtkEnA2bXFK5TLnbaH1RJbJ0mx42xn2zY3gZvmMCvsMuDqhGcUj92JT+0uhN8t6Uyv0CfYVCf/YwENjXvbfEyAtPlRm1LmCbGnWiHp/oWO2ZuDR06SOKDWP05ItV24n8xC1DO7NUCJ6pIuIEdMbMiR8er9DeaiiXeFUW1OcEozZZe61OEI26gyBmsDN3XwoNIToGk1wgax9fcYdWhhAKaHzpqShcu67tGKS9JRRDVH9xbR+B/zkfmhBtIUlXnjHciCdC2M76j1+tkYICwLOCtFaQUgC3CpCmb9bskH8WShik8+ak7emtA7eW6ggamUGOolzzKtLA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=hSyFJZbhNnIGNRXG2mlEKTCqemUEAi39FiSlSUpatpM=; b=ZMbVEkVbXrlO2OKFxSSVTwGeasqaUlc05FptraVxd7KWff5u9ORRdcy3+pGKDWjWmvt8+YtMCkR1RXPMj4TimO9LDlbwi1F10zO5Etp6DbSk0TAOT6SaBR/u2t4tiZORvgZSgtT7wlno5DLuzwuf/0AhQE26PiblRGvPXVfuoqkIWAFcKR/dp5w33nTVAOALk62Z/O7alLvSdIkYeKY43zxO2tfKfwJynPbSnKOL2+HoNxOeUG0iUuJmxPA4YMGJVTixUraJvbYZPGPjTjjh9pKibfVCm+7+xiFblpZ0uzH9wQHatP53AKoPkyRnY5l50XqMAmcMtnIZV38o4zpb6A== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=hSyFJZbhNnIGNRXG2mlEKTCqemUEAi39FiSlSUpatpM=; b=XeGUjO06S1GaTWGZP1YVmEDp1XDdpOHTP4tMpPD1vjCPEo7dBgTbfFuYw4whfmhYK1RBB4PicwAPo5nRy4esF9Wa9/Wo8UVWczFsaAsYPlHkp5+6GLZybsVl26sLZ7w4yKS2KSFAFjyuugQNDhSv5Z9P7+TYJ3LqYFn26GTBLOs= Received: from PH0PR10MB5706.namprd10.prod.outlook.com (2603:10b6:510:148::10) by PH0PR10MB4407.namprd10.prod.outlook.com (2603:10b6:510:31::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6254.28; Mon, 3 Apr 2023 05:46:27 +0000 Received: from PH0PR10MB5706.namprd10.prod.outlook.com ([fe80::3eb1:c999:6a64:205c]) by PH0PR10MB5706.namprd10.prod.outlook.com ([fe80::3eb1:c999:6a64:205c%5]) with mapi id 15.20.6254.026; Mon, 3 Apr 2023 05:46:27 +0000 From: Anand Jain To: stable@vger.kernel.org Cc: linux-btrfs@vger.kernel.org, Anand Jain , Sherry Yang , kernel test robot , David Sterba Subject: [PATCH stable-5.4.y stable-5.10.y] btrfs: scan device in non-exclusive mode Date: Mon, 3 Apr 2023 13:46:08 +0800 Message-Id: X-Mailer: git-send-email 2.39.2 X-ClientProxiedBy: SI2PR04CA0016.apcprd04.prod.outlook.com (2603:1096:4:197::7) To PH0PR10MB5706.namprd10.prod.outlook.com (2603:10b6:510:148::10) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: PH0PR10MB5706:EE_|PH0PR10MB4407:EE_ X-MS-Office365-Filtering-Correlation-Id: 4c7ea477-87ec-4fd1-77de-08db3406c460 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 6Bh5dcS8p/RhRnlP2BLMuw4DbOczFBLfRsbMqQeufSRhE2MD7Mt3b+5nhUrDi4u9HYxn0LdLjADrJf5r5v2nltSq18kOw5yikKTraTqy5dbvOdELJsriWqncHuKMdoYf012QbJFI26dRIhyUIG+d1yv2l6fi0+goXrKpSjoKJq5N3jWl/wy1BmVe5vXo73vk1zihMq6eD9T8K6pO+oQfyh9FzNolFGDYDM9eFDF5wkNmSfdlu84HHQp2A5OkvlPdyvwZ5CXOndfBicLvSE1tgHJlGQdPWVD4k+Jd262t7QjczdhW7C0UBV9snJP3u5GUnvP9x4YrORJ9pahgCvoJ7EIq4tWduj0MvxYyJYJdXiBg6NndfCNPS8Tv/Id4Zdq6zHtneCDNQ4VQBBwQJGpOvm3kCMSKwW/JdyEQQHEj7COpb++CtJSctfr854AVUnJOpHJWPew79O6ADvpaNAvZZLlmxP7GOQVup0gHDYka42y/aGVkaTQcF7Ye1FDGWTm2iOgjXYwyyf8nK79q9S+VheJkvNA2FH6Kq2Ph8iNkoh4= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:PH0PR10MB5706.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230028)(376002)(346002)(39860400002)(136003)(366004)(396003)(451199021)(6486002)(966005)(83380400001)(26005)(6666004)(6512007)(6506007)(186003)(41300700001)(2616005)(36756003)(86362001)(38100700002)(5660300002)(66946007)(478600001)(66476007)(6916009)(8676002)(4326008)(66556008)(2906002)(8936002)(44832011)(316002)(54906003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: fnuLyUOQs7xjn1V81DbAJbmFW2oITNsm0PRwQN8ePPP9yl26J0ev9qSK6ew3RuNjg8uKH1zSV2YQF/Mc79P/U2xVaYjAaP68hSjq8y1VOisOtXpi+d1FoCb2ulAXXMm+Lp7rgDATqzRCT3xy14Zq9C5letc0TRcJ8tcMEXXyOSuOSodp3AN8kFdc9i6fyxO/tpDQELUSc6DAszIKngG6XHpUOATOqlAL06YGX/8kI2UdYjH8tFfOtSFRJ7DHu9SXqjGRB3e8aQzjVSvIuXr2qfnI0SpcvUfqXtfCoAIHt/66tyOIleG9efQeJTfdBvPOvI1Of0bX3gRnQ4fa6Nc4VoxDOQBtY76wnoFKjDJL7frkzcCac5+RWr06K1Oev/sXSzBzzSIEMCgHd11ghnEwGwyuSnBPSIucFBhZwjWwPv+xcamK23RS0TZfYKx4xbbeUUvg3ucQ3PK3Ws4J8S1ErRlWjeFzW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2nu5iUr66IXFBDeQdJg9JHyAjeg X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: MxEPHR2ELpSfOxF+yFT67PuBWnp4oOHcn/l7OShh+Uz7Fn53n5fKFQgxM/h8Px7Yo1vQYJeMx7Xhcz4565IQS6oClSd6Jp7IPw5oJCSFBNN0A0n7oJAmIZGQeVPOz98sRddAebaBNRdepQSXG9xBrBNlnZznXjwZmwxIAdcDmVxReyo4bwuXKHfybCeFg82beU5W7e7mK9RTjs/IDH6+406W3BQ9vTsKK4yjGFHpLJbsaeQ5n5EnWVCHWKruYy2CIH+24KOQzxBoDCBH2tg1rKBcztG/0WCHK/C19B8vJh7/v8GPYExzWem8uCjW6MhDFmYsH5vX8MaOOQlpTy3ri9g6RiUcUQzZeswH2dUV5p/q1erwInJobMuTpIzGpiYjgR957mFce0QrFsebaQrFfK7esN5V2SWekrcPy7NRvNMtrafuJxHz3YMN7vAdlK9QKUIhX9yjfTwbsYKZc+Pt8/Kf7yPYrYxO6/sxSdWwGIAbxn4p1iBkn1L2L7S+dxVferAN2sLuWmeiCWYXyuaQrSaNMMAzPWksHPQZ9itv9+Z/lNfrQkuGooN2VfJBhVUtLW6pr+M3bRkiRx/IH1trn98XpqDWbs6TkdJqSWVkHCRTE649BgllNuNl4B6hduhubn/ZGorlbfBxe8LWPXwlipwwVwjCw7MOjYl2eXK7TtqLiupLZbUhYia3i340vTzT5uM6BX+FbkUAdgI2CXMkSenn8k2IQ1O4S3kpk7NUDRDmxNy6S5OI97sdzcIzEZoTYeTsi2kmpOx7ZajDZ9FTspX7TX97R94SMqatoS3mwWRZbPrjcYoMQ1cYmmJvY3Ms X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 4c7ea477-87ec-4fd1-77de-08db3406c460 X-MS-Exchange-CrossTenant-AuthSource: PH0PR10MB5706.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Apr 2023 05:46:27.4303 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: r6Zcwj82th+e8sM0pLP/JFCXIoWAX8x98/UnY1Qg5O7AZzzVObcea4OO4vmnp3lLuCo8TkNjCYfdWzenpJm50g== X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH0PR10MB4407 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.254,Aquarius:18.0.942,Hydra:6.0.573,FMLib:17.11.170.22 definitions=2023-04-03_02,2023-03-31_01,2023-02-09_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 spamscore=0 mlxscore=0 malwarescore=0 mlxlogscore=999 adultscore=0 phishscore=0 suspectscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2303200000 definitions=main-2304030042 X-Proofpoint-GUID: 6CWueazhXAWjdbm2Jp_Q4lqoc2qGOG8l X-Proofpoint-ORIG-GUID: 6CWueazhXAWjdbm2Jp_Q4lqoc2qGOG8l Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org commit 50d281fc434cb8e2497f5e70a309ccca6b1a09f0 upstream. This fixes mkfs/mount/check failures due to race with systemd-udevd scan. During the device scan initiated by systemd-udevd, other user space EXCL operations such as mkfs, mount, or check may get blocked and result in a "Device or resource busy" error. This is because the device scan process opens the device with the EXCL flag in the kernel. Two reports were received: - btrfs/179 test case, where the fsck command failed with the -EBUSY error - LTP pwritev03 test case, where mkfs.vfs failed with the -EBUSY error, when mkfs.vfs tried to overwrite old btrfs filesystem on the device. In both cases, fsck and mkfs (respectively) were racing with a systemd-udevd device scan, and systemd-udevd won, resulting in the -EBUSY error for fsck and mkfs. Reproducing the problem has been difficult because there is a very small window during which these userspace threads can race to acquire the exclusive device open. Even on the system where the problem was observed, the problem occurrences were anywhere between 10 to 400 iterations and chances of reproducing decreases with debug printk()s. However, an exclusive device open is unnecessary for the scan process, as there are no write operations on the device during scan. Furthermore, during the mount process, the superblock is re-read in the below function call chain: btrfs_mount_root btrfs_open_devices open_fs_devices btrfs_open_one_device btrfs_get_bdev_and_sb So, to fix this issue, removes the FMODE_EXCL flag from the scan operation, and add a comment. The case where mkfs may still write to the device and a scan is running, the btrfs signature is not written at that time so scan will not recognize such device. Reported-by: Sherry Yang Reported-by: kernel test robot Link: https://lore.kernel.org/oe-lkp/202303170839.fdf23068-oliver.sang@intel.com CC: stable@vger.kernel.org # 5.4+ Signed-off-by: Anand Jain Reviewed-by: David Sterba Signed-off-by: David Sterba Signed-off-by: Anand Jain --- The upstream commit 50d281fc434cb8e2497f5e70a309ccca6b1a09f0 can be applied without conflict to LTS stable-5.15.y and stable-6.1.y. However, on LTS stable-5.4.y and stable-5.15.y, a conflict fix is required since the zoned device support commits are not present in these versions. This patch resolves the conflicts. fs/btrfs/volumes.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c index 3fa972a43b5e..c5944c61317f 100644 --- a/fs/btrfs/volumes.c +++ b/fs/btrfs/volumes.c @@ -1579,8 +1579,17 @@ struct btrfs_device *btrfs_scan_one_device(const char *path, fmode_t flags, * later supers, using BTRFS_SUPER_MIRROR_MAX instead */ bytenr = btrfs_sb_offset(0); - flags |= FMODE_EXCL; + /* + * Avoid using flag |= FMODE_EXCL here, as the systemd-udev may + * initiate the device scan which may race with the user's mount + * or mkfs command, resulting in failure. + * Since the device scan is solely for reading purposes, there is + * no need for FMODE_EXCL. Additionally, the devices are read again + * during the mount process. It is ok to get some inconsistent + * values temporarily, as the device paths of the fsid are the only + * required information for assembling the volume. + */ bdev = blkdev_get_by_path(path, flags, holder); if (IS_ERR(bdev)) return ERR_CAST(bdev);