From patchwork Wed Mar 1 01:04:39 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liu Bo X-Patchwork-Id: 9597281 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 5D99C600CB for ; Wed, 1 Mar 2017 01:05:55 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4F05127F93 for ; Wed, 1 Mar 2017 01:05:55 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 430A328490; Wed, 1 Mar 2017 01:05:55 +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=-6.9 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, UNPARSEABLE_RELAY 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 A48C727F93 for ; Wed, 1 Mar 2017 01:05:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751722AbdCABFw (ORCPT ); Tue, 28 Feb 2017 20:05:52 -0500 Received: from userp1040.oracle.com ([156.151.31.81]:33336 "EHLO userp1040.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751683AbdCABFv (ORCPT ); Tue, 28 Feb 2017 20:05:51 -0500 Received: from aserv0021.oracle.com (aserv0021.oracle.com [141.146.126.233]) by userp1040.oracle.com (Sentrion-MTA-4.3.2/Sentrion-MTA-4.3.2) with ESMTP id v2115fdh002719 (version=TLSv1 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Wed, 1 Mar 2017 01:05:42 GMT Received: from aserv0122.oracle.com (aserv0122.oracle.com [141.146.126.236]) by aserv0021.oracle.com (8.13.8/8.14.4) with ESMTP id v2115fYA030731 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Wed, 1 Mar 2017 01:05:41 GMT Received: from abhmp0019.oracle.com (abhmp0019.oracle.com [141.146.116.25]) by aserv0122.oracle.com (8.14.4/8.14.4) with ESMTP id v2115fT0032455; Wed, 1 Mar 2017 01:05:41 GMT Received: from localhost.us.oracle.com (/10.211.47.181) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Tue, 28 Feb 2017 17:05:41 -0800 From: Liu Bo To: linux-btrfs@vger.kernel.org Cc: David Sterba Subject: [PATCH 1/2] Btrfs: fix unexpected file hole after disk errors Date: Tue, 28 Feb 2017 17:04:39 -0800 Message-Id: <1488330280-22678-1-git-send-email-bo.li.liu@oracle.com> X-Mailer: git-send-email 2.5.5 X-Source-IP: aserv0021.oracle.com [141.146.126.233] Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Btrfs creates hole extents to cover any unwritten section right before doing buffer writes after commit 3ac0d7b96a26 ("btrfs: Change the expanding write sequence to fix snapshot related bug."). However, that takes the start position of the buffered write to compare against the current EOF, hole extents would be created only if (EOF < start). If the EOF is at the middle of the buffered write, no hole extents will be created and a file hole without a hole extent is left in this file. This bug was revealed by generic/019 in fstests. 'fsstress' in this test may create the above situation and the test then fails all requests including writes, so the buffer write which is supposed to cover the hole (without the hole extent) couldn't make it on disk. Running fsck against such btrfs ends up with detecting file extent holes. Things could be more serious, some stale data would be exposed to userspace if files with this kind of hole are truncated to a position of the hole, because the on-disk inode size is beyond the last extent in the file. This fixes the bug by comparing the end position against the EOF. Signed-off-by: Liu Bo Reviewed-by: Qu Wenruo Reviewed-by: Qu Wenruo --- fs/btrfs/file.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/fs/btrfs/file.c b/fs/btrfs/file.c index b5c5da2..0be837b 100644 --- a/fs/btrfs/file.c +++ b/fs/btrfs/file.c @@ -1861,11 +1861,10 @@ static ssize_t btrfs_file_write_iter(struct kiocb *iocb, pos = iocb->ki_pos; count = iov_iter_count(from); start_pos = round_down(pos, fs_info->sectorsize); + end_pos = round_up(pos + count, fs_info->sectorsize); oldsize = i_size_read(inode); - if (start_pos > oldsize) { + if (end_pos > oldsize) { /* Expand hole size to cover write data, preventing empty gap */ - end_pos = round_up(pos + count, - fs_info->sectorsize); err = btrfs_cont_expand(inode, oldsize, end_pos); if (err) { inode_unlock(inode);