xref: /openbmc/linux/fs/fs-writeback.c (revision aa373cf5)
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;
3952957fe1SH Hartley Sweeten 	unsigned int for_kupdate:1;
4052957fe1SH Hartley Sweeten 	unsigned int range_cyclic:1;
4152957fe1SH Hartley Sweeten 	unsigned int for_background:1;
42c4a77a6cSJens Axboe 
438010c3b6SJens Axboe 	struct list_head list;		/* pending work list */
4483ba7b07SChristoph Hellwig 	struct completion *done;	/* set if the caller waits */
4503ba3782SJens Axboe };
4603ba3782SJens Axboe 
47455b2864SDave Chinner /*
48455b2864SDave Chinner  * Include the creation of the trace points after defining the
49455b2864SDave Chinner  * wb_writeback_work structure so that the definition remains local to this
50455b2864SDave Chinner  * file.
51455b2864SDave Chinner  */
52455b2864SDave Chinner #define CREATE_TRACE_POINTS
53455b2864SDave Chinner #include <trace/events/writeback.h>
54455b2864SDave Chinner 
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 
876585027aSJan Kara /* Wakeup flusher thread or forker thread to fork it. Requires bdi->wb_lock. */
886585027aSJan Kara static void bdi_wakeup_flusher(struct backing_dev_info *bdi)
894195f73dSNick Piggin {
90fff5b85aSArtem Bityutskiy 	if (bdi->wb.task) {
91fff5b85aSArtem Bityutskiy 		wake_up_process(bdi->wb.task);
92fff5b85aSArtem Bityutskiy 	} else {
931da177e4SLinus Torvalds 		/*
94fff5b85aSArtem Bityutskiy 		 * The bdi thread isn't there, wake up the forker thread which
95fff5b85aSArtem Bityutskiy 		 * will create and run it.
961da177e4SLinus Torvalds 		 */
9703ba3782SJens Axboe 		wake_up_process(default_backing_dev_info.wb.task);
981da177e4SLinus Torvalds 	}
996585027aSJan Kara }
1006585027aSJan Kara 
1016585027aSJan Kara static void bdi_queue_work(struct backing_dev_info *bdi,
1026585027aSJan Kara 			   struct wb_writeback_work *work)
1036585027aSJan Kara {
1046585027aSJan Kara 	trace_writeback_queue(bdi, work);
1056585027aSJan Kara 
1066585027aSJan Kara 	spin_lock_bh(&bdi->wb_lock);
1076585027aSJan Kara 	list_add_tail(&work->list, &bdi->work_list);
1086585027aSJan Kara 	if (!bdi->wb.task)
1096585027aSJan Kara 		trace_writeback_nothread(bdi, work);
1106585027aSJan Kara 	bdi_wakeup_flusher(bdi);
1116467716aSArtem Bityutskiy 	spin_unlock_bh(&bdi->wb_lock);
11203ba3782SJens Axboe }
1131da177e4SLinus Torvalds 
11483ba7b07SChristoph Hellwig static void
11583ba7b07SChristoph Hellwig __bdi_start_writeback(struct backing_dev_info *bdi, long nr_pages,
1166585027aSJan Kara 		      bool range_cyclic)
1171da177e4SLinus Torvalds {
11883ba7b07SChristoph Hellwig 	struct wb_writeback_work *work;
11903ba3782SJens Axboe 
120bcddc3f0SJens Axboe 	/*
121bcddc3f0SJens Axboe 	 * This is WB_SYNC_NONE writeback, so if allocation fails just
122bcddc3f0SJens Axboe 	 * wakeup the thread for old dirty data writeback
123bcddc3f0SJens Axboe 	 */
12483ba7b07SChristoph Hellwig 	work = kzalloc(sizeof(*work), GFP_ATOMIC);
12583ba7b07SChristoph Hellwig 	if (!work) {
126455b2864SDave Chinner 		if (bdi->wb.task) {
127455b2864SDave Chinner 			trace_writeback_nowork(bdi);
12883ba7b07SChristoph Hellwig 			wake_up_process(bdi->wb.task);
129455b2864SDave Chinner 		}
13083ba7b07SChristoph Hellwig 		return;
13183ba7b07SChristoph Hellwig 	}
13283ba7b07SChristoph Hellwig 
13383ba7b07SChristoph Hellwig 	work->sync_mode	= WB_SYNC_NONE;
13483ba7b07SChristoph Hellwig 	work->nr_pages	= nr_pages;
13583ba7b07SChristoph Hellwig 	work->range_cyclic = range_cyclic;
13683ba7b07SChristoph Hellwig 
137f11fcae8SJens Axboe 	bdi_queue_work(bdi, work);
13803ba3782SJens Axboe }
139b6e51316SJens Axboe 
140b6e51316SJens Axboe /**
141b6e51316SJens Axboe  * bdi_start_writeback - start writeback
142b6e51316SJens Axboe  * @bdi: the backing device to write from
143b6e51316SJens Axboe  * @nr_pages: the number of pages to write
144b6e51316SJens Axboe  *
145b6e51316SJens Axboe  * Description:
146b6e51316SJens Axboe  *   This does WB_SYNC_NONE opportunistic writeback. The IO is only
147b6e51316SJens Axboe  *   started when this function returns, we make no guarentees on
1480e3c9a22SJens Axboe  *   completion. Caller need not hold sb s_umount semaphore.
149b6e51316SJens Axboe  *
150b6e51316SJens Axboe  */
151c5444198SChristoph Hellwig void bdi_start_writeback(struct backing_dev_info *bdi, long nr_pages)
152b6e51316SJens Axboe {
1536585027aSJan Kara 	__bdi_start_writeback(bdi, nr_pages, true);
154d3ddec76SWu Fengguang }
155d3ddec76SWu Fengguang 
156c5444198SChristoph Hellwig /**
157c5444198SChristoph Hellwig  * bdi_start_background_writeback - start background writeback
158c5444198SChristoph Hellwig  * @bdi: the backing device to write from
159c5444198SChristoph Hellwig  *
160c5444198SChristoph Hellwig  * Description:
1616585027aSJan Kara  *   This makes sure WB_SYNC_NONE background writeback happens. When
1626585027aSJan Kara  *   this function returns, it is only guaranteed that for given BDI
1636585027aSJan Kara  *   some IO is happening if we are over background dirty threshold.
1646585027aSJan Kara  *   Caller need not hold sb s_umount semaphore.
165c5444198SChristoph Hellwig  */
166c5444198SChristoph Hellwig void bdi_start_background_writeback(struct backing_dev_info *bdi)
167c5444198SChristoph Hellwig {
1686585027aSJan Kara 	/*
1696585027aSJan Kara 	 * We just wake up the flusher thread. It will perform background
1706585027aSJan Kara 	 * writeback as soon as there is no other work to do.
1716585027aSJan Kara 	 */
17271927e84SWu Fengguang 	trace_writeback_wake_background(bdi);
1736585027aSJan Kara 	spin_lock_bh(&bdi->wb_lock);
1746585027aSJan Kara 	bdi_wakeup_flusher(bdi);
1756585027aSJan Kara 	spin_unlock_bh(&bdi->wb_lock);
1761da177e4SLinus Torvalds }
1771da177e4SLinus Torvalds 
1781da177e4SLinus Torvalds /*
1796610a0bcSAndrew Morton  * Redirty an inode: set its when-it-was dirtied timestamp and move it to the
1806610a0bcSAndrew Morton  * furthest end of its superblock's dirty-inode list.
1816610a0bcSAndrew Morton  *
1826610a0bcSAndrew Morton  * Before stamping the inode's ->dirtied_when, we check to see whether it is
18366f3b8e2SJens Axboe  * already the most-recently-dirtied inode on the b_dirty list.  If that is
1846610a0bcSAndrew Morton  * the case then the inode must have been redirtied while it was being written
1856610a0bcSAndrew Morton  * out and we don't reset its dirtied_when.
1866610a0bcSAndrew Morton  */
1876610a0bcSAndrew Morton static void redirty_tail(struct inode *inode)
1886610a0bcSAndrew Morton {
18903ba3782SJens Axboe 	struct bdi_writeback *wb = &inode_to_bdi(inode)->wb;
1906610a0bcSAndrew Morton 
19103ba3782SJens Axboe 	if (!list_empty(&wb->b_dirty)) {
19266f3b8e2SJens Axboe 		struct inode *tail;
1936610a0bcSAndrew Morton 
1947ccf19a8SNick Piggin 		tail = wb_inode(wb->b_dirty.next);
19566f3b8e2SJens Axboe 		if (time_before(inode->dirtied_when, tail->dirtied_when))
1966610a0bcSAndrew Morton 			inode->dirtied_when = jiffies;
1976610a0bcSAndrew Morton 	}
1987ccf19a8SNick Piggin 	list_move(&inode->i_wb_list, &wb->b_dirty);
1996610a0bcSAndrew Morton }
2006610a0bcSAndrew Morton 
2016610a0bcSAndrew Morton /*
20266f3b8e2SJens Axboe  * requeue inode for re-scanning after bdi->b_io list is exhausted.
203c986d1e2SAndrew Morton  */
2040e0f4fc2SKen Chen static void requeue_io(struct inode *inode)
205c986d1e2SAndrew Morton {
20603ba3782SJens Axboe 	struct bdi_writeback *wb = &inode_to_bdi(inode)->wb;
20703ba3782SJens Axboe 
2087ccf19a8SNick Piggin 	list_move(&inode->i_wb_list, &wb->b_more_io);
209c986d1e2SAndrew Morton }
210c986d1e2SAndrew Morton 
2111c0eeaf5SJoern Engel static void inode_sync_complete(struct inode *inode)
2121c0eeaf5SJoern Engel {
2131c0eeaf5SJoern Engel 	/*
2141c0eeaf5SJoern Engel 	 * Prevent speculative execution through spin_unlock(&inode_lock);
2151c0eeaf5SJoern Engel 	 */
2161c0eeaf5SJoern Engel 	smp_mb();
2171c0eeaf5SJoern Engel 	wake_up_bit(&inode->i_state, __I_SYNC);
2181c0eeaf5SJoern Engel }
2191c0eeaf5SJoern Engel 
220d2caa3c5SJeff Layton static bool inode_dirtied_after(struct inode *inode, unsigned long t)
221d2caa3c5SJeff Layton {
222d2caa3c5SJeff Layton 	bool ret = time_after(inode->dirtied_when, t);
223d2caa3c5SJeff Layton #ifndef CONFIG_64BIT
224d2caa3c5SJeff Layton 	/*
225d2caa3c5SJeff Layton 	 * For inodes being constantly redirtied, dirtied_when can get stuck.
226d2caa3c5SJeff Layton 	 * It _appears_ to be in the future, but is actually in distant past.
227d2caa3c5SJeff Layton 	 * This test is necessary to prevent such wrapped-around relative times
2285b0830cbSJens Axboe 	 * from permanently stopping the whole bdi writeback.
229d2caa3c5SJeff Layton 	 */
230d2caa3c5SJeff Layton 	ret = ret && time_before_eq(inode->dirtied_when, jiffies);
231d2caa3c5SJeff Layton #endif
232d2caa3c5SJeff Layton 	return ret;
233d2caa3c5SJeff Layton }
234d2caa3c5SJeff Layton 
235c986d1e2SAndrew Morton /*
2362c136579SFengguang Wu  * Move expired dirty inodes from @delaying_queue to @dispatch_queue.
2372c136579SFengguang Wu  */
2382c136579SFengguang Wu static void move_expired_inodes(struct list_head *delaying_queue,
2392c136579SFengguang Wu 			       struct list_head *dispatch_queue,
2402c136579SFengguang Wu 				unsigned long *older_than_this)
2412c136579SFengguang Wu {
2425c03449dSShaohua Li 	LIST_HEAD(tmp);
2435c03449dSShaohua Li 	struct list_head *pos, *node;
244cf137307SJens Axboe 	struct super_block *sb = NULL;
2455c03449dSShaohua Li 	struct inode *inode;
246cf137307SJens Axboe 	int do_sb_sort = 0;
2475c03449dSShaohua Li 
2482c136579SFengguang Wu 	while (!list_empty(delaying_queue)) {
2497ccf19a8SNick Piggin 		inode = wb_inode(delaying_queue->prev);
2502c136579SFengguang Wu 		if (older_than_this &&
251d2caa3c5SJeff Layton 		    inode_dirtied_after(inode, *older_than_this))
2522c136579SFengguang Wu 			break;
253cf137307SJens Axboe 		if (sb && sb != inode->i_sb)
254cf137307SJens Axboe 			do_sb_sort = 1;
255cf137307SJens Axboe 		sb = inode->i_sb;
2567ccf19a8SNick Piggin 		list_move(&inode->i_wb_list, &tmp);
2575c03449dSShaohua Li 	}
2585c03449dSShaohua Li 
259cf137307SJens Axboe 	/* just one sb in list, splice to dispatch_queue and we're done */
260cf137307SJens Axboe 	if (!do_sb_sort) {
261cf137307SJens Axboe 		list_splice(&tmp, dispatch_queue);
262cf137307SJens Axboe 		return;
263cf137307SJens Axboe 	}
264cf137307SJens Axboe 
2655c03449dSShaohua Li 	/* Move inodes from one superblock together */
2665c03449dSShaohua Li 	while (!list_empty(&tmp)) {
2677ccf19a8SNick Piggin 		sb = wb_inode(tmp.prev)->i_sb;
2685c03449dSShaohua Li 		list_for_each_prev_safe(pos, node, &tmp) {
2697ccf19a8SNick Piggin 			inode = wb_inode(pos);
2705c03449dSShaohua Li 			if (inode->i_sb == sb)
2717ccf19a8SNick Piggin 				list_move(&inode->i_wb_list, dispatch_queue);
2722c136579SFengguang Wu 		}
2732c136579SFengguang Wu 	}
2745c03449dSShaohua Li }
2752c136579SFengguang Wu 
2762c136579SFengguang Wu /*
2772c136579SFengguang Wu  * Queue all expired dirty inodes for io, eldest first.
2784ea879b9SWu Fengguang  * Before
2794ea879b9SWu Fengguang  *         newly dirtied     b_dirty    b_io    b_more_io
2804ea879b9SWu Fengguang  *         =============>    gf         edc     BA
2814ea879b9SWu Fengguang  * After
2824ea879b9SWu Fengguang  *         newly dirtied     b_dirty    b_io    b_more_io
2834ea879b9SWu Fengguang  *         =============>    g          fBAedc
2844ea879b9SWu Fengguang  *                                           |
2854ea879b9SWu Fengguang  *                                           +--> dequeue for IO
2862c136579SFengguang Wu  */
28703ba3782SJens Axboe static void queue_io(struct bdi_writeback *wb, unsigned long *older_than_this)
2882c136579SFengguang Wu {
2894ea879b9SWu Fengguang 	list_splice_init(&wb->b_more_io, &wb->b_io);
29003ba3782SJens Axboe 	move_expired_inodes(&wb->b_dirty, &wb->b_io, older_than_this);
29166f3b8e2SJens Axboe }
29266f3b8e2SJens Axboe 
293a9185b41SChristoph Hellwig static int write_inode(struct inode *inode, struct writeback_control *wbc)
29466f3b8e2SJens Axboe {
29503ba3782SJens Axboe 	if (inode->i_sb->s_op->write_inode && !is_bad_inode(inode))
296a9185b41SChristoph Hellwig 		return inode->i_sb->s_op->write_inode(inode, wbc);
29703ba3782SJens Axboe 	return 0;
29866f3b8e2SJens Axboe }
29908d8e974SFengguang Wu 
3002c136579SFengguang Wu /*
30101c03194SChristoph Hellwig  * Wait for writeback on an inode to complete.
30201c03194SChristoph Hellwig  */
30301c03194SChristoph Hellwig static void inode_wait_for_writeback(struct inode *inode)
30401c03194SChristoph Hellwig {
30501c03194SChristoph Hellwig 	DEFINE_WAIT_BIT(wq, &inode->i_state, __I_SYNC);
30601c03194SChristoph Hellwig 	wait_queue_head_t *wqh;
30701c03194SChristoph Hellwig 
30801c03194SChristoph Hellwig 	wqh = bit_waitqueue(&inode->i_state, __I_SYNC);
30958a9d3d8SRichard Kennedy 	 while (inode->i_state & I_SYNC) {
31001c03194SChristoph Hellwig 		spin_unlock(&inode_lock);
31101c03194SChristoph Hellwig 		__wait_on_bit(wqh, &wq, inode_wait, TASK_UNINTERRUPTIBLE);
31201c03194SChristoph Hellwig 		spin_lock(&inode_lock);
31358a9d3d8SRichard Kennedy 	}
31401c03194SChristoph Hellwig }
31501c03194SChristoph Hellwig 
31601c03194SChristoph Hellwig /*
31701c03194SChristoph Hellwig  * Write out an inode's dirty pages.  Called under inode_lock.  Either the
31801c03194SChristoph Hellwig  * caller has ref on the inode (either via __iget or via syscall against an fd)
31901c03194SChristoph Hellwig  * or the inode has I_WILL_FREE set (via generic_forget_inode)
32001c03194SChristoph Hellwig  *
3211da177e4SLinus Torvalds  * If `wait' is set, wait on the writeout.
3221da177e4SLinus Torvalds  *
3231da177e4SLinus Torvalds  * The whole writeout design is quite complex and fragile.  We want to avoid
3241da177e4SLinus Torvalds  * starvation of particular inodes when others are being redirtied, prevent
3251da177e4SLinus Torvalds  * livelocks, etc.
3261da177e4SLinus Torvalds  *
3271da177e4SLinus Torvalds  * Called under inode_lock.
3281da177e4SLinus Torvalds  */
3291da177e4SLinus Torvalds static int
33001c03194SChristoph Hellwig writeback_single_inode(struct inode *inode, struct writeback_control *wbc)
3311da177e4SLinus Torvalds {
3321da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
33301c03194SChristoph Hellwig 	unsigned dirty;
3341da177e4SLinus Torvalds 	int ret;
3351da177e4SLinus Torvalds 
33601c03194SChristoph Hellwig 	if (!atomic_read(&inode->i_count))
33701c03194SChristoph Hellwig 		WARN_ON(!(inode->i_state & (I_WILL_FREE|I_FREEING)));
33801c03194SChristoph Hellwig 	else
33901c03194SChristoph Hellwig 		WARN_ON(inode->i_state & I_WILL_FREE);
34001c03194SChristoph Hellwig 
34101c03194SChristoph Hellwig 	if (inode->i_state & I_SYNC) {
34201c03194SChristoph Hellwig 		/*
34301c03194SChristoph Hellwig 		 * If this inode is locked for writeback and we are not doing
34466f3b8e2SJens Axboe 		 * writeback-for-data-integrity, move it to b_more_io so that
34501c03194SChristoph Hellwig 		 * writeback can proceed with the other inodes on s_io.
34601c03194SChristoph Hellwig 		 *
34701c03194SChristoph Hellwig 		 * We'll have another go at writing back this inode when we
34866f3b8e2SJens Axboe 		 * completed a full scan of b_io.
34901c03194SChristoph Hellwig 		 */
350a9185b41SChristoph Hellwig 		if (wbc->sync_mode != WB_SYNC_ALL) {
35101c03194SChristoph Hellwig 			requeue_io(inode);
35201c03194SChristoph Hellwig 			return 0;
35301c03194SChristoph Hellwig 		}
35401c03194SChristoph Hellwig 
35501c03194SChristoph Hellwig 		/*
35601c03194SChristoph Hellwig 		 * It's a data-integrity sync.  We must wait.
35701c03194SChristoph Hellwig 		 */
35801c03194SChristoph Hellwig 		inode_wait_for_writeback(inode);
35901c03194SChristoph Hellwig 	}
36001c03194SChristoph Hellwig 
3611c0eeaf5SJoern Engel 	BUG_ON(inode->i_state & I_SYNC);
3621da177e4SLinus Torvalds 
3635547e8aaSDmitry Monakhov 	/* Set I_SYNC, reset I_DIRTY_PAGES */
3641c0eeaf5SJoern Engel 	inode->i_state |= I_SYNC;
3655547e8aaSDmitry Monakhov 	inode->i_state &= ~I_DIRTY_PAGES;
3661da177e4SLinus Torvalds 	spin_unlock(&inode_lock);
3671da177e4SLinus Torvalds 
3681da177e4SLinus Torvalds 	ret = do_writepages(mapping, wbc);
3691da177e4SLinus Torvalds 
37026821ed4SChristoph Hellwig 	/*
37126821ed4SChristoph Hellwig 	 * Make sure to wait on the data before writing out the metadata.
37226821ed4SChristoph Hellwig 	 * This is important for filesystems that modify metadata on data
37326821ed4SChristoph Hellwig 	 * I/O completion.
37426821ed4SChristoph Hellwig 	 */
375a9185b41SChristoph Hellwig 	if (wbc->sync_mode == WB_SYNC_ALL) {
37626821ed4SChristoph Hellwig 		int err = filemap_fdatawait(mapping);
3771da177e4SLinus Torvalds 		if (ret == 0)
3781da177e4SLinus Torvalds 			ret = err;
3791da177e4SLinus Torvalds 	}
3801da177e4SLinus Torvalds 
3815547e8aaSDmitry Monakhov 	/*
3825547e8aaSDmitry Monakhov 	 * Some filesystems may redirty the inode during the writeback
3835547e8aaSDmitry Monakhov 	 * due to delalloc, clear dirty metadata flags right before
3845547e8aaSDmitry Monakhov 	 * write_inode()
3855547e8aaSDmitry Monakhov 	 */
3865547e8aaSDmitry Monakhov 	spin_lock(&inode_lock);
3875547e8aaSDmitry Monakhov 	dirty = inode->i_state & I_DIRTY;
3885547e8aaSDmitry Monakhov 	inode->i_state &= ~(I_DIRTY_SYNC | I_DIRTY_DATASYNC);
3895547e8aaSDmitry Monakhov 	spin_unlock(&inode_lock);
39026821ed4SChristoph Hellwig 	/* Don't write the inode if only I_DIRTY_PAGES was set */
39126821ed4SChristoph Hellwig 	if (dirty & (I_DIRTY_SYNC | I_DIRTY_DATASYNC)) {
392a9185b41SChristoph Hellwig 		int err = write_inode(inode, wbc);
3931da177e4SLinus Torvalds 		if (ret == 0)
3941da177e4SLinus Torvalds 			ret = err;
3951da177e4SLinus Torvalds 	}
3961da177e4SLinus Torvalds 
3971da177e4SLinus Torvalds 	spin_lock(&inode_lock);
3981c0eeaf5SJoern Engel 	inode->i_state &= ~I_SYNC;
399a4ffdde6SAl Viro 	if (!(inode->i_state & I_FREEING)) {
40023539afcSWu Fengguang 		if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) {
4011da177e4SLinus Torvalds 			/*
4021da177e4SLinus Torvalds 			 * We didn't write back all the pages.  nfs_writepages()
403a50aeb40SWu Fengguang 			 * sometimes bales out without doing anything.
4041da177e4SLinus Torvalds 			 */
4051da177e4SLinus Torvalds 			inode->i_state |= I_DIRTY_PAGES;
4068bc3be27SFengguang Wu 			if (wbc->nr_to_write <= 0) {
4078bc3be27SFengguang Wu 				/*
4088bc3be27SFengguang Wu 				 * slice used up: queue for next turn
4098bc3be27SFengguang Wu 				 */
4100e0f4fc2SKen Chen 				requeue_io(inode);
4111da177e4SLinus Torvalds 			} else {
4121da177e4SLinus Torvalds 				/*
413a50aeb40SWu Fengguang 				 * Writeback blocked by something other than
414a50aeb40SWu Fengguang 				 * congestion. Delay the inode for some time to
415a50aeb40SWu Fengguang 				 * avoid spinning on the CPU (100% iowait)
416a50aeb40SWu Fengguang 				 * retrying writeback of the dirty page/inode
417a50aeb40SWu Fengguang 				 * that cannot be performed immediately.
4188bc3be27SFengguang Wu 				 */
4198bc3be27SFengguang Wu 				redirty_tail(inode);
4208bc3be27SFengguang Wu 			}
42123539afcSWu Fengguang 		} else if (inode->i_state & I_DIRTY) {
42223539afcSWu Fengguang 			/*
42323539afcSWu Fengguang 			 * Filesystems can dirty the inode during writeback
42423539afcSWu Fengguang 			 * operations, such as delayed allocation during
42523539afcSWu Fengguang 			 * submission or metadata updates after data IO
42623539afcSWu Fengguang 			 * completion.
42723539afcSWu Fengguang 			 */
42823539afcSWu Fengguang 			redirty_tail(inode);
4291da177e4SLinus Torvalds 		} else {
4301da177e4SLinus Torvalds 			/*
4319e38d86fSNick Piggin 			 * The inode is clean.  At this point we either have
4329e38d86fSNick Piggin 			 * a reference to the inode or it's on it's way out.
4339e38d86fSNick Piggin 			 * No need to add it back to the LRU.
4341da177e4SLinus Torvalds 			 */
4357ccf19a8SNick Piggin 			list_del_init(&inode->i_wb_list);
4361da177e4SLinus Torvalds 		}
4371da177e4SLinus Torvalds 	}
4381c0eeaf5SJoern Engel 	inode_sync_complete(inode);
4391da177e4SLinus Torvalds 	return ret;
4401da177e4SLinus Torvalds }
4411da177e4SLinus Torvalds 
44203ba3782SJens Axboe /*
443d19de7edSChristoph Hellwig  * For background writeback the caller does not have the sb pinned
44403ba3782SJens Axboe  * before calling writeback. So make sure that we do pin it, so it doesn't
44503ba3782SJens Axboe  * go away while we are writing inodes from it.
44603ba3782SJens Axboe  */
447d19de7edSChristoph Hellwig static bool pin_sb_for_writeback(struct super_block *sb)
4481da177e4SLinus Torvalds {
44903ba3782SJens Axboe 	spin_lock(&sb_lock);
45029cb4859SChristoph Hellwig 	if (list_empty(&sb->s_instances)) {
45103ba3782SJens Axboe 		spin_unlock(&sb_lock);
45229cb4859SChristoph Hellwig 		return false;
45303ba3782SJens Axboe 	}
45429cb4859SChristoph Hellwig 
45529cb4859SChristoph Hellwig 	sb->s_count++;
45629cb4859SChristoph Hellwig 	spin_unlock(&sb_lock);
45729cb4859SChristoph Hellwig 
45829cb4859SChristoph Hellwig 	if (down_read_trylock(&sb->s_umount)) {
45929cb4859SChristoph Hellwig 		if (sb->s_root)
46029cb4859SChristoph Hellwig 			return true;
46103ba3782SJens Axboe 		up_read(&sb->s_umount);
46203ba3782SJens Axboe 	}
46329cb4859SChristoph Hellwig 
46429cb4859SChristoph Hellwig 	put_super(sb);
465d19de7edSChristoph Hellwig 	return false;
46603ba3782SJens Axboe }
46703ba3782SJens Axboe 
468f11c9c5cSEdward Shishkin /*
469f11c9c5cSEdward Shishkin  * Write a portion of b_io inodes which belong to @sb.
470edadfb10SChristoph Hellwig  *
471edadfb10SChristoph Hellwig  * If @only_this_sb is true, then find and write all such
472f11c9c5cSEdward Shishkin  * inodes. Otherwise write only ones which go sequentially
473f11c9c5cSEdward Shishkin  * in reverse order.
474edadfb10SChristoph Hellwig  *
475f11c9c5cSEdward Shishkin  * Return 1, if the caller writeback routine should be
476f11c9c5cSEdward Shishkin  * interrupted. Otherwise return 0.
477f11c9c5cSEdward Shishkin  */
478edadfb10SChristoph Hellwig static int writeback_sb_inodes(struct super_block *sb, struct bdi_writeback *wb,
479edadfb10SChristoph Hellwig 		struct writeback_control *wbc, bool only_this_sb)
48003ba3782SJens Axboe {
48103ba3782SJens Axboe 	while (!list_empty(&wb->b_io)) {
482f11c9c5cSEdward Shishkin 		long pages_skipped;
4837ccf19a8SNick Piggin 		struct inode *inode = wb_inode(wb->b_io.prev);
484edadfb10SChristoph Hellwig 
485edadfb10SChristoph Hellwig 		if (inode->i_sb != sb) {
486edadfb10SChristoph Hellwig 			if (only_this_sb) {
487edadfb10SChristoph Hellwig 				/*
488edadfb10SChristoph Hellwig 				 * We only want to write back data for this
489edadfb10SChristoph Hellwig 				 * superblock, move all inodes not belonging
490edadfb10SChristoph Hellwig 				 * to it back onto the dirty list.
491edadfb10SChristoph Hellwig 				 */
49266f3b8e2SJens Axboe 				redirty_tail(inode);
49366f3b8e2SJens Axboe 				continue;
49466f3b8e2SJens Axboe 			}
495edadfb10SChristoph Hellwig 
496edadfb10SChristoph Hellwig 			/*
497edadfb10SChristoph Hellwig 			 * The inode belongs to a different superblock.
498edadfb10SChristoph Hellwig 			 * Bounce back to the caller to unpin this and
499edadfb10SChristoph Hellwig 			 * pin the next superblock.
500edadfb10SChristoph Hellwig 			 */
501f11c9c5cSEdward Shishkin 			return 0;
502edadfb10SChristoph Hellwig 		}
503edadfb10SChristoph Hellwig 
5049843b76aSChristoph Hellwig 		/*
5059843b76aSChristoph Hellwig 		 * Don't bother with new inodes or inodes beeing freed, first
5069843b76aSChristoph Hellwig 		 * kind does not need peridic writeout yet, and for the latter
5079843b76aSChristoph Hellwig 		 * kind writeout is handled by the freer.
5089843b76aSChristoph Hellwig 		 */
5099843b76aSChristoph Hellwig 		if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) {
5107ef0d737SNick Piggin 			requeue_io(inode);
5117ef0d737SNick Piggin 			continue;
5127ef0d737SNick Piggin 		}
5139843b76aSChristoph Hellwig 
514d2caa3c5SJeff Layton 		/*
515d2caa3c5SJeff Layton 		 * Was this inode dirtied after sync_sb_inodes was called?
516d2caa3c5SJeff Layton 		 * This keeps sync from extra jobs and livelock.
517d2caa3c5SJeff Layton 		 */
518f11c9c5cSEdward Shishkin 		if (inode_dirtied_after(inode, wbc->wb_start))
519f11c9c5cSEdward Shishkin 			return 1;
5201da177e4SLinus Torvalds 
5211da177e4SLinus Torvalds 		__iget(inode);
5221da177e4SLinus Torvalds 		pages_skipped = wbc->pages_skipped;
52301c03194SChristoph Hellwig 		writeback_single_inode(inode, wbc);
5241da177e4SLinus Torvalds 		if (wbc->pages_skipped != pages_skipped) {
5251da177e4SLinus Torvalds 			/*
5261da177e4SLinus Torvalds 			 * writeback is not making progress due to locked
5271da177e4SLinus Torvalds 			 * buffers.  Skip this inode for now.
5281da177e4SLinus Torvalds 			 */
529f57b9b7bSAndrew Morton 			redirty_tail(inode);
5301da177e4SLinus Torvalds 		}
5311da177e4SLinus Torvalds 		spin_unlock(&inode_lock);
5321da177e4SLinus Torvalds 		iput(inode);
5334ffc8444SOGAWA Hirofumi 		cond_resched();
5341da177e4SLinus Torvalds 		spin_lock(&inode_lock);
5358bc3be27SFengguang Wu 		if (wbc->nr_to_write <= 0) {
5368bc3be27SFengguang Wu 			wbc->more_io = 1;
537f11c9c5cSEdward Shishkin 			return 1;
5381da177e4SLinus Torvalds 		}
53903ba3782SJens Axboe 		if (!list_empty(&wb->b_more_io))
5408bc3be27SFengguang Wu 			wbc->more_io = 1;
5418bc3be27SFengguang Wu 	}
542f11c9c5cSEdward Shishkin 	/* b_io is empty */
543f11c9c5cSEdward Shishkin 	return 1;
544f11c9c5cSEdward Shishkin }
54538f21977SNick Piggin 
5469c3a8ee8SChristoph Hellwig void writeback_inodes_wb(struct bdi_writeback *wb,
547f11c9c5cSEdward Shishkin 		struct writeback_control *wbc)
548f11c9c5cSEdward Shishkin {
549f11c9c5cSEdward Shishkin 	int ret = 0;
5509ecc2738SJens Axboe 
5517624ee72SJan Kara 	if (!wbc->wb_start)
552f11c9c5cSEdward Shishkin 		wbc->wb_start = jiffies; /* livelock avoidance */
553f11c9c5cSEdward Shishkin 	spin_lock(&inode_lock);
554f11c9c5cSEdward Shishkin 	if (!wbc->for_kupdate || list_empty(&wb->b_io))
555f11c9c5cSEdward Shishkin 		queue_io(wb, wbc->older_than_this);
556f11c9c5cSEdward Shishkin 
557f11c9c5cSEdward Shishkin 	while (!list_empty(&wb->b_io)) {
5587ccf19a8SNick Piggin 		struct inode *inode = wb_inode(wb->b_io.prev);
559f11c9c5cSEdward Shishkin 		struct super_block *sb = inode->i_sb;
560f11c9c5cSEdward Shishkin 
561334132aeSChristoph Hellwig 		if (!pin_sb_for_writeback(sb)) {
562334132aeSChristoph Hellwig 			requeue_io(inode);
563d19de7edSChristoph Hellwig 			continue;
564334132aeSChristoph Hellwig 		}
565edadfb10SChristoph Hellwig 		ret = writeback_sb_inodes(sb, wb, wbc, false);
566d19de7edSChristoph Hellwig 		drop_super(sb);
567f11c9c5cSEdward Shishkin 
568f11c9c5cSEdward Shishkin 		if (ret)
569f11c9c5cSEdward Shishkin 			break;
570f11c9c5cSEdward Shishkin 	}
57166f3b8e2SJens Axboe 	spin_unlock(&inode_lock);
57266f3b8e2SJens Axboe 	/* Leave any unwritten inodes on b_io */
57366f3b8e2SJens Axboe }
57466f3b8e2SJens Axboe 
575edadfb10SChristoph Hellwig static void __writeback_inodes_sb(struct super_block *sb,
576edadfb10SChristoph Hellwig 		struct bdi_writeback *wb, struct writeback_control *wbc)
577edadfb10SChristoph Hellwig {
578edadfb10SChristoph Hellwig 	WARN_ON(!rwsem_is_locked(&sb->s_umount));
579edadfb10SChristoph Hellwig 
580edadfb10SChristoph Hellwig 	spin_lock(&inode_lock);
581edadfb10SChristoph Hellwig 	if (!wbc->for_kupdate || list_empty(&wb->b_io))
582edadfb10SChristoph Hellwig 		queue_io(wb, wbc->older_than_this);
583edadfb10SChristoph Hellwig 	writeback_sb_inodes(sb, wb, wbc, true);
584edadfb10SChristoph Hellwig 	spin_unlock(&inode_lock);
585edadfb10SChristoph Hellwig }
586edadfb10SChristoph Hellwig 
58703ba3782SJens Axboe /*
58803ba3782SJens Axboe  * The maximum number of pages to writeout in a single bdi flush/kupdate
58903ba3782SJens Axboe  * operation.  We do this so we don't hold I_SYNC against an inode for
59003ba3782SJens Axboe  * enormous amounts of time, which would block a userspace task which has
59103ba3782SJens Axboe  * been forced to throttle against that inode.  Also, the code reevaluates
59203ba3782SJens Axboe  * the dirty each time it has written this many pages.
59303ba3782SJens Axboe  */
59403ba3782SJens Axboe #define MAX_WRITEBACK_PAGES     1024
59503ba3782SJens Axboe 
59603ba3782SJens Axboe static inline bool over_bground_thresh(void)
59703ba3782SJens Axboe {
59803ba3782SJens Axboe 	unsigned long background_thresh, dirty_thresh;
59903ba3782SJens Axboe 
60016c4042fSWu Fengguang 	global_dirty_limits(&background_thresh, &dirty_thresh);
60103ba3782SJens Axboe 
60203ba3782SJens Axboe 	return (global_page_state(NR_FILE_DIRTY) +
6034cbec4c8SWu Fengguang 		global_page_state(NR_UNSTABLE_NFS) > background_thresh);
60403ba3782SJens Axboe }
60503ba3782SJens Axboe 
60603ba3782SJens Axboe /*
60703ba3782SJens Axboe  * Explicit flushing or periodic writeback of "old" data.
60803ba3782SJens Axboe  *
60903ba3782SJens Axboe  * Define "old": the first time one of an inode's pages is dirtied, we mark the
61003ba3782SJens Axboe  * dirtying-time in the inode's address_space.  So this periodic writeback code
61103ba3782SJens Axboe  * just walks the superblock inode list, writing back any inodes which are
61203ba3782SJens Axboe  * older than a specific point in time.
61303ba3782SJens Axboe  *
61403ba3782SJens Axboe  * Try to run once per dirty_writeback_interval.  But if a writeback event
61503ba3782SJens Axboe  * takes longer than a dirty_writeback_interval interval, then leave a
61603ba3782SJens Axboe  * one-second gap.
61703ba3782SJens Axboe  *
61803ba3782SJens Axboe  * older_than_this takes precedence over nr_to_write.  So we'll only write back
61903ba3782SJens Axboe  * all dirty pages if they are all attached to "old" mappings.
62003ba3782SJens Axboe  */
621c4a77a6cSJens Axboe static long wb_writeback(struct bdi_writeback *wb,
62283ba7b07SChristoph Hellwig 			 struct wb_writeback_work *work)
62303ba3782SJens Axboe {
62403ba3782SJens Axboe 	struct writeback_control wbc = {
62583ba7b07SChristoph Hellwig 		.sync_mode		= work->sync_mode,
62603ba3782SJens Axboe 		.older_than_this	= NULL,
62783ba7b07SChristoph Hellwig 		.for_kupdate		= work->for_kupdate,
62883ba7b07SChristoph Hellwig 		.for_background		= work->for_background,
62983ba7b07SChristoph Hellwig 		.range_cyclic		= work->range_cyclic,
63003ba3782SJens Axboe 	};
63103ba3782SJens Axboe 	unsigned long oldest_jif;
63203ba3782SJens Axboe 	long wrote = 0;
633a5989bdcSJan Kara 	struct inode *inode;
63403ba3782SJens Axboe 
63503ba3782SJens Axboe 	if (wbc.for_kupdate) {
63603ba3782SJens Axboe 		wbc.older_than_this = &oldest_jif;
63703ba3782SJens Axboe 		oldest_jif = jiffies -
63803ba3782SJens Axboe 				msecs_to_jiffies(dirty_expire_interval * 10);
63903ba3782SJens Axboe 	}
640c4a77a6cSJens Axboe 	if (!wbc.range_cyclic) {
641c4a77a6cSJens Axboe 		wbc.range_start = 0;
642c4a77a6cSJens Axboe 		wbc.range_end = LLONG_MAX;
643c4a77a6cSJens Axboe 	}
64403ba3782SJens Axboe 
6457624ee72SJan Kara 	wbc.wb_start = jiffies; /* livelock avoidance */
64603ba3782SJens Axboe 	for (;;) {
64703ba3782SJens Axboe 		/*
648d3ddec76SWu Fengguang 		 * Stop writeback when nr_pages has been consumed
64903ba3782SJens Axboe 		 */
65083ba7b07SChristoph Hellwig 		if (work->nr_pages <= 0)
65103ba3782SJens Axboe 			break;
65203ba3782SJens Axboe 
65303ba3782SJens Axboe 		/*
654aa373cf5SJan Kara 		 * Background writeout and kupdate-style writeback may
655aa373cf5SJan Kara 		 * run forever. Stop them if there is other work to do
656aa373cf5SJan Kara 		 * so that e.g. sync can proceed. They'll be restarted
657aa373cf5SJan Kara 		 * after the other works are all done.
658aa373cf5SJan Kara 		 */
659aa373cf5SJan Kara 		if ((work->for_background || work->for_kupdate) &&
660aa373cf5SJan Kara 		    !list_empty(&wb->bdi->work_list))
661aa373cf5SJan Kara 			break;
662aa373cf5SJan Kara 
663aa373cf5SJan Kara 		/*
664d3ddec76SWu Fengguang 		 * For background writeout, stop when we are below the
665d3ddec76SWu Fengguang 		 * background dirty threshold
66603ba3782SJens Axboe 		 */
66783ba7b07SChristoph Hellwig 		if (work->for_background && !over_bground_thresh())
66803ba3782SJens Axboe 			break;
66903ba3782SJens Axboe 
67003ba3782SJens Axboe 		wbc.more_io = 0;
67103ba3782SJens Axboe 		wbc.nr_to_write = MAX_WRITEBACK_PAGES;
67203ba3782SJens Axboe 		wbc.pages_skipped = 0;
673028c2dd1SDave Chinner 
674028c2dd1SDave Chinner 		trace_wbc_writeback_start(&wbc, wb->bdi);
67583ba7b07SChristoph Hellwig 		if (work->sb)
67683ba7b07SChristoph Hellwig 			__writeback_inodes_sb(work->sb, wb, &wbc);
677edadfb10SChristoph Hellwig 		else
67803ba3782SJens Axboe 			writeback_inodes_wb(wb, &wbc);
679028c2dd1SDave Chinner 		trace_wbc_writeback_written(&wbc, wb->bdi);
680028c2dd1SDave Chinner 
68183ba7b07SChristoph Hellwig 		work->nr_pages -= MAX_WRITEBACK_PAGES - wbc.nr_to_write;
68203ba3782SJens Axboe 		wrote += MAX_WRITEBACK_PAGES - wbc.nr_to_write;
68303ba3782SJens Axboe 
68403ba3782SJens Axboe 		/*
68571fd05a8SJens Axboe 		 * If we consumed everything, see if we have more
68603ba3782SJens Axboe 		 */
68771fd05a8SJens Axboe 		if (wbc.nr_to_write <= 0)
68871fd05a8SJens Axboe 			continue;
68971fd05a8SJens Axboe 		/*
69071fd05a8SJens Axboe 		 * Didn't write everything and we don't have more IO, bail
69171fd05a8SJens Axboe 		 */
69271fd05a8SJens Axboe 		if (!wbc.more_io)
69371fd05a8SJens Axboe 			break;
69471fd05a8SJens Axboe 		/*
69571fd05a8SJens Axboe 		 * Did we write something? Try for more
69671fd05a8SJens Axboe 		 */
697a5989bdcSJan Kara 		if (wbc.nr_to_write < MAX_WRITEBACK_PAGES)
69803ba3782SJens Axboe 			continue;
699a5989bdcSJan Kara 		/*
700a5989bdcSJan Kara 		 * Nothing written. Wait for some inode to
701a5989bdcSJan Kara 		 * become available for writeback. Otherwise
702a5989bdcSJan Kara 		 * we'll just busyloop.
703a5989bdcSJan Kara 		 */
704a5989bdcSJan Kara 		spin_lock(&inode_lock);
705a5989bdcSJan Kara 		if (!list_empty(&wb->b_more_io))  {
7067ccf19a8SNick Piggin 			inode = wb_inode(wb->b_more_io.prev);
707028c2dd1SDave Chinner 			trace_wbc_writeback_wait(&wbc, wb->bdi);
708a5989bdcSJan Kara 			inode_wait_for_writeback(inode);
709a5989bdcSJan Kara 		}
710a5989bdcSJan Kara 		spin_unlock(&inode_lock);
71103ba3782SJens Axboe 	}
71203ba3782SJens Axboe 
71303ba3782SJens Axboe 	return wrote;
71403ba3782SJens Axboe }
71503ba3782SJens Axboe 
71603ba3782SJens Axboe /*
71783ba7b07SChristoph Hellwig  * Return the next wb_writeback_work struct that hasn't been processed yet.
71803ba3782SJens Axboe  */
71983ba7b07SChristoph Hellwig static struct wb_writeback_work *
72008852b6dSMinchan Kim get_next_work_item(struct backing_dev_info *bdi)
72103ba3782SJens Axboe {
72283ba7b07SChristoph Hellwig 	struct wb_writeback_work *work = NULL;
72303ba3782SJens Axboe 
7246467716aSArtem Bityutskiy 	spin_lock_bh(&bdi->wb_lock);
72583ba7b07SChristoph Hellwig 	if (!list_empty(&bdi->work_list)) {
72683ba7b07SChristoph Hellwig 		work = list_entry(bdi->work_list.next,
72783ba7b07SChristoph Hellwig 				  struct wb_writeback_work, list);
72883ba7b07SChristoph Hellwig 		list_del_init(&work->list);
72903ba3782SJens Axboe 	}
7306467716aSArtem Bityutskiy 	spin_unlock_bh(&bdi->wb_lock);
73183ba7b07SChristoph Hellwig 	return work;
73203ba3782SJens Axboe }
73303ba3782SJens Axboe 
734cdf01dd5SLinus Torvalds /*
735cdf01dd5SLinus Torvalds  * Add in the number of potentially dirty inodes, because each inode
736cdf01dd5SLinus Torvalds  * write can dirty pagecache in the underlying blockdev.
737cdf01dd5SLinus Torvalds  */
738cdf01dd5SLinus Torvalds static unsigned long get_nr_dirty_pages(void)
739cdf01dd5SLinus Torvalds {
740cdf01dd5SLinus Torvalds 	return global_page_state(NR_FILE_DIRTY) +
741cdf01dd5SLinus Torvalds 		global_page_state(NR_UNSTABLE_NFS) +
742cdf01dd5SLinus Torvalds 		get_nr_dirty_inodes();
743cdf01dd5SLinus Torvalds }
744cdf01dd5SLinus Torvalds 
7456585027aSJan Kara static long wb_check_background_flush(struct bdi_writeback *wb)
7466585027aSJan Kara {
7476585027aSJan Kara 	if (over_bground_thresh()) {
7486585027aSJan Kara 
7496585027aSJan Kara 		struct wb_writeback_work work = {
7506585027aSJan Kara 			.nr_pages	= LONG_MAX,
7516585027aSJan Kara 			.sync_mode	= WB_SYNC_NONE,
7526585027aSJan Kara 			.for_background	= 1,
7536585027aSJan Kara 			.range_cyclic	= 1,
7546585027aSJan Kara 		};
7556585027aSJan Kara 
7566585027aSJan Kara 		return wb_writeback(wb, &work);
7576585027aSJan Kara 	}
7586585027aSJan Kara 
7596585027aSJan Kara 	return 0;
7606585027aSJan Kara }
7616585027aSJan Kara 
76203ba3782SJens Axboe static long wb_check_old_data_flush(struct bdi_writeback *wb)
76303ba3782SJens Axboe {
76403ba3782SJens Axboe 	unsigned long expired;
76503ba3782SJens Axboe 	long nr_pages;
76603ba3782SJens Axboe 
76769b62d01SJens Axboe 	/*
76869b62d01SJens Axboe 	 * When set to zero, disable periodic writeback
76969b62d01SJens Axboe 	 */
77069b62d01SJens Axboe 	if (!dirty_writeback_interval)
77169b62d01SJens Axboe 		return 0;
77269b62d01SJens Axboe 
77303ba3782SJens Axboe 	expired = wb->last_old_flush +
77403ba3782SJens Axboe 			msecs_to_jiffies(dirty_writeback_interval * 10);
77503ba3782SJens Axboe 	if (time_before(jiffies, expired))
77603ba3782SJens Axboe 		return 0;
77703ba3782SJens Axboe 
77803ba3782SJens Axboe 	wb->last_old_flush = jiffies;
779cdf01dd5SLinus Torvalds 	nr_pages = get_nr_dirty_pages();
78003ba3782SJens Axboe 
781c4a77a6cSJens Axboe 	if (nr_pages) {
78283ba7b07SChristoph Hellwig 		struct wb_writeback_work work = {
783c4a77a6cSJens Axboe 			.nr_pages	= nr_pages,
784c4a77a6cSJens Axboe 			.sync_mode	= WB_SYNC_NONE,
785c4a77a6cSJens Axboe 			.for_kupdate	= 1,
786c4a77a6cSJens Axboe 			.range_cyclic	= 1,
787c4a77a6cSJens Axboe 		};
788c4a77a6cSJens Axboe 
78983ba7b07SChristoph Hellwig 		return wb_writeback(wb, &work);
790c4a77a6cSJens Axboe 	}
79103ba3782SJens Axboe 
79203ba3782SJens Axboe 	return 0;
79303ba3782SJens Axboe }
79403ba3782SJens Axboe 
79503ba3782SJens Axboe /*
79603ba3782SJens Axboe  * Retrieve work items and do the writeback they describe
79703ba3782SJens Axboe  */
79803ba3782SJens Axboe long wb_do_writeback(struct bdi_writeback *wb, int force_wait)
79903ba3782SJens Axboe {
80003ba3782SJens Axboe 	struct backing_dev_info *bdi = wb->bdi;
80183ba7b07SChristoph Hellwig 	struct wb_writeback_work *work;
802c4a77a6cSJens Axboe 	long wrote = 0;
80303ba3782SJens Axboe 
80481d73a32SJan Kara 	set_bit(BDI_writeback_running, &wb->bdi->state);
80508852b6dSMinchan Kim 	while ((work = get_next_work_item(bdi)) != NULL) {
80603ba3782SJens Axboe 		/*
80703ba3782SJens Axboe 		 * Override sync mode, in case we must wait for completion
80883ba7b07SChristoph Hellwig 		 * because this thread is exiting now.
80903ba3782SJens Axboe 		 */
81003ba3782SJens Axboe 		if (force_wait)
81183ba7b07SChristoph Hellwig 			work->sync_mode = WB_SYNC_ALL;
81283ba7b07SChristoph Hellwig 
813455b2864SDave Chinner 		trace_writeback_exec(bdi, work);
814455b2864SDave Chinner 
81583ba7b07SChristoph Hellwig 		wrote += wb_writeback(wb, work);
81603ba3782SJens Axboe 
81703ba3782SJens Axboe 		/*
81883ba7b07SChristoph Hellwig 		 * Notify the caller of completion if this is a synchronous
81983ba7b07SChristoph Hellwig 		 * work item, otherwise just free it.
82003ba3782SJens Axboe 		 */
82183ba7b07SChristoph Hellwig 		if (work->done)
82283ba7b07SChristoph Hellwig 			complete(work->done);
82383ba7b07SChristoph Hellwig 		else
82483ba7b07SChristoph Hellwig 			kfree(work);
82503ba3782SJens Axboe 	}
82603ba3782SJens Axboe 
82703ba3782SJens Axboe 	/*
82803ba3782SJens Axboe 	 * Check for periodic writeback, kupdated() style
82903ba3782SJens Axboe 	 */
83003ba3782SJens Axboe 	wrote += wb_check_old_data_flush(wb);
8316585027aSJan Kara 	wrote += wb_check_background_flush(wb);
83281d73a32SJan Kara 	clear_bit(BDI_writeback_running, &wb->bdi->state);
83303ba3782SJens Axboe 
83403ba3782SJens Axboe 	return wrote;
83503ba3782SJens Axboe }
83603ba3782SJens Axboe 
83703ba3782SJens Axboe /*
83803ba3782SJens Axboe  * Handle writeback of dirty data for the device backed by this bdi. Also
83903ba3782SJens Axboe  * wakes up periodically and does kupdated style flushing.
84003ba3782SJens Axboe  */
84108243900SChristoph Hellwig int bdi_writeback_thread(void *data)
84203ba3782SJens Axboe {
84308243900SChristoph Hellwig 	struct bdi_writeback *wb = data;
84408243900SChristoph Hellwig 	struct backing_dev_info *bdi = wb->bdi;
84503ba3782SJens Axboe 	long pages_written;
84603ba3782SJens Axboe 
847766f9164SPeter Zijlstra 	current->flags |= PF_SWAPWRITE;
84808243900SChristoph Hellwig 	set_freezable();
849ecd58403SArtem Bityutskiy 	wb->last_active = jiffies;
85003ba3782SJens Axboe 
85103ba3782SJens Axboe 	/*
85208243900SChristoph Hellwig 	 * Our parent may run at a different priority, just set us to normal
85303ba3782SJens Axboe 	 */
85408243900SChristoph Hellwig 	set_user_nice(current, 0);
85508243900SChristoph Hellwig 
856455b2864SDave Chinner 	trace_writeback_thread_start(bdi);
857455b2864SDave Chinner 
85803ba3782SJens Axboe 	while (!kthread_should_stop()) {
8596467716aSArtem Bityutskiy 		/*
8606467716aSArtem Bityutskiy 		 * Remove own delayed wake-up timer, since we are already awake
8616467716aSArtem Bityutskiy 		 * and we'll take care of the preriodic write-back.
8626467716aSArtem Bityutskiy 		 */
8636467716aSArtem Bityutskiy 		del_timer(&wb->wakeup_timer);
8646467716aSArtem Bityutskiy 
86503ba3782SJens Axboe 		pages_written = wb_do_writeback(wb, 0);
86603ba3782SJens Axboe 
867455b2864SDave Chinner 		trace_writeback_pages_written(pages_written);
868455b2864SDave Chinner 
86903ba3782SJens Axboe 		if (pages_written)
870ecd58403SArtem Bityutskiy 			wb->last_active = jiffies;
87103ba3782SJens Axboe 
872297252c8SArtem Bityutskiy 		set_current_state(TASK_INTERRUPTIBLE);
873b76b4014SJ. Bruce Fields 		if (!list_empty(&bdi->work_list) || kthread_should_stop()) {
874297252c8SArtem Bityutskiy 			__set_current_state(TASK_RUNNING);
875297252c8SArtem Bityutskiy 			continue;
87603ba3782SJens Axboe 		}
87703ba3782SJens Axboe 
878253c34e9SArtem Bityutskiy 		if (wb_has_dirty_io(wb) && dirty_writeback_interval)
879fff5b85aSArtem Bityutskiy 			schedule_timeout(msecs_to_jiffies(dirty_writeback_interval * 10));
880253c34e9SArtem Bityutskiy 		else {
881253c34e9SArtem Bityutskiy 			/*
882253c34e9SArtem Bityutskiy 			 * We have nothing to do, so can go sleep without any
883253c34e9SArtem Bityutskiy 			 * timeout and save power. When a work is queued or
884253c34e9SArtem Bityutskiy 			 * something is made dirty - we will be woken up.
885253c34e9SArtem Bityutskiy 			 */
88669b62d01SJens Axboe 			schedule();
887f9eadbbdSJens Axboe 		}
88869b62d01SJens Axboe 
88903ba3782SJens Axboe 		try_to_freeze();
89003ba3782SJens Axboe 	}
89103ba3782SJens Axboe 
892fff5b85aSArtem Bityutskiy 	/* Flush any work that raced with us exiting */
89308243900SChristoph Hellwig 	if (!list_empty(&bdi->work_list))
89408243900SChristoph Hellwig 		wb_do_writeback(wb, 1);
895455b2864SDave Chinner 
896455b2864SDave Chinner 	trace_writeback_thread_stop(bdi);
89703ba3782SJens Axboe 	return 0;
89803ba3782SJens Axboe }
89903ba3782SJens Axboe 
90008243900SChristoph Hellwig 
90103ba3782SJens Axboe /*
90203ba3782SJens Axboe  * Start writeback of `nr_pages' pages.  If `nr_pages' is zero, write back
90303ba3782SJens Axboe  * the whole world.
90403ba3782SJens Axboe  */
90503ba3782SJens Axboe void wakeup_flusher_threads(long nr_pages)
90603ba3782SJens Axboe {
907b8c2f347SChristoph Hellwig 	struct backing_dev_info *bdi;
908b8c2f347SChristoph Hellwig 
90983ba7b07SChristoph Hellwig 	if (!nr_pages) {
91083ba7b07SChristoph Hellwig 		nr_pages = global_page_state(NR_FILE_DIRTY) +
91103ba3782SJens Axboe 				global_page_state(NR_UNSTABLE_NFS);
912b8c2f347SChristoph Hellwig 	}
913b8c2f347SChristoph Hellwig 
914b8c2f347SChristoph Hellwig 	rcu_read_lock();
915b8c2f347SChristoph Hellwig 	list_for_each_entry_rcu(bdi, &bdi_list, bdi_list) {
916b8c2f347SChristoph Hellwig 		if (!bdi_has_dirty_io(bdi))
917b8c2f347SChristoph Hellwig 			continue;
9186585027aSJan Kara 		__bdi_start_writeback(bdi, nr_pages, false);
919b8c2f347SChristoph Hellwig 	}
920b8c2f347SChristoph Hellwig 	rcu_read_unlock();
92103ba3782SJens Axboe }
92203ba3782SJens Axboe 
92303ba3782SJens Axboe static noinline void block_dump___mark_inode_dirty(struct inode *inode)
92403ba3782SJens Axboe {
92503ba3782SJens Axboe 	if (inode->i_ino || strcmp(inode->i_sb->s_id, "bdev")) {
92603ba3782SJens Axboe 		struct dentry *dentry;
92703ba3782SJens Axboe 		const char *name = "?";
92803ba3782SJens Axboe 
92903ba3782SJens Axboe 		dentry = d_find_alias(inode);
93003ba3782SJens Axboe 		if (dentry) {
93103ba3782SJens Axboe 			spin_lock(&dentry->d_lock);
93203ba3782SJens Axboe 			name = (const char *) dentry->d_name.name;
93303ba3782SJens Axboe 		}
93403ba3782SJens Axboe 		printk(KERN_DEBUG
93503ba3782SJens Axboe 		       "%s(%d): dirtied inode %lu (%s) on %s\n",
93603ba3782SJens Axboe 		       current->comm, task_pid_nr(current), inode->i_ino,
93703ba3782SJens Axboe 		       name, inode->i_sb->s_id);
93803ba3782SJens Axboe 		if (dentry) {
93903ba3782SJens Axboe 			spin_unlock(&dentry->d_lock);
94003ba3782SJens Axboe 			dput(dentry);
94103ba3782SJens Axboe 		}
94203ba3782SJens Axboe 	}
94303ba3782SJens Axboe }
94403ba3782SJens Axboe 
94503ba3782SJens Axboe /**
94603ba3782SJens Axboe  *	__mark_inode_dirty -	internal function
94703ba3782SJens Axboe  *	@inode: inode to mark
94803ba3782SJens Axboe  *	@flags: what kind of dirty (i.e. I_DIRTY_SYNC)
94903ba3782SJens Axboe  *	Mark an inode as dirty. Callers should use mark_inode_dirty or
95003ba3782SJens Axboe  *  	mark_inode_dirty_sync.
95103ba3782SJens Axboe  *
95203ba3782SJens Axboe  * Put the inode on the super block's dirty list.
95303ba3782SJens Axboe  *
95403ba3782SJens Axboe  * CAREFUL! We mark it dirty unconditionally, but move it onto the
95503ba3782SJens Axboe  * dirty list only if it is hashed or if it refers to a blockdev.
95603ba3782SJens Axboe  * If it was not hashed, it will never be added to the dirty list
95703ba3782SJens Axboe  * even if it is later hashed, as it will have been marked dirty already.
95803ba3782SJens Axboe  *
95903ba3782SJens Axboe  * In short, make sure you hash any inodes _before_ you start marking
96003ba3782SJens Axboe  * them dirty.
96103ba3782SJens Axboe  *
96203ba3782SJens Axboe  * This function *must* be atomic for the I_DIRTY_PAGES case -
96303ba3782SJens Axboe  * set_page_dirty() is called under spinlock in several places.
96403ba3782SJens Axboe  *
96503ba3782SJens Axboe  * Note that for blockdevs, inode->dirtied_when represents the dirtying time of
96603ba3782SJens Axboe  * the block-special inode (/dev/hda1) itself.  And the ->dirtied_when field of
96703ba3782SJens Axboe  * the kernel-internal blockdev inode represents the dirtying time of the
96803ba3782SJens Axboe  * blockdev's pages.  This is why for I_DIRTY_PAGES we always use
96903ba3782SJens Axboe  * page->mapping->host, so the page-dirtying time is recorded in the internal
97003ba3782SJens Axboe  * blockdev inode.
97103ba3782SJens Axboe  */
97203ba3782SJens Axboe void __mark_inode_dirty(struct inode *inode, int flags)
97303ba3782SJens Axboe {
97403ba3782SJens Axboe 	struct super_block *sb = inode->i_sb;
975253c34e9SArtem Bityutskiy 	struct backing_dev_info *bdi = NULL;
976253c34e9SArtem Bityutskiy 	bool wakeup_bdi = false;
97703ba3782SJens Axboe 
97803ba3782SJens Axboe 	/*
97903ba3782SJens Axboe 	 * Don't do this for I_DIRTY_PAGES - that doesn't actually
98003ba3782SJens Axboe 	 * dirty the inode itself
98103ba3782SJens Axboe 	 */
98203ba3782SJens Axboe 	if (flags & (I_DIRTY_SYNC | I_DIRTY_DATASYNC)) {
98303ba3782SJens Axboe 		if (sb->s_op->dirty_inode)
98403ba3782SJens Axboe 			sb->s_op->dirty_inode(inode);
98503ba3782SJens Axboe 	}
98603ba3782SJens Axboe 
98703ba3782SJens Axboe 	/*
98803ba3782SJens Axboe 	 * make sure that changes are seen by all cpus before we test i_state
98903ba3782SJens Axboe 	 * -- mikulas
99003ba3782SJens Axboe 	 */
99103ba3782SJens Axboe 	smp_mb();
99203ba3782SJens Axboe 
99303ba3782SJens Axboe 	/* avoid the locking if we can */
99403ba3782SJens Axboe 	if ((inode->i_state & flags) == flags)
99503ba3782SJens Axboe 		return;
99603ba3782SJens Axboe 
99703ba3782SJens Axboe 	if (unlikely(block_dump))
99803ba3782SJens Axboe 		block_dump___mark_inode_dirty(inode);
99903ba3782SJens Axboe 
100003ba3782SJens Axboe 	spin_lock(&inode_lock);
100103ba3782SJens Axboe 	if ((inode->i_state & flags) != flags) {
100203ba3782SJens Axboe 		const int was_dirty = inode->i_state & I_DIRTY;
100303ba3782SJens Axboe 
100403ba3782SJens Axboe 		inode->i_state |= flags;
100503ba3782SJens Axboe 
100603ba3782SJens Axboe 		/*
100703ba3782SJens Axboe 		 * If the inode is being synced, just update its dirty state.
100803ba3782SJens Axboe 		 * The unlocker will place the inode on the appropriate
100903ba3782SJens Axboe 		 * superblock list, based upon its state.
101003ba3782SJens Axboe 		 */
101103ba3782SJens Axboe 		if (inode->i_state & I_SYNC)
101203ba3782SJens Axboe 			goto out;
101303ba3782SJens Axboe 
101403ba3782SJens Axboe 		/*
101503ba3782SJens Axboe 		 * Only add valid (hashed) inodes to the superblock's
101603ba3782SJens Axboe 		 * dirty list.  Add blockdev inodes as well.
101703ba3782SJens Axboe 		 */
101803ba3782SJens Axboe 		if (!S_ISBLK(inode->i_mode)) {
10191d3382cbSAl Viro 			if (inode_unhashed(inode))
102003ba3782SJens Axboe 				goto out;
102103ba3782SJens Axboe 		}
1022a4ffdde6SAl Viro 		if (inode->i_state & I_FREEING)
102303ba3782SJens Axboe 			goto out;
102403ba3782SJens Axboe 
102503ba3782SJens Axboe 		/*
102603ba3782SJens Axboe 		 * If the inode was already on b_dirty/b_io/b_more_io, don't
102703ba3782SJens Axboe 		 * reposition it (that would break b_dirty time-ordering).
102803ba3782SJens Axboe 		 */
102903ba3782SJens Axboe 		if (!was_dirty) {
1030253c34e9SArtem Bityutskiy 			bdi = inode_to_bdi(inode);
1031500b067cSJens Axboe 
1032253c34e9SArtem Bityutskiy 			if (bdi_cap_writeback_dirty(bdi)) {
1033253c34e9SArtem Bityutskiy 				WARN(!test_bit(BDI_registered, &bdi->state),
1034253c34e9SArtem Bityutskiy 				     "bdi-%s not registered\n", bdi->name);
1035253c34e9SArtem Bityutskiy 
1036253c34e9SArtem Bityutskiy 				/*
1037253c34e9SArtem Bityutskiy 				 * If this is the first dirty inode for this
1038253c34e9SArtem Bityutskiy 				 * bdi, we have to wake-up the corresponding
1039253c34e9SArtem Bityutskiy 				 * bdi thread to make sure background
1040253c34e9SArtem Bityutskiy 				 * write-back happens later.
1041253c34e9SArtem Bityutskiy 				 */
1042253c34e9SArtem Bityutskiy 				if (!wb_has_dirty_io(&bdi->wb))
1043253c34e9SArtem Bityutskiy 					wakeup_bdi = true;
1044500b067cSJens Axboe 			}
104503ba3782SJens Axboe 
104603ba3782SJens Axboe 			inode->dirtied_when = jiffies;
10477ccf19a8SNick Piggin 			list_move(&inode->i_wb_list, &bdi->wb.b_dirty);
104803ba3782SJens Axboe 		}
104903ba3782SJens Axboe 	}
105003ba3782SJens Axboe out:
105103ba3782SJens Axboe 	spin_unlock(&inode_lock);
1052253c34e9SArtem Bityutskiy 
1053253c34e9SArtem Bityutskiy 	if (wakeup_bdi)
10546467716aSArtem Bityutskiy 		bdi_wakeup_thread_delayed(bdi);
105503ba3782SJens Axboe }
105603ba3782SJens Axboe EXPORT_SYMBOL(__mark_inode_dirty);
105703ba3782SJens Axboe 
105866f3b8e2SJens Axboe /*
105966f3b8e2SJens Axboe  * Write out a superblock's list of dirty inodes.  A wait will be performed
106066f3b8e2SJens Axboe  * upon no inodes, all inodes or the final one, depending upon sync_mode.
106166f3b8e2SJens Axboe  *
106266f3b8e2SJens Axboe  * If older_than_this is non-NULL, then only write out inodes which
106366f3b8e2SJens Axboe  * had their first dirtying at a time earlier than *older_than_this.
106466f3b8e2SJens Axboe  *
106566f3b8e2SJens Axboe  * If `bdi' is non-zero then we're being asked to writeback a specific queue.
106666f3b8e2SJens Axboe  * This function assumes that the blockdev superblock's inodes are backed by
106766f3b8e2SJens Axboe  * a variety of queues, so all inodes are searched.  For other superblocks,
106866f3b8e2SJens Axboe  * assume that all inodes are backed by the same queue.
106966f3b8e2SJens Axboe  *
107066f3b8e2SJens Axboe  * The inodes to be written are parked on bdi->b_io.  They are moved back onto
107166f3b8e2SJens Axboe  * bdi->b_dirty as they are selected for writing.  This way, none can be missed
107266f3b8e2SJens Axboe  * on the writer throttling path, and we get decent balancing between many
107366f3b8e2SJens Axboe  * throttled threads: we don't want them all piling up on inode_sync_wait.
107466f3b8e2SJens Axboe  */
1075b6e51316SJens Axboe static void wait_sb_inodes(struct super_block *sb)
107666f3b8e2SJens Axboe {
107738f21977SNick Piggin 	struct inode *inode, *old_inode = NULL;
107838f21977SNick Piggin 
107903ba3782SJens Axboe 	/*
108003ba3782SJens Axboe 	 * We need to be protected against the filesystem going from
108103ba3782SJens Axboe 	 * r/o to r/w or vice versa.
108203ba3782SJens Axboe 	 */
1083b6e51316SJens Axboe 	WARN_ON(!rwsem_is_locked(&sb->s_umount));
108403ba3782SJens Axboe 
108566f3b8e2SJens Axboe 	spin_lock(&inode_lock);
108666f3b8e2SJens Axboe 
108738f21977SNick Piggin 	/*
108838f21977SNick Piggin 	 * Data integrity sync. Must wait for all pages under writeback,
108938f21977SNick Piggin 	 * because there may have been pages dirtied before our sync
109038f21977SNick Piggin 	 * call, but which had writeout started before we write it out.
109138f21977SNick Piggin 	 * In which case, the inode may not be on the dirty list, but
109238f21977SNick Piggin 	 * we still have to wait for that writeout.
109338f21977SNick Piggin 	 */
1094b6e51316SJens Axboe 	list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
109538f21977SNick Piggin 		struct address_space *mapping;
109638f21977SNick Piggin 
1097a4ffdde6SAl Viro 		if (inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW))
109838f21977SNick Piggin 			continue;
109938f21977SNick Piggin 		mapping = inode->i_mapping;
110038f21977SNick Piggin 		if (mapping->nrpages == 0)
110138f21977SNick Piggin 			continue;
110238f21977SNick Piggin 		__iget(inode);
1103ae8547b0SHans Reiser 		spin_unlock(&inode_lock);
110438f21977SNick Piggin 		/*
110538f21977SNick Piggin 		 * We hold a reference to 'inode' so it couldn't have
110638f21977SNick Piggin 		 * been removed from s_inodes list while we dropped the
110738f21977SNick Piggin 		 * inode_lock.  We cannot iput the inode now as we can
110838f21977SNick Piggin 		 * be holding the last reference and we cannot iput it
110938f21977SNick Piggin 		 * under inode_lock. So we keep the reference and iput
111038f21977SNick Piggin 		 * it later.
111138f21977SNick Piggin 		 */
111238f21977SNick Piggin 		iput(old_inode);
111338f21977SNick Piggin 		old_inode = inode;
111438f21977SNick Piggin 
111538f21977SNick Piggin 		filemap_fdatawait(mapping);
111638f21977SNick Piggin 
111738f21977SNick Piggin 		cond_resched();
111838f21977SNick Piggin 
111938f21977SNick Piggin 		spin_lock(&inode_lock);
112038f21977SNick Piggin 	}
112138f21977SNick Piggin 	spin_unlock(&inode_lock);
112238f21977SNick Piggin 	iput(old_inode);
112366f3b8e2SJens Axboe }
11241da177e4SLinus Torvalds 
1125d8a8559cSJens Axboe /**
11263259f8beSChris Mason  * writeback_inodes_sb_nr -	writeback dirty inodes from given super_block
1127d8a8559cSJens Axboe  * @sb: the superblock
11283259f8beSChris Mason  * @nr: the number of pages to write
11291da177e4SLinus Torvalds  *
1130d8a8559cSJens Axboe  * Start writeback on some inodes on this super_block. No guarantees are made
1131d8a8559cSJens Axboe  * on how many (if any) will be written, and this function does not wait
11323259f8beSChris Mason  * for IO completion of submitted IO.
11331da177e4SLinus Torvalds  */
11343259f8beSChris Mason void writeback_inodes_sb_nr(struct super_block *sb, unsigned long nr)
11351da177e4SLinus Torvalds {
113683ba7b07SChristoph Hellwig 	DECLARE_COMPLETION_ONSTACK(done);
113783ba7b07SChristoph Hellwig 	struct wb_writeback_work work = {
11383c4d7165SChristoph Hellwig 		.sb		= sb,
11393c4d7165SChristoph Hellwig 		.sync_mode	= WB_SYNC_NONE,
114083ba7b07SChristoph Hellwig 		.done		= &done,
11413259f8beSChris Mason 		.nr_pages	= nr,
11423c4d7165SChristoph Hellwig 	};
11430e3c9a22SJens Axboe 
1144cf37e972SChristoph Hellwig 	WARN_ON(!rwsem_is_locked(&sb->s_umount));
114583ba7b07SChristoph Hellwig 	bdi_queue_work(sb->s_bdi, &work);
114683ba7b07SChristoph Hellwig 	wait_for_completion(&done);
11471da177e4SLinus Torvalds }
11483259f8beSChris Mason EXPORT_SYMBOL(writeback_inodes_sb_nr);
11493259f8beSChris Mason 
11503259f8beSChris Mason /**
11513259f8beSChris Mason  * writeback_inodes_sb	-	writeback dirty inodes from given super_block
11523259f8beSChris Mason  * @sb: the superblock
11533259f8beSChris Mason  *
11543259f8beSChris Mason  * Start writeback on some inodes on this super_block. No guarantees are made
11553259f8beSChris Mason  * on how many (if any) will be written, and this function does not wait
11563259f8beSChris Mason  * for IO completion of submitted IO.
11573259f8beSChris Mason  */
11583259f8beSChris Mason void writeback_inodes_sb(struct super_block *sb)
11593259f8beSChris Mason {
1160925d169fSLinus Torvalds 	return writeback_inodes_sb_nr(sb, get_nr_dirty_pages());
11613259f8beSChris Mason }
1162d8a8559cSJens Axboe EXPORT_SYMBOL(writeback_inodes_sb);
1163d8a8559cSJens Axboe 
1164d8a8559cSJens Axboe /**
116517bd55d0SEric Sandeen  * writeback_inodes_sb_if_idle	-	start writeback if none underway
116617bd55d0SEric Sandeen  * @sb: the superblock
116717bd55d0SEric Sandeen  *
116817bd55d0SEric Sandeen  * Invoke writeback_inodes_sb if no writeback is currently underway.
116917bd55d0SEric Sandeen  * Returns 1 if writeback was started, 0 if not.
117017bd55d0SEric Sandeen  */
117117bd55d0SEric Sandeen int writeback_inodes_sb_if_idle(struct super_block *sb)
117217bd55d0SEric Sandeen {
117317bd55d0SEric Sandeen 	if (!writeback_in_progress(sb->s_bdi)) {
1174cf37e972SChristoph Hellwig 		down_read(&sb->s_umount);
117517bd55d0SEric Sandeen 		writeback_inodes_sb(sb);
1176cf37e972SChristoph Hellwig 		up_read(&sb->s_umount);
117717bd55d0SEric Sandeen 		return 1;
117817bd55d0SEric Sandeen 	} else
117917bd55d0SEric Sandeen 		return 0;
118017bd55d0SEric Sandeen }
118117bd55d0SEric Sandeen EXPORT_SYMBOL(writeback_inodes_sb_if_idle);
118217bd55d0SEric Sandeen 
118317bd55d0SEric Sandeen /**
11843259f8beSChris Mason  * writeback_inodes_sb_if_idle	-	start writeback if none underway
11853259f8beSChris Mason  * @sb: the superblock
11863259f8beSChris Mason  * @nr: the number of pages to write
11873259f8beSChris Mason  *
11883259f8beSChris Mason  * Invoke writeback_inodes_sb if no writeback is currently underway.
11893259f8beSChris Mason  * Returns 1 if writeback was started, 0 if not.
11903259f8beSChris Mason  */
11913259f8beSChris Mason int writeback_inodes_sb_nr_if_idle(struct super_block *sb,
11923259f8beSChris Mason 				   unsigned long nr)
11933259f8beSChris Mason {
11943259f8beSChris Mason 	if (!writeback_in_progress(sb->s_bdi)) {
11953259f8beSChris Mason 		down_read(&sb->s_umount);
11963259f8beSChris Mason 		writeback_inodes_sb_nr(sb, nr);
11973259f8beSChris Mason 		up_read(&sb->s_umount);
11983259f8beSChris Mason 		return 1;
11993259f8beSChris Mason 	} else
12003259f8beSChris Mason 		return 0;
12013259f8beSChris Mason }
12023259f8beSChris Mason EXPORT_SYMBOL(writeback_inodes_sb_nr_if_idle);
12033259f8beSChris Mason 
12043259f8beSChris Mason /**
1205d8a8559cSJens Axboe  * sync_inodes_sb	-	sync sb inode pages
1206d8a8559cSJens Axboe  * @sb: the superblock
1207d8a8559cSJens Axboe  *
1208d8a8559cSJens Axboe  * This function writes and waits on any dirty inode belonging to this
1209d8a8559cSJens Axboe  * super_block. The number of pages synced is returned.
1210d8a8559cSJens Axboe  */
1211b6e51316SJens Axboe void sync_inodes_sb(struct super_block *sb)
1212d8a8559cSJens Axboe {
121383ba7b07SChristoph Hellwig 	DECLARE_COMPLETION_ONSTACK(done);
121483ba7b07SChristoph Hellwig 	struct wb_writeback_work work = {
12153c4d7165SChristoph Hellwig 		.sb		= sb,
12163c4d7165SChristoph Hellwig 		.sync_mode	= WB_SYNC_ALL,
12173c4d7165SChristoph Hellwig 		.nr_pages	= LONG_MAX,
12183c4d7165SChristoph Hellwig 		.range_cyclic	= 0,
121983ba7b07SChristoph Hellwig 		.done		= &done,
12203c4d7165SChristoph Hellwig 	};
12213c4d7165SChristoph Hellwig 
1222cf37e972SChristoph Hellwig 	WARN_ON(!rwsem_is_locked(&sb->s_umount));
1223cf37e972SChristoph Hellwig 
122483ba7b07SChristoph Hellwig 	bdi_queue_work(sb->s_bdi, &work);
122583ba7b07SChristoph Hellwig 	wait_for_completion(&done);
122683ba7b07SChristoph Hellwig 
1227b6e51316SJens Axboe 	wait_sb_inodes(sb);
1228d8a8559cSJens Axboe }
1229d8a8559cSJens Axboe EXPORT_SYMBOL(sync_inodes_sb);
12301da177e4SLinus Torvalds 
12311da177e4SLinus Torvalds /**
12321da177e4SLinus Torvalds  * write_inode_now	-	write an inode to disk
12331da177e4SLinus Torvalds  * @inode: inode to write to disk
12341da177e4SLinus Torvalds  * @sync: whether the write should be synchronous or not
12351da177e4SLinus Torvalds  *
12367f04c26dSAndrea Arcangeli  * This function commits an inode to disk immediately if it is dirty. This is
12377f04c26dSAndrea Arcangeli  * primarily needed by knfsd.
12387f04c26dSAndrea Arcangeli  *
12397f04c26dSAndrea Arcangeli  * The caller must either have a ref on the inode or must have set I_WILL_FREE.
12401da177e4SLinus Torvalds  */
12411da177e4SLinus Torvalds int write_inode_now(struct inode *inode, int sync)
12421da177e4SLinus Torvalds {
12431da177e4SLinus Torvalds 	int ret;
12441da177e4SLinus Torvalds 	struct writeback_control wbc = {
12451da177e4SLinus Torvalds 		.nr_to_write = LONG_MAX,
124618914b18SMike Galbraith 		.sync_mode = sync ? WB_SYNC_ALL : WB_SYNC_NONE,
1247111ebb6eSOGAWA Hirofumi 		.range_start = 0,
1248111ebb6eSOGAWA Hirofumi 		.range_end = LLONG_MAX,
12491da177e4SLinus Torvalds 	};
12501da177e4SLinus Torvalds 
12511da177e4SLinus Torvalds 	if (!mapping_cap_writeback_dirty(inode->i_mapping))
125249364ce2SAndrew Morton 		wbc.nr_to_write = 0;
12531da177e4SLinus Torvalds 
12541da177e4SLinus Torvalds 	might_sleep();
12551da177e4SLinus Torvalds 	spin_lock(&inode_lock);
125601c03194SChristoph Hellwig 	ret = writeback_single_inode(inode, &wbc);
12571da177e4SLinus Torvalds 	spin_unlock(&inode_lock);
12581da177e4SLinus Torvalds 	if (sync)
12591c0eeaf5SJoern Engel 		inode_sync_wait(inode);
12601da177e4SLinus Torvalds 	return ret;
12611da177e4SLinus Torvalds }
12621da177e4SLinus Torvalds EXPORT_SYMBOL(write_inode_now);
12631da177e4SLinus Torvalds 
12641da177e4SLinus Torvalds /**
12651da177e4SLinus Torvalds  * sync_inode - write an inode and its pages to disk.
12661da177e4SLinus Torvalds  * @inode: the inode to sync
12671da177e4SLinus Torvalds  * @wbc: controls the writeback mode
12681da177e4SLinus Torvalds  *
12691da177e4SLinus Torvalds  * sync_inode() will write an inode and its pages to disk.  It will also
12701da177e4SLinus Torvalds  * correctly update the inode on its superblock's dirty inode lists and will
12711da177e4SLinus Torvalds  * update inode->i_state.
12721da177e4SLinus Torvalds  *
12731da177e4SLinus Torvalds  * The caller must have a ref on the inode.
12741da177e4SLinus Torvalds  */
12751da177e4SLinus Torvalds int sync_inode(struct inode *inode, struct writeback_control *wbc)
12761da177e4SLinus Torvalds {
12771da177e4SLinus Torvalds 	int ret;
12781da177e4SLinus Torvalds 
12791da177e4SLinus Torvalds 	spin_lock(&inode_lock);
128001c03194SChristoph Hellwig 	ret = writeback_single_inode(inode, wbc);
12811da177e4SLinus Torvalds 	spin_unlock(&inode_lock);
12821da177e4SLinus Torvalds 	return ret;
12831da177e4SLinus Torvalds }
12841da177e4SLinus Torvalds EXPORT_SYMBOL(sync_inode);
1285c3765016SChristoph Hellwig 
1286c3765016SChristoph Hellwig /**
1287c3765016SChristoph Hellwig  * sync_inode - write an inode to disk
1288c3765016SChristoph Hellwig  * @inode: the inode to sync
1289c3765016SChristoph Hellwig  * @wait: wait for I/O to complete.
1290c3765016SChristoph Hellwig  *
1291c3765016SChristoph Hellwig  * Write an inode to disk and adjust it's dirty state after completion.
1292c3765016SChristoph Hellwig  *
1293c3765016SChristoph Hellwig  * Note: only writes the actual inode, no associated data or other metadata.
1294c3765016SChristoph Hellwig  */
1295c3765016SChristoph Hellwig int sync_inode_metadata(struct inode *inode, int wait)
1296c3765016SChristoph Hellwig {
1297c3765016SChristoph Hellwig 	struct writeback_control wbc = {
1298c3765016SChristoph Hellwig 		.sync_mode = wait ? WB_SYNC_ALL : WB_SYNC_NONE,
1299c3765016SChristoph Hellwig 		.nr_to_write = 0, /* metadata-only */
1300c3765016SChristoph Hellwig 	};
1301c3765016SChristoph Hellwig 
1302c3765016SChristoph Hellwig 	return sync_inode(inode, &wbc);
1303c3765016SChristoph Hellwig }
1304c3765016SChristoph Hellwig EXPORT_SYMBOL(sync_inode_metadata);
1305