block/bdev.c
Source file repositories/reference/linux-study-clean/block/bdev.c
File Facts
- System
- Linux kernel
- Corpus path
block/bdev.c- Extension
.c- Size
- 35905 bytes
- Lines
- 1376
- Domain
- Representative Device Path
- Bucket
- PCIe NVMe Storage Path
- Inferred role
- Representative Device Path: exported/initcall integration point
- Status
- integration implementation candidate
Why This File Exists
Part of the selected hardware vertical slice: PCI discovery, driver binding, NVMe queues, block requests, DMA, interrupts, and completion.
- Part of the selected hardware vertical slice: PCI discovery, driver binding, NVMe queues, block requests, DMA, interrupts, and completion.
- Exports symbols or registers init work; inspect boot/module ordering and who consumes the exported contract.
- 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/init.hlinux/mm.hlinux/slab.hlinux/kmod.hlinux/major.hlinux/device_cgroup.hlinux/blkdev.hlinux/blk-integrity.hlinux/backing-dev.hlinux/module.hlinux/blkpg.hlinux/magic.hlinux/buffer_head.hlinux/swap.hlinux/writeback.hlinux/mount.hlinux/pseudo_fs.hlinux/uio.hlinux/namei.hlinux/security.hlinux/part_stat.hlinux/uaccess.hlinux/stat.h../fs/internal.hblk.h
Detected Declarations
struct bdev_inodefunction bdev_write_inodefunction kill_bdevfunction invalidate_bdevfunction ownerfunction set_init_blocksizefunction bdev_validate_blocksizefunction set_blocksizefunction sb_validate_large_blocksizefunction sb_set_blocksizefunction sb_min_blocksizefunction sync_blockdev_nowaitfunction sync_blockdevfunction sync_blockdev_rangefunction counterfunction bdev_freezefunction bdev_free_inodefunction init_oncefunction bd_init_fs_contextfunction bdev_cache_initfunction bdev_set_nr_sectorsfunction bdev_addfunction bdev_unhashfunction bdev_dropfunction nr_blockdev_pagesfunction bd_may_claimfunction bd_prepare_to_claimfunction bd_clear_claimingfunction bd_finish_claimingfunction bd_abort_claimingfunction bd_end_claimfunction blkdev_flush_mappingfunction blkdev_put_wholefunction blkdev_get_wholefunction blkdev_get_partfunction bdev_permissionfunction blkdev_put_partfunction blkdev_put_no_openfunction bdev_writes_blockedfunction bdev_block_writesfunction bdev_unblock_writesfunction bdev_may_openfunction bdev_claim_write_accessfunction bdev_unclaimedfunction bdev_yield_write_accessfunction bdev_openfunction blk_to_file_flagsfunction bd_yield_claim
Annotated Snippet
struct bdev_inode {
struct block_device bdev;
struct inode vfs_inode;
};
static inline struct bdev_inode *BDEV_I(struct inode *inode)
{
return container_of(inode, struct bdev_inode, vfs_inode);
}
static inline struct inode *BD_INODE(struct block_device *bdev)
{
return &container_of(bdev, struct bdev_inode, bdev)->vfs_inode;
}
struct block_device *I_BDEV(struct inode *inode)
{
return &BDEV_I(inode)->bdev;
}
EXPORT_SYMBOL(I_BDEV);
struct block_device *file_bdev(struct file *bdev_file)
{
return I_BDEV(bdev_file->f_mapping->host);
}
EXPORT_SYMBOL(file_bdev);
static void bdev_write_inode(struct block_device *bdev)
{
struct inode *inode = BD_INODE(bdev);
int ret;
spin_lock(&inode->i_lock);
while (inode_state_read(inode) & I_DIRTY) {
spin_unlock(&inode->i_lock);
ret = write_inode_now(inode, true);
if (ret)
pr_warn_ratelimited(
"VFS: Dirty inode writeback failed for block device %pg (err=%d).\n",
bdev, ret);
spin_lock(&inode->i_lock);
}
spin_unlock(&inode->i_lock);
}
/* Kill _all_ buffers and pagecache , dirty or not.. */
static void kill_bdev(struct block_device *bdev)
{
struct address_space *mapping = bdev->bd_mapping;
if (mapping_empty(mapping))
return;
invalidate_bh_lrus();
truncate_inode_pages(mapping, 0);
}
/* Invalidate clean unused buffers and pagecache. */
void invalidate_bdev(struct block_device *bdev)
{
struct address_space *mapping = bdev->bd_mapping;
if (mapping->nrpages) {
invalidate_bh_lrus();
lru_add_drain_all(); /* make sure all lru add caches are flushed */
invalidate_mapping_pages(mapping, 0, -1);
}
}
EXPORT_SYMBOL(invalidate_bdev);
/*
* Drop all buffers & page cache for given bdev range. This function bails
* with error if bdev has other exclusive owner (such as filesystem).
*/
int truncate_bdev_range(struct block_device *bdev, blk_mode_t mode,
loff_t lstart, loff_t lend)
{
/*
* If we don't hold exclusive handle for the device, upgrade to it
* while we discard the buffer cache to avoid discarding buffers
* under live filesystem.
*/
if (!(mode & BLK_OPEN_EXCL)) {
int err = bd_prepare_to_claim(bdev, truncate_bdev_range, NULL);
if (err)
goto invalidate;
}
truncate_inode_pages_range(bdev->bd_mapping, lstart, lend);
if (!(mode & BLK_OPEN_EXCL))
Annotation
- Immediate include surface: `linux/init.h`, `linux/mm.h`, `linux/slab.h`, `linux/kmod.h`, `linux/major.h`, `linux/device_cgroup.h`, `linux/blkdev.h`, `linux/blk-integrity.h`.
- Detected declarations: `struct bdev_inode`, `function bdev_write_inode`, `function kill_bdev`, `function invalidate_bdev`, `function owner`, `function set_init_blocksize`, `function bdev_validate_blocksize`, `function set_blocksize`, `function sb_validate_large_blocksize`, `function sb_set_blocksize`.
- Atlas domain: Representative Device Path / PCIe NVMe Storage Path.
- Implementation status: integration 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.