drivers/block/virtio_blk.c
Source file repositories/reference/linux-study-clean/drivers/block/virtio_blk.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/block/virtio_blk.c- Extension
.c- Size
- 44459 bytes
- Lines
- 1733
- Domain
- Driver Families
- Bucket
- drivers/block
- Inferred role
- Driver Families: operation-table or driver-model contract
- Status
- pattern implementation candidate
Why This File Exists
Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.
- Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.
- Defines an operation table; this is where Linux turns generic core objects into subsystem-specific behavior.
- 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/spinlock.hlinux/slab.hlinux/blkdev.hlinux/hdreg.hlinux/module.hlinux/mutex.hlinux/interrupt.hlinux/virtio.hlinux/virtio_blk.hlinux/scatterlist.hlinux/string_helpers.hlinux/idr.hlinux/blk-mq.hlinux/numa.hlinux/vmalloc.huapi/linux/virtio_ring.h
Detected Declarations
struct virtio_blk_vqstruct virtio_blkstruct virtblk_reqfunction virtblk_resultfunction virtblk_add_reqfunction virtblk_setup_discard_write_zeroes_erasefunction __rq_for_each_biofunction virtblk_unmap_datafunction virtblk_map_datafunction virtblk_cleanup_cmdfunction virtblk_setup_cmdfunction virtblk_vbr_statusfunction virtblk_request_donefunction virtblk_donefunction virtio_commit_rqsfunction virtblk_fail_to_queuefunction virtblk_prep_rqfunction virtio_queue_rqfunction virtblk_prep_rq_batchfunction virtblk_add_req_batchfunction virtio_queue_rqsfunction virtblk_submit_zone_reportfunction virtblk_parse_zonefunction virtblk_report_zonesfunction virtblk_read_zoned_limitsfunction virtblk_read_zoned_limitsfunction virtblk_get_idfunction virtblk_getgeofunction virtblk_free_diskfunction index_to_minorfunction minor_to_indexfunction serial_showfunction virtblk_update_capacityfunction virtblk_config_changed_workfunction virtblk_config_changedfunction init_vqfunction virtblk_name_formatfunction virtblk_get_cache_modefunction cache_type_storefunction cache_type_showfunction virtblk_attrs_are_visiblefunction virtblk_map_queuesfunction virtblk_complete_batchfunction rq_list_for_eachfunction virtblk_pollfunction virtblk_read_limitsfunction virtblk_probefunction virtio_device_ready
Annotated Snippet
static const struct blk_mq_ops virtio_mq_ops = {
.queue_rq = virtio_queue_rq,
.queue_rqs = virtio_queue_rqs,
.commit_rqs = virtio_commit_rqs,
.complete = virtblk_request_done,
.map_queues = virtblk_map_queues,
.poll = virtblk_poll,
};
static unsigned int virtblk_queue_depth;
module_param_named(queue_depth, virtblk_queue_depth, uint, 0444);
static int virtblk_read_limits(struct virtio_blk *vblk,
struct queue_limits *lim)
{
struct virtio_device *vdev = vblk->vdev;
u32 v, max_size, sg_elems, opt_io_size;
u32 max_discard_segs = 0;
u32 discard_granularity = 0;
u16 min_io_size;
u8 physical_block_exp, alignment_offset;
size_t max_dma_size;
int err;
/* We need to know how many segments before we allocate. */
err = virtio_cread_feature(vdev, VIRTIO_BLK_F_SEG_MAX,
struct virtio_blk_config, seg_max,
&sg_elems);
/* We need at least one SG element, whatever they say. */
if (err || !sg_elems)
sg_elems = 1;
/* Prevent integer overflows and honor max vq size */
sg_elems = min_t(u32, sg_elems, VIRTIO_BLK_MAX_SG_ELEMS - 2);
/* We can handle whatever the host told us to handle. */
lim->max_segments = sg_elems;
/* No real sector limit. */
lim->max_hw_sectors = UINT_MAX;
max_dma_size = virtio_max_dma_size(vdev);
max_size = max_dma_size > U32_MAX ? U32_MAX : max_dma_size;
/* Host can optionally specify maximum segment size and number of
* segments. */
err = virtio_cread_feature(vdev, VIRTIO_BLK_F_SIZE_MAX,
struct virtio_blk_config, size_max, &v);
if (!err)
max_size = min(max_size, v);
lim->max_segment_size = max_size;
/* Host can optionally specify the block size of the device */
virtio_cread_feature(vdev, VIRTIO_BLK_F_BLK_SIZE,
struct virtio_blk_config, blk_size,
&lim->logical_block_size);
/* Use topology information if available */
err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
struct virtio_blk_config, physical_block_exp,
&physical_block_exp);
if (!err && physical_block_exp)
lim->physical_block_size =
lim->logical_block_size * (1 << physical_block_exp);
err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
struct virtio_blk_config, alignment_offset,
&alignment_offset);
if (!err && alignment_offset)
lim->alignment_offset =
lim->logical_block_size * alignment_offset;
err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
struct virtio_blk_config, min_io_size,
&min_io_size);
if (!err && min_io_size)
lim->io_min = lim->logical_block_size * min_io_size;
err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
struct virtio_blk_config, opt_io_size,
&opt_io_size);
if (!err && opt_io_size)
lim->io_opt = lim->logical_block_size * opt_io_size;
if (virtio_has_feature(vdev, VIRTIO_BLK_F_DISCARD)) {
virtio_cread(vdev, struct virtio_blk_config,
discard_sector_alignment, &discard_granularity);
Annotation
- Immediate include surface: `linux/spinlock.h`, `linux/slab.h`, `linux/blkdev.h`, `linux/hdreg.h`, `linux/module.h`, `linux/mutex.h`, `linux/interrupt.h`, `linux/virtio.h`.
- Detected declarations: `struct virtio_blk_vq`, `struct virtio_blk`, `struct virtblk_req`, `function virtblk_result`, `function virtblk_add_req`, `function virtblk_setup_discard_write_zeroes_erase`, `function __rq_for_each_bio`, `function virtblk_unmap_data`, `function virtblk_map_data`, `function virtblk_cleanup_cmd`.
- Atlas domain: Driver Families / drivers/block.
- Implementation status: pattern 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.