This commit was manufactured by cvs2svn to create tag
[linux-2.6.git] / mm / page-writeback.c
1 /*
2  * mm/page-writeback.c.
3  *
4  * Copyright (C) 2002, Linus Torvalds.
5  *
6  * Contains functions related to writing back dirty pages at the
7  * address_space level.
8  *
9  * 10Apr2002    akpm@zip.com.au
10  *              Initial version
11  */
12
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/blkdev.h>
25 #include <linux/mpage.h>
26 #include <linux/percpu.h>
27 #include <linux/notifier.h>
28 #include <linux/smp.h>
29 #include <linux/sysctl.h>
30 #include <linux/cpu.h>
31 #include <linux/syscalls.h>
32
33 /*
34  * The maximum number of pages to writeout in a single bdflush/kupdate
35  * operation.  We do this so we don't hold I_LOCK against an inode for
36  * enormous amounts of time, which would block a userspace task which has
37  * been forced to throttle against that inode.  Also, the code reevaluates
38  * the dirty each time it has written this many pages.
39  */
40 #define MAX_WRITEBACK_PAGES     1024
41
42 /*
43  * After a CPU has dirtied this many pages, balance_dirty_pages_ratelimited
44  * will look to see if it needs to force writeback or throttling.
45  */
46 static long ratelimit_pages = 32;
47
48 static long total_pages;        /* The total number of pages in the machine. */
49 static int dirty_exceeded;      /* Dirty mem may be over limit */
50
51 /*
52  * When balance_dirty_pages decides that the caller needs to perform some
53  * non-background writeback, this is how many pages it will attempt to write.
54  * It should be somewhat larger than RATELIMIT_PAGES to ensure that reasonably
55  * large amounts of I/O are submitted.
56  */
57 static inline long sync_writeback_pages(void)
58 {
59         return ratelimit_pages + ratelimit_pages / 2;
60 }
61
62 /* The following parameters are exported via /proc/sys/vm */
63
64 /*
65  * Start background writeback (via pdflush) at this percentage
66  */
67 int dirty_background_ratio = 10;
68
69 /*
70  * The generator of dirty data starts writeback at this percentage
71  */
72 int vm_dirty_ratio = 40;
73
74 /*
75  * The interval between `kupdate'-style writebacks, in centiseconds
76  * (hundredths of a second)
77  */
78 int dirty_writeback_centisecs = 5 * 100;
79
80 /*
81  * The longest number of centiseconds for which data is allowed to remain dirty
82  */
83 int dirty_expire_centisecs = 30 * 100;
84
85 /*
86  * Flag that makes the machine dump writes/reads and block dirtyings.
87  */
88 int block_dump;
89
90 /*
91  * Flag that puts the machine in "laptop mode".
92  */
93 int laptop_mode;
94
95 EXPORT_SYMBOL(laptop_mode);
96
97 /* End of sysctl-exported parameters */
98
99
100 static void background_writeout(unsigned long _min_pages);
101
102 struct writeback_state
103 {
104         unsigned long nr_dirty;
105         unsigned long nr_unstable;
106         unsigned long nr_mapped;
107         unsigned long nr_writeback;
108 };
109
110 static void get_writeback_state(struct writeback_state *wbs)
111 {
112         wbs->nr_dirty = read_page_state(nr_dirty);
113         wbs->nr_unstable = read_page_state(nr_unstable);
114         wbs->nr_mapped = read_page_state(nr_mapped);
115         wbs->nr_writeback = read_page_state(nr_writeback);
116 }
117
118 /*
119  * Work out the current dirty-memory clamping and background writeout
120  * thresholds.
121  *
122  * The main aim here is to lower them aggressively if there is a lot of mapped
123  * memory around.  To avoid stressing page reclaim with lots of unreclaimable
124  * pages.  It is better to clamp down on writers than to start swapping, and
125  * performing lots of scanning.
126  *
127  * We only allow 1/2 of the currently-unmapped memory to be dirtied.
128  *
129  * We don't permit the clamping level to fall below 5% - that is getting rather
130  * excessive.
131  *
132  * We make sure that the background writeout level is below the adjusted
133  * clamping level.
134  */
135 static void
136 get_dirty_limits(struct writeback_state *wbs, long *pbackground, long *pdirty)
137 {
138         int background_ratio;           /* Percentages */
139         int dirty_ratio;
140         int unmapped_ratio;
141         long background;
142         long dirty;
143         struct task_struct *tsk;
144
145         get_writeback_state(wbs);
146
147         unmapped_ratio = 100 - (wbs->nr_mapped * 100) / total_pages;
148
149         dirty_ratio = vm_dirty_ratio;
150         if (dirty_ratio > unmapped_ratio / 2)
151                 dirty_ratio = unmapped_ratio / 2;
152
153         if (dirty_ratio < 5)
154                 dirty_ratio = 5;
155
156         background_ratio = dirty_background_ratio;
157         if (background_ratio >= dirty_ratio)
158                 background_ratio = dirty_ratio / 2;
159
160         background = (background_ratio * total_pages) / 100;
161         dirty = (dirty_ratio * total_pages) / 100;
162         tsk = current;
163         if (tsk->flags & PF_LESS_THROTTLE || rt_task(tsk)) {
164                 background += background / 4;
165                 dirty += dirty / 4;
166         }
167         *pbackground = background;
168         *pdirty = dirty;
169 }
170
171 /*
172  * balance_dirty_pages() must be called by processes which are generating dirty
173  * data.  It looks at the number of dirty pages in the machine and will force
174  * the caller to perform writeback if the system is over `vm_dirty_ratio'.
175  * If we're over `background_thresh' then pdflush is woken to perform some
176  * writeout.
177  */
178 static void balance_dirty_pages(struct address_space *mapping)
179 {
180         struct writeback_state wbs;
181         long nr_reclaimable;
182         long background_thresh;
183         long dirty_thresh;
184         unsigned long pages_written = 0;
185         unsigned long write_chunk = sync_writeback_pages();
186
187         struct backing_dev_info *bdi = mapping->backing_dev_info;
188
189         for (;;) {
190                 struct writeback_control wbc = {
191                         .bdi            = bdi,
192                         .sync_mode      = WB_SYNC_NONE,
193                         .older_than_this = NULL,
194                         .nr_to_write    = write_chunk,
195                 };
196
197                 get_dirty_limits(&wbs, &background_thresh, &dirty_thresh);
198                 nr_reclaimable = wbs.nr_dirty + wbs.nr_unstable;
199                 if (nr_reclaimable + wbs.nr_writeback <= dirty_thresh)
200                         break;
201
202                 dirty_exceeded = 1;
203
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.
209                  */
210                 if (nr_reclaimable) {
211                         writeback_inodes(&wbc);
212                         get_dirty_limits(&wbs, &background_thresh,
213                                         &dirty_thresh);
214                         nr_reclaimable = wbs.nr_dirty + wbs.nr_unstable;
215                         if (nr_reclaimable + wbs.nr_writeback <= dirty_thresh)
216                                 break;
217                         pages_written += write_chunk - wbc.nr_to_write;
218                         if (pages_written >= write_chunk)
219                                 break;          /* We've done our duty */
220                 }
221                 blk_congestion_wait(WRITE, HZ/10);
222         }
223
224         if (nr_reclaimable + wbs.nr_writeback <= dirty_thresh)
225                 dirty_exceeded = 0;
226
227         if (writeback_in_progress(bdi))
228                 return;         /* pdflush is already working this queue */
229
230         /*
231          * In laptop mode, we wait until hitting the higher threshold before
232          * starting background writeout, and then write out all the way down
233          * to the lower threshold.  So slow writers cause minimal disk activity.
234          *
235          * In normal mode, we start background writeout at the lower
236          * background_thresh, to keep the amount of dirty memory low.
237          */
238         if ((laptop_mode && pages_written) ||
239              (!laptop_mode && (nr_reclaimable > background_thresh)))
240                 pdflush_operation(background_writeout, 0);
241 }
242
243 /**
244  * balance_dirty_pages_ratelimited - balance dirty memory state
245  * @mapping - address_space which was dirtied
246  *
247  * Processes which are dirtying memory should call in here once for each page
248  * which was newly dirtied.  The function will periodically check the system's
249  * dirty state and will initiate writeback if needed.
250  *
251  * On really big machines, get_writeback_state is expensive, so try to avoid
252  * calling it too often (ratelimiting).  But once we're over the dirty memory
253  * limit we decrease the ratelimiting by a lot, to prevent individual processes
254  * from overshooting the limit by (ratelimit_pages) each.
255  */
256 void balance_dirty_pages_ratelimited(struct address_space *mapping)
257 {
258         static DEFINE_PER_CPU(int, ratelimits) = 0;
259         long ratelimit;
260
261         ratelimit = ratelimit_pages;
262         if (dirty_exceeded)
263                 ratelimit = 8;
264
265         /*
266          * Check the rate limiting. Also, we do not want to throttle real-time
267          * tasks in balance_dirty_pages(). Period.
268          */
269         if (get_cpu_var(ratelimits)++ >= ratelimit) {
270                 __get_cpu_var(ratelimits) = 0;
271                 put_cpu_var(ratelimits);
272                 balance_dirty_pages(mapping);
273                 return;
274         }
275         put_cpu_var(ratelimits);
276 }
277 EXPORT_SYMBOL(balance_dirty_pages_ratelimited);
278
279 void throttle_vm_writeout(void)
280 {
281         struct writeback_state wbs;
282         long background_thresh;
283         long dirty_thresh;
284
285         for ( ; ; ) {
286                 get_dirty_limits(&wbs, &background_thresh, &dirty_thresh);
287
288                 /*
289                  * Boost the allowable dirty threshold a bit for page
290                  * allocators so they don't get DoS'ed by heavy writers
291                  */
292                 dirty_thresh += dirty_thresh / 10;      /* wheeee... */
293
294                 if (wbs.nr_unstable + wbs.nr_writeback <= dirty_thresh)
295                         break;
296                 blk_congestion_wait(WRITE, HZ/10);
297         }
298 }
299
300
301 /*
302  * writeback at least _min_pages, and keep writing until the amount of dirty
303  * memory is less than the background threshold, or until we're all clean.
304  */
305 static void background_writeout(unsigned long _min_pages)
306 {
307         long min_pages = _min_pages;
308         struct writeback_control wbc = {
309                 .bdi            = NULL,
310                 .sync_mode      = WB_SYNC_NONE,
311                 .older_than_this = NULL,
312                 .nr_to_write    = 0,
313                 .nonblocking    = 1,
314         };
315
316         for ( ; ; ) {
317                 struct writeback_state wbs;
318                 long background_thresh;
319                 long dirty_thresh;
320
321                 get_dirty_limits(&wbs, &background_thresh, &dirty_thresh);
322                 if (wbs.nr_dirty + wbs.nr_unstable < background_thresh
323                                 && min_pages <= 0)
324                         break;
325                 wbc.encountered_congestion = 0;
326                 wbc.nr_to_write = MAX_WRITEBACK_PAGES;
327                 wbc.pages_skipped = 0;
328                 writeback_inodes(&wbc);
329                 min_pages -= MAX_WRITEBACK_PAGES - wbc.nr_to_write;
330                 if (wbc.nr_to_write > 0 || wbc.pages_skipped > 0) {
331                         /* Wrote less than expected */
332                         blk_congestion_wait(WRITE, HZ/10);
333                         if (!wbc.encountered_congestion)
334                                 break;
335                 }
336         }
337 }
338
339 /*
340  * Start writeback of `nr_pages' pages.  If `nr_pages' is zero, write back
341  * the whole world.  Returns 0 if a pdflush thread was dispatched.  Returns
342  * -1 if all pdflush threads were busy.
343  */
344 int wakeup_bdflush(long nr_pages)
345 {
346         if (nr_pages == 0) {
347                 struct writeback_state wbs;
348
349                 get_writeback_state(&wbs);
350                 nr_pages = wbs.nr_dirty + wbs.nr_unstable;
351         }
352         return pdflush_operation(background_writeout, nr_pages);
353 }
354
355 static void wb_timer_fn(unsigned long unused);
356 static void laptop_timer_fn(unsigned long unused);
357
358 static struct timer_list wb_timer =
359                         TIMER_INITIALIZER(wb_timer_fn, 0, 0);
360 static struct timer_list laptop_mode_wb_timer =
361                         TIMER_INITIALIZER(laptop_timer_fn, 0, 0);
362
363 /*
364  * Periodic writeback of "old" data.
365  *
366  * Define "old": the first time one of an inode's pages is dirtied, we mark the
367  * dirtying-time in the inode's address_space.  So this periodic writeback code
368  * just walks the superblock inode list, writing back any inodes which are
369  * older than a specific point in time.
370  *
371  * Try to run once per dirty_writeback_centisecs.  But if a writeback event
372  * takes longer than a dirty_writeback_centisecs interval, then leave a
373  * one-second gap.
374  *
375  * older_than_this takes precedence over nr_to_write.  So we'll only write back
376  * all dirty pages if they are all attached to "old" mappings.
377  */
378 static void wb_kupdate(unsigned long arg)
379 {
380         unsigned long oldest_jif;
381         unsigned long start_jif;
382         unsigned long next_jif;
383         long nr_to_write;
384         struct writeback_state wbs;
385         struct writeback_control wbc = {
386                 .bdi            = NULL,
387                 .sync_mode      = WB_SYNC_NONE,
388                 .older_than_this = &oldest_jif,
389                 .nr_to_write    = 0,
390                 .nonblocking    = 1,
391                 .for_kupdate    = 1,
392         };
393
394         sync_supers();
395
396         get_writeback_state(&wbs);
397         oldest_jif = jiffies - (dirty_expire_centisecs * HZ) / 100;
398         start_jif = jiffies;
399         next_jif = start_jif + (dirty_writeback_centisecs * HZ) / 100;
400         nr_to_write = wbs.nr_dirty + wbs.nr_unstable +
401                         (inodes_stat.nr_inodes - inodes_stat.nr_unused);
402         while (nr_to_write > 0) {
403                 wbc.encountered_congestion = 0;
404                 wbc.nr_to_write = MAX_WRITEBACK_PAGES;
405                 writeback_inodes(&wbc);
406                 if (wbc.nr_to_write > 0) {
407                         if (wbc.encountered_congestion)
408                                 blk_congestion_wait(WRITE, HZ/10);
409                         else
410                                 break;  /* All the old data is written */
411                 }
412                 nr_to_write -= MAX_WRITEBACK_PAGES - wbc.nr_to_write;
413         }
414         if (time_before(next_jif, jiffies + HZ))
415                 next_jif = jiffies + HZ;
416         if (dirty_writeback_centisecs)
417                 mod_timer(&wb_timer, next_jif);
418 }
419
420 /*
421  * sysctl handler for /proc/sys/vm/dirty_writeback_centisecs
422  */
423 int dirty_writeback_centisecs_handler(ctl_table *table, int write,
424                 struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
425 {
426         proc_dointvec(table, write, file, buffer, length, ppos);
427         if (dirty_writeback_centisecs) {
428                 mod_timer(&wb_timer,
429                         jiffies + (dirty_writeback_centisecs * HZ) / 100);
430         } else {
431                 del_timer(&wb_timer);
432         }
433         return 0;
434 }
435
436 static void wb_timer_fn(unsigned long unused)
437 {
438         if (pdflush_operation(wb_kupdate, 0) < 0)
439                 mod_timer(&wb_timer, jiffies + HZ); /* delay 1 second */
440 }
441
442 static void laptop_flush(unsigned long unused)
443 {
444         sys_sync();
445 }
446
447 static void laptop_timer_fn(unsigned long unused)
448 {
449         pdflush_operation(laptop_flush, 0);
450 }
451
452 /*
453  * We've spun up the disk and we're in laptop mode: schedule writeback
454  * of all dirty data a few seconds from now.  If the flush is already scheduled
455  * then push it back - the user is still using the disk.
456  */
457 void laptop_io_completion(void)
458 {
459         mod_timer(&laptop_mode_wb_timer, jiffies + laptop_mode * HZ);
460 }
461
462 /*
463  * We're in laptop mode and we've just synced. The sync's writes will have
464  * caused another writeback to be scheduled by laptop_io_completion.
465  * Nothing needs to be written back anymore, so we unschedule the writeback.
466  */
467 void laptop_sync_completion(void)
468 {
469         del_timer(&laptop_mode_wb_timer);
470 }
471
472 /*
473  * If ratelimit_pages is too high then we can get into dirty-data overload
474  * if a large number of processes all perform writes at the same time.
475  * If it is too low then SMP machines will call the (expensive)
476  * get_writeback_state too often.
477  *
478  * Here we set ratelimit_pages to a level which ensures that when all CPUs are
479  * dirtying in parallel, we cannot go more than 3% (1/32) over the dirty memory
480  * thresholds before writeback cuts in.
481  *
482  * But the limit should not be set too high.  Because it also controls the
483  * amount of memory which the balance_dirty_pages() caller has to write back.
484  * If this is too large then the caller will block on the IO queue all the
485  * time.  So limit it to four megabytes - the balance_dirty_pages() caller
486  * will write six megabyte chunks, max.
487  */
488
489 static void set_ratelimit(void)
490 {
491         ratelimit_pages = total_pages / (num_online_cpus() * 32);
492         if (ratelimit_pages < 16)
493                 ratelimit_pages = 16;
494         if (ratelimit_pages * PAGE_CACHE_SIZE > 4096 * 1024)
495                 ratelimit_pages = (4096 * 1024) / PAGE_CACHE_SIZE;
496 }
497
498 static int
499 ratelimit_handler(struct notifier_block *self, unsigned long u, void *v)
500 {
501         set_ratelimit();
502         return 0;
503 }
504
505 static struct notifier_block ratelimit_nb = {
506         .notifier_call  = ratelimit_handler,
507         .next           = NULL,
508 };
509
510 /*
511  * If the machine has a large highmem:lowmem ratio then scale back the default
512  * dirty memory thresholds: allowing too much dirty highmem pins an excessive
513  * number of buffer_heads.
514  */
515 void __init page_writeback_init(void)
516 {
517         long buffer_pages = nr_free_buffer_pages();
518         long correction;
519
520         total_pages = nr_free_pagecache_pages();
521
522         correction = (100 * 4 * buffer_pages) / total_pages;
523
524         if (correction < 100) {
525                 dirty_background_ratio *= correction;
526                 dirty_background_ratio /= 100;
527                 vm_dirty_ratio *= correction;
528                 vm_dirty_ratio /= 100;
529
530                 if (dirty_background_ratio <= 0)
531                         dirty_background_ratio = 1;
532                 if (vm_dirty_ratio <= 0)
533                         vm_dirty_ratio = 1;
534         }
535         mod_timer(&wb_timer, jiffies + (dirty_writeback_centisecs * HZ) / 100);
536         set_ratelimit();
537         register_cpu_notifier(&ratelimit_nb);
538 }
539
540 int do_writepages(struct address_space *mapping, struct writeback_control *wbc)
541 {
542         if (wbc->nr_to_write <= 0)
543                 return 0;
544         if (mapping->a_ops->writepages)
545                 return mapping->a_ops->writepages(mapping, wbc);
546         return generic_writepages(mapping, wbc);
547 }
548
549 /**
550  * write_one_page - write out a single page and optionally wait on I/O
551  *
552  * @page - the page to write
553  * @wait - if true, wait on writeout
554  *
555  * The page must be locked by the caller and will be unlocked upon return.
556  *
557  * write_one_page() returns a negative error code if I/O failed.
558  */
559 int write_one_page(struct page *page, int wait)
560 {
561         struct address_space *mapping = page->mapping;
562         int ret = 0;
563         struct writeback_control wbc = {
564                 .sync_mode = WB_SYNC_ALL,
565                 .nr_to_write = 1,
566         };
567
568         BUG_ON(!PageLocked(page));
569
570         if (wait)
571                 wait_on_page_writeback(page);
572
573         if (clear_page_dirty_for_io(page)) {
574                 page_cache_get(page);
575                 ret = mapping->a_ops->writepage(page, &wbc);
576                 if (ret == 0 && wait) {
577                         wait_on_page_writeback(page);
578                         if (PageError(page))
579                                 ret = -EIO;
580                 }
581                 page_cache_release(page);
582         } else {
583                 unlock_page(page);
584         }
585         return ret;
586 }
587 EXPORT_SYMBOL(write_one_page);
588
589 /*
590  * For address_spaces which do not use buffers.  Just tag the page as dirty in
591  * its radix tree.
592  *
593  * This is also used when a single buffer is being dirtied: we want to set the
594  * page dirty in that case, but not all the buffers.  This is a "bottom-up"
595  * dirtying, whereas __set_page_dirty_buffers() is a "top-down" dirtying.
596  *
597  * Most callers have locked the page, which pins the address_space in memory.
598  * But zap_pte_range() does not lock the page, however in that case the
599  * mapping is pinned by the vma's ->vm_file reference.
600  *
601  * We take care to handle the case where the page was truncated from the
602  * mapping by re-checking page_mapping() insode tree_lock.
603  */
604 int __set_page_dirty_nobuffers(struct page *page)
605 {
606         int ret = 0;
607
608         if (!TestSetPageDirty(page)) {
609                 struct address_space *mapping = page_mapping(page);
610
611                 if (mapping) {
612                         spin_lock_irq(&mapping->tree_lock);
613                         mapping = page_mapping(page);
614                         if (page_mapping(page)) { /* Race with truncate? */
615                                 BUG_ON(page_mapping(page) != mapping);
616                                 if (!mapping->backing_dev_info->memory_backed)
617                                         inc_page_state(nr_dirty);
618                                 radix_tree_tag_set(&mapping->page_tree,
619                                         page_index(page), PAGECACHE_TAG_DIRTY);
620                         }
621                         spin_unlock_irq(&mapping->tree_lock);
622                         if (mapping->host) {
623                                 /* !PageAnon && !swapper_space */
624                                 __mark_inode_dirty(mapping->host,
625                                                         I_DIRTY_PAGES);
626                         }
627                 }
628         }
629         return ret;
630 }
631 EXPORT_SYMBOL(__set_page_dirty_nobuffers);
632
633 /*
634  * When a writepage implementation decides that it doesn't want to write this
635  * page for some reason, it should redirty the locked page via
636  * redirty_page_for_writepage() and it should then unlock the page and return 0
637  */
638 int redirty_page_for_writepage(struct writeback_control *wbc, struct page *page)
639 {
640         wbc->pages_skipped++;
641         return __set_page_dirty_nobuffers(page);
642 }
643 EXPORT_SYMBOL(redirty_page_for_writepage);
644
645 /*
646  * If the mapping doesn't provide a set_page_dirty a_op, then
647  * just fall through and assume that it wants buffer_heads.
648  */
649 int fastcall set_page_dirty(struct page *page)
650 {
651         struct address_space *mapping = page_mapping(page);
652
653         if (likely(mapping)) {
654                 int (*spd)(struct page *) = mapping->a_ops->set_page_dirty;
655                 if (spd)
656                         return (*spd)(page);
657                 return __set_page_dirty_buffers(page);
658         }
659         if (!PageDirty(page))
660                 SetPageDirty(page);
661         return 0;
662 }
663 EXPORT_SYMBOL(set_page_dirty);
664
665 /*
666  * set_page_dirty() is racy if the caller has no reference against
667  * page->mapping->host, and if the page is unlocked.  This is because another
668  * CPU could truncate the page off the mapping and then free the mapping.
669  *
670  * Usually, the page _is_ locked, or the caller is a user-space process which
671  * holds a reference on the inode by having an open file.
672  *
673  * In other cases, the page should be locked before running set_page_dirty().
674  */
675 int set_page_dirty_lock(struct page *page)
676 {
677         int ret;
678
679         lock_page(page);
680         ret = set_page_dirty(page);
681         unlock_page(page);
682         return ret;
683 }
684 EXPORT_SYMBOL(set_page_dirty_lock);
685
686 /*
687  * Clear a page's dirty flag, while caring for dirty memory accounting. 
688  * Returns true if the page was previously dirty.
689  */
690 int test_clear_page_dirty(struct page *page)
691 {
692         struct address_space *mapping = page_mapping(page);
693         unsigned long flags;
694
695         if (mapping) {
696                 spin_lock_irqsave(&mapping->tree_lock, flags);
697                 if (TestClearPageDirty(page)) {
698                         radix_tree_tag_clear(&mapping->page_tree,
699                                                 page_index(page),
700                                                 PAGECACHE_TAG_DIRTY);
701                         spin_unlock_irqrestore(&mapping->tree_lock, flags);
702                         if (!mapping->backing_dev_info->memory_backed)
703                                 dec_page_state(nr_dirty);
704                         return 1;
705                 }
706                 spin_unlock_irqrestore(&mapping->tree_lock, flags);
707                 return 0;
708         }
709         return TestClearPageDirty(page);
710 }
711 EXPORT_SYMBOL(test_clear_page_dirty);
712
713 /*
714  * Clear a page's dirty flag, while caring for dirty memory accounting.
715  * Returns true if the page was previously dirty.
716  *
717  * This is for preparing to put the page under writeout.  We leave the page
718  * tagged as dirty in the radix tree so that a concurrent write-for-sync
719  * can discover it via a PAGECACHE_TAG_DIRTY walk.  The ->writepage
720  * implementation will run either set_page_writeback() or set_page_dirty(),
721  * at which stage we bring the page's dirty flag and radix-tree dirty tag
722  * back into sync.
723  *
724  * This incoherency between the page's dirty flag and radix-tree tag is
725  * unfortunate, but it only exists while the page is locked.
726  */
727 int clear_page_dirty_for_io(struct page *page)
728 {
729         struct address_space *mapping = page_mapping(page);
730
731         if (mapping) {
732                 if (TestClearPageDirty(page)) {
733                         if (!mapping->backing_dev_info->memory_backed)
734                                 dec_page_state(nr_dirty);
735                         return 1;
736                 }
737                 return 0;
738         }
739         return TestClearPageDirty(page);
740 }
741 EXPORT_SYMBOL(clear_page_dirty_for_io);
742
743 /*
744  * Clear a page's dirty flag while ignoring dirty memory accounting
745  */
746 int __clear_page_dirty(struct page *page)
747 {
748         struct address_space *mapping = page_mapping(page);
749
750         if (mapping) {
751                 unsigned long flags;
752
753                 spin_lock_irqsave(&mapping->tree_lock, flags);
754                 if (TestClearPageDirty(page)) {
755                         radix_tree_tag_clear(&mapping->page_tree,
756                                                 page_index(page),
757                                                 PAGECACHE_TAG_DIRTY);
758                         spin_unlock_irqrestore(&mapping->tree_lock, flags);
759                         return 1;
760                 }
761                 spin_unlock_irqrestore(&mapping->tree_lock, flags);
762                 return 0;
763         }
764         return TestClearPageDirty(page);
765 }
766
767 int test_clear_page_writeback(struct page *page)
768 {
769         struct address_space *mapping = page_mapping(page);
770         int ret;
771
772         if (mapping) {
773                 unsigned long flags;
774
775                 spin_lock_irqsave(&mapping->tree_lock, flags);
776                 ret = TestClearPageWriteback(page);
777                 if (ret)
778                         radix_tree_tag_clear(&mapping->page_tree,
779                                                 page_index(page),
780                                                 PAGECACHE_TAG_WRITEBACK);
781                 spin_unlock_irqrestore(&mapping->tree_lock, flags);
782         } else {
783                 ret = TestClearPageWriteback(page);
784         }
785         return ret;
786 }
787
788 int test_set_page_writeback(struct page *page)
789 {
790         struct address_space *mapping = page_mapping(page);
791         int ret;
792
793         if (mapping) {
794                 unsigned long flags;
795
796                 spin_lock_irqsave(&mapping->tree_lock, flags);
797                 ret = TestSetPageWriteback(page);
798                 if (!ret)
799                         radix_tree_tag_set(&mapping->page_tree,
800                                                 page_index(page),
801                                                 PAGECACHE_TAG_WRITEBACK);
802                 if (!PageDirty(page))
803                         radix_tree_tag_clear(&mapping->page_tree,
804                                                 page_index(page),
805                                                 PAGECACHE_TAG_DIRTY);
806                 spin_unlock_irqrestore(&mapping->tree_lock, flags);
807         } else {
808                 ret = TestSetPageWriteback(page);
809         }
810         return ret;
811
812 }
813 EXPORT_SYMBOL(test_set_page_writeback);
814
815 /*
816  * Return true if any of the pages in the mapping are marged with the
817  * passed tag.
818  */
819 int mapping_tagged(struct address_space *mapping, int tag)
820 {
821         unsigned long flags;
822         int ret;
823
824         spin_lock_irqsave(&mapping->tree_lock, flags);
825         ret = radix_tree_tagged(&mapping->page_tree, tag);
826         spin_unlock_irqrestore(&mapping->tree_lock, flags);
827         return ret;
828 }
829 EXPORT_SYMBOL(mapping_tagged);