Access File in Kernel
Table of Contents
1 How to Read/Write File in Kernel
In generally, you should not open and read/write a file from kernel space, but in same case you really have to, please refrence the following example:
static int virtio_file_write(char *path, uint8_t *buf, uint32_t size)
{
struct file *fp;
int ret = 0;
fp = filp_open(path, O_RDWR | O_CREAT, 0644);
if (IS_ERR(fp)) {
ret = PTR_ERR(fp);
}
else {
loff_t pos = 0;
ret = kernel_write(fp, buf, size, &pos);
if (ret < 0)
VIRTIO_DRM_ERROR("Error writing plane data");
filp_close(fp, NULL);
}
return ret;
}