Under Linux, the drive device has character device driver, block device driver and network device drive three kinds, before learning the character device driver, now began to learn block device driver. First of all to compare the two device drivers why not pass, why to split into different types of drivers. Take an example to illustrate the need to introduce block device drivers:
If you construct the drive in the same frame as the character device,
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/79/37/wKioL1aLwRGxcuEyAACIYQ7Yglo702.png "title=" capture. PNG "alt=" Wkiol1alwrgxcueyaaciyq7yglo702.png "/>
The storage device needs to be frequently erased if another framework is used (block device thinking):
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/79/39/wKiom1aLwVWwNsP0AACnuAbEULA318.png "title=" capture. PNG "alt=" Wkiom1alwvwwnsp0aacnuabeula318.png "/>
In contrast, for such storage devices, the introduction of block devices obviously makes the operation much more effective. The feature of the block device is that the request is placed in the queue and optimized for execution.
The difference between a character device and a block device I/O operation:
A. Block devices can only be input and output in blocks, while character devices are in bytes. Most devices are character devices because they do not require buffering and do not operate at a fixed block size.
B. Block devices have corresponding buffers for I/O requests, so they can respond in any order they choose, and the character device does not need a buffer and can read and write directly. For storage devices, the order of adjustment is important because read-write sequential sectors are faster than detached sectors.
C. Character devices can only be read and written sequentially, while block devices are randomly accessible.
Analogy to a character device, in a block device, there is a structure similar to file_operations in a character device: Block_device_operations; in the Linux kernel, use a gendisk struct to represent a separate disk device or partition ; In the Linux kernel, you use one queue queue to manage I/O requests for this device. This is the overall structure of the block device.
The framework for writing block device drivers is mainly:
1. Assigning Gendisk structures using Alloc_disk (int minors);
2. Setting the GENDISK structure
3. Assigning/Setting queue queues
4. Set additional information for gendisk (e.g. capacity)
5. Register Gendisk structure: Add_disk
Related operation functions:
struct Gendisk *alloc_disk (int minors);//Assign Gendisk structure body
void Add_disk (struct gendisk *disk);//Register Gendisk struct
request_queue_t *blk_init_queue (Request_fn_proc *rfn, spinlock_t *lock);//allocation queue, RFN as a request handler, lock is a spin lock
int Register_blkdev (unsigned int major, const char *name),////register block device, name after registration driver, corresponding to Cat/proc/devices name
Example:
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/79/38/wKioL1aLxAKjuXY0AADbv6cbuZw417.png "title=" capture. PNG "alt=" Wkiol1alxakjuxy0aadbv6cbuzw417.png "/>
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/79/39/wKiom1aLw33z8PiSAALYJy34v2U445.png "title=" capture. PNG "alt=" Wkiom1alw33z8pisaalyjy34v2u445.png "/>
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/79/38/wKioL1aLxCST9-PpAABy1YgnH4w381.png "title=" capture. PNG "alt=" Wkiol1alxcst9-ppaaby1ygnh4w381.png "/>
The above is the framework for block device drivers. Block device drivers are complex relative to character device drivers, but for block devices such as storage devices, such a drive architecture can improve efficiency.
This article is from the "June Feng Eureka" blog, please be sure to keep this source http://10274409.blog.51cto.com/10264409/1731871
Block device drivers