fs/btrfs/ctree.c
Source file repositories/reference/linux-study-clean/fs/btrfs/ctree.c
File Facts
- System
- Linux kernel
- Corpus path
fs/btrfs/ctree.c- Extension
.c- Size
- 136108 bytes
- Lines
- 5066
- 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/sched.hlinux/slab.hlinux/rbtree.hlinux/mm.hlinux/error-injection.hmessages.hctree.hdisk-io.htransaction.hprint-tree.hlocking.hvolumes.hqgroup.htree-mod-log.htree-checker.hfs.haccessors.hextent-tree.hextent_io.hrelocation.hfile-item.h
Detected Declarations
function leaf_data_endfunction memmove_extent_bufferfunction copy_extent_bufferfunction memmove_extent_bufferfunction copy_extent_bufferfunction btrfs_free_pathfunction btrfs_release_pathfunction rootfunction btrfs_copy_rootfunction btrfs_block_can_be_sharedfunction update_ref_for_cowfunction treefunction btrfs_force_cow_blockfunction should_cow_blockfunction btrfs_cow_blockfunction generationfunction btrfs_comp_cpu_keysfunction itemsfunction root_add_used_bytesfunction root_sub_used_bytesfunction takenfunction itemfunction balance_levelfunction push_nodes_for_insertfunction reada_for_searchfunction oncefunction reada_for_balancefunction unlock_upfunction btrfs_search_slotfunction setup_nodes_for_searchfunction BTRFS_NODEPTRS_PER_BLOCKfunction btrfs_find_itemfunction finish_need_commit_sem_searchfunction search_for_key_slotfunction search_leaffunction leaffunction btrfs_search_old_slotfunction btrfs_prev_leaffunction btrfs_search_slotfunction btrfs_search_slot_for_readfunction btrfs_search_backwardsfunction btrfs_get_next_valid_itemfunction fixup_low_keysfunction btrfs_set_item_key_safefunction check_sibling_keysfunction push_node_leftfunction balance_node_rightfunction insert_new_root
Annotated Snippet
if (p->locks[i]) {
btrfs_tree_unlock_rw(p->nodes[i], p->locks[i]);
p->locks[i] = 0;
}
free_extent_buffer(p->nodes[i]);
p->nodes[i] = NULL;
}
}
/*
* safely gets a reference on the root node of a tree. A lock
* is not taken, so a concurrent writer may put a different node
* at the root of the tree. See btrfs_lock_root_node for the
* looping required.
*
* The extent buffer returned by this has a reference taken, so
* it won't disappear. It may stop being the root of the tree
* at any time because there are no locks held.
*/
struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
{
struct extent_buffer *eb;
while (1) {
rcu_read_lock();
eb = rcu_dereference(root->node);
/*
* RCU really hurts here, we could free up the root node because
* it was COWed but we may not get the new root node yet so do
* the inc_not_zero dance and if it doesn't work then
* synchronize_rcu and try again.
*/
if (refcount_inc_not_zero(&eb->refs)) {
rcu_read_unlock();
break;
}
rcu_read_unlock();
synchronize_rcu();
}
return eb;
}
/*
* Cowonly root (not-shareable trees, everything not subvolume or reloc roots),
* just get put onto a simple dirty list. Transaction walks this list to make
* sure they get properly updated on disk.
*/
static void add_root_to_dirty_list(struct btrfs_root *root)
{
struct btrfs_fs_info *fs_info = root->fs_info;
if (test_bit(BTRFS_ROOT_DIRTY, &root->state) ||
!test_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state))
return;
spin_lock(&fs_info->trans_lock);
if (!test_and_set_bit(BTRFS_ROOT_DIRTY, &root->state)) {
/* Want the extent tree to be the last on the list */
if (btrfs_root_id(root) == BTRFS_EXTENT_TREE_OBJECTID)
list_move_tail(&root->dirty_list,
&fs_info->dirty_cowonly_roots);
else
list_move(&root->dirty_list,
&fs_info->dirty_cowonly_roots);
}
spin_unlock(&fs_info->trans_lock);
}
/*
* used by snapshot creation to make a copy of a root for a tree with
* a given objectid. The buffer with the new root node is returned in
* cow_ret, and this func returns zero on success or a negative error code.
*/
int btrfs_copy_root(struct btrfs_trans_handle *trans,
struct btrfs_root *root,
struct extent_buffer *buf,
struct extent_buffer **cow_ret, u64 new_root_objectid)
{
struct btrfs_fs_info *fs_info = root->fs_info;
struct extent_buffer *cow;
int ret = 0;
int level;
struct btrfs_disk_key disk_key;
const bool is_reloc_root = (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID);
u64 reloc_src_root = 0;
WARN_ON(test_bit(BTRFS_ROOT_SHAREABLE, &root->state) &&
trans->transid != fs_info->running_transaction->transid);
WARN_ON(test_bit(BTRFS_ROOT_SHAREABLE, &root->state) &&
Annotation
- Immediate include surface: `linux/sched.h`, `linux/slab.h`, `linux/rbtree.h`, `linux/mm.h`, `linux/error-injection.h`, `messages.h`, `ctree.h`, `disk-io.h`.
- Detected declarations: `function leaf_data_end`, `function memmove_extent_buffer`, `function copy_extent_buffer`, `function memmove_extent_buffer`, `function copy_extent_buffer`, `function btrfs_free_path`, `function btrfs_release_path`, `function root`, `function btrfs_copy_root`, `function btrfs_block_can_be_shared`.
- 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.