xref: /openbmc/linux/fs/fs-writeback.c (revision 27b36d8f)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * fs/fs-writeback.c
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * Copyright (C) 2002, Linus Torvalds.
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  * Contains all the functions related to writing back and waiting
81da177e4SLinus Torvalds  * upon dirty inodes against superblocks, and writing back dirty
91da177e4SLinus Torvalds  * pages against inodes.  ie: data writeback.  Writeout of the
101da177e4SLinus Torvalds  * inode itself is not handled here.
111da177e4SLinus Torvalds  *
12e1f8e874SFrancois Cami  * 10Apr2002	Andrew Morton
131da177e4SLinus Torvalds  *		Split out of fs/inode.c
141da177e4SLinus Torvalds  *		Additions for address_space-based writeback
151da177e4SLinus Torvalds  */
161da177e4SLinus Torvalds 
171da177e4SLinus Torvalds #include <linux/kernel.h>
18630d9c47SPaul Gortmaker #include <linux/export.h>
191da177e4SLinus Torvalds #include <linux/spinlock.h>
205a0e3ad6STejun Heo #include <linux/slab.h>
211da177e4SLinus Torvalds #include <linux/sched.h>
221da177e4SLinus Torvalds #include <linux/fs.h>
231da177e4SLinus Torvalds #include <linux/mm.h>
24bc31b86aSWu Fengguang #include <linux/pagemap.h>
2503ba3782SJens Axboe #include <linux/kthread.h>
261da177e4SLinus Torvalds #include <linux/writeback.h>
271da177e4SLinus Torvalds #include <linux/blkdev.h>
281da177e4SLinus Torvalds #include <linux/backing-dev.h>
29455b2864SDave Chinner #include <linux/tracepoint.h>
30719ea2fbSAl Viro #include <linux/device.h>
3121c6321fSTejun Heo #include <linux/memcontrol.h>
3207f3f05cSDavid Howells #include "internal.h"
331da177e4SLinus Torvalds 
34d0bceac7SJens Axboe /*
35bc31b86aSWu Fengguang  * 4MB minimal write chunk size
36bc31b86aSWu Fengguang  */
3709cbfeafSKirill A. Shutemov #define MIN_WRITEBACK_PAGES	(4096UL >> (PAGE_SHIFT - 10))
38bc31b86aSWu Fengguang 
39cc395d7fSTejun Heo struct wb_completion {
40cc395d7fSTejun Heo 	atomic_t		cnt;
41cc395d7fSTejun Heo };
42cc395d7fSTejun Heo 
43bc31b86aSWu Fengguang /*
44c4a77a6cSJens Axboe  * Passed into wb_writeback(), essentially a subset of writeback_control
45c4a77a6cSJens Axboe  */
4683ba7b07SChristoph Hellwig struct wb_writeback_work {
47c4a77a6cSJens Axboe 	long nr_pages;
48c4a77a6cSJens Axboe 	struct super_block *sb;
490dc83bd3SJan Kara 	unsigned long *older_than_this;
50c4a77a6cSJens Axboe 	enum writeback_sync_modes sync_mode;
516e6938b6SWu Fengguang 	unsigned int tagged_writepages:1;
5252957fe1SH Hartley Sweeten 	unsigned int for_kupdate:1;
5352957fe1SH Hartley Sweeten 	unsigned int range_cyclic:1;
5452957fe1SH Hartley Sweeten 	unsigned int for_background:1;
557747bd4bSDave Chinner 	unsigned int for_sync:1;	/* sync(2) WB_SYNC_ALL writeback */
56ac7b19a3STejun Heo 	unsigned int auto_free:1;	/* free on completion */
570e175a18SCurt Wohlgemuth 	enum wb_reason reason;		/* why was writeback initiated? */
58c4a77a6cSJens Axboe 
598010c3b6SJens Axboe 	struct list_head list;		/* pending work list */
60cc395d7fSTejun Heo 	struct wb_completion *done;	/* set if the caller waits */
6103ba3782SJens Axboe };
6203ba3782SJens Axboe 
63a2f48706STheodore Ts'o /*
64cc395d7fSTejun Heo  * If one wants to wait for one or more wb_writeback_works, each work's
65cc395d7fSTejun Heo  * ->done should be set to a wb_completion defined using the following
66cc395d7fSTejun Heo  * macro.  Once all work items are issued with wb_queue_work(), the caller
67cc395d7fSTejun Heo  * can wait for the completion of all using wb_wait_for_completion().  Work
68cc395d7fSTejun Heo  * items which are waited upon aren't freed automatically on completion.
69cc395d7fSTejun Heo  */
70cc395d7fSTejun Heo #define DEFINE_WB_COMPLETION_ONSTACK(cmpl)				\
71cc395d7fSTejun Heo 	struct wb_completion cmpl = {					\
72cc395d7fSTejun Heo 		.cnt		= ATOMIC_INIT(1),			\
73cc395d7fSTejun Heo 	}
74cc395d7fSTejun Heo 
75cc395d7fSTejun Heo 
76cc395d7fSTejun Heo /*
77a2f48706STheodore Ts'o  * If an inode is constantly having its pages dirtied, but then the
78a2f48706STheodore Ts'o  * updates stop dirtytime_expire_interval seconds in the past, it's
79a2f48706STheodore Ts'o  * possible for the worst case time between when an inode has its
80a2f48706STheodore Ts'o  * timestamps updated and when they finally get written out to be two
81a2f48706STheodore Ts'o  * dirtytime_expire_intervals.  We set the default to 12 hours (in
82a2f48706STheodore Ts'o  * seconds), which means most of the time inodes will have their
83a2f48706STheodore Ts'o  * timestamps written to disk after 12 hours, but in the worst case a
84a2f48706STheodore Ts'o  * few inodes might not their timestamps updated for 24 hours.
85a2f48706STheodore Ts'o  */
86a2f48706STheodore Ts'o unsigned int dirtytime_expire_interval = 12 * 60 * 60;
87a2f48706STheodore Ts'o 
887ccf19a8SNick Piggin static inline struct inode *wb_inode(struct list_head *head)
897ccf19a8SNick Piggin {
90c7f54084SDave Chinner 	return list_entry(head, struct inode, i_io_list);
917ccf19a8SNick Piggin }
927ccf19a8SNick Piggin 
9315eb77a0SWu Fengguang /*
9415eb77a0SWu Fengguang  * Include the creation of the trace points after defining the
9515eb77a0SWu Fengguang  * wb_writeback_work structure and inline functions so that the definition
9615eb77a0SWu Fengguang  * remains local to this file.
9715eb77a0SWu Fengguang  */
9815eb77a0SWu Fengguang #define CREATE_TRACE_POINTS
9915eb77a0SWu Fengguang #include <trace/events/writeback.h>
10015eb77a0SWu Fengguang 
101774016b2SSteven Whitehouse EXPORT_TRACEPOINT_SYMBOL_GPL(wbc_writepage);
102774016b2SSteven Whitehouse 
103d6c10f1fSTejun Heo static bool wb_io_lists_populated(struct bdi_writeback *wb)
104d6c10f1fSTejun Heo {
105d6c10f1fSTejun Heo 	if (wb_has_dirty_io(wb)) {
106d6c10f1fSTejun Heo 		return false;
107d6c10f1fSTejun Heo 	} else {
108d6c10f1fSTejun Heo 		set_bit(WB_has_dirty_io, &wb->state);
10995a46c65STejun Heo 		WARN_ON_ONCE(!wb->avg_write_bandwidth);
110766a9d6eSTejun Heo 		atomic_long_add(wb->avg_write_bandwidth,
111766a9d6eSTejun Heo 				&wb->bdi->tot_write_bandwidth);
112d6c10f1fSTejun Heo 		return true;
113d6c10f1fSTejun Heo 	}
114d6c10f1fSTejun Heo }
115d6c10f1fSTejun Heo 
116d6c10f1fSTejun Heo static void wb_io_lists_depopulated(struct bdi_writeback *wb)
117d6c10f1fSTejun Heo {
118d6c10f1fSTejun Heo 	if (wb_has_dirty_io(wb) && list_empty(&wb->b_dirty) &&
119766a9d6eSTejun Heo 	    list_empty(&wb->b_io) && list_empty(&wb->b_more_io)) {
120d6c10f1fSTejun Heo 		clear_bit(WB_has_dirty_io, &wb->state);
12195a46c65STejun Heo 		WARN_ON_ONCE(atomic_long_sub_return(wb->avg_write_bandwidth,
12295a46c65STejun Heo 					&wb->bdi->tot_write_bandwidth) < 0);
123766a9d6eSTejun Heo 	}
124d6c10f1fSTejun Heo }
125d6c10f1fSTejun Heo 
126d6c10f1fSTejun Heo /**
127c7f54084SDave Chinner  * inode_io_list_move_locked - move an inode onto a bdi_writeback IO list
128d6c10f1fSTejun Heo  * @inode: inode to be moved
129d6c10f1fSTejun Heo  * @wb: target bdi_writeback
130bbbc3c1cSWang Long  * @head: one of @wb->b_{dirty|io|more_io|dirty_time}
131d6c10f1fSTejun Heo  *
132c7f54084SDave Chinner  * Move @inode->i_io_list to @list of @wb and set %WB_has_dirty_io.
133d6c10f1fSTejun Heo  * Returns %true if @inode is the first occupant of the !dirty_time IO
134d6c10f1fSTejun Heo  * lists; otherwise, %false.
135d6c10f1fSTejun Heo  */
136c7f54084SDave Chinner static bool inode_io_list_move_locked(struct inode *inode,
137d6c10f1fSTejun Heo 				      struct bdi_writeback *wb,
138d6c10f1fSTejun Heo 				      struct list_head *head)
139d6c10f1fSTejun Heo {
140d6c10f1fSTejun Heo 	assert_spin_locked(&wb->list_lock);
141d6c10f1fSTejun Heo 
142c7f54084SDave Chinner 	list_move(&inode->i_io_list, head);
143d6c10f1fSTejun Heo 
144d6c10f1fSTejun Heo 	/* dirty_time doesn't count as dirty_io until expiration */
145d6c10f1fSTejun Heo 	if (head != &wb->b_dirty_time)
146d6c10f1fSTejun Heo 		return wb_io_lists_populated(wb);
147d6c10f1fSTejun Heo 
148d6c10f1fSTejun Heo 	wb_io_lists_depopulated(wb);
149d6c10f1fSTejun Heo 	return false;
150d6c10f1fSTejun Heo }
151d6c10f1fSTejun Heo 
152d6c10f1fSTejun Heo /**
153c7f54084SDave Chinner  * inode_io_list_del_locked - remove an inode from its bdi_writeback IO list
154d6c10f1fSTejun Heo  * @inode: inode to be removed
155d6c10f1fSTejun Heo  * @wb: bdi_writeback @inode is being removed from
156d6c10f1fSTejun Heo  *
157d6c10f1fSTejun Heo  * Remove @inode which may be on one of @wb->b_{dirty|io|more_io} lists and
158d6c10f1fSTejun Heo  * clear %WB_has_dirty_io if all are empty afterwards.
159d6c10f1fSTejun Heo  */
160c7f54084SDave Chinner static void inode_io_list_del_locked(struct inode *inode,
161d6c10f1fSTejun Heo 				     struct bdi_writeback *wb)
162d6c10f1fSTejun Heo {
163d6c10f1fSTejun Heo 	assert_spin_locked(&wb->list_lock);
164d6c10f1fSTejun Heo 
165c7f54084SDave Chinner 	list_del_init(&inode->i_io_list);
166d6c10f1fSTejun Heo 	wb_io_lists_depopulated(wb);
167d6c10f1fSTejun Heo }
168d6c10f1fSTejun Heo 
169f0054bb1STejun Heo static void wb_wakeup(struct bdi_writeback *wb)
1705acda9d1SJan Kara {
171f0054bb1STejun Heo 	spin_lock_bh(&wb->work_lock);
172f0054bb1STejun Heo 	if (test_bit(WB_registered, &wb->state))
173f0054bb1STejun Heo 		mod_delayed_work(bdi_wq, &wb->dwork, 0);
174f0054bb1STejun Heo 	spin_unlock_bh(&wb->work_lock);
1755acda9d1SJan Kara }
1765acda9d1SJan Kara 
1774a3a485bSTahsin Erdogan static void finish_writeback_work(struct bdi_writeback *wb,
1784a3a485bSTahsin Erdogan 				  struct wb_writeback_work *work)
1794a3a485bSTahsin Erdogan {
1804a3a485bSTahsin Erdogan 	struct wb_completion *done = work->done;
1814a3a485bSTahsin Erdogan 
1824a3a485bSTahsin Erdogan 	if (work->auto_free)
1834a3a485bSTahsin Erdogan 		kfree(work);
1844a3a485bSTahsin Erdogan 	if (done && atomic_dec_and_test(&done->cnt))
1854a3a485bSTahsin Erdogan 		wake_up_all(&wb->bdi->wb_waitq);
1864a3a485bSTahsin Erdogan }
1874a3a485bSTahsin Erdogan 
188f0054bb1STejun Heo static void wb_queue_work(struct bdi_writeback *wb,
1896585027aSJan Kara 			  struct wb_writeback_work *work)
1906585027aSJan Kara {
1915634cc2aSTejun Heo 	trace_writeback_queue(wb, work);
1926585027aSJan Kara 
193cc395d7fSTejun Heo 	if (work->done)
194cc395d7fSTejun Heo 		atomic_inc(&work->done->cnt);
1954a3a485bSTahsin Erdogan 
1964a3a485bSTahsin Erdogan 	spin_lock_bh(&wb->work_lock);
1974a3a485bSTahsin Erdogan 
1984a3a485bSTahsin Erdogan 	if (test_bit(WB_registered, &wb->state)) {
199f0054bb1STejun Heo 		list_add_tail(&work->list, &wb->work_list);
200f0054bb1STejun Heo 		mod_delayed_work(bdi_wq, &wb->dwork, 0);
2014a3a485bSTahsin Erdogan 	} else
2024a3a485bSTahsin Erdogan 		finish_writeback_work(wb, work);
2034a3a485bSTahsin Erdogan 
204f0054bb1STejun Heo 	spin_unlock_bh(&wb->work_lock);
20503ba3782SJens Axboe }
2061da177e4SLinus Torvalds 
207cc395d7fSTejun Heo /**
208cc395d7fSTejun Heo  * wb_wait_for_completion - wait for completion of bdi_writeback_works
209cc395d7fSTejun Heo  * @bdi: bdi work items were issued to
210cc395d7fSTejun Heo  * @done: target wb_completion
211cc395d7fSTejun Heo  *
212cc395d7fSTejun Heo  * Wait for one or more work items issued to @bdi with their ->done field
213cc395d7fSTejun Heo  * set to @done, which should have been defined with
214cc395d7fSTejun Heo  * DEFINE_WB_COMPLETION_ONSTACK().  This function returns after all such
215cc395d7fSTejun Heo  * work items are completed.  Work items which are waited upon aren't freed
216cc395d7fSTejun Heo  * automatically on completion.
217cc395d7fSTejun Heo  */
218cc395d7fSTejun Heo static void wb_wait_for_completion(struct backing_dev_info *bdi,
219cc395d7fSTejun Heo 				   struct wb_completion *done)
220cc395d7fSTejun Heo {
221cc395d7fSTejun Heo 	atomic_dec(&done->cnt);		/* put down the initial count */
222cc395d7fSTejun Heo 	wait_event(bdi->wb_waitq, !atomic_read(&done->cnt));
223cc395d7fSTejun Heo }
224cc395d7fSTejun Heo 
225703c2708STejun Heo #ifdef CONFIG_CGROUP_WRITEBACK
226703c2708STejun Heo 
2272a814908STejun Heo /* parameters for foreign inode detection, see wb_detach_inode() */
2282a814908STejun Heo #define WB_FRN_TIME_SHIFT	13	/* 1s = 2^13, upto 8 secs w/ 16bit */
2292a814908STejun Heo #define WB_FRN_TIME_AVG_SHIFT	3	/* avg = avg * 7/8 + new * 1/8 */
2302a814908STejun Heo #define WB_FRN_TIME_CUT_DIV	2	/* ignore rounds < avg / 2 */
2312a814908STejun Heo #define WB_FRN_TIME_PERIOD	(2 * (1 << WB_FRN_TIME_SHIFT))	/* 2s */
2322a814908STejun Heo 
2332a814908STejun Heo #define WB_FRN_HIST_SLOTS	16	/* inode->i_wb_frn_history is 16bit */
2342a814908STejun Heo #define WB_FRN_HIST_UNIT	(WB_FRN_TIME_PERIOD / WB_FRN_HIST_SLOTS)
2352a814908STejun Heo 					/* each slot's duration is 2s / 16 */
2362a814908STejun Heo #define WB_FRN_HIST_THR_SLOTS	(WB_FRN_HIST_SLOTS / 2)
2372a814908STejun Heo 					/* if foreign slots >= 8, switch */
2382a814908STejun Heo #define WB_FRN_HIST_MAX_SLOTS	(WB_FRN_HIST_THR_SLOTS / 2 + 1)
2392a814908STejun Heo 					/* one round can affect upto 5 slots */
2402a814908STejun Heo 
241a1a0e23eSTejun Heo static atomic_t isw_nr_in_flight = ATOMIC_INIT(0);
242a1a0e23eSTejun Heo static struct workqueue_struct *isw_wq;
243a1a0e23eSTejun Heo 
24421c6321fSTejun Heo void __inode_attach_wb(struct inode *inode, struct page *page)
24521c6321fSTejun Heo {
24621c6321fSTejun Heo 	struct backing_dev_info *bdi = inode_to_bdi(inode);
24721c6321fSTejun Heo 	struct bdi_writeback *wb = NULL;
24821c6321fSTejun Heo 
24921c6321fSTejun Heo 	if (inode_cgwb_enabled(inode)) {
25021c6321fSTejun Heo 		struct cgroup_subsys_state *memcg_css;
25121c6321fSTejun Heo 
25221c6321fSTejun Heo 		if (page) {
25321c6321fSTejun Heo 			memcg_css = mem_cgroup_css_from_page(page);
25421c6321fSTejun Heo 			wb = wb_get_create(bdi, memcg_css, GFP_ATOMIC);
25521c6321fSTejun Heo 		} else {
25621c6321fSTejun Heo 			/* must pin memcg_css, see wb_get_create() */
25721c6321fSTejun Heo 			memcg_css = task_get_css(current, memory_cgrp_id);
25821c6321fSTejun Heo 			wb = wb_get_create(bdi, memcg_css, GFP_ATOMIC);
25921c6321fSTejun Heo 			css_put(memcg_css);
26021c6321fSTejun Heo 		}
26121c6321fSTejun Heo 	}
26221c6321fSTejun Heo 
26321c6321fSTejun Heo 	if (!wb)
26421c6321fSTejun Heo 		wb = &bdi->wb;
26521c6321fSTejun Heo 
26621c6321fSTejun Heo 	/*
26721c6321fSTejun Heo 	 * There may be multiple instances of this function racing to
26821c6321fSTejun Heo 	 * update the same inode.  Use cmpxchg() to tell the winner.
26921c6321fSTejun Heo 	 */
27021c6321fSTejun Heo 	if (unlikely(cmpxchg(&inode->i_wb, NULL, wb)))
27121c6321fSTejun Heo 		wb_put(wb);
27221c6321fSTejun Heo }
2739b0eb69bSTejun Heo EXPORT_SYMBOL_GPL(__inode_attach_wb);
27421c6321fSTejun Heo 
275703c2708STejun Heo /**
27687e1d789STejun Heo  * locked_inode_to_wb_and_lock_list - determine a locked inode's wb and lock it
27787e1d789STejun Heo  * @inode: inode of interest with i_lock held
27887e1d789STejun Heo  *
27987e1d789STejun Heo  * Returns @inode's wb with its list_lock held.  @inode->i_lock must be
28087e1d789STejun Heo  * held on entry and is released on return.  The returned wb is guaranteed
28187e1d789STejun Heo  * to stay @inode's associated wb until its list_lock is released.
28287e1d789STejun Heo  */
28387e1d789STejun Heo static struct bdi_writeback *
28487e1d789STejun Heo locked_inode_to_wb_and_lock_list(struct inode *inode)
28587e1d789STejun Heo 	__releases(&inode->i_lock)
28687e1d789STejun Heo 	__acquires(&wb->list_lock)
28787e1d789STejun Heo {
28887e1d789STejun Heo 	while (true) {
28987e1d789STejun Heo 		struct bdi_writeback *wb = inode_to_wb(inode);
29087e1d789STejun Heo 
29187e1d789STejun Heo 		/*
29287e1d789STejun Heo 		 * inode_to_wb() association is protected by both
29387e1d789STejun Heo 		 * @inode->i_lock and @wb->list_lock but list_lock nests
29487e1d789STejun Heo 		 * outside i_lock.  Drop i_lock and verify that the
29587e1d789STejun Heo 		 * association hasn't changed after acquiring list_lock.
29687e1d789STejun Heo 		 */
29787e1d789STejun Heo 		wb_get(wb);
29887e1d789STejun Heo 		spin_unlock(&inode->i_lock);
29987e1d789STejun Heo 		spin_lock(&wb->list_lock);
30087e1d789STejun Heo 
301aaa2cacfSTejun Heo 		/* i_wb may have changed inbetween, can't use inode_to_wb() */
302614a4e37STejun Heo 		if (likely(wb == inode->i_wb)) {
303614a4e37STejun Heo 			wb_put(wb);	/* @inode already has ref */
304614a4e37STejun Heo 			return wb;
305614a4e37STejun Heo 		}
30687e1d789STejun Heo 
30787e1d789STejun Heo 		spin_unlock(&wb->list_lock);
308614a4e37STejun Heo 		wb_put(wb);
30987e1d789STejun Heo 		cpu_relax();
31087e1d789STejun Heo 		spin_lock(&inode->i_lock);
31187e1d789STejun Heo 	}
31287e1d789STejun Heo }
31387e1d789STejun Heo 
31487e1d789STejun Heo /**
31587e1d789STejun Heo  * inode_to_wb_and_lock_list - determine an inode's wb and lock it
31687e1d789STejun Heo  * @inode: inode of interest
31787e1d789STejun Heo  *
31887e1d789STejun Heo  * Same as locked_inode_to_wb_and_lock_list() but @inode->i_lock isn't held
31987e1d789STejun Heo  * on entry.
32087e1d789STejun Heo  */
32187e1d789STejun Heo static struct bdi_writeback *inode_to_wb_and_lock_list(struct inode *inode)
32287e1d789STejun Heo 	__acquires(&wb->list_lock)
32387e1d789STejun Heo {
32487e1d789STejun Heo 	spin_lock(&inode->i_lock);
32587e1d789STejun Heo 	return locked_inode_to_wb_and_lock_list(inode);
32687e1d789STejun Heo }
32787e1d789STejun Heo 
328682aa8e1STejun Heo struct inode_switch_wbs_context {
329682aa8e1STejun Heo 	struct inode		*inode;
330682aa8e1STejun Heo 	struct bdi_writeback	*new_wb;
331682aa8e1STejun Heo 
332682aa8e1STejun Heo 	struct rcu_head		rcu_head;
333682aa8e1STejun Heo 	struct work_struct	work;
334682aa8e1STejun Heo };
335682aa8e1STejun Heo 
3367fc5854fSTejun Heo static void bdi_down_write_wb_switch_rwsem(struct backing_dev_info *bdi)
3377fc5854fSTejun Heo {
3387fc5854fSTejun Heo 	down_write(&bdi->wb_switch_rwsem);
3397fc5854fSTejun Heo }
3407fc5854fSTejun Heo 
3417fc5854fSTejun Heo static void bdi_up_write_wb_switch_rwsem(struct backing_dev_info *bdi)
3427fc5854fSTejun Heo {
3437fc5854fSTejun Heo 	up_write(&bdi->wb_switch_rwsem);
3447fc5854fSTejun Heo }
3457fc5854fSTejun Heo 
346682aa8e1STejun Heo static void inode_switch_wbs_work_fn(struct work_struct *work)
347682aa8e1STejun Heo {
348682aa8e1STejun Heo 	struct inode_switch_wbs_context *isw =
349682aa8e1STejun Heo 		container_of(work, struct inode_switch_wbs_context, work);
350682aa8e1STejun Heo 	struct inode *inode = isw->inode;
3517fc5854fSTejun Heo 	struct backing_dev_info *bdi = inode_to_bdi(inode);
352d10c8095STejun Heo 	struct address_space *mapping = inode->i_mapping;
353d10c8095STejun Heo 	struct bdi_writeback *old_wb = inode->i_wb;
354682aa8e1STejun Heo 	struct bdi_writeback *new_wb = isw->new_wb;
35504edf02cSMatthew Wilcox 	XA_STATE(xas, &mapping->i_pages, 0);
35604edf02cSMatthew Wilcox 	struct page *page;
357d10c8095STejun Heo 	bool switched = false;
358682aa8e1STejun Heo 
359682aa8e1STejun Heo 	/*
3607fc5854fSTejun Heo 	 * If @inode switches cgwb membership while sync_inodes_sb() is
3617fc5854fSTejun Heo 	 * being issued, sync_inodes_sb() might miss it.  Synchronize.
3627fc5854fSTejun Heo 	 */
3637fc5854fSTejun Heo 	down_read(&bdi->wb_switch_rwsem);
3647fc5854fSTejun Heo 
3657fc5854fSTejun Heo 	/*
366682aa8e1STejun Heo 	 * By the time control reaches here, RCU grace period has passed
367682aa8e1STejun Heo 	 * since I_WB_SWITCH assertion and all wb stat update transactions
368682aa8e1STejun Heo 	 * between unlocked_inode_to_wb_begin/end() are guaranteed to be
369b93b0163SMatthew Wilcox 	 * synchronizing against the i_pages lock.
370d10c8095STejun Heo 	 *
371b93b0163SMatthew Wilcox 	 * Grabbing old_wb->list_lock, inode->i_lock and the i_pages lock
372d10c8095STejun Heo 	 * gives us exclusion against all wb related operations on @inode
373d10c8095STejun Heo 	 * including IO list manipulations and stat updates.
374682aa8e1STejun Heo 	 */
375d10c8095STejun Heo 	if (old_wb < new_wb) {
376d10c8095STejun Heo 		spin_lock(&old_wb->list_lock);
377d10c8095STejun Heo 		spin_lock_nested(&new_wb->list_lock, SINGLE_DEPTH_NESTING);
378d10c8095STejun Heo 	} else {
379d10c8095STejun Heo 		spin_lock(&new_wb->list_lock);
380d10c8095STejun Heo 		spin_lock_nested(&old_wb->list_lock, SINGLE_DEPTH_NESTING);
381d10c8095STejun Heo 	}
382682aa8e1STejun Heo 	spin_lock(&inode->i_lock);
383b93b0163SMatthew Wilcox 	xa_lock_irq(&mapping->i_pages);
384682aa8e1STejun Heo 
385d10c8095STejun Heo 	/*
386d10c8095STejun Heo 	 * Once I_FREEING is visible under i_lock, the eviction path owns
387c7f54084SDave Chinner 	 * the inode and we shouldn't modify ->i_io_list.
388d10c8095STejun Heo 	 */
389d10c8095STejun Heo 	if (unlikely(inode->i_state & I_FREEING))
390d10c8095STejun Heo 		goto skip_switch;
391d10c8095STejun Heo 
392d10c8095STejun Heo 	/*
393d10c8095STejun Heo 	 * Count and transfer stats.  Note that PAGECACHE_TAG_DIRTY points
394d10c8095STejun Heo 	 * to possibly dirty pages while PAGECACHE_TAG_WRITEBACK points to
395b93b0163SMatthew Wilcox 	 * pages actually under writeback.
396d10c8095STejun Heo 	 */
39704edf02cSMatthew Wilcox 	xas_for_each_marked(&xas, page, ULONG_MAX, PAGECACHE_TAG_DIRTY) {
39804edf02cSMatthew Wilcox 		if (PageDirty(page)) {
3993e8f399dSNikolay Borisov 			dec_wb_stat(old_wb, WB_RECLAIMABLE);
4003e8f399dSNikolay Borisov 			inc_wb_stat(new_wb, WB_RECLAIMABLE);
401d10c8095STejun Heo 		}
402d10c8095STejun Heo 	}
403d10c8095STejun Heo 
40404edf02cSMatthew Wilcox 	xas_set(&xas, 0);
40504edf02cSMatthew Wilcox 	xas_for_each_marked(&xas, page, ULONG_MAX, PAGECACHE_TAG_WRITEBACK) {
406d10c8095STejun Heo 		WARN_ON_ONCE(!PageWriteback(page));
4073e8f399dSNikolay Borisov 		dec_wb_stat(old_wb, WB_WRITEBACK);
4083e8f399dSNikolay Borisov 		inc_wb_stat(new_wb, WB_WRITEBACK);
409d10c8095STejun Heo 	}
410d10c8095STejun Heo 
411d10c8095STejun Heo 	wb_get(new_wb);
412d10c8095STejun Heo 
413d10c8095STejun Heo 	/*
414d10c8095STejun Heo 	 * Transfer to @new_wb's IO list if necessary.  The specific list
415d10c8095STejun Heo 	 * @inode was on is ignored and the inode is put on ->b_dirty which
416d10c8095STejun Heo 	 * is always correct including from ->b_dirty_time.  The transfer
417d10c8095STejun Heo 	 * preserves @inode->dirtied_when ordering.
418d10c8095STejun Heo 	 */
419c7f54084SDave Chinner 	if (!list_empty(&inode->i_io_list)) {
420d10c8095STejun Heo 		struct inode *pos;
421d10c8095STejun Heo 
422c7f54084SDave Chinner 		inode_io_list_del_locked(inode, old_wb);
423d10c8095STejun Heo 		inode->i_wb = new_wb;
424c7f54084SDave Chinner 		list_for_each_entry(pos, &new_wb->b_dirty, i_io_list)
425d10c8095STejun Heo 			if (time_after_eq(inode->dirtied_when,
426d10c8095STejun Heo 					  pos->dirtied_when))
427d10c8095STejun Heo 				break;
428c7f54084SDave Chinner 		inode_io_list_move_locked(inode, new_wb, pos->i_io_list.prev);
429d10c8095STejun Heo 	} else {
430d10c8095STejun Heo 		inode->i_wb = new_wb;
431d10c8095STejun Heo 	}
432d10c8095STejun Heo 
433d10c8095STejun Heo 	/* ->i_wb_frn updates may race wbc_detach_inode() but doesn't matter */
434682aa8e1STejun Heo 	inode->i_wb_frn_winner = 0;
435682aa8e1STejun Heo 	inode->i_wb_frn_avg_time = 0;
436682aa8e1STejun Heo 	inode->i_wb_frn_history = 0;
437d10c8095STejun Heo 	switched = true;
438d10c8095STejun Heo skip_switch:
439682aa8e1STejun Heo 	/*
440682aa8e1STejun Heo 	 * Paired with load_acquire in unlocked_inode_to_wb_begin() and
441682aa8e1STejun Heo 	 * ensures that the new wb is visible if they see !I_WB_SWITCH.
442682aa8e1STejun Heo 	 */
443682aa8e1STejun Heo 	smp_store_release(&inode->i_state, inode->i_state & ~I_WB_SWITCH);
444682aa8e1STejun Heo 
445b93b0163SMatthew Wilcox 	xa_unlock_irq(&mapping->i_pages);
446682aa8e1STejun Heo 	spin_unlock(&inode->i_lock);
447d10c8095STejun Heo 	spin_unlock(&new_wb->list_lock);
448d10c8095STejun Heo 	spin_unlock(&old_wb->list_lock);
449d10c8095STejun Heo 
4507fc5854fSTejun Heo 	up_read(&bdi->wb_switch_rwsem);
4517fc5854fSTejun Heo 
452d10c8095STejun Heo 	if (switched) {
453d10c8095STejun Heo 		wb_wakeup(new_wb);
454d10c8095STejun Heo 		wb_put(old_wb);
455d10c8095STejun Heo 	}
456d10c8095STejun Heo 	wb_put(new_wb);
457682aa8e1STejun Heo 
458682aa8e1STejun Heo 	iput(inode);
459682aa8e1STejun Heo 	kfree(isw);
460a1a0e23eSTejun Heo 
461a1a0e23eSTejun Heo 	atomic_dec(&isw_nr_in_flight);
462682aa8e1STejun Heo }
463682aa8e1STejun Heo 
464682aa8e1STejun Heo static void inode_switch_wbs_rcu_fn(struct rcu_head *rcu_head)
465682aa8e1STejun Heo {
466682aa8e1STejun Heo 	struct inode_switch_wbs_context *isw = container_of(rcu_head,
467682aa8e1STejun Heo 				struct inode_switch_wbs_context, rcu_head);
468682aa8e1STejun Heo 
469682aa8e1STejun Heo 	/* needs to grab bh-unsafe locks, bounce to work item */
470682aa8e1STejun Heo 	INIT_WORK(&isw->work, inode_switch_wbs_work_fn);
471a1a0e23eSTejun Heo 	queue_work(isw_wq, &isw->work);
472682aa8e1STejun Heo }
473682aa8e1STejun Heo 
474682aa8e1STejun Heo /**
475682aa8e1STejun Heo  * inode_switch_wbs - change the wb association of an inode
476682aa8e1STejun Heo  * @inode: target inode
477682aa8e1STejun Heo  * @new_wb_id: ID of the new wb
478682aa8e1STejun Heo  *
479682aa8e1STejun Heo  * Switch @inode's wb association to the wb identified by @new_wb_id.  The
480682aa8e1STejun Heo  * switching is performed asynchronously and may fail silently.
481682aa8e1STejun Heo  */
482682aa8e1STejun Heo static void inode_switch_wbs(struct inode *inode, int new_wb_id)
483682aa8e1STejun Heo {
484682aa8e1STejun Heo 	struct backing_dev_info *bdi = inode_to_bdi(inode);
485682aa8e1STejun Heo 	struct cgroup_subsys_state *memcg_css;
486682aa8e1STejun Heo 	struct inode_switch_wbs_context *isw;
487682aa8e1STejun Heo 
488682aa8e1STejun Heo 	/* noop if seems to be already in progress */
489682aa8e1STejun Heo 	if (inode->i_state & I_WB_SWITCH)
490682aa8e1STejun Heo 		return;
491682aa8e1STejun Heo 
4927fc5854fSTejun Heo 	/*
4937fc5854fSTejun Heo 	 * Avoid starting new switches while sync_inodes_sb() is in
4947fc5854fSTejun Heo 	 * progress.  Otherwise, if the down_write protected issue path
4957fc5854fSTejun Heo 	 * blocks heavily, we might end up starting a large number of
4967fc5854fSTejun Heo 	 * switches which will block on the rwsem.
4977fc5854fSTejun Heo 	 */
4987fc5854fSTejun Heo 	if (!down_read_trylock(&bdi->wb_switch_rwsem))
4997fc5854fSTejun Heo 		return;
5007fc5854fSTejun Heo 
501682aa8e1STejun Heo 	isw = kzalloc(sizeof(*isw), GFP_ATOMIC);
502682aa8e1STejun Heo 	if (!isw)
5037fc5854fSTejun Heo 		goto out_unlock;
504682aa8e1STejun Heo 
505682aa8e1STejun Heo 	/* find and pin the new wb */
506682aa8e1STejun Heo 	rcu_read_lock();
507682aa8e1STejun Heo 	memcg_css = css_from_id(new_wb_id, &memory_cgrp_subsys);
508682aa8e1STejun Heo 	if (memcg_css)
509682aa8e1STejun Heo 		isw->new_wb = wb_get_create(bdi, memcg_css, GFP_ATOMIC);
510682aa8e1STejun Heo 	rcu_read_unlock();
511682aa8e1STejun Heo 	if (!isw->new_wb)
512682aa8e1STejun Heo 		goto out_free;
513682aa8e1STejun Heo 
514682aa8e1STejun Heo 	/* while holding I_WB_SWITCH, no one else can update the association */
515682aa8e1STejun Heo 	spin_lock(&inode->i_lock);
5161751e8a6SLinus Torvalds 	if (!(inode->i_sb->s_flags & SB_ACTIVE) ||
517a1a0e23eSTejun Heo 	    inode->i_state & (I_WB_SWITCH | I_FREEING) ||
518a1a0e23eSTejun Heo 	    inode_to_wb(inode) == isw->new_wb) {
519a1a0e23eSTejun Heo 		spin_unlock(&inode->i_lock);
520a1a0e23eSTejun Heo 		goto out_free;
521a1a0e23eSTejun Heo 	}
522682aa8e1STejun Heo 	inode->i_state |= I_WB_SWITCH;
52374524955STahsin Erdogan 	__iget(inode);
524682aa8e1STejun Heo 	spin_unlock(&inode->i_lock);
525682aa8e1STejun Heo 
526682aa8e1STejun Heo 	isw->inode = inode;
527682aa8e1STejun Heo 
528682aa8e1STejun Heo 	/*
529682aa8e1STejun Heo 	 * In addition to synchronizing among switchers, I_WB_SWITCH tells
530b93b0163SMatthew Wilcox 	 * the RCU protected stat update paths to grab the i_page
531b93b0163SMatthew Wilcox 	 * lock so that stat transfer can synchronize against them.
532682aa8e1STejun Heo 	 * Let's continue after I_WB_SWITCH is guaranteed to be visible.
533682aa8e1STejun Heo 	 */
534682aa8e1STejun Heo 	call_rcu(&isw->rcu_head, inode_switch_wbs_rcu_fn);
535ec084de9SJiufei Xue 
536ec084de9SJiufei Xue 	atomic_inc(&isw_nr_in_flight);
537ec084de9SJiufei Xue 
5387fc5854fSTejun Heo 	goto out_unlock;
539682aa8e1STejun Heo 
540682aa8e1STejun Heo out_free:
541682aa8e1STejun Heo 	if (isw->new_wb)
542682aa8e1STejun Heo 		wb_put(isw->new_wb);
543682aa8e1STejun Heo 	kfree(isw);
5447fc5854fSTejun Heo out_unlock:
5457fc5854fSTejun Heo 	up_read(&bdi->wb_switch_rwsem);
546682aa8e1STejun Heo }
547682aa8e1STejun Heo 
54887e1d789STejun Heo /**
549b16b1debSTejun Heo  * wbc_attach_and_unlock_inode - associate wbc with target inode and unlock it
550b16b1debSTejun Heo  * @wbc: writeback_control of interest
551b16b1debSTejun Heo  * @inode: target inode
552b16b1debSTejun Heo  *
553b16b1debSTejun Heo  * @inode is locked and about to be written back under the control of @wbc.
554b16b1debSTejun Heo  * Record @inode's writeback context into @wbc and unlock the i_lock.  On
555b16b1debSTejun Heo  * writeback completion, wbc_detach_inode() should be called.  This is used
556b16b1debSTejun Heo  * to track the cgroup writeback context.
557b16b1debSTejun Heo  */
558b16b1debSTejun Heo void wbc_attach_and_unlock_inode(struct writeback_control *wbc,
559b16b1debSTejun Heo 				 struct inode *inode)
560b16b1debSTejun Heo {
561dd73e4b7STejun Heo 	if (!inode_cgwb_enabled(inode)) {
562dd73e4b7STejun Heo 		spin_unlock(&inode->i_lock);
563dd73e4b7STejun Heo 		return;
564dd73e4b7STejun Heo 	}
565dd73e4b7STejun Heo 
566b16b1debSTejun Heo 	wbc->wb = inode_to_wb(inode);
5672a814908STejun Heo 	wbc->inode = inode;
5682a814908STejun Heo 
5692a814908STejun Heo 	wbc->wb_id = wbc->wb->memcg_css->id;
5702a814908STejun Heo 	wbc->wb_lcand_id = inode->i_wb_frn_winner;
5712a814908STejun Heo 	wbc->wb_tcand_id = 0;
5722a814908STejun Heo 	wbc->wb_bytes = 0;
5732a814908STejun Heo 	wbc->wb_lcand_bytes = 0;
5742a814908STejun Heo 	wbc->wb_tcand_bytes = 0;
5752a814908STejun Heo 
576b16b1debSTejun Heo 	wb_get(wbc->wb);
577b16b1debSTejun Heo 	spin_unlock(&inode->i_lock);
578e8a7abf5STejun Heo 
579e8a7abf5STejun Heo 	/*
580e8a7abf5STejun Heo 	 * A dying wb indicates that the memcg-blkcg mapping has changed
581e8a7abf5STejun Heo 	 * and a new wb is already serving the memcg.  Switch immediately.
582e8a7abf5STejun Heo 	 */
583e8a7abf5STejun Heo 	if (unlikely(wb_dying(wbc->wb)))
584e8a7abf5STejun Heo 		inode_switch_wbs(inode, wbc->wb_id);
585b16b1debSTejun Heo }
5869b0eb69bSTejun Heo EXPORT_SYMBOL_GPL(wbc_attach_and_unlock_inode);
587b16b1debSTejun Heo 
588b16b1debSTejun Heo /**
5892a814908STejun Heo  * wbc_detach_inode - disassociate wbc from inode and perform foreign detection
5902a814908STejun Heo  * @wbc: writeback_control of the just finished writeback
591b16b1debSTejun Heo  *
592b16b1debSTejun Heo  * To be called after a writeback attempt of an inode finishes and undoes
593b16b1debSTejun Heo  * wbc_attach_and_unlock_inode().  Can be called under any context.
5942a814908STejun Heo  *
5952a814908STejun Heo  * As concurrent write sharing of an inode is expected to be very rare and
5962a814908STejun Heo  * memcg only tracks page ownership on first-use basis severely confining
5972a814908STejun Heo  * the usefulness of such sharing, cgroup writeback tracks ownership
5982a814908STejun Heo  * per-inode.  While the support for concurrent write sharing of an inode
5992a814908STejun Heo  * is deemed unnecessary, an inode being written to by different cgroups at
6002a814908STejun Heo  * different points in time is a lot more common, and, more importantly,
6012a814908STejun Heo  * charging only by first-use can too readily lead to grossly incorrect
6022a814908STejun Heo  * behaviors (single foreign page can lead to gigabytes of writeback to be
6032a814908STejun Heo  * incorrectly attributed).
6042a814908STejun Heo  *
6052a814908STejun Heo  * To resolve this issue, cgroup writeback detects the majority dirtier of
6062a814908STejun Heo  * an inode and transfers the ownership to it.  To avoid unnnecessary
6072a814908STejun Heo  * oscillation, the detection mechanism keeps track of history and gives
6082a814908STejun Heo  * out the switch verdict only if the foreign usage pattern is stable over
6092a814908STejun Heo  * a certain amount of time and/or writeback attempts.
6102a814908STejun Heo  *
6112a814908STejun Heo  * On each writeback attempt, @wbc tries to detect the majority writer
6122a814908STejun Heo  * using Boyer-Moore majority vote algorithm.  In addition to the byte
6132a814908STejun Heo  * count from the majority voting, it also counts the bytes written for the
6142a814908STejun Heo  * current wb and the last round's winner wb (max of last round's current
6152a814908STejun Heo  * wb, the winner from two rounds ago, and the last round's majority
6162a814908STejun Heo  * candidate).  Keeping track of the historical winner helps the algorithm
6172a814908STejun Heo  * to semi-reliably detect the most active writer even when it's not the
6182a814908STejun Heo  * absolute majority.
6192a814908STejun Heo  *
6202a814908STejun Heo  * Once the winner of the round is determined, whether the winner is
6212a814908STejun Heo  * foreign or not and how much IO time the round consumed is recorded in
6222a814908STejun Heo  * inode->i_wb_frn_history.  If the amount of recorded foreign IO time is
6232a814908STejun Heo  * over a certain threshold, the switch verdict is given.
624b16b1debSTejun Heo  */
625b16b1debSTejun Heo void wbc_detach_inode(struct writeback_control *wbc)
626b16b1debSTejun Heo {
6272a814908STejun Heo 	struct bdi_writeback *wb = wbc->wb;
6282a814908STejun Heo 	struct inode *inode = wbc->inode;
629dd73e4b7STejun Heo 	unsigned long avg_time, max_bytes, max_time;
630dd73e4b7STejun Heo 	u16 history;
6312a814908STejun Heo 	int max_id;
6322a814908STejun Heo 
633dd73e4b7STejun Heo 	if (!wb)
634dd73e4b7STejun Heo 		return;
635dd73e4b7STejun Heo 
636dd73e4b7STejun Heo 	history = inode->i_wb_frn_history;
637dd73e4b7STejun Heo 	avg_time = inode->i_wb_frn_avg_time;
638dd73e4b7STejun Heo 
6392a814908STejun Heo 	/* pick the winner of this round */
6402a814908STejun Heo 	if (wbc->wb_bytes >= wbc->wb_lcand_bytes &&
6412a814908STejun Heo 	    wbc->wb_bytes >= wbc->wb_tcand_bytes) {
6422a814908STejun Heo 		max_id = wbc->wb_id;
6432a814908STejun Heo 		max_bytes = wbc->wb_bytes;
6442a814908STejun Heo 	} else if (wbc->wb_lcand_bytes >= wbc->wb_tcand_bytes) {
6452a814908STejun Heo 		max_id = wbc->wb_lcand_id;
6462a814908STejun Heo 		max_bytes = wbc->wb_lcand_bytes;
6472a814908STejun Heo 	} else {
6482a814908STejun Heo 		max_id = wbc->wb_tcand_id;
6492a814908STejun Heo 		max_bytes = wbc->wb_tcand_bytes;
6502a814908STejun Heo 	}
6512a814908STejun Heo 
6522a814908STejun Heo 	/*
6532a814908STejun Heo 	 * Calculate the amount of IO time the winner consumed and fold it
6542a814908STejun Heo 	 * into the running average kept per inode.  If the consumed IO
6552a814908STejun Heo 	 * time is lower than avag / WB_FRN_TIME_CUT_DIV, ignore it for
6562a814908STejun Heo 	 * deciding whether to switch or not.  This is to prevent one-off
6572a814908STejun Heo 	 * small dirtiers from skewing the verdict.
6582a814908STejun Heo 	 */
6592a814908STejun Heo 	max_time = DIV_ROUND_UP((max_bytes >> PAGE_SHIFT) << WB_FRN_TIME_SHIFT,
6602a814908STejun Heo 				wb->avg_write_bandwidth);
6612a814908STejun Heo 	if (avg_time)
6622a814908STejun Heo 		avg_time += (max_time >> WB_FRN_TIME_AVG_SHIFT) -
6632a814908STejun Heo 			    (avg_time >> WB_FRN_TIME_AVG_SHIFT);
6642a814908STejun Heo 	else
6652a814908STejun Heo 		avg_time = max_time;	/* immediate catch up on first run */
6662a814908STejun Heo 
6672a814908STejun Heo 	if (max_time >= avg_time / WB_FRN_TIME_CUT_DIV) {
6682a814908STejun Heo 		int slots;
6692a814908STejun Heo 
6702a814908STejun Heo 		/*
6712a814908STejun Heo 		 * The switch verdict is reached if foreign wb's consume
6722a814908STejun Heo 		 * more than a certain proportion of IO time in a
6732a814908STejun Heo 		 * WB_FRN_TIME_PERIOD.  This is loosely tracked by 16 slot
6742a814908STejun Heo 		 * history mask where each bit represents one sixteenth of
6752a814908STejun Heo 		 * the period.  Determine the number of slots to shift into
6762a814908STejun Heo 		 * history from @max_time.
6772a814908STejun Heo 		 */
6782a814908STejun Heo 		slots = min(DIV_ROUND_UP(max_time, WB_FRN_HIST_UNIT),
6792a814908STejun Heo 			    (unsigned long)WB_FRN_HIST_MAX_SLOTS);
6802a814908STejun Heo 		history <<= slots;
6812a814908STejun Heo 		if (wbc->wb_id != max_id)
6822a814908STejun Heo 			history |= (1U << slots) - 1;
6832a814908STejun Heo 
6842a814908STejun Heo 		/*
6852a814908STejun Heo 		 * Switch if the current wb isn't the consistent winner.
6862a814908STejun Heo 		 * If there are multiple closely competing dirtiers, the
6872a814908STejun Heo 		 * inode may switch across them repeatedly over time, which
6882a814908STejun Heo 		 * is okay.  The main goal is avoiding keeping an inode on
6892a814908STejun Heo 		 * the wrong wb for an extended period of time.
6902a814908STejun Heo 		 */
691682aa8e1STejun Heo 		if (hweight32(history) > WB_FRN_HIST_THR_SLOTS)
692682aa8e1STejun Heo 			inode_switch_wbs(inode, max_id);
6932a814908STejun Heo 	}
6942a814908STejun Heo 
6952a814908STejun Heo 	/*
6962a814908STejun Heo 	 * Multiple instances of this function may race to update the
6972a814908STejun Heo 	 * following fields but we don't mind occassional inaccuracies.
6982a814908STejun Heo 	 */
6992a814908STejun Heo 	inode->i_wb_frn_winner = max_id;
7002a814908STejun Heo 	inode->i_wb_frn_avg_time = min(avg_time, (unsigned long)U16_MAX);
7012a814908STejun Heo 	inode->i_wb_frn_history = history;
7022a814908STejun Heo 
703b16b1debSTejun Heo 	wb_put(wbc->wb);
704b16b1debSTejun Heo 	wbc->wb = NULL;
705b16b1debSTejun Heo }
7069b0eb69bSTejun Heo EXPORT_SYMBOL_GPL(wbc_detach_inode);
707b16b1debSTejun Heo 
708b16b1debSTejun Heo /**
70934e51a5eSTejun Heo  * wbc_account_cgroup_owner - account writeback to update inode cgroup ownership
7102a814908STejun Heo  * @wbc: writeback_control of the writeback in progress
7112a814908STejun Heo  * @page: page being written out
7122a814908STejun Heo  * @bytes: number of bytes being written out
7132a814908STejun Heo  *
7142a814908STejun Heo  * @bytes from @page are about to written out during the writeback
7152a814908STejun Heo  * controlled by @wbc.  Keep the book for foreign inode detection.  See
7162a814908STejun Heo  * wbc_detach_inode().
7172a814908STejun Heo  */
71834e51a5eSTejun Heo void wbc_account_cgroup_owner(struct writeback_control *wbc, struct page *page,
7192a814908STejun Heo 			      size_t bytes)
7202a814908STejun Heo {
72166311422STejun Heo 	struct cgroup_subsys_state *css;
7222a814908STejun Heo 	int id;
7232a814908STejun Heo 
7242a814908STejun Heo 	/*
7252a814908STejun Heo 	 * pageout() path doesn't attach @wbc to the inode being written
7262a814908STejun Heo 	 * out.  This is intentional as we don't want the function to block
7272a814908STejun Heo 	 * behind a slow cgroup.  Ultimately, we want pageout() to kick off
7282a814908STejun Heo 	 * regular writeback instead of writing things out itself.
7292a814908STejun Heo 	 */
73027b36d8fSTejun Heo 	if (!wbc->wb || wbc->no_cgroup_owner)
7312a814908STejun Heo 		return;
7322a814908STejun Heo 
73366311422STejun Heo 	css = mem_cgroup_css_from_page(page);
73466311422STejun Heo 	/* dead cgroups shouldn't contribute to inode ownership arbitration */
73566311422STejun Heo 	if (!(css->flags & CSS_ONLINE))
73666311422STejun Heo 		return;
73766311422STejun Heo 
73866311422STejun Heo 	id = css->id;
7392a814908STejun Heo 
7402a814908STejun Heo 	if (id == wbc->wb_id) {
7412a814908STejun Heo 		wbc->wb_bytes += bytes;
7422a814908STejun Heo 		return;
7432a814908STejun Heo 	}
7442a814908STejun Heo 
7452a814908STejun Heo 	if (id == wbc->wb_lcand_id)
7462a814908STejun Heo 		wbc->wb_lcand_bytes += bytes;
7472a814908STejun Heo 
7482a814908STejun Heo 	/* Boyer-Moore majority vote algorithm */
7492a814908STejun Heo 	if (!wbc->wb_tcand_bytes)
7502a814908STejun Heo 		wbc->wb_tcand_id = id;
7512a814908STejun Heo 	if (id == wbc->wb_tcand_id)
7522a814908STejun Heo 		wbc->wb_tcand_bytes += bytes;
7532a814908STejun Heo 	else
7542a814908STejun Heo 		wbc->wb_tcand_bytes -= min(bytes, wbc->wb_tcand_bytes);
7552a814908STejun Heo }
75634e51a5eSTejun Heo EXPORT_SYMBOL_GPL(wbc_account_cgroup_owner);
7572a814908STejun Heo 
7582a814908STejun Heo /**
759703c2708STejun Heo  * inode_congested - test whether an inode is congested
76060292bccSTejun Heo  * @inode: inode to test for congestion (may be NULL)
761703c2708STejun Heo  * @cong_bits: mask of WB_[a]sync_congested bits to test
762703c2708STejun Heo  *
763703c2708STejun Heo  * Tests whether @inode is congested.  @cong_bits is the mask of congestion
764703c2708STejun Heo  * bits to test and the return value is the mask of set bits.
765703c2708STejun Heo  *
766703c2708STejun Heo  * If cgroup writeback is enabled for @inode, the congestion state is
767703c2708STejun Heo  * determined by whether the cgwb (cgroup bdi_writeback) for the blkcg
768703c2708STejun Heo  * associated with @inode is congested; otherwise, the root wb's congestion
769703c2708STejun Heo  * state is used.
77060292bccSTejun Heo  *
77160292bccSTejun Heo  * @inode is allowed to be NULL as this function is often called on
77260292bccSTejun Heo  * mapping->host which is NULL for the swapper space.
773703c2708STejun Heo  */
774703c2708STejun Heo int inode_congested(struct inode *inode, int cong_bits)
775703c2708STejun Heo {
7765cb8b824STejun Heo 	/*
7775cb8b824STejun Heo 	 * Once set, ->i_wb never becomes NULL while the inode is alive.
7785cb8b824STejun Heo 	 * Start transaction iff ->i_wb is visible.
7795cb8b824STejun Heo 	 */
780aaa2cacfSTejun Heo 	if (inode && inode_to_wb_is_valid(inode)) {
7815cb8b824STejun Heo 		struct bdi_writeback *wb;
7822e898e4cSGreg Thelen 		struct wb_lock_cookie lock_cookie = {};
7832e898e4cSGreg Thelen 		bool congested;
7845cb8b824STejun Heo 
7852e898e4cSGreg Thelen 		wb = unlocked_inode_to_wb_begin(inode, &lock_cookie);
7865cb8b824STejun Heo 		congested = wb_congested(wb, cong_bits);
7872e898e4cSGreg Thelen 		unlocked_inode_to_wb_end(inode, &lock_cookie);
7885cb8b824STejun Heo 		return congested;
789703c2708STejun Heo 	}
790703c2708STejun Heo 
791703c2708STejun Heo 	return wb_congested(&inode_to_bdi(inode)->wb, cong_bits);
792703c2708STejun Heo }
793703c2708STejun Heo EXPORT_SYMBOL_GPL(inode_congested);
794703c2708STejun Heo 
795f2b65121STejun Heo /**
796f2b65121STejun Heo  * wb_split_bdi_pages - split nr_pages to write according to bandwidth
797f2b65121STejun Heo  * @wb: target bdi_writeback to split @nr_pages to
798f2b65121STejun Heo  * @nr_pages: number of pages to write for the whole bdi
799f2b65121STejun Heo  *
800f2b65121STejun Heo  * Split @wb's portion of @nr_pages according to @wb's write bandwidth in
801f2b65121STejun Heo  * relation to the total write bandwidth of all wb's w/ dirty inodes on
802f2b65121STejun Heo  * @wb->bdi.
803f2b65121STejun Heo  */
804f2b65121STejun Heo static long wb_split_bdi_pages(struct bdi_writeback *wb, long nr_pages)
805f2b65121STejun Heo {
806f2b65121STejun Heo 	unsigned long this_bw = wb->avg_write_bandwidth;
807f2b65121STejun Heo 	unsigned long tot_bw = atomic_long_read(&wb->bdi->tot_write_bandwidth);
808f2b65121STejun Heo 
809f2b65121STejun Heo 	if (nr_pages == LONG_MAX)
810f2b65121STejun Heo 		return LONG_MAX;
811f2b65121STejun Heo 
812f2b65121STejun Heo 	/*
813f2b65121STejun Heo 	 * This may be called on clean wb's and proportional distribution
814f2b65121STejun Heo 	 * may not make sense, just use the original @nr_pages in those
815f2b65121STejun Heo 	 * cases.  In general, we wanna err on the side of writing more.
816f2b65121STejun Heo 	 */
817f2b65121STejun Heo 	if (!tot_bw || this_bw >= tot_bw)
818f2b65121STejun Heo 		return nr_pages;
819f2b65121STejun Heo 	else
820f2b65121STejun Heo 		return DIV_ROUND_UP_ULL((u64)nr_pages * this_bw, tot_bw);
821f2b65121STejun Heo }
822f2b65121STejun Heo 
823db125360STejun Heo /**
824db125360STejun Heo  * bdi_split_work_to_wbs - split a wb_writeback_work to all wb's of a bdi
825db125360STejun Heo  * @bdi: target backing_dev_info
826db125360STejun Heo  * @base_work: wb_writeback_work to issue
827db125360STejun Heo  * @skip_if_busy: skip wb's which already have writeback in progress
828db125360STejun Heo  *
829db125360STejun Heo  * Split and issue @base_work to all wb's (bdi_writeback's) of @bdi which
830db125360STejun Heo  * have dirty inodes.  If @base_work->nr_page isn't %LONG_MAX, it's
831db125360STejun Heo  * distributed to the busy wbs according to each wb's proportion in the
832db125360STejun Heo  * total active write bandwidth of @bdi.
833db125360STejun Heo  */
834db125360STejun Heo static void bdi_split_work_to_wbs(struct backing_dev_info *bdi,
835db125360STejun Heo 				  struct wb_writeback_work *base_work,
836db125360STejun Heo 				  bool skip_if_busy)
837db125360STejun Heo {
838b817525aSTejun Heo 	struct bdi_writeback *last_wb = NULL;
839b33e18f6STejun Heo 	struct bdi_writeback *wb = list_entry(&bdi->wb_list,
840b817525aSTejun Heo 					      struct bdi_writeback, bdi_node);
841db125360STejun Heo 
842db125360STejun Heo 	might_sleep();
843db125360STejun Heo restart:
844db125360STejun Heo 	rcu_read_lock();
845b817525aSTejun Heo 	list_for_each_entry_continue_rcu(wb, &bdi->wb_list, bdi_node) {
8468a1270cdSTejun Heo 		DEFINE_WB_COMPLETION_ONSTACK(fallback_work_done);
8478a1270cdSTejun Heo 		struct wb_writeback_work fallback_work;
8488a1270cdSTejun Heo 		struct wb_writeback_work *work;
8498a1270cdSTejun Heo 		long nr_pages;
8508a1270cdSTejun Heo 
851b817525aSTejun Heo 		if (last_wb) {
852b817525aSTejun Heo 			wb_put(last_wb);
853b817525aSTejun Heo 			last_wb = NULL;
854b817525aSTejun Heo 		}
855b817525aSTejun Heo 
856006a0973STejun Heo 		/* SYNC_ALL writes out I_DIRTY_TIME too */
857006a0973STejun Heo 		if (!wb_has_dirty_io(wb) &&
858006a0973STejun Heo 		    (base_work->sync_mode == WB_SYNC_NONE ||
859006a0973STejun Heo 		     list_empty(&wb->b_dirty_time)))
860006a0973STejun Heo 			continue;
861006a0973STejun Heo 		if (skip_if_busy && writeback_in_progress(wb))
862db125360STejun Heo 			continue;
863db125360STejun Heo 
8648a1270cdSTejun Heo 		nr_pages = wb_split_bdi_pages(wb, base_work->nr_pages);
8658a1270cdSTejun Heo 
8668a1270cdSTejun Heo 		work = kmalloc(sizeof(*work), GFP_ATOMIC);
8678a1270cdSTejun Heo 		if (work) {
8688a1270cdSTejun Heo 			*work = *base_work;
8698a1270cdSTejun Heo 			work->nr_pages = nr_pages;
8708a1270cdSTejun Heo 			work->auto_free = 1;
8718a1270cdSTejun Heo 			wb_queue_work(wb, work);
8728a1270cdSTejun Heo 			continue;
873db125360STejun Heo 		}
8748a1270cdSTejun Heo 
8758a1270cdSTejun Heo 		/* alloc failed, execute synchronously using on-stack fallback */
8768a1270cdSTejun Heo 		work = &fallback_work;
8778a1270cdSTejun Heo 		*work = *base_work;
8788a1270cdSTejun Heo 		work->nr_pages = nr_pages;
8798a1270cdSTejun Heo 		work->auto_free = 0;
8808a1270cdSTejun Heo 		work->done = &fallback_work_done;
8818a1270cdSTejun Heo 
8828a1270cdSTejun Heo 		wb_queue_work(wb, work);
8838a1270cdSTejun Heo 
884b817525aSTejun Heo 		/*
885b817525aSTejun Heo 		 * Pin @wb so that it stays on @bdi->wb_list.  This allows
886b817525aSTejun Heo 		 * continuing iteration from @wb after dropping and
887b817525aSTejun Heo 		 * regrabbing rcu read lock.
888b817525aSTejun Heo 		 */
889b817525aSTejun Heo 		wb_get(wb);
890b817525aSTejun Heo 		last_wb = wb;
891b817525aSTejun Heo 
892db125360STejun Heo 		rcu_read_unlock();
8938a1270cdSTejun Heo 		wb_wait_for_completion(bdi, &fallback_work_done);
894db125360STejun Heo 		goto restart;
895db125360STejun Heo 	}
896db125360STejun Heo 	rcu_read_unlock();
897b817525aSTejun Heo 
898b817525aSTejun Heo 	if (last_wb)
899b817525aSTejun Heo 		wb_put(last_wb);
900db125360STejun Heo }
901db125360STejun Heo 
902a1a0e23eSTejun Heo /**
903a1a0e23eSTejun Heo  * cgroup_writeback_umount - flush inode wb switches for umount
904a1a0e23eSTejun Heo  *
905a1a0e23eSTejun Heo  * This function is called when a super_block is about to be destroyed and
906a1a0e23eSTejun Heo  * flushes in-flight inode wb switches.  An inode wb switch goes through
907a1a0e23eSTejun Heo  * RCU and then workqueue, so the two need to be flushed in order to ensure
908a1a0e23eSTejun Heo  * that all previously scheduled switches are finished.  As wb switches are
909a1a0e23eSTejun Heo  * rare occurrences and synchronize_rcu() can take a while, perform
910a1a0e23eSTejun Heo  * flushing iff wb switches are in flight.
911a1a0e23eSTejun Heo  */
912a1a0e23eSTejun Heo void cgroup_writeback_umount(void)
913a1a0e23eSTejun Heo {
914a1a0e23eSTejun Heo 	if (atomic_read(&isw_nr_in_flight)) {
915ec084de9SJiufei Xue 		/*
916ec084de9SJiufei Xue 		 * Use rcu_barrier() to wait for all pending callbacks to
917ec084de9SJiufei Xue 		 * ensure that all in-flight wb switches are in the workqueue.
918ec084de9SJiufei Xue 		 */
919ec084de9SJiufei Xue 		rcu_barrier();
920a1a0e23eSTejun Heo 		flush_workqueue(isw_wq);
921a1a0e23eSTejun Heo 	}
922a1a0e23eSTejun Heo }
923a1a0e23eSTejun Heo 
924a1a0e23eSTejun Heo static int __init cgroup_writeback_init(void)
925a1a0e23eSTejun Heo {
926a1a0e23eSTejun Heo 	isw_wq = alloc_workqueue("inode_switch_wbs", 0, 0);
927a1a0e23eSTejun Heo 	if (!isw_wq)
928a1a0e23eSTejun Heo 		return -ENOMEM;
929a1a0e23eSTejun Heo 	return 0;
930a1a0e23eSTejun Heo }
931a1a0e23eSTejun Heo fs_initcall(cgroup_writeback_init);
932a1a0e23eSTejun Heo 
933f2b65121STejun Heo #else	/* CONFIG_CGROUP_WRITEBACK */
934f2b65121STejun Heo 
9357fc5854fSTejun Heo static void bdi_down_write_wb_switch_rwsem(struct backing_dev_info *bdi) { }
9367fc5854fSTejun Heo static void bdi_up_write_wb_switch_rwsem(struct backing_dev_info *bdi) { }
9377fc5854fSTejun Heo 
93887e1d789STejun Heo static struct bdi_writeback *
93987e1d789STejun Heo locked_inode_to_wb_and_lock_list(struct inode *inode)
94087e1d789STejun Heo 	__releases(&inode->i_lock)
94187e1d789STejun Heo 	__acquires(&wb->list_lock)
94287e1d789STejun Heo {
94387e1d789STejun Heo 	struct bdi_writeback *wb = inode_to_wb(inode);
94487e1d789STejun Heo 
94587e1d789STejun Heo 	spin_unlock(&inode->i_lock);
94687e1d789STejun Heo 	spin_lock(&wb->list_lock);
94787e1d789STejun Heo 	return wb;
94887e1d789STejun Heo }
94987e1d789STejun Heo 
95087e1d789STejun Heo static struct bdi_writeback *inode_to_wb_and_lock_list(struct inode *inode)
95187e1d789STejun Heo 	__acquires(&wb->list_lock)
95287e1d789STejun Heo {
95387e1d789STejun Heo 	struct bdi_writeback *wb = inode_to_wb(inode);
95487e1d789STejun Heo 
95587e1d789STejun Heo 	spin_lock(&wb->list_lock);
95687e1d789STejun Heo 	return wb;
95787e1d789STejun Heo }
95887e1d789STejun Heo 
959f2b65121STejun Heo static long wb_split_bdi_pages(struct bdi_writeback *wb, long nr_pages)
960f2b65121STejun Heo {
961f2b65121STejun Heo 	return nr_pages;
962f2b65121STejun Heo }
963f2b65121STejun Heo 
964db125360STejun Heo static void bdi_split_work_to_wbs(struct backing_dev_info *bdi,
965db125360STejun Heo 				  struct wb_writeback_work *base_work,
966db125360STejun Heo 				  bool skip_if_busy)
967db125360STejun Heo {
968db125360STejun Heo 	might_sleep();
969db125360STejun Heo 
970006a0973STejun Heo 	if (!skip_if_busy || !writeback_in_progress(&bdi->wb)) {
971db125360STejun Heo 		base_work->auto_free = 0;
972db125360STejun Heo 		wb_queue_work(&bdi->wb, base_work);
973db125360STejun Heo 	}
974db125360STejun Heo }
975db125360STejun Heo 
976703c2708STejun Heo #endif	/* CONFIG_CGROUP_WRITEBACK */
977703c2708STejun Heo 
978e8e8a0c6SJens Axboe /*
979e8e8a0c6SJens Axboe  * Add in the number of potentially dirty inodes, because each inode
980e8e8a0c6SJens Axboe  * write can dirty pagecache in the underlying blockdev.
981e8e8a0c6SJens Axboe  */
982e8e8a0c6SJens Axboe static unsigned long get_nr_dirty_pages(void)
983e8e8a0c6SJens Axboe {
984e8e8a0c6SJens Axboe 	return global_node_page_state(NR_FILE_DIRTY) +
985e8e8a0c6SJens Axboe 		global_node_page_state(NR_UNSTABLE_NFS) +
986e8e8a0c6SJens Axboe 		get_nr_dirty_inodes();
987e8e8a0c6SJens Axboe }
988e8e8a0c6SJens Axboe 
989e8e8a0c6SJens Axboe static void wb_start_writeback(struct bdi_writeback *wb, enum wb_reason reason)
990b6e51316SJens Axboe {
991c00ddad3STejun Heo 	if (!wb_has_dirty_io(wb))
992c00ddad3STejun Heo 		return;
993c00ddad3STejun Heo 
994c00ddad3STejun Heo 	/*
995aac8d41cSJens Axboe 	 * All callers of this function want to start writeback of all
996aac8d41cSJens Axboe 	 * dirty pages. Places like vmscan can call this at a very
997aac8d41cSJens Axboe 	 * high frequency, causing pointless allocations of tons of
998aac8d41cSJens Axboe 	 * work items and keeping the flusher threads busy retrieving
999aac8d41cSJens Axboe 	 * that work. Ensure that we only allow one of them pending and
100085009b4fSJens Axboe 	 * inflight at the time.
1001aac8d41cSJens Axboe 	 */
100285009b4fSJens Axboe 	if (test_bit(WB_start_all, &wb->state) ||
100385009b4fSJens Axboe 	    test_and_set_bit(WB_start_all, &wb->state))
1004aac8d41cSJens Axboe 		return;
1005aac8d41cSJens Axboe 
100685009b4fSJens Axboe 	wb->start_all_reason = reason;
1007c00ddad3STejun Heo 	wb_wakeup(wb);
1008d3ddec76SWu Fengguang }
1009d3ddec76SWu Fengguang 
1010c5444198SChristoph Hellwig /**
10119ecf4866STejun Heo  * wb_start_background_writeback - start background writeback
10129ecf4866STejun Heo  * @wb: bdi_writback to write from
1013c5444198SChristoph Hellwig  *
1014c5444198SChristoph Hellwig  * Description:
10156585027aSJan Kara  *   This makes sure WB_SYNC_NONE background writeback happens. When
10169ecf4866STejun Heo  *   this function returns, it is only guaranteed that for given wb
10176585027aSJan Kara  *   some IO is happening if we are over background dirty threshold.
10186585027aSJan Kara  *   Caller need not hold sb s_umount semaphore.
1019c5444198SChristoph Hellwig  */
10209ecf4866STejun Heo void wb_start_background_writeback(struct bdi_writeback *wb)
1021c5444198SChristoph Hellwig {
10226585027aSJan Kara 	/*
10236585027aSJan Kara 	 * We just wake up the flusher thread. It will perform background
10246585027aSJan Kara 	 * writeback as soon as there is no other work to do.
10256585027aSJan Kara 	 */
10265634cc2aSTejun Heo 	trace_writeback_wake_background(wb);
10279ecf4866STejun Heo 	wb_wakeup(wb);
10281da177e4SLinus Torvalds }
10291da177e4SLinus Torvalds 
10301da177e4SLinus Torvalds /*
1031a66979abSDave Chinner  * Remove the inode from the writeback list it is on.
1032a66979abSDave Chinner  */
1033c7f54084SDave Chinner void inode_io_list_del(struct inode *inode)
1034a66979abSDave Chinner {
103587e1d789STejun Heo 	struct bdi_writeback *wb;
1036a66979abSDave Chinner 
103787e1d789STejun Heo 	wb = inode_to_wb_and_lock_list(inode);
1038c7f54084SDave Chinner 	inode_io_list_del_locked(inode, wb);
103952ebea74STejun Heo 	spin_unlock(&wb->list_lock);
1040f758eeabSChristoph Hellwig }
1041a66979abSDave Chinner 
1042a66979abSDave Chinner /*
10436c60d2b5SDave Chinner  * mark an inode as under writeback on the sb
10446c60d2b5SDave Chinner  */
10456c60d2b5SDave Chinner void sb_mark_inode_writeback(struct inode *inode)
10466c60d2b5SDave Chinner {
10476c60d2b5SDave Chinner 	struct super_block *sb = inode->i_sb;
10486c60d2b5SDave Chinner 	unsigned long flags;
10496c60d2b5SDave Chinner 
10506c60d2b5SDave Chinner 	if (list_empty(&inode->i_wb_list)) {
10516c60d2b5SDave Chinner 		spin_lock_irqsave(&sb->s_inode_wblist_lock, flags);
10529a46b04fSBrian Foster 		if (list_empty(&inode->i_wb_list)) {
10536c60d2b5SDave Chinner 			list_add_tail(&inode->i_wb_list, &sb->s_inodes_wb);
10549a46b04fSBrian Foster 			trace_sb_mark_inode_writeback(inode);
10559a46b04fSBrian Foster 		}
10566c60d2b5SDave Chinner 		spin_unlock_irqrestore(&sb->s_inode_wblist_lock, flags);
10576c60d2b5SDave Chinner 	}
10586c60d2b5SDave Chinner }
10596c60d2b5SDave Chinner 
10606c60d2b5SDave Chinner /*
10616c60d2b5SDave Chinner  * clear an inode as under writeback on the sb
10626c60d2b5SDave Chinner  */
10636c60d2b5SDave Chinner void sb_clear_inode_writeback(struct inode *inode)
10646c60d2b5SDave Chinner {
10656c60d2b5SDave Chinner 	struct super_block *sb = inode->i_sb;
10666c60d2b5SDave Chinner 	unsigned long flags;
10676c60d2b5SDave Chinner 
10686c60d2b5SDave Chinner 	if (!list_empty(&inode->i_wb_list)) {
10696c60d2b5SDave Chinner 		spin_lock_irqsave(&sb->s_inode_wblist_lock, flags);
10709a46b04fSBrian Foster 		if (!list_empty(&inode->i_wb_list)) {
10716c60d2b5SDave Chinner 			list_del_init(&inode->i_wb_list);
10729a46b04fSBrian Foster 			trace_sb_clear_inode_writeback(inode);
10739a46b04fSBrian Foster 		}
10746c60d2b5SDave Chinner 		spin_unlock_irqrestore(&sb->s_inode_wblist_lock, flags);
10756c60d2b5SDave Chinner 	}
10766c60d2b5SDave Chinner }
10776c60d2b5SDave Chinner 
10786c60d2b5SDave Chinner /*
10796610a0bcSAndrew Morton  * Redirty an inode: set its when-it-was dirtied timestamp and move it to the
10806610a0bcSAndrew Morton  * furthest end of its superblock's dirty-inode list.
10816610a0bcSAndrew Morton  *
10826610a0bcSAndrew Morton  * Before stamping the inode's ->dirtied_when, we check to see whether it is
108366f3b8e2SJens Axboe  * already the most-recently-dirtied inode on the b_dirty list.  If that is
10846610a0bcSAndrew Morton  * the case then the inode must have been redirtied while it was being written
10856610a0bcSAndrew Morton  * out and we don't reset its dirtied_when.
10866610a0bcSAndrew Morton  */
1087f758eeabSChristoph Hellwig static void redirty_tail(struct inode *inode, struct bdi_writeback *wb)
10886610a0bcSAndrew Morton {
108903ba3782SJens Axboe 	if (!list_empty(&wb->b_dirty)) {
109066f3b8e2SJens Axboe 		struct inode *tail;
10916610a0bcSAndrew Morton 
10927ccf19a8SNick Piggin 		tail = wb_inode(wb->b_dirty.next);
109366f3b8e2SJens Axboe 		if (time_before(inode->dirtied_when, tail->dirtied_when))
10946610a0bcSAndrew Morton 			inode->dirtied_when = jiffies;
10956610a0bcSAndrew Morton 	}
1096c7f54084SDave Chinner 	inode_io_list_move_locked(inode, wb, &wb->b_dirty);
10976610a0bcSAndrew Morton }
10986610a0bcSAndrew Morton 
10996610a0bcSAndrew Morton /*
110066f3b8e2SJens Axboe  * requeue inode for re-scanning after bdi->b_io list is exhausted.
1101c986d1e2SAndrew Morton  */
1102f758eeabSChristoph Hellwig static void requeue_io(struct inode *inode, struct bdi_writeback *wb)
1103c986d1e2SAndrew Morton {
1104c7f54084SDave Chinner 	inode_io_list_move_locked(inode, wb, &wb->b_more_io);
1105c986d1e2SAndrew Morton }
1106c986d1e2SAndrew Morton 
11071c0eeaf5SJoern Engel static void inode_sync_complete(struct inode *inode)
11081c0eeaf5SJoern Engel {
1109365b94aeSJan Kara 	inode->i_state &= ~I_SYNC;
11104eff96ddSJan Kara 	/* If inode is clean an unused, put it into LRU now... */
11114eff96ddSJan Kara 	inode_add_lru(inode);
1112365b94aeSJan Kara 	/* Waiters must see I_SYNC cleared before being woken up */
11131c0eeaf5SJoern Engel 	smp_mb();
11141c0eeaf5SJoern Engel 	wake_up_bit(&inode->i_state, __I_SYNC);
11151c0eeaf5SJoern Engel }
11161c0eeaf5SJoern Engel 
1117d2caa3c5SJeff Layton static bool inode_dirtied_after(struct inode *inode, unsigned long t)
1118d2caa3c5SJeff Layton {
1119d2caa3c5SJeff Layton 	bool ret = time_after(inode->dirtied_when, t);
1120d2caa3c5SJeff Layton #ifndef CONFIG_64BIT
1121d2caa3c5SJeff Layton 	/*
1122d2caa3c5SJeff Layton 	 * For inodes being constantly redirtied, dirtied_when can get stuck.
1123d2caa3c5SJeff Layton 	 * It _appears_ to be in the future, but is actually in distant past.
1124d2caa3c5SJeff Layton 	 * This test is necessary to prevent such wrapped-around relative times
11255b0830cbSJens Axboe 	 * from permanently stopping the whole bdi writeback.
1126d2caa3c5SJeff Layton 	 */
1127d2caa3c5SJeff Layton 	ret = ret && time_before_eq(inode->dirtied_when, jiffies);
1128d2caa3c5SJeff Layton #endif
1129d2caa3c5SJeff Layton 	return ret;
1130d2caa3c5SJeff Layton }
1131d2caa3c5SJeff Layton 
11320ae45f63STheodore Ts'o #define EXPIRE_DIRTY_ATIME 0x0001
11330ae45f63STheodore Ts'o 
1134c986d1e2SAndrew Morton /*
11350e2f2b23SWang Sheng-Hui  * Move expired (dirtied before work->older_than_this) dirty inodes from
1136697e6fedSJan Kara  * @delaying_queue to @dispatch_queue.
11372c136579SFengguang Wu  */
1138e84d0a4fSWu Fengguang static int move_expired_inodes(struct list_head *delaying_queue,
11392c136579SFengguang Wu 			       struct list_head *dispatch_queue,
11400ae45f63STheodore Ts'o 			       int flags,
1141ad4e38ddSCurt Wohlgemuth 			       struct wb_writeback_work *work)
11422c136579SFengguang Wu {
11430ae45f63STheodore Ts'o 	unsigned long *older_than_this = NULL;
11440ae45f63STheodore Ts'o 	unsigned long expire_time;
11455c03449dSShaohua Li 	LIST_HEAD(tmp);
11465c03449dSShaohua Li 	struct list_head *pos, *node;
1147cf137307SJens Axboe 	struct super_block *sb = NULL;
11485c03449dSShaohua Li 	struct inode *inode;
1149cf137307SJens Axboe 	int do_sb_sort = 0;
1150e84d0a4fSWu Fengguang 	int moved = 0;
11515c03449dSShaohua Li 
11520ae45f63STheodore Ts'o 	if ((flags & EXPIRE_DIRTY_ATIME) == 0)
11530ae45f63STheodore Ts'o 		older_than_this = work->older_than_this;
1154a2f48706STheodore Ts'o 	else if (!work->for_sync) {
1155a2f48706STheodore Ts'o 		expire_time = jiffies - (dirtytime_expire_interval * HZ);
11560ae45f63STheodore Ts'o 		older_than_this = &expire_time;
11570ae45f63STheodore Ts'o 	}
11582c136579SFengguang Wu 	while (!list_empty(delaying_queue)) {
11597ccf19a8SNick Piggin 		inode = wb_inode(delaying_queue->prev);
11600ae45f63STheodore Ts'o 		if (older_than_this &&
11610ae45f63STheodore Ts'o 		    inode_dirtied_after(inode, *older_than_this))
11622c136579SFengguang Wu 			break;
1163c7f54084SDave Chinner 		list_move(&inode->i_io_list, &tmp);
1164a8855990SJan Kara 		moved++;
11650ae45f63STheodore Ts'o 		if (flags & EXPIRE_DIRTY_ATIME)
11660ae45f63STheodore Ts'o 			set_bit(__I_DIRTY_TIME_EXPIRED, &inode->i_state);
1167a8855990SJan Kara 		if (sb_is_blkdev_sb(inode->i_sb))
1168a8855990SJan Kara 			continue;
1169cf137307SJens Axboe 		if (sb && sb != inode->i_sb)
1170cf137307SJens Axboe 			do_sb_sort = 1;
1171cf137307SJens Axboe 		sb = inode->i_sb;
11725c03449dSShaohua Li 	}
11735c03449dSShaohua Li 
1174cf137307SJens Axboe 	/* just one sb in list, splice to dispatch_queue and we're done */
1175cf137307SJens Axboe 	if (!do_sb_sort) {
1176cf137307SJens Axboe 		list_splice(&tmp, dispatch_queue);
1177e84d0a4fSWu Fengguang 		goto out;
1178cf137307SJens Axboe 	}
1179cf137307SJens Axboe 
11805c03449dSShaohua Li 	/* Move inodes from one superblock together */
11815c03449dSShaohua Li 	while (!list_empty(&tmp)) {
11827ccf19a8SNick Piggin 		sb = wb_inode(tmp.prev)->i_sb;
11835c03449dSShaohua Li 		list_for_each_prev_safe(pos, node, &tmp) {
11847ccf19a8SNick Piggin 			inode = wb_inode(pos);
11855c03449dSShaohua Li 			if (inode->i_sb == sb)
1186c7f54084SDave Chinner 				list_move(&inode->i_io_list, dispatch_queue);
11872c136579SFengguang Wu 		}
11882c136579SFengguang Wu 	}
1189e84d0a4fSWu Fengguang out:
1190e84d0a4fSWu Fengguang 	return moved;
11915c03449dSShaohua Li }
11922c136579SFengguang Wu 
11932c136579SFengguang Wu /*
11942c136579SFengguang Wu  * Queue all expired dirty inodes for io, eldest first.
11954ea879b9SWu Fengguang  * Before
11964ea879b9SWu Fengguang  *         newly dirtied     b_dirty    b_io    b_more_io
11974ea879b9SWu Fengguang  *         =============>    gf         edc     BA
11984ea879b9SWu Fengguang  * After
11994ea879b9SWu Fengguang  *         newly dirtied     b_dirty    b_io    b_more_io
12004ea879b9SWu Fengguang  *         =============>    g          fBAedc
12014ea879b9SWu Fengguang  *                                           |
12024ea879b9SWu Fengguang  *                                           +--> dequeue for IO
12032c136579SFengguang Wu  */
1204ad4e38ddSCurt Wohlgemuth static void queue_io(struct bdi_writeback *wb, struct wb_writeback_work *work)
12052c136579SFengguang Wu {
1206e84d0a4fSWu Fengguang 	int moved;
12070ae45f63STheodore Ts'o 
1208f758eeabSChristoph Hellwig 	assert_spin_locked(&wb->list_lock);
12094ea879b9SWu Fengguang 	list_splice_init(&wb->b_more_io, &wb->b_io);
12100ae45f63STheodore Ts'o 	moved = move_expired_inodes(&wb->b_dirty, &wb->b_io, 0, work);
12110ae45f63STheodore Ts'o 	moved += move_expired_inodes(&wb->b_dirty_time, &wb->b_io,
12120ae45f63STheodore Ts'o 				     EXPIRE_DIRTY_ATIME, work);
1213d6c10f1fSTejun Heo 	if (moved)
1214d6c10f1fSTejun Heo 		wb_io_lists_populated(wb);
1215ad4e38ddSCurt Wohlgemuth 	trace_writeback_queue_io(wb, work, moved);
121666f3b8e2SJens Axboe }
121766f3b8e2SJens Axboe 
1218a9185b41SChristoph Hellwig static int write_inode(struct inode *inode, struct writeback_control *wbc)
121966f3b8e2SJens Axboe {
12209fb0a7daSTejun Heo 	int ret;
12219fb0a7daSTejun Heo 
12229fb0a7daSTejun Heo 	if (inode->i_sb->s_op->write_inode && !is_bad_inode(inode)) {
12239fb0a7daSTejun Heo 		trace_writeback_write_inode_start(inode, wbc);
12249fb0a7daSTejun Heo 		ret = inode->i_sb->s_op->write_inode(inode, wbc);
12259fb0a7daSTejun Heo 		trace_writeback_write_inode(inode, wbc);
12269fb0a7daSTejun Heo 		return ret;
12279fb0a7daSTejun Heo 	}
122803ba3782SJens Axboe 	return 0;
122966f3b8e2SJens Axboe }
123008d8e974SFengguang Wu 
12312c136579SFengguang Wu /*
1232169ebd90SJan Kara  * Wait for writeback on an inode to complete. Called with i_lock held.
1233169ebd90SJan Kara  * Caller must make sure inode cannot go away when we drop i_lock.
123401c03194SChristoph Hellwig  */
1235169ebd90SJan Kara static void __inode_wait_for_writeback(struct inode *inode)
1236169ebd90SJan Kara 	__releases(inode->i_lock)
1237169ebd90SJan Kara 	__acquires(inode->i_lock)
123801c03194SChristoph Hellwig {
123901c03194SChristoph Hellwig 	DEFINE_WAIT_BIT(wq, &inode->i_state, __I_SYNC);
124001c03194SChristoph Hellwig 	wait_queue_head_t *wqh;
124101c03194SChristoph Hellwig 
124201c03194SChristoph Hellwig 	wqh = bit_waitqueue(&inode->i_state, __I_SYNC);
124358a9d3d8SRichard Kennedy 	while (inode->i_state & I_SYNC) {
1244250df6edSDave Chinner 		spin_unlock(&inode->i_lock);
124574316201SNeilBrown 		__wait_on_bit(wqh, &wq, bit_wait,
124674316201SNeilBrown 			      TASK_UNINTERRUPTIBLE);
1247250df6edSDave Chinner 		spin_lock(&inode->i_lock);
124858a9d3d8SRichard Kennedy 	}
124901c03194SChristoph Hellwig }
125001c03194SChristoph Hellwig 
125101c03194SChristoph Hellwig /*
1252169ebd90SJan Kara  * Wait for writeback on an inode to complete. Caller must have inode pinned.
1253169ebd90SJan Kara  */
1254169ebd90SJan Kara void inode_wait_for_writeback(struct inode *inode)
1255169ebd90SJan Kara {
1256169ebd90SJan Kara 	spin_lock(&inode->i_lock);
1257169ebd90SJan Kara 	__inode_wait_for_writeback(inode);
1258169ebd90SJan Kara 	spin_unlock(&inode->i_lock);
1259169ebd90SJan Kara }
1260169ebd90SJan Kara 
1261169ebd90SJan Kara /*
1262169ebd90SJan Kara  * Sleep until I_SYNC is cleared. This function must be called with i_lock
1263169ebd90SJan Kara  * held and drops it. It is aimed for callers not holding any inode reference
1264169ebd90SJan Kara  * so once i_lock is dropped, inode can go away.
1265169ebd90SJan Kara  */
1266169ebd90SJan Kara static void inode_sleep_on_writeback(struct inode *inode)
1267169ebd90SJan Kara 	__releases(inode->i_lock)
1268169ebd90SJan Kara {
1269169ebd90SJan Kara 	DEFINE_WAIT(wait);
1270169ebd90SJan Kara 	wait_queue_head_t *wqh = bit_waitqueue(&inode->i_state, __I_SYNC);
1271169ebd90SJan Kara 	int sleep;
1272169ebd90SJan Kara 
1273169ebd90SJan Kara 	prepare_to_wait(wqh, &wait, TASK_UNINTERRUPTIBLE);
1274169ebd90SJan Kara 	sleep = inode->i_state & I_SYNC;
1275169ebd90SJan Kara 	spin_unlock(&inode->i_lock);
1276169ebd90SJan Kara 	if (sleep)
1277169ebd90SJan Kara 		schedule();
1278169ebd90SJan Kara 	finish_wait(wqh, &wait);
1279169ebd90SJan Kara }
1280169ebd90SJan Kara 
1281169ebd90SJan Kara /*
1282ccb26b5aSJan Kara  * Find proper writeback list for the inode depending on its current state and
1283ccb26b5aSJan Kara  * possibly also change of its state while we were doing writeback.  Here we
1284ccb26b5aSJan Kara  * handle things such as livelock prevention or fairness of writeback among
1285ccb26b5aSJan Kara  * inodes. This function can be called only by flusher thread - noone else
1286ccb26b5aSJan Kara  * processes all inodes in writeback lists and requeueing inodes behind flusher
1287ccb26b5aSJan Kara  * thread's back can have unexpected consequences.
1288ccb26b5aSJan Kara  */
1289ccb26b5aSJan Kara static void requeue_inode(struct inode *inode, struct bdi_writeback *wb,
1290ccb26b5aSJan Kara 			  struct writeback_control *wbc)
1291ccb26b5aSJan Kara {
1292ccb26b5aSJan Kara 	if (inode->i_state & I_FREEING)
1293ccb26b5aSJan Kara 		return;
1294ccb26b5aSJan Kara 
1295ccb26b5aSJan Kara 	/*
1296ccb26b5aSJan Kara 	 * Sync livelock prevention. Each inode is tagged and synced in one
1297ccb26b5aSJan Kara 	 * shot. If still dirty, it will be redirty_tail()'ed below.  Update
1298ccb26b5aSJan Kara 	 * the dirty time to prevent enqueue and sync it again.
1299ccb26b5aSJan Kara 	 */
1300ccb26b5aSJan Kara 	if ((inode->i_state & I_DIRTY) &&
1301ccb26b5aSJan Kara 	    (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages))
1302ccb26b5aSJan Kara 		inode->dirtied_when = jiffies;
1303ccb26b5aSJan Kara 
13044f8ad655SJan Kara 	if (wbc->pages_skipped) {
13054f8ad655SJan Kara 		/*
13064f8ad655SJan Kara 		 * writeback is not making progress due to locked
13074f8ad655SJan Kara 		 * buffers. Skip this inode for now.
13084f8ad655SJan Kara 		 */
13094f8ad655SJan Kara 		redirty_tail(inode, wb);
13104f8ad655SJan Kara 		return;
13114f8ad655SJan Kara 	}
13124f8ad655SJan Kara 
1313ccb26b5aSJan Kara 	if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY)) {
1314ccb26b5aSJan Kara 		/*
1315ccb26b5aSJan Kara 		 * We didn't write back all the pages.  nfs_writepages()
1316ccb26b5aSJan Kara 		 * sometimes bales out without doing anything.
1317ccb26b5aSJan Kara 		 */
1318ccb26b5aSJan Kara 		if (wbc->nr_to_write <= 0) {
1319ccb26b5aSJan Kara 			/* Slice used up. Queue for next turn. */
1320ccb26b5aSJan Kara 			requeue_io(inode, wb);
1321ccb26b5aSJan Kara 		} else {
1322ccb26b5aSJan Kara 			/*
1323ccb26b5aSJan Kara 			 * Writeback blocked by something other than
1324ccb26b5aSJan Kara 			 * congestion. Delay the inode for some time to
1325ccb26b5aSJan Kara 			 * avoid spinning on the CPU (100% iowait)
1326ccb26b5aSJan Kara 			 * retrying writeback of the dirty page/inode
1327ccb26b5aSJan Kara 			 * that cannot be performed immediately.
1328ccb26b5aSJan Kara 			 */
1329ccb26b5aSJan Kara 			redirty_tail(inode, wb);
1330ccb26b5aSJan Kara 		}
1331ccb26b5aSJan Kara 	} else if (inode->i_state & I_DIRTY) {
1332ccb26b5aSJan Kara 		/*
1333ccb26b5aSJan Kara 		 * Filesystems can dirty the inode during writeback operations,
1334ccb26b5aSJan Kara 		 * such as delayed allocation during submission or metadata
1335ccb26b5aSJan Kara 		 * updates after data IO completion.
1336ccb26b5aSJan Kara 		 */
1337ccb26b5aSJan Kara 		redirty_tail(inode, wb);
13380ae45f63STheodore Ts'o 	} else if (inode->i_state & I_DIRTY_TIME) {
1339a2f48706STheodore Ts'o 		inode->dirtied_when = jiffies;
1340c7f54084SDave Chinner 		inode_io_list_move_locked(inode, wb, &wb->b_dirty_time);
1341ccb26b5aSJan Kara 	} else {
1342ccb26b5aSJan Kara 		/* The inode is clean. Remove from writeback lists. */
1343c7f54084SDave Chinner 		inode_io_list_del_locked(inode, wb);
1344ccb26b5aSJan Kara 	}
1345ccb26b5aSJan Kara }
1346ccb26b5aSJan Kara 
1347ccb26b5aSJan Kara /*
13484f8ad655SJan Kara  * Write out an inode and its dirty pages. Do not update the writeback list
13494f8ad655SJan Kara  * linkage. That is left to the caller. The caller is also responsible for
13504f8ad655SJan Kara  * setting I_SYNC flag and calling inode_sync_complete() to clear it.
13511da177e4SLinus Torvalds  */
13521da177e4SLinus Torvalds static int
1353cd8ed2a4SYan Hong __writeback_single_inode(struct inode *inode, struct writeback_control *wbc)
13541da177e4SLinus Torvalds {
13551da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
1356251d6a47SWu Fengguang 	long nr_to_write = wbc->nr_to_write;
135701c03194SChristoph Hellwig 	unsigned dirty;
13581da177e4SLinus Torvalds 	int ret;
13591da177e4SLinus Torvalds 
13604f8ad655SJan Kara 	WARN_ON(!(inode->i_state & I_SYNC));
13611da177e4SLinus Torvalds 
13629fb0a7daSTejun Heo 	trace_writeback_single_inode_start(inode, wbc, nr_to_write);
13639fb0a7daSTejun Heo 
13641da177e4SLinus Torvalds 	ret = do_writepages(mapping, wbc);
13651da177e4SLinus Torvalds 
136626821ed4SChristoph Hellwig 	/*
136726821ed4SChristoph Hellwig 	 * Make sure to wait on the data before writing out the metadata.
136826821ed4SChristoph Hellwig 	 * This is important for filesystems that modify metadata on data
13697747bd4bSDave Chinner 	 * I/O completion. We don't do it for sync(2) writeback because it has a
13707747bd4bSDave Chinner 	 * separate, external IO completion path and ->sync_fs for guaranteeing
13717747bd4bSDave Chinner 	 * inode metadata is written back correctly.
137226821ed4SChristoph Hellwig 	 */
13737747bd4bSDave Chinner 	if (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync) {
137426821ed4SChristoph Hellwig 		int err = filemap_fdatawait(mapping);
13751da177e4SLinus Torvalds 		if (ret == 0)
13761da177e4SLinus Torvalds 			ret = err;
13771da177e4SLinus Torvalds 	}
13781da177e4SLinus Torvalds 
13795547e8aaSDmitry Monakhov 	/*
13805547e8aaSDmitry Monakhov 	 * Some filesystems may redirty the inode during the writeback
13815547e8aaSDmitry Monakhov 	 * due to delalloc, clear dirty metadata flags right before
13825547e8aaSDmitry Monakhov 	 * write_inode()
13835547e8aaSDmitry Monakhov 	 */
1384250df6edSDave Chinner 	spin_lock(&inode->i_lock);
13859c6ac78eSTejun Heo 
13865547e8aaSDmitry Monakhov 	dirty = inode->i_state & I_DIRTY;
1387a2f48706STheodore Ts'o 	if (inode->i_state & I_DIRTY_TIME) {
13880e11f644SChristoph Hellwig 		if ((dirty & I_DIRTY_INODE) ||
1389dc5ff2b1SJan Kara 		    wbc->sync_mode == WB_SYNC_ALL ||
1390a2f48706STheodore Ts'o 		    unlikely(inode->i_state & I_DIRTY_TIME_EXPIRED) ||
1391a2f48706STheodore Ts'o 		    unlikely(time_after(jiffies,
1392a2f48706STheodore Ts'o 					(inode->dirtied_time_when +
1393a2f48706STheodore Ts'o 					 dirtytime_expire_interval * HZ)))) {
13940ae45f63STheodore Ts'o 			dirty |= I_DIRTY_TIME | I_DIRTY_TIME_EXPIRED;
13950ae45f63STheodore Ts'o 			trace_writeback_lazytime(inode);
13960ae45f63STheodore Ts'o 		}
1397a2f48706STheodore Ts'o 	} else
1398a2f48706STheodore Ts'o 		inode->i_state &= ~I_DIRTY_TIME_EXPIRED;
13990ae45f63STheodore Ts'o 	inode->i_state &= ~dirty;
14009c6ac78eSTejun Heo 
14019c6ac78eSTejun Heo 	/*
14029c6ac78eSTejun Heo 	 * Paired with smp_mb() in __mark_inode_dirty().  This allows
14039c6ac78eSTejun Heo 	 * __mark_inode_dirty() to test i_state without grabbing i_lock -
14049c6ac78eSTejun Heo 	 * either they see the I_DIRTY bits cleared or we see the dirtied
14059c6ac78eSTejun Heo 	 * inode.
14069c6ac78eSTejun Heo 	 *
14079c6ac78eSTejun Heo 	 * I_DIRTY_PAGES is always cleared together above even if @mapping
14089c6ac78eSTejun Heo 	 * still has dirty pages.  The flag is reinstated after smp_mb() if
14099c6ac78eSTejun Heo 	 * necessary.  This guarantees that either __mark_inode_dirty()
14109c6ac78eSTejun Heo 	 * sees clear I_DIRTY_PAGES or we see PAGECACHE_TAG_DIRTY.
14119c6ac78eSTejun Heo 	 */
14129c6ac78eSTejun Heo 	smp_mb();
14139c6ac78eSTejun Heo 
14149c6ac78eSTejun Heo 	if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
14159c6ac78eSTejun Heo 		inode->i_state |= I_DIRTY_PAGES;
14169c6ac78eSTejun Heo 
1417250df6edSDave Chinner 	spin_unlock(&inode->i_lock);
14189c6ac78eSTejun Heo 
14190ae45f63STheodore Ts'o 	if (dirty & I_DIRTY_TIME)
14200ae45f63STheodore Ts'o 		mark_inode_dirty_sync(inode);
142126821ed4SChristoph Hellwig 	/* Don't write the inode if only I_DIRTY_PAGES was set */
14220ae45f63STheodore Ts'o 	if (dirty & ~I_DIRTY_PAGES) {
1423a9185b41SChristoph Hellwig 		int err = write_inode(inode, wbc);
14241da177e4SLinus Torvalds 		if (ret == 0)
14251da177e4SLinus Torvalds 			ret = err;
14261da177e4SLinus Torvalds 	}
14274f8ad655SJan Kara 	trace_writeback_single_inode(inode, wbc, nr_to_write);
14284f8ad655SJan Kara 	return ret;
14294f8ad655SJan Kara }
14304f8ad655SJan Kara 
14314f8ad655SJan Kara /*
14324f8ad655SJan Kara  * Write out an inode's dirty pages. Either the caller has an active reference
14334f8ad655SJan Kara  * on the inode or the inode has I_WILL_FREE set.
14344f8ad655SJan Kara  *
14354f8ad655SJan Kara  * This function is designed to be called for writing back one inode which
14364f8ad655SJan Kara  * we go e.g. from filesystem. Flusher thread uses __writeback_single_inode()
14374f8ad655SJan Kara  * and does more profound writeback list handling in writeback_sb_inodes().
14384f8ad655SJan Kara  */
1439aaf25593STejun Heo static int writeback_single_inode(struct inode *inode,
14404f8ad655SJan Kara 				  struct writeback_control *wbc)
14414f8ad655SJan Kara {
1442aaf25593STejun Heo 	struct bdi_writeback *wb;
14434f8ad655SJan Kara 	int ret = 0;
14444f8ad655SJan Kara 
14454f8ad655SJan Kara 	spin_lock(&inode->i_lock);
14464f8ad655SJan Kara 	if (!atomic_read(&inode->i_count))
14474f8ad655SJan Kara 		WARN_ON(!(inode->i_state & (I_WILL_FREE|I_FREEING)));
14484f8ad655SJan Kara 	else
14494f8ad655SJan Kara 		WARN_ON(inode->i_state & I_WILL_FREE);
14504f8ad655SJan Kara 
14514f8ad655SJan Kara 	if (inode->i_state & I_SYNC) {
14524f8ad655SJan Kara 		if (wbc->sync_mode != WB_SYNC_ALL)
14534f8ad655SJan Kara 			goto out;
14544f8ad655SJan Kara 		/*
1455169ebd90SJan Kara 		 * It's a data-integrity sync. We must wait. Since callers hold
1456169ebd90SJan Kara 		 * inode reference or inode has I_WILL_FREE set, it cannot go
1457169ebd90SJan Kara 		 * away under us.
14584f8ad655SJan Kara 		 */
1459169ebd90SJan Kara 		__inode_wait_for_writeback(inode);
14604f8ad655SJan Kara 	}
14614f8ad655SJan Kara 	WARN_ON(inode->i_state & I_SYNC);
14624f8ad655SJan Kara 	/*
1463f9b0e058SJan Kara 	 * Skip inode if it is clean and we have no outstanding writeback in
1464f9b0e058SJan Kara 	 * WB_SYNC_ALL mode. We don't want to mess with writeback lists in this
1465f9b0e058SJan Kara 	 * function since flusher thread may be doing for example sync in
1466f9b0e058SJan Kara 	 * parallel and if we move the inode, it could get skipped. So here we
1467f9b0e058SJan Kara 	 * make sure inode is on some writeback list and leave it there unless
1468f9b0e058SJan Kara 	 * we have completely cleaned the inode.
14694f8ad655SJan Kara 	 */
14700ae45f63STheodore Ts'o 	if (!(inode->i_state & I_DIRTY_ALL) &&
1471f9b0e058SJan Kara 	    (wbc->sync_mode != WB_SYNC_ALL ||
1472f9b0e058SJan Kara 	     !mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK)))
14734f8ad655SJan Kara 		goto out;
14744f8ad655SJan Kara 	inode->i_state |= I_SYNC;
1475b16b1debSTejun Heo 	wbc_attach_and_unlock_inode(wbc, inode);
14764f8ad655SJan Kara 
1477cd8ed2a4SYan Hong 	ret = __writeback_single_inode(inode, wbc);
14781da177e4SLinus Torvalds 
1479b16b1debSTejun Heo 	wbc_detach_inode(wbc);
1480aaf25593STejun Heo 
1481aaf25593STejun Heo 	wb = inode_to_wb_and_lock_list(inode);
1482250df6edSDave Chinner 	spin_lock(&inode->i_lock);
14834f8ad655SJan Kara 	/*
14844f8ad655SJan Kara 	 * If inode is clean, remove it from writeback lists. Otherwise don't
14854f8ad655SJan Kara 	 * touch it. See comment above for explanation.
14864f8ad655SJan Kara 	 */
14870ae45f63STheodore Ts'o 	if (!(inode->i_state & I_DIRTY_ALL))
1488c7f54084SDave Chinner 		inode_io_list_del_locked(inode, wb);
14894f8ad655SJan Kara 	spin_unlock(&wb->list_lock);
14901c0eeaf5SJoern Engel 	inode_sync_complete(inode);
14914f8ad655SJan Kara out:
14924f8ad655SJan Kara 	spin_unlock(&inode->i_lock);
14931da177e4SLinus Torvalds 	return ret;
14941da177e4SLinus Torvalds }
14951da177e4SLinus Torvalds 
1496a88a341aSTejun Heo static long writeback_chunk_size(struct bdi_writeback *wb,
14971a12d8bdSWu Fengguang 				 struct wb_writeback_work *work)
1498d46db3d5SWu Fengguang {
1499d46db3d5SWu Fengguang 	long pages;
1500d46db3d5SWu Fengguang 
1501d46db3d5SWu Fengguang 	/*
1502d46db3d5SWu Fengguang 	 * WB_SYNC_ALL mode does livelock avoidance by syncing dirty
1503d46db3d5SWu Fengguang 	 * inodes/pages in one big loop. Setting wbc.nr_to_write=LONG_MAX
1504d46db3d5SWu Fengguang 	 * here avoids calling into writeback_inodes_wb() more than once.
1505d46db3d5SWu Fengguang 	 *
1506d46db3d5SWu Fengguang 	 * The intended call sequence for WB_SYNC_ALL writeback is:
1507d46db3d5SWu Fengguang 	 *
1508d46db3d5SWu Fengguang 	 *      wb_writeback()
1509d46db3d5SWu Fengguang 	 *          writeback_sb_inodes()       <== called only once
1510d46db3d5SWu Fengguang 	 *              write_cache_pages()     <== called once for each inode
1511d46db3d5SWu Fengguang 	 *                   (quickly) tag currently dirty pages
1512d46db3d5SWu Fengguang 	 *                   (maybe slowly) sync all tagged pages
1513d46db3d5SWu Fengguang 	 */
1514d46db3d5SWu Fengguang 	if (work->sync_mode == WB_SYNC_ALL || work->tagged_writepages)
1515d46db3d5SWu Fengguang 		pages = LONG_MAX;
15161a12d8bdSWu Fengguang 	else {
1517a88a341aSTejun Heo 		pages = min(wb->avg_write_bandwidth / 2,
1518dcc25ae7STejun Heo 			    global_wb_domain.dirty_limit / DIRTY_SCOPE);
15191a12d8bdSWu Fengguang 		pages = min(pages, work->nr_pages);
15201a12d8bdSWu Fengguang 		pages = round_down(pages + MIN_WRITEBACK_PAGES,
15211a12d8bdSWu Fengguang 				   MIN_WRITEBACK_PAGES);
15221a12d8bdSWu Fengguang 	}
1523d46db3d5SWu Fengguang 
1524d46db3d5SWu Fengguang 	return pages;
1525d46db3d5SWu Fengguang }
1526d46db3d5SWu Fengguang 
152703ba3782SJens Axboe /*
1528f11c9c5cSEdward Shishkin  * Write a portion of b_io inodes which belong to @sb.
1529edadfb10SChristoph Hellwig  *
1530d46db3d5SWu Fengguang  * Return the number of pages and/or inodes written.
15310ba13fd1SLinus Torvalds  *
15320ba13fd1SLinus Torvalds  * NOTE! This is called with wb->list_lock held, and will
15330ba13fd1SLinus Torvalds  * unlock and relock that for each inode it ends up doing
15340ba13fd1SLinus Torvalds  * IO for.
1535f11c9c5cSEdward Shishkin  */
1536d46db3d5SWu Fengguang static long writeback_sb_inodes(struct super_block *sb,
1537d46db3d5SWu Fengguang 				struct bdi_writeback *wb,
1538d46db3d5SWu Fengguang 				struct wb_writeback_work *work)
153903ba3782SJens Axboe {
1540d46db3d5SWu Fengguang 	struct writeback_control wbc = {
1541d46db3d5SWu Fengguang 		.sync_mode		= work->sync_mode,
1542d46db3d5SWu Fengguang 		.tagged_writepages	= work->tagged_writepages,
1543d46db3d5SWu Fengguang 		.for_kupdate		= work->for_kupdate,
1544d46db3d5SWu Fengguang 		.for_background		= work->for_background,
15457747bd4bSDave Chinner 		.for_sync		= work->for_sync,
1546d46db3d5SWu Fengguang 		.range_cyclic		= work->range_cyclic,
1547d46db3d5SWu Fengguang 		.range_start		= 0,
1548d46db3d5SWu Fengguang 		.range_end		= LLONG_MAX,
1549d46db3d5SWu Fengguang 	};
1550d46db3d5SWu Fengguang 	unsigned long start_time = jiffies;
1551d46db3d5SWu Fengguang 	long write_chunk;
1552d46db3d5SWu Fengguang 	long wrote = 0;  /* count both pages and inodes */
1553d46db3d5SWu Fengguang 
155403ba3782SJens Axboe 	while (!list_empty(&wb->b_io)) {
15557ccf19a8SNick Piggin 		struct inode *inode = wb_inode(wb->b_io.prev);
1556aaf25593STejun Heo 		struct bdi_writeback *tmp_wb;
1557edadfb10SChristoph Hellwig 
1558edadfb10SChristoph Hellwig 		if (inode->i_sb != sb) {
1559d46db3d5SWu Fengguang 			if (work->sb) {
1560edadfb10SChristoph Hellwig 				/*
1561edadfb10SChristoph Hellwig 				 * We only want to write back data for this
1562edadfb10SChristoph Hellwig 				 * superblock, move all inodes not belonging
1563edadfb10SChristoph Hellwig 				 * to it back onto the dirty list.
1564edadfb10SChristoph Hellwig 				 */
1565f758eeabSChristoph Hellwig 				redirty_tail(inode, wb);
156666f3b8e2SJens Axboe 				continue;
156766f3b8e2SJens Axboe 			}
1568edadfb10SChristoph Hellwig 
1569edadfb10SChristoph Hellwig 			/*
1570edadfb10SChristoph Hellwig 			 * The inode belongs to a different superblock.
1571edadfb10SChristoph Hellwig 			 * Bounce back to the caller to unpin this and
1572edadfb10SChristoph Hellwig 			 * pin the next superblock.
1573edadfb10SChristoph Hellwig 			 */
1574d46db3d5SWu Fengguang 			break;
1575edadfb10SChristoph Hellwig 		}
1576edadfb10SChristoph Hellwig 
15779843b76aSChristoph Hellwig 		/*
1578331cbdeeSWanpeng Li 		 * Don't bother with new inodes or inodes being freed, first
1579331cbdeeSWanpeng Li 		 * kind does not need periodic writeout yet, and for the latter
15809843b76aSChristoph Hellwig 		 * kind writeout is handled by the freer.
15819843b76aSChristoph Hellwig 		 */
1582250df6edSDave Chinner 		spin_lock(&inode->i_lock);
15839843b76aSChristoph Hellwig 		if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) {
1584250df6edSDave Chinner 			spin_unlock(&inode->i_lock);
1585fcc5c222SWu Fengguang 			redirty_tail(inode, wb);
15867ef0d737SNick Piggin 			continue;
15877ef0d737SNick Piggin 		}
1588cc1676d9SJan Kara 		if ((inode->i_state & I_SYNC) && wbc.sync_mode != WB_SYNC_ALL) {
1589cc1676d9SJan Kara 			/*
1590cc1676d9SJan Kara 			 * If this inode is locked for writeback and we are not
1591cc1676d9SJan Kara 			 * doing writeback-for-data-integrity, move it to
1592cc1676d9SJan Kara 			 * b_more_io so that writeback can proceed with the
1593cc1676d9SJan Kara 			 * other inodes on s_io.
1594cc1676d9SJan Kara 			 *
1595cc1676d9SJan Kara 			 * We'll have another go at writing back this inode
1596cc1676d9SJan Kara 			 * when we completed a full scan of b_io.
1597cc1676d9SJan Kara 			 */
1598cc1676d9SJan Kara 			spin_unlock(&inode->i_lock);
1599cc1676d9SJan Kara 			requeue_io(inode, wb);
1600cc1676d9SJan Kara 			trace_writeback_sb_inodes_requeue(inode);
1601cc1676d9SJan Kara 			continue;
1602cc1676d9SJan Kara 		}
1603f0d07b7fSJan Kara 		spin_unlock(&wb->list_lock);
1604f0d07b7fSJan Kara 
16054f8ad655SJan Kara 		/*
16064f8ad655SJan Kara 		 * We already requeued the inode if it had I_SYNC set and we
16074f8ad655SJan Kara 		 * are doing WB_SYNC_NONE writeback. So this catches only the
16084f8ad655SJan Kara 		 * WB_SYNC_ALL case.
16094f8ad655SJan Kara 		 */
1610169ebd90SJan Kara 		if (inode->i_state & I_SYNC) {
1611169ebd90SJan Kara 			/* Wait for I_SYNC. This function drops i_lock... */
1612169ebd90SJan Kara 			inode_sleep_on_writeback(inode);
1613169ebd90SJan Kara 			/* Inode may be gone, start again */
1614ead188f9SJan Kara 			spin_lock(&wb->list_lock);
1615169ebd90SJan Kara 			continue;
1616169ebd90SJan Kara 		}
16174f8ad655SJan Kara 		inode->i_state |= I_SYNC;
1618b16b1debSTejun Heo 		wbc_attach_and_unlock_inode(&wbc, inode);
1619169ebd90SJan Kara 
1620a88a341aSTejun Heo 		write_chunk = writeback_chunk_size(wb, work);
1621d46db3d5SWu Fengguang 		wbc.nr_to_write = write_chunk;
1622d46db3d5SWu Fengguang 		wbc.pages_skipped = 0;
1623250df6edSDave Chinner 
1624169ebd90SJan Kara 		/*
1625169ebd90SJan Kara 		 * We use I_SYNC to pin the inode in memory. While it is set
1626169ebd90SJan Kara 		 * evict_inode() will wait so the inode cannot be freed.
1627169ebd90SJan Kara 		 */
1628cd8ed2a4SYan Hong 		__writeback_single_inode(inode, &wbc);
1629d46db3d5SWu Fengguang 
1630b16b1debSTejun Heo 		wbc_detach_inode(&wbc);
1631d46db3d5SWu Fengguang 		work->nr_pages -= write_chunk - wbc.nr_to_write;
1632d46db3d5SWu Fengguang 		wrote += write_chunk - wbc.nr_to_write;
1633590dca3aSChris Mason 
1634590dca3aSChris Mason 		if (need_resched()) {
1635590dca3aSChris Mason 			/*
1636590dca3aSChris Mason 			 * We're trying to balance between building up a nice
1637590dca3aSChris Mason 			 * long list of IOs to improve our merge rate, and
1638590dca3aSChris Mason 			 * getting those IOs out quickly for anyone throttling
1639590dca3aSChris Mason 			 * in balance_dirty_pages().  cond_resched() doesn't
1640590dca3aSChris Mason 			 * unplug, so get our IOs out the door before we
1641590dca3aSChris Mason 			 * give up the CPU.
1642590dca3aSChris Mason 			 */
1643590dca3aSChris Mason 			blk_flush_plug(current);
1644590dca3aSChris Mason 			cond_resched();
1645590dca3aSChris Mason 		}
1646590dca3aSChris Mason 
1647aaf25593STejun Heo 		/*
1648aaf25593STejun Heo 		 * Requeue @inode if still dirty.  Be careful as @inode may
1649aaf25593STejun Heo 		 * have been switched to another wb in the meantime.
1650aaf25593STejun Heo 		 */
1651aaf25593STejun Heo 		tmp_wb = inode_to_wb_and_lock_list(inode);
16524f8ad655SJan Kara 		spin_lock(&inode->i_lock);
16530ae45f63STheodore Ts'o 		if (!(inode->i_state & I_DIRTY_ALL))
1654d46db3d5SWu Fengguang 			wrote++;
1655aaf25593STejun Heo 		requeue_inode(inode, tmp_wb, &wbc);
16564f8ad655SJan Kara 		inode_sync_complete(inode);
16570f1b1fd8SDave Chinner 		spin_unlock(&inode->i_lock);
1658590dca3aSChris Mason 
1659aaf25593STejun Heo 		if (unlikely(tmp_wb != wb)) {
1660aaf25593STejun Heo 			spin_unlock(&tmp_wb->list_lock);
1661aaf25593STejun Heo 			spin_lock(&wb->list_lock);
1662aaf25593STejun Heo 		}
1663aaf25593STejun Heo 
1664d46db3d5SWu Fengguang 		/*
1665d46db3d5SWu Fengguang 		 * bail out to wb_writeback() often enough to check
1666d46db3d5SWu Fengguang 		 * background threshold and other termination conditions.
1667d46db3d5SWu Fengguang 		 */
1668d46db3d5SWu Fengguang 		if (wrote) {
1669d46db3d5SWu Fengguang 			if (time_is_before_jiffies(start_time + HZ / 10UL))
1670d46db3d5SWu Fengguang 				break;
1671d46db3d5SWu Fengguang 			if (work->nr_pages <= 0)
1672d46db3d5SWu Fengguang 				break;
16731da177e4SLinus Torvalds 		}
16748bc3be27SFengguang Wu 	}
1675d46db3d5SWu Fengguang 	return wrote;
1676f11c9c5cSEdward Shishkin }
167738f21977SNick Piggin 
1678d46db3d5SWu Fengguang static long __writeback_inodes_wb(struct bdi_writeback *wb,
1679d46db3d5SWu Fengguang 				  struct wb_writeback_work *work)
1680f11c9c5cSEdward Shishkin {
1681d46db3d5SWu Fengguang 	unsigned long start_time = jiffies;
1682d46db3d5SWu Fengguang 	long wrote = 0;
1683f11c9c5cSEdward Shishkin 
1684f11c9c5cSEdward Shishkin 	while (!list_empty(&wb->b_io)) {
16857ccf19a8SNick Piggin 		struct inode *inode = wb_inode(wb->b_io.prev);
1686f11c9c5cSEdward Shishkin 		struct super_block *sb = inode->i_sb;
1687f11c9c5cSEdward Shishkin 
1688eb6ef3dfSKonstantin Khlebnikov 		if (!trylock_super(sb)) {
16890e995816SWu Fengguang 			/*
1690eb6ef3dfSKonstantin Khlebnikov 			 * trylock_super() may fail consistently due to
16910e995816SWu Fengguang 			 * s_umount being grabbed by someone else. Don't use
16920e995816SWu Fengguang 			 * requeue_io() to avoid busy retrying the inode/sb.
16930e995816SWu Fengguang 			 */
16940e995816SWu Fengguang 			redirty_tail(inode, wb);
1695d19de7edSChristoph Hellwig 			continue;
1696334132aeSChristoph Hellwig 		}
1697d46db3d5SWu Fengguang 		wrote += writeback_sb_inodes(sb, wb, work);
1698eb6ef3dfSKonstantin Khlebnikov 		up_read(&sb->s_umount);
1699f11c9c5cSEdward Shishkin 
1700d46db3d5SWu Fengguang 		/* refer to the same tests at the end of writeback_sb_inodes */
1701d46db3d5SWu Fengguang 		if (wrote) {
1702d46db3d5SWu Fengguang 			if (time_is_before_jiffies(start_time + HZ / 10UL))
1703d46db3d5SWu Fengguang 				break;
1704d46db3d5SWu Fengguang 			if (work->nr_pages <= 0)
1705f11c9c5cSEdward Shishkin 				break;
1706f11c9c5cSEdward Shishkin 		}
1707d46db3d5SWu Fengguang 	}
170866f3b8e2SJens Axboe 	/* Leave any unwritten inodes on b_io */
1709d46db3d5SWu Fengguang 	return wrote;
171066f3b8e2SJens Axboe }
171166f3b8e2SJens Axboe 
17127d9f073bSWanpeng Li static long writeback_inodes_wb(struct bdi_writeback *wb, long nr_pages,
17130e175a18SCurt Wohlgemuth 				enum wb_reason reason)
1714edadfb10SChristoph Hellwig {
1715d46db3d5SWu Fengguang 	struct wb_writeback_work work = {
1716d46db3d5SWu Fengguang 		.nr_pages	= nr_pages,
1717d46db3d5SWu Fengguang 		.sync_mode	= WB_SYNC_NONE,
1718d46db3d5SWu Fengguang 		.range_cyclic	= 1,
17190e175a18SCurt Wohlgemuth 		.reason		= reason,
1720d46db3d5SWu Fengguang 	};
1721505a666eSLinus Torvalds 	struct blk_plug plug;
1722edadfb10SChristoph Hellwig 
1723505a666eSLinus Torvalds 	blk_start_plug(&plug);
1724f758eeabSChristoph Hellwig 	spin_lock(&wb->list_lock);
1725424b351fSWu Fengguang 	if (list_empty(&wb->b_io))
1726ad4e38ddSCurt Wohlgemuth 		queue_io(wb, &work);
1727d46db3d5SWu Fengguang 	__writeback_inodes_wb(wb, &work);
1728f758eeabSChristoph Hellwig 	spin_unlock(&wb->list_lock);
1729505a666eSLinus Torvalds 	blk_finish_plug(&plug);
1730edadfb10SChristoph Hellwig 
1731d46db3d5SWu Fengguang 	return nr_pages - work.nr_pages;
173266f3b8e2SJens Axboe }
173366f3b8e2SJens Axboe 
173403ba3782SJens Axboe /*
173503ba3782SJens Axboe  * Explicit flushing or periodic writeback of "old" data.
173603ba3782SJens Axboe  *
173703ba3782SJens Axboe  * Define "old": the first time one of an inode's pages is dirtied, we mark the
173803ba3782SJens Axboe  * dirtying-time in the inode's address_space.  So this periodic writeback code
173903ba3782SJens Axboe  * just walks the superblock inode list, writing back any inodes which are
174003ba3782SJens Axboe  * older than a specific point in time.
174103ba3782SJens Axboe  *
174203ba3782SJens Axboe  * Try to run once per dirty_writeback_interval.  But if a writeback event
174303ba3782SJens Axboe  * takes longer than a dirty_writeback_interval interval, then leave a
174403ba3782SJens Axboe  * one-second gap.
174503ba3782SJens Axboe  *
174603ba3782SJens Axboe  * older_than_this takes precedence over nr_to_write.  So we'll only write back
174703ba3782SJens Axboe  * all dirty pages if they are all attached to "old" mappings.
174803ba3782SJens Axboe  */
1749c4a77a6cSJens Axboe static long wb_writeback(struct bdi_writeback *wb,
175083ba7b07SChristoph Hellwig 			 struct wb_writeback_work *work)
175103ba3782SJens Axboe {
1752e98be2d5SWu Fengguang 	unsigned long wb_start = jiffies;
1753d46db3d5SWu Fengguang 	long nr_pages = work->nr_pages;
17540dc83bd3SJan Kara 	unsigned long oldest_jif;
1755a5989bdcSJan Kara 	struct inode *inode;
1756d46db3d5SWu Fengguang 	long progress;
1757505a666eSLinus Torvalds 	struct blk_plug plug;
175803ba3782SJens Axboe 
17590dc83bd3SJan Kara 	oldest_jif = jiffies;
17600dc83bd3SJan Kara 	work->older_than_this = &oldest_jif;
176103ba3782SJens Axboe 
1762505a666eSLinus Torvalds 	blk_start_plug(&plug);
1763e8dfc305SWu Fengguang 	spin_lock(&wb->list_lock);
176403ba3782SJens Axboe 	for (;;) {
176503ba3782SJens Axboe 		/*
1766d3ddec76SWu Fengguang 		 * Stop writeback when nr_pages has been consumed
176703ba3782SJens Axboe 		 */
176883ba7b07SChristoph Hellwig 		if (work->nr_pages <= 0)
176903ba3782SJens Axboe 			break;
177003ba3782SJens Axboe 
177103ba3782SJens Axboe 		/*
1772aa373cf5SJan Kara 		 * Background writeout and kupdate-style writeback may
1773aa373cf5SJan Kara 		 * run forever. Stop them if there is other work to do
1774aa373cf5SJan Kara 		 * so that e.g. sync can proceed. They'll be restarted
1775aa373cf5SJan Kara 		 * after the other works are all done.
1776aa373cf5SJan Kara 		 */
1777aa373cf5SJan Kara 		if ((work->for_background || work->for_kupdate) &&
1778f0054bb1STejun Heo 		    !list_empty(&wb->work_list))
1779aa373cf5SJan Kara 			break;
1780aa373cf5SJan Kara 
1781aa373cf5SJan Kara 		/*
1782d3ddec76SWu Fengguang 		 * For background writeout, stop when we are below the
1783d3ddec76SWu Fengguang 		 * background dirty threshold
178403ba3782SJens Axboe 		 */
1785aa661bbeSTejun Heo 		if (work->for_background && !wb_over_bg_thresh(wb))
178603ba3782SJens Axboe 			break;
178703ba3782SJens Axboe 
17881bc36b64SJan Kara 		/*
17891bc36b64SJan Kara 		 * Kupdate and background works are special and we want to
17901bc36b64SJan Kara 		 * include all inodes that need writing. Livelock avoidance is
17911bc36b64SJan Kara 		 * handled by these works yielding to any other work so we are
17921bc36b64SJan Kara 		 * safe.
17931bc36b64SJan Kara 		 */
1794ba9aa839SWu Fengguang 		if (work->for_kupdate) {
17950dc83bd3SJan Kara 			oldest_jif = jiffies -
1796ba9aa839SWu Fengguang 				msecs_to_jiffies(dirty_expire_interval * 10);
17971bc36b64SJan Kara 		} else if (work->for_background)
17980dc83bd3SJan Kara 			oldest_jif = jiffies;
1799028c2dd1SDave Chinner 
18005634cc2aSTejun Heo 		trace_writeback_start(wb, work);
1801e8dfc305SWu Fengguang 		if (list_empty(&wb->b_io))
1802ad4e38ddSCurt Wohlgemuth 			queue_io(wb, work);
180383ba7b07SChristoph Hellwig 		if (work->sb)
1804d46db3d5SWu Fengguang 			progress = writeback_sb_inodes(work->sb, wb, work);
1805edadfb10SChristoph Hellwig 		else
1806d46db3d5SWu Fengguang 			progress = __writeback_inodes_wb(wb, work);
18075634cc2aSTejun Heo 		trace_writeback_written(wb, work);
1808028c2dd1SDave Chinner 
1809e98be2d5SWu Fengguang 		wb_update_bandwidth(wb, wb_start);
181003ba3782SJens Axboe 
181103ba3782SJens Axboe 		/*
181271fd05a8SJens Axboe 		 * Did we write something? Try for more
1813e6fb6da2SWu Fengguang 		 *
1814e6fb6da2SWu Fengguang 		 * Dirty inodes are moved to b_io for writeback in batches.
1815e6fb6da2SWu Fengguang 		 * The completion of the current batch does not necessarily
1816e6fb6da2SWu Fengguang 		 * mean the overall work is done. So we keep looping as long
1817e6fb6da2SWu Fengguang 		 * as made some progress on cleaning pages or inodes.
181871fd05a8SJens Axboe 		 */
1819d46db3d5SWu Fengguang 		if (progress)
182003ba3782SJens Axboe 			continue;
1821a5989bdcSJan Kara 		/*
1822e6fb6da2SWu Fengguang 		 * No more inodes for IO, bail
1823a5989bdcSJan Kara 		 */
1824b7a2441fSWu Fengguang 		if (list_empty(&wb->b_more_io))
182503ba3782SJens Axboe 			break;
182603ba3782SJens Axboe 		/*
18278010c3b6SJens Axboe 		 * Nothing written. Wait for some inode to
18288010c3b6SJens Axboe 		 * become available for writeback. Otherwise
18298010c3b6SJens Axboe 		 * we'll just busyloop.
183003ba3782SJens Axboe 		 */
18315634cc2aSTejun Heo 		trace_writeback_wait(wb, work);
183203ba3782SJens Axboe 		inode = wb_inode(wb->b_more_io.prev);
1833250df6edSDave Chinner 		spin_lock(&inode->i_lock);
1834f0d07b7fSJan Kara 		spin_unlock(&wb->list_lock);
1835169ebd90SJan Kara 		/* This function drops i_lock... */
1836169ebd90SJan Kara 		inode_sleep_on_writeback(inode);
1837f0d07b7fSJan Kara 		spin_lock(&wb->list_lock);
183803ba3782SJens Axboe 	}
1839e8dfc305SWu Fengguang 	spin_unlock(&wb->list_lock);
1840505a666eSLinus Torvalds 	blk_finish_plug(&plug);
184103ba3782SJens Axboe 
1842d46db3d5SWu Fengguang 	return nr_pages - work->nr_pages;
184303ba3782SJens Axboe }
184403ba3782SJens Axboe 
184503ba3782SJens Axboe /*
184683ba7b07SChristoph Hellwig  * Return the next wb_writeback_work struct that hasn't been processed yet.
184703ba3782SJens Axboe  */
1848f0054bb1STejun Heo static struct wb_writeback_work *get_next_work_item(struct bdi_writeback *wb)
184903ba3782SJens Axboe {
185083ba7b07SChristoph Hellwig 	struct wb_writeback_work *work = NULL;
185103ba3782SJens Axboe 
1852f0054bb1STejun Heo 	spin_lock_bh(&wb->work_lock);
1853f0054bb1STejun Heo 	if (!list_empty(&wb->work_list)) {
1854f0054bb1STejun Heo 		work = list_entry(wb->work_list.next,
185583ba7b07SChristoph Hellwig 				  struct wb_writeback_work, list);
185683ba7b07SChristoph Hellwig 		list_del_init(&work->list);
185703ba3782SJens Axboe 	}
1858f0054bb1STejun Heo 	spin_unlock_bh(&wb->work_lock);
185983ba7b07SChristoph Hellwig 	return work;
186003ba3782SJens Axboe }
186103ba3782SJens Axboe 
18626585027aSJan Kara static long wb_check_background_flush(struct bdi_writeback *wb)
18636585027aSJan Kara {
1864aa661bbeSTejun Heo 	if (wb_over_bg_thresh(wb)) {
18656585027aSJan Kara 
18666585027aSJan Kara 		struct wb_writeback_work work = {
18676585027aSJan Kara 			.nr_pages	= LONG_MAX,
18686585027aSJan Kara 			.sync_mode	= WB_SYNC_NONE,
18696585027aSJan Kara 			.for_background	= 1,
18706585027aSJan Kara 			.range_cyclic	= 1,
18710e175a18SCurt Wohlgemuth 			.reason		= WB_REASON_BACKGROUND,
18726585027aSJan Kara 		};
18736585027aSJan Kara 
18746585027aSJan Kara 		return wb_writeback(wb, &work);
18756585027aSJan Kara 	}
18766585027aSJan Kara 
18776585027aSJan Kara 	return 0;
18786585027aSJan Kara }
18796585027aSJan Kara 
188003ba3782SJens Axboe static long wb_check_old_data_flush(struct bdi_writeback *wb)
188103ba3782SJens Axboe {
188203ba3782SJens Axboe 	unsigned long expired;
188303ba3782SJens Axboe 	long nr_pages;
188403ba3782SJens Axboe 
188569b62d01SJens Axboe 	/*
188669b62d01SJens Axboe 	 * When set to zero, disable periodic writeback
188769b62d01SJens Axboe 	 */
188869b62d01SJens Axboe 	if (!dirty_writeback_interval)
188969b62d01SJens Axboe 		return 0;
189069b62d01SJens Axboe 
189103ba3782SJens Axboe 	expired = wb->last_old_flush +
189203ba3782SJens Axboe 			msecs_to_jiffies(dirty_writeback_interval * 10);
189303ba3782SJens Axboe 	if (time_before(jiffies, expired))
189403ba3782SJens Axboe 		return 0;
189503ba3782SJens Axboe 
189603ba3782SJens Axboe 	wb->last_old_flush = jiffies;
1897cdf01dd5SLinus Torvalds 	nr_pages = get_nr_dirty_pages();
189803ba3782SJens Axboe 
1899c4a77a6cSJens Axboe 	if (nr_pages) {
190083ba7b07SChristoph Hellwig 		struct wb_writeback_work work = {
1901c4a77a6cSJens Axboe 			.nr_pages	= nr_pages,
1902c4a77a6cSJens Axboe 			.sync_mode	= WB_SYNC_NONE,
1903c4a77a6cSJens Axboe 			.for_kupdate	= 1,
1904c4a77a6cSJens Axboe 			.range_cyclic	= 1,
19050e175a18SCurt Wohlgemuth 			.reason		= WB_REASON_PERIODIC,
1906c4a77a6cSJens Axboe 		};
1907c4a77a6cSJens Axboe 
190883ba7b07SChristoph Hellwig 		return wb_writeback(wb, &work);
1909c4a77a6cSJens Axboe 	}
191003ba3782SJens Axboe 
191103ba3782SJens Axboe 	return 0;
191203ba3782SJens Axboe }
191303ba3782SJens Axboe 
191485009b4fSJens Axboe static long wb_check_start_all(struct bdi_writeback *wb)
191585009b4fSJens Axboe {
191685009b4fSJens Axboe 	long nr_pages;
191785009b4fSJens Axboe 
191885009b4fSJens Axboe 	if (!test_bit(WB_start_all, &wb->state))
191985009b4fSJens Axboe 		return 0;
192085009b4fSJens Axboe 
192185009b4fSJens Axboe 	nr_pages = get_nr_dirty_pages();
192285009b4fSJens Axboe 	if (nr_pages) {
192385009b4fSJens Axboe 		struct wb_writeback_work work = {
192485009b4fSJens Axboe 			.nr_pages	= wb_split_bdi_pages(wb, nr_pages),
192585009b4fSJens Axboe 			.sync_mode	= WB_SYNC_NONE,
192685009b4fSJens Axboe 			.range_cyclic	= 1,
192785009b4fSJens Axboe 			.reason		= wb->start_all_reason,
192885009b4fSJens Axboe 		};
192985009b4fSJens Axboe 
193085009b4fSJens Axboe 		nr_pages = wb_writeback(wb, &work);
193185009b4fSJens Axboe 	}
193285009b4fSJens Axboe 
193385009b4fSJens Axboe 	clear_bit(WB_start_all, &wb->state);
193485009b4fSJens Axboe 	return nr_pages;
193585009b4fSJens Axboe }
193685009b4fSJens Axboe 
193785009b4fSJens Axboe 
193803ba3782SJens Axboe /*
193903ba3782SJens Axboe  * Retrieve work items and do the writeback they describe
194003ba3782SJens Axboe  */
194125d130baSWanpeng Li static long wb_do_writeback(struct bdi_writeback *wb)
194203ba3782SJens Axboe {
194383ba7b07SChristoph Hellwig 	struct wb_writeback_work *work;
1944c4a77a6cSJens Axboe 	long wrote = 0;
194503ba3782SJens Axboe 
19464452226eSTejun Heo 	set_bit(WB_writeback_running, &wb->state);
1947f0054bb1STejun Heo 	while ((work = get_next_work_item(wb)) != NULL) {
19485634cc2aSTejun Heo 		trace_writeback_exec(wb, work);
194983ba7b07SChristoph Hellwig 		wrote += wb_writeback(wb, work);
19504a3a485bSTahsin Erdogan 		finish_writeback_work(wb, work);
195103ba3782SJens Axboe 	}
195203ba3782SJens Axboe 
195303ba3782SJens Axboe 	/*
195485009b4fSJens Axboe 	 * Check for a flush-everything request
195585009b4fSJens Axboe 	 */
195685009b4fSJens Axboe 	wrote += wb_check_start_all(wb);
195785009b4fSJens Axboe 
195885009b4fSJens Axboe 	/*
195903ba3782SJens Axboe 	 * Check for periodic writeback, kupdated() style
196003ba3782SJens Axboe 	 */
196103ba3782SJens Axboe 	wrote += wb_check_old_data_flush(wb);
19626585027aSJan Kara 	wrote += wb_check_background_flush(wb);
19634452226eSTejun Heo 	clear_bit(WB_writeback_running, &wb->state);
196403ba3782SJens Axboe 
196503ba3782SJens Axboe 	return wrote;
196603ba3782SJens Axboe }
196703ba3782SJens Axboe 
196803ba3782SJens Axboe /*
196903ba3782SJens Axboe  * Handle writeback of dirty data for the device backed by this bdi. Also
1970839a8e86STejun Heo  * reschedules periodically and does kupdated style flushing.
197103ba3782SJens Axboe  */
1972f0054bb1STejun Heo void wb_workfn(struct work_struct *work)
197303ba3782SJens Axboe {
1974839a8e86STejun Heo 	struct bdi_writeback *wb = container_of(to_delayed_work(work),
1975839a8e86STejun Heo 						struct bdi_writeback, dwork);
197603ba3782SJens Axboe 	long pages_written;
197703ba3782SJens Axboe 
1978f0054bb1STejun Heo 	set_worker_desc("flush-%s", dev_name(wb->bdi->dev));
1979766f9164SPeter Zijlstra 	current->flags |= PF_SWAPWRITE;
198003ba3782SJens Axboe 
1981839a8e86STejun Heo 	if (likely(!current_is_workqueue_rescuer() ||
19824452226eSTejun Heo 		   !test_bit(WB_registered, &wb->state))) {
198303ba3782SJens Axboe 		/*
1984f0054bb1STejun Heo 		 * The normal path.  Keep writing back @wb until its
1985839a8e86STejun Heo 		 * work_list is empty.  Note that this path is also taken
1986f0054bb1STejun Heo 		 * if @wb is shutting down even when we're running off the
1987839a8e86STejun Heo 		 * rescuer as work_list needs to be drained.
198803ba3782SJens Axboe 		 */
1989839a8e86STejun Heo 		do {
199025d130baSWanpeng Li 			pages_written = wb_do_writeback(wb);
1991455b2864SDave Chinner 			trace_writeback_pages_written(pages_written);
1992f0054bb1STejun Heo 		} while (!list_empty(&wb->work_list));
1993839a8e86STejun Heo 	} else {
1994253c34e9SArtem Bityutskiy 		/*
1995839a8e86STejun Heo 		 * bdi_wq can't get enough workers and we're running off
1996839a8e86STejun Heo 		 * the emergency worker.  Don't hog it.  Hopefully, 1024 is
1997839a8e86STejun Heo 		 * enough for efficient IO.
1998253c34e9SArtem Bityutskiy 		 */
1999f0054bb1STejun Heo 		pages_written = writeback_inodes_wb(wb, 1024,
2000839a8e86STejun Heo 						    WB_REASON_FORKER_THREAD);
2001839a8e86STejun Heo 		trace_writeback_pages_written(pages_written);
200203ba3782SJens Axboe 	}
200303ba3782SJens Axboe 
2004f0054bb1STejun Heo 	if (!list_empty(&wb->work_list))
2005b8b78495SJan Kara 		wb_wakeup(wb);
20066ca738d6SDerek Basehore 	else if (wb_has_dirty_io(wb) && dirty_writeback_interval)
2007f0054bb1STejun Heo 		wb_wakeup_delayed(wb);
2008455b2864SDave Chinner 
2009839a8e86STejun Heo 	current->flags &= ~PF_SWAPWRITE;
201003ba3782SJens Axboe }
201103ba3782SJens Axboe 
201203ba3782SJens Axboe /*
2013595043e5SJens Axboe  * Start writeback of `nr_pages' pages on this bdi. If `nr_pages' is zero,
2014595043e5SJens Axboe  * write back the whole world.
2015595043e5SJens Axboe  */
2016595043e5SJens Axboe static void __wakeup_flusher_threads_bdi(struct backing_dev_info *bdi,
2017e8e8a0c6SJens Axboe 					 enum wb_reason reason)
2018595043e5SJens Axboe {
2019595043e5SJens Axboe 	struct bdi_writeback *wb;
2020595043e5SJens Axboe 
2021595043e5SJens Axboe 	if (!bdi_has_dirty_io(bdi))
2022595043e5SJens Axboe 		return;
2023595043e5SJens Axboe 
2024595043e5SJens Axboe 	list_for_each_entry_rcu(wb, &bdi->wb_list, bdi_node)
2025e8e8a0c6SJens Axboe 		wb_start_writeback(wb, reason);
2026595043e5SJens Axboe }
2027595043e5SJens Axboe 
2028595043e5SJens Axboe void wakeup_flusher_threads_bdi(struct backing_dev_info *bdi,
2029595043e5SJens Axboe 				enum wb_reason reason)
2030595043e5SJens Axboe {
2031595043e5SJens Axboe 	rcu_read_lock();
2032e8e8a0c6SJens Axboe 	__wakeup_flusher_threads_bdi(bdi, reason);
2033595043e5SJens Axboe 	rcu_read_unlock();
2034595043e5SJens Axboe }
2035595043e5SJens Axboe 
2036595043e5SJens Axboe /*
20379ba4b2dfSJens Axboe  * Wakeup the flusher threads to start writeback of all currently dirty pages
203803ba3782SJens Axboe  */
20399ba4b2dfSJens Axboe void wakeup_flusher_threads(enum wb_reason reason)
204003ba3782SJens Axboe {
2041b8c2f347SChristoph Hellwig 	struct backing_dev_info *bdi;
2042b8c2f347SChristoph Hellwig 
204351350ea0SKonstantin Khlebnikov 	/*
204451350ea0SKonstantin Khlebnikov 	 * If we are expecting writeback progress we must submit plugged IO.
204551350ea0SKonstantin Khlebnikov 	 */
204651350ea0SKonstantin Khlebnikov 	if (blk_needs_flush_plug(current))
204751350ea0SKonstantin Khlebnikov 		blk_schedule_flush_plug(current);
204851350ea0SKonstantin Khlebnikov 
2049b8c2f347SChristoph Hellwig 	rcu_read_lock();
2050595043e5SJens Axboe 	list_for_each_entry_rcu(bdi, &bdi_list, bdi_list)
2051e8e8a0c6SJens Axboe 		__wakeup_flusher_threads_bdi(bdi, reason);
2052b8c2f347SChristoph Hellwig 	rcu_read_unlock();
205303ba3782SJens Axboe }
205403ba3782SJens Axboe 
2055a2f48706STheodore Ts'o /*
2056a2f48706STheodore Ts'o  * Wake up bdi's periodically to make sure dirtytime inodes gets
2057a2f48706STheodore Ts'o  * written back periodically.  We deliberately do *not* check the
2058a2f48706STheodore Ts'o  * b_dirtytime list in wb_has_dirty_io(), since this would cause the
2059a2f48706STheodore Ts'o  * kernel to be constantly waking up once there are any dirtytime
2060a2f48706STheodore Ts'o  * inodes on the system.  So instead we define a separate delayed work
2061a2f48706STheodore Ts'o  * function which gets called much more rarely.  (By default, only
2062a2f48706STheodore Ts'o  * once every 12 hours.)
2063a2f48706STheodore Ts'o  *
2064a2f48706STheodore Ts'o  * If there is any other write activity going on in the file system,
2065a2f48706STheodore Ts'o  * this function won't be necessary.  But if the only thing that has
2066a2f48706STheodore Ts'o  * happened on the file system is a dirtytime inode caused by an atime
2067a2f48706STheodore Ts'o  * update, we need this infrastructure below to make sure that inode
2068a2f48706STheodore Ts'o  * eventually gets pushed out to disk.
2069a2f48706STheodore Ts'o  */
2070a2f48706STheodore Ts'o static void wakeup_dirtytime_writeback(struct work_struct *w);
2071a2f48706STheodore Ts'o static DECLARE_DELAYED_WORK(dirtytime_work, wakeup_dirtytime_writeback);
2072a2f48706STheodore Ts'o 
2073a2f48706STheodore Ts'o static void wakeup_dirtytime_writeback(struct work_struct *w)
2074a2f48706STheodore Ts'o {
2075a2f48706STheodore Ts'o 	struct backing_dev_info *bdi;
2076a2f48706STheodore Ts'o 
2077a2f48706STheodore Ts'o 	rcu_read_lock();
2078a2f48706STheodore Ts'o 	list_for_each_entry_rcu(bdi, &bdi_list, bdi_list) {
2079001fe6f6STejun Heo 		struct bdi_writeback *wb;
2080001fe6f6STejun Heo 
2081b817525aSTejun Heo 		list_for_each_entry_rcu(wb, &bdi->wb_list, bdi_node)
20826fdf860fSTejun Heo 			if (!list_empty(&wb->b_dirty_time))
20836fdf860fSTejun Heo 				wb_wakeup(wb);
2084a2f48706STheodore Ts'o 	}
2085a2f48706STheodore Ts'o 	rcu_read_unlock();
2086a2f48706STheodore Ts'o 	schedule_delayed_work(&dirtytime_work, dirtytime_expire_interval * HZ);
2087a2f48706STheodore Ts'o }
2088a2f48706STheodore Ts'o 
2089a2f48706STheodore Ts'o static int __init start_dirtytime_writeback(void)
2090a2f48706STheodore Ts'o {
2091a2f48706STheodore Ts'o 	schedule_delayed_work(&dirtytime_work, dirtytime_expire_interval * HZ);
2092a2f48706STheodore Ts'o 	return 0;
2093a2f48706STheodore Ts'o }
2094a2f48706STheodore Ts'o __initcall(start_dirtytime_writeback);
2095a2f48706STheodore Ts'o 
20961efff914STheodore Ts'o int dirtytime_interval_handler(struct ctl_table *table, int write,
20971efff914STheodore Ts'o 			       void __user *buffer, size_t *lenp, loff_t *ppos)
20981efff914STheodore Ts'o {
20991efff914STheodore Ts'o 	int ret;
21001efff914STheodore Ts'o 
21011efff914STheodore Ts'o 	ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
21021efff914STheodore Ts'o 	if (ret == 0 && write)
21031efff914STheodore Ts'o 		mod_delayed_work(system_wq, &dirtytime_work, 0);
21041efff914STheodore Ts'o 	return ret;
21051efff914STheodore Ts'o }
21061efff914STheodore Ts'o 
210703ba3782SJens Axboe static noinline void block_dump___mark_inode_dirty(struct inode *inode)
210803ba3782SJens Axboe {
210903ba3782SJens Axboe 	if (inode->i_ino || strcmp(inode->i_sb->s_id, "bdev")) {
211003ba3782SJens Axboe 		struct dentry *dentry;
211103ba3782SJens Axboe 		const char *name = "?";
211203ba3782SJens Axboe 
211303ba3782SJens Axboe 		dentry = d_find_alias(inode);
211403ba3782SJens Axboe 		if (dentry) {
211503ba3782SJens Axboe 			spin_lock(&dentry->d_lock);
211603ba3782SJens Axboe 			name = (const char *) dentry->d_name.name;
211703ba3782SJens Axboe 		}
211803ba3782SJens Axboe 		printk(KERN_DEBUG
211903ba3782SJens Axboe 		       "%s(%d): dirtied inode %lu (%s) on %s\n",
212003ba3782SJens Axboe 		       current->comm, task_pid_nr(current), inode->i_ino,
212103ba3782SJens Axboe 		       name, inode->i_sb->s_id);
212203ba3782SJens Axboe 		if (dentry) {
212303ba3782SJens Axboe 			spin_unlock(&dentry->d_lock);
212403ba3782SJens Axboe 			dput(dentry);
212503ba3782SJens Axboe 		}
212603ba3782SJens Axboe 	}
212703ba3782SJens Axboe }
212803ba3782SJens Axboe 
212903ba3782SJens Axboe /**
213003ba3782SJens Axboe  * __mark_inode_dirty -	internal function
21310117d427SMauro Carvalho Chehab  *
213203ba3782SJens Axboe  * @inode: inode to mark
213303ba3782SJens Axboe  * @flags: what kind of dirty (i.e. I_DIRTY_SYNC)
21340117d427SMauro Carvalho Chehab  *
213503ba3782SJens Axboe  * Mark an inode as dirty. Callers should use mark_inode_dirty or
213603ba3782SJens Axboe  * mark_inode_dirty_sync.
213703ba3782SJens Axboe  *
213803ba3782SJens Axboe  * Put the inode on the super block's dirty list.
213903ba3782SJens Axboe  *
214003ba3782SJens Axboe  * CAREFUL! We mark it dirty unconditionally, but move it onto the
214103ba3782SJens Axboe  * dirty list only if it is hashed or if it refers to a blockdev.
214203ba3782SJens Axboe  * If it was not hashed, it will never be added to the dirty list
214303ba3782SJens Axboe  * even if it is later hashed, as it will have been marked dirty already.
214403ba3782SJens Axboe  *
214503ba3782SJens Axboe  * In short, make sure you hash any inodes _before_ you start marking
214603ba3782SJens Axboe  * them dirty.
214703ba3782SJens Axboe  *
214803ba3782SJens Axboe  * Note that for blockdevs, inode->dirtied_when represents the dirtying time of
214903ba3782SJens Axboe  * the block-special inode (/dev/hda1) itself.  And the ->dirtied_when field of
215003ba3782SJens Axboe  * the kernel-internal blockdev inode represents the dirtying time of the
215103ba3782SJens Axboe  * blockdev's pages.  This is why for I_DIRTY_PAGES we always use
215203ba3782SJens Axboe  * page->mapping->host, so the page-dirtying time is recorded in the internal
215303ba3782SJens Axboe  * blockdev inode.
215403ba3782SJens Axboe  */
215503ba3782SJens Axboe void __mark_inode_dirty(struct inode *inode, int flags)
215603ba3782SJens Axboe {
215703ba3782SJens Axboe 	struct super_block *sb = inode->i_sb;
21580ae45f63STheodore Ts'o 	int dirtytime;
21590ae45f63STheodore Ts'o 
21600ae45f63STheodore Ts'o 	trace_writeback_mark_inode_dirty(inode, flags);
216103ba3782SJens Axboe 
216203ba3782SJens Axboe 	/*
216303ba3782SJens Axboe 	 * Don't do this for I_DIRTY_PAGES - that doesn't actually
216403ba3782SJens Axboe 	 * dirty the inode itself
216503ba3782SJens Axboe 	 */
21660e11f644SChristoph Hellwig 	if (flags & (I_DIRTY_INODE | I_DIRTY_TIME)) {
21679fb0a7daSTejun Heo 		trace_writeback_dirty_inode_start(inode, flags);
21689fb0a7daSTejun Heo 
216903ba3782SJens Axboe 		if (sb->s_op->dirty_inode)
2170aa385729SChristoph Hellwig 			sb->s_op->dirty_inode(inode, flags);
21719fb0a7daSTejun Heo 
21729fb0a7daSTejun Heo 		trace_writeback_dirty_inode(inode, flags);
217303ba3782SJens Axboe 	}
21740ae45f63STheodore Ts'o 	if (flags & I_DIRTY_INODE)
21750ae45f63STheodore Ts'o 		flags &= ~I_DIRTY_TIME;
21760ae45f63STheodore Ts'o 	dirtytime = flags & I_DIRTY_TIME;
217703ba3782SJens Axboe 
217803ba3782SJens Axboe 	/*
21799c6ac78eSTejun Heo 	 * Paired with smp_mb() in __writeback_single_inode() for the
21809c6ac78eSTejun Heo 	 * following lockless i_state test.  See there for details.
218103ba3782SJens Axboe 	 */
218203ba3782SJens Axboe 	smp_mb();
218303ba3782SJens Axboe 
21840ae45f63STheodore Ts'o 	if (((inode->i_state & flags) == flags) ||
21850ae45f63STheodore Ts'o 	    (dirtytime && (inode->i_state & I_DIRTY_INODE)))
218603ba3782SJens Axboe 		return;
218703ba3782SJens Axboe 
218803ba3782SJens Axboe 	if (unlikely(block_dump))
218903ba3782SJens Axboe 		block_dump___mark_inode_dirty(inode);
219003ba3782SJens Axboe 
2191250df6edSDave Chinner 	spin_lock(&inode->i_lock);
21920ae45f63STheodore Ts'o 	if (dirtytime && (inode->i_state & I_DIRTY_INODE))
21930ae45f63STheodore Ts'o 		goto out_unlock_inode;
219403ba3782SJens Axboe 	if ((inode->i_state & flags) != flags) {
219503ba3782SJens Axboe 		const int was_dirty = inode->i_state & I_DIRTY;
219603ba3782SJens Axboe 
219752ebea74STejun Heo 		inode_attach_wb(inode, NULL);
219852ebea74STejun Heo 
21990ae45f63STheodore Ts'o 		if (flags & I_DIRTY_INODE)
22000ae45f63STheodore Ts'o 			inode->i_state &= ~I_DIRTY_TIME;
220103ba3782SJens Axboe 		inode->i_state |= flags;
220203ba3782SJens Axboe 
220303ba3782SJens Axboe 		/*
220403ba3782SJens Axboe 		 * If the inode is being synced, just update its dirty state.
220503ba3782SJens Axboe 		 * The unlocker will place the inode on the appropriate
220603ba3782SJens Axboe 		 * superblock list, based upon its state.
220703ba3782SJens Axboe 		 */
220803ba3782SJens Axboe 		if (inode->i_state & I_SYNC)
2209250df6edSDave Chinner 			goto out_unlock_inode;
221003ba3782SJens Axboe 
221103ba3782SJens Axboe 		/*
221203ba3782SJens Axboe 		 * Only add valid (hashed) inodes to the superblock's
221303ba3782SJens Axboe 		 * dirty list.  Add blockdev inodes as well.
221403ba3782SJens Axboe 		 */
221503ba3782SJens Axboe 		if (!S_ISBLK(inode->i_mode)) {
22161d3382cbSAl Viro 			if (inode_unhashed(inode))
2217250df6edSDave Chinner 				goto out_unlock_inode;
221803ba3782SJens Axboe 		}
2219a4ffdde6SAl Viro 		if (inode->i_state & I_FREEING)
2220250df6edSDave Chinner 			goto out_unlock_inode;
222103ba3782SJens Axboe 
222203ba3782SJens Axboe 		/*
222303ba3782SJens Axboe 		 * If the inode was already on b_dirty/b_io/b_more_io, don't
222403ba3782SJens Axboe 		 * reposition it (that would break b_dirty time-ordering).
222503ba3782SJens Axboe 		 */
222603ba3782SJens Axboe 		if (!was_dirty) {
222787e1d789STejun Heo 			struct bdi_writeback *wb;
2228d6c10f1fSTejun Heo 			struct list_head *dirty_list;
2229a66979abSDave Chinner 			bool wakeup_bdi = false;
2230500b067cSJens Axboe 
223187e1d789STejun Heo 			wb = locked_inode_to_wb_and_lock_list(inode);
2232253c34e9SArtem Bityutskiy 
22330747259dSTejun Heo 			WARN(bdi_cap_writeback_dirty(wb->bdi) &&
22340747259dSTejun Heo 			     !test_bit(WB_registered, &wb->state),
22350747259dSTejun Heo 			     "bdi-%s not registered\n", wb->bdi->name);
223603ba3782SJens Axboe 
223703ba3782SJens Axboe 			inode->dirtied_when = jiffies;
2238a2f48706STheodore Ts'o 			if (dirtytime)
2239a2f48706STheodore Ts'o 				inode->dirtied_time_when = jiffies;
2240d6c10f1fSTejun Heo 
22410e11f644SChristoph Hellwig 			if (inode->i_state & I_DIRTY)
22420747259dSTejun Heo 				dirty_list = &wb->b_dirty;
2243a2f48706STheodore Ts'o 			else
22440747259dSTejun Heo 				dirty_list = &wb->b_dirty_time;
2245d6c10f1fSTejun Heo 
2246c7f54084SDave Chinner 			wakeup_bdi = inode_io_list_move_locked(inode, wb,
2247d6c10f1fSTejun Heo 							       dirty_list);
2248d6c10f1fSTejun Heo 
22490747259dSTejun Heo 			spin_unlock(&wb->list_lock);
22500ae45f63STheodore Ts'o 			trace_writeback_dirty_inode_enqueue(inode);
2251253c34e9SArtem Bityutskiy 
2252d6c10f1fSTejun Heo 			/*
2253d6c10f1fSTejun Heo 			 * If this is the first dirty inode for this bdi,
2254d6c10f1fSTejun Heo 			 * we have to wake-up the corresponding bdi thread
2255d6c10f1fSTejun Heo 			 * to make sure background write-back happens
2256d6c10f1fSTejun Heo 			 * later.
2257d6c10f1fSTejun Heo 			 */
22580747259dSTejun Heo 			if (bdi_cap_writeback_dirty(wb->bdi) && wakeup_bdi)
22590747259dSTejun Heo 				wb_wakeup_delayed(wb);
2260a66979abSDave Chinner 			return;
2261a66979abSDave Chinner 		}
2262a66979abSDave Chinner 	}
2263a66979abSDave Chinner out_unlock_inode:
2264a66979abSDave Chinner 	spin_unlock(&inode->i_lock);
226503ba3782SJens Axboe }
226603ba3782SJens Axboe EXPORT_SYMBOL(__mark_inode_dirty);
226703ba3782SJens Axboe 
2268e97fedb9SDave Chinner /*
2269e97fedb9SDave Chinner  * The @s_sync_lock is used to serialise concurrent sync operations
2270e97fedb9SDave Chinner  * to avoid lock contention problems with concurrent wait_sb_inodes() calls.
2271e97fedb9SDave Chinner  * Concurrent callers will block on the s_sync_lock rather than doing contending
2272e97fedb9SDave Chinner  * walks. The queueing maintains sync(2) required behaviour as all the IO that
2273e97fedb9SDave Chinner  * has been issued up to the time this function is enter is guaranteed to be
2274e97fedb9SDave Chinner  * completed by the time we have gained the lock and waited for all IO that is
2275e97fedb9SDave Chinner  * in progress regardless of the order callers are granted the lock.
2276e97fedb9SDave Chinner  */
2277b6e51316SJens Axboe static void wait_sb_inodes(struct super_block *sb)
227866f3b8e2SJens Axboe {
22796c60d2b5SDave Chinner 	LIST_HEAD(sync_list);
228038f21977SNick Piggin 
228103ba3782SJens Axboe 	/*
228203ba3782SJens Axboe 	 * We need to be protected against the filesystem going from
228303ba3782SJens Axboe 	 * r/o to r/w or vice versa.
228403ba3782SJens Axboe 	 */
2285b6e51316SJens Axboe 	WARN_ON(!rwsem_is_locked(&sb->s_umount));
228603ba3782SJens Axboe 
2287e97fedb9SDave Chinner 	mutex_lock(&sb->s_sync_lock);
228866f3b8e2SJens Axboe 
228938f21977SNick Piggin 	/*
22906c60d2b5SDave Chinner 	 * Splice the writeback list onto a temporary list to avoid waiting on
22916c60d2b5SDave Chinner 	 * inodes that have started writeback after this point.
22926c60d2b5SDave Chinner 	 *
22936c60d2b5SDave Chinner 	 * Use rcu_read_lock() to keep the inodes around until we have a
22946c60d2b5SDave Chinner 	 * reference. s_inode_wblist_lock protects sb->s_inodes_wb as well as
22956c60d2b5SDave Chinner 	 * the local list because inodes can be dropped from either by writeback
22966c60d2b5SDave Chinner 	 * completion.
229738f21977SNick Piggin 	 */
22986c60d2b5SDave Chinner 	rcu_read_lock();
22996c60d2b5SDave Chinner 	spin_lock_irq(&sb->s_inode_wblist_lock);
23006c60d2b5SDave Chinner 	list_splice_init(&sb->s_inodes_wb, &sync_list);
23016c60d2b5SDave Chinner 
23026c60d2b5SDave Chinner 	/*
23036c60d2b5SDave Chinner 	 * Data integrity sync. Must wait for all pages under writeback, because
23046c60d2b5SDave Chinner 	 * there may have been pages dirtied before our sync call, but which had
23056c60d2b5SDave Chinner 	 * writeout started before we write it out.  In which case, the inode
23066c60d2b5SDave Chinner 	 * may not be on the dirty list, but we still have to wait for that
23076c60d2b5SDave Chinner 	 * writeout.
23086c60d2b5SDave Chinner 	 */
23096c60d2b5SDave Chinner 	while (!list_empty(&sync_list)) {
23106c60d2b5SDave Chinner 		struct inode *inode = list_first_entry(&sync_list, struct inode,
23116c60d2b5SDave Chinner 						       i_wb_list);
2312250df6edSDave Chinner 		struct address_space *mapping = inode->i_mapping;
231338f21977SNick Piggin 
23146c60d2b5SDave Chinner 		/*
23156c60d2b5SDave Chinner 		 * Move each inode back to the wb list before we drop the lock
23166c60d2b5SDave Chinner 		 * to preserve consistency between i_wb_list and the mapping
23176c60d2b5SDave Chinner 		 * writeback tag. Writeback completion is responsible to remove
23186c60d2b5SDave Chinner 		 * the inode from either list once the writeback tag is cleared.
23196c60d2b5SDave Chinner 		 */
23206c60d2b5SDave Chinner 		list_move_tail(&inode->i_wb_list, &sb->s_inodes_wb);
23216c60d2b5SDave Chinner 
23226c60d2b5SDave Chinner 		/*
23236c60d2b5SDave Chinner 		 * The mapping can appear untagged while still on-list since we
23246c60d2b5SDave Chinner 		 * do not have the mapping lock. Skip it here, wb completion
23256c60d2b5SDave Chinner 		 * will remove it.
23266c60d2b5SDave Chinner 		 */
23276c60d2b5SDave Chinner 		if (!mapping_tagged(mapping, PAGECACHE_TAG_WRITEBACK))
23286c60d2b5SDave Chinner 			continue;
23296c60d2b5SDave Chinner 
23306c60d2b5SDave Chinner 		spin_unlock_irq(&sb->s_inode_wblist_lock);
23316c60d2b5SDave Chinner 
2332250df6edSDave Chinner 		spin_lock(&inode->i_lock);
23336c60d2b5SDave Chinner 		if (inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW)) {
2334250df6edSDave Chinner 			spin_unlock(&inode->i_lock);
23356c60d2b5SDave Chinner 
23366c60d2b5SDave Chinner 			spin_lock_irq(&sb->s_inode_wblist_lock);
233738f21977SNick Piggin 			continue;
2338250df6edSDave Chinner 		}
233938f21977SNick Piggin 		__iget(inode);
2340250df6edSDave Chinner 		spin_unlock(&inode->i_lock);
23416c60d2b5SDave Chinner 		rcu_read_unlock();
234238f21977SNick Piggin 
2343aa750fd7SJunichi Nomura 		/*
2344aa750fd7SJunichi Nomura 		 * We keep the error status of individual mapping so that
2345aa750fd7SJunichi Nomura 		 * applications can catch the writeback error using fsync(2).
2346aa750fd7SJunichi Nomura 		 * See filemap_fdatawait_keep_errors() for details.
2347aa750fd7SJunichi Nomura 		 */
2348aa750fd7SJunichi Nomura 		filemap_fdatawait_keep_errors(mapping);
234938f21977SNick Piggin 
235038f21977SNick Piggin 		cond_resched();
235138f21977SNick Piggin 
23526c60d2b5SDave Chinner 		iput(inode);
23536c60d2b5SDave Chinner 
23546c60d2b5SDave Chinner 		rcu_read_lock();
23556c60d2b5SDave Chinner 		spin_lock_irq(&sb->s_inode_wblist_lock);
235638f21977SNick Piggin 	}
23576c60d2b5SDave Chinner 	spin_unlock_irq(&sb->s_inode_wblist_lock);
23586c60d2b5SDave Chinner 	rcu_read_unlock();
2359e97fedb9SDave Chinner 	mutex_unlock(&sb->s_sync_lock);
236066f3b8e2SJens Axboe }
23611da177e4SLinus Torvalds 
2362f30a7d0cSTejun Heo static void __writeback_inodes_sb_nr(struct super_block *sb, unsigned long nr,
2363f30a7d0cSTejun Heo 				     enum wb_reason reason, bool skip_if_busy)
23641da177e4SLinus Torvalds {
2365cc395d7fSTejun Heo 	DEFINE_WB_COMPLETION_ONSTACK(done);
236683ba7b07SChristoph Hellwig 	struct wb_writeback_work work = {
23673c4d7165SChristoph Hellwig 		.sb			= sb,
23683c4d7165SChristoph Hellwig 		.sync_mode		= WB_SYNC_NONE,
23696e6938b6SWu Fengguang 		.tagged_writepages	= 1,
237083ba7b07SChristoph Hellwig 		.done			= &done,
23713259f8beSChris Mason 		.nr_pages		= nr,
23720e175a18SCurt Wohlgemuth 		.reason			= reason,
23733c4d7165SChristoph Hellwig 	};
2374e7972912STejun Heo 	struct backing_dev_info *bdi = sb->s_bdi;
23750e3c9a22SJens Axboe 
2376e7972912STejun Heo 	if (!bdi_has_dirty_io(bdi) || bdi == &noop_backing_dev_info)
23776eedc701SJan Kara 		return;
2378cf37e972SChristoph Hellwig 	WARN_ON(!rwsem_is_locked(&sb->s_umount));
2379f30a7d0cSTejun Heo 
2380db125360STejun Heo 	bdi_split_work_to_wbs(sb->s_bdi, &work, skip_if_busy);
2381cc395d7fSTejun Heo 	wb_wait_for_completion(bdi, &done);
23821da177e4SLinus Torvalds }
2383f30a7d0cSTejun Heo 
2384f30a7d0cSTejun Heo /**
2385f30a7d0cSTejun Heo  * writeback_inodes_sb_nr -	writeback dirty inodes from given super_block
2386f30a7d0cSTejun Heo  * @sb: the superblock
2387f30a7d0cSTejun Heo  * @nr: the number of pages to write
2388f30a7d0cSTejun Heo  * @reason: reason why some writeback work initiated
2389f30a7d0cSTejun Heo  *
2390f30a7d0cSTejun Heo  * Start writeback on some inodes on this super_block. No guarantees are made
2391f30a7d0cSTejun Heo  * on how many (if any) will be written, and this function does not wait
2392f30a7d0cSTejun Heo  * for IO completion of submitted IO.
2393f30a7d0cSTejun Heo  */
2394f30a7d0cSTejun Heo void writeback_inodes_sb_nr(struct super_block *sb,
2395f30a7d0cSTejun Heo 			    unsigned long nr,
2396f30a7d0cSTejun Heo 			    enum wb_reason reason)
2397f30a7d0cSTejun Heo {
2398f30a7d0cSTejun Heo 	__writeback_inodes_sb_nr(sb, nr, reason, false);
2399f30a7d0cSTejun Heo }
24003259f8beSChris Mason EXPORT_SYMBOL(writeback_inodes_sb_nr);
24013259f8beSChris Mason 
24023259f8beSChris Mason /**
24033259f8beSChris Mason  * writeback_inodes_sb	-	writeback dirty inodes from given super_block
24043259f8beSChris Mason  * @sb: the superblock
2405786228abSMarcos Paulo de Souza  * @reason: reason why some writeback work was initiated
24063259f8beSChris Mason  *
24073259f8beSChris Mason  * Start writeback on some inodes on this super_block. No guarantees are made
24083259f8beSChris Mason  * on how many (if any) will be written, and this function does not wait
24093259f8beSChris Mason  * for IO completion of submitted IO.
24103259f8beSChris Mason  */
24110e175a18SCurt Wohlgemuth void writeback_inodes_sb(struct super_block *sb, enum wb_reason reason)
24123259f8beSChris Mason {
24130e175a18SCurt Wohlgemuth 	return writeback_inodes_sb_nr(sb, get_nr_dirty_pages(), reason);
24143259f8beSChris Mason }
2415d8a8559cSJens Axboe EXPORT_SYMBOL(writeback_inodes_sb);
2416d8a8559cSJens Axboe 
2417d8a8559cSJens Axboe /**
241810ee27a0SMiao Xie  * try_to_writeback_inodes_sb - try to start writeback if none underway
241910ee27a0SMiao Xie  * @sb: the superblock
242010ee27a0SMiao Xie  * @reason: reason why some writeback work was initiated
242110ee27a0SMiao Xie  *
24228264c321SRakesh Pandit  * Invoke __writeback_inodes_sb_nr if no writeback is currently underway.
242310ee27a0SMiao Xie  */
24248264c321SRakesh Pandit void try_to_writeback_inodes_sb(struct super_block *sb, enum wb_reason reason)
242510ee27a0SMiao Xie {
24268264c321SRakesh Pandit 	if (!down_read_trylock(&sb->s_umount))
24278264c321SRakesh Pandit 		return;
24288264c321SRakesh Pandit 
24298264c321SRakesh Pandit 	__writeback_inodes_sb_nr(sb, get_nr_dirty_pages(), reason, true);
24308264c321SRakesh Pandit 	up_read(&sb->s_umount);
243110ee27a0SMiao Xie }
243210ee27a0SMiao Xie EXPORT_SYMBOL(try_to_writeback_inodes_sb);
24333259f8beSChris Mason 
24343259f8beSChris Mason /**
2435d8a8559cSJens Axboe  * sync_inodes_sb	-	sync sb inode pages
2436d8a8559cSJens Axboe  * @sb: the superblock
2437d8a8559cSJens Axboe  *
2438d8a8559cSJens Axboe  * This function writes and waits on any dirty inode belonging to this
24390dc83bd3SJan Kara  * super_block.
2440d8a8559cSJens Axboe  */
24410dc83bd3SJan Kara void sync_inodes_sb(struct super_block *sb)
2442d8a8559cSJens Axboe {
2443cc395d7fSTejun Heo 	DEFINE_WB_COMPLETION_ONSTACK(done);
244483ba7b07SChristoph Hellwig 	struct wb_writeback_work work = {
24453c4d7165SChristoph Hellwig 		.sb		= sb,
24463c4d7165SChristoph Hellwig 		.sync_mode	= WB_SYNC_ALL,
24473c4d7165SChristoph Hellwig 		.nr_pages	= LONG_MAX,
24483c4d7165SChristoph Hellwig 		.range_cyclic	= 0,
244983ba7b07SChristoph Hellwig 		.done		= &done,
24500e175a18SCurt Wohlgemuth 		.reason		= WB_REASON_SYNC,
24517747bd4bSDave Chinner 		.for_sync	= 1,
24523c4d7165SChristoph Hellwig 	};
2453e7972912STejun Heo 	struct backing_dev_info *bdi = sb->s_bdi;
24543c4d7165SChristoph Hellwig 
2455006a0973STejun Heo 	/*
2456006a0973STejun Heo 	 * Can't skip on !bdi_has_dirty() because we should wait for !dirty
2457006a0973STejun Heo 	 * inodes under writeback and I_DIRTY_TIME inodes ignored by
2458006a0973STejun Heo 	 * bdi_has_dirty() need to be written out too.
2459006a0973STejun Heo 	 */
2460006a0973STejun Heo 	if (bdi == &noop_backing_dev_info)
24616eedc701SJan Kara 		return;
2462cf37e972SChristoph Hellwig 	WARN_ON(!rwsem_is_locked(&sb->s_umount));
2463cf37e972SChristoph Hellwig 
24647fc5854fSTejun Heo 	/* protect against inode wb switch, see inode_switch_wbs_work_fn() */
24657fc5854fSTejun Heo 	bdi_down_write_wb_switch_rwsem(bdi);
2466db125360STejun Heo 	bdi_split_work_to_wbs(bdi, &work, false);
2467cc395d7fSTejun Heo 	wb_wait_for_completion(bdi, &done);
24687fc5854fSTejun Heo 	bdi_up_write_wb_switch_rwsem(bdi);
246983ba7b07SChristoph Hellwig 
2470b6e51316SJens Axboe 	wait_sb_inodes(sb);
2471d8a8559cSJens Axboe }
2472d8a8559cSJens Axboe EXPORT_SYMBOL(sync_inodes_sb);
24731da177e4SLinus Torvalds 
24741da177e4SLinus Torvalds /**
24751da177e4SLinus Torvalds  * write_inode_now	-	write an inode to disk
24761da177e4SLinus Torvalds  * @inode: inode to write to disk
24771da177e4SLinus Torvalds  * @sync: whether the write should be synchronous or not
24781da177e4SLinus Torvalds  *
24797f04c26dSAndrea Arcangeli  * This function commits an inode to disk immediately if it is dirty. This is
24807f04c26dSAndrea Arcangeli  * primarily needed by knfsd.
24817f04c26dSAndrea Arcangeli  *
24827f04c26dSAndrea Arcangeli  * The caller must either have a ref on the inode or must have set I_WILL_FREE.
24831da177e4SLinus Torvalds  */
24841da177e4SLinus Torvalds int write_inode_now(struct inode *inode, int sync)
24851da177e4SLinus Torvalds {
24861da177e4SLinus Torvalds 	struct writeback_control wbc = {
24871da177e4SLinus Torvalds 		.nr_to_write = LONG_MAX,
248818914b18SMike Galbraith 		.sync_mode = sync ? WB_SYNC_ALL : WB_SYNC_NONE,
2489111ebb6eSOGAWA Hirofumi 		.range_start = 0,
2490111ebb6eSOGAWA Hirofumi 		.range_end = LLONG_MAX,
24911da177e4SLinus Torvalds 	};
24921da177e4SLinus Torvalds 
24931da177e4SLinus Torvalds 	if (!mapping_cap_writeback_dirty(inode->i_mapping))
249449364ce2SAndrew Morton 		wbc.nr_to_write = 0;
24951da177e4SLinus Torvalds 
24961da177e4SLinus Torvalds 	might_sleep();
2497aaf25593STejun Heo 	return writeback_single_inode(inode, &wbc);
24981da177e4SLinus Torvalds }
24991da177e4SLinus Torvalds EXPORT_SYMBOL(write_inode_now);
25001da177e4SLinus Torvalds 
25011da177e4SLinus Torvalds /**
25021da177e4SLinus Torvalds  * sync_inode - write an inode and its pages to disk.
25031da177e4SLinus Torvalds  * @inode: the inode to sync
25041da177e4SLinus Torvalds  * @wbc: controls the writeback mode
25051da177e4SLinus Torvalds  *
25061da177e4SLinus Torvalds  * sync_inode() will write an inode and its pages to disk.  It will also
25071da177e4SLinus Torvalds  * correctly update the inode on its superblock's dirty inode lists and will
25081da177e4SLinus Torvalds  * update inode->i_state.
25091da177e4SLinus Torvalds  *
25101da177e4SLinus Torvalds  * The caller must have a ref on the inode.
25111da177e4SLinus Torvalds  */
25121da177e4SLinus Torvalds int sync_inode(struct inode *inode, struct writeback_control *wbc)
25131da177e4SLinus Torvalds {
2514aaf25593STejun Heo 	return writeback_single_inode(inode, wbc);
25151da177e4SLinus Torvalds }
25161da177e4SLinus Torvalds EXPORT_SYMBOL(sync_inode);
2517c3765016SChristoph Hellwig 
2518c3765016SChristoph Hellwig /**
2519c691b9d9SAndrew Morton  * sync_inode_metadata - write an inode to disk
2520c3765016SChristoph Hellwig  * @inode: the inode to sync
2521c3765016SChristoph Hellwig  * @wait: wait for I/O to complete.
2522c3765016SChristoph Hellwig  *
2523c691b9d9SAndrew Morton  * Write an inode to disk and adjust its dirty state after completion.
2524c3765016SChristoph Hellwig  *
2525c3765016SChristoph Hellwig  * Note: only writes the actual inode, no associated data or other metadata.
2526c3765016SChristoph Hellwig  */
2527c3765016SChristoph Hellwig int sync_inode_metadata(struct inode *inode, int wait)
2528c3765016SChristoph Hellwig {
2529c3765016SChristoph Hellwig 	struct writeback_control wbc = {
2530c3765016SChristoph Hellwig 		.sync_mode = wait ? WB_SYNC_ALL : WB_SYNC_NONE,
2531c3765016SChristoph Hellwig 		.nr_to_write = 0, /* metadata-only */
2532c3765016SChristoph Hellwig 	};
2533c3765016SChristoph Hellwig 
2534c3765016SChristoph Hellwig 	return sync_inode(inode, &wbc);
2535c3765016SChristoph Hellwig }
2536c3765016SChristoph Hellwig EXPORT_SYMBOL(sync_inode_metadata);
2537