[Linux Driver] - Linux Device Driver - Concept and Framework of Block Device (1)

basic concept

Block device

--- It is a random access device with a certain structure. The reading and writing of this device is performed in blocks . It uses a buffer to store temporary data. After the conditions are mature, it is written to the device from the cache at a time Or read from the device to the buffer at a time.

Character device

--- It is a sequential data stream device. The reading and writing of this device is carried out by characters , and these characters continuously form a data stream. He does not have a buffer, and all reading and writing to this device is real-time .

 

Sectors: The basic unit of data processing by any block device hardware. Usually, the size of a community is 512 bytes. (For equipment)

Blocks: The basic unit of data processing such as the kernel or file system designated by Linux. Generally, one block is composed of one or more sectors. (For Linux operating system)

Segments: consists of several adjacent blocks. It is a part of a memory or memory page of the Linux memory management mechanism.

The relationship diagram among pages, segments, blocks, and sectors is as follows:

 

Block device driver overall framework

The application of block devices is a complete subsystem in Linux.

In Linux, when a driver performs an input or output (I/O) operation on a block device, it will send a request to the block device, which is described by the request structure in the driver . But for some disk devices, the request speed is very slow. At this time, the kernel provides a queue mechanism to add these I/O requests to the queue (ie: request queue), which is described by the request_queue structure in the driver . Before submitting these requests to the block device, the kernel will perform request merging and sorting pre-operations to improve access efficiency, and then the I/O scheduler subsystem in the kernel is responsible for submitting I/O requests, and the scheduler will Resources are allocated to all pending block I/O requests in the system. Its job is to manage the request queue of the block device, determine the order of the requests in the queue and when to dispatch the request to the device .

The Generic Block Layer is responsible for maintaining the relationship between an I/O request in the upper file system and the disk in the lower room. In the general block layer, a bio structure is usually used to correspond to an I/O request .

Linux provides a gendisk data structure , used to represent an independent disk device or partition, used to access the underlying physical disk. In gendisk, another hardware operation structure pointer similar to file_operations in character devices is the block_device_operations structure .

When multiple requests are submitted to the block device, the execution efficiency depends on the order of the requests. If all requests are in the same direction (such as writing data), the execution efficiency is the greatest. The kernel collects I/O requests and sorts the requests before calling the block device driver routines to process the requests, and then merges multiple requests for consecutive sector operations to improve execution efficiency (the kernel algorithm will do it by itself, without you Tube) , the algorithm for sorting I/O requests is called elevator algorithm . The elevator algorithm is completed at the I/O dispatch level. The kernel provides different types of elevator algorithms. The elevator algorithms are:

  1. noop (implement simple FIFO, basic direct merging and sorting)
  2. anticipatory (delay I/O requests and optimize the sorting of critical sections)
  3. Deadline (improve anticipatory shortcomings and reduce delay time)
  4. Cfq (uniform distribution of I/O bandwidth, fairness mechanism)

PS: In fact, the IO scheduling layer (including the request merge sorting algorithm) does not need to be managed by the user, the kernel is already ready

Related data structure

block_device: describes a block device instance of a partition or an entire disk to the kernel

gendisk: Describes a generic hard disk object.

hd_struct: Describe the partition information that the partition should have

bio: describe how to fill or read the block to the driver during block data transfer

request: describes requesting a list from the kernel to prepare for queue processing

request_queue: Describes that the kernel applies for request resources to establish a request linked list and fills in the BIO to form a queue.

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/u014674293/article/details/104406005