fs/xfs/xfs_discard.c
Source file repositories/reference/linux-study-clean/fs/xfs/xfs_discard.c
File Facts
- System
- Linux kernel
- Corpus path
fs/xfs/xfs_discard.c- Extension
.c- Size
- 23938 bytes
- Lines
- 887
- 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.
- Touches user memory; correctness depends on fault-safe copying and privilege boundary handling.
- 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
xfs_platform.hxfs_shared.hxfs_format.hxfs_log_format.hxfs_trans_resv.hxfs_trans.hxfs_mount.hxfs_btree.hxfs_alloc_btree.hxfs_alloc.hxfs_discard.hxfs_error.hxfs_extent_busy.hxfs_trace.hxfs_log.hxfs_ag.hxfs_health.hxfs_rtbitmap.hxfs_rtgroup.h
Detected Declarations
struct xfs_trim_curstruct xfs_trim_rtdevstruct xfs_rtx_busystruct xfs_trim_rtgroupfunction xfs_discard_endio_workfunction xfs_discard_endiofunction xfs_discard_extentsfunction xfs_trim_gather_extentsfunction xfs_trim_should_stopfunction xfs_trim_perag_extentsfunction xfs_trim_datadev_extentsfunction xfs_discard_free_rtdev_extentsfunction list_for_each_entry_safefunction xfs_discard_rtdev_extentsfunction xfs_trim_gather_rtextentfunction xfs_trim_rtextentsfunction xfs_trim_gather_rtgroup_extentfunction xfs_trim_rtgroup_extentsfunction xfs_trim_rtdev_extentsfunction xfs_ioc_trim
Annotated Snippet
struct xfs_trim_cur {
xfs_agblock_t start;
xfs_extlen_t count;
xfs_agblock_t end;
xfs_extlen_t minlen;
bool by_bno;
};
static int
xfs_trim_gather_extents(
struct xfs_perag *pag,
struct xfs_trim_cur *tcur,
struct xfs_busy_extents *extents)
{
struct xfs_mount *mp = pag_mount(pag);
struct xfs_trans *tp;
struct xfs_btree_cur *cur;
struct xfs_buf *agbp;
int error;
int i;
int batch = XFS_DISCARD_MAX_EXAMINE;
/*
* Force out the log. This means any transactions that might have freed
* space before we take the AGF buffer lock are now on disk, and the
* volatile disk cache is flushed.
*/
xfs_log_force(mp, XFS_LOG_SYNC);
tp = xfs_trans_alloc_empty(mp);
error = xfs_alloc_read_agf(pag, tp, 0, &agbp);
if (error)
goto out_trans_cancel;
/*
* First time through tcur->count will not have been initialised as
* pag->pagf_longest is not guaranteed to be valid before we read
* the AGF buffer above.
*/
if (!tcur->count)
tcur->count = pag->pagf_longest;
if (tcur->by_bno) {
/* sub-AG discard request always starts at tcur->start */
cur = xfs_bnobt_init_cursor(mp, tp, agbp, pag);
error = xfs_alloc_lookup_le(cur, tcur->start, 0, &i);
if (!error && !i)
error = xfs_alloc_lookup_ge(cur, tcur->start, 0, &i);
} else if (tcur->start == 0) {
/* first time through a by-len starts with max length */
cur = xfs_cntbt_init_cursor(mp, tp, agbp, pag);
error = xfs_alloc_lookup_ge(cur, 0, tcur->count, &i);
} else {
/* nth time through a by-len starts where we left off */
cur = xfs_cntbt_init_cursor(mp, tp, agbp, pag);
error = xfs_alloc_lookup_le(cur, tcur->start, tcur->count, &i);
}
if (error)
goto out_del_cursor;
if (i == 0) {
/* nothing of that length left in the AG, we are done */
tcur->count = 0;
goto out_del_cursor;
}
/*
* Loop until we are done with all extents that are large
* enough to be worth discarding or we hit batch limits.
*/
while (i) {
xfs_agblock_t fbno;
xfs_extlen_t flen;
error = xfs_alloc_get_rec(cur, &fbno, &flen, &i);
if (error)
break;
if (XFS_IS_CORRUPT(mp, i != 1)) {
xfs_btree_mark_sick(cur);
error = -EFSCORRUPTED;
break;
}
if (--batch <= 0) {
/*
* Update the cursor to point at this extent so we
* restart the next batch from this extent.
*/
tcur->start = fbno;
tcur->count = flen;
Annotation
- Immediate include surface: `xfs_platform.h`, `xfs_shared.h`, `xfs_format.h`, `xfs_log_format.h`, `xfs_trans_resv.h`, `xfs_trans.h`, `xfs_mount.h`, `xfs_btree.h`.
- Detected declarations: `struct xfs_trim_cur`, `struct xfs_trim_rtdev`, `struct xfs_rtx_busy`, `struct xfs_trim_rtgroup`, `function xfs_discard_endio_work`, `function xfs_discard_endio`, `function xfs_discard_extents`, `function xfs_trim_gather_extents`, `function xfs_trim_should_stop`, `function xfs_trim_perag_extents`.
- Atlas domain: Core OS / VFS And Filesystem Core.
- Implementation status: source implementation candidate.
- This snippet crosses the user/kernel memory boundary; validate fault handling and access checks before translating the pattern.
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.