From patchwork Thu Oct 10 17:02:39 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Darrick J. Wong" X-Patchwork-Id: 11183963 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 2525C18B7 for ; Thu, 10 Oct 2019 17:04:54 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id EEEC921929 for ; Thu, 10 Oct 2019 17:04:53 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b="iiu1gsn5" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726593AbfJJREx (ORCPT ); Thu, 10 Oct 2019 13:04:53 -0400 Received: from userp2130.oracle.com ([156.151.31.86]:44304 "EHLO userp2130.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726007AbfJJREw (ORCPT ); Thu, 10 Oct 2019 13:04:52 -0400 Received: from pps.filterd (userp2130.oracle.com [127.0.0.1]) by userp2130.oracle.com (8.16.0.27/8.16.0.27) with SMTP id x9AGxhQb015048; Thu, 10 Oct 2019 17:04:45 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=date : from : to : cc : subject : message-id : mime-version : content-type; s=corp-2019-08-05; bh=VL6R48RwqDkFrj5F5+e3HamQIy5Wl2g3BYnKqnglJVc=; b=iiu1gsn5A9NOsFAA80jsx0D2Sgh7GBulceiom2WLKeQdJJ8l4tR3BPz0q3ZIje2mI+E3 8+dd5v5QY2qiJXHVCgqhf7X2XhLl9L+OkfADj0ctYom7z4v0s2fp9PRpWPZnqPHYIbsE C3kp2MP2OTLrOwRmKezaTtYZ/+AEXEw6ZYhU/Z3+RqJNuOi0oVoe0SYcFbcF1ZaMNBDA 8VO40qYC/5RXcEh//LcRkSW0u4doDgNM7Cc36gzgNnBM+Hare3UztyGDySr+otBuyXVd /69fdgyZvNnTmnYYiYYzU+3xfUQCrBZvk4Usl69xRfustZbNZyL+hYsii9/bCFcxYmTg Rw== Received: from aserp3020.oracle.com (aserp3020.oracle.com [141.146.126.70]) by userp2130.oracle.com with ESMTP id 2vejkuvhsg-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 10 Oct 2019 17:04:45 +0000 Received: from pps.filterd (aserp3020.oracle.com [127.0.0.1]) by aserp3020.oracle.com (8.16.0.27/8.16.0.27) with SMTP id x9AGx6Sq102259; Thu, 10 Oct 2019 17:02:44 GMT Received: from aserv0121.oracle.com (aserv0121.oracle.com [141.146.126.235]) by aserp3020.oracle.com with ESMTP id 2vhhsq1nqa-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 10 Oct 2019 17:02:44 +0000 Received: from abhmp0003.oracle.com (abhmp0003.oracle.com [141.146.116.9]) by aserv0121.oracle.com (8.14.4/8.13.8) with ESMTP id x9AH2fOp011813; Thu, 10 Oct 2019 17:02:43 GMT Received: from localhost (/67.169.218.210) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Thu, 10 Oct 2019 10:02:40 -0700 Date: Thu, 10 Oct 2019 10:02:39 -0700 From: "Darrick J. Wong" To: Jens Axboe , Christoph Hellwig Cc: linux-block@vger.kernel.org, linux-fsdevel , xfs Subject: [PATCH] loop: fix no-unmap write-zeroes request behavior Message-ID: <20191010170239.GC13098@magnolia> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.9.4 (2018-02-28) X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9405 signatures=668684 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 malwarescore=0 phishscore=0 bulkscore=0 spamscore=0 mlxscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1908290000 definitions=main-1910100151 X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9405 signatures=668684 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 priorityscore=1501 malwarescore=0 suspectscore=0 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1908290000 definitions=main-1910100151 Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org From: Darrick J. Wong 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 forbids us from freeing 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 --- drivers/block/loop.c | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/drivers/block/loop.c b/drivers/block/loop.c index f6f77eaa7217..0dc981e94bf0 100644 --- a/drivers/block/loop.c +++ b/drivers/block/loop.c @@ -441,6 +441,35 @@ static int lo_discard(struct loop_device *lo, struct request *rq, loff_t pos) return ret; } +static int lo_zeroout(struct loop_device *lo, struct request *rq, loff_t pos) +{ + struct file *file = lo->lo_backing_file; + int mode = FALLOC_FL_ZERO_RANGE | FALLOC_FL_KEEP_SIZE; + int ret; + + /* If we're allowed to unmap the blocks, ask the fs to punch them. */ + if (!(rq->cmd_flags & REQ_NOUNMAP)) { + ret = lo_discard(lo, rq, pos); + if (!ret) + return 0; + } + + /* + * Otherwise, ask the fs to zero out the blocks, which will result in + * space being allocated to the file. + */ + if (!file->f_op->fallocate) { + ret = -EOPNOTSUPP; + goto out; + } + + ret = file->f_op->fallocate(file, mode, pos, blk_rq_bytes(rq)); + if (unlikely(ret && ret != -EINVAL && ret != -EOPNOTSUPP)) + ret = -EIO; + out: + return ret; +} + static int lo_req_flush(struct loop_device *lo, struct request *rq) { struct file *file = lo->lo_backing_file; @@ -597,8 +626,9 @@ 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: - case REQ_OP_WRITE_ZEROES: return lo_discard(lo, rq, pos); + case REQ_OP_WRITE_ZEROES: + return lo_zeroout(lo, rq, pos); case REQ_OP_WRITE: if (lo->transfer) return lo_write_transfer(lo, rq, pos);