Skip to content

Commit 22a0ae1

Browse files
fdmananakdave
authored andcommitted
btrfs: move btrfs_destroy_delayed_refs() to delayed-ref.c
It's better suited at delayed-ref.c since it's about delayed refs and contains logic to iterate over them (using the red black tree, doing all the locking, freeing, etc), so move it from disk-io.c, which is pretty big, into delayed-ref.c, hiding implementation details of how delayed refs are tracked and managed. This also facilitates the next patches in the series. This change moves the code between files but also does the following simple cleanups: 1) Rename the 'cache' variable to 'bg', since it's a block group (the 'cache' logic comes from old days where the block group structure was named 'btrfs_block_group_cache'); 2) Move the 'ref' variable declaration to the scope of the inner while loop, since it's not used outside that loop. Reviewed-by: Boris Burkov <boris@bur.io> Reviewed-by: Qu Wenruo <wqu@suse.com> Signed-off-by: Filipe Manana <fdmanana@suse.com> Reviewed-by: David Sterba <dsterba@suse.com> Signed-off-by: David Sterba <dsterba@suse.com>
1 parent 00f5296 commit 22a0ae1

File tree

3 files changed

+83
-80
lines changed

3 files changed

+83
-80
lines changed

fs/btrfs/delayed-ref.c

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "messages.h"
1010
#include "ctree.h"
1111
#include "delayed-ref.h"
12+
#include "extent-tree.h"
1213
#include "transaction.h"
1314
#include "qgroup.h"
1415
#include "space-info.h"
@@ -1238,6 +1239,86 @@ bool btrfs_find_delayed_tree_ref(struct btrfs_delayed_ref_head *head,
12381239
return found;
12391240
}
12401241

1242+
void btrfs_destroy_delayed_refs(struct btrfs_transaction *trans,
1243+
struct btrfs_fs_info *fs_info)
1244+
{
1245+
struct rb_node *node;
1246+
struct btrfs_delayed_ref_root *delayed_refs = &trans->delayed_refs;
1247+
1248+
spin_lock(&delayed_refs->lock);
1249+
while ((node = rb_first_cached(&delayed_refs->href_root)) != NULL) {
1250+
struct btrfs_delayed_ref_head *head;
1251+
struct rb_node *n;
1252+
bool pin_bytes = false;
1253+
1254+
head = rb_entry(node, struct btrfs_delayed_ref_head,
1255+
href_node);
1256+
if (btrfs_delayed_ref_lock(delayed_refs, head))
1257+
continue;
1258+
1259+
spin_lock(&head->lock);
1260+
while ((n = rb_first_cached(&head->ref_tree)) != NULL) {
1261+
struct btrfs_delayed_ref_node *ref;
1262+
1263+
ref = rb_entry(n, struct btrfs_delayed_ref_node, ref_node);
1264+
rb_erase_cached(&ref->ref_node, &head->ref_tree);
1265+
RB_CLEAR_NODE(&ref->ref_node);
1266+
if (!list_empty(&ref->add_list))
1267+
list_del(&ref->add_list);
1268+
atomic_dec(&delayed_refs->num_entries);
1269+
btrfs_put_delayed_ref(ref);
1270+
btrfs_delayed_refs_rsv_release(fs_info, 1, 0);
1271+
}
1272+
if (head->must_insert_reserved)
1273+
pin_bytes = true;
1274+
btrfs_free_delayed_extent_op(head->extent_op);
1275+
btrfs_delete_ref_head(delayed_refs, head);
1276+
spin_unlock(&head->lock);
1277+
spin_unlock(&delayed_refs->lock);
1278+
mutex_unlock(&head->mutex);
1279+
1280+
if (pin_bytes) {
1281+
struct btrfs_block_group *bg;
1282+
1283+
bg = btrfs_lookup_block_group(fs_info, head->bytenr);
1284+
if (WARN_ON_ONCE(bg == NULL)) {
1285+
/*
1286+
* Unexpected and there's nothing we can do here
1287+
* because we are in a transaction abort path,
1288+
* so any errors can only be ignored or reported
1289+
* while attempting to cleanup all resources.
1290+
*/
1291+
btrfs_err(fs_info,
1292+
"block group for delayed ref at %llu was not found while destroying ref head",
1293+
head->bytenr);
1294+
} else {
1295+
spin_lock(&bg->space_info->lock);
1296+
spin_lock(&bg->lock);
1297+
bg->pinned += head->num_bytes;
1298+
btrfs_space_info_update_bytes_pinned(fs_info,
1299+
bg->space_info,
1300+
head->num_bytes);
1301+
bg->reserved -= head->num_bytes;
1302+
bg->space_info->bytes_reserved -= head->num_bytes;
1303+
spin_unlock(&bg->lock);
1304+
spin_unlock(&bg->space_info->lock);
1305+
1306+
btrfs_put_block_group(bg);
1307+
}
1308+
1309+
btrfs_error_unpin_extent_range(fs_info, head->bytenr,
1310+
head->bytenr + head->num_bytes - 1);
1311+
}
1312+
btrfs_cleanup_ref_head_accounting(fs_info, delayed_refs, head);
1313+
btrfs_put_delayed_ref_head(head);
1314+
cond_resched();
1315+
spin_lock(&delayed_refs->lock);
1316+
}
1317+
btrfs_qgroup_destroy_extent_records(trans);
1318+
1319+
spin_unlock(&delayed_refs->lock);
1320+
}
1321+
12411322
void __cold btrfs_delayed_ref_exit(void)
12421323
{
12431324
kmem_cache_destroy(btrfs_delayed_ref_head_cachep);

fs/btrfs/delayed-ref.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,8 @@ int btrfs_delayed_refs_rsv_refill(struct btrfs_fs_info *fs_info,
399399
bool btrfs_check_space_for_delayed_refs(struct btrfs_fs_info *fs_info);
400400
bool btrfs_find_delayed_tree_ref(struct btrfs_delayed_ref_head *head,
401401
u64 root, u64 parent);
402+
void btrfs_destroy_delayed_refs(struct btrfs_transaction *trans,
403+
struct btrfs_fs_info *fs_info);
402404

403405
static inline u64 btrfs_delayed_ref_owner(struct btrfs_delayed_ref_node *node)
404406
{

fs/btrfs/disk-io.c

Lines changed: 0 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -4529,86 +4529,6 @@ static void btrfs_destroy_all_ordered_extents(struct btrfs_fs_info *fs_info)
45294529
btrfs_wait_ordered_roots(fs_info, U64_MAX, NULL);
45304530
}
45314531

4532-
static void btrfs_destroy_delayed_refs(struct btrfs_transaction *trans,
4533-
struct btrfs_fs_info *fs_info)
4534-
{
4535-
struct rb_node *node;
4536-
struct btrfs_delayed_ref_root *delayed_refs = &trans->delayed_refs;
4537-
struct btrfs_delayed_ref_node *ref;
4538-
4539-
spin_lock(&delayed_refs->lock);
4540-
while ((node = rb_first_cached(&delayed_refs->href_root)) != NULL) {
4541-
struct btrfs_delayed_ref_head *head;
4542-
struct rb_node *n;
4543-
bool pin_bytes = false;
4544-
4545-
head = rb_entry(node, struct btrfs_delayed_ref_head,
4546-
href_node);
4547-
if (btrfs_delayed_ref_lock(delayed_refs, head))
4548-
continue;
4549-
4550-
spin_lock(&head->lock);
4551-
while ((n = rb_first_cached(&head->ref_tree)) != NULL) {
4552-
ref = rb_entry(n, struct btrfs_delayed_ref_node,
4553-
ref_node);
4554-
rb_erase_cached(&ref->ref_node, &head->ref_tree);
4555-
RB_CLEAR_NODE(&ref->ref_node);
4556-
if (!list_empty(&ref->add_list))
4557-
list_del(&ref->add_list);
4558-
atomic_dec(&delayed_refs->num_entries);
4559-
btrfs_put_delayed_ref(ref);
4560-
btrfs_delayed_refs_rsv_release(fs_info, 1, 0);
4561-
}
4562-
if (head->must_insert_reserved)
4563-
pin_bytes = true;
4564-
btrfs_free_delayed_extent_op(head->extent_op);
4565-
btrfs_delete_ref_head(delayed_refs, head);
4566-
spin_unlock(&head->lock);
4567-
spin_unlock(&delayed_refs->lock);
4568-
mutex_unlock(&head->mutex);
4569-
4570-
if (pin_bytes) {
4571-
struct btrfs_block_group *cache;
4572-
4573-
cache = btrfs_lookup_block_group(fs_info, head->bytenr);
4574-
if (WARN_ON_ONCE(cache == NULL)) {
4575-
/*
4576-
* Unexpected and there's nothing we can do here
4577-
* because we are in a transaction abort path,
4578-
* so any errors can only be ignored or reported
4579-
* while attempting to cleanup all resources.
4580-
*/
4581-
btrfs_err(fs_info,
4582-
"block group for delayed ref at %llu was not found while destroying ref head",
4583-
head->bytenr);
4584-
} else {
4585-
spin_lock(&cache->space_info->lock);
4586-
spin_lock(&cache->lock);
4587-
cache->pinned += head->num_bytes;
4588-
btrfs_space_info_update_bytes_pinned(fs_info,
4589-
cache->space_info,
4590-
head->num_bytes);
4591-
cache->reserved -= head->num_bytes;
4592-
cache->space_info->bytes_reserved -= head->num_bytes;
4593-
spin_unlock(&cache->lock);
4594-
spin_unlock(&cache->space_info->lock);
4595-
4596-
btrfs_put_block_group(cache);
4597-
}
4598-
4599-
btrfs_error_unpin_extent_range(fs_info, head->bytenr,
4600-
head->bytenr + head->num_bytes - 1);
4601-
}
4602-
btrfs_cleanup_ref_head_accounting(fs_info, delayed_refs, head);
4603-
btrfs_put_delayed_ref_head(head);
4604-
cond_resched();
4605-
spin_lock(&delayed_refs->lock);
4606-
}
4607-
btrfs_qgroup_destroy_extent_records(trans);
4608-
4609-
spin_unlock(&delayed_refs->lock);
4610-
}
4611-
46124532
static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root)
46134533
{
46144534
struct btrfs_inode *btrfs_inode;

0 commit comments

Comments
 (0)