fs/btrfs/direct-io.c
Source file repositories/reference/linux-study-clean/fs/btrfs/direct-io.c
File Facts
- System
- Linux kernel
- Corpus path
fs/btrfs/direct-io.c- Extension
.c- Size
- 36333 bytes
- Lines
- 1157
- 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.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/fsverity.hlinux/iomap.hctree.hdelalloc-space.hdirect-io.hextent-tree.hfile.hfs.htransaction.hvolumes.hbio.hordered-data.h
Detected Declarations
struct btrfs_dio_datastruct btrfs_dio_privatefunction lock_extent_directfunction btrfs_get_blocks_direct_writefunction btrfs_dio_iomap_beginfunction iomap_dio_rwfunction i_sizefunction blockingfunction extentsfunction btrfs_dio_iomap_endfunction btrfs_dio_end_iofunction btrfs_extract_ordered_extentfunction btrfs_dio_submit_iofunction btrfs_dio_readfunction check_direct_IOfunction btrfs_direct_writefunction generic_write_checkfunction tofunction check_direct_readfunction btrfs_direct_readfunction btrfs_init_diofunction btrfs_destroy_dio
Annotated Snippet
struct btrfs_dio_data {
ssize_t submitted;
loff_t old_isize;
struct extent_changeset *data_reserved;
struct btrfs_ordered_extent *ordered;
bool data_space_reserved;
bool nocow_done;
bool updated_isize;
};
struct btrfs_dio_private {
/* Range of I/O */
u64 file_offset;
u32 bytes;
/* This must be last */
struct btrfs_bio bbio;
};
static struct bio_set btrfs_dio_bioset;
static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
struct extent_state **cached_state,
unsigned int iomap_flags)
{
const bool writing = (iomap_flags & IOMAP_WRITE);
const bool nowait = (iomap_flags & IOMAP_NOWAIT);
struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
struct btrfs_ordered_extent *ordered;
int ret = 0;
/* Direct lock must be taken before the extent lock. */
if (nowait) {
if (!btrfs_try_lock_dio_extent(io_tree, lockstart, lockend, cached_state))
return -EAGAIN;
} else {
btrfs_lock_dio_extent(io_tree, lockstart, lockend, cached_state);
}
while (1) {
if (nowait) {
if (!btrfs_try_lock_extent(io_tree, lockstart, lockend,
cached_state)) {
ret = -EAGAIN;
break;
}
} else {
btrfs_lock_extent(io_tree, lockstart, lockend, cached_state);
}
/*
* We're concerned with the entire range that we're going to be
* doing DIO to, so we need to make sure there's no ordered
* extents in this range.
*/
ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
lockend - lockstart + 1);
/*
* We need to make sure there are no buffered pages in this
* range either, we could have raced between the invalidate in
* generic_file_direct_write and locking the extent. The
* invalidate needs to happen so that reads after a write do not
* get stale data.
*/
if (!ordered &&
(!writing || !filemap_range_has_page(inode->i_mapping,
lockstart, lockend)))
break;
btrfs_unlock_extent(io_tree, lockstart, lockend, cached_state);
if (ordered) {
if (nowait) {
btrfs_put_ordered_extent(ordered);
ret = -EAGAIN;
break;
}
/*
* If we are doing a DIO read and the ordered extent we
* found is for a buffered write, we can not wait for it
* to complete and retry, because if we do so we can
* deadlock with concurrent buffered writes on page
* locks. This happens only if our DIO read covers more
* than one extent map, if at this point has already
* created an ordered extent for a previous extent map
* and locked its range in the inode's io tree, and a
* concurrent write against that previous extent map's
* range and this range started (we unlock the ranges
* in the io tree only when the bios complete and
* buffered writes always lock pages before attempting
Annotation
- Immediate include surface: `linux/fsverity.h`, `linux/iomap.h`, `ctree.h`, `delalloc-space.h`, `direct-io.h`, `extent-tree.h`, `file.h`, `fs.h`.
- Detected declarations: `struct btrfs_dio_data`, `struct btrfs_dio_private`, `function lock_extent_direct`, `function btrfs_get_blocks_direct_write`, `function btrfs_dio_iomap_begin`, `function iomap_dio_rw`, `function i_size`, `function blocking`, `function extents`, `function btrfs_dio_iomap_end`.
- Atlas domain: Core OS / VFS And Filesystem Core.
- Implementation status: source implementation candidate.
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.