btrfs: add and use helper to get lowest sequence number for the tree mod log
authorFilipe Manana <fdmanana@suse.com>
Thu, 11 Mar 2021 14:31:12 +0000 (14:31 +0000)
committerDavid Sterba <dsterba@suse.com>
Mon, 19 Apr 2021 15:25:17 +0000 (17:25 +0200)
There are two places outside the tree mod log module that extract the
lowest sequence number of the tree mod log. These places end up
duplicating code and open coding the logic and internal implementation
details of the tree mod log. So add a helper to the tree mod log module
and header that returns the lowest sequence number or 0 if there aren't
any tree mod log users at the moment.

Signed-off-by: Filipe Manana <fdmanana@suse.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/delayed-ref.c
fs/btrfs/tree-mod-log.c
fs/btrfs/tree-mod-log.h

index d87472a68bf4f0760b3bd53a8acebb0b83b9ce97..1c46d9b497bce2f072056aca32caca13cf7345cf 100644 (file)
@@ -495,16 +495,7 @@ void btrfs_merge_delayed_refs(struct btrfs_trans_handle *trans,
        if (head->is_data)
                return;
 
-       read_lock(&fs_info->tree_mod_log_lock);
-       if (!list_empty(&fs_info->tree_mod_seq_list)) {
-               struct btrfs_seq_list *elem;
-
-               elem = list_first_entry(&fs_info->tree_mod_seq_list,
-                                       struct btrfs_seq_list, list);
-               seq = elem->seq;
-       }
-       read_unlock(&fs_info->tree_mod_log_lock);
-
+       seq = btrfs_tree_mod_log_lowest_seq(fs_info);
 again:
        for (node = rb_first_cached(&head->ref_tree); node;
             node = rb_next(node)) {
@@ -518,23 +509,17 @@ again:
 
 int btrfs_check_delayed_seq(struct btrfs_fs_info *fs_info, u64 seq)
 {
-       struct btrfs_seq_list *elem;
        int ret = 0;
-
-       read_lock(&fs_info->tree_mod_log_lock);
-       if (!list_empty(&fs_info->tree_mod_seq_list)) {
-               elem = list_first_entry(&fs_info->tree_mod_seq_list,
-                                       struct btrfs_seq_list, list);
-               if (seq >= elem->seq) {
-                       btrfs_debug(fs_info,
-                               "holding back delayed_ref %#x.%x, lowest is %#x.%x",
-                               (u32)(seq >> 32), (u32)seq,
-                               (u32)(elem->seq >> 32), (u32)elem->seq);
-                       ret = 1;
-               }
+       u64 min_seq = btrfs_tree_mod_log_lowest_seq(fs_info);
+
+       if (min_seq != 0 && seq >= min_seq) {
+               btrfs_debug(fs_info,
+                           "holding back delayed_ref %#x.%x, lowest is %#x.%x",
+                           (u32)(seq >> 32), (u32)seq,
+                           (u32)(min_seq >> 32), (u32)min_seq);
+               ret = 1;
        }
 
-       read_unlock(&fs_info->tree_mod_log_lock);
        return ret;
 }
 
index b10170e691d17f95731d1c8a1b4d98bfc6b0a7d6..1e52584f3fa561b5f1a99b811ea566ae5a775732 100644 (file)
@@ -884,3 +884,26 @@ int btrfs_old_root_level(struct btrfs_root *root, u64 time_seq)
        return level;
 }
 
+/*
+ * Return the lowest sequence number in the tree modification log.
+ *
+ * Return the sequence number of the oldest tree modification log user, which
+ * corresponds to the lowest sequence number of all existing users. If there are
+ * no users it returns 0.
+ */
+u64 btrfs_tree_mod_log_lowest_seq(struct btrfs_fs_info *fs_info)
+{
+       u64 ret = 0;
+
+       read_lock(&fs_info->tree_mod_log_lock);
+       if (!list_empty(&fs_info->tree_mod_seq_list)) {
+               struct btrfs_seq_list *elem;
+
+               elem = list_first_entry(&fs_info->tree_mod_seq_list,
+                                       struct btrfs_seq_list, list);
+               ret = elem->seq;
+       }
+       read_unlock(&fs_info->tree_mod_log_lock);
+
+       return ret;
+}
index e74eab720a28346622026acfbf9c097cc54cfae4..12605d19621b6e5cabda66a2fe299f9c75f1daba 100644 (file)
@@ -48,5 +48,6 @@ int btrfs_tree_mod_log_eb_copy(struct extent_buffer *dst,
 int btrfs_tree_mod_log_insert_move(struct extent_buffer *eb,
                                   int dst_slot, int src_slot,
                                   int nr_items);
+u64 btrfs_tree_mod_log_lowest_seq(struct btrfs_fs_info *fs_info);
 
 #endif