[Linux drive]linux block device learning Note (ii)

Source: Internet
Author: User

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
  1. struct Gendisk {
  2. int major; //main device number
  3. int First_minor; //Secondary device number
  4. int minors; //maximum number of secondary devices, 1 if not partitioned
  5. Char Disk_name[disk_name_len]; //device name, displayed in/proc/partitions and Sysfs
  6. struct DISK_PART_TBL *part_tbl; //means to accommodate partitioned tables
  7. struct Hd_struct part0; //Represents a partition
  8. struct Block_device_operations *fops; //point to block device operations collection
  9. struct Request_queue *queue; //point to I/O request queue
  10. void *private_data; //Can point to disk for any private data
  11. int flags; //Some signs
  12. struct device *driverfs_dev; //Fixme:remove
  13. struct Kobject *slave_dir;
  14. sector_t capacity //When a sector is represented by 512 bytes, the drive contains the number of sectors
  15. struct Timer_rand_state *random;
  16. atomic_t Sync_io;
  17. struct Work_struct async_notify;
  18. #ifdef config_blk_dev_integrity
  19. struct blk_integrity *integrity;
  20. #endif
  21. int node_id;
  22. };



[CPP]View Plaincopy
    1. <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
  1. struct Block_device {
  2. dev_t Bd_dev; /device number of the device (partition)/
  3. struct Inode * Bd_inode/inode/pointing to the device file
  4. struct Super_block * bd_super;
  5. 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
  6. struct Mutex Bd_mutex; / * Open/close Mutex * /
  7. struct List_head bd_inodes;
  8. void * Bd_holder;
  9. int bd_holders;
  10. 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/
  11. unsigned bd_block_size;
  12. struct hd_struct * bd_part; /If the block_device describes a partition, the variable points to the partition's information
  13. /* Number of times partitions within this device has been opened. * *
  14. unsigned bd_part_count;
  15. int bd_invalidated;
  16. struct Gendisk * bd_disk; /point to describe the gendisk structure of the entire device
  17. struct List_head bd_list;
  18. unsigned long bd_private;
  19. / * The counter of freeze processes * /
  20. int bd_fsfreeze_count;
  21. / * Mutex for Freeze * /
  22. struct Mutex Bd_fsfreeze_mutex;
  23. };

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)

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.