Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 6: Device Drivers


sample_requestis a hardware-independent sample routine forrequest_fnthat illustrates the basic
steps performed by all drivers.

void sample_request (request_queue_t *q)
int status;
struct request *req;

while ((req = elv_next_request(q)) != NULL)
if (!blk_fs_request(req))
end_request(req, 0);
continue;

status = perform_sample_transfer(req);
end_request(req, status);

The basic layout of the strategy function is simple;elv_next_request— embedded in awhile
loop — is used to read the requests sequentially from the queue. Transfer itself is carried out by
perform_sample_transfer.end_requestis a standard kernel function to delete the request from the
request queue, to update the kernel statistics, and to execute any completions (see Chapter 5) waiting
inrequest->completion. Also invoked is the BIO-specificbi_end_iofunction to which the kernel can
assign a cleanup that depends on the purpose of the BIO.

As BIOs can be used to transfer not only data but also diagnostics information, the driver must invoke
blk_fs_requestto check whether, in fact, data are to be transferred — for the sake of simplicity, I ignore
all other types of transfer.

The hardware-specific actions in genuine drivers are typically segregated into separate functions
to keep code concise. I have adopted the same approach in our sample strategy function. The
hardware-specific functions that would be found in a genuine driver are replaced with comments in
perform_sample_transfer.

int perform_transfer(request *req)
switch(req->cmd)
case READ:
/* Perform hardware-specific reading of data */
break;
case WRITE:
/* Perform hardware-specific writing of data */
break;
default:
return -EFAULT;

Thecmdfield is referenced to establish whether the request is for a read or write operation. The appropri-
ate actions are then taken to transfer the data between the system and the hardware.

6.5.8 I/O Scheduling


The various algorithms employed in the kernel for scheduling and reordering I/O operations are known
asI/O schedulers(in contrast to normal process schedulers or packet schedulers for traffic shaping in
networks). Traditionally, I/O schedulers are also calledelevators. They are represented by a collection of
functions grouped together in the following data structure^15 :

(^15) The kernel also definestypedef struct elevator_s elevator_t.

Free download pdf