Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.2 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_SANE_1 autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 90CC3C35254 for ; Mon, 17 Feb 2020 11:42:24 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 67B68206F4 for ; Mon, 17 Feb 2020 11:42:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728713AbgBQLmX (ORCPT ); Mon, 17 Feb 2020 06:42:23 -0500 Received: from mx2.suse.de ([195.135.220.15]:60126 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728687AbgBQLmX (ORCPT ); Mon, 17 Feb 2020 06:42:23 -0500 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id AF359AD08; Mon, 17 Feb 2020 11:42:20 +0000 (UTC) Subject: Re: [PATCH v3 2/3] btrfs: backref: Implement btrfs_backref_iterator_next() To: Qu Wenruo , Qu Wenruo , linux-btrfs@vger.kernel.org References: <20200217063111.65941-1-wqu@suse.com> <20200217063111.65941-3-wqu@suse.com> <2cee1b97-b6a3-bffd-8cb0-cb7d903497ca@gmx.com> From: Nikolay Borisov Autocrypt: addr=nborisov@suse.com; prefer-encrypt=mutual; keydata= xsFNBFiKBz4BEADNHZmqwhuN6EAzXj9SpPpH/nSSP8YgfwoOqwrP+JR4pIqRK0AWWeWCSwmZ T7g+Rbf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QUC WIoHPgIbDAAKCRBxvoJG5T8oV1pyD/4zdXdOL0lhkSIjJWGqz7Idvo0wjVHSSQCbOwZDWNTN JBTP0BUxHpPu/Z8gRNNP9/k6i63T4eL1xjy4umTwJaej1X15H8Hsh+zakADyWHadbjcUXCkg OJK4NsfqhMuaIYIHbToi9K5pAKnV953xTrK6oYVyd/Rmkmb+wgsbYQJ0Ur1Ficwhp6qU1CaJ mJwFjaWaVgUERoxcejL4ruds66LM9Z1Qqgoer62ZneID6ovmzpCWbi2sfbz98+kW46aA/w8r 7sulgs1KXWhBSv5aWqKU8C4twKjlV2XsztUUsyrjHFj91j31pnHRklBgXHTD/pSRsN0UvM26 lPs0g3ryVlG5wiZ9+JbI3sKMfbdfdOeLxtL25ujs443rw1s/PVghphoeadVAKMPINeRCgoJH zZV/2Z/myWPRWWl/79amy/9MfxffZqO9rfugRBORY0ywPHLDdo9Kmzoxoxp9w3uTrTLZaT9M KIuxEcV8wcVjr+Wr9zRl06waOCkgrQbTPp631hToxo+4rA1jiQF2M80HAet65ytBVR2pFGZF zGYYLqiG+mpUZ+FPjxk9kpkRYz61mTLSY7tuFljExfJWMGfgSg1OxfLV631jV1TcdUnx+h3l Sqs2vMhAVt14zT8mpIuu2VNxcontxgVr1kzYA/tQg32fVRbGr449j1gw57BV9i0vww== Message-ID: <696547c7-84ea-d346-cecb-6270c2430031@suse.com> Date: Mon, 17 Feb 2020 13:42:19 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.4.1 MIME-Version: 1.0 In-Reply-To: <2cee1b97-b6a3-bffd-8cb0-cb7d903497ca@gmx.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 8bit Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org On 17.02.20 г. 13:29 ч., Qu Wenruo wrote: > > > On 2020/2/17 下午6:47, Nikolay Borisov wrote: >> >> >> On 17.02.20 г. 8:31 ч., Qu Wenruo wrote: >>> This function will go next inline/keyed backref for >>> btrfs_backref_iterator infrastructure. >>> >>> Signed-off-by: Qu Wenruo >>> --- >>> fs/btrfs/backref.c | 47 ++++++++++++++++++++++++++++++++++++++++++++++ >>> fs/btrfs/backref.h | 34 +++++++++++++++++++++++++++++++++ >>> 2 files changed, 81 insertions(+) >>> >>> diff --git a/fs/btrfs/backref.c b/fs/btrfs/backref.c >>> index 8bd5e067831c..fb0abe344851 100644 >>> --- a/fs/btrfs/backref.c >>> +++ b/fs/btrfs/backref.c >>> @@ -2310,3 +2310,50 @@ int btrfs_backref_iterator_start(struct btrfs_backref_iterator *iterator, >>> btrfs_backref_iterator_release(iterator); >>> return ret; >>> } >>> + >>> +int btrfs_backref_iterator_next(struct btrfs_backref_iterator *iterator) >> >> Document the return values: 0 in case there are more backerfs for the >> given bytenr or 1 in case there are'nt. And a negative value in case of >> error. >> >>> +{ >>> + struct extent_buffer *eb = btrfs_backref_get_eb(iterator); >>> + struct btrfs_path *path = iterator->path; >>> + struct btrfs_extent_inline_ref *iref; >>> + int ret; >>> + u32 size; >>> + >>> + if (btrfs_backref_iterator_is_inline_ref(iterator)) { >>> + /* We're still inside the inline refs */ >>> + if (btrfs_backref_has_tree_block_info(iterator)) { >>> + /* First tree block info */ >>> + size = sizeof(struct btrfs_tree_block_info); >>> + } else { >>> + /* Use inline ref type to determine the size */ >>> + int type; >>> + >>> + iref = (struct btrfs_extent_inline_ref *) >>> + (iterator->cur_ptr); >>> + type = btrfs_extent_inline_ref_type(eb, iref); >>> + >>> + size = btrfs_extent_inline_ref_size(type); >>> + } >>> + iterator->cur_ptr += size; >>> + if (iterator->cur_ptr < iterator->end_ptr) >>> + return 0; >>> + >>> + /* All inline items iterated, fall through */ >>> + } >> >> This if could be rewritten as: >> if (btrfs_backref_iterator_is_inline_ref(iterator) && iterator->cur_ptr >> < iterator->end_ptr) >> >> what this achieves is: >> >> 1. Clarity that this whole branch is executed only if we are within the >> inline refs limits >> 2. It also optimises that function since in the current version, after >> the last inline backref has been processed iterator->cur_ptr == >> iterator->end_ptr. On the next call to btrfs_backref_iterator_next you >> will execute (needlessly) >> >> (struct btrfs_extent_inline_ref *) (iterator->cur_ptr); >> type = btrfs_extent_inline_ref_type(eb, iref); >> size = btrfs_extent_inline_ref_size(type); >> iterator->cur_ptr += size; >> only to fail "if (iterator->cur_ptr < iterator->end_ptr)" check and >> continue processing keyed items. >> >> As a matter of fact you will be reading past the metadata_item since >> cur_ptr will be at the end of them and any deferences will read from the >> next item this might not cause a crash but it's still wrong. > > This shouldn't happen, as we must ensure the cur_ptr < item_end for callers. How are you ensuring this? Before processing the last inline ref cur_ptr would be end_ptr - btrfs_extent_inline_ref_size(type); After it's processed cur_ptr == end_ptr. THen you will do another call to btrfs_backref_iterator_next which will do the same calculation? What am I missing? > > For the _next() call, the check after increased cur_ptr check it's OK. > > But it's a problem for _start() call, as we may have a case where an > EXTENT_ITEM/METADATA_ITEM has no inlined ref. > > I'll fix this in next version. > > Thanks, > Qu > >> >> >>> + /* We're at keyed items, there is no inline item, just go next item */ >>> + ret = btrfs_next_item(iterator->fs_info->extent_root, iterator->path); >>> + if (ret > 0 || ret < 0) >>> + return ret; >> >> nit: if (ret != 0) return ret; >> >> >>