1,gendisk Structural Body
In the Linux kernel, using the Gendisk struct to represent an abstraction of an actual disk device, the struct definition is as follows:
[CPP]View Plaincopy
- struct Gendisk {
- int major; //main device number
- int First_minor; //Secondary device number
- int minors; //maximum number of secondary devices, 1 if not partitioned
- Char Disk_name[disk_name_len]; //device name, displayed in/proc/partitions and Sysfs
- struct DISK_PART_TBL *part_tbl; //means to accommodate partitioned tables
- struct Hd_struct part0; //Represents a partition
- struct Block_device_operations *fops; //point to block device operations collection
- struct Request_queue *queue; //point to I/O request queue
- void *private_data; //Can point to disk for any private data
- int flags; //Some signs
- struct device *driverfs_dev; //Fixme:remove
- struct Kobject *slave_dir;
- sector_t capacity //When a sector is represented by 512 bytes, the drive contains the number of sectors
- struct Timer_rand_state *random;
- atomic_t Sync_io;
- struct Work_struct async_notify;
- #ifdef config_blk_dev_integrity
- struct blk_integrity *integrity;
- #endif
- int node_id;
- };
[CPP]View Plaincopy
- <span style="font-family:arial, Helvetica, Sans-serif; Background-color:rgb (255, 255, 255); " The >linux kernel provides a set of functions to manipulate Gendisk, as follows </span>
1. Assigning Gendisk
Use the following function to assign Gendisk
struct Gendisk *alloc_disk (int minors);
The minors parameter is the number of secondary device numbers used by this disk
2. Increase Gendisk
After the gendisk structure is allocated, the system is not able to use this disk, it is necessary to use the following function to register the device, before using this function to ensure that the driver initialization work completed and respond to the disk request
void Add_disk (struct gendisk *disk);
3. Release Gendisk
When you do not need a disk, use the following function to release Gendisk;
void Del_gendisk (struct gendisk *gp);
Reference calculation for 4.gendisk
The following two functions can be used to manipulate gendisk reference calculations and to release this reference
struct Kobject *get_disk (struct gendisk *disk);
void Put_disk (struct gendisk *disk);
If a disk is partitioned into several partitions, its partitioned table is stored in an array of hd_struct structures, where the address of the array exists in the Gendisk part field
2,block_device Structural Body
Each block device is represented by a struct block_device struct block_device to represent a logical block device object.
The data structure of the description block device is two, one is a struct block_device, which is used to describe a block device or a partition of a block device, is a logical abstraction, focuses on the file system interaction, and the other is a struct gendisk, which describes the characteristics of the actual block device. Focus on interacting with block device drivers. For a block device with multiple partitions, there are multiple struct block_device structures, and the struct gendisk structure is always only one.
[CPP]View Plaincopy
- struct Block_device {
- dev_t Bd_dev; /device number of the device (partition)/
- struct Inode * Bd_inode/inode/pointing to the device file
- struct Super_block * bd_super;
- int bd_openers; /a reference count that records the number of times the block device has been opened, or how many processes have opened the device
- struct Mutex Bd_mutex; / * Open/close Mutex * /
- struct List_head bd_inodes;
- void * Bd_holder;
- int bd_holders;
- struct Block_device * bd_contains; /If the block_device describes a partition, the variable points to the Block_device that describes the primary block device, which, conversely, points to itself/
- unsigned bd_block_size;
- struct hd_struct * bd_part; /If the block_device describes a partition, the variable points to the partition's information
- /* Number of times partitions within this device has been opened. * *
- unsigned bd_part_count;
- int bd_invalidated;
- struct Gendisk * bd_disk; /point to describe the gendisk structure of the entire device
- struct List_head bd_list;
- unsigned long bd_private;
- / * The counter of freeze processes * /
- int bd_fsfreeze_count;
- / * Mutex for Freeze * /
- struct Mutex Bd_fsfreeze_mutex;
- };
3,struct Hd_struct Structural Body
The struct is used to represent a partition information in which the Start_sect nr_sects and PartNo represent the starting sector of the current partition, the size of the partition, the number of sectors, and the number of the partition
[Linux drive]linux block device learning Note (ii)