struct deadline_data {
/*
* run time data
*/
/*
* requests (deadline_rq s) are present on both sort_list and fifo_list
*/
struct rb_root sort_list[2];
struct list_head fifo_list[2];
/*
* next in sort order. read, write or both are NULL
*/
struct request *next_rq[2];
unsigned int batching; /* number of sequential requests made */
unsigned int starved; /* times reads have starved writes */
/*
* settings that change how the i/o scheduler behaves
*/
int fifo_expire[2];
int fifo_batch;
int writes_starved;
int front_merges;
};
static const int read_expire = HZ / 2; /* max time before a read is submitted. */
static const int write_expire = 5 * HZ; /* ditto for writes, these limits are SOFT! */
static const int writes_starved = 2; /* max times reads can starve a write */
static const int fifo_batch = 16; /* # of sequential requests treated as one
by the above parameters. For throughput. */
INIT_LIST_HEAD(&dd->fifo_list[READ]);
INIT_LIST_HEAD(&dd->fifo_list[WRITE]);
dd->sort_list[READ] = RB_ROOT; //红黑树的根节点,初始化为NULL, #define RB_ROOT (struct rb_root) { NULL, }
dd->sort_list[WRITE] = RB_ROOT;
dd->fifo_expire[READ] = read_expire;
dd->fifo_expire[WRITE] = write_expire;
dd->writes_starved = writes_starved;
dd->front_merges = 1;
dd->fifo_batch = fifo_batch;
static int
deadline_merge(struct request_queue *q, struct request **req, struct bio *bio)
{
struct deadline_data *dd = q->elevator->elevator_data;
struct request *__rq;
int ret;
/*
* check for front merge
*/
if (dd->front_merges) { //如果可以进行前向合并
sector_t sector = bio_end_sector(bio); //找到bio的结束扇区号
//如果能找到一个请求,它的起始扇区号和sector相同(就是连续的)
__rq = elv_rb_find(&dd->sort_list[bio_data_dir(bio)], sector);
if (__rq) {
BUG_ON(sector != blk_rq_pos(__rq));
//进行合并,然后成功返回ret,表示前向合并完成
if (elv_rq_merge_ok(__rq, bio)) {
ret = ELEVATOR_FRONT_MERGE;
goto out;
}
}
}
return ELEVATOR_NO_MERGE;
out:
*req = __rq;
return ret;
}
static void
deadline_merged_requests(struct request_queue *q, struct request *req,
struct request *next)
{
/*
* if next expires before rq, assign its expire time to rq
* and move into next position (next will be deleted) in fifo
*/
if (!list_empty(&req->queuelist) && !list_empty(&next->queuelist)) {
if (time_before(next->fifo_time, req->fifo_time)) { //如果next的期限小于req
list_move(&req->queuelist, &next->queuelist); //这个queuelist实际上就是真正发给设备驱动程序的队列,处理完了的队列
req->fifo_time = next->fifo_time; //因为是next比req要先响应,合并完了肯定要以先响应的为准
}
}
/*
* kill knowledge of next, this one is a goner
*/
deadline_remove_request(q, next); //从fifo_list和sort_list删除next
}
static void deadline_merged_request(struct request_queue *q,
struct request *req, int type)
{
struct deadline_data *dd = q->elevator->elevator_data;
/*
* if the merge was a front merge, we need to reposition request
*/
if (type == ELEVATOR_FRONT_MERGE) {
elv_rb_del(deadline_rb_root(dd, req), req);
deadline_add_rq_rb(dd, req);
}
}
到这终于合并完请求了,接下来是加入请求,通过blk_queue_bio调用__elv_add_request,__elv_add_request函数又调用q->elevator->type->ops.elevator_add_req_fn,这个函数在deadline算法中被定义为deadline_add_request
/*
* add rq to rbtree and fifo
*/
static void
deadline_add_request(struct request_queue *q, struct request *rq)
{
struct deadline_data *dd = q->elevator->elevator_data;
const int data_dir = rq_data_dir(rq); // 获取请求的方向(direction),读/写
deadline_add_rq_rb(dd, rq); //加入sort_list,rb是readblack的缩写,也就是红黑树
/*
* set expire time and add to fifo list
*/
rq->fifo_time = jiffies + dd->fifo_expire[data_dir]; //jiffies是系统全局变量
list_add_tail(&rq->queuelist, &dd->fifo_list[data_dir]); //将rq加入fifo_list
}
static inline struct rb_root *
deadline_rb_root(struct deadline_data *dd, struct request *rq)
{
return &dd->sort_list[rq_data_dir(rq)];
}
static void
deadline_add_rq_rb(struct deadline_data *dd, struct request *rq)
{
//获取sort_list中指向根节点的指针root,因为从rb_node开始才是正式的红黑树,root只是指向根节点的指针
struct rb_root *root = deadline_rb_root(dd, rq);
//这个函数会把rq的rb_node指针指向sort_list的一个节点,也就是把每个请求与sort_list关联
elv_rb_add(root, rq);
}
最后是dispatch请求
static int deadline_dispatch_requests(struct request_queue *q, int force)
{
struct deadline_data *dd = q->elevator->elevator_data;
const int reads = !list_empty(&dd->fifo_list[READ]);
const int writes = !list_empty(&dd->fifo_list[WRITE]);
struct request *rq;
int data_dir;
/*
* batches are currently reads XOR writes
*/
if (dd->next_rq[WRITE])
rq = dd->next_rq[WRITE];
else
rq = dd->next_rq[READ];
//如果有rq并且批量dispatch未到上限,则直接进行dispatch
if (rq && dd->batching < dd->fifo_batch)
/* we have a next request are still entitled to batch */
goto dispatch_request;
/*
* at this point we are not running a batch. select the appropriate
* data direction (read / write)
*/
if (reads) {
BUG_ON(RB_EMPTY_ROOT(&dd->sort_list[READ]));
//触发写请求饥饿线,必须处理写请求了
if (writes && (dd->starved++ >= dd->writes_starved))
goto dispatch_writes;
//同时把请求方向变为写
data_dir = READ;
goto dispatch_find_request;
}
/*
* there are either no reads or writes have been starved
*/
if (writes) {
dispatch_writes:
BUG_ON(RB_EMPTY_ROOT(&dd->sort_list[WRITE]));
dd->starved = 0;
data_dir = WRITE;
goto dispatch_find_request;
}
return 0;
dispatch_find_request:
/*
* we are not running a batch, find best request for selected data_dir
*/
//如果fifo_list有超时或者下一个请求的方向变了,就去fifo_list取request,然后还得执行下面的dispatch_request
if (deadline_check_fifo(dd, data_dir) || !dd->next_rq[data_dir]) {
/*
* A deadline has expired, the last request was in the other
* direction, or we have run out of higher-sectored requests.
* Start again from the request with the earliest expiry time. */
rq = rq_entry_fifo(dd->fifo_list[data_dir].next);
} else { // 为什么这里可以直接从fifo_list取而不是sort_list,因为最终都需要通过rq的rb_node到sort_list找
/*
* The last req was the same dir and we have a next request in
* sort order. No expired requests so continue on from here.
*/
rq = dd->next_rq[data_dir]; //否则接着取请求
}
dd->batching = 0; //表示启动新一批请求dispatch
dispatch_request:
/*
* rq is the selected appropriate request.
*/
dd->batching++;
deadline_move_request(dd, rq); //从fifo_list和sort_list中删除,再加入req->queuelist中,这里就从IO调度层离开了
return 1;
}