Block device operations in llinux

Source: Internet
Author: User

When the kernel wants a device to send a read/write request or other requests, ll_rw_block will be based on the operation commands specified by its parameters and the device number in the data buffer block header, use the do_xx_request function of the corresponding request item to create a block device request item, and use the elevator algorithm to insert the new request item into the request item queue.

1.

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 [nr_request];

2.

Struct blk_dev_struct {

(Void *) (request_fn )();

Struct Request * current_request;

}

Struct blk_dev_struct blk_dev [nr_blk];

The current request item in each block device and the request item linked list of the device in the request item array constitute the Request queue of the current block device.

 

For a currently idle block device, when ll_rw_block creates the first request item for it, the current request item pointer will point to the newly created request item, and immediately call the request item operation function of the current block device to perform read and write operations.

 

3.

In end_request (), there are:

# Define current (blk_dev [major_nr]-> currnt_request)

# Define current_dev device_nr (current-> Dev)
Wake_up (& Current-> waiting );
Wake_up (& wait_for_request );
The third sentence is to wake up the process waiting for the device. What does wait_for_request in the second sentence mean?
Zhao Bo said in the extern struct task_struct * wait_for_request; that is, the structure of the task waiting for the request. When performing I/O operations on Block devices, you need to use the request item

Struct request [nr_reauest];

Therefore, the request item itself is a kind of resource, so the second sentence consciousness is to wake up those processes waiting for available idle requests to wait in the queue.

Current-> waiting is the process queue of the current request item, and wait_for_rquest is the process waiting queue waiting for the available request item

 

For write requests, the request item Pointer Points to 2/3 in the request item array.

 

1. Handling of read/write hard disk errors

The bad_wr_intr function is called to determine whether the number of failures exceeds the maximum number of errors. If yes, the process waiting for the request to be sent is ended and the process waiting for the request to be sent is wakened, and the flag of the buffer corresponding to the slot, indicating that the buffer is not updated. If the number of errors exceeds half of the maximum error value, you must set the hard disk controller.

Static void bad_wr_intr (void)

{

If (++ current-> error> = max_errors)

End_request (0 );

If (current-> error> max_errors/2)

Reset = 1;

}

// Reset flag during reset. This flag is set when read/write hard disk errors occur and relevant reset functions are called to reset the hard disk controller and hard disk.

Static int reset = 1;

 

Reset hard disk controller function

Static int reset_controller (void)

{

Unsigned int I;

Oub_p (4, hd_cmd); // send the reset byte to the Controller

For (I = 0; I <100; I ++) {}; // wait for a while

Oub_p (hd_info [0]. CTL & 0x0f), hd_cmd): // send normal control bytes, the hd_info [0] information is transmitted through the BIOS to initialize the hard disk information array.

If (device_busy) // wait until the hard disk is ready; otherwise, an error message is displayed.

Printk ("HD controller still busy/N ");

If (I = inb_p (hd_err ))! = 1)

Printk ("HD controller reset failed! /N ");

}

Reset Hard Disk

Static void reset_hd (INT nr)
{
Reset_controller ();
Hd_out (NR, hd_info [Nr]. sect, hd_info [Nr]. sect, hd_info [Nr]. Head-1,

Hd_info [Nr]. cyl, win_specify, & recal_intr );
}

Win_specify indicates that the hard drive parameters are created. recal_intr is the re-correction processing function called in the hard drive interrupt processing function.

 

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.