Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp310704ybi; Wed, 29 May 2019 22:01:25 -0700 (PDT) X-Google-Smtp-Source: APXvYqydG1v1UT4bVE3Juvc5twyyhMgvfp6XLdxFpF2h7HbtaTxgbex/CZY97sNbq11bVQ5N/VPI X-Received: by 2002:a17:90a:238d:: with SMTP id g13mr2080510pje.0.1559192485560; Wed, 29 May 2019 22:01:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1559192485; cv=none; d=google.com; s=arc-20160816; b=VbWpu1w4xuIz6De5RnJXJyE/R1mHmbPj6Qnx1MUT84mJLFmlvYG43ly2xqnCdgjOr8 B5GSBGb+Rt8LYcJbL+1AyFGWzdKeKeymsqEzglCpzO+rlirKayzSAqd4RYdClTKOxXln Ozdnj49hIjarhbvsPN2O5z7MtkLMI9hG5WcMHpDla466GxLaipL/FU8p08LCfuOKNdR7 4B5/pQeY5GJZCvecr7N9ok48P9cHmd1sPDzbkPex/l7/sGnKV5lKWFKnP56Eh1yKWV2b kdX1Qis9Yt/u60Iyv++CLh5DkmJ/CrdBUBEtalPMlVC51wwumHLJSlETAb3Vvuu5qrqY A2mA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=olLzw1ZpMM4p1F2OUk3xOlHpBhQO0rlqiX5kULXNnL4=; b=CzkZQ/YDGEOeGHSGSVhDZHOl6G0TccCAg2gJUwy2FAFfV7BlGRqLnV18/7eWmoL9Bg g2SGf/qsuS8Iw5OTq0jzlrT/4a0fc3IpT/Ud7cAxhMZvht/pF0SM0up/z2+MnJmwdgA0 /aS+RvJivQLxcDBFP6mPWF0QLq3SLfEPAdFEwVo8QqDzzl11UfBP7/cnnX4/Kpb3MHpL ifl3O0QQPjPgqZdLbAcLZwp0Zrzqz7pjFxzi2koqlbSXZMjxCLS/ghTq8VvHPJLR/ypk og6qppSBc+YltDRFGQszHIm2jzwhIDKqFCK6o+ZGWNjO8gZ2NjCA9X0tVr417FPKVn8v ecZw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=CN8D9iW8; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id j23si2213016pgj.85.2019.05.29.22.01.08; Wed, 29 May 2019 22:01:25 -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=@kernel.org header.s=default header.b=CN8D9iW8; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727720AbfE3DJm (ORCPT + 99 others); Wed, 29 May 2019 23:09:42 -0400 Received: from mail.kernel.org ([198.145.29.99]:44512 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727628AbfE3DJe (ORCPT ); Wed, 29 May 2019 23:09:34 -0400 Received: from localhost (ip67-88-213-2.z213-88-67.customer.algx.net [67.88.213.2]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 2E5222447A; Thu, 30 May 2019 03:09:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1559185773; bh=5csleT8ghslY0fWFB+ZwY/DX6vuencMblx5MN3WuJ6Q=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=CN8D9iW8HgtpRlIY4Sjs1HFPxHYw43h8SfCuZAwDDFTH+wW7hCFGPLIme1etvclgY UMJQm4aYeUOxTbI3Z2l3UVLcwKX5SDNd5erO9Se7LzF/2OkVEWcuiTe5DqIX4uk4l9 WqVKmdEYaFGmZkNNCY2uHxo5wdMF0Pn2Vta/U1ig= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Josef Bacik , Filipe Manana , David Sterba Subject: [PATCH 5.1 026/405] Btrfs: avoid fallback to transaction commit during fsync of files with holes Date: Wed, 29 May 2019 20:00:24 -0700 Message-Id: <20190530030541.995801841@linuxfoundation.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190530030540.291644921@linuxfoundation.org> References: <20190530030540.291644921@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Filipe Manana commit ebb929060aeb162417b4c1307e63daee47b208d9 upstream. When we are doing a full fsync (bit BTRFS_INODE_NEEDS_FULL_SYNC set) of a file that has holes and has file extent items spanning two or more leafs, we can end up falling to back to a full transaction commit due to a logic bug that leads to failure to insert a duplicate file extent item that is meant to represent a hole between the last file extent item of a leaf and the first file extent item in the next leaf. The failure (EEXIST error) leads to a transaction commit (as most errors when logging an inode do). For example, we have the two following leafs: Leaf N: ----------------------------------------------- | ..., ..., ..., (257, FILE_EXTENT_ITEM, 64K) | ----------------------------------------------- The file extent item at the end of leaf N has a length of 4Kb, representing the file range from 64K to 68K - 1. Leaf N + 1: ----------------------------------------------- | (257, FILE_EXTENT_ITEM, 72K), ..., ..., ... | ----------------------------------------------- The file extent item at the first slot of leaf N + 1 has a length of 4Kb too, representing the file range from 72K to 76K - 1. During the full fsync path, when we are at tree-log.c:copy_items() with leaf N as a parameter, after processing the last file extent item, that represents the extent at offset 64K, we take a look at the first file extent item at the next leaf (leaf N + 1), and notice there's a 4K hole between the two extents, and therefore we insert a file extent item representing that hole, starting at file offset 68K and ending at offset 72K - 1. However we don't update the value of *last_extent, which is used to represent the end offset (plus 1, non-inclusive end) of the last file extent item inserted in the log, so it stays with a value of 68K and not with a value of 72K. Then, when copy_items() is called for leaf N + 1, because the value of *last_extent is smaller then the offset of the first extent item in the leaf (68K < 72K), we look at the last file extent item in the previous leaf (leaf N) and see it there's a 4K gap between it and our first file extent item (again, 68K < 72K), so we decide to insert a file extent item representing the hole, starting at file offset 68K and ending at offset 72K - 1, this insertion will fail with -EEXIST being returned from btrfs_insert_file_extent() because we already inserted a file extent item representing a hole for this offset (68K) in the previous call to copy_items(), when processing leaf N. The -EEXIST error gets propagated to the fsync callback, btrfs_sync_file(), which falls back to a full transaction commit. Fix this by adjusting *last_extent after inserting a hole when we had to look at the next leaf. Fixes: 4ee3fad34a9c ("Btrfs: fix fsync after hole punching when using no-holes feature") Cc: stable@vger.kernel.org # 4.14+ Reviewed-by: Josef Bacik Signed-off-by: Filipe Manana Signed-off-by: David Sterba Signed-off-by: Greg Kroah-Hartman --- fs/btrfs/tree-log.c | 1 + 1 file changed, 1 insertion(+) --- a/fs/btrfs/tree-log.c +++ b/fs/btrfs/tree-log.c @@ -4169,6 +4169,7 @@ fill_holes: *last_extent, 0, 0, len, 0, len, 0, 0, 0); + *last_extent += len; } } }