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:
- Why does
open
have two parameters? (Or) whydoesn't read
havea struct inode *
parameter? - 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? - How many ways can we find references
to struct inodes
throughstruct 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
In your
open
function, you look up your own data from the inode (or allocate your own data) and setthe file->private_data
pointer; andIn your
read
function, you then usefile->private_data
to access your own content.