Re: file_operations struct vs file struct

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Mon, 2002-05-06 at 16:42, Dan Erickson wrote:
> 
> 
> In chapter 3 of Linux Device Drivers 2nd Edition it says
> An open device is identified internally by a file structure, 
> and the kernel uses the file_operations structure to access the driver's functions.
> 	
> 	My first question would be, when it says " identified internally 
> by a file structure", is is refering to *the* file structure, as in 
> "struct file", or is it refering to the file_operations structure?
> 

As I understand it, the "struct file" structure is what the kernel uses
to identify each open file. The "struct file_operations" actually tags
along as a member of struct file. In addition, struct file contains
fields which describe other attributes of the open file, such as whether
the file is open for read, write or both, whether read/write operations
should block, the current reading/writing position in the file, the
current owner of the file, the inode structure associated with the file,
etc. The file_operations structure only stores pointers to the functions
(open, read, write, etc) associated with that file. 

> 
>  	Also, since I dont seem to understand the basic workings of 
> either, could somebody give me a quick overview of both, describing it in 
> a diffrent way then LDD did.
> 

For device drivers, you only need to create a file_operations structure
somewhat like this:
struct file_operations my_fileops = {
	open: my_open,
	release: my_release,
	read: my_read,
	write: my_write,
	.
	.
	.
};

Then when you register a character device, for example, you pass a
pointer to your file_operations structure, my_fileops, in the
register_chrdev() function. 
(ie, register_chrdev(0,"my_chrdev",&my_fileops); )

I don't know the exact details of what happens now, but apparently the
kernel takes the pointer to my_fileops and sticks it into a box labeled
"my_chrdev" and stores it for later access. (I'm being very abstract now
;-) When a program calls open() on your device (which you would probably
create a device node for in /dev/my_chrdev), the kernel creates a struct
file for the device, grabs the struct file_operations pointer from the
box, and sticks it into the struct file. The kernel would then call the
open() method you declared in the file_operations structure, passing the
file structure as one of the arguments. Other system calls on the
device, like read, write, ioctl, and others, work similarly, with the
kernel calling the related methods in struct file_operations.

So basically, you are responsible for creating the file_operations
structure (and the associated open, write, read functions; you don't
actually have to implement them all), and the kernel handles struct
file. The rest of the story is better explained in LDD :-)

> 	I appolagize for these questions as I didnt know how to word the 
> quite properly.
> 
> Thanks

-- 
Trevor Hamm

--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/kernelnewbies/
FAQ:           http://kernelnewbies.org/faq/


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux