Skip to content

Commit 1c11b63

Browse files
jeffmahoneykdave
authored andcommitted
btrfs: replace pending/pinned chunks lists with io tree
The pending chunks list contains chunks that are allocated in the current transaction but haven't been created yet. The pinned chunks list contains chunks that are being released in the current transaction. Both describe chunks that are not reflected on disk as in use but are unavailable just the same. The pending chunks list is anchored by the transaction handle, which means that we need to hold a reference to a transaction when working with the list. The way we use them is by iterating over both lists to perform comparisons on the stripes they describe for each device. This is backwards and requires that we keep a transaction handle open while we're trimming. This patchset adds an extent_io_tree to btrfs_device that maintains the allocation state of the device. Extents are set dirty when chunks are first allocated -- when the extent maps are added to the mapping tree. They're cleared when last removed -- when the extent maps are removed from the mapping tree. This matches the lifespan of the pending and pinned chunks list and allows us to do trims on unallocated space safely without pinning the transaction for what may be a lengthy operation. We can also use this io tree to mark which chunks have already been trimmed so we don't repeat the operation. Signed-off-by: Jeff Mahoney <jeffm@suse.com> Signed-off-by: Nikolay Borisov <nborisov@suse.com> Signed-off-by: David Sterba <dsterba@suse.com>
1 parent 68c94e5 commit 1c11b63

9 files changed

Lines changed: 63 additions & 119 deletions

File tree

‎fs/btrfs/ctree.h‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,12 +1149,6 @@ struct btrfs_fs_info {
11491149
struct mutex unused_bg_unpin_mutex;
11501150
struct mutex delete_unused_bgs_mutex;
11511151

1152-
/*
1153-
* Chunks that can't be freed yet (under a trim/discard operation)
1154-
* and will be latter freed. Protected by fs_info->chunk_mutex.
1155-
*/
1156-
struct list_head pinned_chunks;
1157-
11581152
/* Cached block sizes */
11591153
u32 nodesize;
11601154
u32 sectorsize;

‎fs/btrfs/disk-io.c‎

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2774,8 +2774,6 @@ int open_ctree(struct super_block *sb,
27742774
init_waitqueue_head(&fs_info->async_submit_wait);
27752775
init_waitqueue_head(&fs_info->delayed_iputs_wait);
27762776

2777-
INIT_LIST_HEAD(&fs_info->pinned_chunks);
2778-
27792777
/* Usable values until the real ones are cached from the superblock */
27802778
fs_info->nodesize = 4096;
27812779
fs_info->sectorsize = 4096;
@@ -4050,15 +4048,6 @@ void close_ctree(struct btrfs_fs_info *fs_info)
40504048

40514049
btrfs_free_stripe_hash_table(fs_info);
40524050
btrfs_free_ref_cache(fs_info);
4053-
4054-
while (!list_empty(&fs_info->pinned_chunks)) {
4055-
struct extent_map *em;
4056-
4057-
em = list_first_entry(&fs_info->pinned_chunks,
4058-
struct extent_map, list);
4059-
list_del_init(&em->list);
4060-
free_extent_map(em);
4061-
}
40624051
}
40634052

40644053
int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid,

‎fs/btrfs/extent-tree.c‎

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10946,10 +10946,6 @@ int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
1094610946
memcpy(&key, &block_group->key, sizeof(key));
1094710947

1094810948
mutex_lock(&fs_info->chunk_mutex);
10949-
if (!list_empty(&em->list)) {
10950-
/* We're in the transaction->pending_chunks list. */
10951-
free_extent_map(em);
10952-
}
1095310949
spin_lock(&block_group->lock);
1095410950
block_group->removed = 1;
1095510951
/*
@@ -10976,37 +10972,13 @@ int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
1097610972
* the transaction commit has completed.
1097710973
*/
1097810974
remove_em = (atomic_read(&block_group->trimming) == 0);
10979-
/*
10980-
* Make sure a trimmer task always sees the em in the pinned_chunks list
10981-
* if it sees block_group->removed == 1 (needs to lock block_group->lock
10982-
* before checking block_group->removed).
10983-
*/
10984-
if (!remove_em) {
10985-
/*
10986-
* Our em might be in trans->transaction->pending_chunks which
10987-
* is protected by fs_info->chunk_mutex ([lock|unlock]_chunks),
10988-
* and so is the fs_info->pinned_chunks list.
10989-
*
10990-
* So at this point we must be holding the chunk_mutex to avoid
10991-
* any races with chunk allocation (more specifically at
10992-
* volumes.c:contains_pending_extent()), to ensure it always
10993-
* sees the em, either in the pending_chunks list or in the
10994-
* pinned_chunks list.
10995-
*/
10996-
list_move_tail(&em->list, &fs_info->pinned_chunks);
10997-
}
1099810975
spin_unlock(&block_group->lock);
1099910976

1100010977
if (remove_em) {
1100110978
struct extent_map_tree *em_tree;
1100210979

1100310980
em_tree = &fs_info->mapping_tree.map_tree;
1100410981
write_lock(&em_tree->lock);
11005-
/*
11006-
* The em might be in the pending_chunks list, so make sure the
11007-
* chunk mutex is locked, since remove_extent_mapping() will
11008-
* delete us from that list.
11009-
*/
1101010982
remove_extent_mapping(em_tree, em);
1101110983
write_unlock(&em_tree->lock);
1101210984
/* once for the tree */

‎fs/btrfs/extent_map.c‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <linux/slab.h>
55
#include <linux/spinlock.h>
66
#include "ctree.h"
7+
#include "volumes.h"
78
#include "extent_map.h"
89
#include "compression.h"
910

@@ -337,6 +338,37 @@ static inline void setup_extent_mapping(struct extent_map_tree *tree,
337338
try_merge_map(tree, em);
338339
}
339340

341+
static void extent_map_device_set_bits(struct extent_map *em, unsigned bits)
342+
{
343+
struct map_lookup *map = em->map_lookup;
344+
u64 stripe_size = em->orig_block_len;
345+
int i;
346+
347+
for (i = 0; i < map->num_stripes; i++) {
348+
struct btrfs_bio_stripe *stripe = &map->stripes[i];
349+
struct btrfs_device *device = stripe->dev;
350+
351+
set_extent_bits_nowait(&device->alloc_state, stripe->physical,
352+
stripe->physical + stripe_size - 1, bits);
353+
}
354+
}
355+
356+
static void extent_map_device_clear_bits(struct extent_map *em, unsigned bits)
357+
{
358+
struct map_lookup *map = em->map_lookup;
359+
u64 stripe_size = em->orig_block_len;
360+
int i;
361+
362+
for (i = 0; i < map->num_stripes; i++) {
363+
struct btrfs_bio_stripe *stripe = &map->stripes[i];
364+
struct btrfs_device *device = stripe->dev;
365+
366+
__clear_extent_bit(&device->alloc_state, stripe->physical,
367+
stripe->physical + stripe_size - 1, bits,
368+
0, 0, NULL, GFP_NOWAIT, NULL);
369+
}
370+
}
371+
340372
/**
341373
* add_extent_mapping - add new extent map to the extent tree
342374
* @tree: tree to insert new map in
@@ -357,6 +389,8 @@ int add_extent_mapping(struct extent_map_tree *tree,
357389
goto out;
358390

359391
setup_extent_mapping(tree, em, modified);
392+
if (test_bit(EXTENT_FLAG_FS_MAPPING, &em->flags))
393+
extent_map_device_set_bits(em, CHUNK_ALLOCATED);
360394
out:
361395
return ret;
362396
}
@@ -438,6 +472,8 @@ void remove_extent_mapping(struct extent_map_tree *tree, struct extent_map *em)
438472
rb_erase_cached(&em->rb_node, &tree->map);
439473
if (!test_bit(EXTENT_FLAG_LOGGING, &em->flags))
440474
list_del_init(&em->list);
475+
if (test_bit(EXTENT_FLAG_FS_MAPPING, &em->flags))
476+
extent_map_device_clear_bits(em, CHUNK_ALLOCATED);
441477
RB_CLEAR_NODE(&em->rb_node);
442478
}
443479

‎fs/btrfs/free-space-cache.c‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3366,10 +3366,6 @@ void btrfs_put_block_group_trimming(struct btrfs_block_group_cache *block_group)
33663366
em = lookup_extent_mapping(em_tree, block_group->key.objectid,
33673367
1);
33683368
BUG_ON(!em); /* logic error, can't happen */
3369-
/*
3370-
* remove_extent_mapping() will delete us from the pinned_chunks
3371-
* list, which is protected by the chunk mutex.
3372-
*/
33733369
remove_extent_mapping(em_tree, em);
33743370
write_unlock(&em_tree->lock);
33753371
mutex_unlock(&fs_info->chunk_mutex);

‎fs/btrfs/transaction.c‎

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,6 @@ void btrfs_put_transaction(struct btrfs_transaction *transaction)
5050
btrfs_err(transaction->fs_info,
5151
"pending csums is %llu",
5252
transaction->delayed_refs.pending_csums);
53-
while (!list_empty(&transaction->pending_chunks)) {
54-
struct extent_map *em;
55-
56-
em = list_first_entry(&transaction->pending_chunks,
57-
struct extent_map, list);
58-
list_del_init(&em->list);
59-
free_extent_map(em);
60-
}
6153
/*
6254
* If any block groups are found in ->deleted_bgs then it's
6355
* because the transaction was aborted and a commit did not
@@ -235,7 +227,6 @@ static noinline int join_transaction(struct btrfs_fs_info *fs_info,
235227
spin_lock_init(&cur_trans->delayed_refs.lock);
236228

237229
INIT_LIST_HEAD(&cur_trans->pending_snapshots);
238-
INIT_LIST_HEAD(&cur_trans->pending_chunks);
239230
INIT_LIST_HEAD(&cur_trans->dev_update_list);
240231
INIT_LIST_HEAD(&cur_trans->switch_commits);
241232
INIT_LIST_HEAD(&cur_trans->dirty_bgs);

‎fs/btrfs/transaction.h‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ struct btrfs_transaction {
5151
wait_queue_head_t writer_wait;
5252
wait_queue_head_t commit_wait;
5353
struct list_head pending_snapshots;
54-
struct list_head pending_chunks;
5554
struct list_head dev_update_list;
5655
struct list_head switch_commits;
5756
struct list_head dirty_bgs;

‎fs/btrfs/volumes.c‎

Lines changed: 25 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ void btrfs_free_device(struct btrfs_device *device)
336336
{
337337
WARN_ON(!list_empty(&device->post_commit_list));
338338
rcu_string_free(device->name);
339+
extent_io_tree_release(&device->alloc_state);
339340
bio_put(device->flush_bio);
340341
kfree(device);
341342
}
@@ -412,6 +413,7 @@ static struct btrfs_device *__alloc_device(void)
412413
btrfs_device_data_ordered_init(dev);
413414
INIT_RADIX_TREE(&dev->reada_zones, GFP_NOFS & ~__GFP_DIRECT_RECLAIM);
414415
INIT_RADIX_TREE(&dev->reada_extents, GFP_NOFS & ~__GFP_DIRECT_RECLAIM);
416+
extent_io_tree_init(NULL, &dev->alloc_state, 0, NULL);
415417

416418
return dev;
417419
}
@@ -1499,58 +1501,30 @@ struct btrfs_device *btrfs_scan_one_device(const char *path, fmode_t flags,
14991501
return device;
15001502
}
15011503

1502-
static int contains_pending_extent(struct btrfs_transaction *transaction,
1503-
struct btrfs_device *device,
1504-
u64 *start, u64 len)
1504+
/*
1505+
* Try to find a chunk that intersects [start, start + len] range and when one
1506+
* such is found, record the end of it in *start
1507+
*/
1508+
#define in_range(b, first, len) ((b) >= (first) && (b) < (first) + (len))
1509+
static bool contains_pending_extent(struct btrfs_device *device, u64 *start,
1510+
u64 len)
15051511
{
1506-
struct btrfs_fs_info *fs_info = device->fs_info;
1507-
struct extent_map *em;
1508-
struct list_head *search_list = &fs_info->pinned_chunks;
1509-
int ret = 0;
1510-
u64 physical_start = *start;
1512+
u64 physical_start, physical_end;
15111513

1512-
if (transaction)
1513-
search_list = &transaction->pending_chunks;
1514-
again:
1515-
list_for_each_entry(em, search_list, list) {
1516-
struct map_lookup *map;
1517-
int i;
1514+
lockdep_assert_held(&device->fs_info->chunk_mutex);
15181515

1519-
map = em->map_lookup;
1520-
for (i = 0; i < map->num_stripes; i++) {
1521-
u64 end;
1516+
if (!find_first_extent_bit(&device->alloc_state, *start,
1517+
&physical_start, &physical_end,
1518+
CHUNK_ALLOCATED, NULL)) {
15221519

1523-
if (map->stripes[i].dev != device)
1524-
continue;
1525-
if (map->stripes[i].physical >= physical_start + len ||
1526-
map->stripes[i].physical + em->orig_block_len <=
1527-
physical_start)
1528-
continue;
1529-
/*
1530-
* Make sure that while processing the pinned list we do
1531-
* not override our *start with a lower value, because
1532-
* we can have pinned chunks that fall within this
1533-
* device hole and that have lower physical addresses
1534-
* than the pending chunks we processed before. If we
1535-
* do not take this special care we can end up getting
1536-
* 2 pending chunks that start at the same physical
1537-
* device offsets because the end offset of a pinned
1538-
* chunk can be equal to the start offset of some
1539-
* pending chunk.
1540-
*/
1541-
end = map->stripes[i].physical + em->orig_block_len;
1542-
if (end > *start) {
1543-
*start = end;
1544-
ret = 1;
1545-
}
1520+
if (in_range(physical_start, *start, len) ||
1521+
in_range(*start, physical_start,
1522+
physical_end - physical_start)) {
1523+
*start = physical_end + 1;
1524+
return true;
15461525
}
15471526
}
1548-
if (search_list != &fs_info->pinned_chunks) {
1549-
search_list = &fs_info->pinned_chunks;
1550-
goto again;
1551-
}
1552-
1553-
return ret;
1527+
return false;
15541528
}
15551529

15561530

@@ -1661,15 +1635,12 @@ int find_free_dev_extent_start(struct btrfs_transaction *transaction,
16611635
* Have to check before we set max_hole_start, otherwise
16621636
* we could end up sending back this offset anyway.
16631637
*/
1664-
if (contains_pending_extent(transaction, device,
1665-
&search_start,
1638+
if (contains_pending_extent(device, &search_start,
16661639
hole_size)) {
1667-
if (key.offset >= search_start) {
1640+
if (key.offset >= search_start)
16681641
hole_size = key.offset - search_start;
1669-
} else {
1670-
WARN_ON_ONCE(1);
1642+
else
16711643
hole_size = 0;
1672-
}
16731644
}
16741645

16751646
if (hole_size > max_hole_size) {
@@ -1710,8 +1681,7 @@ int find_free_dev_extent_start(struct btrfs_transaction *transaction,
17101681
if (search_end > search_start) {
17111682
hole_size = search_end - search_start;
17121683

1713-
if (contains_pending_extent(transaction, device, &search_start,
1714-
hole_size)) {
1684+
if (contains_pending_extent(device, &search_start, hole_size)) {
17151685
btrfs_release_path(path);
17161686
goto again;
17171687
}
@@ -4756,7 +4726,7 @@ int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
47564726
* in-memory chunks are synced to disk so that the loop below sees them
47574727
* and relocates them accordingly.
47584728
*/
4759-
if (contains_pending_extent(trans->transaction, device, &start, diff)) {
4729+
if (contains_pending_extent(device, &start, diff)) {
47604730
mutex_unlock(&fs_info->chunk_mutex);
47614731
ret = btrfs_commit_transaction(trans);
47624732
if (ret)
@@ -5189,9 +5159,6 @@ static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
51895159
free_extent_map(em);
51905160
goto error;
51915161
}
5192-
5193-
list_add_tail(&em->list, &trans->transaction->pending_chunks);
5194-
refcount_inc(&em->refs);
51955162
write_unlock(&em_tree->lock);
51965163

51975164
ret = btrfs_make_block_group(trans, 0, type, start, chunk_size);
@@ -5224,8 +5191,6 @@ static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
52245191
free_extent_map(em);
52255192
/* One for the tree reference */
52265193
free_extent_map(em);
5227-
/* One for the pending_chunks list reference */
5228-
free_extent_map(em);
52295194
error:
52305195
kfree(devices_info);
52315196
return ret;

‎fs/btrfs/volumes.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ struct btrfs_device {
133133
/* Counter to record the change of device stats */
134134
atomic_t dev_stats_ccnt;
135135
atomic_t dev_stat_values[BTRFS_DEV_STAT_VALUES_MAX];
136+
137+
struct extent_io_tree alloc_state;
136138
};
137139

138140
/*

0 commit comments

Comments
 (0)