xref: /openbmc/linux/fs/fs-writeback.c (revision 8a1270cd)
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>
17630d9c47SPaul Gortmaker #include <linux/export.h>
181da177e4SLinus Torvalds #include <linux/spinlock.h>
195a0e3ad6STejun Heo #include <linux/slab.h>
201da177e4SLinus Torvalds #include <linux/sched.h>
211da177e4SLinus Torvalds #include <linux/fs.h>
221da177e4SLinus Torvalds #include <linux/mm.h>
23bc31b86aSWu Fengguang #include <linux/pagemap.h>
2403ba3782SJens Axboe #include <linux/kthread.h>
251da177e4SLinus Torvalds #include <linux/writeback.h>
261da177e4SLinus Torvalds #include <linux/blkdev.h>
271da177e4SLinus Torvalds #include <linux/backing-dev.h>
28455b2864SDave Chinner #include <linux/tracepoint.h>
29719ea2fbSAl Viro #include <linux/device.h>
3021c6321fSTejun Heo #include <linux/memcontrol.h>
3107f3f05cSDavid Howells #include "internal.h"
321da177e4SLinus Torvalds 
33d0bceac7SJens Axboe /*
34bc31b86aSWu Fengguang  * 4MB minimal write chunk size
35bc31b86aSWu Fengguang  */
36bc31b86aSWu Fengguang #define MIN_WRITEBACK_PAGES	(4096UL >> (PAGE_CACHE_SHIFT - 10))
37bc31b86aSWu Fengguang 
38cc395d7fSTejun Heo struct wb_completion {
39cc395d7fSTejun Heo 	atomic_t		cnt;
40cc395d7fSTejun Heo };
41cc395d7fSTejun Heo 
42bc31b86aSWu Fengguang /*
43c4a77a6cSJens Axboe  * Passed into wb_writeback(), essentially a subset of writeback_control
44c4a77a6cSJens Axboe  */
4583ba7b07SChristoph Hellwig struct wb_writeback_work {
46c4a77a6cSJens Axboe 	long nr_pages;
47c4a77a6cSJens Axboe 	struct super_block *sb;
480dc83bd3SJan Kara 	unsigned long *older_than_this;
49c4a77a6cSJens Axboe 	enum writeback_sync_modes sync_mode;
506e6938b6SWu Fengguang 	unsigned int tagged_writepages:1;
5152957fe1SH Hartley Sweeten 	unsigned int for_kupdate:1;
5252957fe1SH Hartley Sweeten 	unsigned int range_cyclic:1;
5352957fe1SH Hartley Sweeten 	unsigned int for_background:1;
547747bd4bSDave Chinner 	unsigned int for_sync:1;	/* sync(2) WB_SYNC_ALL writeback */
55ac7b19a3STejun Heo 	unsigned int auto_free:1;	/* free on completion */
560e175a18SCurt Wohlgemuth 	enum wb_reason reason;		/* why was writeback initiated? */
57c4a77a6cSJens Axboe 
588010c3b6SJens Axboe 	struct list_head list;		/* pending work list */
59cc395d7fSTejun Heo 	struct wb_completion *done;	/* set if the caller waits */
6003ba3782SJens Axboe };
6103ba3782SJens Axboe 
62a2f48706STheodore Ts'o /*
63cc395d7fSTejun Heo  * If one wants to wait for one or more wb_writeback_works, each work's
64cc395d7fSTejun Heo  * ->done should be set to a wb_completion defined using the following
65cc395d7fSTejun Heo  * macro.  Once all work items are issued with wb_queue_work(), the caller
66cc395d7fSTejun Heo  * can wait for the completion of all using wb_wait_for_completion().  Work
67cc395d7fSTejun Heo  * items which are waited upon aren't freed automatically on completion.
68cc395d7fSTejun Heo  */
69cc395d7fSTejun Heo #define DEFINE_WB_COMPLETION_ONSTACK(cmpl)				\
70cc395d7fSTejun Heo 	struct wb_completion cmpl = {					\
71cc395d7fSTejun Heo 		.cnt		= ATOMIC_INIT(1),			\
72cc395d7fSTejun Heo 	}
73cc395d7fSTejun Heo 
74cc395d7fSTejun Heo 
75cc395d7fSTejun Heo /*
76a2f48706STheodore Ts'o  * If an inode is constantly having its pages dirtied, but then the
77a2f48706STheodore Ts'o  * updates stop dirtytime_expire_interval seconds in the past, it's
78a2f48706STheodore Ts'o  * possible for the worst case time between when an inode has its
79a2f48706STheodore Ts'o  * timestamps updated and when they finally get written out to be two
80a2f48706STheodore Ts'o  * dirtytime_expire_intervals.  We set the default to 12 hours (in
81a2f48706STheodore Ts'o  * seconds), which means most of the time inodes will have their
82a2f48706STheodore Ts'o  * timestamps written to disk after 12 hours, but in the worst case a
83a2f48706STheodore Ts'o  * few inodes might not their timestamps updated for 24 hours.
84a2f48706STheodore Ts'o  */
85a2f48706STheodore Ts'o unsigned int dirtytime_expire_interval = 12 * 60 * 60;
86a2f48706STheodore Ts'o 
877ccf19a8SNick Piggin static inline struct inode *wb_inode(struct list_head *head)
887ccf19a8SNick Piggin {
897ccf19a8SNick Piggin 	return list_entry(head, struct inode, i_wb_list);
907ccf19a8SNick Piggin }
917ccf19a8SNick Piggin 
9215eb77a0SWu Fengguang /*
9315eb77a0SWu Fengguang  * Include the creation of the trace points after defining the
9415eb77a0SWu Fengguang  * wb_writeback_work structure and inline functions so that the definition
9515eb77a0SWu Fengguang  * remains local to this file.
9615eb77a0SWu Fengguang  */
9715eb77a0SWu Fengguang #define CREATE_TRACE_POINTS
9815eb77a0SWu Fengguang #include <trace/events/writeback.h>
9915eb77a0SWu Fengguang 
100774016b2SSteven Whitehouse EXPORT_TRACEPOINT_SYMBOL_GPL(wbc_writepage);
101774016b2SSteven Whitehouse 
102d6c10f1fSTejun Heo static bool wb_io_lists_populated(struct bdi_writeback *wb)
103d6c10f1fSTejun Heo {
104d6c10f1fSTejun Heo 	if (wb_has_dirty_io(wb)) {
105d6c10f1fSTejun Heo 		return false;
106d6c10f1fSTejun Heo 	} else {
107d6c10f1fSTejun Heo 		set_bit(WB_has_dirty_io, &wb->state);
10895a46c65STejun Heo 		WARN_ON_ONCE(!wb->avg_write_bandwidth);
109766a9d6eSTejun Heo 		atomic_long_add(wb->avg_write_bandwidth,
110766a9d6eSTejun Heo 				&wb->bdi->tot_write_bandwidth);
111d6c10f1fSTejun Heo 		return true;
112d6c10f1fSTejun Heo 	}
113d6c10f1fSTejun Heo }
114d6c10f1fSTejun Heo 
115d6c10f1fSTejun Heo static void wb_io_lists_depopulated(struct bdi_writeback *wb)
116d6c10f1fSTejun Heo {
117d6c10f1fSTejun Heo 	if (wb_has_dirty_io(wb) && list_empty(&wb->b_dirty) &&
118766a9d6eSTejun Heo 	    list_empty(&wb->b_io) && list_empty(&wb->b_more_io)) {
119d6c10f1fSTejun Heo 		clear_bit(WB_has_dirty_io, &wb->state);
12095a46c65STejun Heo 		WARN_ON_ONCE(atomic_long_sub_return(wb->avg_write_bandwidth,
12195a46c65STejun Heo 					&wb->bdi->tot_write_bandwidth) < 0);
122766a9d6eSTejun Heo 	}
123d6c10f1fSTejun Heo }
124d6c10f1fSTejun Heo 
125d6c10f1fSTejun Heo /**
126d6c10f1fSTejun Heo  * inode_wb_list_move_locked - move an inode onto a bdi_writeback IO list
127d6c10f1fSTejun Heo  * @inode: inode to be moved
128d6c10f1fSTejun Heo  * @wb: target bdi_writeback
129d6c10f1fSTejun Heo  * @head: one of @wb->b_{dirty|io|more_io}
130d6c10f1fSTejun Heo  *
131d6c10f1fSTejun Heo  * Move @inode->i_wb_list to @list of @wb and set %WB_has_dirty_io.
132d6c10f1fSTejun Heo  * Returns %true if @inode is the first occupant of the !dirty_time IO
133d6c10f1fSTejun Heo  * lists; otherwise, %false.
134d6c10f1fSTejun Heo  */
135d6c10f1fSTejun Heo static bool inode_wb_list_move_locked(struct inode *inode,
136d6c10f1fSTejun Heo 				      struct bdi_writeback *wb,
137d6c10f1fSTejun Heo 				      struct list_head *head)
138d6c10f1fSTejun Heo {
139d6c10f1fSTejun Heo 	assert_spin_locked(&wb->list_lock);
140d6c10f1fSTejun Heo 
141d6c10f1fSTejun Heo 	list_move(&inode->i_wb_list, head);
142d6c10f1fSTejun Heo 
143d6c10f1fSTejun Heo 	/* dirty_time doesn't count as dirty_io until expiration */
144d6c10f1fSTejun Heo 	if (head != &wb->b_dirty_time)
145d6c10f1fSTejun Heo 		return wb_io_lists_populated(wb);
146d6c10f1fSTejun Heo 
147d6c10f1fSTejun Heo 	wb_io_lists_depopulated(wb);
148d6c10f1fSTejun Heo 	return false;
149d6c10f1fSTejun Heo }
150d6c10f1fSTejun Heo 
151d6c10f1fSTejun Heo /**
152d6c10f1fSTejun Heo  * inode_wb_list_del_locked - remove an inode from its bdi_writeback IO list
153d6c10f1fSTejun Heo  * @inode: inode to be removed
154d6c10f1fSTejun Heo  * @wb: bdi_writeback @inode is being removed from
155d6c10f1fSTejun Heo  *
156d6c10f1fSTejun Heo  * Remove @inode which may be on one of @wb->b_{dirty|io|more_io} lists and
157d6c10f1fSTejun Heo  * clear %WB_has_dirty_io if all are empty afterwards.
158d6c10f1fSTejun Heo  */
159d6c10f1fSTejun Heo static void inode_wb_list_del_locked(struct inode *inode,
160d6c10f1fSTejun Heo 				     struct bdi_writeback *wb)
161d6c10f1fSTejun Heo {
162d6c10f1fSTejun Heo 	assert_spin_locked(&wb->list_lock);
163d6c10f1fSTejun Heo 
164d6c10f1fSTejun Heo 	list_del_init(&inode->i_wb_list);
165d6c10f1fSTejun Heo 	wb_io_lists_depopulated(wb);
166d6c10f1fSTejun Heo }
167d6c10f1fSTejun Heo 
168f0054bb1STejun Heo static void wb_wakeup(struct bdi_writeback *wb)
1695acda9d1SJan Kara {
170f0054bb1STejun Heo 	spin_lock_bh(&wb->work_lock);
171f0054bb1STejun Heo 	if (test_bit(WB_registered, &wb->state))
172f0054bb1STejun Heo 		mod_delayed_work(bdi_wq, &wb->dwork, 0);
173f0054bb1STejun Heo 	spin_unlock_bh(&wb->work_lock);
1745acda9d1SJan Kara }
1755acda9d1SJan Kara 
176f0054bb1STejun Heo static void wb_queue_work(struct bdi_writeback *wb,
1776585027aSJan Kara 			  struct wb_writeback_work *work)
1786585027aSJan Kara {
179f0054bb1STejun Heo 	trace_writeback_queue(wb->bdi, work);
1806585027aSJan Kara 
181f0054bb1STejun Heo 	spin_lock_bh(&wb->work_lock);
1828a1270cdSTejun Heo 	if (!test_bit(WB_registered, &wb->state))
1835acda9d1SJan Kara 		goto out_unlock;
184cc395d7fSTejun Heo 	if (work->done)
185cc395d7fSTejun Heo 		atomic_inc(&work->done->cnt);
186f0054bb1STejun Heo 	list_add_tail(&work->list, &wb->work_list);
187f0054bb1STejun Heo 	mod_delayed_work(bdi_wq, &wb->dwork, 0);
1885acda9d1SJan Kara out_unlock:
189f0054bb1STejun Heo 	spin_unlock_bh(&wb->work_lock);
19003ba3782SJens Axboe }
1911da177e4SLinus Torvalds 
192cc395d7fSTejun Heo /**
193cc395d7fSTejun Heo  * wb_wait_for_completion - wait for completion of bdi_writeback_works
194cc395d7fSTejun Heo  * @bdi: bdi work items were issued to
195cc395d7fSTejun Heo  * @done: target wb_completion
196cc395d7fSTejun Heo  *
197cc395d7fSTejun Heo  * Wait for one or more work items issued to @bdi with their ->done field
198cc395d7fSTejun Heo  * set to @done, which should have been defined with
199cc395d7fSTejun Heo  * DEFINE_WB_COMPLETION_ONSTACK().  This function returns after all such
200cc395d7fSTejun Heo  * work items are completed.  Work items which are waited upon aren't freed
201cc395d7fSTejun Heo  * automatically on completion.
202cc395d7fSTejun Heo  */
203cc395d7fSTejun Heo static void wb_wait_for_completion(struct backing_dev_info *bdi,
204cc395d7fSTejun Heo 				   struct wb_completion *done)
205cc395d7fSTejun Heo {
206cc395d7fSTejun Heo 	atomic_dec(&done->cnt);		/* put down the initial count */
207cc395d7fSTejun Heo 	wait_event(bdi->wb_waitq, !atomic_read(&done->cnt));
208cc395d7fSTejun Heo }
209cc395d7fSTejun Heo 
210703c2708STejun Heo #ifdef CONFIG_CGROUP_WRITEBACK
211703c2708STejun Heo 
2122a814908STejun Heo /* parameters for foreign inode detection, see wb_detach_inode() */
2132a814908STejun Heo #define WB_FRN_TIME_SHIFT	13	/* 1s = 2^13, upto 8 secs w/ 16bit */
2142a814908STejun Heo #define WB_FRN_TIME_AVG_SHIFT	3	/* avg = avg * 7/8 + new * 1/8 */
2152a814908STejun Heo #define WB_FRN_TIME_CUT_DIV	2	/* ignore rounds < avg / 2 */
2162a814908STejun Heo #define WB_FRN_TIME_PERIOD	(2 * (1 << WB_FRN_TIME_SHIFT))	/* 2s */
2172a814908STejun Heo 
2182a814908STejun Heo #define WB_FRN_HIST_SLOTS	16	/* inode->i_wb_frn_history is 16bit */
2192a814908STejun Heo #define WB_FRN_HIST_UNIT	(WB_FRN_TIME_PERIOD / WB_FRN_HIST_SLOTS)
2202a814908STejun Heo 					/* each slot's duration is 2s / 16 */
2212a814908STejun Heo #define WB_FRN_HIST_THR_SLOTS	(WB_FRN_HIST_SLOTS / 2)
2222a814908STejun Heo 					/* if foreign slots >= 8, switch */
2232a814908STejun Heo #define WB_FRN_HIST_MAX_SLOTS	(WB_FRN_HIST_THR_SLOTS / 2 + 1)
2242a814908STejun Heo 					/* one round can affect upto 5 slots */
2252a814908STejun Heo 
22621c6321fSTejun Heo void __inode_attach_wb(struct inode *inode, struct page *page)
22721c6321fSTejun Heo {
22821c6321fSTejun Heo 	struct backing_dev_info *bdi = inode_to_bdi(inode);
22921c6321fSTejun Heo 	struct bdi_writeback *wb = NULL;
23021c6321fSTejun Heo 
23121c6321fSTejun Heo 	if (inode_cgwb_enabled(inode)) {
23221c6321fSTejun Heo 		struct cgroup_subsys_state *memcg_css;
23321c6321fSTejun Heo 
23421c6321fSTejun Heo 		if (page) {
23521c6321fSTejun Heo 			memcg_css = mem_cgroup_css_from_page(page);
23621c6321fSTejun Heo 			wb = wb_get_create(bdi, memcg_css, GFP_ATOMIC);
23721c6321fSTejun Heo 		} else {
23821c6321fSTejun Heo 			/* must pin memcg_css, see wb_get_create() */
23921c6321fSTejun Heo 			memcg_css = task_get_css(current, memory_cgrp_id);
24021c6321fSTejun Heo 			wb = wb_get_create(bdi, memcg_css, GFP_ATOMIC);
24121c6321fSTejun Heo 			css_put(memcg_css);
24221c6321fSTejun Heo 		}
24321c6321fSTejun Heo 	}
24421c6321fSTejun Heo 
24521c6321fSTejun Heo 	if (!wb)
24621c6321fSTejun Heo 		wb = &bdi->wb;
24721c6321fSTejun Heo 
24821c6321fSTejun Heo 	/*
24921c6321fSTejun Heo 	 * There may be multiple instances of this function racing to
25021c6321fSTejun Heo 	 * update the same inode.  Use cmpxchg() to tell the winner.
25121c6321fSTejun Heo 	 */
25221c6321fSTejun Heo 	if (unlikely(cmpxchg(&inode->i_wb, NULL, wb)))
25321c6321fSTejun Heo 		wb_put(wb);
25421c6321fSTejun Heo }
25521c6321fSTejun Heo 
256703c2708STejun Heo /**
25787e1d789STejun Heo  * locked_inode_to_wb_and_lock_list - determine a locked inode's wb and lock it
25887e1d789STejun Heo  * @inode: inode of interest with i_lock held
25987e1d789STejun Heo  *
26087e1d789STejun Heo  * Returns @inode's wb with its list_lock held.  @inode->i_lock must be
26187e1d789STejun Heo  * held on entry and is released on return.  The returned wb is guaranteed
26287e1d789STejun Heo  * to stay @inode's associated wb until its list_lock is released.
26387e1d789STejun Heo  */
26487e1d789STejun Heo static struct bdi_writeback *
26587e1d789STejun Heo locked_inode_to_wb_and_lock_list(struct inode *inode)
26687e1d789STejun Heo 	__releases(&inode->i_lock)
26787e1d789STejun Heo 	__acquires(&wb->list_lock)
26887e1d789STejun Heo {
26987e1d789STejun Heo 	while (true) {
27087e1d789STejun Heo 		struct bdi_writeback *wb = inode_to_wb(inode);
27187e1d789STejun Heo 
27287e1d789STejun Heo 		/*
27387e1d789STejun Heo 		 * inode_to_wb() association is protected by both
27487e1d789STejun Heo 		 * @inode->i_lock and @wb->list_lock but list_lock nests
27587e1d789STejun Heo 		 * outside i_lock.  Drop i_lock and verify that the
27687e1d789STejun Heo 		 * association hasn't changed after acquiring list_lock.
27787e1d789STejun Heo 		 */
27887e1d789STejun Heo 		wb_get(wb);
27987e1d789STejun Heo 		spin_unlock(&inode->i_lock);
28087e1d789STejun Heo 		spin_lock(&wb->list_lock);
28187e1d789STejun Heo 		wb_put(wb);		/* not gonna deref it anymore */
28287e1d789STejun Heo 
283aaa2cacfSTejun Heo 		/* i_wb may have changed inbetween, can't use inode_to_wb() */
284aaa2cacfSTejun Heo 		if (likely(wb == inode->i_wb))
28587e1d789STejun Heo 			return wb;	/* @inode already has ref */
28687e1d789STejun Heo 
28787e1d789STejun Heo 		spin_unlock(&wb->list_lock);
28887e1d789STejun Heo 		cpu_relax();
28987e1d789STejun Heo 		spin_lock(&inode->i_lock);
29087e1d789STejun Heo 	}
29187e1d789STejun Heo }
29287e1d789STejun Heo 
29387e1d789STejun Heo /**
29487e1d789STejun Heo  * inode_to_wb_and_lock_list - determine an inode's wb and lock it
29587e1d789STejun Heo  * @inode: inode of interest
29687e1d789STejun Heo  *
29787e1d789STejun Heo  * Same as locked_inode_to_wb_and_lock_list() but @inode->i_lock isn't held
29887e1d789STejun Heo  * on entry.
29987e1d789STejun Heo  */
30087e1d789STejun Heo static struct bdi_writeback *inode_to_wb_and_lock_list(struct inode *inode)
30187e1d789STejun Heo 	__acquires(&wb->list_lock)
30287e1d789STejun Heo {
30387e1d789STejun Heo 	spin_lock(&inode->i_lock);
30487e1d789STejun Heo 	return locked_inode_to_wb_and_lock_list(inode);
30587e1d789STejun Heo }
30687e1d789STejun Heo 
307682aa8e1STejun Heo struct inode_switch_wbs_context {
308682aa8e1STejun Heo 	struct inode		*inode;
309682aa8e1STejun Heo 	struct bdi_writeback	*new_wb;
310682aa8e1STejun Heo 
311682aa8e1STejun Heo 	struct rcu_head		rcu_head;
312682aa8e1STejun Heo 	struct work_struct	work;
313682aa8e1STejun Heo };
314682aa8e1STejun Heo 
315682aa8e1STejun Heo static void inode_switch_wbs_work_fn(struct work_struct *work)
316682aa8e1STejun Heo {
317682aa8e1STejun Heo 	struct inode_switch_wbs_context *isw =
318682aa8e1STejun Heo 		container_of(work, struct inode_switch_wbs_context, work);
319682aa8e1STejun Heo 	struct inode *inode = isw->inode;
320d10c8095STejun Heo 	struct address_space *mapping = inode->i_mapping;
321d10c8095STejun Heo 	struct bdi_writeback *old_wb = inode->i_wb;
322682aa8e1STejun Heo 	struct bdi_writeback *new_wb = isw->new_wb;
323d10c8095STejun Heo 	struct radix_tree_iter iter;
324d10c8095STejun Heo 	bool switched = false;
325d10c8095STejun Heo 	void **slot;
326682aa8e1STejun Heo 
327682aa8e1STejun Heo 	/*
328682aa8e1STejun Heo 	 * By the time control reaches here, RCU grace period has passed
329682aa8e1STejun Heo 	 * since I_WB_SWITCH assertion and all wb stat update transactions
330682aa8e1STejun Heo 	 * between unlocked_inode_to_wb_begin/end() are guaranteed to be
331682aa8e1STejun Heo 	 * synchronizing against mapping->tree_lock.
332d10c8095STejun Heo 	 *
333d10c8095STejun Heo 	 * Grabbing old_wb->list_lock, inode->i_lock and mapping->tree_lock
334d10c8095STejun Heo 	 * gives us exclusion against all wb related operations on @inode
335d10c8095STejun Heo 	 * including IO list manipulations and stat updates.
336682aa8e1STejun Heo 	 */
337d10c8095STejun Heo 	if (old_wb < new_wb) {
338d10c8095STejun Heo 		spin_lock(&old_wb->list_lock);
339d10c8095STejun Heo 		spin_lock_nested(&new_wb->list_lock, SINGLE_DEPTH_NESTING);
340d10c8095STejun Heo 	} else {
341d10c8095STejun Heo 		spin_lock(&new_wb->list_lock);
342d10c8095STejun Heo 		spin_lock_nested(&old_wb->list_lock, SINGLE_DEPTH_NESTING);
343d10c8095STejun Heo 	}
344682aa8e1STejun Heo 	spin_lock(&inode->i_lock);
345d10c8095STejun Heo 	spin_lock_irq(&mapping->tree_lock);
346682aa8e1STejun Heo 
347d10c8095STejun Heo 	/*
348d10c8095STejun Heo 	 * Once I_FREEING is visible under i_lock, the eviction path owns
349d10c8095STejun Heo 	 * the inode and we shouldn't modify ->i_wb_list.
350d10c8095STejun Heo 	 */
351d10c8095STejun Heo 	if (unlikely(inode->i_state & I_FREEING))
352d10c8095STejun Heo 		goto skip_switch;
353d10c8095STejun Heo 
354d10c8095STejun Heo 	/*
355d10c8095STejun Heo 	 * Count and transfer stats.  Note that PAGECACHE_TAG_DIRTY points
356d10c8095STejun Heo 	 * to possibly dirty pages while PAGECACHE_TAG_WRITEBACK points to
357d10c8095STejun Heo 	 * pages actually under underwriteback.
358d10c8095STejun Heo 	 */
359d10c8095STejun Heo 	radix_tree_for_each_tagged(slot, &mapping->page_tree, &iter, 0,
360d10c8095STejun Heo 				   PAGECACHE_TAG_DIRTY) {
361d10c8095STejun Heo 		struct page *page = radix_tree_deref_slot_protected(slot,
362d10c8095STejun Heo 							&mapping->tree_lock);
363d10c8095STejun Heo 		if (likely(page) && PageDirty(page)) {
364d10c8095STejun Heo 			__dec_wb_stat(old_wb, WB_RECLAIMABLE);
365d10c8095STejun Heo 			__inc_wb_stat(new_wb, WB_RECLAIMABLE);
366d10c8095STejun Heo 		}
367d10c8095STejun Heo 	}
368d10c8095STejun Heo 
369d10c8095STejun Heo 	radix_tree_for_each_tagged(slot, &mapping->page_tree, &iter, 0,
370d10c8095STejun Heo 				   PAGECACHE_TAG_WRITEBACK) {
371d10c8095STejun Heo 		struct page *page = radix_tree_deref_slot_protected(slot,
372d10c8095STejun Heo 							&mapping->tree_lock);
373d10c8095STejun Heo 		if (likely(page)) {
374d10c8095STejun Heo 			WARN_ON_ONCE(!PageWriteback(page));
375d10c8095STejun Heo 			__dec_wb_stat(old_wb, WB_WRITEBACK);
376d10c8095STejun Heo 			__inc_wb_stat(new_wb, WB_WRITEBACK);
377d10c8095STejun Heo 		}
378d10c8095STejun Heo 	}
379d10c8095STejun Heo 
380d10c8095STejun Heo 	wb_get(new_wb);
381d10c8095STejun Heo 
382d10c8095STejun Heo 	/*
383d10c8095STejun Heo 	 * Transfer to @new_wb's IO list if necessary.  The specific list
384d10c8095STejun Heo 	 * @inode was on is ignored and the inode is put on ->b_dirty which
385d10c8095STejun Heo 	 * is always correct including from ->b_dirty_time.  The transfer
386d10c8095STejun Heo 	 * preserves @inode->dirtied_when ordering.
387d10c8095STejun Heo 	 */
388d10c8095STejun Heo 	if (!list_empty(&inode->i_wb_list)) {
389d10c8095STejun Heo 		struct inode *pos;
390d10c8095STejun Heo 
391d10c8095STejun Heo 		inode_wb_list_del_locked(inode, old_wb);
392d10c8095STejun Heo 		inode->i_wb = new_wb;
393d10c8095STejun Heo 		list_for_each_entry(pos, &new_wb->b_dirty, i_wb_list)
394d10c8095STejun Heo 			if (time_after_eq(inode->dirtied_when,
395d10c8095STejun Heo 					  pos->dirtied_when))
396d10c8095STejun Heo 				break;
397d10c8095STejun Heo 		inode_wb_list_move_locked(inode, new_wb, pos->i_wb_list.prev);
398d10c8095STejun Heo 	} else {
399d10c8095STejun Heo 		inode->i_wb = new_wb;
400d10c8095STejun Heo 	}
401d10c8095STejun Heo 
402d10c8095STejun Heo 	/* ->i_wb_frn updates may race wbc_detach_inode() but doesn't matter */
403682aa8e1STejun Heo 	inode->i_wb_frn_winner = 0;
404682aa8e1STejun Heo 	inode->i_wb_frn_avg_time = 0;
405682aa8e1STejun Heo 	inode->i_wb_frn_history = 0;
406d10c8095STejun Heo 	switched = true;
407d10c8095STejun Heo skip_switch:
408682aa8e1STejun Heo 	/*
409682aa8e1STejun Heo 	 * Paired with load_acquire in unlocked_inode_to_wb_begin() and
410682aa8e1STejun Heo 	 * ensures that the new wb is visible if they see !I_WB_SWITCH.
411682aa8e1STejun Heo 	 */
412682aa8e1STejun Heo 	smp_store_release(&inode->i_state, inode->i_state & ~I_WB_SWITCH);
413682aa8e1STejun Heo 
414d10c8095STejun Heo 	spin_unlock_irq(&mapping->tree_lock);
415682aa8e1STejun Heo 	spin_unlock(&inode->i_lock);
416d10c8095STejun Heo 	spin_unlock(&new_wb->list_lock);
417d10c8095STejun Heo 	spin_unlock(&old_wb->list_lock);
418d10c8095STejun Heo 
419d10c8095STejun Heo 	if (switched) {
420d10c8095STejun Heo 		wb_wakeup(new_wb);
421d10c8095STejun Heo 		wb_put(old_wb);
422d10c8095STejun Heo 	}
423d10c8095STejun Heo 	wb_put(new_wb);
424682aa8e1STejun Heo 
425682aa8e1STejun Heo 	iput(inode);
426682aa8e1STejun Heo 	kfree(isw);
427682aa8e1STejun Heo }
428682aa8e1STejun Heo 
429682aa8e1STejun Heo static void inode_switch_wbs_rcu_fn(struct rcu_head *rcu_head)
430682aa8e1STejun Heo {
431682aa8e1STejun Heo 	struct inode_switch_wbs_context *isw = container_of(rcu_head,
432682aa8e1STejun Heo 				struct inode_switch_wbs_context, rcu_head);
433682aa8e1STejun Heo 
434682aa8e1STejun Heo 	/* needs to grab bh-unsafe locks, bounce to work item */
435682aa8e1STejun Heo 	INIT_WORK(&isw->work, inode_switch_wbs_work_fn);
436682aa8e1STejun Heo 	schedule_work(&isw->work);
437682aa8e1STejun Heo }
438682aa8e1STejun Heo 
439682aa8e1STejun Heo /**
440682aa8e1STejun Heo  * inode_switch_wbs - change the wb association of an inode
441682aa8e1STejun Heo  * @inode: target inode
442682aa8e1STejun Heo  * @new_wb_id: ID of the new wb
443682aa8e1STejun Heo  *
444682aa8e1STejun Heo  * Switch @inode's wb association to the wb identified by @new_wb_id.  The
445682aa8e1STejun Heo  * switching is performed asynchronously and may fail silently.
446682aa8e1STejun Heo  */
447682aa8e1STejun Heo static void inode_switch_wbs(struct inode *inode, int new_wb_id)
448682aa8e1STejun Heo {
449682aa8e1STejun Heo 	struct backing_dev_info *bdi = inode_to_bdi(inode);
450682aa8e1STejun Heo 	struct cgroup_subsys_state *memcg_css;
451682aa8e1STejun Heo 	struct inode_switch_wbs_context *isw;
452682aa8e1STejun Heo 
453682aa8e1STejun Heo 	/* noop if seems to be already in progress */
454682aa8e1STejun Heo 	if (inode->i_state & I_WB_SWITCH)
455682aa8e1STejun Heo 		return;
456682aa8e1STejun Heo 
457682aa8e1STejun Heo 	isw = kzalloc(sizeof(*isw), GFP_ATOMIC);
458682aa8e1STejun Heo 	if (!isw)
459682aa8e1STejun Heo 		return;
460682aa8e1STejun Heo 
461682aa8e1STejun Heo 	/* find and pin the new wb */
462682aa8e1STejun Heo 	rcu_read_lock();
463682aa8e1STejun Heo 	memcg_css = css_from_id(new_wb_id, &memory_cgrp_subsys);
464682aa8e1STejun Heo 	if (memcg_css)
465682aa8e1STejun Heo 		isw->new_wb = wb_get_create(bdi, memcg_css, GFP_ATOMIC);
466682aa8e1STejun Heo 	rcu_read_unlock();
467682aa8e1STejun Heo 	if (!isw->new_wb)
468682aa8e1STejun Heo 		goto out_free;
469682aa8e1STejun Heo 
470682aa8e1STejun Heo 	/* while holding I_WB_SWITCH, no one else can update the association */
471682aa8e1STejun Heo 	spin_lock(&inode->i_lock);
472682aa8e1STejun Heo 	if (inode->i_state & (I_WB_SWITCH | I_FREEING) ||
473682aa8e1STejun Heo 	    inode_to_wb(inode) == isw->new_wb) {
474682aa8e1STejun Heo 		spin_unlock(&inode->i_lock);
475682aa8e1STejun Heo 		goto out_free;
476682aa8e1STejun Heo 	}
477682aa8e1STejun Heo 	inode->i_state |= I_WB_SWITCH;
478682aa8e1STejun Heo 	spin_unlock(&inode->i_lock);
479682aa8e1STejun Heo 
480682aa8e1STejun Heo 	ihold(inode);
481682aa8e1STejun Heo 	isw->inode = inode;
482682aa8e1STejun Heo 
483682aa8e1STejun Heo 	/*
484682aa8e1STejun Heo 	 * In addition to synchronizing among switchers, I_WB_SWITCH tells
485682aa8e1STejun Heo 	 * the RCU protected stat update paths to grab the mapping's
486682aa8e1STejun Heo 	 * tree_lock so that stat transfer can synchronize against them.
487682aa8e1STejun Heo 	 * Let's continue after I_WB_SWITCH is guaranteed to be visible.
488682aa8e1STejun Heo 	 */
489682aa8e1STejun Heo 	call_rcu(&isw->rcu_head, inode_switch_wbs_rcu_fn);
490682aa8e1STejun Heo 	return;
491682aa8e1STejun Heo 
492682aa8e1STejun Heo out_free:
493682aa8e1STejun Heo 	if (isw->new_wb)
494682aa8e1STejun Heo 		wb_put(isw->new_wb);
495682aa8e1STejun Heo 	kfree(isw);
496682aa8e1STejun Heo }
497682aa8e1STejun Heo 
49887e1d789STejun Heo /**
499b16b1debSTejun Heo  * wbc_attach_and_unlock_inode - associate wbc with target inode and unlock it
500b16b1debSTejun Heo  * @wbc: writeback_control of interest
501b16b1debSTejun Heo  * @inode: target inode
502b16b1debSTejun Heo  *
503b16b1debSTejun Heo  * @inode is locked and about to be written back under the control of @wbc.
504b16b1debSTejun Heo  * Record @inode's writeback context into @wbc and unlock the i_lock.  On
505b16b1debSTejun Heo  * writeback completion, wbc_detach_inode() should be called.  This is used
506b16b1debSTejun Heo  * to track the cgroup writeback context.
507b16b1debSTejun Heo  */
508b16b1debSTejun Heo void wbc_attach_and_unlock_inode(struct writeback_control *wbc,
509b16b1debSTejun Heo 				 struct inode *inode)
510b16b1debSTejun Heo {
511dd73e4b7STejun Heo 	if (!inode_cgwb_enabled(inode)) {
512dd73e4b7STejun Heo 		spin_unlock(&inode->i_lock);
513dd73e4b7STejun Heo 		return;
514dd73e4b7STejun Heo 	}
515dd73e4b7STejun Heo 
516b16b1debSTejun Heo 	wbc->wb = inode_to_wb(inode);
5172a814908STejun Heo 	wbc->inode = inode;
5182a814908STejun Heo 
5192a814908STejun Heo 	wbc->wb_id = wbc->wb->memcg_css->id;
5202a814908STejun Heo 	wbc->wb_lcand_id = inode->i_wb_frn_winner;
5212a814908STejun Heo 	wbc->wb_tcand_id = 0;
5222a814908STejun Heo 	wbc->wb_bytes = 0;
5232a814908STejun Heo 	wbc->wb_lcand_bytes = 0;
5242a814908STejun Heo 	wbc->wb_tcand_bytes = 0;
5252a814908STejun Heo 
526b16b1debSTejun Heo 	wb_get(wbc->wb);
527b16b1debSTejun Heo 	spin_unlock(&inode->i_lock);
528e8a7abf5STejun Heo 
529e8a7abf5STejun Heo 	/*
530e8a7abf5STejun Heo 	 * A dying wb indicates that the memcg-blkcg mapping has changed
531e8a7abf5STejun Heo 	 * and a new wb is already serving the memcg.  Switch immediately.
532e8a7abf5STejun Heo 	 */
533e8a7abf5STejun Heo 	if (unlikely(wb_dying(wbc->wb)))
534e8a7abf5STejun Heo 		inode_switch_wbs(inode, wbc->wb_id);
535b16b1debSTejun Heo }
536b16b1debSTejun Heo 
537b16b1debSTejun Heo /**
5382a814908STejun Heo  * wbc_detach_inode - disassociate wbc from inode and perform foreign detection
5392a814908STejun Heo  * @wbc: writeback_control of the just finished writeback
540b16b1debSTejun Heo  *
541b16b1debSTejun Heo  * To be called after a writeback attempt of an inode finishes and undoes
542b16b1debSTejun Heo  * wbc_attach_and_unlock_inode().  Can be called under any context.
5432a814908STejun Heo  *
5442a814908STejun Heo  * As concurrent write sharing of an inode is expected to be very rare and
5452a814908STejun Heo  * memcg only tracks page ownership on first-use basis severely confining
5462a814908STejun Heo  * the usefulness of such sharing, cgroup writeback tracks ownership
5472a814908STejun Heo  * per-inode.  While the support for concurrent write sharing of an inode
5482a814908STejun Heo  * is deemed unnecessary, an inode being written to by different cgroups at
5492a814908STejun Heo  * different points in time is a lot more common, and, more importantly,
5502a814908STejun Heo  * charging only by first-use can too readily lead to grossly incorrect
5512a814908STejun Heo  * behaviors (single foreign page can lead to gigabytes of writeback to be
5522a814908STejun Heo  * incorrectly attributed).
5532a814908STejun Heo  *
5542a814908STejun Heo  * To resolve this issue, cgroup writeback detects the majority dirtier of
5552a814908STejun Heo  * an inode and transfers the ownership to it.  To avoid unnnecessary
5562a814908STejun Heo  * oscillation, the detection mechanism keeps track of history and gives
5572a814908STejun Heo  * out the switch verdict only if the foreign usage pattern is stable over
5582a814908STejun Heo  * a certain amount of time and/or writeback attempts.
5592a814908STejun Heo  *
5602a814908STejun Heo  * On each writeback attempt, @wbc tries to detect the majority writer
5612a814908STejun Heo  * using Boyer-Moore majority vote algorithm.  In addition to the byte
5622a814908STejun Heo  * count from the majority voting, it also counts the bytes written for the
5632a814908STejun Heo  * current wb and the last round's winner wb (max of last round's current
5642a814908STejun Heo  * wb, the winner from two rounds ago, and the last round's majority
5652a814908STejun Heo  * candidate).  Keeping track of the historical winner helps the algorithm
5662a814908STejun Heo  * to semi-reliably detect the most active writer even when it's not the
5672a814908STejun Heo  * absolute majority.
5682a814908STejun Heo  *
5692a814908STejun Heo  * Once the winner of the round is determined, whether the winner is
5702a814908STejun Heo  * foreign or not and how much IO time the round consumed is recorded in
5712a814908STejun Heo  * inode->i_wb_frn_history.  If the amount of recorded foreign IO time is
5722a814908STejun Heo  * over a certain threshold, the switch verdict is given.
573b16b1debSTejun Heo  */
574b16b1debSTejun Heo void wbc_detach_inode(struct writeback_control *wbc)
575b16b1debSTejun Heo {
5762a814908STejun Heo 	struct bdi_writeback *wb = wbc->wb;
5772a814908STejun Heo 	struct inode *inode = wbc->inode;
578dd73e4b7STejun Heo 	unsigned long avg_time, max_bytes, max_time;
579dd73e4b7STejun Heo 	u16 history;
5802a814908STejun Heo 	int max_id;
5812a814908STejun Heo 
582dd73e4b7STejun Heo 	if (!wb)
583dd73e4b7STejun Heo 		return;
584dd73e4b7STejun Heo 
585dd73e4b7STejun Heo 	history = inode->i_wb_frn_history;
586dd73e4b7STejun Heo 	avg_time = inode->i_wb_frn_avg_time;
587dd73e4b7STejun Heo 
5882a814908STejun Heo 	/* pick the winner of this round */
5892a814908STejun Heo 	if (wbc->wb_bytes >= wbc->wb_lcand_bytes &&
5902a814908STejun Heo 	    wbc->wb_bytes >= wbc->wb_tcand_bytes) {
5912a814908STejun Heo 		max_id = wbc->wb_id;
5922a814908STejun Heo 		max_bytes = wbc->wb_bytes;
5932a814908STejun Heo 	} else if (wbc->wb_lcand_bytes >= wbc->wb_tcand_bytes) {
5942a814908STejun Heo 		max_id = wbc->wb_lcand_id;
5952a814908STejun Heo 		max_bytes = wbc->wb_lcand_bytes;
5962a814908STejun Heo 	} else {
5972a814908STejun Heo 		max_id = wbc->wb_tcand_id;
5982a814908STejun Heo 		max_bytes = wbc->wb_tcand_bytes;
5992a814908STejun Heo 	}
6002a814908STejun Heo 
6012a814908STejun Heo 	/*
6022a814908STejun Heo 	 * Calculate the amount of IO time the winner consumed and fold it
6032a814908STejun Heo 	 * into the running average kept per inode.  If the consumed IO
6042a814908STejun Heo 	 * time is lower than avag / WB_FRN_TIME_CUT_DIV, ignore it for
6052a814908STejun Heo 	 * deciding whether to switch or not.  This is to prevent one-off
6062a814908STejun Heo 	 * small dirtiers from skewing the verdict.
6072a814908STejun Heo 	 */
6082a814908STejun Heo 	max_time = DIV_ROUND_UP((max_bytes >> PAGE_SHIFT) << WB_FRN_TIME_SHIFT,
6092a814908STejun Heo 				wb->avg_write_bandwidth);
6102a814908STejun Heo 	if (avg_time)
6112a814908STejun Heo 		avg_time += (max_time >> WB_FRN_TIME_AVG_SHIFT) -
6122a814908STejun Heo 			    (avg_time >> WB_FRN_TIME_AVG_SHIFT);
6132a814908STejun Heo 	else
6142a814908STejun Heo 		avg_time = max_time;	/* immediate catch up on first run */
6152a814908STejun Heo 
6162a814908STejun Heo 	if (max_time >= avg_time / WB_FRN_TIME_CUT_DIV) {
6172a814908STejun Heo 		int slots;
6182a814908STejun Heo 
6192a814908STejun Heo 		/*
6202a814908STejun Heo 		 * The switch verdict is reached if foreign wb's consume
6212a814908STejun Heo 		 * more than a certain proportion of IO time in a
6222a814908STejun Heo 		 * WB_FRN_TIME_PERIOD.  This is loosely tracked by 16 slot
6232a814908STejun Heo 		 * history mask where each bit represents one sixteenth of
6242a814908STejun Heo 		 * the period.  Determine the number of slots to shift into
6252a814908STejun Heo 		 * history from @max_time.
6262a814908STejun Heo 		 */
6272a814908STejun Heo 		slots = min(DIV_ROUND_UP(max_time, WB_FRN_HIST_UNIT),
6282a814908STejun Heo 			    (unsigned long)WB_FRN_HIST_MAX_SLOTS);
6292a814908STejun Heo 		history <<= slots;
6302a814908STejun Heo 		if (wbc->wb_id != max_id)
6312a814908STejun Heo 			history |= (1U << slots) - 1;
6322a814908STejun Heo 
6332a814908STejun Heo 		/*
6342a814908STejun Heo 		 * Switch if the current wb isn't the consistent winner.
6352a814908STejun Heo 		 * If there are multiple closely competing dirtiers, the
6362a814908STejun Heo 		 * inode may switch across them repeatedly over time, which
6372a814908STejun Heo 		 * is okay.  The main goal is avoiding keeping an inode on
6382a814908STejun Heo 		 * the wrong wb for an extended period of time.
6392a814908STejun Heo 		 */
640682aa8e1STejun Heo 		if (hweight32(history) > WB_FRN_HIST_THR_SLOTS)
641682aa8e1STejun Heo 			inode_switch_wbs(inode, max_id);
6422a814908STejun Heo 	}
6432a814908STejun Heo 
6442a814908STejun Heo 	/*
6452a814908STejun Heo 	 * Multiple instances of this function may race to update the
6462a814908STejun Heo 	 * following fields but we don't mind occassional inaccuracies.
6472a814908STejun Heo 	 */
6482a814908STejun Heo 	inode->i_wb_frn_winner = max_id;
6492a814908STejun Heo 	inode->i_wb_frn_avg_time = min(avg_time, (unsigned long)U16_MAX);
6502a814908STejun Heo 	inode->i_wb_frn_history = history;
6512a814908STejun Heo 
652b16b1debSTejun Heo 	wb_put(wbc->wb);
653b16b1debSTejun Heo 	wbc->wb = NULL;
654b16b1debSTejun Heo }
655b16b1debSTejun Heo 
656b16b1debSTejun Heo /**
6572a814908STejun Heo  * wbc_account_io - account IO issued during writeback
6582a814908STejun Heo  * @wbc: writeback_control of the writeback in progress
6592a814908STejun Heo  * @page: page being written out
6602a814908STejun Heo  * @bytes: number of bytes being written out
6612a814908STejun Heo  *
6622a814908STejun Heo  * @bytes from @page are about to written out during the writeback
6632a814908STejun Heo  * controlled by @wbc.  Keep the book for foreign inode detection.  See
6642a814908STejun Heo  * wbc_detach_inode().
6652a814908STejun Heo  */
6662a814908STejun Heo void wbc_account_io(struct writeback_control *wbc, struct page *page,
6672a814908STejun Heo 		    size_t bytes)
6682a814908STejun Heo {
6692a814908STejun Heo 	int id;
6702a814908STejun Heo 
6712a814908STejun Heo 	/*
6722a814908STejun Heo 	 * pageout() path doesn't attach @wbc to the inode being written
6732a814908STejun Heo 	 * out.  This is intentional as we don't want the function to block
6742a814908STejun Heo 	 * behind a slow cgroup.  Ultimately, we want pageout() to kick off
6752a814908STejun Heo 	 * regular writeback instead of writing things out itself.
6762a814908STejun Heo 	 */
6772a814908STejun Heo 	if (!wbc->wb)
6782a814908STejun Heo 		return;
6792a814908STejun Heo 
6802a814908STejun Heo 	rcu_read_lock();
6812a814908STejun Heo 	id = mem_cgroup_css_from_page(page)->id;
6822a814908STejun Heo 	rcu_read_unlock();
6832a814908STejun Heo 
6842a814908STejun Heo 	if (id == wbc->wb_id) {
6852a814908STejun Heo 		wbc->wb_bytes += bytes;
6862a814908STejun Heo 		return;
6872a814908STejun Heo 	}
6882a814908STejun Heo 
6892a814908STejun Heo 	if (id == wbc->wb_lcand_id)
6902a814908STejun Heo 		wbc->wb_lcand_bytes += bytes;
6912a814908STejun Heo 
6922a814908STejun Heo 	/* Boyer-Moore majority vote algorithm */
6932a814908STejun Heo 	if (!wbc->wb_tcand_bytes)
6942a814908STejun Heo 		wbc->wb_tcand_id = id;
6952a814908STejun Heo 	if (id == wbc->wb_tcand_id)
6962a814908STejun Heo 		wbc->wb_tcand_bytes += bytes;
6972a814908STejun Heo 	else
6982a814908STejun Heo 		wbc->wb_tcand_bytes -= min(bytes, wbc->wb_tcand_bytes);
6992a814908STejun Heo }
7005aa2a96bSTejun Heo EXPORT_SYMBOL_GPL(wbc_account_io);
7012a814908STejun Heo 
7022a814908STejun Heo /**
703703c2708STejun Heo  * inode_congested - test whether an inode is congested
704703c2708STejun Heo  * @inode: inode to test for congestion
705703c2708STejun Heo  * @cong_bits: mask of WB_[a]sync_congested bits to test
706703c2708STejun Heo  *
707703c2708STejun Heo  * Tests whether @inode is congested.  @cong_bits is the mask of congestion
708703c2708STejun Heo  * bits to test and the return value is the mask of set bits.
709703c2708STejun Heo  *
710703c2708STejun Heo  * If cgroup writeback is enabled for @inode, the congestion state is
711703c2708STejun Heo  * determined by whether the cgwb (cgroup bdi_writeback) for the blkcg
712703c2708STejun Heo  * associated with @inode is congested; otherwise, the root wb's congestion
713703c2708STejun Heo  * state is used.
714703c2708STejun Heo  */
715703c2708STejun Heo int inode_congested(struct inode *inode, int cong_bits)
716703c2708STejun Heo {
7175cb8b824STejun Heo 	/*
7185cb8b824STejun Heo 	 * Once set, ->i_wb never becomes NULL while the inode is alive.
7195cb8b824STejun Heo 	 * Start transaction iff ->i_wb is visible.
7205cb8b824STejun Heo 	 */
721aaa2cacfSTejun Heo 	if (inode && inode_to_wb_is_valid(inode)) {
7225cb8b824STejun Heo 		struct bdi_writeback *wb;
7235cb8b824STejun Heo 		bool locked, congested;
7245cb8b824STejun Heo 
7255cb8b824STejun Heo 		wb = unlocked_inode_to_wb_begin(inode, &locked);
7265cb8b824STejun Heo 		congested = wb_congested(wb, cong_bits);
7275cb8b824STejun Heo 		unlocked_inode_to_wb_end(inode, locked);
7285cb8b824STejun Heo 		return congested;
729703c2708STejun Heo 	}
730703c2708STejun Heo 
731703c2708STejun Heo 	return wb_congested(&inode_to_bdi(inode)->wb, cong_bits);
732703c2708STejun Heo }
733703c2708STejun Heo EXPORT_SYMBOL_GPL(inode_congested);
734703c2708STejun Heo 
735f2b65121STejun Heo /**
736f2b65121STejun Heo  * wb_split_bdi_pages - split nr_pages to write according to bandwidth
737f2b65121STejun Heo  * @wb: target bdi_writeback to split @nr_pages to
738f2b65121STejun Heo  * @nr_pages: number of pages to write for the whole bdi
739f2b65121STejun Heo  *
740f2b65121STejun Heo  * Split @wb's portion of @nr_pages according to @wb's write bandwidth in
741f2b65121STejun Heo  * relation to the total write bandwidth of all wb's w/ dirty inodes on
742f2b65121STejun Heo  * @wb->bdi.
743f2b65121STejun Heo  */
744f2b65121STejun Heo static long wb_split_bdi_pages(struct bdi_writeback *wb, long nr_pages)
745f2b65121STejun Heo {
746f2b65121STejun Heo 	unsigned long this_bw = wb->avg_write_bandwidth;
747f2b65121STejun Heo 	unsigned long tot_bw = atomic_long_read(&wb->bdi->tot_write_bandwidth);
748f2b65121STejun Heo 
749f2b65121STejun Heo 	if (nr_pages == LONG_MAX)
750f2b65121STejun Heo 		return LONG_MAX;
751f2b65121STejun Heo 
752f2b65121STejun Heo 	/*
753f2b65121STejun Heo 	 * This may be called on clean wb's and proportional distribution
754f2b65121STejun Heo 	 * may not make sense, just use the original @nr_pages in those
755f2b65121STejun Heo 	 * cases.  In general, we wanna err on the side of writing more.
756f2b65121STejun Heo 	 */
757f2b65121STejun Heo 	if (!tot_bw || this_bw >= tot_bw)
758f2b65121STejun Heo 		return nr_pages;
759f2b65121STejun Heo 	else
760f2b65121STejun Heo 		return DIV_ROUND_UP_ULL((u64)nr_pages * this_bw, tot_bw);
761f2b65121STejun Heo }
762f2b65121STejun Heo 
763db125360STejun Heo /**
764db125360STejun Heo  * bdi_split_work_to_wbs - split a wb_writeback_work to all wb's of a bdi
765db125360STejun Heo  * @bdi: target backing_dev_info
766db125360STejun Heo  * @base_work: wb_writeback_work to issue
767db125360STejun Heo  * @skip_if_busy: skip wb's which already have writeback in progress
768db125360STejun Heo  *
769db125360STejun Heo  * Split and issue @base_work to all wb's (bdi_writeback's) of @bdi which
770db125360STejun Heo  * have dirty inodes.  If @base_work->nr_page isn't %LONG_MAX, it's
771db125360STejun Heo  * distributed to the busy wbs according to each wb's proportion in the
772db125360STejun Heo  * total active write bandwidth of @bdi.
773db125360STejun Heo  */
774db125360STejun Heo static void bdi_split_work_to_wbs(struct backing_dev_info *bdi,
775db125360STejun Heo 				  struct wb_writeback_work *base_work,
776db125360STejun Heo 				  bool skip_if_busy)
777db125360STejun Heo {
7781ed8d48cSTejun Heo 	int next_memcg_id = 0;
779db125360STejun Heo 	struct bdi_writeback *wb;
780db125360STejun Heo 	struct wb_iter iter;
781db125360STejun Heo 
782db125360STejun Heo 	might_sleep();
783db125360STejun Heo 
784db125360STejun Heo 	if (!bdi_has_dirty_io(bdi))
785db125360STejun Heo 		return;
786db125360STejun Heo restart:
787db125360STejun Heo 	rcu_read_lock();
7881ed8d48cSTejun Heo 	bdi_for_each_wb(wb, bdi, &iter, next_memcg_id) {
7898a1270cdSTejun Heo 		DEFINE_WB_COMPLETION_ONSTACK(fallback_work_done);
7908a1270cdSTejun Heo 		struct wb_writeback_work fallback_work;
7918a1270cdSTejun Heo 		struct wb_writeback_work *work;
7928a1270cdSTejun Heo 		long nr_pages;
7938a1270cdSTejun Heo 
794db125360STejun Heo 		if (!wb_has_dirty_io(wb) ||
795db125360STejun Heo 		    (skip_if_busy && writeback_in_progress(wb)))
796db125360STejun Heo 			continue;
797db125360STejun Heo 
7988a1270cdSTejun Heo 		nr_pages = wb_split_bdi_pages(wb, base_work->nr_pages);
7998a1270cdSTejun Heo 
8008a1270cdSTejun Heo 		work = kmalloc(sizeof(*work), GFP_ATOMIC);
8018a1270cdSTejun Heo 		if (work) {
8028a1270cdSTejun Heo 			*work = *base_work;
8038a1270cdSTejun Heo 			work->nr_pages = nr_pages;
8048a1270cdSTejun Heo 			work->auto_free = 1;
8058a1270cdSTejun Heo 			wb_queue_work(wb, work);
8068a1270cdSTejun Heo 			continue;
8078a1270cdSTejun Heo 		}
8088a1270cdSTejun Heo 
8098a1270cdSTejun Heo 		/* alloc failed, execute synchronously using on-stack fallback */
8108a1270cdSTejun Heo 		work = &fallback_work;
8118a1270cdSTejun Heo 		*work = *base_work;
8128a1270cdSTejun Heo 		work->nr_pages = nr_pages;
8138a1270cdSTejun Heo 		work->auto_free = 0;
8148a1270cdSTejun Heo 		work->done = &fallback_work_done;
8158a1270cdSTejun Heo 
8168a1270cdSTejun Heo 		wb_queue_work(wb, work);
8178a1270cdSTejun Heo 
8181ed8d48cSTejun Heo 		next_memcg_id = wb->memcg_css->id + 1;
819db125360STejun Heo 		rcu_read_unlock();
8208a1270cdSTejun Heo 		wb_wait_for_completion(bdi, &fallback_work_done);
821db125360STejun Heo 		goto restart;
822db125360STejun Heo 	}
823db125360STejun Heo 	rcu_read_unlock();
824db125360STejun Heo }
825db125360STejun Heo 
826f2b65121STejun Heo #else	/* CONFIG_CGROUP_WRITEBACK */
827f2b65121STejun Heo 
82887e1d789STejun Heo static struct bdi_writeback *
82987e1d789STejun Heo locked_inode_to_wb_and_lock_list(struct inode *inode)
83087e1d789STejun Heo 	__releases(&inode->i_lock)
83187e1d789STejun Heo 	__acquires(&wb->list_lock)
83287e1d789STejun Heo {
83387e1d789STejun Heo 	struct bdi_writeback *wb = inode_to_wb(inode);
83487e1d789STejun Heo 
83587e1d789STejun Heo 	spin_unlock(&inode->i_lock);
83687e1d789STejun Heo 	spin_lock(&wb->list_lock);
83787e1d789STejun Heo 	return wb;
83887e1d789STejun Heo }
83987e1d789STejun Heo 
84087e1d789STejun Heo static struct bdi_writeback *inode_to_wb_and_lock_list(struct inode *inode)
84187e1d789STejun Heo 	__acquires(&wb->list_lock)
84287e1d789STejun Heo {
84387e1d789STejun Heo 	struct bdi_writeback *wb = inode_to_wb(inode);
84487e1d789STejun Heo 
84587e1d789STejun Heo 	spin_lock(&wb->list_lock);
84687e1d789STejun Heo 	return wb;
84787e1d789STejun Heo }
84887e1d789STejun Heo 
849f2b65121STejun Heo static long wb_split_bdi_pages(struct bdi_writeback *wb, long nr_pages)
850f2b65121STejun Heo {
851f2b65121STejun Heo 	return nr_pages;
852f2b65121STejun Heo }
853f2b65121STejun Heo 
854db125360STejun Heo static void bdi_split_work_to_wbs(struct backing_dev_info *bdi,
855db125360STejun Heo 				  struct wb_writeback_work *base_work,
856db125360STejun Heo 				  bool skip_if_busy)
857db125360STejun Heo {
858db125360STejun Heo 	might_sleep();
859db125360STejun Heo 
860db125360STejun Heo 	if (bdi_has_dirty_io(bdi) &&
861db125360STejun Heo 	    (!skip_if_busy || !writeback_in_progress(&bdi->wb))) {
862db125360STejun Heo 		base_work->auto_free = 0;
863db125360STejun Heo 		wb_queue_work(&bdi->wb, base_work);
864db125360STejun Heo 	}
865db125360STejun Heo }
866db125360STejun Heo 
867703c2708STejun Heo #endif	/* CONFIG_CGROUP_WRITEBACK */
868703c2708STejun Heo 
869c00ddad3STejun Heo void wb_start_writeback(struct bdi_writeback *wb, long nr_pages,
870c00ddad3STejun Heo 			bool range_cyclic, enum wb_reason reason)
871b6e51316SJens Axboe {
872c00ddad3STejun Heo 	struct wb_writeback_work *work;
873c00ddad3STejun Heo 
874c00ddad3STejun Heo 	if (!wb_has_dirty_io(wb))
875c00ddad3STejun Heo 		return;
876c00ddad3STejun Heo 
877c00ddad3STejun Heo 	/*
878c00ddad3STejun Heo 	 * This is WB_SYNC_NONE writeback, so if allocation fails just
879c00ddad3STejun Heo 	 * wakeup the thread for old dirty data writeback
880c00ddad3STejun Heo 	 */
881c00ddad3STejun Heo 	work = kzalloc(sizeof(*work), GFP_ATOMIC);
882c00ddad3STejun Heo 	if (!work) {
883c00ddad3STejun Heo 		trace_writeback_nowork(wb->bdi);
884c00ddad3STejun Heo 		wb_wakeup(wb);
885c00ddad3STejun Heo 		return;
886c00ddad3STejun Heo 	}
887c00ddad3STejun Heo 
888c00ddad3STejun Heo 	work->sync_mode	= WB_SYNC_NONE;
889c00ddad3STejun Heo 	work->nr_pages	= nr_pages;
890c00ddad3STejun Heo 	work->range_cyclic = range_cyclic;
891c00ddad3STejun Heo 	work->reason	= reason;
892ac7b19a3STejun Heo 	work->auto_free	= 1;
893c00ddad3STejun Heo 
894c00ddad3STejun Heo 	wb_queue_work(wb, work);
895d3ddec76SWu Fengguang }
896d3ddec76SWu Fengguang 
897c5444198SChristoph Hellwig /**
8989ecf4866STejun Heo  * wb_start_background_writeback - start background writeback
8999ecf4866STejun Heo  * @wb: bdi_writback to write from
900c5444198SChristoph Hellwig  *
901c5444198SChristoph Hellwig  * Description:
9026585027aSJan Kara  *   This makes sure WB_SYNC_NONE background writeback happens. When
9039ecf4866STejun Heo  *   this function returns, it is only guaranteed that for given wb
9046585027aSJan Kara  *   some IO is happening if we are over background dirty threshold.
9056585027aSJan Kara  *   Caller need not hold sb s_umount semaphore.
906c5444198SChristoph Hellwig  */
9079ecf4866STejun Heo void wb_start_background_writeback(struct bdi_writeback *wb)
908c5444198SChristoph Hellwig {
9096585027aSJan Kara 	/*
9106585027aSJan Kara 	 * We just wake up the flusher thread. It will perform background
9116585027aSJan Kara 	 * writeback as soon as there is no other work to do.
9126585027aSJan Kara 	 */
9139ecf4866STejun Heo 	trace_writeback_wake_background(wb->bdi);
9149ecf4866STejun Heo 	wb_wakeup(wb);
9151da177e4SLinus Torvalds }
9161da177e4SLinus Torvalds 
9171da177e4SLinus Torvalds /*
918a66979abSDave Chinner  * Remove the inode from the writeback list it is on.
919a66979abSDave Chinner  */
920a66979abSDave Chinner void inode_wb_list_del(struct inode *inode)
921a66979abSDave Chinner {
92287e1d789STejun Heo 	struct bdi_writeback *wb;
923a66979abSDave Chinner 
92487e1d789STejun Heo 	wb = inode_to_wb_and_lock_list(inode);
925d6c10f1fSTejun Heo 	inode_wb_list_del_locked(inode, wb);
92652ebea74STejun Heo 	spin_unlock(&wb->list_lock);
927f758eeabSChristoph Hellwig }
928a66979abSDave Chinner 
929a66979abSDave Chinner /*
9306610a0bcSAndrew Morton  * Redirty an inode: set its when-it-was dirtied timestamp and move it to the
9316610a0bcSAndrew Morton  * furthest end of its superblock's dirty-inode list.
9326610a0bcSAndrew Morton  *
9336610a0bcSAndrew Morton  * Before stamping the inode's ->dirtied_when, we check to see whether it is
93466f3b8e2SJens Axboe  * already the most-recently-dirtied inode on the b_dirty list.  If that is
9356610a0bcSAndrew Morton  * the case then the inode must have been redirtied while it was being written
9366610a0bcSAndrew Morton  * out and we don't reset its dirtied_when.
9376610a0bcSAndrew Morton  */
938f758eeabSChristoph Hellwig static void redirty_tail(struct inode *inode, struct bdi_writeback *wb)
9396610a0bcSAndrew Morton {
94003ba3782SJens Axboe 	if (!list_empty(&wb->b_dirty)) {
94166f3b8e2SJens Axboe 		struct inode *tail;
9426610a0bcSAndrew Morton 
9437ccf19a8SNick Piggin 		tail = wb_inode(wb->b_dirty.next);
94466f3b8e2SJens Axboe 		if (time_before(inode->dirtied_when, tail->dirtied_when))
9456610a0bcSAndrew Morton 			inode->dirtied_when = jiffies;
9466610a0bcSAndrew Morton 	}
947d6c10f1fSTejun Heo 	inode_wb_list_move_locked(inode, wb, &wb->b_dirty);
9486610a0bcSAndrew Morton }
9496610a0bcSAndrew Morton 
9506610a0bcSAndrew Morton /*
95166f3b8e2SJens Axboe  * requeue inode for re-scanning after bdi->b_io list is exhausted.
952c986d1e2SAndrew Morton  */
953f758eeabSChristoph Hellwig static void requeue_io(struct inode *inode, struct bdi_writeback *wb)
954c986d1e2SAndrew Morton {
955d6c10f1fSTejun Heo 	inode_wb_list_move_locked(inode, wb, &wb->b_more_io);
956c986d1e2SAndrew Morton }
957c986d1e2SAndrew Morton 
9581c0eeaf5SJoern Engel static void inode_sync_complete(struct inode *inode)
9591c0eeaf5SJoern Engel {
960365b94aeSJan Kara 	inode->i_state &= ~I_SYNC;
9614eff96ddSJan Kara 	/* If inode is clean an unused, put it into LRU now... */
9624eff96ddSJan Kara 	inode_add_lru(inode);
963365b94aeSJan Kara 	/* Waiters must see I_SYNC cleared before being woken up */
9641c0eeaf5SJoern Engel 	smp_mb();
9651c0eeaf5SJoern Engel 	wake_up_bit(&inode->i_state, __I_SYNC);
9661c0eeaf5SJoern Engel }
9671c0eeaf5SJoern Engel 
968d2caa3c5SJeff Layton static bool inode_dirtied_after(struct inode *inode, unsigned long t)
969d2caa3c5SJeff Layton {
970d2caa3c5SJeff Layton 	bool ret = time_after(inode->dirtied_when, t);
971d2caa3c5SJeff Layton #ifndef CONFIG_64BIT
972d2caa3c5SJeff Layton 	/*
973d2caa3c5SJeff Layton 	 * For inodes being constantly redirtied, dirtied_when can get stuck.
974d2caa3c5SJeff Layton 	 * It _appears_ to be in the future, but is actually in distant past.
975d2caa3c5SJeff Layton 	 * This test is necessary to prevent such wrapped-around relative times
9765b0830cbSJens Axboe 	 * from permanently stopping the whole bdi writeback.
977d2caa3c5SJeff Layton 	 */
978d2caa3c5SJeff Layton 	ret = ret && time_before_eq(inode->dirtied_when, jiffies);
979d2caa3c5SJeff Layton #endif
980d2caa3c5SJeff Layton 	return ret;
981d2caa3c5SJeff Layton }
982d2caa3c5SJeff Layton 
9830ae45f63STheodore Ts'o #define EXPIRE_DIRTY_ATIME 0x0001
9840ae45f63STheodore Ts'o 
985c986d1e2SAndrew Morton /*
9860e2f2b23SWang Sheng-Hui  * Move expired (dirtied before work->older_than_this) dirty inodes from
987697e6fedSJan Kara  * @delaying_queue to @dispatch_queue.
9882c136579SFengguang Wu  */
989e84d0a4fSWu Fengguang static int move_expired_inodes(struct list_head *delaying_queue,
9902c136579SFengguang Wu 			       struct list_head *dispatch_queue,
9910ae45f63STheodore Ts'o 			       int flags,
992ad4e38ddSCurt Wohlgemuth 			       struct wb_writeback_work *work)
9932c136579SFengguang Wu {
9940ae45f63STheodore Ts'o 	unsigned long *older_than_this = NULL;
9950ae45f63STheodore Ts'o 	unsigned long expire_time;
9965c03449dSShaohua Li 	LIST_HEAD(tmp);
9975c03449dSShaohua Li 	struct list_head *pos, *node;
998cf137307SJens Axboe 	struct super_block *sb = NULL;
9995c03449dSShaohua Li 	struct inode *inode;
1000cf137307SJens Axboe 	int do_sb_sort = 0;
1001e84d0a4fSWu Fengguang 	int moved = 0;
10025c03449dSShaohua Li 
10030ae45f63STheodore Ts'o 	if ((flags & EXPIRE_DIRTY_ATIME) == 0)
10040ae45f63STheodore Ts'o 		older_than_this = work->older_than_this;
1005a2f48706STheodore Ts'o 	else if (!work->for_sync) {
1006a2f48706STheodore Ts'o 		expire_time = jiffies - (dirtytime_expire_interval * HZ);
10070ae45f63STheodore Ts'o 		older_than_this = &expire_time;
10080ae45f63STheodore Ts'o 	}
10092c136579SFengguang Wu 	while (!list_empty(delaying_queue)) {
10107ccf19a8SNick Piggin 		inode = wb_inode(delaying_queue->prev);
10110ae45f63STheodore Ts'o 		if (older_than_this &&
10120ae45f63STheodore Ts'o 		    inode_dirtied_after(inode, *older_than_this))
10132c136579SFengguang Wu 			break;
1014a8855990SJan Kara 		list_move(&inode->i_wb_list, &tmp);
1015a8855990SJan Kara 		moved++;
10160ae45f63STheodore Ts'o 		if (flags & EXPIRE_DIRTY_ATIME)
10170ae45f63STheodore Ts'o 			set_bit(__I_DIRTY_TIME_EXPIRED, &inode->i_state);
1018a8855990SJan Kara 		if (sb_is_blkdev_sb(inode->i_sb))
1019a8855990SJan Kara 			continue;
1020cf137307SJens Axboe 		if (sb && sb != inode->i_sb)
1021cf137307SJens Axboe 			do_sb_sort = 1;
1022cf137307SJens Axboe 		sb = inode->i_sb;
10235c03449dSShaohua Li 	}
10245c03449dSShaohua Li 
1025cf137307SJens Axboe 	/* just one sb in list, splice to dispatch_queue and we're done */
1026cf137307SJens Axboe 	if (!do_sb_sort) {
1027cf137307SJens Axboe 		list_splice(&tmp, dispatch_queue);
1028e84d0a4fSWu Fengguang 		goto out;
1029cf137307SJens Axboe 	}
1030cf137307SJens Axboe 
10315c03449dSShaohua Li 	/* Move inodes from one superblock together */
10325c03449dSShaohua Li 	while (!list_empty(&tmp)) {
10337ccf19a8SNick Piggin 		sb = wb_inode(tmp.prev)->i_sb;
10345c03449dSShaohua Li 		list_for_each_prev_safe(pos, node, &tmp) {
10357ccf19a8SNick Piggin 			inode = wb_inode(pos);
10365c03449dSShaohua Li 			if (inode->i_sb == sb)
10377ccf19a8SNick Piggin 				list_move(&inode->i_wb_list, dispatch_queue);
10382c136579SFengguang Wu 		}
10392c136579SFengguang Wu 	}
1040e84d0a4fSWu Fengguang out:
1041e84d0a4fSWu Fengguang 	return moved;
10425c03449dSShaohua Li }
10432c136579SFengguang Wu 
10442c136579SFengguang Wu /*
10452c136579SFengguang Wu  * Queue all expired dirty inodes for io, eldest first.
10464ea879b9SWu Fengguang  * Before
10474ea879b9SWu Fengguang  *         newly dirtied     b_dirty    b_io    b_more_io
10484ea879b9SWu Fengguang  *         =============>    gf         edc     BA
10494ea879b9SWu Fengguang  * After
10504ea879b9SWu Fengguang  *         newly dirtied     b_dirty    b_io    b_more_io
10514ea879b9SWu Fengguang  *         =============>    g          fBAedc
10524ea879b9SWu Fengguang  *                                           |
10534ea879b9SWu Fengguang  *                                           +--> dequeue for IO
10542c136579SFengguang Wu  */
1055ad4e38ddSCurt Wohlgemuth static void queue_io(struct bdi_writeback *wb, struct wb_writeback_work *work)
10562c136579SFengguang Wu {
1057e84d0a4fSWu Fengguang 	int moved;
10580ae45f63STheodore Ts'o 
1059f758eeabSChristoph Hellwig 	assert_spin_locked(&wb->list_lock);
10604ea879b9SWu Fengguang 	list_splice_init(&wb->b_more_io, &wb->b_io);
10610ae45f63STheodore Ts'o 	moved = move_expired_inodes(&wb->b_dirty, &wb->b_io, 0, work);
10620ae45f63STheodore Ts'o 	moved += move_expired_inodes(&wb->b_dirty_time, &wb->b_io,
10630ae45f63STheodore Ts'o 				     EXPIRE_DIRTY_ATIME, work);
1064d6c10f1fSTejun Heo 	if (moved)
1065d6c10f1fSTejun Heo 		wb_io_lists_populated(wb);
1066ad4e38ddSCurt Wohlgemuth 	trace_writeback_queue_io(wb, work, moved);
106766f3b8e2SJens Axboe }
106866f3b8e2SJens Axboe 
1069a9185b41SChristoph Hellwig static int write_inode(struct inode *inode, struct writeback_control *wbc)
107066f3b8e2SJens Axboe {
10719fb0a7daSTejun Heo 	int ret;
10729fb0a7daSTejun Heo 
10739fb0a7daSTejun Heo 	if (inode->i_sb->s_op->write_inode && !is_bad_inode(inode)) {
10749fb0a7daSTejun Heo 		trace_writeback_write_inode_start(inode, wbc);
10759fb0a7daSTejun Heo 		ret = inode->i_sb->s_op->write_inode(inode, wbc);
10769fb0a7daSTejun Heo 		trace_writeback_write_inode(inode, wbc);
10779fb0a7daSTejun Heo 		return ret;
10789fb0a7daSTejun Heo 	}
107903ba3782SJens Axboe 	return 0;
108066f3b8e2SJens Axboe }
108108d8e974SFengguang Wu 
10822c136579SFengguang Wu /*
1083169ebd90SJan Kara  * Wait for writeback on an inode to complete. Called with i_lock held.
1084169ebd90SJan Kara  * Caller must make sure inode cannot go away when we drop i_lock.
108501c03194SChristoph Hellwig  */
1086169ebd90SJan Kara static void __inode_wait_for_writeback(struct inode *inode)
1087169ebd90SJan Kara 	__releases(inode->i_lock)
1088169ebd90SJan Kara 	__acquires(inode->i_lock)
108901c03194SChristoph Hellwig {
109001c03194SChristoph Hellwig 	DEFINE_WAIT_BIT(wq, &inode->i_state, __I_SYNC);
109101c03194SChristoph Hellwig 	wait_queue_head_t *wqh;
109201c03194SChristoph Hellwig 
109301c03194SChristoph Hellwig 	wqh = bit_waitqueue(&inode->i_state, __I_SYNC);
109458a9d3d8SRichard Kennedy 	while (inode->i_state & I_SYNC) {
1095250df6edSDave Chinner 		spin_unlock(&inode->i_lock);
109674316201SNeilBrown 		__wait_on_bit(wqh, &wq, bit_wait,
109774316201SNeilBrown 			      TASK_UNINTERRUPTIBLE);
1098250df6edSDave Chinner 		spin_lock(&inode->i_lock);
109958a9d3d8SRichard Kennedy 	}
110001c03194SChristoph Hellwig }
110101c03194SChristoph Hellwig 
110201c03194SChristoph Hellwig /*
1103169ebd90SJan Kara  * Wait for writeback on an inode to complete. Caller must have inode pinned.
1104169ebd90SJan Kara  */
1105169ebd90SJan Kara void inode_wait_for_writeback(struct inode *inode)
1106169ebd90SJan Kara {
1107169ebd90SJan Kara 	spin_lock(&inode->i_lock);
1108169ebd90SJan Kara 	__inode_wait_for_writeback(inode);
1109169ebd90SJan Kara 	spin_unlock(&inode->i_lock);
1110169ebd90SJan Kara }
1111169ebd90SJan Kara 
1112169ebd90SJan Kara /*
1113169ebd90SJan Kara  * Sleep until I_SYNC is cleared. This function must be called with i_lock
1114169ebd90SJan Kara  * held and drops it. It is aimed for callers not holding any inode reference
1115169ebd90SJan Kara  * so once i_lock is dropped, inode can go away.
1116169ebd90SJan Kara  */
1117169ebd90SJan Kara static void inode_sleep_on_writeback(struct inode *inode)
1118169ebd90SJan Kara 	__releases(inode->i_lock)
1119169ebd90SJan Kara {
1120169ebd90SJan Kara 	DEFINE_WAIT(wait);
1121169ebd90SJan Kara 	wait_queue_head_t *wqh = bit_waitqueue(&inode->i_state, __I_SYNC);
1122169ebd90SJan Kara 	int sleep;
1123169ebd90SJan Kara 
1124169ebd90SJan Kara 	prepare_to_wait(wqh, &wait, TASK_UNINTERRUPTIBLE);
1125169ebd90SJan Kara 	sleep = inode->i_state & I_SYNC;
1126169ebd90SJan Kara 	spin_unlock(&inode->i_lock);
1127169ebd90SJan Kara 	if (sleep)
1128169ebd90SJan Kara 		schedule();
1129169ebd90SJan Kara 	finish_wait(wqh, &wait);
1130169ebd90SJan Kara }
1131169ebd90SJan Kara 
1132169ebd90SJan Kara /*
1133ccb26b5aSJan Kara  * Find proper writeback list for the inode depending on its current state and
1134ccb26b5aSJan Kara  * possibly also change of its state while we were doing writeback.  Here we
1135ccb26b5aSJan Kara  * handle things such as livelock prevention or fairness of writeback among
1136ccb26b5aSJan Kara  * inodes. This function can be called only by flusher thread - noone else
1137ccb26b5aSJan Kara  * processes all inodes in writeback lists and requeueing inodes behind flusher
1138ccb26b5aSJan Kara  * thread's back can have unexpected consequences.
1139ccb26b5aSJan Kara  */
1140ccb26b5aSJan Kara static void requeue_inode(struct inode *inode, struct bdi_writeback *wb,
1141ccb26b5aSJan Kara 			  struct writeback_control *wbc)
1142ccb26b5aSJan Kara {
1143ccb26b5aSJan Kara 	if (inode->i_state & I_FREEING)
1144ccb26b5aSJan Kara 		return;
1145ccb26b5aSJan Kara 
1146ccb26b5aSJan Kara 	/*
1147ccb26b5aSJan Kara 	 * Sync livelock prevention. Each inode is tagged and synced in one
1148ccb26b5aSJan Kara 	 * shot. If still dirty, it will be redirty_tail()'ed below.  Update
1149ccb26b5aSJan Kara 	 * the dirty time to prevent enqueue and sync it again.
1150ccb26b5aSJan Kara 	 */
1151ccb26b5aSJan Kara 	if ((inode->i_state & I_DIRTY) &&
1152ccb26b5aSJan Kara 	    (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages))
1153ccb26b5aSJan Kara 		inode->dirtied_when = jiffies;
1154ccb26b5aSJan Kara 
11554f8ad655SJan Kara 	if (wbc->pages_skipped) {
11564f8ad655SJan Kara 		/*
11574f8ad655SJan Kara 		 * writeback is not making progress due to locked
11584f8ad655SJan Kara 		 * buffers. Skip this inode for now.
11594f8ad655SJan Kara 		 */
11604f8ad655SJan Kara 		redirty_tail(inode, wb);
11614f8ad655SJan Kara 		return;
11624f8ad655SJan Kara 	}
11634f8ad655SJan Kara 
1164ccb26b5aSJan Kara 	if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY)) {
1165ccb26b5aSJan Kara 		/*
1166ccb26b5aSJan Kara 		 * We didn't write back all the pages.  nfs_writepages()
1167ccb26b5aSJan Kara 		 * sometimes bales out without doing anything.
1168ccb26b5aSJan Kara 		 */
1169ccb26b5aSJan Kara 		if (wbc->nr_to_write <= 0) {
1170ccb26b5aSJan Kara 			/* Slice used up. Queue for next turn. */
1171ccb26b5aSJan Kara 			requeue_io(inode, wb);
1172ccb26b5aSJan Kara 		} else {
1173ccb26b5aSJan Kara 			/*
1174ccb26b5aSJan Kara 			 * Writeback blocked by something other than
1175ccb26b5aSJan Kara 			 * congestion. Delay the inode for some time to
1176ccb26b5aSJan Kara 			 * avoid spinning on the CPU (100% iowait)
1177ccb26b5aSJan Kara 			 * retrying writeback of the dirty page/inode
1178ccb26b5aSJan Kara 			 * that cannot be performed immediately.
1179ccb26b5aSJan Kara 			 */
1180ccb26b5aSJan Kara 			redirty_tail(inode, wb);
1181ccb26b5aSJan Kara 		}
1182ccb26b5aSJan Kara 	} else if (inode->i_state & I_DIRTY) {
1183ccb26b5aSJan Kara 		/*
1184ccb26b5aSJan Kara 		 * Filesystems can dirty the inode during writeback operations,
1185ccb26b5aSJan Kara 		 * such as delayed allocation during submission or metadata
1186ccb26b5aSJan Kara 		 * updates after data IO completion.
1187ccb26b5aSJan Kara 		 */
1188ccb26b5aSJan Kara 		redirty_tail(inode, wb);
11890ae45f63STheodore Ts'o 	} else if (inode->i_state & I_DIRTY_TIME) {
1190a2f48706STheodore Ts'o 		inode->dirtied_when = jiffies;
1191d6c10f1fSTejun Heo 		inode_wb_list_move_locked(inode, wb, &wb->b_dirty_time);
1192ccb26b5aSJan Kara 	} else {
1193ccb26b5aSJan Kara 		/* The inode is clean. Remove from writeback lists. */
1194d6c10f1fSTejun Heo 		inode_wb_list_del_locked(inode, wb);
1195ccb26b5aSJan Kara 	}
1196ccb26b5aSJan Kara }
1197ccb26b5aSJan Kara 
1198ccb26b5aSJan Kara /*
11994f8ad655SJan Kara  * Write out an inode and its dirty pages. Do not update the writeback list
12004f8ad655SJan Kara  * linkage. That is left to the caller. The caller is also responsible for
12014f8ad655SJan Kara  * setting I_SYNC flag and calling inode_sync_complete() to clear it.
12021da177e4SLinus Torvalds  */
12031da177e4SLinus Torvalds static int
1204cd8ed2a4SYan Hong __writeback_single_inode(struct inode *inode, struct writeback_control *wbc)
12051da177e4SLinus Torvalds {
12061da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
1207251d6a47SWu Fengguang 	long nr_to_write = wbc->nr_to_write;
120801c03194SChristoph Hellwig 	unsigned dirty;
12091da177e4SLinus Torvalds 	int ret;
12101da177e4SLinus Torvalds 
12114f8ad655SJan Kara 	WARN_ON(!(inode->i_state & I_SYNC));
12121da177e4SLinus Torvalds 
12139fb0a7daSTejun Heo 	trace_writeback_single_inode_start(inode, wbc, nr_to_write);
12149fb0a7daSTejun Heo 
12151da177e4SLinus Torvalds 	ret = do_writepages(mapping, wbc);
12161da177e4SLinus Torvalds 
121726821ed4SChristoph Hellwig 	/*
121826821ed4SChristoph Hellwig 	 * Make sure to wait on the data before writing out the metadata.
121926821ed4SChristoph Hellwig 	 * This is important for filesystems that modify metadata on data
12207747bd4bSDave Chinner 	 * I/O completion. We don't do it for sync(2) writeback because it has a
12217747bd4bSDave Chinner 	 * separate, external IO completion path and ->sync_fs for guaranteeing
12227747bd4bSDave Chinner 	 * inode metadata is written back correctly.
122326821ed4SChristoph Hellwig 	 */
12247747bd4bSDave Chinner 	if (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync) {
122526821ed4SChristoph Hellwig 		int err = filemap_fdatawait(mapping);
12261da177e4SLinus Torvalds 		if (ret == 0)
12271da177e4SLinus Torvalds 			ret = err;
12281da177e4SLinus Torvalds 	}
12291da177e4SLinus Torvalds 
12305547e8aaSDmitry Monakhov 	/*
12315547e8aaSDmitry Monakhov 	 * Some filesystems may redirty the inode during the writeback
12325547e8aaSDmitry Monakhov 	 * due to delalloc, clear dirty metadata flags right before
12335547e8aaSDmitry Monakhov 	 * write_inode()
12345547e8aaSDmitry Monakhov 	 */
1235250df6edSDave Chinner 	spin_lock(&inode->i_lock);
12369c6ac78eSTejun Heo 
12375547e8aaSDmitry Monakhov 	dirty = inode->i_state & I_DIRTY;
1238a2f48706STheodore Ts'o 	if (inode->i_state & I_DIRTY_TIME) {
1239a2f48706STheodore Ts'o 		if ((dirty & (I_DIRTY_SYNC | I_DIRTY_DATASYNC)) ||
1240a2f48706STheodore Ts'o 		    unlikely(inode->i_state & I_DIRTY_TIME_EXPIRED) ||
1241a2f48706STheodore Ts'o 		    unlikely(time_after(jiffies,
1242a2f48706STheodore Ts'o 					(inode->dirtied_time_when +
1243a2f48706STheodore Ts'o 					 dirtytime_expire_interval * HZ)))) {
12440ae45f63STheodore Ts'o 			dirty |= I_DIRTY_TIME | I_DIRTY_TIME_EXPIRED;
12450ae45f63STheodore Ts'o 			trace_writeback_lazytime(inode);
12460ae45f63STheodore Ts'o 		}
1247a2f48706STheodore Ts'o 	} else
1248a2f48706STheodore Ts'o 		inode->i_state &= ~I_DIRTY_TIME_EXPIRED;
12490ae45f63STheodore Ts'o 	inode->i_state &= ~dirty;
12509c6ac78eSTejun Heo 
12519c6ac78eSTejun Heo 	/*
12529c6ac78eSTejun Heo 	 * Paired with smp_mb() in __mark_inode_dirty().  This allows
12539c6ac78eSTejun Heo 	 * __mark_inode_dirty() to test i_state without grabbing i_lock -
12549c6ac78eSTejun Heo 	 * either they see the I_DIRTY bits cleared or we see the dirtied
12559c6ac78eSTejun Heo 	 * inode.
12569c6ac78eSTejun Heo 	 *
12579c6ac78eSTejun Heo 	 * I_DIRTY_PAGES is always cleared together above even if @mapping
12589c6ac78eSTejun Heo 	 * still has dirty pages.  The flag is reinstated after smp_mb() if
12599c6ac78eSTejun Heo 	 * necessary.  This guarantees that either __mark_inode_dirty()
12609c6ac78eSTejun Heo 	 * sees clear I_DIRTY_PAGES or we see PAGECACHE_TAG_DIRTY.
12619c6ac78eSTejun Heo 	 */
12629c6ac78eSTejun Heo 	smp_mb();
12639c6ac78eSTejun Heo 
12649c6ac78eSTejun Heo 	if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
12659c6ac78eSTejun Heo 		inode->i_state |= I_DIRTY_PAGES;
12669c6ac78eSTejun Heo 
1267250df6edSDave Chinner 	spin_unlock(&inode->i_lock);
12689c6ac78eSTejun Heo 
12690ae45f63STheodore Ts'o 	if (dirty & I_DIRTY_TIME)
12700ae45f63STheodore Ts'o 		mark_inode_dirty_sync(inode);
127126821ed4SChristoph Hellwig 	/* Don't write the inode if only I_DIRTY_PAGES was set */
12720ae45f63STheodore Ts'o 	if (dirty & ~I_DIRTY_PAGES) {
1273a9185b41SChristoph Hellwig 		int err = write_inode(inode, wbc);
12741da177e4SLinus Torvalds 		if (ret == 0)
12751da177e4SLinus Torvalds 			ret = err;
12761da177e4SLinus Torvalds 	}
12774f8ad655SJan Kara 	trace_writeback_single_inode(inode, wbc, nr_to_write);
12784f8ad655SJan Kara 	return ret;
12794f8ad655SJan Kara }
12804f8ad655SJan Kara 
12814f8ad655SJan Kara /*
12824f8ad655SJan Kara  * Write out an inode's dirty pages. Either the caller has an active reference
12834f8ad655SJan Kara  * on the inode or the inode has I_WILL_FREE set.
12844f8ad655SJan Kara  *
12854f8ad655SJan Kara  * This function is designed to be called for writing back one inode which
12864f8ad655SJan Kara  * we go e.g. from filesystem. Flusher thread uses __writeback_single_inode()
12874f8ad655SJan Kara  * and does more profound writeback list handling in writeback_sb_inodes().
12884f8ad655SJan Kara  */
12894f8ad655SJan Kara static int
12904f8ad655SJan Kara writeback_single_inode(struct inode *inode, struct bdi_writeback *wb,
12914f8ad655SJan Kara 		       struct writeback_control *wbc)
12924f8ad655SJan Kara {
12934f8ad655SJan Kara 	int ret = 0;
12944f8ad655SJan Kara 
12954f8ad655SJan Kara 	spin_lock(&inode->i_lock);
12964f8ad655SJan Kara 	if (!atomic_read(&inode->i_count))
12974f8ad655SJan Kara 		WARN_ON(!(inode->i_state & (I_WILL_FREE|I_FREEING)));
12984f8ad655SJan Kara 	else
12994f8ad655SJan Kara 		WARN_ON(inode->i_state & I_WILL_FREE);
13004f8ad655SJan Kara 
13014f8ad655SJan Kara 	if (inode->i_state & I_SYNC) {
13024f8ad655SJan Kara 		if (wbc->sync_mode != WB_SYNC_ALL)
13034f8ad655SJan Kara 			goto out;
13044f8ad655SJan Kara 		/*
1305169ebd90SJan Kara 		 * It's a data-integrity sync. We must wait. Since callers hold
1306169ebd90SJan Kara 		 * inode reference or inode has I_WILL_FREE set, it cannot go
1307169ebd90SJan Kara 		 * away under us.
13084f8ad655SJan Kara 		 */
1309169ebd90SJan Kara 		__inode_wait_for_writeback(inode);
13104f8ad655SJan Kara 	}
13114f8ad655SJan Kara 	WARN_ON(inode->i_state & I_SYNC);
13124f8ad655SJan Kara 	/*
1313f9b0e058SJan Kara 	 * Skip inode if it is clean and we have no outstanding writeback in
1314f9b0e058SJan Kara 	 * WB_SYNC_ALL mode. We don't want to mess with writeback lists in this
1315f9b0e058SJan Kara 	 * function since flusher thread may be doing for example sync in
1316f9b0e058SJan Kara 	 * parallel and if we move the inode, it could get skipped. So here we
1317f9b0e058SJan Kara 	 * make sure inode is on some writeback list and leave it there unless
1318f9b0e058SJan Kara 	 * we have completely cleaned the inode.
13194f8ad655SJan Kara 	 */
13200ae45f63STheodore Ts'o 	if (!(inode->i_state & I_DIRTY_ALL) &&
1321f9b0e058SJan Kara 	    (wbc->sync_mode != WB_SYNC_ALL ||
1322f9b0e058SJan Kara 	     !mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK)))
13234f8ad655SJan Kara 		goto out;
13244f8ad655SJan Kara 	inode->i_state |= I_SYNC;
1325b16b1debSTejun Heo 	wbc_attach_and_unlock_inode(wbc, inode);
13264f8ad655SJan Kara 
1327cd8ed2a4SYan Hong 	ret = __writeback_single_inode(inode, wbc);
13281da177e4SLinus Torvalds 
1329b16b1debSTejun Heo 	wbc_detach_inode(wbc);
1330f758eeabSChristoph Hellwig 	spin_lock(&wb->list_lock);
1331250df6edSDave Chinner 	spin_lock(&inode->i_lock);
13324f8ad655SJan Kara 	/*
13334f8ad655SJan Kara 	 * If inode is clean, remove it from writeback lists. Otherwise don't
13344f8ad655SJan Kara 	 * touch it. See comment above for explanation.
13354f8ad655SJan Kara 	 */
13360ae45f63STheodore Ts'o 	if (!(inode->i_state & I_DIRTY_ALL))
1337d6c10f1fSTejun Heo 		inode_wb_list_del_locked(inode, wb);
13384f8ad655SJan Kara 	spin_unlock(&wb->list_lock);
13391c0eeaf5SJoern Engel 	inode_sync_complete(inode);
13404f8ad655SJan Kara out:
13414f8ad655SJan Kara 	spin_unlock(&inode->i_lock);
13421da177e4SLinus Torvalds 	return ret;
13431da177e4SLinus Torvalds }
13441da177e4SLinus Torvalds 
1345a88a341aSTejun Heo static long writeback_chunk_size(struct bdi_writeback *wb,
13461a12d8bdSWu Fengguang 				 struct wb_writeback_work *work)
1347d46db3d5SWu Fengguang {
1348d46db3d5SWu Fengguang 	long pages;
1349d46db3d5SWu Fengguang 
1350d46db3d5SWu Fengguang 	/*
1351d46db3d5SWu Fengguang 	 * WB_SYNC_ALL mode does livelock avoidance by syncing dirty
1352d46db3d5SWu Fengguang 	 * inodes/pages in one big loop. Setting wbc.nr_to_write=LONG_MAX
1353d46db3d5SWu Fengguang 	 * here avoids calling into writeback_inodes_wb() more than once.
1354d46db3d5SWu Fengguang 	 *
1355d46db3d5SWu Fengguang 	 * The intended call sequence for WB_SYNC_ALL writeback is:
1356d46db3d5SWu Fengguang 	 *
1357d46db3d5SWu Fengguang 	 *      wb_writeback()
1358d46db3d5SWu Fengguang 	 *          writeback_sb_inodes()       <== called only once
1359d46db3d5SWu Fengguang 	 *              write_cache_pages()     <== called once for each inode
1360d46db3d5SWu Fengguang 	 *                   (quickly) tag currently dirty pages
1361d46db3d5SWu Fengguang 	 *                   (maybe slowly) sync all tagged pages
1362d46db3d5SWu Fengguang 	 */
1363d46db3d5SWu Fengguang 	if (work->sync_mode == WB_SYNC_ALL || work->tagged_writepages)
1364d46db3d5SWu Fengguang 		pages = LONG_MAX;
13651a12d8bdSWu Fengguang 	else {
1366a88a341aSTejun Heo 		pages = min(wb->avg_write_bandwidth / 2,
1367dcc25ae7STejun Heo 			    global_wb_domain.dirty_limit / DIRTY_SCOPE);
13681a12d8bdSWu Fengguang 		pages = min(pages, work->nr_pages);
13691a12d8bdSWu Fengguang 		pages = round_down(pages + MIN_WRITEBACK_PAGES,
13701a12d8bdSWu Fengguang 				   MIN_WRITEBACK_PAGES);
13711a12d8bdSWu Fengguang 	}
1372d46db3d5SWu Fengguang 
1373d46db3d5SWu Fengguang 	return pages;
1374d46db3d5SWu Fengguang }
1375d46db3d5SWu Fengguang 
137603ba3782SJens Axboe /*
1377f11c9c5cSEdward Shishkin  * Write a portion of b_io inodes which belong to @sb.
1378edadfb10SChristoph Hellwig  *
1379d46db3d5SWu Fengguang  * Return the number of pages and/or inodes written.
1380f11c9c5cSEdward Shishkin  */
1381d46db3d5SWu Fengguang static long writeback_sb_inodes(struct super_block *sb,
1382d46db3d5SWu Fengguang 				struct bdi_writeback *wb,
1383d46db3d5SWu Fengguang 				struct wb_writeback_work *work)
138403ba3782SJens Axboe {
1385d46db3d5SWu Fengguang 	struct writeback_control wbc = {
1386d46db3d5SWu Fengguang 		.sync_mode		= work->sync_mode,
1387d46db3d5SWu Fengguang 		.tagged_writepages	= work->tagged_writepages,
1388d46db3d5SWu Fengguang 		.for_kupdate		= work->for_kupdate,
1389d46db3d5SWu Fengguang 		.for_background		= work->for_background,
13907747bd4bSDave Chinner 		.for_sync		= work->for_sync,
1391d46db3d5SWu Fengguang 		.range_cyclic		= work->range_cyclic,
1392d46db3d5SWu Fengguang 		.range_start		= 0,
1393d46db3d5SWu Fengguang 		.range_end		= LLONG_MAX,
1394d46db3d5SWu Fengguang 	};
1395d46db3d5SWu Fengguang 	unsigned long start_time = jiffies;
1396d46db3d5SWu Fengguang 	long write_chunk;
1397d46db3d5SWu Fengguang 	long wrote = 0;  /* count both pages and inodes */
1398d46db3d5SWu Fengguang 
139903ba3782SJens Axboe 	while (!list_empty(&wb->b_io)) {
14007ccf19a8SNick Piggin 		struct inode *inode = wb_inode(wb->b_io.prev);
1401edadfb10SChristoph Hellwig 
1402edadfb10SChristoph Hellwig 		if (inode->i_sb != sb) {
1403d46db3d5SWu Fengguang 			if (work->sb) {
1404edadfb10SChristoph Hellwig 				/*
1405edadfb10SChristoph Hellwig 				 * We only want to write back data for this
1406edadfb10SChristoph Hellwig 				 * superblock, move all inodes not belonging
1407edadfb10SChristoph Hellwig 				 * to it back onto the dirty list.
1408edadfb10SChristoph Hellwig 				 */
1409f758eeabSChristoph Hellwig 				redirty_tail(inode, wb);
141066f3b8e2SJens Axboe 				continue;
141166f3b8e2SJens Axboe 			}
1412edadfb10SChristoph Hellwig 
1413edadfb10SChristoph Hellwig 			/*
1414edadfb10SChristoph Hellwig 			 * The inode belongs to a different superblock.
1415edadfb10SChristoph Hellwig 			 * Bounce back to the caller to unpin this and
1416edadfb10SChristoph Hellwig 			 * pin the next superblock.
1417edadfb10SChristoph Hellwig 			 */
1418d46db3d5SWu Fengguang 			break;
1419edadfb10SChristoph Hellwig 		}
1420edadfb10SChristoph Hellwig 
14219843b76aSChristoph Hellwig 		/*
1422331cbdeeSWanpeng Li 		 * Don't bother with new inodes or inodes being freed, first
1423331cbdeeSWanpeng Li 		 * kind does not need periodic writeout yet, and for the latter
14249843b76aSChristoph Hellwig 		 * kind writeout is handled by the freer.
14259843b76aSChristoph Hellwig 		 */
1426250df6edSDave Chinner 		spin_lock(&inode->i_lock);
14279843b76aSChristoph Hellwig 		if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) {
1428250df6edSDave Chinner 			spin_unlock(&inode->i_lock);
1429fcc5c222SWu Fengguang 			redirty_tail(inode, wb);
14307ef0d737SNick Piggin 			continue;
14317ef0d737SNick Piggin 		}
1432cc1676d9SJan Kara 		if ((inode->i_state & I_SYNC) && wbc.sync_mode != WB_SYNC_ALL) {
1433cc1676d9SJan Kara 			/*
1434cc1676d9SJan Kara 			 * If this inode is locked for writeback and we are not
1435cc1676d9SJan Kara 			 * doing writeback-for-data-integrity, move it to
1436cc1676d9SJan Kara 			 * b_more_io so that writeback can proceed with the
1437cc1676d9SJan Kara 			 * other inodes on s_io.
1438cc1676d9SJan Kara 			 *
1439cc1676d9SJan Kara 			 * We'll have another go at writing back this inode
1440cc1676d9SJan Kara 			 * when we completed a full scan of b_io.
1441cc1676d9SJan Kara 			 */
1442cc1676d9SJan Kara 			spin_unlock(&inode->i_lock);
1443cc1676d9SJan Kara 			requeue_io(inode, wb);
1444cc1676d9SJan Kara 			trace_writeback_sb_inodes_requeue(inode);
1445cc1676d9SJan Kara 			continue;
1446cc1676d9SJan Kara 		}
1447f0d07b7fSJan Kara 		spin_unlock(&wb->list_lock);
1448f0d07b7fSJan Kara 
14494f8ad655SJan Kara 		/*
14504f8ad655SJan Kara 		 * We already requeued the inode if it had I_SYNC set and we
14514f8ad655SJan Kara 		 * are doing WB_SYNC_NONE writeback. So this catches only the
14524f8ad655SJan Kara 		 * WB_SYNC_ALL case.
14534f8ad655SJan Kara 		 */
1454169ebd90SJan Kara 		if (inode->i_state & I_SYNC) {
1455169ebd90SJan Kara 			/* Wait for I_SYNC. This function drops i_lock... */
1456169ebd90SJan Kara 			inode_sleep_on_writeback(inode);
1457169ebd90SJan Kara 			/* Inode may be gone, start again */
1458ead188f9SJan Kara 			spin_lock(&wb->list_lock);
1459169ebd90SJan Kara 			continue;
1460169ebd90SJan Kara 		}
14614f8ad655SJan Kara 		inode->i_state |= I_SYNC;
1462b16b1debSTejun Heo 		wbc_attach_and_unlock_inode(&wbc, inode);
1463169ebd90SJan Kara 
1464a88a341aSTejun Heo 		write_chunk = writeback_chunk_size(wb, work);
1465d46db3d5SWu Fengguang 		wbc.nr_to_write = write_chunk;
1466d46db3d5SWu Fengguang 		wbc.pages_skipped = 0;
1467250df6edSDave Chinner 
1468169ebd90SJan Kara 		/*
1469169ebd90SJan Kara 		 * We use I_SYNC to pin the inode in memory. While it is set
1470169ebd90SJan Kara 		 * evict_inode() will wait so the inode cannot be freed.
1471169ebd90SJan Kara 		 */
1472cd8ed2a4SYan Hong 		__writeback_single_inode(inode, &wbc);
1473d46db3d5SWu Fengguang 
1474b16b1debSTejun Heo 		wbc_detach_inode(&wbc);
1475d46db3d5SWu Fengguang 		work->nr_pages -= write_chunk - wbc.nr_to_write;
1476d46db3d5SWu Fengguang 		wrote += write_chunk - wbc.nr_to_write;
14774f8ad655SJan Kara 		spin_lock(&wb->list_lock);
14784f8ad655SJan Kara 		spin_lock(&inode->i_lock);
14790ae45f63STheodore Ts'o 		if (!(inode->i_state & I_DIRTY_ALL))
1480d46db3d5SWu Fengguang 			wrote++;
14814f8ad655SJan Kara 		requeue_inode(inode, wb, &wbc);
14824f8ad655SJan Kara 		inode_sync_complete(inode);
14830f1b1fd8SDave Chinner 		spin_unlock(&inode->i_lock);
1484169ebd90SJan Kara 		cond_resched_lock(&wb->list_lock);
1485d46db3d5SWu Fengguang 		/*
1486d46db3d5SWu Fengguang 		 * bail out to wb_writeback() often enough to check
1487d46db3d5SWu Fengguang 		 * background threshold and other termination conditions.
1488d46db3d5SWu Fengguang 		 */
1489d46db3d5SWu Fengguang 		if (wrote) {
1490d46db3d5SWu Fengguang 			if (time_is_before_jiffies(start_time + HZ / 10UL))
1491d46db3d5SWu Fengguang 				break;
1492d46db3d5SWu Fengguang 			if (work->nr_pages <= 0)
1493d46db3d5SWu Fengguang 				break;
14941da177e4SLinus Torvalds 		}
14958bc3be27SFengguang Wu 	}
1496d46db3d5SWu Fengguang 	return wrote;
1497f11c9c5cSEdward Shishkin }
149838f21977SNick Piggin 
1499d46db3d5SWu Fengguang static long __writeback_inodes_wb(struct bdi_writeback *wb,
1500d46db3d5SWu Fengguang 				  struct wb_writeback_work *work)
1501f11c9c5cSEdward Shishkin {
1502d46db3d5SWu Fengguang 	unsigned long start_time = jiffies;
1503d46db3d5SWu Fengguang 	long wrote = 0;
1504f11c9c5cSEdward Shishkin 
1505f11c9c5cSEdward Shishkin 	while (!list_empty(&wb->b_io)) {
15067ccf19a8SNick Piggin 		struct inode *inode = wb_inode(wb->b_io.prev);
1507f11c9c5cSEdward Shishkin 		struct super_block *sb = inode->i_sb;
1508f11c9c5cSEdward Shishkin 
1509eb6ef3dfSKonstantin Khlebnikov 		if (!trylock_super(sb)) {
15100e995816SWu Fengguang 			/*
1511eb6ef3dfSKonstantin Khlebnikov 			 * trylock_super() may fail consistently due to
15120e995816SWu Fengguang 			 * s_umount being grabbed by someone else. Don't use
15130e995816SWu Fengguang 			 * requeue_io() to avoid busy retrying the inode/sb.
15140e995816SWu Fengguang 			 */
15150e995816SWu Fengguang 			redirty_tail(inode, wb);
1516d19de7edSChristoph Hellwig 			continue;
1517334132aeSChristoph Hellwig 		}
1518d46db3d5SWu Fengguang 		wrote += writeback_sb_inodes(sb, wb, work);
1519eb6ef3dfSKonstantin Khlebnikov 		up_read(&sb->s_umount);
1520f11c9c5cSEdward Shishkin 
1521d46db3d5SWu Fengguang 		/* refer to the same tests at the end of writeback_sb_inodes */
1522d46db3d5SWu Fengguang 		if (wrote) {
1523d46db3d5SWu Fengguang 			if (time_is_before_jiffies(start_time + HZ / 10UL))
1524d46db3d5SWu Fengguang 				break;
1525d46db3d5SWu Fengguang 			if (work->nr_pages <= 0)
1526f11c9c5cSEdward Shishkin 				break;
1527f11c9c5cSEdward Shishkin 		}
1528d46db3d5SWu Fengguang 	}
152966f3b8e2SJens Axboe 	/* Leave any unwritten inodes on b_io */
1530d46db3d5SWu Fengguang 	return wrote;
153166f3b8e2SJens Axboe }
153266f3b8e2SJens Axboe 
15337d9f073bSWanpeng Li static long writeback_inodes_wb(struct bdi_writeback *wb, long nr_pages,
15340e175a18SCurt Wohlgemuth 				enum wb_reason reason)
1535edadfb10SChristoph Hellwig {
1536d46db3d5SWu Fengguang 	struct wb_writeback_work work = {
1537d46db3d5SWu Fengguang 		.nr_pages	= nr_pages,
1538d46db3d5SWu Fengguang 		.sync_mode	= WB_SYNC_NONE,
1539d46db3d5SWu Fengguang 		.range_cyclic	= 1,
15400e175a18SCurt Wohlgemuth 		.reason		= reason,
1541d46db3d5SWu Fengguang 	};
1542edadfb10SChristoph Hellwig 
1543f758eeabSChristoph Hellwig 	spin_lock(&wb->list_lock);
1544424b351fSWu Fengguang 	if (list_empty(&wb->b_io))
1545ad4e38ddSCurt Wohlgemuth 		queue_io(wb, &work);
1546d46db3d5SWu Fengguang 	__writeback_inodes_wb(wb, &work);
1547f758eeabSChristoph Hellwig 	spin_unlock(&wb->list_lock);
1548edadfb10SChristoph Hellwig 
1549d46db3d5SWu Fengguang 	return nr_pages - work.nr_pages;
155066f3b8e2SJens Axboe }
155166f3b8e2SJens Axboe 
155203ba3782SJens Axboe /*
155303ba3782SJens Axboe  * Explicit flushing or periodic writeback of "old" data.
155403ba3782SJens Axboe  *
155503ba3782SJens Axboe  * Define "old": the first time one of an inode's pages is dirtied, we mark the
155603ba3782SJens Axboe  * dirtying-time in the inode's address_space.  So this periodic writeback code
155703ba3782SJens Axboe  * just walks the superblock inode list, writing back any inodes which are
155803ba3782SJens Axboe  * older than a specific point in time.
155903ba3782SJens Axboe  *
156003ba3782SJens Axboe  * Try to run once per dirty_writeback_interval.  But if a writeback event
156103ba3782SJens Axboe  * takes longer than a dirty_writeback_interval interval, then leave a
156203ba3782SJens Axboe  * one-second gap.
156303ba3782SJens Axboe  *
156403ba3782SJens Axboe  * older_than_this takes precedence over nr_to_write.  So we'll only write back
156503ba3782SJens Axboe  * all dirty pages if they are all attached to "old" mappings.
156603ba3782SJens Axboe  */
1567c4a77a6cSJens Axboe static long wb_writeback(struct bdi_writeback *wb,
156883ba7b07SChristoph Hellwig 			 struct wb_writeback_work *work)
156903ba3782SJens Axboe {
1570e98be2d5SWu Fengguang 	unsigned long wb_start = jiffies;
1571d46db3d5SWu Fengguang 	long nr_pages = work->nr_pages;
15720dc83bd3SJan Kara 	unsigned long oldest_jif;
1573a5989bdcSJan Kara 	struct inode *inode;
1574d46db3d5SWu Fengguang 	long progress;
157503ba3782SJens Axboe 
15760dc83bd3SJan Kara 	oldest_jif = jiffies;
15770dc83bd3SJan Kara 	work->older_than_this = &oldest_jif;
157803ba3782SJens Axboe 
1579e8dfc305SWu Fengguang 	spin_lock(&wb->list_lock);
158003ba3782SJens Axboe 	for (;;) {
158103ba3782SJens Axboe 		/*
1582d3ddec76SWu Fengguang 		 * Stop writeback when nr_pages has been consumed
158303ba3782SJens Axboe 		 */
158483ba7b07SChristoph Hellwig 		if (work->nr_pages <= 0)
158503ba3782SJens Axboe 			break;
158603ba3782SJens Axboe 
158703ba3782SJens Axboe 		/*
1588aa373cf5SJan Kara 		 * Background writeout and kupdate-style writeback may
1589aa373cf5SJan Kara 		 * run forever. Stop them if there is other work to do
1590aa373cf5SJan Kara 		 * so that e.g. sync can proceed. They'll be restarted
1591aa373cf5SJan Kara 		 * after the other works are all done.
1592aa373cf5SJan Kara 		 */
1593aa373cf5SJan Kara 		if ((work->for_background || work->for_kupdate) &&
1594f0054bb1STejun Heo 		    !list_empty(&wb->work_list))
1595aa373cf5SJan Kara 			break;
1596aa373cf5SJan Kara 
1597aa373cf5SJan Kara 		/*
1598d3ddec76SWu Fengguang 		 * For background writeout, stop when we are below the
1599d3ddec76SWu Fengguang 		 * background dirty threshold
160003ba3782SJens Axboe 		 */
1601aa661bbeSTejun Heo 		if (work->for_background && !wb_over_bg_thresh(wb))
160203ba3782SJens Axboe 			break;
160303ba3782SJens Axboe 
16041bc36b64SJan Kara 		/*
16051bc36b64SJan Kara 		 * Kupdate and background works are special and we want to
16061bc36b64SJan Kara 		 * include all inodes that need writing. Livelock avoidance is
16071bc36b64SJan Kara 		 * handled by these works yielding to any other work so we are
16081bc36b64SJan Kara 		 * safe.
16091bc36b64SJan Kara 		 */
1610ba9aa839SWu Fengguang 		if (work->for_kupdate) {
16110dc83bd3SJan Kara 			oldest_jif = jiffies -
1612ba9aa839SWu Fengguang 				msecs_to_jiffies(dirty_expire_interval * 10);
16131bc36b64SJan Kara 		} else if (work->for_background)
16140dc83bd3SJan Kara 			oldest_jif = jiffies;
1615028c2dd1SDave Chinner 
1616d46db3d5SWu Fengguang 		trace_writeback_start(wb->bdi, work);
1617e8dfc305SWu Fengguang 		if (list_empty(&wb->b_io))
1618ad4e38ddSCurt Wohlgemuth 			queue_io(wb, work);
161983ba7b07SChristoph Hellwig 		if (work->sb)
1620d46db3d5SWu Fengguang 			progress = writeback_sb_inodes(work->sb, wb, work);
1621edadfb10SChristoph Hellwig 		else
1622d46db3d5SWu Fengguang 			progress = __writeback_inodes_wb(wb, work);
1623d46db3d5SWu Fengguang 		trace_writeback_written(wb->bdi, work);
1624028c2dd1SDave Chinner 
1625e98be2d5SWu Fengguang 		wb_update_bandwidth(wb, wb_start);
162603ba3782SJens Axboe 
162703ba3782SJens Axboe 		/*
162871fd05a8SJens Axboe 		 * Did we write something? Try for more
1629e6fb6da2SWu Fengguang 		 *
1630e6fb6da2SWu Fengguang 		 * Dirty inodes are moved to b_io for writeback in batches.
1631e6fb6da2SWu Fengguang 		 * The completion of the current batch does not necessarily
1632e6fb6da2SWu Fengguang 		 * mean the overall work is done. So we keep looping as long
1633e6fb6da2SWu Fengguang 		 * as made some progress on cleaning pages or inodes.
163471fd05a8SJens Axboe 		 */
1635d46db3d5SWu Fengguang 		if (progress)
163603ba3782SJens Axboe 			continue;
1637a5989bdcSJan Kara 		/*
1638e6fb6da2SWu Fengguang 		 * No more inodes for IO, bail
1639a5989bdcSJan Kara 		 */
1640b7a2441fSWu Fengguang 		if (list_empty(&wb->b_more_io))
164103ba3782SJens Axboe 			break;
164203ba3782SJens Axboe 		/*
16438010c3b6SJens Axboe 		 * Nothing written. Wait for some inode to
16448010c3b6SJens Axboe 		 * become available for writeback. Otherwise
16458010c3b6SJens Axboe 		 * we'll just busyloop.
164603ba3782SJens Axboe 		 */
164703ba3782SJens Axboe 		if (!list_empty(&wb->b_more_io))  {
1648d46db3d5SWu Fengguang 			trace_writeback_wait(wb->bdi, work);
164903ba3782SJens Axboe 			inode = wb_inode(wb->b_more_io.prev);
1650250df6edSDave Chinner 			spin_lock(&inode->i_lock);
1651f0d07b7fSJan Kara 			spin_unlock(&wb->list_lock);
1652169ebd90SJan Kara 			/* This function drops i_lock... */
1653169ebd90SJan Kara 			inode_sleep_on_writeback(inode);
1654f0d07b7fSJan Kara 			spin_lock(&wb->list_lock);
165503ba3782SJens Axboe 		}
165603ba3782SJens Axboe 	}
1657e8dfc305SWu Fengguang 	spin_unlock(&wb->list_lock);
165803ba3782SJens Axboe 
1659d46db3d5SWu Fengguang 	return nr_pages - work->nr_pages;
166003ba3782SJens Axboe }
166103ba3782SJens Axboe 
166203ba3782SJens Axboe /*
166383ba7b07SChristoph Hellwig  * Return the next wb_writeback_work struct that hasn't been processed yet.
166403ba3782SJens Axboe  */
1665f0054bb1STejun Heo static struct wb_writeback_work *get_next_work_item(struct bdi_writeback *wb)
166603ba3782SJens Axboe {
166783ba7b07SChristoph Hellwig 	struct wb_writeback_work *work = NULL;
166803ba3782SJens Axboe 
1669f0054bb1STejun Heo 	spin_lock_bh(&wb->work_lock);
1670f0054bb1STejun Heo 	if (!list_empty(&wb->work_list)) {
1671f0054bb1STejun Heo 		work = list_entry(wb->work_list.next,
167283ba7b07SChristoph Hellwig 				  struct wb_writeback_work, list);
167383ba7b07SChristoph Hellwig 		list_del_init(&work->list);
167403ba3782SJens Axboe 	}
1675f0054bb1STejun Heo 	spin_unlock_bh(&wb->work_lock);
167683ba7b07SChristoph Hellwig 	return work;
167703ba3782SJens Axboe }
167803ba3782SJens Axboe 
1679cdf01dd5SLinus Torvalds /*
1680cdf01dd5SLinus Torvalds  * Add in the number of potentially dirty inodes, because each inode
1681cdf01dd5SLinus Torvalds  * write can dirty pagecache in the underlying blockdev.
1682cdf01dd5SLinus Torvalds  */
1683cdf01dd5SLinus Torvalds static unsigned long get_nr_dirty_pages(void)
1684cdf01dd5SLinus Torvalds {
1685cdf01dd5SLinus Torvalds 	return global_page_state(NR_FILE_DIRTY) +
1686cdf01dd5SLinus Torvalds 		global_page_state(NR_UNSTABLE_NFS) +
1687cdf01dd5SLinus Torvalds 		get_nr_dirty_inodes();
1688cdf01dd5SLinus Torvalds }
1689cdf01dd5SLinus Torvalds 
16906585027aSJan Kara static long wb_check_background_flush(struct bdi_writeback *wb)
16916585027aSJan Kara {
1692aa661bbeSTejun Heo 	if (wb_over_bg_thresh(wb)) {
16936585027aSJan Kara 
16946585027aSJan Kara 		struct wb_writeback_work work = {
16956585027aSJan Kara 			.nr_pages	= LONG_MAX,
16966585027aSJan Kara 			.sync_mode	= WB_SYNC_NONE,
16976585027aSJan Kara 			.for_background	= 1,
16986585027aSJan Kara 			.range_cyclic	= 1,
16990e175a18SCurt Wohlgemuth 			.reason		= WB_REASON_BACKGROUND,
17006585027aSJan Kara 		};
17016585027aSJan Kara 
17026585027aSJan Kara 		return wb_writeback(wb, &work);
17036585027aSJan Kara 	}
17046585027aSJan Kara 
17056585027aSJan Kara 	return 0;
17066585027aSJan Kara }
17076585027aSJan Kara 
170803ba3782SJens Axboe static long wb_check_old_data_flush(struct bdi_writeback *wb)
170903ba3782SJens Axboe {
171003ba3782SJens Axboe 	unsigned long expired;
171103ba3782SJens Axboe 	long nr_pages;
171203ba3782SJens Axboe 
171369b62d01SJens Axboe 	/*
171469b62d01SJens Axboe 	 * When set to zero, disable periodic writeback
171569b62d01SJens Axboe 	 */
171669b62d01SJens Axboe 	if (!dirty_writeback_interval)
171769b62d01SJens Axboe 		return 0;
171869b62d01SJens Axboe 
171903ba3782SJens Axboe 	expired = wb->last_old_flush +
172003ba3782SJens Axboe 			msecs_to_jiffies(dirty_writeback_interval * 10);
172103ba3782SJens Axboe 	if (time_before(jiffies, expired))
172203ba3782SJens Axboe 		return 0;
172303ba3782SJens Axboe 
172403ba3782SJens Axboe 	wb->last_old_flush = jiffies;
1725cdf01dd5SLinus Torvalds 	nr_pages = get_nr_dirty_pages();
172603ba3782SJens Axboe 
1727c4a77a6cSJens Axboe 	if (nr_pages) {
172883ba7b07SChristoph Hellwig 		struct wb_writeback_work work = {
1729c4a77a6cSJens Axboe 			.nr_pages	= nr_pages,
1730c4a77a6cSJens Axboe 			.sync_mode	= WB_SYNC_NONE,
1731c4a77a6cSJens Axboe 			.for_kupdate	= 1,
1732c4a77a6cSJens Axboe 			.range_cyclic	= 1,
17330e175a18SCurt Wohlgemuth 			.reason		= WB_REASON_PERIODIC,
1734c4a77a6cSJens Axboe 		};
1735c4a77a6cSJens Axboe 
173683ba7b07SChristoph Hellwig 		return wb_writeback(wb, &work);
1737c4a77a6cSJens Axboe 	}
173803ba3782SJens Axboe 
173903ba3782SJens Axboe 	return 0;
174003ba3782SJens Axboe }
174103ba3782SJens Axboe 
174203ba3782SJens Axboe /*
174303ba3782SJens Axboe  * Retrieve work items and do the writeback they describe
174403ba3782SJens Axboe  */
174525d130baSWanpeng Li static long wb_do_writeback(struct bdi_writeback *wb)
174603ba3782SJens Axboe {
174783ba7b07SChristoph Hellwig 	struct wb_writeback_work *work;
1748c4a77a6cSJens Axboe 	long wrote = 0;
174903ba3782SJens Axboe 
17504452226eSTejun Heo 	set_bit(WB_writeback_running, &wb->state);
1751f0054bb1STejun Heo 	while ((work = get_next_work_item(wb)) != NULL) {
1752cc395d7fSTejun Heo 		struct wb_completion *done = work->done;
175383ba7b07SChristoph Hellwig 
1754f0054bb1STejun Heo 		trace_writeback_exec(wb->bdi, work);
1755455b2864SDave Chinner 
175683ba7b07SChristoph Hellwig 		wrote += wb_writeback(wb, work);
175703ba3782SJens Axboe 
17588a1270cdSTejun Heo 		if (work->auto_free)
175983ba7b07SChristoph Hellwig 			kfree(work);
1760cc395d7fSTejun Heo 		if (done && atomic_dec_and_test(&done->cnt))
1761cc395d7fSTejun Heo 			wake_up_all(&wb->bdi->wb_waitq);
176203ba3782SJens Axboe 	}
176303ba3782SJens Axboe 
176403ba3782SJens Axboe 	/*
176503ba3782SJens Axboe 	 * Check for periodic writeback, kupdated() style
176603ba3782SJens Axboe 	 */
176703ba3782SJens Axboe 	wrote += wb_check_old_data_flush(wb);
17686585027aSJan Kara 	wrote += wb_check_background_flush(wb);
17694452226eSTejun Heo 	clear_bit(WB_writeback_running, &wb->state);
177003ba3782SJens Axboe 
177103ba3782SJens Axboe 	return wrote;
177203ba3782SJens Axboe }
177303ba3782SJens Axboe 
177403ba3782SJens Axboe /*
177503ba3782SJens Axboe  * Handle writeback of dirty data for the device backed by this bdi. Also
1776839a8e86STejun Heo  * reschedules periodically and does kupdated style flushing.
177703ba3782SJens Axboe  */
1778f0054bb1STejun Heo void wb_workfn(struct work_struct *work)
177903ba3782SJens Axboe {
1780839a8e86STejun Heo 	struct bdi_writeback *wb = container_of(to_delayed_work(work),
1781839a8e86STejun Heo 						struct bdi_writeback, dwork);
178203ba3782SJens Axboe 	long pages_written;
178303ba3782SJens Axboe 
1784f0054bb1STejun Heo 	set_worker_desc("flush-%s", dev_name(wb->bdi->dev));
1785766f9164SPeter Zijlstra 	current->flags |= PF_SWAPWRITE;
178603ba3782SJens Axboe 
1787839a8e86STejun Heo 	if (likely(!current_is_workqueue_rescuer() ||
17884452226eSTejun Heo 		   !test_bit(WB_registered, &wb->state))) {
178903ba3782SJens Axboe 		/*
1790f0054bb1STejun Heo 		 * The normal path.  Keep writing back @wb until its
1791839a8e86STejun Heo 		 * work_list is empty.  Note that this path is also taken
1792f0054bb1STejun Heo 		 * if @wb is shutting down even when we're running off the
1793839a8e86STejun Heo 		 * rescuer as work_list needs to be drained.
179403ba3782SJens Axboe 		 */
1795839a8e86STejun Heo 		do {
179625d130baSWanpeng Li 			pages_written = wb_do_writeback(wb);
1797455b2864SDave Chinner 			trace_writeback_pages_written(pages_written);
1798f0054bb1STejun Heo 		} while (!list_empty(&wb->work_list));
1799839a8e86STejun Heo 	} else {
1800253c34e9SArtem Bityutskiy 		/*
1801839a8e86STejun Heo 		 * bdi_wq can't get enough workers and we're running off
1802839a8e86STejun Heo 		 * the emergency worker.  Don't hog it.  Hopefully, 1024 is
1803839a8e86STejun Heo 		 * enough for efficient IO.
1804253c34e9SArtem Bityutskiy 		 */
1805f0054bb1STejun Heo 		pages_written = writeback_inodes_wb(wb, 1024,
1806839a8e86STejun Heo 						    WB_REASON_FORKER_THREAD);
1807839a8e86STejun Heo 		trace_writeback_pages_written(pages_written);
180803ba3782SJens Axboe 	}
180903ba3782SJens Axboe 
1810f0054bb1STejun Heo 	if (!list_empty(&wb->work_list))
18116ca738d6SDerek Basehore 		mod_delayed_work(bdi_wq, &wb->dwork, 0);
18126ca738d6SDerek Basehore 	else if (wb_has_dirty_io(wb) && dirty_writeback_interval)
1813f0054bb1STejun Heo 		wb_wakeup_delayed(wb);
1814455b2864SDave Chinner 
1815839a8e86STejun Heo 	current->flags &= ~PF_SWAPWRITE;
181603ba3782SJens Axboe }
181703ba3782SJens Axboe 
181803ba3782SJens Axboe /*
181903ba3782SJens Axboe  * Start writeback of `nr_pages' pages.  If `nr_pages' is zero, write back
182003ba3782SJens Axboe  * the whole world.
182103ba3782SJens Axboe  */
18220e175a18SCurt Wohlgemuth void wakeup_flusher_threads(long nr_pages, enum wb_reason reason)
182303ba3782SJens Axboe {
1824b8c2f347SChristoph Hellwig 	struct backing_dev_info *bdi;
1825b8c2f347SChristoph Hellwig 
182647df3ddeSJan Kara 	if (!nr_pages)
182747df3ddeSJan Kara 		nr_pages = get_nr_dirty_pages();
1828b8c2f347SChristoph Hellwig 
1829b8c2f347SChristoph Hellwig 	rcu_read_lock();
1830f2b65121STejun Heo 	list_for_each_entry_rcu(bdi, &bdi_list, bdi_list) {
1831f2b65121STejun Heo 		struct bdi_writeback *wb;
1832f2b65121STejun Heo 		struct wb_iter iter;
1833f2b65121STejun Heo 
1834f2b65121STejun Heo 		if (!bdi_has_dirty_io(bdi))
1835f2b65121STejun Heo 			continue;
1836f2b65121STejun Heo 
1837f2b65121STejun Heo 		bdi_for_each_wb(wb, bdi, &iter, 0)
1838f2b65121STejun Heo 			wb_start_writeback(wb, wb_split_bdi_pages(wb, nr_pages),
1839f2b65121STejun Heo 					   false, reason);
1840f2b65121STejun Heo 	}
1841b8c2f347SChristoph Hellwig 	rcu_read_unlock();
184203ba3782SJens Axboe }
184303ba3782SJens Axboe 
1844a2f48706STheodore Ts'o /*
1845a2f48706STheodore Ts'o  * Wake up bdi's periodically to make sure dirtytime inodes gets
1846a2f48706STheodore Ts'o  * written back periodically.  We deliberately do *not* check the
1847a2f48706STheodore Ts'o  * b_dirtytime list in wb_has_dirty_io(), since this would cause the
1848a2f48706STheodore Ts'o  * kernel to be constantly waking up once there are any dirtytime
1849a2f48706STheodore Ts'o  * inodes on the system.  So instead we define a separate delayed work
1850a2f48706STheodore Ts'o  * function which gets called much more rarely.  (By default, only
1851a2f48706STheodore Ts'o  * once every 12 hours.)
1852a2f48706STheodore Ts'o  *
1853a2f48706STheodore Ts'o  * If there is any other write activity going on in the file system,
1854a2f48706STheodore Ts'o  * this function won't be necessary.  But if the only thing that has
1855a2f48706STheodore Ts'o  * happened on the file system is a dirtytime inode caused by an atime
1856a2f48706STheodore Ts'o  * update, we need this infrastructure below to make sure that inode
1857a2f48706STheodore Ts'o  * eventually gets pushed out to disk.
1858a2f48706STheodore Ts'o  */
1859a2f48706STheodore Ts'o static void wakeup_dirtytime_writeback(struct work_struct *w);
1860a2f48706STheodore Ts'o static DECLARE_DELAYED_WORK(dirtytime_work, wakeup_dirtytime_writeback);
1861a2f48706STheodore Ts'o 
1862a2f48706STheodore Ts'o static void wakeup_dirtytime_writeback(struct work_struct *w)
1863a2f48706STheodore Ts'o {
1864a2f48706STheodore Ts'o 	struct backing_dev_info *bdi;
1865a2f48706STheodore Ts'o 
1866a2f48706STheodore Ts'o 	rcu_read_lock();
1867a2f48706STheodore Ts'o 	list_for_each_entry_rcu(bdi, &bdi_list, bdi_list) {
1868001fe6f6STejun Heo 		struct bdi_writeback *wb;
1869001fe6f6STejun Heo 		struct wb_iter iter;
1870001fe6f6STejun Heo 
1871001fe6f6STejun Heo 		bdi_for_each_wb(wb, bdi, &iter, 0)
1872001fe6f6STejun Heo 			if (!list_empty(&bdi->wb.b_dirty_time))
1873f0054bb1STejun Heo 				wb_wakeup(&bdi->wb);
1874a2f48706STheodore Ts'o 	}
1875a2f48706STheodore Ts'o 	rcu_read_unlock();
1876a2f48706STheodore Ts'o 	schedule_delayed_work(&dirtytime_work, dirtytime_expire_interval * HZ);
1877a2f48706STheodore Ts'o }
1878a2f48706STheodore Ts'o 
1879a2f48706STheodore Ts'o static int __init start_dirtytime_writeback(void)
1880a2f48706STheodore Ts'o {
1881a2f48706STheodore Ts'o 	schedule_delayed_work(&dirtytime_work, dirtytime_expire_interval * HZ);
1882a2f48706STheodore Ts'o 	return 0;
1883a2f48706STheodore Ts'o }
1884a2f48706STheodore Ts'o __initcall(start_dirtytime_writeback);
1885a2f48706STheodore Ts'o 
18861efff914STheodore Ts'o int dirtytime_interval_handler(struct ctl_table *table, int write,
18871efff914STheodore Ts'o 			       void __user *buffer, size_t *lenp, loff_t *ppos)
18881efff914STheodore Ts'o {
18891efff914STheodore Ts'o 	int ret;
18901efff914STheodore Ts'o 
18911efff914STheodore Ts'o 	ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
18921efff914STheodore Ts'o 	if (ret == 0 && write)
18931efff914STheodore Ts'o 		mod_delayed_work(system_wq, &dirtytime_work, 0);
18941efff914STheodore Ts'o 	return ret;
18951efff914STheodore Ts'o }
18961efff914STheodore Ts'o 
189703ba3782SJens Axboe static noinline void block_dump___mark_inode_dirty(struct inode *inode)
189803ba3782SJens Axboe {
189903ba3782SJens Axboe 	if (inode->i_ino || strcmp(inode->i_sb->s_id, "bdev")) {
190003ba3782SJens Axboe 		struct dentry *dentry;
190103ba3782SJens Axboe 		const char *name = "?";
190203ba3782SJens Axboe 
190303ba3782SJens Axboe 		dentry = d_find_alias(inode);
190403ba3782SJens Axboe 		if (dentry) {
190503ba3782SJens Axboe 			spin_lock(&dentry->d_lock);
190603ba3782SJens Axboe 			name = (const char *) dentry->d_name.name;
190703ba3782SJens Axboe 		}
190803ba3782SJens Axboe 		printk(KERN_DEBUG
190903ba3782SJens Axboe 		       "%s(%d): dirtied inode %lu (%s) on %s\n",
191003ba3782SJens Axboe 		       current->comm, task_pid_nr(current), inode->i_ino,
191103ba3782SJens Axboe 		       name, inode->i_sb->s_id);
191203ba3782SJens Axboe 		if (dentry) {
191303ba3782SJens Axboe 			spin_unlock(&dentry->d_lock);
191403ba3782SJens Axboe 			dput(dentry);
191503ba3782SJens Axboe 		}
191603ba3782SJens Axboe 	}
191703ba3782SJens Axboe }
191803ba3782SJens Axboe 
191903ba3782SJens Axboe /**
192003ba3782SJens Axboe  *	__mark_inode_dirty -	internal function
192103ba3782SJens Axboe  *	@inode: inode to mark
192203ba3782SJens Axboe  *	@flags: what kind of dirty (i.e. I_DIRTY_SYNC)
192303ba3782SJens Axboe  *	Mark an inode as dirty. Callers should use mark_inode_dirty or
192403ba3782SJens Axboe  *  	mark_inode_dirty_sync.
192503ba3782SJens Axboe  *
192603ba3782SJens Axboe  * Put the inode on the super block's dirty list.
192703ba3782SJens Axboe  *
192803ba3782SJens Axboe  * CAREFUL! We mark it dirty unconditionally, but move it onto the
192903ba3782SJens Axboe  * dirty list only if it is hashed or if it refers to a blockdev.
193003ba3782SJens Axboe  * If it was not hashed, it will never be added to the dirty list
193103ba3782SJens Axboe  * even if it is later hashed, as it will have been marked dirty already.
193203ba3782SJens Axboe  *
193303ba3782SJens Axboe  * In short, make sure you hash any inodes _before_ you start marking
193403ba3782SJens Axboe  * them dirty.
193503ba3782SJens Axboe  *
193603ba3782SJens Axboe  * Note that for blockdevs, inode->dirtied_when represents the dirtying time of
193703ba3782SJens Axboe  * the block-special inode (/dev/hda1) itself.  And the ->dirtied_when field of
193803ba3782SJens Axboe  * the kernel-internal blockdev inode represents the dirtying time of the
193903ba3782SJens Axboe  * blockdev's pages.  This is why for I_DIRTY_PAGES we always use
194003ba3782SJens Axboe  * page->mapping->host, so the page-dirtying time is recorded in the internal
194103ba3782SJens Axboe  * blockdev inode.
194203ba3782SJens Axboe  */
19430ae45f63STheodore Ts'o #define I_DIRTY_INODE (I_DIRTY_SYNC | I_DIRTY_DATASYNC)
194403ba3782SJens Axboe void __mark_inode_dirty(struct inode *inode, int flags)
194503ba3782SJens Axboe {
194603ba3782SJens Axboe 	struct super_block *sb = inode->i_sb;
19470ae45f63STheodore Ts'o 	int dirtytime;
19480ae45f63STheodore Ts'o 
19490ae45f63STheodore Ts'o 	trace_writeback_mark_inode_dirty(inode, flags);
195003ba3782SJens Axboe 
195103ba3782SJens Axboe 	/*
195203ba3782SJens Axboe 	 * Don't do this for I_DIRTY_PAGES - that doesn't actually
195303ba3782SJens Axboe 	 * dirty the inode itself
195403ba3782SJens Axboe 	 */
19550ae45f63STheodore Ts'o 	if (flags & (I_DIRTY_SYNC | I_DIRTY_DATASYNC | I_DIRTY_TIME)) {
19569fb0a7daSTejun Heo 		trace_writeback_dirty_inode_start(inode, flags);
19579fb0a7daSTejun Heo 
195803ba3782SJens Axboe 		if (sb->s_op->dirty_inode)
1959aa385729SChristoph Hellwig 			sb->s_op->dirty_inode(inode, flags);
19609fb0a7daSTejun Heo 
19619fb0a7daSTejun Heo 		trace_writeback_dirty_inode(inode, flags);
196203ba3782SJens Axboe 	}
19630ae45f63STheodore Ts'o 	if (flags & I_DIRTY_INODE)
19640ae45f63STheodore Ts'o 		flags &= ~I_DIRTY_TIME;
19650ae45f63STheodore Ts'o 	dirtytime = flags & I_DIRTY_TIME;
196603ba3782SJens Axboe 
196703ba3782SJens Axboe 	/*
19689c6ac78eSTejun Heo 	 * Paired with smp_mb() in __writeback_single_inode() for the
19699c6ac78eSTejun Heo 	 * following lockless i_state test.  See there for details.
197003ba3782SJens Axboe 	 */
197103ba3782SJens Axboe 	smp_mb();
197203ba3782SJens Axboe 
19730ae45f63STheodore Ts'o 	if (((inode->i_state & flags) == flags) ||
19740ae45f63STheodore Ts'o 	    (dirtytime && (inode->i_state & I_DIRTY_INODE)))
197503ba3782SJens Axboe 		return;
197603ba3782SJens Axboe 
197703ba3782SJens Axboe 	if (unlikely(block_dump))
197803ba3782SJens Axboe 		block_dump___mark_inode_dirty(inode);
197903ba3782SJens Axboe 
1980250df6edSDave Chinner 	spin_lock(&inode->i_lock);
19810ae45f63STheodore Ts'o 	if (dirtytime && (inode->i_state & I_DIRTY_INODE))
19820ae45f63STheodore Ts'o 		goto out_unlock_inode;
198303ba3782SJens Axboe 	if ((inode->i_state & flags) != flags) {
198403ba3782SJens Axboe 		const int was_dirty = inode->i_state & I_DIRTY;
198503ba3782SJens Axboe 
198652ebea74STejun Heo 		inode_attach_wb(inode, NULL);
198752ebea74STejun Heo 
19880ae45f63STheodore Ts'o 		if (flags & I_DIRTY_INODE)
19890ae45f63STheodore Ts'o 			inode->i_state &= ~I_DIRTY_TIME;
199003ba3782SJens Axboe 		inode->i_state |= flags;
199103ba3782SJens Axboe 
199203ba3782SJens Axboe 		/*
199303ba3782SJens Axboe 		 * If the inode is being synced, just update its dirty state.
199403ba3782SJens Axboe 		 * The unlocker will place the inode on the appropriate
199503ba3782SJens Axboe 		 * superblock list, based upon its state.
199603ba3782SJens Axboe 		 */
199703ba3782SJens Axboe 		if (inode->i_state & I_SYNC)
1998250df6edSDave Chinner 			goto out_unlock_inode;
199903ba3782SJens Axboe 
200003ba3782SJens Axboe 		/*
200103ba3782SJens Axboe 		 * Only add valid (hashed) inodes to the superblock's
200203ba3782SJens Axboe 		 * dirty list.  Add blockdev inodes as well.
200303ba3782SJens Axboe 		 */
200403ba3782SJens Axboe 		if (!S_ISBLK(inode->i_mode)) {
20051d3382cbSAl Viro 			if (inode_unhashed(inode))
2006250df6edSDave Chinner 				goto out_unlock_inode;
200703ba3782SJens Axboe 		}
2008a4ffdde6SAl Viro 		if (inode->i_state & I_FREEING)
2009250df6edSDave Chinner 			goto out_unlock_inode;
201003ba3782SJens Axboe 
201103ba3782SJens Axboe 		/*
201203ba3782SJens Axboe 		 * If the inode was already on b_dirty/b_io/b_more_io, don't
201303ba3782SJens Axboe 		 * reposition it (that would break b_dirty time-ordering).
201403ba3782SJens Axboe 		 */
201503ba3782SJens Axboe 		if (!was_dirty) {
201687e1d789STejun Heo 			struct bdi_writeback *wb;
2017d6c10f1fSTejun Heo 			struct list_head *dirty_list;
2018a66979abSDave Chinner 			bool wakeup_bdi = false;
2019500b067cSJens Axboe 
202087e1d789STejun Heo 			wb = locked_inode_to_wb_and_lock_list(inode);
2021253c34e9SArtem Bityutskiy 
20220747259dSTejun Heo 			WARN(bdi_cap_writeback_dirty(wb->bdi) &&
20230747259dSTejun Heo 			     !test_bit(WB_registered, &wb->state),
20240747259dSTejun Heo 			     "bdi-%s not registered\n", wb->bdi->name);
202503ba3782SJens Axboe 
202603ba3782SJens Axboe 			inode->dirtied_when = jiffies;
2027a2f48706STheodore Ts'o 			if (dirtytime)
2028a2f48706STheodore Ts'o 				inode->dirtied_time_when = jiffies;
2029d6c10f1fSTejun Heo 
2030a2f48706STheodore Ts'o 			if (inode->i_state & (I_DIRTY_INODE | I_DIRTY_PAGES))
20310747259dSTejun Heo 				dirty_list = &wb->b_dirty;
2032a2f48706STheodore Ts'o 			else
20330747259dSTejun Heo 				dirty_list = &wb->b_dirty_time;
2034d6c10f1fSTejun Heo 
20350747259dSTejun Heo 			wakeup_bdi = inode_wb_list_move_locked(inode, wb,
2036d6c10f1fSTejun Heo 							       dirty_list);
2037d6c10f1fSTejun Heo 
20380747259dSTejun Heo 			spin_unlock(&wb->list_lock);
20390ae45f63STheodore Ts'o 			trace_writeback_dirty_inode_enqueue(inode);
2040253c34e9SArtem Bityutskiy 
2041d6c10f1fSTejun Heo 			/*
2042d6c10f1fSTejun Heo 			 * If this is the first dirty inode for this bdi,
2043d6c10f1fSTejun Heo 			 * we have to wake-up the corresponding bdi thread
2044d6c10f1fSTejun Heo 			 * to make sure background write-back happens
2045d6c10f1fSTejun Heo 			 * later.
2046d6c10f1fSTejun Heo 			 */
20470747259dSTejun Heo 			if (bdi_cap_writeback_dirty(wb->bdi) && wakeup_bdi)
20480747259dSTejun Heo 				wb_wakeup_delayed(wb);
2049a66979abSDave Chinner 			return;
2050a66979abSDave Chinner 		}
2051a66979abSDave Chinner 	}
2052a66979abSDave Chinner out_unlock_inode:
2053a66979abSDave Chinner 	spin_unlock(&inode->i_lock);
2054a66979abSDave Chinner 
205503ba3782SJens Axboe }
205603ba3782SJens Axboe EXPORT_SYMBOL(__mark_inode_dirty);
205703ba3782SJens Axboe 
2058b6e51316SJens Axboe static void wait_sb_inodes(struct super_block *sb)
205966f3b8e2SJens Axboe {
206038f21977SNick Piggin 	struct inode *inode, *old_inode = NULL;
206138f21977SNick Piggin 
206203ba3782SJens Axboe 	/*
206303ba3782SJens Axboe 	 * We need to be protected against the filesystem going from
206403ba3782SJens Axboe 	 * r/o to r/w or vice versa.
206503ba3782SJens Axboe 	 */
2066b6e51316SJens Axboe 	WARN_ON(!rwsem_is_locked(&sb->s_umount));
206703ba3782SJens Axboe 
206855fa6091SDave Chinner 	spin_lock(&inode_sb_list_lock);
206966f3b8e2SJens Axboe 
207038f21977SNick Piggin 	/*
207138f21977SNick Piggin 	 * Data integrity sync. Must wait for all pages under writeback,
207238f21977SNick Piggin 	 * because there may have been pages dirtied before our sync
207338f21977SNick Piggin 	 * call, but which had writeout started before we write it out.
207438f21977SNick Piggin 	 * In which case, the inode may not be on the dirty list, but
207538f21977SNick Piggin 	 * we still have to wait for that writeout.
207638f21977SNick Piggin 	 */
2077b6e51316SJens Axboe 	list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
2078250df6edSDave Chinner 		struct address_space *mapping = inode->i_mapping;
207938f21977SNick Piggin 
2080250df6edSDave Chinner 		spin_lock(&inode->i_lock);
2081250df6edSDave Chinner 		if ((inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW)) ||
2082250df6edSDave Chinner 		    (mapping->nrpages == 0)) {
2083250df6edSDave Chinner 			spin_unlock(&inode->i_lock);
208438f21977SNick Piggin 			continue;
2085250df6edSDave Chinner 		}
208638f21977SNick Piggin 		__iget(inode);
2087250df6edSDave Chinner 		spin_unlock(&inode->i_lock);
208855fa6091SDave Chinner 		spin_unlock(&inode_sb_list_lock);
208955fa6091SDave Chinner 
209038f21977SNick Piggin 		/*
209155fa6091SDave Chinner 		 * We hold a reference to 'inode' so it couldn't have been
209255fa6091SDave Chinner 		 * removed from s_inodes list while we dropped the
209355fa6091SDave Chinner 		 * inode_sb_list_lock.  We cannot iput the inode now as we can
209455fa6091SDave Chinner 		 * be holding the last reference and we cannot iput it under
209555fa6091SDave Chinner 		 * inode_sb_list_lock. So we keep the reference and iput it
209655fa6091SDave Chinner 		 * later.
209738f21977SNick Piggin 		 */
209838f21977SNick Piggin 		iput(old_inode);
209938f21977SNick Piggin 		old_inode = inode;
210038f21977SNick Piggin 
210138f21977SNick Piggin 		filemap_fdatawait(mapping);
210238f21977SNick Piggin 
210338f21977SNick Piggin 		cond_resched();
210438f21977SNick Piggin 
210555fa6091SDave Chinner 		spin_lock(&inode_sb_list_lock);
210638f21977SNick Piggin 	}
210755fa6091SDave Chinner 	spin_unlock(&inode_sb_list_lock);
210838f21977SNick Piggin 	iput(old_inode);
210966f3b8e2SJens Axboe }
21101da177e4SLinus Torvalds 
2111f30a7d0cSTejun Heo static void __writeback_inodes_sb_nr(struct super_block *sb, unsigned long nr,
2112f30a7d0cSTejun Heo 				     enum wb_reason reason, bool skip_if_busy)
21131da177e4SLinus Torvalds {
2114cc395d7fSTejun Heo 	DEFINE_WB_COMPLETION_ONSTACK(done);
211583ba7b07SChristoph Hellwig 	struct wb_writeback_work work = {
21163c4d7165SChristoph Hellwig 		.sb			= sb,
21173c4d7165SChristoph Hellwig 		.sync_mode		= WB_SYNC_NONE,
21186e6938b6SWu Fengguang 		.tagged_writepages	= 1,
211983ba7b07SChristoph Hellwig 		.done			= &done,
21203259f8beSChris Mason 		.nr_pages		= nr,
21210e175a18SCurt Wohlgemuth 		.reason			= reason,
21223c4d7165SChristoph Hellwig 	};
2123e7972912STejun Heo 	struct backing_dev_info *bdi = sb->s_bdi;
21240e3c9a22SJens Axboe 
2125e7972912STejun Heo 	if (!bdi_has_dirty_io(bdi) || bdi == &noop_backing_dev_info)
21266eedc701SJan Kara 		return;
2127cf37e972SChristoph Hellwig 	WARN_ON(!rwsem_is_locked(&sb->s_umount));
2128f30a7d0cSTejun Heo 
2129db125360STejun Heo 	bdi_split_work_to_wbs(sb->s_bdi, &work, skip_if_busy);
2130cc395d7fSTejun Heo 	wb_wait_for_completion(bdi, &done);
21311da177e4SLinus Torvalds }
2132f30a7d0cSTejun Heo 
2133f30a7d0cSTejun Heo /**
2134f30a7d0cSTejun Heo  * writeback_inodes_sb_nr -	writeback dirty inodes from given super_block
2135f30a7d0cSTejun Heo  * @sb: the superblock
2136f30a7d0cSTejun Heo  * @nr: the number of pages to write
2137f30a7d0cSTejun Heo  * @reason: reason why some writeback work initiated
2138f30a7d0cSTejun Heo  *
2139f30a7d0cSTejun Heo  * Start writeback on some inodes on this super_block. No guarantees are made
2140f30a7d0cSTejun Heo  * on how many (if any) will be written, and this function does not wait
2141f30a7d0cSTejun Heo  * for IO completion of submitted IO.
2142f30a7d0cSTejun Heo  */
2143f30a7d0cSTejun Heo void writeback_inodes_sb_nr(struct super_block *sb,
2144f30a7d0cSTejun Heo 			    unsigned long nr,
2145f30a7d0cSTejun Heo 			    enum wb_reason reason)
2146f30a7d0cSTejun Heo {
2147f30a7d0cSTejun Heo 	__writeback_inodes_sb_nr(sb, nr, reason, false);
2148f30a7d0cSTejun Heo }
21493259f8beSChris Mason EXPORT_SYMBOL(writeback_inodes_sb_nr);
21503259f8beSChris Mason 
21513259f8beSChris Mason /**
21523259f8beSChris Mason  * writeback_inodes_sb	-	writeback dirty inodes from given super_block
21533259f8beSChris Mason  * @sb: the superblock
2154786228abSMarcos Paulo de Souza  * @reason: reason why some writeback work was initiated
21553259f8beSChris Mason  *
21563259f8beSChris Mason  * Start writeback on some inodes on this super_block. No guarantees are made
21573259f8beSChris Mason  * on how many (if any) will be written, and this function does not wait
21583259f8beSChris Mason  * for IO completion of submitted IO.
21593259f8beSChris Mason  */
21600e175a18SCurt Wohlgemuth void writeback_inodes_sb(struct super_block *sb, enum wb_reason reason)
21613259f8beSChris Mason {
21620e175a18SCurt Wohlgemuth 	return writeback_inodes_sb_nr(sb, get_nr_dirty_pages(), reason);
21633259f8beSChris Mason }
2164d8a8559cSJens Axboe EXPORT_SYMBOL(writeback_inodes_sb);
2165d8a8559cSJens Axboe 
2166d8a8559cSJens Axboe /**
216710ee27a0SMiao Xie  * try_to_writeback_inodes_sb_nr - try to start writeback if none underway
21683259f8beSChris Mason  * @sb: the superblock
21693259f8beSChris Mason  * @nr: the number of pages to write
217010ee27a0SMiao Xie  * @reason: the reason of writeback
21713259f8beSChris Mason  *
217210ee27a0SMiao Xie  * Invoke writeback_inodes_sb_nr if no writeback is currently underway.
21733259f8beSChris Mason  * Returns 1 if writeback was started, 0 if not.
21743259f8beSChris Mason  */
2175f30a7d0cSTejun Heo bool try_to_writeback_inodes_sb_nr(struct super_block *sb, unsigned long nr,
21760e175a18SCurt Wohlgemuth 				   enum wb_reason reason)
21773259f8beSChris Mason {
217810ee27a0SMiao Xie 	if (!down_read_trylock(&sb->s_umount))
2179f30a7d0cSTejun Heo 		return false;
218010ee27a0SMiao Xie 
2181f30a7d0cSTejun Heo 	__writeback_inodes_sb_nr(sb, nr, reason, true);
21823259f8beSChris Mason 	up_read(&sb->s_umount);
2183f30a7d0cSTejun Heo 	return true;
21843259f8beSChris Mason }
218510ee27a0SMiao Xie EXPORT_SYMBOL(try_to_writeback_inodes_sb_nr);
218610ee27a0SMiao Xie 
218710ee27a0SMiao Xie /**
218810ee27a0SMiao Xie  * try_to_writeback_inodes_sb - try to start writeback if none underway
218910ee27a0SMiao Xie  * @sb: the superblock
219010ee27a0SMiao Xie  * @reason: reason why some writeback work was initiated
219110ee27a0SMiao Xie  *
219210ee27a0SMiao Xie  * Implement by try_to_writeback_inodes_sb_nr()
219310ee27a0SMiao Xie  * Returns 1 if writeback was started, 0 if not.
219410ee27a0SMiao Xie  */
2195f30a7d0cSTejun Heo bool try_to_writeback_inodes_sb(struct super_block *sb, enum wb_reason reason)
219610ee27a0SMiao Xie {
219710ee27a0SMiao Xie 	return try_to_writeback_inodes_sb_nr(sb, get_nr_dirty_pages(), reason);
219810ee27a0SMiao Xie }
219910ee27a0SMiao Xie EXPORT_SYMBOL(try_to_writeback_inodes_sb);
22003259f8beSChris Mason 
22013259f8beSChris Mason /**
2202d8a8559cSJens Axboe  * sync_inodes_sb	-	sync sb inode pages
2203d8a8559cSJens Axboe  * @sb: the superblock
2204d8a8559cSJens Axboe  *
2205d8a8559cSJens Axboe  * This function writes and waits on any dirty inode belonging to this
22060dc83bd3SJan Kara  * super_block.
2207d8a8559cSJens Axboe  */
22080dc83bd3SJan Kara void sync_inodes_sb(struct super_block *sb)
2209d8a8559cSJens Axboe {
2210cc395d7fSTejun Heo 	DEFINE_WB_COMPLETION_ONSTACK(done);
221183ba7b07SChristoph Hellwig 	struct wb_writeback_work work = {
22123c4d7165SChristoph Hellwig 		.sb		= sb,
22133c4d7165SChristoph Hellwig 		.sync_mode	= WB_SYNC_ALL,
22143c4d7165SChristoph Hellwig 		.nr_pages	= LONG_MAX,
22153c4d7165SChristoph Hellwig 		.range_cyclic	= 0,
221683ba7b07SChristoph Hellwig 		.done		= &done,
22170e175a18SCurt Wohlgemuth 		.reason		= WB_REASON_SYNC,
22187747bd4bSDave Chinner 		.for_sync	= 1,
22193c4d7165SChristoph Hellwig 	};
2220e7972912STejun Heo 	struct backing_dev_info *bdi = sb->s_bdi;
22213c4d7165SChristoph Hellwig 
22226eedc701SJan Kara 	/* Nothing to do? */
2223e7972912STejun Heo 	if (!bdi_has_dirty_io(bdi) || bdi == &noop_backing_dev_info)
22246eedc701SJan Kara 		return;
2225cf37e972SChristoph Hellwig 	WARN_ON(!rwsem_is_locked(&sb->s_umount));
2226cf37e972SChristoph Hellwig 
2227db125360STejun Heo 	bdi_split_work_to_wbs(bdi, &work, false);
2228cc395d7fSTejun Heo 	wb_wait_for_completion(bdi, &done);
222983ba7b07SChristoph Hellwig 
2230b6e51316SJens Axboe 	wait_sb_inodes(sb);
2231d8a8559cSJens Axboe }
2232d8a8559cSJens Axboe EXPORT_SYMBOL(sync_inodes_sb);
22331da177e4SLinus Torvalds 
22341da177e4SLinus Torvalds /**
22351da177e4SLinus Torvalds  * write_inode_now	-	write an inode to disk
22361da177e4SLinus Torvalds  * @inode: inode to write to disk
22371da177e4SLinus Torvalds  * @sync: whether the write should be synchronous or not
22381da177e4SLinus Torvalds  *
22397f04c26dSAndrea Arcangeli  * This function commits an inode to disk immediately if it is dirty. This is
22407f04c26dSAndrea Arcangeli  * primarily needed by knfsd.
22417f04c26dSAndrea Arcangeli  *
22427f04c26dSAndrea Arcangeli  * The caller must either have a ref on the inode or must have set I_WILL_FREE.
22431da177e4SLinus Torvalds  */
22441da177e4SLinus Torvalds int write_inode_now(struct inode *inode, int sync)
22451da177e4SLinus Torvalds {
2246f758eeabSChristoph Hellwig 	struct bdi_writeback *wb = &inode_to_bdi(inode)->wb;
22471da177e4SLinus Torvalds 	struct writeback_control wbc = {
22481da177e4SLinus Torvalds 		.nr_to_write = LONG_MAX,
224918914b18SMike Galbraith 		.sync_mode = sync ? WB_SYNC_ALL : WB_SYNC_NONE,
2250111ebb6eSOGAWA Hirofumi 		.range_start = 0,
2251111ebb6eSOGAWA Hirofumi 		.range_end = LLONG_MAX,
22521da177e4SLinus Torvalds 	};
22531da177e4SLinus Torvalds 
22541da177e4SLinus Torvalds 	if (!mapping_cap_writeback_dirty(inode->i_mapping))
225549364ce2SAndrew Morton 		wbc.nr_to_write = 0;
22561da177e4SLinus Torvalds 
22571da177e4SLinus Torvalds 	might_sleep();
22584f8ad655SJan Kara 	return writeback_single_inode(inode, wb, &wbc);
22591da177e4SLinus Torvalds }
22601da177e4SLinus Torvalds EXPORT_SYMBOL(write_inode_now);
22611da177e4SLinus Torvalds 
22621da177e4SLinus Torvalds /**
22631da177e4SLinus Torvalds  * sync_inode - write an inode and its pages to disk.
22641da177e4SLinus Torvalds  * @inode: the inode to sync
22651da177e4SLinus Torvalds  * @wbc: controls the writeback mode
22661da177e4SLinus Torvalds  *
22671da177e4SLinus Torvalds  * sync_inode() will write an inode and its pages to disk.  It will also
22681da177e4SLinus Torvalds  * correctly update the inode on its superblock's dirty inode lists and will
22691da177e4SLinus Torvalds  * update inode->i_state.
22701da177e4SLinus Torvalds  *
22711da177e4SLinus Torvalds  * The caller must have a ref on the inode.
22721da177e4SLinus Torvalds  */
22731da177e4SLinus Torvalds int sync_inode(struct inode *inode, struct writeback_control *wbc)
22741da177e4SLinus Torvalds {
22754f8ad655SJan Kara 	return writeback_single_inode(inode, &inode_to_bdi(inode)->wb, wbc);
22761da177e4SLinus Torvalds }
22771da177e4SLinus Torvalds EXPORT_SYMBOL(sync_inode);
2278c3765016SChristoph Hellwig 
2279c3765016SChristoph Hellwig /**
2280c691b9d9SAndrew Morton  * sync_inode_metadata - write an inode to disk
2281c3765016SChristoph Hellwig  * @inode: the inode to sync
2282c3765016SChristoph Hellwig  * @wait: wait for I/O to complete.
2283c3765016SChristoph Hellwig  *
2284c691b9d9SAndrew Morton  * Write an inode to disk and adjust its dirty state after completion.
2285c3765016SChristoph Hellwig  *
2286c3765016SChristoph Hellwig  * Note: only writes the actual inode, no associated data or other metadata.
2287c3765016SChristoph Hellwig  */
2288c3765016SChristoph Hellwig int sync_inode_metadata(struct inode *inode, int wait)
2289c3765016SChristoph Hellwig {
2290c3765016SChristoph Hellwig 	struct writeback_control wbc = {
2291c3765016SChristoph Hellwig 		.sync_mode = wait ? WB_SYNC_ALL : WB_SYNC_NONE,
2292c3765016SChristoph Hellwig 		.nr_to_write = 0, /* metadata-only */
2293c3765016SChristoph Hellwig 	};
2294c3765016SChristoph Hellwig 
2295c3765016SChristoph Hellwig 	return sync_inode(inode, &wbc);
2296c3765016SChristoph Hellwig }
2297c3765016SChristoph Hellwig EXPORT_SYMBOL(sync_inode_metadata);
2298