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