xref: /openbmc/linux/fs/fs-writeback.c (revision 77b9d059)
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;
44c4a77a6cSJens Axboe 	int for_kupdate;
45c4a77a6cSJens Axboe 	int range_cyclic;
46c4a77a6cSJens Axboe };
47c4a77a6cSJens Axboe 
48c4a77a6cSJens Axboe /*
4903ba3782SJens Axboe  * Work items for the bdi_writeback threads
50f11b00f3SAdrian Bunk  */
5103ba3782SJens Axboe struct bdi_work {
528010c3b6SJens Axboe 	struct list_head list;		/* pending work list */
538010c3b6SJens Axboe 	struct rcu_head rcu_head;	/* for RCU free/clear of work */
5403ba3782SJens Axboe 
558010c3b6SJens Axboe 	unsigned long seen;		/* threads that have seen this work */
568010c3b6SJens Axboe 	atomic_t pending;		/* number of threads still to do work */
5703ba3782SJens Axboe 
588010c3b6SJens Axboe 	struct wb_writeback_args args;	/* writeback arguments */
5903ba3782SJens Axboe 
608010c3b6SJens Axboe 	unsigned long state;		/* flag bits, see WS_* */
6103ba3782SJens Axboe };
6203ba3782SJens Axboe 
6303ba3782SJens Axboe enum {
6403ba3782SJens Axboe 	WS_USED_B = 0,
6503ba3782SJens Axboe 	WS_ONSTACK_B,
6603ba3782SJens Axboe };
6703ba3782SJens Axboe 
6803ba3782SJens Axboe #define WS_USED (1 << WS_USED_B)
6903ba3782SJens Axboe #define WS_ONSTACK (1 << WS_ONSTACK_B)
7003ba3782SJens Axboe 
7103ba3782SJens Axboe static inline bool bdi_work_on_stack(struct bdi_work *work)
72f11b00f3SAdrian Bunk {
7303ba3782SJens Axboe 	return test_bit(WS_ONSTACK_B, &work->state);
7403ba3782SJens Axboe }
7503ba3782SJens Axboe 
7603ba3782SJens Axboe static inline void bdi_work_init(struct bdi_work *work,
77b6e51316SJens Axboe 				 struct wb_writeback_args *args)
7803ba3782SJens Axboe {
7903ba3782SJens Axboe 	INIT_RCU_HEAD(&work->rcu_head);
80b6e51316SJens Axboe 	work->args = *args;
8103ba3782SJens Axboe 	work->state = WS_USED;
8203ba3782SJens Axboe }
8303ba3782SJens Axboe 
84f11b00f3SAdrian Bunk /**
85f11b00f3SAdrian Bunk  * writeback_in_progress - determine whether there is writeback in progress
86f11b00f3SAdrian Bunk  * @bdi: the device's backing_dev_info structure.
87f11b00f3SAdrian Bunk  *
8803ba3782SJens Axboe  * Determine whether there is writeback waiting to be handled against a
8903ba3782SJens Axboe  * backing device.
90f11b00f3SAdrian Bunk  */
91f11b00f3SAdrian Bunk int writeback_in_progress(struct backing_dev_info *bdi)
92f11b00f3SAdrian Bunk {
9303ba3782SJens Axboe 	return !list_empty(&bdi->work_list);
94f11b00f3SAdrian Bunk }
95f11b00f3SAdrian Bunk 
9603ba3782SJens Axboe static void bdi_work_clear(struct bdi_work *work)
97f11b00f3SAdrian Bunk {
9803ba3782SJens Axboe 	clear_bit(WS_USED_B, &work->state);
9903ba3782SJens Axboe 	smp_mb__after_clear_bit();
10003ba3782SJens Axboe 	wake_up_bit(&work->state, WS_USED_B);
101f11b00f3SAdrian Bunk }
102f11b00f3SAdrian Bunk 
10303ba3782SJens Axboe static void bdi_work_free(struct rcu_head *head)
1044195f73dSNick Piggin {
10503ba3782SJens Axboe 	struct bdi_work *work = container_of(head, struct bdi_work, rcu_head);
1064195f73dSNick Piggin 
10703ba3782SJens Axboe 	if (!bdi_work_on_stack(work))
10803ba3782SJens Axboe 		kfree(work);
10903ba3782SJens Axboe 	else
11003ba3782SJens Axboe 		bdi_work_clear(work);
1114195f73dSNick Piggin }
1124195f73dSNick Piggin 
11303ba3782SJens Axboe static void wb_work_complete(struct bdi_work *work)
1141da177e4SLinus Torvalds {
115c4a77a6cSJens Axboe 	const enum writeback_sync_modes sync_mode = work->args.sync_mode;
11677b9d059SNick Piggin 	int onstack = bdi_work_on_stack(work);
1171da177e4SLinus Torvalds 
1181da177e4SLinus Torvalds 	/*
11903ba3782SJens Axboe 	 * For allocated work, we can clear the done/seen bit right here.
12003ba3782SJens Axboe 	 * For on-stack work, we need to postpone both the clear and free
12103ba3782SJens Axboe 	 * to after the RCU grace period, since the stack could be invalidated
12203ba3782SJens Axboe 	 * as soon as bdi_work_clear() has done the wakeup.
1231da177e4SLinus Torvalds 	 */
12477b9d059SNick Piggin 	if (!onstack)
12503ba3782SJens Axboe 		bdi_work_clear(work);
12677b9d059SNick Piggin 	if (sync_mode == WB_SYNC_NONE || onstack)
12703ba3782SJens Axboe 		call_rcu(&work->rcu_head, bdi_work_free);
1281da177e4SLinus Torvalds }
1291da177e4SLinus Torvalds 
13003ba3782SJens Axboe static void wb_clear_pending(struct bdi_writeback *wb, struct bdi_work *work)
13103ba3782SJens Axboe {
1321da177e4SLinus Torvalds 	/*
13303ba3782SJens Axboe 	 * The caller has retrieved the work arguments from this work,
13403ba3782SJens Axboe 	 * drop our reference. If this is the last ref, delete and free it
13503ba3782SJens Axboe 	 */
13603ba3782SJens Axboe 	if (atomic_dec_and_test(&work->pending)) {
13703ba3782SJens Axboe 		struct backing_dev_info *bdi = wb->bdi;
13803ba3782SJens Axboe 
13903ba3782SJens Axboe 		spin_lock(&bdi->wb_lock);
14003ba3782SJens Axboe 		list_del_rcu(&work->list);
14103ba3782SJens Axboe 		spin_unlock(&bdi->wb_lock);
14203ba3782SJens Axboe 
14303ba3782SJens Axboe 		wb_work_complete(work);
14403ba3782SJens Axboe 	}
14503ba3782SJens Axboe }
14603ba3782SJens Axboe 
14703ba3782SJens Axboe static void bdi_queue_work(struct backing_dev_info *bdi, struct bdi_work *work)
14803ba3782SJens Axboe {
14903ba3782SJens Axboe 	work->seen = bdi->wb_mask;
15003ba3782SJens Axboe 	BUG_ON(!work->seen);
15103ba3782SJens Axboe 	atomic_set(&work->pending, bdi->wb_cnt);
15203ba3782SJens Axboe 	BUG_ON(!bdi->wb_cnt);
15303ba3782SJens Axboe 
15403ba3782SJens Axboe 	/*
155deed62edSNick Piggin 	 * list_add_tail_rcu() contains the necessary barriers to
156deed62edSNick Piggin 	 * make sure the above stores are seen before the item is
157deed62edSNick Piggin 	 * noticed on the list
1581da177e4SLinus Torvalds 	 */
15903ba3782SJens Axboe 	spin_lock(&bdi->wb_lock);
16003ba3782SJens Axboe 	list_add_tail_rcu(&work->list, &bdi->work_list);
16103ba3782SJens Axboe 	spin_unlock(&bdi->wb_lock);
1621da177e4SLinus Torvalds 
1631da177e4SLinus Torvalds 	/*
16403ba3782SJens Axboe 	 * If the default thread isn't there, make sure we add it. When
16503ba3782SJens Axboe 	 * it gets created and wakes up, we'll run this work.
1661da177e4SLinus Torvalds 	 */
16703ba3782SJens Axboe 	if (unlikely(list_empty_careful(&bdi->wb_list)))
16803ba3782SJens Axboe 		wake_up_process(default_backing_dev_info.wb.task);
16903ba3782SJens Axboe 	else {
17003ba3782SJens Axboe 		struct bdi_writeback *wb = &bdi->wb;
1711da177e4SLinus Torvalds 
1721da177e4SLinus Torvalds 		/*
173bcddc3f0SJens Axboe 		 * End work now if this wb has no dirty IO pending. Otherwise
174bcddc3f0SJens Axboe 		 * wakeup the handling thread
1751da177e4SLinus Torvalds 		 */
176bcddc3f0SJens Axboe 		if (!wb_has_dirty_io(wb))
17703ba3782SJens Axboe 			wb_clear_pending(wb, work);
178bcddc3f0SJens Axboe 		else 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  */
253b6e51316SJens Axboe void bdi_start_writeback(struct backing_dev_info *bdi, long nr_pages)
254b6e51316SJens Axboe {
255b6e51316SJens Axboe 	struct wb_writeback_args args = {
256b6e51316SJens Axboe 		.sync_mode	= WB_SYNC_NONE,
257b6e51316SJens Axboe 		.nr_pages	= nr_pages,
258b6e51316SJens Axboe 		.range_cyclic	= 1,
259b6e51316SJens Axboe 	};
260b6e51316SJens Axboe 
261b6e51316SJens Axboe 	bdi_alloc_queue_work(bdi, &args);
2621da177e4SLinus Torvalds }
2631da177e4SLinus Torvalds 
2641da177e4SLinus Torvalds /*
2656610a0bcSAndrew Morton  * Redirty an inode: set its when-it-was dirtied timestamp and move it to the
2666610a0bcSAndrew Morton  * furthest end of its superblock's dirty-inode list.
2676610a0bcSAndrew Morton  *
2686610a0bcSAndrew Morton  * Before stamping the inode's ->dirtied_when, we check to see whether it is
26966f3b8e2SJens Axboe  * already the most-recently-dirtied inode on the b_dirty list.  If that is
2706610a0bcSAndrew Morton  * the case then the inode must have been redirtied while it was being written
2716610a0bcSAndrew Morton  * out and we don't reset its dirtied_when.
2726610a0bcSAndrew Morton  */
2736610a0bcSAndrew Morton static void redirty_tail(struct inode *inode)
2746610a0bcSAndrew Morton {
27503ba3782SJens Axboe 	struct bdi_writeback *wb = &inode_to_bdi(inode)->wb;
2766610a0bcSAndrew Morton 
27703ba3782SJens Axboe 	if (!list_empty(&wb->b_dirty)) {
27866f3b8e2SJens Axboe 		struct inode *tail;
2796610a0bcSAndrew Morton 
28003ba3782SJens Axboe 		tail = list_entry(wb->b_dirty.next, struct inode, i_list);
28166f3b8e2SJens Axboe 		if (time_before(inode->dirtied_when, tail->dirtied_when))
2826610a0bcSAndrew Morton 			inode->dirtied_when = jiffies;
2836610a0bcSAndrew Morton 	}
28403ba3782SJens Axboe 	list_move(&inode->i_list, &wb->b_dirty);
2856610a0bcSAndrew Morton }
2866610a0bcSAndrew Morton 
2876610a0bcSAndrew Morton /*
28866f3b8e2SJens Axboe  * requeue inode for re-scanning after bdi->b_io list is exhausted.
289c986d1e2SAndrew Morton  */
2900e0f4fc2SKen Chen static void requeue_io(struct inode *inode)
291c986d1e2SAndrew Morton {
29203ba3782SJens Axboe 	struct bdi_writeback *wb = &inode_to_bdi(inode)->wb;
29303ba3782SJens Axboe 
29403ba3782SJens Axboe 	list_move(&inode->i_list, &wb->b_more_io);
295c986d1e2SAndrew Morton }
296c986d1e2SAndrew Morton 
2971c0eeaf5SJoern Engel static void inode_sync_complete(struct inode *inode)
2981c0eeaf5SJoern Engel {
2991c0eeaf5SJoern Engel 	/*
3001c0eeaf5SJoern Engel 	 * Prevent speculative execution through spin_unlock(&inode_lock);
3011c0eeaf5SJoern Engel 	 */
3021c0eeaf5SJoern Engel 	smp_mb();
3031c0eeaf5SJoern Engel 	wake_up_bit(&inode->i_state, __I_SYNC);
3041c0eeaf5SJoern Engel }
3051c0eeaf5SJoern Engel 
306d2caa3c5SJeff Layton static bool inode_dirtied_after(struct inode *inode, unsigned long t)
307d2caa3c5SJeff Layton {
308d2caa3c5SJeff Layton 	bool ret = time_after(inode->dirtied_when, t);
309d2caa3c5SJeff Layton #ifndef CONFIG_64BIT
310d2caa3c5SJeff Layton 	/*
311d2caa3c5SJeff Layton 	 * For inodes being constantly redirtied, dirtied_when can get stuck.
312d2caa3c5SJeff Layton 	 * It _appears_ to be in the future, but is actually in distant past.
313d2caa3c5SJeff Layton 	 * This test is necessary to prevent such wrapped-around relative times
314d2caa3c5SJeff Layton 	 * from permanently stopping the whole pdflush writeback.
315d2caa3c5SJeff Layton 	 */
316d2caa3c5SJeff Layton 	ret = ret && time_before_eq(inode->dirtied_when, jiffies);
317d2caa3c5SJeff Layton #endif
318d2caa3c5SJeff Layton 	return ret;
319d2caa3c5SJeff Layton }
320d2caa3c5SJeff Layton 
321c986d1e2SAndrew Morton /*
3222c136579SFengguang Wu  * Move expired dirty inodes from @delaying_queue to @dispatch_queue.
3232c136579SFengguang Wu  */
3242c136579SFengguang Wu static void move_expired_inodes(struct list_head *delaying_queue,
3252c136579SFengguang Wu 			       struct list_head *dispatch_queue,
3262c136579SFengguang Wu 				unsigned long *older_than_this)
3272c136579SFengguang Wu {
3282c136579SFengguang Wu 	while (!list_empty(delaying_queue)) {
3292c136579SFengguang Wu 		struct inode *inode = list_entry(delaying_queue->prev,
3302c136579SFengguang Wu 						struct inode, i_list);
3312c136579SFengguang Wu 		if (older_than_this &&
332d2caa3c5SJeff Layton 		    inode_dirtied_after(inode, *older_than_this))
3332c136579SFengguang Wu 			break;
3342c136579SFengguang Wu 		list_move(&inode->i_list, dispatch_queue);
3352c136579SFengguang Wu 	}
3362c136579SFengguang Wu }
3372c136579SFengguang Wu 
3382c136579SFengguang Wu /*
3392c136579SFengguang Wu  * Queue all expired dirty inodes for io, eldest first.
3402c136579SFengguang Wu  */
34103ba3782SJens Axboe static void queue_io(struct bdi_writeback *wb, unsigned long *older_than_this)
3422c136579SFengguang Wu {
34303ba3782SJens Axboe 	list_splice_init(&wb->b_more_io, wb->b_io.prev);
34403ba3782SJens Axboe 	move_expired_inodes(&wb->b_dirty, &wb->b_io, older_than_this);
34566f3b8e2SJens Axboe }
34666f3b8e2SJens Axboe 
34703ba3782SJens Axboe static int write_inode(struct inode *inode, int sync)
34866f3b8e2SJens Axboe {
34903ba3782SJens Axboe 	if (inode->i_sb->s_op->write_inode && !is_bad_inode(inode))
35003ba3782SJens Axboe 		return inode->i_sb->s_op->write_inode(inode, sync);
35103ba3782SJens Axboe 	return 0;
35266f3b8e2SJens Axboe }
35308d8e974SFengguang Wu 
3542c136579SFengguang Wu /*
35501c03194SChristoph Hellwig  * Wait for writeback on an inode to complete.
35601c03194SChristoph Hellwig  */
35701c03194SChristoph Hellwig static void inode_wait_for_writeback(struct inode *inode)
35801c03194SChristoph Hellwig {
35901c03194SChristoph Hellwig 	DEFINE_WAIT_BIT(wq, &inode->i_state, __I_SYNC);
36001c03194SChristoph Hellwig 	wait_queue_head_t *wqh;
36101c03194SChristoph Hellwig 
36201c03194SChristoph Hellwig 	wqh = bit_waitqueue(&inode->i_state, __I_SYNC);
36301c03194SChristoph Hellwig 	do {
36401c03194SChristoph Hellwig 		spin_unlock(&inode_lock);
36501c03194SChristoph Hellwig 		__wait_on_bit(wqh, &wq, inode_wait, TASK_UNINTERRUPTIBLE);
36601c03194SChristoph Hellwig 		spin_lock(&inode_lock);
36701c03194SChristoph Hellwig 	} while (inode->i_state & I_SYNC);
36801c03194SChristoph Hellwig }
36901c03194SChristoph Hellwig 
37001c03194SChristoph Hellwig /*
37101c03194SChristoph Hellwig  * Write out an inode's dirty pages.  Called under inode_lock.  Either the
37201c03194SChristoph Hellwig  * caller has ref on the inode (either via __iget or via syscall against an fd)
37301c03194SChristoph Hellwig  * or the inode has I_WILL_FREE set (via generic_forget_inode)
37401c03194SChristoph Hellwig  *
3751da177e4SLinus Torvalds  * If `wait' is set, wait on the writeout.
3761da177e4SLinus Torvalds  *
3771da177e4SLinus Torvalds  * The whole writeout design is quite complex and fragile.  We want to avoid
3781da177e4SLinus Torvalds  * starvation of particular inodes when others are being redirtied, prevent
3791da177e4SLinus Torvalds  * livelocks, etc.
3801da177e4SLinus Torvalds  *
3811da177e4SLinus Torvalds  * Called under inode_lock.
3821da177e4SLinus Torvalds  */
3831da177e4SLinus Torvalds static int
38401c03194SChristoph Hellwig writeback_single_inode(struct inode *inode, struct writeback_control *wbc)
3851da177e4SLinus Torvalds {
3861da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
3871da177e4SLinus Torvalds 	int wait = wbc->sync_mode == WB_SYNC_ALL;
38801c03194SChristoph Hellwig 	unsigned dirty;
3891da177e4SLinus Torvalds 	int ret;
3901da177e4SLinus Torvalds 
39101c03194SChristoph Hellwig 	if (!atomic_read(&inode->i_count))
39201c03194SChristoph Hellwig 		WARN_ON(!(inode->i_state & (I_WILL_FREE|I_FREEING)));
39301c03194SChristoph Hellwig 	else
39401c03194SChristoph Hellwig 		WARN_ON(inode->i_state & I_WILL_FREE);
39501c03194SChristoph Hellwig 
39601c03194SChristoph Hellwig 	if (inode->i_state & I_SYNC) {
39701c03194SChristoph Hellwig 		/*
39801c03194SChristoph Hellwig 		 * If this inode is locked for writeback and we are not doing
39966f3b8e2SJens Axboe 		 * writeback-for-data-integrity, move it to b_more_io so that
40001c03194SChristoph Hellwig 		 * writeback can proceed with the other inodes on s_io.
40101c03194SChristoph Hellwig 		 *
40201c03194SChristoph Hellwig 		 * We'll have another go at writing back this inode when we
40366f3b8e2SJens Axboe 		 * completed a full scan of b_io.
40401c03194SChristoph Hellwig 		 */
40501c03194SChristoph Hellwig 		if (!wait) {
40601c03194SChristoph Hellwig 			requeue_io(inode);
40701c03194SChristoph Hellwig 			return 0;
40801c03194SChristoph Hellwig 		}
40901c03194SChristoph Hellwig 
41001c03194SChristoph Hellwig 		/*
41101c03194SChristoph Hellwig 		 * It's a data-integrity sync.  We must wait.
41201c03194SChristoph Hellwig 		 */
41301c03194SChristoph Hellwig 		inode_wait_for_writeback(inode);
41401c03194SChristoph Hellwig 	}
41501c03194SChristoph Hellwig 
4161c0eeaf5SJoern Engel 	BUG_ON(inode->i_state & I_SYNC);
4171da177e4SLinus Torvalds 
4181c0eeaf5SJoern Engel 	/* Set I_SYNC, reset I_DIRTY */
4191da177e4SLinus Torvalds 	dirty = inode->i_state & I_DIRTY;
4201c0eeaf5SJoern Engel 	inode->i_state |= I_SYNC;
4211da177e4SLinus Torvalds 	inode->i_state &= ~I_DIRTY;
4221da177e4SLinus Torvalds 
4231da177e4SLinus Torvalds 	spin_unlock(&inode_lock);
4241da177e4SLinus Torvalds 
4251da177e4SLinus Torvalds 	ret = do_writepages(mapping, wbc);
4261da177e4SLinus Torvalds 
4271da177e4SLinus Torvalds 	/* Don't write the inode if only I_DIRTY_PAGES was set */
4281da177e4SLinus Torvalds 	if (dirty & (I_DIRTY_SYNC | I_DIRTY_DATASYNC)) {
4291da177e4SLinus Torvalds 		int err = write_inode(inode, wait);
4301da177e4SLinus Torvalds 		if (ret == 0)
4311da177e4SLinus Torvalds 			ret = err;
4321da177e4SLinus Torvalds 	}
4331da177e4SLinus Torvalds 
4341da177e4SLinus Torvalds 	if (wait) {
4351da177e4SLinus Torvalds 		int err = filemap_fdatawait(mapping);
4361da177e4SLinus Torvalds 		if (ret == 0)
4371da177e4SLinus Torvalds 			ret = err;
4381da177e4SLinus Torvalds 	}
4391da177e4SLinus Torvalds 
4401da177e4SLinus Torvalds 	spin_lock(&inode_lock);
4411c0eeaf5SJoern Engel 	inode->i_state &= ~I_SYNC;
44284a89245SWu Fengguang 	if (!(inode->i_state & (I_FREEING | I_CLEAR))) {
4431da177e4SLinus Torvalds 		if (!(inode->i_state & I_DIRTY) &&
4441da177e4SLinus Torvalds 		    mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) {
4451da177e4SLinus Torvalds 			/*
4461da177e4SLinus Torvalds 			 * We didn't write back all the pages.  nfs_writepages()
4471da177e4SLinus Torvalds 			 * sometimes bales out without doing anything. Redirty
44866f3b8e2SJens Axboe 			 * the inode; Move it from b_io onto b_more_io/b_dirty.
4491b43ef91SAndrew Morton 			 */
4501b43ef91SAndrew Morton 			/*
4511b43ef91SAndrew Morton 			 * akpm: if the caller was the kupdate function we put
45266f3b8e2SJens Axboe 			 * this inode at the head of b_dirty so it gets first
4531b43ef91SAndrew Morton 			 * consideration.  Otherwise, move it to the tail, for
4541b43ef91SAndrew Morton 			 * the reasons described there.  I'm not really sure
4551b43ef91SAndrew Morton 			 * how much sense this makes.  Presumably I had a good
4561b43ef91SAndrew Morton 			 * reasons for doing it this way, and I'd rather not
4571b43ef91SAndrew Morton 			 * muck with it at present.
4581da177e4SLinus Torvalds 			 */
4591da177e4SLinus Torvalds 			if (wbc->for_kupdate) {
4601da177e4SLinus Torvalds 				/*
4612c136579SFengguang Wu 				 * For the kupdate function we move the inode
46266f3b8e2SJens Axboe 				 * to b_more_io so it will get more writeout as
4632c136579SFengguang Wu 				 * soon as the queue becomes uncongested.
4641da177e4SLinus Torvalds 				 */
4651da177e4SLinus Torvalds 				inode->i_state |= I_DIRTY_PAGES;
4668bc3be27SFengguang Wu 				if (wbc->nr_to_write <= 0) {
4678bc3be27SFengguang Wu 					/*
4688bc3be27SFengguang Wu 					 * slice used up: queue for next turn
4698bc3be27SFengguang Wu 					 */
4700e0f4fc2SKen Chen 					requeue_io(inode);
4711da177e4SLinus Torvalds 				} else {
4721da177e4SLinus Torvalds 					/*
4738bc3be27SFengguang Wu 					 * somehow blocked: retry later
4748bc3be27SFengguang Wu 					 */
4758bc3be27SFengguang Wu 					redirty_tail(inode);
4768bc3be27SFengguang Wu 				}
4778bc3be27SFengguang Wu 			} else {
4788bc3be27SFengguang Wu 				/*
4791da177e4SLinus Torvalds 				 * Otherwise fully redirty the inode so that
4801da177e4SLinus Torvalds 				 * other inodes on this superblock will get some
4811da177e4SLinus Torvalds 				 * writeout.  Otherwise heavy writing to one
4821da177e4SLinus Torvalds 				 * file would indefinitely suspend writeout of
4831da177e4SLinus Torvalds 				 * all the other files.
4841da177e4SLinus Torvalds 				 */
4851da177e4SLinus Torvalds 				inode->i_state |= I_DIRTY_PAGES;
4861b43ef91SAndrew Morton 				redirty_tail(inode);
4871da177e4SLinus Torvalds 			}
4881da177e4SLinus Torvalds 		} else if (inode->i_state & I_DIRTY) {
4891da177e4SLinus Torvalds 			/*
4901da177e4SLinus Torvalds 			 * Someone redirtied the inode while were writing back
4911da177e4SLinus Torvalds 			 * the pages.
4921da177e4SLinus Torvalds 			 */
4936610a0bcSAndrew Morton 			redirty_tail(inode);
4941da177e4SLinus Torvalds 		} else if (atomic_read(&inode->i_count)) {
4951da177e4SLinus Torvalds 			/*
4961da177e4SLinus Torvalds 			 * The inode is clean, inuse
4971da177e4SLinus Torvalds 			 */
4981da177e4SLinus Torvalds 			list_move(&inode->i_list, &inode_in_use);
4991da177e4SLinus Torvalds 		} else {
5001da177e4SLinus Torvalds 			/*
5011da177e4SLinus Torvalds 			 * The inode is clean, unused
5021da177e4SLinus Torvalds 			 */
5031da177e4SLinus Torvalds 			list_move(&inode->i_list, &inode_unused);
5041da177e4SLinus Torvalds 		}
5051da177e4SLinus Torvalds 	}
5061c0eeaf5SJoern Engel 	inode_sync_complete(inode);
5071da177e4SLinus Torvalds 	return ret;
5081da177e4SLinus Torvalds }
5091da177e4SLinus Torvalds 
51003ba3782SJens Axboe /*
51103ba3782SJens Axboe  * For WB_SYNC_NONE writeback, the caller does not have the sb pinned
51203ba3782SJens Axboe  * before calling writeback. So make sure that we do pin it, so it doesn't
51303ba3782SJens Axboe  * go away while we are writing inodes from it.
51403ba3782SJens Axboe  *
51503ba3782SJens Axboe  * Returns 0 if the super was successfully pinned (or pinning wasn't needed),
51603ba3782SJens Axboe  * 1 if we failed.
51703ba3782SJens Axboe  */
51803ba3782SJens Axboe static int pin_sb_for_writeback(struct writeback_control *wbc,
51903ba3782SJens Axboe 				   struct inode *inode)
5201da177e4SLinus Torvalds {
52103ba3782SJens Axboe 	struct super_block *sb = inode->i_sb;
52203ba3782SJens Axboe 
52303ba3782SJens Axboe 	/*
52403ba3782SJens Axboe 	 * Caller must already hold the ref for this
52503ba3782SJens Axboe 	 */
52603ba3782SJens Axboe 	if (wbc->sync_mode == WB_SYNC_ALL) {
52703ba3782SJens Axboe 		WARN_ON(!rwsem_is_locked(&sb->s_umount));
52803ba3782SJens Axboe 		return 0;
52903ba3782SJens Axboe 	}
53003ba3782SJens Axboe 
53103ba3782SJens Axboe 	spin_lock(&sb_lock);
53203ba3782SJens Axboe 	sb->s_count++;
53303ba3782SJens Axboe 	if (down_read_trylock(&sb->s_umount)) {
53403ba3782SJens Axboe 		if (sb->s_root) {
53503ba3782SJens Axboe 			spin_unlock(&sb_lock);
53603ba3782SJens Axboe 			return 0;
53703ba3782SJens Axboe 		}
53803ba3782SJens Axboe 		/*
53903ba3782SJens Axboe 		 * umounted, drop rwsem again and fall through to failure
54003ba3782SJens Axboe 		 */
54103ba3782SJens Axboe 		up_read(&sb->s_umount);
54203ba3782SJens Axboe 	}
54303ba3782SJens Axboe 
54403ba3782SJens Axboe 	sb->s_count--;
54503ba3782SJens Axboe 	spin_unlock(&sb_lock);
54603ba3782SJens Axboe 	return 1;
54703ba3782SJens Axboe }
54803ba3782SJens Axboe 
54903ba3782SJens Axboe static void unpin_sb_for_writeback(struct writeback_control *wbc,
55003ba3782SJens Axboe 				   struct inode *inode)
55103ba3782SJens Axboe {
55203ba3782SJens Axboe 	struct super_block *sb = inode->i_sb;
55303ba3782SJens Axboe 
55403ba3782SJens Axboe 	if (wbc->sync_mode == WB_SYNC_ALL)
55503ba3782SJens Axboe 		return;
55603ba3782SJens Axboe 
55703ba3782SJens Axboe 	up_read(&sb->s_umount);
55803ba3782SJens Axboe 	put_super(sb);
55903ba3782SJens Axboe }
56003ba3782SJens Axboe 
56103ba3782SJens Axboe static void writeback_inodes_wb(struct bdi_writeback *wb,
56203ba3782SJens Axboe 				struct writeback_control *wbc)
56303ba3782SJens Axboe {
56403ba3782SJens Axboe 	struct super_block *sb = wbc->sb;
56566f3b8e2SJens Axboe 	const int is_blkdev_sb = sb_is_blkdev_sb(sb);
5661da177e4SLinus Torvalds 	const unsigned long start = jiffies;	/* livelock avoidance */
5671da177e4SLinus Torvalds 
568ae8547b0SHans Reiser 	spin_lock(&inode_lock);
5691da177e4SLinus Torvalds 
57003ba3782SJens Axboe 	if (!wbc->for_kupdate || list_empty(&wb->b_io))
57103ba3782SJens Axboe 		queue_io(wb, wbc->older_than_this);
57266f3b8e2SJens Axboe 
57303ba3782SJens Axboe 	while (!list_empty(&wb->b_io)) {
57403ba3782SJens Axboe 		struct inode *inode = list_entry(wb->b_io.prev,
5751da177e4SLinus Torvalds 						struct inode, i_list);
5761da177e4SLinus Torvalds 		long pages_skipped;
5771da177e4SLinus Torvalds 
57866f3b8e2SJens Axboe 		/*
57966f3b8e2SJens Axboe 		 * super block given and doesn't match, skip this inode
58066f3b8e2SJens Axboe 		 */
58166f3b8e2SJens Axboe 		if (sb && sb != inode->i_sb) {
58266f3b8e2SJens Axboe 			redirty_tail(inode);
58366f3b8e2SJens Axboe 			continue;
58466f3b8e2SJens Axboe 		}
58566f3b8e2SJens Axboe 
58603ba3782SJens Axboe 		if (!bdi_cap_writeback_dirty(wb->bdi)) {
5879852a0e7SAndrew Morton 			redirty_tail(inode);
58866f3b8e2SJens Axboe 			if (is_blkdev_sb) {
5891da177e4SLinus Torvalds 				/*
5901da177e4SLinus Torvalds 				 * Dirty memory-backed blockdev: the ramdisk
5911da177e4SLinus Torvalds 				 * driver does this.  Skip just this inode
5921da177e4SLinus Torvalds 				 */
5931da177e4SLinus Torvalds 				continue;
5941da177e4SLinus Torvalds 			}
5951da177e4SLinus Torvalds 			/*
5961da177e4SLinus Torvalds 			 * Dirty memory-backed inode against a filesystem other
5971da177e4SLinus Torvalds 			 * than the kernel-internal bdev filesystem.  Skip the
5981da177e4SLinus Torvalds 			 * entire superblock.
5991da177e4SLinus Torvalds 			 */
6001da177e4SLinus Torvalds 			break;
6011da177e4SLinus Torvalds 		}
6021da177e4SLinus Torvalds 
60384a89245SWu Fengguang 		if (inode->i_state & (I_NEW | I_WILL_FREE)) {
6047ef0d737SNick Piggin 			requeue_io(inode);
6057ef0d737SNick Piggin 			continue;
6067ef0d737SNick Piggin 		}
6077ef0d737SNick Piggin 
60803ba3782SJens Axboe 		if (wbc->nonblocking && bdi_write_congested(wb->bdi)) {
6091da177e4SLinus Torvalds 			wbc->encountered_congestion = 1;
61066f3b8e2SJens Axboe 			if (!is_blkdev_sb)
6111da177e4SLinus Torvalds 				break;		/* Skip a congested fs */
6120e0f4fc2SKen Chen 			requeue_io(inode);
6131da177e4SLinus Torvalds 			continue;		/* Skip a congested blockdev */
6141da177e4SLinus Torvalds 		}
6151da177e4SLinus Torvalds 
616d2caa3c5SJeff Layton 		/*
617d2caa3c5SJeff Layton 		 * Was this inode dirtied after sync_sb_inodes was called?
618d2caa3c5SJeff Layton 		 * This keeps sync from extra jobs and livelock.
619d2caa3c5SJeff Layton 		 */
620d2caa3c5SJeff Layton 		if (inode_dirtied_after(inode, start))
6211da177e4SLinus Torvalds 			break;
6221da177e4SLinus Torvalds 
62303ba3782SJens Axboe 		if (pin_sb_for_writeback(wbc, inode)) {
62403ba3782SJens Axboe 			requeue_io(inode);
62503ba3782SJens Axboe 			continue;
62603ba3782SJens Axboe 		}
6271da177e4SLinus Torvalds 
62884a89245SWu Fengguang 		BUG_ON(inode->i_state & (I_FREEING | I_CLEAR));
6291da177e4SLinus Torvalds 		__iget(inode);
6301da177e4SLinus Torvalds 		pages_skipped = wbc->pages_skipped;
63101c03194SChristoph Hellwig 		writeback_single_inode(inode, wbc);
63203ba3782SJens Axboe 		unpin_sb_for_writeback(wbc, inode);
6331da177e4SLinus Torvalds 		if (wbc->pages_skipped != pages_skipped) {
6341da177e4SLinus Torvalds 			/*
6351da177e4SLinus Torvalds 			 * writeback is not making progress due to locked
6361da177e4SLinus Torvalds 			 * buffers.  Skip this inode for now.
6371da177e4SLinus Torvalds 			 */
638f57b9b7bSAndrew Morton 			redirty_tail(inode);
6391da177e4SLinus Torvalds 		}
6401da177e4SLinus Torvalds 		spin_unlock(&inode_lock);
6411da177e4SLinus Torvalds 		iput(inode);
6424ffc8444SOGAWA Hirofumi 		cond_resched();
6431da177e4SLinus Torvalds 		spin_lock(&inode_lock);
6448bc3be27SFengguang Wu 		if (wbc->nr_to_write <= 0) {
6458bc3be27SFengguang Wu 			wbc->more_io = 1;
6461da177e4SLinus Torvalds 			break;
6471da177e4SLinus Torvalds 		}
64803ba3782SJens Axboe 		if (!list_empty(&wb->b_more_io))
6498bc3be27SFengguang Wu 			wbc->more_io = 1;
6508bc3be27SFengguang Wu 	}
65138f21977SNick Piggin 
65266f3b8e2SJens Axboe 	spin_unlock(&inode_lock);
65366f3b8e2SJens Axboe 	/* Leave any unwritten inodes on b_io */
65466f3b8e2SJens Axboe }
65566f3b8e2SJens Axboe 
65603ba3782SJens Axboe void writeback_inodes_wbc(struct writeback_control *wbc)
65703ba3782SJens Axboe {
65803ba3782SJens Axboe 	struct backing_dev_info *bdi = wbc->bdi;
65903ba3782SJens Axboe 
66003ba3782SJens Axboe 	writeback_inodes_wb(&bdi->wb, wbc);
66103ba3782SJens Axboe }
66203ba3782SJens Axboe 
66303ba3782SJens Axboe /*
66403ba3782SJens Axboe  * The maximum number of pages to writeout in a single bdi flush/kupdate
66503ba3782SJens Axboe  * operation.  We do this so we don't hold I_SYNC against an inode for
66603ba3782SJens Axboe  * enormous amounts of time, which would block a userspace task which has
66703ba3782SJens Axboe  * been forced to throttle against that inode.  Also, the code reevaluates
66803ba3782SJens Axboe  * the dirty each time it has written this many pages.
66903ba3782SJens Axboe  */
67003ba3782SJens Axboe #define MAX_WRITEBACK_PAGES     1024
67103ba3782SJens Axboe 
67203ba3782SJens Axboe static inline bool over_bground_thresh(void)
67303ba3782SJens Axboe {
67403ba3782SJens Axboe 	unsigned long background_thresh, dirty_thresh;
67503ba3782SJens Axboe 
67603ba3782SJens Axboe 	get_dirty_limits(&background_thresh, &dirty_thresh, NULL, NULL);
67703ba3782SJens Axboe 
67803ba3782SJens Axboe 	return (global_page_state(NR_FILE_DIRTY) +
67903ba3782SJens Axboe 		global_page_state(NR_UNSTABLE_NFS) >= background_thresh);
68003ba3782SJens Axboe }
68103ba3782SJens Axboe 
68203ba3782SJens Axboe /*
68303ba3782SJens Axboe  * Explicit flushing or periodic writeback of "old" data.
68403ba3782SJens Axboe  *
68503ba3782SJens Axboe  * Define "old": the first time one of an inode's pages is dirtied, we mark the
68603ba3782SJens Axboe  * dirtying-time in the inode's address_space.  So this periodic writeback code
68703ba3782SJens Axboe  * just walks the superblock inode list, writing back any inodes which are
68803ba3782SJens Axboe  * older than a specific point in time.
68903ba3782SJens Axboe  *
69003ba3782SJens Axboe  * Try to run once per dirty_writeback_interval.  But if a writeback event
69103ba3782SJens Axboe  * takes longer than a dirty_writeback_interval interval, then leave a
69203ba3782SJens Axboe  * one-second gap.
69303ba3782SJens Axboe  *
69403ba3782SJens Axboe  * older_than_this takes precedence over nr_to_write.  So we'll only write back
69503ba3782SJens Axboe  * all dirty pages if they are all attached to "old" mappings.
69603ba3782SJens Axboe  */
697c4a77a6cSJens Axboe static long wb_writeback(struct bdi_writeback *wb,
698c4a77a6cSJens Axboe 			 struct wb_writeback_args *args)
69903ba3782SJens Axboe {
70003ba3782SJens Axboe 	struct writeback_control wbc = {
70103ba3782SJens Axboe 		.bdi			= wb->bdi,
702c4a77a6cSJens Axboe 		.sb			= args->sb,
703c4a77a6cSJens Axboe 		.sync_mode		= args->sync_mode,
70403ba3782SJens Axboe 		.older_than_this	= NULL,
705c4a77a6cSJens Axboe 		.for_kupdate		= args->for_kupdate,
706c4a77a6cSJens Axboe 		.range_cyclic		= args->range_cyclic,
70703ba3782SJens Axboe 	};
70803ba3782SJens Axboe 	unsigned long oldest_jif;
70903ba3782SJens Axboe 	long wrote = 0;
71003ba3782SJens Axboe 
71103ba3782SJens Axboe 	if (wbc.for_kupdate) {
71203ba3782SJens Axboe 		wbc.older_than_this = &oldest_jif;
71303ba3782SJens Axboe 		oldest_jif = jiffies -
71403ba3782SJens Axboe 				msecs_to_jiffies(dirty_expire_interval * 10);
71503ba3782SJens Axboe 	}
716c4a77a6cSJens Axboe 	if (!wbc.range_cyclic) {
717c4a77a6cSJens Axboe 		wbc.range_start = 0;
718c4a77a6cSJens Axboe 		wbc.range_end = LLONG_MAX;
719c4a77a6cSJens Axboe 	}
72003ba3782SJens Axboe 
72103ba3782SJens Axboe 	for (;;) {
72203ba3782SJens Axboe 		/*
72303ba3782SJens Axboe 		 * Don't flush anything for non-integrity writeback where
72403ba3782SJens Axboe 		 * no nr_pages was given
72503ba3782SJens Axboe 		 */
726c4a77a6cSJens Axboe 		if (!args->for_kupdate && args->nr_pages <= 0 &&
727c4a77a6cSJens Axboe 		     args->sync_mode == WB_SYNC_NONE)
72803ba3782SJens Axboe 			break;
72903ba3782SJens Axboe 
73003ba3782SJens Axboe 		/*
73103ba3782SJens Axboe 		 * If no specific pages were given and this is just a
73203ba3782SJens Axboe 		 * periodic background writeout and we are below the
73303ba3782SJens Axboe 		 * background dirty threshold, don't do anything
73403ba3782SJens Axboe 		 */
735c4a77a6cSJens Axboe 		if (args->for_kupdate && args->nr_pages <= 0 &&
736c4a77a6cSJens Axboe 		    !over_bground_thresh())
73703ba3782SJens Axboe 			break;
73803ba3782SJens Axboe 
73903ba3782SJens Axboe 		wbc.more_io = 0;
74003ba3782SJens Axboe 		wbc.encountered_congestion = 0;
74103ba3782SJens Axboe 		wbc.nr_to_write = MAX_WRITEBACK_PAGES;
74203ba3782SJens Axboe 		wbc.pages_skipped = 0;
74303ba3782SJens Axboe 		writeback_inodes_wb(wb, &wbc);
744c4a77a6cSJens Axboe 		args->nr_pages -= MAX_WRITEBACK_PAGES - wbc.nr_to_write;
74503ba3782SJens Axboe 		wrote += MAX_WRITEBACK_PAGES - wbc.nr_to_write;
74603ba3782SJens Axboe 
74703ba3782SJens Axboe 		/*
74803ba3782SJens Axboe 		 * If we ran out of stuff to write, bail unless more_io got set
74903ba3782SJens Axboe 		 */
75003ba3782SJens Axboe 		if (wbc.nr_to_write > 0 || wbc.pages_skipped > 0) {
75103ba3782SJens Axboe 			if (wbc.more_io && !wbc.for_kupdate)
75203ba3782SJens Axboe 				continue;
75303ba3782SJens Axboe 			break;
75403ba3782SJens Axboe 		}
75503ba3782SJens Axboe 	}
75603ba3782SJens Axboe 
75703ba3782SJens Axboe 	return wrote;
75803ba3782SJens Axboe }
75903ba3782SJens Axboe 
76003ba3782SJens Axboe /*
76103ba3782SJens Axboe  * Return the next bdi_work struct that hasn't been processed by this
7628010c3b6SJens Axboe  * wb thread yet. ->seen is initially set for each thread that exists
7638010c3b6SJens Axboe  * for this device, when a thread first notices a piece of work it
7648010c3b6SJens Axboe  * clears its bit. Depending on writeback type, the thread will notify
7658010c3b6SJens Axboe  * completion on either receiving the work (WB_SYNC_NONE) or after
7668010c3b6SJens Axboe  * it is done (WB_SYNC_ALL).
76703ba3782SJens Axboe  */
76803ba3782SJens Axboe static struct bdi_work *get_next_work_item(struct backing_dev_info *bdi,
76903ba3782SJens Axboe 					   struct bdi_writeback *wb)
77003ba3782SJens Axboe {
77103ba3782SJens Axboe 	struct bdi_work *work, *ret = NULL;
77203ba3782SJens Axboe 
77303ba3782SJens Axboe 	rcu_read_lock();
77403ba3782SJens Axboe 
77503ba3782SJens Axboe 	list_for_each_entry_rcu(work, &bdi->work_list, list) {
77677fad5e6SNick Piggin 		if (!test_bit(wb->nr, &work->seen))
77703ba3782SJens Axboe 			continue;
77877fad5e6SNick Piggin 		clear_bit(wb->nr, &work->seen);
77903ba3782SJens Axboe 
78003ba3782SJens Axboe 		ret = work;
78103ba3782SJens Axboe 		break;
78203ba3782SJens Axboe 	}
78303ba3782SJens Axboe 
78403ba3782SJens Axboe 	rcu_read_unlock();
78503ba3782SJens Axboe 	return ret;
78603ba3782SJens Axboe }
78703ba3782SJens Axboe 
78803ba3782SJens Axboe static long wb_check_old_data_flush(struct bdi_writeback *wb)
78903ba3782SJens Axboe {
79003ba3782SJens Axboe 	unsigned long expired;
79103ba3782SJens Axboe 	long nr_pages;
79203ba3782SJens Axboe 
79303ba3782SJens Axboe 	expired = wb->last_old_flush +
79403ba3782SJens Axboe 			msecs_to_jiffies(dirty_writeback_interval * 10);
79503ba3782SJens Axboe 	if (time_before(jiffies, expired))
79603ba3782SJens Axboe 		return 0;
79703ba3782SJens Axboe 
79803ba3782SJens Axboe 	wb->last_old_flush = jiffies;
79903ba3782SJens Axboe 	nr_pages = global_page_state(NR_FILE_DIRTY) +
80003ba3782SJens Axboe 			global_page_state(NR_UNSTABLE_NFS) +
80103ba3782SJens Axboe 			(inodes_stat.nr_inodes - inodes_stat.nr_unused);
80203ba3782SJens Axboe 
803c4a77a6cSJens Axboe 	if (nr_pages) {
804c4a77a6cSJens Axboe 		struct wb_writeback_args args = {
805c4a77a6cSJens Axboe 			.nr_pages	= nr_pages,
806c4a77a6cSJens Axboe 			.sync_mode	= WB_SYNC_NONE,
807c4a77a6cSJens Axboe 			.for_kupdate	= 1,
808c4a77a6cSJens Axboe 			.range_cyclic	= 1,
809c4a77a6cSJens Axboe 		};
810c4a77a6cSJens Axboe 
811c4a77a6cSJens Axboe 		return wb_writeback(wb, &args);
812c4a77a6cSJens Axboe 	}
81303ba3782SJens Axboe 
81403ba3782SJens Axboe 	return 0;
81503ba3782SJens Axboe }
81603ba3782SJens Axboe 
81703ba3782SJens Axboe /*
81803ba3782SJens Axboe  * Retrieve work items and do the writeback they describe
81903ba3782SJens Axboe  */
82003ba3782SJens Axboe long wb_do_writeback(struct bdi_writeback *wb, int force_wait)
82103ba3782SJens Axboe {
82203ba3782SJens Axboe 	struct backing_dev_info *bdi = wb->bdi;
82303ba3782SJens Axboe 	struct bdi_work *work;
824c4a77a6cSJens Axboe 	long wrote = 0;
82503ba3782SJens Axboe 
82603ba3782SJens Axboe 	while ((work = get_next_work_item(bdi, wb)) != NULL) {
827c4a77a6cSJens Axboe 		struct wb_writeback_args args = work->args;
82803ba3782SJens Axboe 
82903ba3782SJens Axboe 		/*
83003ba3782SJens Axboe 		 * Override sync mode, in case we must wait for completion
83103ba3782SJens Axboe 		 */
83203ba3782SJens Axboe 		if (force_wait)
833c4a77a6cSJens Axboe 			work->args.sync_mode = args.sync_mode = WB_SYNC_ALL;
83403ba3782SJens Axboe 
83503ba3782SJens Axboe 		/*
83603ba3782SJens Axboe 		 * If this isn't a data integrity operation, just notify
83703ba3782SJens Axboe 		 * that we have seen this work and we are now starting it.
83803ba3782SJens Axboe 		 */
839c4a77a6cSJens Axboe 		if (args.sync_mode == WB_SYNC_NONE)
84003ba3782SJens Axboe 			wb_clear_pending(wb, work);
84103ba3782SJens Axboe 
842c4a77a6cSJens Axboe 		wrote += wb_writeback(wb, &args);
84303ba3782SJens Axboe 
84403ba3782SJens Axboe 		/*
84503ba3782SJens Axboe 		 * This is a data integrity writeback, so only do the
84603ba3782SJens Axboe 		 * notification when we have completed the work.
84703ba3782SJens Axboe 		 */
848c4a77a6cSJens Axboe 		if (args.sync_mode == WB_SYNC_ALL)
84903ba3782SJens Axboe 			wb_clear_pending(wb, work);
85003ba3782SJens Axboe 	}
85103ba3782SJens Axboe 
85203ba3782SJens Axboe 	/*
85303ba3782SJens Axboe 	 * Check for periodic writeback, kupdated() style
85403ba3782SJens Axboe 	 */
85503ba3782SJens Axboe 	wrote += wb_check_old_data_flush(wb);
85603ba3782SJens Axboe 
85703ba3782SJens Axboe 	return wrote;
85803ba3782SJens Axboe }
85903ba3782SJens Axboe 
86003ba3782SJens Axboe /*
86103ba3782SJens Axboe  * Handle writeback of dirty data for the device backed by this bdi. Also
86203ba3782SJens Axboe  * wakes up periodically and does kupdated style flushing.
86303ba3782SJens Axboe  */
86403ba3782SJens Axboe int bdi_writeback_task(struct bdi_writeback *wb)
86503ba3782SJens Axboe {
86603ba3782SJens Axboe 	unsigned long last_active = jiffies;
86703ba3782SJens Axboe 	unsigned long wait_jiffies = -1UL;
86803ba3782SJens Axboe 	long pages_written;
86903ba3782SJens Axboe 
87003ba3782SJens Axboe 	while (!kthread_should_stop()) {
87103ba3782SJens Axboe 		pages_written = wb_do_writeback(wb, 0);
87203ba3782SJens Axboe 
87303ba3782SJens Axboe 		if (pages_written)
87403ba3782SJens Axboe 			last_active = jiffies;
87503ba3782SJens Axboe 		else if (wait_jiffies != -1UL) {
87603ba3782SJens Axboe 			unsigned long max_idle;
87703ba3782SJens Axboe 
87803ba3782SJens Axboe 			/*
87903ba3782SJens Axboe 			 * Longest period of inactivity that we tolerate. If we
88003ba3782SJens Axboe 			 * see dirty data again later, the task will get
88103ba3782SJens Axboe 			 * recreated automatically.
88203ba3782SJens Axboe 			 */
88303ba3782SJens Axboe 			max_idle = max(5UL * 60 * HZ, wait_jiffies);
88403ba3782SJens Axboe 			if (time_after(jiffies, max_idle + last_active))
88503ba3782SJens Axboe 				break;
88603ba3782SJens Axboe 		}
88703ba3782SJens Axboe 
88803ba3782SJens Axboe 		wait_jiffies = msecs_to_jiffies(dirty_writeback_interval * 10);
88949db0414SJens Axboe 		schedule_timeout_interruptible(wait_jiffies);
89003ba3782SJens Axboe 		try_to_freeze();
89103ba3782SJens Axboe 	}
89203ba3782SJens Axboe 
89303ba3782SJens Axboe 	return 0;
89403ba3782SJens Axboe }
89503ba3782SJens Axboe 
89603ba3782SJens Axboe /*
897b6e51316SJens Axboe  * Schedule writeback for all backing devices. This does WB_SYNC_NONE
898b6e51316SJens Axboe  * writeback, for integrity writeback see bdi_sync_writeback().
89903ba3782SJens Axboe  */
900b6e51316SJens Axboe static void bdi_writeback_all(struct super_block *sb, long nr_pages)
90103ba3782SJens Axboe {
902b6e51316SJens Axboe 	struct wb_writeback_args args = {
903b6e51316SJens Axboe 		.sb		= sb,
904b6e51316SJens Axboe 		.nr_pages	= nr_pages,
905b6e51316SJens Axboe 		.sync_mode	= WB_SYNC_NONE,
906b6e51316SJens Axboe 	};
90703ba3782SJens Axboe 	struct backing_dev_info *bdi;
90803ba3782SJens Axboe 
909cfc4ba53SJens Axboe 	rcu_read_lock();
91003ba3782SJens Axboe 
911cfc4ba53SJens Axboe 	list_for_each_entry_rcu(bdi, &bdi_list, bdi_list) {
91203ba3782SJens Axboe 		if (!bdi_has_dirty_io(bdi))
91303ba3782SJens Axboe 			continue;
91403ba3782SJens Axboe 
915b6e51316SJens Axboe 		bdi_alloc_queue_work(bdi, &args);
91603ba3782SJens Axboe 	}
91703ba3782SJens Axboe 
918cfc4ba53SJens Axboe 	rcu_read_unlock();
91903ba3782SJens Axboe }
92003ba3782SJens Axboe 
92103ba3782SJens Axboe /*
92203ba3782SJens Axboe  * Start writeback of `nr_pages' pages.  If `nr_pages' is zero, write back
92303ba3782SJens Axboe  * the whole world.
92403ba3782SJens Axboe  */
92503ba3782SJens Axboe void wakeup_flusher_threads(long nr_pages)
92603ba3782SJens Axboe {
92703ba3782SJens Axboe 	if (nr_pages == 0)
92803ba3782SJens Axboe 		nr_pages = global_page_state(NR_FILE_DIRTY) +
92903ba3782SJens Axboe 				global_page_state(NR_UNSTABLE_NFS);
930b6e51316SJens Axboe 	bdi_writeback_all(NULL, nr_pages);
93103ba3782SJens Axboe }
93203ba3782SJens Axboe 
93303ba3782SJens Axboe static noinline void block_dump___mark_inode_dirty(struct inode *inode)
93403ba3782SJens Axboe {
93503ba3782SJens Axboe 	if (inode->i_ino || strcmp(inode->i_sb->s_id, "bdev")) {
93603ba3782SJens Axboe 		struct dentry *dentry;
93703ba3782SJens Axboe 		const char *name = "?";
93803ba3782SJens Axboe 
93903ba3782SJens Axboe 		dentry = d_find_alias(inode);
94003ba3782SJens Axboe 		if (dentry) {
94103ba3782SJens Axboe 			spin_lock(&dentry->d_lock);
94203ba3782SJens Axboe 			name = (const char *) dentry->d_name.name;
94303ba3782SJens Axboe 		}
94403ba3782SJens Axboe 		printk(KERN_DEBUG
94503ba3782SJens Axboe 		       "%s(%d): dirtied inode %lu (%s) on %s\n",
94603ba3782SJens Axboe 		       current->comm, task_pid_nr(current), inode->i_ino,
94703ba3782SJens Axboe 		       name, inode->i_sb->s_id);
94803ba3782SJens Axboe 		if (dentry) {
94903ba3782SJens Axboe 			spin_unlock(&dentry->d_lock);
95003ba3782SJens Axboe 			dput(dentry);
95103ba3782SJens Axboe 		}
95203ba3782SJens Axboe 	}
95303ba3782SJens Axboe }
95403ba3782SJens Axboe 
95503ba3782SJens Axboe /**
95603ba3782SJens Axboe  *	__mark_inode_dirty -	internal function
95703ba3782SJens Axboe  *	@inode: inode to mark
95803ba3782SJens Axboe  *	@flags: what kind of dirty (i.e. I_DIRTY_SYNC)
95903ba3782SJens Axboe  *	Mark an inode as dirty. Callers should use mark_inode_dirty or
96003ba3782SJens Axboe  *  	mark_inode_dirty_sync.
96103ba3782SJens Axboe  *
96203ba3782SJens Axboe  * Put the inode on the super block's dirty list.
96303ba3782SJens Axboe  *
96403ba3782SJens Axboe  * CAREFUL! We mark it dirty unconditionally, but move it onto the
96503ba3782SJens Axboe  * dirty list only if it is hashed or if it refers to a blockdev.
96603ba3782SJens Axboe  * If it was not hashed, it will never be added to the dirty list
96703ba3782SJens Axboe  * even if it is later hashed, as it will have been marked dirty already.
96803ba3782SJens Axboe  *
96903ba3782SJens Axboe  * In short, make sure you hash any inodes _before_ you start marking
97003ba3782SJens Axboe  * them dirty.
97103ba3782SJens Axboe  *
97203ba3782SJens Axboe  * This function *must* be atomic for the I_DIRTY_PAGES case -
97303ba3782SJens Axboe  * set_page_dirty() is called under spinlock in several places.
97403ba3782SJens Axboe  *
97503ba3782SJens Axboe  * Note that for blockdevs, inode->dirtied_when represents the dirtying time of
97603ba3782SJens Axboe  * the block-special inode (/dev/hda1) itself.  And the ->dirtied_when field of
97703ba3782SJens Axboe  * the kernel-internal blockdev inode represents the dirtying time of the
97803ba3782SJens Axboe  * blockdev's pages.  This is why for I_DIRTY_PAGES we always use
97903ba3782SJens Axboe  * page->mapping->host, so the page-dirtying time is recorded in the internal
98003ba3782SJens Axboe  * blockdev inode.
98103ba3782SJens Axboe  */
98203ba3782SJens Axboe void __mark_inode_dirty(struct inode *inode, int flags)
98303ba3782SJens Axboe {
98403ba3782SJens Axboe 	struct super_block *sb = inode->i_sb;
98503ba3782SJens Axboe 
98603ba3782SJens Axboe 	/*
98703ba3782SJens Axboe 	 * Don't do this for I_DIRTY_PAGES - that doesn't actually
98803ba3782SJens Axboe 	 * dirty the inode itself
98903ba3782SJens Axboe 	 */
99003ba3782SJens Axboe 	if (flags & (I_DIRTY_SYNC | I_DIRTY_DATASYNC)) {
99103ba3782SJens Axboe 		if (sb->s_op->dirty_inode)
99203ba3782SJens Axboe 			sb->s_op->dirty_inode(inode);
99303ba3782SJens Axboe 	}
99403ba3782SJens Axboe 
99503ba3782SJens Axboe 	/*
99603ba3782SJens Axboe 	 * make sure that changes are seen by all cpus before we test i_state
99703ba3782SJens Axboe 	 * -- mikulas
99803ba3782SJens Axboe 	 */
99903ba3782SJens Axboe 	smp_mb();
100003ba3782SJens Axboe 
100103ba3782SJens Axboe 	/* avoid the locking if we can */
100203ba3782SJens Axboe 	if ((inode->i_state & flags) == flags)
100303ba3782SJens Axboe 		return;
100403ba3782SJens Axboe 
100503ba3782SJens Axboe 	if (unlikely(block_dump))
100603ba3782SJens Axboe 		block_dump___mark_inode_dirty(inode);
100703ba3782SJens Axboe 
100803ba3782SJens Axboe 	spin_lock(&inode_lock);
100903ba3782SJens Axboe 	if ((inode->i_state & flags) != flags) {
101003ba3782SJens Axboe 		const int was_dirty = inode->i_state & I_DIRTY;
101103ba3782SJens Axboe 
101203ba3782SJens Axboe 		inode->i_state |= flags;
101303ba3782SJens Axboe 
101403ba3782SJens Axboe 		/*
101503ba3782SJens Axboe 		 * If the inode is being synced, just update its dirty state.
101603ba3782SJens Axboe 		 * The unlocker will place the inode on the appropriate
101703ba3782SJens Axboe 		 * superblock list, based upon its state.
101803ba3782SJens Axboe 		 */
101903ba3782SJens Axboe 		if (inode->i_state & I_SYNC)
102003ba3782SJens Axboe 			goto out;
102103ba3782SJens Axboe 
102203ba3782SJens Axboe 		/*
102303ba3782SJens Axboe 		 * Only add valid (hashed) inodes to the superblock's
102403ba3782SJens Axboe 		 * dirty list.  Add blockdev inodes as well.
102503ba3782SJens Axboe 		 */
102603ba3782SJens Axboe 		if (!S_ISBLK(inode->i_mode)) {
102703ba3782SJens Axboe 			if (hlist_unhashed(&inode->i_hash))
102803ba3782SJens Axboe 				goto out;
102903ba3782SJens Axboe 		}
103003ba3782SJens Axboe 		if (inode->i_state & (I_FREEING|I_CLEAR))
103103ba3782SJens Axboe 			goto out;
103203ba3782SJens Axboe 
103303ba3782SJens Axboe 		/*
103403ba3782SJens Axboe 		 * If the inode was already on b_dirty/b_io/b_more_io, don't
103503ba3782SJens Axboe 		 * reposition it (that would break b_dirty time-ordering).
103603ba3782SJens Axboe 		 */
103703ba3782SJens Axboe 		if (!was_dirty) {
103803ba3782SJens Axboe 			struct bdi_writeback *wb = &inode_to_bdi(inode)->wb;
1039500b067cSJens Axboe 			struct backing_dev_info *bdi = wb->bdi;
1040500b067cSJens Axboe 
1041500b067cSJens Axboe 			if (bdi_cap_writeback_dirty(bdi) &&
1042500b067cSJens Axboe 			    !test_bit(BDI_registered, &bdi->state)) {
1043500b067cSJens Axboe 				WARN_ON(1);
1044500b067cSJens Axboe 				printk(KERN_ERR "bdi-%s not registered\n",
1045500b067cSJens Axboe 								bdi->name);
1046500b067cSJens Axboe 			}
104703ba3782SJens Axboe 
104803ba3782SJens Axboe 			inode->dirtied_when = jiffies;
104903ba3782SJens Axboe 			list_move(&inode->i_list, &wb->b_dirty);
105003ba3782SJens Axboe 		}
105103ba3782SJens Axboe 	}
105203ba3782SJens Axboe out:
105303ba3782SJens Axboe 	spin_unlock(&inode_lock);
105403ba3782SJens Axboe }
105503ba3782SJens Axboe EXPORT_SYMBOL(__mark_inode_dirty);
105603ba3782SJens Axboe 
105766f3b8e2SJens Axboe /*
105866f3b8e2SJens Axboe  * Write out a superblock's list of dirty inodes.  A wait will be performed
105966f3b8e2SJens Axboe  * upon no inodes, all inodes or the final one, depending upon sync_mode.
106066f3b8e2SJens Axboe  *
106166f3b8e2SJens Axboe  * If older_than_this is non-NULL, then only write out inodes which
106266f3b8e2SJens Axboe  * had their first dirtying at a time earlier than *older_than_this.
106366f3b8e2SJens Axboe  *
106466f3b8e2SJens Axboe  * If we're a pdlfush thread, then implement pdflush collision avoidance
106566f3b8e2SJens Axboe  * against the entire list.
106666f3b8e2SJens Axboe  *
106766f3b8e2SJens Axboe  * If `bdi' is non-zero then we're being asked to writeback a specific queue.
106866f3b8e2SJens Axboe  * This function assumes that the blockdev superblock's inodes are backed by
106966f3b8e2SJens Axboe  * a variety of queues, so all inodes are searched.  For other superblocks,
107066f3b8e2SJens Axboe  * assume that all inodes are backed by the same queue.
107166f3b8e2SJens Axboe  *
107266f3b8e2SJens Axboe  * The inodes to be written are parked on bdi->b_io.  They are moved back onto
107366f3b8e2SJens Axboe  * bdi->b_dirty as they are selected for writing.  This way, none can be missed
107466f3b8e2SJens Axboe  * on the writer throttling path, and we get decent balancing between many
107566f3b8e2SJens Axboe  * throttled threads: we don't want them all piling up on inode_sync_wait.
107666f3b8e2SJens Axboe  */
1077b6e51316SJens Axboe static void wait_sb_inodes(struct super_block *sb)
107866f3b8e2SJens Axboe {
107938f21977SNick Piggin 	struct inode *inode, *old_inode = NULL;
108038f21977SNick Piggin 
108103ba3782SJens Axboe 	/*
108203ba3782SJens Axboe 	 * We need to be protected against the filesystem going from
108303ba3782SJens Axboe 	 * r/o to r/w or vice versa.
108403ba3782SJens Axboe 	 */
1085b6e51316SJens Axboe 	WARN_ON(!rwsem_is_locked(&sb->s_umount));
108603ba3782SJens Axboe 
108766f3b8e2SJens Axboe 	spin_lock(&inode_lock);
108866f3b8e2SJens Axboe 
108938f21977SNick Piggin 	/*
109038f21977SNick Piggin 	 * Data integrity sync. Must wait for all pages under writeback,
109138f21977SNick Piggin 	 * because there may have been pages dirtied before our sync
109238f21977SNick Piggin 	 * call, but which had writeout started before we write it out.
109338f21977SNick Piggin 	 * In which case, the inode may not be on the dirty list, but
109438f21977SNick Piggin 	 * we still have to wait for that writeout.
109538f21977SNick Piggin 	 */
1096b6e51316SJens Axboe 	list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
109738f21977SNick Piggin 		struct address_space *mapping;
109838f21977SNick Piggin 
109903ba3782SJens Axboe 		if (inode->i_state & (I_FREEING|I_CLEAR|I_WILL_FREE|I_NEW))
110038f21977SNick Piggin 			continue;
110138f21977SNick Piggin 		mapping = inode->i_mapping;
110238f21977SNick Piggin 		if (mapping->nrpages == 0)
110338f21977SNick Piggin 			continue;
110438f21977SNick Piggin 		__iget(inode);
1105ae8547b0SHans Reiser 		spin_unlock(&inode_lock);
110638f21977SNick Piggin 		/*
110738f21977SNick Piggin 		 * We hold a reference to 'inode' so it couldn't have
110838f21977SNick Piggin 		 * been removed from s_inodes list while we dropped the
110938f21977SNick Piggin 		 * inode_lock.  We cannot iput the inode now as we can
111038f21977SNick Piggin 		 * be holding the last reference and we cannot iput it
111138f21977SNick Piggin 		 * under inode_lock. So we keep the reference and iput
111238f21977SNick Piggin 		 * it later.
111338f21977SNick Piggin 		 */
111438f21977SNick Piggin 		iput(old_inode);
111538f21977SNick Piggin 		old_inode = inode;
111638f21977SNick Piggin 
111738f21977SNick Piggin 		filemap_fdatawait(mapping);
111838f21977SNick Piggin 
111938f21977SNick Piggin 		cond_resched();
112038f21977SNick Piggin 
112138f21977SNick Piggin 		spin_lock(&inode_lock);
112238f21977SNick Piggin 	}
112338f21977SNick Piggin 	spin_unlock(&inode_lock);
112438f21977SNick Piggin 	iput(old_inode);
112566f3b8e2SJens Axboe }
11261da177e4SLinus Torvalds 
1127d8a8559cSJens Axboe /**
1128d8a8559cSJens Axboe  * writeback_inodes_sb	-	writeback dirty inodes from given super_block
1129d8a8559cSJens Axboe  * @sb: the superblock
11301da177e4SLinus Torvalds  *
1131d8a8559cSJens Axboe  * Start writeback on some inodes on this super_block. No guarantees are made
1132d8a8559cSJens Axboe  * on how many (if any) will be written, and this function does not wait
1133d8a8559cSJens Axboe  * for IO completion of submitted IO. The number of pages submitted is
1134d8a8559cSJens Axboe  * returned.
11351da177e4SLinus Torvalds  */
1136b6e51316SJens Axboe void writeback_inodes_sb(struct super_block *sb)
11371da177e4SLinus Torvalds {
1138b1e7a8fdSChristoph Lameter 	unsigned long nr_dirty = global_page_state(NR_FILE_DIRTY);
1139fd39fc85SChristoph Lameter 	unsigned long nr_unstable = global_page_state(NR_UNSTABLE_NFS);
1140d8a8559cSJens Axboe 	long nr_to_write;
11411da177e4SLinus Torvalds 
1142d8a8559cSJens Axboe 	nr_to_write = nr_dirty + nr_unstable +
114338f21977SNick Piggin 			(inodes_stat.nr_inodes - inodes_stat.nr_unused);
114438f21977SNick Piggin 
1145b6e51316SJens Axboe 	bdi_writeback_all(sb, nr_to_write);
11461da177e4SLinus Torvalds }
1147d8a8559cSJens Axboe EXPORT_SYMBOL(writeback_inodes_sb);
1148d8a8559cSJens Axboe 
1149d8a8559cSJens Axboe /**
1150d8a8559cSJens Axboe  * sync_inodes_sb	-	sync sb inode pages
1151d8a8559cSJens Axboe  * @sb: the superblock
1152d8a8559cSJens Axboe  *
1153d8a8559cSJens Axboe  * This function writes and waits on any dirty inode belonging to this
1154d8a8559cSJens Axboe  * super_block. The number of pages synced is returned.
1155d8a8559cSJens Axboe  */
1156b6e51316SJens Axboe void sync_inodes_sb(struct super_block *sb)
1157d8a8559cSJens Axboe {
1158b6e51316SJens Axboe 	bdi_sync_writeback(sb->s_bdi, sb);
1159b6e51316SJens Axboe 	wait_sb_inodes(sb);
1160d8a8559cSJens Axboe }
1161d8a8559cSJens Axboe EXPORT_SYMBOL(sync_inodes_sb);
11621da177e4SLinus Torvalds 
11631da177e4SLinus Torvalds /**
11641da177e4SLinus Torvalds  * write_inode_now	-	write an inode to disk
11651da177e4SLinus Torvalds  * @inode: inode to write to disk
11661da177e4SLinus Torvalds  * @sync: whether the write should be synchronous or not
11671da177e4SLinus Torvalds  *
11687f04c26dSAndrea Arcangeli  * This function commits an inode to disk immediately if it is dirty. This is
11697f04c26dSAndrea Arcangeli  * primarily needed by knfsd.
11707f04c26dSAndrea Arcangeli  *
11717f04c26dSAndrea Arcangeli  * The caller must either have a ref on the inode or must have set I_WILL_FREE.
11721da177e4SLinus Torvalds  */
11731da177e4SLinus Torvalds int write_inode_now(struct inode *inode, int sync)
11741da177e4SLinus Torvalds {
11751da177e4SLinus Torvalds 	int ret;
11761da177e4SLinus Torvalds 	struct writeback_control wbc = {
11771da177e4SLinus Torvalds 		.nr_to_write = LONG_MAX,
117818914b18SMike Galbraith 		.sync_mode = sync ? WB_SYNC_ALL : WB_SYNC_NONE,
1179111ebb6eSOGAWA Hirofumi 		.range_start = 0,
1180111ebb6eSOGAWA Hirofumi 		.range_end = LLONG_MAX,
11811da177e4SLinus Torvalds 	};
11821da177e4SLinus Torvalds 
11831da177e4SLinus Torvalds 	if (!mapping_cap_writeback_dirty(inode->i_mapping))
118449364ce2SAndrew Morton 		wbc.nr_to_write = 0;
11851da177e4SLinus Torvalds 
11861da177e4SLinus Torvalds 	might_sleep();
11871da177e4SLinus Torvalds 	spin_lock(&inode_lock);
118801c03194SChristoph Hellwig 	ret = writeback_single_inode(inode, &wbc);
11891da177e4SLinus Torvalds 	spin_unlock(&inode_lock);
11901da177e4SLinus Torvalds 	if (sync)
11911c0eeaf5SJoern Engel 		inode_sync_wait(inode);
11921da177e4SLinus Torvalds 	return ret;
11931da177e4SLinus Torvalds }
11941da177e4SLinus Torvalds EXPORT_SYMBOL(write_inode_now);
11951da177e4SLinus Torvalds 
11961da177e4SLinus Torvalds /**
11971da177e4SLinus Torvalds  * sync_inode - write an inode and its pages to disk.
11981da177e4SLinus Torvalds  * @inode: the inode to sync
11991da177e4SLinus Torvalds  * @wbc: controls the writeback mode
12001da177e4SLinus Torvalds  *
12011da177e4SLinus Torvalds  * sync_inode() will write an inode and its pages to disk.  It will also
12021da177e4SLinus Torvalds  * correctly update the inode on its superblock's dirty inode lists and will
12031da177e4SLinus Torvalds  * update inode->i_state.
12041da177e4SLinus Torvalds  *
12051da177e4SLinus Torvalds  * The caller must have a ref on the inode.
12061da177e4SLinus Torvalds  */
12071da177e4SLinus Torvalds int sync_inode(struct inode *inode, struct writeback_control *wbc)
12081da177e4SLinus Torvalds {
12091da177e4SLinus Torvalds 	int ret;
12101da177e4SLinus Torvalds 
12111da177e4SLinus Torvalds 	spin_lock(&inode_lock);
121201c03194SChristoph Hellwig 	ret = writeback_single_inode(inode, wbc);
12131da177e4SLinus Torvalds 	spin_unlock(&inode_lock);
12141da177e4SLinus Torvalds 	return ret;
12151da177e4SLinus Torvalds }
12161da177e4SLinus Torvalds EXPORT_SYMBOL(sync_inode);
1217