I am trying to overwrite all the data in a file in a kernel system call (Yes I know I shouldnt do a file read/write in kernel but I am just trying it out! ) based on the answer here How to read/write files within a Linux kernel module?
Here is the code:
int sys_mycompleteerase(const char __user * inputFileUser) {
struct file* filp = NULL;
int err = 0,count = 0;
unsigned long long offset =0;
mm_segment_t old_fs;
old_fs = get_fs();
set_fs(KERNEL_DS);
filp = filp_open(inputFileUser, O_WRONLY, 0644);
if(IS_ERR(filp)) {
err = PTR_ERR(filp);
return NULL;
}
do {
count = vfs_write(filp," ",sizeof(" "), &offset);
offset+=sizeof(" ");
}while(count > 0);
filp_close(filp, NULL);
set_fs(old_fs);
return 0;
}
If I give a proper file name in the user space program, it just keeps on writing without stopping. Why?