xref: /openbmc/linux/fs/fs-writeback.c (revision cc395d7f)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * fs/fs-writeback.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Copyright (C) 2002, Linus Torvalds.
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  * Contains all the functions related to writing back and waiting
71da177e4SLinus Torvalds  * upon dirty inodes against superblocks, and writing back dirty
81da177e4SLinus Torvalds  * pages against inodes.  ie: data writeback.  Writeout of the
91da177e4SLinus Torvalds  * inode itself is not handled here.
101da177e4SLinus Torvalds  *
11e1f8e874SFrancois Cami  * 10Apr2002	Andrew Morton
121da177e4SLinus Torvalds  *		Split out of fs/inode.c
131da177e4SLinus Torvalds  *		Additions for address_space-based writeback
141da177e4SLinus Torvalds  */
151da177e4SLinus Torvalds 
161da177e4SLinus Torvalds #include <linux/kernel.h>
17630d9c47SPaul Gortmaker #include <linux/export.h>
181da177e4SLinus Torvalds #include <linux/spinlock.h>
195a0e3ad6STejun Heo #include <linux/slab.h>
201da177e4SLinus Torvalds #include <linux/sched.h>
211da177e4SLinus Torvalds #include <linux/fs.h>
221da177e4SLinus Torvalds #include <linux/mm.h>
23bc31b86aSWu Fengguang #include <linux/pagemap.h>
2403ba3782SJens Axboe #include <linux/kthread.h>
251da177e4SLinus Torvalds #include <linux/writeback.h>
261da177e4SLinus Torvalds #include <linux/blkdev.h>
271da177e4SLinus Torvalds #include <linux/backing-dev.h>
28455b2864SDave Chinner #include <linux/tracepoint.h>
29719ea2fbSAl Viro #include <linux/device.h>
3007f3f05cSDavid Howells #include "internal.h"
311da177e4SLinus Torvalds 
32d0bceac7SJens Axboe /*
33bc31b86aSWu Fengguang  * 4MB minimal write chunk size
34bc31b86aSWu Fengguang  */
35bc31b86aSWu Fengguang #define MIN_WRITEBACK_PAGES	(4096UL >> (PAGE_CACHE_SHIFT - 10))
36bc31b86aSWu Fengguang 
37cc395d7fSTejun Heo struct wb_completion {
38cc395d7fSTejun Heo 	atomic_t		cnt;
39cc395d7fSTejun Heo };
40cc395d7fSTejun Heo 
41bc31b86aSWu Fengguang /*
42c4a77a6cSJens Axboe  * Passed into wb_writeback(), essentially a subset of writeback_control
43c4a77a6cSJens Axboe  */
4483ba7b07SChristoph Hellwig struct wb_writeback_work {
45c4a77a6cSJens Axboe 	long nr_pages;
46c4a77a6cSJens Axboe 	struct super_block *sb;
470dc83bd3SJan Kara 	unsigned long *older_than_this;
48c4a77a6cSJens Axboe 	enum writeback_sync_modes sync_mode;
496e6938b6SWu Fengguang 	unsigned int tagged_writepages:1;
5052957fe1SH Hartley Sweeten 	unsigned int for_kupdate:1;
5152957fe1SH Hartley Sweeten 	unsigned int range_cyclic:1;
5252957fe1SH Hartley Sweeten 	unsigned int for_background:1;
537747bd4bSDave Chinner 	unsigned int for_sync:1;	/* sync(2) WB_SYNC_ALL writeback */
54ac7b19a3STejun Heo 	unsigned int auto_free:1;	/* free on completion */
550e175a18SCurt Wohlgemuth 	enum wb_reason reason;		/* why was writeback initiated? */
56c4a77a6cSJens Axboe 
578010c3b6SJens Axboe 	struct list_head list;		/* pending work list */
58cc395d7fSTejun Heo 	struct wb_completion *done;	/* set if the caller waits */
5903ba3782SJens Axboe };
6003ba3782SJens Axboe 
61a2f48706STheodore Ts'o /*
62cc395d7fSTejun Heo  * If one wants to wait for one or more wb_writeback_works, each work's
63cc395d7fSTejun Heo  * ->done should be set to a wb_completion defined using the following
64cc395d7fSTejun Heo  * macro.  Once all work items are issued with wb_queue_work(), the caller
65cc395d7fSTejun Heo  * can wait for the completion of all using wb_wait_for_completion().  Work
66cc395d7fSTejun Heo  * items which are waited upon aren't freed automatically on completion.
67cc395d7fSTejun Heo  */
68cc395d7fSTejun Heo #define DEFINE_WB_COMPLETION_ONSTACK(cmpl)				\
69cc395d7fSTejun Heo 	struct wb_completion cmpl = {					\
70cc395d7fSTejun Heo 		.cnt		= ATOMIC_INIT(1),			\
71cc395d7fSTejun Heo 	}
72cc395d7fSTejun Heo 
73cc395d7fSTejun Heo 
74cc395d7fSTejun Heo /*
75a2f48706STheodore Ts'o  * If an inode is constantly having its pages dirtied, but then the
76a2f48706STheodore Ts'o  * updates stop dirtytime_expire_interval seconds in the past, it's
77a2f48706STheodore Ts'o  * possible for the worst case time between when an inode has its
78a2f48706STheodore Ts'o  * timestamps updated and when they finally get written out to be two
79a2f48706STheodore Ts'o  * dirtytime_expire_intervals.  We set the default to 12 hours (in
80a2f48706STheodore Ts'o  * seconds), which means most of the time inodes will have their
81a2f48706STheodore Ts'o  * timestamps written to disk after 12 hours, but in the worst case a
82a2f48706STheodore Ts'o  * few inodes might not their timestamps updated for 24 hours.
83a2f48706STheodore Ts'o  */
84a2f48706STheodore Ts'o unsigned int dirtytime_expire_interval = 12 * 60 * 60;
85a2f48706STheodore Ts'o 
867ccf19a8SNick Piggin static inline struct inode *wb_inode(struct list_head *head)
877ccf19a8SNick Piggin {
887ccf19a8SNick Piggin 	return list_entry(head, struct inode, i_wb_list);
897ccf19a8SNick Piggin }
907ccf19a8SNick Piggin 
9115eb77a0SWu Fengguang /*
9215eb77a0SWu Fengguang  * Include the creation of the trace points after defining the
9315eb77a0SWu Fengguang  * wb_writeback_work structure and inline functions so that the definition
9415eb77a0SWu Fengguang  * remains local to this file.
9515eb77a0SWu Fengguang  */
9615eb77a0SWu Fengguang #define CREATE_TRACE_POINTS
9715eb77a0SWu Fengguang #include <trace/events/writeback.h>
9815eb77a0SWu Fengguang 
99774016b2SSteven Whitehouse EXPORT_TRACEPOINT_SYMBOL_GPL(wbc_writepage);
100774016b2SSteven Whitehouse 
101d6c10f1fSTejun Heo static bool wb_io_lists_populated(struct bdi_writeback *wb)
102d6c10f1fSTejun Heo {
103d6c10f1fSTejun Heo 	if (wb_has_dirty_io(wb)) {
104d6c10f1fSTejun Heo 		return false;
105d6c10f1fSTejun Heo 	} else {
106d6c10f1fSTejun Heo 		set_bit(WB_has_dirty_io, &wb->state);
10795a46c65STejun Heo 		WARN_ON_ONCE(!wb->avg_write_bandwidth);
108766a9d6eSTejun Heo 		atomic_long_add(wb->avg_write_bandwidth,
109766a9d6eSTejun Heo 				&wb->bdi->tot_write_bandwidth);
110d6c10f1fSTejun Heo 		return true;
111d6c10f1fSTejun Heo 	}
112d6c10f1fSTejun Heo }
113d6c10f1fSTejun Heo 
114d6c10f1fSTejun Heo static void wb_io_lists_depopulated(struct bdi_writeback *wb)
115d6c10f1fSTejun Heo {
116d6c10f1fSTejun Heo 	if (wb_has_dirty_io(wb) && list_empty(&wb->b_dirty) &&
117766a9d6eSTejun Heo 	    list_empty(&wb->b_io) && list_empty(&wb->b_more_io)) {
118d6c10f1fSTejun Heo 		clear_bit(WB_has_dirty_io, &wb->state);
11995a46c65STejun Heo 		WARN_ON_ONCE(atomic_long_sub_return(wb->avg_write_bandwidth,
12095a46c65STejun Heo 					&wb->bdi->tot_write_bandwidth) < 0);
121766a9d6eSTejun Heo 	}
122d6c10f1fSTejun Heo }
123d6c10f1fSTejun Heo 
124d6c10f1fSTejun Heo /**
125d6c10f1fSTejun Heo  * inode_wb_list_move_locked - move an inode onto a bdi_writeback IO list
126d6c10f1fSTejun Heo  * @inode: inode to be moved
127d6c10f1fSTejun Heo  * @wb: target bdi_writeback
128d6c10f1fSTejun Heo  * @head: one of @wb->b_{dirty|io|more_io}
129d6c10f1fSTejun Heo  *
130d6c10f1fSTejun Heo  * Move @inode->i_wb_list to @list of @wb and set %WB_has_dirty_io.
131d6c10f1fSTejun Heo  * Returns %true if @inode is the first occupant of the !dirty_time IO
132d6c10f1fSTejun Heo  * lists; otherwise, %false.
133d6c10f1fSTejun Heo  */
134d6c10f1fSTejun Heo static bool inode_wb_list_move_locked(struct inode *inode,
135d6c10f1fSTejun Heo 				      struct bdi_writeback *wb,
136d6c10f1fSTejun Heo 				      struct list_head *head)
137d6c10f1fSTejun Heo {
138d6c10f1fSTejun Heo 	assert_spin_locked(&wb->list_lock);
139d6c10f1fSTejun Heo 
140d6c10f1fSTejun Heo 	list_move(&inode->i_wb_list, head);
141d6c10f1fSTejun Heo 
142d6c10f1fSTejun Heo 	/* dirty_time doesn't count as dirty_io until expiration */
143d6c10f1fSTejun Heo 	if (head != &wb->b_dirty_time)
144d6c10f1fSTejun Heo 		return wb_io_lists_populated(wb);
145d6c10f1fSTejun Heo 
146d6c10f1fSTejun Heo 	wb_io_lists_depopulated(wb);
147d6c10f1fSTejun Heo 	return false;
148d6c10f1fSTejun Heo }
149d6c10f1fSTejun Heo 
150d6c10f1fSTejun Heo /**
151d6c10f1fSTejun Heo  * inode_wb_list_del_locked - remove an inode from its bdi_writeback IO list
152d6c10f1fSTejun Heo  * @inode: inode to be removed
153d6c10f1fSTejun Heo  * @wb: bdi_writeback @inode is being removed from
154d6c10f1fSTejun Heo  *
155d6c10f1fSTejun Heo  * Remove @inode which may be on one of @wb->b_{dirty|io|more_io} lists and
156d6c10f1fSTejun Heo  * clear %WB_has_dirty_io if all are empty afterwards.
157d6c10f1fSTejun Heo  */
158d6c10f1fSTejun Heo static void inode_wb_list_del_locked(struct inode *inode,
159d6c10f1fSTejun Heo 				     struct bdi_writeback *wb)
160d6c10f1fSTejun Heo {
161d6c10f1fSTejun Heo 	assert_spin_locked(&wb->list_lock);
162d6c10f1fSTejun Heo 
163d6c10f1fSTejun Heo 	list_del_init(&inode->i_wb_list);
164d6c10f1fSTejun Heo 	wb_io_lists_depopulated(wb);
165d6c10f1fSTejun Heo }
166d6c10f1fSTejun Heo 
167f0054bb1STejun Heo static void wb_wakeup(struct bdi_writeback *wb)
1685acda9d1SJan Kara {
169f0054bb1STejun Heo 	spin_lock_bh(&wb->work_lock);
170f0054bb1STejun Heo 	if (test_bit(WB_registered, &wb->state))
171f0054bb1STejun Heo 		mod_delayed_work(bdi_wq, &wb->dwork, 0);
172f0054bb1STejun Heo 	spin_unlock_bh(&wb->work_lock);
1735acda9d1SJan Kara }
1745acda9d1SJan Kara 
175f0054bb1STejun Heo static void wb_queue_work(struct bdi_writeback *wb,
1766585027aSJan Kara 			  struct wb_writeback_work *work)
1776585027aSJan Kara {
178f0054bb1STejun Heo 	trace_writeback_queue(wb->bdi, work);
1796585027aSJan Kara 
180f0054bb1STejun Heo 	spin_lock_bh(&wb->work_lock);
181cc395d7fSTejun Heo 	if (!test_bit(WB_registered, &wb->state))
1825acda9d1SJan Kara 		goto out_unlock;
183cc395d7fSTejun Heo 	if (work->done)
184cc395d7fSTejun Heo 		atomic_inc(&work->done->cnt);
185f0054bb1STejun Heo 	list_add_tail(&work->list, &wb->work_list);
186f0054bb1STejun Heo 	mod_delayed_work(bdi_wq, &wb->dwork, 0);
1875acda9d1SJan Kara out_unlock:
188f0054bb1STejun Heo 	spin_unlock_bh(&wb->work_lock);
18903ba3782SJens Axboe }
1901da177e4SLinus Torvalds 
191cc395d7fSTejun Heo /**
192cc395d7fSTejun Heo  * wb_wait_for_completion - wait for completion of bdi_writeback_works
193cc395d7fSTejun Heo  * @bdi: bdi work items were issued to
194cc395d7fSTejun Heo  * @done: target wb_completion
195cc395d7fSTejun Heo  *
196cc395d7fSTejun Heo  * Wait for one or more work items issued to @bdi with their ->done field
197cc395d7fSTejun Heo  * set to @done, which should have been defined with
198cc395d7fSTejun Heo  * DEFINE_WB_COMPLETION_ONSTACK().  This function returns after all such
199cc395d7fSTejun Heo  * work items are completed.  Work items which are waited upon aren't freed
200cc395d7fSTejun Heo  * automatically on completion.
201cc395d7fSTejun Heo  */
202cc395d7fSTejun Heo static void wb_wait_for_completion(struct backing_dev_info *bdi,
203cc395d7fSTejun Heo 				   struct wb_completion *done)
204cc395d7fSTejun Heo {
205cc395d7fSTejun Heo 	atomic_dec(&done->cnt);		/* put down the initial count */
206cc395d7fSTejun Heo 	wait_event(bdi->wb_waitq, !atomic_read(&done->cnt));
207cc395d7fSTejun Heo }
208cc395d7fSTejun Heo 
209703c2708STejun Heo #ifdef CONFIG_CGROUP_WRITEBACK
210703c2708STejun Heo 
211703c2708STejun Heo /**
212703c2708STejun Heo  * inode_congested - test whether an inode is congested
213703c2708STejun Heo  * @inode: inode to test for congestion
214703c2708STejun Heo  * @cong_bits: mask of WB_[a]sync_congested bits to test
215703c2708STejun Heo  *
216703c2708STejun Heo  * Tests whether @inode is congested.  @cong_bits is the mask of congestion
217703c2708STejun Heo  * bits to test and the return value is the mask of set bits.
218703c2708STejun Heo  *
219703c2708STejun Heo  * If cgroup writeback is enabled for @inode, the congestion state is
220703c2708STejun Heo  * determined by whether the cgwb (cgroup bdi_writeback) for the blkcg
221703c2708STejun Heo  * associated with @inode is congested; otherwise, the root wb's congestion
222703c2708STejun Heo  * state is used.
223703c2708STejun Heo  */
224703c2708STejun Heo int inode_congested(struct inode *inode, int cong_bits)
225703c2708STejun Heo {
226703c2708STejun Heo 	if (inode) {
227703c2708STejun Heo 		struct bdi_writeback *wb = inode_to_wb(inode);
228703c2708STejun Heo 		if (wb)
229703c2708STejun Heo 			return wb_congested(wb, cong_bits);
230703c2708STejun Heo 	}
231703c2708STejun Heo 
232703c2708STejun Heo 	return wb_congested(&inode_to_bdi(inode)->wb, cong_bits);
233703c2708STejun Heo }
234703c2708STejun Heo EXPORT_SYMBOL_GPL(inode_congested);
235703c2708STejun Heo 
236f2b65121STejun Heo /**
237f2b65121STejun Heo  * wb_split_bdi_pages - split nr_pages to write according to bandwidth
238f2b65121STejun Heo  * @wb: target bdi_writeback to split @nr_pages to
239f2b65121STejun Heo  * @nr_pages: number of pages to write for the whole bdi
240f2b65121STejun Heo  *
241f2b65121STejun Heo  * Split @wb's portion of @nr_pages according to @wb's write bandwidth in
242f2b65121STejun Heo  * relation to the total write bandwidth of all wb's w/ dirty inodes on
243f2b65121STejun Heo  * @wb->bdi.
244f2b65121STejun Heo  */
245f2b65121STejun Heo static long wb_split_bdi_pages(struct bdi_writeback *wb, long nr_pages)
246f2b65121STejun Heo {
247f2b65121STejun Heo 	unsigned long this_bw = wb->avg_write_bandwidth;
248f2b65121STejun Heo 	unsigned long tot_bw = atomic_long_read(&wb->bdi->tot_write_bandwidth);
249f2b65121STejun Heo 
250f2b65121STejun Heo 	if (nr_pages == LONG_MAX)
251f2b65121STejun Heo 		return LONG_MAX;
252f2b65121STejun Heo 
253f2b65121STejun Heo 	/*
254f2b65121STejun Heo 	 * This may be called on clean wb's and proportional distribution
255f2b65121STejun Heo 	 * may not make sense, just use the original @nr_pages in those
256f2b65121STejun Heo 	 * cases.  In general, we wanna err on the side of writing more.
257f2b65121STejun Heo 	 */
258f2b65121STejun Heo 	if (!tot_bw || this_bw >= tot_bw)
259f2b65121STejun Heo 		return nr_pages;
260f2b65121STejun Heo 	else
261f2b65121STejun Heo 		return DIV_ROUND_UP_ULL((u64)nr_pages * this_bw, tot_bw);
262f2b65121STejun Heo }
263f2b65121STejun Heo 
264f2b65121STejun Heo #else	/* CONFIG_CGROUP_WRITEBACK */
265f2b65121STejun Heo 
266f2b65121STejun Heo static long wb_split_bdi_pages(struct bdi_writeback *wb, long nr_pages)
267f2b65121STejun Heo {
268f2b65121STejun Heo 	return nr_pages;
269f2b65121STejun Heo }
270f2b65121STejun Heo 
271703c2708STejun Heo #endif	/* CONFIG_CGROUP_WRITEBACK */
272703c2708STejun Heo 
273c00ddad3STejun Heo void wb_start_writeback(struct bdi_writeback *wb, long nr_pages,
274c00ddad3STejun Heo 			bool range_cyclic, enum wb_reason reason)
275b6e51316SJens Axboe {
276c00ddad3STejun Heo 	struct wb_writeback_work *work;
277c00ddad3STejun Heo 
278c00ddad3STejun Heo 	if (!wb_has_dirty_io(wb))
279c00ddad3STejun Heo 		return;
280c00ddad3STejun Heo 
281c00ddad3STejun Heo 	/*
282c00ddad3STejun Heo 	 * This is WB_SYNC_NONE writeback, so if allocation fails just
283c00ddad3STejun Heo 	 * wakeup the thread for old dirty data writeback
284c00ddad3STejun Heo 	 */
285c00ddad3STejun Heo 	work = kzalloc(sizeof(*work), GFP_ATOMIC);
286c00ddad3STejun Heo 	if (!work) {
287c00ddad3STejun Heo 		trace_writeback_nowork(wb->bdi);
288c00ddad3STejun Heo 		wb_wakeup(wb);
289c00ddad3STejun Heo 		return;
290c00ddad3STejun Heo 	}
291c00ddad3STejun Heo 
292c00ddad3STejun Heo 	work->sync_mode	= WB_SYNC_NONE;
293c00ddad3STejun Heo 	work->nr_pages	= nr_pages;
294c00ddad3STejun Heo 	work->range_cyclic = range_cyclic;
295c00ddad3STejun Heo 	work->reason	= reason;
296ac7b19a3STejun Heo 	work->auto_free	= 1;
297c00ddad3STejun Heo 
298c00ddad3STejun Heo 	wb_queue_work(wb, work);
299d3ddec76SWu Fengguang }
300d3ddec76SWu Fengguang 
301c5444198SChristoph Hellwig /**
3029ecf4866STejun Heo  * wb_start_background_writeback - start background writeback
3039ecf4866STejun Heo  * @wb: bdi_writback to write from
304c5444198SChristoph Hellwig  *
305c5444198SChristoph Hellwig  * Description:
3066585027aSJan Kara  *   This makes sure WB_SYNC_NONE background writeback happens. When
3079ecf4866STejun Heo  *   this function returns, it is only guaranteed that for given wb
3086585027aSJan Kara  *   some IO is happening if we are over background dirty threshold.
3096585027aSJan Kara  *   Caller need not hold sb s_umount semaphore.
310c5444198SChristoph Hellwig  */
3119ecf4866STejun Heo void wb_start_background_writeback(struct bdi_writeback *wb)
312c5444198SChristoph Hellwig {
3136585027aSJan Kara 	/*
3146585027aSJan Kara 	 * We just wake up the flusher thread. It will perform background
3156585027aSJan Kara 	 * writeback as soon as there is no other work to do.
3166585027aSJan Kara 	 */
3179ecf4866STejun Heo 	trace_writeback_wake_background(wb->bdi);
3189ecf4866STejun Heo 	wb_wakeup(wb);
3191da177e4SLinus Torvalds }
3201da177e4SLinus Torvalds 
3211da177e4SLinus Torvalds /*
322a66979abSDave Chinner  * Remove the inode from the writeback list it is on.
323a66979abSDave Chinner  */
324a66979abSDave Chinner void inode_wb_list_del(struct inode *inode)
325a66979abSDave Chinner {
32652ebea74STejun Heo 	struct bdi_writeback *wb = inode_to_wb(inode);
327a66979abSDave Chinner 
32852ebea74STejun Heo 	spin_lock(&wb->list_lock);
329d6c10f1fSTejun Heo 	inode_wb_list_del_locked(inode, wb);
33052ebea74STejun Heo 	spin_unlock(&wb->list_lock);
331f758eeabSChristoph Hellwig }
332a66979abSDave Chinner 
333a66979abSDave Chinner /*
3346610a0bcSAndrew Morton  * Redirty an inode: set its when-it-was dirtied timestamp and move it to the
3356610a0bcSAndrew Morton  * furthest end of its superblock's dirty-inode list.
3366610a0bcSAndrew Morton  *
3376610a0bcSAndrew Morton  * Before stamping the inode's ->dirtied_when, we check to see whether it is
33866f3b8e2SJens Axboe  * already the most-recently-dirtied inode on the b_dirty list.  If that is
3396610a0bcSAndrew Morton  * the case then the inode must have been redirtied while it was being written
3406610a0bcSAndrew Morton  * out and we don't reset its dirtied_when.
3416610a0bcSAndrew Morton  */
342f758eeabSChristoph Hellwig static void redirty_tail(struct inode *inode, struct bdi_writeback *wb)
3436610a0bcSAndrew Morton {
34403ba3782SJens Axboe 	if (!list_empty(&wb->b_dirty)) {
34566f3b8e2SJens Axboe 		struct inode *tail;
3466610a0bcSAndrew Morton 
3477ccf19a8SNick Piggin 		tail = wb_inode(wb->b_dirty.next);
34866f3b8e2SJens Axboe 		if (time_before(inode->dirtied_when, tail->dirtied_when))
3496610a0bcSAndrew Morton 			inode->dirtied_when = jiffies;
3506610a0bcSAndrew Morton 	}
351d6c10f1fSTejun Heo 	inode_wb_list_move_locked(inode, wb, &wb->b_dirty);
3526610a0bcSAndrew Morton }
3536610a0bcSAndrew Morton 
3546610a0bcSAndrew Morton /*
35566f3b8e2SJens Axboe  * requeue inode for re-scanning after bdi->b_io list is exhausted.
356c986d1e2SAndrew Morton  */
357f758eeabSChristoph Hellwig static void requeue_io(struct inode *inode, struct bdi_writeback *wb)
358c986d1e2SAndrew Morton {
359d6c10f1fSTejun Heo 	inode_wb_list_move_locked(inode, wb, &wb->b_more_io);
360c986d1e2SAndrew Morton }
361c986d1e2SAndrew Morton 
3621c0eeaf5SJoern Engel static void inode_sync_complete(struct inode *inode)
3631c0eeaf5SJoern Engel {
364365b94aeSJan Kara 	inode->i_state &= ~I_SYNC;
3654eff96ddSJan Kara 	/* If inode is clean an unused, put it into LRU now... */
3664eff96ddSJan Kara 	inode_add_lru(inode);
367365b94aeSJan Kara 	/* Waiters must see I_SYNC cleared before being woken up */
3681c0eeaf5SJoern Engel 	smp_mb();
3691c0eeaf5SJoern Engel 	wake_up_bit(&inode->i_state, __I_SYNC);
3701c0eeaf5SJoern Engel }
3711c0eeaf5SJoern Engel 
372d2caa3c5SJeff Layton static bool inode_dirtied_after(struct inode *inode, unsigned long t)
373d2caa3c5SJeff Layton {
374d2caa3c5SJeff Layton 	bool ret = time_after(inode->dirtied_when, t);
375d2caa3c5SJeff Layton #ifndef CONFIG_64BIT
376d2caa3c5SJeff Layton 	/*
377d2caa3c5SJeff Layton 	 * For inodes being constantly redirtied, dirtied_when can get stuck.
378d2caa3c5SJeff Layton 	 * It _appears_ to be in the future, but is actually in distant past.
379d2caa3c5SJeff Layton 	 * This test is necessary to prevent such wrapped-around relative times
3805b0830cbSJens Axboe 	 * from permanently stopping the whole bdi writeback.
381d2caa3c5SJeff Layton 	 */
382d2caa3c5SJeff Layton 	ret = ret && time_before_eq(inode->dirtied_when, jiffies);
383d2caa3c5SJeff Layton #endif
384d2caa3c5SJeff Layton 	return ret;
385d2caa3c5SJeff Layton }
386d2caa3c5SJeff Layton 
3870ae45f63STheodore Ts'o #define EXPIRE_DIRTY_ATIME 0x0001
3880ae45f63STheodore Ts'o 
389c986d1e2SAndrew Morton /*
3900e2f2b23SWang Sheng-Hui  * Move expired (dirtied before work->older_than_this) dirty inodes from
391697e6fedSJan Kara  * @delaying_queue to @dispatch_queue.
3922c136579SFengguang Wu  */
393e84d0a4fSWu Fengguang static int move_expired_inodes(struct list_head *delaying_queue,
3942c136579SFengguang Wu 			       struct list_head *dispatch_queue,
3950ae45f63STheodore Ts'o 			       int flags,
396ad4e38ddSCurt Wohlgemuth 			       struct wb_writeback_work *work)
3972c136579SFengguang Wu {
3980ae45f63STheodore Ts'o 	unsigned long *older_than_this = NULL;
3990ae45f63STheodore Ts'o 	unsigned long expire_time;
4005c03449dSShaohua Li 	LIST_HEAD(tmp);
4015c03449dSShaohua Li 	struct list_head *pos, *node;
402cf137307SJens Axboe 	struct super_block *sb = NULL;
4035c03449dSShaohua Li 	struct inode *inode;
404cf137307SJens Axboe 	int do_sb_sort = 0;
405e84d0a4fSWu Fengguang 	int moved = 0;
4065c03449dSShaohua Li 
4070ae45f63STheodore Ts'o 	if ((flags & EXPIRE_DIRTY_ATIME) == 0)
4080ae45f63STheodore Ts'o 		older_than_this = work->older_than_this;
409a2f48706STheodore Ts'o 	else if (!work->for_sync) {
410a2f48706STheodore Ts'o 		expire_time = jiffies - (dirtytime_expire_interval * HZ);
4110ae45f63STheodore Ts'o 		older_than_this = &expire_time;
4120ae45f63STheodore Ts'o 	}
4132c136579SFengguang Wu 	while (!list_empty(delaying_queue)) {
4147ccf19a8SNick Piggin 		inode = wb_inode(delaying_queue->prev);
4150ae45f63STheodore Ts'o 		if (older_than_this &&
4160ae45f63STheodore Ts'o 		    inode_dirtied_after(inode, *older_than_this))
4172c136579SFengguang Wu 			break;
418a8855990SJan Kara 		list_move(&inode->i_wb_list, &tmp);
419a8855990SJan Kara 		moved++;
4200ae45f63STheodore Ts'o 		if (flags & EXPIRE_DIRTY_ATIME)
4210ae45f63STheodore Ts'o 			set_bit(__I_DIRTY_TIME_EXPIRED, &inode->i_state);
422a8855990SJan Kara 		if (sb_is_blkdev_sb(inode->i_sb))
423a8855990SJan Kara 			continue;
424cf137307SJens Axboe 		if (sb && sb != inode->i_sb)
425cf137307SJens Axboe 			do_sb_sort = 1;
426cf137307SJens Axboe 		sb = inode->i_sb;
4275c03449dSShaohua Li 	}
4285c03449dSShaohua Li 
429cf137307SJens Axboe 	/* just one sb in list, splice to dispatch_queue and we're done */
430cf137307SJens Axboe 	if (!do_sb_sort) {
431cf137307SJens Axboe 		list_splice(&tmp, dispatch_queue);
432e84d0a4fSWu Fengguang 		goto out;
433cf137307SJens Axboe 	}
434cf137307SJens Axboe 
4355c03449dSShaohua Li 	/* Move inodes from one superblock together */
4365c03449dSShaohua Li 	while (!list_empty(&tmp)) {
4377ccf19a8SNick Piggin 		sb = wb_inode(tmp.prev)->i_sb;
4385c03449dSShaohua Li 		list_for_each_prev_safe(pos, node, &tmp) {
4397ccf19a8SNick Piggin 			inode = wb_inode(pos);
4405c03449dSShaohua Li 			if (inode->i_sb == sb)
4417ccf19a8SNick Piggin 				list_move(&inode->i_wb_list, dispatch_queue);
4422c136579SFengguang Wu 		}
4432c136579SFengguang Wu 	}
444e84d0a4fSWu Fengguang out:
445e84d0a4fSWu Fengguang 	return moved;
4465c03449dSShaohua Li }
4472c136579SFengguang Wu 
4482c136579SFengguang Wu /*
4492c136579SFengguang Wu  * Queue all expired dirty inodes for io, eldest first.
4504ea879b9SWu Fengguang  * Before
4514ea879b9SWu Fengguang  *         newly dirtied     b_dirty    b_io    b_more_io
4524ea879b9SWu Fengguang  *         =============>    gf         edc     BA
4534ea879b9SWu Fengguang  * After
4544ea879b9SWu Fengguang  *         newly dirtied     b_dirty    b_io    b_more_io
4554ea879b9SWu Fengguang  *         =============>    g          fBAedc
4564ea879b9SWu Fengguang  *                                           |
4574ea879b9SWu Fengguang  *                                           +--> dequeue for IO
4582c136579SFengguang Wu  */
459ad4e38ddSCurt Wohlgemuth static void queue_io(struct bdi_writeback *wb, struct wb_writeback_work *work)
4602c136579SFengguang Wu {
461e84d0a4fSWu Fengguang 	int moved;
4620ae45f63STheodore Ts'o 
463f758eeabSChristoph Hellwig 	assert_spin_locked(&wb->list_lock);
4644ea879b9SWu Fengguang 	list_splice_init(&wb->b_more_io, &wb->b_io);
4650ae45f63STheodore Ts'o 	moved = move_expired_inodes(&wb->b_dirty, &wb->b_io, 0, work);
4660ae45f63STheodore Ts'o 	moved += move_expired_inodes(&wb->b_dirty_time, &wb->b_io,
4670ae45f63STheodore Ts'o 				     EXPIRE_DIRTY_ATIME, work);
468d6c10f1fSTejun Heo 	if (moved)
469d6c10f1fSTejun Heo 		wb_io_lists_populated(wb);
470ad4e38ddSCurt Wohlgemuth 	trace_writeback_queue_io(wb, work, moved);
47166f3b8e2SJens Axboe }
47266f3b8e2SJens Axboe 
473a9185b41SChristoph Hellwig static int write_inode(struct inode *inode, struct writeback_control *wbc)
47466f3b8e2SJens Axboe {
4759fb0a7daSTejun Heo 	int ret;
4769fb0a7daSTejun Heo 
4779fb0a7daSTejun Heo 	if (inode->i_sb->s_op->write_inode && !is_bad_inode(inode)) {
4789fb0a7daSTejun Heo 		trace_writeback_write_inode_start(inode, wbc);
4799fb0a7daSTejun Heo 		ret = inode->i_sb->s_op->write_inode(inode, wbc);
4809fb0a7daSTejun Heo 		trace_writeback_write_inode(inode, wbc);
4819fb0a7daSTejun Heo 		return ret;
4829fb0a7daSTejun Heo 	}
48303ba3782SJens Axboe 	return 0;
48466f3b8e2SJens Axboe }
48508d8e974SFengguang Wu 
4862c136579SFengguang Wu /*
487169ebd90SJan Kara  * Wait for writeback on an inode to complete. Called with i_lock held.
488169ebd90SJan Kara  * Caller must make sure inode cannot go away when we drop i_lock.
48901c03194SChristoph Hellwig  */
490169ebd90SJan Kara static void __inode_wait_for_writeback(struct inode *inode)
491169ebd90SJan Kara 	__releases(inode->i_lock)
492169ebd90SJan Kara 	__acquires(inode->i_lock)
49301c03194SChristoph Hellwig {
49401c03194SChristoph Hellwig 	DEFINE_WAIT_BIT(wq, &inode->i_state, __I_SYNC);
49501c03194SChristoph Hellwig 	wait_queue_head_t *wqh;
49601c03194SChristoph Hellwig 
49701c03194SChristoph Hellwig 	wqh = bit_waitqueue(&inode->i_state, __I_SYNC);
49858a9d3d8SRichard Kennedy 	while (inode->i_state & I_SYNC) {
499250df6edSDave Chinner 		spin_unlock(&inode->i_lock);
50074316201SNeilBrown 		__wait_on_bit(wqh, &wq, bit_wait,
50174316201SNeilBrown 			      TASK_UNINTERRUPTIBLE);
502250df6edSDave Chinner 		spin_lock(&inode->i_lock);
50358a9d3d8SRichard Kennedy 	}
50401c03194SChristoph Hellwig }
50501c03194SChristoph Hellwig 
50601c03194SChristoph Hellwig /*
507169ebd90SJan Kara  * Wait for writeback on an inode to complete. Caller must have inode pinned.
508169ebd90SJan Kara  */
509169ebd90SJan Kara void inode_wait_for_writeback(struct inode *inode)
510169ebd90SJan Kara {
511169ebd90SJan Kara 	spin_lock(&inode->i_lock);
512169ebd90SJan Kara 	__inode_wait_for_writeback(inode);
513169ebd90SJan Kara 	spin_unlock(&inode->i_lock);
514169ebd90SJan Kara }
515169ebd90SJan Kara 
516169ebd90SJan Kara /*
517169ebd90SJan Kara  * Sleep until I_SYNC is cleared. This function must be called with i_lock
518169ebd90SJan Kara  * held and drops it. It is aimed for callers not holding any inode reference
519169ebd90SJan Kara  * so once i_lock is dropped, inode can go away.
520169ebd90SJan Kara  */
521169ebd90SJan Kara static void inode_sleep_on_writeback(struct inode *inode)
522169ebd90SJan Kara 	__releases(inode->i_lock)
523169ebd90SJan Kara {
524169ebd90SJan Kara 	DEFINE_WAIT(wait);
525169ebd90SJan Kara 	wait_queue_head_t *wqh = bit_waitqueue(&inode->i_state, __I_SYNC);
526169ebd90SJan Kara 	int sleep;
527169ebd90SJan Kara 
528169ebd90SJan Kara 	prepare_to_wait(wqh, &wait, TASK_UNINTERRUPTIBLE);
529169ebd90SJan Kara 	sleep = inode->i_state & I_SYNC;
530169ebd90SJan Kara 	spin_unlock(&inode->i_lock);
531169ebd90SJan Kara 	if (sleep)
532169ebd90SJan Kara 		schedule();
533169ebd90SJan Kara 	finish_wait(wqh, &wait);
534169ebd90SJan Kara }
535169ebd90SJan Kara 
536169ebd90SJan Kara /*
537ccb26b5aSJan Kara  * Find proper writeback list for the inode depending on its current state and
538ccb26b5aSJan Kara  * possibly also change of its state while we were doing writeback.  Here we
539ccb26b5aSJan Kara  * handle things such as livelock prevention or fairness of writeback among
540ccb26b5aSJan Kara  * inodes. This function can be called only by flusher thread - noone else
541ccb26b5aSJan Kara  * processes all inodes in writeback lists and requeueing inodes behind flusher
542ccb26b5aSJan Kara  * thread's back can have unexpected consequences.
543ccb26b5aSJan Kara  */
544ccb26b5aSJan Kara static void requeue_inode(struct inode *inode, struct bdi_writeback *wb,
545ccb26b5aSJan Kara 			  struct writeback_control *wbc)
546ccb26b5aSJan Kara {
547ccb26b5aSJan Kara 	if (inode->i_state & I_FREEING)
548ccb26b5aSJan Kara 		return;
549ccb26b5aSJan Kara 
550ccb26b5aSJan Kara 	/*
551ccb26b5aSJan Kara 	 * Sync livelock prevention. Each inode is tagged and synced in one
552ccb26b5aSJan Kara 	 * shot. If still dirty, it will be redirty_tail()'ed below.  Update
553ccb26b5aSJan Kara 	 * the dirty time to prevent enqueue and sync it again.
554ccb26b5aSJan Kara 	 */
555ccb26b5aSJan Kara 	if ((inode->i_state & I_DIRTY) &&
556ccb26b5aSJan Kara 	    (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages))
557ccb26b5aSJan Kara 		inode->dirtied_when = jiffies;
558ccb26b5aSJan Kara 
5594f8ad655SJan Kara 	if (wbc->pages_skipped) {
5604f8ad655SJan Kara 		/*
5614f8ad655SJan Kara 		 * writeback is not making progress due to locked
5624f8ad655SJan Kara 		 * buffers. Skip this inode for now.
5634f8ad655SJan Kara 		 */
5644f8ad655SJan Kara 		redirty_tail(inode, wb);
5654f8ad655SJan Kara 		return;
5664f8ad655SJan Kara 	}
5674f8ad655SJan Kara 
568ccb26b5aSJan Kara 	if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY)) {
569ccb26b5aSJan Kara 		/*
570ccb26b5aSJan Kara 		 * We didn't write back all the pages.  nfs_writepages()
571ccb26b5aSJan Kara 		 * sometimes bales out without doing anything.
572ccb26b5aSJan Kara 		 */
573ccb26b5aSJan Kara 		if (wbc->nr_to_write <= 0) {
574ccb26b5aSJan Kara 			/* Slice used up. Queue for next turn. */
575ccb26b5aSJan Kara 			requeue_io(inode, wb);
576ccb26b5aSJan Kara 		} else {
577ccb26b5aSJan Kara 			/*
578ccb26b5aSJan Kara 			 * Writeback blocked by something other than
579ccb26b5aSJan Kara 			 * congestion. Delay the inode for some time to
580ccb26b5aSJan Kara 			 * avoid spinning on the CPU (100% iowait)
581ccb26b5aSJan Kara 			 * retrying writeback of the dirty page/inode
582ccb26b5aSJan Kara 			 * that cannot be performed immediately.
583ccb26b5aSJan Kara 			 */
584ccb26b5aSJan Kara 			redirty_tail(inode, wb);
585ccb26b5aSJan Kara 		}
586ccb26b5aSJan Kara 	} else if (inode->i_state & I_DIRTY) {
587ccb26b5aSJan Kara 		/*
588ccb26b5aSJan Kara 		 * Filesystems can dirty the inode during writeback operations,
589ccb26b5aSJan Kara 		 * such as delayed allocation during submission or metadata
590ccb26b5aSJan Kara 		 * updates after data IO completion.
591ccb26b5aSJan Kara 		 */
592ccb26b5aSJan Kara 		redirty_tail(inode, wb);
5930ae45f63STheodore Ts'o 	} else if (inode->i_state & I_DIRTY_TIME) {
594a2f48706STheodore Ts'o 		inode->dirtied_when = jiffies;
595d6c10f1fSTejun Heo 		inode_wb_list_move_locked(inode, wb, &wb->b_dirty_time);
596ccb26b5aSJan Kara 	} else {
597ccb26b5aSJan Kara 		/* The inode is clean. Remove from writeback lists. */
598d6c10f1fSTejun Heo 		inode_wb_list_del_locked(inode, wb);
599ccb26b5aSJan Kara 	}
600ccb26b5aSJan Kara }
601ccb26b5aSJan Kara 
602ccb26b5aSJan Kara /*
6034f8ad655SJan Kara  * Write out an inode and its dirty pages. Do not update the writeback list
6044f8ad655SJan Kara  * linkage. That is left to the caller. The caller is also responsible for
6054f8ad655SJan Kara  * setting I_SYNC flag and calling inode_sync_complete() to clear it.
6061da177e4SLinus Torvalds  */
6071da177e4SLinus Torvalds static int
608cd8ed2a4SYan Hong __writeback_single_inode(struct inode *inode, struct writeback_control *wbc)
6091da177e4SLinus Torvalds {
6101da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
611251d6a47SWu Fengguang 	long nr_to_write = wbc->nr_to_write;
61201c03194SChristoph Hellwig 	unsigned dirty;
6131da177e4SLinus Torvalds 	int ret;
6141da177e4SLinus Torvalds 
6154f8ad655SJan Kara 	WARN_ON(!(inode->i_state & I_SYNC));
6161da177e4SLinus Torvalds 
6179fb0a7daSTejun Heo 	trace_writeback_single_inode_start(inode, wbc, nr_to_write);
6189fb0a7daSTejun Heo 
6191da177e4SLinus Torvalds 	ret = do_writepages(mapping, wbc);
6201da177e4SLinus Torvalds 
62126821ed4SChristoph Hellwig 	/*
62226821ed4SChristoph Hellwig 	 * Make sure to wait on the data before writing out the metadata.
62326821ed4SChristoph Hellwig 	 * This is important for filesystems that modify metadata on data
6247747bd4bSDave Chinner 	 * I/O completion. We don't do it for sync(2) writeback because it has a
6257747bd4bSDave Chinner 	 * separate, external IO completion path and ->sync_fs for guaranteeing
6267747bd4bSDave Chinner 	 * inode metadata is written back correctly.
62726821ed4SChristoph Hellwig 	 */
6287747bd4bSDave Chinner 	if (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync) {
62926821ed4SChristoph Hellwig 		int err = filemap_fdatawait(mapping);
6301da177e4SLinus Torvalds 		if (ret == 0)
6311da177e4SLinus Torvalds 			ret = err;
6321da177e4SLinus Torvalds 	}
6331da177e4SLinus Torvalds 
6345547e8aaSDmitry Monakhov 	/*
6355547e8aaSDmitry Monakhov 	 * Some filesystems may redirty the inode during the writeback
6365547e8aaSDmitry Monakhov 	 * due to delalloc, clear dirty metadata flags right before
6375547e8aaSDmitry Monakhov 	 * write_inode()
6385547e8aaSDmitry Monakhov 	 */
639250df6edSDave Chinner 	spin_lock(&inode->i_lock);
6409c6ac78eSTejun Heo 
6415547e8aaSDmitry Monakhov 	dirty = inode->i_state & I_DIRTY;
642a2f48706STheodore Ts'o 	if (inode->i_state & I_DIRTY_TIME) {
643a2f48706STheodore Ts'o 		if ((dirty & (I_DIRTY_SYNC | I_DIRTY_DATASYNC)) ||
644a2f48706STheodore Ts'o 		    unlikely(inode->i_state & I_DIRTY_TIME_EXPIRED) ||
645a2f48706STheodore Ts'o 		    unlikely(time_after(jiffies,
646a2f48706STheodore Ts'o 					(inode->dirtied_time_when +
647a2f48706STheodore Ts'o 					 dirtytime_expire_interval * HZ)))) {
6480ae45f63STheodore Ts'o 			dirty |= I_DIRTY_TIME | I_DIRTY_TIME_EXPIRED;
6490ae45f63STheodore Ts'o 			trace_writeback_lazytime(inode);
6500ae45f63STheodore Ts'o 		}
651a2f48706STheodore Ts'o 	} else
652a2f48706STheodore Ts'o 		inode->i_state &= ~I_DIRTY_TIME_EXPIRED;
6530ae45f63STheodore Ts'o 	inode->i_state &= ~dirty;
6549c6ac78eSTejun Heo 
6559c6ac78eSTejun Heo 	/*
6569c6ac78eSTejun Heo 	 * Paired with smp_mb() in __mark_inode_dirty().  This allows
6579c6ac78eSTejun Heo 	 * __mark_inode_dirty() to test i_state without grabbing i_lock -
6589c6ac78eSTejun Heo 	 * either they see the I_DIRTY bits cleared or we see the dirtied
6599c6ac78eSTejun Heo 	 * inode.
6609c6ac78eSTejun Heo 	 *
6619c6ac78eSTejun Heo 	 * I_DIRTY_PAGES is always cleared together above even if @mapping
6629c6ac78eSTejun Heo 	 * still has dirty pages.  The flag is reinstated after smp_mb() if
6639c6ac78eSTejun Heo 	 * necessary.  This guarantees that either __mark_inode_dirty()
6649c6ac78eSTejun Heo 	 * sees clear I_DIRTY_PAGES or we see PAGECACHE_TAG_DIRTY.
6659c6ac78eSTejun Heo 	 */
6669c6ac78eSTejun Heo 	smp_mb();
6679c6ac78eSTejun Heo 
6689c6ac78eSTejun Heo 	if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
6699c6ac78eSTejun Heo 		inode->i_state |= I_DIRTY_PAGES;
6709c6ac78eSTejun Heo 
671250df6edSDave Chinner 	spin_unlock(&inode->i_lock);
6729c6ac78eSTejun Heo 
6730ae45f63STheodore Ts'o 	if (dirty & I_DIRTY_TIME)
6740ae45f63STheodore Ts'o 		mark_inode_dirty_sync(inode);
67526821ed4SChristoph Hellwig 	/* Don't write the inode if only I_DIRTY_PAGES was set */
6760ae45f63STheodore Ts'o 	if (dirty & ~I_DIRTY_PAGES) {
677a9185b41SChristoph Hellwig 		int err = write_inode(inode, wbc);
6781da177e4SLinus Torvalds 		if (ret == 0)
6791da177e4SLinus Torvalds 			ret = err;
6801da177e4SLinus Torvalds 	}
6814f8ad655SJan Kara 	trace_writeback_single_inode(inode, wbc, nr_to_write);
6824f8ad655SJan Kara 	return ret;
6834f8ad655SJan Kara }
6844f8ad655SJan Kara 
6854f8ad655SJan Kara /*
6864f8ad655SJan Kara  * Write out an inode's dirty pages. Either the caller has an active reference
6874f8ad655SJan Kara  * on the inode or the inode has I_WILL_FREE set.
6884f8ad655SJan Kara  *
6894f8ad655SJan Kara  * This function is designed to be called for writing back one inode which
6904f8ad655SJan Kara  * we go e.g. from filesystem. Flusher thread uses __writeback_single_inode()
6914f8ad655SJan Kara  * and does more profound writeback list handling in writeback_sb_inodes().
6924f8ad655SJan Kara  */
6934f8ad655SJan Kara static int
6944f8ad655SJan Kara writeback_single_inode(struct inode *inode, struct bdi_writeback *wb,
6954f8ad655SJan Kara 		       struct writeback_control *wbc)
6964f8ad655SJan Kara {
6974f8ad655SJan Kara 	int ret = 0;
6984f8ad655SJan Kara 
6994f8ad655SJan Kara 	spin_lock(&inode->i_lock);
7004f8ad655SJan Kara 	if (!atomic_read(&inode->i_count))
7014f8ad655SJan Kara 		WARN_ON(!(inode->i_state & (I_WILL_FREE|I_FREEING)));
7024f8ad655SJan Kara 	else
7034f8ad655SJan Kara 		WARN_ON(inode->i_state & I_WILL_FREE);
7044f8ad655SJan Kara 
7054f8ad655SJan Kara 	if (inode->i_state & I_SYNC) {
7064f8ad655SJan Kara 		if (wbc->sync_mode != WB_SYNC_ALL)
7074f8ad655SJan Kara 			goto out;
7084f8ad655SJan Kara 		/*
709169ebd90SJan Kara 		 * It's a data-integrity sync. We must wait. Since callers hold
710169ebd90SJan Kara 		 * inode reference or inode has I_WILL_FREE set, it cannot go
711169ebd90SJan Kara 		 * away under us.
7124f8ad655SJan Kara 		 */
713169ebd90SJan Kara 		__inode_wait_for_writeback(inode);
7144f8ad655SJan Kara 	}
7154f8ad655SJan Kara 	WARN_ON(inode->i_state & I_SYNC);
7164f8ad655SJan Kara 	/*
717f9b0e058SJan Kara 	 * Skip inode if it is clean and we have no outstanding writeback in
718f9b0e058SJan Kara 	 * WB_SYNC_ALL mode. We don't want to mess with writeback lists in this
719f9b0e058SJan Kara 	 * function since flusher thread may be doing for example sync in
720f9b0e058SJan Kara 	 * parallel and if we move the inode, it could get skipped. So here we
721f9b0e058SJan Kara 	 * make sure inode is on some writeback list and leave it there unless
722f9b0e058SJan Kara 	 * we have completely cleaned the inode.
7234f8ad655SJan Kara 	 */
7240ae45f63STheodore Ts'o 	if (!(inode->i_state & I_DIRTY_ALL) &&
725f9b0e058SJan Kara 	    (wbc->sync_mode != WB_SYNC_ALL ||
726f9b0e058SJan Kara 	     !mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK)))
7274f8ad655SJan Kara 		goto out;
7284f8ad655SJan Kara 	inode->i_state |= I_SYNC;
7294f8ad655SJan Kara 	spin_unlock(&inode->i_lock);
7304f8ad655SJan Kara 
731cd8ed2a4SYan Hong 	ret = __writeback_single_inode(inode, wbc);
7321da177e4SLinus Torvalds 
733f758eeabSChristoph Hellwig 	spin_lock(&wb->list_lock);
734250df6edSDave Chinner 	spin_lock(&inode->i_lock);
7354f8ad655SJan Kara 	/*
7364f8ad655SJan Kara 	 * If inode is clean, remove it from writeback lists. Otherwise don't
7374f8ad655SJan Kara 	 * touch it. See comment above for explanation.
7384f8ad655SJan Kara 	 */
7390ae45f63STheodore Ts'o 	if (!(inode->i_state & I_DIRTY_ALL))
740d6c10f1fSTejun Heo 		inode_wb_list_del_locked(inode, wb);
7414f8ad655SJan Kara 	spin_unlock(&wb->list_lock);
7421c0eeaf5SJoern Engel 	inode_sync_complete(inode);
7434f8ad655SJan Kara out:
7444f8ad655SJan Kara 	spin_unlock(&inode->i_lock);
7451da177e4SLinus Torvalds 	return ret;
7461da177e4SLinus Torvalds }
7471da177e4SLinus Torvalds 
748a88a341aSTejun Heo static long writeback_chunk_size(struct bdi_writeback *wb,
7491a12d8bdSWu Fengguang 				 struct wb_writeback_work *work)
750d46db3d5SWu Fengguang {
751d46db3d5SWu Fengguang 	long pages;
752d46db3d5SWu Fengguang 
753d46db3d5SWu Fengguang 	/*
754d46db3d5SWu Fengguang 	 * WB_SYNC_ALL mode does livelock avoidance by syncing dirty
755d46db3d5SWu Fengguang 	 * inodes/pages in one big loop. Setting wbc.nr_to_write=LONG_MAX
756d46db3d5SWu Fengguang 	 * here avoids calling into writeback_inodes_wb() more than once.
757d46db3d5SWu Fengguang 	 *
758d46db3d5SWu Fengguang 	 * The intended call sequence for WB_SYNC_ALL writeback is:
759d46db3d5SWu Fengguang 	 *
760d46db3d5SWu Fengguang 	 *      wb_writeback()
761d46db3d5SWu Fengguang 	 *          writeback_sb_inodes()       <== called only once
762d46db3d5SWu Fengguang 	 *              write_cache_pages()     <== called once for each inode
763d46db3d5SWu Fengguang 	 *                   (quickly) tag currently dirty pages
764d46db3d5SWu Fengguang 	 *                   (maybe slowly) sync all tagged pages
765d46db3d5SWu Fengguang 	 */
766d46db3d5SWu Fengguang 	if (work->sync_mode == WB_SYNC_ALL || work->tagged_writepages)
767d46db3d5SWu Fengguang 		pages = LONG_MAX;
7681a12d8bdSWu Fengguang 	else {
769a88a341aSTejun Heo 		pages = min(wb->avg_write_bandwidth / 2,
7701a12d8bdSWu Fengguang 			    global_dirty_limit / DIRTY_SCOPE);
7711a12d8bdSWu Fengguang 		pages = min(pages, work->nr_pages);
7721a12d8bdSWu Fengguang 		pages = round_down(pages + MIN_WRITEBACK_PAGES,
7731a12d8bdSWu Fengguang 				   MIN_WRITEBACK_PAGES);
7741a12d8bdSWu Fengguang 	}
775d46db3d5SWu Fengguang 
776d46db3d5SWu Fengguang 	return pages;
777d46db3d5SWu Fengguang }
778d46db3d5SWu Fengguang 
77903ba3782SJens Axboe /*
780f11c9c5cSEdward Shishkin  * Write a portion of b_io inodes which belong to @sb.
781edadfb10SChristoph Hellwig  *
782d46db3d5SWu Fengguang  * Return the number of pages and/or inodes written.
783f11c9c5cSEdward Shishkin  */
784d46db3d5SWu Fengguang static long writeback_sb_inodes(struct super_block *sb,
785d46db3d5SWu Fengguang 				struct bdi_writeback *wb,
786d46db3d5SWu Fengguang 				struct wb_writeback_work *work)
78703ba3782SJens Axboe {
788d46db3d5SWu Fengguang 	struct writeback_control wbc = {
789d46db3d5SWu Fengguang 		.sync_mode		= work->sync_mode,
790d46db3d5SWu Fengguang 		.tagged_writepages	= work->tagged_writepages,
791d46db3d5SWu Fengguang 		.for_kupdate		= work->for_kupdate,
792d46db3d5SWu Fengguang 		.for_background		= work->for_background,
7937747bd4bSDave Chinner 		.for_sync		= work->for_sync,
794d46db3d5SWu Fengguang 		.range_cyclic		= work->range_cyclic,
795d46db3d5SWu Fengguang 		.range_start		= 0,
796d46db3d5SWu Fengguang 		.range_end		= LLONG_MAX,
797d46db3d5SWu Fengguang 	};
798d46db3d5SWu Fengguang 	unsigned long start_time = jiffies;
799d46db3d5SWu Fengguang 	long write_chunk;
800d46db3d5SWu Fengguang 	long wrote = 0;  /* count both pages and inodes */
801d46db3d5SWu Fengguang 
80203ba3782SJens Axboe 	while (!list_empty(&wb->b_io)) {
8037ccf19a8SNick Piggin 		struct inode *inode = wb_inode(wb->b_io.prev);
804edadfb10SChristoph Hellwig 
805edadfb10SChristoph Hellwig 		if (inode->i_sb != sb) {
806d46db3d5SWu Fengguang 			if (work->sb) {
807edadfb10SChristoph Hellwig 				/*
808edadfb10SChristoph Hellwig 				 * We only want to write back data for this
809edadfb10SChristoph Hellwig 				 * superblock, move all inodes not belonging
810edadfb10SChristoph Hellwig 				 * to it back onto the dirty list.
811edadfb10SChristoph Hellwig 				 */
812f758eeabSChristoph Hellwig 				redirty_tail(inode, wb);
81366f3b8e2SJens Axboe 				continue;
81466f3b8e2SJens Axboe 			}
815edadfb10SChristoph Hellwig 
816edadfb10SChristoph Hellwig 			/*
817edadfb10SChristoph Hellwig 			 * The inode belongs to a different superblock.
818edadfb10SChristoph Hellwig 			 * Bounce back to the caller to unpin this and
819edadfb10SChristoph Hellwig 			 * pin the next superblock.
820edadfb10SChristoph Hellwig 			 */
821d46db3d5SWu Fengguang 			break;
822edadfb10SChristoph Hellwig 		}
823edadfb10SChristoph Hellwig 
8249843b76aSChristoph Hellwig 		/*
825331cbdeeSWanpeng Li 		 * Don't bother with new inodes or inodes being freed, first
826331cbdeeSWanpeng Li 		 * kind does not need periodic writeout yet, and for the latter
8279843b76aSChristoph Hellwig 		 * kind writeout is handled by the freer.
8289843b76aSChristoph Hellwig 		 */
829250df6edSDave Chinner 		spin_lock(&inode->i_lock);
8309843b76aSChristoph Hellwig 		if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) {
831250df6edSDave Chinner 			spin_unlock(&inode->i_lock);
832fcc5c222SWu Fengguang 			redirty_tail(inode, wb);
8337ef0d737SNick Piggin 			continue;
8347ef0d737SNick Piggin 		}
835cc1676d9SJan Kara 		if ((inode->i_state & I_SYNC) && wbc.sync_mode != WB_SYNC_ALL) {
836cc1676d9SJan Kara 			/*
837cc1676d9SJan Kara 			 * If this inode is locked for writeback and we are not
838cc1676d9SJan Kara 			 * doing writeback-for-data-integrity, move it to
839cc1676d9SJan Kara 			 * b_more_io so that writeback can proceed with the
840cc1676d9SJan Kara 			 * other inodes on s_io.
841cc1676d9SJan Kara 			 *
842cc1676d9SJan Kara 			 * We'll have another go at writing back this inode
843cc1676d9SJan Kara 			 * when we completed a full scan of b_io.
844cc1676d9SJan Kara 			 */
845cc1676d9SJan Kara 			spin_unlock(&inode->i_lock);
846cc1676d9SJan Kara 			requeue_io(inode, wb);
847cc1676d9SJan Kara 			trace_writeback_sb_inodes_requeue(inode);
848cc1676d9SJan Kara 			continue;
849cc1676d9SJan Kara 		}
850f0d07b7fSJan Kara 		spin_unlock(&wb->list_lock);
851f0d07b7fSJan Kara 
8524f8ad655SJan Kara 		/*
8534f8ad655SJan Kara 		 * We already requeued the inode if it had I_SYNC set and we
8544f8ad655SJan Kara 		 * are doing WB_SYNC_NONE writeback. So this catches only the
8554f8ad655SJan Kara 		 * WB_SYNC_ALL case.
8564f8ad655SJan Kara 		 */
857169ebd90SJan Kara 		if (inode->i_state & I_SYNC) {
858169ebd90SJan Kara 			/* Wait for I_SYNC. This function drops i_lock... */
859169ebd90SJan Kara 			inode_sleep_on_writeback(inode);
860169ebd90SJan Kara 			/* Inode may be gone, start again */
861ead188f9SJan Kara 			spin_lock(&wb->list_lock);
862169ebd90SJan Kara 			continue;
863169ebd90SJan Kara 		}
8644f8ad655SJan Kara 		inode->i_state |= I_SYNC;
8654f8ad655SJan Kara 		spin_unlock(&inode->i_lock);
866169ebd90SJan Kara 
867a88a341aSTejun Heo 		write_chunk = writeback_chunk_size(wb, work);
868d46db3d5SWu Fengguang 		wbc.nr_to_write = write_chunk;
869d46db3d5SWu Fengguang 		wbc.pages_skipped = 0;
870250df6edSDave Chinner 
871169ebd90SJan Kara 		/*
872169ebd90SJan Kara 		 * We use I_SYNC to pin the inode in memory. While it is set
873169ebd90SJan Kara 		 * evict_inode() will wait so the inode cannot be freed.
874169ebd90SJan Kara 		 */
875cd8ed2a4SYan Hong 		__writeback_single_inode(inode, &wbc);
876d46db3d5SWu Fengguang 
877d46db3d5SWu Fengguang 		work->nr_pages -= write_chunk - wbc.nr_to_write;
878d46db3d5SWu Fengguang 		wrote += write_chunk - wbc.nr_to_write;
8794f8ad655SJan Kara 		spin_lock(&wb->list_lock);
8804f8ad655SJan Kara 		spin_lock(&inode->i_lock);
8810ae45f63STheodore Ts'o 		if (!(inode->i_state & I_DIRTY_ALL))
882d46db3d5SWu Fengguang 			wrote++;
8834f8ad655SJan Kara 		requeue_inode(inode, wb, &wbc);
8844f8ad655SJan Kara 		inode_sync_complete(inode);
8850f1b1fd8SDave Chinner 		spin_unlock(&inode->i_lock);
886169ebd90SJan Kara 		cond_resched_lock(&wb->list_lock);
887d46db3d5SWu Fengguang 		/*
888d46db3d5SWu Fengguang 		 * bail out to wb_writeback() often enough to check
889d46db3d5SWu Fengguang 		 * background threshold and other termination conditions.
890d46db3d5SWu Fengguang 		 */
891d46db3d5SWu Fengguang 		if (wrote) {
892d46db3d5SWu Fengguang 			if (time_is_before_jiffies(start_time + HZ / 10UL))
893d46db3d5SWu Fengguang 				break;
894d46db3d5SWu Fengguang 			if (work->nr_pages <= 0)
895d46db3d5SWu Fengguang 				break;
8961da177e4SLinus Torvalds 		}
8978bc3be27SFengguang Wu 	}
898d46db3d5SWu Fengguang 	return wrote;
899f11c9c5cSEdward Shishkin }
90038f21977SNick Piggin 
901d46db3d5SWu Fengguang static long __writeback_inodes_wb(struct bdi_writeback *wb,
902d46db3d5SWu Fengguang 				  struct wb_writeback_work *work)
903f11c9c5cSEdward Shishkin {
904d46db3d5SWu Fengguang 	unsigned long start_time = jiffies;
905d46db3d5SWu Fengguang 	long wrote = 0;
906f11c9c5cSEdward Shishkin 
907f11c9c5cSEdward Shishkin 	while (!list_empty(&wb->b_io)) {
9087ccf19a8SNick Piggin 		struct inode *inode = wb_inode(wb->b_io.prev);
909f11c9c5cSEdward Shishkin 		struct super_block *sb = inode->i_sb;
910f11c9c5cSEdward Shishkin 
911eb6ef3dfSKonstantin Khlebnikov 		if (!trylock_super(sb)) {
9120e995816SWu Fengguang 			/*
913eb6ef3dfSKonstantin Khlebnikov 			 * trylock_super() may fail consistently due to
9140e995816SWu Fengguang 			 * s_umount being grabbed by someone else. Don't use
9150e995816SWu Fengguang 			 * requeue_io() to avoid busy retrying the inode/sb.
9160e995816SWu Fengguang 			 */
9170e995816SWu Fengguang 			redirty_tail(inode, wb);
918d19de7edSChristoph Hellwig 			continue;
919334132aeSChristoph Hellwig 		}
920d46db3d5SWu Fengguang 		wrote += writeback_sb_inodes(sb, wb, work);
921eb6ef3dfSKonstantin Khlebnikov 		up_read(&sb->s_umount);
922f11c9c5cSEdward Shishkin 
923d46db3d5SWu Fengguang 		/* refer to the same tests at the end of writeback_sb_inodes */
924d46db3d5SWu Fengguang 		if (wrote) {
925d46db3d5SWu Fengguang 			if (time_is_before_jiffies(start_time + HZ / 10UL))
926d46db3d5SWu Fengguang 				break;
927d46db3d5SWu Fengguang 			if (work->nr_pages <= 0)
928f11c9c5cSEdward Shishkin 				break;
929f11c9c5cSEdward Shishkin 		}
930d46db3d5SWu Fengguang 	}
93166f3b8e2SJens Axboe 	/* Leave any unwritten inodes on b_io */
932d46db3d5SWu Fengguang 	return wrote;
93366f3b8e2SJens Axboe }
93466f3b8e2SJens Axboe 
9357d9f073bSWanpeng Li static long writeback_inodes_wb(struct bdi_writeback *wb, long nr_pages,
9360e175a18SCurt Wohlgemuth 				enum wb_reason reason)
937edadfb10SChristoph Hellwig {
938d46db3d5SWu Fengguang 	struct wb_writeback_work work = {
939d46db3d5SWu Fengguang 		.nr_pages	= nr_pages,
940d46db3d5SWu Fengguang 		.sync_mode	= WB_SYNC_NONE,
941d46db3d5SWu Fengguang 		.range_cyclic	= 1,
9420e175a18SCurt Wohlgemuth 		.reason		= reason,
943d46db3d5SWu Fengguang 	};
944edadfb10SChristoph Hellwig 
945f758eeabSChristoph Hellwig 	spin_lock(&wb->list_lock);
946424b351fSWu Fengguang 	if (list_empty(&wb->b_io))
947ad4e38ddSCurt Wohlgemuth 		queue_io(wb, &work);
948d46db3d5SWu Fengguang 	__writeback_inodes_wb(wb, &work);
949f758eeabSChristoph Hellwig 	spin_unlock(&wb->list_lock);
950edadfb10SChristoph Hellwig 
951d46db3d5SWu Fengguang 	return nr_pages - work.nr_pages;
95266f3b8e2SJens Axboe }
95366f3b8e2SJens Axboe 
954a88a341aSTejun Heo static bool over_bground_thresh(struct bdi_writeback *wb)
95503ba3782SJens Axboe {
95603ba3782SJens Axboe 	unsigned long background_thresh, dirty_thresh;
95703ba3782SJens Axboe 
95816c4042fSWu Fengguang 	global_dirty_limits(&background_thresh, &dirty_thresh);
95903ba3782SJens Axboe 
960b00949aaSWu Fengguang 	if (global_page_state(NR_FILE_DIRTY) +
961b00949aaSWu Fengguang 	    global_page_state(NR_UNSTABLE_NFS) > background_thresh)
962b00949aaSWu Fengguang 		return true;
963b00949aaSWu Fengguang 
964a88a341aSTejun Heo 	if (wb_stat(wb, WB_RECLAIMABLE) > wb_dirty_limit(wb, background_thresh))
965b00949aaSWu Fengguang 		return true;
966b00949aaSWu Fengguang 
967b00949aaSWu Fengguang 	return false;
96803ba3782SJens Axboe }
96903ba3782SJens Axboe 
97003ba3782SJens Axboe /*
971e98be2d5SWu Fengguang  * Called under wb->list_lock. If there are multiple wb per bdi,
972e98be2d5SWu Fengguang  * only the flusher working on the first wb should do it.
973e98be2d5SWu Fengguang  */
974e98be2d5SWu Fengguang static void wb_update_bandwidth(struct bdi_writeback *wb,
975e98be2d5SWu Fengguang 				unsigned long start_time)
976e98be2d5SWu Fengguang {
977a88a341aSTejun Heo 	__wb_update_bandwidth(wb, 0, 0, 0, 0, 0, start_time);
978e98be2d5SWu Fengguang }
979e98be2d5SWu Fengguang 
980e98be2d5SWu Fengguang /*
98103ba3782SJens Axboe  * Explicit flushing or periodic writeback of "old" data.
98203ba3782SJens Axboe  *
98303ba3782SJens Axboe  * Define "old": the first time one of an inode's pages is dirtied, we mark the
98403ba3782SJens Axboe  * dirtying-time in the inode's address_space.  So this periodic writeback code
98503ba3782SJens Axboe  * just walks the superblock inode list, writing back any inodes which are
98603ba3782SJens Axboe  * older than a specific point in time.
98703ba3782SJens Axboe  *
98803ba3782SJens Axboe  * Try to run once per dirty_writeback_interval.  But if a writeback event
98903ba3782SJens Axboe  * takes longer than a dirty_writeback_interval interval, then leave a
99003ba3782SJens Axboe  * one-second gap.
99103ba3782SJens Axboe  *
99203ba3782SJens Axboe  * older_than_this takes precedence over nr_to_write.  So we'll only write back
99303ba3782SJens Axboe  * all dirty pages if they are all attached to "old" mappings.
99403ba3782SJens Axboe  */
995c4a77a6cSJens Axboe static long wb_writeback(struct bdi_writeback *wb,
99683ba7b07SChristoph Hellwig 			 struct wb_writeback_work *work)
99703ba3782SJens Axboe {
998e98be2d5SWu Fengguang 	unsigned long wb_start = jiffies;
999d46db3d5SWu Fengguang 	long nr_pages = work->nr_pages;
10000dc83bd3SJan Kara 	unsigned long oldest_jif;
1001a5989bdcSJan Kara 	struct inode *inode;
1002d46db3d5SWu Fengguang 	long progress;
100303ba3782SJens Axboe 
10040dc83bd3SJan Kara 	oldest_jif = jiffies;
10050dc83bd3SJan Kara 	work->older_than_this = &oldest_jif;
100603ba3782SJens Axboe 
1007e8dfc305SWu Fengguang 	spin_lock(&wb->list_lock);
100803ba3782SJens Axboe 	for (;;) {
100903ba3782SJens Axboe 		/*
1010d3ddec76SWu Fengguang 		 * Stop writeback when nr_pages has been consumed
101103ba3782SJens Axboe 		 */
101283ba7b07SChristoph Hellwig 		if (work->nr_pages <= 0)
101303ba3782SJens Axboe 			break;
101403ba3782SJens Axboe 
101503ba3782SJens Axboe 		/*
1016aa373cf5SJan Kara 		 * Background writeout and kupdate-style writeback may
1017aa373cf5SJan Kara 		 * run forever. Stop them if there is other work to do
1018aa373cf5SJan Kara 		 * so that e.g. sync can proceed. They'll be restarted
1019aa373cf5SJan Kara 		 * after the other works are all done.
1020aa373cf5SJan Kara 		 */
1021aa373cf5SJan Kara 		if ((work->for_background || work->for_kupdate) &&
1022f0054bb1STejun Heo 		    !list_empty(&wb->work_list))
1023aa373cf5SJan Kara 			break;
1024aa373cf5SJan Kara 
1025aa373cf5SJan Kara 		/*
1026d3ddec76SWu Fengguang 		 * For background writeout, stop when we are below the
1027d3ddec76SWu Fengguang 		 * background dirty threshold
102803ba3782SJens Axboe 		 */
1029a88a341aSTejun Heo 		if (work->for_background && !over_bground_thresh(wb))
103003ba3782SJens Axboe 			break;
103103ba3782SJens Axboe 
10321bc36b64SJan Kara 		/*
10331bc36b64SJan Kara 		 * Kupdate and background works are special and we want to
10341bc36b64SJan Kara 		 * include all inodes that need writing. Livelock avoidance is
10351bc36b64SJan Kara 		 * handled by these works yielding to any other work so we are
10361bc36b64SJan Kara 		 * safe.
10371bc36b64SJan Kara 		 */
1038ba9aa839SWu Fengguang 		if (work->for_kupdate) {
10390dc83bd3SJan Kara 			oldest_jif = jiffies -
1040ba9aa839SWu Fengguang 				msecs_to_jiffies(dirty_expire_interval * 10);
10411bc36b64SJan Kara 		} else if (work->for_background)
10420dc83bd3SJan Kara 			oldest_jif = jiffies;
1043028c2dd1SDave Chinner 
1044d46db3d5SWu Fengguang 		trace_writeback_start(wb->bdi, work);
1045e8dfc305SWu Fengguang 		if (list_empty(&wb->b_io))
1046ad4e38ddSCurt Wohlgemuth 			queue_io(wb, work);
104783ba7b07SChristoph Hellwig 		if (work->sb)
1048d46db3d5SWu Fengguang 			progress = writeback_sb_inodes(work->sb, wb, work);
1049edadfb10SChristoph Hellwig 		else
1050d46db3d5SWu Fengguang 			progress = __writeback_inodes_wb(wb, work);
1051d46db3d5SWu Fengguang 		trace_writeback_written(wb->bdi, work);
1052028c2dd1SDave Chinner 
1053e98be2d5SWu Fengguang 		wb_update_bandwidth(wb, wb_start);
105403ba3782SJens Axboe 
105503ba3782SJens Axboe 		/*
105671fd05a8SJens Axboe 		 * Did we write something? Try for more
1057e6fb6da2SWu Fengguang 		 *
1058e6fb6da2SWu Fengguang 		 * Dirty inodes are moved to b_io for writeback in batches.
1059e6fb6da2SWu Fengguang 		 * The completion of the current batch does not necessarily
1060e6fb6da2SWu Fengguang 		 * mean the overall work is done. So we keep looping as long
1061e6fb6da2SWu Fengguang 		 * as made some progress on cleaning pages or inodes.
106271fd05a8SJens Axboe 		 */
1063d46db3d5SWu Fengguang 		if (progress)
106403ba3782SJens Axboe 			continue;
1065a5989bdcSJan Kara 		/*
1066e6fb6da2SWu Fengguang 		 * No more inodes for IO, bail
1067a5989bdcSJan Kara 		 */
1068b7a2441fSWu Fengguang 		if (list_empty(&wb->b_more_io))
106903ba3782SJens Axboe 			break;
107003ba3782SJens Axboe 		/*
10718010c3b6SJens Axboe 		 * Nothing written. Wait for some inode to
10728010c3b6SJens Axboe 		 * become available for writeback. Otherwise
10738010c3b6SJens Axboe 		 * we'll just busyloop.
107403ba3782SJens Axboe 		 */
107503ba3782SJens Axboe 		if (!list_empty(&wb->b_more_io))  {
1076d46db3d5SWu Fengguang 			trace_writeback_wait(wb->bdi, work);
107703ba3782SJens Axboe 			inode = wb_inode(wb->b_more_io.prev);
1078250df6edSDave Chinner 			spin_lock(&inode->i_lock);
1079f0d07b7fSJan Kara 			spin_unlock(&wb->list_lock);
1080169ebd90SJan Kara 			/* This function drops i_lock... */
1081169ebd90SJan Kara 			inode_sleep_on_writeback(inode);
1082f0d07b7fSJan Kara 			spin_lock(&wb->list_lock);
108303ba3782SJens Axboe 		}
108403ba3782SJens Axboe 	}
1085e8dfc305SWu Fengguang 	spin_unlock(&wb->list_lock);
108603ba3782SJens Axboe 
1087d46db3d5SWu Fengguang 	return nr_pages - work->nr_pages;
108803ba3782SJens Axboe }
108903ba3782SJens Axboe 
109003ba3782SJens Axboe /*
109183ba7b07SChristoph Hellwig  * Return the next wb_writeback_work struct that hasn't been processed yet.
109203ba3782SJens Axboe  */
1093f0054bb1STejun Heo static struct wb_writeback_work *get_next_work_item(struct bdi_writeback *wb)
109403ba3782SJens Axboe {
109583ba7b07SChristoph Hellwig 	struct wb_writeback_work *work = NULL;
109603ba3782SJens Axboe 
1097f0054bb1STejun Heo 	spin_lock_bh(&wb->work_lock);
1098f0054bb1STejun Heo 	if (!list_empty(&wb->work_list)) {
1099f0054bb1STejun Heo 		work = list_entry(wb->work_list.next,
110083ba7b07SChristoph Hellwig 				  struct wb_writeback_work, list);
110183ba7b07SChristoph Hellwig 		list_del_init(&work->list);
110203ba3782SJens Axboe 	}
1103f0054bb1STejun Heo 	spin_unlock_bh(&wb->work_lock);
110483ba7b07SChristoph Hellwig 	return work;
110503ba3782SJens Axboe }
110603ba3782SJens Axboe 
1107cdf01dd5SLinus Torvalds /*
1108cdf01dd5SLinus Torvalds  * Add in the number of potentially dirty inodes, because each inode
1109cdf01dd5SLinus Torvalds  * write can dirty pagecache in the underlying blockdev.
1110cdf01dd5SLinus Torvalds  */
1111cdf01dd5SLinus Torvalds static unsigned long get_nr_dirty_pages(void)
1112cdf01dd5SLinus Torvalds {
1113cdf01dd5SLinus Torvalds 	return global_page_state(NR_FILE_DIRTY) +
1114cdf01dd5SLinus Torvalds 		global_page_state(NR_UNSTABLE_NFS) +
1115cdf01dd5SLinus Torvalds 		get_nr_dirty_inodes();
1116cdf01dd5SLinus Torvalds }
1117cdf01dd5SLinus Torvalds 
11186585027aSJan Kara static long wb_check_background_flush(struct bdi_writeback *wb)
11196585027aSJan Kara {
1120a88a341aSTejun Heo 	if (over_bground_thresh(wb)) {
11216585027aSJan Kara 
11226585027aSJan Kara 		struct wb_writeback_work work = {
11236585027aSJan Kara 			.nr_pages	= LONG_MAX,
11246585027aSJan Kara 			.sync_mode	= WB_SYNC_NONE,
11256585027aSJan Kara 			.for_background	= 1,
11266585027aSJan Kara 			.range_cyclic	= 1,
11270e175a18SCurt Wohlgemuth 			.reason		= WB_REASON_BACKGROUND,
11286585027aSJan Kara 		};
11296585027aSJan Kara 
11306585027aSJan Kara 		return wb_writeback(wb, &work);
11316585027aSJan Kara 	}
11326585027aSJan Kara 
11336585027aSJan Kara 	return 0;
11346585027aSJan Kara }
11356585027aSJan Kara 
113603ba3782SJens Axboe static long wb_check_old_data_flush(struct bdi_writeback *wb)
113703ba3782SJens Axboe {
113803ba3782SJens Axboe 	unsigned long expired;
113903ba3782SJens Axboe 	long nr_pages;
114003ba3782SJens Axboe 
114169b62d01SJens Axboe 	/*
114269b62d01SJens Axboe 	 * When set to zero, disable periodic writeback
114369b62d01SJens Axboe 	 */
114469b62d01SJens Axboe 	if (!dirty_writeback_interval)
114569b62d01SJens Axboe 		return 0;
114669b62d01SJens Axboe 
114703ba3782SJens Axboe 	expired = wb->last_old_flush +
114803ba3782SJens Axboe 			msecs_to_jiffies(dirty_writeback_interval * 10);
114903ba3782SJens Axboe 	if (time_before(jiffies, expired))
115003ba3782SJens Axboe 		return 0;
115103ba3782SJens Axboe 
115203ba3782SJens Axboe 	wb->last_old_flush = jiffies;
1153cdf01dd5SLinus Torvalds 	nr_pages = get_nr_dirty_pages();
115403ba3782SJens Axboe 
1155c4a77a6cSJens Axboe 	if (nr_pages) {
115683ba7b07SChristoph Hellwig 		struct wb_writeback_work work = {
1157c4a77a6cSJens Axboe 			.nr_pages	= nr_pages,
1158c4a77a6cSJens Axboe 			.sync_mode	= WB_SYNC_NONE,
1159c4a77a6cSJens Axboe 			.for_kupdate	= 1,
1160c4a77a6cSJens Axboe 			.range_cyclic	= 1,
11610e175a18SCurt Wohlgemuth 			.reason		= WB_REASON_PERIODIC,
1162c4a77a6cSJens Axboe 		};
1163c4a77a6cSJens Axboe 
116483ba7b07SChristoph Hellwig 		return wb_writeback(wb, &work);
1165c4a77a6cSJens Axboe 	}
116603ba3782SJens Axboe 
116703ba3782SJens Axboe 	return 0;
116803ba3782SJens Axboe }
116903ba3782SJens Axboe 
117003ba3782SJens Axboe /*
117103ba3782SJens Axboe  * Retrieve work items and do the writeback they describe
117203ba3782SJens Axboe  */
117325d130baSWanpeng Li static long wb_do_writeback(struct bdi_writeback *wb)
117403ba3782SJens Axboe {
117583ba7b07SChristoph Hellwig 	struct wb_writeback_work *work;
1176c4a77a6cSJens Axboe 	long wrote = 0;
117703ba3782SJens Axboe 
11784452226eSTejun Heo 	set_bit(WB_writeback_running, &wb->state);
1179f0054bb1STejun Heo 	while ((work = get_next_work_item(wb)) != NULL) {
1180cc395d7fSTejun Heo 		struct wb_completion *done = work->done;
118183ba7b07SChristoph Hellwig 
1182f0054bb1STejun Heo 		trace_writeback_exec(wb->bdi, work);
1183455b2864SDave Chinner 
118483ba7b07SChristoph Hellwig 		wrote += wb_writeback(wb, work);
118503ba3782SJens Axboe 
1186ac7b19a3STejun Heo 		if (work->auto_free)
118783ba7b07SChristoph Hellwig 			kfree(work);
1188cc395d7fSTejun Heo 		if (done && atomic_dec_and_test(&done->cnt))
1189cc395d7fSTejun Heo 			wake_up_all(&wb->bdi->wb_waitq);
119003ba3782SJens Axboe 	}
119103ba3782SJens Axboe 
119203ba3782SJens Axboe 	/*
119303ba3782SJens Axboe 	 * Check for periodic writeback, kupdated() style
119403ba3782SJens Axboe 	 */
119503ba3782SJens Axboe 	wrote += wb_check_old_data_flush(wb);
11966585027aSJan Kara 	wrote += wb_check_background_flush(wb);
11974452226eSTejun Heo 	clear_bit(WB_writeback_running, &wb->state);
119803ba3782SJens Axboe 
119903ba3782SJens Axboe 	return wrote;
120003ba3782SJens Axboe }
120103ba3782SJens Axboe 
120203ba3782SJens Axboe /*
120303ba3782SJens Axboe  * Handle writeback of dirty data for the device backed by this bdi. Also
1204839a8e86STejun Heo  * reschedules periodically and does kupdated style flushing.
120503ba3782SJens Axboe  */
1206f0054bb1STejun Heo void wb_workfn(struct work_struct *work)
120703ba3782SJens Axboe {
1208839a8e86STejun Heo 	struct bdi_writeback *wb = container_of(to_delayed_work(work),
1209839a8e86STejun Heo 						struct bdi_writeback, dwork);
121003ba3782SJens Axboe 	long pages_written;
121103ba3782SJens Axboe 
1212f0054bb1STejun Heo 	set_worker_desc("flush-%s", dev_name(wb->bdi->dev));
1213766f9164SPeter Zijlstra 	current->flags |= PF_SWAPWRITE;
121403ba3782SJens Axboe 
1215839a8e86STejun Heo 	if (likely(!current_is_workqueue_rescuer() ||
12164452226eSTejun Heo 		   !test_bit(WB_registered, &wb->state))) {
121703ba3782SJens Axboe 		/*
1218f0054bb1STejun Heo 		 * The normal path.  Keep writing back @wb until its
1219839a8e86STejun Heo 		 * work_list is empty.  Note that this path is also taken
1220f0054bb1STejun Heo 		 * if @wb is shutting down even when we're running off the
1221839a8e86STejun Heo 		 * rescuer as work_list needs to be drained.
122203ba3782SJens Axboe 		 */
1223839a8e86STejun Heo 		do {
122425d130baSWanpeng Li 			pages_written = wb_do_writeback(wb);
1225455b2864SDave Chinner 			trace_writeback_pages_written(pages_written);
1226f0054bb1STejun Heo 		} while (!list_empty(&wb->work_list));
1227839a8e86STejun Heo 	} else {
1228253c34e9SArtem Bityutskiy 		/*
1229839a8e86STejun Heo 		 * bdi_wq can't get enough workers and we're running off
1230839a8e86STejun Heo 		 * the emergency worker.  Don't hog it.  Hopefully, 1024 is
1231839a8e86STejun Heo 		 * enough for efficient IO.
1232253c34e9SArtem Bityutskiy 		 */
1233f0054bb1STejun Heo 		pages_written = writeback_inodes_wb(wb, 1024,
1234839a8e86STejun Heo 						    WB_REASON_FORKER_THREAD);
1235839a8e86STejun Heo 		trace_writeback_pages_written(pages_written);
123603ba3782SJens Axboe 	}
123703ba3782SJens Axboe 
1238f0054bb1STejun Heo 	if (!list_empty(&wb->work_list))
12396ca738d6SDerek Basehore 		mod_delayed_work(bdi_wq, &wb->dwork, 0);
12406ca738d6SDerek Basehore 	else if (wb_has_dirty_io(wb) && dirty_writeback_interval)
1241f0054bb1STejun Heo 		wb_wakeup_delayed(wb);
1242455b2864SDave Chinner 
1243839a8e86STejun Heo 	current->flags &= ~PF_SWAPWRITE;
124403ba3782SJens Axboe }
124503ba3782SJens Axboe 
124603ba3782SJens Axboe /*
124703ba3782SJens Axboe  * Start writeback of `nr_pages' pages.  If `nr_pages' is zero, write back
124803ba3782SJens Axboe  * the whole world.
124903ba3782SJens Axboe  */
12500e175a18SCurt Wohlgemuth void wakeup_flusher_threads(long nr_pages, enum wb_reason reason)
125103ba3782SJens Axboe {
1252b8c2f347SChristoph Hellwig 	struct backing_dev_info *bdi;
1253b8c2f347SChristoph Hellwig 
125447df3ddeSJan Kara 	if (!nr_pages)
125547df3ddeSJan Kara 		nr_pages = get_nr_dirty_pages();
1256b8c2f347SChristoph Hellwig 
1257b8c2f347SChristoph Hellwig 	rcu_read_lock();
1258f2b65121STejun Heo 	list_for_each_entry_rcu(bdi, &bdi_list, bdi_list) {
1259f2b65121STejun Heo 		struct bdi_writeback *wb;
1260f2b65121STejun Heo 		struct wb_iter iter;
1261f2b65121STejun Heo 
1262f2b65121STejun Heo 		if (!bdi_has_dirty_io(bdi))
1263f2b65121STejun Heo 			continue;
1264f2b65121STejun Heo 
1265f2b65121STejun Heo 		bdi_for_each_wb(wb, bdi, &iter, 0)
1266f2b65121STejun Heo 			wb_start_writeback(wb, wb_split_bdi_pages(wb, nr_pages),
1267f2b65121STejun Heo 					   false, reason);
1268f2b65121STejun Heo 	}
1269b8c2f347SChristoph Hellwig 	rcu_read_unlock();
127003ba3782SJens Axboe }
127103ba3782SJens Axboe 
1272a2f48706STheodore Ts'o /*
1273a2f48706STheodore Ts'o  * Wake up bdi's periodically to make sure dirtytime inodes gets
1274a2f48706STheodore Ts'o  * written back periodically.  We deliberately do *not* check the
1275a2f48706STheodore Ts'o  * b_dirtytime list in wb_has_dirty_io(), since this would cause the
1276a2f48706STheodore Ts'o  * kernel to be constantly waking up once there are any dirtytime
1277a2f48706STheodore Ts'o  * inodes on the system.  So instead we define a separate delayed work
1278a2f48706STheodore Ts'o  * function which gets called much more rarely.  (By default, only
1279a2f48706STheodore Ts'o  * once every 12 hours.)
1280a2f48706STheodore Ts'o  *
1281a2f48706STheodore Ts'o  * If there is any other write activity going on in the file system,
1282a2f48706STheodore Ts'o  * this function won't be necessary.  But if the only thing that has
1283a2f48706STheodore Ts'o  * happened on the file system is a dirtytime inode caused by an atime
1284a2f48706STheodore Ts'o  * update, we need this infrastructure below to make sure that inode
1285a2f48706STheodore Ts'o  * eventually gets pushed out to disk.
1286a2f48706STheodore Ts'o  */
1287a2f48706STheodore Ts'o static void wakeup_dirtytime_writeback(struct work_struct *w);
1288a2f48706STheodore Ts'o static DECLARE_DELAYED_WORK(dirtytime_work, wakeup_dirtytime_writeback);
1289a2f48706STheodore Ts'o 
1290a2f48706STheodore Ts'o static void wakeup_dirtytime_writeback(struct work_struct *w)
1291a2f48706STheodore Ts'o {
1292a2f48706STheodore Ts'o 	struct backing_dev_info *bdi;
1293a2f48706STheodore Ts'o 
1294a2f48706STheodore Ts'o 	rcu_read_lock();
1295a2f48706STheodore Ts'o 	list_for_each_entry_rcu(bdi, &bdi_list, bdi_list) {
1296001fe6f6STejun Heo 		struct bdi_writeback *wb;
1297001fe6f6STejun Heo 		struct wb_iter iter;
1298001fe6f6STejun Heo 
1299001fe6f6STejun Heo 		bdi_for_each_wb(wb, bdi, &iter, 0)
1300001fe6f6STejun Heo 			if (!list_empty(&bdi->wb.b_dirty_time))
1301f0054bb1STejun Heo 				wb_wakeup(&bdi->wb);
1302a2f48706STheodore Ts'o 	}
1303a2f48706STheodore Ts'o 	rcu_read_unlock();
1304a2f48706STheodore Ts'o 	schedule_delayed_work(&dirtytime_work, dirtytime_expire_interval * HZ);
1305a2f48706STheodore Ts'o }
1306a2f48706STheodore Ts'o 
1307a2f48706STheodore Ts'o static int __init start_dirtytime_writeback(void)
1308a2f48706STheodore Ts'o {
1309a2f48706STheodore Ts'o 	schedule_delayed_work(&dirtytime_work, dirtytime_expire_interval * HZ);
1310a2f48706STheodore Ts'o 	return 0;
1311a2f48706STheodore Ts'o }
1312a2f48706STheodore Ts'o __initcall(start_dirtytime_writeback);
1313a2f48706STheodore Ts'o 
13141efff914STheodore Ts'o int dirtytime_interval_handler(struct ctl_table *table, int write,
13151efff914STheodore Ts'o 			       void __user *buffer, size_t *lenp, loff_t *ppos)
13161efff914STheodore Ts'o {
13171efff914STheodore Ts'o 	int ret;
13181efff914STheodore Ts'o 
13191efff914STheodore Ts'o 	ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
13201efff914STheodore Ts'o 	if (ret == 0 && write)
13211efff914STheodore Ts'o 		mod_delayed_work(system_wq, &dirtytime_work, 0);
13221efff914STheodore Ts'o 	return ret;
13231efff914STheodore Ts'o }
13241efff914STheodore Ts'o 
132503ba3782SJens Axboe static noinline void block_dump___mark_inode_dirty(struct inode *inode)
132603ba3782SJens Axboe {
132703ba3782SJens Axboe 	if (inode->i_ino || strcmp(inode->i_sb->s_id, "bdev")) {
132803ba3782SJens Axboe 		struct dentry *dentry;
132903ba3782SJens Axboe 		const char *name = "?";
133003ba3782SJens Axboe 
133103ba3782SJens Axboe 		dentry = d_find_alias(inode);
133203ba3782SJens Axboe 		if (dentry) {
133303ba3782SJens Axboe 			spin_lock(&dentry->d_lock);
133403ba3782SJens Axboe 			name = (const char *) dentry->d_name.name;
133503ba3782SJens Axboe 		}
133603ba3782SJens Axboe 		printk(KERN_DEBUG
133703ba3782SJens Axboe 		       "%s(%d): dirtied inode %lu (%s) on %s\n",
133803ba3782SJens Axboe 		       current->comm, task_pid_nr(current), inode->i_ino,
133903ba3782SJens Axboe 		       name, inode->i_sb->s_id);
134003ba3782SJens Axboe 		if (dentry) {
134103ba3782SJens Axboe 			spin_unlock(&dentry->d_lock);
134203ba3782SJens Axboe 			dput(dentry);
134303ba3782SJens Axboe 		}
134403ba3782SJens Axboe 	}
134503ba3782SJens Axboe }
134603ba3782SJens Axboe 
134703ba3782SJens Axboe /**
134803ba3782SJens Axboe  *	__mark_inode_dirty -	internal function
134903ba3782SJens Axboe  *	@inode: inode to mark
135003ba3782SJens Axboe  *	@flags: what kind of dirty (i.e. I_DIRTY_SYNC)
135103ba3782SJens Axboe  *	Mark an inode as dirty. Callers should use mark_inode_dirty or
135203ba3782SJens Axboe  *  	mark_inode_dirty_sync.
135303ba3782SJens Axboe  *
135403ba3782SJens Axboe  * Put the inode on the super block's dirty list.
135503ba3782SJens Axboe  *
135603ba3782SJens Axboe  * CAREFUL! We mark it dirty unconditionally, but move it onto the
135703ba3782SJens Axboe  * dirty list only if it is hashed or if it refers to a blockdev.
135803ba3782SJens Axboe  * If it was not hashed, it will never be added to the dirty list
135903ba3782SJens Axboe  * even if it is later hashed, as it will have been marked dirty already.
136003ba3782SJens Axboe  *
136103ba3782SJens Axboe  * In short, make sure you hash any inodes _before_ you start marking
136203ba3782SJens Axboe  * them dirty.
136303ba3782SJens Axboe  *
136403ba3782SJens Axboe  * Note that for blockdevs, inode->dirtied_when represents the dirtying time of
136503ba3782SJens Axboe  * the block-special inode (/dev/hda1) itself.  And the ->dirtied_when field of
136603ba3782SJens Axboe  * the kernel-internal blockdev inode represents the dirtying time of the
136703ba3782SJens Axboe  * blockdev's pages.  This is why for I_DIRTY_PAGES we always use
136803ba3782SJens Axboe  * page->mapping->host, so the page-dirtying time is recorded in the internal
136903ba3782SJens Axboe  * blockdev inode.
137003ba3782SJens Axboe  */
13710ae45f63STheodore Ts'o #define I_DIRTY_INODE (I_DIRTY_SYNC | I_DIRTY_DATASYNC)
137203ba3782SJens Axboe void __mark_inode_dirty(struct inode *inode, int flags)
137303ba3782SJens Axboe {
137403ba3782SJens Axboe 	struct super_block *sb = inode->i_sb;
1375253c34e9SArtem Bityutskiy 	struct backing_dev_info *bdi = NULL;
13760ae45f63STheodore Ts'o 	int dirtytime;
13770ae45f63STheodore Ts'o 
13780ae45f63STheodore Ts'o 	trace_writeback_mark_inode_dirty(inode, flags);
137903ba3782SJens Axboe 
138003ba3782SJens Axboe 	/*
138103ba3782SJens Axboe 	 * Don't do this for I_DIRTY_PAGES - that doesn't actually
138203ba3782SJens Axboe 	 * dirty the inode itself
138303ba3782SJens Axboe 	 */
13840ae45f63STheodore Ts'o 	if (flags & (I_DIRTY_SYNC | I_DIRTY_DATASYNC | I_DIRTY_TIME)) {
13859fb0a7daSTejun Heo 		trace_writeback_dirty_inode_start(inode, flags);
13869fb0a7daSTejun Heo 
138703ba3782SJens Axboe 		if (sb->s_op->dirty_inode)
1388aa385729SChristoph Hellwig 			sb->s_op->dirty_inode(inode, flags);
13899fb0a7daSTejun Heo 
13909fb0a7daSTejun Heo 		trace_writeback_dirty_inode(inode, flags);
139103ba3782SJens Axboe 	}
13920ae45f63STheodore Ts'o 	if (flags & I_DIRTY_INODE)
13930ae45f63STheodore Ts'o 		flags &= ~I_DIRTY_TIME;
13940ae45f63STheodore Ts'o 	dirtytime = flags & I_DIRTY_TIME;
139503ba3782SJens Axboe 
139603ba3782SJens Axboe 	/*
13979c6ac78eSTejun Heo 	 * Paired with smp_mb() in __writeback_single_inode() for the
13989c6ac78eSTejun Heo 	 * following lockless i_state test.  See there for details.
139903ba3782SJens Axboe 	 */
140003ba3782SJens Axboe 	smp_mb();
140103ba3782SJens Axboe 
14020ae45f63STheodore Ts'o 	if (((inode->i_state & flags) == flags) ||
14030ae45f63STheodore Ts'o 	    (dirtytime && (inode->i_state & I_DIRTY_INODE)))
140403ba3782SJens Axboe 		return;
140503ba3782SJens Axboe 
140603ba3782SJens Axboe 	if (unlikely(block_dump))
140703ba3782SJens Axboe 		block_dump___mark_inode_dirty(inode);
140803ba3782SJens Axboe 
1409250df6edSDave Chinner 	spin_lock(&inode->i_lock);
14100ae45f63STheodore Ts'o 	if (dirtytime && (inode->i_state & I_DIRTY_INODE))
14110ae45f63STheodore Ts'o 		goto out_unlock_inode;
141203ba3782SJens Axboe 	if ((inode->i_state & flags) != flags) {
141303ba3782SJens Axboe 		const int was_dirty = inode->i_state & I_DIRTY;
141403ba3782SJens Axboe 
141552ebea74STejun Heo 		inode_attach_wb(inode, NULL);
141652ebea74STejun Heo 
14170ae45f63STheodore Ts'o 		if (flags & I_DIRTY_INODE)
14180ae45f63STheodore Ts'o 			inode->i_state &= ~I_DIRTY_TIME;
141903ba3782SJens Axboe 		inode->i_state |= flags;
142003ba3782SJens Axboe 
142103ba3782SJens Axboe 		/*
142203ba3782SJens Axboe 		 * If the inode is being synced, just update its dirty state.
142303ba3782SJens Axboe 		 * The unlocker will place the inode on the appropriate
142403ba3782SJens Axboe 		 * superblock list, based upon its state.
142503ba3782SJens Axboe 		 */
142603ba3782SJens Axboe 		if (inode->i_state & I_SYNC)
1427250df6edSDave Chinner 			goto out_unlock_inode;
142803ba3782SJens Axboe 
142903ba3782SJens Axboe 		/*
143003ba3782SJens Axboe 		 * Only add valid (hashed) inodes to the superblock's
143103ba3782SJens Axboe 		 * dirty list.  Add blockdev inodes as well.
143203ba3782SJens Axboe 		 */
143303ba3782SJens Axboe 		if (!S_ISBLK(inode->i_mode)) {
14341d3382cbSAl Viro 			if (inode_unhashed(inode))
1435250df6edSDave Chinner 				goto out_unlock_inode;
143603ba3782SJens Axboe 		}
1437a4ffdde6SAl Viro 		if (inode->i_state & I_FREEING)
1438250df6edSDave Chinner 			goto out_unlock_inode;
143903ba3782SJens Axboe 
144003ba3782SJens Axboe 		/*
144103ba3782SJens Axboe 		 * If the inode was already on b_dirty/b_io/b_more_io, don't
144203ba3782SJens Axboe 		 * reposition it (that would break b_dirty time-ordering).
144303ba3782SJens Axboe 		 */
144403ba3782SJens Axboe 		if (!was_dirty) {
1445d6c10f1fSTejun Heo 			struct list_head *dirty_list;
1446a66979abSDave Chinner 			bool wakeup_bdi = false;
1447253c34e9SArtem Bityutskiy 			bdi = inode_to_bdi(inode);
1448500b067cSJens Axboe 
1449146d7009SJunxiao Bi 			spin_unlock(&inode->i_lock);
1450146d7009SJunxiao Bi 			spin_lock(&bdi->wb.list_lock);
1451253c34e9SArtem Bityutskiy 
1452d6c10f1fSTejun Heo 			WARN(bdi_cap_writeback_dirty(bdi) &&
1453d6c10f1fSTejun Heo 			     !test_bit(WB_registered, &bdi->wb.state),
1454d6c10f1fSTejun Heo 			     "bdi-%s not registered\n", bdi->name);
145503ba3782SJens Axboe 
145603ba3782SJens Axboe 			inode->dirtied_when = jiffies;
1457a2f48706STheodore Ts'o 			if (dirtytime)
1458a2f48706STheodore Ts'o 				inode->dirtied_time_when = jiffies;
1459d6c10f1fSTejun Heo 
1460a2f48706STheodore Ts'o 			if (inode->i_state & (I_DIRTY_INODE | I_DIRTY_PAGES))
1461d6c10f1fSTejun Heo 				dirty_list = &bdi->wb.b_dirty;
1462a2f48706STheodore Ts'o 			else
1463d6c10f1fSTejun Heo 				dirty_list = &bdi->wb.b_dirty_time;
1464d6c10f1fSTejun Heo 
1465d6c10f1fSTejun Heo 			wakeup_bdi = inode_wb_list_move_locked(inode, &bdi->wb,
1466d6c10f1fSTejun Heo 							       dirty_list);
1467d6c10f1fSTejun Heo 
1468f758eeabSChristoph Hellwig 			spin_unlock(&bdi->wb.list_lock);
14690ae45f63STheodore Ts'o 			trace_writeback_dirty_inode_enqueue(inode);
1470253c34e9SArtem Bityutskiy 
1471d6c10f1fSTejun Heo 			/*
1472d6c10f1fSTejun Heo 			 * If this is the first dirty inode for this bdi,
1473d6c10f1fSTejun Heo 			 * we have to wake-up the corresponding bdi thread
1474d6c10f1fSTejun Heo 			 * to make sure background write-back happens
1475d6c10f1fSTejun Heo 			 * later.
1476d6c10f1fSTejun Heo 			 */
1477d6c10f1fSTejun Heo 			if (bdi_cap_writeback_dirty(bdi) && wakeup_bdi)
1478f0054bb1STejun Heo 				wb_wakeup_delayed(&bdi->wb);
1479a66979abSDave Chinner 			return;
1480a66979abSDave Chinner 		}
1481a66979abSDave Chinner 	}
1482a66979abSDave Chinner out_unlock_inode:
1483a66979abSDave Chinner 	spin_unlock(&inode->i_lock);
1484a66979abSDave Chinner 
148503ba3782SJens Axboe }
148603ba3782SJens Axboe EXPORT_SYMBOL(__mark_inode_dirty);
148703ba3782SJens Axboe 
1488b6e51316SJens Axboe static void wait_sb_inodes(struct super_block *sb)
148966f3b8e2SJens Axboe {
149038f21977SNick Piggin 	struct inode *inode, *old_inode = NULL;
149138f21977SNick Piggin 
149203ba3782SJens Axboe 	/*
149303ba3782SJens Axboe 	 * We need to be protected against the filesystem going from
149403ba3782SJens Axboe 	 * r/o to r/w or vice versa.
149503ba3782SJens Axboe 	 */
1496b6e51316SJens Axboe 	WARN_ON(!rwsem_is_locked(&sb->s_umount));
149703ba3782SJens Axboe 
149855fa6091SDave Chinner 	spin_lock(&inode_sb_list_lock);
149966f3b8e2SJens Axboe 
150038f21977SNick Piggin 	/*
150138f21977SNick Piggin 	 * Data integrity sync. Must wait for all pages under writeback,
150238f21977SNick Piggin 	 * because there may have been pages dirtied before our sync
150338f21977SNick Piggin 	 * call, but which had writeout started before we write it out.
150438f21977SNick Piggin 	 * In which case, the inode may not be on the dirty list, but
150538f21977SNick Piggin 	 * we still have to wait for that writeout.
150638f21977SNick Piggin 	 */
1507b6e51316SJens Axboe 	list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
1508250df6edSDave Chinner 		struct address_space *mapping = inode->i_mapping;
150938f21977SNick Piggin 
1510250df6edSDave Chinner 		spin_lock(&inode->i_lock);
1511250df6edSDave Chinner 		if ((inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW)) ||
1512250df6edSDave Chinner 		    (mapping->nrpages == 0)) {
1513250df6edSDave Chinner 			spin_unlock(&inode->i_lock);
151438f21977SNick Piggin 			continue;
1515250df6edSDave Chinner 		}
151638f21977SNick Piggin 		__iget(inode);
1517250df6edSDave Chinner 		spin_unlock(&inode->i_lock);
151855fa6091SDave Chinner 		spin_unlock(&inode_sb_list_lock);
151955fa6091SDave Chinner 
152038f21977SNick Piggin 		/*
152155fa6091SDave Chinner 		 * We hold a reference to 'inode' so it couldn't have been
152255fa6091SDave Chinner 		 * removed from s_inodes list while we dropped the
152355fa6091SDave Chinner 		 * inode_sb_list_lock.  We cannot iput the inode now as we can
152455fa6091SDave Chinner 		 * be holding the last reference and we cannot iput it under
152555fa6091SDave Chinner 		 * inode_sb_list_lock. So we keep the reference and iput it
152655fa6091SDave Chinner 		 * later.
152738f21977SNick Piggin 		 */
152838f21977SNick Piggin 		iput(old_inode);
152938f21977SNick Piggin 		old_inode = inode;
153038f21977SNick Piggin 
153138f21977SNick Piggin 		filemap_fdatawait(mapping);
153238f21977SNick Piggin 
153338f21977SNick Piggin 		cond_resched();
153438f21977SNick Piggin 
153555fa6091SDave Chinner 		spin_lock(&inode_sb_list_lock);
153638f21977SNick Piggin 	}
153755fa6091SDave Chinner 	spin_unlock(&inode_sb_list_lock);
153838f21977SNick Piggin 	iput(old_inode);
153966f3b8e2SJens Axboe }
15401da177e4SLinus Torvalds 
1541d8a8559cSJens Axboe /**
15423259f8beSChris Mason  * writeback_inodes_sb_nr -	writeback dirty inodes from given super_block
1543d8a8559cSJens Axboe  * @sb: the superblock
15443259f8beSChris Mason  * @nr: the number of pages to write
1545786228abSMarcos Paulo de Souza  * @reason: reason why some writeback work initiated
15461da177e4SLinus Torvalds  *
1547d8a8559cSJens Axboe  * Start writeback on some inodes on this super_block. No guarantees are made
1548d8a8559cSJens Axboe  * on how many (if any) will be written, and this function does not wait
15493259f8beSChris Mason  * for IO completion of submitted IO.
15501da177e4SLinus Torvalds  */
15510e175a18SCurt Wohlgemuth void writeback_inodes_sb_nr(struct super_block *sb,
15520e175a18SCurt Wohlgemuth 			    unsigned long nr,
15530e175a18SCurt Wohlgemuth 			    enum wb_reason reason)
15541da177e4SLinus Torvalds {
1555cc395d7fSTejun Heo 	DEFINE_WB_COMPLETION_ONSTACK(done);
155683ba7b07SChristoph Hellwig 	struct wb_writeback_work work = {
15573c4d7165SChristoph Hellwig 		.sb			= sb,
15583c4d7165SChristoph Hellwig 		.sync_mode		= WB_SYNC_NONE,
15596e6938b6SWu Fengguang 		.tagged_writepages	= 1,
156083ba7b07SChristoph Hellwig 		.done			= &done,
15613259f8beSChris Mason 		.nr_pages		= nr,
15620e175a18SCurt Wohlgemuth 		.reason			= reason,
15633c4d7165SChristoph Hellwig 	};
1564e7972912STejun Heo 	struct backing_dev_info *bdi = sb->s_bdi;
15650e3c9a22SJens Axboe 
1566e7972912STejun Heo 	if (!bdi_has_dirty_io(bdi) || bdi == &noop_backing_dev_info)
15676eedc701SJan Kara 		return;
1568cf37e972SChristoph Hellwig 	WARN_ON(!rwsem_is_locked(&sb->s_umount));
1569e7972912STejun Heo 	wb_queue_work(&bdi->wb, &work);
1570cc395d7fSTejun Heo 	wb_wait_for_completion(bdi, &done);
15711da177e4SLinus Torvalds }
15723259f8beSChris Mason EXPORT_SYMBOL(writeback_inodes_sb_nr);
15733259f8beSChris Mason 
15743259f8beSChris Mason /**
15753259f8beSChris Mason  * writeback_inodes_sb	-	writeback dirty inodes from given super_block
15763259f8beSChris Mason  * @sb: the superblock
1577786228abSMarcos Paulo de Souza  * @reason: reason why some writeback work was initiated
15783259f8beSChris Mason  *
15793259f8beSChris Mason  * Start writeback on some inodes on this super_block. No guarantees are made
15803259f8beSChris Mason  * on how many (if any) will be written, and this function does not wait
15813259f8beSChris Mason  * for IO completion of submitted IO.
15823259f8beSChris Mason  */
15830e175a18SCurt Wohlgemuth void writeback_inodes_sb(struct super_block *sb, enum wb_reason reason)
15843259f8beSChris Mason {
15850e175a18SCurt Wohlgemuth 	return writeback_inodes_sb_nr(sb, get_nr_dirty_pages(), reason);
15863259f8beSChris Mason }
1587d8a8559cSJens Axboe EXPORT_SYMBOL(writeback_inodes_sb);
1588d8a8559cSJens Axboe 
1589d8a8559cSJens Axboe /**
159010ee27a0SMiao Xie  * try_to_writeback_inodes_sb_nr - try to start writeback if none underway
15913259f8beSChris Mason  * @sb: the superblock
15923259f8beSChris Mason  * @nr: the number of pages to write
159310ee27a0SMiao Xie  * @reason: the reason of writeback
15943259f8beSChris Mason  *
159510ee27a0SMiao Xie  * Invoke writeback_inodes_sb_nr if no writeback is currently underway.
15963259f8beSChris Mason  * Returns 1 if writeback was started, 0 if not.
15973259f8beSChris Mason  */
159810ee27a0SMiao Xie int try_to_writeback_inodes_sb_nr(struct super_block *sb,
15990e175a18SCurt Wohlgemuth 				  unsigned long nr,
16000e175a18SCurt Wohlgemuth 				  enum wb_reason reason)
16013259f8beSChris Mason {
1602bc05873dSTejun Heo 	if (writeback_in_progress(&sb->s_bdi->wb))
160310ee27a0SMiao Xie 		return 1;
160410ee27a0SMiao Xie 
160510ee27a0SMiao Xie 	if (!down_read_trylock(&sb->s_umount))
160610ee27a0SMiao Xie 		return 0;
160710ee27a0SMiao Xie 
16080e175a18SCurt Wohlgemuth 	writeback_inodes_sb_nr(sb, nr, reason);
16093259f8beSChris Mason 	up_read(&sb->s_umount);
16103259f8beSChris Mason 	return 1;
16113259f8beSChris Mason }
161210ee27a0SMiao Xie EXPORT_SYMBOL(try_to_writeback_inodes_sb_nr);
161310ee27a0SMiao Xie 
161410ee27a0SMiao Xie /**
161510ee27a0SMiao Xie  * try_to_writeback_inodes_sb - try to start writeback if none underway
161610ee27a0SMiao Xie  * @sb: the superblock
161710ee27a0SMiao Xie  * @reason: reason why some writeback work was initiated
161810ee27a0SMiao Xie  *
161910ee27a0SMiao Xie  * Implement by try_to_writeback_inodes_sb_nr()
162010ee27a0SMiao Xie  * Returns 1 if writeback was started, 0 if not.
162110ee27a0SMiao Xie  */
162210ee27a0SMiao Xie int try_to_writeback_inodes_sb(struct super_block *sb, enum wb_reason reason)
162310ee27a0SMiao Xie {
162410ee27a0SMiao Xie 	return try_to_writeback_inodes_sb_nr(sb, get_nr_dirty_pages(), reason);
162510ee27a0SMiao Xie }
162610ee27a0SMiao Xie EXPORT_SYMBOL(try_to_writeback_inodes_sb);
16273259f8beSChris Mason 
16283259f8beSChris Mason /**
1629d8a8559cSJens Axboe  * sync_inodes_sb	-	sync sb inode pages
1630d8a8559cSJens Axboe  * @sb: the superblock
1631d8a8559cSJens Axboe  *
1632d8a8559cSJens Axboe  * This function writes and waits on any dirty inode belonging to this
16330dc83bd3SJan Kara  * super_block.
1634d8a8559cSJens Axboe  */
16350dc83bd3SJan Kara void sync_inodes_sb(struct super_block *sb)
1636d8a8559cSJens Axboe {
1637cc395d7fSTejun Heo 	DEFINE_WB_COMPLETION_ONSTACK(done);
163883ba7b07SChristoph Hellwig 	struct wb_writeback_work work = {
16393c4d7165SChristoph Hellwig 		.sb		= sb,
16403c4d7165SChristoph Hellwig 		.sync_mode	= WB_SYNC_ALL,
16413c4d7165SChristoph Hellwig 		.nr_pages	= LONG_MAX,
16423c4d7165SChristoph Hellwig 		.range_cyclic	= 0,
164383ba7b07SChristoph Hellwig 		.done		= &done,
16440e175a18SCurt Wohlgemuth 		.reason		= WB_REASON_SYNC,
16457747bd4bSDave Chinner 		.for_sync	= 1,
16463c4d7165SChristoph Hellwig 	};
1647e7972912STejun Heo 	struct backing_dev_info *bdi = sb->s_bdi;
16483c4d7165SChristoph Hellwig 
16496eedc701SJan Kara 	/* Nothing to do? */
1650e7972912STejun Heo 	if (!bdi_has_dirty_io(bdi) || bdi == &noop_backing_dev_info)
16516eedc701SJan Kara 		return;
1652cf37e972SChristoph Hellwig 	WARN_ON(!rwsem_is_locked(&sb->s_umount));
1653cf37e972SChristoph Hellwig 
1654e7972912STejun Heo 	wb_queue_work(&bdi->wb, &work);
1655cc395d7fSTejun Heo 	wb_wait_for_completion(bdi, &done);
165683ba7b07SChristoph Hellwig 
1657b6e51316SJens Axboe 	wait_sb_inodes(sb);
1658d8a8559cSJens Axboe }
1659d8a8559cSJens Axboe EXPORT_SYMBOL(sync_inodes_sb);
16601da177e4SLinus Torvalds 
16611da177e4SLinus Torvalds /**
16621da177e4SLinus Torvalds  * write_inode_now	-	write an inode to disk
16631da177e4SLinus Torvalds  * @inode: inode to write to disk
16641da177e4SLinus Torvalds  * @sync: whether the write should be synchronous or not
16651da177e4SLinus Torvalds  *
16667f04c26dSAndrea Arcangeli  * This function commits an inode to disk immediately if it is dirty. This is
16677f04c26dSAndrea Arcangeli  * primarily needed by knfsd.
16687f04c26dSAndrea Arcangeli  *
16697f04c26dSAndrea Arcangeli  * The caller must either have a ref on the inode or must have set I_WILL_FREE.
16701da177e4SLinus Torvalds  */
16711da177e4SLinus Torvalds int write_inode_now(struct inode *inode, int sync)
16721da177e4SLinus Torvalds {
1673f758eeabSChristoph Hellwig 	struct bdi_writeback *wb = &inode_to_bdi(inode)->wb;
16741da177e4SLinus Torvalds 	struct writeback_control wbc = {
16751da177e4SLinus Torvalds 		.nr_to_write = LONG_MAX,
167618914b18SMike Galbraith 		.sync_mode = sync ? WB_SYNC_ALL : WB_SYNC_NONE,
1677111ebb6eSOGAWA Hirofumi 		.range_start = 0,
1678111ebb6eSOGAWA Hirofumi 		.range_end = LLONG_MAX,
16791da177e4SLinus Torvalds 	};
16801da177e4SLinus Torvalds 
16811da177e4SLinus Torvalds 	if (!mapping_cap_writeback_dirty(inode->i_mapping))
168249364ce2SAndrew Morton 		wbc.nr_to_write = 0;
16831da177e4SLinus Torvalds 
16841da177e4SLinus Torvalds 	might_sleep();
16854f8ad655SJan Kara 	return writeback_single_inode(inode, wb, &wbc);
16861da177e4SLinus Torvalds }
16871da177e4SLinus Torvalds EXPORT_SYMBOL(write_inode_now);
16881da177e4SLinus Torvalds 
16891da177e4SLinus Torvalds /**
16901da177e4SLinus Torvalds  * sync_inode - write an inode and its pages to disk.
16911da177e4SLinus Torvalds  * @inode: the inode to sync
16921da177e4SLinus Torvalds  * @wbc: controls the writeback mode
16931da177e4SLinus Torvalds  *
16941da177e4SLinus Torvalds  * sync_inode() will write an inode and its pages to disk.  It will also
16951da177e4SLinus Torvalds  * correctly update the inode on its superblock's dirty inode lists and will
16961da177e4SLinus Torvalds  * update inode->i_state.
16971da177e4SLinus Torvalds  *
16981da177e4SLinus Torvalds  * The caller must have a ref on the inode.
16991da177e4SLinus Torvalds  */
17001da177e4SLinus Torvalds int sync_inode(struct inode *inode, struct writeback_control *wbc)
17011da177e4SLinus Torvalds {
17024f8ad655SJan Kara 	return writeback_single_inode(inode, &inode_to_bdi(inode)->wb, wbc);
17031da177e4SLinus Torvalds }
17041da177e4SLinus Torvalds EXPORT_SYMBOL(sync_inode);
1705c3765016SChristoph Hellwig 
1706c3765016SChristoph Hellwig /**
1707c691b9d9SAndrew Morton  * sync_inode_metadata - write an inode to disk
1708c3765016SChristoph Hellwig  * @inode: the inode to sync
1709c3765016SChristoph Hellwig  * @wait: wait for I/O to complete.
1710c3765016SChristoph Hellwig  *
1711c691b9d9SAndrew Morton  * Write an inode to disk and adjust its dirty state after completion.
1712c3765016SChristoph Hellwig  *
1713c3765016SChristoph Hellwig  * Note: only writes the actual inode, no associated data or other metadata.
1714c3765016SChristoph Hellwig  */
1715c3765016SChristoph Hellwig int sync_inode_metadata(struct inode *inode, int wait)
1716c3765016SChristoph Hellwig {
1717c3765016SChristoph Hellwig 	struct writeback_control wbc = {
1718c3765016SChristoph Hellwig 		.sync_mode = wait ? WB_SYNC_ALL : WB_SYNC_NONE,
1719c3765016SChristoph Hellwig 		.nr_to_write = 0, /* metadata-only */
1720c3765016SChristoph Hellwig 	};
1721c3765016SChristoph Hellwig 
1722c3765016SChristoph Hellwig 	return sync_inode(inode, &wbc);
1723c3765016SChristoph Hellwig }
1724c3765016SChristoph Hellwig EXPORT_SYMBOL(sync_inode_metadata);
1725