xref: /openbmc/linux/fs/fs-writeback.c (revision 03ba3782)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * fs/fs-writeback.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Copyright (C) 2002, Linus Torvalds.
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  * Contains all the functions related to writing back and waiting
71da177e4SLinus Torvalds  * upon dirty inodes against superblocks, and writing back dirty
81da177e4SLinus Torvalds  * pages against inodes.  ie: data writeback.  Writeout of the
91da177e4SLinus Torvalds  * inode itself is not handled here.
101da177e4SLinus Torvalds  *
11e1f8e874SFrancois Cami  * 10Apr2002	Andrew Morton
121da177e4SLinus Torvalds  *		Split out of fs/inode.c
131da177e4SLinus Torvalds  *		Additions for address_space-based writeback
141da177e4SLinus Torvalds  */
151da177e4SLinus Torvalds 
161da177e4SLinus Torvalds #include <linux/kernel.h>
17f5ff8422SJens Axboe #include <linux/module.h>
181da177e4SLinus Torvalds #include <linux/spinlock.h>
191da177e4SLinus Torvalds #include <linux/sched.h>
201da177e4SLinus Torvalds #include <linux/fs.h>
211da177e4SLinus Torvalds #include <linux/mm.h>
2203ba3782SJens Axboe #include <linux/kthread.h>
2303ba3782SJens Axboe #include <linux/freezer.h>
241da177e4SLinus Torvalds #include <linux/writeback.h>
251da177e4SLinus Torvalds #include <linux/blkdev.h>
261da177e4SLinus Torvalds #include <linux/backing-dev.h>
271da177e4SLinus Torvalds #include <linux/buffer_head.h>
2807f3f05cSDavid Howells #include "internal.h"
291da177e4SLinus Torvalds 
3066f3b8e2SJens Axboe #define inode_to_bdi(inode)	((inode)->i_mapping->backing_dev_info)
31f11b00f3SAdrian Bunk 
3203ba3782SJens Axboe /*
3303ba3782SJens Axboe  * Work items for the bdi_writeback threads
34f11b00f3SAdrian Bunk  */
3503ba3782SJens Axboe struct bdi_work {
3603ba3782SJens Axboe 	struct list_head list;
3703ba3782SJens Axboe 	struct list_head wait_list;
3803ba3782SJens Axboe 	struct rcu_head rcu_head;
3903ba3782SJens Axboe 
4003ba3782SJens Axboe 	unsigned long seen;
4103ba3782SJens Axboe 	atomic_t pending;
4203ba3782SJens Axboe 
4303ba3782SJens Axboe 	struct super_block *sb;
4403ba3782SJens Axboe 	unsigned long nr_pages;
4503ba3782SJens Axboe 	enum writeback_sync_modes sync_mode;
4603ba3782SJens Axboe 
4703ba3782SJens Axboe 	unsigned long state;
4803ba3782SJens Axboe };
4903ba3782SJens Axboe 
5003ba3782SJens Axboe enum {
5103ba3782SJens Axboe 	WS_USED_B = 0,
5203ba3782SJens Axboe 	WS_ONSTACK_B,
5303ba3782SJens Axboe };
5403ba3782SJens Axboe 
5503ba3782SJens Axboe #define WS_USED (1 << WS_USED_B)
5603ba3782SJens Axboe #define WS_ONSTACK (1 << WS_ONSTACK_B)
5703ba3782SJens Axboe 
5803ba3782SJens Axboe static inline bool bdi_work_on_stack(struct bdi_work *work)
59f11b00f3SAdrian Bunk {
6003ba3782SJens Axboe 	return test_bit(WS_ONSTACK_B, &work->state);
6103ba3782SJens Axboe }
6203ba3782SJens Axboe 
6303ba3782SJens Axboe static inline void bdi_work_init(struct bdi_work *work,
6403ba3782SJens Axboe 				 struct writeback_control *wbc)
6503ba3782SJens Axboe {
6603ba3782SJens Axboe 	INIT_RCU_HEAD(&work->rcu_head);
6703ba3782SJens Axboe 	work->sb = wbc->sb;
6803ba3782SJens Axboe 	work->nr_pages = wbc->nr_to_write;
6903ba3782SJens Axboe 	work->sync_mode = wbc->sync_mode;
7003ba3782SJens Axboe 	work->state = WS_USED;
7103ba3782SJens Axboe }
7203ba3782SJens Axboe 
7303ba3782SJens Axboe static inline void bdi_work_init_on_stack(struct bdi_work *work,
7403ba3782SJens Axboe 					  struct writeback_control *wbc)
7503ba3782SJens Axboe {
7603ba3782SJens Axboe 	bdi_work_init(work, wbc);
7703ba3782SJens Axboe 	work->state |= WS_ONSTACK;
78f11b00f3SAdrian Bunk }
79f11b00f3SAdrian Bunk 
80f11b00f3SAdrian Bunk /**
81f11b00f3SAdrian Bunk  * writeback_in_progress - determine whether there is writeback in progress
82f11b00f3SAdrian Bunk  * @bdi: the device's backing_dev_info structure.
83f11b00f3SAdrian Bunk  *
8403ba3782SJens Axboe  * Determine whether there is writeback waiting to be handled against a
8503ba3782SJens Axboe  * backing device.
86f11b00f3SAdrian Bunk  */
87f11b00f3SAdrian Bunk int writeback_in_progress(struct backing_dev_info *bdi)
88f11b00f3SAdrian Bunk {
8903ba3782SJens Axboe 	return !list_empty(&bdi->work_list);
90f11b00f3SAdrian Bunk }
91f11b00f3SAdrian Bunk 
9203ba3782SJens Axboe static void bdi_work_clear(struct bdi_work *work)
93f11b00f3SAdrian Bunk {
9403ba3782SJens Axboe 	clear_bit(WS_USED_B, &work->state);
9503ba3782SJens Axboe 	smp_mb__after_clear_bit();
9603ba3782SJens Axboe 	wake_up_bit(&work->state, WS_USED_B);
97f11b00f3SAdrian Bunk }
98f11b00f3SAdrian Bunk 
9903ba3782SJens Axboe static void bdi_work_free(struct rcu_head *head)
1004195f73dSNick Piggin {
10103ba3782SJens Axboe 	struct bdi_work *work = container_of(head, struct bdi_work, rcu_head);
1024195f73dSNick Piggin 
10303ba3782SJens Axboe 	if (!bdi_work_on_stack(work))
10403ba3782SJens Axboe 		kfree(work);
10503ba3782SJens Axboe 	else
10603ba3782SJens Axboe 		bdi_work_clear(work);
1074195f73dSNick Piggin }
1084195f73dSNick Piggin 
10903ba3782SJens Axboe static void wb_work_complete(struct bdi_work *work)
1101da177e4SLinus Torvalds {
11103ba3782SJens Axboe 	const enum writeback_sync_modes sync_mode = work->sync_mode;
1121da177e4SLinus Torvalds 
1131da177e4SLinus Torvalds 	/*
11403ba3782SJens Axboe 	 * For allocated work, we can clear the done/seen bit right here.
11503ba3782SJens Axboe 	 * For on-stack work, we need to postpone both the clear and free
11603ba3782SJens Axboe 	 * to after the RCU grace period, since the stack could be invalidated
11703ba3782SJens Axboe 	 * as soon as bdi_work_clear() has done the wakeup.
1181da177e4SLinus Torvalds 	 */
11903ba3782SJens Axboe 	if (!bdi_work_on_stack(work))
12003ba3782SJens Axboe 		bdi_work_clear(work);
12103ba3782SJens Axboe 	if (sync_mode == WB_SYNC_NONE || bdi_work_on_stack(work))
12203ba3782SJens Axboe 		call_rcu(&work->rcu_head, bdi_work_free);
1231da177e4SLinus Torvalds }
1241da177e4SLinus Torvalds 
12503ba3782SJens Axboe static void wb_clear_pending(struct bdi_writeback *wb, struct bdi_work *work)
12603ba3782SJens Axboe {
1271da177e4SLinus Torvalds 	/*
12803ba3782SJens Axboe 	 * The caller has retrieved the work arguments from this work,
12903ba3782SJens Axboe 	 * drop our reference. If this is the last ref, delete and free it
13003ba3782SJens Axboe 	 */
13103ba3782SJens Axboe 	if (atomic_dec_and_test(&work->pending)) {
13203ba3782SJens Axboe 		struct backing_dev_info *bdi = wb->bdi;
13303ba3782SJens Axboe 
13403ba3782SJens Axboe 		spin_lock(&bdi->wb_lock);
13503ba3782SJens Axboe 		list_del_rcu(&work->list);
13603ba3782SJens Axboe 		spin_unlock(&bdi->wb_lock);
13703ba3782SJens Axboe 
13803ba3782SJens Axboe 		wb_work_complete(work);
13903ba3782SJens Axboe 	}
14003ba3782SJens Axboe }
14103ba3782SJens Axboe 
14203ba3782SJens Axboe static void bdi_queue_work(struct backing_dev_info *bdi, struct bdi_work *work)
14303ba3782SJens Axboe {
14403ba3782SJens Axboe 	if (work) {
14503ba3782SJens Axboe 		work->seen = bdi->wb_mask;
14603ba3782SJens Axboe 		BUG_ON(!work->seen);
14703ba3782SJens Axboe 		atomic_set(&work->pending, bdi->wb_cnt);
14803ba3782SJens Axboe 		BUG_ON(!bdi->wb_cnt);
14903ba3782SJens Axboe 
15003ba3782SJens Axboe 		/*
15103ba3782SJens Axboe 		 * Make sure stores are seen before it appears on the list
1521da177e4SLinus Torvalds 		 */
1531da177e4SLinus Torvalds 		smp_mb();
1541da177e4SLinus Torvalds 
15503ba3782SJens Axboe 		spin_lock(&bdi->wb_lock);
15603ba3782SJens Axboe 		list_add_tail_rcu(&work->list, &bdi->work_list);
15703ba3782SJens Axboe 		spin_unlock(&bdi->wb_lock);
15803ba3782SJens Axboe 	}
1591da177e4SLinus Torvalds 
1601da177e4SLinus Torvalds 	/*
16103ba3782SJens Axboe 	 * If the default thread isn't there, make sure we add it. When
16203ba3782SJens Axboe 	 * it gets created and wakes up, we'll run this work.
1631da177e4SLinus Torvalds 	 */
16403ba3782SJens Axboe 	if (unlikely(list_empty_careful(&bdi->wb_list)))
16503ba3782SJens Axboe 		wake_up_process(default_backing_dev_info.wb.task);
16603ba3782SJens Axboe 	else {
16703ba3782SJens Axboe 		struct bdi_writeback *wb = &bdi->wb;
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds 		/*
17003ba3782SJens Axboe 		 * If we failed allocating the bdi work item, wake up the wb
17103ba3782SJens Axboe 		 * thread always. As a safety precaution, it'll flush out
17203ba3782SJens Axboe 		 * everything
1731da177e4SLinus Torvalds 		 */
17403ba3782SJens Axboe 		if (!wb_has_dirty_io(wb)) {
17503ba3782SJens Axboe 			if (work)
17603ba3782SJens Axboe 				wb_clear_pending(wb, work);
17703ba3782SJens Axboe 		} else if (wb->task)
17803ba3782SJens Axboe 			wake_up_process(wb->task);
1791da177e4SLinus Torvalds 	}
18003ba3782SJens Axboe }
1811da177e4SLinus Torvalds 
1821da177e4SLinus Torvalds /*
18303ba3782SJens Axboe  * Used for on-stack allocated work items. The caller needs to wait until
18403ba3782SJens Axboe  * the wb threads have acked the work before it's safe to continue.
1851da177e4SLinus Torvalds  */
18603ba3782SJens Axboe static void bdi_wait_on_work_clear(struct bdi_work *work)
1871da177e4SLinus Torvalds {
18803ba3782SJens Axboe 	wait_on_bit(&work->state, WS_USED_B, bdi_sched_wait,
18903ba3782SJens Axboe 		    TASK_UNINTERRUPTIBLE);
19003ba3782SJens Axboe }
19103ba3782SJens Axboe 
19203ba3782SJens Axboe static struct bdi_work *bdi_alloc_work(struct writeback_control *wbc)
19303ba3782SJens Axboe {
19403ba3782SJens Axboe 	struct bdi_work *work;
19503ba3782SJens Axboe 
19603ba3782SJens Axboe 	work = kmalloc(sizeof(*work), GFP_ATOMIC);
19703ba3782SJens Axboe 	if (work)
19803ba3782SJens Axboe 		bdi_work_init(work, wbc);
19903ba3782SJens Axboe 
20003ba3782SJens Axboe 	return work;
20103ba3782SJens Axboe }
20203ba3782SJens Axboe 
20303ba3782SJens Axboe void bdi_start_writeback(struct writeback_control *wbc)
20403ba3782SJens Axboe {
20503ba3782SJens Axboe 	const bool must_wait = wbc->sync_mode == WB_SYNC_ALL;
20603ba3782SJens Axboe 	struct bdi_work work_stack, *work = NULL;
20703ba3782SJens Axboe 
20803ba3782SJens Axboe 	if (!must_wait)
20903ba3782SJens Axboe 		work = bdi_alloc_work(wbc);
21003ba3782SJens Axboe 
21103ba3782SJens Axboe 	if (!work) {
21203ba3782SJens Axboe 		work = &work_stack;
21303ba3782SJens Axboe 		bdi_work_init_on_stack(work, wbc);
21403ba3782SJens Axboe 	}
21503ba3782SJens Axboe 
21603ba3782SJens Axboe 	bdi_queue_work(wbc->bdi, work);
21703ba3782SJens Axboe 
21803ba3782SJens Axboe 	/*
21903ba3782SJens Axboe 	 * If the sync mode is WB_SYNC_ALL, block waiting for the work to
22003ba3782SJens Axboe 	 * complete. If not, we only need to wait for the work to be started,
22103ba3782SJens Axboe 	 * if we allocated it on-stack. We use the same mechanism, if the
22203ba3782SJens Axboe 	 * wait bit is set in the bdi_work struct, then threads will not
22303ba3782SJens Axboe 	 * clear pending until after they are done.
22403ba3782SJens Axboe 	 *
22503ba3782SJens Axboe 	 * Note that work == &work_stack if must_wait is true, so we don't
22603ba3782SJens Axboe 	 * need to do call_rcu() here ever, since the completion path will
22703ba3782SJens Axboe 	 * have done that for us.
22803ba3782SJens Axboe 	 */
22903ba3782SJens Axboe 	if (must_wait || work == &work_stack) {
23003ba3782SJens Axboe 		bdi_wait_on_work_clear(work);
23103ba3782SJens Axboe 		if (work != &work_stack)
23203ba3782SJens Axboe 			call_rcu(&work->rcu_head, bdi_work_free);
23303ba3782SJens Axboe 	}
2341da177e4SLinus Torvalds }
2351da177e4SLinus Torvalds 
2361da177e4SLinus Torvalds /*
2376610a0bcSAndrew Morton  * Redirty an inode: set its when-it-was dirtied timestamp and move it to the
2386610a0bcSAndrew Morton  * furthest end of its superblock's dirty-inode list.
2396610a0bcSAndrew Morton  *
2406610a0bcSAndrew Morton  * Before stamping the inode's ->dirtied_when, we check to see whether it is
24166f3b8e2SJens Axboe  * already the most-recently-dirtied inode on the b_dirty list.  If that is
2426610a0bcSAndrew Morton  * the case then the inode must have been redirtied while it was being written
2436610a0bcSAndrew Morton  * out and we don't reset its dirtied_when.
2446610a0bcSAndrew Morton  */
2456610a0bcSAndrew Morton static void redirty_tail(struct inode *inode)
2466610a0bcSAndrew Morton {
24703ba3782SJens Axboe 	struct bdi_writeback *wb = &inode_to_bdi(inode)->wb;
2486610a0bcSAndrew Morton 
24903ba3782SJens Axboe 	if (!list_empty(&wb->b_dirty)) {
25066f3b8e2SJens Axboe 		struct inode *tail;
2516610a0bcSAndrew Morton 
25203ba3782SJens Axboe 		tail = list_entry(wb->b_dirty.next, struct inode, i_list);
25366f3b8e2SJens Axboe 		if (time_before(inode->dirtied_when, tail->dirtied_when))
2546610a0bcSAndrew Morton 			inode->dirtied_when = jiffies;
2556610a0bcSAndrew Morton 	}
25603ba3782SJens Axboe 	list_move(&inode->i_list, &wb->b_dirty);
2576610a0bcSAndrew Morton }
2586610a0bcSAndrew Morton 
2596610a0bcSAndrew Morton /*
26066f3b8e2SJens Axboe  * requeue inode for re-scanning after bdi->b_io list is exhausted.
261c986d1e2SAndrew Morton  */
2620e0f4fc2SKen Chen static void requeue_io(struct inode *inode)
263c986d1e2SAndrew Morton {
26403ba3782SJens Axboe 	struct bdi_writeback *wb = &inode_to_bdi(inode)->wb;
26503ba3782SJens Axboe 
26603ba3782SJens Axboe 	list_move(&inode->i_list, &wb->b_more_io);
267c986d1e2SAndrew Morton }
268c986d1e2SAndrew Morton 
2691c0eeaf5SJoern Engel static void inode_sync_complete(struct inode *inode)
2701c0eeaf5SJoern Engel {
2711c0eeaf5SJoern Engel 	/*
2721c0eeaf5SJoern Engel 	 * Prevent speculative execution through spin_unlock(&inode_lock);
2731c0eeaf5SJoern Engel 	 */
2741c0eeaf5SJoern Engel 	smp_mb();
2751c0eeaf5SJoern Engel 	wake_up_bit(&inode->i_state, __I_SYNC);
2761c0eeaf5SJoern Engel }
2771c0eeaf5SJoern Engel 
278d2caa3c5SJeff Layton static bool inode_dirtied_after(struct inode *inode, unsigned long t)
279d2caa3c5SJeff Layton {
280d2caa3c5SJeff Layton 	bool ret = time_after(inode->dirtied_when, t);
281d2caa3c5SJeff Layton #ifndef CONFIG_64BIT
282d2caa3c5SJeff Layton 	/*
283d2caa3c5SJeff Layton 	 * For inodes being constantly redirtied, dirtied_when can get stuck.
284d2caa3c5SJeff Layton 	 * It _appears_ to be in the future, but is actually in distant past.
285d2caa3c5SJeff Layton 	 * This test is necessary to prevent such wrapped-around relative times
286d2caa3c5SJeff Layton 	 * from permanently stopping the whole pdflush writeback.
287d2caa3c5SJeff Layton 	 */
288d2caa3c5SJeff Layton 	ret = ret && time_before_eq(inode->dirtied_when, jiffies);
289d2caa3c5SJeff Layton #endif
290d2caa3c5SJeff Layton 	return ret;
291d2caa3c5SJeff Layton }
292d2caa3c5SJeff Layton 
293c986d1e2SAndrew Morton /*
2942c136579SFengguang Wu  * Move expired dirty inodes from @delaying_queue to @dispatch_queue.
2952c136579SFengguang Wu  */
2962c136579SFengguang Wu static void move_expired_inodes(struct list_head *delaying_queue,
2972c136579SFengguang Wu 			       struct list_head *dispatch_queue,
2982c136579SFengguang Wu 				unsigned long *older_than_this)
2992c136579SFengguang Wu {
3002c136579SFengguang Wu 	while (!list_empty(delaying_queue)) {
3012c136579SFengguang Wu 		struct inode *inode = list_entry(delaying_queue->prev,
3022c136579SFengguang Wu 						struct inode, i_list);
3032c136579SFengguang Wu 		if (older_than_this &&
304d2caa3c5SJeff Layton 		    inode_dirtied_after(inode, *older_than_this))
3052c136579SFengguang Wu 			break;
3062c136579SFengguang Wu 		list_move(&inode->i_list, dispatch_queue);
3072c136579SFengguang Wu 	}
3082c136579SFengguang Wu }
3092c136579SFengguang Wu 
3102c136579SFengguang Wu /*
3112c136579SFengguang Wu  * Queue all expired dirty inodes for io, eldest first.
3122c136579SFengguang Wu  */
31303ba3782SJens Axboe static void queue_io(struct bdi_writeback *wb, unsigned long *older_than_this)
3142c136579SFengguang Wu {
31503ba3782SJens Axboe 	list_splice_init(&wb->b_more_io, wb->b_io.prev);
31603ba3782SJens Axboe 	move_expired_inodes(&wb->b_dirty, &wb->b_io, older_than_this);
31766f3b8e2SJens Axboe }
31866f3b8e2SJens Axboe 
31903ba3782SJens Axboe static int write_inode(struct inode *inode, int sync)
32066f3b8e2SJens Axboe {
32103ba3782SJens Axboe 	if (inode->i_sb->s_op->write_inode && !is_bad_inode(inode))
32203ba3782SJens Axboe 		return inode->i_sb->s_op->write_inode(inode, sync);
32303ba3782SJens Axboe 	return 0;
32466f3b8e2SJens Axboe }
32508d8e974SFengguang Wu 
3262c136579SFengguang Wu /*
32701c03194SChristoph Hellwig  * Wait for writeback on an inode to complete.
32801c03194SChristoph Hellwig  */
32901c03194SChristoph Hellwig static void inode_wait_for_writeback(struct inode *inode)
33001c03194SChristoph Hellwig {
33101c03194SChristoph Hellwig 	DEFINE_WAIT_BIT(wq, &inode->i_state, __I_SYNC);
33201c03194SChristoph Hellwig 	wait_queue_head_t *wqh;
33301c03194SChristoph Hellwig 
33401c03194SChristoph Hellwig 	wqh = bit_waitqueue(&inode->i_state, __I_SYNC);
33501c03194SChristoph Hellwig 	do {
33601c03194SChristoph Hellwig 		spin_unlock(&inode_lock);
33701c03194SChristoph Hellwig 		__wait_on_bit(wqh, &wq, inode_wait, TASK_UNINTERRUPTIBLE);
33801c03194SChristoph Hellwig 		spin_lock(&inode_lock);
33901c03194SChristoph Hellwig 	} while (inode->i_state & I_SYNC);
34001c03194SChristoph Hellwig }
34101c03194SChristoph Hellwig 
34201c03194SChristoph Hellwig /*
34301c03194SChristoph Hellwig  * Write out an inode's dirty pages.  Called under inode_lock.  Either the
34401c03194SChristoph Hellwig  * caller has ref on the inode (either via __iget or via syscall against an fd)
34501c03194SChristoph Hellwig  * or the inode has I_WILL_FREE set (via generic_forget_inode)
34601c03194SChristoph Hellwig  *
3471da177e4SLinus Torvalds  * If `wait' is set, wait on the writeout.
3481da177e4SLinus Torvalds  *
3491da177e4SLinus Torvalds  * The whole writeout design is quite complex and fragile.  We want to avoid
3501da177e4SLinus Torvalds  * starvation of particular inodes when others are being redirtied, prevent
3511da177e4SLinus Torvalds  * livelocks, etc.
3521da177e4SLinus Torvalds  *
3531da177e4SLinus Torvalds  * Called under inode_lock.
3541da177e4SLinus Torvalds  */
3551da177e4SLinus Torvalds static int
35601c03194SChristoph Hellwig writeback_single_inode(struct inode *inode, struct writeback_control *wbc)
3571da177e4SLinus Torvalds {
3581da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
3591da177e4SLinus Torvalds 	int wait = wbc->sync_mode == WB_SYNC_ALL;
36001c03194SChristoph Hellwig 	unsigned dirty;
3611da177e4SLinus Torvalds 	int ret;
3621da177e4SLinus Torvalds 
36301c03194SChristoph Hellwig 	if (!atomic_read(&inode->i_count))
36401c03194SChristoph Hellwig 		WARN_ON(!(inode->i_state & (I_WILL_FREE|I_FREEING)));
36501c03194SChristoph Hellwig 	else
36601c03194SChristoph Hellwig 		WARN_ON(inode->i_state & I_WILL_FREE);
36701c03194SChristoph Hellwig 
36801c03194SChristoph Hellwig 	if (inode->i_state & I_SYNC) {
36901c03194SChristoph Hellwig 		/*
37001c03194SChristoph Hellwig 		 * If this inode is locked for writeback and we are not doing
37166f3b8e2SJens Axboe 		 * writeback-for-data-integrity, move it to b_more_io so that
37201c03194SChristoph Hellwig 		 * writeback can proceed with the other inodes on s_io.
37301c03194SChristoph Hellwig 		 *
37401c03194SChristoph Hellwig 		 * We'll have another go at writing back this inode when we
37566f3b8e2SJens Axboe 		 * completed a full scan of b_io.
37601c03194SChristoph Hellwig 		 */
37701c03194SChristoph Hellwig 		if (!wait) {
37801c03194SChristoph Hellwig 			requeue_io(inode);
37901c03194SChristoph Hellwig 			return 0;
38001c03194SChristoph Hellwig 		}
38101c03194SChristoph Hellwig 
38201c03194SChristoph Hellwig 		/*
38301c03194SChristoph Hellwig 		 * It's a data-integrity sync.  We must wait.
38401c03194SChristoph Hellwig 		 */
38501c03194SChristoph Hellwig 		inode_wait_for_writeback(inode);
38601c03194SChristoph Hellwig 	}
38701c03194SChristoph Hellwig 
3881c0eeaf5SJoern Engel 	BUG_ON(inode->i_state & I_SYNC);
3891da177e4SLinus Torvalds 
3901c0eeaf5SJoern Engel 	/* Set I_SYNC, reset I_DIRTY */
3911da177e4SLinus Torvalds 	dirty = inode->i_state & I_DIRTY;
3921c0eeaf5SJoern Engel 	inode->i_state |= I_SYNC;
3931da177e4SLinus Torvalds 	inode->i_state &= ~I_DIRTY;
3941da177e4SLinus Torvalds 
3951da177e4SLinus Torvalds 	spin_unlock(&inode_lock);
3961da177e4SLinus Torvalds 
3971da177e4SLinus Torvalds 	ret = do_writepages(mapping, wbc);
3981da177e4SLinus Torvalds 
3991da177e4SLinus Torvalds 	/* Don't write the inode if only I_DIRTY_PAGES was set */
4001da177e4SLinus Torvalds 	if (dirty & (I_DIRTY_SYNC | I_DIRTY_DATASYNC)) {
4011da177e4SLinus Torvalds 		int err = write_inode(inode, wait);
4021da177e4SLinus Torvalds 		if (ret == 0)
4031da177e4SLinus Torvalds 			ret = err;
4041da177e4SLinus Torvalds 	}
4051da177e4SLinus Torvalds 
4061da177e4SLinus Torvalds 	if (wait) {
4071da177e4SLinus Torvalds 		int err = filemap_fdatawait(mapping);
4081da177e4SLinus Torvalds 		if (ret == 0)
4091da177e4SLinus Torvalds 			ret = err;
4101da177e4SLinus Torvalds 	}
4111da177e4SLinus Torvalds 
4121da177e4SLinus Torvalds 	spin_lock(&inode_lock);
4131c0eeaf5SJoern Engel 	inode->i_state &= ~I_SYNC;
41484a89245SWu Fengguang 	if (!(inode->i_state & (I_FREEING | I_CLEAR))) {
4151da177e4SLinus Torvalds 		if (!(inode->i_state & I_DIRTY) &&
4161da177e4SLinus Torvalds 		    mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) {
4171da177e4SLinus Torvalds 			/*
4181da177e4SLinus Torvalds 			 * We didn't write back all the pages.  nfs_writepages()
4191da177e4SLinus Torvalds 			 * sometimes bales out without doing anything. Redirty
42066f3b8e2SJens Axboe 			 * the inode; Move it from b_io onto b_more_io/b_dirty.
4211b43ef91SAndrew Morton 			 */
4221b43ef91SAndrew Morton 			/*
4231b43ef91SAndrew Morton 			 * akpm: if the caller was the kupdate function we put
42466f3b8e2SJens Axboe 			 * this inode at the head of b_dirty so it gets first
4251b43ef91SAndrew Morton 			 * consideration.  Otherwise, move it to the tail, for
4261b43ef91SAndrew Morton 			 * the reasons described there.  I'm not really sure
4271b43ef91SAndrew Morton 			 * how much sense this makes.  Presumably I had a good
4281b43ef91SAndrew Morton 			 * reasons for doing it this way, and I'd rather not
4291b43ef91SAndrew Morton 			 * muck with it at present.
4301da177e4SLinus Torvalds 			 */
4311da177e4SLinus Torvalds 			if (wbc->for_kupdate) {
4321da177e4SLinus Torvalds 				/*
4332c136579SFengguang Wu 				 * For the kupdate function we move the inode
43466f3b8e2SJens Axboe 				 * to b_more_io so it will get more writeout as
4352c136579SFengguang Wu 				 * soon as the queue becomes uncongested.
4361da177e4SLinus Torvalds 				 */
4371da177e4SLinus Torvalds 				inode->i_state |= I_DIRTY_PAGES;
4388bc3be27SFengguang Wu 				if (wbc->nr_to_write <= 0) {
4398bc3be27SFengguang Wu 					/*
4408bc3be27SFengguang Wu 					 * slice used up: queue for next turn
4418bc3be27SFengguang Wu 					 */
4420e0f4fc2SKen Chen 					requeue_io(inode);
4431da177e4SLinus Torvalds 				} else {
4441da177e4SLinus Torvalds 					/*
4458bc3be27SFengguang Wu 					 * somehow blocked: retry later
4468bc3be27SFengguang Wu 					 */
4478bc3be27SFengguang Wu 					redirty_tail(inode);
4488bc3be27SFengguang Wu 				}
4498bc3be27SFengguang Wu 			} else {
4508bc3be27SFengguang Wu 				/*
4511da177e4SLinus Torvalds 				 * Otherwise fully redirty the inode so that
4521da177e4SLinus Torvalds 				 * other inodes on this superblock will get some
4531da177e4SLinus Torvalds 				 * writeout.  Otherwise heavy writing to one
4541da177e4SLinus Torvalds 				 * file would indefinitely suspend writeout of
4551da177e4SLinus Torvalds 				 * all the other files.
4561da177e4SLinus Torvalds 				 */
4571da177e4SLinus Torvalds 				inode->i_state |= I_DIRTY_PAGES;
4581b43ef91SAndrew Morton 				redirty_tail(inode);
4591da177e4SLinus Torvalds 			}
4601da177e4SLinus Torvalds 		} else if (inode->i_state & I_DIRTY) {
4611da177e4SLinus Torvalds 			/*
4621da177e4SLinus Torvalds 			 * Someone redirtied the inode while were writing back
4631da177e4SLinus Torvalds 			 * the pages.
4641da177e4SLinus Torvalds 			 */
4656610a0bcSAndrew Morton 			redirty_tail(inode);
4661da177e4SLinus Torvalds 		} else if (atomic_read(&inode->i_count)) {
4671da177e4SLinus Torvalds 			/*
4681da177e4SLinus Torvalds 			 * The inode is clean, inuse
4691da177e4SLinus Torvalds 			 */
4701da177e4SLinus Torvalds 			list_move(&inode->i_list, &inode_in_use);
4711da177e4SLinus Torvalds 		} else {
4721da177e4SLinus Torvalds 			/*
4731da177e4SLinus Torvalds 			 * The inode is clean, unused
4741da177e4SLinus Torvalds 			 */
4751da177e4SLinus Torvalds 			list_move(&inode->i_list, &inode_unused);
4761da177e4SLinus Torvalds 		}
4771da177e4SLinus Torvalds 	}
4781c0eeaf5SJoern Engel 	inode_sync_complete(inode);
4791da177e4SLinus Torvalds 	return ret;
4801da177e4SLinus Torvalds }
4811da177e4SLinus Torvalds 
48203ba3782SJens Axboe /*
48303ba3782SJens Axboe  * For WB_SYNC_NONE writeback, the caller does not have the sb pinned
48403ba3782SJens Axboe  * before calling writeback. So make sure that we do pin it, so it doesn't
48503ba3782SJens Axboe  * go away while we are writing inodes from it.
48603ba3782SJens Axboe  *
48703ba3782SJens Axboe  * Returns 0 if the super was successfully pinned (or pinning wasn't needed),
48803ba3782SJens Axboe  * 1 if we failed.
48903ba3782SJens Axboe  */
49003ba3782SJens Axboe static int pin_sb_for_writeback(struct writeback_control *wbc,
49103ba3782SJens Axboe 				   struct inode *inode)
4921da177e4SLinus Torvalds {
49303ba3782SJens Axboe 	struct super_block *sb = inode->i_sb;
49403ba3782SJens Axboe 
49503ba3782SJens Axboe 	/*
49603ba3782SJens Axboe 	 * Caller must already hold the ref for this
49703ba3782SJens Axboe 	 */
49803ba3782SJens Axboe 	if (wbc->sync_mode == WB_SYNC_ALL) {
49903ba3782SJens Axboe 		WARN_ON(!rwsem_is_locked(&sb->s_umount));
50003ba3782SJens Axboe 		return 0;
50103ba3782SJens Axboe 	}
50203ba3782SJens Axboe 
50303ba3782SJens Axboe 	spin_lock(&sb_lock);
50403ba3782SJens Axboe 	sb->s_count++;
50503ba3782SJens Axboe 	if (down_read_trylock(&sb->s_umount)) {
50603ba3782SJens Axboe 		if (sb->s_root) {
50703ba3782SJens Axboe 			spin_unlock(&sb_lock);
50803ba3782SJens Axboe 			return 0;
50903ba3782SJens Axboe 		}
51003ba3782SJens Axboe 		/*
51103ba3782SJens Axboe 		 * umounted, drop rwsem again and fall through to failure
51203ba3782SJens Axboe 		 */
51303ba3782SJens Axboe 		up_read(&sb->s_umount);
51403ba3782SJens Axboe 	}
51503ba3782SJens Axboe 
51603ba3782SJens Axboe 	sb->s_count--;
51703ba3782SJens Axboe 	spin_unlock(&sb_lock);
51803ba3782SJens Axboe 	return 1;
51903ba3782SJens Axboe }
52003ba3782SJens Axboe 
52103ba3782SJens Axboe static void unpin_sb_for_writeback(struct writeback_control *wbc,
52203ba3782SJens Axboe 				   struct inode *inode)
52303ba3782SJens Axboe {
52403ba3782SJens Axboe 	struct super_block *sb = inode->i_sb;
52503ba3782SJens Axboe 
52603ba3782SJens Axboe 	if (wbc->sync_mode == WB_SYNC_ALL)
52703ba3782SJens Axboe 		return;
52803ba3782SJens Axboe 
52903ba3782SJens Axboe 	up_read(&sb->s_umount);
53003ba3782SJens Axboe 	put_super(sb);
53103ba3782SJens Axboe }
53203ba3782SJens Axboe 
53303ba3782SJens Axboe static void writeback_inodes_wb(struct bdi_writeback *wb,
53403ba3782SJens Axboe 				struct writeback_control *wbc)
53503ba3782SJens Axboe {
53603ba3782SJens Axboe 	struct super_block *sb = wbc->sb;
53766f3b8e2SJens Axboe 	const int is_blkdev_sb = sb_is_blkdev_sb(sb);
5381da177e4SLinus Torvalds 	const unsigned long start = jiffies;	/* livelock avoidance */
5391da177e4SLinus Torvalds 
540ae8547b0SHans Reiser 	spin_lock(&inode_lock);
5411da177e4SLinus Torvalds 
54203ba3782SJens Axboe 	if (!wbc->for_kupdate || list_empty(&wb->b_io))
54303ba3782SJens Axboe 		queue_io(wb, wbc->older_than_this);
54466f3b8e2SJens Axboe 
54503ba3782SJens Axboe 	while (!list_empty(&wb->b_io)) {
54603ba3782SJens Axboe 		struct inode *inode = list_entry(wb->b_io.prev,
5471da177e4SLinus Torvalds 						struct inode, i_list);
5481da177e4SLinus Torvalds 		long pages_skipped;
5491da177e4SLinus Torvalds 
55066f3b8e2SJens Axboe 		/*
55166f3b8e2SJens Axboe 		 * super block given and doesn't match, skip this inode
55266f3b8e2SJens Axboe 		 */
55366f3b8e2SJens Axboe 		if (sb && sb != inode->i_sb) {
55466f3b8e2SJens Axboe 			redirty_tail(inode);
55566f3b8e2SJens Axboe 			continue;
55666f3b8e2SJens Axboe 		}
55766f3b8e2SJens Axboe 
55803ba3782SJens Axboe 		if (!bdi_cap_writeback_dirty(wb->bdi)) {
5599852a0e7SAndrew Morton 			redirty_tail(inode);
56066f3b8e2SJens Axboe 			if (is_blkdev_sb) {
5611da177e4SLinus Torvalds 				/*
5621da177e4SLinus Torvalds 				 * Dirty memory-backed blockdev: the ramdisk
5631da177e4SLinus Torvalds 				 * driver does this.  Skip just this inode
5641da177e4SLinus Torvalds 				 */
5651da177e4SLinus Torvalds 				continue;
5661da177e4SLinus Torvalds 			}
5671da177e4SLinus Torvalds 			/*
5681da177e4SLinus Torvalds 			 * Dirty memory-backed inode against a filesystem other
5691da177e4SLinus Torvalds 			 * than the kernel-internal bdev filesystem.  Skip the
5701da177e4SLinus Torvalds 			 * entire superblock.
5711da177e4SLinus Torvalds 			 */
5721da177e4SLinus Torvalds 			break;
5731da177e4SLinus Torvalds 		}
5741da177e4SLinus Torvalds 
57584a89245SWu Fengguang 		if (inode->i_state & (I_NEW | I_WILL_FREE)) {
5767ef0d737SNick Piggin 			requeue_io(inode);
5777ef0d737SNick Piggin 			continue;
5787ef0d737SNick Piggin 		}
5797ef0d737SNick Piggin 
58003ba3782SJens Axboe 		if (wbc->nonblocking && bdi_write_congested(wb->bdi)) {
5811da177e4SLinus Torvalds 			wbc->encountered_congestion = 1;
58266f3b8e2SJens Axboe 			if (!is_blkdev_sb)
5831da177e4SLinus Torvalds 				break;		/* Skip a congested fs */
5840e0f4fc2SKen Chen 			requeue_io(inode);
5851da177e4SLinus Torvalds 			continue;		/* Skip a congested blockdev */
5861da177e4SLinus Torvalds 		}
5871da177e4SLinus Torvalds 
588d2caa3c5SJeff Layton 		/*
589d2caa3c5SJeff Layton 		 * Was this inode dirtied after sync_sb_inodes was called?
590d2caa3c5SJeff Layton 		 * This keeps sync from extra jobs and livelock.
591d2caa3c5SJeff Layton 		 */
592d2caa3c5SJeff Layton 		if (inode_dirtied_after(inode, start))
5931da177e4SLinus Torvalds 			break;
5941da177e4SLinus Torvalds 
59503ba3782SJens Axboe 		if (pin_sb_for_writeback(wbc, inode)) {
59603ba3782SJens Axboe 			requeue_io(inode);
59703ba3782SJens Axboe 			continue;
59803ba3782SJens Axboe 		}
5991da177e4SLinus Torvalds 
60084a89245SWu Fengguang 		BUG_ON(inode->i_state & (I_FREEING | I_CLEAR));
6011da177e4SLinus Torvalds 		__iget(inode);
6021da177e4SLinus Torvalds 		pages_skipped = wbc->pages_skipped;
60301c03194SChristoph Hellwig 		writeback_single_inode(inode, wbc);
60403ba3782SJens Axboe 		unpin_sb_for_writeback(wbc, inode);
6051da177e4SLinus Torvalds 		if (wbc->pages_skipped != pages_skipped) {
6061da177e4SLinus Torvalds 			/*
6071da177e4SLinus Torvalds 			 * writeback is not making progress due to locked
6081da177e4SLinus Torvalds 			 * buffers.  Skip this inode for now.
6091da177e4SLinus Torvalds 			 */
610f57b9b7bSAndrew Morton 			redirty_tail(inode);
6111da177e4SLinus Torvalds 		}
6121da177e4SLinus Torvalds 		spin_unlock(&inode_lock);
6131da177e4SLinus Torvalds 		iput(inode);
6144ffc8444SOGAWA Hirofumi 		cond_resched();
6151da177e4SLinus Torvalds 		spin_lock(&inode_lock);
6168bc3be27SFengguang Wu 		if (wbc->nr_to_write <= 0) {
6178bc3be27SFengguang Wu 			wbc->more_io = 1;
6181da177e4SLinus Torvalds 			break;
6191da177e4SLinus Torvalds 		}
62003ba3782SJens Axboe 		if (!list_empty(&wb->b_more_io))
6218bc3be27SFengguang Wu 			wbc->more_io = 1;
6228bc3be27SFengguang Wu 	}
62338f21977SNick Piggin 
62466f3b8e2SJens Axboe 	spin_unlock(&inode_lock);
62566f3b8e2SJens Axboe 	/* Leave any unwritten inodes on b_io */
62666f3b8e2SJens Axboe }
62766f3b8e2SJens Axboe 
62803ba3782SJens Axboe void writeback_inodes_wbc(struct writeback_control *wbc)
62903ba3782SJens Axboe {
63003ba3782SJens Axboe 	struct backing_dev_info *bdi = wbc->bdi;
63103ba3782SJens Axboe 
63203ba3782SJens Axboe 	writeback_inodes_wb(&bdi->wb, wbc);
63303ba3782SJens Axboe }
63403ba3782SJens Axboe 
63503ba3782SJens Axboe /*
63603ba3782SJens Axboe  * The maximum number of pages to writeout in a single bdi flush/kupdate
63703ba3782SJens Axboe  * operation.  We do this so we don't hold I_SYNC against an inode for
63803ba3782SJens Axboe  * enormous amounts of time, which would block a userspace task which has
63903ba3782SJens Axboe  * been forced to throttle against that inode.  Also, the code reevaluates
64003ba3782SJens Axboe  * the dirty each time it has written this many pages.
64103ba3782SJens Axboe  */
64203ba3782SJens Axboe #define MAX_WRITEBACK_PAGES     1024
64303ba3782SJens Axboe 
64403ba3782SJens Axboe static inline bool over_bground_thresh(void)
64503ba3782SJens Axboe {
64603ba3782SJens Axboe 	unsigned long background_thresh, dirty_thresh;
64703ba3782SJens Axboe 
64803ba3782SJens Axboe 	get_dirty_limits(&background_thresh, &dirty_thresh, NULL, NULL);
64903ba3782SJens Axboe 
65003ba3782SJens Axboe 	return (global_page_state(NR_FILE_DIRTY) +
65103ba3782SJens Axboe 		global_page_state(NR_UNSTABLE_NFS) >= background_thresh);
65203ba3782SJens Axboe }
65303ba3782SJens Axboe 
65403ba3782SJens Axboe /*
65503ba3782SJens Axboe  * Explicit flushing or periodic writeback of "old" data.
65603ba3782SJens Axboe  *
65703ba3782SJens Axboe  * Define "old": the first time one of an inode's pages is dirtied, we mark the
65803ba3782SJens Axboe  * dirtying-time in the inode's address_space.  So this periodic writeback code
65903ba3782SJens Axboe  * just walks the superblock inode list, writing back any inodes which are
66003ba3782SJens Axboe  * older than a specific point in time.
66103ba3782SJens Axboe  *
66203ba3782SJens Axboe  * Try to run once per dirty_writeback_interval.  But if a writeback event
66303ba3782SJens Axboe  * takes longer than a dirty_writeback_interval interval, then leave a
66403ba3782SJens Axboe  * one-second gap.
66503ba3782SJens Axboe  *
66603ba3782SJens Axboe  * older_than_this takes precedence over nr_to_write.  So we'll only write back
66703ba3782SJens Axboe  * all dirty pages if they are all attached to "old" mappings.
66803ba3782SJens Axboe  */
66903ba3782SJens Axboe static long wb_writeback(struct bdi_writeback *wb, long nr_pages,
67003ba3782SJens Axboe 			 struct super_block *sb,
67103ba3782SJens Axboe 			 enum writeback_sync_modes sync_mode, int for_kupdate)
67203ba3782SJens Axboe {
67303ba3782SJens Axboe 	struct writeback_control wbc = {
67403ba3782SJens Axboe 		.bdi			= wb->bdi,
67503ba3782SJens Axboe 		.sb			= sb,
67603ba3782SJens Axboe 		.sync_mode		= sync_mode,
67703ba3782SJens Axboe 		.older_than_this	= NULL,
67803ba3782SJens Axboe 		.for_kupdate		= for_kupdate,
67903ba3782SJens Axboe 		.range_cyclic		= 1,
68003ba3782SJens Axboe 	};
68103ba3782SJens Axboe 	unsigned long oldest_jif;
68203ba3782SJens Axboe 	long wrote = 0;
68303ba3782SJens Axboe 
68403ba3782SJens Axboe 	if (wbc.for_kupdate) {
68503ba3782SJens Axboe 		wbc.older_than_this = &oldest_jif;
68603ba3782SJens Axboe 		oldest_jif = jiffies -
68703ba3782SJens Axboe 				msecs_to_jiffies(dirty_expire_interval * 10);
68803ba3782SJens Axboe 	}
68903ba3782SJens Axboe 
69003ba3782SJens Axboe 	for (;;) {
69103ba3782SJens Axboe 		/*
69203ba3782SJens Axboe 		 * Don't flush anything for non-integrity writeback where
69303ba3782SJens Axboe 		 * no nr_pages was given
69403ba3782SJens Axboe 		 */
69503ba3782SJens Axboe 		if (!for_kupdate && nr_pages <= 0 && sync_mode == WB_SYNC_NONE)
69603ba3782SJens Axboe 			break;
69703ba3782SJens Axboe 
69803ba3782SJens Axboe 		/*
69903ba3782SJens Axboe 		 * If no specific pages were given and this is just a
70003ba3782SJens Axboe 		 * periodic background writeout and we are below the
70103ba3782SJens Axboe 		 * background dirty threshold, don't do anything
70203ba3782SJens Axboe 		 */
70303ba3782SJens Axboe 		if (for_kupdate && nr_pages <= 0 && !over_bground_thresh())
70403ba3782SJens Axboe 			break;
70503ba3782SJens Axboe 
70603ba3782SJens Axboe 		wbc.more_io = 0;
70703ba3782SJens Axboe 		wbc.encountered_congestion = 0;
70803ba3782SJens Axboe 		wbc.nr_to_write = MAX_WRITEBACK_PAGES;
70903ba3782SJens Axboe 		wbc.pages_skipped = 0;
71003ba3782SJens Axboe 		writeback_inodes_wb(wb, &wbc);
71103ba3782SJens Axboe 		nr_pages -= MAX_WRITEBACK_PAGES - wbc.nr_to_write;
71203ba3782SJens Axboe 		wrote += MAX_WRITEBACK_PAGES - wbc.nr_to_write;
71303ba3782SJens Axboe 
71403ba3782SJens Axboe 		/*
71503ba3782SJens Axboe 		 * If we ran out of stuff to write, bail unless more_io got set
71603ba3782SJens Axboe 		 */
71703ba3782SJens Axboe 		if (wbc.nr_to_write > 0 || wbc.pages_skipped > 0) {
71803ba3782SJens Axboe 			if (wbc.more_io && !wbc.for_kupdate)
71903ba3782SJens Axboe 				continue;
72003ba3782SJens Axboe 			break;
72103ba3782SJens Axboe 		}
72203ba3782SJens Axboe 	}
72303ba3782SJens Axboe 
72403ba3782SJens Axboe 	return wrote;
72503ba3782SJens Axboe }
72603ba3782SJens Axboe 
72703ba3782SJens Axboe /*
72803ba3782SJens Axboe  * Return the next bdi_work struct that hasn't been processed by this
72903ba3782SJens Axboe  * wb thread yet
73003ba3782SJens Axboe  */
73103ba3782SJens Axboe static struct bdi_work *get_next_work_item(struct backing_dev_info *bdi,
73203ba3782SJens Axboe 					   struct bdi_writeback *wb)
73303ba3782SJens Axboe {
73403ba3782SJens Axboe 	struct bdi_work *work, *ret = NULL;
73503ba3782SJens Axboe 
73603ba3782SJens Axboe 	rcu_read_lock();
73703ba3782SJens Axboe 
73803ba3782SJens Axboe 	list_for_each_entry_rcu(work, &bdi->work_list, list) {
73903ba3782SJens Axboe 		if (!test_and_clear_bit(wb->nr, &work->seen))
74003ba3782SJens Axboe 			continue;
74103ba3782SJens Axboe 
74203ba3782SJens Axboe 		ret = work;
74303ba3782SJens Axboe 		break;
74403ba3782SJens Axboe 	}
74503ba3782SJens Axboe 
74603ba3782SJens Axboe 	rcu_read_unlock();
74703ba3782SJens Axboe 	return ret;
74803ba3782SJens Axboe }
74903ba3782SJens Axboe 
75003ba3782SJens Axboe static long wb_check_old_data_flush(struct bdi_writeback *wb)
75103ba3782SJens Axboe {
75203ba3782SJens Axboe 	unsigned long expired;
75303ba3782SJens Axboe 	long nr_pages;
75403ba3782SJens Axboe 
75503ba3782SJens Axboe 	expired = wb->last_old_flush +
75603ba3782SJens Axboe 			msecs_to_jiffies(dirty_writeback_interval * 10);
75703ba3782SJens Axboe 	if (time_before(jiffies, expired))
75803ba3782SJens Axboe 		return 0;
75903ba3782SJens Axboe 
76003ba3782SJens Axboe 	wb->last_old_flush = jiffies;
76103ba3782SJens Axboe 	nr_pages = global_page_state(NR_FILE_DIRTY) +
76203ba3782SJens Axboe 			global_page_state(NR_UNSTABLE_NFS) +
76303ba3782SJens Axboe 			(inodes_stat.nr_inodes - inodes_stat.nr_unused);
76403ba3782SJens Axboe 
76503ba3782SJens Axboe 	if (nr_pages)
76603ba3782SJens Axboe 		return wb_writeback(wb, nr_pages, NULL, WB_SYNC_NONE, 1);
76703ba3782SJens Axboe 
76803ba3782SJens Axboe 	return 0;
76903ba3782SJens Axboe }
77003ba3782SJens Axboe 
77103ba3782SJens Axboe /*
77203ba3782SJens Axboe  * Retrieve work items and do the writeback they describe
77303ba3782SJens Axboe  */
77403ba3782SJens Axboe long wb_do_writeback(struct bdi_writeback *wb, int force_wait)
77503ba3782SJens Axboe {
77603ba3782SJens Axboe 	struct backing_dev_info *bdi = wb->bdi;
77703ba3782SJens Axboe 	struct bdi_work *work;
77803ba3782SJens Axboe 	long nr_pages, wrote = 0;
77903ba3782SJens Axboe 
78003ba3782SJens Axboe 	while ((work = get_next_work_item(bdi, wb)) != NULL) {
78103ba3782SJens Axboe 		enum writeback_sync_modes sync_mode;
78203ba3782SJens Axboe 
78303ba3782SJens Axboe 		nr_pages = work->nr_pages;
78403ba3782SJens Axboe 
78503ba3782SJens Axboe 		/*
78603ba3782SJens Axboe 		 * Override sync mode, in case we must wait for completion
78703ba3782SJens Axboe 		 */
78803ba3782SJens Axboe 		if (force_wait)
78903ba3782SJens Axboe 			work->sync_mode = sync_mode = WB_SYNC_ALL;
79003ba3782SJens Axboe 		else
79103ba3782SJens Axboe 			sync_mode = work->sync_mode;
79203ba3782SJens Axboe 
79303ba3782SJens Axboe 		/*
79403ba3782SJens Axboe 		 * If this isn't a data integrity operation, just notify
79503ba3782SJens Axboe 		 * that we have seen this work and we are now starting it.
79603ba3782SJens Axboe 		 */
79703ba3782SJens Axboe 		if (sync_mode == WB_SYNC_NONE)
79803ba3782SJens Axboe 			wb_clear_pending(wb, work);
79903ba3782SJens Axboe 
80003ba3782SJens Axboe 		wrote += wb_writeback(wb, nr_pages, work->sb, sync_mode, 0);
80103ba3782SJens Axboe 
80203ba3782SJens Axboe 		/*
80303ba3782SJens Axboe 		 * This is a data integrity writeback, so only do the
80403ba3782SJens Axboe 		 * notification when we have completed the work.
80503ba3782SJens Axboe 		 */
80603ba3782SJens Axboe 		if (sync_mode == WB_SYNC_ALL)
80703ba3782SJens Axboe 			wb_clear_pending(wb, work);
80803ba3782SJens Axboe 	}
80903ba3782SJens Axboe 
81003ba3782SJens Axboe 	/*
81103ba3782SJens Axboe 	 * Check for periodic writeback, kupdated() style
81203ba3782SJens Axboe 	 */
81303ba3782SJens Axboe 	wrote += wb_check_old_data_flush(wb);
81403ba3782SJens Axboe 
81503ba3782SJens Axboe 	return wrote;
81603ba3782SJens Axboe }
81703ba3782SJens Axboe 
81803ba3782SJens Axboe /*
81903ba3782SJens Axboe  * Handle writeback of dirty data for the device backed by this bdi. Also
82003ba3782SJens Axboe  * wakes up periodically and does kupdated style flushing.
82103ba3782SJens Axboe  */
82203ba3782SJens Axboe int bdi_writeback_task(struct bdi_writeback *wb)
82303ba3782SJens Axboe {
82403ba3782SJens Axboe 	unsigned long last_active = jiffies;
82503ba3782SJens Axboe 	unsigned long wait_jiffies = -1UL;
82603ba3782SJens Axboe 	long pages_written;
82703ba3782SJens Axboe 
82803ba3782SJens Axboe 	while (!kthread_should_stop()) {
82903ba3782SJens Axboe 		pages_written = wb_do_writeback(wb, 0);
83003ba3782SJens Axboe 
83103ba3782SJens Axboe 		if (pages_written)
83203ba3782SJens Axboe 			last_active = jiffies;
83303ba3782SJens Axboe 		else if (wait_jiffies != -1UL) {
83403ba3782SJens Axboe 			unsigned long max_idle;
83503ba3782SJens Axboe 
83603ba3782SJens Axboe 			/*
83703ba3782SJens Axboe 			 * Longest period of inactivity that we tolerate. If we
83803ba3782SJens Axboe 			 * see dirty data again later, the task will get
83903ba3782SJens Axboe 			 * recreated automatically.
84003ba3782SJens Axboe 			 */
84103ba3782SJens Axboe 			max_idle = max(5UL * 60 * HZ, wait_jiffies);
84203ba3782SJens Axboe 			if (time_after(jiffies, max_idle + last_active))
84303ba3782SJens Axboe 				break;
84403ba3782SJens Axboe 		}
84503ba3782SJens Axboe 
84603ba3782SJens Axboe 		wait_jiffies = msecs_to_jiffies(dirty_writeback_interval * 10);
84703ba3782SJens Axboe 		set_current_state(TASK_INTERRUPTIBLE);
84803ba3782SJens Axboe 		schedule_timeout(wait_jiffies);
84903ba3782SJens Axboe 		try_to_freeze();
85003ba3782SJens Axboe 	}
85103ba3782SJens Axboe 
85203ba3782SJens Axboe 	return 0;
85303ba3782SJens Axboe }
85403ba3782SJens Axboe 
85503ba3782SJens Axboe /*
85603ba3782SJens Axboe  * Schedule writeback for all backing devices. Expensive! If this is a data
85703ba3782SJens Axboe  * integrity operation, writeback will be complete when this returns. If
85803ba3782SJens Axboe  * we are simply called for WB_SYNC_NONE, then writeback will merely be
85903ba3782SJens Axboe  * scheduled to run.
86003ba3782SJens Axboe  */
86103ba3782SJens Axboe static void bdi_writeback_all(struct writeback_control *wbc)
86203ba3782SJens Axboe {
86303ba3782SJens Axboe 	const bool must_wait = wbc->sync_mode == WB_SYNC_ALL;
86403ba3782SJens Axboe 	struct backing_dev_info *bdi;
86503ba3782SJens Axboe 	struct bdi_work *work;
86603ba3782SJens Axboe 	LIST_HEAD(list);
86703ba3782SJens Axboe 
86803ba3782SJens Axboe restart:
86903ba3782SJens Axboe 	spin_lock(&bdi_lock);
87003ba3782SJens Axboe 
87103ba3782SJens Axboe 	list_for_each_entry(bdi, &bdi_list, bdi_list) {
87203ba3782SJens Axboe 		struct bdi_work *work;
87303ba3782SJens Axboe 
87403ba3782SJens Axboe 		if (!bdi_has_dirty_io(bdi))
87503ba3782SJens Axboe 			continue;
87603ba3782SJens Axboe 
87703ba3782SJens Axboe 		/*
87803ba3782SJens Axboe 		 * If work allocation fails, do the writes inline. We drop
87903ba3782SJens Axboe 		 * the lock and restart the list writeout. This should be OK,
88003ba3782SJens Axboe 		 * since this happens rarely and because the writeout should
88103ba3782SJens Axboe 		 * eventually make more free memory available.
88203ba3782SJens Axboe 		 */
88303ba3782SJens Axboe 		work = bdi_alloc_work(wbc);
88403ba3782SJens Axboe 		if (!work) {
88503ba3782SJens Axboe 			struct writeback_control __wbc;
88603ba3782SJens Axboe 
88703ba3782SJens Axboe 			/*
88803ba3782SJens Axboe 			 * Not a data integrity writeout, just continue
88903ba3782SJens Axboe 			 */
89003ba3782SJens Axboe 			if (!must_wait)
89103ba3782SJens Axboe 				continue;
89203ba3782SJens Axboe 
89303ba3782SJens Axboe 			spin_unlock(&bdi_lock);
89403ba3782SJens Axboe 			__wbc = *wbc;
89503ba3782SJens Axboe 			__wbc.bdi = bdi;
89603ba3782SJens Axboe 			writeback_inodes_wbc(&__wbc);
89703ba3782SJens Axboe 			goto restart;
89803ba3782SJens Axboe 		}
89903ba3782SJens Axboe 		if (must_wait)
90003ba3782SJens Axboe 			list_add_tail(&work->wait_list, &list);
90103ba3782SJens Axboe 
90203ba3782SJens Axboe 		bdi_queue_work(bdi, work);
90303ba3782SJens Axboe 	}
90403ba3782SJens Axboe 
90503ba3782SJens Axboe 	spin_unlock(&bdi_lock);
90603ba3782SJens Axboe 
90703ba3782SJens Axboe 	/*
90803ba3782SJens Axboe 	 * If this is for WB_SYNC_ALL, wait for pending work to complete
90903ba3782SJens Axboe 	 * before returning.
91003ba3782SJens Axboe 	 */
91103ba3782SJens Axboe 	while (!list_empty(&list)) {
91203ba3782SJens Axboe 		work = list_entry(list.next, struct bdi_work, wait_list);
91303ba3782SJens Axboe 		list_del(&work->wait_list);
91403ba3782SJens Axboe 		bdi_wait_on_work_clear(work);
91503ba3782SJens Axboe 		call_rcu(&work->rcu_head, bdi_work_free);
91603ba3782SJens Axboe 	}
91703ba3782SJens Axboe }
91803ba3782SJens Axboe 
91903ba3782SJens Axboe /*
92003ba3782SJens Axboe  * Start writeback of `nr_pages' pages.  If `nr_pages' is zero, write back
92103ba3782SJens Axboe  * the whole world.
92203ba3782SJens Axboe  */
92303ba3782SJens Axboe void wakeup_flusher_threads(long nr_pages)
92403ba3782SJens Axboe {
92503ba3782SJens Axboe 	struct writeback_control wbc = {
92603ba3782SJens Axboe 		.sync_mode	= WB_SYNC_NONE,
92703ba3782SJens Axboe 		.older_than_this = NULL,
92803ba3782SJens Axboe 		.range_cyclic	= 1,
92903ba3782SJens Axboe 	};
93003ba3782SJens Axboe 
93103ba3782SJens Axboe 	if (nr_pages == 0)
93203ba3782SJens Axboe 		nr_pages = global_page_state(NR_FILE_DIRTY) +
93303ba3782SJens Axboe 				global_page_state(NR_UNSTABLE_NFS);
93403ba3782SJens Axboe 	wbc.nr_to_write = nr_pages;
93503ba3782SJens Axboe 	bdi_writeback_all(&wbc);
93603ba3782SJens Axboe }
93703ba3782SJens Axboe 
93803ba3782SJens Axboe static noinline void block_dump___mark_inode_dirty(struct inode *inode)
93903ba3782SJens Axboe {
94003ba3782SJens Axboe 	if (inode->i_ino || strcmp(inode->i_sb->s_id, "bdev")) {
94103ba3782SJens Axboe 		struct dentry *dentry;
94203ba3782SJens Axboe 		const char *name = "?";
94303ba3782SJens Axboe 
94403ba3782SJens Axboe 		dentry = d_find_alias(inode);
94503ba3782SJens Axboe 		if (dentry) {
94603ba3782SJens Axboe 			spin_lock(&dentry->d_lock);
94703ba3782SJens Axboe 			name = (const char *) dentry->d_name.name;
94803ba3782SJens Axboe 		}
94903ba3782SJens Axboe 		printk(KERN_DEBUG
95003ba3782SJens Axboe 		       "%s(%d): dirtied inode %lu (%s) on %s\n",
95103ba3782SJens Axboe 		       current->comm, task_pid_nr(current), inode->i_ino,
95203ba3782SJens Axboe 		       name, inode->i_sb->s_id);
95303ba3782SJens Axboe 		if (dentry) {
95403ba3782SJens Axboe 			spin_unlock(&dentry->d_lock);
95503ba3782SJens Axboe 			dput(dentry);
95603ba3782SJens Axboe 		}
95703ba3782SJens Axboe 	}
95803ba3782SJens Axboe }
95903ba3782SJens Axboe 
96003ba3782SJens Axboe /**
96103ba3782SJens Axboe  *	__mark_inode_dirty -	internal function
96203ba3782SJens Axboe  *	@inode: inode to mark
96303ba3782SJens Axboe  *	@flags: what kind of dirty (i.e. I_DIRTY_SYNC)
96403ba3782SJens Axboe  *	Mark an inode as dirty. Callers should use mark_inode_dirty or
96503ba3782SJens Axboe  *  	mark_inode_dirty_sync.
96603ba3782SJens Axboe  *
96703ba3782SJens Axboe  * Put the inode on the super block's dirty list.
96803ba3782SJens Axboe  *
96903ba3782SJens Axboe  * CAREFUL! We mark it dirty unconditionally, but move it onto the
97003ba3782SJens Axboe  * dirty list only if it is hashed or if it refers to a blockdev.
97103ba3782SJens Axboe  * If it was not hashed, it will never be added to the dirty list
97203ba3782SJens Axboe  * even if it is later hashed, as it will have been marked dirty already.
97303ba3782SJens Axboe  *
97403ba3782SJens Axboe  * In short, make sure you hash any inodes _before_ you start marking
97503ba3782SJens Axboe  * them dirty.
97603ba3782SJens Axboe  *
97703ba3782SJens Axboe  * This function *must* be atomic for the I_DIRTY_PAGES case -
97803ba3782SJens Axboe  * set_page_dirty() is called under spinlock in several places.
97903ba3782SJens Axboe  *
98003ba3782SJens Axboe  * Note that for blockdevs, inode->dirtied_when represents the dirtying time of
98103ba3782SJens Axboe  * the block-special inode (/dev/hda1) itself.  And the ->dirtied_when field of
98203ba3782SJens Axboe  * the kernel-internal blockdev inode represents the dirtying time of the
98303ba3782SJens Axboe  * blockdev's pages.  This is why for I_DIRTY_PAGES we always use
98403ba3782SJens Axboe  * page->mapping->host, so the page-dirtying time is recorded in the internal
98503ba3782SJens Axboe  * blockdev inode.
98603ba3782SJens Axboe  */
98703ba3782SJens Axboe void __mark_inode_dirty(struct inode *inode, int flags)
98803ba3782SJens Axboe {
98903ba3782SJens Axboe 	struct super_block *sb = inode->i_sb;
99003ba3782SJens Axboe 
99103ba3782SJens Axboe 	/*
99203ba3782SJens Axboe 	 * Don't do this for I_DIRTY_PAGES - that doesn't actually
99303ba3782SJens Axboe 	 * dirty the inode itself
99403ba3782SJens Axboe 	 */
99503ba3782SJens Axboe 	if (flags & (I_DIRTY_SYNC | I_DIRTY_DATASYNC)) {
99603ba3782SJens Axboe 		if (sb->s_op->dirty_inode)
99703ba3782SJens Axboe 			sb->s_op->dirty_inode(inode);
99803ba3782SJens Axboe 	}
99903ba3782SJens Axboe 
100003ba3782SJens Axboe 	/*
100103ba3782SJens Axboe 	 * make sure that changes are seen by all cpus before we test i_state
100203ba3782SJens Axboe 	 * -- mikulas
100303ba3782SJens Axboe 	 */
100403ba3782SJens Axboe 	smp_mb();
100503ba3782SJens Axboe 
100603ba3782SJens Axboe 	/* avoid the locking if we can */
100703ba3782SJens Axboe 	if ((inode->i_state & flags) == flags)
100803ba3782SJens Axboe 		return;
100903ba3782SJens Axboe 
101003ba3782SJens Axboe 	if (unlikely(block_dump))
101103ba3782SJens Axboe 		block_dump___mark_inode_dirty(inode);
101203ba3782SJens Axboe 
101303ba3782SJens Axboe 	spin_lock(&inode_lock);
101403ba3782SJens Axboe 	if ((inode->i_state & flags) != flags) {
101503ba3782SJens Axboe 		const int was_dirty = inode->i_state & I_DIRTY;
101603ba3782SJens Axboe 
101703ba3782SJens Axboe 		inode->i_state |= flags;
101803ba3782SJens Axboe 
101903ba3782SJens Axboe 		/*
102003ba3782SJens Axboe 		 * If the inode is being synced, just update its dirty state.
102103ba3782SJens Axboe 		 * The unlocker will place the inode on the appropriate
102203ba3782SJens Axboe 		 * superblock list, based upon its state.
102303ba3782SJens Axboe 		 */
102403ba3782SJens Axboe 		if (inode->i_state & I_SYNC)
102503ba3782SJens Axboe 			goto out;
102603ba3782SJens Axboe 
102703ba3782SJens Axboe 		/*
102803ba3782SJens Axboe 		 * Only add valid (hashed) inodes to the superblock's
102903ba3782SJens Axboe 		 * dirty list.  Add blockdev inodes as well.
103003ba3782SJens Axboe 		 */
103103ba3782SJens Axboe 		if (!S_ISBLK(inode->i_mode)) {
103203ba3782SJens Axboe 			if (hlist_unhashed(&inode->i_hash))
103303ba3782SJens Axboe 				goto out;
103403ba3782SJens Axboe 		}
103503ba3782SJens Axboe 		if (inode->i_state & (I_FREEING|I_CLEAR))
103603ba3782SJens Axboe 			goto out;
103703ba3782SJens Axboe 
103803ba3782SJens Axboe 		/*
103903ba3782SJens Axboe 		 * If the inode was already on b_dirty/b_io/b_more_io, don't
104003ba3782SJens Axboe 		 * reposition it (that would break b_dirty time-ordering).
104103ba3782SJens Axboe 		 */
104203ba3782SJens Axboe 		if (!was_dirty) {
104303ba3782SJens Axboe 			struct bdi_writeback *wb = &inode_to_bdi(inode)->wb;
104403ba3782SJens Axboe 
104503ba3782SJens Axboe 			inode->dirtied_when = jiffies;
104603ba3782SJens Axboe 			list_move(&inode->i_list, &wb->b_dirty);
104703ba3782SJens Axboe 		}
104803ba3782SJens Axboe 	}
104903ba3782SJens Axboe out:
105003ba3782SJens Axboe 	spin_unlock(&inode_lock);
105103ba3782SJens Axboe }
105203ba3782SJens Axboe EXPORT_SYMBOL(__mark_inode_dirty);
105303ba3782SJens Axboe 
105466f3b8e2SJens Axboe /*
105566f3b8e2SJens Axboe  * Write out a superblock's list of dirty inodes.  A wait will be performed
105666f3b8e2SJens Axboe  * upon no inodes, all inodes or the final one, depending upon sync_mode.
105766f3b8e2SJens Axboe  *
105866f3b8e2SJens Axboe  * If older_than_this is non-NULL, then only write out inodes which
105966f3b8e2SJens Axboe  * had their first dirtying at a time earlier than *older_than_this.
106066f3b8e2SJens Axboe  *
106166f3b8e2SJens Axboe  * If we're a pdlfush thread, then implement pdflush collision avoidance
106266f3b8e2SJens Axboe  * against the entire list.
106366f3b8e2SJens Axboe  *
106466f3b8e2SJens Axboe  * If `bdi' is non-zero then we're being asked to writeback a specific queue.
106566f3b8e2SJens Axboe  * This function assumes that the blockdev superblock's inodes are backed by
106666f3b8e2SJens Axboe  * a variety of queues, so all inodes are searched.  For other superblocks,
106766f3b8e2SJens Axboe  * assume that all inodes are backed by the same queue.
106866f3b8e2SJens Axboe  *
106966f3b8e2SJens Axboe  * The inodes to be written are parked on bdi->b_io.  They are moved back onto
107066f3b8e2SJens Axboe  * bdi->b_dirty as they are selected for writing.  This way, none can be missed
107166f3b8e2SJens Axboe  * on the writer throttling path, and we get decent balancing between many
107266f3b8e2SJens Axboe  * throttled threads: we don't want them all piling up on inode_sync_wait.
107366f3b8e2SJens Axboe  */
107403ba3782SJens Axboe static void wait_sb_inodes(struct writeback_control *wbc)
107566f3b8e2SJens Axboe {
107638f21977SNick Piggin 	struct inode *inode, *old_inode = NULL;
107738f21977SNick Piggin 
107803ba3782SJens Axboe 	/*
107903ba3782SJens Axboe 	 * We need to be protected against the filesystem going from
108003ba3782SJens Axboe 	 * r/o to r/w or vice versa.
108103ba3782SJens Axboe 	 */
108203ba3782SJens Axboe 	WARN_ON(!rwsem_is_locked(&wbc->sb->s_umount));
108303ba3782SJens Axboe 
108466f3b8e2SJens Axboe 	spin_lock(&inode_lock);
108566f3b8e2SJens Axboe 
108638f21977SNick Piggin 	/*
108738f21977SNick Piggin 	 * Data integrity sync. Must wait for all pages under writeback,
108838f21977SNick Piggin 	 * because there may have been pages dirtied before our sync
108938f21977SNick Piggin 	 * call, but which had writeout started before we write it out.
109038f21977SNick Piggin 	 * In which case, the inode may not be on the dirty list, but
109138f21977SNick Piggin 	 * we still have to wait for that writeout.
109238f21977SNick Piggin 	 */
109303ba3782SJens Axboe 	list_for_each_entry(inode, &wbc->sb->s_inodes, i_sb_list) {
109438f21977SNick Piggin 		struct address_space *mapping;
109538f21977SNick Piggin 
109603ba3782SJens Axboe 		if (inode->i_state & (I_FREEING|I_CLEAR|I_WILL_FREE|I_NEW))
109738f21977SNick Piggin 			continue;
109838f21977SNick Piggin 		mapping = inode->i_mapping;
109938f21977SNick Piggin 		if (mapping->nrpages == 0)
110038f21977SNick Piggin 			continue;
110138f21977SNick Piggin 		__iget(inode);
1102ae8547b0SHans Reiser 		spin_unlock(&inode_lock);
110338f21977SNick Piggin 		/*
110438f21977SNick Piggin 		 * We hold a reference to 'inode' so it couldn't have
110538f21977SNick Piggin 		 * been removed from s_inodes list while we dropped the
110638f21977SNick Piggin 		 * inode_lock.  We cannot iput the inode now as we can
110738f21977SNick Piggin 		 * be holding the last reference and we cannot iput it
110838f21977SNick Piggin 		 * under inode_lock. So we keep the reference and iput
110938f21977SNick Piggin 		 * it later.
111038f21977SNick Piggin 		 */
111138f21977SNick Piggin 		iput(old_inode);
111238f21977SNick Piggin 		old_inode = inode;
111338f21977SNick Piggin 
111438f21977SNick Piggin 		filemap_fdatawait(mapping);
111538f21977SNick Piggin 
111638f21977SNick Piggin 		cond_resched();
111738f21977SNick Piggin 
111838f21977SNick Piggin 		spin_lock(&inode_lock);
111938f21977SNick Piggin 	}
112038f21977SNick Piggin 	spin_unlock(&inode_lock);
112138f21977SNick Piggin 	iput(old_inode);
112266f3b8e2SJens Axboe }
11231da177e4SLinus Torvalds 
1124d8a8559cSJens Axboe /**
1125d8a8559cSJens Axboe  * writeback_inodes_sb	-	writeback dirty inodes from given super_block
1126d8a8559cSJens Axboe  * @sb: the superblock
11271da177e4SLinus Torvalds  *
1128d8a8559cSJens Axboe  * Start writeback on some inodes on this super_block. No guarantees are made
1129d8a8559cSJens Axboe  * on how many (if any) will be written, and this function does not wait
1130d8a8559cSJens Axboe  * for IO completion of submitted IO. The number of pages submitted is
1131d8a8559cSJens Axboe  * returned.
11321da177e4SLinus Torvalds  */
1133d8a8559cSJens Axboe long writeback_inodes_sb(struct super_block *sb)
11341da177e4SLinus Torvalds {
11351da177e4SLinus Torvalds 	struct writeback_control wbc = {
113603ba3782SJens Axboe 		.sb		= sb,
1137d8a8559cSJens Axboe 		.sync_mode	= WB_SYNC_NONE,
1138111ebb6eSOGAWA Hirofumi 		.range_start	= 0,
1139111ebb6eSOGAWA Hirofumi 		.range_end	= LLONG_MAX,
11401da177e4SLinus Torvalds 	};
1141b1e7a8fdSChristoph Lameter 	unsigned long nr_dirty = global_page_state(NR_FILE_DIRTY);
1142fd39fc85SChristoph Lameter 	unsigned long nr_unstable = global_page_state(NR_UNSTABLE_NFS);
1143d8a8559cSJens Axboe 	long nr_to_write;
11441da177e4SLinus Torvalds 
1145d8a8559cSJens Axboe 	nr_to_write = nr_dirty + nr_unstable +
114638f21977SNick Piggin 			(inodes_stat.nr_inodes - inodes_stat.nr_unused);
114738f21977SNick Piggin 
1148d8a8559cSJens Axboe 	wbc.nr_to_write = nr_to_write;
114903ba3782SJens Axboe 	bdi_writeback_all(&wbc);
1150d8a8559cSJens Axboe 	return nr_to_write - wbc.nr_to_write;
11511da177e4SLinus Torvalds }
1152d8a8559cSJens Axboe EXPORT_SYMBOL(writeback_inodes_sb);
1153d8a8559cSJens Axboe 
1154d8a8559cSJens Axboe /**
1155d8a8559cSJens Axboe  * sync_inodes_sb	-	sync sb inode pages
1156d8a8559cSJens Axboe  * @sb: the superblock
1157d8a8559cSJens Axboe  *
1158d8a8559cSJens Axboe  * This function writes and waits on any dirty inode belonging to this
1159d8a8559cSJens Axboe  * super_block. The number of pages synced is returned.
1160d8a8559cSJens Axboe  */
1161d8a8559cSJens Axboe long sync_inodes_sb(struct super_block *sb)
1162d8a8559cSJens Axboe {
1163d8a8559cSJens Axboe 	struct writeback_control wbc = {
116403ba3782SJens Axboe 		.sb		= sb,
1165d8a8559cSJens Axboe 		.sync_mode	= WB_SYNC_ALL,
1166d8a8559cSJens Axboe 		.range_start	= 0,
1167d8a8559cSJens Axboe 		.range_end	= LLONG_MAX,
1168d8a8559cSJens Axboe 	};
1169d8a8559cSJens Axboe 	long nr_to_write = LONG_MAX; /* doesn't actually matter */
1170d8a8559cSJens Axboe 
1171d8a8559cSJens Axboe 	wbc.nr_to_write = nr_to_write;
117203ba3782SJens Axboe 	bdi_writeback_all(&wbc);
117303ba3782SJens Axboe 	wait_sb_inodes(&wbc);
1174d8a8559cSJens Axboe 	return nr_to_write - wbc.nr_to_write;
1175d8a8559cSJens Axboe }
1176d8a8559cSJens Axboe EXPORT_SYMBOL(sync_inodes_sb);
11771da177e4SLinus Torvalds 
11781da177e4SLinus Torvalds /**
11791da177e4SLinus Torvalds  * write_inode_now	-	write an inode to disk
11801da177e4SLinus Torvalds  * @inode: inode to write to disk
11811da177e4SLinus Torvalds  * @sync: whether the write should be synchronous or not
11821da177e4SLinus Torvalds  *
11837f04c26dSAndrea Arcangeli  * This function commits an inode to disk immediately if it is dirty. This is
11847f04c26dSAndrea Arcangeli  * primarily needed by knfsd.
11857f04c26dSAndrea Arcangeli  *
11867f04c26dSAndrea Arcangeli  * The caller must either have a ref on the inode or must have set I_WILL_FREE.
11871da177e4SLinus Torvalds  */
11881da177e4SLinus Torvalds int write_inode_now(struct inode *inode, int sync)
11891da177e4SLinus Torvalds {
11901da177e4SLinus Torvalds 	int ret;
11911da177e4SLinus Torvalds 	struct writeback_control wbc = {
11921da177e4SLinus Torvalds 		.nr_to_write = LONG_MAX,
119318914b18SMike Galbraith 		.sync_mode = sync ? WB_SYNC_ALL : WB_SYNC_NONE,
1194111ebb6eSOGAWA Hirofumi 		.range_start = 0,
1195111ebb6eSOGAWA Hirofumi 		.range_end = LLONG_MAX,
11961da177e4SLinus Torvalds 	};
11971da177e4SLinus Torvalds 
11981da177e4SLinus Torvalds 	if (!mapping_cap_writeback_dirty(inode->i_mapping))
119949364ce2SAndrew Morton 		wbc.nr_to_write = 0;
12001da177e4SLinus Torvalds 
12011da177e4SLinus Torvalds 	might_sleep();
12021da177e4SLinus Torvalds 	spin_lock(&inode_lock);
120301c03194SChristoph Hellwig 	ret = writeback_single_inode(inode, &wbc);
12041da177e4SLinus Torvalds 	spin_unlock(&inode_lock);
12051da177e4SLinus Torvalds 	if (sync)
12061c0eeaf5SJoern Engel 		inode_sync_wait(inode);
12071da177e4SLinus Torvalds 	return ret;
12081da177e4SLinus Torvalds }
12091da177e4SLinus Torvalds EXPORT_SYMBOL(write_inode_now);
12101da177e4SLinus Torvalds 
12111da177e4SLinus Torvalds /**
12121da177e4SLinus Torvalds  * sync_inode - write an inode and its pages to disk.
12131da177e4SLinus Torvalds  * @inode: the inode to sync
12141da177e4SLinus Torvalds  * @wbc: controls the writeback mode
12151da177e4SLinus Torvalds  *
12161da177e4SLinus Torvalds  * sync_inode() will write an inode and its pages to disk.  It will also
12171da177e4SLinus Torvalds  * correctly update the inode on its superblock's dirty inode lists and will
12181da177e4SLinus Torvalds  * update inode->i_state.
12191da177e4SLinus Torvalds  *
12201da177e4SLinus Torvalds  * The caller must have a ref on the inode.
12211da177e4SLinus Torvalds  */
12221da177e4SLinus Torvalds int sync_inode(struct inode *inode, struct writeback_control *wbc)
12231da177e4SLinus Torvalds {
12241da177e4SLinus Torvalds 	int ret;
12251da177e4SLinus Torvalds 
12261da177e4SLinus Torvalds 	spin_lock(&inode_lock);
122701c03194SChristoph Hellwig 	ret = writeback_single_inode(inode, wbc);
12281da177e4SLinus Torvalds 	spin_unlock(&inode_lock);
12291da177e4SLinus Torvalds 	return ret;
12301da177e4SLinus Torvalds }
12311da177e4SLinus Torvalds EXPORT_SYMBOL(sync_inode);
12321da177e4SLinus Torvalds 
12331da177e4SLinus Torvalds /**
12341da177e4SLinus Torvalds  * generic_osync_inode - flush all dirty data for a given inode to disk
12351da177e4SLinus Torvalds  * @inode: inode to write
123667be2dd1SMartin Waitz  * @mapping: the address_space that should be flushed
12371da177e4SLinus Torvalds  * @what:  what to write and wait upon
12381da177e4SLinus Torvalds  *
12391da177e4SLinus Torvalds  * This can be called by file_write functions for files which have the
12401da177e4SLinus Torvalds  * O_SYNC flag set, to flush dirty writes to disk.
12411da177e4SLinus Torvalds  *
12421da177e4SLinus Torvalds  * @what is a bitmask, specifying which part of the inode's data should be
1243b8887e6eSRandy Dunlap  * written and waited upon.
12441da177e4SLinus Torvalds  *
12451da177e4SLinus Torvalds  *    OSYNC_DATA:     i_mapping's dirty data
12461da177e4SLinus Torvalds  *    OSYNC_METADATA: the buffers at i_mapping->private_list
12471da177e4SLinus Torvalds  *    OSYNC_INODE:    the inode itself
12481da177e4SLinus Torvalds  */
12491da177e4SLinus Torvalds 
12501da177e4SLinus Torvalds int generic_osync_inode(struct inode *inode, struct address_space *mapping, int what)
12511da177e4SLinus Torvalds {
12521da177e4SLinus Torvalds 	int err = 0;
12531da177e4SLinus Torvalds 	int need_write_inode_now = 0;
12541da177e4SLinus Torvalds 	int err2;
12551da177e4SLinus Torvalds 
12561da177e4SLinus Torvalds 	if (what & OSYNC_DATA)
12571da177e4SLinus Torvalds 		err = filemap_fdatawrite(mapping);
12581da177e4SLinus Torvalds 	if (what & (OSYNC_METADATA|OSYNC_DATA)) {
12591da177e4SLinus Torvalds 		err2 = sync_mapping_buffers(mapping);
12601da177e4SLinus Torvalds 		if (!err)
12611da177e4SLinus Torvalds 			err = err2;
12621da177e4SLinus Torvalds 	}
12631da177e4SLinus Torvalds 	if (what & OSYNC_DATA) {
12641da177e4SLinus Torvalds 		err2 = filemap_fdatawait(mapping);
12651da177e4SLinus Torvalds 		if (!err)
12661da177e4SLinus Torvalds 			err = err2;
12671da177e4SLinus Torvalds 	}
12681da177e4SLinus Torvalds 
12691da177e4SLinus Torvalds 	spin_lock(&inode_lock);
12701da177e4SLinus Torvalds 	if ((inode->i_state & I_DIRTY) &&
12711da177e4SLinus Torvalds 	    ((what & OSYNC_INODE) || (inode->i_state & I_DIRTY_DATASYNC)))
12721da177e4SLinus Torvalds 		need_write_inode_now = 1;
12731da177e4SLinus Torvalds 	spin_unlock(&inode_lock);
12741da177e4SLinus Torvalds 
12751da177e4SLinus Torvalds 	if (need_write_inode_now) {
12761da177e4SLinus Torvalds 		err2 = write_inode_now(inode, 1);
12771da177e4SLinus Torvalds 		if (!err)
12781da177e4SLinus Torvalds 			err = err2;
12791da177e4SLinus Torvalds 	}
12801da177e4SLinus Torvalds 	else
12811c0eeaf5SJoern Engel 		inode_sync_wait(inode);
12821da177e4SLinus Torvalds 
12831da177e4SLinus Torvalds 	return err;
12841da177e4SLinus Torvalds }
12851da177e4SLinus Torvalds EXPORT_SYMBOL(generic_osync_inode);
1286