2 、讀入操作。完成了上面的準備工作,下一步就是執行讀操作的核心函數 do_generic_mapping_read , 這是一個比較複雜的函數,裏面有大量的 goto 跳轉,但還是比較清晰的。
它工作過程可以描述如下:
a. 如果所要讀取的文件在頁面緩存中,則跳轉到步驟 d 。
b. 文件還沒有被緩衝,所以要從設備中去讀取,首先分配一個頁面,並將這個頁面鏈入到相應的 address_space 中去
c. 然後調用 address_space 中的 readpage() 函數,去從設備中讀出一個頁面大小的數據到這個頁面緩存中。
d. 檢查 PageUptodate(page)
e. 調用由參數傳入的 actor 函數指針,在此爲 file_read_actor() ,將數據中頁面緩存中拷貝到用戶緩衝區。
f. 如果請求讀取的數據長度已完成,則函數返回,否則跳轉到步驟 a 重複執行。
先看看 file_read_actor() :
int file_read_actor(read_descriptor_t *desc, struct page *page,
unsigned long offset, unsigned long size)
{
char *kaddr;
unsigned long left, count = desc->count;
if (size > count)
size = count;
……
/* Do it the slow way */
kaddr = kmap(page);
left = __copy_to_user(desc->arg.buf, kaddr + offset, size); // 將數據拷貝到用戶空間
kunmap(page);
if (left) {
size -= left;
desc->error = -EFAULT;
}
success:
desc->count = count - size;
desc->written += size;
desc->arg.buf += size;
return size;
}
/**
* This is a generic file read routine, and uses the
* mapping->a_ops->readpage() function for the actual low-level stuff.
*/
void do_generic_mapping_read(struct address_space *mapping,
struct file_ra_state *_ra,
struct file *filp,
loff_t *ppos,
read_descriptor_t *desc,
read_actor_t actor)
{
struct inode *inode = mapping->host;
unsigned long index;
unsigned long end_index;
unsigned long offset;
unsigned long last_index;
unsigned long next_index;
unsigned long prev_index;
loff_t isize;
struct page *cached_page;
int error;
struct file_ra_state ra = *_ra;
cached_page = NULL;
index = *ppos >> PAGE_CACHE_SHIFT;
next_index = index;
prev_index = ra.prev_page;
last_index = (*ppos + desc->count + PAGE_CACHE_SIZE-1) >> PAGE_CACHE_SHIFT;
offset = *ppos & ~PAGE_CACHE_MASK;
isize = i_size_read(inode);
if (!isize)
goto out;
end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
for (;;) {
struct page *page;
unsigned long nr, ret;
/* nr is the maximum number of bytes to copy from this page */
nr = PAGE_CACHE_SIZE;
if (index >= end_index) {
if (index > end_index)
goto out;
nr = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
if (nr <= offset) {
goto out;
}
}
nr = nr - offset;
cond_resched();
if (index == next_index)
next_index = page_cache_readahead(mapping, &ra, filp,
index, last_index - index);
find_page:
page = find_get_page(mapping, index); // 在緩存中查找
if (unlikely(page == NULL)) {
handle_ra_miss(mapping, &ra, index);
goto no_cached_page; // 沒有找到
}
if (!PageUptodate(page)) //Uptodate
goto page_not_up_to_date;
page_ok: // 找到了相關緩存頁面
ret = actor(desc, page, offset, nr); // 拷貝數據到用戶緩衝區
// 更新一些變量值
offset += ret;
index += offset >> PAGE_CACHE_SHIFT;
offset &= ~PAGE_CACHE_MASK;
page_cache_release(page);
if (ret == nr && desc->count)
continue; // 未完成,進入下一次循環
goto out; // 完成
page_not_up_to_date:
/* Get exclusive access to the page ... */
lock_page(page);
/* Did it get truncated before we got the lock? */
if (!page->mapping) {
unlock_page(page);
page_cache_release(page);
continue;
}
/* Did somebody else fill it already? */
if (PageUptodate(page)) {
unlock_page(page);
goto page_ok;
}
readpage : // 讀操作
/* Start the actual read. The read will unlock the page. */
error = mapping->a_ops->readpage(filp, page); // 真正的讀操作
……
/* nr is the maximum number of bytes to copy from this page */
nr = PAGE_CACHE_SIZE;
if (index == end_index) {
nr = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
if (nr <= offset) {
page_cache_release(page);
goto out;
}
}
nr = nr - offset;
goto page_ok;
readpage_error:
/* UHHUH! A synchronous read error occurred. Report it */
desc->error = error;
page_cache_release(page);
goto out;
no_cached_page: // 分配一個新的頁面,比將它鏈入緩存樹中。
/*
* Ok, it wasn't cached, so we need to create a new
* page..
*/
if (!cached_page) {
cached_page = page_cache_alloc_cold(mapping);
if (!cached_page) {
desc->error = -ENOMEM;
goto out;
}
}
error = add_to_page_cache_lru(cached_page, mapping,
index, GFP_KERNEL);
page = cached_page;
cached_page = NULL;
goto readpage ;
}
out:
*_ra = ra;
*ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
if (cached_page)
page_cache_release(cached_page);
if (filp)
file_accessed(filp);
}
3 、從設備讀取
對於不同的文件系統有不同的 address_space ,而且有不同的 address_space_operations ,對於 ext2 文件系統來說,這個是如下一個結構:
const struct address_space_operations ext2_aops = {
.readpage = ext2_readpage,
.readpages = ext2_readpages,
.writepage = ext2_writepage,
.sync_page = block_sync_page,
.prepare_write = ext2_prepare_write,
.commit_write = generic_commit_write,
.bmap = ext2_bmap,
.direct_IO = ext2_direct_IO,
.writepages = ext2_writepages,
.migratepage = buffer_migrate_page,
};
可見,這個 readpage() 便是 ext2_readpage() , 它負責從設備中讀取一個頁面。
static int ext2_readpage(struct file *file, struct page *page)
{
return mpage_readpage(page, ext2_get_block);
}
/*
* This isn't called much at all
*/
int mpage_readpage(struct page *page, get_block_t get_block)
{
struct bio *bio = NULL;
sector_t last_block_in_bio = 0;
struct buffer_head map_bh;
unsigned long first_logical_block = 0;
clear_buffer_mapped(&map_bh);
bio = do_mpage_readpage(bio, page, 1, &last_block_in_bio,
&map_bh, &first_logical_block, get_block);
if (bio)
mpage_bio_submit(READ, bio);
return 0;
}
這個函數最終將讀請求轉成 submit_bio() ,之後就是通用塊層的事情了。