Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Uwe Zeisberger | f30c226 | 2006-10-03 23:01:26 +0200 | [diff] [blame] | 2 | * mm/page-writeback.c |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2002, Linus Torvalds. |
| 5 | * |
| 6 | * Contains functions related to writing back dirty pages at the |
| 7 | * address_space level. |
| 8 | * |
| 9 | * 10Apr2002 akpm@zip.com.au |
| 10 | * Initial version |
| 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/swap.h> |
| 19 | #include <linux/slab.h> |
| 20 | #include <linux/pagemap.h> |
| 21 | #include <linux/writeback.h> |
| 22 | #include <linux/init.h> |
| 23 | #include <linux/backing-dev.h> |
Andrew Morton | 55e829a | 2006-12-10 02:19:27 -0800 | [diff] [blame] | 24 | #include <linux/task_io_accounting_ops.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <linux/blkdev.h> |
| 26 | #include <linux/mpage.h> |
Peter Zijlstra | d08b385 | 2006-09-25 23:30:57 -0700 | [diff] [blame] | 27 | #include <linux/rmap.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <linux/percpu.h> |
| 29 | #include <linux/notifier.h> |
| 30 | #include <linux/smp.h> |
| 31 | #include <linux/sysctl.h> |
| 32 | #include <linux/cpu.h> |
| 33 | #include <linux/syscalls.h> |
David Howells | cf9a2ae | 2006-08-29 19:05:54 +0100 | [diff] [blame] | 34 | #include <linux/buffer_head.h> |
David Howells | 811d736 | 2006-08-29 19:06:09 +0100 | [diff] [blame] | 35 | #include <linux/pagevec.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
| 37 | /* |
| 38 | * The maximum number of pages to writeout in a single bdflush/kupdate |
| 39 | * operation. We do this so we don't hold I_LOCK against an inode for |
| 40 | * enormous amounts of time, which would block a userspace task which has |
| 41 | * been forced to throttle against that inode. Also, the code reevaluates |
| 42 | * the dirty each time it has written this many pages. |
| 43 | */ |
| 44 | #define MAX_WRITEBACK_PAGES 1024 |
| 45 | |
| 46 | /* |
| 47 | * After a CPU has dirtied this many pages, balance_dirty_pages_ratelimited |
| 48 | * will look to see if it needs to force writeback or throttling. |
| 49 | */ |
| 50 | static long ratelimit_pages = 32; |
| 51 | |
Andrew Morton | e236a16 | 2006-01-18 17:42:26 -0800 | [diff] [blame] | 52 | static int dirty_exceeded __cacheline_aligned_in_smp; /* Dirty mem may be over limit */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
| 54 | /* |
| 55 | * When balance_dirty_pages decides that the caller needs to perform some |
| 56 | * non-background writeback, this is how many pages it will attempt to write. |
| 57 | * It should be somewhat larger than RATELIMIT_PAGES to ensure that reasonably |
| 58 | * large amounts of I/O are submitted. |
| 59 | */ |
| 60 | static inline long sync_writeback_pages(void) |
| 61 | { |
| 62 | return ratelimit_pages + ratelimit_pages / 2; |
| 63 | } |
| 64 | |
| 65 | /* The following parameters are exported via /proc/sys/vm */ |
| 66 | |
| 67 | /* |
| 68 | * Start background writeback (via pdflush) at this percentage |
| 69 | */ |
| 70 | int dirty_background_ratio = 10; |
| 71 | |
| 72 | /* |
| 73 | * The generator of dirty data starts writeback at this percentage |
| 74 | */ |
| 75 | int vm_dirty_ratio = 40; |
| 76 | |
| 77 | /* |
Coywolf Qi Hunt | fd5403c | 2006-04-10 22:54:35 -0700 | [diff] [blame] | 78 | * The interval between `kupdate'-style writebacks, in jiffies |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | */ |
Bart Samwel | f6ef943 | 2006-03-24 03:15:48 -0800 | [diff] [blame] | 80 | int dirty_writeback_interval = 5 * HZ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | |
| 82 | /* |
Coywolf Qi Hunt | fd5403c | 2006-04-10 22:54:35 -0700 | [diff] [blame] | 83 | * The longest number of jiffies for which data is allowed to remain dirty |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | */ |
Bart Samwel | f6ef943 | 2006-03-24 03:15:48 -0800 | [diff] [blame] | 85 | int dirty_expire_interval = 30 * HZ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | |
| 87 | /* |
| 88 | * Flag that makes the machine dump writes/reads and block dirtyings. |
| 89 | */ |
| 90 | int block_dump; |
| 91 | |
| 92 | /* |
Bart Samwel | ed5b43f | 2006-03-24 03:15:49 -0800 | [diff] [blame] | 93 | * Flag that puts the machine in "laptop mode". Doubles as a timeout in jiffies: |
| 94 | * a full sync is triggered after this time elapses without any disk activity. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | */ |
| 96 | int laptop_mode; |
| 97 | |
| 98 | EXPORT_SYMBOL(laptop_mode); |
| 99 | |
| 100 | /* End of sysctl-exported parameters */ |
| 101 | |
| 102 | |
| 103 | static void background_writeout(unsigned long _min_pages); |
| 104 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | /* |
| 106 | * Work out the current dirty-memory clamping and background writeout |
| 107 | * thresholds. |
| 108 | * |
| 109 | * The main aim here is to lower them aggressively if there is a lot of mapped |
| 110 | * memory around. To avoid stressing page reclaim with lots of unreclaimable |
| 111 | * pages. It is better to clamp down on writers than to start swapping, and |
| 112 | * performing lots of scanning. |
| 113 | * |
| 114 | * We only allow 1/2 of the currently-unmapped memory to be dirtied. |
| 115 | * |
| 116 | * We don't permit the clamping level to fall below 5% - that is getting rather |
| 117 | * excessive. |
| 118 | * |
| 119 | * We make sure that the background writeout level is below the adjusted |
| 120 | * clamping level. |
| 121 | */ |
| 122 | static void |
Christoph Lameter | c24f21b | 2006-06-30 01:55:42 -0700 | [diff] [blame] | 123 | get_dirty_limits(long *pbackground, long *pdirty, |
| 124 | struct address_space *mapping) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | { |
| 126 | int background_ratio; /* Percentages */ |
| 127 | int dirty_ratio; |
| 128 | int unmapped_ratio; |
| 129 | long background; |
| 130 | long dirty; |
Chandra Seetharaman | 40c99aa | 2006-09-29 02:01:24 -0700 | [diff] [blame] | 131 | unsigned long available_memory = vm_total_pages; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | struct task_struct *tsk; |
| 133 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | #ifdef CONFIG_HIGHMEM |
| 135 | /* |
| 136 | * If this mapping can only allocate from low memory, |
| 137 | * we exclude high memory from our count. |
| 138 | */ |
| 139 | if (mapping && !(mapping_gfp_mask(mapping) & __GFP_HIGHMEM)) |
| 140 | available_memory -= totalhigh_pages; |
| 141 | #endif |
| 142 | |
| 143 | |
Christoph Lameter | c24f21b | 2006-06-30 01:55:42 -0700 | [diff] [blame] | 144 | unmapped_ratio = 100 - ((global_page_state(NR_FILE_MAPPED) + |
| 145 | global_page_state(NR_ANON_PAGES)) * 100) / |
Chandra Seetharaman | 40c99aa | 2006-09-29 02:01:24 -0700 | [diff] [blame] | 146 | vm_total_pages; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | |
| 148 | dirty_ratio = vm_dirty_ratio; |
| 149 | if (dirty_ratio > unmapped_ratio / 2) |
| 150 | dirty_ratio = unmapped_ratio / 2; |
| 151 | |
| 152 | if (dirty_ratio < 5) |
| 153 | dirty_ratio = 5; |
| 154 | |
| 155 | background_ratio = dirty_background_ratio; |
| 156 | if (background_ratio >= dirty_ratio) |
| 157 | background_ratio = dirty_ratio / 2; |
| 158 | |
| 159 | background = (background_ratio * available_memory) / 100; |
| 160 | dirty = (dirty_ratio * available_memory) / 100; |
| 161 | tsk = current; |
| 162 | if (tsk->flags & PF_LESS_THROTTLE || rt_task(tsk)) { |
| 163 | background += background / 4; |
| 164 | dirty += dirty / 4; |
| 165 | } |
| 166 | *pbackground = background; |
| 167 | *pdirty = dirty; |
| 168 | } |
| 169 | |
| 170 | /* |
| 171 | * balance_dirty_pages() must be called by processes which are generating dirty |
| 172 | * data. It looks at the number of dirty pages in the machine and will force |
| 173 | * the caller to perform writeback if the system is over `vm_dirty_ratio'. |
| 174 | * If we're over `background_thresh' then pdflush is woken to perform some |
| 175 | * writeout. |
| 176 | */ |
| 177 | static void balance_dirty_pages(struct address_space *mapping) |
| 178 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | long nr_reclaimable; |
| 180 | long background_thresh; |
| 181 | long dirty_thresh; |
| 182 | unsigned long pages_written = 0; |
| 183 | unsigned long write_chunk = sync_writeback_pages(); |
| 184 | |
| 185 | struct backing_dev_info *bdi = mapping->backing_dev_info; |
| 186 | |
| 187 | for (;;) { |
| 188 | struct writeback_control wbc = { |
| 189 | .bdi = bdi, |
| 190 | .sync_mode = WB_SYNC_NONE, |
| 191 | .older_than_this = NULL, |
| 192 | .nr_to_write = write_chunk, |
OGAWA Hirofumi | 111ebb6 | 2006-06-23 02:03:26 -0700 | [diff] [blame] | 193 | .range_cyclic = 1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | }; |
| 195 | |
Christoph Lameter | c24f21b | 2006-06-30 01:55:42 -0700 | [diff] [blame] | 196 | get_dirty_limits(&background_thresh, &dirty_thresh, mapping); |
| 197 | nr_reclaimable = global_page_state(NR_FILE_DIRTY) + |
| 198 | global_page_state(NR_UNSTABLE_NFS); |
| 199 | if (nr_reclaimable + global_page_state(NR_WRITEBACK) <= |
| 200 | dirty_thresh) |
| 201 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | |
Andrew Morton | e236a16 | 2006-01-18 17:42:26 -0800 | [diff] [blame] | 203 | if (!dirty_exceeded) |
| 204 | dirty_exceeded = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | |
| 206 | /* Note: nr_reclaimable denotes nr_dirty + nr_unstable. |
| 207 | * Unstable writes are a feature of certain networked |
| 208 | * filesystems (i.e. NFS) in which data may have been |
| 209 | * written to the server's write cache, but has not yet |
| 210 | * been flushed to permanent storage. |
| 211 | */ |
| 212 | if (nr_reclaimable) { |
| 213 | writeback_inodes(&wbc); |
Christoph Lameter | c24f21b | 2006-06-30 01:55:42 -0700 | [diff] [blame] | 214 | get_dirty_limits(&background_thresh, |
| 215 | &dirty_thresh, mapping); |
| 216 | nr_reclaimable = global_page_state(NR_FILE_DIRTY) + |
| 217 | global_page_state(NR_UNSTABLE_NFS); |
| 218 | if (nr_reclaimable + |
| 219 | global_page_state(NR_WRITEBACK) |
| 220 | <= dirty_thresh) |
| 221 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | pages_written += write_chunk - wbc.nr_to_write; |
| 223 | if (pages_written >= write_chunk) |
| 224 | break; /* We've done our duty */ |
| 225 | } |
Andrew Morton | 3fcfab1 | 2006-10-19 23:28:16 -0700 | [diff] [blame] | 226 | congestion_wait(WRITE, HZ/10); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | } |
| 228 | |
Christoph Lameter | c24f21b | 2006-06-30 01:55:42 -0700 | [diff] [blame] | 229 | if (nr_reclaimable + global_page_state(NR_WRITEBACK) |
| 230 | <= dirty_thresh && dirty_exceeded) |
| 231 | dirty_exceeded = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | |
| 233 | if (writeback_in_progress(bdi)) |
| 234 | return; /* pdflush is already working this queue */ |
| 235 | |
| 236 | /* |
| 237 | * In laptop mode, we wait until hitting the higher threshold before |
| 238 | * starting background writeout, and then write out all the way down |
| 239 | * to the lower threshold. So slow writers cause minimal disk activity. |
| 240 | * |
| 241 | * In normal mode, we start background writeout at the lower |
| 242 | * background_thresh, to keep the amount of dirty memory low. |
| 243 | */ |
| 244 | if ((laptop_mode && pages_written) || |
| 245 | (!laptop_mode && (nr_reclaimable > background_thresh))) |
| 246 | pdflush_operation(background_writeout, 0); |
| 247 | } |
| 248 | |
Peter Zijlstra | edc79b2 | 2006-09-25 23:30:58 -0700 | [diff] [blame] | 249 | void set_page_dirty_balance(struct page *page) |
| 250 | { |
| 251 | if (set_page_dirty(page)) { |
| 252 | struct address_space *mapping = page_mapping(page); |
| 253 | |
| 254 | if (mapping) |
| 255 | balance_dirty_pages_ratelimited(mapping); |
| 256 | } |
| 257 | } |
| 258 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | /** |
Andrew Morton | fa5a734 | 2006-03-24 03:18:10 -0800 | [diff] [blame] | 260 | * balance_dirty_pages_ratelimited_nr - balance dirty memory state |
Martin Waitz | 67be2dd | 2005-05-01 08:59:26 -0700 | [diff] [blame] | 261 | * @mapping: address_space which was dirtied |
Martin Waitz | a580290 | 2006-04-02 13:59:55 +0200 | [diff] [blame] | 262 | * @nr_pages_dirtied: number of pages which the caller has just dirtied |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | * |
| 264 | * Processes which are dirtying memory should call in here once for each page |
| 265 | * which was newly dirtied. The function will periodically check the system's |
| 266 | * dirty state and will initiate writeback if needed. |
| 267 | * |
| 268 | * On really big machines, get_writeback_state is expensive, so try to avoid |
| 269 | * calling it too often (ratelimiting). But once we're over the dirty memory |
| 270 | * limit we decrease the ratelimiting by a lot, to prevent individual processes |
| 271 | * from overshooting the limit by (ratelimit_pages) each. |
| 272 | */ |
Andrew Morton | fa5a734 | 2006-03-24 03:18:10 -0800 | [diff] [blame] | 273 | void balance_dirty_pages_ratelimited_nr(struct address_space *mapping, |
| 274 | unsigned long nr_pages_dirtied) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | { |
Andrew Morton | fa5a734 | 2006-03-24 03:18:10 -0800 | [diff] [blame] | 276 | static DEFINE_PER_CPU(unsigned long, ratelimits) = 0; |
| 277 | unsigned long ratelimit; |
| 278 | unsigned long *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | |
| 280 | ratelimit = ratelimit_pages; |
| 281 | if (dirty_exceeded) |
| 282 | ratelimit = 8; |
| 283 | |
| 284 | /* |
| 285 | * Check the rate limiting. Also, we do not want to throttle real-time |
| 286 | * tasks in balance_dirty_pages(). Period. |
| 287 | */ |
Andrew Morton | fa5a734 | 2006-03-24 03:18:10 -0800 | [diff] [blame] | 288 | preempt_disable(); |
| 289 | p = &__get_cpu_var(ratelimits); |
| 290 | *p += nr_pages_dirtied; |
| 291 | if (unlikely(*p >= ratelimit)) { |
| 292 | *p = 0; |
| 293 | preempt_enable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | balance_dirty_pages(mapping); |
| 295 | return; |
| 296 | } |
Andrew Morton | fa5a734 | 2006-03-24 03:18:10 -0800 | [diff] [blame] | 297 | preempt_enable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | } |
Andrew Morton | fa5a734 | 2006-03-24 03:18:10 -0800 | [diff] [blame] | 299 | EXPORT_SYMBOL(balance_dirty_pages_ratelimited_nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | |
| 301 | void throttle_vm_writeout(void) |
| 302 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | long background_thresh; |
| 304 | long dirty_thresh; |
| 305 | |
| 306 | for ( ; ; ) { |
Christoph Lameter | c24f21b | 2006-06-30 01:55:42 -0700 | [diff] [blame] | 307 | get_dirty_limits(&background_thresh, &dirty_thresh, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | |
| 309 | /* |
| 310 | * Boost the allowable dirty threshold a bit for page |
| 311 | * allocators so they don't get DoS'ed by heavy writers |
| 312 | */ |
| 313 | dirty_thresh += dirty_thresh / 10; /* wheeee... */ |
| 314 | |
Christoph Lameter | c24f21b | 2006-06-30 01:55:42 -0700 | [diff] [blame] | 315 | if (global_page_state(NR_UNSTABLE_NFS) + |
| 316 | global_page_state(NR_WRITEBACK) <= dirty_thresh) |
| 317 | break; |
Andrew Morton | 3fcfab1 | 2006-10-19 23:28:16 -0700 | [diff] [blame] | 318 | congestion_wait(WRITE, HZ/10); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | } |
| 320 | } |
| 321 | |
| 322 | |
| 323 | /* |
| 324 | * writeback at least _min_pages, and keep writing until the amount of dirty |
| 325 | * memory is less than the background threshold, or until we're all clean. |
| 326 | */ |
| 327 | static void background_writeout(unsigned long _min_pages) |
| 328 | { |
| 329 | long min_pages = _min_pages; |
| 330 | struct writeback_control wbc = { |
| 331 | .bdi = NULL, |
| 332 | .sync_mode = WB_SYNC_NONE, |
| 333 | .older_than_this = NULL, |
| 334 | .nr_to_write = 0, |
| 335 | .nonblocking = 1, |
OGAWA Hirofumi | 111ebb6 | 2006-06-23 02:03:26 -0700 | [diff] [blame] | 336 | .range_cyclic = 1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | }; |
| 338 | |
| 339 | for ( ; ; ) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | long background_thresh; |
| 341 | long dirty_thresh; |
| 342 | |
Christoph Lameter | c24f21b | 2006-06-30 01:55:42 -0700 | [diff] [blame] | 343 | get_dirty_limits(&background_thresh, &dirty_thresh, NULL); |
| 344 | if (global_page_state(NR_FILE_DIRTY) + |
| 345 | global_page_state(NR_UNSTABLE_NFS) < background_thresh |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | && min_pages <= 0) |
| 347 | break; |
| 348 | wbc.encountered_congestion = 0; |
| 349 | wbc.nr_to_write = MAX_WRITEBACK_PAGES; |
| 350 | wbc.pages_skipped = 0; |
| 351 | writeback_inodes(&wbc); |
| 352 | min_pages -= MAX_WRITEBACK_PAGES - wbc.nr_to_write; |
| 353 | if (wbc.nr_to_write > 0 || wbc.pages_skipped > 0) { |
| 354 | /* Wrote less than expected */ |
Andrew Morton | 3fcfab1 | 2006-10-19 23:28:16 -0700 | [diff] [blame] | 355 | congestion_wait(WRITE, HZ/10); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | if (!wbc.encountered_congestion) |
| 357 | break; |
| 358 | } |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | /* |
| 363 | * Start writeback of `nr_pages' pages. If `nr_pages' is zero, write back |
| 364 | * the whole world. Returns 0 if a pdflush thread was dispatched. Returns |
| 365 | * -1 if all pdflush threads were busy. |
| 366 | */ |
Pekka J Enberg | 687a21c | 2005-06-28 20:44:55 -0700 | [diff] [blame] | 367 | int wakeup_pdflush(long nr_pages) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | { |
Christoph Lameter | c24f21b | 2006-06-30 01:55:42 -0700 | [diff] [blame] | 369 | if (nr_pages == 0) |
| 370 | nr_pages = global_page_state(NR_FILE_DIRTY) + |
| 371 | global_page_state(NR_UNSTABLE_NFS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | return pdflush_operation(background_writeout, nr_pages); |
| 373 | } |
| 374 | |
| 375 | static void wb_timer_fn(unsigned long unused); |
| 376 | static void laptop_timer_fn(unsigned long unused); |
| 377 | |
Ingo Molnar | 8d06afa | 2005-09-09 13:10:40 -0700 | [diff] [blame] | 378 | static DEFINE_TIMER(wb_timer, wb_timer_fn, 0, 0); |
| 379 | static DEFINE_TIMER(laptop_mode_wb_timer, laptop_timer_fn, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | |
| 381 | /* |
| 382 | * Periodic writeback of "old" data. |
| 383 | * |
| 384 | * Define "old": the first time one of an inode's pages is dirtied, we mark the |
| 385 | * dirtying-time in the inode's address_space. So this periodic writeback code |
| 386 | * just walks the superblock inode list, writing back any inodes which are |
| 387 | * older than a specific point in time. |
| 388 | * |
Bart Samwel | f6ef943 | 2006-03-24 03:15:48 -0800 | [diff] [blame] | 389 | * Try to run once per dirty_writeback_interval. But if a writeback event |
| 390 | * takes longer than a dirty_writeback_interval interval, then leave a |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | * one-second gap. |
| 392 | * |
| 393 | * older_than_this takes precedence over nr_to_write. So we'll only write back |
| 394 | * all dirty pages if they are all attached to "old" mappings. |
| 395 | */ |
| 396 | static void wb_kupdate(unsigned long arg) |
| 397 | { |
| 398 | unsigned long oldest_jif; |
| 399 | unsigned long start_jif; |
| 400 | unsigned long next_jif; |
| 401 | long nr_to_write; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | struct writeback_control wbc = { |
| 403 | .bdi = NULL, |
| 404 | .sync_mode = WB_SYNC_NONE, |
| 405 | .older_than_this = &oldest_jif, |
| 406 | .nr_to_write = 0, |
| 407 | .nonblocking = 1, |
| 408 | .for_kupdate = 1, |
OGAWA Hirofumi | 111ebb6 | 2006-06-23 02:03:26 -0700 | [diff] [blame] | 409 | .range_cyclic = 1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | }; |
| 411 | |
| 412 | sync_supers(); |
| 413 | |
Bart Samwel | f6ef943 | 2006-03-24 03:15:48 -0800 | [diff] [blame] | 414 | oldest_jif = jiffies - dirty_expire_interval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | start_jif = jiffies; |
Bart Samwel | f6ef943 | 2006-03-24 03:15:48 -0800 | [diff] [blame] | 416 | next_jif = start_jif + dirty_writeback_interval; |
Christoph Lameter | c24f21b | 2006-06-30 01:55:42 -0700 | [diff] [blame] | 417 | nr_to_write = global_page_state(NR_FILE_DIRTY) + |
| 418 | global_page_state(NR_UNSTABLE_NFS) + |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | (inodes_stat.nr_inodes - inodes_stat.nr_unused); |
| 420 | while (nr_to_write > 0) { |
| 421 | wbc.encountered_congestion = 0; |
| 422 | wbc.nr_to_write = MAX_WRITEBACK_PAGES; |
| 423 | writeback_inodes(&wbc); |
| 424 | if (wbc.nr_to_write > 0) { |
| 425 | if (wbc.encountered_congestion) |
Andrew Morton | 3fcfab1 | 2006-10-19 23:28:16 -0700 | [diff] [blame] | 426 | congestion_wait(WRITE, HZ/10); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | else |
| 428 | break; /* All the old data is written */ |
| 429 | } |
| 430 | nr_to_write -= MAX_WRITEBACK_PAGES - wbc.nr_to_write; |
| 431 | } |
| 432 | if (time_before(next_jif, jiffies + HZ)) |
| 433 | next_jif = jiffies + HZ; |
Bart Samwel | f6ef943 | 2006-03-24 03:15:48 -0800 | [diff] [blame] | 434 | if (dirty_writeback_interval) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | mod_timer(&wb_timer, next_jif); |
| 436 | } |
| 437 | |
| 438 | /* |
| 439 | * sysctl handler for /proc/sys/vm/dirty_writeback_centisecs |
| 440 | */ |
| 441 | int dirty_writeback_centisecs_handler(ctl_table *table, int write, |
| 442 | struct file *file, void __user *buffer, size_t *length, loff_t *ppos) |
| 443 | { |
Bart Samwel | f6ef943 | 2006-03-24 03:15:48 -0800 | [diff] [blame] | 444 | proc_dointvec_userhz_jiffies(table, write, file, buffer, length, ppos); |
| 445 | if (dirty_writeback_interval) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | mod_timer(&wb_timer, |
Bart Samwel | f6ef943 | 2006-03-24 03:15:48 -0800 | [diff] [blame] | 447 | jiffies + dirty_writeback_interval); |
| 448 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | del_timer(&wb_timer); |
| 450 | } |
| 451 | return 0; |
| 452 | } |
| 453 | |
| 454 | static void wb_timer_fn(unsigned long unused) |
| 455 | { |
| 456 | if (pdflush_operation(wb_kupdate, 0) < 0) |
| 457 | mod_timer(&wb_timer, jiffies + HZ); /* delay 1 second */ |
| 458 | } |
| 459 | |
| 460 | static void laptop_flush(unsigned long unused) |
| 461 | { |
| 462 | sys_sync(); |
| 463 | } |
| 464 | |
| 465 | static void laptop_timer_fn(unsigned long unused) |
| 466 | { |
| 467 | pdflush_operation(laptop_flush, 0); |
| 468 | } |
| 469 | |
| 470 | /* |
| 471 | * We've spun up the disk and we're in laptop mode: schedule writeback |
| 472 | * of all dirty data a few seconds from now. If the flush is already scheduled |
| 473 | * then push it back - the user is still using the disk. |
| 474 | */ |
| 475 | void laptop_io_completion(void) |
| 476 | { |
Bart Samwel | ed5b43f | 2006-03-24 03:15:49 -0800 | [diff] [blame] | 477 | mod_timer(&laptop_mode_wb_timer, jiffies + laptop_mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | /* |
| 481 | * We're in laptop mode and we've just synced. The sync's writes will have |
| 482 | * caused another writeback to be scheduled by laptop_io_completion. |
| 483 | * Nothing needs to be written back anymore, so we unschedule the writeback. |
| 484 | */ |
| 485 | void laptop_sync_completion(void) |
| 486 | { |
| 487 | del_timer(&laptop_mode_wb_timer); |
| 488 | } |
| 489 | |
| 490 | /* |
| 491 | * If ratelimit_pages is too high then we can get into dirty-data overload |
| 492 | * if a large number of processes all perform writes at the same time. |
| 493 | * If it is too low then SMP machines will call the (expensive) |
| 494 | * get_writeback_state too often. |
| 495 | * |
| 496 | * Here we set ratelimit_pages to a level which ensures that when all CPUs are |
| 497 | * dirtying in parallel, we cannot go more than 3% (1/32) over the dirty memory |
| 498 | * thresholds before writeback cuts in. |
| 499 | * |
| 500 | * But the limit should not be set too high. Because it also controls the |
| 501 | * amount of memory which the balance_dirty_pages() caller has to write back. |
| 502 | * If this is too large then the caller will block on the IO queue all the |
| 503 | * time. So limit it to four megabytes - the balance_dirty_pages() caller |
| 504 | * will write six megabyte chunks, max. |
| 505 | */ |
| 506 | |
Chandra Seetharaman | 2d1d43f | 2006-09-29 02:01:25 -0700 | [diff] [blame] | 507 | void writeback_set_ratelimit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | { |
Chandra Seetharaman | 40c99aa | 2006-09-29 02:01:24 -0700 | [diff] [blame] | 509 | ratelimit_pages = vm_total_pages / (num_online_cpus() * 32); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | if (ratelimit_pages < 16) |
| 511 | ratelimit_pages = 16; |
| 512 | if (ratelimit_pages * PAGE_CACHE_SIZE > 4096 * 1024) |
| 513 | ratelimit_pages = (4096 * 1024) / PAGE_CACHE_SIZE; |
| 514 | } |
| 515 | |
Chandra Seetharaman | 26c2143 | 2006-06-27 02:54:10 -0700 | [diff] [blame] | 516 | static int __cpuinit |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | ratelimit_handler(struct notifier_block *self, unsigned long u, void *v) |
| 518 | { |
Chandra Seetharaman | 2d1d43f | 2006-09-29 02:01:25 -0700 | [diff] [blame] | 519 | writeback_set_ratelimit(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | return 0; |
| 521 | } |
| 522 | |
Chandra Seetharaman | 74b85f3 | 2006-06-27 02:54:09 -0700 | [diff] [blame] | 523 | static struct notifier_block __cpuinitdata ratelimit_nb = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | .notifier_call = ratelimit_handler, |
| 525 | .next = NULL, |
| 526 | }; |
| 527 | |
| 528 | /* |
| 529 | * If the machine has a large highmem:lowmem ratio then scale back the default |
| 530 | * dirty memory thresholds: allowing too much dirty highmem pins an excessive |
| 531 | * number of buffer_heads. |
| 532 | */ |
| 533 | void __init page_writeback_init(void) |
| 534 | { |
| 535 | long buffer_pages = nr_free_buffer_pages(); |
| 536 | long correction; |
| 537 | |
Chandra Seetharaman | 40c99aa | 2006-09-29 02:01:24 -0700 | [diff] [blame] | 538 | correction = (100 * 4 * buffer_pages) / vm_total_pages; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | |
| 540 | if (correction < 100) { |
| 541 | dirty_background_ratio *= correction; |
| 542 | dirty_background_ratio /= 100; |
| 543 | vm_dirty_ratio *= correction; |
| 544 | vm_dirty_ratio /= 100; |
| 545 | |
| 546 | if (dirty_background_ratio <= 0) |
| 547 | dirty_background_ratio = 1; |
| 548 | if (vm_dirty_ratio <= 0) |
| 549 | vm_dirty_ratio = 1; |
| 550 | } |
Bart Samwel | f6ef943 | 2006-03-24 03:15:48 -0800 | [diff] [blame] | 551 | mod_timer(&wb_timer, jiffies + dirty_writeback_interval); |
Chandra Seetharaman | 2d1d43f | 2006-09-29 02:01:25 -0700 | [diff] [blame] | 552 | writeback_set_ratelimit(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | register_cpu_notifier(&ratelimit_nb); |
| 554 | } |
| 555 | |
David Howells | 811d736 | 2006-08-29 19:06:09 +0100 | [diff] [blame] | 556 | /** |
| 557 | * generic_writepages - walk the list of dirty pages of the given |
| 558 | * address space and writepage() all of them. |
| 559 | * |
| 560 | * @mapping: address space structure to write |
| 561 | * @wbc: subtract the number of written pages from *@wbc->nr_to_write |
| 562 | * |
| 563 | * This is a library function, which implements the writepages() |
| 564 | * address_space_operation. |
| 565 | * |
| 566 | * If a page is already under I/O, generic_writepages() skips it, even |
| 567 | * if it's dirty. This is desirable behaviour for memory-cleaning writeback, |
| 568 | * but it is INCORRECT for data-integrity system calls such as fsync(). fsync() |
| 569 | * and msync() need to guarantee that all the data which was dirty at the time |
| 570 | * the call was made get new I/O started against them. If wbc->sync_mode is |
| 571 | * WB_SYNC_ALL then we were called for data integrity and we must wait for |
| 572 | * existing IO to complete. |
| 573 | * |
| 574 | * Derived from mpage_writepages() - if you fix this you should check that |
| 575 | * also! |
| 576 | */ |
| 577 | int generic_writepages(struct address_space *mapping, |
| 578 | struct writeback_control *wbc) |
| 579 | { |
| 580 | struct backing_dev_info *bdi = mapping->backing_dev_info; |
| 581 | int ret = 0; |
| 582 | int done = 0; |
| 583 | int (*writepage)(struct page *page, struct writeback_control *wbc); |
| 584 | struct pagevec pvec; |
| 585 | int nr_pages; |
| 586 | pgoff_t index; |
| 587 | pgoff_t end; /* Inclusive */ |
| 588 | int scanned = 0; |
| 589 | int range_whole = 0; |
| 590 | |
| 591 | if (wbc->nonblocking && bdi_write_congested(bdi)) { |
| 592 | wbc->encountered_congestion = 1; |
| 593 | return 0; |
| 594 | } |
| 595 | |
| 596 | writepage = mapping->a_ops->writepage; |
| 597 | |
| 598 | /* deal with chardevs and other special file */ |
| 599 | if (!writepage) |
| 600 | return 0; |
| 601 | |
| 602 | pagevec_init(&pvec, 0); |
| 603 | if (wbc->range_cyclic) { |
| 604 | index = mapping->writeback_index; /* Start from prev offset */ |
| 605 | end = -1; |
| 606 | } else { |
| 607 | index = wbc->range_start >> PAGE_CACHE_SHIFT; |
| 608 | end = wbc->range_end >> PAGE_CACHE_SHIFT; |
| 609 | if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) |
| 610 | range_whole = 1; |
| 611 | scanned = 1; |
| 612 | } |
| 613 | retry: |
| 614 | while (!done && (index <= end) && |
| 615 | (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, |
| 616 | PAGECACHE_TAG_DIRTY, |
| 617 | min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) { |
| 618 | unsigned i; |
| 619 | |
| 620 | scanned = 1; |
| 621 | for (i = 0; i < nr_pages; i++) { |
| 622 | struct page *page = pvec.pages[i]; |
| 623 | |
| 624 | /* |
| 625 | * At this point we hold neither mapping->tree_lock nor |
| 626 | * lock on the page itself: the page may be truncated or |
| 627 | * invalidated (changing page->mapping to NULL), or even |
| 628 | * swizzled back from swapper_space to tmpfs file |
| 629 | * mapping |
| 630 | */ |
| 631 | lock_page(page); |
| 632 | |
| 633 | if (unlikely(page->mapping != mapping)) { |
| 634 | unlock_page(page); |
| 635 | continue; |
| 636 | } |
| 637 | |
| 638 | if (!wbc->range_cyclic && page->index > end) { |
| 639 | done = 1; |
| 640 | unlock_page(page); |
| 641 | continue; |
| 642 | } |
| 643 | |
| 644 | if (wbc->sync_mode != WB_SYNC_NONE) |
| 645 | wait_on_page_writeback(page); |
| 646 | |
| 647 | if (PageWriteback(page) || |
| 648 | !clear_page_dirty_for_io(page)) { |
| 649 | unlock_page(page); |
| 650 | continue; |
| 651 | } |
| 652 | |
| 653 | ret = (*writepage)(page, wbc); |
| 654 | if (ret) { |
| 655 | if (ret == -ENOSPC) |
| 656 | set_bit(AS_ENOSPC, &mapping->flags); |
| 657 | else |
| 658 | set_bit(AS_EIO, &mapping->flags); |
| 659 | } |
| 660 | |
| 661 | if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) |
| 662 | unlock_page(page); |
| 663 | if (ret || (--(wbc->nr_to_write) <= 0)) |
| 664 | done = 1; |
| 665 | if (wbc->nonblocking && bdi_write_congested(bdi)) { |
| 666 | wbc->encountered_congestion = 1; |
| 667 | done = 1; |
| 668 | } |
| 669 | } |
| 670 | pagevec_release(&pvec); |
| 671 | cond_resched(); |
| 672 | } |
| 673 | if (!scanned && !done) { |
| 674 | /* |
| 675 | * We hit the last page and there is more work to be done: wrap |
| 676 | * back to the start of the file |
| 677 | */ |
| 678 | scanned = 1; |
| 679 | index = 0; |
| 680 | goto retry; |
| 681 | } |
| 682 | if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) |
| 683 | mapping->writeback_index = index; |
| 684 | return ret; |
| 685 | } |
| 686 | |
| 687 | EXPORT_SYMBOL(generic_writepages); |
| 688 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | int do_writepages(struct address_space *mapping, struct writeback_control *wbc) |
| 690 | { |
Andrew Morton | 22905f7 | 2005-11-16 15:07:01 -0800 | [diff] [blame] | 691 | int ret; |
| 692 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | if (wbc->nr_to_write <= 0) |
| 694 | return 0; |
Andrew Morton | 22905f7 | 2005-11-16 15:07:01 -0800 | [diff] [blame] | 695 | wbc->for_writepages = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | if (mapping->a_ops->writepages) |
Peter Zijlstra | d08b385 | 2006-09-25 23:30:57 -0700 | [diff] [blame] | 697 | ret = mapping->a_ops->writepages(mapping, wbc); |
Andrew Morton | 22905f7 | 2005-11-16 15:07:01 -0800 | [diff] [blame] | 698 | else |
| 699 | ret = generic_writepages(mapping, wbc); |
| 700 | wbc->for_writepages = 0; |
| 701 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | /** |
| 705 | * write_one_page - write out a single page and optionally wait on I/O |
| 706 | * |
Martin Waitz | 67be2dd | 2005-05-01 08:59:26 -0700 | [diff] [blame] | 707 | * @page: the page to write |
| 708 | * @wait: if true, wait on writeout |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | * |
| 710 | * The page must be locked by the caller and will be unlocked upon return. |
| 711 | * |
| 712 | * write_one_page() returns a negative error code if I/O failed. |
| 713 | */ |
| 714 | int write_one_page(struct page *page, int wait) |
| 715 | { |
| 716 | struct address_space *mapping = page->mapping; |
| 717 | int ret = 0; |
| 718 | struct writeback_control wbc = { |
| 719 | .sync_mode = WB_SYNC_ALL, |
| 720 | .nr_to_write = 1, |
| 721 | }; |
| 722 | |
| 723 | BUG_ON(!PageLocked(page)); |
| 724 | |
| 725 | if (wait) |
| 726 | wait_on_page_writeback(page); |
| 727 | |
| 728 | if (clear_page_dirty_for_io(page)) { |
| 729 | page_cache_get(page); |
| 730 | ret = mapping->a_ops->writepage(page, &wbc); |
| 731 | if (ret == 0 && wait) { |
| 732 | wait_on_page_writeback(page); |
| 733 | if (PageError(page)) |
| 734 | ret = -EIO; |
| 735 | } |
| 736 | page_cache_release(page); |
| 737 | } else { |
| 738 | unlock_page(page); |
| 739 | } |
| 740 | return ret; |
| 741 | } |
| 742 | EXPORT_SYMBOL(write_one_page); |
| 743 | |
| 744 | /* |
| 745 | * For address_spaces which do not use buffers. Just tag the page as dirty in |
| 746 | * its radix tree. |
| 747 | * |
| 748 | * This is also used when a single buffer is being dirtied: we want to set the |
| 749 | * page dirty in that case, but not all the buffers. This is a "bottom-up" |
| 750 | * dirtying, whereas __set_page_dirty_buffers() is a "top-down" dirtying. |
| 751 | * |
| 752 | * Most callers have locked the page, which pins the address_space in memory. |
| 753 | * But zap_pte_range() does not lock the page, however in that case the |
| 754 | * mapping is pinned by the vma's ->vm_file reference. |
| 755 | * |
| 756 | * We take care to handle the case where the page was truncated from the |
| 757 | * mapping by re-checking page_mapping() insode tree_lock. |
| 758 | */ |
| 759 | int __set_page_dirty_nobuffers(struct page *page) |
| 760 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | if (!TestSetPageDirty(page)) { |
| 762 | struct address_space *mapping = page_mapping(page); |
| 763 | struct address_space *mapping2; |
| 764 | |
Andrew Morton | 8c08540 | 2006-12-10 02:19:24 -0800 | [diff] [blame] | 765 | if (!mapping) |
| 766 | return 1; |
| 767 | |
| 768 | write_lock_irq(&mapping->tree_lock); |
| 769 | mapping2 = page_mapping(page); |
| 770 | if (mapping2) { /* Race with truncate? */ |
| 771 | BUG_ON(mapping2 != mapping); |
Andrew Morton | 55e829a | 2006-12-10 02:19:27 -0800 | [diff] [blame] | 772 | if (mapping_cap_account_dirty(mapping)) { |
Andrew Morton | 8c08540 | 2006-12-10 02:19:24 -0800 | [diff] [blame] | 773 | __inc_zone_page_state(page, NR_FILE_DIRTY); |
Andrew Morton | 55e829a | 2006-12-10 02:19:27 -0800 | [diff] [blame] | 774 | task_io_account_write(PAGE_CACHE_SIZE); |
| 775 | } |
Andrew Morton | 8c08540 | 2006-12-10 02:19:24 -0800 | [diff] [blame] | 776 | radix_tree_tag_set(&mapping->page_tree, |
| 777 | page_index(page), PAGECACHE_TAG_DIRTY); |
| 778 | } |
| 779 | write_unlock_irq(&mapping->tree_lock); |
| 780 | if (mapping->host) { |
| 781 | /* !PageAnon && !swapper_space */ |
| 782 | __mark_inode_dirty(mapping->host, I_DIRTY_PAGES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | } |
Andrew Morton | 4741c9f | 2006-03-24 03:18:11 -0800 | [diff] [blame] | 784 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | } |
Andrew Morton | 4741c9f | 2006-03-24 03:18:11 -0800 | [diff] [blame] | 786 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | } |
| 788 | EXPORT_SYMBOL(__set_page_dirty_nobuffers); |
| 789 | |
| 790 | /* |
| 791 | * When a writepage implementation decides that it doesn't want to write this |
| 792 | * page for some reason, it should redirty the locked page via |
| 793 | * redirty_page_for_writepage() and it should then unlock the page and return 0 |
| 794 | */ |
| 795 | int redirty_page_for_writepage(struct writeback_control *wbc, struct page *page) |
| 796 | { |
| 797 | wbc->pages_skipped++; |
| 798 | return __set_page_dirty_nobuffers(page); |
| 799 | } |
| 800 | EXPORT_SYMBOL(redirty_page_for_writepage); |
| 801 | |
| 802 | /* |
| 803 | * If the mapping doesn't provide a set_page_dirty a_op, then |
| 804 | * just fall through and assume that it wants buffer_heads. |
| 805 | */ |
| 806 | int fastcall set_page_dirty(struct page *page) |
| 807 | { |
| 808 | struct address_space *mapping = page_mapping(page); |
| 809 | |
| 810 | if (likely(mapping)) { |
| 811 | int (*spd)(struct page *) = mapping->a_ops->set_page_dirty; |
David Howells | 9361401 | 2006-09-30 20:45:40 +0200 | [diff] [blame] | 812 | #ifdef CONFIG_BLOCK |
| 813 | if (!spd) |
| 814 | spd = __set_page_dirty_buffers; |
| 815 | #endif |
| 816 | return (*spd)(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | } |
Andrew Morton | 4741c9f | 2006-03-24 03:18:11 -0800 | [diff] [blame] | 818 | if (!PageDirty(page)) { |
| 819 | if (!TestSetPageDirty(page)) |
| 820 | return 1; |
| 821 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | return 0; |
| 823 | } |
| 824 | EXPORT_SYMBOL(set_page_dirty); |
| 825 | |
| 826 | /* |
| 827 | * set_page_dirty() is racy if the caller has no reference against |
| 828 | * page->mapping->host, and if the page is unlocked. This is because another |
| 829 | * CPU could truncate the page off the mapping and then free the mapping. |
| 830 | * |
| 831 | * Usually, the page _is_ locked, or the caller is a user-space process which |
| 832 | * holds a reference on the inode by having an open file. |
| 833 | * |
| 834 | * In other cases, the page should be locked before running set_page_dirty(). |
| 835 | */ |
| 836 | int set_page_dirty_lock(struct page *page) |
| 837 | { |
| 838 | int ret; |
| 839 | |
Nick Piggin | db37648 | 2006-09-25 23:31:24 -0700 | [diff] [blame] | 840 | lock_page_nosync(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 841 | ret = set_page_dirty(page); |
| 842 | unlock_page(page); |
| 843 | return ret; |
| 844 | } |
| 845 | EXPORT_SYMBOL(set_page_dirty_lock); |
| 846 | |
| 847 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | * Clear a page's dirty flag, while caring for dirty memory accounting. |
| 849 | * Returns true if the page was previously dirty. |
| 850 | * |
| 851 | * This is for preparing to put the page under writeout. We leave the page |
| 852 | * tagged as dirty in the radix tree so that a concurrent write-for-sync |
| 853 | * can discover it via a PAGECACHE_TAG_DIRTY walk. The ->writepage |
| 854 | * implementation will run either set_page_writeback() or set_page_dirty(), |
| 855 | * at which stage we bring the page's dirty flag and radix-tree dirty tag |
| 856 | * back into sync. |
| 857 | * |
| 858 | * This incoherency between the page's dirty flag and radix-tree tag is |
| 859 | * unfortunate, but it only exists while the page is locked. |
| 860 | */ |
| 861 | int clear_page_dirty_for_io(struct page *page) |
| 862 | { |
| 863 | struct address_space *mapping = page_mapping(page); |
| 864 | |
Linus Torvalds | 7658cc2 | 2006-12-29 10:00:58 -0800 | [diff] [blame] | 865 | if (mapping && mapping_cap_account_dirty(mapping)) { |
| 866 | /* |
| 867 | * Yes, Virginia, this is indeed insane. |
| 868 | * |
| 869 | * We use this sequence to make sure that |
| 870 | * (a) we account for dirty stats properly |
| 871 | * (b) we tell the low-level filesystem to |
| 872 | * mark the whole page dirty if it was |
| 873 | * dirty in a pagetable. Only to then |
| 874 | * (c) clean the page again and return 1 to |
| 875 | * cause the writeback. |
| 876 | * |
| 877 | * This way we avoid all nasty races with the |
| 878 | * dirty bit in multiple places and clearing |
| 879 | * them concurrently from different threads. |
| 880 | * |
| 881 | * Note! Normally the "set_page_dirty(page)" |
| 882 | * has no effect on the actual dirty bit - since |
| 883 | * that will already usually be set. But we |
| 884 | * need the side effects, and it can help us |
| 885 | * avoid races. |
| 886 | * |
| 887 | * We basically use the page "master dirty bit" |
| 888 | * as a serialization point for all the different |
| 889 | * threads doing their things. |
| 890 | * |
| 891 | * FIXME! We still have a race here: if somebody |
| 892 | * adds the page back to the page tables in |
| 893 | * between the "page_mkclean()" and the "TestClearPageDirty()", |
| 894 | * we might have it mapped without the dirty bit set. |
| 895 | */ |
| 896 | if (page_mkclean(page)) |
| 897 | set_page_dirty(page); |
| 898 | if (TestClearPageDirty(page)) { |
Andrew Morton | 8c08540 | 2006-12-10 02:19:24 -0800 | [diff] [blame] | 899 | dec_zone_page_state(page, NR_FILE_DIRTY); |
Linus Torvalds | 7658cc2 | 2006-12-29 10:00:58 -0800 | [diff] [blame] | 900 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | } |
Linus Torvalds | 7658cc2 | 2006-12-29 10:00:58 -0800 | [diff] [blame] | 902 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | } |
Linus Torvalds | 7658cc2 | 2006-12-29 10:00:58 -0800 | [diff] [blame] | 904 | return TestClearPageDirty(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | } |
Hans Reiser | 58bb01a | 2005-11-18 01:10:53 -0800 | [diff] [blame] | 906 | EXPORT_SYMBOL(clear_page_dirty_for_io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 907 | |
| 908 | int test_clear_page_writeback(struct page *page) |
| 909 | { |
| 910 | struct address_space *mapping = page_mapping(page); |
| 911 | int ret; |
| 912 | |
| 913 | if (mapping) { |
| 914 | unsigned long flags; |
| 915 | |
| 916 | write_lock_irqsave(&mapping->tree_lock, flags); |
| 917 | ret = TestClearPageWriteback(page); |
| 918 | if (ret) |
| 919 | radix_tree_tag_clear(&mapping->page_tree, |
| 920 | page_index(page), |
| 921 | PAGECACHE_TAG_WRITEBACK); |
| 922 | write_unlock_irqrestore(&mapping->tree_lock, flags); |
| 923 | } else { |
| 924 | ret = TestClearPageWriteback(page); |
| 925 | } |
| 926 | return ret; |
| 927 | } |
| 928 | |
| 929 | int test_set_page_writeback(struct page *page) |
| 930 | { |
| 931 | struct address_space *mapping = page_mapping(page); |
| 932 | int ret; |
| 933 | |
| 934 | if (mapping) { |
| 935 | unsigned long flags; |
| 936 | |
| 937 | write_lock_irqsave(&mapping->tree_lock, flags); |
| 938 | ret = TestSetPageWriteback(page); |
| 939 | if (!ret) |
| 940 | radix_tree_tag_set(&mapping->page_tree, |
| 941 | page_index(page), |
| 942 | PAGECACHE_TAG_WRITEBACK); |
| 943 | if (!PageDirty(page)) |
| 944 | radix_tree_tag_clear(&mapping->page_tree, |
| 945 | page_index(page), |
| 946 | PAGECACHE_TAG_DIRTY); |
| 947 | write_unlock_irqrestore(&mapping->tree_lock, flags); |
| 948 | } else { |
| 949 | ret = TestSetPageWriteback(page); |
| 950 | } |
| 951 | return ret; |
| 952 | |
| 953 | } |
| 954 | EXPORT_SYMBOL(test_set_page_writeback); |
| 955 | |
| 956 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | * Return true if any of the pages in the mapping are marged with the |
| 958 | * passed tag. |
| 959 | */ |
| 960 | int mapping_tagged(struct address_space *mapping, int tag) |
| 961 | { |
| 962 | unsigned long flags; |
| 963 | int ret; |
| 964 | |
| 965 | read_lock_irqsave(&mapping->tree_lock, flags); |
| 966 | ret = radix_tree_tagged(&mapping->page_tree, tag); |
| 967 | read_unlock_irqrestore(&mapping->tree_lock, flags); |
| 968 | return ret; |
| 969 | } |
| 970 | EXPORT_SYMBOL(mapping_tagged); |