inotify: fix race
[linux-2.6.22.y-op.git] / mm / readahead.c
blob1448e53224b6c42b8beccd3e48628d9c05a1fbcf
1 /*
2 * mm/readahead.c - address_space-level file readahead.
4 * Copyright (C) 2002, Linus Torvalds
6 * 09Apr2002 akpm@zip.com.au
7 * Initial version.
8 */
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/task_io_accounting_ops.h>
17 #include <linux/pagevec.h>
19 void default_unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
22 EXPORT_SYMBOL(default_unplug_io_fn);
25 * Convienent macros for min/max read-ahead pages.
26 * Note that MAX_RA_PAGES is rounded down, while MIN_RA_PAGES is rounded up.
27 * The latter is necessary for systems with large page size(i.e. 64k).
29 #define MAX_RA_PAGES (VM_MAX_READAHEAD*1024 / PAGE_CACHE_SIZE)
30 #define MIN_RA_PAGES DIV_ROUND_UP(VM_MIN_READAHEAD*1024, PAGE_CACHE_SIZE)
32 struct backing_dev_info default_backing_dev_info = {
33 .ra_pages = MAX_RA_PAGES,
34 .state = 0,
35 .capabilities = BDI_CAP_MAP_COPY,
36 .unplug_io_fn = default_unplug_io_fn,
38 EXPORT_SYMBOL_GPL(default_backing_dev_info);
41 * Initialise a struct file's readahead state. Assumes that the caller has
42 * memset *ra to zero.
44 void
45 file_ra_state_init(struct file_ra_state *ra, struct address_space *mapping)
47 ra->ra_pages = mapping->backing_dev_info->ra_pages;
48 ra->prev_index = -1;
50 EXPORT_SYMBOL_GPL(file_ra_state_init);
53 * Return max readahead size for this inode in number-of-pages.
55 static inline unsigned long get_max_readahead(struct file_ra_state *ra)
57 return ra->ra_pages;
60 static inline unsigned long get_min_readahead(struct file_ra_state *ra)
62 return MIN_RA_PAGES;
65 static inline void reset_ahead_window(struct file_ra_state *ra)
68 * ... but preserve ahead_start + ahead_size value,
69 * see 'recheck:' label in page_cache_readahead().
70 * Note: We never use ->ahead_size as rvalue without
71 * checking ->ahead_start != 0 first.
73 ra->ahead_size += ra->ahead_start;
74 ra->ahead_start = 0;
77 static inline void ra_off(struct file_ra_state *ra)
79 ra->start = 0;
80 ra->flags = 0;
81 ra->size = 0;
82 reset_ahead_window(ra);
83 return;
87 * Set the initial window size, round to next power of 2 and square
88 * for small size, x 4 for medium, and x 2 for large
89 * for 128k (32 page) max ra
90 * 1-8 page = 32k initial, > 8 page = 128k initial
92 static unsigned long get_init_ra_size(unsigned long size, unsigned long max)
94 unsigned long newsize = roundup_pow_of_two(size);
96 if (newsize <= max / 32)
97 newsize = newsize * 4;
98 else if (newsize <= max / 4)
99 newsize = newsize * 2;
100 else
101 newsize = max;
102 return newsize;
106 * Set the new window size, this is called only when I/O is to be submitted,
107 * not for each call to readahead. If a cache miss occured, reduce next I/O
108 * size, else increase depending on how close to max we are.
110 static inline unsigned long get_next_ra_size(struct file_ra_state *ra)
112 unsigned long max = get_max_readahead(ra);
113 unsigned long min = get_min_readahead(ra);
114 unsigned long cur = ra->size;
115 unsigned long newsize;
117 if (ra->flags & RA_FLAG_MISS) {
118 ra->flags &= ~RA_FLAG_MISS;
119 newsize = max((cur - 2), min);
120 } else if (cur < max / 16) {
121 newsize = 4 * cur;
122 } else {
123 newsize = 2 * cur;
125 return min(newsize, max);
128 #define list_to_page(head) (list_entry((head)->prev, struct page, lru))
131 * read_cache_pages - populate an address space with some pages & start reads against them
132 * @mapping: the address_space
133 * @pages: The address of a list_head which contains the target pages. These
134 * pages have their ->index populated and are otherwise uninitialised.
135 * @filler: callback routine for filling a single page.
136 * @data: private data for the callback routine.
138 * Hides the details of the LRU cache etc from the filesystems.
140 int read_cache_pages(struct address_space *mapping, struct list_head *pages,
141 int (*filler)(void *, struct page *), void *data)
143 struct page *page;
144 struct pagevec lru_pvec;
145 int ret = 0;
147 pagevec_init(&lru_pvec, 0);
149 while (!list_empty(pages)) {
150 page = list_to_page(pages);
151 list_del(&page->lru);
152 if (add_to_page_cache(page, mapping, page->index, GFP_KERNEL)) {
153 page_cache_release(page);
154 continue;
156 ret = filler(data, page);
157 if (!pagevec_add(&lru_pvec, page))
158 __pagevec_lru_add(&lru_pvec);
159 if (ret) {
160 put_pages_list(pages);
161 break;
163 task_io_account_read(PAGE_CACHE_SIZE);
165 pagevec_lru_add(&lru_pvec);
166 return ret;
169 EXPORT_SYMBOL(read_cache_pages);
171 static int read_pages(struct address_space *mapping, struct file *filp,
172 struct list_head *pages, unsigned nr_pages)
174 unsigned page_idx;
175 struct pagevec lru_pvec;
176 int ret;
178 if (mapping->a_ops->readpages) {
179 ret = mapping->a_ops->readpages(filp, mapping, pages, nr_pages);
180 /* Clean up the remaining pages */
181 put_pages_list(pages);
182 goto out;
185 pagevec_init(&lru_pvec, 0);
186 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
187 struct page *page = list_to_page(pages);
188 list_del(&page->lru);
189 if (!add_to_page_cache(page, mapping,
190 page->index, GFP_KERNEL)) {
191 mapping->a_ops->readpage(filp, page);
192 if (!pagevec_add(&lru_pvec, page))
193 __pagevec_lru_add(&lru_pvec);
194 } else
195 page_cache_release(page);
197 pagevec_lru_add(&lru_pvec);
198 ret = 0;
199 out:
200 return ret;
204 * Readahead design.
206 * The fields in struct file_ra_state represent the most-recently-executed
207 * readahead attempt:
209 * start: Page index at which we started the readahead
210 * size: Number of pages in that read
211 * Together, these form the "current window".
212 * Together, start and size represent the `readahead window'.
213 * prev_index: The page which the readahead algorithm most-recently inspected.
214 * It is mainly used to detect sequential file reading.
215 * If page_cache_readahead sees that it is again being called for
216 * a page which it just looked at, it can return immediately without
217 * making any state changes.
218 * offset: Offset in the prev_index where the last read ended - used for
219 * detection of sequential file reading.
220 * ahead_start,
221 * ahead_size: Together, these form the "ahead window".
222 * ra_pages: The externally controlled max readahead for this fd.
224 * When readahead is in the off state (size == 0), readahead is disabled.
225 * In this state, prev_index is used to detect the resumption of sequential I/O.
227 * The readahead code manages two windows - the "current" and the "ahead"
228 * windows. The intent is that while the application is walking the pages
229 * in the current window, I/O is underway on the ahead window. When the
230 * current window is fully traversed, it is replaced by the ahead window
231 * and the ahead window is invalidated. When this copying happens, the
232 * new current window's pages are probably still locked. So
233 * we submit a new batch of I/O immediately, creating a new ahead window.
235 * So:
237 * ----|----------------|----------------|-----
238 * ^start ^start+size
239 * ^ahead_start ^ahead_start+ahead_size
241 * ^ When this page is read, we submit I/O for the
242 * ahead window.
244 * A `readahead hit' occurs when a read request is made against a page which is
245 * the next sequential page. Ahead window calculations are done only when it
246 * is time to submit a new IO. The code ramps up the size agressively at first,
247 * but slow down as it approaches max_readhead.
249 * Any seek/ramdom IO will result in readahead being turned off. It will resume
250 * at the first sequential access.
252 * There is a special-case: if the first page which the application tries to
253 * read happens to be the first page of the file, it is assumed that a linear
254 * read is about to happen and the window is immediately set to the initial size
255 * based on I/O request size and the max_readahead.
257 * This function is to be called for every read request, rather than when
258 * it is time to perform readahead. It is called only once for the entire I/O
259 * regardless of size unless readahead is unable to start enough I/O to satisfy
260 * the request (I/O request > max_readahead).
264 * do_page_cache_readahead actually reads a chunk of disk. It allocates all
265 * the pages first, then submits them all for I/O. This avoids the very bad
266 * behaviour which would occur if page allocations are causing VM writeback.
267 * We really don't want to intermingle reads and writes like that.
269 * Returns the number of pages requested, or the maximum amount of I/O allowed.
271 * do_page_cache_readahead() returns -1 if it encountered request queue
272 * congestion.
274 static int
275 __do_page_cache_readahead(struct address_space *mapping, struct file *filp,
276 pgoff_t offset, unsigned long nr_to_read)
278 struct inode *inode = mapping->host;
279 struct page *page;
280 unsigned long end_index; /* The last page we want to read */
281 LIST_HEAD(page_pool);
282 int page_idx;
283 int ret = 0;
284 loff_t isize = i_size_read(inode);
286 if (isize == 0)
287 goto out;
289 end_index = ((isize - 1) >> PAGE_CACHE_SHIFT);
292 * Preallocate as many pages as we will need.
294 read_lock_irq(&mapping->tree_lock);
295 for (page_idx = 0; page_idx < nr_to_read; page_idx++) {
296 pgoff_t page_offset = offset + page_idx;
298 if (page_offset > end_index)
299 break;
301 page = radix_tree_lookup(&mapping->page_tree, page_offset);
302 if (page)
303 continue;
305 read_unlock_irq(&mapping->tree_lock);
306 page = page_cache_alloc_cold(mapping);
307 read_lock_irq(&mapping->tree_lock);
308 if (!page)
309 break;
310 page->index = page_offset;
311 list_add(&page->lru, &page_pool);
312 ret++;
314 read_unlock_irq(&mapping->tree_lock);
317 * Now start the IO. We ignore I/O errors - if the page is not
318 * uptodate then the caller will launch readpage again, and
319 * will then handle the error.
321 if (ret)
322 read_pages(mapping, filp, &page_pool, ret);
323 BUG_ON(!list_empty(&page_pool));
324 out:
325 return ret;
329 * Chunk the readahead into 2 megabyte units, so that we don't pin too much
330 * memory at once.
332 int force_page_cache_readahead(struct address_space *mapping, struct file *filp,
333 pgoff_t offset, unsigned long nr_to_read)
335 int ret = 0;
337 if (unlikely(!mapping->a_ops->readpage && !mapping->a_ops->readpages))
338 return -EINVAL;
340 while (nr_to_read) {
341 int err;
343 unsigned long this_chunk = (2 * 1024 * 1024) / PAGE_CACHE_SIZE;
345 if (this_chunk > nr_to_read)
346 this_chunk = nr_to_read;
347 err = __do_page_cache_readahead(mapping, filp,
348 offset, this_chunk);
349 if (err < 0) {
350 ret = err;
351 break;
353 ret += err;
354 offset += this_chunk;
355 nr_to_read -= this_chunk;
357 return ret;
361 * Check how effective readahead is being. If the amount of started IO is
362 * less than expected then the file is partly or fully in pagecache and
363 * readahead isn't helping.
366 static inline int check_ra_success(struct file_ra_state *ra,
367 unsigned long nr_to_read, unsigned long actual)
369 if (actual == 0) {
370 ra->cache_hit += nr_to_read;
371 if (ra->cache_hit >= VM_MAX_CACHE_HIT) {
372 ra_off(ra);
373 ra->flags |= RA_FLAG_INCACHE;
374 return 0;
376 } else {
377 ra->cache_hit=0;
379 return 1;
383 * This version skips the IO if the queue is read-congested, and will tell the
384 * block layer to abandon the readahead if request allocation would block.
386 * force_page_cache_readahead() will ignore queue congestion and will block on
387 * request queues.
389 int do_page_cache_readahead(struct address_space *mapping, struct file *filp,
390 pgoff_t offset, unsigned long nr_to_read)
392 if (bdi_read_congested(mapping->backing_dev_info))
393 return -1;
395 return __do_page_cache_readahead(mapping, filp, offset, nr_to_read);
399 * Read 'nr_to_read' pages starting at page 'offset'. If the flag 'block'
400 * is set wait till the read completes. Otherwise attempt to read without
401 * blocking.
402 * Returns 1 meaning 'success' if read is successful without switching off
403 * readahead mode. Otherwise return failure.
405 static int
406 blockable_page_cache_readahead(struct address_space *mapping, struct file *filp,
407 pgoff_t offset, unsigned long nr_to_read,
408 struct file_ra_state *ra, int block)
410 int actual;
412 if (!block && bdi_read_congested(mapping->backing_dev_info))
413 return 0;
415 actual = __do_page_cache_readahead(mapping, filp, offset, nr_to_read);
417 return check_ra_success(ra, nr_to_read, actual);
420 static int make_ahead_window(struct address_space *mapping, struct file *filp,
421 struct file_ra_state *ra, int force)
423 int block, ret;
425 ra->ahead_size = get_next_ra_size(ra);
426 ra->ahead_start = ra->start + ra->size;
428 block = force || (ra->prev_index >= ra->ahead_start);
429 ret = blockable_page_cache_readahead(mapping, filp,
430 ra->ahead_start, ra->ahead_size, ra, block);
432 if (!ret && !force) {
433 /* A read failure in blocking mode, implies pages are
434 * all cached. So we can safely assume we have taken
435 * care of all the pages requested in this call.
436 * A read failure in non-blocking mode, implies we are
437 * reading more pages than requested in this call. So
438 * we safely assume we have taken care of all the pages
439 * requested in this call.
441 * Just reset the ahead window in case we failed due to
442 * congestion. The ahead window will any way be closed
443 * in case we failed due to excessive page cache hits.
445 reset_ahead_window(ra);
448 return ret;
452 * page_cache_readahead - generic adaptive readahead
453 * @mapping: address_space which holds the pagecache and I/O vectors
454 * @ra: file_ra_state which holds the readahead state
455 * @filp: passed on to ->readpage() and ->readpages()
456 * @offset: start offset into @mapping, in PAGE_CACHE_SIZE units
457 * @req_size: hint: total size of the read which the caller is performing in
458 * PAGE_CACHE_SIZE units
460 * page_cache_readahead() is the main function. If performs the adaptive
461 * readahead window size management and submits the readahead I/O.
463 * Note that @filp is purely used for passing on to the ->readpage[s]()
464 * handler: it may refer to a different file from @mapping (so we may not use
465 * @filp->f_mapping or @filp->f_path.dentry->d_inode here).
466 * Also, @ra may not be equal to &@filp->f_ra.
469 unsigned long
470 page_cache_readahead(struct address_space *mapping, struct file_ra_state *ra,
471 struct file *filp, pgoff_t offset, unsigned long req_size)
473 unsigned long max, newsize;
474 int sequential;
477 * We avoid doing extra work and bogusly perturbing the readahead
478 * window expansion logic.
480 if (offset == ra->prev_index && --req_size)
481 ++offset;
483 /* Note that prev_index == -1 if it is a first read */
484 sequential = (offset == ra->prev_index + 1);
485 ra->prev_index = offset;
486 ra->prev_offset = 0;
488 max = get_max_readahead(ra);
489 newsize = min(req_size, max);
491 /* No readahead or sub-page sized read or file already in cache */
492 if (newsize == 0 || (ra->flags & RA_FLAG_INCACHE))
493 goto out;
495 ra->prev_index += newsize - 1;
498 * Special case - first read at start of file. We'll assume it's
499 * a whole-file read and grow the window fast. Or detect first
500 * sequential access
502 if (sequential && ra->size == 0) {
503 ra->size = get_init_ra_size(newsize, max);
504 ra->start = offset;
505 if (!blockable_page_cache_readahead(mapping, filp, offset,
506 ra->size, ra, 1))
507 goto out;
510 * If the request size is larger than our max readahead, we
511 * at least want to be sure that we get 2 IOs in flight and
512 * we know that we will definitly need the new I/O.
513 * once we do this, subsequent calls should be able to overlap
514 * IOs,* thus preventing stalls. so issue the ahead window
515 * immediately.
517 if (req_size >= max)
518 make_ahead_window(mapping, filp, ra, 1);
520 goto out;
524 * Now handle the random case:
525 * partial page reads and first access were handled above,
526 * so this must be the next page otherwise it is random
528 if (!sequential) {
529 ra_off(ra);
530 blockable_page_cache_readahead(mapping, filp, offset,
531 newsize, ra, 1);
532 goto out;
536 * If we get here we are doing sequential IO and this was not the first
537 * occurence (ie we have an existing window)
539 if (ra->ahead_start == 0) { /* no ahead window yet */
540 if (!make_ahead_window(mapping, filp, ra, 0))
541 goto recheck;
545 * Already have an ahead window, check if we crossed into it.
546 * If so, shift windows and issue a new ahead window.
547 * Only return the #pages that are in the current window, so that
548 * we get called back on the first page of the ahead window which
549 * will allow us to submit more IO.
551 if (ra->prev_index >= ra->ahead_start) {
552 ra->start = ra->ahead_start;
553 ra->size = ra->ahead_size;
554 make_ahead_window(mapping, filp, ra, 0);
555 recheck:
556 /* prev_index shouldn't overrun the ahead window */
557 ra->prev_index = min(ra->prev_index,
558 ra->ahead_start + ra->ahead_size - 1);
561 out:
562 return ra->prev_index + 1;
564 EXPORT_SYMBOL_GPL(page_cache_readahead);
567 * handle_ra_miss() is called when it is known that a page which should have
568 * been present in the pagecache (we just did some readahead there) was in fact
569 * not found. This will happen if it was evicted by the VM (readahead
570 * thrashing)
572 * Turn on the cache miss flag in the RA struct, this will cause the RA code
573 * to reduce the RA size on the next read.
575 void handle_ra_miss(struct address_space *mapping,
576 struct file_ra_state *ra, pgoff_t offset)
578 ra->flags |= RA_FLAG_MISS;
579 ra->flags &= ~RA_FLAG_INCACHE;
580 ra->cache_hit = 0;
584 * Given a desired number of PAGE_CACHE_SIZE readahead pages, return a
585 * sensible upper limit.
587 unsigned long max_sane_readahead(unsigned long nr)
589 return min(nr, (node_page_state(numa_node_id(), NR_INACTIVE)
590 + node_page_state(numa_node_id(), NR_FREE_PAGES)) / 2);