Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp2745686pxj; Mon, 31 May 2021 09:40:20 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxPiEGMNnlx07SSlVb/BDlFn5HEiigG9Ht7SNPpcXoppGQusx/n1fLr9rpAhZEfILaIK1YS X-Received: by 2002:a17:906:7c4:: with SMTP id m4mr23394150ejc.479.1622479220029; Mon, 31 May 2021 09:40:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1622479220; cv=none; d=google.com; s=arc-20160816; b=gn724+j5gDHp2Jhm2puIqOB/Q/5ms5Mzb9piXmRuCvq9aSPzsQjB6275Z/vuGX+nUK JFYZZlT9Yt0UTtA8k8uJQXWgdU2is5p3KSrWPgMzaGl9Yrq2bik4QiimEFa17Yw1XyOH e3Ys7qDk6vjJAqiMEALWPWbCtJWYDhB674EYHRW1oUdDxXYIwG2qFRTUv1fW7LFk9Cfo M0y1W5/VpVBvYIpyKkH5RGq1Fn4X8ZzIgxXIsZKYzlt69Lg19qwSPZ7Zcmmh5ynbCyUn Tl8RnXNqJB700b49NUEzhwF/F0FaFxXSrgL2ww9lomXBKhXyxlXaMRpGy6z6ta9elP+E mm4w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=BwLQwIjWGDwqgynNlUXyVQI82tzvsgq/bzvmtJNkM1k=; b=LEMmUIiNXHvGjBnTmZCGDSPbCLvC6T+CSDNaDhaelO66FaQZlQLtUPiPOGEleNjrTM GJLMJMZa34qC4Q64PiJTUlwJ8Vz+0i3bC5qWnSsFyQaTn/YdW1wGzmYmtPFu28Rn1cWu ValxY04lEfsdzfIx+vQ5iivUcNc1tcEzYAasCkkBqZqKjk6/UufIPz6QqptDM7I2sHuV cOYqh89kLSFX7oDfvWp9O1XRevUtxZEX5M1ait+6F4lEbC9gZB6qV9k4Av8rk2aT3M/a qILb5UC+mPxDVH31ZnzeMeD+p6WyhkiSsCcsVwL4JZFAJQzN4wBLcFfrlU7EwzjXRHV6 xHwQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Z+UBex+D; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id v24si13203576ejc.165.2021.05.31.09.39.57; Mon, 31 May 2021 09:40:20 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Z+UBex+D; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233577AbhEaQiv (ORCPT + 99 others); Mon, 31 May 2021 12:38:51 -0400 Received: from mail.kernel.org ([198.145.29.99]:45938 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232043AbhEaOtn (ORCPT ); Mon, 31 May 2021 10:49:43 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 7083961C9B; Mon, 31 May 2021 13:57:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1622469447; bh=gvmv6dZueX+ccYGnRveAXVCBHyGB4Q5Y5MB37H0YJkY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Z+UBex+DuAeEEFGmfjmUg5aYJQR3vA/eB1NP8s9/lS0G3k2BvSUKG437sEYM24V/W dCDlp59QR8PbE36qUjNzsuZ7C0/ELwqzhW06HcsUw+5me+QGCoSDqF96WsPD8bN0jm VkKOGMqgULtSvlVamSwqM431wWX3PmBr+q0pzqwY= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Josef Bacik , Boris Burkov , David Sterba , Sasha Levin Subject: [PATCH 5.12 200/296] btrfs: return whole extents in fiemap Date: Mon, 31 May 2021 15:14:15 +0200 Message-Id: <20210531130710.608274346@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210531130703.762129381@linuxfoundation.org> References: <20210531130703.762129381@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Boris Burkov [ Upstream commit 15c7745c9a0078edad1f7df5a6bb7b80bc8cca23 ] `xfs_io -c 'fiemap ' ` can give surprising results on btrfs that differ from xfs. btrfs prints out extents trimmed to fit the user input. If the user's fiemap request has an offset, then rather than returning each whole extent which intersects that range, we also trim the start extent to not have start < off. Documentation in filesystems/fiemap.txt and the xfs_io man page suggests that returning the whole extent is expected. Some cases which all yield the same fiemap in xfs, but not btrfs: dd if=/dev/zero of=$f bs=4k count=1 sudo xfs_io -c 'fiemap 0 1024' $f 0: [0..7]: 26624..26631 sudo xfs_io -c 'fiemap 2048 1024' $f 0: [4..7]: 26628..26631 sudo xfs_io -c 'fiemap 2048 4096' $f 0: [4..7]: 26628..26631 sudo xfs_io -c 'fiemap 3584 512' $f 0: [7..7]: 26631..26631 sudo xfs_io -c 'fiemap 4091 5' $f 0: [7..6]: 26631..26630 I believe this is a consequence of the logic for merging contiguous extents represented by separate extent items. That logic needs to track the last offset as it loops through the extent items, which happens to pick up the start offset on the first iteration, and trim off the beginning of the full extent. To fix it, start `off` at 0 rather than `start` so that we keep the iteration/merging intact without cutting off the start of the extent. after the fix, all the above commands give: 0: [0..7]: 26624..26631 The merging logic is exercised by fstest generic/483, and I have written a new fstest for checking we don't have backwards or zero-length fiemaps for cases like those above. Reviewed-by: Josef Bacik Signed-off-by: Boris Burkov Signed-off-by: David Sterba Signed-off-by: Sasha Levin --- fs/btrfs/extent_io.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/fs/btrfs/extent_io.c b/fs/btrfs/extent_io.c index 910769d5fcdb..1eb5d22d5373 100644 --- a/fs/btrfs/extent_io.c +++ b/fs/btrfs/extent_io.c @@ -4975,7 +4975,7 @@ int extent_fiemap(struct btrfs_inode *inode, struct fiemap_extent_info *fieinfo, u64 start, u64 len) { int ret = 0; - u64 off = start; + u64 off; u64 max = start + len; u32 flags = 0; u32 found_type; @@ -5010,6 +5010,11 @@ int extent_fiemap(struct btrfs_inode *inode, struct fiemap_extent_info *fieinfo, goto out_free_ulist; } + /* + * We can't initialize that to 'start' as this could miss extents due + * to extent item merging + */ + off = 0; start = round_down(start, btrfs_inode_sectorsize(inode)); len = round_up(max, btrfs_inode_sectorsize(inode)) - start; -- 2.30.2