io_uring/mock_file.c
Source file repositories/reference/linux-study-clean/io_uring/mock_file.c
File Facts
- System
- Linux kernel
- Corpus path
io_uring/mock_file.c- Extension
.c- Size
- 8470 bytes
- Lines
- 352
- Domain
- Kernel Services
- Bucket
- io_uring
- Inferred role
- Kernel Services: operation-table or driver-model contract
- Status
- pattern implementation candidate
Why This File Exists
Shared kernel service surface used by multiple subsystems, including helpers, cryptography, virtualization support, and async I/O infrastructure.
- Shared kernel service surface used by multiple subsystems, including helpers, cryptography, virtualization support, and async I/O infrastructure.
- 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/device.hlinux/init.hlinux/kernel.hlinux/miscdevice.hlinux/module.hlinux/anon_inodes.hlinux/ktime.hlinux/hrtimer.hlinux/poll.hlinux/io_uring/cmd.hlinux/io_uring_types.huapi/linux/io_uring/mock_file.h
Detected Declarations
struct io_mock_iocbstruct io_mock_filefunction io_copy_regbuffunction io_cmd_copy_regbuffunction io_mock_cmdfunction io_mock_rw_timer_expiredfunction io_mock_delay_rwfunction io_mock_read_iterfunction io_mock_write_iterfunction io_mock_llseekfunction io_mock_pollfunction io_mock_releasefunction io_create_mock_filefunction io_probe_mockfunction iou_mock_mgr_cmdfunction io_mock_initfunction io_mock_exitmodule init io_mock_init
Annotated Snippet
static const struct file_operations io_mock_fops = {
.owner = THIS_MODULE,
.release = io_mock_release,
.uring_cmd = io_mock_cmd,
.read_iter = io_mock_read_iter,
.write_iter = io_mock_write_iter,
.llseek = io_mock_llseek,
};
static const struct file_operations io_mock_poll_fops = {
.owner = THIS_MODULE,
.release = io_mock_release,
.uring_cmd = io_mock_cmd,
.read_iter = io_mock_read_iter,
.write_iter = io_mock_write_iter,
.llseek = io_mock_llseek,
.poll = io_mock_poll,
};
#define IO_VALID_CREATE_FLAGS (IORING_MOCK_CREATE_F_SUPPORT_NOWAIT | \
IORING_MOCK_CREATE_F_POLL)
static int io_create_mock_file(struct io_uring_cmd *cmd, unsigned int issue_flags)
{
const struct file_operations *fops = &io_mock_fops;
const struct io_uring_sqe *sqe = cmd->sqe;
struct io_uring_mock_create mc, __user *uarg;
struct file *file;
struct io_mock_file *mf __free(kfree) = NULL;
size_t uarg_size;
/*
* It's a testing only driver that allows exercising edge cases
* that wouldn't be possible to hit otherwise.
*/
add_taint(TAINT_TEST, LOCKDEP_STILL_OK);
uarg = u64_to_user_ptr(READ_ONCE(sqe->addr));
uarg_size = READ_ONCE(sqe->len);
if (sqe->ioprio || sqe->__pad1 || sqe->addr3 || sqe->file_index)
return -EINVAL;
if (uarg_size != sizeof(mc))
return -EINVAL;
memset(&mc, 0, sizeof(mc));
if (copy_from_user(&mc, uarg, uarg_size))
return -EFAULT;
if (!mem_is_zero(mc.__resv, sizeof(mc.__resv)))
return -EINVAL;
if (mc.flags & ~IO_VALID_CREATE_FLAGS)
return -EINVAL;
if (mc.file_size > SZ_1G)
return -EINVAL;
if (mc.rw_delay_ns > NSEC_PER_SEC)
return -EINVAL;
mf = kzalloc_obj(*mf, GFP_KERNEL_ACCOUNT);
if (!mf)
return -ENOMEM;
init_waitqueue_head(&mf->poll_wq);
mf->size = mc.file_size;
mf->rw_delay_ns = mc.rw_delay_ns;
if (mc.flags & IORING_MOCK_CREATE_F_POLL) {
fops = &io_mock_poll_fops;
mf->pollable = true;
}
FD_PREPARE(fdf, O_RDWR | O_CLOEXEC,
anon_inode_create_getfile("[io_uring_mock]", fops, mf,
O_RDWR | O_CLOEXEC, NULL));
if (fdf.err)
return fdf.err;
retain_and_null_ptr(mf);
file = fd_prepare_file(fdf);
file->f_mode |= FMODE_READ | FMODE_CAN_READ | FMODE_WRITE |
FMODE_CAN_WRITE | FMODE_LSEEK;
if (mc.flags & IORING_MOCK_CREATE_F_SUPPORT_NOWAIT)
file->f_mode |= FMODE_NOWAIT;
mc.out_fd = fd_prepare_fd(fdf);
if (copy_to_user(uarg, &mc, uarg_size))
return -EFAULT;
fd_publish(fdf);
return 0;
}
Annotation
- Immediate include surface: `linux/device.h`, `linux/init.h`, `linux/kernel.h`, `linux/miscdevice.h`, `linux/module.h`, `linux/anon_inodes.h`, `linux/ktime.h`, `linux/hrtimer.h`.
- Detected declarations: `struct io_mock_iocb`, `struct io_mock_file`, `function io_copy_regbuf`, `function io_cmd_copy_regbuf`, `function io_mock_cmd`, `function io_mock_rw_timer_expired`, `function io_mock_delay_rw`, `function io_mock_read_iter`, `function io_mock_write_iter`, `function io_mock_llseek`.
- Atlas domain: Kernel Services / io_uring.
- 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.