drivers/block/ublk_drv.c
Source file repositories/reference/linux-study-clean/drivers/block/ublk_drv.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/block/ublk_drv.c- Extension
.c- Size
- 152736 bytes
- Lines
- 5916
- 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.
- Touches user memory; correctness depends on fault-safe copying and privilege boundary handling.
- 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/module.hlinux/moduleparam.hlinux/sched.hlinux/fs.hlinux/pagemap.hlinux/file.hlinux/stat.hlinux/errno.hlinux/major.hlinux/wait.hlinux/blkdev.hlinux/init.hlinux/swap.hlinux/slab.hlinux/compat.hlinux/mutex.hlinux/writeback.hlinux/completion.hlinux/highmem.hlinux/sysfs.hlinux/miscdevice.hlinux/falloc.hlinux/uio.hlinux/ioprio.hlinux/sched/mm.hlinux/uaccess.hlinux/cdev.hlinux/io_uring/cmd.hlinux/blk-mq.hlinux/delay.hlinux/mm.hasm/page.h
Detected Declarations
struct ublk_batch_fetch_cmdstruct ublk_uring_cmd_pdustruct ublk_batch_io_datastruct ublk_iostruct ublk_queuestruct ublk_buf_rangestruct ublk_devicestruct ublk_params_headerstruct ublk_zoned_report_descstruct ublk_batch_io_iterstruct count_busyenum auto_buf_reg_resfunction ublk_dev_support_batch_iofunction ublk_support_batch_iofunction ublk_io_lockfunction ublk_io_unlockfunction ublk_io_evts_initfunction ublk_io_evts_emptyfunction ublk_io_evts_deinitfunction ublk_get_iodfunction ublk_support_zero_copyfunction ublk_dev_support_zero_copyfunction ublk_support_shmem_zcfunction ublk_iod_is_shmem_zcfunction ublk_dev_support_shmem_zcfunction ublk_support_auto_buf_regfunction ublk_dev_support_auto_buf_regfunction ublk_support_user_copyfunction ublk_dev_support_user_copyfunction ublk_dev_is_zonedfunction ublk_queue_is_zonedfunction ublk_dev_support_integrityfunction ublk_req_build_flagsfunction ublk_init_iodfunction ublk_zoned_insert_report_descfunction ublk_get_nr_zonesfunction ublk_revalidate_disk_zonesfunction ublk_dev_param_zoned_validatefunction ublk_dev_param_zoned_applyfunction ublk_report_zonesfunction ublk_setup_iod_zonedfunction ublk_dev_param_zoned_validatefunction ublk_dev_param_zoned_applyfunction ublk_setup_iod_zonedfunction ublk_batch_alloc_fcmdfunction ublk_batch_free_fcmdfunction __ublk_release_fcmdfunction ublk_batch_deinit_fetch_buf
Annotated Snippet
static const struct blk_mq_ops ublk_mq_ops = {
.queue_rq = ublk_queue_rq,
.queue_rqs = ublk_queue_rqs,
.init_hctx = ublk_init_hctx,
.timeout = ublk_timeout,
};
static const struct blk_mq_ops ublk_batch_mq_ops = {
.commit_rqs = ublk_commit_rqs,
.queue_rq = ublk_batch_queue_rq,
.queue_rqs = ublk_batch_queue_rqs,
.init_hctx = ublk_init_hctx,
.timeout = ublk_timeout,
};
static void ublk_queue_reinit(struct ublk_device *ub, struct ublk_queue *ubq)
{
int i;
ubq->nr_io_ready = 0;
for (i = 0; i < ubq->q_depth; i++) {
struct ublk_io *io = &ubq->ios[i];
/*
* UBLK_IO_FLAG_CANCELED is kept for avoiding to touch
* io->cmd
*/
io->flags &= UBLK_IO_FLAG_CANCELED;
io->cmd = NULL;
io->buf.addr = 0;
/*
* old task is PF_EXITING, put it now
*
* It could be NULL in case of closing one quiesced
* device.
*/
if (io->task) {
put_task_struct(io->task);
io->task = NULL;
}
WARN_ON_ONCE(refcount_read(&io->ref));
WARN_ON_ONCE(io->task_registered_buffers);
}
}
static int ublk_ch_open(struct inode *inode, struct file *filp)
{
struct ublk_device *ub = container_of(inode->i_cdev,
struct ublk_device, cdev);
if (test_and_set_bit(UB_STATE_OPEN, &ub->state))
return -EBUSY;
filp->private_data = ub;
ub->ublksrv_tgid = current->tgid;
return 0;
}
static void ublk_reset_ch_dev(struct ublk_device *ub)
{
int i;
for (i = 0; i < ub->dev_info.nr_hw_queues; i++) {
struct ublk_queue *ubq = ublk_get_queue(ub, i);
/* Sync with ublk_cancel_cmd() */
spin_lock(&ubq->cancel_lock);
ublk_queue_reinit(ub, ubq);
spin_unlock(&ubq->cancel_lock);
}
/* set to NULL, otherwise new tasks cannot mmap io_cmd_buf */
ub->mm = NULL;
ub->nr_queue_ready = 0;
ub->unprivileged_daemons = false;
ub->ublksrv_tgid = -1;
}
static struct gendisk *ublk_get_disk(struct ublk_device *ub)
{
struct gendisk *disk;
spin_lock(&ub->lock);
disk = ub->ub_disk;
if (disk)
get_device(disk_to_dev(disk));
spin_unlock(&ub->lock);
Annotation
- Immediate include surface: `linux/module.h`, `linux/moduleparam.h`, `linux/sched.h`, `linux/fs.h`, `linux/pagemap.h`, `linux/file.h`, `linux/stat.h`, `linux/errno.h`.
- Detected declarations: `struct ublk_batch_fetch_cmd`, `struct ublk_uring_cmd_pdu`, `struct ublk_batch_io_data`, `struct ublk_io`, `struct ublk_queue`, `struct ublk_buf_range`, `struct ublk_device`, `struct ublk_params_header`, `struct ublk_zoned_report_desc`, `struct ublk_batch_io_iter`.
- Atlas domain: Driver Families / drivers/block.
- Implementation status: pattern implementation candidate.
- This snippet crosses the user/kernel memory boundary; validate fault handling and access checks before translating the pattern.
- 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.