Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp13370385ybl; Sun, 29 Dec 2019 10:16:18 -0800 (PST) X-Google-Smtp-Source: APXvYqwNqeZjeQuP8Isd3HZ/jv7kBtn7hIZJ6oXHxGgN338zW+MBp7hNvu6KgX9W6q1WceTbDBJG X-Received: by 2002:a9d:1e4:: with SMTP id e91mr65533431ote.324.1577643378276; Sun, 29 Dec 2019 10:16:18 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1577643378; cv=none; d=google.com; s=arc-20160816; b=T+hRXqV1PzTL0+UGQP/6jgRk5kL8sqlSIZG5Sq9BNyhAe0L8C6yyCGDOR/1ZuAFymz rjfQcDLXf5tss5EXvwsZC6I8zvKe/wRHevWelUj9XckzHhRj3F39HxXc+x4Ljfq8xOWq 6uzUeoDo2HHn7HUHJn4h65XedPXa6JOIt1MW/9YS5NkkzUwhy6etTj7vEGbhIhWeYraP jvYnGYIFDBLsB5Pa/JFrz/kdwFcrbKYXyoe6E3PR306dCDtr40IA+hLerbptdsE5GZEv ihTLq6ocOcLYAjTPJiw+VvI43lvC7yWcRMBmfKDeQMZGMSqUTEdSdG9deT8VF3mDVA2h x98A== 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=TVrZDwXmrTlsqLARIX8D/l4p+ezuSa+fhZK9BB8LpLg=; b=x0RUIM9nNBGcoQzdHup81q5Zs7/su+EvmjH+kuUUGC1sD/svgfAnTQ/TWEBgXGmNFa /gQbe2589QsZ+76jiPRLsVQjPFjVqxtzLBPOSMCxtb1+WfB0m7aEo0sbVqI5TCmEqFXW y/YDDdLati11dlpDloMJpa2npVrd8LWK4LpmE3WTj2dQZrB3EJuJdAeTctkMkE2tabE+ Wl04iqHpjAUwvbMXASPuLUKmfunl2Ftn9+1WYBRZjzPH4S95QmqS7KFBDVlx056wXGOZ Jty21uP0+cbAKkFmh63T3eJLyyYA1XPilF3CETmHxWV91nwab/E8tpEBvj25tk8v1uxb jAxQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=TlnSVbJC; 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 y65si20889738oia.162.2019.12.29.10.16.06; Sun, 29 Dec 2019 10:16:18 -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=TlnSVbJC; 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 S2387881AbfL2SPD (ORCPT + 99 others); Sun, 29 Dec 2019 13:15:03 -0500 Received: from mail.kernel.org ([198.145.29.99]:46644 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728113AbfL2R0E (ORCPT ); Sun, 29 Dec 2019 12:26:04 -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 199F720409; Sun, 29 Dec 2019 17:26:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1577640363; bh=XktTSAd02PzNSs9Fr+ngFpJyUsg0zvuHqJMAgGXLKbU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=TlnSVbJCrlNaBCUIUsEgcJu6L/ubsHcjBPJb/6NDFLapLrl8RbP5k5xQjjvyUfqop pEstNE5KPb2a0RRKK2KDptGUL+NlXrC4hbc4PVoohygf3QfDFbbaJy1VcnOQx3/8Hv q2sJ9vGy7MK9s7fNmVlrK19Am1aFRz8WZ7YxQ/Ps= 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.14 079/161] loop: fix no-unmap write-zeroes request behavior Date: Sun, 29 Dec 2019 18:18:47 +0100 Message-Id: <20191229162424.024626860@linuxfoundation.org> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20191229162355.500086350@linuxfoundation.org> References: <20191229162355.500086350@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 ec61dd873c93..453e3728e657 100644 --- a/drivers/block/loop.c +++ b/drivers/block/loop.c @@ -414,18 +414,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; @@ -565,9 +567,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