ACPI: ibm-acpi: organize code
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / mm / page-writeback.c
blob17255ab1d380323a208b2fe8493bcc95e7bb6929
1 /*
2 * mm/page-writeback.c
4 * Copyright (C) 2002, Linus Torvalds.
6 * Contains functions related to writing back dirty pages at the
7 * address_space level.
9 * 10Apr2002 akpm@zip.com.au
10 * Initial version
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/spinlock.h>
16 #include <linux/fs.h>
17 #include <linux/mm.h>
18 #include <linux/swap.h>
19 #include <linux/slab.h>
20 #include <linux/pagemap.h>
21 #include <linux/writeback.h>
22 #include <linux/init.h>
23 #include <linux/backing-dev.h>
24 #include <linux/task_io_accounting_ops.h>
25 #include <linux/blkdev.h>
26 #include <linux/mpage.h>
27 #include <linux/rmap.h>
28 #include <linux/percpu.h>
29 #include <linux/notifier.h>
30 #include <linux/smp.h>
31 #include <linux/sysctl.h>
32 #include <linux/cpu.h>
33 #include <linux/syscalls.h>
34 #include <linux/buffer_head.h>
35 #include <linux/pagevec.h>
38 * The maximum number of pages to writeout in a single bdflush/kupdate
39 * operation. We do this so we don't hold I_LOCK against an inode for
40 * enormous amounts of time, which would block a userspace task which has
41 * been forced to throttle against that inode. Also, the code reevaluates
42 * the dirty each time it has written this many pages.
44 #define MAX_WRITEBACK_PAGES 1024
47 * After a CPU has dirtied this many pages, balance_dirty_pages_ratelimited
48 * will look to see if it needs to force writeback or throttling.
50 static long ratelimit_pages = 32;
52 static int dirty_exceeded __cacheline_aligned_in_smp; /* Dirty mem may be over limit */
55 * When balance_dirty_pages decides that the caller needs to perform some
56 * non-background writeback, this is how many pages it will attempt to write.
57 * It should be somewhat larger than RATELIMIT_PAGES to ensure that reasonably
58 * large amounts of I/O are submitted.
60 static inline long sync_writeback_pages(void)
62 return ratelimit_pages + ratelimit_pages / 2;
65 /* The following parameters are exported via /proc/sys/vm */
68 * Start background writeback (via pdflush) at this percentage
70 int dirty_background_ratio = 10;
73 * The generator of dirty data starts writeback at this percentage
75 int vm_dirty_ratio = 40;
78 * The interval between `kupdate'-style writebacks, in jiffies
80 int dirty_writeback_interval = 5 * HZ;
83 * The longest number of jiffies for which data is allowed to remain dirty
85 int dirty_expire_interval = 30 * HZ;
88 * Flag that makes the machine dump writes/reads and block dirtyings.
90 int block_dump;
93 * Flag that puts the machine in "laptop mode". Doubles as a timeout in jiffies:
94 * a full sync is triggered after this time elapses without any disk activity.
96 int laptop_mode;
98 EXPORT_SYMBOL(laptop_mode);
100 /* End of sysctl-exported parameters */
103 static void background_writeout(unsigned long _min_pages);
106 * Work out the current dirty-memory clamping and background writeout
107 * thresholds.
109 * The main aim here is to lower them aggressively if there is a lot of mapped
110 * memory around. To avoid stressing page reclaim with lots of unreclaimable
111 * pages. It is better to clamp down on writers than to start swapping, and
112 * performing lots of scanning.
114 * We only allow 1/2 of the currently-unmapped memory to be dirtied.
116 * We don't permit the clamping level to fall below 5% - that is getting rather
117 * excessive.
119 * We make sure that the background writeout level is below the adjusted
120 * clamping level.
122 static void
123 get_dirty_limits(long *pbackground, long *pdirty,
124 struct address_space *mapping)
126 int background_ratio; /* Percentages */
127 int dirty_ratio;
128 int unmapped_ratio;
129 long background;
130 long dirty;
131 unsigned long available_memory = vm_total_pages;
132 struct task_struct *tsk;
134 #ifdef CONFIG_HIGHMEM
136 * We always exclude high memory from our count.
138 available_memory -= totalhigh_pages;
139 #endif
142 unmapped_ratio = 100 - ((global_page_state(NR_FILE_MAPPED) +
143 global_page_state(NR_ANON_PAGES)) * 100) /
144 vm_total_pages;
146 dirty_ratio = vm_dirty_ratio;
147 if (dirty_ratio > unmapped_ratio / 2)
148 dirty_ratio = unmapped_ratio / 2;
150 if (dirty_ratio < 5)
151 dirty_ratio = 5;
153 background_ratio = dirty_background_ratio;
154 if (background_ratio >= dirty_ratio)
155 background_ratio = dirty_ratio / 2;
157 background = (background_ratio * available_memory) / 100;
158 dirty = (dirty_ratio * available_memory) / 100;
159 tsk = current;
160 if (tsk->flags & PF_LESS_THROTTLE || rt_task(tsk)) {
161 background += background / 4;
162 dirty += dirty / 4;
164 *pbackground = background;
165 *pdirty = dirty;
169 * balance_dirty_pages() must be called by processes which are generating dirty
170 * data. It looks at the number of dirty pages in the machine and will force
171 * the caller to perform writeback if the system is over `vm_dirty_ratio'.
172 * If we're over `background_thresh' then pdflush is woken to perform some
173 * writeout.
175 static void balance_dirty_pages(struct address_space *mapping)
177 long nr_reclaimable;
178 long background_thresh;
179 long dirty_thresh;
180 unsigned long pages_written = 0;
181 unsigned long write_chunk = sync_writeback_pages();
183 struct backing_dev_info *bdi = mapping->backing_dev_info;
185 for (;;) {
186 struct writeback_control wbc = {
187 .bdi = bdi,
188 .sync_mode = WB_SYNC_NONE,
189 .older_than_this = NULL,
190 .nr_to_write = write_chunk,
191 .range_cyclic = 1,
194 get_dirty_limits(&background_thresh, &dirty_thresh, mapping);
195 nr_reclaimable = global_page_state(NR_FILE_DIRTY) +
196 global_page_state(NR_UNSTABLE_NFS);
197 if (nr_reclaimable + global_page_state(NR_WRITEBACK) <=
198 dirty_thresh)
199 break;
201 if (!dirty_exceeded)
202 dirty_exceeded = 1;
204 /* Note: nr_reclaimable denotes nr_dirty + nr_unstable.
205 * Unstable writes are a feature of certain networked
206 * filesystems (i.e. NFS) in which data may have been
207 * written to the server's write cache, but has not yet
208 * been flushed to permanent storage.
210 if (nr_reclaimable) {
211 writeback_inodes(&wbc);
212 get_dirty_limits(&background_thresh,
213 &dirty_thresh, mapping);
214 nr_reclaimable = global_page_state(NR_FILE_DIRTY) +
215 global_page_state(NR_UNSTABLE_NFS);
216 if (nr_reclaimable +
217 global_page_state(NR_WRITEBACK)
218 <= dirty_thresh)
219 break;
220 pages_written += write_chunk - wbc.nr_to_write;
221 if (pages_written >= write_chunk)
222 break; /* We've done our duty */
224 congestion_wait(WRITE, HZ/10);
227 if (nr_reclaimable + global_page_state(NR_WRITEBACK)
228 <= dirty_thresh && dirty_exceeded)
229 dirty_exceeded = 0;
231 if (writeback_in_progress(bdi))
232 return; /* pdflush is already working this queue */
235 * In laptop mode, we wait until hitting the higher threshold before
236 * starting background writeout, and then write out all the way down
237 * to the lower threshold. So slow writers cause minimal disk activity.
239 * In normal mode, we start background writeout at the lower
240 * background_thresh, to keep the amount of dirty memory low.
242 if ((laptop_mode && pages_written) ||
243 (!laptop_mode && (nr_reclaimable > background_thresh)))
244 pdflush_operation(background_writeout, 0);
247 void set_page_dirty_balance(struct page *page)
249 if (set_page_dirty(page)) {
250 struct address_space *mapping = page_mapping(page);
252 if (mapping)
253 balance_dirty_pages_ratelimited(mapping);
258 * balance_dirty_pages_ratelimited_nr - balance dirty memory state
259 * @mapping: address_space which was dirtied
260 * @nr_pages_dirtied: number of pages which the caller has just dirtied
262 * Processes which are dirtying memory should call in here once for each page
263 * which was newly dirtied. The function will periodically check the system's
264 * dirty state and will initiate writeback if needed.
266 * On really big machines, get_writeback_state is expensive, so try to avoid
267 * calling it too often (ratelimiting). But once we're over the dirty memory
268 * limit we decrease the ratelimiting by a lot, to prevent individual processes
269 * from overshooting the limit by (ratelimit_pages) each.
271 void balance_dirty_pages_ratelimited_nr(struct address_space *mapping,
272 unsigned long nr_pages_dirtied)
274 static DEFINE_PER_CPU(unsigned long, ratelimits) = 0;
275 unsigned long ratelimit;
276 unsigned long *p;
278 ratelimit = ratelimit_pages;
279 if (dirty_exceeded)
280 ratelimit = 8;
283 * Check the rate limiting. Also, we do not want to throttle real-time
284 * tasks in balance_dirty_pages(). Period.
286 preempt_disable();
287 p = &__get_cpu_var(ratelimits);
288 *p += nr_pages_dirtied;
289 if (unlikely(*p >= ratelimit)) {
290 *p = 0;
291 preempt_enable();
292 balance_dirty_pages(mapping);
293 return;
295 preempt_enable();
297 EXPORT_SYMBOL(balance_dirty_pages_ratelimited_nr);
299 void throttle_vm_writeout(gfp_t gfp_mask)
301 long background_thresh;
302 long dirty_thresh;
304 if ((gfp_mask & (__GFP_FS|__GFP_IO)) != (__GFP_FS|__GFP_IO)) {
306 * The caller might hold locks which can prevent IO completion
307 * or progress in the filesystem. So we cannot just sit here
308 * waiting for IO to complete.
310 congestion_wait(WRITE, HZ/10);
311 return;
314 for ( ; ; ) {
315 get_dirty_limits(&background_thresh, &dirty_thresh, NULL);
318 * Boost the allowable dirty threshold a bit for page
319 * allocators so they don't get DoS'ed by heavy writers
321 dirty_thresh += dirty_thresh / 10; /* wheeee... */
323 if (global_page_state(NR_UNSTABLE_NFS) +
324 global_page_state(NR_WRITEBACK) <= dirty_thresh)
325 break;
326 congestion_wait(WRITE, HZ/10);
331 * writeback at least _min_pages, and keep writing until the amount of dirty
332 * memory is less than the background threshold, or until we're all clean.
334 static void background_writeout(unsigned long _min_pages)
336 long min_pages = _min_pages;
337 struct writeback_control wbc = {
338 .bdi = NULL,
339 .sync_mode = WB_SYNC_NONE,
340 .older_than_this = NULL,
341 .nr_to_write = 0,
342 .nonblocking = 1,
343 .range_cyclic = 1,
346 for ( ; ; ) {
347 long background_thresh;
348 long dirty_thresh;
350 get_dirty_limits(&background_thresh, &dirty_thresh, NULL);
351 if (global_page_state(NR_FILE_DIRTY) +
352 global_page_state(NR_UNSTABLE_NFS) < background_thresh
353 && min_pages <= 0)
354 break;
355 wbc.encountered_congestion = 0;
356 wbc.nr_to_write = MAX_WRITEBACK_PAGES;
357 wbc.pages_skipped = 0;
358 writeback_inodes(&wbc);
359 min_pages -= MAX_WRITEBACK_PAGES - wbc.nr_to_write;
360 if (wbc.nr_to_write > 0 || wbc.pages_skipped > 0) {
361 /* Wrote less than expected */
362 congestion_wait(WRITE, HZ/10);
363 if (!wbc.encountered_congestion)
364 break;
370 * Start writeback of `nr_pages' pages. If `nr_pages' is zero, write back
371 * the whole world. Returns 0 if a pdflush thread was dispatched. Returns
372 * -1 if all pdflush threads were busy.
374 int wakeup_pdflush(long nr_pages)
376 if (nr_pages == 0)
377 nr_pages = global_page_state(NR_FILE_DIRTY) +
378 global_page_state(NR_UNSTABLE_NFS);
379 return pdflush_operation(background_writeout, nr_pages);
382 static void wb_timer_fn(unsigned long unused);
383 static void laptop_timer_fn(unsigned long unused);
385 static DEFINE_TIMER(wb_timer, wb_timer_fn, 0, 0);
386 static DEFINE_TIMER(laptop_mode_wb_timer, laptop_timer_fn, 0, 0);
389 * Periodic writeback of "old" data.
391 * Define "old": the first time one of an inode's pages is dirtied, we mark the
392 * dirtying-time in the inode's address_space. So this periodic writeback code
393 * just walks the superblock inode list, writing back any inodes which are
394 * older than a specific point in time.
396 * Try to run once per dirty_writeback_interval. But if a writeback event
397 * takes longer than a dirty_writeback_interval interval, then leave a
398 * one-second gap.
400 * older_than_this takes precedence over nr_to_write. So we'll only write back
401 * all dirty pages if they are all attached to "old" mappings.
403 static void wb_kupdate(unsigned long arg)
405 unsigned long oldest_jif;
406 unsigned long start_jif;
407 unsigned long next_jif;
408 long nr_to_write;
409 struct writeback_control wbc = {
410 .bdi = NULL,
411 .sync_mode = WB_SYNC_NONE,
412 .older_than_this = &oldest_jif,
413 .nr_to_write = 0,
414 .nonblocking = 1,
415 .for_kupdate = 1,
416 .range_cyclic = 1,
419 sync_supers();
421 oldest_jif = jiffies - dirty_expire_interval;
422 start_jif = jiffies;
423 next_jif = start_jif + dirty_writeback_interval;
424 nr_to_write = global_page_state(NR_FILE_DIRTY) +
425 global_page_state(NR_UNSTABLE_NFS) +
426 (inodes_stat.nr_inodes - inodes_stat.nr_unused);
427 while (nr_to_write > 0) {
428 wbc.encountered_congestion = 0;
429 wbc.nr_to_write = MAX_WRITEBACK_PAGES;
430 writeback_inodes(&wbc);
431 if (wbc.nr_to_write > 0) {
432 if (wbc.encountered_congestion)
433 congestion_wait(WRITE, HZ/10);
434 else
435 break; /* All the old data is written */
437 nr_to_write -= MAX_WRITEBACK_PAGES - wbc.nr_to_write;
439 if (time_before(next_jif, jiffies + HZ))
440 next_jif = jiffies + HZ;
441 if (dirty_writeback_interval)
442 mod_timer(&wb_timer, next_jif);
446 * sysctl handler for /proc/sys/vm/dirty_writeback_centisecs
448 int dirty_writeback_centisecs_handler(ctl_table *table, int write,
449 struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
451 proc_dointvec_userhz_jiffies(table, write, file, buffer, length, ppos);
452 if (dirty_writeback_interval) {
453 mod_timer(&wb_timer,
454 jiffies + dirty_writeback_interval);
455 } else {
456 del_timer(&wb_timer);
458 return 0;
461 static void wb_timer_fn(unsigned long unused)
463 if (pdflush_operation(wb_kupdate, 0) < 0)
464 mod_timer(&wb_timer, jiffies + HZ); /* delay 1 second */
467 static void laptop_flush(unsigned long unused)
469 sys_sync();
472 static void laptop_timer_fn(unsigned long unused)
474 pdflush_operation(laptop_flush, 0);
478 * We've spun up the disk and we're in laptop mode: schedule writeback
479 * of all dirty data a few seconds from now. If the flush is already scheduled
480 * then push it back - the user is still using the disk.
482 void laptop_io_completion(void)
484 mod_timer(&laptop_mode_wb_timer, jiffies + laptop_mode);
488 * We're in laptop mode and we've just synced. The sync's writes will have
489 * caused another writeback to be scheduled by laptop_io_completion.
490 * Nothing needs to be written back anymore, so we unschedule the writeback.
492 void laptop_sync_completion(void)
494 del_timer(&laptop_mode_wb_timer);
498 * If ratelimit_pages is too high then we can get into dirty-data overload
499 * if a large number of processes all perform writes at the same time.
500 * If it is too low then SMP machines will call the (expensive)
501 * get_writeback_state too often.
503 * Here we set ratelimit_pages to a level which ensures that when all CPUs are
504 * dirtying in parallel, we cannot go more than 3% (1/32) over the dirty memory
505 * thresholds before writeback cuts in.
507 * But the limit should not be set too high. Because it also controls the
508 * amount of memory which the balance_dirty_pages() caller has to write back.
509 * If this is too large then the caller will block on the IO queue all the
510 * time. So limit it to four megabytes - the balance_dirty_pages() caller
511 * will write six megabyte chunks, max.
514 void writeback_set_ratelimit(void)
516 ratelimit_pages = vm_total_pages / (num_online_cpus() * 32);
517 if (ratelimit_pages < 16)
518 ratelimit_pages = 16;
519 if (ratelimit_pages * PAGE_CACHE_SIZE > 4096 * 1024)
520 ratelimit_pages = (4096 * 1024) / PAGE_CACHE_SIZE;
523 static int __cpuinit
524 ratelimit_handler(struct notifier_block *self, unsigned long u, void *v)
526 writeback_set_ratelimit();
527 return 0;
530 static struct notifier_block __cpuinitdata ratelimit_nb = {
531 .notifier_call = ratelimit_handler,
532 .next = NULL,
536 * Called early on to tune the page writeback dirty limits.
538 * We used to scale dirty pages according to how total memory
539 * related to pages that could be allocated for buffers (by
540 * comparing nr_free_buffer_pages() to vm_total_pages.
542 * However, that was when we used "dirty_ratio" to scale with
543 * all memory, and we don't do that any more. "dirty_ratio"
544 * is now applied to total non-HIGHPAGE memory (by subtracting
545 * totalhigh_pages from vm_total_pages), and as such we can't
546 * get into the old insane situation any more where we had
547 * large amounts of dirty pages compared to a small amount of
548 * non-HIGHMEM memory.
550 * But we might still want to scale the dirty_ratio by how
551 * much memory the box has..
553 void __init page_writeback_init(void)
555 mod_timer(&wb_timer, jiffies + dirty_writeback_interval);
556 writeback_set_ratelimit();
557 register_cpu_notifier(&ratelimit_nb);
561 * generic_writepages - walk the list of dirty pages of the given
562 * address space and writepage() all of them.
564 * @mapping: address space structure to write
565 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
567 * This is a library function, which implements the writepages()
568 * address_space_operation.
570 * If a page is already under I/O, generic_writepages() skips it, even
571 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
572 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
573 * and msync() need to guarantee that all the data which was dirty at the time
574 * the call was made get new I/O started against them. If wbc->sync_mode is
575 * WB_SYNC_ALL then we were called for data integrity and we must wait for
576 * existing IO to complete.
578 * Derived from mpage_writepages() - if you fix this you should check that
579 * also!
581 int generic_writepages(struct address_space *mapping,
582 struct writeback_control *wbc)
584 struct backing_dev_info *bdi = mapping->backing_dev_info;
585 int ret = 0;
586 int done = 0;
587 int (*writepage)(struct page *page, struct writeback_control *wbc);
588 struct pagevec pvec;
589 int nr_pages;
590 pgoff_t index;
591 pgoff_t end; /* Inclusive */
592 int scanned = 0;
593 int range_whole = 0;
595 if (wbc->nonblocking && bdi_write_congested(bdi)) {
596 wbc->encountered_congestion = 1;
597 return 0;
600 writepage = mapping->a_ops->writepage;
602 /* deal with chardevs and other special file */
603 if (!writepage)
604 return 0;
606 pagevec_init(&pvec, 0);
607 if (wbc->range_cyclic) {
608 index = mapping->writeback_index; /* Start from prev offset */
609 end = -1;
610 } else {
611 index = wbc->range_start >> PAGE_CACHE_SHIFT;
612 end = wbc->range_end >> PAGE_CACHE_SHIFT;
613 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
614 range_whole = 1;
615 scanned = 1;
617 retry:
618 while (!done && (index <= end) &&
619 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
620 PAGECACHE_TAG_DIRTY,
621 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
622 unsigned i;
624 scanned = 1;
625 for (i = 0; i < nr_pages; i++) {
626 struct page *page = pvec.pages[i];
629 * At this point we hold neither mapping->tree_lock nor
630 * lock on the page itself: the page may be truncated or
631 * invalidated (changing page->mapping to NULL), or even
632 * swizzled back from swapper_space to tmpfs file
633 * mapping
635 lock_page(page);
637 if (unlikely(page->mapping != mapping)) {
638 unlock_page(page);
639 continue;
642 if (!wbc->range_cyclic && page->index > end) {
643 done = 1;
644 unlock_page(page);
645 continue;
648 if (wbc->sync_mode != WB_SYNC_NONE)
649 wait_on_page_writeback(page);
651 if (PageWriteback(page) ||
652 !clear_page_dirty_for_io(page)) {
653 unlock_page(page);
654 continue;
657 ret = (*writepage)(page, wbc);
658 if (ret) {
659 if (ret == -ENOSPC)
660 set_bit(AS_ENOSPC, &mapping->flags);
661 else
662 set_bit(AS_EIO, &mapping->flags);
665 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE))
666 unlock_page(page);
667 if (ret || (--(wbc->nr_to_write) <= 0))
668 done = 1;
669 if (wbc->nonblocking && bdi_write_congested(bdi)) {
670 wbc->encountered_congestion = 1;
671 done = 1;
674 pagevec_release(&pvec);
675 cond_resched();
677 if (!scanned && !done) {
679 * We hit the last page and there is more work to be done: wrap
680 * back to the start of the file
682 scanned = 1;
683 index = 0;
684 goto retry;
686 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
687 mapping->writeback_index = index;
688 return ret;
691 EXPORT_SYMBOL(generic_writepages);
693 int do_writepages(struct address_space *mapping, struct writeback_control *wbc)
695 int ret;
697 if (wbc->nr_to_write <= 0)
698 return 0;
699 wbc->for_writepages = 1;
700 if (mapping->a_ops->writepages)
701 ret = mapping->a_ops->writepages(mapping, wbc);
702 else
703 ret = generic_writepages(mapping, wbc);
704 wbc->for_writepages = 0;
705 return ret;
709 * write_one_page - write out a single page and optionally wait on I/O
711 * @page: the page to write
712 * @wait: if true, wait on writeout
714 * The page must be locked by the caller and will be unlocked upon return.
716 * write_one_page() returns a negative error code if I/O failed.
718 int write_one_page(struct page *page, int wait)
720 struct address_space *mapping = page->mapping;
721 int ret = 0;
722 struct writeback_control wbc = {
723 .sync_mode = WB_SYNC_ALL,
724 .nr_to_write = 1,
727 BUG_ON(!PageLocked(page));
729 if (wait)
730 wait_on_page_writeback(page);
732 if (clear_page_dirty_for_io(page)) {
733 page_cache_get(page);
734 ret = mapping->a_ops->writepage(page, &wbc);
735 if (ret == 0 && wait) {
736 wait_on_page_writeback(page);
737 if (PageError(page))
738 ret = -EIO;
740 page_cache_release(page);
741 } else {
742 unlock_page(page);
744 return ret;
746 EXPORT_SYMBOL(write_one_page);
749 * For address_spaces which do not use buffers. Just tag the page as dirty in
750 * its radix tree.
752 * This is also used when a single buffer is being dirtied: we want to set the
753 * page dirty in that case, but not all the buffers. This is a "bottom-up"
754 * dirtying, whereas __set_page_dirty_buffers() is a "top-down" dirtying.
756 * Most callers have locked the page, which pins the address_space in memory.
757 * But zap_pte_range() does not lock the page, however in that case the
758 * mapping is pinned by the vma's ->vm_file reference.
760 * We take care to handle the case where the page was truncated from the
761 * mapping by re-checking page_mapping() insode tree_lock.
763 int __set_page_dirty_nobuffers(struct page *page)
765 if (!TestSetPageDirty(page)) {
766 struct address_space *mapping = page_mapping(page);
767 struct address_space *mapping2;
769 if (!mapping)
770 return 1;
772 write_lock_irq(&mapping->tree_lock);
773 mapping2 = page_mapping(page);
774 if (mapping2) { /* Race with truncate? */
775 BUG_ON(mapping2 != mapping);
776 if (mapping_cap_account_dirty(mapping)) {
777 __inc_zone_page_state(page, NR_FILE_DIRTY);
778 task_io_account_write(PAGE_CACHE_SIZE);
780 radix_tree_tag_set(&mapping->page_tree,
781 page_index(page), PAGECACHE_TAG_DIRTY);
783 write_unlock_irq(&mapping->tree_lock);
784 if (mapping->host) {
785 /* !PageAnon && !swapper_space */
786 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
788 return 1;
790 return 0;
792 EXPORT_SYMBOL(__set_page_dirty_nobuffers);
795 * When a writepage implementation decides that it doesn't want to write this
796 * page for some reason, it should redirty the locked page via
797 * redirty_page_for_writepage() and it should then unlock the page and return 0
799 int redirty_page_for_writepage(struct writeback_control *wbc, struct page *page)
801 wbc->pages_skipped++;
802 return __set_page_dirty_nobuffers(page);
804 EXPORT_SYMBOL(redirty_page_for_writepage);
807 * If the mapping doesn't provide a set_page_dirty a_op, then
808 * just fall through and assume that it wants buffer_heads.
810 int fastcall set_page_dirty(struct page *page)
812 struct address_space *mapping = page_mapping(page);
814 if (likely(mapping)) {
815 int (*spd)(struct page *) = mapping->a_ops->set_page_dirty;
816 #ifdef CONFIG_BLOCK
817 if (!spd)
818 spd = __set_page_dirty_buffers;
819 #endif
820 return (*spd)(page);
822 if (!PageDirty(page)) {
823 if (!TestSetPageDirty(page))
824 return 1;
826 return 0;
828 EXPORT_SYMBOL(set_page_dirty);
831 * set_page_dirty() is racy if the caller has no reference against
832 * page->mapping->host, and if the page is unlocked. This is because another
833 * CPU could truncate the page off the mapping and then free the mapping.
835 * Usually, the page _is_ locked, or the caller is a user-space process which
836 * holds a reference on the inode by having an open file.
838 * In other cases, the page should be locked before running set_page_dirty().
840 int set_page_dirty_lock(struct page *page)
842 int ret;
844 lock_page_nosync(page);
845 ret = set_page_dirty(page);
846 unlock_page(page);
847 return ret;
849 EXPORT_SYMBOL(set_page_dirty_lock);
852 * Clear a page's dirty flag, while caring for dirty memory accounting.
853 * Returns true if the page was previously dirty.
855 * This is for preparing to put the page under writeout. We leave the page
856 * tagged as dirty in the radix tree so that a concurrent write-for-sync
857 * can discover it via a PAGECACHE_TAG_DIRTY walk. The ->writepage
858 * implementation will run either set_page_writeback() or set_page_dirty(),
859 * at which stage we bring the page's dirty flag and radix-tree dirty tag
860 * back into sync.
862 * This incoherency between the page's dirty flag and radix-tree tag is
863 * unfortunate, but it only exists while the page is locked.
865 int clear_page_dirty_for_io(struct page *page)
867 struct address_space *mapping = page_mapping(page);
869 if (mapping && mapping_cap_account_dirty(mapping)) {
871 * Yes, Virginia, this is indeed insane.
873 * We use this sequence to make sure that
874 * (a) we account for dirty stats properly
875 * (b) we tell the low-level filesystem to
876 * mark the whole page dirty if it was
877 * dirty in a pagetable. Only to then
878 * (c) clean the page again and return 1 to
879 * cause the writeback.
881 * This way we avoid all nasty races with the
882 * dirty bit in multiple places and clearing
883 * them concurrently from different threads.
885 * Note! Normally the "set_page_dirty(page)"
886 * has no effect on the actual dirty bit - since
887 * that will already usually be set. But we
888 * need the side effects, and it can help us
889 * avoid races.
891 * We basically use the page "master dirty bit"
892 * as a serialization point for all the different
893 * threads doing their things.
895 * FIXME! We still have a race here: if somebody
896 * adds the page back to the page tables in
897 * between the "page_mkclean()" and the "TestClearPageDirty()",
898 * we might have it mapped without the dirty bit set.
900 if (page_mkclean(page))
901 set_page_dirty(page);
902 if (TestClearPageDirty(page)) {
903 dec_zone_page_state(page, NR_FILE_DIRTY);
904 return 1;
906 return 0;
908 return TestClearPageDirty(page);
910 EXPORT_SYMBOL(clear_page_dirty_for_io);
912 int test_clear_page_writeback(struct page *page)
914 struct address_space *mapping = page_mapping(page);
915 int ret;
917 if (mapping) {
918 unsigned long flags;
920 write_lock_irqsave(&mapping->tree_lock, flags);
921 ret = TestClearPageWriteback(page);
922 if (ret)
923 radix_tree_tag_clear(&mapping->page_tree,
924 page_index(page),
925 PAGECACHE_TAG_WRITEBACK);
926 write_unlock_irqrestore(&mapping->tree_lock, flags);
927 } else {
928 ret = TestClearPageWriteback(page);
930 return ret;
933 int test_set_page_writeback(struct page *page)
935 struct address_space *mapping = page_mapping(page);
936 int ret;
938 if (mapping) {
939 unsigned long flags;
941 write_lock_irqsave(&mapping->tree_lock, flags);
942 ret = TestSetPageWriteback(page);
943 if (!ret)
944 radix_tree_tag_set(&mapping->page_tree,
945 page_index(page),
946 PAGECACHE_TAG_WRITEBACK);
947 if (!PageDirty(page))
948 radix_tree_tag_clear(&mapping->page_tree,
949 page_index(page),
950 PAGECACHE_TAG_DIRTY);
951 write_unlock_irqrestore(&mapping->tree_lock, flags);
952 } else {
953 ret = TestSetPageWriteback(page);
955 return ret;
958 EXPORT_SYMBOL(test_set_page_writeback);
961 * Return true if any of the pages in the mapping are marged with the
962 * passed tag.
964 int mapping_tagged(struct address_space *mapping, int tag)
966 unsigned long flags;
967 int ret;
969 read_lock_irqsave(&mapping->tree_lock, flags);
970 ret = radix_tree_tagged(&mapping->page_tree, tag);
971 read_unlock_irqrestore(&mapping->tree_lock, flags);
972 return ret;
974 EXPORT_SYMBOL(mapping_tagged);