From patchwork Wed May 30 06:58:20 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anand Jain X-Patchwork-Id: 10437713 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id D17BF602BF for ; Wed, 30 May 2018 06:58:01 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C1BC728752 for ; Wed, 30 May 2018 06:58:01 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B69C228869; Wed, 30 May 2018 06:58:01 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI, T_DKIM_INVALID, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6FB0D28870 for ; Wed, 30 May 2018 06:58:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S968632AbeE3G56 (ORCPT ); Wed, 30 May 2018 02:57:58 -0400 Received: from userp2130.oracle.com ([156.151.31.86]:48152 "EHLO userp2130.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S937114AbeE3Gzx (ORCPT ); Wed, 30 May 2018 02:55:53 -0400 Received: from pps.filterd (userp2130.oracle.com [127.0.0.1]) by userp2130.oracle.com (8.16.0.22/8.16.0.22) with SMTP id w4U6ovVv031695 for ; Wed, 30 May 2018 06:55:52 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : subject : date : message-id : in-reply-to : references; s=corp-2017-10-26; bh=3iOQJCaEbEFWvcAO5f6LfOT2HhGjVeb4+f67isJzc2A=; b=wQ0HpPOyIbgXm7mvT6hx7rwD4MIzVhqK6TDydJFxsRMGmWC7VkxMUPqH54yjExUuSQq5 6os8E0E4fVmTpP1ZHN68jsdMl83YQziZmb8UXm1IJpb4ojAQVZ1yB5+ONy5YpRogvvEE bY34F/N6mc7aGPgYZ3Ju+FEV7gRsSGxlqQfV/TLlGtGDLRZ16IeVNOvldGsqVpJN7Gkc 7SGnW9wBcTMjo94DA4PwBSq/SsRO/NOzcUI/IC9cY3c+H1Utc/P8rcUatLXZcNCLp4rQ r8CNQeE8kAkTxmHeiY24HRQXk3eGQUKPvm5gEUQGPSnPT9p6xae/2kLtDu4QD6Im0pGi Rw== Received: from userv0022.oracle.com (userv0022.oracle.com [156.151.31.74]) by userp2130.oracle.com with ESMTP id 2j9ev890fd-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Wed, 30 May 2018 06:55:52 +0000 Received: from aserv0122.oracle.com (aserv0122.oracle.com [141.146.126.236]) by userv0022.oracle.com (8.14.4/8.14.4) with ESMTP id w4U6tq5E004279 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Wed, 30 May 2018 06:55:52 GMT Received: from abhmp0007.oracle.com (abhmp0007.oracle.com [141.146.116.13]) by aserv0122.oracle.com (8.14.4/8.14.4) with ESMTP id w4U6tpVE024089 for ; Wed, 30 May 2018 06:55:51 GMT Received: from tp.sg.oracle.com (/10.186.49.52) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Tue, 29 May 2018 23:55:51 -0700 From: Anand Jain To: linux-btrfs@vger.kernel.org Subject: [PATCH 5/6] btrfs: fix btrfs_free_stale_devices() with needed locks Date: Wed, 30 May 2018 14:58:20 +0800 Message-Id: <20180530065821.28385-6-anand.jain@oracle.com> X-Mailer: git-send-email 2.15.0 In-Reply-To: <20180530065821.28385-1-anand.jain@oracle.com> References: <20180530065821.28385-1-anand.jain@oracle.com> X-Proofpoint-Virus-Version: vendor=nai engine=5900 definitions=8908 signatures=668702 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=3 malwarescore=0 phishscore=0 bulkscore=0 spamscore=0 mlxscore=0 mlxlogscore=887 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1805220000 definitions=main-1805300079 Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP btrfs_free_stale_devices() finds the stale (not opened) matching device path in the fs_uuid list. We are already under uuid_mutex so when we check for each fs_devices, hold the respective device_list_mutex. Signed-off-by: Anand Jain --- fs/btrfs/volumes.c | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c index fd656eb75225..a6a23b0185e5 100644 --- a/fs/btrfs/volumes.c +++ b/fs/btrfs/volumes.c @@ -643,8 +643,11 @@ static void btrfs_free_stale_devices(const char *path, list_for_each_entry_safe(fs_devices, tmp_fs_devices, &fs_uuids, fs_list) { - if (fs_devices->opened) + mutex_lock(&fs_devices->device_list_mutex); + if (fs_devices->opened) { + mutex_unlock(&fs_devices->device_list_mutex); continue; + } list_for_each_entry_safe(device, tmp_device, &fs_devices->devices, dev_list) { @@ -664,16 +667,18 @@ static void btrfs_free_stale_devices(const char *path, continue; /* delete the stale device */ - if (fs_devices->num_devices == 1) { - btrfs_sysfs_remove_fsid(fs_devices); - list_del(&fs_devices->fs_list); - free_fs_devices(fs_devices); + fs_devices->num_devices--; + list_del(&device->dev_list); + btrfs_free_device(device); + + if (fs_devices->num_devices == 0) break; - } else { - fs_devices->num_devices--; - list_del(&device->dev_list); - btrfs_free_device(device); - } + } + mutex_unlock(&fs_devices->device_list_mutex); + if (fs_devices->num_devices == 0) { + btrfs_sysfs_remove_fsid(fs_devices); + list_del(&fs_devices->fs_list); + free_fs_devices(fs_devices); } } }