Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * mm/readahead.c - address_space-level file readahead. |
| 3 | * |
| 4 | * Copyright (C) 2002, Linus Torvalds |
| 5 | * |
| 6 | * 09Apr2002 akpm@zip.com.au |
| 7 | * Initial version. |
| 8 | */ |
| 9 | |
| 10 | #include <linux/kernel.h> |
| 11 | #include <linux/fs.h> |
| 12 | #include <linux/mm.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/blkdev.h> |
| 15 | #include <linux/backing-dev.h> |
Andrew Morton | 8bde37f | 2006-12-10 02:19:40 -0800 | [diff] [blame] | 16 | #include <linux/task_io_accounting_ops.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/pagevec.h> |
| 18 | |
| 19 | void default_unplug_io_fn(struct backing_dev_info *bdi, struct page *page) |
| 20 | { |
| 21 | } |
| 22 | EXPORT_SYMBOL(default_unplug_io_fn); |
| 23 | |
| 24 | struct backing_dev_info default_backing_dev_info = { |
| 25 | .ra_pages = (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE, |
| 26 | .state = 0, |
| 27 | .capabilities = BDI_CAP_MAP_COPY, |
| 28 | .unplug_io_fn = default_unplug_io_fn, |
| 29 | }; |
| 30 | EXPORT_SYMBOL_GPL(default_backing_dev_info); |
| 31 | |
| 32 | /* |
| 33 | * Initialise a struct file's readahead state. Assumes that the caller has |
| 34 | * memset *ra to zero. |
| 35 | */ |
| 36 | void |
| 37 | file_ra_state_init(struct file_ra_state *ra, struct address_space *mapping) |
| 38 | { |
| 39 | ra->ra_pages = mapping->backing_dev_info->ra_pages; |
Jan Kara | 6ce745e | 2007-05-06 14:49:26 -0700 | [diff] [blame] | 40 | ra->prev_index = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | } |
Steven Whitehouse | d41cc70 | 2006-01-30 08:53:33 +0000 | [diff] [blame] | 42 | EXPORT_SYMBOL_GPL(file_ra_state_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
| 44 | /* |
| 45 | * Return max readahead size for this inode in number-of-pages. |
| 46 | */ |
| 47 | static inline unsigned long get_max_readahead(struct file_ra_state *ra) |
| 48 | { |
| 49 | return ra->ra_pages; |
| 50 | } |
| 51 | |
| 52 | static inline unsigned long get_min_readahead(struct file_ra_state *ra) |
| 53 | { |
| 54 | return (VM_MIN_READAHEAD * 1024) / PAGE_CACHE_SIZE; |
| 55 | } |
| 56 | |
Oleg Nesterov | a564da3 | 2006-03-22 00:08:47 -0800 | [diff] [blame] | 57 | static inline void reset_ahead_window(struct file_ra_state *ra) |
| 58 | { |
| 59 | /* |
| 60 | * ... but preserve ahead_start + ahead_size value, |
| 61 | * see 'recheck:' label in page_cache_readahead(). |
| 62 | * Note: We never use ->ahead_size as rvalue without |
| 63 | * checking ->ahead_start != 0 first. |
| 64 | */ |
| 65 | ra->ahead_size += ra->ahead_start; |
| 66 | ra->ahead_start = 0; |
| 67 | } |
| 68 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | static inline void ra_off(struct file_ra_state *ra) |
| 70 | { |
| 71 | ra->start = 0; |
| 72 | ra->flags = 0; |
| 73 | ra->size = 0; |
Oleg Nesterov | a564da3 | 2006-03-22 00:08:47 -0800 | [diff] [blame] | 74 | reset_ahead_window(ra); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | return; |
| 76 | } |
| 77 | |
| 78 | /* |
| 79 | * Set the initial window size, round to next power of 2 and square |
| 80 | * for small size, x 4 for medium, and x 2 for large |
| 81 | * for 128k (32 page) max ra |
| 82 | * 1-8 page = 32k initial, > 8 page = 128k initial |
| 83 | */ |
| 84 | static unsigned long get_init_ra_size(unsigned long size, unsigned long max) |
| 85 | { |
| 86 | unsigned long newsize = roundup_pow_of_two(size); |
| 87 | |
Steven Pratt | aed75ff | 2006-03-22 00:08:48 -0800 | [diff] [blame] | 88 | if (newsize <= max / 32) |
| 89 | newsize = newsize * 4; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | else if (newsize <= max / 4) |
Steven Pratt | aed75ff | 2006-03-22 00:08:48 -0800 | [diff] [blame] | 91 | newsize = newsize * 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | else |
| 93 | newsize = max; |
| 94 | return newsize; |
| 95 | } |
| 96 | |
| 97 | /* |
| 98 | * Set the new window size, this is called only when I/O is to be submitted, |
| 99 | * not for each call to readahead. If a cache miss occured, reduce next I/O |
| 100 | * size, else increase depending on how close to max we are. |
| 101 | */ |
| 102 | static inline unsigned long get_next_ra_size(struct file_ra_state *ra) |
| 103 | { |
| 104 | unsigned long max = get_max_readahead(ra); |
| 105 | unsigned long min = get_min_readahead(ra); |
| 106 | unsigned long cur = ra->size; |
| 107 | unsigned long newsize; |
| 108 | |
| 109 | if (ra->flags & RA_FLAG_MISS) { |
| 110 | ra->flags &= ~RA_FLAG_MISS; |
| 111 | newsize = max((cur - 2), min); |
| 112 | } else if (cur < max / 16) { |
| 113 | newsize = 4 * cur; |
| 114 | } else { |
| 115 | newsize = 2 * cur; |
| 116 | } |
| 117 | return min(newsize, max); |
| 118 | } |
| 119 | |
| 120 | #define list_to_page(head) (list_entry((head)->prev, struct page, lru)) |
| 121 | |
| 122 | /** |
Randy Dunlap | bd40cdd | 2006-06-25 05:48:08 -0700 | [diff] [blame] | 123 | * read_cache_pages - populate an address space with some pages & start reads against them |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | * @mapping: the address_space |
| 125 | * @pages: The address of a list_head which contains the target pages. These |
| 126 | * pages have their ->index populated and are otherwise uninitialised. |
| 127 | * @filler: callback routine for filling a single page. |
| 128 | * @data: private data for the callback routine. |
| 129 | * |
| 130 | * Hides the details of the LRU cache etc from the filesystems. |
| 131 | */ |
| 132 | int read_cache_pages(struct address_space *mapping, struct list_head *pages, |
| 133 | int (*filler)(void *, struct page *), void *data) |
| 134 | { |
| 135 | struct page *page; |
| 136 | struct pagevec lru_pvec; |
| 137 | int ret = 0; |
| 138 | |
| 139 | pagevec_init(&lru_pvec, 0); |
| 140 | |
| 141 | while (!list_empty(pages)) { |
| 142 | page = list_to_page(pages); |
| 143 | list_del(&page->lru); |
| 144 | if (add_to_page_cache(page, mapping, page->index, GFP_KERNEL)) { |
| 145 | page_cache_release(page); |
| 146 | continue; |
| 147 | } |
| 148 | ret = filler(data, page); |
| 149 | if (!pagevec_add(&lru_pvec, page)) |
| 150 | __pagevec_lru_add(&lru_pvec); |
| 151 | if (ret) { |
OGAWA Hirofumi | 38da288 | 2006-12-06 20:36:46 -0800 | [diff] [blame] | 152 | put_pages_list(pages); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | break; |
| 154 | } |
Andrew Morton | 8bde37f | 2006-12-10 02:19:40 -0800 | [diff] [blame] | 155 | task_io_account_read(PAGE_CACHE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | } |
| 157 | pagevec_lru_add(&lru_pvec); |
| 158 | return ret; |
| 159 | } |
| 160 | |
| 161 | EXPORT_SYMBOL(read_cache_pages); |
| 162 | |
| 163 | static int read_pages(struct address_space *mapping, struct file *filp, |
| 164 | struct list_head *pages, unsigned nr_pages) |
| 165 | { |
| 166 | unsigned page_idx; |
| 167 | struct pagevec lru_pvec; |
Zach Brown | 994fc28c | 2005-12-15 14:28:17 -0800 | [diff] [blame] | 168 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | |
| 170 | if (mapping->a_ops->readpages) { |
| 171 | ret = mapping->a_ops->readpages(filp, mapping, pages, nr_pages); |
OGAWA Hirofumi | 029e332 | 2006-11-02 22:07:06 -0800 | [diff] [blame] | 172 | /* Clean up the remaining pages */ |
| 173 | put_pages_list(pages); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | goto out; |
| 175 | } |
| 176 | |
| 177 | pagevec_init(&lru_pvec, 0); |
| 178 | for (page_idx = 0; page_idx < nr_pages; page_idx++) { |
| 179 | struct page *page = list_to_page(pages); |
| 180 | list_del(&page->lru); |
| 181 | if (!add_to_page_cache(page, mapping, |
| 182 | page->index, GFP_KERNEL)) { |
Zach Brown | 9f1a3cf | 2006-06-25 05:46:46 -0700 | [diff] [blame] | 183 | mapping->a_ops->readpage(filp, page); |
| 184 | if (!pagevec_add(&lru_pvec, page)) |
| 185 | __pagevec_lru_add(&lru_pvec); |
| 186 | } else |
| 187 | page_cache_release(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | } |
| 189 | pagevec_lru_add(&lru_pvec); |
Zach Brown | 994fc28c | 2005-12-15 14:28:17 -0800 | [diff] [blame] | 190 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | out: |
| 192 | return ret; |
| 193 | } |
| 194 | |
| 195 | /* |
| 196 | * Readahead design. |
| 197 | * |
| 198 | * The fields in struct file_ra_state represent the most-recently-executed |
| 199 | * readahead attempt: |
| 200 | * |
| 201 | * start: Page index at which we started the readahead |
| 202 | * size: Number of pages in that read |
| 203 | * Together, these form the "current window". |
| 204 | * Together, start and size represent the `readahead window'. |
Jan Kara | 6ce745e | 2007-05-06 14:49:26 -0700 | [diff] [blame] | 205 | * prev_index: The page which the readahead algorithm most-recently inspected. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | * It is mainly used to detect sequential file reading. |
| 207 | * If page_cache_readahead sees that it is again being called for |
| 208 | * a page which it just looked at, it can return immediately without |
| 209 | * making any state changes. |
Jan Kara | 6ce745e | 2007-05-06 14:49:26 -0700 | [diff] [blame] | 210 | * offset: Offset in the prev_index where the last read ended - used for |
Jan Kara | ec0f163 | 2007-05-06 14:49:25 -0700 | [diff] [blame] | 211 | * detection of sequential file reading. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | * ahead_start, |
| 213 | * ahead_size: Together, these form the "ahead window". |
| 214 | * ra_pages: The externally controlled max readahead for this fd. |
| 215 | * |
| 216 | * When readahead is in the off state (size == 0), readahead is disabled. |
Jan Kara | 6ce745e | 2007-05-06 14:49:26 -0700 | [diff] [blame] | 217 | * In this state, prev_index is used to detect the resumption of sequential I/O. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | * |
| 219 | * The readahead code manages two windows - the "current" and the "ahead" |
| 220 | * windows. The intent is that while the application is walking the pages |
| 221 | * in the current window, I/O is underway on the ahead window. When the |
| 222 | * current window is fully traversed, it is replaced by the ahead window |
| 223 | * and the ahead window is invalidated. When this copying happens, the |
| 224 | * new current window's pages are probably still locked. So |
| 225 | * we submit a new batch of I/O immediately, creating a new ahead window. |
| 226 | * |
| 227 | * So: |
| 228 | * |
| 229 | * ----|----------------|----------------|----- |
| 230 | * ^start ^start+size |
| 231 | * ^ahead_start ^ahead_start+ahead_size |
| 232 | * |
| 233 | * ^ When this page is read, we submit I/O for the |
| 234 | * ahead window. |
| 235 | * |
| 236 | * A `readahead hit' occurs when a read request is made against a page which is |
| 237 | * the next sequential page. Ahead window calculations are done only when it |
| 238 | * is time to submit a new IO. The code ramps up the size agressively at first, |
| 239 | * but slow down as it approaches max_readhead. |
| 240 | * |
| 241 | * Any seek/ramdom IO will result in readahead being turned off. It will resume |
| 242 | * at the first sequential access. |
| 243 | * |
| 244 | * There is a special-case: if the first page which the application tries to |
| 245 | * read happens to be the first page of the file, it is assumed that a linear |
| 246 | * read is about to happen and the window is immediately set to the initial size |
| 247 | * based on I/O request size and the max_readahead. |
| 248 | * |
| 249 | * This function is to be called for every read request, rather than when |
| 250 | * it is time to perform readahead. It is called only once for the entire I/O |
| 251 | * regardless of size unless readahead is unable to start enough I/O to satisfy |
| 252 | * the request (I/O request > max_readahead). |
| 253 | */ |
| 254 | |
| 255 | /* |
| 256 | * do_page_cache_readahead actually reads a chunk of disk. It allocates all |
| 257 | * the pages first, then submits them all for I/O. This avoids the very bad |
| 258 | * behaviour which would occur if page allocations are causing VM writeback. |
| 259 | * We really don't want to intermingle reads and writes like that. |
| 260 | * |
| 261 | * Returns the number of pages requested, or the maximum amount of I/O allowed. |
| 262 | * |
| 263 | * do_page_cache_readahead() returns -1 if it encountered request queue |
| 264 | * congestion. |
| 265 | */ |
| 266 | static int |
| 267 | __do_page_cache_readahead(struct address_space *mapping, struct file *filp, |
Fengguang Wu | 46fc3e7 | 2007-07-19 01:47:57 -0700 | [diff] [blame^] | 268 | pgoff_t offset, unsigned long nr_to_read, |
| 269 | unsigned long lookahead_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | { |
| 271 | struct inode *inode = mapping->host; |
| 272 | struct page *page; |
| 273 | unsigned long end_index; /* The last page we want to read */ |
| 274 | LIST_HEAD(page_pool); |
| 275 | int page_idx; |
| 276 | int ret = 0; |
| 277 | loff_t isize = i_size_read(inode); |
| 278 | |
| 279 | if (isize == 0) |
| 280 | goto out; |
| 281 | |
Fengguang Wu | 46fc3e7 | 2007-07-19 01:47:57 -0700 | [diff] [blame^] | 282 | end_index = ((isize - 1) >> PAGE_CACHE_SHIFT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | |
| 284 | /* |
| 285 | * Preallocate as many pages as we will need. |
| 286 | */ |
| 287 | read_lock_irq(&mapping->tree_lock); |
| 288 | for (page_idx = 0; page_idx < nr_to_read; page_idx++) { |
Andrew Morton | 7361f4d | 2005-11-07 00:59:28 -0800 | [diff] [blame] | 289 | pgoff_t page_offset = offset + page_idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | |
| 291 | if (page_offset > end_index) |
| 292 | break; |
| 293 | |
| 294 | page = radix_tree_lookup(&mapping->page_tree, page_offset); |
| 295 | if (page) |
| 296 | continue; |
| 297 | |
| 298 | read_unlock_irq(&mapping->tree_lock); |
| 299 | page = page_cache_alloc_cold(mapping); |
| 300 | read_lock_irq(&mapping->tree_lock); |
| 301 | if (!page) |
| 302 | break; |
| 303 | page->index = page_offset; |
| 304 | list_add(&page->lru, &page_pool); |
Fengguang Wu | 46fc3e7 | 2007-07-19 01:47:57 -0700 | [diff] [blame^] | 305 | if (page_idx == nr_to_read - lookahead_size) |
| 306 | SetPageReadahead(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | ret++; |
| 308 | } |
| 309 | read_unlock_irq(&mapping->tree_lock); |
| 310 | |
| 311 | /* |
| 312 | * Now start the IO. We ignore I/O errors - if the page is not |
| 313 | * uptodate then the caller will launch readpage again, and |
| 314 | * will then handle the error. |
| 315 | */ |
| 316 | if (ret) |
| 317 | read_pages(mapping, filp, &page_pool, ret); |
| 318 | BUG_ON(!list_empty(&page_pool)); |
| 319 | out: |
| 320 | return ret; |
| 321 | } |
| 322 | |
| 323 | /* |
| 324 | * Chunk the readahead into 2 megabyte units, so that we don't pin too much |
| 325 | * memory at once. |
| 326 | */ |
| 327 | int force_page_cache_readahead(struct address_space *mapping, struct file *filp, |
Andrew Morton | 7361f4d | 2005-11-07 00:59:28 -0800 | [diff] [blame] | 328 | pgoff_t offset, unsigned long nr_to_read) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | { |
| 330 | int ret = 0; |
| 331 | |
| 332 | if (unlikely(!mapping->a_ops->readpage && !mapping->a_ops->readpages)) |
| 333 | return -EINVAL; |
| 334 | |
| 335 | while (nr_to_read) { |
| 336 | int err; |
| 337 | |
| 338 | unsigned long this_chunk = (2 * 1024 * 1024) / PAGE_CACHE_SIZE; |
| 339 | |
| 340 | if (this_chunk > nr_to_read) |
| 341 | this_chunk = nr_to_read; |
| 342 | err = __do_page_cache_readahead(mapping, filp, |
Fengguang Wu | 46fc3e7 | 2007-07-19 01:47:57 -0700 | [diff] [blame^] | 343 | offset, this_chunk, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | if (err < 0) { |
| 345 | ret = err; |
| 346 | break; |
| 347 | } |
| 348 | ret += err; |
| 349 | offset += this_chunk; |
| 350 | nr_to_read -= this_chunk; |
| 351 | } |
| 352 | return ret; |
| 353 | } |
| 354 | |
| 355 | /* |
| 356 | * Check how effective readahead is being. If the amount of started IO is |
| 357 | * less than expected then the file is partly or fully in pagecache and |
| 358 | * readahead isn't helping. |
| 359 | * |
| 360 | */ |
| 361 | static inline int check_ra_success(struct file_ra_state *ra, |
| 362 | unsigned long nr_to_read, unsigned long actual) |
| 363 | { |
| 364 | if (actual == 0) { |
| 365 | ra->cache_hit += nr_to_read; |
| 366 | if (ra->cache_hit >= VM_MAX_CACHE_HIT) { |
| 367 | ra_off(ra); |
| 368 | ra->flags |= RA_FLAG_INCACHE; |
| 369 | return 0; |
| 370 | } |
| 371 | } else { |
| 372 | ra->cache_hit=0; |
| 373 | } |
| 374 | return 1; |
| 375 | } |
| 376 | |
| 377 | /* |
| 378 | * This version skips the IO if the queue is read-congested, and will tell the |
| 379 | * block layer to abandon the readahead if request allocation would block. |
| 380 | * |
| 381 | * force_page_cache_readahead() will ignore queue congestion and will block on |
| 382 | * request queues. |
| 383 | */ |
| 384 | int do_page_cache_readahead(struct address_space *mapping, struct file *filp, |
Andrew Morton | 7361f4d | 2005-11-07 00:59:28 -0800 | [diff] [blame] | 385 | pgoff_t offset, unsigned long nr_to_read) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | { |
| 387 | if (bdi_read_congested(mapping->backing_dev_info)) |
| 388 | return -1; |
| 389 | |
Fengguang Wu | 46fc3e7 | 2007-07-19 01:47:57 -0700 | [diff] [blame^] | 390 | return __do_page_cache_readahead(mapping, filp, offset, nr_to_read, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | /* |
| 394 | * Read 'nr_to_read' pages starting at page 'offset'. If the flag 'block' |
| 395 | * is set wait till the read completes. Otherwise attempt to read without |
| 396 | * blocking. |
Andreas Mohr | d6e05ed | 2006-06-26 18:35:02 +0200 | [diff] [blame] | 397 | * Returns 1 meaning 'success' if read is successful without switching off |
| 398 | * readahead mode. Otherwise return failure. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | */ |
| 400 | static int |
| 401 | blockable_page_cache_readahead(struct address_space *mapping, struct file *filp, |
Andrew Morton | 7361f4d | 2005-11-07 00:59:28 -0800 | [diff] [blame] | 402 | pgoff_t offset, unsigned long nr_to_read, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | struct file_ra_state *ra, int block) |
| 404 | { |
| 405 | int actual; |
| 406 | |
| 407 | if (!block && bdi_read_congested(mapping->backing_dev_info)) |
| 408 | return 0; |
| 409 | |
Fengguang Wu | 46fc3e7 | 2007-07-19 01:47:57 -0700 | [diff] [blame^] | 410 | actual = __do_page_cache_readahead(mapping, filp, offset, nr_to_read, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | |
| 412 | return check_ra_success(ra, nr_to_read, actual); |
| 413 | } |
| 414 | |
| 415 | static int make_ahead_window(struct address_space *mapping, struct file *filp, |
| 416 | struct file_ra_state *ra, int force) |
| 417 | { |
| 418 | int block, ret; |
| 419 | |
| 420 | ra->ahead_size = get_next_ra_size(ra); |
| 421 | ra->ahead_start = ra->start + ra->size; |
| 422 | |
Jan Kara | 6ce745e | 2007-05-06 14:49:26 -0700 | [diff] [blame] | 423 | block = force || (ra->prev_index >= ra->ahead_start); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | ret = blockable_page_cache_readahead(mapping, filp, |
| 425 | ra->ahead_start, ra->ahead_size, ra, block); |
| 426 | |
| 427 | if (!ret && !force) { |
| 428 | /* A read failure in blocking mode, implies pages are |
| 429 | * all cached. So we can safely assume we have taken |
| 430 | * care of all the pages requested in this call. |
| 431 | * A read failure in non-blocking mode, implies we are |
| 432 | * reading more pages than requested in this call. So |
| 433 | * we safely assume we have taken care of all the pages |
| 434 | * requested in this call. |
| 435 | * |
| 436 | * Just reset the ahead window in case we failed due to |
| 437 | * congestion. The ahead window will any way be closed |
| 438 | * in case we failed due to excessive page cache hits. |
| 439 | */ |
Oleg Nesterov | a564da3 | 2006-03-22 00:08:47 -0800 | [diff] [blame] | 440 | reset_ahead_window(ra); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | return ret; |
| 444 | } |
| 445 | |
Andrew Morton | 7361f4d | 2005-11-07 00:59:28 -0800 | [diff] [blame] | 446 | /** |
| 447 | * page_cache_readahead - generic adaptive readahead |
| 448 | * @mapping: address_space which holds the pagecache and I/O vectors |
| 449 | * @ra: file_ra_state which holds the readahead state |
| 450 | * @filp: passed on to ->readpage() and ->readpages() |
| 451 | * @offset: start offset into @mapping, in PAGE_CACHE_SIZE units |
| 452 | * @req_size: hint: total size of the read which the caller is performing in |
| 453 | * PAGE_CACHE_SIZE units |
| 454 | * |
Fengguang Wu | 46fc3e7 | 2007-07-19 01:47:57 -0700 | [diff] [blame^] | 455 | * page_cache_readahead() is the main function. It performs the adaptive |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | * readahead window size management and submits the readahead I/O. |
Andrew Morton | 7361f4d | 2005-11-07 00:59:28 -0800 | [diff] [blame] | 457 | * |
| 458 | * Note that @filp is purely used for passing on to the ->readpage[s]() |
| 459 | * handler: it may refer to a different file from @mapping (so we may not use |
Josef Sipek | e9536ae | 2006-12-08 02:37:21 -0800 | [diff] [blame] | 460 | * @filp->f_mapping or @filp->f_path.dentry->d_inode here). |
Andrew Morton | 7361f4d | 2005-11-07 00:59:28 -0800 | [diff] [blame] | 461 | * Also, @ra may not be equal to &@filp->f_ra. |
| 462 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | */ |
| 464 | unsigned long |
| 465 | page_cache_readahead(struct address_space *mapping, struct file_ra_state *ra, |
Andrew Morton | 7361f4d | 2005-11-07 00:59:28 -0800 | [diff] [blame] | 466 | struct file *filp, pgoff_t offset, unsigned long req_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | { |
| 468 | unsigned long max, newsize; |
| 469 | int sequential; |
| 470 | |
| 471 | /* |
| 472 | * We avoid doing extra work and bogusly perturbing the readahead |
| 473 | * window expansion logic. |
| 474 | */ |
Jan Kara | 6ce745e | 2007-05-06 14:49:26 -0700 | [diff] [blame] | 475 | if (offset == ra->prev_index && --req_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | ++offset; |
| 477 | |
Jan Kara | 6ce745e | 2007-05-06 14:49:26 -0700 | [diff] [blame] | 478 | /* Note that prev_index == -1 if it is a first read */ |
| 479 | sequential = (offset == ra->prev_index + 1); |
| 480 | ra->prev_index = offset; |
| 481 | ra->prev_offset = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | |
| 483 | max = get_max_readahead(ra); |
| 484 | newsize = min(req_size, max); |
| 485 | |
| 486 | /* No readahead or sub-page sized read or file already in cache */ |
| 487 | if (newsize == 0 || (ra->flags & RA_FLAG_INCACHE)) |
| 488 | goto out; |
| 489 | |
Jan Kara | 6ce745e | 2007-05-06 14:49:26 -0700 | [diff] [blame] | 490 | ra->prev_index += newsize - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | |
| 492 | /* |
| 493 | * Special case - first read at start of file. We'll assume it's |
| 494 | * a whole-file read and grow the window fast. Or detect first |
| 495 | * sequential access |
| 496 | */ |
| 497 | if (sequential && ra->size == 0) { |
| 498 | ra->size = get_init_ra_size(newsize, max); |
| 499 | ra->start = offset; |
| 500 | if (!blockable_page_cache_readahead(mapping, filp, offset, |
| 501 | ra->size, ra, 1)) |
| 502 | goto out; |
| 503 | |
| 504 | /* |
| 505 | * If the request size is larger than our max readahead, we |
| 506 | * at least want to be sure that we get 2 IOs in flight and |
| 507 | * we know that we will definitly need the new I/O. |
| 508 | * once we do this, subsequent calls should be able to overlap |
| 509 | * IOs,* thus preventing stalls. so issue the ahead window |
| 510 | * immediately. |
| 511 | */ |
| 512 | if (req_size >= max) |
| 513 | make_ahead_window(mapping, filp, ra, 1); |
| 514 | |
| 515 | goto out; |
| 516 | } |
| 517 | |
| 518 | /* |
| 519 | * Now handle the random case: |
| 520 | * partial page reads and first access were handled above, |
| 521 | * so this must be the next page otherwise it is random |
| 522 | */ |
| 523 | if (!sequential) { |
| 524 | ra_off(ra); |
| 525 | blockable_page_cache_readahead(mapping, filp, offset, |
| 526 | newsize, ra, 1); |
| 527 | goto out; |
| 528 | } |
| 529 | |
| 530 | /* |
| 531 | * If we get here we are doing sequential IO and this was not the first |
| 532 | * occurence (ie we have an existing window) |
| 533 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | if (ra->ahead_start == 0) { /* no ahead window yet */ |
| 535 | if (!make_ahead_window(mapping, filp, ra, 0)) |
Oleg Nesterov | a564da3 | 2006-03-22 00:08:47 -0800 | [diff] [blame] | 536 | goto recheck; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | } |
Oleg Nesterov | a564da3 | 2006-03-22 00:08:47 -0800 | [diff] [blame] | 538 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | /* |
| 540 | * Already have an ahead window, check if we crossed into it. |
| 541 | * If so, shift windows and issue a new ahead window. |
| 542 | * Only return the #pages that are in the current window, so that |
| 543 | * we get called back on the first page of the ahead window which |
| 544 | * will allow us to submit more IO. |
| 545 | */ |
Jan Kara | 6ce745e | 2007-05-06 14:49:26 -0700 | [diff] [blame] | 546 | if (ra->prev_index >= ra->ahead_start) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | ra->start = ra->ahead_start; |
| 548 | ra->size = ra->ahead_size; |
| 549 | make_ahead_window(mapping, filp, ra, 0); |
Oleg Nesterov | a564da3 | 2006-03-22 00:08:47 -0800 | [diff] [blame] | 550 | recheck: |
Jan Kara | 6ce745e | 2007-05-06 14:49:26 -0700 | [diff] [blame] | 551 | /* prev_index shouldn't overrun the ahead window */ |
| 552 | ra->prev_index = min(ra->prev_index, |
Oleg Nesterov | a564da3 | 2006-03-22 00:08:47 -0800 | [diff] [blame] | 553 | ra->ahead_start + ra->ahead_size - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | out: |
Jan Kara | 6ce745e | 2007-05-06 14:49:26 -0700 | [diff] [blame] | 557 | return ra->prev_index + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | } |
Andrew Morton | d8733c2 | 2006-03-23 03:00:11 -0800 | [diff] [blame] | 559 | EXPORT_SYMBOL_GPL(page_cache_readahead); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | |
| 561 | /* |
| 562 | * handle_ra_miss() is called when it is known that a page which should have |
| 563 | * been present in the pagecache (we just did some readahead there) was in fact |
| 564 | * not found. This will happen if it was evicted by the VM (readahead |
| 565 | * thrashing) |
| 566 | * |
| 567 | * Turn on the cache miss flag in the RA struct, this will cause the RA code |
| 568 | * to reduce the RA size on the next read. |
| 569 | */ |
| 570 | void handle_ra_miss(struct address_space *mapping, |
| 571 | struct file_ra_state *ra, pgoff_t offset) |
| 572 | { |
| 573 | ra->flags |= RA_FLAG_MISS; |
| 574 | ra->flags &= ~RA_FLAG_INCACHE; |
Steven Pratt | 3b30bbd | 2005-09-06 15:17:06 -0700 | [diff] [blame] | 575 | ra->cache_hit = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | /* |
| 579 | * Given a desired number of PAGE_CACHE_SIZE readahead pages, return a |
| 580 | * sensible upper limit. |
| 581 | */ |
| 582 | unsigned long max_sane_readahead(unsigned long nr) |
| 583 | { |
Christoph Lameter | 05a0416 | 2007-02-10 01:43:05 -0800 | [diff] [blame] | 584 | return min(nr, (node_page_state(numa_node_id(), NR_INACTIVE) |
| 585 | + node_page_state(numa_node_id(), NR_FREE_PAGES)) / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | } |