- From kernel space upon referencing a file with
filp_open,kernel_read,file_close. - When we try to open the file (
filp_open) in kernel space, the corresponding inode dentry will be remove or not, after closing (filp_close) of file pointer.
Sample code which present in kernel space.
static int sample_test_fun(struct file *filp, void __user *_uarg, bool all_users)
{
struct sample_device_data_buffer buffer = {0};
struct file *fp;
loff_t pos = 0;
int status, i;
fp = filp_open("/opt/secure/test/secure_dir/data.txt", O_RDONLY|O_CLOEXEC, 0400);
if (IS_ERR(fp))
{
pr_err("failed to open %p %ld\n", fp, PTR_ERR(fp));
return PTR_ERR(fp);
}
printk("%s:%d:: file isize is from inode %lld\n",__FUNCTION__, __LINE__, (file_inode(fp))->i_size);
buffer.len = file_inode(fp)->i_size;
printk("%s: file size is %d \n", __FUNCTION__, buffer.len);
buffer.data = kzalloc(buffer.len, GFP_KERNEL);
if (!buffer.data)
{
printk("%s: allocation failed for size %d \n", __FUNCTION__, buffer.len);
filp_close(fp, NULL);
return -ENOMEM;
}
printk("%s:%d",__FUNCTION__, __LINE__);
status = kernel_read(fp, buffer.data, buffer.len, &pos);
printk("%s:%d read bytes %d",__FUNCTION__, __LINE__, status);
filp_close(fp, NULL);
if (status != buffer.len) {
if (status >= 0)
status = -EIO;
goto cleanup;
}
for (i=0; i < buffer.len;i++)
{
printk(" buffer data %c", buffer.data[i]);
}
}
We have tried from kernel space upon referencing a file with filp_open, kernel_read, file_close, but after file_close also still the corresponding file inode dentry is not removed.
Please find code logic above.