From: Liu Bo <bo.liu@linux.alibaba.com>
To: <linux-btrfs@vger.kernel.org>
Subject: [PATCH v2 3/6] Btrfs: move get root of btrfs_search_slot to a helper
Date: Fri, 18 May 2018 11:00:21 +0800 [thread overview]
Message-ID: <1526612424-97061-4-git-send-email-bo.liu@linux.alibaba.com> (raw)
In-Reply-To: <1526612424-97061-1-git-send-email-bo.liu@linux.alibaba.com>
It's good to have a helper instead of having all get-root details
open-coded.
The new helper locks (if necessary) and sets root node of the path.
Also invert the checks to make the code flow easier to read.
There is no functional change in this commit.
Signed-off-by: Liu Bo <bo.liu@linux.alibaba.com>
---
fs/btrfs/ctree.c | 115 +++++++++++++++++++++++++++++++++----------------------
1 file changed, 70 insertions(+), 45 deletions(-)
diff --git a/fs/btrfs/ctree.c b/fs/btrfs/ctree.c
index a96d308c51b8..d12fc0474e21 100644
--- a/fs/btrfs/ctree.c
+++ b/fs/btrfs/ctree.c
@@ -2598,6 +2598,75 @@ int btrfs_find_item(struct btrfs_root *fs_root, struct btrfs_path *path,
return 0;
}
+static struct extent_buffer *btrfs_search_slot_get_root(struct btrfs_root *root,
+ struct btrfs_path *p,
+ int write_lock_level)
+{
+ struct btrfs_fs_info *fs_info = root->fs_info;
+ struct extent_buffer *b;
+ int root_lock;
+ int level = 0;
+
+ /*
+ * we try very hard to do read locks on the root
+ */
+ root_lock = BTRFS_READ_LOCK;
+
+ if (p->search_commit_root) {
+ /*
+ * the commit roots are read only so we always do read locks
+ */
+ if (p->need_commit_sem)
+ down_read(&fs_info->commit_root_sem);
+ b = root->commit_root;
+ extent_buffer_get(b);
+ level = btrfs_header_level(b);
+ if (p->need_commit_sem)
+ up_read(&fs_info->commit_root_sem);
+ if (!p->skip_locking)
+ btrfs_tree_read_lock(b);
+
+ goto out;
+ }
+
+ if (p->skip_locking) {
+ b = btrfs_root_node(root);
+ level = btrfs_header_level(b);
+ goto out;
+ }
+
+ /*
+ * we don't know the level of the root node until we actually
+ * have it read locked
+ */
+ b = btrfs_read_lock_root_node(root);
+ level = btrfs_header_level(b);
+ if (level > write_lock_level)
+ goto out;
+
+ /*
+ * whoops, must trade for write lock
+ */
+ btrfs_tree_read_unlock(b);
+ free_extent_buffer(b);
+ b = btrfs_lock_root_node(root);
+ root_lock = BTRFS_WRITE_LOCK;
+ /*
+ * the level might have changed, check again
+ */
+ level = btrfs_header_level(b);
+
+out:
+ p->nodes[level] = b;
+ if (!p->skip_locking)
+ p->locks[level] = root_lock;
+ /*
+ * Callers are responsible for drop b's refs.
+ */
+ return b;
+}
+
+
/*
* btrfs_search_slot - look for a key in a tree and perform necessary
* modifications to preserve tree invariants.
@@ -2634,7 +2703,6 @@ int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root *root,
int err;
int level;
int lowest_unlock = 1;
- int root_lock;
/* everything at write_lock_level or lower must be write locked */
int write_lock_level = 0;
u8 lowest_level = 0;
@@ -2672,50 +2740,7 @@ int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root *root,
again:
prev_cmp = -1;
- /*
- * we try very hard to do read locks on the root
- */
- root_lock = BTRFS_READ_LOCK;
- level = 0;
- if (p->search_commit_root) {
- /*
- * the commit roots are read only
- * so we always do read locks
- */
- if (p->need_commit_sem)
- down_read(&fs_info->commit_root_sem);
- b = root->commit_root;
- extent_buffer_get(b);
- level = btrfs_header_level(b);
- if (p->need_commit_sem)
- up_read(&fs_info->commit_root_sem);
- if (!p->skip_locking)
- btrfs_tree_read_lock(b);
- } else {
- if (p->skip_locking) {
- b = btrfs_root_node(root);
- level = btrfs_header_level(b);
- } else {
- /* we don't know the level of the root node
- * until we actually have it read locked
- */
- b = btrfs_read_lock_root_node(root);
- level = btrfs_header_level(b);
- if (level <= write_lock_level) {
- /* whoops, must trade for write lock */
- btrfs_tree_read_unlock(b);
- free_extent_buffer(b);
- b = btrfs_lock_root_node(root);
- root_lock = BTRFS_WRITE_LOCK;
-
- /* the level might have changed, check again */
- level = btrfs_header_level(b);
- }
- }
- }
- p->nodes[level] = b;
- if (!p->skip_locking)
- p->locks[level] = root_lock;
+ b = btrfs_search_slot_get_root(root, p, write_lock_level);
while (b) {
level = btrfs_header_level(b);
--
1.8.3.1
next prev parent reply other threads:[~2018-05-18 3:00 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-05-18 3:00 [PATCH v2 0/6] btrfs_search_slot cleanups Liu Bo
2018-05-18 3:00 ` [PATCH v2 1/6] Btrfs: remove superfluous free_extent_buffer Liu Bo
2018-05-18 5:15 ` Qu Wenruo
2018-05-18 3:00 ` [PATCH v2 2/6] Btrfs: use more straightforward extent_buffer_uptodate Liu Bo
2018-05-18 5:17 ` Qu Wenruo
2018-05-18 16:00 ` David Sterba
2018-05-18 3:00 ` Liu Bo [this message]
2018-05-18 5:20 ` [PATCH v2 3/6] Btrfs: move get root of btrfs_search_slot to a helper Qu Wenruo
2018-05-18 3:00 ` [PATCH v2 4/6] Btrfs: remove unused check of skip_locking Liu Bo
2018-05-18 5:27 ` Qu Wenruo
2018-05-28 14:21 ` David Sterba
2018-05-28 15:33 ` Nikolay Borisov
2018-05-29 13:27 ` [PATCH v3] " Liu Bo
2018-05-29 14:49 ` David Sterba
2018-05-18 3:00 ` [PATCH v2 5/6] Btrfs: grab write lock directly if write_lock_level is the max level Liu Bo
2018-05-28 14:32 ` David Sterba
2018-05-18 3:00 ` [PATCH v2 6/6] Btrfs: remove always true check in unlock_up Liu Bo
2018-05-18 5:31 ` Qu Wenruo
2018-05-22 11:05 ` [PATCH v2 0/6] btrfs_search_slot cleanups Su Yue
2018-05-22 12:02 ` David Sterba
2018-05-22 12:35 ` Nikolay Borisov
2018-05-23 2:16 ` Su Yue
2018-05-23 12:34 ` David Sterba
2018-05-23 14:11 ` Liu Bo
2018-05-28 14:40 ` David Sterba
2018-05-28 16:17 ` David Sterba
2018-05-29 8:27 ` Liu Bo
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1526612424-97061-4-git-send-email-bo.liu@linux.alibaba.com \
--to=bo.liu@linux.alibaba.com \
--cc=linux-btrfs@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).