4 * Copyright (C) 2002, Linus Torvalds.
6 * Contains all the functions related to writing back and waiting
7 * upon dirty inodes against superblocks, and writing back dirty
8 * pages against inodes. ie: data writeback. Writeout of the
9 * inode itself is not handled here.
11 * 10Apr2002 akpm@zip.com.au
12 * Split out of fs/inode.c
13 * Additions for address_space-based writeback
16 #include <linux/kernel.h>
17 #include <linux/spinlock.h>
18 #include <linux/sched.h>
21 #include <linux/writeback.h>
22 #include <linux/blkdev.h>
23 #include <linux/backing-dev.h>
24 #include <linux/buffer_head.h>
28 * __mark_inode_dirty - internal function
29 * @inode: inode to mark
30 * @flags: what kind of dirty (i.e. I_DIRTY_SYNC)
31 * Mark an inode as dirty. Callers should use mark_inode_dirty or
32 * mark_inode_dirty_sync.
34 * Put the inode on the super block's dirty list.
36 * CAREFUL! We mark it dirty unconditionally, but move it onto the
37 * dirty list only if it is hashed or if it refers to a blockdev.
38 * If it was not hashed, it will never be added to the dirty list
39 * even if it is later hashed, as it will have been marked dirty already.
41 * In short, make sure you hash any inodes _before_ you start marking
44 * This function *must* be atomic for the I_DIRTY_PAGES case -
45 * set_page_dirty() is called under spinlock in several places.
47 * Note that for blockdevs, inode->dirtied_when represents the dirtying time of
48 * the block-special inode (/dev/hda1) itself. And the ->dirtied_when field of
49 * the kernel-internal blockdev inode represents the dirtying time of the
50 * blockdev's pages. This is why for I_DIRTY_PAGES we always use
51 * page->mapping->host, so the page-dirtying time is recorded in the internal
54 void __mark_inode_dirty(struct inode
*inode
, int flags
)
56 struct super_block
*sb
= inode
->i_sb
;
59 * Don't do this for I_DIRTY_PAGES - that doesn't actually
60 * dirty the inode itself
62 if (flags
& (I_DIRTY_SYNC
| I_DIRTY_DATASYNC
)) {
63 if (sb
->s_op
->dirty_inode
)
64 sb
->s_op
->dirty_inode(inode
);
68 * make sure that changes are seen by all cpus before we test i_state
73 /* avoid the locking if we can */
74 if ((inode
->i_state
& flags
) == flags
)
77 if (unlikely(block_dump
)) {
78 struct dentry
*dentry
= NULL
;
79 const char *name
= "?";
81 if (!list_empty(&inode
->i_dentry
)) {
82 dentry
= list_entry(inode
->i_dentry
.next
,
83 struct dentry
, d_alias
);
84 if (dentry
&& dentry
->d_name
.name
)
85 name
= (const char *) dentry
->d_name
.name
;
88 if (inode
->i_ino
|| strcmp(inode
->i_sb
->s_id
, "bdev"))
90 "%s(%d): dirtied inode %lu (%s) on %s\n",
91 current
->comm
, current
->pid
, inode
->i_ino
,
92 name
, inode
->i_sb
->s_id
);
95 spin_lock(&inode_lock
);
96 if ((inode
->i_state
& flags
) != flags
) {
97 const int was_dirty
= inode
->i_state
& I_DIRTY
;
99 inode
->i_state
|= flags
;
102 * If the inode is locked, just update its dirty state.
103 * The unlocker will place the inode on the appropriate
104 * superblock list, based upon its state.
106 if (inode
->i_state
& I_LOCK
)
110 * Only add valid (hashed) inodes to the superblock's
111 * dirty list. Add blockdev inodes as well.
113 if (!S_ISBLK(inode
->i_mode
)) {
114 if (hlist_unhashed(&inode
->i_hash
))
117 if (inode
->i_state
& (I_FREEING
|I_CLEAR
))
121 * If the inode was already on s_dirty/s_io/s_more_io, don't
122 * reposition it (that would break s_dirty time-ordering).
125 inode
->dirtied_when
= jiffies
;
126 list_move(&inode
->i_list
, &sb
->s_dirty
);
130 spin_unlock(&inode_lock
);
133 EXPORT_SYMBOL(__mark_inode_dirty
);
135 static int write_inode(struct inode
*inode
, int sync
)
137 if (inode
->i_sb
->s_op
->write_inode
&& !is_bad_inode(inode
))
138 return inode
->i_sb
->s_op
->write_inode(inode
, sync
);
143 * Redirty an inode: set its when-it-was dirtied timestamp and move it to the
144 * furthest end of its superblock's dirty-inode list.
146 * Before stamping the inode's ->dirtied_when, we check to see whether it is
147 * already the most-recently-dirtied inode on the s_dirty list. If that is
148 * the case then the inode must have been redirtied while it was being written
149 * out and we don't reset its dirtied_when.
151 static void redirty_tail(struct inode
*inode
)
153 struct super_block
*sb
= inode
->i_sb
;
155 if (!list_empty(&sb
->s_dirty
)) {
156 struct inode
*tail_inode
;
158 tail_inode
= list_entry(sb
->s_dirty
.next
, struct inode
, i_list
);
159 if (!time_after_eq(inode
->dirtied_when
,
160 tail_inode
->dirtied_when
))
161 inode
->dirtied_when
= jiffies
;
163 list_move(&inode
->i_list
, &sb
->s_dirty
);
167 * requeue inode for re-scanning after sb->s_io list is exhausted.
169 static void requeue_io(struct inode
*inode
)
171 list_move(&inode
->i_list
, &inode
->i_sb
->s_more_io
);
175 * Move expired dirty inodes from @delaying_queue to @dispatch_queue.
177 static void move_expired_inodes(struct list_head
*delaying_queue
,
178 struct list_head
*dispatch_queue
,
179 unsigned long *older_than_this
)
181 while (!list_empty(delaying_queue
)) {
182 struct inode
*inode
= list_entry(delaying_queue
->prev
,
183 struct inode
, i_list
);
184 if (older_than_this
&&
185 time_after(inode
->dirtied_when
, *older_than_this
))
187 list_move(&inode
->i_list
, dispatch_queue
);
192 * Queue all expired dirty inodes for io, eldest first.
194 static void queue_io(struct super_block
*sb
,
195 unsigned long *older_than_this
)
197 list_splice_init(&sb
->s_more_io
, sb
->s_io
.prev
);
198 move_expired_inodes(&sb
->s_dirty
, &sb
->s_io
, older_than_this
);
201 int sb_has_dirty_inodes(struct super_block
*sb
)
203 return !list_empty(&sb
->s_dirty
) ||
204 !list_empty(&sb
->s_io
) ||
205 !list_empty(&sb
->s_more_io
);
207 EXPORT_SYMBOL(sb_has_dirty_inodes
);
210 * Write a single inode's dirty pages and inode data out to disk.
211 * If `wait' is set, wait on the writeout.
213 * The whole writeout design is quite complex and fragile. We want to avoid
214 * starvation of particular inodes when others are being redirtied, prevent
217 * Called under inode_lock.
220 __sync_single_inode(struct inode
*inode
, struct writeback_control
*wbc
)
223 struct address_space
*mapping
= inode
->i_mapping
;
224 int wait
= wbc
->sync_mode
== WB_SYNC_ALL
;
227 BUG_ON(inode
->i_state
& I_LOCK
);
229 /* Set I_LOCK, reset I_DIRTY */
230 dirty
= inode
->i_state
& I_DIRTY
;
231 inode
->i_state
|= I_LOCK
;
232 inode
->i_state
&= ~I_DIRTY
;
234 spin_unlock(&inode_lock
);
236 ret
= do_writepages(mapping
, wbc
);
238 /* Don't write the inode if only I_DIRTY_PAGES was set */
239 if (dirty
& (I_DIRTY_SYNC
| I_DIRTY_DATASYNC
)) {
240 int err
= write_inode(inode
, wait
);
246 int err
= filemap_fdatawait(mapping
);
251 spin_lock(&inode_lock
);
252 inode
->i_state
&= ~I_LOCK
;
253 if (!(inode
->i_state
& I_FREEING
)) {
254 if (!(inode
->i_state
& I_DIRTY
) &&
255 mapping_tagged(mapping
, PAGECACHE_TAG_DIRTY
)) {
257 * We didn't write back all the pages. nfs_writepages()
258 * sometimes bales out without doing anything. Redirty
259 * the inode; Move it from s_io onto s_more_io/s_dirty.
262 * akpm: if the caller was the kupdate function we put
263 * this inode at the head of s_dirty so it gets first
264 * consideration. Otherwise, move it to the tail, for
265 * the reasons described there. I'm not really sure
266 * how much sense this makes. Presumably I had a good
267 * reasons for doing it this way, and I'd rather not
268 * muck with it at present.
270 if (wbc
->for_kupdate
) {
272 * For the kupdate function we move the inode
273 * to s_more_io so it will get more writeout as
274 * soon as the queue becomes uncongested.
276 inode
->i_state
|= I_DIRTY_PAGES
;
277 if (wbc
->nr_to_write
<= 0) {
279 * slice used up: queue for next turn
284 * somehow blocked: retry later
290 * Otherwise fully redirty the inode so that
291 * other inodes on this superblock will get some
292 * writeout. Otherwise heavy writing to one
293 * file would indefinitely suspend writeout of
294 * all the other files.
296 inode
->i_state
|= I_DIRTY_PAGES
;
299 } else if (inode
->i_state
& I_DIRTY
) {
301 * Someone redirtied the inode while were writing back
305 } else if (atomic_read(&inode
->i_count
)) {
307 * The inode is clean, inuse
309 list_move(&inode
->i_list
, &inode_in_use
);
312 * The inode is clean, unused
314 list_move(&inode
->i_list
, &inode_unused
);
317 wake_up_inode(inode
);
322 * Write out an inode's dirty pages. Called under inode_lock. Either the
323 * caller has ref on the inode (either via __iget or via syscall against an fd)
324 * or the inode has I_WILL_FREE set (via generic_forget_inode)
327 __writeback_single_inode(struct inode
*inode
, struct writeback_control
*wbc
)
329 wait_queue_head_t
*wqh
;
331 if (!atomic_read(&inode
->i_count
))
332 WARN_ON(!(inode
->i_state
& (I_WILL_FREE
|I_FREEING
)));
334 WARN_ON(inode
->i_state
& I_WILL_FREE
);
336 if ((wbc
->sync_mode
!= WB_SYNC_ALL
) && (inode
->i_state
& I_LOCK
)) {
337 struct address_space
*mapping
= inode
->i_mapping
;
341 * We're skipping this inode because it's locked, and we're not
342 * doing writeback-for-data-integrity. Move it to s_more_io so
343 * that writeback can proceed with the other inodes on s_io.
344 * We'll have another go at writing back this inode when we
345 * completed a full scan of s_io.
350 * Even if we don't actually write the inode itself here,
351 * we can at least start some of the data writeout..
353 spin_unlock(&inode_lock
);
354 ret
= do_writepages(mapping
, wbc
);
355 spin_lock(&inode_lock
);
360 * It's a data-integrity sync. We must wait.
362 if (inode
->i_state
& I_LOCK
) {
363 DEFINE_WAIT_BIT(wq
, &inode
->i_state
, __I_LOCK
);
365 wqh
= bit_waitqueue(&inode
->i_state
, __I_LOCK
);
367 spin_unlock(&inode_lock
);
368 __wait_on_bit(wqh
, &wq
, inode_wait
,
369 TASK_UNINTERRUPTIBLE
);
370 spin_lock(&inode_lock
);
371 } while (inode
->i_state
& I_LOCK
);
373 return __sync_single_inode(inode
, wbc
);
377 * Write out a superblock's list of dirty inodes. A wait will be performed
378 * upon no inodes, all inodes or the final one, depending upon sync_mode.
380 * If older_than_this is non-NULL, then only write out inodes which
381 * had their first dirtying at a time earlier than *older_than_this.
383 * If we're a pdlfush thread, then implement pdflush collision avoidance
384 * against the entire list.
386 * WB_SYNC_HOLD is a hack for sys_sync(): reattach the inode to sb->s_dirty so
387 * that it can be located for waiting on in __writeback_single_inode().
389 * Called under inode_lock.
391 * If `bdi' is non-zero then we're being asked to writeback a specific queue.
392 * This function assumes that the blockdev superblock's inodes are backed by
393 * a variety of queues, so all inodes are searched. For other superblocks,
394 * assume that all inodes are backed by the same queue.
396 * FIXME: this linear search could get expensive with many fileystems. But
397 * how to fix? We need to go from an address_space to all inodes which share
398 * a queue with that address_space. (Easy: have a global "dirty superblocks"
401 * The inodes to be written are parked on sb->s_io. They are moved back onto
402 * sb->s_dirty as they are selected for writing. This way, none can be missed
403 * on the writer throttling path, and we get decent balancing between many
404 * throttled threads: we don't want them all piling up on __wait_on_inode.
407 sync_sb_inodes(struct super_block
*sb
, struct writeback_control
*wbc
)
409 const unsigned long start
= jiffies
; /* livelock avoidance */
411 if (!wbc
->for_kupdate
|| list_empty(&sb
->s_io
))
412 queue_io(sb
, wbc
->older_than_this
);
414 while (!list_empty(&sb
->s_io
)) {
415 struct inode
*inode
= list_entry(sb
->s_io
.prev
,
416 struct inode
, i_list
);
417 struct address_space
*mapping
= inode
->i_mapping
;
418 struct backing_dev_info
*bdi
= mapping
->backing_dev_info
;
421 if (!bdi_cap_writeback_dirty(bdi
)) {
423 if (sb_is_blkdev_sb(sb
)) {
425 * Dirty memory-backed blockdev: the ramdisk
426 * driver does this. Skip just this inode
431 * Dirty memory-backed inode against a filesystem other
432 * than the kernel-internal bdev filesystem. Skip the
438 if (wbc
->nonblocking
&& bdi_write_congested(bdi
)) {
439 wbc
->encountered_congestion
= 1;
440 if (!sb_is_blkdev_sb(sb
))
441 break; /* Skip a congested fs */
443 continue; /* Skip a congested blockdev */
446 if (wbc
->bdi
&& bdi
!= wbc
->bdi
) {
447 if (!sb_is_blkdev_sb(sb
))
448 break; /* fs has the wrong queue */
450 continue; /* blockdev has wrong queue */
453 /* Was this inode dirtied after sync_sb_inodes was called? */
454 if (time_after(inode
->dirtied_when
, start
))
457 /* Is another pdflush already flushing this queue? */
458 if (current_is_pdflush() && !writeback_acquire(bdi
))
461 BUG_ON(inode
->i_state
& I_FREEING
);
463 pages_skipped
= wbc
->pages_skipped
;
464 __writeback_single_inode(inode
, wbc
);
465 if (wbc
->sync_mode
== WB_SYNC_HOLD
) {
466 inode
->dirtied_when
= jiffies
;
467 list_move(&inode
->i_list
, &sb
->s_dirty
);
469 if (current_is_pdflush())
470 writeback_release(bdi
);
471 if (wbc
->pages_skipped
!= pages_skipped
) {
473 * writeback is not making progress due to locked
474 * buffers. Skip this inode for now.
478 spin_unlock(&inode_lock
);
481 spin_lock(&inode_lock
);
482 if (wbc
->nr_to_write
<= 0) {
486 if (!list_empty(&sb
->s_more_io
))
489 return; /* Leave any unwritten inodes on s_io */
493 * Start writeback of dirty pagecache data against all unlocked inodes.
496 * We don't need to grab a reference to superblock here. If it has non-empty
497 * ->s_dirty it's hadn't been killed yet and kill_super() won't proceed
498 * past sync_inodes_sb() until the ->s_dirty/s_io/s_more_io lists are all
499 * empty. Since __sync_single_inode() regains inode_lock before it finally moves
500 * inode from superblock lists we are OK.
502 * If `older_than_this' is non-zero then only flush inodes which have a
503 * flushtime older than *older_than_this.
505 * If `bdi' is non-zero then we will scan the first inode against each
506 * superblock until we find the matching ones. One group will be the dirty
507 * inodes against a filesystem. Then when we hit the dummy blockdev superblock,
508 * sync_sb_inodes will seekout the blockdev which matches `bdi'. Maybe not
509 * super-efficient but we're about to do a ton of I/O...
512 writeback_inodes(struct writeback_control
*wbc
)
514 struct super_block
*sb
;
519 sb
= sb_entry(super_blocks
.prev
);
520 for (; sb
!= sb_entry(&super_blocks
); sb
= sb_entry(sb
->s_list
.prev
)) {
521 if (sb_has_dirty_inodes(sb
)) {
522 /* we're making our own get_super here */
524 spin_unlock(&sb_lock
);
526 * If we can't get the readlock, there's no sense in
527 * waiting around, most of the time the FS is going to
528 * be unmounted by the time it is released.
530 if (down_read_trylock(&sb
->s_umount
)) {
532 spin_lock(&inode_lock
);
533 sync_sb_inodes(sb
, wbc
);
534 spin_unlock(&inode_lock
);
536 up_read(&sb
->s_umount
);
539 if (__put_super_and_need_restart(sb
))
542 if (wbc
->nr_to_write
<= 0)
545 spin_unlock(&sb_lock
);
549 * writeback and wait upon the filesystem's dirty inodes. The caller will
550 * do this in two passes - one to write, and one to wait. WB_SYNC_HOLD is
551 * used to park the written inodes on sb->s_dirty for the wait pass.
553 * A finite limit is set on the number of pages which will be written.
554 * To prevent infinite livelock of sys_sync().
556 * We add in the number of potentially dirty inodes, because each inode write
557 * can dirty pagecache in the underlying blockdev.
559 void sync_inodes_sb(struct super_block
*sb
, int wait
)
561 struct writeback_control wbc
= {
562 .sync_mode
= wait
? WB_SYNC_ALL
: WB_SYNC_HOLD
,
564 .range_end
= LLONG_MAX
,
566 unsigned long nr_dirty
= global_page_state(NR_FILE_DIRTY
);
567 unsigned long nr_unstable
= global_page_state(NR_UNSTABLE_NFS
);
569 wbc
.nr_to_write
= nr_dirty
+ nr_unstable
+
570 (inodes_stat
.nr_inodes
- inodes_stat
.nr_unused
) +
571 nr_dirty
+ nr_unstable
;
572 wbc
.nr_to_write
+= wbc
.nr_to_write
/ 2; /* Bit more for luck */
573 spin_lock(&inode_lock
);
574 sync_sb_inodes(sb
, &wbc
);
575 spin_unlock(&inode_lock
);
579 * Rather lame livelock avoidance.
581 static void set_sb_syncing(int val
)
583 struct super_block
*sb
;
585 sb
= sb_entry(super_blocks
.prev
);
586 for (; sb
!= sb_entry(&super_blocks
); sb
= sb_entry(sb
->s_list
.prev
)) {
589 spin_unlock(&sb_lock
);
593 * sync_inodes - writes all inodes to disk
594 * @wait: wait for completion
596 * sync_inodes() goes through each super block's dirty inode list, writes the
597 * inodes out, waits on the writeout and puts the inodes back on the normal
600 * This is for sys_sync(). fsync_dev() uses the same algorithm. The subtle
601 * part of the sync functions is that the blockdev "superblock" is processed
602 * last. This is because the write_inode() function of a typical fs will
603 * perform no I/O, but will mark buffers in the blockdev mapping as dirty.
604 * What we want to do is to perform all that dirtying first, and then write
605 * back all those inode blocks via the blockdev mapping in one sweep. So the
606 * additional (somewhat redundant) sync_blockdev() calls here are to make
607 * sure that really happens. Because if we call sync_inodes_sb(wait=1) with
608 * outstanding dirty inodes, the writeback goes block-at-a-time within the
609 * filesystem's write_inode(). This is extremely slow.
611 static void __sync_inodes(int wait
)
613 struct super_block
*sb
;
617 list_for_each_entry(sb
, &super_blocks
, s_list
) {
622 spin_unlock(&sb_lock
);
623 down_read(&sb
->s_umount
);
625 sync_inodes_sb(sb
, wait
);
626 sync_blockdev(sb
->s_bdev
);
628 up_read(&sb
->s_umount
);
630 if (__put_super_and_need_restart(sb
))
633 spin_unlock(&sb_lock
);
636 void sync_inodes(int wait
)
648 * write_inode_now - write an inode to disk
649 * @inode: inode to write to disk
650 * @sync: whether the write should be synchronous or not
652 * This function commits an inode to disk immediately if it is dirty. This is
653 * primarily needed by knfsd.
655 * The caller must either have a ref on the inode or must have set I_WILL_FREE.
657 int write_inode_now(struct inode
*inode
, int sync
)
660 struct writeback_control wbc
= {
661 .nr_to_write
= LONG_MAX
,
662 .sync_mode
= WB_SYNC_ALL
,
664 .range_end
= LLONG_MAX
,
667 if (!mapping_cap_writeback_dirty(inode
->i_mapping
))
671 spin_lock(&inode_lock
);
672 ret
= __writeback_single_inode(inode
, &wbc
);
673 spin_unlock(&inode_lock
);
675 wait_on_inode(inode
);
678 EXPORT_SYMBOL(write_inode_now
);
681 * sync_inode - write an inode and its pages to disk.
682 * @inode: the inode to sync
683 * @wbc: controls the writeback mode
685 * sync_inode() will write an inode and its pages to disk. It will also
686 * correctly update the inode on its superblock's dirty inode lists and will
687 * update inode->i_state.
689 * The caller must have a ref on the inode.
691 int sync_inode(struct inode
*inode
, struct writeback_control
*wbc
)
695 spin_lock(&inode_lock
);
696 ret
= __writeback_single_inode(inode
, wbc
);
697 spin_unlock(&inode_lock
);
700 EXPORT_SYMBOL(sync_inode
);
703 * generic_osync_inode - flush all dirty data for a given inode to disk
704 * @inode: inode to write
705 * @mapping: the address_space that should be flushed
706 * @what: what to write and wait upon
708 * This can be called by file_write functions for files which have the
709 * O_SYNC flag set, to flush dirty writes to disk.
711 * @what is a bitmask, specifying which part of the inode's data should be
712 * written and waited upon.
714 * OSYNC_DATA: i_mapping's dirty data
715 * OSYNC_METADATA: the buffers at i_mapping->private_list
716 * OSYNC_INODE: the inode itself
719 int generic_osync_inode(struct inode
*inode
, struct address_space
*mapping
, int what
)
722 int need_write_inode_now
= 0;
725 if (what
& OSYNC_DATA
)
726 err
= filemap_fdatawrite(mapping
);
727 if (what
& (OSYNC_METADATA
|OSYNC_DATA
)) {
728 err2
= sync_mapping_buffers(mapping
);
732 if (what
& OSYNC_DATA
) {
733 err2
= filemap_fdatawait(mapping
);
738 spin_lock(&inode_lock
);
739 if ((inode
->i_state
& I_DIRTY
) &&
740 ((what
& OSYNC_INODE
) || (inode
->i_state
& I_DIRTY_DATASYNC
)))
741 need_write_inode_now
= 1;
742 spin_unlock(&inode_lock
);
744 if (need_write_inode_now
) {
745 err2
= write_inode_now(inode
, 1);
750 wait_on_inode(inode
);
755 EXPORT_SYMBOL(generic_osync_inode
);
758 * writeback_acquire: attempt to get exclusive writeback access to a device
759 * @bdi: the device's backing_dev_info structure
761 * It is a waste of resources to have more than one pdflush thread blocked on
762 * a single request queue. Exclusion at the request_queue level is obtained
763 * via a flag in the request_queue's backing_dev_info.state.
765 * Non-request_queue-backed address_spaces will share default_backing_dev_info,
766 * unless they implement their own. Which is somewhat inefficient, as this
767 * may prevent concurrent writeback against multiple devices.
769 int writeback_acquire(struct backing_dev_info
*bdi
)
771 return !test_and_set_bit(BDI_pdflush
, &bdi
->state
);
775 * writeback_in_progress: determine whether there is writeback in progress
776 * @bdi: the device's backing_dev_info structure.
778 * Determine whether there is writeback in progress against a backing device.
780 int writeback_in_progress(struct backing_dev_info
*bdi
)
782 return test_bit(BDI_pdflush
, &bdi
->state
);
786 * writeback_release: relinquish exclusive writeback access against a device.
787 * @bdi: the device's backing_dev_info structure
789 void writeback_release(struct backing_dev_info
*bdi
)
791 BUG_ON(!writeback_in_progress(bdi
));
792 clear_bit(BDI_pdflush
, &bdi
->state
);