xref: /openbmc/linux/fs/fs-writeback.c (revision a72bfd4d)
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>
191da177e4SLinus Torvalds #include <linux/sched.h>
201da177e4SLinus Torvalds #include <linux/fs.h>
211da177e4SLinus Torvalds #include <linux/mm.h>
2203ba3782SJens Axboe #include <linux/kthread.h>
2303ba3782SJens Axboe #include <linux/freezer.h>
241da177e4SLinus Torvalds #include <linux/writeback.h>
251da177e4SLinus Torvalds #include <linux/blkdev.h>
261da177e4SLinus Torvalds #include <linux/backing-dev.h>
271da177e4SLinus Torvalds #include <linux/buffer_head.h>
2807f3f05cSDavid Howells #include "internal.h"
291da177e4SLinus Torvalds 
3066f3b8e2SJens Axboe #define inode_to_bdi(inode)	((inode)->i_mapping->backing_dev_info)
31f11b00f3SAdrian Bunk 
3203ba3782SJens Axboe /*
33d0bceac7SJens Axboe  * We don't actually have pdflush, but this one is exported though /proc...
34d0bceac7SJens Axboe  */
35d0bceac7SJens Axboe int nr_pdflush_threads;
36d0bceac7SJens Axboe 
37d0bceac7SJens Axboe /*
38c4a77a6cSJens Axboe  * Passed into wb_writeback(), essentially a subset of writeback_control
39c4a77a6cSJens Axboe  */
40c4a77a6cSJens Axboe struct wb_writeback_args {
41c4a77a6cSJens Axboe 	long nr_pages;
42c4a77a6cSJens Axboe 	struct super_block *sb;
43c4a77a6cSJens Axboe 	enum writeback_sync_modes sync_mode;
44d3ddec76SWu Fengguang 	int for_kupdate:1;
45d3ddec76SWu Fengguang 	int range_cyclic:1;
46d3ddec76SWu Fengguang 	int for_background:1;
47c4a77a6cSJens Axboe };
48c4a77a6cSJens Axboe 
49c4a77a6cSJens Axboe /*
5003ba3782SJens Axboe  * Work items for the bdi_writeback threads
51f11b00f3SAdrian Bunk  */
5203ba3782SJens Axboe struct bdi_work {
538010c3b6SJens Axboe 	struct list_head list;		/* pending work list */
548010c3b6SJens Axboe 	struct rcu_head rcu_head;	/* for RCU free/clear of work */
5503ba3782SJens Axboe 
568010c3b6SJens Axboe 	unsigned long seen;		/* threads that have seen this work */
578010c3b6SJens Axboe 	atomic_t pending;		/* number of threads still to do work */
5803ba3782SJens Axboe 
598010c3b6SJens Axboe 	struct wb_writeback_args args;	/* writeback arguments */
6003ba3782SJens Axboe 
618010c3b6SJens Axboe 	unsigned long state;		/* flag bits, see WS_* */
6203ba3782SJens Axboe };
6303ba3782SJens Axboe 
6403ba3782SJens Axboe enum {
6503ba3782SJens Axboe 	WS_USED_B = 0,
6603ba3782SJens Axboe 	WS_ONSTACK_B,
6703ba3782SJens Axboe };
6803ba3782SJens Axboe 
6903ba3782SJens Axboe #define WS_USED (1 << WS_USED_B)
7003ba3782SJens Axboe #define WS_ONSTACK (1 << WS_ONSTACK_B)
7103ba3782SJens Axboe 
7203ba3782SJens Axboe static inline bool bdi_work_on_stack(struct bdi_work *work)
73f11b00f3SAdrian Bunk {
7403ba3782SJens Axboe 	return test_bit(WS_ONSTACK_B, &work->state);
7503ba3782SJens Axboe }
7603ba3782SJens Axboe 
7703ba3782SJens Axboe static inline void bdi_work_init(struct bdi_work *work,
78b6e51316SJens Axboe 				 struct wb_writeback_args *args)
7903ba3782SJens Axboe {
8003ba3782SJens Axboe 	INIT_RCU_HEAD(&work->rcu_head);
81b6e51316SJens Axboe 	work->args = *args;
8203ba3782SJens Axboe 	work->state = WS_USED;
8303ba3782SJens Axboe }
8403ba3782SJens Axboe 
85f11b00f3SAdrian Bunk /**
86f11b00f3SAdrian Bunk  * writeback_in_progress - determine whether there is writeback in progress
87f11b00f3SAdrian Bunk  * @bdi: the device's backing_dev_info structure.
88f11b00f3SAdrian Bunk  *
8903ba3782SJens Axboe  * Determine whether there is writeback waiting to be handled against a
9003ba3782SJens Axboe  * backing device.
91f11b00f3SAdrian Bunk  */
92f11b00f3SAdrian Bunk int writeback_in_progress(struct backing_dev_info *bdi)
93f11b00f3SAdrian Bunk {
9403ba3782SJens Axboe 	return !list_empty(&bdi->work_list);
95f11b00f3SAdrian Bunk }
96f11b00f3SAdrian Bunk 
9703ba3782SJens Axboe static void bdi_work_clear(struct bdi_work *work)
98f11b00f3SAdrian Bunk {
9903ba3782SJens Axboe 	clear_bit(WS_USED_B, &work->state);
10003ba3782SJens Axboe 	smp_mb__after_clear_bit();
1011ef7d9aaSNick Piggin 	/*
1021ef7d9aaSNick Piggin 	 * work can have disappeared at this point. bit waitq functions
1031ef7d9aaSNick Piggin 	 * should be able to tolerate this, provided bdi_sched_wait does
1041ef7d9aaSNick Piggin 	 * not dereference it's pointer argument.
1051ef7d9aaSNick Piggin 	*/
10603ba3782SJens Axboe 	wake_up_bit(&work->state, WS_USED_B);
107f11b00f3SAdrian Bunk }
108f11b00f3SAdrian Bunk 
10903ba3782SJens Axboe static void bdi_work_free(struct rcu_head *head)
1104195f73dSNick Piggin {
11103ba3782SJens Axboe 	struct bdi_work *work = container_of(head, struct bdi_work, rcu_head);
1124195f73dSNick Piggin 
11303ba3782SJens Axboe 	if (!bdi_work_on_stack(work))
11403ba3782SJens Axboe 		kfree(work);
11503ba3782SJens Axboe 	else
11603ba3782SJens Axboe 		bdi_work_clear(work);
1174195f73dSNick Piggin }
1184195f73dSNick Piggin 
11903ba3782SJens Axboe static void wb_work_complete(struct bdi_work *work)
1201da177e4SLinus Torvalds {
121c4a77a6cSJens Axboe 	const enum writeback_sync_modes sync_mode = work->args.sync_mode;
12277b9d059SNick Piggin 	int onstack = bdi_work_on_stack(work);
1231da177e4SLinus Torvalds 
1241da177e4SLinus Torvalds 	/*
12503ba3782SJens Axboe 	 * For allocated work, we can clear the done/seen bit right here.
12603ba3782SJens Axboe 	 * For on-stack work, we need to postpone both the clear and free
12703ba3782SJens Axboe 	 * to after the RCU grace period, since the stack could be invalidated
12803ba3782SJens Axboe 	 * as soon as bdi_work_clear() has done the wakeup.
1291da177e4SLinus Torvalds 	 */
13077b9d059SNick Piggin 	if (!onstack)
13103ba3782SJens Axboe 		bdi_work_clear(work);
13277b9d059SNick Piggin 	if (sync_mode == WB_SYNC_NONE || onstack)
13303ba3782SJens Axboe 		call_rcu(&work->rcu_head, bdi_work_free);
1341da177e4SLinus Torvalds }
1351da177e4SLinus Torvalds 
13603ba3782SJens Axboe static void wb_clear_pending(struct bdi_writeback *wb, struct bdi_work *work)
13703ba3782SJens Axboe {
1381da177e4SLinus Torvalds 	/*
13903ba3782SJens Axboe 	 * The caller has retrieved the work arguments from this work,
14003ba3782SJens Axboe 	 * drop our reference. If this is the last ref, delete and free it
14103ba3782SJens Axboe 	 */
14203ba3782SJens Axboe 	if (atomic_dec_and_test(&work->pending)) {
14303ba3782SJens Axboe 		struct backing_dev_info *bdi = wb->bdi;
14403ba3782SJens Axboe 
14503ba3782SJens Axboe 		spin_lock(&bdi->wb_lock);
14603ba3782SJens Axboe 		list_del_rcu(&work->list);
14703ba3782SJens Axboe 		spin_unlock(&bdi->wb_lock);
14803ba3782SJens Axboe 
14903ba3782SJens Axboe 		wb_work_complete(work);
15003ba3782SJens Axboe 	}
15103ba3782SJens Axboe }
15203ba3782SJens Axboe 
15303ba3782SJens Axboe static void bdi_queue_work(struct backing_dev_info *bdi, struct bdi_work *work)
15403ba3782SJens Axboe {
15503ba3782SJens Axboe 	work->seen = bdi->wb_mask;
15603ba3782SJens Axboe 	BUG_ON(!work->seen);
15703ba3782SJens Axboe 	atomic_set(&work->pending, bdi->wb_cnt);
15803ba3782SJens Axboe 	BUG_ON(!bdi->wb_cnt);
15903ba3782SJens Axboe 
16003ba3782SJens Axboe 	/*
161deed62edSNick Piggin 	 * list_add_tail_rcu() contains the necessary barriers to
162deed62edSNick Piggin 	 * make sure the above stores are seen before the item is
163deed62edSNick Piggin 	 * noticed on the list
1641da177e4SLinus Torvalds 	 */
16503ba3782SJens Axboe 	spin_lock(&bdi->wb_lock);
16603ba3782SJens Axboe 	list_add_tail_rcu(&work->list, &bdi->work_list);
16703ba3782SJens Axboe 	spin_unlock(&bdi->wb_lock);
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds 	/*
17003ba3782SJens Axboe 	 * If the default thread isn't there, make sure we add it. When
17103ba3782SJens Axboe 	 * it gets created and wakes up, we'll run this work.
1721da177e4SLinus Torvalds 	 */
17303ba3782SJens Axboe 	if (unlikely(list_empty_careful(&bdi->wb_list)))
17403ba3782SJens Axboe 		wake_up_process(default_backing_dev_info.wb.task);
17503ba3782SJens Axboe 	else {
17603ba3782SJens Axboe 		struct bdi_writeback *wb = &bdi->wb;
1771da177e4SLinus Torvalds 
1781ef7d9aaSNick Piggin 		if (wb->task)
17903ba3782SJens Axboe 			wake_up_process(wb->task);
1801da177e4SLinus Torvalds 	}
18103ba3782SJens Axboe }
1821da177e4SLinus Torvalds 
1831da177e4SLinus Torvalds /*
18403ba3782SJens Axboe  * Used for on-stack allocated work items. The caller needs to wait until
18503ba3782SJens Axboe  * the wb threads have acked the work before it's safe to continue.
1861da177e4SLinus Torvalds  */
18703ba3782SJens Axboe static void bdi_wait_on_work_clear(struct bdi_work *work)
1881da177e4SLinus Torvalds {
18903ba3782SJens Axboe 	wait_on_bit(&work->state, WS_USED_B, bdi_sched_wait,
19003ba3782SJens Axboe 		    TASK_UNINTERRUPTIBLE);
19103ba3782SJens Axboe }
19203ba3782SJens Axboe 
193f11fcae8SJens Axboe static void bdi_alloc_queue_work(struct backing_dev_info *bdi,
194b6e51316SJens Axboe 				 struct wb_writeback_args *args)
19503ba3782SJens Axboe {
19603ba3782SJens Axboe 	struct bdi_work *work;
19703ba3782SJens Axboe 
198bcddc3f0SJens Axboe 	/*
199bcddc3f0SJens Axboe 	 * This is WB_SYNC_NONE writeback, so if allocation fails just
200bcddc3f0SJens Axboe 	 * wakeup the thread for old dirty data writeback
201bcddc3f0SJens Axboe 	 */
20203ba3782SJens Axboe 	work = kmalloc(sizeof(*work), GFP_ATOMIC);
203bcddc3f0SJens Axboe 	if (work) {
204b6e51316SJens Axboe 		bdi_work_init(work, args);
205f11fcae8SJens Axboe 		bdi_queue_work(bdi, work);
206bcddc3f0SJens Axboe 	} else {
207bcddc3f0SJens Axboe 		struct bdi_writeback *wb = &bdi->wb;
208bcddc3f0SJens Axboe 
209bcddc3f0SJens Axboe 		if (wb->task)
210bcddc3f0SJens Axboe 			wake_up_process(wb->task);
211bcddc3f0SJens Axboe 	}
21203ba3782SJens Axboe }
21303ba3782SJens Axboe 
214b6e51316SJens Axboe /**
215b6e51316SJens Axboe  * bdi_sync_writeback - start and wait for writeback
216b6e51316SJens Axboe  * @bdi: the backing device to write from
217b6e51316SJens Axboe  * @sb: write inodes from this super_block
218b6e51316SJens Axboe  *
219b6e51316SJens Axboe  * Description:
220b6e51316SJens Axboe  *   This does WB_SYNC_ALL data integrity writeback and waits for the
221b6e51316SJens Axboe  *   IO to complete. Callers must hold the sb s_umount semaphore for
222b6e51316SJens Axboe  *   reading, to avoid having the super disappear before we are done.
22303ba3782SJens Axboe  */
224b6e51316SJens Axboe static void bdi_sync_writeback(struct backing_dev_info *bdi,
225b6e51316SJens Axboe 			       struct super_block *sb)
226b6e51316SJens Axboe {
227b6e51316SJens Axboe 	struct wb_writeback_args args = {
228b6e51316SJens Axboe 		.sb		= sb,
229b6e51316SJens Axboe 		.sync_mode	= WB_SYNC_ALL,
230b6e51316SJens Axboe 		.nr_pages	= LONG_MAX,
231b6e51316SJens Axboe 		.range_cyclic	= 0,
232b6e51316SJens Axboe 	};
233f0fad8a5SChristoph Hellwig 	struct bdi_work work;
234f0fad8a5SChristoph Hellwig 
235b6e51316SJens Axboe 	bdi_work_init(&work, &args);
236f0fad8a5SChristoph Hellwig 	work.state |= WS_ONSTACK;
237f0fad8a5SChristoph Hellwig 
238b6e51316SJens Axboe 	bdi_queue_work(bdi, &work);
239f0fad8a5SChristoph Hellwig 	bdi_wait_on_work_clear(&work);
24003ba3782SJens Axboe }
241b6e51316SJens Axboe 
242b6e51316SJens Axboe /**
243b6e51316SJens Axboe  * bdi_start_writeback - start writeback
244b6e51316SJens Axboe  * @bdi: the backing device to write from
245b6e51316SJens Axboe  * @nr_pages: the number of pages to write
246b6e51316SJens Axboe  *
247b6e51316SJens Axboe  * Description:
248b6e51316SJens Axboe  *   This does WB_SYNC_NONE opportunistic writeback. The IO is only
249b6e51316SJens Axboe  *   started when this function returns, we make no guarentees on
250b6e51316SJens Axboe  *   completion. Caller need not hold sb s_umount semaphore.
251b6e51316SJens Axboe  *
252b6e51316SJens Axboe  */
253a72bfd4dSJens Axboe void bdi_start_writeback(struct backing_dev_info *bdi, struct super_block *sb,
254a72bfd4dSJens Axboe 			 long nr_pages)
255b6e51316SJens Axboe {
256b6e51316SJens Axboe 	struct wb_writeback_args args = {
257a72bfd4dSJens Axboe 		.sb		= sb,
258b6e51316SJens Axboe 		.sync_mode	= WB_SYNC_NONE,
259b6e51316SJens Axboe 		.nr_pages	= nr_pages,
260b6e51316SJens Axboe 		.range_cyclic	= 1,
261b6e51316SJens Axboe 	};
262b6e51316SJens Axboe 
263d3ddec76SWu Fengguang 	/*
264d3ddec76SWu Fengguang 	 * We treat @nr_pages=0 as the special case to do background writeback,
265d3ddec76SWu Fengguang 	 * ie. to sync pages until the background dirty threshold is reached.
266d3ddec76SWu Fengguang 	 */
267d3ddec76SWu Fengguang 	if (!nr_pages) {
268d3ddec76SWu Fengguang 		args.nr_pages = LONG_MAX;
269d3ddec76SWu Fengguang 		args.for_background = 1;
270d3ddec76SWu Fengguang 	}
271d3ddec76SWu Fengguang 
272b6e51316SJens Axboe 	bdi_alloc_queue_work(bdi, &args);
2731da177e4SLinus Torvalds }
2741da177e4SLinus Torvalds 
2751da177e4SLinus Torvalds /*
2766610a0bcSAndrew Morton  * Redirty an inode: set its when-it-was dirtied timestamp and move it to the
2776610a0bcSAndrew Morton  * furthest end of its superblock's dirty-inode list.
2786610a0bcSAndrew Morton  *
2796610a0bcSAndrew Morton  * Before stamping the inode's ->dirtied_when, we check to see whether it is
28066f3b8e2SJens Axboe  * already the most-recently-dirtied inode on the b_dirty list.  If that is
2816610a0bcSAndrew Morton  * the case then the inode must have been redirtied while it was being written
2826610a0bcSAndrew Morton  * out and we don't reset its dirtied_when.
2836610a0bcSAndrew Morton  */
2846610a0bcSAndrew Morton static void redirty_tail(struct inode *inode)
2856610a0bcSAndrew Morton {
28603ba3782SJens Axboe 	struct bdi_writeback *wb = &inode_to_bdi(inode)->wb;
2876610a0bcSAndrew Morton 
28803ba3782SJens Axboe 	if (!list_empty(&wb->b_dirty)) {
28966f3b8e2SJens Axboe 		struct inode *tail;
2906610a0bcSAndrew Morton 
29103ba3782SJens Axboe 		tail = list_entry(wb->b_dirty.next, struct inode, i_list);
29266f3b8e2SJens Axboe 		if (time_before(inode->dirtied_when, tail->dirtied_when))
2936610a0bcSAndrew Morton 			inode->dirtied_when = jiffies;
2946610a0bcSAndrew Morton 	}
29503ba3782SJens Axboe 	list_move(&inode->i_list, &wb->b_dirty);
2966610a0bcSAndrew Morton }
2976610a0bcSAndrew Morton 
2986610a0bcSAndrew Morton /*
29966f3b8e2SJens Axboe  * requeue inode for re-scanning after bdi->b_io list is exhausted.
300c986d1e2SAndrew Morton  */
3010e0f4fc2SKen Chen static void requeue_io(struct inode *inode)
302c986d1e2SAndrew Morton {
30303ba3782SJens Axboe 	struct bdi_writeback *wb = &inode_to_bdi(inode)->wb;
30403ba3782SJens Axboe 
30503ba3782SJens Axboe 	list_move(&inode->i_list, &wb->b_more_io);
306c986d1e2SAndrew Morton }
307c986d1e2SAndrew Morton 
3081c0eeaf5SJoern Engel static void inode_sync_complete(struct inode *inode)
3091c0eeaf5SJoern Engel {
3101c0eeaf5SJoern Engel 	/*
3111c0eeaf5SJoern Engel 	 * Prevent speculative execution through spin_unlock(&inode_lock);
3121c0eeaf5SJoern Engel 	 */
3131c0eeaf5SJoern Engel 	smp_mb();
3141c0eeaf5SJoern Engel 	wake_up_bit(&inode->i_state, __I_SYNC);
3151c0eeaf5SJoern Engel }
3161c0eeaf5SJoern Engel 
317d2caa3c5SJeff Layton static bool inode_dirtied_after(struct inode *inode, unsigned long t)
318d2caa3c5SJeff Layton {
319d2caa3c5SJeff Layton 	bool ret = time_after(inode->dirtied_when, t);
320d2caa3c5SJeff Layton #ifndef CONFIG_64BIT
321d2caa3c5SJeff Layton 	/*
322d2caa3c5SJeff Layton 	 * For inodes being constantly redirtied, dirtied_when can get stuck.
323d2caa3c5SJeff Layton 	 * It _appears_ to be in the future, but is actually in distant past.
324d2caa3c5SJeff Layton 	 * This test is necessary to prevent such wrapped-around relative times
3255b0830cbSJens Axboe 	 * from permanently stopping the whole bdi writeback.
326d2caa3c5SJeff Layton 	 */
327d2caa3c5SJeff Layton 	ret = ret && time_before_eq(inode->dirtied_when, jiffies);
328d2caa3c5SJeff Layton #endif
329d2caa3c5SJeff Layton 	return ret;
330d2caa3c5SJeff Layton }
331d2caa3c5SJeff Layton 
332c986d1e2SAndrew Morton /*
3332c136579SFengguang Wu  * Move expired dirty inodes from @delaying_queue to @dispatch_queue.
3342c136579SFengguang Wu  */
3352c136579SFengguang Wu static void move_expired_inodes(struct list_head *delaying_queue,
3362c136579SFengguang Wu 			       struct list_head *dispatch_queue,
3372c136579SFengguang Wu 				unsigned long *older_than_this)
3382c136579SFengguang Wu {
3395c03449dSShaohua Li 	LIST_HEAD(tmp);
3405c03449dSShaohua Li 	struct list_head *pos, *node;
341cf137307SJens Axboe 	struct super_block *sb = NULL;
3425c03449dSShaohua Li 	struct inode *inode;
343cf137307SJens Axboe 	int do_sb_sort = 0;
3445c03449dSShaohua Li 
3452c136579SFengguang Wu 	while (!list_empty(delaying_queue)) {
3465c03449dSShaohua Li 		inode = list_entry(delaying_queue->prev, struct inode, i_list);
3472c136579SFengguang Wu 		if (older_than_this &&
348d2caa3c5SJeff Layton 		    inode_dirtied_after(inode, *older_than_this))
3492c136579SFengguang Wu 			break;
350cf137307SJens Axboe 		if (sb && sb != inode->i_sb)
351cf137307SJens Axboe 			do_sb_sort = 1;
352cf137307SJens Axboe 		sb = inode->i_sb;
3535c03449dSShaohua Li 		list_move(&inode->i_list, &tmp);
3545c03449dSShaohua Li 	}
3555c03449dSShaohua Li 
356cf137307SJens Axboe 	/* just one sb in list, splice to dispatch_queue and we're done */
357cf137307SJens Axboe 	if (!do_sb_sort) {
358cf137307SJens Axboe 		list_splice(&tmp, dispatch_queue);
359cf137307SJens Axboe 		return;
360cf137307SJens Axboe 	}
361cf137307SJens Axboe 
3625c03449dSShaohua Li 	/* Move inodes from one superblock together */
3635c03449dSShaohua Li 	while (!list_empty(&tmp)) {
3645c03449dSShaohua Li 		inode = list_entry(tmp.prev, struct inode, i_list);
3655c03449dSShaohua Li 		sb = inode->i_sb;
3665c03449dSShaohua Li 		list_for_each_prev_safe(pos, node, &tmp) {
3675c03449dSShaohua Li 			inode = list_entry(pos, struct inode, i_list);
3685c03449dSShaohua Li 			if (inode->i_sb == sb)
3692c136579SFengguang Wu 				list_move(&inode->i_list, dispatch_queue);
3702c136579SFengguang Wu 		}
3712c136579SFengguang Wu 	}
3725c03449dSShaohua Li }
3732c136579SFengguang Wu 
3742c136579SFengguang Wu /*
3752c136579SFengguang Wu  * Queue all expired dirty inodes for io, eldest first.
3762c136579SFengguang Wu  */
37703ba3782SJens Axboe static void queue_io(struct bdi_writeback *wb, unsigned long *older_than_this)
3782c136579SFengguang Wu {
37903ba3782SJens Axboe 	list_splice_init(&wb->b_more_io, wb->b_io.prev);
38003ba3782SJens Axboe 	move_expired_inodes(&wb->b_dirty, &wb->b_io, older_than_this);
38166f3b8e2SJens Axboe }
38266f3b8e2SJens Axboe 
38303ba3782SJens Axboe static int write_inode(struct inode *inode, int sync)
38466f3b8e2SJens Axboe {
38503ba3782SJens Axboe 	if (inode->i_sb->s_op->write_inode && !is_bad_inode(inode))
38603ba3782SJens Axboe 		return inode->i_sb->s_op->write_inode(inode, sync);
38703ba3782SJens Axboe 	return 0;
38866f3b8e2SJens Axboe }
38908d8e974SFengguang Wu 
3902c136579SFengguang Wu /*
39101c03194SChristoph Hellwig  * Wait for writeback on an inode to complete.
39201c03194SChristoph Hellwig  */
39301c03194SChristoph Hellwig static void inode_wait_for_writeback(struct inode *inode)
39401c03194SChristoph Hellwig {
39501c03194SChristoph Hellwig 	DEFINE_WAIT_BIT(wq, &inode->i_state, __I_SYNC);
39601c03194SChristoph Hellwig 	wait_queue_head_t *wqh;
39701c03194SChristoph Hellwig 
39801c03194SChristoph Hellwig 	wqh = bit_waitqueue(&inode->i_state, __I_SYNC);
39901c03194SChristoph Hellwig 	do {
40001c03194SChristoph Hellwig 		spin_unlock(&inode_lock);
40101c03194SChristoph Hellwig 		__wait_on_bit(wqh, &wq, inode_wait, TASK_UNINTERRUPTIBLE);
40201c03194SChristoph Hellwig 		spin_lock(&inode_lock);
40301c03194SChristoph Hellwig 	} while (inode->i_state & I_SYNC);
40401c03194SChristoph Hellwig }
40501c03194SChristoph Hellwig 
40601c03194SChristoph Hellwig /*
40701c03194SChristoph Hellwig  * Write out an inode's dirty pages.  Called under inode_lock.  Either the
40801c03194SChristoph Hellwig  * caller has ref on the inode (either via __iget or via syscall against an fd)
40901c03194SChristoph Hellwig  * or the inode has I_WILL_FREE set (via generic_forget_inode)
41001c03194SChristoph Hellwig  *
4111da177e4SLinus Torvalds  * If `wait' is set, wait on the writeout.
4121da177e4SLinus Torvalds  *
4131da177e4SLinus Torvalds  * The whole writeout design is quite complex and fragile.  We want to avoid
4141da177e4SLinus Torvalds  * starvation of particular inodes when others are being redirtied, prevent
4151da177e4SLinus Torvalds  * livelocks, etc.
4161da177e4SLinus Torvalds  *
4171da177e4SLinus Torvalds  * Called under inode_lock.
4181da177e4SLinus Torvalds  */
4191da177e4SLinus Torvalds static int
42001c03194SChristoph Hellwig writeback_single_inode(struct inode *inode, struct writeback_control *wbc)
4211da177e4SLinus Torvalds {
4221da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
4231da177e4SLinus Torvalds 	int wait = wbc->sync_mode == WB_SYNC_ALL;
42401c03194SChristoph Hellwig 	unsigned dirty;
4251da177e4SLinus Torvalds 	int ret;
4261da177e4SLinus Torvalds 
42701c03194SChristoph Hellwig 	if (!atomic_read(&inode->i_count))
42801c03194SChristoph Hellwig 		WARN_ON(!(inode->i_state & (I_WILL_FREE|I_FREEING)));
42901c03194SChristoph Hellwig 	else
43001c03194SChristoph Hellwig 		WARN_ON(inode->i_state & I_WILL_FREE);
43101c03194SChristoph Hellwig 
43201c03194SChristoph Hellwig 	if (inode->i_state & I_SYNC) {
43301c03194SChristoph Hellwig 		/*
43401c03194SChristoph Hellwig 		 * If this inode is locked for writeback and we are not doing
43566f3b8e2SJens Axboe 		 * writeback-for-data-integrity, move it to b_more_io so that
43601c03194SChristoph Hellwig 		 * writeback can proceed with the other inodes on s_io.
43701c03194SChristoph Hellwig 		 *
43801c03194SChristoph Hellwig 		 * We'll have another go at writing back this inode when we
43966f3b8e2SJens Axboe 		 * completed a full scan of b_io.
44001c03194SChristoph Hellwig 		 */
44101c03194SChristoph Hellwig 		if (!wait) {
44201c03194SChristoph Hellwig 			requeue_io(inode);
44301c03194SChristoph Hellwig 			return 0;
44401c03194SChristoph Hellwig 		}
44501c03194SChristoph Hellwig 
44601c03194SChristoph Hellwig 		/*
44701c03194SChristoph Hellwig 		 * It's a data-integrity sync.  We must wait.
44801c03194SChristoph Hellwig 		 */
44901c03194SChristoph Hellwig 		inode_wait_for_writeback(inode);
45001c03194SChristoph Hellwig 	}
45101c03194SChristoph Hellwig 
4521c0eeaf5SJoern Engel 	BUG_ON(inode->i_state & I_SYNC);
4531da177e4SLinus Torvalds 
4541c0eeaf5SJoern Engel 	/* Set I_SYNC, reset I_DIRTY */
4551da177e4SLinus Torvalds 	dirty = inode->i_state & I_DIRTY;
4561c0eeaf5SJoern Engel 	inode->i_state |= I_SYNC;
4571da177e4SLinus Torvalds 	inode->i_state &= ~I_DIRTY;
4581da177e4SLinus Torvalds 
4591da177e4SLinus Torvalds 	spin_unlock(&inode_lock);
4601da177e4SLinus Torvalds 
4611da177e4SLinus Torvalds 	ret = do_writepages(mapping, wbc);
4621da177e4SLinus Torvalds 
4631da177e4SLinus Torvalds 	/* Don't write the inode if only I_DIRTY_PAGES was set */
4641da177e4SLinus Torvalds 	if (dirty & (I_DIRTY_SYNC | I_DIRTY_DATASYNC)) {
4651da177e4SLinus Torvalds 		int err = write_inode(inode, wait);
4661da177e4SLinus Torvalds 		if (ret == 0)
4671da177e4SLinus Torvalds 			ret = err;
4681da177e4SLinus Torvalds 	}
4691da177e4SLinus Torvalds 
4701da177e4SLinus Torvalds 	if (wait) {
4711da177e4SLinus Torvalds 		int err = filemap_fdatawait(mapping);
4721da177e4SLinus Torvalds 		if (ret == 0)
4731da177e4SLinus Torvalds 			ret = err;
4741da177e4SLinus Torvalds 	}
4751da177e4SLinus Torvalds 
4761da177e4SLinus Torvalds 	spin_lock(&inode_lock);
4771c0eeaf5SJoern Engel 	inode->i_state &= ~I_SYNC;
47884a89245SWu Fengguang 	if (!(inode->i_state & (I_FREEING | I_CLEAR))) {
479b3af9468SWu Fengguang 		if ((inode->i_state & I_DIRTY_PAGES) && wbc->for_kupdate) {
480ae1b7f7dSWu Fengguang 			/*
481b3af9468SWu Fengguang 			 * More pages get dirtied by a fast dirtier.
482b3af9468SWu Fengguang 			 */
483b3af9468SWu Fengguang 			goto select_queue;
484b3af9468SWu Fengguang 		} else if (inode->i_state & I_DIRTY) {
485b3af9468SWu Fengguang 			/*
486b3af9468SWu Fengguang 			 * At least XFS will redirty the inode during the
487b3af9468SWu Fengguang 			 * writeback (delalloc) and on io completion (isize).
488ae1b7f7dSWu Fengguang 			 */
489ae1b7f7dSWu Fengguang 			redirty_tail(inode);
490ae1b7f7dSWu Fengguang 		} else if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) {
4911da177e4SLinus Torvalds 			/*
4921da177e4SLinus Torvalds 			 * We didn't write back all the pages.  nfs_writepages()
4931da177e4SLinus Torvalds 			 * sometimes bales out without doing anything. Redirty
49466f3b8e2SJens Axboe 			 * the inode; Move it from b_io onto b_more_io/b_dirty.
4951b43ef91SAndrew Morton 			 */
4961b43ef91SAndrew Morton 			/*
4971b43ef91SAndrew Morton 			 * akpm: if the caller was the kupdate function we put
49866f3b8e2SJens Axboe 			 * this inode at the head of b_dirty so it gets first
4991b43ef91SAndrew Morton 			 * consideration.  Otherwise, move it to the tail, for
5001b43ef91SAndrew Morton 			 * the reasons described there.  I'm not really sure
5011b43ef91SAndrew Morton 			 * how much sense this makes.  Presumably I had a good
5021b43ef91SAndrew Morton 			 * reasons for doing it this way, and I'd rather not
5031b43ef91SAndrew Morton 			 * muck with it at present.
5041da177e4SLinus Torvalds 			 */
5051da177e4SLinus Torvalds 			if (wbc->for_kupdate) {
5061da177e4SLinus Torvalds 				/*
5072c136579SFengguang Wu 				 * For the kupdate function we move the inode
50866f3b8e2SJens Axboe 				 * to b_more_io so it will get more writeout as
5092c136579SFengguang Wu 				 * soon as the queue becomes uncongested.
5101da177e4SLinus Torvalds 				 */
5111da177e4SLinus Torvalds 				inode->i_state |= I_DIRTY_PAGES;
512b3af9468SWu Fengguang select_queue:
5138bc3be27SFengguang Wu 				if (wbc->nr_to_write <= 0) {
5148bc3be27SFengguang Wu 					/*
5158bc3be27SFengguang Wu 					 * slice used up: queue for next turn
5168bc3be27SFengguang Wu 					 */
5170e0f4fc2SKen Chen 					requeue_io(inode);
5181da177e4SLinus Torvalds 				} else {
5191da177e4SLinus Torvalds 					/*
5208bc3be27SFengguang Wu 					 * somehow blocked: retry later
5218bc3be27SFengguang Wu 					 */
5228bc3be27SFengguang Wu 					redirty_tail(inode);
5238bc3be27SFengguang Wu 				}
5248bc3be27SFengguang Wu 			} else {
5258bc3be27SFengguang Wu 				/*
5261da177e4SLinus Torvalds 				 * Otherwise fully redirty the inode so that
5271da177e4SLinus Torvalds 				 * other inodes on this superblock will get some
5281da177e4SLinus Torvalds 				 * writeout.  Otherwise heavy writing to one
5291da177e4SLinus Torvalds 				 * file would indefinitely suspend writeout of
5301da177e4SLinus Torvalds 				 * all the other files.
5311da177e4SLinus Torvalds 				 */
5321da177e4SLinus Torvalds 				inode->i_state |= I_DIRTY_PAGES;
5331b43ef91SAndrew Morton 				redirty_tail(inode);
5341da177e4SLinus Torvalds 			}
5351da177e4SLinus Torvalds 		} else if (atomic_read(&inode->i_count)) {
5361da177e4SLinus Torvalds 			/*
5371da177e4SLinus Torvalds 			 * The inode is clean, inuse
5381da177e4SLinus Torvalds 			 */
5391da177e4SLinus Torvalds 			list_move(&inode->i_list, &inode_in_use);
5401da177e4SLinus Torvalds 		} else {
5411da177e4SLinus Torvalds 			/*
5421da177e4SLinus Torvalds 			 * The inode is clean, unused
5431da177e4SLinus Torvalds 			 */
5441da177e4SLinus Torvalds 			list_move(&inode->i_list, &inode_unused);
5451da177e4SLinus Torvalds 		}
5461da177e4SLinus Torvalds 	}
5471c0eeaf5SJoern Engel 	inode_sync_complete(inode);
5481da177e4SLinus Torvalds 	return ret;
5491da177e4SLinus Torvalds }
5501da177e4SLinus Torvalds 
5519ecc2738SJens Axboe static void unpin_sb_for_writeback(struct super_block **psb)
5529ecc2738SJens Axboe {
5539ecc2738SJens Axboe 	struct super_block *sb = *psb;
5549ecc2738SJens Axboe 
5559ecc2738SJens Axboe 	if (sb) {
5569ecc2738SJens Axboe 		up_read(&sb->s_umount);
5579ecc2738SJens Axboe 		put_super(sb);
5589ecc2738SJens Axboe 		*psb = NULL;
5599ecc2738SJens Axboe 	}
5609ecc2738SJens Axboe }
5619ecc2738SJens Axboe 
56203ba3782SJens Axboe /*
56303ba3782SJens Axboe  * For WB_SYNC_NONE writeback, the caller does not have the sb pinned
56403ba3782SJens Axboe  * before calling writeback. So make sure that we do pin it, so it doesn't
56503ba3782SJens Axboe  * go away while we are writing inodes from it.
56603ba3782SJens Axboe  *
56703ba3782SJens Axboe  * Returns 0 if the super was successfully pinned (or pinning wasn't needed),
56803ba3782SJens Axboe  * 1 if we failed.
56903ba3782SJens Axboe  */
57003ba3782SJens Axboe static int pin_sb_for_writeback(struct writeback_control *wbc,
5719ecc2738SJens Axboe 				struct inode *inode, struct super_block **psb)
5721da177e4SLinus Torvalds {
57303ba3782SJens Axboe 	struct super_block *sb = inode->i_sb;
57403ba3782SJens Axboe 
57503ba3782SJens Axboe 	/*
5769ecc2738SJens Axboe 	 * If this sb is already pinned, nothing more to do. If not and
5779ecc2738SJens Axboe 	 * *psb is non-NULL, unpin the old one first
5789ecc2738SJens Axboe 	 */
5799ecc2738SJens Axboe 	if (sb == *psb)
5809ecc2738SJens Axboe 		return 0;
5819ecc2738SJens Axboe 	else if (*psb)
5829ecc2738SJens Axboe 		unpin_sb_for_writeback(psb);
5839ecc2738SJens Axboe 
5849ecc2738SJens Axboe 	/*
58503ba3782SJens Axboe 	 * Caller must already hold the ref for this
58603ba3782SJens Axboe 	 */
58703ba3782SJens Axboe 	if (wbc->sync_mode == WB_SYNC_ALL) {
58803ba3782SJens Axboe 		WARN_ON(!rwsem_is_locked(&sb->s_umount));
58903ba3782SJens Axboe 		return 0;
59003ba3782SJens Axboe 	}
59103ba3782SJens Axboe 
59203ba3782SJens Axboe 	spin_lock(&sb_lock);
59303ba3782SJens Axboe 	sb->s_count++;
59403ba3782SJens Axboe 	if (down_read_trylock(&sb->s_umount)) {
59503ba3782SJens Axboe 		if (sb->s_root) {
59603ba3782SJens Axboe 			spin_unlock(&sb_lock);
5979ecc2738SJens Axboe 			goto pinned;
59803ba3782SJens Axboe 		}
59903ba3782SJens Axboe 		/*
60003ba3782SJens Axboe 		 * umounted, drop rwsem again and fall through to failure
60103ba3782SJens Axboe 		 */
60203ba3782SJens Axboe 		up_read(&sb->s_umount);
60303ba3782SJens Axboe 	}
60403ba3782SJens Axboe 
60503ba3782SJens Axboe 	sb->s_count--;
60603ba3782SJens Axboe 	spin_unlock(&sb_lock);
60703ba3782SJens Axboe 	return 1;
6089ecc2738SJens Axboe pinned:
6099ecc2738SJens Axboe 	*psb = sb;
6109ecc2738SJens Axboe 	return 0;
61103ba3782SJens Axboe }
61203ba3782SJens Axboe 
61303ba3782SJens Axboe static void writeback_inodes_wb(struct bdi_writeback *wb,
61403ba3782SJens Axboe 				struct writeback_control *wbc)
61503ba3782SJens Axboe {
6169ecc2738SJens Axboe 	struct super_block *sb = wbc->sb, *pin_sb = NULL;
61766f3b8e2SJens Axboe 	const int is_blkdev_sb = sb_is_blkdev_sb(sb);
6181da177e4SLinus Torvalds 	const unsigned long start = jiffies;	/* livelock avoidance */
6191da177e4SLinus Torvalds 
620ae8547b0SHans Reiser 	spin_lock(&inode_lock);
6211da177e4SLinus Torvalds 
62203ba3782SJens Axboe 	if (!wbc->for_kupdate || list_empty(&wb->b_io))
62303ba3782SJens Axboe 		queue_io(wb, wbc->older_than_this);
62466f3b8e2SJens Axboe 
62503ba3782SJens Axboe 	while (!list_empty(&wb->b_io)) {
62603ba3782SJens Axboe 		struct inode *inode = list_entry(wb->b_io.prev,
6271da177e4SLinus Torvalds 						struct inode, i_list);
6281da177e4SLinus Torvalds 		long pages_skipped;
6291da177e4SLinus Torvalds 
63066f3b8e2SJens Axboe 		/*
63166f3b8e2SJens Axboe 		 * super block given and doesn't match, skip this inode
63266f3b8e2SJens Axboe 		 */
63366f3b8e2SJens Axboe 		if (sb && sb != inode->i_sb) {
63466f3b8e2SJens Axboe 			redirty_tail(inode);
63566f3b8e2SJens Axboe 			continue;
63666f3b8e2SJens Axboe 		}
63766f3b8e2SJens Axboe 
63803ba3782SJens Axboe 		if (!bdi_cap_writeback_dirty(wb->bdi)) {
6399852a0e7SAndrew Morton 			redirty_tail(inode);
64066f3b8e2SJens Axboe 			if (is_blkdev_sb) {
6411da177e4SLinus Torvalds 				/*
6421da177e4SLinus Torvalds 				 * Dirty memory-backed blockdev: the ramdisk
6431da177e4SLinus Torvalds 				 * driver does this.  Skip just this inode
6441da177e4SLinus Torvalds 				 */
6451da177e4SLinus Torvalds 				continue;
6461da177e4SLinus Torvalds 			}
6471da177e4SLinus Torvalds 			/*
6481da177e4SLinus Torvalds 			 * Dirty memory-backed inode against a filesystem other
6491da177e4SLinus Torvalds 			 * than the kernel-internal bdev filesystem.  Skip the
6501da177e4SLinus Torvalds 			 * entire superblock.
6511da177e4SLinus Torvalds 			 */
6521da177e4SLinus Torvalds 			break;
6531da177e4SLinus Torvalds 		}
6541da177e4SLinus Torvalds 
65584a89245SWu Fengguang 		if (inode->i_state & (I_NEW | I_WILL_FREE)) {
6567ef0d737SNick Piggin 			requeue_io(inode);
6577ef0d737SNick Piggin 			continue;
6587ef0d737SNick Piggin 		}
6597ef0d737SNick Piggin 
66003ba3782SJens Axboe 		if (wbc->nonblocking && bdi_write_congested(wb->bdi)) {
6611da177e4SLinus Torvalds 			wbc->encountered_congestion = 1;
66266f3b8e2SJens Axboe 			if (!is_blkdev_sb)
6631da177e4SLinus Torvalds 				break;		/* Skip a congested fs */
6640e0f4fc2SKen Chen 			requeue_io(inode);
6651da177e4SLinus Torvalds 			continue;		/* Skip a congested blockdev */
6661da177e4SLinus Torvalds 		}
6671da177e4SLinus Torvalds 
668d2caa3c5SJeff Layton 		/*
669d2caa3c5SJeff Layton 		 * Was this inode dirtied after sync_sb_inodes was called?
670d2caa3c5SJeff Layton 		 * This keeps sync from extra jobs and livelock.
671d2caa3c5SJeff Layton 		 */
672d2caa3c5SJeff Layton 		if (inode_dirtied_after(inode, start))
6731da177e4SLinus Torvalds 			break;
6741da177e4SLinus Torvalds 
6759ecc2738SJens Axboe 		if (pin_sb_for_writeback(wbc, inode, &pin_sb)) {
67603ba3782SJens Axboe 			requeue_io(inode);
67703ba3782SJens Axboe 			continue;
67803ba3782SJens Axboe 		}
6791da177e4SLinus Torvalds 
68084a89245SWu Fengguang 		BUG_ON(inode->i_state & (I_FREEING | I_CLEAR));
6811da177e4SLinus Torvalds 		__iget(inode);
6821da177e4SLinus Torvalds 		pages_skipped = wbc->pages_skipped;
68301c03194SChristoph Hellwig 		writeback_single_inode(inode, wbc);
6841da177e4SLinus Torvalds 		if (wbc->pages_skipped != pages_skipped) {
6851da177e4SLinus Torvalds 			/*
6861da177e4SLinus Torvalds 			 * writeback is not making progress due to locked
6871da177e4SLinus Torvalds 			 * buffers.  Skip this inode for now.
6881da177e4SLinus Torvalds 			 */
689f57b9b7bSAndrew Morton 			redirty_tail(inode);
6901da177e4SLinus Torvalds 		}
6911da177e4SLinus Torvalds 		spin_unlock(&inode_lock);
6921da177e4SLinus Torvalds 		iput(inode);
6934ffc8444SOGAWA Hirofumi 		cond_resched();
6941da177e4SLinus Torvalds 		spin_lock(&inode_lock);
6958bc3be27SFengguang Wu 		if (wbc->nr_to_write <= 0) {
6968bc3be27SFengguang Wu 			wbc->more_io = 1;
6971da177e4SLinus Torvalds 			break;
6981da177e4SLinus Torvalds 		}
69903ba3782SJens Axboe 		if (!list_empty(&wb->b_more_io))
7008bc3be27SFengguang Wu 			wbc->more_io = 1;
7018bc3be27SFengguang Wu 	}
70238f21977SNick Piggin 
7039ecc2738SJens Axboe 	unpin_sb_for_writeback(&pin_sb);
7049ecc2738SJens Axboe 
70566f3b8e2SJens Axboe 	spin_unlock(&inode_lock);
70666f3b8e2SJens Axboe 	/* Leave any unwritten inodes on b_io */
70766f3b8e2SJens Axboe }
70866f3b8e2SJens Axboe 
70903ba3782SJens Axboe void writeback_inodes_wbc(struct writeback_control *wbc)
71003ba3782SJens Axboe {
71103ba3782SJens Axboe 	struct backing_dev_info *bdi = wbc->bdi;
71203ba3782SJens Axboe 
71303ba3782SJens Axboe 	writeback_inodes_wb(&bdi->wb, wbc);
71403ba3782SJens Axboe }
71503ba3782SJens Axboe 
71603ba3782SJens Axboe /*
71703ba3782SJens Axboe  * The maximum number of pages to writeout in a single bdi flush/kupdate
71803ba3782SJens Axboe  * operation.  We do this so we don't hold I_SYNC against an inode for
71903ba3782SJens Axboe  * enormous amounts of time, which would block a userspace task which has
72003ba3782SJens Axboe  * been forced to throttle against that inode.  Also, the code reevaluates
72103ba3782SJens Axboe  * the dirty each time it has written this many pages.
72203ba3782SJens Axboe  */
72303ba3782SJens Axboe #define MAX_WRITEBACK_PAGES     1024
72403ba3782SJens Axboe 
72503ba3782SJens Axboe static inline bool over_bground_thresh(void)
72603ba3782SJens Axboe {
72703ba3782SJens Axboe 	unsigned long background_thresh, dirty_thresh;
72803ba3782SJens Axboe 
72903ba3782SJens Axboe 	get_dirty_limits(&background_thresh, &dirty_thresh, NULL, NULL);
73003ba3782SJens Axboe 
73103ba3782SJens Axboe 	return (global_page_state(NR_FILE_DIRTY) +
73203ba3782SJens Axboe 		global_page_state(NR_UNSTABLE_NFS) >= background_thresh);
73303ba3782SJens Axboe }
73403ba3782SJens Axboe 
73503ba3782SJens Axboe /*
73603ba3782SJens Axboe  * Explicit flushing or periodic writeback of "old" data.
73703ba3782SJens Axboe  *
73803ba3782SJens Axboe  * Define "old": the first time one of an inode's pages is dirtied, we mark the
73903ba3782SJens Axboe  * dirtying-time in the inode's address_space.  So this periodic writeback code
74003ba3782SJens Axboe  * just walks the superblock inode list, writing back any inodes which are
74103ba3782SJens Axboe  * older than a specific point in time.
74203ba3782SJens Axboe  *
74303ba3782SJens Axboe  * Try to run once per dirty_writeback_interval.  But if a writeback event
74403ba3782SJens Axboe  * takes longer than a dirty_writeback_interval interval, then leave a
74503ba3782SJens Axboe  * one-second gap.
74603ba3782SJens Axboe  *
74703ba3782SJens Axboe  * older_than_this takes precedence over nr_to_write.  So we'll only write back
74803ba3782SJens Axboe  * all dirty pages if they are all attached to "old" mappings.
74903ba3782SJens Axboe  */
750c4a77a6cSJens Axboe static long wb_writeback(struct bdi_writeback *wb,
751c4a77a6cSJens Axboe 			 struct wb_writeback_args *args)
75203ba3782SJens Axboe {
75303ba3782SJens Axboe 	struct writeback_control wbc = {
75403ba3782SJens Axboe 		.bdi			= wb->bdi,
755c4a77a6cSJens Axboe 		.sb			= args->sb,
756c4a77a6cSJens Axboe 		.sync_mode		= args->sync_mode,
75703ba3782SJens Axboe 		.older_than_this	= NULL,
758c4a77a6cSJens Axboe 		.for_kupdate		= args->for_kupdate,
759c4a77a6cSJens Axboe 		.range_cyclic		= args->range_cyclic,
76003ba3782SJens Axboe 	};
76103ba3782SJens Axboe 	unsigned long oldest_jif;
76203ba3782SJens Axboe 	long wrote = 0;
763a5989bdcSJan Kara 	struct inode *inode;
76403ba3782SJens Axboe 
76503ba3782SJens Axboe 	if (wbc.for_kupdate) {
76603ba3782SJens Axboe 		wbc.older_than_this = &oldest_jif;
76703ba3782SJens Axboe 		oldest_jif = jiffies -
76803ba3782SJens Axboe 				msecs_to_jiffies(dirty_expire_interval * 10);
76903ba3782SJens Axboe 	}
770c4a77a6cSJens Axboe 	if (!wbc.range_cyclic) {
771c4a77a6cSJens Axboe 		wbc.range_start = 0;
772c4a77a6cSJens Axboe 		wbc.range_end = LLONG_MAX;
773c4a77a6cSJens Axboe 	}
77403ba3782SJens Axboe 
77503ba3782SJens Axboe 	for (;;) {
77603ba3782SJens Axboe 		/*
777d3ddec76SWu Fengguang 		 * Stop writeback when nr_pages has been consumed
77803ba3782SJens Axboe 		 */
779d3ddec76SWu Fengguang 		if (args->nr_pages <= 0)
78003ba3782SJens Axboe 			break;
78103ba3782SJens Axboe 
78203ba3782SJens Axboe 		/*
783d3ddec76SWu Fengguang 		 * For background writeout, stop when we are below the
784d3ddec76SWu Fengguang 		 * background dirty threshold
78503ba3782SJens Axboe 		 */
786d3ddec76SWu Fengguang 		if (args->for_background && !over_bground_thresh())
78703ba3782SJens Axboe 			break;
78803ba3782SJens Axboe 
78903ba3782SJens Axboe 		wbc.more_io = 0;
79003ba3782SJens Axboe 		wbc.encountered_congestion = 0;
79103ba3782SJens Axboe 		wbc.nr_to_write = MAX_WRITEBACK_PAGES;
79203ba3782SJens Axboe 		wbc.pages_skipped = 0;
79303ba3782SJens Axboe 		writeback_inodes_wb(wb, &wbc);
794c4a77a6cSJens Axboe 		args->nr_pages -= MAX_WRITEBACK_PAGES - wbc.nr_to_write;
79503ba3782SJens Axboe 		wrote += MAX_WRITEBACK_PAGES - wbc.nr_to_write;
79603ba3782SJens Axboe 
79703ba3782SJens Axboe 		/*
79871fd05a8SJens Axboe 		 * If we consumed everything, see if we have more
79903ba3782SJens Axboe 		 */
80071fd05a8SJens Axboe 		if (wbc.nr_to_write <= 0)
80171fd05a8SJens Axboe 			continue;
80271fd05a8SJens Axboe 		/*
80371fd05a8SJens Axboe 		 * Didn't write everything and we don't have more IO, bail
80471fd05a8SJens Axboe 		 */
80571fd05a8SJens Axboe 		if (!wbc.more_io)
80671fd05a8SJens Axboe 			break;
80771fd05a8SJens Axboe 		/*
80871fd05a8SJens Axboe 		 * Did we write something? Try for more
80971fd05a8SJens Axboe 		 */
810a5989bdcSJan Kara 		if (wbc.nr_to_write < MAX_WRITEBACK_PAGES)
81103ba3782SJens Axboe 			continue;
812a5989bdcSJan Kara 		/*
813a5989bdcSJan Kara 		 * Nothing written. Wait for some inode to
814a5989bdcSJan Kara 		 * become available for writeback. Otherwise
815a5989bdcSJan Kara 		 * we'll just busyloop.
816a5989bdcSJan Kara 		 */
817a5989bdcSJan Kara 		spin_lock(&inode_lock);
818a5989bdcSJan Kara 		if (!list_empty(&wb->b_more_io))  {
81971fd05a8SJens Axboe 			inode = list_entry(wb->b_more_io.prev,
820a5989bdcSJan Kara 						struct inode, i_list);
821a5989bdcSJan Kara 			inode_wait_for_writeback(inode);
822a5989bdcSJan Kara 		}
823a5989bdcSJan Kara 		spin_unlock(&inode_lock);
82403ba3782SJens Axboe 	}
82503ba3782SJens Axboe 
82603ba3782SJens Axboe 	return wrote;
82703ba3782SJens Axboe }
82803ba3782SJens Axboe 
82903ba3782SJens Axboe /*
83003ba3782SJens Axboe  * Return the next bdi_work struct that hasn't been processed by this
8318010c3b6SJens Axboe  * wb thread yet. ->seen is initially set for each thread that exists
8328010c3b6SJens Axboe  * for this device, when a thread first notices a piece of work it
8338010c3b6SJens Axboe  * clears its bit. Depending on writeback type, the thread will notify
8348010c3b6SJens Axboe  * completion on either receiving the work (WB_SYNC_NONE) or after
8358010c3b6SJens Axboe  * it is done (WB_SYNC_ALL).
83603ba3782SJens Axboe  */
83703ba3782SJens Axboe static struct bdi_work *get_next_work_item(struct backing_dev_info *bdi,
83803ba3782SJens Axboe 					   struct bdi_writeback *wb)
83903ba3782SJens Axboe {
84003ba3782SJens Axboe 	struct bdi_work *work, *ret = NULL;
84103ba3782SJens Axboe 
84203ba3782SJens Axboe 	rcu_read_lock();
84303ba3782SJens Axboe 
84403ba3782SJens Axboe 	list_for_each_entry_rcu(work, &bdi->work_list, list) {
84577fad5e6SNick Piggin 		if (!test_bit(wb->nr, &work->seen))
84603ba3782SJens Axboe 			continue;
84777fad5e6SNick Piggin 		clear_bit(wb->nr, &work->seen);
84803ba3782SJens Axboe 
84903ba3782SJens Axboe 		ret = work;
85003ba3782SJens Axboe 		break;
85103ba3782SJens Axboe 	}
85203ba3782SJens Axboe 
85303ba3782SJens Axboe 	rcu_read_unlock();
85403ba3782SJens Axboe 	return ret;
85503ba3782SJens Axboe }
85603ba3782SJens Axboe 
85703ba3782SJens Axboe static long wb_check_old_data_flush(struct bdi_writeback *wb)
85803ba3782SJens Axboe {
85903ba3782SJens Axboe 	unsigned long expired;
86003ba3782SJens Axboe 	long nr_pages;
86103ba3782SJens Axboe 
86203ba3782SJens Axboe 	expired = wb->last_old_flush +
86303ba3782SJens Axboe 			msecs_to_jiffies(dirty_writeback_interval * 10);
86403ba3782SJens Axboe 	if (time_before(jiffies, expired))
86503ba3782SJens Axboe 		return 0;
86603ba3782SJens Axboe 
86703ba3782SJens Axboe 	wb->last_old_flush = jiffies;
86803ba3782SJens Axboe 	nr_pages = global_page_state(NR_FILE_DIRTY) +
86903ba3782SJens Axboe 			global_page_state(NR_UNSTABLE_NFS) +
87003ba3782SJens Axboe 			(inodes_stat.nr_inodes - inodes_stat.nr_unused);
87103ba3782SJens Axboe 
872c4a77a6cSJens Axboe 	if (nr_pages) {
873c4a77a6cSJens Axboe 		struct wb_writeback_args args = {
874c4a77a6cSJens Axboe 			.nr_pages	= nr_pages,
875c4a77a6cSJens Axboe 			.sync_mode	= WB_SYNC_NONE,
876c4a77a6cSJens Axboe 			.for_kupdate	= 1,
877c4a77a6cSJens Axboe 			.range_cyclic	= 1,
878c4a77a6cSJens Axboe 		};
879c4a77a6cSJens Axboe 
880c4a77a6cSJens Axboe 		return wb_writeback(wb, &args);
881c4a77a6cSJens Axboe 	}
88203ba3782SJens Axboe 
88303ba3782SJens Axboe 	return 0;
88403ba3782SJens Axboe }
88503ba3782SJens Axboe 
88603ba3782SJens Axboe /*
88703ba3782SJens Axboe  * Retrieve work items and do the writeback they describe
88803ba3782SJens Axboe  */
88903ba3782SJens Axboe long wb_do_writeback(struct bdi_writeback *wb, int force_wait)
89003ba3782SJens Axboe {
89103ba3782SJens Axboe 	struct backing_dev_info *bdi = wb->bdi;
89203ba3782SJens Axboe 	struct bdi_work *work;
893c4a77a6cSJens Axboe 	long wrote = 0;
89403ba3782SJens Axboe 
89503ba3782SJens Axboe 	while ((work = get_next_work_item(bdi, wb)) != NULL) {
896c4a77a6cSJens Axboe 		struct wb_writeback_args args = work->args;
89703ba3782SJens Axboe 
89803ba3782SJens Axboe 		/*
89903ba3782SJens Axboe 		 * Override sync mode, in case we must wait for completion
90003ba3782SJens Axboe 		 */
90103ba3782SJens Axboe 		if (force_wait)
902c4a77a6cSJens Axboe 			work->args.sync_mode = args.sync_mode = WB_SYNC_ALL;
90303ba3782SJens Axboe 
90403ba3782SJens Axboe 		/*
90503ba3782SJens Axboe 		 * If this isn't a data integrity operation, just notify
90603ba3782SJens Axboe 		 * that we have seen this work and we are now starting it.
90703ba3782SJens Axboe 		 */
908c4a77a6cSJens Axboe 		if (args.sync_mode == WB_SYNC_NONE)
90903ba3782SJens Axboe 			wb_clear_pending(wb, work);
91003ba3782SJens Axboe 
911c4a77a6cSJens Axboe 		wrote += wb_writeback(wb, &args);
91203ba3782SJens Axboe 
91303ba3782SJens Axboe 		/*
91403ba3782SJens Axboe 		 * This is a data integrity writeback, so only do the
91503ba3782SJens Axboe 		 * notification when we have completed the work.
91603ba3782SJens Axboe 		 */
917c4a77a6cSJens Axboe 		if (args.sync_mode == WB_SYNC_ALL)
91803ba3782SJens Axboe 			wb_clear_pending(wb, work);
91903ba3782SJens Axboe 	}
92003ba3782SJens Axboe 
92103ba3782SJens Axboe 	/*
92203ba3782SJens Axboe 	 * Check for periodic writeback, kupdated() style
92303ba3782SJens Axboe 	 */
92403ba3782SJens Axboe 	wrote += wb_check_old_data_flush(wb);
92503ba3782SJens Axboe 
92603ba3782SJens Axboe 	return wrote;
92703ba3782SJens Axboe }
92803ba3782SJens Axboe 
92903ba3782SJens Axboe /*
93003ba3782SJens Axboe  * Handle writeback of dirty data for the device backed by this bdi. Also
93103ba3782SJens Axboe  * wakes up periodically and does kupdated style flushing.
93203ba3782SJens Axboe  */
93303ba3782SJens Axboe int bdi_writeback_task(struct bdi_writeback *wb)
93403ba3782SJens Axboe {
93503ba3782SJens Axboe 	unsigned long last_active = jiffies;
93603ba3782SJens Axboe 	unsigned long wait_jiffies = -1UL;
93703ba3782SJens Axboe 	long pages_written;
93803ba3782SJens Axboe 
93903ba3782SJens Axboe 	while (!kthread_should_stop()) {
94003ba3782SJens Axboe 		pages_written = wb_do_writeback(wb, 0);
94103ba3782SJens Axboe 
94203ba3782SJens Axboe 		if (pages_written)
94303ba3782SJens Axboe 			last_active = jiffies;
94403ba3782SJens Axboe 		else if (wait_jiffies != -1UL) {
94503ba3782SJens Axboe 			unsigned long max_idle;
94603ba3782SJens Axboe 
94703ba3782SJens Axboe 			/*
94803ba3782SJens Axboe 			 * Longest period of inactivity that we tolerate. If we
94903ba3782SJens Axboe 			 * see dirty data again later, the task will get
95003ba3782SJens Axboe 			 * recreated automatically.
95103ba3782SJens Axboe 			 */
95203ba3782SJens Axboe 			max_idle = max(5UL * 60 * HZ, wait_jiffies);
95303ba3782SJens Axboe 			if (time_after(jiffies, max_idle + last_active))
95403ba3782SJens Axboe 				break;
95503ba3782SJens Axboe 		}
95603ba3782SJens Axboe 
95703ba3782SJens Axboe 		wait_jiffies = msecs_to_jiffies(dirty_writeback_interval * 10);
95849db0414SJens Axboe 		schedule_timeout_interruptible(wait_jiffies);
95903ba3782SJens Axboe 		try_to_freeze();
96003ba3782SJens Axboe 	}
96103ba3782SJens Axboe 
96203ba3782SJens Axboe 	return 0;
96303ba3782SJens Axboe }
96403ba3782SJens Axboe 
96503ba3782SJens Axboe /*
966b6e51316SJens Axboe  * Schedule writeback for all backing devices. This does WB_SYNC_NONE
967b6e51316SJens Axboe  * writeback, for integrity writeback see bdi_sync_writeback().
96803ba3782SJens Axboe  */
969b6e51316SJens Axboe static void bdi_writeback_all(struct super_block *sb, long nr_pages)
97003ba3782SJens Axboe {
971b6e51316SJens Axboe 	struct wb_writeback_args args = {
972b6e51316SJens Axboe 		.sb		= sb,
973b6e51316SJens Axboe 		.nr_pages	= nr_pages,
974b6e51316SJens Axboe 		.sync_mode	= WB_SYNC_NONE,
975b6e51316SJens Axboe 	};
97603ba3782SJens Axboe 	struct backing_dev_info *bdi;
97703ba3782SJens Axboe 
978cfc4ba53SJens Axboe 	rcu_read_lock();
97903ba3782SJens Axboe 
980cfc4ba53SJens Axboe 	list_for_each_entry_rcu(bdi, &bdi_list, bdi_list) {
98103ba3782SJens Axboe 		if (!bdi_has_dirty_io(bdi))
98203ba3782SJens Axboe 			continue;
98303ba3782SJens Axboe 
984b6e51316SJens Axboe 		bdi_alloc_queue_work(bdi, &args);
98503ba3782SJens Axboe 	}
98603ba3782SJens Axboe 
987cfc4ba53SJens Axboe 	rcu_read_unlock();
98803ba3782SJens Axboe }
98903ba3782SJens Axboe 
99003ba3782SJens Axboe /*
99103ba3782SJens Axboe  * Start writeback of `nr_pages' pages.  If `nr_pages' is zero, write back
99203ba3782SJens Axboe  * the whole world.
99303ba3782SJens Axboe  */
99403ba3782SJens Axboe void wakeup_flusher_threads(long nr_pages)
99503ba3782SJens Axboe {
99603ba3782SJens Axboe 	if (nr_pages == 0)
99703ba3782SJens Axboe 		nr_pages = global_page_state(NR_FILE_DIRTY) +
99803ba3782SJens Axboe 				global_page_state(NR_UNSTABLE_NFS);
999b6e51316SJens Axboe 	bdi_writeback_all(NULL, nr_pages);
100003ba3782SJens Axboe }
100103ba3782SJens Axboe 
100203ba3782SJens Axboe static noinline void block_dump___mark_inode_dirty(struct inode *inode)
100303ba3782SJens Axboe {
100403ba3782SJens Axboe 	if (inode->i_ino || strcmp(inode->i_sb->s_id, "bdev")) {
100503ba3782SJens Axboe 		struct dentry *dentry;
100603ba3782SJens Axboe 		const char *name = "?";
100703ba3782SJens Axboe 
100803ba3782SJens Axboe 		dentry = d_find_alias(inode);
100903ba3782SJens Axboe 		if (dentry) {
101003ba3782SJens Axboe 			spin_lock(&dentry->d_lock);
101103ba3782SJens Axboe 			name = (const char *) dentry->d_name.name;
101203ba3782SJens Axboe 		}
101303ba3782SJens Axboe 		printk(KERN_DEBUG
101403ba3782SJens Axboe 		       "%s(%d): dirtied inode %lu (%s) on %s\n",
101503ba3782SJens Axboe 		       current->comm, task_pid_nr(current), inode->i_ino,
101603ba3782SJens Axboe 		       name, inode->i_sb->s_id);
101703ba3782SJens Axboe 		if (dentry) {
101803ba3782SJens Axboe 			spin_unlock(&dentry->d_lock);
101903ba3782SJens Axboe 			dput(dentry);
102003ba3782SJens Axboe 		}
102103ba3782SJens Axboe 	}
102203ba3782SJens Axboe }
102303ba3782SJens Axboe 
102403ba3782SJens Axboe /**
102503ba3782SJens Axboe  *	__mark_inode_dirty -	internal function
102603ba3782SJens Axboe  *	@inode: inode to mark
102703ba3782SJens Axboe  *	@flags: what kind of dirty (i.e. I_DIRTY_SYNC)
102803ba3782SJens Axboe  *	Mark an inode as dirty. Callers should use mark_inode_dirty or
102903ba3782SJens Axboe  *  	mark_inode_dirty_sync.
103003ba3782SJens Axboe  *
103103ba3782SJens Axboe  * Put the inode on the super block's dirty list.
103203ba3782SJens Axboe  *
103303ba3782SJens Axboe  * CAREFUL! We mark it dirty unconditionally, but move it onto the
103403ba3782SJens Axboe  * dirty list only if it is hashed or if it refers to a blockdev.
103503ba3782SJens Axboe  * If it was not hashed, it will never be added to the dirty list
103603ba3782SJens Axboe  * even if it is later hashed, as it will have been marked dirty already.
103703ba3782SJens Axboe  *
103803ba3782SJens Axboe  * In short, make sure you hash any inodes _before_ you start marking
103903ba3782SJens Axboe  * them dirty.
104003ba3782SJens Axboe  *
104103ba3782SJens Axboe  * This function *must* be atomic for the I_DIRTY_PAGES case -
104203ba3782SJens Axboe  * set_page_dirty() is called under spinlock in several places.
104303ba3782SJens Axboe  *
104403ba3782SJens Axboe  * Note that for blockdevs, inode->dirtied_when represents the dirtying time of
104503ba3782SJens Axboe  * the block-special inode (/dev/hda1) itself.  And the ->dirtied_when field of
104603ba3782SJens Axboe  * the kernel-internal blockdev inode represents the dirtying time of the
104703ba3782SJens Axboe  * blockdev's pages.  This is why for I_DIRTY_PAGES we always use
104803ba3782SJens Axboe  * page->mapping->host, so the page-dirtying time is recorded in the internal
104903ba3782SJens Axboe  * blockdev inode.
105003ba3782SJens Axboe  */
105103ba3782SJens Axboe void __mark_inode_dirty(struct inode *inode, int flags)
105203ba3782SJens Axboe {
105303ba3782SJens Axboe 	struct super_block *sb = inode->i_sb;
105403ba3782SJens Axboe 
105503ba3782SJens Axboe 	/*
105603ba3782SJens Axboe 	 * Don't do this for I_DIRTY_PAGES - that doesn't actually
105703ba3782SJens Axboe 	 * dirty the inode itself
105803ba3782SJens Axboe 	 */
105903ba3782SJens Axboe 	if (flags & (I_DIRTY_SYNC | I_DIRTY_DATASYNC)) {
106003ba3782SJens Axboe 		if (sb->s_op->dirty_inode)
106103ba3782SJens Axboe 			sb->s_op->dirty_inode(inode);
106203ba3782SJens Axboe 	}
106303ba3782SJens Axboe 
106403ba3782SJens Axboe 	/*
106503ba3782SJens Axboe 	 * make sure that changes are seen by all cpus before we test i_state
106603ba3782SJens Axboe 	 * -- mikulas
106703ba3782SJens Axboe 	 */
106803ba3782SJens Axboe 	smp_mb();
106903ba3782SJens Axboe 
107003ba3782SJens Axboe 	/* avoid the locking if we can */
107103ba3782SJens Axboe 	if ((inode->i_state & flags) == flags)
107203ba3782SJens Axboe 		return;
107303ba3782SJens Axboe 
107403ba3782SJens Axboe 	if (unlikely(block_dump))
107503ba3782SJens Axboe 		block_dump___mark_inode_dirty(inode);
107603ba3782SJens Axboe 
107703ba3782SJens Axboe 	spin_lock(&inode_lock);
107803ba3782SJens Axboe 	if ((inode->i_state & flags) != flags) {
107903ba3782SJens Axboe 		const int was_dirty = inode->i_state & I_DIRTY;
108003ba3782SJens Axboe 
108103ba3782SJens Axboe 		inode->i_state |= flags;
108203ba3782SJens Axboe 
108303ba3782SJens Axboe 		/*
108403ba3782SJens Axboe 		 * If the inode is being synced, just update its dirty state.
108503ba3782SJens Axboe 		 * The unlocker will place the inode on the appropriate
108603ba3782SJens Axboe 		 * superblock list, based upon its state.
108703ba3782SJens Axboe 		 */
108803ba3782SJens Axboe 		if (inode->i_state & I_SYNC)
108903ba3782SJens Axboe 			goto out;
109003ba3782SJens Axboe 
109103ba3782SJens Axboe 		/*
109203ba3782SJens Axboe 		 * Only add valid (hashed) inodes to the superblock's
109303ba3782SJens Axboe 		 * dirty list.  Add blockdev inodes as well.
109403ba3782SJens Axboe 		 */
109503ba3782SJens Axboe 		if (!S_ISBLK(inode->i_mode)) {
109603ba3782SJens Axboe 			if (hlist_unhashed(&inode->i_hash))
109703ba3782SJens Axboe 				goto out;
109803ba3782SJens Axboe 		}
109903ba3782SJens Axboe 		if (inode->i_state & (I_FREEING|I_CLEAR))
110003ba3782SJens Axboe 			goto out;
110103ba3782SJens Axboe 
110203ba3782SJens Axboe 		/*
110303ba3782SJens Axboe 		 * If the inode was already on b_dirty/b_io/b_more_io, don't
110403ba3782SJens Axboe 		 * reposition it (that would break b_dirty time-ordering).
110503ba3782SJens Axboe 		 */
110603ba3782SJens Axboe 		if (!was_dirty) {
110703ba3782SJens Axboe 			struct bdi_writeback *wb = &inode_to_bdi(inode)->wb;
1108500b067cSJens Axboe 			struct backing_dev_info *bdi = wb->bdi;
1109500b067cSJens Axboe 
1110500b067cSJens Axboe 			if (bdi_cap_writeback_dirty(bdi) &&
1111500b067cSJens Axboe 			    !test_bit(BDI_registered, &bdi->state)) {
1112500b067cSJens Axboe 				WARN_ON(1);
1113500b067cSJens Axboe 				printk(KERN_ERR "bdi-%s not registered\n",
1114500b067cSJens Axboe 								bdi->name);
1115500b067cSJens Axboe 			}
111603ba3782SJens Axboe 
111703ba3782SJens Axboe 			inode->dirtied_when = jiffies;
111803ba3782SJens Axboe 			list_move(&inode->i_list, &wb->b_dirty);
111903ba3782SJens Axboe 		}
112003ba3782SJens Axboe 	}
112103ba3782SJens Axboe out:
112203ba3782SJens Axboe 	spin_unlock(&inode_lock);
112303ba3782SJens Axboe }
112403ba3782SJens Axboe EXPORT_SYMBOL(__mark_inode_dirty);
112503ba3782SJens Axboe 
112666f3b8e2SJens Axboe /*
112766f3b8e2SJens Axboe  * Write out a superblock's list of dirty inodes.  A wait will be performed
112866f3b8e2SJens Axboe  * upon no inodes, all inodes or the final one, depending upon sync_mode.
112966f3b8e2SJens Axboe  *
113066f3b8e2SJens Axboe  * If older_than_this is non-NULL, then only write out inodes which
113166f3b8e2SJens Axboe  * had their first dirtying at a time earlier than *older_than_this.
113266f3b8e2SJens Axboe  *
113366f3b8e2SJens Axboe  * If `bdi' is non-zero then we're being asked to writeback a specific queue.
113466f3b8e2SJens Axboe  * This function assumes that the blockdev superblock's inodes are backed by
113566f3b8e2SJens Axboe  * a variety of queues, so all inodes are searched.  For other superblocks,
113666f3b8e2SJens Axboe  * assume that all inodes are backed by the same queue.
113766f3b8e2SJens Axboe  *
113866f3b8e2SJens Axboe  * The inodes to be written are parked on bdi->b_io.  They are moved back onto
113966f3b8e2SJens Axboe  * bdi->b_dirty as they are selected for writing.  This way, none can be missed
114066f3b8e2SJens Axboe  * on the writer throttling path, and we get decent balancing between many
114166f3b8e2SJens Axboe  * throttled threads: we don't want them all piling up on inode_sync_wait.
114266f3b8e2SJens Axboe  */
1143b6e51316SJens Axboe static void wait_sb_inodes(struct super_block *sb)
114466f3b8e2SJens Axboe {
114538f21977SNick Piggin 	struct inode *inode, *old_inode = NULL;
114638f21977SNick Piggin 
114703ba3782SJens Axboe 	/*
114803ba3782SJens Axboe 	 * We need to be protected against the filesystem going from
114903ba3782SJens Axboe 	 * r/o to r/w or vice versa.
115003ba3782SJens Axboe 	 */
1151b6e51316SJens Axboe 	WARN_ON(!rwsem_is_locked(&sb->s_umount));
115203ba3782SJens Axboe 
115366f3b8e2SJens Axboe 	spin_lock(&inode_lock);
115466f3b8e2SJens Axboe 
115538f21977SNick Piggin 	/*
115638f21977SNick Piggin 	 * Data integrity sync. Must wait for all pages under writeback,
115738f21977SNick Piggin 	 * because there may have been pages dirtied before our sync
115838f21977SNick Piggin 	 * call, but which had writeout started before we write it out.
115938f21977SNick Piggin 	 * In which case, the inode may not be on the dirty list, but
116038f21977SNick Piggin 	 * we still have to wait for that writeout.
116138f21977SNick Piggin 	 */
1162b6e51316SJens Axboe 	list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
116338f21977SNick Piggin 		struct address_space *mapping;
116438f21977SNick Piggin 
116503ba3782SJens Axboe 		if (inode->i_state & (I_FREEING|I_CLEAR|I_WILL_FREE|I_NEW))
116638f21977SNick Piggin 			continue;
116738f21977SNick Piggin 		mapping = inode->i_mapping;
116838f21977SNick Piggin 		if (mapping->nrpages == 0)
116938f21977SNick Piggin 			continue;
117038f21977SNick Piggin 		__iget(inode);
1171ae8547b0SHans Reiser 		spin_unlock(&inode_lock);
117238f21977SNick Piggin 		/*
117338f21977SNick Piggin 		 * We hold a reference to 'inode' so it couldn't have
117438f21977SNick Piggin 		 * been removed from s_inodes list while we dropped the
117538f21977SNick Piggin 		 * inode_lock.  We cannot iput the inode now as we can
117638f21977SNick Piggin 		 * be holding the last reference and we cannot iput it
117738f21977SNick Piggin 		 * under inode_lock. So we keep the reference and iput
117838f21977SNick Piggin 		 * it later.
117938f21977SNick Piggin 		 */
118038f21977SNick Piggin 		iput(old_inode);
118138f21977SNick Piggin 		old_inode = inode;
118238f21977SNick Piggin 
118338f21977SNick Piggin 		filemap_fdatawait(mapping);
118438f21977SNick Piggin 
118538f21977SNick Piggin 		cond_resched();
118638f21977SNick Piggin 
118738f21977SNick Piggin 		spin_lock(&inode_lock);
118838f21977SNick Piggin 	}
118938f21977SNick Piggin 	spin_unlock(&inode_lock);
119038f21977SNick Piggin 	iput(old_inode);
119166f3b8e2SJens Axboe }
11921da177e4SLinus Torvalds 
1193d8a8559cSJens Axboe /**
1194d8a8559cSJens Axboe  * writeback_inodes_sb	-	writeback dirty inodes from given super_block
1195d8a8559cSJens Axboe  * @sb: the superblock
11961da177e4SLinus Torvalds  *
1197d8a8559cSJens Axboe  * Start writeback on some inodes on this super_block. No guarantees are made
1198d8a8559cSJens Axboe  * on how many (if any) will be written, and this function does not wait
1199d8a8559cSJens Axboe  * for IO completion of submitted IO. The number of pages submitted is
1200d8a8559cSJens Axboe  * returned.
12011da177e4SLinus Torvalds  */
1202b6e51316SJens Axboe void writeback_inodes_sb(struct super_block *sb)
12031da177e4SLinus Torvalds {
1204b1e7a8fdSChristoph Lameter 	unsigned long nr_dirty = global_page_state(NR_FILE_DIRTY);
1205fd39fc85SChristoph Lameter 	unsigned long nr_unstable = global_page_state(NR_UNSTABLE_NFS);
1206d8a8559cSJens Axboe 	long nr_to_write;
12071da177e4SLinus Torvalds 
1208d8a8559cSJens Axboe 	nr_to_write = nr_dirty + nr_unstable +
120938f21977SNick Piggin 			(inodes_stat.nr_inodes - inodes_stat.nr_unused);
121038f21977SNick Piggin 
1211a72bfd4dSJens Axboe 	bdi_start_writeback(sb->s_bdi, sb, nr_to_write);
12121da177e4SLinus Torvalds }
1213d8a8559cSJens Axboe EXPORT_SYMBOL(writeback_inodes_sb);
1214d8a8559cSJens Axboe 
1215d8a8559cSJens Axboe /**
1216d8a8559cSJens Axboe  * sync_inodes_sb	-	sync sb inode pages
1217d8a8559cSJens Axboe  * @sb: the superblock
1218d8a8559cSJens Axboe  *
1219d8a8559cSJens Axboe  * This function writes and waits on any dirty inode belonging to this
1220d8a8559cSJens Axboe  * super_block. The number of pages synced is returned.
1221d8a8559cSJens Axboe  */
1222b6e51316SJens Axboe void sync_inodes_sb(struct super_block *sb)
1223d8a8559cSJens Axboe {
1224b6e51316SJens Axboe 	bdi_sync_writeback(sb->s_bdi, sb);
1225b6e51316SJens Axboe 	wait_sb_inodes(sb);
1226d8a8559cSJens Axboe }
1227d8a8559cSJens Axboe EXPORT_SYMBOL(sync_inodes_sb);
12281da177e4SLinus Torvalds 
12291da177e4SLinus Torvalds /**
12301da177e4SLinus Torvalds  * write_inode_now	-	write an inode to disk
12311da177e4SLinus Torvalds  * @inode: inode to write to disk
12321da177e4SLinus Torvalds  * @sync: whether the write should be synchronous or not
12331da177e4SLinus Torvalds  *
12347f04c26dSAndrea Arcangeli  * This function commits an inode to disk immediately if it is dirty. This is
12357f04c26dSAndrea Arcangeli  * primarily needed by knfsd.
12367f04c26dSAndrea Arcangeli  *
12377f04c26dSAndrea Arcangeli  * The caller must either have a ref on the inode or must have set I_WILL_FREE.
12381da177e4SLinus Torvalds  */
12391da177e4SLinus Torvalds int write_inode_now(struct inode *inode, int sync)
12401da177e4SLinus Torvalds {
12411da177e4SLinus Torvalds 	int ret;
12421da177e4SLinus Torvalds 	struct writeback_control wbc = {
12431da177e4SLinus Torvalds 		.nr_to_write = LONG_MAX,
124418914b18SMike Galbraith 		.sync_mode = sync ? WB_SYNC_ALL : WB_SYNC_NONE,
1245111ebb6eSOGAWA Hirofumi 		.range_start = 0,
1246111ebb6eSOGAWA Hirofumi 		.range_end = LLONG_MAX,
12471da177e4SLinus Torvalds 	};
12481da177e4SLinus Torvalds 
12491da177e4SLinus Torvalds 	if (!mapping_cap_writeback_dirty(inode->i_mapping))
125049364ce2SAndrew Morton 		wbc.nr_to_write = 0;
12511da177e4SLinus Torvalds 
12521da177e4SLinus Torvalds 	might_sleep();
12531da177e4SLinus Torvalds 	spin_lock(&inode_lock);
125401c03194SChristoph Hellwig 	ret = writeback_single_inode(inode, &wbc);
12551da177e4SLinus Torvalds 	spin_unlock(&inode_lock);
12561da177e4SLinus Torvalds 	if (sync)
12571c0eeaf5SJoern Engel 		inode_sync_wait(inode);
12581da177e4SLinus Torvalds 	return ret;
12591da177e4SLinus Torvalds }
12601da177e4SLinus Torvalds EXPORT_SYMBOL(write_inode_now);
12611da177e4SLinus Torvalds 
12621da177e4SLinus Torvalds /**
12631da177e4SLinus Torvalds  * sync_inode - write an inode and its pages to disk.
12641da177e4SLinus Torvalds  * @inode: the inode to sync
12651da177e4SLinus Torvalds  * @wbc: controls the writeback mode
12661da177e4SLinus Torvalds  *
12671da177e4SLinus Torvalds  * sync_inode() will write an inode and its pages to disk.  It will also
12681da177e4SLinus Torvalds  * correctly update the inode on its superblock's dirty inode lists and will
12691da177e4SLinus Torvalds  * update inode->i_state.
12701da177e4SLinus Torvalds  *
12711da177e4SLinus Torvalds  * The caller must have a ref on the inode.
12721da177e4SLinus Torvalds  */
12731da177e4SLinus Torvalds int sync_inode(struct inode *inode, struct writeback_control *wbc)
12741da177e4SLinus Torvalds {
12751da177e4SLinus Torvalds 	int ret;
12761da177e4SLinus Torvalds 
12771da177e4SLinus Torvalds 	spin_lock(&inode_lock);
127801c03194SChristoph Hellwig 	ret = writeback_single_inode(inode, wbc);
12791da177e4SLinus Torvalds 	spin_unlock(&inode_lock);
12801da177e4SLinus Torvalds 	return ret;
12811da177e4SLinus Torvalds }
12821da177e4SLinus Torvalds EXPORT_SYMBOL(sync_inode);
1283