Block device request queue and hard drive

Source: Internet
Author: User

linux0.11 in Reading and writing block equipment, not directly to the block device operation, but by the low-level Ll_rw_block function through the request word and equipment to contact, that is, added an intermediate link. Of course, you can also operate on a block device directly, but there are some problems with direct operation:

1. The same process cannot make a read request in the process of writing, whereas a written request cannot be made during the reading process.

2. When a process is writing or reading a hard disk, the other process cannot issue a hard drive operation command, can only go to sleep, waiting for the hard disk to idle, and can not do other things.

So to solve the above problem, the best way is to define a request queue, the process issued a variety of requests in the queue, as to when the hard drive began to read and write without the tube, the process can perform other operations. Another advantage of defining this queue is the use of so-called elevator algorithms, which can be very effective at reducing the total distance of the head movement and increasing the disk life during frequent disk operations.

The data structure for the requested item defined by linux0.11 is as follows:

struct request {
	int dev;		/*-1 if no request */
	int cmd;		/* READ or WRITE *
	/int errors;
	unsigned long sector;
	unsigned long nr_sectors;
	char * buffer;
	struct task_struct * waiting;
	struct buffer_head * BH;
	struct request * NEXT;
struct request Request[nr_request];
A total of 32 requests are defined, and then another structure is defined for each block device, which specifies the request operation function Do_xx_request for the device.

struct Blk_dev_struct {
	void (*REQUEST_FN) (void);
	struct request * current_request;
struct Blk_dev_struct Blk_dev[nr_blk_dev] = {
	{null, NULL},/		* No_dev/
	{null, NULL},		* Dev mem */
  {null, NULL},/		* Dev FD/
	{null, NULL},/		dev HD/
	{null, NULL},/* Dev		ttyx * *
	NULL, NULL},		/* Dev TTY
	/{null, NULL}		/* Dev LP
/};

Each device occupies a fixed item in the structure array and is consistent with the main device number, for example, for a hard disk device, the main device number is 0x03.

When the hard disk is initialized (Hd_init ()), the operation function of the request item has been specified for the hard disk do_hd_request, and all the hard drive reads and writes will eventually call this function. When the high-level execution block device reads and writes the low-level Ll_rw_block function obtains the main device number, then calls the Make_request function to obtain an empty request item from the request item queue, fills in the request item structure body, finally calls the Add_request function. In the Add_request function, you first determine whether the current request is busy, that is, whether the current_request pointer is empty, or if it is empty, means that the current hard disk is idle, so you can immediately operate the hard drive if Current_ The request pointer is not empty, indicating that the hard drive is busy, inserting the current request item into the request item queue, and noticing that the request items in the queue are sorted by the elevator algorithm before inserting.

The entire call process is shown in the following illustration:


Because of the slow I/O response of the block device, therefore, in the actual reading and writing process needs to arrange a buffer link, in which the existence of high-speed buffer zone has been very good balance between the file system layer and the device layer speed mismatch between the situation, and here in the device layer of the arrangement of the request queue is actually a buffer link, Using a queue data structure, the requests of various block devices are responded in a certain order, which in turn improves the performance of the operating system again.

-----------------------------------------------------------------------------------------------------------


It can be seen from the above figure that the common hard disk mainly consists of 3 parts: Head, track (also called cylinder) and sector. Therefore, the capacity of a hard disk is related to these 3 aspects.

Head (heads): A platter contains two heads (both sides).

Track (cylindrical, cylinders): a circle of concentric circles on a disk.

Sector (sectors): a small area on a track.

Note: The number of sectors on each track is equal , although the track circumference is smaller at the center of the circle. Because the more toward the center, the smaller the area that each sector occupies.
The smallest unit of hard disk reading and writing is sector. Imagine that all tracks of the hard disk are expanded, then end-to-end, all sectors in order, so to get or set the data for one sector of the hard drive (locating a sector), you must supply the 3 parameters to the hard drive: magnetic number one, cylinder number, and sector area code. For example, the MBR sector of the hard disk is positioned as: 0 heads, 0 cylinders, 1 sectors.

The slices of the hard disk are abstracted into logical blocks (block), a logical block occupies two sectors, the hard disk begins to represent the 1th block, so the upper program can access the hard disk through the logical block number. So the upper level is going to access the contents of a logical block on the hard disk, first you convert the logical number to a sequential sector area code (Sector=block x 2), and then convert the sequential sector area code to the number one, the cylinder number, and the sector area code, and then read and write the contents of two sectors in the sector.

The sequential sector area code converts to the number one, the cylinder number and the sector area code are as follows:

Sector/track_secs = integer is tracks, remainder is sec

tracks/dev_heads = integer is cyl, remainder is head

which

Input:sector: Sequential sector area code ; dev_heads: total number of heads; Track_secs: Number of sectors per track; tracks: Total number of current tracks.

Output: Head, Cys, sec




Reference documents:

Zhao, Linux kernel fully commented.





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.