linux_struct file () struct

Source: Internet
Author: User

The struct file struct is defined in the/linux/include/linux/fs.h (Linux 2.6.11 kernel), and its prototype is:
struct File {
        /*
* Fu_list becomes invalid after file_free are called and queued via
* Fu_rcuhead for RCU freeing
         */
Union {
struct List_head fu_list;
struct Rcu_head fu_rcuhead;
} F_u;
struct path F_path;
#define F_dentry F_path.dentry
#define F_VFSMNT f_path.mnt
const struct File_operations *f_op;
atomic_t F_count;
unsigned int f_flags;
mode_t F_mode;
loff_t F_pos;
struct fown_struct f_owner;
unsigned int f_uid, f_gid;
struct file_ra_state f_ra;
unsigned long f_version;
#ifdef config_security
void *f_security;
#endif
/ * 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;
};
  

The file structure represents an open file, and each open file in the system has an associated struct file in the kernel space. It is created by the kernel when the file is opened and passed to any function that operates on the file. After all instances of the file have been closed, the kernel releases this data structure. In the kernel creation and driver source code, the pointer to the struct file is usually named file or Filp. Here is an explanation of each data member in the structure:
One
Union {
struct List_head fu_list;
struct Rcu_head rcuhead;
}f_u;

The struct list_head is defined in Linux/include/linux/list.h, and the prototype is:
struct List_head {
struct List_head *next, *prev;
};


A pointer to a list of common file objects.
The struct rcu_head is defined in Linux/include/linux/rcupdate.h, and its prototype is:
/**
* struct RCU_HEAD-CALLBACK structure for use with RCU
* @next: Next update requests in a list
* @func: Actual update function to call after the grace period.
*/
struct Rcu_head {
struct Rcu_head *next;
void (*func) (struct rcu_head *head);
};

Second,
struct path F_path;
is defined in linux/include/linux/namei.h, and its prototype is:
struct Path {
struct Vfsmount *mnt;
struct dentry *dentry;
};


struct Vfsmount *mnt is to indicate the file's installed file system, struct dentry *dentry is a file-related directory item object.


three,
const struct file_operations     *f_op;
is defined in Linux/include/linux/fs.h, which contains the actions associated with the file, such as:
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 *);


Four
atomic_t F_count;
atomic_t is defined as:
typedef struct {volatile int counter;} atomic_t;
The volatile modifier field tells GCC not to optimize for this type of data, access to it is access to memory, not to registers.
The essence is the int type, and the reason for this is to have the compiler perform strict type checking on operations based on that type of variable. The role of F_count here is to record a reference count on a file object, that is, how many processes are currently using the file.

Five
unsigned int f_flags;
When the file is opened with the specified flag, the corresponding system calls the open int flags parameter. The driver needs to check for this flag in order to support non-blocking operations.


VI,
mode_t                   F_mode;
Read and write mode of the file, corresponding to the system call the Open mod_t mode parameter. If the driver needs this value, you can read the field directly.
mod_t is defined as:
typedef unsigned int __kernel_mode_t;
typedef __kernel_mode_t         Mode_ t;


Seven
loff_t F_pos;
The current file pointer location, which is the read and write location of the file.
loff_t is defined as:
typedef long Long __kernel_loff_t;
typedef __kernel_loff_t LOFF_T;


Eight,
struct fown_struct f_owner;
the struct fown_struct is defined in Linux/include/linux/fs.h, the prototype is:
struct Fown_struct {
rwlock_t Lock; /* Protects PID, UID, euid fields * /
struct PID *pid; /* PID OR-PGRP where SIGIO should be sent * /
enum Pid_type pid_type;/* Kind of process group SIGIO should be sent to * /
uid_t uid, euid; /* Uid/euid of process Setting the owner * *
int Signum; /* Posix.1b RT signal To is delivered on IO * /
};
the function of this structure is the data of the I/O time notification through the signal.


Nine
unsigned int f_uid, f_gid;
Identifies the owner ID of the file and the ID of the group where the owner resides.


10.
struct file_ra_state f_ra;
The struct file_ra_state structure is defined in the/linux/include/linux/fs.h, and the prototype is:
struct File_ra_state {
pgoff_t start; /* Where ReadAhead started * /
unsigned long size; /* # of ReadAhead pages * *
unsigned long async_size; /* Do asynchronous ReadAhead when
there is only # of pages ahead * *
unsigned long ra_pages; /* Maximum readahead window * /
unsigned long mmap_hit; /* Cache hit stat for mmap accesses * /
unsigned long Mmap_miss; /* Cache Miss Stat for mmap accesses * /
unsigned long prev_index; /* Cache last Read () position */
unsigned int prev_offset; /* Offset where last Read () ended in a page */
};
The file read-ahead status, the file pre-read algorithm uses the main data structure, when a file is opened, F_ra out the Perv_page (default is-1) and ra_apges (the maximum amount of pre-read that the file allows), all the other West End is set to 0.


Eleven
unsigned long f_version;
The version number of the record file, which is automatically incremented after each use.


Twelve
#ifdef config_security
void *f_security;

linux_struct file () struct

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.