将数据写到page cache

写文件的时候其实是通过文件系统写到page cache中,然后再由相应的线程在适当的时机将page cache中的数据写到磁盘中。

//fs/fat/file.c
const struct file_operations fat_file_operations = {
...
.aio_write = generic_file_aio_write,
...
};

//mm/filemap.c
generic_file_aio_write
__generic_file_aio_write
generic_file_buffered_write
generic_perform_write
a_ops->write_begin
iov_iter_copy_from_user_atomic(page, i, offset, bytes)
a_ops->write_end

回写page cache到磁盘


通过sync系统调用将page cache写到磁盘中

//fs/fat/file.c
const struct file_operations fat_file_operations = {
...
.fsync = fat_file_fsync,
...
};

fat_file_fsync
generic_file_fsync
sync_inode_metadata
sync_inode
writeback_single_inode
__writeback_single_inode
do_writepages
mapping->a_ops->writepages
write_inode

通过回写工作队列bdi_writeback将page cache写到磁盘中

//mm/backing_dev.c
bdi_init
bdi_wb_init
INIT_DELAYED_WORK(&wb->dwork, bdi_writeback_workfn);

//fs/fs-writeback.c
bdi_writeback_workfn
wb_do_writeback
wb_writeback
writeback_sb_inodes
__writeback_single_inode
do_writepages
mapping->a_ops->writepages
write_inode

在这里初始化的时候安装了回写wb->dwork,并且wb->dwork的回调函数和前面系统sync系统调用一样最终都会调用__writeback_single_inode来回写page cache。

blk_init_queue
blk_init_queue_node
blk_alloc_queue_node
bdi_init(&q->backing_dev_info);
setup_timer(&q->backing_dev_info.laptop_mode_wb_timer, laptop_mode_timer_fn, ...

//mm/page-writeback.c
laptop_mode_timer_fn
bdi_start_writeback //fs/fs-writeback.c
__bdi_start_writeback
bdi_queue_work
mod_delayed_work(bdi_wq, &bdi->wb.dwork, 0);

在blk_init_queue时会初始化一个timer,并且timer的回调函数是laptop_mode_timer_fn,他里面会通过bdi_queue_work来调度回写wb->dwork。这样这个回写wb->dwork就会不断的被这个timer定时的调度执行了。