[File System] File System Study Notes (4) --- Common Data Structures

Source: Internet
Author: User

1. Basic Data Structure:
File_system_type
Each file system corresponds to a file system type structure. After registration, a single-chain table is formed. The table header is file_systems (global variable ).
Superblock
Reflects the overall control information of the file system. Super blocks exist in multiple ways. (Ultra-fast disks, super memory blocks, and super VFS blocks)
Inode
Reflects the metadata of a file system object ). (Disk inode, memory inode, VFS inode)
Dentry
Reflects the location of a file system object in the file system tree.
Mount
Indicates a mounted file system instance.
 

2. File System Registration
Register the file system with the kernel in the register_filesystem () function. All file systems are in a single-chain table. The names of each file system are stored as strings to describe the file system structure as file_system_type, in the kernel, a global variable named file_systems is used to point to the table header of the linked list.

[CPP]View plaincopy
  1. Struct file_system_type {
  2. Const char * Name; // stores the name of the file system, which is a string
  3. Int fs_flags;
  4. Struct super_block * (* get_sb) (struct file_system_type *, Int,) // read the super block method, called when the file system is loaded; called in the kern_mount function, used to obtain
  5. Obtain the super_block of the file system (partition) and fill it in the mnt_sb member of struct vfsmount.
  6. Const char *, void *, struct vfsmount *);
  7. Void (* kill_sb) (struct super_block *); // Method for deleting a superblock
  8. Struct module * owner;
  9. Struct file_system_type * next;
  10. Struct list_head fs_supers; // super block object bidirectional linked list header of file systems of the same type; each file system has a super block, but some file systems may be installed on different devices, each specific device has a super block, which forms a linked list.
  11. };

On a system, such as the smartphone platform, there are many partitions. For example, the/data and/system partitions are both ext4 file systems, but the system still has only one file_system_type member, however, the ext4 file system corresponding to each partition corresponds to different super_blocks. fs_supers links these Super blocks of the same file system to form a bidirectional cyclic linked list. fs_supers is the linked list header, the linked list element is represented by the s_instance Member of the super_block struct. All super_blocks in the system are linked by s_list of the super_block struct to a bidirectional cyclic linked list. The header is represented by the super_blocks variable.

 

3. File System loading and unloading
The mount command can be used to query the loading of various file systems in the directory tree. When a file system is loaded to a directory, the content of the mount point is replaced with the content of the relative root directory of the file system to be loaded. The data in the previous directory disappears until the new file system is detached.

[CPP]View plaincopy
  1. Struct vfsmount {
  2. Struct list_head mnt_hash;
  3. Struct vfsmount * mnt_parent;/* FS we are mounted on */
  4. Struct dentry * mnt_mountpoint;/* dentry of mountpoint */
  5. Struct dentry * mnt_root;/* root of the mounted tree */
  6. Struct super_block * mnt_sb;/* pointer to superblock */
  7. Struct list_head mnt_mounts;/* List of children, anchored here */
  8. Struct list_head mnt_child;/* and going through their mnt_child */
  9. Int mnt_flags;
  10. Char * mnt_devname;/* Name of device e.g./dev/DSK/hda1 */
  11. Struct list_head mnt_list;
  12. Struct list_head mnt_expire;/* link in FS-specific expiry list */
  13. Struct list_head mnt_share;/* Circular List of shared mounts */
  14. Struct list_head mnt_slave_list;/* List of slave mounts */
  15. Struct list_head mnt_slave;/* slave List entry */
  16. Struct vfsmount * mnt_master;/* slave is on Master-> mnt_slave_list */
  17. Struct mnt_namespace * mnt_ns;/* containing namespace */
  18. Atomic_t mnt_count;
  19. Int mnt_expiry_mark;/* true if marked for expiry */
  20. };

Mnt_mountpoint, A vfsmount Member, indicates that the mount point is the dentry in the parent file system. The dentry corresponding to the root directory of the file system is saved in mnt_root, and the two dentry instances represent the same directory, this means that you do not have to delete the previous mount point information after the file system is uninstalled.

In the new Linux kernel, the struct Mount struct is used to replace the struct vfsmount struct.
When loading a new file system, vfsmount is not the only structure that needs to be created in the memory. The loading operation starts with reading the super block, and the super block is the metadata container of the entire file system. In a disk-based file system, a super block (on a disk) is one or more disks stored in a fixed position on the disk device. When a file system on the disk is mounted, the super block on the disk is read into the memory and the super block in the memory is constructed based on it.

[CPP]View plaincopy
  1. Struct super_block {
  2. Struct list_head s_list;/* linked list element. The header super_blokcs indicates all super linked lists and points to the adjacent element pointers of the linked list,
  3. Dev_t s_dev;/* block device that stores super block information
  4. Unsigned long s_blocksize;
  5. Unsigned char s_blocksize_bits; // The Block length of the file system, which is indicated in bits.
  6. Unsigned char s_dirt; // whether the super block is written dirty. You need to write it back to the disk.
  7. Unsigned long s_maxbytes;/* Maximum length of supported files. The default value is 231-1.
  8. Struct file_system_type * s_type; // pointer to file_system_type
  9. Struct super_operations * s_op;
  10. Unsigned long s_flags; // s_flags is set to 1 or 0 to indicate whether the force lock is allowed for files on the entire device.
  11. Unsigned long s_magic;
  12. Struct dentry * s_root; // point to the dentry object in the root directory of the file system
  13. Struct xattr_handler ** s_xattr;
  14. Struct list_head s_inodes;/* Header of all inode linked lists in the file system
  15. Struct list_head s_dirty;/* point to all dirty inode objects */
  16. Struct list_head s_io;/* parked for writeback */
  17. Struct list_head s_more_io;/* parked for more writeback */
  18. Struct list_head s_files; // header, all open files
  19. Struct block_device * s_bdev; // For the disk file system, the pointer to the block device descriptor; otherwise, it is null.
  20. Struct list_head s_instances; // linked list element. The header is file_system_type-> fs_supers, which indicates multiple loading linked lists of the same file system.
  21. Char s_id [32];/* specific device name, for example,/dev/block/mmcblkop1 */
  22. Void * s_fs_info;/* points to the super block structure of a specific file system. For example, the structure of FAT32 is msdos_sb_info */
  23. U32 s_time_gran;
  24. };

The reason why super fast can manage inode nodes in the system is that all inodes in the file system need to be linked to the super fast linked list header.

All super fast objects are connected together in the form of a bidirectional cyclic linked list. The first element of the linked list is represented by the super_blocks variable. The sb_lock spin lock protects the linked list from simultaneous access on the multi-processor system. S_fs_info points to the super fast information of a specific file system. For example, if the specific file system is ext2, s_fs_info points to ext2_sb_info, and s_dirty indicates modifying the dirty mark, indicates whether the super fast information in the memory is synchronized with the super fast information on the hard disk,
Each reprinted file system has only one super fast instance.

 

 

[CPP]View plaincopy
    1. Struct inode {
    2. Struct hlist_node I _hash; // used to hash the linked list pointer
    3. Struct list_head I _list; // describes the linked list pointer of the index node.
    4. Struct list_head I _sb_list; // pointer USED FOR THE linked list of the super-fast index Node
    5. Struct list_head I _dentry; // reference the head of the linked list of the Directory item object of the index Node
    6. Unsigned long I _ino; // index node number
    7. Atomic_t I _count; // Reference Counter
    8. Unsigned int I _nlink; // number of hard links
    9. Uid_t I _uid; // owner ID
    10. Gid_t I _gid; // group ID
    11. Dev_t I _rdev; // real device identifier
    12. Unsigned long I _version;
    13. Loff_t I _size; // number of file bytes
    14. Struct timespec I _atime; // last access time
    15. Struct timespec I _mtime; // time when the last file was written
    16. Struct timespec I _ctime; // time when the index node was last modified
    17. Unsigned int I _blkbits; // number of digits of the block
    18. Blkcnt_t I _blocks; // number of file blocks
    19. Umode_t I _mode; // file type and access permission
    20. Struct inode_operations * I _op; // index node operation
    21. Const struct file_operations * I _fop; // default file operations
    22. Struct super_block * I _sb; // pointer to a super fast object
    23. Struct address_space * I _mapping; // pointer to the address_space object
    24. Struct address_space I _data; // file address_space object
    25. Struct dquot * I _dquot [maxquotas]; // disk quota of the index Node
    26. Struct list_head I _devices; // used as a node linked list pointer for specific characters or fast Devices
    27. Union {
    28. Struct pipe_inode_info * I _pipe;
    29. Struct block_device * I _bdev;
    30. Struct cdev * I _cdev;
    31. };
    32. Int I _cindex; // index of the device file that has a set of device numbers
    33. _ U32 I _generation;
    34. Unsigned long I _state; // index node status ID
    35. Unsigned long dirtied_when;/* jiffies of first dirtying */
    36. Unsigned int I _flags; // file system installation flag. For example, if ms_mandlock is used, you can choose whether to use a force lock for a file.
    37. Atomic_t I _writecount; // used for reference counting of write Processes
    38. Void * I _security; // pointer to the security structure of the index Node
    39. };

[File System] File System Study Notes (4) --- Common Data Structures

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.