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