Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp3426519imu; Sun, 11 Nov 2018 14:50:30 -0800 (PST) X-Google-Smtp-Source: AJdET5eqj3WBfBlz1uXdV6HgqBfYZTVtW8q09e5fs7hEEjsOMTb5Oa9cA3Awq1ev0pLYxyfDmure X-Received: by 2002:a17:902:560f:: with SMTP id h15-v6mr14776228pli.160.1541976630062; Sun, 11 Nov 2018 14:50:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1541976630; cv=none; d=google.com; s=arc-20160816; b=FZNlxi+lpoaHlN3nZaVsv4eiKWLlibJQy5j6SZrMAC7RqLEakw1F1u5lwgTMVQUpuQ MeMk9/9CDjJmAMbdCsRy4XaQc8HBGCy7cJ4+x8KQvnh8p3YPFG+UKsev12K1lHiKeQN4 qovqIBKn6wYGknlQV9IXg93WqFDlalspQ3W3Ui4fChsekaV6aMaey6G5mDC3kyT+FPnb zjvE0PHYSgH8ir+OkemzkSVA1V6KWB0Uqxj2OoYW4pjNvmV8jn2aotN2d6dCtMTQzgVb IRVFroZcyO86YqwURf6vmT5FWk0ruQFKdDcP27nILZZD/jZlaDbJrLjG3OqkJuITgokl Vtfg== 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=NiRFx4+9qEevdkwYly+yfF3ZjzFTMLGXwn4iDc8ieuY=; b=pIf4U9Z9ruJgpGGSs4MxJCtvDtvVY0o5cY3TWh5aQzmHWyVKlKasQtu2rezxyexiLa mDomjgShv1d5Ya63UfAvGRz+nWChRj48nkeb74C2b8AyKZjelS2qWTwtXF6vDqu0XE3R x2j9sYscwEUIJR4bIiedYLg3iq9BN7sdDp3lDakZuxVnCSD/5YJvA2Th/jNjJevFQSxe vwvPHK4+Bc5hZEfh6B0i+c1p48xT2iC17BtDcqOLu43AqtiU+lUbGOJkmHd4GkLumc3/ ud4YQH+hnSs99KRneBt+TXNuSqJWYxtnnv9jaxvoUSR3AXOR4nBbq7Acc3DzAOSB616c 2+Aw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=E0nPUmxf; 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 h20si14250805pgm.366.2018.11.11.14.50.15; Sun, 11 Nov 2018 14:50:30 -0800 (PST) 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=E0nPUmxf; 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 S2391041AbeKLIir (ORCPT + 99 others); Mon, 12 Nov 2018 03:38:47 -0500 Received: from mail.kernel.org ([198.145.29.99]:56662 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2404369AbeKLIXU (ORCPT ); Mon, 12 Nov 2018 03:23:20 -0500 Received: from localhost (unknown [206.108.79.134]) (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 B141722353; Sun, 11 Nov 2018 22:33:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1541975602; bh=+hkaSOwKNjNd0W6KwOhKGDVPSeQGCUoVg1Pwl4pGjPo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=E0nPUmxfmd3bthUyPVgVdhxJ6y0g73dv1W8ie8ZGVqneuUGZbHGRy0cRNq42skgQo FYPwuPjX+rOS0XQ5NUHQso/ZezW3mF6H6I9FPV+9PpID9gqEYDo61vxUrlKKKLU8QB KzlRk8lO74X9zGQScWMjTs7xXmVwiFA4IgXTcPNA= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Filipe Manana , David Sterba , Sudip Mukherjee Subject: [PATCH 4.14 220/222] Btrfs: fix fsync after hole punching when using no-holes feature Date: Sun, 11 Nov 2018 14:25:17 -0800 Message-Id: <20181111221705.925278597@linuxfoundation.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181111221647.665769131@linuxfoundation.org> References: <20181111221647.665769131@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review 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 4.14-stable review patch. If anyone has any objections, please let me know. ------------------ From: Filipe Manana commit 4ee3fad34a9cc2cf33303dfbd0cf554248651c86 upstream. When we have the no-holes mode enabled and fsync a file after punching a hole in it, we can end up not logging the whole hole range in the log tree. This happens if the file has extent items that span more than one leaf and we punch a hole that covers a range that starts in a leaf but does not go beyond the offset of the first extent in the next leaf. Example: $ mkfs.btrfs -f -O no-holes -n 65536 /dev/sdb $ mount /dev/sdb /mnt $ for ((i = 0; i <= 831; i++)); do offset=$((i * 2 * 256 * 1024)) xfs_io -f -c "pwrite -S 0xab -b 256K $offset 256K" \ /mnt/foobar >/dev/null done $ sync # We now have 2 leafs in our filesystem fs tree, the first leaf has an # item corresponding the extent at file offset 216530944 and the second # leaf has a first item corresponding to the extent at offset 217055232. # Now we punch a hole that partially covers the range of the extent at # offset 216530944 but does go beyond the offset 217055232. $ xfs_io -c "fpunch $((216530944 + 128 * 1024 - 4000)) 256K" /mnt/foobar $ xfs_io -c "fsync" /mnt/foobar # mount to replay the log $ mount /dev/sdb /mnt # Before this patch, only the subrange [216658016, 216662016[ (length of # 4000 bytes) was logged, leaving an incorrect file layout after log # replay. Fix this by checking if there is a hole between the last extent item that we processed and the first extent item in the next leaf, and if there is one, log an explicit hole extent item. Fixes: 16e7549f045d ("Btrfs: incompatible format change to remove hole extents") Signed-off-by: Filipe Manana Signed-off-by: David Sterba Signed-off-by: Sudip Mukherjee Signed-off-by: Greg Kroah-Hartman --- fs/btrfs/tree-log.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) --- a/fs/btrfs/tree-log.c +++ b/fs/btrfs/tree-log.c @@ -3978,6 +3978,36 @@ fill_holes: break; *last_extent = extent_end; } + + /* + * Check if there is a hole between the last extent found in our leaf + * and the first extent in the next leaf. If there is one, we need to + * log an explicit hole so that at replay time we can punch the hole. + */ + if (ret == 0 && + key.objectid == btrfs_ino(inode) && + key.type == BTRFS_EXTENT_DATA_KEY && + i == btrfs_header_nritems(src_path->nodes[0])) { + ret = btrfs_next_leaf(inode->root, src_path); + need_find_last_extent = true; + if (ret > 0) { + ret = 0; + } else if (ret == 0) { + btrfs_item_key_to_cpu(src_path->nodes[0], &key, + src_path->slots[0]); + if (key.objectid == btrfs_ino(inode) && + key.type == BTRFS_EXTENT_DATA_KEY && + *last_extent < key.offset) { + const u64 len = key.offset - *last_extent; + + ret = btrfs_insert_file_extent(trans, log, + btrfs_ino(inode), + *last_extent, 0, + 0, len, 0, len, + 0, 0, 0); + } + } + } /* * Need to let the callers know we dropped the path so they should * re-search.