Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp13336246ybl; Sun, 29 Dec 2019 09:34:12 -0800 (PST) X-Google-Smtp-Source: APXvYqwfi6RwnD2RiR9/iBXwSh6H/5u4NMO1TdYISZJx/LyASwgAwN+ZGbgG8KA0pjJJB3UBWB3l X-Received: by 2002:a9d:4f18:: with SMTP id d24mr65834701otl.179.1577640852704; Sun, 29 Dec 2019 09:34:12 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1577640852; cv=none; d=google.com; s=arc-20160816; b=W4Mxtx6/3YyUuSzKJIDmayr6/MkiumcdjzRR53RtGDOPJSXH9qS8BI2EJb18vkt8cH o6wRGZUchxCGxtWCXgesQHKnhwq7gvHAML9ul33FrcGBuN7FYMxGysTZHlR0O8ymbZpn KubZPRAkp6Zr3e1QiWelnKZ/hWPKjYSXDYIZL+EVJ6Oo6e1RaJQXxtjoHeQp/EyrKXR2 +24KeVhPNAbUKupgMu8Fij+/FV4dIqoGcjgQJdGJXRbLUFN/CKDbJcYN/vWCiNNB3Olm 9nd6Q9rYbEC2SFMj7fv2ozJr5x/w3hpJLICjZzFA3z3ojB7rir96QHG2jT+fwFSPZ+0Q FndQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=/Dtq04IPkw5+u0Rvp7eVNnb+S3hxQmkpgg6LcSCW7Zc=; b=vfaZlpu1cbQERQW9XtGxOQ2ivtymF0gdz9hnwG36BnaK1EzNHQe7ux985RfRFcCRWs XLfxts7Sgu9uHShKdI+UcTMr0cwRCGLxgurJuZV/3oQXK8slChn4A81xRz7eNqgDkVdB y3tXkVeJiwzG2tdq7Dj6Wb8+gmFzckpLKnM4Gyr7nAo488wKoRTQPwbI9Wr61wdsAdH8 1oP4+2AxZJgo9E+DotiHtNmQL3o4usRaFs9pfBsUO0kTzHDM3FRse59sUnW1T+WFK2+z xRYCLKejyapCvhb4Ty2W0A3qqAsNava0itS69dtNRueTPbs7sy2l09++cpJLnnEQOazZ AXZQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=KDut789Z; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f25si11651024oti.244.2019.12.29.09.34.00; Sun, 29 Dec 2019 09:34:12 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=KDut789Z; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729237AbfL2Rb5 (ORCPT + 99 others); Sun, 29 Dec 2019 12:31:57 -0500 Received: from mail.kernel.org ([198.145.29.99]:59776 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728854AbfL2Rbz (ORCPT ); Sun, 29 Dec 2019 12:31:55 -0500 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 66A6C20722; Sun, 29 Dec 2019 17:31:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1577640713; bh=DvxDSge1NwyEUQ8XLZt3RYThuAPqYkl4cBNFYm9y9rU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=KDut789Zxce2U9zzU547s4guva2l159j2mCcnyF1LhqDxDOgzbJhQ1+5n9yE93HNG lStFU1MJfbLCGHUidBeEKnX1Y5KW9/bv5RcgKj6tOBXSNwcdvUG6ZtPNu95oW2bcEg 0/TlrYX4YbY/PfUhVx1JMTbRkhbL1SrVNm/T46jg= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, "Darrick J. Wong" , Christoph Hellwig , Jens Axboe , Sasha Levin Subject: [PATCH 4.19 108/219] loop: fix no-unmap write-zeroes request behavior Date: Sun, 29 Dec 2019 18:18:30 +0100 Message-Id: <20191229162525.032149668@linuxfoundation.org> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20191229162508.458551679@linuxfoundation.org> References: <20191229162508.458551679@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Darrick J. Wong [ Upstream commit efcfec579f6139528c9e6925eca2bc4a36da65c6 ] Currently, if the loop device receives a WRITE_ZEROES request, it asks the underlying filesystem to punch out the range. This behavior is correct if unmapping is allowed. However, a NOUNMAP request means that the caller doesn't want us to free the storage backing the range, so punching out the range is incorrect behavior. To satisfy a NOUNMAP | WRITE_ZEROES request, loop should ask the underlying filesystem to FALLOC_FL_ZERO_RANGE, which is (according to the fallocate documentation) required to ensure that the entire range is backed by real storage, which suffices for our purposes. Fixes: 19372e2769179dd ("loop: implement REQ_OP_WRITE_ZEROES") Signed-off-by: Darrick J. Wong Reviewed-by: Christoph Hellwig Signed-off-by: Jens Axboe Signed-off-by: Sasha Levin --- drivers/block/loop.c | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/drivers/block/loop.c b/drivers/block/loop.c index 126c2c514673..9cd231a27328 100644 --- a/drivers/block/loop.c +++ b/drivers/block/loop.c @@ -416,18 +416,20 @@ out_free_page: return ret; } -static int lo_discard(struct loop_device *lo, struct request *rq, loff_t pos) +static int lo_fallocate(struct loop_device *lo, struct request *rq, loff_t pos, + int mode) { /* - * We use punch hole to reclaim the free space used by the - * image a.k.a. discard. However we do not support discard if - * encryption is enabled, because it may give an attacker - * useful information. + * We use fallocate to manipulate the space mappings used by the image + * a.k.a. discard/zerorange. However we do not support this if + * encryption is enabled, because it may give an attacker useful + * information. */ struct file *file = lo->lo_backing_file; - int mode = FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE; int ret; + mode |= FALLOC_FL_KEEP_SIZE; + if ((!file->f_op->fallocate) || lo->lo_encrypt_key_size) { ret = -EOPNOTSUPP; goto out; @@ -596,9 +598,17 @@ static int do_req_filebacked(struct loop_device *lo, struct request *rq) switch (req_op(rq)) { case REQ_OP_FLUSH: return lo_req_flush(lo, rq); - case REQ_OP_DISCARD: case REQ_OP_WRITE_ZEROES: - return lo_discard(lo, rq, pos); + /* + * If the caller doesn't want deallocation, call zeroout to + * write zeroes the range. Otherwise, punch them out. + */ + return lo_fallocate(lo, rq, pos, + (rq->cmd_flags & REQ_NOUNMAP) ? + FALLOC_FL_ZERO_RANGE : + FALLOC_FL_PUNCH_HOLE); + case REQ_OP_DISCARD: + return lo_fallocate(lo, rq, pos, FALLOC_FL_PUNCH_HOLE); case REQ_OP_WRITE: if (lo->transfer) return lo_write_transfer(lo, rq, pos); -- 2.20.1