block-group.c (534cf531ccf6331b6e63a054f2ceb8e1ccff1ec9) block-group.c (6b7304af62d02d77d740defd4cfddf2ef3188067)
1// SPDX-License-Identifier: GPL-2.0
2
3#include "misc.h"
4#include "ctree.h"
5#include "block-group.h"
6#include "space-info.h"
7#include "disk-io.h"
8#include "free-space-cache.h"

--- 1059 unchanged lines hidden (view full) ---

1068 key.objectid = block_group->start;
1069 key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
1070 key.offset = block_group->length;
1071
1072 mutex_lock(&fs_info->chunk_mutex);
1073 spin_lock(&block_group->lock);
1074 block_group->removed = 1;
1075 /*
1// SPDX-License-Identifier: GPL-2.0
2
3#include "misc.h"
4#include "ctree.h"
5#include "block-group.h"
6#include "space-info.h"
7#include "disk-io.h"
8#include "free-space-cache.h"

--- 1059 unchanged lines hidden (view full) ---

1068 key.objectid = block_group->start;
1069 key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
1070 key.offset = block_group->length;
1071
1072 mutex_lock(&fs_info->chunk_mutex);
1073 spin_lock(&block_group->lock);
1074 block_group->removed = 1;
1075 /*
1076 * At this point trimming can't start on this block group, because we
1077 * removed the block group from the tree fs_info->block_group_cache_tree
1078 * so no one can't find it anymore and even if someone already got this
1079 * block group before we removed it from the rbtree, they have already
1080 * incremented block_group->trimming - if they didn't, they won't find
1081 * any free space entries because we already removed them all when we
1082 * called btrfs_remove_free_space_cache().
1076 * At this point trimming or scrub can't start on this block group,
1077 * because we removed the block group from the rbtree
1078 * fs_info->block_group_cache_tree so no one can't find it anymore and
1079 * even if someone already got this block group before we removed it
1080 * from the rbtree, they have already incremented block_group->frozen -
1081 * if they didn't, for the trimming case they won't find any free space
1082 * entries because we already removed them all when we called
1083 * btrfs_remove_free_space_cache().
1083 *
1084 * And we must not remove the extent map from the fs_info->mapping_tree
1085 * to prevent the same logical address range and physical device space
1084 *
1085 * And we must not remove the extent map from the fs_info->mapping_tree
1086 * to prevent the same logical address range and physical device space
1086 * ranges from being reused for a new block group. This is because our
1087 * fs trim operation (btrfs_trim_fs() / btrfs_ioctl_fitrim()) is
1087 * ranges from being reused for a new block group. This is needed to
1088 * avoid races with trimming and scrub.
1089 *
1090 * An fs trim operation (btrfs_trim_fs() / btrfs_ioctl_fitrim()) is
1088 * completely transactionless, so while it is trimming a range the
1089 * currently running transaction might finish and a new one start,
1090 * allowing for new block groups to be created that can reuse the same
1091 * physical device locations unless we take this special care.
1092 *
1093 * There may also be an implicit trim operation if the file system
1094 * is mounted with -odiscard. The same protections must remain
1095 * in place until the extents have been discarded completely when
1096 * the transaction commit has completed.
1097 */
1091 * completely transactionless, so while it is trimming a range the
1092 * currently running transaction might finish and a new one start,
1093 * allowing for new block groups to be created that can reuse the same
1094 * physical device locations unless we take this special care.
1095 *
1096 * There may also be an implicit trim operation if the file system
1097 * is mounted with -odiscard. The same protections must remain
1098 * in place until the extents have been discarded completely when
1099 * the transaction commit has completed.
1100 */
1098 remove_em = (atomic_read(&block_group->trimming) == 0);
1101 remove_em = (atomic_read(&block_group->frozen) == 0);
1099 spin_unlock(&block_group->lock);
1100
1101 mutex_unlock(&fs_info->chunk_mutex);
1102
1103 ret = remove_block_group_free_space(trans, block_group);
1104 if (ret)
1105 goto out_put_group;
1106

--- 332 unchanged lines hidden (view full) ---

1439 if (!async_trim_enabled && btrfs_test_opt(fs_info, DISCARD_ASYNC))
1440 goto flip_async;
1441
1442 /* DISCARD can flip during remount */
1443 trimming = btrfs_test_opt(fs_info, DISCARD_SYNC);
1444
1445 /* Implicit trim during transaction commit. */
1446 if (trimming)
1102 spin_unlock(&block_group->lock);
1103
1104 mutex_unlock(&fs_info->chunk_mutex);
1105
1106 ret = remove_block_group_free_space(trans, block_group);
1107 if (ret)
1108 goto out_put_group;
1109

--- 332 unchanged lines hidden (view full) ---

1442 if (!async_trim_enabled && btrfs_test_opt(fs_info, DISCARD_ASYNC))
1443 goto flip_async;
1444
1445 /* DISCARD can flip during remount */
1446 trimming = btrfs_test_opt(fs_info, DISCARD_SYNC);
1447
1448 /* Implicit trim during transaction commit. */
1449 if (trimming)
1447 btrfs_get_block_group_trimming(block_group);
1450 btrfs_freeze_block_group(block_group);
1448
1449 /*
1450 * Btrfs_remove_chunk will abort the transaction if things go
1451 * horribly wrong.
1452 */
1453 ret = btrfs_remove_chunk(trans, block_group->start);
1454
1455 if (ret) {
1456 if (trimming)
1451
1452 /*
1453 * Btrfs_remove_chunk will abort the transaction if things go
1454 * horribly wrong.
1455 */
1456 ret = btrfs_remove_chunk(trans, block_group->start);
1457
1458 if (ret) {
1459 if (trimming)
1457 btrfs_put_block_group_trimming(block_group);
1460 btrfs_unfreeze_block_group(block_group);
1458 goto end_trans;
1459 }
1460
1461 /*
1462 * If we're not mounted with -odiscard, we can just forget
1463 * about this block group. Otherwise we'll need to wait
1464 * until transaction commit to do the actual discard.
1465 */

--- 332 unchanged lines hidden (view full) ---

1798 INIT_LIST_HEAD(&cache->list);
1799 INIT_LIST_HEAD(&cache->cluster_list);
1800 INIT_LIST_HEAD(&cache->bg_list);
1801 INIT_LIST_HEAD(&cache->ro_list);
1802 INIT_LIST_HEAD(&cache->discard_list);
1803 INIT_LIST_HEAD(&cache->dirty_list);
1804 INIT_LIST_HEAD(&cache->io_list);
1805 btrfs_init_free_space_ctl(cache);
1461 goto end_trans;
1462 }
1463
1464 /*
1465 * If we're not mounted with -odiscard, we can just forget
1466 * about this block group. Otherwise we'll need to wait
1467 * until transaction commit to do the actual discard.
1468 */

--- 332 unchanged lines hidden (view full) ---

1801 INIT_LIST_HEAD(&cache->list);
1802 INIT_LIST_HEAD(&cache->cluster_list);
1803 INIT_LIST_HEAD(&cache->bg_list);
1804 INIT_LIST_HEAD(&cache->ro_list);
1805 INIT_LIST_HEAD(&cache->discard_list);
1806 INIT_LIST_HEAD(&cache->dirty_list);
1807 INIT_LIST_HEAD(&cache->io_list);
1808 btrfs_init_free_space_ctl(cache);
1806 atomic_set(&cache->trimming, 0);
1809 atomic_set(&cache->frozen, 0);
1807 mutex_init(&cache->free_space_lock);
1808 btrfs_init_full_stripe_locks_tree(&cache->full_stripe_locks_root);
1809
1810 return cache;
1811}
1812
1813/*
1814 * Iterate all chunks and verify that each of them has the corresponding block

--- 1566 unchanged lines hidden ---
1810 mutex_init(&cache->free_space_lock);
1811 btrfs_init_full_stripe_locks_tree(&cache->full_stripe_locks_root);
1812
1813 return cache;
1814}
1815
1816/*
1817 * Iterate all chunks and verify that each of them has the corresponding block

--- 1566 unchanged lines hidden ---