File Operation file inode Concept

Source: Internet
Author: User
Tags sendfile

File_operation (File Operations) file (File) inode (node) to-dish blog | embedded online blog homepage | embedded online homepage | help |

File_operation is the key data structure associated with system calls and drivers. Each member of this structure corresponds to a system call. Read the corresponding function pointer in file_operation, and then transfer the control to the function to complete the Linux Device Driver.

In the system, access operations on I/O devices are performed through specific entry points, which are provided by the device driver. Generally, the driver interface of this set of devices is explained to the system by the file_operations structure, which is defined in include/Linux/fs. h.
Traditionally, a file_operation structure or a pointer is called FOPS (or some of its variants ). each member in the structure must point to a function in the driver. These functions perform a special operation or leave unsupported operations as null. when a null pointer is specified, the exact behavior of the kernel is different for each function.
When you read the list of file_operations methods, you will notice that many parameters contain the string _ User. this annotation is a document form. Note that a pointer is a user space address that cannot be directly referenced. for normal compilation,
_ User has no effect, but it can be used by external check software to find out the incorrect use of the user space address.
---------------------------------------------------------------------
Registering a device number is only the first of many tasks required by the driver code. First of all, we need to involve a majority of basic driver operations, including three important kernel data structures, called file_operations, file, and inode. You need to have a basic understanding of these structures to do a lot of things of interest.
Struct
File_operations is the link between driver operations and device numbers by a character device. It is a collection of pointers. Each opened file corresponds to a series of operations. This is file_operations, used to execute a series of system calls.
Struct
File indicates an open file, which is created when the open operation in file_operation is executed. Note the difference between file and inode pointer in the user space, the file pointer is defined by the C library in the user space. Itpub personal space MPD + L: R6 [T # ZK: u
Struct inode is used by the kernel to represent a file. Note the difference with struct file. One struct inode represents a file, struct
File indicates an opened file.
/F/MB: Y j9e c0struct inode includes two important members: itpub personal space 6e ''' | 4 @ _ p? 0u V;
Dev_t I _rdev Device File device number
U5ydjn A) x m0struct cdev * I _cdev represents the data structure of the character device itpub personal space 4ear3rn t4p0u7z8 {
The struct inode structure is used to represent files in the kernel. The same file can be opened many times, so it can correspond to many struct files, but only one struct
Inode.
Struct file_operations {struct module * owner; loff_t (* llseek)
(Struct file *, loff_t, INT); ssize_t (* read) (struct file *, char _ User
*, Size_t, loff_t *); ssize_t (* aio_read) (struct kiocb *, char _ User *,
Size_t, loff_t); ssize_t (* write) (struct file *, const char _ User *,
Size_t, loff_t *); ssize_t (* aio_write) (struct kiocb *, const char _ User
*, Size_t, loff_t); int (* readdir) (struct file *, void *, filldir_t );
Unsigned int (* poll) (struct file *, struct poll_table_struct *); int
(* IOCTL) (struct inode *, struct file *, unsigned int, unsigned long); long
(* Unlocked_ioctl) (struct file *, unsigned int, unsigned long); long
(* Compat_ioctl) (struct file *, unsigned int, unsigned long); int (* MMAP)
(Struct file *, struct vm_area_struct *); int (* open) (struct inode *,
Struct file *); int (* flush) (struct file *, fl_owner_t ID); int
(* Release) (struct inode *, struct file *); int (* fsync) (struct file *,
Struct dentry *, int datasync); int (* aio_fsync) (struct kiocb *, int
Datasync); int (* fasync) (INT, struct file *, INT); int (* Lock)
(Struct file *, Int, struct file_lock *); ssize_t (* readv) (struct file *,
Const struct iovec *, unsigned long, loff_t *); ssize_t (* writev) (struct
File *, const struct iovec *, unsigned long, loff_t *); ssize_t (* sendfile)
(Struct file *, loff_t *, size_t, read_actor_t, void *); ssize_t
(* Sendpage) (struct file *, struct page *, Int, size_t, loff_t *, INT );
Unsigned long (* get_unmapped_area) (struct file *, unsigned long, unsigned long,
Unsigned long, unsigned long); int (* check_flags) (INT); int
(* Dir_notify) (struct file * filp, unsigned long Arg); int (* flock) (struct
File *, Int, struct file_lock *); ssize_t (* splice_write) (struct
Pipe_inode_info *, struct file *, loff_t *, size_t, unsigned INT); ssize_t
(* Splice_read) (struct file *, loff_t *, struct pipe_inode_info *, size_t,
Unsigned INT) ;}; the data structure of file_operations is as follows:
Struct module * owner
The first file_operations member is not an operation at all; it is a pointer to a module with this structure. this member is used to prevent the module from being detached when its operations are still in use. in almost all time, It is initialized as this_module, a macro defined in.
Loff_t (* llseek) (struct file *, loff_t, INT );
The llseek method is used to change the current read/write position in the file, and the new position is used as the (positive) return value. The loff_t parameter is a "Long
Offset ", and at least 64 bit width even on the 32-bit platform. The error is indicated by a negative return value. If this function pointer is null,
Seek calls the location counter in the file structure in a way that is potentially unpredictable (described in the "file structure" section ).
Ssize_t (* read) (struct file *, char _ User *, size_t, loff_t *);
This is used to obtain data from the device. a null pointer at this location causes the read system to call-einval ("invalid
Argument ") failed. A non-negative return value indicates the number of bytes successfully read (the return value is a" signed size "type, often a local Integer type on the target platform ).
Ssize_t (* aio_read) (struct kiocb *, char _ User *, size_t, loff_t );
Initialize an asynchronous read operation -- read operations that may not end before the function returns. If this method is null, all operations will be performed in place of read (synchronous ).
Ssize_t (* write) (struct file *, const char _ User *, size_t, loff_t *);
Send data to the device. If null,-einval is returned to the program called by the write system. If not negative, the returned value indicates the number of bytes successfully written.
Ssize_t (* aio_write) (struct kiocb *, const char _ User *, size_t, loff_t *);
Initializes an asynchronous write on the device.
INT (* readdir) (struct file *, void *, filldir_t );
The member of the device file should be null. It is used to read the Directory and is only useful to the file system.
Unsigned int (* poll) (struct file *, struct poll_table_struct *);
The poll method is the backend of three system calls: poll, epoll, and select. It is used to query whether reading or writing to one or more file descriptors will be blocked.
The poll method should return a single bit mask to indicate whether non-blocking read or write operations are possible, and, possibly, provide the kernel information to make the calling process sleep until I/O becomes possible. if the poll method of a driver is null, the device is assumed to be readable and writable without blocking.
INT (* IOCTL) (struct inode *, struct file *, unsigned int, unsigned long );
The IOCTL system provides a method for issuing device-specific commands (for example, formatting a floppy disk, which is neither read nor write ). in addition, several IOCTL commands are identified by the kernel without reference to the FoPs table. if the device does not provide the ioctl method,
"IOCTL does not exist on the device"). An error is returned when the system calls the device.
INT (* MMAP) (struct file *, struct vm_area_struct *);
MMAP is used to map the device memory to the address space of the process. If this method is null, The MMAP system will return-enodev.
INT (* open) (struct inode *, struct file *);
Although this is often the first operation on the device file, the driver is not required to declare a corresponding method. if this item is null, the device is successfully enabled, but your driver will not be notified.
INT (* flush) (struct file *);
The flush operation is called when a process disables the copy of its device file descriptor; it should perform (and wait) any unfinished operations on the device. this must not be confused with the fsync operation of the user query request. current,
Flush is rarely used in drivers;
The SCSI tape driver uses it. For example, to ensure that all written data is written to the tape before the device is closed. If flush is null, the kernel simply ignores user application requests.
INT (* release) (struct inode *, struct file *);
This operation is referenced when the file structure is released. Like open, release can be null.
INT (* fsync) (struct file *, struct dentry *, INT );
This method is the backend of the fsync system call. You can call this method to refresh any hanging data. if the pointer is null, the system will return-einval.
INT (* aio_fsync) (struct kiocb *, INT );
This is the asynchronous version of The fsync method.
INT (* fasync) (INT, struct file *, INT );
This operation is used to notify the device of changes to its fasync flag. asynchronous notification is an advanced topic, which is described in Chapter 6th. This member can be null if the driver does not support asynchronous notification.
INT (* Lock) (struct file *, Int, struct file_lock *);
The lock method is used to implement File Locking. Locking is essential to conventional files, but the device driver never implements it.
Ssize_t (* readv) (struct file *, const struct iovec *, unsigned long, loff_t *);
Ssize_t (* writev) (struct file *, const struct iovec *, unsigned long, loff_t
*);
These methods implement divergence/aggregation read and write operations. applications occasionally need to perform a single read or write operation that contains multiple memory areas. These system calls allow them to do so without having to copy additional data. if these function pointers are null,
The read and write methods are called (more than once ).
Ssize_t (* sendfile) (struct file *, loff_t *, size_t, read_actor_t, void *);
This method is used to implement the reading of sendfile system calls, and the data is moved from one file descriptor to another using the minimum copy. for example, it is used by a web server that needs to send file content to a network connection. device Drivers often set sendfile to null.
Ssize_t (* sendpage) (struct file *, struct page *, Int, size_t, loff_t *, INT );
Sendpage is the other half of sendfile. It is called by the kernel to send data, one page at a time, to the corresponding file. The device driver does not actually implement sendpage.
Unsigned long (* get_unmapped_area) (struct file *, unsigned long, unsigned long,
Unsigned long, unsigned long );
The purpose of this method is to find a suitable location in the address space of the process to map it To the memory segment on the underlying device. this task is usually performed by Memory Management Code. This method is used to enable the driver to force any alignment requests that may exist on special devices. most drivers can set this method to null.
INT (* check_flags) (INT) This method allows the module to check the flag passed to fnctl (f_setfl...) for calling.
INT (* dir_notify) (struct file *, unsigned long); this method is called when the application uses fcntl to request Directory change notifications. it is only useful to the file system; the driver does not need to implement dir_policy.
---------------------------------------------------------------------
Struct file {/** fu_list becomes invalid after file_free is called
And queued via * fu_rcuhead for RCU freeing */Union {
Struct list_head fu_list; struct rcu_head fu_rcuhead ;}
F_u; struct dentry * f_dentry; struct vfsmount * f_vfsmnt;
Const struct file_operations * f_op; atomic_t f_count;
Unsigned int f_flags; mode_t f_mode; loff_t
F_pos; struct fown_structf_owner; unsigned int
F_uid, f_gid; struct file_ra_statef_ra; unsigned long
F_version; void * f_security;/* needed for tty driver,
And maybe others */void * private_data; # ifdef config_epoll
/* Used by fs/eventpoll. C to link all the hooks to this file */struct
List_head f_ep_links; spinlock_t f_ep_lock; # endif/* # ifdef
Config_epoll */struct address_space * f_mapping ;};
A file struct represents an opened file. Each opened file in the system has an associated struct in the kernel space.
File. It is created by the kernel when the file is opened and passed to any function that operates on the file. After all instances in the file are closed, the kernel releases the data structure. In kernel creation and driver source code, struct file pointers are usually named file or filp.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.