blob: bcdbbf6c4a85e727de84a6df9f5cb02958fffe44 [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
40 * operation. We do this so we don't hold I_LOCK against an inode for
41 * 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;
300 int unmapped_ratio;
301 long background;
302 long dirty;
Christoph Lameter1b424462007-05-06 14:48:59 -0700303 unsigned long available_memory = determine_dirtyable_memory();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 struct task_struct *tsk;
305
Christoph Lameterc24f21b2006-06-30 01:55:42 -0700306 unmapped_ratio = 100 - ((global_page_state(NR_FILE_MAPPED) +
307 global_page_state(NR_ANON_PAGES)) * 100) /
Christoph Lameter1b424462007-05-06 14:48:59 -0700308 available_memory;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
310 dirty_ratio = vm_dirty_ratio;
311 if (dirty_ratio > unmapped_ratio / 2)
312 dirty_ratio = unmapped_ratio / 2;
313
314 if (dirty_ratio < 5)
315 dirty_ratio = 5;
316
317 background_ratio = dirty_background_ratio;
318 if (background_ratio >= dirty_ratio)
319 background_ratio = dirty_ratio / 2;
320
321 background = (background_ratio * available_memory) / 100;
322 dirty = (dirty_ratio * available_memory) / 100;
323 tsk = current;
324 if (tsk->flags & PF_LESS_THROTTLE || rt_task(tsk)) {
325 background += background / 4;
326 dirty += dirty / 4;
327 }
328 *pbackground = background;
329 *pdirty = dirty;
Peter Zijlstra04fbfdc2007-10-16 23:25:50 -0700330
331 if (bdi) {
332 u64 bdi_dirty = dirty;
333 long numerator, denominator;
334
335 /*
336 * Calculate this BDI's share of the dirty ratio.
337 */
338 bdi_writeout_fraction(bdi, &numerator, &denominator);
339
340 bdi_dirty *= numerator;
341 do_div(bdi_dirty, denominator);
342
343 *pbdi_dirty = bdi_dirty;
344 clip_bdi_dirty_limit(bdi, dirty, pbdi_dirty);
Peter Zijlstra3e26c142007-10-16 23:25:50 -0700345 task_dirty_limit(current, pbdi_dirty);
Peter Zijlstra04fbfdc2007-10-16 23:25:50 -0700346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347}
348
349/*
350 * balance_dirty_pages() must be called by processes which are generating dirty
351 * data. It looks at the number of dirty pages in the machine and will force
352 * the caller to perform writeback if the system is over `vm_dirty_ratio'.
353 * If we're over `background_thresh' then pdflush is woken to perform some
354 * writeout.
355 */
356static void balance_dirty_pages(struct address_space *mapping)
357{
Peter Zijlstra04fbfdc2007-10-16 23:25:50 -0700358 long bdi_nr_reclaimable;
359 long bdi_nr_writeback;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 long background_thresh;
361 long dirty_thresh;
Peter Zijlstra04fbfdc2007-10-16 23:25:50 -0700362 long bdi_thresh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 unsigned long pages_written = 0;
364 unsigned long write_chunk = sync_writeback_pages();
365
366 struct backing_dev_info *bdi = mapping->backing_dev_info;
367
368 for (;;) {
369 struct writeback_control wbc = {
370 .bdi = bdi,
371 .sync_mode = WB_SYNC_NONE,
372 .older_than_this = NULL,
373 .nr_to_write = write_chunk,
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700374 .range_cyclic = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 };
376
Peter Zijlstra04fbfdc2007-10-16 23:25:50 -0700377 get_dirty_limits(&background_thresh, &dirty_thresh,
378 &bdi_thresh, bdi);
379 bdi_nr_reclaimable = bdi_stat(bdi, BDI_RECLAIMABLE);
380 bdi_nr_writeback = bdi_stat(bdi, BDI_WRITEBACK);
381 if (bdi_nr_reclaimable + bdi_nr_writeback <= bdi_thresh)
382 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Peter Zijlstra04fbfdc2007-10-16 23:25:50 -0700384 if (!bdi->dirty_exceeded)
385 bdi->dirty_exceeded = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387 /* Note: nr_reclaimable denotes nr_dirty + nr_unstable.
388 * Unstable writes are a feature of certain networked
389 * filesystems (i.e. NFS) in which data may have been
390 * written to the server's write cache, but has not yet
391 * been flushed to permanent storage.
392 */
Peter Zijlstra04fbfdc2007-10-16 23:25:50 -0700393 if (bdi_nr_reclaimable) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 writeback_inodes(&wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 pages_written += write_chunk - wbc.nr_to_write;
Peter Zijlstra04fbfdc2007-10-16 23:25:50 -0700396 get_dirty_limits(&background_thresh, &dirty_thresh,
397 &bdi_thresh, bdi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 }
Peter Zijlstra04fbfdc2007-10-16 23:25:50 -0700399
400 /*
401 * In order to avoid the stacked BDI deadlock we need
402 * to ensure we accurately count the 'dirty' pages when
403 * the threshold is low.
404 *
405 * Otherwise it would be possible to get thresh+n pages
406 * reported dirty, even though there are thresh-m pages
407 * actually dirty; with m+n sitting in the percpu
408 * deltas.
409 */
410 if (bdi_thresh < 2*bdi_stat_error(bdi)) {
411 bdi_nr_reclaimable = bdi_stat_sum(bdi, BDI_RECLAIMABLE);
412 bdi_nr_writeback = bdi_stat_sum(bdi, BDI_WRITEBACK);
413 } else if (bdi_nr_reclaimable) {
414 bdi_nr_reclaimable = bdi_stat(bdi, BDI_RECLAIMABLE);
415 bdi_nr_writeback = bdi_stat(bdi, BDI_WRITEBACK);
416 }
417
418 if (bdi_nr_reclaimable + bdi_nr_writeback <= bdi_thresh)
419 break;
420 if (pages_written >= write_chunk)
421 break; /* We've done our duty */
422
Andrew Morton3fcfab12006-10-19 23:28:16 -0700423 congestion_wait(WRITE, HZ/10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 }
425
Peter Zijlstra04fbfdc2007-10-16 23:25:50 -0700426 if (bdi_nr_reclaimable + bdi_nr_writeback < bdi_thresh &&
427 bdi->dirty_exceeded)
428 bdi->dirty_exceeded = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430 if (writeback_in_progress(bdi))
431 return; /* pdflush is already working this queue */
432
433 /*
434 * In laptop mode, we wait until hitting the higher threshold before
435 * starting background writeout, and then write out all the way down
436 * to the lower threshold. So slow writers cause minimal disk activity.
437 *
438 * In normal mode, we start background writeout at the lower
439 * background_thresh, to keep the amount of dirty memory low.
440 */
441 if ((laptop_mode && pages_written) ||
Peter Zijlstra04fbfdc2007-10-16 23:25:50 -0700442 (!laptop_mode && (global_page_state(NR_FILE_DIRTY)
443 + global_page_state(NR_UNSTABLE_NFS)
444 > background_thresh)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 pdflush_operation(background_writeout, 0);
446}
447
Peter Zijlstraa200ee12007-10-08 18:54:37 +0200448void set_page_dirty_balance(struct page *page, int page_mkwrite)
Peter Zijlstraedc79b22006-09-25 23:30:58 -0700449{
Peter Zijlstraa200ee12007-10-08 18:54:37 +0200450 if (set_page_dirty(page) || page_mkwrite) {
Peter Zijlstraedc79b22006-09-25 23:30:58 -0700451 struct address_space *mapping = page_mapping(page);
452
453 if (mapping)
454 balance_dirty_pages_ratelimited(mapping);
455 }
456}
457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458/**
Andrew Mortonfa5a7342006-03-24 03:18:10 -0800459 * balance_dirty_pages_ratelimited_nr - balance dirty memory state
Martin Waitz67be2dd2005-05-01 08:59:26 -0700460 * @mapping: address_space which was dirtied
Martin Waitza5802902006-04-02 13:59:55 +0200461 * @nr_pages_dirtied: number of pages which the caller has just dirtied
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 *
463 * Processes which are dirtying memory should call in here once for each page
464 * which was newly dirtied. The function will periodically check the system's
465 * dirty state and will initiate writeback if needed.
466 *
467 * On really big machines, get_writeback_state is expensive, so try to avoid
468 * calling it too often (ratelimiting). But once we're over the dirty memory
469 * limit we decrease the ratelimiting by a lot, to prevent individual processes
470 * from overshooting the limit by (ratelimit_pages) each.
471 */
Andrew Mortonfa5a7342006-03-24 03:18:10 -0800472void balance_dirty_pages_ratelimited_nr(struct address_space *mapping,
473 unsigned long nr_pages_dirtied)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
Andrew Mortonfa5a7342006-03-24 03:18:10 -0800475 static DEFINE_PER_CPU(unsigned long, ratelimits) = 0;
476 unsigned long ratelimit;
477 unsigned long *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
479 ratelimit = ratelimit_pages;
Peter Zijlstra04fbfdc2007-10-16 23:25:50 -0700480 if (mapping->backing_dev_info->dirty_exceeded)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 ratelimit = 8;
482
483 /*
484 * Check the rate limiting. Also, we do not want to throttle real-time
485 * tasks in balance_dirty_pages(). Period.
486 */
Andrew Mortonfa5a7342006-03-24 03:18:10 -0800487 preempt_disable();
488 p = &__get_cpu_var(ratelimits);
489 *p += nr_pages_dirtied;
490 if (unlikely(*p >= ratelimit)) {
491 *p = 0;
492 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 balance_dirty_pages(mapping);
494 return;
495 }
Andrew Mortonfa5a7342006-03-24 03:18:10 -0800496 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497}
Andrew Mortonfa5a7342006-03-24 03:18:10 -0800498EXPORT_SYMBOL(balance_dirty_pages_ratelimited_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Andrew Morton232ea4d2007-02-28 20:13:21 -0800500void throttle_vm_writeout(gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 long background_thresh;
503 long dirty_thresh;
504
Andrew Morton232ea4d2007-02-28 20:13:21 -0800505 if ((gfp_mask & (__GFP_FS|__GFP_IO)) != (__GFP_FS|__GFP_IO)) {
506 /*
507 * The caller might hold locks which can prevent IO completion
508 * or progress in the filesystem. So we cannot just sit here
509 * waiting for IO to complete.
510 */
511 congestion_wait(WRITE, HZ/10);
512 return;
513 }
514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 for ( ; ; ) {
Peter Zijlstra04fbfdc2007-10-16 23:25:50 -0700516 get_dirty_limits(&background_thresh, &dirty_thresh, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
518 /*
519 * Boost the allowable dirty threshold a bit for page
520 * allocators so they don't get DoS'ed by heavy writers
521 */
522 dirty_thresh += dirty_thresh / 10; /* wheeee... */
523
Christoph Lameterc24f21b2006-06-30 01:55:42 -0700524 if (global_page_state(NR_UNSTABLE_NFS) +
525 global_page_state(NR_WRITEBACK) <= dirty_thresh)
526 break;
Andrew Morton3fcfab12006-10-19 23:28:16 -0700527 congestion_wait(WRITE, HZ/10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 }
529}
530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531/*
532 * writeback at least _min_pages, and keep writing until the amount of dirty
533 * memory is less than the background threshold, or until we're all clean.
534 */
535static void background_writeout(unsigned long _min_pages)
536{
537 long min_pages = _min_pages;
538 struct writeback_control wbc = {
539 .bdi = NULL,
540 .sync_mode = WB_SYNC_NONE,
541 .older_than_this = NULL,
542 .nr_to_write = 0,
543 .nonblocking = 1,
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700544 .range_cyclic = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 };
546
547 for ( ; ; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 long background_thresh;
549 long dirty_thresh;
550
Peter Zijlstra04fbfdc2007-10-16 23:25:50 -0700551 get_dirty_limits(&background_thresh, &dirty_thresh, NULL, NULL);
Christoph Lameterc24f21b2006-06-30 01:55:42 -0700552 if (global_page_state(NR_FILE_DIRTY) +
553 global_page_state(NR_UNSTABLE_NFS) < background_thresh
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 && min_pages <= 0)
555 break;
Fengguang Wu2e6883b2007-10-16 23:30:43 -0700556 wbc.more_io = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 wbc.encountered_congestion = 0;
558 wbc.nr_to_write = MAX_WRITEBACK_PAGES;
559 wbc.pages_skipped = 0;
560 writeback_inodes(&wbc);
561 min_pages -= MAX_WRITEBACK_PAGES - wbc.nr_to_write;
562 if (wbc.nr_to_write > 0 || wbc.pages_skipped > 0) {
563 /* Wrote less than expected */
Fengguang Wu2e6883b2007-10-16 23:30:43 -0700564 if (wbc.encountered_congestion || wbc.more_io)
565 congestion_wait(WRITE, HZ/10);
566 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 break;
568 }
569 }
570}
571
572/*
573 * Start writeback of `nr_pages' pages. If `nr_pages' is zero, write back
574 * the whole world. Returns 0 if a pdflush thread was dispatched. Returns
575 * -1 if all pdflush threads were busy.
576 */
Pekka J Enberg687a21c2005-06-28 20:44:55 -0700577int wakeup_pdflush(long nr_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
Christoph Lameterc24f21b2006-06-30 01:55:42 -0700579 if (nr_pages == 0)
580 nr_pages = global_page_state(NR_FILE_DIRTY) +
581 global_page_state(NR_UNSTABLE_NFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 return pdflush_operation(background_writeout, nr_pages);
583}
584
585static void wb_timer_fn(unsigned long unused);
586static void laptop_timer_fn(unsigned long unused);
587
Ingo Molnar8d06afa2005-09-09 13:10:40 -0700588static DEFINE_TIMER(wb_timer, wb_timer_fn, 0, 0);
589static DEFINE_TIMER(laptop_mode_wb_timer, laptop_timer_fn, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
591/*
592 * Periodic writeback of "old" data.
593 *
594 * Define "old": the first time one of an inode's pages is dirtied, we mark the
595 * dirtying-time in the inode's address_space. So this periodic writeback code
596 * just walks the superblock inode list, writing back any inodes which are
597 * older than a specific point in time.
598 *
Bart Samwelf6ef9432006-03-24 03:15:48 -0800599 * Try to run once per dirty_writeback_interval. But if a writeback event
600 * takes longer than a dirty_writeback_interval interval, then leave a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 * one-second gap.
602 *
603 * older_than_this takes precedence over nr_to_write. So we'll only write back
604 * all dirty pages if they are all attached to "old" mappings.
605 */
606static void wb_kupdate(unsigned long arg)
607{
608 unsigned long oldest_jif;
609 unsigned long start_jif;
610 unsigned long next_jif;
611 long nr_to_write;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 struct writeback_control wbc = {
613 .bdi = NULL,
614 .sync_mode = WB_SYNC_NONE,
615 .older_than_this = &oldest_jif,
616 .nr_to_write = 0,
617 .nonblocking = 1,
618 .for_kupdate = 1,
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700619 .range_cyclic = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 };
621
622 sync_supers();
623
Bart Samwelf6ef9432006-03-24 03:15:48 -0800624 oldest_jif = jiffies - dirty_expire_interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 start_jif = jiffies;
Bart Samwelf6ef9432006-03-24 03:15:48 -0800626 next_jif = start_jif + dirty_writeback_interval;
Christoph Lameterc24f21b2006-06-30 01:55:42 -0700627 nr_to_write = global_page_state(NR_FILE_DIRTY) +
628 global_page_state(NR_UNSTABLE_NFS) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 (inodes_stat.nr_inodes - inodes_stat.nr_unused);
630 while (nr_to_write > 0) {
Fengguang Wu2e6883b2007-10-16 23:30:43 -0700631 wbc.more_io = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 wbc.encountered_congestion = 0;
633 wbc.nr_to_write = MAX_WRITEBACK_PAGES;
634 writeback_inodes(&wbc);
635 if (wbc.nr_to_write > 0) {
Fengguang Wu2e6883b2007-10-16 23:30:43 -0700636 if (wbc.encountered_congestion || wbc.more_io)
Andrew Morton3fcfab12006-10-19 23:28:16 -0700637 congestion_wait(WRITE, HZ/10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 else
639 break; /* All the old data is written */
640 }
641 nr_to_write -= MAX_WRITEBACK_PAGES - wbc.nr_to_write;
642 }
643 if (time_before(next_jif, jiffies + HZ))
644 next_jif = jiffies + HZ;
Bart Samwelf6ef9432006-03-24 03:15:48 -0800645 if (dirty_writeback_interval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 mod_timer(&wb_timer, next_jif);
647}
648
649/*
650 * sysctl handler for /proc/sys/vm/dirty_writeback_centisecs
651 */
652int dirty_writeback_centisecs_handler(ctl_table *table, int write,
Andrew Morton3e733f02007-07-15 23:41:05 -0700653 struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654{
Bart Samwelf6ef9432006-03-24 03:15:48 -0800655 proc_dointvec_userhz_jiffies(table, write, file, buffer, length, ppos);
Andrew Morton3e733f02007-07-15 23:41:05 -0700656 if (dirty_writeback_interval)
657 mod_timer(&wb_timer, jiffies + dirty_writeback_interval);
658 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 del_timer(&wb_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 return 0;
661}
662
663static void wb_timer_fn(unsigned long unused)
664{
665 if (pdflush_operation(wb_kupdate, 0) < 0)
666 mod_timer(&wb_timer, jiffies + HZ); /* delay 1 second */
667}
668
669static void laptop_flush(unsigned long unused)
670{
671 sys_sync();
672}
673
674static void laptop_timer_fn(unsigned long unused)
675{
676 pdflush_operation(laptop_flush, 0);
677}
678
679/*
680 * We've spun up the disk and we're in laptop mode: schedule writeback
681 * of all dirty data a few seconds from now. If the flush is already scheduled
682 * then push it back - the user is still using the disk.
683 */
684void laptop_io_completion(void)
685{
Bart Samweled5b43f2006-03-24 03:15:49 -0800686 mod_timer(&laptop_mode_wb_timer, jiffies + laptop_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687}
688
689/*
690 * We're in laptop mode and we've just synced. The sync's writes will have
691 * caused another writeback to be scheduled by laptop_io_completion.
692 * Nothing needs to be written back anymore, so we unschedule the writeback.
693 */
694void laptop_sync_completion(void)
695{
696 del_timer(&laptop_mode_wb_timer);
697}
698
699/*
700 * If ratelimit_pages is too high then we can get into dirty-data overload
701 * if a large number of processes all perform writes at the same time.
702 * If it is too low then SMP machines will call the (expensive)
703 * get_writeback_state too often.
704 *
705 * Here we set ratelimit_pages to a level which ensures that when all CPUs are
706 * dirtying in parallel, we cannot go more than 3% (1/32) over the dirty memory
707 * thresholds before writeback cuts in.
708 *
709 * But the limit should not be set too high. Because it also controls the
710 * amount of memory which the balance_dirty_pages() caller has to write back.
711 * If this is too large then the caller will block on the IO queue all the
712 * time. So limit it to four megabytes - the balance_dirty_pages() caller
713 * will write six megabyte chunks, max.
714 */
715
Chandra Seetharaman2d1d43f2006-09-29 02:01:25 -0700716void writeback_set_ratelimit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
Chandra Seetharaman40c99aa2006-09-29 02:01:24 -0700718 ratelimit_pages = vm_total_pages / (num_online_cpus() * 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 if (ratelimit_pages < 16)
720 ratelimit_pages = 16;
721 if (ratelimit_pages * PAGE_CACHE_SIZE > 4096 * 1024)
722 ratelimit_pages = (4096 * 1024) / PAGE_CACHE_SIZE;
723}
724
Chandra Seetharaman26c21432006-06-27 02:54:10 -0700725static int __cpuinit
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726ratelimit_handler(struct notifier_block *self, unsigned long u, void *v)
727{
Chandra Seetharaman2d1d43f2006-09-29 02:01:25 -0700728 writeback_set_ratelimit();
Paul E. McKenneyaa0f0302007-02-10 01:46:37 -0800729 return NOTIFY_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730}
731
Chandra Seetharaman74b85f32006-06-27 02:54:09 -0700732static struct notifier_block __cpuinitdata ratelimit_nb = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 .notifier_call = ratelimit_handler,
734 .next = NULL,
735};
736
737/*
Linus Torvaldsdc6e29d2007-01-29 16:37:38 -0800738 * Called early on to tune the page writeback dirty limits.
739 *
740 * We used to scale dirty pages according to how total memory
741 * related to pages that could be allocated for buffers (by
742 * comparing nr_free_buffer_pages() to vm_total_pages.
743 *
744 * However, that was when we used "dirty_ratio" to scale with
745 * all memory, and we don't do that any more. "dirty_ratio"
746 * is now applied to total non-HIGHPAGE memory (by subtracting
747 * totalhigh_pages from vm_total_pages), and as such we can't
748 * get into the old insane situation any more where we had
749 * large amounts of dirty pages compared to a small amount of
750 * non-HIGHMEM memory.
751 *
752 * But we might still want to scale the dirty_ratio by how
753 * much memory the box has..
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 */
755void __init page_writeback_init(void)
756{
Peter Zijlstra04fbfdc2007-10-16 23:25:50 -0700757 int shift;
758
Bart Samwelf6ef9432006-03-24 03:15:48 -0800759 mod_timer(&wb_timer, jiffies + dirty_writeback_interval);
Chandra Seetharaman2d1d43f2006-09-29 02:01:25 -0700760 writeback_set_ratelimit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 register_cpu_notifier(&ratelimit_nb);
Peter Zijlstra04fbfdc2007-10-16 23:25:50 -0700762
763 shift = calc_period_shift();
764 prop_descriptor_init(&vm_completions, shift);
Peter Zijlstra3e26c142007-10-16 23:25:50 -0700765 prop_descriptor_init(&vm_dirties, shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766}
767
David Howells811d7362006-08-29 19:06:09 +0100768/**
Miklos Szeredi0ea97182007-05-10 22:22:51 -0700769 * 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 +0100770 * @mapping: address space structure to write
771 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
Miklos Szeredi0ea97182007-05-10 22:22:51 -0700772 * @writepage: function called for each page
773 * @data: data passed to writepage function
David Howells811d7362006-08-29 19:06:09 +0100774 *
Miklos Szeredi0ea97182007-05-10 22:22:51 -0700775 * If a page is already under I/O, write_cache_pages() skips it, even
David Howells811d7362006-08-29 19:06:09 +0100776 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
777 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
778 * and msync() need to guarantee that all the data which was dirty at the time
779 * the call was made get new I/O started against them. If wbc->sync_mode is
780 * WB_SYNC_ALL then we were called for data integrity and we must wait for
781 * existing IO to complete.
David Howells811d7362006-08-29 19:06:09 +0100782 */
Miklos Szeredi0ea97182007-05-10 22:22:51 -0700783int write_cache_pages(struct address_space *mapping,
784 struct writeback_control *wbc, writepage_t writepage,
785 void *data)
David Howells811d7362006-08-29 19:06:09 +0100786{
787 struct backing_dev_info *bdi = mapping->backing_dev_info;
788 int ret = 0;
789 int done = 0;
David Howells811d7362006-08-29 19:06:09 +0100790 struct pagevec pvec;
791 int nr_pages;
792 pgoff_t index;
793 pgoff_t end; /* Inclusive */
794 int scanned = 0;
795 int range_whole = 0;
796
797 if (wbc->nonblocking && bdi_write_congested(bdi)) {
798 wbc->encountered_congestion = 1;
799 return 0;
800 }
801
David Howells811d7362006-08-29 19:06:09 +0100802 pagevec_init(&pvec, 0);
803 if (wbc->range_cyclic) {
804 index = mapping->writeback_index; /* Start from prev offset */
805 end = -1;
806 } else {
807 index = wbc->range_start >> PAGE_CACHE_SHIFT;
808 end = wbc->range_end >> PAGE_CACHE_SHIFT;
809 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
810 range_whole = 1;
811 scanned = 1;
812 }
813retry:
814 while (!done && (index <= end) &&
815 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
816 PAGECACHE_TAG_DIRTY,
817 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
818 unsigned i;
819
820 scanned = 1;
821 for (i = 0; i < nr_pages; i++) {
822 struct page *page = pvec.pages[i];
823
824 /*
825 * At this point we hold neither mapping->tree_lock nor
826 * lock on the page itself: the page may be truncated or
827 * invalidated (changing page->mapping to NULL), or even
828 * swizzled back from swapper_space to tmpfs file
829 * mapping
830 */
831 lock_page(page);
832
833 if (unlikely(page->mapping != mapping)) {
834 unlock_page(page);
835 continue;
836 }
837
838 if (!wbc->range_cyclic && page->index > end) {
839 done = 1;
840 unlock_page(page);
841 continue;
842 }
843
844 if (wbc->sync_mode != WB_SYNC_NONE)
845 wait_on_page_writeback(page);
846
847 if (PageWriteback(page) ||
848 !clear_page_dirty_for_io(page)) {
849 unlock_page(page);
850 continue;
851 }
852
Miklos Szeredi0ea97182007-05-10 22:22:51 -0700853 ret = (*writepage)(page, wbc, data);
David Howells811d7362006-08-29 19:06:09 +0100854
Andrew Mortone4230032007-10-16 23:26:02 -0700855 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
David Howells811d7362006-08-29 19:06:09 +0100856 unlock_page(page);
Andrew Mortone4230032007-10-16 23:26:02 -0700857 ret = 0;
858 }
David Howells811d7362006-08-29 19:06:09 +0100859 if (ret || (--(wbc->nr_to_write) <= 0))
860 done = 1;
861 if (wbc->nonblocking && bdi_write_congested(bdi)) {
862 wbc->encountered_congestion = 1;
863 done = 1;
864 }
865 }
866 pagevec_release(&pvec);
867 cond_resched();
868 }
869 if (!scanned && !done) {
870 /*
871 * We hit the last page and there is more work to be done: wrap
872 * back to the start of the file
873 */
874 scanned = 1;
875 index = 0;
876 goto retry;
877 }
878 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
879 mapping->writeback_index = index;
880 return ret;
881}
Miklos Szeredi0ea97182007-05-10 22:22:51 -0700882EXPORT_SYMBOL(write_cache_pages);
883
884/*
885 * Function used by generic_writepages to call the real writepage
886 * function and set the mapping flags on error
887 */
888static int __writepage(struct page *page, struct writeback_control *wbc,
889 void *data)
890{
891 struct address_space *mapping = data;
892 int ret = mapping->a_ops->writepage(page, wbc);
893 mapping_set_error(mapping, ret);
894 return ret;
895}
896
897/**
898 * generic_writepages - walk the list of dirty pages of the given address space and writepage() all of them.
899 * @mapping: address space structure to write
900 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
901 *
902 * This is a library function, which implements the writepages()
903 * address_space_operation.
904 */
905int generic_writepages(struct address_space *mapping,
906 struct writeback_control *wbc)
907{
908 /* deal with chardevs and other special file */
909 if (!mapping->a_ops->writepage)
910 return 0;
911
912 return write_cache_pages(mapping, wbc, __writepage, mapping);
913}
David Howells811d7362006-08-29 19:06:09 +0100914
915EXPORT_SYMBOL(generic_writepages);
916
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917int do_writepages(struct address_space *mapping, struct writeback_control *wbc)
918{
Andrew Morton22905f72005-11-16 15:07:01 -0800919 int ret;
920
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 if (wbc->nr_to_write <= 0)
922 return 0;
Andrew Morton22905f72005-11-16 15:07:01 -0800923 wbc->for_writepages = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 if (mapping->a_ops->writepages)
Peter Zijlstrad08b3852006-09-25 23:30:57 -0700925 ret = mapping->a_ops->writepages(mapping, wbc);
Andrew Morton22905f72005-11-16 15:07:01 -0800926 else
927 ret = generic_writepages(mapping, wbc);
928 wbc->for_writepages = 0;
929 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930}
931
932/**
933 * write_one_page - write out a single page and optionally wait on I/O
Martin Waitz67be2dd2005-05-01 08:59:26 -0700934 * @page: the page to write
935 * @wait: if true, wait on writeout
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 *
937 * The page must be locked by the caller and will be unlocked upon return.
938 *
939 * write_one_page() returns a negative error code if I/O failed.
940 */
941int write_one_page(struct page *page, int wait)
942{
943 struct address_space *mapping = page->mapping;
944 int ret = 0;
945 struct writeback_control wbc = {
946 .sync_mode = WB_SYNC_ALL,
947 .nr_to_write = 1,
948 };
949
950 BUG_ON(!PageLocked(page));
951
952 if (wait)
953 wait_on_page_writeback(page);
954
955 if (clear_page_dirty_for_io(page)) {
956 page_cache_get(page);
957 ret = mapping->a_ops->writepage(page, &wbc);
958 if (ret == 0 && wait) {
959 wait_on_page_writeback(page);
960 if (PageError(page))
961 ret = -EIO;
962 }
963 page_cache_release(page);
964 } else {
965 unlock_page(page);
966 }
967 return ret;
968}
969EXPORT_SYMBOL(write_one_page);
970
971/*
Ken Chen76719322007-02-10 01:43:15 -0800972 * For address_spaces which do not use buffers nor write back.
973 */
974int __set_page_dirty_no_writeback(struct page *page)
975{
976 if (!PageDirty(page))
977 SetPageDirty(page);
978 return 0;
979}
980
981/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 * For address_spaces which do not use buffers. Just tag the page as dirty in
983 * its radix tree.
984 *
985 * This is also used when a single buffer is being dirtied: we want to set the
986 * page dirty in that case, but not all the buffers. This is a "bottom-up"
987 * dirtying, whereas __set_page_dirty_buffers() is a "top-down" dirtying.
988 *
989 * Most callers have locked the page, which pins the address_space in memory.
990 * But zap_pte_range() does not lock the page, however in that case the
991 * mapping is pinned by the vma's ->vm_file reference.
992 *
993 * We take care to handle the case where the page was truncated from the
994 * mapping by re-checking page_mapping() insode tree_lock.
995 */
996int __set_page_dirty_nobuffers(struct page *page)
997{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 if (!TestSetPageDirty(page)) {
999 struct address_space *mapping = page_mapping(page);
1000 struct address_space *mapping2;
1001
Andrew Morton8c085402006-12-10 02:19:24 -08001002 if (!mapping)
1003 return 1;
1004
1005 write_lock_irq(&mapping->tree_lock);
1006 mapping2 = page_mapping(page);
1007 if (mapping2) { /* Race with truncate? */
1008 BUG_ON(mapping2 != mapping);
Nick Piggin787d2212007-07-17 04:03:34 -07001009 WARN_ON_ONCE(!PagePrivate(page) && !PageUptodate(page));
Andrew Morton55e829a2006-12-10 02:19:27 -08001010 if (mapping_cap_account_dirty(mapping)) {
Andrew Morton8c085402006-12-10 02:19:24 -08001011 __inc_zone_page_state(page, NR_FILE_DIRTY);
Peter Zijlstrac9e51e42007-10-16 23:25:47 -07001012 __inc_bdi_stat(mapping->backing_dev_info,
1013 BDI_RECLAIMABLE);
Andrew Morton55e829a2006-12-10 02:19:27 -08001014 task_io_account_write(PAGE_CACHE_SIZE);
1015 }
Andrew Morton8c085402006-12-10 02:19:24 -08001016 radix_tree_tag_set(&mapping->page_tree,
1017 page_index(page), PAGECACHE_TAG_DIRTY);
1018 }
1019 write_unlock_irq(&mapping->tree_lock);
1020 if (mapping->host) {
1021 /* !PageAnon && !swapper_space */
1022 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 }
Andrew Morton4741c9f2006-03-24 03:18:11 -08001024 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 }
Andrew Morton4741c9f2006-03-24 03:18:11 -08001026 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027}
1028EXPORT_SYMBOL(__set_page_dirty_nobuffers);
1029
1030/*
1031 * When a writepage implementation decides that it doesn't want to write this
1032 * page for some reason, it should redirty the locked page via
1033 * redirty_page_for_writepage() and it should then unlock the page and return 0
1034 */
1035int redirty_page_for_writepage(struct writeback_control *wbc, struct page *page)
1036{
1037 wbc->pages_skipped++;
1038 return __set_page_dirty_nobuffers(page);
1039}
1040EXPORT_SYMBOL(redirty_page_for_writepage);
1041
1042/*
1043 * If the mapping doesn't provide a set_page_dirty a_op, then
1044 * just fall through and assume that it wants buffer_heads.
1045 */
Peter Zijlstra3e26c142007-10-16 23:25:50 -07001046static int __set_page_dirty(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047{
1048 struct address_space *mapping = page_mapping(page);
1049
1050 if (likely(mapping)) {
1051 int (*spd)(struct page *) = mapping->a_ops->set_page_dirty;
David Howells93614012006-09-30 20:45:40 +02001052#ifdef CONFIG_BLOCK
1053 if (!spd)
1054 spd = __set_page_dirty_buffers;
1055#endif
1056 return (*spd)(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 }
Andrew Morton4741c9f2006-03-24 03:18:11 -08001058 if (!PageDirty(page)) {
1059 if (!TestSetPageDirty(page))
1060 return 1;
1061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 return 0;
1063}
Peter Zijlstra3e26c142007-10-16 23:25:50 -07001064
1065int fastcall set_page_dirty(struct page *page)
1066{
1067 int ret = __set_page_dirty(page);
1068 if (ret)
1069 task_dirty_inc(current);
1070 return ret;
1071}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072EXPORT_SYMBOL(set_page_dirty);
1073
1074/*
1075 * set_page_dirty() is racy if the caller has no reference against
1076 * page->mapping->host, and if the page is unlocked. This is because another
1077 * CPU could truncate the page off the mapping and then free the mapping.
1078 *
1079 * Usually, the page _is_ locked, or the caller is a user-space process which
1080 * holds a reference on the inode by having an open file.
1081 *
1082 * In other cases, the page should be locked before running set_page_dirty().
1083 */
1084int set_page_dirty_lock(struct page *page)
1085{
1086 int ret;
1087
Nick Piggindb376482006-09-25 23:31:24 -07001088 lock_page_nosync(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 ret = set_page_dirty(page);
1090 unlock_page(page);
1091 return ret;
1092}
1093EXPORT_SYMBOL(set_page_dirty_lock);
1094
1095/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 * Clear a page's dirty flag, while caring for dirty memory accounting.
1097 * Returns true if the page was previously dirty.
1098 *
1099 * This is for preparing to put the page under writeout. We leave the page
1100 * tagged as dirty in the radix tree so that a concurrent write-for-sync
1101 * can discover it via a PAGECACHE_TAG_DIRTY walk. The ->writepage
1102 * implementation will run either set_page_writeback() or set_page_dirty(),
1103 * at which stage we bring the page's dirty flag and radix-tree dirty tag
1104 * back into sync.
1105 *
1106 * This incoherency between the page's dirty flag and radix-tree tag is
1107 * unfortunate, but it only exists while the page is locked.
1108 */
1109int clear_page_dirty_for_io(struct page *page)
1110{
1111 struct address_space *mapping = page_mapping(page);
1112
Nick Piggin79352892007-07-19 01:47:22 -07001113 BUG_ON(!PageLocked(page));
1114
Fengguang Wufe3cba12007-07-19 01:48:07 -07001115 ClearPageReclaim(page);
Linus Torvalds7658cc22006-12-29 10:00:58 -08001116 if (mapping && mapping_cap_account_dirty(mapping)) {
1117 /*
1118 * Yes, Virginia, this is indeed insane.
1119 *
1120 * We use this sequence to make sure that
1121 * (a) we account for dirty stats properly
1122 * (b) we tell the low-level filesystem to
1123 * mark the whole page dirty if it was
1124 * dirty in a pagetable. Only to then
1125 * (c) clean the page again and return 1 to
1126 * cause the writeback.
1127 *
1128 * This way we avoid all nasty races with the
1129 * dirty bit in multiple places and clearing
1130 * them concurrently from different threads.
1131 *
1132 * Note! Normally the "set_page_dirty(page)"
1133 * has no effect on the actual dirty bit - since
1134 * that will already usually be set. But we
1135 * need the side effects, and it can help us
1136 * avoid races.
1137 *
1138 * We basically use the page "master dirty bit"
1139 * as a serialization point for all the different
1140 * threads doing their things.
Linus Torvalds7658cc22006-12-29 10:00:58 -08001141 */
1142 if (page_mkclean(page))
1143 set_page_dirty(page);
Nick Piggin79352892007-07-19 01:47:22 -07001144 /*
1145 * We carefully synchronise fault handlers against
1146 * installing a dirty pte and marking the page dirty
1147 * at this point. We do this by having them hold the
1148 * page lock at some point after installing their
1149 * pte, but before marking the page dirty.
1150 * Pages are always locked coming in here, so we get
1151 * the desired exclusion. See mm/memory.c:do_wp_page()
1152 * for more comments.
1153 */
Linus Torvalds7658cc22006-12-29 10:00:58 -08001154 if (TestClearPageDirty(page)) {
Andrew Morton8c085402006-12-10 02:19:24 -08001155 dec_zone_page_state(page, NR_FILE_DIRTY);
Peter Zijlstrac9e51e42007-10-16 23:25:47 -07001156 dec_bdi_stat(mapping->backing_dev_info,
1157 BDI_RECLAIMABLE);
Linus Torvalds7658cc22006-12-29 10:00:58 -08001158 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 }
Linus Torvalds7658cc22006-12-29 10:00:58 -08001160 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 }
Linus Torvalds7658cc22006-12-29 10:00:58 -08001162 return TestClearPageDirty(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163}
Hans Reiser58bb01a2005-11-18 01:10:53 -08001164EXPORT_SYMBOL(clear_page_dirty_for_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
1166int test_clear_page_writeback(struct page *page)
1167{
1168 struct address_space *mapping = page_mapping(page);
1169 int ret;
1170
1171 if (mapping) {
Peter Zijlstra69cb51d2007-10-16 23:25:48 -07001172 struct backing_dev_info *bdi = mapping->backing_dev_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 unsigned long flags;
1174
1175 write_lock_irqsave(&mapping->tree_lock, flags);
1176 ret = TestClearPageWriteback(page);
Peter Zijlstra69cb51d2007-10-16 23:25:48 -07001177 if (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 radix_tree_tag_clear(&mapping->page_tree,
1179 page_index(page),
1180 PAGECACHE_TAG_WRITEBACK);
Peter Zijlstra04fbfdc2007-10-16 23:25:50 -07001181 if (bdi_cap_writeback_dirty(bdi)) {
Peter Zijlstra69cb51d2007-10-16 23:25:48 -07001182 __dec_bdi_stat(bdi, BDI_WRITEBACK);
Peter Zijlstra04fbfdc2007-10-16 23:25:50 -07001183 __bdi_writeout_inc(bdi);
1184 }
Peter Zijlstra69cb51d2007-10-16 23:25:48 -07001185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 write_unlock_irqrestore(&mapping->tree_lock, flags);
1187 } else {
1188 ret = TestClearPageWriteback(page);
1189 }
Andrew Mortond688abf2007-07-19 01:49:17 -07001190 if (ret)
1191 dec_zone_page_state(page, NR_WRITEBACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 return ret;
1193}
1194
1195int test_set_page_writeback(struct page *page)
1196{
1197 struct address_space *mapping = page_mapping(page);
1198 int ret;
1199
1200 if (mapping) {
Peter Zijlstra69cb51d2007-10-16 23:25:48 -07001201 struct backing_dev_info *bdi = mapping->backing_dev_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 unsigned long flags;
1203
1204 write_lock_irqsave(&mapping->tree_lock, flags);
1205 ret = TestSetPageWriteback(page);
Peter Zijlstra69cb51d2007-10-16 23:25:48 -07001206 if (!ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 radix_tree_tag_set(&mapping->page_tree,
1208 page_index(page),
1209 PAGECACHE_TAG_WRITEBACK);
Peter Zijlstra69cb51d2007-10-16 23:25:48 -07001210 if (bdi_cap_writeback_dirty(bdi))
1211 __inc_bdi_stat(bdi, BDI_WRITEBACK);
1212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 if (!PageDirty(page))
1214 radix_tree_tag_clear(&mapping->page_tree,
1215 page_index(page),
1216 PAGECACHE_TAG_DIRTY);
1217 write_unlock_irqrestore(&mapping->tree_lock, flags);
1218 } else {
1219 ret = TestSetPageWriteback(page);
1220 }
Andrew Mortond688abf2007-07-19 01:49:17 -07001221 if (!ret)
1222 inc_zone_page_state(page, NR_WRITEBACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 return ret;
1224
1225}
1226EXPORT_SYMBOL(test_set_page_writeback);
1227
1228/*
Nick Piggin00128182007-10-16 01:24:40 -07001229 * Return true if any of the pages in the mapping are marked with the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 * passed tag.
1231 */
1232int mapping_tagged(struct address_space *mapping, int tag)
1233{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 int ret;
Nick Piggin00128182007-10-16 01:24:40 -07001235 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 ret = radix_tree_tagged(&mapping->page_tree, tag);
Nick Piggin00128182007-10-16 01:24:40 -07001237 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 return ret;
1239}
1240EXPORT_SYMBOL(mapping_tagged);