1. Field of Invention
The present invention relates to a data access method, and more particularly, to a data access method for making an asynchronous request to a block device.
2. Related Art
A Linux operating system includes a character device and a block device. Access modes of the two types of devices are entirely different. The character device is accessed through character transfer, while the block device is accessed in a unit of block. The character device does not need buffering and is not operated in a fixed block size. The block device has corresponding buffer areas for access requests, so that a particular sequence may be selected to perform operations. The character device does not set any buffer space, and can thus be accessed directly. Besides, the character device may only be read in sequentially, while the block device may be accessed randomly. Though the block device may be accessed randomly, for mechanic devices such as magnetic disks, a magnetic head has to be moved around in a random access, which may affect the access performance of the magnetic disks.
Thereby, when a large number of data access requests take place, the data access performance of the block device is easily degraded due to discontinuous access addresses.
Accordingly, the present invention is a data access method for making an asynchronous request to a block device, which is adapted to perform corresponding access processing according to a plurality of data access requests.
In order to achieve the above objective, a data access method for making an asynchronous request to a block device is provided. The method comprises the following steps. A current request is received from a client. A virtual device of the block device is invoked. The current request is compared with a request queue storing a plurality of requests to be processed. The corresponding requests to be processed are selected from the request queue according to an access address of the current request. A request merging program is performed on the requests to be processed with adjacent access addresses and the current request to generate a merged request. The merged request is submitted to the request queue.
In view of the above, an asynchronous processing method for data access requests is adapted to merge the data access requests for accessing the block device, such that the data access requests with continuous access addresses are merged into a single data access request, thereby enhancing the access efficiency of the block device.
The present invention will become more fully understood from the detailed description given herein below for illustration only, and thus are not limitative of the present invention, and wherein:
In Step S210, a current request is received from a client.
In Step S220, a virtual device of the block device is invoked.
In Step S230, the current request is compared with a request queue of the virtual device that stores a plurality of requests to be processed.
In Step S240, the corresponding requests to be processed are selected from the request queue according to an access address of the current request.
In Step S250, a request merging program is performed on the requests to be processed with adjacent access addresses and the current request to generate a merged request.
In Step S260, the merged request is submitted to the request queue of the virtual device.
In Step S270, the request of the virtual device is submitted to a physical block device.
First, the client 110 sends the current request to the server end 120, so as to access data from the corresponding block device 130. Next, after obtaining the current request, the server end 120 compares access addresses of the current request and the requests to be processed stored in the request queue. In the present invention, the access addresses are selected according to the continuity of the access addresses and sequentiality of the data access requests.
In Step S241, at least one of the requests to be processed is selected from the request queue.
In Step S242, it is determined whether the access addresses of the selected data access requests that are received and the access address of the current request are continuous addresses.
In Step S243, it is determined whether an access capacity of the merged request exceeds a block size of the block device after the current request and the request to be processed are merged.
In Step S244, the request is no longer merged if the access capacity of the merged request is larger than the block size of the block device.
The above selection process is based on the sequentially of the data access requests and the continuity of the access addresses. In other words, a writing sequence of the current request and the requests to be processed serves as a condition of the selection. Moreover, the access address of the current request and the access addresses of the requests to be processed are regarded as another set of selection conditions. The reason is that the data access requests sent by the client 110 are not intended to access the continuous addresses of the block devices 130. If each data access request has discontinuous access addresses, the access efficiency of the block device 130 is affected as the block device 130 accesses data at these access addresses by leaping. Further, in order to avoid that the amount of accessed data after the merge is greater than the block size of the block device 130, it is determined in Step S243 whether the access capacity of each merged request exceeds the block size of the block device 130.
In Step S271, a request is obtained from the request queue of the virtual block device.
In Step S272, the request is submitted to the physical device.
In Step S273, it is determined whether the submission is successful.
In Step S274, it is determined whether the request is a merged request if the request submission fails.
In Step S275, if the request is a merged request, an error is returned.
In Step S276, if the request is not a merged request, the request is split into the original requests and errors are sequentially returned.
In Step S277, a complete message is returned if the request submission is successful.
To further illustrate the operation details of the present invention, the following program structure is illustrated together with the operation flow. First, a structure of a data access request is defined (based on linux2.6 kernel):
A structure for recording the merge information:
The prototype of a request callback function is defined as:
typedef void (end_req_t) (struct*v_request, unsigned int size, int result);
A block device is defined as:
The process of receiving the current request from the client 110 is as follows.
In Step S210, a current request sent to the block device 130 is received. A v_request structure is configured for the current request. An index of the current request is stored in an ori_req member of the v_request structure. Meanwhile, a device identifier, initialized position, and size of the current request are stored in corresponding members of the v_request structure.
Next, a v_device device related to the block device 130 (i.e., the virtual device) is searched. If the device is not found, an access error is returned; otherwise, Step S230 is performed.
In Step S230, a request queue is obtained from the v_device device, and the last request to be processed in the request queue is obtained. It is calculated whether the request to be processed may be merged with the current request, and if yes, Step S250 is performed; otherwise, Step S260 is performed.
In Step S250, the request to be processed and the current request are merged into a merged request. Information such as the initialized address and size of the merged request is filled into a minfo member of the v_request structure, and Step S260 is performed.
In Step S260, the merged request is added into the request queue.
The process of submitting the current request is as follows.
Firstly, a first data access request is obtained from the request queue. The first data access request in the request queue means the data access request stored in the request queue for the longest time. Then, the data access request is submitted to the block device 130, and it is determined whether the minfo member of the data access request is null or not. If the minfo member is null, the data access request is not a merged request, and ori_make_request_fn of the v_device is invoked to submit the request; otherwise, the data access request is a merged request, and ori_make_request_fn is invoked for submission according to information in the minfo.
When the submission or request processing fails, if the data access request is not a merged request, an error is returned. Otherwise, the data access request is split into the original requests and errors are sequentially returned. During the split, information stored in the mreq queue in the minfo member is used.
In view of the above, an asynchronous processing method for data access requests is adapted to merge the data access requests for accessing the block device 130, such that the data access requests with continuous access addresses are merged into a single data access request, thereby enhancing the access efficiency of the block device 130.