Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp7810057yba; Thu, 2 May 2019 17:07:41 -0700 (PDT) X-Google-Smtp-Source: APXvYqwDWVPw4BczAV9aPkydegZpwd+a2DmVKXk/SoV9P8TEz886/K41cC/aFLP4ZFvBGBqC6gMo X-Received: by 2002:a17:902:a582:: with SMTP id az2mr6835788plb.315.1556842061301; Thu, 02 May 2019 17:07:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1556842061; cv=none; d=google.com; s=arc-20160816; b=fJKXfx3ds4lI7DmqyIunDVrySowbfVZwJAfU7/8vDw6BuQODzZpE5sYI2ujqcPPAKc e3y3ewbvmPTKlP+E1fq4TElFMDPylQ3dhN89sW0SViOcdRN2WM7ARreMHMRruNs6sSwB iQYeO4YoKq/BATvD7jWEweua/xUS2yQJ5qkCCbP5nCaM/er15J1oTCXHejnkJ6fEyE/Z EIfRcvfZnvtx5RmOWzw7FIF70e1SBoD2YzB4wZSjK8RRmzWx85Ul+xUTcSxCtVyGNu6d xHnFDAtLCt/glYSsokPHsxWGkrR+A15bF8FoURWfl0F/HY6r0INC3YCjUUIkEyslfkr1 XL/g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=e7XiQW4yO5S75GOxtPW7qYJgx3vLILexPigP9cVq26Q=; b=V6Z19XcYJLPPkX3SBr34mUpEHuhOkC2zqk9UMbdacjvkCG75ygrYxkYRBkP/igTcTv 5acm4j8PO4g7h56daY7CcpOD8Zhz3N4wBACGArsdQ9kTuyHOKw1qSF0TnpLeL3fk72R1 W03uC/QK7tSd9kTnxATBsetnImpEstHIeUexeMnZGek7aqOwiNw+1xpSMfEbVDrCCcq4 dqRSAEEWKDtYByPlF4IdWX4sni0MZLaMxiwZXsCQiuRgLlq9x7wxRzq5K4CwKUobaOT+ mzJ05yQ2kHxjp492SKu2E4ghZax5+E3r9b21LUxhcQqk4Hi5siVYrZJyR2/J8brLsLZu CGkw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=kqp3rIPL; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id gn22si580294plb.263.2019.05.02.17.07.11; Thu, 02 May 2019 17:07:41 -0700 (PDT) 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=@chromium.org header.s=google header.b=kqp3rIPL; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726267AbfECAGT (ORCPT + 99 others); Thu, 2 May 2019 20:06:19 -0400 Received: from mail-lf1-f68.google.com ([209.85.167.68]:37206 "EHLO mail-lf1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726114AbfECAGS (ORCPT ); Thu, 2 May 2019 20:06:18 -0400 Received: by mail-lf1-f68.google.com with SMTP id h126so3188508lfh.4 for ; Thu, 02 May 2019 17:06:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=e7XiQW4yO5S75GOxtPW7qYJgx3vLILexPigP9cVq26Q=; b=kqp3rIPLnKkXsxqKe8m6UOPqXNL1ZiPyj+ow8LjF0BZ0NvuqIKemPw6OChxYP1qO0z vLuG0K8maM37uqvletWnDWnwzEIvgy8We4yqEb28xN4VJr8bSTS1i5jtnnqOaVXkeomF 5bqT+7bF9PofJjZaPhysDZ++RkRfKxlK12obI= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=e7XiQW4yO5S75GOxtPW7qYJgx3vLILexPigP9cVq26Q=; b=OfOqwu70I2jEaVzDq9e966w7F/KC9+6ScOIB3yxHdWIJfb+CdI9qgMQBsulYDIFHoq BDIRPcEOtlALo7HzaU7UtIyhGpb1ODyFiqxlMf8xhmRMmXZbMdiR/nnq/g35kl8GmZXg HdPLiKcs2pX/dGgd/UOB6gW7VttR7rGoiYkQRExHiBEOqUIXDfvQLMczAK0LJdTYw+P5 fh8dJsZWgdq7wprtEQdXy4XBM9BbBz0GLqLB01NjNBnXOIAPhi0IqEFeQE8gKduXLAXw GXLukD/o4QzOENC6508pd5ptDCoIdT/5A8lIFp91itgzYKI5ia8DV+guUjTFipvPJbzR pKug== X-Gm-Message-State: APjAAAXSw3xPA/WXZkJybEtc2Bu/FNazMgjL0Tp3lOKrAW2nEwvEIApP hnXzH17hbFA0Tv6LtqExF2/8Ugj8mno= X-Received: by 2002:ac2:4357:: with SMTP id o23mr3711684lfl.146.1556841975887; Thu, 02 May 2019 17:06:15 -0700 (PDT) Received: from mail-lj1-f169.google.com (mail-lj1-f169.google.com. [209.85.208.169]) by smtp.gmail.com with ESMTPSA id n20sm77385lji.53.2019.05.02.17.06.13 for (version=TLS1_3 cipher=AEAD-AES128-GCM-SHA256 bits=128/128); Thu, 02 May 2019 17:06:15 -0700 (PDT) Received: by mail-lj1-f169.google.com with SMTP id f23so3837031ljc.0 for ; Thu, 02 May 2019 17:06:13 -0700 (PDT) X-Received: by 2002:a2e:8ecd:: with SMTP id e13mr3509475ljl.30.1556841973316; Thu, 02 May 2019 17:06:13 -0700 (PDT) MIME-Version: 1.0 References: <20190502174409.74623-1-evgreen@chromium.org> <20190502174409.74623-3-evgreen@chromium.org> In-Reply-To: <20190502174409.74623-3-evgreen@chromium.org> From: Evan Green Date: Thu, 2 May 2019 17:05:37 -0700 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH v4 2/2] loop: Better discard support for block devices To: Jens Axboe , Martin K Petersen Cc: Bart Van Assche , Gwendal Grignou , Alexis Savery , Ming Lei , linux-block , LKML Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, May 2, 2019 at 10:44 AM Evan Green wrote: > > If the backing device for a loop device is a block device, > then mirror the "write zeroes" capabilities of the underlying > block device into the loop device. Copy this capability into both > max_write_zeroes_sectors and max_discard_sectors of the loop device. > > The reason for this is that REQ_OP_DISCARD on a loop device translates > into blkdev_issue_zeroout(), rather than blkdev_issue_discard(). This > presents a consistent interface for loop devices (that discarded data > is zeroed), regardless of the backing device type of the loop device. > There should be no behavior change for loop devices backed by regular > files. > > While in there, differentiate between REQ_OP_DISCARD and > REQ_OP_WRITE_ZEROES, which are different for block devices, > but which the loop device had just been lumping together, since > they're largely the same for files. > > This change fixes blktest block/003, and removes an extraneous > error print in block/013 when testing on a loop device backed > by a block device that does not support discard. > > Signed-off-by: Evan Green > --- > > Changes in v4: > - Mirror blkdev's write_zeroes into loopdev's discard_sectors. > > Changes in v3: > - Updated commit description > > Changes in v2: None > > drivers/block/loop.c | 57 ++++++++++++++++++++++++++++---------------- > 1 file changed, 37 insertions(+), 20 deletions(-) > > diff --git a/drivers/block/loop.c b/drivers/block/loop.c > index bbf21ebeccd3..ca6983a2c975 100644 > --- a/drivers/block/loop.c > +++ b/drivers/block/loop.c > @@ -417,19 +417,14 @@ static int lo_read_transfer(struct loop_device *lo, struct request *rq, > return ret; > } > > -static int lo_discard(struct loop_device *lo, struct request *rq, loff_t pos) > +static int lo_discard(struct loop_device *lo, struct request *rq, > + int mode, loff_t pos) > { > - /* > - * 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. > - */ > struct file *file = lo->lo_backing_file; > - int mode = FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE; > + struct request_queue *q = lo->lo_queue; > int ret; > > - if ((!file->f_op->fallocate) || lo->lo_encrypt_key_size) { > + if (!blk_queue_discard(q)) { > ret = -EOPNOTSUPP; > goto out; > } > @@ -599,8 +594,13 @@ static int do_req_filebacked(struct loop_device *lo, struct request *rq) > case REQ_OP_FLUSH: > return lo_req_flush(lo, rq); > case REQ_OP_DISCARD: > + return lo_discard(lo, rq, > + FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, pos); > + > case REQ_OP_WRITE_ZEROES: > - return lo_discard(lo, rq, pos); > + return lo_discard(lo, rq, > + FALLOC_FL_ZERO_RANGE | FALLOC_FL_KEEP_SIZE, pos); > + > case REQ_OP_WRITE: > if (lo->transfer) > return lo_write_transfer(lo, rq, pos); > @@ -854,6 +854,21 @@ static void loop_config_discard(struct loop_device *lo) > struct file *file = lo->lo_backing_file; > struct inode *inode = file->f_mapping->host; > struct request_queue *q = lo->lo_queue; > + struct request_queue *backingq; > + > + /* > + * If the backing device is a block device, mirror its zeroing > + * capability. REQ_OP_DISCARD translates to a zero-out even when backed > + * by block devices to keep consistent behavior with file-backed loop > + * devices. > + */ > + if (S_ISBLK(inode->i_mode)) { Gwendal pointed out elsewhere that this should be if (S_ISBLK(inode->i_mode) && (lo->lo_encrypt_key_size == 0)). I think that's correct because like the file-backed device, we want to fail discard, forcing the user to manually zero out regions and write out the encrypted zeroes. I'll plan to send a v5 soon.