xref: /openbmc/linux/fs/fs-writeback.c (revision 7ccf19a8)
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 
8783ba7b07SChristoph Hellwig static void bdi_queue_work(struct backing_dev_info *bdi,
8883ba7b07SChristoph Hellwig 		struct wb_writeback_work *work)
894195f73dSNick Piggin {
90455b2864SDave Chinner 	trace_writeback_queue(bdi, work);
91455b2864SDave Chinner 
926467716aSArtem Bityutskiy 	spin_lock_bh(&bdi->wb_lock);
9383ba7b07SChristoph Hellwig 	list_add_tail(&work->list, &bdi->work_list);
94fff5b85aSArtem Bityutskiy 	if (bdi->wb.task) {
95fff5b85aSArtem Bityutskiy 		wake_up_process(bdi->wb.task);
96fff5b85aSArtem Bityutskiy 	} else {
971da177e4SLinus Torvalds 		/*
98fff5b85aSArtem Bityutskiy 		 * The bdi thread isn't there, wake up the forker thread which
99fff5b85aSArtem Bityutskiy 		 * will create and run it.
1001da177e4SLinus Torvalds 		 */
101455b2864SDave Chinner 		trace_writeback_nothread(bdi, work);
10203ba3782SJens Axboe 		wake_up_process(default_backing_dev_info.wb.task);
1031da177e4SLinus Torvalds 	}
1046467716aSArtem Bityutskiy 	spin_unlock_bh(&bdi->wb_lock);
10503ba3782SJens Axboe }
1061da177e4SLinus Torvalds 
10783ba7b07SChristoph Hellwig static void
10883ba7b07SChristoph Hellwig __bdi_start_writeback(struct backing_dev_info *bdi, long nr_pages,
10983ba7b07SChristoph Hellwig 		bool range_cyclic, bool for_background)
1101da177e4SLinus Torvalds {
11183ba7b07SChristoph Hellwig 	struct wb_writeback_work *work;
11203ba3782SJens Axboe 
113bcddc3f0SJens Axboe 	/*
114bcddc3f0SJens Axboe 	 * This is WB_SYNC_NONE writeback, so if allocation fails just
115bcddc3f0SJens Axboe 	 * wakeup the thread for old dirty data writeback
116bcddc3f0SJens Axboe 	 */
11783ba7b07SChristoph Hellwig 	work = kzalloc(sizeof(*work), GFP_ATOMIC);
11883ba7b07SChristoph Hellwig 	if (!work) {
119455b2864SDave Chinner 		if (bdi->wb.task) {
120455b2864SDave Chinner 			trace_writeback_nowork(bdi);
12183ba7b07SChristoph Hellwig 			wake_up_process(bdi->wb.task);
122455b2864SDave Chinner 		}
12383ba7b07SChristoph Hellwig 		return;
12483ba7b07SChristoph Hellwig 	}
12583ba7b07SChristoph Hellwig 
12683ba7b07SChristoph Hellwig 	work->sync_mode	= WB_SYNC_NONE;
12783ba7b07SChristoph Hellwig 	work->nr_pages	= nr_pages;
12883ba7b07SChristoph Hellwig 	work->range_cyclic = range_cyclic;
12983ba7b07SChristoph Hellwig 	work->for_background = for_background;
13083ba7b07SChristoph Hellwig 
131f11fcae8SJens Axboe 	bdi_queue_work(bdi, work);
13203ba3782SJens Axboe }
133b6e51316SJens Axboe 
134b6e51316SJens Axboe /**
135b6e51316SJens Axboe  * bdi_start_writeback - start writeback
136b6e51316SJens Axboe  * @bdi: the backing device to write from
137b6e51316SJens Axboe  * @nr_pages: the number of pages to write
138b6e51316SJens Axboe  *
139b6e51316SJens Axboe  * Description:
140b6e51316SJens Axboe  *   This does WB_SYNC_NONE opportunistic writeback. The IO is only
141b6e51316SJens Axboe  *   started when this function returns, we make no guarentees on
1420e3c9a22SJens Axboe  *   completion. Caller need not hold sb s_umount semaphore.
143b6e51316SJens Axboe  *
144b6e51316SJens Axboe  */
145c5444198SChristoph Hellwig void bdi_start_writeback(struct backing_dev_info *bdi, long nr_pages)
146b6e51316SJens Axboe {
14783ba7b07SChristoph Hellwig 	__bdi_start_writeback(bdi, nr_pages, true, false);
148d3ddec76SWu Fengguang }
149d3ddec76SWu Fengguang 
150c5444198SChristoph Hellwig /**
151c5444198SChristoph Hellwig  * bdi_start_background_writeback - start background writeback
152c5444198SChristoph Hellwig  * @bdi: the backing device to write from
153c5444198SChristoph Hellwig  *
154c5444198SChristoph Hellwig  * Description:
155c5444198SChristoph Hellwig  *   This does WB_SYNC_NONE background writeback. The IO is only
156c5444198SChristoph Hellwig  *   started when this function returns, we make no guarentees on
157c5444198SChristoph Hellwig  *   completion. Caller need not hold sb s_umount semaphore.
158c5444198SChristoph Hellwig  */
159c5444198SChristoph Hellwig void bdi_start_background_writeback(struct backing_dev_info *bdi)
160c5444198SChristoph Hellwig {
16183ba7b07SChristoph Hellwig 	__bdi_start_writeback(bdi, LONG_MAX, true, true);
1621da177e4SLinus Torvalds }
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds /*
1656610a0bcSAndrew Morton  * Redirty an inode: set its when-it-was dirtied timestamp and move it to the
1666610a0bcSAndrew Morton  * furthest end of its superblock's dirty-inode list.
1676610a0bcSAndrew Morton  *
1686610a0bcSAndrew Morton  * Before stamping the inode's ->dirtied_when, we check to see whether it is
16966f3b8e2SJens Axboe  * already the most-recently-dirtied inode on the b_dirty list.  If that is
1706610a0bcSAndrew Morton  * the case then the inode must have been redirtied while it was being written
1716610a0bcSAndrew Morton  * out and we don't reset its dirtied_when.
1726610a0bcSAndrew Morton  */
1736610a0bcSAndrew Morton static void redirty_tail(struct inode *inode)
1746610a0bcSAndrew Morton {
17503ba3782SJens Axboe 	struct bdi_writeback *wb = &inode_to_bdi(inode)->wb;
1766610a0bcSAndrew Morton 
17703ba3782SJens Axboe 	if (!list_empty(&wb->b_dirty)) {
17866f3b8e2SJens Axboe 		struct inode *tail;
1796610a0bcSAndrew Morton 
1807ccf19a8SNick Piggin 		tail = wb_inode(wb->b_dirty.next);
18166f3b8e2SJens Axboe 		if (time_before(inode->dirtied_when, tail->dirtied_when))
1826610a0bcSAndrew Morton 			inode->dirtied_when = jiffies;
1836610a0bcSAndrew Morton 	}
1847ccf19a8SNick Piggin 	list_move(&inode->i_wb_list, &wb->b_dirty);
1856610a0bcSAndrew Morton }
1866610a0bcSAndrew Morton 
1876610a0bcSAndrew Morton /*
18866f3b8e2SJens Axboe  * requeue inode for re-scanning after bdi->b_io list is exhausted.
189c986d1e2SAndrew Morton  */
1900e0f4fc2SKen Chen static void requeue_io(struct inode *inode)
191c986d1e2SAndrew Morton {
19203ba3782SJens Axboe 	struct bdi_writeback *wb = &inode_to_bdi(inode)->wb;
19303ba3782SJens Axboe 
1947ccf19a8SNick Piggin 	list_move(&inode->i_wb_list, &wb->b_more_io);
195c986d1e2SAndrew Morton }
196c986d1e2SAndrew Morton 
1971c0eeaf5SJoern Engel static void inode_sync_complete(struct inode *inode)
1981c0eeaf5SJoern Engel {
1991c0eeaf5SJoern Engel 	/*
2001c0eeaf5SJoern Engel 	 * Prevent speculative execution through spin_unlock(&inode_lock);
2011c0eeaf5SJoern Engel 	 */
2021c0eeaf5SJoern Engel 	smp_mb();
2031c0eeaf5SJoern Engel 	wake_up_bit(&inode->i_state, __I_SYNC);
2041c0eeaf5SJoern Engel }
2051c0eeaf5SJoern Engel 
206d2caa3c5SJeff Layton static bool inode_dirtied_after(struct inode *inode, unsigned long t)
207d2caa3c5SJeff Layton {
208d2caa3c5SJeff Layton 	bool ret = time_after(inode->dirtied_when, t);
209d2caa3c5SJeff Layton #ifndef CONFIG_64BIT
210d2caa3c5SJeff Layton 	/*
211d2caa3c5SJeff Layton 	 * For inodes being constantly redirtied, dirtied_when can get stuck.
212d2caa3c5SJeff Layton 	 * It _appears_ to be in the future, but is actually in distant past.
213d2caa3c5SJeff Layton 	 * This test is necessary to prevent such wrapped-around relative times
2145b0830cbSJens Axboe 	 * from permanently stopping the whole bdi writeback.
215d2caa3c5SJeff Layton 	 */
216d2caa3c5SJeff Layton 	ret = ret && time_before_eq(inode->dirtied_when, jiffies);
217d2caa3c5SJeff Layton #endif
218d2caa3c5SJeff Layton 	return ret;
219d2caa3c5SJeff Layton }
220d2caa3c5SJeff Layton 
221c986d1e2SAndrew Morton /*
2222c136579SFengguang Wu  * Move expired dirty inodes from @delaying_queue to @dispatch_queue.
2232c136579SFengguang Wu  */
2242c136579SFengguang Wu static void move_expired_inodes(struct list_head *delaying_queue,
2252c136579SFengguang Wu 			       struct list_head *dispatch_queue,
2262c136579SFengguang Wu 				unsigned long *older_than_this)
2272c136579SFengguang Wu {
2285c03449dSShaohua Li 	LIST_HEAD(tmp);
2295c03449dSShaohua Li 	struct list_head *pos, *node;
230cf137307SJens Axboe 	struct super_block *sb = NULL;
2315c03449dSShaohua Li 	struct inode *inode;
232cf137307SJens Axboe 	int do_sb_sort = 0;
2335c03449dSShaohua Li 
2342c136579SFengguang Wu 	while (!list_empty(delaying_queue)) {
2357ccf19a8SNick Piggin 		inode = wb_inode(delaying_queue->prev);
2362c136579SFengguang Wu 		if (older_than_this &&
237d2caa3c5SJeff Layton 		    inode_dirtied_after(inode, *older_than_this))
2382c136579SFengguang Wu 			break;
239cf137307SJens Axboe 		if (sb && sb != inode->i_sb)
240cf137307SJens Axboe 			do_sb_sort = 1;
241cf137307SJens Axboe 		sb = inode->i_sb;
2427ccf19a8SNick Piggin 		list_move(&inode->i_wb_list, &tmp);
2435c03449dSShaohua Li 	}
2445c03449dSShaohua Li 
245cf137307SJens Axboe 	/* just one sb in list, splice to dispatch_queue and we're done */
246cf137307SJens Axboe 	if (!do_sb_sort) {
247cf137307SJens Axboe 		list_splice(&tmp, dispatch_queue);
248cf137307SJens Axboe 		return;
249cf137307SJens Axboe 	}
250cf137307SJens Axboe 
2515c03449dSShaohua Li 	/* Move inodes from one superblock together */
2525c03449dSShaohua Li 	while (!list_empty(&tmp)) {
2537ccf19a8SNick Piggin 		sb = wb_inode(tmp.prev)->i_sb;
2545c03449dSShaohua Li 		list_for_each_prev_safe(pos, node, &tmp) {
2557ccf19a8SNick Piggin 			inode = wb_inode(pos);
2565c03449dSShaohua Li 			if (inode->i_sb == sb)
2577ccf19a8SNick Piggin 				list_move(&inode->i_wb_list, dispatch_queue);
2582c136579SFengguang Wu 		}
2592c136579SFengguang Wu 	}
2605c03449dSShaohua Li }
2612c136579SFengguang Wu 
2622c136579SFengguang Wu /*
2632c136579SFengguang Wu  * Queue all expired dirty inodes for io, eldest first.
2644ea879b9SWu Fengguang  * Before
2654ea879b9SWu Fengguang  *         newly dirtied     b_dirty    b_io    b_more_io
2664ea879b9SWu Fengguang  *         =============>    gf         edc     BA
2674ea879b9SWu Fengguang  * After
2684ea879b9SWu Fengguang  *         newly dirtied     b_dirty    b_io    b_more_io
2694ea879b9SWu Fengguang  *         =============>    g          fBAedc
2704ea879b9SWu Fengguang  *                                           |
2714ea879b9SWu Fengguang  *                                           +--> dequeue for IO
2722c136579SFengguang Wu  */
27303ba3782SJens Axboe static void queue_io(struct bdi_writeback *wb, unsigned long *older_than_this)
2742c136579SFengguang Wu {
2754ea879b9SWu Fengguang 	list_splice_init(&wb->b_more_io, &wb->b_io);
27603ba3782SJens Axboe 	move_expired_inodes(&wb->b_dirty, &wb->b_io, older_than_this);
27766f3b8e2SJens Axboe }
27866f3b8e2SJens Axboe 
279a9185b41SChristoph Hellwig static int write_inode(struct inode *inode, struct writeback_control *wbc)
28066f3b8e2SJens Axboe {
28103ba3782SJens Axboe 	if (inode->i_sb->s_op->write_inode && !is_bad_inode(inode))
282a9185b41SChristoph Hellwig 		return inode->i_sb->s_op->write_inode(inode, wbc);
28303ba3782SJens Axboe 	return 0;
28466f3b8e2SJens Axboe }
28508d8e974SFengguang Wu 
2862c136579SFengguang Wu /*
28701c03194SChristoph Hellwig  * Wait for writeback on an inode to complete.
28801c03194SChristoph Hellwig  */
28901c03194SChristoph Hellwig static void inode_wait_for_writeback(struct inode *inode)
29001c03194SChristoph Hellwig {
29101c03194SChristoph Hellwig 	DEFINE_WAIT_BIT(wq, &inode->i_state, __I_SYNC);
29201c03194SChristoph Hellwig 	wait_queue_head_t *wqh;
29301c03194SChristoph Hellwig 
29401c03194SChristoph Hellwig 	wqh = bit_waitqueue(&inode->i_state, __I_SYNC);
29558a9d3d8SRichard Kennedy 	 while (inode->i_state & I_SYNC) {
29601c03194SChristoph Hellwig 		spin_unlock(&inode_lock);
29701c03194SChristoph Hellwig 		__wait_on_bit(wqh, &wq, inode_wait, TASK_UNINTERRUPTIBLE);
29801c03194SChristoph Hellwig 		spin_lock(&inode_lock);
29958a9d3d8SRichard Kennedy 	}
30001c03194SChristoph Hellwig }
30101c03194SChristoph Hellwig 
30201c03194SChristoph Hellwig /*
30301c03194SChristoph Hellwig  * Write out an inode's dirty pages.  Called under inode_lock.  Either the
30401c03194SChristoph Hellwig  * caller has ref on the inode (either via __iget or via syscall against an fd)
30501c03194SChristoph Hellwig  * or the inode has I_WILL_FREE set (via generic_forget_inode)
30601c03194SChristoph Hellwig  *
3071da177e4SLinus Torvalds  * If `wait' is set, wait on the writeout.
3081da177e4SLinus Torvalds  *
3091da177e4SLinus Torvalds  * The whole writeout design is quite complex and fragile.  We want to avoid
3101da177e4SLinus Torvalds  * starvation of particular inodes when others are being redirtied, prevent
3111da177e4SLinus Torvalds  * livelocks, etc.
3121da177e4SLinus Torvalds  *
3131da177e4SLinus Torvalds  * Called under inode_lock.
3141da177e4SLinus Torvalds  */
3151da177e4SLinus Torvalds static int
31601c03194SChristoph Hellwig writeback_single_inode(struct inode *inode, struct writeback_control *wbc)
3171da177e4SLinus Torvalds {
3181da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
31901c03194SChristoph Hellwig 	unsigned dirty;
3201da177e4SLinus Torvalds 	int ret;
3211da177e4SLinus Torvalds 
32201c03194SChristoph Hellwig 	if (!atomic_read(&inode->i_count))
32301c03194SChristoph Hellwig 		WARN_ON(!(inode->i_state & (I_WILL_FREE|I_FREEING)));
32401c03194SChristoph Hellwig 	else
32501c03194SChristoph Hellwig 		WARN_ON(inode->i_state & I_WILL_FREE);
32601c03194SChristoph Hellwig 
32701c03194SChristoph Hellwig 	if (inode->i_state & I_SYNC) {
32801c03194SChristoph Hellwig 		/*
32901c03194SChristoph Hellwig 		 * If this inode is locked for writeback and we are not doing
33066f3b8e2SJens Axboe 		 * writeback-for-data-integrity, move it to b_more_io so that
33101c03194SChristoph Hellwig 		 * writeback can proceed with the other inodes on s_io.
33201c03194SChristoph Hellwig 		 *
33301c03194SChristoph Hellwig 		 * We'll have another go at writing back this inode when we
33466f3b8e2SJens Axboe 		 * completed a full scan of b_io.
33501c03194SChristoph Hellwig 		 */
336a9185b41SChristoph Hellwig 		if (wbc->sync_mode != WB_SYNC_ALL) {
33701c03194SChristoph Hellwig 			requeue_io(inode);
33801c03194SChristoph Hellwig 			return 0;
33901c03194SChristoph Hellwig 		}
34001c03194SChristoph Hellwig 
34101c03194SChristoph Hellwig 		/*
34201c03194SChristoph Hellwig 		 * It's a data-integrity sync.  We must wait.
34301c03194SChristoph Hellwig 		 */
34401c03194SChristoph Hellwig 		inode_wait_for_writeback(inode);
34501c03194SChristoph Hellwig 	}
34601c03194SChristoph Hellwig 
3471c0eeaf5SJoern Engel 	BUG_ON(inode->i_state & I_SYNC);
3481da177e4SLinus Torvalds 
3495547e8aaSDmitry Monakhov 	/* Set I_SYNC, reset I_DIRTY_PAGES */
3501c0eeaf5SJoern Engel 	inode->i_state |= I_SYNC;
3515547e8aaSDmitry Monakhov 	inode->i_state &= ~I_DIRTY_PAGES;
3521da177e4SLinus Torvalds 	spin_unlock(&inode_lock);
3531da177e4SLinus Torvalds 
3541da177e4SLinus Torvalds 	ret = do_writepages(mapping, wbc);
3551da177e4SLinus Torvalds 
35626821ed4SChristoph Hellwig 	/*
35726821ed4SChristoph Hellwig 	 * Make sure to wait on the data before writing out the metadata.
35826821ed4SChristoph Hellwig 	 * This is important for filesystems that modify metadata on data
35926821ed4SChristoph Hellwig 	 * I/O completion.
36026821ed4SChristoph Hellwig 	 */
361a9185b41SChristoph Hellwig 	if (wbc->sync_mode == WB_SYNC_ALL) {
36226821ed4SChristoph Hellwig 		int err = filemap_fdatawait(mapping);
3631da177e4SLinus Torvalds 		if (ret == 0)
3641da177e4SLinus Torvalds 			ret = err;
3651da177e4SLinus Torvalds 	}
3661da177e4SLinus Torvalds 
3675547e8aaSDmitry Monakhov 	/*
3685547e8aaSDmitry Monakhov 	 * Some filesystems may redirty the inode during the writeback
3695547e8aaSDmitry Monakhov 	 * due to delalloc, clear dirty metadata flags right before
3705547e8aaSDmitry Monakhov 	 * write_inode()
3715547e8aaSDmitry Monakhov 	 */
3725547e8aaSDmitry Monakhov 	spin_lock(&inode_lock);
3735547e8aaSDmitry Monakhov 	dirty = inode->i_state & I_DIRTY;
3745547e8aaSDmitry Monakhov 	inode->i_state &= ~(I_DIRTY_SYNC | I_DIRTY_DATASYNC);
3755547e8aaSDmitry Monakhov 	spin_unlock(&inode_lock);
37626821ed4SChristoph Hellwig 	/* Don't write the inode if only I_DIRTY_PAGES was set */
37726821ed4SChristoph Hellwig 	if (dirty & (I_DIRTY_SYNC | I_DIRTY_DATASYNC)) {
378a9185b41SChristoph Hellwig 		int err = write_inode(inode, wbc);
3791da177e4SLinus Torvalds 		if (ret == 0)
3801da177e4SLinus Torvalds 			ret = err;
3811da177e4SLinus Torvalds 	}
3821da177e4SLinus Torvalds 
3831da177e4SLinus Torvalds 	spin_lock(&inode_lock);
3841c0eeaf5SJoern Engel 	inode->i_state &= ~I_SYNC;
385a4ffdde6SAl Viro 	if (!(inode->i_state & I_FREEING)) {
38623539afcSWu Fengguang 		if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) {
3871da177e4SLinus Torvalds 			/*
3881da177e4SLinus Torvalds 			 * We didn't write back all the pages.  nfs_writepages()
389a50aeb40SWu Fengguang 			 * sometimes bales out without doing anything.
3901da177e4SLinus Torvalds 			 */
3911da177e4SLinus Torvalds 			inode->i_state |= I_DIRTY_PAGES;
3928bc3be27SFengguang Wu 			if (wbc->nr_to_write <= 0) {
3938bc3be27SFengguang Wu 				/*
3948bc3be27SFengguang Wu 				 * slice used up: queue for next turn
3958bc3be27SFengguang Wu 				 */
3960e0f4fc2SKen Chen 				requeue_io(inode);
3971da177e4SLinus Torvalds 			} else {
3981da177e4SLinus Torvalds 				/*
399a50aeb40SWu Fengguang 				 * Writeback blocked by something other than
400a50aeb40SWu Fengguang 				 * congestion. Delay the inode for some time to
401a50aeb40SWu Fengguang 				 * avoid spinning on the CPU (100% iowait)
402a50aeb40SWu Fengguang 				 * retrying writeback of the dirty page/inode
403a50aeb40SWu Fengguang 				 * that cannot be performed immediately.
4048bc3be27SFengguang Wu 				 */
4058bc3be27SFengguang Wu 				redirty_tail(inode);
4068bc3be27SFengguang Wu 			}
40723539afcSWu Fengguang 		} else if (inode->i_state & I_DIRTY) {
40823539afcSWu Fengguang 			/*
40923539afcSWu Fengguang 			 * Filesystems can dirty the inode during writeback
41023539afcSWu Fengguang 			 * operations, such as delayed allocation during
41123539afcSWu Fengguang 			 * submission or metadata updates after data IO
41223539afcSWu Fengguang 			 * completion.
41323539afcSWu Fengguang 			 */
41423539afcSWu Fengguang 			redirty_tail(inode);
4151da177e4SLinus Torvalds 		} else {
4161da177e4SLinus Torvalds 			/*
4179e38d86fSNick Piggin 			 * The inode is clean.  At this point we either have
4189e38d86fSNick Piggin 			 * a reference to the inode or it's on it's way out.
4199e38d86fSNick Piggin 			 * No need to add it back to the LRU.
4201da177e4SLinus Torvalds 			 */
4217ccf19a8SNick Piggin 			list_del_init(&inode->i_wb_list);
4221da177e4SLinus Torvalds 		}
4231da177e4SLinus Torvalds 	}
4241c0eeaf5SJoern Engel 	inode_sync_complete(inode);
4251da177e4SLinus Torvalds 	return ret;
4261da177e4SLinus Torvalds }
4271da177e4SLinus Torvalds 
42803ba3782SJens Axboe /*
429d19de7edSChristoph Hellwig  * For background writeback the caller does not have the sb pinned
43003ba3782SJens Axboe  * before calling writeback. So make sure that we do pin it, so it doesn't
43103ba3782SJens Axboe  * go away while we are writing inodes from it.
43203ba3782SJens Axboe  */
433d19de7edSChristoph Hellwig static bool pin_sb_for_writeback(struct super_block *sb)
4341da177e4SLinus Torvalds {
43503ba3782SJens Axboe 	spin_lock(&sb_lock);
43629cb4859SChristoph Hellwig 	if (list_empty(&sb->s_instances)) {
43703ba3782SJens Axboe 		spin_unlock(&sb_lock);
43829cb4859SChristoph Hellwig 		return false;
43903ba3782SJens Axboe 	}
44029cb4859SChristoph Hellwig 
44129cb4859SChristoph Hellwig 	sb->s_count++;
44229cb4859SChristoph Hellwig 	spin_unlock(&sb_lock);
44329cb4859SChristoph Hellwig 
44429cb4859SChristoph Hellwig 	if (down_read_trylock(&sb->s_umount)) {
44529cb4859SChristoph Hellwig 		if (sb->s_root)
44629cb4859SChristoph Hellwig 			return true;
44703ba3782SJens Axboe 		up_read(&sb->s_umount);
44803ba3782SJens Axboe 	}
44929cb4859SChristoph Hellwig 
45029cb4859SChristoph Hellwig 	put_super(sb);
451d19de7edSChristoph Hellwig 	return false;
45203ba3782SJens Axboe }
45303ba3782SJens Axboe 
454f11c9c5cSEdward Shishkin /*
455f11c9c5cSEdward Shishkin  * Write a portion of b_io inodes which belong to @sb.
456edadfb10SChristoph Hellwig  *
457edadfb10SChristoph Hellwig  * If @only_this_sb is true, then find and write all such
458f11c9c5cSEdward Shishkin  * inodes. Otherwise write only ones which go sequentially
459f11c9c5cSEdward Shishkin  * in reverse order.
460edadfb10SChristoph Hellwig  *
461f11c9c5cSEdward Shishkin  * Return 1, if the caller writeback routine should be
462f11c9c5cSEdward Shishkin  * interrupted. Otherwise return 0.
463f11c9c5cSEdward Shishkin  */
464edadfb10SChristoph Hellwig static int writeback_sb_inodes(struct super_block *sb, struct bdi_writeback *wb,
465edadfb10SChristoph Hellwig 		struct writeback_control *wbc, bool only_this_sb)
46603ba3782SJens Axboe {
46703ba3782SJens Axboe 	while (!list_empty(&wb->b_io)) {
468f11c9c5cSEdward Shishkin 		long pages_skipped;
4697ccf19a8SNick Piggin 		struct inode *inode = wb_inode(wb->b_io.prev);
470edadfb10SChristoph Hellwig 
471edadfb10SChristoph Hellwig 		if (inode->i_sb != sb) {
472edadfb10SChristoph Hellwig 			if (only_this_sb) {
473edadfb10SChristoph Hellwig 				/*
474edadfb10SChristoph Hellwig 				 * We only want to write back data for this
475edadfb10SChristoph Hellwig 				 * superblock, move all inodes not belonging
476edadfb10SChristoph Hellwig 				 * to it back onto the dirty list.
477edadfb10SChristoph Hellwig 				 */
47866f3b8e2SJens Axboe 				redirty_tail(inode);
47966f3b8e2SJens Axboe 				continue;
48066f3b8e2SJens Axboe 			}
481edadfb10SChristoph Hellwig 
482edadfb10SChristoph Hellwig 			/*
483edadfb10SChristoph Hellwig 			 * The inode belongs to a different superblock.
484edadfb10SChristoph Hellwig 			 * Bounce back to the caller to unpin this and
485edadfb10SChristoph Hellwig 			 * pin the next superblock.
486edadfb10SChristoph Hellwig 			 */
487f11c9c5cSEdward Shishkin 			return 0;
488edadfb10SChristoph Hellwig 		}
489edadfb10SChristoph Hellwig 
49084a89245SWu Fengguang 		if (inode->i_state & (I_NEW | I_WILL_FREE)) {
4917ef0d737SNick Piggin 			requeue_io(inode);
4927ef0d737SNick Piggin 			continue;
4937ef0d737SNick Piggin 		}
494d2caa3c5SJeff Layton 		/*
495d2caa3c5SJeff Layton 		 * Was this inode dirtied after sync_sb_inodes was called?
496d2caa3c5SJeff Layton 		 * This keeps sync from extra jobs and livelock.
497d2caa3c5SJeff Layton 		 */
498f11c9c5cSEdward Shishkin 		if (inode_dirtied_after(inode, wbc->wb_start))
499f11c9c5cSEdward Shishkin 			return 1;
5001da177e4SLinus Torvalds 
501a4ffdde6SAl Viro 		BUG_ON(inode->i_state & I_FREEING);
5021da177e4SLinus Torvalds 		__iget(inode);
5031da177e4SLinus Torvalds 		pages_skipped = wbc->pages_skipped;
50401c03194SChristoph Hellwig 		writeback_single_inode(inode, wbc);
5051da177e4SLinus Torvalds 		if (wbc->pages_skipped != pages_skipped) {
5061da177e4SLinus Torvalds 			/*
5071da177e4SLinus Torvalds 			 * writeback is not making progress due to locked
5081da177e4SLinus Torvalds 			 * buffers.  Skip this inode for now.
5091da177e4SLinus Torvalds 			 */
510f57b9b7bSAndrew Morton 			redirty_tail(inode);
5111da177e4SLinus Torvalds 		}
5121da177e4SLinus Torvalds 		spin_unlock(&inode_lock);
5131da177e4SLinus Torvalds 		iput(inode);
5144ffc8444SOGAWA Hirofumi 		cond_resched();
5151da177e4SLinus Torvalds 		spin_lock(&inode_lock);
5168bc3be27SFengguang Wu 		if (wbc->nr_to_write <= 0) {
5178bc3be27SFengguang Wu 			wbc->more_io = 1;
518f11c9c5cSEdward Shishkin 			return 1;
5191da177e4SLinus Torvalds 		}
52003ba3782SJens Axboe 		if (!list_empty(&wb->b_more_io))
5218bc3be27SFengguang Wu 			wbc->more_io = 1;
5228bc3be27SFengguang Wu 	}
523f11c9c5cSEdward Shishkin 	/* b_io is empty */
524f11c9c5cSEdward Shishkin 	return 1;
525f11c9c5cSEdward Shishkin }
52638f21977SNick Piggin 
5279c3a8ee8SChristoph Hellwig void writeback_inodes_wb(struct bdi_writeback *wb,
528f11c9c5cSEdward Shishkin 		struct writeback_control *wbc)
529f11c9c5cSEdward Shishkin {
530f11c9c5cSEdward Shishkin 	int ret = 0;
5319ecc2738SJens Axboe 
5327624ee72SJan Kara 	if (!wbc->wb_start)
533f11c9c5cSEdward Shishkin 		wbc->wb_start = jiffies; /* livelock avoidance */
534f11c9c5cSEdward Shishkin 	spin_lock(&inode_lock);
535f11c9c5cSEdward Shishkin 	if (!wbc->for_kupdate || list_empty(&wb->b_io))
536f11c9c5cSEdward Shishkin 		queue_io(wb, wbc->older_than_this);
537f11c9c5cSEdward Shishkin 
538f11c9c5cSEdward Shishkin 	while (!list_empty(&wb->b_io)) {
5397ccf19a8SNick Piggin 		struct inode *inode = wb_inode(wb->b_io.prev);
540f11c9c5cSEdward Shishkin 		struct super_block *sb = inode->i_sb;
541f11c9c5cSEdward Shishkin 
542334132aeSChristoph Hellwig 		if (!pin_sb_for_writeback(sb)) {
543334132aeSChristoph Hellwig 			requeue_io(inode);
544d19de7edSChristoph Hellwig 			continue;
545334132aeSChristoph Hellwig 		}
546edadfb10SChristoph Hellwig 		ret = writeback_sb_inodes(sb, wb, wbc, false);
547d19de7edSChristoph Hellwig 		drop_super(sb);
548f11c9c5cSEdward Shishkin 
549f11c9c5cSEdward Shishkin 		if (ret)
550f11c9c5cSEdward Shishkin 			break;
551f11c9c5cSEdward Shishkin 	}
55266f3b8e2SJens Axboe 	spin_unlock(&inode_lock);
55366f3b8e2SJens Axboe 	/* Leave any unwritten inodes on b_io */
55466f3b8e2SJens Axboe }
55566f3b8e2SJens Axboe 
556edadfb10SChristoph Hellwig static void __writeback_inodes_sb(struct super_block *sb,
557edadfb10SChristoph Hellwig 		struct bdi_writeback *wb, struct writeback_control *wbc)
558edadfb10SChristoph Hellwig {
559edadfb10SChristoph Hellwig 	WARN_ON(!rwsem_is_locked(&sb->s_umount));
560edadfb10SChristoph Hellwig 
561edadfb10SChristoph Hellwig 	spin_lock(&inode_lock);
562edadfb10SChristoph Hellwig 	if (!wbc->for_kupdate || list_empty(&wb->b_io))
563edadfb10SChristoph Hellwig 		queue_io(wb, wbc->older_than_this);
564edadfb10SChristoph Hellwig 	writeback_sb_inodes(sb, wb, wbc, true);
565edadfb10SChristoph Hellwig 	spin_unlock(&inode_lock);
566edadfb10SChristoph Hellwig }
567edadfb10SChristoph Hellwig 
56803ba3782SJens Axboe /*
56903ba3782SJens Axboe  * The maximum number of pages to writeout in a single bdi flush/kupdate
57003ba3782SJens Axboe  * operation.  We do this so we don't hold I_SYNC against an inode for
57103ba3782SJens Axboe  * enormous amounts of time, which would block a userspace task which has
57203ba3782SJens Axboe  * been forced to throttle against that inode.  Also, the code reevaluates
57303ba3782SJens Axboe  * the dirty each time it has written this many pages.
57403ba3782SJens Axboe  */
57503ba3782SJens Axboe #define MAX_WRITEBACK_PAGES     1024
57603ba3782SJens Axboe 
57703ba3782SJens Axboe static inline bool over_bground_thresh(void)
57803ba3782SJens Axboe {
57903ba3782SJens Axboe 	unsigned long background_thresh, dirty_thresh;
58003ba3782SJens Axboe 
58116c4042fSWu Fengguang 	global_dirty_limits(&background_thresh, &dirty_thresh);
58203ba3782SJens Axboe 
58303ba3782SJens Axboe 	return (global_page_state(NR_FILE_DIRTY) +
58403ba3782SJens Axboe 		global_page_state(NR_UNSTABLE_NFS) >= background_thresh);
58503ba3782SJens Axboe }
58603ba3782SJens Axboe 
58703ba3782SJens Axboe /*
58803ba3782SJens Axboe  * Explicit flushing or periodic writeback of "old" data.
58903ba3782SJens Axboe  *
59003ba3782SJens Axboe  * Define "old": the first time one of an inode's pages is dirtied, we mark the
59103ba3782SJens Axboe  * dirtying-time in the inode's address_space.  So this periodic writeback code
59203ba3782SJens Axboe  * just walks the superblock inode list, writing back any inodes which are
59303ba3782SJens Axboe  * older than a specific point in time.
59403ba3782SJens Axboe  *
59503ba3782SJens Axboe  * Try to run once per dirty_writeback_interval.  But if a writeback event
59603ba3782SJens Axboe  * takes longer than a dirty_writeback_interval interval, then leave a
59703ba3782SJens Axboe  * one-second gap.
59803ba3782SJens Axboe  *
59903ba3782SJens Axboe  * older_than_this takes precedence over nr_to_write.  So we'll only write back
60003ba3782SJens Axboe  * all dirty pages if they are all attached to "old" mappings.
60103ba3782SJens Axboe  */
602c4a77a6cSJens Axboe static long wb_writeback(struct bdi_writeback *wb,
60383ba7b07SChristoph Hellwig 			 struct wb_writeback_work *work)
60403ba3782SJens Axboe {
60503ba3782SJens Axboe 	struct writeback_control wbc = {
60683ba7b07SChristoph Hellwig 		.sync_mode		= work->sync_mode,
60703ba3782SJens Axboe 		.older_than_this	= NULL,
60883ba7b07SChristoph Hellwig 		.for_kupdate		= work->for_kupdate,
60983ba7b07SChristoph Hellwig 		.for_background		= work->for_background,
61083ba7b07SChristoph Hellwig 		.range_cyclic		= work->range_cyclic,
61103ba3782SJens Axboe 	};
61203ba3782SJens Axboe 	unsigned long oldest_jif;
61303ba3782SJens Axboe 	long wrote = 0;
614a5989bdcSJan Kara 	struct inode *inode;
61503ba3782SJens Axboe 
61603ba3782SJens Axboe 	if (wbc.for_kupdate) {
61703ba3782SJens Axboe 		wbc.older_than_this = &oldest_jif;
61803ba3782SJens Axboe 		oldest_jif = jiffies -
61903ba3782SJens Axboe 				msecs_to_jiffies(dirty_expire_interval * 10);
62003ba3782SJens Axboe 	}
621c4a77a6cSJens Axboe 	if (!wbc.range_cyclic) {
622c4a77a6cSJens Axboe 		wbc.range_start = 0;
623c4a77a6cSJens Axboe 		wbc.range_end = LLONG_MAX;
624c4a77a6cSJens Axboe 	}
62503ba3782SJens Axboe 
6267624ee72SJan Kara 	wbc.wb_start = jiffies; /* livelock avoidance */
62703ba3782SJens Axboe 	for (;;) {
62803ba3782SJens Axboe 		/*
629d3ddec76SWu Fengguang 		 * Stop writeback when nr_pages has been consumed
63003ba3782SJens Axboe 		 */
63183ba7b07SChristoph Hellwig 		if (work->nr_pages <= 0)
63203ba3782SJens Axboe 			break;
63303ba3782SJens Axboe 
63403ba3782SJens Axboe 		/*
635d3ddec76SWu Fengguang 		 * For background writeout, stop when we are below the
636d3ddec76SWu Fengguang 		 * background dirty threshold
63703ba3782SJens Axboe 		 */
63883ba7b07SChristoph Hellwig 		if (work->for_background && !over_bground_thresh())
63903ba3782SJens Axboe 			break;
64003ba3782SJens Axboe 
64103ba3782SJens Axboe 		wbc.more_io = 0;
64203ba3782SJens Axboe 		wbc.nr_to_write = MAX_WRITEBACK_PAGES;
64303ba3782SJens Axboe 		wbc.pages_skipped = 0;
644028c2dd1SDave Chinner 
645028c2dd1SDave Chinner 		trace_wbc_writeback_start(&wbc, wb->bdi);
64683ba7b07SChristoph Hellwig 		if (work->sb)
64783ba7b07SChristoph Hellwig 			__writeback_inodes_sb(work->sb, wb, &wbc);
648edadfb10SChristoph Hellwig 		else
64903ba3782SJens Axboe 			writeback_inodes_wb(wb, &wbc);
650028c2dd1SDave Chinner 		trace_wbc_writeback_written(&wbc, wb->bdi);
651028c2dd1SDave Chinner 
65283ba7b07SChristoph Hellwig 		work->nr_pages -= MAX_WRITEBACK_PAGES - wbc.nr_to_write;
65303ba3782SJens Axboe 		wrote += MAX_WRITEBACK_PAGES - wbc.nr_to_write;
65403ba3782SJens Axboe 
65503ba3782SJens Axboe 		/*
65671fd05a8SJens Axboe 		 * If we consumed everything, see if we have more
65703ba3782SJens Axboe 		 */
65871fd05a8SJens Axboe 		if (wbc.nr_to_write <= 0)
65971fd05a8SJens Axboe 			continue;
66071fd05a8SJens Axboe 		/*
66171fd05a8SJens Axboe 		 * Didn't write everything and we don't have more IO, bail
66271fd05a8SJens Axboe 		 */
66371fd05a8SJens Axboe 		if (!wbc.more_io)
66471fd05a8SJens Axboe 			break;
66571fd05a8SJens Axboe 		/*
66671fd05a8SJens Axboe 		 * Did we write something? Try for more
66771fd05a8SJens Axboe 		 */
668a5989bdcSJan Kara 		if (wbc.nr_to_write < MAX_WRITEBACK_PAGES)
66903ba3782SJens Axboe 			continue;
670a5989bdcSJan Kara 		/*
671a5989bdcSJan Kara 		 * Nothing written. Wait for some inode to
672a5989bdcSJan Kara 		 * become available for writeback. Otherwise
673a5989bdcSJan Kara 		 * we'll just busyloop.
674a5989bdcSJan Kara 		 */
675a5989bdcSJan Kara 		spin_lock(&inode_lock);
676a5989bdcSJan Kara 		if (!list_empty(&wb->b_more_io))  {
6777ccf19a8SNick Piggin 			inode = wb_inode(wb->b_more_io.prev);
678028c2dd1SDave Chinner 			trace_wbc_writeback_wait(&wbc, wb->bdi);
679a5989bdcSJan Kara 			inode_wait_for_writeback(inode);
680a5989bdcSJan Kara 		}
681a5989bdcSJan Kara 		spin_unlock(&inode_lock);
68203ba3782SJens Axboe 	}
68303ba3782SJens Axboe 
68403ba3782SJens Axboe 	return wrote;
68503ba3782SJens Axboe }
68603ba3782SJens Axboe 
68703ba3782SJens Axboe /*
68883ba7b07SChristoph Hellwig  * Return the next wb_writeback_work struct that hasn't been processed yet.
68903ba3782SJens Axboe  */
69083ba7b07SChristoph Hellwig static struct wb_writeback_work *
69108852b6dSMinchan Kim get_next_work_item(struct backing_dev_info *bdi)
69203ba3782SJens Axboe {
69383ba7b07SChristoph Hellwig 	struct wb_writeback_work *work = NULL;
69403ba3782SJens Axboe 
6956467716aSArtem Bityutskiy 	spin_lock_bh(&bdi->wb_lock);
69683ba7b07SChristoph Hellwig 	if (!list_empty(&bdi->work_list)) {
69783ba7b07SChristoph Hellwig 		work = list_entry(bdi->work_list.next,
69883ba7b07SChristoph Hellwig 				  struct wb_writeback_work, list);
69983ba7b07SChristoph Hellwig 		list_del_init(&work->list);
70003ba3782SJens Axboe 	}
7016467716aSArtem Bityutskiy 	spin_unlock_bh(&bdi->wb_lock);
70283ba7b07SChristoph Hellwig 	return work;
70303ba3782SJens Axboe }
70403ba3782SJens Axboe 
70503ba3782SJens Axboe static long wb_check_old_data_flush(struct bdi_writeback *wb)
70603ba3782SJens Axboe {
70703ba3782SJens Axboe 	unsigned long expired;
70803ba3782SJens Axboe 	long nr_pages;
70903ba3782SJens Axboe 
71069b62d01SJens Axboe 	/*
71169b62d01SJens Axboe 	 * When set to zero, disable periodic writeback
71269b62d01SJens Axboe 	 */
71369b62d01SJens Axboe 	if (!dirty_writeback_interval)
71469b62d01SJens Axboe 		return 0;
71569b62d01SJens Axboe 
71603ba3782SJens Axboe 	expired = wb->last_old_flush +
71703ba3782SJens Axboe 			msecs_to_jiffies(dirty_writeback_interval * 10);
71803ba3782SJens Axboe 	if (time_before(jiffies, expired))
71903ba3782SJens Axboe 		return 0;
72003ba3782SJens Axboe 
72103ba3782SJens Axboe 	wb->last_old_flush = jiffies;
72203ba3782SJens Axboe 	nr_pages = global_page_state(NR_FILE_DIRTY) +
72303ba3782SJens Axboe 			global_page_state(NR_UNSTABLE_NFS) +
724cffbc8aaSDave Chinner 			get_nr_dirty_inodes();
72503ba3782SJens Axboe 
726c4a77a6cSJens Axboe 	if (nr_pages) {
72783ba7b07SChristoph Hellwig 		struct wb_writeback_work work = {
728c4a77a6cSJens Axboe 			.nr_pages	= nr_pages,
729c4a77a6cSJens Axboe 			.sync_mode	= WB_SYNC_NONE,
730c4a77a6cSJens Axboe 			.for_kupdate	= 1,
731c4a77a6cSJens Axboe 			.range_cyclic	= 1,
732c4a77a6cSJens Axboe 		};
733c4a77a6cSJens Axboe 
73483ba7b07SChristoph Hellwig 		return wb_writeback(wb, &work);
735c4a77a6cSJens Axboe 	}
73603ba3782SJens Axboe 
73703ba3782SJens Axboe 	return 0;
73803ba3782SJens Axboe }
73903ba3782SJens Axboe 
74003ba3782SJens Axboe /*
74103ba3782SJens Axboe  * Retrieve work items and do the writeback they describe
74203ba3782SJens Axboe  */
74303ba3782SJens Axboe long wb_do_writeback(struct bdi_writeback *wb, int force_wait)
74403ba3782SJens Axboe {
74503ba3782SJens Axboe 	struct backing_dev_info *bdi = wb->bdi;
74683ba7b07SChristoph Hellwig 	struct wb_writeback_work *work;
747c4a77a6cSJens Axboe 	long wrote = 0;
74803ba3782SJens Axboe 
74981d73a32SJan Kara 	set_bit(BDI_writeback_running, &wb->bdi->state);
75008852b6dSMinchan Kim 	while ((work = get_next_work_item(bdi)) != NULL) {
75103ba3782SJens Axboe 		/*
75203ba3782SJens Axboe 		 * Override sync mode, in case we must wait for completion
75383ba7b07SChristoph Hellwig 		 * because this thread is exiting now.
75403ba3782SJens Axboe 		 */
75503ba3782SJens Axboe 		if (force_wait)
75683ba7b07SChristoph Hellwig 			work->sync_mode = WB_SYNC_ALL;
75783ba7b07SChristoph Hellwig 
758455b2864SDave Chinner 		trace_writeback_exec(bdi, work);
759455b2864SDave Chinner 
76083ba7b07SChristoph Hellwig 		wrote += wb_writeback(wb, work);
76103ba3782SJens Axboe 
76203ba3782SJens Axboe 		/*
76383ba7b07SChristoph Hellwig 		 * Notify the caller of completion if this is a synchronous
76483ba7b07SChristoph Hellwig 		 * work item, otherwise just free it.
76503ba3782SJens Axboe 		 */
76683ba7b07SChristoph Hellwig 		if (work->done)
76783ba7b07SChristoph Hellwig 			complete(work->done);
76883ba7b07SChristoph Hellwig 		else
76983ba7b07SChristoph Hellwig 			kfree(work);
77003ba3782SJens Axboe 	}
77103ba3782SJens Axboe 
77203ba3782SJens Axboe 	/*
77303ba3782SJens Axboe 	 * Check for periodic writeback, kupdated() style
77403ba3782SJens Axboe 	 */
77503ba3782SJens Axboe 	wrote += wb_check_old_data_flush(wb);
77681d73a32SJan Kara 	clear_bit(BDI_writeback_running, &wb->bdi->state);
77703ba3782SJens Axboe 
77803ba3782SJens Axboe 	return wrote;
77903ba3782SJens Axboe }
78003ba3782SJens Axboe 
78103ba3782SJens Axboe /*
78203ba3782SJens Axboe  * Handle writeback of dirty data for the device backed by this bdi. Also
78303ba3782SJens Axboe  * wakes up periodically and does kupdated style flushing.
78403ba3782SJens Axboe  */
78508243900SChristoph Hellwig int bdi_writeback_thread(void *data)
78603ba3782SJens Axboe {
78708243900SChristoph Hellwig 	struct bdi_writeback *wb = data;
78808243900SChristoph Hellwig 	struct backing_dev_info *bdi = wb->bdi;
78903ba3782SJens Axboe 	long pages_written;
79003ba3782SJens Axboe 
79108243900SChristoph Hellwig 	current->flags |= PF_FLUSHER | PF_SWAPWRITE;
79208243900SChristoph Hellwig 	set_freezable();
793ecd58403SArtem Bityutskiy 	wb->last_active = jiffies;
79403ba3782SJens Axboe 
79503ba3782SJens Axboe 	/*
79608243900SChristoph Hellwig 	 * Our parent may run at a different priority, just set us to normal
79703ba3782SJens Axboe 	 */
79808243900SChristoph Hellwig 	set_user_nice(current, 0);
79908243900SChristoph Hellwig 
800455b2864SDave Chinner 	trace_writeback_thread_start(bdi);
801455b2864SDave Chinner 
80203ba3782SJens Axboe 	while (!kthread_should_stop()) {
8036467716aSArtem Bityutskiy 		/*
8046467716aSArtem Bityutskiy 		 * Remove own delayed wake-up timer, since we are already awake
8056467716aSArtem Bityutskiy 		 * and we'll take care of the preriodic write-back.
8066467716aSArtem Bityutskiy 		 */
8076467716aSArtem Bityutskiy 		del_timer(&wb->wakeup_timer);
8086467716aSArtem Bityutskiy 
80903ba3782SJens Axboe 		pages_written = wb_do_writeback(wb, 0);
81003ba3782SJens Axboe 
811455b2864SDave Chinner 		trace_writeback_pages_written(pages_written);
812455b2864SDave Chinner 
81303ba3782SJens Axboe 		if (pages_written)
814ecd58403SArtem Bityutskiy 			wb->last_active = jiffies;
81503ba3782SJens Axboe 
816297252c8SArtem Bityutskiy 		set_current_state(TASK_INTERRUPTIBLE);
817b76b4014SJ. Bruce Fields 		if (!list_empty(&bdi->work_list) || kthread_should_stop()) {
818297252c8SArtem Bityutskiy 			__set_current_state(TASK_RUNNING);
819297252c8SArtem Bityutskiy 			continue;
82003ba3782SJens Axboe 		}
82103ba3782SJens Axboe 
822253c34e9SArtem Bityutskiy 		if (wb_has_dirty_io(wb) && dirty_writeback_interval)
823fff5b85aSArtem Bityutskiy 			schedule_timeout(msecs_to_jiffies(dirty_writeback_interval * 10));
824253c34e9SArtem Bityutskiy 		else {
825253c34e9SArtem Bityutskiy 			/*
826253c34e9SArtem Bityutskiy 			 * We have nothing to do, so can go sleep without any
827253c34e9SArtem Bityutskiy 			 * timeout and save power. When a work is queued or
828253c34e9SArtem Bityutskiy 			 * something is made dirty - we will be woken up.
829253c34e9SArtem Bityutskiy 			 */
83069b62d01SJens Axboe 			schedule();
831f9eadbbdSJens Axboe 		}
83269b62d01SJens Axboe 
83303ba3782SJens Axboe 		try_to_freeze();
83403ba3782SJens Axboe 	}
83503ba3782SJens Axboe 
836fff5b85aSArtem Bityutskiy 	/* Flush any work that raced with us exiting */
83708243900SChristoph Hellwig 	if (!list_empty(&bdi->work_list))
83808243900SChristoph Hellwig 		wb_do_writeback(wb, 1);
839455b2864SDave Chinner 
840455b2864SDave Chinner 	trace_writeback_thread_stop(bdi);
84103ba3782SJens Axboe 	return 0;
84203ba3782SJens Axboe }
84303ba3782SJens Axboe 
84408243900SChristoph Hellwig 
84503ba3782SJens Axboe /*
84603ba3782SJens Axboe  * Start writeback of `nr_pages' pages.  If `nr_pages' is zero, write back
84703ba3782SJens Axboe  * the whole world.
84803ba3782SJens Axboe  */
84903ba3782SJens Axboe void wakeup_flusher_threads(long nr_pages)
85003ba3782SJens Axboe {
851b8c2f347SChristoph Hellwig 	struct backing_dev_info *bdi;
852b8c2f347SChristoph Hellwig 
85383ba7b07SChristoph Hellwig 	if (!nr_pages) {
85483ba7b07SChristoph Hellwig 		nr_pages = global_page_state(NR_FILE_DIRTY) +
85503ba3782SJens Axboe 				global_page_state(NR_UNSTABLE_NFS);
856b8c2f347SChristoph Hellwig 	}
857b8c2f347SChristoph Hellwig 
858b8c2f347SChristoph Hellwig 	rcu_read_lock();
859b8c2f347SChristoph Hellwig 	list_for_each_entry_rcu(bdi, &bdi_list, bdi_list) {
860b8c2f347SChristoph Hellwig 		if (!bdi_has_dirty_io(bdi))
861b8c2f347SChristoph Hellwig 			continue;
86283ba7b07SChristoph Hellwig 		__bdi_start_writeback(bdi, nr_pages, false, false);
863b8c2f347SChristoph Hellwig 	}
864b8c2f347SChristoph Hellwig 	rcu_read_unlock();
86503ba3782SJens Axboe }
86603ba3782SJens Axboe 
86703ba3782SJens Axboe static noinline void block_dump___mark_inode_dirty(struct inode *inode)
86803ba3782SJens Axboe {
86903ba3782SJens Axboe 	if (inode->i_ino || strcmp(inode->i_sb->s_id, "bdev")) {
87003ba3782SJens Axboe 		struct dentry *dentry;
87103ba3782SJens Axboe 		const char *name = "?";
87203ba3782SJens Axboe 
87303ba3782SJens Axboe 		dentry = d_find_alias(inode);
87403ba3782SJens Axboe 		if (dentry) {
87503ba3782SJens Axboe 			spin_lock(&dentry->d_lock);
87603ba3782SJens Axboe 			name = (const char *) dentry->d_name.name;
87703ba3782SJens Axboe 		}
87803ba3782SJens Axboe 		printk(KERN_DEBUG
87903ba3782SJens Axboe 		       "%s(%d): dirtied inode %lu (%s) on %s\n",
88003ba3782SJens Axboe 		       current->comm, task_pid_nr(current), inode->i_ino,
88103ba3782SJens Axboe 		       name, inode->i_sb->s_id);
88203ba3782SJens Axboe 		if (dentry) {
88303ba3782SJens Axboe 			spin_unlock(&dentry->d_lock);
88403ba3782SJens Axboe 			dput(dentry);
88503ba3782SJens Axboe 		}
88603ba3782SJens Axboe 	}
88703ba3782SJens Axboe }
88803ba3782SJens Axboe 
88903ba3782SJens Axboe /**
89003ba3782SJens Axboe  *	__mark_inode_dirty -	internal function
89103ba3782SJens Axboe  *	@inode: inode to mark
89203ba3782SJens Axboe  *	@flags: what kind of dirty (i.e. I_DIRTY_SYNC)
89303ba3782SJens Axboe  *	Mark an inode as dirty. Callers should use mark_inode_dirty or
89403ba3782SJens Axboe  *  	mark_inode_dirty_sync.
89503ba3782SJens Axboe  *
89603ba3782SJens Axboe  * Put the inode on the super block's dirty list.
89703ba3782SJens Axboe  *
89803ba3782SJens Axboe  * CAREFUL! We mark it dirty unconditionally, but move it onto the
89903ba3782SJens Axboe  * dirty list only if it is hashed or if it refers to a blockdev.
90003ba3782SJens Axboe  * If it was not hashed, it will never be added to the dirty list
90103ba3782SJens Axboe  * even if it is later hashed, as it will have been marked dirty already.
90203ba3782SJens Axboe  *
90303ba3782SJens Axboe  * In short, make sure you hash any inodes _before_ you start marking
90403ba3782SJens Axboe  * them dirty.
90503ba3782SJens Axboe  *
90603ba3782SJens Axboe  * This function *must* be atomic for the I_DIRTY_PAGES case -
90703ba3782SJens Axboe  * set_page_dirty() is called under spinlock in several places.
90803ba3782SJens Axboe  *
90903ba3782SJens Axboe  * Note that for blockdevs, inode->dirtied_when represents the dirtying time of
91003ba3782SJens Axboe  * the block-special inode (/dev/hda1) itself.  And the ->dirtied_when field of
91103ba3782SJens Axboe  * the kernel-internal blockdev inode represents the dirtying time of the
91203ba3782SJens Axboe  * blockdev's pages.  This is why for I_DIRTY_PAGES we always use
91303ba3782SJens Axboe  * page->mapping->host, so the page-dirtying time is recorded in the internal
91403ba3782SJens Axboe  * blockdev inode.
91503ba3782SJens Axboe  */
91603ba3782SJens Axboe void __mark_inode_dirty(struct inode *inode, int flags)
91703ba3782SJens Axboe {
91803ba3782SJens Axboe 	struct super_block *sb = inode->i_sb;
919253c34e9SArtem Bityutskiy 	struct backing_dev_info *bdi = NULL;
920253c34e9SArtem Bityutskiy 	bool wakeup_bdi = false;
92103ba3782SJens Axboe 
92203ba3782SJens Axboe 	/*
92303ba3782SJens Axboe 	 * Don't do this for I_DIRTY_PAGES - that doesn't actually
92403ba3782SJens Axboe 	 * dirty the inode itself
92503ba3782SJens Axboe 	 */
92603ba3782SJens Axboe 	if (flags & (I_DIRTY_SYNC | I_DIRTY_DATASYNC)) {
92703ba3782SJens Axboe 		if (sb->s_op->dirty_inode)
92803ba3782SJens Axboe 			sb->s_op->dirty_inode(inode);
92903ba3782SJens Axboe 	}
93003ba3782SJens Axboe 
93103ba3782SJens Axboe 	/*
93203ba3782SJens Axboe 	 * make sure that changes are seen by all cpus before we test i_state
93303ba3782SJens Axboe 	 * -- mikulas
93403ba3782SJens Axboe 	 */
93503ba3782SJens Axboe 	smp_mb();
93603ba3782SJens Axboe 
93703ba3782SJens Axboe 	/* avoid the locking if we can */
93803ba3782SJens Axboe 	if ((inode->i_state & flags) == flags)
93903ba3782SJens Axboe 		return;
94003ba3782SJens Axboe 
94103ba3782SJens Axboe 	if (unlikely(block_dump))
94203ba3782SJens Axboe 		block_dump___mark_inode_dirty(inode);
94303ba3782SJens Axboe 
94403ba3782SJens Axboe 	spin_lock(&inode_lock);
94503ba3782SJens Axboe 	if ((inode->i_state & flags) != flags) {
94603ba3782SJens Axboe 		const int was_dirty = inode->i_state & I_DIRTY;
94703ba3782SJens Axboe 
94803ba3782SJens Axboe 		inode->i_state |= flags;
94903ba3782SJens Axboe 
95003ba3782SJens Axboe 		/*
95103ba3782SJens Axboe 		 * If the inode is being synced, just update its dirty state.
95203ba3782SJens Axboe 		 * The unlocker will place the inode on the appropriate
95303ba3782SJens Axboe 		 * superblock list, based upon its state.
95403ba3782SJens Axboe 		 */
95503ba3782SJens Axboe 		if (inode->i_state & I_SYNC)
95603ba3782SJens Axboe 			goto out;
95703ba3782SJens Axboe 
95803ba3782SJens Axboe 		/*
95903ba3782SJens Axboe 		 * Only add valid (hashed) inodes to the superblock's
96003ba3782SJens Axboe 		 * dirty list.  Add blockdev inodes as well.
96103ba3782SJens Axboe 		 */
96203ba3782SJens Axboe 		if (!S_ISBLK(inode->i_mode)) {
9631d3382cbSAl Viro 			if (inode_unhashed(inode))
96403ba3782SJens Axboe 				goto out;
96503ba3782SJens Axboe 		}
966a4ffdde6SAl Viro 		if (inode->i_state & I_FREEING)
96703ba3782SJens Axboe 			goto out;
96803ba3782SJens Axboe 
96903ba3782SJens Axboe 		/*
97003ba3782SJens Axboe 		 * If the inode was already on b_dirty/b_io/b_more_io, don't
97103ba3782SJens Axboe 		 * reposition it (that would break b_dirty time-ordering).
97203ba3782SJens Axboe 		 */
97303ba3782SJens Axboe 		if (!was_dirty) {
974253c34e9SArtem Bityutskiy 			bdi = inode_to_bdi(inode);
975500b067cSJens Axboe 
976253c34e9SArtem Bityutskiy 			if (bdi_cap_writeback_dirty(bdi)) {
977253c34e9SArtem Bityutskiy 				WARN(!test_bit(BDI_registered, &bdi->state),
978253c34e9SArtem Bityutskiy 				     "bdi-%s not registered\n", bdi->name);
979253c34e9SArtem Bityutskiy 
980253c34e9SArtem Bityutskiy 				/*
981253c34e9SArtem Bityutskiy 				 * If this is the first dirty inode for this
982253c34e9SArtem Bityutskiy 				 * bdi, we have to wake-up the corresponding
983253c34e9SArtem Bityutskiy 				 * bdi thread to make sure background
984253c34e9SArtem Bityutskiy 				 * write-back happens later.
985253c34e9SArtem Bityutskiy 				 */
986253c34e9SArtem Bityutskiy 				if (!wb_has_dirty_io(&bdi->wb))
987253c34e9SArtem Bityutskiy 					wakeup_bdi = true;
988500b067cSJens Axboe 			}
98903ba3782SJens Axboe 
99003ba3782SJens Axboe 			inode->dirtied_when = jiffies;
9917ccf19a8SNick Piggin 			list_move(&inode->i_wb_list, &bdi->wb.b_dirty);
99203ba3782SJens Axboe 		}
99303ba3782SJens Axboe 	}
99403ba3782SJens Axboe out:
99503ba3782SJens Axboe 	spin_unlock(&inode_lock);
996253c34e9SArtem Bityutskiy 
997253c34e9SArtem Bityutskiy 	if (wakeup_bdi)
9986467716aSArtem Bityutskiy 		bdi_wakeup_thread_delayed(bdi);
99903ba3782SJens Axboe }
100003ba3782SJens Axboe EXPORT_SYMBOL(__mark_inode_dirty);
100103ba3782SJens Axboe 
100266f3b8e2SJens Axboe /*
100366f3b8e2SJens Axboe  * Write out a superblock's list of dirty inodes.  A wait will be performed
100466f3b8e2SJens Axboe  * upon no inodes, all inodes or the final one, depending upon sync_mode.
100566f3b8e2SJens Axboe  *
100666f3b8e2SJens Axboe  * If older_than_this is non-NULL, then only write out inodes which
100766f3b8e2SJens Axboe  * had their first dirtying at a time earlier than *older_than_this.
100866f3b8e2SJens Axboe  *
100966f3b8e2SJens Axboe  * If `bdi' is non-zero then we're being asked to writeback a specific queue.
101066f3b8e2SJens Axboe  * This function assumes that the blockdev superblock's inodes are backed by
101166f3b8e2SJens Axboe  * a variety of queues, so all inodes are searched.  For other superblocks,
101266f3b8e2SJens Axboe  * assume that all inodes are backed by the same queue.
101366f3b8e2SJens Axboe  *
101466f3b8e2SJens Axboe  * The inodes to be written are parked on bdi->b_io.  They are moved back onto
101566f3b8e2SJens Axboe  * bdi->b_dirty as they are selected for writing.  This way, none can be missed
101666f3b8e2SJens Axboe  * on the writer throttling path, and we get decent balancing between many
101766f3b8e2SJens Axboe  * throttled threads: we don't want them all piling up on inode_sync_wait.
101866f3b8e2SJens Axboe  */
1019b6e51316SJens Axboe static void wait_sb_inodes(struct super_block *sb)
102066f3b8e2SJens Axboe {
102138f21977SNick Piggin 	struct inode *inode, *old_inode = NULL;
102238f21977SNick Piggin 
102303ba3782SJens Axboe 	/*
102403ba3782SJens Axboe 	 * We need to be protected against the filesystem going from
102503ba3782SJens Axboe 	 * r/o to r/w or vice versa.
102603ba3782SJens Axboe 	 */
1027b6e51316SJens Axboe 	WARN_ON(!rwsem_is_locked(&sb->s_umount));
102803ba3782SJens Axboe 
102966f3b8e2SJens Axboe 	spin_lock(&inode_lock);
103066f3b8e2SJens Axboe 
103138f21977SNick Piggin 	/*
103238f21977SNick Piggin 	 * Data integrity sync. Must wait for all pages under writeback,
103338f21977SNick Piggin 	 * because there may have been pages dirtied before our sync
103438f21977SNick Piggin 	 * call, but which had writeout started before we write it out.
103538f21977SNick Piggin 	 * In which case, the inode may not be on the dirty list, but
103638f21977SNick Piggin 	 * we still have to wait for that writeout.
103738f21977SNick Piggin 	 */
1038b6e51316SJens Axboe 	list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
103938f21977SNick Piggin 		struct address_space *mapping;
104038f21977SNick Piggin 
1041a4ffdde6SAl Viro 		if (inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW))
104238f21977SNick Piggin 			continue;
104338f21977SNick Piggin 		mapping = inode->i_mapping;
104438f21977SNick Piggin 		if (mapping->nrpages == 0)
104538f21977SNick Piggin 			continue;
104638f21977SNick Piggin 		__iget(inode);
1047ae8547b0SHans Reiser 		spin_unlock(&inode_lock);
104838f21977SNick Piggin 		/*
104938f21977SNick Piggin 		 * We hold a reference to 'inode' so it couldn't have
105038f21977SNick Piggin 		 * been removed from s_inodes list while we dropped the
105138f21977SNick Piggin 		 * inode_lock.  We cannot iput the inode now as we can
105238f21977SNick Piggin 		 * be holding the last reference and we cannot iput it
105338f21977SNick Piggin 		 * under inode_lock. So we keep the reference and iput
105438f21977SNick Piggin 		 * it later.
105538f21977SNick Piggin 		 */
105638f21977SNick Piggin 		iput(old_inode);
105738f21977SNick Piggin 		old_inode = inode;
105838f21977SNick Piggin 
105938f21977SNick Piggin 		filemap_fdatawait(mapping);
106038f21977SNick Piggin 
106138f21977SNick Piggin 		cond_resched();
106238f21977SNick Piggin 
106338f21977SNick Piggin 		spin_lock(&inode_lock);
106438f21977SNick Piggin 	}
106538f21977SNick Piggin 	spin_unlock(&inode_lock);
106638f21977SNick Piggin 	iput(old_inode);
106766f3b8e2SJens Axboe }
10681da177e4SLinus Torvalds 
1069d8a8559cSJens Axboe /**
1070d8a8559cSJens Axboe  * writeback_inodes_sb	-	writeback dirty inodes from given super_block
1071d8a8559cSJens Axboe  * @sb: the superblock
10721da177e4SLinus Torvalds  *
1073d8a8559cSJens Axboe  * Start writeback on some inodes on this super_block. No guarantees are made
1074d8a8559cSJens Axboe  * on how many (if any) will be written, and this function does not wait
1075d8a8559cSJens Axboe  * for IO completion of submitted IO. The number of pages submitted is
1076d8a8559cSJens Axboe  * returned.
10771da177e4SLinus Torvalds  */
1078b6e51316SJens Axboe void writeback_inodes_sb(struct super_block *sb)
10791da177e4SLinus Torvalds {
10800e3c9a22SJens Axboe 	unsigned long nr_dirty = global_page_state(NR_FILE_DIRTY);
10810e3c9a22SJens Axboe 	unsigned long nr_unstable = global_page_state(NR_UNSTABLE_NFS);
108283ba7b07SChristoph Hellwig 	DECLARE_COMPLETION_ONSTACK(done);
108383ba7b07SChristoph Hellwig 	struct wb_writeback_work work = {
10843c4d7165SChristoph Hellwig 		.sb		= sb,
10853c4d7165SChristoph Hellwig 		.sync_mode	= WB_SYNC_NONE,
108683ba7b07SChristoph Hellwig 		.done		= &done,
10873c4d7165SChristoph Hellwig 	};
10880e3c9a22SJens Axboe 
1089cf37e972SChristoph Hellwig 	WARN_ON(!rwsem_is_locked(&sb->s_umount));
1090cf37e972SChristoph Hellwig 
1091cffbc8aaSDave Chinner 	work.nr_pages = nr_dirty + nr_unstable + get_nr_dirty_inodes();
10920e3c9a22SJens Axboe 
109383ba7b07SChristoph Hellwig 	bdi_queue_work(sb->s_bdi, &work);
109483ba7b07SChristoph Hellwig 	wait_for_completion(&done);
10951da177e4SLinus Torvalds }
1096d8a8559cSJens Axboe EXPORT_SYMBOL(writeback_inodes_sb);
1097d8a8559cSJens Axboe 
1098d8a8559cSJens Axboe /**
109917bd55d0SEric Sandeen  * writeback_inodes_sb_if_idle	-	start writeback if none underway
110017bd55d0SEric Sandeen  * @sb: the superblock
110117bd55d0SEric Sandeen  *
110217bd55d0SEric Sandeen  * Invoke writeback_inodes_sb if no writeback is currently underway.
110317bd55d0SEric Sandeen  * Returns 1 if writeback was started, 0 if not.
110417bd55d0SEric Sandeen  */
110517bd55d0SEric Sandeen int writeback_inodes_sb_if_idle(struct super_block *sb)
110617bd55d0SEric Sandeen {
110717bd55d0SEric Sandeen 	if (!writeback_in_progress(sb->s_bdi)) {
1108cf37e972SChristoph Hellwig 		down_read(&sb->s_umount);
110917bd55d0SEric Sandeen 		writeback_inodes_sb(sb);
1110cf37e972SChristoph Hellwig 		up_read(&sb->s_umount);
111117bd55d0SEric Sandeen 		return 1;
111217bd55d0SEric Sandeen 	} else
111317bd55d0SEric Sandeen 		return 0;
111417bd55d0SEric Sandeen }
111517bd55d0SEric Sandeen EXPORT_SYMBOL(writeback_inodes_sb_if_idle);
111617bd55d0SEric Sandeen 
111717bd55d0SEric Sandeen /**
1118d8a8559cSJens Axboe  * sync_inodes_sb	-	sync sb inode pages
1119d8a8559cSJens Axboe  * @sb: the superblock
1120d8a8559cSJens Axboe  *
1121d8a8559cSJens Axboe  * This function writes and waits on any dirty inode belonging to this
1122d8a8559cSJens Axboe  * super_block. The number of pages synced is returned.
1123d8a8559cSJens Axboe  */
1124b6e51316SJens Axboe void sync_inodes_sb(struct super_block *sb)
1125d8a8559cSJens Axboe {
112683ba7b07SChristoph Hellwig 	DECLARE_COMPLETION_ONSTACK(done);
112783ba7b07SChristoph Hellwig 	struct wb_writeback_work work = {
11283c4d7165SChristoph Hellwig 		.sb		= sb,
11293c4d7165SChristoph Hellwig 		.sync_mode	= WB_SYNC_ALL,
11303c4d7165SChristoph Hellwig 		.nr_pages	= LONG_MAX,
11313c4d7165SChristoph Hellwig 		.range_cyclic	= 0,
113283ba7b07SChristoph Hellwig 		.done		= &done,
11333c4d7165SChristoph Hellwig 	};
11343c4d7165SChristoph Hellwig 
1135cf37e972SChristoph Hellwig 	WARN_ON(!rwsem_is_locked(&sb->s_umount));
1136cf37e972SChristoph Hellwig 
113783ba7b07SChristoph Hellwig 	bdi_queue_work(sb->s_bdi, &work);
113883ba7b07SChristoph Hellwig 	wait_for_completion(&done);
113983ba7b07SChristoph Hellwig 
1140b6e51316SJens Axboe 	wait_sb_inodes(sb);
1141d8a8559cSJens Axboe }
1142d8a8559cSJens Axboe EXPORT_SYMBOL(sync_inodes_sb);
11431da177e4SLinus Torvalds 
11441da177e4SLinus Torvalds /**
11451da177e4SLinus Torvalds  * write_inode_now	-	write an inode to disk
11461da177e4SLinus Torvalds  * @inode: inode to write to disk
11471da177e4SLinus Torvalds  * @sync: whether the write should be synchronous or not
11481da177e4SLinus Torvalds  *
11497f04c26dSAndrea Arcangeli  * This function commits an inode to disk immediately if it is dirty. This is
11507f04c26dSAndrea Arcangeli  * primarily needed by knfsd.
11517f04c26dSAndrea Arcangeli  *
11527f04c26dSAndrea Arcangeli  * The caller must either have a ref on the inode or must have set I_WILL_FREE.
11531da177e4SLinus Torvalds  */
11541da177e4SLinus Torvalds int write_inode_now(struct inode *inode, int sync)
11551da177e4SLinus Torvalds {
11561da177e4SLinus Torvalds 	int ret;
11571da177e4SLinus Torvalds 	struct writeback_control wbc = {
11581da177e4SLinus Torvalds 		.nr_to_write = LONG_MAX,
115918914b18SMike Galbraith 		.sync_mode = sync ? WB_SYNC_ALL : WB_SYNC_NONE,
1160111ebb6eSOGAWA Hirofumi 		.range_start = 0,
1161111ebb6eSOGAWA Hirofumi 		.range_end = LLONG_MAX,
11621da177e4SLinus Torvalds 	};
11631da177e4SLinus Torvalds 
11641da177e4SLinus Torvalds 	if (!mapping_cap_writeback_dirty(inode->i_mapping))
116549364ce2SAndrew Morton 		wbc.nr_to_write = 0;
11661da177e4SLinus Torvalds 
11671da177e4SLinus Torvalds 	might_sleep();
11681da177e4SLinus Torvalds 	spin_lock(&inode_lock);
116901c03194SChristoph Hellwig 	ret = writeback_single_inode(inode, &wbc);
11701da177e4SLinus Torvalds 	spin_unlock(&inode_lock);
11711da177e4SLinus Torvalds 	if (sync)
11721c0eeaf5SJoern Engel 		inode_sync_wait(inode);
11731da177e4SLinus Torvalds 	return ret;
11741da177e4SLinus Torvalds }
11751da177e4SLinus Torvalds EXPORT_SYMBOL(write_inode_now);
11761da177e4SLinus Torvalds 
11771da177e4SLinus Torvalds /**
11781da177e4SLinus Torvalds  * sync_inode - write an inode and its pages to disk.
11791da177e4SLinus Torvalds  * @inode: the inode to sync
11801da177e4SLinus Torvalds  * @wbc: controls the writeback mode
11811da177e4SLinus Torvalds  *
11821da177e4SLinus Torvalds  * sync_inode() will write an inode and its pages to disk.  It will also
11831da177e4SLinus Torvalds  * correctly update the inode on its superblock's dirty inode lists and will
11841da177e4SLinus Torvalds  * update inode->i_state.
11851da177e4SLinus Torvalds  *
11861da177e4SLinus Torvalds  * The caller must have a ref on the inode.
11871da177e4SLinus Torvalds  */
11881da177e4SLinus Torvalds int sync_inode(struct inode *inode, struct writeback_control *wbc)
11891da177e4SLinus Torvalds {
11901da177e4SLinus Torvalds 	int ret;
11911da177e4SLinus Torvalds 
11921da177e4SLinus Torvalds 	spin_lock(&inode_lock);
119301c03194SChristoph Hellwig 	ret = writeback_single_inode(inode, wbc);
11941da177e4SLinus Torvalds 	spin_unlock(&inode_lock);
11951da177e4SLinus Torvalds 	return ret;
11961da177e4SLinus Torvalds }
11971da177e4SLinus Torvalds EXPORT_SYMBOL(sync_inode);
1198c3765016SChristoph Hellwig 
1199c3765016SChristoph Hellwig /**
1200c3765016SChristoph Hellwig  * sync_inode - write an inode to disk
1201c3765016SChristoph Hellwig  * @inode: the inode to sync
1202c3765016SChristoph Hellwig  * @wait: wait for I/O to complete.
1203c3765016SChristoph Hellwig  *
1204c3765016SChristoph Hellwig  * Write an inode to disk and adjust it's dirty state after completion.
1205c3765016SChristoph Hellwig  *
1206c3765016SChristoph Hellwig  * Note: only writes the actual inode, no associated data or other metadata.
1207c3765016SChristoph Hellwig  */
1208c3765016SChristoph Hellwig int sync_inode_metadata(struct inode *inode, int wait)
1209c3765016SChristoph Hellwig {
1210c3765016SChristoph Hellwig 	struct writeback_control wbc = {
1211c3765016SChristoph Hellwig 		.sync_mode = wait ? WB_SYNC_ALL : WB_SYNC_NONE,
1212c3765016SChristoph Hellwig 		.nr_to_write = 0, /* metadata-only */
1213c3765016SChristoph Hellwig 	};
1214c3765016SChristoph Hellwig 
1215c3765016SChristoph Hellwig 	return sync_inode(inode, &wbc);
1216c3765016SChristoph Hellwig }
1217c3765016SChristoph Hellwig EXPORT_SYMBOL(sync_inode_metadata);
1218