gangadhars
gangadhars

Reputation: 2728

why open call taking two arguments (struct inode *, struct file*)?

I'm implementing a Character Driver. So I'm registering file operations. When I'm registering read function i extracted minor number 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 will apply to open call too.

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

struct file definition have the reference to struct inode. So one argument is enough for open call.

My questions are:

  1. Why open have two arguments? (or) why read don't have struct inode * argument?
  2. To extract minor number in read call, i used above instruction. To find the definions and header files it took 1hr 30min to me. Is there any easy way to find the definitions of structures?
  3. How many ways we can find the reference of struct inode through struct file and what is the best way?

Upvotes: 3

Views: 2370

Answers (1)

CL.
CL.

Reputation: 180050

You must not use that construction to search for the inode; the file or even directory might have been deleted after the file was opened.

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

  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 stuff.

Upvotes: 4

Related Questions