xref: /openbmc/linux/fs/fs-writeback.c (revision aaa2cacf)
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 */
5698754bf7STejun Heo 	unsigned int single_wait:1;
5798754bf7STejun Heo 	unsigned int single_done:1;
580e175a18SCurt Wohlgemuth 	enum wb_reason reason;		/* why was writeback initiated? */
59c4a77a6cSJens Axboe 
608010c3b6SJens Axboe 	struct list_head list;		/* pending work list */
61cc395d7fSTejun Heo 	struct wb_completion *done;	/* set if the caller waits */
6203ba3782SJens Axboe };
6303ba3782SJens Axboe 
64a2f48706STheodore Ts'o /*
65cc395d7fSTejun Heo  * If one wants to wait for one or more wb_writeback_works, each work's
66cc395d7fSTejun Heo  * ->done should be set to a wb_completion defined using the following
67cc395d7fSTejun Heo  * macro.  Once all work items are issued with wb_queue_work(), the caller
68cc395d7fSTejun Heo  * can wait for the completion of all using wb_wait_for_completion().  Work
69cc395d7fSTejun Heo  * items which are waited upon aren't freed automatically on completion.
70cc395d7fSTejun Heo  */
71cc395d7fSTejun Heo #define DEFINE_WB_COMPLETION_ONSTACK(cmpl)				\
72cc395d7fSTejun Heo 	struct wb_completion cmpl = {					\
73cc395d7fSTejun Heo 		.cnt		= ATOMIC_INIT(1),			\
74cc395d7fSTejun Heo 	}
75cc395d7fSTejun Heo 
76cc395d7fSTejun Heo 
77cc395d7fSTejun Heo /*
78a2f48706STheodore Ts'o  * If an inode is constantly having its pages dirtied, but then the
79a2f48706STheodore Ts'o  * updates stop dirtytime_expire_interval seconds in the past, it's
80a2f48706STheodore Ts'o  * possible for the worst case time between when an inode has its
81a2f48706STheodore Ts'o  * timestamps updated and when they finally get written out to be two
82a2f48706STheodore Ts'o  * dirtytime_expire_intervals.  We set the default to 12 hours (in
83a2f48706STheodore Ts'o  * seconds), which means most of the time inodes will have their
84a2f48706STheodore Ts'o  * timestamps written to disk after 12 hours, but in the worst case a
85a2f48706STheodore Ts'o  * few inodes might not their timestamps updated for 24 hours.
86a2f48706STheodore Ts'o  */
87a2f48706STheodore Ts'o unsigned int dirtytime_expire_interval = 12 * 60 * 60;
88a2f48706STheodore Ts'o 
897ccf19a8SNick Piggin static inline struct inode *wb_inode(struct list_head *head)
907ccf19a8SNick Piggin {
917ccf19a8SNick Piggin 	return list_entry(head, struct inode, i_wb_list);
927ccf19a8SNick Piggin }
937ccf19a8SNick Piggin 
9415eb77a0SWu Fengguang /*
9515eb77a0SWu Fengguang  * Include the creation of the trace points after defining the
9615eb77a0SWu Fengguang  * wb_writeback_work structure and inline functions so that the definition
9715eb77a0SWu Fengguang  * remains local to this file.
9815eb77a0SWu Fengguang  */
9915eb77a0SWu Fengguang #define CREATE_TRACE_POINTS
10015eb77a0SWu Fengguang #include <trace/events/writeback.h>
10115eb77a0SWu Fengguang 
102774016b2SSteven Whitehouse EXPORT_TRACEPOINT_SYMBOL_GPL(wbc_writepage);
103774016b2SSteven Whitehouse 
104d6c10f1fSTejun Heo static bool wb_io_lists_populated(struct bdi_writeback *wb)
105d6c10f1fSTejun Heo {
106d6c10f1fSTejun Heo 	if (wb_has_dirty_io(wb)) {
107d6c10f1fSTejun Heo 		return false;
108d6c10f1fSTejun Heo 	} else {
109d6c10f1fSTejun Heo 		set_bit(WB_has_dirty_io, &wb->state);
11095a46c65STejun Heo 		WARN_ON_ONCE(!wb->avg_write_bandwidth);
111766a9d6eSTejun Heo 		atomic_long_add(wb->avg_write_bandwidth,
112766a9d6eSTejun Heo 				&wb->bdi->tot_write_bandwidth);
113d6c10f1fSTejun Heo 		return true;
114d6c10f1fSTejun Heo 	}
115d6c10f1fSTejun Heo }
116d6c10f1fSTejun Heo 
117d6c10f1fSTejun Heo static void wb_io_lists_depopulated(struct bdi_writeback *wb)
118d6c10f1fSTejun Heo {
119d6c10f1fSTejun Heo 	if (wb_has_dirty_io(wb) && list_empty(&wb->b_dirty) &&
120766a9d6eSTejun Heo 	    list_empty(&wb->b_io) && list_empty(&wb->b_more_io)) {
121d6c10f1fSTejun Heo 		clear_bit(WB_has_dirty_io, &wb->state);
12295a46c65STejun Heo 		WARN_ON_ONCE(atomic_long_sub_return(wb->avg_write_bandwidth,
12395a46c65STejun Heo 					&wb->bdi->tot_write_bandwidth) < 0);
124766a9d6eSTejun Heo 	}
125d6c10f1fSTejun Heo }
126d6c10f1fSTejun Heo 
127d6c10f1fSTejun Heo /**
128d6c10f1fSTejun Heo  * inode_wb_list_move_locked - move an inode onto a bdi_writeback IO list
129d6c10f1fSTejun Heo  * @inode: inode to be moved
130d6c10f1fSTejun Heo  * @wb: target bdi_writeback
131d6c10f1fSTejun Heo  * @head: one of @wb->b_{dirty|io|more_io}
132d6c10f1fSTejun Heo  *
133d6c10f1fSTejun Heo  * Move @inode->i_wb_list to @list of @wb and set %WB_has_dirty_io.
134d6c10f1fSTejun Heo  * Returns %true if @inode is the first occupant of the !dirty_time IO
135d6c10f1fSTejun Heo  * lists; otherwise, %false.
136d6c10f1fSTejun Heo  */
137d6c10f1fSTejun Heo static bool inode_wb_list_move_locked(struct inode *inode,
138d6c10f1fSTejun Heo 				      struct bdi_writeback *wb,
139d6c10f1fSTejun Heo 				      struct list_head *head)
140d6c10f1fSTejun Heo {
141d6c10f1fSTejun Heo 	assert_spin_locked(&wb->list_lock);
142d6c10f1fSTejun Heo 
143d6c10f1fSTejun Heo 	list_move(&inode->i_wb_list, head);
144d6c10f1fSTejun Heo 
145d6c10f1fSTejun Heo 	/* dirty_time doesn't count as dirty_io until expiration */
146d6c10f1fSTejun Heo 	if (head != &wb->b_dirty_time)
147d6c10f1fSTejun Heo 		return wb_io_lists_populated(wb);
148d6c10f1fSTejun Heo 
149d6c10f1fSTejun Heo 	wb_io_lists_depopulated(wb);
150d6c10f1fSTejun Heo 	return false;
151d6c10f1fSTejun Heo }
152d6c10f1fSTejun Heo 
153d6c10f1fSTejun Heo /**
154d6c10f1fSTejun Heo  * inode_wb_list_del_locked - remove an inode from its bdi_writeback IO list
155d6c10f1fSTejun Heo  * @inode: inode to be removed
156d6c10f1fSTejun Heo  * @wb: bdi_writeback @inode is being removed from
157d6c10f1fSTejun Heo  *
158d6c10f1fSTejun Heo  * Remove @inode which may be on one of @wb->b_{dirty|io|more_io} lists and
159d6c10f1fSTejun Heo  * clear %WB_has_dirty_io if all are empty afterwards.
160d6c10f1fSTejun Heo  */
161d6c10f1fSTejun Heo static void inode_wb_list_del_locked(struct inode *inode,
162d6c10f1fSTejun Heo 				     struct bdi_writeback *wb)
163d6c10f1fSTejun Heo {
164d6c10f1fSTejun Heo 	assert_spin_locked(&wb->list_lock);
165d6c10f1fSTejun Heo 
166d6c10f1fSTejun Heo 	list_del_init(&inode->i_wb_list);
167d6c10f1fSTejun Heo 	wb_io_lists_depopulated(wb);
168d6c10f1fSTejun Heo }
169d6c10f1fSTejun Heo 
170f0054bb1STejun Heo static void wb_wakeup(struct bdi_writeback *wb)
1715acda9d1SJan Kara {
172f0054bb1STejun Heo 	spin_lock_bh(&wb->work_lock);
173f0054bb1STejun Heo 	if (test_bit(WB_registered, &wb->state))
174f0054bb1STejun Heo 		mod_delayed_work(bdi_wq, &wb->dwork, 0);
175f0054bb1STejun Heo 	spin_unlock_bh(&wb->work_lock);
1765acda9d1SJan Kara }
1775acda9d1SJan Kara 
178f0054bb1STejun Heo static void wb_queue_work(struct bdi_writeback *wb,
1796585027aSJan Kara 			  struct wb_writeback_work *work)
1806585027aSJan Kara {
181f0054bb1STejun Heo 	trace_writeback_queue(wb->bdi, work);
1826585027aSJan Kara 
183f0054bb1STejun Heo 	spin_lock_bh(&wb->work_lock);
18498754bf7STejun Heo 	if (!test_bit(WB_registered, &wb->state)) {
18598754bf7STejun Heo 		if (work->single_wait)
18698754bf7STejun Heo 			work->single_done = 1;
1875acda9d1SJan Kara 		goto out_unlock;
18898754bf7STejun Heo 	}
189cc395d7fSTejun Heo 	if (work->done)
190cc395d7fSTejun Heo 		atomic_inc(&work->done->cnt);
191f0054bb1STejun Heo 	list_add_tail(&work->list, &wb->work_list);
192f0054bb1STejun Heo 	mod_delayed_work(bdi_wq, &wb->dwork, 0);
1935acda9d1SJan Kara out_unlock:
194f0054bb1STejun Heo 	spin_unlock_bh(&wb->work_lock);
19503ba3782SJens Axboe }
1961da177e4SLinus Torvalds 
197cc395d7fSTejun Heo /**
198cc395d7fSTejun Heo  * wb_wait_for_completion - wait for completion of bdi_writeback_works
199cc395d7fSTejun Heo  * @bdi: bdi work items were issued to
200cc395d7fSTejun Heo  * @done: target wb_completion
201cc395d7fSTejun Heo  *
202cc395d7fSTejun Heo  * Wait for one or more work items issued to @bdi with their ->done field
203cc395d7fSTejun Heo  * set to @done, which should have been defined with
204cc395d7fSTejun Heo  * DEFINE_WB_COMPLETION_ONSTACK().  This function returns after all such
205cc395d7fSTejun Heo  * work items are completed.  Work items which are waited upon aren't freed
206cc395d7fSTejun Heo  * automatically on completion.
207cc395d7fSTejun Heo  */
208cc395d7fSTejun Heo static void wb_wait_for_completion(struct backing_dev_info *bdi,
209cc395d7fSTejun Heo 				   struct wb_completion *done)
210cc395d7fSTejun Heo {
211cc395d7fSTejun Heo 	atomic_dec(&done->cnt);		/* put down the initial count */
212cc395d7fSTejun Heo 	wait_event(bdi->wb_waitq, !atomic_read(&done->cnt));
213cc395d7fSTejun Heo }
214cc395d7fSTejun Heo 
215703c2708STejun Heo #ifdef CONFIG_CGROUP_WRITEBACK
216703c2708STejun Heo 
2172a814908STejun Heo /* parameters for foreign inode detection, see wb_detach_inode() */
2182a814908STejun Heo #define WB_FRN_TIME_SHIFT	13	/* 1s = 2^13, upto 8 secs w/ 16bit */
2192a814908STejun Heo #define WB_FRN_TIME_AVG_SHIFT	3	/* avg = avg * 7/8 + new * 1/8 */
2202a814908STejun Heo #define WB_FRN_TIME_CUT_DIV	2	/* ignore rounds < avg / 2 */
2212a814908STejun Heo #define WB_FRN_TIME_PERIOD	(2 * (1 << WB_FRN_TIME_SHIFT))	/* 2s */
2222a814908STejun Heo 
2232a814908STejun Heo #define WB_FRN_HIST_SLOTS	16	/* inode->i_wb_frn_history is 16bit */
2242a814908STejun Heo #define WB_FRN_HIST_UNIT	(WB_FRN_TIME_PERIOD / WB_FRN_HIST_SLOTS)
2252a814908STejun Heo 					/* each slot's duration is 2s / 16 */
2262a814908STejun Heo #define WB_FRN_HIST_THR_SLOTS	(WB_FRN_HIST_SLOTS / 2)
2272a814908STejun Heo 					/* if foreign slots >= 8, switch */
2282a814908STejun Heo #define WB_FRN_HIST_MAX_SLOTS	(WB_FRN_HIST_THR_SLOTS / 2 + 1)
2292a814908STejun Heo 					/* one round can affect upto 5 slots */
2302a814908STejun Heo 
23121c6321fSTejun Heo void __inode_attach_wb(struct inode *inode, struct page *page)
23221c6321fSTejun Heo {
23321c6321fSTejun Heo 	struct backing_dev_info *bdi = inode_to_bdi(inode);
23421c6321fSTejun Heo 	struct bdi_writeback *wb = NULL;
23521c6321fSTejun Heo 
23621c6321fSTejun Heo 	if (inode_cgwb_enabled(inode)) {
23721c6321fSTejun Heo 		struct cgroup_subsys_state *memcg_css;
23821c6321fSTejun Heo 
23921c6321fSTejun Heo 		if (page) {
24021c6321fSTejun Heo 			memcg_css = mem_cgroup_css_from_page(page);
24121c6321fSTejun Heo 			wb = wb_get_create(bdi, memcg_css, GFP_ATOMIC);
24221c6321fSTejun Heo 		} else {
24321c6321fSTejun Heo 			/* must pin memcg_css, see wb_get_create() */
24421c6321fSTejun Heo 			memcg_css = task_get_css(current, memory_cgrp_id);
24521c6321fSTejun Heo 			wb = wb_get_create(bdi, memcg_css, GFP_ATOMIC);
24621c6321fSTejun Heo 			css_put(memcg_css);
24721c6321fSTejun Heo 		}
24821c6321fSTejun Heo 	}
24921c6321fSTejun Heo 
25021c6321fSTejun Heo 	if (!wb)
25121c6321fSTejun Heo 		wb = &bdi->wb;
25221c6321fSTejun Heo 
25321c6321fSTejun Heo 	/*
25421c6321fSTejun Heo 	 * There may be multiple instances of this function racing to
25521c6321fSTejun Heo 	 * update the same inode.  Use cmpxchg() to tell the winner.
25621c6321fSTejun Heo 	 */
25721c6321fSTejun Heo 	if (unlikely(cmpxchg(&inode->i_wb, NULL, wb)))
25821c6321fSTejun Heo 		wb_put(wb);
25921c6321fSTejun Heo }
26021c6321fSTejun Heo 
261703c2708STejun Heo /**
26287e1d789STejun Heo  * locked_inode_to_wb_and_lock_list - determine a locked inode's wb and lock it
26387e1d789STejun Heo  * @inode: inode of interest with i_lock held
26487e1d789STejun Heo  *
26587e1d789STejun Heo  * Returns @inode's wb with its list_lock held.  @inode->i_lock must be
26687e1d789STejun Heo  * held on entry and is released on return.  The returned wb is guaranteed
26787e1d789STejun Heo  * to stay @inode's associated wb until its list_lock is released.
26887e1d789STejun Heo  */
26987e1d789STejun Heo static struct bdi_writeback *
27087e1d789STejun Heo locked_inode_to_wb_and_lock_list(struct inode *inode)
27187e1d789STejun Heo 	__releases(&inode->i_lock)
27287e1d789STejun Heo 	__acquires(&wb->list_lock)
27387e1d789STejun Heo {
27487e1d789STejun Heo 	while (true) {
27587e1d789STejun Heo 		struct bdi_writeback *wb = inode_to_wb(inode);
27687e1d789STejun Heo 
27787e1d789STejun Heo 		/*
27887e1d789STejun Heo 		 * inode_to_wb() association is protected by both
27987e1d789STejun Heo 		 * @inode->i_lock and @wb->list_lock but list_lock nests
28087e1d789STejun Heo 		 * outside i_lock.  Drop i_lock and verify that the
28187e1d789STejun Heo 		 * association hasn't changed after acquiring list_lock.
28287e1d789STejun Heo 		 */
28387e1d789STejun Heo 		wb_get(wb);
28487e1d789STejun Heo 		spin_unlock(&inode->i_lock);
28587e1d789STejun Heo 		spin_lock(&wb->list_lock);
28687e1d789STejun Heo 		wb_put(wb);		/* not gonna deref it anymore */
28787e1d789STejun Heo 
288aaa2cacfSTejun Heo 		/* i_wb may have changed inbetween, can't use inode_to_wb() */
289aaa2cacfSTejun Heo 		if (likely(wb == inode->i_wb))
29087e1d789STejun Heo 			return wb;	/* @inode already has ref */
29187e1d789STejun Heo 
29287e1d789STejun Heo 		spin_unlock(&wb->list_lock);
29387e1d789STejun Heo 		cpu_relax();
29487e1d789STejun Heo 		spin_lock(&inode->i_lock);
29587e1d789STejun Heo 	}
29687e1d789STejun Heo }
29787e1d789STejun Heo 
29887e1d789STejun Heo /**
29987e1d789STejun Heo  * inode_to_wb_and_lock_list - determine an inode's wb and lock it
30087e1d789STejun Heo  * @inode: inode of interest
30187e1d789STejun Heo  *
30287e1d789STejun Heo  * Same as locked_inode_to_wb_and_lock_list() but @inode->i_lock isn't held
30387e1d789STejun Heo  * on entry.
30487e1d789STejun Heo  */
30587e1d789STejun Heo static struct bdi_writeback *inode_to_wb_and_lock_list(struct inode *inode)
30687e1d789STejun Heo 	__acquires(&wb->list_lock)
30787e1d789STejun Heo {
30887e1d789STejun Heo 	spin_lock(&inode->i_lock);
30987e1d789STejun Heo 	return locked_inode_to_wb_and_lock_list(inode);
31087e1d789STejun Heo }
31187e1d789STejun Heo 
312682aa8e1STejun Heo struct inode_switch_wbs_context {
313682aa8e1STejun Heo 	struct inode		*inode;
314682aa8e1STejun Heo 	struct bdi_writeback	*new_wb;
315682aa8e1STejun Heo 
316682aa8e1STejun Heo 	struct rcu_head		rcu_head;
317682aa8e1STejun Heo 	struct work_struct	work;
318682aa8e1STejun Heo };
319682aa8e1STejun Heo 
320682aa8e1STejun Heo static void inode_switch_wbs_work_fn(struct work_struct *work)
321682aa8e1STejun Heo {
322682aa8e1STejun Heo 	struct inode_switch_wbs_context *isw =
323682aa8e1STejun Heo 		container_of(work, struct inode_switch_wbs_context, work);
324682aa8e1STejun Heo 	struct inode *inode = isw->inode;
325682aa8e1STejun Heo 	struct bdi_writeback *new_wb = isw->new_wb;
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.
332682aa8e1STejun Heo 	 */
333682aa8e1STejun Heo 	spin_lock(&inode->i_lock);
334682aa8e1STejun Heo 
335682aa8e1STejun Heo 	inode->i_wb_frn_winner = 0;
336682aa8e1STejun Heo 	inode->i_wb_frn_avg_time = 0;
337682aa8e1STejun Heo 	inode->i_wb_frn_history = 0;
338682aa8e1STejun Heo 
339682aa8e1STejun Heo 	/*
340682aa8e1STejun Heo 	 * Paired with load_acquire in unlocked_inode_to_wb_begin() and
341682aa8e1STejun Heo 	 * ensures that the new wb is visible if they see !I_WB_SWITCH.
342682aa8e1STejun Heo 	 */
343682aa8e1STejun Heo 	smp_store_release(&inode->i_state, inode->i_state & ~I_WB_SWITCH);
344682aa8e1STejun Heo 
345682aa8e1STejun Heo 	spin_unlock(&inode->i_lock);
346682aa8e1STejun Heo 
347682aa8e1STejun Heo 	iput(inode);
348682aa8e1STejun Heo 	wb_put(new_wb);
349682aa8e1STejun Heo 	kfree(isw);
350682aa8e1STejun Heo }
351682aa8e1STejun Heo 
352682aa8e1STejun Heo static void inode_switch_wbs_rcu_fn(struct rcu_head *rcu_head)
353682aa8e1STejun Heo {
354682aa8e1STejun Heo 	struct inode_switch_wbs_context *isw = container_of(rcu_head,
355682aa8e1STejun Heo 				struct inode_switch_wbs_context, rcu_head);
356682aa8e1STejun Heo 
357682aa8e1STejun Heo 	/* needs to grab bh-unsafe locks, bounce to work item */
358682aa8e1STejun Heo 	INIT_WORK(&isw->work, inode_switch_wbs_work_fn);
359682aa8e1STejun Heo 	schedule_work(&isw->work);
360682aa8e1STejun Heo }
361682aa8e1STejun Heo 
362682aa8e1STejun Heo /**
363682aa8e1STejun Heo  * inode_switch_wbs - change the wb association of an inode
364682aa8e1STejun Heo  * @inode: target inode
365682aa8e1STejun Heo  * @new_wb_id: ID of the new wb
366682aa8e1STejun Heo  *
367682aa8e1STejun Heo  * Switch @inode's wb association to the wb identified by @new_wb_id.  The
368682aa8e1STejun Heo  * switching is performed asynchronously and may fail silently.
369682aa8e1STejun Heo  */
370682aa8e1STejun Heo static void inode_switch_wbs(struct inode *inode, int new_wb_id)
371682aa8e1STejun Heo {
372682aa8e1STejun Heo 	struct backing_dev_info *bdi = inode_to_bdi(inode);
373682aa8e1STejun Heo 	struct cgroup_subsys_state *memcg_css;
374682aa8e1STejun Heo 	struct inode_switch_wbs_context *isw;
375682aa8e1STejun Heo 
376682aa8e1STejun Heo 	/* noop if seems to be already in progress */
377682aa8e1STejun Heo 	if (inode->i_state & I_WB_SWITCH)
378682aa8e1STejun Heo 		return;
379682aa8e1STejun Heo 
380682aa8e1STejun Heo 	isw = kzalloc(sizeof(*isw), GFP_ATOMIC);
381682aa8e1STejun Heo 	if (!isw)
382682aa8e1STejun Heo 		return;
383682aa8e1STejun Heo 
384682aa8e1STejun Heo 	/* find and pin the new wb */
385682aa8e1STejun Heo 	rcu_read_lock();
386682aa8e1STejun Heo 	memcg_css = css_from_id(new_wb_id, &memory_cgrp_subsys);
387682aa8e1STejun Heo 	if (memcg_css)
388682aa8e1STejun Heo 		isw->new_wb = wb_get_create(bdi, memcg_css, GFP_ATOMIC);
389682aa8e1STejun Heo 	rcu_read_unlock();
390682aa8e1STejun Heo 	if (!isw->new_wb)
391682aa8e1STejun Heo 		goto out_free;
392682aa8e1STejun Heo 
393682aa8e1STejun Heo 	/* while holding I_WB_SWITCH, no one else can update the association */
394682aa8e1STejun Heo 	spin_lock(&inode->i_lock);
395682aa8e1STejun Heo 	if (inode->i_state & (I_WB_SWITCH | I_FREEING) ||
396682aa8e1STejun Heo 	    inode_to_wb(inode) == isw->new_wb) {
397682aa8e1STejun Heo 		spin_unlock(&inode->i_lock);
398682aa8e1STejun Heo 		goto out_free;
399682aa8e1STejun Heo 	}
400682aa8e1STejun Heo 	inode->i_state |= I_WB_SWITCH;
401682aa8e1STejun Heo 	spin_unlock(&inode->i_lock);
402682aa8e1STejun Heo 
403682aa8e1STejun Heo 	ihold(inode);
404682aa8e1STejun Heo 	isw->inode = inode;
405682aa8e1STejun Heo 
406682aa8e1STejun Heo 	/*
407682aa8e1STejun Heo 	 * In addition to synchronizing among switchers, I_WB_SWITCH tells
408682aa8e1STejun Heo 	 * the RCU protected stat update paths to grab the mapping's
409682aa8e1STejun Heo 	 * tree_lock so that stat transfer can synchronize against them.
410682aa8e1STejun Heo 	 * Let's continue after I_WB_SWITCH is guaranteed to be visible.
411682aa8e1STejun Heo 	 */
412682aa8e1STejun Heo 	call_rcu(&isw->rcu_head, inode_switch_wbs_rcu_fn);
413682aa8e1STejun Heo 	return;
414682aa8e1STejun Heo 
415682aa8e1STejun Heo out_free:
416682aa8e1STejun Heo 	if (isw->new_wb)
417682aa8e1STejun Heo 		wb_put(isw->new_wb);
418682aa8e1STejun Heo 	kfree(isw);
419682aa8e1STejun Heo }
420682aa8e1STejun Heo 
42187e1d789STejun Heo /**
422b16b1debSTejun Heo  * wbc_attach_and_unlock_inode - associate wbc with target inode and unlock it
423b16b1debSTejun Heo  * @wbc: writeback_control of interest
424b16b1debSTejun Heo  * @inode: target inode
425b16b1debSTejun Heo  *
426b16b1debSTejun Heo  * @inode is locked and about to be written back under the control of @wbc.
427b16b1debSTejun Heo  * Record @inode's writeback context into @wbc and unlock the i_lock.  On
428b16b1debSTejun Heo  * writeback completion, wbc_detach_inode() should be called.  This is used
429b16b1debSTejun Heo  * to track the cgroup writeback context.
430b16b1debSTejun Heo  */
431b16b1debSTejun Heo void wbc_attach_and_unlock_inode(struct writeback_control *wbc,
432b16b1debSTejun Heo 				 struct inode *inode)
433b16b1debSTejun Heo {
434b16b1debSTejun Heo 	wbc->wb = inode_to_wb(inode);
4352a814908STejun Heo 	wbc->inode = inode;
4362a814908STejun Heo 
4372a814908STejun Heo 	wbc->wb_id = wbc->wb->memcg_css->id;
4382a814908STejun Heo 	wbc->wb_lcand_id = inode->i_wb_frn_winner;
4392a814908STejun Heo 	wbc->wb_tcand_id = 0;
4402a814908STejun Heo 	wbc->wb_bytes = 0;
4412a814908STejun Heo 	wbc->wb_lcand_bytes = 0;
4422a814908STejun Heo 	wbc->wb_tcand_bytes = 0;
4432a814908STejun Heo 
444b16b1debSTejun Heo 	wb_get(wbc->wb);
445b16b1debSTejun Heo 	spin_unlock(&inode->i_lock);
446b16b1debSTejun Heo }
447b16b1debSTejun Heo 
448b16b1debSTejun Heo /**
4492a814908STejun Heo  * wbc_detach_inode - disassociate wbc from inode and perform foreign detection
4502a814908STejun Heo  * @wbc: writeback_control of the just finished writeback
451b16b1debSTejun Heo  *
452b16b1debSTejun Heo  * To be called after a writeback attempt of an inode finishes and undoes
453b16b1debSTejun Heo  * wbc_attach_and_unlock_inode().  Can be called under any context.
4542a814908STejun Heo  *
4552a814908STejun Heo  * As concurrent write sharing of an inode is expected to be very rare and
4562a814908STejun Heo  * memcg only tracks page ownership on first-use basis severely confining
4572a814908STejun Heo  * the usefulness of such sharing, cgroup writeback tracks ownership
4582a814908STejun Heo  * per-inode.  While the support for concurrent write sharing of an inode
4592a814908STejun Heo  * is deemed unnecessary, an inode being written to by different cgroups at
4602a814908STejun Heo  * different points in time is a lot more common, and, more importantly,
4612a814908STejun Heo  * charging only by first-use can too readily lead to grossly incorrect
4622a814908STejun Heo  * behaviors (single foreign page can lead to gigabytes of writeback to be
4632a814908STejun Heo  * incorrectly attributed).
4642a814908STejun Heo  *
4652a814908STejun Heo  * To resolve this issue, cgroup writeback detects the majority dirtier of
4662a814908STejun Heo  * an inode and transfers the ownership to it.  To avoid unnnecessary
4672a814908STejun Heo  * oscillation, the detection mechanism keeps track of history and gives
4682a814908STejun Heo  * out the switch verdict only if the foreign usage pattern is stable over
4692a814908STejun Heo  * a certain amount of time and/or writeback attempts.
4702a814908STejun Heo  *
4712a814908STejun Heo  * On each writeback attempt, @wbc tries to detect the majority writer
4722a814908STejun Heo  * using Boyer-Moore majority vote algorithm.  In addition to the byte
4732a814908STejun Heo  * count from the majority voting, it also counts the bytes written for the
4742a814908STejun Heo  * current wb and the last round's winner wb (max of last round's current
4752a814908STejun Heo  * wb, the winner from two rounds ago, and the last round's majority
4762a814908STejun Heo  * candidate).  Keeping track of the historical winner helps the algorithm
4772a814908STejun Heo  * to semi-reliably detect the most active writer even when it's not the
4782a814908STejun Heo  * absolute majority.
4792a814908STejun Heo  *
4802a814908STejun Heo  * Once the winner of the round is determined, whether the winner is
4812a814908STejun Heo  * foreign or not and how much IO time the round consumed is recorded in
4822a814908STejun Heo  * inode->i_wb_frn_history.  If the amount of recorded foreign IO time is
4832a814908STejun Heo  * over a certain threshold, the switch verdict is given.
484b16b1debSTejun Heo  */
485b16b1debSTejun Heo void wbc_detach_inode(struct writeback_control *wbc)
486b16b1debSTejun Heo {
4872a814908STejun Heo 	struct bdi_writeback *wb = wbc->wb;
4882a814908STejun Heo 	struct inode *inode = wbc->inode;
4892a814908STejun Heo 	u16 history = inode->i_wb_frn_history;
4902a814908STejun Heo 	unsigned long avg_time = inode->i_wb_frn_avg_time;
4912a814908STejun Heo 	unsigned long max_bytes, max_time;
4922a814908STejun Heo 	int max_id;
4932a814908STejun Heo 
4942a814908STejun Heo 	/* pick the winner of this round */
4952a814908STejun Heo 	if (wbc->wb_bytes >= wbc->wb_lcand_bytes &&
4962a814908STejun Heo 	    wbc->wb_bytes >= wbc->wb_tcand_bytes) {
4972a814908STejun Heo 		max_id = wbc->wb_id;
4982a814908STejun Heo 		max_bytes = wbc->wb_bytes;
4992a814908STejun Heo 	} else if (wbc->wb_lcand_bytes >= wbc->wb_tcand_bytes) {
5002a814908STejun Heo 		max_id = wbc->wb_lcand_id;
5012a814908STejun Heo 		max_bytes = wbc->wb_lcand_bytes;
5022a814908STejun Heo 	} else {
5032a814908STejun Heo 		max_id = wbc->wb_tcand_id;
5042a814908STejun Heo 		max_bytes = wbc->wb_tcand_bytes;
5052a814908STejun Heo 	}
5062a814908STejun Heo 
5072a814908STejun Heo 	/*
5082a814908STejun Heo 	 * Calculate the amount of IO time the winner consumed and fold it
5092a814908STejun Heo 	 * into the running average kept per inode.  If the consumed IO
5102a814908STejun Heo 	 * time is lower than avag / WB_FRN_TIME_CUT_DIV, ignore it for
5112a814908STejun Heo 	 * deciding whether to switch or not.  This is to prevent one-off
5122a814908STejun Heo 	 * small dirtiers from skewing the verdict.
5132a814908STejun Heo 	 */
5142a814908STejun Heo 	max_time = DIV_ROUND_UP((max_bytes >> PAGE_SHIFT) << WB_FRN_TIME_SHIFT,
5152a814908STejun Heo 				wb->avg_write_bandwidth);
5162a814908STejun Heo 	if (avg_time)
5172a814908STejun Heo 		avg_time += (max_time >> WB_FRN_TIME_AVG_SHIFT) -
5182a814908STejun Heo 			    (avg_time >> WB_FRN_TIME_AVG_SHIFT);
5192a814908STejun Heo 	else
5202a814908STejun Heo 		avg_time = max_time;	/* immediate catch up on first run */
5212a814908STejun Heo 
5222a814908STejun Heo 	if (max_time >= avg_time / WB_FRN_TIME_CUT_DIV) {
5232a814908STejun Heo 		int slots;
5242a814908STejun Heo 
5252a814908STejun Heo 		/*
5262a814908STejun Heo 		 * The switch verdict is reached if foreign wb's consume
5272a814908STejun Heo 		 * more than a certain proportion of IO time in a
5282a814908STejun Heo 		 * WB_FRN_TIME_PERIOD.  This is loosely tracked by 16 slot
5292a814908STejun Heo 		 * history mask where each bit represents one sixteenth of
5302a814908STejun Heo 		 * the period.  Determine the number of slots to shift into
5312a814908STejun Heo 		 * history from @max_time.
5322a814908STejun Heo 		 */
5332a814908STejun Heo 		slots = min(DIV_ROUND_UP(max_time, WB_FRN_HIST_UNIT),
5342a814908STejun Heo 			    (unsigned long)WB_FRN_HIST_MAX_SLOTS);
5352a814908STejun Heo 		history <<= slots;
5362a814908STejun Heo 		if (wbc->wb_id != max_id)
5372a814908STejun Heo 			history |= (1U << slots) - 1;
5382a814908STejun Heo 
5392a814908STejun Heo 		/*
5402a814908STejun Heo 		 * Switch if the current wb isn't the consistent winner.
5412a814908STejun Heo 		 * If there are multiple closely competing dirtiers, the
5422a814908STejun Heo 		 * inode may switch across them repeatedly over time, which
5432a814908STejun Heo 		 * is okay.  The main goal is avoiding keeping an inode on
5442a814908STejun Heo 		 * the wrong wb for an extended period of time.
5452a814908STejun Heo 		 */
546682aa8e1STejun Heo 		if (hweight32(history) > WB_FRN_HIST_THR_SLOTS)
547682aa8e1STejun Heo 			inode_switch_wbs(inode, max_id);
5482a814908STejun Heo 	}
5492a814908STejun Heo 
5502a814908STejun Heo 	/*
5512a814908STejun Heo 	 * Multiple instances of this function may race to update the
5522a814908STejun Heo 	 * following fields but we don't mind occassional inaccuracies.
5532a814908STejun Heo 	 */
5542a814908STejun Heo 	inode->i_wb_frn_winner = max_id;
5552a814908STejun Heo 	inode->i_wb_frn_avg_time = min(avg_time, (unsigned long)U16_MAX);
5562a814908STejun Heo 	inode->i_wb_frn_history = history;
5572a814908STejun Heo 
558b16b1debSTejun Heo 	wb_put(wbc->wb);
559b16b1debSTejun Heo 	wbc->wb = NULL;
560b16b1debSTejun Heo }
561b16b1debSTejun Heo 
562b16b1debSTejun Heo /**
5632a814908STejun Heo  * wbc_account_io - account IO issued during writeback
5642a814908STejun Heo  * @wbc: writeback_control of the writeback in progress
5652a814908STejun Heo  * @page: page being written out
5662a814908STejun Heo  * @bytes: number of bytes being written out
5672a814908STejun Heo  *
5682a814908STejun Heo  * @bytes from @page are about to written out during the writeback
5692a814908STejun Heo  * controlled by @wbc.  Keep the book for foreign inode detection.  See
5702a814908STejun Heo  * wbc_detach_inode().
5712a814908STejun Heo  */
5722a814908STejun Heo void wbc_account_io(struct writeback_control *wbc, struct page *page,
5732a814908STejun Heo 		    size_t bytes)
5742a814908STejun Heo {
5752a814908STejun Heo 	int id;
5762a814908STejun Heo 
5772a814908STejun Heo 	/*
5782a814908STejun Heo 	 * pageout() path doesn't attach @wbc to the inode being written
5792a814908STejun Heo 	 * out.  This is intentional as we don't want the function to block
5802a814908STejun Heo 	 * behind a slow cgroup.  Ultimately, we want pageout() to kick off
5812a814908STejun Heo 	 * regular writeback instead of writing things out itself.
5822a814908STejun Heo 	 */
5832a814908STejun Heo 	if (!wbc->wb)
5842a814908STejun Heo 		return;
5852a814908STejun Heo 
5862a814908STejun Heo 	rcu_read_lock();
5872a814908STejun Heo 	id = mem_cgroup_css_from_page(page)->id;
5882a814908STejun Heo 	rcu_read_unlock();
5892a814908STejun Heo 
5902a814908STejun Heo 	if (id == wbc->wb_id) {
5912a814908STejun Heo 		wbc->wb_bytes += bytes;
5922a814908STejun Heo 		return;
5932a814908STejun Heo 	}
5942a814908STejun Heo 
5952a814908STejun Heo 	if (id == wbc->wb_lcand_id)
5962a814908STejun Heo 		wbc->wb_lcand_bytes += bytes;
5972a814908STejun Heo 
5982a814908STejun Heo 	/* Boyer-Moore majority vote algorithm */
5992a814908STejun Heo 	if (!wbc->wb_tcand_bytes)
6002a814908STejun Heo 		wbc->wb_tcand_id = id;
6012a814908STejun Heo 	if (id == wbc->wb_tcand_id)
6022a814908STejun Heo 		wbc->wb_tcand_bytes += bytes;
6032a814908STejun Heo 	else
6042a814908STejun Heo 		wbc->wb_tcand_bytes -= min(bytes, wbc->wb_tcand_bytes);
6052a814908STejun Heo }
6062a814908STejun Heo 
6072a814908STejun Heo /**
608703c2708STejun Heo  * inode_congested - test whether an inode is congested
609703c2708STejun Heo  * @inode: inode to test for congestion
610703c2708STejun Heo  * @cong_bits: mask of WB_[a]sync_congested bits to test
611703c2708STejun Heo  *
612703c2708STejun Heo  * Tests whether @inode is congested.  @cong_bits is the mask of congestion
613703c2708STejun Heo  * bits to test and the return value is the mask of set bits.
614703c2708STejun Heo  *
615703c2708STejun Heo  * If cgroup writeback is enabled for @inode, the congestion state is
616703c2708STejun Heo  * determined by whether the cgwb (cgroup bdi_writeback) for the blkcg
617703c2708STejun Heo  * associated with @inode is congested; otherwise, the root wb's congestion
618703c2708STejun Heo  * state is used.
619703c2708STejun Heo  */
620703c2708STejun Heo int inode_congested(struct inode *inode, int cong_bits)
621703c2708STejun Heo {
6225cb8b824STejun Heo 	/*
6235cb8b824STejun Heo 	 * Once set, ->i_wb never becomes NULL while the inode is alive.
6245cb8b824STejun Heo 	 * Start transaction iff ->i_wb is visible.
6255cb8b824STejun Heo 	 */
626aaa2cacfSTejun Heo 	if (inode && inode_to_wb_is_valid(inode)) {
6275cb8b824STejun Heo 		struct bdi_writeback *wb;
6285cb8b824STejun Heo 		bool locked, congested;
6295cb8b824STejun Heo 
6305cb8b824STejun Heo 		wb = unlocked_inode_to_wb_begin(inode, &locked);
6315cb8b824STejun Heo 		congested = wb_congested(wb, cong_bits);
6325cb8b824STejun Heo 		unlocked_inode_to_wb_end(inode, locked);
6335cb8b824STejun Heo 		return congested;
634703c2708STejun Heo 	}
635703c2708STejun Heo 
636703c2708STejun Heo 	return wb_congested(&inode_to_bdi(inode)->wb, cong_bits);
637703c2708STejun Heo }
638703c2708STejun Heo EXPORT_SYMBOL_GPL(inode_congested);
639703c2708STejun Heo 
640f2b65121STejun Heo /**
64198754bf7STejun Heo  * wb_wait_for_single_work - wait for completion of a single bdi_writeback_work
64298754bf7STejun Heo  * @bdi: bdi the work item was issued to
64398754bf7STejun Heo  * @work: work item to wait for
64498754bf7STejun Heo  *
64598754bf7STejun Heo  * Wait for the completion of @work which was issued to one of @bdi's
64698754bf7STejun Heo  * bdi_writeback's.  The caller must have set @work->single_wait before
64798754bf7STejun Heo  * issuing it.  This wait operates independently fo
64898754bf7STejun Heo  * wb_wait_for_completion() and also disables automatic freeing of @work.
64998754bf7STejun Heo  */
65098754bf7STejun Heo static void wb_wait_for_single_work(struct backing_dev_info *bdi,
65198754bf7STejun Heo 				    struct wb_writeback_work *work)
65298754bf7STejun Heo {
65398754bf7STejun Heo 	if (WARN_ON_ONCE(!work->single_wait))
65498754bf7STejun Heo 		return;
65598754bf7STejun Heo 
65698754bf7STejun Heo 	wait_event(bdi->wb_waitq, work->single_done);
65798754bf7STejun Heo 
65898754bf7STejun Heo 	/*
65998754bf7STejun Heo 	 * Paired with smp_wmb() in wb_do_writeback() and ensures that all
66098754bf7STejun Heo 	 * modifications to @work prior to assertion of ->single_done is
66198754bf7STejun Heo 	 * visible to the caller once this function returns.
66298754bf7STejun Heo 	 */
66398754bf7STejun Heo 	smp_rmb();
66498754bf7STejun Heo }
66598754bf7STejun Heo 
66698754bf7STejun Heo /**
667f2b65121STejun Heo  * wb_split_bdi_pages - split nr_pages to write according to bandwidth
668f2b65121STejun Heo  * @wb: target bdi_writeback to split @nr_pages to
669f2b65121STejun Heo  * @nr_pages: number of pages to write for the whole bdi
670f2b65121STejun Heo  *
671f2b65121STejun Heo  * Split @wb's portion of @nr_pages according to @wb's write bandwidth in
672f2b65121STejun Heo  * relation to the total write bandwidth of all wb's w/ dirty inodes on
673f2b65121STejun Heo  * @wb->bdi.
674f2b65121STejun Heo  */
675f2b65121STejun Heo static long wb_split_bdi_pages(struct bdi_writeback *wb, long nr_pages)
676f2b65121STejun Heo {
677f2b65121STejun Heo 	unsigned long this_bw = wb->avg_write_bandwidth;
678f2b65121STejun Heo 	unsigned long tot_bw = atomic_long_read(&wb->bdi->tot_write_bandwidth);
679f2b65121STejun Heo 
680f2b65121STejun Heo 	if (nr_pages == LONG_MAX)
681f2b65121STejun Heo 		return LONG_MAX;
682f2b65121STejun Heo 
683f2b65121STejun Heo 	/*
684f2b65121STejun Heo 	 * This may be called on clean wb's and proportional distribution
685f2b65121STejun Heo 	 * may not make sense, just use the original @nr_pages in those
686f2b65121STejun Heo 	 * cases.  In general, we wanna err on the side of writing more.
687f2b65121STejun Heo 	 */
688f2b65121STejun Heo 	if (!tot_bw || this_bw >= tot_bw)
689f2b65121STejun Heo 		return nr_pages;
690f2b65121STejun Heo 	else
691f2b65121STejun Heo 		return DIV_ROUND_UP_ULL((u64)nr_pages * this_bw, tot_bw);
692f2b65121STejun Heo }
693f2b65121STejun Heo 
694db125360STejun Heo /**
695db125360STejun Heo  * wb_clone_and_queue_work - clone a wb_writeback_work and issue it to a wb
696db125360STejun Heo  * @wb: target bdi_writeback
697db125360STejun Heo  * @base_work: source wb_writeback_work
698db125360STejun Heo  *
699db125360STejun Heo  * Try to make a clone of @base_work and issue it to @wb.  If cloning
700db125360STejun Heo  * succeeds, %true is returned; otherwise, @base_work is issued directly
701db125360STejun Heo  * and %false is returned.  In the latter case, the caller is required to
702db125360STejun Heo  * wait for @base_work's completion using wb_wait_for_single_work().
703db125360STejun Heo  *
704db125360STejun Heo  * A clone is auto-freed on completion.  @base_work never is.
705db125360STejun Heo  */
706db125360STejun Heo static bool wb_clone_and_queue_work(struct bdi_writeback *wb,
707db125360STejun Heo 				    struct wb_writeback_work *base_work)
708db125360STejun Heo {
709db125360STejun Heo 	struct wb_writeback_work *work;
710db125360STejun Heo 
711db125360STejun Heo 	work = kmalloc(sizeof(*work), GFP_ATOMIC);
712db125360STejun Heo 	if (work) {
713db125360STejun Heo 		*work = *base_work;
714db125360STejun Heo 		work->auto_free = 1;
715db125360STejun Heo 		work->single_wait = 0;
716db125360STejun Heo 	} else {
717db125360STejun Heo 		work = base_work;
718db125360STejun Heo 		work->auto_free = 0;
719db125360STejun Heo 		work->single_wait = 1;
720db125360STejun Heo 	}
721db125360STejun Heo 	work->single_done = 0;
722db125360STejun Heo 	wb_queue_work(wb, work);
723db125360STejun Heo 	return work != base_work;
724db125360STejun Heo }
725db125360STejun Heo 
726db125360STejun Heo /**
727db125360STejun Heo  * bdi_split_work_to_wbs - split a wb_writeback_work to all wb's of a bdi
728db125360STejun Heo  * @bdi: target backing_dev_info
729db125360STejun Heo  * @base_work: wb_writeback_work to issue
730db125360STejun Heo  * @skip_if_busy: skip wb's which already have writeback in progress
731db125360STejun Heo  *
732db125360STejun Heo  * Split and issue @base_work to all wb's (bdi_writeback's) of @bdi which
733db125360STejun Heo  * have dirty inodes.  If @base_work->nr_page isn't %LONG_MAX, it's
734db125360STejun Heo  * distributed to the busy wbs according to each wb's proportion in the
735db125360STejun Heo  * total active write bandwidth of @bdi.
736db125360STejun Heo  */
737db125360STejun Heo static void bdi_split_work_to_wbs(struct backing_dev_info *bdi,
738db125360STejun Heo 				  struct wb_writeback_work *base_work,
739db125360STejun Heo 				  bool skip_if_busy)
740db125360STejun Heo {
741db125360STejun Heo 	long nr_pages = base_work->nr_pages;
742db125360STejun Heo 	int next_blkcg_id = 0;
743db125360STejun Heo 	struct bdi_writeback *wb;
744db125360STejun Heo 	struct wb_iter iter;
745db125360STejun Heo 
746db125360STejun Heo 	might_sleep();
747db125360STejun Heo 
748db125360STejun Heo 	if (!bdi_has_dirty_io(bdi))
749db125360STejun Heo 		return;
750db125360STejun Heo restart:
751db125360STejun Heo 	rcu_read_lock();
752db125360STejun Heo 	bdi_for_each_wb(wb, bdi, &iter, next_blkcg_id) {
753db125360STejun Heo 		if (!wb_has_dirty_io(wb) ||
754db125360STejun Heo 		    (skip_if_busy && writeback_in_progress(wb)))
755db125360STejun Heo 			continue;
756db125360STejun Heo 
757db125360STejun Heo 		base_work->nr_pages = wb_split_bdi_pages(wb, nr_pages);
758db125360STejun Heo 		if (!wb_clone_and_queue_work(wb, base_work)) {
759db125360STejun Heo 			next_blkcg_id = wb->blkcg_css->id + 1;
760db125360STejun Heo 			rcu_read_unlock();
761db125360STejun Heo 			wb_wait_for_single_work(bdi, base_work);
762db125360STejun Heo 			goto restart;
763db125360STejun Heo 		}
764db125360STejun Heo 	}
765db125360STejun Heo 	rcu_read_unlock();
766db125360STejun Heo }
767db125360STejun Heo 
768f2b65121STejun Heo #else	/* CONFIG_CGROUP_WRITEBACK */
769f2b65121STejun Heo 
77087e1d789STejun Heo static struct bdi_writeback *
77187e1d789STejun Heo locked_inode_to_wb_and_lock_list(struct inode *inode)
77287e1d789STejun Heo 	__releases(&inode->i_lock)
77387e1d789STejun Heo 	__acquires(&wb->list_lock)
77487e1d789STejun Heo {
77587e1d789STejun Heo 	struct bdi_writeback *wb = inode_to_wb(inode);
77687e1d789STejun Heo 
77787e1d789STejun Heo 	spin_unlock(&inode->i_lock);
77887e1d789STejun Heo 	spin_lock(&wb->list_lock);
77987e1d789STejun Heo 	return wb;
78087e1d789STejun Heo }
78187e1d789STejun Heo 
78287e1d789STejun Heo static struct bdi_writeback *inode_to_wb_and_lock_list(struct inode *inode)
78387e1d789STejun Heo 	__acquires(&wb->list_lock)
78487e1d789STejun Heo {
78587e1d789STejun Heo 	struct bdi_writeback *wb = inode_to_wb(inode);
78687e1d789STejun Heo 
78787e1d789STejun Heo 	spin_lock(&wb->list_lock);
78887e1d789STejun Heo 	return wb;
78987e1d789STejun Heo }
79087e1d789STejun Heo 
791f2b65121STejun Heo static long wb_split_bdi_pages(struct bdi_writeback *wb, long nr_pages)
792f2b65121STejun Heo {
793f2b65121STejun Heo 	return nr_pages;
794f2b65121STejun Heo }
795f2b65121STejun Heo 
796db125360STejun Heo static void bdi_split_work_to_wbs(struct backing_dev_info *bdi,
797db125360STejun Heo 				  struct wb_writeback_work *base_work,
798db125360STejun Heo 				  bool skip_if_busy)
799db125360STejun Heo {
800db125360STejun Heo 	might_sleep();
801db125360STejun Heo 
802db125360STejun Heo 	if (bdi_has_dirty_io(bdi) &&
803db125360STejun Heo 	    (!skip_if_busy || !writeback_in_progress(&bdi->wb))) {
804db125360STejun Heo 		base_work->auto_free = 0;
805db125360STejun Heo 		base_work->single_wait = 0;
806db125360STejun Heo 		base_work->single_done = 0;
807db125360STejun Heo 		wb_queue_work(&bdi->wb, base_work);
808db125360STejun Heo 	}
809db125360STejun Heo }
810db125360STejun Heo 
811703c2708STejun Heo #endif	/* CONFIG_CGROUP_WRITEBACK */
812703c2708STejun Heo 
813c00ddad3STejun Heo void wb_start_writeback(struct bdi_writeback *wb, long nr_pages,
814c00ddad3STejun Heo 			bool range_cyclic, enum wb_reason reason)
815b6e51316SJens Axboe {
816c00ddad3STejun Heo 	struct wb_writeback_work *work;
817c00ddad3STejun Heo 
818c00ddad3STejun Heo 	if (!wb_has_dirty_io(wb))
819c00ddad3STejun Heo 		return;
820c00ddad3STejun Heo 
821c00ddad3STejun Heo 	/*
822c00ddad3STejun Heo 	 * This is WB_SYNC_NONE writeback, so if allocation fails just
823c00ddad3STejun Heo 	 * wakeup the thread for old dirty data writeback
824c00ddad3STejun Heo 	 */
825c00ddad3STejun Heo 	work = kzalloc(sizeof(*work), GFP_ATOMIC);
826c00ddad3STejun Heo 	if (!work) {
827c00ddad3STejun Heo 		trace_writeback_nowork(wb->bdi);
828c00ddad3STejun Heo 		wb_wakeup(wb);
829c00ddad3STejun Heo 		return;
830c00ddad3STejun Heo 	}
831c00ddad3STejun Heo 
832c00ddad3STejun Heo 	work->sync_mode	= WB_SYNC_NONE;
833c00ddad3STejun Heo 	work->nr_pages	= nr_pages;
834c00ddad3STejun Heo 	work->range_cyclic = range_cyclic;
835c00ddad3STejun Heo 	work->reason	= reason;
836ac7b19a3STejun Heo 	work->auto_free	= 1;
837c00ddad3STejun Heo 
838c00ddad3STejun Heo 	wb_queue_work(wb, work);
839d3ddec76SWu Fengguang }
840d3ddec76SWu Fengguang 
841c5444198SChristoph Hellwig /**
8429ecf4866STejun Heo  * wb_start_background_writeback - start background writeback
8439ecf4866STejun Heo  * @wb: bdi_writback to write from
844c5444198SChristoph Hellwig  *
845c5444198SChristoph Hellwig  * Description:
8466585027aSJan Kara  *   This makes sure WB_SYNC_NONE background writeback happens. When
8479ecf4866STejun Heo  *   this function returns, it is only guaranteed that for given wb
8486585027aSJan Kara  *   some IO is happening if we are over background dirty threshold.
8496585027aSJan Kara  *   Caller need not hold sb s_umount semaphore.
850c5444198SChristoph Hellwig  */
8519ecf4866STejun Heo void wb_start_background_writeback(struct bdi_writeback *wb)
852c5444198SChristoph Hellwig {
8536585027aSJan Kara 	/*
8546585027aSJan Kara 	 * We just wake up the flusher thread. It will perform background
8556585027aSJan Kara 	 * writeback as soon as there is no other work to do.
8566585027aSJan Kara 	 */
8579ecf4866STejun Heo 	trace_writeback_wake_background(wb->bdi);
8589ecf4866STejun Heo 	wb_wakeup(wb);
8591da177e4SLinus Torvalds }
8601da177e4SLinus Torvalds 
8611da177e4SLinus Torvalds /*
862a66979abSDave Chinner  * Remove the inode from the writeback list it is on.
863a66979abSDave Chinner  */
864a66979abSDave Chinner void inode_wb_list_del(struct inode *inode)
865a66979abSDave Chinner {
86687e1d789STejun Heo 	struct bdi_writeback *wb;
867a66979abSDave Chinner 
86887e1d789STejun Heo 	wb = inode_to_wb_and_lock_list(inode);
869d6c10f1fSTejun Heo 	inode_wb_list_del_locked(inode, wb);
87052ebea74STejun Heo 	spin_unlock(&wb->list_lock);
871f758eeabSChristoph Hellwig }
872a66979abSDave Chinner 
873a66979abSDave Chinner /*
8746610a0bcSAndrew Morton  * Redirty an inode: set its when-it-was dirtied timestamp and move it to the
8756610a0bcSAndrew Morton  * furthest end of its superblock's dirty-inode list.
8766610a0bcSAndrew Morton  *
8776610a0bcSAndrew Morton  * Before stamping the inode's ->dirtied_when, we check to see whether it is
87866f3b8e2SJens Axboe  * already the most-recently-dirtied inode on the b_dirty list.  If that is
8796610a0bcSAndrew Morton  * the case then the inode must have been redirtied while it was being written
8806610a0bcSAndrew Morton  * out and we don't reset its dirtied_when.
8816610a0bcSAndrew Morton  */
882f758eeabSChristoph Hellwig static void redirty_tail(struct inode *inode, struct bdi_writeback *wb)
8836610a0bcSAndrew Morton {
88403ba3782SJens Axboe 	if (!list_empty(&wb->b_dirty)) {
88566f3b8e2SJens Axboe 		struct inode *tail;
8866610a0bcSAndrew Morton 
8877ccf19a8SNick Piggin 		tail = wb_inode(wb->b_dirty.next);
88866f3b8e2SJens Axboe 		if (time_before(inode->dirtied_when, tail->dirtied_when))
8896610a0bcSAndrew Morton 			inode->dirtied_when = jiffies;
8906610a0bcSAndrew Morton 	}
891d6c10f1fSTejun Heo 	inode_wb_list_move_locked(inode, wb, &wb->b_dirty);
8926610a0bcSAndrew Morton }
8936610a0bcSAndrew Morton 
8946610a0bcSAndrew Morton /*
89566f3b8e2SJens Axboe  * requeue inode for re-scanning after bdi->b_io list is exhausted.
896c986d1e2SAndrew Morton  */
897f758eeabSChristoph Hellwig static void requeue_io(struct inode *inode, struct bdi_writeback *wb)
898c986d1e2SAndrew Morton {
899d6c10f1fSTejun Heo 	inode_wb_list_move_locked(inode, wb, &wb->b_more_io);
900c986d1e2SAndrew Morton }
901c986d1e2SAndrew Morton 
9021c0eeaf5SJoern Engel static void inode_sync_complete(struct inode *inode)
9031c0eeaf5SJoern Engel {
904365b94aeSJan Kara 	inode->i_state &= ~I_SYNC;
9054eff96ddSJan Kara 	/* If inode is clean an unused, put it into LRU now... */
9064eff96ddSJan Kara 	inode_add_lru(inode);
907365b94aeSJan Kara 	/* Waiters must see I_SYNC cleared before being woken up */
9081c0eeaf5SJoern Engel 	smp_mb();
9091c0eeaf5SJoern Engel 	wake_up_bit(&inode->i_state, __I_SYNC);
9101c0eeaf5SJoern Engel }
9111c0eeaf5SJoern Engel 
912d2caa3c5SJeff Layton static bool inode_dirtied_after(struct inode *inode, unsigned long t)
913d2caa3c5SJeff Layton {
914d2caa3c5SJeff Layton 	bool ret = time_after(inode->dirtied_when, t);
915d2caa3c5SJeff Layton #ifndef CONFIG_64BIT
916d2caa3c5SJeff Layton 	/*
917d2caa3c5SJeff Layton 	 * For inodes being constantly redirtied, dirtied_when can get stuck.
918d2caa3c5SJeff Layton 	 * It _appears_ to be in the future, but is actually in distant past.
919d2caa3c5SJeff Layton 	 * This test is necessary to prevent such wrapped-around relative times
9205b0830cbSJens Axboe 	 * from permanently stopping the whole bdi writeback.
921d2caa3c5SJeff Layton 	 */
922d2caa3c5SJeff Layton 	ret = ret && time_before_eq(inode->dirtied_when, jiffies);
923d2caa3c5SJeff Layton #endif
924d2caa3c5SJeff Layton 	return ret;
925d2caa3c5SJeff Layton }
926d2caa3c5SJeff Layton 
9270ae45f63STheodore Ts'o #define EXPIRE_DIRTY_ATIME 0x0001
9280ae45f63STheodore Ts'o 
929c986d1e2SAndrew Morton /*
9300e2f2b23SWang Sheng-Hui  * Move expired (dirtied before work->older_than_this) dirty inodes from
931697e6fedSJan Kara  * @delaying_queue to @dispatch_queue.
9322c136579SFengguang Wu  */
933e84d0a4fSWu Fengguang static int move_expired_inodes(struct list_head *delaying_queue,
9342c136579SFengguang Wu 			       struct list_head *dispatch_queue,
9350ae45f63STheodore Ts'o 			       int flags,
936ad4e38ddSCurt Wohlgemuth 			       struct wb_writeback_work *work)
9372c136579SFengguang Wu {
9380ae45f63STheodore Ts'o 	unsigned long *older_than_this = NULL;
9390ae45f63STheodore Ts'o 	unsigned long expire_time;
9405c03449dSShaohua Li 	LIST_HEAD(tmp);
9415c03449dSShaohua Li 	struct list_head *pos, *node;
942cf137307SJens Axboe 	struct super_block *sb = NULL;
9435c03449dSShaohua Li 	struct inode *inode;
944cf137307SJens Axboe 	int do_sb_sort = 0;
945e84d0a4fSWu Fengguang 	int moved = 0;
9465c03449dSShaohua Li 
9470ae45f63STheodore Ts'o 	if ((flags & EXPIRE_DIRTY_ATIME) == 0)
9480ae45f63STheodore Ts'o 		older_than_this = work->older_than_this;
949a2f48706STheodore Ts'o 	else if (!work->for_sync) {
950a2f48706STheodore Ts'o 		expire_time = jiffies - (dirtytime_expire_interval * HZ);
9510ae45f63STheodore Ts'o 		older_than_this = &expire_time;
9520ae45f63STheodore Ts'o 	}
9532c136579SFengguang Wu 	while (!list_empty(delaying_queue)) {
9547ccf19a8SNick Piggin 		inode = wb_inode(delaying_queue->prev);
9550ae45f63STheodore Ts'o 		if (older_than_this &&
9560ae45f63STheodore Ts'o 		    inode_dirtied_after(inode, *older_than_this))
9572c136579SFengguang Wu 			break;
958a8855990SJan Kara 		list_move(&inode->i_wb_list, &tmp);
959a8855990SJan Kara 		moved++;
9600ae45f63STheodore Ts'o 		if (flags & EXPIRE_DIRTY_ATIME)
9610ae45f63STheodore Ts'o 			set_bit(__I_DIRTY_TIME_EXPIRED, &inode->i_state);
962a8855990SJan Kara 		if (sb_is_blkdev_sb(inode->i_sb))
963a8855990SJan Kara 			continue;
964cf137307SJens Axboe 		if (sb && sb != inode->i_sb)
965cf137307SJens Axboe 			do_sb_sort = 1;
966cf137307SJens Axboe 		sb = inode->i_sb;
9675c03449dSShaohua Li 	}
9685c03449dSShaohua Li 
969cf137307SJens Axboe 	/* just one sb in list, splice to dispatch_queue and we're done */
970cf137307SJens Axboe 	if (!do_sb_sort) {
971cf137307SJens Axboe 		list_splice(&tmp, dispatch_queue);
972e84d0a4fSWu Fengguang 		goto out;
973cf137307SJens Axboe 	}
974cf137307SJens Axboe 
9755c03449dSShaohua Li 	/* Move inodes from one superblock together */
9765c03449dSShaohua Li 	while (!list_empty(&tmp)) {
9777ccf19a8SNick Piggin 		sb = wb_inode(tmp.prev)->i_sb;
9785c03449dSShaohua Li 		list_for_each_prev_safe(pos, node, &tmp) {
9797ccf19a8SNick Piggin 			inode = wb_inode(pos);
9805c03449dSShaohua Li 			if (inode->i_sb == sb)
9817ccf19a8SNick Piggin 				list_move(&inode->i_wb_list, dispatch_queue);
9822c136579SFengguang Wu 		}
9832c136579SFengguang Wu 	}
984e84d0a4fSWu Fengguang out:
985e84d0a4fSWu Fengguang 	return moved;
9865c03449dSShaohua Li }
9872c136579SFengguang Wu 
9882c136579SFengguang Wu /*
9892c136579SFengguang Wu  * Queue all expired dirty inodes for io, eldest first.
9904ea879b9SWu Fengguang  * Before
9914ea879b9SWu Fengguang  *         newly dirtied     b_dirty    b_io    b_more_io
9924ea879b9SWu Fengguang  *         =============>    gf         edc     BA
9934ea879b9SWu Fengguang  * After
9944ea879b9SWu Fengguang  *         newly dirtied     b_dirty    b_io    b_more_io
9954ea879b9SWu Fengguang  *         =============>    g          fBAedc
9964ea879b9SWu Fengguang  *                                           |
9974ea879b9SWu Fengguang  *                                           +--> dequeue for IO
9982c136579SFengguang Wu  */
999ad4e38ddSCurt Wohlgemuth static void queue_io(struct bdi_writeback *wb, struct wb_writeback_work *work)
10002c136579SFengguang Wu {
1001e84d0a4fSWu Fengguang 	int moved;
10020ae45f63STheodore Ts'o 
1003f758eeabSChristoph Hellwig 	assert_spin_locked(&wb->list_lock);
10044ea879b9SWu Fengguang 	list_splice_init(&wb->b_more_io, &wb->b_io);
10050ae45f63STheodore Ts'o 	moved = move_expired_inodes(&wb->b_dirty, &wb->b_io, 0, work);
10060ae45f63STheodore Ts'o 	moved += move_expired_inodes(&wb->b_dirty_time, &wb->b_io,
10070ae45f63STheodore Ts'o 				     EXPIRE_DIRTY_ATIME, work);
1008d6c10f1fSTejun Heo 	if (moved)
1009d6c10f1fSTejun Heo 		wb_io_lists_populated(wb);
1010ad4e38ddSCurt Wohlgemuth 	trace_writeback_queue_io(wb, work, moved);
101166f3b8e2SJens Axboe }
101266f3b8e2SJens Axboe 
1013a9185b41SChristoph Hellwig static int write_inode(struct inode *inode, struct writeback_control *wbc)
101466f3b8e2SJens Axboe {
10159fb0a7daSTejun Heo 	int ret;
10169fb0a7daSTejun Heo 
10179fb0a7daSTejun Heo 	if (inode->i_sb->s_op->write_inode && !is_bad_inode(inode)) {
10189fb0a7daSTejun Heo 		trace_writeback_write_inode_start(inode, wbc);
10199fb0a7daSTejun Heo 		ret = inode->i_sb->s_op->write_inode(inode, wbc);
10209fb0a7daSTejun Heo 		trace_writeback_write_inode(inode, wbc);
10219fb0a7daSTejun Heo 		return ret;
10229fb0a7daSTejun Heo 	}
102303ba3782SJens Axboe 	return 0;
102466f3b8e2SJens Axboe }
102508d8e974SFengguang Wu 
10262c136579SFengguang Wu /*
1027169ebd90SJan Kara  * Wait for writeback on an inode to complete. Called with i_lock held.
1028169ebd90SJan Kara  * Caller must make sure inode cannot go away when we drop i_lock.
102901c03194SChristoph Hellwig  */
1030169ebd90SJan Kara static void __inode_wait_for_writeback(struct inode *inode)
1031169ebd90SJan Kara 	__releases(inode->i_lock)
1032169ebd90SJan Kara 	__acquires(inode->i_lock)
103301c03194SChristoph Hellwig {
103401c03194SChristoph Hellwig 	DEFINE_WAIT_BIT(wq, &inode->i_state, __I_SYNC);
103501c03194SChristoph Hellwig 	wait_queue_head_t *wqh;
103601c03194SChristoph Hellwig 
103701c03194SChristoph Hellwig 	wqh = bit_waitqueue(&inode->i_state, __I_SYNC);
103858a9d3d8SRichard Kennedy 	while (inode->i_state & I_SYNC) {
1039250df6edSDave Chinner 		spin_unlock(&inode->i_lock);
104074316201SNeilBrown 		__wait_on_bit(wqh, &wq, bit_wait,
104174316201SNeilBrown 			      TASK_UNINTERRUPTIBLE);
1042250df6edSDave Chinner 		spin_lock(&inode->i_lock);
104358a9d3d8SRichard Kennedy 	}
104401c03194SChristoph Hellwig }
104501c03194SChristoph Hellwig 
104601c03194SChristoph Hellwig /*
1047169ebd90SJan Kara  * Wait for writeback on an inode to complete. Caller must have inode pinned.
1048169ebd90SJan Kara  */
1049169ebd90SJan Kara void inode_wait_for_writeback(struct inode *inode)
1050169ebd90SJan Kara {
1051169ebd90SJan Kara 	spin_lock(&inode->i_lock);
1052169ebd90SJan Kara 	__inode_wait_for_writeback(inode);
1053169ebd90SJan Kara 	spin_unlock(&inode->i_lock);
1054169ebd90SJan Kara }
1055169ebd90SJan Kara 
1056169ebd90SJan Kara /*
1057169ebd90SJan Kara  * Sleep until I_SYNC is cleared. This function must be called with i_lock
1058169ebd90SJan Kara  * held and drops it. It is aimed for callers not holding any inode reference
1059169ebd90SJan Kara  * so once i_lock is dropped, inode can go away.
1060169ebd90SJan Kara  */
1061169ebd90SJan Kara static void inode_sleep_on_writeback(struct inode *inode)
1062169ebd90SJan Kara 	__releases(inode->i_lock)
1063169ebd90SJan Kara {
1064169ebd90SJan Kara 	DEFINE_WAIT(wait);
1065169ebd90SJan Kara 	wait_queue_head_t *wqh = bit_waitqueue(&inode->i_state, __I_SYNC);
1066169ebd90SJan Kara 	int sleep;
1067169ebd90SJan Kara 
1068169ebd90SJan Kara 	prepare_to_wait(wqh, &wait, TASK_UNINTERRUPTIBLE);
1069169ebd90SJan Kara 	sleep = inode->i_state & I_SYNC;
1070169ebd90SJan Kara 	spin_unlock(&inode->i_lock);
1071169ebd90SJan Kara 	if (sleep)
1072169ebd90SJan Kara 		schedule();
1073169ebd90SJan Kara 	finish_wait(wqh, &wait);
1074169ebd90SJan Kara }
1075169ebd90SJan Kara 
1076169ebd90SJan Kara /*
1077ccb26b5aSJan Kara  * Find proper writeback list for the inode depending on its current state and
1078ccb26b5aSJan Kara  * possibly also change of its state while we were doing writeback.  Here we
1079ccb26b5aSJan Kara  * handle things such as livelock prevention or fairness of writeback among
1080ccb26b5aSJan Kara  * inodes. This function can be called only by flusher thread - noone else
1081ccb26b5aSJan Kara  * processes all inodes in writeback lists and requeueing inodes behind flusher
1082ccb26b5aSJan Kara  * thread's back can have unexpected consequences.
1083ccb26b5aSJan Kara  */
1084ccb26b5aSJan Kara static void requeue_inode(struct inode *inode, struct bdi_writeback *wb,
1085ccb26b5aSJan Kara 			  struct writeback_control *wbc)
1086ccb26b5aSJan Kara {
1087ccb26b5aSJan Kara 	if (inode->i_state & I_FREEING)
1088ccb26b5aSJan Kara 		return;
1089ccb26b5aSJan Kara 
1090ccb26b5aSJan Kara 	/*
1091ccb26b5aSJan Kara 	 * Sync livelock prevention. Each inode is tagged and synced in one
1092ccb26b5aSJan Kara 	 * shot. If still dirty, it will be redirty_tail()'ed below.  Update
1093ccb26b5aSJan Kara 	 * the dirty time to prevent enqueue and sync it again.
1094ccb26b5aSJan Kara 	 */
1095ccb26b5aSJan Kara 	if ((inode->i_state & I_DIRTY) &&
1096ccb26b5aSJan Kara 	    (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages))
1097ccb26b5aSJan Kara 		inode->dirtied_when = jiffies;
1098ccb26b5aSJan Kara 
10994f8ad655SJan Kara 	if (wbc->pages_skipped) {
11004f8ad655SJan Kara 		/*
11014f8ad655SJan Kara 		 * writeback is not making progress due to locked
11024f8ad655SJan Kara 		 * buffers. Skip this inode for now.
11034f8ad655SJan Kara 		 */
11044f8ad655SJan Kara 		redirty_tail(inode, wb);
11054f8ad655SJan Kara 		return;
11064f8ad655SJan Kara 	}
11074f8ad655SJan Kara 
1108ccb26b5aSJan Kara 	if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY)) {
1109ccb26b5aSJan Kara 		/*
1110ccb26b5aSJan Kara 		 * We didn't write back all the pages.  nfs_writepages()
1111ccb26b5aSJan Kara 		 * sometimes bales out without doing anything.
1112ccb26b5aSJan Kara 		 */
1113ccb26b5aSJan Kara 		if (wbc->nr_to_write <= 0) {
1114ccb26b5aSJan Kara 			/* Slice used up. Queue for next turn. */
1115ccb26b5aSJan Kara 			requeue_io(inode, wb);
1116ccb26b5aSJan Kara 		} else {
1117ccb26b5aSJan Kara 			/*
1118ccb26b5aSJan Kara 			 * Writeback blocked by something other than
1119ccb26b5aSJan Kara 			 * congestion. Delay the inode for some time to
1120ccb26b5aSJan Kara 			 * avoid spinning on the CPU (100% iowait)
1121ccb26b5aSJan Kara 			 * retrying writeback of the dirty page/inode
1122ccb26b5aSJan Kara 			 * that cannot be performed immediately.
1123ccb26b5aSJan Kara 			 */
1124ccb26b5aSJan Kara 			redirty_tail(inode, wb);
1125ccb26b5aSJan Kara 		}
1126ccb26b5aSJan Kara 	} else if (inode->i_state & I_DIRTY) {
1127ccb26b5aSJan Kara 		/*
1128ccb26b5aSJan Kara 		 * Filesystems can dirty the inode during writeback operations,
1129ccb26b5aSJan Kara 		 * such as delayed allocation during submission or metadata
1130ccb26b5aSJan Kara 		 * updates after data IO completion.
1131ccb26b5aSJan Kara 		 */
1132ccb26b5aSJan Kara 		redirty_tail(inode, wb);
11330ae45f63STheodore Ts'o 	} else if (inode->i_state & I_DIRTY_TIME) {
1134a2f48706STheodore Ts'o 		inode->dirtied_when = jiffies;
1135d6c10f1fSTejun Heo 		inode_wb_list_move_locked(inode, wb, &wb->b_dirty_time);
1136ccb26b5aSJan Kara 	} else {
1137ccb26b5aSJan Kara 		/* The inode is clean. Remove from writeback lists. */
1138d6c10f1fSTejun Heo 		inode_wb_list_del_locked(inode, wb);
1139ccb26b5aSJan Kara 	}
1140ccb26b5aSJan Kara }
1141ccb26b5aSJan Kara 
1142ccb26b5aSJan Kara /*
11434f8ad655SJan Kara  * Write out an inode and its dirty pages. Do not update the writeback list
11444f8ad655SJan Kara  * linkage. That is left to the caller. The caller is also responsible for
11454f8ad655SJan Kara  * setting I_SYNC flag and calling inode_sync_complete() to clear it.
11461da177e4SLinus Torvalds  */
11471da177e4SLinus Torvalds static int
1148cd8ed2a4SYan Hong __writeback_single_inode(struct inode *inode, struct writeback_control *wbc)
11491da177e4SLinus Torvalds {
11501da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
1151251d6a47SWu Fengguang 	long nr_to_write = wbc->nr_to_write;
115201c03194SChristoph Hellwig 	unsigned dirty;
11531da177e4SLinus Torvalds 	int ret;
11541da177e4SLinus Torvalds 
11554f8ad655SJan Kara 	WARN_ON(!(inode->i_state & I_SYNC));
11561da177e4SLinus Torvalds 
11579fb0a7daSTejun Heo 	trace_writeback_single_inode_start(inode, wbc, nr_to_write);
11589fb0a7daSTejun Heo 
11591da177e4SLinus Torvalds 	ret = do_writepages(mapping, wbc);
11601da177e4SLinus Torvalds 
116126821ed4SChristoph Hellwig 	/*
116226821ed4SChristoph Hellwig 	 * Make sure to wait on the data before writing out the metadata.
116326821ed4SChristoph Hellwig 	 * This is important for filesystems that modify metadata on data
11647747bd4bSDave Chinner 	 * I/O completion. We don't do it for sync(2) writeback because it has a
11657747bd4bSDave Chinner 	 * separate, external IO completion path and ->sync_fs for guaranteeing
11667747bd4bSDave Chinner 	 * inode metadata is written back correctly.
116726821ed4SChristoph Hellwig 	 */
11687747bd4bSDave Chinner 	if (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync) {
116926821ed4SChristoph Hellwig 		int err = filemap_fdatawait(mapping);
11701da177e4SLinus Torvalds 		if (ret == 0)
11711da177e4SLinus Torvalds 			ret = err;
11721da177e4SLinus Torvalds 	}
11731da177e4SLinus Torvalds 
11745547e8aaSDmitry Monakhov 	/*
11755547e8aaSDmitry Monakhov 	 * Some filesystems may redirty the inode during the writeback
11765547e8aaSDmitry Monakhov 	 * due to delalloc, clear dirty metadata flags right before
11775547e8aaSDmitry Monakhov 	 * write_inode()
11785547e8aaSDmitry Monakhov 	 */
1179250df6edSDave Chinner 	spin_lock(&inode->i_lock);
11809c6ac78eSTejun Heo 
11815547e8aaSDmitry Monakhov 	dirty = inode->i_state & I_DIRTY;
1182a2f48706STheodore Ts'o 	if (inode->i_state & I_DIRTY_TIME) {
1183a2f48706STheodore Ts'o 		if ((dirty & (I_DIRTY_SYNC | I_DIRTY_DATASYNC)) ||
1184a2f48706STheodore Ts'o 		    unlikely(inode->i_state & I_DIRTY_TIME_EXPIRED) ||
1185a2f48706STheodore Ts'o 		    unlikely(time_after(jiffies,
1186a2f48706STheodore Ts'o 					(inode->dirtied_time_when +
1187a2f48706STheodore Ts'o 					 dirtytime_expire_interval * HZ)))) {
11880ae45f63STheodore Ts'o 			dirty |= I_DIRTY_TIME | I_DIRTY_TIME_EXPIRED;
11890ae45f63STheodore Ts'o 			trace_writeback_lazytime(inode);
11900ae45f63STheodore Ts'o 		}
1191a2f48706STheodore Ts'o 	} else
1192a2f48706STheodore Ts'o 		inode->i_state &= ~I_DIRTY_TIME_EXPIRED;
11930ae45f63STheodore Ts'o 	inode->i_state &= ~dirty;
11949c6ac78eSTejun Heo 
11959c6ac78eSTejun Heo 	/*
11969c6ac78eSTejun Heo 	 * Paired with smp_mb() in __mark_inode_dirty().  This allows
11979c6ac78eSTejun Heo 	 * __mark_inode_dirty() to test i_state without grabbing i_lock -
11989c6ac78eSTejun Heo 	 * either they see the I_DIRTY bits cleared or we see the dirtied
11999c6ac78eSTejun Heo 	 * inode.
12009c6ac78eSTejun Heo 	 *
12019c6ac78eSTejun Heo 	 * I_DIRTY_PAGES is always cleared together above even if @mapping
12029c6ac78eSTejun Heo 	 * still has dirty pages.  The flag is reinstated after smp_mb() if
12039c6ac78eSTejun Heo 	 * necessary.  This guarantees that either __mark_inode_dirty()
12049c6ac78eSTejun Heo 	 * sees clear I_DIRTY_PAGES or we see PAGECACHE_TAG_DIRTY.
12059c6ac78eSTejun Heo 	 */
12069c6ac78eSTejun Heo 	smp_mb();
12079c6ac78eSTejun Heo 
12089c6ac78eSTejun Heo 	if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
12099c6ac78eSTejun Heo 		inode->i_state |= I_DIRTY_PAGES;
12109c6ac78eSTejun Heo 
1211250df6edSDave Chinner 	spin_unlock(&inode->i_lock);
12129c6ac78eSTejun Heo 
12130ae45f63STheodore Ts'o 	if (dirty & I_DIRTY_TIME)
12140ae45f63STheodore Ts'o 		mark_inode_dirty_sync(inode);
121526821ed4SChristoph Hellwig 	/* Don't write the inode if only I_DIRTY_PAGES was set */
12160ae45f63STheodore Ts'o 	if (dirty & ~I_DIRTY_PAGES) {
1217a9185b41SChristoph Hellwig 		int err = write_inode(inode, wbc);
12181da177e4SLinus Torvalds 		if (ret == 0)
12191da177e4SLinus Torvalds 			ret = err;
12201da177e4SLinus Torvalds 	}
12214f8ad655SJan Kara 	trace_writeback_single_inode(inode, wbc, nr_to_write);
12224f8ad655SJan Kara 	return ret;
12234f8ad655SJan Kara }
12244f8ad655SJan Kara 
12254f8ad655SJan Kara /*
12264f8ad655SJan Kara  * Write out an inode's dirty pages. Either the caller has an active reference
12274f8ad655SJan Kara  * on the inode or the inode has I_WILL_FREE set.
12284f8ad655SJan Kara  *
12294f8ad655SJan Kara  * This function is designed to be called for writing back one inode which
12304f8ad655SJan Kara  * we go e.g. from filesystem. Flusher thread uses __writeback_single_inode()
12314f8ad655SJan Kara  * and does more profound writeback list handling in writeback_sb_inodes().
12324f8ad655SJan Kara  */
12334f8ad655SJan Kara static int
12344f8ad655SJan Kara writeback_single_inode(struct inode *inode, struct bdi_writeback *wb,
12354f8ad655SJan Kara 		       struct writeback_control *wbc)
12364f8ad655SJan Kara {
12374f8ad655SJan Kara 	int ret = 0;
12384f8ad655SJan Kara 
12394f8ad655SJan Kara 	spin_lock(&inode->i_lock);
12404f8ad655SJan Kara 	if (!atomic_read(&inode->i_count))
12414f8ad655SJan Kara 		WARN_ON(!(inode->i_state & (I_WILL_FREE|I_FREEING)));
12424f8ad655SJan Kara 	else
12434f8ad655SJan Kara 		WARN_ON(inode->i_state & I_WILL_FREE);
12444f8ad655SJan Kara 
12454f8ad655SJan Kara 	if (inode->i_state & I_SYNC) {
12464f8ad655SJan Kara 		if (wbc->sync_mode != WB_SYNC_ALL)
12474f8ad655SJan Kara 			goto out;
12484f8ad655SJan Kara 		/*
1249169ebd90SJan Kara 		 * It's a data-integrity sync. We must wait. Since callers hold
1250169ebd90SJan Kara 		 * inode reference or inode has I_WILL_FREE set, it cannot go
1251169ebd90SJan Kara 		 * away under us.
12524f8ad655SJan Kara 		 */
1253169ebd90SJan Kara 		__inode_wait_for_writeback(inode);
12544f8ad655SJan Kara 	}
12554f8ad655SJan Kara 	WARN_ON(inode->i_state & I_SYNC);
12564f8ad655SJan Kara 	/*
1257f9b0e058SJan Kara 	 * Skip inode if it is clean and we have no outstanding writeback in
1258f9b0e058SJan Kara 	 * WB_SYNC_ALL mode. We don't want to mess with writeback lists in this
1259f9b0e058SJan Kara 	 * function since flusher thread may be doing for example sync in
1260f9b0e058SJan Kara 	 * parallel and if we move the inode, it could get skipped. So here we
1261f9b0e058SJan Kara 	 * make sure inode is on some writeback list and leave it there unless
1262f9b0e058SJan Kara 	 * we have completely cleaned the inode.
12634f8ad655SJan Kara 	 */
12640ae45f63STheodore Ts'o 	if (!(inode->i_state & I_DIRTY_ALL) &&
1265f9b0e058SJan Kara 	    (wbc->sync_mode != WB_SYNC_ALL ||
1266f9b0e058SJan Kara 	     !mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK)))
12674f8ad655SJan Kara 		goto out;
12684f8ad655SJan Kara 	inode->i_state |= I_SYNC;
1269b16b1debSTejun Heo 	wbc_attach_and_unlock_inode(wbc, inode);
12704f8ad655SJan Kara 
1271cd8ed2a4SYan Hong 	ret = __writeback_single_inode(inode, wbc);
12721da177e4SLinus Torvalds 
1273b16b1debSTejun Heo 	wbc_detach_inode(wbc);
1274f758eeabSChristoph Hellwig 	spin_lock(&wb->list_lock);
1275250df6edSDave Chinner 	spin_lock(&inode->i_lock);
12764f8ad655SJan Kara 	/*
12774f8ad655SJan Kara 	 * If inode is clean, remove it from writeback lists. Otherwise don't
12784f8ad655SJan Kara 	 * touch it. See comment above for explanation.
12794f8ad655SJan Kara 	 */
12800ae45f63STheodore Ts'o 	if (!(inode->i_state & I_DIRTY_ALL))
1281d6c10f1fSTejun Heo 		inode_wb_list_del_locked(inode, wb);
12824f8ad655SJan Kara 	spin_unlock(&wb->list_lock);
12831c0eeaf5SJoern Engel 	inode_sync_complete(inode);
12844f8ad655SJan Kara out:
12854f8ad655SJan Kara 	spin_unlock(&inode->i_lock);
12861da177e4SLinus Torvalds 	return ret;
12871da177e4SLinus Torvalds }
12881da177e4SLinus Torvalds 
1289a88a341aSTejun Heo static long writeback_chunk_size(struct bdi_writeback *wb,
12901a12d8bdSWu Fengguang 				 struct wb_writeback_work *work)
1291d46db3d5SWu Fengguang {
1292d46db3d5SWu Fengguang 	long pages;
1293d46db3d5SWu Fengguang 
1294d46db3d5SWu Fengguang 	/*
1295d46db3d5SWu Fengguang 	 * WB_SYNC_ALL mode does livelock avoidance by syncing dirty
1296d46db3d5SWu Fengguang 	 * inodes/pages in one big loop. Setting wbc.nr_to_write=LONG_MAX
1297d46db3d5SWu Fengguang 	 * here avoids calling into writeback_inodes_wb() more than once.
1298d46db3d5SWu Fengguang 	 *
1299d46db3d5SWu Fengguang 	 * The intended call sequence for WB_SYNC_ALL writeback is:
1300d46db3d5SWu Fengguang 	 *
1301d46db3d5SWu Fengguang 	 *      wb_writeback()
1302d46db3d5SWu Fengguang 	 *          writeback_sb_inodes()       <== called only once
1303d46db3d5SWu Fengguang 	 *              write_cache_pages()     <== called once for each inode
1304d46db3d5SWu Fengguang 	 *                   (quickly) tag currently dirty pages
1305d46db3d5SWu Fengguang 	 *                   (maybe slowly) sync all tagged pages
1306d46db3d5SWu Fengguang 	 */
1307d46db3d5SWu Fengguang 	if (work->sync_mode == WB_SYNC_ALL || work->tagged_writepages)
1308d46db3d5SWu Fengguang 		pages = LONG_MAX;
13091a12d8bdSWu Fengguang 	else {
1310a88a341aSTejun Heo 		pages = min(wb->avg_write_bandwidth / 2,
1311dcc25ae7STejun Heo 			    global_wb_domain.dirty_limit / DIRTY_SCOPE);
13121a12d8bdSWu Fengguang 		pages = min(pages, work->nr_pages);
13131a12d8bdSWu Fengguang 		pages = round_down(pages + MIN_WRITEBACK_PAGES,
13141a12d8bdSWu Fengguang 				   MIN_WRITEBACK_PAGES);
13151a12d8bdSWu Fengguang 	}
1316d46db3d5SWu Fengguang 
1317d46db3d5SWu Fengguang 	return pages;
1318d46db3d5SWu Fengguang }
1319d46db3d5SWu Fengguang 
132003ba3782SJens Axboe /*
1321f11c9c5cSEdward Shishkin  * Write a portion of b_io inodes which belong to @sb.
1322edadfb10SChristoph Hellwig  *
1323d46db3d5SWu Fengguang  * Return the number of pages and/or inodes written.
1324f11c9c5cSEdward Shishkin  */
1325d46db3d5SWu Fengguang static long writeback_sb_inodes(struct super_block *sb,
1326d46db3d5SWu Fengguang 				struct bdi_writeback *wb,
1327d46db3d5SWu Fengguang 				struct wb_writeback_work *work)
132803ba3782SJens Axboe {
1329d46db3d5SWu Fengguang 	struct writeback_control wbc = {
1330d46db3d5SWu Fengguang 		.sync_mode		= work->sync_mode,
1331d46db3d5SWu Fengguang 		.tagged_writepages	= work->tagged_writepages,
1332d46db3d5SWu Fengguang 		.for_kupdate		= work->for_kupdate,
1333d46db3d5SWu Fengguang 		.for_background		= work->for_background,
13347747bd4bSDave Chinner 		.for_sync		= work->for_sync,
1335d46db3d5SWu Fengguang 		.range_cyclic		= work->range_cyclic,
1336d46db3d5SWu Fengguang 		.range_start		= 0,
1337d46db3d5SWu Fengguang 		.range_end		= LLONG_MAX,
1338d46db3d5SWu Fengguang 	};
1339d46db3d5SWu Fengguang 	unsigned long start_time = jiffies;
1340d46db3d5SWu Fengguang 	long write_chunk;
1341d46db3d5SWu Fengguang 	long wrote = 0;  /* count both pages and inodes */
1342d46db3d5SWu Fengguang 
134303ba3782SJens Axboe 	while (!list_empty(&wb->b_io)) {
13447ccf19a8SNick Piggin 		struct inode *inode = wb_inode(wb->b_io.prev);
1345edadfb10SChristoph Hellwig 
1346edadfb10SChristoph Hellwig 		if (inode->i_sb != sb) {
1347d46db3d5SWu Fengguang 			if (work->sb) {
1348edadfb10SChristoph Hellwig 				/*
1349edadfb10SChristoph Hellwig 				 * We only want to write back data for this
1350edadfb10SChristoph Hellwig 				 * superblock, move all inodes not belonging
1351edadfb10SChristoph Hellwig 				 * to it back onto the dirty list.
1352edadfb10SChristoph Hellwig 				 */
1353f758eeabSChristoph Hellwig 				redirty_tail(inode, wb);
135466f3b8e2SJens Axboe 				continue;
135566f3b8e2SJens Axboe 			}
1356edadfb10SChristoph Hellwig 
1357edadfb10SChristoph Hellwig 			/*
1358edadfb10SChristoph Hellwig 			 * The inode belongs to a different superblock.
1359edadfb10SChristoph Hellwig 			 * Bounce back to the caller to unpin this and
1360edadfb10SChristoph Hellwig 			 * pin the next superblock.
1361edadfb10SChristoph Hellwig 			 */
1362d46db3d5SWu Fengguang 			break;
1363edadfb10SChristoph Hellwig 		}
1364edadfb10SChristoph Hellwig 
13659843b76aSChristoph Hellwig 		/*
1366331cbdeeSWanpeng Li 		 * Don't bother with new inodes or inodes being freed, first
1367331cbdeeSWanpeng Li 		 * kind does not need periodic writeout yet, and for the latter
13689843b76aSChristoph Hellwig 		 * kind writeout is handled by the freer.
13699843b76aSChristoph Hellwig 		 */
1370250df6edSDave Chinner 		spin_lock(&inode->i_lock);
13719843b76aSChristoph Hellwig 		if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) {
1372250df6edSDave Chinner 			spin_unlock(&inode->i_lock);
1373fcc5c222SWu Fengguang 			redirty_tail(inode, wb);
13747ef0d737SNick Piggin 			continue;
13757ef0d737SNick Piggin 		}
1376cc1676d9SJan Kara 		if ((inode->i_state & I_SYNC) && wbc.sync_mode != WB_SYNC_ALL) {
1377cc1676d9SJan Kara 			/*
1378cc1676d9SJan Kara 			 * If this inode is locked for writeback and we are not
1379cc1676d9SJan Kara 			 * doing writeback-for-data-integrity, move it to
1380cc1676d9SJan Kara 			 * b_more_io so that writeback can proceed with the
1381cc1676d9SJan Kara 			 * other inodes on s_io.
1382cc1676d9SJan Kara 			 *
1383cc1676d9SJan Kara 			 * We'll have another go at writing back this inode
1384cc1676d9SJan Kara 			 * when we completed a full scan of b_io.
1385cc1676d9SJan Kara 			 */
1386cc1676d9SJan Kara 			spin_unlock(&inode->i_lock);
1387cc1676d9SJan Kara 			requeue_io(inode, wb);
1388cc1676d9SJan Kara 			trace_writeback_sb_inodes_requeue(inode);
1389cc1676d9SJan Kara 			continue;
1390cc1676d9SJan Kara 		}
1391f0d07b7fSJan Kara 		spin_unlock(&wb->list_lock);
1392f0d07b7fSJan Kara 
13934f8ad655SJan Kara 		/*
13944f8ad655SJan Kara 		 * We already requeued the inode if it had I_SYNC set and we
13954f8ad655SJan Kara 		 * are doing WB_SYNC_NONE writeback. So this catches only the
13964f8ad655SJan Kara 		 * WB_SYNC_ALL case.
13974f8ad655SJan Kara 		 */
1398169ebd90SJan Kara 		if (inode->i_state & I_SYNC) {
1399169ebd90SJan Kara 			/* Wait for I_SYNC. This function drops i_lock... */
1400169ebd90SJan Kara 			inode_sleep_on_writeback(inode);
1401169ebd90SJan Kara 			/* Inode may be gone, start again */
1402ead188f9SJan Kara 			spin_lock(&wb->list_lock);
1403169ebd90SJan Kara 			continue;
1404169ebd90SJan Kara 		}
14054f8ad655SJan Kara 		inode->i_state |= I_SYNC;
1406b16b1debSTejun Heo 		wbc_attach_and_unlock_inode(&wbc, inode);
1407169ebd90SJan Kara 
1408a88a341aSTejun Heo 		write_chunk = writeback_chunk_size(wb, work);
1409d46db3d5SWu Fengguang 		wbc.nr_to_write = write_chunk;
1410d46db3d5SWu Fengguang 		wbc.pages_skipped = 0;
1411250df6edSDave Chinner 
1412169ebd90SJan Kara 		/*
1413169ebd90SJan Kara 		 * We use I_SYNC to pin the inode in memory. While it is set
1414169ebd90SJan Kara 		 * evict_inode() will wait so the inode cannot be freed.
1415169ebd90SJan Kara 		 */
1416cd8ed2a4SYan Hong 		__writeback_single_inode(inode, &wbc);
1417d46db3d5SWu Fengguang 
1418b16b1debSTejun Heo 		wbc_detach_inode(&wbc);
1419d46db3d5SWu Fengguang 		work->nr_pages -= write_chunk - wbc.nr_to_write;
1420d46db3d5SWu Fengguang 		wrote += write_chunk - wbc.nr_to_write;
14214f8ad655SJan Kara 		spin_lock(&wb->list_lock);
14224f8ad655SJan Kara 		spin_lock(&inode->i_lock);
14230ae45f63STheodore Ts'o 		if (!(inode->i_state & I_DIRTY_ALL))
1424d46db3d5SWu Fengguang 			wrote++;
14254f8ad655SJan Kara 		requeue_inode(inode, wb, &wbc);
14264f8ad655SJan Kara 		inode_sync_complete(inode);
14270f1b1fd8SDave Chinner 		spin_unlock(&inode->i_lock);
1428169ebd90SJan Kara 		cond_resched_lock(&wb->list_lock);
1429d46db3d5SWu Fengguang 		/*
1430d46db3d5SWu Fengguang 		 * bail out to wb_writeback() often enough to check
1431d46db3d5SWu Fengguang 		 * background threshold and other termination conditions.
1432d46db3d5SWu Fengguang 		 */
1433d46db3d5SWu Fengguang 		if (wrote) {
1434d46db3d5SWu Fengguang 			if (time_is_before_jiffies(start_time + HZ / 10UL))
1435d46db3d5SWu Fengguang 				break;
1436d46db3d5SWu Fengguang 			if (work->nr_pages <= 0)
1437d46db3d5SWu Fengguang 				break;
14381da177e4SLinus Torvalds 		}
14398bc3be27SFengguang Wu 	}
1440d46db3d5SWu Fengguang 	return wrote;
1441f11c9c5cSEdward Shishkin }
144238f21977SNick Piggin 
1443d46db3d5SWu Fengguang static long __writeback_inodes_wb(struct bdi_writeback *wb,
1444d46db3d5SWu Fengguang 				  struct wb_writeback_work *work)
1445f11c9c5cSEdward Shishkin {
1446d46db3d5SWu Fengguang 	unsigned long start_time = jiffies;
1447d46db3d5SWu Fengguang 	long wrote = 0;
1448f11c9c5cSEdward Shishkin 
1449f11c9c5cSEdward Shishkin 	while (!list_empty(&wb->b_io)) {
14507ccf19a8SNick Piggin 		struct inode *inode = wb_inode(wb->b_io.prev);
1451f11c9c5cSEdward Shishkin 		struct super_block *sb = inode->i_sb;
1452f11c9c5cSEdward Shishkin 
1453eb6ef3dfSKonstantin Khlebnikov 		if (!trylock_super(sb)) {
14540e995816SWu Fengguang 			/*
1455eb6ef3dfSKonstantin Khlebnikov 			 * trylock_super() may fail consistently due to
14560e995816SWu Fengguang 			 * s_umount being grabbed by someone else. Don't use
14570e995816SWu Fengguang 			 * requeue_io() to avoid busy retrying the inode/sb.
14580e995816SWu Fengguang 			 */
14590e995816SWu Fengguang 			redirty_tail(inode, wb);
1460d19de7edSChristoph Hellwig 			continue;
1461334132aeSChristoph Hellwig 		}
1462d46db3d5SWu Fengguang 		wrote += writeback_sb_inodes(sb, wb, work);
1463eb6ef3dfSKonstantin Khlebnikov 		up_read(&sb->s_umount);
1464f11c9c5cSEdward Shishkin 
1465d46db3d5SWu Fengguang 		/* refer to the same tests at the end of writeback_sb_inodes */
1466d46db3d5SWu Fengguang 		if (wrote) {
1467d46db3d5SWu Fengguang 			if (time_is_before_jiffies(start_time + HZ / 10UL))
1468d46db3d5SWu Fengguang 				break;
1469d46db3d5SWu Fengguang 			if (work->nr_pages <= 0)
1470f11c9c5cSEdward Shishkin 				break;
1471f11c9c5cSEdward Shishkin 		}
1472d46db3d5SWu Fengguang 	}
147366f3b8e2SJens Axboe 	/* Leave any unwritten inodes on b_io */
1474d46db3d5SWu Fengguang 	return wrote;
147566f3b8e2SJens Axboe }
147666f3b8e2SJens Axboe 
14777d9f073bSWanpeng Li static long writeback_inodes_wb(struct bdi_writeback *wb, long nr_pages,
14780e175a18SCurt Wohlgemuth 				enum wb_reason reason)
1479edadfb10SChristoph Hellwig {
1480d46db3d5SWu Fengguang 	struct wb_writeback_work work = {
1481d46db3d5SWu Fengguang 		.nr_pages	= nr_pages,
1482d46db3d5SWu Fengguang 		.sync_mode	= WB_SYNC_NONE,
1483d46db3d5SWu Fengguang 		.range_cyclic	= 1,
14840e175a18SCurt Wohlgemuth 		.reason		= reason,
1485d46db3d5SWu Fengguang 	};
1486edadfb10SChristoph Hellwig 
1487f758eeabSChristoph Hellwig 	spin_lock(&wb->list_lock);
1488424b351fSWu Fengguang 	if (list_empty(&wb->b_io))
1489ad4e38ddSCurt Wohlgemuth 		queue_io(wb, &work);
1490d46db3d5SWu Fengguang 	__writeback_inodes_wb(wb, &work);
1491f758eeabSChristoph Hellwig 	spin_unlock(&wb->list_lock);
1492edadfb10SChristoph Hellwig 
1493d46db3d5SWu Fengguang 	return nr_pages - work.nr_pages;
149466f3b8e2SJens Axboe }
149566f3b8e2SJens Axboe 
149603ba3782SJens Axboe /*
149703ba3782SJens Axboe  * Explicit flushing or periodic writeback of "old" data.
149803ba3782SJens Axboe  *
149903ba3782SJens Axboe  * Define "old": the first time one of an inode's pages is dirtied, we mark the
150003ba3782SJens Axboe  * dirtying-time in the inode's address_space.  So this periodic writeback code
150103ba3782SJens Axboe  * just walks the superblock inode list, writing back any inodes which are
150203ba3782SJens Axboe  * older than a specific point in time.
150303ba3782SJens Axboe  *
150403ba3782SJens Axboe  * Try to run once per dirty_writeback_interval.  But if a writeback event
150503ba3782SJens Axboe  * takes longer than a dirty_writeback_interval interval, then leave a
150603ba3782SJens Axboe  * one-second gap.
150703ba3782SJens Axboe  *
150803ba3782SJens Axboe  * older_than_this takes precedence over nr_to_write.  So we'll only write back
150903ba3782SJens Axboe  * all dirty pages if they are all attached to "old" mappings.
151003ba3782SJens Axboe  */
1511c4a77a6cSJens Axboe static long wb_writeback(struct bdi_writeback *wb,
151283ba7b07SChristoph Hellwig 			 struct wb_writeback_work *work)
151303ba3782SJens Axboe {
1514e98be2d5SWu Fengguang 	unsigned long wb_start = jiffies;
1515d46db3d5SWu Fengguang 	long nr_pages = work->nr_pages;
15160dc83bd3SJan Kara 	unsigned long oldest_jif;
1517a5989bdcSJan Kara 	struct inode *inode;
1518d46db3d5SWu Fengguang 	long progress;
151903ba3782SJens Axboe 
15200dc83bd3SJan Kara 	oldest_jif = jiffies;
15210dc83bd3SJan Kara 	work->older_than_this = &oldest_jif;
152203ba3782SJens Axboe 
1523e8dfc305SWu Fengguang 	spin_lock(&wb->list_lock);
152403ba3782SJens Axboe 	for (;;) {
152503ba3782SJens Axboe 		/*
1526d3ddec76SWu Fengguang 		 * Stop writeback when nr_pages has been consumed
152703ba3782SJens Axboe 		 */
152883ba7b07SChristoph Hellwig 		if (work->nr_pages <= 0)
152903ba3782SJens Axboe 			break;
153003ba3782SJens Axboe 
153103ba3782SJens Axboe 		/*
1532aa373cf5SJan Kara 		 * Background writeout and kupdate-style writeback may
1533aa373cf5SJan Kara 		 * run forever. Stop them if there is other work to do
1534aa373cf5SJan Kara 		 * so that e.g. sync can proceed. They'll be restarted
1535aa373cf5SJan Kara 		 * after the other works are all done.
1536aa373cf5SJan Kara 		 */
1537aa373cf5SJan Kara 		if ((work->for_background || work->for_kupdate) &&
1538f0054bb1STejun Heo 		    !list_empty(&wb->work_list))
1539aa373cf5SJan Kara 			break;
1540aa373cf5SJan Kara 
1541aa373cf5SJan Kara 		/*
1542d3ddec76SWu Fengguang 		 * For background writeout, stop when we are below the
1543d3ddec76SWu Fengguang 		 * background dirty threshold
154403ba3782SJens Axboe 		 */
1545aa661bbeSTejun Heo 		if (work->for_background && !wb_over_bg_thresh(wb))
154603ba3782SJens Axboe 			break;
154703ba3782SJens Axboe 
15481bc36b64SJan Kara 		/*
15491bc36b64SJan Kara 		 * Kupdate and background works are special and we want to
15501bc36b64SJan Kara 		 * include all inodes that need writing. Livelock avoidance is
15511bc36b64SJan Kara 		 * handled by these works yielding to any other work so we are
15521bc36b64SJan Kara 		 * safe.
15531bc36b64SJan Kara 		 */
1554ba9aa839SWu Fengguang 		if (work->for_kupdate) {
15550dc83bd3SJan Kara 			oldest_jif = jiffies -
1556ba9aa839SWu Fengguang 				msecs_to_jiffies(dirty_expire_interval * 10);
15571bc36b64SJan Kara 		} else if (work->for_background)
15580dc83bd3SJan Kara 			oldest_jif = jiffies;
1559028c2dd1SDave Chinner 
1560d46db3d5SWu Fengguang 		trace_writeback_start(wb->bdi, work);
1561e8dfc305SWu Fengguang 		if (list_empty(&wb->b_io))
1562ad4e38ddSCurt Wohlgemuth 			queue_io(wb, work);
156383ba7b07SChristoph Hellwig 		if (work->sb)
1564d46db3d5SWu Fengguang 			progress = writeback_sb_inodes(work->sb, wb, work);
1565edadfb10SChristoph Hellwig 		else
1566d46db3d5SWu Fengguang 			progress = __writeback_inodes_wb(wb, work);
1567d46db3d5SWu Fengguang 		trace_writeback_written(wb->bdi, work);
1568028c2dd1SDave Chinner 
1569e98be2d5SWu Fengguang 		wb_update_bandwidth(wb, wb_start);
157003ba3782SJens Axboe 
157103ba3782SJens Axboe 		/*
157271fd05a8SJens Axboe 		 * Did we write something? Try for more
1573e6fb6da2SWu Fengguang 		 *
1574e6fb6da2SWu Fengguang 		 * Dirty inodes are moved to b_io for writeback in batches.
1575e6fb6da2SWu Fengguang 		 * The completion of the current batch does not necessarily
1576e6fb6da2SWu Fengguang 		 * mean the overall work is done. So we keep looping as long
1577e6fb6da2SWu Fengguang 		 * as made some progress on cleaning pages or inodes.
157871fd05a8SJens Axboe 		 */
1579d46db3d5SWu Fengguang 		if (progress)
158003ba3782SJens Axboe 			continue;
1581a5989bdcSJan Kara 		/*
1582e6fb6da2SWu Fengguang 		 * No more inodes for IO, bail
1583a5989bdcSJan Kara 		 */
1584b7a2441fSWu Fengguang 		if (list_empty(&wb->b_more_io))
158503ba3782SJens Axboe 			break;
158603ba3782SJens Axboe 		/*
15878010c3b6SJens Axboe 		 * Nothing written. Wait for some inode to
15888010c3b6SJens Axboe 		 * become available for writeback. Otherwise
15898010c3b6SJens Axboe 		 * we'll just busyloop.
159003ba3782SJens Axboe 		 */
159103ba3782SJens Axboe 		if (!list_empty(&wb->b_more_io))  {
1592d46db3d5SWu Fengguang 			trace_writeback_wait(wb->bdi, work);
159303ba3782SJens Axboe 			inode = wb_inode(wb->b_more_io.prev);
1594250df6edSDave Chinner 			spin_lock(&inode->i_lock);
1595f0d07b7fSJan Kara 			spin_unlock(&wb->list_lock);
1596169ebd90SJan Kara 			/* This function drops i_lock... */
1597169ebd90SJan Kara 			inode_sleep_on_writeback(inode);
1598f0d07b7fSJan Kara 			spin_lock(&wb->list_lock);
159903ba3782SJens Axboe 		}
160003ba3782SJens Axboe 	}
1601e8dfc305SWu Fengguang 	spin_unlock(&wb->list_lock);
160203ba3782SJens Axboe 
1603d46db3d5SWu Fengguang 	return nr_pages - work->nr_pages;
160403ba3782SJens Axboe }
160503ba3782SJens Axboe 
160603ba3782SJens Axboe /*
160783ba7b07SChristoph Hellwig  * Return the next wb_writeback_work struct that hasn't been processed yet.
160803ba3782SJens Axboe  */
1609f0054bb1STejun Heo static struct wb_writeback_work *get_next_work_item(struct bdi_writeback *wb)
161003ba3782SJens Axboe {
161183ba7b07SChristoph Hellwig 	struct wb_writeback_work *work = NULL;
161203ba3782SJens Axboe 
1613f0054bb1STejun Heo 	spin_lock_bh(&wb->work_lock);
1614f0054bb1STejun Heo 	if (!list_empty(&wb->work_list)) {
1615f0054bb1STejun Heo 		work = list_entry(wb->work_list.next,
161683ba7b07SChristoph Hellwig 				  struct wb_writeback_work, list);
161783ba7b07SChristoph Hellwig 		list_del_init(&work->list);
161803ba3782SJens Axboe 	}
1619f0054bb1STejun Heo 	spin_unlock_bh(&wb->work_lock);
162083ba7b07SChristoph Hellwig 	return work;
162103ba3782SJens Axboe }
162203ba3782SJens Axboe 
1623cdf01dd5SLinus Torvalds /*
1624cdf01dd5SLinus Torvalds  * Add in the number of potentially dirty inodes, because each inode
1625cdf01dd5SLinus Torvalds  * write can dirty pagecache in the underlying blockdev.
1626cdf01dd5SLinus Torvalds  */
1627cdf01dd5SLinus Torvalds static unsigned long get_nr_dirty_pages(void)
1628cdf01dd5SLinus Torvalds {
1629cdf01dd5SLinus Torvalds 	return global_page_state(NR_FILE_DIRTY) +
1630cdf01dd5SLinus Torvalds 		global_page_state(NR_UNSTABLE_NFS) +
1631cdf01dd5SLinus Torvalds 		get_nr_dirty_inodes();
1632cdf01dd5SLinus Torvalds }
1633cdf01dd5SLinus Torvalds 
16346585027aSJan Kara static long wb_check_background_flush(struct bdi_writeback *wb)
16356585027aSJan Kara {
1636aa661bbeSTejun Heo 	if (wb_over_bg_thresh(wb)) {
16376585027aSJan Kara 
16386585027aSJan Kara 		struct wb_writeback_work work = {
16396585027aSJan Kara 			.nr_pages	= LONG_MAX,
16406585027aSJan Kara 			.sync_mode	= WB_SYNC_NONE,
16416585027aSJan Kara 			.for_background	= 1,
16426585027aSJan Kara 			.range_cyclic	= 1,
16430e175a18SCurt Wohlgemuth 			.reason		= WB_REASON_BACKGROUND,
16446585027aSJan Kara 		};
16456585027aSJan Kara 
16466585027aSJan Kara 		return wb_writeback(wb, &work);
16476585027aSJan Kara 	}
16486585027aSJan Kara 
16496585027aSJan Kara 	return 0;
16506585027aSJan Kara }
16516585027aSJan Kara 
165203ba3782SJens Axboe static long wb_check_old_data_flush(struct bdi_writeback *wb)
165303ba3782SJens Axboe {
165403ba3782SJens Axboe 	unsigned long expired;
165503ba3782SJens Axboe 	long nr_pages;
165603ba3782SJens Axboe 
165769b62d01SJens Axboe 	/*
165869b62d01SJens Axboe 	 * When set to zero, disable periodic writeback
165969b62d01SJens Axboe 	 */
166069b62d01SJens Axboe 	if (!dirty_writeback_interval)
166169b62d01SJens Axboe 		return 0;
166269b62d01SJens Axboe 
166303ba3782SJens Axboe 	expired = wb->last_old_flush +
166403ba3782SJens Axboe 			msecs_to_jiffies(dirty_writeback_interval * 10);
166503ba3782SJens Axboe 	if (time_before(jiffies, expired))
166603ba3782SJens Axboe 		return 0;
166703ba3782SJens Axboe 
166803ba3782SJens Axboe 	wb->last_old_flush = jiffies;
1669cdf01dd5SLinus Torvalds 	nr_pages = get_nr_dirty_pages();
167003ba3782SJens Axboe 
1671c4a77a6cSJens Axboe 	if (nr_pages) {
167283ba7b07SChristoph Hellwig 		struct wb_writeback_work work = {
1673c4a77a6cSJens Axboe 			.nr_pages	= nr_pages,
1674c4a77a6cSJens Axboe 			.sync_mode	= WB_SYNC_NONE,
1675c4a77a6cSJens Axboe 			.for_kupdate	= 1,
1676c4a77a6cSJens Axboe 			.range_cyclic	= 1,
16770e175a18SCurt Wohlgemuth 			.reason		= WB_REASON_PERIODIC,
1678c4a77a6cSJens Axboe 		};
1679c4a77a6cSJens Axboe 
168083ba7b07SChristoph Hellwig 		return wb_writeback(wb, &work);
1681c4a77a6cSJens Axboe 	}
168203ba3782SJens Axboe 
168303ba3782SJens Axboe 	return 0;
168403ba3782SJens Axboe }
168503ba3782SJens Axboe 
168603ba3782SJens Axboe /*
168703ba3782SJens Axboe  * Retrieve work items and do the writeback they describe
168803ba3782SJens Axboe  */
168925d130baSWanpeng Li static long wb_do_writeback(struct bdi_writeback *wb)
169003ba3782SJens Axboe {
169183ba7b07SChristoph Hellwig 	struct wb_writeback_work *work;
1692c4a77a6cSJens Axboe 	long wrote = 0;
169303ba3782SJens Axboe 
16944452226eSTejun Heo 	set_bit(WB_writeback_running, &wb->state);
1695f0054bb1STejun Heo 	while ((work = get_next_work_item(wb)) != NULL) {
1696cc395d7fSTejun Heo 		struct wb_completion *done = work->done;
169798754bf7STejun Heo 		bool need_wake_up = false;
169883ba7b07SChristoph Hellwig 
1699f0054bb1STejun Heo 		trace_writeback_exec(wb->bdi, work);
1700455b2864SDave Chinner 
170183ba7b07SChristoph Hellwig 		wrote += wb_writeback(wb, work);
170203ba3782SJens Axboe 
170398754bf7STejun Heo 		if (work->single_wait) {
170498754bf7STejun Heo 			WARN_ON_ONCE(work->auto_free);
170598754bf7STejun Heo 			/* paired w/ rmb in wb_wait_for_single_work() */
170698754bf7STejun Heo 			smp_wmb();
170798754bf7STejun Heo 			work->single_done = 1;
170898754bf7STejun Heo 			need_wake_up = true;
170998754bf7STejun Heo 		} else if (work->auto_free) {
171083ba7b07SChristoph Hellwig 			kfree(work);
171198754bf7STejun Heo 		}
171298754bf7STejun Heo 
1713cc395d7fSTejun Heo 		if (done && atomic_dec_and_test(&done->cnt))
171498754bf7STejun Heo 			need_wake_up = true;
171598754bf7STejun Heo 
171698754bf7STejun Heo 		if (need_wake_up)
1717cc395d7fSTejun Heo 			wake_up_all(&wb->bdi->wb_waitq);
171803ba3782SJens Axboe 	}
171903ba3782SJens Axboe 
172003ba3782SJens Axboe 	/*
172103ba3782SJens Axboe 	 * Check for periodic writeback, kupdated() style
172203ba3782SJens Axboe 	 */
172303ba3782SJens Axboe 	wrote += wb_check_old_data_flush(wb);
17246585027aSJan Kara 	wrote += wb_check_background_flush(wb);
17254452226eSTejun Heo 	clear_bit(WB_writeback_running, &wb->state);
172603ba3782SJens Axboe 
172703ba3782SJens Axboe 	return wrote;
172803ba3782SJens Axboe }
172903ba3782SJens Axboe 
173003ba3782SJens Axboe /*
173103ba3782SJens Axboe  * Handle writeback of dirty data for the device backed by this bdi. Also
1732839a8e86STejun Heo  * reschedules periodically and does kupdated style flushing.
173303ba3782SJens Axboe  */
1734f0054bb1STejun Heo void wb_workfn(struct work_struct *work)
173503ba3782SJens Axboe {
1736839a8e86STejun Heo 	struct bdi_writeback *wb = container_of(to_delayed_work(work),
1737839a8e86STejun Heo 						struct bdi_writeback, dwork);
173803ba3782SJens Axboe 	long pages_written;
173903ba3782SJens Axboe 
1740f0054bb1STejun Heo 	set_worker_desc("flush-%s", dev_name(wb->bdi->dev));
1741766f9164SPeter Zijlstra 	current->flags |= PF_SWAPWRITE;
174203ba3782SJens Axboe 
1743839a8e86STejun Heo 	if (likely(!current_is_workqueue_rescuer() ||
17444452226eSTejun Heo 		   !test_bit(WB_registered, &wb->state))) {
174503ba3782SJens Axboe 		/*
1746f0054bb1STejun Heo 		 * The normal path.  Keep writing back @wb until its
1747839a8e86STejun Heo 		 * work_list is empty.  Note that this path is also taken
1748f0054bb1STejun Heo 		 * if @wb is shutting down even when we're running off the
1749839a8e86STejun Heo 		 * rescuer as work_list needs to be drained.
175003ba3782SJens Axboe 		 */
1751839a8e86STejun Heo 		do {
175225d130baSWanpeng Li 			pages_written = wb_do_writeback(wb);
1753455b2864SDave Chinner 			trace_writeback_pages_written(pages_written);
1754f0054bb1STejun Heo 		} while (!list_empty(&wb->work_list));
1755839a8e86STejun Heo 	} else {
1756253c34e9SArtem Bityutskiy 		/*
1757839a8e86STejun Heo 		 * bdi_wq can't get enough workers and we're running off
1758839a8e86STejun Heo 		 * the emergency worker.  Don't hog it.  Hopefully, 1024 is
1759839a8e86STejun Heo 		 * enough for efficient IO.
1760253c34e9SArtem Bityutskiy 		 */
1761f0054bb1STejun Heo 		pages_written = writeback_inodes_wb(wb, 1024,
1762839a8e86STejun Heo 						    WB_REASON_FORKER_THREAD);
1763839a8e86STejun Heo 		trace_writeback_pages_written(pages_written);
176403ba3782SJens Axboe 	}
176503ba3782SJens Axboe 
1766f0054bb1STejun Heo 	if (!list_empty(&wb->work_list))
17676ca738d6SDerek Basehore 		mod_delayed_work(bdi_wq, &wb->dwork, 0);
17686ca738d6SDerek Basehore 	else if (wb_has_dirty_io(wb) && dirty_writeback_interval)
1769f0054bb1STejun Heo 		wb_wakeup_delayed(wb);
1770455b2864SDave Chinner 
1771839a8e86STejun Heo 	current->flags &= ~PF_SWAPWRITE;
177203ba3782SJens Axboe }
177303ba3782SJens Axboe 
177403ba3782SJens Axboe /*
177503ba3782SJens Axboe  * Start writeback of `nr_pages' pages.  If `nr_pages' is zero, write back
177603ba3782SJens Axboe  * the whole world.
177703ba3782SJens Axboe  */
17780e175a18SCurt Wohlgemuth void wakeup_flusher_threads(long nr_pages, enum wb_reason reason)
177903ba3782SJens Axboe {
1780b8c2f347SChristoph Hellwig 	struct backing_dev_info *bdi;
1781b8c2f347SChristoph Hellwig 
178247df3ddeSJan Kara 	if (!nr_pages)
178347df3ddeSJan Kara 		nr_pages = get_nr_dirty_pages();
1784b8c2f347SChristoph Hellwig 
1785b8c2f347SChristoph Hellwig 	rcu_read_lock();
1786f2b65121STejun Heo 	list_for_each_entry_rcu(bdi, &bdi_list, bdi_list) {
1787f2b65121STejun Heo 		struct bdi_writeback *wb;
1788f2b65121STejun Heo 		struct wb_iter iter;
1789f2b65121STejun Heo 
1790f2b65121STejun Heo 		if (!bdi_has_dirty_io(bdi))
1791f2b65121STejun Heo 			continue;
1792f2b65121STejun Heo 
1793f2b65121STejun Heo 		bdi_for_each_wb(wb, bdi, &iter, 0)
1794f2b65121STejun Heo 			wb_start_writeback(wb, wb_split_bdi_pages(wb, nr_pages),
1795f2b65121STejun Heo 					   false, reason);
1796f2b65121STejun Heo 	}
1797b8c2f347SChristoph Hellwig 	rcu_read_unlock();
179803ba3782SJens Axboe }
179903ba3782SJens Axboe 
1800a2f48706STheodore Ts'o /*
1801a2f48706STheodore Ts'o  * Wake up bdi's periodically to make sure dirtytime inodes gets
1802a2f48706STheodore Ts'o  * written back periodically.  We deliberately do *not* check the
1803a2f48706STheodore Ts'o  * b_dirtytime list in wb_has_dirty_io(), since this would cause the
1804a2f48706STheodore Ts'o  * kernel to be constantly waking up once there are any dirtytime
1805a2f48706STheodore Ts'o  * inodes on the system.  So instead we define a separate delayed work
1806a2f48706STheodore Ts'o  * function which gets called much more rarely.  (By default, only
1807a2f48706STheodore Ts'o  * once every 12 hours.)
1808a2f48706STheodore Ts'o  *
1809a2f48706STheodore Ts'o  * If there is any other write activity going on in the file system,
1810a2f48706STheodore Ts'o  * this function won't be necessary.  But if the only thing that has
1811a2f48706STheodore Ts'o  * happened on the file system is a dirtytime inode caused by an atime
1812a2f48706STheodore Ts'o  * update, we need this infrastructure below to make sure that inode
1813a2f48706STheodore Ts'o  * eventually gets pushed out to disk.
1814a2f48706STheodore Ts'o  */
1815a2f48706STheodore Ts'o static void wakeup_dirtytime_writeback(struct work_struct *w);
1816a2f48706STheodore Ts'o static DECLARE_DELAYED_WORK(dirtytime_work, wakeup_dirtytime_writeback);
1817a2f48706STheodore Ts'o 
1818a2f48706STheodore Ts'o static void wakeup_dirtytime_writeback(struct work_struct *w)
1819a2f48706STheodore Ts'o {
1820a2f48706STheodore Ts'o 	struct backing_dev_info *bdi;
1821a2f48706STheodore Ts'o 
1822a2f48706STheodore Ts'o 	rcu_read_lock();
1823a2f48706STheodore Ts'o 	list_for_each_entry_rcu(bdi, &bdi_list, bdi_list) {
1824001fe6f6STejun Heo 		struct bdi_writeback *wb;
1825001fe6f6STejun Heo 		struct wb_iter iter;
1826001fe6f6STejun Heo 
1827001fe6f6STejun Heo 		bdi_for_each_wb(wb, bdi, &iter, 0)
1828001fe6f6STejun Heo 			if (!list_empty(&bdi->wb.b_dirty_time))
1829f0054bb1STejun Heo 				wb_wakeup(&bdi->wb);
1830a2f48706STheodore Ts'o 	}
1831a2f48706STheodore Ts'o 	rcu_read_unlock();
1832a2f48706STheodore Ts'o 	schedule_delayed_work(&dirtytime_work, dirtytime_expire_interval * HZ);
1833a2f48706STheodore Ts'o }
1834a2f48706STheodore Ts'o 
1835a2f48706STheodore Ts'o static int __init start_dirtytime_writeback(void)
1836a2f48706STheodore Ts'o {
1837a2f48706STheodore Ts'o 	schedule_delayed_work(&dirtytime_work, dirtytime_expire_interval * HZ);
1838a2f48706STheodore Ts'o 	return 0;
1839a2f48706STheodore Ts'o }
1840a2f48706STheodore Ts'o __initcall(start_dirtytime_writeback);
1841a2f48706STheodore Ts'o 
18421efff914STheodore Ts'o int dirtytime_interval_handler(struct ctl_table *table, int write,
18431efff914STheodore Ts'o 			       void __user *buffer, size_t *lenp, loff_t *ppos)
18441efff914STheodore Ts'o {
18451efff914STheodore Ts'o 	int ret;
18461efff914STheodore Ts'o 
18471efff914STheodore Ts'o 	ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
18481efff914STheodore Ts'o 	if (ret == 0 && write)
18491efff914STheodore Ts'o 		mod_delayed_work(system_wq, &dirtytime_work, 0);
18501efff914STheodore Ts'o 	return ret;
18511efff914STheodore Ts'o }
18521efff914STheodore Ts'o 
185303ba3782SJens Axboe static noinline void block_dump___mark_inode_dirty(struct inode *inode)
185403ba3782SJens Axboe {
185503ba3782SJens Axboe 	if (inode->i_ino || strcmp(inode->i_sb->s_id, "bdev")) {
185603ba3782SJens Axboe 		struct dentry *dentry;
185703ba3782SJens Axboe 		const char *name = "?";
185803ba3782SJens Axboe 
185903ba3782SJens Axboe 		dentry = d_find_alias(inode);
186003ba3782SJens Axboe 		if (dentry) {
186103ba3782SJens Axboe 			spin_lock(&dentry->d_lock);
186203ba3782SJens Axboe 			name = (const char *) dentry->d_name.name;
186303ba3782SJens Axboe 		}
186403ba3782SJens Axboe 		printk(KERN_DEBUG
186503ba3782SJens Axboe 		       "%s(%d): dirtied inode %lu (%s) on %s\n",
186603ba3782SJens Axboe 		       current->comm, task_pid_nr(current), inode->i_ino,
186703ba3782SJens Axboe 		       name, inode->i_sb->s_id);
186803ba3782SJens Axboe 		if (dentry) {
186903ba3782SJens Axboe 			spin_unlock(&dentry->d_lock);
187003ba3782SJens Axboe 			dput(dentry);
187103ba3782SJens Axboe 		}
187203ba3782SJens Axboe 	}
187303ba3782SJens Axboe }
187403ba3782SJens Axboe 
187503ba3782SJens Axboe /**
187603ba3782SJens Axboe  *	__mark_inode_dirty -	internal function
187703ba3782SJens Axboe  *	@inode: inode to mark
187803ba3782SJens Axboe  *	@flags: what kind of dirty (i.e. I_DIRTY_SYNC)
187903ba3782SJens Axboe  *	Mark an inode as dirty. Callers should use mark_inode_dirty or
188003ba3782SJens Axboe  *  	mark_inode_dirty_sync.
188103ba3782SJens Axboe  *
188203ba3782SJens Axboe  * Put the inode on the super block's dirty list.
188303ba3782SJens Axboe  *
188403ba3782SJens Axboe  * CAREFUL! We mark it dirty unconditionally, but move it onto the
188503ba3782SJens Axboe  * dirty list only if it is hashed or if it refers to a blockdev.
188603ba3782SJens Axboe  * If it was not hashed, it will never be added to the dirty list
188703ba3782SJens Axboe  * even if it is later hashed, as it will have been marked dirty already.
188803ba3782SJens Axboe  *
188903ba3782SJens Axboe  * In short, make sure you hash any inodes _before_ you start marking
189003ba3782SJens Axboe  * them dirty.
189103ba3782SJens Axboe  *
189203ba3782SJens Axboe  * Note that for blockdevs, inode->dirtied_when represents the dirtying time of
189303ba3782SJens Axboe  * the block-special inode (/dev/hda1) itself.  And the ->dirtied_when field of
189403ba3782SJens Axboe  * the kernel-internal blockdev inode represents the dirtying time of the
189503ba3782SJens Axboe  * blockdev's pages.  This is why for I_DIRTY_PAGES we always use
189603ba3782SJens Axboe  * page->mapping->host, so the page-dirtying time is recorded in the internal
189703ba3782SJens Axboe  * blockdev inode.
189803ba3782SJens Axboe  */
18990ae45f63STheodore Ts'o #define I_DIRTY_INODE (I_DIRTY_SYNC | I_DIRTY_DATASYNC)
190003ba3782SJens Axboe void __mark_inode_dirty(struct inode *inode, int flags)
190103ba3782SJens Axboe {
190203ba3782SJens Axboe 	struct super_block *sb = inode->i_sb;
19030ae45f63STheodore Ts'o 	int dirtytime;
19040ae45f63STheodore Ts'o 
19050ae45f63STheodore Ts'o 	trace_writeback_mark_inode_dirty(inode, flags);
190603ba3782SJens Axboe 
190703ba3782SJens Axboe 	/*
190803ba3782SJens Axboe 	 * Don't do this for I_DIRTY_PAGES - that doesn't actually
190903ba3782SJens Axboe 	 * dirty the inode itself
191003ba3782SJens Axboe 	 */
19110ae45f63STheodore Ts'o 	if (flags & (I_DIRTY_SYNC | I_DIRTY_DATASYNC | I_DIRTY_TIME)) {
19129fb0a7daSTejun Heo 		trace_writeback_dirty_inode_start(inode, flags);
19139fb0a7daSTejun Heo 
191403ba3782SJens Axboe 		if (sb->s_op->dirty_inode)
1915aa385729SChristoph Hellwig 			sb->s_op->dirty_inode(inode, flags);
19169fb0a7daSTejun Heo 
19179fb0a7daSTejun Heo 		trace_writeback_dirty_inode(inode, flags);
191803ba3782SJens Axboe 	}
19190ae45f63STheodore Ts'o 	if (flags & I_DIRTY_INODE)
19200ae45f63STheodore Ts'o 		flags &= ~I_DIRTY_TIME;
19210ae45f63STheodore Ts'o 	dirtytime = flags & I_DIRTY_TIME;
192203ba3782SJens Axboe 
192303ba3782SJens Axboe 	/*
19249c6ac78eSTejun Heo 	 * Paired with smp_mb() in __writeback_single_inode() for the
19259c6ac78eSTejun Heo 	 * following lockless i_state test.  See there for details.
192603ba3782SJens Axboe 	 */
192703ba3782SJens Axboe 	smp_mb();
192803ba3782SJens Axboe 
19290ae45f63STheodore Ts'o 	if (((inode->i_state & flags) == flags) ||
19300ae45f63STheodore Ts'o 	    (dirtytime && (inode->i_state & I_DIRTY_INODE)))
193103ba3782SJens Axboe 		return;
193203ba3782SJens Axboe 
193303ba3782SJens Axboe 	if (unlikely(block_dump))
193403ba3782SJens Axboe 		block_dump___mark_inode_dirty(inode);
193503ba3782SJens Axboe 
1936250df6edSDave Chinner 	spin_lock(&inode->i_lock);
19370ae45f63STheodore Ts'o 	if (dirtytime && (inode->i_state & I_DIRTY_INODE))
19380ae45f63STheodore Ts'o 		goto out_unlock_inode;
193903ba3782SJens Axboe 	if ((inode->i_state & flags) != flags) {
194003ba3782SJens Axboe 		const int was_dirty = inode->i_state & I_DIRTY;
194103ba3782SJens Axboe 
194252ebea74STejun Heo 		inode_attach_wb(inode, NULL);
194352ebea74STejun Heo 
19440ae45f63STheodore Ts'o 		if (flags & I_DIRTY_INODE)
19450ae45f63STheodore Ts'o 			inode->i_state &= ~I_DIRTY_TIME;
194603ba3782SJens Axboe 		inode->i_state |= flags;
194703ba3782SJens Axboe 
194803ba3782SJens Axboe 		/*
194903ba3782SJens Axboe 		 * If the inode is being synced, just update its dirty state.
195003ba3782SJens Axboe 		 * The unlocker will place the inode on the appropriate
195103ba3782SJens Axboe 		 * superblock list, based upon its state.
195203ba3782SJens Axboe 		 */
195303ba3782SJens Axboe 		if (inode->i_state & I_SYNC)
1954250df6edSDave Chinner 			goto out_unlock_inode;
195503ba3782SJens Axboe 
195603ba3782SJens Axboe 		/*
195703ba3782SJens Axboe 		 * Only add valid (hashed) inodes to the superblock's
195803ba3782SJens Axboe 		 * dirty list.  Add blockdev inodes as well.
195903ba3782SJens Axboe 		 */
196003ba3782SJens Axboe 		if (!S_ISBLK(inode->i_mode)) {
19611d3382cbSAl Viro 			if (inode_unhashed(inode))
1962250df6edSDave Chinner 				goto out_unlock_inode;
196303ba3782SJens Axboe 		}
1964a4ffdde6SAl Viro 		if (inode->i_state & I_FREEING)
1965250df6edSDave Chinner 			goto out_unlock_inode;
196603ba3782SJens Axboe 
196703ba3782SJens Axboe 		/*
196803ba3782SJens Axboe 		 * If the inode was already on b_dirty/b_io/b_more_io, don't
196903ba3782SJens Axboe 		 * reposition it (that would break b_dirty time-ordering).
197003ba3782SJens Axboe 		 */
197103ba3782SJens Axboe 		if (!was_dirty) {
197287e1d789STejun Heo 			struct bdi_writeback *wb;
1973d6c10f1fSTejun Heo 			struct list_head *dirty_list;
1974a66979abSDave Chinner 			bool wakeup_bdi = false;
1975500b067cSJens Axboe 
197687e1d789STejun Heo 			wb = locked_inode_to_wb_and_lock_list(inode);
1977253c34e9SArtem Bityutskiy 
19780747259dSTejun Heo 			WARN(bdi_cap_writeback_dirty(wb->bdi) &&
19790747259dSTejun Heo 			     !test_bit(WB_registered, &wb->state),
19800747259dSTejun Heo 			     "bdi-%s not registered\n", wb->bdi->name);
198103ba3782SJens Axboe 
198203ba3782SJens Axboe 			inode->dirtied_when = jiffies;
1983a2f48706STheodore Ts'o 			if (dirtytime)
1984a2f48706STheodore Ts'o 				inode->dirtied_time_when = jiffies;
1985d6c10f1fSTejun Heo 
1986a2f48706STheodore Ts'o 			if (inode->i_state & (I_DIRTY_INODE | I_DIRTY_PAGES))
19870747259dSTejun Heo 				dirty_list = &wb->b_dirty;
1988a2f48706STheodore Ts'o 			else
19890747259dSTejun Heo 				dirty_list = &wb->b_dirty_time;
1990d6c10f1fSTejun Heo 
19910747259dSTejun Heo 			wakeup_bdi = inode_wb_list_move_locked(inode, wb,
1992d6c10f1fSTejun Heo 							       dirty_list);
1993d6c10f1fSTejun Heo 
19940747259dSTejun Heo 			spin_unlock(&wb->list_lock);
19950ae45f63STheodore Ts'o 			trace_writeback_dirty_inode_enqueue(inode);
1996253c34e9SArtem Bityutskiy 
1997d6c10f1fSTejun Heo 			/*
1998d6c10f1fSTejun Heo 			 * If this is the first dirty inode for this bdi,
1999d6c10f1fSTejun Heo 			 * we have to wake-up the corresponding bdi thread
2000d6c10f1fSTejun Heo 			 * to make sure background write-back happens
2001d6c10f1fSTejun Heo 			 * later.
2002d6c10f1fSTejun Heo 			 */
20030747259dSTejun Heo 			if (bdi_cap_writeback_dirty(wb->bdi) && wakeup_bdi)
20040747259dSTejun Heo 				wb_wakeup_delayed(wb);
2005a66979abSDave Chinner 			return;
2006a66979abSDave Chinner 		}
2007a66979abSDave Chinner 	}
2008a66979abSDave Chinner out_unlock_inode:
2009a66979abSDave Chinner 	spin_unlock(&inode->i_lock);
2010a66979abSDave Chinner 
201103ba3782SJens Axboe }
201203ba3782SJens Axboe EXPORT_SYMBOL(__mark_inode_dirty);
201303ba3782SJens Axboe 
2014b6e51316SJens Axboe static void wait_sb_inodes(struct super_block *sb)
201566f3b8e2SJens Axboe {
201638f21977SNick Piggin 	struct inode *inode, *old_inode = NULL;
201738f21977SNick Piggin 
201803ba3782SJens Axboe 	/*
201903ba3782SJens Axboe 	 * We need to be protected against the filesystem going from
202003ba3782SJens Axboe 	 * r/o to r/w or vice versa.
202103ba3782SJens Axboe 	 */
2022b6e51316SJens Axboe 	WARN_ON(!rwsem_is_locked(&sb->s_umount));
202303ba3782SJens Axboe 
202455fa6091SDave Chinner 	spin_lock(&inode_sb_list_lock);
202566f3b8e2SJens Axboe 
202638f21977SNick Piggin 	/*
202738f21977SNick Piggin 	 * Data integrity sync. Must wait for all pages under writeback,
202838f21977SNick Piggin 	 * because there may have been pages dirtied before our sync
202938f21977SNick Piggin 	 * call, but which had writeout started before we write it out.
203038f21977SNick Piggin 	 * In which case, the inode may not be on the dirty list, but
203138f21977SNick Piggin 	 * we still have to wait for that writeout.
203238f21977SNick Piggin 	 */
2033b6e51316SJens Axboe 	list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
2034250df6edSDave Chinner 		struct address_space *mapping = inode->i_mapping;
203538f21977SNick Piggin 
2036250df6edSDave Chinner 		spin_lock(&inode->i_lock);
2037250df6edSDave Chinner 		if ((inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW)) ||
2038250df6edSDave Chinner 		    (mapping->nrpages == 0)) {
2039250df6edSDave Chinner 			spin_unlock(&inode->i_lock);
204038f21977SNick Piggin 			continue;
2041250df6edSDave Chinner 		}
204238f21977SNick Piggin 		__iget(inode);
2043250df6edSDave Chinner 		spin_unlock(&inode->i_lock);
204455fa6091SDave Chinner 		spin_unlock(&inode_sb_list_lock);
204555fa6091SDave Chinner 
204638f21977SNick Piggin 		/*
204755fa6091SDave Chinner 		 * We hold a reference to 'inode' so it couldn't have been
204855fa6091SDave Chinner 		 * removed from s_inodes list while we dropped the
204955fa6091SDave Chinner 		 * inode_sb_list_lock.  We cannot iput the inode now as we can
205055fa6091SDave Chinner 		 * be holding the last reference and we cannot iput it under
205155fa6091SDave Chinner 		 * inode_sb_list_lock. So we keep the reference and iput it
205255fa6091SDave Chinner 		 * later.
205338f21977SNick Piggin 		 */
205438f21977SNick Piggin 		iput(old_inode);
205538f21977SNick Piggin 		old_inode = inode;
205638f21977SNick Piggin 
205738f21977SNick Piggin 		filemap_fdatawait(mapping);
205838f21977SNick Piggin 
205938f21977SNick Piggin 		cond_resched();
206038f21977SNick Piggin 
206155fa6091SDave Chinner 		spin_lock(&inode_sb_list_lock);
206238f21977SNick Piggin 	}
206355fa6091SDave Chinner 	spin_unlock(&inode_sb_list_lock);
206438f21977SNick Piggin 	iput(old_inode);
206566f3b8e2SJens Axboe }
20661da177e4SLinus Torvalds 
2067f30a7d0cSTejun Heo static void __writeback_inodes_sb_nr(struct super_block *sb, unsigned long nr,
2068f30a7d0cSTejun Heo 				     enum wb_reason reason, bool skip_if_busy)
20691da177e4SLinus Torvalds {
2070cc395d7fSTejun Heo 	DEFINE_WB_COMPLETION_ONSTACK(done);
207183ba7b07SChristoph Hellwig 	struct wb_writeback_work work = {
20723c4d7165SChristoph Hellwig 		.sb			= sb,
20733c4d7165SChristoph Hellwig 		.sync_mode		= WB_SYNC_NONE,
20746e6938b6SWu Fengguang 		.tagged_writepages	= 1,
207583ba7b07SChristoph Hellwig 		.done			= &done,
20763259f8beSChris Mason 		.nr_pages		= nr,
20770e175a18SCurt Wohlgemuth 		.reason			= reason,
20783c4d7165SChristoph Hellwig 	};
2079e7972912STejun Heo 	struct backing_dev_info *bdi = sb->s_bdi;
20800e3c9a22SJens Axboe 
2081e7972912STejun Heo 	if (!bdi_has_dirty_io(bdi) || bdi == &noop_backing_dev_info)
20826eedc701SJan Kara 		return;
2083cf37e972SChristoph Hellwig 	WARN_ON(!rwsem_is_locked(&sb->s_umount));
2084f30a7d0cSTejun Heo 
2085db125360STejun Heo 	bdi_split_work_to_wbs(sb->s_bdi, &work, skip_if_busy);
2086cc395d7fSTejun Heo 	wb_wait_for_completion(bdi, &done);
20871da177e4SLinus Torvalds }
2088f30a7d0cSTejun Heo 
2089f30a7d0cSTejun Heo /**
2090f30a7d0cSTejun Heo  * writeback_inodes_sb_nr -	writeback dirty inodes from given super_block
2091f30a7d0cSTejun Heo  * @sb: the superblock
2092f30a7d0cSTejun Heo  * @nr: the number of pages to write
2093f30a7d0cSTejun Heo  * @reason: reason why some writeback work initiated
2094f30a7d0cSTejun Heo  *
2095f30a7d0cSTejun Heo  * Start writeback on some inodes on this super_block. No guarantees are made
2096f30a7d0cSTejun Heo  * on how many (if any) will be written, and this function does not wait
2097f30a7d0cSTejun Heo  * for IO completion of submitted IO.
2098f30a7d0cSTejun Heo  */
2099f30a7d0cSTejun Heo void writeback_inodes_sb_nr(struct super_block *sb,
2100f30a7d0cSTejun Heo 			    unsigned long nr,
2101f30a7d0cSTejun Heo 			    enum wb_reason reason)
2102f30a7d0cSTejun Heo {
2103f30a7d0cSTejun Heo 	__writeback_inodes_sb_nr(sb, nr, reason, false);
2104f30a7d0cSTejun Heo }
21053259f8beSChris Mason EXPORT_SYMBOL(writeback_inodes_sb_nr);
21063259f8beSChris Mason 
21073259f8beSChris Mason /**
21083259f8beSChris Mason  * writeback_inodes_sb	-	writeback dirty inodes from given super_block
21093259f8beSChris Mason  * @sb: the superblock
2110786228abSMarcos Paulo de Souza  * @reason: reason why some writeback work was initiated
21113259f8beSChris Mason  *
21123259f8beSChris Mason  * Start writeback on some inodes on this super_block. No guarantees are made
21133259f8beSChris Mason  * on how many (if any) will be written, and this function does not wait
21143259f8beSChris Mason  * for IO completion of submitted IO.
21153259f8beSChris Mason  */
21160e175a18SCurt Wohlgemuth void writeback_inodes_sb(struct super_block *sb, enum wb_reason reason)
21173259f8beSChris Mason {
21180e175a18SCurt Wohlgemuth 	return writeback_inodes_sb_nr(sb, get_nr_dirty_pages(), reason);
21193259f8beSChris Mason }
2120d8a8559cSJens Axboe EXPORT_SYMBOL(writeback_inodes_sb);
2121d8a8559cSJens Axboe 
2122d8a8559cSJens Axboe /**
212310ee27a0SMiao Xie  * try_to_writeback_inodes_sb_nr - try to start writeback if none underway
21243259f8beSChris Mason  * @sb: the superblock
21253259f8beSChris Mason  * @nr: the number of pages to write
212610ee27a0SMiao Xie  * @reason: the reason of writeback
21273259f8beSChris Mason  *
212810ee27a0SMiao Xie  * Invoke writeback_inodes_sb_nr if no writeback is currently underway.
21293259f8beSChris Mason  * Returns 1 if writeback was started, 0 if not.
21303259f8beSChris Mason  */
2131f30a7d0cSTejun Heo bool try_to_writeback_inodes_sb_nr(struct super_block *sb, unsigned long nr,
21320e175a18SCurt Wohlgemuth 				   enum wb_reason reason)
21333259f8beSChris Mason {
213410ee27a0SMiao Xie 	if (!down_read_trylock(&sb->s_umount))
2135f30a7d0cSTejun Heo 		return false;
213610ee27a0SMiao Xie 
2137f30a7d0cSTejun Heo 	__writeback_inodes_sb_nr(sb, nr, reason, true);
21383259f8beSChris Mason 	up_read(&sb->s_umount);
2139f30a7d0cSTejun Heo 	return true;
21403259f8beSChris Mason }
214110ee27a0SMiao Xie EXPORT_SYMBOL(try_to_writeback_inodes_sb_nr);
214210ee27a0SMiao Xie 
214310ee27a0SMiao Xie /**
214410ee27a0SMiao Xie  * try_to_writeback_inodes_sb - try to start writeback if none underway
214510ee27a0SMiao Xie  * @sb: the superblock
214610ee27a0SMiao Xie  * @reason: reason why some writeback work was initiated
214710ee27a0SMiao Xie  *
214810ee27a0SMiao Xie  * Implement by try_to_writeback_inodes_sb_nr()
214910ee27a0SMiao Xie  * Returns 1 if writeback was started, 0 if not.
215010ee27a0SMiao Xie  */
2151f30a7d0cSTejun Heo bool try_to_writeback_inodes_sb(struct super_block *sb, enum wb_reason reason)
215210ee27a0SMiao Xie {
215310ee27a0SMiao Xie 	return try_to_writeback_inodes_sb_nr(sb, get_nr_dirty_pages(), reason);
215410ee27a0SMiao Xie }
215510ee27a0SMiao Xie EXPORT_SYMBOL(try_to_writeback_inodes_sb);
21563259f8beSChris Mason 
21573259f8beSChris Mason /**
2158d8a8559cSJens Axboe  * sync_inodes_sb	-	sync sb inode pages
2159d8a8559cSJens Axboe  * @sb: the superblock
2160d8a8559cSJens Axboe  *
2161d8a8559cSJens Axboe  * This function writes and waits on any dirty inode belonging to this
21620dc83bd3SJan Kara  * super_block.
2163d8a8559cSJens Axboe  */
21640dc83bd3SJan Kara void sync_inodes_sb(struct super_block *sb)
2165d8a8559cSJens Axboe {
2166cc395d7fSTejun Heo 	DEFINE_WB_COMPLETION_ONSTACK(done);
216783ba7b07SChristoph Hellwig 	struct wb_writeback_work work = {
21683c4d7165SChristoph Hellwig 		.sb		= sb,
21693c4d7165SChristoph Hellwig 		.sync_mode	= WB_SYNC_ALL,
21703c4d7165SChristoph Hellwig 		.nr_pages	= LONG_MAX,
21713c4d7165SChristoph Hellwig 		.range_cyclic	= 0,
217283ba7b07SChristoph Hellwig 		.done		= &done,
21730e175a18SCurt Wohlgemuth 		.reason		= WB_REASON_SYNC,
21747747bd4bSDave Chinner 		.for_sync	= 1,
21753c4d7165SChristoph Hellwig 	};
2176e7972912STejun Heo 	struct backing_dev_info *bdi = sb->s_bdi;
21773c4d7165SChristoph Hellwig 
21786eedc701SJan Kara 	/* Nothing to do? */
2179e7972912STejun Heo 	if (!bdi_has_dirty_io(bdi) || bdi == &noop_backing_dev_info)
21806eedc701SJan Kara 		return;
2181cf37e972SChristoph Hellwig 	WARN_ON(!rwsem_is_locked(&sb->s_umount));
2182cf37e972SChristoph Hellwig 
2183db125360STejun Heo 	bdi_split_work_to_wbs(bdi, &work, false);
2184cc395d7fSTejun Heo 	wb_wait_for_completion(bdi, &done);
218583ba7b07SChristoph Hellwig 
2186b6e51316SJens Axboe 	wait_sb_inodes(sb);
2187d8a8559cSJens Axboe }
2188d8a8559cSJens Axboe EXPORT_SYMBOL(sync_inodes_sb);
21891da177e4SLinus Torvalds 
21901da177e4SLinus Torvalds /**
21911da177e4SLinus Torvalds  * write_inode_now	-	write an inode to disk
21921da177e4SLinus Torvalds  * @inode: inode to write to disk
21931da177e4SLinus Torvalds  * @sync: whether the write should be synchronous or not
21941da177e4SLinus Torvalds  *
21957f04c26dSAndrea Arcangeli  * This function commits an inode to disk immediately if it is dirty. This is
21967f04c26dSAndrea Arcangeli  * primarily needed by knfsd.
21977f04c26dSAndrea Arcangeli  *
21987f04c26dSAndrea Arcangeli  * The caller must either have a ref on the inode or must have set I_WILL_FREE.
21991da177e4SLinus Torvalds  */
22001da177e4SLinus Torvalds int write_inode_now(struct inode *inode, int sync)
22011da177e4SLinus Torvalds {
2202f758eeabSChristoph Hellwig 	struct bdi_writeback *wb = &inode_to_bdi(inode)->wb;
22031da177e4SLinus Torvalds 	struct writeback_control wbc = {
22041da177e4SLinus Torvalds 		.nr_to_write = LONG_MAX,
220518914b18SMike Galbraith 		.sync_mode = sync ? WB_SYNC_ALL : WB_SYNC_NONE,
2206111ebb6eSOGAWA Hirofumi 		.range_start = 0,
2207111ebb6eSOGAWA Hirofumi 		.range_end = LLONG_MAX,
22081da177e4SLinus Torvalds 	};
22091da177e4SLinus Torvalds 
22101da177e4SLinus Torvalds 	if (!mapping_cap_writeback_dirty(inode->i_mapping))
221149364ce2SAndrew Morton 		wbc.nr_to_write = 0;
22121da177e4SLinus Torvalds 
22131da177e4SLinus Torvalds 	might_sleep();
22144f8ad655SJan Kara 	return writeback_single_inode(inode, wb, &wbc);
22151da177e4SLinus Torvalds }
22161da177e4SLinus Torvalds EXPORT_SYMBOL(write_inode_now);
22171da177e4SLinus Torvalds 
22181da177e4SLinus Torvalds /**
22191da177e4SLinus Torvalds  * sync_inode - write an inode and its pages to disk.
22201da177e4SLinus Torvalds  * @inode: the inode to sync
22211da177e4SLinus Torvalds  * @wbc: controls the writeback mode
22221da177e4SLinus Torvalds  *
22231da177e4SLinus Torvalds  * sync_inode() will write an inode and its pages to disk.  It will also
22241da177e4SLinus Torvalds  * correctly update the inode on its superblock's dirty inode lists and will
22251da177e4SLinus Torvalds  * update inode->i_state.
22261da177e4SLinus Torvalds  *
22271da177e4SLinus Torvalds  * The caller must have a ref on the inode.
22281da177e4SLinus Torvalds  */
22291da177e4SLinus Torvalds int sync_inode(struct inode *inode, struct writeback_control *wbc)
22301da177e4SLinus Torvalds {
22314f8ad655SJan Kara 	return writeback_single_inode(inode, &inode_to_bdi(inode)->wb, wbc);
22321da177e4SLinus Torvalds }
22331da177e4SLinus Torvalds EXPORT_SYMBOL(sync_inode);
2234c3765016SChristoph Hellwig 
2235c3765016SChristoph Hellwig /**
2236c691b9d9SAndrew Morton  * sync_inode_metadata - write an inode to disk
2237c3765016SChristoph Hellwig  * @inode: the inode to sync
2238c3765016SChristoph Hellwig  * @wait: wait for I/O to complete.
2239c3765016SChristoph Hellwig  *
2240c691b9d9SAndrew Morton  * Write an inode to disk and adjust its dirty state after completion.
2241c3765016SChristoph Hellwig  *
2242c3765016SChristoph Hellwig  * Note: only writes the actual inode, no associated data or other metadata.
2243c3765016SChristoph Hellwig  */
2244c3765016SChristoph Hellwig int sync_inode_metadata(struct inode *inode, int wait)
2245c3765016SChristoph Hellwig {
2246c3765016SChristoph Hellwig 	struct writeback_control wbc = {
2247c3765016SChristoph Hellwig 		.sync_mode = wait ? WB_SYNC_ALL : WB_SYNC_NONE,
2248c3765016SChristoph Hellwig 		.nr_to_write = 0, /* metadata-only */
2249c3765016SChristoph Hellwig 	};
2250c3765016SChristoph Hellwig 
2251c3765016SChristoph Hellwig 	return sync_inode(inode, &wbc);
2252c3765016SChristoph Hellwig }
2253c3765016SChristoph Hellwig EXPORT_SYMBOL(sync_inode_metadata);
2254