From patchwork Tue Jul 7 08:45:51 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ming Lei X-Patchwork-Id: 11648055 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id B9B2F13B6 for ; Tue, 7 Jul 2020 08:46:16 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A04732064B for ; Tue, 7 Jul 2020 08:46:16 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="iZ0i6X1z" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726757AbgGGIqQ (ORCPT ); Tue, 7 Jul 2020 04:46:16 -0400 Received: from us-smtp-1.mimecast.com ([207.211.31.81]:36564 "EHLO us-smtp-delivery-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1725944AbgGGIqP (ORCPT ); Tue, 7 Jul 2020 04:46:15 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1594111573; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=YIUjGZlGq7r4VPMZlY5hjKjO/lR8b42Aa+UVkakrz6k=; b=iZ0i6X1zsCws52RBDy34W1DtpylHhCCBEeQJdp3t4NjyVXZtHRyoMqQPyuCdRweaAyvGVy xtXfBZnmX+Kt8oOTqUaid/sckqp15V3PybtaqEkNCd2ZCGoYkNrUOJd/NcTnETkn4HQD1H zeaqoYVgq2h64+Ng22UYSTfiuR8ropg= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-265-9B1yxLeGOgq4Zt70GPS9kA-1; Tue, 07 Jul 2020 04:46:11 -0400 X-MC-Unique: 9B1yxLeGOgq4Zt70GPS9kA-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id D89D3107ACF2; Tue, 7 Jul 2020 08:46:10 +0000 (UTC) Received: from localhost (ovpn-12-188.pek2.redhat.com [10.72.12.188]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2C0E35D9F3; Tue, 7 Jul 2020 08:46:06 +0000 (UTC) From: Ming Lei To: Jens Axboe , Christoph Hellwig Cc: linux-block@vger.kernel.org, Ming Lei Subject: [PATCH 1/2] block: loop: share code of reread partitions Date: Tue, 7 Jul 2020 16:45:51 +0800 Message-Id: <20200707084552.3294693-2-ming.lei@redhat.com> In-Reply-To: <20200707084552.3294693-1-ming.lei@redhat.com> References: <20200707084552.3294693-1-ming.lei@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Sender: linux-block-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org loop_reread_partitions() has been there for rereading partitions, so replace the open code in __loop_clr_fd() with loop_reread_partitions() by passing 'locked' parameter. Signed-off-by: Ming Lei --- drivers/block/loop.c | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/drivers/block/loop.c b/drivers/block/loop.c index a943207705dd..0e08468b9ce0 100644 --- a/drivers/block/loop.c +++ b/drivers/block/loop.c @@ -650,13 +650,17 @@ static inline void loop_update_dio(struct loop_device *lo) } static void loop_reread_partitions(struct loop_device *lo, - struct block_device *bdev) + struct block_device *bdev, bool locked) { int rc; - mutex_lock(&bdev->bd_mutex); - rc = bdev_disk_changed(bdev, false); - mutex_unlock(&bdev->bd_mutex); + if (locked) { + rc = bdev_disk_changed(bdev, false); + } else { + mutex_lock(&bdev->bd_mutex); + rc = bdev_disk_changed(bdev, false); + mutex_unlock(&bdev->bd_mutex); + } if (rc) pr_warn("%s: partition scan of loop%d (%s) failed (rc=%d)\n", __func__, lo->lo_number, lo->lo_file_name, rc); @@ -754,7 +758,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev, */ fput(old_file); if (partscan) - loop_reread_partitions(lo, bdev); + loop_reread_partitions(lo, bdev, false); return 0; out_err: @@ -1179,7 +1183,7 @@ static int loop_configure(struct loop_device *lo, fmode_t mode, bdgrab(bdev); mutex_unlock(&loop_ctl_mutex); if (partscan) - loop_reread_partitions(lo, bdev); + loop_reread_partitions(lo, bdev, false); if (claimed_bdev) bd_abort_claiming(bdev, claimed_bdev, loop_configure); return 0; @@ -1270,16 +1274,7 @@ static int __loop_clr_fd(struct loop_device *lo, bool release) * must be at least one and it can only become zero when the * current holder is released. */ - if (!release) - mutex_lock(&bdev->bd_mutex); - err = bdev_disk_changed(bdev, false); - if (!release) - mutex_unlock(&bdev->bd_mutex); - if (err) - pr_warn("%s: partition scan of loop%d failed (rc=%d)\n", - __func__, lo_number, err); - /* Device is gone, no point in returning error */ - err = 0; + loop_reread_partitions(lo, bdev, release); } /* @@ -1420,7 +1415,7 @@ loop_set_status(struct loop_device *lo, const struct loop_info64 *info) out_unlock: mutex_unlock(&loop_ctl_mutex); if (partscan) - loop_reread_partitions(lo, bdev); + loop_reread_partitions(lo, bdev, false); return err; } From patchwork Tue Jul 7 08:45:52 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ming Lei X-Patchwork-Id: 11648057 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 455CB6C1 for ; Tue, 7 Jul 2020 08:46:21 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2D71D206F6 for ; Tue, 7 Jul 2020 08:46:21 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="NmgPDPaY" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726817AbgGGIqU (ORCPT ); Tue, 7 Jul 2020 04:46:20 -0400 Received: from us-smtp-delivery-1.mimecast.com ([207.211.31.120]:41937 "EHLO us-smtp-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1725944AbgGGIqU (ORCPT ); Tue, 7 Jul 2020 04:46:20 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1594111579; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=X3/5qKEEIHenwqx1XIbYlB5tYhqdYlS0V/aQaqfa+0k=; b=NmgPDPaYPWbAfkJgGRKqh2npI8S3mOcTkPRRgKnXmn+vvwcSdYoj3E3MsR2TrKUgfCpF+4 VO9z1YuAYiaCAWY7HS55LuKrv3NxPdZL/g2nWVf/EE+n/rr0nSws0z2QJfYUutigkHdLmN yWoM/2VaC8ix7x0ucAeS6fqoJTGf78A= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-207-9c5N46fbP_Oxq5FHfheEUA-1; Tue, 07 Jul 2020 04:46:15 -0400 X-MC-Unique: 9c5N46fbP_Oxq5FHfheEUA-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 453BC461; Tue, 7 Jul 2020 08:46:14 +0000 (UTC) Received: from localhost (ovpn-12-188.pek2.redhat.com [10.72.12.188]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9B1C160BF3; Tue, 7 Jul 2020 08:46:13 +0000 (UTC) From: Ming Lei To: Jens Axboe , Christoph Hellwig Cc: linux-block@vger.kernel.org, Ming Lei Subject: [PATCH 2/2] block: loop: delete partitions after clearing & changing fd Date: Tue, 7 Jul 2020 16:45:52 +0800 Message-Id: <20200707084552.3294693-3-ming.lei@redhat.com> In-Reply-To: <20200707084552.3294693-1-ming.lei@redhat.com> References: <20200707084552.3294693-1-ming.lei@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 Sender: linux-block-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org After clearing fd or changing fd, we have to delete old partitions, otherwise they may become ghost partitions. Fix this issue by clearing GENHD_FL_NO_PART_SCAN during calling bdev_disk_changed() which won't drop old partitions if GENHD_FL_NO_PART_SCAN isn't set. Signed-off-by: Ming Lei Signed-off-by: Ming Lei --- drivers/block/loop.c | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/drivers/block/loop.c b/drivers/block/loop.c index 0e08468b9ce0..cf71a1bbcd45 100644 --- a/drivers/block/loop.c +++ b/drivers/block/loop.c @@ -650,17 +650,26 @@ static inline void loop_update_dio(struct loop_device *lo) } static void loop_reread_partitions(struct loop_device *lo, - struct block_device *bdev, bool locked) + struct block_device *bdev, bool locked, + bool force_scan) { int rc; + bool no_scan; - if (locked) { - rc = bdev_disk_changed(bdev, false); - } else { + if (!locked) mutex_lock(&bdev->bd_mutex); - rc = bdev_disk_changed(bdev, false); + + no_scan = lo->lo_disk->flags & GENHD_FL_NO_PART_SCAN; + if (force_scan && no_scan) + lo->lo_disk->flags &= ~GENHD_FL_NO_PART_SCAN; + + rc = bdev_disk_changed(bdev, false); + + if (force_scan && no_scan) + lo->lo_disk->flags |= GENHD_FL_NO_PART_SCAN; + + if (!locked) mutex_unlock(&bdev->bd_mutex); - } if (rc) pr_warn("%s: partition scan of loop%d (%s) failed (rc=%d)\n", __func__, lo->lo_number, lo->lo_file_name, rc); @@ -758,7 +767,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev, */ fput(old_file); if (partscan) - loop_reread_partitions(lo, bdev, false); + loop_reread_partitions(lo, bdev, false, true); return 0; out_err: @@ -1183,7 +1192,7 @@ static int loop_configure(struct loop_device *lo, fmode_t mode, bdgrab(bdev); mutex_unlock(&loop_ctl_mutex); if (partscan) - loop_reread_partitions(lo, bdev, false); + loop_reread_partitions(lo, bdev, false, false); if (claimed_bdev) bd_abort_claiming(bdev, claimed_bdev, loop_configure); return 0; @@ -1274,7 +1283,7 @@ static int __loop_clr_fd(struct loop_device *lo, bool release) * must be at least one and it can only become zero when the * current holder is released. */ - loop_reread_partitions(lo, bdev, release); + loop_reread_partitions(lo, bdev, release, true); } /* @@ -1415,7 +1424,7 @@ loop_set_status(struct loop_device *lo, const struct loop_info64 *info) out_unlock: mutex_unlock(&loop_ctl_mutex); if (partscan) - loop_reread_partitions(lo, bdev, false); + loop_reread_partitions(lo, bdev, false, false); return err; }