fs/kernel_read_file.c
Source file repositories/reference/linux-study-clean/fs/kernel_read_file.c
File Facts
- System
- Linux kernel
- Corpus path
fs/kernel_read_file.c- Extension
.c- Size
- 4456 bytes
- Lines
- 186
- Domain
- Core OS
- Bucket
- VFS And Filesystem Core
- Inferred role
- Core OS: exported/initcall integration point
- Status
- integration implementation candidate
Why This File Exists
Core operating-system implementation surface: boot, tasks, memory, VFS, syscall-facing interfaces, synchronization, credentials, and isolation.
- Core operating-system implementation surface: boot, tasks, memory, VFS, syscall-facing interfaces, synchronization, credentials, and isolation.
- Exports symbols or registers init work; inspect boot/module ordering and who consumes the exported contract.
- 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/fs.hlinux/fs_struct.hlinux/kernel_read_file.hlinux/security.hlinux/vmalloc.h
Detected Declarations
function kernel_read_filefunction kernel_read_file_from_pathfunction kernel_read_file_from_path_initnsfunction kernel_read_file_from_fdexport kernel_read_fileexport kernel_read_file_from_pathexport kernel_read_file_from_path_initnsexport kernel_read_file_from_fd
Annotated Snippet
if (bytes < 0) {
ret = bytes;
goto out_free;
}
if (bytes == 0)
break;
copied += bytes;
}
if (whole_file) {
if (pos != i_size) {
ret = -EIO;
goto out_free;
}
ret = security_kernel_post_read_file(file, *buf, i_size, id);
}
out_free:
if (ret < 0) {
if (allocated) {
vfree(*buf);
*buf = NULL;
}
}
out:
allow_write_access(file);
return ret == 0 ? copied : ret;
}
EXPORT_SYMBOL_GPL(kernel_read_file);
ssize_t kernel_read_file_from_path(const char *path, loff_t offset, void **buf,
size_t buf_size, size_t *file_size,
enum kernel_read_file_id id)
{
struct file *file;
ssize_t ret;
if (!path || !*path)
return -EINVAL;
file = filp_open(path, O_RDONLY, 0);
if (IS_ERR(file))
return PTR_ERR(file);
ret = kernel_read_file(file, offset, buf, buf_size, file_size, id);
fput(file);
return ret;
}
EXPORT_SYMBOL_GPL(kernel_read_file_from_path);
ssize_t kernel_read_file_from_path_initns(const char *path, loff_t offset,
void **buf, size_t buf_size,
size_t *file_size,
enum kernel_read_file_id id)
{
struct file *file;
struct path root;
ssize_t ret;
if (!path || !*path)
return -EINVAL;
task_lock(&init_task);
get_fs_root(init_task.fs, &root);
task_unlock(&init_task);
file = file_open_root(&root, path, O_RDONLY, 0);
path_put(&root);
if (IS_ERR(file))
return PTR_ERR(file);
ret = kernel_read_file(file, offset, buf, buf_size, file_size, id);
fput(file);
return ret;
}
EXPORT_SYMBOL_GPL(kernel_read_file_from_path_initns);
ssize_t kernel_read_file_from_fd(int fd, loff_t offset, void **buf,
size_t buf_size, size_t *file_size,
enum kernel_read_file_id id)
{
CLASS(fd, f)(fd);
if (fd_empty(f) || !(fd_file(f)->f_mode & FMODE_READ))
return -EBADF;
return kernel_read_file(fd_file(f), offset, buf, buf_size, file_size, id);
Annotation
- Immediate include surface: `linux/fs.h`, `linux/fs_struct.h`, `linux/kernel_read_file.h`, `linux/security.h`, `linux/vmalloc.h`.
- Detected declarations: `function kernel_read_file`, `function kernel_read_file_from_path`, `function kernel_read_file_from_path_initns`, `function kernel_read_file_from_fd`, `export kernel_read_file`, `export kernel_read_file_from_path`, `export kernel_read_file_from_path_initns`, `export kernel_read_file_from_fd`.
- Atlas domain: Core OS / VFS And Filesystem Core.
- Implementation status: integration implementation candidate.
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.