Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp634018pxv; Thu, 15 Jul 2021 12:03:54 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx1KnRUGtbaZNtpoeQJB5OU0UYP2DUdmcDIUBj7GwmHol4IP5E2R4lf1hLKEGdcXJzu63vj X-Received: by 2002:a02:628b:: with SMTP id d133mr5309982jac.27.1626375834277; Thu, 15 Jul 2021 12:03:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626375834; cv=none; d=google.com; s=arc-20160816; b=lQGxZ4gNNdb3XXar8m8v5d/Kh21/oQz864Ki+t17PsUeSucITf2/za0SSS8KVf30l8 6tO7UqhXT3FK/rgYfveD4JozZkROljxQizs9uWFJ4jZmIW8HTdqNNbL/Oc3tSUVQD8+k e/vDAhZOsyoW8cbU4ODT5A0X9pYj4QFnDTZ1RByt5zW2BKU4Qn0bKfQZpsCrFfkTmEdt b6veARXtcfAOURs86Lj9wvRFbnZ3xbCxO3rIn88Esiw8Ub7BhY5HzUnlxaVWeebeCJQ3 AhTO9IbLN0cQkiaE3hI8mNxdFlvxy3Z9U+kC9bOVDFUKbvTeUFvbIqAWKEsViUAYnLpk dmwg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=jIMd9alWq46fqDxjRLiMAGEoUPEthzQ8PdiN7mcaTmg=; b=b3kz8NevwW2sWTU6aRSvg+zwLV/cOwH6gTjwCYx2pk1TDjdn2mp+eTr29GRIoOCF3O 2V088lAHf02o9yM3X7Vfx+6qm9lfNAA7/ZZL1E31HCQby19hfLSZJnxA1IqakZWK4C1v 6/5CuaiAuvcxh27XQtks4ItClDxjnylSOF9DtZf79UOvzSEbW/9ixwVqLlvWQoyuKNs8 P7/juLmvSBzk6dGoIqiE1NWi3ylGWgfWx/LSNjQ53XEC6NA9bNKZqturtBQeVxWgCl/2 3nJCcCcxTmBKM9PjfwvP52u2m2uNu7r6061EVvv37cdM8tkNhPiVS6sL1G/p7xd8Ni1p efiA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=2N6HPgzX; 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=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id z10si8133392ioq.37.2021.07.15.12.03.41; Thu, 15 Jul 2021 12:03:54 -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; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=2N6HPgzX; 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=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243462AbhGOTE0 (ORCPT + 99 others); Thu, 15 Jul 2021 15:04:26 -0400 Received: from mail.kernel.org ([198.145.29.99]:57144 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240256AbhGOSxt (ORCPT ); Thu, 15 Jul 2021 14:53:49 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 0E59C613ED; Thu, 15 Jul 2021 18:50:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1626375055; bh=uVE04i3VRKABrlOZWq05fkd587OP1AIH8sa9nrjJCBM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=2N6HPgzXULrEFGQahfmJjCe6XJFZLFM2H52VEIZn3XmykS1OceN/n4KMRFLoQQawW leRRkvyqk5brnvzypZy+JzSKFN7REwI7ioBkGMq96yhQHuVs+LF0EJuNI2LxdGjWy0 16Laxw7SOTo1ZPUXcmObW6+VTwa9yZxazvJ3e4YM= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Eric Desrochers , Mauricio Faria de Oliveira , Gabriel Krisman Bertazi , Ming Lei , Jens Axboe , Hanjun Guo Subject: [PATCH 5.10 139/215] loop: fix I/O error on fsync() in detached loop devices Date: Thu, 15 Jul 2021 20:38:31 +0200 Message-Id: <20210715182624.121950937@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210715182558.381078833@linuxfoundation.org> References: <20210715182558.381078833@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Signed-off-by: Greg Kroah-Hartman --- drivers/block/loop.c | 3 +++ 1 file changed, 3 insertions(+) --- a/drivers/block/loop.c +++ b/drivers/block/loop.c @@ -1224,6 +1224,9 @@ static int __loop_clr_fd(struct loop_dev 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);