1
2
3
4
5
6
7
8
9
10
11
12
13
14#include <linux/kernel.h>
15#include <linux/export.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>
25#include <linux/task_io_accounting_ops.h>
26#include <linux/blkdev.h>
27#include <linux/mpage.h>
28#include <linux/rmap.h>
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>
35#include <linux/buffer_head.h>
36#include <linux/pagevec.h>
37#include <linux/timer.h>
38#include <trace/events/writeback.h>
39
40
41
42
43#define MAX_PAUSE max(HZ/5, 1)
44
45
46
47
48
49#define DIRTY_POLL_THRESH (128 >> (PAGE_SHIFT - 10))
50
51
52
53
54#define BANDWIDTH_INTERVAL max(HZ/5, 1)
55
56#define RATELIMIT_CALC_SHIFT 10
57
58
59
60
61
62static long ratelimit_pages = 32;
63
64
65
66
67
68
69int dirty_background_ratio = 10;
70
71
72
73
74
75unsigned long dirty_background_bytes;
76
77
78
79
80
81int vm_highmem_is_dirtyable;
82
83
84
85
86int vm_dirty_ratio = 20;
87
88
89
90
91
92unsigned long vm_dirty_bytes;
93
94
95
96
97unsigned int dirty_writeback_interval = 5 * 100;
98
99EXPORT_SYMBOL_GPL(dirty_writeback_interval);
100
101
102
103
104unsigned int dirty_expire_interval = 30 * 100;
105
106
107
108
109int block_dump;
110
111
112
113
114
115int laptop_mode;
116
117EXPORT_SYMBOL(laptop_mode);
118
119
120
121unsigned long global_dirty_limit;
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139static struct fprop_global writeout_completions;
140
141static void writeout_period(unsigned long t);
142
143static struct timer_list writeout_period_timer =
144 TIMER_DEFERRED_INITIALIZER(writeout_period, 0, 0);
145static unsigned long writeout_period_time = 0;
146
147
148
149
150
151
152#define VM_COMPLETIONS_PERIOD_LEN (3*HZ)
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190static unsigned long highmem_dirtyable_memory(unsigned long total)
191{
192#ifdef CONFIG_HIGHMEM
193 int node;
194 unsigned long x = 0;
195
196 for_each_node_state(node, N_HIGH_MEMORY) {
197 struct zone *z =
198 &NODE_DATA(node)->node_zones[ZONE_HIGHMEM];
199
200 x += zone_page_state(z, NR_FREE_PAGES) +
201 zone_reclaimable_pages(z) - z->dirty_balance_reserve;
202 }
203
204
205
206
207
208
209 return min(x, total);
210#else
211 return 0;
212#endif
213}
214
215
216
217
218
219
220
221static unsigned long global_dirtyable_memory(void)
222{
223 unsigned long x;
224
225 x = global_page_state(NR_FREE_PAGES) + global_reclaimable_pages() -
226 dirty_balance_reserve;
227
228 if (!vm_highmem_is_dirtyable)
229 x -= highmem_dirtyable_memory(x);
230
231 return x + 1;
232}
233
234
235
236
237
238
239
240
241
242
243void global_dirty_limits(unsigned long *pbackground, unsigned long *pdirty)
244{
245 unsigned long background;
246 unsigned long dirty;
247 unsigned long uninitialized_var(available_memory);
248 struct task_struct *tsk;
249
250 if (!vm_dirty_bytes || !dirty_background_bytes)
251 available_memory = global_dirtyable_memory();
252
253 if (vm_dirty_bytes)
254 dirty = DIV_ROUND_UP(vm_dirty_bytes, PAGE_SIZE);
255 else
256 dirty = (vm_dirty_ratio * available_memory) / 100;
257
258 if (dirty_background_bytes)
259 background = DIV_ROUND_UP(dirty_background_bytes, PAGE_SIZE);
260 else
261 background = (dirty_background_ratio * available_memory) / 100;
262
263 if (background >= dirty)
264 background = dirty / 2;
265 tsk = current;
266 if (tsk->flags & PF_LESS_THROTTLE || rt_task(tsk)) {
267 background += background / 4;
268 dirty += dirty / 4;
269 }
270 *pbackground = background;
271 *pdirty = dirty;
272 trace_global_dirty_state(background, dirty);
273}
274
275
276
277
278
279
280
281
282static unsigned long zone_dirtyable_memory(struct zone *zone)
283{
284
285
286
287
288
289
290
291
292
293 return zone_page_state(zone, NR_FREE_PAGES) +
294 zone_reclaimable_pages(zone) -
295 zone->dirty_balance_reserve;
296}
297
298
299
300
301
302
303
304
305static unsigned long zone_dirty_limit(struct zone *zone)
306{
307 unsigned long zone_memory = zone_dirtyable_memory(zone);
308 struct task_struct *tsk = current;
309 unsigned long dirty;
310
311 if (vm_dirty_bytes)
312 dirty = DIV_ROUND_UP(vm_dirty_bytes, PAGE_SIZE) *
313 zone_memory / global_dirtyable_memory();
314 else
315 dirty = vm_dirty_ratio * zone_memory / 100;
316
317 if (tsk->flags & PF_LESS_THROTTLE || rt_task(tsk))
318 dirty += dirty / 4;
319
320 return dirty;
321}
322
323
324
325
326
327
328
329
330bool zone_dirty_ok(struct zone *zone)
331{
332 unsigned long limit = zone_dirty_limit(zone);
333
334 return zone_page_state(zone, NR_FILE_DIRTY) +
335 zone_page_state(zone, NR_UNSTABLE_NFS) +
336 zone_page_state(zone, NR_WRITEBACK) <= limit;
337}
338
339int dirty_background_ratio_handler(struct ctl_table *table, int write,
340 void __user *buffer, size_t *lenp,
341 loff_t *ppos)
342{
343 int ret;
344
345 ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
346 if (ret == 0 && write)
347 dirty_background_bytes = 0;
348 return ret;
349}
350
351int dirty_background_bytes_handler(struct ctl_table *table, int write,
352 void __user *buffer, size_t *lenp,
353 loff_t *ppos)
354{
355 int ret;
356
357 ret = proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
358 if (ret == 0 && write)
359 dirty_background_ratio = 0;
360 return ret;
361}
362
363int dirty_ratio_handler(struct ctl_table *table, int write,
364 void __user *buffer, size_t *lenp,
365 loff_t *ppos)
366{
367 int old_ratio = vm_dirty_ratio;
368 int ret;
369
370 ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
371 if (ret == 0 && write && vm_dirty_ratio != old_ratio) {
372 writeback_set_ratelimit();
373 vm_dirty_bytes = 0;
374 }
375 return ret;
376}
377
378int dirty_bytes_handler(struct ctl_table *table, int write,
379 void __user *buffer, size_t *lenp,
380 loff_t *ppos)
381{
382 unsigned long old_bytes = vm_dirty_bytes;
383 int ret;
384
385 ret = proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
386 if (ret == 0 && write && vm_dirty_bytes != old_bytes) {
387 writeback_set_ratelimit();
388 vm_dirty_ratio = 0;
389 }
390 return ret;
391}
392
393static unsigned long wp_next_time(unsigned long cur_time)
394{
395 cur_time += VM_COMPLETIONS_PERIOD_LEN;
396
397 if (!cur_time)
398 return 1;
399 return cur_time;
400}
401
402
403
404
405
406static inline void __bdi_writeout_inc(struct backing_dev_info *bdi)
407{
408 __inc_bdi_stat(bdi, BDI_WRITTEN);
409 __fprop_inc_percpu_max(&writeout_completions, &bdi->completions,
410 bdi->max_prop_frac);
411
412 if (!unlikely(writeout_period_time)) {
413
414
415
416
417
418
419 writeout_period_time = wp_next_time(jiffies);
420 mod_timer(&writeout_period_timer, writeout_period_time);
421 }
422}
423
424void bdi_writeout_inc(struct backing_dev_info *bdi)
425{
426 unsigned long flags;
427
428 local_irq_save(flags);
429 __bdi_writeout_inc(bdi);
430 local_irq_restore(flags);
431}
432EXPORT_SYMBOL_GPL(bdi_writeout_inc);
433
434
435
436
437static void bdi_writeout_fraction(struct backing_dev_info *bdi,
438 long *numerator, long *denominator)
439{
440 fprop_fraction_percpu(&writeout_completions, &bdi->completions,
441 numerator, denominator);
442}
443
444
445
446
447
448static void writeout_period(unsigned long t)
449{
450 int miss_periods = (jiffies - writeout_period_time) /
451 VM_COMPLETIONS_PERIOD_LEN;
452
453 if (fprop_new_period(&writeout_completions, miss_periods + 1)) {
454 writeout_period_time = wp_next_time(writeout_period_time +
455 miss_periods * VM_COMPLETIONS_PERIOD_LEN);
456 mod_timer(&writeout_period_timer, writeout_period_time);
457 } else {
458
459
460
461
462 writeout_period_time = 0;
463 }
464}
465
466
467
468
469
470
471static unsigned int bdi_min_ratio;
472
473int bdi_set_min_ratio(struct backing_dev_info *bdi, unsigned int min_ratio)
474{
475 int ret = 0;
476
477 spin_lock_bh(&bdi_lock);
478 if (min_ratio > bdi->max_ratio) {
479 ret = -EINVAL;
480 } else {
481 min_ratio -= bdi->min_ratio;
482 if (bdi_min_ratio + min_ratio < 100) {
483 bdi_min_ratio += min_ratio;
484 bdi->min_ratio += min_ratio;
485 } else {
486 ret = -EINVAL;
487 }
488 }
489 spin_unlock_bh(&bdi_lock);
490
491 return ret;
492}
493
494int bdi_set_max_ratio(struct backing_dev_info *bdi, unsigned max_ratio)
495{
496 int ret = 0;
497
498 if (max_ratio > 100)
499 return -EINVAL;
500
501 spin_lock_bh(&bdi_lock);
502 if (bdi->min_ratio > max_ratio) {
503 ret = -EINVAL;
504 } else {
505 bdi->max_ratio = max_ratio;
506 bdi->max_prop_frac = (FPROP_FRAC_BASE * max_ratio) / 100;
507 }
508 spin_unlock_bh(&bdi_lock);
509
510 return ret;
511}
512EXPORT_SYMBOL(bdi_set_max_ratio);
513
514static unsigned long dirty_freerun_ceiling(unsigned long thresh,
515 unsigned long bg_thresh)
516{
517 return (thresh + bg_thresh) / 2;
518}
519
520static unsigned long hard_dirty_limit(unsigned long thresh)
521{
522 return max(thresh, global_dirty_limit);
523}
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547unsigned long bdi_dirty_limit(struct backing_dev_info *bdi, unsigned long dirty)
548{
549 u64 bdi_dirty;
550 long numerator, denominator;
551
552
553
554
555 bdi_writeout_fraction(bdi, &numerator, &denominator);
556
557 bdi_dirty = (dirty * (100 - bdi_min_ratio)) / 100;
558 bdi_dirty *= numerator;
559 do_div(bdi_dirty, denominator);
560
561 bdi_dirty += (dirty * bdi->min_ratio) / 100;
562 if (bdi_dirty > (dirty * bdi->max_ratio) / 100)
563 bdi_dirty = dirty * bdi->max_ratio / 100;
564
565 return bdi_dirty;
566}
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643static unsigned long bdi_position_ratio(struct backing_dev_info *bdi,
644 unsigned long thresh,
645 unsigned long bg_thresh,
646 unsigned long dirty,
647 unsigned long bdi_thresh,
648 unsigned long bdi_dirty)
649{
650 unsigned long write_bw = bdi->avg_write_bandwidth;
651 unsigned long freerun = dirty_freerun_ceiling(thresh, bg_thresh);
652 unsigned long limit = hard_dirty_limit(thresh);
653 unsigned long x_intercept;
654 unsigned long setpoint;
655 unsigned long bdi_setpoint;
656 unsigned long span;
657 long long pos_ratio;
658 long x;
659
660 if (unlikely(dirty >= limit))
661 return 0;
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679 setpoint = (freerun + limit) / 2;
680 x = div_s64((setpoint - dirty) << RATELIMIT_CALC_SHIFT,
681 limit - setpoint + 1);
682 pos_ratio = x;
683 pos_ratio = pos_ratio * x >> RATELIMIT_CALC_SHIFT;
684 pos_ratio = pos_ratio * x >> RATELIMIT_CALC_SHIFT;
685 pos_ratio += 1 << RATELIMIT_CALC_SHIFT;
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718 if (unlikely(bdi_thresh > thresh))
719 bdi_thresh = thresh;
720
721
722
723
724
725
726
727 bdi_thresh = max(bdi_thresh, (limit - dirty) / 8);
728
729
730
731
732 x = div_u64((u64)bdi_thresh << 16, thresh + 1);
733 bdi_setpoint = setpoint * (u64)x >> 16;
734
735
736
737
738
739
740
741
742 span = (thresh - bdi_thresh + 8 * write_bw) * (u64)x >> 16;
743 x_intercept = bdi_setpoint + span;
744
745 if (bdi_dirty < x_intercept - span / 4) {
746 pos_ratio = div_u64(pos_ratio * (x_intercept - bdi_dirty),
747 x_intercept - bdi_setpoint + 1);
748 } else
749 pos_ratio /= 4;
750
751
752
753
754
755
756 x_intercept = bdi_thresh / 2;
757 if (bdi_dirty < x_intercept) {
758 if (bdi_dirty > x_intercept / 8)
759 pos_ratio = div_u64(pos_ratio * x_intercept, bdi_dirty);
760 else
761 pos_ratio *= 8;
762 }
763
764 return pos_ratio;
765}
766
767static void bdi_update_write_bandwidth(struct backing_dev_info *bdi,
768 unsigned long elapsed,
769 unsigned long written)
770{
771 const unsigned long period = roundup_pow_of_two(3 * HZ);
772 unsigned long avg = bdi->avg_write_bandwidth;
773 unsigned long old = bdi->write_bandwidth;
774 u64 bw;
775
776
777
778
779
780
781
782
783 bw = written - bdi->written_stamp;
784 bw *= HZ;
785 if (unlikely(elapsed > period)) {
786 do_div(bw, elapsed);
787 avg = bw;
788 goto out;
789 }
790 bw += (u64)bdi->write_bandwidth * (period - elapsed);
791 bw >>= ilog2(period);
792
793
794
795
796 if (avg > old && old >= (unsigned long)bw)
797 avg -= (avg - old) >> 3;
798
799 if (avg < old && old <= (unsigned long)bw)
800 avg += (old - avg) >> 3;
801
802out:
803 bdi->write_bandwidth = bw;
804 bdi->avg_write_bandwidth = avg;
805}
806
807
808
809
810
811
812
813
814
815static void update_dirty_limit(unsigned long thresh, unsigned long dirty)
816{
817 unsigned long limit = global_dirty_limit;
818
819
820
821
822 if (limit < thresh) {
823 limit = thresh;
824 goto update;
825 }
826
827
828
829
830
831
832 thresh = max(thresh, dirty);
833 if (limit > thresh) {
834 limit -= (limit - thresh) >> 5;
835 goto update;
836 }
837 return;
838update:
839 global_dirty_limit = limit;
840}
841
842static void global_update_bandwidth(unsigned long thresh,
843 unsigned long dirty,
844 unsigned long now)
845{
846 static DEFINE_SPINLOCK(dirty_lock);
847 static unsigned long update_time;
848
849
850
851
852 if (time_before(now, update_time + BANDWIDTH_INTERVAL))
853 return;
854
855 spin_lock(&dirty_lock);
856 if (time_after_eq(now, update_time + BANDWIDTH_INTERVAL)) {
857 update_dirty_limit(thresh, dirty);
858 update_time = now;
859 }
860 spin_unlock(&dirty_lock);
861}
862
863
864
865
866
867
868
869static void bdi_update_dirty_ratelimit(struct backing_dev_info *bdi,
870 unsigned long thresh,
871 unsigned long bg_thresh,
872 unsigned long dirty,
873 unsigned long bdi_thresh,
874 unsigned long bdi_dirty,
875 unsigned long dirtied,
876 unsigned long elapsed)
877{
878 unsigned long freerun = dirty_freerun_ceiling(thresh, bg_thresh);
879 unsigned long limit = hard_dirty_limit(thresh);
880 unsigned long setpoint = (freerun + limit) / 2;
881 unsigned long write_bw = bdi->avg_write_bandwidth;
882 unsigned long dirty_ratelimit = bdi->dirty_ratelimit;
883 unsigned long dirty_rate;
884 unsigned long task_ratelimit;
885 unsigned long balanced_dirty_ratelimit;
886 unsigned long pos_ratio;
887 unsigned long step;
888 unsigned long x;
889
890
891
892
893
894 dirty_rate = (dirtied - bdi->dirtied_stamp) * HZ / elapsed;
895
896 pos_ratio = bdi_position_ratio(bdi, thresh, bg_thresh, dirty,
897 bdi_thresh, bdi_dirty);
898
899
900
901 task_ratelimit = (u64)dirty_ratelimit *
902 pos_ratio >> RATELIMIT_CALC_SHIFT;
903 task_ratelimit++;
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935 balanced_dirty_ratelimit = div_u64((u64)task_ratelimit * write_bw,
936 dirty_rate | 1);
937
938
939
940 if (unlikely(balanced_dirty_ratelimit > write_bw))
941 balanced_dirty_ratelimit = write_bw;
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977 step = 0;
978 if (dirty < setpoint) {
979 x = min(bdi->balanced_dirty_ratelimit,
980 min(balanced_dirty_ratelimit, task_ratelimit));
981 if (dirty_ratelimit < x)
982 step = x - dirty_ratelimit;
983 } else {
984 x = max(bdi->balanced_dirty_ratelimit,
985 max(balanced_dirty_ratelimit, task_ratelimit));
986 if (dirty_ratelimit > x)
987 step = dirty_ratelimit - x;
988 }
989
990
991
992
993
994
995 step >>= dirty_ratelimit / (2 * step + 1);
996
997
998
999 step = (step + 7) / 8;
1000
1001 if (dirty_ratelimit < balanced_dirty_ratelimit)
1002 dirty_ratelimit += step;
1003 else
1004 dirty_ratelimit -= step;
1005
1006 bdi->dirty_ratelimit = max(dirty_ratelimit, 1UL);
1007 bdi->balanced_dirty_ratelimit = balanced_dirty_ratelimit;
1008
1009 trace_bdi_dirty_ratelimit(bdi, dirty_rate, task_ratelimit);
1010}
1011
1012void __bdi_update_bandwidth(struct backing_dev_info *bdi,
1013 unsigned long thresh,
1014 unsigned long bg_thresh,
1015 unsigned long dirty,
1016 unsigned long bdi_thresh,
1017 unsigned long bdi_dirty,
1018 unsigned long start_time)
1019{
1020 unsigned long now = jiffies;
1021 unsigned long elapsed = now - bdi->bw_time_stamp;
1022 unsigned long dirtied;
1023 unsigned long written;
1024
1025
1026
1027
1028 if (elapsed < BANDWIDTH_INTERVAL)
1029 return;
1030
1031 dirtied = percpu_counter_read(&bdi->bdi_stat[BDI_DIRTIED]);
1032 written = percpu_counter_read(&bdi->bdi_stat[BDI_WRITTEN]);
1033
1034
1035
1036
1037
1038 if (elapsed > HZ && time_before(bdi->bw_time_stamp, start_time))
1039 goto snapshot;
1040
1041 if (thresh) {
1042 global_update_bandwidth(thresh, dirty, now);
1043 bdi_update_dirty_ratelimit(bdi, thresh, bg_thresh, dirty,
1044 bdi_thresh, bdi_dirty,
1045 dirtied, elapsed);
1046 }
1047 bdi_update_write_bandwidth(bdi, elapsed, written);
1048
1049snapshot:
1050 bdi->dirtied_stamp = dirtied;
1051 bdi->written_stamp = written;
1052 bdi->bw_time_stamp = now;
1053}
1054
1055static void bdi_update_bandwidth(struct backing_dev_info *bdi,
1056 unsigned long thresh,
1057 unsigned long bg_thresh,
1058 unsigned long dirty,
1059 unsigned long bdi_thresh,
1060 unsigned long bdi_dirty,
1061 unsigned long start_time)
1062{
1063 if (time_is_after_eq_jiffies(bdi->bw_time_stamp + BANDWIDTH_INTERVAL))
1064 return;
1065 spin_lock(&bdi->wb.list_lock);
1066 __bdi_update_bandwidth(bdi, thresh, bg_thresh, dirty,
1067 bdi_thresh, bdi_dirty, start_time);
1068 spin_unlock(&bdi->wb.list_lock);
1069}
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079static unsigned long dirty_poll_interval(unsigned long dirty,
1080 unsigned long thresh)
1081{
1082 if (thresh > dirty)
1083 return 1UL << (ilog2(thresh - dirty) >> 1);
1084
1085 return 1;
1086}
1087
1088static long bdi_max_pause(struct backing_dev_info *bdi,
1089 unsigned long bdi_dirty)
1090{
1091 long bw = bdi->avg_write_bandwidth;
1092 long t;
1093
1094
1095
1096
1097
1098
1099
1100
1101 t = bdi_dirty / (1 + bw / roundup_pow_of_two(1 + HZ / 8));
1102 t++;
1103
1104 return min_t(long, t, MAX_PAUSE);
1105}
1106
1107static long bdi_min_pause(struct backing_dev_info *bdi,
1108 long max_pause,
1109 unsigned long task_ratelimit,
1110 unsigned long dirty_ratelimit,
1111 int *nr_dirtied_pause)
1112{
1113 long hi = ilog2(bdi->avg_write_bandwidth);
1114 long lo = ilog2(bdi->dirty_ratelimit);
1115 long t;
1116 long pause;
1117 int pages;
1118
1119
1120 t = max(1, HZ / 100);
1121
1122
1123
1124
1125
1126
1127
1128 if (hi > lo)
1129 t += (hi - lo) * (10 * HZ) / 1024;
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149 t = min(t, 1 + max_pause / 2);
1150 pages = dirty_ratelimit * t / roundup_pow_of_two(HZ);
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160 if (pages < DIRTY_POLL_THRESH) {
1161 t = max_pause;
1162 pages = dirty_ratelimit * t / roundup_pow_of_two(HZ);
1163 if (pages > DIRTY_POLL_THRESH) {
1164 pages = DIRTY_POLL_THRESH;
1165 t = HZ * DIRTY_POLL_THRESH / dirty_ratelimit;
1166 }
1167 }
1168
1169 pause = HZ * pages / (task_ratelimit + 1);
1170 if (pause > max_pause) {
1171 t = max_pause;
1172 pages = task_ratelimit * t / roundup_pow_of_two(HZ);
1173 }
1174
1175 *nr_dirtied_pause = pages;
1176
1177
1178
1179 return pages >= DIRTY_POLL_THRESH ? 1 + t / 2 : t;
1180}
1181
1182
1183
1184
1185
1186
1187
1188
1189static void balance_dirty_pages(struct address_space *mapping,
1190 unsigned long pages_dirtied)
1191{
1192 unsigned long nr_reclaimable;
1193 unsigned long bdi_reclaimable;
1194 unsigned long nr_dirty;
1195 unsigned long bdi_dirty;
1196 unsigned long freerun;
1197 unsigned long background_thresh;
1198 unsigned long dirty_thresh;
1199 unsigned long bdi_thresh;
1200 long period;
1201 long pause;
1202 long max_pause;
1203 long min_pause;
1204 int nr_dirtied_pause;
1205 bool dirty_exceeded = false;
1206 unsigned long task_ratelimit;
1207 unsigned long dirty_ratelimit;
1208 unsigned long pos_ratio;
1209 struct backing_dev_info *bdi = mapping->backing_dev_info;
1210 unsigned long start_time = jiffies;
1211
1212 for (;;) {
1213 unsigned long now = jiffies;
1214
1215
1216
1217
1218
1219
1220
1221 nr_reclaimable = global_page_state(NR_FILE_DIRTY) +
1222 global_page_state(NR_UNSTABLE_NFS);
1223 nr_dirty = nr_reclaimable + global_page_state(NR_WRITEBACK);
1224
1225 global_dirty_limits(&background_thresh, &dirty_thresh);
1226
1227
1228
1229
1230
1231
1232 freerun = dirty_freerun_ceiling(dirty_thresh,
1233 background_thresh);
1234 if (nr_dirty <= freerun) {
1235 current->dirty_paused_when = now;
1236 current->nr_dirtied = 0;
1237 current->nr_dirtied_pause =
1238 dirty_poll_interval(nr_dirty, dirty_thresh);
1239 break;
1240 }
1241
1242 if (unlikely(!writeback_in_progress(bdi)))
1243 bdi_start_background_writeback(bdi);
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258 bdi_thresh = bdi_dirty_limit(bdi, dirty_thresh);
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270 if (bdi_thresh < 2 * bdi_stat_error(bdi)) {
1271 bdi_reclaimable = bdi_stat_sum(bdi, BDI_RECLAIMABLE);
1272 bdi_dirty = bdi_reclaimable +
1273 bdi_stat_sum(bdi, BDI_WRITEBACK);
1274 } else {
1275 bdi_reclaimable = bdi_stat(bdi, BDI_RECLAIMABLE);
1276 bdi_dirty = bdi_reclaimable +
1277 bdi_stat(bdi, BDI_WRITEBACK);
1278 }
1279
1280 dirty_exceeded = (bdi_dirty > bdi_thresh) &&
1281 (nr_dirty > dirty_thresh);
1282 if (dirty_exceeded && !bdi->dirty_exceeded)
1283 bdi->dirty_exceeded = 1;
1284
1285 bdi_update_bandwidth(bdi, dirty_thresh, background_thresh,
1286 nr_dirty, bdi_thresh, bdi_dirty,
1287 start_time);
1288
1289 dirty_ratelimit = bdi->dirty_ratelimit;
1290 pos_ratio = bdi_position_ratio(bdi, dirty_thresh,
1291 background_thresh, nr_dirty,
1292 bdi_thresh, bdi_dirty);
1293 task_ratelimit = ((u64)dirty_ratelimit * pos_ratio) >>
1294 RATELIMIT_CALC_SHIFT;
1295 max_pause = bdi_max_pause(bdi, bdi_dirty);
1296 min_pause = bdi_min_pause(bdi, max_pause,
1297 task_ratelimit, dirty_ratelimit,
1298 &nr_dirtied_pause);
1299
1300 if (unlikely(task_ratelimit == 0)) {
1301 period = max_pause;
1302 pause = max_pause;
1303 goto pause;
1304 }
1305 period = HZ * pages_dirtied / task_ratelimit;
1306 pause = period;
1307 if (current->dirty_paused_when)
1308 pause -= now - current->dirty_paused_when;
1309
1310
1311
1312
1313
1314
1315
1316 if (pause < min_pause) {
1317 trace_balance_dirty_pages(bdi,
1318 dirty_thresh,
1319 background_thresh,
1320 nr_dirty,
1321 bdi_thresh,
1322 bdi_dirty,
1323 dirty_ratelimit,
1324 task_ratelimit,
1325 pages_dirtied,
1326 period,
1327 min(pause, 0L),
1328 start_time);
1329 if (pause < -HZ) {
1330 current->dirty_paused_when = now;
1331 current->nr_dirtied = 0;
1332 } else if (period) {
1333 current->dirty_paused_when += period;
1334 current->nr_dirtied = 0;
1335 } else if (current->nr_dirtied_pause <= pages_dirtied)
1336 current->nr_dirtied_pause += pages_dirtied;
1337 break;
1338 }
1339 if (unlikely(pause > max_pause)) {
1340
1341 now += min(pause - max_pause, max_pause);
1342 pause = max_pause;
1343 }
1344
1345pause:
1346 trace_balance_dirty_pages(bdi,
1347 dirty_thresh,
1348 background_thresh,
1349 nr_dirty,
1350 bdi_thresh,
1351 bdi_dirty,
1352 dirty_ratelimit,
1353 task_ratelimit,
1354 pages_dirtied,
1355 period,
1356 pause,
1357 start_time);
1358 __set_current_state(TASK_KILLABLE);
1359 io_schedule_timeout(pause);
1360
1361 current->dirty_paused_when = now + pause;
1362 current->nr_dirtied = 0;
1363 current->nr_dirtied_pause = nr_dirtied_pause;
1364
1365
1366
1367
1368
1369 if (task_ratelimit)
1370 break;
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382 if (bdi_dirty <= bdi_stat_error(bdi))
1383 break;
1384
1385 if (fatal_signal_pending(current))
1386 break;
1387 }
1388
1389 if (!dirty_exceeded && bdi->dirty_exceeded)
1390 bdi->dirty_exceeded = 0;
1391
1392 if (writeback_in_progress(bdi))
1393 return;
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403 if (laptop_mode)
1404 return;
1405
1406 if (nr_reclaimable > background_thresh)
1407 bdi_start_background_writeback(bdi);
1408}
1409
1410void set_page_dirty_balance(struct page *page, int page_mkwrite)
1411{
1412 if (set_page_dirty(page) || page_mkwrite) {
1413 struct address_space *mapping = page_mapping(page);
1414
1415 if (mapping)
1416 balance_dirty_pages_ratelimited(mapping);
1417 }
1418}
1419
1420static DEFINE_PER_CPU(int, bdp_ratelimits);
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436DEFINE_PER_CPU(int, dirty_throttle_leaks) = 0;
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452void balance_dirty_pages_ratelimited_nr(struct address_space *mapping,
1453 unsigned long nr_pages_dirtied)
1454{
1455 struct backing_dev_info *bdi = mapping->backing_dev_info;
1456 int ratelimit;
1457 int *p;
1458
1459 if (!bdi_cap_account_dirty(bdi))
1460 return;
1461
1462 ratelimit = current->nr_dirtied_pause;
1463 if (bdi->dirty_exceeded)
1464 ratelimit = min(ratelimit, 32 >> (PAGE_SHIFT - 10));
1465
1466 preempt_disable();
1467
1468
1469
1470
1471
1472
1473 p = &__get_cpu_var(bdp_ratelimits);
1474 if (unlikely(current->nr_dirtied >= ratelimit))
1475 *p = 0;
1476 else if (unlikely(*p >= ratelimit_pages)) {
1477 *p = 0;
1478 ratelimit = 0;
1479 }
1480
1481
1482
1483
1484
1485 p = &__get_cpu_var(dirty_throttle_leaks);
1486 if (*p > 0 && current->nr_dirtied < ratelimit) {
1487 nr_pages_dirtied = min(*p, ratelimit - current->nr_dirtied);
1488 *p -= nr_pages_dirtied;
1489 current->nr_dirtied += nr_pages_dirtied;
1490 }
1491 preempt_enable();
1492
1493 if (unlikely(current->nr_dirtied >= ratelimit))
1494 balance_dirty_pages(mapping, current->nr_dirtied);
1495}
1496EXPORT_SYMBOL(balance_dirty_pages_ratelimited_nr);
1497
1498void throttle_vm_writeout(gfp_t gfp_mask)
1499{
1500 unsigned long background_thresh;
1501 unsigned long dirty_thresh;
1502
1503 for ( ; ; ) {
1504 global_dirty_limits(&background_thresh, &dirty_thresh);
1505 dirty_thresh = hard_dirty_limit(dirty_thresh);
1506
1507
1508
1509
1510
1511 dirty_thresh += dirty_thresh / 10;
1512
1513 if (global_page_state(NR_UNSTABLE_NFS) +
1514 global_page_state(NR_WRITEBACK) <= dirty_thresh)
1515 break;
1516 congestion_wait(BLK_RW_ASYNC, HZ/10);
1517
1518
1519
1520
1521
1522
1523 if ((gfp_mask & (__GFP_FS|__GFP_IO)) != (__GFP_FS|__GFP_IO))
1524 break;
1525 }
1526}
1527
1528
1529
1530
1531int dirty_writeback_centisecs_handler(ctl_table *table, int write,
1532 void __user *buffer, size_t *length, loff_t *ppos)
1533{
1534 proc_dointvec(table, write, buffer, length, ppos);
1535 return 0;
1536}
1537
1538#ifdef CONFIG_BLOCK
1539void laptop_mode_timer_fn(unsigned long data)
1540{
1541 struct request_queue *q = (struct request_queue *)data;
1542 int nr_pages = global_page_state(NR_FILE_DIRTY) +
1543 global_page_state(NR_UNSTABLE_NFS);
1544
1545
1546
1547
1548
1549 if (bdi_has_dirty_io(&q->backing_dev_info))
1550 bdi_start_writeback(&q->backing_dev_info, nr_pages,
1551 WB_REASON_LAPTOP_TIMER);
1552}
1553
1554
1555
1556
1557
1558
1559void laptop_io_completion(struct backing_dev_info *info)
1560{
1561 mod_timer(&info->laptop_mode_wb_timer, jiffies + laptop_mode);
1562}
1563
1564
1565
1566
1567
1568
1569void laptop_sync_completion(void)
1570{
1571 struct backing_dev_info *bdi;
1572
1573 rcu_read_lock();
1574
1575 list_for_each_entry_rcu(bdi, &bdi_list, bdi_list)
1576 del_timer(&bdi->laptop_mode_wb_timer);
1577
1578 rcu_read_unlock();
1579}
1580#endif
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593void writeback_set_ratelimit(void)
1594{
1595 unsigned long background_thresh;
1596 unsigned long dirty_thresh;
1597 global_dirty_limits(&background_thresh, &dirty_thresh);
1598 global_dirty_limit = dirty_thresh;
1599 ratelimit_pages = dirty_thresh / (num_online_cpus() * 32);
1600 if (ratelimit_pages < 16)
1601 ratelimit_pages = 16;
1602}
1603
1604static int __cpuinit
1605ratelimit_handler(struct notifier_block *self, unsigned long u, void *v)
1606{
1607 writeback_set_ratelimit();
1608 return NOTIFY_DONE;
1609}
1610
1611static struct notifier_block __cpuinitdata ratelimit_nb = {
1612 .notifier_call = ratelimit_handler,
1613 .next = NULL,
1614};
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634void __init page_writeback_init(void)
1635{
1636 writeback_set_ratelimit();
1637 register_cpu_notifier(&ratelimit_nb);
1638
1639 fprop_global_init(&writeout_completions);
1640}
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659void tag_pages_for_writeback(struct address_space *mapping,
1660 pgoff_t start, pgoff_t end)
1661{
1662#define WRITEBACK_TAG_BATCH 4096
1663 unsigned long tagged;
1664
1665 do {
1666 spin_lock_irq(&mapping->tree_lock);
1667 tagged = radix_tree_range_tag_if_tagged(&mapping->page_tree,
1668 &start, end, WRITEBACK_TAG_BATCH,
1669 PAGECACHE_TAG_DIRTY, PAGECACHE_TAG_TOWRITE);
1670 spin_unlock_irq(&mapping->tree_lock);
1671 WARN_ON_ONCE(tagged > WRITEBACK_TAG_BATCH);
1672 cond_resched();
1673
1674 } while (tagged >= WRITEBACK_TAG_BATCH && start);
1675}
1676EXPORT_SYMBOL(tag_pages_for_writeback);
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700int write_cache_pages(struct address_space *mapping,
1701 struct writeback_control *wbc, writepage_t writepage,
1702 void *data)
1703{
1704 int ret = 0;
1705 int done = 0;
1706 struct pagevec pvec;
1707 int nr_pages;
1708 pgoff_t uninitialized_var(writeback_index);
1709 pgoff_t index;
1710 pgoff_t end;
1711 pgoff_t done_index;
1712 int cycled;
1713 int range_whole = 0;
1714 int tag;
1715
1716 pagevec_init(&pvec, 0);
1717 if (wbc->range_cyclic) {
1718 writeback_index = mapping->writeback_index;
1719 index = writeback_index;
1720 if (index == 0)
1721 cycled = 1;
1722 else
1723 cycled = 0;
1724 end = -1;
1725 } else {
1726 index = wbc->range_start >> PAGE_CACHE_SHIFT;
1727 end = wbc->range_end >> PAGE_CACHE_SHIFT;
1728 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
1729 range_whole = 1;
1730 cycled = 1;
1731 }
1732 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
1733 tag = PAGECACHE_TAG_TOWRITE;
1734 else
1735 tag = PAGECACHE_TAG_DIRTY;
1736retry:
1737 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
1738 tag_pages_for_writeback(mapping, index, end);
1739 done_index = index;
1740 while (!done && (index <= end)) {
1741 int i;
1742
1743 nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
1744 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
1745 if (nr_pages == 0)
1746 break;
1747
1748 for (i = 0; i < nr_pages; i++) {
1749 struct page *page = pvec.pages[i];
1750
1751
1752
1753
1754
1755
1756
1757
1758 if (page->index > end) {
1759
1760
1761
1762
1763 done = 1;
1764 break;
1765 }
1766
1767 done_index = page->index;
1768
1769 lock_page(page);
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779 if (unlikely(page->mapping != mapping)) {
1780continue_unlock:
1781 unlock_page(page);
1782 continue;
1783 }
1784
1785 if (!PageDirty(page)) {
1786
1787 goto continue_unlock;
1788 }
1789
1790 if (PageWriteback(page)) {
1791 if (wbc->sync_mode != WB_SYNC_NONE)
1792 wait_on_page_writeback(page);
1793 else
1794 goto continue_unlock;
1795 }
1796
1797 BUG_ON(PageWriteback(page));
1798 if (!clear_page_dirty_for_io(page))
1799 goto continue_unlock;
1800
1801 trace_wbc_writepage(wbc, mapping->backing_dev_info);
1802 ret = (*writepage)(page, wbc, data);
1803 if (unlikely(ret)) {
1804 if (ret == AOP_WRITEPAGE_ACTIVATE) {
1805 unlock_page(page);
1806 ret = 0;
1807 } else {
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817 done_index = page->index + 1;
1818 done = 1;
1819 break;
1820 }
1821 }
1822
1823
1824
1825
1826
1827
1828
1829 if (--wbc->nr_to_write <= 0 &&
1830 wbc->sync_mode == WB_SYNC_NONE) {
1831 done = 1;
1832 break;
1833 }
1834 }
1835 pagevec_release(&pvec);
1836 cond_resched();
1837 }
1838 if (!cycled && !done) {
1839
1840
1841
1842
1843
1844 cycled = 1;
1845 index = 0;
1846 end = writeback_index - 1;
1847 goto retry;
1848 }
1849 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
1850 mapping->writeback_index = done_index;
1851
1852 return ret;
1853}
1854EXPORT_SYMBOL(write_cache_pages);
1855
1856
1857
1858
1859
1860static int __writepage(struct page *page, struct writeback_control *wbc,
1861 void *data)
1862{
1863 struct address_space *mapping = data;
1864 int ret = mapping->a_ops->writepage(page, wbc);
1865 mapping_set_error(mapping, ret);
1866 return ret;
1867}
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877int generic_writepages(struct address_space *mapping,
1878 struct writeback_control *wbc)
1879{
1880 struct blk_plug plug;
1881 int ret;
1882
1883
1884 if (!mapping->a_ops->writepage)
1885 return 0;
1886
1887 blk_start_plug(&plug);
1888 ret = write_cache_pages(mapping, wbc, __writepage, mapping);
1889 blk_finish_plug(&plug);
1890 return ret;
1891}
1892
1893EXPORT_SYMBOL(generic_writepages);
1894
1895int do_writepages(struct address_space *mapping, struct writeback_control *wbc)
1896{
1897 int ret;
1898
1899 if (wbc->nr_to_write <= 0)
1900 return 0;
1901 if (mapping->a_ops->writepages)
1902 ret = mapping->a_ops->writepages(mapping, wbc);
1903 else
1904 ret = generic_writepages(mapping, wbc);
1905 return ret;
1906}
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917int write_one_page(struct page *page, int wait)
1918{
1919 struct address_space *mapping = page->mapping;
1920 int ret = 0;
1921 struct writeback_control wbc = {
1922 .sync_mode = WB_SYNC_ALL,
1923 .nr_to_write = 1,
1924 };
1925
1926 BUG_ON(!PageLocked(page));
1927
1928 if (wait)
1929 wait_on_page_writeback(page);
1930
1931 if (clear_page_dirty_for_io(page)) {
1932 page_cache_get(page);
1933 ret = mapping->a_ops->writepage(page, &wbc);
1934 if (ret == 0 && wait) {
1935 wait_on_page_writeback(page);
1936 if (PageError(page))
1937 ret = -EIO;
1938 }
1939 page_cache_release(page);
1940 } else {
1941 unlock_page(page);
1942 }
1943 return ret;
1944}
1945EXPORT_SYMBOL(write_one_page);
1946
1947
1948
1949
1950int __set_page_dirty_no_writeback(struct page *page)
1951{
1952 if (!PageDirty(page))
1953 return !TestSetPageDirty(page);
1954 return 0;
1955}
1956
1957
1958
1959
1960
1961void account_page_dirtied(struct page *page, struct address_space *mapping)
1962{
1963 if (mapping_cap_account_dirty(mapping)) {
1964 __inc_zone_page_state(page, NR_FILE_DIRTY);
1965 __inc_zone_page_state(page, NR_DIRTIED);
1966 __inc_bdi_stat(mapping->backing_dev_info, BDI_RECLAIMABLE);
1967 __inc_bdi_stat(mapping->backing_dev_info, BDI_DIRTIED);
1968 task_io_account_write(PAGE_CACHE_SIZE);
1969 current->nr_dirtied++;
1970 this_cpu_inc(bdp_ratelimits);
1971 }
1972}
1973EXPORT_SYMBOL(account_page_dirtied);
1974
1975
1976
1977
1978
1979
1980void account_page_writeback(struct page *page)
1981{
1982 inc_zone_page_state(page, NR_WRITEBACK);
1983}
1984EXPORT_SYMBOL(account_page_writeback);
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001int __set_page_dirty_nobuffers(struct page *page)
2002{
2003 if (!TestSetPageDirty(page)) {
2004 struct address_space *mapping = page_mapping(page);
2005 struct address_space *mapping2;
2006
2007 if (!mapping)
2008 return 1;
2009
2010 spin_lock_irq(&mapping->tree_lock);
2011 mapping2 = page_mapping(page);
2012 if (mapping2) {
2013 BUG_ON(mapping2 != mapping);
2014 WARN_ON_ONCE(!PagePrivate(page) && !PageUptodate(page));
2015 account_page_dirtied(page, mapping);
2016 radix_tree_tag_set(&mapping->page_tree,
2017 page_index(page), PAGECACHE_TAG_DIRTY);
2018 }
2019 spin_unlock_irq(&mapping->tree_lock);
2020 if (mapping->host) {
2021
2022 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
2023 }
2024 return 1;
2025 }
2026 return 0;
2027}
2028EXPORT_SYMBOL(__set_page_dirty_nobuffers);
2029
2030
2031
2032
2033
2034
2035
2036
2037void account_page_redirty(struct page *page)
2038{
2039 struct address_space *mapping = page->mapping;
2040 if (mapping && mapping_cap_account_dirty(mapping)) {
2041 current->nr_dirtied--;
2042 dec_zone_page_state(page, NR_DIRTIED);
2043 dec_bdi_stat(mapping->backing_dev_info, BDI_DIRTIED);
2044 }
2045}
2046EXPORT_SYMBOL(account_page_redirty);
2047
2048
2049
2050
2051
2052
2053int redirty_page_for_writepage(struct writeback_control *wbc, struct page *page)
2054{
2055 wbc->pages_skipped++;
2056 account_page_redirty(page);
2057 return __set_page_dirty_nobuffers(page);
2058}
2059EXPORT_SYMBOL(redirty_page_for_writepage);
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072int set_page_dirty(struct page *page)
2073{
2074 struct address_space *mapping = page_mapping(page);
2075
2076 if (likely(mapping)) {
2077 int (*spd)(struct page *) = mapping->a_ops->set_page_dirty;
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088 ClearPageReclaim(page);
2089#ifdef CONFIG_BLOCK
2090 if (!spd)
2091 spd = __set_page_dirty_buffers;
2092#endif
2093 return (*spd)(page);
2094 }
2095 if (!PageDirty(page)) {
2096 if (!TestSetPageDirty(page))
2097 return 1;
2098 }
2099 return 0;
2100}
2101EXPORT_SYMBOL(set_page_dirty);
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113int set_page_dirty_lock(struct page *page)
2114{
2115 int ret;
2116
2117 lock_page(page);
2118 ret = set_page_dirty(page);
2119 unlock_page(page);
2120 return ret;
2121}
2122EXPORT_SYMBOL(set_page_dirty_lock);
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138int clear_page_dirty_for_io(struct page *page)
2139{
2140 struct address_space *mapping = page_mapping(page);
2141
2142 BUG_ON(!PageLocked(page));
2143
2144 if (mapping && mapping_cap_account_dirty(mapping)) {
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170 if (page_mkclean(page))
2171 set_page_dirty(page);
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182 if (TestClearPageDirty(page)) {
2183 dec_zone_page_state(page, NR_FILE_DIRTY);
2184 dec_bdi_stat(mapping->backing_dev_info,
2185 BDI_RECLAIMABLE);
2186 return 1;
2187 }
2188 return 0;
2189 }
2190 return TestClearPageDirty(page);
2191}
2192EXPORT_SYMBOL(clear_page_dirty_for_io);
2193
2194int test_clear_page_writeback(struct page *page)
2195{
2196 struct address_space *mapping = page_mapping(page);
2197 int ret;
2198
2199 if (mapping) {
2200 struct backing_dev_info *bdi = mapping->backing_dev_info;
2201 unsigned long flags;
2202
2203 spin_lock_irqsave(&mapping->tree_lock, flags);
2204 ret = TestClearPageWriteback(page);
2205 if (ret) {
2206 radix_tree_tag_clear(&mapping->page_tree,
2207 page_index(page),
2208 PAGECACHE_TAG_WRITEBACK);
2209 if (bdi_cap_account_writeback(bdi)) {
2210 __dec_bdi_stat(bdi, BDI_WRITEBACK);
2211 __bdi_writeout_inc(bdi);
2212 }
2213 }
2214 spin_unlock_irqrestore(&mapping->tree_lock, flags);
2215 } else {
2216 ret = TestClearPageWriteback(page);
2217 }
2218 if (ret) {
2219 dec_zone_page_state(page, NR_WRITEBACK);
2220 inc_zone_page_state(page, NR_WRITTEN);
2221 }
2222 return ret;
2223}
2224
2225int test_set_page_writeback(struct page *page)
2226{
2227 struct address_space *mapping = page_mapping(page);
2228 int ret;
2229
2230 if (mapping) {
2231 struct backing_dev_info *bdi = mapping->backing_dev_info;
2232 unsigned long flags;
2233
2234 spin_lock_irqsave(&mapping->tree_lock, flags);
2235 ret = TestSetPageWriteback(page);
2236 if (!ret) {
2237 radix_tree_tag_set(&mapping->page_tree,
2238 page_index(page),
2239 PAGECACHE_TAG_WRITEBACK);
2240 if (bdi_cap_account_writeback(bdi))
2241 __inc_bdi_stat(bdi, BDI_WRITEBACK);
2242 }
2243 if (!PageDirty(page))
2244 radix_tree_tag_clear(&mapping->page_tree,
2245 page_index(page),
2246 PAGECACHE_TAG_DIRTY);
2247 radix_tree_tag_clear(&mapping->page_tree,
2248 page_index(page),
2249 PAGECACHE_TAG_TOWRITE);
2250 spin_unlock_irqrestore(&mapping->tree_lock, flags);
2251 } else {
2252 ret = TestSetPageWriteback(page);
2253 }
2254 if (!ret)
2255 account_page_writeback(page);
2256 return ret;
2257
2258}
2259EXPORT_SYMBOL(test_set_page_writeback);
2260
2261
2262
2263
2264
2265int mapping_tagged(struct address_space *mapping, int tag)
2266{
2267 return radix_tree_tagged(&mapping->page_tree, tag);
2268}
2269EXPORT_SYMBOL(mapping_tagged);
2270