From patchwork Tue Oct 30 23:06:23 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Evan Green X-Patchwork-Id: 10661797 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id E31EB14DE for ; Tue, 30 Oct 2018 23:06:51 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D271628CCE for ; Tue, 30 Oct 2018 23:06:51 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C519B2A779; Tue, 30 Oct 2018 23:06:51 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 78E3828CCE for ; Tue, 30 Oct 2018 23:06:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728643AbeJaICF (ORCPT ); Wed, 31 Oct 2018 04:02:05 -0400 Received: from mail-pf1-f193.google.com ([209.85.210.193]:34907 "EHLO mail-pf1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728641AbeJaICF (ORCPT ); Wed, 31 Oct 2018 04:02:05 -0400 Received: by mail-pf1-f193.google.com with SMTP id z2-v6so4029975pfe.2 for ; Tue, 30 Oct 2018 16:06:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=HqkIIj+yPtVDWhA7Dl1mMunLg/jddgwc20rm34UXfPk=; b=QKQBOXPGrfBIc5BMpy9JSoIu75TeQDveLZ3FwTHPY3hbfwqx7y9+mRFcOP3ww0GnEF ATV3k2O/MoEMxQWG1TnqxNhKpD3e+R0+C4qnqTrdSwL8GNfuUB+zqNNsjROfLywXp/YX keDUhP/QZGbolG7Rcjwxff85X7VKa7piufgzk= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=HqkIIj+yPtVDWhA7Dl1mMunLg/jddgwc20rm34UXfPk=; b=TdTVK4vXHgwS/zGzVmVGClR5phH0CiWnSDMoLHmM1o2GPdeUCglVZXxynAedZ0/qRV U/GhWSaHj1kJ9puH9uq33w2/YABciUdiIaxJPhWJHFoV+vD1fOGBoabJp2CelRNJdfrb FQJ8HdE4XQhnVplsArdTc5UuE6F+REu62ngDY2OLwGNr6DSkHwlkzpxUpnrrSzDjDYPe RNt+uI1QdgE7K+lXBxFz2d0X+hxoqE22cKnqiUNbnUYzc1W4LEWFvsDYkXDyOk+sinip f6fdyQcpMe8EFZoX79AHMztdvdpV/bTUfjksMk50e7hMO1AZ4d2utWZUStV4MTJAdvJo rSjA== X-Gm-Message-State: AGRZ1gIrtJYnVr4i2W/blmN4He9NxSnLjCzRYHdfErVymRIKSwNNfQYQ ouWvDKCip9B/afU5RjscfTI2XQ== X-Google-Smtp-Source: AJdET5exaX9NP66IdYrFbmITcfg/g2cQ+L3CyDu1Cj2se23A/hl/hPrjkn43bSWTE+FAn1Knb2YbOQ== X-Received: by 2002:a62:968a:: with SMTP id s10-v6mr663408pfk.191.1540940798380; Tue, 30 Oct 2018 16:06:38 -0700 (PDT) Received: from evgreen2.mtv.corp.google.com ([2620:15c:202:201:e418:c825:76cf:5f64]) by smtp.gmail.com with ESMTPSA id z15-v6sm27953490pfn.170.2018.10.30.16.06.37 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 30 Oct 2018 16:06:37 -0700 (PDT) From: Evan Green To: Jens Axboe Cc: Gwendal Grignou , Alexis Savery , Evan Green , linux-block@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 1/2] loop: Report EOPNOTSUPP properly Date: Tue, 30 Oct 2018 16:06:23 -0700 Message-Id: <20181030230624.61834-2-evgreen@chromium.org> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20181030230624.61834-1-evgreen@chromium.org> References: <20181030230624.61834-1-evgreen@chromium.org> Sender: linux-block-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Properly plumb out EOPNOTSUPP from loop driver operations, which may get returned when for instance a discard operation is attempted but not supported by the underlying block device. Before this change, everything was reported in the log as an I/O error, which is scary and not helpful in debugging. Signed-off-by: Evan Green Reviewed-by: Ming Lei --- drivers/block/loop.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/drivers/block/loop.c b/drivers/block/loop.c index abad6d15f9563..28990fc94841a 100644 --- a/drivers/block/loop.c +++ b/drivers/block/loop.c @@ -458,8 +458,13 @@ static void lo_complete_rq(struct request *rq) if (!cmd->use_aio || cmd->ret < 0 || cmd->ret == blk_rq_bytes(rq) || req_op(rq) != REQ_OP_READ) { - if (cmd->ret < 0) - ret = BLK_STS_IOERR; + if (cmd->ret < 0) { + if (cmd->ret == -EOPNOTSUPP) + ret = BLK_STS_NOTSUPP; + else + ret = BLK_STS_IOERR; + } + goto end_io; } @@ -1788,7 +1793,10 @@ static void loop_handle_cmd(struct loop_cmd *cmd) failed: /* complete non-aio request */ if (!cmd->use_aio || ret) { - cmd->ret = ret ? -EIO : 0; + if (ret == -EOPNOTSUPP) + cmd->ret = ret; + else + cmd->ret = ret ? -EIO : 0; blk_mq_complete_request(rq); } } From patchwork Tue Oct 30 23:06:24 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Evan Green X-Patchwork-Id: 10661795 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 4B28614BD for ; Tue, 30 Oct 2018 23:06:50 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3988A28CCE for ; Tue, 30 Oct 2018 23:06:50 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2A7942A779; Tue, 30 Oct 2018 23:06:50 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9E86328CCE for ; Tue, 30 Oct 2018 23:06:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728509AbeJaICJ (ORCPT ); Wed, 31 Oct 2018 04:02:09 -0400 Received: from mail-pf1-f193.google.com ([209.85.210.193]:40917 "EHLO mail-pf1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728493AbeJaICJ (ORCPT ); Wed, 31 Oct 2018 04:02:09 -0400 Received: by mail-pf1-f193.google.com with SMTP id g21-v6so6606250pfi.7 for ; Tue, 30 Oct 2018 16:06:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=c4zZNrFqTbcbGzWoE2ofhWPIl6q01gLvGD3sNL54Djw=; b=gBOQqkDdb5psZ47oq0yObJq3+E5newa37J1ai0VgVcy+Cu4LDbTO2aX+baiX2S8qza p3L6qepzuLYpdVJxWQB0Ju5H0KWi1rV3m4c+w3q2tzZLaA4CBOCW2PFmsCmrdpmH0AJX y6b/LBjD1dN4d4Hh99sJORQWNLFCyAY+vB6Ug= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=c4zZNrFqTbcbGzWoE2ofhWPIl6q01gLvGD3sNL54Djw=; b=G2DJT71W1OstQzlN6mgiq33i98WNWBtCZ7h9e62Z/aWtKVOQMvmVmBDfrfCH6bw9Et PGGubGAdhcH8v5tpn/axSfHsf1xpJeImpTKXCyObBhPKzHJv/iBVIS/5+T/6qubv3nKO WxJi/g9yt1YtKok5dDJ+en5xtfFz5ydaFUdOl3ZWWh+yYq7kJvxOIooaqZ1wujsXS+Wm hOUn53F/W76rfOIZw4A5iOGVZz+xYmIpECdO7QfT4xmgmQuEuUcDJV6VdZLOpZEN+j9X y1Tw8fPmk4SEcouabmUN5Yz1xN97g0dGlabNVDg4eio96u1cshrme2Wl1Mxz/oOT8XfD kdjQ== X-Gm-Message-State: AGRZ1gJU2qFdGgZEn/jW8zQsAa9e0KPNguNgRnFjEVvLi13KZBBSPMye 0Ue5PArxIcZ+rFHa1pYcIGE8XA== X-Google-Smtp-Source: AJdET5euFY9NlkbBIvSUaOh5TifG726vulH/AflNaH9yPOzOLtxWePbmAno1+zThbmx/3sD0Lk+3uA== X-Received: by 2002:a62:8a91:: with SMTP id o17-v6mr705259pfk.184.1540940801239; Tue, 30 Oct 2018 16:06:41 -0700 (PDT) Received: from evgreen2.mtv.corp.google.com ([2620:15c:202:201:e418:c825:76cf:5f64]) by smtp.gmail.com with ESMTPSA id z15-v6sm27953490pfn.170.2018.10.30.16.06.39 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 30 Oct 2018 16:06:40 -0700 (PDT) From: Evan Green To: Jens Axboe Cc: Gwendal Grignou , Alexis Savery , Evan Green , linux-block@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 2/2] loop: Better discard support for block devices Date: Tue, 30 Oct 2018 16:06:24 -0700 Message-Id: <20181030230624.61834-3-evgreen@chromium.org> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20181030230624.61834-1-evgreen@chromium.org> References: <20181030230624.61834-1-evgreen@chromium.org> Sender: linux-block-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP If the backing device for a loop device is a block device, then mirror the discard properties of the underlying block device into the loop device. 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. Signed-off-by: Evan Green --- drivers/block/loop.c | 61 +++++++++++++++++++++++++++++++++++----------------- 1 file changed, 41 insertions(+), 20 deletions(-) diff --git a/drivers/block/loop.c b/drivers/block/loop.c index 28990fc94841a..176e65101c4ef 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; } @@ -603,8 +598,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); @@ -859,6 +859,25 @@ 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 discard + * capabilities. + */ + if (S_ISBLK(inode->i_mode)) { + backingq = bdev_get_queue(inode->i_bdev); + blk_queue_max_discard_sectors(q, + backingq->limits.max_discard_sectors); + + blk_queue_max_write_zeroes_sectors(q, + backingq->limits.max_write_zeroes_sectors); + + q->limits.discard_granularity = + backingq->limits.discard_granularity; + + q->limits.discard_alignment = + backingq->limits.discard_alignment; /* * We use punch hole to reclaim the free space used by the @@ -866,22 +885,24 @@ static void loop_config_discard(struct loop_device *lo) * encryption is enabled, because it may give an attacker * useful information. */ - if ((!file->f_op->fallocate) || - lo->lo_encrypt_key_size) { + } else if ((!file->f_op->fallocate) || lo->lo_encrypt_key_size) { q->limits.discard_granularity = 0; q->limits.discard_alignment = 0; blk_queue_max_discard_sectors(q, 0); blk_queue_max_write_zeroes_sectors(q, 0); - blk_queue_flag_clear(QUEUE_FLAG_DISCARD, q); - return; - } - q->limits.discard_granularity = inode->i_sb->s_blocksize; - q->limits.discard_alignment = 0; + } else { + q->limits.discard_granularity = inode->i_sb->s_blocksize; + q->limits.discard_alignment = 0; + + blk_queue_max_discard_sectors(q, UINT_MAX >> 9); + blk_queue_max_write_zeroes_sectors(q, UINT_MAX >> 9); + } - blk_queue_max_discard_sectors(q, UINT_MAX >> 9); - blk_queue_max_write_zeroes_sectors(q, UINT_MAX >> 9); - blk_queue_flag_set(QUEUE_FLAG_DISCARD, q); + if (q->limits.max_discard_sectors || q->limits.max_write_zeroes_sectors) + blk_queue_flag_set(QUEUE_FLAG_DISCARD, q); + else + blk_queue_flag_clear(QUEUE_FLAG_DISCARD, q); } static void loop_unprepare_queue(struct loop_device *lo)