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
};
----------------------------------------------------------------------------
File struct and inode struct
(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
};
(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 linked list
Struct list_head I _dentry; directory linked list
Unsigned long I _ino; node number
Atomic_t I _count; reference count
Umode_t I _mode; access control
Unsigned int I _nlink; number of hard links
Uid_t I _uid; User ID
Gid_t I _gid; User ID Group
Kdev_t I _rdev; real device identifier
Loff_t I _size; file size in bytes
Struct timespec I _atime; last access time
Struct timespec I _mtime; last modify time
Struct timespec I _ctime; last change time
Unsigned int I _blkbits; block size in bits
Unsigned long I _blksize; block size in bytes
Unsigned long I _version; version number
Unsigned long I _blocks; number of file blocks
Unsigned short I _bytes; number of bytes used
Spinlock_t I _lock; spin lock
Struct rw_semaphore I _alloc_sem; index node semaphore
Struct inode_operations * I _op; index node operation table
Struct file_operations * I _fop; default index node operations
Struct super_block * I _sb; related super block
Struct file_lock * I _flock; file lock linked list
Struct address_space * I _mapping; address ing
Struct address_space I _data; device address ing
Struct dquot * I _dquot [maxquotas]; Disk Quota of nodes
Struct list_head I _devices; block device linked list
Struct pipe_inode_info * I _pipe; MPS queue Information
Struct block_device * I _bdev; block Device Driver
Unsigned long I _dpolicy_mask; Directory Notification mask
Struct dnotify_struct * I _dnotify; Directory Notification
Unsigned long I _state; Status flag
Unsigned long dirtied_when; first modification time
Unsigned int I _flags; File System flag
Unsigned char I _sock; socket
Atomic_t I _writecount; writer count
Void * I _security; Security Module
_ U32 I _generation; index node version number
Union {
Void * generic_ip; special file information
} U;
};
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.