xref: /openbmc/linux/fs/fs-writeback.c (revision f0d07b7f)
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>
2503ba3782SJens Axboe #include <linux/freezer.h>
261da177e4SLinus Torvalds #include <linux/writeback.h>
271da177e4SLinus Torvalds #include <linux/blkdev.h>
281da177e4SLinus Torvalds #include <linux/backing-dev.h>
29455b2864SDave Chinner #include <linux/tracepoint.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;
43d46db3d5SWu Fengguang 	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;
490e175a18SCurt Wohlgemuth 	enum wb_reason reason;		/* why was writeback initiated? */
50c4a77a6cSJens Axboe 
518010c3b6SJens Axboe 	struct list_head list;		/* pending work list */
5283ba7b07SChristoph Hellwig 	struct completion *done;	/* set if the caller waits */
5303ba3782SJens Axboe };
5403ba3782SJens Axboe 
55455b2864SDave Chinner /*
56455b2864SDave Chinner  * We don't actually have pdflush, but this one is exported though /proc...
57455b2864SDave Chinner  */
58455b2864SDave Chinner int nr_pdflush_threads;
59455b2864SDave Chinner 
60f11b00f3SAdrian Bunk /**
61f11b00f3SAdrian Bunk  * writeback_in_progress - determine whether there is writeback in progress
62f11b00f3SAdrian Bunk  * @bdi: the device's backing_dev_info structure.
63f11b00f3SAdrian Bunk  *
6403ba3782SJens Axboe  * Determine whether there is writeback waiting to be handled against a
6503ba3782SJens Axboe  * backing device.
66f11b00f3SAdrian Bunk  */
67f11b00f3SAdrian Bunk int writeback_in_progress(struct backing_dev_info *bdi)
68f11b00f3SAdrian Bunk {
6981d73a32SJan Kara 	return test_bit(BDI_writeback_running, &bdi->state);
70f11b00f3SAdrian Bunk }
71f11b00f3SAdrian Bunk 
72692ebd17SJan Kara static inline struct backing_dev_info *inode_to_bdi(struct inode *inode)
73692ebd17SJan Kara {
74692ebd17SJan Kara 	struct super_block *sb = inode->i_sb;
75692ebd17SJan Kara 
76aaead25bSChristoph Hellwig 	if (strcmp(sb->s_type->name, "bdev") == 0)
77aaead25bSChristoph Hellwig 		return inode->i_mapping->backing_dev_info;
78aaead25bSChristoph Hellwig 
79692ebd17SJan Kara 	return sb->s_bdi;
80692ebd17SJan Kara }
81692ebd17SJan Kara 
827ccf19a8SNick Piggin static inline struct inode *wb_inode(struct list_head *head)
837ccf19a8SNick Piggin {
847ccf19a8SNick Piggin 	return list_entry(head, struct inode, i_wb_list);
857ccf19a8SNick Piggin }
867ccf19a8SNick Piggin 
8715eb77a0SWu Fengguang /*
8815eb77a0SWu Fengguang  * Include the creation of the trace points after defining the
8915eb77a0SWu Fengguang  * wb_writeback_work structure and inline functions so that the definition
9015eb77a0SWu Fengguang  * remains local to this file.
9115eb77a0SWu Fengguang  */
9215eb77a0SWu Fengguang #define CREATE_TRACE_POINTS
9315eb77a0SWu Fengguang #include <trace/events/writeback.h>
9415eb77a0SWu Fengguang 
956585027aSJan Kara /* Wakeup flusher thread or forker thread to fork it. Requires bdi->wb_lock. */
966585027aSJan Kara static void bdi_wakeup_flusher(struct backing_dev_info *bdi)
974195f73dSNick Piggin {
98fff5b85aSArtem Bityutskiy 	if (bdi->wb.task) {
99fff5b85aSArtem Bityutskiy 		wake_up_process(bdi->wb.task);
100fff5b85aSArtem Bityutskiy 	} else {
1011da177e4SLinus Torvalds 		/*
102fff5b85aSArtem Bityutskiy 		 * The bdi thread isn't there, wake up the forker thread which
103fff5b85aSArtem Bityutskiy 		 * will create and run it.
1041da177e4SLinus Torvalds 		 */
10503ba3782SJens Axboe 		wake_up_process(default_backing_dev_info.wb.task);
1061da177e4SLinus Torvalds 	}
1076585027aSJan Kara }
1086585027aSJan Kara 
1096585027aSJan Kara static void bdi_queue_work(struct backing_dev_info *bdi,
1106585027aSJan Kara 			   struct wb_writeback_work *work)
1116585027aSJan Kara {
1126585027aSJan Kara 	trace_writeback_queue(bdi, work);
1136585027aSJan Kara 
1146585027aSJan Kara 	spin_lock_bh(&bdi->wb_lock);
1156585027aSJan Kara 	list_add_tail(&work->list, &bdi->work_list);
1166585027aSJan Kara 	if (!bdi->wb.task)
1176585027aSJan Kara 		trace_writeback_nothread(bdi, work);
1186585027aSJan Kara 	bdi_wakeup_flusher(bdi);
1196467716aSArtem Bityutskiy 	spin_unlock_bh(&bdi->wb_lock);
12003ba3782SJens Axboe }
1211da177e4SLinus Torvalds 
12283ba7b07SChristoph Hellwig static void
12383ba7b07SChristoph Hellwig __bdi_start_writeback(struct backing_dev_info *bdi, long nr_pages,
1240e175a18SCurt Wohlgemuth 		      bool range_cyclic, enum wb_reason reason)
1251da177e4SLinus Torvalds {
12683ba7b07SChristoph Hellwig 	struct wb_writeback_work *work;
12703ba3782SJens Axboe 
128bcddc3f0SJens Axboe 	/*
129bcddc3f0SJens Axboe 	 * This is WB_SYNC_NONE writeback, so if allocation fails just
130bcddc3f0SJens Axboe 	 * wakeup the thread for old dirty data writeback
131bcddc3f0SJens Axboe 	 */
13283ba7b07SChristoph Hellwig 	work = kzalloc(sizeof(*work), GFP_ATOMIC);
13383ba7b07SChristoph Hellwig 	if (!work) {
134455b2864SDave Chinner 		if (bdi->wb.task) {
135455b2864SDave Chinner 			trace_writeback_nowork(bdi);
13683ba7b07SChristoph Hellwig 			wake_up_process(bdi->wb.task);
137455b2864SDave Chinner 		}
13883ba7b07SChristoph Hellwig 		return;
13983ba7b07SChristoph Hellwig 	}
14083ba7b07SChristoph Hellwig 
14183ba7b07SChristoph Hellwig 	work->sync_mode	= WB_SYNC_NONE;
14283ba7b07SChristoph Hellwig 	work->nr_pages	= nr_pages;
14383ba7b07SChristoph Hellwig 	work->range_cyclic = range_cyclic;
1440e175a18SCurt Wohlgemuth 	work->reason	= reason;
14583ba7b07SChristoph Hellwig 
146f11fcae8SJens Axboe 	bdi_queue_work(bdi, work);
14703ba3782SJens Axboe }
148b6e51316SJens Axboe 
149b6e51316SJens Axboe /**
150b6e51316SJens Axboe  * bdi_start_writeback - start writeback
151b6e51316SJens Axboe  * @bdi: the backing device to write from
152b6e51316SJens Axboe  * @nr_pages: the number of pages to write
153786228abSMarcos Paulo de Souza  * @reason: reason why some writeback work was initiated
154b6e51316SJens Axboe  *
155b6e51316SJens Axboe  * Description:
156b6e51316SJens Axboe  *   This does WB_SYNC_NONE opportunistic writeback. The IO is only
15725985edcSLucas De Marchi  *   started when this function returns, we make no guarantees on
1580e3c9a22SJens Axboe  *   completion. Caller need not hold sb s_umount semaphore.
159b6e51316SJens Axboe  *
160b6e51316SJens Axboe  */
1610e175a18SCurt Wohlgemuth void bdi_start_writeback(struct backing_dev_info *bdi, long nr_pages,
1620e175a18SCurt Wohlgemuth 			enum wb_reason reason)
163b6e51316SJens Axboe {
1640e175a18SCurt Wohlgemuth 	__bdi_start_writeback(bdi, nr_pages, true, reason);
165d3ddec76SWu Fengguang }
166d3ddec76SWu Fengguang 
167c5444198SChristoph Hellwig /**
168c5444198SChristoph Hellwig  * bdi_start_background_writeback - start background writeback
169c5444198SChristoph Hellwig  * @bdi: the backing device to write from
170c5444198SChristoph Hellwig  *
171c5444198SChristoph Hellwig  * Description:
1726585027aSJan Kara  *   This makes sure WB_SYNC_NONE background writeback happens. When
1736585027aSJan Kara  *   this function returns, it is only guaranteed that for given BDI
1746585027aSJan Kara  *   some IO is happening if we are over background dirty threshold.
1756585027aSJan Kara  *   Caller need not hold sb s_umount semaphore.
176c5444198SChristoph Hellwig  */
177c5444198SChristoph Hellwig void bdi_start_background_writeback(struct backing_dev_info *bdi)
178c5444198SChristoph Hellwig {
1796585027aSJan Kara 	/*
1806585027aSJan Kara 	 * We just wake up the flusher thread. It will perform background
1816585027aSJan Kara 	 * writeback as soon as there is no other work to do.
1826585027aSJan Kara 	 */
18371927e84SWu Fengguang 	trace_writeback_wake_background(bdi);
1846585027aSJan Kara 	spin_lock_bh(&bdi->wb_lock);
1856585027aSJan Kara 	bdi_wakeup_flusher(bdi);
1866585027aSJan Kara 	spin_unlock_bh(&bdi->wb_lock);
1871da177e4SLinus Torvalds }
1881da177e4SLinus Torvalds 
1891da177e4SLinus Torvalds /*
190a66979abSDave Chinner  * Remove the inode from the writeback list it is on.
191a66979abSDave Chinner  */
192a66979abSDave Chinner void inode_wb_list_del(struct inode *inode)
193a66979abSDave Chinner {
194f758eeabSChristoph Hellwig 	struct backing_dev_info *bdi = inode_to_bdi(inode);
195a66979abSDave Chinner 
196f758eeabSChristoph Hellwig 	spin_lock(&bdi->wb.list_lock);
197f758eeabSChristoph Hellwig 	list_del_init(&inode->i_wb_list);
198f758eeabSChristoph Hellwig 	spin_unlock(&bdi->wb.list_lock);
199f758eeabSChristoph Hellwig }
200a66979abSDave Chinner 
201a66979abSDave Chinner /*
2026610a0bcSAndrew Morton  * Redirty an inode: set its when-it-was dirtied timestamp and move it to the
2036610a0bcSAndrew Morton  * furthest end of its superblock's dirty-inode list.
2046610a0bcSAndrew Morton  *
2056610a0bcSAndrew Morton  * Before stamping the inode's ->dirtied_when, we check to see whether it is
20666f3b8e2SJens Axboe  * already the most-recently-dirtied inode on the b_dirty list.  If that is
2076610a0bcSAndrew Morton  * the case then the inode must have been redirtied while it was being written
2086610a0bcSAndrew Morton  * out and we don't reset its dirtied_when.
2096610a0bcSAndrew Morton  */
210f758eeabSChristoph Hellwig static void redirty_tail(struct inode *inode, struct bdi_writeback *wb)
2116610a0bcSAndrew Morton {
212f758eeabSChristoph Hellwig 	assert_spin_locked(&wb->list_lock);
21303ba3782SJens Axboe 	if (!list_empty(&wb->b_dirty)) {
21466f3b8e2SJens Axboe 		struct inode *tail;
2156610a0bcSAndrew Morton 
2167ccf19a8SNick Piggin 		tail = wb_inode(wb->b_dirty.next);
21766f3b8e2SJens Axboe 		if (time_before(inode->dirtied_when, tail->dirtied_when))
2186610a0bcSAndrew Morton 			inode->dirtied_when = jiffies;
2196610a0bcSAndrew Morton 	}
2207ccf19a8SNick Piggin 	list_move(&inode->i_wb_list, &wb->b_dirty);
2216610a0bcSAndrew Morton }
2226610a0bcSAndrew Morton 
2236610a0bcSAndrew Morton /*
22466f3b8e2SJens Axboe  * requeue inode for re-scanning after bdi->b_io list is exhausted.
225c986d1e2SAndrew Morton  */
226f758eeabSChristoph Hellwig static void requeue_io(struct inode *inode, struct bdi_writeback *wb)
227c986d1e2SAndrew Morton {
228f758eeabSChristoph Hellwig 	assert_spin_locked(&wb->list_lock);
2297ccf19a8SNick Piggin 	list_move(&inode->i_wb_list, &wb->b_more_io);
230c986d1e2SAndrew Morton }
231c986d1e2SAndrew Morton 
2321c0eeaf5SJoern Engel static void inode_sync_complete(struct inode *inode)
2331c0eeaf5SJoern Engel {
234365b94aeSJan Kara 	inode->i_state &= ~I_SYNC;
235365b94aeSJan Kara 	/* Waiters must see I_SYNC cleared before being woken up */
2361c0eeaf5SJoern Engel 	smp_mb();
2371c0eeaf5SJoern Engel 	wake_up_bit(&inode->i_state, __I_SYNC);
2381c0eeaf5SJoern Engel }
2391c0eeaf5SJoern Engel 
240d2caa3c5SJeff Layton static bool inode_dirtied_after(struct inode *inode, unsigned long t)
241d2caa3c5SJeff Layton {
242d2caa3c5SJeff Layton 	bool ret = time_after(inode->dirtied_when, t);
243d2caa3c5SJeff Layton #ifndef CONFIG_64BIT
244d2caa3c5SJeff Layton 	/*
245d2caa3c5SJeff Layton 	 * For inodes being constantly redirtied, dirtied_when can get stuck.
246d2caa3c5SJeff Layton 	 * It _appears_ to be in the future, but is actually in distant past.
247d2caa3c5SJeff Layton 	 * This test is necessary to prevent such wrapped-around relative times
2485b0830cbSJens Axboe 	 * from permanently stopping the whole bdi writeback.
249d2caa3c5SJeff Layton 	 */
250d2caa3c5SJeff Layton 	ret = ret && time_before_eq(inode->dirtied_when, jiffies);
251d2caa3c5SJeff Layton #endif
252d2caa3c5SJeff Layton 	return ret;
253d2caa3c5SJeff Layton }
254d2caa3c5SJeff Layton 
255c986d1e2SAndrew Morton /*
256697e6fedSJan Kara  * Move expired (dirtied after work->older_than_this) dirty inodes from
257697e6fedSJan Kara  * @delaying_queue to @dispatch_queue.
2582c136579SFengguang Wu  */
259e84d0a4fSWu Fengguang static int move_expired_inodes(struct list_head *delaying_queue,
2602c136579SFengguang Wu 			       struct list_head *dispatch_queue,
261ad4e38ddSCurt Wohlgemuth 			       struct wb_writeback_work *work)
2622c136579SFengguang Wu {
2635c03449dSShaohua Li 	LIST_HEAD(tmp);
2645c03449dSShaohua Li 	struct list_head *pos, *node;
265cf137307SJens Axboe 	struct super_block *sb = NULL;
2665c03449dSShaohua Li 	struct inode *inode;
267cf137307SJens Axboe 	int do_sb_sort = 0;
268e84d0a4fSWu Fengguang 	int moved = 0;
2695c03449dSShaohua Li 
2702c136579SFengguang Wu 	while (!list_empty(delaying_queue)) {
2717ccf19a8SNick Piggin 		inode = wb_inode(delaying_queue->prev);
272ad4e38ddSCurt Wohlgemuth 		if (work->older_than_this &&
273ad4e38ddSCurt Wohlgemuth 		    inode_dirtied_after(inode, *work->older_than_this))
2742c136579SFengguang Wu 			break;
275cf137307SJens Axboe 		if (sb && sb != inode->i_sb)
276cf137307SJens Axboe 			do_sb_sort = 1;
277cf137307SJens Axboe 		sb = inode->i_sb;
2787ccf19a8SNick Piggin 		list_move(&inode->i_wb_list, &tmp);
279e84d0a4fSWu Fengguang 		moved++;
2805c03449dSShaohua Li 	}
2815c03449dSShaohua Li 
282cf137307SJens Axboe 	/* just one sb in list, splice to dispatch_queue and we're done */
283cf137307SJens Axboe 	if (!do_sb_sort) {
284cf137307SJens Axboe 		list_splice(&tmp, dispatch_queue);
285e84d0a4fSWu Fengguang 		goto out;
286cf137307SJens Axboe 	}
287cf137307SJens Axboe 
2885c03449dSShaohua Li 	/* Move inodes from one superblock together */
2895c03449dSShaohua Li 	while (!list_empty(&tmp)) {
2907ccf19a8SNick Piggin 		sb = wb_inode(tmp.prev)->i_sb;
2915c03449dSShaohua Li 		list_for_each_prev_safe(pos, node, &tmp) {
2927ccf19a8SNick Piggin 			inode = wb_inode(pos);
2935c03449dSShaohua Li 			if (inode->i_sb == sb)
2947ccf19a8SNick Piggin 				list_move(&inode->i_wb_list, dispatch_queue);
2952c136579SFengguang Wu 		}
2962c136579SFengguang Wu 	}
297e84d0a4fSWu Fengguang out:
298e84d0a4fSWu Fengguang 	return moved;
2995c03449dSShaohua Li }
3002c136579SFengguang Wu 
3012c136579SFengguang Wu /*
3022c136579SFengguang Wu  * Queue all expired dirty inodes for io, eldest first.
3034ea879b9SWu Fengguang  * Before
3044ea879b9SWu Fengguang  *         newly dirtied     b_dirty    b_io    b_more_io
3054ea879b9SWu Fengguang  *         =============>    gf         edc     BA
3064ea879b9SWu Fengguang  * After
3074ea879b9SWu Fengguang  *         newly dirtied     b_dirty    b_io    b_more_io
3084ea879b9SWu Fengguang  *         =============>    g          fBAedc
3094ea879b9SWu Fengguang  *                                           |
3104ea879b9SWu Fengguang  *                                           +--> dequeue for IO
3112c136579SFengguang Wu  */
312ad4e38ddSCurt Wohlgemuth static void queue_io(struct bdi_writeback *wb, struct wb_writeback_work *work)
3132c136579SFengguang Wu {
314e84d0a4fSWu Fengguang 	int moved;
315f758eeabSChristoph Hellwig 	assert_spin_locked(&wb->list_lock);
3164ea879b9SWu Fengguang 	list_splice_init(&wb->b_more_io, &wb->b_io);
317ad4e38ddSCurt Wohlgemuth 	moved = move_expired_inodes(&wb->b_dirty, &wb->b_io, work);
318ad4e38ddSCurt Wohlgemuth 	trace_writeback_queue_io(wb, work, moved);
31966f3b8e2SJens Axboe }
32066f3b8e2SJens Axboe 
321a9185b41SChristoph Hellwig static int write_inode(struct inode *inode, struct writeback_control *wbc)
32266f3b8e2SJens Axboe {
32303ba3782SJens Axboe 	if (inode->i_sb->s_op->write_inode && !is_bad_inode(inode))
324a9185b41SChristoph Hellwig 		return inode->i_sb->s_op->write_inode(inode, wbc);
32503ba3782SJens Axboe 	return 0;
32666f3b8e2SJens Axboe }
32708d8e974SFengguang Wu 
3282c136579SFengguang Wu /*
32901c03194SChristoph Hellwig  * Wait for writeback on an inode to complete.
33001c03194SChristoph Hellwig  */
331f0d07b7fSJan Kara static void inode_wait_for_writeback(struct inode *inode)
33201c03194SChristoph Hellwig {
33301c03194SChristoph Hellwig 	DEFINE_WAIT_BIT(wq, &inode->i_state, __I_SYNC);
33401c03194SChristoph Hellwig 	wait_queue_head_t *wqh;
33501c03194SChristoph Hellwig 
33601c03194SChristoph Hellwig 	wqh = bit_waitqueue(&inode->i_state, __I_SYNC);
33758a9d3d8SRichard Kennedy 	while (inode->i_state & I_SYNC) {
338250df6edSDave Chinner 		spin_unlock(&inode->i_lock);
33901c03194SChristoph Hellwig 		__wait_on_bit(wqh, &wq, inode_wait, TASK_UNINTERRUPTIBLE);
340250df6edSDave Chinner 		spin_lock(&inode->i_lock);
34158a9d3d8SRichard Kennedy 	}
34201c03194SChristoph Hellwig }
34301c03194SChristoph Hellwig 
34401c03194SChristoph Hellwig /*
345ccb26b5aSJan Kara  * Find proper writeback list for the inode depending on its current state and
346ccb26b5aSJan Kara  * possibly also change of its state while we were doing writeback.  Here we
347ccb26b5aSJan Kara  * handle things such as livelock prevention or fairness of writeback among
348ccb26b5aSJan Kara  * inodes. This function can be called only by flusher thread - noone else
349ccb26b5aSJan Kara  * processes all inodes in writeback lists and requeueing inodes behind flusher
350ccb26b5aSJan Kara  * thread's back can have unexpected consequences.
351ccb26b5aSJan Kara  */
352ccb26b5aSJan Kara static void requeue_inode(struct inode *inode, struct bdi_writeback *wb,
353ccb26b5aSJan Kara 			  struct writeback_control *wbc)
354ccb26b5aSJan Kara {
355ccb26b5aSJan Kara 	if (inode->i_state & I_FREEING)
356ccb26b5aSJan Kara 		return;
357ccb26b5aSJan Kara 
358ccb26b5aSJan Kara 	/*
359ccb26b5aSJan Kara 	 * Sync livelock prevention. Each inode is tagged and synced in one
360ccb26b5aSJan Kara 	 * shot. If still dirty, it will be redirty_tail()'ed below.  Update
361ccb26b5aSJan Kara 	 * the dirty time to prevent enqueue and sync it again.
362ccb26b5aSJan Kara 	 */
363ccb26b5aSJan Kara 	if ((inode->i_state & I_DIRTY) &&
364ccb26b5aSJan Kara 	    (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages))
365ccb26b5aSJan Kara 		inode->dirtied_when = jiffies;
366ccb26b5aSJan Kara 
367ccb26b5aSJan Kara 	if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY)) {
368ccb26b5aSJan Kara 		/*
369ccb26b5aSJan Kara 		 * We didn't write back all the pages.  nfs_writepages()
370ccb26b5aSJan Kara 		 * sometimes bales out without doing anything.
371ccb26b5aSJan Kara 		 */
372ccb26b5aSJan Kara 		if (wbc->nr_to_write <= 0) {
373ccb26b5aSJan Kara 			/* Slice used up. Queue for next turn. */
374ccb26b5aSJan Kara 			requeue_io(inode, wb);
375ccb26b5aSJan Kara 		} else {
376ccb26b5aSJan Kara 			/*
377ccb26b5aSJan Kara 			 * Writeback blocked by something other than
378ccb26b5aSJan Kara 			 * congestion. Delay the inode for some time to
379ccb26b5aSJan Kara 			 * avoid spinning on the CPU (100% iowait)
380ccb26b5aSJan Kara 			 * retrying writeback of the dirty page/inode
381ccb26b5aSJan Kara 			 * that cannot be performed immediately.
382ccb26b5aSJan Kara 			 */
383ccb26b5aSJan Kara 			redirty_tail(inode, wb);
384ccb26b5aSJan Kara 		}
385ccb26b5aSJan Kara 	} else if (inode->i_state & I_DIRTY) {
386ccb26b5aSJan Kara 		/*
387ccb26b5aSJan Kara 		 * Filesystems can dirty the inode during writeback operations,
388ccb26b5aSJan Kara 		 * such as delayed allocation during submission or metadata
389ccb26b5aSJan Kara 		 * updates after data IO completion.
390ccb26b5aSJan Kara 		 */
391ccb26b5aSJan Kara 		redirty_tail(inode, wb);
392ccb26b5aSJan Kara 	} else {
393ccb26b5aSJan Kara 		/* The inode is clean. Remove from writeback lists. */
394ccb26b5aSJan Kara 		list_del_init(&inode->i_wb_list);
395ccb26b5aSJan Kara 	}
396ccb26b5aSJan Kara }
397ccb26b5aSJan Kara 
398ccb26b5aSJan Kara /*
399f758eeabSChristoph Hellwig  * Write out an inode's dirty pages.  Called under wb->list_lock and
4000f1b1fd8SDave Chinner  * inode->i_lock.  Either the caller has an active reference on the inode or
4010f1b1fd8SDave Chinner  * the inode has I_WILL_FREE set.
40201c03194SChristoph Hellwig  *
4031da177e4SLinus Torvalds  * If `wait' is set, wait on the writeout.
4041da177e4SLinus Torvalds  *
4051da177e4SLinus Torvalds  * The whole writeout design is quite complex and fragile.  We want to avoid
4061da177e4SLinus Torvalds  * starvation of particular inodes when others are being redirtied, prevent
4071da177e4SLinus Torvalds  * livelocks, etc.
4081da177e4SLinus Torvalds  */
4091da177e4SLinus Torvalds static int
410f758eeabSChristoph Hellwig writeback_single_inode(struct inode *inode, struct bdi_writeback *wb,
411f758eeabSChristoph Hellwig 		       struct writeback_control *wbc)
4121da177e4SLinus Torvalds {
4131da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
414251d6a47SWu Fengguang 	long nr_to_write = wbc->nr_to_write;
41501c03194SChristoph Hellwig 	unsigned dirty;
4161da177e4SLinus Torvalds 	int ret;
4171da177e4SLinus Torvalds 
4180f1b1fd8SDave Chinner 	assert_spin_locked(&inode->i_lock);
4190f1b1fd8SDave Chinner 
42001c03194SChristoph Hellwig 	if (!atomic_read(&inode->i_count))
42101c03194SChristoph Hellwig 		WARN_ON(!(inode->i_state & (I_WILL_FREE|I_FREEING)));
42201c03194SChristoph Hellwig 	else
42301c03194SChristoph Hellwig 		WARN_ON(inode->i_state & I_WILL_FREE);
42401c03194SChristoph Hellwig 
42501c03194SChristoph Hellwig 	if (inode->i_state & I_SYNC) {
426cc1676d9SJan Kara 		if (wbc->sync_mode != WB_SYNC_ALL)
42701c03194SChristoph Hellwig 			return 0;
42801c03194SChristoph Hellwig 		/*
42901c03194SChristoph Hellwig 		 * It's a data-integrity sync.  We must wait.
43001c03194SChristoph Hellwig 		 */
431f0d07b7fSJan Kara 		inode_wait_for_writeback(inode);
43201c03194SChristoph Hellwig 	}
43301c03194SChristoph Hellwig 
4341c0eeaf5SJoern Engel 	BUG_ON(inode->i_state & I_SYNC);
4351da177e4SLinus Torvalds 
4365547e8aaSDmitry Monakhov 	/* Set I_SYNC, reset I_DIRTY_PAGES */
4371c0eeaf5SJoern Engel 	inode->i_state |= I_SYNC;
438250df6edSDave Chinner 	spin_unlock(&inode->i_lock);
4391da177e4SLinus Torvalds 
4401da177e4SLinus Torvalds 	ret = do_writepages(mapping, wbc);
4411da177e4SLinus Torvalds 
44226821ed4SChristoph Hellwig 	/*
44326821ed4SChristoph Hellwig 	 * Make sure to wait on the data before writing out the metadata.
44426821ed4SChristoph Hellwig 	 * This is important for filesystems that modify metadata on data
44526821ed4SChristoph Hellwig 	 * I/O completion.
44626821ed4SChristoph Hellwig 	 */
447a9185b41SChristoph Hellwig 	if (wbc->sync_mode == WB_SYNC_ALL) {
44826821ed4SChristoph Hellwig 		int err = filemap_fdatawait(mapping);
4491da177e4SLinus Torvalds 		if (ret == 0)
4501da177e4SLinus Torvalds 			ret = err;
4511da177e4SLinus Torvalds 	}
4521da177e4SLinus Torvalds 
4535547e8aaSDmitry Monakhov 	/*
4545547e8aaSDmitry Monakhov 	 * Some filesystems may redirty the inode during the writeback
4555547e8aaSDmitry Monakhov 	 * due to delalloc, clear dirty metadata flags right before
4565547e8aaSDmitry Monakhov 	 * write_inode()
4575547e8aaSDmitry Monakhov 	 */
458250df6edSDave Chinner 	spin_lock(&inode->i_lock);
4596290be1cSJan Kara 	/* Clear I_DIRTY_PAGES if we've written out all dirty pages */
4606290be1cSJan Kara 	if (!mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
4616290be1cSJan Kara 		inode->i_state &= ~I_DIRTY_PAGES;
4625547e8aaSDmitry Monakhov 	dirty = inode->i_state & I_DIRTY;
4635547e8aaSDmitry Monakhov 	inode->i_state &= ~(I_DIRTY_SYNC | I_DIRTY_DATASYNC);
464250df6edSDave Chinner 	spin_unlock(&inode->i_lock);
46526821ed4SChristoph Hellwig 	/* Don't write the inode if only I_DIRTY_PAGES was set */
46626821ed4SChristoph Hellwig 	if (dirty & (I_DIRTY_SYNC | I_DIRTY_DATASYNC)) {
467a9185b41SChristoph Hellwig 		int err = write_inode(inode, wbc);
4681da177e4SLinus Torvalds 		if (ret == 0)
4691da177e4SLinus Torvalds 			ret = err;
4701da177e4SLinus Torvalds 	}
4711da177e4SLinus Torvalds 
472f758eeabSChristoph Hellwig 	spin_lock(&wb->list_lock);
473250df6edSDave Chinner 	spin_lock(&inode->i_lock);
474ccb26b5aSJan Kara 	requeue_inode(inode, wb, wbc);
4751c0eeaf5SJoern Engel 	inode_sync_complete(inode);
476251d6a47SWu Fengguang 	trace_writeback_single_inode(inode, wbc, nr_to_write);
4771da177e4SLinus Torvalds 	return ret;
4781da177e4SLinus Torvalds }
4791da177e4SLinus Torvalds 
4801a12d8bdSWu Fengguang static long writeback_chunk_size(struct backing_dev_info *bdi,
4811a12d8bdSWu Fengguang 				 struct wb_writeback_work *work)
482d46db3d5SWu Fengguang {
483d46db3d5SWu Fengguang 	long pages;
484d46db3d5SWu Fengguang 
485d46db3d5SWu Fengguang 	/*
486d46db3d5SWu Fengguang 	 * WB_SYNC_ALL mode does livelock avoidance by syncing dirty
487d46db3d5SWu Fengguang 	 * inodes/pages in one big loop. Setting wbc.nr_to_write=LONG_MAX
488d46db3d5SWu Fengguang 	 * here avoids calling into writeback_inodes_wb() more than once.
489d46db3d5SWu Fengguang 	 *
490d46db3d5SWu Fengguang 	 * The intended call sequence for WB_SYNC_ALL writeback is:
491d46db3d5SWu Fengguang 	 *
492d46db3d5SWu Fengguang 	 *      wb_writeback()
493d46db3d5SWu Fengguang 	 *          writeback_sb_inodes()       <== called only once
494d46db3d5SWu Fengguang 	 *              write_cache_pages()     <== called once for each inode
495d46db3d5SWu Fengguang 	 *                   (quickly) tag currently dirty pages
496d46db3d5SWu Fengguang 	 *                   (maybe slowly) sync all tagged pages
497d46db3d5SWu Fengguang 	 */
498d46db3d5SWu Fengguang 	if (work->sync_mode == WB_SYNC_ALL || work->tagged_writepages)
499d46db3d5SWu Fengguang 		pages = LONG_MAX;
5001a12d8bdSWu Fengguang 	else {
5011a12d8bdSWu Fengguang 		pages = min(bdi->avg_write_bandwidth / 2,
5021a12d8bdSWu Fengguang 			    global_dirty_limit / DIRTY_SCOPE);
5031a12d8bdSWu Fengguang 		pages = min(pages, work->nr_pages);
5041a12d8bdSWu Fengguang 		pages = round_down(pages + MIN_WRITEBACK_PAGES,
5051a12d8bdSWu Fengguang 				   MIN_WRITEBACK_PAGES);
5061a12d8bdSWu Fengguang 	}
507d46db3d5SWu Fengguang 
508d46db3d5SWu Fengguang 	return pages;
509d46db3d5SWu Fengguang }
510d46db3d5SWu Fengguang 
51103ba3782SJens Axboe /*
512f11c9c5cSEdward Shishkin  * Write a portion of b_io inodes which belong to @sb.
513edadfb10SChristoph Hellwig  *
514edadfb10SChristoph Hellwig  * If @only_this_sb is true, then find and write all such
515f11c9c5cSEdward Shishkin  * inodes. Otherwise write only ones which go sequentially
516f11c9c5cSEdward Shishkin  * in reverse order.
517edadfb10SChristoph Hellwig  *
518d46db3d5SWu Fengguang  * Return the number of pages and/or inodes written.
519f11c9c5cSEdward Shishkin  */
520d46db3d5SWu Fengguang static long writeback_sb_inodes(struct super_block *sb,
521d46db3d5SWu Fengguang 				struct bdi_writeback *wb,
522d46db3d5SWu Fengguang 				struct wb_writeback_work *work)
52303ba3782SJens Axboe {
524d46db3d5SWu Fengguang 	struct writeback_control wbc = {
525d46db3d5SWu Fengguang 		.sync_mode		= work->sync_mode,
526d46db3d5SWu Fengguang 		.tagged_writepages	= work->tagged_writepages,
527d46db3d5SWu Fengguang 		.for_kupdate		= work->for_kupdate,
528d46db3d5SWu Fengguang 		.for_background		= work->for_background,
529d46db3d5SWu Fengguang 		.range_cyclic		= work->range_cyclic,
530d46db3d5SWu Fengguang 		.range_start		= 0,
531d46db3d5SWu Fengguang 		.range_end		= LLONG_MAX,
532d46db3d5SWu Fengguang 	};
533d46db3d5SWu Fengguang 	unsigned long start_time = jiffies;
534d46db3d5SWu Fengguang 	long write_chunk;
535d46db3d5SWu Fengguang 	long wrote = 0;  /* count both pages and inodes */
536d46db3d5SWu Fengguang 
53703ba3782SJens Axboe 	while (!list_empty(&wb->b_io)) {
5387ccf19a8SNick Piggin 		struct inode *inode = wb_inode(wb->b_io.prev);
539edadfb10SChristoph Hellwig 
540edadfb10SChristoph Hellwig 		if (inode->i_sb != sb) {
541d46db3d5SWu Fengguang 			if (work->sb) {
542edadfb10SChristoph Hellwig 				/*
543edadfb10SChristoph Hellwig 				 * We only want to write back data for this
544edadfb10SChristoph Hellwig 				 * superblock, move all inodes not belonging
545edadfb10SChristoph Hellwig 				 * to it back onto the dirty list.
546edadfb10SChristoph Hellwig 				 */
547f758eeabSChristoph Hellwig 				redirty_tail(inode, wb);
54866f3b8e2SJens Axboe 				continue;
54966f3b8e2SJens Axboe 			}
550edadfb10SChristoph Hellwig 
551edadfb10SChristoph Hellwig 			/*
552edadfb10SChristoph Hellwig 			 * The inode belongs to a different superblock.
553edadfb10SChristoph Hellwig 			 * Bounce back to the caller to unpin this and
554edadfb10SChristoph Hellwig 			 * pin the next superblock.
555edadfb10SChristoph Hellwig 			 */
556d46db3d5SWu Fengguang 			break;
557edadfb10SChristoph Hellwig 		}
558edadfb10SChristoph Hellwig 
5599843b76aSChristoph Hellwig 		/*
5609843b76aSChristoph Hellwig 		 * Don't bother with new inodes or inodes beeing freed, first
5619843b76aSChristoph Hellwig 		 * kind does not need peridic writeout yet, and for the latter
5629843b76aSChristoph Hellwig 		 * kind writeout is handled by the freer.
5639843b76aSChristoph Hellwig 		 */
564250df6edSDave Chinner 		spin_lock(&inode->i_lock);
5659843b76aSChristoph Hellwig 		if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) {
566250df6edSDave Chinner 			spin_unlock(&inode->i_lock);
567fcc5c222SWu Fengguang 			redirty_tail(inode, wb);
5687ef0d737SNick Piggin 			continue;
5697ef0d737SNick Piggin 		}
570cc1676d9SJan Kara 		if ((inode->i_state & I_SYNC) && wbc.sync_mode != WB_SYNC_ALL) {
571cc1676d9SJan Kara 			/*
572cc1676d9SJan Kara 			 * If this inode is locked for writeback and we are not
573cc1676d9SJan Kara 			 * doing writeback-for-data-integrity, move it to
574cc1676d9SJan Kara 			 * b_more_io so that writeback can proceed with the
575cc1676d9SJan Kara 			 * other inodes on s_io.
576cc1676d9SJan Kara 			 *
577cc1676d9SJan Kara 			 * We'll have another go at writing back this inode
578cc1676d9SJan Kara 			 * when we completed a full scan of b_io.
579cc1676d9SJan Kara 			 */
580cc1676d9SJan Kara 			spin_unlock(&inode->i_lock);
581cc1676d9SJan Kara 			requeue_io(inode, wb);
582cc1676d9SJan Kara 			trace_writeback_sb_inodes_requeue(inode);
583cc1676d9SJan Kara 			continue;
584cc1676d9SJan Kara 		}
585f0d07b7fSJan Kara 		spin_unlock(&wb->list_lock);
586f0d07b7fSJan Kara 
5871da177e4SLinus Torvalds 		__iget(inode);
5881a12d8bdSWu Fengguang 		write_chunk = writeback_chunk_size(wb->bdi, work);
589d46db3d5SWu Fengguang 		wbc.nr_to_write = write_chunk;
590d46db3d5SWu Fengguang 		wbc.pages_skipped = 0;
591250df6edSDave Chinner 
592d46db3d5SWu Fengguang 		writeback_single_inode(inode, wb, &wbc);
593d46db3d5SWu Fengguang 
594d46db3d5SWu Fengguang 		work->nr_pages -= write_chunk - wbc.nr_to_write;
595d46db3d5SWu Fengguang 		wrote += write_chunk - wbc.nr_to_write;
596d46db3d5SWu Fengguang 		if (!(inode->i_state & I_DIRTY))
597d46db3d5SWu Fengguang 			wrote++;
598d46db3d5SWu Fengguang 		if (wbc.pages_skipped) {
5991da177e4SLinus Torvalds 			/*
6001da177e4SLinus Torvalds 			 * writeback is not making progress due to locked
6011da177e4SLinus Torvalds 			 * buffers.  Skip this inode for now.
6021da177e4SLinus Torvalds 			 */
603f758eeabSChristoph Hellwig 			redirty_tail(inode, wb);
6041da177e4SLinus Torvalds 		}
6050f1b1fd8SDave Chinner 		spin_unlock(&inode->i_lock);
606f758eeabSChristoph Hellwig 		spin_unlock(&wb->list_lock);
6071da177e4SLinus Torvalds 		iput(inode);
6084ffc8444SOGAWA Hirofumi 		cond_resched();
609f758eeabSChristoph Hellwig 		spin_lock(&wb->list_lock);
610d46db3d5SWu Fengguang 		/*
611d46db3d5SWu Fengguang 		 * bail out to wb_writeback() often enough to check
612d46db3d5SWu Fengguang 		 * background threshold and other termination conditions.
613d46db3d5SWu Fengguang 		 */
614d46db3d5SWu Fengguang 		if (wrote) {
615d46db3d5SWu Fengguang 			if (time_is_before_jiffies(start_time + HZ / 10UL))
616d46db3d5SWu Fengguang 				break;
617d46db3d5SWu Fengguang 			if (work->nr_pages <= 0)
618d46db3d5SWu Fengguang 				break;
6191da177e4SLinus Torvalds 		}
6208bc3be27SFengguang Wu 	}
621d46db3d5SWu Fengguang 	return wrote;
622f11c9c5cSEdward Shishkin }
62338f21977SNick Piggin 
624d46db3d5SWu Fengguang static long __writeback_inodes_wb(struct bdi_writeback *wb,
625d46db3d5SWu Fengguang 				  struct wb_writeback_work *work)
626f11c9c5cSEdward Shishkin {
627d46db3d5SWu Fengguang 	unsigned long start_time = jiffies;
628d46db3d5SWu Fengguang 	long wrote = 0;
629f11c9c5cSEdward Shishkin 
630f11c9c5cSEdward Shishkin 	while (!list_empty(&wb->b_io)) {
6317ccf19a8SNick Piggin 		struct inode *inode = wb_inode(wb->b_io.prev);
632f11c9c5cSEdward Shishkin 		struct super_block *sb = inode->i_sb;
633f11c9c5cSEdward Shishkin 
63412ad3ab6SDave Chinner 		if (!grab_super_passive(sb)) {
6350e995816SWu Fengguang 			/*
6360e995816SWu Fengguang 			 * grab_super_passive() may fail consistently due to
6370e995816SWu Fengguang 			 * s_umount being grabbed by someone else. Don't use
6380e995816SWu Fengguang 			 * requeue_io() to avoid busy retrying the inode/sb.
6390e995816SWu Fengguang 			 */
6400e995816SWu Fengguang 			redirty_tail(inode, wb);
641d19de7edSChristoph Hellwig 			continue;
642334132aeSChristoph Hellwig 		}
643d46db3d5SWu Fengguang 		wrote += writeback_sb_inodes(sb, wb, work);
644d19de7edSChristoph Hellwig 		drop_super(sb);
645f11c9c5cSEdward Shishkin 
646d46db3d5SWu Fengguang 		/* refer to the same tests at the end of writeback_sb_inodes */
647d46db3d5SWu Fengguang 		if (wrote) {
648d46db3d5SWu Fengguang 			if (time_is_before_jiffies(start_time + HZ / 10UL))
649d46db3d5SWu Fengguang 				break;
650d46db3d5SWu Fengguang 			if (work->nr_pages <= 0)
651f11c9c5cSEdward Shishkin 				break;
652f11c9c5cSEdward Shishkin 		}
653d46db3d5SWu Fengguang 	}
65466f3b8e2SJens Axboe 	/* Leave any unwritten inodes on b_io */
655d46db3d5SWu Fengguang 	return wrote;
65666f3b8e2SJens Axboe }
65766f3b8e2SJens Axboe 
6580e175a18SCurt Wohlgemuth long writeback_inodes_wb(struct bdi_writeback *wb, long nr_pages,
6590e175a18SCurt Wohlgemuth 				enum wb_reason reason)
660edadfb10SChristoph Hellwig {
661d46db3d5SWu Fengguang 	struct wb_writeback_work work = {
662d46db3d5SWu Fengguang 		.nr_pages	= nr_pages,
663d46db3d5SWu Fengguang 		.sync_mode	= WB_SYNC_NONE,
664d46db3d5SWu Fengguang 		.range_cyclic	= 1,
6650e175a18SCurt Wohlgemuth 		.reason		= reason,
666d46db3d5SWu Fengguang 	};
667edadfb10SChristoph Hellwig 
668f758eeabSChristoph Hellwig 	spin_lock(&wb->list_lock);
669424b351fSWu Fengguang 	if (list_empty(&wb->b_io))
670ad4e38ddSCurt Wohlgemuth 		queue_io(wb, &work);
671d46db3d5SWu Fengguang 	__writeback_inodes_wb(wb, &work);
672f758eeabSChristoph Hellwig 	spin_unlock(&wb->list_lock);
673edadfb10SChristoph Hellwig 
674d46db3d5SWu Fengguang 	return nr_pages - work.nr_pages;
67566f3b8e2SJens Axboe }
67666f3b8e2SJens Axboe 
677b00949aaSWu Fengguang static bool over_bground_thresh(struct backing_dev_info *bdi)
67803ba3782SJens Axboe {
67903ba3782SJens Axboe 	unsigned long background_thresh, dirty_thresh;
68003ba3782SJens Axboe 
68116c4042fSWu Fengguang 	global_dirty_limits(&background_thresh, &dirty_thresh);
68203ba3782SJens Axboe 
683b00949aaSWu Fengguang 	if (global_page_state(NR_FILE_DIRTY) +
684b00949aaSWu Fengguang 	    global_page_state(NR_UNSTABLE_NFS) > background_thresh)
685b00949aaSWu Fengguang 		return true;
686b00949aaSWu Fengguang 
687b00949aaSWu Fengguang 	if (bdi_stat(bdi, BDI_RECLAIMABLE) >
688b00949aaSWu Fengguang 				bdi_dirty_limit(bdi, background_thresh))
689b00949aaSWu Fengguang 		return true;
690b00949aaSWu Fengguang 
691b00949aaSWu Fengguang 	return false;
69203ba3782SJens Axboe }
69303ba3782SJens Axboe 
69403ba3782SJens Axboe /*
695e98be2d5SWu Fengguang  * Called under wb->list_lock. If there are multiple wb per bdi,
696e98be2d5SWu Fengguang  * only the flusher working on the first wb should do it.
697e98be2d5SWu Fengguang  */
698e98be2d5SWu Fengguang static void wb_update_bandwidth(struct bdi_writeback *wb,
699e98be2d5SWu Fengguang 				unsigned long start_time)
700e98be2d5SWu Fengguang {
701af6a3113SWu Fengguang 	__bdi_update_bandwidth(wb->bdi, 0, 0, 0, 0, 0, start_time);
702e98be2d5SWu Fengguang }
703e98be2d5SWu Fengguang 
704e98be2d5SWu Fengguang /*
70503ba3782SJens Axboe  * Explicit flushing or periodic writeback of "old" data.
70603ba3782SJens Axboe  *
70703ba3782SJens Axboe  * Define "old": the first time one of an inode's pages is dirtied, we mark the
70803ba3782SJens Axboe  * dirtying-time in the inode's address_space.  So this periodic writeback code
70903ba3782SJens Axboe  * just walks the superblock inode list, writing back any inodes which are
71003ba3782SJens Axboe  * older than a specific point in time.
71103ba3782SJens Axboe  *
71203ba3782SJens Axboe  * Try to run once per dirty_writeback_interval.  But if a writeback event
71303ba3782SJens Axboe  * takes longer than a dirty_writeback_interval interval, then leave a
71403ba3782SJens Axboe  * one-second gap.
71503ba3782SJens Axboe  *
71603ba3782SJens Axboe  * older_than_this takes precedence over nr_to_write.  So we'll only write back
71703ba3782SJens Axboe  * all dirty pages if they are all attached to "old" mappings.
71803ba3782SJens Axboe  */
719c4a77a6cSJens Axboe static long wb_writeback(struct bdi_writeback *wb,
72083ba7b07SChristoph Hellwig 			 struct wb_writeback_work *work)
72103ba3782SJens Axboe {
722e98be2d5SWu Fengguang 	unsigned long wb_start = jiffies;
723d46db3d5SWu Fengguang 	long nr_pages = work->nr_pages;
72403ba3782SJens Axboe 	unsigned long oldest_jif;
725a5989bdcSJan Kara 	struct inode *inode;
726d46db3d5SWu Fengguang 	long progress;
72703ba3782SJens Axboe 
728e185dda8SWu Fengguang 	oldest_jif = jiffies;
729d46db3d5SWu Fengguang 	work->older_than_this = &oldest_jif;
73003ba3782SJens Axboe 
731e8dfc305SWu Fengguang 	spin_lock(&wb->list_lock);
73203ba3782SJens Axboe 	for (;;) {
73303ba3782SJens Axboe 		/*
734d3ddec76SWu Fengguang 		 * Stop writeback when nr_pages has been consumed
73503ba3782SJens Axboe 		 */
73683ba7b07SChristoph Hellwig 		if (work->nr_pages <= 0)
73703ba3782SJens Axboe 			break;
73803ba3782SJens Axboe 
73903ba3782SJens Axboe 		/*
740aa373cf5SJan Kara 		 * Background writeout and kupdate-style writeback may
741aa373cf5SJan Kara 		 * run forever. Stop them if there is other work to do
742aa373cf5SJan Kara 		 * so that e.g. sync can proceed. They'll be restarted
743aa373cf5SJan Kara 		 * after the other works are all done.
744aa373cf5SJan Kara 		 */
745aa373cf5SJan Kara 		if ((work->for_background || work->for_kupdate) &&
746aa373cf5SJan Kara 		    !list_empty(&wb->bdi->work_list))
747aa373cf5SJan Kara 			break;
748aa373cf5SJan Kara 
749aa373cf5SJan Kara 		/*
750d3ddec76SWu Fengguang 		 * For background writeout, stop when we are below the
751d3ddec76SWu Fengguang 		 * background dirty threshold
75203ba3782SJens Axboe 		 */
753b00949aaSWu Fengguang 		if (work->for_background && !over_bground_thresh(wb->bdi))
75403ba3782SJens Axboe 			break;
75503ba3782SJens Axboe 
7561bc36b64SJan Kara 		/*
7571bc36b64SJan Kara 		 * Kupdate and background works are special and we want to
7581bc36b64SJan Kara 		 * include all inodes that need writing. Livelock avoidance is
7591bc36b64SJan Kara 		 * handled by these works yielding to any other work so we are
7601bc36b64SJan Kara 		 * safe.
7611bc36b64SJan Kara 		 */
762ba9aa839SWu Fengguang 		if (work->for_kupdate) {
763ba9aa839SWu Fengguang 			oldest_jif = jiffies -
764ba9aa839SWu Fengguang 				msecs_to_jiffies(dirty_expire_interval * 10);
7651bc36b64SJan Kara 		} else if (work->for_background)
7661bc36b64SJan Kara 			oldest_jif = jiffies;
767028c2dd1SDave Chinner 
768d46db3d5SWu Fengguang 		trace_writeback_start(wb->bdi, work);
769e8dfc305SWu Fengguang 		if (list_empty(&wb->b_io))
770ad4e38ddSCurt Wohlgemuth 			queue_io(wb, work);
77183ba7b07SChristoph Hellwig 		if (work->sb)
772d46db3d5SWu Fengguang 			progress = writeback_sb_inodes(work->sb, wb, work);
773edadfb10SChristoph Hellwig 		else
774d46db3d5SWu Fengguang 			progress = __writeback_inodes_wb(wb, work);
775d46db3d5SWu Fengguang 		trace_writeback_written(wb->bdi, work);
776028c2dd1SDave Chinner 
777e98be2d5SWu Fengguang 		wb_update_bandwidth(wb, wb_start);
77803ba3782SJens Axboe 
77903ba3782SJens Axboe 		/*
78071fd05a8SJens Axboe 		 * Did we write something? Try for more
781e6fb6da2SWu Fengguang 		 *
782e6fb6da2SWu Fengguang 		 * Dirty inodes are moved to b_io for writeback in batches.
783e6fb6da2SWu Fengguang 		 * The completion of the current batch does not necessarily
784e6fb6da2SWu Fengguang 		 * mean the overall work is done. So we keep looping as long
785e6fb6da2SWu Fengguang 		 * as made some progress on cleaning pages or inodes.
78671fd05a8SJens Axboe 		 */
787d46db3d5SWu Fengguang 		if (progress)
78803ba3782SJens Axboe 			continue;
789a5989bdcSJan Kara 		/*
790e6fb6da2SWu Fengguang 		 * No more inodes for IO, bail
791a5989bdcSJan Kara 		 */
792b7a2441fSWu Fengguang 		if (list_empty(&wb->b_more_io))
79303ba3782SJens Axboe 			break;
79403ba3782SJens Axboe 		/*
7958010c3b6SJens Axboe 		 * Nothing written. Wait for some inode to
7968010c3b6SJens Axboe 		 * become available for writeback. Otherwise
7978010c3b6SJens Axboe 		 * we'll just busyloop.
79803ba3782SJens Axboe 		 */
79903ba3782SJens Axboe 		if (!list_empty(&wb->b_more_io))  {
800d46db3d5SWu Fengguang 			trace_writeback_wait(wb->bdi, work);
80103ba3782SJens Axboe 			inode = wb_inode(wb->b_more_io.prev);
802250df6edSDave Chinner 			spin_lock(&inode->i_lock);
803f0d07b7fSJan Kara 			spin_unlock(&wb->list_lock);
804f0d07b7fSJan Kara 			inode_wait_for_writeback(inode);
805250df6edSDave Chinner 			spin_unlock(&inode->i_lock);
806f0d07b7fSJan Kara 			spin_lock(&wb->list_lock);
80703ba3782SJens Axboe 		}
80803ba3782SJens Axboe 	}
809e8dfc305SWu Fengguang 	spin_unlock(&wb->list_lock);
81003ba3782SJens Axboe 
811d46db3d5SWu Fengguang 	return nr_pages - work->nr_pages;
81203ba3782SJens Axboe }
81303ba3782SJens Axboe 
81403ba3782SJens Axboe /*
81583ba7b07SChristoph Hellwig  * Return the next wb_writeback_work struct that hasn't been processed yet.
81603ba3782SJens Axboe  */
81783ba7b07SChristoph Hellwig static struct wb_writeback_work *
81808852b6dSMinchan Kim get_next_work_item(struct backing_dev_info *bdi)
81903ba3782SJens Axboe {
82083ba7b07SChristoph Hellwig 	struct wb_writeback_work *work = NULL;
82103ba3782SJens Axboe 
8226467716aSArtem Bityutskiy 	spin_lock_bh(&bdi->wb_lock);
82383ba7b07SChristoph Hellwig 	if (!list_empty(&bdi->work_list)) {
82483ba7b07SChristoph Hellwig 		work = list_entry(bdi->work_list.next,
82583ba7b07SChristoph Hellwig 				  struct wb_writeback_work, list);
82683ba7b07SChristoph Hellwig 		list_del_init(&work->list);
82703ba3782SJens Axboe 	}
8286467716aSArtem Bityutskiy 	spin_unlock_bh(&bdi->wb_lock);
82983ba7b07SChristoph Hellwig 	return work;
83003ba3782SJens Axboe }
83103ba3782SJens Axboe 
832cdf01dd5SLinus Torvalds /*
833cdf01dd5SLinus Torvalds  * Add in the number of potentially dirty inodes, because each inode
834cdf01dd5SLinus Torvalds  * write can dirty pagecache in the underlying blockdev.
835cdf01dd5SLinus Torvalds  */
836cdf01dd5SLinus Torvalds static unsigned long get_nr_dirty_pages(void)
837cdf01dd5SLinus Torvalds {
838cdf01dd5SLinus Torvalds 	return global_page_state(NR_FILE_DIRTY) +
839cdf01dd5SLinus Torvalds 		global_page_state(NR_UNSTABLE_NFS) +
840cdf01dd5SLinus Torvalds 		get_nr_dirty_inodes();
841cdf01dd5SLinus Torvalds }
842cdf01dd5SLinus Torvalds 
8436585027aSJan Kara static long wb_check_background_flush(struct bdi_writeback *wb)
8446585027aSJan Kara {
845b00949aaSWu Fengguang 	if (over_bground_thresh(wb->bdi)) {
8466585027aSJan Kara 
8476585027aSJan Kara 		struct wb_writeback_work work = {
8486585027aSJan Kara 			.nr_pages	= LONG_MAX,
8496585027aSJan Kara 			.sync_mode	= WB_SYNC_NONE,
8506585027aSJan Kara 			.for_background	= 1,
8516585027aSJan Kara 			.range_cyclic	= 1,
8520e175a18SCurt Wohlgemuth 			.reason		= WB_REASON_BACKGROUND,
8536585027aSJan Kara 		};
8546585027aSJan Kara 
8556585027aSJan Kara 		return wb_writeback(wb, &work);
8566585027aSJan Kara 	}
8576585027aSJan Kara 
8586585027aSJan Kara 	return 0;
8596585027aSJan Kara }
8606585027aSJan Kara 
86103ba3782SJens Axboe static long wb_check_old_data_flush(struct bdi_writeback *wb)
86203ba3782SJens Axboe {
86303ba3782SJens Axboe 	unsigned long expired;
86403ba3782SJens Axboe 	long nr_pages;
86503ba3782SJens Axboe 
86669b62d01SJens Axboe 	/*
86769b62d01SJens Axboe 	 * When set to zero, disable periodic writeback
86869b62d01SJens Axboe 	 */
86969b62d01SJens Axboe 	if (!dirty_writeback_interval)
87069b62d01SJens Axboe 		return 0;
87169b62d01SJens Axboe 
87203ba3782SJens Axboe 	expired = wb->last_old_flush +
87303ba3782SJens Axboe 			msecs_to_jiffies(dirty_writeback_interval * 10);
87403ba3782SJens Axboe 	if (time_before(jiffies, expired))
87503ba3782SJens Axboe 		return 0;
87603ba3782SJens Axboe 
87703ba3782SJens Axboe 	wb->last_old_flush = jiffies;
878cdf01dd5SLinus Torvalds 	nr_pages = get_nr_dirty_pages();
87903ba3782SJens Axboe 
880c4a77a6cSJens Axboe 	if (nr_pages) {
88183ba7b07SChristoph Hellwig 		struct wb_writeback_work work = {
882c4a77a6cSJens Axboe 			.nr_pages	= nr_pages,
883c4a77a6cSJens Axboe 			.sync_mode	= WB_SYNC_NONE,
884c4a77a6cSJens Axboe 			.for_kupdate	= 1,
885c4a77a6cSJens Axboe 			.range_cyclic	= 1,
8860e175a18SCurt Wohlgemuth 			.reason		= WB_REASON_PERIODIC,
887c4a77a6cSJens Axboe 		};
888c4a77a6cSJens Axboe 
88983ba7b07SChristoph Hellwig 		return wb_writeback(wb, &work);
890c4a77a6cSJens Axboe 	}
89103ba3782SJens Axboe 
89203ba3782SJens Axboe 	return 0;
89303ba3782SJens Axboe }
89403ba3782SJens Axboe 
89503ba3782SJens Axboe /*
89603ba3782SJens Axboe  * Retrieve work items and do the writeback they describe
89703ba3782SJens Axboe  */
89803ba3782SJens Axboe long wb_do_writeback(struct bdi_writeback *wb, int force_wait)
89903ba3782SJens Axboe {
90003ba3782SJens Axboe 	struct backing_dev_info *bdi = wb->bdi;
90183ba7b07SChristoph Hellwig 	struct wb_writeback_work *work;
902c4a77a6cSJens Axboe 	long wrote = 0;
90303ba3782SJens Axboe 
90481d73a32SJan Kara 	set_bit(BDI_writeback_running, &wb->bdi->state);
90508852b6dSMinchan Kim 	while ((work = get_next_work_item(bdi)) != NULL) {
90603ba3782SJens Axboe 		/*
90703ba3782SJens Axboe 		 * Override sync mode, in case we must wait for completion
90883ba7b07SChristoph Hellwig 		 * because this thread is exiting now.
90903ba3782SJens Axboe 		 */
91003ba3782SJens Axboe 		if (force_wait)
91183ba7b07SChristoph Hellwig 			work->sync_mode = WB_SYNC_ALL;
91283ba7b07SChristoph Hellwig 
913455b2864SDave Chinner 		trace_writeback_exec(bdi, work);
914455b2864SDave Chinner 
91583ba7b07SChristoph Hellwig 		wrote += wb_writeback(wb, work);
91603ba3782SJens Axboe 
91703ba3782SJens Axboe 		/*
91883ba7b07SChristoph Hellwig 		 * Notify the caller of completion if this is a synchronous
91983ba7b07SChristoph Hellwig 		 * work item, otherwise just free it.
92003ba3782SJens Axboe 		 */
92183ba7b07SChristoph Hellwig 		if (work->done)
92283ba7b07SChristoph Hellwig 			complete(work->done);
92383ba7b07SChristoph Hellwig 		else
92483ba7b07SChristoph Hellwig 			kfree(work);
92503ba3782SJens Axboe 	}
92603ba3782SJens Axboe 
92703ba3782SJens Axboe 	/*
92803ba3782SJens Axboe 	 * Check for periodic writeback, kupdated() style
92903ba3782SJens Axboe 	 */
93003ba3782SJens Axboe 	wrote += wb_check_old_data_flush(wb);
9316585027aSJan Kara 	wrote += wb_check_background_flush(wb);
93281d73a32SJan Kara 	clear_bit(BDI_writeback_running, &wb->bdi->state);
93303ba3782SJens Axboe 
93403ba3782SJens Axboe 	return wrote;
93503ba3782SJens Axboe }
93603ba3782SJens Axboe 
93703ba3782SJens Axboe /*
93803ba3782SJens Axboe  * Handle writeback of dirty data for the device backed by this bdi. Also
93903ba3782SJens Axboe  * wakes up periodically and does kupdated style flushing.
94003ba3782SJens Axboe  */
94108243900SChristoph Hellwig int bdi_writeback_thread(void *data)
94203ba3782SJens Axboe {
94308243900SChristoph Hellwig 	struct bdi_writeback *wb = data;
94408243900SChristoph Hellwig 	struct backing_dev_info *bdi = wb->bdi;
94503ba3782SJens Axboe 	long pages_written;
94603ba3782SJens Axboe 
947766f9164SPeter Zijlstra 	current->flags |= PF_SWAPWRITE;
94808243900SChristoph Hellwig 	set_freezable();
949ecd58403SArtem Bityutskiy 	wb->last_active = jiffies;
95003ba3782SJens Axboe 
95103ba3782SJens Axboe 	/*
95208243900SChristoph Hellwig 	 * Our parent may run at a different priority, just set us to normal
95303ba3782SJens Axboe 	 */
95408243900SChristoph Hellwig 	set_user_nice(current, 0);
95508243900SChristoph Hellwig 
956455b2864SDave Chinner 	trace_writeback_thread_start(bdi);
957455b2864SDave Chinner 
9588a32c441STejun Heo 	while (!kthread_freezable_should_stop(NULL)) {
9596467716aSArtem Bityutskiy 		/*
9606467716aSArtem Bityutskiy 		 * Remove own delayed wake-up timer, since we are already awake
9616467716aSArtem Bityutskiy 		 * and we'll take care of the preriodic write-back.
9626467716aSArtem Bityutskiy 		 */
9636467716aSArtem Bityutskiy 		del_timer(&wb->wakeup_timer);
9646467716aSArtem Bityutskiy 
96503ba3782SJens Axboe 		pages_written = wb_do_writeback(wb, 0);
96603ba3782SJens Axboe 
967455b2864SDave Chinner 		trace_writeback_pages_written(pages_written);
968455b2864SDave Chinner 
96903ba3782SJens Axboe 		if (pages_written)
970ecd58403SArtem Bityutskiy 			wb->last_active = jiffies;
97103ba3782SJens Axboe 
972297252c8SArtem Bityutskiy 		set_current_state(TASK_INTERRUPTIBLE);
973b76b4014SJ. Bruce Fields 		if (!list_empty(&bdi->work_list) || kthread_should_stop()) {
974297252c8SArtem Bityutskiy 			__set_current_state(TASK_RUNNING);
975297252c8SArtem Bityutskiy 			continue;
97603ba3782SJens Axboe 		}
97703ba3782SJens Axboe 
978253c34e9SArtem Bityutskiy 		if (wb_has_dirty_io(wb) && dirty_writeback_interval)
979fff5b85aSArtem Bityutskiy 			schedule_timeout(msecs_to_jiffies(dirty_writeback_interval * 10));
980253c34e9SArtem Bityutskiy 		else {
981253c34e9SArtem Bityutskiy 			/*
982253c34e9SArtem Bityutskiy 			 * We have nothing to do, so can go sleep without any
983253c34e9SArtem Bityutskiy 			 * timeout and save power. When a work is queued or
984253c34e9SArtem Bityutskiy 			 * something is made dirty - we will be woken up.
985253c34e9SArtem Bityutskiy 			 */
98669b62d01SJens Axboe 			schedule();
987f9eadbbdSJens Axboe 		}
98803ba3782SJens Axboe 	}
98903ba3782SJens Axboe 
990fff5b85aSArtem Bityutskiy 	/* Flush any work that raced with us exiting */
99108243900SChristoph Hellwig 	if (!list_empty(&bdi->work_list))
99208243900SChristoph Hellwig 		wb_do_writeback(wb, 1);
993455b2864SDave Chinner 
994455b2864SDave Chinner 	trace_writeback_thread_stop(bdi);
99503ba3782SJens Axboe 	return 0;
99603ba3782SJens Axboe }
99703ba3782SJens Axboe 
99808243900SChristoph Hellwig 
99903ba3782SJens Axboe /*
100003ba3782SJens Axboe  * Start writeback of `nr_pages' pages.  If `nr_pages' is zero, write back
100103ba3782SJens Axboe  * the whole world.
100203ba3782SJens Axboe  */
10030e175a18SCurt Wohlgemuth void wakeup_flusher_threads(long nr_pages, enum wb_reason reason)
100403ba3782SJens Axboe {
1005b8c2f347SChristoph Hellwig 	struct backing_dev_info *bdi;
1006b8c2f347SChristoph Hellwig 
100783ba7b07SChristoph Hellwig 	if (!nr_pages) {
100883ba7b07SChristoph Hellwig 		nr_pages = global_page_state(NR_FILE_DIRTY) +
100903ba3782SJens Axboe 				global_page_state(NR_UNSTABLE_NFS);
1010b8c2f347SChristoph Hellwig 	}
1011b8c2f347SChristoph Hellwig 
1012b8c2f347SChristoph Hellwig 	rcu_read_lock();
1013b8c2f347SChristoph Hellwig 	list_for_each_entry_rcu(bdi, &bdi_list, bdi_list) {
1014b8c2f347SChristoph Hellwig 		if (!bdi_has_dirty_io(bdi))
1015b8c2f347SChristoph Hellwig 			continue;
10160e175a18SCurt Wohlgemuth 		__bdi_start_writeback(bdi, nr_pages, false, reason);
1017b8c2f347SChristoph Hellwig 	}
1018b8c2f347SChristoph Hellwig 	rcu_read_unlock();
101903ba3782SJens Axboe }
102003ba3782SJens Axboe 
102103ba3782SJens Axboe static noinline void block_dump___mark_inode_dirty(struct inode *inode)
102203ba3782SJens Axboe {
102303ba3782SJens Axboe 	if (inode->i_ino || strcmp(inode->i_sb->s_id, "bdev")) {
102403ba3782SJens Axboe 		struct dentry *dentry;
102503ba3782SJens Axboe 		const char *name = "?";
102603ba3782SJens Axboe 
102703ba3782SJens Axboe 		dentry = d_find_alias(inode);
102803ba3782SJens Axboe 		if (dentry) {
102903ba3782SJens Axboe 			spin_lock(&dentry->d_lock);
103003ba3782SJens Axboe 			name = (const char *) dentry->d_name.name;
103103ba3782SJens Axboe 		}
103203ba3782SJens Axboe 		printk(KERN_DEBUG
103303ba3782SJens Axboe 		       "%s(%d): dirtied inode %lu (%s) on %s\n",
103403ba3782SJens Axboe 		       current->comm, task_pid_nr(current), inode->i_ino,
103503ba3782SJens Axboe 		       name, inode->i_sb->s_id);
103603ba3782SJens Axboe 		if (dentry) {
103703ba3782SJens Axboe 			spin_unlock(&dentry->d_lock);
103803ba3782SJens Axboe 			dput(dentry);
103903ba3782SJens Axboe 		}
104003ba3782SJens Axboe 	}
104103ba3782SJens Axboe }
104203ba3782SJens Axboe 
104303ba3782SJens Axboe /**
104403ba3782SJens Axboe  *	__mark_inode_dirty -	internal function
104503ba3782SJens Axboe  *	@inode: inode to mark
104603ba3782SJens Axboe  *	@flags: what kind of dirty (i.e. I_DIRTY_SYNC)
104703ba3782SJens Axboe  *	Mark an inode as dirty. Callers should use mark_inode_dirty or
104803ba3782SJens Axboe  *  	mark_inode_dirty_sync.
104903ba3782SJens Axboe  *
105003ba3782SJens Axboe  * Put the inode on the super block's dirty list.
105103ba3782SJens Axboe  *
105203ba3782SJens Axboe  * CAREFUL! We mark it dirty unconditionally, but move it onto the
105303ba3782SJens Axboe  * dirty list only if it is hashed or if it refers to a blockdev.
105403ba3782SJens Axboe  * If it was not hashed, it will never be added to the dirty list
105503ba3782SJens Axboe  * even if it is later hashed, as it will have been marked dirty already.
105603ba3782SJens Axboe  *
105703ba3782SJens Axboe  * In short, make sure you hash any inodes _before_ you start marking
105803ba3782SJens Axboe  * them dirty.
105903ba3782SJens Axboe  *
106003ba3782SJens Axboe  * Note that for blockdevs, inode->dirtied_when represents the dirtying time of
106103ba3782SJens Axboe  * the block-special inode (/dev/hda1) itself.  And the ->dirtied_when field of
106203ba3782SJens Axboe  * the kernel-internal blockdev inode represents the dirtying time of the
106303ba3782SJens Axboe  * blockdev's pages.  This is why for I_DIRTY_PAGES we always use
106403ba3782SJens Axboe  * page->mapping->host, so the page-dirtying time is recorded in the internal
106503ba3782SJens Axboe  * blockdev inode.
106603ba3782SJens Axboe  */
106703ba3782SJens Axboe void __mark_inode_dirty(struct inode *inode, int flags)
106803ba3782SJens Axboe {
106903ba3782SJens Axboe 	struct super_block *sb = inode->i_sb;
1070253c34e9SArtem Bityutskiy 	struct backing_dev_info *bdi = NULL;
107103ba3782SJens Axboe 
107203ba3782SJens Axboe 	/*
107303ba3782SJens Axboe 	 * Don't do this for I_DIRTY_PAGES - that doesn't actually
107403ba3782SJens Axboe 	 * dirty the inode itself
107503ba3782SJens Axboe 	 */
107603ba3782SJens Axboe 	if (flags & (I_DIRTY_SYNC | I_DIRTY_DATASYNC)) {
107703ba3782SJens Axboe 		if (sb->s_op->dirty_inode)
1078aa385729SChristoph Hellwig 			sb->s_op->dirty_inode(inode, flags);
107903ba3782SJens Axboe 	}
108003ba3782SJens Axboe 
108103ba3782SJens Axboe 	/*
108203ba3782SJens Axboe 	 * make sure that changes are seen by all cpus before we test i_state
108303ba3782SJens Axboe 	 * -- mikulas
108403ba3782SJens Axboe 	 */
108503ba3782SJens Axboe 	smp_mb();
108603ba3782SJens Axboe 
108703ba3782SJens Axboe 	/* avoid the locking if we can */
108803ba3782SJens Axboe 	if ((inode->i_state & flags) == flags)
108903ba3782SJens Axboe 		return;
109003ba3782SJens Axboe 
109103ba3782SJens Axboe 	if (unlikely(block_dump))
109203ba3782SJens Axboe 		block_dump___mark_inode_dirty(inode);
109303ba3782SJens Axboe 
1094250df6edSDave Chinner 	spin_lock(&inode->i_lock);
109503ba3782SJens Axboe 	if ((inode->i_state & flags) != flags) {
109603ba3782SJens Axboe 		const int was_dirty = inode->i_state & I_DIRTY;
109703ba3782SJens Axboe 
109803ba3782SJens Axboe 		inode->i_state |= flags;
109903ba3782SJens Axboe 
110003ba3782SJens Axboe 		/*
110103ba3782SJens Axboe 		 * If the inode is being synced, just update its dirty state.
110203ba3782SJens Axboe 		 * The unlocker will place the inode on the appropriate
110303ba3782SJens Axboe 		 * superblock list, based upon its state.
110403ba3782SJens Axboe 		 */
110503ba3782SJens Axboe 		if (inode->i_state & I_SYNC)
1106250df6edSDave Chinner 			goto out_unlock_inode;
110703ba3782SJens Axboe 
110803ba3782SJens Axboe 		/*
110903ba3782SJens Axboe 		 * Only add valid (hashed) inodes to the superblock's
111003ba3782SJens Axboe 		 * dirty list.  Add blockdev inodes as well.
111103ba3782SJens Axboe 		 */
111203ba3782SJens Axboe 		if (!S_ISBLK(inode->i_mode)) {
11131d3382cbSAl Viro 			if (inode_unhashed(inode))
1114250df6edSDave Chinner 				goto out_unlock_inode;
111503ba3782SJens Axboe 		}
1116a4ffdde6SAl Viro 		if (inode->i_state & I_FREEING)
1117250df6edSDave Chinner 			goto out_unlock_inode;
111803ba3782SJens Axboe 
111903ba3782SJens Axboe 		/*
112003ba3782SJens Axboe 		 * If the inode was already on b_dirty/b_io/b_more_io, don't
112103ba3782SJens Axboe 		 * reposition it (that would break b_dirty time-ordering).
112203ba3782SJens Axboe 		 */
112303ba3782SJens Axboe 		if (!was_dirty) {
1124a66979abSDave Chinner 			bool wakeup_bdi = false;
1125253c34e9SArtem Bityutskiy 			bdi = inode_to_bdi(inode);
1126500b067cSJens Axboe 
1127253c34e9SArtem Bityutskiy 			if (bdi_cap_writeback_dirty(bdi)) {
1128253c34e9SArtem Bityutskiy 				WARN(!test_bit(BDI_registered, &bdi->state),
1129253c34e9SArtem Bityutskiy 				     "bdi-%s not registered\n", bdi->name);
1130253c34e9SArtem Bityutskiy 
1131253c34e9SArtem Bityutskiy 				/*
1132253c34e9SArtem Bityutskiy 				 * If this is the first dirty inode for this
1133253c34e9SArtem Bityutskiy 				 * bdi, we have to wake-up the corresponding
1134253c34e9SArtem Bityutskiy 				 * bdi thread to make sure background
1135253c34e9SArtem Bityutskiy 				 * write-back happens later.
1136253c34e9SArtem Bityutskiy 				 */
1137253c34e9SArtem Bityutskiy 				if (!wb_has_dirty_io(&bdi->wb))
1138253c34e9SArtem Bityutskiy 					wakeup_bdi = true;
1139500b067cSJens Axboe 			}
114003ba3782SJens Axboe 
1141a66979abSDave Chinner 			spin_unlock(&inode->i_lock);
1142f758eeabSChristoph Hellwig 			spin_lock(&bdi->wb.list_lock);
114303ba3782SJens Axboe 			inode->dirtied_when = jiffies;
11447ccf19a8SNick Piggin 			list_move(&inode->i_wb_list, &bdi->wb.b_dirty);
1145f758eeabSChristoph Hellwig 			spin_unlock(&bdi->wb.list_lock);
1146253c34e9SArtem Bityutskiy 
1147253c34e9SArtem Bityutskiy 			if (wakeup_bdi)
11486467716aSArtem Bityutskiy 				bdi_wakeup_thread_delayed(bdi);
1149a66979abSDave Chinner 			return;
1150a66979abSDave Chinner 		}
1151a66979abSDave Chinner 	}
1152a66979abSDave Chinner out_unlock_inode:
1153a66979abSDave Chinner 	spin_unlock(&inode->i_lock);
1154a66979abSDave Chinner 
115503ba3782SJens Axboe }
115603ba3782SJens Axboe EXPORT_SYMBOL(__mark_inode_dirty);
115703ba3782SJens Axboe 
1158b6e51316SJens Axboe static void wait_sb_inodes(struct super_block *sb)
115966f3b8e2SJens Axboe {
116038f21977SNick Piggin 	struct inode *inode, *old_inode = NULL;
116138f21977SNick Piggin 
116203ba3782SJens Axboe 	/*
116303ba3782SJens Axboe 	 * We need to be protected against the filesystem going from
116403ba3782SJens Axboe 	 * r/o to r/w or vice versa.
116503ba3782SJens Axboe 	 */
1166b6e51316SJens Axboe 	WARN_ON(!rwsem_is_locked(&sb->s_umount));
116703ba3782SJens Axboe 
116855fa6091SDave Chinner 	spin_lock(&inode_sb_list_lock);
116966f3b8e2SJens Axboe 
117038f21977SNick Piggin 	/*
117138f21977SNick Piggin 	 * Data integrity sync. Must wait for all pages under writeback,
117238f21977SNick Piggin 	 * because there may have been pages dirtied before our sync
117338f21977SNick Piggin 	 * call, but which had writeout started before we write it out.
117438f21977SNick Piggin 	 * In which case, the inode may not be on the dirty list, but
117538f21977SNick Piggin 	 * we still have to wait for that writeout.
117638f21977SNick Piggin 	 */
1177b6e51316SJens Axboe 	list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
1178250df6edSDave Chinner 		struct address_space *mapping = inode->i_mapping;
117938f21977SNick Piggin 
1180250df6edSDave Chinner 		spin_lock(&inode->i_lock);
1181250df6edSDave Chinner 		if ((inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW)) ||
1182250df6edSDave Chinner 		    (mapping->nrpages == 0)) {
1183250df6edSDave Chinner 			spin_unlock(&inode->i_lock);
118438f21977SNick Piggin 			continue;
1185250df6edSDave Chinner 		}
118638f21977SNick Piggin 		__iget(inode);
1187250df6edSDave Chinner 		spin_unlock(&inode->i_lock);
118855fa6091SDave Chinner 		spin_unlock(&inode_sb_list_lock);
118955fa6091SDave Chinner 
119038f21977SNick Piggin 		/*
119155fa6091SDave Chinner 		 * We hold a reference to 'inode' so it couldn't have been
119255fa6091SDave Chinner 		 * removed from s_inodes list while we dropped the
119355fa6091SDave Chinner 		 * inode_sb_list_lock.  We cannot iput the inode now as we can
119455fa6091SDave Chinner 		 * be holding the last reference and we cannot iput it under
119555fa6091SDave Chinner 		 * inode_sb_list_lock. So we keep the reference and iput it
119655fa6091SDave Chinner 		 * later.
119738f21977SNick Piggin 		 */
119838f21977SNick Piggin 		iput(old_inode);
119938f21977SNick Piggin 		old_inode = inode;
120038f21977SNick Piggin 
120138f21977SNick Piggin 		filemap_fdatawait(mapping);
120238f21977SNick Piggin 
120338f21977SNick Piggin 		cond_resched();
120438f21977SNick Piggin 
120555fa6091SDave Chinner 		spin_lock(&inode_sb_list_lock);
120638f21977SNick Piggin 	}
120755fa6091SDave Chinner 	spin_unlock(&inode_sb_list_lock);
120838f21977SNick Piggin 	iput(old_inode);
120966f3b8e2SJens Axboe }
12101da177e4SLinus Torvalds 
1211d8a8559cSJens Axboe /**
12123259f8beSChris Mason  * writeback_inodes_sb_nr -	writeback dirty inodes from given super_block
1213d8a8559cSJens Axboe  * @sb: the superblock
12143259f8beSChris Mason  * @nr: the number of pages to write
1215786228abSMarcos Paulo de Souza  * @reason: reason why some writeback work initiated
12161da177e4SLinus Torvalds  *
1217d8a8559cSJens Axboe  * Start writeback on some inodes on this super_block. No guarantees are made
1218d8a8559cSJens Axboe  * on how many (if any) will be written, and this function does not wait
12193259f8beSChris Mason  * for IO completion of submitted IO.
12201da177e4SLinus Torvalds  */
12210e175a18SCurt Wohlgemuth void writeback_inodes_sb_nr(struct super_block *sb,
12220e175a18SCurt Wohlgemuth 			    unsigned long nr,
12230e175a18SCurt Wohlgemuth 			    enum wb_reason reason)
12241da177e4SLinus Torvalds {
122583ba7b07SChristoph Hellwig 	DECLARE_COMPLETION_ONSTACK(done);
122683ba7b07SChristoph Hellwig 	struct wb_writeback_work work = {
12273c4d7165SChristoph Hellwig 		.sb			= sb,
12283c4d7165SChristoph Hellwig 		.sync_mode		= WB_SYNC_NONE,
12296e6938b6SWu Fengguang 		.tagged_writepages	= 1,
123083ba7b07SChristoph Hellwig 		.done			= &done,
12313259f8beSChris Mason 		.nr_pages		= nr,
12320e175a18SCurt Wohlgemuth 		.reason			= reason,
12333c4d7165SChristoph Hellwig 	};
12340e3c9a22SJens Axboe 
1235cf37e972SChristoph Hellwig 	WARN_ON(!rwsem_is_locked(&sb->s_umount));
123683ba7b07SChristoph Hellwig 	bdi_queue_work(sb->s_bdi, &work);
123783ba7b07SChristoph Hellwig 	wait_for_completion(&done);
12381da177e4SLinus Torvalds }
12393259f8beSChris Mason EXPORT_SYMBOL(writeback_inodes_sb_nr);
12403259f8beSChris Mason 
12413259f8beSChris Mason /**
12423259f8beSChris Mason  * writeback_inodes_sb	-	writeback dirty inodes from given super_block
12433259f8beSChris Mason  * @sb: the superblock
1244786228abSMarcos Paulo de Souza  * @reason: reason why some writeback work was initiated
12453259f8beSChris Mason  *
12463259f8beSChris Mason  * Start writeback on some inodes on this super_block. No guarantees are made
12473259f8beSChris Mason  * on how many (if any) will be written, and this function does not wait
12483259f8beSChris Mason  * for IO completion of submitted IO.
12493259f8beSChris Mason  */
12500e175a18SCurt Wohlgemuth void writeback_inodes_sb(struct super_block *sb, enum wb_reason reason)
12513259f8beSChris Mason {
12520e175a18SCurt Wohlgemuth 	return writeback_inodes_sb_nr(sb, get_nr_dirty_pages(), reason);
12533259f8beSChris Mason }
1254d8a8559cSJens Axboe EXPORT_SYMBOL(writeback_inodes_sb);
1255d8a8559cSJens Axboe 
1256d8a8559cSJens Axboe /**
125717bd55d0SEric Sandeen  * writeback_inodes_sb_if_idle	-	start writeback if none underway
125817bd55d0SEric Sandeen  * @sb: the superblock
1259786228abSMarcos Paulo de Souza  * @reason: reason why some writeback work was initiated
126017bd55d0SEric Sandeen  *
126117bd55d0SEric Sandeen  * Invoke writeback_inodes_sb if no writeback is currently underway.
126217bd55d0SEric Sandeen  * Returns 1 if writeback was started, 0 if not.
126317bd55d0SEric Sandeen  */
12640e175a18SCurt Wohlgemuth int writeback_inodes_sb_if_idle(struct super_block *sb, enum wb_reason reason)
126517bd55d0SEric Sandeen {
126617bd55d0SEric Sandeen 	if (!writeback_in_progress(sb->s_bdi)) {
1267cf37e972SChristoph Hellwig 		down_read(&sb->s_umount);
12680e175a18SCurt Wohlgemuth 		writeback_inodes_sb(sb, reason);
1269cf37e972SChristoph Hellwig 		up_read(&sb->s_umount);
127017bd55d0SEric Sandeen 		return 1;
127117bd55d0SEric Sandeen 	} else
127217bd55d0SEric Sandeen 		return 0;
127317bd55d0SEric Sandeen }
127417bd55d0SEric Sandeen EXPORT_SYMBOL(writeback_inodes_sb_if_idle);
127517bd55d0SEric Sandeen 
127617bd55d0SEric Sandeen /**
1277c097b2caSFengguang Wu  * writeback_inodes_sb_nr_if_idle	-	start writeback if none underway
12783259f8beSChris Mason  * @sb: the superblock
12793259f8beSChris Mason  * @nr: the number of pages to write
1280786228abSMarcos Paulo de Souza  * @reason: reason why some writeback work was initiated
12813259f8beSChris Mason  *
12823259f8beSChris Mason  * Invoke writeback_inodes_sb if no writeback is currently underway.
12833259f8beSChris Mason  * Returns 1 if writeback was started, 0 if not.
12843259f8beSChris Mason  */
12853259f8beSChris Mason int writeback_inodes_sb_nr_if_idle(struct super_block *sb,
12860e175a18SCurt Wohlgemuth 				   unsigned long nr,
12870e175a18SCurt Wohlgemuth 				   enum wb_reason reason)
12883259f8beSChris Mason {
12893259f8beSChris Mason 	if (!writeback_in_progress(sb->s_bdi)) {
12903259f8beSChris Mason 		down_read(&sb->s_umount);
12910e175a18SCurt Wohlgemuth 		writeback_inodes_sb_nr(sb, nr, reason);
12923259f8beSChris Mason 		up_read(&sb->s_umount);
12933259f8beSChris Mason 		return 1;
12943259f8beSChris Mason 	} else
12953259f8beSChris Mason 		return 0;
12963259f8beSChris Mason }
12973259f8beSChris Mason EXPORT_SYMBOL(writeback_inodes_sb_nr_if_idle);
12983259f8beSChris Mason 
12993259f8beSChris Mason /**
1300d8a8559cSJens Axboe  * sync_inodes_sb	-	sync sb inode pages
1301d8a8559cSJens Axboe  * @sb: the superblock
1302d8a8559cSJens Axboe  *
1303d8a8559cSJens Axboe  * This function writes and waits on any dirty inode belonging to this
1304cb9ef8d5SStefan Hajnoczi  * super_block.
1305d8a8559cSJens Axboe  */
1306b6e51316SJens Axboe void sync_inodes_sb(struct super_block *sb)
1307d8a8559cSJens Axboe {
130883ba7b07SChristoph Hellwig 	DECLARE_COMPLETION_ONSTACK(done);
130983ba7b07SChristoph Hellwig 	struct wb_writeback_work work = {
13103c4d7165SChristoph Hellwig 		.sb		= sb,
13113c4d7165SChristoph Hellwig 		.sync_mode	= WB_SYNC_ALL,
13123c4d7165SChristoph Hellwig 		.nr_pages	= LONG_MAX,
13133c4d7165SChristoph Hellwig 		.range_cyclic	= 0,
131483ba7b07SChristoph Hellwig 		.done		= &done,
13150e175a18SCurt Wohlgemuth 		.reason		= WB_REASON_SYNC,
13163c4d7165SChristoph Hellwig 	};
13173c4d7165SChristoph Hellwig 
1318cf37e972SChristoph Hellwig 	WARN_ON(!rwsem_is_locked(&sb->s_umount));
1319cf37e972SChristoph Hellwig 
132083ba7b07SChristoph Hellwig 	bdi_queue_work(sb->s_bdi, &work);
132183ba7b07SChristoph Hellwig 	wait_for_completion(&done);
132283ba7b07SChristoph Hellwig 
1323b6e51316SJens Axboe 	wait_sb_inodes(sb);
1324d8a8559cSJens Axboe }
1325d8a8559cSJens Axboe EXPORT_SYMBOL(sync_inodes_sb);
13261da177e4SLinus Torvalds 
13271da177e4SLinus Torvalds /**
13281da177e4SLinus Torvalds  * write_inode_now	-	write an inode to disk
13291da177e4SLinus Torvalds  * @inode: inode to write to disk
13301da177e4SLinus Torvalds  * @sync: whether the write should be synchronous or not
13311da177e4SLinus Torvalds  *
13327f04c26dSAndrea Arcangeli  * This function commits an inode to disk immediately if it is dirty. This is
13337f04c26dSAndrea Arcangeli  * primarily needed by knfsd.
13347f04c26dSAndrea Arcangeli  *
13357f04c26dSAndrea Arcangeli  * The caller must either have a ref on the inode or must have set I_WILL_FREE.
13361da177e4SLinus Torvalds  */
13371da177e4SLinus Torvalds int write_inode_now(struct inode *inode, int sync)
13381da177e4SLinus Torvalds {
1339f758eeabSChristoph Hellwig 	struct bdi_writeback *wb = &inode_to_bdi(inode)->wb;
13401da177e4SLinus Torvalds 	int ret;
13411da177e4SLinus Torvalds 	struct writeback_control wbc = {
13421da177e4SLinus Torvalds 		.nr_to_write = LONG_MAX,
134318914b18SMike Galbraith 		.sync_mode = sync ? WB_SYNC_ALL : WB_SYNC_NONE,
1344111ebb6eSOGAWA Hirofumi 		.range_start = 0,
1345111ebb6eSOGAWA Hirofumi 		.range_end = LLONG_MAX,
13461da177e4SLinus Torvalds 	};
13471da177e4SLinus Torvalds 
13481da177e4SLinus Torvalds 	if (!mapping_cap_writeback_dirty(inode->i_mapping))
134949364ce2SAndrew Morton 		wbc.nr_to_write = 0;
13501da177e4SLinus Torvalds 
13511da177e4SLinus Torvalds 	might_sleep();
13520f1b1fd8SDave Chinner 	spin_lock(&inode->i_lock);
1353f758eeabSChristoph Hellwig 	ret = writeback_single_inode(inode, wb, &wbc);
13540f1b1fd8SDave Chinner 	spin_unlock(&inode->i_lock);
1355f758eeabSChristoph Hellwig 	spin_unlock(&wb->list_lock);
13561da177e4SLinus Torvalds 	return ret;
13571da177e4SLinus Torvalds }
13581da177e4SLinus Torvalds EXPORT_SYMBOL(write_inode_now);
13591da177e4SLinus Torvalds 
13601da177e4SLinus Torvalds /**
13611da177e4SLinus Torvalds  * sync_inode - write an inode and its pages to disk.
13621da177e4SLinus Torvalds  * @inode: the inode to sync
13631da177e4SLinus Torvalds  * @wbc: controls the writeback mode
13641da177e4SLinus Torvalds  *
13651da177e4SLinus Torvalds  * sync_inode() will write an inode and its pages to disk.  It will also
13661da177e4SLinus Torvalds  * correctly update the inode on its superblock's dirty inode lists and will
13671da177e4SLinus Torvalds  * update inode->i_state.
13681da177e4SLinus Torvalds  *
13691da177e4SLinus Torvalds  * The caller must have a ref on the inode.
13701da177e4SLinus Torvalds  */
13711da177e4SLinus Torvalds int sync_inode(struct inode *inode, struct writeback_control *wbc)
13721da177e4SLinus Torvalds {
1373f758eeabSChristoph Hellwig 	struct bdi_writeback *wb = &inode_to_bdi(inode)->wb;
13741da177e4SLinus Torvalds 	int ret;
13751da177e4SLinus Torvalds 
13760f1b1fd8SDave Chinner 	spin_lock(&inode->i_lock);
1377f758eeabSChristoph Hellwig 	ret = writeback_single_inode(inode, wb, wbc);
13780f1b1fd8SDave Chinner 	spin_unlock(&inode->i_lock);
1379f758eeabSChristoph Hellwig 	spin_unlock(&wb->list_lock);
13801da177e4SLinus Torvalds 	return ret;
13811da177e4SLinus Torvalds }
13821da177e4SLinus Torvalds EXPORT_SYMBOL(sync_inode);
1383c3765016SChristoph Hellwig 
1384c3765016SChristoph Hellwig /**
1385c691b9d9SAndrew Morton  * sync_inode_metadata - write an inode to disk
1386c3765016SChristoph Hellwig  * @inode: the inode to sync
1387c3765016SChristoph Hellwig  * @wait: wait for I/O to complete.
1388c3765016SChristoph Hellwig  *
1389c691b9d9SAndrew Morton  * Write an inode to disk and adjust its dirty state after completion.
1390c3765016SChristoph Hellwig  *
1391c3765016SChristoph Hellwig  * Note: only writes the actual inode, no associated data or other metadata.
1392c3765016SChristoph Hellwig  */
1393c3765016SChristoph Hellwig int sync_inode_metadata(struct inode *inode, int wait)
1394c3765016SChristoph Hellwig {
1395c3765016SChristoph Hellwig 	struct writeback_control wbc = {
1396c3765016SChristoph Hellwig 		.sync_mode = wait ? WB_SYNC_ALL : WB_SYNC_NONE,
1397c3765016SChristoph Hellwig 		.nr_to_write = 0, /* metadata-only */
1398c3765016SChristoph Hellwig 	};
1399c3765016SChristoph Hellwig 
1400c3765016SChristoph Hellwig 	return sync_inode(inode, &wbc);
1401c3765016SChristoph Hellwig }
1402c3765016SChristoph Hellwig EXPORT_SYMBOL(sync_inode_metadata);
1403