blob: 9fdcc7903956318fbe53678349175ab01428e768 [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>
David Howellscf9a2ae2006-08-29 19:05:54 +010033#include <linux/buffer_head.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35/*
36 * The maximum number of pages to writeout in a single bdflush/kupdate
37 * operation. We do this so we don't hold I_LOCK against an inode for
38 * enormous amounts of time, which would block a userspace task which has
39 * been forced to throttle against that inode. Also, the code reevaluates
40 * the dirty each time it has written this many pages.
41 */
42#define MAX_WRITEBACK_PAGES 1024
43
44/*
45 * After a CPU has dirtied this many pages, balance_dirty_pages_ratelimited
46 * will look to see if it needs to force writeback or throttling.
47 */
48static long ratelimit_pages = 32;
49
Andrew Mortone236a162006-01-18 17:42:26 -080050static int dirty_exceeded __cacheline_aligned_in_smp; /* Dirty mem may be over limit */
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52/*
53 * When balance_dirty_pages decides that the caller needs to perform some
54 * non-background writeback, this is how many pages it will attempt to write.
55 * It should be somewhat larger than RATELIMIT_PAGES to ensure that reasonably
56 * large amounts of I/O are submitted.
57 */
58static inline long sync_writeback_pages(void)
59{
60 return ratelimit_pages + ratelimit_pages / 2;
61}
62
63/* The following parameters are exported via /proc/sys/vm */
64
65/*
66 * Start background writeback (via pdflush) at this percentage
67 */
68int dirty_background_ratio = 10;
69
70/*
71 * The generator of dirty data starts writeback at this percentage
72 */
73int vm_dirty_ratio = 40;
74
75/*
Coywolf Qi Huntfd5403c2006-04-10 22:54:35 -070076 * The interval between `kupdate'-style writebacks, in jiffies
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 */
Bart Samwelf6ef9432006-03-24 03:15:48 -080078int dirty_writeback_interval = 5 * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80/*
Coywolf Qi Huntfd5403c2006-04-10 22:54:35 -070081 * The longest number of jiffies for which data is allowed to remain dirty
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 */
Bart Samwelf6ef9432006-03-24 03:15:48 -080083int dirty_expire_interval = 30 * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85/*
86 * Flag that makes the machine dump writes/reads and block dirtyings.
87 */
88int block_dump;
89
90/*
Bart Samweled5b43f2006-03-24 03:15:49 -080091 * Flag that puts the machine in "laptop mode". Doubles as a timeout in jiffies:
92 * a full sync is triggered after this time elapses without any disk activity.
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 */
94int laptop_mode;
95
96EXPORT_SYMBOL(laptop_mode);
97
98/* End of sysctl-exported parameters */
99
100
101static void background_writeout(unsigned long _min_pages);
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103/*
104 * Work out the current dirty-memory clamping and background writeout
105 * thresholds.
106 *
107 * The main aim here is to lower them aggressively if there is a lot of mapped
108 * memory around. To avoid stressing page reclaim with lots of unreclaimable
109 * pages. It is better to clamp down on writers than to start swapping, and
110 * performing lots of scanning.
111 *
112 * We only allow 1/2 of the currently-unmapped memory to be dirtied.
113 *
114 * We don't permit the clamping level to fall below 5% - that is getting rather
115 * excessive.
116 *
117 * We make sure that the background writeout level is below the adjusted
118 * clamping level.
119 */
120static void
Christoph Lameterc24f21b2006-06-30 01:55:42 -0700121get_dirty_limits(long *pbackground, long *pdirty,
122 struct address_space *mapping)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
124 int background_ratio; /* Percentages */
125 int dirty_ratio;
126 int unmapped_ratio;
127 long background;
128 long dirty;
Chandra Seetharaman40c99aa2006-09-29 02:01:24 -0700129 unsigned long available_memory = vm_total_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 struct task_struct *tsk;
131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132#ifdef CONFIG_HIGHMEM
133 /*
134 * If this mapping can only allocate from low memory,
135 * we exclude high memory from our count.
136 */
137 if (mapping && !(mapping_gfp_mask(mapping) & __GFP_HIGHMEM))
138 available_memory -= totalhigh_pages;
139#endif
140
141
Christoph Lameterc24f21b2006-06-30 01:55:42 -0700142 unmapped_ratio = 100 - ((global_page_state(NR_FILE_MAPPED) +
143 global_page_state(NR_ANON_PAGES)) * 100) /
Chandra Seetharaman40c99aa2006-09-29 02:01:24 -0700144 vm_total_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146 dirty_ratio = vm_dirty_ratio;
147 if (dirty_ratio > unmapped_ratio / 2)
148 dirty_ratio = unmapped_ratio / 2;
149
150 if (dirty_ratio < 5)
151 dirty_ratio = 5;
152
153 background_ratio = dirty_background_ratio;
154 if (background_ratio >= dirty_ratio)
155 background_ratio = dirty_ratio / 2;
156
157 background = (background_ratio * available_memory) / 100;
158 dirty = (dirty_ratio * available_memory) / 100;
159 tsk = current;
160 if (tsk->flags & PF_LESS_THROTTLE || rt_task(tsk)) {
161 background += background / 4;
162 dirty += dirty / 4;
163 }
164 *pbackground = background;
165 *pdirty = dirty;
166}
167
168/*
169 * balance_dirty_pages() must be called by processes which are generating dirty
170 * data. It looks at the number of dirty pages in the machine and will force
171 * the caller to perform writeback if the system is over `vm_dirty_ratio'.
172 * If we're over `background_thresh' then pdflush is woken to perform some
173 * writeout.
174 */
175static void balance_dirty_pages(struct address_space *mapping)
176{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 long nr_reclaimable;
178 long background_thresh;
179 long dirty_thresh;
180 unsigned long pages_written = 0;
181 unsigned long write_chunk = sync_writeback_pages();
182
183 struct backing_dev_info *bdi = mapping->backing_dev_info;
184
185 for (;;) {
186 struct writeback_control wbc = {
187 .bdi = bdi,
188 .sync_mode = WB_SYNC_NONE,
189 .older_than_this = NULL,
190 .nr_to_write = write_chunk,
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700191 .range_cyclic = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 };
193
Christoph Lameterc24f21b2006-06-30 01:55:42 -0700194 get_dirty_limits(&background_thresh, &dirty_thresh, mapping);
195 nr_reclaimable = global_page_state(NR_FILE_DIRTY) +
196 global_page_state(NR_UNSTABLE_NFS);
197 if (nr_reclaimable + global_page_state(NR_WRITEBACK) <=
198 dirty_thresh)
199 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Andrew Mortone236a162006-01-18 17:42:26 -0800201 if (!dirty_exceeded)
202 dirty_exceeded = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
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);
Christoph Lameterc24f21b2006-06-30 01:55:42 -0700212 get_dirty_limits(&background_thresh,
213 &dirty_thresh, mapping);
214 nr_reclaimable = global_page_state(NR_FILE_DIRTY) +
215 global_page_state(NR_UNSTABLE_NFS);
216 if (nr_reclaimable +
217 global_page_state(NR_WRITEBACK)
218 <= dirty_thresh)
219 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 pages_written += write_chunk - wbc.nr_to_write;
221 if (pages_written >= write_chunk)
222 break; /* We've done our duty */
223 }
224 blk_congestion_wait(WRITE, HZ/10);
225 }
226
Christoph Lameterc24f21b2006-06-30 01:55:42 -0700227 if (nr_reclaimable + global_page_state(NR_WRITEBACK)
228 <= dirty_thresh && dirty_exceeded)
229 dirty_exceeded = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
231 if (writeback_in_progress(bdi))
232 return; /* pdflush is already working this queue */
233
234 /*
235 * In laptop mode, we wait until hitting the higher threshold before
236 * starting background writeout, and then write out all the way down
237 * to the lower threshold. So slow writers cause minimal disk activity.
238 *
239 * In normal mode, we start background writeout at the lower
240 * background_thresh, to keep the amount of dirty memory low.
241 */
242 if ((laptop_mode && pages_written) ||
243 (!laptop_mode && (nr_reclaimable > background_thresh)))
244 pdflush_operation(background_writeout, 0);
245}
246
Peter Zijlstraedc79b22006-09-25 23:30:58 -0700247void set_page_dirty_balance(struct page *page)
248{
249 if (set_page_dirty(page)) {
250 struct address_space *mapping = page_mapping(page);
251
252 if (mapping)
253 balance_dirty_pages_ratelimited(mapping);
254 }
255}
256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257/**
Andrew Mortonfa5a7342006-03-24 03:18:10 -0800258 * balance_dirty_pages_ratelimited_nr - balance dirty memory state
Martin Waitz67be2dd2005-05-01 08:59:26 -0700259 * @mapping: address_space which was dirtied
Martin Waitza5802902006-04-02 13:59:55 +0200260 * @nr_pages_dirtied: number of pages which the caller has just dirtied
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 *
262 * Processes which are dirtying memory should call in here once for each page
263 * which was newly dirtied. The function will periodically check the system's
264 * dirty state and will initiate writeback if needed.
265 *
266 * On really big machines, get_writeback_state is expensive, so try to avoid
267 * calling it too often (ratelimiting). But once we're over the dirty memory
268 * limit we decrease the ratelimiting by a lot, to prevent individual processes
269 * from overshooting the limit by (ratelimit_pages) each.
270 */
Andrew Mortonfa5a7342006-03-24 03:18:10 -0800271void balance_dirty_pages_ratelimited_nr(struct address_space *mapping,
272 unsigned long nr_pages_dirtied)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
Andrew Mortonfa5a7342006-03-24 03:18:10 -0800274 static DEFINE_PER_CPU(unsigned long, ratelimits) = 0;
275 unsigned long ratelimit;
276 unsigned long *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278 ratelimit = ratelimit_pages;
279 if (dirty_exceeded)
280 ratelimit = 8;
281
282 /*
283 * Check the rate limiting. Also, we do not want to throttle real-time
284 * tasks in balance_dirty_pages(). Period.
285 */
Andrew Mortonfa5a7342006-03-24 03:18:10 -0800286 preempt_disable();
287 p = &__get_cpu_var(ratelimits);
288 *p += nr_pages_dirtied;
289 if (unlikely(*p >= ratelimit)) {
290 *p = 0;
291 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 balance_dirty_pages(mapping);
293 return;
294 }
Andrew Mortonfa5a7342006-03-24 03:18:10 -0800295 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296}
Andrew Mortonfa5a7342006-03-24 03:18:10 -0800297EXPORT_SYMBOL(balance_dirty_pages_ratelimited_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
299void throttle_vm_writeout(void)
300{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 long background_thresh;
302 long dirty_thresh;
303
304 for ( ; ; ) {
Christoph Lameterc24f21b2006-06-30 01:55:42 -0700305 get_dirty_limits(&background_thresh, &dirty_thresh, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
307 /*
308 * Boost the allowable dirty threshold a bit for page
309 * allocators so they don't get DoS'ed by heavy writers
310 */
311 dirty_thresh += dirty_thresh / 10; /* wheeee... */
312
Christoph Lameterc24f21b2006-06-30 01:55:42 -0700313 if (global_page_state(NR_UNSTABLE_NFS) +
314 global_page_state(NR_WRITEBACK) <= dirty_thresh)
315 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 blk_congestion_wait(WRITE, HZ/10);
317 }
318}
319
320
321/*
322 * writeback at least _min_pages, and keep writing until the amount of dirty
323 * memory is less than the background threshold, or until we're all clean.
324 */
325static void background_writeout(unsigned long _min_pages)
326{
327 long min_pages = _min_pages;
328 struct writeback_control wbc = {
329 .bdi = NULL,
330 .sync_mode = WB_SYNC_NONE,
331 .older_than_this = NULL,
332 .nr_to_write = 0,
333 .nonblocking = 1,
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700334 .range_cyclic = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 };
336
337 for ( ; ; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 long background_thresh;
339 long dirty_thresh;
340
Christoph Lameterc24f21b2006-06-30 01:55:42 -0700341 get_dirty_limits(&background_thresh, &dirty_thresh, NULL);
342 if (global_page_state(NR_FILE_DIRTY) +
343 global_page_state(NR_UNSTABLE_NFS) < background_thresh
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 && min_pages <= 0)
345 break;
346 wbc.encountered_congestion = 0;
347 wbc.nr_to_write = MAX_WRITEBACK_PAGES;
348 wbc.pages_skipped = 0;
349 writeback_inodes(&wbc);
350 min_pages -= MAX_WRITEBACK_PAGES - wbc.nr_to_write;
351 if (wbc.nr_to_write > 0 || wbc.pages_skipped > 0) {
352 /* Wrote less than expected */
353 blk_congestion_wait(WRITE, HZ/10);
354 if (!wbc.encountered_congestion)
355 break;
356 }
357 }
358}
359
360/*
361 * Start writeback of `nr_pages' pages. If `nr_pages' is zero, write back
362 * the whole world. Returns 0 if a pdflush thread was dispatched. Returns
363 * -1 if all pdflush threads were busy.
364 */
Pekka J Enberg687a21c2005-06-28 20:44:55 -0700365int wakeup_pdflush(long nr_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
Christoph Lameterc24f21b2006-06-30 01:55:42 -0700367 if (nr_pages == 0)
368 nr_pages = global_page_state(NR_FILE_DIRTY) +
369 global_page_state(NR_UNSTABLE_NFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 return pdflush_operation(background_writeout, nr_pages);
371}
372
373static void wb_timer_fn(unsigned long unused);
374static void laptop_timer_fn(unsigned long unused);
375
Ingo Molnar8d06afa2005-09-09 13:10:40 -0700376static DEFINE_TIMER(wb_timer, wb_timer_fn, 0, 0);
377static DEFINE_TIMER(laptop_mode_wb_timer, laptop_timer_fn, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
379/*
380 * Periodic writeback of "old" data.
381 *
382 * Define "old": the first time one of an inode's pages is dirtied, we mark the
383 * dirtying-time in the inode's address_space. So this periodic writeback code
384 * just walks the superblock inode list, writing back any inodes which are
385 * older than a specific point in time.
386 *
Bart Samwelf6ef9432006-03-24 03:15:48 -0800387 * Try to run once per dirty_writeback_interval. But if a writeback event
388 * takes longer than a dirty_writeback_interval interval, then leave a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 * one-second gap.
390 *
391 * older_than_this takes precedence over nr_to_write. So we'll only write back
392 * all dirty pages if they are all attached to "old" mappings.
393 */
394static void wb_kupdate(unsigned long arg)
395{
396 unsigned long oldest_jif;
397 unsigned long start_jif;
398 unsigned long next_jif;
399 long nr_to_write;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 struct writeback_control wbc = {
401 .bdi = NULL,
402 .sync_mode = WB_SYNC_NONE,
403 .older_than_this = &oldest_jif,
404 .nr_to_write = 0,
405 .nonblocking = 1,
406 .for_kupdate = 1,
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700407 .range_cyclic = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 };
409
410 sync_supers();
411
Bart Samwelf6ef9432006-03-24 03:15:48 -0800412 oldest_jif = jiffies - dirty_expire_interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 start_jif = jiffies;
Bart Samwelf6ef9432006-03-24 03:15:48 -0800414 next_jif = start_jif + dirty_writeback_interval;
Christoph Lameterc24f21b2006-06-30 01:55:42 -0700415 nr_to_write = global_page_state(NR_FILE_DIRTY) +
416 global_page_state(NR_UNSTABLE_NFS) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 (inodes_stat.nr_inodes - inodes_stat.nr_unused);
418 while (nr_to_write > 0) {
419 wbc.encountered_congestion = 0;
420 wbc.nr_to_write = MAX_WRITEBACK_PAGES;
421 writeback_inodes(&wbc);
422 if (wbc.nr_to_write > 0) {
423 if (wbc.encountered_congestion)
424 blk_congestion_wait(WRITE, HZ/10);
425 else
426 break; /* All the old data is written */
427 }
428 nr_to_write -= MAX_WRITEBACK_PAGES - wbc.nr_to_write;
429 }
430 if (time_before(next_jif, jiffies + HZ))
431 next_jif = jiffies + HZ;
Bart Samwelf6ef9432006-03-24 03:15:48 -0800432 if (dirty_writeback_interval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 mod_timer(&wb_timer, next_jif);
434}
435
436/*
437 * sysctl handler for /proc/sys/vm/dirty_writeback_centisecs
438 */
439int dirty_writeback_centisecs_handler(ctl_table *table, int write,
440 struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
441{
Bart Samwelf6ef9432006-03-24 03:15:48 -0800442 proc_dointvec_userhz_jiffies(table, write, file, buffer, length, ppos);
443 if (dirty_writeback_interval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 mod_timer(&wb_timer,
Bart Samwelf6ef9432006-03-24 03:15:48 -0800445 jiffies + dirty_writeback_interval);
446 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 del_timer(&wb_timer);
448 }
449 return 0;
450}
451
452static void wb_timer_fn(unsigned long unused)
453{
454 if (pdflush_operation(wb_kupdate, 0) < 0)
455 mod_timer(&wb_timer, jiffies + HZ); /* delay 1 second */
456}
457
458static void laptop_flush(unsigned long unused)
459{
460 sys_sync();
461}
462
463static void laptop_timer_fn(unsigned long unused)
464{
465 pdflush_operation(laptop_flush, 0);
466}
467
468/*
469 * We've spun up the disk and we're in laptop mode: schedule writeback
470 * of all dirty data a few seconds from now. If the flush is already scheduled
471 * then push it back - the user is still using the disk.
472 */
473void laptop_io_completion(void)
474{
Bart Samweled5b43f2006-03-24 03:15:49 -0800475 mod_timer(&laptop_mode_wb_timer, jiffies + laptop_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476}
477
478/*
479 * We're in laptop mode and we've just synced. The sync's writes will have
480 * caused another writeback to be scheduled by laptop_io_completion.
481 * Nothing needs to be written back anymore, so we unschedule the writeback.
482 */
483void laptop_sync_completion(void)
484{
485 del_timer(&laptop_mode_wb_timer);
486}
487
488/*
489 * If ratelimit_pages is too high then we can get into dirty-data overload
490 * if a large number of processes all perform writes at the same time.
491 * If it is too low then SMP machines will call the (expensive)
492 * get_writeback_state too often.
493 *
494 * Here we set ratelimit_pages to a level which ensures that when all CPUs are
495 * dirtying in parallel, we cannot go more than 3% (1/32) over the dirty memory
496 * thresholds before writeback cuts in.
497 *
498 * But the limit should not be set too high. Because it also controls the
499 * amount of memory which the balance_dirty_pages() caller has to write back.
500 * If this is too large then the caller will block on the IO queue all the
501 * time. So limit it to four megabytes - the balance_dirty_pages() caller
502 * will write six megabyte chunks, max.
503 */
504
Chandra Seetharaman2d1d43f2006-09-29 02:01:25 -0700505void writeback_set_ratelimit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
Chandra Seetharaman40c99aa2006-09-29 02:01:24 -0700507 ratelimit_pages = vm_total_pages / (num_online_cpus() * 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 if (ratelimit_pages < 16)
509 ratelimit_pages = 16;
510 if (ratelimit_pages * PAGE_CACHE_SIZE > 4096 * 1024)
511 ratelimit_pages = (4096 * 1024) / PAGE_CACHE_SIZE;
512}
513
Chandra Seetharaman26c21432006-06-27 02:54:10 -0700514static int __cpuinit
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515ratelimit_handler(struct notifier_block *self, unsigned long u, void *v)
516{
Chandra Seetharaman2d1d43f2006-09-29 02:01:25 -0700517 writeback_set_ratelimit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 return 0;
519}
520
Chandra Seetharaman74b85f32006-06-27 02:54:09 -0700521static struct notifier_block __cpuinitdata ratelimit_nb = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 .notifier_call = ratelimit_handler,
523 .next = NULL,
524};
525
526/*
527 * If the machine has a large highmem:lowmem ratio then scale back the default
528 * dirty memory thresholds: allowing too much dirty highmem pins an excessive
529 * number of buffer_heads.
530 */
531void __init page_writeback_init(void)
532{
533 long buffer_pages = nr_free_buffer_pages();
534 long correction;
535
Chandra Seetharaman40c99aa2006-09-29 02:01:24 -0700536 correction = (100 * 4 * buffer_pages) / vm_total_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538 if (correction < 100) {
539 dirty_background_ratio *= correction;
540 dirty_background_ratio /= 100;
541 vm_dirty_ratio *= correction;
542 vm_dirty_ratio /= 100;
543
544 if (dirty_background_ratio <= 0)
545 dirty_background_ratio = 1;
546 if (vm_dirty_ratio <= 0)
547 vm_dirty_ratio = 1;
548 }
Bart Samwelf6ef9432006-03-24 03:15:48 -0800549 mod_timer(&wb_timer, jiffies + dirty_writeback_interval);
Chandra Seetharaman2d1d43f2006-09-29 02:01:25 -0700550 writeback_set_ratelimit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 register_cpu_notifier(&ratelimit_nb);
552}
553
554int do_writepages(struct address_space *mapping, struct writeback_control *wbc)
555{
Andrew Morton22905f72005-11-16 15:07:01 -0800556 int ret;
557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 if (wbc->nr_to_write <= 0)
559 return 0;
Andrew Morton22905f72005-11-16 15:07:01 -0800560 wbc->for_writepages = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 if (mapping->a_ops->writepages)
Peter Zijlstrad08b3852006-09-25 23:30:57 -0700562 ret = mapping->a_ops->writepages(mapping, wbc);
Andrew Morton22905f72005-11-16 15:07:01 -0800563 else
564 ret = generic_writepages(mapping, wbc);
565 wbc->for_writepages = 0;
566 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567}
568
569/**
570 * write_one_page - write out a single page and optionally wait on I/O
571 *
Martin Waitz67be2dd2005-05-01 08:59:26 -0700572 * @page: the page to write
573 * @wait: if true, wait on writeout
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 *
575 * The page must be locked by the caller and will be unlocked upon return.
576 *
577 * write_one_page() returns a negative error code if I/O failed.
578 */
579int write_one_page(struct page *page, int wait)
580{
581 struct address_space *mapping = page->mapping;
582 int ret = 0;
583 struct writeback_control wbc = {
584 .sync_mode = WB_SYNC_ALL,
585 .nr_to_write = 1,
586 };
587
588 BUG_ON(!PageLocked(page));
589
590 if (wait)
591 wait_on_page_writeback(page);
592
593 if (clear_page_dirty_for_io(page)) {
594 page_cache_get(page);
595 ret = mapping->a_ops->writepage(page, &wbc);
596 if (ret == 0 && wait) {
597 wait_on_page_writeback(page);
598 if (PageError(page))
599 ret = -EIO;
600 }
601 page_cache_release(page);
602 } else {
603 unlock_page(page);
604 }
605 return ret;
606}
607EXPORT_SYMBOL(write_one_page);
608
609/*
610 * For address_spaces which do not use buffers. Just tag the page as dirty in
611 * its radix tree.
612 *
613 * This is also used when a single buffer is being dirtied: we want to set the
614 * page dirty in that case, but not all the buffers. This is a "bottom-up"
615 * dirtying, whereas __set_page_dirty_buffers() is a "top-down" dirtying.
616 *
617 * Most callers have locked the page, which pins the address_space in memory.
618 * But zap_pte_range() does not lock the page, however in that case the
619 * mapping is pinned by the vma's ->vm_file reference.
620 *
621 * We take care to handle the case where the page was truncated from the
622 * mapping by re-checking page_mapping() insode tree_lock.
623 */
624int __set_page_dirty_nobuffers(struct page *page)
625{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 if (!TestSetPageDirty(page)) {
627 struct address_space *mapping = page_mapping(page);
628 struct address_space *mapping2;
629
630 if (mapping) {
631 write_lock_irq(&mapping->tree_lock);
632 mapping2 = page_mapping(page);
633 if (mapping2) { /* Race with truncate? */
634 BUG_ON(mapping2 != mapping);
635 if (mapping_cap_account_dirty(mapping))
Christoph Lameterb1e7a8f2006-06-30 01:55:39 -0700636 __inc_zone_page_state(page,
637 NR_FILE_DIRTY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 radix_tree_tag_set(&mapping->page_tree,
639 page_index(page), PAGECACHE_TAG_DIRTY);
640 }
641 write_unlock_irq(&mapping->tree_lock);
642 if (mapping->host) {
643 /* !PageAnon && !swapper_space */
644 __mark_inode_dirty(mapping->host,
645 I_DIRTY_PAGES);
646 }
647 }
Andrew Morton4741c9f2006-03-24 03:18:11 -0800648 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 }
Andrew Morton4741c9f2006-03-24 03:18:11 -0800650 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651}
652EXPORT_SYMBOL(__set_page_dirty_nobuffers);
653
654/*
655 * When a writepage implementation decides that it doesn't want to write this
656 * page for some reason, it should redirty the locked page via
657 * redirty_page_for_writepage() and it should then unlock the page and return 0
658 */
659int redirty_page_for_writepage(struct writeback_control *wbc, struct page *page)
660{
661 wbc->pages_skipped++;
662 return __set_page_dirty_nobuffers(page);
663}
664EXPORT_SYMBOL(redirty_page_for_writepage);
665
666/*
667 * If the mapping doesn't provide a set_page_dirty a_op, then
668 * just fall through and assume that it wants buffer_heads.
669 */
670int fastcall set_page_dirty(struct page *page)
671{
672 struct address_space *mapping = page_mapping(page);
673
674 if (likely(mapping)) {
675 int (*spd)(struct page *) = mapping->a_ops->set_page_dirty;
676 if (spd)
677 return (*spd)(page);
678 return __set_page_dirty_buffers(page);
679 }
Andrew Morton4741c9f2006-03-24 03:18:11 -0800680 if (!PageDirty(page)) {
681 if (!TestSetPageDirty(page))
682 return 1;
683 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 return 0;
685}
686EXPORT_SYMBOL(set_page_dirty);
687
688/*
689 * set_page_dirty() is racy if the caller has no reference against
690 * page->mapping->host, and if the page is unlocked. This is because another
691 * CPU could truncate the page off the mapping and then free the mapping.
692 *
693 * Usually, the page _is_ locked, or the caller is a user-space process which
694 * holds a reference on the inode by having an open file.
695 *
696 * In other cases, the page should be locked before running set_page_dirty().
697 */
698int set_page_dirty_lock(struct page *page)
699{
700 int ret;
701
Nick Piggindb376482006-09-25 23:31:24 -0700702 lock_page_nosync(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 ret = set_page_dirty(page);
704 unlock_page(page);
705 return ret;
706}
707EXPORT_SYMBOL(set_page_dirty_lock);
708
709/*
710 * Clear a page's dirty flag, while caring for dirty memory accounting.
711 * Returns true if the page was previously dirty.
712 */
713int test_clear_page_dirty(struct page *page)
714{
715 struct address_space *mapping = page_mapping(page);
716 unsigned long flags;
717
718 if (mapping) {
719 write_lock_irqsave(&mapping->tree_lock, flags);
720 if (TestClearPageDirty(page)) {
721 radix_tree_tag_clear(&mapping->page_tree,
722 page_index(page),
723 PAGECACHE_TAG_DIRTY);
Christoph Lameterb1e7a8f2006-06-30 01:55:39 -0700724 write_unlock_irqrestore(&mapping->tree_lock, flags);
Peter Zijlstrad08b3852006-09-25 23:30:57 -0700725 /*
726 * We can continue to use `mapping' here because the
727 * page is locked, which pins the address_space
728 */
729 if (mapping_cap_account_dirty(mapping)) {
730 page_mkclean(page);
731 dec_zone_page_state(page, NR_FILE_DIRTY);
732 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 return 1;
734 }
735 write_unlock_irqrestore(&mapping->tree_lock, flags);
736 return 0;
737 }
738 return TestClearPageDirty(page);
739}
740EXPORT_SYMBOL(test_clear_page_dirty);
741
742/*
743 * Clear a page's dirty flag, while caring for dirty memory accounting.
744 * Returns true if the page was previously dirty.
745 *
746 * This is for preparing to put the page under writeout. We leave the page
747 * tagged as dirty in the radix tree so that a concurrent write-for-sync
748 * can discover it via a PAGECACHE_TAG_DIRTY walk. The ->writepage
749 * implementation will run either set_page_writeback() or set_page_dirty(),
750 * at which stage we bring the page's dirty flag and radix-tree dirty tag
751 * back into sync.
752 *
753 * This incoherency between the page's dirty flag and radix-tree tag is
754 * unfortunate, but it only exists while the page is locked.
755 */
756int clear_page_dirty_for_io(struct page *page)
757{
758 struct address_space *mapping = page_mapping(page);
759
760 if (mapping) {
761 if (TestClearPageDirty(page)) {
Peter Zijlstrad08b3852006-09-25 23:30:57 -0700762 if (mapping_cap_account_dirty(mapping)) {
763 page_mkclean(page);
Christoph Lameterb1e7a8f2006-06-30 01:55:39 -0700764 dec_zone_page_state(page, NR_FILE_DIRTY);
Peter Zijlstrad08b3852006-09-25 23:30:57 -0700765 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 return 1;
767 }
768 return 0;
769 }
770 return TestClearPageDirty(page);
771}
Hans Reiser58bb01a2005-11-18 01:10:53 -0800772EXPORT_SYMBOL(clear_page_dirty_for_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
774int test_clear_page_writeback(struct page *page)
775{
776 struct address_space *mapping = page_mapping(page);
777 int ret;
778
779 if (mapping) {
780 unsigned long flags;
781
782 write_lock_irqsave(&mapping->tree_lock, flags);
783 ret = TestClearPageWriteback(page);
784 if (ret)
785 radix_tree_tag_clear(&mapping->page_tree,
786 page_index(page),
787 PAGECACHE_TAG_WRITEBACK);
788 write_unlock_irqrestore(&mapping->tree_lock, flags);
789 } else {
790 ret = TestClearPageWriteback(page);
791 }
792 return ret;
793}
794
795int test_set_page_writeback(struct page *page)
796{
797 struct address_space *mapping = page_mapping(page);
798 int ret;
799
800 if (mapping) {
801 unsigned long flags;
802
803 write_lock_irqsave(&mapping->tree_lock, flags);
804 ret = TestSetPageWriteback(page);
805 if (!ret)
806 radix_tree_tag_set(&mapping->page_tree,
807 page_index(page),
808 PAGECACHE_TAG_WRITEBACK);
809 if (!PageDirty(page))
810 radix_tree_tag_clear(&mapping->page_tree,
811 page_index(page),
812 PAGECACHE_TAG_DIRTY);
813 write_unlock_irqrestore(&mapping->tree_lock, flags);
814 } else {
815 ret = TestSetPageWriteback(page);
816 }
817 return ret;
818
819}
820EXPORT_SYMBOL(test_set_page_writeback);
821
822/*
Trond Myklebust275a0822006-08-22 20:06:24 -0400823 * Wakes up tasks that are being throttled due to writeback congestion
824 */
825void writeback_congestion_end(void)
826{
827 blk_congestion_end(WRITE);
828}
829EXPORT_SYMBOL(writeback_congestion_end);
830
831/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 * Return true if any of the pages in the mapping are marged with the
833 * passed tag.
834 */
835int mapping_tagged(struct address_space *mapping, int tag)
836{
837 unsigned long flags;
838 int ret;
839
840 read_lock_irqsave(&mapping->tree_lock, flags);
841 ret = radix_tree_tagged(&mapping->page_tree, tag);
842 read_unlock_irqrestore(&mapping->tree_lock, flags);
843 return ret;
844}
845EXPORT_SYMBOL(mapping_tagged);