xref: /openbmc/linux/fs/fs-writeback.c (revision 0e995816)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * fs/fs-writeback.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Copyright (C) 2002, Linus Torvalds.
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  * Contains all the functions related to writing back and waiting
71da177e4SLinus Torvalds  * upon dirty inodes against superblocks, and writing back dirty
81da177e4SLinus Torvalds  * pages against inodes.  ie: data writeback.  Writeout of the
91da177e4SLinus Torvalds  * inode itself is not handled here.
101da177e4SLinus Torvalds  *
11e1f8e874SFrancois Cami  * 10Apr2002	Andrew Morton
121da177e4SLinus Torvalds  *		Split out of fs/inode.c
131da177e4SLinus Torvalds  *		Additions for address_space-based writeback
141da177e4SLinus Torvalds  */
151da177e4SLinus Torvalds 
161da177e4SLinus Torvalds #include <linux/kernel.h>
17f5ff8422SJens Axboe #include <linux/module.h>
181da177e4SLinus Torvalds #include <linux/spinlock.h>
195a0e3ad6STejun Heo #include <linux/slab.h>
201da177e4SLinus Torvalds #include <linux/sched.h>
211da177e4SLinus Torvalds #include <linux/fs.h>
221da177e4SLinus Torvalds #include <linux/mm.h>
2303ba3782SJens Axboe #include <linux/kthread.h>
2403ba3782SJens Axboe #include <linux/freezer.h>
251da177e4SLinus Torvalds #include <linux/writeback.h>
261da177e4SLinus Torvalds #include <linux/blkdev.h>
271da177e4SLinus Torvalds #include <linux/backing-dev.h>
281da177e4SLinus Torvalds #include <linux/buffer_head.h>
29455b2864SDave Chinner #include <linux/tracepoint.h>
3007f3f05cSDavid Howells #include "internal.h"
311da177e4SLinus Torvalds 
32d0bceac7SJens Axboe /*
33c4a77a6cSJens Axboe  * Passed into wb_writeback(), essentially a subset of writeback_control
34c4a77a6cSJens Axboe  */
3583ba7b07SChristoph Hellwig struct wb_writeback_work {
36c4a77a6cSJens Axboe 	long nr_pages;
37c4a77a6cSJens Axboe 	struct super_block *sb;
38d46db3d5SWu Fengguang 	unsigned long *older_than_this;
39c4a77a6cSJens Axboe 	enum writeback_sync_modes sync_mode;
406e6938b6SWu Fengguang 	unsigned int tagged_writepages:1;
4152957fe1SH Hartley Sweeten 	unsigned int for_kupdate:1;
4252957fe1SH Hartley Sweeten 	unsigned int range_cyclic:1;
4352957fe1SH Hartley Sweeten 	unsigned int for_background:1;
44c4a77a6cSJens Axboe 
458010c3b6SJens Axboe 	struct list_head list;		/* pending work list */
4683ba7b07SChristoph Hellwig 	struct completion *done;	/* set if the caller waits */
4703ba3782SJens Axboe };
4803ba3782SJens Axboe 
49455b2864SDave Chinner /*
50455b2864SDave Chinner  * Include the creation of the trace points after defining the
51455b2864SDave Chinner  * wb_writeback_work structure so that the definition remains local to this
52455b2864SDave Chinner  * file.
53455b2864SDave Chinner  */
54455b2864SDave Chinner #define CREATE_TRACE_POINTS
55455b2864SDave Chinner #include <trace/events/writeback.h>
56455b2864SDave Chinner 
57455b2864SDave Chinner /*
58455b2864SDave Chinner  * We don't actually have pdflush, but this one is exported though /proc...
59455b2864SDave Chinner  */
60455b2864SDave Chinner int nr_pdflush_threads;
61455b2864SDave Chinner 
62f11b00f3SAdrian Bunk /**
63f11b00f3SAdrian Bunk  * writeback_in_progress - determine whether there is writeback in progress
64f11b00f3SAdrian Bunk  * @bdi: the device's backing_dev_info structure.
65f11b00f3SAdrian Bunk  *
6603ba3782SJens Axboe  * Determine whether there is writeback waiting to be handled against a
6703ba3782SJens Axboe  * backing device.
68f11b00f3SAdrian Bunk  */
69f11b00f3SAdrian Bunk int writeback_in_progress(struct backing_dev_info *bdi)
70f11b00f3SAdrian Bunk {
7181d73a32SJan Kara 	return test_bit(BDI_writeback_running, &bdi->state);
72f11b00f3SAdrian Bunk }
73f11b00f3SAdrian Bunk 
74692ebd17SJan Kara static inline struct backing_dev_info *inode_to_bdi(struct inode *inode)
75692ebd17SJan Kara {
76692ebd17SJan Kara 	struct super_block *sb = inode->i_sb;
77692ebd17SJan Kara 
78aaead25bSChristoph Hellwig 	if (strcmp(sb->s_type->name, "bdev") == 0)
79aaead25bSChristoph Hellwig 		return inode->i_mapping->backing_dev_info;
80aaead25bSChristoph Hellwig 
81692ebd17SJan Kara 	return sb->s_bdi;
82692ebd17SJan Kara }
83692ebd17SJan Kara 
847ccf19a8SNick Piggin static inline struct inode *wb_inode(struct list_head *head)
857ccf19a8SNick Piggin {
867ccf19a8SNick Piggin 	return list_entry(head, struct inode, i_wb_list);
877ccf19a8SNick Piggin }
887ccf19a8SNick Piggin 
896585027aSJan Kara /* Wakeup flusher thread or forker thread to fork it. Requires bdi->wb_lock. */
906585027aSJan Kara static void bdi_wakeup_flusher(struct backing_dev_info *bdi)
914195f73dSNick Piggin {
92fff5b85aSArtem Bityutskiy 	if (bdi->wb.task) {
93fff5b85aSArtem Bityutskiy 		wake_up_process(bdi->wb.task);
94fff5b85aSArtem Bityutskiy 	} else {
951da177e4SLinus Torvalds 		/*
96fff5b85aSArtem Bityutskiy 		 * The bdi thread isn't there, wake up the forker thread which
97fff5b85aSArtem Bityutskiy 		 * will create and run it.
981da177e4SLinus Torvalds 		 */
9903ba3782SJens Axboe 		wake_up_process(default_backing_dev_info.wb.task);
1001da177e4SLinus Torvalds 	}
1016585027aSJan Kara }
1026585027aSJan Kara 
1036585027aSJan Kara static void bdi_queue_work(struct backing_dev_info *bdi,
1046585027aSJan Kara 			   struct wb_writeback_work *work)
1056585027aSJan Kara {
1066585027aSJan Kara 	trace_writeback_queue(bdi, work);
1076585027aSJan Kara 
1086585027aSJan Kara 	spin_lock_bh(&bdi->wb_lock);
1096585027aSJan Kara 	list_add_tail(&work->list, &bdi->work_list);
1106585027aSJan Kara 	if (!bdi->wb.task)
1116585027aSJan Kara 		trace_writeback_nothread(bdi, work);
1126585027aSJan Kara 	bdi_wakeup_flusher(bdi);
1136467716aSArtem Bityutskiy 	spin_unlock_bh(&bdi->wb_lock);
11403ba3782SJens Axboe }
1151da177e4SLinus Torvalds 
11683ba7b07SChristoph Hellwig static void
11783ba7b07SChristoph Hellwig __bdi_start_writeback(struct backing_dev_info *bdi, long nr_pages,
1186585027aSJan Kara 		      bool range_cyclic)
1191da177e4SLinus Torvalds {
12083ba7b07SChristoph Hellwig 	struct wb_writeback_work *work;
12103ba3782SJens Axboe 
122bcddc3f0SJens Axboe 	/*
123bcddc3f0SJens Axboe 	 * This is WB_SYNC_NONE writeback, so if allocation fails just
124bcddc3f0SJens Axboe 	 * wakeup the thread for old dirty data writeback
125bcddc3f0SJens Axboe 	 */
12683ba7b07SChristoph Hellwig 	work = kzalloc(sizeof(*work), GFP_ATOMIC);
12783ba7b07SChristoph Hellwig 	if (!work) {
128455b2864SDave Chinner 		if (bdi->wb.task) {
129455b2864SDave Chinner 			trace_writeback_nowork(bdi);
13083ba7b07SChristoph Hellwig 			wake_up_process(bdi->wb.task);
131455b2864SDave Chinner 		}
13283ba7b07SChristoph Hellwig 		return;
13383ba7b07SChristoph Hellwig 	}
13483ba7b07SChristoph Hellwig 
13583ba7b07SChristoph Hellwig 	work->sync_mode	= WB_SYNC_NONE;
13683ba7b07SChristoph Hellwig 	work->nr_pages	= nr_pages;
13783ba7b07SChristoph Hellwig 	work->range_cyclic = range_cyclic;
13883ba7b07SChristoph Hellwig 
139f11fcae8SJens Axboe 	bdi_queue_work(bdi, work);
14003ba3782SJens Axboe }
141b6e51316SJens Axboe 
142b6e51316SJens Axboe /**
143b6e51316SJens Axboe  * bdi_start_writeback - start writeback
144b6e51316SJens Axboe  * @bdi: the backing device to write from
145b6e51316SJens Axboe  * @nr_pages: the number of pages to write
146b6e51316SJens Axboe  *
147b6e51316SJens Axboe  * Description:
148b6e51316SJens Axboe  *   This does WB_SYNC_NONE opportunistic writeback. The IO is only
14925985edcSLucas De Marchi  *   started when this function returns, we make no guarantees on
1500e3c9a22SJens Axboe  *   completion. Caller need not hold sb s_umount semaphore.
151b6e51316SJens Axboe  *
152b6e51316SJens Axboe  */
153c5444198SChristoph Hellwig void bdi_start_writeback(struct backing_dev_info *bdi, long nr_pages)
154b6e51316SJens Axboe {
1556585027aSJan Kara 	__bdi_start_writeback(bdi, nr_pages, true);
156d3ddec76SWu Fengguang }
157d3ddec76SWu Fengguang 
158c5444198SChristoph Hellwig /**
159c5444198SChristoph Hellwig  * bdi_start_background_writeback - start background writeback
160c5444198SChristoph Hellwig  * @bdi: the backing device to write from
161c5444198SChristoph Hellwig  *
162c5444198SChristoph Hellwig  * Description:
1636585027aSJan Kara  *   This makes sure WB_SYNC_NONE background writeback happens. When
1646585027aSJan Kara  *   this function returns, it is only guaranteed that for given BDI
1656585027aSJan Kara  *   some IO is happening if we are over background dirty threshold.
1666585027aSJan Kara  *   Caller need not hold sb s_umount semaphore.
167c5444198SChristoph Hellwig  */
168c5444198SChristoph Hellwig void bdi_start_background_writeback(struct backing_dev_info *bdi)
169c5444198SChristoph Hellwig {
1706585027aSJan Kara 	/*
1716585027aSJan Kara 	 * We just wake up the flusher thread. It will perform background
1726585027aSJan Kara 	 * writeback as soon as there is no other work to do.
1736585027aSJan Kara 	 */
17471927e84SWu Fengguang 	trace_writeback_wake_background(bdi);
1756585027aSJan Kara 	spin_lock_bh(&bdi->wb_lock);
1766585027aSJan Kara 	bdi_wakeup_flusher(bdi);
1776585027aSJan Kara 	spin_unlock_bh(&bdi->wb_lock);
1781da177e4SLinus Torvalds }
1791da177e4SLinus Torvalds 
1801da177e4SLinus Torvalds /*
181a66979abSDave Chinner  * Remove the inode from the writeback list it is on.
182a66979abSDave Chinner  */
183a66979abSDave Chinner void inode_wb_list_del(struct inode *inode)
184a66979abSDave Chinner {
185f758eeabSChristoph Hellwig 	struct backing_dev_info *bdi = inode_to_bdi(inode);
186a66979abSDave Chinner 
187f758eeabSChristoph Hellwig 	spin_lock(&bdi->wb.list_lock);
188f758eeabSChristoph Hellwig 	list_del_init(&inode->i_wb_list);
189f758eeabSChristoph Hellwig 	spin_unlock(&bdi->wb.list_lock);
190f758eeabSChristoph Hellwig }
191a66979abSDave Chinner 
192a66979abSDave Chinner /*
1936610a0bcSAndrew Morton  * Redirty an inode: set its when-it-was dirtied timestamp and move it to the
1946610a0bcSAndrew Morton  * furthest end of its superblock's dirty-inode list.
1956610a0bcSAndrew Morton  *
1966610a0bcSAndrew Morton  * Before stamping the inode's ->dirtied_when, we check to see whether it is
19766f3b8e2SJens Axboe  * already the most-recently-dirtied inode on the b_dirty list.  If that is
1986610a0bcSAndrew Morton  * the case then the inode must have been redirtied while it was being written
1996610a0bcSAndrew Morton  * out and we don't reset its dirtied_when.
2006610a0bcSAndrew Morton  */
201f758eeabSChristoph Hellwig static void redirty_tail(struct inode *inode, struct bdi_writeback *wb)
2026610a0bcSAndrew Morton {
203f758eeabSChristoph Hellwig 	assert_spin_locked(&wb->list_lock);
20403ba3782SJens Axboe 	if (!list_empty(&wb->b_dirty)) {
20566f3b8e2SJens Axboe 		struct inode *tail;
2066610a0bcSAndrew Morton 
2077ccf19a8SNick Piggin 		tail = wb_inode(wb->b_dirty.next);
20866f3b8e2SJens Axboe 		if (time_before(inode->dirtied_when, tail->dirtied_when))
2096610a0bcSAndrew Morton 			inode->dirtied_when = jiffies;
2106610a0bcSAndrew Morton 	}
2117ccf19a8SNick Piggin 	list_move(&inode->i_wb_list, &wb->b_dirty);
2126610a0bcSAndrew Morton }
2136610a0bcSAndrew Morton 
2146610a0bcSAndrew Morton /*
21566f3b8e2SJens Axboe  * requeue inode for re-scanning after bdi->b_io list is exhausted.
216c986d1e2SAndrew Morton  */
217f758eeabSChristoph Hellwig static void requeue_io(struct inode *inode, struct bdi_writeback *wb)
218c986d1e2SAndrew Morton {
219f758eeabSChristoph Hellwig 	assert_spin_locked(&wb->list_lock);
2207ccf19a8SNick Piggin 	list_move(&inode->i_wb_list, &wb->b_more_io);
221c986d1e2SAndrew Morton }
222c986d1e2SAndrew Morton 
2231c0eeaf5SJoern Engel static void inode_sync_complete(struct inode *inode)
2241c0eeaf5SJoern Engel {
2251c0eeaf5SJoern Engel 	/*
226a66979abSDave Chinner 	 * Prevent speculative execution through
227f758eeabSChristoph Hellwig 	 * spin_unlock(&wb->list_lock);
2281c0eeaf5SJoern Engel 	 */
229a66979abSDave Chinner 
2301c0eeaf5SJoern Engel 	smp_mb();
2311c0eeaf5SJoern Engel 	wake_up_bit(&inode->i_state, __I_SYNC);
2321c0eeaf5SJoern Engel }
2331c0eeaf5SJoern Engel 
234d2caa3c5SJeff Layton static bool inode_dirtied_after(struct inode *inode, unsigned long t)
235d2caa3c5SJeff Layton {
236d2caa3c5SJeff Layton 	bool ret = time_after(inode->dirtied_when, t);
237d2caa3c5SJeff Layton #ifndef CONFIG_64BIT
238d2caa3c5SJeff Layton 	/*
239d2caa3c5SJeff Layton 	 * For inodes being constantly redirtied, dirtied_when can get stuck.
240d2caa3c5SJeff Layton 	 * It _appears_ to be in the future, but is actually in distant past.
241d2caa3c5SJeff Layton 	 * This test is necessary to prevent such wrapped-around relative times
2425b0830cbSJens Axboe 	 * from permanently stopping the whole bdi writeback.
243d2caa3c5SJeff Layton 	 */
244d2caa3c5SJeff Layton 	ret = ret && time_before_eq(inode->dirtied_when, jiffies);
245d2caa3c5SJeff Layton #endif
246d2caa3c5SJeff Layton 	return ret;
247d2caa3c5SJeff Layton }
248d2caa3c5SJeff Layton 
249c986d1e2SAndrew Morton /*
2502c136579SFengguang Wu  * Move expired dirty inodes from @delaying_queue to @dispatch_queue.
2512c136579SFengguang Wu  */
252e84d0a4fSWu Fengguang static int move_expired_inodes(struct list_head *delaying_queue,
2532c136579SFengguang Wu 			       struct list_head *dispatch_queue,
2542c136579SFengguang Wu 			       unsigned long *older_than_this)
2552c136579SFengguang Wu {
2565c03449dSShaohua Li 	LIST_HEAD(tmp);
2575c03449dSShaohua Li 	struct list_head *pos, *node;
258cf137307SJens Axboe 	struct super_block *sb = NULL;
2595c03449dSShaohua Li 	struct inode *inode;
260cf137307SJens Axboe 	int do_sb_sort = 0;
261e84d0a4fSWu Fengguang 	int moved = 0;
2625c03449dSShaohua Li 
2632c136579SFengguang Wu 	while (!list_empty(delaying_queue)) {
2647ccf19a8SNick Piggin 		inode = wb_inode(delaying_queue->prev);
2652c136579SFengguang Wu 		if (older_than_this &&
266d2caa3c5SJeff Layton 		    inode_dirtied_after(inode, *older_than_this))
2672c136579SFengguang Wu 			break;
268cf137307SJens Axboe 		if (sb && sb != inode->i_sb)
269cf137307SJens Axboe 			do_sb_sort = 1;
270cf137307SJens Axboe 		sb = inode->i_sb;
2717ccf19a8SNick Piggin 		list_move(&inode->i_wb_list, &tmp);
272e84d0a4fSWu Fengguang 		moved++;
2735c03449dSShaohua Li 	}
2745c03449dSShaohua Li 
275cf137307SJens Axboe 	/* just one sb in list, splice to dispatch_queue and we're done */
276cf137307SJens Axboe 	if (!do_sb_sort) {
277cf137307SJens Axboe 		list_splice(&tmp, dispatch_queue);
278e84d0a4fSWu Fengguang 		goto out;
279cf137307SJens Axboe 	}
280cf137307SJens Axboe 
2815c03449dSShaohua Li 	/* Move inodes from one superblock together */
2825c03449dSShaohua Li 	while (!list_empty(&tmp)) {
2837ccf19a8SNick Piggin 		sb = wb_inode(tmp.prev)->i_sb;
2845c03449dSShaohua Li 		list_for_each_prev_safe(pos, node, &tmp) {
2857ccf19a8SNick Piggin 			inode = wb_inode(pos);
2865c03449dSShaohua Li 			if (inode->i_sb == sb)
2877ccf19a8SNick Piggin 				list_move(&inode->i_wb_list, dispatch_queue);
2882c136579SFengguang Wu 		}
2892c136579SFengguang Wu 	}
290e84d0a4fSWu Fengguang out:
291e84d0a4fSWu Fengguang 	return moved;
2925c03449dSShaohua Li }
2932c136579SFengguang Wu 
2942c136579SFengguang Wu /*
2952c136579SFengguang Wu  * Queue all expired dirty inodes for io, eldest first.
2964ea879b9SWu Fengguang  * Before
2974ea879b9SWu Fengguang  *         newly dirtied     b_dirty    b_io    b_more_io
2984ea879b9SWu Fengguang  *         =============>    gf         edc     BA
2994ea879b9SWu Fengguang  * After
3004ea879b9SWu Fengguang  *         newly dirtied     b_dirty    b_io    b_more_io
3014ea879b9SWu Fengguang  *         =============>    g          fBAedc
3024ea879b9SWu Fengguang  *                                           |
3034ea879b9SWu Fengguang  *                                           +--> dequeue for IO
3042c136579SFengguang Wu  */
30503ba3782SJens Axboe static void queue_io(struct bdi_writeback *wb, unsigned long *older_than_this)
3062c136579SFengguang Wu {
307e84d0a4fSWu Fengguang 	int moved;
308f758eeabSChristoph Hellwig 	assert_spin_locked(&wb->list_lock);
3094ea879b9SWu Fengguang 	list_splice_init(&wb->b_more_io, &wb->b_io);
310e84d0a4fSWu Fengguang 	moved = move_expired_inodes(&wb->b_dirty, &wb->b_io, older_than_this);
311e84d0a4fSWu Fengguang 	trace_writeback_queue_io(wb, older_than_this, moved);
31266f3b8e2SJens Axboe }
31366f3b8e2SJens Axboe 
314a9185b41SChristoph Hellwig static int write_inode(struct inode *inode, struct writeback_control *wbc)
31566f3b8e2SJens Axboe {
31603ba3782SJens Axboe 	if (inode->i_sb->s_op->write_inode && !is_bad_inode(inode))
317a9185b41SChristoph Hellwig 		return inode->i_sb->s_op->write_inode(inode, wbc);
31803ba3782SJens Axboe 	return 0;
31966f3b8e2SJens Axboe }
32008d8e974SFengguang Wu 
3212c136579SFengguang Wu /*
32201c03194SChristoph Hellwig  * Wait for writeback on an inode to complete.
32301c03194SChristoph Hellwig  */
324f758eeabSChristoph Hellwig static void inode_wait_for_writeback(struct inode *inode,
325f758eeabSChristoph Hellwig 				     struct bdi_writeback *wb)
32601c03194SChristoph Hellwig {
32701c03194SChristoph Hellwig 	DEFINE_WAIT_BIT(wq, &inode->i_state, __I_SYNC);
32801c03194SChristoph Hellwig 	wait_queue_head_t *wqh;
32901c03194SChristoph Hellwig 
33001c03194SChristoph Hellwig 	wqh = bit_waitqueue(&inode->i_state, __I_SYNC);
33158a9d3d8SRichard Kennedy 	while (inode->i_state & I_SYNC) {
332250df6edSDave Chinner 		spin_unlock(&inode->i_lock);
333f758eeabSChristoph Hellwig 		spin_unlock(&wb->list_lock);
33401c03194SChristoph Hellwig 		__wait_on_bit(wqh, &wq, inode_wait, TASK_UNINTERRUPTIBLE);
335f758eeabSChristoph Hellwig 		spin_lock(&wb->list_lock);
336250df6edSDave Chinner 		spin_lock(&inode->i_lock);
33758a9d3d8SRichard Kennedy 	}
33801c03194SChristoph Hellwig }
33901c03194SChristoph Hellwig 
34001c03194SChristoph Hellwig /*
341f758eeabSChristoph Hellwig  * Write out an inode's dirty pages.  Called under wb->list_lock and
3420f1b1fd8SDave Chinner  * inode->i_lock.  Either the caller has an active reference on the inode or
3430f1b1fd8SDave Chinner  * the inode has I_WILL_FREE set.
34401c03194SChristoph Hellwig  *
3451da177e4SLinus Torvalds  * If `wait' is set, wait on the writeout.
3461da177e4SLinus Torvalds  *
3471da177e4SLinus Torvalds  * The whole writeout design is quite complex and fragile.  We want to avoid
3481da177e4SLinus Torvalds  * starvation of particular inodes when others are being redirtied, prevent
3491da177e4SLinus Torvalds  * livelocks, etc.
3501da177e4SLinus Torvalds  */
3511da177e4SLinus Torvalds static int
352f758eeabSChristoph Hellwig writeback_single_inode(struct inode *inode, struct bdi_writeback *wb,
353f758eeabSChristoph Hellwig 		       struct writeback_control *wbc)
3541da177e4SLinus Torvalds {
3551da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
356251d6a47SWu Fengguang 	long nr_to_write = wbc->nr_to_write;
35701c03194SChristoph Hellwig 	unsigned dirty;
3581da177e4SLinus Torvalds 	int ret;
3591da177e4SLinus Torvalds 
360f758eeabSChristoph Hellwig 	assert_spin_locked(&wb->list_lock);
3610f1b1fd8SDave Chinner 	assert_spin_locked(&inode->i_lock);
3620f1b1fd8SDave Chinner 
36301c03194SChristoph Hellwig 	if (!atomic_read(&inode->i_count))
36401c03194SChristoph Hellwig 		WARN_ON(!(inode->i_state & (I_WILL_FREE|I_FREEING)));
36501c03194SChristoph Hellwig 	else
36601c03194SChristoph Hellwig 		WARN_ON(inode->i_state & I_WILL_FREE);
36701c03194SChristoph Hellwig 
36801c03194SChristoph Hellwig 	if (inode->i_state & I_SYNC) {
36901c03194SChristoph Hellwig 		/*
37001c03194SChristoph Hellwig 		 * If this inode is locked for writeback and we are not doing
37166f3b8e2SJens Axboe 		 * writeback-for-data-integrity, move it to b_more_io so that
37201c03194SChristoph Hellwig 		 * writeback can proceed with the other inodes on s_io.
37301c03194SChristoph Hellwig 		 *
37401c03194SChristoph Hellwig 		 * We'll have another go at writing back this inode when we
37566f3b8e2SJens Axboe 		 * completed a full scan of b_io.
37601c03194SChristoph Hellwig 		 */
377a9185b41SChristoph Hellwig 		if (wbc->sync_mode != WB_SYNC_ALL) {
378f758eeabSChristoph Hellwig 			requeue_io(inode, wb);
379251d6a47SWu Fengguang 			trace_writeback_single_inode_requeue(inode, wbc,
380251d6a47SWu Fengguang 							     nr_to_write);
38101c03194SChristoph Hellwig 			return 0;
38201c03194SChristoph Hellwig 		}
38301c03194SChristoph Hellwig 
38401c03194SChristoph Hellwig 		/*
38501c03194SChristoph Hellwig 		 * It's a data-integrity sync.  We must wait.
38601c03194SChristoph Hellwig 		 */
387f758eeabSChristoph Hellwig 		inode_wait_for_writeback(inode, wb);
38801c03194SChristoph Hellwig 	}
38901c03194SChristoph Hellwig 
3901c0eeaf5SJoern Engel 	BUG_ON(inode->i_state & I_SYNC);
3911da177e4SLinus Torvalds 
3925547e8aaSDmitry Monakhov 	/* Set I_SYNC, reset I_DIRTY_PAGES */
3931c0eeaf5SJoern Engel 	inode->i_state |= I_SYNC;
3945547e8aaSDmitry Monakhov 	inode->i_state &= ~I_DIRTY_PAGES;
395250df6edSDave Chinner 	spin_unlock(&inode->i_lock);
396f758eeabSChristoph Hellwig 	spin_unlock(&wb->list_lock);
3971da177e4SLinus Torvalds 
3981da177e4SLinus Torvalds 	ret = do_writepages(mapping, wbc);
3991da177e4SLinus Torvalds 
40026821ed4SChristoph Hellwig 	/*
40126821ed4SChristoph Hellwig 	 * Make sure to wait on the data before writing out the metadata.
40226821ed4SChristoph Hellwig 	 * This is important for filesystems that modify metadata on data
40326821ed4SChristoph Hellwig 	 * I/O completion.
40426821ed4SChristoph Hellwig 	 */
405a9185b41SChristoph Hellwig 	if (wbc->sync_mode == WB_SYNC_ALL) {
40626821ed4SChristoph Hellwig 		int err = filemap_fdatawait(mapping);
4071da177e4SLinus Torvalds 		if (ret == 0)
4081da177e4SLinus Torvalds 			ret = err;
4091da177e4SLinus Torvalds 	}
4101da177e4SLinus Torvalds 
4115547e8aaSDmitry Monakhov 	/*
4125547e8aaSDmitry Monakhov 	 * Some filesystems may redirty the inode during the writeback
4135547e8aaSDmitry Monakhov 	 * due to delalloc, clear dirty metadata flags right before
4145547e8aaSDmitry Monakhov 	 * write_inode()
4155547e8aaSDmitry Monakhov 	 */
416250df6edSDave Chinner 	spin_lock(&inode->i_lock);
4175547e8aaSDmitry Monakhov 	dirty = inode->i_state & I_DIRTY;
4185547e8aaSDmitry Monakhov 	inode->i_state &= ~(I_DIRTY_SYNC | I_DIRTY_DATASYNC);
419250df6edSDave Chinner 	spin_unlock(&inode->i_lock);
42026821ed4SChristoph Hellwig 	/* Don't write the inode if only I_DIRTY_PAGES was set */
42126821ed4SChristoph Hellwig 	if (dirty & (I_DIRTY_SYNC | I_DIRTY_DATASYNC)) {
422a9185b41SChristoph Hellwig 		int err = write_inode(inode, wbc);
4231da177e4SLinus Torvalds 		if (ret == 0)
4241da177e4SLinus Torvalds 			ret = err;
4251da177e4SLinus Torvalds 	}
4261da177e4SLinus Torvalds 
427f758eeabSChristoph Hellwig 	spin_lock(&wb->list_lock);
428250df6edSDave Chinner 	spin_lock(&inode->i_lock);
4291c0eeaf5SJoern Engel 	inode->i_state &= ~I_SYNC;
430a4ffdde6SAl Viro 	if (!(inode->i_state & I_FREEING)) {
43194c3dcbbSWu Fengguang 		/*
43294c3dcbbSWu Fengguang 		 * Sync livelock prevention. Each inode is tagged and synced in
43394c3dcbbSWu Fengguang 		 * one shot. If still dirty, it will be redirty_tail()'ed below.
43494c3dcbbSWu Fengguang 		 * Update the dirty time to prevent enqueue and sync it again.
43594c3dcbbSWu Fengguang 		 */
43694c3dcbbSWu Fengguang 		if ((inode->i_state & I_DIRTY) &&
43794c3dcbbSWu Fengguang 		    (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages))
43894c3dcbbSWu Fengguang 			inode->dirtied_when = jiffies;
43994c3dcbbSWu Fengguang 
44023539afcSWu Fengguang 		if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) {
4411da177e4SLinus Torvalds 			/*
4421da177e4SLinus Torvalds 			 * We didn't write back all the pages.  nfs_writepages()
443a50aeb40SWu Fengguang 			 * sometimes bales out without doing anything.
4441da177e4SLinus Torvalds 			 */
4451da177e4SLinus Torvalds 			inode->i_state |= I_DIRTY_PAGES;
4468bc3be27SFengguang Wu 			if (wbc->nr_to_write <= 0) {
4478bc3be27SFengguang Wu 				/*
4488bc3be27SFengguang Wu 				 * slice used up: queue for next turn
4498bc3be27SFengguang Wu 				 */
450f758eeabSChristoph Hellwig 				requeue_io(inode, wb);
4511da177e4SLinus Torvalds 			} else {
4521da177e4SLinus Torvalds 				/*
453a50aeb40SWu Fengguang 				 * Writeback blocked by something other than
454a50aeb40SWu Fengguang 				 * congestion. Delay the inode for some time to
455a50aeb40SWu Fengguang 				 * avoid spinning on the CPU (100% iowait)
456a50aeb40SWu Fengguang 				 * retrying writeback of the dirty page/inode
457a50aeb40SWu Fengguang 				 * that cannot be performed immediately.
4588bc3be27SFengguang Wu 				 */
459f758eeabSChristoph Hellwig 				redirty_tail(inode, wb);
4608bc3be27SFengguang Wu 			}
46123539afcSWu Fengguang 		} else if (inode->i_state & I_DIRTY) {
46223539afcSWu Fengguang 			/*
46323539afcSWu Fengguang 			 * Filesystems can dirty the inode during writeback
46423539afcSWu Fengguang 			 * operations, such as delayed allocation during
46523539afcSWu Fengguang 			 * submission or metadata updates after data IO
46623539afcSWu Fengguang 			 * completion.
46723539afcSWu Fengguang 			 */
468f758eeabSChristoph Hellwig 			redirty_tail(inode, wb);
4691da177e4SLinus Torvalds 		} else {
4701da177e4SLinus Torvalds 			/*
4719e38d86fSNick Piggin 			 * The inode is clean.  At this point we either have
4729e38d86fSNick Piggin 			 * a reference to the inode or it's on it's way out.
4739e38d86fSNick Piggin 			 * No need to add it back to the LRU.
4741da177e4SLinus Torvalds 			 */
4757ccf19a8SNick Piggin 			list_del_init(&inode->i_wb_list);
4761da177e4SLinus Torvalds 		}
4771da177e4SLinus Torvalds 	}
4781c0eeaf5SJoern Engel 	inode_sync_complete(inode);
479251d6a47SWu Fengguang 	trace_writeback_single_inode(inode, wbc, nr_to_write);
4801da177e4SLinus Torvalds 	return ret;
4811da177e4SLinus Torvalds }
4821da177e4SLinus Torvalds 
4831a12d8bdSWu Fengguang static long writeback_chunk_size(struct backing_dev_info *bdi,
4841a12d8bdSWu Fengguang 				 struct wb_writeback_work *work)
485d46db3d5SWu Fengguang {
486d46db3d5SWu Fengguang 	long pages;
487d46db3d5SWu Fengguang 
488d46db3d5SWu Fengguang 	/*
489d46db3d5SWu Fengguang 	 * WB_SYNC_ALL mode does livelock avoidance by syncing dirty
490d46db3d5SWu Fengguang 	 * inodes/pages in one big loop. Setting wbc.nr_to_write=LONG_MAX
491d46db3d5SWu Fengguang 	 * here avoids calling into writeback_inodes_wb() more than once.
492d46db3d5SWu Fengguang 	 *
493d46db3d5SWu Fengguang 	 * The intended call sequence for WB_SYNC_ALL writeback is:
494d46db3d5SWu Fengguang 	 *
495d46db3d5SWu Fengguang 	 *      wb_writeback()
496d46db3d5SWu Fengguang 	 *          writeback_sb_inodes()       <== called only once
497d46db3d5SWu Fengguang 	 *              write_cache_pages()     <== called once for each inode
498d46db3d5SWu Fengguang 	 *                   (quickly) tag currently dirty pages
499d46db3d5SWu Fengguang 	 *                   (maybe slowly) sync all tagged pages
500d46db3d5SWu Fengguang 	 */
501d46db3d5SWu Fengguang 	if (work->sync_mode == WB_SYNC_ALL || work->tagged_writepages)
502d46db3d5SWu Fengguang 		pages = LONG_MAX;
5031a12d8bdSWu Fengguang 	else {
5041a12d8bdSWu Fengguang 		pages = min(bdi->avg_write_bandwidth / 2,
5051a12d8bdSWu Fengguang 			    global_dirty_limit / DIRTY_SCOPE);
5061a12d8bdSWu Fengguang 		pages = min(pages, work->nr_pages);
5071a12d8bdSWu Fengguang 		pages = round_down(pages + MIN_WRITEBACK_PAGES,
5081a12d8bdSWu Fengguang 				   MIN_WRITEBACK_PAGES);
5091a12d8bdSWu Fengguang 	}
510d46db3d5SWu Fengguang 
511d46db3d5SWu Fengguang 	return pages;
512d46db3d5SWu Fengguang }
513d46db3d5SWu Fengguang 
51403ba3782SJens Axboe /*
515f11c9c5cSEdward Shishkin  * Write a portion of b_io inodes which belong to @sb.
516edadfb10SChristoph Hellwig  *
517edadfb10SChristoph Hellwig  * If @only_this_sb is true, then find and write all such
518f11c9c5cSEdward Shishkin  * inodes. Otherwise write only ones which go sequentially
519f11c9c5cSEdward Shishkin  * in reverse order.
520edadfb10SChristoph Hellwig  *
521d46db3d5SWu Fengguang  * Return the number of pages and/or inodes written.
522f11c9c5cSEdward Shishkin  */
523d46db3d5SWu Fengguang static long writeback_sb_inodes(struct super_block *sb,
524d46db3d5SWu Fengguang 				struct bdi_writeback *wb,
525d46db3d5SWu Fengguang 				struct wb_writeback_work *work)
52603ba3782SJens Axboe {
527d46db3d5SWu Fengguang 	struct writeback_control wbc = {
528d46db3d5SWu Fengguang 		.sync_mode		= work->sync_mode,
529d46db3d5SWu Fengguang 		.tagged_writepages	= work->tagged_writepages,
530d46db3d5SWu Fengguang 		.for_kupdate		= work->for_kupdate,
531d46db3d5SWu Fengguang 		.for_background		= work->for_background,
532d46db3d5SWu Fengguang 		.range_cyclic		= work->range_cyclic,
533d46db3d5SWu Fengguang 		.range_start		= 0,
534d46db3d5SWu Fengguang 		.range_end		= LLONG_MAX,
535d46db3d5SWu Fengguang 	};
536d46db3d5SWu Fengguang 	unsigned long start_time = jiffies;
537d46db3d5SWu Fengguang 	long write_chunk;
538d46db3d5SWu Fengguang 	long wrote = 0;  /* count both pages and inodes */
539d46db3d5SWu Fengguang 
54003ba3782SJens Axboe 	while (!list_empty(&wb->b_io)) {
5417ccf19a8SNick Piggin 		struct inode *inode = wb_inode(wb->b_io.prev);
542edadfb10SChristoph Hellwig 
543edadfb10SChristoph Hellwig 		if (inode->i_sb != sb) {
544d46db3d5SWu Fengguang 			if (work->sb) {
545edadfb10SChristoph Hellwig 				/*
546edadfb10SChristoph Hellwig 				 * We only want to write back data for this
547edadfb10SChristoph Hellwig 				 * superblock, move all inodes not belonging
548edadfb10SChristoph Hellwig 				 * to it back onto the dirty list.
549edadfb10SChristoph Hellwig 				 */
550f758eeabSChristoph Hellwig 				redirty_tail(inode, wb);
55166f3b8e2SJens Axboe 				continue;
55266f3b8e2SJens Axboe 			}
553edadfb10SChristoph Hellwig 
554edadfb10SChristoph Hellwig 			/*
555edadfb10SChristoph Hellwig 			 * The inode belongs to a different superblock.
556edadfb10SChristoph Hellwig 			 * Bounce back to the caller to unpin this and
557edadfb10SChristoph Hellwig 			 * pin the next superblock.
558edadfb10SChristoph Hellwig 			 */
559d46db3d5SWu Fengguang 			break;
560edadfb10SChristoph Hellwig 		}
561edadfb10SChristoph Hellwig 
5629843b76aSChristoph Hellwig 		/*
5639843b76aSChristoph Hellwig 		 * Don't bother with new inodes or inodes beeing freed, first
5649843b76aSChristoph Hellwig 		 * kind does not need peridic writeout yet, and for the latter
5659843b76aSChristoph Hellwig 		 * kind writeout is handled by the freer.
5669843b76aSChristoph Hellwig 		 */
567250df6edSDave Chinner 		spin_lock(&inode->i_lock);
5689843b76aSChristoph Hellwig 		if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) {
569250df6edSDave Chinner 			spin_unlock(&inode->i_lock);
570fcc5c222SWu Fengguang 			redirty_tail(inode, wb);
5717ef0d737SNick Piggin 			continue;
5727ef0d737SNick Piggin 		}
5731da177e4SLinus Torvalds 		__iget(inode);
5741a12d8bdSWu Fengguang 		write_chunk = writeback_chunk_size(wb->bdi, work);
575d46db3d5SWu Fengguang 		wbc.nr_to_write = write_chunk;
576d46db3d5SWu Fengguang 		wbc.pages_skipped = 0;
577250df6edSDave Chinner 
578d46db3d5SWu Fengguang 		writeback_single_inode(inode, wb, &wbc);
579d46db3d5SWu Fengguang 
580d46db3d5SWu Fengguang 		work->nr_pages -= write_chunk - wbc.nr_to_write;
581d46db3d5SWu Fengguang 		wrote += write_chunk - wbc.nr_to_write;
582d46db3d5SWu Fengguang 		if (!(inode->i_state & I_DIRTY))
583d46db3d5SWu Fengguang 			wrote++;
584d46db3d5SWu Fengguang 		if (wbc.pages_skipped) {
5851da177e4SLinus Torvalds 			/*
5861da177e4SLinus Torvalds 			 * writeback is not making progress due to locked
5871da177e4SLinus Torvalds 			 * buffers.  Skip this inode for now.
5881da177e4SLinus Torvalds 			 */
589f758eeabSChristoph Hellwig 			redirty_tail(inode, wb);
5901da177e4SLinus Torvalds 		}
5910f1b1fd8SDave Chinner 		spin_unlock(&inode->i_lock);
592f758eeabSChristoph Hellwig 		spin_unlock(&wb->list_lock);
5931da177e4SLinus Torvalds 		iput(inode);
5944ffc8444SOGAWA Hirofumi 		cond_resched();
595f758eeabSChristoph Hellwig 		spin_lock(&wb->list_lock);
596d46db3d5SWu Fengguang 		/*
597d46db3d5SWu Fengguang 		 * bail out to wb_writeback() often enough to check
598d46db3d5SWu Fengguang 		 * background threshold and other termination conditions.
599d46db3d5SWu Fengguang 		 */
600d46db3d5SWu Fengguang 		if (wrote) {
601d46db3d5SWu Fengguang 			if (time_is_before_jiffies(start_time + HZ / 10UL))
602d46db3d5SWu Fengguang 				break;
603d46db3d5SWu Fengguang 			if (work->nr_pages <= 0)
604d46db3d5SWu Fengguang 				break;
6051da177e4SLinus Torvalds 		}
6068bc3be27SFengguang Wu 	}
607d46db3d5SWu Fengguang 	return wrote;
608f11c9c5cSEdward Shishkin }
60938f21977SNick Piggin 
610d46db3d5SWu Fengguang static long __writeback_inodes_wb(struct bdi_writeback *wb,
611d46db3d5SWu Fengguang 				  struct wb_writeback_work *work)
612f11c9c5cSEdward Shishkin {
613d46db3d5SWu Fengguang 	unsigned long start_time = jiffies;
614d46db3d5SWu Fengguang 	long wrote = 0;
615f11c9c5cSEdward Shishkin 
616f11c9c5cSEdward Shishkin 	while (!list_empty(&wb->b_io)) {
6177ccf19a8SNick Piggin 		struct inode *inode = wb_inode(wb->b_io.prev);
618f11c9c5cSEdward Shishkin 		struct super_block *sb = inode->i_sb;
619f11c9c5cSEdward Shishkin 
62012ad3ab6SDave Chinner 		if (!grab_super_passive(sb)) {
6210e995816SWu Fengguang 			/*
6220e995816SWu Fengguang 			 * grab_super_passive() may fail consistently due to
6230e995816SWu Fengguang 			 * s_umount being grabbed by someone else. Don't use
6240e995816SWu Fengguang 			 * requeue_io() to avoid busy retrying the inode/sb.
6250e995816SWu Fengguang 			 */
6260e995816SWu Fengguang 			redirty_tail(inode, wb);
627d19de7edSChristoph Hellwig 			continue;
628334132aeSChristoph Hellwig 		}
629d46db3d5SWu Fengguang 		wrote += writeback_sb_inodes(sb, wb, work);
630d19de7edSChristoph Hellwig 		drop_super(sb);
631f11c9c5cSEdward Shishkin 
632d46db3d5SWu Fengguang 		/* refer to the same tests at the end of writeback_sb_inodes */
633d46db3d5SWu Fengguang 		if (wrote) {
634d46db3d5SWu Fengguang 			if (time_is_before_jiffies(start_time + HZ / 10UL))
635d46db3d5SWu Fengguang 				break;
636d46db3d5SWu Fengguang 			if (work->nr_pages <= 0)
637f11c9c5cSEdward Shishkin 				break;
638f11c9c5cSEdward Shishkin 		}
639d46db3d5SWu Fengguang 	}
64066f3b8e2SJens Axboe 	/* Leave any unwritten inodes on b_io */
641d46db3d5SWu Fengguang 	return wrote;
64266f3b8e2SJens Axboe }
64366f3b8e2SJens Axboe 
644d46db3d5SWu Fengguang long writeback_inodes_wb(struct bdi_writeback *wb, long nr_pages)
645edadfb10SChristoph Hellwig {
646d46db3d5SWu Fengguang 	struct wb_writeback_work work = {
647d46db3d5SWu Fengguang 		.nr_pages	= nr_pages,
648d46db3d5SWu Fengguang 		.sync_mode	= WB_SYNC_NONE,
649d46db3d5SWu Fengguang 		.range_cyclic	= 1,
650d46db3d5SWu Fengguang 	};
651edadfb10SChristoph Hellwig 
652f758eeabSChristoph Hellwig 	spin_lock(&wb->list_lock);
653424b351fSWu Fengguang 	if (list_empty(&wb->b_io))
654d46db3d5SWu Fengguang 		queue_io(wb, NULL);
655d46db3d5SWu Fengguang 	__writeback_inodes_wb(wb, &work);
656f758eeabSChristoph Hellwig 	spin_unlock(&wb->list_lock);
657edadfb10SChristoph Hellwig 
658d46db3d5SWu Fengguang 	return nr_pages - work.nr_pages;
65966f3b8e2SJens Axboe }
66066f3b8e2SJens Axboe 
66103ba3782SJens Axboe static inline bool over_bground_thresh(void)
66203ba3782SJens Axboe {
66303ba3782SJens Axboe 	unsigned long background_thresh, dirty_thresh;
66403ba3782SJens Axboe 
66516c4042fSWu Fengguang 	global_dirty_limits(&background_thresh, &dirty_thresh);
66603ba3782SJens Axboe 
66703ba3782SJens Axboe 	return (global_page_state(NR_FILE_DIRTY) +
6684cbec4c8SWu Fengguang 		global_page_state(NR_UNSTABLE_NFS) > background_thresh);
66903ba3782SJens Axboe }
67003ba3782SJens Axboe 
67103ba3782SJens Axboe /*
672e98be2d5SWu Fengguang  * Called under wb->list_lock. If there are multiple wb per bdi,
673e98be2d5SWu Fengguang  * only the flusher working on the first wb should do it.
674e98be2d5SWu Fengguang  */
675e98be2d5SWu Fengguang static void wb_update_bandwidth(struct bdi_writeback *wb,
676e98be2d5SWu Fengguang 				unsigned long start_time)
677e98be2d5SWu Fengguang {
678c42843f2SWu Fengguang 	__bdi_update_bandwidth(wb->bdi, 0, 0, 0, 0, start_time);
679e98be2d5SWu Fengguang }
680e98be2d5SWu Fengguang 
681e98be2d5SWu Fengguang /*
68203ba3782SJens Axboe  * Explicit flushing or periodic writeback of "old" data.
68303ba3782SJens Axboe  *
68403ba3782SJens Axboe  * Define "old": the first time one of an inode's pages is dirtied, we mark the
68503ba3782SJens Axboe  * dirtying-time in the inode's address_space.  So this periodic writeback code
68603ba3782SJens Axboe  * just walks the superblock inode list, writing back any inodes which are
68703ba3782SJens Axboe  * older than a specific point in time.
68803ba3782SJens Axboe  *
68903ba3782SJens Axboe  * Try to run once per dirty_writeback_interval.  But if a writeback event
69003ba3782SJens Axboe  * takes longer than a dirty_writeback_interval interval, then leave a
69103ba3782SJens Axboe  * one-second gap.
69203ba3782SJens Axboe  *
69303ba3782SJens Axboe  * older_than_this takes precedence over nr_to_write.  So we'll only write back
69403ba3782SJens Axboe  * all dirty pages if they are all attached to "old" mappings.
69503ba3782SJens Axboe  */
696c4a77a6cSJens Axboe static long wb_writeback(struct bdi_writeback *wb,
69783ba7b07SChristoph Hellwig 			 struct wb_writeback_work *work)
69803ba3782SJens Axboe {
699e98be2d5SWu Fengguang 	unsigned long wb_start = jiffies;
700d46db3d5SWu Fengguang 	long nr_pages = work->nr_pages;
70103ba3782SJens Axboe 	unsigned long oldest_jif;
702a5989bdcSJan Kara 	struct inode *inode;
703d46db3d5SWu Fengguang 	long progress;
70403ba3782SJens Axboe 
705e185dda8SWu Fengguang 	oldest_jif = jiffies;
706d46db3d5SWu Fengguang 	work->older_than_this = &oldest_jif;
70703ba3782SJens Axboe 
708e8dfc305SWu Fengguang 	spin_lock(&wb->list_lock);
70903ba3782SJens Axboe 	for (;;) {
71003ba3782SJens Axboe 		/*
711d3ddec76SWu Fengguang 		 * Stop writeback when nr_pages has been consumed
71203ba3782SJens Axboe 		 */
71383ba7b07SChristoph Hellwig 		if (work->nr_pages <= 0)
71403ba3782SJens Axboe 			break;
71503ba3782SJens Axboe 
71603ba3782SJens Axboe 		/*
717aa373cf5SJan Kara 		 * Background writeout and kupdate-style writeback may
718aa373cf5SJan Kara 		 * run forever. Stop them if there is other work to do
719aa373cf5SJan Kara 		 * so that e.g. sync can proceed. They'll be restarted
720aa373cf5SJan Kara 		 * after the other works are all done.
721aa373cf5SJan Kara 		 */
722aa373cf5SJan Kara 		if ((work->for_background || work->for_kupdate) &&
723aa373cf5SJan Kara 		    !list_empty(&wb->bdi->work_list))
724aa373cf5SJan Kara 			break;
725aa373cf5SJan Kara 
726aa373cf5SJan Kara 		/*
727d3ddec76SWu Fengguang 		 * For background writeout, stop when we are below the
728d3ddec76SWu Fengguang 		 * background dirty threshold
72903ba3782SJens Axboe 		 */
73083ba7b07SChristoph Hellwig 		if (work->for_background && !over_bground_thresh())
73103ba3782SJens Axboe 			break;
73203ba3782SJens Axboe 
733ba9aa839SWu Fengguang 		if (work->for_kupdate) {
734ba9aa839SWu Fengguang 			oldest_jif = jiffies -
735ba9aa839SWu Fengguang 				msecs_to_jiffies(dirty_expire_interval * 10);
736d46db3d5SWu Fengguang 			work->older_than_this = &oldest_jif;
737ba9aa839SWu Fengguang 		}
738028c2dd1SDave Chinner 
739d46db3d5SWu Fengguang 		trace_writeback_start(wb->bdi, work);
740e8dfc305SWu Fengguang 		if (list_empty(&wb->b_io))
741d46db3d5SWu Fengguang 			queue_io(wb, work->older_than_this);
74283ba7b07SChristoph Hellwig 		if (work->sb)
743d46db3d5SWu Fengguang 			progress = writeback_sb_inodes(work->sb, wb, work);
744edadfb10SChristoph Hellwig 		else
745d46db3d5SWu Fengguang 			progress = __writeback_inodes_wb(wb, work);
746d46db3d5SWu Fengguang 		trace_writeback_written(wb->bdi, work);
747028c2dd1SDave Chinner 
748e98be2d5SWu Fengguang 		wb_update_bandwidth(wb, wb_start);
74903ba3782SJens Axboe 
75003ba3782SJens Axboe 		/*
75171fd05a8SJens Axboe 		 * Did we write something? Try for more
752e6fb6da2SWu Fengguang 		 *
753e6fb6da2SWu Fengguang 		 * Dirty inodes are moved to b_io for writeback in batches.
754e6fb6da2SWu Fengguang 		 * The completion of the current batch does not necessarily
755e6fb6da2SWu Fengguang 		 * mean the overall work is done. So we keep looping as long
756e6fb6da2SWu Fengguang 		 * as made some progress on cleaning pages or inodes.
75771fd05a8SJens Axboe 		 */
758d46db3d5SWu Fengguang 		if (progress)
75903ba3782SJens Axboe 			continue;
760a5989bdcSJan Kara 		/*
761e6fb6da2SWu Fengguang 		 * No more inodes for IO, bail
762a5989bdcSJan Kara 		 */
763b7a2441fSWu Fengguang 		if (list_empty(&wb->b_more_io))
76403ba3782SJens Axboe 			break;
76503ba3782SJens Axboe 		/*
7668010c3b6SJens Axboe 		 * Nothing written. Wait for some inode to
7678010c3b6SJens Axboe 		 * become available for writeback. Otherwise
7688010c3b6SJens Axboe 		 * we'll just busyloop.
76903ba3782SJens Axboe 		 */
77003ba3782SJens Axboe 		if (!list_empty(&wb->b_more_io))  {
771d46db3d5SWu Fengguang 			trace_writeback_wait(wb->bdi, work);
77203ba3782SJens Axboe 			inode = wb_inode(wb->b_more_io.prev);
773250df6edSDave Chinner 			spin_lock(&inode->i_lock);
774f758eeabSChristoph Hellwig 			inode_wait_for_writeback(inode, wb);
775250df6edSDave Chinner 			spin_unlock(&inode->i_lock);
77603ba3782SJens Axboe 		}
77703ba3782SJens Axboe 	}
778e8dfc305SWu Fengguang 	spin_unlock(&wb->list_lock);
77903ba3782SJens Axboe 
780d46db3d5SWu Fengguang 	return nr_pages - work->nr_pages;
78103ba3782SJens Axboe }
78203ba3782SJens Axboe 
78303ba3782SJens Axboe /*
78483ba7b07SChristoph Hellwig  * Return the next wb_writeback_work struct that hasn't been processed yet.
78503ba3782SJens Axboe  */
78683ba7b07SChristoph Hellwig static struct wb_writeback_work *
78708852b6dSMinchan Kim get_next_work_item(struct backing_dev_info *bdi)
78803ba3782SJens Axboe {
78983ba7b07SChristoph Hellwig 	struct wb_writeback_work *work = NULL;
79003ba3782SJens Axboe 
7916467716aSArtem Bityutskiy 	spin_lock_bh(&bdi->wb_lock);
79283ba7b07SChristoph Hellwig 	if (!list_empty(&bdi->work_list)) {
79383ba7b07SChristoph Hellwig 		work = list_entry(bdi->work_list.next,
79483ba7b07SChristoph Hellwig 				  struct wb_writeback_work, list);
79583ba7b07SChristoph Hellwig 		list_del_init(&work->list);
79603ba3782SJens Axboe 	}
7976467716aSArtem Bityutskiy 	spin_unlock_bh(&bdi->wb_lock);
79883ba7b07SChristoph Hellwig 	return work;
79903ba3782SJens Axboe }
80003ba3782SJens Axboe 
801cdf01dd5SLinus Torvalds /*
802cdf01dd5SLinus Torvalds  * Add in the number of potentially dirty inodes, because each inode
803cdf01dd5SLinus Torvalds  * write can dirty pagecache in the underlying blockdev.
804cdf01dd5SLinus Torvalds  */
805cdf01dd5SLinus Torvalds static unsigned long get_nr_dirty_pages(void)
806cdf01dd5SLinus Torvalds {
807cdf01dd5SLinus Torvalds 	return global_page_state(NR_FILE_DIRTY) +
808cdf01dd5SLinus Torvalds 		global_page_state(NR_UNSTABLE_NFS) +
809cdf01dd5SLinus Torvalds 		get_nr_dirty_inodes();
810cdf01dd5SLinus Torvalds }
811cdf01dd5SLinus Torvalds 
8126585027aSJan Kara static long wb_check_background_flush(struct bdi_writeback *wb)
8136585027aSJan Kara {
8146585027aSJan Kara 	if (over_bground_thresh()) {
8156585027aSJan Kara 
8166585027aSJan Kara 		struct wb_writeback_work work = {
8176585027aSJan Kara 			.nr_pages	= LONG_MAX,
8186585027aSJan Kara 			.sync_mode	= WB_SYNC_NONE,
8196585027aSJan Kara 			.for_background	= 1,
8206585027aSJan Kara 			.range_cyclic	= 1,
8216585027aSJan Kara 		};
8226585027aSJan Kara 
8236585027aSJan Kara 		return wb_writeback(wb, &work);
8246585027aSJan Kara 	}
8256585027aSJan Kara 
8266585027aSJan Kara 	return 0;
8276585027aSJan Kara }
8286585027aSJan Kara 
82903ba3782SJens Axboe static long wb_check_old_data_flush(struct bdi_writeback *wb)
83003ba3782SJens Axboe {
83103ba3782SJens Axboe 	unsigned long expired;
83203ba3782SJens Axboe 	long nr_pages;
83303ba3782SJens Axboe 
83469b62d01SJens Axboe 	/*
83569b62d01SJens Axboe 	 * When set to zero, disable periodic writeback
83669b62d01SJens Axboe 	 */
83769b62d01SJens Axboe 	if (!dirty_writeback_interval)
83869b62d01SJens Axboe 		return 0;
83969b62d01SJens Axboe 
84003ba3782SJens Axboe 	expired = wb->last_old_flush +
84103ba3782SJens Axboe 			msecs_to_jiffies(dirty_writeback_interval * 10);
84203ba3782SJens Axboe 	if (time_before(jiffies, expired))
84303ba3782SJens Axboe 		return 0;
84403ba3782SJens Axboe 
84503ba3782SJens Axboe 	wb->last_old_flush = jiffies;
846cdf01dd5SLinus Torvalds 	nr_pages = get_nr_dirty_pages();
84703ba3782SJens Axboe 
848c4a77a6cSJens Axboe 	if (nr_pages) {
84983ba7b07SChristoph Hellwig 		struct wb_writeback_work work = {
850c4a77a6cSJens Axboe 			.nr_pages	= nr_pages,
851c4a77a6cSJens Axboe 			.sync_mode	= WB_SYNC_NONE,
852c4a77a6cSJens Axboe 			.for_kupdate	= 1,
853c4a77a6cSJens Axboe 			.range_cyclic	= 1,
854c4a77a6cSJens Axboe 		};
855c4a77a6cSJens Axboe 
85683ba7b07SChristoph Hellwig 		return wb_writeback(wb, &work);
857c4a77a6cSJens Axboe 	}
85803ba3782SJens Axboe 
85903ba3782SJens Axboe 	return 0;
86003ba3782SJens Axboe }
86103ba3782SJens Axboe 
86203ba3782SJens Axboe /*
86303ba3782SJens Axboe  * Retrieve work items and do the writeback they describe
86403ba3782SJens Axboe  */
86503ba3782SJens Axboe long wb_do_writeback(struct bdi_writeback *wb, int force_wait)
86603ba3782SJens Axboe {
86703ba3782SJens Axboe 	struct backing_dev_info *bdi = wb->bdi;
86883ba7b07SChristoph Hellwig 	struct wb_writeback_work *work;
869c4a77a6cSJens Axboe 	long wrote = 0;
87003ba3782SJens Axboe 
87181d73a32SJan Kara 	set_bit(BDI_writeback_running, &wb->bdi->state);
87208852b6dSMinchan Kim 	while ((work = get_next_work_item(bdi)) != NULL) {
87303ba3782SJens Axboe 		/*
87403ba3782SJens Axboe 		 * Override sync mode, in case we must wait for completion
87583ba7b07SChristoph Hellwig 		 * because this thread is exiting now.
87603ba3782SJens Axboe 		 */
87703ba3782SJens Axboe 		if (force_wait)
87883ba7b07SChristoph Hellwig 			work->sync_mode = WB_SYNC_ALL;
87983ba7b07SChristoph Hellwig 
880455b2864SDave Chinner 		trace_writeback_exec(bdi, work);
881455b2864SDave Chinner 
88283ba7b07SChristoph Hellwig 		wrote += wb_writeback(wb, work);
88303ba3782SJens Axboe 
88403ba3782SJens Axboe 		/*
88583ba7b07SChristoph Hellwig 		 * Notify the caller of completion if this is a synchronous
88683ba7b07SChristoph Hellwig 		 * work item, otherwise just free it.
88703ba3782SJens Axboe 		 */
88883ba7b07SChristoph Hellwig 		if (work->done)
88983ba7b07SChristoph Hellwig 			complete(work->done);
89083ba7b07SChristoph Hellwig 		else
89183ba7b07SChristoph Hellwig 			kfree(work);
89203ba3782SJens Axboe 	}
89303ba3782SJens Axboe 
89403ba3782SJens Axboe 	/*
89503ba3782SJens Axboe 	 * Check for periodic writeback, kupdated() style
89603ba3782SJens Axboe 	 */
89703ba3782SJens Axboe 	wrote += wb_check_old_data_flush(wb);
8986585027aSJan Kara 	wrote += wb_check_background_flush(wb);
89981d73a32SJan Kara 	clear_bit(BDI_writeback_running, &wb->bdi->state);
90003ba3782SJens Axboe 
90103ba3782SJens Axboe 	return wrote;
90203ba3782SJens Axboe }
90303ba3782SJens Axboe 
90403ba3782SJens Axboe /*
90503ba3782SJens Axboe  * Handle writeback of dirty data for the device backed by this bdi. Also
90603ba3782SJens Axboe  * wakes up periodically and does kupdated style flushing.
90703ba3782SJens Axboe  */
90808243900SChristoph Hellwig int bdi_writeback_thread(void *data)
90903ba3782SJens Axboe {
91008243900SChristoph Hellwig 	struct bdi_writeback *wb = data;
91108243900SChristoph Hellwig 	struct backing_dev_info *bdi = wb->bdi;
91203ba3782SJens Axboe 	long pages_written;
91303ba3782SJens Axboe 
914766f9164SPeter Zijlstra 	current->flags |= PF_SWAPWRITE;
91508243900SChristoph Hellwig 	set_freezable();
916ecd58403SArtem Bityutskiy 	wb->last_active = jiffies;
91703ba3782SJens Axboe 
91803ba3782SJens Axboe 	/*
91908243900SChristoph Hellwig 	 * Our parent may run at a different priority, just set us to normal
92003ba3782SJens Axboe 	 */
92108243900SChristoph Hellwig 	set_user_nice(current, 0);
92208243900SChristoph Hellwig 
923455b2864SDave Chinner 	trace_writeback_thread_start(bdi);
924455b2864SDave Chinner 
92503ba3782SJens Axboe 	while (!kthread_should_stop()) {
9266467716aSArtem Bityutskiy 		/*
9276467716aSArtem Bityutskiy 		 * Remove own delayed wake-up timer, since we are already awake
9286467716aSArtem Bityutskiy 		 * and we'll take care of the preriodic write-back.
9296467716aSArtem Bityutskiy 		 */
9306467716aSArtem Bityutskiy 		del_timer(&wb->wakeup_timer);
9316467716aSArtem Bityutskiy 
93203ba3782SJens Axboe 		pages_written = wb_do_writeback(wb, 0);
93303ba3782SJens Axboe 
934455b2864SDave Chinner 		trace_writeback_pages_written(pages_written);
935455b2864SDave Chinner 
93603ba3782SJens Axboe 		if (pages_written)
937ecd58403SArtem Bityutskiy 			wb->last_active = jiffies;
93803ba3782SJens Axboe 
939297252c8SArtem Bityutskiy 		set_current_state(TASK_INTERRUPTIBLE);
940b76b4014SJ. Bruce Fields 		if (!list_empty(&bdi->work_list) || kthread_should_stop()) {
941297252c8SArtem Bityutskiy 			__set_current_state(TASK_RUNNING);
942297252c8SArtem Bityutskiy 			continue;
94303ba3782SJens Axboe 		}
94403ba3782SJens Axboe 
945253c34e9SArtem Bityutskiy 		if (wb_has_dirty_io(wb) && dirty_writeback_interval)
946fff5b85aSArtem Bityutskiy 			schedule_timeout(msecs_to_jiffies(dirty_writeback_interval * 10));
947253c34e9SArtem Bityutskiy 		else {
948253c34e9SArtem Bityutskiy 			/*
949253c34e9SArtem Bityutskiy 			 * We have nothing to do, so can go sleep without any
950253c34e9SArtem Bityutskiy 			 * timeout and save power. When a work is queued or
951253c34e9SArtem Bityutskiy 			 * something is made dirty - we will be woken up.
952253c34e9SArtem Bityutskiy 			 */
95369b62d01SJens Axboe 			schedule();
954f9eadbbdSJens Axboe 		}
95569b62d01SJens Axboe 
95603ba3782SJens Axboe 		try_to_freeze();
95703ba3782SJens Axboe 	}
95803ba3782SJens Axboe 
959fff5b85aSArtem Bityutskiy 	/* Flush any work that raced with us exiting */
96008243900SChristoph Hellwig 	if (!list_empty(&bdi->work_list))
96108243900SChristoph Hellwig 		wb_do_writeback(wb, 1);
962455b2864SDave Chinner 
963455b2864SDave Chinner 	trace_writeback_thread_stop(bdi);
96403ba3782SJens Axboe 	return 0;
96503ba3782SJens Axboe }
96603ba3782SJens Axboe 
96708243900SChristoph Hellwig 
96803ba3782SJens Axboe /*
96903ba3782SJens Axboe  * Start writeback of `nr_pages' pages.  If `nr_pages' is zero, write back
97003ba3782SJens Axboe  * the whole world.
97103ba3782SJens Axboe  */
97203ba3782SJens Axboe void wakeup_flusher_threads(long nr_pages)
97303ba3782SJens Axboe {
974b8c2f347SChristoph Hellwig 	struct backing_dev_info *bdi;
975b8c2f347SChristoph Hellwig 
97683ba7b07SChristoph Hellwig 	if (!nr_pages) {
97783ba7b07SChristoph Hellwig 		nr_pages = global_page_state(NR_FILE_DIRTY) +
97803ba3782SJens Axboe 				global_page_state(NR_UNSTABLE_NFS);
979b8c2f347SChristoph Hellwig 	}
980b8c2f347SChristoph Hellwig 
981b8c2f347SChristoph Hellwig 	rcu_read_lock();
982b8c2f347SChristoph Hellwig 	list_for_each_entry_rcu(bdi, &bdi_list, bdi_list) {
983b8c2f347SChristoph Hellwig 		if (!bdi_has_dirty_io(bdi))
984b8c2f347SChristoph Hellwig 			continue;
9856585027aSJan Kara 		__bdi_start_writeback(bdi, nr_pages, false);
986b8c2f347SChristoph Hellwig 	}
987b8c2f347SChristoph Hellwig 	rcu_read_unlock();
98803ba3782SJens Axboe }
98903ba3782SJens Axboe 
99003ba3782SJens Axboe static noinline void block_dump___mark_inode_dirty(struct inode *inode)
99103ba3782SJens Axboe {
99203ba3782SJens Axboe 	if (inode->i_ino || strcmp(inode->i_sb->s_id, "bdev")) {
99303ba3782SJens Axboe 		struct dentry *dentry;
99403ba3782SJens Axboe 		const char *name = "?";
99503ba3782SJens Axboe 
99603ba3782SJens Axboe 		dentry = d_find_alias(inode);
99703ba3782SJens Axboe 		if (dentry) {
99803ba3782SJens Axboe 			spin_lock(&dentry->d_lock);
99903ba3782SJens Axboe 			name = (const char *) dentry->d_name.name;
100003ba3782SJens Axboe 		}
100103ba3782SJens Axboe 		printk(KERN_DEBUG
100203ba3782SJens Axboe 		       "%s(%d): dirtied inode %lu (%s) on %s\n",
100303ba3782SJens Axboe 		       current->comm, task_pid_nr(current), inode->i_ino,
100403ba3782SJens Axboe 		       name, inode->i_sb->s_id);
100503ba3782SJens Axboe 		if (dentry) {
100603ba3782SJens Axboe 			spin_unlock(&dentry->d_lock);
100703ba3782SJens Axboe 			dput(dentry);
100803ba3782SJens Axboe 		}
100903ba3782SJens Axboe 	}
101003ba3782SJens Axboe }
101103ba3782SJens Axboe 
101203ba3782SJens Axboe /**
101303ba3782SJens Axboe  *	__mark_inode_dirty -	internal function
101403ba3782SJens Axboe  *	@inode: inode to mark
101503ba3782SJens Axboe  *	@flags: what kind of dirty (i.e. I_DIRTY_SYNC)
101603ba3782SJens Axboe  *	Mark an inode as dirty. Callers should use mark_inode_dirty or
101703ba3782SJens Axboe  *  	mark_inode_dirty_sync.
101803ba3782SJens Axboe  *
101903ba3782SJens Axboe  * Put the inode on the super block's dirty list.
102003ba3782SJens Axboe  *
102103ba3782SJens Axboe  * CAREFUL! We mark it dirty unconditionally, but move it onto the
102203ba3782SJens Axboe  * dirty list only if it is hashed or if it refers to a blockdev.
102303ba3782SJens Axboe  * If it was not hashed, it will never be added to the dirty list
102403ba3782SJens Axboe  * even if it is later hashed, as it will have been marked dirty already.
102503ba3782SJens Axboe  *
102603ba3782SJens Axboe  * In short, make sure you hash any inodes _before_ you start marking
102703ba3782SJens Axboe  * them dirty.
102803ba3782SJens Axboe  *
102903ba3782SJens Axboe  * Note that for blockdevs, inode->dirtied_when represents the dirtying time of
103003ba3782SJens Axboe  * the block-special inode (/dev/hda1) itself.  And the ->dirtied_when field of
103103ba3782SJens Axboe  * the kernel-internal blockdev inode represents the dirtying time of the
103203ba3782SJens Axboe  * blockdev's pages.  This is why for I_DIRTY_PAGES we always use
103303ba3782SJens Axboe  * page->mapping->host, so the page-dirtying time is recorded in the internal
103403ba3782SJens Axboe  * blockdev inode.
103503ba3782SJens Axboe  */
103603ba3782SJens Axboe void __mark_inode_dirty(struct inode *inode, int flags)
103703ba3782SJens Axboe {
103803ba3782SJens Axboe 	struct super_block *sb = inode->i_sb;
1039253c34e9SArtem Bityutskiy 	struct backing_dev_info *bdi = NULL;
104003ba3782SJens Axboe 
104103ba3782SJens Axboe 	/*
104203ba3782SJens Axboe 	 * Don't do this for I_DIRTY_PAGES - that doesn't actually
104303ba3782SJens Axboe 	 * dirty the inode itself
104403ba3782SJens Axboe 	 */
104503ba3782SJens Axboe 	if (flags & (I_DIRTY_SYNC | I_DIRTY_DATASYNC)) {
104603ba3782SJens Axboe 		if (sb->s_op->dirty_inode)
1047aa385729SChristoph Hellwig 			sb->s_op->dirty_inode(inode, flags);
104803ba3782SJens Axboe 	}
104903ba3782SJens Axboe 
105003ba3782SJens Axboe 	/*
105103ba3782SJens Axboe 	 * make sure that changes are seen by all cpus before we test i_state
105203ba3782SJens Axboe 	 * -- mikulas
105303ba3782SJens Axboe 	 */
105403ba3782SJens Axboe 	smp_mb();
105503ba3782SJens Axboe 
105603ba3782SJens Axboe 	/* avoid the locking if we can */
105703ba3782SJens Axboe 	if ((inode->i_state & flags) == flags)
105803ba3782SJens Axboe 		return;
105903ba3782SJens Axboe 
106003ba3782SJens Axboe 	if (unlikely(block_dump))
106103ba3782SJens Axboe 		block_dump___mark_inode_dirty(inode);
106203ba3782SJens Axboe 
1063250df6edSDave Chinner 	spin_lock(&inode->i_lock);
106403ba3782SJens Axboe 	if ((inode->i_state & flags) != flags) {
106503ba3782SJens Axboe 		const int was_dirty = inode->i_state & I_DIRTY;
106603ba3782SJens Axboe 
106703ba3782SJens Axboe 		inode->i_state |= flags;
106803ba3782SJens Axboe 
106903ba3782SJens Axboe 		/*
107003ba3782SJens Axboe 		 * If the inode is being synced, just update its dirty state.
107103ba3782SJens Axboe 		 * The unlocker will place the inode on the appropriate
107203ba3782SJens Axboe 		 * superblock list, based upon its state.
107303ba3782SJens Axboe 		 */
107403ba3782SJens Axboe 		if (inode->i_state & I_SYNC)
1075250df6edSDave Chinner 			goto out_unlock_inode;
107603ba3782SJens Axboe 
107703ba3782SJens Axboe 		/*
107803ba3782SJens Axboe 		 * Only add valid (hashed) inodes to the superblock's
107903ba3782SJens Axboe 		 * dirty list.  Add blockdev inodes as well.
108003ba3782SJens Axboe 		 */
108103ba3782SJens Axboe 		if (!S_ISBLK(inode->i_mode)) {
10821d3382cbSAl Viro 			if (inode_unhashed(inode))
1083250df6edSDave Chinner 				goto out_unlock_inode;
108403ba3782SJens Axboe 		}
1085a4ffdde6SAl Viro 		if (inode->i_state & I_FREEING)
1086250df6edSDave Chinner 			goto out_unlock_inode;
108703ba3782SJens Axboe 
108803ba3782SJens Axboe 		/*
108903ba3782SJens Axboe 		 * If the inode was already on b_dirty/b_io/b_more_io, don't
109003ba3782SJens Axboe 		 * reposition it (that would break b_dirty time-ordering).
109103ba3782SJens Axboe 		 */
109203ba3782SJens Axboe 		if (!was_dirty) {
1093a66979abSDave Chinner 			bool wakeup_bdi = false;
1094253c34e9SArtem Bityutskiy 			bdi = inode_to_bdi(inode);
1095500b067cSJens Axboe 
1096253c34e9SArtem Bityutskiy 			if (bdi_cap_writeback_dirty(bdi)) {
1097253c34e9SArtem Bityutskiy 				WARN(!test_bit(BDI_registered, &bdi->state),
1098253c34e9SArtem Bityutskiy 				     "bdi-%s not registered\n", bdi->name);
1099253c34e9SArtem Bityutskiy 
1100253c34e9SArtem Bityutskiy 				/*
1101253c34e9SArtem Bityutskiy 				 * If this is the first dirty inode for this
1102253c34e9SArtem Bityutskiy 				 * bdi, we have to wake-up the corresponding
1103253c34e9SArtem Bityutskiy 				 * bdi thread to make sure background
1104253c34e9SArtem Bityutskiy 				 * write-back happens later.
1105253c34e9SArtem Bityutskiy 				 */
1106253c34e9SArtem Bityutskiy 				if (!wb_has_dirty_io(&bdi->wb))
1107253c34e9SArtem Bityutskiy 					wakeup_bdi = true;
1108500b067cSJens Axboe 			}
110903ba3782SJens Axboe 
1110a66979abSDave Chinner 			spin_unlock(&inode->i_lock);
1111f758eeabSChristoph Hellwig 			spin_lock(&bdi->wb.list_lock);
111203ba3782SJens Axboe 			inode->dirtied_when = jiffies;
11137ccf19a8SNick Piggin 			list_move(&inode->i_wb_list, &bdi->wb.b_dirty);
1114f758eeabSChristoph Hellwig 			spin_unlock(&bdi->wb.list_lock);
1115253c34e9SArtem Bityutskiy 
1116253c34e9SArtem Bityutskiy 			if (wakeup_bdi)
11176467716aSArtem Bityutskiy 				bdi_wakeup_thread_delayed(bdi);
1118a66979abSDave Chinner 			return;
1119a66979abSDave Chinner 		}
1120a66979abSDave Chinner 	}
1121a66979abSDave Chinner out_unlock_inode:
1122a66979abSDave Chinner 	spin_unlock(&inode->i_lock);
1123a66979abSDave Chinner 
112403ba3782SJens Axboe }
112503ba3782SJens Axboe EXPORT_SYMBOL(__mark_inode_dirty);
112603ba3782SJens Axboe 
112766f3b8e2SJens Axboe /*
112866f3b8e2SJens Axboe  * Write out a superblock's list of dirty inodes.  A wait will be performed
112966f3b8e2SJens Axboe  * upon no inodes, all inodes or the final one, depending upon sync_mode.
113066f3b8e2SJens Axboe  *
113166f3b8e2SJens Axboe  * If older_than_this is non-NULL, then only write out inodes which
113266f3b8e2SJens Axboe  * had their first dirtying at a time earlier than *older_than_this.
113366f3b8e2SJens Axboe  *
113466f3b8e2SJens Axboe  * If `bdi' is non-zero then we're being asked to writeback a specific queue.
113566f3b8e2SJens Axboe  * This function assumes that the blockdev superblock's inodes are backed by
113666f3b8e2SJens Axboe  * a variety of queues, so all inodes are searched.  For other superblocks,
113766f3b8e2SJens Axboe  * assume that all inodes are backed by the same queue.
113866f3b8e2SJens Axboe  *
113966f3b8e2SJens Axboe  * The inodes to be written are parked on bdi->b_io.  They are moved back onto
114066f3b8e2SJens Axboe  * bdi->b_dirty as they are selected for writing.  This way, none can be missed
114166f3b8e2SJens Axboe  * on the writer throttling path, and we get decent balancing between many
114266f3b8e2SJens Axboe  * throttled threads: we don't want them all piling up on inode_sync_wait.
114366f3b8e2SJens Axboe  */
1144b6e51316SJens Axboe static void wait_sb_inodes(struct super_block *sb)
114566f3b8e2SJens Axboe {
114638f21977SNick Piggin 	struct inode *inode, *old_inode = NULL;
114738f21977SNick Piggin 
114803ba3782SJens Axboe 	/*
114903ba3782SJens Axboe 	 * We need to be protected against the filesystem going from
115003ba3782SJens Axboe 	 * r/o to r/w or vice versa.
115103ba3782SJens Axboe 	 */
1152b6e51316SJens Axboe 	WARN_ON(!rwsem_is_locked(&sb->s_umount));
115303ba3782SJens Axboe 
115455fa6091SDave Chinner 	spin_lock(&inode_sb_list_lock);
115566f3b8e2SJens Axboe 
115638f21977SNick Piggin 	/*
115738f21977SNick Piggin 	 * Data integrity sync. Must wait for all pages under writeback,
115838f21977SNick Piggin 	 * because there may have been pages dirtied before our sync
115938f21977SNick Piggin 	 * call, but which had writeout started before we write it out.
116038f21977SNick Piggin 	 * In which case, the inode may not be on the dirty list, but
116138f21977SNick Piggin 	 * we still have to wait for that writeout.
116238f21977SNick Piggin 	 */
1163b6e51316SJens Axboe 	list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
1164250df6edSDave Chinner 		struct address_space *mapping = inode->i_mapping;
116538f21977SNick Piggin 
1166250df6edSDave Chinner 		spin_lock(&inode->i_lock);
1167250df6edSDave Chinner 		if ((inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW)) ||
1168250df6edSDave Chinner 		    (mapping->nrpages == 0)) {
1169250df6edSDave Chinner 			spin_unlock(&inode->i_lock);
117038f21977SNick Piggin 			continue;
1171250df6edSDave Chinner 		}
117238f21977SNick Piggin 		__iget(inode);
1173250df6edSDave Chinner 		spin_unlock(&inode->i_lock);
117455fa6091SDave Chinner 		spin_unlock(&inode_sb_list_lock);
117555fa6091SDave Chinner 
117638f21977SNick Piggin 		/*
117755fa6091SDave Chinner 		 * We hold a reference to 'inode' so it couldn't have been
117855fa6091SDave Chinner 		 * removed from s_inodes list while we dropped the
117955fa6091SDave Chinner 		 * inode_sb_list_lock.  We cannot iput the inode now as we can
118055fa6091SDave Chinner 		 * be holding the last reference and we cannot iput it under
118155fa6091SDave Chinner 		 * inode_sb_list_lock. So we keep the reference and iput it
118255fa6091SDave Chinner 		 * later.
118338f21977SNick Piggin 		 */
118438f21977SNick Piggin 		iput(old_inode);
118538f21977SNick Piggin 		old_inode = inode;
118638f21977SNick Piggin 
118738f21977SNick Piggin 		filemap_fdatawait(mapping);
118838f21977SNick Piggin 
118938f21977SNick Piggin 		cond_resched();
119038f21977SNick Piggin 
119155fa6091SDave Chinner 		spin_lock(&inode_sb_list_lock);
119238f21977SNick Piggin 	}
119355fa6091SDave Chinner 	spin_unlock(&inode_sb_list_lock);
119438f21977SNick Piggin 	iput(old_inode);
119566f3b8e2SJens Axboe }
11961da177e4SLinus Torvalds 
1197d8a8559cSJens Axboe /**
11983259f8beSChris Mason  * writeback_inodes_sb_nr -	writeback dirty inodes from given super_block
1199d8a8559cSJens Axboe  * @sb: the superblock
12003259f8beSChris Mason  * @nr: the number of pages to write
12011da177e4SLinus Torvalds  *
1202d8a8559cSJens Axboe  * Start writeback on some inodes on this super_block. No guarantees are made
1203d8a8559cSJens Axboe  * on how many (if any) will be written, and this function does not wait
12043259f8beSChris Mason  * for IO completion of submitted IO.
12051da177e4SLinus Torvalds  */
12063259f8beSChris Mason void writeback_inodes_sb_nr(struct super_block *sb, unsigned long nr)
12071da177e4SLinus Torvalds {
120883ba7b07SChristoph Hellwig 	DECLARE_COMPLETION_ONSTACK(done);
120983ba7b07SChristoph Hellwig 	struct wb_writeback_work work = {
12103c4d7165SChristoph Hellwig 		.sb			= sb,
12113c4d7165SChristoph Hellwig 		.sync_mode		= WB_SYNC_NONE,
12126e6938b6SWu Fengguang 		.tagged_writepages	= 1,
121383ba7b07SChristoph Hellwig 		.done			= &done,
12143259f8beSChris Mason 		.nr_pages		= nr,
12153c4d7165SChristoph Hellwig 	};
12160e3c9a22SJens Axboe 
1217cf37e972SChristoph Hellwig 	WARN_ON(!rwsem_is_locked(&sb->s_umount));
121883ba7b07SChristoph Hellwig 	bdi_queue_work(sb->s_bdi, &work);
121983ba7b07SChristoph Hellwig 	wait_for_completion(&done);
12201da177e4SLinus Torvalds }
12213259f8beSChris Mason EXPORT_SYMBOL(writeback_inodes_sb_nr);
12223259f8beSChris Mason 
12233259f8beSChris Mason /**
12243259f8beSChris Mason  * writeback_inodes_sb	-	writeback dirty inodes from given super_block
12253259f8beSChris Mason  * @sb: the superblock
12263259f8beSChris Mason  *
12273259f8beSChris Mason  * Start writeback on some inodes on this super_block. No guarantees are made
12283259f8beSChris Mason  * on how many (if any) will be written, and this function does not wait
12293259f8beSChris Mason  * for IO completion of submitted IO.
12303259f8beSChris Mason  */
12313259f8beSChris Mason void writeback_inodes_sb(struct super_block *sb)
12323259f8beSChris Mason {
1233925d169fSLinus Torvalds 	return writeback_inodes_sb_nr(sb, get_nr_dirty_pages());
12343259f8beSChris Mason }
1235d8a8559cSJens Axboe EXPORT_SYMBOL(writeback_inodes_sb);
1236d8a8559cSJens Axboe 
1237d8a8559cSJens Axboe /**
123817bd55d0SEric Sandeen  * writeback_inodes_sb_if_idle	-	start writeback if none underway
123917bd55d0SEric Sandeen  * @sb: the superblock
124017bd55d0SEric Sandeen  *
124117bd55d0SEric Sandeen  * Invoke writeback_inodes_sb if no writeback is currently underway.
124217bd55d0SEric Sandeen  * Returns 1 if writeback was started, 0 if not.
124317bd55d0SEric Sandeen  */
124417bd55d0SEric Sandeen int writeback_inodes_sb_if_idle(struct super_block *sb)
124517bd55d0SEric Sandeen {
124617bd55d0SEric Sandeen 	if (!writeback_in_progress(sb->s_bdi)) {
1247cf37e972SChristoph Hellwig 		down_read(&sb->s_umount);
124817bd55d0SEric Sandeen 		writeback_inodes_sb(sb);
1249cf37e972SChristoph Hellwig 		up_read(&sb->s_umount);
125017bd55d0SEric Sandeen 		return 1;
125117bd55d0SEric Sandeen 	} else
125217bd55d0SEric Sandeen 		return 0;
125317bd55d0SEric Sandeen }
125417bd55d0SEric Sandeen EXPORT_SYMBOL(writeback_inodes_sb_if_idle);
125517bd55d0SEric Sandeen 
125617bd55d0SEric Sandeen /**
12573259f8beSChris Mason  * writeback_inodes_sb_if_idle	-	start writeback if none underway
12583259f8beSChris Mason  * @sb: the superblock
12593259f8beSChris Mason  * @nr: the number of pages to write
12603259f8beSChris Mason  *
12613259f8beSChris Mason  * Invoke writeback_inodes_sb if no writeback is currently underway.
12623259f8beSChris Mason  * Returns 1 if writeback was started, 0 if not.
12633259f8beSChris Mason  */
12643259f8beSChris Mason int writeback_inodes_sb_nr_if_idle(struct super_block *sb,
12653259f8beSChris Mason 				   unsigned long nr)
12663259f8beSChris Mason {
12673259f8beSChris Mason 	if (!writeback_in_progress(sb->s_bdi)) {
12683259f8beSChris Mason 		down_read(&sb->s_umount);
12693259f8beSChris Mason 		writeback_inodes_sb_nr(sb, nr);
12703259f8beSChris Mason 		up_read(&sb->s_umount);
12713259f8beSChris Mason 		return 1;
12723259f8beSChris Mason 	} else
12733259f8beSChris Mason 		return 0;
12743259f8beSChris Mason }
12753259f8beSChris Mason EXPORT_SYMBOL(writeback_inodes_sb_nr_if_idle);
12763259f8beSChris Mason 
12773259f8beSChris Mason /**
1278d8a8559cSJens Axboe  * sync_inodes_sb	-	sync sb inode pages
1279d8a8559cSJens Axboe  * @sb: the superblock
1280d8a8559cSJens Axboe  *
1281d8a8559cSJens Axboe  * This function writes and waits on any dirty inode belonging to this
1282cb9ef8d5SStefan Hajnoczi  * super_block.
1283d8a8559cSJens Axboe  */
1284b6e51316SJens Axboe void sync_inodes_sb(struct super_block *sb)
1285d8a8559cSJens Axboe {
128683ba7b07SChristoph Hellwig 	DECLARE_COMPLETION_ONSTACK(done);
128783ba7b07SChristoph Hellwig 	struct wb_writeback_work work = {
12883c4d7165SChristoph Hellwig 		.sb		= sb,
12893c4d7165SChristoph Hellwig 		.sync_mode	= WB_SYNC_ALL,
12903c4d7165SChristoph Hellwig 		.nr_pages	= LONG_MAX,
12913c4d7165SChristoph Hellwig 		.range_cyclic	= 0,
129283ba7b07SChristoph Hellwig 		.done		= &done,
12933c4d7165SChristoph Hellwig 	};
12943c4d7165SChristoph Hellwig 
1295cf37e972SChristoph Hellwig 	WARN_ON(!rwsem_is_locked(&sb->s_umount));
1296cf37e972SChristoph Hellwig 
129783ba7b07SChristoph Hellwig 	bdi_queue_work(sb->s_bdi, &work);
129883ba7b07SChristoph Hellwig 	wait_for_completion(&done);
129983ba7b07SChristoph Hellwig 
1300b6e51316SJens Axboe 	wait_sb_inodes(sb);
1301d8a8559cSJens Axboe }
1302d8a8559cSJens Axboe EXPORT_SYMBOL(sync_inodes_sb);
13031da177e4SLinus Torvalds 
13041da177e4SLinus Torvalds /**
13051da177e4SLinus Torvalds  * write_inode_now	-	write an inode to disk
13061da177e4SLinus Torvalds  * @inode: inode to write to disk
13071da177e4SLinus Torvalds  * @sync: whether the write should be synchronous or not
13081da177e4SLinus Torvalds  *
13097f04c26dSAndrea Arcangeli  * This function commits an inode to disk immediately if it is dirty. This is
13107f04c26dSAndrea Arcangeli  * primarily needed by knfsd.
13117f04c26dSAndrea Arcangeli  *
13127f04c26dSAndrea Arcangeli  * The caller must either have a ref on the inode or must have set I_WILL_FREE.
13131da177e4SLinus Torvalds  */
13141da177e4SLinus Torvalds int write_inode_now(struct inode *inode, int sync)
13151da177e4SLinus Torvalds {
1316f758eeabSChristoph Hellwig 	struct bdi_writeback *wb = &inode_to_bdi(inode)->wb;
13171da177e4SLinus Torvalds 	int ret;
13181da177e4SLinus Torvalds 	struct writeback_control wbc = {
13191da177e4SLinus Torvalds 		.nr_to_write = LONG_MAX,
132018914b18SMike Galbraith 		.sync_mode = sync ? WB_SYNC_ALL : WB_SYNC_NONE,
1321111ebb6eSOGAWA Hirofumi 		.range_start = 0,
1322111ebb6eSOGAWA Hirofumi 		.range_end = LLONG_MAX,
13231da177e4SLinus Torvalds 	};
13241da177e4SLinus Torvalds 
13251da177e4SLinus Torvalds 	if (!mapping_cap_writeback_dirty(inode->i_mapping))
132649364ce2SAndrew Morton 		wbc.nr_to_write = 0;
13271da177e4SLinus Torvalds 
13281da177e4SLinus Torvalds 	might_sleep();
1329f758eeabSChristoph Hellwig 	spin_lock(&wb->list_lock);
13300f1b1fd8SDave Chinner 	spin_lock(&inode->i_lock);
1331f758eeabSChristoph Hellwig 	ret = writeback_single_inode(inode, wb, &wbc);
13320f1b1fd8SDave Chinner 	spin_unlock(&inode->i_lock);
1333f758eeabSChristoph Hellwig 	spin_unlock(&wb->list_lock);
13341da177e4SLinus Torvalds 	if (sync)
13351c0eeaf5SJoern Engel 		inode_sync_wait(inode);
13361da177e4SLinus Torvalds 	return ret;
13371da177e4SLinus Torvalds }
13381da177e4SLinus Torvalds EXPORT_SYMBOL(write_inode_now);
13391da177e4SLinus Torvalds 
13401da177e4SLinus Torvalds /**
13411da177e4SLinus Torvalds  * sync_inode - write an inode and its pages to disk.
13421da177e4SLinus Torvalds  * @inode: the inode to sync
13431da177e4SLinus Torvalds  * @wbc: controls the writeback mode
13441da177e4SLinus Torvalds  *
13451da177e4SLinus Torvalds  * sync_inode() will write an inode and its pages to disk.  It will also
13461da177e4SLinus Torvalds  * correctly update the inode on its superblock's dirty inode lists and will
13471da177e4SLinus Torvalds  * update inode->i_state.
13481da177e4SLinus Torvalds  *
13491da177e4SLinus Torvalds  * The caller must have a ref on the inode.
13501da177e4SLinus Torvalds  */
13511da177e4SLinus Torvalds int sync_inode(struct inode *inode, struct writeback_control *wbc)
13521da177e4SLinus Torvalds {
1353f758eeabSChristoph Hellwig 	struct bdi_writeback *wb = &inode_to_bdi(inode)->wb;
13541da177e4SLinus Torvalds 	int ret;
13551da177e4SLinus Torvalds 
1356f758eeabSChristoph Hellwig 	spin_lock(&wb->list_lock);
13570f1b1fd8SDave Chinner 	spin_lock(&inode->i_lock);
1358f758eeabSChristoph Hellwig 	ret = writeback_single_inode(inode, wb, wbc);
13590f1b1fd8SDave Chinner 	spin_unlock(&inode->i_lock);
1360f758eeabSChristoph Hellwig 	spin_unlock(&wb->list_lock);
13611da177e4SLinus Torvalds 	return ret;
13621da177e4SLinus Torvalds }
13631da177e4SLinus Torvalds EXPORT_SYMBOL(sync_inode);
1364c3765016SChristoph Hellwig 
1365c3765016SChristoph Hellwig /**
1366c691b9d9SAndrew Morton  * sync_inode_metadata - write an inode to disk
1367c3765016SChristoph Hellwig  * @inode: the inode to sync
1368c3765016SChristoph Hellwig  * @wait: wait for I/O to complete.
1369c3765016SChristoph Hellwig  *
1370c691b9d9SAndrew Morton  * Write an inode to disk and adjust its dirty state after completion.
1371c3765016SChristoph Hellwig  *
1372c3765016SChristoph Hellwig  * Note: only writes the actual inode, no associated data or other metadata.
1373c3765016SChristoph Hellwig  */
1374c3765016SChristoph Hellwig int sync_inode_metadata(struct inode *inode, int wait)
1375c3765016SChristoph Hellwig {
1376c3765016SChristoph Hellwig 	struct writeback_control wbc = {
1377c3765016SChristoph Hellwig 		.sync_mode = wait ? WB_SYNC_ALL : WB_SYNC_NONE,
1378c3765016SChristoph Hellwig 		.nr_to_write = 0, /* metadata-only */
1379c3765016SChristoph Hellwig 	};
1380c3765016SChristoph Hellwig 
1381c3765016SChristoph Hellwig 	return sync_inode(inode, &wbc);
1382c3765016SChristoph Hellwig }
1383c3765016SChristoph Hellwig EXPORT_SYMBOL(sync_inode_metadata);
1384