1949/** 1950 * generic_file_buffered_read - generic file read routine 1951 * @iocb: the iocb to read 1952 * @iter: data destination 1953 * @written: already copied 1954 * 1955 * This is a generic file read routine, and uses the 1956 * mapping->a_ops->readpage() function for the actual low-level stuff. 1957 * 1958 * This is really ugly. But the goto's actually try to clarify some 1959 * of the logic when it comes to error handling etc. 1960 */ 1961staticssize_tgeneric_file_buffered_read(struct kiocb *iocb, 1962struct iov_iter *iter, ssize_t written) 1963 {
263/** 264 * find_get_page - find and get a page reference 265 * @mapping: the address_space to search 266 * @offset: the page index 267 * 268 * Looks up the page cache slot at @mapping & @offset. If there is a 269 * page cache page, it is returned with an increased refcount. 270 * 271 * Otherwise, %NULL is returned. 272 */ 273staticinlinestruct page *find_get_page(struct address_space *mapping, 274pgoff_t offset) 275 { 276return pagecache_get_page(mapping, offset, 0, 0); 277 }
489/** 490 * page_cache_sync_readahead - generic file readahead 491 * @mapping: address_space which holds the pagecache and I/O vectors 492 * @ra: file_ra_state which holds the readahead state 493 * @filp: passed on to ->readpage() and ->readpages() 494 * @offset: start offset into @mapping, in pagecache page-sized units 495 * @req_size: hint: total size of the read which the caller is performing in 496 * pagecache pages 497 * 498 * page_cache_sync_readahead() should be called when a cache miss happened: 499 * it will submit the read. The readahead logic may decide to piggyback more 500 * pages onto the read request if access patterns suggest it will improve 501 * performance. 502 */ 503voidpage_cache_sync_readahead(struct address_space *mapping, 504struct file_ra_state *ra, struct file *filp, 505pgoff_t offset, unsignedlong req_size) 506 { 507/* no read-ahead */ 508if (!ra->ra_pages) 509return; 510 511/* be dumb */ 512if (filp && (filp->f_mode & FMODE_RANDOM)) { 513 force_page_cache_readahead(mapping, filp, offset, req_size); 514return; 515 } 516 517/* do read-ahead */ 518 ondemand_readahead(mapping, ra, filp, false, offset, req_size); 519 }
142/* 143 * __do_page_cache_readahead() actually reads a chunk of disk. It allocates all 144 * the pages first, then submits them all for I/O. This avoids the very bad 145 * behaviour which would occur if page allocations are causing VM writeback. 146 * We really don't want to intermingle reads and writes like that. 147 * 148 * Returns the number of pages requested, or the maximum amount of I/O allowed. 149 */ 150int __do_page_cache_readahead(struct address_space *mapping, struct file *filp, 151pgoff_t offset, unsignedlong nr_to_read, 152unsignedlong lookahead_size)
先分配all pages再开始IO:
193/* 194 * Now start the IO. We ignore I/O errors - if the page is not 195 * uptodate then the caller will launch readpage again, and 196 * will then handle the error. 197 */ 198if (ret) 199 read_pages(mapping, filp, &page_pool, ret, gfp_mask);
2727/* 2728 * Page is not up to date and may be locked due one of the following 2729 * case a: Page is being filled and the page lock is held 2730 * case b: Read/write error clearing the page uptodate status 2731 * case c: Truncation in progress (page locked) 2732 * case d: Reclaim in progress 2733 * ... 2757 */ 2758 wait_on_page_locked(page); 2759if (PageUptodate(page)) 2760goto out; 2761 2762/* Distinguish between all the cases under the safety of the lock */ 2763 lock_page(page); 2764 2765/* Case c or d, restart the operation */ 2766if (!page->mapping) { 2767 unlock_page(page); 2768 put_page(page); 2769goto repeat; 2770 } 2771 2772/* Someone else locked and filled the page in a very small window */ 2773if (PageUptodate(page)) { 2774 unlock_page(page); 2775goto out; 2776 } 2777goto filler; 2778 2779 out: 2780 mark_page_accessed(page); 2781return page; 2782 }
可见,page的状态有多繁琐。
等待page被解锁:
516/* 517 * Wait for a page to be unlocked. 518 * 519 * This must be called with the caller "holding" the page, 520 * ie with increased "page->count" so that the page won't 521 * go away during the wait.. 522 */ 523staticinlinevoidwait_on_page_locked(struct page *page) 524 { 525if (PageLocked(page)) 526 wait_on_page_bit(compound_head(page), PG_locked); 527 }
ok,so 等待unlock后,pageuptodate走page_ok, cp to user then next page:
2091/* 2092 * Ok, we have the page, and it's up-to-date, so 2093 * now we can copy it to user space... 2094 */ 2095 2096 ret = copy_page_to_iter(page, offset, nr, iter); 2097 offset += ret; 2098 index += offset >> PAGE_SHIFT; 2099 offset &= ~PAGE_MASK; 2100 prev_offset = offset; 2101 2102 put_page(page); 2103 written += ret; 2104if (!iov_iter_count(iter)) 2105goto out; 2106if (ret < nr) { 2107 error = -EFAULT; 2108goto out; 2109 } 2110continue;