fs/btrfs/defrag.c
Source file repositories/reference/linux-study-clean/fs/btrfs/defrag.c
File Facts
- System
- Linux kernel
- Corpus path
fs/btrfs/defrag.c- Extension
.c- Size
- 41058 bytes
- Lines
- 1500
- 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.hctree.hdisk-io.htransaction.hlocking.haccessors.hmessages.hdelalloc-space.hsubpage.hdefrag.hfile-item.hsuper.hcompression.h
Detected Declarations
struct inode_defragstruct defrag_target_rangefunction compare_inode_defragfunction inode_defrag_cmpfunction btrfs_insert_inode_defragfunction need_auto_defragfunction btrfs_add_inode_defragfunction btrfs_cleanup_defrag_inodesfunction btrfs_run_defrag_inodefunction btrfs_run_defrag_inodesfunction close_blocksfunction btrfs_realloc_nodefunction generationfunction btrfs_defrag_leavesfunction btrfs_defrag_rootfunction btrfs_get_extentfunction compatiblefunction get_extent_max_capacityfunction defrag_check_next_extentfunction defrag_collect_targetsfunction list_for_each_entry_safefunction defrag_one_locked_targetfunction defrag_one_rangefunction list_for_each_entryfunction list_for_each_entry_safefunction defrag_one_clusterfunction list_for_each_entryfunction btrfs_defrag_filefunction btrfs_auto_defrag_exitfunction btrfs_auto_defrag_init
Annotated Snippet
struct inode_defrag {
struct rb_node rb_node;
/* Inode number */
u64 ino;
/*
* Transid where the defrag was added, we search for extents newer than
* this.
*/
u64 transid;
/* Root objectid */
u64 root;
/*
* The extent size threshold for autodefrag.
*
* This value is different for compressed/non-compressed extents, thus
* needs to be passed from higher layer.
* (aka, inode_should_defrag())
*/
u32 extent_thresh;
};
static int compare_inode_defrag(const struct inode_defrag *defrag1,
const struct inode_defrag *defrag2)
{
if (defrag1->root > defrag2->root)
return 1;
else if (defrag1->root < defrag2->root)
return -1;
else if (defrag1->ino > defrag2->ino)
return 1;
else if (defrag1->ino < defrag2->ino)
return -1;
else
return 0;
}
static int inode_defrag_cmp(struct rb_node *new, const struct rb_node *existing)
{
const struct inode_defrag *new_defrag = rb_entry(new, struct inode_defrag, rb_node);
const struct inode_defrag *existing_defrag = rb_entry(existing, struct inode_defrag, rb_node);
return compare_inode_defrag(new_defrag, existing_defrag);
}
/*
* Insert a record for an inode into the defrag tree. The lock must be held
* already.
*
* If you're inserting a record for an older transid than an existing record,
* the transid already in the tree is lowered.
*/
static int btrfs_insert_inode_defrag(struct btrfs_inode *inode,
struct inode_defrag *defrag)
{
struct btrfs_fs_info *fs_info = inode->root->fs_info;
struct rb_node *node;
node = rb_find_add(&defrag->rb_node, &fs_info->defrag_inodes, inode_defrag_cmp);
if (node) {
struct inode_defrag *entry;
entry = rb_entry(node, struct inode_defrag, rb_node);
/*
* If we're reinserting an entry for an old defrag run, make
* sure to lower the transid of our existing record.
*/
if (defrag->transid < entry->transid)
entry->transid = defrag->transid;
entry->extent_thresh = min(defrag->extent_thresh, entry->extent_thresh);
return -EEXIST;
}
set_bit(BTRFS_INODE_IN_DEFRAG, &inode->runtime_flags);
return 0;
}
static inline bool need_auto_defrag(struct btrfs_fs_info *fs_info)
{
if (!btrfs_test_opt(fs_info, AUTO_DEFRAG))
return false;
if (btrfs_fs_closing(fs_info))
return false;
return true;
}
/*
* Insert a defrag record for this inode if auto defrag is enabled. No errors
Annotation
- Immediate include surface: `linux/sched.h`, `ctree.h`, `disk-io.h`, `transaction.h`, `locking.h`, `accessors.h`, `messages.h`, `delalloc-space.h`.
- Detected declarations: `struct inode_defrag`, `struct defrag_target_range`, `function compare_inode_defrag`, `function inode_defrag_cmp`, `function btrfs_insert_inode_defrag`, `function need_auto_defrag`, `function btrfs_add_inode_defrag`, `function btrfs_cleanup_defrag_inodes`, `function btrfs_run_defrag_inode`, `function btrfs_run_defrag_inodes`.
- 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.