blob: efd2705e4986d64f82fc2a4f87452020b17ee6a7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Peter Zijlstrad08b3852006-09-25 23:30:57 -070026#include <linux/rmap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/percpu.h>
28#include <linux/notifier.h>
29#include <linux/smp.h>
30#include <linux/sysctl.h>
31#include <linux/cpu.h>
32#include <linux/syscalls.h>
33
34/*
35 * The maximum number of pages to writeout in a single bdflush/kupdate
36 * operation. We do this so we don't hold I_LOCK against an inode for
37 * enormous amounts of time, which would block a userspace task which has
38 * been forced to throttle against that inode. Also, the code reevaluates
39 * the dirty each time it has written this many pages.
40 */
41#define MAX_WRITEBACK_PAGES 1024
42
43/*
44 * After a CPU has dirtied this many pages, balance_dirty_pages_ratelimited
45 * will look to see if it needs to force writeback or throttling.
46 */
47static long ratelimit_pages = 32;
48
Andrew Mortone236a162006-01-18 17:42:26 -080049static int dirty_exceeded __cacheline_aligned_in_smp; /* Dirty mem may be over limit */
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
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 */
57static 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 */
67int dirty_background_ratio = 10;
68
69/*
70 * The generator of dirty data starts writeback at this percentage
71 */
72int vm_dirty_ratio = 40;
73
74/*
Coywolf Qi Huntfd5403c2006-04-10 22:54:35 -070075 * The interval between `kupdate'-style writebacks, in jiffies
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 */
Bart Samwelf6ef9432006-03-24 03:15:48 -080077int dirty_writeback_interval = 5 * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79/*
Coywolf Qi Huntfd5403c2006-04-10 22:54:35 -070080 * The longest number of jiffies for which data is allowed to remain dirty
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 */
Bart Samwelf6ef9432006-03-24 03:15:48 -080082int dirty_expire_interval = 30 * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84/*
85 * Flag that makes the machine dump writes/reads and block dirtyings.
86 */
87int block_dump;
88
89/*
Bart Samweled5b43f2006-03-24 03:15:49 -080090 * Flag that puts the machine in "laptop mode". Doubles as a timeout in jiffies:
91 * a full sync is triggered after this time elapses without any disk activity.
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 */
93int laptop_mode;
94
95EXPORT_SYMBOL(laptop_mode);
96
97/* End of sysctl-exported parameters */
98
99
100static void background_writeout(unsigned long _min_pages);
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102/*
103 * Work out the current dirty-memory clamping and background writeout
104 * thresholds.
105 *
106 * The main aim here is to lower them aggressively if there is a lot of mapped
107 * memory around. To avoid stressing page reclaim with lots of unreclaimable
108 * pages. It is better to clamp down on writers than to start swapping, and
109 * performing lots of scanning.
110 *
111 * We only allow 1/2 of the currently-unmapped memory to be dirtied.
112 *
113 * We don't permit the clamping level to fall below 5% - that is getting rather
114 * excessive.
115 *
116 * We make sure that the background writeout level is below the adjusted
117 * clamping level.
118 */
119static void
Christoph Lameterc24f21b2006-06-30 01:55:42 -0700120get_dirty_limits(long *pbackground, long *pdirty,
121 struct address_space *mapping)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
123 int background_ratio; /* Percentages */
124 int dirty_ratio;
125 int unmapped_ratio;
126 long background;
127 long dirty;
Chandra Seetharaman40c99aa2006-09-29 02:01:24 -0700128 unsigned long available_memory = vm_total_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 struct task_struct *tsk;
130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131#ifdef CONFIG_HIGHMEM
132 /*
133 * If this mapping can only allocate from low memory,
134 * we exclude high memory from our count.
135 */
136 if (mapping && !(mapping_gfp_mask(mapping) & __GFP_HIGHMEM))
137 available_memory -= totalhigh_pages;
138#endif
139
140
Christoph Lameterc24f21b2006-06-30 01:55:42 -0700141 unmapped_ratio = 100 - ((global_page_state(NR_FILE_MAPPED) +
142 global_page_state(NR_ANON_PAGES)) * 100) /
Chandra Seetharaman40c99aa2006-09-29 02:01:24 -0700143 vm_total_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145 dirty_ratio = vm_dirty_ratio;
146 if (dirty_ratio > unmapped_ratio / 2)
147 dirty_ratio = unmapped_ratio / 2;
148
149 if (dirty_ratio < 5)
150 dirty_ratio = 5;
151
152 background_ratio = dirty_background_ratio;
153 if (background_ratio >= dirty_ratio)
154 background_ratio = dirty_ratio / 2;
155
156 background = (background_ratio * available_memory) / 100;
157 dirty = (dirty_ratio * available_memory) / 100;
158 tsk = current;
159 if (tsk->flags & PF_LESS_THROTTLE || rt_task(tsk)) {
160 background += background / 4;
161 dirty += dirty / 4;
162 }
163 *pbackground = background;
164 *pdirty = dirty;
165}
166
167/*
168 * balance_dirty_pages() must be called by processes which are generating dirty
169 * data. It looks at the number of dirty pages in the machine and will force
170 * the caller to perform writeback if the system is over `vm_dirty_ratio'.
171 * If we're over `background_thresh' then pdflush is woken to perform some
172 * writeout.
173 */
174static void balance_dirty_pages(struct address_space *mapping)
175{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 long nr_reclaimable;
177 long background_thresh;
178 long dirty_thresh;
179 unsigned long pages_written = 0;
180 unsigned long write_chunk = sync_writeback_pages();
181
182 struct backing_dev_info *bdi = mapping->backing_dev_info;
183
184 for (;;) {
185 struct writeback_control wbc = {
186 .bdi = bdi,
187 .sync_mode = WB_SYNC_NONE,
188 .older_than_this = NULL,
189 .nr_to_write = write_chunk,
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700190 .range_cyclic = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 };
192
Christoph Lameterc24f21b2006-06-30 01:55:42 -0700193 get_dirty_limits(&background_thresh, &dirty_thresh, mapping);
194 nr_reclaimable = global_page_state(NR_FILE_DIRTY) +
195 global_page_state(NR_UNSTABLE_NFS);
196 if (nr_reclaimable + global_page_state(NR_WRITEBACK) <=
197 dirty_thresh)
198 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Andrew Mortone236a162006-01-18 17:42:26 -0800200 if (!dirty_exceeded)
201 dirty_exceeded = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203 /* Note: nr_reclaimable denotes nr_dirty + nr_unstable.
204 * Unstable writes are a feature of certain networked
205 * filesystems (i.e. NFS) in which data may have been
206 * written to the server's write cache, but has not yet
207 * been flushed to permanent storage.
208 */
209 if (nr_reclaimable) {
210 writeback_inodes(&wbc);
Christoph Lameterc24f21b2006-06-30 01:55:42 -0700211 get_dirty_limits(&background_thresh,
212 &dirty_thresh, mapping);
213 nr_reclaimable = global_page_state(NR_FILE_DIRTY) +
214 global_page_state(NR_UNSTABLE_NFS);
215 if (nr_reclaimable +
216 global_page_state(NR_WRITEBACK)
217 <= dirty_thresh)
218 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 pages_written += write_chunk - wbc.nr_to_write;
220 if (pages_written >= write_chunk)
221 break; /* We've done our duty */
222 }
223 blk_congestion_wait(WRITE, HZ/10);
224 }
225
Christoph Lameterc24f21b2006-06-30 01:55:42 -0700226 if (nr_reclaimable + global_page_state(NR_WRITEBACK)
227 <= dirty_thresh && dirty_exceeded)
228 dirty_exceeded = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 if (writeback_in_progress(bdi))
231 return; /* pdflush is already working this queue */
232
233 /*
234 * In laptop mode, we wait until hitting the higher threshold before
235 * starting background writeout, and then write out all the way down
236 * to the lower threshold. So slow writers cause minimal disk activity.
237 *
238 * In normal mode, we start background writeout at the lower
239 * background_thresh, to keep the amount of dirty memory low.
240 */
241 if ((laptop_mode && pages_written) ||
242 (!laptop_mode && (nr_reclaimable > background_thresh)))
243 pdflush_operation(background_writeout, 0);
244}
245
Peter Zijlstraedc79b22006-09-25 23:30:58 -0700246void set_page_dirty_balance(struct page *page)
247{
248 if (set_page_dirty(page)) {
249 struct address_space *mapping = page_mapping(page);
250
251 if (mapping)
252 balance_dirty_pages_ratelimited(mapping);
253 }
254}
255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256/**
Andrew Mortonfa5a7342006-03-24 03:18:10 -0800257 * balance_dirty_pages_ratelimited_nr - balance dirty memory state
Martin Waitz67be2dd2005-05-01 08:59:26 -0700258 * @mapping: address_space which was dirtied
Martin Waitza5802902006-04-02 13:59:55 +0200259 * @nr_pages_dirtied: number of pages which the caller has just dirtied
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 *
261 * Processes which are dirtying memory should call in here once for each page
262 * which was newly dirtied. The function will periodically check the system's
263 * dirty state and will initiate writeback if needed.
264 *
265 * On really big machines, get_writeback_state is expensive, so try to avoid
266 * calling it too often (ratelimiting). But once we're over the dirty memory
267 * limit we decrease the ratelimiting by a lot, to prevent individual processes
268 * from overshooting the limit by (ratelimit_pages) each.
269 */
Andrew Mortonfa5a7342006-03-24 03:18:10 -0800270void balance_dirty_pages_ratelimited_nr(struct address_space *mapping,
271 unsigned long nr_pages_dirtied)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
Andrew Mortonfa5a7342006-03-24 03:18:10 -0800273 static DEFINE_PER_CPU(unsigned long, ratelimits) = 0;
274 unsigned long ratelimit;
275 unsigned long *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277 ratelimit = ratelimit_pages;
278 if (dirty_exceeded)
279 ratelimit = 8;
280
281 /*
282 * Check the rate limiting. Also, we do not want to throttle real-time
283 * tasks in balance_dirty_pages(). Period.
284 */
Andrew Mortonfa5a7342006-03-24 03:18:10 -0800285 preempt_disable();
286 p = &__get_cpu_var(ratelimits);
287 *p += nr_pages_dirtied;
288 if (unlikely(*p >= ratelimit)) {
289 *p = 0;
290 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 balance_dirty_pages(mapping);
292 return;
293 }
Andrew Mortonfa5a7342006-03-24 03:18:10 -0800294 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295}
Andrew Mortonfa5a7342006-03-24 03:18:10 -0800296EXPORT_SYMBOL(balance_dirty_pages_ratelimited_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
298void throttle_vm_writeout(void)
299{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 long background_thresh;
301 long dirty_thresh;
302
303 for ( ; ; ) {
Christoph Lameterc24f21b2006-06-30 01:55:42 -0700304 get_dirty_limits(&background_thresh, &dirty_thresh, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306 /*
307 * Boost the allowable dirty threshold a bit for page
308 * allocators so they don't get DoS'ed by heavy writers
309 */
310 dirty_thresh += dirty_thresh / 10; /* wheeee... */
311
Christoph Lameterc24f21b2006-06-30 01:55:42 -0700312 if (global_page_state(NR_UNSTABLE_NFS) +
313 global_page_state(NR_WRITEBACK) <= dirty_thresh)
314 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 blk_congestion_wait(WRITE, HZ/10);
316 }
317}
318
319
320/*
321 * writeback at least _min_pages, and keep writing until the amount of dirty
322 * memory is less than the background threshold, or until we're all clean.
323 */
324static void background_writeout(unsigned long _min_pages)
325{
326 long min_pages = _min_pages;
327 struct writeback_control wbc = {
328 .bdi = NULL,
329 .sync_mode = WB_SYNC_NONE,
330 .older_than_this = NULL,
331 .nr_to_write = 0,
332 .nonblocking = 1,
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700333 .range_cyclic = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 };
335
336 for ( ; ; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 long background_thresh;
338 long dirty_thresh;
339
Christoph Lameterc24f21b2006-06-30 01:55:42 -0700340 get_dirty_limits(&background_thresh, &dirty_thresh, NULL);
341 if (global_page_state(NR_FILE_DIRTY) +
342 global_page_state(NR_UNSTABLE_NFS) < background_thresh
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 && min_pages <= 0)
344 break;
345 wbc.encountered_congestion = 0;
346 wbc.nr_to_write = MAX_WRITEBACK_PAGES;
347 wbc.pages_skipped = 0;
348 writeback_inodes(&wbc);
349 min_pages -= MAX_WRITEBACK_PAGES - wbc.nr_to_write;
350 if (wbc.nr_to_write > 0 || wbc.pages_skipped > 0) {
351 /* Wrote less than expected */
352 blk_congestion_wait(WRITE, HZ/10);
353 if (!wbc.encountered_congestion)
354 break;
355 }
356 }
357}
358
359/*
360 * Start writeback of `nr_pages' pages. If `nr_pages' is zero, write back
361 * the whole world. Returns 0 if a pdflush thread was dispatched. Returns
362 * -1 if all pdflush threads were busy.
363 */
Pekka J Enberg687a21c2005-06-28 20:44:55 -0700364int wakeup_pdflush(long nr_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365{
Christoph Lameterc24f21b2006-06-30 01:55:42 -0700366 if (nr_pages == 0)
367 nr_pages = global_page_state(NR_FILE_DIRTY) +
368 global_page_state(NR_UNSTABLE_NFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 return pdflush_operation(background_writeout, nr_pages);
370}
371
372static void wb_timer_fn(unsigned long unused);
373static void laptop_timer_fn(unsigned long unused);
374
Ingo Molnar8d06afa2005-09-09 13:10:40 -0700375static DEFINE_TIMER(wb_timer, wb_timer_fn, 0, 0);
376static DEFINE_TIMER(laptop_mode_wb_timer, laptop_timer_fn, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
378/*
379 * Periodic writeback of "old" data.
380 *
381 * Define "old": the first time one of an inode's pages is dirtied, we mark the
382 * dirtying-time in the inode's address_space. So this periodic writeback code
383 * just walks the superblock inode list, writing back any inodes which are
384 * older than a specific point in time.
385 *
Bart Samwelf6ef9432006-03-24 03:15:48 -0800386 * Try to run once per dirty_writeback_interval. But if a writeback event
387 * takes longer than a dirty_writeback_interval interval, then leave a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 * one-second gap.
389 *
390 * older_than_this takes precedence over nr_to_write. So we'll only write back
391 * all dirty pages if they are all attached to "old" mappings.
392 */
393static void wb_kupdate(unsigned long arg)
394{
395 unsigned long oldest_jif;
396 unsigned long start_jif;
397 unsigned long next_jif;
398 long nr_to_write;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 struct writeback_control wbc = {
400 .bdi = NULL,
401 .sync_mode = WB_SYNC_NONE,
402 .older_than_this = &oldest_jif,
403 .nr_to_write = 0,
404 .nonblocking = 1,
405 .for_kupdate = 1,
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700406 .range_cyclic = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 };
408
409 sync_supers();
410
Bart Samwelf6ef9432006-03-24 03:15:48 -0800411 oldest_jif = jiffies - dirty_expire_interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 start_jif = jiffies;
Bart Samwelf6ef9432006-03-24 03:15:48 -0800413 next_jif = start_jif + dirty_writeback_interval;
Christoph Lameterc24f21b2006-06-30 01:55:42 -0700414 nr_to_write = global_page_state(NR_FILE_DIRTY) +
415 global_page_state(NR_UNSTABLE_NFS) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 (inodes_stat.nr_inodes - inodes_stat.nr_unused);
417 while (nr_to_write > 0) {
418 wbc.encountered_congestion = 0;
419 wbc.nr_to_write = MAX_WRITEBACK_PAGES;
420 writeback_inodes(&wbc);
421 if (wbc.nr_to_write > 0) {
422 if (wbc.encountered_congestion)
423 blk_congestion_wait(WRITE, HZ/10);
424 else
425 break; /* All the old data is written */
426 }
427 nr_to_write -= MAX_WRITEBACK_PAGES - wbc.nr_to_write;
428 }
429 if (time_before(next_jif, jiffies + HZ))
430 next_jif = jiffies + HZ;
Bart Samwelf6ef9432006-03-24 03:15:48 -0800431 if (dirty_writeback_interval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 mod_timer(&wb_timer, next_jif);
433}
434
435/*
436 * sysctl handler for /proc/sys/vm/dirty_writeback_centisecs
437 */
438int dirty_writeback_centisecs_handler(ctl_table *table, int write,
439 struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
440{
Bart Samwelf6ef9432006-03-24 03:15:48 -0800441 proc_dointvec_userhz_jiffies(table, write, file, buffer, length, ppos);
442 if (dirty_writeback_interval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 mod_timer(&wb_timer,
Bart Samwelf6ef9432006-03-24 03:15:48 -0800444 jiffies + dirty_writeback_interval);
445 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 del_timer(&wb_timer);
447 }
448 return 0;
449}
450
451static void wb_timer_fn(unsigned long unused)
452{
453 if (pdflush_operation(wb_kupdate, 0) < 0)
454 mod_timer(&wb_timer, jiffies + HZ); /* delay 1 second */
455}
456
457static void laptop_flush(unsigned long unused)
458{
459 sys_sync();
460}
461
462static void laptop_timer_fn(unsigned long unused)
463{
464 pdflush_operation(laptop_flush, 0);
465}
466
467/*
468 * We've spun up the disk and we're in laptop mode: schedule writeback
469 * of all dirty data a few seconds from now. If the flush is already scheduled
470 * then push it back - the user is still using the disk.
471 */
472void laptop_io_completion(void)
473{
Bart Samweled5b43f2006-03-24 03:15:49 -0800474 mod_timer(&laptop_mode_wb_timer, jiffies + laptop_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475}
476
477/*
478 * We're in laptop mode and we've just synced. The sync's writes will have
479 * caused another writeback to be scheduled by laptop_io_completion.
480 * Nothing needs to be written back anymore, so we unschedule the writeback.
481 */
482void laptop_sync_completion(void)
483{
484 del_timer(&laptop_mode_wb_timer);
485}
486
487/*
488 * If ratelimit_pages is too high then we can get into dirty-data overload
489 * if a large number of processes all perform writes at the same time.
490 * If it is too low then SMP machines will call the (expensive)
491 * get_writeback_state too often.
492 *
493 * Here we set ratelimit_pages to a level which ensures that when all CPUs are
494 * dirtying in parallel, we cannot go more than 3% (1/32) over the dirty memory
495 * thresholds before writeback cuts in.
496 *
497 * But the limit should not be set too high. Because it also controls the
498 * amount of memory which the balance_dirty_pages() caller has to write back.
499 * If this is too large then the caller will block on the IO queue all the
500 * time. So limit it to four megabytes - the balance_dirty_pages() caller
501 * will write six megabyte chunks, max.
502 */
503
504static void set_ratelimit(void)
505{
Chandra Seetharaman40c99aa2006-09-29 02:01:24 -0700506 ratelimit_pages = vm_total_pages / (num_online_cpus() * 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 if (ratelimit_pages < 16)
508 ratelimit_pages = 16;
509 if (ratelimit_pages * PAGE_CACHE_SIZE > 4096 * 1024)
510 ratelimit_pages = (4096 * 1024) / PAGE_CACHE_SIZE;
511}
512
Chandra Seetharaman26c21432006-06-27 02:54:10 -0700513static int __cpuinit
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514ratelimit_handler(struct notifier_block *self, unsigned long u, void *v)
515{
516 set_ratelimit();
517 return 0;
518}
519
Chandra Seetharaman74b85f32006-06-27 02:54:09 -0700520static struct notifier_block __cpuinitdata ratelimit_nb = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 .notifier_call = ratelimit_handler,
522 .next = NULL,
523};
524
525/*
526 * If the machine has a large highmem:lowmem ratio then scale back the default
527 * dirty memory thresholds: allowing too much dirty highmem pins an excessive
528 * number of buffer_heads.
529 */
530void __init page_writeback_init(void)
531{
532 long buffer_pages = nr_free_buffer_pages();
533 long correction;
534
Chandra Seetharaman40c99aa2006-09-29 02:01:24 -0700535 correction = (100 * 4 * buffer_pages) / vm_total_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
537 if (correction < 100) {
538 dirty_background_ratio *= correction;
539 dirty_background_ratio /= 100;
540 vm_dirty_ratio *= correction;
541 vm_dirty_ratio /= 100;
542
543 if (dirty_background_ratio <= 0)
544 dirty_background_ratio = 1;
545 if (vm_dirty_ratio <= 0)
546 vm_dirty_ratio = 1;
547 }
Bart Samwelf6ef9432006-03-24 03:15:48 -0800548 mod_timer(&wb_timer, jiffies + dirty_writeback_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 set_ratelimit();
550 register_cpu_notifier(&ratelimit_nb);
551}
552
553int do_writepages(struct address_space *mapping, struct writeback_control *wbc)
554{
Andrew Morton22905f72005-11-16 15:07:01 -0800555 int ret;
556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 if (wbc->nr_to_write <= 0)
558 return 0;
Andrew Morton22905f72005-11-16 15:07:01 -0800559 wbc->for_writepages = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 if (mapping->a_ops->writepages)
Peter Zijlstrad08b3852006-09-25 23:30:57 -0700561 ret = mapping->a_ops->writepages(mapping, wbc);
Andrew Morton22905f72005-11-16 15:07:01 -0800562 else
563 ret = generic_writepages(mapping, wbc);
564 wbc->for_writepages = 0;
565 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566}
567
568/**
569 * write_one_page - write out a single page and optionally wait on I/O
570 *
Martin Waitz67be2dd2005-05-01 08:59:26 -0700571 * @page: the page to write
572 * @wait: if true, wait on writeout
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 *
574 * The page must be locked by the caller and will be unlocked upon return.
575 *
576 * write_one_page() returns a negative error code if I/O failed.
577 */
578int write_one_page(struct page *page, int wait)
579{
580 struct address_space *mapping = page->mapping;
581 int ret = 0;
582 struct writeback_control wbc = {
583 .sync_mode = WB_SYNC_ALL,
584 .nr_to_write = 1,
585 };
586
587 BUG_ON(!PageLocked(page));
588
589 if (wait)
590 wait_on_page_writeback(page);
591
592 if (clear_page_dirty_for_io(page)) {
593 page_cache_get(page);
594 ret = mapping->a_ops->writepage(page, &wbc);
595 if (ret == 0 && wait) {
596 wait_on_page_writeback(page);
597 if (PageError(page))
598 ret = -EIO;
599 }
600 page_cache_release(page);
601 } else {
602 unlock_page(page);
603 }
604 return ret;
605}
606EXPORT_SYMBOL(write_one_page);
607
608/*
609 * For address_spaces which do not use buffers. Just tag the page as dirty in
610 * its radix tree.
611 *
612 * This is also used when a single buffer is being dirtied: we want to set the
613 * page dirty in that case, but not all the buffers. This is a "bottom-up"
614 * dirtying, whereas __set_page_dirty_buffers() is a "top-down" dirtying.
615 *
616 * Most callers have locked the page, which pins the address_space in memory.
617 * But zap_pte_range() does not lock the page, however in that case the
618 * mapping is pinned by the vma's ->vm_file reference.
619 *
620 * We take care to handle the case where the page was truncated from the
621 * mapping by re-checking page_mapping() insode tree_lock.
622 */
623int __set_page_dirty_nobuffers(struct page *page)
624{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 if (!TestSetPageDirty(page)) {
626 struct address_space *mapping = page_mapping(page);
627 struct address_space *mapping2;
628
629 if (mapping) {
630 write_lock_irq(&mapping->tree_lock);
631 mapping2 = page_mapping(page);
632 if (mapping2) { /* Race with truncate? */
633 BUG_ON(mapping2 != mapping);
634 if (mapping_cap_account_dirty(mapping))
Christoph Lameterb1e7a8f2006-06-30 01:55:39 -0700635 __inc_zone_page_state(page,
636 NR_FILE_DIRTY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 radix_tree_tag_set(&mapping->page_tree,
638 page_index(page), PAGECACHE_TAG_DIRTY);
639 }
640 write_unlock_irq(&mapping->tree_lock);
641 if (mapping->host) {
642 /* !PageAnon && !swapper_space */
643 __mark_inode_dirty(mapping->host,
644 I_DIRTY_PAGES);
645 }
646 }
Andrew Morton4741c9f2006-03-24 03:18:11 -0800647 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 }
Andrew Morton4741c9f2006-03-24 03:18:11 -0800649 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650}
651EXPORT_SYMBOL(__set_page_dirty_nobuffers);
652
653/*
654 * When a writepage implementation decides that it doesn't want to write this
655 * page for some reason, it should redirty the locked page via
656 * redirty_page_for_writepage() and it should then unlock the page and return 0
657 */
658int redirty_page_for_writepage(struct writeback_control *wbc, struct page *page)
659{
660 wbc->pages_skipped++;
661 return __set_page_dirty_nobuffers(page);
662}
663EXPORT_SYMBOL(redirty_page_for_writepage);
664
665/*
666 * If the mapping doesn't provide a set_page_dirty a_op, then
667 * just fall through and assume that it wants buffer_heads.
668 */
669int fastcall set_page_dirty(struct page *page)
670{
671 struct address_space *mapping = page_mapping(page);
672
673 if (likely(mapping)) {
674 int (*spd)(struct page *) = mapping->a_ops->set_page_dirty;
675 if (spd)
676 return (*spd)(page);
677 return __set_page_dirty_buffers(page);
678 }
Andrew Morton4741c9f2006-03-24 03:18:11 -0800679 if (!PageDirty(page)) {
680 if (!TestSetPageDirty(page))
681 return 1;
682 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 return 0;
684}
685EXPORT_SYMBOL(set_page_dirty);
686
687/*
688 * set_page_dirty() is racy if the caller has no reference against
689 * page->mapping->host, and if the page is unlocked. This is because another
690 * CPU could truncate the page off the mapping and then free the mapping.
691 *
692 * Usually, the page _is_ locked, or the caller is a user-space process which
693 * holds a reference on the inode by having an open file.
694 *
695 * In other cases, the page should be locked before running set_page_dirty().
696 */
697int set_page_dirty_lock(struct page *page)
698{
699 int ret;
700
Nick Piggindb376482006-09-25 23:31:24 -0700701 lock_page_nosync(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 ret = set_page_dirty(page);
703 unlock_page(page);
704 return ret;
705}
706EXPORT_SYMBOL(set_page_dirty_lock);
707
708/*
709 * Clear a page's dirty flag, while caring for dirty memory accounting.
710 * Returns true if the page was previously dirty.
711 */
712int test_clear_page_dirty(struct page *page)
713{
714 struct address_space *mapping = page_mapping(page);
715 unsigned long flags;
716
717 if (mapping) {
718 write_lock_irqsave(&mapping->tree_lock, flags);
719 if (TestClearPageDirty(page)) {
720 radix_tree_tag_clear(&mapping->page_tree,
721 page_index(page),
722 PAGECACHE_TAG_DIRTY);
Christoph Lameterb1e7a8f2006-06-30 01:55:39 -0700723 write_unlock_irqrestore(&mapping->tree_lock, flags);
Peter Zijlstrad08b3852006-09-25 23:30:57 -0700724 /*
725 * We can continue to use `mapping' here because the
726 * page is locked, which pins the address_space
727 */
728 if (mapping_cap_account_dirty(mapping)) {
729 page_mkclean(page);
730 dec_zone_page_state(page, NR_FILE_DIRTY);
731 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 return 1;
733 }
734 write_unlock_irqrestore(&mapping->tree_lock, flags);
735 return 0;
736 }
737 return TestClearPageDirty(page);
738}
739EXPORT_SYMBOL(test_clear_page_dirty);
740
741/*
742 * Clear a page's dirty flag, while caring for dirty memory accounting.
743 * Returns true if the page was previously dirty.
744 *
745 * This is for preparing to put the page under writeout. We leave the page
746 * tagged as dirty in the radix tree so that a concurrent write-for-sync
747 * can discover it via a PAGECACHE_TAG_DIRTY walk. The ->writepage
748 * implementation will run either set_page_writeback() or set_page_dirty(),
749 * at which stage we bring the page's dirty flag and radix-tree dirty tag
750 * back into sync.
751 *
752 * This incoherency between the page's dirty flag and radix-tree tag is
753 * unfortunate, but it only exists while the page is locked.
754 */
755int clear_page_dirty_for_io(struct page *page)
756{
757 struct address_space *mapping = page_mapping(page);
758
759 if (mapping) {
760 if (TestClearPageDirty(page)) {
Peter Zijlstrad08b3852006-09-25 23:30:57 -0700761 if (mapping_cap_account_dirty(mapping)) {
762 page_mkclean(page);
Christoph Lameterb1e7a8f2006-06-30 01:55:39 -0700763 dec_zone_page_state(page, NR_FILE_DIRTY);
Peter Zijlstrad08b3852006-09-25 23:30:57 -0700764 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 return 1;
766 }
767 return 0;
768 }
769 return TestClearPageDirty(page);
770}
Hans Reiser58bb01a2005-11-18 01:10:53 -0800771EXPORT_SYMBOL(clear_page_dirty_for_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
773int test_clear_page_writeback(struct page *page)
774{
775 struct address_space *mapping = page_mapping(page);
776 int ret;
777
778 if (mapping) {
779 unsigned long flags;
780
781 write_lock_irqsave(&mapping->tree_lock, flags);
782 ret = TestClearPageWriteback(page);
783 if (ret)
784 radix_tree_tag_clear(&mapping->page_tree,
785 page_index(page),
786 PAGECACHE_TAG_WRITEBACK);
787 write_unlock_irqrestore(&mapping->tree_lock, flags);
788 } else {
789 ret = TestClearPageWriteback(page);
790 }
791 return ret;
792}
793
794int test_set_page_writeback(struct page *page)
795{
796 struct address_space *mapping = page_mapping(page);
797 int ret;
798
799 if (mapping) {
800 unsigned long flags;
801
802 write_lock_irqsave(&mapping->tree_lock, flags);
803 ret = TestSetPageWriteback(page);
804 if (!ret)
805 radix_tree_tag_set(&mapping->page_tree,
806 page_index(page),
807 PAGECACHE_TAG_WRITEBACK);
808 if (!PageDirty(page))
809 radix_tree_tag_clear(&mapping->page_tree,
810 page_index(page),
811 PAGECACHE_TAG_DIRTY);
812 write_unlock_irqrestore(&mapping->tree_lock, flags);
813 } else {
814 ret = TestSetPageWriteback(page);
815 }
816 return ret;
817
818}
819EXPORT_SYMBOL(test_set_page_writeback);
820
821/*
Trond Myklebust275a0822006-08-22 20:06:24 -0400822 * Wakes up tasks that are being throttled due to writeback congestion
823 */
824void writeback_congestion_end(void)
825{
826 blk_congestion_end(WRITE);
827}
828EXPORT_SYMBOL(writeback_congestion_end);
829
830/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 * Return true if any of the pages in the mapping are marged with the
832 * passed tag.
833 */
834int mapping_tagged(struct address_space *mapping, int tag)
835{
836 unsigned long flags;
837 int ret;
838
839 read_lock_irqsave(&mapping->tree_lock, flags);
840 ret = radix_tree_tagged(&mapping->page_tree, tag);
841 read_unlock_irqrestore(&mapping->tree_lock, flags);
842 return ret;
843}
844EXPORT_SYMBOL(mapping_tagged);