block/bsg-lib.c
Source file repositories/reference/linux-study-clean/block/bsg-lib.c
File Facts
- System
- Linux kernel
- Corpus path
block/bsg-lib.c- Extension
.c- Size
- 10090 bytes
- Lines
- 413
- Domain
- Representative Device Path
- Bucket
- PCIe NVMe Storage Path
- Inferred role
- Representative Device Path: operation-table or driver-model contract
- Status
- pattern 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.
- Defines an operation table; this is where Linux turns generic core objects into subsystem-specific behavior.
- 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
linux/bsg.hlinux/slab.hlinux/blk-mq.hlinux/delay.hlinux/scatterlist.hlinux/bsg-lib.hlinux/export.hscsi/scsi_cmnd.hscsi/sg.h
Detected Declarations
struct bsg_setfunction bsg_transport_sg_io_fnfunction bsg_teardown_jobfunction bsg_job_putfunction bsg_job_getfunction bsg_job_donefunction bsg_completefunction bsg_map_bufferfunction bsg_prepare_jobfunction bsg_queue_rqfunction bsg_init_rqfunction bsg_exit_rqfunction bsg_remove_queuefunction bsg_timeoutexport bsg_job_putexport bsg_job_getexport bsg_job_doneexport bsg_remove_queueexport bsg_setup_queue
Annotated Snippet
static const struct blk_mq_ops bsg_mq_ops = {
.queue_rq = bsg_queue_rq,
.init_request = bsg_init_rq,
.exit_request = bsg_exit_rq,
.complete = bsg_complete,
.timeout = bsg_timeout,
};
/**
* bsg_setup_queue - Create and add the bsg hooks so we can receive requests
* @dev: device to attach bsg device to
* @name: device to give bsg device
* @lim: queue limits for the bsg queue
* @job_fn: bsg job handler
* @timeout: timeout handler function pointer
* @dd_job_size: size of LLD data needed for each job
*/
struct request_queue *bsg_setup_queue(struct device *dev, const char *name,
struct queue_limits *lim, bsg_job_fn *job_fn,
bsg_timeout_fn *timeout, int dd_job_size)
{
struct bsg_set *bset;
struct blk_mq_tag_set *set;
struct request_queue *q;
int ret = -ENOMEM;
bset = kzalloc_obj(*bset);
if (!bset)
return ERR_PTR(-ENOMEM);
bset->job_fn = job_fn;
bset->timeout_fn = timeout;
set = &bset->tag_set;
set->ops = &bsg_mq_ops;
set->nr_hw_queues = 1;
set->queue_depth = 128;
set->numa_node = NUMA_NO_NODE;
set->cmd_size = sizeof(struct bsg_job) + dd_job_size;
set->flags = BLK_MQ_F_BLOCKING;
if (blk_mq_alloc_tag_set(set))
goto out_tag_set;
q = blk_mq_alloc_queue(set, lim, dev);
if (IS_ERR(q)) {
ret = PTR_ERR(q);
goto out_queue;
}
blk_queue_rq_timeout(q, BLK_DEFAULT_SG_TIMEOUT);
bset->bd = bsg_register_queue(q, dev, name, bsg_transport_sg_io_fn, NULL);
if (IS_ERR(bset->bd)) {
ret = PTR_ERR(bset->bd);
goto out_cleanup_queue;
}
return q;
out_cleanup_queue:
blk_mq_destroy_queue(q);
blk_put_queue(q);
out_queue:
blk_mq_free_tag_set(set);
out_tag_set:
kfree(bset);
return ERR_PTR(ret);
}
EXPORT_SYMBOL_GPL(bsg_setup_queue);
Annotation
- Immediate include surface: `linux/bsg.h`, `linux/slab.h`, `linux/blk-mq.h`, `linux/delay.h`, `linux/scatterlist.h`, `linux/bsg-lib.h`, `linux/export.h`, `scsi/scsi_cmnd.h`.
- Detected declarations: `struct bsg_set`, `function bsg_transport_sg_io_fn`, `function bsg_teardown_job`, `function bsg_job_put`, `function bsg_job_get`, `function bsg_job_done`, `function bsg_complete`, `function bsg_map_buffer`, `function bsg_prepare_job`, `function bsg_queue_rq`.
- Atlas domain: Representative Device Path / PCIe NVMe Storage Path.
- Implementation status: pattern 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.