xref: /openbmc/linux/fs/fs-writeback.c (revision cb9bd115)
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;
38c4a77a6cSJens Axboe 	enum writeback_sync_modes sync_mode;
396e6938b6SWu Fengguang 	unsigned int tagged_writepages:1;
4052957fe1SH Hartley Sweeten 	unsigned int for_kupdate:1;
4152957fe1SH Hartley Sweeten 	unsigned int range_cyclic:1;
4252957fe1SH Hartley Sweeten 	unsigned int for_background:1;
43c4a77a6cSJens Axboe 
448010c3b6SJens Axboe 	struct list_head list;		/* pending work list */
4583ba7b07SChristoph Hellwig 	struct completion *done;	/* set if the caller waits */
4603ba3782SJens Axboe };
4703ba3782SJens Axboe 
48455b2864SDave Chinner /*
49455b2864SDave Chinner  * Include the creation of the trace points after defining the
50455b2864SDave Chinner  * wb_writeback_work structure so that the definition remains local to this
51455b2864SDave Chinner  * file.
52455b2864SDave Chinner  */
53455b2864SDave Chinner #define CREATE_TRACE_POINTS
54455b2864SDave Chinner #include <trace/events/writeback.h>
55455b2864SDave Chinner 
56455b2864SDave Chinner /*
57455b2864SDave Chinner  * We don't actually have pdflush, but this one is exported though /proc...
58455b2864SDave Chinner  */
59455b2864SDave Chinner int nr_pdflush_threads;
60455b2864SDave Chinner 
61f11b00f3SAdrian Bunk /**
62f11b00f3SAdrian Bunk  * writeback_in_progress - determine whether there is writeback in progress
63f11b00f3SAdrian Bunk  * @bdi: the device's backing_dev_info structure.
64f11b00f3SAdrian Bunk  *
6503ba3782SJens Axboe  * Determine whether there is writeback waiting to be handled against a
6603ba3782SJens Axboe  * backing device.
67f11b00f3SAdrian Bunk  */
68f11b00f3SAdrian Bunk int writeback_in_progress(struct backing_dev_info *bdi)
69f11b00f3SAdrian Bunk {
7081d73a32SJan Kara 	return test_bit(BDI_writeback_running, &bdi->state);
71f11b00f3SAdrian Bunk }
72f11b00f3SAdrian Bunk 
73692ebd17SJan Kara static inline struct backing_dev_info *inode_to_bdi(struct inode *inode)
74692ebd17SJan Kara {
75692ebd17SJan Kara 	struct super_block *sb = inode->i_sb;
76692ebd17SJan Kara 
77aaead25bSChristoph Hellwig 	if (strcmp(sb->s_type->name, "bdev") == 0)
78aaead25bSChristoph Hellwig 		return inode->i_mapping->backing_dev_info;
79aaead25bSChristoph Hellwig 
80692ebd17SJan Kara 	return sb->s_bdi;
81692ebd17SJan Kara }
82692ebd17SJan Kara 
837ccf19a8SNick Piggin static inline struct inode *wb_inode(struct list_head *head)
847ccf19a8SNick Piggin {
857ccf19a8SNick Piggin 	return list_entry(head, struct inode, i_wb_list);
867ccf19a8SNick Piggin }
877ccf19a8SNick Piggin 
886585027aSJan Kara /* Wakeup flusher thread or forker thread to fork it. Requires bdi->wb_lock. */
896585027aSJan Kara static void bdi_wakeup_flusher(struct backing_dev_info *bdi)
904195f73dSNick Piggin {
91fff5b85aSArtem Bityutskiy 	if (bdi->wb.task) {
92fff5b85aSArtem Bityutskiy 		wake_up_process(bdi->wb.task);
93fff5b85aSArtem Bityutskiy 	} else {
941da177e4SLinus Torvalds 		/*
95fff5b85aSArtem Bityutskiy 		 * The bdi thread isn't there, wake up the forker thread which
96fff5b85aSArtem Bityutskiy 		 * will create and run it.
971da177e4SLinus Torvalds 		 */
9803ba3782SJens Axboe 		wake_up_process(default_backing_dev_info.wb.task);
991da177e4SLinus Torvalds 	}
1006585027aSJan Kara }
1016585027aSJan Kara 
1026585027aSJan Kara static void bdi_queue_work(struct backing_dev_info *bdi,
1036585027aSJan Kara 			   struct wb_writeback_work *work)
1046585027aSJan Kara {
1056585027aSJan Kara 	trace_writeback_queue(bdi, work);
1066585027aSJan Kara 
1076585027aSJan Kara 	spin_lock_bh(&bdi->wb_lock);
1086585027aSJan Kara 	list_add_tail(&work->list, &bdi->work_list);
1096585027aSJan Kara 	if (!bdi->wb.task)
1106585027aSJan Kara 		trace_writeback_nothread(bdi, work);
1116585027aSJan Kara 	bdi_wakeup_flusher(bdi);
1126467716aSArtem Bityutskiy 	spin_unlock_bh(&bdi->wb_lock);
11303ba3782SJens Axboe }
1141da177e4SLinus Torvalds 
11583ba7b07SChristoph Hellwig static void
11683ba7b07SChristoph Hellwig __bdi_start_writeback(struct backing_dev_info *bdi, long nr_pages,
1176585027aSJan Kara 		      bool range_cyclic)
1181da177e4SLinus Torvalds {
11983ba7b07SChristoph Hellwig 	struct wb_writeback_work *work;
12003ba3782SJens Axboe 
121bcddc3f0SJens Axboe 	/*
122bcddc3f0SJens Axboe 	 * This is WB_SYNC_NONE writeback, so if allocation fails just
123bcddc3f0SJens Axboe 	 * wakeup the thread for old dirty data writeback
124bcddc3f0SJens Axboe 	 */
12583ba7b07SChristoph Hellwig 	work = kzalloc(sizeof(*work), GFP_ATOMIC);
12683ba7b07SChristoph Hellwig 	if (!work) {
127455b2864SDave Chinner 		if (bdi->wb.task) {
128455b2864SDave Chinner 			trace_writeback_nowork(bdi);
12983ba7b07SChristoph Hellwig 			wake_up_process(bdi->wb.task);
130455b2864SDave Chinner 		}
13183ba7b07SChristoph Hellwig 		return;
13283ba7b07SChristoph Hellwig 	}
13383ba7b07SChristoph Hellwig 
13483ba7b07SChristoph Hellwig 	work->sync_mode	= WB_SYNC_NONE;
13583ba7b07SChristoph Hellwig 	work->nr_pages	= nr_pages;
13683ba7b07SChristoph Hellwig 	work->range_cyclic = range_cyclic;
13783ba7b07SChristoph Hellwig 
138f11fcae8SJens Axboe 	bdi_queue_work(bdi, work);
13903ba3782SJens Axboe }
140b6e51316SJens Axboe 
141b6e51316SJens Axboe /**
142b6e51316SJens Axboe  * bdi_start_writeback - start writeback
143b6e51316SJens Axboe  * @bdi: the backing device to write from
144b6e51316SJens Axboe  * @nr_pages: the number of pages to write
145b6e51316SJens Axboe  *
146b6e51316SJens Axboe  * Description:
147b6e51316SJens Axboe  *   This does WB_SYNC_NONE opportunistic writeback. The IO is only
14825985edcSLucas De Marchi  *   started when this function returns, we make no guarantees on
1490e3c9a22SJens Axboe  *   completion. Caller need not hold sb s_umount semaphore.
150b6e51316SJens Axboe  *
151b6e51316SJens Axboe  */
152c5444198SChristoph Hellwig void bdi_start_writeback(struct backing_dev_info *bdi, long nr_pages)
153b6e51316SJens Axboe {
1546585027aSJan Kara 	__bdi_start_writeback(bdi, nr_pages, true);
155d3ddec76SWu Fengguang }
156d3ddec76SWu Fengguang 
157c5444198SChristoph Hellwig /**
158c5444198SChristoph Hellwig  * bdi_start_background_writeback - start background writeback
159c5444198SChristoph Hellwig  * @bdi: the backing device to write from
160c5444198SChristoph Hellwig  *
161c5444198SChristoph Hellwig  * Description:
1626585027aSJan Kara  *   This makes sure WB_SYNC_NONE background writeback happens. When
1636585027aSJan Kara  *   this function returns, it is only guaranteed that for given BDI
1646585027aSJan Kara  *   some IO is happening if we are over background dirty threshold.
1656585027aSJan Kara  *   Caller need not hold sb s_umount semaphore.
166c5444198SChristoph Hellwig  */
167c5444198SChristoph Hellwig void bdi_start_background_writeback(struct backing_dev_info *bdi)
168c5444198SChristoph Hellwig {
1696585027aSJan Kara 	/*
1706585027aSJan Kara 	 * We just wake up the flusher thread. It will perform background
1716585027aSJan Kara 	 * writeback as soon as there is no other work to do.
1726585027aSJan Kara 	 */
17371927e84SWu Fengguang 	trace_writeback_wake_background(bdi);
1746585027aSJan Kara 	spin_lock_bh(&bdi->wb_lock);
1756585027aSJan Kara 	bdi_wakeup_flusher(bdi);
1766585027aSJan Kara 	spin_unlock_bh(&bdi->wb_lock);
1771da177e4SLinus Torvalds }
1781da177e4SLinus Torvalds 
1791da177e4SLinus Torvalds /*
180a66979abSDave Chinner  * Remove the inode from the writeback list it is on.
181a66979abSDave Chinner  */
182a66979abSDave Chinner void inode_wb_list_del(struct inode *inode)
183a66979abSDave Chinner {
184a66979abSDave Chinner 	spin_lock(&inode_wb_list_lock);
185a66979abSDave Chinner 	list_del_init(&inode->i_wb_list);
186a66979abSDave Chinner 	spin_unlock(&inode_wb_list_lock);
187a66979abSDave Chinner }
188a66979abSDave Chinner 
189a66979abSDave Chinner 
190a66979abSDave Chinner /*
1916610a0bcSAndrew Morton  * Redirty an inode: set its when-it-was dirtied timestamp and move it to the
1926610a0bcSAndrew Morton  * furthest end of its superblock's dirty-inode list.
1936610a0bcSAndrew Morton  *
1946610a0bcSAndrew Morton  * Before stamping the inode's ->dirtied_when, we check to see whether it is
19566f3b8e2SJens Axboe  * already the most-recently-dirtied inode on the b_dirty list.  If that is
1966610a0bcSAndrew Morton  * the case then the inode must have been redirtied while it was being written
1976610a0bcSAndrew Morton  * out and we don't reset its dirtied_when.
1986610a0bcSAndrew Morton  */
1996610a0bcSAndrew Morton static void redirty_tail(struct inode *inode)
2006610a0bcSAndrew Morton {
20103ba3782SJens Axboe 	struct bdi_writeback *wb = &inode_to_bdi(inode)->wb;
2026610a0bcSAndrew Morton 
203a66979abSDave Chinner 	assert_spin_locked(&inode_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  */
2170e0f4fc2SKen Chen static void requeue_io(struct inode *inode)
218c986d1e2SAndrew Morton {
21903ba3782SJens Axboe 	struct bdi_writeback *wb = &inode_to_bdi(inode)->wb;
22003ba3782SJens Axboe 
221a66979abSDave Chinner 	assert_spin_locked(&inode_wb_list_lock);
2227ccf19a8SNick Piggin 	list_move(&inode->i_wb_list, &wb->b_more_io);
223c986d1e2SAndrew Morton }
224c986d1e2SAndrew Morton 
2251c0eeaf5SJoern Engel static void inode_sync_complete(struct inode *inode)
2261c0eeaf5SJoern Engel {
2271c0eeaf5SJoern Engel 	/*
228a66979abSDave Chinner 	 * Prevent speculative execution through
229a66979abSDave Chinner 	 * spin_unlock(&inode_wb_list_lock);
2301c0eeaf5SJoern Engel 	 */
231a66979abSDave Chinner 
2321c0eeaf5SJoern Engel 	smp_mb();
2331c0eeaf5SJoern Engel 	wake_up_bit(&inode->i_state, __I_SYNC);
2341c0eeaf5SJoern Engel }
2351c0eeaf5SJoern Engel 
236d2caa3c5SJeff Layton static bool inode_dirtied_after(struct inode *inode, unsigned long t)
237d2caa3c5SJeff Layton {
238d2caa3c5SJeff Layton 	bool ret = time_after(inode->dirtied_when, t);
239d2caa3c5SJeff Layton #ifndef CONFIG_64BIT
240d2caa3c5SJeff Layton 	/*
241d2caa3c5SJeff Layton 	 * For inodes being constantly redirtied, dirtied_when can get stuck.
242d2caa3c5SJeff Layton 	 * It _appears_ to be in the future, but is actually in distant past.
243d2caa3c5SJeff Layton 	 * This test is necessary to prevent such wrapped-around relative times
2445b0830cbSJens Axboe 	 * from permanently stopping the whole bdi writeback.
245d2caa3c5SJeff Layton 	 */
246d2caa3c5SJeff Layton 	ret = ret && time_before_eq(inode->dirtied_when, jiffies);
247d2caa3c5SJeff Layton #endif
248d2caa3c5SJeff Layton 	return ret;
249d2caa3c5SJeff Layton }
250d2caa3c5SJeff Layton 
251c986d1e2SAndrew Morton /*
2522c136579SFengguang Wu  * Move expired dirty inodes from @delaying_queue to @dispatch_queue.
2532c136579SFengguang Wu  */
2542c136579SFengguang Wu static void move_expired_inodes(struct list_head *delaying_queue,
2552c136579SFengguang Wu 			       struct list_head *dispatch_queue,
2562c136579SFengguang Wu 				unsigned long *older_than_this)
2572c136579SFengguang Wu {
2585c03449dSShaohua Li 	LIST_HEAD(tmp);
2595c03449dSShaohua Li 	struct list_head *pos, *node;
260cf137307SJens Axboe 	struct super_block *sb = NULL;
2615c03449dSShaohua Li 	struct inode *inode;
262cf137307SJens Axboe 	int do_sb_sort = 0;
2635c03449dSShaohua Li 
2642c136579SFengguang Wu 	while (!list_empty(delaying_queue)) {
2657ccf19a8SNick Piggin 		inode = wb_inode(delaying_queue->prev);
2662c136579SFengguang Wu 		if (older_than_this &&
267d2caa3c5SJeff Layton 		    inode_dirtied_after(inode, *older_than_this))
2682c136579SFengguang Wu 			break;
269cf137307SJens Axboe 		if (sb && sb != inode->i_sb)
270cf137307SJens Axboe 			do_sb_sort = 1;
271cf137307SJens Axboe 		sb = inode->i_sb;
2727ccf19a8SNick Piggin 		list_move(&inode->i_wb_list, &tmp);
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);
278cf137307SJens Axboe 		return;
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 	}
2905c03449dSShaohua Li }
2912c136579SFengguang Wu 
2922c136579SFengguang Wu /*
2932c136579SFengguang Wu  * Queue all expired dirty inodes for io, eldest first.
2944ea879b9SWu Fengguang  * Before
2954ea879b9SWu Fengguang  *         newly dirtied     b_dirty    b_io    b_more_io
2964ea879b9SWu Fengguang  *         =============>    gf         edc     BA
2974ea879b9SWu Fengguang  * After
2984ea879b9SWu Fengguang  *         newly dirtied     b_dirty    b_io    b_more_io
2994ea879b9SWu Fengguang  *         =============>    g          fBAedc
3004ea879b9SWu Fengguang  *                                           |
3014ea879b9SWu Fengguang  *                                           +--> dequeue for IO
3022c136579SFengguang Wu  */
30303ba3782SJens Axboe static void queue_io(struct bdi_writeback *wb, unsigned long *older_than_this)
3042c136579SFengguang Wu {
305a66979abSDave Chinner 	assert_spin_locked(&inode_wb_list_lock);
3064ea879b9SWu Fengguang 	list_splice_init(&wb->b_more_io, &wb->b_io);
30703ba3782SJens Axboe 	move_expired_inodes(&wb->b_dirty, &wb->b_io, older_than_this);
30866f3b8e2SJens Axboe }
30966f3b8e2SJens Axboe 
310a9185b41SChristoph Hellwig static int write_inode(struct inode *inode, struct writeback_control *wbc)
31166f3b8e2SJens Axboe {
31203ba3782SJens Axboe 	if (inode->i_sb->s_op->write_inode && !is_bad_inode(inode))
313a9185b41SChristoph Hellwig 		return inode->i_sb->s_op->write_inode(inode, wbc);
31403ba3782SJens Axboe 	return 0;
31566f3b8e2SJens Axboe }
31608d8e974SFengguang Wu 
3172c136579SFengguang Wu /*
31801c03194SChristoph Hellwig  * Wait for writeback on an inode to complete.
31901c03194SChristoph Hellwig  */
32001c03194SChristoph Hellwig static void inode_wait_for_writeback(struct inode *inode)
32101c03194SChristoph Hellwig {
32201c03194SChristoph Hellwig 	DEFINE_WAIT_BIT(wq, &inode->i_state, __I_SYNC);
32301c03194SChristoph Hellwig 	wait_queue_head_t *wqh;
32401c03194SChristoph Hellwig 
32501c03194SChristoph Hellwig 	wqh = bit_waitqueue(&inode->i_state, __I_SYNC);
32658a9d3d8SRichard Kennedy 	while (inode->i_state & I_SYNC) {
327250df6edSDave Chinner 		spin_unlock(&inode->i_lock);
328a66979abSDave Chinner 		spin_unlock(&inode_wb_list_lock);
32901c03194SChristoph Hellwig 		__wait_on_bit(wqh, &wq, inode_wait, TASK_UNINTERRUPTIBLE);
330a66979abSDave Chinner 		spin_lock(&inode_wb_list_lock);
331250df6edSDave Chinner 		spin_lock(&inode->i_lock);
33258a9d3d8SRichard Kennedy 	}
33301c03194SChristoph Hellwig }
33401c03194SChristoph Hellwig 
33501c03194SChristoph Hellwig /*
3360f1b1fd8SDave Chinner  * Write out an inode's dirty pages.  Called under inode_wb_list_lock and
3370f1b1fd8SDave Chinner  * inode->i_lock.  Either the caller has an active reference on the inode or
3380f1b1fd8SDave Chinner  * the inode has I_WILL_FREE set.
33901c03194SChristoph Hellwig  *
3401da177e4SLinus Torvalds  * If `wait' is set, wait on the writeout.
3411da177e4SLinus Torvalds  *
3421da177e4SLinus Torvalds  * The whole writeout design is quite complex and fragile.  We want to avoid
3431da177e4SLinus Torvalds  * starvation of particular inodes when others are being redirtied, prevent
3441da177e4SLinus Torvalds  * livelocks, etc.
3451da177e4SLinus Torvalds  */
3461da177e4SLinus Torvalds static int
34701c03194SChristoph Hellwig writeback_single_inode(struct inode *inode, struct writeback_control *wbc)
3481da177e4SLinus Torvalds {
3491da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
35001c03194SChristoph Hellwig 	unsigned dirty;
3511da177e4SLinus Torvalds 	int ret;
3521da177e4SLinus Torvalds 
3530f1b1fd8SDave Chinner 	assert_spin_locked(&inode_wb_list_lock);
3540f1b1fd8SDave Chinner 	assert_spin_locked(&inode->i_lock);
3550f1b1fd8SDave Chinner 
35601c03194SChristoph Hellwig 	if (!atomic_read(&inode->i_count))
35701c03194SChristoph Hellwig 		WARN_ON(!(inode->i_state & (I_WILL_FREE|I_FREEING)));
35801c03194SChristoph Hellwig 	else
35901c03194SChristoph Hellwig 		WARN_ON(inode->i_state & I_WILL_FREE);
36001c03194SChristoph Hellwig 
36101c03194SChristoph Hellwig 	if (inode->i_state & I_SYNC) {
36201c03194SChristoph Hellwig 		/*
36301c03194SChristoph Hellwig 		 * If this inode is locked for writeback and we are not doing
36466f3b8e2SJens Axboe 		 * writeback-for-data-integrity, move it to b_more_io so that
36501c03194SChristoph Hellwig 		 * writeback can proceed with the other inodes on s_io.
36601c03194SChristoph Hellwig 		 *
36701c03194SChristoph Hellwig 		 * We'll have another go at writing back this inode when we
36866f3b8e2SJens Axboe 		 * completed a full scan of b_io.
36901c03194SChristoph Hellwig 		 */
370a9185b41SChristoph Hellwig 		if (wbc->sync_mode != WB_SYNC_ALL) {
37101c03194SChristoph Hellwig 			requeue_io(inode);
37201c03194SChristoph Hellwig 			return 0;
37301c03194SChristoph Hellwig 		}
37401c03194SChristoph Hellwig 
37501c03194SChristoph Hellwig 		/*
37601c03194SChristoph Hellwig 		 * It's a data-integrity sync.  We must wait.
37701c03194SChristoph Hellwig 		 */
37801c03194SChristoph Hellwig 		inode_wait_for_writeback(inode);
37901c03194SChristoph Hellwig 	}
38001c03194SChristoph Hellwig 
3811c0eeaf5SJoern Engel 	BUG_ON(inode->i_state & I_SYNC);
3821da177e4SLinus Torvalds 
3835547e8aaSDmitry Monakhov 	/* Set I_SYNC, reset I_DIRTY_PAGES */
3841c0eeaf5SJoern Engel 	inode->i_state |= I_SYNC;
3855547e8aaSDmitry Monakhov 	inode->i_state &= ~I_DIRTY_PAGES;
386250df6edSDave Chinner 	spin_unlock(&inode->i_lock);
387a66979abSDave Chinner 	spin_unlock(&inode_wb_list_lock);
3881da177e4SLinus Torvalds 
3891da177e4SLinus Torvalds 	ret = do_writepages(mapping, wbc);
3901da177e4SLinus Torvalds 
39126821ed4SChristoph Hellwig 	/*
39226821ed4SChristoph Hellwig 	 * Make sure to wait on the data before writing out the metadata.
39326821ed4SChristoph Hellwig 	 * This is important for filesystems that modify metadata on data
39426821ed4SChristoph Hellwig 	 * I/O completion.
39526821ed4SChristoph Hellwig 	 */
396a9185b41SChristoph Hellwig 	if (wbc->sync_mode == WB_SYNC_ALL) {
39726821ed4SChristoph Hellwig 		int err = filemap_fdatawait(mapping);
3981da177e4SLinus Torvalds 		if (ret == 0)
3991da177e4SLinus Torvalds 			ret = err;
4001da177e4SLinus Torvalds 	}
4011da177e4SLinus Torvalds 
4025547e8aaSDmitry Monakhov 	/*
4035547e8aaSDmitry Monakhov 	 * Some filesystems may redirty the inode during the writeback
4045547e8aaSDmitry Monakhov 	 * due to delalloc, clear dirty metadata flags right before
4055547e8aaSDmitry Monakhov 	 * write_inode()
4065547e8aaSDmitry Monakhov 	 */
407250df6edSDave Chinner 	spin_lock(&inode->i_lock);
4085547e8aaSDmitry Monakhov 	dirty = inode->i_state & I_DIRTY;
4095547e8aaSDmitry Monakhov 	inode->i_state &= ~(I_DIRTY_SYNC | I_DIRTY_DATASYNC);
410250df6edSDave Chinner 	spin_unlock(&inode->i_lock);
41126821ed4SChristoph Hellwig 	/* Don't write the inode if only I_DIRTY_PAGES was set */
41226821ed4SChristoph Hellwig 	if (dirty & (I_DIRTY_SYNC | I_DIRTY_DATASYNC)) {
413a9185b41SChristoph Hellwig 		int err = write_inode(inode, wbc);
4141da177e4SLinus Torvalds 		if (ret == 0)
4151da177e4SLinus Torvalds 			ret = err;
4161da177e4SLinus Torvalds 	}
4171da177e4SLinus Torvalds 
418a66979abSDave Chinner 	spin_lock(&inode_wb_list_lock);
419250df6edSDave Chinner 	spin_lock(&inode->i_lock);
4201c0eeaf5SJoern Engel 	inode->i_state &= ~I_SYNC;
421a4ffdde6SAl Viro 	if (!(inode->i_state & I_FREEING)) {
42294c3dcbbSWu Fengguang 		/*
42394c3dcbbSWu Fengguang 		 * Sync livelock prevention. Each inode is tagged and synced in
42494c3dcbbSWu Fengguang 		 * one shot. If still dirty, it will be redirty_tail()'ed below.
42594c3dcbbSWu Fengguang 		 * Update the dirty time to prevent enqueue and sync it again.
42694c3dcbbSWu Fengguang 		 */
42794c3dcbbSWu Fengguang 		if ((inode->i_state & I_DIRTY) &&
42894c3dcbbSWu Fengguang 		    (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages))
42994c3dcbbSWu Fengguang 			inode->dirtied_when = jiffies;
43094c3dcbbSWu Fengguang 
43123539afcSWu Fengguang 		if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) {
4321da177e4SLinus Torvalds 			/*
4331da177e4SLinus Torvalds 			 * We didn't write back all the pages.  nfs_writepages()
434a50aeb40SWu Fengguang 			 * sometimes bales out without doing anything.
4351da177e4SLinus Torvalds 			 */
4361da177e4SLinus Torvalds 			inode->i_state |= I_DIRTY_PAGES;
4378bc3be27SFengguang Wu 			if (wbc->nr_to_write <= 0) {
4388bc3be27SFengguang Wu 				/*
4398bc3be27SFengguang Wu 				 * slice used up: queue for next turn
4408bc3be27SFengguang Wu 				 */
4410e0f4fc2SKen Chen 				requeue_io(inode);
4421da177e4SLinus Torvalds 			} else {
4431da177e4SLinus Torvalds 				/*
444a50aeb40SWu Fengguang 				 * Writeback blocked by something other than
445a50aeb40SWu Fengguang 				 * congestion. Delay the inode for some time to
446a50aeb40SWu Fengguang 				 * avoid spinning on the CPU (100% iowait)
447a50aeb40SWu Fengguang 				 * retrying writeback of the dirty page/inode
448a50aeb40SWu Fengguang 				 * that cannot be performed immediately.
4498bc3be27SFengguang Wu 				 */
4508bc3be27SFengguang Wu 				redirty_tail(inode);
4518bc3be27SFengguang Wu 			}
45223539afcSWu Fengguang 		} else if (inode->i_state & I_DIRTY) {
45323539afcSWu Fengguang 			/*
45423539afcSWu Fengguang 			 * Filesystems can dirty the inode during writeback
45523539afcSWu Fengguang 			 * operations, such as delayed allocation during
45623539afcSWu Fengguang 			 * submission or metadata updates after data IO
45723539afcSWu Fengguang 			 * completion.
45823539afcSWu Fengguang 			 */
45923539afcSWu Fengguang 			redirty_tail(inode);
4601da177e4SLinus Torvalds 		} else {
4611da177e4SLinus Torvalds 			/*
4629e38d86fSNick Piggin 			 * The inode is clean.  At this point we either have
4639e38d86fSNick Piggin 			 * a reference to the inode or it's on it's way out.
4649e38d86fSNick Piggin 			 * No need to add it back to the LRU.
4651da177e4SLinus Torvalds 			 */
4667ccf19a8SNick Piggin 			list_del_init(&inode->i_wb_list);
467cb9bd115SWu Fengguang 			wbc->inodes_written++;
4681da177e4SLinus Torvalds 		}
4691da177e4SLinus Torvalds 	}
4701c0eeaf5SJoern Engel 	inode_sync_complete(inode);
4711da177e4SLinus Torvalds 	return ret;
4721da177e4SLinus Torvalds }
4731da177e4SLinus Torvalds 
47403ba3782SJens Axboe /*
475d19de7edSChristoph Hellwig  * For background writeback the caller does not have the sb pinned
47603ba3782SJens Axboe  * before calling writeback. So make sure that we do pin it, so it doesn't
47703ba3782SJens Axboe  * go away while we are writing inodes from it.
47803ba3782SJens Axboe  */
479d19de7edSChristoph Hellwig static bool pin_sb_for_writeback(struct super_block *sb)
4801da177e4SLinus Torvalds {
48103ba3782SJens Axboe 	spin_lock(&sb_lock);
48229cb4859SChristoph Hellwig 	if (list_empty(&sb->s_instances)) {
48303ba3782SJens Axboe 		spin_unlock(&sb_lock);
48429cb4859SChristoph Hellwig 		return false;
48503ba3782SJens Axboe 	}
48629cb4859SChristoph Hellwig 
48729cb4859SChristoph Hellwig 	sb->s_count++;
48829cb4859SChristoph Hellwig 	spin_unlock(&sb_lock);
48929cb4859SChristoph Hellwig 
49029cb4859SChristoph Hellwig 	if (down_read_trylock(&sb->s_umount)) {
49129cb4859SChristoph Hellwig 		if (sb->s_root)
49229cb4859SChristoph Hellwig 			return true;
49303ba3782SJens Axboe 		up_read(&sb->s_umount);
49403ba3782SJens Axboe 	}
49529cb4859SChristoph Hellwig 
49629cb4859SChristoph Hellwig 	put_super(sb);
497d19de7edSChristoph Hellwig 	return false;
49803ba3782SJens Axboe }
49903ba3782SJens Axboe 
500f11c9c5cSEdward Shishkin /*
501f11c9c5cSEdward Shishkin  * Write a portion of b_io inodes which belong to @sb.
502edadfb10SChristoph Hellwig  *
503edadfb10SChristoph Hellwig  * If @only_this_sb is true, then find and write all such
504f11c9c5cSEdward Shishkin  * inodes. Otherwise write only ones which go sequentially
505f11c9c5cSEdward Shishkin  * in reverse order.
506edadfb10SChristoph Hellwig  *
507f11c9c5cSEdward Shishkin  * Return 1, if the caller writeback routine should be
508f11c9c5cSEdward Shishkin  * interrupted. Otherwise return 0.
509f11c9c5cSEdward Shishkin  */
510edadfb10SChristoph Hellwig static int writeback_sb_inodes(struct super_block *sb, struct bdi_writeback *wb,
511edadfb10SChristoph Hellwig 		struct writeback_control *wbc, bool only_this_sb)
51203ba3782SJens Axboe {
51303ba3782SJens Axboe 	while (!list_empty(&wb->b_io)) {
514f11c9c5cSEdward Shishkin 		long pages_skipped;
5157ccf19a8SNick Piggin 		struct inode *inode = wb_inode(wb->b_io.prev);
516edadfb10SChristoph Hellwig 
517edadfb10SChristoph Hellwig 		if (inode->i_sb != sb) {
518edadfb10SChristoph Hellwig 			if (only_this_sb) {
519edadfb10SChristoph Hellwig 				/*
520edadfb10SChristoph Hellwig 				 * We only want to write back data for this
521edadfb10SChristoph Hellwig 				 * superblock, move all inodes not belonging
522edadfb10SChristoph Hellwig 				 * to it back onto the dirty list.
523edadfb10SChristoph Hellwig 				 */
52466f3b8e2SJens Axboe 				redirty_tail(inode);
52566f3b8e2SJens Axboe 				continue;
52666f3b8e2SJens Axboe 			}
527edadfb10SChristoph Hellwig 
528edadfb10SChristoph Hellwig 			/*
529edadfb10SChristoph Hellwig 			 * The inode belongs to a different superblock.
530edadfb10SChristoph Hellwig 			 * Bounce back to the caller to unpin this and
531edadfb10SChristoph Hellwig 			 * pin the next superblock.
532edadfb10SChristoph Hellwig 			 */
533f11c9c5cSEdward Shishkin 			return 0;
534edadfb10SChristoph Hellwig 		}
535edadfb10SChristoph Hellwig 
5369843b76aSChristoph Hellwig 		/*
5379843b76aSChristoph Hellwig 		 * Don't bother with new inodes or inodes beeing freed, first
5389843b76aSChristoph Hellwig 		 * kind does not need peridic writeout yet, and for the latter
5399843b76aSChristoph Hellwig 		 * kind writeout is handled by the freer.
5409843b76aSChristoph Hellwig 		 */
541250df6edSDave Chinner 		spin_lock(&inode->i_lock);
5429843b76aSChristoph Hellwig 		if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) {
543250df6edSDave Chinner 			spin_unlock(&inode->i_lock);
5447ef0d737SNick Piggin 			requeue_io(inode);
5457ef0d737SNick Piggin 			continue;
5467ef0d737SNick Piggin 		}
5479843b76aSChristoph Hellwig 
548d2caa3c5SJeff Layton 		/*
549d2caa3c5SJeff Layton 		 * Was this inode dirtied after sync_sb_inodes was called?
550d2caa3c5SJeff Layton 		 * This keeps sync from extra jobs and livelock.
551d2caa3c5SJeff Layton 		 */
552250df6edSDave Chinner 		if (inode_dirtied_after(inode, wbc->wb_start)) {
553250df6edSDave Chinner 			spin_unlock(&inode->i_lock);
554f11c9c5cSEdward Shishkin 			return 1;
555250df6edSDave Chinner 		}
5561da177e4SLinus Torvalds 
5571da177e4SLinus Torvalds 		__iget(inode);
558250df6edSDave Chinner 
5591da177e4SLinus Torvalds 		pages_skipped = wbc->pages_skipped;
56001c03194SChristoph Hellwig 		writeback_single_inode(inode, wbc);
5611da177e4SLinus Torvalds 		if (wbc->pages_skipped != pages_skipped) {
5621da177e4SLinus Torvalds 			/*
5631da177e4SLinus Torvalds 			 * writeback is not making progress due to locked
5641da177e4SLinus Torvalds 			 * buffers.  Skip this inode for now.
5651da177e4SLinus Torvalds 			 */
566f57b9b7bSAndrew Morton 			redirty_tail(inode);
5671da177e4SLinus Torvalds 		}
5680f1b1fd8SDave Chinner 		spin_unlock(&inode->i_lock);
569a66979abSDave Chinner 		spin_unlock(&inode_wb_list_lock);
5701da177e4SLinus Torvalds 		iput(inode);
5714ffc8444SOGAWA Hirofumi 		cond_resched();
572a66979abSDave Chinner 		spin_lock(&inode_wb_list_lock);
5738bc3be27SFengguang Wu 		if (wbc->nr_to_write <= 0) {
5748bc3be27SFengguang Wu 			wbc->more_io = 1;
575f11c9c5cSEdward Shishkin 			return 1;
5761da177e4SLinus Torvalds 		}
57703ba3782SJens Axboe 		if (!list_empty(&wb->b_more_io))
5788bc3be27SFengguang Wu 			wbc->more_io = 1;
5798bc3be27SFengguang Wu 	}
580f11c9c5cSEdward Shishkin 	/* b_io is empty */
581f11c9c5cSEdward Shishkin 	return 1;
582f11c9c5cSEdward Shishkin }
58338f21977SNick Piggin 
5849c3a8ee8SChristoph Hellwig void writeback_inodes_wb(struct bdi_writeback *wb,
585f11c9c5cSEdward Shishkin 		struct writeback_control *wbc)
586f11c9c5cSEdward Shishkin {
587f11c9c5cSEdward Shishkin 	int ret = 0;
5889ecc2738SJens Axboe 
5897624ee72SJan Kara 	if (!wbc->wb_start)
590f11c9c5cSEdward Shishkin 		wbc->wb_start = jiffies; /* livelock avoidance */
591a66979abSDave Chinner 	spin_lock(&inode_wb_list_lock);
592f11c9c5cSEdward Shishkin 	if (!wbc->for_kupdate || list_empty(&wb->b_io))
593f11c9c5cSEdward Shishkin 		queue_io(wb, wbc->older_than_this);
594f11c9c5cSEdward Shishkin 
595f11c9c5cSEdward Shishkin 	while (!list_empty(&wb->b_io)) {
5967ccf19a8SNick Piggin 		struct inode *inode = wb_inode(wb->b_io.prev);
597f11c9c5cSEdward Shishkin 		struct super_block *sb = inode->i_sb;
598f11c9c5cSEdward Shishkin 
599334132aeSChristoph Hellwig 		if (!pin_sb_for_writeback(sb)) {
600334132aeSChristoph Hellwig 			requeue_io(inode);
601d19de7edSChristoph Hellwig 			continue;
602334132aeSChristoph Hellwig 		}
603edadfb10SChristoph Hellwig 		ret = writeback_sb_inodes(sb, wb, wbc, false);
604d19de7edSChristoph Hellwig 		drop_super(sb);
605f11c9c5cSEdward Shishkin 
606f11c9c5cSEdward Shishkin 		if (ret)
607f11c9c5cSEdward Shishkin 			break;
608f11c9c5cSEdward Shishkin 	}
609a66979abSDave Chinner 	spin_unlock(&inode_wb_list_lock);
61066f3b8e2SJens Axboe 	/* Leave any unwritten inodes on b_io */
61166f3b8e2SJens Axboe }
61266f3b8e2SJens Axboe 
613edadfb10SChristoph Hellwig static void __writeback_inodes_sb(struct super_block *sb,
614edadfb10SChristoph Hellwig 		struct bdi_writeback *wb, struct writeback_control *wbc)
615edadfb10SChristoph Hellwig {
616edadfb10SChristoph Hellwig 	WARN_ON(!rwsem_is_locked(&sb->s_umount));
617edadfb10SChristoph Hellwig 
618a66979abSDave Chinner 	spin_lock(&inode_wb_list_lock);
619edadfb10SChristoph Hellwig 	if (!wbc->for_kupdate || list_empty(&wb->b_io))
620edadfb10SChristoph Hellwig 		queue_io(wb, wbc->older_than_this);
621edadfb10SChristoph Hellwig 	writeback_sb_inodes(sb, wb, wbc, true);
622a66979abSDave Chinner 	spin_unlock(&inode_wb_list_lock);
623edadfb10SChristoph Hellwig }
624edadfb10SChristoph Hellwig 
62503ba3782SJens Axboe /*
62603ba3782SJens Axboe  * The maximum number of pages to writeout in a single bdi flush/kupdate
62703ba3782SJens Axboe  * operation.  We do this so we don't hold I_SYNC against an inode for
62803ba3782SJens Axboe  * enormous amounts of time, which would block a userspace task which has
62903ba3782SJens Axboe  * been forced to throttle against that inode.  Also, the code reevaluates
63003ba3782SJens Axboe  * the dirty each time it has written this many pages.
63103ba3782SJens Axboe  */
63203ba3782SJens Axboe #define MAX_WRITEBACK_PAGES     1024
63303ba3782SJens Axboe 
63403ba3782SJens Axboe static inline bool over_bground_thresh(void)
63503ba3782SJens Axboe {
63603ba3782SJens Axboe 	unsigned long background_thresh, dirty_thresh;
63703ba3782SJens Axboe 
63816c4042fSWu Fengguang 	global_dirty_limits(&background_thresh, &dirty_thresh);
63903ba3782SJens Axboe 
64003ba3782SJens Axboe 	return (global_page_state(NR_FILE_DIRTY) +
6414cbec4c8SWu Fengguang 		global_page_state(NR_UNSTABLE_NFS) > background_thresh);
64203ba3782SJens Axboe }
64303ba3782SJens Axboe 
64403ba3782SJens Axboe /*
64503ba3782SJens Axboe  * Explicit flushing or periodic writeback of "old" data.
64603ba3782SJens Axboe  *
64703ba3782SJens Axboe  * Define "old": the first time one of an inode's pages is dirtied, we mark the
64803ba3782SJens Axboe  * dirtying-time in the inode's address_space.  So this periodic writeback code
64903ba3782SJens Axboe  * just walks the superblock inode list, writing back any inodes which are
65003ba3782SJens Axboe  * older than a specific point in time.
65103ba3782SJens Axboe  *
65203ba3782SJens Axboe  * Try to run once per dirty_writeback_interval.  But if a writeback event
65303ba3782SJens Axboe  * takes longer than a dirty_writeback_interval interval, then leave a
65403ba3782SJens Axboe  * one-second gap.
65503ba3782SJens Axboe  *
65603ba3782SJens Axboe  * older_than_this takes precedence over nr_to_write.  So we'll only write back
65703ba3782SJens Axboe  * all dirty pages if they are all attached to "old" mappings.
65803ba3782SJens Axboe  */
659c4a77a6cSJens Axboe static long wb_writeback(struct bdi_writeback *wb,
66083ba7b07SChristoph Hellwig 			 struct wb_writeback_work *work)
66103ba3782SJens Axboe {
66203ba3782SJens Axboe 	struct writeback_control wbc = {
66383ba7b07SChristoph Hellwig 		.sync_mode		= work->sync_mode,
6646e6938b6SWu Fengguang 		.tagged_writepages	= work->tagged_writepages,
66503ba3782SJens Axboe 		.older_than_this	= NULL,
66683ba7b07SChristoph Hellwig 		.for_kupdate		= work->for_kupdate,
66783ba7b07SChristoph Hellwig 		.for_background		= work->for_background,
66883ba7b07SChristoph Hellwig 		.range_cyclic		= work->range_cyclic,
66903ba3782SJens Axboe 	};
67003ba3782SJens Axboe 	unsigned long oldest_jif;
67103ba3782SJens Axboe 	long wrote = 0;
6726e6938b6SWu Fengguang 	long write_chunk = MAX_WRITEBACK_PAGES;
673a5989bdcSJan Kara 	struct inode *inode;
67403ba3782SJens Axboe 
67503ba3782SJens Axboe 	if (wbc.for_kupdate) {
67603ba3782SJens Axboe 		wbc.older_than_this = &oldest_jif;
67703ba3782SJens Axboe 		oldest_jif = jiffies -
67803ba3782SJens Axboe 				msecs_to_jiffies(dirty_expire_interval * 10);
67903ba3782SJens Axboe 	}
680c4a77a6cSJens Axboe 	if (!wbc.range_cyclic) {
681c4a77a6cSJens Axboe 		wbc.range_start = 0;
682c4a77a6cSJens Axboe 		wbc.range_end = LLONG_MAX;
683c4a77a6cSJens Axboe 	}
68403ba3782SJens Axboe 
685b9543dacSJan Kara 	/*
686b9543dacSJan Kara 	 * WB_SYNC_ALL mode does livelock avoidance by syncing dirty
687b9543dacSJan Kara 	 * inodes/pages in one big loop. Setting wbc.nr_to_write=LONG_MAX
688b9543dacSJan Kara 	 * here avoids calling into writeback_inodes_wb() more than once.
689b9543dacSJan Kara 	 *
690b9543dacSJan Kara 	 * The intended call sequence for WB_SYNC_ALL writeback is:
691b9543dacSJan Kara 	 *
692b9543dacSJan Kara 	 *      wb_writeback()
693b9543dacSJan Kara 	 *          __writeback_inodes_sb()     <== called only once
694b9543dacSJan Kara 	 *              write_cache_pages()     <== called once for each inode
695b9543dacSJan Kara 	 *                   (quickly) tag currently dirty pages
696b9543dacSJan Kara 	 *                   (maybe slowly) sync all tagged pages
697b9543dacSJan Kara 	 */
6986e6938b6SWu Fengguang 	if (wbc.sync_mode == WB_SYNC_ALL || wbc.tagged_writepages)
699b9543dacSJan Kara 		write_chunk = LONG_MAX;
700b9543dacSJan Kara 
7017624ee72SJan Kara 	wbc.wb_start = jiffies; /* livelock avoidance */
70203ba3782SJens Axboe 	for (;;) {
70303ba3782SJens Axboe 		/*
704d3ddec76SWu Fengguang 		 * Stop writeback when nr_pages has been consumed
70503ba3782SJens Axboe 		 */
70683ba7b07SChristoph Hellwig 		if (work->nr_pages <= 0)
70703ba3782SJens Axboe 			break;
70803ba3782SJens Axboe 
70903ba3782SJens Axboe 		/*
710aa373cf5SJan Kara 		 * Background writeout and kupdate-style writeback may
711aa373cf5SJan Kara 		 * run forever. Stop them if there is other work to do
712aa373cf5SJan Kara 		 * so that e.g. sync can proceed. They'll be restarted
713aa373cf5SJan Kara 		 * after the other works are all done.
714aa373cf5SJan Kara 		 */
715aa373cf5SJan Kara 		if ((work->for_background || work->for_kupdate) &&
716aa373cf5SJan Kara 		    !list_empty(&wb->bdi->work_list))
717aa373cf5SJan Kara 			break;
718aa373cf5SJan Kara 
719aa373cf5SJan Kara 		/*
720d3ddec76SWu Fengguang 		 * For background writeout, stop when we are below the
721d3ddec76SWu Fengguang 		 * background dirty threshold
72203ba3782SJens Axboe 		 */
72383ba7b07SChristoph Hellwig 		if (work->for_background && !over_bground_thresh())
72403ba3782SJens Axboe 			break;
72503ba3782SJens Axboe 
72603ba3782SJens Axboe 		wbc.more_io = 0;
727b9543dacSJan Kara 		wbc.nr_to_write = write_chunk;
72803ba3782SJens Axboe 		wbc.pages_skipped = 0;
729cb9bd115SWu Fengguang 		wbc.inodes_written = 0;
730028c2dd1SDave Chinner 
731028c2dd1SDave Chinner 		trace_wbc_writeback_start(&wbc, wb->bdi);
73283ba7b07SChristoph Hellwig 		if (work->sb)
73383ba7b07SChristoph Hellwig 			__writeback_inodes_sb(work->sb, wb, &wbc);
734edadfb10SChristoph Hellwig 		else
73503ba3782SJens Axboe 			writeback_inodes_wb(wb, &wbc);
736028c2dd1SDave Chinner 		trace_wbc_writeback_written(&wbc, wb->bdi);
737028c2dd1SDave Chinner 
738b9543dacSJan Kara 		work->nr_pages -= write_chunk - wbc.nr_to_write;
739b9543dacSJan Kara 		wrote += write_chunk - wbc.nr_to_write;
74003ba3782SJens Axboe 
74103ba3782SJens Axboe 		/*
74271fd05a8SJens Axboe 		 * If we consumed everything, see if we have more
74303ba3782SJens Axboe 		 */
74471fd05a8SJens Axboe 		if (wbc.nr_to_write <= 0)
74571fd05a8SJens Axboe 			continue;
746cb9bd115SWu Fengguang 		if (wbc.inodes_written)
747cb9bd115SWu Fengguang 			continue;
74871fd05a8SJens Axboe 		/*
74971fd05a8SJens Axboe 		 * Didn't write everything and we don't have more IO, bail
75071fd05a8SJens Axboe 		 */
75171fd05a8SJens Axboe 		if (!wbc.more_io)
75271fd05a8SJens Axboe 			break;
75371fd05a8SJens Axboe 		/*
75471fd05a8SJens Axboe 		 * Did we write something? Try for more
75571fd05a8SJens Axboe 		 */
756b9543dacSJan Kara 		if (wbc.nr_to_write < write_chunk)
75703ba3782SJens Axboe 			continue;
758a5989bdcSJan Kara 		/*
759a5989bdcSJan Kara 		 * Nothing written. Wait for some inode to
760a5989bdcSJan Kara 		 * become available for writeback. Otherwise
761a5989bdcSJan Kara 		 * we'll just busyloop.
762a5989bdcSJan Kara 		 */
763a66979abSDave Chinner 		spin_lock(&inode_wb_list_lock);
764a5989bdcSJan Kara 		if (!list_empty(&wb->b_more_io))  {
7657ccf19a8SNick Piggin 			inode = wb_inode(wb->b_more_io.prev);
766028c2dd1SDave Chinner 			trace_wbc_writeback_wait(&wbc, wb->bdi);
767250df6edSDave Chinner 			spin_lock(&inode->i_lock);
768a5989bdcSJan Kara 			inode_wait_for_writeback(inode);
769250df6edSDave Chinner 			spin_unlock(&inode->i_lock);
770a5989bdcSJan Kara 		}
771a66979abSDave Chinner 		spin_unlock(&inode_wb_list_lock);
77203ba3782SJens Axboe 	}
77303ba3782SJens Axboe 
77403ba3782SJens Axboe 	return wrote;
77503ba3782SJens Axboe }
77603ba3782SJens Axboe 
77703ba3782SJens Axboe /*
77883ba7b07SChristoph Hellwig  * Return the next wb_writeback_work struct that hasn't been processed yet.
77903ba3782SJens Axboe  */
78083ba7b07SChristoph Hellwig static struct wb_writeback_work *
78108852b6dSMinchan Kim get_next_work_item(struct backing_dev_info *bdi)
78203ba3782SJens Axboe {
78383ba7b07SChristoph Hellwig 	struct wb_writeback_work *work = NULL;
78403ba3782SJens Axboe 
7856467716aSArtem Bityutskiy 	spin_lock_bh(&bdi->wb_lock);
78683ba7b07SChristoph Hellwig 	if (!list_empty(&bdi->work_list)) {
78783ba7b07SChristoph Hellwig 		work = list_entry(bdi->work_list.next,
78883ba7b07SChristoph Hellwig 				  struct wb_writeback_work, list);
78983ba7b07SChristoph Hellwig 		list_del_init(&work->list);
79003ba3782SJens Axboe 	}
7916467716aSArtem Bityutskiy 	spin_unlock_bh(&bdi->wb_lock);
79283ba7b07SChristoph Hellwig 	return work;
79303ba3782SJens Axboe }
79403ba3782SJens Axboe 
795cdf01dd5SLinus Torvalds /*
796cdf01dd5SLinus Torvalds  * Add in the number of potentially dirty inodes, because each inode
797cdf01dd5SLinus Torvalds  * write can dirty pagecache in the underlying blockdev.
798cdf01dd5SLinus Torvalds  */
799cdf01dd5SLinus Torvalds static unsigned long get_nr_dirty_pages(void)
800cdf01dd5SLinus Torvalds {
801cdf01dd5SLinus Torvalds 	return global_page_state(NR_FILE_DIRTY) +
802cdf01dd5SLinus Torvalds 		global_page_state(NR_UNSTABLE_NFS) +
803cdf01dd5SLinus Torvalds 		get_nr_dirty_inodes();
804cdf01dd5SLinus Torvalds }
805cdf01dd5SLinus Torvalds 
8066585027aSJan Kara static long wb_check_background_flush(struct bdi_writeback *wb)
8076585027aSJan Kara {
8086585027aSJan Kara 	if (over_bground_thresh()) {
8096585027aSJan Kara 
8106585027aSJan Kara 		struct wb_writeback_work work = {
8116585027aSJan Kara 			.nr_pages	= LONG_MAX,
8126585027aSJan Kara 			.sync_mode	= WB_SYNC_NONE,
8136585027aSJan Kara 			.for_background	= 1,
8146585027aSJan Kara 			.range_cyclic	= 1,
8156585027aSJan Kara 		};
8166585027aSJan Kara 
8176585027aSJan Kara 		return wb_writeback(wb, &work);
8186585027aSJan Kara 	}
8196585027aSJan Kara 
8206585027aSJan Kara 	return 0;
8216585027aSJan Kara }
8226585027aSJan Kara 
82303ba3782SJens Axboe static long wb_check_old_data_flush(struct bdi_writeback *wb)
82403ba3782SJens Axboe {
82503ba3782SJens Axboe 	unsigned long expired;
82603ba3782SJens Axboe 	long nr_pages;
82703ba3782SJens Axboe 
82869b62d01SJens Axboe 	/*
82969b62d01SJens Axboe 	 * When set to zero, disable periodic writeback
83069b62d01SJens Axboe 	 */
83169b62d01SJens Axboe 	if (!dirty_writeback_interval)
83269b62d01SJens Axboe 		return 0;
83369b62d01SJens Axboe 
83403ba3782SJens Axboe 	expired = wb->last_old_flush +
83503ba3782SJens Axboe 			msecs_to_jiffies(dirty_writeback_interval * 10);
83603ba3782SJens Axboe 	if (time_before(jiffies, expired))
83703ba3782SJens Axboe 		return 0;
83803ba3782SJens Axboe 
83903ba3782SJens Axboe 	wb->last_old_flush = jiffies;
840cdf01dd5SLinus Torvalds 	nr_pages = get_nr_dirty_pages();
84103ba3782SJens Axboe 
842c4a77a6cSJens Axboe 	if (nr_pages) {
84383ba7b07SChristoph Hellwig 		struct wb_writeback_work work = {
844c4a77a6cSJens Axboe 			.nr_pages	= nr_pages,
845c4a77a6cSJens Axboe 			.sync_mode	= WB_SYNC_NONE,
846c4a77a6cSJens Axboe 			.for_kupdate	= 1,
847c4a77a6cSJens Axboe 			.range_cyclic	= 1,
848c4a77a6cSJens Axboe 		};
849c4a77a6cSJens Axboe 
85083ba7b07SChristoph Hellwig 		return wb_writeback(wb, &work);
851c4a77a6cSJens Axboe 	}
85203ba3782SJens Axboe 
85303ba3782SJens Axboe 	return 0;
85403ba3782SJens Axboe }
85503ba3782SJens Axboe 
85603ba3782SJens Axboe /*
85703ba3782SJens Axboe  * Retrieve work items and do the writeback they describe
85803ba3782SJens Axboe  */
85903ba3782SJens Axboe long wb_do_writeback(struct bdi_writeback *wb, int force_wait)
86003ba3782SJens Axboe {
86103ba3782SJens Axboe 	struct backing_dev_info *bdi = wb->bdi;
86283ba7b07SChristoph Hellwig 	struct wb_writeback_work *work;
863c4a77a6cSJens Axboe 	long wrote = 0;
86403ba3782SJens Axboe 
86581d73a32SJan Kara 	set_bit(BDI_writeback_running, &wb->bdi->state);
86608852b6dSMinchan Kim 	while ((work = get_next_work_item(bdi)) != NULL) {
86703ba3782SJens Axboe 		/*
86803ba3782SJens Axboe 		 * Override sync mode, in case we must wait for completion
86983ba7b07SChristoph Hellwig 		 * because this thread is exiting now.
87003ba3782SJens Axboe 		 */
87103ba3782SJens Axboe 		if (force_wait)
87283ba7b07SChristoph Hellwig 			work->sync_mode = WB_SYNC_ALL;
87383ba7b07SChristoph Hellwig 
874455b2864SDave Chinner 		trace_writeback_exec(bdi, work);
875455b2864SDave Chinner 
87683ba7b07SChristoph Hellwig 		wrote += wb_writeback(wb, work);
87703ba3782SJens Axboe 
87803ba3782SJens Axboe 		/*
87983ba7b07SChristoph Hellwig 		 * Notify the caller of completion if this is a synchronous
88083ba7b07SChristoph Hellwig 		 * work item, otherwise just free it.
88103ba3782SJens Axboe 		 */
88283ba7b07SChristoph Hellwig 		if (work->done)
88383ba7b07SChristoph Hellwig 			complete(work->done);
88483ba7b07SChristoph Hellwig 		else
88583ba7b07SChristoph Hellwig 			kfree(work);
88603ba3782SJens Axboe 	}
88703ba3782SJens Axboe 
88803ba3782SJens Axboe 	/*
88903ba3782SJens Axboe 	 * Check for periodic writeback, kupdated() style
89003ba3782SJens Axboe 	 */
89103ba3782SJens Axboe 	wrote += wb_check_old_data_flush(wb);
8926585027aSJan Kara 	wrote += wb_check_background_flush(wb);
89381d73a32SJan Kara 	clear_bit(BDI_writeback_running, &wb->bdi->state);
89403ba3782SJens Axboe 
89503ba3782SJens Axboe 	return wrote;
89603ba3782SJens Axboe }
89703ba3782SJens Axboe 
89803ba3782SJens Axboe /*
89903ba3782SJens Axboe  * Handle writeback of dirty data for the device backed by this bdi. Also
90003ba3782SJens Axboe  * wakes up periodically and does kupdated style flushing.
90103ba3782SJens Axboe  */
90208243900SChristoph Hellwig int bdi_writeback_thread(void *data)
90303ba3782SJens Axboe {
90408243900SChristoph Hellwig 	struct bdi_writeback *wb = data;
90508243900SChristoph Hellwig 	struct backing_dev_info *bdi = wb->bdi;
90603ba3782SJens Axboe 	long pages_written;
90703ba3782SJens Axboe 
908766f9164SPeter Zijlstra 	current->flags |= PF_SWAPWRITE;
90908243900SChristoph Hellwig 	set_freezable();
910ecd58403SArtem Bityutskiy 	wb->last_active = jiffies;
91103ba3782SJens Axboe 
91203ba3782SJens Axboe 	/*
91308243900SChristoph Hellwig 	 * Our parent may run at a different priority, just set us to normal
91403ba3782SJens Axboe 	 */
91508243900SChristoph Hellwig 	set_user_nice(current, 0);
91608243900SChristoph Hellwig 
917455b2864SDave Chinner 	trace_writeback_thread_start(bdi);
918455b2864SDave Chinner 
91903ba3782SJens Axboe 	while (!kthread_should_stop()) {
9206467716aSArtem Bityutskiy 		/*
9216467716aSArtem Bityutskiy 		 * Remove own delayed wake-up timer, since we are already awake
9226467716aSArtem Bityutskiy 		 * and we'll take care of the preriodic write-back.
9236467716aSArtem Bityutskiy 		 */
9246467716aSArtem Bityutskiy 		del_timer(&wb->wakeup_timer);
9256467716aSArtem Bityutskiy 
92603ba3782SJens Axboe 		pages_written = wb_do_writeback(wb, 0);
92703ba3782SJens Axboe 
928455b2864SDave Chinner 		trace_writeback_pages_written(pages_written);
929455b2864SDave Chinner 
93003ba3782SJens Axboe 		if (pages_written)
931ecd58403SArtem Bityutskiy 			wb->last_active = jiffies;
93203ba3782SJens Axboe 
933297252c8SArtem Bityutskiy 		set_current_state(TASK_INTERRUPTIBLE);
934b76b4014SJ. Bruce Fields 		if (!list_empty(&bdi->work_list) || kthread_should_stop()) {
935297252c8SArtem Bityutskiy 			__set_current_state(TASK_RUNNING);
936297252c8SArtem Bityutskiy 			continue;
93703ba3782SJens Axboe 		}
93803ba3782SJens Axboe 
939253c34e9SArtem Bityutskiy 		if (wb_has_dirty_io(wb) && dirty_writeback_interval)
940fff5b85aSArtem Bityutskiy 			schedule_timeout(msecs_to_jiffies(dirty_writeback_interval * 10));
941253c34e9SArtem Bityutskiy 		else {
942253c34e9SArtem Bityutskiy 			/*
943253c34e9SArtem Bityutskiy 			 * We have nothing to do, so can go sleep without any
944253c34e9SArtem Bityutskiy 			 * timeout and save power. When a work is queued or
945253c34e9SArtem Bityutskiy 			 * something is made dirty - we will be woken up.
946253c34e9SArtem Bityutskiy 			 */
94769b62d01SJens Axboe 			schedule();
948f9eadbbdSJens Axboe 		}
94969b62d01SJens Axboe 
95003ba3782SJens Axboe 		try_to_freeze();
95103ba3782SJens Axboe 	}
95203ba3782SJens Axboe 
953fff5b85aSArtem Bityutskiy 	/* Flush any work that raced with us exiting */
95408243900SChristoph Hellwig 	if (!list_empty(&bdi->work_list))
95508243900SChristoph Hellwig 		wb_do_writeback(wb, 1);
956455b2864SDave Chinner 
957455b2864SDave Chinner 	trace_writeback_thread_stop(bdi);
95803ba3782SJens Axboe 	return 0;
95903ba3782SJens Axboe }
96003ba3782SJens Axboe 
96108243900SChristoph Hellwig 
96203ba3782SJens Axboe /*
96303ba3782SJens Axboe  * Start writeback of `nr_pages' pages.  If `nr_pages' is zero, write back
96403ba3782SJens Axboe  * the whole world.
96503ba3782SJens Axboe  */
96603ba3782SJens Axboe void wakeup_flusher_threads(long nr_pages)
96703ba3782SJens Axboe {
968b8c2f347SChristoph Hellwig 	struct backing_dev_info *bdi;
969b8c2f347SChristoph Hellwig 
97083ba7b07SChristoph Hellwig 	if (!nr_pages) {
97183ba7b07SChristoph Hellwig 		nr_pages = global_page_state(NR_FILE_DIRTY) +
97203ba3782SJens Axboe 				global_page_state(NR_UNSTABLE_NFS);
973b8c2f347SChristoph Hellwig 	}
974b8c2f347SChristoph Hellwig 
975b8c2f347SChristoph Hellwig 	rcu_read_lock();
976b8c2f347SChristoph Hellwig 	list_for_each_entry_rcu(bdi, &bdi_list, bdi_list) {
977b8c2f347SChristoph Hellwig 		if (!bdi_has_dirty_io(bdi))
978b8c2f347SChristoph Hellwig 			continue;
9796585027aSJan Kara 		__bdi_start_writeback(bdi, nr_pages, false);
980b8c2f347SChristoph Hellwig 	}
981b8c2f347SChristoph Hellwig 	rcu_read_unlock();
98203ba3782SJens Axboe }
98303ba3782SJens Axboe 
98403ba3782SJens Axboe static noinline void block_dump___mark_inode_dirty(struct inode *inode)
98503ba3782SJens Axboe {
98603ba3782SJens Axboe 	if (inode->i_ino || strcmp(inode->i_sb->s_id, "bdev")) {
98703ba3782SJens Axboe 		struct dentry *dentry;
98803ba3782SJens Axboe 		const char *name = "?";
98903ba3782SJens Axboe 
99003ba3782SJens Axboe 		dentry = d_find_alias(inode);
99103ba3782SJens Axboe 		if (dentry) {
99203ba3782SJens Axboe 			spin_lock(&dentry->d_lock);
99303ba3782SJens Axboe 			name = (const char *) dentry->d_name.name;
99403ba3782SJens Axboe 		}
99503ba3782SJens Axboe 		printk(KERN_DEBUG
99603ba3782SJens Axboe 		       "%s(%d): dirtied inode %lu (%s) on %s\n",
99703ba3782SJens Axboe 		       current->comm, task_pid_nr(current), inode->i_ino,
99803ba3782SJens Axboe 		       name, inode->i_sb->s_id);
99903ba3782SJens Axboe 		if (dentry) {
100003ba3782SJens Axboe 			spin_unlock(&dentry->d_lock);
100103ba3782SJens Axboe 			dput(dentry);
100203ba3782SJens Axboe 		}
100303ba3782SJens Axboe 	}
100403ba3782SJens Axboe }
100503ba3782SJens Axboe 
100603ba3782SJens Axboe /**
100703ba3782SJens Axboe  *	__mark_inode_dirty -	internal function
100803ba3782SJens Axboe  *	@inode: inode to mark
100903ba3782SJens Axboe  *	@flags: what kind of dirty (i.e. I_DIRTY_SYNC)
101003ba3782SJens Axboe  *	Mark an inode as dirty. Callers should use mark_inode_dirty or
101103ba3782SJens Axboe  *  	mark_inode_dirty_sync.
101203ba3782SJens Axboe  *
101303ba3782SJens Axboe  * Put the inode on the super block's dirty list.
101403ba3782SJens Axboe  *
101503ba3782SJens Axboe  * CAREFUL! We mark it dirty unconditionally, but move it onto the
101603ba3782SJens Axboe  * dirty list only if it is hashed or if it refers to a blockdev.
101703ba3782SJens Axboe  * If it was not hashed, it will never be added to the dirty list
101803ba3782SJens Axboe  * even if it is later hashed, as it will have been marked dirty already.
101903ba3782SJens Axboe  *
102003ba3782SJens Axboe  * In short, make sure you hash any inodes _before_ you start marking
102103ba3782SJens Axboe  * them dirty.
102203ba3782SJens Axboe  *
102303ba3782SJens Axboe  * Note that for blockdevs, inode->dirtied_when represents the dirtying time of
102403ba3782SJens Axboe  * the block-special inode (/dev/hda1) itself.  And the ->dirtied_when field of
102503ba3782SJens Axboe  * the kernel-internal blockdev inode represents the dirtying time of the
102603ba3782SJens Axboe  * blockdev's pages.  This is why for I_DIRTY_PAGES we always use
102703ba3782SJens Axboe  * page->mapping->host, so the page-dirtying time is recorded in the internal
102803ba3782SJens Axboe  * blockdev inode.
102903ba3782SJens Axboe  */
103003ba3782SJens Axboe void __mark_inode_dirty(struct inode *inode, int flags)
103103ba3782SJens Axboe {
103203ba3782SJens Axboe 	struct super_block *sb = inode->i_sb;
1033253c34e9SArtem Bityutskiy 	struct backing_dev_info *bdi = NULL;
103403ba3782SJens Axboe 
103503ba3782SJens Axboe 	/*
103603ba3782SJens Axboe 	 * Don't do this for I_DIRTY_PAGES - that doesn't actually
103703ba3782SJens Axboe 	 * dirty the inode itself
103803ba3782SJens Axboe 	 */
103903ba3782SJens Axboe 	if (flags & (I_DIRTY_SYNC | I_DIRTY_DATASYNC)) {
104003ba3782SJens Axboe 		if (sb->s_op->dirty_inode)
1041aa385729SChristoph Hellwig 			sb->s_op->dirty_inode(inode, flags);
104203ba3782SJens Axboe 	}
104303ba3782SJens Axboe 
104403ba3782SJens Axboe 	/*
104503ba3782SJens Axboe 	 * make sure that changes are seen by all cpus before we test i_state
104603ba3782SJens Axboe 	 * -- mikulas
104703ba3782SJens Axboe 	 */
104803ba3782SJens Axboe 	smp_mb();
104903ba3782SJens Axboe 
105003ba3782SJens Axboe 	/* avoid the locking if we can */
105103ba3782SJens Axboe 	if ((inode->i_state & flags) == flags)
105203ba3782SJens Axboe 		return;
105303ba3782SJens Axboe 
105403ba3782SJens Axboe 	if (unlikely(block_dump))
105503ba3782SJens Axboe 		block_dump___mark_inode_dirty(inode);
105603ba3782SJens Axboe 
1057250df6edSDave Chinner 	spin_lock(&inode->i_lock);
105803ba3782SJens Axboe 	if ((inode->i_state & flags) != flags) {
105903ba3782SJens Axboe 		const int was_dirty = inode->i_state & I_DIRTY;
106003ba3782SJens Axboe 
106103ba3782SJens Axboe 		inode->i_state |= flags;
106203ba3782SJens Axboe 
106303ba3782SJens Axboe 		/*
106403ba3782SJens Axboe 		 * If the inode is being synced, just update its dirty state.
106503ba3782SJens Axboe 		 * The unlocker will place the inode on the appropriate
106603ba3782SJens Axboe 		 * superblock list, based upon its state.
106703ba3782SJens Axboe 		 */
106803ba3782SJens Axboe 		if (inode->i_state & I_SYNC)
1069250df6edSDave Chinner 			goto out_unlock_inode;
107003ba3782SJens Axboe 
107103ba3782SJens Axboe 		/*
107203ba3782SJens Axboe 		 * Only add valid (hashed) inodes to the superblock's
107303ba3782SJens Axboe 		 * dirty list.  Add blockdev inodes as well.
107403ba3782SJens Axboe 		 */
107503ba3782SJens Axboe 		if (!S_ISBLK(inode->i_mode)) {
10761d3382cbSAl Viro 			if (inode_unhashed(inode))
1077250df6edSDave Chinner 				goto out_unlock_inode;
107803ba3782SJens Axboe 		}
1079a4ffdde6SAl Viro 		if (inode->i_state & I_FREEING)
1080250df6edSDave Chinner 			goto out_unlock_inode;
108103ba3782SJens Axboe 
108203ba3782SJens Axboe 		/*
108303ba3782SJens Axboe 		 * If the inode was already on b_dirty/b_io/b_more_io, don't
108403ba3782SJens Axboe 		 * reposition it (that would break b_dirty time-ordering).
108503ba3782SJens Axboe 		 */
108603ba3782SJens Axboe 		if (!was_dirty) {
1087a66979abSDave Chinner 			bool wakeup_bdi = false;
1088253c34e9SArtem Bityutskiy 			bdi = inode_to_bdi(inode);
1089500b067cSJens Axboe 
1090253c34e9SArtem Bityutskiy 			if (bdi_cap_writeback_dirty(bdi)) {
1091253c34e9SArtem Bityutskiy 				WARN(!test_bit(BDI_registered, &bdi->state),
1092253c34e9SArtem Bityutskiy 				     "bdi-%s not registered\n", bdi->name);
1093253c34e9SArtem Bityutskiy 
1094253c34e9SArtem Bityutskiy 				/*
1095253c34e9SArtem Bityutskiy 				 * If this is the first dirty inode for this
1096253c34e9SArtem Bityutskiy 				 * bdi, we have to wake-up the corresponding
1097253c34e9SArtem Bityutskiy 				 * bdi thread to make sure background
1098253c34e9SArtem Bityutskiy 				 * write-back happens later.
1099253c34e9SArtem Bityutskiy 				 */
1100253c34e9SArtem Bityutskiy 				if (!wb_has_dirty_io(&bdi->wb))
1101253c34e9SArtem Bityutskiy 					wakeup_bdi = true;
1102500b067cSJens Axboe 			}
110303ba3782SJens Axboe 
1104a66979abSDave Chinner 			spin_unlock(&inode->i_lock);
1105a66979abSDave Chinner 			spin_lock(&inode_wb_list_lock);
110603ba3782SJens Axboe 			inode->dirtied_when = jiffies;
11077ccf19a8SNick Piggin 			list_move(&inode->i_wb_list, &bdi->wb.b_dirty);
1108a66979abSDave Chinner 			spin_unlock(&inode_wb_list_lock);
1109253c34e9SArtem Bityutskiy 
1110253c34e9SArtem Bityutskiy 			if (wakeup_bdi)
11116467716aSArtem Bityutskiy 				bdi_wakeup_thread_delayed(bdi);
1112a66979abSDave Chinner 			return;
1113a66979abSDave Chinner 		}
1114a66979abSDave Chinner 	}
1115a66979abSDave Chinner out_unlock_inode:
1116a66979abSDave Chinner 	spin_unlock(&inode->i_lock);
1117a66979abSDave Chinner 
111803ba3782SJens Axboe }
111903ba3782SJens Axboe EXPORT_SYMBOL(__mark_inode_dirty);
112003ba3782SJens Axboe 
112166f3b8e2SJens Axboe /*
112266f3b8e2SJens Axboe  * Write out a superblock's list of dirty inodes.  A wait will be performed
112366f3b8e2SJens Axboe  * upon no inodes, all inodes or the final one, depending upon sync_mode.
112466f3b8e2SJens Axboe  *
112566f3b8e2SJens Axboe  * If older_than_this is non-NULL, then only write out inodes which
112666f3b8e2SJens Axboe  * had their first dirtying at a time earlier than *older_than_this.
112766f3b8e2SJens Axboe  *
112866f3b8e2SJens Axboe  * If `bdi' is non-zero then we're being asked to writeback a specific queue.
112966f3b8e2SJens Axboe  * This function assumes that the blockdev superblock's inodes are backed by
113066f3b8e2SJens Axboe  * a variety of queues, so all inodes are searched.  For other superblocks,
113166f3b8e2SJens Axboe  * assume that all inodes are backed by the same queue.
113266f3b8e2SJens Axboe  *
113366f3b8e2SJens Axboe  * The inodes to be written are parked on bdi->b_io.  They are moved back onto
113466f3b8e2SJens Axboe  * bdi->b_dirty as they are selected for writing.  This way, none can be missed
113566f3b8e2SJens Axboe  * on the writer throttling path, and we get decent balancing between many
113666f3b8e2SJens Axboe  * throttled threads: we don't want them all piling up on inode_sync_wait.
113766f3b8e2SJens Axboe  */
1138b6e51316SJens Axboe static void wait_sb_inodes(struct super_block *sb)
113966f3b8e2SJens Axboe {
114038f21977SNick Piggin 	struct inode *inode, *old_inode = NULL;
114138f21977SNick Piggin 
114203ba3782SJens Axboe 	/*
114303ba3782SJens Axboe 	 * We need to be protected against the filesystem going from
114403ba3782SJens Axboe 	 * r/o to r/w or vice versa.
114503ba3782SJens Axboe 	 */
1146b6e51316SJens Axboe 	WARN_ON(!rwsem_is_locked(&sb->s_umount));
114703ba3782SJens Axboe 
114855fa6091SDave Chinner 	spin_lock(&inode_sb_list_lock);
114966f3b8e2SJens Axboe 
115038f21977SNick Piggin 	/*
115138f21977SNick Piggin 	 * Data integrity sync. Must wait for all pages under writeback,
115238f21977SNick Piggin 	 * because there may have been pages dirtied before our sync
115338f21977SNick Piggin 	 * call, but which had writeout started before we write it out.
115438f21977SNick Piggin 	 * In which case, the inode may not be on the dirty list, but
115538f21977SNick Piggin 	 * we still have to wait for that writeout.
115638f21977SNick Piggin 	 */
1157b6e51316SJens Axboe 	list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
1158250df6edSDave Chinner 		struct address_space *mapping = inode->i_mapping;
115938f21977SNick Piggin 
1160250df6edSDave Chinner 		spin_lock(&inode->i_lock);
1161250df6edSDave Chinner 		if ((inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW)) ||
1162250df6edSDave Chinner 		    (mapping->nrpages == 0)) {
1163250df6edSDave Chinner 			spin_unlock(&inode->i_lock);
116438f21977SNick Piggin 			continue;
1165250df6edSDave Chinner 		}
116638f21977SNick Piggin 		__iget(inode);
1167250df6edSDave Chinner 		spin_unlock(&inode->i_lock);
116855fa6091SDave Chinner 		spin_unlock(&inode_sb_list_lock);
116955fa6091SDave Chinner 
117038f21977SNick Piggin 		/*
117155fa6091SDave Chinner 		 * We hold a reference to 'inode' so it couldn't have been
117255fa6091SDave Chinner 		 * removed from s_inodes list while we dropped the
117355fa6091SDave Chinner 		 * inode_sb_list_lock.  We cannot iput the inode now as we can
117455fa6091SDave Chinner 		 * be holding the last reference and we cannot iput it under
117555fa6091SDave Chinner 		 * inode_sb_list_lock. So we keep the reference and iput it
117655fa6091SDave Chinner 		 * later.
117738f21977SNick Piggin 		 */
117838f21977SNick Piggin 		iput(old_inode);
117938f21977SNick Piggin 		old_inode = inode;
118038f21977SNick Piggin 
118138f21977SNick Piggin 		filemap_fdatawait(mapping);
118238f21977SNick Piggin 
118338f21977SNick Piggin 		cond_resched();
118438f21977SNick Piggin 
118555fa6091SDave Chinner 		spin_lock(&inode_sb_list_lock);
118638f21977SNick Piggin 	}
118755fa6091SDave Chinner 	spin_unlock(&inode_sb_list_lock);
118838f21977SNick Piggin 	iput(old_inode);
118966f3b8e2SJens Axboe }
11901da177e4SLinus Torvalds 
1191d8a8559cSJens Axboe /**
11923259f8beSChris Mason  * writeback_inodes_sb_nr -	writeback dirty inodes from given super_block
1193d8a8559cSJens Axboe  * @sb: the superblock
11943259f8beSChris Mason  * @nr: the number of pages to write
11951da177e4SLinus Torvalds  *
1196d8a8559cSJens Axboe  * Start writeback on some inodes on this super_block. No guarantees are made
1197d8a8559cSJens Axboe  * on how many (if any) will be written, and this function does not wait
11983259f8beSChris Mason  * for IO completion of submitted IO.
11991da177e4SLinus Torvalds  */
12003259f8beSChris Mason void writeback_inodes_sb_nr(struct super_block *sb, unsigned long nr)
12011da177e4SLinus Torvalds {
120283ba7b07SChristoph Hellwig 	DECLARE_COMPLETION_ONSTACK(done);
120383ba7b07SChristoph Hellwig 	struct wb_writeback_work work = {
12043c4d7165SChristoph Hellwig 		.sb			= sb,
12053c4d7165SChristoph Hellwig 		.sync_mode		= WB_SYNC_NONE,
12066e6938b6SWu Fengguang 		.tagged_writepages	= 1,
120783ba7b07SChristoph Hellwig 		.done			= &done,
12083259f8beSChris Mason 		.nr_pages		= nr,
12093c4d7165SChristoph Hellwig 	};
12100e3c9a22SJens Axboe 
1211cf37e972SChristoph Hellwig 	WARN_ON(!rwsem_is_locked(&sb->s_umount));
121283ba7b07SChristoph Hellwig 	bdi_queue_work(sb->s_bdi, &work);
121383ba7b07SChristoph Hellwig 	wait_for_completion(&done);
12141da177e4SLinus Torvalds }
12153259f8beSChris Mason EXPORT_SYMBOL(writeback_inodes_sb_nr);
12163259f8beSChris Mason 
12173259f8beSChris Mason /**
12183259f8beSChris Mason  * writeback_inodes_sb	-	writeback dirty inodes from given super_block
12193259f8beSChris Mason  * @sb: the superblock
12203259f8beSChris Mason  *
12213259f8beSChris Mason  * Start writeback on some inodes on this super_block. No guarantees are made
12223259f8beSChris Mason  * on how many (if any) will be written, and this function does not wait
12233259f8beSChris Mason  * for IO completion of submitted IO.
12243259f8beSChris Mason  */
12253259f8beSChris Mason void writeback_inodes_sb(struct super_block *sb)
12263259f8beSChris Mason {
1227925d169fSLinus Torvalds 	return writeback_inodes_sb_nr(sb, get_nr_dirty_pages());
12283259f8beSChris Mason }
1229d8a8559cSJens Axboe EXPORT_SYMBOL(writeback_inodes_sb);
1230d8a8559cSJens Axboe 
1231d8a8559cSJens Axboe /**
123217bd55d0SEric Sandeen  * writeback_inodes_sb_if_idle	-	start writeback if none underway
123317bd55d0SEric Sandeen  * @sb: the superblock
123417bd55d0SEric Sandeen  *
123517bd55d0SEric Sandeen  * Invoke writeback_inodes_sb if no writeback is currently underway.
123617bd55d0SEric Sandeen  * Returns 1 if writeback was started, 0 if not.
123717bd55d0SEric Sandeen  */
123817bd55d0SEric Sandeen int writeback_inodes_sb_if_idle(struct super_block *sb)
123917bd55d0SEric Sandeen {
124017bd55d0SEric Sandeen 	if (!writeback_in_progress(sb->s_bdi)) {
1241cf37e972SChristoph Hellwig 		down_read(&sb->s_umount);
124217bd55d0SEric Sandeen 		writeback_inodes_sb(sb);
1243cf37e972SChristoph Hellwig 		up_read(&sb->s_umount);
124417bd55d0SEric Sandeen 		return 1;
124517bd55d0SEric Sandeen 	} else
124617bd55d0SEric Sandeen 		return 0;
124717bd55d0SEric Sandeen }
124817bd55d0SEric Sandeen EXPORT_SYMBOL(writeback_inodes_sb_if_idle);
124917bd55d0SEric Sandeen 
125017bd55d0SEric Sandeen /**
12513259f8beSChris Mason  * writeback_inodes_sb_if_idle	-	start writeback if none underway
12523259f8beSChris Mason  * @sb: the superblock
12533259f8beSChris Mason  * @nr: the number of pages to write
12543259f8beSChris Mason  *
12553259f8beSChris Mason  * Invoke writeback_inodes_sb if no writeback is currently underway.
12563259f8beSChris Mason  * Returns 1 if writeback was started, 0 if not.
12573259f8beSChris Mason  */
12583259f8beSChris Mason int writeback_inodes_sb_nr_if_idle(struct super_block *sb,
12593259f8beSChris Mason 				   unsigned long nr)
12603259f8beSChris Mason {
12613259f8beSChris Mason 	if (!writeback_in_progress(sb->s_bdi)) {
12623259f8beSChris Mason 		down_read(&sb->s_umount);
12633259f8beSChris Mason 		writeback_inodes_sb_nr(sb, nr);
12643259f8beSChris Mason 		up_read(&sb->s_umount);
12653259f8beSChris Mason 		return 1;
12663259f8beSChris Mason 	} else
12673259f8beSChris Mason 		return 0;
12683259f8beSChris Mason }
12693259f8beSChris Mason EXPORT_SYMBOL(writeback_inodes_sb_nr_if_idle);
12703259f8beSChris Mason 
12713259f8beSChris Mason /**
1272d8a8559cSJens Axboe  * sync_inodes_sb	-	sync sb inode pages
1273d8a8559cSJens Axboe  * @sb: the superblock
1274d8a8559cSJens Axboe  *
1275d8a8559cSJens Axboe  * This function writes and waits on any dirty inode belonging to this
1276cb9ef8d5SStefan Hajnoczi  * super_block.
1277d8a8559cSJens Axboe  */
1278b6e51316SJens Axboe void sync_inodes_sb(struct super_block *sb)
1279d8a8559cSJens Axboe {
128083ba7b07SChristoph Hellwig 	DECLARE_COMPLETION_ONSTACK(done);
128183ba7b07SChristoph Hellwig 	struct wb_writeback_work work = {
12823c4d7165SChristoph Hellwig 		.sb		= sb,
12833c4d7165SChristoph Hellwig 		.sync_mode	= WB_SYNC_ALL,
12843c4d7165SChristoph Hellwig 		.nr_pages	= LONG_MAX,
12853c4d7165SChristoph Hellwig 		.range_cyclic	= 0,
128683ba7b07SChristoph Hellwig 		.done		= &done,
12873c4d7165SChristoph Hellwig 	};
12883c4d7165SChristoph Hellwig 
1289cf37e972SChristoph Hellwig 	WARN_ON(!rwsem_is_locked(&sb->s_umount));
1290cf37e972SChristoph Hellwig 
129183ba7b07SChristoph Hellwig 	bdi_queue_work(sb->s_bdi, &work);
129283ba7b07SChristoph Hellwig 	wait_for_completion(&done);
129383ba7b07SChristoph Hellwig 
1294b6e51316SJens Axboe 	wait_sb_inodes(sb);
1295d8a8559cSJens Axboe }
1296d8a8559cSJens Axboe EXPORT_SYMBOL(sync_inodes_sb);
12971da177e4SLinus Torvalds 
12981da177e4SLinus Torvalds /**
12991da177e4SLinus Torvalds  * write_inode_now	-	write an inode to disk
13001da177e4SLinus Torvalds  * @inode: inode to write to disk
13011da177e4SLinus Torvalds  * @sync: whether the write should be synchronous or not
13021da177e4SLinus Torvalds  *
13037f04c26dSAndrea Arcangeli  * This function commits an inode to disk immediately if it is dirty. This is
13047f04c26dSAndrea Arcangeli  * primarily needed by knfsd.
13057f04c26dSAndrea Arcangeli  *
13067f04c26dSAndrea Arcangeli  * The caller must either have a ref on the inode or must have set I_WILL_FREE.
13071da177e4SLinus Torvalds  */
13081da177e4SLinus Torvalds int write_inode_now(struct inode *inode, int sync)
13091da177e4SLinus Torvalds {
13101da177e4SLinus Torvalds 	int ret;
13111da177e4SLinus Torvalds 	struct writeback_control wbc = {
13121da177e4SLinus Torvalds 		.nr_to_write = LONG_MAX,
131318914b18SMike Galbraith 		.sync_mode = sync ? WB_SYNC_ALL : WB_SYNC_NONE,
1314111ebb6eSOGAWA Hirofumi 		.range_start = 0,
1315111ebb6eSOGAWA Hirofumi 		.range_end = LLONG_MAX,
13161da177e4SLinus Torvalds 	};
13171da177e4SLinus Torvalds 
13181da177e4SLinus Torvalds 	if (!mapping_cap_writeback_dirty(inode->i_mapping))
131949364ce2SAndrew Morton 		wbc.nr_to_write = 0;
13201da177e4SLinus Torvalds 
13211da177e4SLinus Torvalds 	might_sleep();
1322a66979abSDave Chinner 	spin_lock(&inode_wb_list_lock);
13230f1b1fd8SDave Chinner 	spin_lock(&inode->i_lock);
132401c03194SChristoph Hellwig 	ret = writeback_single_inode(inode, &wbc);
13250f1b1fd8SDave Chinner 	spin_unlock(&inode->i_lock);
1326a66979abSDave Chinner 	spin_unlock(&inode_wb_list_lock);
13271da177e4SLinus Torvalds 	if (sync)
13281c0eeaf5SJoern Engel 		inode_sync_wait(inode);
13291da177e4SLinus Torvalds 	return ret;
13301da177e4SLinus Torvalds }
13311da177e4SLinus Torvalds EXPORT_SYMBOL(write_inode_now);
13321da177e4SLinus Torvalds 
13331da177e4SLinus Torvalds /**
13341da177e4SLinus Torvalds  * sync_inode - write an inode and its pages to disk.
13351da177e4SLinus Torvalds  * @inode: the inode to sync
13361da177e4SLinus Torvalds  * @wbc: controls the writeback mode
13371da177e4SLinus Torvalds  *
13381da177e4SLinus Torvalds  * sync_inode() will write an inode and its pages to disk.  It will also
13391da177e4SLinus Torvalds  * correctly update the inode on its superblock's dirty inode lists and will
13401da177e4SLinus Torvalds  * update inode->i_state.
13411da177e4SLinus Torvalds  *
13421da177e4SLinus Torvalds  * The caller must have a ref on the inode.
13431da177e4SLinus Torvalds  */
13441da177e4SLinus Torvalds int sync_inode(struct inode *inode, struct writeback_control *wbc)
13451da177e4SLinus Torvalds {
13461da177e4SLinus Torvalds 	int ret;
13471da177e4SLinus Torvalds 
1348a66979abSDave Chinner 	spin_lock(&inode_wb_list_lock);
13490f1b1fd8SDave Chinner 	spin_lock(&inode->i_lock);
135001c03194SChristoph Hellwig 	ret = writeback_single_inode(inode, wbc);
13510f1b1fd8SDave Chinner 	spin_unlock(&inode->i_lock);
1352a66979abSDave Chinner 	spin_unlock(&inode_wb_list_lock);
13531da177e4SLinus Torvalds 	return ret;
13541da177e4SLinus Torvalds }
13551da177e4SLinus Torvalds EXPORT_SYMBOL(sync_inode);
1356c3765016SChristoph Hellwig 
1357c3765016SChristoph Hellwig /**
1358c691b9d9SAndrew Morton  * sync_inode_metadata - write an inode to disk
1359c3765016SChristoph Hellwig  * @inode: the inode to sync
1360c3765016SChristoph Hellwig  * @wait: wait for I/O to complete.
1361c3765016SChristoph Hellwig  *
1362c691b9d9SAndrew Morton  * Write an inode to disk and adjust its dirty state after completion.
1363c3765016SChristoph Hellwig  *
1364c3765016SChristoph Hellwig  * Note: only writes the actual inode, no associated data or other metadata.
1365c3765016SChristoph Hellwig  */
1366c3765016SChristoph Hellwig int sync_inode_metadata(struct inode *inode, int wait)
1367c3765016SChristoph Hellwig {
1368c3765016SChristoph Hellwig 	struct writeback_control wbc = {
1369c3765016SChristoph Hellwig 		.sync_mode = wait ? WB_SYNC_ALL : WB_SYNC_NONE,
1370c3765016SChristoph Hellwig 		.nr_to_write = 0, /* metadata-only */
1371c3765016SChristoph Hellwig 	};
1372c3765016SChristoph Hellwig 
1373c3765016SChristoph Hellwig 	return sync_inode(inode, &wbc);
1374c3765016SChristoph Hellwig }
1375c3765016SChristoph Hellwig EXPORT_SYMBOL(sync_inode_metadata);
1376