From patchwork Tue Aug 28 23:38:00 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Darrick Wong X-Patchwork-Id: 963176 Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=linux-ext4-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=oracle.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=oracle.com header.i=@oracle.com header.b="44wkuSYH"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 420QDR4xw5z9s3x for ; Wed, 29 Aug 2018 09:38:15 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727679AbeH2DcN (ORCPT ); Tue, 28 Aug 2018 23:32:13 -0400 Received: from userp2120.oracle.com ([156.151.31.85]:42200 "EHLO userp2120.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727162AbeH2DcN (ORCPT ); Tue, 28 Aug 2018 23:32:13 -0400 Received: from pps.filterd (userp2120.oracle.com [127.0.0.1]) by userp2120.oracle.com (8.16.0.22/8.16.0.22) with SMTP id w7SNXkgP074845; Tue, 28 Aug 2018 23:38:10 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=subject : from : to : cc : date : message-id : in-reply-to : references : mime-version : content-type : content-transfer-encoding; s=corp-2018-07-02; bh=b2UIBWHne2Q2NJXli3RftJud45KOdtGq7Fg6cOuQap4=; b=44wkuSYHZUW1hbBX8/HH8C8gPaTQ4lvHrbFs3gLb/h8Z0oPb+UOYA0hLzGrhLk1bx6xj yHW69wN2ToxBJ+3LaNHqjzt2j+KzZsqv4TuZYZ5fbewB7YOvLVFRztU/cjj1e8M661RK Ewch/Scxbr46ELS9AW2k/WojdanpYUiAOQKgyNxlZc6vCaNZl6fkKZCo+cv+kqj0PviI JS2/eJviP3ytIRlkBW07uJyDXTJ5We8pftSf4RkfsbbMv82xUcYJtKo21CCpK46SlA+g /YMsaFezeZ2NDIMwGVJ3KPNLtIJlNVXwxWNZr/6qwQ1zThkQUHBBtAaz8IgtLU2pkC5r GA== Received: from aserv0021.oracle.com (aserv0021.oracle.com [141.146.126.233]) by userp2120.oracle.com with ESMTP id 2m2yrq6q3y-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 28 Aug 2018 23:38:09 +0000 Received: from userv0122.oracle.com (userv0122.oracle.com [156.151.31.75]) by aserv0021.oracle.com (8.14.4/8.14.4) with ESMTP id w7SNc3Ln018553 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 28 Aug 2018 23:38:04 GMT Received: from abhmp0001.oracle.com (abhmp0001.oracle.com [141.146.116.7]) by userv0122.oracle.com (8.14.4/8.14.4) with ESMTP id w7SNc2eS001926; Tue, 28 Aug 2018 23:38:03 GMT Received: from localhost (/67.169.218.210) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Tue, 28 Aug 2018 16:38:02 -0700 Subject: [PATCH 1/3] libext2fs: use ZERO_RANGE before PUNCH_HOLE in unix_zeroout From: "Darrick J. Wong" To: tytso@mit.edu, darrick.wong@oracle.com Cc: Andreas Dilger , linux-ext4@vger.kernel.org Date: Tue, 28 Aug 2018 16:38:00 -0700 Message-ID: <153549948026.19148.1559925598066978819.stgit@magnolia> In-Reply-To: <153549947412.19148.4891642282274103717.stgit@magnolia> References: <153549947412.19148.4891642282274103717.stgit@magnolia> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=nai engine=5900 definitions=8999 signatures=668708 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-1807170000 definitions=main-1808280228 Sender: linux-ext4-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-ext4@vger.kernel.org From: Darrick J. Wong In unix_zeroout() for files, we should try a ZERO_RANGE before we try PUNCH_HOLE because the former will not cause us to lose preallocated blocks. Since block devices have supported fallocate for a few years now, refactor the fallocate calls into a helper and call it from either case. Reported-by: Andreas Dilger Signed-off-by: Darrick J. Wong Reviewed-by: Andreas Dilger --- lib/ext2fs/unix_io.c | 53 ++++++++++++++++++++++++++++---------------------- 1 file changed, 30 insertions(+), 23 deletions(-) diff --git a/lib/ext2fs/unix_io.c b/lib/ext2fs/unix_io.c index 7a4c9bf5e..74fc8a75d 100644 --- a/lib/ext2fs/unix_io.c +++ b/lib/ext2fs/unix_io.c @@ -1126,6 +1126,31 @@ static errcode_t unix_discard(io_channel channel, unsigned long long block, return EXT2_ET_UNIMPLEMENTED; } +/* + * If we know about ZERO_RANGE, try that before we try PUNCH_HOLE because + * ZERO_RANGE doesn't unmap preallocated blocks. We prefer fallocate because + * it always invalidates page cache, and libext2fs requires that reads after + * ZERO_RANGE return zeroes. + */ +static int __unix_zeroout(int fd, off_t offset, off_t len) +{ + int ret = -1; + +#if defined(HAVE_FALLOCATE) && defined(FALLOC_FL_ZERO_RANGE) + ret = fallocate(fd, FALLOC_FL_ZERO_RANGE, offset, len); + if (ret == 0) + return 0; +#endif +#if defined(HAVE_FALLOCATE) && defined(FALLOC_FL_PUNCH_HOLE) && defined(FALLOC_FL_KEEP_SIZE) + ret = fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, + offset, len); + if (ret == 0) + return 0; +#endif + errno = EOPNOTSUPP; + return ret; +} + /* parameters might not be used if OS doesn't support zeroout */ #if __GNUC_PREREQ (4, 6) #pragma GCC diagnostic push @@ -1144,10 +1169,7 @@ static errcode_t unix_zeroout(io_channel channel, unsigned long long block, if (safe_getenv("UNIX_IO_NOZEROOUT")) goto unimplemented; - if (channel->flags & CHANNEL_FLAGS_BLOCK_DEVICE) { - /* Not implemented until the BLKZEROOUT mess is fixed */ - goto unimplemented; - } else { + if (!(channel->flags & CHANNEL_FLAGS_BLOCK_DEVICE)) { /* Regular file, try to use truncate/punch/zero. */ struct stat statbuf; @@ -1167,26 +1189,11 @@ static errcode_t unix_zeroout(io_channel channel, unsigned long long block, if (ret) goto err; } -#if defined(HAVE_FALLOCATE) && (defined(FALLOC_FL_ZERO_RANGE) || \ - (defined(FALLOC_FL_PUNCH_HOLE) && defined(FALLOC_FL_KEEP_SIZE))) -#if defined(FALLOC_FL_PUNCH_HOLE) && defined(FALLOC_FL_KEEP_SIZE) - ret = fallocate(data->dev, - FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, - (off_t)(block) * channel->block_size + data->offset, - (off_t)(count) * channel->block_size); - if (ret == 0) - goto err; -#endif -#ifdef FALLOC_FL_ZERO_RANGE - ret = fallocate(data->dev, - FALLOC_FL_ZERO_RANGE, - (off_t)(block) * channel->block_size + data->offset, - (off_t)(count) * channel->block_size); -#endif -#else - goto unimplemented; -#endif /* HAVE_FALLOCATE && (ZERO_RANGE || (PUNCH_HOLE && KEEP_SIZE)) */ } + + ret = __unix_zeroout(data->dev, + (off_t)(block) * channel->block_size + data->offset, + (off_t)(count) * channel->block_size); err: if (ret < 0) { if (errno == EOPNOTSUPP)