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.0 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,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 BB8F2C7619F for ; Mon, 17 Feb 2020 11:29:57 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 8B233207FD for ; Mon, 17 Feb 2020 11:29:57 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=gmx.net header.i=@gmx.net header.b="iS+2t3as" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728930AbgBQL35 (ORCPT ); Mon, 17 Feb 2020 06:29:57 -0500 Received: from mout.gmx.net ([212.227.15.19]:38885 "EHLO mout.gmx.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728926AbgBQL34 (ORCPT ); Mon, 17 Feb 2020 06:29:56 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=badeba3b8450; t=1581938990; bh=h0kzELxuH2CQbFn+62LHDHuj4+MbKdIzBbDifYfopbc=; h=X-UI-Sender-Class:Subject:To:References:From:Date:In-Reply-To; b=iS+2t3asZYkBXBX++3ZsuKRNVEGBZEZ+nomrRoPFX6Z0BKOQgKptGk5nKQ7fZLA8w WqnxXREObU9ay2jvl+SoYO4AX6FqLMncz8KLwH8Lvp1wXAc/kAGKKznfGsMZw3845c oEbEVywJIaGuv8wHEujVkEJxNkhYbanzMrBwFdqI= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from [0.0.0.0] ([149.28.201.231]) by mail.gmx.com (mrgmx004 [212.227.17.184]) with ESMTPSA (Nemesis) id 1MXXyP-1ixkv41LPb-00Ywnu; Mon, 17 Feb 2020 12:29:50 +0100 Subject: Re: [PATCH v3 2/3] btrfs: backref: Implement btrfs_backref_iterator_next() To: Nikolay Borisov , Qu Wenruo , linux-btrfs@vger.kernel.org References: <20200217063111.65941-1-wqu@suse.com> <20200217063111.65941-3-wqu@suse.com> From: Qu Wenruo Message-ID: <2cee1b97-b6a3-bffd-8cb0-cb7d903497ca@gmx.com> Date: Mon, 17 Feb 2020 19:29:43 +0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.4.2 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: quoted-printable X-Provags-ID: V03:K1:en2syb0ScueBX1CfYpm/HYcj7J3/u7ameP7oR6PJAgmYC+JfBRm aTGwJMYDXeuEAbC97E1SXwJ7axqNMt4HCuiPEDgKseEIg5DexAlj3dgk3HKO8WQzP20Fb5F 7NzC7YqJtxlO3ZpEFA1KwrfBAcIQexzJN9ljhDgjE36m/G8XnPz6lQfCkvyaiYG5cRsEDDk 208BYpwV+6DCB05PHOVxQ== X-UI-Out-Filterresults: notjunk:1;V03:K0:yq4/bqQZeh0=:cX4XUer+vVIiUUIq7aY5vZ 1Lj958j94lKxHdvk4Rlgf4hw+mPJ4kI+qucbT86O8zQbgadfnw27IzC6ojO9Nr+kMHDQdcT9T 1IIkusevFGuy20jnZVly7czrF/maPhS/XJfDPn2Nam4SjRtyB87rbXFF9VRRNdbz1PKWiYw0B KL50CxCLpDowfCz2T/iPIXHrueqPsJcIu/YeUiRU2XLtsHKG7bXJOOtrMTV3abwwtbtO6iXB3 CgUgGkOXSyT7jE0YxYi0uX9C5/30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mailing-List: linux-btrfs@vger.kernel.org On 2020/2/17 =E4=B8=8B=E5=8D=886:47, Nikolay Borisov wrote: > > > On 17.02.20 =D0=B3. 8:31 =D1=87., 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_ba= ckref_iterator *iterator, >> btrfs_backref_iterator_release(iterator); >> return ret; >> } >> + >> +int btrfs_backref_iterator_next(struct btrfs_backref_iterator *iterato= r) > > 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 =3D btrfs_backref_get_eb(iterator); >> + struct btrfs_path *path =3D 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 =3D sizeof(struct btrfs_tree_block_info); >> + } else { >> + /* Use inline ref type to determine the size */ >> + int type; >> + >> + iref =3D (struct btrfs_extent_inline_ref *) >> + (iterator->cur_ptr); >> + type =3D btrfs_extent_inline_ref_type(eb, iref); >> + >> + size =3D btrfs_extent_inline_ref_size(type); >> + } >> + iterator->cur_ptr +=3D 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 =3D=3D > 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 =3D btrfs_extent_inline_ref_type(eb, iref); > size =3D btrfs_extent_inline_ref_size(type); > iterator->cur_ptr +=3D 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 caller= s. 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 =3D btrfs_next_item(iterator->fs_info->extent_root, iterator->pat= h); >> + if (ret > 0 || ret < 0) >> + return ret; > > nit: if (ret !=3D 0) return ret; > > >