xref: /openbmc/linux/fs/fs-writeback.c (revision ac7b19a3)
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 
37bc31b86aSWu Fengguang /*
38c4a77a6cSJens Axboe  * Passed into wb_writeback(), essentially a subset of writeback_control
39c4a77a6cSJens Axboe  */
4083ba7b07SChristoph Hellwig struct wb_writeback_work {
41c4a77a6cSJens Axboe 	long nr_pages;
42c4a77a6cSJens Axboe 	struct super_block *sb;
430dc83bd3SJan Kara 	unsigned long *older_than_this;
44c4a77a6cSJens Axboe 	enum writeback_sync_modes sync_mode;
456e6938b6SWu Fengguang 	unsigned int tagged_writepages:1;
4652957fe1SH Hartley Sweeten 	unsigned int for_kupdate:1;
4752957fe1SH Hartley Sweeten 	unsigned int range_cyclic:1;
4852957fe1SH Hartley Sweeten 	unsigned int for_background:1;
497747bd4bSDave Chinner 	unsigned int for_sync:1;	/* sync(2) WB_SYNC_ALL writeback */
50ac7b19a3STejun Heo 	unsigned int auto_free:1;	/* free on completion */
510e175a18SCurt Wohlgemuth 	enum wb_reason reason;		/* why was writeback initiated? */
52c4a77a6cSJens Axboe 
538010c3b6SJens Axboe 	struct list_head list;		/* pending work list */
5483ba7b07SChristoph Hellwig 	struct completion *done;	/* set if the caller waits */
5503ba3782SJens Axboe };
5603ba3782SJens Axboe 
57a2f48706STheodore Ts'o /*
58a2f48706STheodore Ts'o  * If an inode is constantly having its pages dirtied, but then the
59a2f48706STheodore Ts'o  * updates stop dirtytime_expire_interval seconds in the past, it's
60a2f48706STheodore Ts'o  * possible for the worst case time between when an inode has its
61a2f48706STheodore Ts'o  * timestamps updated and when they finally get written out to be two
62a2f48706STheodore Ts'o  * dirtytime_expire_intervals.  We set the default to 12 hours (in
63a2f48706STheodore Ts'o  * seconds), which means most of the time inodes will have their
64a2f48706STheodore Ts'o  * timestamps written to disk after 12 hours, but in the worst case a
65a2f48706STheodore Ts'o  * few inodes might not their timestamps updated for 24 hours.
66a2f48706STheodore Ts'o  */
67a2f48706STheodore Ts'o unsigned int dirtytime_expire_interval = 12 * 60 * 60;
68a2f48706STheodore Ts'o 
697ccf19a8SNick Piggin static inline struct inode *wb_inode(struct list_head *head)
707ccf19a8SNick Piggin {
717ccf19a8SNick Piggin 	return list_entry(head, struct inode, i_wb_list);
727ccf19a8SNick Piggin }
737ccf19a8SNick Piggin 
7415eb77a0SWu Fengguang /*
7515eb77a0SWu Fengguang  * Include the creation of the trace points after defining the
7615eb77a0SWu Fengguang  * wb_writeback_work structure and inline functions so that the definition
7715eb77a0SWu Fengguang  * remains local to this file.
7815eb77a0SWu Fengguang  */
7915eb77a0SWu Fengguang #define CREATE_TRACE_POINTS
8015eb77a0SWu Fengguang #include <trace/events/writeback.h>
8115eb77a0SWu Fengguang 
82774016b2SSteven Whitehouse EXPORT_TRACEPOINT_SYMBOL_GPL(wbc_writepage);
83774016b2SSteven Whitehouse 
84d6c10f1fSTejun Heo static bool wb_io_lists_populated(struct bdi_writeback *wb)
85d6c10f1fSTejun Heo {
86d6c10f1fSTejun Heo 	if (wb_has_dirty_io(wb)) {
87d6c10f1fSTejun Heo 		return false;
88d6c10f1fSTejun Heo 	} else {
89d6c10f1fSTejun Heo 		set_bit(WB_has_dirty_io, &wb->state);
9095a46c65STejun Heo 		WARN_ON_ONCE(!wb->avg_write_bandwidth);
91766a9d6eSTejun Heo 		atomic_long_add(wb->avg_write_bandwidth,
92766a9d6eSTejun Heo 				&wb->bdi->tot_write_bandwidth);
93d6c10f1fSTejun Heo 		return true;
94d6c10f1fSTejun Heo 	}
95d6c10f1fSTejun Heo }
96d6c10f1fSTejun Heo 
97d6c10f1fSTejun Heo static void wb_io_lists_depopulated(struct bdi_writeback *wb)
98d6c10f1fSTejun Heo {
99d6c10f1fSTejun Heo 	if (wb_has_dirty_io(wb) && list_empty(&wb->b_dirty) &&
100766a9d6eSTejun Heo 	    list_empty(&wb->b_io) && list_empty(&wb->b_more_io)) {
101d6c10f1fSTejun Heo 		clear_bit(WB_has_dirty_io, &wb->state);
10295a46c65STejun Heo 		WARN_ON_ONCE(atomic_long_sub_return(wb->avg_write_bandwidth,
10395a46c65STejun Heo 					&wb->bdi->tot_write_bandwidth) < 0);
104766a9d6eSTejun Heo 	}
105d6c10f1fSTejun Heo }
106d6c10f1fSTejun Heo 
107d6c10f1fSTejun Heo /**
108d6c10f1fSTejun Heo  * inode_wb_list_move_locked - move an inode onto a bdi_writeback IO list
109d6c10f1fSTejun Heo  * @inode: inode to be moved
110d6c10f1fSTejun Heo  * @wb: target bdi_writeback
111d6c10f1fSTejun Heo  * @head: one of @wb->b_{dirty|io|more_io}
112d6c10f1fSTejun Heo  *
113d6c10f1fSTejun Heo  * Move @inode->i_wb_list to @list of @wb and set %WB_has_dirty_io.
114d6c10f1fSTejun Heo  * Returns %true if @inode is the first occupant of the !dirty_time IO
115d6c10f1fSTejun Heo  * lists; otherwise, %false.
116d6c10f1fSTejun Heo  */
117d6c10f1fSTejun Heo static bool inode_wb_list_move_locked(struct inode *inode,
118d6c10f1fSTejun Heo 				      struct bdi_writeback *wb,
119d6c10f1fSTejun Heo 				      struct list_head *head)
120d6c10f1fSTejun Heo {
121d6c10f1fSTejun Heo 	assert_spin_locked(&wb->list_lock);
122d6c10f1fSTejun Heo 
123d6c10f1fSTejun Heo 	list_move(&inode->i_wb_list, head);
124d6c10f1fSTejun Heo 
125d6c10f1fSTejun Heo 	/* dirty_time doesn't count as dirty_io until expiration */
126d6c10f1fSTejun Heo 	if (head != &wb->b_dirty_time)
127d6c10f1fSTejun Heo 		return wb_io_lists_populated(wb);
128d6c10f1fSTejun Heo 
129d6c10f1fSTejun Heo 	wb_io_lists_depopulated(wb);
130d6c10f1fSTejun Heo 	return false;
131d6c10f1fSTejun Heo }
132d6c10f1fSTejun Heo 
133d6c10f1fSTejun Heo /**
134d6c10f1fSTejun Heo  * inode_wb_list_del_locked - remove an inode from its bdi_writeback IO list
135d6c10f1fSTejun Heo  * @inode: inode to be removed
136d6c10f1fSTejun Heo  * @wb: bdi_writeback @inode is being removed from
137d6c10f1fSTejun Heo  *
138d6c10f1fSTejun Heo  * Remove @inode which may be on one of @wb->b_{dirty|io|more_io} lists and
139d6c10f1fSTejun Heo  * clear %WB_has_dirty_io if all are empty afterwards.
140d6c10f1fSTejun Heo  */
141d6c10f1fSTejun Heo static void inode_wb_list_del_locked(struct inode *inode,
142d6c10f1fSTejun Heo 				     struct bdi_writeback *wb)
143d6c10f1fSTejun Heo {
144d6c10f1fSTejun Heo 	assert_spin_locked(&wb->list_lock);
145d6c10f1fSTejun Heo 
146d6c10f1fSTejun Heo 	list_del_init(&inode->i_wb_list);
147d6c10f1fSTejun Heo 	wb_io_lists_depopulated(wb);
148d6c10f1fSTejun Heo }
149d6c10f1fSTejun Heo 
150f0054bb1STejun Heo static void wb_wakeup(struct bdi_writeback *wb)
1515acda9d1SJan Kara {
152f0054bb1STejun Heo 	spin_lock_bh(&wb->work_lock);
153f0054bb1STejun Heo 	if (test_bit(WB_registered, &wb->state))
154f0054bb1STejun Heo 		mod_delayed_work(bdi_wq, &wb->dwork, 0);
155f0054bb1STejun Heo 	spin_unlock_bh(&wb->work_lock);
1565acda9d1SJan Kara }
1575acda9d1SJan Kara 
158f0054bb1STejun Heo static void wb_queue_work(struct bdi_writeback *wb,
1596585027aSJan Kara 			  struct wb_writeback_work *work)
1606585027aSJan Kara {
161f0054bb1STejun Heo 	trace_writeback_queue(wb->bdi, work);
1626585027aSJan Kara 
163f0054bb1STejun Heo 	spin_lock_bh(&wb->work_lock);
164f0054bb1STejun Heo 	if (!test_bit(WB_registered, &wb->state)) {
1655acda9d1SJan Kara 		if (work->done)
1665acda9d1SJan Kara 			complete(work->done);
1675acda9d1SJan Kara 		goto out_unlock;
1685acda9d1SJan Kara 	}
169f0054bb1STejun Heo 	list_add_tail(&work->list, &wb->work_list);
170f0054bb1STejun Heo 	mod_delayed_work(bdi_wq, &wb->dwork, 0);
1715acda9d1SJan Kara out_unlock:
172f0054bb1STejun Heo 	spin_unlock_bh(&wb->work_lock);
17303ba3782SJens Axboe }
1741da177e4SLinus Torvalds 
175703c2708STejun Heo #ifdef CONFIG_CGROUP_WRITEBACK
176703c2708STejun Heo 
177703c2708STejun Heo /**
178703c2708STejun Heo  * inode_congested - test whether an inode is congested
179703c2708STejun Heo  * @inode: inode to test for congestion
180703c2708STejun Heo  * @cong_bits: mask of WB_[a]sync_congested bits to test
181703c2708STejun Heo  *
182703c2708STejun Heo  * Tests whether @inode is congested.  @cong_bits is the mask of congestion
183703c2708STejun Heo  * bits to test and the return value is the mask of set bits.
184703c2708STejun Heo  *
185703c2708STejun Heo  * If cgroup writeback is enabled for @inode, the congestion state is
186703c2708STejun Heo  * determined by whether the cgwb (cgroup bdi_writeback) for the blkcg
187703c2708STejun Heo  * associated with @inode is congested; otherwise, the root wb's congestion
188703c2708STejun Heo  * state is used.
189703c2708STejun Heo  */
190703c2708STejun Heo int inode_congested(struct inode *inode, int cong_bits)
191703c2708STejun Heo {
192703c2708STejun Heo 	if (inode) {
193703c2708STejun Heo 		struct bdi_writeback *wb = inode_to_wb(inode);
194703c2708STejun Heo 		if (wb)
195703c2708STejun Heo 			return wb_congested(wb, cong_bits);
196703c2708STejun Heo 	}
197703c2708STejun Heo 
198703c2708STejun Heo 	return wb_congested(&inode_to_bdi(inode)->wb, cong_bits);
199703c2708STejun Heo }
200703c2708STejun Heo EXPORT_SYMBOL_GPL(inode_congested);
201703c2708STejun Heo 
202f2b65121STejun Heo /**
203f2b65121STejun Heo  * wb_split_bdi_pages - split nr_pages to write according to bandwidth
204f2b65121STejun Heo  * @wb: target bdi_writeback to split @nr_pages to
205f2b65121STejun Heo  * @nr_pages: number of pages to write for the whole bdi
206f2b65121STejun Heo  *
207f2b65121STejun Heo  * Split @wb's portion of @nr_pages according to @wb's write bandwidth in
208f2b65121STejun Heo  * relation to the total write bandwidth of all wb's w/ dirty inodes on
209f2b65121STejun Heo  * @wb->bdi.
210f2b65121STejun Heo  */
211f2b65121STejun Heo static long wb_split_bdi_pages(struct bdi_writeback *wb, long nr_pages)
212f2b65121STejun Heo {
213f2b65121STejun Heo 	unsigned long this_bw = wb->avg_write_bandwidth;
214f2b65121STejun Heo 	unsigned long tot_bw = atomic_long_read(&wb->bdi->tot_write_bandwidth);
215f2b65121STejun Heo 
216f2b65121STejun Heo 	if (nr_pages == LONG_MAX)
217f2b65121STejun Heo 		return LONG_MAX;
218f2b65121STejun Heo 
219f2b65121STejun Heo 	/*
220f2b65121STejun Heo 	 * This may be called on clean wb's and proportional distribution
221f2b65121STejun Heo 	 * may not make sense, just use the original @nr_pages in those
222f2b65121STejun Heo 	 * cases.  In general, we wanna err on the side of writing more.
223f2b65121STejun Heo 	 */
224f2b65121STejun Heo 	if (!tot_bw || this_bw >= tot_bw)
225f2b65121STejun Heo 		return nr_pages;
226f2b65121STejun Heo 	else
227f2b65121STejun Heo 		return DIV_ROUND_UP_ULL((u64)nr_pages * this_bw, tot_bw);
228f2b65121STejun Heo }
229f2b65121STejun Heo 
230f2b65121STejun Heo #else	/* CONFIG_CGROUP_WRITEBACK */
231f2b65121STejun Heo 
232f2b65121STejun Heo static long wb_split_bdi_pages(struct bdi_writeback *wb, long nr_pages)
233f2b65121STejun Heo {
234f2b65121STejun Heo 	return nr_pages;
235f2b65121STejun Heo }
236f2b65121STejun Heo 
237703c2708STejun Heo #endif	/* CONFIG_CGROUP_WRITEBACK */
238703c2708STejun Heo 
239c00ddad3STejun Heo void wb_start_writeback(struct bdi_writeback *wb, long nr_pages,
240c00ddad3STejun Heo 			bool range_cyclic, enum wb_reason reason)
241b6e51316SJens Axboe {
242c00ddad3STejun Heo 	struct wb_writeback_work *work;
243c00ddad3STejun Heo 
244c00ddad3STejun Heo 	if (!wb_has_dirty_io(wb))
245c00ddad3STejun Heo 		return;
246c00ddad3STejun Heo 
247c00ddad3STejun Heo 	/*
248c00ddad3STejun Heo 	 * This is WB_SYNC_NONE writeback, so if allocation fails just
249c00ddad3STejun Heo 	 * wakeup the thread for old dirty data writeback
250c00ddad3STejun Heo 	 */
251c00ddad3STejun Heo 	work = kzalloc(sizeof(*work), GFP_ATOMIC);
252c00ddad3STejun Heo 	if (!work) {
253c00ddad3STejun Heo 		trace_writeback_nowork(wb->bdi);
254c00ddad3STejun Heo 		wb_wakeup(wb);
255c00ddad3STejun Heo 		return;
256c00ddad3STejun Heo 	}
257c00ddad3STejun Heo 
258c00ddad3STejun Heo 	work->sync_mode	= WB_SYNC_NONE;
259c00ddad3STejun Heo 	work->nr_pages	= nr_pages;
260c00ddad3STejun Heo 	work->range_cyclic = range_cyclic;
261c00ddad3STejun Heo 	work->reason	= reason;
262ac7b19a3STejun Heo 	work->auto_free	= 1;
263c00ddad3STejun Heo 
264c00ddad3STejun Heo 	wb_queue_work(wb, work);
265d3ddec76SWu Fengguang }
266d3ddec76SWu Fengguang 
267c5444198SChristoph Hellwig /**
2689ecf4866STejun Heo  * wb_start_background_writeback - start background writeback
2699ecf4866STejun Heo  * @wb: bdi_writback to write from
270c5444198SChristoph Hellwig  *
271c5444198SChristoph Hellwig  * Description:
2726585027aSJan Kara  *   This makes sure WB_SYNC_NONE background writeback happens. When
2739ecf4866STejun Heo  *   this function returns, it is only guaranteed that for given wb
2746585027aSJan Kara  *   some IO is happening if we are over background dirty threshold.
2756585027aSJan Kara  *   Caller need not hold sb s_umount semaphore.
276c5444198SChristoph Hellwig  */
2779ecf4866STejun Heo void wb_start_background_writeback(struct bdi_writeback *wb)
278c5444198SChristoph Hellwig {
2796585027aSJan Kara 	/*
2806585027aSJan Kara 	 * We just wake up the flusher thread. It will perform background
2816585027aSJan Kara 	 * writeback as soon as there is no other work to do.
2826585027aSJan Kara 	 */
2839ecf4866STejun Heo 	trace_writeback_wake_background(wb->bdi);
2849ecf4866STejun Heo 	wb_wakeup(wb);
2851da177e4SLinus Torvalds }
2861da177e4SLinus Torvalds 
2871da177e4SLinus Torvalds /*
288a66979abSDave Chinner  * Remove the inode from the writeback list it is on.
289a66979abSDave Chinner  */
290a66979abSDave Chinner void inode_wb_list_del(struct inode *inode)
291a66979abSDave Chinner {
29252ebea74STejun Heo 	struct bdi_writeback *wb = inode_to_wb(inode);
293a66979abSDave Chinner 
29452ebea74STejun Heo 	spin_lock(&wb->list_lock);
295d6c10f1fSTejun Heo 	inode_wb_list_del_locked(inode, wb);
29652ebea74STejun Heo 	spin_unlock(&wb->list_lock);
297f758eeabSChristoph Hellwig }
298a66979abSDave Chinner 
299a66979abSDave Chinner /*
3006610a0bcSAndrew Morton  * Redirty an inode: set its when-it-was dirtied timestamp and move it to the
3016610a0bcSAndrew Morton  * furthest end of its superblock's dirty-inode list.
3026610a0bcSAndrew Morton  *
3036610a0bcSAndrew Morton  * Before stamping the inode's ->dirtied_when, we check to see whether it is
30466f3b8e2SJens Axboe  * already the most-recently-dirtied inode on the b_dirty list.  If that is
3056610a0bcSAndrew Morton  * the case then the inode must have been redirtied while it was being written
3066610a0bcSAndrew Morton  * out and we don't reset its dirtied_when.
3076610a0bcSAndrew Morton  */
308f758eeabSChristoph Hellwig static void redirty_tail(struct inode *inode, struct bdi_writeback *wb)
3096610a0bcSAndrew Morton {
31003ba3782SJens Axboe 	if (!list_empty(&wb->b_dirty)) {
31166f3b8e2SJens Axboe 		struct inode *tail;
3126610a0bcSAndrew Morton 
3137ccf19a8SNick Piggin 		tail = wb_inode(wb->b_dirty.next);
31466f3b8e2SJens Axboe 		if (time_before(inode->dirtied_when, tail->dirtied_when))
3156610a0bcSAndrew Morton 			inode->dirtied_when = jiffies;
3166610a0bcSAndrew Morton 	}
317d6c10f1fSTejun Heo 	inode_wb_list_move_locked(inode, wb, &wb->b_dirty);
3186610a0bcSAndrew Morton }
3196610a0bcSAndrew Morton 
3206610a0bcSAndrew Morton /*
32166f3b8e2SJens Axboe  * requeue inode for re-scanning after bdi->b_io list is exhausted.
322c986d1e2SAndrew Morton  */
323f758eeabSChristoph Hellwig static void requeue_io(struct inode *inode, struct bdi_writeback *wb)
324c986d1e2SAndrew Morton {
325d6c10f1fSTejun Heo 	inode_wb_list_move_locked(inode, wb, &wb->b_more_io);
326c986d1e2SAndrew Morton }
327c986d1e2SAndrew Morton 
3281c0eeaf5SJoern Engel static void inode_sync_complete(struct inode *inode)
3291c0eeaf5SJoern Engel {
330365b94aeSJan Kara 	inode->i_state &= ~I_SYNC;
3314eff96ddSJan Kara 	/* If inode is clean an unused, put it into LRU now... */
3324eff96ddSJan Kara 	inode_add_lru(inode);
333365b94aeSJan Kara 	/* Waiters must see I_SYNC cleared before being woken up */
3341c0eeaf5SJoern Engel 	smp_mb();
3351c0eeaf5SJoern Engel 	wake_up_bit(&inode->i_state, __I_SYNC);
3361c0eeaf5SJoern Engel }
3371c0eeaf5SJoern Engel 
338d2caa3c5SJeff Layton static bool inode_dirtied_after(struct inode *inode, unsigned long t)
339d2caa3c5SJeff Layton {
340d2caa3c5SJeff Layton 	bool ret = time_after(inode->dirtied_when, t);
341d2caa3c5SJeff Layton #ifndef CONFIG_64BIT
342d2caa3c5SJeff Layton 	/*
343d2caa3c5SJeff Layton 	 * For inodes being constantly redirtied, dirtied_when can get stuck.
344d2caa3c5SJeff Layton 	 * It _appears_ to be in the future, but is actually in distant past.
345d2caa3c5SJeff Layton 	 * This test is necessary to prevent such wrapped-around relative times
3465b0830cbSJens Axboe 	 * from permanently stopping the whole bdi writeback.
347d2caa3c5SJeff Layton 	 */
348d2caa3c5SJeff Layton 	ret = ret && time_before_eq(inode->dirtied_when, jiffies);
349d2caa3c5SJeff Layton #endif
350d2caa3c5SJeff Layton 	return ret;
351d2caa3c5SJeff Layton }
352d2caa3c5SJeff Layton 
3530ae45f63STheodore Ts'o #define EXPIRE_DIRTY_ATIME 0x0001
3540ae45f63STheodore Ts'o 
355c986d1e2SAndrew Morton /*
3560e2f2b23SWang Sheng-Hui  * Move expired (dirtied before work->older_than_this) dirty inodes from
357697e6fedSJan Kara  * @delaying_queue to @dispatch_queue.
3582c136579SFengguang Wu  */
359e84d0a4fSWu Fengguang static int move_expired_inodes(struct list_head *delaying_queue,
3602c136579SFengguang Wu 			       struct list_head *dispatch_queue,
3610ae45f63STheodore Ts'o 			       int flags,
362ad4e38ddSCurt Wohlgemuth 			       struct wb_writeback_work *work)
3632c136579SFengguang Wu {
3640ae45f63STheodore Ts'o 	unsigned long *older_than_this = NULL;
3650ae45f63STheodore Ts'o 	unsigned long expire_time;
3665c03449dSShaohua Li 	LIST_HEAD(tmp);
3675c03449dSShaohua Li 	struct list_head *pos, *node;
368cf137307SJens Axboe 	struct super_block *sb = NULL;
3695c03449dSShaohua Li 	struct inode *inode;
370cf137307SJens Axboe 	int do_sb_sort = 0;
371e84d0a4fSWu Fengguang 	int moved = 0;
3725c03449dSShaohua Li 
3730ae45f63STheodore Ts'o 	if ((flags & EXPIRE_DIRTY_ATIME) == 0)
3740ae45f63STheodore Ts'o 		older_than_this = work->older_than_this;
375a2f48706STheodore Ts'o 	else if (!work->for_sync) {
376a2f48706STheodore Ts'o 		expire_time = jiffies - (dirtytime_expire_interval * HZ);
3770ae45f63STheodore Ts'o 		older_than_this = &expire_time;
3780ae45f63STheodore Ts'o 	}
3792c136579SFengguang Wu 	while (!list_empty(delaying_queue)) {
3807ccf19a8SNick Piggin 		inode = wb_inode(delaying_queue->prev);
3810ae45f63STheodore Ts'o 		if (older_than_this &&
3820ae45f63STheodore Ts'o 		    inode_dirtied_after(inode, *older_than_this))
3832c136579SFengguang Wu 			break;
384a8855990SJan Kara 		list_move(&inode->i_wb_list, &tmp);
385a8855990SJan Kara 		moved++;
3860ae45f63STheodore Ts'o 		if (flags & EXPIRE_DIRTY_ATIME)
3870ae45f63STheodore Ts'o 			set_bit(__I_DIRTY_TIME_EXPIRED, &inode->i_state);
388a8855990SJan Kara 		if (sb_is_blkdev_sb(inode->i_sb))
389a8855990SJan Kara 			continue;
390cf137307SJens Axboe 		if (sb && sb != inode->i_sb)
391cf137307SJens Axboe 			do_sb_sort = 1;
392cf137307SJens Axboe 		sb = inode->i_sb;
3935c03449dSShaohua Li 	}
3945c03449dSShaohua Li 
395cf137307SJens Axboe 	/* just one sb in list, splice to dispatch_queue and we're done */
396cf137307SJens Axboe 	if (!do_sb_sort) {
397cf137307SJens Axboe 		list_splice(&tmp, dispatch_queue);
398e84d0a4fSWu Fengguang 		goto out;
399cf137307SJens Axboe 	}
400cf137307SJens Axboe 
4015c03449dSShaohua Li 	/* Move inodes from one superblock together */
4025c03449dSShaohua Li 	while (!list_empty(&tmp)) {
4037ccf19a8SNick Piggin 		sb = wb_inode(tmp.prev)->i_sb;
4045c03449dSShaohua Li 		list_for_each_prev_safe(pos, node, &tmp) {
4057ccf19a8SNick Piggin 			inode = wb_inode(pos);
4065c03449dSShaohua Li 			if (inode->i_sb == sb)
4077ccf19a8SNick Piggin 				list_move(&inode->i_wb_list, dispatch_queue);
4082c136579SFengguang Wu 		}
4092c136579SFengguang Wu 	}
410e84d0a4fSWu Fengguang out:
411e84d0a4fSWu Fengguang 	return moved;
4125c03449dSShaohua Li }
4132c136579SFengguang Wu 
4142c136579SFengguang Wu /*
4152c136579SFengguang Wu  * Queue all expired dirty inodes for io, eldest first.
4164ea879b9SWu Fengguang  * Before
4174ea879b9SWu Fengguang  *         newly dirtied     b_dirty    b_io    b_more_io
4184ea879b9SWu Fengguang  *         =============>    gf         edc     BA
4194ea879b9SWu Fengguang  * After
4204ea879b9SWu Fengguang  *         newly dirtied     b_dirty    b_io    b_more_io
4214ea879b9SWu Fengguang  *         =============>    g          fBAedc
4224ea879b9SWu Fengguang  *                                           |
4234ea879b9SWu Fengguang  *                                           +--> dequeue for IO
4242c136579SFengguang Wu  */
425ad4e38ddSCurt Wohlgemuth static void queue_io(struct bdi_writeback *wb, struct wb_writeback_work *work)
4262c136579SFengguang Wu {
427e84d0a4fSWu Fengguang 	int moved;
4280ae45f63STheodore Ts'o 
429f758eeabSChristoph Hellwig 	assert_spin_locked(&wb->list_lock);
4304ea879b9SWu Fengguang 	list_splice_init(&wb->b_more_io, &wb->b_io);
4310ae45f63STheodore Ts'o 	moved = move_expired_inodes(&wb->b_dirty, &wb->b_io, 0, work);
4320ae45f63STheodore Ts'o 	moved += move_expired_inodes(&wb->b_dirty_time, &wb->b_io,
4330ae45f63STheodore Ts'o 				     EXPIRE_DIRTY_ATIME, work);
434d6c10f1fSTejun Heo 	if (moved)
435d6c10f1fSTejun Heo 		wb_io_lists_populated(wb);
436ad4e38ddSCurt Wohlgemuth 	trace_writeback_queue_io(wb, work, moved);
43766f3b8e2SJens Axboe }
43866f3b8e2SJens Axboe 
439a9185b41SChristoph Hellwig static int write_inode(struct inode *inode, struct writeback_control *wbc)
44066f3b8e2SJens Axboe {
4419fb0a7daSTejun Heo 	int ret;
4429fb0a7daSTejun Heo 
4439fb0a7daSTejun Heo 	if (inode->i_sb->s_op->write_inode && !is_bad_inode(inode)) {
4449fb0a7daSTejun Heo 		trace_writeback_write_inode_start(inode, wbc);
4459fb0a7daSTejun Heo 		ret = inode->i_sb->s_op->write_inode(inode, wbc);
4469fb0a7daSTejun Heo 		trace_writeback_write_inode(inode, wbc);
4479fb0a7daSTejun Heo 		return ret;
4489fb0a7daSTejun Heo 	}
44903ba3782SJens Axboe 	return 0;
45066f3b8e2SJens Axboe }
45108d8e974SFengguang Wu 
4522c136579SFengguang Wu /*
453169ebd90SJan Kara  * Wait for writeback on an inode to complete. Called with i_lock held.
454169ebd90SJan Kara  * Caller must make sure inode cannot go away when we drop i_lock.
45501c03194SChristoph Hellwig  */
456169ebd90SJan Kara static void __inode_wait_for_writeback(struct inode *inode)
457169ebd90SJan Kara 	__releases(inode->i_lock)
458169ebd90SJan Kara 	__acquires(inode->i_lock)
45901c03194SChristoph Hellwig {
46001c03194SChristoph Hellwig 	DEFINE_WAIT_BIT(wq, &inode->i_state, __I_SYNC);
46101c03194SChristoph Hellwig 	wait_queue_head_t *wqh;
46201c03194SChristoph Hellwig 
46301c03194SChristoph Hellwig 	wqh = bit_waitqueue(&inode->i_state, __I_SYNC);
46458a9d3d8SRichard Kennedy 	while (inode->i_state & I_SYNC) {
465250df6edSDave Chinner 		spin_unlock(&inode->i_lock);
46674316201SNeilBrown 		__wait_on_bit(wqh, &wq, bit_wait,
46774316201SNeilBrown 			      TASK_UNINTERRUPTIBLE);
468250df6edSDave Chinner 		spin_lock(&inode->i_lock);
46958a9d3d8SRichard Kennedy 	}
47001c03194SChristoph Hellwig }
47101c03194SChristoph Hellwig 
47201c03194SChristoph Hellwig /*
473169ebd90SJan Kara  * Wait for writeback on an inode to complete. Caller must have inode pinned.
474169ebd90SJan Kara  */
475169ebd90SJan Kara void inode_wait_for_writeback(struct inode *inode)
476169ebd90SJan Kara {
477169ebd90SJan Kara 	spin_lock(&inode->i_lock);
478169ebd90SJan Kara 	__inode_wait_for_writeback(inode);
479169ebd90SJan Kara 	spin_unlock(&inode->i_lock);
480169ebd90SJan Kara }
481169ebd90SJan Kara 
482169ebd90SJan Kara /*
483169ebd90SJan Kara  * Sleep until I_SYNC is cleared. This function must be called with i_lock
484169ebd90SJan Kara  * held and drops it. It is aimed for callers not holding any inode reference
485169ebd90SJan Kara  * so once i_lock is dropped, inode can go away.
486169ebd90SJan Kara  */
487169ebd90SJan Kara static void inode_sleep_on_writeback(struct inode *inode)
488169ebd90SJan Kara 	__releases(inode->i_lock)
489169ebd90SJan Kara {
490169ebd90SJan Kara 	DEFINE_WAIT(wait);
491169ebd90SJan Kara 	wait_queue_head_t *wqh = bit_waitqueue(&inode->i_state, __I_SYNC);
492169ebd90SJan Kara 	int sleep;
493169ebd90SJan Kara 
494169ebd90SJan Kara 	prepare_to_wait(wqh, &wait, TASK_UNINTERRUPTIBLE);
495169ebd90SJan Kara 	sleep = inode->i_state & I_SYNC;
496169ebd90SJan Kara 	spin_unlock(&inode->i_lock);
497169ebd90SJan Kara 	if (sleep)
498169ebd90SJan Kara 		schedule();
499169ebd90SJan Kara 	finish_wait(wqh, &wait);
500169ebd90SJan Kara }
501169ebd90SJan Kara 
502169ebd90SJan Kara /*
503ccb26b5aSJan Kara  * Find proper writeback list for the inode depending on its current state and
504ccb26b5aSJan Kara  * possibly also change of its state while we were doing writeback.  Here we
505ccb26b5aSJan Kara  * handle things such as livelock prevention or fairness of writeback among
506ccb26b5aSJan Kara  * inodes. This function can be called only by flusher thread - noone else
507ccb26b5aSJan Kara  * processes all inodes in writeback lists and requeueing inodes behind flusher
508ccb26b5aSJan Kara  * thread's back can have unexpected consequences.
509ccb26b5aSJan Kara  */
510ccb26b5aSJan Kara static void requeue_inode(struct inode *inode, struct bdi_writeback *wb,
511ccb26b5aSJan Kara 			  struct writeback_control *wbc)
512ccb26b5aSJan Kara {
513ccb26b5aSJan Kara 	if (inode->i_state & I_FREEING)
514ccb26b5aSJan Kara 		return;
515ccb26b5aSJan Kara 
516ccb26b5aSJan Kara 	/*
517ccb26b5aSJan Kara 	 * Sync livelock prevention. Each inode is tagged and synced in one
518ccb26b5aSJan Kara 	 * shot. If still dirty, it will be redirty_tail()'ed below.  Update
519ccb26b5aSJan Kara 	 * the dirty time to prevent enqueue and sync it again.
520ccb26b5aSJan Kara 	 */
521ccb26b5aSJan Kara 	if ((inode->i_state & I_DIRTY) &&
522ccb26b5aSJan Kara 	    (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages))
523ccb26b5aSJan Kara 		inode->dirtied_when = jiffies;
524ccb26b5aSJan Kara 
5254f8ad655SJan Kara 	if (wbc->pages_skipped) {
5264f8ad655SJan Kara 		/*
5274f8ad655SJan Kara 		 * writeback is not making progress due to locked
5284f8ad655SJan Kara 		 * buffers. Skip this inode for now.
5294f8ad655SJan Kara 		 */
5304f8ad655SJan Kara 		redirty_tail(inode, wb);
5314f8ad655SJan Kara 		return;
5324f8ad655SJan Kara 	}
5334f8ad655SJan Kara 
534ccb26b5aSJan Kara 	if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY)) {
535ccb26b5aSJan Kara 		/*
536ccb26b5aSJan Kara 		 * We didn't write back all the pages.  nfs_writepages()
537ccb26b5aSJan Kara 		 * sometimes bales out without doing anything.
538ccb26b5aSJan Kara 		 */
539ccb26b5aSJan Kara 		if (wbc->nr_to_write <= 0) {
540ccb26b5aSJan Kara 			/* Slice used up. Queue for next turn. */
541ccb26b5aSJan Kara 			requeue_io(inode, wb);
542ccb26b5aSJan Kara 		} else {
543ccb26b5aSJan Kara 			/*
544ccb26b5aSJan Kara 			 * Writeback blocked by something other than
545ccb26b5aSJan Kara 			 * congestion. Delay the inode for some time to
546ccb26b5aSJan Kara 			 * avoid spinning on the CPU (100% iowait)
547ccb26b5aSJan Kara 			 * retrying writeback of the dirty page/inode
548ccb26b5aSJan Kara 			 * that cannot be performed immediately.
549ccb26b5aSJan Kara 			 */
550ccb26b5aSJan Kara 			redirty_tail(inode, wb);
551ccb26b5aSJan Kara 		}
552ccb26b5aSJan Kara 	} else if (inode->i_state & I_DIRTY) {
553ccb26b5aSJan Kara 		/*
554ccb26b5aSJan Kara 		 * Filesystems can dirty the inode during writeback operations,
555ccb26b5aSJan Kara 		 * such as delayed allocation during submission or metadata
556ccb26b5aSJan Kara 		 * updates after data IO completion.
557ccb26b5aSJan Kara 		 */
558ccb26b5aSJan Kara 		redirty_tail(inode, wb);
5590ae45f63STheodore Ts'o 	} else if (inode->i_state & I_DIRTY_TIME) {
560a2f48706STheodore Ts'o 		inode->dirtied_when = jiffies;
561d6c10f1fSTejun Heo 		inode_wb_list_move_locked(inode, wb, &wb->b_dirty_time);
562ccb26b5aSJan Kara 	} else {
563ccb26b5aSJan Kara 		/* The inode is clean. Remove from writeback lists. */
564d6c10f1fSTejun Heo 		inode_wb_list_del_locked(inode, wb);
565ccb26b5aSJan Kara 	}
566ccb26b5aSJan Kara }
567ccb26b5aSJan Kara 
568ccb26b5aSJan Kara /*
5694f8ad655SJan Kara  * Write out an inode and its dirty pages. Do not update the writeback list
5704f8ad655SJan Kara  * linkage. That is left to the caller. The caller is also responsible for
5714f8ad655SJan Kara  * setting I_SYNC flag and calling inode_sync_complete() to clear it.
5721da177e4SLinus Torvalds  */
5731da177e4SLinus Torvalds static int
574cd8ed2a4SYan Hong __writeback_single_inode(struct inode *inode, struct writeback_control *wbc)
5751da177e4SLinus Torvalds {
5761da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
577251d6a47SWu Fengguang 	long nr_to_write = wbc->nr_to_write;
57801c03194SChristoph Hellwig 	unsigned dirty;
5791da177e4SLinus Torvalds 	int ret;
5801da177e4SLinus Torvalds 
5814f8ad655SJan Kara 	WARN_ON(!(inode->i_state & I_SYNC));
5821da177e4SLinus Torvalds 
5839fb0a7daSTejun Heo 	trace_writeback_single_inode_start(inode, wbc, nr_to_write);
5849fb0a7daSTejun Heo 
5851da177e4SLinus Torvalds 	ret = do_writepages(mapping, wbc);
5861da177e4SLinus Torvalds 
58726821ed4SChristoph Hellwig 	/*
58826821ed4SChristoph Hellwig 	 * Make sure to wait on the data before writing out the metadata.
58926821ed4SChristoph Hellwig 	 * This is important for filesystems that modify metadata on data
5907747bd4bSDave Chinner 	 * I/O completion. We don't do it for sync(2) writeback because it has a
5917747bd4bSDave Chinner 	 * separate, external IO completion path and ->sync_fs for guaranteeing
5927747bd4bSDave Chinner 	 * inode metadata is written back correctly.
59326821ed4SChristoph Hellwig 	 */
5947747bd4bSDave Chinner 	if (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync) {
59526821ed4SChristoph Hellwig 		int err = filemap_fdatawait(mapping);
5961da177e4SLinus Torvalds 		if (ret == 0)
5971da177e4SLinus Torvalds 			ret = err;
5981da177e4SLinus Torvalds 	}
5991da177e4SLinus Torvalds 
6005547e8aaSDmitry Monakhov 	/*
6015547e8aaSDmitry Monakhov 	 * Some filesystems may redirty the inode during the writeback
6025547e8aaSDmitry Monakhov 	 * due to delalloc, clear dirty metadata flags right before
6035547e8aaSDmitry Monakhov 	 * write_inode()
6045547e8aaSDmitry Monakhov 	 */
605250df6edSDave Chinner 	spin_lock(&inode->i_lock);
6069c6ac78eSTejun Heo 
6075547e8aaSDmitry Monakhov 	dirty = inode->i_state & I_DIRTY;
608a2f48706STheodore Ts'o 	if (inode->i_state & I_DIRTY_TIME) {
609a2f48706STheodore Ts'o 		if ((dirty & (I_DIRTY_SYNC | I_DIRTY_DATASYNC)) ||
610a2f48706STheodore Ts'o 		    unlikely(inode->i_state & I_DIRTY_TIME_EXPIRED) ||
611a2f48706STheodore Ts'o 		    unlikely(time_after(jiffies,
612a2f48706STheodore Ts'o 					(inode->dirtied_time_when +
613a2f48706STheodore Ts'o 					 dirtytime_expire_interval * HZ)))) {
6140ae45f63STheodore Ts'o 			dirty |= I_DIRTY_TIME | I_DIRTY_TIME_EXPIRED;
6150ae45f63STheodore Ts'o 			trace_writeback_lazytime(inode);
6160ae45f63STheodore Ts'o 		}
617a2f48706STheodore Ts'o 	} else
618a2f48706STheodore Ts'o 		inode->i_state &= ~I_DIRTY_TIME_EXPIRED;
6190ae45f63STheodore Ts'o 	inode->i_state &= ~dirty;
6209c6ac78eSTejun Heo 
6219c6ac78eSTejun Heo 	/*
6229c6ac78eSTejun Heo 	 * Paired with smp_mb() in __mark_inode_dirty().  This allows
6239c6ac78eSTejun Heo 	 * __mark_inode_dirty() to test i_state without grabbing i_lock -
6249c6ac78eSTejun Heo 	 * either they see the I_DIRTY bits cleared or we see the dirtied
6259c6ac78eSTejun Heo 	 * inode.
6269c6ac78eSTejun Heo 	 *
6279c6ac78eSTejun Heo 	 * I_DIRTY_PAGES is always cleared together above even if @mapping
6289c6ac78eSTejun Heo 	 * still has dirty pages.  The flag is reinstated after smp_mb() if
6299c6ac78eSTejun Heo 	 * necessary.  This guarantees that either __mark_inode_dirty()
6309c6ac78eSTejun Heo 	 * sees clear I_DIRTY_PAGES or we see PAGECACHE_TAG_DIRTY.
6319c6ac78eSTejun Heo 	 */
6329c6ac78eSTejun Heo 	smp_mb();
6339c6ac78eSTejun Heo 
6349c6ac78eSTejun Heo 	if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
6359c6ac78eSTejun Heo 		inode->i_state |= I_DIRTY_PAGES;
6369c6ac78eSTejun Heo 
637250df6edSDave Chinner 	spin_unlock(&inode->i_lock);
6389c6ac78eSTejun Heo 
6390ae45f63STheodore Ts'o 	if (dirty & I_DIRTY_TIME)
6400ae45f63STheodore Ts'o 		mark_inode_dirty_sync(inode);
64126821ed4SChristoph Hellwig 	/* Don't write the inode if only I_DIRTY_PAGES was set */
6420ae45f63STheodore Ts'o 	if (dirty & ~I_DIRTY_PAGES) {
643a9185b41SChristoph Hellwig 		int err = write_inode(inode, wbc);
6441da177e4SLinus Torvalds 		if (ret == 0)
6451da177e4SLinus Torvalds 			ret = err;
6461da177e4SLinus Torvalds 	}
6474f8ad655SJan Kara 	trace_writeback_single_inode(inode, wbc, nr_to_write);
6484f8ad655SJan Kara 	return ret;
6494f8ad655SJan Kara }
6504f8ad655SJan Kara 
6514f8ad655SJan Kara /*
6524f8ad655SJan Kara  * Write out an inode's dirty pages. Either the caller has an active reference
6534f8ad655SJan Kara  * on the inode or the inode has I_WILL_FREE set.
6544f8ad655SJan Kara  *
6554f8ad655SJan Kara  * This function is designed to be called for writing back one inode which
6564f8ad655SJan Kara  * we go e.g. from filesystem. Flusher thread uses __writeback_single_inode()
6574f8ad655SJan Kara  * and does more profound writeback list handling in writeback_sb_inodes().
6584f8ad655SJan Kara  */
6594f8ad655SJan Kara static int
6604f8ad655SJan Kara writeback_single_inode(struct inode *inode, struct bdi_writeback *wb,
6614f8ad655SJan Kara 		       struct writeback_control *wbc)
6624f8ad655SJan Kara {
6634f8ad655SJan Kara 	int ret = 0;
6644f8ad655SJan Kara 
6654f8ad655SJan Kara 	spin_lock(&inode->i_lock);
6664f8ad655SJan Kara 	if (!atomic_read(&inode->i_count))
6674f8ad655SJan Kara 		WARN_ON(!(inode->i_state & (I_WILL_FREE|I_FREEING)));
6684f8ad655SJan Kara 	else
6694f8ad655SJan Kara 		WARN_ON(inode->i_state & I_WILL_FREE);
6704f8ad655SJan Kara 
6714f8ad655SJan Kara 	if (inode->i_state & I_SYNC) {
6724f8ad655SJan Kara 		if (wbc->sync_mode != WB_SYNC_ALL)
6734f8ad655SJan Kara 			goto out;
6744f8ad655SJan Kara 		/*
675169ebd90SJan Kara 		 * It's a data-integrity sync. We must wait. Since callers hold
676169ebd90SJan Kara 		 * inode reference or inode has I_WILL_FREE set, it cannot go
677169ebd90SJan Kara 		 * away under us.
6784f8ad655SJan Kara 		 */
679169ebd90SJan Kara 		__inode_wait_for_writeback(inode);
6804f8ad655SJan Kara 	}
6814f8ad655SJan Kara 	WARN_ON(inode->i_state & I_SYNC);
6824f8ad655SJan Kara 	/*
683f9b0e058SJan Kara 	 * Skip inode if it is clean and we have no outstanding writeback in
684f9b0e058SJan Kara 	 * WB_SYNC_ALL mode. We don't want to mess with writeback lists in this
685f9b0e058SJan Kara 	 * function since flusher thread may be doing for example sync in
686f9b0e058SJan Kara 	 * parallel and if we move the inode, it could get skipped. So here we
687f9b0e058SJan Kara 	 * make sure inode is on some writeback list and leave it there unless
688f9b0e058SJan Kara 	 * we have completely cleaned the inode.
6894f8ad655SJan Kara 	 */
6900ae45f63STheodore Ts'o 	if (!(inode->i_state & I_DIRTY_ALL) &&
691f9b0e058SJan Kara 	    (wbc->sync_mode != WB_SYNC_ALL ||
692f9b0e058SJan Kara 	     !mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK)))
6934f8ad655SJan Kara 		goto out;
6944f8ad655SJan Kara 	inode->i_state |= I_SYNC;
6954f8ad655SJan Kara 	spin_unlock(&inode->i_lock);
6964f8ad655SJan Kara 
697cd8ed2a4SYan Hong 	ret = __writeback_single_inode(inode, wbc);
6981da177e4SLinus Torvalds 
699f758eeabSChristoph Hellwig 	spin_lock(&wb->list_lock);
700250df6edSDave Chinner 	spin_lock(&inode->i_lock);
7014f8ad655SJan Kara 	/*
7024f8ad655SJan Kara 	 * If inode is clean, remove it from writeback lists. Otherwise don't
7034f8ad655SJan Kara 	 * touch it. See comment above for explanation.
7044f8ad655SJan Kara 	 */
7050ae45f63STheodore Ts'o 	if (!(inode->i_state & I_DIRTY_ALL))
706d6c10f1fSTejun Heo 		inode_wb_list_del_locked(inode, wb);
7074f8ad655SJan Kara 	spin_unlock(&wb->list_lock);
7081c0eeaf5SJoern Engel 	inode_sync_complete(inode);
7094f8ad655SJan Kara out:
7104f8ad655SJan Kara 	spin_unlock(&inode->i_lock);
7111da177e4SLinus Torvalds 	return ret;
7121da177e4SLinus Torvalds }
7131da177e4SLinus Torvalds 
714a88a341aSTejun Heo static long writeback_chunk_size(struct bdi_writeback *wb,
7151a12d8bdSWu Fengguang 				 struct wb_writeback_work *work)
716d46db3d5SWu Fengguang {
717d46db3d5SWu Fengguang 	long pages;
718d46db3d5SWu Fengguang 
719d46db3d5SWu Fengguang 	/*
720d46db3d5SWu Fengguang 	 * WB_SYNC_ALL mode does livelock avoidance by syncing dirty
721d46db3d5SWu Fengguang 	 * inodes/pages in one big loop. Setting wbc.nr_to_write=LONG_MAX
722d46db3d5SWu Fengguang 	 * here avoids calling into writeback_inodes_wb() more than once.
723d46db3d5SWu Fengguang 	 *
724d46db3d5SWu Fengguang 	 * The intended call sequence for WB_SYNC_ALL writeback is:
725d46db3d5SWu Fengguang 	 *
726d46db3d5SWu Fengguang 	 *      wb_writeback()
727d46db3d5SWu Fengguang 	 *          writeback_sb_inodes()       <== called only once
728d46db3d5SWu Fengguang 	 *              write_cache_pages()     <== called once for each inode
729d46db3d5SWu Fengguang 	 *                   (quickly) tag currently dirty pages
730d46db3d5SWu Fengguang 	 *                   (maybe slowly) sync all tagged pages
731d46db3d5SWu Fengguang 	 */
732d46db3d5SWu Fengguang 	if (work->sync_mode == WB_SYNC_ALL || work->tagged_writepages)
733d46db3d5SWu Fengguang 		pages = LONG_MAX;
7341a12d8bdSWu Fengguang 	else {
735a88a341aSTejun Heo 		pages = min(wb->avg_write_bandwidth / 2,
7361a12d8bdSWu Fengguang 			    global_dirty_limit / DIRTY_SCOPE);
7371a12d8bdSWu Fengguang 		pages = min(pages, work->nr_pages);
7381a12d8bdSWu Fengguang 		pages = round_down(pages + MIN_WRITEBACK_PAGES,
7391a12d8bdSWu Fengguang 				   MIN_WRITEBACK_PAGES);
7401a12d8bdSWu Fengguang 	}
741d46db3d5SWu Fengguang 
742d46db3d5SWu Fengguang 	return pages;
743d46db3d5SWu Fengguang }
744d46db3d5SWu Fengguang 
74503ba3782SJens Axboe /*
746f11c9c5cSEdward Shishkin  * Write a portion of b_io inodes which belong to @sb.
747edadfb10SChristoph Hellwig  *
748d46db3d5SWu Fengguang  * Return the number of pages and/or inodes written.
749f11c9c5cSEdward Shishkin  */
750d46db3d5SWu Fengguang static long writeback_sb_inodes(struct super_block *sb,
751d46db3d5SWu Fengguang 				struct bdi_writeback *wb,
752d46db3d5SWu Fengguang 				struct wb_writeback_work *work)
75303ba3782SJens Axboe {
754d46db3d5SWu Fengguang 	struct writeback_control wbc = {
755d46db3d5SWu Fengguang 		.sync_mode		= work->sync_mode,
756d46db3d5SWu Fengguang 		.tagged_writepages	= work->tagged_writepages,
757d46db3d5SWu Fengguang 		.for_kupdate		= work->for_kupdate,
758d46db3d5SWu Fengguang 		.for_background		= work->for_background,
7597747bd4bSDave Chinner 		.for_sync		= work->for_sync,
760d46db3d5SWu Fengguang 		.range_cyclic		= work->range_cyclic,
761d46db3d5SWu Fengguang 		.range_start		= 0,
762d46db3d5SWu Fengguang 		.range_end		= LLONG_MAX,
763d46db3d5SWu Fengguang 	};
764d46db3d5SWu Fengguang 	unsigned long start_time = jiffies;
765d46db3d5SWu Fengguang 	long write_chunk;
766d46db3d5SWu Fengguang 	long wrote = 0;  /* count both pages and inodes */
767d46db3d5SWu Fengguang 
76803ba3782SJens Axboe 	while (!list_empty(&wb->b_io)) {
7697ccf19a8SNick Piggin 		struct inode *inode = wb_inode(wb->b_io.prev);
770edadfb10SChristoph Hellwig 
771edadfb10SChristoph Hellwig 		if (inode->i_sb != sb) {
772d46db3d5SWu Fengguang 			if (work->sb) {
773edadfb10SChristoph Hellwig 				/*
774edadfb10SChristoph Hellwig 				 * We only want to write back data for this
775edadfb10SChristoph Hellwig 				 * superblock, move all inodes not belonging
776edadfb10SChristoph Hellwig 				 * to it back onto the dirty list.
777edadfb10SChristoph Hellwig 				 */
778f758eeabSChristoph Hellwig 				redirty_tail(inode, wb);
77966f3b8e2SJens Axboe 				continue;
78066f3b8e2SJens Axboe 			}
781edadfb10SChristoph Hellwig 
782edadfb10SChristoph Hellwig 			/*
783edadfb10SChristoph Hellwig 			 * The inode belongs to a different superblock.
784edadfb10SChristoph Hellwig 			 * Bounce back to the caller to unpin this and
785edadfb10SChristoph Hellwig 			 * pin the next superblock.
786edadfb10SChristoph Hellwig 			 */
787d46db3d5SWu Fengguang 			break;
788edadfb10SChristoph Hellwig 		}
789edadfb10SChristoph Hellwig 
7909843b76aSChristoph Hellwig 		/*
791331cbdeeSWanpeng Li 		 * Don't bother with new inodes or inodes being freed, first
792331cbdeeSWanpeng Li 		 * kind does not need periodic writeout yet, and for the latter
7939843b76aSChristoph Hellwig 		 * kind writeout is handled by the freer.
7949843b76aSChristoph Hellwig 		 */
795250df6edSDave Chinner 		spin_lock(&inode->i_lock);
7969843b76aSChristoph Hellwig 		if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) {
797250df6edSDave Chinner 			spin_unlock(&inode->i_lock);
798fcc5c222SWu Fengguang 			redirty_tail(inode, wb);
7997ef0d737SNick Piggin 			continue;
8007ef0d737SNick Piggin 		}
801cc1676d9SJan Kara 		if ((inode->i_state & I_SYNC) && wbc.sync_mode != WB_SYNC_ALL) {
802cc1676d9SJan Kara 			/*
803cc1676d9SJan Kara 			 * If this inode is locked for writeback and we are not
804cc1676d9SJan Kara 			 * doing writeback-for-data-integrity, move it to
805cc1676d9SJan Kara 			 * b_more_io so that writeback can proceed with the
806cc1676d9SJan Kara 			 * other inodes on s_io.
807cc1676d9SJan Kara 			 *
808cc1676d9SJan Kara 			 * We'll have another go at writing back this inode
809cc1676d9SJan Kara 			 * when we completed a full scan of b_io.
810cc1676d9SJan Kara 			 */
811cc1676d9SJan Kara 			spin_unlock(&inode->i_lock);
812cc1676d9SJan Kara 			requeue_io(inode, wb);
813cc1676d9SJan Kara 			trace_writeback_sb_inodes_requeue(inode);
814cc1676d9SJan Kara 			continue;
815cc1676d9SJan Kara 		}
816f0d07b7fSJan Kara 		spin_unlock(&wb->list_lock);
817f0d07b7fSJan Kara 
8184f8ad655SJan Kara 		/*
8194f8ad655SJan Kara 		 * We already requeued the inode if it had I_SYNC set and we
8204f8ad655SJan Kara 		 * are doing WB_SYNC_NONE writeback. So this catches only the
8214f8ad655SJan Kara 		 * WB_SYNC_ALL case.
8224f8ad655SJan Kara 		 */
823169ebd90SJan Kara 		if (inode->i_state & I_SYNC) {
824169ebd90SJan Kara 			/* Wait for I_SYNC. This function drops i_lock... */
825169ebd90SJan Kara 			inode_sleep_on_writeback(inode);
826169ebd90SJan Kara 			/* Inode may be gone, start again */
827ead188f9SJan Kara 			spin_lock(&wb->list_lock);
828169ebd90SJan Kara 			continue;
829169ebd90SJan Kara 		}
8304f8ad655SJan Kara 		inode->i_state |= I_SYNC;
8314f8ad655SJan Kara 		spin_unlock(&inode->i_lock);
832169ebd90SJan Kara 
833a88a341aSTejun Heo 		write_chunk = writeback_chunk_size(wb, work);
834d46db3d5SWu Fengguang 		wbc.nr_to_write = write_chunk;
835d46db3d5SWu Fengguang 		wbc.pages_skipped = 0;
836250df6edSDave Chinner 
837169ebd90SJan Kara 		/*
838169ebd90SJan Kara 		 * We use I_SYNC to pin the inode in memory. While it is set
839169ebd90SJan Kara 		 * evict_inode() will wait so the inode cannot be freed.
840169ebd90SJan Kara 		 */
841cd8ed2a4SYan Hong 		__writeback_single_inode(inode, &wbc);
842d46db3d5SWu Fengguang 
843d46db3d5SWu Fengguang 		work->nr_pages -= write_chunk - wbc.nr_to_write;
844d46db3d5SWu Fengguang 		wrote += write_chunk - wbc.nr_to_write;
8454f8ad655SJan Kara 		spin_lock(&wb->list_lock);
8464f8ad655SJan Kara 		spin_lock(&inode->i_lock);
8470ae45f63STheodore Ts'o 		if (!(inode->i_state & I_DIRTY_ALL))
848d46db3d5SWu Fengguang 			wrote++;
8494f8ad655SJan Kara 		requeue_inode(inode, wb, &wbc);
8504f8ad655SJan Kara 		inode_sync_complete(inode);
8510f1b1fd8SDave Chinner 		spin_unlock(&inode->i_lock);
852169ebd90SJan Kara 		cond_resched_lock(&wb->list_lock);
853d46db3d5SWu Fengguang 		/*
854d46db3d5SWu Fengguang 		 * bail out to wb_writeback() often enough to check
855d46db3d5SWu Fengguang 		 * background threshold and other termination conditions.
856d46db3d5SWu Fengguang 		 */
857d46db3d5SWu Fengguang 		if (wrote) {
858d46db3d5SWu Fengguang 			if (time_is_before_jiffies(start_time + HZ / 10UL))
859d46db3d5SWu Fengguang 				break;
860d46db3d5SWu Fengguang 			if (work->nr_pages <= 0)
861d46db3d5SWu Fengguang 				break;
8621da177e4SLinus Torvalds 		}
8638bc3be27SFengguang Wu 	}
864d46db3d5SWu Fengguang 	return wrote;
865f11c9c5cSEdward Shishkin }
86638f21977SNick Piggin 
867d46db3d5SWu Fengguang static long __writeback_inodes_wb(struct bdi_writeback *wb,
868d46db3d5SWu Fengguang 				  struct wb_writeback_work *work)
869f11c9c5cSEdward Shishkin {
870d46db3d5SWu Fengguang 	unsigned long start_time = jiffies;
871d46db3d5SWu Fengguang 	long wrote = 0;
872f11c9c5cSEdward Shishkin 
873f11c9c5cSEdward Shishkin 	while (!list_empty(&wb->b_io)) {
8747ccf19a8SNick Piggin 		struct inode *inode = wb_inode(wb->b_io.prev);
875f11c9c5cSEdward Shishkin 		struct super_block *sb = inode->i_sb;
876f11c9c5cSEdward Shishkin 
877eb6ef3dfSKonstantin Khlebnikov 		if (!trylock_super(sb)) {
8780e995816SWu Fengguang 			/*
879eb6ef3dfSKonstantin Khlebnikov 			 * trylock_super() may fail consistently due to
8800e995816SWu Fengguang 			 * s_umount being grabbed by someone else. Don't use
8810e995816SWu Fengguang 			 * requeue_io() to avoid busy retrying the inode/sb.
8820e995816SWu Fengguang 			 */
8830e995816SWu Fengguang 			redirty_tail(inode, wb);
884d19de7edSChristoph Hellwig 			continue;
885334132aeSChristoph Hellwig 		}
886d46db3d5SWu Fengguang 		wrote += writeback_sb_inodes(sb, wb, work);
887eb6ef3dfSKonstantin Khlebnikov 		up_read(&sb->s_umount);
888f11c9c5cSEdward Shishkin 
889d46db3d5SWu Fengguang 		/* refer to the same tests at the end of writeback_sb_inodes */
890d46db3d5SWu Fengguang 		if (wrote) {
891d46db3d5SWu Fengguang 			if (time_is_before_jiffies(start_time + HZ / 10UL))
892d46db3d5SWu Fengguang 				break;
893d46db3d5SWu Fengguang 			if (work->nr_pages <= 0)
894f11c9c5cSEdward Shishkin 				break;
895f11c9c5cSEdward Shishkin 		}
896d46db3d5SWu Fengguang 	}
89766f3b8e2SJens Axboe 	/* Leave any unwritten inodes on b_io */
898d46db3d5SWu Fengguang 	return wrote;
89966f3b8e2SJens Axboe }
90066f3b8e2SJens Axboe 
9017d9f073bSWanpeng Li static long writeback_inodes_wb(struct bdi_writeback *wb, long nr_pages,
9020e175a18SCurt Wohlgemuth 				enum wb_reason reason)
903edadfb10SChristoph Hellwig {
904d46db3d5SWu Fengguang 	struct wb_writeback_work work = {
905d46db3d5SWu Fengguang 		.nr_pages	= nr_pages,
906d46db3d5SWu Fengguang 		.sync_mode	= WB_SYNC_NONE,
907d46db3d5SWu Fengguang 		.range_cyclic	= 1,
9080e175a18SCurt Wohlgemuth 		.reason		= reason,
909d46db3d5SWu Fengguang 	};
910edadfb10SChristoph Hellwig 
911f758eeabSChristoph Hellwig 	spin_lock(&wb->list_lock);
912424b351fSWu Fengguang 	if (list_empty(&wb->b_io))
913ad4e38ddSCurt Wohlgemuth 		queue_io(wb, &work);
914d46db3d5SWu Fengguang 	__writeback_inodes_wb(wb, &work);
915f758eeabSChristoph Hellwig 	spin_unlock(&wb->list_lock);
916edadfb10SChristoph Hellwig 
917d46db3d5SWu Fengguang 	return nr_pages - work.nr_pages;
91866f3b8e2SJens Axboe }
91966f3b8e2SJens Axboe 
920a88a341aSTejun Heo static bool over_bground_thresh(struct bdi_writeback *wb)
92103ba3782SJens Axboe {
92203ba3782SJens Axboe 	unsigned long background_thresh, dirty_thresh;
92303ba3782SJens Axboe 
92416c4042fSWu Fengguang 	global_dirty_limits(&background_thresh, &dirty_thresh);
92503ba3782SJens Axboe 
926b00949aaSWu Fengguang 	if (global_page_state(NR_FILE_DIRTY) +
927b00949aaSWu Fengguang 	    global_page_state(NR_UNSTABLE_NFS) > background_thresh)
928b00949aaSWu Fengguang 		return true;
929b00949aaSWu Fengguang 
930a88a341aSTejun Heo 	if (wb_stat(wb, WB_RECLAIMABLE) > wb_dirty_limit(wb, background_thresh))
931b00949aaSWu Fengguang 		return true;
932b00949aaSWu Fengguang 
933b00949aaSWu Fengguang 	return false;
93403ba3782SJens Axboe }
93503ba3782SJens Axboe 
93603ba3782SJens Axboe /*
937e98be2d5SWu Fengguang  * Called under wb->list_lock. If there are multiple wb per bdi,
938e98be2d5SWu Fengguang  * only the flusher working on the first wb should do it.
939e98be2d5SWu Fengguang  */
940e98be2d5SWu Fengguang static void wb_update_bandwidth(struct bdi_writeback *wb,
941e98be2d5SWu Fengguang 				unsigned long start_time)
942e98be2d5SWu Fengguang {
943a88a341aSTejun Heo 	__wb_update_bandwidth(wb, 0, 0, 0, 0, 0, start_time);
944e98be2d5SWu Fengguang }
945e98be2d5SWu Fengguang 
946e98be2d5SWu Fengguang /*
94703ba3782SJens Axboe  * Explicit flushing or periodic writeback of "old" data.
94803ba3782SJens Axboe  *
94903ba3782SJens Axboe  * Define "old": the first time one of an inode's pages is dirtied, we mark the
95003ba3782SJens Axboe  * dirtying-time in the inode's address_space.  So this periodic writeback code
95103ba3782SJens Axboe  * just walks the superblock inode list, writing back any inodes which are
95203ba3782SJens Axboe  * older than a specific point in time.
95303ba3782SJens Axboe  *
95403ba3782SJens Axboe  * Try to run once per dirty_writeback_interval.  But if a writeback event
95503ba3782SJens Axboe  * takes longer than a dirty_writeback_interval interval, then leave a
95603ba3782SJens Axboe  * one-second gap.
95703ba3782SJens Axboe  *
95803ba3782SJens Axboe  * older_than_this takes precedence over nr_to_write.  So we'll only write back
95903ba3782SJens Axboe  * all dirty pages if they are all attached to "old" mappings.
96003ba3782SJens Axboe  */
961c4a77a6cSJens Axboe static long wb_writeback(struct bdi_writeback *wb,
96283ba7b07SChristoph Hellwig 			 struct wb_writeback_work *work)
96303ba3782SJens Axboe {
964e98be2d5SWu Fengguang 	unsigned long wb_start = jiffies;
965d46db3d5SWu Fengguang 	long nr_pages = work->nr_pages;
9660dc83bd3SJan Kara 	unsigned long oldest_jif;
967a5989bdcSJan Kara 	struct inode *inode;
968d46db3d5SWu Fengguang 	long progress;
96903ba3782SJens Axboe 
9700dc83bd3SJan Kara 	oldest_jif = jiffies;
9710dc83bd3SJan Kara 	work->older_than_this = &oldest_jif;
97203ba3782SJens Axboe 
973e8dfc305SWu Fengguang 	spin_lock(&wb->list_lock);
97403ba3782SJens Axboe 	for (;;) {
97503ba3782SJens Axboe 		/*
976d3ddec76SWu Fengguang 		 * Stop writeback when nr_pages has been consumed
97703ba3782SJens Axboe 		 */
97883ba7b07SChristoph Hellwig 		if (work->nr_pages <= 0)
97903ba3782SJens Axboe 			break;
98003ba3782SJens Axboe 
98103ba3782SJens Axboe 		/*
982aa373cf5SJan Kara 		 * Background writeout and kupdate-style writeback may
983aa373cf5SJan Kara 		 * run forever. Stop them if there is other work to do
984aa373cf5SJan Kara 		 * so that e.g. sync can proceed. They'll be restarted
985aa373cf5SJan Kara 		 * after the other works are all done.
986aa373cf5SJan Kara 		 */
987aa373cf5SJan Kara 		if ((work->for_background || work->for_kupdate) &&
988f0054bb1STejun Heo 		    !list_empty(&wb->work_list))
989aa373cf5SJan Kara 			break;
990aa373cf5SJan Kara 
991aa373cf5SJan Kara 		/*
992d3ddec76SWu Fengguang 		 * For background writeout, stop when we are below the
993d3ddec76SWu Fengguang 		 * background dirty threshold
99403ba3782SJens Axboe 		 */
995a88a341aSTejun Heo 		if (work->for_background && !over_bground_thresh(wb))
99603ba3782SJens Axboe 			break;
99703ba3782SJens Axboe 
9981bc36b64SJan Kara 		/*
9991bc36b64SJan Kara 		 * Kupdate and background works are special and we want to
10001bc36b64SJan Kara 		 * include all inodes that need writing. Livelock avoidance is
10011bc36b64SJan Kara 		 * handled by these works yielding to any other work so we are
10021bc36b64SJan Kara 		 * safe.
10031bc36b64SJan Kara 		 */
1004ba9aa839SWu Fengguang 		if (work->for_kupdate) {
10050dc83bd3SJan Kara 			oldest_jif = jiffies -
1006ba9aa839SWu Fengguang 				msecs_to_jiffies(dirty_expire_interval * 10);
10071bc36b64SJan Kara 		} else if (work->for_background)
10080dc83bd3SJan Kara 			oldest_jif = jiffies;
1009028c2dd1SDave Chinner 
1010d46db3d5SWu Fengguang 		trace_writeback_start(wb->bdi, work);
1011e8dfc305SWu Fengguang 		if (list_empty(&wb->b_io))
1012ad4e38ddSCurt Wohlgemuth 			queue_io(wb, work);
101383ba7b07SChristoph Hellwig 		if (work->sb)
1014d46db3d5SWu Fengguang 			progress = writeback_sb_inodes(work->sb, wb, work);
1015edadfb10SChristoph Hellwig 		else
1016d46db3d5SWu Fengguang 			progress = __writeback_inodes_wb(wb, work);
1017d46db3d5SWu Fengguang 		trace_writeback_written(wb->bdi, work);
1018028c2dd1SDave Chinner 
1019e98be2d5SWu Fengguang 		wb_update_bandwidth(wb, wb_start);
102003ba3782SJens Axboe 
102103ba3782SJens Axboe 		/*
102271fd05a8SJens Axboe 		 * Did we write something? Try for more
1023e6fb6da2SWu Fengguang 		 *
1024e6fb6da2SWu Fengguang 		 * Dirty inodes are moved to b_io for writeback in batches.
1025e6fb6da2SWu Fengguang 		 * The completion of the current batch does not necessarily
1026e6fb6da2SWu Fengguang 		 * mean the overall work is done. So we keep looping as long
1027e6fb6da2SWu Fengguang 		 * as made some progress on cleaning pages or inodes.
102871fd05a8SJens Axboe 		 */
1029d46db3d5SWu Fengguang 		if (progress)
103003ba3782SJens Axboe 			continue;
1031a5989bdcSJan Kara 		/*
1032e6fb6da2SWu Fengguang 		 * No more inodes for IO, bail
1033a5989bdcSJan Kara 		 */
1034b7a2441fSWu Fengguang 		if (list_empty(&wb->b_more_io))
103503ba3782SJens Axboe 			break;
103603ba3782SJens Axboe 		/*
10378010c3b6SJens Axboe 		 * Nothing written. Wait for some inode to
10388010c3b6SJens Axboe 		 * become available for writeback. Otherwise
10398010c3b6SJens Axboe 		 * we'll just busyloop.
104003ba3782SJens Axboe 		 */
104103ba3782SJens Axboe 		if (!list_empty(&wb->b_more_io))  {
1042d46db3d5SWu Fengguang 			trace_writeback_wait(wb->bdi, work);
104303ba3782SJens Axboe 			inode = wb_inode(wb->b_more_io.prev);
1044250df6edSDave Chinner 			spin_lock(&inode->i_lock);
1045f0d07b7fSJan Kara 			spin_unlock(&wb->list_lock);
1046169ebd90SJan Kara 			/* This function drops i_lock... */
1047169ebd90SJan Kara 			inode_sleep_on_writeback(inode);
1048f0d07b7fSJan Kara 			spin_lock(&wb->list_lock);
104903ba3782SJens Axboe 		}
105003ba3782SJens Axboe 	}
1051e8dfc305SWu Fengguang 	spin_unlock(&wb->list_lock);
105203ba3782SJens Axboe 
1053d46db3d5SWu Fengguang 	return nr_pages - work->nr_pages;
105403ba3782SJens Axboe }
105503ba3782SJens Axboe 
105603ba3782SJens Axboe /*
105783ba7b07SChristoph Hellwig  * Return the next wb_writeback_work struct that hasn't been processed yet.
105803ba3782SJens Axboe  */
1059f0054bb1STejun Heo static struct wb_writeback_work *get_next_work_item(struct bdi_writeback *wb)
106003ba3782SJens Axboe {
106183ba7b07SChristoph Hellwig 	struct wb_writeback_work *work = NULL;
106203ba3782SJens Axboe 
1063f0054bb1STejun Heo 	spin_lock_bh(&wb->work_lock);
1064f0054bb1STejun Heo 	if (!list_empty(&wb->work_list)) {
1065f0054bb1STejun Heo 		work = list_entry(wb->work_list.next,
106683ba7b07SChristoph Hellwig 				  struct wb_writeback_work, list);
106783ba7b07SChristoph Hellwig 		list_del_init(&work->list);
106803ba3782SJens Axboe 	}
1069f0054bb1STejun Heo 	spin_unlock_bh(&wb->work_lock);
107083ba7b07SChristoph Hellwig 	return work;
107103ba3782SJens Axboe }
107203ba3782SJens Axboe 
1073cdf01dd5SLinus Torvalds /*
1074cdf01dd5SLinus Torvalds  * Add in the number of potentially dirty inodes, because each inode
1075cdf01dd5SLinus Torvalds  * write can dirty pagecache in the underlying blockdev.
1076cdf01dd5SLinus Torvalds  */
1077cdf01dd5SLinus Torvalds static unsigned long get_nr_dirty_pages(void)
1078cdf01dd5SLinus Torvalds {
1079cdf01dd5SLinus Torvalds 	return global_page_state(NR_FILE_DIRTY) +
1080cdf01dd5SLinus Torvalds 		global_page_state(NR_UNSTABLE_NFS) +
1081cdf01dd5SLinus Torvalds 		get_nr_dirty_inodes();
1082cdf01dd5SLinus Torvalds }
1083cdf01dd5SLinus Torvalds 
10846585027aSJan Kara static long wb_check_background_flush(struct bdi_writeback *wb)
10856585027aSJan Kara {
1086a88a341aSTejun Heo 	if (over_bground_thresh(wb)) {
10876585027aSJan Kara 
10886585027aSJan Kara 		struct wb_writeback_work work = {
10896585027aSJan Kara 			.nr_pages	= LONG_MAX,
10906585027aSJan Kara 			.sync_mode	= WB_SYNC_NONE,
10916585027aSJan Kara 			.for_background	= 1,
10926585027aSJan Kara 			.range_cyclic	= 1,
10930e175a18SCurt Wohlgemuth 			.reason		= WB_REASON_BACKGROUND,
10946585027aSJan Kara 		};
10956585027aSJan Kara 
10966585027aSJan Kara 		return wb_writeback(wb, &work);
10976585027aSJan Kara 	}
10986585027aSJan Kara 
10996585027aSJan Kara 	return 0;
11006585027aSJan Kara }
11016585027aSJan Kara 
110203ba3782SJens Axboe static long wb_check_old_data_flush(struct bdi_writeback *wb)
110303ba3782SJens Axboe {
110403ba3782SJens Axboe 	unsigned long expired;
110503ba3782SJens Axboe 	long nr_pages;
110603ba3782SJens Axboe 
110769b62d01SJens Axboe 	/*
110869b62d01SJens Axboe 	 * When set to zero, disable periodic writeback
110969b62d01SJens Axboe 	 */
111069b62d01SJens Axboe 	if (!dirty_writeback_interval)
111169b62d01SJens Axboe 		return 0;
111269b62d01SJens Axboe 
111303ba3782SJens Axboe 	expired = wb->last_old_flush +
111403ba3782SJens Axboe 			msecs_to_jiffies(dirty_writeback_interval * 10);
111503ba3782SJens Axboe 	if (time_before(jiffies, expired))
111603ba3782SJens Axboe 		return 0;
111703ba3782SJens Axboe 
111803ba3782SJens Axboe 	wb->last_old_flush = jiffies;
1119cdf01dd5SLinus Torvalds 	nr_pages = get_nr_dirty_pages();
112003ba3782SJens Axboe 
1121c4a77a6cSJens Axboe 	if (nr_pages) {
112283ba7b07SChristoph Hellwig 		struct wb_writeback_work work = {
1123c4a77a6cSJens Axboe 			.nr_pages	= nr_pages,
1124c4a77a6cSJens Axboe 			.sync_mode	= WB_SYNC_NONE,
1125c4a77a6cSJens Axboe 			.for_kupdate	= 1,
1126c4a77a6cSJens Axboe 			.range_cyclic	= 1,
11270e175a18SCurt Wohlgemuth 			.reason		= WB_REASON_PERIODIC,
1128c4a77a6cSJens Axboe 		};
1129c4a77a6cSJens Axboe 
113083ba7b07SChristoph Hellwig 		return wb_writeback(wb, &work);
1131c4a77a6cSJens Axboe 	}
113203ba3782SJens Axboe 
113303ba3782SJens Axboe 	return 0;
113403ba3782SJens Axboe }
113503ba3782SJens Axboe 
113603ba3782SJens Axboe /*
113703ba3782SJens Axboe  * Retrieve work items and do the writeback they describe
113803ba3782SJens Axboe  */
113925d130baSWanpeng Li static long wb_do_writeback(struct bdi_writeback *wb)
114003ba3782SJens Axboe {
114183ba7b07SChristoph Hellwig 	struct wb_writeback_work *work;
1142c4a77a6cSJens Axboe 	long wrote = 0;
114303ba3782SJens Axboe 
11444452226eSTejun Heo 	set_bit(WB_writeback_running, &wb->state);
1145f0054bb1STejun Heo 	while ((work = get_next_work_item(wb)) != NULL) {
1146ac7b19a3STejun Heo 		struct completion *done = work->done;
114783ba7b07SChristoph Hellwig 
1148f0054bb1STejun Heo 		trace_writeback_exec(wb->bdi, work);
1149455b2864SDave Chinner 
115083ba7b07SChristoph Hellwig 		wrote += wb_writeback(wb, work);
115103ba3782SJens Axboe 
1152ac7b19a3STejun Heo 		if (work->auto_free)
115383ba7b07SChristoph Hellwig 			kfree(work);
1154ac7b19a3STejun Heo 		if (done)
1155ac7b19a3STejun Heo 			complete(done);
115603ba3782SJens Axboe 	}
115703ba3782SJens Axboe 
115803ba3782SJens Axboe 	/*
115903ba3782SJens Axboe 	 * Check for periodic writeback, kupdated() style
116003ba3782SJens Axboe 	 */
116103ba3782SJens Axboe 	wrote += wb_check_old_data_flush(wb);
11626585027aSJan Kara 	wrote += wb_check_background_flush(wb);
11634452226eSTejun Heo 	clear_bit(WB_writeback_running, &wb->state);
116403ba3782SJens Axboe 
116503ba3782SJens Axboe 	return wrote;
116603ba3782SJens Axboe }
116703ba3782SJens Axboe 
116803ba3782SJens Axboe /*
116903ba3782SJens Axboe  * Handle writeback of dirty data for the device backed by this bdi. Also
1170839a8e86STejun Heo  * reschedules periodically and does kupdated style flushing.
117103ba3782SJens Axboe  */
1172f0054bb1STejun Heo void wb_workfn(struct work_struct *work)
117303ba3782SJens Axboe {
1174839a8e86STejun Heo 	struct bdi_writeback *wb = container_of(to_delayed_work(work),
1175839a8e86STejun Heo 						struct bdi_writeback, dwork);
117603ba3782SJens Axboe 	long pages_written;
117703ba3782SJens Axboe 
1178f0054bb1STejun Heo 	set_worker_desc("flush-%s", dev_name(wb->bdi->dev));
1179766f9164SPeter Zijlstra 	current->flags |= PF_SWAPWRITE;
118003ba3782SJens Axboe 
1181839a8e86STejun Heo 	if (likely(!current_is_workqueue_rescuer() ||
11824452226eSTejun Heo 		   !test_bit(WB_registered, &wb->state))) {
118303ba3782SJens Axboe 		/*
1184f0054bb1STejun Heo 		 * The normal path.  Keep writing back @wb until its
1185839a8e86STejun Heo 		 * work_list is empty.  Note that this path is also taken
1186f0054bb1STejun Heo 		 * if @wb is shutting down even when we're running off the
1187839a8e86STejun Heo 		 * rescuer as work_list needs to be drained.
118803ba3782SJens Axboe 		 */
1189839a8e86STejun Heo 		do {
119025d130baSWanpeng Li 			pages_written = wb_do_writeback(wb);
1191455b2864SDave Chinner 			trace_writeback_pages_written(pages_written);
1192f0054bb1STejun Heo 		} while (!list_empty(&wb->work_list));
1193839a8e86STejun Heo 	} else {
1194253c34e9SArtem Bityutskiy 		/*
1195839a8e86STejun Heo 		 * bdi_wq can't get enough workers and we're running off
1196839a8e86STejun Heo 		 * the emergency worker.  Don't hog it.  Hopefully, 1024 is
1197839a8e86STejun Heo 		 * enough for efficient IO.
1198253c34e9SArtem Bityutskiy 		 */
1199f0054bb1STejun Heo 		pages_written = writeback_inodes_wb(wb, 1024,
1200839a8e86STejun Heo 						    WB_REASON_FORKER_THREAD);
1201839a8e86STejun Heo 		trace_writeback_pages_written(pages_written);
120203ba3782SJens Axboe 	}
120303ba3782SJens Axboe 
1204f0054bb1STejun Heo 	if (!list_empty(&wb->work_list))
12056ca738d6SDerek Basehore 		mod_delayed_work(bdi_wq, &wb->dwork, 0);
12066ca738d6SDerek Basehore 	else if (wb_has_dirty_io(wb) && dirty_writeback_interval)
1207f0054bb1STejun Heo 		wb_wakeup_delayed(wb);
1208455b2864SDave Chinner 
1209839a8e86STejun Heo 	current->flags &= ~PF_SWAPWRITE;
121003ba3782SJens Axboe }
121103ba3782SJens Axboe 
121203ba3782SJens Axboe /*
121303ba3782SJens Axboe  * Start writeback of `nr_pages' pages.  If `nr_pages' is zero, write back
121403ba3782SJens Axboe  * the whole world.
121503ba3782SJens Axboe  */
12160e175a18SCurt Wohlgemuth void wakeup_flusher_threads(long nr_pages, enum wb_reason reason)
121703ba3782SJens Axboe {
1218b8c2f347SChristoph Hellwig 	struct backing_dev_info *bdi;
1219b8c2f347SChristoph Hellwig 
122047df3ddeSJan Kara 	if (!nr_pages)
122147df3ddeSJan Kara 		nr_pages = get_nr_dirty_pages();
1222b8c2f347SChristoph Hellwig 
1223b8c2f347SChristoph Hellwig 	rcu_read_lock();
1224f2b65121STejun Heo 	list_for_each_entry_rcu(bdi, &bdi_list, bdi_list) {
1225f2b65121STejun Heo 		struct bdi_writeback *wb;
1226f2b65121STejun Heo 		struct wb_iter iter;
1227f2b65121STejun Heo 
1228f2b65121STejun Heo 		if (!bdi_has_dirty_io(bdi))
1229f2b65121STejun Heo 			continue;
1230f2b65121STejun Heo 
1231f2b65121STejun Heo 		bdi_for_each_wb(wb, bdi, &iter, 0)
1232f2b65121STejun Heo 			wb_start_writeback(wb, wb_split_bdi_pages(wb, nr_pages),
1233f2b65121STejun Heo 					   false, reason);
1234f2b65121STejun Heo 	}
1235b8c2f347SChristoph Hellwig 	rcu_read_unlock();
123603ba3782SJens Axboe }
123703ba3782SJens Axboe 
1238a2f48706STheodore Ts'o /*
1239a2f48706STheodore Ts'o  * Wake up bdi's periodically to make sure dirtytime inodes gets
1240a2f48706STheodore Ts'o  * written back periodically.  We deliberately do *not* check the
1241a2f48706STheodore Ts'o  * b_dirtytime list in wb_has_dirty_io(), since this would cause the
1242a2f48706STheodore Ts'o  * kernel to be constantly waking up once there are any dirtytime
1243a2f48706STheodore Ts'o  * inodes on the system.  So instead we define a separate delayed work
1244a2f48706STheodore Ts'o  * function which gets called much more rarely.  (By default, only
1245a2f48706STheodore Ts'o  * once every 12 hours.)
1246a2f48706STheodore Ts'o  *
1247a2f48706STheodore Ts'o  * If there is any other write activity going on in the file system,
1248a2f48706STheodore Ts'o  * this function won't be necessary.  But if the only thing that has
1249a2f48706STheodore Ts'o  * happened on the file system is a dirtytime inode caused by an atime
1250a2f48706STheodore Ts'o  * update, we need this infrastructure below to make sure that inode
1251a2f48706STheodore Ts'o  * eventually gets pushed out to disk.
1252a2f48706STheodore Ts'o  */
1253a2f48706STheodore Ts'o static void wakeup_dirtytime_writeback(struct work_struct *w);
1254a2f48706STheodore Ts'o static DECLARE_DELAYED_WORK(dirtytime_work, wakeup_dirtytime_writeback);
1255a2f48706STheodore Ts'o 
1256a2f48706STheodore Ts'o static void wakeup_dirtytime_writeback(struct work_struct *w)
1257a2f48706STheodore Ts'o {
1258a2f48706STheodore Ts'o 	struct backing_dev_info *bdi;
1259a2f48706STheodore Ts'o 
1260a2f48706STheodore Ts'o 	rcu_read_lock();
1261a2f48706STheodore Ts'o 	list_for_each_entry_rcu(bdi, &bdi_list, bdi_list) {
1262001fe6f6STejun Heo 		struct bdi_writeback *wb;
1263001fe6f6STejun Heo 		struct wb_iter iter;
1264001fe6f6STejun Heo 
1265001fe6f6STejun Heo 		bdi_for_each_wb(wb, bdi, &iter, 0)
1266001fe6f6STejun Heo 			if (!list_empty(&bdi->wb.b_dirty_time))
1267f0054bb1STejun Heo 				wb_wakeup(&bdi->wb);
1268a2f48706STheodore Ts'o 	}
1269a2f48706STheodore Ts'o 	rcu_read_unlock();
1270a2f48706STheodore Ts'o 	schedule_delayed_work(&dirtytime_work, dirtytime_expire_interval * HZ);
1271a2f48706STheodore Ts'o }
1272a2f48706STheodore Ts'o 
1273a2f48706STheodore Ts'o static int __init start_dirtytime_writeback(void)
1274a2f48706STheodore Ts'o {
1275a2f48706STheodore Ts'o 	schedule_delayed_work(&dirtytime_work, dirtytime_expire_interval * HZ);
1276a2f48706STheodore Ts'o 	return 0;
1277a2f48706STheodore Ts'o }
1278a2f48706STheodore Ts'o __initcall(start_dirtytime_writeback);
1279a2f48706STheodore Ts'o 
12801efff914STheodore Ts'o int dirtytime_interval_handler(struct ctl_table *table, int write,
12811efff914STheodore Ts'o 			       void __user *buffer, size_t *lenp, loff_t *ppos)
12821efff914STheodore Ts'o {
12831efff914STheodore Ts'o 	int ret;
12841efff914STheodore Ts'o 
12851efff914STheodore Ts'o 	ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
12861efff914STheodore Ts'o 	if (ret == 0 && write)
12871efff914STheodore Ts'o 		mod_delayed_work(system_wq, &dirtytime_work, 0);
12881efff914STheodore Ts'o 	return ret;
12891efff914STheodore Ts'o }
12901efff914STheodore Ts'o 
129103ba3782SJens Axboe static noinline void block_dump___mark_inode_dirty(struct inode *inode)
129203ba3782SJens Axboe {
129303ba3782SJens Axboe 	if (inode->i_ino || strcmp(inode->i_sb->s_id, "bdev")) {
129403ba3782SJens Axboe 		struct dentry *dentry;
129503ba3782SJens Axboe 		const char *name = "?";
129603ba3782SJens Axboe 
129703ba3782SJens Axboe 		dentry = d_find_alias(inode);
129803ba3782SJens Axboe 		if (dentry) {
129903ba3782SJens Axboe 			spin_lock(&dentry->d_lock);
130003ba3782SJens Axboe 			name = (const char *) dentry->d_name.name;
130103ba3782SJens Axboe 		}
130203ba3782SJens Axboe 		printk(KERN_DEBUG
130303ba3782SJens Axboe 		       "%s(%d): dirtied inode %lu (%s) on %s\n",
130403ba3782SJens Axboe 		       current->comm, task_pid_nr(current), inode->i_ino,
130503ba3782SJens Axboe 		       name, inode->i_sb->s_id);
130603ba3782SJens Axboe 		if (dentry) {
130703ba3782SJens Axboe 			spin_unlock(&dentry->d_lock);
130803ba3782SJens Axboe 			dput(dentry);
130903ba3782SJens Axboe 		}
131003ba3782SJens Axboe 	}
131103ba3782SJens Axboe }
131203ba3782SJens Axboe 
131303ba3782SJens Axboe /**
131403ba3782SJens Axboe  *	__mark_inode_dirty -	internal function
131503ba3782SJens Axboe  *	@inode: inode to mark
131603ba3782SJens Axboe  *	@flags: what kind of dirty (i.e. I_DIRTY_SYNC)
131703ba3782SJens Axboe  *	Mark an inode as dirty. Callers should use mark_inode_dirty or
131803ba3782SJens Axboe  *  	mark_inode_dirty_sync.
131903ba3782SJens Axboe  *
132003ba3782SJens Axboe  * Put the inode on the super block's dirty list.
132103ba3782SJens Axboe  *
132203ba3782SJens Axboe  * CAREFUL! We mark it dirty unconditionally, but move it onto the
132303ba3782SJens Axboe  * dirty list only if it is hashed or if it refers to a blockdev.
132403ba3782SJens Axboe  * If it was not hashed, it will never be added to the dirty list
132503ba3782SJens Axboe  * even if it is later hashed, as it will have been marked dirty already.
132603ba3782SJens Axboe  *
132703ba3782SJens Axboe  * In short, make sure you hash any inodes _before_ you start marking
132803ba3782SJens Axboe  * them dirty.
132903ba3782SJens Axboe  *
133003ba3782SJens Axboe  * Note that for blockdevs, inode->dirtied_when represents the dirtying time of
133103ba3782SJens Axboe  * the block-special inode (/dev/hda1) itself.  And the ->dirtied_when field of
133203ba3782SJens Axboe  * the kernel-internal blockdev inode represents the dirtying time of the
133303ba3782SJens Axboe  * blockdev's pages.  This is why for I_DIRTY_PAGES we always use
133403ba3782SJens Axboe  * page->mapping->host, so the page-dirtying time is recorded in the internal
133503ba3782SJens Axboe  * blockdev inode.
133603ba3782SJens Axboe  */
13370ae45f63STheodore Ts'o #define I_DIRTY_INODE (I_DIRTY_SYNC | I_DIRTY_DATASYNC)
133803ba3782SJens Axboe void __mark_inode_dirty(struct inode *inode, int flags)
133903ba3782SJens Axboe {
134003ba3782SJens Axboe 	struct super_block *sb = inode->i_sb;
1341253c34e9SArtem Bityutskiy 	struct backing_dev_info *bdi = NULL;
13420ae45f63STheodore Ts'o 	int dirtytime;
13430ae45f63STheodore Ts'o 
13440ae45f63STheodore Ts'o 	trace_writeback_mark_inode_dirty(inode, flags);
134503ba3782SJens Axboe 
134603ba3782SJens Axboe 	/*
134703ba3782SJens Axboe 	 * Don't do this for I_DIRTY_PAGES - that doesn't actually
134803ba3782SJens Axboe 	 * dirty the inode itself
134903ba3782SJens Axboe 	 */
13500ae45f63STheodore Ts'o 	if (flags & (I_DIRTY_SYNC | I_DIRTY_DATASYNC | I_DIRTY_TIME)) {
13519fb0a7daSTejun Heo 		trace_writeback_dirty_inode_start(inode, flags);
13529fb0a7daSTejun Heo 
135303ba3782SJens Axboe 		if (sb->s_op->dirty_inode)
1354aa385729SChristoph Hellwig 			sb->s_op->dirty_inode(inode, flags);
13559fb0a7daSTejun Heo 
13569fb0a7daSTejun Heo 		trace_writeback_dirty_inode(inode, flags);
135703ba3782SJens Axboe 	}
13580ae45f63STheodore Ts'o 	if (flags & I_DIRTY_INODE)
13590ae45f63STheodore Ts'o 		flags &= ~I_DIRTY_TIME;
13600ae45f63STheodore Ts'o 	dirtytime = flags & I_DIRTY_TIME;
136103ba3782SJens Axboe 
136203ba3782SJens Axboe 	/*
13639c6ac78eSTejun Heo 	 * Paired with smp_mb() in __writeback_single_inode() for the
13649c6ac78eSTejun Heo 	 * following lockless i_state test.  See there for details.
136503ba3782SJens Axboe 	 */
136603ba3782SJens Axboe 	smp_mb();
136703ba3782SJens Axboe 
13680ae45f63STheodore Ts'o 	if (((inode->i_state & flags) == flags) ||
13690ae45f63STheodore Ts'o 	    (dirtytime && (inode->i_state & I_DIRTY_INODE)))
137003ba3782SJens Axboe 		return;
137103ba3782SJens Axboe 
137203ba3782SJens Axboe 	if (unlikely(block_dump))
137303ba3782SJens Axboe 		block_dump___mark_inode_dirty(inode);
137403ba3782SJens Axboe 
1375250df6edSDave Chinner 	spin_lock(&inode->i_lock);
13760ae45f63STheodore Ts'o 	if (dirtytime && (inode->i_state & I_DIRTY_INODE))
13770ae45f63STheodore Ts'o 		goto out_unlock_inode;
137803ba3782SJens Axboe 	if ((inode->i_state & flags) != flags) {
137903ba3782SJens Axboe 		const int was_dirty = inode->i_state & I_DIRTY;
138003ba3782SJens Axboe 
138152ebea74STejun Heo 		inode_attach_wb(inode, NULL);
138252ebea74STejun Heo 
13830ae45f63STheodore Ts'o 		if (flags & I_DIRTY_INODE)
13840ae45f63STheodore Ts'o 			inode->i_state &= ~I_DIRTY_TIME;
138503ba3782SJens Axboe 		inode->i_state |= flags;
138603ba3782SJens Axboe 
138703ba3782SJens Axboe 		/*
138803ba3782SJens Axboe 		 * If the inode is being synced, just update its dirty state.
138903ba3782SJens Axboe 		 * The unlocker will place the inode on the appropriate
139003ba3782SJens Axboe 		 * superblock list, based upon its state.
139103ba3782SJens Axboe 		 */
139203ba3782SJens Axboe 		if (inode->i_state & I_SYNC)
1393250df6edSDave Chinner 			goto out_unlock_inode;
139403ba3782SJens Axboe 
139503ba3782SJens Axboe 		/*
139603ba3782SJens Axboe 		 * Only add valid (hashed) inodes to the superblock's
139703ba3782SJens Axboe 		 * dirty list.  Add blockdev inodes as well.
139803ba3782SJens Axboe 		 */
139903ba3782SJens Axboe 		if (!S_ISBLK(inode->i_mode)) {
14001d3382cbSAl Viro 			if (inode_unhashed(inode))
1401250df6edSDave Chinner 				goto out_unlock_inode;
140203ba3782SJens Axboe 		}
1403a4ffdde6SAl Viro 		if (inode->i_state & I_FREEING)
1404250df6edSDave Chinner 			goto out_unlock_inode;
140503ba3782SJens Axboe 
140603ba3782SJens Axboe 		/*
140703ba3782SJens Axboe 		 * If the inode was already on b_dirty/b_io/b_more_io, don't
140803ba3782SJens Axboe 		 * reposition it (that would break b_dirty time-ordering).
140903ba3782SJens Axboe 		 */
141003ba3782SJens Axboe 		if (!was_dirty) {
1411d6c10f1fSTejun Heo 			struct list_head *dirty_list;
1412a66979abSDave Chinner 			bool wakeup_bdi = false;
1413253c34e9SArtem Bityutskiy 			bdi = inode_to_bdi(inode);
1414500b067cSJens Axboe 
1415146d7009SJunxiao Bi 			spin_unlock(&inode->i_lock);
1416146d7009SJunxiao Bi 			spin_lock(&bdi->wb.list_lock);
1417253c34e9SArtem Bityutskiy 
1418d6c10f1fSTejun Heo 			WARN(bdi_cap_writeback_dirty(bdi) &&
1419d6c10f1fSTejun Heo 			     !test_bit(WB_registered, &bdi->wb.state),
1420d6c10f1fSTejun Heo 			     "bdi-%s not registered\n", bdi->name);
142103ba3782SJens Axboe 
142203ba3782SJens Axboe 			inode->dirtied_when = jiffies;
1423a2f48706STheodore Ts'o 			if (dirtytime)
1424a2f48706STheodore Ts'o 				inode->dirtied_time_when = jiffies;
1425d6c10f1fSTejun Heo 
1426a2f48706STheodore Ts'o 			if (inode->i_state & (I_DIRTY_INODE | I_DIRTY_PAGES))
1427d6c10f1fSTejun Heo 				dirty_list = &bdi->wb.b_dirty;
1428a2f48706STheodore Ts'o 			else
1429d6c10f1fSTejun Heo 				dirty_list = &bdi->wb.b_dirty_time;
1430d6c10f1fSTejun Heo 
1431d6c10f1fSTejun Heo 			wakeup_bdi = inode_wb_list_move_locked(inode, &bdi->wb,
1432d6c10f1fSTejun Heo 							       dirty_list);
1433d6c10f1fSTejun Heo 
1434f758eeabSChristoph Hellwig 			spin_unlock(&bdi->wb.list_lock);
14350ae45f63STheodore Ts'o 			trace_writeback_dirty_inode_enqueue(inode);
1436253c34e9SArtem Bityutskiy 
1437d6c10f1fSTejun Heo 			/*
1438d6c10f1fSTejun Heo 			 * If this is the first dirty inode for this bdi,
1439d6c10f1fSTejun Heo 			 * we have to wake-up the corresponding bdi thread
1440d6c10f1fSTejun Heo 			 * to make sure background write-back happens
1441d6c10f1fSTejun Heo 			 * later.
1442d6c10f1fSTejun Heo 			 */
1443d6c10f1fSTejun Heo 			if (bdi_cap_writeback_dirty(bdi) && wakeup_bdi)
1444f0054bb1STejun Heo 				wb_wakeup_delayed(&bdi->wb);
1445a66979abSDave Chinner 			return;
1446a66979abSDave Chinner 		}
1447a66979abSDave Chinner 	}
1448a66979abSDave Chinner out_unlock_inode:
1449a66979abSDave Chinner 	spin_unlock(&inode->i_lock);
1450a66979abSDave Chinner 
145103ba3782SJens Axboe }
145203ba3782SJens Axboe EXPORT_SYMBOL(__mark_inode_dirty);
145303ba3782SJens Axboe 
1454b6e51316SJens Axboe static void wait_sb_inodes(struct super_block *sb)
145566f3b8e2SJens Axboe {
145638f21977SNick Piggin 	struct inode *inode, *old_inode = NULL;
145738f21977SNick Piggin 
145803ba3782SJens Axboe 	/*
145903ba3782SJens Axboe 	 * We need to be protected against the filesystem going from
146003ba3782SJens Axboe 	 * r/o to r/w or vice versa.
146103ba3782SJens Axboe 	 */
1462b6e51316SJens Axboe 	WARN_ON(!rwsem_is_locked(&sb->s_umount));
146303ba3782SJens Axboe 
146455fa6091SDave Chinner 	spin_lock(&inode_sb_list_lock);
146566f3b8e2SJens Axboe 
146638f21977SNick Piggin 	/*
146738f21977SNick Piggin 	 * Data integrity sync. Must wait for all pages under writeback,
146838f21977SNick Piggin 	 * because there may have been pages dirtied before our sync
146938f21977SNick Piggin 	 * call, but which had writeout started before we write it out.
147038f21977SNick Piggin 	 * In which case, the inode may not be on the dirty list, but
147138f21977SNick Piggin 	 * we still have to wait for that writeout.
147238f21977SNick Piggin 	 */
1473b6e51316SJens Axboe 	list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
1474250df6edSDave Chinner 		struct address_space *mapping = inode->i_mapping;
147538f21977SNick Piggin 
1476250df6edSDave Chinner 		spin_lock(&inode->i_lock);
1477250df6edSDave Chinner 		if ((inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW)) ||
1478250df6edSDave Chinner 		    (mapping->nrpages == 0)) {
1479250df6edSDave Chinner 			spin_unlock(&inode->i_lock);
148038f21977SNick Piggin 			continue;
1481250df6edSDave Chinner 		}
148238f21977SNick Piggin 		__iget(inode);
1483250df6edSDave Chinner 		spin_unlock(&inode->i_lock);
148455fa6091SDave Chinner 		spin_unlock(&inode_sb_list_lock);
148555fa6091SDave Chinner 
148638f21977SNick Piggin 		/*
148755fa6091SDave Chinner 		 * We hold a reference to 'inode' so it couldn't have been
148855fa6091SDave Chinner 		 * removed from s_inodes list while we dropped the
148955fa6091SDave Chinner 		 * inode_sb_list_lock.  We cannot iput the inode now as we can
149055fa6091SDave Chinner 		 * be holding the last reference and we cannot iput it under
149155fa6091SDave Chinner 		 * inode_sb_list_lock. So we keep the reference and iput it
149255fa6091SDave Chinner 		 * later.
149338f21977SNick Piggin 		 */
149438f21977SNick Piggin 		iput(old_inode);
149538f21977SNick Piggin 		old_inode = inode;
149638f21977SNick Piggin 
149738f21977SNick Piggin 		filemap_fdatawait(mapping);
149838f21977SNick Piggin 
149938f21977SNick Piggin 		cond_resched();
150038f21977SNick Piggin 
150155fa6091SDave Chinner 		spin_lock(&inode_sb_list_lock);
150238f21977SNick Piggin 	}
150355fa6091SDave Chinner 	spin_unlock(&inode_sb_list_lock);
150438f21977SNick Piggin 	iput(old_inode);
150566f3b8e2SJens Axboe }
15061da177e4SLinus Torvalds 
1507d8a8559cSJens Axboe /**
15083259f8beSChris Mason  * writeback_inodes_sb_nr -	writeback dirty inodes from given super_block
1509d8a8559cSJens Axboe  * @sb: the superblock
15103259f8beSChris Mason  * @nr: the number of pages to write
1511786228abSMarcos Paulo de Souza  * @reason: reason why some writeback work initiated
15121da177e4SLinus Torvalds  *
1513d8a8559cSJens Axboe  * Start writeback on some inodes on this super_block. No guarantees are made
1514d8a8559cSJens Axboe  * on how many (if any) will be written, and this function does not wait
15153259f8beSChris Mason  * for IO completion of submitted IO.
15161da177e4SLinus Torvalds  */
15170e175a18SCurt Wohlgemuth void writeback_inodes_sb_nr(struct super_block *sb,
15180e175a18SCurt Wohlgemuth 			    unsigned long nr,
15190e175a18SCurt Wohlgemuth 			    enum wb_reason reason)
15201da177e4SLinus Torvalds {
152183ba7b07SChristoph Hellwig 	DECLARE_COMPLETION_ONSTACK(done);
152283ba7b07SChristoph Hellwig 	struct wb_writeback_work work = {
15233c4d7165SChristoph Hellwig 		.sb			= sb,
15243c4d7165SChristoph Hellwig 		.sync_mode		= WB_SYNC_NONE,
15256e6938b6SWu Fengguang 		.tagged_writepages	= 1,
152683ba7b07SChristoph Hellwig 		.done			= &done,
15273259f8beSChris Mason 		.nr_pages		= nr,
15280e175a18SCurt Wohlgemuth 		.reason			= reason,
15293c4d7165SChristoph Hellwig 	};
1530e7972912STejun Heo 	struct backing_dev_info *bdi = sb->s_bdi;
15310e3c9a22SJens Axboe 
1532e7972912STejun Heo 	if (!bdi_has_dirty_io(bdi) || bdi == &noop_backing_dev_info)
15336eedc701SJan Kara 		return;
1534cf37e972SChristoph Hellwig 	WARN_ON(!rwsem_is_locked(&sb->s_umount));
1535e7972912STejun Heo 	wb_queue_work(&bdi->wb, &work);
153683ba7b07SChristoph Hellwig 	wait_for_completion(&done);
15371da177e4SLinus Torvalds }
15383259f8beSChris Mason EXPORT_SYMBOL(writeback_inodes_sb_nr);
15393259f8beSChris Mason 
15403259f8beSChris Mason /**
15413259f8beSChris Mason  * writeback_inodes_sb	-	writeback dirty inodes from given super_block
15423259f8beSChris Mason  * @sb: the superblock
1543786228abSMarcos Paulo de Souza  * @reason: reason why some writeback work was initiated
15443259f8beSChris Mason  *
15453259f8beSChris Mason  * Start writeback on some inodes on this super_block. No guarantees are made
15463259f8beSChris Mason  * on how many (if any) will be written, and this function does not wait
15473259f8beSChris Mason  * for IO completion of submitted IO.
15483259f8beSChris Mason  */
15490e175a18SCurt Wohlgemuth void writeback_inodes_sb(struct super_block *sb, enum wb_reason reason)
15503259f8beSChris Mason {
15510e175a18SCurt Wohlgemuth 	return writeback_inodes_sb_nr(sb, get_nr_dirty_pages(), reason);
15523259f8beSChris Mason }
1553d8a8559cSJens Axboe EXPORT_SYMBOL(writeback_inodes_sb);
1554d8a8559cSJens Axboe 
1555d8a8559cSJens Axboe /**
155610ee27a0SMiao Xie  * try_to_writeback_inodes_sb_nr - try to start writeback if none underway
15573259f8beSChris Mason  * @sb: the superblock
15583259f8beSChris Mason  * @nr: the number of pages to write
155910ee27a0SMiao Xie  * @reason: the reason of writeback
15603259f8beSChris Mason  *
156110ee27a0SMiao Xie  * Invoke writeback_inodes_sb_nr if no writeback is currently underway.
15623259f8beSChris Mason  * Returns 1 if writeback was started, 0 if not.
15633259f8beSChris Mason  */
156410ee27a0SMiao Xie int try_to_writeback_inodes_sb_nr(struct super_block *sb,
15650e175a18SCurt Wohlgemuth 				  unsigned long nr,
15660e175a18SCurt Wohlgemuth 				  enum wb_reason reason)
15673259f8beSChris Mason {
1568bc05873dSTejun Heo 	if (writeback_in_progress(&sb->s_bdi->wb))
156910ee27a0SMiao Xie 		return 1;
157010ee27a0SMiao Xie 
157110ee27a0SMiao Xie 	if (!down_read_trylock(&sb->s_umount))
157210ee27a0SMiao Xie 		return 0;
157310ee27a0SMiao Xie 
15740e175a18SCurt Wohlgemuth 	writeback_inodes_sb_nr(sb, nr, reason);
15753259f8beSChris Mason 	up_read(&sb->s_umount);
15763259f8beSChris Mason 	return 1;
15773259f8beSChris Mason }
157810ee27a0SMiao Xie EXPORT_SYMBOL(try_to_writeback_inodes_sb_nr);
157910ee27a0SMiao Xie 
158010ee27a0SMiao Xie /**
158110ee27a0SMiao Xie  * try_to_writeback_inodes_sb - try to start writeback if none underway
158210ee27a0SMiao Xie  * @sb: the superblock
158310ee27a0SMiao Xie  * @reason: reason why some writeback work was initiated
158410ee27a0SMiao Xie  *
158510ee27a0SMiao Xie  * Implement by try_to_writeback_inodes_sb_nr()
158610ee27a0SMiao Xie  * Returns 1 if writeback was started, 0 if not.
158710ee27a0SMiao Xie  */
158810ee27a0SMiao Xie int try_to_writeback_inodes_sb(struct super_block *sb, enum wb_reason reason)
158910ee27a0SMiao Xie {
159010ee27a0SMiao Xie 	return try_to_writeback_inodes_sb_nr(sb, get_nr_dirty_pages(), reason);
159110ee27a0SMiao Xie }
159210ee27a0SMiao Xie EXPORT_SYMBOL(try_to_writeback_inodes_sb);
15933259f8beSChris Mason 
15943259f8beSChris Mason /**
1595d8a8559cSJens Axboe  * sync_inodes_sb	-	sync sb inode pages
1596d8a8559cSJens Axboe  * @sb: the superblock
1597d8a8559cSJens Axboe  *
1598d8a8559cSJens Axboe  * This function writes and waits on any dirty inode belonging to this
15990dc83bd3SJan Kara  * super_block.
1600d8a8559cSJens Axboe  */
16010dc83bd3SJan Kara void sync_inodes_sb(struct super_block *sb)
1602d8a8559cSJens Axboe {
160383ba7b07SChristoph Hellwig 	DECLARE_COMPLETION_ONSTACK(done);
160483ba7b07SChristoph Hellwig 	struct wb_writeback_work work = {
16053c4d7165SChristoph Hellwig 		.sb		= sb,
16063c4d7165SChristoph Hellwig 		.sync_mode	= WB_SYNC_ALL,
16073c4d7165SChristoph Hellwig 		.nr_pages	= LONG_MAX,
16083c4d7165SChristoph Hellwig 		.range_cyclic	= 0,
160983ba7b07SChristoph Hellwig 		.done		= &done,
16100e175a18SCurt Wohlgemuth 		.reason		= WB_REASON_SYNC,
16117747bd4bSDave Chinner 		.for_sync	= 1,
16123c4d7165SChristoph Hellwig 	};
1613e7972912STejun Heo 	struct backing_dev_info *bdi = sb->s_bdi;
16143c4d7165SChristoph Hellwig 
16156eedc701SJan Kara 	/* Nothing to do? */
1616e7972912STejun Heo 	if (!bdi_has_dirty_io(bdi) || bdi == &noop_backing_dev_info)
16176eedc701SJan Kara 		return;
1618cf37e972SChristoph Hellwig 	WARN_ON(!rwsem_is_locked(&sb->s_umount));
1619cf37e972SChristoph Hellwig 
1620e7972912STejun Heo 	wb_queue_work(&bdi->wb, &work);
162183ba7b07SChristoph Hellwig 	wait_for_completion(&done);
162283ba7b07SChristoph Hellwig 
1623b6e51316SJens Axboe 	wait_sb_inodes(sb);
1624d8a8559cSJens Axboe }
1625d8a8559cSJens Axboe EXPORT_SYMBOL(sync_inodes_sb);
16261da177e4SLinus Torvalds 
16271da177e4SLinus Torvalds /**
16281da177e4SLinus Torvalds  * write_inode_now	-	write an inode to disk
16291da177e4SLinus Torvalds  * @inode: inode to write to disk
16301da177e4SLinus Torvalds  * @sync: whether the write should be synchronous or not
16311da177e4SLinus Torvalds  *
16327f04c26dSAndrea Arcangeli  * This function commits an inode to disk immediately if it is dirty. This is
16337f04c26dSAndrea Arcangeli  * primarily needed by knfsd.
16347f04c26dSAndrea Arcangeli  *
16357f04c26dSAndrea Arcangeli  * The caller must either have a ref on the inode or must have set I_WILL_FREE.
16361da177e4SLinus Torvalds  */
16371da177e4SLinus Torvalds int write_inode_now(struct inode *inode, int sync)
16381da177e4SLinus Torvalds {
1639f758eeabSChristoph Hellwig 	struct bdi_writeback *wb = &inode_to_bdi(inode)->wb;
16401da177e4SLinus Torvalds 	struct writeback_control wbc = {
16411da177e4SLinus Torvalds 		.nr_to_write = LONG_MAX,
164218914b18SMike Galbraith 		.sync_mode = sync ? WB_SYNC_ALL : WB_SYNC_NONE,
1643111ebb6eSOGAWA Hirofumi 		.range_start = 0,
1644111ebb6eSOGAWA Hirofumi 		.range_end = LLONG_MAX,
16451da177e4SLinus Torvalds 	};
16461da177e4SLinus Torvalds 
16471da177e4SLinus Torvalds 	if (!mapping_cap_writeback_dirty(inode->i_mapping))
164849364ce2SAndrew Morton 		wbc.nr_to_write = 0;
16491da177e4SLinus Torvalds 
16501da177e4SLinus Torvalds 	might_sleep();
16514f8ad655SJan Kara 	return writeback_single_inode(inode, wb, &wbc);
16521da177e4SLinus Torvalds }
16531da177e4SLinus Torvalds EXPORT_SYMBOL(write_inode_now);
16541da177e4SLinus Torvalds 
16551da177e4SLinus Torvalds /**
16561da177e4SLinus Torvalds  * sync_inode - write an inode and its pages to disk.
16571da177e4SLinus Torvalds  * @inode: the inode to sync
16581da177e4SLinus Torvalds  * @wbc: controls the writeback mode
16591da177e4SLinus Torvalds  *
16601da177e4SLinus Torvalds  * sync_inode() will write an inode and its pages to disk.  It will also
16611da177e4SLinus Torvalds  * correctly update the inode on its superblock's dirty inode lists and will
16621da177e4SLinus Torvalds  * update inode->i_state.
16631da177e4SLinus Torvalds  *
16641da177e4SLinus Torvalds  * The caller must have a ref on the inode.
16651da177e4SLinus Torvalds  */
16661da177e4SLinus Torvalds int sync_inode(struct inode *inode, struct writeback_control *wbc)
16671da177e4SLinus Torvalds {
16684f8ad655SJan Kara 	return writeback_single_inode(inode, &inode_to_bdi(inode)->wb, wbc);
16691da177e4SLinus Torvalds }
16701da177e4SLinus Torvalds EXPORT_SYMBOL(sync_inode);
1671c3765016SChristoph Hellwig 
1672c3765016SChristoph Hellwig /**
1673c691b9d9SAndrew Morton  * sync_inode_metadata - write an inode to disk
1674c3765016SChristoph Hellwig  * @inode: the inode to sync
1675c3765016SChristoph Hellwig  * @wait: wait for I/O to complete.
1676c3765016SChristoph Hellwig  *
1677c691b9d9SAndrew Morton  * Write an inode to disk and adjust its dirty state after completion.
1678c3765016SChristoph Hellwig  *
1679c3765016SChristoph Hellwig  * Note: only writes the actual inode, no associated data or other metadata.
1680c3765016SChristoph Hellwig  */
1681c3765016SChristoph Hellwig int sync_inode_metadata(struct inode *inode, int wait)
1682c3765016SChristoph Hellwig {
1683c3765016SChristoph Hellwig 	struct writeback_control wbc = {
1684c3765016SChristoph Hellwig 		.sync_mode = wait ? WB_SYNC_ALL : WB_SYNC_NONE,
1685c3765016SChristoph Hellwig 		.nr_to_write = 0, /* metadata-only */
1686c3765016SChristoph Hellwig 	};
1687c3765016SChristoph Hellwig 
1688c3765016SChristoph Hellwig 	return sync_inode(inode, &wbc);
1689c3765016SChristoph Hellwig }
1690c3765016SChristoph Hellwig EXPORT_SYMBOL(sync_inode_metadata);
1691