1 /*
2 * mm/page-writeback.c.
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>
24 #include <linux/blkdev.h>
25 #include <linux/mpage.h>
26 #include <linux/percpu.h>
27 #include <linux/notifier.h>
28 #include <linux/smp.h>
29 #include <linux/sysctl.h>
30 #include <linux/cpu.h>
31 #include <linux/syscalls.h>
32
33 /*
34 * The maximum number of pages to writeout in a single bdflush/kupdate
35 * operation. We do this so we don't hold I_LOCK against an inode for
36 * enormous amounts of time, which would block a userspace task which has
37 * been forced to throttle against that inode. Also, the code reevaluates
38 * the dirty each time it has written this many pages.
39 */
40 #define MAX_WRITEBACK_PAGES 1024
41
42 /*
43 * After a CPU has dirtied this many pages, balance_dirty_pages_ratelimited
44 * will look to see if it needs to force writeback or throttling.
45 */
46 static long ratelimit_pages = 32;
47
48 static long total_pages; /* The total number of pages in the machine. */
49 static int dirty_exceeded; /* Dirty mem may be over limit */
50
51 /*
52 * When balance_dirty_pages decides that the caller needs to perform some
53 * non-background writeback, this is how many pages it will attempt to write.
54 * It should be somewhat larger than RATELIMIT_PAGES to ensure that reasonably
55 * large amounts of I/O are submitted.
56 */
57 static inline long sync_writeback_pages(void)
58 {
59 return ratelimit_pages + ratelimit_pages / 2;
60 }
61
62 /* The following parameters are exported via /proc/sys/vm */
63
64 /*
65 * Start background writeback (via pdflush) at this percentage
66 */
67 int dirty_background_ratio = 10;
68
69 /*
70 * The generator of dirty data starts writeback at this percentage
71 */
72 int vm_dirty_ratio = 40;
73
74 /*
75 * The interval between `kupdate'-style writebacks, in centiseconds
76 * (hundredths of a second)
77 */
78 int dirty_writeback_centisecs = 5 * 100;
79
80 /*
81 * The longest number of centiseconds for which data is allowed to remain dirty
82 */
83 int dirty_expire_centisecs = 30 * 100;
84
85 /*
86 * Flag that makes the machine dump writes/reads and block dirtyings.
87 */
88 int block_dump;
89
90 /*
91 * Flag that puts the machine in "laptop mode".
92 */
93 int laptop_mode;
94
95 EXPORT_SYMBOL(laptop_mode);
96
97 /* End of sysctl-exported parameters */
98
99
100 static void background_writeout(unsigned long _min_pages);
101
102 struct writeback_state
103 {
104 unsigned long nr_dirty;
105 unsigned long nr_unstable;
106 unsigned long nr_mapped;
107 unsigned long nr_writeback;
108 };
109
110 static void get_writeback_state(struct writeback_state *wbs)
111 {
112 wbs->nr_dirty = read_page_state(nr_dirty);
113 wbs->nr_unstable = read_page_state(nr_unstable);
114 wbs->nr_mapped = read_page_state(nr_mapped);
115 wbs->nr_writeback = read_page_state(nr_writeback);
116 }
117
118 /*
119 * Work out the current dirty-memory clamping and background writeout
120 * thresholds.
121 *
122 * The main aim here is to lower them aggressively if there is a lot of mapped
123 * memory around. To avoid stressing page reclaim with lots of unreclaimable
124 * pages. It is better to clamp down on writers than to start swapping, and
125 * performing lots of scanning.
126 *
127 * We only allow 1/2 of the currently-unmapped memory to be dirtied.
128 *
129 * We don't permit the clamping level to fall below 5% - that is getting rather
130 * excessive.
131 *
132 * We make sure that the background writeout level is below the adjusted
133 * clamping level.
134 */
135 static void
136 get_dirty_limits(struct writeback_state *wbs, long *pbackground, long *pdirty,
137 struct address_space *mapping)
138 {
139 int background_ratio; /* Percentages */
140 int dirty_ratio;
141 int unmapped_ratio;
142 long background;
143 long dirty;
144 unsigned long available_memory = total_pages;
145 struct task_struct *tsk;
146
147 get_writeback_state(wbs);
148
149 #ifdef CONFIG_HIGHMEM
150 /*
151 * If this mapping can only allocate from low memory,
152 * we exclude high memory from our count.
153 */
154 if (mapping && !(mapping_gfp_mask(mapping) & __GFP_HIGHMEM))
155 available_memory -= totalhigh_pages;
156 #endif
157
158
159 unmapped_ratio = 100 - (wbs->nr_mapped * 100) / total_pages;
160
161 dirty_ratio = vm_dirty_ratio;
162 if (dirty_ratio > unmapped_ratio / 2)
163 dirty_ratio = unmapped_ratio / 2;
164
165 if (dirty_ratio < 5)
166 dirty_ratio = 5;
167
168 background_ratio = dirty_background_ratio;
169 if (background_ratio >= dirty_ratio)
170 background_ratio = dirty_ratio / 2;
171
172 background = (background_ratio * available_memory) / 100;
173 dirty = (dirty_ratio * available_memory) / 100;
174 tsk = current;
175 if (tsk->flags & PF_LESS_THROTTLE || rt_task(tsk)) {
176 background += background / 4;
177 dirty += dirty / 4;
178 }
179 *pbackground = background;
180 *pdirty = dirty;
181 }
182
183 /*
184 * balance_dirty_pages() must be called by processes which are generating dirty
185 * data. It looks at the number of dirty pages in the machine and will force
186 * the caller to perform writeback if the system is over `vm_dirty_ratio'.
187 * If we're over `background_thresh' then pdflush is woken to perform some
188 * writeout.
189 */
190 static void balance_dirty_pages(struct address_space *mapping)
191 {
192 struct writeback_state wbs;
193 long nr_reclaimable;
194 long background_thresh;
195 long dirty_thresh;
196 unsigned long pages_written = 0;
197 unsigned long write_chunk = sync_writeback_pages();
198
199 struct backing_dev_info *bdi = mapping->backing_dev_info;
200
201 for (;;) {
202 struct writeback_control wbc = {
203 .bdi = bdi,
204 .sync_mode = WB_SYNC_NONE,
205 .older_than_this = NULL,
206 .nr_to_write = write_chunk,
207 };
208
209 get_dirty_limits(&wbs, &background_thresh,
210 &dirty_thresh, mapping);
211 nr_reclaimable = wbs.nr_dirty + wbs.nr_unstable;
212 if (nr_reclaimable + wbs.nr_writeback <= dirty_thresh)
213 break;
214
215 dirty_exceeded = 1;
216
217 /* Note: nr_reclaimable denotes nr_dirty + nr_unstable.
218 * Unstable writes are a feature of certain networked
219 * filesystems (i.e. NFS) in which data may have been
220 * written to the server's write cache, but has not yet
221 * been flushed to permanent storage.
222 */
223 if (nr_reclaimable) {
224 writeback_inodes(&wbc);
225 get_dirty_limits(&wbs, &background_thresh,
226 &dirty_thresh, mapping);
227 nr_reclaimable = wbs.nr_dirty + wbs.nr_unstable;
228 if (nr_reclaimable + wbs.nr_writeback <= dirty_thresh)
229 break;
230 pages_written += write_chunk - wbc.nr_to_write;
231 if (pages_written >= write_chunk)
232 break; /* We've done our duty */
233 }
234 blk_congestion_wait(WRITE, HZ/10);
235 }
236
237 if (nr_reclaimable + wbs.nr_writeback <= dirty_thresh)
238 dirty_exceeded = 0;
239
240 if (writeback_in_progress(bdi))
241 return; /* pdflush is already working this queue */
242
243 /*
244 * In laptop mode, we wait until hitting the higher threshold before
245 * starting background writeout, and then write out all the way down
246 * to the lower threshold. So slow writers cause minimal disk activity.
247 *
248 * In normal mode, we start background writeout at the lower
249 * background_thresh, to keep the amount of dirty memory low.
250 */
251 if ((laptop_mode && pages_written) ||
252 (!laptop_mode && (nr_reclaimable > background_thresh)))
253 pdflush_operation(background_writeout, 0);
254 }
255
256 /**
257 * balance_dirty_pages_ratelimited - balance dirty memory state
258 * @mapping - address_space which was dirtied
259 *
260 * Processes which are dirtying memory should call in here once for each page
261 * which was newly dirtied. The function will periodically check the system's
262 * dirty state and will initiate writeback if needed.
263 *
264 * On really big machines, get_writeback_state is expensive, so try to avoid
265 * calling it too often (ratelimiting). But once we're over the dirty memory
266 * limit we decrease the ratelimiting by a lot, to prevent individual processes
267 * from overshooting the limit by (ratelimit_pages) each.
268 */
269 void balance_dirty_pages_ratelimited(struct address_space *mapping)
270 {
271 static DEFINE_PER_CPU(int, ratelimits) = 0;
272 long ratelimit;
273
274 ratelimit = ratelimit_pages;
275 if (dirty_exceeded)
276 ratelimit = 8;
277
278 /*
279 * Check the rate limiting. Also, we do not want to throttle real-time
280 * tasks in balance_dirty_pages(). Period.
281 */
282 if (get_cpu_var(ratelimits)++ >= ratelimit) {
283 __get_cpu_var(ratelimits) = 0;
284 put_cpu_var(ratelimits);
285 balance_dirty_pages(mapping);
286 return;
287 }
288 put_cpu_var(ratelimits);
289 }
290 EXPORT_SYMBOL(balance_dirty_pages_ratelimited);
291
292 /*
293 * writeback at least _min_pages, and keep writing until the amount of dirty
294 * memory is less than the background threshold, or until we're all clean.
295 */
296 static void background_writeout(unsigned long _min_pages)
297 {
298 long min_pages = _min_pages;
299 struct writeback_control wbc = {
300 .bdi = NULL,
301 .sync_mode = WB_SYNC_NONE,
302 .older_than_this = NULL,
303 .nr_to_write = 0,
304 .nonblocking = 1,
305 };
306
307 for ( ; ; ) {
308 struct writeback_state wbs;
309 long background_thresh;
310 long dirty_thresh;
311
312 get_dirty_limits(&wbs, &background_thresh, &dirty_thresh, NULL);
313 if (wbs.nr_dirty + wbs.nr_unstable < background_thresh
314 && min_pages <= 0)
315 break;
316 wbc.encountered_congestion = 0;
317 wbc.nr_to_write = MAX_WRITEBACK_PAGES;
318 wbc.pages_skipped = 0;
319 writeback_inodes(&wbc);
320 min_pages -= MAX_WRITEBACK_PAGES - wbc.nr_to_write;
321 if (wbc.nr_to_write > 0 || wbc.pages_skipped > 0) {
322 /* Wrote less than expected */
323 blk_congestion_wait(WRITE, HZ/10);
324 if (!wbc.encountered_congestion)
325 break;
326 }
327 }
328 }
329
330 /*
331 * Start writeback of `nr_pages' pages. If `nr_pages' is zero, write back
332 * the whole world. Returns 0 if a pdflush thread was dispatched. Returns
333 * -1 if all pdflush threads were busy.
334 */
335 int wakeup_bdflush(long nr_pages)
336 {
337 if (nr_pages == 0) {
338 struct writeback_state wbs;
339
340 get_writeback_state(&wbs);
341 nr_pages = wbs.nr_dirty + wbs.nr_unstable;
342 }
343 return pdflush_operation(background_writeout, nr_pages);
344 }
345
346 static void wb_timer_fn(unsigned long unused);
347 static void laptop_timer_fn(unsigned long unused);
348
349 static struct timer_list wb_timer =
350 TIMER_INITIALIZER(wb_timer_fn, 0, 0);
351 static struct timer_list laptop_mode_wb_timer =
352 TIMER_INITIALIZER(laptop_timer_fn, 0, 0);
353
354 /*
355 * Periodic writeback of "old" data.
356 *
357 * Define "old": the first time one of an inode's pages is dirtied, we mark the
358 * dirtying-time in the inode's address_space. So this periodic writeback code
359 * just walks the superblock inode list, writing back any inodes which are
360 * older than a specific point in time.
361 *
362 * Try to run once per dirty_writeback_centisecs. But if a writeback event
363 * takes longer than a dirty_writeback_centisecs interval, then leave a
364 * one-second gap.
365 *
366 * older_than_this takes precedence over nr_to_write. So we'll only write back
367 * all dirty pages if they are all attached to "old" mappings.
368 */
369 static void wb_kupdate(unsigned long arg)
370 {
371 unsigned long oldest_jif;
372 unsigned long start_jif;
373 unsigned long next_jif;
374 long nr_to_write;
375 struct writeback_state wbs;
376 struct writeback_control wbc = {
377 .bdi = NULL,
378 .sync_mode = WB_SYNC_NONE,
379 .older_than_this = &oldest_jif,
380 .nr_to_write = 0,
381 .nonblocking = 1,
382 .for_kupdate = 1,
383 };
384
385 sync_supers();
386
387 get_writeback_state(&wbs);
388 oldest_jif = jiffies - (dirty_expire_centisecs * HZ) / 100;
389 start_jif = jiffies;
390 next_jif = start_jif + (dirty_writeback_centisecs * HZ) / 100;
391 nr_to_write = wbs.nr_dirty + wbs.nr_unstable +
392 (inodes_stat.nr_inodes - inodes_stat.nr_unused);
393 while (nr_to_write > 0) {
394 wbc.encountered_congestion = 0;
395 wbc.nr_to_write = MAX_WRITEBACK_PAGES;
396 writeback_inodes(&wbc);
397 if (wbc.nr_to_write > 0) {
398 if (wbc.encountered_congestion)
399 blk_congestion_wait(WRITE, HZ/10);
400 else
401 break; /* All the old data is written */
402 }
403 nr_to_write -= MAX_WRITEBACK_PAGES - wbc.nr_to_write;
404 }
405 if (time_before(next_jif, jiffies + HZ))
406 next_jif = jiffies + HZ;
407 if (dirty_writeback_centisecs)
408 mod_timer(&wb_timer, next_jif);
409 }
410
411 /*
412 * sysctl handler for /proc/sys/vm/dirty_writeback_centisecs
413 */
414 int dirty_writeback_centisecs_handler(ctl_table *table, int write,
415 struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
416 {
417 proc_dointvec(table, write, file, buffer, length, ppos);
418 if (dirty_writeback_centisecs) {
419 mod_timer(&wb_timer,
420 jiffies + (dirty_writeback_centisecs * HZ) / 100);
421 } else {
422 del_timer(&wb_timer);
423 }
424 return 0;
425 }
426
427 static void wb_timer_fn(unsigned long unused)
428 {
429 if (pdflush_operation(wb_kupdate, 0) < 0)
430 mod_timer(&wb_timer, jiffies + HZ); /* delay 1 second */
431 }
432
433 static void laptop_flush(unsigned long unused)
434 {
435 sys_sync();
436 }
437
438 static void laptop_timer_fn(unsigned long unused)
439 {
440 pdflush_operation(laptop_flush, 0);
441 }
442
443 /*
444 * We've spun up the disk and we're in laptop mode: schedule writeback
445 * of all dirty data a few seconds from now. If the flush is already scheduled
446 * then push it back - the user is still using the disk.
447 */
448 void laptop_io_completion(void)
449 {
450 mod_timer(&laptop_mode_wb_timer, jiffies + laptop_mode * HZ);
451 }
452
453 /*
454 * We're in laptop mode and we've just synced. The sync's writes will have
455 * caused another writeback to be scheduled by laptop_io_completion.
456 * Nothing needs to be written back anymore, so we unschedule the writeback.
457 */
458 void laptop_sync_completion(void)
459 {
460 del_timer(&laptop_mode_wb_timer);
461 }
462
463 /*
464 * If ratelimit_pages is too high then we can get into dirty-data overload
465 * if a large number of processes all perform writes at the same time.
466 * If it is too low then SMP machines will call the (expensive)
467 * get_writeback_state too often.
468 *
469 * Here we set ratelimit_pages to a level which ensures that when all CPUs are
470 * dirtying in parallel, we cannot go more than 3% (1/32) over the dirty memory
471 * thresholds before writeback cuts in.
472 *
473 * But the limit should not be set too high. Because it also controls the
474 * amount of memory which the balance_dirty_pages() caller has to write back.
475 * If this is too large then the caller will block on the IO queue all the
476 * time. So limit it to four megabytes - the balance_dirty_pages() caller
477 * will write six megabyte chunks, max.
478 */
479
480 static void set_ratelimit(void)
481 {
482 ratelimit_pages = total_pages / (num_online_cpus() * 32);
483 if (ratelimit_pages < 16)
484 ratelimit_pages = 16;
485 if (ratelimit_pages * PAGE_CACHE_SIZE > 4096 * 1024)
486 ratelimit_pages = (4096 * 1024) / PAGE_CACHE_SIZE;
487 }
488
489 static int
490 ratelimit_handler(struct notifier_block *self, unsigned long u, void *v)
491 {
492 set_ratelimit();
493 return 0;
494 }
495
496 static struct notifier_block ratelimit_nb = {
497 .notifier_call = ratelimit_handler,
498 .next = NULL,
499 };
500
501 /*
502 * If the machine has a large highmem:lowmem ratio then scale back the default
503 * dirty memory thresholds: allowing too much dirty highmem pins an excessive
504 * number of buffer_heads.
505 */
506 void __init page_writeback_init(void)
507 {
508 long buffer_pages = nr_free_buffer_pages();
509 long correction;
510
511 total_pages = nr_free_pagecache_pages();
512
513 correction = (100 * 4 * buffer_pages) / total_pages;
514
515 if (correction < 100) {
516 dirty_background_ratio *= correction;
517 dirty_background_ratio /= 100;
518 vm_dirty_ratio *= correction;
519 vm_dirty_ratio /= 100;
520
521 if (dirty_background_ratio <= 0)
522 dirty_background_ratio = 1;
523 if (vm_dirty_ratio <= 0)
524 vm_dirty_ratio = 1;
525 }
526 mod_timer(&wb_timer, jiffies + (dirty_writeback_centisecs * HZ) / 100);
527 set_ratelimit();
528 register_cpu_notifier(&ratelimit_nb);
529 }
530
531 int do_writepages(struct address_space *mapping, struct writeback_control *wbc)
532 {
533 if (wbc->nr_to_write <= 0)
534 return 0;
535 if (mapping->a_ops->writepages)
536 return mapping->a_ops->writepages(mapping, wbc);
537 return generic_writepages(mapping, wbc);
538 }
539
540 /**
541 * write_one_page - write out a single page and optionally wait on I/O
542 *
543 * @page - the page to write
544 * @wait - if true, wait on writeout
545 *
546 * The page must be locked by the caller and will be unlocked upon return.
547 *
548 * write_one_page() returns a negative error code if I/O failed.
549 */
550 int write_one_page(struct page *page, int wait)
551 {
552 struct address_space *mapping = page->mapping;
553 int ret = 0;
554 struct writeback_control wbc = {
555 .sync_mode = WB_SYNC_ALL,
556 .nr_to_write = 1,
557 };
558
559 BUG_ON(!PageLocked(page));
560
561 if (wait)
562 wait_on_page_writeback(page);
563
564 if (clear_page_dirty_for_io(page)) {
565 page_cache_get(page);
566 ret = mapping->a_ops->writepage(page, &wbc);
567 if (ret == 0 && wait) {
568 wait_on_page_writeback(page);
569 if (PageError(page))
570 ret = -EIO;
571 }
572 page_cache_release(page);
573 } else {
574 unlock_page(page);
575 }
576 return ret;
577 }
578 EXPORT_SYMBOL(write_one_page);
579
580 /*
581 * For address_spaces which do not use buffers. Just tag the page as dirty in
582 * its radix tree.
583 *
584 * This is also used when a single buffer is being dirtied: we want to set the
585 * page dirty in that case, but not all the buffers. This is a "bottom-up"
586 * dirtying, whereas __set_page_dirty_buffers() is a "top-down" dirtying.
587 *
588 * Most callers have locked the page, which pins the address_space in memory.
589 * But zap_pte_range() does not lock the page, however in that case the
590 * mapping is pinned by the vma's ->vm_file reference.
591 *
592 * We take care to handle the case where the page was truncated from the
593 * mapping by re-checking page_mapping() insode tree_lock.
594 */
595 int __set_page_dirty_nobuffers(struct page *page)
596 {
597 int ret = 0;
598
599 if (!TestSetPageDirty(page)) {
600 struct address_space *mapping = page_mapping(page);
601 struct address_space *mapping2;
602
603 if (mapping) {
604 spin_lock_irq(&mapping->tree_lock);
605 mapping2 = page_mapping(page);
606 if (mapping2) { /* Race with truncate? */
607 BUG_ON(mapping2 != mapping);
608 if (!mapping->backing_dev_info->memory_backed)
609 inc_page_state(nr_dirty);
610 radix_tree_tag_set(&mapping->page_tree,
611 page_index(page), PAGECACHE_TAG_DIRTY);
612 }
613 spin_unlock_irq(&mapping->tree_lock);
614 if (mapping->host) {
615 /* !PageAnon && !swapper_space */
616 __mark_inode_dirty(mapping->host,
617 I_DIRTY_PAGES);
618 }
619 }
620 }
621 return ret;
622 }
623 EXPORT_SYMBOL(__set_page_dirty_nobuffers);
624
625 /*
626 * When a writepage implementation decides that it doesn't want to write this
627 * page for some reason, it should redirty the locked page via
628 * redirty_page_for_writepage() and it should then unlock the page and return 0
629 */
630 int redirty_page_for_writepage(struct writeback_control *wbc, struct page *page)
631 {
632 wbc->pages_skipped++;
633 return __set_page_dirty_nobuffers(page);
634 }
635 EXPORT_SYMBOL(redirty_page_for_writepage);
636
637 /*
638 * If the mapping doesn't provide a set_page_dirty a_op, then
639 * just fall through and assume that it wants buffer_heads.
640 */
641 int fastcall set_page_dirty(struct page *page)
642 {
643 struct address_space *mapping = page_mapping(page);
644
645 if (likely(mapping)) {
646 int (*spd)(struct page *) = mapping->a_ops->set_page_dirty;
647 if (spd)
648 return (*spd)(page);
649 return __set_page_dirty_buffers(page);
650 }
651 if (!PageDirty(page))
652 SetPageDirty(page);
653 return 0;
654 }
655 EXPORT_SYMBOL(set_page_dirty);
656
657 /*
658 * set_page_dirty() is racy if the caller has no reference against
659 * page->mapping->host, and if the page is unlocked. This is because another
660 * CPU could truncate the page off the mapping and then free the mapping.
661 *
662 * Usually, the page _is_ locked, or the caller is a user-space process which
663 * holds a reference on the inode by having an open file.
664 *
665 * In other cases, the page should be locked before running set_page_dirty().
666 */
667 int set_page_dirty_lock(struct page *page)
668 {
669 int ret;
670
671 lock_page(page);
672 ret = set_page_dirty(page);
673 unlock_page(page);
674 return ret;
675 }
676 EXPORT_SYMBOL(set_page_dirty_lock);
677
678 /*
679 * Clear a page's dirty flag, while caring for dirty memory accounting.
680 * Returns true if the page was previously dirty.
681 */
682 int test_clear_page_dirty(struct page *page)
683 {
684 struct address_space *mapping = page_mapping(page);
685 unsigned long flags;
686
687 if (mapping) {
688 spin_lock_irqsave(&mapping->tree_lock, flags);
689 if (TestClearPageDirty(page)) {
690 radix_tree_tag_clear(&mapping->page_tree,
691 page_index(page),
692 PAGECACHE_TAG_DIRTY);
693 spin_unlock_irqrestore(&mapping->tree_lock, flags);
694 if (!mapping->backing_dev_info->memory_backed)
695 dec_page_state(nr_dirty);
696 return 1;
697 }
698 spin_unlock_irqrestore(&mapping->tree_lock, flags);
699 return 0;
700 }
701 return TestClearPageDirty(page);
702 }
703 EXPORT_SYMBOL(test_clear_page_dirty);
704
705 /*
706 * Clear a page's dirty flag, while caring for dirty memory accounting.
707 * Returns true if the page was previously dirty.
708 *
709 * This is for preparing to put the page under writeout. We leave the page
710 * tagged as dirty in the radix tree so that a concurrent write-for-sync
711 * can discover it via a PAGECACHE_TAG_DIRTY walk. The ->writepage
712 * implementation will run either set_page_writeback() or set_page_dirty(),
713 * at which stage we bring the page's dirty flag and radix-tree dirty tag
714 * back into sync.
715 *
716 * This incoherency between the page's dirty flag and radix-tree tag is
717 * unfortunate, but it only exists while the page is locked.
718 */
719 int clear_page_dirty_for_io(struct page *page)
720 {
721 struct address_space *mapping = page_mapping(page);
722
723 if (mapping) {
724 if (TestClearPageDirty(page)) {
725 if (!mapping->backing_dev_info->memory_backed)
726 dec_page_state(nr_dirty);
727 return 1;
728 }
729 return 0;
730 }
731 return TestClearPageDirty(page);
732 }
733 EXPORT_SYMBOL(clear_page_dirty_for_io);
734
735 /*
736 * Clear a page's dirty flag while ignoring dirty memory accounting
737 */
738 int __clear_page_dirty(struct page *page)
739 {
740 struct address_space *mapping = page_mapping(page);
741
742 if (mapping) {
743 unsigned long flags;
744
745 spin_lock_irqsave(&mapping->tree_lock, flags);
746 if (TestClearPageDirty(page)) {
747 radix_tree_tag_clear(&mapping->page_tree,
748 page_index(page),
749 PAGECACHE_TAG_DIRTY);
750 spin_unlock_irqrestore(&mapping->tree_lock, flags);
751 return 1;
752 }
753 spin_unlock_irqrestore(&mapping->tree_lock, flags);
754 return 0;
755 }
756 return TestClearPageDirty(page);
757 }
758
759 int test_clear_page_writeback(struct page *page)
760 {
761 struct address_space *mapping = page_mapping(page);
762 int ret;
763
764 if (mapping) {
765 unsigned long flags;
766
767 spin_lock_irqsave(&mapping->tree_lock, flags);
768 ret = TestClearPageWriteback(page);
769 if (ret)
770 radix_tree_tag_clear(&mapping->page_tree,
771 page_index(page),
772 PAGECACHE_TAG_WRITEBACK);
773 spin_unlock_irqrestore(&mapping->tree_lock, flags);
774 } else {
775 ret = TestClearPageWriteback(page);
776 }
777 return ret;
778 }
779
780 int test_set_page_writeback(struct page *page)
781 {
782 struct address_space *mapping = page_mapping(page);
783 int ret;
784
785 if (mapping) {
786 unsigned long flags;
787
788 spin_lock_irqsave(&mapping->tree_lock, flags);
789 ret = TestSetPageWriteback(page);
790 if (!ret)
791 radix_tree_tag_set(&mapping->page_tree,
792 page_index(page),
793 PAGECACHE_TAG_WRITEBACK);
794 if (!PageDirty(page))
795 radix_tree_tag_clear(&mapping->page_tree,
796 page_index(page),
797 PAGECACHE_TAG_DIRTY);
798 spin_unlock_irqrestore(&mapping->tree_lock, flags);
799 } else {
800 ret = TestSetPageWriteback(page);
801 }
802 return ret;
803
804 }
805 EXPORT_SYMBOL(test_set_page_writeback);
806
807 /*
808 * Return true if any of the pages in the mapping are marged with the
809 * passed tag.
810 */
811 int mapping_tagged(struct address_space *mapping, int tag)
812 {
813 unsigned long flags;
814 int ret;
815
816 spin_lock_irqsave(&mapping->tree_lock, flags);
817 ret = radix_tree_tagged(&mapping->page_tree, tag);
818 spin_unlock_irqrestore(&mapping->tree_lock, flags);
819 return ret;
820 }
821 EXPORT_SYMBOL(mapping_tagged);
822
|
This page was automatically generated by the
LXR engine.
|