Introduction to file_operations struct

Source: Internet
Author: User

1. file_operations source code linux-2.6.38/include/linux/fs. h In the kernel


[Cpp]
/*
* NOTE:
* All file operations should t setlease can be called
* The big kernel lock held in all filesystems.
*/
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 (* write) (struct file *, const char _ user *, size_t, loff_t *);
Ssize_t (* aio_read) (struct kiocb *, const struct iovec *, unsigned long, loff_t );
Ssize_t (* aio_write) (struct kiocb *, const struct iovec *, unsigned long, loff_t );
Int (* readdir) (struct file *, void *, filldir_t );
Unsigned int (* poll) (struct file *, struct poll_table_struct *);
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 *, 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 (* sendpage) (struct file *, struct page *, int, size_t, loff_t *, int );
Unsigned long (* get_unmapped_area) (struct file *, unsigned long, unsigned long );
Int (* check_flags) (int );
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 );
Int (* setlease) (struct file *, long, struct file_lock **);
Long (* fallocate) (struct file * file, int mode, loff_t offset,
Loff_t len );
};

/*
* NOTE:
* All file operations should t setlease can be called
* The big kernel lock held in all filesystems.
*/
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 (* write) (struct file *, const char _ user *, size_t, loff_t *);
Ssize_t (* aio_read) (struct kiocb *, const struct iovec *, unsigned long, loff_t );
Ssize_t (* aio_write) (struct kiocb *, const struct iovec *, unsigned long, loff_t );
Int (* readdir) (struct file *, void *, filldir_t );
Unsigned int (* poll) (struct file *, struct poll_table_struct *);
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 *, 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 (* sendpage) (struct file *, struct page *, int, size_t, loff_t *, int );
Unsigned long (* get_unmapped_area) (struct file *, unsigned long, unsigned long );
Int (* check_flags) (int );
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 );
Int (* setlease) (struct file *, long, struct file_lock **);
Long (* fallocate) (struct file * file, int mode, loff_t offset,
Loff_t len );
};

 


1. struct module * owner

The first file_operations member is not an operation at all; it is a pointer to a module with this structure.

Members are used to prevent the module from being detached when its operations are still in use. Almost all the time, it is initialized

THIS_MODULE, a macro defined in <linux/module. h>.

2. 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 one.

"Longoffset", and at least 64 bit width even on the 32-bit platform. The error is indicated by a negative return value. If this

If the function pointer is NULL, seek calls the function to modify the position counter in the file structure in an unpredictable way.

3. 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,

It is usually the local Integer type of the target platform ).

4. 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 replaced by read (synchronous location ).

5. 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 is

Number of successfully written bytes.

6. ssize_t (* aio_write) (struct kiocb *, const char _ user *, size_t, loff_t *);

Initializes an asynchronous write on the device.

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

8. unsigned int (* poll) (struct file *, struct poll_table_struct *);

The poll method is the backend of three system calls: poll, epoll, and select, which are used to query one or more file descriptors.

Whether the read or write operations will be blocked. The poll method should return a single bit mask to indicate whether non-blocking read or write operations are possible,

Possibly, the kernel information is provided to make the calling process sleep until I/O becomes possible. If a driver's poll Method

NULL indicates that the device is readable and writable without blocking.

9. int (* ioctl) (struct inode *, struct file *, unsigned int, unsigned long );

Ioctl system calls provide methods for issuing device-specific commands (for example, formatting a disk path of a floppy disk, which is neither read nor read

Is written). 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

For any unspecified request (-ENOTTY, "the device does not have such ioctl"), the system returns an error.

10. 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 calls

Return-ENODEV.

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

If it is NULL, the device has been successfully opened, but your driver will not be notified.

12. int (* flush) (struct file *);

The flush operation is called when a process disables the copy of its device file descriptor; it should execute (and wait) Any

Incomplete operation. This must not be confused with the fsync operation requested by the user. Currently, flush is rarely driven.

Use; SCSI tape driver use it, for example, to ensure that all written data is written to the tape before the device is closed. If flush

If it is NULL, the kernel simply ignores user application requests.

13. int (* release) (struct inode *, struct file *);

This operation is referenced when the file structure is released. Like open, release can be NULL.

14. 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 this pointer is

NULL, system call returns-EINVAL.

15. int (* aio_fsync) (struct kiocb *, int );

This is the asynchronous version of The fsync method.

16. 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, in chapter 6th

Description. This member can be NULL if the driver does not support asynchronous notifications.

17. int (* lock) (struct file *, int, struct file_lock *);

The lock method is used to lock files. Locking is essential to conventional files, but the device driver is almost never implemented.

Now it is.

18. ssize_t (* readv) (struct file *, const struct iovec *, unsignedlong, loff_t *);

19. ssize_t (* writev) (struct file *, const struct iovec *, unsigned long, loff_t *);

These methods implement divergence/aggregation read and write operations. The application occasionally needs to do a single read or

Write operations; 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 ).

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

Sendfile is NULL.

21. 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. Device Driver

In fact, sendpage is not implemented.

22. unsigned long (* get_unmapped_area) (struct file *, unsignedlong, 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 the Memory Management Code. This method can be used to enable the driver to force any

What's the alignment request? Most drivers can set this method to NULL .[

23. int (* check_flags) (int)

This method allows the module to check the flag passed to the fnctl (F_SETFL...) Call.

24. int (* dir_notify) (struct file *, unsigned long );

This method is called when the application uses fcntl to request a Directory change notification. It is only useful to the file system.

Implement dir_notify.

 

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.