fs/btrfs/block-group.c
Source file repositories/reference/linux-study-clean/fs/btrfs/block-group.c
File Facts
- System
- Linux kernel
- Corpus path
fs/btrfs/block-group.c- Extension
.c- Size
- 155999 bytes
- Lines
- 5019
- Domain
- Core OS
- Bucket
- VFS And Filesystem Core
- Inferred role
- Core OS: implementation source
- Status
- source implementation candidate
Why This File Exists
Core operating-system implementation surface: boot, tasks, memory, VFS, syscall-facing interfaces, synchronization, credentials, and isolation.
- Core operating-system implementation surface: boot, tasks, memory, VFS, syscall-facing interfaces, synchronization, credentials, and isolation.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/sizes.hlinux/list_sort.hmisc.hctree.hblock-group.hspace-info.hdisk-io.hfree-space-cache.hfree-space-tree.hvolumes.htransaction.href-verify.hsysfs.htree-log.hdelalloc-space.hdiscard.hraid56.hzoned.hfs.haccessors.hextent-tree.h
Detected Declarations
function btrfs_init_block_groupfunction btrfs_exit_block_groupfunction btrfs_should_fragment_free_spacefunction has_unwritten_metadatafunction get_restripe_targetfunction btrfs_reduce_alloc_profilefunction btrfs_get_alloc_profilefunction btrfs_get_block_groupfunction btrfs_put_block_groupfunction btrfs_bg_start_cmpfunction btrfs_add_block_group_cachefunction btrfs_dec_nocow_writersfunction btrfs_inc_nocow_writersfunction btrfs_wait_nocow_writersfunction btrfs_dec_block_group_reservationsfunction btrfs_wait_block_group_reservationsfunction btrfs_put_caching_controlfunction btrfs_wait_block_group_cache_progressfunction btrfs_caching_ctl_wait_donefunction btrfs_wait_block_group_cache_donefunction fragment_free_spacefunction btrfs_add_new_free_spacefunction sample_block_group_extent_itemfunction btrfs_for_each_slotfunction load_block_group_size_classfunction load_extent_tree_freefunction btrfs_free_excluded_extentsfunction caching_threadfunction btrfs_cache_block_groupfunction clear_avail_alloc_bitsfunction featurefunction list_for_each_entry_rcufunction remove_block_group_itemfunction btrfs_remove_bg_from_sinfofunction btrfs_remove_block_groupfunction list_for_each_entryfunction keyfunction spacefunction clean_pinned_extentsfunction list_add_tailfunction btrfs_delete_unused_bgsfunction btrfs_mark_bg_unusedfunction reclaim_bgs_cmpfunction btrfs_should_reclaimfunction should_reclaim_block_groupfunction btrfs_reclaim_block_groupfunction btrfs_reclaim_block_groupsfunction btrfs_reclaim_bgs_work
Annotated Snippet
if (bytenr < start) {
if (!contains && (!ret || start < ret->start))
ret = cache;
n = n->rb_left;
} else if (bytenr > start) {
if (contains && bytenr <= end) {
ret = cache;
break;
}
n = n->rb_right;
} else {
ret = cache;
break;
}
}
if (ret)
btrfs_get_block_group(ret);
read_unlock(&info->block_group_cache_lock);
return ret;
}
/*
* Return the block group that starts at or after bytenr
*/
struct btrfs_block_group *btrfs_lookup_first_block_group(
struct btrfs_fs_info *info, u64 bytenr)
{
return block_group_cache_tree_search(info, bytenr, 0);
}
/*
* Return the block group that contains the given bytenr
*/
struct btrfs_block_group *btrfs_lookup_block_group(
struct btrfs_fs_info *info, u64 bytenr)
{
return block_group_cache_tree_search(info, bytenr, 1);
}
struct btrfs_block_group *btrfs_next_block_group(
struct btrfs_block_group *cache)
{
struct btrfs_fs_info *fs_info = cache->fs_info;
struct rb_node *node;
read_lock(&fs_info->block_group_cache_lock);
/* If our block group was removed, we need a full search. */
if (RB_EMPTY_NODE(&cache->cache_node)) {
const u64 next_bytenr = btrfs_block_group_end(cache);
read_unlock(&fs_info->block_group_cache_lock);
btrfs_put_block_group(cache);
return btrfs_lookup_first_block_group(fs_info, next_bytenr);
}
node = rb_next(&cache->cache_node);
btrfs_put_block_group(cache);
if (node) {
cache = rb_entry(node, struct btrfs_block_group, cache_node);
btrfs_get_block_group(cache);
} else
cache = NULL;
read_unlock(&fs_info->block_group_cache_lock);
return cache;
}
/*
* Check if we can do a NOCOW write for a given extent.
*
* @fs_info: The filesystem information object.
* @bytenr: Logical start address of the extent.
*
* Check if we can do a NOCOW write for the given extent, and increments the
* number of NOCOW writers in the block group that contains the extent, as long
* as the block group exists and it's currently not in read-only mode.
*
* Returns: A non-NULL block group pointer if we can do a NOCOW write, the caller
* is responsible for calling btrfs_dec_nocow_writers() later.
*
* Or NULL if we can not do a NOCOW write
*/
struct btrfs_block_group *btrfs_inc_nocow_writers(struct btrfs_fs_info *fs_info,
u64 bytenr)
{
struct btrfs_block_group *bg;
bool can_nocow = true;
bg = btrfs_lookup_block_group(fs_info, bytenr);
if (!bg)
Annotation
- Immediate include surface: `linux/sizes.h`, `linux/list_sort.h`, `misc.h`, `ctree.h`, `block-group.h`, `space-info.h`, `disk-io.h`, `free-space-cache.h`.
- Detected declarations: `function btrfs_init_block_group`, `function btrfs_exit_block_group`, `function btrfs_should_fragment_free_space`, `function has_unwritten_metadata`, `function get_restripe_target`, `function btrfs_reduce_alloc_profile`, `function btrfs_get_alloc_profile`, `function btrfs_get_block_group`, `function btrfs_put_block_group`, `function btrfs_bg_start_cmp`.
- Atlas domain: Core OS / VFS And Filesystem Core.
- Implementation status: source implementation candidate.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.