4 * Copyright (C) 2002, Linus Torvalds.
6 * Contains functions related to writing back dirty pages at the
9 * 10Apr2002 akpm@zip.com.au
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/spinlock.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/task_io_accounting_ops.h>
25 #include <linux/blkdev.h>
26 #include <linux/mpage.h>
27 #include <linux/rmap.h>
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>
34 #include <linux/buffer_head.h>
35 #include <linux/pagevec.h>
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.
44 #define MAX_WRITEBACK_PAGES 1024
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.
50 static long ratelimit_pages
= 32;
52 static int dirty_exceeded __cacheline_aligned_in_smp
; /* Dirty mem may be over limit */
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.
60 static inline long sync_writeback_pages(void)
62 return ratelimit_pages
+ ratelimit_pages
/ 2;
65 /* The following parameters are exported via /proc/sys/vm */
68 * Start background writeback (via pdflush) at this percentage
70 int dirty_background_ratio
= 5;
73 * The generator of dirty data starts writeback at this percentage
75 int vm_dirty_ratio
= 10;
78 * The interval between `kupdate'-style writebacks, in jiffies
80 int dirty_writeback_interval
= 5 * HZ
;
83 * The longest number of jiffies for which data is allowed to remain dirty
85 int dirty_expire_interval
= 30 * HZ
;
88 * Flag that makes the machine dump writes/reads and block dirtyings.
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.
98 EXPORT_SYMBOL(laptop_mode
);
100 /* End of sysctl-exported parameters */
103 static void background_writeout(unsigned long _min_pages
);
106 * Work out the current dirty-memory clamping and background writeout
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.
114 * We only allow 1/2 of the currently-unmapped memory to be dirtied.
116 * We don't permit the clamping level to fall below 5% - that is getting rather
119 * We make sure that the background writeout level is below the adjusted
123 static unsigned long highmem_dirtyable_memory(unsigned long total
)
125 #ifdef CONFIG_HIGHMEM
129 for_each_online_node(node
) {
131 &NODE_DATA(node
)->node_zones
[ZONE_HIGHMEM
];
133 x
+= zone_page_state(z
, NR_FREE_PAGES
)
134 + zone_page_state(z
, NR_INACTIVE
)
135 + zone_page_state(z
, NR_ACTIVE
);
138 * Make sure that the number of highmem pages is never larger
139 * than the number of the total dirtyable memory. This can only
140 * occur in very strange VM situations but we want to make sure
141 * that this does not occur.
143 return min(x
, total
);
149 static unsigned long determine_dirtyable_memory(void)
153 x
= global_page_state(NR_FREE_PAGES
)
154 + global_page_state(NR_INACTIVE
)
155 + global_page_state(NR_ACTIVE
);
156 x
-= highmem_dirtyable_memory(x
);
157 return x
+ 1; /* Ensure that we never return 0 */
161 get_dirty_limits(long *pbackground
, long *pdirty
,
162 struct address_space
*mapping
)
164 int background_ratio
; /* Percentages */
169 unsigned long available_memory
= determine_dirtyable_memory();
170 struct task_struct
*tsk
;
172 unmapped_ratio
= 100 - ((global_page_state(NR_FILE_MAPPED
) +
173 global_page_state(NR_ANON_PAGES
)) * 100) /
176 dirty_ratio
= vm_dirty_ratio
;
177 if (dirty_ratio
> unmapped_ratio
/ 2)
178 dirty_ratio
= unmapped_ratio
/ 2;
183 background_ratio
= dirty_background_ratio
;
184 if (background_ratio
>= dirty_ratio
)
185 background_ratio
= dirty_ratio
/ 2;
187 background
= (background_ratio
* available_memory
) / 100;
188 dirty
= (dirty_ratio
* available_memory
) / 100;
190 if (tsk
->flags
& PF_LESS_THROTTLE
|| rt_task(tsk
)) {
191 background
+= background
/ 4;
194 *pbackground
= background
;
199 * balance_dirty_pages() must be called by processes which are generating dirty
200 * data. It looks at the number of dirty pages in the machine and will force
201 * the caller to perform writeback if the system is over `vm_dirty_ratio'.
202 * If we're over `background_thresh' then pdflush is woken to perform some
205 static void balance_dirty_pages(struct address_space
*mapping
)
208 long background_thresh
;
210 unsigned long pages_written
= 0;
211 unsigned long write_chunk
= sync_writeback_pages();
213 struct backing_dev_info
*bdi
= mapping
->backing_dev_info
;
216 struct writeback_control wbc
= {
218 .sync_mode
= WB_SYNC_NONE
,
219 .older_than_this
= NULL
,
220 .nr_to_write
= write_chunk
,
224 get_dirty_limits(&background_thresh
, &dirty_thresh
, mapping
);
225 nr_reclaimable
= global_page_state(NR_FILE_DIRTY
) +
226 global_page_state(NR_UNSTABLE_NFS
);
227 if (nr_reclaimable
+ global_page_state(NR_WRITEBACK
) <=
234 /* Note: nr_reclaimable denotes nr_dirty + nr_unstable.
235 * Unstable writes are a feature of certain networked
236 * filesystems (i.e. NFS) in which data may have been
237 * written to the server's write cache, but has not yet
238 * been flushed to permanent storage.
240 if (nr_reclaimable
) {
241 writeback_inodes(&wbc
);
242 get_dirty_limits(&background_thresh
,
243 &dirty_thresh
, mapping
);
244 nr_reclaimable
= global_page_state(NR_FILE_DIRTY
) +
245 global_page_state(NR_UNSTABLE_NFS
);
247 global_page_state(NR_WRITEBACK
)
250 pages_written
+= write_chunk
- wbc
.nr_to_write
;
251 if (pages_written
>= write_chunk
)
252 break; /* We've done our duty */
254 congestion_wait(WRITE
, HZ
/10);
257 if (nr_reclaimable
+ global_page_state(NR_WRITEBACK
)
258 <= dirty_thresh
&& dirty_exceeded
)
261 if (writeback_in_progress(bdi
))
262 return; /* pdflush is already working this queue */
265 * In laptop mode, we wait until hitting the higher threshold before
266 * starting background writeout, and then write out all the way down
267 * to the lower threshold. So slow writers cause minimal disk activity.
269 * In normal mode, we start background writeout at the lower
270 * background_thresh, to keep the amount of dirty memory low.
272 if ((laptop_mode
&& pages_written
) ||
273 (!laptop_mode
&& (nr_reclaimable
> background_thresh
)))
274 pdflush_operation(background_writeout
, 0);
277 void set_page_dirty_balance(struct page
*page
)
279 if (set_page_dirty(page
)) {
280 struct address_space
*mapping
= page_mapping(page
);
283 balance_dirty_pages_ratelimited(mapping
);
288 * balance_dirty_pages_ratelimited_nr - balance dirty memory state
289 * @mapping: address_space which was dirtied
290 * @nr_pages_dirtied: number of pages which the caller has just dirtied
292 * Processes which are dirtying memory should call in here once for each page
293 * which was newly dirtied. The function will periodically check the system's
294 * dirty state and will initiate writeback if needed.
296 * On really big machines, get_writeback_state is expensive, so try to avoid
297 * calling it too often (ratelimiting). But once we're over the dirty memory
298 * limit we decrease the ratelimiting by a lot, to prevent individual processes
299 * from overshooting the limit by (ratelimit_pages) each.
301 void balance_dirty_pages_ratelimited_nr(struct address_space
*mapping
,
302 unsigned long nr_pages_dirtied
)
304 static DEFINE_PER_CPU(unsigned long, ratelimits
) = 0;
305 unsigned long ratelimit
;
308 ratelimit
= ratelimit_pages
;
313 * Check the rate limiting. Also, we do not want to throttle real-time
314 * tasks in balance_dirty_pages(). Period.
317 p
= &__get_cpu_var(ratelimits
);
318 *p
+= nr_pages_dirtied
;
319 if (unlikely(*p
>= ratelimit
)) {
322 balance_dirty_pages(mapping
);
327 EXPORT_SYMBOL(balance_dirty_pages_ratelimited_nr
);
329 void throttle_vm_writeout(gfp_t gfp_mask
)
331 long background_thresh
;
334 if ((gfp_mask
& (__GFP_FS
|__GFP_IO
)) != (__GFP_FS
|__GFP_IO
)) {
336 * The caller might hold locks which can prevent IO completion
337 * or progress in the filesystem. So we cannot just sit here
338 * waiting for IO to complete.
340 congestion_wait(WRITE
, HZ
/10);
345 get_dirty_limits(&background_thresh
, &dirty_thresh
, NULL
);
348 * Boost the allowable dirty threshold a bit for page
349 * allocators so they don't get DoS'ed by heavy writers
351 dirty_thresh
+= dirty_thresh
/ 10; /* wheeee... */
353 if (global_page_state(NR_UNSTABLE_NFS
) +
354 global_page_state(NR_WRITEBACK
) <= dirty_thresh
)
356 congestion_wait(WRITE
, HZ
/10);
361 * writeback at least _min_pages, and keep writing until the amount of dirty
362 * memory is less than the background threshold, or until we're all clean.
364 static void background_writeout(unsigned long _min_pages
)
366 long min_pages
= _min_pages
;
367 struct writeback_control wbc
= {
369 .sync_mode
= WB_SYNC_NONE
,
370 .older_than_this
= NULL
,
377 long background_thresh
;
380 get_dirty_limits(&background_thresh
, &dirty_thresh
, NULL
);
381 if (global_page_state(NR_FILE_DIRTY
) +
382 global_page_state(NR_UNSTABLE_NFS
) < background_thresh
385 wbc
.encountered_congestion
= 0;
386 wbc
.nr_to_write
= MAX_WRITEBACK_PAGES
;
387 wbc
.pages_skipped
= 0;
388 writeback_inodes(&wbc
);
389 min_pages
-= MAX_WRITEBACK_PAGES
- wbc
.nr_to_write
;
390 if (wbc
.nr_to_write
> 0 || wbc
.pages_skipped
> 0) {
391 /* Wrote less than expected */
392 congestion_wait(WRITE
, HZ
/10);
393 if (!wbc
.encountered_congestion
)
400 * Start writeback of `nr_pages' pages. If `nr_pages' is zero, write back
401 * the whole world. Returns 0 if a pdflush thread was dispatched. Returns
402 * -1 if all pdflush threads were busy.
404 int wakeup_pdflush(long nr_pages
)
407 nr_pages
= global_page_state(NR_FILE_DIRTY
) +
408 global_page_state(NR_UNSTABLE_NFS
);
409 return pdflush_operation(background_writeout
, nr_pages
);
412 static void wb_timer_fn(unsigned long unused
);
413 static void laptop_timer_fn(unsigned long unused
);
415 static DEFINE_TIMER(wb_timer
, wb_timer_fn
, 0, 0);
416 static DEFINE_TIMER(laptop_mode_wb_timer
, laptop_timer_fn
, 0, 0);
419 * Periodic writeback of "old" data.
421 * Define "old": the first time one of an inode's pages is dirtied, we mark the
422 * dirtying-time in the inode's address_space. So this periodic writeback code
423 * just walks the superblock inode list, writing back any inodes which are
424 * older than a specific point in time.
426 * Try to run once per dirty_writeback_interval. But if a writeback event
427 * takes longer than a dirty_writeback_interval interval, then leave a
430 * older_than_this takes precedence over nr_to_write. So we'll only write back
431 * all dirty pages if they are all attached to "old" mappings.
433 static void wb_kupdate(unsigned long arg
)
435 unsigned long oldest_jif
;
436 unsigned long start_jif
;
437 unsigned long next_jif
;
439 struct writeback_control wbc
= {
441 .sync_mode
= WB_SYNC_NONE
,
442 .older_than_this
= &oldest_jif
,
451 oldest_jif
= jiffies
- dirty_expire_interval
;
453 next_jif
= start_jif
+ dirty_writeback_interval
;
454 nr_to_write
= global_page_state(NR_FILE_DIRTY
) +
455 global_page_state(NR_UNSTABLE_NFS
) +
456 (inodes_stat
.nr_inodes
- inodes_stat
.nr_unused
);
457 while (nr_to_write
> 0) {
458 wbc
.encountered_congestion
= 0;
459 wbc
.nr_to_write
= MAX_WRITEBACK_PAGES
;
460 writeback_inodes(&wbc
);
461 if (wbc
.nr_to_write
> 0) {
462 if (wbc
.encountered_congestion
)
463 congestion_wait(WRITE
, HZ
/10);
465 break; /* All the old data is written */
467 nr_to_write
-= MAX_WRITEBACK_PAGES
- wbc
.nr_to_write
;
469 if (time_before(next_jif
, jiffies
+ HZ
))
470 next_jif
= jiffies
+ HZ
;
471 if (dirty_writeback_interval
)
472 mod_timer(&wb_timer
, next_jif
);
476 * sysctl handler for /proc/sys/vm/dirty_writeback_centisecs
478 int dirty_writeback_centisecs_handler(ctl_table
*table
, int write
,
479 struct file
*file
, void __user
*buffer
, size_t *length
, loff_t
*ppos
)
481 proc_dointvec_userhz_jiffies(table
, write
, file
, buffer
, length
, ppos
);
482 if (dirty_writeback_interval
) {
484 jiffies
+ dirty_writeback_interval
);
486 del_timer(&wb_timer
);
491 static void wb_timer_fn(unsigned long unused
)
493 if (pdflush_operation(wb_kupdate
, 0) < 0)
494 mod_timer(&wb_timer
, jiffies
+ HZ
); /* delay 1 second */
497 static void laptop_flush(unsigned long unused
)
502 static void laptop_timer_fn(unsigned long unused
)
504 pdflush_operation(laptop_flush
, 0);
508 * We've spun up the disk and we're in laptop mode: schedule writeback
509 * of all dirty data a few seconds from now. If the flush is already scheduled
510 * then push it back - the user is still using the disk.
512 void laptop_io_completion(void)
514 mod_timer(&laptop_mode_wb_timer
, jiffies
+ laptop_mode
);
518 * We're in laptop mode and we've just synced. The sync's writes will have
519 * caused another writeback to be scheduled by laptop_io_completion.
520 * Nothing needs to be written back anymore, so we unschedule the writeback.
522 void laptop_sync_completion(void)
524 del_timer(&laptop_mode_wb_timer
);
528 * If ratelimit_pages is too high then we can get into dirty-data overload
529 * if a large number of processes all perform writes at the same time.
530 * If it is too low then SMP machines will call the (expensive)
531 * get_writeback_state too often.
533 * Here we set ratelimit_pages to a level which ensures that when all CPUs are
534 * dirtying in parallel, we cannot go more than 3% (1/32) over the dirty memory
535 * thresholds before writeback cuts in.
537 * But the limit should not be set too high. Because it also controls the
538 * amount of memory which the balance_dirty_pages() caller has to write back.
539 * If this is too large then the caller will block on the IO queue all the
540 * time. So limit it to four megabytes - the balance_dirty_pages() caller
541 * will write six megabyte chunks, max.
544 void writeback_set_ratelimit(void)
546 ratelimit_pages
= vm_total_pages
/ (num_online_cpus() * 32);
547 if (ratelimit_pages
< 16)
548 ratelimit_pages
= 16;
549 if (ratelimit_pages
* PAGE_CACHE_SIZE
> 4096 * 1024)
550 ratelimit_pages
= (4096 * 1024) / PAGE_CACHE_SIZE
;
554 ratelimit_handler(struct notifier_block
*self
, unsigned long u
, void *v
)
556 writeback_set_ratelimit();
560 static struct notifier_block __cpuinitdata ratelimit_nb
= {
561 .notifier_call
= ratelimit_handler
,
566 * Called early on to tune the page writeback dirty limits.
568 * We used to scale dirty pages according to how total memory
569 * related to pages that could be allocated for buffers (by
570 * comparing nr_free_buffer_pages() to vm_total_pages.
572 * However, that was when we used "dirty_ratio" to scale with
573 * all memory, and we don't do that any more. "dirty_ratio"
574 * is now applied to total non-HIGHPAGE memory (by subtracting
575 * totalhigh_pages from vm_total_pages), and as such we can't
576 * get into the old insane situation any more where we had
577 * large amounts of dirty pages compared to a small amount of
578 * non-HIGHMEM memory.
580 * But we might still want to scale the dirty_ratio by how
581 * much memory the box has..
583 void __init
page_writeback_init(void)
585 mod_timer(&wb_timer
, jiffies
+ dirty_writeback_interval
);
586 writeback_set_ratelimit();
587 register_cpu_notifier(&ratelimit_nb
);
591 * write_cache_pages - walk the list of dirty pages of the given address space and write all of them.
592 * @mapping: address space structure to write
593 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
594 * @writepage: function called for each page
595 * @data: data passed to writepage function
597 * If a page is already under I/O, write_cache_pages() skips it, even
598 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
599 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
600 * and msync() need to guarantee that all the data which was dirty at the time
601 * the call was made get new I/O started against them. If wbc->sync_mode is
602 * WB_SYNC_ALL then we were called for data integrity and we must wait for
603 * existing IO to complete.
605 int write_cache_pages(struct address_space
*mapping
,
606 struct writeback_control
*wbc
, writepage_t writepage
,
609 struct backing_dev_info
*bdi
= mapping
->backing_dev_info
;
614 pgoff_t
uninitialized_var(writeback_index
);
616 pgoff_t end
; /* Inclusive */
620 if (wbc
->nonblocking
&& bdi_write_congested(bdi
)) {
621 wbc
->encountered_congestion
= 1;
625 pagevec_init(&pvec
, 0);
626 if (wbc
->range_cyclic
) {
627 writeback_index
= mapping
->writeback_index
; /* prev offset */
628 index
= writeback_index
;
635 index
= wbc
->range_start
>> PAGE_CACHE_SHIFT
;
636 end
= wbc
->range_end
>> PAGE_CACHE_SHIFT
;
637 if (wbc
->range_start
== 0 && wbc
->range_end
== LLONG_MAX
)
639 cycled
= 1; /* ignore range_cyclic tests */
642 while (!done
&& (index
<= end
) &&
643 (nr_pages
= pagevec_lookup_tag(&pvec
, mapping
, &index
,
645 min(end
- index
, (pgoff_t
)PAGEVEC_SIZE
-1) + 1))) {
648 for (i
= 0; i
< nr_pages
; i
++) {
649 struct page
*page
= pvec
.pages
[i
];
652 * At this point we hold neither mapping->tree_lock nor
653 * lock on the page itself: the page may be truncated or
654 * invalidated (changing page->mapping to NULL), or even
655 * swizzled back from swapper_space to tmpfs file
660 if (unlikely(page
->mapping
!= mapping
)) {
665 if (page
->index
> end
) {
667 * can't be range_cyclic (1st pass) because
668 * end == -1 in that case.
675 if (wbc
->sync_mode
!= WB_SYNC_NONE
)
676 wait_on_page_writeback(page
);
678 if (PageWriteback(page
) ||
679 !clear_page_dirty_for_io(page
)) {
684 ret
= (*writepage
)(page
, wbc
, data
);
686 if (unlikely(ret
== AOP_WRITEPAGE_ACTIVATE
)) {
690 if (ret
|| (--(wbc
->nr_to_write
) <= 0))
692 if (wbc
->nonblocking
&& bdi_write_congested(bdi
)) {
693 wbc
->encountered_congestion
= 1;
697 pagevec_release(&pvec
);
700 if (!cycled
&& !done
) {
703 * We hit the last page and there is more work to be done: wrap
704 * back to the start of the file
708 end
= writeback_index
- 1;
711 if (wbc
->range_cyclic
|| (range_whole
&& wbc
->nr_to_write
> 0))
712 mapping
->writeback_index
= index
;
715 EXPORT_SYMBOL(write_cache_pages
);
718 * Function used by generic_writepages to call the real writepage
719 * function and set the mapping flags on error
721 static int __writepage(struct page
*page
, struct writeback_control
*wbc
,
724 struct address_space
*mapping
= data
;
725 int ret
= mapping
->a_ops
->writepage(page
, wbc
);
726 mapping_set_error(mapping
, ret
);
731 * generic_writepages - walk the list of dirty pages of the given address space and writepage() all of them.
732 * @mapping: address space structure to write
733 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
735 * This is a library function, which implements the writepages()
736 * address_space_operation.
738 int generic_writepages(struct address_space
*mapping
,
739 struct writeback_control
*wbc
)
741 /* deal with chardevs and other special file */
742 if (!mapping
->a_ops
->writepage
)
745 return write_cache_pages(mapping
, wbc
, __writepage
, mapping
);
748 EXPORT_SYMBOL(generic_writepages
);
750 int do_writepages(struct address_space
*mapping
, struct writeback_control
*wbc
)
754 if (wbc
->nr_to_write
<= 0)
756 wbc
->for_writepages
= 1;
757 if (mapping
->a_ops
->writepages
)
758 ret
= mapping
->a_ops
->writepages(mapping
, wbc
);
760 ret
= generic_writepages(mapping
, wbc
);
761 wbc
->for_writepages
= 0;
766 * write_one_page - write out a single page and optionally wait on I/O
767 * @page: the page to write
768 * @wait: if true, wait on writeout
770 * The page must be locked by the caller and will be unlocked upon return.
772 * write_one_page() returns a negative error code if I/O failed.
774 int write_one_page(struct page
*page
, int wait
)
776 struct address_space
*mapping
= page
->mapping
;
778 struct writeback_control wbc
= {
779 .sync_mode
= WB_SYNC_ALL
,
783 BUG_ON(!PageLocked(page
));
786 wait_on_page_writeback(page
);
788 if (clear_page_dirty_for_io(page
)) {
789 page_cache_get(page
);
790 ret
= mapping
->a_ops
->writepage(page
, &wbc
);
791 if (ret
== 0 && wait
) {
792 wait_on_page_writeback(page
);
796 page_cache_release(page
);
802 EXPORT_SYMBOL(write_one_page
);
805 * For address_spaces which do not use buffers nor write back.
807 int __set_page_dirty_no_writeback(struct page
*page
)
809 if (!PageDirty(page
))
815 * For address_spaces which do not use buffers. Just tag the page as dirty in
818 * This is also used when a single buffer is being dirtied: we want to set the
819 * page dirty in that case, but not all the buffers. This is a "bottom-up"
820 * dirtying, whereas __set_page_dirty_buffers() is a "top-down" dirtying.
822 * Most callers have locked the page, which pins the address_space in memory.
823 * But zap_pte_range() does not lock the page, however in that case the
824 * mapping is pinned by the vma's ->vm_file reference.
826 * We take care to handle the case where the page was truncated from the
827 * mapping by re-checking page_mapping() insode tree_lock.
829 int __set_page_dirty_nobuffers(struct page
*page
)
831 if (!TestSetPageDirty(page
)) {
832 struct address_space
*mapping
= page_mapping(page
);
833 struct address_space
*mapping2
;
838 write_lock_irq(&mapping
->tree_lock
);
839 mapping2
= page_mapping(page
);
840 if (mapping2
) { /* Race with truncate? */
841 BUG_ON(mapping2
!= mapping
);
842 if (mapping_cap_account_dirty(mapping
)) {
843 __inc_zone_page_state(page
, NR_FILE_DIRTY
);
844 task_io_account_write(PAGE_CACHE_SIZE
);
846 radix_tree_tag_set(&mapping
->page_tree
,
847 page_index(page
), PAGECACHE_TAG_DIRTY
);
849 write_unlock_irq(&mapping
->tree_lock
);
851 /* !PageAnon && !swapper_space */
852 __mark_inode_dirty(mapping
->host
, I_DIRTY_PAGES
);
858 EXPORT_SYMBOL(__set_page_dirty_nobuffers
);
861 * When a writepage implementation decides that it doesn't want to write this
862 * page for some reason, it should redirty the locked page via
863 * redirty_page_for_writepage() and it should then unlock the page and return 0
865 int redirty_page_for_writepage(struct writeback_control
*wbc
, struct page
*page
)
867 wbc
->pages_skipped
++;
868 return __set_page_dirty_nobuffers(page
);
870 EXPORT_SYMBOL(redirty_page_for_writepage
);
873 * If the mapping doesn't provide a set_page_dirty a_op, then
874 * just fall through and assume that it wants buffer_heads.
876 int fastcall
set_page_dirty(struct page
*page
)
878 struct address_space
*mapping
= page_mapping(page
);
880 if (likely(mapping
)) {
881 int (*spd
)(struct page
*) = mapping
->a_ops
->set_page_dirty
;
884 spd
= __set_page_dirty_buffers
;
888 if (!PageDirty(page
)) {
889 if (!TestSetPageDirty(page
))
894 EXPORT_SYMBOL(set_page_dirty
);
897 * set_page_dirty() is racy if the caller has no reference against
898 * page->mapping->host, and if the page is unlocked. This is because another
899 * CPU could truncate the page off the mapping and then free the mapping.
901 * Usually, the page _is_ locked, or the caller is a user-space process which
902 * holds a reference on the inode by having an open file.
904 * In other cases, the page should be locked before running set_page_dirty().
906 int set_page_dirty_lock(struct page
*page
)
910 lock_page_nosync(page
);
911 ret
= set_page_dirty(page
);
915 EXPORT_SYMBOL(set_page_dirty_lock
);
918 * Clear a page's dirty flag, while caring for dirty memory accounting.
919 * Returns true if the page was previously dirty.
921 * This is for preparing to put the page under writeout. We leave the page
922 * tagged as dirty in the radix tree so that a concurrent write-for-sync
923 * can discover it via a PAGECACHE_TAG_DIRTY walk. The ->writepage
924 * implementation will run either set_page_writeback() or set_page_dirty(),
925 * at which stage we bring the page's dirty flag and radix-tree dirty tag
928 * This incoherency between the page's dirty flag and radix-tree tag is
929 * unfortunate, but it only exists while the page is locked.
931 int clear_page_dirty_for_io(struct page
*page
)
933 struct address_space
*mapping
= page_mapping(page
);
935 BUG_ON(!PageLocked(page
));
937 if (mapping
&& mapping_cap_account_dirty(mapping
)) {
939 * Yes, Virginia, this is indeed insane.
941 * We use this sequence to make sure that
942 * (a) we account for dirty stats properly
943 * (b) we tell the low-level filesystem to
944 * mark the whole page dirty if it was
945 * dirty in a pagetable. Only to then
946 * (c) clean the page again and return 1 to
947 * cause the writeback.
949 * This way we avoid all nasty races with the
950 * dirty bit in multiple places and clearing
951 * them concurrently from different threads.
953 * Note! Normally the "set_page_dirty(page)"
954 * has no effect on the actual dirty bit - since
955 * that will already usually be set. But we
956 * need the side effects, and it can help us
959 * We basically use the page "master dirty bit"
960 * as a serialization point for all the different
961 * threads doing their things.
963 if (page_mkclean(page
))
964 set_page_dirty(page
);
966 * We carefully synchronise fault handlers against
967 * installing a dirty pte and marking the page dirty
968 * at this point. We do this by having them hold the
969 * page lock at some point after installing their
970 * pte, but before marking the page dirty.
971 * Pages are always locked coming in here, so we get
972 * the desired exclusion. See mm/memory.c:do_wp_page()
975 if (TestClearPageDirty(page
)) {
976 dec_zone_page_state(page
, NR_FILE_DIRTY
);
981 return TestClearPageDirty(page
);
983 EXPORT_SYMBOL(clear_page_dirty_for_io
);
985 int test_clear_page_writeback(struct page
*page
)
987 struct address_space
*mapping
= page_mapping(page
);
993 write_lock_irqsave(&mapping
->tree_lock
, flags
);
994 ret
= TestClearPageWriteback(page
);
996 radix_tree_tag_clear(&mapping
->page_tree
,
998 PAGECACHE_TAG_WRITEBACK
);
999 write_unlock_irqrestore(&mapping
->tree_lock
, flags
);
1001 ret
= TestClearPageWriteback(page
);
1006 int test_set_page_writeback(struct page
*page
)
1008 struct address_space
*mapping
= page_mapping(page
);
1012 unsigned long flags
;
1014 write_lock_irqsave(&mapping
->tree_lock
, flags
);
1015 ret
= TestSetPageWriteback(page
);
1017 radix_tree_tag_set(&mapping
->page_tree
,
1019 PAGECACHE_TAG_WRITEBACK
);
1020 if (!PageDirty(page
))
1021 radix_tree_tag_clear(&mapping
->page_tree
,
1023 PAGECACHE_TAG_DIRTY
);
1024 write_unlock_irqrestore(&mapping
->tree_lock
, flags
);
1026 ret
= TestSetPageWriteback(page
);
1031 EXPORT_SYMBOL(test_set_page_writeback
);
1034 * Return true if any of the pages in the mapping are marked with the
1037 int mapping_tagged(struct address_space
*mapping
, int tag
)
1041 ret
= radix_tree_tagged(&mapping
->page_tree
, tag
);
1045 EXPORT_SYMBOL(mapping_tagged
);