Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp3736281pxv; Tue, 13 Jul 2021 02:30:37 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw85RMWT4IEOFI5iZMzgDuRmjdXj72TcoD9w0IYQOBFUa1nJt01U2wB/+4zHoDiFSENKOmZ X-Received: by 2002:a6b:185:: with SMTP id 127mr2465381iob.64.1626168636789; Tue, 13 Jul 2021 02:30:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626168636; cv=none; d=google.com; s=arc-20160816; b=Mv4om3oDky4mur3H6NYbZfWJ9COPq/DOmudIscNmk0lvcGSXHbB+HpeFTFajxyT5/m it0gpdRSwu2S9kZT+REWBC0F2mAPAItYEaAfWGK3C4np1ATkZxIK9b3nQhdoVyND7ryC BiL2Lg8KzghKVsrxAG18ITEcD7Qw4rehHS3867KCOvmJ1ePXDA37fGrZEDbA536uleub SVwDTiWUhw3vYKLZK7bjipBnIxuuqgetj75tZEhMVjEtUdaifZLk9+JKcdT9C2GoyT2i drMM/8zi2j2VhDtxbxREglf9zmEJC+6sRfhvQITKyqzuJrHtP0I78d1ouDLMFeppsUIY LmjQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:references:in-reply-to:message-id :date:subject:cc:to:from; bh=CXUelzwlZB0qC76AAGwGtOJ7VITFeekvCMkZXWQ4Bes=; b=RaH22FrIOJIfno7p1S6pM7FxtgwLbBLgJBoqRv4DYnH0T2YQNap1K6wagk0hiGU4Rt NPEF1l5cLiPvisZaHUeyMs5ORBcyRTv08WnJYm0S2sLDF9iZPzPL6JvJ/tHHUE0iiezv Tlw+UdXc+KXCaVC9wVB5m2cJ21IwR4I/iQCy9u8VCmg0StKFnvS0vWxXzW88hL24DgC/ 1lOH4PMJW09e9Eh7UkRtJ8q35OF5foKykJfSg4z+7IXyekkFGTVH3Duh03E8sL7slpMI WW9LCx2CWUxxCJ0EaZScdShqa8gOyjg/JOMnTLubMSViyqWuC7Lsjsn+ezhT2atcOX9H LC/w== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=huawei.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id l17si6481381ilk.49.2021.07.13.02.30.21; Tue, 13 Jul 2021 02:30:36 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235041AbhGMJcm (ORCPT + 99 others); Tue, 13 Jul 2021 05:32:42 -0400 Received: from szxga02-in.huawei.com ([45.249.212.188]:10475 "EHLO szxga02-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235078AbhGMJcl (ORCPT ); Tue, 13 Jul 2021 05:32:41 -0400 Received: from dggemv711-chm.china.huawei.com (unknown [172.30.72.57]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4GPFd60VZ2zccyp; Tue, 13 Jul 2021 17:26:34 +0800 (CST) Received: from dggpemm500002.china.huawei.com (7.185.36.229) by dggemv711-chm.china.huawei.com (10.1.198.66) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2176.2; Tue, 13 Jul 2021 17:29:50 +0800 Received: from linux-ibm.site (10.175.102.37) by dggpemm500002.china.huawei.com (7.185.36.229) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2176.2; Tue, 13 Jul 2021 17:29:49 +0800 From: Hanjun Guo To: CC: , Greg Kroah-Hartman , Sasha Levin , "Mauricio Faria de Oliveira" , Eric Desrochers , Jens Axboe , Hanjun Guo Subject: [Backport for 5.10.y PATCH 1/7] loop: fix I/O error on fsync() in detached loop devices Date: Tue, 13 Jul 2021 17:18:31 +0800 Message-ID: <1626167917-11972-2-git-send-email-guohanjun@huawei.com> X-Mailer: git-send-email 1.7.12.4 In-Reply-To: <1626167917-11972-1-git-send-email-guohanjun@huawei.com> References: <1626167917-11972-1-git-send-email-guohanjun@huawei.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.175.102.37] X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To dggpemm500002.china.huawei.com (7.185.36.229) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Mauricio Faria de Oliveira commit 4ceddce55eb35d15b0f87f5dcf6f0058fd15d3a4 upstream. There's an I/O error on fsync() in a detached loop device if it has been previously attached. The issue is write cache is enabled in the attach path in loop_configure() but it isn't disabled in the detach path; thus it remains enabled in the block device regardless of whether it is attached or not. Now fsync() can get an I/O request that will just be failed later in loop_queue_rq() as device's state is not 'Lo_bound'. So, disable write cache in the detach path. Do so based on the queue flag, not the loop device flag for read-only (used to enable) as the queue flag can be changed via sysfs even on read-only loop devices (e.g., losetup -r.) Test-case: # DEV=/dev/loop7 # IMG=/tmp/image # truncate --size 1M $IMG # losetup $DEV $IMG # losetup -d $DEV Before: # strace -e fsync parted -s $DEV print 2>&1 | grep fsync fsync(3) = -1 EIO (Input/output error) Warning: Error fsyncing/closing /dev/loop7: Input/output error [ 982.529929] blk_update_request: I/O error, dev loop7, sector 0 op 0x1:(WRITE) flags 0x800 phys_seg 0 prio class 0 After: # strace -e fsync parted -s $DEV print 2>&1 | grep fsync fsync(3) = 0 Co-developed-by: Eric Desrochers Signed-off-by: Eric Desrochers Signed-off-by: Mauricio Faria de Oliveira Tested-by: Gabriel Krisman Bertazi Reviewed-by: Ming Lei Signed-off-by: Jens Axboe Signed-off-by: Hanjun Guo --- drivers/block/loop.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/drivers/block/loop.c b/drivers/block/loop.c index 06d44ae..f0fa0c8 100644 --- a/drivers/block/loop.c +++ b/drivers/block/loop.c @@ -1224,6 +1224,9 @@ static int __loop_clr_fd(struct loop_device *lo, bool release) goto out_unlock; } + if (test_bit(QUEUE_FLAG_WC, &lo->lo_queue->queue_flags)) + blk_queue_write_cache(lo->lo_queue, false, false); + /* freeze request queue during the transition */ blk_mq_freeze_queue(lo->lo_queue); -- 1.7.12.4