1
2
3
4
5
6
7
8
9
10
11
12
13#include <linux/kernel.h>
14#include <linux/module.h>
15#include <linux/spinlock.h>
16#include <linux/fs.h>
17#include <linux/mm.h>
18#include <linux/slab.h>
19#include <linux/pagemap.h>
20#include <linux/writeback.h>
21#include <linux/init.h>
22#include <linux/sysrq.h>
23#include <linux/backing-dev.h>
24#include <linux/mpage.h>
25#include <linux/notifier.h>
26#include <linux/smp.h>
27
28
29
30
31
32
33
34
35#define MAX_WRITEBACK_PAGES 1024
36
37
38
39
40
41static long ratelimit_pages = 32;
42
43
44
45
46static long total_pages;
47
48
49
50
51
52
53
54static inline long sync_writeback_pages(void)
55{
56 return ratelimit_pages + ratelimit_pages / 2;
57}
58
59
60
61
62
63
64
65
66
67
68int dirty_background_ratio = 10;
69
70
71
72
73int dirty_async_ratio = 40;
74
75
76
77
78
79int dirty_writeback_centisecs = 5 * 100;
80
81
82
83
84int dirty_expire_centisecs = 30 * 100;
85
86
87
88
89static void background_writeout(unsigned long _min_pages);
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104void balance_dirty_pages(struct address_space *mapping)
105{
106 struct page_state ps;
107 long background_thresh, async_thresh;
108 unsigned long dirty_and_writeback;
109 struct backing_dev_info *bdi;
110
111 get_page_state(&ps);
112 dirty_and_writeback = ps.nr_dirty + ps.nr_writeback;
113
114 background_thresh = (dirty_background_ratio * total_pages) / 100;
115 async_thresh = (dirty_async_ratio * total_pages) / 100;
116 bdi = mapping->backing_dev_info;
117
118 if (dirty_and_writeback > async_thresh) {
119 struct writeback_control wbc = {
120 .bdi = bdi,
121 .sync_mode = WB_SYNC_NONE,
122 .older_than_this = NULL,
123 .nr_to_write = sync_writeback_pages(),
124 };
125
126 writeback_inodes(&wbc);
127 get_page_state(&ps);
128 }
129
130 if (!writeback_in_progress(bdi) && ps.nr_dirty > background_thresh)
131 pdflush_operation(background_writeout, 0);
132}
133EXPORT_SYMBOL_GPL(balance_dirty_pages);
134
135
136
137
138
139
140
141
142
143
144
145void balance_dirty_pages_ratelimited(struct address_space *mapping)
146{
147 static struct rate_limit_struct {
148 int count;
149 } ____cacheline_aligned ratelimits[NR_CPUS];
150 int cpu;
151
152 cpu = get_cpu();
153 if (ratelimits[cpu].count++ >= ratelimit_pages) {
154 ratelimits[cpu].count = 0;
155 put_cpu();
156 balance_dirty_pages(mapping);
157 return;
158 }
159 put_cpu();
160}
161
162
163
164
165
166static void background_writeout(unsigned long _min_pages)
167{
168 long min_pages = _min_pages;
169 long background_thresh;
170 struct writeback_control wbc = {
171 .bdi = NULL,
172 .sync_mode = WB_SYNC_NONE,
173 .older_than_this = NULL,
174 .nr_to_write = 0,
175 };
176
177 CHECK_EMERGENCY_SYNC
178
179 background_thresh = (dirty_background_ratio * total_pages) / 100;
180
181 do {
182 struct page_state ps;
183 get_page_state(&ps);
184 if (ps.nr_dirty < background_thresh && min_pages <= 0)
185 break;
186 wbc.nr_to_write = MAX_WRITEBACK_PAGES;
187 writeback_inodes(&wbc);
188 min_pages -= MAX_WRITEBACK_PAGES - wbc.nr_to_write;
189 } while (wbc.nr_to_write <= 0);
190 blk_run_queues();
191}
192
193
194
195
196void wakeup_bdflush(void)
197{
198 struct page_state ps;
199
200 get_page_state(&ps);
201 pdflush_operation(background_writeout, ps.nr_dirty);
202}
203
204static struct timer_list wb_timer;
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221static void wb_kupdate(unsigned long arg)
222{
223 unsigned long oldest_jif;
224 unsigned long start_jif;
225 unsigned long next_jif;
226 struct page_state ps;
227 struct writeback_control wbc = {
228 .bdi = NULL,
229 .sync_mode = WB_SYNC_NONE,
230 .older_than_this = &oldest_jif,
231 .nr_to_write = 0,
232 };
233
234 sync_supers();
235 get_page_state(&ps);
236
237 oldest_jif = jiffies - (dirty_expire_centisecs * HZ) / 100;
238 start_jif = jiffies;
239 next_jif = start_jif + (dirty_writeback_centisecs * HZ) / 100;
240 wbc.nr_to_write = ps.nr_dirty;
241 writeback_inodes(&wbc);
242 blk_run_queues();
243 yield();
244
245 if (time_before(next_jif, jiffies + HZ))
246 next_jif = jiffies + HZ;
247 mod_timer(&wb_timer, next_jif);
248}
249
250static void wb_timer_fn(unsigned long unused)
251{
252 if (pdflush_operation(wb_kupdate, 0) < 0)
253 mod_timer(&wb_timer, jiffies + HZ);
254
255}
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274static void set_ratelimit(void)
275{
276 ratelimit_pages = total_pages / (num_online_cpus() * 32);
277 if (ratelimit_pages < 16)
278 ratelimit_pages = 16;
279 if (ratelimit_pages * PAGE_CACHE_SIZE > 4096 * 1024)
280 ratelimit_pages = (4096 * 1024) / PAGE_CACHE_SIZE;
281}
282
283static int
284ratelimit_handler(struct notifier_block *self, unsigned long u, void *v)
285{
286 set_ratelimit();
287 return 0;
288}
289
290static struct notifier_block ratelimit_nb = {
291 .notifier_call = ratelimit_handler,
292 .next = NULL,
293};
294
295
296
297
298
299
300static int __init page_writeback_init(void)
301{
302 long buffer_pages = nr_free_buffer_pages();
303 long correction;
304
305 total_pages = nr_free_pagecache_pages();
306
307 correction = (100 * 4 * buffer_pages) / total_pages;
308
309 if (correction < 100) {
310 dirty_background_ratio *= correction;
311 dirty_background_ratio /= 100;
312 dirty_async_ratio *= correction;
313 dirty_async_ratio /= 100;
314 }
315
316 init_timer(&wb_timer);
317 wb_timer.expires = jiffies + (dirty_writeback_centisecs * HZ) / 100;
318 wb_timer.data = 0;
319 wb_timer.function = wb_timer_fn;
320 add_timer(&wb_timer);
321 set_ratelimit();
322 register_cpu_notifier(&ratelimit_nb);
323 return 0;
324}
325module_init(page_writeback_init);
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351int generic_vm_writeback(struct page *page, struct writeback_control *wbc)
352{
353 struct inode *inode = page->mapping->host;
354
355
356
357
358
359 inode = igrab(inode);
360 unlock_page(page);
361
362 if (inode) {
363 do_writepages(inode->i_mapping, wbc);
364
365
366
367
368
369
370
371
372 iput(inode);
373#if 0
374 if (!PageWriteback(page) && PageDirty(page)) {
375 lock_page(page);
376 if (!PageWriteback(page)&&test_clear_page_dirty(page)) {
377 int ret;
378
379 ret = page->mapping->a_ops->writepage(page);
380 if (ret == -EAGAIN)
381 __set_page_dirty_nobuffers(page);
382 } else {
383 unlock_page(page);
384 }
385 }
386#endif
387 }
388 return 0;
389}
390EXPORT_SYMBOL(generic_vm_writeback);
391
392int do_writepages(struct address_space *mapping, struct writeback_control *wbc)
393{
394 if (mapping->a_ops->writepages)
395 return mapping->a_ops->writepages(mapping, wbc);
396 return generic_writepages(mapping, wbc);
397}
398
399
400
401
402
403
404
405
406
407
408
409int write_one_page(struct page *page, int wait)
410{
411 struct address_space *mapping = page->mapping;
412 int ret = 0;
413
414 BUG_ON(!PageLocked(page));
415
416 if (wait && PageWriteback(page))
417 wait_on_page_writeback(page);
418
419 write_lock(&mapping->page_lock);
420 list_del(&page->list);
421 if (test_clear_page_dirty(page)) {
422 list_add(&page->list, &mapping->locked_pages);
423 page_cache_get(page);
424 write_unlock(&mapping->page_lock);
425 ret = mapping->a_ops->writepage(page);
426 if (ret == -EAGAIN) {
427 __set_page_dirty_nobuffers(page);
428 ret = 0;
429 }
430 if (ret == 0 && wait) {
431 wait_on_page_writeback(page);
432 if (PageError(page))
433 ret = -EIO;
434 }
435 page_cache_release(page);
436 } else {
437 list_add(&page->list, &mapping->clean_pages);
438 write_unlock(&mapping->page_lock);
439 unlock_page(page);
440 }
441 return ret;
442}
443EXPORT_SYMBOL(write_one_page);
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481#include <linux/buffer_head.h>
482int __set_page_dirty_buffers(struct page *page)
483{
484 struct address_space * const mapping = page->mapping;
485 int ret = 0;
486
487 if (mapping == NULL) {
488 SetPageDirty(page);
489 goto out;
490 }
491
492 if (!PageUptodate(page))
493 buffer_error();
494
495 spin_lock(&mapping->private_lock);
496
497 if (page_has_buffers(page)) {
498 struct buffer_head *head = page_buffers(page);
499 struct buffer_head *bh = head;
500
501 do {
502 if (buffer_uptodate(bh))
503 set_buffer_dirty(bh);
504 else
505 buffer_error();
506 bh = bh->b_this_page;
507 } while (bh != head);
508 }
509
510 if (!TestSetPageDirty(page)) {
511 write_lock(&mapping->page_lock);
512 if (page->mapping) {
513 if (!mapping->backing_dev_info->memory_backed)
514 inc_page_state(nr_dirty);
515 list_del(&page->list);
516 list_add(&page->list, &mapping->dirty_pages);
517 }
518 write_unlock(&mapping->page_lock);
519 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
520 }
521
522 spin_unlock(&mapping->private_lock);
523out:
524 return ret;
525}
526EXPORT_SYMBOL(__set_page_dirty_buffers);
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541int __set_page_dirty_nobuffers(struct page *page)
542{
543 int ret = 0;
544
545 if (!TestSetPageDirty(page)) {
546 struct address_space *mapping = page->mapping;
547
548 if (mapping) {
549 write_lock(&mapping->page_lock);
550 if (page->mapping) {
551 if (!mapping->backing_dev_info->memory_backed)
552 inc_page_state(nr_dirty);
553 list_del(&page->list);
554 list_add(&page->list, &mapping->dirty_pages);
555 }
556 write_unlock(&mapping->page_lock);
557 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
558 }
559 }
560 return ret;
561}
562EXPORT_SYMBOL(__set_page_dirty_nobuffers);
563
564
565
566
567
568int test_clear_page_dirty(struct page *page)
569{
570 if (TestClearPageDirty(page)) {
571 struct address_space *mapping = page->mapping;
572
573 if (mapping && !mapping->backing_dev_info->memory_backed)
574 dec_page_state(nr_dirty);
575 return 1;
576 }
577 return 0;
578}
579