writeback机制源码分析(3)

writeback线程是bdi_forker_thread 创建的,该线程的任务就是处理等待的数据回刷任务。线程处理函数为bdi_writeback_thread,其会调用wb_do_writeback函数完成具体操作,该函数分析如下:

long wb_do_writeback(struct bdi_writeback *wb, int force_wait)
{
struct backing_dev_info *bdi = wb->bdi;
struct wb_writeback_work *work;
long wrote = 0;

set_bit(BDI_writeback_running, &wb->bdi->state);
/* 处理等待的work,所有等待work pengding在bdi->work_list上 */
while ((work = get_next_work_item(bdi)) != NULL) {
/*
* Override sync mode, in case we must wait for completion
* because this thread is exiting now.
*/
if (force_wait)
work->sync_mode = WB_SYNC_ALL;

trace_writeback_exec(bdi, work);
/* 调用wb_writeback函数处理相应的inode */
wrote += wb_writeback(wb, work);

/*
* Notify the caller of completion if this is a synchronous
* work item, otherwise just free it.
*/
/* 通知上层软件,相应的work已经完成 */
if (work->done)
complete(work->done);
else
kfree(work);
}

/*
* Check for periodic writeback, kupdated() style
*/
/* 处理周期性的dirty page刷新作业,buffer cache就会走这条路径,在下面的函数中会创建work,并且调用wb_writeback函数进行处理 */
wrote += wb_check_old_data_flush(wb);
wrote += wb_check_background_flush(wb);
clear_bit(BDI_writeback_running, &wb->bdi->state);

return wrote;
}

小结

本文在linux-3.2的基础上对writeback代码进行了浏览。整体上来讲,writeback机制是比较简单的,其核心是通过一个常驻内核线程为bdi对象分配writeback线程,实现对cache中dirty page的数据回刷。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/3cd45cd455010d40942cb35c7bf40743.html