xref: /openbmc/linux/fs/fs-writeback.c (revision 495a276e)
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>
17630d9c47SPaul Gortmaker #include <linux/export.h>
181da177e4SLinus Torvalds #include <linux/spinlock.h>
195a0e3ad6STejun Heo #include <linux/slab.h>
201da177e4SLinus Torvalds #include <linux/sched.h>
211da177e4SLinus Torvalds #include <linux/fs.h>
221da177e4SLinus Torvalds #include <linux/mm.h>
23bc31b86aSWu Fengguang #include <linux/pagemap.h>
2403ba3782SJens Axboe #include <linux/kthread.h>
251da177e4SLinus Torvalds #include <linux/writeback.h>
261da177e4SLinus Torvalds #include <linux/blkdev.h>
271da177e4SLinus Torvalds #include <linux/backing-dev.h>
28455b2864SDave Chinner #include <linux/tracepoint.h>
29719ea2fbSAl Viro #include <linux/device.h>
3007f3f05cSDavid Howells #include "internal.h"
311da177e4SLinus Torvalds 
32d0bceac7SJens Axboe /*
33bc31b86aSWu Fengguang  * 4MB minimal write chunk size
34bc31b86aSWu Fengguang  */
35bc31b86aSWu Fengguang #define MIN_WRITEBACK_PAGES	(4096UL >> (PAGE_CACHE_SHIFT - 10))
36bc31b86aSWu Fengguang 
37bc31b86aSWu Fengguang /*
38c4a77a6cSJens Axboe  * Passed into wb_writeback(), essentially a subset of writeback_control
39c4a77a6cSJens Axboe  */
4083ba7b07SChristoph Hellwig struct wb_writeback_work {
41c4a77a6cSJens Axboe 	long nr_pages;
42c4a77a6cSJens Axboe 	struct super_block *sb;
430dc83bd3SJan Kara 	unsigned long *older_than_this;
44c4a77a6cSJens Axboe 	enum writeback_sync_modes sync_mode;
456e6938b6SWu Fengguang 	unsigned int tagged_writepages:1;
4652957fe1SH Hartley Sweeten 	unsigned int for_kupdate:1;
4752957fe1SH Hartley Sweeten 	unsigned int range_cyclic:1;
4852957fe1SH Hartley Sweeten 	unsigned int for_background:1;
497747bd4bSDave Chinner 	unsigned int for_sync:1;	/* sync(2) WB_SYNC_ALL writeback */
500e175a18SCurt Wohlgemuth 	enum wb_reason reason;		/* why was writeback initiated? */
51c4a77a6cSJens Axboe 
528010c3b6SJens Axboe 	struct list_head list;		/* pending work list */
5383ba7b07SChristoph Hellwig 	struct completion *done;	/* set if the caller waits */
5403ba3782SJens Axboe };
5503ba3782SJens Axboe 
56f11b00f3SAdrian Bunk /**
57f11b00f3SAdrian Bunk  * writeback_in_progress - determine whether there is writeback in progress
58f11b00f3SAdrian Bunk  * @bdi: the device's backing_dev_info structure.
59f11b00f3SAdrian Bunk  *
6003ba3782SJens Axboe  * Determine whether there is writeback waiting to be handled against a
6103ba3782SJens Axboe  * backing device.
62f11b00f3SAdrian Bunk  */
63f11b00f3SAdrian Bunk int writeback_in_progress(struct backing_dev_info *bdi)
64f11b00f3SAdrian Bunk {
6581d73a32SJan Kara 	return test_bit(BDI_writeback_running, &bdi->state);
66f11b00f3SAdrian Bunk }
6700d4e736STheodore Ts'o EXPORT_SYMBOL(writeback_in_progress);
68f11b00f3SAdrian Bunk 
69692ebd17SJan Kara static inline struct backing_dev_info *inode_to_bdi(struct inode *inode)
70692ebd17SJan Kara {
71692ebd17SJan Kara 	struct super_block *sb = inode->i_sb;
72495a276eSChristoph Hellwig #ifdef CONFIG_BLOCK
73a8855990SJan Kara 	if (sb_is_blkdev_sb(sb))
74495a276eSChristoph Hellwig 		return blk_get_backing_dev_info(I_BDEV(inode));
75495a276eSChristoph Hellwig #endif
76692ebd17SJan Kara 	return sb->s_bdi;
77692ebd17SJan Kara }
78692ebd17SJan Kara 
797ccf19a8SNick Piggin static inline struct inode *wb_inode(struct list_head *head)
807ccf19a8SNick Piggin {
817ccf19a8SNick Piggin 	return list_entry(head, struct inode, i_wb_list);
827ccf19a8SNick Piggin }
837ccf19a8SNick Piggin 
8415eb77a0SWu Fengguang /*
8515eb77a0SWu Fengguang  * Include the creation of the trace points after defining the
8615eb77a0SWu Fengguang  * wb_writeback_work structure and inline functions so that the definition
8715eb77a0SWu Fengguang  * remains local to this file.
8815eb77a0SWu Fengguang  */
8915eb77a0SWu Fengguang #define CREATE_TRACE_POINTS
9015eb77a0SWu Fengguang #include <trace/events/writeback.h>
9115eb77a0SWu Fengguang 
92774016b2SSteven Whitehouse EXPORT_TRACEPOINT_SYMBOL_GPL(wbc_writepage);
93774016b2SSteven Whitehouse 
945acda9d1SJan Kara static void bdi_wakeup_thread(struct backing_dev_info *bdi)
955acda9d1SJan Kara {
965acda9d1SJan Kara 	spin_lock_bh(&bdi->wb_lock);
975acda9d1SJan Kara 	if (test_bit(BDI_registered, &bdi->state))
985acda9d1SJan Kara 		mod_delayed_work(bdi_wq, &bdi->wb.dwork, 0);
995acda9d1SJan Kara 	spin_unlock_bh(&bdi->wb_lock);
1005acda9d1SJan Kara }
1015acda9d1SJan Kara 
1026585027aSJan Kara static void bdi_queue_work(struct backing_dev_info *bdi,
1036585027aSJan Kara 			   struct wb_writeback_work *work)
1046585027aSJan Kara {
1056585027aSJan Kara 	trace_writeback_queue(bdi, work);
1066585027aSJan Kara 
1076585027aSJan Kara 	spin_lock_bh(&bdi->wb_lock);
1085acda9d1SJan Kara 	if (!test_bit(BDI_registered, &bdi->state)) {
1095acda9d1SJan Kara 		if (work->done)
1105acda9d1SJan Kara 			complete(work->done);
1115acda9d1SJan Kara 		goto out_unlock;
1125acda9d1SJan Kara 	}
1136585027aSJan Kara 	list_add_tail(&work->list, &bdi->work_list);
114839a8e86STejun Heo 	mod_delayed_work(bdi_wq, &bdi->wb.dwork, 0);
1155acda9d1SJan Kara out_unlock:
1165acda9d1SJan Kara 	spin_unlock_bh(&bdi->wb_lock);
11703ba3782SJens Axboe }
1181da177e4SLinus Torvalds 
11983ba7b07SChristoph Hellwig static void
12083ba7b07SChristoph Hellwig __bdi_start_writeback(struct backing_dev_info *bdi, long nr_pages,
1210e175a18SCurt Wohlgemuth 		      bool range_cyclic, enum wb_reason reason)
1221da177e4SLinus Torvalds {
12383ba7b07SChristoph Hellwig 	struct wb_writeback_work *work;
12403ba3782SJens Axboe 
125bcddc3f0SJens Axboe 	/*
126bcddc3f0SJens Axboe 	 * This is WB_SYNC_NONE writeback, so if allocation fails just
127bcddc3f0SJens Axboe 	 * wakeup the thread for old dirty data writeback
128bcddc3f0SJens Axboe 	 */
12983ba7b07SChristoph Hellwig 	work = kzalloc(sizeof(*work), GFP_ATOMIC);
13083ba7b07SChristoph Hellwig 	if (!work) {
131455b2864SDave Chinner 		trace_writeback_nowork(bdi);
1325acda9d1SJan Kara 		bdi_wakeup_thread(bdi);
13383ba7b07SChristoph Hellwig 		return;
13483ba7b07SChristoph Hellwig 	}
13583ba7b07SChristoph Hellwig 
13683ba7b07SChristoph Hellwig 	work->sync_mode	= WB_SYNC_NONE;
13783ba7b07SChristoph Hellwig 	work->nr_pages	= nr_pages;
13883ba7b07SChristoph Hellwig 	work->range_cyclic = range_cyclic;
1390e175a18SCurt Wohlgemuth 	work->reason	= reason;
14083ba7b07SChristoph Hellwig 
141f11fcae8SJens Axboe 	bdi_queue_work(bdi, work);
14203ba3782SJens Axboe }
143b6e51316SJens Axboe 
144b6e51316SJens Axboe /**
145b6e51316SJens Axboe  * bdi_start_writeback - start writeback
146b6e51316SJens Axboe  * @bdi: the backing device to write from
147b6e51316SJens Axboe  * @nr_pages: the number of pages to write
148786228abSMarcos Paulo de Souza  * @reason: reason why some writeback work was initiated
149b6e51316SJens Axboe  *
150b6e51316SJens Axboe  * Description:
151b6e51316SJens Axboe  *   This does WB_SYNC_NONE opportunistic writeback. The IO is only
15225985edcSLucas De Marchi  *   started when this function returns, we make no guarantees on
1530e3c9a22SJens Axboe  *   completion. Caller need not hold sb s_umount semaphore.
154b6e51316SJens Axboe  *
155b6e51316SJens Axboe  */
1560e175a18SCurt Wohlgemuth void bdi_start_writeback(struct backing_dev_info *bdi, long nr_pages,
1570e175a18SCurt Wohlgemuth 			enum wb_reason reason)
158b6e51316SJens Axboe {
1590e175a18SCurt Wohlgemuth 	__bdi_start_writeback(bdi, nr_pages, true, reason);
160d3ddec76SWu Fengguang }
161d3ddec76SWu Fengguang 
162c5444198SChristoph Hellwig /**
163c5444198SChristoph Hellwig  * bdi_start_background_writeback - start background writeback
164c5444198SChristoph Hellwig  * @bdi: the backing device to write from
165c5444198SChristoph Hellwig  *
166c5444198SChristoph Hellwig  * Description:
1676585027aSJan Kara  *   This makes sure WB_SYNC_NONE background writeback happens. When
1686585027aSJan Kara  *   this function returns, it is only guaranteed that for given BDI
1696585027aSJan Kara  *   some IO is happening if we are over background dirty threshold.
1706585027aSJan Kara  *   Caller need not hold sb s_umount semaphore.
171c5444198SChristoph Hellwig  */
172c5444198SChristoph Hellwig void bdi_start_background_writeback(struct backing_dev_info *bdi)
173c5444198SChristoph Hellwig {
1746585027aSJan Kara 	/*
1756585027aSJan Kara 	 * We just wake up the flusher thread. It will perform background
1766585027aSJan Kara 	 * writeback as soon as there is no other work to do.
1776585027aSJan Kara 	 */
17871927e84SWu Fengguang 	trace_writeback_wake_background(bdi);
1795acda9d1SJan Kara 	bdi_wakeup_thread(bdi);
1801da177e4SLinus Torvalds }
1811da177e4SLinus Torvalds 
1821da177e4SLinus Torvalds /*
183a66979abSDave Chinner  * Remove the inode from the writeback list it is on.
184a66979abSDave Chinner  */
185a66979abSDave Chinner void inode_wb_list_del(struct inode *inode)
186a66979abSDave Chinner {
187f758eeabSChristoph Hellwig 	struct backing_dev_info *bdi = inode_to_bdi(inode);
188a66979abSDave Chinner 
189f758eeabSChristoph Hellwig 	spin_lock(&bdi->wb.list_lock);
190f758eeabSChristoph Hellwig 	list_del_init(&inode->i_wb_list);
191f758eeabSChristoph Hellwig 	spin_unlock(&bdi->wb.list_lock);
192f758eeabSChristoph Hellwig }
193a66979abSDave Chinner 
194a66979abSDave Chinner /*
1956610a0bcSAndrew Morton  * Redirty an inode: set its when-it-was dirtied timestamp and move it to the
1966610a0bcSAndrew Morton  * furthest end of its superblock's dirty-inode list.
1976610a0bcSAndrew Morton  *
1986610a0bcSAndrew Morton  * Before stamping the inode's ->dirtied_when, we check to see whether it is
19966f3b8e2SJens Axboe  * already the most-recently-dirtied inode on the b_dirty list.  If that is
2006610a0bcSAndrew Morton  * the case then the inode must have been redirtied while it was being written
2016610a0bcSAndrew Morton  * out and we don't reset its dirtied_when.
2026610a0bcSAndrew Morton  */
203f758eeabSChristoph Hellwig static void redirty_tail(struct inode *inode, struct bdi_writeback *wb)
2046610a0bcSAndrew Morton {
205f758eeabSChristoph Hellwig 	assert_spin_locked(&wb->list_lock);
20603ba3782SJens Axboe 	if (!list_empty(&wb->b_dirty)) {
20766f3b8e2SJens Axboe 		struct inode *tail;
2086610a0bcSAndrew Morton 
2097ccf19a8SNick Piggin 		tail = wb_inode(wb->b_dirty.next);
21066f3b8e2SJens Axboe 		if (time_before(inode->dirtied_when, tail->dirtied_when))
2116610a0bcSAndrew Morton 			inode->dirtied_when = jiffies;
2126610a0bcSAndrew Morton 	}
2137ccf19a8SNick Piggin 	list_move(&inode->i_wb_list, &wb->b_dirty);
2146610a0bcSAndrew Morton }
2156610a0bcSAndrew Morton 
2166610a0bcSAndrew Morton /*
21766f3b8e2SJens Axboe  * requeue inode for re-scanning after bdi->b_io list is exhausted.
218c986d1e2SAndrew Morton  */
219f758eeabSChristoph Hellwig static void requeue_io(struct inode *inode, struct bdi_writeback *wb)
220c986d1e2SAndrew Morton {
221f758eeabSChristoph Hellwig 	assert_spin_locked(&wb->list_lock);
2227ccf19a8SNick Piggin 	list_move(&inode->i_wb_list, &wb->b_more_io);
223c986d1e2SAndrew Morton }
224c986d1e2SAndrew Morton 
2251c0eeaf5SJoern Engel static void inode_sync_complete(struct inode *inode)
2261c0eeaf5SJoern Engel {
227365b94aeSJan Kara 	inode->i_state &= ~I_SYNC;
2284eff96ddSJan Kara 	/* If inode is clean an unused, put it into LRU now... */
2294eff96ddSJan Kara 	inode_add_lru(inode);
230365b94aeSJan Kara 	/* Waiters must see I_SYNC cleared before being woken up */
2311c0eeaf5SJoern Engel 	smp_mb();
2321c0eeaf5SJoern Engel 	wake_up_bit(&inode->i_state, __I_SYNC);
2331c0eeaf5SJoern Engel }
2341c0eeaf5SJoern Engel 
235d2caa3c5SJeff Layton static bool inode_dirtied_after(struct inode *inode, unsigned long t)
236d2caa3c5SJeff Layton {
237d2caa3c5SJeff Layton 	bool ret = time_after(inode->dirtied_when, t);
238d2caa3c5SJeff Layton #ifndef CONFIG_64BIT
239d2caa3c5SJeff Layton 	/*
240d2caa3c5SJeff Layton 	 * For inodes being constantly redirtied, dirtied_when can get stuck.
241d2caa3c5SJeff Layton 	 * It _appears_ to be in the future, but is actually in distant past.
242d2caa3c5SJeff Layton 	 * This test is necessary to prevent such wrapped-around relative times
2435b0830cbSJens Axboe 	 * from permanently stopping the whole bdi writeback.
244d2caa3c5SJeff Layton 	 */
245d2caa3c5SJeff Layton 	ret = ret && time_before_eq(inode->dirtied_when, jiffies);
246d2caa3c5SJeff Layton #endif
247d2caa3c5SJeff Layton 	return ret;
248d2caa3c5SJeff Layton }
249d2caa3c5SJeff Layton 
250c986d1e2SAndrew Morton /*
2510e2f2b23SWang Sheng-Hui  * Move expired (dirtied before work->older_than_this) dirty inodes from
252697e6fedSJan Kara  * @delaying_queue to @dispatch_queue.
2532c136579SFengguang Wu  */
254e84d0a4fSWu Fengguang static int move_expired_inodes(struct list_head *delaying_queue,
2552c136579SFengguang Wu 			       struct list_head *dispatch_queue,
256ad4e38ddSCurt Wohlgemuth 			       struct wb_writeback_work *work)
2572c136579SFengguang Wu {
2585c03449dSShaohua Li 	LIST_HEAD(tmp);
2595c03449dSShaohua Li 	struct list_head *pos, *node;
260cf137307SJens Axboe 	struct super_block *sb = NULL;
2615c03449dSShaohua Li 	struct inode *inode;
262cf137307SJens Axboe 	int do_sb_sort = 0;
263e84d0a4fSWu Fengguang 	int moved = 0;
2645c03449dSShaohua Li 
2652c136579SFengguang Wu 	while (!list_empty(delaying_queue)) {
2667ccf19a8SNick Piggin 		inode = wb_inode(delaying_queue->prev);
2670dc83bd3SJan Kara 		if (work->older_than_this &&
2680dc83bd3SJan Kara 		    inode_dirtied_after(inode, *work->older_than_this))
2692c136579SFengguang Wu 			break;
270a8855990SJan Kara 		list_move(&inode->i_wb_list, &tmp);
271a8855990SJan Kara 		moved++;
272a8855990SJan Kara 		if (sb_is_blkdev_sb(inode->i_sb))
273a8855990SJan Kara 			continue;
274cf137307SJens Axboe 		if (sb && sb != inode->i_sb)
275cf137307SJens Axboe 			do_sb_sort = 1;
276cf137307SJens Axboe 		sb = inode->i_sb;
2775c03449dSShaohua Li 	}
2785c03449dSShaohua Li 
279cf137307SJens Axboe 	/* just one sb in list, splice to dispatch_queue and we're done */
280cf137307SJens Axboe 	if (!do_sb_sort) {
281cf137307SJens Axboe 		list_splice(&tmp, dispatch_queue);
282e84d0a4fSWu Fengguang 		goto out;
283cf137307SJens Axboe 	}
284cf137307SJens Axboe 
2855c03449dSShaohua Li 	/* Move inodes from one superblock together */
2865c03449dSShaohua Li 	while (!list_empty(&tmp)) {
2877ccf19a8SNick Piggin 		sb = wb_inode(tmp.prev)->i_sb;
2885c03449dSShaohua Li 		list_for_each_prev_safe(pos, node, &tmp) {
2897ccf19a8SNick Piggin 			inode = wb_inode(pos);
2905c03449dSShaohua Li 			if (inode->i_sb == sb)
2917ccf19a8SNick Piggin 				list_move(&inode->i_wb_list, dispatch_queue);
2922c136579SFengguang Wu 		}
2932c136579SFengguang Wu 	}
294e84d0a4fSWu Fengguang out:
295e84d0a4fSWu Fengguang 	return moved;
2965c03449dSShaohua Li }
2972c136579SFengguang Wu 
2982c136579SFengguang Wu /*
2992c136579SFengguang Wu  * Queue all expired dirty inodes for io, eldest first.
3004ea879b9SWu Fengguang  * Before
3014ea879b9SWu Fengguang  *         newly dirtied     b_dirty    b_io    b_more_io
3024ea879b9SWu Fengguang  *         =============>    gf         edc     BA
3034ea879b9SWu Fengguang  * After
3044ea879b9SWu Fengguang  *         newly dirtied     b_dirty    b_io    b_more_io
3054ea879b9SWu Fengguang  *         =============>    g          fBAedc
3064ea879b9SWu Fengguang  *                                           |
3074ea879b9SWu Fengguang  *                                           +--> dequeue for IO
3082c136579SFengguang Wu  */
309ad4e38ddSCurt Wohlgemuth static void queue_io(struct bdi_writeback *wb, struct wb_writeback_work *work)
3102c136579SFengguang Wu {
311e84d0a4fSWu Fengguang 	int moved;
312f758eeabSChristoph Hellwig 	assert_spin_locked(&wb->list_lock);
3134ea879b9SWu Fengguang 	list_splice_init(&wb->b_more_io, &wb->b_io);
314ad4e38ddSCurt Wohlgemuth 	moved = move_expired_inodes(&wb->b_dirty, &wb->b_io, work);
315ad4e38ddSCurt Wohlgemuth 	trace_writeback_queue_io(wb, work, moved);
31666f3b8e2SJens Axboe }
31766f3b8e2SJens Axboe 
318a9185b41SChristoph Hellwig static int write_inode(struct inode *inode, struct writeback_control *wbc)
31966f3b8e2SJens Axboe {
3209fb0a7daSTejun Heo 	int ret;
3219fb0a7daSTejun Heo 
3229fb0a7daSTejun Heo 	if (inode->i_sb->s_op->write_inode && !is_bad_inode(inode)) {
3239fb0a7daSTejun Heo 		trace_writeback_write_inode_start(inode, wbc);
3249fb0a7daSTejun Heo 		ret = inode->i_sb->s_op->write_inode(inode, wbc);
3259fb0a7daSTejun Heo 		trace_writeback_write_inode(inode, wbc);
3269fb0a7daSTejun Heo 		return ret;
3279fb0a7daSTejun Heo 	}
32803ba3782SJens Axboe 	return 0;
32966f3b8e2SJens Axboe }
33008d8e974SFengguang Wu 
3312c136579SFengguang Wu /*
332169ebd90SJan Kara  * Wait for writeback on an inode to complete. Called with i_lock held.
333169ebd90SJan Kara  * Caller must make sure inode cannot go away when we drop i_lock.
33401c03194SChristoph Hellwig  */
335169ebd90SJan Kara static void __inode_wait_for_writeback(struct inode *inode)
336169ebd90SJan Kara 	__releases(inode->i_lock)
337169ebd90SJan Kara 	__acquires(inode->i_lock)
33801c03194SChristoph Hellwig {
33901c03194SChristoph Hellwig 	DEFINE_WAIT_BIT(wq, &inode->i_state, __I_SYNC);
34001c03194SChristoph Hellwig 	wait_queue_head_t *wqh;
34101c03194SChristoph Hellwig 
34201c03194SChristoph Hellwig 	wqh = bit_waitqueue(&inode->i_state, __I_SYNC);
34358a9d3d8SRichard Kennedy 	while (inode->i_state & I_SYNC) {
344250df6edSDave Chinner 		spin_unlock(&inode->i_lock);
34574316201SNeilBrown 		__wait_on_bit(wqh, &wq, bit_wait,
34674316201SNeilBrown 			      TASK_UNINTERRUPTIBLE);
347250df6edSDave Chinner 		spin_lock(&inode->i_lock);
34858a9d3d8SRichard Kennedy 	}
34901c03194SChristoph Hellwig }
35001c03194SChristoph Hellwig 
35101c03194SChristoph Hellwig /*
352169ebd90SJan Kara  * Wait for writeback on an inode to complete. Caller must have inode pinned.
353169ebd90SJan Kara  */
354169ebd90SJan Kara void inode_wait_for_writeback(struct inode *inode)
355169ebd90SJan Kara {
356169ebd90SJan Kara 	spin_lock(&inode->i_lock);
357169ebd90SJan Kara 	__inode_wait_for_writeback(inode);
358169ebd90SJan Kara 	spin_unlock(&inode->i_lock);
359169ebd90SJan Kara }
360169ebd90SJan Kara 
361169ebd90SJan Kara /*
362169ebd90SJan Kara  * Sleep until I_SYNC is cleared. This function must be called with i_lock
363169ebd90SJan Kara  * held and drops it. It is aimed for callers not holding any inode reference
364169ebd90SJan Kara  * so once i_lock is dropped, inode can go away.
365169ebd90SJan Kara  */
366169ebd90SJan Kara static void inode_sleep_on_writeback(struct inode *inode)
367169ebd90SJan Kara 	__releases(inode->i_lock)
368169ebd90SJan Kara {
369169ebd90SJan Kara 	DEFINE_WAIT(wait);
370169ebd90SJan Kara 	wait_queue_head_t *wqh = bit_waitqueue(&inode->i_state, __I_SYNC);
371169ebd90SJan Kara 	int sleep;
372169ebd90SJan Kara 
373169ebd90SJan Kara 	prepare_to_wait(wqh, &wait, TASK_UNINTERRUPTIBLE);
374169ebd90SJan Kara 	sleep = inode->i_state & I_SYNC;
375169ebd90SJan Kara 	spin_unlock(&inode->i_lock);
376169ebd90SJan Kara 	if (sleep)
377169ebd90SJan Kara 		schedule();
378169ebd90SJan Kara 	finish_wait(wqh, &wait);
379169ebd90SJan Kara }
380169ebd90SJan Kara 
381169ebd90SJan Kara /*
382ccb26b5aSJan Kara  * Find proper writeback list for the inode depending on its current state and
383ccb26b5aSJan Kara  * possibly also change of its state while we were doing writeback.  Here we
384ccb26b5aSJan Kara  * handle things such as livelock prevention or fairness of writeback among
385ccb26b5aSJan Kara  * inodes. This function can be called only by flusher thread - noone else
386ccb26b5aSJan Kara  * processes all inodes in writeback lists and requeueing inodes behind flusher
387ccb26b5aSJan Kara  * thread's back can have unexpected consequences.
388ccb26b5aSJan Kara  */
389ccb26b5aSJan Kara static void requeue_inode(struct inode *inode, struct bdi_writeback *wb,
390ccb26b5aSJan Kara 			  struct writeback_control *wbc)
391ccb26b5aSJan Kara {
392ccb26b5aSJan Kara 	if (inode->i_state & I_FREEING)
393ccb26b5aSJan Kara 		return;
394ccb26b5aSJan Kara 
395ccb26b5aSJan Kara 	/*
396ccb26b5aSJan Kara 	 * Sync livelock prevention. Each inode is tagged and synced in one
397ccb26b5aSJan Kara 	 * shot. If still dirty, it will be redirty_tail()'ed below.  Update
398ccb26b5aSJan Kara 	 * the dirty time to prevent enqueue and sync it again.
399ccb26b5aSJan Kara 	 */
400ccb26b5aSJan Kara 	if ((inode->i_state & I_DIRTY) &&
401ccb26b5aSJan Kara 	    (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages))
402ccb26b5aSJan Kara 		inode->dirtied_when = jiffies;
403ccb26b5aSJan Kara 
4044f8ad655SJan Kara 	if (wbc->pages_skipped) {
4054f8ad655SJan Kara 		/*
4064f8ad655SJan Kara 		 * writeback is not making progress due to locked
4074f8ad655SJan Kara 		 * buffers. Skip this inode for now.
4084f8ad655SJan Kara 		 */
4094f8ad655SJan Kara 		redirty_tail(inode, wb);
4104f8ad655SJan Kara 		return;
4114f8ad655SJan Kara 	}
4124f8ad655SJan Kara 
413ccb26b5aSJan Kara 	if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY)) {
414ccb26b5aSJan Kara 		/*
415ccb26b5aSJan Kara 		 * We didn't write back all the pages.  nfs_writepages()
416ccb26b5aSJan Kara 		 * sometimes bales out without doing anything.
417ccb26b5aSJan Kara 		 */
418ccb26b5aSJan Kara 		if (wbc->nr_to_write <= 0) {
419ccb26b5aSJan Kara 			/* Slice used up. Queue for next turn. */
420ccb26b5aSJan Kara 			requeue_io(inode, wb);
421ccb26b5aSJan Kara 		} else {
422ccb26b5aSJan Kara 			/*
423ccb26b5aSJan Kara 			 * Writeback blocked by something other than
424ccb26b5aSJan Kara 			 * congestion. Delay the inode for some time to
425ccb26b5aSJan Kara 			 * avoid spinning on the CPU (100% iowait)
426ccb26b5aSJan Kara 			 * retrying writeback of the dirty page/inode
427ccb26b5aSJan Kara 			 * that cannot be performed immediately.
428ccb26b5aSJan Kara 			 */
429ccb26b5aSJan Kara 			redirty_tail(inode, wb);
430ccb26b5aSJan Kara 		}
431ccb26b5aSJan Kara 	} else if (inode->i_state & I_DIRTY) {
432ccb26b5aSJan Kara 		/*
433ccb26b5aSJan Kara 		 * Filesystems can dirty the inode during writeback operations,
434ccb26b5aSJan Kara 		 * such as delayed allocation during submission or metadata
435ccb26b5aSJan Kara 		 * updates after data IO completion.
436ccb26b5aSJan Kara 		 */
437ccb26b5aSJan Kara 		redirty_tail(inode, wb);
438ccb26b5aSJan Kara 	} else {
439ccb26b5aSJan Kara 		/* The inode is clean. Remove from writeback lists. */
440ccb26b5aSJan Kara 		list_del_init(&inode->i_wb_list);
441ccb26b5aSJan Kara 	}
442ccb26b5aSJan Kara }
443ccb26b5aSJan Kara 
444ccb26b5aSJan Kara /*
4454f8ad655SJan Kara  * Write out an inode and its dirty pages. Do not update the writeback list
4464f8ad655SJan Kara  * linkage. That is left to the caller. The caller is also responsible for
4474f8ad655SJan Kara  * setting I_SYNC flag and calling inode_sync_complete() to clear it.
4481da177e4SLinus Torvalds  */
4491da177e4SLinus Torvalds static int
450cd8ed2a4SYan Hong __writeback_single_inode(struct inode *inode, struct writeback_control *wbc)
4511da177e4SLinus Torvalds {
4521da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
453251d6a47SWu Fengguang 	long nr_to_write = wbc->nr_to_write;
45401c03194SChristoph Hellwig 	unsigned dirty;
4551da177e4SLinus Torvalds 	int ret;
4561da177e4SLinus Torvalds 
4574f8ad655SJan Kara 	WARN_ON(!(inode->i_state & I_SYNC));
4581da177e4SLinus Torvalds 
4599fb0a7daSTejun Heo 	trace_writeback_single_inode_start(inode, wbc, nr_to_write);
4609fb0a7daSTejun Heo 
4611da177e4SLinus Torvalds 	ret = do_writepages(mapping, wbc);
4621da177e4SLinus Torvalds 
46326821ed4SChristoph Hellwig 	/*
46426821ed4SChristoph Hellwig 	 * Make sure to wait on the data before writing out the metadata.
46526821ed4SChristoph Hellwig 	 * This is important for filesystems that modify metadata on data
4667747bd4bSDave Chinner 	 * I/O completion. We don't do it for sync(2) writeback because it has a
4677747bd4bSDave Chinner 	 * separate, external IO completion path and ->sync_fs for guaranteeing
4687747bd4bSDave Chinner 	 * inode metadata is written back correctly.
46926821ed4SChristoph Hellwig 	 */
4707747bd4bSDave Chinner 	if (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync) {
47126821ed4SChristoph Hellwig 		int err = filemap_fdatawait(mapping);
4721da177e4SLinus Torvalds 		if (ret == 0)
4731da177e4SLinus Torvalds 			ret = err;
4741da177e4SLinus Torvalds 	}
4751da177e4SLinus Torvalds 
4765547e8aaSDmitry Monakhov 	/*
4775547e8aaSDmitry Monakhov 	 * Some filesystems may redirty the inode during the writeback
4785547e8aaSDmitry Monakhov 	 * due to delalloc, clear dirty metadata flags right before
4795547e8aaSDmitry Monakhov 	 * write_inode()
4805547e8aaSDmitry Monakhov 	 */
481250df6edSDave Chinner 	spin_lock(&inode->i_lock);
4829c6ac78eSTejun Heo 
4835547e8aaSDmitry Monakhov 	dirty = inode->i_state & I_DIRTY;
4849c6ac78eSTejun Heo 	inode->i_state &= ~I_DIRTY;
4859c6ac78eSTejun Heo 
4869c6ac78eSTejun Heo 	/*
4879c6ac78eSTejun Heo 	 * Paired with smp_mb() in __mark_inode_dirty().  This allows
4889c6ac78eSTejun Heo 	 * __mark_inode_dirty() to test i_state without grabbing i_lock -
4899c6ac78eSTejun Heo 	 * either they see the I_DIRTY bits cleared or we see the dirtied
4909c6ac78eSTejun Heo 	 * inode.
4919c6ac78eSTejun Heo 	 *
4929c6ac78eSTejun Heo 	 * I_DIRTY_PAGES is always cleared together above even if @mapping
4939c6ac78eSTejun Heo 	 * still has dirty pages.  The flag is reinstated after smp_mb() if
4949c6ac78eSTejun Heo 	 * necessary.  This guarantees that either __mark_inode_dirty()
4959c6ac78eSTejun Heo 	 * sees clear I_DIRTY_PAGES or we see PAGECACHE_TAG_DIRTY.
4969c6ac78eSTejun Heo 	 */
4979c6ac78eSTejun Heo 	smp_mb();
4989c6ac78eSTejun Heo 
4999c6ac78eSTejun Heo 	if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
5009c6ac78eSTejun Heo 		inode->i_state |= I_DIRTY_PAGES;
5019c6ac78eSTejun Heo 
502250df6edSDave Chinner 	spin_unlock(&inode->i_lock);
5039c6ac78eSTejun Heo 
50426821ed4SChristoph Hellwig 	/* Don't write the inode if only I_DIRTY_PAGES was set */
50526821ed4SChristoph Hellwig 	if (dirty & (I_DIRTY_SYNC | I_DIRTY_DATASYNC)) {
506a9185b41SChristoph Hellwig 		int err = write_inode(inode, wbc);
5071da177e4SLinus Torvalds 		if (ret == 0)
5081da177e4SLinus Torvalds 			ret = err;
5091da177e4SLinus Torvalds 	}
5104f8ad655SJan Kara 	trace_writeback_single_inode(inode, wbc, nr_to_write);
5114f8ad655SJan Kara 	return ret;
5124f8ad655SJan Kara }
5134f8ad655SJan Kara 
5144f8ad655SJan Kara /*
5154f8ad655SJan Kara  * Write out an inode's dirty pages. Either the caller has an active reference
5164f8ad655SJan Kara  * on the inode or the inode has I_WILL_FREE set.
5174f8ad655SJan Kara  *
5184f8ad655SJan Kara  * This function is designed to be called for writing back one inode which
5194f8ad655SJan Kara  * we go e.g. from filesystem. Flusher thread uses __writeback_single_inode()
5204f8ad655SJan Kara  * and does more profound writeback list handling in writeback_sb_inodes().
5214f8ad655SJan Kara  */
5224f8ad655SJan Kara static int
5234f8ad655SJan Kara writeback_single_inode(struct inode *inode, struct bdi_writeback *wb,
5244f8ad655SJan Kara 		       struct writeback_control *wbc)
5254f8ad655SJan Kara {
5264f8ad655SJan Kara 	int ret = 0;
5274f8ad655SJan Kara 
5284f8ad655SJan Kara 	spin_lock(&inode->i_lock);
5294f8ad655SJan Kara 	if (!atomic_read(&inode->i_count))
5304f8ad655SJan Kara 		WARN_ON(!(inode->i_state & (I_WILL_FREE|I_FREEING)));
5314f8ad655SJan Kara 	else
5324f8ad655SJan Kara 		WARN_ON(inode->i_state & I_WILL_FREE);
5334f8ad655SJan Kara 
5344f8ad655SJan Kara 	if (inode->i_state & I_SYNC) {
5354f8ad655SJan Kara 		if (wbc->sync_mode != WB_SYNC_ALL)
5364f8ad655SJan Kara 			goto out;
5374f8ad655SJan Kara 		/*
538169ebd90SJan Kara 		 * It's a data-integrity sync. We must wait. Since callers hold
539169ebd90SJan Kara 		 * inode reference or inode has I_WILL_FREE set, it cannot go
540169ebd90SJan Kara 		 * away under us.
5414f8ad655SJan Kara 		 */
542169ebd90SJan Kara 		__inode_wait_for_writeback(inode);
5434f8ad655SJan Kara 	}
5444f8ad655SJan Kara 	WARN_ON(inode->i_state & I_SYNC);
5454f8ad655SJan Kara 	/*
546f9b0e058SJan Kara 	 * Skip inode if it is clean and we have no outstanding writeback in
547f9b0e058SJan Kara 	 * WB_SYNC_ALL mode. We don't want to mess with writeback lists in this
548f9b0e058SJan Kara 	 * function since flusher thread may be doing for example sync in
549f9b0e058SJan Kara 	 * parallel and if we move the inode, it could get skipped. So here we
550f9b0e058SJan Kara 	 * make sure inode is on some writeback list and leave it there unless
551f9b0e058SJan Kara 	 * we have completely cleaned the inode.
5524f8ad655SJan Kara 	 */
553f9b0e058SJan Kara 	if (!(inode->i_state & I_DIRTY) &&
554f9b0e058SJan Kara 	    (wbc->sync_mode != WB_SYNC_ALL ||
555f9b0e058SJan Kara 	     !mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK)))
5564f8ad655SJan Kara 		goto out;
5574f8ad655SJan Kara 	inode->i_state |= I_SYNC;
5584f8ad655SJan Kara 	spin_unlock(&inode->i_lock);
5594f8ad655SJan Kara 
560cd8ed2a4SYan Hong 	ret = __writeback_single_inode(inode, wbc);
5611da177e4SLinus Torvalds 
562f758eeabSChristoph Hellwig 	spin_lock(&wb->list_lock);
563250df6edSDave Chinner 	spin_lock(&inode->i_lock);
5644f8ad655SJan Kara 	/*
5654f8ad655SJan Kara 	 * If inode is clean, remove it from writeback lists. Otherwise don't
5664f8ad655SJan Kara 	 * touch it. See comment above for explanation.
5674f8ad655SJan Kara 	 */
5684f8ad655SJan Kara 	if (!(inode->i_state & I_DIRTY))
5694f8ad655SJan Kara 		list_del_init(&inode->i_wb_list);
5704f8ad655SJan Kara 	spin_unlock(&wb->list_lock);
5711c0eeaf5SJoern Engel 	inode_sync_complete(inode);
5724f8ad655SJan Kara out:
5734f8ad655SJan Kara 	spin_unlock(&inode->i_lock);
5741da177e4SLinus Torvalds 	return ret;
5751da177e4SLinus Torvalds }
5761da177e4SLinus Torvalds 
5771a12d8bdSWu Fengguang static long writeback_chunk_size(struct backing_dev_info *bdi,
5781a12d8bdSWu Fengguang 				 struct wb_writeback_work *work)
579d46db3d5SWu Fengguang {
580d46db3d5SWu Fengguang 	long pages;
581d46db3d5SWu Fengguang 
582d46db3d5SWu Fengguang 	/*
583d46db3d5SWu Fengguang 	 * WB_SYNC_ALL mode does livelock avoidance by syncing dirty
584d46db3d5SWu Fengguang 	 * inodes/pages in one big loop. Setting wbc.nr_to_write=LONG_MAX
585d46db3d5SWu Fengguang 	 * here avoids calling into writeback_inodes_wb() more than once.
586d46db3d5SWu Fengguang 	 *
587d46db3d5SWu Fengguang 	 * The intended call sequence for WB_SYNC_ALL writeback is:
588d46db3d5SWu Fengguang 	 *
589d46db3d5SWu Fengguang 	 *      wb_writeback()
590d46db3d5SWu Fengguang 	 *          writeback_sb_inodes()       <== called only once
591d46db3d5SWu Fengguang 	 *              write_cache_pages()     <== called once for each inode
592d46db3d5SWu Fengguang 	 *                   (quickly) tag currently dirty pages
593d46db3d5SWu Fengguang 	 *                   (maybe slowly) sync all tagged pages
594d46db3d5SWu Fengguang 	 */
595d46db3d5SWu Fengguang 	if (work->sync_mode == WB_SYNC_ALL || work->tagged_writepages)
596d46db3d5SWu Fengguang 		pages = LONG_MAX;
5971a12d8bdSWu Fengguang 	else {
5981a12d8bdSWu Fengguang 		pages = min(bdi->avg_write_bandwidth / 2,
5991a12d8bdSWu Fengguang 			    global_dirty_limit / DIRTY_SCOPE);
6001a12d8bdSWu Fengguang 		pages = min(pages, work->nr_pages);
6011a12d8bdSWu Fengguang 		pages = round_down(pages + MIN_WRITEBACK_PAGES,
6021a12d8bdSWu Fengguang 				   MIN_WRITEBACK_PAGES);
6031a12d8bdSWu Fengguang 	}
604d46db3d5SWu Fengguang 
605d46db3d5SWu Fengguang 	return pages;
606d46db3d5SWu Fengguang }
607d46db3d5SWu Fengguang 
60803ba3782SJens Axboe /*
609f11c9c5cSEdward Shishkin  * Write a portion of b_io inodes which belong to @sb.
610edadfb10SChristoph Hellwig  *
611d46db3d5SWu Fengguang  * Return the number of pages and/or inodes written.
612f11c9c5cSEdward Shishkin  */
613d46db3d5SWu Fengguang static long writeback_sb_inodes(struct super_block *sb,
614d46db3d5SWu Fengguang 				struct bdi_writeback *wb,
615d46db3d5SWu Fengguang 				struct wb_writeback_work *work)
61603ba3782SJens Axboe {
617d46db3d5SWu Fengguang 	struct writeback_control wbc = {
618d46db3d5SWu Fengguang 		.sync_mode		= work->sync_mode,
619d46db3d5SWu Fengguang 		.tagged_writepages	= work->tagged_writepages,
620d46db3d5SWu Fengguang 		.for_kupdate		= work->for_kupdate,
621d46db3d5SWu Fengguang 		.for_background		= work->for_background,
6227747bd4bSDave Chinner 		.for_sync		= work->for_sync,
623d46db3d5SWu Fengguang 		.range_cyclic		= work->range_cyclic,
624d46db3d5SWu Fengguang 		.range_start		= 0,
625d46db3d5SWu Fengguang 		.range_end		= LLONG_MAX,
626d46db3d5SWu Fengguang 	};
627d46db3d5SWu Fengguang 	unsigned long start_time = jiffies;
628d46db3d5SWu Fengguang 	long write_chunk;
629d46db3d5SWu Fengguang 	long wrote = 0;  /* count both pages and inodes */
630d46db3d5SWu Fengguang 
63103ba3782SJens Axboe 	while (!list_empty(&wb->b_io)) {
6327ccf19a8SNick Piggin 		struct inode *inode = wb_inode(wb->b_io.prev);
633edadfb10SChristoph Hellwig 
634edadfb10SChristoph Hellwig 		if (inode->i_sb != sb) {
635d46db3d5SWu Fengguang 			if (work->sb) {
636edadfb10SChristoph Hellwig 				/*
637edadfb10SChristoph Hellwig 				 * We only want to write back data for this
638edadfb10SChristoph Hellwig 				 * superblock, move all inodes not belonging
639edadfb10SChristoph Hellwig 				 * to it back onto the dirty list.
640edadfb10SChristoph Hellwig 				 */
641f758eeabSChristoph Hellwig 				redirty_tail(inode, wb);
64266f3b8e2SJens Axboe 				continue;
64366f3b8e2SJens Axboe 			}
644edadfb10SChristoph Hellwig 
645edadfb10SChristoph Hellwig 			/*
646edadfb10SChristoph Hellwig 			 * The inode belongs to a different superblock.
647edadfb10SChristoph Hellwig 			 * Bounce back to the caller to unpin this and
648edadfb10SChristoph Hellwig 			 * pin the next superblock.
649edadfb10SChristoph Hellwig 			 */
650d46db3d5SWu Fengguang 			break;
651edadfb10SChristoph Hellwig 		}
652edadfb10SChristoph Hellwig 
6539843b76aSChristoph Hellwig 		/*
654331cbdeeSWanpeng Li 		 * Don't bother with new inodes or inodes being freed, first
655331cbdeeSWanpeng Li 		 * kind does not need periodic writeout yet, and for the latter
6569843b76aSChristoph Hellwig 		 * kind writeout is handled by the freer.
6579843b76aSChristoph Hellwig 		 */
658250df6edSDave Chinner 		spin_lock(&inode->i_lock);
6599843b76aSChristoph Hellwig 		if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) {
660250df6edSDave Chinner 			spin_unlock(&inode->i_lock);
661fcc5c222SWu Fengguang 			redirty_tail(inode, wb);
6627ef0d737SNick Piggin 			continue;
6637ef0d737SNick Piggin 		}
664cc1676d9SJan Kara 		if ((inode->i_state & I_SYNC) && wbc.sync_mode != WB_SYNC_ALL) {
665cc1676d9SJan Kara 			/*
666cc1676d9SJan Kara 			 * If this inode is locked for writeback and we are not
667cc1676d9SJan Kara 			 * doing writeback-for-data-integrity, move it to
668cc1676d9SJan Kara 			 * b_more_io so that writeback can proceed with the
669cc1676d9SJan Kara 			 * other inodes on s_io.
670cc1676d9SJan Kara 			 *
671cc1676d9SJan Kara 			 * We'll have another go at writing back this inode
672cc1676d9SJan Kara 			 * when we completed a full scan of b_io.
673cc1676d9SJan Kara 			 */
674cc1676d9SJan Kara 			spin_unlock(&inode->i_lock);
675cc1676d9SJan Kara 			requeue_io(inode, wb);
676cc1676d9SJan Kara 			trace_writeback_sb_inodes_requeue(inode);
677cc1676d9SJan Kara 			continue;
678cc1676d9SJan Kara 		}
679f0d07b7fSJan Kara 		spin_unlock(&wb->list_lock);
680f0d07b7fSJan Kara 
6814f8ad655SJan Kara 		/*
6824f8ad655SJan Kara 		 * We already requeued the inode if it had I_SYNC set and we
6834f8ad655SJan Kara 		 * are doing WB_SYNC_NONE writeback. So this catches only the
6844f8ad655SJan Kara 		 * WB_SYNC_ALL case.
6854f8ad655SJan Kara 		 */
686169ebd90SJan Kara 		if (inode->i_state & I_SYNC) {
687169ebd90SJan Kara 			/* Wait for I_SYNC. This function drops i_lock... */
688169ebd90SJan Kara 			inode_sleep_on_writeback(inode);
689169ebd90SJan Kara 			/* Inode may be gone, start again */
690ead188f9SJan Kara 			spin_lock(&wb->list_lock);
691169ebd90SJan Kara 			continue;
692169ebd90SJan Kara 		}
6934f8ad655SJan Kara 		inode->i_state |= I_SYNC;
6944f8ad655SJan Kara 		spin_unlock(&inode->i_lock);
695169ebd90SJan Kara 
6961a12d8bdSWu Fengguang 		write_chunk = writeback_chunk_size(wb->bdi, work);
697d46db3d5SWu Fengguang 		wbc.nr_to_write = write_chunk;
698d46db3d5SWu Fengguang 		wbc.pages_skipped = 0;
699250df6edSDave Chinner 
700169ebd90SJan Kara 		/*
701169ebd90SJan Kara 		 * We use I_SYNC to pin the inode in memory. While it is set
702169ebd90SJan Kara 		 * evict_inode() will wait so the inode cannot be freed.
703169ebd90SJan Kara 		 */
704cd8ed2a4SYan Hong 		__writeback_single_inode(inode, &wbc);
705d46db3d5SWu Fengguang 
706d46db3d5SWu Fengguang 		work->nr_pages -= write_chunk - wbc.nr_to_write;
707d46db3d5SWu Fengguang 		wrote += write_chunk - wbc.nr_to_write;
7084f8ad655SJan Kara 		spin_lock(&wb->list_lock);
7094f8ad655SJan Kara 		spin_lock(&inode->i_lock);
710d46db3d5SWu Fengguang 		if (!(inode->i_state & I_DIRTY))
711d46db3d5SWu Fengguang 			wrote++;
7124f8ad655SJan Kara 		requeue_inode(inode, wb, &wbc);
7134f8ad655SJan Kara 		inode_sync_complete(inode);
7140f1b1fd8SDave Chinner 		spin_unlock(&inode->i_lock);
715169ebd90SJan Kara 		cond_resched_lock(&wb->list_lock);
716d46db3d5SWu Fengguang 		/*
717d46db3d5SWu Fengguang 		 * bail out to wb_writeback() often enough to check
718d46db3d5SWu Fengguang 		 * background threshold and other termination conditions.
719d46db3d5SWu Fengguang 		 */
720d46db3d5SWu Fengguang 		if (wrote) {
721d46db3d5SWu Fengguang 			if (time_is_before_jiffies(start_time + HZ / 10UL))
722d46db3d5SWu Fengguang 				break;
723d46db3d5SWu Fengguang 			if (work->nr_pages <= 0)
724d46db3d5SWu Fengguang 				break;
7251da177e4SLinus Torvalds 		}
7268bc3be27SFengguang Wu 	}
727d46db3d5SWu Fengguang 	return wrote;
728f11c9c5cSEdward Shishkin }
72938f21977SNick Piggin 
730d46db3d5SWu Fengguang static long __writeback_inodes_wb(struct bdi_writeback *wb,
731d46db3d5SWu Fengguang 				  struct wb_writeback_work *work)
732f11c9c5cSEdward Shishkin {
733d46db3d5SWu Fengguang 	unsigned long start_time = jiffies;
734d46db3d5SWu Fengguang 	long wrote = 0;
735f11c9c5cSEdward Shishkin 
736f11c9c5cSEdward Shishkin 	while (!list_empty(&wb->b_io)) {
7377ccf19a8SNick Piggin 		struct inode *inode = wb_inode(wb->b_io.prev);
738f11c9c5cSEdward Shishkin 		struct super_block *sb = inode->i_sb;
739f11c9c5cSEdward Shishkin 
74012ad3ab6SDave Chinner 		if (!grab_super_passive(sb)) {
7410e995816SWu Fengguang 			/*
7420e995816SWu Fengguang 			 * grab_super_passive() may fail consistently due to
7430e995816SWu Fengguang 			 * s_umount being grabbed by someone else. Don't use
7440e995816SWu Fengguang 			 * requeue_io() to avoid busy retrying the inode/sb.
7450e995816SWu Fengguang 			 */
7460e995816SWu Fengguang 			redirty_tail(inode, wb);
747d19de7edSChristoph Hellwig 			continue;
748334132aeSChristoph Hellwig 		}
749d46db3d5SWu Fengguang 		wrote += writeback_sb_inodes(sb, wb, work);
750d19de7edSChristoph Hellwig 		drop_super(sb);
751f11c9c5cSEdward Shishkin 
752d46db3d5SWu Fengguang 		/* refer to the same tests at the end of writeback_sb_inodes */
753d46db3d5SWu Fengguang 		if (wrote) {
754d46db3d5SWu Fengguang 			if (time_is_before_jiffies(start_time + HZ / 10UL))
755d46db3d5SWu Fengguang 				break;
756d46db3d5SWu Fengguang 			if (work->nr_pages <= 0)
757f11c9c5cSEdward Shishkin 				break;
758f11c9c5cSEdward Shishkin 		}
759d46db3d5SWu Fengguang 	}
76066f3b8e2SJens Axboe 	/* Leave any unwritten inodes on b_io */
761d46db3d5SWu Fengguang 	return wrote;
76266f3b8e2SJens Axboe }
76366f3b8e2SJens Axboe 
7647d9f073bSWanpeng Li static long writeback_inodes_wb(struct bdi_writeback *wb, long nr_pages,
7650e175a18SCurt Wohlgemuth 				enum wb_reason reason)
766edadfb10SChristoph Hellwig {
767d46db3d5SWu Fengguang 	struct wb_writeback_work work = {
768d46db3d5SWu Fengguang 		.nr_pages	= nr_pages,
769d46db3d5SWu Fengguang 		.sync_mode	= WB_SYNC_NONE,
770d46db3d5SWu Fengguang 		.range_cyclic	= 1,
7710e175a18SCurt Wohlgemuth 		.reason		= reason,
772d46db3d5SWu Fengguang 	};
773edadfb10SChristoph Hellwig 
774f758eeabSChristoph Hellwig 	spin_lock(&wb->list_lock);
775424b351fSWu Fengguang 	if (list_empty(&wb->b_io))
776ad4e38ddSCurt Wohlgemuth 		queue_io(wb, &work);
777d46db3d5SWu Fengguang 	__writeback_inodes_wb(wb, &work);
778f758eeabSChristoph Hellwig 	spin_unlock(&wb->list_lock);
779edadfb10SChristoph Hellwig 
780d46db3d5SWu Fengguang 	return nr_pages - work.nr_pages;
78166f3b8e2SJens Axboe }
78266f3b8e2SJens Axboe 
783b00949aaSWu Fengguang static bool over_bground_thresh(struct backing_dev_info *bdi)
78403ba3782SJens Axboe {
78503ba3782SJens Axboe 	unsigned long background_thresh, dirty_thresh;
78603ba3782SJens Axboe 
78716c4042fSWu Fengguang 	global_dirty_limits(&background_thresh, &dirty_thresh);
78803ba3782SJens Axboe 
789b00949aaSWu Fengguang 	if (global_page_state(NR_FILE_DIRTY) +
790b00949aaSWu Fengguang 	    global_page_state(NR_UNSTABLE_NFS) > background_thresh)
791b00949aaSWu Fengguang 		return true;
792b00949aaSWu Fengguang 
793b00949aaSWu Fengguang 	if (bdi_stat(bdi, BDI_RECLAIMABLE) >
794b00949aaSWu Fengguang 				bdi_dirty_limit(bdi, background_thresh))
795b00949aaSWu Fengguang 		return true;
796b00949aaSWu Fengguang 
797b00949aaSWu Fengguang 	return false;
79803ba3782SJens Axboe }
79903ba3782SJens Axboe 
80003ba3782SJens Axboe /*
801e98be2d5SWu Fengguang  * Called under wb->list_lock. If there are multiple wb per bdi,
802e98be2d5SWu Fengguang  * only the flusher working on the first wb should do it.
803e98be2d5SWu Fengguang  */
804e98be2d5SWu Fengguang static void wb_update_bandwidth(struct bdi_writeback *wb,
805e98be2d5SWu Fengguang 				unsigned long start_time)
806e98be2d5SWu Fengguang {
807af6a3113SWu Fengguang 	__bdi_update_bandwidth(wb->bdi, 0, 0, 0, 0, 0, start_time);
808e98be2d5SWu Fengguang }
809e98be2d5SWu Fengguang 
810e98be2d5SWu Fengguang /*
81103ba3782SJens Axboe  * Explicit flushing or periodic writeback of "old" data.
81203ba3782SJens Axboe  *
81303ba3782SJens Axboe  * Define "old": the first time one of an inode's pages is dirtied, we mark the
81403ba3782SJens Axboe  * dirtying-time in the inode's address_space.  So this periodic writeback code
81503ba3782SJens Axboe  * just walks the superblock inode list, writing back any inodes which are
81603ba3782SJens Axboe  * older than a specific point in time.
81703ba3782SJens Axboe  *
81803ba3782SJens Axboe  * Try to run once per dirty_writeback_interval.  But if a writeback event
81903ba3782SJens Axboe  * takes longer than a dirty_writeback_interval interval, then leave a
82003ba3782SJens Axboe  * one-second gap.
82103ba3782SJens Axboe  *
82203ba3782SJens Axboe  * older_than_this takes precedence over nr_to_write.  So we'll only write back
82303ba3782SJens Axboe  * all dirty pages if they are all attached to "old" mappings.
82403ba3782SJens Axboe  */
825c4a77a6cSJens Axboe static long wb_writeback(struct bdi_writeback *wb,
82683ba7b07SChristoph Hellwig 			 struct wb_writeback_work *work)
82703ba3782SJens Axboe {
828e98be2d5SWu Fengguang 	unsigned long wb_start = jiffies;
829d46db3d5SWu Fengguang 	long nr_pages = work->nr_pages;
8300dc83bd3SJan Kara 	unsigned long oldest_jif;
831a5989bdcSJan Kara 	struct inode *inode;
832d46db3d5SWu Fengguang 	long progress;
83303ba3782SJens Axboe 
8340dc83bd3SJan Kara 	oldest_jif = jiffies;
8350dc83bd3SJan Kara 	work->older_than_this = &oldest_jif;
83603ba3782SJens Axboe 
837e8dfc305SWu Fengguang 	spin_lock(&wb->list_lock);
83803ba3782SJens Axboe 	for (;;) {
83903ba3782SJens Axboe 		/*
840d3ddec76SWu Fengguang 		 * Stop writeback when nr_pages has been consumed
84103ba3782SJens Axboe 		 */
84283ba7b07SChristoph Hellwig 		if (work->nr_pages <= 0)
84303ba3782SJens Axboe 			break;
84403ba3782SJens Axboe 
84503ba3782SJens Axboe 		/*
846aa373cf5SJan Kara 		 * Background writeout and kupdate-style writeback may
847aa373cf5SJan Kara 		 * run forever. Stop them if there is other work to do
848aa373cf5SJan Kara 		 * so that e.g. sync can proceed. They'll be restarted
849aa373cf5SJan Kara 		 * after the other works are all done.
850aa373cf5SJan Kara 		 */
851aa373cf5SJan Kara 		if ((work->for_background || work->for_kupdate) &&
852aa373cf5SJan Kara 		    !list_empty(&wb->bdi->work_list))
853aa373cf5SJan Kara 			break;
854aa373cf5SJan Kara 
855aa373cf5SJan Kara 		/*
856d3ddec76SWu Fengguang 		 * For background writeout, stop when we are below the
857d3ddec76SWu Fengguang 		 * background dirty threshold
85803ba3782SJens Axboe 		 */
859b00949aaSWu Fengguang 		if (work->for_background && !over_bground_thresh(wb->bdi))
86003ba3782SJens Axboe 			break;
86103ba3782SJens Axboe 
8621bc36b64SJan Kara 		/*
8631bc36b64SJan Kara 		 * Kupdate and background works are special and we want to
8641bc36b64SJan Kara 		 * include all inodes that need writing. Livelock avoidance is
8651bc36b64SJan Kara 		 * handled by these works yielding to any other work so we are
8661bc36b64SJan Kara 		 * safe.
8671bc36b64SJan Kara 		 */
868ba9aa839SWu Fengguang 		if (work->for_kupdate) {
8690dc83bd3SJan Kara 			oldest_jif = jiffies -
870ba9aa839SWu Fengguang 				msecs_to_jiffies(dirty_expire_interval * 10);
8711bc36b64SJan Kara 		} else if (work->for_background)
8720dc83bd3SJan Kara 			oldest_jif = jiffies;
873028c2dd1SDave Chinner 
874d46db3d5SWu Fengguang 		trace_writeback_start(wb->bdi, work);
875e8dfc305SWu Fengguang 		if (list_empty(&wb->b_io))
876ad4e38ddSCurt Wohlgemuth 			queue_io(wb, work);
87783ba7b07SChristoph Hellwig 		if (work->sb)
878d46db3d5SWu Fengguang 			progress = writeback_sb_inodes(work->sb, wb, work);
879edadfb10SChristoph Hellwig 		else
880d46db3d5SWu Fengguang 			progress = __writeback_inodes_wb(wb, work);
881d46db3d5SWu Fengguang 		trace_writeback_written(wb->bdi, work);
882028c2dd1SDave Chinner 
883e98be2d5SWu Fengguang 		wb_update_bandwidth(wb, wb_start);
88403ba3782SJens Axboe 
88503ba3782SJens Axboe 		/*
88671fd05a8SJens Axboe 		 * Did we write something? Try for more
887e6fb6da2SWu Fengguang 		 *
888e6fb6da2SWu Fengguang 		 * Dirty inodes are moved to b_io for writeback in batches.
889e6fb6da2SWu Fengguang 		 * The completion of the current batch does not necessarily
890e6fb6da2SWu Fengguang 		 * mean the overall work is done. So we keep looping as long
891e6fb6da2SWu Fengguang 		 * as made some progress on cleaning pages or inodes.
89271fd05a8SJens Axboe 		 */
893d46db3d5SWu Fengguang 		if (progress)
89403ba3782SJens Axboe 			continue;
895a5989bdcSJan Kara 		/*
896e6fb6da2SWu Fengguang 		 * No more inodes for IO, bail
897a5989bdcSJan Kara 		 */
898b7a2441fSWu Fengguang 		if (list_empty(&wb->b_more_io))
89903ba3782SJens Axboe 			break;
90003ba3782SJens Axboe 		/*
9018010c3b6SJens Axboe 		 * Nothing written. Wait for some inode to
9028010c3b6SJens Axboe 		 * become available for writeback. Otherwise
9038010c3b6SJens Axboe 		 * we'll just busyloop.
90403ba3782SJens Axboe 		 */
90503ba3782SJens Axboe 		if (!list_empty(&wb->b_more_io))  {
906d46db3d5SWu Fengguang 			trace_writeback_wait(wb->bdi, work);
90703ba3782SJens Axboe 			inode = wb_inode(wb->b_more_io.prev);
908250df6edSDave Chinner 			spin_lock(&inode->i_lock);
909f0d07b7fSJan Kara 			spin_unlock(&wb->list_lock);
910169ebd90SJan Kara 			/* This function drops i_lock... */
911169ebd90SJan Kara 			inode_sleep_on_writeback(inode);
912f0d07b7fSJan Kara 			spin_lock(&wb->list_lock);
91303ba3782SJens Axboe 		}
91403ba3782SJens Axboe 	}
915e8dfc305SWu Fengguang 	spin_unlock(&wb->list_lock);
91603ba3782SJens Axboe 
917d46db3d5SWu Fengguang 	return nr_pages - work->nr_pages;
91803ba3782SJens Axboe }
91903ba3782SJens Axboe 
92003ba3782SJens Axboe /*
92183ba7b07SChristoph Hellwig  * Return the next wb_writeback_work struct that hasn't been processed yet.
92203ba3782SJens Axboe  */
92383ba7b07SChristoph Hellwig static struct wb_writeback_work *
92408852b6dSMinchan Kim get_next_work_item(struct backing_dev_info *bdi)
92503ba3782SJens Axboe {
92683ba7b07SChristoph Hellwig 	struct wb_writeback_work *work = NULL;
92703ba3782SJens Axboe 
9286467716aSArtem Bityutskiy 	spin_lock_bh(&bdi->wb_lock);
92983ba7b07SChristoph Hellwig 	if (!list_empty(&bdi->work_list)) {
93083ba7b07SChristoph Hellwig 		work = list_entry(bdi->work_list.next,
93183ba7b07SChristoph Hellwig 				  struct wb_writeback_work, list);
93283ba7b07SChristoph Hellwig 		list_del_init(&work->list);
93303ba3782SJens Axboe 	}
9346467716aSArtem Bityutskiy 	spin_unlock_bh(&bdi->wb_lock);
93583ba7b07SChristoph Hellwig 	return work;
93603ba3782SJens Axboe }
93703ba3782SJens Axboe 
938cdf01dd5SLinus Torvalds /*
939cdf01dd5SLinus Torvalds  * Add in the number of potentially dirty inodes, because each inode
940cdf01dd5SLinus Torvalds  * write can dirty pagecache in the underlying blockdev.
941cdf01dd5SLinus Torvalds  */
942cdf01dd5SLinus Torvalds static unsigned long get_nr_dirty_pages(void)
943cdf01dd5SLinus Torvalds {
944cdf01dd5SLinus Torvalds 	return global_page_state(NR_FILE_DIRTY) +
945cdf01dd5SLinus Torvalds 		global_page_state(NR_UNSTABLE_NFS) +
946cdf01dd5SLinus Torvalds 		get_nr_dirty_inodes();
947cdf01dd5SLinus Torvalds }
948cdf01dd5SLinus Torvalds 
9496585027aSJan Kara static long wb_check_background_flush(struct bdi_writeback *wb)
9506585027aSJan Kara {
951b00949aaSWu Fengguang 	if (over_bground_thresh(wb->bdi)) {
9526585027aSJan Kara 
9536585027aSJan Kara 		struct wb_writeback_work work = {
9546585027aSJan Kara 			.nr_pages	= LONG_MAX,
9556585027aSJan Kara 			.sync_mode	= WB_SYNC_NONE,
9566585027aSJan Kara 			.for_background	= 1,
9576585027aSJan Kara 			.range_cyclic	= 1,
9580e175a18SCurt Wohlgemuth 			.reason		= WB_REASON_BACKGROUND,
9596585027aSJan Kara 		};
9606585027aSJan Kara 
9616585027aSJan Kara 		return wb_writeback(wb, &work);
9626585027aSJan Kara 	}
9636585027aSJan Kara 
9646585027aSJan Kara 	return 0;
9656585027aSJan Kara }
9666585027aSJan Kara 
96703ba3782SJens Axboe static long wb_check_old_data_flush(struct bdi_writeback *wb)
96803ba3782SJens Axboe {
96903ba3782SJens Axboe 	unsigned long expired;
97003ba3782SJens Axboe 	long nr_pages;
97103ba3782SJens Axboe 
97269b62d01SJens Axboe 	/*
97369b62d01SJens Axboe 	 * When set to zero, disable periodic writeback
97469b62d01SJens Axboe 	 */
97569b62d01SJens Axboe 	if (!dirty_writeback_interval)
97669b62d01SJens Axboe 		return 0;
97769b62d01SJens Axboe 
97803ba3782SJens Axboe 	expired = wb->last_old_flush +
97903ba3782SJens Axboe 			msecs_to_jiffies(dirty_writeback_interval * 10);
98003ba3782SJens Axboe 	if (time_before(jiffies, expired))
98103ba3782SJens Axboe 		return 0;
98203ba3782SJens Axboe 
98303ba3782SJens Axboe 	wb->last_old_flush = jiffies;
984cdf01dd5SLinus Torvalds 	nr_pages = get_nr_dirty_pages();
98503ba3782SJens Axboe 
986c4a77a6cSJens Axboe 	if (nr_pages) {
98783ba7b07SChristoph Hellwig 		struct wb_writeback_work work = {
988c4a77a6cSJens Axboe 			.nr_pages	= nr_pages,
989c4a77a6cSJens Axboe 			.sync_mode	= WB_SYNC_NONE,
990c4a77a6cSJens Axboe 			.for_kupdate	= 1,
991c4a77a6cSJens Axboe 			.range_cyclic	= 1,
9920e175a18SCurt Wohlgemuth 			.reason		= WB_REASON_PERIODIC,
993c4a77a6cSJens Axboe 		};
994c4a77a6cSJens Axboe 
99583ba7b07SChristoph Hellwig 		return wb_writeback(wb, &work);
996c4a77a6cSJens Axboe 	}
99703ba3782SJens Axboe 
99803ba3782SJens Axboe 	return 0;
99903ba3782SJens Axboe }
100003ba3782SJens Axboe 
100103ba3782SJens Axboe /*
100203ba3782SJens Axboe  * Retrieve work items and do the writeback they describe
100303ba3782SJens Axboe  */
100425d130baSWanpeng Li static long wb_do_writeback(struct bdi_writeback *wb)
100503ba3782SJens Axboe {
100603ba3782SJens Axboe 	struct backing_dev_info *bdi = wb->bdi;
100783ba7b07SChristoph Hellwig 	struct wb_writeback_work *work;
1008c4a77a6cSJens Axboe 	long wrote = 0;
100903ba3782SJens Axboe 
101081d73a32SJan Kara 	set_bit(BDI_writeback_running, &wb->bdi->state);
101108852b6dSMinchan Kim 	while ((work = get_next_work_item(bdi)) != NULL) {
101283ba7b07SChristoph Hellwig 
1013455b2864SDave Chinner 		trace_writeback_exec(bdi, work);
1014455b2864SDave Chinner 
101583ba7b07SChristoph Hellwig 		wrote += wb_writeback(wb, work);
101603ba3782SJens Axboe 
101703ba3782SJens Axboe 		/*
101883ba7b07SChristoph Hellwig 		 * Notify the caller of completion if this is a synchronous
101983ba7b07SChristoph Hellwig 		 * work item, otherwise just free it.
102003ba3782SJens Axboe 		 */
102183ba7b07SChristoph Hellwig 		if (work->done)
102283ba7b07SChristoph Hellwig 			complete(work->done);
102383ba7b07SChristoph Hellwig 		else
102483ba7b07SChristoph Hellwig 			kfree(work);
102503ba3782SJens Axboe 	}
102603ba3782SJens Axboe 
102703ba3782SJens Axboe 	/*
102803ba3782SJens Axboe 	 * Check for periodic writeback, kupdated() style
102903ba3782SJens Axboe 	 */
103003ba3782SJens Axboe 	wrote += wb_check_old_data_flush(wb);
10316585027aSJan Kara 	wrote += wb_check_background_flush(wb);
103281d73a32SJan Kara 	clear_bit(BDI_writeback_running, &wb->bdi->state);
103303ba3782SJens Axboe 
103403ba3782SJens Axboe 	return wrote;
103503ba3782SJens Axboe }
103603ba3782SJens Axboe 
103703ba3782SJens Axboe /*
103803ba3782SJens Axboe  * Handle writeback of dirty data for the device backed by this bdi. Also
1039839a8e86STejun Heo  * reschedules periodically and does kupdated style flushing.
104003ba3782SJens Axboe  */
1041839a8e86STejun Heo void bdi_writeback_workfn(struct work_struct *work)
104203ba3782SJens Axboe {
1043839a8e86STejun Heo 	struct bdi_writeback *wb = container_of(to_delayed_work(work),
1044839a8e86STejun Heo 						struct bdi_writeback, dwork);
104508243900SChristoph Hellwig 	struct backing_dev_info *bdi = wb->bdi;
104603ba3782SJens Axboe 	long pages_written;
104703ba3782SJens Axboe 
1048ef3b1019STejun Heo 	set_worker_desc("flush-%s", dev_name(bdi->dev));
1049766f9164SPeter Zijlstra 	current->flags |= PF_SWAPWRITE;
105003ba3782SJens Axboe 
1051839a8e86STejun Heo 	if (likely(!current_is_workqueue_rescuer() ||
10525acda9d1SJan Kara 		   !test_bit(BDI_registered, &bdi->state))) {
105303ba3782SJens Axboe 		/*
1054839a8e86STejun Heo 		 * The normal path.  Keep writing back @bdi until its
1055839a8e86STejun Heo 		 * work_list is empty.  Note that this path is also taken
1056839a8e86STejun Heo 		 * if @bdi is shutting down even when we're running off the
1057839a8e86STejun Heo 		 * rescuer as work_list needs to be drained.
105803ba3782SJens Axboe 		 */
1059839a8e86STejun Heo 		do {
106025d130baSWanpeng Li 			pages_written = wb_do_writeback(wb);
1061455b2864SDave Chinner 			trace_writeback_pages_written(pages_written);
1062839a8e86STejun Heo 		} while (!list_empty(&bdi->work_list));
1063839a8e86STejun Heo 	} else {
1064253c34e9SArtem Bityutskiy 		/*
1065839a8e86STejun Heo 		 * bdi_wq can't get enough workers and we're running off
1066839a8e86STejun Heo 		 * the emergency worker.  Don't hog it.  Hopefully, 1024 is
1067839a8e86STejun Heo 		 * enough for efficient IO.
1068253c34e9SArtem Bityutskiy 		 */
1069839a8e86STejun Heo 		pages_written = writeback_inodes_wb(&bdi->wb, 1024,
1070839a8e86STejun Heo 						    WB_REASON_FORKER_THREAD);
1071839a8e86STejun Heo 		trace_writeback_pages_written(pages_written);
107203ba3782SJens Axboe 	}
107303ba3782SJens Axboe 
10746ca738d6SDerek Basehore 	if (!list_empty(&bdi->work_list))
10756ca738d6SDerek Basehore 		mod_delayed_work(bdi_wq, &wb->dwork, 0);
10766ca738d6SDerek Basehore 	else if (wb_has_dirty_io(wb) && dirty_writeback_interval)
10776ca738d6SDerek Basehore 		bdi_wakeup_thread_delayed(bdi);
1078455b2864SDave Chinner 
1079839a8e86STejun Heo 	current->flags &= ~PF_SWAPWRITE;
108003ba3782SJens Axboe }
108103ba3782SJens Axboe 
108203ba3782SJens Axboe /*
108303ba3782SJens Axboe  * Start writeback of `nr_pages' pages.  If `nr_pages' is zero, write back
108403ba3782SJens Axboe  * the whole world.
108503ba3782SJens Axboe  */
10860e175a18SCurt Wohlgemuth void wakeup_flusher_threads(long nr_pages, enum wb_reason reason)
108703ba3782SJens Axboe {
1088b8c2f347SChristoph Hellwig 	struct backing_dev_info *bdi;
1089b8c2f347SChristoph Hellwig 
109047df3ddeSJan Kara 	if (!nr_pages)
109147df3ddeSJan Kara 		nr_pages = get_nr_dirty_pages();
1092b8c2f347SChristoph Hellwig 
1093b8c2f347SChristoph Hellwig 	rcu_read_lock();
1094b8c2f347SChristoph Hellwig 	list_for_each_entry_rcu(bdi, &bdi_list, bdi_list) {
1095b8c2f347SChristoph Hellwig 		if (!bdi_has_dirty_io(bdi))
1096b8c2f347SChristoph Hellwig 			continue;
10970e175a18SCurt Wohlgemuth 		__bdi_start_writeback(bdi, nr_pages, false, reason);
1098b8c2f347SChristoph Hellwig 	}
1099b8c2f347SChristoph Hellwig 	rcu_read_unlock();
110003ba3782SJens Axboe }
110103ba3782SJens Axboe 
110203ba3782SJens Axboe static noinline void block_dump___mark_inode_dirty(struct inode *inode)
110303ba3782SJens Axboe {
110403ba3782SJens Axboe 	if (inode->i_ino || strcmp(inode->i_sb->s_id, "bdev")) {
110503ba3782SJens Axboe 		struct dentry *dentry;
110603ba3782SJens Axboe 		const char *name = "?";
110703ba3782SJens Axboe 
110803ba3782SJens Axboe 		dentry = d_find_alias(inode);
110903ba3782SJens Axboe 		if (dentry) {
111003ba3782SJens Axboe 			spin_lock(&dentry->d_lock);
111103ba3782SJens Axboe 			name = (const char *) dentry->d_name.name;
111203ba3782SJens Axboe 		}
111303ba3782SJens Axboe 		printk(KERN_DEBUG
111403ba3782SJens Axboe 		       "%s(%d): dirtied inode %lu (%s) on %s\n",
111503ba3782SJens Axboe 		       current->comm, task_pid_nr(current), inode->i_ino,
111603ba3782SJens Axboe 		       name, inode->i_sb->s_id);
111703ba3782SJens Axboe 		if (dentry) {
111803ba3782SJens Axboe 			spin_unlock(&dentry->d_lock);
111903ba3782SJens Axboe 			dput(dentry);
112003ba3782SJens Axboe 		}
112103ba3782SJens Axboe 	}
112203ba3782SJens Axboe }
112303ba3782SJens Axboe 
112403ba3782SJens Axboe /**
112503ba3782SJens Axboe  *	__mark_inode_dirty -	internal function
112603ba3782SJens Axboe  *	@inode: inode to mark
112703ba3782SJens Axboe  *	@flags: what kind of dirty (i.e. I_DIRTY_SYNC)
112803ba3782SJens Axboe  *	Mark an inode as dirty. Callers should use mark_inode_dirty or
112903ba3782SJens Axboe  *  	mark_inode_dirty_sync.
113003ba3782SJens Axboe  *
113103ba3782SJens Axboe  * Put the inode on the super block's dirty list.
113203ba3782SJens Axboe  *
113303ba3782SJens Axboe  * CAREFUL! We mark it dirty unconditionally, but move it onto the
113403ba3782SJens Axboe  * dirty list only if it is hashed or if it refers to a blockdev.
113503ba3782SJens Axboe  * If it was not hashed, it will never be added to the dirty list
113603ba3782SJens Axboe  * even if it is later hashed, as it will have been marked dirty already.
113703ba3782SJens Axboe  *
113803ba3782SJens Axboe  * In short, make sure you hash any inodes _before_ you start marking
113903ba3782SJens Axboe  * them dirty.
114003ba3782SJens Axboe  *
114103ba3782SJens Axboe  * Note that for blockdevs, inode->dirtied_when represents the dirtying time of
114203ba3782SJens Axboe  * the block-special inode (/dev/hda1) itself.  And the ->dirtied_when field of
114303ba3782SJens Axboe  * the kernel-internal blockdev inode represents the dirtying time of the
114403ba3782SJens Axboe  * blockdev's pages.  This is why for I_DIRTY_PAGES we always use
114503ba3782SJens Axboe  * page->mapping->host, so the page-dirtying time is recorded in the internal
114603ba3782SJens Axboe  * blockdev inode.
114703ba3782SJens Axboe  */
114803ba3782SJens Axboe void __mark_inode_dirty(struct inode *inode, int flags)
114903ba3782SJens Axboe {
115003ba3782SJens Axboe 	struct super_block *sb = inode->i_sb;
1151253c34e9SArtem Bityutskiy 	struct backing_dev_info *bdi = NULL;
115203ba3782SJens Axboe 
115303ba3782SJens Axboe 	/*
115403ba3782SJens Axboe 	 * Don't do this for I_DIRTY_PAGES - that doesn't actually
115503ba3782SJens Axboe 	 * dirty the inode itself
115603ba3782SJens Axboe 	 */
115703ba3782SJens Axboe 	if (flags & (I_DIRTY_SYNC | I_DIRTY_DATASYNC)) {
11589fb0a7daSTejun Heo 		trace_writeback_dirty_inode_start(inode, flags);
11599fb0a7daSTejun Heo 
116003ba3782SJens Axboe 		if (sb->s_op->dirty_inode)
1161aa385729SChristoph Hellwig 			sb->s_op->dirty_inode(inode, flags);
11629fb0a7daSTejun Heo 
11639fb0a7daSTejun Heo 		trace_writeback_dirty_inode(inode, flags);
116403ba3782SJens Axboe 	}
116503ba3782SJens Axboe 
116603ba3782SJens Axboe 	/*
11679c6ac78eSTejun Heo 	 * Paired with smp_mb() in __writeback_single_inode() for the
11689c6ac78eSTejun Heo 	 * following lockless i_state test.  See there for details.
116903ba3782SJens Axboe 	 */
117003ba3782SJens Axboe 	smp_mb();
117103ba3782SJens Axboe 
117203ba3782SJens Axboe 	if ((inode->i_state & flags) == flags)
117303ba3782SJens Axboe 		return;
117403ba3782SJens Axboe 
117503ba3782SJens Axboe 	if (unlikely(block_dump))
117603ba3782SJens Axboe 		block_dump___mark_inode_dirty(inode);
117703ba3782SJens Axboe 
1178250df6edSDave Chinner 	spin_lock(&inode->i_lock);
117903ba3782SJens Axboe 	if ((inode->i_state & flags) != flags) {
118003ba3782SJens Axboe 		const int was_dirty = inode->i_state & I_DIRTY;
118103ba3782SJens Axboe 
118203ba3782SJens Axboe 		inode->i_state |= flags;
118303ba3782SJens Axboe 
118403ba3782SJens Axboe 		/*
118503ba3782SJens Axboe 		 * If the inode is being synced, just update its dirty state.
118603ba3782SJens Axboe 		 * The unlocker will place the inode on the appropriate
118703ba3782SJens Axboe 		 * superblock list, based upon its state.
118803ba3782SJens Axboe 		 */
118903ba3782SJens Axboe 		if (inode->i_state & I_SYNC)
1190250df6edSDave Chinner 			goto out_unlock_inode;
119103ba3782SJens Axboe 
119203ba3782SJens Axboe 		/*
119303ba3782SJens Axboe 		 * Only add valid (hashed) inodes to the superblock's
119403ba3782SJens Axboe 		 * dirty list.  Add blockdev inodes as well.
119503ba3782SJens Axboe 		 */
119603ba3782SJens Axboe 		if (!S_ISBLK(inode->i_mode)) {
11971d3382cbSAl Viro 			if (inode_unhashed(inode))
1198250df6edSDave Chinner 				goto out_unlock_inode;
119903ba3782SJens Axboe 		}
1200a4ffdde6SAl Viro 		if (inode->i_state & I_FREEING)
1201250df6edSDave Chinner 			goto out_unlock_inode;
120203ba3782SJens Axboe 
120303ba3782SJens Axboe 		/*
120403ba3782SJens Axboe 		 * If the inode was already on b_dirty/b_io/b_more_io, don't
120503ba3782SJens Axboe 		 * reposition it (that would break b_dirty time-ordering).
120603ba3782SJens Axboe 		 */
120703ba3782SJens Axboe 		if (!was_dirty) {
1208a66979abSDave Chinner 			bool wakeup_bdi = false;
1209253c34e9SArtem Bityutskiy 			bdi = inode_to_bdi(inode);
1210500b067cSJens Axboe 
1211146d7009SJunxiao Bi 			spin_unlock(&inode->i_lock);
1212146d7009SJunxiao Bi 			spin_lock(&bdi->wb.list_lock);
1213253c34e9SArtem Bityutskiy 			if (bdi_cap_writeback_dirty(bdi)) {
1214253c34e9SArtem Bityutskiy 				WARN(!test_bit(BDI_registered, &bdi->state),
1215253c34e9SArtem Bityutskiy 				     "bdi-%s not registered\n", bdi->name);
1216253c34e9SArtem Bityutskiy 
1217253c34e9SArtem Bityutskiy 				/*
1218253c34e9SArtem Bityutskiy 				 * If this is the first dirty inode for this
1219253c34e9SArtem Bityutskiy 				 * bdi, we have to wake-up the corresponding
1220253c34e9SArtem Bityutskiy 				 * bdi thread to make sure background
1221253c34e9SArtem Bityutskiy 				 * write-back happens later.
1222253c34e9SArtem Bityutskiy 				 */
1223253c34e9SArtem Bityutskiy 				if (!wb_has_dirty_io(&bdi->wb))
1224253c34e9SArtem Bityutskiy 					wakeup_bdi = true;
1225500b067cSJens Axboe 			}
122603ba3782SJens Axboe 
122703ba3782SJens Axboe 			inode->dirtied_when = jiffies;
12287ccf19a8SNick Piggin 			list_move(&inode->i_wb_list, &bdi->wb.b_dirty);
1229f758eeabSChristoph Hellwig 			spin_unlock(&bdi->wb.list_lock);
1230253c34e9SArtem Bityutskiy 
1231253c34e9SArtem Bityutskiy 			if (wakeup_bdi)
12326467716aSArtem Bityutskiy 				bdi_wakeup_thread_delayed(bdi);
1233a66979abSDave Chinner 			return;
1234a66979abSDave Chinner 		}
1235a66979abSDave Chinner 	}
1236a66979abSDave Chinner out_unlock_inode:
1237a66979abSDave Chinner 	spin_unlock(&inode->i_lock);
1238a66979abSDave Chinner 
123903ba3782SJens Axboe }
124003ba3782SJens Axboe EXPORT_SYMBOL(__mark_inode_dirty);
124103ba3782SJens Axboe 
1242b6e51316SJens Axboe static void wait_sb_inodes(struct super_block *sb)
124366f3b8e2SJens Axboe {
124438f21977SNick Piggin 	struct inode *inode, *old_inode = NULL;
124538f21977SNick Piggin 
124603ba3782SJens Axboe 	/*
124703ba3782SJens Axboe 	 * We need to be protected against the filesystem going from
124803ba3782SJens Axboe 	 * r/o to r/w or vice versa.
124903ba3782SJens Axboe 	 */
1250b6e51316SJens Axboe 	WARN_ON(!rwsem_is_locked(&sb->s_umount));
125103ba3782SJens Axboe 
125255fa6091SDave Chinner 	spin_lock(&inode_sb_list_lock);
125366f3b8e2SJens Axboe 
125438f21977SNick Piggin 	/*
125538f21977SNick Piggin 	 * Data integrity sync. Must wait for all pages under writeback,
125638f21977SNick Piggin 	 * because there may have been pages dirtied before our sync
125738f21977SNick Piggin 	 * call, but which had writeout started before we write it out.
125838f21977SNick Piggin 	 * In which case, the inode may not be on the dirty list, but
125938f21977SNick Piggin 	 * we still have to wait for that writeout.
126038f21977SNick Piggin 	 */
1261b6e51316SJens Axboe 	list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
1262250df6edSDave Chinner 		struct address_space *mapping = inode->i_mapping;
126338f21977SNick Piggin 
1264250df6edSDave Chinner 		spin_lock(&inode->i_lock);
1265250df6edSDave Chinner 		if ((inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW)) ||
1266250df6edSDave Chinner 		    (mapping->nrpages == 0)) {
1267250df6edSDave Chinner 			spin_unlock(&inode->i_lock);
126838f21977SNick Piggin 			continue;
1269250df6edSDave Chinner 		}
127038f21977SNick Piggin 		__iget(inode);
1271250df6edSDave Chinner 		spin_unlock(&inode->i_lock);
127255fa6091SDave Chinner 		spin_unlock(&inode_sb_list_lock);
127355fa6091SDave Chinner 
127438f21977SNick Piggin 		/*
127555fa6091SDave Chinner 		 * We hold a reference to 'inode' so it couldn't have been
127655fa6091SDave Chinner 		 * removed from s_inodes list while we dropped the
127755fa6091SDave Chinner 		 * inode_sb_list_lock.  We cannot iput the inode now as we can
127855fa6091SDave Chinner 		 * be holding the last reference and we cannot iput it under
127955fa6091SDave Chinner 		 * inode_sb_list_lock. So we keep the reference and iput it
128055fa6091SDave Chinner 		 * later.
128138f21977SNick Piggin 		 */
128238f21977SNick Piggin 		iput(old_inode);
128338f21977SNick Piggin 		old_inode = inode;
128438f21977SNick Piggin 
128538f21977SNick Piggin 		filemap_fdatawait(mapping);
128638f21977SNick Piggin 
128738f21977SNick Piggin 		cond_resched();
128838f21977SNick Piggin 
128955fa6091SDave Chinner 		spin_lock(&inode_sb_list_lock);
129038f21977SNick Piggin 	}
129155fa6091SDave Chinner 	spin_unlock(&inode_sb_list_lock);
129238f21977SNick Piggin 	iput(old_inode);
129366f3b8e2SJens Axboe }
12941da177e4SLinus Torvalds 
1295d8a8559cSJens Axboe /**
12963259f8beSChris Mason  * writeback_inodes_sb_nr -	writeback dirty inodes from given super_block
1297d8a8559cSJens Axboe  * @sb: the superblock
12983259f8beSChris Mason  * @nr: the number of pages to write
1299786228abSMarcos Paulo de Souza  * @reason: reason why some writeback work initiated
13001da177e4SLinus Torvalds  *
1301d8a8559cSJens Axboe  * Start writeback on some inodes on this super_block. No guarantees are made
1302d8a8559cSJens Axboe  * on how many (if any) will be written, and this function does not wait
13033259f8beSChris Mason  * for IO completion of submitted IO.
13041da177e4SLinus Torvalds  */
13050e175a18SCurt Wohlgemuth void writeback_inodes_sb_nr(struct super_block *sb,
13060e175a18SCurt Wohlgemuth 			    unsigned long nr,
13070e175a18SCurt Wohlgemuth 			    enum wb_reason reason)
13081da177e4SLinus Torvalds {
130983ba7b07SChristoph Hellwig 	DECLARE_COMPLETION_ONSTACK(done);
131083ba7b07SChristoph Hellwig 	struct wb_writeback_work work = {
13113c4d7165SChristoph Hellwig 		.sb			= sb,
13123c4d7165SChristoph Hellwig 		.sync_mode		= WB_SYNC_NONE,
13136e6938b6SWu Fengguang 		.tagged_writepages	= 1,
131483ba7b07SChristoph Hellwig 		.done			= &done,
13153259f8beSChris Mason 		.nr_pages		= nr,
13160e175a18SCurt Wohlgemuth 		.reason			= reason,
13173c4d7165SChristoph Hellwig 	};
13180e3c9a22SJens Axboe 
13196eedc701SJan Kara 	if (sb->s_bdi == &noop_backing_dev_info)
13206eedc701SJan Kara 		return;
1321cf37e972SChristoph Hellwig 	WARN_ON(!rwsem_is_locked(&sb->s_umount));
132283ba7b07SChristoph Hellwig 	bdi_queue_work(sb->s_bdi, &work);
132383ba7b07SChristoph Hellwig 	wait_for_completion(&done);
13241da177e4SLinus Torvalds }
13253259f8beSChris Mason EXPORT_SYMBOL(writeback_inodes_sb_nr);
13263259f8beSChris Mason 
13273259f8beSChris Mason /**
13283259f8beSChris Mason  * writeback_inodes_sb	-	writeback dirty inodes from given super_block
13293259f8beSChris Mason  * @sb: the superblock
1330786228abSMarcos Paulo de Souza  * @reason: reason why some writeback work was initiated
13313259f8beSChris Mason  *
13323259f8beSChris Mason  * Start writeback on some inodes on this super_block. No guarantees are made
13333259f8beSChris Mason  * on how many (if any) will be written, and this function does not wait
13343259f8beSChris Mason  * for IO completion of submitted IO.
13353259f8beSChris Mason  */
13360e175a18SCurt Wohlgemuth void writeback_inodes_sb(struct super_block *sb, enum wb_reason reason)
13373259f8beSChris Mason {
13380e175a18SCurt Wohlgemuth 	return writeback_inodes_sb_nr(sb, get_nr_dirty_pages(), reason);
13393259f8beSChris Mason }
1340d8a8559cSJens Axboe EXPORT_SYMBOL(writeback_inodes_sb);
1341d8a8559cSJens Axboe 
1342d8a8559cSJens Axboe /**
134310ee27a0SMiao Xie  * try_to_writeback_inodes_sb_nr - try to start writeback if none underway
13443259f8beSChris Mason  * @sb: the superblock
13453259f8beSChris Mason  * @nr: the number of pages to write
134610ee27a0SMiao Xie  * @reason: the reason of writeback
13473259f8beSChris Mason  *
134810ee27a0SMiao Xie  * Invoke writeback_inodes_sb_nr if no writeback is currently underway.
13493259f8beSChris Mason  * Returns 1 if writeback was started, 0 if not.
13503259f8beSChris Mason  */
135110ee27a0SMiao Xie int try_to_writeback_inodes_sb_nr(struct super_block *sb,
13520e175a18SCurt Wohlgemuth 				  unsigned long nr,
13530e175a18SCurt Wohlgemuth 				  enum wb_reason reason)
13543259f8beSChris Mason {
135510ee27a0SMiao Xie 	if (writeback_in_progress(sb->s_bdi))
135610ee27a0SMiao Xie 		return 1;
135710ee27a0SMiao Xie 
135810ee27a0SMiao Xie 	if (!down_read_trylock(&sb->s_umount))
135910ee27a0SMiao Xie 		return 0;
136010ee27a0SMiao Xie 
13610e175a18SCurt Wohlgemuth 	writeback_inodes_sb_nr(sb, nr, reason);
13623259f8beSChris Mason 	up_read(&sb->s_umount);
13633259f8beSChris Mason 	return 1;
13643259f8beSChris Mason }
136510ee27a0SMiao Xie EXPORT_SYMBOL(try_to_writeback_inodes_sb_nr);
136610ee27a0SMiao Xie 
136710ee27a0SMiao Xie /**
136810ee27a0SMiao Xie  * try_to_writeback_inodes_sb - try to start writeback if none underway
136910ee27a0SMiao Xie  * @sb: the superblock
137010ee27a0SMiao Xie  * @reason: reason why some writeback work was initiated
137110ee27a0SMiao Xie  *
137210ee27a0SMiao Xie  * Implement by try_to_writeback_inodes_sb_nr()
137310ee27a0SMiao Xie  * Returns 1 if writeback was started, 0 if not.
137410ee27a0SMiao Xie  */
137510ee27a0SMiao Xie int try_to_writeback_inodes_sb(struct super_block *sb, enum wb_reason reason)
137610ee27a0SMiao Xie {
137710ee27a0SMiao Xie 	return try_to_writeback_inodes_sb_nr(sb, get_nr_dirty_pages(), reason);
137810ee27a0SMiao Xie }
137910ee27a0SMiao Xie EXPORT_SYMBOL(try_to_writeback_inodes_sb);
13803259f8beSChris Mason 
13813259f8beSChris Mason /**
1382d8a8559cSJens Axboe  * sync_inodes_sb	-	sync sb inode pages
1383d8a8559cSJens Axboe  * @sb: the superblock
1384d8a8559cSJens Axboe  *
1385d8a8559cSJens Axboe  * This function writes and waits on any dirty inode belonging to this
13860dc83bd3SJan Kara  * super_block.
1387d8a8559cSJens Axboe  */
13880dc83bd3SJan Kara void sync_inodes_sb(struct super_block *sb)
1389d8a8559cSJens Axboe {
139083ba7b07SChristoph Hellwig 	DECLARE_COMPLETION_ONSTACK(done);
139183ba7b07SChristoph Hellwig 	struct wb_writeback_work work = {
13923c4d7165SChristoph Hellwig 		.sb		= sb,
13933c4d7165SChristoph Hellwig 		.sync_mode	= WB_SYNC_ALL,
13943c4d7165SChristoph Hellwig 		.nr_pages	= LONG_MAX,
13953c4d7165SChristoph Hellwig 		.range_cyclic	= 0,
139683ba7b07SChristoph Hellwig 		.done		= &done,
13970e175a18SCurt Wohlgemuth 		.reason		= WB_REASON_SYNC,
13987747bd4bSDave Chinner 		.for_sync	= 1,
13993c4d7165SChristoph Hellwig 	};
14003c4d7165SChristoph Hellwig 
14016eedc701SJan Kara 	/* Nothing to do? */
14026eedc701SJan Kara 	if (sb->s_bdi == &noop_backing_dev_info)
14036eedc701SJan Kara 		return;
1404cf37e972SChristoph Hellwig 	WARN_ON(!rwsem_is_locked(&sb->s_umount));
1405cf37e972SChristoph Hellwig 
140683ba7b07SChristoph Hellwig 	bdi_queue_work(sb->s_bdi, &work);
140783ba7b07SChristoph Hellwig 	wait_for_completion(&done);
140883ba7b07SChristoph Hellwig 
1409b6e51316SJens Axboe 	wait_sb_inodes(sb);
1410d8a8559cSJens Axboe }
1411d8a8559cSJens Axboe EXPORT_SYMBOL(sync_inodes_sb);
14121da177e4SLinus Torvalds 
14131da177e4SLinus Torvalds /**
14141da177e4SLinus Torvalds  * write_inode_now	-	write an inode to disk
14151da177e4SLinus Torvalds  * @inode: inode to write to disk
14161da177e4SLinus Torvalds  * @sync: whether the write should be synchronous or not
14171da177e4SLinus Torvalds  *
14187f04c26dSAndrea Arcangeli  * This function commits an inode to disk immediately if it is dirty. This is
14197f04c26dSAndrea Arcangeli  * primarily needed by knfsd.
14207f04c26dSAndrea Arcangeli  *
14217f04c26dSAndrea Arcangeli  * The caller must either have a ref on the inode or must have set I_WILL_FREE.
14221da177e4SLinus Torvalds  */
14231da177e4SLinus Torvalds int write_inode_now(struct inode *inode, int sync)
14241da177e4SLinus Torvalds {
1425f758eeabSChristoph Hellwig 	struct bdi_writeback *wb = &inode_to_bdi(inode)->wb;
14261da177e4SLinus Torvalds 	struct writeback_control wbc = {
14271da177e4SLinus Torvalds 		.nr_to_write = LONG_MAX,
142818914b18SMike Galbraith 		.sync_mode = sync ? WB_SYNC_ALL : WB_SYNC_NONE,
1429111ebb6eSOGAWA Hirofumi 		.range_start = 0,
1430111ebb6eSOGAWA Hirofumi 		.range_end = LLONG_MAX,
14311da177e4SLinus Torvalds 	};
14321da177e4SLinus Torvalds 
14331da177e4SLinus Torvalds 	if (!mapping_cap_writeback_dirty(inode->i_mapping))
143449364ce2SAndrew Morton 		wbc.nr_to_write = 0;
14351da177e4SLinus Torvalds 
14361da177e4SLinus Torvalds 	might_sleep();
14374f8ad655SJan Kara 	return writeback_single_inode(inode, wb, &wbc);
14381da177e4SLinus Torvalds }
14391da177e4SLinus Torvalds EXPORT_SYMBOL(write_inode_now);
14401da177e4SLinus Torvalds 
14411da177e4SLinus Torvalds /**
14421da177e4SLinus Torvalds  * sync_inode - write an inode and its pages to disk.
14431da177e4SLinus Torvalds  * @inode: the inode to sync
14441da177e4SLinus Torvalds  * @wbc: controls the writeback mode
14451da177e4SLinus Torvalds  *
14461da177e4SLinus Torvalds  * sync_inode() will write an inode and its pages to disk.  It will also
14471da177e4SLinus Torvalds  * correctly update the inode on its superblock's dirty inode lists and will
14481da177e4SLinus Torvalds  * update inode->i_state.
14491da177e4SLinus Torvalds  *
14501da177e4SLinus Torvalds  * The caller must have a ref on the inode.
14511da177e4SLinus Torvalds  */
14521da177e4SLinus Torvalds int sync_inode(struct inode *inode, struct writeback_control *wbc)
14531da177e4SLinus Torvalds {
14544f8ad655SJan Kara 	return writeback_single_inode(inode, &inode_to_bdi(inode)->wb, wbc);
14551da177e4SLinus Torvalds }
14561da177e4SLinus Torvalds EXPORT_SYMBOL(sync_inode);
1457c3765016SChristoph Hellwig 
1458c3765016SChristoph Hellwig /**
1459c691b9d9SAndrew Morton  * sync_inode_metadata - write an inode to disk
1460c3765016SChristoph Hellwig  * @inode: the inode to sync
1461c3765016SChristoph Hellwig  * @wait: wait for I/O to complete.
1462c3765016SChristoph Hellwig  *
1463c691b9d9SAndrew Morton  * Write an inode to disk and adjust its dirty state after completion.
1464c3765016SChristoph Hellwig  *
1465c3765016SChristoph Hellwig  * Note: only writes the actual inode, no associated data or other metadata.
1466c3765016SChristoph Hellwig  */
1467c3765016SChristoph Hellwig int sync_inode_metadata(struct inode *inode, int wait)
1468c3765016SChristoph Hellwig {
1469c3765016SChristoph Hellwig 	struct writeback_control wbc = {
1470c3765016SChristoph Hellwig 		.sync_mode = wait ? WB_SYNC_ALL : WB_SYNC_NONE,
1471c3765016SChristoph Hellwig 		.nr_to_write = 0, /* metadata-only */
1472c3765016SChristoph Hellwig 	};
1473c3765016SChristoph Hellwig 
1474c3765016SChristoph Hellwig 	return sync_inode(inode, &wbc);
1475c3765016SChristoph Hellwig }
1476c3765016SChristoph Hellwig EXPORT_SYMBOL(sync_inode_metadata);
1477