0

I'm trying to write a driver with a Linux kernel module. I created a special file, with mknod, called "newfile". I'm executed chmod on that file to enable writing permission, and I put 777. I'm trying to write on this file whit this command from terminal: echo "Writing on file" > newfile but I got an error, and from my driver I got -EINVAL in vfs_write. This is my code:

static ssize_t device_write(struct file *flip, const char *buffer, size_t len, loff_t *offset) {

    printk(KERN_INFO "writing on file; buffer is: %s   len is: %d    offest is:  %lld \n",buffer,len,offset);


    return file_write(flip, offset,buffer,len); 
}




int file_write(struct file *file, unsigned long long offset, unsigned char *data, unsigned int size) 
{
//mm_segment_t oldfs;
    int ret;

    //oldfs = get_fs();
    //set_fs(get_ds());

    ret = vfs_write(file, data, size, &offset);

    if(ret == -EINVAL)
    printk(KERN_INFO "is EINVAL!");

    else
     printk(KERN_INFO "not EINVAL!");
 return ret;
}

Where is the problem?I can't understand

Claudio Santoro
  • 69
  • 2
  • 11
  • `vfs_write` asks your driver to write to your file, that is you `file_write` is called as a part of it. So it is not clear what do you want to achieve by calling `vfs_write` in your function. – Tsyvarev Oct 21 '19 at 21:58
  • So vfs_write will call device_write?I could write something on a file, are there other functions? – Claudio Santoro Oct 22 '19 at 11:17
  • "So vfs_write will call device_write?" - Yes, assuming you register `device_write` function as a `.write` operation for your file. "I could write something on a file, are there other functions?" - If you want to read/write the file outside of the driver for that file, then see that question: https://stackoverflow.com/questions/1184274/read-write-files-within-a-linux-kernel-module. – Tsyvarev Oct 22 '19 at 12:15
  • Yes, I took my functions from that question, but it calls vfs_write inside the device_write, I'm doing the same action – Claudio Santoro Oct 22 '19 at 13:06
  • In you question post you show two functions, and we can only **guess** how you (and your module) uses them. The signature of `device_write` function suggests that is is used as the file's `.write` operation, but even after two of my comments (and your responses) I am still not sure about that. Please, provide (add it to the question post) **more detailed code** and description for it. Ideally, we want the question to contain [mcve], so everyone can understand what you are doing and, what is also important, try to reproduce your problem. – Tsyvarev Oct 22 '19 at 14:02
  • Yes, device_write is the write operation of my driver, I've all functions of that questions but first I'm trying to write on my file. The problem I've got is that when I try to write on file with echo, I get -EINVAL from vfs_write. Where could be the problem? – Claudio Santoro Oct 22 '19 at 15:23
  • I also tried executing that with a user program which tries to write on that file, but I got same problem – Claudio Santoro Oct 22 '19 at 15:25
  • When I log dmesg, print on device_write appears only one time, I think that vfs_write it's not calling again my device_write – Claudio Santoro Oct 22 '19 at 16:21
  • So, returning to the my first comment: There is **no sense** to call `vfs_write` from your driver `.write` function. I don't know why your code doesn't crash with infinite recursion. It could be, that `vfs_write` has some protection against recursive calls. If you want to ask a proper for doing things, then you need to formulate what do you want. Because the code you show doesn't reveal your intentions. – Tsyvarev Oct 22 '19 at 17:24
  • Ok, maybe I don't explain my problem: I want simply write on a file that I created with mknod. So, what I have to do?I wrote a user program which opens this file and tries to write on that; now I added copy_from_user in my driver but I got-EFAULT. If I don't have to call vfs_write, how could I write on my flip? – Claudio Santoro Oct 25 '19 at 18:15
  • It seems you misunderstood the meaning of the file "creation" in the Linux kernel. When create a file by defining its `file_operations` you provide a **semantic** of writing the file, reading it, and other operations with the file. You may want a `write` to store written string for read it later. This is what "normal" filesystems do. But you may want just to `printk` the written file, or perform some action based on the written string. Or you may ignore the write completely by rerurning `len` argument. So the phrase "I want simply write on a file" means nothing. Like "I want simply to go". – Tsyvarev Oct 25 '19 at 21:02
  • So I can only define its semantic?I cannot explicity write on that file? – Claudio Santoro Oct 28 '19 at 12:57
  • Problem is that my teacher didn't explain anything and he doesn't help me, so I have confusion about that. if I wrote something with a user program, and I want call my driver and store what user wrote, I only can save that in a buffer/write on some file?I can't write directly on struct file* that is parameter of my device_write? – Claudio Santoro Oct 28 '19 at 13:06
  • You may define a semantic and program it too. But if you want us to help you in programming, you need to tell us which semantic you want to implement. Think of writting into a file like a "pushing a button". Your driver(module) is responsible for some button and implements the action(s) happened when a user pushes given button. That action(s) could be "turn on the light", "play a music", etc. Inside the actions you may push some **other** button (that is, write to some other file) with `vfs_write`. But it has a little sense to push your button from your code, what currently you are doing. – Tsyvarev Oct 28 '19 at 13:22
  • I have to implement access to file session-based with my driver; I have a directory dirA which some files, and another directory called dirB. When I open a file in dirB, I have to read the content of the corresponding file in dirA, and a user can write on file but he has to see only his modifies until he closes the file. So, when user open a file in dirB, he has to read the file content like is in dirA. – Claudio Santoro Oct 28 '19 at 14:47
  • Here is my problem: I want write something on this file in dirB and after I want save all, writing in the corresponding file in dirA. So, when I try to write something on file in dirB, I can't understand if I have to do vfs_write/kernel_write on filp parameter of device_write or if I have to save all in a buffer and write all on file in dirA. – Claudio Santoro Oct 28 '19 at 14:47
  • So, inside my function: int file_write(struct file *file, unsigned long long offset, unsigned char *data, unsigned int size) I think I can't do vfs_write on struct file* file because I can't write on this file because like you said it's only a "button", it's correct? – Claudio Santoro Oct 28 '19 at 14:49
  • Look into your own **question post**: Given its current content, how should we (who read your post) *guess* that your `vfs_write` call is targeted to **another** file? How should we *guess* about the information your write in your last two comments?? Please, [edit] your question post and include all relevant information about the problem into it. Then check that your question post becomes **understandable without** reading the **comments**. – Tsyvarev Oct 28 '19 at 14:57
  • I didn't know that vfs_write was targeted to another file, I just was asking if I can execute vfs_write on the same struct file* psrameter or if I had to do that on another struct file*; I think all these details are useless with my doubt – Claudio Santoro Oct 29 '19 at 17:11

0 Answers0