Linux – Why does the open call require two parameters (struct inode *, struct file*)?

Why does the open call require two parameters (struct inode *, struct file*)?… here is a solution to the problem.

Why does the open call require two parameters (struct inode *, struct file*)?

I’m implementing a role driver. So I’m registering file operations. When I register the read function, I extract the minor number in this way

     myread(struct file * file, char __user * ubuf, size_t lbuf, loff_t *offset)
     {
         int minor;

minor = MINOR(file->f_path.dentry->d_inode->f_pos->i_rdev);
         .......

This rule also applies to open calls.

     myopen(struct inode * inode, struct file * file)

The struct file definition references struct inode. So for the open call, one parameter is sufficient.

My question is:

  1. Why does open have two parameters? (Or) why doesn't read have a struct inode * parameter?
  2. To extract the secondary number in the read call, I used the directive above. It took me 1 hour and 30 minutes to find the definition and header files. Is there any easy way to find the definition of structure?
  3. How many ways can we find references to struct inodes through struct files, and which way is best?

Solution

You cannot use this construct to search for inodes; After the file is opened, the file or even the directory may have been deleted.

The kernel convention (see chapter 3 of Linux Device Drivers) is

  1. In your open function, you look up your own data from the inode (or allocate your own data) and set the file->private_data pointer; and

  2. In your read function, you then use file->private_data to access your own content.

Related Problems and Solutions