struct Inode and struct file

Source: Internet
Author: User

1, struct inode── character device driver related important structure introduction

The kernel uses the inode structure to represent the specific file, and the file structure represents the open descriptor. In the Linux2.6.27 kernel, the inode structure is defined as follows:
struct Inode
{
struct Hlist_node i_hash;
struct List_head i_list;
struct List_head i_sb_list;
struct List_head i_dentry;
unsigned long I_ino;
atomic_t I_count;
unsigned int i_nlink;
uid_t I_uid;
gid_t I_gid;
dev_t I_rdev; This member represents the INODE structure of the device file, which contains the true device number.
U64 i_version;
loff_t i_size;
#ifdef __need_i_size_ordered
seqcount_t I_size_seqcount;
#endif
struct Timespec i_atime;
struct Timespec i_mtime;
struct Timespec i_ctime;
unsigned int i_blkbits;
blkcnt_t i_blocks;
unsigned short i_bytes;
umode_t I_mode;
spinlock_t I_lock; /* I_blocks, I_bytes, maybe I_size */
struct Mutex I_mutex;
struct Rw_semaphore I_alloc_sem;
const struct Inode_operations *i_op;
const struct File_operations *i_fop; /* Former->i_op->default_file_ops */
struct Super_block *i_sb;
struct File_lock *i_flock;
struct Address_space *i_mapping;
struct Address_space i_data;
#ifdef Config_quota
struct Dquot *i_dquot[maxquotas];
#endif
struct List_head i_devices;
Union {
struct Pipe_inode_info *i_pipe;
struct Block_device *i_bdev;
struct Cdev *i_cdev; The member represents the internal structure of the kernel of the character device. When the inode points to a character device file, the member contains a pointer to the struct CDEV structure, where the CDEV structure is the character device structure body.
};
int i_cindex;

__u32 i_generation;

#ifdef config_dnotify
unsigned long i_dnotify_mask; /* Directory Notify Events */
struct Dnotify_struct *i_dnotify; /* for directory Notifications */
#endif

#ifdef config_inotify
struct List_head inotify_watches; /* Watches on the This inode */
struct Mutex Inotify_mutex; /* Protects the watches list */
#endif

unsigned long i_state;
unsigned long dirtied_when; /* Jiffies of First dirtying */

unsigned int i_flags;

atomic_t I_writecount;
#ifdef config_security
void *i_security;
#endif
void *i_private; /* fs or device private pointer */
};

2, struct file── character device drive related important structure

File StructureRepresents an open file descriptor, which is not intended for use by the driver, and each open file in the system has an associated struct file in the kernel. It is created by the kernel at open and passed to any function that operates on the file, knowing that the last shutdown. When all instances of the file are closed, the kernel releases the data structure.
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//The member is the corresponding directory structure.
#define F_VFSMNT F_path.mnt
const struct File_operations *f_op; This action is defined by the action of the file association. The kernel assigns a value to this pointer when it executes the open.
atomic_long_t F_count;
unsigned int f_flags; The member is a file flag.
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;

U64 f_version;
#ifdef config_security
void *f_security;
#endif
/* Needed for TTY driver, and maybe others */
Void *private_data;//This member is a very useful resource to save state information when the system is called.

#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;
#ifdef Config_debug_writecount
unsigned long f_mnt_write_state;
#endif
};

----------------------------------------------------------------------------

File structure and Inode structure

(1) struct file struct definition is defined in include/linux/fs.h. 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. As shown below:
struct File {
Union {
struct List_head fu_list; File object Linked list pointer linux/include/linux/list.h
struct Rcu_head fu_rcuhead; RCU (Read-copy Update) is the new lock mechanism in the Linux 2.6 kernel
} F_u;
struct path F_path; Contains two members of Dentry and MNT for determining file path
#define A member of the F_dentry F_path.dentry F_path, the dentry structure of the current file
#define F_VFSMNT F_path.mnt represents the Mount root of the file system where the current file resides
const struct File_operations *f_op; The action function associated with the file
atomic_t F_count; The reference count of the file (how many processes open the file)
unsigned int f_flags; Flags that correspond to the specified flag at open
mode_t F_mode; Read-write mode: Open mod_t mode parameter
off_t F_pos; The file offset of the file in the current process
struct Fown_struct f_owner; The function of this structure is the data of the I/O time notification through the signal.
unsigned int f_uid, f_gid; File owner ID, owner group ID
struct File_ra_state F_ra; Defined in Linux/include/linux/fs.h, file pre-read related
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;
};
(2) struct dentry
The Chinese name of Dentry is a directory entry and is a link to an index node (inode) in the Linux file system. The index node can be a file or a directory. The Inode (which can be understood as ext2 inode) corresponds to a specific object on the physical disk, and Dentry is a memory entity in which D_inode members point to the corresponding inode. In other words, an inode can link multiple dentry at run time, while D_count records the number of links.
struct Dentry {
atomic_t D_count; Catalog item objects Use counters that can have unused states, usage states, and negative states
unsigned int d_flags; Catalog Item Flags
struct Inode * d_inode; The index node associated with the file name
struct Dentry * d_parent; Directory entry object for parent directory
struct List_head d_hash; Pointers to hash table entries
struct List_head d_lru; Pointers that do not use linked lists
struct List_head d_child; Pointer to the list of catalog item objects in the parent directory
struct List_head d_subdirs; For a directory, a list that represents a subdirectory of a directory item object
struct List_head d_alias; Linked list of related index nodes (aliases)
int d_mounted; For installation points, the file system root key that is installed
struct QSTR d_name; Filename
unsigned long d_time; /* Used by d_revalidate */
struct Dentry_operations *d_op; Catalog Item method
struct Super_block * D_SB; The super Block object of the file
vunsigned long d_vfs_flags;
void * D_FSDATA; file system-related data
unsigned char d_iname [Dname_inline_len]; Store short file names

};

(3) The index node object is represented by the inode struct, and the definition file is in Linux/fs.h.

struct Inode {
struct Hlist_node i_hash; Hash table
struct List_head i_list; Index node Chain list
struct List_head i_dentry; Catalog Necklace Table
unsigned long I_ino; Node number
atomic_t I_count; Reference count
umode_t I_mode; Access Rights control
unsigned int i_nlink; Number of hard links
uid_t I_uid; User ID
gid_t I_gid; Consumer ID Group
kdev_t I_rdev; Real device identifiers
loff_t i_size; File size in bytes
struct Timespec i_atime; Last Access time
struct Timespec i_mtime; Last modified (Modify) time
struct Timespec i_ctime; Last Change time
unsigned int i_blkbits; The block size in bits
unsigned long i_blksize; Block size in bytes
unsigned long i_version; Version number
unsigned long i_blocks; Number of blocks in the file
unsigned short i_bytes; Number of bytes used
spinlock_t I_lock; Spin lock
struct Rw_semaphore I_alloc_sem; Index node Signal volume
struct Inode_operations *i_op; Index node action table
struct File_operations *i_fop; The default index node operation
struct Super_block *i_sb; Related Super Block
struct File_lock *i_flock; File Chain List
struct Address_space *i_mapping; Related Address mappings
struct Address_space i_data; Device Address Mapping
struct Dquot *i_dquot[maxquotas]; disk quotas for nodes
struct List_head i_devices; Block Device Chain List
struct Pipe_inode_info *i_pipe; Pipeline information
struct Block_device *i_bdev; Block device drivers
unsigned long i_dnotify_mask; directory notification mask
struct Dnotify_struct *i_dnotify; Directory Notifications
unsigned long i_state; Status flag
unsigned long dirtied_when; first time modified
unsigned int i_flags; File System Flags
unsigned char i_sock; Sockets
atomic_t I_writecount; Writer Count
void *i_security; Safety Modules
__u32 i_generation; Index node version number
Union {
void *generic_ip; file special information
} u;
};

The inode is translated into Chinese as an index node. Partition of each storage device or storage device (storage device is hard disk, floppy disk, USB stick ...) After being formatted as a filesystem, there should be two parts, one for the inode, and the other for the Block,block to store the data. The inode is the information that is used to store this data, including file size, owner, attribution user group, read-write permission, and so on. The Inode indexes the information for each file, so there is a value for the inode. According to the instructions, the operating system can find the corresponding file by the Inode value.
To make a metaphor, such as a book, a storage device or partition equivalent to this book, block equivalent to each page of the book, the Inode is equivalent to the book in front of the directory, a book has a lot of content, if you want to find a part of the content, we can first look at the directory, through the directory can find the fastest we want to see
When we use LS to view a directory or file, if you add the-i parameter, you can see the Inode node, such as Ls-li lsfile.sh, the first value is the Inode information

Ext.: http://www.cnblogs.com/QJohnson/archive/2011/06/24/2089414.html

struct Inode and struct file

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.