Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
f30c2269 | 2 | * mm/page-writeback.c |
1da177e4 LT |
3 | * |
4 | * Copyright (C) 2002, Linus Torvalds. | |
04fbfdc1 | 5 | * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com> |
1da177e4 LT |
6 | * |
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> | |
55e829af | 25 | #include <linux/task_io_accounting_ops.h> |
1da177e4 LT |
26 | #include <linux/blkdev.h> |
27 | #include <linux/mpage.h> | |
d08b3851 | 28 | #include <linux/rmap.h> |
1da177e4 LT |
29 | #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> | |
cf9a2ae8 | 35 | #include <linux/buffer_head.h> |
811d736f | 36 | #include <linux/pagevec.h> |
1da177e4 LT |
37 | |
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 | */ | |
51 | static long ratelimit_pages = 32; | |
52 | ||
1da177e4 LT |
53 | /* |
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 | */ | |
59 | static 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 | */ | |
07db59bd | 69 | int dirty_background_ratio = 5; |
1da177e4 LT |
70 | |
71 | /* | |
72 | * The generator of dirty data starts writeback at this percentage | |
73 | */ | |
07db59bd | 74 | int vm_dirty_ratio = 10; |
1da177e4 LT |
75 | |
76 | /* | |
fd5403c7 | 77 | * The interval between `kupdate'-style writebacks, in jiffies |
1da177e4 | 78 | */ |
f6ef9438 | 79 | int dirty_writeback_interval = 5 * HZ; |
1da177e4 LT |
80 | |
81 | /* | |
fd5403c7 | 82 | * The longest number of jiffies for which data is allowed to remain dirty |
1da177e4 | 83 | */ |
f6ef9438 | 84 | int dirty_expire_interval = 30 * HZ; |
1da177e4 LT |
85 | |
86 | /* | |
87 | * Flag that makes the machine dump writes/reads and block dirtyings. | |
88 | */ | |
89 | int block_dump; | |
90 | ||
91 | /* | |
ed5b43f1 BS |
92 | * 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. | |
1da177e4 LT |
94 | */ |
95 | int laptop_mode; | |
96 | ||
97 | EXPORT_SYMBOL(laptop_mode); | |
98 | ||
99 | /* End of sysctl-exported parameters */ | |
100 | ||
101 | ||
102 | static void background_writeout(unsigned long _min_pages); | |
103 | ||
04fbfdc1 PZ |
104 | /* |
105 | * 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 | */ | |
120 | static struct prop_descriptor vm_completions; | |
3e26c149 | 121 | static struct prop_descriptor vm_dirties; |
04fbfdc1 PZ |
122 | |
123 | static 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 | */ | |
130 | static 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 | */ | |
141 | int 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); | |
3e26c149 | 150 | prop_change_shift(&vm_dirties, shift); |
04fbfdc1 PZ |
151 | } |
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 | */ | |
159 | static inline void __bdi_writeout_inc(struct backing_dev_info *bdi) | |
160 | { | |
161 | __prop_inc_percpu(&vm_completions, &bdi->completions); | |
162 | } | |
163 | ||
3e26c149 PZ |
164 | static inline void task_dirty_inc(struct task_struct *tsk) |
165 | { | |
166 | prop_inc_single(&vm_dirties, &tsk->dirties); | |
167 | } | |
168 | ||
04fbfdc1 PZ |
169 | /* |
170 | * Obtain an accurate fraction of the BDI's portion. | |
171 | */ | |
172 | static 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 | */ | |
189 | static void | |
190 | clip_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 | ||
3e26c149 PZ |
208 | static 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 | */ | |
222 | void 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 | ||
1da177e4 LT |
239 | /* |
240 | * 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 | */ | |
1b424464 CL |
256 | |
257 | static unsigned long highmem_dirtyable_memory(unsigned long total) | |
258 | { | |
259 | #ifdef CONFIG_HIGHMEM | |
260 | int node; | |
261 | unsigned long x = 0; | |
262 | ||
37b07e41 | 263 | for_each_node_state(node, N_HIGH_MEMORY) { |
1b424464 CL |
264 | 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 | ||
283 | static 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 | ||
1da177e4 | 294 | static void |
04fbfdc1 PZ |
295 | get_dirty_limits(long *pbackground, long *pdirty, long *pbdi_dirty, |
296 | struct backing_dev_info *bdi) | |
1da177e4 LT |
297 | { |
298 | int background_ratio; /* Percentages */ | |
299 | int dirty_ratio; | |
300 | int unmapped_ratio; | |
301 | long background; | |
302 | long dirty; | |
1b424464 | 303 | unsigned long available_memory = determine_dirtyable_memory(); |
1da177e4 LT |
304 | struct task_struct *tsk; |
305 | ||
c24f21bd CL |
306 | unmapped_ratio = 100 - ((global_page_state(NR_FILE_MAPPED) + |
307 | global_page_state(NR_ANON_PAGES)) * 100) / | |
1b424464 | 308 | available_memory; |
1da177e4 LT |
309 | |
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; | |
04fbfdc1 PZ |
330 | |
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); | |
3e26c149 | 345 | task_dirty_limit(current, pbdi_dirty); |
04fbfdc1 | 346 | } |
1da177e4 LT |
347 | } |
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 | */ | |
356 | static void balance_dirty_pages(struct address_space *mapping) | |
357 | { | |
04fbfdc1 PZ |
358 | long bdi_nr_reclaimable; |
359 | long bdi_nr_writeback; | |
1da177e4 LT |
360 | long background_thresh; |
361 | long dirty_thresh; | |
04fbfdc1 | 362 | long bdi_thresh; |
1da177e4 LT |
363 | 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, | |
111ebb6e | 374 | .range_cyclic = 1, |
1da177e4 LT |
375 | }; |
376 | ||
04fbfdc1 PZ |
377 | 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; | |
1da177e4 | 383 | |
04fbfdc1 PZ |
384 | if (!bdi->dirty_exceeded) |
385 | bdi->dirty_exceeded = 1; | |
1da177e4 LT |
386 | |
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 | */ | |
04fbfdc1 | 393 | if (bdi_nr_reclaimable) { |
1da177e4 | 394 | writeback_inodes(&wbc); |
1da177e4 | 395 | pages_written += write_chunk - wbc.nr_to_write; |
04fbfdc1 PZ |
396 | get_dirty_limits(&background_thresh, &dirty_thresh, |
397 | &bdi_thresh, bdi); | |
398 | } | |
399 | ||
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); | |
1da177e4 | 416 | } |
04fbfdc1 PZ |
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 | ||
3fcfab16 | 423 | congestion_wait(WRITE, HZ/10); |
1da177e4 LT |
424 | } |
425 | ||
04fbfdc1 PZ |
426 | if (bdi_nr_reclaimable + bdi_nr_writeback < bdi_thresh && |
427 | bdi->dirty_exceeded) | |
428 | bdi->dirty_exceeded = 0; | |
1da177e4 LT |
429 | |
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) || | |
04fbfdc1 PZ |
442 | (!laptop_mode && (global_page_state(NR_FILE_DIRTY) |
443 | + global_page_state(NR_UNSTABLE_NFS) | |
444 | > background_thresh))) | |
1da177e4 LT |
445 | pdflush_operation(background_writeout, 0); |
446 | } | |
447 | ||
a200ee18 | 448 | void set_page_dirty_balance(struct page *page, int page_mkwrite) |
edc79b2a | 449 | { |
a200ee18 | 450 | if (set_page_dirty(page) || page_mkwrite) { |
edc79b2a PZ |
451 | struct address_space *mapping = page_mapping(page); |
452 | ||
453 | if (mapping) | |
454 | balance_dirty_pages_ratelimited(mapping); | |
455 | } | |
456 | } | |
457 | ||
1da177e4 | 458 | /** |
fa5a734e | 459 | * balance_dirty_pages_ratelimited_nr - balance dirty memory state |
67be2dd1 | 460 | * @mapping: address_space which was dirtied |
a580290c | 461 | * @nr_pages_dirtied: number of pages which the caller has just dirtied |
1da177e4 LT |
462 | * |
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 | */ | |
fa5a734e AM |
472 | void balance_dirty_pages_ratelimited_nr(struct address_space *mapping, |
473 | unsigned long nr_pages_dirtied) | |
1da177e4 | 474 | { |
fa5a734e AM |
475 | static DEFINE_PER_CPU(unsigned long, ratelimits) = 0; |
476 | unsigned long ratelimit; | |
477 | unsigned long *p; | |
1da177e4 LT |
478 | |
479 | ratelimit = ratelimit_pages; | |
04fbfdc1 | 480 | if (mapping->backing_dev_info->dirty_exceeded) |
1da177e4 LT |
481 | 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 | */ | |
fa5a734e AM |
487 | preempt_disable(); |
488 | p = &__get_cpu_var(ratelimits); | |
489 | *p += nr_pages_dirtied; | |
490 | if (unlikely(*p >= ratelimit)) { | |
491 | *p = 0; | |
492 | preempt_enable(); | |
1da177e4 LT |
493 | balance_dirty_pages(mapping); |
494 | return; | |
495 | } | |
fa5a734e | 496 | preempt_enable(); |
1da177e4 | 497 | } |
fa5a734e | 498 | EXPORT_SYMBOL(balance_dirty_pages_ratelimited_nr); |
1da177e4 | 499 | |
232ea4d6 | 500 | void throttle_vm_writeout(gfp_t gfp_mask) |
1da177e4 | 501 | { |
1da177e4 LT |
502 | long background_thresh; |
503 | long dirty_thresh; | |
504 | ||
232ea4d6 AM |
505 | 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 | ||
1da177e4 | 515 | for ( ; ; ) { |
04fbfdc1 | 516 | get_dirty_limits(&background_thresh, &dirty_thresh, NULL, NULL); |
1da177e4 LT |
517 | |
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 | ||
c24f21bd CL |
524 | if (global_page_state(NR_UNSTABLE_NFS) + |
525 | global_page_state(NR_WRITEBACK) <= dirty_thresh) | |
526 | break; | |
3fcfab16 | 527 | congestion_wait(WRITE, HZ/10); |
1da177e4 LT |
528 | } |
529 | } | |
530 | ||
1da177e4 LT |
531 | /* |
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 | */ | |
535 | static 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, | |
111ebb6e | 544 | .range_cyclic = 1, |
1da177e4 LT |
545 | }; |
546 | ||
547 | for ( ; ; ) { | |
1da177e4 LT |
548 | long background_thresh; |
549 | long dirty_thresh; | |
550 | ||
04fbfdc1 | 551 | get_dirty_limits(&background_thresh, &dirty_thresh, NULL, NULL); |
c24f21bd CL |
552 | if (global_page_state(NR_FILE_DIRTY) + |
553 | global_page_state(NR_UNSTABLE_NFS) < background_thresh | |
1da177e4 LT |
554 | && min_pages <= 0) |
555 | break; | |
556 | wbc.encountered_congestion = 0; | |
557 | wbc.nr_to_write = MAX_WRITEBACK_PAGES; | |
558 | wbc.pages_skipped = 0; | |
559 | writeback_inodes(&wbc); | |
560 | min_pages -= MAX_WRITEBACK_PAGES - wbc.nr_to_write; | |
561 | if (wbc.nr_to_write > 0 || wbc.pages_skipped > 0) { | |
562 | /* Wrote less than expected */ | |
3fcfab16 | 563 | congestion_wait(WRITE, HZ/10); |
1da177e4 LT |
564 | if (!wbc.encountered_congestion) |
565 | break; | |
566 | } | |
567 | } | |
568 | } | |
569 | ||
570 | /* | |
571 | * Start writeback of `nr_pages' pages. If `nr_pages' is zero, write back | |
572 | * the whole world. Returns 0 if a pdflush thread was dispatched. Returns | |
573 | * -1 if all pdflush threads were busy. | |
574 | */ | |
687a21ce | 575 | int wakeup_pdflush(long nr_pages) |
1da177e4 | 576 | { |
c24f21bd CL |
577 | if (nr_pages == 0) |
578 | nr_pages = global_page_state(NR_FILE_DIRTY) + | |
579 | global_page_state(NR_UNSTABLE_NFS); | |
1da177e4 LT |
580 | return pdflush_operation(background_writeout, nr_pages); |
581 | } | |
582 | ||
583 | static void wb_timer_fn(unsigned long unused); | |
584 | static void laptop_timer_fn(unsigned long unused); | |
585 | ||
8d06afab IM |
586 | static DEFINE_TIMER(wb_timer, wb_timer_fn, 0, 0); |
587 | static DEFINE_TIMER(laptop_mode_wb_timer, laptop_timer_fn, 0, 0); | |
1da177e4 LT |
588 | |
589 | /* | |
590 | * Periodic writeback of "old" data. | |
591 | * | |
592 | * Define "old": the first time one of an inode's pages is dirtied, we mark the | |
593 | * dirtying-time in the inode's address_space. So this periodic writeback code | |
594 | * just walks the superblock inode list, writing back any inodes which are | |
595 | * older than a specific point in time. | |
596 | * | |
f6ef9438 BS |
597 | * Try to run once per dirty_writeback_interval. But if a writeback event |
598 | * takes longer than a dirty_writeback_interval interval, then leave a | |
1da177e4 LT |
599 | * one-second gap. |
600 | * | |
601 | * older_than_this takes precedence over nr_to_write. So we'll only write back | |
602 | * all dirty pages if they are all attached to "old" mappings. | |
603 | */ | |
604 | static void wb_kupdate(unsigned long arg) | |
605 | { | |
606 | unsigned long oldest_jif; | |
607 | unsigned long start_jif; | |
608 | unsigned long next_jif; | |
609 | long nr_to_write; | |
1da177e4 LT |
610 | struct writeback_control wbc = { |
611 | .bdi = NULL, | |
612 | .sync_mode = WB_SYNC_NONE, | |
613 | .older_than_this = &oldest_jif, | |
614 | .nr_to_write = 0, | |
615 | .nonblocking = 1, | |
616 | .for_kupdate = 1, | |
111ebb6e | 617 | .range_cyclic = 1, |
1da177e4 LT |
618 | }; |
619 | ||
620 | sync_supers(); | |
621 | ||
f6ef9438 | 622 | oldest_jif = jiffies - dirty_expire_interval; |
1da177e4 | 623 | start_jif = jiffies; |
f6ef9438 | 624 | next_jif = start_jif + dirty_writeback_interval; |
c24f21bd CL |
625 | nr_to_write = global_page_state(NR_FILE_DIRTY) + |
626 | global_page_state(NR_UNSTABLE_NFS) + | |
1da177e4 LT |
627 | (inodes_stat.nr_inodes - inodes_stat.nr_unused); |
628 | while (nr_to_write > 0) { | |
629 | wbc.encountered_congestion = 0; | |
630 | wbc.nr_to_write = MAX_WRITEBACK_PAGES; | |
631 | writeback_inodes(&wbc); | |
632 | if (wbc.nr_to_write > 0) { | |
633 | if (wbc.encountered_congestion) | |
3fcfab16 | 634 | congestion_wait(WRITE, HZ/10); |
1da177e4 LT |
635 | else |
636 | break; /* All the old data is written */ | |
637 | } | |
638 | nr_to_write -= MAX_WRITEBACK_PAGES - wbc.nr_to_write; | |
639 | } | |
640 | if (time_before(next_jif, jiffies + HZ)) | |
641 | next_jif = jiffies + HZ; | |
f6ef9438 | 642 | if (dirty_writeback_interval) |
1da177e4 LT |
643 | mod_timer(&wb_timer, next_jif); |
644 | } | |
645 | ||
646 | /* | |
647 | * sysctl handler for /proc/sys/vm/dirty_writeback_centisecs | |
648 | */ | |
649 | int dirty_writeback_centisecs_handler(ctl_table *table, int write, | |
3e733f07 | 650 | struct file *file, void __user *buffer, size_t *length, loff_t *ppos) |
1da177e4 | 651 | { |
f6ef9438 | 652 | proc_dointvec_userhz_jiffies(table, write, file, buffer, length, ppos); |
3e733f07 AM |
653 | if (dirty_writeback_interval) |
654 | mod_timer(&wb_timer, jiffies + dirty_writeback_interval); | |
655 | else | |
1da177e4 | 656 | del_timer(&wb_timer); |
1da177e4 LT |
657 | return 0; |
658 | } | |
659 | ||
660 | static void wb_timer_fn(unsigned long unused) | |
661 | { | |
662 | if (pdflush_operation(wb_kupdate, 0) < 0) | |
663 | mod_timer(&wb_timer, jiffies + HZ); /* delay 1 second */ | |
664 | } | |
665 | ||
666 | static void laptop_flush(unsigned long unused) | |
667 | { | |
668 | sys_sync(); | |
669 | } | |
670 | ||
671 | static void laptop_timer_fn(unsigned long unused) | |
672 | { | |
673 | pdflush_operation(laptop_flush, 0); | |
674 | } | |
675 | ||
676 | /* | |
677 | * We've spun up the disk and we're in laptop mode: schedule writeback | |
678 | * of all dirty data a few seconds from now. If the flush is already scheduled | |
679 | * then push it back - the user is still using the disk. | |
680 | */ | |
681 | void laptop_io_completion(void) | |
682 | { | |
ed5b43f1 | 683 | mod_timer(&laptop_mode_wb_timer, jiffies + laptop_mode); |
1da177e4 LT |
684 | } |
685 | ||
686 | /* | |
687 | * We're in laptop mode and we've just synced. The sync's writes will have | |
688 | * caused another writeback to be scheduled by laptop_io_completion. | |
689 | * Nothing needs to be written back anymore, so we unschedule the writeback. | |
690 | */ | |
691 | void laptop_sync_completion(void) | |
692 | { | |
693 | del_timer(&laptop_mode_wb_timer); | |
694 | } | |
695 | ||
696 | /* | |
697 | * If ratelimit_pages is too high then we can get into dirty-data overload | |
698 | * if a large number of processes all perform writes at the same time. | |
699 | * If it is too low then SMP machines will call the (expensive) | |
700 | * get_writeback_state too often. | |
701 | * | |
702 | * Here we set ratelimit_pages to a level which ensures that when all CPUs are | |
703 | * dirtying in parallel, we cannot go more than 3% (1/32) over the dirty memory | |
704 | * thresholds before writeback cuts in. | |
705 | * | |
706 | * But the limit should not be set too high. Because it also controls the | |
707 | * amount of memory which the balance_dirty_pages() caller has to write back. | |
708 | * If this is too large then the caller will block on the IO queue all the | |
709 | * time. So limit it to four megabytes - the balance_dirty_pages() caller | |
710 | * will write six megabyte chunks, max. | |
711 | */ | |
712 | ||
2d1d43f6 | 713 | void writeback_set_ratelimit(void) |
1da177e4 | 714 | { |
40c99aae | 715 | ratelimit_pages = vm_total_pages / (num_online_cpus() * 32); |
1da177e4 LT |
716 | if (ratelimit_pages < 16) |
717 | ratelimit_pages = 16; | |
718 | if (ratelimit_pages * PAGE_CACHE_SIZE > 4096 * 1024) | |
719 | ratelimit_pages = (4096 * 1024) / PAGE_CACHE_SIZE; | |
720 | } | |
721 | ||
26c2143b | 722 | static int __cpuinit |
1da177e4 LT |
723 | ratelimit_handler(struct notifier_block *self, unsigned long u, void *v) |
724 | { | |
2d1d43f6 | 725 | writeback_set_ratelimit(); |
aa0f0303 | 726 | return NOTIFY_DONE; |
1da177e4 LT |
727 | } |
728 | ||
74b85f37 | 729 | static struct notifier_block __cpuinitdata ratelimit_nb = { |
1da177e4 LT |
730 | .notifier_call = ratelimit_handler, |
731 | .next = NULL, | |
732 | }; | |
733 | ||
734 | /* | |
dc6e29da LT |
735 | * Called early on to tune the page writeback dirty limits. |
736 | * | |
737 | * We used to scale dirty pages according to how total memory | |
738 | * related to pages that could be allocated for buffers (by | |
739 | * comparing nr_free_buffer_pages() to vm_total_pages. | |
740 | * | |
741 | * However, that was when we used "dirty_ratio" to scale with | |
742 | * all memory, and we don't do that any more. "dirty_ratio" | |
743 | * is now applied to total non-HIGHPAGE memory (by subtracting | |
744 | * totalhigh_pages from vm_total_pages), and as such we can't | |
745 | * get into the old insane situation any more where we had | |
746 | * large amounts of dirty pages compared to a small amount of | |
747 | * non-HIGHMEM memory. | |
748 | * | |
749 | * But we might still want to scale the dirty_ratio by how | |
750 | * much memory the box has.. | |
1da177e4 LT |
751 | */ |
752 | void __init page_writeback_init(void) | |
753 | { | |
04fbfdc1 PZ |
754 | int shift; |
755 | ||
f6ef9438 | 756 | mod_timer(&wb_timer, jiffies + dirty_writeback_interval); |
2d1d43f6 | 757 | writeback_set_ratelimit(); |
1da177e4 | 758 | register_cpu_notifier(&ratelimit_nb); |
04fbfdc1 PZ |
759 | |
760 | shift = calc_period_shift(); | |
761 | prop_descriptor_init(&vm_completions, shift); | |
3e26c149 | 762 | prop_descriptor_init(&vm_dirties, shift); |
1da177e4 LT |
763 | } |
764 | ||
811d736f | 765 | /** |
0ea97180 | 766 | * write_cache_pages - walk the list of dirty pages of the given address space and write all of them. |
811d736f DH |
767 | * @mapping: address space structure to write |
768 | * @wbc: subtract the number of written pages from *@wbc->nr_to_write | |
0ea97180 MS |
769 | * @writepage: function called for each page |
770 | * @data: data passed to writepage function | |
811d736f | 771 | * |
0ea97180 | 772 | * If a page is already under I/O, write_cache_pages() skips it, even |
811d736f DH |
773 | * if it's dirty. This is desirable behaviour for memory-cleaning writeback, |
774 | * but it is INCORRECT for data-integrity system calls such as fsync(). fsync() | |
775 | * and msync() need to guarantee that all the data which was dirty at the time | |
776 | * the call was made get new I/O started against them. If wbc->sync_mode is | |
777 | * WB_SYNC_ALL then we were called for data integrity and we must wait for | |
778 | * existing IO to complete. | |
811d736f | 779 | */ |
0ea97180 MS |
780 | int write_cache_pages(struct address_space *mapping, |
781 | struct writeback_control *wbc, writepage_t writepage, | |
782 | void *data) | |
811d736f DH |
783 | { |
784 | struct backing_dev_info *bdi = mapping->backing_dev_info; | |
785 | int ret = 0; | |
786 | int done = 0; | |
811d736f DH |
787 | struct pagevec pvec; |
788 | int nr_pages; | |
789 | pgoff_t index; | |
790 | pgoff_t end; /* Inclusive */ | |
791 | int scanned = 0; | |
792 | int range_whole = 0; | |
793 | ||
794 | if (wbc->nonblocking && bdi_write_congested(bdi)) { | |
795 | wbc->encountered_congestion = 1; | |
796 | return 0; | |
797 | } | |
798 | ||
811d736f DH |
799 | pagevec_init(&pvec, 0); |
800 | if (wbc->range_cyclic) { | |
801 | index = mapping->writeback_index; /* Start from prev offset */ | |
802 | end = -1; | |
803 | } else { | |
804 | index = wbc->range_start >> PAGE_CACHE_SHIFT; | |
805 | end = wbc->range_end >> PAGE_CACHE_SHIFT; | |
806 | if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) | |
807 | range_whole = 1; | |
808 | scanned = 1; | |
809 | } | |
810 | retry: | |
811 | while (!done && (index <= end) && | |
812 | (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, | |
813 | PAGECACHE_TAG_DIRTY, | |
814 | min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) { | |
815 | unsigned i; | |
816 | ||
817 | scanned = 1; | |
818 | for (i = 0; i < nr_pages; i++) { | |
819 | struct page *page = pvec.pages[i]; | |
820 | ||
821 | /* | |
822 | * At this point we hold neither mapping->tree_lock nor | |
823 | * lock on the page itself: the page may be truncated or | |
824 | * invalidated (changing page->mapping to NULL), or even | |
825 | * swizzled back from swapper_space to tmpfs file | |
826 | * mapping | |
827 | */ | |
828 | lock_page(page); | |
829 | ||
830 | if (unlikely(page->mapping != mapping)) { | |
831 | unlock_page(page); | |
832 | continue; | |
833 | } | |
834 | ||
835 | if (!wbc->range_cyclic && page->index > end) { | |
836 | done = 1; | |
837 | unlock_page(page); | |
838 | continue; | |
839 | } | |
840 | ||
841 | if (wbc->sync_mode != WB_SYNC_NONE) | |
842 | wait_on_page_writeback(page); | |
843 | ||
844 | if (PageWriteback(page) || | |
845 | !clear_page_dirty_for_io(page)) { | |
846 | unlock_page(page); | |
847 | continue; | |
848 | } | |
849 | ||
0ea97180 | 850 | ret = (*writepage)(page, wbc, data); |
811d736f | 851 | |
e4230030 | 852 | if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) { |
811d736f | 853 | unlock_page(page); |
e4230030 AM |
854 | ret = 0; |
855 | } | |
811d736f DH |
856 | if (ret || (--(wbc->nr_to_write) <= 0)) |
857 | done = 1; | |
858 | if (wbc->nonblocking && bdi_write_congested(bdi)) { | |
859 | wbc->encountered_congestion = 1; | |
860 | done = 1; | |
861 | } | |
862 | } | |
863 | pagevec_release(&pvec); | |
864 | cond_resched(); | |
865 | } | |
866 | if (!scanned && !done) { | |
867 | /* | |
868 | * We hit the last page and there is more work to be done: wrap | |
869 | * back to the start of the file | |
870 | */ | |
871 | scanned = 1; | |
872 | index = 0; | |
873 | goto retry; | |
874 | } | |
875 | if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) | |
876 | mapping->writeback_index = index; | |
877 | return ret; | |
878 | } | |
0ea97180 MS |
879 | EXPORT_SYMBOL(write_cache_pages); |
880 | ||
881 | /* | |
882 | * Function used by generic_writepages to call the real writepage | |
883 | * function and set the mapping flags on error | |
884 | */ | |
885 | static int __writepage(struct page *page, struct writeback_control *wbc, | |
886 | void *data) | |
887 | { | |
888 | struct address_space *mapping = data; | |
889 | int ret = mapping->a_ops->writepage(page, wbc); | |
890 | mapping_set_error(mapping, ret); | |
891 | return ret; | |
892 | } | |
893 | ||
894 | /** | |
895 | * generic_writepages - walk the list of dirty pages of the given address space and writepage() all of them. | |
896 | * @mapping: address space structure to write | |
897 | * @wbc: subtract the number of written pages from *@wbc->nr_to_write | |
898 | * | |
899 | * This is a library function, which implements the writepages() | |
900 | * address_space_operation. | |
901 | */ | |
902 | int generic_writepages(struct address_space *mapping, | |
903 | struct writeback_control *wbc) | |
904 | { | |
905 | /* deal with chardevs and other special file */ | |
906 | if (!mapping->a_ops->writepage) | |
907 | return 0; | |
908 | ||
909 | return write_cache_pages(mapping, wbc, __writepage, mapping); | |
910 | } | |
811d736f DH |
911 | |
912 | EXPORT_SYMBOL(generic_writepages); | |
913 | ||
1da177e4 LT |
914 | int do_writepages(struct address_space *mapping, struct writeback_control *wbc) |
915 | { | |
22905f77 AM |
916 | int ret; |
917 | ||
1da177e4 LT |
918 | if (wbc->nr_to_write <= 0) |
919 | return 0; | |
22905f77 | 920 | wbc->for_writepages = 1; |
1da177e4 | 921 | if (mapping->a_ops->writepages) |
d08b3851 | 922 | ret = mapping->a_ops->writepages(mapping, wbc); |
22905f77 AM |
923 | else |
924 | ret = generic_writepages(mapping, wbc); | |
925 | wbc->for_writepages = 0; | |
926 | return ret; | |
1da177e4 LT |
927 | } |
928 | ||
929 | /** | |
930 | * write_one_page - write out a single page and optionally wait on I/O | |
67be2dd1 MW |
931 | * @page: the page to write |
932 | * @wait: if true, wait on writeout | |
1da177e4 LT |
933 | * |
934 | * The page must be locked by the caller and will be unlocked upon return. | |
935 | * | |
936 | * write_one_page() returns a negative error code if I/O failed. | |
937 | */ | |
938 | int write_one_page(struct page *page, int wait) | |
939 | { | |
940 | struct address_space *mapping = page->mapping; | |
941 | int ret = 0; | |
942 | struct writeback_control wbc = { | |
943 | .sync_mode = WB_SYNC_ALL, | |
944 | .nr_to_write = 1, | |
945 | }; | |
946 | ||
947 | BUG_ON(!PageLocked(page)); | |
948 | ||
949 | if (wait) | |
950 | wait_on_page_writeback(page); | |
951 | ||
952 | if (clear_page_dirty_for_io(page)) { | |
953 | page_cache_get(page); | |
954 | ret = mapping->a_ops->writepage(page, &wbc); | |
955 | if (ret == 0 && wait) { | |
956 | wait_on_page_writeback(page); | |
957 | if (PageError(page)) | |
958 | ret = -EIO; | |
959 | } | |
960 | page_cache_release(page); | |
961 | } else { | |
962 | unlock_page(page); | |
963 | } | |
964 | return ret; | |
965 | } | |
966 | EXPORT_SYMBOL(write_one_page); | |
967 | ||
76719325 KC |
968 | /* |
969 | * For address_spaces which do not use buffers nor write back. | |
970 | */ | |
971 | int __set_page_dirty_no_writeback(struct page *page) | |
972 | { | |
973 | if (!PageDirty(page)) | |
974 | SetPageDirty(page); | |
975 | return 0; | |
976 | } | |
977 | ||
1da177e4 LT |
978 | /* |
979 | * For address_spaces which do not use buffers. Just tag the page as dirty in | |
980 | * its radix tree. | |
981 | * | |
982 | * This is also used when a single buffer is being dirtied: we want to set the | |
983 | * page dirty in that case, but not all the buffers. This is a "bottom-up" | |
984 | * dirtying, whereas __set_page_dirty_buffers() is a "top-down" dirtying. | |
985 | * | |
986 | * Most callers have locked the page, which pins the address_space in memory. | |
987 | * But zap_pte_range() does not lock the page, however in that case the | |
988 | * mapping is pinned by the vma's ->vm_file reference. | |
989 | * | |
990 | * We take care to handle the case where the page was truncated from the | |
991 | * mapping by re-checking page_mapping() insode tree_lock. | |
992 | */ | |
993 | int __set_page_dirty_nobuffers(struct page *page) | |
994 | { | |
1da177e4 LT |
995 | if (!TestSetPageDirty(page)) { |
996 | struct address_space *mapping = page_mapping(page); | |
997 | struct address_space *mapping2; | |
998 | ||
8c08540f AM |
999 | if (!mapping) |
1000 | return 1; | |
1001 | ||
1002 | write_lock_irq(&mapping->tree_lock); | |
1003 | mapping2 = page_mapping(page); | |
1004 | if (mapping2) { /* Race with truncate? */ | |
1005 | BUG_ON(mapping2 != mapping); | |
787d2214 | 1006 | WARN_ON_ONCE(!PagePrivate(page) && !PageUptodate(page)); |
55e829af | 1007 | if (mapping_cap_account_dirty(mapping)) { |
8c08540f | 1008 | __inc_zone_page_state(page, NR_FILE_DIRTY); |
c9e51e41 PZ |
1009 | __inc_bdi_stat(mapping->backing_dev_info, |
1010 | BDI_RECLAIMABLE); | |
55e829af AM |
1011 | task_io_account_write(PAGE_CACHE_SIZE); |
1012 | } | |
8c08540f AM |
1013 | radix_tree_tag_set(&mapping->page_tree, |
1014 | page_index(page), PAGECACHE_TAG_DIRTY); | |
1015 | } | |
1016 | write_unlock_irq(&mapping->tree_lock); | |
1017 | if (mapping->host) { | |
1018 | /* !PageAnon && !swapper_space */ | |
1019 | __mark_inode_dirty(mapping->host, I_DIRTY_PAGES); | |
1da177e4 | 1020 | } |
4741c9fd | 1021 | return 1; |
1da177e4 | 1022 | } |
4741c9fd | 1023 | return 0; |
1da177e4 LT |
1024 | } |
1025 | EXPORT_SYMBOL(__set_page_dirty_nobuffers); | |
1026 | ||
1027 | /* | |
1028 | * When a writepage implementation decides that it doesn't want to write this | |
1029 | * page for some reason, it should redirty the locked page via | |
1030 | * redirty_page_for_writepage() and it should then unlock the page and return 0 | |
1031 | */ | |
1032 | int redirty_page_for_writepage(struct writeback_control *wbc, struct page *page) | |
1033 | { | |
1034 | wbc->pages_skipped++; | |
1035 | return __set_page_dirty_nobuffers(page); | |
1036 | } | |
1037 | EXPORT_SYMBOL(redirty_page_for_writepage); | |
1038 | ||
1039 | /* | |
1040 | * If the mapping doesn't provide a set_page_dirty a_op, then | |
1041 | * just fall through and assume that it wants buffer_heads. | |
1042 | */ | |
3e26c149 | 1043 | static int __set_page_dirty(struct page *page) |
1da177e4 LT |
1044 | { |
1045 | struct address_space *mapping = page_mapping(page); | |
1046 | ||
1047 | if (likely(mapping)) { | |
1048 | int (*spd)(struct page *) = mapping->a_ops->set_page_dirty; | |
9361401e DH |
1049 | #ifdef CONFIG_BLOCK |
1050 | if (!spd) | |
1051 | spd = __set_page_dirty_buffers; | |
1052 | #endif | |
1053 | return (*spd)(page); | |
1da177e4 | 1054 | } |
4741c9fd AM |
1055 | if (!PageDirty(page)) { |
1056 | if (!TestSetPageDirty(page)) | |
1057 | return 1; | |
1058 | } | |
1da177e4 LT |
1059 | return 0; |
1060 | } | |
3e26c149 PZ |
1061 | |
1062 | int fastcall set_page_dirty(struct page *page) | |
1063 | { | |
1064 | int ret = __set_page_dirty(page); | |
1065 | if (ret) | |
1066 | task_dirty_inc(current); | |
1067 | return ret; | |
1068 | } | |
1da177e4 LT |
1069 | EXPORT_SYMBOL(set_page_dirty); |
1070 | ||
1071 | /* | |
1072 | * set_page_dirty() is racy if the caller has no reference against | |
1073 | * page->mapping->host, and if the page is unlocked. This is because another | |
1074 | * CPU could truncate the page off the mapping and then free the mapping. | |
1075 | * | |
1076 | * Usually, the page _is_ locked, or the caller is a user-space process which | |
1077 | * holds a reference on the inode by having an open file. | |
1078 | * | |
1079 | * In other cases, the page should be locked before running set_page_dirty(). | |
1080 | */ | |
1081 | int set_page_dirty_lock(struct page *page) | |
1082 | { | |
1083 | int ret; | |
1084 | ||
db37648c | 1085 | lock_page_nosync(page); |
1da177e4 LT |
1086 | ret = set_page_dirty(page); |
1087 | unlock_page(page); | |
1088 | return ret; | |
1089 | } | |
1090 | EXPORT_SYMBOL(set_page_dirty_lock); | |
1091 | ||
1da177e4 LT |
1092 | /* |
1093 | * Clear a page's dirty flag, while caring for dirty memory accounting. | |
1094 | * Returns true if the page was previously dirty. | |
1095 | * | |
1096 | * This is for preparing to put the page under writeout. We leave the page | |
1097 | * tagged as dirty in the radix tree so that a concurrent write-for-sync | |
1098 | * can discover it via a PAGECACHE_TAG_DIRTY walk. The ->writepage | |
1099 | * implementation will run either set_page_writeback() or set_page_dirty(), | |
1100 | * at which stage we bring the page's dirty flag and radix-tree dirty tag | |
1101 | * back into sync. | |
1102 | * | |
1103 | * This incoherency between the page's dirty flag and radix-tree tag is | |
1104 | * unfortunate, but it only exists while the page is locked. | |
1105 | */ | |
1106 | int clear_page_dirty_for_io(struct page *page) | |
1107 | { | |
1108 | struct address_space *mapping = page_mapping(page); | |
1109 | ||
79352894 NP |
1110 | BUG_ON(!PageLocked(page)); |
1111 | ||
fe3cba17 | 1112 | ClearPageReclaim(page); |
7658cc28 LT |
1113 | if (mapping && mapping_cap_account_dirty(mapping)) { |
1114 | /* | |
1115 | * Yes, Virginia, this is indeed insane. | |
1116 | * | |
1117 | * We use this sequence to make sure that | |
1118 | * (a) we account for dirty stats properly | |
1119 | * (b) we tell the low-level filesystem to | |
1120 | * mark the whole page dirty if it was | |
1121 | * dirty in a pagetable. Only to then | |
1122 | * (c) clean the page again and return 1 to | |
1123 | * cause the writeback. | |
1124 | * | |
1125 | * This way we avoid all nasty races with the | |
1126 | * dirty bit in multiple places and clearing | |
1127 | * them concurrently from different threads. | |
1128 | * | |
1129 | * Note! Normally the "set_page_dirty(page)" | |
1130 | * has no effect on the actual dirty bit - since | |
1131 | * that will already usually be set. But we | |
1132 | * need the side effects, and it can help us | |
1133 | * avoid races. | |
1134 | * | |
1135 | * We basically use the page "master dirty bit" | |
1136 | * as a serialization point for all the different | |
1137 | * threads doing their things. | |
7658cc28 LT |
1138 | */ |
1139 | if (page_mkclean(page)) | |
1140 | set_page_dirty(page); | |
79352894 NP |
1141 | /* |
1142 | * We carefully synchronise fault handlers against | |
1143 | * installing a dirty pte and marking the page dirty | |
1144 | * at this point. We do this by having them hold the | |
1145 | * page lock at some point after installing their | |
1146 | * pte, but before marking the page dirty. | |
1147 | * Pages are always locked coming in here, so we get | |
1148 | * the desired exclusion. See mm/memory.c:do_wp_page() | |
1149 | * for more comments. | |
1150 | */ | |
7658cc28 | 1151 | if (TestClearPageDirty(page)) { |
8c08540f | 1152 | dec_zone_page_state(page, NR_FILE_DIRTY); |
c9e51e41 PZ |
1153 | dec_bdi_stat(mapping->backing_dev_info, |
1154 | BDI_RECLAIMABLE); | |
7658cc28 | 1155 | return 1; |
1da177e4 | 1156 | } |
7658cc28 | 1157 | return 0; |
1da177e4 | 1158 | } |
7658cc28 | 1159 | return TestClearPageDirty(page); |
1da177e4 | 1160 | } |
58bb01a9 | 1161 | EXPORT_SYMBOL(clear_page_dirty_for_io); |
1da177e4 LT |
1162 | |
1163 | int test_clear_page_writeback(struct page *page) | |
1164 | { | |
1165 | struct address_space *mapping = page_mapping(page); | |
1166 | int ret; | |
1167 | ||
1168 | if (mapping) { | |
69cb51d1 | 1169 | struct backing_dev_info *bdi = mapping->backing_dev_info; |
1da177e4 LT |
1170 | unsigned long flags; |
1171 | ||
1172 | write_lock_irqsave(&mapping->tree_lock, flags); | |
1173 | ret = TestClearPageWriteback(page); | |
69cb51d1 | 1174 | if (ret) { |
1da177e4 LT |
1175 | radix_tree_tag_clear(&mapping->page_tree, |
1176 | page_index(page), | |
1177 | PAGECACHE_TAG_WRITEBACK); | |
04fbfdc1 | 1178 | if (bdi_cap_writeback_dirty(bdi)) { |
69cb51d1 | 1179 | __dec_bdi_stat(bdi, BDI_WRITEBACK); |
04fbfdc1 PZ |
1180 | __bdi_writeout_inc(bdi); |
1181 | } | |
69cb51d1 | 1182 | } |
1da177e4 LT |
1183 | write_unlock_irqrestore(&mapping->tree_lock, flags); |
1184 | } else { | |
1185 | ret = TestClearPageWriteback(page); | |
1186 | } | |
d688abf5 AM |
1187 | if (ret) |
1188 | dec_zone_page_state(page, NR_WRITEBACK); | |
1da177e4 LT |
1189 | return ret; |
1190 | } | |
1191 | ||
1192 | int test_set_page_writeback(struct page *page) | |
1193 | { | |
1194 | struct address_space *mapping = page_mapping(page); | |
1195 | int ret; | |
1196 | ||
1197 | if (mapping) { | |
69cb51d1 | 1198 | struct backing_dev_info *bdi = mapping->backing_dev_info; |
1da177e4 LT |
1199 | unsigned long flags; |
1200 | ||
1201 | write_lock_irqsave(&mapping->tree_lock, flags); | |
1202 | ret = TestSetPageWriteback(page); | |
69cb51d1 | 1203 | if (!ret) { |
1da177e4 LT |
1204 | radix_tree_tag_set(&mapping->page_tree, |
1205 | page_index(page), | |
1206 | PAGECACHE_TAG_WRITEBACK); | |
69cb51d1 PZ |
1207 | if (bdi_cap_writeback_dirty(bdi)) |
1208 | __inc_bdi_stat(bdi, BDI_WRITEBACK); | |
1209 | } | |
1da177e4 LT |
1210 | if (!PageDirty(page)) |
1211 | radix_tree_tag_clear(&mapping->page_tree, | |
1212 | page_index(page), | |
1213 | PAGECACHE_TAG_DIRTY); | |
1214 | write_unlock_irqrestore(&mapping->tree_lock, flags); | |
1215 | } else { | |
1216 | ret = TestSetPageWriteback(page); | |
1217 | } | |
d688abf5 AM |
1218 | if (!ret) |
1219 | inc_zone_page_state(page, NR_WRITEBACK); | |
1da177e4 LT |
1220 | return ret; |
1221 | ||
1222 | } | |
1223 | EXPORT_SYMBOL(test_set_page_writeback); | |
1224 | ||
1225 | /* | |
00128188 | 1226 | * Return true if any of the pages in the mapping are marked with the |
1da177e4 LT |
1227 | * passed tag. |
1228 | */ | |
1229 | int mapping_tagged(struct address_space *mapping, int tag) | |
1230 | { | |
1da177e4 | 1231 | int ret; |
00128188 | 1232 | rcu_read_lock(); |
1da177e4 | 1233 | ret = radix_tree_tagged(&mapping->page_tree, tag); |
00128188 | 1234 | rcu_read_unlock(); |
1da177e4 LT |
1235 | return ret; |
1236 | } | |
1237 | EXPORT_SYMBOL(mapping_tagged); |