From patchwork Thu Jan 4 10:39:24 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ming Lei X-Patchwork-Id: 10144413 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 B80546034B for ; Thu, 4 Jan 2018 10:39:41 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9445428332 for ; Thu, 4 Jan 2018 10:39:41 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 885542833E; Thu, 4 Jan 2018 10:39:41 +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=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI 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 1FA7A28332 for ; Thu, 4 Jan 2018 10:39:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752609AbeADKjk (ORCPT ); Thu, 4 Jan 2018 05:39:40 -0500 Received: from mx1.redhat.com ([209.132.183.28]:45696 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752595AbeADKjj (ORCPT ); Thu, 4 Jan 2018 05:39:39 -0500 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 mx1.redhat.com (Postfix) with ESMTPS id C0B191662; Thu, 4 Jan 2018 10:39:39 +0000 (UTC) Received: from localhost (ovpn-12-33.pek2.redhat.com [10.72.12.33]) by smtp.corp.redhat.com (Postfix) with ESMTP id 84D255DA5B; Thu, 4 Jan 2018 10:39:30 +0000 (UTC) From: Ming Lei To: Jens Axboe , linux-block@vger.kernel.org Cc: reddot.rocks@gmail.com, Christoph Hellwig , Ming Lei , linux-fsdevel@vger.kernel.org Subject: [PATCH] block: loop: remove & invalidate partitions when detaching Date: Thu, 4 Jan 2018 18:39:24 +0800 Message-Id: <20180104103924.8814-1-ming.lei@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.29]); Thu, 04 Jan 2018 10:39:39 +0000 (UTC) Sender: linux-block-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP When detaching loop disk, neither we remove loop partitions, nor invalidate them. This way may cause data loss, and often confuse people. This patch fixes the above issue by removing & invalidating loop partitions in loop_clr_fd(), Reported-by: reddot.rocks@gmail.com Cc: linux-fsdevel@vger.kernel.org Signed-off-by: Ming Lei --- block/genhd.c | 15 +++++++++++---- drivers/block/loop.c | 4 ++-- include/linux/genhd.h | 1 + 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/block/genhd.c b/block/genhd.c index 96a66f671720..24c66e940c0c 100644 --- a/block/genhd.c +++ b/block/genhd.c @@ -695,14 +695,11 @@ void device_add_disk(struct device *parent, struct gendisk *disk) } EXPORT_SYMBOL(device_add_disk); -void del_gendisk(struct gendisk *disk) +void del_gendisk_partitions(struct gendisk *disk) { struct disk_part_iter piter; struct hd_struct *part; - blk_integrity_del(disk); - disk_del_events(disk); - /* invalidate stuff */ disk_part_iter_init(&piter, disk, DISK_PITER_INCL_EMPTY | DISK_PITER_REVERSE); @@ -714,7 +711,17 @@ void del_gendisk(struct gendisk *disk) disk_part_iter_exit(&piter); invalidate_partition(disk, 0); +} +EXPORT_SYMBOL(del_gendisk_partitions); + +void del_gendisk(struct gendisk *disk) +{ + blk_integrity_del(disk); + disk_del_events(disk); + + del_gendisk_partitions(disk); bdev_unhash_inode(disk_devt(disk)); + set_capacity(disk, 0); disk->flags &= ~GENHD_FL_UP; diff --git a/drivers/block/loop.c b/drivers/block/loop.c index bc8e61506968..a84c7befcc94 100644 --- a/drivers/block/loop.c +++ b/drivers/block/loop.c @@ -1021,6 +1021,8 @@ static int loop_clr_fd(struct loop_device *lo) if (filp == NULL) return -EINVAL; + del_gendisk_partitions(lo->lo_disk); + /* freeze request queue during the transition */ blk_mq_freeze_queue(lo->lo_queue); @@ -1060,8 +1062,6 @@ static int loop_clr_fd(struct loop_device *lo) module_put(THIS_MODULE); blk_mq_unfreeze_queue(lo->lo_queue); - if (lo->lo_flags & LO_FLAGS_PARTSCAN && bdev) - loop_reread_partitions(lo, bdev); lo->lo_flags = 0; if (!part_shift) lo->lo_disk->flags |= GENHD_FL_NO_PART_SCAN; diff --git a/include/linux/genhd.h b/include/linux/genhd.h index 5144ebe046c9..ae41535e705c 100644 --- a/include/linux/genhd.h +++ b/include/linux/genhd.h @@ -397,6 +397,7 @@ static inline void add_disk(struct gendisk *disk) } extern void del_gendisk(struct gendisk *gp); +extern void del_gendisk_partitions(struct gendisk *gp); extern struct gendisk *get_gendisk(dev_t dev, int *partno); extern struct block_device *bdget_disk(struct gendisk *disk, int partno);