From patchwork Thu Jul 21 13:50:04 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikolay Borisov X-Patchwork-Id: 12925215 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E6E5DCCA48A for ; Thu, 21 Jul 2022 13:50:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230430AbiGUNue (ORCPT ); Thu, 21 Jul 2022 09:50:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58596 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230291AbiGUNu1 (ORCPT ); Thu, 21 Jul 2022 09:50:27 -0400 Received: from smtp-out2.suse.de (smtp-out2.suse.de [IPv6:2001:67c:2178:6::1d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 18E69BCBC for ; Thu, 21 Jul 2022 06:50:11 -0700 (PDT) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 92E561F8F9; Thu, 21 Jul 2022 13:50:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1658411410; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=nfkbMJdt6xI4buRaRNDdPK/wQJt6Rmy5payxSexnvdA=; b=UgVna7sjpncsy/zOfXeBZNptQVeyaI8lzmGQXOx1it8kNj9xJVRy/hiHcMzVy6FG013ylD BI+mgXzZDZtxGCmvTpA9dK9kn/UTRf3Bcq1wrx5U1+u7AFZgQUCXaYgBQaMSRGVOoaUMTZ uQE+eBvjjQJeT4OC5LJjfF9B8tQBNv4= Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 6267913A1B; Thu, 21 Jul 2022 13:50:10 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id GLBeFZJZ2WIPMgAAMHmgww (envelope-from ); Thu, 21 Jul 2022 13:50:10 +0000 From: Nikolay Borisov To: linux-btrfs@vger.kernel.org Cc: Nikolay Borisov Subject: [PATCH 1/3] btrfs: introduce btrfs_find_inode Date: Thu, 21 Jul 2022 16:50:04 +0300 Message-Id: <20220721135006.3345302-2-nborisov@suse.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220721135006.3345302-1-nborisov@suse.com> References: <20220721135006.3345302-1-nborisov@suse.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org This function holds common code for searching the root's inode rb tree. It will be used to reduce code duplication in future patches. Signed-off-by: Nikolay Borisov --- fs/btrfs/ctree.h | 1 + fs/btrfs/inode.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/fs/btrfs/ctree.h b/fs/btrfs/ctree.h index 0ae7f6530da1..fc0a0ab01761 100644 --- a/fs/btrfs/ctree.h +++ b/fs/btrfs/ctree.h @@ -3311,6 +3311,7 @@ int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index); int btrfs_unlink_inode(struct btrfs_trans_handle *trans, struct btrfs_inode *dir, struct btrfs_inode *inode, const char *name, int name_len); +struct rb_node *btrfs_find_inode(struct btrfs_root *root, const u64 objectid); int btrfs_add_link(struct btrfs_trans_handle *trans, struct btrfs_inode *parent_inode, struct btrfs_inode *inode, const char *name, int name_len, int add_backref, u64 index); diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c index 5fc831a8eba1..c11169ba28b2 100644 --- a/fs/btrfs/inode.c +++ b/fs/btrfs/inode.c @@ -4587,6 +4587,50 @@ static noinline int may_destroy_subvol(struct btrfs_root *root) return ret; } +/** + * btrfs_find_inode - returns the rb_node pointing to the inode with an ino + * equal or larger than @objectid + * + * @root: root which is going to be searched for an inode + * @objectid: ino being searched for, if no exact match can be found the + * function returns the first largest inode + * + * Returns the rb_node pointing to the specified inode or returns NULL if no + * match is found. + * + */ +struct rb_node *btrfs_find_inode(struct btrfs_root *root, const u64 objectid) +{ + struct rb_node *node = root->inode_tree.rb_node; + struct rb_node *prev = NULL; + struct btrfs_inode *entry; + + lockdep_assert_held(&root->inode_lock); + + while (node) { + prev = node; + entry = rb_entry(node, struct btrfs_inode, rb_node); + + if (objectid < btrfs_ino(entry)) + node = node->rb_left; + else if (objectid > btrfs_ino(entry)) + node = node->rb_right; + else + break; + } + + if (!node) { + while (prev) { + entry = rb_entry(prev, struct btrfs_inode, rb_node); + if (objectid <= btrfs_ino(entry)) + return prev; + prev = rb_next(prev); + } + } + + return node; +} + /* Delete all dentries for inodes belonging to the root */ static void btrfs_prune_dentries(struct btrfs_root *root) { From patchwork Thu Jul 21 13:50:05 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikolay Borisov X-Patchwork-Id: 12925216 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 00C90C43334 for ; Thu, 21 Jul 2022 13:50:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230391AbiGUNuj (ORCPT ); Thu, 21 Jul 2022 09:50:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58560 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230328AbiGUNu1 (ORCPT ); Thu, 21 Jul 2022 09:50:27 -0400 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 18D2FBCB6 for ; Thu, 21 Jul 2022 06:50:12 -0700 (PDT) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id D681834074; Thu, 21 Jul 2022 13:50:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1658411410; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ss71LQ3xFqT2YUtyv6/FJCYUCZ/Q5LUfqyKPUhmAEEo=; b=Pbg9UudEJYnQOmCyBs1m0xz2vGg8TDT8lad/dapPUcV+/ALMMBk2mFTEwDQhWeMxem3dxP zM0TDuV/PKKzOJ5xVHuKIrY91aBkhb8S352ArVLJznri3QypU6j9MjozHvTT8dRaL0lwHk WBCTJkeKsK3LJJF5LCo0p1X1AqOzUrU= Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id A518D13A1B; Thu, 21 Jul 2022 13:50:10 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id EIydJZJZ2WIPMgAAMHmgww (envelope-from ); Thu, 21 Jul 2022 13:50:10 +0000 From: Nikolay Borisov To: linux-btrfs@vger.kernel.org Cc: Nikolay Borisov Subject: [PATCH 2/3] btrfs: use btrfs_find_inode in btrfs_prune_dentries Date: Thu, 21 Jul 2022 16:50:05 +0300 Message-Id: <20220721135006.3345302-3-nborisov@suse.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220721135006.3345302-1-nborisov@suse.com> References: <20220721135006.3345302-1-nborisov@suse.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org Now that we have a standalone function which encapsulates the logic of searching the root's ino rb tree use that. It results in massive simplification of the code. Signed-off-by: Nikolay Borisov --- fs/btrfs/inode.c | 47 +++++++++++++++++------------------------------ 1 file changed, 17 insertions(+), 30 deletions(-) diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c index c11169ba28b2..06724925a3d3 100644 --- a/fs/btrfs/inode.c +++ b/fs/btrfs/inode.c @@ -4635,44 +4635,27 @@ struct rb_node *btrfs_find_inode(struct btrfs_root *root, const u64 objectid) static void btrfs_prune_dentries(struct btrfs_root *root) { struct btrfs_fs_info *fs_info = root->fs_info; - struct rb_node *node; - struct rb_node *prev; - struct btrfs_inode *entry; - struct inode *inode; + struct rb_node *node = NULL; u64 objectid = 0; if (!BTRFS_FS_ERROR(fs_info)) WARN_ON(btrfs_root_refs(&root->root_item) != 0); spin_lock(&root->inode_lock); -again: - node = root->inode_tree.rb_node; - prev = NULL; - while (node) { - prev = node; - entry = rb_entry(node, struct btrfs_inode, rb_node); + do { + struct btrfs_inode *entry; + struct inode *inode; - if (objectid < btrfs_ino(entry)) - node = node->rb_left; - else if (objectid > btrfs_ino(entry)) - node = node->rb_right; - else - break; - } - if (!node) { - while (prev) { - entry = rb_entry(prev, struct btrfs_inode, rb_node); - if (objectid <= btrfs_ino(entry)) { - node = prev; + if (!node) { + node = btrfs_find_inode(root, objectid); + if (!node) break; - } - prev = rb_next(prev); } - } - while (node) { + entry = rb_entry(node, struct btrfs_inode, rb_node); objectid = btrfs_ino(entry) + 1; inode = igrab(&entry->vfs_inode); + if (inode) { spin_unlock(&root->inode_lock); if (atomic_read(&inode->i_count) > 1) @@ -4684,14 +4667,18 @@ static void btrfs_prune_dentries(struct btrfs_root *root) iput(inode); cond_resched(); spin_lock(&root->inode_lock); - goto again; + node = NULL; + continue; } - if (cond_resched_lock(&root->inode_lock)) - goto again; + if (cond_resched_lock(&root->inode_lock)) { + node = NULL; + continue; + } node = rb_next(node); - } + } while(node); + spin_unlock(&root->inode_lock); } From patchwork Thu Jul 21 13:50:06 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikolay Borisov X-Patchwork-Id: 12925213 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E2B8EC43334 for ; Thu, 21 Jul 2022 13:50:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230210AbiGUNub (ORCPT ); Thu, 21 Jul 2022 09:50:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59672 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231185AbiGUNu0 (ORCPT ); Thu, 21 Jul 2022 09:50:26 -0400 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 18B48BC97 for ; Thu, 21 Jul 2022 06:50:12 -0700 (PDT) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 25C803412A; Thu, 21 Jul 2022 13:50:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1658411411; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=npPK0/AvXiNC6l3OGlILHinaKAJBg6kMrjnHHzGAYBk=; b=ePJxkQMaRsFtkPs3m9mgUyDPftcL3zA+zTGiaiWdi7pPy3DkB15usjg5pZwglFR7XeWcY7 h1vA6FHB6CPPr37paQIWekonrRvpZKqaWf+z5T6CGPEJcUjQXuwvoaDnacFjoyZdPEfSFk Ju/9dJzu9H7lLsbz+jjt7W3CQleP84g= Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id E8B4913A1B; Thu, 21 Jul 2022 13:50:10 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id oFcnNpJZ2WIPMgAAMHmgww (envelope-from ); Thu, 21 Jul 2022 13:50:10 +0000 From: Nikolay Borisov To: linux-btrfs@vger.kernel.org Cc: Nikolay Borisov Subject: [PATCH 3/3] btrfs: use btrfs_find_inode in find_next_inode Date: Thu, 21 Jul 2022 16:50:06 +0300 Message-Id: <20220721135006.3345302-4-nborisov@suse.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220721135006.3345302-1-nborisov@suse.com> References: <20220721135006.3345302-1-nborisov@suse.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org Start using btrfs_find_inode in find_next_inode, now that the common logic has been encapsulated in the former function. This simplifies the body of find_next_inode. Signed-off-by: Nikolay Borisov --- fs/btrfs/relocation.c | 54 +++++++++++++++---------------------------- 1 file changed, 19 insertions(+), 35 deletions(-) diff --git a/fs/btrfs/relocation.c b/fs/btrfs/relocation.c index a6dc827e75af..fdb99e2ce949 100644 --- a/fs/btrfs/relocation.c +++ b/fs/btrfs/relocation.c @@ -951,52 +951,36 @@ int btrfs_update_reloc_root(struct btrfs_trans_handle *trans, */ static struct inode *find_next_inode(struct btrfs_root *root, u64 objectid) { - struct rb_node *node; - struct rb_node *prev; - struct btrfs_inode *entry; - struct inode *inode; + struct rb_node *node = NULL; + struct inode *inode = NULL; spin_lock(&root->inode_lock); -again: - node = root->inode_tree.rb_node; - prev = NULL; - while (node) { - prev = node; - entry = rb_entry(node, struct btrfs_inode, rb_node); - if (objectid < btrfs_ino(entry)) - node = node->rb_left; - else if (objectid > btrfs_ino(entry)) - node = node->rb_right; - else - break; - } - if (!node) { - while (prev) { - entry = rb_entry(prev, struct btrfs_inode, rb_node); - if (objectid <= btrfs_ino(entry)) { - node = prev; + do { + struct btrfs_inode *entry; + + if (!node) { + node = btrfs_find_inode(root, objectid); + if (!node) break; - } - prev = rb_next(prev); } - } - while (node) { + entry = rb_entry(node, struct btrfs_inode, rb_node); + objectid = btrfs_ino(entry) + 1; inode = igrab(&entry->vfs_inode); - if (inode) { - spin_unlock(&root->inode_lock); - return inode; - } + if (inode) + break; - objectid = btrfs_ino(entry) + 1; - if (cond_resched_lock(&root->inode_lock)) - goto again; + if (cond_resched_lock(&root->inode_lock)) { + node = NULL; + continue; + } node = rb_next(node); - } + } while(node); + spin_unlock(&root->inode_lock); - return NULL; + return inode; } /*