Simple parsing of struct inode and struct File

Source: Internet
Author: User

1. struct inode-an important structure of character device drivers

In the kernel, The inode structure is used to represent a specific file, and the file structure is used to represent the opened file descriptor. In the linux2.6.27 kernel, The inode struct 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 indicates the inode STRUCTURE OF THE DEVICE file, which contains the real 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; // This Member indicates the internal structure of the kernel of the character device. When 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.
};
Int I _cindex;

_ U32 I _generation;

# Ifdef config_dnotify
Unsigned long I _dpolicy_mask;/* directory named y events */
Struct dnotify_struct * I _dnotify;/* for directory Communications */
# Endif

# Ifdef config_inotify
Struct list_head inotify_watches;/* watches on 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-important structures related to character device drivers

File structureRepresents an opened file descriptor, which is not used by the driver. Each opened file in the system has an associated struct file in the kernel. It is created when the kernel is open and passed to any function operated on the file. When all instances of the file are closed, the kernel releases the data structure.
Struct File
{
/*
* Fu_list becomes invalid after file_free is called and queued
* 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 // This member is the corresponding directory structure.
# Define f_vfsmnt f_path.mnt
Const struct file_operations * f_op; // This operation defines object Association operations. The kernel assigns a value to this pointer when executing open.
Atomic_long_t f_count;
Unsigned int f_flags;
// This 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 useful resource for saving status information during system calls.

# 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
}

(1) The struct file struct is defined in include/Linux/fs. h. A file struct represents an opened file. Each opened 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 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. As follows:
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 a new lock mechanism in Linux 2.6 kernel.
} F_u;
Struct path f_path; contains dentry and MNT members, used to determine the file path
# Define f_dentry f_path.dentry f_path
# Define f_vfsmnt f_path.mnt indicates the Mount root directory of the file system where the current file is located
Const struct file_operations * f_op; operation functions associated with the file
Atomic_t f_count; file reference count (How many processes open the file)
Unsigned int f_flags; corresponds to the flag specified during open
Mode_t f_mode; read/write mode: Open mod_t Mode Parameter
Off_t f_pos; file offset of the file in the current process
Struct fown_struct f_owner; this structure is used to send I/O time notification data through signals.
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, which is a link to an index node (inode) in the Linux File System. This index node can be a file or a directory. Inode (which can be understood as ext2 inode) corresponds to a specific object on a physical disk. dentry is a memory entity, where the d_inode Member points to the corresponding inode. That is to say, an inode can link Multiple dentry while running, while d_count records the number of this link.
Struct dentry {
Atomic_t d_count; the directory item object uses counters, and there can be unused States, using states and negative States
Unsigned int d_flags; directory entry flag
Struct inode * d_inode; the index node associated with the file name
Struct dentry * d_parent; directory item object of parent directory
Struct list_head d_hash; pointer of table items in the hash table
Struct list_head d_lru; the linked list pointer is not used.
Struct list_head d_child; pointer to the linked list of the Directory item object in the parent directory
Struct list_head d_subdirs; indicates the linked list of the sub-directory item object.
Struct list_head d_alias; linked list of related index nodes (alias)
Int d_mounted; indicates the root entry of the installed file system.
Struct qstr d_name; file name
Unsigned long d_time;/* used by d_revalidate */
Struct dentry_operations * d_op; directory item method
Struct super_block * d_sb; 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]; stores short file names

};

Inode is an index node. Partition of each storage device or storage device (the storage device is a hard disk, floppy disk, USB flash disk ......) after being formatted as a file system, there should be two parts: one part is inode, the other part is block, and the block is used to store data. Inode is used to store the data information, including the file size, owner, owner user group, and read/write permissions. Inode indexes information for each file, so there is an inode value. Based on commands, the operating system can find the corresponding files with the fastest inode value.
To make a metaphor, for example, a book, a storage device or partition is equivalent to this book. Block is equivalent to every page in the book, and inode is equivalent to the directory before this book. A book has a lot of content, if you want to find a part of the content, you can first check the directory to find the content we want to see as quickly as possible.
When we use ls to view a directory or file, if we add the-I parameter, we can see the inode node. For example, LS-Li lsfile. Sh, the first value is the inode information.

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 {/** 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.

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

Http://blog.csdn.net/yeqishi/article/details/5802932

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.