File struct file (Linux 2.6.23 kernel)

Source: Internet
Author: User

I. Definition:

Struct
The file struct is defined in/Linux/include/Linux/fs. H (Linux
2.6.11 kernel), its prototype is:


721 struct File
{

 722        /* 723         * fu_list becomes invalid after file_free is called and queued via 724         * fu_rcuhead for RCU freeing 725         */ 726        union { 727                struct list_head        fu_list; 728                struct rcu_head         fu_rcuhead; 729        } f_u; 730        struct path             f_path; 731#define f_dentry        f_path.dentry 732#define f_vfsmnt        f_path.mnt 733        const struct file_operations    *f_op; 734        atomic_t                f_count; 735        unsigned int            f_flags; 736        mode_t                  f_mode; 737        loff_t                  f_pos; 738        struct fown_struct      f_owner; 739        unsigned int            f_uid, f_gid; 740        struct file_ra_state    f_ra; 741 742        unsigned long           f_version; 743#ifdef CONFIG_SECURITY 744        void                    *f_security; 745#endif 746        /* needed for tty driver, and maybe others */ 747        void                    *private_data; 748 749#ifdef CONFIG_EPOLL 750        /* Used by fs/eventpoll.c to link all the hooks to this file */ 751        struct list_head        f_ep_links; 752        spinlock_t              f_ep_lock; 753#endif /* #ifdef CONFIG_EPOLL */ 754        struct address_space    *f_mapping;


755 };

Ii. role:

Text
The component structure 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. Create a driver in the kernel and use the driver source code.
Medium, struct
The file pointer is usually named file or filp.

3. Detailed description of fields:

1,
Union
{
Struct list_head fu_list;

Struct rcu_head rcuhead;
} F_u;
Struct
List_head is defined in
In Linux/include/Linux/list. H, the prototype is:


21 struct list_head
{

  22        struct list_head *next, *prev;


23 };

List_head is the most commonly used structure in the kernel to establish a bidirectional circular linked list. It is used as a pointer to a universal object linked list.
Struct
Rcu_head is defined in Linux/include/Linux/rcupdate. H. Its prototype is:


50 struct rcu_head
{

  51        struct rcu_head *next;  52        void (*func)(struct rcu_head *head);


53 };

RCU (read-copy
Update) is a Linux
2.6 The New lock mechanism in the kernel. For details, refer:
Http://www.ibm.com/developerworks/cn/linux/l-rcu/

This is used to update files. Fu_list is invalid when the file_free () function is called. The queue uses rcu_head to release RCU.
2,Struct
Path f_path;
It is defined in Linux/include/Linux/nameI. h and its prototype is:


32 struct path
{

  33        struct vfsmount *mnt;  34        struct dentry *dentry;


35 };


In earlier versions, the kernel does not have this structure, but directly uses two data members of path as struct
File data member,
Struct
Vfsmount * MNT indicates that the file system has been installed,
Struct
Dentry * dentry is a file-related directory item object.
3. Const
Struct file_operations
* F_op;
It is defined in Linux/include/Linux/fs. h and contains operations associated with files, such:
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
*);
. When a file is opened, the kernel creates a struct associated with the file.
File structure, where * f_op points to
Object. For example, when a user calls the system to call read to read the content of the file, the system calls read and the kernel calls the sys_read function.
Sys_read will eventually call the struct associated with the file
The f_op-> READ function in the file structure reads the file content.
4. atomic_t
F_count;
Atomic_t is defined:
Typedef
Struct {volatile int counter ;}
Atomic_t;
The volatile modifier field tells GCC not to optimize the data of this type, and its access is to the memory instead of the register.
The essence is the int type. The reason for writing this is to allow the compiler to strictly check the type of operations based on this type of variable. F_count is used to record the reference count of the file object, that is, the number of clone processes using the clone_files flag currently using this file. Typical applications are in POSIX Threads. Just like the common reference counting module in the kernel, the last process calls put_files_struct () to release the file descriptor.
5. unsigned
Intf_flags;
The identifier specified when the file is opened, corresponding to the system call open int
Flags parameter. The driver needs to check this flag to support non-blocking operations.
6. mode_t
F_mode;
The read/write mode of the file, which corresponds to the mod_t called by the System
Mode parameter. If the driver needs this value, you can directly read this field.
Mod_t is defined:
Typedef
Unsigned int _ kernel_mode_t;
Typedef _ kernel_mode_t
Mode_t;
7. loff_t
F_pos;
The current file pointer location, that is, the file's read/write location.
Loff_t is defined:
Typedef
Long long _ kernel_loff_t;
Typedef _ kernel_loff_t
Loff_t;
8. struct
Fown_structf_owner;
Struct
Fown_struct is defined in Linux/include/Linux/fs. H. Its prototype is:

 688struct fown_struct { 689  rwlock_t lock;          /* protects pid, uid, euid fields */ 690  struct pid *pid;        /* pid or -pgrp where SIGIO should be sent */ 691  enum pid_type pid_type;/*Kind of process group SIGIO should be sent to*/ 692  uid_t uid, euid;        /* uid/euid of process setting the owner */ 693  int signum;             /* posix.1b rt signal to be delivered on IO */
694};

This structure is used to send I/O time notifications through signals.
9. unsigned
Intf_uid, f_gid;
ID of the owner of the file, the ID of the group where the owner is located.
10. struct
File_ra_state f_ra;
Struct
The file_ra_state structure is defined in/Linux/include/Linux/fs. h. The prototype is:


699 struct file_ra_state
{

 700        pgoff_t start;                  /* where readahead started */ 701        unsigned long size;             /* # of readahead pages */ 702        unsigned long async_size;       /* do asynchronous readahead when 703                                           there are only # of pages ahead */ 704 705        unsigned long ra_pages;         /* Maximum readahead window */ 706        unsigned long mmap_hit;       /* Cache hit stat for mmap accesses */ 707        unsigned long mmap_miss;  /* Cache miss stat for mmap accesses */ 708       unsigned long prev_index;       /* Cache last read() position */ 709     unsigned int prev_offset; /* Offset where last read() ended in a page */


710 };

File pre-read status, the main data structure used by the file pre-read algorithm. When a file is opened, the perv_page (-1 by default) appears in f_ra) and ra_apges (the maximum number of pre-reads allowed for the file), all other word ends are set to 0.
11. unsigned
Long f_version;
Record the version number of the file, which increases automatically after each use.
12,

# Ifdef
Config_security

Void * f_security;
# Endif
Here, I understand that if security measures are configured during kernel compilation, struct
The file structure contains void.
* F_security data item, used to describe security measures or record security-related information.
13. Void
* Private_data;
The system sets this pointer to null before calling the open method of the driver. The driver can use this field for any purpose, or ignore this field. The driver can use this field to point to the allocated data, but it must be cleared in the release method before the file structure is released by the kernel.
14,
# Ifdef
Config_epoll
/* Used by fs/eventpoll. C to link all the hooks
This file */
Struct list_head f_ep_links;
Spinlock_t
F_ep_lock;
# Endif/* # ifdef config_epoll
*/
It is used in FS/eventpoll. C to link all hooks to this file. F_ep_links is the header of the file event polling waiting list, and f_ep_lock is the spin lock that protects the f_ep_links linked list.
15. struct
Address_space * f_mapping;
Struct
Address_space is defined in/Linux/include/Linux/fs. H, which is a pointer to the file address space.
Iv. application:
 
In driver development, file read/write mode and f_flags are the content that the device driver cares about, while private Data Pointer private_data is widely used in the driver, most of them are directed to the device driver's custom struct used to describe the device. The following code is often used in the driver to detect the reading and writing methods of open files:
If
(File-> f_mode & fmode_write) // user requirements can be written
 
{
}
If (file-> f_mode & fmode_read)
// The user must be readable.
 
{
}
The following code can be used to determine whether to open a device file in blocking or non-blocking mode:
 
If (file-> f_flags & o_nonblock)
// Non-blocking
Pr_debug ("open: non-blocking/N ");
 
Else // Blocking

Pr_debug ("open: blocking/N ");

******************
Inode structure and file structure:
The kernel uses the inode structure to internally represent files. The file structure indicates the opened file descriptor. For a single file, there may be many file structures that indicate opened file descriptors, but they all point to a single inode structure.

Related Article

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.