drivers/scsi/sg.c
Source file repositories/reference/linux-study-clean/drivers/scsi/sg.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/scsi/sg.c- Extension
.c- Size
- 72011 bytes
- Lines
- 2628
- Domain
- Driver Families
- Bucket
- drivers/scsi
- 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/fs.hlinux/kernel.hlinux/sched.hlinux/string.hlinux/mm.hlinux/errno.hlinux/mtio.hlinux/ioctl.hlinux/major.hlinux/slab.hlinux/fcntl.hlinux/init.hlinux/poll.hlinux/moduleparam.hlinux/cdev.hlinux/idr.hlinux/seq_file.hlinux/blkdev.hlinux/delay.hlinux/blktrace_api.hlinux/mutex.hlinux/atomic.hlinux/ratelimit.hlinux/uio.hlinux/cred.hscsi/scsi.hscsi/scsi_cmnd.hscsi/scsi_dbg.hscsi/scsi_device.hscsi/scsi_driver.hscsi/scsi_eh.h
Detected Declarations
struct sg_devicestruct sg_fdstruct compat_sg_req_infostruct sg_proc_deviterfunction readfunction sg_allow_accessfunction open_waitfunction sg_openfunction scsi_block_when_processing_errorsfunction sg_releasefunction openfunction get_sg_io_pack_idfunction sg_readfunction sg_new_readfunction sg_writefunction sg_new_writefunction sg_common_writefunction srp_donefunction max_sectors_bytesfunction sg_fill_request_tablefunction put_compat_request_tablefunction sg_ioctl_commonfunction list_for_each_entryfunction list_for_each_entryfunction sg_ioctlfunction sg_pollfunction sg_fasyncfunction sg_vma_faultfunction sg_mmapfunction sg_rq_end_io_usercontextfunction completedfunction sg_allocfunction sg_add_devicefunction sg_device_destroyfunction sg_remove_devicefunction def_reserved_size_setfunction init_sgfunction exit_sgfunction sg_start_reqfunction sg_finish_rem_reqfunction sg_build_sgatfunction sg_build_indirectfunction sg_remove_scatfunction sg_read_oxferfunction sg_build_reservefunction sg_link_reservefunction sg_unlink_reservefunction sg_get_rq_mark
Annotated Snippet
static const struct file_operations sg_fops = {
.owner = THIS_MODULE,
.read = sg_read,
.write = sg_write,
.poll = sg_poll,
.unlocked_ioctl = sg_ioctl,
.compat_ioctl = compat_ptr_ioctl,
.open = sg_open,
.mmap = sg_mmap,
.release = sg_release,
.fasync = sg_fasync,
};
static const struct class sg_sysfs_class = {
.name = "scsi_generic"
};
static int sg_sysfs_valid = 0;
static Sg_device *
sg_alloc(struct scsi_device *scsidp)
{
struct request_queue *q = scsidp->request_queue;
Sg_device *sdp;
unsigned long iflags;
int error;
u32 k;
sdp = kzalloc_obj(Sg_device);
if (!sdp) {
sdev_printk(KERN_WARNING, scsidp, "%s: kmalloc Sg_device "
"failure\n", __func__);
return ERR_PTR(-ENOMEM);
}
idr_preload(GFP_KERNEL);
write_lock_irqsave(&sg_index_lock, iflags);
error = idr_alloc(&sg_index_idr, sdp, 0, SG_MAX_DEVS, GFP_NOWAIT);
if (error < 0) {
if (error == -ENOSPC) {
sdev_printk(KERN_WARNING, scsidp,
"Unable to attach sg device type=%d, minor number exceeds %d\n",
scsidp->type, SG_MAX_DEVS - 1);
error = -ENODEV;
} else {
sdev_printk(KERN_WARNING, scsidp, "%s: idr "
"allocation Sg_device failure: %d\n",
__func__, error);
}
goto out_unlock;
}
k = error;
SCSI_LOG_TIMEOUT(3, sdev_printk(KERN_INFO, scsidp,
"sg_alloc: dev=%d \n", k));
sprintf(sdp->name, "sg%d", k);
sdp->device = scsidp;
mutex_init(&sdp->open_rel_lock);
INIT_LIST_HEAD(&sdp->sfds);
init_waitqueue_head(&sdp->open_wait);
atomic_set(&sdp->detaching, 0);
rwlock_init(&sdp->sfd_lock);
sdp->sg_tablesize = queue_max_segments(q);
sdp->index = k;
kref_init(&sdp->d_ref);
error = 0;
out_unlock:
write_unlock_irqrestore(&sg_index_lock, iflags);
idr_preload_end();
if (error) {
kfree(sdp);
return ERR_PTR(error);
}
return sdp;
}
static int
sg_add_device(struct device *cl_dev)
{
struct scsi_device *scsidp = to_scsi_device(cl_dev->parent);
Sg_device *sdp = NULL;
struct cdev * cdev = NULL;
int error;
unsigned long iflags;
if (!blk_get_queue(scsidp->request_queue)) {
pr_warn("%s: get scsi_device queue failed\n", __func__);
Annotation
- Immediate include surface: `linux/module.h`, `linux/fs.h`, `linux/kernel.h`, `linux/sched.h`, `linux/string.h`, `linux/mm.h`, `linux/errno.h`, `linux/mtio.h`.
- Detected declarations: `struct sg_device`, `struct sg_fd`, `struct compat_sg_req_info`, `struct sg_proc_deviter`, `function read`, `function sg_allow_access`, `function open_wait`, `function sg_open`, `function scsi_block_when_processing_errors`, `function sg_release`.
- Atlas domain: Driver Families / drivers/scsi.
- 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.