xref: /openbmc/linux/fs/fs-writeback.c (revision eb6ef3df)
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 
69de1414a6SChristoph Hellwig struct backing_dev_info *inode_to_bdi(struct inode *inode)
70692ebd17SJan Kara {
71b520252aSJens Axboe 	struct super_block *sb;
72b520252aSJens Axboe 
73b520252aSJens Axboe 	if (!inode)
74b520252aSJens Axboe 		return &noop_backing_dev_info;
75b520252aSJens Axboe 
76b520252aSJens Axboe 	sb = inode->i_sb;
77495a276eSChristoph Hellwig #ifdef CONFIG_BLOCK
78a8855990SJan Kara 	if (sb_is_blkdev_sb(sb))
79495a276eSChristoph Hellwig 		return blk_get_backing_dev_info(I_BDEV(inode));
80495a276eSChristoph Hellwig #endif
81692ebd17SJan Kara 	return sb->s_bdi;
82692ebd17SJan Kara }
83de1414a6SChristoph Hellwig EXPORT_SYMBOL_GPL(inode_to_bdi);
84692ebd17SJan Kara 
857ccf19a8SNick Piggin static inline struct inode *wb_inode(struct list_head *head)
867ccf19a8SNick Piggin {
877ccf19a8SNick Piggin 	return list_entry(head, struct inode, i_wb_list);
887ccf19a8SNick Piggin }
897ccf19a8SNick Piggin 
9015eb77a0SWu Fengguang /*
9115eb77a0SWu Fengguang  * Include the creation of the trace points after defining the
9215eb77a0SWu Fengguang  * wb_writeback_work structure and inline functions so that the definition
9315eb77a0SWu Fengguang  * remains local to this file.
9415eb77a0SWu Fengguang  */
9515eb77a0SWu Fengguang #define CREATE_TRACE_POINTS
9615eb77a0SWu Fengguang #include <trace/events/writeback.h>
9715eb77a0SWu Fengguang 
98774016b2SSteven Whitehouse EXPORT_TRACEPOINT_SYMBOL_GPL(wbc_writepage);
99774016b2SSteven Whitehouse 
1005acda9d1SJan Kara static void bdi_wakeup_thread(struct backing_dev_info *bdi)
1015acda9d1SJan Kara {
1025acda9d1SJan Kara 	spin_lock_bh(&bdi->wb_lock);
1035acda9d1SJan Kara 	if (test_bit(BDI_registered, &bdi->state))
1045acda9d1SJan Kara 		mod_delayed_work(bdi_wq, &bdi->wb.dwork, 0);
1055acda9d1SJan Kara 	spin_unlock_bh(&bdi->wb_lock);
1065acda9d1SJan Kara }
1075acda9d1SJan Kara 
1086585027aSJan Kara static void bdi_queue_work(struct backing_dev_info *bdi,
1096585027aSJan Kara 			   struct wb_writeback_work *work)
1106585027aSJan Kara {
1116585027aSJan Kara 	trace_writeback_queue(bdi, work);
1126585027aSJan Kara 
1136585027aSJan Kara 	spin_lock_bh(&bdi->wb_lock);
1145acda9d1SJan Kara 	if (!test_bit(BDI_registered, &bdi->state)) {
1155acda9d1SJan Kara 		if (work->done)
1165acda9d1SJan Kara 			complete(work->done);
1175acda9d1SJan Kara 		goto out_unlock;
1185acda9d1SJan Kara 	}
1196585027aSJan Kara 	list_add_tail(&work->list, &bdi->work_list);
120839a8e86STejun Heo 	mod_delayed_work(bdi_wq, &bdi->wb.dwork, 0);
1215acda9d1SJan Kara out_unlock:
1225acda9d1SJan Kara 	spin_unlock_bh(&bdi->wb_lock);
12303ba3782SJens Axboe }
1241da177e4SLinus Torvalds 
12583ba7b07SChristoph Hellwig static void
12683ba7b07SChristoph Hellwig __bdi_start_writeback(struct backing_dev_info *bdi, long nr_pages,
1270e175a18SCurt Wohlgemuth 		      bool range_cyclic, enum wb_reason reason)
1281da177e4SLinus Torvalds {
12983ba7b07SChristoph Hellwig 	struct wb_writeback_work *work;
13003ba3782SJens Axboe 
131bcddc3f0SJens Axboe 	/*
132bcddc3f0SJens Axboe 	 * This is WB_SYNC_NONE writeback, so if allocation fails just
133bcddc3f0SJens Axboe 	 * wakeup the thread for old dirty data writeback
134bcddc3f0SJens Axboe 	 */
13583ba7b07SChristoph Hellwig 	work = kzalloc(sizeof(*work), GFP_ATOMIC);
13683ba7b07SChristoph Hellwig 	if (!work) {
137455b2864SDave Chinner 		trace_writeback_nowork(bdi);
1385acda9d1SJan Kara 		bdi_wakeup_thread(bdi);
13983ba7b07SChristoph Hellwig 		return;
14083ba7b07SChristoph Hellwig 	}
14183ba7b07SChristoph Hellwig 
14283ba7b07SChristoph Hellwig 	work->sync_mode	= WB_SYNC_NONE;
14383ba7b07SChristoph Hellwig 	work->nr_pages	= nr_pages;
14483ba7b07SChristoph Hellwig 	work->range_cyclic = range_cyclic;
1450e175a18SCurt Wohlgemuth 	work->reason	= reason;
14683ba7b07SChristoph Hellwig 
147f11fcae8SJens Axboe 	bdi_queue_work(bdi, work);
14803ba3782SJens Axboe }
149b6e51316SJens Axboe 
150b6e51316SJens Axboe /**
151b6e51316SJens Axboe  * bdi_start_writeback - start writeback
152b6e51316SJens Axboe  * @bdi: the backing device to write from
153b6e51316SJens Axboe  * @nr_pages: the number of pages to write
154786228abSMarcos Paulo de Souza  * @reason: reason why some writeback work was initiated
155b6e51316SJens Axboe  *
156b6e51316SJens Axboe  * Description:
157b6e51316SJens Axboe  *   This does WB_SYNC_NONE opportunistic writeback. The IO is only
15825985edcSLucas De Marchi  *   started when this function returns, we make no guarantees on
1590e3c9a22SJens Axboe  *   completion. Caller need not hold sb s_umount semaphore.
160b6e51316SJens Axboe  *
161b6e51316SJens Axboe  */
1620e175a18SCurt Wohlgemuth void bdi_start_writeback(struct backing_dev_info *bdi, long nr_pages,
1630e175a18SCurt Wohlgemuth 			enum wb_reason reason)
164b6e51316SJens Axboe {
1650e175a18SCurt Wohlgemuth 	__bdi_start_writeback(bdi, nr_pages, true, reason);
166d3ddec76SWu Fengguang }
167d3ddec76SWu Fengguang 
168c5444198SChristoph Hellwig /**
169c5444198SChristoph Hellwig  * bdi_start_background_writeback - start background writeback
170c5444198SChristoph Hellwig  * @bdi: the backing device to write from
171c5444198SChristoph Hellwig  *
172c5444198SChristoph Hellwig  * Description:
1736585027aSJan Kara  *   This makes sure WB_SYNC_NONE background writeback happens. When
1746585027aSJan Kara  *   this function returns, it is only guaranteed that for given BDI
1756585027aSJan Kara  *   some IO is happening if we are over background dirty threshold.
1766585027aSJan Kara  *   Caller need not hold sb s_umount semaphore.
177c5444198SChristoph Hellwig  */
178c5444198SChristoph Hellwig void bdi_start_background_writeback(struct backing_dev_info *bdi)
179c5444198SChristoph Hellwig {
1806585027aSJan Kara 	/*
1816585027aSJan Kara 	 * We just wake up the flusher thread. It will perform background
1826585027aSJan Kara 	 * writeback as soon as there is no other work to do.
1836585027aSJan Kara 	 */
18471927e84SWu Fengguang 	trace_writeback_wake_background(bdi);
1855acda9d1SJan Kara 	bdi_wakeup_thread(bdi);
1861da177e4SLinus Torvalds }
1871da177e4SLinus Torvalds 
1881da177e4SLinus Torvalds /*
189a66979abSDave Chinner  * Remove the inode from the writeback list it is on.
190a66979abSDave Chinner  */
191a66979abSDave Chinner void inode_wb_list_del(struct inode *inode)
192a66979abSDave Chinner {
193f758eeabSChristoph Hellwig 	struct backing_dev_info *bdi = inode_to_bdi(inode);
194a66979abSDave Chinner 
195f758eeabSChristoph Hellwig 	spin_lock(&bdi->wb.list_lock);
196f758eeabSChristoph Hellwig 	list_del_init(&inode->i_wb_list);
197f758eeabSChristoph Hellwig 	spin_unlock(&bdi->wb.list_lock);
198f758eeabSChristoph Hellwig }
199a66979abSDave Chinner 
200a66979abSDave Chinner /*
2016610a0bcSAndrew Morton  * Redirty an inode: set its when-it-was dirtied timestamp and move it to the
2026610a0bcSAndrew Morton  * furthest end of its superblock's dirty-inode list.
2036610a0bcSAndrew Morton  *
2046610a0bcSAndrew Morton  * Before stamping the inode's ->dirtied_when, we check to see whether it is
20566f3b8e2SJens Axboe  * already the most-recently-dirtied inode on the b_dirty list.  If that is
2066610a0bcSAndrew Morton  * the case then the inode must have been redirtied while it was being written
2076610a0bcSAndrew Morton  * out and we don't reset its dirtied_when.
2086610a0bcSAndrew Morton  */
209f758eeabSChristoph Hellwig static void redirty_tail(struct inode *inode, struct bdi_writeback *wb)
2106610a0bcSAndrew Morton {
211f758eeabSChristoph Hellwig 	assert_spin_locked(&wb->list_lock);
21203ba3782SJens Axboe 	if (!list_empty(&wb->b_dirty)) {
21366f3b8e2SJens Axboe 		struct inode *tail;
2146610a0bcSAndrew Morton 
2157ccf19a8SNick Piggin 		tail = wb_inode(wb->b_dirty.next);
21666f3b8e2SJens Axboe 		if (time_before(inode->dirtied_when, tail->dirtied_when))
2176610a0bcSAndrew Morton 			inode->dirtied_when = jiffies;
2186610a0bcSAndrew Morton 	}
2197ccf19a8SNick Piggin 	list_move(&inode->i_wb_list, &wb->b_dirty);
2206610a0bcSAndrew Morton }
2216610a0bcSAndrew Morton 
2226610a0bcSAndrew Morton /*
22366f3b8e2SJens Axboe  * requeue inode for re-scanning after bdi->b_io list is exhausted.
224c986d1e2SAndrew Morton  */
225f758eeabSChristoph Hellwig static void requeue_io(struct inode *inode, struct bdi_writeback *wb)
226c986d1e2SAndrew Morton {
227f758eeabSChristoph Hellwig 	assert_spin_locked(&wb->list_lock);
2287ccf19a8SNick Piggin 	list_move(&inode->i_wb_list, &wb->b_more_io);
229c986d1e2SAndrew Morton }
230c986d1e2SAndrew Morton 
2311c0eeaf5SJoern Engel static void inode_sync_complete(struct inode *inode)
2321c0eeaf5SJoern Engel {
233365b94aeSJan Kara 	inode->i_state &= ~I_SYNC;
2344eff96ddSJan Kara 	/* If inode is clean an unused, put it into LRU now... */
2354eff96ddSJan Kara 	inode_add_lru(inode);
236365b94aeSJan Kara 	/* Waiters must see I_SYNC cleared before being woken up */
2371c0eeaf5SJoern Engel 	smp_mb();
2381c0eeaf5SJoern Engel 	wake_up_bit(&inode->i_state, __I_SYNC);
2391c0eeaf5SJoern Engel }
2401c0eeaf5SJoern Engel 
241d2caa3c5SJeff Layton static bool inode_dirtied_after(struct inode *inode, unsigned long t)
242d2caa3c5SJeff Layton {
243d2caa3c5SJeff Layton 	bool ret = time_after(inode->dirtied_when, t);
244d2caa3c5SJeff Layton #ifndef CONFIG_64BIT
245d2caa3c5SJeff Layton 	/*
246d2caa3c5SJeff Layton 	 * For inodes being constantly redirtied, dirtied_when can get stuck.
247d2caa3c5SJeff Layton 	 * It _appears_ to be in the future, but is actually in distant past.
248d2caa3c5SJeff Layton 	 * This test is necessary to prevent such wrapped-around relative times
2495b0830cbSJens Axboe 	 * from permanently stopping the whole bdi writeback.
250d2caa3c5SJeff Layton 	 */
251d2caa3c5SJeff Layton 	ret = ret && time_before_eq(inode->dirtied_when, jiffies);
252d2caa3c5SJeff Layton #endif
253d2caa3c5SJeff Layton 	return ret;
254d2caa3c5SJeff Layton }
255d2caa3c5SJeff Layton 
2560ae45f63STheodore Ts'o #define EXPIRE_DIRTY_ATIME 0x0001
2570ae45f63STheodore Ts'o 
258c986d1e2SAndrew Morton /*
2590e2f2b23SWang Sheng-Hui  * Move expired (dirtied before work->older_than_this) dirty inodes from
260697e6fedSJan Kara  * @delaying_queue to @dispatch_queue.
2612c136579SFengguang Wu  */
262e84d0a4fSWu Fengguang static int move_expired_inodes(struct list_head *delaying_queue,
2632c136579SFengguang Wu 			       struct list_head *dispatch_queue,
2640ae45f63STheodore Ts'o 			       int flags,
265ad4e38ddSCurt Wohlgemuth 			       struct wb_writeback_work *work)
2662c136579SFengguang Wu {
2670ae45f63STheodore Ts'o 	unsigned long *older_than_this = NULL;
2680ae45f63STheodore Ts'o 	unsigned long expire_time;
2695c03449dSShaohua Li 	LIST_HEAD(tmp);
2705c03449dSShaohua Li 	struct list_head *pos, *node;
271cf137307SJens Axboe 	struct super_block *sb = NULL;
2725c03449dSShaohua Li 	struct inode *inode;
273cf137307SJens Axboe 	int do_sb_sort = 0;
274e84d0a4fSWu Fengguang 	int moved = 0;
2755c03449dSShaohua Li 
2760ae45f63STheodore Ts'o 	if ((flags & EXPIRE_DIRTY_ATIME) == 0)
2770ae45f63STheodore Ts'o 		older_than_this = work->older_than_this;
2780ae45f63STheodore Ts'o 	else if ((work->reason == WB_REASON_SYNC) == 0) {
2790ae45f63STheodore Ts'o 		expire_time = jiffies - (HZ * 86400);
2800ae45f63STheodore Ts'o 		older_than_this = &expire_time;
2810ae45f63STheodore Ts'o 	}
2822c136579SFengguang Wu 	while (!list_empty(delaying_queue)) {
2837ccf19a8SNick Piggin 		inode = wb_inode(delaying_queue->prev);
2840ae45f63STheodore Ts'o 		if (older_than_this &&
2850ae45f63STheodore Ts'o 		    inode_dirtied_after(inode, *older_than_this))
2862c136579SFengguang Wu 			break;
287a8855990SJan Kara 		list_move(&inode->i_wb_list, &tmp);
288a8855990SJan Kara 		moved++;
2890ae45f63STheodore Ts'o 		if (flags & EXPIRE_DIRTY_ATIME)
2900ae45f63STheodore Ts'o 			set_bit(__I_DIRTY_TIME_EXPIRED, &inode->i_state);
291a8855990SJan Kara 		if (sb_is_blkdev_sb(inode->i_sb))
292a8855990SJan Kara 			continue;
293cf137307SJens Axboe 		if (sb && sb != inode->i_sb)
294cf137307SJens Axboe 			do_sb_sort = 1;
295cf137307SJens Axboe 		sb = inode->i_sb;
2965c03449dSShaohua Li 	}
2975c03449dSShaohua Li 
298cf137307SJens Axboe 	/* just one sb in list, splice to dispatch_queue and we're done */
299cf137307SJens Axboe 	if (!do_sb_sort) {
300cf137307SJens Axboe 		list_splice(&tmp, dispatch_queue);
301e84d0a4fSWu Fengguang 		goto out;
302cf137307SJens Axboe 	}
303cf137307SJens Axboe 
3045c03449dSShaohua Li 	/* Move inodes from one superblock together */
3055c03449dSShaohua Li 	while (!list_empty(&tmp)) {
3067ccf19a8SNick Piggin 		sb = wb_inode(tmp.prev)->i_sb;
3075c03449dSShaohua Li 		list_for_each_prev_safe(pos, node, &tmp) {
3087ccf19a8SNick Piggin 			inode = wb_inode(pos);
3095c03449dSShaohua Li 			if (inode->i_sb == sb)
3107ccf19a8SNick Piggin 				list_move(&inode->i_wb_list, dispatch_queue);
3112c136579SFengguang Wu 		}
3122c136579SFengguang Wu 	}
313e84d0a4fSWu Fengguang out:
314e84d0a4fSWu Fengguang 	return moved;
3155c03449dSShaohua Li }
3162c136579SFengguang Wu 
3172c136579SFengguang Wu /*
3182c136579SFengguang Wu  * Queue all expired dirty inodes for io, eldest first.
3194ea879b9SWu Fengguang  * Before
3204ea879b9SWu Fengguang  *         newly dirtied     b_dirty    b_io    b_more_io
3214ea879b9SWu Fengguang  *         =============>    gf         edc     BA
3224ea879b9SWu Fengguang  * After
3234ea879b9SWu Fengguang  *         newly dirtied     b_dirty    b_io    b_more_io
3244ea879b9SWu Fengguang  *         =============>    g          fBAedc
3254ea879b9SWu Fengguang  *                                           |
3264ea879b9SWu Fengguang  *                                           +--> dequeue for IO
3272c136579SFengguang Wu  */
328ad4e38ddSCurt Wohlgemuth static void queue_io(struct bdi_writeback *wb, struct wb_writeback_work *work)
3292c136579SFengguang Wu {
330e84d0a4fSWu Fengguang 	int moved;
3310ae45f63STheodore Ts'o 
332f758eeabSChristoph Hellwig 	assert_spin_locked(&wb->list_lock);
3334ea879b9SWu Fengguang 	list_splice_init(&wb->b_more_io, &wb->b_io);
3340ae45f63STheodore Ts'o 	moved = move_expired_inodes(&wb->b_dirty, &wb->b_io, 0, work);
3350ae45f63STheodore Ts'o 	moved += move_expired_inodes(&wb->b_dirty_time, &wb->b_io,
3360ae45f63STheodore Ts'o 				     EXPIRE_DIRTY_ATIME, work);
337ad4e38ddSCurt Wohlgemuth 	trace_writeback_queue_io(wb, work, moved);
33866f3b8e2SJens Axboe }
33966f3b8e2SJens Axboe 
340a9185b41SChristoph Hellwig static int write_inode(struct inode *inode, struct writeback_control *wbc)
34166f3b8e2SJens Axboe {
3429fb0a7daSTejun Heo 	int ret;
3439fb0a7daSTejun Heo 
3449fb0a7daSTejun Heo 	if (inode->i_sb->s_op->write_inode && !is_bad_inode(inode)) {
3459fb0a7daSTejun Heo 		trace_writeback_write_inode_start(inode, wbc);
3469fb0a7daSTejun Heo 		ret = inode->i_sb->s_op->write_inode(inode, wbc);
3479fb0a7daSTejun Heo 		trace_writeback_write_inode(inode, wbc);
3489fb0a7daSTejun Heo 		return ret;
3499fb0a7daSTejun Heo 	}
35003ba3782SJens Axboe 	return 0;
35166f3b8e2SJens Axboe }
35208d8e974SFengguang Wu 
3532c136579SFengguang Wu /*
354169ebd90SJan Kara  * Wait for writeback on an inode to complete. Called with i_lock held.
355169ebd90SJan Kara  * Caller must make sure inode cannot go away when we drop i_lock.
35601c03194SChristoph Hellwig  */
357169ebd90SJan Kara static void __inode_wait_for_writeback(struct inode *inode)
358169ebd90SJan Kara 	__releases(inode->i_lock)
359169ebd90SJan Kara 	__acquires(inode->i_lock)
36001c03194SChristoph Hellwig {
36101c03194SChristoph Hellwig 	DEFINE_WAIT_BIT(wq, &inode->i_state, __I_SYNC);
36201c03194SChristoph Hellwig 	wait_queue_head_t *wqh;
36301c03194SChristoph Hellwig 
36401c03194SChristoph Hellwig 	wqh = bit_waitqueue(&inode->i_state, __I_SYNC);
36558a9d3d8SRichard Kennedy 	while (inode->i_state & I_SYNC) {
366250df6edSDave Chinner 		spin_unlock(&inode->i_lock);
36774316201SNeilBrown 		__wait_on_bit(wqh, &wq, bit_wait,
36874316201SNeilBrown 			      TASK_UNINTERRUPTIBLE);
369250df6edSDave Chinner 		spin_lock(&inode->i_lock);
37058a9d3d8SRichard Kennedy 	}
37101c03194SChristoph Hellwig }
37201c03194SChristoph Hellwig 
37301c03194SChristoph Hellwig /*
374169ebd90SJan Kara  * Wait for writeback on an inode to complete. Caller must have inode pinned.
375169ebd90SJan Kara  */
376169ebd90SJan Kara void inode_wait_for_writeback(struct inode *inode)
377169ebd90SJan Kara {
378169ebd90SJan Kara 	spin_lock(&inode->i_lock);
379169ebd90SJan Kara 	__inode_wait_for_writeback(inode);
380169ebd90SJan Kara 	spin_unlock(&inode->i_lock);
381169ebd90SJan Kara }
382169ebd90SJan Kara 
383169ebd90SJan Kara /*
384169ebd90SJan Kara  * Sleep until I_SYNC is cleared. This function must be called with i_lock
385169ebd90SJan Kara  * held and drops it. It is aimed for callers not holding any inode reference
386169ebd90SJan Kara  * so once i_lock is dropped, inode can go away.
387169ebd90SJan Kara  */
388169ebd90SJan Kara static void inode_sleep_on_writeback(struct inode *inode)
389169ebd90SJan Kara 	__releases(inode->i_lock)
390169ebd90SJan Kara {
391169ebd90SJan Kara 	DEFINE_WAIT(wait);
392169ebd90SJan Kara 	wait_queue_head_t *wqh = bit_waitqueue(&inode->i_state, __I_SYNC);
393169ebd90SJan Kara 	int sleep;
394169ebd90SJan Kara 
395169ebd90SJan Kara 	prepare_to_wait(wqh, &wait, TASK_UNINTERRUPTIBLE);
396169ebd90SJan Kara 	sleep = inode->i_state & I_SYNC;
397169ebd90SJan Kara 	spin_unlock(&inode->i_lock);
398169ebd90SJan Kara 	if (sleep)
399169ebd90SJan Kara 		schedule();
400169ebd90SJan Kara 	finish_wait(wqh, &wait);
401169ebd90SJan Kara }
402169ebd90SJan Kara 
403169ebd90SJan Kara /*
404ccb26b5aSJan Kara  * Find proper writeback list for the inode depending on its current state and
405ccb26b5aSJan Kara  * possibly also change of its state while we were doing writeback.  Here we
406ccb26b5aSJan Kara  * handle things such as livelock prevention or fairness of writeback among
407ccb26b5aSJan Kara  * inodes. This function can be called only by flusher thread - noone else
408ccb26b5aSJan Kara  * processes all inodes in writeback lists and requeueing inodes behind flusher
409ccb26b5aSJan Kara  * thread's back can have unexpected consequences.
410ccb26b5aSJan Kara  */
411ccb26b5aSJan Kara static void requeue_inode(struct inode *inode, struct bdi_writeback *wb,
412ccb26b5aSJan Kara 			  struct writeback_control *wbc)
413ccb26b5aSJan Kara {
414ccb26b5aSJan Kara 	if (inode->i_state & I_FREEING)
415ccb26b5aSJan Kara 		return;
416ccb26b5aSJan Kara 
417ccb26b5aSJan Kara 	/*
418ccb26b5aSJan Kara 	 * Sync livelock prevention. Each inode is tagged and synced in one
419ccb26b5aSJan Kara 	 * shot. If still dirty, it will be redirty_tail()'ed below.  Update
420ccb26b5aSJan Kara 	 * the dirty time to prevent enqueue and sync it again.
421ccb26b5aSJan Kara 	 */
422ccb26b5aSJan Kara 	if ((inode->i_state & I_DIRTY) &&
423ccb26b5aSJan Kara 	    (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages))
424ccb26b5aSJan Kara 		inode->dirtied_when = jiffies;
425ccb26b5aSJan Kara 
4264f8ad655SJan Kara 	if (wbc->pages_skipped) {
4274f8ad655SJan Kara 		/*
4284f8ad655SJan Kara 		 * writeback is not making progress due to locked
4294f8ad655SJan Kara 		 * buffers. Skip this inode for now.
4304f8ad655SJan Kara 		 */
4314f8ad655SJan Kara 		redirty_tail(inode, wb);
4324f8ad655SJan Kara 		return;
4334f8ad655SJan Kara 	}
4344f8ad655SJan Kara 
435ccb26b5aSJan Kara 	if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY)) {
436ccb26b5aSJan Kara 		/*
437ccb26b5aSJan Kara 		 * We didn't write back all the pages.  nfs_writepages()
438ccb26b5aSJan Kara 		 * sometimes bales out without doing anything.
439ccb26b5aSJan Kara 		 */
440ccb26b5aSJan Kara 		if (wbc->nr_to_write <= 0) {
441ccb26b5aSJan Kara 			/* Slice used up. Queue for next turn. */
442ccb26b5aSJan Kara 			requeue_io(inode, wb);
443ccb26b5aSJan Kara 		} else {
444ccb26b5aSJan Kara 			/*
445ccb26b5aSJan Kara 			 * Writeback blocked by something other than
446ccb26b5aSJan Kara 			 * congestion. Delay the inode for some time to
447ccb26b5aSJan Kara 			 * avoid spinning on the CPU (100% iowait)
448ccb26b5aSJan Kara 			 * retrying writeback of the dirty page/inode
449ccb26b5aSJan Kara 			 * that cannot be performed immediately.
450ccb26b5aSJan Kara 			 */
451ccb26b5aSJan Kara 			redirty_tail(inode, wb);
452ccb26b5aSJan Kara 		}
453ccb26b5aSJan Kara 	} else if (inode->i_state & I_DIRTY) {
454ccb26b5aSJan Kara 		/*
455ccb26b5aSJan Kara 		 * Filesystems can dirty the inode during writeback operations,
456ccb26b5aSJan Kara 		 * such as delayed allocation during submission or metadata
457ccb26b5aSJan Kara 		 * updates after data IO completion.
458ccb26b5aSJan Kara 		 */
459ccb26b5aSJan Kara 		redirty_tail(inode, wb);
4600ae45f63STheodore Ts'o 	} else if (inode->i_state & I_DIRTY_TIME) {
4610ae45f63STheodore Ts'o 		list_move(&inode->i_wb_list, &wb->b_dirty_time);
462ccb26b5aSJan Kara 	} else {
463ccb26b5aSJan Kara 		/* The inode is clean. Remove from writeback lists. */
464ccb26b5aSJan Kara 		list_del_init(&inode->i_wb_list);
465ccb26b5aSJan Kara 	}
466ccb26b5aSJan Kara }
467ccb26b5aSJan Kara 
468ccb26b5aSJan Kara /*
4694f8ad655SJan Kara  * Write out an inode and its dirty pages. Do not update the writeback list
4704f8ad655SJan Kara  * linkage. That is left to the caller. The caller is also responsible for
4714f8ad655SJan Kara  * setting I_SYNC flag and calling inode_sync_complete() to clear it.
4721da177e4SLinus Torvalds  */
4731da177e4SLinus Torvalds static int
474cd8ed2a4SYan Hong __writeback_single_inode(struct inode *inode, struct writeback_control *wbc)
4751da177e4SLinus Torvalds {
4761da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
477251d6a47SWu Fengguang 	long nr_to_write = wbc->nr_to_write;
47801c03194SChristoph Hellwig 	unsigned dirty;
4791da177e4SLinus Torvalds 	int ret;
4801da177e4SLinus Torvalds 
4814f8ad655SJan Kara 	WARN_ON(!(inode->i_state & I_SYNC));
4821da177e4SLinus Torvalds 
4839fb0a7daSTejun Heo 	trace_writeback_single_inode_start(inode, wbc, nr_to_write);
4849fb0a7daSTejun Heo 
4851da177e4SLinus Torvalds 	ret = do_writepages(mapping, wbc);
4861da177e4SLinus Torvalds 
48726821ed4SChristoph Hellwig 	/*
48826821ed4SChristoph Hellwig 	 * Make sure to wait on the data before writing out the metadata.
48926821ed4SChristoph Hellwig 	 * This is important for filesystems that modify metadata on data
4907747bd4bSDave Chinner 	 * I/O completion. We don't do it for sync(2) writeback because it has a
4917747bd4bSDave Chinner 	 * separate, external IO completion path and ->sync_fs for guaranteeing
4927747bd4bSDave Chinner 	 * inode metadata is written back correctly.
49326821ed4SChristoph Hellwig 	 */
4947747bd4bSDave Chinner 	if (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync) {
49526821ed4SChristoph Hellwig 		int err = filemap_fdatawait(mapping);
4961da177e4SLinus Torvalds 		if (ret == 0)
4971da177e4SLinus Torvalds 			ret = err;
4981da177e4SLinus Torvalds 	}
4991da177e4SLinus Torvalds 
5005547e8aaSDmitry Monakhov 	/*
5015547e8aaSDmitry Monakhov 	 * Some filesystems may redirty the inode during the writeback
5025547e8aaSDmitry Monakhov 	 * due to delalloc, clear dirty metadata flags right before
5035547e8aaSDmitry Monakhov 	 * write_inode()
5045547e8aaSDmitry Monakhov 	 */
505250df6edSDave Chinner 	spin_lock(&inode->i_lock);
5069c6ac78eSTejun Heo 
5075547e8aaSDmitry Monakhov 	dirty = inode->i_state & I_DIRTY;
5080ae45f63STheodore Ts'o 	if (((dirty & (I_DIRTY_SYNC | I_DIRTY_DATASYNC)) &&
5090ae45f63STheodore Ts'o 	     (inode->i_state & I_DIRTY_TIME)) ||
5100ae45f63STheodore Ts'o 	    (inode->i_state & I_DIRTY_TIME_EXPIRED)) {
5110ae45f63STheodore Ts'o 		dirty |= I_DIRTY_TIME | I_DIRTY_TIME_EXPIRED;
5120ae45f63STheodore Ts'o 		trace_writeback_lazytime(inode);
5130ae45f63STheodore Ts'o 	}
5140ae45f63STheodore Ts'o 	inode->i_state &= ~dirty;
5159c6ac78eSTejun Heo 
5169c6ac78eSTejun Heo 	/*
5179c6ac78eSTejun Heo 	 * Paired with smp_mb() in __mark_inode_dirty().  This allows
5189c6ac78eSTejun Heo 	 * __mark_inode_dirty() to test i_state without grabbing i_lock -
5199c6ac78eSTejun Heo 	 * either they see the I_DIRTY bits cleared or we see the dirtied
5209c6ac78eSTejun Heo 	 * inode.
5219c6ac78eSTejun Heo 	 *
5229c6ac78eSTejun Heo 	 * I_DIRTY_PAGES is always cleared together above even if @mapping
5239c6ac78eSTejun Heo 	 * still has dirty pages.  The flag is reinstated after smp_mb() if
5249c6ac78eSTejun Heo 	 * necessary.  This guarantees that either __mark_inode_dirty()
5259c6ac78eSTejun Heo 	 * sees clear I_DIRTY_PAGES or we see PAGECACHE_TAG_DIRTY.
5269c6ac78eSTejun Heo 	 */
5279c6ac78eSTejun Heo 	smp_mb();
5289c6ac78eSTejun Heo 
5299c6ac78eSTejun Heo 	if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
5309c6ac78eSTejun Heo 		inode->i_state |= I_DIRTY_PAGES;
5319c6ac78eSTejun Heo 
532250df6edSDave Chinner 	spin_unlock(&inode->i_lock);
5339c6ac78eSTejun Heo 
5340ae45f63STheodore Ts'o 	if (dirty & I_DIRTY_TIME)
5350ae45f63STheodore Ts'o 		mark_inode_dirty_sync(inode);
53626821ed4SChristoph Hellwig 	/* Don't write the inode if only I_DIRTY_PAGES was set */
5370ae45f63STheodore Ts'o 	if (dirty & ~I_DIRTY_PAGES) {
538a9185b41SChristoph Hellwig 		int err = write_inode(inode, wbc);
5391da177e4SLinus Torvalds 		if (ret == 0)
5401da177e4SLinus Torvalds 			ret = err;
5411da177e4SLinus Torvalds 	}
5424f8ad655SJan Kara 	trace_writeback_single_inode(inode, wbc, nr_to_write);
5434f8ad655SJan Kara 	return ret;
5444f8ad655SJan Kara }
5454f8ad655SJan Kara 
5464f8ad655SJan Kara /*
5474f8ad655SJan Kara  * Write out an inode's dirty pages. Either the caller has an active reference
5484f8ad655SJan Kara  * on the inode or the inode has I_WILL_FREE set.
5494f8ad655SJan Kara  *
5504f8ad655SJan Kara  * This function is designed to be called for writing back one inode which
5514f8ad655SJan Kara  * we go e.g. from filesystem. Flusher thread uses __writeback_single_inode()
5524f8ad655SJan Kara  * and does more profound writeback list handling in writeback_sb_inodes().
5534f8ad655SJan Kara  */
5544f8ad655SJan Kara static int
5554f8ad655SJan Kara writeback_single_inode(struct inode *inode, struct bdi_writeback *wb,
5564f8ad655SJan Kara 		       struct writeback_control *wbc)
5574f8ad655SJan Kara {
5584f8ad655SJan Kara 	int ret = 0;
5594f8ad655SJan Kara 
5604f8ad655SJan Kara 	spin_lock(&inode->i_lock);
5614f8ad655SJan Kara 	if (!atomic_read(&inode->i_count))
5624f8ad655SJan Kara 		WARN_ON(!(inode->i_state & (I_WILL_FREE|I_FREEING)));
5634f8ad655SJan Kara 	else
5644f8ad655SJan Kara 		WARN_ON(inode->i_state & I_WILL_FREE);
5654f8ad655SJan Kara 
5664f8ad655SJan Kara 	if (inode->i_state & I_SYNC) {
5674f8ad655SJan Kara 		if (wbc->sync_mode != WB_SYNC_ALL)
5684f8ad655SJan Kara 			goto out;
5694f8ad655SJan Kara 		/*
570169ebd90SJan Kara 		 * It's a data-integrity sync. We must wait. Since callers hold
571169ebd90SJan Kara 		 * inode reference or inode has I_WILL_FREE set, it cannot go
572169ebd90SJan Kara 		 * away under us.
5734f8ad655SJan Kara 		 */
574169ebd90SJan Kara 		__inode_wait_for_writeback(inode);
5754f8ad655SJan Kara 	}
5764f8ad655SJan Kara 	WARN_ON(inode->i_state & I_SYNC);
5774f8ad655SJan Kara 	/*
578f9b0e058SJan Kara 	 * Skip inode if it is clean and we have no outstanding writeback in
579f9b0e058SJan Kara 	 * WB_SYNC_ALL mode. We don't want to mess with writeback lists in this
580f9b0e058SJan Kara 	 * function since flusher thread may be doing for example sync in
581f9b0e058SJan Kara 	 * parallel and if we move the inode, it could get skipped. So here we
582f9b0e058SJan Kara 	 * make sure inode is on some writeback list and leave it there unless
583f9b0e058SJan Kara 	 * we have completely cleaned the inode.
5844f8ad655SJan Kara 	 */
5850ae45f63STheodore Ts'o 	if (!(inode->i_state & I_DIRTY_ALL) &&
586f9b0e058SJan Kara 	    (wbc->sync_mode != WB_SYNC_ALL ||
587f9b0e058SJan Kara 	     !mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK)))
5884f8ad655SJan Kara 		goto out;
5894f8ad655SJan Kara 	inode->i_state |= I_SYNC;
5904f8ad655SJan Kara 	spin_unlock(&inode->i_lock);
5914f8ad655SJan Kara 
592cd8ed2a4SYan Hong 	ret = __writeback_single_inode(inode, wbc);
5931da177e4SLinus Torvalds 
594f758eeabSChristoph Hellwig 	spin_lock(&wb->list_lock);
595250df6edSDave Chinner 	spin_lock(&inode->i_lock);
5964f8ad655SJan Kara 	/*
5974f8ad655SJan Kara 	 * If inode is clean, remove it from writeback lists. Otherwise don't
5984f8ad655SJan Kara 	 * touch it. See comment above for explanation.
5994f8ad655SJan Kara 	 */
6000ae45f63STheodore Ts'o 	if (!(inode->i_state & I_DIRTY_ALL))
6014f8ad655SJan Kara 		list_del_init(&inode->i_wb_list);
6024f8ad655SJan Kara 	spin_unlock(&wb->list_lock);
6031c0eeaf5SJoern Engel 	inode_sync_complete(inode);
6044f8ad655SJan Kara out:
6054f8ad655SJan Kara 	spin_unlock(&inode->i_lock);
6061da177e4SLinus Torvalds 	return ret;
6071da177e4SLinus Torvalds }
6081da177e4SLinus Torvalds 
6091a12d8bdSWu Fengguang static long writeback_chunk_size(struct backing_dev_info *bdi,
6101a12d8bdSWu Fengguang 				 struct wb_writeback_work *work)
611d46db3d5SWu Fengguang {
612d46db3d5SWu Fengguang 	long pages;
613d46db3d5SWu Fengguang 
614d46db3d5SWu Fengguang 	/*
615d46db3d5SWu Fengguang 	 * WB_SYNC_ALL mode does livelock avoidance by syncing dirty
616d46db3d5SWu Fengguang 	 * inodes/pages in one big loop. Setting wbc.nr_to_write=LONG_MAX
617d46db3d5SWu Fengguang 	 * here avoids calling into writeback_inodes_wb() more than once.
618d46db3d5SWu Fengguang 	 *
619d46db3d5SWu Fengguang 	 * The intended call sequence for WB_SYNC_ALL writeback is:
620d46db3d5SWu Fengguang 	 *
621d46db3d5SWu Fengguang 	 *      wb_writeback()
622d46db3d5SWu Fengguang 	 *          writeback_sb_inodes()       <== called only once
623d46db3d5SWu Fengguang 	 *              write_cache_pages()     <== called once for each inode
624d46db3d5SWu Fengguang 	 *                   (quickly) tag currently dirty pages
625d46db3d5SWu Fengguang 	 *                   (maybe slowly) sync all tagged pages
626d46db3d5SWu Fengguang 	 */
627d46db3d5SWu Fengguang 	if (work->sync_mode == WB_SYNC_ALL || work->tagged_writepages)
628d46db3d5SWu Fengguang 		pages = LONG_MAX;
6291a12d8bdSWu Fengguang 	else {
6301a12d8bdSWu Fengguang 		pages = min(bdi->avg_write_bandwidth / 2,
6311a12d8bdSWu Fengguang 			    global_dirty_limit / DIRTY_SCOPE);
6321a12d8bdSWu Fengguang 		pages = min(pages, work->nr_pages);
6331a12d8bdSWu Fengguang 		pages = round_down(pages + MIN_WRITEBACK_PAGES,
6341a12d8bdSWu Fengguang 				   MIN_WRITEBACK_PAGES);
6351a12d8bdSWu Fengguang 	}
636d46db3d5SWu Fengguang 
637d46db3d5SWu Fengguang 	return pages;
638d46db3d5SWu Fengguang }
639d46db3d5SWu Fengguang 
64003ba3782SJens Axboe /*
641f11c9c5cSEdward Shishkin  * Write a portion of b_io inodes which belong to @sb.
642edadfb10SChristoph Hellwig  *
643d46db3d5SWu Fengguang  * Return the number of pages and/or inodes written.
644f11c9c5cSEdward Shishkin  */
645d46db3d5SWu Fengguang static long writeback_sb_inodes(struct super_block *sb,
646d46db3d5SWu Fengguang 				struct bdi_writeback *wb,
647d46db3d5SWu Fengguang 				struct wb_writeback_work *work)
64803ba3782SJens Axboe {
649d46db3d5SWu Fengguang 	struct writeback_control wbc = {
650d46db3d5SWu Fengguang 		.sync_mode		= work->sync_mode,
651d46db3d5SWu Fengguang 		.tagged_writepages	= work->tagged_writepages,
652d46db3d5SWu Fengguang 		.for_kupdate		= work->for_kupdate,
653d46db3d5SWu Fengguang 		.for_background		= work->for_background,
6547747bd4bSDave Chinner 		.for_sync		= work->for_sync,
655d46db3d5SWu Fengguang 		.range_cyclic		= work->range_cyclic,
656d46db3d5SWu Fengguang 		.range_start		= 0,
657d46db3d5SWu Fengguang 		.range_end		= LLONG_MAX,
658d46db3d5SWu Fengguang 	};
659d46db3d5SWu Fengguang 	unsigned long start_time = jiffies;
660d46db3d5SWu Fengguang 	long write_chunk;
661d46db3d5SWu Fengguang 	long wrote = 0;  /* count both pages and inodes */
662d46db3d5SWu Fengguang 
66303ba3782SJens Axboe 	while (!list_empty(&wb->b_io)) {
6647ccf19a8SNick Piggin 		struct inode *inode = wb_inode(wb->b_io.prev);
665edadfb10SChristoph Hellwig 
666edadfb10SChristoph Hellwig 		if (inode->i_sb != sb) {
667d46db3d5SWu Fengguang 			if (work->sb) {
668edadfb10SChristoph Hellwig 				/*
669edadfb10SChristoph Hellwig 				 * We only want to write back data for this
670edadfb10SChristoph Hellwig 				 * superblock, move all inodes not belonging
671edadfb10SChristoph Hellwig 				 * to it back onto the dirty list.
672edadfb10SChristoph Hellwig 				 */
673f758eeabSChristoph Hellwig 				redirty_tail(inode, wb);
67466f3b8e2SJens Axboe 				continue;
67566f3b8e2SJens Axboe 			}
676edadfb10SChristoph Hellwig 
677edadfb10SChristoph Hellwig 			/*
678edadfb10SChristoph Hellwig 			 * The inode belongs to a different superblock.
679edadfb10SChristoph Hellwig 			 * Bounce back to the caller to unpin this and
680edadfb10SChristoph Hellwig 			 * pin the next superblock.
681edadfb10SChristoph Hellwig 			 */
682d46db3d5SWu Fengguang 			break;
683edadfb10SChristoph Hellwig 		}
684edadfb10SChristoph Hellwig 
6859843b76aSChristoph Hellwig 		/*
686331cbdeeSWanpeng Li 		 * Don't bother with new inodes or inodes being freed, first
687331cbdeeSWanpeng Li 		 * kind does not need periodic writeout yet, and for the latter
6889843b76aSChristoph Hellwig 		 * kind writeout is handled by the freer.
6899843b76aSChristoph Hellwig 		 */
690250df6edSDave Chinner 		spin_lock(&inode->i_lock);
6919843b76aSChristoph Hellwig 		if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) {
692250df6edSDave Chinner 			spin_unlock(&inode->i_lock);
693fcc5c222SWu Fengguang 			redirty_tail(inode, wb);
6947ef0d737SNick Piggin 			continue;
6957ef0d737SNick Piggin 		}
696cc1676d9SJan Kara 		if ((inode->i_state & I_SYNC) && wbc.sync_mode != WB_SYNC_ALL) {
697cc1676d9SJan Kara 			/*
698cc1676d9SJan Kara 			 * If this inode is locked for writeback and we are not
699cc1676d9SJan Kara 			 * doing writeback-for-data-integrity, move it to
700cc1676d9SJan Kara 			 * b_more_io so that writeback can proceed with the
701cc1676d9SJan Kara 			 * other inodes on s_io.
702cc1676d9SJan Kara 			 *
703cc1676d9SJan Kara 			 * We'll have another go at writing back this inode
704cc1676d9SJan Kara 			 * when we completed a full scan of b_io.
705cc1676d9SJan Kara 			 */
706cc1676d9SJan Kara 			spin_unlock(&inode->i_lock);
707cc1676d9SJan Kara 			requeue_io(inode, wb);
708cc1676d9SJan Kara 			trace_writeback_sb_inodes_requeue(inode);
709cc1676d9SJan Kara 			continue;
710cc1676d9SJan Kara 		}
711f0d07b7fSJan Kara 		spin_unlock(&wb->list_lock);
712f0d07b7fSJan Kara 
7134f8ad655SJan Kara 		/*
7144f8ad655SJan Kara 		 * We already requeued the inode if it had I_SYNC set and we
7154f8ad655SJan Kara 		 * are doing WB_SYNC_NONE writeback. So this catches only the
7164f8ad655SJan Kara 		 * WB_SYNC_ALL case.
7174f8ad655SJan Kara 		 */
718169ebd90SJan Kara 		if (inode->i_state & I_SYNC) {
719169ebd90SJan Kara 			/* Wait for I_SYNC. This function drops i_lock... */
720169ebd90SJan Kara 			inode_sleep_on_writeback(inode);
721169ebd90SJan Kara 			/* Inode may be gone, start again */
722ead188f9SJan Kara 			spin_lock(&wb->list_lock);
723169ebd90SJan Kara 			continue;
724169ebd90SJan Kara 		}
7254f8ad655SJan Kara 		inode->i_state |= I_SYNC;
7264f8ad655SJan Kara 		spin_unlock(&inode->i_lock);
727169ebd90SJan Kara 
7281a12d8bdSWu Fengguang 		write_chunk = writeback_chunk_size(wb->bdi, work);
729d46db3d5SWu Fengguang 		wbc.nr_to_write = write_chunk;
730d46db3d5SWu Fengguang 		wbc.pages_skipped = 0;
731250df6edSDave Chinner 
732169ebd90SJan Kara 		/*
733169ebd90SJan Kara 		 * We use I_SYNC to pin the inode in memory. While it is set
734169ebd90SJan Kara 		 * evict_inode() will wait so the inode cannot be freed.
735169ebd90SJan Kara 		 */
736cd8ed2a4SYan Hong 		__writeback_single_inode(inode, &wbc);
737d46db3d5SWu Fengguang 
738d46db3d5SWu Fengguang 		work->nr_pages -= write_chunk - wbc.nr_to_write;
739d46db3d5SWu Fengguang 		wrote += write_chunk - wbc.nr_to_write;
7404f8ad655SJan Kara 		spin_lock(&wb->list_lock);
7414f8ad655SJan Kara 		spin_lock(&inode->i_lock);
7420ae45f63STheodore Ts'o 		if (!(inode->i_state & I_DIRTY_ALL))
743d46db3d5SWu Fengguang 			wrote++;
7444f8ad655SJan Kara 		requeue_inode(inode, wb, &wbc);
7454f8ad655SJan Kara 		inode_sync_complete(inode);
7460f1b1fd8SDave Chinner 		spin_unlock(&inode->i_lock);
747169ebd90SJan Kara 		cond_resched_lock(&wb->list_lock);
748d46db3d5SWu Fengguang 		/*
749d46db3d5SWu Fengguang 		 * bail out to wb_writeback() often enough to check
750d46db3d5SWu Fengguang 		 * background threshold and other termination conditions.
751d46db3d5SWu Fengguang 		 */
752d46db3d5SWu Fengguang 		if (wrote) {
753d46db3d5SWu Fengguang 			if (time_is_before_jiffies(start_time + HZ / 10UL))
754d46db3d5SWu Fengguang 				break;
755d46db3d5SWu Fengguang 			if (work->nr_pages <= 0)
756d46db3d5SWu Fengguang 				break;
7571da177e4SLinus Torvalds 		}
7588bc3be27SFengguang Wu 	}
759d46db3d5SWu Fengguang 	return wrote;
760f11c9c5cSEdward Shishkin }
76138f21977SNick Piggin 
762d46db3d5SWu Fengguang static long __writeback_inodes_wb(struct bdi_writeback *wb,
763d46db3d5SWu Fengguang 				  struct wb_writeback_work *work)
764f11c9c5cSEdward Shishkin {
765d46db3d5SWu Fengguang 	unsigned long start_time = jiffies;
766d46db3d5SWu Fengguang 	long wrote = 0;
767f11c9c5cSEdward Shishkin 
768f11c9c5cSEdward Shishkin 	while (!list_empty(&wb->b_io)) {
7697ccf19a8SNick Piggin 		struct inode *inode = wb_inode(wb->b_io.prev);
770f11c9c5cSEdward Shishkin 		struct super_block *sb = inode->i_sb;
771f11c9c5cSEdward Shishkin 
772eb6ef3dfSKonstantin Khlebnikov 		if (!trylock_super(sb)) {
7730e995816SWu Fengguang 			/*
774eb6ef3dfSKonstantin Khlebnikov 			 * trylock_super() may fail consistently due to
7750e995816SWu Fengguang 			 * s_umount being grabbed by someone else. Don't use
7760e995816SWu Fengguang 			 * requeue_io() to avoid busy retrying the inode/sb.
7770e995816SWu Fengguang 			 */
7780e995816SWu Fengguang 			redirty_tail(inode, wb);
779d19de7edSChristoph Hellwig 			continue;
780334132aeSChristoph Hellwig 		}
781d46db3d5SWu Fengguang 		wrote += writeback_sb_inodes(sb, wb, work);
782eb6ef3dfSKonstantin Khlebnikov 		up_read(&sb->s_umount);
783f11c9c5cSEdward Shishkin 
784d46db3d5SWu Fengguang 		/* refer to the same tests at the end of writeback_sb_inodes */
785d46db3d5SWu Fengguang 		if (wrote) {
786d46db3d5SWu Fengguang 			if (time_is_before_jiffies(start_time + HZ / 10UL))
787d46db3d5SWu Fengguang 				break;
788d46db3d5SWu Fengguang 			if (work->nr_pages <= 0)
789f11c9c5cSEdward Shishkin 				break;
790f11c9c5cSEdward Shishkin 		}
791d46db3d5SWu Fengguang 	}
79266f3b8e2SJens Axboe 	/* Leave any unwritten inodes on b_io */
793d46db3d5SWu Fengguang 	return wrote;
79466f3b8e2SJens Axboe }
79566f3b8e2SJens Axboe 
7967d9f073bSWanpeng Li static long writeback_inodes_wb(struct bdi_writeback *wb, long nr_pages,
7970e175a18SCurt Wohlgemuth 				enum wb_reason reason)
798edadfb10SChristoph Hellwig {
799d46db3d5SWu Fengguang 	struct wb_writeback_work work = {
800d46db3d5SWu Fengguang 		.nr_pages	= nr_pages,
801d46db3d5SWu Fengguang 		.sync_mode	= WB_SYNC_NONE,
802d46db3d5SWu Fengguang 		.range_cyclic	= 1,
8030e175a18SCurt Wohlgemuth 		.reason		= reason,
804d46db3d5SWu Fengguang 	};
805edadfb10SChristoph Hellwig 
806f758eeabSChristoph Hellwig 	spin_lock(&wb->list_lock);
807424b351fSWu Fengguang 	if (list_empty(&wb->b_io))
808ad4e38ddSCurt Wohlgemuth 		queue_io(wb, &work);
809d46db3d5SWu Fengguang 	__writeback_inodes_wb(wb, &work);
810f758eeabSChristoph Hellwig 	spin_unlock(&wb->list_lock);
811edadfb10SChristoph Hellwig 
812d46db3d5SWu Fengguang 	return nr_pages - work.nr_pages;
81366f3b8e2SJens Axboe }
81466f3b8e2SJens Axboe 
815b00949aaSWu Fengguang static bool over_bground_thresh(struct backing_dev_info *bdi)
81603ba3782SJens Axboe {
81703ba3782SJens Axboe 	unsigned long background_thresh, dirty_thresh;
81803ba3782SJens Axboe 
81916c4042fSWu Fengguang 	global_dirty_limits(&background_thresh, &dirty_thresh);
82003ba3782SJens Axboe 
821b00949aaSWu Fengguang 	if (global_page_state(NR_FILE_DIRTY) +
822b00949aaSWu Fengguang 	    global_page_state(NR_UNSTABLE_NFS) > background_thresh)
823b00949aaSWu Fengguang 		return true;
824b00949aaSWu Fengguang 
825b00949aaSWu Fengguang 	if (bdi_stat(bdi, BDI_RECLAIMABLE) >
826b00949aaSWu Fengguang 				bdi_dirty_limit(bdi, background_thresh))
827b00949aaSWu Fengguang 		return true;
828b00949aaSWu Fengguang 
829b00949aaSWu Fengguang 	return false;
83003ba3782SJens Axboe }
83103ba3782SJens Axboe 
83203ba3782SJens Axboe /*
833e98be2d5SWu Fengguang  * Called under wb->list_lock. If there are multiple wb per bdi,
834e98be2d5SWu Fengguang  * only the flusher working on the first wb should do it.
835e98be2d5SWu Fengguang  */
836e98be2d5SWu Fengguang static void wb_update_bandwidth(struct bdi_writeback *wb,
837e98be2d5SWu Fengguang 				unsigned long start_time)
838e98be2d5SWu Fengguang {
839af6a3113SWu Fengguang 	__bdi_update_bandwidth(wb->bdi, 0, 0, 0, 0, 0, start_time);
840e98be2d5SWu Fengguang }
841e98be2d5SWu Fengguang 
842e98be2d5SWu Fengguang /*
84303ba3782SJens Axboe  * Explicit flushing or periodic writeback of "old" data.
84403ba3782SJens Axboe  *
84503ba3782SJens Axboe  * Define "old": the first time one of an inode's pages is dirtied, we mark the
84603ba3782SJens Axboe  * dirtying-time in the inode's address_space.  So this periodic writeback code
84703ba3782SJens Axboe  * just walks the superblock inode list, writing back any inodes which are
84803ba3782SJens Axboe  * older than a specific point in time.
84903ba3782SJens Axboe  *
85003ba3782SJens Axboe  * Try to run once per dirty_writeback_interval.  But if a writeback event
85103ba3782SJens Axboe  * takes longer than a dirty_writeback_interval interval, then leave a
85203ba3782SJens Axboe  * one-second gap.
85303ba3782SJens Axboe  *
85403ba3782SJens Axboe  * older_than_this takes precedence over nr_to_write.  So we'll only write back
85503ba3782SJens Axboe  * all dirty pages if they are all attached to "old" mappings.
85603ba3782SJens Axboe  */
857c4a77a6cSJens Axboe static long wb_writeback(struct bdi_writeback *wb,
85883ba7b07SChristoph Hellwig 			 struct wb_writeback_work *work)
85903ba3782SJens Axboe {
860e98be2d5SWu Fengguang 	unsigned long wb_start = jiffies;
861d46db3d5SWu Fengguang 	long nr_pages = work->nr_pages;
8620dc83bd3SJan Kara 	unsigned long oldest_jif;
863a5989bdcSJan Kara 	struct inode *inode;
864d46db3d5SWu Fengguang 	long progress;
86503ba3782SJens Axboe 
8660dc83bd3SJan Kara 	oldest_jif = jiffies;
8670dc83bd3SJan Kara 	work->older_than_this = &oldest_jif;
86803ba3782SJens Axboe 
869e8dfc305SWu Fengguang 	spin_lock(&wb->list_lock);
87003ba3782SJens Axboe 	for (;;) {
87103ba3782SJens Axboe 		/*
872d3ddec76SWu Fengguang 		 * Stop writeback when nr_pages has been consumed
87303ba3782SJens Axboe 		 */
87483ba7b07SChristoph Hellwig 		if (work->nr_pages <= 0)
87503ba3782SJens Axboe 			break;
87603ba3782SJens Axboe 
87703ba3782SJens Axboe 		/*
878aa373cf5SJan Kara 		 * Background writeout and kupdate-style writeback may
879aa373cf5SJan Kara 		 * run forever. Stop them if there is other work to do
880aa373cf5SJan Kara 		 * so that e.g. sync can proceed. They'll be restarted
881aa373cf5SJan Kara 		 * after the other works are all done.
882aa373cf5SJan Kara 		 */
883aa373cf5SJan Kara 		if ((work->for_background || work->for_kupdate) &&
884aa373cf5SJan Kara 		    !list_empty(&wb->bdi->work_list))
885aa373cf5SJan Kara 			break;
886aa373cf5SJan Kara 
887aa373cf5SJan Kara 		/*
888d3ddec76SWu Fengguang 		 * For background writeout, stop when we are below the
889d3ddec76SWu Fengguang 		 * background dirty threshold
89003ba3782SJens Axboe 		 */
891b00949aaSWu Fengguang 		if (work->for_background && !over_bground_thresh(wb->bdi))
89203ba3782SJens Axboe 			break;
89303ba3782SJens Axboe 
8941bc36b64SJan Kara 		/*
8951bc36b64SJan Kara 		 * Kupdate and background works are special and we want to
8961bc36b64SJan Kara 		 * include all inodes that need writing. Livelock avoidance is
8971bc36b64SJan Kara 		 * handled by these works yielding to any other work so we are
8981bc36b64SJan Kara 		 * safe.
8991bc36b64SJan Kara 		 */
900ba9aa839SWu Fengguang 		if (work->for_kupdate) {
9010dc83bd3SJan Kara 			oldest_jif = jiffies -
902ba9aa839SWu Fengguang 				msecs_to_jiffies(dirty_expire_interval * 10);
9031bc36b64SJan Kara 		} else if (work->for_background)
9040dc83bd3SJan Kara 			oldest_jif = jiffies;
905028c2dd1SDave Chinner 
906d46db3d5SWu Fengguang 		trace_writeback_start(wb->bdi, work);
907e8dfc305SWu Fengguang 		if (list_empty(&wb->b_io))
908ad4e38ddSCurt Wohlgemuth 			queue_io(wb, work);
90983ba7b07SChristoph Hellwig 		if (work->sb)
910d46db3d5SWu Fengguang 			progress = writeback_sb_inodes(work->sb, wb, work);
911edadfb10SChristoph Hellwig 		else
912d46db3d5SWu Fengguang 			progress = __writeback_inodes_wb(wb, work);
913d46db3d5SWu Fengguang 		trace_writeback_written(wb->bdi, work);
914028c2dd1SDave Chinner 
915e98be2d5SWu Fengguang 		wb_update_bandwidth(wb, wb_start);
91603ba3782SJens Axboe 
91703ba3782SJens Axboe 		/*
91871fd05a8SJens Axboe 		 * Did we write something? Try for more
919e6fb6da2SWu Fengguang 		 *
920e6fb6da2SWu Fengguang 		 * Dirty inodes are moved to b_io for writeback in batches.
921e6fb6da2SWu Fengguang 		 * The completion of the current batch does not necessarily
922e6fb6da2SWu Fengguang 		 * mean the overall work is done. So we keep looping as long
923e6fb6da2SWu Fengguang 		 * as made some progress on cleaning pages or inodes.
92471fd05a8SJens Axboe 		 */
925d46db3d5SWu Fengguang 		if (progress)
92603ba3782SJens Axboe 			continue;
927a5989bdcSJan Kara 		/*
928e6fb6da2SWu Fengguang 		 * No more inodes for IO, bail
929a5989bdcSJan Kara 		 */
930b7a2441fSWu Fengguang 		if (list_empty(&wb->b_more_io))
93103ba3782SJens Axboe 			break;
93203ba3782SJens Axboe 		/*
9338010c3b6SJens Axboe 		 * Nothing written. Wait for some inode to
9348010c3b6SJens Axboe 		 * become available for writeback. Otherwise
9358010c3b6SJens Axboe 		 * we'll just busyloop.
93603ba3782SJens Axboe 		 */
93703ba3782SJens Axboe 		if (!list_empty(&wb->b_more_io))  {
938d46db3d5SWu Fengguang 			trace_writeback_wait(wb->bdi, work);
93903ba3782SJens Axboe 			inode = wb_inode(wb->b_more_io.prev);
940250df6edSDave Chinner 			spin_lock(&inode->i_lock);
941f0d07b7fSJan Kara 			spin_unlock(&wb->list_lock);
942169ebd90SJan Kara 			/* This function drops i_lock... */
943169ebd90SJan Kara 			inode_sleep_on_writeback(inode);
944f0d07b7fSJan Kara 			spin_lock(&wb->list_lock);
94503ba3782SJens Axboe 		}
94603ba3782SJens Axboe 	}
947e8dfc305SWu Fengguang 	spin_unlock(&wb->list_lock);
94803ba3782SJens Axboe 
949d46db3d5SWu Fengguang 	return nr_pages - work->nr_pages;
95003ba3782SJens Axboe }
95103ba3782SJens Axboe 
95203ba3782SJens Axboe /*
95383ba7b07SChristoph Hellwig  * Return the next wb_writeback_work struct that hasn't been processed yet.
95403ba3782SJens Axboe  */
95583ba7b07SChristoph Hellwig static struct wb_writeback_work *
95608852b6dSMinchan Kim get_next_work_item(struct backing_dev_info *bdi)
95703ba3782SJens Axboe {
95883ba7b07SChristoph Hellwig 	struct wb_writeback_work *work = NULL;
95903ba3782SJens Axboe 
9606467716aSArtem Bityutskiy 	spin_lock_bh(&bdi->wb_lock);
96183ba7b07SChristoph Hellwig 	if (!list_empty(&bdi->work_list)) {
96283ba7b07SChristoph Hellwig 		work = list_entry(bdi->work_list.next,
96383ba7b07SChristoph Hellwig 				  struct wb_writeback_work, list);
96483ba7b07SChristoph Hellwig 		list_del_init(&work->list);
96503ba3782SJens Axboe 	}
9666467716aSArtem Bityutskiy 	spin_unlock_bh(&bdi->wb_lock);
96783ba7b07SChristoph Hellwig 	return work;
96803ba3782SJens Axboe }
96903ba3782SJens Axboe 
970cdf01dd5SLinus Torvalds /*
971cdf01dd5SLinus Torvalds  * Add in the number of potentially dirty inodes, because each inode
972cdf01dd5SLinus Torvalds  * write can dirty pagecache in the underlying blockdev.
973cdf01dd5SLinus Torvalds  */
974cdf01dd5SLinus Torvalds static unsigned long get_nr_dirty_pages(void)
975cdf01dd5SLinus Torvalds {
976cdf01dd5SLinus Torvalds 	return global_page_state(NR_FILE_DIRTY) +
977cdf01dd5SLinus Torvalds 		global_page_state(NR_UNSTABLE_NFS) +
978cdf01dd5SLinus Torvalds 		get_nr_dirty_inodes();
979cdf01dd5SLinus Torvalds }
980cdf01dd5SLinus Torvalds 
9816585027aSJan Kara static long wb_check_background_flush(struct bdi_writeback *wb)
9826585027aSJan Kara {
983b00949aaSWu Fengguang 	if (over_bground_thresh(wb->bdi)) {
9846585027aSJan Kara 
9856585027aSJan Kara 		struct wb_writeback_work work = {
9866585027aSJan Kara 			.nr_pages	= LONG_MAX,
9876585027aSJan Kara 			.sync_mode	= WB_SYNC_NONE,
9886585027aSJan Kara 			.for_background	= 1,
9896585027aSJan Kara 			.range_cyclic	= 1,
9900e175a18SCurt Wohlgemuth 			.reason		= WB_REASON_BACKGROUND,
9916585027aSJan Kara 		};
9926585027aSJan Kara 
9936585027aSJan Kara 		return wb_writeback(wb, &work);
9946585027aSJan Kara 	}
9956585027aSJan Kara 
9966585027aSJan Kara 	return 0;
9976585027aSJan Kara }
9986585027aSJan Kara 
99903ba3782SJens Axboe static long wb_check_old_data_flush(struct bdi_writeback *wb)
100003ba3782SJens Axboe {
100103ba3782SJens Axboe 	unsigned long expired;
100203ba3782SJens Axboe 	long nr_pages;
100303ba3782SJens Axboe 
100469b62d01SJens Axboe 	/*
100569b62d01SJens Axboe 	 * When set to zero, disable periodic writeback
100669b62d01SJens Axboe 	 */
100769b62d01SJens Axboe 	if (!dirty_writeback_interval)
100869b62d01SJens Axboe 		return 0;
100969b62d01SJens Axboe 
101003ba3782SJens Axboe 	expired = wb->last_old_flush +
101103ba3782SJens Axboe 			msecs_to_jiffies(dirty_writeback_interval * 10);
101203ba3782SJens Axboe 	if (time_before(jiffies, expired))
101303ba3782SJens Axboe 		return 0;
101403ba3782SJens Axboe 
101503ba3782SJens Axboe 	wb->last_old_flush = jiffies;
1016cdf01dd5SLinus Torvalds 	nr_pages = get_nr_dirty_pages();
101703ba3782SJens Axboe 
1018c4a77a6cSJens Axboe 	if (nr_pages) {
101983ba7b07SChristoph Hellwig 		struct wb_writeback_work work = {
1020c4a77a6cSJens Axboe 			.nr_pages	= nr_pages,
1021c4a77a6cSJens Axboe 			.sync_mode	= WB_SYNC_NONE,
1022c4a77a6cSJens Axboe 			.for_kupdate	= 1,
1023c4a77a6cSJens Axboe 			.range_cyclic	= 1,
10240e175a18SCurt Wohlgemuth 			.reason		= WB_REASON_PERIODIC,
1025c4a77a6cSJens Axboe 		};
1026c4a77a6cSJens Axboe 
102783ba7b07SChristoph Hellwig 		return wb_writeback(wb, &work);
1028c4a77a6cSJens Axboe 	}
102903ba3782SJens Axboe 
103003ba3782SJens Axboe 	return 0;
103103ba3782SJens Axboe }
103203ba3782SJens Axboe 
103303ba3782SJens Axboe /*
103403ba3782SJens Axboe  * Retrieve work items and do the writeback they describe
103503ba3782SJens Axboe  */
103625d130baSWanpeng Li static long wb_do_writeback(struct bdi_writeback *wb)
103703ba3782SJens Axboe {
103803ba3782SJens Axboe 	struct backing_dev_info *bdi = wb->bdi;
103983ba7b07SChristoph Hellwig 	struct wb_writeback_work *work;
1040c4a77a6cSJens Axboe 	long wrote = 0;
104103ba3782SJens Axboe 
104281d73a32SJan Kara 	set_bit(BDI_writeback_running, &wb->bdi->state);
104308852b6dSMinchan Kim 	while ((work = get_next_work_item(bdi)) != NULL) {
104483ba7b07SChristoph Hellwig 
1045455b2864SDave Chinner 		trace_writeback_exec(bdi, work);
1046455b2864SDave Chinner 
104783ba7b07SChristoph Hellwig 		wrote += wb_writeback(wb, work);
104803ba3782SJens Axboe 
104903ba3782SJens Axboe 		/*
105083ba7b07SChristoph Hellwig 		 * Notify the caller of completion if this is a synchronous
105183ba7b07SChristoph Hellwig 		 * work item, otherwise just free it.
105203ba3782SJens Axboe 		 */
105383ba7b07SChristoph Hellwig 		if (work->done)
105483ba7b07SChristoph Hellwig 			complete(work->done);
105583ba7b07SChristoph Hellwig 		else
105683ba7b07SChristoph Hellwig 			kfree(work);
105703ba3782SJens Axboe 	}
105803ba3782SJens Axboe 
105903ba3782SJens Axboe 	/*
106003ba3782SJens Axboe 	 * Check for periodic writeback, kupdated() style
106103ba3782SJens Axboe 	 */
106203ba3782SJens Axboe 	wrote += wb_check_old_data_flush(wb);
10636585027aSJan Kara 	wrote += wb_check_background_flush(wb);
106481d73a32SJan Kara 	clear_bit(BDI_writeback_running, &wb->bdi->state);
106503ba3782SJens Axboe 
106603ba3782SJens Axboe 	return wrote;
106703ba3782SJens Axboe }
106803ba3782SJens Axboe 
106903ba3782SJens Axboe /*
107003ba3782SJens Axboe  * Handle writeback of dirty data for the device backed by this bdi. Also
1071839a8e86STejun Heo  * reschedules periodically and does kupdated style flushing.
107203ba3782SJens Axboe  */
1073839a8e86STejun Heo void bdi_writeback_workfn(struct work_struct *work)
107403ba3782SJens Axboe {
1075839a8e86STejun Heo 	struct bdi_writeback *wb = container_of(to_delayed_work(work),
1076839a8e86STejun Heo 						struct bdi_writeback, dwork);
107708243900SChristoph Hellwig 	struct backing_dev_info *bdi = wb->bdi;
107803ba3782SJens Axboe 	long pages_written;
107903ba3782SJens Axboe 
1080ef3b1019STejun Heo 	set_worker_desc("flush-%s", dev_name(bdi->dev));
1081766f9164SPeter Zijlstra 	current->flags |= PF_SWAPWRITE;
108203ba3782SJens Axboe 
1083839a8e86STejun Heo 	if (likely(!current_is_workqueue_rescuer() ||
10845acda9d1SJan Kara 		   !test_bit(BDI_registered, &bdi->state))) {
108503ba3782SJens Axboe 		/*
1086839a8e86STejun Heo 		 * The normal path.  Keep writing back @bdi until its
1087839a8e86STejun Heo 		 * work_list is empty.  Note that this path is also taken
1088839a8e86STejun Heo 		 * if @bdi is shutting down even when we're running off the
1089839a8e86STejun Heo 		 * rescuer as work_list needs to be drained.
109003ba3782SJens Axboe 		 */
1091839a8e86STejun Heo 		do {
109225d130baSWanpeng Li 			pages_written = wb_do_writeback(wb);
1093455b2864SDave Chinner 			trace_writeback_pages_written(pages_written);
1094839a8e86STejun Heo 		} while (!list_empty(&bdi->work_list));
1095839a8e86STejun Heo 	} else {
1096253c34e9SArtem Bityutskiy 		/*
1097839a8e86STejun Heo 		 * bdi_wq can't get enough workers and we're running off
1098839a8e86STejun Heo 		 * the emergency worker.  Don't hog it.  Hopefully, 1024 is
1099839a8e86STejun Heo 		 * enough for efficient IO.
1100253c34e9SArtem Bityutskiy 		 */
1101839a8e86STejun Heo 		pages_written = writeback_inodes_wb(&bdi->wb, 1024,
1102839a8e86STejun Heo 						    WB_REASON_FORKER_THREAD);
1103839a8e86STejun Heo 		trace_writeback_pages_written(pages_written);
110403ba3782SJens Axboe 	}
110503ba3782SJens Axboe 
11066ca738d6SDerek Basehore 	if (!list_empty(&bdi->work_list))
11076ca738d6SDerek Basehore 		mod_delayed_work(bdi_wq, &wb->dwork, 0);
11086ca738d6SDerek Basehore 	else if (wb_has_dirty_io(wb) && dirty_writeback_interval)
11096ca738d6SDerek Basehore 		bdi_wakeup_thread_delayed(bdi);
1110455b2864SDave Chinner 
1111839a8e86STejun Heo 	current->flags &= ~PF_SWAPWRITE;
111203ba3782SJens Axboe }
111303ba3782SJens Axboe 
111403ba3782SJens Axboe /*
111503ba3782SJens Axboe  * Start writeback of `nr_pages' pages.  If `nr_pages' is zero, write back
111603ba3782SJens Axboe  * the whole world.
111703ba3782SJens Axboe  */
11180e175a18SCurt Wohlgemuth void wakeup_flusher_threads(long nr_pages, enum wb_reason reason)
111903ba3782SJens Axboe {
1120b8c2f347SChristoph Hellwig 	struct backing_dev_info *bdi;
1121b8c2f347SChristoph Hellwig 
112247df3ddeSJan Kara 	if (!nr_pages)
112347df3ddeSJan Kara 		nr_pages = get_nr_dirty_pages();
1124b8c2f347SChristoph Hellwig 
1125b8c2f347SChristoph Hellwig 	rcu_read_lock();
1126b8c2f347SChristoph Hellwig 	list_for_each_entry_rcu(bdi, &bdi_list, bdi_list) {
1127b8c2f347SChristoph Hellwig 		if (!bdi_has_dirty_io(bdi))
1128b8c2f347SChristoph Hellwig 			continue;
11290e175a18SCurt Wohlgemuth 		__bdi_start_writeback(bdi, nr_pages, false, reason);
1130b8c2f347SChristoph Hellwig 	}
1131b8c2f347SChristoph Hellwig 	rcu_read_unlock();
113203ba3782SJens Axboe }
113303ba3782SJens Axboe 
113403ba3782SJens Axboe static noinline void block_dump___mark_inode_dirty(struct inode *inode)
113503ba3782SJens Axboe {
113603ba3782SJens Axboe 	if (inode->i_ino || strcmp(inode->i_sb->s_id, "bdev")) {
113703ba3782SJens Axboe 		struct dentry *dentry;
113803ba3782SJens Axboe 		const char *name = "?";
113903ba3782SJens Axboe 
114003ba3782SJens Axboe 		dentry = d_find_alias(inode);
114103ba3782SJens Axboe 		if (dentry) {
114203ba3782SJens Axboe 			spin_lock(&dentry->d_lock);
114303ba3782SJens Axboe 			name = (const char *) dentry->d_name.name;
114403ba3782SJens Axboe 		}
114503ba3782SJens Axboe 		printk(KERN_DEBUG
114603ba3782SJens Axboe 		       "%s(%d): dirtied inode %lu (%s) on %s\n",
114703ba3782SJens Axboe 		       current->comm, task_pid_nr(current), inode->i_ino,
114803ba3782SJens Axboe 		       name, inode->i_sb->s_id);
114903ba3782SJens Axboe 		if (dentry) {
115003ba3782SJens Axboe 			spin_unlock(&dentry->d_lock);
115103ba3782SJens Axboe 			dput(dentry);
115203ba3782SJens Axboe 		}
115303ba3782SJens Axboe 	}
115403ba3782SJens Axboe }
115503ba3782SJens Axboe 
115603ba3782SJens Axboe /**
115703ba3782SJens Axboe  *	__mark_inode_dirty -	internal function
115803ba3782SJens Axboe  *	@inode: inode to mark
115903ba3782SJens Axboe  *	@flags: what kind of dirty (i.e. I_DIRTY_SYNC)
116003ba3782SJens Axboe  *	Mark an inode as dirty. Callers should use mark_inode_dirty or
116103ba3782SJens Axboe  *  	mark_inode_dirty_sync.
116203ba3782SJens Axboe  *
116303ba3782SJens Axboe  * Put the inode on the super block's dirty list.
116403ba3782SJens Axboe  *
116503ba3782SJens Axboe  * CAREFUL! We mark it dirty unconditionally, but move it onto the
116603ba3782SJens Axboe  * dirty list only if it is hashed or if it refers to a blockdev.
116703ba3782SJens Axboe  * If it was not hashed, it will never be added to the dirty list
116803ba3782SJens Axboe  * even if it is later hashed, as it will have been marked dirty already.
116903ba3782SJens Axboe  *
117003ba3782SJens Axboe  * In short, make sure you hash any inodes _before_ you start marking
117103ba3782SJens Axboe  * them dirty.
117203ba3782SJens Axboe  *
117303ba3782SJens Axboe  * Note that for blockdevs, inode->dirtied_when represents the dirtying time of
117403ba3782SJens Axboe  * the block-special inode (/dev/hda1) itself.  And the ->dirtied_when field of
117503ba3782SJens Axboe  * the kernel-internal blockdev inode represents the dirtying time of the
117603ba3782SJens Axboe  * blockdev's pages.  This is why for I_DIRTY_PAGES we always use
117703ba3782SJens Axboe  * page->mapping->host, so the page-dirtying time is recorded in the internal
117803ba3782SJens Axboe  * blockdev inode.
117903ba3782SJens Axboe  */
11800ae45f63STheodore Ts'o #define I_DIRTY_INODE (I_DIRTY_SYNC | I_DIRTY_DATASYNC)
118103ba3782SJens Axboe void __mark_inode_dirty(struct inode *inode, int flags)
118203ba3782SJens Axboe {
118303ba3782SJens Axboe 	struct super_block *sb = inode->i_sb;
1184253c34e9SArtem Bityutskiy 	struct backing_dev_info *bdi = NULL;
11850ae45f63STheodore Ts'o 	int dirtytime;
11860ae45f63STheodore Ts'o 
11870ae45f63STheodore Ts'o 	trace_writeback_mark_inode_dirty(inode, flags);
118803ba3782SJens Axboe 
118903ba3782SJens Axboe 	/*
119003ba3782SJens Axboe 	 * Don't do this for I_DIRTY_PAGES - that doesn't actually
119103ba3782SJens Axboe 	 * dirty the inode itself
119203ba3782SJens Axboe 	 */
11930ae45f63STheodore Ts'o 	if (flags & (I_DIRTY_SYNC | I_DIRTY_DATASYNC | I_DIRTY_TIME)) {
11949fb0a7daSTejun Heo 		trace_writeback_dirty_inode_start(inode, flags);
11959fb0a7daSTejun Heo 
119603ba3782SJens Axboe 		if (sb->s_op->dirty_inode)
1197aa385729SChristoph Hellwig 			sb->s_op->dirty_inode(inode, flags);
11989fb0a7daSTejun Heo 
11999fb0a7daSTejun Heo 		trace_writeback_dirty_inode(inode, flags);
120003ba3782SJens Axboe 	}
12010ae45f63STheodore Ts'o 	if (flags & I_DIRTY_INODE)
12020ae45f63STheodore Ts'o 		flags &= ~I_DIRTY_TIME;
12030ae45f63STheodore Ts'o 	dirtytime = flags & I_DIRTY_TIME;
120403ba3782SJens Axboe 
120503ba3782SJens Axboe 	/*
12069c6ac78eSTejun Heo 	 * Paired with smp_mb() in __writeback_single_inode() for the
12079c6ac78eSTejun Heo 	 * following lockless i_state test.  See there for details.
120803ba3782SJens Axboe 	 */
120903ba3782SJens Axboe 	smp_mb();
121003ba3782SJens Axboe 
12110ae45f63STheodore Ts'o 	if (((inode->i_state & flags) == flags) ||
12120ae45f63STheodore Ts'o 	    (dirtytime && (inode->i_state & I_DIRTY_INODE)))
121303ba3782SJens Axboe 		return;
121403ba3782SJens Axboe 
121503ba3782SJens Axboe 	if (unlikely(block_dump))
121603ba3782SJens Axboe 		block_dump___mark_inode_dirty(inode);
121703ba3782SJens Axboe 
1218250df6edSDave Chinner 	spin_lock(&inode->i_lock);
12190ae45f63STheodore Ts'o 	if (dirtytime && (inode->i_state & I_DIRTY_INODE))
12200ae45f63STheodore Ts'o 		goto out_unlock_inode;
122103ba3782SJens Axboe 	if ((inode->i_state & flags) != flags) {
122203ba3782SJens Axboe 		const int was_dirty = inode->i_state & I_DIRTY;
122303ba3782SJens Axboe 
12240ae45f63STheodore Ts'o 		if (flags & I_DIRTY_INODE)
12250ae45f63STheodore Ts'o 			inode->i_state &= ~I_DIRTY_TIME;
122603ba3782SJens Axboe 		inode->i_state |= flags;
122703ba3782SJens Axboe 
122803ba3782SJens Axboe 		/*
122903ba3782SJens Axboe 		 * If the inode is being synced, just update its dirty state.
123003ba3782SJens Axboe 		 * The unlocker will place the inode on the appropriate
123103ba3782SJens Axboe 		 * superblock list, based upon its state.
123203ba3782SJens Axboe 		 */
123303ba3782SJens Axboe 		if (inode->i_state & I_SYNC)
1234250df6edSDave Chinner 			goto out_unlock_inode;
123503ba3782SJens Axboe 
123603ba3782SJens Axboe 		/*
123703ba3782SJens Axboe 		 * Only add valid (hashed) inodes to the superblock's
123803ba3782SJens Axboe 		 * dirty list.  Add blockdev inodes as well.
123903ba3782SJens Axboe 		 */
124003ba3782SJens Axboe 		if (!S_ISBLK(inode->i_mode)) {
12411d3382cbSAl Viro 			if (inode_unhashed(inode))
1242250df6edSDave Chinner 				goto out_unlock_inode;
124303ba3782SJens Axboe 		}
1244a4ffdde6SAl Viro 		if (inode->i_state & I_FREEING)
1245250df6edSDave Chinner 			goto out_unlock_inode;
124603ba3782SJens Axboe 
124703ba3782SJens Axboe 		/*
124803ba3782SJens Axboe 		 * If the inode was already on b_dirty/b_io/b_more_io, don't
124903ba3782SJens Axboe 		 * reposition it (that would break b_dirty time-ordering).
125003ba3782SJens Axboe 		 */
125103ba3782SJens Axboe 		if (!was_dirty) {
1252a66979abSDave Chinner 			bool wakeup_bdi = false;
1253253c34e9SArtem Bityutskiy 			bdi = inode_to_bdi(inode);
1254500b067cSJens Axboe 
1255146d7009SJunxiao Bi 			spin_unlock(&inode->i_lock);
1256146d7009SJunxiao Bi 			spin_lock(&bdi->wb.list_lock);
1257253c34e9SArtem Bityutskiy 			if (bdi_cap_writeback_dirty(bdi)) {
1258253c34e9SArtem Bityutskiy 				WARN(!test_bit(BDI_registered, &bdi->state),
1259253c34e9SArtem Bityutskiy 				     "bdi-%s not registered\n", bdi->name);
1260253c34e9SArtem Bityutskiy 
1261253c34e9SArtem Bityutskiy 				/*
1262253c34e9SArtem Bityutskiy 				 * If this is the first dirty inode for this
1263253c34e9SArtem Bityutskiy 				 * bdi, we have to wake-up the corresponding
1264253c34e9SArtem Bityutskiy 				 * bdi thread to make sure background
1265253c34e9SArtem Bityutskiy 				 * write-back happens later.
1266253c34e9SArtem Bityutskiy 				 */
1267253c34e9SArtem Bityutskiy 				if (!wb_has_dirty_io(&bdi->wb))
1268253c34e9SArtem Bityutskiy 					wakeup_bdi = true;
1269500b067cSJens Axboe 			}
127003ba3782SJens Axboe 
127103ba3782SJens Axboe 			inode->dirtied_when = jiffies;
12720ae45f63STheodore Ts'o 			list_move(&inode->i_wb_list, dirtytime ?
12730ae45f63STheodore Ts'o 				  &bdi->wb.b_dirty_time : &bdi->wb.b_dirty);
1274f758eeabSChristoph Hellwig 			spin_unlock(&bdi->wb.list_lock);
12750ae45f63STheodore Ts'o 			trace_writeback_dirty_inode_enqueue(inode);
1276253c34e9SArtem Bityutskiy 
1277253c34e9SArtem Bityutskiy 			if (wakeup_bdi)
12786467716aSArtem Bityutskiy 				bdi_wakeup_thread_delayed(bdi);
1279a66979abSDave Chinner 			return;
1280a66979abSDave Chinner 		}
1281a66979abSDave Chinner 	}
1282a66979abSDave Chinner out_unlock_inode:
1283a66979abSDave Chinner 	spin_unlock(&inode->i_lock);
1284a66979abSDave Chinner 
128503ba3782SJens Axboe }
128603ba3782SJens Axboe EXPORT_SYMBOL(__mark_inode_dirty);
128703ba3782SJens Axboe 
1288b6e51316SJens Axboe static void wait_sb_inodes(struct super_block *sb)
128966f3b8e2SJens Axboe {
129038f21977SNick Piggin 	struct inode *inode, *old_inode = NULL;
129138f21977SNick Piggin 
129203ba3782SJens Axboe 	/*
129303ba3782SJens Axboe 	 * We need to be protected against the filesystem going from
129403ba3782SJens Axboe 	 * r/o to r/w or vice versa.
129503ba3782SJens Axboe 	 */
1296b6e51316SJens Axboe 	WARN_ON(!rwsem_is_locked(&sb->s_umount));
129703ba3782SJens Axboe 
129855fa6091SDave Chinner 	spin_lock(&inode_sb_list_lock);
129966f3b8e2SJens Axboe 
130038f21977SNick Piggin 	/*
130138f21977SNick Piggin 	 * Data integrity sync. Must wait for all pages under writeback,
130238f21977SNick Piggin 	 * because there may have been pages dirtied before our sync
130338f21977SNick Piggin 	 * call, but which had writeout started before we write it out.
130438f21977SNick Piggin 	 * In which case, the inode may not be on the dirty list, but
130538f21977SNick Piggin 	 * we still have to wait for that writeout.
130638f21977SNick Piggin 	 */
1307b6e51316SJens Axboe 	list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
1308250df6edSDave Chinner 		struct address_space *mapping = inode->i_mapping;
130938f21977SNick Piggin 
1310250df6edSDave Chinner 		spin_lock(&inode->i_lock);
1311250df6edSDave Chinner 		if ((inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW)) ||
1312250df6edSDave Chinner 		    (mapping->nrpages == 0)) {
1313250df6edSDave Chinner 			spin_unlock(&inode->i_lock);
131438f21977SNick Piggin 			continue;
1315250df6edSDave Chinner 		}
131638f21977SNick Piggin 		__iget(inode);
1317250df6edSDave Chinner 		spin_unlock(&inode->i_lock);
131855fa6091SDave Chinner 		spin_unlock(&inode_sb_list_lock);
131955fa6091SDave Chinner 
132038f21977SNick Piggin 		/*
132155fa6091SDave Chinner 		 * We hold a reference to 'inode' so it couldn't have been
132255fa6091SDave Chinner 		 * removed from s_inodes list while we dropped the
132355fa6091SDave Chinner 		 * inode_sb_list_lock.  We cannot iput the inode now as we can
132455fa6091SDave Chinner 		 * be holding the last reference and we cannot iput it under
132555fa6091SDave Chinner 		 * inode_sb_list_lock. So we keep the reference and iput it
132655fa6091SDave Chinner 		 * later.
132738f21977SNick Piggin 		 */
132838f21977SNick Piggin 		iput(old_inode);
132938f21977SNick Piggin 		old_inode = inode;
133038f21977SNick Piggin 
133138f21977SNick Piggin 		filemap_fdatawait(mapping);
133238f21977SNick Piggin 
133338f21977SNick Piggin 		cond_resched();
133438f21977SNick Piggin 
133555fa6091SDave Chinner 		spin_lock(&inode_sb_list_lock);
133638f21977SNick Piggin 	}
133755fa6091SDave Chinner 	spin_unlock(&inode_sb_list_lock);
133838f21977SNick Piggin 	iput(old_inode);
133966f3b8e2SJens Axboe }
13401da177e4SLinus Torvalds 
1341d8a8559cSJens Axboe /**
13423259f8beSChris Mason  * writeback_inodes_sb_nr -	writeback dirty inodes from given super_block
1343d8a8559cSJens Axboe  * @sb: the superblock
13443259f8beSChris Mason  * @nr: the number of pages to write
1345786228abSMarcos Paulo de Souza  * @reason: reason why some writeback work initiated
13461da177e4SLinus Torvalds  *
1347d8a8559cSJens Axboe  * Start writeback on some inodes on this super_block. No guarantees are made
1348d8a8559cSJens Axboe  * on how many (if any) will be written, and this function does not wait
13493259f8beSChris Mason  * for IO completion of submitted IO.
13501da177e4SLinus Torvalds  */
13510e175a18SCurt Wohlgemuth void writeback_inodes_sb_nr(struct super_block *sb,
13520e175a18SCurt Wohlgemuth 			    unsigned long nr,
13530e175a18SCurt Wohlgemuth 			    enum wb_reason reason)
13541da177e4SLinus Torvalds {
135583ba7b07SChristoph Hellwig 	DECLARE_COMPLETION_ONSTACK(done);
135683ba7b07SChristoph Hellwig 	struct wb_writeback_work work = {
13573c4d7165SChristoph Hellwig 		.sb			= sb,
13583c4d7165SChristoph Hellwig 		.sync_mode		= WB_SYNC_NONE,
13596e6938b6SWu Fengguang 		.tagged_writepages	= 1,
136083ba7b07SChristoph Hellwig 		.done			= &done,
13613259f8beSChris Mason 		.nr_pages		= nr,
13620e175a18SCurt Wohlgemuth 		.reason			= reason,
13633c4d7165SChristoph Hellwig 	};
13640e3c9a22SJens Axboe 
13656eedc701SJan Kara 	if (sb->s_bdi == &noop_backing_dev_info)
13666eedc701SJan Kara 		return;
1367cf37e972SChristoph Hellwig 	WARN_ON(!rwsem_is_locked(&sb->s_umount));
136883ba7b07SChristoph Hellwig 	bdi_queue_work(sb->s_bdi, &work);
136983ba7b07SChristoph Hellwig 	wait_for_completion(&done);
13701da177e4SLinus Torvalds }
13713259f8beSChris Mason EXPORT_SYMBOL(writeback_inodes_sb_nr);
13723259f8beSChris Mason 
13733259f8beSChris Mason /**
13743259f8beSChris Mason  * writeback_inodes_sb	-	writeback dirty inodes from given super_block
13753259f8beSChris Mason  * @sb: the superblock
1376786228abSMarcos Paulo de Souza  * @reason: reason why some writeback work was initiated
13773259f8beSChris Mason  *
13783259f8beSChris Mason  * Start writeback on some inodes on this super_block. No guarantees are made
13793259f8beSChris Mason  * on how many (if any) will be written, and this function does not wait
13803259f8beSChris Mason  * for IO completion of submitted IO.
13813259f8beSChris Mason  */
13820e175a18SCurt Wohlgemuth void writeback_inodes_sb(struct super_block *sb, enum wb_reason reason)
13833259f8beSChris Mason {
13840e175a18SCurt Wohlgemuth 	return writeback_inodes_sb_nr(sb, get_nr_dirty_pages(), reason);
13853259f8beSChris Mason }
1386d8a8559cSJens Axboe EXPORT_SYMBOL(writeback_inodes_sb);
1387d8a8559cSJens Axboe 
1388d8a8559cSJens Axboe /**
138910ee27a0SMiao Xie  * try_to_writeback_inodes_sb_nr - try to start writeback if none underway
13903259f8beSChris Mason  * @sb: the superblock
13913259f8beSChris Mason  * @nr: the number of pages to write
139210ee27a0SMiao Xie  * @reason: the reason of writeback
13933259f8beSChris Mason  *
139410ee27a0SMiao Xie  * Invoke writeback_inodes_sb_nr if no writeback is currently underway.
13953259f8beSChris Mason  * Returns 1 if writeback was started, 0 if not.
13963259f8beSChris Mason  */
139710ee27a0SMiao Xie int try_to_writeback_inodes_sb_nr(struct super_block *sb,
13980e175a18SCurt Wohlgemuth 				  unsigned long nr,
13990e175a18SCurt Wohlgemuth 				  enum wb_reason reason)
14003259f8beSChris Mason {
140110ee27a0SMiao Xie 	if (writeback_in_progress(sb->s_bdi))
140210ee27a0SMiao Xie 		return 1;
140310ee27a0SMiao Xie 
140410ee27a0SMiao Xie 	if (!down_read_trylock(&sb->s_umount))
140510ee27a0SMiao Xie 		return 0;
140610ee27a0SMiao Xie 
14070e175a18SCurt Wohlgemuth 	writeback_inodes_sb_nr(sb, nr, reason);
14083259f8beSChris Mason 	up_read(&sb->s_umount);
14093259f8beSChris Mason 	return 1;
14103259f8beSChris Mason }
141110ee27a0SMiao Xie EXPORT_SYMBOL(try_to_writeback_inodes_sb_nr);
141210ee27a0SMiao Xie 
141310ee27a0SMiao Xie /**
141410ee27a0SMiao Xie  * try_to_writeback_inodes_sb - try to start writeback if none underway
141510ee27a0SMiao Xie  * @sb: the superblock
141610ee27a0SMiao Xie  * @reason: reason why some writeback work was initiated
141710ee27a0SMiao Xie  *
141810ee27a0SMiao Xie  * Implement by try_to_writeback_inodes_sb_nr()
141910ee27a0SMiao Xie  * Returns 1 if writeback was started, 0 if not.
142010ee27a0SMiao Xie  */
142110ee27a0SMiao Xie int try_to_writeback_inodes_sb(struct super_block *sb, enum wb_reason reason)
142210ee27a0SMiao Xie {
142310ee27a0SMiao Xie 	return try_to_writeback_inodes_sb_nr(sb, get_nr_dirty_pages(), reason);
142410ee27a0SMiao Xie }
142510ee27a0SMiao Xie EXPORT_SYMBOL(try_to_writeback_inodes_sb);
14263259f8beSChris Mason 
14273259f8beSChris Mason /**
1428d8a8559cSJens Axboe  * sync_inodes_sb	-	sync sb inode pages
1429d8a8559cSJens Axboe  * @sb: the superblock
1430d8a8559cSJens Axboe  *
1431d8a8559cSJens Axboe  * This function writes and waits on any dirty inode belonging to this
14320dc83bd3SJan Kara  * super_block.
1433d8a8559cSJens Axboe  */
14340dc83bd3SJan Kara void sync_inodes_sb(struct super_block *sb)
1435d8a8559cSJens Axboe {
143683ba7b07SChristoph Hellwig 	DECLARE_COMPLETION_ONSTACK(done);
143783ba7b07SChristoph Hellwig 	struct wb_writeback_work work = {
14383c4d7165SChristoph Hellwig 		.sb		= sb,
14393c4d7165SChristoph Hellwig 		.sync_mode	= WB_SYNC_ALL,
14403c4d7165SChristoph Hellwig 		.nr_pages	= LONG_MAX,
14413c4d7165SChristoph Hellwig 		.range_cyclic	= 0,
144283ba7b07SChristoph Hellwig 		.done		= &done,
14430e175a18SCurt Wohlgemuth 		.reason		= WB_REASON_SYNC,
14447747bd4bSDave Chinner 		.for_sync	= 1,
14453c4d7165SChristoph Hellwig 	};
14463c4d7165SChristoph Hellwig 
14476eedc701SJan Kara 	/* Nothing to do? */
14486eedc701SJan Kara 	if (sb->s_bdi == &noop_backing_dev_info)
14496eedc701SJan Kara 		return;
1450cf37e972SChristoph Hellwig 	WARN_ON(!rwsem_is_locked(&sb->s_umount));
1451cf37e972SChristoph Hellwig 
145283ba7b07SChristoph Hellwig 	bdi_queue_work(sb->s_bdi, &work);
145383ba7b07SChristoph Hellwig 	wait_for_completion(&done);
145483ba7b07SChristoph Hellwig 
1455b6e51316SJens Axboe 	wait_sb_inodes(sb);
1456d8a8559cSJens Axboe }
1457d8a8559cSJens Axboe EXPORT_SYMBOL(sync_inodes_sb);
14581da177e4SLinus Torvalds 
14591da177e4SLinus Torvalds /**
14601da177e4SLinus Torvalds  * write_inode_now	-	write an inode to disk
14611da177e4SLinus Torvalds  * @inode: inode to write to disk
14621da177e4SLinus Torvalds  * @sync: whether the write should be synchronous or not
14631da177e4SLinus Torvalds  *
14647f04c26dSAndrea Arcangeli  * This function commits an inode to disk immediately if it is dirty. This is
14657f04c26dSAndrea Arcangeli  * primarily needed by knfsd.
14667f04c26dSAndrea Arcangeli  *
14677f04c26dSAndrea Arcangeli  * The caller must either have a ref on the inode or must have set I_WILL_FREE.
14681da177e4SLinus Torvalds  */
14691da177e4SLinus Torvalds int write_inode_now(struct inode *inode, int sync)
14701da177e4SLinus Torvalds {
1471f758eeabSChristoph Hellwig 	struct bdi_writeback *wb = &inode_to_bdi(inode)->wb;
14721da177e4SLinus Torvalds 	struct writeback_control wbc = {
14731da177e4SLinus Torvalds 		.nr_to_write = LONG_MAX,
147418914b18SMike Galbraith 		.sync_mode = sync ? WB_SYNC_ALL : WB_SYNC_NONE,
1475111ebb6eSOGAWA Hirofumi 		.range_start = 0,
1476111ebb6eSOGAWA Hirofumi 		.range_end = LLONG_MAX,
14771da177e4SLinus Torvalds 	};
14781da177e4SLinus Torvalds 
14791da177e4SLinus Torvalds 	if (!mapping_cap_writeback_dirty(inode->i_mapping))
148049364ce2SAndrew Morton 		wbc.nr_to_write = 0;
14811da177e4SLinus Torvalds 
14821da177e4SLinus Torvalds 	might_sleep();
14834f8ad655SJan Kara 	return writeback_single_inode(inode, wb, &wbc);
14841da177e4SLinus Torvalds }
14851da177e4SLinus Torvalds EXPORT_SYMBOL(write_inode_now);
14861da177e4SLinus Torvalds 
14871da177e4SLinus Torvalds /**
14881da177e4SLinus Torvalds  * sync_inode - write an inode and its pages to disk.
14891da177e4SLinus Torvalds  * @inode: the inode to sync
14901da177e4SLinus Torvalds  * @wbc: controls the writeback mode
14911da177e4SLinus Torvalds  *
14921da177e4SLinus Torvalds  * sync_inode() will write an inode and its pages to disk.  It will also
14931da177e4SLinus Torvalds  * correctly update the inode on its superblock's dirty inode lists and will
14941da177e4SLinus Torvalds  * update inode->i_state.
14951da177e4SLinus Torvalds  *
14961da177e4SLinus Torvalds  * The caller must have a ref on the inode.
14971da177e4SLinus Torvalds  */
14981da177e4SLinus Torvalds int sync_inode(struct inode *inode, struct writeback_control *wbc)
14991da177e4SLinus Torvalds {
15004f8ad655SJan Kara 	return writeback_single_inode(inode, &inode_to_bdi(inode)->wb, wbc);
15011da177e4SLinus Torvalds }
15021da177e4SLinus Torvalds EXPORT_SYMBOL(sync_inode);
1503c3765016SChristoph Hellwig 
1504c3765016SChristoph Hellwig /**
1505c691b9d9SAndrew Morton  * sync_inode_metadata - write an inode to disk
1506c3765016SChristoph Hellwig  * @inode: the inode to sync
1507c3765016SChristoph Hellwig  * @wait: wait for I/O to complete.
1508c3765016SChristoph Hellwig  *
1509c691b9d9SAndrew Morton  * Write an inode to disk and adjust its dirty state after completion.
1510c3765016SChristoph Hellwig  *
1511c3765016SChristoph Hellwig  * Note: only writes the actual inode, no associated data or other metadata.
1512c3765016SChristoph Hellwig  */
1513c3765016SChristoph Hellwig int sync_inode_metadata(struct inode *inode, int wait)
1514c3765016SChristoph Hellwig {
1515c3765016SChristoph Hellwig 	struct writeback_control wbc = {
1516c3765016SChristoph Hellwig 		.sync_mode = wait ? WB_SYNC_ALL : WB_SYNC_NONE,
1517c3765016SChristoph Hellwig 		.nr_to_write = 0, /* metadata-only */
1518c3765016SChristoph Hellwig 	};
1519c3765016SChristoph Hellwig 
1520c3765016SChristoph Hellwig 	return sync_inode(inode, &wbc);
1521c3765016SChristoph Hellwig }
1522c3765016SChristoph Hellwig EXPORT_SYMBOL(sync_inode_metadata);
1523