blob: 3d3848fa6324ee30c8bfa4f39ac867808af84627 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Uwe Zeisbergerf30c2262006-10-03 23:01:26 +02002 * mm/page-writeback.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Copyright (C) 2002, Linus Torvalds.
Peter Zijlstra04fbfdc2007-10-16 23:25:50 -07005 * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * Contains functions related to writing back dirty pages at the
8 * address_space level.
9 *
10 * 10Apr2002 akpm@zip.com.au
11 * Initial version
12 */
13
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/spinlock.h>
17#include <linux/fs.h>
18#include <linux/mm.h>
19#include <linux/swap.h>
20#include <linux/slab.h>
21#include <linux/pagemap.h>
22#include <linux/writeback.h>
23#include <linux/init.h>
24#include <linux/backing-dev.h>
Andrew Morton55e829a2006-12-10 02:19:27 -080025#include <linux/task_io_accounting_ops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/blkdev.h>
27#include <linux/mpage.h>
Peter Zijlstrad08b3852006-09-25 23:30:57 -070028#include <linux/rmap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/percpu.h>
30#include <linux/notifier.h>
31#include <linux/smp.h>
32#include <linux/sysctl.h>
33#include <linux/cpu.h>
34#include <linux/syscalls.h>
David Howellscf9a2ae2006-08-29 19:05:54 +010035#include <linux/buffer_head.h>
David Howells811d7362006-08-29 19:06:09 +010036#include <linux/pagevec.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38/*
39 * The maximum number of pages to writeout in a single bdflush/kupdate
Joern Engel1c0eeaf2007-10-16 23:30:44 -070040 * operation. We do this so we don't hold I_SYNC against an inode for
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 * enormous amounts of time, which would block a userspace task which has
42 * been forced to throttle against that inode. Also, the code reevaluates
43 * the dirty each time it has written this many pages.
44 */
45#define MAX_WRITEBACK_PAGES 1024
46
47/*
48 * After a CPU has dirtied this many pages, balance_dirty_pages_ratelimited
49 * will look to see if it needs to force writeback or throttling.
50 */
51static long ratelimit_pages = 32;
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053/*
54 * When balance_dirty_pages decides that the caller needs to perform some
55 * non-background writeback, this is how many pages it will attempt to write.
56 * It should be somewhat larger than RATELIMIT_PAGES to ensure that reasonably
57 * large amounts of I/O are submitted.
58 */
59static inline long sync_writeback_pages(void)
60{
61 return ratelimit_pages + ratelimit_pages / 2;
62}
63
64/* The following parameters are exported via /proc/sys/vm */
65
66/*
67 * Start background writeback (via pdflush) at this percentage
68 */
Linus Torvalds07db59b2007-04-27 09:10:47 -070069int dirty_background_ratio = 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71/*
72 * The generator of dirty data starts writeback at this percentage
73 */
Linus Torvalds07db59b2007-04-27 09:10:47 -070074int vm_dirty_ratio = 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76/*
Coywolf Qi Huntfd5403c2006-04-10 22:54:35 -070077 * The interval between `kupdate'-style writebacks, in jiffies
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 */
Bart Samwelf6ef9432006-03-24 03:15:48 -080079int dirty_writeback_interval = 5 * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81/*
Coywolf Qi Huntfd5403c2006-04-10 22:54:35 -070082 * The longest number of jiffies for which data is allowed to remain dirty
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 */
Bart Samwelf6ef9432006-03-24 03:15:48 -080084int dirty_expire_interval = 30 * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86/*
87 * Flag that makes the machine dump writes/reads and block dirtyings.
88 */
89int block_dump;
90
91/*
Bart Samweled5b43f2006-03-24 03:15:49 -080092 * Flag that puts the machine in "laptop mode". Doubles as a timeout in jiffies:
93 * a full sync is triggered after this time elapses without any disk activity.
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 */
95int laptop_mode;
96
97EXPORT_SYMBOL(laptop_mode);
98
99/* End of sysctl-exported parameters */
100
101
102static void background_writeout(unsigned long _min_pages);
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104/*
Peter Zijlstra04fbfdc2007-10-16 23:25:50 -0700105 * Scale the writeback cache size proportional to the relative writeout speeds.
106 *
107 * We do this by keeping a floating proportion between BDIs, based on page
108 * writeback completions [end_page_writeback()]. Those devices that write out
109 * pages fastest will get the larger share, while the slower will get a smaller
110 * share.
111 *
112 * We use page writeout completions because we are interested in getting rid of
113 * dirty pages. Having them written out is the primary goal.
114 *
115 * We introduce a concept of time, a period over which we measure these events,
116 * because demand can/will vary over time. The length of this period itself is
117 * measured in page writeback completions.
118 *
119 */
120static struct prop_descriptor vm_completions;
Peter Zijlstra3e26c142007-10-16 23:25:50 -0700121static struct prop_descriptor vm_dirties;
Peter Zijlstra04fbfdc2007-10-16 23:25:50 -0700122
123static unsigned long determine_dirtyable_memory(void);
124
125/*
126 * couple the period to the dirty_ratio:
127 *
128 * period/2 ~ roundup_pow_of_two(dirty limit)
129 */
130static int calc_period_shift(void)
131{
132 unsigned long dirty_total;
133
134 dirty_total = (vm_dirty_ratio * determine_dirtyable_memory()) / 100;
135 return 2 + ilog2(dirty_total - 1);
136}
137
138/*
139 * update the period when the dirty ratio changes.
140 */
141int dirty_ratio_handler(struct ctl_table *table, int write,
142 struct file *filp, void __user *buffer, size_t *lenp,
143 loff_t *ppos)
144{
145 int old_ratio = vm_dirty_ratio;
146 int ret = proc_dointvec_minmax(table, write, filp, buffer, lenp, ppos);
147 if (ret == 0 && write && vm_dirty_ratio != old_ratio) {
148 int shift = calc_period_shift();
149 prop_change_shift(&vm_completions, shift);
Peter Zijlstra3e26c142007-10-16 23:25:50 -0700150 prop_change_shift(&vm_dirties, shift);
Peter Zijlstra04fbfdc2007-10-16 23:25:50 -0700151 }
152 return ret;
153}
154
155/*
156 * Increment the BDI's writeout completion count and the global writeout
157 * completion count. Called from test_clear_page_writeback().
158 */
159static inline void __bdi_writeout_inc(struct backing_dev_info *bdi)
160{
161 __prop_inc_percpu(&vm_completions, &bdi->completions);
162}
163
Peter Zijlstra3e26c142007-10-16 23:25:50 -0700164static inline void task_dirty_inc(struct task_struct *tsk)
165{
166 prop_inc_single(&vm_dirties, &tsk->dirties);
167}
168
Peter Zijlstra04fbfdc2007-10-16 23:25:50 -0700169/*
170 * Obtain an accurate fraction of the BDI's portion.
171 */
172static void bdi_writeout_fraction(struct backing_dev_info *bdi,
173 long *numerator, long *denominator)
174{
175 if (bdi_cap_writeback_dirty(bdi)) {
176 prop_fraction_percpu(&vm_completions, &bdi->completions,
177 numerator, denominator);
178 } else {
179 *numerator = 0;
180 *denominator = 1;
181 }
182}
183
184/*
185 * Clip the earned share of dirty pages to that which is actually available.
186 * This avoids exceeding the total dirty_limit when the floating averages
187 * fluctuate too quickly.
188 */
189static void
190clip_bdi_dirty_limit(struct backing_dev_info *bdi, long dirty, long *pbdi_dirty)
191{
192 long avail_dirty;
193
194 avail_dirty = dirty -
195 (global_page_state(NR_FILE_DIRTY) +
196 global_page_state(NR_WRITEBACK) +
197 global_page_state(NR_UNSTABLE_NFS));
198
199 if (avail_dirty < 0)
200 avail_dirty = 0;
201
202 avail_dirty += bdi_stat(bdi, BDI_RECLAIMABLE) +
203 bdi_stat(bdi, BDI_WRITEBACK);
204
205 *pbdi_dirty = min(*pbdi_dirty, avail_dirty);
206}
207
Peter Zijlstra3e26c142007-10-16 23:25:50 -0700208static inline void task_dirties_fraction(struct task_struct *tsk,
209 long *numerator, long *denominator)
210{
211 prop_fraction_single(&vm_dirties, &tsk->dirties,
212 numerator, denominator);
213}
214
215/*
216 * scale the dirty limit
217 *
218 * task specific dirty limit:
219 *
220 * dirty -= (dirty/8) * p_{t}
221 */
222void task_dirty_limit(struct task_struct *tsk, long *pdirty)
223{
224 long numerator, denominator;
225 long dirty = *pdirty;
226 u64 inv = dirty >> 3;
227
228 task_dirties_fraction(tsk, &numerator, &denominator);
229 inv *= numerator;
230 do_div(inv, denominator);
231
232 dirty -= inv;
233 if (dirty < *pdirty/2)
234 dirty = *pdirty/2;
235
236 *pdirty = dirty;
237}
238
Peter Zijlstra04fbfdc2007-10-16 23:25:50 -0700239/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 * Work out the current dirty-memory clamping and background writeout
241 * thresholds.
242 *
243 * The main aim here is to lower them aggressively if there is a lot of mapped
244 * memory around. To avoid stressing page reclaim with lots of unreclaimable
245 * pages. It is better to clamp down on writers than to start swapping, and
246 * performing lots of scanning.
247 *
248 * We only allow 1/2 of the currently-unmapped memory to be dirtied.
249 *
250 * We don't permit the clamping level to fall below 5% - that is getting rather
251 * excessive.
252 *
253 * We make sure that the background writeout level is below the adjusted
254 * clamping level.
255 */
Christoph Lameter1b424462007-05-06 14:48:59 -0700256
257static unsigned long highmem_dirtyable_memory(unsigned long total)
258{
259#ifdef CONFIG_HIGHMEM
260 int node;
261 unsigned long x = 0;
262
Lee Schermerhorn37b07e42007-10-16 01:25:39 -0700263 for_each_node_state(node, N_HIGH_MEMORY) {
Christoph Lameter1b424462007-05-06 14:48:59 -0700264 struct zone *z =
265 &NODE_DATA(node)->node_zones[ZONE_HIGHMEM];
266
267 x += zone_page_state(z, NR_FREE_PAGES)
268 + zone_page_state(z, NR_INACTIVE)
269 + zone_page_state(z, NR_ACTIVE);
270 }
271 /*
272 * Make sure that the number of highmem pages is never larger
273 * than the number of the total dirtyable memory. This can only
274 * occur in very strange VM situations but we want to make sure
275 * that this does not occur.
276 */
277 return min(x, total);
278#else
279 return 0;
280#endif
281}
282
283static unsigned long determine_dirtyable_memory(void)
284{
285 unsigned long x;
286
287 x = global_page_state(NR_FREE_PAGES)
288 + global_page_state(NR_INACTIVE)
289 + global_page_state(NR_ACTIVE);
290 x -= highmem_dirtyable_memory(x);
291 return x + 1; /* Ensure that we never return 0 */
292}
293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294static void
Peter Zijlstra04fbfdc2007-10-16 23:25:50 -0700295get_dirty_limits(long *pbackground, long *pdirty, long *pbdi_dirty,
296 struct backing_dev_info *bdi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
298 int background_ratio; /* Percentages */
299 int dirty_ratio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 long background;
301 long dirty;
Christoph Lameter1b424462007-05-06 14:48:59 -0700302 unsigned long available_memory = determine_dirtyable_memory();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 struct task_struct *tsk;
304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 dirty_ratio = vm_dirty_ratio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 if (dirty_ratio < 5)
307 dirty_ratio = 5;
308
309 background_ratio = dirty_background_ratio;
310 if (background_ratio >= dirty_ratio)
311 background_ratio = dirty_ratio / 2;
312
313 background = (background_ratio * available_memory) / 100;
314 dirty = (dirty_ratio * available_memory) / 100;
315 tsk = current;
316 if (tsk->flags & PF_LESS_THROTTLE || rt_task(tsk)) {
317 background += background / 4;
318 dirty += dirty / 4;
319 }
320 *pbackground = background;
321 *pdirty = dirty;
Peter Zijlstra04fbfdc2007-10-16 23:25:50 -0700322
323 if (bdi) {
324 u64 bdi_dirty = dirty;
325 long numerator, denominator;
326
327 /*
328 * Calculate this BDI's share of the dirty ratio.
329 */
330 bdi_writeout_fraction(bdi, &numerator, &denominator);
331
332 bdi_dirty *= numerator;
333 do_div(bdi_dirty, denominator);
334
335 *pbdi_dirty = bdi_dirty;
336 clip_bdi_dirty_limit(bdi, dirty, pbdi_dirty);
Peter Zijlstra3e26c142007-10-16 23:25:50 -0700337 task_dirty_limit(current, pbdi_dirty);
Peter Zijlstra04fbfdc2007-10-16 23:25:50 -0700338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339}
340
341/*
342 * balance_dirty_pages() must be called by processes which are generating dirty
343 * data. It looks at the number of dirty pages in the machine and will force
344 * the caller to perform writeback if the system is over `vm_dirty_ratio'.
345 * If we're over `background_thresh' then pdflush is woken to perform some
346 * writeout.
347 */
348static void balance_dirty_pages(struct address_space *mapping)
349{
Peter Zijlstra5fce25a2007-11-14 16:59:15 -0800350 long nr_reclaimable, bdi_nr_reclaimable;
351 long nr_writeback, bdi_nr_writeback;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 long background_thresh;
353 long dirty_thresh;
Peter Zijlstra04fbfdc2007-10-16 23:25:50 -0700354 long bdi_thresh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 unsigned long pages_written = 0;
356 unsigned long write_chunk = sync_writeback_pages();
357
358 struct backing_dev_info *bdi = mapping->backing_dev_info;
359
360 for (;;) {
361 struct writeback_control wbc = {
362 .bdi = bdi,
363 .sync_mode = WB_SYNC_NONE,
364 .older_than_this = NULL,
365 .nr_to_write = write_chunk,
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700366 .range_cyclic = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 };
368
Peter Zijlstra04fbfdc2007-10-16 23:25:50 -0700369 get_dirty_limits(&background_thresh, &dirty_thresh,
370 &bdi_thresh, bdi);
Peter Zijlstra5fce25a2007-11-14 16:59:15 -0800371
372 nr_reclaimable = global_page_state(NR_FILE_DIRTY) +
373 global_page_state(NR_UNSTABLE_NFS);
374 nr_writeback = global_page_state(NR_WRITEBACK);
375
Peter Zijlstra04fbfdc2007-10-16 23:25:50 -0700376 bdi_nr_reclaimable = bdi_stat(bdi, BDI_RECLAIMABLE);
377 bdi_nr_writeback = bdi_stat(bdi, BDI_WRITEBACK);
Peter Zijlstra5fce25a2007-11-14 16:59:15 -0800378
Peter Zijlstra04fbfdc2007-10-16 23:25:50 -0700379 if (bdi_nr_reclaimable + bdi_nr_writeback <= bdi_thresh)
380 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Peter Zijlstra5fce25a2007-11-14 16:59:15 -0800382 /*
383 * Throttle it only when the background writeback cannot
384 * catch-up. This avoids (excessively) small writeouts
385 * when the bdi limits are ramping up.
386 */
387 if (nr_reclaimable + nr_writeback <
388 (background_thresh + dirty_thresh) / 2)
389 break;
390
Peter Zijlstra04fbfdc2007-10-16 23:25:50 -0700391 if (!bdi->dirty_exceeded)
392 bdi->dirty_exceeded = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
394 /* Note: nr_reclaimable denotes nr_dirty + nr_unstable.
395 * Unstable writes are a feature of certain networked
396 * filesystems (i.e. NFS) in which data may have been
397 * written to the server's write cache, but has not yet
398 * been flushed to permanent storage.
399 */
Peter Zijlstra04fbfdc2007-10-16 23:25:50 -0700400 if (bdi_nr_reclaimable) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 writeback_inodes(&wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 pages_written += write_chunk - wbc.nr_to_write;
Peter Zijlstra04fbfdc2007-10-16 23:25:50 -0700403 get_dirty_limits(&background_thresh, &dirty_thresh,
404 &bdi_thresh, bdi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 }
Peter Zijlstra04fbfdc2007-10-16 23:25:50 -0700406
407 /*
408 * In order to avoid the stacked BDI deadlock we need
409 * to ensure we accurately count the 'dirty' pages when
410 * the threshold is low.
411 *
412 * Otherwise it would be possible to get thresh+n pages
413 * reported dirty, even though there are thresh-m pages
414 * actually dirty; with m+n sitting in the percpu
415 * deltas.
416 */
417 if (bdi_thresh < 2*bdi_stat_error(bdi)) {
418 bdi_nr_reclaimable = bdi_stat_sum(bdi, BDI_RECLAIMABLE);
419 bdi_nr_writeback = bdi_stat_sum(bdi, BDI_WRITEBACK);
420 } else if (bdi_nr_reclaimable) {
421 bdi_nr_reclaimable = bdi_stat(bdi, BDI_RECLAIMABLE);
422 bdi_nr_writeback = bdi_stat(bdi, BDI_WRITEBACK);
423 }
424
425 if (bdi_nr_reclaimable + bdi_nr_writeback <= bdi_thresh)
426 break;
427 if (pages_written >= write_chunk)
428 break; /* We've done our duty */
429
Andrew Morton3fcfab12006-10-19 23:28:16 -0700430 congestion_wait(WRITE, HZ/10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 }
432
Peter Zijlstra04fbfdc2007-10-16 23:25:50 -0700433 if (bdi_nr_reclaimable + bdi_nr_writeback < bdi_thresh &&
434 bdi->dirty_exceeded)
435 bdi->dirty_exceeded = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
437 if (writeback_in_progress(bdi))
438 return; /* pdflush is already working this queue */
439
440 /*
441 * In laptop mode, we wait until hitting the higher threshold before
442 * starting background writeout, and then write out all the way down
443 * to the lower threshold. So slow writers cause minimal disk activity.
444 *
445 * In normal mode, we start background writeout at the lower
446 * background_thresh, to keep the amount of dirty memory low.
447 */
448 if ((laptop_mode && pages_written) ||
Peter Zijlstra04fbfdc2007-10-16 23:25:50 -0700449 (!laptop_mode && (global_page_state(NR_FILE_DIRTY)
450 + global_page_state(NR_UNSTABLE_NFS)
451 > background_thresh)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 pdflush_operation(background_writeout, 0);
453}
454
Peter Zijlstraa200ee12007-10-08 18:54:37 +0200455void set_page_dirty_balance(struct page *page, int page_mkwrite)
Peter Zijlstraedc79b22006-09-25 23:30:58 -0700456{
Peter Zijlstraa200ee12007-10-08 18:54:37 +0200457 if (set_page_dirty(page) || page_mkwrite) {
Peter Zijlstraedc79b22006-09-25 23:30:58 -0700458 struct address_space *mapping = page_mapping(page);
459
460 if (mapping)
461 balance_dirty_pages_ratelimited(mapping);
462 }
463}
464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465/**
Andrew Mortonfa5a7342006-03-24 03:18:10 -0800466 * balance_dirty_pages_ratelimited_nr - balance dirty memory state
Martin Waitz67be2dd2005-05-01 08:59:26 -0700467 * @mapping: address_space which was dirtied
Martin Waitza5802902006-04-02 13:59:55 +0200468 * @nr_pages_dirtied: number of pages which the caller has just dirtied
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 *
470 * Processes which are dirtying memory should call in here once for each page
471 * which was newly dirtied. The function will periodically check the system's
472 * dirty state and will initiate writeback if needed.
473 *
474 * On really big machines, get_writeback_state is expensive, so try to avoid
475 * calling it too often (ratelimiting). But once we're over the dirty memory
476 * limit we decrease the ratelimiting by a lot, to prevent individual processes
477 * from overshooting the limit by (ratelimit_pages) each.
478 */
Andrew Mortonfa5a7342006-03-24 03:18:10 -0800479void balance_dirty_pages_ratelimited_nr(struct address_space *mapping,
480 unsigned long nr_pages_dirtied)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
Andrew Mortonfa5a7342006-03-24 03:18:10 -0800482 static DEFINE_PER_CPU(unsigned long, ratelimits) = 0;
483 unsigned long ratelimit;
484 unsigned long *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486 ratelimit = ratelimit_pages;
Peter Zijlstra04fbfdc2007-10-16 23:25:50 -0700487 if (mapping->backing_dev_info->dirty_exceeded)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 ratelimit = 8;
489
490 /*
491 * Check the rate limiting. Also, we do not want to throttle real-time
492 * tasks in balance_dirty_pages(). Period.
493 */
Andrew Mortonfa5a7342006-03-24 03:18:10 -0800494 preempt_disable();
495 p = &__get_cpu_var(ratelimits);
496 *p += nr_pages_dirtied;
497 if (unlikely(*p >= ratelimit)) {
498 *p = 0;
499 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 balance_dirty_pages(mapping);
501 return;
502 }
Andrew Mortonfa5a7342006-03-24 03:18:10 -0800503 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504}
Andrew Mortonfa5a7342006-03-24 03:18:10 -0800505EXPORT_SYMBOL(balance_dirty_pages_ratelimited_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
Andrew Morton232ea4d2007-02-28 20:13:21 -0800507void throttle_vm_writeout(gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 long background_thresh;
510 long dirty_thresh;
511
512 for ( ; ; ) {
Peter Zijlstra04fbfdc2007-10-16 23:25:50 -0700513 get_dirty_limits(&background_thresh, &dirty_thresh, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515 /*
516 * Boost the allowable dirty threshold a bit for page
517 * allocators so they don't get DoS'ed by heavy writers
518 */
519 dirty_thresh += dirty_thresh / 10; /* wheeee... */
520
Christoph Lameterc24f21b2006-06-30 01:55:42 -0700521 if (global_page_state(NR_UNSTABLE_NFS) +
522 global_page_state(NR_WRITEBACK) <= dirty_thresh)
523 break;
Andrew Morton3fcfab12006-10-19 23:28:16 -0700524 congestion_wait(WRITE, HZ/10);
Fengguang Wu369f2382007-10-16 23:30:45 -0700525
526 /*
527 * The caller might hold locks which can prevent IO completion
528 * or progress in the filesystem. So we cannot just sit here
529 * waiting for IO to complete.
530 */
531 if ((gfp_mask & (__GFP_FS|__GFP_IO)) != (__GFP_FS|__GFP_IO))
532 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 }
534}
535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536/*
537 * writeback at least _min_pages, and keep writing until the amount of dirty
538 * memory is less than the background threshold, or until we're all clean.
539 */
540static void background_writeout(unsigned long _min_pages)
541{
542 long min_pages = _min_pages;
543 struct writeback_control wbc = {
544 .bdi = NULL,
545 .sync_mode = WB_SYNC_NONE,
546 .older_than_this = NULL,
547 .nr_to_write = 0,
548 .nonblocking = 1,
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700549 .range_cyclic = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 };
551
552 for ( ; ; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 long background_thresh;
554 long dirty_thresh;
555
Peter Zijlstra04fbfdc2007-10-16 23:25:50 -0700556 get_dirty_limits(&background_thresh, &dirty_thresh, NULL, NULL);
Christoph Lameterc24f21b2006-06-30 01:55:42 -0700557 if (global_page_state(NR_FILE_DIRTY) +
558 global_page_state(NR_UNSTABLE_NFS) < background_thresh
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 && min_pages <= 0)
560 break;
561 wbc.encountered_congestion = 0;
562 wbc.nr_to_write = MAX_WRITEBACK_PAGES;
563 wbc.pages_skipped = 0;
564 writeback_inodes(&wbc);
565 min_pages -= MAX_WRITEBACK_PAGES - wbc.nr_to_write;
566 if (wbc.nr_to_write > 0 || wbc.pages_skipped > 0) {
567 /* Wrote less than expected */
Linus Torvaldsc23f72c2008-01-14 21:21:29 -0800568 congestion_wait(WRITE, HZ/10);
569 if (!wbc.encountered_congestion)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 break;
571 }
572 }
573}
574
575/*
576 * Start writeback of `nr_pages' pages. If `nr_pages' is zero, write back
577 * the whole world. Returns 0 if a pdflush thread was dispatched. Returns
578 * -1 if all pdflush threads were busy.
579 */
Pekka J Enberg687a21c2005-06-28 20:44:55 -0700580int wakeup_pdflush(long nr_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
Christoph Lameterc24f21b2006-06-30 01:55:42 -0700582 if (nr_pages == 0)
583 nr_pages = global_page_state(NR_FILE_DIRTY) +
584 global_page_state(NR_UNSTABLE_NFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 return pdflush_operation(background_writeout, nr_pages);
586}
587
588static void wb_timer_fn(unsigned long unused);
589static void laptop_timer_fn(unsigned long unused);
590
Ingo Molnar8d06afa2005-09-09 13:10:40 -0700591static DEFINE_TIMER(wb_timer, wb_timer_fn, 0, 0);
592static DEFINE_TIMER(laptop_mode_wb_timer, laptop_timer_fn, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594/*
595 * Periodic writeback of "old" data.
596 *
597 * Define "old": the first time one of an inode's pages is dirtied, we mark the
598 * dirtying-time in the inode's address_space. So this periodic writeback code
599 * just walks the superblock inode list, writing back any inodes which are
600 * older than a specific point in time.
601 *
Bart Samwelf6ef9432006-03-24 03:15:48 -0800602 * Try to run once per dirty_writeback_interval. But if a writeback event
603 * takes longer than a dirty_writeback_interval interval, then leave a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 * one-second gap.
605 *
606 * older_than_this takes precedence over nr_to_write. So we'll only write back
607 * all dirty pages if they are all attached to "old" mappings.
608 */
609static void wb_kupdate(unsigned long arg)
610{
611 unsigned long oldest_jif;
612 unsigned long start_jif;
613 unsigned long next_jif;
614 long nr_to_write;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 struct writeback_control wbc = {
616 .bdi = NULL,
617 .sync_mode = WB_SYNC_NONE,
618 .older_than_this = &oldest_jif,
619 .nr_to_write = 0,
620 .nonblocking = 1,
621 .for_kupdate = 1,
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700622 .range_cyclic = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 };
624
625 sync_supers();
626
Bart Samwelf6ef9432006-03-24 03:15:48 -0800627 oldest_jif = jiffies - dirty_expire_interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 start_jif = jiffies;
Bart Samwelf6ef9432006-03-24 03:15:48 -0800629 next_jif = start_jif + dirty_writeback_interval;
Christoph Lameterc24f21b2006-06-30 01:55:42 -0700630 nr_to_write = global_page_state(NR_FILE_DIRTY) +
631 global_page_state(NR_UNSTABLE_NFS) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 (inodes_stat.nr_inodes - inodes_stat.nr_unused);
633 while (nr_to_write > 0) {
634 wbc.encountered_congestion = 0;
635 wbc.nr_to_write = MAX_WRITEBACK_PAGES;
636 writeback_inodes(&wbc);
637 if (wbc.nr_to_write > 0) {
Linus Torvaldsc23f72c2008-01-14 21:21:29 -0800638 if (wbc.encountered_congestion)
Andrew Morton3fcfab12006-10-19 23:28:16 -0700639 congestion_wait(WRITE, HZ/10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 else
641 break; /* All the old data is written */
642 }
643 nr_to_write -= MAX_WRITEBACK_PAGES - wbc.nr_to_write;
644 }
645 if (time_before(next_jif, jiffies + HZ))
646 next_jif = jiffies + HZ;
Bart Samwelf6ef9432006-03-24 03:15:48 -0800647 if (dirty_writeback_interval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 mod_timer(&wb_timer, next_jif);
649}
650
651/*
652 * sysctl handler for /proc/sys/vm/dirty_writeback_centisecs
653 */
654int dirty_writeback_centisecs_handler(ctl_table *table, int write,
Andrew Morton3e733f02007-07-15 23:41:05 -0700655 struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
Bart Samwelf6ef9432006-03-24 03:15:48 -0800657 proc_dointvec_userhz_jiffies(table, write, file, buffer, length, ppos);
Andrew Morton3e733f02007-07-15 23:41:05 -0700658 if (dirty_writeback_interval)
659 mod_timer(&wb_timer, jiffies + dirty_writeback_interval);
660 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 del_timer(&wb_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 return 0;
663}
664
665static void wb_timer_fn(unsigned long unused)
666{
667 if (pdflush_operation(wb_kupdate, 0) < 0)
668 mod_timer(&wb_timer, jiffies + HZ); /* delay 1 second */
669}
670
671static void laptop_flush(unsigned long unused)
672{
673 sys_sync();
674}
675
676static void laptop_timer_fn(unsigned long unused)
677{
678 pdflush_operation(laptop_flush, 0);
679}
680
681/*
682 * We've spun up the disk and we're in laptop mode: schedule writeback
683 * of all dirty data a few seconds from now. If the flush is already scheduled
684 * then push it back - the user is still using the disk.
685 */
686void laptop_io_completion(void)
687{
Bart Samweled5b43f2006-03-24 03:15:49 -0800688 mod_timer(&laptop_mode_wb_timer, jiffies + laptop_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689}
690
691/*
692 * We're in laptop mode and we've just synced. The sync's writes will have
693 * caused another writeback to be scheduled by laptop_io_completion.
694 * Nothing needs to be written back anymore, so we unschedule the writeback.
695 */
696void laptop_sync_completion(void)
697{
698 del_timer(&laptop_mode_wb_timer);
699}
700
701/*
702 * If ratelimit_pages is too high then we can get into dirty-data overload
703 * if a large number of processes all perform writes at the same time.
704 * If it is too low then SMP machines will call the (expensive)
705 * get_writeback_state too often.
706 *
707 * Here we set ratelimit_pages to a level which ensures that when all CPUs are
708 * dirtying in parallel, we cannot go more than 3% (1/32) over the dirty memory
709 * thresholds before writeback cuts in.
710 *
711 * But the limit should not be set too high. Because it also controls the
712 * amount of memory which the balance_dirty_pages() caller has to write back.
713 * If this is too large then the caller will block on the IO queue all the
714 * time. So limit it to four megabytes - the balance_dirty_pages() caller
715 * will write six megabyte chunks, max.
716 */
717
Chandra Seetharaman2d1d43f2006-09-29 02:01:25 -0700718void writeback_set_ratelimit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
Chandra Seetharaman40c99aa2006-09-29 02:01:24 -0700720 ratelimit_pages = vm_total_pages / (num_online_cpus() * 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 if (ratelimit_pages < 16)
722 ratelimit_pages = 16;
723 if (ratelimit_pages * PAGE_CACHE_SIZE > 4096 * 1024)
724 ratelimit_pages = (4096 * 1024) / PAGE_CACHE_SIZE;
725}
726
Chandra Seetharaman26c21432006-06-27 02:54:10 -0700727static int __cpuinit
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728ratelimit_handler(struct notifier_block *self, unsigned long u, void *v)
729{
Chandra Seetharaman2d1d43f2006-09-29 02:01:25 -0700730 writeback_set_ratelimit();
Paul E. McKenneyaa0f0302007-02-10 01:46:37 -0800731 return NOTIFY_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732}
733
Chandra Seetharaman74b85f32006-06-27 02:54:09 -0700734static struct notifier_block __cpuinitdata ratelimit_nb = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 .notifier_call = ratelimit_handler,
736 .next = NULL,
737};
738
739/*
Linus Torvaldsdc6e29d2007-01-29 16:37:38 -0800740 * Called early on to tune the page writeback dirty limits.
741 *
742 * We used to scale dirty pages according to how total memory
743 * related to pages that could be allocated for buffers (by
744 * comparing nr_free_buffer_pages() to vm_total_pages.
745 *
746 * However, that was when we used "dirty_ratio" to scale with
747 * all memory, and we don't do that any more. "dirty_ratio"
748 * is now applied to total non-HIGHPAGE memory (by subtracting
749 * totalhigh_pages from vm_total_pages), and as such we can't
750 * get into the old insane situation any more where we had
751 * large amounts of dirty pages compared to a small amount of
752 * non-HIGHMEM memory.
753 *
754 * But we might still want to scale the dirty_ratio by how
755 * much memory the box has..
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 */
757void __init page_writeback_init(void)
758{
Peter Zijlstra04fbfdc2007-10-16 23:25:50 -0700759 int shift;
760
Bart Samwelf6ef9432006-03-24 03:15:48 -0800761 mod_timer(&wb_timer, jiffies + dirty_writeback_interval);
Chandra Seetharaman2d1d43f2006-09-29 02:01:25 -0700762 writeback_set_ratelimit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 register_cpu_notifier(&ratelimit_nb);
Peter Zijlstra04fbfdc2007-10-16 23:25:50 -0700764
765 shift = calc_period_shift();
766 prop_descriptor_init(&vm_completions, shift);
Peter Zijlstra3e26c142007-10-16 23:25:50 -0700767 prop_descriptor_init(&vm_dirties, shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768}
769
David Howells811d7362006-08-29 19:06:09 +0100770/**
Miklos Szeredi0ea97182007-05-10 22:22:51 -0700771 * write_cache_pages - walk the list of dirty pages of the given address space and write all of them.
David Howells811d7362006-08-29 19:06:09 +0100772 * @mapping: address space structure to write
773 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
Miklos Szeredi0ea97182007-05-10 22:22:51 -0700774 * @writepage: function called for each page
775 * @data: data passed to writepage function
David Howells811d7362006-08-29 19:06:09 +0100776 *
Miklos Szeredi0ea97182007-05-10 22:22:51 -0700777 * If a page is already under I/O, write_cache_pages() skips it, even
David Howells811d7362006-08-29 19:06:09 +0100778 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
779 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
780 * and msync() need to guarantee that all the data which was dirty at the time
781 * the call was made get new I/O started against them. If wbc->sync_mode is
782 * WB_SYNC_ALL then we were called for data integrity and we must wait for
783 * existing IO to complete.
David Howells811d7362006-08-29 19:06:09 +0100784 */
Miklos Szeredi0ea97182007-05-10 22:22:51 -0700785int write_cache_pages(struct address_space *mapping,
786 struct writeback_control *wbc, writepage_t writepage,
787 void *data)
David Howells811d7362006-08-29 19:06:09 +0100788{
789 struct backing_dev_info *bdi = mapping->backing_dev_info;
790 int ret = 0;
791 int done = 0;
David Howells811d7362006-08-29 19:06:09 +0100792 struct pagevec pvec;
793 int nr_pages;
794 pgoff_t index;
795 pgoff_t end; /* Inclusive */
796 int scanned = 0;
797 int range_whole = 0;
798
799 if (wbc->nonblocking && bdi_write_congested(bdi)) {
800 wbc->encountered_congestion = 1;
801 return 0;
802 }
803
David Howells811d7362006-08-29 19:06:09 +0100804 pagevec_init(&pvec, 0);
805 if (wbc->range_cyclic) {
806 index = mapping->writeback_index; /* Start from prev offset */
807 end = -1;
808 } else {
809 index = wbc->range_start >> PAGE_CACHE_SHIFT;
810 end = wbc->range_end >> PAGE_CACHE_SHIFT;
811 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
812 range_whole = 1;
813 scanned = 1;
814 }
815retry:
816 while (!done && (index <= end) &&
817 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
818 PAGECACHE_TAG_DIRTY,
819 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
820 unsigned i;
821
822 scanned = 1;
823 for (i = 0; i < nr_pages; i++) {
824 struct page *page = pvec.pages[i];
825
826 /*
827 * At this point we hold neither mapping->tree_lock nor
828 * lock on the page itself: the page may be truncated or
829 * invalidated (changing page->mapping to NULL), or even
830 * swizzled back from swapper_space to tmpfs file
831 * mapping
832 */
833 lock_page(page);
834
835 if (unlikely(page->mapping != mapping)) {
836 unlock_page(page);
837 continue;
838 }
839
840 if (!wbc->range_cyclic && page->index > end) {
841 done = 1;
842 unlock_page(page);
843 continue;
844 }
845
846 if (wbc->sync_mode != WB_SYNC_NONE)
847 wait_on_page_writeback(page);
848
849 if (PageWriteback(page) ||
850 !clear_page_dirty_for_io(page)) {
851 unlock_page(page);
852 continue;
853 }
854
Miklos Szeredi0ea97182007-05-10 22:22:51 -0700855 ret = (*writepage)(page, wbc, data);
David Howells811d7362006-08-29 19:06:09 +0100856
Andrew Mortone4230032007-10-16 23:26:02 -0700857 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
David Howells811d7362006-08-29 19:06:09 +0100858 unlock_page(page);
Andrew Mortone4230032007-10-16 23:26:02 -0700859 ret = 0;
860 }
David Howells811d7362006-08-29 19:06:09 +0100861 if (ret || (--(wbc->nr_to_write) <= 0))
862 done = 1;
863 if (wbc->nonblocking && bdi_write_congested(bdi)) {
864 wbc->encountered_congestion = 1;
865 done = 1;
866 }
867 }
868 pagevec_release(&pvec);
869 cond_resched();
870 }
871 if (!scanned && !done) {
872 /*
873 * We hit the last page and there is more work to be done: wrap
874 * back to the start of the file
875 */
876 scanned = 1;
877 index = 0;
878 goto retry;
879 }
880 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
881 mapping->writeback_index = index;
882 return ret;
883}
Miklos Szeredi0ea97182007-05-10 22:22:51 -0700884EXPORT_SYMBOL(write_cache_pages);
885
886/*
887 * Function used by generic_writepages to call the real writepage
888 * function and set the mapping flags on error
889 */
890static int __writepage(struct page *page, struct writeback_control *wbc,
891 void *data)
892{
893 struct address_space *mapping = data;
894 int ret = mapping->a_ops->writepage(page, wbc);
895 mapping_set_error(mapping, ret);
896 return ret;
897}
898
899/**
900 * generic_writepages - walk the list of dirty pages of the given address space and writepage() all of them.
901 * @mapping: address space structure to write
902 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
903 *
904 * This is a library function, which implements the writepages()
905 * address_space_operation.
906 */
907int generic_writepages(struct address_space *mapping,
908 struct writeback_control *wbc)
909{
910 /* deal with chardevs and other special file */
911 if (!mapping->a_ops->writepage)
912 return 0;
913
914 return write_cache_pages(mapping, wbc, __writepage, mapping);
915}
David Howells811d7362006-08-29 19:06:09 +0100916
917EXPORT_SYMBOL(generic_writepages);
918
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919int do_writepages(struct address_space *mapping, struct writeback_control *wbc)
920{
Andrew Morton22905f72005-11-16 15:07:01 -0800921 int ret;
922
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 if (wbc->nr_to_write <= 0)
924 return 0;
Andrew Morton22905f72005-11-16 15:07:01 -0800925 wbc->for_writepages = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 if (mapping->a_ops->writepages)
Peter Zijlstrad08b3852006-09-25 23:30:57 -0700927 ret = mapping->a_ops->writepages(mapping, wbc);
Andrew Morton22905f72005-11-16 15:07:01 -0800928 else
929 ret = generic_writepages(mapping, wbc);
930 wbc->for_writepages = 0;
931 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932}
933
934/**
935 * write_one_page - write out a single page and optionally wait on I/O
Martin Waitz67be2dd2005-05-01 08:59:26 -0700936 * @page: the page to write
937 * @wait: if true, wait on writeout
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 *
939 * The page must be locked by the caller and will be unlocked upon return.
940 *
941 * write_one_page() returns a negative error code if I/O failed.
942 */
943int write_one_page(struct page *page, int wait)
944{
945 struct address_space *mapping = page->mapping;
946 int ret = 0;
947 struct writeback_control wbc = {
948 .sync_mode = WB_SYNC_ALL,
949 .nr_to_write = 1,
950 };
951
952 BUG_ON(!PageLocked(page));
953
954 if (wait)
955 wait_on_page_writeback(page);
956
957 if (clear_page_dirty_for_io(page)) {
958 page_cache_get(page);
959 ret = mapping->a_ops->writepage(page, &wbc);
960 if (ret == 0 && wait) {
961 wait_on_page_writeback(page);
962 if (PageError(page))
963 ret = -EIO;
964 }
965 page_cache_release(page);
966 } else {
967 unlock_page(page);
968 }
969 return ret;
970}
971EXPORT_SYMBOL(write_one_page);
972
973/*
Ken Chen76719322007-02-10 01:43:15 -0800974 * For address_spaces which do not use buffers nor write back.
975 */
976int __set_page_dirty_no_writeback(struct page *page)
977{
978 if (!PageDirty(page))
979 SetPageDirty(page);
980 return 0;
981}
982
983/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 * For address_spaces which do not use buffers. Just tag the page as dirty in
985 * its radix tree.
986 *
987 * This is also used when a single buffer is being dirtied: we want to set the
988 * page dirty in that case, but not all the buffers. This is a "bottom-up"
989 * dirtying, whereas __set_page_dirty_buffers() is a "top-down" dirtying.
990 *
991 * Most callers have locked the page, which pins the address_space in memory.
992 * But zap_pte_range() does not lock the page, however in that case the
993 * mapping is pinned by the vma's ->vm_file reference.
994 *
995 * We take care to handle the case where the page was truncated from the
Simon Arlott183ff222007-10-20 01:27:18 +0200996 * mapping by re-checking page_mapping() inside tree_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 */
998int __set_page_dirty_nobuffers(struct page *page)
999{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 if (!TestSetPageDirty(page)) {
1001 struct address_space *mapping = page_mapping(page);
1002 struct address_space *mapping2;
1003
Andrew Morton8c085402006-12-10 02:19:24 -08001004 if (!mapping)
1005 return 1;
1006
1007 write_lock_irq(&mapping->tree_lock);
1008 mapping2 = page_mapping(page);
1009 if (mapping2) { /* Race with truncate? */
1010 BUG_ON(mapping2 != mapping);
Nick Piggin787d2212007-07-17 04:03:34 -07001011 WARN_ON_ONCE(!PagePrivate(page) && !PageUptodate(page));
Andrew Morton55e829a2006-12-10 02:19:27 -08001012 if (mapping_cap_account_dirty(mapping)) {
Andrew Morton8c085402006-12-10 02:19:24 -08001013 __inc_zone_page_state(page, NR_FILE_DIRTY);
Peter Zijlstrac9e51e42007-10-16 23:25:47 -07001014 __inc_bdi_stat(mapping->backing_dev_info,
1015 BDI_RECLAIMABLE);
Andrew Morton55e829a2006-12-10 02:19:27 -08001016 task_io_account_write(PAGE_CACHE_SIZE);
1017 }
Andrew Morton8c085402006-12-10 02:19:24 -08001018 radix_tree_tag_set(&mapping->page_tree,
1019 page_index(page), PAGECACHE_TAG_DIRTY);
1020 }
1021 write_unlock_irq(&mapping->tree_lock);
1022 if (mapping->host) {
1023 /* !PageAnon && !swapper_space */
1024 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 }
Andrew Morton4741c9f2006-03-24 03:18:11 -08001026 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 }
Andrew Morton4741c9f2006-03-24 03:18:11 -08001028 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029}
1030EXPORT_SYMBOL(__set_page_dirty_nobuffers);
1031
1032/*
1033 * When a writepage implementation decides that it doesn't want to write this
1034 * page for some reason, it should redirty the locked page via
1035 * redirty_page_for_writepage() and it should then unlock the page and return 0
1036 */
1037int redirty_page_for_writepage(struct writeback_control *wbc, struct page *page)
1038{
1039 wbc->pages_skipped++;
1040 return __set_page_dirty_nobuffers(page);
1041}
1042EXPORT_SYMBOL(redirty_page_for_writepage);
1043
1044/*
1045 * If the mapping doesn't provide a set_page_dirty a_op, then
1046 * just fall through and assume that it wants buffer_heads.
1047 */
Peter Zijlstra3e26c142007-10-16 23:25:50 -07001048static int __set_page_dirty(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049{
1050 struct address_space *mapping = page_mapping(page);
1051
1052 if (likely(mapping)) {
1053 int (*spd)(struct page *) = mapping->a_ops->set_page_dirty;
David Howells93614012006-09-30 20:45:40 +02001054#ifdef CONFIG_BLOCK
1055 if (!spd)
1056 spd = __set_page_dirty_buffers;
1057#endif
1058 return (*spd)(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 }
Andrew Morton4741c9f2006-03-24 03:18:11 -08001060 if (!PageDirty(page)) {
1061 if (!TestSetPageDirty(page))
1062 return 1;
1063 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 return 0;
1065}
Peter Zijlstra3e26c142007-10-16 23:25:50 -07001066
1067int fastcall set_page_dirty(struct page *page)
1068{
1069 int ret = __set_page_dirty(page);
1070 if (ret)
1071 task_dirty_inc(current);
1072 return ret;
1073}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074EXPORT_SYMBOL(set_page_dirty);
1075
1076/*
1077 * set_page_dirty() is racy if the caller has no reference against
1078 * page->mapping->host, and if the page is unlocked. This is because another
1079 * CPU could truncate the page off the mapping and then free the mapping.
1080 *
1081 * Usually, the page _is_ locked, or the caller is a user-space process which
1082 * holds a reference on the inode by having an open file.
1083 *
1084 * In other cases, the page should be locked before running set_page_dirty().
1085 */
1086int set_page_dirty_lock(struct page *page)
1087{
1088 int ret;
1089
Nick Piggindb376482006-09-25 23:31:24 -07001090 lock_page_nosync(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 ret = set_page_dirty(page);
1092 unlock_page(page);
1093 return ret;
1094}
1095EXPORT_SYMBOL(set_page_dirty_lock);
1096
1097/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 * Clear a page's dirty flag, while caring for dirty memory accounting.
1099 * Returns true if the page was previously dirty.
1100 *
1101 * This is for preparing to put the page under writeout. We leave the page
1102 * tagged as dirty in the radix tree so that a concurrent write-for-sync
1103 * can discover it via a PAGECACHE_TAG_DIRTY walk. The ->writepage
1104 * implementation will run either set_page_writeback() or set_page_dirty(),
1105 * at which stage we bring the page's dirty flag and radix-tree dirty tag
1106 * back into sync.
1107 *
1108 * This incoherency between the page's dirty flag and radix-tree tag is
1109 * unfortunate, but it only exists while the page is locked.
1110 */
1111int clear_page_dirty_for_io(struct page *page)
1112{
1113 struct address_space *mapping = page_mapping(page);
1114
Nick Piggin79352892007-07-19 01:47:22 -07001115 BUG_ON(!PageLocked(page));
1116
Fengguang Wufe3cba12007-07-19 01:48:07 -07001117 ClearPageReclaim(page);
Linus Torvalds7658cc22006-12-29 10:00:58 -08001118 if (mapping && mapping_cap_account_dirty(mapping)) {
1119 /*
1120 * Yes, Virginia, this is indeed insane.
1121 *
1122 * We use this sequence to make sure that
1123 * (a) we account for dirty stats properly
1124 * (b) we tell the low-level filesystem to
1125 * mark the whole page dirty if it was
1126 * dirty in a pagetable. Only to then
1127 * (c) clean the page again and return 1 to
1128 * cause the writeback.
1129 *
1130 * This way we avoid all nasty races with the
1131 * dirty bit in multiple places and clearing
1132 * them concurrently from different threads.
1133 *
1134 * Note! Normally the "set_page_dirty(page)"
1135 * has no effect on the actual dirty bit - since
1136 * that will already usually be set. But we
1137 * need the side effects, and it can help us
1138 * avoid races.
1139 *
1140 * We basically use the page "master dirty bit"
1141 * as a serialization point for all the different
1142 * threads doing their things.
Linus Torvalds7658cc22006-12-29 10:00:58 -08001143 */
1144 if (page_mkclean(page))
1145 set_page_dirty(page);
Nick Piggin79352892007-07-19 01:47:22 -07001146 /*
1147 * We carefully synchronise fault handlers against
1148 * installing a dirty pte and marking the page dirty
1149 * at this point. We do this by having them hold the
1150 * page lock at some point after installing their
1151 * pte, but before marking the page dirty.
1152 * Pages are always locked coming in here, so we get
1153 * the desired exclusion. See mm/memory.c:do_wp_page()
1154 * for more comments.
1155 */
Linus Torvalds7658cc22006-12-29 10:00:58 -08001156 if (TestClearPageDirty(page)) {
Andrew Morton8c085402006-12-10 02:19:24 -08001157 dec_zone_page_state(page, NR_FILE_DIRTY);
Peter Zijlstrac9e51e42007-10-16 23:25:47 -07001158 dec_bdi_stat(mapping->backing_dev_info,
1159 BDI_RECLAIMABLE);
Linus Torvalds7658cc22006-12-29 10:00:58 -08001160 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 }
Linus Torvalds7658cc22006-12-29 10:00:58 -08001162 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 }
Linus Torvalds7658cc22006-12-29 10:00:58 -08001164 return TestClearPageDirty(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165}
Hans Reiser58bb01a2005-11-18 01:10:53 -08001166EXPORT_SYMBOL(clear_page_dirty_for_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
1168int test_clear_page_writeback(struct page *page)
1169{
1170 struct address_space *mapping = page_mapping(page);
1171 int ret;
1172
1173 if (mapping) {
Peter Zijlstra69cb51d2007-10-16 23:25:48 -07001174 struct backing_dev_info *bdi = mapping->backing_dev_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 unsigned long flags;
1176
1177 write_lock_irqsave(&mapping->tree_lock, flags);
1178 ret = TestClearPageWriteback(page);
Peter Zijlstra69cb51d2007-10-16 23:25:48 -07001179 if (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 radix_tree_tag_clear(&mapping->page_tree,
1181 page_index(page),
1182 PAGECACHE_TAG_WRITEBACK);
Peter Zijlstra04fbfdc2007-10-16 23:25:50 -07001183 if (bdi_cap_writeback_dirty(bdi)) {
Peter Zijlstra69cb51d2007-10-16 23:25:48 -07001184 __dec_bdi_stat(bdi, BDI_WRITEBACK);
Peter Zijlstra04fbfdc2007-10-16 23:25:50 -07001185 __bdi_writeout_inc(bdi);
1186 }
Peter Zijlstra69cb51d2007-10-16 23:25:48 -07001187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 write_unlock_irqrestore(&mapping->tree_lock, flags);
1189 } else {
1190 ret = TestClearPageWriteback(page);
1191 }
Andrew Mortond688abf2007-07-19 01:49:17 -07001192 if (ret)
1193 dec_zone_page_state(page, NR_WRITEBACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 return ret;
1195}
1196
1197int test_set_page_writeback(struct page *page)
1198{
1199 struct address_space *mapping = page_mapping(page);
1200 int ret;
1201
1202 if (mapping) {
Peter Zijlstra69cb51d2007-10-16 23:25:48 -07001203 struct backing_dev_info *bdi = mapping->backing_dev_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 unsigned long flags;
1205
1206 write_lock_irqsave(&mapping->tree_lock, flags);
1207 ret = TestSetPageWriteback(page);
Peter Zijlstra69cb51d2007-10-16 23:25:48 -07001208 if (!ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 radix_tree_tag_set(&mapping->page_tree,
1210 page_index(page),
1211 PAGECACHE_TAG_WRITEBACK);
Peter Zijlstra69cb51d2007-10-16 23:25:48 -07001212 if (bdi_cap_writeback_dirty(bdi))
1213 __inc_bdi_stat(bdi, BDI_WRITEBACK);
1214 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 if (!PageDirty(page))
1216 radix_tree_tag_clear(&mapping->page_tree,
1217 page_index(page),
1218 PAGECACHE_TAG_DIRTY);
1219 write_unlock_irqrestore(&mapping->tree_lock, flags);
1220 } else {
1221 ret = TestSetPageWriteback(page);
1222 }
Andrew Mortond688abf2007-07-19 01:49:17 -07001223 if (!ret)
1224 inc_zone_page_state(page, NR_WRITEBACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 return ret;
1226
1227}
1228EXPORT_SYMBOL(test_set_page_writeback);
1229
1230/*
Nick Piggin00128182007-10-16 01:24:40 -07001231 * Return true if any of the pages in the mapping are marked with the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 * passed tag.
1233 */
1234int mapping_tagged(struct address_space *mapping, int tag)
1235{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 int ret;
Nick Piggin00128182007-10-16 01:24:40 -07001237 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 ret = radix_tree_tagged(&mapping->page_tree, tag);
Nick Piggin00128182007-10-16 01:24:40 -07001239 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 return ret;
1241}
1242EXPORT_SYMBOL(mapping_tagged);